emacs-devel
[Top][All Lists]
Advanced

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

Re: switch-to-buffer: for interactive use only


From: Chong Yidong
Subject: Re: switch-to-buffer: for interactive use only
Date: Sat, 09 Jul 2011 15:25:29 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux)

Stefan Monnier <address@hidden> writes:

>> Fair enough.  So, I propose the following:
>> - Make switch-to-buffer basically do
>>   (pop-to-buffer buffer-or-name 'same-window norecord label)
>
> OK.  So mostly revert to Emacs-23 behavior.
>
> Note that (set-window-buffer (selected-window) buffer) isn't quite
> good enough, since set-window-buffer will be happy to change the
> buffer of a mini-window, whereas callers of switch-to-buffer that want
> to operate on the selected window should signal an error in that case.
> I think adding a `selected-window-only' argument to switch-to-buffer
> is a better solution so callers don't need to worry about such details
> of set-window-buffer.

How's this patch?

*** lisp/window.el      2011-07-09 12:32:38 +0000
--- lisp/window.el      2011-07-09 19:21:36 +0000
***************
*** 5925,5931 ****
            buffer))
      (other-buffer)))
  
! (defun switch-to-buffer (buffer-or-name &optional norecord)
    "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'
--- 5925,5931 ----
            buffer))
      (other-buffer)))
  
! (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,5965 ****
  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.
  
! This function is intended for interactive use only.  Lisp
! functions should call `pop-to-buffer-same-window' instead."
    (interactive
     (list (read-buffer-to-switch "Switch to buffer: ")))
    (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)))
  
  (defun switch-to-buffer-same-frame (buffer-or-name &optional norecord)
    "Switch to buffer BUFFER-OR-NAME in a window on the selected frame.
--- 5941,5967 ----
  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.
  
! If the selected window is weakly dedicated, switch to
! BUFFER-OR-NAME anyway (see `set-window-dedicated-p').  If the
! selected window is strongly dedicated, display BUFFER-OR-NAME in
! another window if FORCE-SAME-WINDOW is nil, and signal an error
! if FORCE-SAME-WINDOW if non-nil.
! 
! Return the buffer switched to."
    (interactive
     (list (read-buffer-to-switch "Switch to buffer: ")))
    (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name)))
!     (when force-same-window
!       (cond
!        ((window-minibuffer-p)
!       (error "Cannot switch buffers in minibuffer window"))
!        ((eq (window-dedicated-p) t)
!       (error "Cannot switch buffers in a dedicated window"))))
!     (pop-to-buffer buffer-or-name
!                  '(same-window (reuse-window-dedicated . weak))
!                  norecord nil)))
  
  (defun switch-to-buffer-same-frame (buffer-or-name &optional norecord)
    "Switch to buffer BUFFER-OR-NAME in a window on the selected frame.



reply via email to

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