With the new user option disabled (default), behavior is exactly as before:

- When you enter a preview, it is temporarily replaced by a
placeholder icon together with the tex code.
- If you exit the preview without editing, the preview reappears and
the tex code disappears.
- When you edit the tex code, the preview is gone forever.
- When you regenerate a preview, a construction sign briefly appears.

With the new user option disabled, behavior is instead:

- When you enter a preview, it remains visible.  The tex code appears
below the preview.
- When you edit the tex code, the preview does not disappear.  The tex
code persists if you then exit.
- When you regenerate a preview, there is no construction sign, but
instead a direct transition from the old image to the new one.

The advantage is that while editing a formula, you (and anyone else
watching your screen) can continue to look at its rendered form.  If
the user option "preview-protect-point" is active, then a quick "C-c
C-p C-p" or "C-c C-p C-e" will update the rendered form, without
flickering.

I would welcome suggestions for better names than
`preview-leave-open-previews-visible'.

Thanks, best,

Paul
From acd41e3a761b5140055e2ea8cf500a15f747bf76 Mon Sep 17 00:00:00 2001
From: Paul Nelson <[email protected]>
Date: Thu, 11 Apr 2024 23:09:28 +0200
Subject: [PATCH] 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.
---
 doc/preview-latex.texi |  4 ++++
 preview.el.in          | 47 ++++++++++++++++++++++++++++++++++--------
 2 files changed, 42 insertions(+), 9 deletions(-)

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..509359a8 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,17 @@ 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 ((ovr (cl-find-if
+                                    (lambda (ovr)
+                                      (and
+                                       (eq (overlay-start ovr) (overlay-start ov))
+                                       (overlay-get ovr 'preview-image)))
+                                    (overlays-at (overlay-start ov)))))
+                         (overlay-get ovr 'preview-image)
+                       default)
+                   default)))
   (preview-add-urgentization #'preview-gs-urgentize ov run-buffer)
   (list ov))
 
@@ -1895,6 +1912,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 +2212,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 +2237,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 +2456,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 +2598,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 +2615,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.
-- 
2.39.3 (Apple Git-145)

_______________________________________________
bug-auctex mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/bug-auctex

Reply via email to