bug#81636: Fix switch-to-buffer-other-*

Juri Linkov <[email protected]>
Newsgroups gmane.emacs.bugs
Organization LINKOV.NET
Message-ID <[email protected]>
Better late than never, so let's finally fix the problem of using
'C-x 4 b', 'C-x 5 b', 'C-x t b' when 'display-buffer-alist'
is customized for the default display by the commands that
display a buffer.  For example, it's expected that
after this customization:

(setq display-buffer-alist
      '(("\\`\\*Messages\\*\\'"
         display-buffer-same-window
         (inhibit-same-window . nil))))

'C-h e' (view-echo-area-messages) will display the *Messages* buffer
in the same window.  Or after

(setq display-buffer-alist
      '(("\\`\\*Help\\*\\'"
         display-buffer-same-window
         (inhibit-same-window . nil))))

help commands will display the *Help* buffer
in the same window, etc.

But then 'C-x 4 b *Messages* RET', 'C-x 5 b *Messages* RET',
'C-x t b *Messages* RET' do a wrong unexpected thing,
contrary to the definitions of these commands.

So let's fix them, but only for interactive uses,
because calling them from other commands should still
respect the user customization.  For example, the command
'dictionary' directly calls 'switch-to-buffer-other-window'.
It still should show the buffer in the same window when
'display-buffer-alist' is customized to do this.

Is there any reason not to do this:
switch-to-buffer-other.patch (text/x-diff, 2.3 KB)
diff --git a/lisp/window.el b/lisp/window.el
index 04484c11e25..36ee20afe24 100644
--- a/lisp/window.el
+++ b/lisp/window.el
@@ -9743,8 +9743,12 @@ switch-to-buffer-other-window
 when it decides whether to split the window horizontally or vertically."
   (interactive
    (list (read-buffer-to-switch "Switch to buffer in other window: ")))
-  (let ((pop-up-windows t))
-    (pop-to-buffer buffer-or-name t norecord)))
+  (let ((pop-up-windows t)
+        (display-buffer-overriding-action
+         (if (called-interactively-p 'interactive)
+             '(nil (inhibit-same-window . t))
+           display-buffer-overriding-action)))
+    (pop-to-buffer buffer-or-name nil norecord)))
 
 (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord)
   "Switch to buffer BUFFER-OR-NAME in another frame.
@@ -9772,7 +9776,11 @@ switch-to-buffer-other-frame
 buffer at the front of the list of recently selected ones."
   (interactive
    (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
-  (pop-to-buffer buffer-or-name display-buffer--other-frame-action norecord))
+  (let ((display-buffer-overriding-action
+         (if (called-interactively-p 'interactive)
+             display-buffer--other-frame-action
+           display-buffer-overriding-action)))
+    (pop-to-buffer buffer-or-name nil norecord)))
 
 (defun display-buffer-override-next-command (pre-function &optional post-function echo)
   "Set `display-buffer-overriding-action' for the next command.
diff --git a/lisp/tab-bar.el b/lisp/tab-bar.el
index 15ef7967344..c3a3e044755 100644
--- a/lisp/tab-bar.el
+++ b/lisp/tab-bar.el
@@ -3107,9 +3107,12 @@ switch-to-buffer-other-tab
   (declare (advertised-calling-convention (buffer-or-name) "28.1"))
   (interactive
    (list (read-buffer-to-switch "Switch to buffer in other tab: ")))
-  (pop-to-buffer (window-normalize-buffer-to-switch-to buffer-or-name)
-                 '((display-buffer-in-tab)
-                   (inhibit-same-window . nil))))
+  (let ((display-buffer-overriding-action
+         (if (called-interactively-p 'interactive)
+             '((display-buffer-in-tab)
+               (inhibit-same-window . nil))
+           display-buffer-overriding-action)))
+    (pop-to-buffer (window-normalize-buffer-to-switch-to buffer-or-name))))
 
 (defun find-file-other-tab (filename &optional wildcards)
   "Edit file FILENAME, in another tab.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.