help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: save-current-buffer


From: Nikolaj Schumacher
Subject: Re: save-current-buffer
Date: Sun, 19 Oct 2008 17:44:01 +0200
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.3 (darwin)

Andreas Politz <politza@fh-trier.de> wrote:

> Is this a bug, or am I missing something ?
> Eval this in *scratch* buffer and you end up in the *Help* one.
>
> (save-current-buffer
>   (switch-to-buffer "*Help*"))

There is a subtle difference between the current-buffer and the window's
buffer.  Any buffer can be current without being displayed.  The
function for that is `set-buffer'.

`switch-to-buffer' changes both the current-buffer and the selected window's
buffer.

(progn
  (message "%s/%s" (current-buffer) (window-buffer (selected-window)))
  (save-current-buffer
    (switch-to-buffer "*Help*")
    (message "%s/%s" (current-buffer) (window-buffer (selected-window))))
  (message "%s/%s" (current-buffer) (window-buffer (selected-window))))

will output:

*scratch*/*scratch*
*Help*/*Help*
*scratch*/*Help*

As you can see, current-buffer is restored, but the window's
buffer is not.

Replacing `save-current-buffer' with `save-window-excursion'

gets you:

*scratch*/*scratch*
*Help*/*Help*
*scratch*/*scratch*


But generally, `switch-to-buffer' should only be used when the user
wants to see the new buffer.  Otherwise `set-buffer' is the better choice.



regards,
Nikolaj Schumacher




reply via email to

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