Function: org-edit-src-exit

org-edit-src-exit is an interactive and byte-compiled function defined in org-src.el.gz.

Signature

(org-edit-src-exit)

Documentation

Kill current sub-editing buffer and return to source buffer.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/org/org-src.el.gz
(defun org-edit-src-exit ()
  "Kill current sub-editing buffer and return to source buffer."
  (interactive)
  (unless (org-src-edit-buffer-p)
    (error "Not in a sub-editing buffer"))
  (let* ((beg org-src--beg-marker)
	 (end org-src--end-marker)
	 (write-back org-src--allow-write-back)
	 (remote org-src--remote)
	 (coordinates (and (not remote)
			   (org-src--coordinates (point) 1 (point-max))))
	 (write-back-buf
          (and write-back (generate-new-buffer "*org-src-write-back*"))))
    (when write-back (org-src--contents-for-write-back write-back-buf))
    (set-buffer-modified-p nil)
    ;; Switch to source buffer.  Kill sub-editing buffer.
    (let ((edit-buffer (current-buffer))
	  (source-buffer (marker-buffer beg)))
      (unless source-buffer
        (when write-back-buf (kill-buffer write-back-buf))
        (error "Source buffer disappeared.  Aborting"))
      (org-src-switch-to-buffer source-buffer 'exit)
      (kill-buffer edit-buffer))
    ;; Insert modified code.  Ensure it ends with a newline character.
    (org-with-wide-buffer
     (when (and write-back
                (not (equal (buffer-substring beg end)
			    (with-current-buffer write-back-buf
                              (buffer-string)))))
       (undo-boundary)
       (goto-char beg)
       (let ((expecting-bol (bolp)))
	 (if (version< emacs-version "27.1")
	     (progn (delete-region beg end)
		    (insert (with-current-buffer write-back-buf
                              (buffer-string))))
	   (save-restriction
	     (narrow-to-region beg end)
	     (org-replace-buffer-contents write-back-buf 0.1 nil)
	     (goto-char (point-max))))
	 (when (and expecting-bol (not (bolp))) (insert "\n")))))
    (when write-back-buf (kill-buffer write-back-buf))
    ;; If we are to return to source buffer, put point at an
    ;; appropriate location.  In particular, if block is hidden, move
    ;; to the beginning of the block opening line.
    (unless remote
      (goto-char beg)
      (cond
       ;; Block is hidden; move at start of block.
       ((org-fold-folded-p nil 'block) (forward-line -1))
       (write-back (org-src--goto-coordinates coordinates beg end))))
    ;; Clean up left-over markers and restore window configuration.
    (set-marker beg nil)
    (set-marker end nil)
    (when org-src--saved-temp-window-config
      (unwind-protect
	  (set-window-configuration org-src--saved-temp-window-config)
	(setq org-src--saved-temp-window-config nil)))))