Function: org-edit-latex-fragment

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

Signature

(org-edit-latex-fragment)

Documentation

Edit LaTeX fragment at point.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/org/org-src.el.gz
(defun org-edit-latex-fragment ()
  "Edit LaTeX fragment at point."
  (interactive)
  (let ((context (org-element-context)))
    (unless (and (org-element-type-p context 'latex-fragment)
		 (org-src--on-datum-p context))
      (user-error "Not on a LaTeX fragment"))
    (let* ((contents
	    (buffer-substring-no-properties
	     (org-element-begin context)
	     (- (org-element-end context)
		(org-element-post-blank context))))
	   (delim-length (if (string-match "\\`\\$[^$]" contents) 1 2)))
      ;; Make the LaTeX deliminators read-only.
      (add-text-properties 0 delim-length
			   (list 'read-only "Cannot edit LaTeX deliminator"
				 'front-sticky t
				 'rear-nonsticky t)
			   contents)
      (let ((l (length contents)))
	(add-text-properties (- l delim-length) l
			     (list 'read-only "Cannot edit LaTeX deliminator"
				   'front-sticky nil
				   'rear-nonsticky nil)
			     contents))
      (org-src--edit-element
       context
       (org-src--construct-edit-buffer-name (buffer-name) "LaTeX fragment")
       (org-src-get-lang-mode "latex")
       (lambda ()
	 ;; Blank lines break things, replace with a single newline.
	 (while (re-search-forward "\n[ \t]*\n" nil t) (replace-match "\n"))
	 ;; If within a table a newline would disrupt the structure,
	 ;; so remove newlines.
	 (goto-char (point-min))
	 (when (org-element-lineage context 'table-cell)
	   (while (search-forward "\n" nil t) (replace-match " "))))
       contents))
    t))