From 31d58d45249b3fb13a0a9a2c921f04cd9b42ff3f Mon Sep 17 00:00:00 2001 From: Martin Rudalics Date: Wed, 20 May 2015 08:49:23 +0200 Subject: [PATCH] Improve `switch-to-buffer' in strongly dedicated windows (Bug#20472) * 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'. --- doc/lispref/windows.texi | 49 +++++++++++++++++++++++++------ etc/NEWS | 13 ++++++--- lisp/window.el | 62 +++++++++++++++++++++++++++++++++++----- 3 files changed, 105 insertions(+), 19 deletions(-) diff --git a/doc/lispref/windows.texi b/doc/lispref/windows.texi index 6da3582ddd1..b2bc6378008 100644 --- a/doc/lispref/windows.texi +++ b/doc/lispref/windows.texi @@ -2005,16 +2005,49 @@ list---both the global buffer list and the selected frame's buffer 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. diff --git a/etc/NEWS b/etc/NEWS index c4c9d774b5f..2540756b9be 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -165,7 +165,7 @@ buffers to allow certain parts of the text to be writable. ** 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. @@ -585,8 +585,8 @@ nil to disable this. 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. @@ -755,7 +755,7 @@ If you want the old behavior of calling the function in the buffer 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. @@ -960,6 +960,11 @@ fullwidth frames, the behavior may depend on the toolkit used. 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. diff --git a/lisp/window.el b/lisp/window.el index 49b7e2cc55d..c13499f542b 100644 --- a/lisp/window.el +++ b/lisp/window.el @@ -6884,6 +6884,33 @@ the selected window or never appeared in it before, or if :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. @@ -6891,10 +6918,12 @@ WARNING: This is NOT the way to work on another buffer temporarily 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' @@ -6911,8 +6940,9 @@ at the front of the buffer list, and do not make the window 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 @@ -6920,7 +6950,25 @@ window. 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 -- 2.39.2