Andreas Leha <andreas.l...@med.uni-goettingen.de> writes:

> I am talking about `org-toggle-latex-fragment'.  And even if that is
> fast, it is very annoying behaviour.  I'd very much like to be able to
> toggle individual images.  In a math-heavy document the redisplay of all
> formula images of even the current section takes noticable moment.  But
> much worse: if I am working on a formula, I usually like to see other
> formulas as images.  But they disappear as soon as I switch off the
> image of the current formula to debug it.
>
> It is quite hard to even achieve the state where all formulas except the
> formula under the point are displayed as image.
>
> And I'd expect `org-toggle-latex-fragment' to do what its name suggests:
> to toggle the latex fragment, i.e. to produce the same state after the
> second invocation.  Similarly to visibility cycling only a prefix should
> act globally, IMO.

Fair enough.

Would you mind testing the following patch, then? It makes
`org-toggle-latex-fragment' behave more to your liking.


Regards,
>From 4fecb645b6c03118ba46d508ceb9159018a5d6f4 Mon Sep 17 00:00:00 2001
From: Nicolas Goaziou <m...@nicolasgoaziou.fr>
Date: Sat, 28 Feb 2015 00:30:43 +0100
Subject: [PATCH] Change `org-toggle-latex-fragment' behaviour

* lisp/org.el (org-remove-latex-fragment-image-overlays): Allow to
  limit overlay removal through optional arguments.  Define a new
  return value.
(org-toggle-latex-fragment): Change behaviour.  Update docstring
accordingly.

The new behaviour is the following:

  - With a double prefix argument, toggle overlays buffer wide;

  - With a single prefix overlay, or if there is no latex fragment at
    point, toggle overlays in the current section;

  - Otherwise, toggle overlay at point.

Suggested-by: <kuangd...@163.com>
<http://permalink.gmane.org/gmane.emacs.orgmode/95492>
---
 lisp/org.el | 133 +++++++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 82 insertions(+), 51 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index d05a7b8..cd5c5be 100755
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -18748,65 +18748,96 @@ looks only before point, not after."
   "List of overlays carrying the images of latex fragments.")
 (make-variable-buffer-local 'org-latex-fragment-image-overlays)
 
-(defun org-remove-latex-fragment-image-overlays ()
-  "Remove all overlays with LaTeX fragment images in current buffer."
-  (mapc 'delete-overlay org-latex-fragment-image-overlays)
-  (setq org-latex-fragment-image-overlays nil))
+(defun org-remove-latex-fragment-image-overlays (&optional beg end)
+  "Remove all overlays with LaTeX fragment images in current buffer.
+When optional arguments BEG and END are non-nil, remove all
+overlays between them instead.  Return t when some overlays were
+removed, nil otherwise."
+  (let (removedp)
+    (setq org-latex-fragment-image-overlays
+	  (let ((beg (or beg (point-min)))
+		(end (or end (point-max))))
+	    (org-remove-if
+	     (lambda (o)
+	       (let ((s (overlay-start o))
+		     (e (overlay-end o)))
+		 (cond
+		  ((= s e)
+		   (delete-overlay o) t)
+		  ((and (>= s beg) (<= e end))
+		   (delete-overlay o)
+		   (or removedp (setq removedp t))))))
+	     org-latex-fragment-image-overlays)))
+    removedp))
 
 (define-obsolete-function-alias
   'org-preview-latex-fragment 'org-toggle-latex-fragment "24.4")
-(defun org-toggle-latex-fragment (&optional subtree)
+(defun org-toggle-latex-fragment (&optional arg)
   "Preview the LaTeX fragment at point, or all locally or globally.
+
 If the cursor is in a LaTeX fragment, create the image and overlay
-it over the source code.  If there is no fragment at point, display
-all fragments in the current text, from one headline to the next.  With
-prefix SUBTREE, display all fragments in the current subtree.  With a
-double prefix arg \\[universal-argument] \\[universal-argument], or when \
-the cursor is before the first headline,
-display all fragments in the buffer.
-The images can be removed again with \\[org-toggle-latex-fragment]."
+it over the source code, if there is none, or 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 section.  With a double prefix ARG \\[universal-argument] \
+\\[universal-argument] preview or clear
+images for all fragments in the buffer."
   (interactive "P")
   (unless (buffer-file-name (buffer-base-buffer))
     (user-error "Can't preview LaTeX fragment in a non-file buffer"))
-  (if org-latex-fragment-image-overlays
-      (progn (org-remove-latex-fragment-image-overlays)
-	     (message "LaTeX fragments images removed"))
-    (when (display-graphic-p)
-      (org-remove-latex-fragment-image-overlays)
-      (org-with-wide-buffer
-       (let (beg end msg)
-	 (cond
-	  ((equal subtree '(16))
-	   (setq beg (point-min) end (point-max)
-		 msg "Creating images for buffer...%s"))
-	  ((equal subtree '(4))
-	   (org-back-to-heading)
-	   (setq beg (point) end (org-end-of-subtree t)
-		 msg "Creating images for subtree...%s"))
-	  ((let ((context (org-element-context)))
-	     (when (memq (org-element-type context)
-			 '(latex-environment latex-fragment))
-	       (setq beg (org-element-property :begin context)
-		     end (org-element-property :end context)
-		     msg "Creating image...%s"))))
-	  ((org-before-first-heading-p)
-	   (setq beg (point-min) end (point-max)
-		 msg "Creating images for buffer...%s"))
-	  (t
-	   (org-back-to-heading)
-	   (setq beg (point) end (progn (outline-next-heading) (point))
-		 msg "Creating images for entry...%s")))
-	 (message msg "")
-	 (narrow-to-region beg end)
-	 (goto-char beg)
-	 (org-format-latex
-	  (concat org-latex-preview-ltxpng-directory
-		  (file-name-sans-extension
-		   (file-name-nondirectory
-		    (buffer-file-name (buffer-base-buffer)))))
-	  default-directory 'overlays msg 'forbuffer
-	  org-latex-create-formula-image-program)
-	 (message msg "done.  Use `C-c C-x C-l' to remove images."))))))
+  (when (display-graphic-p)
+    (catch 'exit
+      (save-excursion
+	(let ((window-start (window-start)) msg)
+	  (save-restriction
+	    (let (datum)
+	      (cond
+	       ((equal arg '(16))
+		(if (org-remove-latex-fragment-image-overlays)
+		    (progn (message "LaTeX fragments images removed in buffer")
+			   (throw 'exit nil))
+		  (setq msg "Creating images for buffer...")
+		  (goto-char (point-min))))
+	       ((or (equal arg '(4))
+		    (not (memq
+			  (org-element-type (setq datum (org-element-context)))
+			  '(latex-environment latex-fragment))))
+		(let ((beg
+		       (save-excursion
+			 (org-with-limited-levels (outline-previous-heading))
+			 (point)))
+		      (end (save-excursion
+			     (org-with-limited-levels (outline-next-heading))
+			     (point))))
+		  (if (org-remove-latex-fragment-image-overlays beg end)
+		      (progn
+			(message "LaTeX fragment images removed in section")
+			(throw 'exit nil))
+		    (setq msg "Creating images for section...")
+		    (narrow-to-region beg end)
+		    (goto-char beg))))
+	       (t
+		(let* ((latex (or datum (org-element-context)))
+		       (beg (org-element-property :begin latex))
+		       (end (org-element-property :end latex)))
+		  (if (org-remove-latex-fragment-image-overlays beg end)
+		      (progn (message "LaTeX fragment image removed")
+			     (throw 'exit nil))
+		    (setq msg "Creating image...")
+		    (narrow-to-region beg end)))))
+	      (org-format-latex
+	       (concat org-latex-preview-ltxpng-directory
+		       (file-name-sans-extension
+			(file-name-nondirectory
+			 (buffer-file-name (buffer-base-buffer)))))
+	       default-directory 'overlays msg 'forbuffer
+	       org-latex-create-formula-image-program)))
+	  ;; Work around a bug that doesn't restore window's start when
+	  ;; widening back the buffer.
+	  (set-window-start nil window-start)
+	  (message (concat msg "done")))))))
 
 (defun org-format-latex
     (prefix &optional dir overlays msg forbuffer processing-type)
-- 
2.3.1

Reply via email to