unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Harald Hanche-Olsen <hanche@math.ntnu.no>
To: help-gnu-emacs@gnu.org
Subject: Re: save-buffer like save-excursion
Date: Sun, 18 Mar 2007 10:23:08 +0100	[thread overview]
Message-ID: <pcofy82j477.fsf@shuttle.math.ntnu.no> (raw)
In-Reply-To: mailman.1069.1174204758.7795.help-gnu-emacs@gnu.org

+ Matthew Flaschen <matthew.flaschen@gatech.edu>:

| 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	other threads:[~2007-03-18  9:23 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.1069.1174204758.7795.help-gnu-emacs@gnu.org>
2007-03-18  9:23 ` Harald Hanche-Olsen [this message]
2007-03-18  9:56   ` save-buffer like save-excursion Matthew Flaschen
2007-03-19  3:54 ` Stefan Monnier
2007-03-19  5:30   ` Matthew Flaschen
2007-03-19  7:33   ` Harald Hanche-Olsen
2007-03-19 21:36     ` Matthew Flaschen
     [not found]     ` <mailman.1132.1174340311.7795.help-gnu-emacs@gnu.org>
2007-03-21 19:42       ` Stefan Monnier
2007-03-23  1:35         ` Matthew Flaschen
2007-03-18  7:57 Matthew Flaschen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=pcofy82j477.fsf@shuttle.math.ntnu.no \
    --to=hanche@math.ntnu.no \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).