=== modified file 'lisp/window.el' --- lisp/window.el 2011-10-10 17:52:03 +0000 +++ lisp/window.el 2011-10-12 09:23:47 +0000 @@ -4976,6 +4976,19 @@ buffer)) (other-buffer))) +(defcustom switch-to-buffer-preserve-window-point nil + "If non-nil, `switch-to-buffer' tries to preserve `window-point'. +If nil, `switch-to-buffer' displays the buffer at that buffer's +`point'. If non-nil, it tries to display the buffer at the last +position of `window-point' in the window used for display. + +If the window used for display is either new, or the buffer +already appears in it, or the buffer never appeared in that +window, this variable has no impact." + :type 'boolean + :group 'windows + :version "24.1") + (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 @@ -5001,7 +5014,7 @@ Return the buffer switched to." (interactive - (list (read-buffer-to-switch "Switch to buffer: ") nil nil)) + (list (read-buffer-to-switch "Switch to buffer: ") nil t)) (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name))) (if (null force-same-window) (pop-to-buffer buffer display-buffer--same-window-action norecord) @@ -5013,7 +5026,19 @@ (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))) + (t + (let* ((entry (and switch-to-buffer-preserve-window-point + (assq buffer (window-prev-buffers)))) + (start (and entry (nth 1 entry))) + (pos (and entry (nth 2 entry)))) + (set-window-buffer nil buffer) + (when entry + ;; If BUFFER-OR-NAME (1) was shown in the selected window + ;; before and (2) is currently displayed in some other + ;; visible window, try to restore start and point of buffer + ;; in the selected window. + (set-window-start (selected-window) start t) + (set-window-point-1 nil pos))))) (unless norecord (select-window (selected-window)))