2025-09-15 Entering dates in Emacs Calc

I perform date arithmetic pretty often in Emacs Calc. One problem I’ve always had with that is that entering date forms is very far from ergonomic. (To be fair to Calc, once you get used to how Org mode allows to enter dates, basically any other way of entering them feels slow and inconvenient. This is what Org manual says about it: “The actions of the date/time prompt may seem complex, but I assure you they will grow on you, and you will start getting annoyed by pretty much any other way of entering a date/time out there” – and boy, is it true!)

So, it occurred to me that it would be great to be able to use the Org date prompt in Calc. Of course, this is Emacs, so it shouldn’t be difficult, right?

I started with pressing C-h c and C-h k in Calc and instrumenting Calc functions for Edebug to see which functions I could use. This led me to functions like calc-push and math-read-expr, which would work perfectly fine for my use-case. However, in parallel I asked an LLM how to programmatically push something to Emacs Calc stack (fully expecting gibberish!), and to my surprise, I actually got a reasonable answer. The LLM first told me about calc-push and then reminded me about calc-eval, which I already knew how to use but completely forgot! (I think one of the reasons the answer was so good is that Calc manual actually does document Calc’s internals pretty well.)

So, the only things that remained were to actually code this and find a suitable keybinding. Let’s do the coding first.

(defun calc-enter-date-with-org ()
  "Use `org-read-date' to read a date form and push it onto the Calc stack."
  (interactive)
  (let ((org-read-date-prefer-future nil))
    (calc-eval (format "<%s>" (org-read-date)) 'push)))

(Note that while Calc allows for second resolution of the date forms, org-read-date apparently cannot go below a minute.)

Now for the keybinding. Calc uses angle brackets for date forms, very similar to Org (active) timestamps. It turns out that C-c . (the Org key sequence used for org-timestamp, which inserts an active timestamp at point) is not allocated in Calc mode (which is not surprising given that according to Emacs key binding conventions it is reserved for minor modes – Org does not comply with those conventions, but given that it’s a huge application more than just a “major mode”, it is completely forgivable). So, let’s just put this in my init.el:

(keymap-set calc-mode-map "C-c ."
            'calc-enter-date-with-org)

and we’re good to go! (Of course, this way I have violated the convention, too. But it’s completely ok – it’s my init.el, I can do whatever I want. It would be completely different if I was writing a major mode to be distributed.)

Now, witness the power of Org date prompt in your Calc date calculations;-)!

CategoryEnglish, CategoryBlog, CategoryEmacs, CategoryOrgMode