1

is it possible to disallow modification if there is an autosave version? I sometimes save a file only to find out that it has an autosave version but I am unable to recover because I saved.

1

One can derive how to detect existing autosave data from the part of after-find-file that emits the message "%s has auto save data; consider M-x recover-this-file".

Just enter read-only-mode in find-file-hook if a buffer has autosave data.

(defun my-make-auto-saved-file-read-only ()
  "Set the buffer read-only if it has a recent auto-save file."
  ;; Stolen from `after-find-file':
  (when (and 
     ;; No need to warn if buffer is auto-saved
     ;; under the name of the visited file.
     (not (and buffer-file-name
           auto-save-visited-file-name))
     (file-newer-than-file-p (or buffer-auto-save-file-name
                     (make-auto-save-file-name))
                 buffer-file-name))
    (read-only-mode)))

(add-hook 'find-file-hook #'my-make-auto-saved-file-read-only)

Note, with this approach you must reset read-only-mode yourself after reverting the file. But that is very simple in Emacs with GUI. Just click on the remaining % in the mode line.

1
  • thanks a bunch. This should really be default behavior. – Joann Morris Feb 4 '20 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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