Making Emacs Autosaving of Files Ignore Email Drafts

I found these fellas, and many more, all of a sudden.

This week I noticed that a ton of files like *message*-20201029-134012 piled up in my home folder. These are email drafts from message-mode, and the numbers are a date-time-stamp.

These didn’t appear ever before. So I figured it might have something to do with autosaving after looking at the change history of my configuration file – and it sure does.

I had enabled auto-save-visited-mode in emacs some time last month. It saves the files you open (“visit”) in Emacs automatically to disk. That’s a fairly new feature since version 26.1: before, you had auto-save-mode, but that would auto-save to a temporary copy of the file you looked at.

Normally, I’d deactivate the autosave feature for email drafts, and that’d be it. But auto-save-visited-mode is a global mode. You can turn it on or off, but the setting affects all buffers for all files. Meh.

The real-auto-save package is a bit more clever. It has the same effect, but can be activated on a per-buffer (or per-file) basis.

I just created a PR on GitHub to create a global mode so that I don’t have to selectively enable real-auto-save. I want to have it on by default, and deactivate for e.g. message-mode.

I’m no Emacs Lisp wiz, but this was pretty simple (after looking at how existing packages do such things).

I basically executed this code to test if a global mode would work, and then submitted a PR with the relevant changes:

;; Load the package
(require 'real-auto-save)
(setq real-auto-save-interval 10) ;; in seconds

;; Helpers to enable/disable the note per-buffer
;; (so I don't have to use lambdas)
(defun turn-on-real-auto-save () (real-auto-save-mode 1))
(defun turn-off-real-auto-save () (real-auto-save-mode -1))

;; Here's the kicker: add a global mode!
(define-globalized-minor-mode global-real-auto-save-mode
  real-auto-save-mode turn-on-real-auto-save)

;; Enable globally
(global-real-auto-save-mode 1)

;; Disable for message-mode
(add-hook 'message-mode-hook #'turn-off-real-auto-save)

In my layman understanding, define-globalized-minor-mode takes 3 arguments:

  1. The global mode’s name
  2. The corresponding local mode’s name (which has no effect on it’s own apart from the name association?)
  3. The global mode execution function (which actually turns this on)

The global minor modes are enabled first, then the hook for having an email buffer open is executed, and I don’t get the autosaving files anymore. Nice!

I have to figure out why the drafts (1) appeared in these locations, since they don’t match any of my configuration, and (2) why they didn’t auto-delete when sending the email.