3

When working with files changed on disk, there are two warnings

  • ... changed on disk; really edit the buffer?
  • ... has changed since visited or saved. Save anyway?

They're useful, but get very impractical when switching branches fort and back (as I commonly do with git).

Is it possible to make Emacs determine that the content hasn't actually changed (or has been restored to a state equals to what's in its Emacs buffer)?


The fix given in the nearly-duplicate solves the first part only. As @Stefan wrote, the second is easy using (set-visited-file-modtime).

3
  • possible duplicate emacs.stackexchange.com/questions/21137/…
    – JeanPierre
    Commented Aug 25, 2016 at 16:28
  • FWIW, I think it'd be good to have such a behavior in Emacs, so I'd welcome a patch to Emacs which does that cleanly. It might be easier to make it work well by patching Emacs than with a hack to ask-user-about-supersession-threat.
    – Stefan
    Commented Aug 28, 2016 at 22:55
  • @Stefan A patch would be fine, but I'm not good enough in "emacsing" yet. Anyway, I'd suggest to rewrite the 50 line monster called basic-save-buffer first.
    – maaartinus
    Commented Aug 28, 2016 at 23:41

2 Answers 2

1

You might be able to tweak ask-user-about-supersession-threat to just call set-visited-file-modtime when the buffer and the file have the same contents, so that Emacs refreshes its notion of "which was the state of the file when I read it".

Checking "same contents" basically involves saving the buffer into an auxiliary file, and then comparing the two files (or loading the file into a new buffer and comparing the two buffers).

1

@Stefan's answer solved it, I'm just adding the details. I'm using the following modification of compare-buffer-with-file from the related question:

(save-restriction
  (widen)
  (let ((original-buffer (current-buffer))
        (original-size (point-max)))
    (and
     (with-temp-buffer
       (insert-file-contents filename)
       (zerop (compare-buffer-substrings original-buffer 1 original-size
                                        (current-buffer) 1 (point-max))))
     (progn (set-visited-file-modtime) t)))))

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.