On Wed, 17 Aug 2022 at 15:14, Ihor Radchenko <yanta...@gmail.com> wrote:
> >> Yikes! Then, can also check for window-minibuffer-p, but I feel that it
> >> will be a fight against all kinds of edge cases.
> >
> > I haven't been able to make much progress on this bug in the last two
> > weeks. Will try to send a patch in a few days, as the steps I gave above
> > feels like an edge case which we won't see happening hopefully.
>
> Your patch will be already an improvement.

I'm attaching the patch for the current approach we discussed in this
thread. I have tried basic operations like taking note on state change,
and have tried with recurring entries which need more than 10 cycles to
be marked as done (original bug report).

As I was changing the indentation of code from org-add-log-note, I converted
some tabs to spaces, is it okay to do so?

-- 
Bhavin Gandhi (bhavin192) | https://geeksocket.in
From afb1158d9a360020f093436228cdf4e689474db8 Mon Sep 17 00:00:00 2001
From: Bhavin Gandhi <bhavin...@geeksocket.in>
Date: Sun, 25 Sep 2022 22:06:53 +0530
Subject: [PATCH] org.el: Make sure `org-add-log-note' runs at the end of Org
 command
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* lisp/org.el (org-add-log-setup): Save `this-command' and
`recursion-depth' before adding the `org-add-log-note'to
post-command-hook.
(org-add-log-note): Execute only if the current `(recursion-depth)'
and `this-command' are same as the ones we saved during the log-setup.

This change tries to make sure that we run the `org-add-log-note' only
after the current Org command has finished executing. Previously, the
post-command-hook was getting triggered if the Org command in turn
runs some other command.

Fixes the bug originally reported by Michael Powe.

Bhavin Gandhi. [BUG] org-auto-repeat-maybe: error "Can’t expand
minibuffer to full frame" and missing log note.
Sat, 18 Jun 2022 23:30:50 +0530.
https://list.orgmode.org/CAOn=hbcsoco++we0xgrhfoxxcexrocpygd1ncjzkyy-9lck...@mail.gmail.com/

Relevant discussion on bug-gnu-emacs: https://debbugs.gnu.org/56425
---
 lisp/org.el | 64 ++++++++++++++++++++++++++++-------------------------
 1 file changed, 34 insertions(+), 30 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index b63aaed4f..ba20df59f 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -10355,6 +10355,8 @@ EXTRA is additional text that will be inserted into the notes buffer."
 	org-log-note-how how
 	org-log-note-extra extra
 	org-log-note-effective-time (org-current-effective-time)
+        org-log-note-this-command this-command
+        org-log-note-recursion-depth (recursion-depth)
         org-log-setup t)
   (add-hook 'post-command-hook 'org-add-log-note 'append))
 
@@ -10383,37 +10385,39 @@ EXTRA is additional text that will be inserted into the notes buffer."
 
 (defun org-add-log-note (&optional _purpose)
   "Pop up a window for taking a note, and add this note later."
-  (remove-hook 'post-command-hook 'org-add-log-note)
-  (setq org-log-setup nil)
-  (setq org-log-note-window-configuration (current-window-configuration))
-  (delete-other-windows)
-  (move-marker org-log-note-return-to (point))
-  (pop-to-buffer-same-window (marker-buffer org-log-note-marker))
-  (goto-char org-log-note-marker)
-  (org-switch-to-buffer-other-window "*Org Note*")
-  (erase-buffer)
-  (if (memq org-log-note-how '(time state))
-      (org-store-log-note)
-    (let ((org-inhibit-startup t)) (org-mode))
-    (insert (format "# Insert note for %s.
+  (when (and (equal org-log-note-this-command this-command)
+             (= org-log-note-recursion-depth (recursion-depth)))
+    (remove-hook 'post-command-hook 'org-add-log-note)
+    (setq org-log-setup nil)
+    (setq org-log-note-window-configuration (current-window-configuration))
+    (delete-other-windows)
+    (move-marker org-log-note-return-to (point))
+    (pop-to-buffer-same-window (marker-buffer org-log-note-marker))
+    (goto-char org-log-note-marker)
+    (org-switch-to-buffer-other-window "*Org Note*")
+    (erase-buffer)
+    (if (memq org-log-note-how '(time state))
+        (org-store-log-note)
+      (let ((org-inhibit-startup t)) (org-mode))
+      (insert (format "# Insert note for %s.
 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
-		    (cl-case org-log-note-purpose
-		      (clock-out "stopped clock")
-		      (done  "closed todo item")
-		      (reschedule "rescheduling")
-		      (delschedule "no longer scheduled")
-		      (redeadline "changing deadline")
-		      (deldeadline "removing deadline")
-		      (refile "refiling")
-		      (note "this entry")
-		      (state
-		       (format "state change from \"%s\" to \"%s\""
-			       (or org-log-note-previous-state "")
-			       (or org-log-note-state "")))
-		      (t (error "This should not happen")))))
-    (when org-log-note-extra (insert org-log-note-extra))
-    (setq-local org-finish-function 'org-store-log-note)
-    (run-hooks 'org-log-buffer-setup-hook)))
+                      (cl-case org-log-note-purpose
+                        (clock-out "stopped clock")
+                        (done  "closed todo item")
+                        (reschedule "rescheduling")
+                        (delschedule "no longer scheduled")
+                        (redeadline "changing deadline")
+                        (deldeadline "removing deadline")
+                        (refile "refiling")
+                        (note "this entry")
+                        (state
+                         (format "state change from \"%s\" to \"%s\""
+                                 (or org-log-note-previous-state "")
+                                 (or org-log-note-state "")))
+                        (t (error "This should not happen")))))
+      (when org-log-note-extra (insert org-log-note-extra))
+      (setq-local org-finish-function 'org-store-log-note)
+      (run-hooks 'org-log-buffer-setup-hook))))
 
 (defvar org-note-abort nil) ; dynamically scoped
 (defun org-store-log-note ()
-- 
2.37.3

Reply via email to