2

I'm clocking the time spent on daily tasks like email and for that I have a "Daily Routine" TODO entry. Clocking in this task adds a new clock line everyday, leading to a very populated CLOCK drawer. I'd like to limit its size by either a maximum number of clock entries by removing the older ones or better, removing the entries older than some date. Ideally, I'd like to find a function that tidies the clock drawer of an entry, to be added to the clock-in or clock-out hook.

Any idea how to achieve that ?

2 Answers 2

1

Have a look at org-clock-remove-empty-clock-drawer. It uses org-map-tree and a regular expression based on org-clock-drawer-name to find the :LOGBOOK: drawers.

You can use the same logic to get point to the start of :LOGBOOK: and then handle all CLOCK: entries on a line-by-line basis. To parse the timestamps, you can use org-ts-regexp-both and org-element-timestamp-parser. Afterwards, you can use kill-whole-line to get rid of the entry.

So in short:

  1. Find the next :LOGBOOK: via re-search-forward
  2. Go forward a single line
  3. Check the start of the line.
    • If it starts with :END:, stop the clock handling for the current drawer
    • If it starts with CLOCK:, search for the next timestamp.
      1. If it's too old, remove the whole line with kill-whole-line.
      2. Otherwise continue to the next line
    • goto 3.
1
  • Thanks, those are very good pointers. I'll try and come up with something.
    – wilk
    Commented Mar 24, 2021 at 22:16
0

Here is what I came up with:

(defun jc-org-clock-remove-old-clock-entries (n)
  "Remove clock entries whose end is older than N weeks in current subtree.
Skip over dangling clock entries."
  (interactive "nnumber of weeks: ")
  (save-excursion
    (org-back-to-heading t)
    (org-map-tree
     (lambda ()
       (let ((drawer (re-search-forward org-clock-drawer-start-re (save-excursion (org-end-of-subtree)) t))
         (case-fold-search t))
     (when drawer
       (let ((re org-clock-line-re)
         (end (save-excursion (re-search-forward org-clock-drawer-end-re (save-excursion (org-end-of-subtree)) nil)))
         )
         (while (re-search-forward re end t)
           (skip-chars-forward " \t\r")
           (looking-at org-tr-regexp-both)
           (when (>= (/ (time-to-number-of-days (org-time-since (match-string-no-properties 2))) 7) (float n)) (kill-whole-line))
           ))))))
    (org-clock-remove-empty-clock-drawer)
    ))

It should work if your clocking uses either a specific :CLOCK: drawer or the default :LOGBOOK: drawer, according to the value of the org-clock-into-drawer variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.