From d17c90d084a29430b2c131e34ff76d4e9762b8dc Mon Sep 17 00:00:00 2001 From: Spencer Baugh Date: Sat, 19 Aug 2023 16:01:54 -0400 Subject: [PATCH] Add toggle-window-dedicated command and mode-line-window-dedicated It's sometimes useful to interactively make certain windows dedicated. This allows a level of interactive control over which window display-buffer uses. Additionally, when a window is dedicated (even without this new command) it can affect display-buffer behavior in ways which may be unexpected for users. Let's display the window dedicated status in the mode-line to help indicate what's going on. * doc/emacs/windows.texi (Displaying Buffers): Add information about dedicated windows and toggle-window-dedicated. * doc/emacs/screen.texi (Mode Line): Add information about the window dedicated indicator. * etc/NEWS: Announce mode-line-window-dedicated and toggle-window-dedicated. * lisp/window.el (toggle-window-dedicated): Add. (bug#64619) (window-prefix-map): Add C-x w d binding. * lisp/bindings.el (mode-line-window-control): Add. (mode-line-window-dedicated): Add. (standard-mode-line-format): Insert mode-line-window-dedicated. --- doc/emacs/screen.texi | 8 +++++- doc/emacs/windows.texi | 22 ++++++++++++++++ etc/NEWS | 21 +++++++++++++++ lisp/bindings.el | 34 ++++++++++++++++++++++-- lisp/window.el | 59 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 141 insertions(+), 3 deletions(-) diff --git a/doc/emacs/screen.texi b/doc/emacs/screen.texi index 5e9e89e6b11..3b910587260 100644 --- a/doc/emacs/screen.texi +++ b/doc/emacs/screen.texi @@ -173,7 +173,7 @@ Mode Line The text displayed in the mode line has the following format: @example - @var{cs}:@var{ch}-@var{fr} @var{buf} @var{pos} @var{line} (@var{major} @var{minor}) + @var{cs}:@var{ch}-@var{d}@var{fr} @var{buf} @var{pos} @var{line} (@var{major} @var{minor}) @end example @noindent @@ -231,6 +231,12 @@ Mode Line However, if @code{default-directory} (@pxref{File Names}) for the current buffer is on a remote machine, @samp{@@} is displayed instead. + @var{d} appears if the window is dedicated to its current buffer. +It appears as @samp{D} for strong dedication and @samp{d} for other +forms of dedication. If the window is not dedicated, @var{d} does not +appear. @xref{Dedicated Windows,, elisp, The Emacs Lisp Reference +Manual}. + @var{fr} gives the selected frame name (@pxref{Frames}). It appears only on text terminals. The initial frame's name is @samp{F1}. diff --git a/doc/emacs/windows.texi b/doc/emacs/windows.texi index 665fd80e53b..ca5e424d939 100644 --- a/doc/emacs/windows.texi +++ b/doc/emacs/windows.texi @@ -411,6 +411,28 @@ Displaying Buffers window on some other frame to display the desired buffer. Several of these commands are bound in the @kbd{C-x 5} prefix key. +@cindex dedicated window + Sometimes, a window is ``dedicated'' to its current buffer. +@xref{Dedicated Windows,, elisp, The Emacs Lisp Reference Manual}. +@code{display-buffer} will avoid reusing dedicated windows most of the +time. This is indicated by a @samp{d} in the mode line (@pxref{Mode +Line}). A window can also be strongly dedicated, which prevents any +changes to the buffer displayed in the window. This is indicated by a +@samp{D} in the mode line. + +Usually, dedicated windows are used to display specialized buffers, +but dedication can sometimes be useful interactively. For example, +when viewing errors with @kbd{M-g M-n} @code{next-error}, newly +displayed source code may replace a buffer you want to refer to. If +you dedicate a window to that buffer, the command (through +@code{display-buffer}) will prefer to use a different window instead. + +@kindex C-x w d +@findex toggle-window-dedicated + Toggle whether the selected window is dedicated to the current +buffer. With a prefix argument, make the window strongly dedicated +instead. + @menu * Window Choice:: How @code{display-buffer} works. * Temporary Displays:: Displaying non-editable buffers. diff --git a/etc/NEWS b/etc/NEWS index cfcb8104cca..647562143de 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -135,6 +135,27 @@ window systems other than Nextstep. When this minor mode is enabled, buttons representing modifier keys are displayed along the tool bar. ++++ +** 'd' in the mode line now indicates that the window is dedicated. +Windows have always been able to be dedicated to a specific buffer; +see 'window-dedicated-p'. Now the mode line indicates the dedicated +status of a window, with 'd' appearing in the mode line if a window is +dedicated and 'D' if the window is strongly dedicated. This indicator +appears before the buffer name, and after the buffer modification and +remote buffer indicators (usually "---" together). + ++++ +** New command 'toggle-window-dedicated'. +This makes it easy to interactively mark a specific window as +dedicated, so it won't be reused by 'display-buffer'. This can be +useful for complicated window setups. It is bound to 'C-x w d' +globally. + +** cl-print +*** You can expand the "..." truncation everywhere. +The code that allowed "..." to be expanded in the *Backtrace* should +now work anywhere the data is generated by `cl-print`. + --- ** New user option 'uniquify-dirname-transform'. This can be used to customize how buffer names are uniquified, by diff --git a/lisp/bindings.el b/lisp/bindings.el index 70e4087e131..418ee265e69 100644 --- a/lisp/bindings.el +++ b/lisp/bindings.el @@ -298,6 +298,35 @@ mode-line-frame-identification ;;;###autoload (put 'mode-line-frame-identification 'risky-local-variable t) +(defvar mode-line-window-dedicated-keymap + (let ((map (make-sparse-keymap))) + (define-key map [mode-line mouse-1] #'toggle-window-dedicated) + (purecopy map)) "\ +Keymap for what is displayed by `mode-line-window-dedicated'.") + +(defun mode-line-window-control () + "Compute mode line construct for window dedicated state. +Value is used for `mode-line-window-dedicated', which see." + (cond + ((eq (window-dedicated-p) t) + (propertize + "D" + 'help-echo "Window strongly dedicated to its buffer\nmouse-1: Toggle" + 'local-map mode-line-window-dedicated-keymap + 'mouse-face 'mode-line-highlight)) + ((window-dedicated-p) + (propertize + "d" + 'help-echo "Window dedicated to its buffer\nmouse-1: Toggle" + 'local-map mode-line-window-dedicated-keymap + 'mouse-face 'mode-line-highlight)) + (t ""))) + +(defvar mode-line-window-dedicated '(:eval (mode-line-window-control)) + "Mode line construct to describe the current window.") +;;;###autoload +(put 'mode-line-window-dedicated 'risky-local-variable t) + (defvar-local mode-line-process nil "Mode line construct for displaying info on process status. Normally nil in most modes, since there is no process to display.") @@ -676,8 +705,9 @@ mode-line-end-spaces 'mode-line-mule-info 'mode-line-client 'mode-line-modified - 'mode-line-remote) - 'display '(min-width (5.0))) + 'mode-line-remote + 'mode-line-window-dedicated) + 'display '(min-width (6.0))) 'mode-line-frame-identification 'mode-line-buffer-identification " " diff --git a/lisp/window.el b/lisp/window.el index 2f9b46ebb0a..7c2ead7e5fe 100644 --- a/lisp/window.el +++ b/lisp/window.el @@ -7468,6 +7468,64 @@ display-buffer-mark-dedicated The actual non-nil value of this variable will be copied to the `window-dedicated-p' flag.") +(defcustom toggle-window-dedicated-flag 'interactive + "What dedicated flag should `toggle-window-dedicated' use by default. + +If `toggle-window-dedicated' does not receive a flag argument, +the value of this variable is used and passed to +`set-window-dedicated-p'. Setting this to t will make +`toggle-window-dedicated' use strong dedication by default. Any +other non-nil value will result in the same kind of non-strong +dedication." + :type '(choice (const :tag "Strongly dedicated" t) + (const :tag "Dedicated" interactive)) + :version "30.0" + :group 'windows) + +(defun toggle-window-dedicated (&optional window flag interactive) + "Toggle whether WINDOW is dedicated to its current buffer. + +WINDOW must be a live window and defaults to the selected one. +If FLAG is t (interactively, the prefix argument), make the window +\"strongly\" dedicated to its buffer. FLAG defaults to a non-nil, +non-t value, and is passed to `set-window-dedicated-p', which see. +If INTERACTIVE is non-nil, print a message describing the dedication +status of WINDOW, after toggling it. Interactively, this argument is +always non-nil. + +When a window is dedicated to its buffer, `display-buffer' will avoid +displaying another buffer in it, if possible. When a window is +strongly dedicated to its buffer, changing the buffer shown in the +window will usually signal an error. + +You can control the default of FLAG with +`toggle-window-dedicated-flag'. Consequently, if you set that +variable to t, strong dedication will be used by default and +\\[universal-argument] will make the window weakly dedicated. + +See the info node `(elisp)Dedicated Windows' for more details." + (interactive "i\nP\np") + (setq window (window-normalize-window window)) + (setq flag (cond + ((consp flag) + (if (eq toggle-window-dedicated-flag t) + 'interactive + t)) + ((null flag) toggle-window-dedicated-flag) + (t flag))) + (if (window-dedicated-p window) + (set-window-dedicated-p window nil) + (set-window-dedicated-p window flag)) + (when interactive + (message "Window is %s dedicated to buffer %s" + (let ((status (window-dedicated-p window))) + (cond + ((null status) "no longer") + ((eq status t) "now strongly") + (t "now"))) + (current-buffer)) + (force-mode-line-update))) + (defconst display-buffer--action-function-custom-type '(choice :tag "Function" (const :tag "--" ignore) ; default for insertion @@ -10750,6 +10808,7 @@ window-prefix-map "2" #'split-root-window-below "3" #'split-root-window-right "s" #'window-toggle-side-windows + "d" #'toggle-window-dedicated "^ f" #'tear-off-window "^ t" #'tab-window-detach "-" #'fit-window-to-buffer -- 2.41.0