Function: org-export--generate-copy-script

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

Signature

(org-export--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/ox.el.gz
(cl-defun org-export--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'."
  (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 ((bound-variables (org-export--list-bound-variables))
		   (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-export-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))
			      (assq var bound-variables)
			      (string-match "^\\(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)
                                 invis-prop)
			   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 and variables set
	  ;; through BIND keywords.
	  (pcase-dolist (`(,var . ,val) varvals)
	    (set (make-local-variable var) val))
	  ;; Whole buffer contents when requested.
          (when str (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 ,invis) ols)
	    (overlay-put (make-overlay start end) 'invisible invis))
          ;; Never write the buffer copy to disk, despite
          ;; `buffer-file-name' not being nil.
          (setq write-contents-functions (list (lambda (&rest _) t))))))))