From c45980461f738a6d48eb392b713a1e6e19739961 Mon Sep 17 00:00:00 2001
From: Paul Nelson <ultrono@gmail.com>
Date: Sun, 28 Apr 2024 17:10:11 +0200
Subject: [PATCH] Allow programmatic preview abortion

* preview.el.in (preview-abort-flag): New variable.
(preview-TeX-inline-sentinel): Use it.
---
 preview.el.in | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/preview.el.in b/preview.el.in
index c29e546e..f1262163 100644
--- a/preview.el.in
+++ b/preview.el.in
@@ -3893,6 +3893,12 @@ If FAST is set, do a fast conversion."
                     TeX-shell-command-option
                     command))))
 
+(defvar-local preview-abort-flag nil
+  "Cause ongoing preview generation to abort.
+If non-nil, then `preview-TeX-inline-sentinel' aborts and resets this
+variable to nil.  This is intended to give a way for external packages
+to abort preview generation, more reliably than via process signals.")
+
 (defun preview-TeX-inline-sentinel (process _name)
   "Sentinel function for preview.
 See `TeX-sentinel-function' and `set-process-sentinel'
@@ -3901,7 +3907,12 @@ for definition of PROCESS and NAME."
   (let ((status (process-status process)))
     (if (memq status '(signal exit))
         (delete-process process))
-    (when (eq status 'exit)
+    (cond
+     ((with-current-buffer TeX-command-buffer
+        (prog1
+            preview-abort-flag
+          (setq preview-abort-flag nil))))
+     ((eq status 'exit)
       (save-excursion
         (goto-char (point-max))
         (forward-line -1)
@@ -3912,7 +3923,7 @@ for definition of PROCESS and NAME."
       (condition-case err
           (preview-call-hook 'open)
         (error (preview-log-error err "LaTeX" process)))
-      (preview-reraise-error process))))
+      (preview-reraise-error process)))))
 
 (defcustom preview-format-extensions '(".fmt" ".efmt")
   "Possible extensions for format files.
-- 
2.39.3 (Apple Git-145)

