branch: master
commit 4731168eca3f103732a026f70912597faef8bd99
Author: Paul Nelson <ultr...@gmail.com>
Commit: Arash Esbati <ar...@gnu.org>

    Allow opened previews to remain visible
    
    * preview.el.in (preview-leave-open-previews-visible): New user
    option.
    (preview-gs-place, preview-disable, preview-inactive-string)
    (preview-place-preview, preview-check-changes): Use it.
    (preview-clearout): New optional argument, EXCEPTION.
    * doc/preview-latex.texi (The Emacs interface): Document the
    new user option.
    * NEWS.org (Added): Announce the new custom option.  (bug#70455)
---
 NEWS.org               |  6 ++++--
 doc/preview-latex.texi |  4 ++++
 preview.el.in          | 51 +++++++++++++++++++++++++++++++++++++++++---------
 3 files changed, 50 insertions(+), 11 deletions(-)

diff --git a/NEWS.org b/NEWS.org
index b0c8676a..21b98e30 100644
--- a/NEWS.org
+++ b/NEWS.org
@@ -21,11 +21,13 @@
 - Add new custom option ~TeX-fold-auto-reveal~ which contols how the
   original source text is revealed when user clicks the folded portion
   by mouse.
+- Add new custom option ~TeX-fold-region-functions~ which is a list of
+  additional functions to call when folding a region.
 - Add new custom option ~preview-protect-point~ which determines whether
   previews generated on top of the current point should be temporarily
   opened (~nil~ by default).
-- Add new custom option ~TeX-fold-region-functions~ which is a list of
-  additional functions to call when folding a region.
+- Add new custom option ~preview-leave-open-previews-visible~ which
+  determines if the preview code stays visible once opened.
 - Support query and insert of ~mcite~ compatibility macro
   (=style/biblatex.el=).
 - Support the =\verbatiminput*= macro (=style/verbatim.el=).
diff --git a/doc/preview-latex.texi b/doc/preview-latex.texi
index 07adc2a9..0c1fb730 100644
--- a/doc/preview-latex.texi
+++ b/doc/preview-latex.texi
@@ -672,6 +672,10 @@ accordingly @code{split} is one entry in
 This boolean variable determines whether previews generated on top of the
 current point should be temporarily opened.  Default value is @code{nil}.
 
+@item preview-leave-open-previews-visible
+This boolean variable determines whether to leave preview images visible
+(above their generating TeX code) when they are opened.
+
 @end vtable
 
 @node The preview images, Misplaced previews, The Emacs interface, For 
advanced users
diff --git a/preview.el.in b/preview.el.in
index 5e6b556c..1f65f728 100644
--- a/preview.el.in
+++ b/preview.el.in
@@ -1210,6 +1210,13 @@ is located."
     (setcdr (car img) (cdar replacement))
     (setcdr img (cdr replacement))))
 
+(defcustom preview-leave-open-previews-visible nil
+  "Whether to leave previews visible when they are opened.
+If nil, then the TeX preview icon is used when the preview is opened.
+If non-nil, then the preview image is moved above the text."
+  :group 'preview-appearance
+  :type 'boolean)
+
 (defun preview-gs-place (ov snippet box run-buffer tempdir ps-file _imagetype)
   "Generate an image placeholder rendered over by Ghostscript.
 This enters OV into all proper queues in order to make it render
@@ -1231,7 +1238,21 @@ for the file extension."
   (overlay-put ov 'queued
                (vector box nil snippet))
   (overlay-put ov 'preview-image
-               (list (preview-icon-copy preview-nonready-icon)))
+               (let ((default (list (preview-icon-copy 
preview-nonready-icon))))
+                 (if preview-leave-open-previews-visible
+                     (if-let ((img
+                               (car
+                                (delq
+                                 nil
+                                 (mapcar
+                                  (lambda (ovr)
+                                    (and
+                                     (eq (overlay-start ovr) (overlay-start 
ov))
+                                     (overlay-get ovr 'preview-image)))
+                                  (overlays-at (overlay-start ov)))))))
+                         img
+                       default)
+                   default)))
   (preview-add-urgentization #'preview-gs-urgentize ov run-buffer)
   (list ov))
 
@@ -1895,6 +1916,11 @@ Disable it if that is the case.  Ignores text 
properties."
                        text (overlay-get ov 'preview-prechange)))
                 (overlay-put ov 'insert-in-front-hooks nil)
                 (overlay-put ov 'insert-behind-hooks nil)
+                (when (and preview-leave-open-previews-visible
+                           (eq (overlay-get ov 'preview-state) 'active))
+                  ;; This is so that remote commands, such as `undo',
+                  ;; open active previews before disabling them.
+                  (preview-toggle ov))
                 (preview-disable ov)))))
       (error nil))
     (overlay-put ov 'preview-prechange nil))
@@ -2190,10 +2216,12 @@ active (`transient-mark-mode'), it is run through 
`preview-region'."
   "Change overlay behaviour of OVR after source edits."
   (overlay-put ovr 'queued nil)
   (preview-remove-urgentization ovr)
-  (overlay-put ovr 'preview-image nil)
+  (unless preview-leave-open-previews-visible
+    (overlay-put ovr 'preview-image nil))
   (overlay-put ovr 'timestamp nil)
   (setcdr (overlay-get ovr 'strings) (preview-disabled-string ovr))
-  (preview-toggle ovr)
+  (unless preview-leave-open-previews-visible
+    (preview-toggle ovr))
   (overlay-put ovr 'preview-state 'disabled)
   (dolist (filename (overlay-get ovr 'filenames))
     (condition-case nil
@@ -2213,17 +2241,20 @@ a hook in some cases"
           (preview-delete-file filename)
         (file-error nil)))))
 
-(defun preview-clearout (&optional start end timestamp)
+(defun preview-clearout (&optional start end timestamp exception)
   "Clear out all previews in the current region.
 When called interactively, the current region is used.
 Non-interactively, the region between START and END is
 affected.  Those two values default to the borders of
 the entire buffer.  If TIMESTAMP is non-nil, previews
-with a `timestamp' property of it are kept."
+with a `timestamp' property of it are kept.  If EXCEPTION
+is a non-nil overlay, then it is not cleared."
   (interactive "r")
   (dolist (ov (overlays-in (or start (point-min))
                            (or end (point-max))))
-    (and (overlay-get ov 'preview-state)
+    (and (or (not exception)
+             (not (eq ov exception)))
+         (overlay-get ov 'preview-state)
          (not (and timestamp
                    (equal timestamp (overlay-get ov 'timestamp))))
          (preview-delete ov))))
@@ -2429,7 +2460,9 @@ visible.  For efficiency reasons it is expected that the 
buffer
 is already selected and unnarrowed."
   (concat
    (preview-make-clickable (overlay-get ov 'preview-map)
-                           preview-icon
+                           (if preview-leave-open-previews-visible
+                               (overlay-get ov 'preview-image)
+                             preview-icon)
                            "\
 %s redisplays preview
 %s more options")
@@ -2569,7 +2602,6 @@ a list with additional info from the placement hook.
 Those lists get concatenated together and get passed
 to the close hook."
   (setq preview-current-region nil)
-  (preview-clearout start end tempdir)
   (let ((ov (make-overlay start end nil nil nil)))
     (overlay-put ov 'priority (TeX-overlay-prioritize start end))
     (overlay-put ov 'preview-map
@@ -2587,7 +2619,8 @@ to the close hook."
                   place-opts)
       (overlay-put ov 'strings
                    (list (preview-active-string ov)))
-      (preview-toggle ov t))))
+      (preview-toggle ov t)
+      (preview-clearout start end tempdir ov))))
 
 (defun preview-counter-find (begin)
   "Fetch the next preceding or next preview-counters property.

Reply via email to