emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Nicolas Goaziou <mail@nicolasgoaziou.fr>
To: Andreas Leha <andreas.leha@med.uni-goettingen.de>
Cc: emacs-orgmode@gnu.org
Subject: Re: Here is a patch I want to add to org.el……
Date: Sat, 28 Feb 2015 00:37:31 +0100	[thread overview]
Message-ID: <877fv3rkn8.fsf@nicolasgoaziou.fr> (raw)
In-Reply-To: <olu4mq7pe81.fsf@med.uni-goettingen.de> (Andreas Leha's message of "Fri, 27 Feb 2015 15:26:54 +0000")

[-- Attachment #1: Type: text/plain, Size: 1052 bytes --]

Andreas Leha <andreas.leha@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,

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Change-org-toggle-latex-fragment-behaviour.patch --]
[-- Type: text/x-diff, Size: 7032 bytes --]

From 4fecb645b6c03118ba46d508ceb9159018a5d6f4 Mon Sep 17 00:00:00 2001
From: Nicolas Goaziou <mail@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: <kuangdash@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


  parent reply	other threads:[~2015-02-27 23:36 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-25  4:14 Here is a patch I want to add to org.el…… kuangdash
2015-02-25  4:41 ` kuangdash
2015-02-26 18:49   ` Nicolas Goaziou
2015-02-25 13:03 ` Nicolas Goaziou
2015-02-27  6:25   ` kuangdash
2015-02-27  9:29     ` Andreas Leha
2015-02-27  9:49       ` Nicolas Goaziou
2015-02-27 14:36         ` kuangdash
2015-02-27 17:32           ` Nicolas Goaziou
2015-02-27 19:38             ` kuangdash
2015-02-27 21:19               ` Nick Dokos
     [not found]               ` <54F0D25E.AC5A60.07341@m50-132.163.com>
2015-02-28  9:03                 ` kuangdash
2015-02-28 15:43                   ` Nick Dokos
2015-02-28 16:26                     ` kuangdash
2015-02-27 15:26         ` Andreas Leha
2015-02-27 15:55           ` Rasmus
2015-02-27 22:22             ` Andreas Leha
2015-02-28  9:44               ` Rasmus
2015-02-27 23:37           ` Nicolas Goaziou [this message]
2015-02-28 11:01             ` Andreas Leha
2015-03-02 22:48               ` Andreas Leha
2015-03-03  0:21                 ` Nicolas Goaziou
2015-03-03  9:38                   ` Andreas Leha
2015-03-03  9:40                     ` Andreas Leha
2015-03-03 21:00                       ` Nicolas Goaziou
2015-03-03 21:11                         ` Andreas Leha
2015-03-06 11:08                           ` Nicolas Goaziou
2015-03-06 11:29                             ` Andreas Leha
2015-02-27  9:46     ` Nicolas Goaziou

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=877fv3rkn8.fsf@nicolasgoaziou.fr \
    --to=mail@nicolasgoaziou.fr \
    --cc=andreas.leha@med.uni-goettingen.de \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).