From 21b4c8df1fa2d40958235b95026b212e72251275 Mon Sep 17 00:00:00 2001
From: Paul Nelson <ultrono@gmail.com>
Date: Thu, 11 Apr 2024 21:24:02 +0200
Subject: [PATCH] Make preview placement more flexible

* preview.el.in (preview-find-end-function): New variable.
(preview-parse-messages): Use it.
---
 preview.el.in | 36 +++++++++++++++++++++++++-----------
 1 file changed, 25 insertions(+), 11 deletions(-)

diff --git a/preview.el.in b/preview.el.in
index 7f3a5c29..906da3d3 100644
--- a/preview.el.in
+++ b/preview.el.in
@@ -3273,6 +3273,14 @@ Return a new string."
     (setq result (concat result string))
     result))
 
+(defvar preview-find-end-function nil
+  "Function used to compute the end position for a new overlay.
+The function bound to this variable will be called inside
+`preview-parse-messages' with one argument, an integer describing the
+beginning of the overlay.  This is intended to be used in conjunction
+with `preview-preprocess-function' when the latter introduces
+significant modifications.")
+
 (defun preview-parse-messages (open-closure)
   "Turn all preview snippets into overlays.
 This parses the pseudo error messages from the preview
@@ -3591,22 +3599,28 @@ name(\\([^)]+\\))\\)\\|\
                     (if box
                         (progn
                           (if (and lstart (= snippet lsnippet))
-                              (setq close-data
-                                    (nconc
-                                     (preview-place-preview
-                                      snippet
+                              (let* ((region-beg
                                       (save-excursion
                                         (preview-back-command
                                          (= (prog1 (point)
                                               (goto-char lstart))
                                             lstart))
-                                        (point))
-                                      (point)
-                                      (preview-TeX-bb box)
-                                      (cons lcounters counters)
-                                      tempdir
-                                      (cdr open-data))
-                                     close-data))
+                                        (point)))
+                                     (region-end
+                                      (if preview-find-end-function
+                                          (funcall preview-find-end-function
+                                                   region-beg)
+                                        (point)))
+                                     (ovl (preview-place-preview
+                                           snippet
+                                           region-beg
+                                           region-end
+                                           (preview-TeX-bb box)
+                                           (cons lcounters counters)
+                                           tempdir
+                                           (cdr open-data)))
+                                     (ov (car ovl)))
+                                (setq close-data (nconc ovl close-data))
                             (with-current-buffer run-buffer
                               (preview-log-error
                                (list 'error
-- 
2.39.3 (Apple Git-145)

