Emacs Calendar And Org Mode

Emacs Calendar And Org Mode

Org Mode integrates quite nicely with Emacs' calendar but the two have rather different views on how to format dates. Let's adjust the calendar to follow the Org Mode conventions: ISO:8601 dates with a three letter abbreviated day name. Note that ISO:8601 uses Monday as the first day of the week and does not abbreviate the year (remember Y2K?).

(require 'calendar)

(setq calendar-week-start-day    1      ; 0 == Sunday
      calendar-date-style       'iso
      abbreviated-calendar-year 'nil)

(setq calendar-date-display-form
      '((format "%s-%.2d-%.2d"
                year (string-to-number month) (string-to-number day))
        (if dayname (format " %s" (substring dayname 0 3)))))

The same ISO standard stipulates a 24-hour clock and allows several ways of formatting the time. The format used by Org Mode corresponds to one of these, so we're good. All we have to do is make the Emacs' calendar use the same formatting for display.

(setq calendar-time-display-form
      '(24-hours ":" minutes            ; both always two digits
                 (if time-zone (format "+%s" time-zone))))

As a final touch, we add some key bindings that let us move around the calendar using the same key bindings as Org Mode uses to change dates at its date/time prompt.

(define-key calendar-mode-map [M-S-left]  'calendar-backward-month)
(define-key calendar-mode-map [M-S-right] 'calendar-forward-month)
(define-key calendar-mode-map [M-S-up]    'calendar-backward-year)
(define-key calendar-mode-map [M-S-down]  'calendar-forward-year)

A use-package based variant of the above can be found in my dotter/pim module.