emacs-devel
[Top][All Lists]
Advanced

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

Re: Missing `with' macro?


From: Jorgen Schaefer
Subject: Re: Missing `with' macro?
Date: Mon, 24 Jul 2006 22:55:31 +0200
User-agent: Gnus/5.110006 (No Gnus v0.6) Emacs/22.0.50 (gnu/linux)

Richard Stallman <address@hidden> 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 . "address@hidden") (www . "http://www.forcix.cx/";)
 (gpg   . "1024D/028AF63C")   (irc . "nick forcer on IRCnet"))





reply via email to

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