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?

1
  • 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 8, 2023 at 2:01

1 Answer 1

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)
8
  • github.com/lewang/backup-walker would likely be a good complement as well.
    – phils
    Mar 7, 2023 at 4: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 7, 2023 at 11:36
  • Additional recommended reading: stackoverflow.com/questions/151945/… and stackoverflow.com/questions/22775098/…
    – phils
    Mar 7, 2023 at 11:42
  • And of course, the user manual: C-h i g (emacs)Backup
    – phils
    Mar 7, 2023 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. Mar 7, 2023 at 16:40

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.