Subject: Re: bug#68235: 29.1.90; Switching tabs stops following process output in selected window

From: martin rudalics <[email-masked]>
Date: Fri, 12 Apr 2024 10:18:03 +0200

> Using 'find-alternate-file' in 'image-next-file' looks fine.
 > There are no other problems.  And revealing " **lose**"
 > while restoring windows is not a bug deal.

OK.  Please try the attached patch.  With something like

(let ((buffer (find-file-noselect "~/foo.el")))
   (with-current-buffer buffer
     (find-alternate-file "~/bar.el")
     (message "%s..%s" (buffer-last-name buffer) (buffer-file-name buffer))))

it should work fine thus fixing the image mode problem.  With

(let ((buffer (find-file-noselect "~/foo/bar.el")))
   (with-current-buffer buffer
     (find-alternate-file "~/bar/bar.el")
     (message "%s..%s" (buffer-last-name buffer) (buffer-file-name buffer))))

you can still use the old file name to restore the buffer.

martin

diff --git a/lisp/files.el b/lisp/files.el
index e8a5e5783b6..48a39c242b7 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -2105,6 +2105,15 @@ find-alternate-file
 	(rename-buffer oname)))
     (unless (eq (current-buffer) obuf)
       (with-current-buffer obuf
+	(unless (get-buffer oname)
+	  ;; Restore original's buffer name so 'kill-buffer' can use it
+	  ;; to assign its last name (Bug#68235).
+	  (rename-buffer oname))
+	;; Restore original buffer's file names so they can be still
+	;; used when referencing the now defunct buffer (Bug#68235).
+	(setq buffer-file-name ofile)
+	(setq buffer-file-number onum)
+	(setq buffer-file-truename otrue)
 	;; We already ran these; don't run them again.
 	(let (kill-buffer-query-functions kill-buffer-hook)
 	  (kill-buffer obuf))))))