From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Jorgen Schaefer Newsgroups: gmane.emacs.devel Subject: Re: Missing `with' macro? Date: Mon, 24 Jul 2006 22:55:31 +0200 Message-ID: <87fygqyb7g.fsf@forcix.kollektiv-hamburg.de> References: <7dbe73ed0607240317g1bcdd564g66d075f809bcb7b2@mail.gmail.com> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1153774601 20264 80.91.229.2 (24 Jul 2006 20:56:41 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Mon, 24 Jul 2006 20:56:41 +0000 (UTC) Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Mon Jul 24 22:56:34 2006 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by ciao.gmane.org with esmtp (Exim 4.43) id 1G57TR-0007nd-L6 for ged-emacs-devel@m.gmane.org; Mon, 24 Jul 2006 22:56:23 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1G57TQ-0005Qu-LB for ged-emacs-devel@m.gmane.org; Mon, 24 Jul 2006 16:56:20 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1G57TG-0005Qm-3H for emacs-devel@gnu.org; Mon, 24 Jul 2006 16:56:10 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1G57TE-0005QB-J0 for emacs-devel@gnu.org; Mon, 24 Jul 2006 16:56:09 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1G57TE-0005Q8-Ev for emacs-devel@gnu.org; Mon, 24 Jul 2006 16:56:08 -0400 Original-Received: from [80.91.229.2] (helo=ciao.gmane.org) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA:32) (Exim 4.52) id 1G57UO-0006jK-9Q for emacs-devel@gnu.org; Mon, 24 Jul 2006 16:57:20 -0400 Original-Received: from list by ciao.gmane.org with local (Exim 4.43) id 1G57T0-0007hD-Ll for emacs-devel@gnu.org; Mon, 24 Jul 2006 22:55:54 +0200 Original-Received: from c173105.adsl.hansenet.de ([213.39.173.105]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 24 Jul 2006 22:55:54 +0200 Original-Received: from forcer by c173105.adsl.hansenet.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 24 Jul 2006 22:55:54 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-To: emacs-devel@gnu.org Original-Lines: 44 Original-X-Complaints-To: usenet@sea.gmane.org X-Gmane-NNTP-Posting-Host: c173105.adsl.hansenet.de User-Agent: Gnus/5.110006 (No Gnus v0.6) Emacs/22.0.50 (gnu/linux) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:57574 Archived-At: Richard Stallman 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"))