Saving Time with Org Mode

I’m an advocate for simple, future-proof technologies, which is why I prefer Emacs’ Org Mode and UTF-8 text for data storage, paired with an extensible Lisp environment for various tasks. Org Mode is not just a feature but a powerhouse, enabling users to manage schedules, author content, track projects, and much more—all within one platform.

In the realm of business, tracking is crucial, whether it’s inventory, staff, or revenue. Org Mode excels here too. I use it to monitor projects and hours, generating reports and invoices with its clock table feature. This post isn’t a deep dive into clock tables but a look at making their usage more efficient.

A pain point was the manual update of clock table reports—time-consuming and cumbersome. Inspired by an article by Alain M. Lafon and Phil Hofmann, I sought an automatic solution. Their approach didn’t fit my needs, so I devised my own, leveraging Emacs Lisp for an automatic update upon saving.

Here’s the refined solution:

(defun autocalc-clocktable ()
  (when (derived-mode-p 'org-mode)
    (save-excursion
      (if (string-equal
           (cadar (org-collect-keywords '("AUTOCALC_CLOCKTABLES")))
           "t")
          (progn
            (org-find-dblock "clocktable")
            (org-fold-show-entry)
            (org-update-dblock))))))

(defun my/org-save-hook ()
  (add-hook 'before-save-hook #'autocalc-clocktable nil :local))

(add-hook 'org-mode-hook #'my/org-save-hook nil t)

This script is designed to update only the first clock table in a buffer. To extend this functionality and update all clock tables, modify the script to employ org-map-dblocks. This function iterates over all dynamic blocks, allowing the update of every clock table in the document, ensuring comprehensive and consistent data management.

This approach is more reliable and doesn’t require manual navigation. Ensure your Org Mode file includes, #+AUTOCALC_CLOCKTABLES: t to enable this feature. Now, each save triggers an automatic clock table recalculation.