Function: org-element-with-buffer-copy

org-element-with-buffer-copy is a macro defined in org-element.el.gz.

Signature

(org-element-with-buffer-copy &rest BODY &key TO-BUFFER DROP-VISIBILITY DROP-NARROWING DROP-CONTENTS DROP-LOCALS &allow-other-keys)

Documentation

Apply BODY in a copy of the current buffer.

The copy preserves local variables, visibility and contents of the original buffer. Point is at the beginning of the buffer when BODY is applied.

Optional keys can modify what is being copied and the generated buffer copy. TO-BUFFER, DROP-VISIBILITY, DROP-NARROWING, DROP-CONTENTS, and DROP-LOCALS are passed as arguments to org-element-copy-buffer.

Source Code

;; Defined in /usr/src/emacs/lisp/org/org-element.el.gz
(cl-defmacro org-element-with-buffer-copy ( &rest body
                                            &key to-buffer drop-visibility
                                            drop-narrowing drop-contents
                                            drop-locals
                                            &allow-other-keys)
  "Apply BODY in a copy of the current buffer.
The copy preserves local variables, visibility and contents of
the original buffer.  Point is at the beginning of the buffer
when BODY is applied.

Optional keys can modify what is being copied and the generated buffer
copy.  TO-BUFFER, DROP-VISIBILITY, DROP-NARROWING, DROP-CONTENTS, and
DROP-LOCALS are passed as arguments to `org-element-copy-buffer'."
  (declare (debug t))
  ;; Drop keyword arguments from BODY.
  (while (keywordp (car body)) (pop body) (pop body))
  (org-with-gensyms (buf-copy)
    `(let ((,buf-copy (org-element-copy-buffer
                       :to-buffer ,to-buffer
                       :drop-visibility ,drop-visibility
                       :drop-narrowing ,drop-narrowing
                       :drop-contents ,drop-contents
                       :drop-locals ,drop-locals)))
       (unwind-protect
	   (with-current-buffer ,buf-copy
	     (goto-char (point-min))
             (prog1
                 (progn ,@body)
               ;; `org-element-copy-buffer' carried the value of
               ;; `buffer-file-name' from the original buffer.  When not
               ;; killed, the new buffer copy may become a target of
               ;; `find-file'.  Prevent this.
               (setq buffer-file-name nil)))
	 (and (buffer-live-p ,buf-copy)
	      ;; Kill copy without confirmation.
	      (progn (with-current-buffer ,buf-copy
		       (restore-buffer-modified-p nil))
                     (unless ,to-buffer
		       (kill-buffer ,buf-copy))))))))