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

Juri Linkov <[email protected]>
Newsgroups gmane.emacs.bugs
Organization LINKOV.NET
Message-ID <[email protected]>
> 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.

It seems that the option 'switch-to-buffer-obey-display-actions'
has no relevance here?  Despite the documentation speaking about
"switch-to-buffer commands":

  When `switch-to-buffer-obey-display-actions' is non-nil,
  `switch-to-buffer' commands are also supported.

I don't remember, but it seems these "switch-to-buffer commands"
are commands that call 'switch-to-buffer', but not the commands
that share the same name prefix 'switch-to-buffer-...'?
Because 'switch-to-buffer-other-...' commands don't call
'switch-to-buffer'.

Therefore what we can do here it to add a new 'interactive' arg
to all commands whose names end with the suffixes '-other-window',
'-other-frame', '-other-tab'.  Here is an example for two commands:
switch-to-buffer-other-tab.patch (text/x-diff, 2.3 KB)
diff --git a/lisp/dired.el b/lisp/dired.el
index ac12bf42565..fa067a469fe 100644
--- a/lisp/dired.el
+++ b/lisp/dired.el
@@ -1280,12 +1280,13 @@ dired-other-frame
 
 ;;;###autoload (keymap-set tab-prefix-map "d" #'dired-other-tab)
 ;;;###autoload
-(defun dired-other-tab (dirname &optional switches)
+(defun dired-other-tab (dirname &optional switches interactive)
   "\"Edit\" directory DIRNAME.  Like `dired' but make a new tab."
-  (interactive (dired-read-dir-and-switches "in other tab "))
+  (interactive (dired-read-dir-and-switches "in other tab ")
+               nil t)
   (let ((buf (dired-noselect dirname switches)))
     (prog1 (if buf
-               (switch-to-buffer-other-tab buf)
+               (switch-to-buffer-other-tab buf nil interactive)
              (current-buffer))
         (dired--display-ls-error))))
 
diff --git a/lisp/tab-bar.el b/lisp/tab-bar.el
index 15ef7967344..e537be4fc21 100644
--- a/lisp/tab-bar.el
+++ b/lisp/tab-bar.el
@@ -3100,16 +3100,22 @@ display-buffer-in-new-tab
         (tab-bar-change-tab-group tab-group)))
     (window--display-buffer buffer (selected-window) 'tab alist)))
 
-(defun switch-to-buffer-other-tab (buffer-or-name &optional _norecord)
+(defun switch-to-buffer-other-tab (buffer-or-name &optional
+                                                  _norecord interactive)
   "Switch to buffer BUFFER-OR-NAME in another tab.
 Like \\[switch-to-buffer-other-frame] (which see), but creates a new tab.
 Interactively, prompt for the buffer to switch to."
   (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))))
+   (list (read-buffer-to-switch "Switch to buffer in other tab: ")
+         nil t))
+  (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name))
+        (action '((display-buffer-in-tab)
+                  (inhibit-same-window . nil))))
+    (if interactive
+        (let ((display-buffer-overriding-action action))
+          (pop-to-buffer buffer))
+      (pop-to-buffer buffer action))))
 
 (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.