bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#64619: [PATCH] Add toggle-window-dedicated command


From: sbaugh
Subject: bug#64619: [PATCH] Add toggle-window-dedicated command
Date: Mon, 21 Aug 2023 13:00:48 +0000 (UTC)
User-agent: Gnus/5.13 (Gnus v5.13)

Applied Eli's comments and added support for customizing which flag
toggle-window-dedicated sets by default.  New patch attached.

Eli Zaretskii <eliz@gnu.org> writes:
>> From: sbaugh@catern.com
>> Date: Sat, 19 Aug 2023 20:02:35 +0000 (UTC)
>> Cc: Spencer Baugh <sbaugh@janestreet.com>, Eli Zaretskii <eliz@gnu.org>,
>>      martin rudalics <rudalics@gmx.at>, Drew Adams <drew.adams@oracle.com>,
>>      64619@debbugs.gnu.org
>> +
>> +@kindex C-x w d
>> +@findex toggle-window-dedicated
>> +  Toggle whether the current window is dedicated to the current
>                         ^^^^^^^
> "selected", not "current".  Or maybe "currently-selected".

Selected seems more common than currently-selected so went with that.

>> @@ -675,6 +696,7 @@ mode-line-end-spaces
>>                  'mode-line-modified
>>                  'mode-line-remote)
>>                'display '(min-width (5.0)))
>> +             'mode-line-window-dedicated
>>           'mode-line-frame-identification
>>           'mode-line-buffer-identification
>>           "   "
>
> Why not add this to the group with the min-width property (and enlarge
> that to 6.0)?  That way, we prevent annoying horizontal movement of
> the rest of the mode-line display when toggling the state.

Hm, I tried doing that, but maybe I don't understand min-width, because
there's still horizontal movement of the mode-line when I toggle it on
and off.  I tried various combinations of strings, but they all had this
behavior, maybe I'm doing something obviously wrong?

Also, if we make it fixed-size in this way, should we display "-"
instead of nothing when the window is not dedicated? Or just an empty
space?

>From b92b7dcb9e8dcb017e69462f8f7e580dba2b8fce Mon Sep 17 00:00:00 2001
From: Spencer Baugh <sbaugh@catern.com>
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.
(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               | 16 ++++++++++++
 lisp/bindings.el       | 34 ++++++++++++++++++++++++--
 lisp/window.el         | 55 ++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 132 insertions(+), 3 deletions(-)

diff --git a/doc/emacs/screen.texi b/doc/emacs/screen.texi
index 5e9e89e6b11..d4e92c035b8 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 e4abdef76be..b1c36aec426 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 ``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
+``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 3cfc36e10da..cd4c414a64c 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -92,6 +92,22 @@ plus, minus, check-mark, start, etc.
 The 'tool-bar-position' frame parameter can be set to 'bottom' on all
 window systems other than Nextstep.
 
++++
+** '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
diff --git a/lisp/bindings.el b/lisp/bindings.el
index f1a75b080be..1e05c1de43c 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.")
@@ -673,8 +702,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 d91bbabc010..f1677060310 100644
--- a/lisp/window.el
+++ b/lisp/window.el
@@ -7463,6 +7463,60 @@ 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.
+
+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) 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
@@ -10746,6 +10800,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


reply via email to

[Prev in Thread] Current Thread [Next in Thread]