From: Lars Ingebrigtsen Date: Thu, 7 Jan 2021 15:35:48 +0000 (+0100) Subject: Add a display-buffer window selection function that's more like XEmacs X-Git-Tag: emacs-28.0.90~4350^2 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=40a0f8a3a2ac790bb398c321e7eb6928da330511;p=emacs.git Add a display-buffer window selection function that's more like XEmacs * doc/lispref/windows.texi (Buffer Display Action Functions): Document it. * lisp/window.el (display-buffer--action-function-custom-type): Add. (display-buffer): Mention it. (display-buffer-use-least-recent-window): New function (bug#45688). * src/window.c (Fwindow_bump_use_time): New function. --- diff --git a/doc/lispref/windows.texi b/doc/lispref/windows.texi index b0906acbad5..f305d1a8ee8 100644 --- a/doc/lispref/windows.texi +++ b/doc/lispref/windows.texi @@ -2634,6 +2634,12 @@ window and displaying the buffer in that window. It can fail if all windows are dedicated to other buffers (@pxref{Dedicated Windows}). @end defun +@defun display-buffer-use-least-recent-window buffer alist +This function is like @code{display-buffer-use-some-window}, but will +not reuse the current window, and will use the least recently +switched-to window. +@end defun + @defun display-buffer-in-direction buffer alist This function tries to display @var{buffer} at a location specified by @var{alist}. For this purpose, @var{alist} should contain a diff --git a/etc/NEWS b/etc/NEWS index 48fb4b88e15..14d6b45c929 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -373,6 +373,15 @@ disabled entirely. ** Windows ++++ +*** New 'display-buffer' function 'display-buffer-use-least-recent-window' +This is like 'display-buffer-use-some-window', but won't reuse the +current window, and when called repeatedly will try not to reuse a +previously selected window. + +*** New function 'window-bump-use-time'. +This updates the use time of a window. + *** The key prefix 'C-x 4 1' displays next command buffer in the same window. It's bound to the command 'same-window-prefix' that requests the buffer of the next command to be displayed in the same window. diff --git a/lisp/window.el b/lisp/window.el index c54a0db211e..37e1800ad11 100644 --- a/lisp/window.el +++ b/lisp/window.el @@ -7243,6 +7243,7 @@ The actual non-nil value of this variable will be copied to the (const display-buffer-below-selected) (const display-buffer-at-bottom) (const display-buffer-in-previous-window) + (const display-buffer-use-least-recent-window) (const display-buffer-use-some-window) (const display-buffer-use-some-frame) (function :tag "Other function")) @@ -7387,6 +7388,8 @@ to a list containing one of these \"action\" functions: `display-buffer-in-previous-window' -- Use a window that did show the buffer before. `display-buffer-use-some-window' -- Use some existing window. + `display-buffer-use-least-recent-window' -- Try to avoid re-using + windows that have recently been switched to. `display-buffer-pop-up-window' -- Pop up a new window. `display-buffer-below-selected' -- Use or pop up a window below the selected one. @@ -8256,6 +8259,16 @@ indirectly called by the latter." (when (setq window (or best-window second-best-window)) (window--display-buffer buffer window 'reuse alist)))) +(defun display-buffer-use-least-recent-window (buffer alist) + "Display BUFFER in an existing window, but that hasn't been used lately. +This `display-buffer' action function is like +`display-buffer-use-some-window', but will cycle through windows +when displaying buffers repeatedly, and if there's only a single +window, it will split the window." + (when-let ((window (display-buffer-use-some-window + buffer (cons (cons 'inhibit-same-window t) alist)))) + (window-bump-use-time window))) + (defun display-buffer-use-some-window (buffer alist) "Display BUFFER in an existing window. Search for a usable window, set that window to the buffer, and diff --git a/src/window.c b/src/window.c index 58204c13e44..5e78aa400b5 100644 --- a/src/window.c +++ b/src/window.c @@ -8100,6 +8100,18 @@ and scrolling positions. */) return Qt; return Qnil; } + +DEFUN ("window-bump-use-time", Fwindow_bump_use_time, + Swindow_bump_use_time, 1, 1, 0, + doc: /* Mark WINDOW as having been recently used. */) + (Lisp_Object window) +{ + struct window *w = decode_valid_window (window); + + w->use_time = ++window_select_count; + return Qnil; +} + static void init_window_once_for_pdumper (void); @@ -8573,6 +8585,7 @@ displayed after a scrolling operation to be somewhat inaccurate. */); defsubr (&Swindow_vscroll); defsubr (&Sset_window_vscroll); defsubr (&Scompare_window_configurations); + defsubr (&Swindow_bump_use_time); defsubr (&Swindow_list); defsubr (&Swindow_list_1); defsubr (&Swindow_prev_buffers);