Subject: Re: bug#48144: 13.0.11; TeX-run-format signals error in sentinel

From: Ikumi Keita <[email-masked]>
Date: Thu, 06 May 2021 20:33:10 +0900

Hi Basil, thanks for your report.

>>>>> "Basil L. Contovounesios" <[email-masked]> writes:
>>> AFAICT, this is because TeX-run-format uses TeX-TeX-sentinel, which
>>> calls TeX-TeX-sentinel-check, which expects TeX-error-report-switches to
>>> have been initialised by TeX-run-TeX, but we're not using TeX-run-TeX!

I agree with your analysis. I made a tentative fix as attachment, which
moves initialization of `TeX-error-report-switches' into
`TeX-format-run'. Could you test whether it works on your side?

> The bug report is about the inconsistency between TeX-run-format and its
> sentinel TeX-TeX-sentinel.  In some cases the latter expects
> TeX-error-report-switches to be initialised, but TeX-run-format never
> does this.  IMO if TeX-TeX-sentinel is going to be used with
> TeX-run-format then it should not assume that its process was started by
> TeX-run-TeX.  Unless I'm missing something?

I think this is a AUCTeX bug. `TeX-run-format' is a valid choice for
"How" item of `TeX-command-list' according to its defcustom. However,
current implementation of `TeX-run-format' doesn't achieve its job as
your analysis shows.

>>> I'm not sure whether the TeX-error-report-switches initialisation should
>>> be moved to TeX-run-format, or TeX-TeX-sentinel-check should guard
>>> against an uninitialised TeX-error-report-switches.

Actually, it isn't clear, at least for me, which is the proper fix. :-)
It's very difficult to determine what the proper behavior of
`TeX-run-format' and `TeX-run-TeX' should be, from the relevant doc
stirngs:
,----
| (defun TeX-run-format (name command file)
|   "Create a process for NAME using COMMAND to format FILE with TeX."
`----
,----
| (defun TeX-run-TeX (name command file)
|   "Create a process for NAME using COMMAND to format FILE with TeX."
`----
,----
| (defcustom TeX-command-list
|   [...]
| TeX-run-format: As `TeX-run-command', but assume the output is created
| by a TeX macro package.  Return the process object.
| 
| TeX-run-TeX: For TeX output.
`----
Actually (again), not all the TeX-run-* series are maintained well. The
ones which are seldom used sometimes have nonsense implementation:
https://lists.gnu.org/archive/html/auctex-devel/2017-05/msg00014.html

Regards,
Ikumi Keita

diff --git a/tex-buf.el b/tex-buf.el
index 114ceac4..4dd200e3 100644
--- a/tex-buf.el
+++ b/tex-buf.el
@@ -1124,42 +1124,28 @@ Return the new process."
                 lst nil)
         (setq lst (cdr lst))))))
 
-(defun TeX-run-format (name command file)
-  "Create a process for NAME using COMMAND to format FILE with TeX."
-  (TeX-run-set-command name command)
-  (let ((buffer (TeX-process-buffer-name file))
-        (process (TeX-run-command name command file)))
-    ;; Hook to TeX debugger.
-    (with-current-buffer buffer
-      (TeX-parse-reset)
-      (setq TeX-parse-function #'TeX-parse-TeX)
-      (setq TeX-sentinel-function #'TeX-TeX-sentinel)
-      (if TeX-process-asynchronous
-          (progn
-            ;; Updating the mode line.
-            (setq TeX-current-page "[0]")
-            (TeX-format-mode-line process)
-            (set-process-filter process #'TeX-format-filter)))
-      process)))
-
 (defvar TeX-error-report-switches nil
   "Reports presence of errors after `TeX-run-TeX'.
+Actually, `TeX-run-format' sets it.
 To test whether the current buffer has a compile error from last
-run of `TeX-run-TeX', use
+run of `TeX-run-format', use
   (TeX-error-report-has-errors-p)")
 
 (defun TeX-error-report-has-errors-p ()
   "Return non-nil if current buffer has compile errors from last TeX run."
   (plist-get TeX-error-report-switches (intern (TeX-master-file))))
 
-(defun TeX-run-TeX (name command file)
+(defun TeX-run-format (name command file)
   "Create a process for NAME using COMMAND to format FILE with TeX."
+  (TeX-run-set-command name command)
+  (let ((buffer (TeX-process-buffer-name file))
+        (process (TeX-run-command name command file))
+        (current-master (TeX-master-file)))
+
+    ;; Save information in TeX-error-report-switches
+    ;; Initialize error to nil (no error) for current master.
+    ;; Presence of error is reported inside `TeX-TeX-sentinel-check'
 
-  ;; Save information in TeX-error-report-switches
-  ;; Initialize error to nil (no error) for current master.
-  ;; Presence of error is reported inside `TeX-TeX-sentinel-check'
-  (let ((current-master (TeX-master-file))
-        (idx-file nil) (element nil))
     ;; the current master file is saved because error routines are
     ;; parsed in other buffers;
     (setq TeX-error-report-switches
@@ -1170,6 +1156,23 @@ run of `TeX-run-TeX', use
           (plist-put TeX-error-report-switches
                      (intern current-master) nil))
 
+    ;; Hook to TeX debugger.
+    (with-current-buffer buffer
+      (TeX-parse-reset)
+      (setq TeX-parse-function #'TeX-parse-TeX)
+      (setq TeX-sentinel-function #'TeX-TeX-sentinel)
+      (if TeX-process-asynchronous
+          (progn
+            ;; Updating the mode line.
+            (setq TeX-current-page "[0]")
+            (TeX-format-mode-line process)
+            (set-process-filter process #'TeX-format-filter)))
+      process)))
+
+(defun TeX-run-TeX (name command file)
+  "Create a process for NAME using COMMAND to format FILE with TeX."
+
+  (let ((idx-file nil) (element nil))
     ;; Store md5 hash of the index file before running LaTeX.
     (and (memq major-mode '(doctex-mode latex-mode))
          (prog1 (file-exists-p