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: Harald Hanche-Olsen
Subject: Re: save-buffer like save-excursion
Date: Sun, 18 Mar 2007 10:23:08 +0100
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/23.0.0 (berkeley-unix)

+ 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?  Even if a function does set-buffer, at
the end of an interactive command you are back in the buffer you had.
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

The unwind-protect in there is not necessary in the case where an
error brings you back to the interactive top-level.  But if your code
performs a non-local exit for some reason, it's essential.

-- 
* Harald Hanche-Olsen     <URL:http://www.math.ntnu.no/~hanche/>
- It is undesirable to believe a proposition
  when there is no ground whatsoever for supposing it is true.
  -- Bertrand Russell


reply via email to

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