all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Processing a file that may already be open
@ 2011-10-17  6:57 Sean McAfee
       [not found] ` <jwvobxfpvav.fsf-monnier+gnu.emacs.help@gnu.org>
  0 siblings, 1 reply; 2+ messages in thread
From: Sean McAfee @ 2011-10-17  6:57 UTC (permalink / raw)
  To: help-gnu-emacs

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?


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2011-10-18  6:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-10-17  6:57 Processing a file that may already be open Sean McAfee
     [not found] ` <jwvobxfpvav.fsf-monnier+gnu.emacs.help@gnu.org>
2011-10-18  6:45   ` Sean McAfee

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.