Subject: Re: bug#39190: 28.0.50; two buffers with same buffer-file-name (diff-syntax-fontify-props)

From: Juri Linkov <[email-masked]>
Date: Mon, 03 Feb 2020 01:41:47 +0200

>> vc-find-revision-no-save also calls set-auto-mode.  So, I don't know if
>> the following patch is correct, but together with your patch it does
>> solve the original issue.
>>
>> diff --git a/lisp/vc/vc.el b/lisp/vc/vc.el
>> index f64b6c0631..c50ba132e7 100644
>> --- a/lisp/vc/vc.el
>> +++ b/lisp/vc/vc.el
>> @@ -2098,7 +2098,7 @@ vc-find-revision-no-save
>>                      ;; For non-interactive, skip any questions
>>                      (let ((enable-local-variables :safe) ;; to find `mode:'
>>                            (buffer-file-name file))
>> -                      (ignore-errors (set-auto-mode)))
>> +                      (ignore-errors (delay-mode-hooks (set-auto-mode))))
>>                    (normal-mode))
>>  	        (set-buffer-modified-p nil)
>>                  (setq buffer-read-only t))
>
> How 'bout we first consolidate the two cases into one by introducing
> a new function `set-auto-mode-for-filename` that both
> vc-find-revision-no-save and diff-mode can use?

Actually vc-find-revision-no-save when called as a command (e.g. by the
key 'f' from log-view) still should run hooks because the buffer it creates
is not internal.

Only its call from diff-syntax-fontify-hunk should not run hooks:

diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el
index 2dbab80208..9cdd732923 100644
--- a/lisp/vc/diff-mode.el
+++ b/lisp/vc/diff-mode.el
@@ -2635,11 +2635,11 @@ diff-syntax-fontify-hunk
                          ;; Don't re-initialize the buffer (which would throw
                          ;; away the previous fontification work).
                          (setq file nil)
-                       (setq buffer (ignore-errors
+                       (setq buffer (ignore-errors (delay-mode-hooks
                                       (vc-find-revision-no-save
                                        file revision
                                        diff-vc-backend
-                                       (get-buffer-create buffer-name)))))
+                                       (get-buffer-create buffer-name))))))
                      (when buffer
                        (with-current-buffer buffer
                          (diff-syntax-fontify-props file text line-nb))))))))
@@ -2719,7 +2719,7 @@ diff-syntax-fontify-props
     (cl-assert (null buffer-file-name))
     (let ((enable-local-variables :safe) ;; to find `mode:'
           (buffer-file-name file))
-      (set-auto-mode)
+      (delay-mode-hooks (set-auto-mode))
       ;; FIXME: Is this really worth the trouble?
       (when (and (fboundp 'generic-mode-find-file-hook)
                  (memq #'generic-mode-find-file-hook
diff --git a/lisp/gnus/mm-view.el b/lisp/gnus/mm-view.el
index a6be04e313..e629674ea6 100644
--- a/lisp/gnus/mm-view.el
+++ b/lisp/gnus/mm-view.el
@@ -497,7 +497,7 @@ mm-display-inline-fontify
 	    (let ((auto-mode-alist
 		   (delq (rassq 'doc-view-mode-maybe auto-mode-alist)
 			 (copy-sequence auto-mode-alist))))
-	      (set-auto-mode)
+	      (delay-mode-hooks (set-auto-mode))
 	      (setq mode major-mode)))
 	  ;; Do not fontify if the guess mode is fundamental.
 	  (unless (eq major-mode 'fundamental-mode)
diff --git a/lisp/progmodes/xref.el b/lisp/progmodes/xref.el
index 1a34456340..741ed73905 100644
--- a/lisp/progmodes/xref.el
+++ b/lisp/progmodes/xref.el
@@ -1395,7 +1395,7 @@ xref--collect-matches
                 (inhibit-message t)
                 message-log-max)
             (ignore-errors
-              (set-auto-mode t)))
+              (delay-mode-hooks (set-auto-mode t))))
           (setq-local xref--temp-buffer-file-name file)
           (setq-local inhibit-read-only t)
           (erase-buffer))