Function: org-src--edit-element

org-src--edit-element is a byte-compiled function defined in org-src.el.gz.

Signature

(org-src--edit-element DATUM NAME &optional INITIALIZE WRITE-BACK CONTENTS REMOTE)

Documentation

Edit DATUM contents in a dedicated buffer NAME.

INITIALIZE is a function to call upon creating the buffer.

When WRITE-BACK is non-nil, assume contents will replace original region. Moreover, if it is a function, apply it in the edit buffer, from point min, before returning the contents.

When CONTENTS is non-nil, display them in the edit buffer. Otherwise, show DATUM contents as specified by org-src--contents-area.

When REMOTE is non-nil, do not try to preserve point or mark when moving from the edit area to the source.

Leave point in edit buffer.

Source Code

;; Defined in /usr/src/emacs/lisp/org/org-src.el.gz
(defun org-src--edit-element
    (datum name &optional initialize write-back contents remote)
  "Edit DATUM contents in a dedicated buffer NAME.

INITIALIZE is a function to call upon creating the buffer.

When WRITE-BACK is non-nil, assume contents will replace original
region.  Moreover, if it is a function, apply it in the edit
buffer, from point min, before returning the contents.

When CONTENTS is non-nil, display them in the edit buffer.
Otherwise, show DATUM contents as specified by
`org-src--contents-area'.

When REMOTE is non-nil, do not try to preserve point or mark when
moving from the edit area to the source.

Leave point in edit buffer."
  (when (memq org-src-window-setup '(reorganize-frame
				     split-window-below
				     split-window-right))
    (setq org-src--saved-temp-window-config (current-window-configuration)))
  (let* ((area (org-src--contents-area datum))
	 (beg (copy-marker (nth 0 area)))
	 (end (copy-marker (nth 1 area) t))
	 (old-edit-buffer (org-src--edit-buffer beg end))
	 (contents (or contents (nth 2 area))))
    (if (and old-edit-buffer
	     (or (not org-src-ask-before-returning-to-edit-buffer)
		 (y-or-n-p "Return to existing edit buffer ([n] will revert changes)? ")))
	;; Move to existing buffer.
	(org-src-switch-to-buffer old-edit-buffer 'return)
      ;; Discard old edit buffer.
      (when old-edit-buffer
	(with-current-buffer old-edit-buffer (org-src--remove-overlay))
	(kill-buffer old-edit-buffer))
      (let* ((org-mode-p (derived-mode-p 'org-mode))
	     (source-file-name (buffer-file-name (buffer-base-buffer)))
	     (source-tab-width (if indent-tabs-mode tab-width 0))
	     (type (org-element-type datum))
	     (block-ind (org-with-point-at (org-element-begin datum)
                          (cond
                           ((save-excursion (skip-chars-backward " \t") (bolp))
			    (org-current-text-indentation))
                           ((org-element-parent datum)
                            (org--get-expected-indentation
                             (org-element-parent datum) nil))
                           (t (org-current-text-indentation)))))
	     (content-ind org-edit-src-content-indentation)
	     (preserve-ind (org-src-preserve-indentation-p datum))
	     ;; Store relative positions of mark (if any) and point
	     ;; within the edited area.
	     (point-coordinates (and (not remote)
				     (org-src--coordinates (point) beg end)))
	     (mark-coordinates (and (not remote)
				    (org-region-active-p)
				    (let ((m (mark)))
				      (and (>= m beg) (>= end m)
					   (org-src--coordinates m beg end)))))
	     ;; Generate a new edit buffer.
	     (buffer (generate-new-buffer name))
	     ;; Add an overlay on top of source.
	     (overlay (org-src--make-source-overlay beg end buffer)))
	;; Switch to edit buffer.
	(org-src-switch-to-buffer buffer 'edit)
	;; Insert contents.
	(insert contents)
	(remove-text-properties (point-min) (point-max)
				'(display nil invisible nil intangible nil))
	(let ((lf (eq type 'latex-fragment)))
          (unless preserve-ind (org-do-remove-indentation (and lf block-ind) lf)))
	(set-buffer-modified-p nil)
	(setq buffer-file-name nil)
	;; Initialize buffer.
	(when (functionp initialize)
	  (let ((org-inhibit-startup t))
	    (condition-case-unless-debug e
		(funcall initialize)
	      (error (message "Initialization fails with: %S"
			      (error-message-string e))))))
	;; Transmit buffer-local variables for exit function.  It must
	;; be done after initializing major mode, as this operation
	;; may reset them otherwise.
	(setq org-src--tab-width source-tab-width)
	(setq org-src--from-org-mode org-mode-p)
	(setq org-src--beg-marker beg)
	(setq org-src--end-marker end)
	(setq org-src--remote remote)
	(setq org-src--source-type type)
	(setq org-src--block-indentation block-ind)
	(setq org-src--content-indentation content-ind)
	(setq org-src--preserve-indentation preserve-ind)
	(setq org-src--overlay overlay)
	(setq org-src--allow-write-back write-back)
	(setq org-src-source-file-name source-file-name)
	;; Start minor mode.
	(org-src-mode)
	;; Clear undo information so we cannot undo back to the
	;; initial empty buffer.
	(buffer-disable-undo (current-buffer))
	(buffer-enable-undo)
	;; Move mark and point in edit buffer to the corresponding
	;; location.
	(if remote
	    (progn
	      ;; Put point at first non read-only character after
	      ;; leading blank.
	      (goto-char
	       (or (text-property-any (point-min) (point-max) 'read-only nil)
		   (point-max)))
	      (skip-chars-forward " \r\t\n"))
	  ;; Set mark and point.
	  (when mark-coordinates
	    (org-src--goto-coordinates mark-coordinates (point-min) (point-max))
	    (push-mark (point) 'no-message t)
	    (setq deactivate-mark nil))
	  (org-src--goto-coordinates
	   point-coordinates (point-min) (point-max)))))))