Emacs:如何禁用';磁盘上的文件已更改';检查?

Emacs:如何禁用';磁盘上的文件已更改';检查?,emacs,Emacs,如何禁止Emacs检查在编辑器外部更改的缓冲区文件?Emacs在这里确实在帮助您。阅读上的信息页面 但是,如果仍要避免该消息/提示,可以重新定义执行提示的函数: (defun ask-user-about-supersession-threat (fn) "blatantly ignore files that changed on disk" ) (defun ask-user-about-lock (file opponent) "always grab lock" t)

如何禁止Emacs检查在编辑器外部更改的缓冲区文件?

Emacs在这里确实在帮助您。阅读上的信息页面

但是,如果仍要避免该消息/提示,可以重新定义执行提示的函数:

(defun ask-user-about-supersession-threat (fn)
  "blatantly ignore files that changed on disk"
  )
(defun ask-user-about-lock (file opponent)
  "always grab lock"
   t)
第二个函数用于两个人使用Emacs编辑同一个文件,并提供类似的提示(但不是您在问题中提到的提示)

我建议不要覆盖这两个例程,但如果你想的话,它就在那里


当off chance
全局自动恢复模式
打开时,您可以禁用该模式。将此添加到.emacs中:

(global-auto-revert-mode -1)
通过查看相同名称的变量,可以判断模式是否打开:

C-h v global-auto-revert-mode RET

如果值为
t
,则模式为on,否则为off。

我对此很恼火,因为每次我在git中切换分支时,emacs都认为我的所有文件都已更改

帮助您应对这种症状。它允许您重新加载所有缓冲区


您还可以尝试
(全局自动还原模式)
,它将自动将您的文件还原为磁盘上的文件。

我的
.emacs
中有以下内容。这使得Emacs只询问真正更改的文件。如果一个文件保持相同的字节,只更新它的时间戳,就像在VCS中切换分支时经常发生的那样,这个“更改”被Emacs忽略

;; Ignore modification-time-only changes in files, i.e. ones that
;; don't really change the contents.  This happens often with
;; switching between different VC buffers.

(defun update-buffer-modtime-if-byte-identical ()
  (let* ((size      (buffer-size))
         (byte-size (position-bytes size))
         (filename  buffer-file-name))
    (when (and byte-size (<= size 1000000))
      (let* ((attributes (file-attributes filename))
             (file-size  (nth 7 attributes)))
        (when (and file-size
                   (= file-size byte-size)
                   (string= (buffer-substring-no-properties 1 (1+ size))
                            (with-temp-buffer
                              (insert-file-contents filename)
                              (buffer-string))))
          (set-visited-file-modtime (nth 5 attributes))
          t)))))

(defun verify-visited-file-modtime--ignore-byte-identical (original &optional buffer)
  (or (funcall original buffer)
      (with-current-buffer buffer
        (update-buffer-modtime-if-byte-identical))))
(advice-add 'verify-visited-file-modtime :around #'verify-visited-file-modtime--ignore-byte-identical)

(defun ask-user-about-supersession-threat--ignore-byte-identical (original &rest arguments)
  (unless (update-buffer-modtime-if-byte-identical)
    (apply original arguments)))
(advice-add 'ask-user-about-supersession-threat :around #'ask-user-about-supersession-threat--ignore-byte-identical)
;;仅忽略文件中的修改时间更改,即
;; 不要真的改变内容。这种情况经常发生在
;; 在不同的VC缓冲区之间切换。
(如果字节相同()
(let*((大小(缓冲区大小))
(字节大小(位置字节大小))
(文件名缓冲区文件名))
(当(和字节大小(在我的例子中,我想要:

(setq revert-without-query '(".*"))
无查询还原的文档

Specify which files should be reverted without query.
The value is a list of regular expressions.
If the file name matches one of these regular expressions,
then ‘revert-buffer’ reverts the file without querying
if the file has changed on disk and you have not edited the buffer.

在VMWare中运行emacs并在/mnt/hgfs(指向主机文件夹的链接)中编辑文件时,会出现很多问题。VMWare的时钟与主机不同步--有时,您甚至可以看到时间系统调用在时间上向后跳转的结果。因此,…只有在编辑特定目录中的文件时,才可以禁用检查吗?(例如./mnt/hgfs?)在Windows上通过OSX上的并行程序使用Emacs修改本机/主机OSX上的文件时,也需要这种解决方法。除了修改
询问用户有关抑制威胁的问题之外,我还修改了
基本保存缓冲区
,使用了
defalias
和一个省略/删除:
(或(验证访问的文件modtime(当前缓冲区))(非(文件存在-p缓冲区文件名))(是或否-p(格式“%s”在访问或保存后已更改。是否仍保存?”(文件名非目录缓冲区文件名)))(用户错误“保存未确认”))
.Emacs应该真正检查文件内容是否已更改,而不仅仅是时间戳。每次我临时切换git分支并切换回(或git stash save/pop)时,我的所有缓冲区都会发生这种情况.Edit刚刚发现@doublep的答案对于那些与VMWare
/mnt/hgfs
有问题的人来说,看看这个:+1这与我的想法非常相似,但只有当缓冲区没有未保存的更改时它才会正常工作。我想我会维护一个缓冲区SHA1的assoc数组(或MD5或任何便宜到可以即时计算且足以抵御冲突的产品)并在访问新文件或保存文件时进行更新。如果具有更新时间戳的文件具有相同的哈希值,则无需发出警报。是的,正如您所述,它具有未处理的大小写。但这对我来说已经足够了,因为我有经常保存缓冲区的习惯。@doublep you是我的英雄!此功能现已内置,从Emacs 26开始:@martinweiss:很抱歉,我错误地链接到主分支,而主分支在此期间发生了变化。以下是。我在发布较旧版本的发行版上编译了自己的Emacs 27,我已经好几个月没有遇到过这个问题了,而在Emacs 24之前,我每天早上都会遇到这个问题(我想是NFS问题).这是一个很好的答案!比猴子打补丁好多了。