Emacs: how to disable 'file changed on disk' checking?

前端 未结 4 1300
一生所求
一生所求 2020-12-08 10:38

How to disable Emacs from checking the buffer file was changed outside the editor?

相关标签:
4条回答
  • 2020-12-08 11:08

    I have the following in my .emacs. It makes Emacs only ask about really changed files. If a file remains the same bytewise, just its timestamp is updated, as often happens when you switch branches in VCS, this "change" is ignored by 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)
    
    0 讨论(0)
  • 2020-12-08 11:14

    Emacs is really trying to help you here. Read the info page on Protection against Simultaneous Editing.

    But, if you still want to avoid that message/prompt, you can redefine the function that is doing the prompting:

    (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)
    

    The second function there is for when two people are using Emacs to edit the same file, and would provide a similar prompt (but not the one you seemed to refer to in the question).

    I'd advise against overriding the two routines, but it's there if you want.


    On the off chance global-auto-revert-mode is on, you could disable that. Add this to your .emacs:

    (global-auto-revert-mode -1)
    

    You can tell if the mode is on by looking at the variable of the same name:

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

    If the value is t, then the mode is on, otherwise it is off.

    0 讨论(0)
  • 2020-12-08 11:27

    I had annoyance with this because every time I switched branches in git, emacs thought all my files had changed.

    Revbuffs helps you cope with the symptoms of this. It allows you to cause all your buffers to be reloaded.

    You can also try (global-auto-revert-mode) which will automatically revert your files to what's on disk.

    0 讨论(0)
  • 2020-12-08 11:29

    In my case I wanted:

    (setq revert-without-query '(".*"))
    

    Documentation for 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.
    
    0 讨论(0)
提交回复
热议问题