2

当缓冲区等于文件但被标记为已修改时,有没有办法重置缓冲区修改标志?在这种情况下,我希望 emacs 不要要求我保存。

4

3 回答 3

2

鉴于您已经diff安装,这将做到这一点:

(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)))
于 2012-07-12T13:28:15.450 回答
1

你可以使用这个表达式:

(set-buffer-modified-p nil)

谨防!即使缓冲区确实被修改,它也会清除标志

于 2012-07-12T07:22:21.817 回答
0

受@scottfrazer 回答的启发,我编写了一组函数来自动检查与文件关联的缓冲区是否应更新为“未修改”:

unmodified-buffer.el

对原始代码的一些改进是:

  • 首先比较文件大小以避免不必要的运行差异/创建临时文件;
  • 将要比较的文件大小限制为 50kb 以获得良好的性能;
  • 必要时自动执行状态更新的挂钩;
  • Timer calls to make checking run only after an idle time period;
  • combine-after-change-calls so that Emacs can handle 'after-change-functions hook 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!

于 2019-09-01T01:22:07.020 回答