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

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

Re: save-buffer like save-excursion


From: Matthew Flaschen
Subject: Re: save-buffer like save-excursion
Date: Sun, 18 Mar 2007 05:56:39 -0400
User-agent: Thunderbird 1.5.0.10 (X11/20070306)

Harald Hanche-Olsen wrote:
> + Matthew Flaschen <address@hidden>:
> 
> | Is there a command like save-excursion that only saves the buffer?  I
> | have several functions that must set-buffer temporarily, then do things
> | that change point.  I always want these point changes, but if set-buffer
> | changed the buffer (not necessarily true), I want to reverse that change.
> |
> | So, is there already a command for this?
> 
> You are aware of the difference between set-buffer and
> swith-to-buffer, I presume?

I think so.  set-buffer doesn't change the UI, only the working buffer.

  Even if a function does set-buffer, at
> the end of an interactive command you are back in the buffer you had.

Right...but I'd like to preserve the buffer within interactive commands.

> So there may not be a need to do what you want at all.
> 
> On the other hand, if you want to temporarily change the buffer within
> a function, you can use macro like this one:

> (defmacro with-saved-buffer (&rest command)
>   (let ((cur (make-symbol "cur")))
>    `(let ((,cur (current-buffer)))
>      (unwind-protect
>        (progn ,@command)
>        (set-buffer ,cur)))))
> 
> and use it like
> 
>   (with-saved-buffer
>    (set-buffer "blah")
>    ...)
>   ;; here, the buffer is back to what it was


Right, thanks.  I'll probably use something similar, or just do it
manually (depending on how often it's necessary).  I might also be able
to avoid the issue neatly just by calling set-buffer again in the client
code.  I was just wondering if there was already something, so I
wouldn't be wasting my time.

> The unwind-protect in there is not necessary in the case where an
> error brings you back to the interactive top-level.

I think that's the case for me.  But I won't have any errors anyway! My
code is tested. ;)

Matt Flaschen




reply via email to

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