* lisp/window.el (switch-to-buffer-in-dedicated-window): New option.
(switch-to-buffer): If the selected window is strongly dedicated
to its buffer, signal error before prompting for buffer name. Handle
`switch-to-buffer-in-dedicated-window'. (Bug#20472)
* doc/lispref/windows.texi (Switching Buffers): Document
`switch-to-buffer-in-dedicated-window'.
list (@pxref{Buffer List}). However, this is not done if the
optional argument @var{norecord} is non-@code{nil}.
-Sometimes, @code{switch-to-buffer} may be unable to display the buffer
-in the selected window. This happens if the selected window is a
-minibuffer window, or if the selected window is strongly dedicated to
-its buffer (@pxref{Dedicated Windows}). In that case, the command
-normally tries to display the buffer in some other window, by invoking
-@code{pop-to-buffer} (see below). However, if the optional argument
-@var{force-same-window} is non-@code{nil}, it signals an error
-instead.
+Sometimes, the selected window may not be suitable for displaying the
+buffer. This happens if the selected window is a minibuffer window, or
+if the selected window is strongly dedicated to its buffer
+(@pxref{Dedicated Windows}). In such cases, the command normally tries
+to display the buffer in some other window, by invoking
+@code{pop-to-buffer} (see below).
+
+If the optional argument @var{force-same-window} is non-@code{nil} and
+the selected window is not suitable for displaying the buffer, this
+function always signals an error when called non-interactively. In
+interactive use, if the selected window is a minibuffer window, this
+function will try to use some other window instead. If the selected
+window is strongly dedicated to its buffer, the option
+@code{switch-to-buffer-in-dedicated-window} described next can be used
+to proceed.
@end deffn
+@defopt switch-to-buffer-in-dedicated-window
+This option, if non-@code{nil}, allows @code{switch-to-buffer} to
+proceed when called interactively and the selected window is strongly
+dedicated to its buffer.
+
+The following values are respected:
+
+@table @code
+@item nil
+Disallows switching and signals an error as in non-interactive use.
+
+@item prompt
+Prompts the user whether to allow switching.
+
+@item pop
+Invokes @code{pop-to-buffer} to proceed.
+
+@item t
+Marks the selected window as non-dedicated and proceeds.
+@end table
+
+When called non-interactively, @code{switch-to-buffer} always signals an
+error when the selected window is dedicated to its buffer and
+@var{force-same-window} is non-@code{nil}.
+@end defopt
+
By default, @code{switch-to-buffer} shows the buffer at its position of
@code{point}. This behavior can be tuned using the following option.
** A new function `directory-files-recursively' returns all matching
files (recursively) under a directory.
-** The new `directory-name-p' can be used to check whether a file
+** The new function `directory-name-p' can be used to check whether a file
name (as returned from, for instance, `file-name-all-completions' is
a directory file name. It returns non-nil if the last character in
the name is a forward slash.
fitting for use in money calculations; factorial works with
non-integer inputs.
-** HideIfDef mode now support full C/C++ expressions, argumented macro expansions
-, interactive macro evaluation and automatic scanning of #defined symbols.
+** HideIfDef mode now support full C/C++ expressions, argumented macro expansions,
+interactive macro evaluation and automatic scanning of #defined symbols.
*** New custom variable `hide-ifdef-header-regexp' to define C/C++ header file
name patterns. Default case-insensitive .h, .hh, .hpp and .hxx.
from which the minibuffer was entered, call it with the new argument
`switch-buffer'.
-** window-configurations no longer record the buffers's marks.
+** window-configurations no longer record the buffers' marks.
** inhibit-modification-hooks now also inhibits lock-file checks, as well as
active region handling.
windows without "fixing" it. It's supported by `fit-window-to-buffer',
`temp-buffer-resize-mode' and `display-buffer'.
++++
+** New option `switch-to-buffer-in-dedicated-window' allows to customize
+how `switch-to-buffer' proceeds interactively when the selected window
+is strongly dedicated to its buffer.
+
** Tearoff menus and detachable toolbars for Gtk+ has been removed.
Those features have been deprecated in Gtk+ for a long time.
:group 'windows
:version "24.3")
+(defcustom switch-to-buffer-in-dedicated-window nil
+ "Allow switching to buffer in strongly dedicated windows.
+If non-nil, allow `switch-to-buffer' to proceed when called
+interactively and the selected window is strongly dedicated to
+its buffer.
+
+The following values are recognized:
+
+nil - disallow switching; signal an error
+
+prompt - prompt user whether to allow switching
+
+pop - perform `pop-to-buffer' instead
+
+t - undedicate selected window and switch
+
+When called non-interactively, `switch-to-buffer' always signals
+an error when the selected window is dedicated to its buffer and
+FORCE-SAME-WINDOW is non-nil."
+ :type '(choice
+ (const :tag "Disallow" nil)
+ (const :tag "Prompt" prompt)
+ (const :tag "Pop" pop)
+ (const :tag "Allow" t))
+ :group 'windows
+ :version "25.1")
+
(defun switch-to-buffer (buffer-or-name &optional norecord force-same-window)
"Display buffer BUFFER-OR-NAME in the selected window.
within a Lisp program! Use `set-buffer' instead. That avoids
messing with the window-buffer correspondences.
-If the selected window cannot display the specified
-buffer (e.g. if it is a minibuffer window or strongly dedicated
-to another buffer), call `pop-to-buffer' to select the buffer in
-another window.
+If the selected window cannot display the specified buffer
+because it is a minibuffer window or strongly dedicated to
+another buffer, call `pop-to-buffer' to select the buffer in
+another window. In interactive use, if the selected window is
+strongly dedicated to its buffer, the value of the option
+`switch-to-buffer-in-dedicated-window' specifies how to proceed.
If called interactively, read the buffer name using the
minibuffer. The variable `confirm-nonexistent-file-or-buffer'
displaying it the most recently selected one.
If optional argument FORCE-SAME-WINDOW is non-nil, the buffer
-must be displayed in the selected window; if that is impossible,
-signal an error rather than calling `pop-to-buffer'.
+must be displayed in the selected window when called
+non-interactively; if that is impossible, signal an error rather
+than calling `pop-to-buffer'.
The option `switch-to-buffer-preserve-window-point' can be used
to make the buffer appear at its last position in the selected
Return the buffer switched to."
(interactive
- (list (read-buffer-to-switch "Switch to buffer: ") nil 'force-same-window))
+ (let ((force-same-window
+ (cond
+ ((window-minibuffer-p) nil)
+ ((not (eq (window-dedicated-p) t)) 'force-same-window)
+ ((pcase switch-to-buffer-in-dedicated-window
+ (`nil (user-error
+ "Cannot switch buffers in a dedicated window"))
+ (`prompt
+ (if (y-or-n-p
+ (format "Window is dedicated to %s; undedicate it"
+ (window-buffer)))
+ (progn
+ (set-window-dedicated-p nil nil)
+ 'force-same-window)
+ (user-error
+ "Cannot switch buffers in a dedicated window")))
+ (`pop nil)
+ (_ (set-window-dedicated-p nil nil) 'force-same-window))))))
+ (list (read-buffer-to-switch "Switch to buffer: ") nil force-same-window)))
(let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name)))
(cond
;; Don't call set-window-buffer if it's not needed since it