Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jacott/3191131 to your computer and use it in GitHub Desktop.
Save jacott/3191131 to your computer and use it in GitHub Desktop.
Fix Emacs thinking a file has changed when its contents haven't only its modification time. Useful when using git checkout
(defun update-visited-file-modtime-if-unchanged ()
"Check the contents of all file buffers with changed file-modtime to see if they have really changed.
If not `set-visited-file-modtime' to match file."
(interactive)
(with-temp-buffer
(buffer-disable-undo)
(let (buf
fn
(cbuf (current-buffer))
(buffers (buffer-list)))
(while buffers
(setq buf (car buffers))
(setq buffers (cdr buffers))
(when (and (setq fn (buffer-file-name buf)) (not (verify-visited-file-modtime buf)) (file-exists-p fn))
(erase-buffer)
(insert-file-contents fn)
(when (= (buffer-size) (buffer-size buf))
(with-current-buffer buf
(save-restriction
(widen)
(when (= 0 (compare-buffer-substrings cbuf 1 (buffer-size) buf 1 (buffer-size)))
(set-visited-file-modtime))))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment