0

I installed 28.2 on Monterey via Homebrew (as a cask). I have desktop-save-mode set; auto-save-* options are all default.

Recently, I restarted the computer for updates; apparently I had somehow not saved an important Org file beforehand (although the changes were made the day before and I typically save several times a day). The unsaved state was discarded without warning before restart or offering to restore anything when the file was automatically re-opened afterward, and there is no sufficiently recent backup or auto-save file. (There are backup files, #agenda.org#, agenda.org~, and agenda.org~crash. The first also lacks the critical data, although fairly recent; the other two are months stale, presumably because backup is inhibited due to technically being in Git.)

After carefully reading the relevant manual section, it looks like I got confused by the redundant prompts to restore the auto-save (one phrased to restore the file itself agenda.org, y/n/!/q/?, and the other phrased to restore the autosave, #agenda.org#, yes/no — restore from what? and to what?), causing it to helpfully drop it on the floor. (All other editor packages I've used somehow manage to avoid asking any questions at all when handling autosaves and crash recovery, or needing complicated manual sections for those, but I guess I should have known what I was getting into.)

How do I get Emacs to not drop bits on the floor, ever, without simply giving up all control over file save timing, or needing to fight my way through a checklist to make sure I don't avoid not forgetting to restore the wrong version?

Nathan Tuggy
  • 111
  • 5
  • 1
    Do you think there's a way in which desktop-save-mode and auto-saves interact badly, such that data loss occurs? If so, it would be good to try to establish a recipe for reproducing that, as I think a bug report would then be desirable. – phils Mar 08 '23 at 02:01

1 Answers1

2

Enable the vc-make-backup-files user option to get normal backups despite version-control.

The following is essentially untested (I've just written it), but seems like it plugs a hole in the standard backup system for long-running sessions (namely that Emacs creates a new backup file only when you first save a buffer during a given session).

You would almost certainly want to enable the version-control user option if you were to use this code.

(defvar my-backup-buffer-interval (* 60 60) ;; 1 hour.
  "Time threshold in seconds for resetting `buffer-backed-up' in a buffer.")

(defvar-local my-backup-buffer-timestamp nil
  "When the current buffer was last backed up by `backup-buffer'.")

(define-advice backup-buffer (:after () my-set-timestamp)
  "Update `my-backup-buffer-timestamp'.  Advice for `backup-buffer'."
  (setq-local my-backup-buffer-timestamp (current-time)))

(define-advice save-buffer (:before (&optional arg) my-backup-threshold)
  "Maybe clear `buffer-backed-up', based on `my-backup-buffer-timestamp'.
Advice for `save-buffer'."
  (when (and buffer-backed-up
             (or (not my-backup-buffer-timestamp)
                 (> (time-to-seconds (time-since my-backup-buffer-timestamp))
                    my-backup-buffer-interval)))
    (setq-local buffer-backed-up nil)))

If you want to disable the advice:

(advice-remove 'backup-buffer 'backup-buffer@my-set-timestamp)
(advice-remove 'save-buffer 'save-buffer@my-backup-threshold)
phils
  • 48,657
  • 3
  • 76
  • 115
  • https://github.com/lewang/backup-walker would likely be a good complement as well. – phils Mar 07 '23 at 04:11
  • Now slightly tested, and it seems to work. Note that backups are still subject to `C-h v backup-enable-predicate` and `C-h v backup-inhibited` (the latter being set buffer-locally if the former says that the buffer shouldn't have backups). – phils Mar 07 '23 at 11:36
  • Additional recommended reading: https://stackoverflow.com/questions/151945/how-do-i-control-how-emacs-makes-backup-files and https://stackoverflow.com/questions/22775098/how-to-configure-emacs-to-save-backup-for-files-under-temp-directory – phils Mar 07 '23 at 11:42
  • And of course, the user manual: `C-h i g (emacs)Backup` – phils Mar 07 '23 at 11:43
  • Correct me if I'm wrong, but with this code, backups would only be created when I actually save the file, yes? I thought I had, but obviously I didn't. – Nathan Tuggy Mar 07 '23 at 16:40
  • Yes, backups are always only created when saving the file. (If you're not saving the file, there's no reason to make a back-up of the previous version of that file.) – phils Mar 07 '23 at 21:35
  • Un-saved buffer changes are supposed to be accounted for via the auto-save system, rather than the backup system. Your `#agenda.org#` file would be an example of an auto-save file. Refer to `C-h i g (emacs)Auto Save` – phils Mar 07 '23 at 21:36
  • Thanks for the tips. Looks like I probably managed to get confused by the weird prompts, so I've updated the question accordingly. – Nathan Tuggy Mar 07 '23 at 22:56