Function: org-element--generate-copy-script

org-element--generate-copy-script is a byte-compiled function defined in org-element.el.gz.

Signature

(org-element--generate-copy-script BUFFER &key COPY-UNREADABLE DROP-VISIBILITY DROP-NARROWING DROP-CONTENTS DROP-LOCALS)

Documentation

Generate a function duplicating BUFFER.

The copy will preserve local variables, visibility, contents and narrowing of the original buffer. If a region was active in BUFFER, contents will be narrowed to that region instead.

When optional key COPY-UNREADABLE is non-nil, do not ensure that all the copied local variables will be readable in another Emacs session.

When optional keys DROP-VISIBILITY, DROP-NARROWING, DROP-CONTENTS, or DROP-LOCALS are non-nil, do not preserve visibility, narrowing, contents, or local variables correspondingly.

The resulting function can be evaluated at a later time, from another buffer, effectively cloning the original buffer there.

The function assumes BUFFER's major mode is org-mode.

Source Code

;; Defined in /usr/src/emacs/lisp/org/org-element.el.gz
(cl-defun org-element--generate-copy-script (buffer
                                             &key
                                             copy-unreadable
                                             drop-visibility
                                             drop-narrowing
                                             drop-contents
                                             drop-locals)
  "Generate a function duplicating BUFFER.

The copy will preserve local variables, visibility, contents and
narrowing of the original buffer.  If a region was active in
BUFFER, contents will be narrowed to that region instead.

When optional key COPY-UNREADABLE is non-nil, do not ensure that all
the copied local variables will be readable in another Emacs session.

When optional keys DROP-VISIBILITY, DROP-NARROWING, DROP-CONTENTS, or
DROP-LOCALS are non-nil, do not preserve visibility, narrowing,
contents, or local variables correspondingly.

The resulting function can be evaluated at a later time, from
another buffer, effectively cloning the original buffer there.

The function assumes BUFFER's major mode is `org-mode'."
  (declare-function org-fold-core--update-buffer-folds "org-fold-core" ())
  (require 'org-fold-core)
  (with-current-buffer buffer
    (let ((str (unless drop-contents (org-with-wide-buffer (buffer-string))))
          (narrowing
           (unless drop-narrowing
             (if (org-region-active-p)
                 (list (region-beginning) (region-end))
	       (list (point-min) (point-max)))))
	  (pos (point))
	  (varvals
           (unless drop-locals
	     (let ((varvals nil))
	       (dolist (entry (buffer-local-variables (buffer-base-buffer)))
                 (when (consp entry)
		   (let ((var (car entry))
                         (val (cdr entry)))
		     (and (not (memq var org-element-ignored-local-variables))
			  (or (memq var
				    '(default-directory
                                      ;; Required to convert file
                                      ;; links in the #+INCLUDEd
                                      ;; files.  See
                                      ;; `org-export--prepare-file-contents'.
				      buffer-file-name
				      buffer-file-coding-system
                                      ;; Needed to preserve folding state
                                      char-property-alias-alist))
			      (string-match-p "^\\(org-\\|orgtbl-\\)"
					      (symbol-name var)))
			  ;; Skip unreadable values, as they cannot be
			  ;; sent to external process.
			  (or copy-unreadable (not val)
                              (ignore-errors (read (format "%S" val))))
			  (push (cons var val) varvals)))))
               varvals)))
	  (ols
           (unless drop-visibility
	     (let (ov-set)
	       (dolist (ov (overlays-in (point-min) (point-max)))
                 (let ((invis-prop (overlay-get ov 'invisible)))
		   (when invis-prop
		     (push (list (overlay-start ov) (overlay-end ov)
                                 (overlay-properties ov))
			   ov-set))))
	       ov-set))))
      (lambda ()
	(let ((inhibit-modification-hooks t))
	  ;; Set major mode. Ignore `org-mode-hook' and other hooks as
	  ;; they have been run already in BUFFER.
          (unless (eq major-mode 'org-mode)
            (delay-mode-hooks
              (let ((org-inhibit-startup t)) (org-mode))))
	  ;; Copy specific buffer local variables.
	  (pcase-dolist (`(,var . ,val) varvals)
	    (set (make-local-variable var) val))
	  ;; Whole buffer contents when requested.
          (when str
            (let ((inhibit-read-only t))
              (erase-buffer) (insert str)))
          ;; Make org-element-cache not complain about changed buffer
          ;; state.
          (org-element-cache-reset nil 'no-persistence)
	  ;; Narrowing.
          (when narrowing
	    (apply #'narrow-to-region narrowing))
	  ;; Current position of point.
	  (goto-char pos)
	  ;; Overlays with invisible property.
	  (pcase-dolist (`(,start ,end ,props) ols)
            (let ((ov (make-overlay start end)))
              (while props
                (overlay-put ov (pop props) (pop props)))))
          ;; Text property folds.
          (unless drop-visibility (org-fold-core--update-buffer-folds))
          ;; Never write the buffer copy to disk, despite
          ;; `buffer-file-name' not being nil.
          (setq write-contents-functions (list (lambda (&rest _) t))))))))