Function: org-with-file-buffer

org-with-file-buffer is a macro defined in org-macs.el.

Signature

(org-with-file-buffer FILE &rest BODY)

Documentation

Evaluate BODY with current buffer visiting FILE.

When no live buffer is visiting FILE, create one and kill after evaluating BODY. During evaluation, when the buffer was created, org-file-buffer-created variable is set to FILE.

Source Code

;; Defined in ~/.emacs.d/elpa/org-9.8.2/org-macs.el
(defmacro org-with-file-buffer (file &rest body)
  "Evaluate BODY with current buffer visiting FILE.
When no live buffer is visiting FILE, create one and kill after
evaluating BODY.
During evaluation, when the buffer was created, `org-file-buffer-created'
variable is set to FILE."
  (declare (debug (form body)) (indent 1))
  (org-with-gensyms (mark-function filename buffer)
    `(let ((,mark-function (lambda () (setq-local org-file-buffer-created ,file)))
           (,filename ,file)
           ,buffer)
       (add-hook 'find-file-hook ,mark-function)
       (unwind-protect
           (progn
             (setq ,buffer (find-file-noselect ,filename t))
             (with-current-buffer ,buffer
               (prog1 (progn ,@body)
                 (with-current-buffer ,buffer
                   (when (equal ,filename org-file-buffer-created)
                     (kill-buffer))))))
         (remove-hook 'find-file-hook ,mark-function)))))