>From 748c20d65df9cd0ae9f9f80c2bb5ee87aea101e4 Mon Sep 17 00:00:00 2001
From: TEC <t...@tecosaur.com>
Date: Sun, 24 May 2020 23:35:33 +0800
Subject: [PATCH] Extend org-edit-special to editing LaTeX-fragments Defines a
 new function, `org-edit-latex-fragment' which is hooked into
 `org-edit-special', modifying `org-src--contents-area-modified' to recognise
 the element type.

---
 lisp/org-src.el | 29 +++++++++++++++++++++++++++--
 lisp/org.el     |  1 +
 2 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/lisp/org-src.el b/lisp/org-src.el
index c9eef744e..d41879c09 100644
--- a/lisp/org-src.el
+++ b/lisp/org-src.el
@@ -360,8 +360,13 @@ where BEG and END are buffer positions and CONTENTS is a string."
       ((eq type 'inline-src-block)
        (let ((beg (progn (goto-char (org-element-property :begin datum))
 			 (search-forward "{" (line-end-position) t)))
-	     (end (progn (goto-char (org-element-property :end datum))
-			 (search-backward "}" (line-beginning-position) t))))
+	     (end (org-with-point-at (org-element-property :end datum)
+		    (skip-chars-backward " \t")
+		    (point))))
+	 (list beg end (buffer-substring-no-properties beg end))))
+      ((eq type 'latex-fragment)
+       (let ((beg (org-element-property :begin datum))
+	     (end (org-element-property :end datum)))
 	 (list beg end (buffer-substring-no-properties beg end))))
       ((org-element-property :contents-begin datum)
        (let ((beg (org-element-property :contents-begin datum))
@@ -959,6 +964,26 @@ Throw an error when not at such a table."
     (table-recognize)
     t))
 
+(defun org-edit-latex-fragment ()
+  "Edit LaTeX fragment at point."
+  (interactive)
+  (let ((context (org-element-context)))
+    (unless (and (eq (org-element-type context) 'latex-fragment)
+		 (org-src--on-datum-p context))
+      (user-error "Not on a LaTeX fragment"))
+    (org-src--edit-element
+     context
+     (org-src--construct-edit-buffer-name (buffer-name) "LaTeX fragment")
+     (org-src-get-lang-mode "latex")
+     (lambda () ; trim content
+       (goto-char (point-min))
+       (skip-chars-forward " \t")
+       (delete-region (point-min) (point))
+       (goto-char (point-max))
+       (skip-chars-backward " \t")
+       (delete-region (point) (point-max))))
+  t))
+
 (defun org-edit-latex-environment ()
   "Edit LaTeX environment at point.
 \\<org-src-mode-map>
diff --git a/lisp/org.el b/lisp/org.el
index dcd446745..2f5b89231 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -17340,6 +17340,7 @@ Otherwise, return a user error."
 	 (pcase (org-element-type context)
 	   (`footnote-reference (org-edit-footnote-reference))
 	   (`inline-src-block (org-edit-inline-src-code))
+	   (`latex-fragment (org-edit-latex-fragment))
 	   (`timestamp (if (eq 'inactive (org-element-property :type context))
 			   (call-interactively #'org-time-stamp-inactive)
 			 (call-interactively #'org-time-stamp)))
-- 
2.26.2

Reply via email to