Is there a way to reset the buffer-modified flag when buffer is equal to the file but is marked as modified? In this case I want emacs don't ask me for save.
2
votes
3 Answers
2
votes
Given you have diff installed, this will do it:
(defun my-update-modified-flag ()
"Update the buffer modified flag."
(interactive)
(let* ((buffer (current-buffer))
(basefile
(or (buffer-file-name buffer)
(error "Buffer %s has no associated file" buffer)))
(tempfile (make-temp-file "buffer-content-")))
(with-current-buffer buffer
(save-restriction
(widen)
(write-region (point-min) (point-max) tempfile nil 'silent)))
(if (= (call-process "diff" nil nil nil basefile tempfile) 0)
(progn
(set-buffer-modified-p nil)
(message "Buffer matches file"))
(message "Buffer doesn't match file"))
(delete-file tempfile)))
1
votes
0
votes
Inspired by @scottfrazer's answer, I wrote a set of functions to automatically check if buffers that are associated with files should be updated to 'unmodified':
unmodified-buffer.el
A few improvements on the original code are:
- Compare file size first to avoid unnecessary run of diff/creation of temp files;
- Limit size of files to be compared to 50kb for good performance;
- Hooks to automatically perform state updates when necessary;
- Timer calls to make checking run only after an idle time period;
combine-after-change-callsso that Emacs can handle'after-change-functionshook more efficiently.
To be honest I am not so experienced with Elisp although I have been hacking with Emacs for a few years. I would appreciate very much the community's feedback to help improve this. Hope this can help!