From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Sean McAfee Newsgroups: gmane.emacs.help Subject: Processing a file that may already be open Date: Sun, 16 Oct 2011 23:57:54 -0700 Organization: A noiseless patient Spider Message-ID: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: dough.gmane.org 1332966425 30260 80.91.229.3 (28 Mar 2012 20:27:05 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Wed, 28 Mar 2012 20:27:05 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Mar 28 22:27:05 2012 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1SCzSS-0004Cq-KG for geh-help-gnu-emacs@m.gmane.org; Wed, 28 Mar 2012 22:27:04 +0200 Original-Received: from localhost ([::1]:39108 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SCzSR-0006nm-KI for geh-help-gnu-emacs@m.gmane.org; Wed, 28 Mar 2012 16:27:03 -0400 Original-Path: usenet.stanford.edu!news.glorb.com!feeder.erje.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 24 Injection-Info: mx04.eternal-september.org; posting-host="XVJ+HMFWLEVITYyjSp06tg"; logging-data="24804"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18KAZhIc3La5KXJM+ruBczPa2TBbDHnVRU=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.90 (darwin) Cancel-Lock: sha1:5gEjkSfKzvg6yLyA8YtQy0YkQ20= sha1:xLKGInooYDOyPnhd//C27XAy7Mg= Original-Xref: usenet.stanford.edu gnu.emacs.help:189342 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:84134 Archived-At: I'd like to be able to write a routine that opens a file and reads it, and potentially modifies its contents and saves it back to disk. The slightly tricky bit is that if there are any open buffers visiting that file, they should be updated immediately. (I don't want to modify the file behind any buffers' backs, that is.) I've written the following macro to encapsulate what seems to be the necessary logic: (defmacro with-visited-file (path &rest body) (let ((retain (gensym)) (buffer (gensym))) `(let* ((,retain (find-buffer-visiting ,path)) (,buffer (find-file ,path))) (unwind-protect (save-excursion (save-restriction (widen) (beginning-of-buffer) ,@body)) (or ,retain (kill-buffer ,buffer)))))) It seems to work okay, but are there any lurking gotchas? Or, is there any better approach?