Subject: 14.1.0; [PATCH] Save ini preview files inside TeX-output-dir

From: Al Haji-Ali <[email-masked]>
Date: Fri, 19 Sep 2025 20:00:01 +0200

[Re-sending to correct mailing-list]

Hello,

This is another follow-up bug-report/patch from:
https://lists.gnu.org/archive/html/auctex-devel/2025-08/msg00026.html

When caching the preamble while previewing files, an ini file is
created..  The attached places this ini file inside TeX-output-dir when
that is non-nil.  A change to the format of the variable
`preview-dumped-alist` was required to save the name of the output file.

Best regards,
-- Al

>From 61d952df797957dc66e2f065a24bd691a832b855 Mon Sep 17 00:00:00 2001
From: Al Haji-Ali <[email-masked]>
Date: Wed, 3 Sep 2025 21:56:08 +0100
Subject: [PATCH] Save ini preview files inside TeX-output-dir

* preview.el (preview-dumped-alist): Change format to include output
file.
(preview-watch-preamble): Accept output file as argument to save in
format-cons.
(preview-unwatch-preamble, preview-mode-setup): Accommodate changes of format-cons above.
(preview-format-kill): Delete output file.
(preview-cache-preamble): Use TeX-master-output-file instead of
TeX-master-output for ini file.
---
 preview.el | 71 ++++++++++++++++++++++++++++++------------------------
 1 file changed, 39 insertions(+), 32 deletions(-)

diff --git a/preview.el b/preview.el
index bb0f8ee9..e86b0035 100644
--- a/preview.el
+++ b/preview.el
@@ -1898,7 +1898,7 @@ definition of OV, AFTER-CHANGE, BEG, END and LENGTH."
     (preview-format-kill format-cons)
     (setcdr format-cons t)))

-(defun preview-watch-preamble (file command format-cons)
+(defun preview-watch-preamble (file out-file command format-cons)
   "Set up a watch on master file FILE.
 FILE can be an associated buffer instead of a filename.
 COMMAND is the command that generated the format.
@@ -1906,32 +1906,35 @@ FORMAT-CONS contains the format info for the main
 format dump handler."
   (let ((buffer (if (bufferp file)
                     file
-                  (find-buffer-visiting file))) ov)
+                  (find-buffer-visiting file)))
+        ov)
     (setcdr
      format-cons
-     (cons command
-           (when buffer
-             (with-current-buffer buffer
-               (save-excursion
-                 (save-restriction
-                   (widen)
-                   (goto-char (point-min))
-                   (unless (re-search-forward preview-dump-threshold nil t)
-                     (error "Can't find preamble of `%s'" file))
-                   (setq ov (make-overlay (point-min) (point)))
-                   (overlay-put ov 'format-cons format-cons)
-                   (overlay-put ov 'insert-in-front-hooks
-                                '(preview-preamble-changed-function))
-                   (overlay-put ov 'modification-hooks
-                                '(preview-preamble-changed-function))
-                   ov))))))))
+     (cl-list*
+      out-file
+      command
+      (when buffer
+        (with-current-buffer buffer
+          (save-excursion
+            (save-restriction
+              (widen)
+              (goto-char (point-min))
+              (unless (re-search-forward preview-dump-threshold nil t)
+                (error "Can't find preamble of `%s'" file))
+              (setq ov (make-overlay (point-min) (point)))
+              (overlay-put ov 'format-cons format-cons)
+              (overlay-put ov 'insert-in-front-hooks
+                           '(preview-preamble-changed-function))
+              (overlay-put ov 'modification-hooks
+                           '(preview-preamble-changed-function))
+              ov))))))))

 (defun preview-unwatch-preamble (format-cons)
   "Stop watching a format on FORMAT-CONS.
 The watch has been set up by `preview-watch-preamble'."
   (when (consp (cdr format-cons))
-    (when (cddr format-cons)
-      (delete-overlay (cddr format-cons)))
+    (when (cdddr format-cons)
+      (delete-overlay (cdddr format-cons)))
     (setcdr (cdr format-cons) nil)))

 (defun preview-register-change (ov)
@@ -2498,10 +2501,10 @@ The elements are (NAME . ASSOC).  NAME is the master file name
 format.  Possible values: nil means no format is available
 and none should be generated.  t means no format is available,
 it should be generated on demand.  If the value is a cons cell,
-the CAR of the cons cell is the command with which the format
-has been generated, and the CDR is some Emacs-flavor specific
-value used for maintaining a watch on possible changes of the
-preamble.")
+the CAR of the cons cell is the name of output master file, the CADR of
+the cons cell is the command with which the format has been generated,
+and the CDDR is some Emacs-flavor specific value used for maintaining a
+watch on possible changes of the preamble.")

 (defun preview-cleanout-tempfiles ()
   "Clean out all directories and files with non-persistent data.
@@ -3157,6 +3160,7 @@ pp")
         (preview-unwatch-preamble format-cons)
         (preview-watch-preamble (current-buffer)
                                 (cadr format-cons)
+                                (caddr format-cons)
                                 format-cons)))))

 ;;;###autoload
@@ -3996,10 +4000,11 @@ Those are just needed for cleanup."
   "Kill a cached format.
 FORMAT-CONS is intended to be an element of `preview-dumped-alist'.
 Tries through `preview-format-extensions'."
-  (dolist (ext preview-format-extensions)
-    (condition-case nil
-        (delete-file (preview-dump-file-name (concat (car format-cons) ext)))
-      (file-error nil))))
+  (when (consp (cdr format-cons))
+    (dolist (ext preview-format-extensions)
+      (condition-case nil
+          (delete-file (preview-dump-file-name (concat (cadr format-cons) ext)))
+        (file-error nil)))))

 (defun preview-dump-file-name (file)
   "Make a file name suitable for dumping from FILE."
@@ -4101,20 +4106,21 @@ If FORMAT-CONS is non-nil, a previous format may get reused."
   (interactive)
   (setq TeX-current-process-region-p nil)
   (let* ((dump-file
-          (expand-file-name (preview-dump-file-name (TeX-master-file "ini"))))
+          (expand-file-name (preview-dump-file-name (TeX-master-output-file "ini"))))
          (master (TeX-master-file))
          (format-name (expand-file-name master))
          (preview-format-name (shell-quote-argument
-                               (preview-dump-file-name (file-name-nondirectory
-                                                        master))))
+                               (preview-dump-file-name
+                                (TeX-master-output-file nil))))
          (master-file (expand-file-name (TeX-master-file t)))
+         (master-output-file (expand-file-name (TeX-master-output-file nil)))
          (command (preview-do-replacements
                    (TeX-command-expand
                     (preview-string-expand preview-LaTeX-command))
                    preview-dump-replacements))
          (preview-auto-cache-preamble nil))
     (unless (and (consp (cdr format-cons))
-                 (string= command (cadr format-cons)))
+                 (string= command (caddr format-cons)))
       (unless format-cons
         (setq format-cons (assoc format-name preview-dumped-alist)))
       (if format-cons
@@ -4161,6 +4167,7 @@ If FORMAT-CONS is non-nil, a previous format may get reused."
                                (zerop (process-exit-status process)))
                           (preview-watch-preamble
                            master-file
+                           master-output-file
                            command
                            format-cons)
                         (preview-format-kill format-cons))
--
2.39.5 (Apple Git-154)