When I create appointments in Emacs diary via the calendar, I have to save changes to the diary file before being able to exit the calendar. I have tried to set up Emacs to automatically save the diary file once changes have been made with this code:

(add-hook 'diary-mode-hook
          (lambda ()
            (add-hook 'auto-save-hook 'diary-show-all-entries nil t)

However, hitting q in calendar still prompts me with these words:

Diary modified; do you really want to exit the calendar? (y or n) 

This from calendar.el:

(defun calendar-exit (&optional kill)
  "Get out of the calendar window and hide it and related buffers."
  (interactive "P")
  (let ((diary-buffer (get-file-buffer diary-file))
        (calendar-buffers (calendar-buffer-list)))
    (when (or (not diary-buffer)
              (not (buffer-modified-p diary-buffer))
               "Diary modified; do you really want to exit the calendar? "))
      (if (and calendar-setup (display-multi-frame-p))
          ;; FIXME: replace this cruft with the `quit-restore' window property
          (dolist (w (window-list-1 nil nil t))
            (if (and (memq (window-buffer w) calendar-buffers)
                     (window-dedicated-p w))
                (if (eq (window-deletable-p w) 'frame)
            (if calendar-remove-frame-by-deleting
            (delete-frame (window-frame w))
              (iconify-frame (window-frame w)))
          (quit-window kill w))))
        (dolist (b calendar-buffers)
          (quit-windows-on b kill))))))

I would like to be able to save the diary file automatically and exit calendar without confirmation.

  • 1
    You can add a before advice to calendar-exit to save the diary: do C-h i g (elisp)Advising RET for more information.
    – NickD
    Feb 22, 2021 at 18:20
  • I get user-error: No such node or anchor: Advising
    – Edman
    Feb 23, 2021 at 2:41
  • 1
    You can get there from the Elisp section, in Advising Functions. Or just go to the documentation on the web: gnu.org/software/emacs/manual/html_node/elisp/… What you're looking for is something like (advice-add 'calendar-exit :before 'my/save-calendar-before-exit) Feb 23, 2021 at 3:46
  • 1
    Do you have the Emacs Lisp documentation installed? If you say C-h i do you see the Elisp manual? If not, you should install it. You could use the online docs as @BrianCully suggests, but that is no substitute for using Info in emacs itself.
    – NickD
    Feb 23, 2021 at 13:59
  • I will install it and revert if necessary. Thanks.
    – Edman
    Feb 23, 2021 at 14:07

1 Answer 1


Here's a (lightly tested) implementation along the lines in the comments:

(defun my/save-diary-before-calendar-exit (_)
   (let ((diary-buffer (get-file-buffer diary-file)))
        (or (not diary-buffer)
            (not (buffer-modified-p diary-buffer))
            (with-current-buffer diary-buffer (save-buffer)))))

(advice-add 'calendar-exit :before #'my/save-diary-before-calendar-exit)

The tests are cribbed from calendar-exit itself: get the buffer of the diary file and if the buffer exists and is modified, save it.

If you want to make this permanent, you need to add the code above to your init file.

And if at any point you want to remove the advice, say:

(advice-remove 'calendar-exit #'my/save-diary-before-calendar-exit)
  • This is awesome. Thanks so much.
    – Edman
    Feb 24, 2021 at 8:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.