2013-05-25 Clocking in from anywhere (en)

OK, I’ll admit it. I’m an Org-mode fan. (Is there any serious Emacs user nowadays who isn’t one - or at least wasn’t at some point in time?) One of the things I like in Org-mode is clocking. I’m not clocking every single minute of my life, not even of my work; but I use it more and more.

One thing I found quite annoying is that I couldn’t use C-u C-c C-x C-i (org-clock-in with a prefix argument) in any buffer, not necessarily when in Org-mode. When preceded by a C-u, it doesn’t require the point to be on the item to clock in to, so it is perfectly reasonable (from usability point of view) to be able to call it from outside Org-mode (e.g., from LaTeX-mode buffers, which is often the case with me). It’s a pity you can’t do that…

(defun org-clock-in-anywhere (&optional select)
  "Clock in.  If called without prefix, check whether we are in an org-mode buffer first."
  (interactive "P")
  (if select
      (org-clock-in select)
    (if (equal major-mode 'org-mode) (org-clock-in) (error "Not in Org-mode"))))
    
(global-set-key (kbd "C-c C-x C-i") 'org-clock-in-anywhere)
(global-set-key (kbd "C-c C-x C-o") 'org-clock-out)
(global-set-key (kbd "C-c C-x C-j") 'org-clock-goto)
(global-set-key (kbd "C-c C-x C-x") 'org-clock-cancel)

…but I can;).

Notice that the then-part of the else-part of the if should actually never be called, since C-c C-x C-i is bound to org-clock-in in Org-mode, which shadows the global definition anyway. Is it good or bad style? I don’t know, maybe some serious Elisp hacker will enlighten me.

Notice also how I bound the other clock-related commands to their usual Org-mode keybindings in the global keymap. Quite handy.

Remark. After writing this, I installed a newer Org-mode (v8) and realized that it makes this functionality more or less obsolete thanks to its org-clock-in-last command, bound to C-c C-x C-x by default. I decided to share this anyway, since it seems to be a good example of how easy it is to make simple customizations of the Emacs environment, even though binding org-clock-in-last globally is much better idea.

CategoryEnglish, CategoryBlog, CategoryEmacs