2

When I have unsaved buffers and I kill Emacs with save-buffers-kill-terminal (C-x C-c) I'm asked whether to save each buffer. Is it possible to delete a buffer's auto-save data when I answer n?

I had tried to add a function that deletes the auto-save data to kill-emacs-hook, but that would run even if I killed Emacs from the terminal, when the auto-save data would actually be useful.

Arch Stanton
  • 1,525
  • 9
  • 22
  • Consider either advising `save-buffers-kill-terminal` or looking for hooks which get triggered when a frame is deleted, but that would require checks to see if there any other frames left and whether Emacs is running as a daemon. –  May 02 '18 at 10:07
  • To simply delete one or more recovery files, try `M-x recover-session`, mark one or more sessions with `d`, and then `x` to execute and `y` to confirm. Not exactly what the question asked, but searches for it may lead here :) – David Lord Dec 09 '20 at 21:51

1 Answers1

1

I think the problem is that save-buffers-kill-emacs asks that yes-or-no question. When you're asked that you must either answer yes or no or use C-g to quit (in this case, quit exiting).

You could advise or redefine save-buffers-kill-emacs to accommodate what you want. Or you could just define a command that you use after using C-g to quit exiting.

That command would iterate over the unsaved buffers, asking you whether you want to delete an auto-save file. (Or if you always want to delete auto-save files for all unsaved buffers, the command would just do that.)

But something like this is what I'd suggest doing:

(add-to-list 'kill-emacs-query-functions #'my-confirm-kill-emacs)

(defun my-confirm-kill-emacs ()
  "Get confirmation..."
  (let ((bufs  (cl-remove-if-not (lambda (buf)                                       
                                   (and (buffer-file-name buf)
                                        (buffer-modified-p buf)))

                                 (buffer-list)))
        autosave-name)
    (and bufs
         (dolist (buf  bufs)
           (setq autosave-name  (with-current-buffer buf (make-auto-save-file-name)))
           (when (and (file-exists-p autosave-name)
                      (y-or-n-p (format "Delete autosave file `%s' " autosave-name)))
             (delete-file autosave-name)
             (message "File `%s' deleted" autosave-name) (sit-for 0.5)))))
  t)                               ; Return non-nil so Emacs can quit.

(Be sure to test any such code in an Emacs session with files you don't really care about. ;-))

Drew
  • 75,699
  • 9
  • 109
  • 225