From 2d62c0e173dabc4ef1eb5172ceae32407e6049be Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Tue, 1 Sep 2015 10:29:33 +0200 Subject: [PATCH] org-clock: Fix `org-clock-remove-empty-clock-drawer' * lisp/org-clock.el (org-clock-drawer-name): New function. (org-clock-find-position): Use new function. (org-clock-remove-empty-clock-drawer): Remove drawer actually used for clocks, which may be different from the one used for storing notes. Also, consider that headlines could use different drawer names in the same tree. --- lisp/org-clock.el | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/lisp/org-clock.el b/lisp/org-clock.el index 6de093f7b..5533d4a3b 100644 --- a/lisp/org-clock.el +++ b/lisp/org-clock.el @@ -556,6 +556,13 @@ of a different task.") (mapc (lambda (m) (org-check-and-save-marker m beg end)) org-clock-history)) +(defun org-clock-drawer-name () + "Return clock drawer's name for current entry, or nil." + (let ((drawer (org-clock-into-drawer))) + (cond ((integerp drawer) (org-log-into-drawer)) + ((stringp drawer) drawer) + (t nil)))) + (defun org-clocking-buffer () "Return the clocking buffer if we are currently clocking a task or nil." (marker-buffer org-clock-marker)) @@ -1451,10 +1458,7 @@ line and position cursor in that line." (let* ((beg (line-beginning-position 2)) (end (save-excursion (outline-next-heading) (point))) (org-clock-into-drawer (org-clock-into-drawer)) - (drawer (cond - ((not org-clock-into-drawer) nil) - ((stringp org-clock-into-drawer) org-clock-into-drawer) - (t "LOGBOOK")))) + (drawer (org-clock-drawer-name))) ;; Look for a running clock if FIND-UNCLOSED in non-nil. (when find-unclosed (let ((open-clock-re @@ -1646,18 +1650,19 @@ to, overriding the existing value of `org-clock-out-switch-to-state'." (add-hook 'org-clock-out-hook 'org-clock-remove-empty-clock-drawer) -(defun org-clock-remove-empty-clock-drawer nil - "Remove empty clock drawer in the current subtree." - (let ((clock-drawer (org-log-into-drawer)) - (end (save-excursion (org-end-of-subtree t t)))) - (when clock-drawer - (save-excursion - (org-back-to-heading t) - (while (and (< (point) end) - (search-forward clock-drawer end t)) - (goto-char (match-beginning 0)) - (org-remove-empty-drawer-at (point)) - (forward-line 1)))))) +(defun org-clock-remove-empty-clock-drawer () + "Remove empty clock drawers in current subtree." + (save-excursion + (org-back-to-heading t) + (org-map-tree + (lambda () + (let ((drawer (org-clock-drawer-name)) + (case-fold-search t)) + (when drawer + (let ((re (format "^[ \t]*:%s:[ \t]*$" (regexp-quote drawer))) + (end (save-excursion (outline-next-heading)))) + (while (re-search-forward re end t) + (org-remove-empty-drawer-at (point)))))))))) (defun org-clock-timestamps-up (&optional n) "Increase CLOCK timestamps at cursor. -- 2.11.4.GIT