Skip to content

Instantly share code, notes, and snippets.

@nxtr
Created August 16, 2018 15:02
Show Gist options
  • Save nxtr/83ac3c549aff23428d130c1993918893 to your computer and use it in GitHub Desktop.
Save nxtr/83ac3c549aff23428d130c1993918893 to your computer and use it in GitHub Desktop.
with-file-temp-buffer: (FILENAME &rest BODY)
(defmacro with-file-temp-buffer (filename &rest body)
"Create a temporary buffer visiting FILENAME, and evaluate BODY there like
`progn'."
(declare (indent 1) (debug t))
(let* ((temp-buffer (make-symbol "temp-buffer")))
`(let* ((truename (abbreviate-file-name (file-truename ,filename)))
(number (nthcdr 10 (file-attributes truename)))
(,temp-buffer (find-file-noselect-1
(create-file-buffer ,filename)
,filename 'nowarn nil truename number)))
(with-current-buffer ,temp-buffer
(unwind-protect
(progn ,@body)
(and (buffer-name ,temp-buffer)
(kill-buffer ,temp-buffer)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment