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

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

Re: save-current-buffer


From: Andreas Politz
Subject: Re: save-current-buffer
Date: Tue, 21 Oct 2008 13:26:35 +0200
User-agent: Mozilla-Thunderbird 2.0.0.16 (X11/20080724)

Nikolaj Schumacher wrote:
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



I see. I assume after the form is evaluated, some event will set the 
current-buffer
to the one switch-to-buffer switched to.

The reason I was asking is some problem I had with the recentf package. It's
really a minor one, but I was just curious what to make of it and how things 
are working.
The idiom from above is used in one of it's functions.

(require 'recentf)
(run-with-timer 0 nil 'recentf-open-files)

Now pressing 'q' gives a read-only error, though it is
bound to recentf-cancel-dialog. Pressing it twice works fine.

-ap


reply via email to

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