From: Jorgen Schaefer <forcer@forcix.cx>
Subject: Re: Missing `with' macro?
Date: Mon, 24 Jul 2006 22:55:31 +0200 [thread overview]
Message-ID: <87fygqyb7g.fsf@forcix.kollektiv-hamburg.de> (raw)
In-Reply-To: E1G554h-0002Ol-5n@fencepost.gnu.org
Richard Stallman <rms@gnu.org> writes:
> We don't usually call find-file for such temporary purposes.
> We usually use code like this:
>
> (with-temp-buffer
> (insert-file-contents file)
> operate on the file)
>
> except that if the file's already visited in a buffer,
> it is more efficient to use that buffer (and not kill it).
>
> To develop a general macro for this would be useful. It is not a
> trivial thing. Please start working on it if you want.
I have written such a macro for my program to generate my weblog
and rss feed:
(put 'with-file 'lisp-indent-function 1)
(defmacro with-file (file &rest body)
"Run BODY in a buffer visiting FILE.
If the buffer was not open before calling this macro, it's killed
afterwards."
(let ((file-sym (make-symbol "file"))
(existsp-sym (make-symbol "existsp"))
(buf-sym (make-symbol "buf")))
`(let* ((,file-sym ,file)
(,existsp-sym (get-file-buffer ,file-sym))
(,buf-sym (find-file-noselect ,file-sym)))
(unwind-protect
(with-current-buffer ,buf-sym
(save-excursion
(save-restriction
(widen)
,@body)))
(when (not ,existsp-sym)
(kill-buffer ,buf-sym))))))
Regards,
-- Jorgen
--
((email . "forcer@forcix.cx") (www . "http://www.forcix.cx/")
(gpg . "1024D/028AF63C") (irc . "nick forcer on IRCnet"))
next prev parent reply other threads:[~2006-07-24 20:55 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-07-24 10:17 Missing `with' macro? Mathias Dahl
2006-07-24 13:46 ` Stefan Monnier
2006-07-24 14:33 ` Mathias Dahl
2006-07-24 18:22 ` Richard Stallman
2006-07-24 20:55 ` Jorgen Schaefer [this message]
2006-07-25 3:09 ` Richard Stallman
2006-07-28 2:14 ` Stuart D. Herring
2006-07-28 2:23 ` Stuart D. Herring
2006-07-29 15:18 ` Richard Stallman
2006-08-01 1:06 ` Stuart D. Herring
2006-08-07 5:01 ` Richard Stallman
2006-08-07 21:38 ` Stuart D. Herring
2006-08-08 18:01 ` Richard Stallman
2006-08-08 18:32 ` Stuart D. Herring
2006-08-09 4:58 ` Richard Stallman
2006-08-08 18:01 ` Richard Stallman
2006-08-16 19:59 ` Stuart D. Herring
2006-08-17 15:18 ` Richard Stallman
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87fygqyb7g.fsf@forcix.kollektiv-hamburg.de \
--to=forcer@forcix.cx \
/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.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.