clock: Prevent "before first headline" error on clock-out note

* lisp/org-clock.el (org-clock-out): Possibly create a note at the
very end of the process, when there's no need for point to be on the
correct headline. Also call `org-clock-remove-empty-clock-drawer'
unconditionally instead of putting it in `org-clock-out-hook'.

Reported-by: Dave Dai <wenlong.dai@gmail.com>
<https://yhetil.org/orgmode/87pmwaang6.fsf@HUE.i-did-not-set--mail-host-address--so-tickle-me/»
This commit is contained in:
Nicolas Goaziou 2021-06-27 16:13:46 +02:00
parent e2550506eb
commit 8e3e2f667f
1 changed files with 15 additions and 16 deletions

View File

@ -1676,17 +1676,13 @@ to, overriding the existing value of `org-clock-out-switch-to-state'."
(insert " => " (format "%2d:%02d" h m))
(move-marker org-clock-marker nil)
(move-marker org-clock-hd-marker nil)
;; Possibly remove zero time clocks. However, do not add
;; a note associated to the CLOCK line in this case.
(cond ((and org-clock-out-remove-zero-time-clocks
(= 0 h m))
(setq remove t)
(delete-region (line-beginning-position)
(line-beginning-position 2)))
(org-log-note-clock-out
(org-add-log-setup
'clock-out nil nil nil
(concat "# Task: " (org-get-heading t) "\n\n"))))
;; Possibly remove zero time clocks.
(when (and org-clock-out-remove-zero-time-clocks
(= 0 h m))
(setq remove t)
(delete-region (line-beginning-position)
(line-beginning-position 2)))
(org-clock-remove-empty-clock-drawer)
(when org-clock-mode-line-timer
(cancel-timer org-clock-mode-line-timer)
(setq org-clock-mode-line-timer nil))
@ -1717,11 +1713,14 @@ to, overriding the existing value of `org-clock-out-switch-to-state'."
"Clock stopped at %s after %s => LINE REMOVED"
"Clock stopped at %s after %s")
te (org-duration-from-minutes (+ (* 60 h) m)))
(run-hooks 'org-clock-out-hook)
(unless (org-clocking-p)
(setq org-clock-current-task nil)))))))
(add-hook 'org-clock-out-hook #'org-clock-remove-empty-clock-drawer)
(unless (org-clocking-p)
(setq org-clock-current-task nil))
(run-hooks 'org-clock-out-hook)
;; Add a note, but only if we didn't remove the clock line.
(when (and org-log-note-clock-out (not remove))
(org-add-log-setup
'clock-out nil nil nil
(concat "# Task: " (org-get-heading t) "\n\n"))))))))
(defun org-clock-remove-empty-clock-drawer ()
"Remove empty clock drawers in current subtree."