From bee0fcef3d9c332b9907369b4e6f6f61d6fbdf35 Mon Sep 17 00:00:00 2001 From: Chong Yidong Date: Wed, 13 Jul 2011 18:00:48 -0400 Subject: [PATCH] Add FORCE-SAME-WINDOW argument to switch-to-buffer. * lisp/window.el (switch-to-buffer): New arg FORCE-SAME-WINDOW. Use pop-to-buffer buffer-or-name if it is nil. * lisp/emacs-lisp/bytecomp.el (byte-compile-interactive-only-functions): Remove switch-to-buffer. --- etc/NEWS | 9 ++++++++ lisp/ChangeLog | 8 +++++++ lisp/emacs-lisp/bytecomp.el | 2 +- lisp/window.el | 42 ++++++++++++++++++++++--------------- 4 files changed, 43 insertions(+), 18 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 8a37735b41f..3f23c23fe3a 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -986,6 +986,15 @@ sc.el, x-menu.el, rnews.el, rnewspost.el * Lisp changes in Emacs 24.1 +** Window changes + +*** `switch-to-buffer' has a new optional argument FORCE-SAME-WINDOW, +which if non-nil requires the buffer to be displayed in the currently +selected window, signaling an error otherwise. If nil, another window +can be used, e.g. if the selected one is strongly dedicated. + +*** FIXME: buffer-display-alist changes + ** Completion *** New variable completion-extra-properties used to specify extra properties of the current completion: diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 86bc82f3ecd..17cbc948536 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,11 @@ +2011-07-13 Chong Yidong + + * window.el (switch-to-buffer): New arg FORCE-SAME-WINDOW. Use + pop-to-buffer buffer-or-name if it is nil. + + * emacs-lisp/bytecomp.el (byte-compile-interactive-only-functions): + Remove switch-to-buffer. + 2011-07-13 Lars Magne Ingebrigtsen * startup.el (initial-buffer-choice): Add `none' as a choice diff --git a/lisp/emacs-lisp/bytecomp.el b/lisp/emacs-lisp/bytecomp.el index 223e9667ac3..127f93c6858 100644 --- a/lisp/emacs-lisp/bytecomp.el +++ b/lisp/emacs-lisp/bytecomp.el @@ -355,7 +355,7 @@ else the global value will be modified." (defvar byte-compile-interactive-only-functions '(beginning-of-buffer end-of-buffer replace-string replace-regexp insert-file insert-buffer insert-file-literally previous-line next-line - goto-line comint-run delete-backward-char switch-to-buffer) + goto-line comint-run delete-backward-char) "List of commands that are not meant to be called from Lisp.") (defvar byte-compile-not-obsolete-vars nil diff --git a/lisp/window.el b/lisp/window.el index 999e408bdb1..593fa14d215 100644 --- a/lisp/window.el +++ b/lisp/window.el @@ -5925,7 +5925,7 @@ buffer with the name BUFFER-OR-NAME and return that buffer." buffer)) (other-buffer))) -(defun switch-to-buffer (buffer-or-name &optional norecord) +(defun switch-to-buffer (buffer-or-name &optional norecord force-same-window) "Switch to buffer BUFFER-OR-NAME in the selected window. If called interactively, prompt for the buffer name using the minibuffer. The variable `confirm-nonexistent-file-or-buffer' @@ -5941,25 +5941,33 @@ BUFFER-OR-NAME is nil, switch to the buffer returned by Optional argument NORECORD non-nil means do not put the buffer specified by BUFFER-OR-NAME at the front of the buffer list and do not make the window displaying it the most recently selected -one. Return the buffer switched to. +one. -This function is intended for interactive use only. Lisp -functions should call `pop-to-buffer-same-window' instead." +If FORCE-SAME-WINDOW is non-nil, BUFFER-OR-NAME must be displayed +in the currently selected window; signal an error if that is +impossible (e.g. if the selected window is minibuffer-only). +If non-nil, BUFFER-OR-NAME may be displayed in another window. + +Return the buffer switched to." (interactive - (list (read-buffer-to-switch "Switch to buffer: "))) + (list (read-buffer-to-switch "Switch to buffer: ") nil nil)) (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name))) - (cond - ;; Don't call set-window-buffer if it's not needed since it - ;; might signal an error (e.g. if the window is dedicated). - ((eq buffer (window-buffer)) nil) - ((window-minibuffer-p) - (error "Cannot switch buffers in minibuffer window")) - ((eq (window-dedicated-p) t) - (error "Cannot switch buffers in a dedicated window")) - (t (set-window-buffer nil buffer))) - (unless norecord - (select-window (selected-window))) - (set-buffer buffer))) + (if (null force-same-window) + (pop-to-buffer buffer-or-name + '(same-window (reuse-window-dedicated . weak)) + norecord nil) + (cond + ;; Don't call set-window-buffer if it's not needed since it + ;; might signal an error (e.g. if the window is dedicated). + ((eq buffer (window-buffer)) nil) + ((window-minibuffer-p) + (error "Cannot switch buffers in minibuffer window")) + ((eq (window-dedicated-p) t) + (error "Cannot switch buffers in a dedicated window")) + (t (set-window-buffer nil buffer))) + (unless norecord + (select-window (selected-window))) + (set-buffer buffer)))) (defun switch-to-buffer-same-frame (buffer-or-name &optional norecord) "Switch to buffer BUFFER-OR-NAME in a window on the selected frame. -- 2.39.2