From: eeide%asylum.cs.utah.edu@cs.utah.edu (Eric Eide)
Subject: Re: switch-to-buffer : how to require an existing buffer ??
In-reply-to: delacour@waxwing.parc.xerox.com's message of 26 Aug 92 11:50:23
Organization: University of Utah Department of Computer Science
Date: 29 Aug 92 19:05:05

>Does somebody know the right way to have switch-to-buffer and
>switch-to-buffer-other-window require an existing buffer ?

Below are the functions that I use.  Have fun.

-------------------------------------------------------------------------------

(global-set-key "\C-xb" 'switch-to-existing-buffer)
(global-set-key "\C-x4b" 'switch-to-existing-buffer-other-window)

;;;
;;; Command switch-to-existing-buffer
;;;
;;; switch-to-existing-buffer is a replacement for switch-to-buffer.  Unlike
;;; switch-to-buffer, when used interactively this function does not allow the
;;; user to create a new buffer.
;;;

(defun switch-to-existing-buffer (buffer &optional norecord)
  "Select buffer BUFFER in the current window.  BUFFER may be a buffer or a
buffer name.  Optional second arg NORECORD non-nil means do not put this buffer
at the front of the list of recently selected ones.

When called interactively, this function will not create a new buffer.

WARNING: This is NOT the way to work on another buffer temporarily within a
Lisp program!  Use `set-buffer' instead.  That avoids messing with the
window-buffer correspondences."
  ;; NOTE that (interactive "bSwitch to buffer: ") would use the current buffer
  ;; as the default -- not very useful here.
  (interactive (list (read-buffer "Switch to buffer: "
				  (buffer-name (other-buffer)) t)))
  (switch-to-buffer buffer norecord))

;;;
;;; Command switch-to-existing-buffer-other-window
;;;
;;; switch-to-existing-buffer-other-window is a replacement for switch-to-
;;; buffer-other-window.  Unlike switch-to-buffer-other-window, when used
;;; interactively this function does not allow the user to create a new buffer.
;;;

(defun switch-to-existing-buffer-other-window (buffer)
  "Select buffer BUFFER in another window.  When called interactively, this
function will not create a new buffer."
  ;; NOTE that (interactive "bSwitch to buffer in other window: ") would use
  ;; the current buffer as the default -- not very useful here.
  (interactive (list (read-buffer "Switch to buffer in other window: "
				  (buffer-name (other-buffer)) t)))
  (let ((pop-up-windows t))
    (pop-to-buffer buffer t)))

;;;
;;; Function print-help-return-message
;;;
;;; This is a hacked version of print-help-return-messge from the GNU Emacs
;;; file help.el; it looks for my buffer-switching functions rather than the
;;; standard ones.
;;;

(defun print-help-return-message (&optional function)
  "Display or return message saying how to restore windows after help command.
Computes a message and applies the argument FUNCTION to it.
If FUNCTION is nil, applies `message' to it, thus printing it."
  (and (not (get-buffer-window standard-output))
       (funcall	(or function 'message)
		(substitute-command-keys
		 (if (one-window-p t)
		     (if pop-up-windows
			 "Type \\[delete-other-windows] to remove help window."
		       "Type \\[switch-to-existing-buffer] RET to remove help window.")
		   "Type \\[switch-to-existing-buffer-other-window] RET to restore old contents of help window.")))))

;; End of functions.

--
-------------------------------------------------------------------------------
Eric Eide          |          University of Utah Department of Computer Science
eeide@cs.utah.edu  | Buddhist to hot dog vendor: "Make me one with everything."