diff --git a/lisp/window.el b/lisp/window.el index 7478047939..5e43c73ef3 100644 --- a/lisp/window.el +++ b/lisp/window.el @@ -4409,6 +4409,35 @@ switch-to-visible-buffer :version "24.1" :group 'windows) +(make-obsolete-variable 'switch-to-visible-buffer + 'switch-to-buffer-skip-visible "27.1") + +(defcustom switch-to-buffer-skip-visible nil + "If nil, allow switching to an already visible buffer. +If this variable is nil, `switch-to-prev-buffer' and +`switch-to-next-buffer' allow switching to a buffer that is +already visible in a window. + +If this variable is non-nil, it specifies the frames to skip when +a window on that frame already shows the buffer. In particular: + +- t means all windows on all existing frames. + +- `visible' means all windows on all visible frames. + +- 0 (the number zero) means all windows on all visible and + iconified frames. + +- `this' means all windows on the same frame only." + :type + '(choice (const :tag "Never" nil) + (const :tag "Any frame" t) + (const :tag "Visible frames" visible) + (const :tag "Visible and iconified frames" 0) + (const :tag "This frame" this)) + :version "27.1" + :group 'windows) + (defun switch-to-prev-buffer (&optional window bury-or-kill) "In WINDOW switch to previous buffer. WINDOW must be a live window and defaults to the selected one. @@ -4424,6 +4453,10 @@ switch-to-prev-buffer future invocation of `switch-to-prev-buffer' less likely switches to it. +The option `switch-to-buffer-skip-visible' controls whether +switching to a buffer already visible in another window is +allowed. + This function is called by `prev-buffer'." (interactive) (let* ((window (window-normalize-window window t)) @@ -4433,7 +4466,14 @@ switch-to-prev-buffer ;; Save this since it's destroyed by `set-window-buffer'. (next-buffers (window-next-buffers window)) (pred (frame-parameter frame 'buffer-predicate)) - entry new-buffer killed-buffers visible) + (skip-visible + (cond + ((memq switch-to-buffer-skip-visible '(t visible 0)) + switch-to-buffer-skip-visible) + ((or switch-to-buffer-skip-visible + (not switch-to-visible-buffer)) + frame))) + entry new-buffer killed-buffers visible) (when (window-minibuffer-p window) ;; Don't switch in minibuffer window. (unless (setq window (minibuffer-selected-window)) @@ -4456,8 +4496,7 @@ switch-to-prev-buffer ;; When BURY-OR-KILL is nil, avoid switching to a ;; buffer in WINDOW's next buffers list. (or bury-or-kill (not (memq new-buffer next-buffers)))) - (if (and (not switch-to-visible-buffer) - (get-buffer-window new-buffer frame)) + (if (and skip-visible (get-buffer-window new-buffer skip-visible)) ;; Try to avoid showing a buffer visible in some other ;; window. (setq visible new-buffer) @@ -4482,8 +4521,7 @@ switch-to-prev-buffer ;; Don't show a buffer shown in a side window before. (not (buffer-local-value 'window--sides-shown buffer)) (or bury-or-kill (not (memq buffer next-buffers)))) - (if (and (not switch-to-visible-buffer) - (get-buffer-window buffer frame)) + (if (and skip-visible (get-buffer-window buffer skip-visible)) ;; Try to avoid showing a buffer visible in some other window. (unless visible (setq visible buffer)) @@ -4547,7 +4585,13 @@ switch-to-next-buffer "In WINDOW switch to next buffer. WINDOW must be a live window and defaults to the selected one. Return the buffer switched to, nil if no suitable buffer could be -found. This function is called by `next-buffer'." +found. + +The option `switch-to-buffer-skip-visible' controls whether +switching to a buffer already visible in another window is +allowed. + +This function is called by `next-buffer'." (interactive) (let* ((window (window-normalize-window window t)) (frame (window-frame window)) @@ -4555,6 +4599,13 @@ switch-to-next-buffer (old-buffer (window-buffer window)) (next-buffers (window-next-buffers window)) (pred (frame-parameter frame 'buffer-predicate)) + (skip-visible + (cond + ((memq switch-to-buffer-skip-visible '(t visible 0)) + switch-to-buffer-skip-visible) + ((or switch-to-buffer-skip-visible + (not switch-to-visible-buffer)) + frame))) new-buffer entry killed-buffers visible) (when (window-minibuffer-p window) ;; Don't switch in minibuffer window. @@ -4589,8 +4640,7 @@ switch-to-next-buffer ;; Don't show a buffer shown in a side window before. (not (buffer-local-value 'window--sides-shown buffer)) (not (assq buffer (window-prev-buffers window)))) - (if (and (not switch-to-visible-buffer) - (get-buffer-window buffer frame)) + (if (and skip-visible (get-buffer-window buffer skip-visible)) ;; Try to avoid showing a buffer visible in some other window. (setq visible buffer) (setq new-buffer buffer) @@ -4605,8 +4655,7 @@ switch-to-next-buffer (cons new-buffer killed-buffers)))) (not (eq new-buffer old-buffer)) (or (null pred) (funcall pred new-buffer))) - (if (and (not switch-to-visible-buffer) - (get-buffer-window new-buffer frame)) + (if (and skip-visible (get-buffer-window new-buffer skip-visible)) ;; Try to avoid showing a buffer visible in some other window. (unless visible (setq visible new-buffer))