diff options
| author | Juri Linkov | 2018-12-21 01:28:31 +0200 |
|---|---|---|
| committer | Juri Linkov | 2018-12-21 01:28:31 +0200 |
| commit | 3f36651c6470bab951f12f486eb4928235f1ba50 (patch) | |
| tree | 5f32e7b4f9f4a5b6f825be61588b9f11b8570e39 | |
| parent | a4e6dbdcbc18a12deb8b63e43e0a7cf63b8516fc (diff) | |
| download | emacs-3f36651c6470bab951f12f486eb4928235f1ba50.tar.gz emacs-3f36651c6470bab951f12f486eb4928235f1ba50.zip | |
* lisp/window.el (switch-to-buffer-obey-display-actions): New defcustom.
(switch-to-buffer): Use it. (Bug#32790)
(switch-to-buffer-preserve-window-point): Mention it in docstring.
* lisp/windmove.el (windmove-display-in-direction): Mention it in docstring.
* doc/lispref/windows.texi (Switching Buffers):
Document switch-to-buffer-obey-display-actions.
| -rw-r--r-- | doc/lispref/windows.texi | 6 | ||||
| -rw-r--r-- | etc/NEWS | 6 | ||||
| -rw-r--r-- | lisp/windmove.el | 4 | ||||
| -rw-r--r-- | lisp/window.el | 106 |
4 files changed, 86 insertions, 36 deletions
diff --git a/doc/lispref/windows.texi b/doc/lispref/windows.texi index eb057662112..f2f6cd3a42a 100644 --- a/doc/lispref/windows.texi +++ b/doc/lispref/windows.texi | |||
| @@ -2263,6 +2263,12 @@ selected window or never appeared in it before, or if | |||
| 2263 | buffer. | 2263 | buffer. |
| 2264 | @end defopt | 2264 | @end defopt |
| 2265 | 2265 | ||
| 2266 | @defopt switch-to-buffer-obey-display-actions | ||
| 2267 | If this variable is non-@code{nil}, @code{switch-to-buffer} respects | ||
| 2268 | display actions specified by @code{display-buffer-overriding-action}, | ||
| 2269 | @code{display-buffer-alist} and other display related variables. | ||
| 2270 | @end defopt | ||
| 2271 | |||
| 2266 | The next two commands are similar to @code{switch-to-buffer}, except for | 2272 | The next two commands are similar to @code{switch-to-buffer}, except for |
| 2267 | the described features. | 2273 | the described features. |
| 2268 | 2274 | ||
| @@ -243,6 +243,12 @@ when the last screen line in a window is not fully visible. | |||
| 243 | ** New variable 'emacs-repository-branch'. | 243 | ** New variable 'emacs-repository-branch'. |
| 244 | It reports the git branch from which Emacs was built. | 244 | It reports the git branch from which Emacs was built. |
| 245 | 245 | ||
| 246 | +++ | ||
| 247 | ** New user option 'switch-to-buffer-obey-display-actions'. | ||
| 248 | When non-nil, `switch-to-buffer' uses `pop-to-buffer-same-window' that | ||
| 249 | respects display actions specified by `display-buffer-alist' and | ||
| 250 | `display-buffer-overriding-action'. | ||
| 251 | |||
| 246 | 252 | ||
| 247 | * Editing Changes in Emacs 27.1 | 253 | * Editing Changes in Emacs 27.1 |
| 248 | 254 | ||
diff --git a/lisp/windmove.el b/lisp/windmove.el index 6d61806a831..c16c12444a0 100644 --- a/lisp/windmove.el +++ b/lisp/windmove.el | |||
| @@ -588,7 +588,9 @@ By default, select the window with a displayed buffer. | |||
| 588 | If prefix ARG is `C-u', reselect a previously selected window. | 588 | If prefix ARG is `C-u', reselect a previously selected window. |
| 589 | If `windmove-display-no-select' is non-nil, this command doesn't | 589 | If `windmove-display-no-select' is non-nil, this command doesn't |
| 590 | select the window with a displayed buffer, and the meaning of | 590 | select the window with a displayed buffer, and the meaning of |
| 591 | the prefix argument is reversed." | 591 | the prefix argument is reversed. |
| 592 | When `switch-to-buffer-obey-display-actions' is non-nil, | ||
| 593 | `switch-to-buffer' commands are also supported." | ||
| 592 | (let* ((no-select (not (eq (consp arg) windmove-display-no-select))) ; xor | 594 | (let* ((no-select (not (eq (consp arg) windmove-display-no-select))) ; xor |
| 593 | (old-window (or (minibuffer-selected-window) (selected-window))) | 595 | (old-window (or (minibuffer-selected-window) (selected-window))) |
| 594 | (new-window) | 596 | (new-window) |
diff --git a/lisp/window.el b/lisp/window.el index a52b8951b15..50aec86a427 100644 --- a/lisp/window.el +++ b/lisp/window.el | |||
| @@ -7768,7 +7768,9 @@ position in the selected window. | |||
| 7768 | 7768 | ||
| 7769 | This variable is ignored if the buffer is already displayed in | 7769 | This variable is ignored if the buffer is already displayed in |
| 7770 | the selected window or never appeared in it before, or if | 7770 | the selected window or never appeared in it before, or if |
| 7771 | `switch-to-buffer' calls `pop-to-buffer' to display the buffer." | 7771 | `switch-to-buffer' calls `pop-to-buffer' to display the buffer, |
| 7772 | or non-nil `switch-to-buffer-obey-display-actions' displays it | ||
| 7773 | in another window." | ||
| 7772 | :type '(choice | 7774 | :type '(choice |
| 7773 | (const :tag "Never" nil) | 7775 | (const :tag "Never" nil) |
| 7774 | (const :tag "If already displayed elsewhere" already-displayed) | 7776 | (const :tag "If already displayed elsewhere" already-displayed) |
| @@ -7803,6 +7805,16 @@ FORCE-SAME-WINDOW is non-nil." | |||
| 7803 | :group 'windows | 7805 | :group 'windows |
| 7804 | :version "25.1") | 7806 | :version "25.1") |
| 7805 | 7807 | ||
| 7808 | (defcustom switch-to-buffer-obey-display-actions nil | ||
| 7809 | "If non-nil, `switch-to-buffer' runs `pop-to-buffer-same-window' instead. | ||
| 7810 | This means that when switching the buffer it respects display actions | ||
| 7811 | specified by `display-buffer-overriding-action', `display-buffer-alist' | ||
| 7812 | and other display related variables. So `switch-to-buffer' will display | ||
| 7813 | the buffer in the window specified by the rules from these variables." | ||
| 7814 | :type 'boolean | ||
| 7815 | :group 'windows | ||
| 7816 | :version "27.1") | ||
| 7817 | |||
| 7806 | (defun switch-to-buffer (buffer-or-name &optional norecord force-same-window) | 7818 | (defun switch-to-buffer (buffer-or-name &optional norecord force-same-window) |
| 7807 | "Display buffer BUFFER-OR-NAME in the selected window. | 7819 | "Display buffer BUFFER-OR-NAME in the selected window. |
| 7808 | 7820 | ||
| @@ -7835,59 +7847,83 @@ displaying it the most recently selected one. | |||
| 7835 | If optional argument FORCE-SAME-WINDOW is non-nil, the buffer | 7847 | If optional argument FORCE-SAME-WINDOW is non-nil, the buffer |
| 7836 | must be displayed in the selected window when called | 7848 | must be displayed in the selected window when called |
| 7837 | non-interactively; if that is impossible, signal an error rather | 7849 | non-interactively; if that is impossible, signal an error rather |
| 7838 | than calling `pop-to-buffer'. | 7850 | than calling `pop-to-buffer'. It has no effect when the option |
| 7851 | `switch-to-buffer-obey-display-actions' is non-nil. | ||
| 7839 | 7852 | ||
| 7840 | The option `switch-to-buffer-preserve-window-point' can be used | 7853 | The option `switch-to-buffer-preserve-window-point' can be used |
| 7841 | to make the buffer appear at its last position in the selected | 7854 | to make the buffer appear at its last position in the selected |
| 7842 | window. | 7855 | window. |
| 7843 | 7856 | ||
| 7857 | If the option `switch-to-buffer-obey-display-actions' is non-nil, | ||
| 7858 | run the function `pop-to-buffer-same-window' instead. | ||
| 7859 | This may display the buffer in another window as specified by | ||
| 7860 | `display-buffer-overriding-action', `display-buffer-alist' and | ||
| 7861 | other display related variables. If this results in displaying | ||
| 7862 | the buffer in the selected window, window start and point are adjusted | ||
| 7863 | as prescribed by the option `switch-to-buffer-preserve-window-point'. | ||
| 7864 | Otherwise, these are left alone. | ||
| 7865 | |||
| 7844 | Return the buffer switched to." | 7866 | Return the buffer switched to." |
| 7845 | (interactive | 7867 | (interactive |
| 7846 | (let ((force-same-window | 7868 | (let ((force-same-window |
| 7847 | (cond | 7869 | (unless switch-to-buffer-obey-display-actions |
| 7848 | ((window-minibuffer-p) nil) | 7870 | (cond |
| 7849 | ((not (eq (window-dedicated-p) t)) 'force-same-window) | 7871 | ((window-minibuffer-p) nil) |
| 7850 | ((pcase switch-to-buffer-in-dedicated-window | 7872 | ((not (eq (window-dedicated-p) t)) 'force-same-window) |
| 7851 | ('nil (user-error | 7873 | ((pcase switch-to-buffer-in-dedicated-window |
| 7852 | "Cannot switch buffers in a dedicated window")) | 7874 | ('nil (user-error |
| 7853 | ('prompt | 7875 | "Cannot switch buffers in a dedicated window")) |
| 7854 | (if (y-or-n-p | 7876 | ('prompt |
| 7855 | (format "Window is dedicated to %s; undedicate it" | 7877 | (if (y-or-n-p |
| 7856 | (window-buffer))) | 7878 | (format "Window is dedicated to %s; undedicate it" |
| 7857 | (progn | 7879 | (window-buffer))) |
| 7858 | (set-window-dedicated-p nil nil) | 7880 | (progn |
| 7859 | 'force-same-window) | 7881 | (set-window-dedicated-p nil nil) |
| 7860 | (user-error | 7882 | 'force-same-window) |
| 7861 | "Cannot switch buffers in a dedicated window"))) | 7883 | (user-error |
| 7862 | ('pop nil) | 7884 | "Cannot switch buffers in a dedicated window"))) |
| 7863 | (_ (set-window-dedicated-p nil nil) 'force-same-window)))))) | 7885 | ('pop nil) |
| 7886 | (_ (set-window-dedicated-p nil nil) 'force-same-window))))))) | ||
| 7864 | (list (read-buffer-to-switch "Switch to buffer: ") nil force-same-window))) | 7887 | (list (read-buffer-to-switch "Switch to buffer: ") nil force-same-window))) |
| 7865 | (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name))) | 7888 | (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name)) |
| 7889 | (set-window-start-and-point (not switch-to-buffer-obey-display-actions))) | ||
| 7866 | (cond | 7890 | (cond |
| 7867 | ;; Don't call set-window-buffer if it's not needed since it | 7891 | ;; Don't call set-window-buffer if it's not needed since it |
| 7868 | ;; might signal an error (e.g. if the window is dedicated). | 7892 | ;; might signal an error (e.g. if the window is dedicated). |
| 7869 | ((eq buffer (window-buffer))) | 7893 | ((and (eq buffer (window-buffer)) |
| 7870 | ((window-minibuffer-p) | 7894 | ;; pop-to-buffer-same-window might decide to display |
| 7895 | ;; the same buffer in another window | ||
| 7896 | (not switch-to-buffer-obey-display-actions))) | ||
| 7897 | ((and (window-minibuffer-p) | ||
| 7898 | (not switch-to-buffer-obey-display-actions)) | ||
| 7871 | (if force-same-window | 7899 | (if force-same-window |
| 7872 | (user-error "Cannot switch buffers in minibuffer window") | 7900 | (user-error "Cannot switch buffers in minibuffer window") |
| 7873 | (pop-to-buffer buffer norecord))) | 7901 | (pop-to-buffer buffer norecord))) |
| 7874 | ((eq (window-dedicated-p) t) | 7902 | ((and (eq (window-dedicated-p) t) |
| 7903 | (not switch-to-buffer-obey-display-actions)) | ||
| 7875 | (if force-same-window | 7904 | (if force-same-window |
| 7876 | (user-error "Cannot switch buffers in a dedicated window") | 7905 | (user-error "Cannot switch buffers in a dedicated window") |
| 7877 | (pop-to-buffer buffer norecord))) | 7906 | (pop-to-buffer buffer norecord))) |
| 7878 | (t | 7907 | (t |
| 7879 | (let* ((entry (assq buffer (window-prev-buffers))) | 7908 | (when switch-to-buffer-obey-display-actions |
| 7880 | (displayed (and (eq switch-to-buffer-preserve-window-point | 7909 | (let ((selected-window (selected-window))) |
| 7881 | 'already-displayed) | 7910 | (pop-to-buffer-same-window buffer norecord) |
| 7882 | (get-buffer-window buffer 0)))) | 7911 | (when (eq (selected-window) selected-window) |
| 7883 | (set-window-buffer nil buffer) | 7912 | (setq set-window-start-and-point t)))) |
| 7884 | (when (and entry | 7913 | |
| 7885 | (or (eq switch-to-buffer-preserve-window-point t) | 7914 | (when set-window-start-and-point |
| 7886 | displayed)) | 7915 | (let* ((entry (assq buffer (window-prev-buffers))) |
| 7887 | ;; Try to restore start and point of buffer in the selected | 7916 | (displayed (and (eq switch-to-buffer-preserve-window-point |
| 7888 | ;; window (Bug#4041). | 7917 | 'already-displayed) |
| 7889 | (set-window-start (selected-window) (nth 1 entry) t) | 7918 | (get-buffer-window buffer 0)))) |
| 7890 | (set-window-point nil (nth 2 entry)))))) | 7919 | (set-window-buffer nil buffer) |
| 7920 | (when (and entry | ||
| 7921 | (or (eq switch-to-buffer-preserve-window-point t) | ||
| 7922 | displayed)) | ||
| 7923 | ;; Try to restore start and point of buffer in the selected | ||
| 7924 | ;; window (Bug#4041). | ||
| 7925 | (set-window-start (selected-window) (nth 1 entry) t) | ||
| 7926 | (set-window-point nil (nth 2 entry))))))) | ||
| 7891 | 7927 | ||
| 7892 | (unless norecord | 7928 | (unless norecord |
| 7893 | (select-window (selected-window))) | 7929 | (select-window (selected-window))) |