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

From: Juri Linkov <[email-masked]>
Date: Tue, 28 Jan 2020 02:01:44 +0200

> Passing it down as an arg is indeed problematic.
> Maybe we should just add yet-another buffer-<foo>-filename and change
> the corresponding places to so that instead of consulting
>
>     buffer-file-name
>
> they consult
>
>     (or buffer-file-name buffer-<foo>-filename)
>
> Maybe this could also be used for things like `vc-revision-other-window`.

This requires more testing, but at least such patch
basically would look like this:

diff --git a/lisp/files.el b/lisp/files.el
index 683f4a8ce7..e2721f43fc 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -3080,6 +3080,8 @@ magic-mode-regexp-match-limit
   "Upper limit on `magic-mode-alist' regexp matches.
 Also applies to `magic-fallback-mode-alist'.")
 
+(defvar-local buffer-file-name-for-mode nil)
+
 (defun set-auto-mode (&optional keep-mode-if-same)
   "Select major mode appropriate for current buffer.
 
@@ -3197,11 +3199,10 @@ set-auto-mode
 	  (set-auto-mode-0 done keep-mode-if-same)))
     ;; Next compare the filename against the entries in auto-mode-alist.
     (unless done
-      (if buffer-file-name
-	  (let ((name buffer-file-name)
-		(remote-id (file-remote-p buffer-file-name))
-		(case-insensitive-p (file-name-case-insensitive-p
-				     buffer-file-name)))
+      (if (or buffer-file-name buffer-file-name-for-mode)
+	  (let* ((name (or buffer-file-name buffer-file-name-for-mode))
+		 (remote-id (file-remote-p name))
+		 (case-insensitive-p (file-name-case-insensitive-p name)))
 	    ;; Remove backup-suffixes from file name.
 	    (setq name (file-name-sans-versions name))
 	    ;; Remove remote file name identification.
@@ -3459,6 +3460,8 @@ hack-local-variables-confirm
     (let ((name (cond (dir-name)
 		      (buffer-file-name
 		       (file-name-nondirectory buffer-file-name))
+		      (buffer-file-name-for-mode
+		       (file-name-nondirectory buffer-file-name-for-mode))
 		      ((concat "buffer " (buffer-name)))))
 	  (offer-save (and (eq enable-local-variables t)
 			   unsafe-vars))
diff --git a/lisp/gnus/mm-view.el b/lisp/gnus/mm-view.el
index a6be04e313..0c2b985956 100644
--- a/lisp/gnus/mm-view.el
+++ b/lisp/gnus/mm-view.el
@@ -482,7 +482,7 @@ mm-display-inline-fontify
         ;; aren't a problem any more.  So we could probably get rid of this
         ;; setting now, but it seems harmless and potentially still useful.
 	(set (make-local-variable 'font-lock-mode-hook) nil)
-        (setq buffer-file-name (mm-handle-filename handle))
+        (setq buffer-file-name-for-mode (mm-handle-filename handle))
 	(with-demoted-errors
 	    (if mode
                 (save-window-excursion
diff --git a/lisp/progmodes/xref.el b/lisp/progmodes/xref.el
index 1a34456340..4290eeb66a 100644
--- a/lisp/progmodes/xref.el
+++ b/lisp/progmodes/xref.el
@@ -1391,7 +1391,7 @@ xref--collect-matches
           (insert-file-contents file nil 0 200)
           ;; Can't (setq-local delay-mode-hooks t) because of
           ;; bug#23272, but the performance penalty seems minimal.
-          (let ((buffer-file-name file)
+          (let ((buffer-file-name-for-mode file)
                 (inhibit-message t)
                 message-log-max)
             (ignore-errors
diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el
index 2dbab80208..cff0a5a103 100644
--- a/lisp/vc/diff-mode.el
+++ b/lisp/vc/diff-mode.el
@@ -2718,7 +2718,7 @@ diff-syntax-fontify-props
     ;; temp buffer.
     (cl-assert (null buffer-file-name))
     (let ((enable-local-variables :safe) ;; to find `mode:'
-          (buffer-file-name file))
+          (buffer-file-name-for-mode file))
       (set-auto-mode)
       ;; FIXME: Is this really worth the trouble?
       (when (and (fboundp 'generic-mode-find-file-hook)
diff --git a/lisp/vc/vc.el b/lisp/vc/vc.el
index f64b6c0631..79f49ab68b 100644
--- a/lisp/vc/vc.el
+++ b/lisp/vc/vc.el
@@ -2097,7 +2099,7 @@ vc-find-revision-no-save
                 (if buffer
                     ;; For non-interactive, skip any questions
                     (let ((enable-local-variables :safe) ;; to find `mode:'
-                          (buffer-file-name file))
+                          (buffer-file-name-for-mode file))
                       (ignore-errors (set-auto-mode)))
                   (normal-mode))
 	        (set-buffer-modified-p nil)