8

Context When working on a larger document in org-mode I would like to clock the whole work time without gaps. As I usually have a logbook in each section I wonder if it's possible to automatically clock in when moving the cursor into a section and clock out when leaving it.

As far as I know there are only projects that clock out automatically when finishing tasks and that clock in a default clock afterwards. Although I really like the approach I have the feeling that it's not appropriate when writing a longer document whose sections usually get into the finished state very late and where sometimes jumping between section occurs, for example in periods in which only small issues are fixed.

The first time at least I saw such automatic clocking was in Bernt Hansen's org-mode configuration.

Question: How can automatic clocking be implemented, such that there is a clock-in when moving into a section and a clock-out when moving out of a section?

1
  • Do you think one could use keyboard input event listener to check wether the key stroke inserted a new character into the buffer and if yes just clock in if a clock is not already running in this section?
    – Christoph
    Commented Jun 3, 2015 at 10:55

1 Answer 1

3

This seems pretty close. I set this up to work on sections with an AUTOCLOCK property. I don't know a more elegant way to do it than the post-command-hook.

#+BEGIN_SRC emacs-lisp
(defun action-1 ()
  "hook function to run in headings with a clock"
  (and (eq major-mode 'org-mode)
       (not (org-before-first-heading-p)))
  (if (org-entry-get (point) "AUTOCLOCK")
      (org-clock-in)
    (when (marker-buffer org-clock-marker) (org-clock-out))))

(add-hook 'post-command-hook 'action-1)
#+END_SRC

EDIT 12-17-2020 The solution above seems to have quit working at some time. Here is a more sophisticated approach that seems to work today.

(defun in-same-heading-as-clock-p ()
  "Check if the cursor is in the same heading as the current clock.
That means:
1. There is a current clock
2. The cursor is in the same buffer as that clock.
3. The cursor is in the same heading as that clock."
  (let ((cb (current-buffer))
    (clockb (marker-buffer org-clock-marker))
    clock-hb
    cursor-hb)

    (when (and
       clockb             ; clock buffer
       ;; clock buffer is the same as this buffer
       (eq cb clockb))
      (setq clock-hb (save-excursion
               (goto-char (marker-position org-clock-marker))
               (org-back-to-heading t)
               (point))
        cursor-hb (save-excursion
            (org-back-to-heading t)
            (point)))
      (= cursor-hb clock-hb))))

(defun action-1 ()
  (cond
   ;; clock is running in this heading, do nothing
   ((in-same-heading-as-clock-p)
    nil)

   ;; clock is running in another heading. IF this heading has autoclock
   ((and (marker-buffer org-clock-marker)
     (not (in-same-heading-as-clock-p)))
    ;; first clock out
    (org-clock-out)
    (when (org-entry-get (point) "AUTOCLOCK")
      (org-clock-in)))
   ;; no clock is running, and 
   ((and (null (marker-buffer org-clock-marker))
     (org-entry-get (point) "AUTOCLOCK"))
    (org-clock-in))))


(add-hook 'post-command-hook 'action-1)
4
  • That's great I'll check it out tomorrow!
    – Christoph
    Commented Apr 27, 2016 at 13:42
  • Thanks, me too I was looking for exactly this. Setting "INHERIT" to t, like (if (org-entry-get (point) "AUTOCLOCK" t), makes it possible that only level-1-headings have to have the AUTOCLOCK tag (in case someone, like me, wants to clock in on any heading).
    – purlupar
    Commented Dec 11, 2020 at 12:25
  • @John-Kitchin, do you also experience org inserting a new clocking line (wiht 0 minutes sum) on each single character that is edited (typed into) a headline, while typing?
    – purlupar
    Commented Dec 15, 2020 at 15:59
  • yes, I see that now. I guess something has changed maybe since I wrote that solution. Commented Dec 16, 2020 at 23:16

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.