Automatic Clock in in Org Mode
I have stared to use Org’s clocking feature— a feature that helps tracking the time worked on a specific task. As everything in Org mode and Emacs, this feature is enabled through a series of functions, configuration options, variables, and bindings.
After using it for a couple of weeks, I have noticed that I am usually performing two actions one after the other:
- switch the task into “WIP” state
- clock-in
Would it be possible to automatically perform these two actions together? It’s Emacs and I assumed the default answer is yes.
What I currently ended up doing is:
- automatically clock-in when switching a task into “WIP” state
- automatically switching a task to “WIP” state when clocking-in
I do have both of these in place at this time so I don’t have to remember which initial action triggers the complete chain.
Automatically clocking-in when switching a task into specific state
I achieved this functionality by defining a org-after-todo-state-change-hook
hook:
(defun alpo/org-clock-in-wip (&optional STATE)
"Automatically clocks-in a task transitioned to STATE.
The default state is set using alpo/org-state-automatically-clocked."
(interactive)
(when (string= org-state (or STATE alpo/org-state-automatically-clocked))
(org-clock-in)))
What the function does is checking the org-state
which points to the new state of the task with the specific state
for which the clock should be started and if equal clock-in.
Then:
(add-hook 'org-after-todo-state-change-hook 'alpo/org-clock-in-wip)
Automatically switching a task to WIP state when clocking-in
Have I said that Emacs seems to have a function and a bunch of variables/configurations for pretty much every functionality that crossed your mind?
In this case, doing the automatically switch of a task to a specific state
when clocking-in is simply achieved by setting org-clock-in-switch-to-state
which takes either a string or a function. For my need the string was enough:
(setq org-clock-in-switch-to-state "WIP)
And this was all it took to automate clocking-in when moving a task to in progress and to automatically switch a task to in progressing when clocking in.
Well, almost.
There’s a case in which a task ends up having two CLOCK
entries.
Things are working well even when this happend.
Still I’ll look into figuring out how to address it.
Many thanks to all the people who provided ideas and suggestions on Maston! You can see the Mastodon thread.