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

From: "Basil L. Contovounesios" <[email-masked]>
Date: Sat, 01 May 2021 17:15:26 +0100

I was experimenting with configuring latexmk as best I could in
TeX-command-list when I noticed an inconsistency between TeX-run-format
and its sentinel.  Here's a recipe to illustrate what I mean:

0. emacs -Q
1. M-x package-initialize RET
2. (progn
     (setq debug-on-error t)
     (add-hook 'LaTeX-mode-hook
               (lambda () (setq TeX-command-default "Latexmk")))
     (with-eval-after-load 'tex
       (push '("Latexmk"
               "latexmk%(file-line-error) %(extraopts)%(mode) %t"
               TeX-run-format nil (latex-mode))
             TeX-command-list)))
3. C-x C-e
4. C-x C-f /tmp/helloworld.tex RET
5. M-x auto-insert RET RET article RET RET RET
6. \helloworld
7. C-c C-c y RET

  Debugger entered--Lisp error: (wrong-type-argument stringp nil)
    intern(nil)
    TeX-TeX-sentinel-check(#<process Latexmk> "Latexmk")
    TeX-TeX-sentinel(#<process Latexmk> "Latexmk")
    TeX-command-sentinel(#<process Latexmk> "exited abnormally with code 12\n")

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'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.

Are my assumptions about or use of TeX-run-format wrong?  E.g., should I
just be using TeX-run-command for this purpose instead?

---

Some off-topic justification for how I ended up here:

I tried using a sentinel that is as close to TeX-run-TeX as possible,
whilst avoiding the false positive output parsing about cross-references
and the like in TeX-LaTeX-sentinel, which latexmk already handles.
TeX-run-format seems to fit a nice middle-ground between TeX-run-TeX and
TeX-run-command.

I also tried TeX-command-run-all instead of latexmk, but AFAICT the
former doesn't handle as many scenarios as the latter, such as running
Biber as needed.

Since neither TeX-run-format nor TeX-run-command runs
TeX-after-compilation-finished-functions, I've ended up with the
following custom sentinel for latexmk:

  (defun blc-latexmk-sentinel (proc _msg)
    "Run `TeX-after-compilation-finished-functions' on PROC success."
    (when (and (eq (process-status proc) 'exit)
               (zerop (process-exit-status proc)))
      (defvar TeX-command-buffer)
      (run-hook-with-args 'TeX-after-compilation-finished-functions
                          (with-current-buffer TeX-command-buffer
                            (expand-file-name
                             (TeX-active-master (TeX-output-extension)))))))

  (defun blc-TeX-run-latexmk (&rest args)
    "Like `TeX-run-format', but also run TeX compilation hooks."
    (let ((proc (apply #'TeX-run-format args)))
      (add-function :after (process-sentinel proc) #'blc-latexmk-sentinel)
      proc))

Please let me know if any of this runs afoul of AUCTeX's design, or if
there's a better way to integrate latexmk or call a hook on successful
compilation.  [I read through all of the relevant AUCTeX mailing list
discussions I could find, and I'm undecided about the auctex-latexmk
package on MELPA, which uses old-style function advice.]

Thanks,

-- 
Basil

Emacs  : GNU Emacs 28.0.50 (build 1, x86_64-pc-linux-gnu, X toolkit, cairo version 1.16.0, Xaw3d scroll bars)
 of 2021-05-01
Package: 13.0.11

current state:
==============
(setq
 AUCTeX-date "2021-04-28"
 window-system 'x
 LaTeX-version "2e"
 TeX-style-path '("~/.emacs.d/auctex"
		  "/home/blc/.emacs.d/elpa/auctex-13.0.11.0.20210428.195438/style"
		  "/home/blc/.emacs.d/auctex/auto" "/home/blc/.emacs.d/auctex/style" "auto" "style")
 TeX-auto-save nil
 TeX-parse-self nil
 TeX-master t
 TeX-command-list '(("Latexmk" "latexmk%(file-line-error) %(extraopts)%(mode) %t" TeX-run-format nil
		     (latex-mode))
		    ("TeX"
		     "%(PDF)%(tex) %(file-line-error) %`%(extraopts) %S%(PDFout)%(mode)%' %(output-dir) %t"
		     TeX-run-TeX nil (plain-tex-mode ams-tex-mode texinfo-mode) :help "Run plain TeX")
		    ("LaTeX" "%`%l%(mode)%' %T" TeX-run-TeX nil (latex-mode doctex-mode) :help
		     "Run LaTeX")
		    ("Makeinfo" "makeinfo %(extraopts) %(o-dir) %t" TeX-run-compile nil
		     (texinfo-mode) :help "Run Makeinfo with Info output")
		    ("Makeinfo HTML" "makeinfo %(extraopts) %(o-dir) --html %t" TeX-run-compile nil
		     (texinfo-mode) :help "Run Makeinfo with HTML output")
		    ("AmSTeX" "amstex %(PDFout) %`%(extraopts) %S%(mode)%' %(output-dir) %t"
		     TeX-run-TeX nil (ams-tex-mode) :help "Run AMSTeX")
		    ("ConTeXt" "%(cntxcom) --once --texutil %(extraopts) %(execopts)%t" TeX-run-TeX
		     nil (context-mode) :help "Run ConTeXt once")
		    ("ConTeXt Full" "%(cntxcom) %(extraopts) %(execopts)%t" TeX-run-TeX nil
		     (context-mode) :help "Run ConTeXt until completion")
		    ("BibTeX" "bibtex %(O?aux)" TeX-run-BibTeX nil
		     (plain-tex-mode latex-mode doctex-mode ams-tex-mode texinfo-mode context-mode)
		     :help "Run BibTeX")
		    ("Biber" "biber %s %(output-dir)" TeX-run-Biber nil
		     (plain-tex-mode latex-mode doctex-mode ams-tex-mode texinfo-mode) :help
		     "Run Biber")
		    ("View" "%V" TeX-run-discard-or-function t t :help "Run Viewer")
		    ("Print" "%p" TeX-run-command t t :help "Print the file")
		    ("Queue" "%q" TeX-run-background nil t :help "View the printer queue" :visible
		     TeX-queue-command)
		    ("File" "%(o?)dvips %d -o %f " TeX-run-dvips t
		     (plain-tex-mode latex-mode doctex-mode ams-tex-mode texinfo-mode) :help
		     "Generate PostScript file")
		    ("Dvips" "%(o?)dvips %d -o %f " TeX-run-dvips nil
		     (plain-tex-mode latex-mode doctex-mode ams-tex-mode texinfo-mode) :help
		     "Convert DVI file to PostScript")
		    ("Dvipdfmx" "dvipdfmx %d -o %(O?pdf)" TeX-run-dvipdfmx nil
		     (plain-tex-mode latex-mode doctex-mode ams-tex-mode texinfo-mode) :help
		     "Convert DVI file to PDF with dvipdfmx")
		    ("Ps2pdf" "ps2pdf %f %(O?pdf)" TeX-run-ps2pdf nil
		     (plain-tex-mode latex-mode doctex-mode ams-tex-mode texinfo-mode) :help
		     "Convert PostScript file to PDF")
		    ("Glossaries" "makeglossaries %(O?aux)" TeX-run-command nil
		     (plain-tex-mode latex-mode doctex-mode ams-tex-mode texinfo-mode) :help
		     "Run makeglossaries to create glossary\n     file")
		    ("Index" "makeindex %(O?idx)" TeX-run-index nil
		     (plain-tex-mode latex-mode doctex-mode ams-tex-mode texinfo-mode) :help
		     "Run makeindex to create index file")
		    ("upMendex" "upmendex %(O?idx)" TeX-run-index t
		     (plain-tex-mode latex-mode doctex-mode ams-tex-mode texinfo-mode) :help
		     "Run upmendex to create index file")
		    ("Xindy" "texindy %s" TeX-run-command nil
		     (plain-tex-mode latex-mode doctex-mode ams-tex-mode texinfo-mode) :help
		     "Run xindy to create index file")
		    ("Check" "lacheck %s" TeX-run-compile nil (latex-mode) :help
		     "Check LaTeX file for correctness")
		    ("ChkTeX" "chktex -v6 %s" TeX-run-compile nil (latex-mode) :help
		     "Check LaTeX file for common mistakes")
		    ("Spell" "(TeX-ispell-document \"\")" TeX-run-function nil t :help
		     "Spell-check the document")
		    ("Clean" "TeX-clean" TeX-run-function nil t :help
		     "Delete generated intermediate files")
		    ("Clean All" "(TeX-clean t)" TeX-run-function nil t :help
		     "Delete generated intermediate and output files")
		    ("Other" "" TeX-run-command t t :help "Run an arbitrary command"))
 )