Nicolas, here is a patch implementing alternative B above with
ORG-NEWS entry and everything.

I have been playing with it and find the bindings quite handy.

Code is indeed a bit simpler.

If you like it, feel free to amend it before merging.

Best regards
--
Carlos
From 799ecd332e81a31b06f69ba5546db74eb9583ba7 Mon Sep 17 00:00:00 2001
From: memeplex <carlosjosep...@gmail.com>
Date: Wed, 13 Feb 2019 16:26:46 -0300
Subject: [PATCH] org: Make latex preview toggle more useful and predictable

* lisp/org.el (org-toggle-latex-fragment):
  - Avoid toggling behavior for subtree/buffer scope
  - Make common use cases simpler to type

* Detailed discussion:

  http://lists.gnu.org/archive/html/emacs-orgmode/2019-02/msg00138.html
---
 lisp/org.el | 71 ++++++++++++++++++++++++++---------------------------
 1 file changed, 35 insertions(+), 36 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index 003058434..afd3f8709 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -18057,60 +18057,59 @@ overlays were removed, nil otherwise."
     overlays))
 
 (defun org-toggle-latex-fragment (&optional arg)
-  "Preview the LaTeX fragment at point, or all locally or globally.
-
-If the cursor is on a LaTeX fragment, create the image and overlay
-it over the source code, if there is none.  Remove it otherwise.
-If there is no fragment at point, display all fragments in the
-current section.
-
-With prefix ARG, preview or clear image for all fragments in the
-current subtree or in the whole buffer when used before the first
-headline.  With a prefix ARG `\\[universal-argument] \
-\\[universal-argument]' preview or clear images
-for all fragments in the buffer."
+  "Toggle preview of the LaTeX fragment at point.
+
+If the cursor is on a LaTeX fragment, create the image and
+overlay it over the source code, if there is none.  Remove it
+otherwise.
+
+If there is no fragment at point, display image for all fragments
+in the current section.
+
+With prefix ARG, clear image for all fragments in the current
+subtree.
+
+With double prefix ARG, display image for all fragments in the
+buffer.
+
+With triple prefix ARG, clear image for all fragments in the
+buffer."
   (interactive "P")
   (when (display-graphic-p)
     (catch 'exit
       (save-excursion
 	(let (beg end msg)
 	  (cond
-	   ((or (equal arg '(16))
-		(and (equal arg '(4))
-		     (org-with-limited-levels (org-before-first-heading-p))))
-	    (if (org-remove-latex-fragment-image-overlays)
-		(progn (message "LaTeX fragments images removed from buffer")
-		       (throw 'exit nil))
-	      (setq msg "Creating images for buffer...")))
-	   ((equal arg '(4))
+	   ((member arg '((16) (64)))	; Double or triple prefix
+	    (when (org-remove-latex-fragment-image-overlays)
+	      (message "LaTeX fragments images removed from buffer"))
+	    (when (equal arg '(64)) (throw 'exit nil))
+	    (setq msg "Creating images for buffer..."))
+	   ((member arg '((4)))		; Single prefix
 	    (org-with-limited-levels (org-back-to-heading t))
 	    (setq beg (point))
 	    (setq end (progn (org-end-of-subtree t) (point)))
-	    (if (org-remove-latex-fragment-image-overlays beg end)
-		(progn
-		  (message "LaTeX fragment images removed from subtree")
-		  (throw 'exit nil))
-	      (setq msg "Creating images for subtree...")))
+	    (when (org-remove-latex-fragment-image-overlays beg end)
+	      (message "LaTeX fragment images removed from subtree"))
+	    (throw 'exit nil))
 	   ((let ((datum (org-element-context)))
 	      (when (memq (org-element-type datum)
 			  '(latex-environment latex-fragment))
 		(setq beg (org-element-property :begin datum))
 		(setq end (org-element-property :end datum))
-		(if (org-remove-latex-fragment-image-overlays beg end)
-		    (progn (message "LaTeX fragment image removed")
-			   (throw 'exit nil))
-		  (setq msg "Creating image...")))))
+		(when (org-remove-latex-fragment-image-overlays beg end)
+		  (message "LaTeX fragment image removed")
+		  (throw 'exit nil))
+		(setq msg "Creating image..."))))
 	   (t
 	    (org-with-limited-levels
 	     (setq beg (if (org-at-heading-p) (line-beginning-position)
 			 (outline-previous-heading)
-			 (point)))
-	     (setq end (progn (outline-next-heading) (point)))
-	     (if (org-remove-latex-fragment-image-overlays beg end)
-		 (progn
-		   (message "LaTeX fragment images removed from section")
-		   (throw 'exit nil))
-	       (setq msg "Creating images for section...")))))
+			 (point))
+		   end (progn (outline-next-heading) (point)))
+	     (when (org-remove-latex-fragment-image-overlays beg end)
+	       (message "LaTeX fragment images removed from section"))
+	       (setq msg "Creating images for section..."))))
 	  (let ((file (buffer-file-name (buffer-base-buffer))))
 	    (org-format-latex
 	     (concat org-preview-latex-image-directory "org-ltximg")
-- 
2.20.1

Reply via email to