"Fossies" - the Fresh Open Source Software Archive

Member "auctex-12.3/tex-buf.el" (18 Oct 2020, 151071 Bytes) of package /linux/misc/auctex-12.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Lisp source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "tex-buf.el": 12.2_vs_12.3.

    1 ;;; tex-buf.el --- External commands for AUCTeX.
    2 
    3 ;; Copyright (C) 1991-1999, 2001-2020 Free Software Foundation, Inc.
    4 
    5 ;; Maintainer: auctex-devel@gnu.org
    6 ;; Keywords: tex, wp
    7 
    8 ;; This file is part of AUCTeX.
    9 
   10 ;; AUCTeX is free software; you can redistribute it and/or modify it
   11 ;; under the terms of the GNU General Public License as published by
   12 ;; the Free Software Foundation; either version 3, or (at your option)
   13 ;; any later version.
   14 
   15 ;; AUCTeX is distributed in the hope that it will be useful, but
   16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
   17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   18 ;; General Public License for more details.
   19 
   20 ;; You should have received a copy of the GNU General Public License
   21 ;; along with AUCTeX; see the file COPYING.  If not, write to the Free
   22 ;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   23 ;; 02110-1301, USA.
   24 
   25 ;;; Commentary:
   26 
   27 ;; This file provides support for external commands.
   28 
   29 ;;; Code:
   30 
   31 (require 'tex)
   32 (require 'latex)
   33 (require 'comint)
   34 
   35 ;;; Customization:
   36 
   37 (defcustom TeX-process-asynchronous (not (eq system-type 'ms-dos))
   38   "*Use asynchronous processes."
   39   :group 'TeX-command
   40   :type 'boolean)
   41 
   42 (defcustom TeX-shell
   43   (if (memq system-type '(ms-dos emx windows-nt))
   44       shell-file-name
   45     "/bin/sh")
   46   "Name of shell used to parse TeX commands."
   47   :group 'TeX-command
   48   :type 'file)
   49 
   50 (defcustom TeX-shell-command-option
   51   (cond ((memq system-type '(ms-dos emx windows-nt) )
   52      (cond ((boundp 'shell-command-option)
   53         shell-command-option)
   54            ((boundp 'shell-command-switch)
   55         shell-command-switch)
   56            (t
   57         "/c")))
   58     (t              ;Unix & EMX (Emacs 19 port to OS/2)
   59      "-c"))
   60   "Shell argument indicating that next argument is the command."
   61   :group 'TeX-command
   62   :type 'string)
   63 
   64 ;;; Interactive Commands
   65 ;;
   66 ;; The general idea is, that there is one process and process buffer
   67 ;; associated with each master file, and one process and process buffer
   68 ;; for running TeX on a region.   Thus, if you have N master files, you
   69 ;; can run N + 1 processes simultaneously.
   70 ;;
   71 ;; Some user commands operates on ``the'' process.  The following
   72 ;; algorithm determine what ``the'' process is.
   73 ;;
   74 ;; IF   last process started was on a region
   75 ;; THEN ``the'' process is the region process
   76 ;; ELSE ``the'' process is the master file (of the current buffer) process
   77 
   78 (defun TeX-save-document (name)
   79   "Save all files belonging to the current document.
   80 Return non-nil if document needs to be re-TeX'ed."
   81   (interactive (list (TeX-master-file)))
   82   (if (string-equal name "")
   83       (setq name (TeX-master-file)))
   84 
   85   (TeX-check-files (concat name "." (TeX-output-extension))
   86            (cons name (TeX-style-list))
   87            TeX-file-extensions))
   88 
   89 (defun TeX-command-master (&optional override-confirm)
   90   "Run command on the current document.
   91 
   92 If a prefix argument OVERRIDE-CONFIRM is given, confirmation will
   93 depend on it being positive instead of the entry in `TeX-command-list'."
   94   (interactive "P")
   95   (TeX-command (TeX-command-query (TeX-master-file nil nil t))
   96            'TeX-master-file override-confirm))
   97 
   98 (defvar TeX-command-region-begin nil)
   99 (defvar TeX-command-region-end nil)
  100 ;; Used for marking the last region.
  101 
  102 (make-variable-buffer-local 'TeX-command-region-begin)
  103 (make-variable-buffer-local 'TeX-command-region-end)
  104 
  105 (defun TeX-current-offset (&optional pos)
  106   "Calculate line offset of POS, or of point if POS is nil."
  107   (save-restriction
  108     (widen)
  109     (save-excursion
  110       (let ((inhibit-point-motion-hooks t)
  111         (inhibit-field-text-motion t))
  112     (if pos (goto-char pos))
  113     (+ (count-lines (point-min) (point))
  114        (if (bolp) 0 -1))))))
  115 
  116 (defun TeX-pin-region (begin end)
  117   "Pin the TeX region specified by BEGIN and END.
  118 If BEGIN is nil, the region is unpinned.
  119 
  120 In interactive use, a positive prefix arg will pin the region,
  121 a non-positive one will unpin it.  Without a prefix arg, if
  122 a region is actively marked, it will get pinned.  If not, a
  123 pinned region will get unpinned and vice versa."
  124   (interactive
  125    (if
  126        (if current-prefix-arg
  127        (> (prefix-numeric-value current-prefix-arg) 0)
  128      (or (TeX-active-mark)
  129          (null TeX-command-region-begin)))
  130        (list (region-beginning) (region-end))
  131      '(nil nil)))
  132   (if begin
  133       (progn
  134     (unless (markerp TeX-command-region-begin)
  135       (setq TeX-command-region-begin (make-marker))
  136       (setq TeX-command-region-end (make-marker)))
  137     (set-marker TeX-command-region-begin begin)
  138     (set-marker TeX-command-region-end end)
  139     (message "TeX region pinned."))
  140     (when (markerp TeX-command-region-begin)
  141       (set-marker TeX-command-region-begin nil)
  142       (set-marker TeX-command-region-end nil))
  143     (setq TeX-command-region-begin nil)
  144     (setq TeX-command-region-end nil)
  145     (message "TeX region unpinned.")))
  146 
  147 (defun TeX-region-update ()
  148   "Update the TeX-region file."
  149   ;; Note that TeX-command-region-begin is not a marker when called
  150   ;; from TeX-command-buffer.
  151   (and (or (null TeX-command-region-begin)
  152        (markerp TeX-command-region-begin))
  153        (TeX-active-mark)
  154        (TeX-pin-region (region-beginning) (region-end)))
  155   (let ((begin (or TeX-command-region-begin (region-beginning)))
  156     (end (or TeX-command-region-end (region-end))))
  157     (TeX-region-create (TeX-region-file TeX-default-extension)
  158                (buffer-substring begin end)
  159                (file-name-nondirectory (buffer-file-name))
  160                (TeX-current-offset begin))))
  161 
  162 (defun TeX-command-region (&optional override-confirm)
  163   "Run TeX on the current region.
  164 
  165 Query the user for a command to run on the temporary file specified by
  166 the variable `TeX-region'.  If there is an explicitly active region,
  167 it is stored for later commands.  If not, a previously stored region
  168 \(can be also be set with `TeX-pin-region') overrides the current region,
  169 if present.
  170 
  171 If a prefix argument OVERRIDE-CONFIRM is given, prompting will
  172 ignore the prompting flag from `TeX-command-list' and instead
  173 will prompt iff the prefix is positive.
  174 
  175 If the master file for the document has a header, it is written to the
  176 temporary file before the region itself.  The document's header is all
  177 text before `TeX-header-end'.
  178 
  179 If the master file for the document has a trailer, it is written to
  180 the temporary file after the region itself.  The document's trailer is
  181 all text after `TeX-trailer-start'."
  182   (interactive "P")
  183   (TeX-region-update)
  184   ;; In the next line, `TeX-region-file' should be called with nil
  185   ;; `nondirectory' argument, otherwise `TeX-comand-default' called
  186   ;; within `TeX-command-query' won't work in included files not
  187   ;; placed in `TeX-master-directory'.
  188   (TeX-command (TeX-command-query (TeX-region-file)) 'TeX-region-file
  189            override-confirm))
  190 
  191 (defun TeX-command-buffer (&optional override-confirm)
  192   "Run TeX on the current buffer.
  193 
  194 Query the user for a command to run on the temporary file specified by
  195 the variable `TeX-region'.  The region file will be recreated from the
  196 visible part of the buffer.
  197 
  198 If a prefix argument OVERRIDE-CONFIRM is given, confirmation will
  199 depend on it being positive instead of the entry in `TeX-command-list'."
  200   (interactive "P")
  201   (let ((TeX-command-region-begin (point-min))
  202     (TeX-command-region-end (point-max)))
  203     (TeX-command-region override-confirm)))
  204 
  205 (defcustom TeX-record-buffer nil
  206   "Whether to record buffer names of generated TeX buffers.
  207 When non-nil, these buffers are put at the front of the list of
  208 recently selected ones."
  209   :group 'TeX-command
  210   :type 'boolean)
  211 
  212 (defun TeX-pop-to-buffer (buffer &optional other-window norecord)
  213   "Compatibility wrapper for `pop-to-buffer'.
  214 
  215 Select buffer BUFFER in some window, preferably a different one.
  216 BUFFER may be a buffer, a string (a buffer name), or nil.
  217 If BUFFER is a string which is not the name of an existing buffer,
  218 then this function creates a buffer with that name.
  219 If BUFFER is nil, then it chooses some other buffer.
  220 If `pop-up-windows' is non-nil, windows can be split to do this.
  221 If optional second arg OTHER-WINDOW is non-nil, insist on finding another
  222 window even if BUFFER is already visible in the selected window,
  223 and ignore `same-window-regexps' and `same-window-buffer-names'.
  224 This function returns the buffer it switched to.
  225 This uses the function `display-buffer' as a subroutine; see the documentation
  226 of `display-buffer' for additional customization information.
  227 
  228 Optional third arg NORECORD non-nil means do not put this buffer
  229 at the front of the list of recently selected ones."
  230   (pop-to-buffer buffer other-window (and norecord (not TeX-record-buffer))))
  231 
  232 (defun TeX-recenter-output-buffer (line)
  233   "Redisplay buffer of TeX job output so that most recent output can be seen.
  234 The last line of the buffer is displayed on line LINE of the window, or
  235 at bottom if LINE is nil."
  236   (interactive "P")
  237   (let ((buffer (TeX-active-buffer)))
  238     (if buffer
  239     (let ((old-buffer (current-buffer)))
  240       (TeX-pop-to-buffer buffer t t)
  241       (bury-buffer buffer)
  242       (goto-char (point-max))
  243       (recenter (if line
  244             (prefix-numeric-value line)
  245               (/ (window-height) 2)))
  246       (TeX-pop-to-buffer old-buffer nil t))
  247       (message "No process for this document."))))
  248 
  249 (defun TeX-kill-job ()
  250   "Kill the currently running TeX job."
  251   (interactive)
  252   (let ((process (TeX-active-process)))
  253     (if process
  254     (kill-process process)
  255       ;; Should test for TeX background process here.
  256       (error "No TeX process to kill"))))
  257 
  258 ;; FIXME: The vars below are defined in this file, but they're defined too
  259 ;; far down (i.e. further down than their first use), so we have to pre-declare
  260 ;; them here to explain it to the compiler.
  261 ;; We should move those vars's definitions earlier instead!
  262 (defvar TeX-current-process-region-p)
  263 (defvar TeX-save-query)
  264 (defvar TeX-parse-function)
  265 (defvar TeX-sentinel-function)
  266 (defvar TeX-sentinel-default-function)
  267 (defvar compilation-in-progress)
  268 (defvar TeX-current-page)
  269 (defvar TeX-error-overview-open-after-TeX-run)
  270 (defvar TeX-error-list)
  271 (defvar TeX-parse-all-errors)
  272 (defvar TeX-command-buffer)
  273 (defvar TeX-region)
  274 
  275 (defun TeX-home-buffer ()
  276   "Go to the buffer where you last issued a TeX command.
  277 If there is no such buffer, or you already are in that buffer, find
  278 the master file."
  279   (interactive)
  280   (if (or (null TeX-command-buffer)
  281       (null (buffer-name TeX-command-buffer))
  282       (eq TeX-command-buffer (current-buffer)))
  283       (find-file (TeX-master-file TeX-default-extension))
  284     (switch-to-buffer TeX-command-buffer)))
  285 
  286 (defvar TeX-error-last-visited -1
  287   "Index of the last visited error listed in `TeX-error-list'.
  288 
  289 This variable is intended to be set only in output buffer so it
  290 will be shared among all files of the same document.")
  291 (make-variable-buffer-local 'TeX-error-last-visited)
  292 
  293 (defun TeX-get-parse-function ()
  294   "Get the parse function for the current buffer."
  295   (with-current-buffer TeX-command-buffer
  296     (TeX-process-get-variable (TeX-active-master) 'TeX-parse-function)))
  297 
  298 (defun TeX-next-error (&optional arg reparse)
  299   "Find the next error in the TeX output buffer.
  300 
  301 A prefix ARG specifies how many error messages to move;
  302 negative means move back to previous error messages, if possible.
  303 
  304 If REPARSE is non-nil, reparse the error message buffer.
  305 
  306 \\[universal-argument] as a prefix means reparse the error
  307 message buffer and start at the first error."
  308   (interactive "P")
  309   (if (or (null (TeX-active-buffer))
  310       (eq 'compilation-mode (with-current-buffer TeX-command-buffer
  311                   major-mode)))
  312       (next-error arg reparse)
  313 
  314     ;; Force reparsing when the function is called with a universal-argument.
  315     (if (consp arg) (setq reparse t arg nil))
  316 
  317     (funcall (TeX-get-parse-function) arg reparse)))
  318 
  319 (defun TeX-previous-error (arg)
  320   "Find the previous error in the TeX output buffer.
  321 
  322 Prefix arg N says how many error messages to move backward (or
  323 forward, if negative).
  324 
  325 This works only with TeX commands and if the
  326 `TeX-parse-all-errors' variable is non-nil."
  327   (interactive "p")
  328   (if (or (null (TeX-active-buffer))
  329       (eq 'compilation-mode (with-current-buffer TeX-command-buffer
  330                   major-mode)))
  331       (previous-error arg)
  332 
  333     (let ((parse-function (TeX-get-parse-function)))
  334       (if (and TeX-parse-all-errors (equal parse-function #'TeX-parse-TeX))
  335       ;; When `TeX-parse-all-errors' is non-nil and the parsing function is
  336       ;; `TeX-parse-TeX' we can move backward in the errors.
  337       (TeX-parse-TeX (- arg) nil)
  338     ;; XXX: moving backward in the errors hasn't yet been implemented for
  339     ;; other parsing functions.
  340     (error "Jumping to previous error not supported")))))
  341 
  342 ;;; Command Query
  343 
  344 (defvar TeX-error-overview-frame nil
  345   "The frame of the error overview.")
  346 
  347 (defconst TeX-error-overview-buffer-name "*TeX errors*"
  348   "Name of the buffer in which to show error list.")
  349 
  350 (defvar LaTeX-idx-md5-alist nil
  351   "Alist of MD5 hashes of idx file.
  352 
  353 Car is the idx file, cdr is its md5 hash.")
  354 
  355 (defvar LaTeX-idx-changed-alist nil
  356   "Whether the idx files changed.
  357 
  358 Car is the idx file, cdr is whether idx changed after LaTeX
  359 run.")
  360 
  361 (defcustom TeX-check-engine t
  362   "Whether AUCTeX should check the correct engine has been set before running LaTeX commands."
  363   :group 'TeX-command
  364   :type 'boolean)
  365 
  366 (defvar TeX-check-engine-list '(default luatex omega xetex)
  367   "List of engines required by the loaded TeX packages.
  368 
  369 Do not set this variable directly, use
  370 `TeX-check-engine-add-engines' to specify required engines.")
  371 (make-variable-buffer-local 'TeX-check-engine-list)
  372 
  373 (defun TeX-check-engine-add-engines (&rest engines)
  374   "Add ENGINES to list of required engines.
  375 
  376 Set `TeX-check-engine-list' to the intersection between the list
  377 itself and the list of provided engines.
  378 
  379 See for example style/fontspec.el"
  380   (let ((list TeX-check-engine-list)
  381     (res nil))
  382     (setq TeX-check-engine-list
  383       ;; The following is based on the definition of `cl-intersection' of
  384       ;; GNU Emacs.
  385       (and list engines
  386            (if (equal list engines) list
  387          (or (>= (length list) (length engines))
  388              (setq list (prog1 engines (setq engines list))))
  389          (while engines
  390            (if (memq (car engines) list)
  391                (push (car engines) res))
  392            (pop engines))
  393          res)))))
  394 
  395 (defun TeX-check-engine (name)
  396   "Check the correct engine has been set.
  397 
  398 Look into `TeX-check-engine-list' for the required engines.
  399 
  400 NAME is the command to be run.  Actually do the check only if the
  401 variable `TeX-check-engine' is non-nil and LaTeX is the command
  402 to be run."
  403   (and
  404    (string= name "LaTeX")
  405    TeX-check-engine
  406    TeX-check-engine-list
  407    (null (memq TeX-engine TeX-check-engine-list))
  408    (memq TeX-engine '(default luatex omega xetex))
  409    ;; The set engine is not listed in `TeX-check-engine-list'.  We check only
  410    ;; builtin engines because we can't take care of custom ones.  Do nothing if
  411    ;; there is no allowed engine, we don't know what to do in that case.
  412    (let ((length (length TeX-check-engine-list))
  413      (name-alist '((default . "TeX")
  414                (luatex  . "LuaTeX")
  415                (omega   . "Omega")
  416                (xetex   . "XeTeX")))
  417      (completion-ignore-case t)
  418      (engine nil))
  419      (when
  420      (cond
  421       ;; There is exactly one allowed engine.
  422       ((= length 1)
  423        (setq engine (car TeX-check-engine-list))
  424        (y-or-n-p (format "%s is required to build this document.
  425 Do you want to use this engine? " (cdr (assoc engine name-alist)))))
  426       ;; More than one engine is allowed.
  427       ((> length 1)
  428        (if (y-or-n-p (format "It appears %s are required to build this document.
  429 Do you want to select one of these engines? "
  430                  (mapconcat
  431                   (lambda (elt) (cdr (assoc elt name-alist)))
  432                   TeX-check-engine-list ", ")))
  433            (setq engine
  434              (car (rassoc
  435                (completing-read
  436                 (format
  437                  "Choose between %s: "
  438                  (mapconcat
  439                   (lambda (elt) (cdr (assoc elt name-alist)))
  440                   TeX-check-engine-list ", "))
  441                 (mapcar
  442                  (lambda (elt) (cdr (assoc elt name-alist)))
  443                  TeX-check-engine-list))
  444                name-alist)))
  445          ;; Don't keep asking.  If user doesn't want to change engine,
  446          ;; probably has a good reason.  In order to do so, without adding
  447          ;; yet another variable we just hack `TeX-check-engine-list' and
  448          ;; make it nil.
  449          (setq TeX-check-engine-list nil))))
  450        (TeX-engine-set engine)
  451        (when (and (fboundp 'add-file-local-variable)
  452           (y-or-n-p "Do you want to remember the choice? "))
  453      (add-file-local-variable 'TeX-engine engine)
  454      (save-buffer))))))
  455 
  456 (defcustom TeX-check-TeX t
  457   "Whether AUCTeX should check if a working TeX distribution is present."
  458   :group 'TeX-command
  459   :type 'boolean)
  460 
  461 (defcustom TeX-check-TeX-command-not-found 127
  462   "Numerical code returned by shell for a command not found error."
  463   :group 'TeX-command
  464   :type 'integer)
  465 
  466 (defun TeX-command (name file &optional override-confirm)
  467   "Run command NAME on the file returned by calling FILE.
  468 
  469 FILE is the symbol of a function returning a file name.  The
  470 function has one optional argument, the extension to use on the
  471 file.
  472 
  473 Use the information in `TeX-command-list' to determine how to run
  474 the command.
  475 
  476 If OVERRIDE-CONFIRM is a prefix argument, confirmation will be
  477 asked if it is positive, and suppressed if it is not.
  478 
  479 Run function `TeX-check-engine' to check the correct engine has
  480 been set."
  481   (TeX-check-engine name)
  482 
  483   ;; Make sure that `TeX-command-buffer' is set always.
  484   ;; It isn't safe to remove similar lines in `TeX-run-command' etc.
  485   ;; because preview-latex calls `TeX-run-command' directly.
  486   (setq-default TeX-command-buffer (current-buffer))
  487 
  488   (cond ((eq file #'TeX-region-file)
  489      (setq TeX-current-process-region-p t))
  490     ((eq file #'TeX-master-file)
  491      (setq TeX-current-process-region-p nil)))
  492 
  493   ;; When we're operating on a region, we need to update the position
  494   ;; of point in the region file so that forward search works.
  495   (if (string= name "View") (TeX-region-update-point))
  496 
  497   (let ((command (TeX-command-expand (nth 1 (assoc name TeX-command-list))
  498                      file))
  499     (hook (nth 2 (assoc name TeX-command-list)))
  500     (confirm (if override-confirm
  501              (> (prefix-numeric-value override-confirm) 0)
  502            (nth 3 (assoc name TeX-command-list)))))
  503 
  504     ;; Verify the expanded command
  505     (if confirm
  506     (setq command
  507           (read-from-minibuffer (concat name " command: ") command
  508                     nil nil)))
  509 
  510     ;; Kill the frame and buffer associated to the error overview before running
  511     ;; the command, but keep them if the command to be run is View.
  512     (unless (string= name "View")
  513       (if (frame-live-p TeX-error-overview-frame)
  514       (delete-frame TeX-error-overview-frame))
  515       (if (get-buffer TeX-error-overview-buffer-name)
  516       (kill-buffer TeX-error-overview-buffer-name)))
  517 
  518     ;; Before running some commands, check that AUCTeX is able to find "tex"
  519     ;; program.
  520     (and TeX-check-TeX
  521          (member name '("TeX" "LaTeX" "AmSTeX" "ConTeXt" "ConTeXt Full"))
  522      (= TeX-check-TeX-command-not-found
  523             (call-process TeX-shell nil nil nil
  524                           TeX-shell-command-option TeX-command))
  525      (error (format "ERROR: AUCTeX cannot find a working TeX distribution.
  526 Make sure you have one and that TeX binaries are in PATH environment variable%s"
  527             (if (eq system-type 'darwin)
  528                 ".
  529 If you are using OS X El Capitan or later
  530 remember to add /Library/TeX/texbin/ to your PATH"
  531               ""))))
  532 
  533     ;; Now start the process
  534     (setq file (funcall file))
  535     (TeX-process-set-variable file 'TeX-command-next TeX-command-Show)
  536     (funcall hook name command file)))
  537 
  538 (defvar TeX-command-text)               ;Dynamically scoped.
  539 (defvar TeX-command-pos)                ;Dynamically scoped.
  540 
  541 (defun TeX-command-expand (command file &optional list)
  542   "Expand COMMAND for FILE as described in LIST.
  543 LIST default to `TeX-expand-list'.  As a special exception,
  544 `%%' can be used to produce a single `%' sign in the output
  545 without further expansion."
  546   (defvar TeX-command-pos)
  547   (let (pat
  548     pos ;;FIXME: Should this be dynamically scoped?
  549     entry TeX-command-text TeX-command-pos
  550         expansion-res case-fold-search string expansion arguments)
  551     (setq list (cons
  552         (list "%%" (lambda nil
  553                  (setq pos (1+ pos))
  554                  "%"))
  555         (or list (TeX-expand-list)))
  556       pat (regexp-opt (mapcar #'car list)))
  557     ;; `TeX-command-expand' is called with `file' argument being one
  558     ;; of `TeX-master-file', `TeX-region-file' and
  559     ;; `TeX-active-master'.  The return value of these functions
  560     ;; sometimes needs suitable "decorations" for an argument for
  561     ;; underlying shell or latex executable, or both, when the
  562     ;; relavant file name involves some special characters such as
  563     ;; space and multibyte characters.  Hence embed that function in a
  564     ;; template prepared for that purpose.
  565     (setq file (apply-partially
  566         #'TeX--master-or-region-file-with-extra-quotes
  567         file))
  568     (while (setq pos (string-match pat command pos))
  569       (setq string (match-string 0 command)
  570         entry (assoc string list)
  571         expansion (car (cdr entry)) ;Second element
  572         arguments (cdr (cdr entry)) ;Remaining elements
  573         string (save-match-data
  574              ;; Note regarding the special casing of `file':
  575              ;; `file' is prevented from being evaluated as a
  576              ;; function because inside of AUCTeX it only has
  577              ;; a meaning as a variable.  This makes sure that
  578              ;; a function definition made by an external
  579              ;; package (e.g. icicles) is not picked up.
  580              (cond ((and (not (eq expansion 'file))
  581                  (functionp expansion))
  582                 (apply expansion arguments))
  583                ((boundp expansion)
  584                             (setq expansion-res
  585                                   (apply (symbol-value expansion) arguments))
  586                             (when (eq expansion 'file)
  587                               ;; Advance past the file name in order to
  588                               ;; prevent expanding any substring of it.
  589                               (setq pos (+ pos (length expansion-res))))
  590                 expansion-res)
  591                (t
  592                 (error "Nonexpansion %s" expansion)))))
  593       (if (stringp string)
  594       (setq command
  595         (replace-match string t t command)))))
  596   command)
  597 
  598 (defun TeX--master-or-region-file-with-extra-quotes
  599     (file-fn &optional extension nondirectory ask extra)
  600   "Return file name with quote for shell.
  601 Helper function of `TeX-command-expand'.
  602 
  603 This is a kind of template.  How to use:
  604 Fix, by `apply-partially', the first argument FILE-FN as one of
  605 the three functions `TeX-master-file', `TeX-region-file' or
  606 `TeX-active-master'.  Then the result is just a wrapper for that
  607 function suitable in `TeX-command-expand'.
  608 
  609 As a wrapper described above, it passes EXTENSION, NONDIRECTORY
  610 and ASK to the \"bare\" function as-is, and arranges the returned
  611 file name for use with command shell.  I.e. it encloses the file
  612 name with space within quotes `\"' first when \" \\input\" is
  613 supplemented (indicated by dynamically binded variable
  614 `TeX-command-text' having string value.)  It also encloses the
  615 file name within \\detokenize{} when the following three
  616 conditions are met:
  617 1. compiling with standard (pdf)LaTeX or upLaTeX
  618 2. \" \\input\" is supplemented
  619 3. EXTRA is non-nil (default when expanding \"%T\")"
  620   (shell-quote-argument
  621    (let* ((raw (funcall file-fn extension nondirectory ask))
  622       ;; String `TeX-command-text' means that the file name is
  623       ;; given through \input command.
  624       (quote-for-space (if (and (stringp TeX-command-text)
  625                     (string-match " " raw))
  626                    "\"" "")))
  627      (format
  628       (if (and extra
  629            (stringp TeX-command-text)
  630            (memq major-mode '(latex-mode doctex-mode))
  631            (memq TeX-engine '(default uptex)))
  632       ;; Since TeXLive 2018, the default encoding for LaTeX
  633       ;; files has been changed to UTF-8 if used with
  634       ;; classic TeX or pdfTeX.  I.e.,
  635       ;; \usepackage[utf8]{inputenc} is enabled by default
  636       ;; in (pdf)latex.
  637       ;; c.f. LaTeX News issue 28
  638       ;; Due to this change, \detokenize is required to
  639       ;; recognize non-ascii characters in the file name
  640       ;; when \input precedes.
  641       "\\detokenize{ %s }" "%s")
  642       (concat quote-for-space raw quote-for-space)))))
  643 
  644 (defun TeX-check-files (derived originals extensions)
  645   "Check if DERIVED is newer than any of the ORIGINALS.
  646 Try each original with each member of EXTENSIONS, in all directories
  647 in `TeX-check-path'.  Returns true if any of the ORIGINALS with any of the
  648 EXTENSIONS are newer than DERIVED.  Will prompt to save the buffer of any
  649 ORIGINALS which are modified but not saved yet."
  650   (let (existingoriginals
  651         found
  652     (extensions (TeX-delete-duplicate-strings extensions))
  653         (buffers (buffer-list)))
  654     (dolist (path (TeX-delete-duplicate-strings
  655            (mapcar (lambda (dir)
  656                  (expand-file-name (file-name-as-directory dir)))
  657                (append
  658                 TeX-check-path
  659                 ;; In `TeX-command-default', this function is used to
  660                 ;; check whether bibliography databases are newer
  661                 ;; than generated *.bbl files, but bibliography
  662                 ;; database are relative to `TeX-master-directory'
  663                 ;; and the test can be run also from included files
  664                 ;; that are in directories different from
  665                 ;; `TeX-master-directory'.
  666                 (list (TeX-master-directory))))))
  667       (dolist (orig originals)
  668     (dolist (ext extensions)
  669       (let ((filepath (concat path orig "." ext)))
  670         (if (or (file-exists-p filepath)
  671             (get-file-buffer filepath))
  672                 (setq existingoriginals (cons filepath existingoriginals)))))))
  673     (while buffers
  674       (let* ((buffer (car buffers))
  675              (name (buffer-file-name buffer)))
  676         (setq buffers (cdr buffers))
  677         (if (and name (member name existingoriginals))
  678             (progn
  679               (and (buffer-modified-p buffer)
  680                    (or (not TeX-save-query)
  681                        (y-or-n-p (concat "Save file "
  682                                          (buffer-file-name buffer)
  683                                          "? ")))
  684                    (with-current-buffer buffer (save-buffer)))))))
  685     (dolist (eo existingoriginals)
  686       (if (file-newer-than-file-p eo derived)
  687           (setq found t)))
  688     found))
  689 
  690 (defcustom TeX-command-sequence-max-runs-same-command 4
  691   "Maximum number of runs of the same command."
  692   :type 'integer
  693   :group 'TeX-command)
  694 
  695 (defcustom TeX-command-sequence-max-runs 12
  696   "Maximum number of total runs."
  697   :type 'integer
  698   :group 'TeX-command)
  699 
  700 (defvar TeX-command-sequence-count-same-command 1
  701   "Counter for the runs of the same command in `TeX-command-sequence'.")
  702 
  703 (defvar TeX-command-sequence-count 1
  704   "Counter for the total runs of `TeX-command-sequence'.")
  705 
  706 (defvar TeX-command-sequence-last-command nil
  707   "Last command run in `TeX-command-sequence'.")
  708 
  709 (defvar TeX-command-sequence-sentinel nil
  710   "Sentinel for `TeX-command-sequence'.")
  711 
  712 (defvar TeX-command-sequence-file-function nil
  713   "File function for `TeX-command-sequence'.")
  714 
  715 (defvar TeX-command-sequence-command nil
  716   "Command argument for `TeX-command-sequence'.
  717 
  718 It is set in `TeX-command-sequence' and used in
  719 `TeX-command-sequence-sentinel' to call again
  720 `TeX-command-sequence' with the appropriate command argument.")
  721 
  722 (defun TeX-command-sequence (command &optional reset file-fn)
  723   "Run a sequence of TeX commands defined by COMMAND.
  724 
  725 The COMMAND argument may be
  726 
  727   * nil: no command will be run in this case
  728 
  729   * a string with a command from `TeX-command-list'
  730 
  731   * a non-nil list of strings, which are commands from
  732     `TeX-command-list'; the car of the list is used as command to
  733     be executed in the first run of `TeX-command-sequence', the
  734     cdr of the list will be passed to the function in the next
  735     run, etc.
  736 
  737   * a function name, returning a string which is command from
  738     `TeX-command-list'; it will be funcall'd (without arguments!)
  739     and used again in the next run of `TeX-command-sequence'.
  740 
  741   * with any other value the function `TeX-command-default' is
  742     used to determine the command to run, until a stopping
  743     condition is met.
  744 
  745 This function runs at most
  746 `TeX-command-sequence-max-runs-same-command' times the same
  747 command in a row, and `TeX-command-sequence-max-runs' times in
  748 total in any case.  It ends when `TeX-command-Show' is the
  749 command to be run.
  750 
  751 A non-nil value for the optional argument RESET means this is the
  752 first run of the function and some variables need to be reset.
  753 
  754 FILE-FN is a function of zero arguments returning the current
  755 filename.  Valid choices are `TeX-master-file' (default if
  756 omitted) and `TeX-region-file'."
  757   (setq TeX-command-sequence-file-function (or file-fn #'TeX-master-file))
  758   (if (null command)
  759       (message "No command to run.")
  760     (let (cmd process)
  761       (cond
  762        ((stringp command)
  763     (setq cmd command
  764           TeX-command-sequence-command nil))
  765        ((listp command)
  766     (setq cmd (pop command)
  767           TeX-command-sequence-command command))
  768        ((functionp command)
  769     (setq cmd (funcall command)
  770           TeX-command-sequence-command command))
  771        (t
  772     (setq cmd (TeX-command-default
  773            ;; File function should be called with nil `nondirectory'
  774            ;; argument, otherwise `TeX-command-sequence' won't work in
  775            ;; included files not placed in `TeX-master-directory'.  In
  776            ;; addition, `TeX-master-file' is called with the third
  777            ;; argument (`ask') set to t, so that the master file is
  778            ;; properly set.  This is also what `TeX-command-master'
  779            ;; does.
  780            (funcall TeX-command-sequence-file-function nil nil t))
  781           TeX-command-sequence-command t)))
  782       (TeX-command cmd TeX-command-sequence-file-function 0)
  783       (when reset
  784     (setq TeX-command-sequence-count-same-command 1
  785           TeX-command-sequence-count 1
  786           TeX-command-sequence-last-command nil))
  787       (cond
  788        ;; Stop when the same command has been run
  789        ;; `TeX-command-sequence-max-runs-same-command' times in a row.
  790        ((>= TeX-command-sequence-count-same-command
  791         TeX-command-sequence-max-runs-same-command)
  792     (message "Stopping after running %S %d times in a row."
  793          TeX-command-sequence-last-command
  794          TeX-command-sequence-count-same-command))
  795        ;; Stop when there have been `TeX-command-sequence-max-runs' total
  796        ;; compilations.
  797        ((>= TeX-command-sequence-count TeX-command-sequence-max-runs)
  798     (message "Stopping after %d compilations." TeX-command-sequence-count))
  799        ;; The command just run is `TeX-command-Show'.
  800        ((equal command TeX-command-Show))
  801        ;; In any other case continue: increase counters (when needed), update
  802        ;; `TeX-command-sequence-last-command' and run the sentinel.
  803        (t
  804     (if (equal cmd TeX-command-sequence-last-command)
  805         (setq TeX-command-sequence-count-same-command
  806           (1+ TeX-command-sequence-count-same-command))
  807       (setq TeX-command-sequence-count-same-command 1))
  808     (setq TeX-command-sequence-count (1+ TeX-command-sequence-count)
  809           TeX-command-sequence-last-command cmd)
  810     (and (setq process (get-buffer-process (current-buffer)))
  811          (setq TeX-command-sequence-sentinel (process-sentinel process))
  812          (set-process-sentinel process
  813                                    #'TeX-command-sequence-sentinel)))))))
  814 
  815 (defcustom TeX-save-query t
  816   "*If non-nil, ask user for permission to save files before starting TeX."
  817   :group 'TeX-command
  818   :type 'boolean)
  819 
  820 (defvar TeX-command-history nil)
  821 
  822 (defun TeX-command-default (name)
  823   "Guess the next command to be run on NAME."
  824   (let ((command-next nil))
  825     (cond (;; name might be absolute or relative, so expand it for
  826        ;; comparison.
  827        (if (string-equal (expand-file-name name)
  828                  (expand-file-name (TeX-region-file)))
  829            (TeX-check-files (concat name "." (TeX-output-extension))
  830                 ;; Each original will be checked for all dirs
  831                 ;; in `TeX-check-path' so this needs to be just
  832                 ;; a filename without directory.
  833                 (list (file-name-nondirectory name))
  834                 TeX-file-extensions)
  835          (TeX-save-document (TeX-master-file)))
  836        TeX-command-default)
  837       ((and (memq major-mode '(doctex-mode latex-mode))
  838         ;; Want to know if bib file is newer than .bbl
  839         ;; We don't care whether the bib files are open in emacs
  840         (TeX-check-files (concat name ".bbl")
  841                  (mapcar #'car
  842                      (LaTeX-bibliography-list))
  843                  (append BibTeX-file-extensions
  844                      TeX-Biber-file-extensions)))
  845        ;; We should check for bst files here as well.
  846        (if LaTeX-using-Biber TeX-command-Biber TeX-command-BibTeX))
  847       ((and
  848         ;; Rationale: makeindex should be run when final document is almost
  849         ;; complete (see
  850         ;; https://tex-talk.net/2012/09/dont-forget-to-run-makeindex/),
  851         ;; otherwise, after following latex runs, index pages may change due
  852         ;; to changes in final document, resulting in extra makeindex and
  853         ;; latex runs.
  854         (member
  855          (setq command-next
  856            (TeX-process-get-variable
  857             name
  858             'TeX-command-next
  859             (or (and TeX-PDF-mode (TeX-PDF-from-DVI))
  860             TeX-command-Show)))
  861          (list "Dvips" "Dvipdfmx" TeX-command-Show))
  862         (cdr (assoc (expand-file-name (concat name ".idx"))
  863             LaTeX-idx-changed-alist)))
  864        "Index")
  865       (command-next)
  866       (TeX-command-Show))))
  867 
  868 (defun TeX-command-query (name)
  869   "Query the user for what TeX command to use."
  870   (let* ((default (TeX-command-default name))
  871          (completion-ignore-case t)
  872          (answer (or TeX-command-force
  873                      (completing-read
  874                       (concat "Command (default " default "): ")
  875                       (TeX-mode-specific-command-list major-mode) nil t
  876                       nil 'TeX-command-history default))))
  877     ;; If the answer is "latex" it will not be expanded to "LaTeX"
  878     (setq answer (car-safe (TeX-assoc answer TeX-command-list)))
  879     (if (and answer
  880              (not (string-equal answer "")))
  881         answer
  882       default)))
  883 
  884 (defvar TeX-command-next nil
  885   "The default command next time `TeX-command' is invoked.")
  886 
  887  (make-variable-buffer-local 'TeX-command-next)
  888 
  889 (defun TeX-printer-query (&optional queue)
  890   "Query the user for a printer name.
  891 QUEUE is non-nil when we are checking for the printer queue."
  892   (let (command element printer)
  893     (if queue
  894     (unless (setq element 2 command TeX-queue-command)
  895       (error "Need to customize `TeX-queue-command'"))
  896       (unless (setq element 1 command TeX-print-command)
  897       (error "Need to customize `TeX-print-command'")))
  898     (while (progn
  899          (setq printer (if TeX-printer-list
  900                    (let ((completion-ignore-case t))
  901                  (completing-read
  902                   (format "Printer%s: "
  903                       (if TeX-printer-default
  904                           (format " (default %s)" TeX-printer-default) ""))
  905                   TeX-printer-list))
  906                  ""))
  907          (setq printer (or (car-safe (TeX-assoc printer TeX-printer-list))
  908                    printer))
  909          (not (if (or (null printer) (string-equal "" printer))
  910               (setq printer TeX-printer-default)
  911             (setq TeX-printer-default printer)))))
  912 
  913     (let ((expansion (let ((entry (assoc printer TeX-printer-list)))
  914                (or (nth element entry)
  915                command))))
  916       (if (string-match "%p" printer)
  917       (error "Don't use %s in printer names" "%p"))
  918       (while (string-match "%p" expansion)
  919     (setq expansion (replace-match printer t t expansion 0)))
  920       expansion)))
  921 
  922 (defun TeX-style-check (styles)
  923   "Check STYLES compared to the current style options."
  924   (let ((files (TeX-style-list)))
  925     (while (and styles
  926         (not (TeX-member (car (car styles)) files 'string-match)))
  927       (setq styles (cdr styles))))
  928   (if styles
  929       (nth 1 (car styles))
  930     ""))
  931 
  932 (defun TeX-output-extension ()
  933   "Get the extension of the current TeX output file."
  934   (if (listp TeX-output-extension)
  935       (car TeX-output-extension)
  936     (or (TeX-process-get-variable (TeX-active-master)
  937                   'TeX-output-extension
  938                   TeX-output-extension)
  939     TeX-output-extension)))
  940 
  941 (defun TeX-view-mouse (event)
  942   "Start `TeX-view' at mouse position."
  943   (interactive "e")
  944   (with-current-buffer (window-buffer (posn-window (event-start event)))
  945     (goto-char (posn-point (event-start event)))
  946     (TeX-view)))
  947 
  948 (defun TeX-region-update-point ()
  949   "Syncs the location of point in the region file with the current file.
  950 
  951 Thereafter, point in the region file is on the same text as in
  952 the current buffer.
  953 
  954 Do nothing in case the last command hasn't operated on the region
  955 or `TeX-source-correlate-mode' is disabled."
  956   (when (and TeX-current-process-region-p TeX-source-correlate-mode)
  957     (let ((region-buf (get-file-buffer (TeX-region-file t)))
  958       (orig-line (TeX-current-offset))
  959       (pos-in-line (- (point) (max (line-beginning-position)
  960                        (or TeX-command-region-begin
  961                        (region-beginning))))))
  962       (when region-buf
  963     (with-current-buffer region-buf
  964       (goto-char (point-min))
  965       (when (re-search-forward "!offset(\\(-?[0-9]+\\)" nil t)
  966         (let ((offset (string-to-number (match-string 1))))
  967           (goto-char (point-min))
  968           (forward-line (- orig-line offset))
  969           (forward-char pos-in-line))))))))
  970 
  971 (defun TeX-view ()
  972   "Start a viewer without confirmation.
  973 The viewer is started either on region or master file,
  974 depending on the last command issued."
  975   (interactive)
  976   (let ((output-file (TeX-active-master (TeX-output-extension))))
  977     (if (file-exists-p output-file)
  978     (TeX-command "View" 'TeX-active-master 0)
  979       (message "Output file %S does not exist." output-file))))
  980 
  981 (defun TeX-output-style-check (styles)
  982   "Check STYLES compared to the current view output file extension and
  983 the current style options."
  984   (let ((ext  (TeX-output-extension))
  985     (files (TeX-style-list)))
  986     (while (and
  987         styles
  988         (or
  989          (not (string-match (car (car styles)) ext))
  990          (let ((style (nth 1 (car styles))))
  991            (cond
  992         ((listp style)
  993          (while
  994              (and style
  995               (TeX-member (car style) files 'string-match))
  996            (setq style (cdr style)))
  997          style)
  998         ((not (TeX-member style files 'string-match)))))))
  999       (setq styles (cdr styles)))
 1000     (if styles
 1001     (nth 2 (car styles))
 1002       "%v")))
 1003 
 1004 ;;; Command Hooks
 1005 
 1006 (defvar TeX-after-compilation-finished-functions nil
 1007   "Hook being run after TeX/LaTeX/ConTeXt finished successfully.
 1008 The functions in this hook are run with the DVI/PDF output file
 1009 given as argument.  Using this hook can be useful for updating
 1010 the viewer automatically after re-compilation of the document.
 1011 
 1012 If you use an emacs-internal viewer such as `doc-view-mode' or
 1013 `pdf-view-mode', add `TeX-revert-document-buffer' to this hook.")
 1014 
 1015 (make-obsolete-variable 'TeX-after-TeX-LaTeX-command-finished-hook
 1016             'TeX-after-compilation-finished-functions
 1017             "11.89")
 1018 
 1019 (defun TeX-revert-document-buffer (file)
 1020   "Revert the buffer visiting FILE.
 1021 This function is intended to be used in
 1022 `TeX-after-compilation-finished-functions' for users that view
 1023 their compiled document with an emacs viewer such as
 1024 `doc-view-mode' or `pdf-view-mode'.  (Note that this function
 1025 just calls `revert-buffer' in the respective buffer and thus
 1026 requires that the corresponding mode defines a sensible
 1027 `revert-buffer-function'.)"
 1028   (let ((buf (find-buffer-visiting file)))
 1029     (when buf
 1030       (with-current-buffer buf
 1031     (revert-buffer nil t t)))))
 1032 
 1033 (defvar TeX-after-start-process-function
 1034   #'TeX-adjust-process-coding-system
 1035   "Function to adjust coding system of an asynchronous process.
 1036 Called with one argument PROCESS.")
 1037 
 1038 (defun TeX-adjust-process-coding-system (process)
 1039   "Adjust coding system of PROCESS to suitable value.
 1040 Usually coding system is the same as the TeX file with eol format
 1041 adjusted to OS default value.  Take care of Japanese TeX, which
 1042 requires special treatment."
 1043   (if (and (boundp 'japanese-TeX-mode)
 1044        (fboundp 'japanese-TeX-set-process-coding-system)
 1045        (with-current-buffer TeX-command-buffer
 1046          japanese-TeX-mode))
 1047       (japanese-TeX-set-process-coding-system process)
 1048     (let ((cs (coding-system-base (with-current-buffer TeX-command-buffer
 1049                     buffer-file-coding-system))))
 1050       ;; The value of `buffer-file-coding-system' is sometimes
 1051       ;; undecided-{unix,dos,mac}.  That happens when the file
 1052       ;; contains no multibyte chars and only end of line format is
 1053       ;; determined.  Emacs lisp reference recommends not to use
 1054       ;; undecided-* for process coding system, so it might seem
 1055       ;; reasonable to change `undecided' into some fixed coding
 1056       ;; system like this:
 1057       ;; (if (eq 'undecided cs)
 1058       ;;     (setq cs 'utf-8))
 1059       ;; However, that can lose when the following conditions are met:
 1060       ;; (1) The document is divided into multiple files.
 1061       ;; (2) The command buffer contains no multibyte chars.
 1062       ;; (3) The other files contain mutlibyte chars and saved in a
 1063       ;;     coding system other than the one chosen above.
 1064       ;; So we leave `undecided' unchanged here.  Although `undecided'
 1065       ;; is not quite safe for the coding system for encoding, i.e.,
 1066       ;; keyboard input to the TeX process, we expect that this does
 1067       ;; not raise serious problems because it is pretty rare that TeX
 1068       ;; process needs keyboard input of multibyte chars.
 1069 
 1070       ;; `utf-8-with-signature' (UTF-8 with BOM) doesn't suit at all
 1071       ;; for the coding system for encoding because it always injects
 1072       ;; 3-byte BOM in front of its return value (even when the string
 1073       ;; to be sent has only ascii characters!)  Thus we change it
 1074       ;; into `utf-8'.  On decoding, `utf-8' can decode UTF-8 with
 1075       ;; BOM.  So it is safe for both decoding and encoding.
 1076       (if (eq cs 'utf-8-with-signature)
 1077       (setq cs 'utf-8))
 1078 
 1079       ;; Eol format of TeX files can differ from OS default. TeX
 1080       ;; binaries accept all type of eol format in the given files
 1081       ;; and output messages according to OS default.  So we set eol
 1082       ;; format to OS default value.
 1083       (setq cs (coding-system-change-eol-conversion
 1084         cs
 1085         ;; The eol of macosX is LF, not CR.  So we choose
 1086         ;; other than `unix' only for w32 system.
 1087         ;; FIXME: what should we do for cygwin?
 1088         (if (eq system-type 'windows-nt) 'dos 'unix)))
 1089       (set-process-coding-system process cs cs))))
 1090 
 1091 (defcustom TeX-show-compilation nil
 1092   "*If non-nil, show output of TeX compilation in other window."
 1093   :group 'TeX-command
 1094   :type 'boolean)
 1095 
 1096 (defun TeX-run-command (name command file)
 1097   "Create a process for NAME using COMMAND to process FILE.
 1098 Return the new process."
 1099   (let ((default TeX-command-default)
 1100     (buffer (TeX-process-buffer-name file))
 1101     (dir (TeX-master-directory))
 1102     (command-buff (current-buffer)))
 1103     (TeX-process-check file)        ; Check that no process is running
 1104     (setq-default TeX-command-buffer command-buff)
 1105     (get-buffer-create buffer)
 1106     (set-buffer buffer)
 1107     (buffer-disable-undo)
 1108     (erase-buffer)
 1109     (set (make-local-variable 'line-number-display-limit) 0)
 1110     (setq TeX-output-extension nil)
 1111     (set (make-local-variable 'TeX-command-buffer) command-buff)
 1112     (if dir (cd dir))
 1113     (insert "Running `" name "' on `" file "' with ``" command "''\n")
 1114     (TeX-output-mode)
 1115     (if TeX-show-compilation
 1116     (display-buffer buffer)
 1117       (message "Type `%s' to display results of compilation."
 1118            (substitute-command-keys
 1119         "\\<TeX-mode-map>\\[TeX-recenter-output-buffer]")))
 1120     (setq TeX-parse-function #'TeX-parse-command)
 1121     (setq TeX-command-default default)
 1122     (setq TeX-sentinel-function
 1123       (lambda (_process name)
 1124         (message (concat name ": done."))))
 1125     (if TeX-process-asynchronous
 1126     (let ((process (start-process name buffer TeX-shell
 1127                       TeX-shell-command-option command)))
 1128       (if TeX-after-start-process-function
 1129           (funcall TeX-after-start-process-function process))
 1130       (TeX-command-mode-line process)
 1131       (set-process-filter process #'TeX-command-filter)
 1132       (set-process-sentinel process #'TeX-command-sentinel)
 1133       (set-marker (process-mark process) (point-max))
 1134       (setq compilation-in-progress (cons process compilation-in-progress))
 1135       process)
 1136       (setq mode-line-process ": run")
 1137       (force-mode-line-update)
 1138       (call-process TeX-shell nil buffer nil
 1139             TeX-shell-command-option command))))
 1140 
 1141 (defun TeX-run-set-command (name command)
 1142   "Remember TeX command to use to NAME and set corresponding output extension."
 1143   (setq TeX-command-default name
 1144     TeX-output-extension
 1145     (if (and (null (TeX-PDF-from-DVI)) TeX-PDF-mode) "pdf" "dvi"))
 1146   (let ((case-fold-search t)
 1147     (lst TeX-command-output-list))
 1148     (while lst
 1149       (if (string-match (car (car lst)) command)
 1150       (setq TeX-output-extension (car (cdr (car lst)))
 1151         lst nil)
 1152     (setq lst (cdr lst))))))
 1153 
 1154 (defun TeX-run-format (name command file)
 1155   "Create a process for NAME using COMMAND to format FILE with TeX."
 1156   (TeX-run-set-command name command)
 1157   (let ((buffer (TeX-process-buffer-name file))
 1158     (process (TeX-run-command name command file)))
 1159     ;; Hook to TeX debugger.
 1160     (with-current-buffer buffer
 1161       (TeX-parse-reset)
 1162       (setq TeX-parse-function #'TeX-parse-TeX)
 1163       (setq TeX-sentinel-function #'TeX-TeX-sentinel)
 1164       (if TeX-process-asynchronous
 1165       (progn
 1166         ;; Updating the mode line.
 1167         (setq TeX-current-page "[0]")
 1168         (TeX-format-mode-line process)
 1169         (set-process-filter process #'TeX-format-filter)))
 1170       process)))
 1171 
 1172 (defvar TeX-error-report-switches nil
 1173   "Reports presence of errors after `TeX-run-TeX'.
 1174 To test whether the current buffer has a compile error from last
 1175 run of `TeX-run-TeX', use
 1176   (TeX-error-report-has-errors-p)")
 1177 
 1178 (defun TeX-error-report-has-errors-p ()
 1179   "Return non-nil if current buffer has compile errors from last TeX run."
 1180   (plist-get TeX-error-report-switches (intern (TeX-master-file))))
 1181 
 1182 (defun TeX-run-TeX (name command file)
 1183   "Create a process for NAME using COMMAND to format FILE with TeX."
 1184 
 1185   ;; Save information in TeX-error-report-switches
 1186   ;; Initialize error to nil (no error) for current master.
 1187   ;; Presence of error is reported inside `TeX-TeX-sentinel-check'
 1188   (let ((current-master (TeX-master-file))
 1189     (idx-file nil) (element nil))
 1190     ;; the current master file is saved because error routines are
 1191     ;; parsed in other buffers;
 1192     (setq TeX-error-report-switches
 1193       (plist-put TeX-error-report-switches
 1194              'TeX-current-master current-master))
 1195     ;; reset error to nil (no error)
 1196     (setq TeX-error-report-switches
 1197       (plist-put TeX-error-report-switches
 1198              (intern current-master) nil))
 1199 
 1200     ;; Store md5 hash of the index file before running LaTeX.
 1201     (and (memq major-mode '(doctex-mode latex-mode))
 1202      (prog1 (file-exists-p
 1203          (setq idx-file (expand-file-name (concat file ".idx"))))
 1204        ;; In order to avoid confusion and pollution of
 1205        ;; `LaTeX-idx-md5-alist', remove from this alist all md5 hashes of
 1206        ;; the current index file.  Note `assq-delete-all' doesn't work with
 1207        ;; string keys and has problems with non-list elements in Emacs 21
 1208        ;; (see file tex-site.el).
 1209        (while (setq element (assoc idx-file LaTeX-idx-md5-alist))
 1210          (setq LaTeX-idx-md5-alist (delq element LaTeX-idx-md5-alist))))
 1211      (with-temp-buffer
 1212        (insert-file-contents idx-file)
 1213        (push (cons idx-file (md5 (current-buffer))) LaTeX-idx-md5-alist))))
 1214 
 1215   ;; can we assume that TeX-sentinel-function will not be changed
 1216   ;; during (TeX-run-format ..)? --pg
 1217   ;; rather use let* ? --pg
 1218 
 1219   (if TeX-interactive-mode
 1220       (TeX-run-interactive name command file)
 1221     (let* ((sentinel-function TeX-sentinel-default-function)
 1222            (process (TeX-run-format name command file)))
 1223       (setq TeX-sentinel-function sentinel-function)
 1224       (if TeX-process-asynchronous
 1225           process
 1226         (TeX-synchronous-sentinel name file process)))))
 1227 
 1228 ;; backward compatibilty
 1229 
 1230 (defalias 'TeX-run-LaTeX 'TeX-run-TeX)
 1231 
 1232 
 1233 (defun TeX-run-BibTeX (name command file)
 1234   "Create a process for NAME using COMMAND to format FILE with BibTeX."
 1235   (let ((process (TeX-run-command name command file)))
 1236     (setq TeX-sentinel-function #'TeX-BibTeX-sentinel)
 1237     (if TeX-process-asynchronous
 1238     process
 1239       (TeX-synchronous-sentinel name file process))))
 1240 
 1241 (defun TeX-run-Biber (name command file)
 1242   "Create a process for NAME using COMMAND to format FILE with Biber."
 1243   (let ((process (TeX-run-command name command file)))
 1244     (setq TeX-sentinel-function #'TeX-Biber-sentinel)
 1245     (if TeX-process-asynchronous
 1246         process
 1247       (TeX-synchronous-sentinel name file process))))
 1248 
 1249 (defun TeX-run-dvips (name command file)
 1250   "Create a process for NAME using COMMAND to convert FILE with dvips."
 1251   (let ((process (TeX-run-command name command file)))
 1252     (setq TeX-sentinel-function #'TeX-dvips-sentinel)
 1253     (if TeX-process-asynchronous
 1254         process
 1255       (TeX-synchronous-sentinel name file process))))
 1256 
 1257 (defun TeX-run-dvipdfmx (name command file)
 1258   "Create a process for NAME using COMMAND to convert FILE with dvipdfmx."
 1259   (let ((process (TeX-run-command name command file)))
 1260     (setq TeX-sentinel-function #'TeX-dvipdfmx-sentinel)
 1261     (if TeX-process-asynchronous
 1262         process
 1263       (TeX-synchronous-sentinel name file process))))
 1264 
 1265 (defun TeX-run-ps2pdf (name command file)
 1266   "Create a process for NAME using COMMAND to convert FILE with ps2pdf."
 1267   (let ((process (TeX-run-command name command file)))
 1268     (setq TeX-sentinel-function #'TeX-ps2pdf-sentinel)
 1269     (if TeX-process-asynchronous
 1270         process
 1271       (TeX-synchronous-sentinel name file process))))
 1272 
 1273 (defun TeX-run-index (name command file)
 1274   "Create a process for NAME using COMMAND to compile the index file."
 1275   (let ((process (TeX-run-command name command file))
 1276     (element nil))
 1277     (setq TeX-sentinel-function #'TeX-index-sentinel)
 1278     ;; Same cleaning as that for `LaTeX-idx-md5-alist' in `TeX-run-TeX'.
 1279     (while (setq element
 1280          ;; `file' has been determined in `TeX-command-buffer', while
 1281          ;; this function has `TeX-master-directory' as
 1282          ;; `default-directory', then we have to expand `file' file-name
 1283          ;; in the same directory of `TeX-command-buffer'.
 1284          (assoc (with-current-buffer TeX-command-buffer
 1285                 (expand-file-name (concat file ".idx")))
 1286             LaTeX-idx-changed-alist))
 1287       (setq LaTeX-idx-changed-alist (delq element LaTeX-idx-changed-alist)))
 1288     (if TeX-process-asynchronous
 1289         process
 1290       (TeX-synchronous-sentinel name file process))))
 1291 
 1292 (defun TeX-run-compile (_name command _file)
 1293   "Ignore first and third argument, start compile with second argument."
 1294   (let ((default-directory (TeX-master-directory)))
 1295     (setq TeX-command-buffer (compile command)))
 1296   ;; Make `compilation-mode' recognize file names with spaces.
 1297   ;; (bug#36483)
 1298   ;; FIXME: This is just an ad-hoc workaround and it's better to fix
 1299   ;; the regular expression in compile.el properly, if possible.  But
 1300   ;; there was no response to such request in emacs-devel@gnu.org.
 1301   (with-current-buffer TeX-command-buffer
 1302     (make-local-variable 'compilation-error-regexp-alist)
 1303     ;; Add slightly modified entry of the one associated with `comma'
 1304     ;; in `compilation-error-regexp-alist-alist' to pick up file names
 1305     ;; with spaces.
 1306     (add-to-list 'compilation-error-regexp-alist
 1307          '("^\"\\([^,\"\n\t]+\\)\", line \\([0-9]+\\)\
 1308 \\(?:[(. pos]+\\([0-9]+\\))?\\)?[:.,; (-]\\( warning:\\|[-0-9 ]*(W)\\)?" 1 2 3 (4))
 1309          t)))
 1310 
 1311 (defun TeX-run-shell (_name command _file)
 1312   "Ignore first and third argument, start shell-command with second argument."
 1313   (let ((default-directory (TeX-master-directory)))
 1314     (shell-command command)
 1315     (if (eq system-type 'ms-dos)
 1316     (redraw-display))))
 1317 
 1318 (defun TeX-run-discard (_name command _file)
 1319   "Start COMMAND as process, discarding its output.
 1320 NAME and FILE are ignored."
 1321   (let ((default-directory (TeX-master-directory)))
 1322     (call-process TeX-shell
 1323           nil 0 nil
 1324           TeX-shell-command-option
 1325           command)))
 1326 
 1327 (defun TeX-run-discard-foreground (_name command _file)
 1328   "Call process with second argument in the foreground, discarding its output.
 1329 With support for MS-DOS, especially when dviout is used with PC-9801 series."
 1330   (if (and (boundp 'dos-machine-type) (eq dos-machine-type 'pc98)) ;if PC-9801
 1331       (send-string-to-terminal "\e[2J")) ; clear screen
 1332   (call-process TeX-shell (if (eq system-type 'ms-dos) "con") nil nil
 1333         TeX-shell-command-option command)
 1334   (if (eq system-type 'ms-dos)
 1335       (redraw-display)))
 1336 (defalias 'TeX-run-dviout 'TeX-run-discard-foreground)
 1337 
 1338 (defun TeX-run-background (name command _file)
 1339   "Start process with second argument, show output when and if it arrives."
 1340   (let ((dir (TeX-master-directory)))
 1341     (set-buffer (get-buffer-create "*TeX background*"))
 1342     (if dir (cd dir))
 1343     (erase-buffer)
 1344     (let ((process (start-process (concat name " background")
 1345                   nil TeX-shell
 1346                   TeX-shell-command-option command)))
 1347       (if TeX-after-start-process-function
 1348       (funcall TeX-after-start-process-function process))
 1349       (set-process-filter process #'TeX-background-filter)
 1350       (set-process-query-on-exit-flag process nil))))
 1351 
 1352 (defun TeX-run-silent (name command _file)
 1353   "Start process with second argument."
 1354   (let ((dir (TeX-master-directory)))
 1355     (set-buffer (get-buffer-create "*TeX silent*"))
 1356     (if dir (cd dir))
 1357     (erase-buffer)
 1358     (let ((process (start-process (concat name " silent")
 1359                   (current-buffer) TeX-shell
 1360                   TeX-shell-command-option command)))
 1361       (if TeX-after-start-process-function
 1362       (funcall TeX-after-start-process-function process))
 1363       (set-process-query-on-exit-flag process nil))))
 1364 
 1365 (defun TeX-run-interactive (name command file)
 1366   "Run TeX interactively.
 1367 Run command in a buffer (in comint-shell-mode) so that it accepts user
 1368 interaction. If you return to the file buffer after the TeX run,
 1369 Error parsing on \\[next-error] should work with a bit of luck."
 1370   (TeX-run-set-command name command)
 1371   (require 'comint)
 1372   (let ((default TeX-command-default)
 1373     (buffer (TeX-process-buffer-name file))
 1374     (process nil)
 1375     (dir (TeX-master-directory))
 1376     (command-buff (current-buffer))
 1377     (sentinel-function TeX-sentinel-default-function)) ; inherit from major mode
 1378     (TeX-process-check file)        ; Check that no process is running
 1379     (setq-default TeX-command-buffer command-buff)
 1380     (with-output-to-temp-buffer buffer)
 1381     (set-buffer buffer)
 1382     (set (make-local-variable 'TeX-command-buffer) command-buff)
 1383     (setq buffer-read-only nil)
 1384     (if dir (cd dir))
 1385     (insert "Running `" name "' on `" file "' with ``" command "''\n")
 1386     (comint-exec buffer name TeX-shell nil
 1387          (list TeX-shell-command-option command))
 1388     (comint-mode)
 1389     (add-hook 'comint-output-filter-functions #'TeX-interactive-goto-prompt)
 1390     (setq mode-name name)
 1391     (setq TeX-command-default default)
 1392     (setq process (get-buffer-process buffer))
 1393     (if TeX-after-start-process-function
 1394     (funcall TeX-after-start-process-function process))
 1395     (TeX-command-mode-line process)
 1396     (set-process-sentinel process #'TeX-command-sentinel)
 1397     (set-marker (process-mark process) (point-max))
 1398     (setq compilation-in-progress (cons process compilation-in-progress))
 1399     (TeX-parse-reset)
 1400     (setq TeX-parse-function #'TeX-parse-TeX)
 1401     ;; use the sentinel-function that the major mode sets, not the LaTeX one
 1402     (setq TeX-sentinel-function sentinel-function)))
 1403 
 1404 (defun TeX-run-function (_name command _file)
 1405   "Execute Lisp function or function call given as the string COMMAND.
 1406 Parameters NAME and FILE are ignored."
 1407   (let ((fun (car (read-from-string command))))
 1408     (if (functionp fun) (funcall fun) (eval fun))))
 1409 
 1410 (defun TeX-run-discard-or-function (name command file)
 1411   "Start COMMAND as process or execute it as a Lisp function.
 1412 If run as a process, the output is discarded.  COMMAND is
 1413 expected to be a string.  NAME and FILE are ignored."
 1414   (if (functionp (car (read-from-string command)))
 1415       (TeX-run-function name command file)
 1416     (TeX-run-discard name command file)))
 1417 
 1418 (defun TeX-run-ispell-on-document (_command _ignored _name)
 1419   "Run ispell on all open files belonging to the current document.
 1420 This function is *obsolete* and only here for compatibility
 1421 reasons.  Use `TeX-run-function' instead."
 1422   (interactive)
 1423   (TeX-ispell-document ""))
 1424 
 1425 
 1426 ;;; Command Sentinels
 1427 
 1428 (defun TeX-synchronous-sentinel (name file result)
 1429   "Process TeX command output buffer after the process dies."
 1430   (let ((buffer (TeX-process-buffer (file-name-nondirectory file))))
 1431     (with-current-buffer buffer
 1432 
 1433       ;; Append post-mortem information to the buffer
 1434       (goto-char (point-max))
 1435       (insert "\n" mode-name (if (and result (zerop result))
 1436                  " finished" " exited") " at "
 1437           (substring (current-time-string) 0 -5))
 1438       (setq mode-line-process ": exit")
 1439 
 1440       ;; Do command specific actions.
 1441       (setq TeX-command-next TeX-command-Show)
 1442       (goto-char (point-min))
 1443       (apply TeX-sentinel-function nil name nil)
 1444 
 1445       ;; Force mode line redisplay soon
 1446       (force-mode-line-update))))
 1447 
 1448 (defun TeX-command-sentinel (process msg)
 1449   "Process TeX command output buffer after the process dies."
 1450   ;; Set `TeX-transient-master' here because `preview-parse-messages'
 1451   ;; may open files and thereby trigger master file questions which we
 1452   ;; don't want and need because we already know the master.  Use
 1453   ;; `TeX-master-file' instead of `TeX-active-master' to determine the
 1454   ;; master because the region file should never be the master.
 1455   (let* ((TeX-transient-master (TeX-master-file))
 1456      (buffer (process-buffer process))
 1457      (name (process-name process)))
 1458     (cond ((null (buffer-name buffer))  ; buffer killed
 1459        (set-process-buffer process nil)
 1460        (set-process-sentinel process nil))
 1461       ((memq (process-status process) '(signal exit))
 1462        (with-current-buffer buffer
 1463 
 1464          ;; Append post-mortem information to the buffer
 1465          (goto-char (point-max))
 1466          (insert-before-markers "\n" mode-name " " msg)
 1467          (forward-char -1)
 1468          (insert " at "
 1469              (substring (current-time-string) 0 -5))
 1470          (forward-char 1)
 1471 
 1472          ;; Do command specific actions.
 1473          (TeX-command-mode-line process)
 1474          (setq TeX-command-next TeX-command-Show)
 1475          (goto-char (point-min))
 1476          (apply TeX-sentinel-function process name nil)
 1477 
 1478 
 1479          ;; If buffer and mode line will show that the process
 1480          ;; is dead, we can delete it now.  Otherwise it
 1481          ;; will stay around until M-x list-processes.
 1482          (delete-process process))
 1483 
 1484        ;; Force mode line redisplay soon
 1485        ;; Do this in all buffers (bug#38058 and bug#40965)
 1486        (force-mode-line-update t))))
 1487 
 1488   (setq compilation-in-progress (delq process compilation-in-progress)))
 1489 
 1490 
 1491 (defvar TeX-sentinel-function (lambda (_process _name) nil)
 1492   "Hook to cleanup TeX command buffer after temination of PROCESS.
 1493 NAME is the name of the process.")
 1494 
 1495 (make-variable-buffer-local 'TeX-sentinel-function)
 1496 
 1497 
 1498 (defvar TeX-sentinel-default-function (lambda (_process _name) nil)
 1499   "Default for `TeX-sentinel-function'.  To be set in major mode.
 1500 Hook to cleanup TeX command buffer after temination of PROCESS.
 1501 NAME is the name of the process.")
 1502 
 1503 (make-variable-buffer-local 'TeX-sentinel-default-function)
 1504 
 1505 (defun TeX-TeX-sentinel (process name)
 1506   "Cleanup TeX output buffer after running TeX.
 1507 
 1508 Parse the output buffer to collect errors and warnings if the
 1509 variable `TeX-parse-all-errors' is non-nil.
 1510 
 1511 Open the error overview if
 1512 `TeX-error-overview-open-after-TeX-run' is non-nil and there are
 1513 errors or warnings to show."
 1514   (if (TeX-TeX-sentinel-check process name)
 1515       (progn
 1516     (if TeX-parse-all-errors
 1517         (TeX-parse-all-errors))
 1518     (if (and TeX-error-overview-open-after-TeX-run
 1519          (TeX-error-overview-make-entries
 1520           (TeX-master-directory) (TeX-active-buffer)))
 1521         (TeX-error-overview)))
 1522     (message (concat name ": formatted " (TeX-current-pages)))
 1523     (let (dvi2pdf)
 1524     (if (with-current-buffer TeX-command-buffer
 1525        (and TeX-PDF-mode (setq dvi2pdf (TeX-PDF-from-DVI))))
 1526      (setq TeX-command-next dvi2pdf)
 1527        (setq TeX-command-next TeX-command-Show)))))
 1528 
 1529 (defun TeX-current-pages ()
 1530   "Return string indicating the number of pages formatted."
 1531   (cond ((null TeX-current-page)
 1532      "some pages")
 1533     ((string-match "[^0-9]1[^0-9]" TeX-current-page)
 1534      (concat TeX-current-page " page"))
 1535     (t
 1536      (concat TeX-current-page " pages"))))
 1537 
 1538 (defun TeX-TeX-sentinel-check (process name)
 1539   "Cleanup TeX output buffer after running TeX.
 1540 Return nil ifs no errors were found."
 1541   (save-excursion
 1542     (goto-char (point-max))
 1543     (cond
 1544      ((and (string-match "ConTeXt" name) (boundp 'ConTeXt-Mark-version)
 1545        (with-current-buffer TeX-command-buffer
 1546          (string= ConTeXt-Mark-version "IV")))
 1547       (when (re-search-backward " > result saved in file: \\(.*?\\), " nil t)
 1548     (let ((output-file (TeX-match-buffer 1)))
 1549       ;; Shave off quotation marks if present.
 1550       (when (string-match "\\`\"\\(.*\\)\"\\'" output-file)
 1551         (setq output-file (match-string 1 output-file)))
 1552       (setq TeX-output-extension
 1553         (if (string-match "\\.\\([^.]*\\)$" output-file)
 1554             (match-string 1 output-file)
 1555           "dvi")))
 1556     (if (re-search-forward ", \\([0-9]+\\) shipped pages, " nil t)
 1557         (setq TeX-current-page (concat "{" (TeX-match-buffer 1) "}")))))
 1558      (t
 1559       (if (re-search-backward "^Output written on \\(.*?\\) (\\([0-9]+\\) page"
 1560                   nil t)
 1561       (let ((output-file (TeX-match-buffer 1)))
 1562         (setq TeX-current-page (concat "{" (TeX-match-buffer 2) "}"))
 1563         ;; Shave off quotation marks if present.
 1564         (when (string-match "\\`\"\\(.*\\)\"\\'" output-file)
 1565           (setq output-file (match-string 1 output-file)))
 1566         (setq TeX-output-extension
 1567           (if (string-match "\\.\\([^.]*\\)$" output-file)
 1568               (match-string 1 output-file)
 1569             "dvi")))))))
 1570   (if process (TeX-format-mode-line process))
 1571   (if (re-search-forward "^\\(!\\|.*:[0-9]+:\\) " nil t)
 1572       (progn
 1573     (message "%s errors in `%s'. Use %s to display." name (buffer-name)
 1574          (substitute-command-keys
 1575           "\\<TeX-mode-map>\\[TeX-next-error]"))
 1576     (setq TeX-command-next TeX-command-default)
 1577     ;; error reported to TeX-error-report-switches
 1578     (setq TeX-error-report-switches
 1579           (plist-put TeX-error-report-switches
 1580              (intern (plist-get TeX-error-report-switches
 1581                         'TeX-current-master))
 1582              t))
 1583     t)
 1584     (let (dvi2pdf)
 1585     (if (with-current-buffer TeX-command-buffer
 1586        (and TeX-PDF-mode (setq dvi2pdf (TeX-PDF-from-DVI))))
 1587      (setq TeX-command-next dvi2pdf)
 1588        (setq TeX-command-next TeX-command-Show)))
 1589     nil))
 1590 
 1591 ;; This regexp should catch warnings of the type
 1592 ;;   LaTeX Warning: ...
 1593 ;;   LaTeX Font Warning: ...
 1594 ;;   Package xyz123 Warning: ...
 1595 ;;   Class xyz123 Warning: ...
 1596 (defvar LaTeX-warnings-regexp
 1597   "\\(?:LaTeX\\|Class\\|Package\\|\\*\\) [-A-Za-z0-9]* ?[Ww]arning:"
 1598   "Regexp matching LaTeX warnings.")
 1599 
 1600 (defun TeX-LaTeX-sentinel-has-warnings ()
 1601   "Return non-nil, if the output buffer contains warnings.
 1602 Warnings can be indicated by LaTeX or packages."
 1603   (save-excursion
 1604     (goto-char (point-min))
 1605     (re-search-forward (concat "^" LaTeX-warnings-regexp) nil t)))
 1606 
 1607 (defun TeX-LaTeX-sentinel-has-bad-boxes ()
 1608   "Return non-nil, if LaTeX output indicates overfull or underfull boxes."
 1609   (save-excursion
 1610     (goto-char (point-min))
 1611     (re-search-forward "^\\(Ov\\|Und\\)erfull \\\\" nil t)))
 1612 
 1613 ;; should go into latex.el? --pg
 1614 (defun TeX-LaTeX-sentinel (process name)
 1615   "Cleanup TeX output buffer after running LaTeX.
 1616 
 1617 Parse the output buffer to collect errors and warnings if the
 1618 variable `TeX-parse-all-errors' is non-nil.
 1619 
 1620 Open the error overview if
 1621 `TeX-error-overview-open-after-TeX-run' is non-nil and there are
 1622 errors or warnings to show."
 1623   (if TeX-parse-all-errors
 1624       (TeX-parse-all-errors))
 1625   (if (and TeX-error-overview-open-after-TeX-run
 1626        (TeX-error-overview-make-entries
 1627         (TeX-master-directory) (TeX-active-buffer)))
 1628       (TeX-error-overview))
 1629   (cond ((TeX-TeX-sentinel-check process name))
 1630     ((and (save-excursion
 1631         (re-search-forward
 1632          "^Package biblatex Warning: Please (re)run Biber on the file"
 1633          nil t))
 1634           (with-current-buffer TeX-command-buffer
 1635         (and (LaTeX-bibliography-list)
 1636              (TeX-check-files (TeX-master-file "bbl")
 1637                       (TeX-style-list)
 1638                       (append TeX-file-extensions
 1639                           BibTeX-file-extensions
 1640                           TeX-Biber-file-extensions)))))
 1641      (message "%s%s" "You should run Biber to get citations right, "
 1642           (TeX-current-pages))
 1643      (setq TeX-command-next (with-current-buffer TeX-command-buffer
 1644                   TeX-command-Biber)))
 1645     ((and (save-excursion
 1646         (re-search-forward
 1647          "^\\(?:LaTeX\\|Package natbib\\) Warning: Citation" nil t))
 1648           (with-current-buffer TeX-command-buffer
 1649         (and (LaTeX-bibliography-list)
 1650              (TeX-check-files (TeX-master-file "bbl")
 1651                       (TeX-style-list)
 1652                       (append TeX-file-extensions
 1653                           BibTeX-file-extensions
 1654                           TeX-Biber-file-extensions)))))
 1655      (message "%s%s" "You should run BibTeX to get citations right, "
 1656           (TeX-current-pages))
 1657      (setq TeX-command-next (with-current-buffer TeX-command-buffer
 1658                   TeX-command-BibTeX)))
 1659     ((re-search-forward "Package biblatex Warning: Please rerun LaTeX" nil t)
 1660      (message "%s%s" "You should run LaTeX again, " (TeX-current-pages))
 1661      (setq TeX-command-next TeX-command-default))
 1662     ((re-search-forward "^(biblatex)\\W+Page breaks have changed" nil t)
 1663      (message "%s%s" "You should run LaTeX again - page breaks have changed, "
 1664           (TeX-current-pages))
 1665      (setq TeX-command-next TeX-command-default))
 1666     ((re-search-forward "^\\(?:LaTeX Warning: Label(s)\\|\
 1667 Package natbib Warning: Citation(s)\\)" nil t)
 1668      (message "%s%s" "You should run LaTeX again to get references right, "
 1669           (TeX-current-pages))
 1670      (setq TeX-command-next TeX-command-default))
 1671     ((re-search-forward
 1672       "^\\(?:(rerunfilecheck)\\|Package hyperref Warning:\\)\\W+\
 1673 Rerun to get outlines right" nil t)
 1674      (message "%s%s" "You should run LaTeX again to get outlines right, "
 1675           (TeX-current-pages))
 1676      (setq TeX-command-next TeX-command-default))
 1677     ((re-search-forward "^LaTeX Warning: Reference" nil t)
 1678      (message "%s%s%s" name ": there were unresolved references, "
 1679           (TeX-current-pages))
 1680      (let (dvi2pdf)
 1681        (if (with-current-buffer TeX-command-buffer
 1682          (and TeX-PDF-mode (setq dvi2pdf (TeX-PDF-from-DVI))))
 1683            (setq TeX-command-next dvi2pdf)
 1684          (setq TeX-command-next TeX-command-Show))))
 1685     ((re-search-forward "^\\(?:LaTeX Warning: Citation\\|\
 1686 Package natbib Warning:.*undefined citations\\)" nil t)
 1687      (message "%s%s%s" name ": there were unresolved citations, "
 1688           (TeX-current-pages))
 1689      (let (dvi2pdf)
 1690        (if (with-current-buffer TeX-command-buffer
 1691          (and TeX-PDF-mode (setq dvi2pdf (TeX-PDF-from-DVI))))
 1692            (setq TeX-command-next dvi2pdf)
 1693          (setq TeX-command-next TeX-command-Show))))
 1694     ((re-search-forward "Package longtable Warning: Table widths have \
 1695 changed\\. Rerun LaTeX\\." nil t)
 1696      (message
 1697       "%s" "You should run LaTeX again to get table formatting right")
 1698      (setq TeX-command-next TeX-command-default))
 1699     ((re-search-forward "^hf-TikZ Warning: Mark '.*' changed\\. \
 1700 Rerun to get mark in right position\\." nil t)
 1701      (message
 1702       "%s" "You should run LaTeX again to get TikZ marks in right position")
 1703      (setq TeX-command-next TeX-command-default))
 1704     ((re-search-forward "^\\* xsim warning: \"rerun\"" nil t)
 1705      (message
 1706       "%s" "You should run LaTeX again to synchronize exercise properties")
 1707      (setq TeX-command-next TeX-command-default))
 1708     ((re-search-forward
 1709       "^\\(\\*\\* \\)?J?I?p?\\(La\\|Sli\\)TeX\\(2e\\)? \
 1710 \\(Version\\|ver\\.\\|<[0-9/-]*\\(?:u[^>]*\\)?>\\)" nil t)
 1711      (let* ((warnings (and TeX-debug-warnings
 1712                    (TeX-LaTeX-sentinel-has-warnings)))
 1713         (bad-boxes (and TeX-debug-bad-boxes
 1714                 (TeX-LaTeX-sentinel-has-bad-boxes)))
 1715         (add-info (when (or warnings bad-boxes)
 1716                 (concat " (with "
 1717                     (when warnings "warnings")
 1718                     (when (and warnings bad-boxes) " and ")
 1719                     (when bad-boxes "bad boxes")
 1720                     ")"))))
 1721        (message "%s" (concat name ": successfully formatted "
 1722                  (TeX-current-pages) add-info)))
 1723      (let (dvi2pdf)
 1724        (if (with-current-buffer TeX-command-buffer
 1725          (and TeX-PDF-mode (setq dvi2pdf (TeX-PDF-from-DVI))))
 1726            (setq TeX-command-next dvi2pdf)
 1727          (setq TeX-command-next TeX-command-Show))))
 1728     (t
 1729      (message "%s%s%s" name ": problems after " (TeX-current-pages))
 1730      (setq TeX-command-next TeX-command-default)))
 1731 
 1732   ;; Check whether the idx file changed.
 1733   (let ((idx-file nil) (master nil))
 1734     (and (file-exists-p
 1735       (setq idx-file
 1736         (concat
 1737          (setq master
 1738                (with-current-buffer TeX-command-buffer
 1739              (expand-file-name (TeX-active-master)))) ".idx")))
 1740      ;; imakeidx package automatically runs makeindex, thus, we need to be
 1741      ;; sure .ind file isn't newer than .idx.
 1742      (TeX-check-files (concat master ".ind")
 1743               (list (file-name-nondirectory master)) '("idx"))
 1744      (with-temp-buffer
 1745        (insert-file-contents idx-file)
 1746        (not (equal
 1747          ;; Compare old md5 hash of the idx file with the new one.
 1748          (cdr (assoc idx-file LaTeX-idx-md5-alist))
 1749          (md5 (current-buffer)))))
 1750      (push (cons idx-file t) LaTeX-idx-changed-alist)))
 1751 
 1752   (unless (TeX-error-report-has-errors-p)
 1753     (run-hook-with-args 'TeX-after-compilation-finished-functions
 1754             (with-current-buffer TeX-command-buffer
 1755               (expand-file-name
 1756                (TeX-active-master (TeX-output-extension)))))))
 1757 
 1758 ;; should go into latex.el? --pg
 1759 (defun TeX-BibTeX-sentinel (_process _name)
 1760   "Cleanup TeX output buffer after running BibTeX."
 1761   (goto-char (point-max))
 1762   (cond
 1763    ;; Check whether BibTeX reports any warnings or errors.
 1764    ((re-search-backward (concat
 1765              "^(There \\(?:was\\|were\\) \\([0-9]+\\) "
 1766              "\\(warnings?\\|error messages?\\))")
 1767                         nil t)
 1768     ;; Tell the user their number so that she sees whether the
 1769     ;; situation is getting better or worse.
 1770     (message (concat "BibTeX finished with %s %s. "
 1771              "Type `%s' to display output.")
 1772          (match-string 1) (match-string 2)
 1773          (substitute-command-keys
 1774           "\\<TeX-mode-map>\\[TeX-recenter-output-buffer]")))
 1775    (t
 1776     (message (concat "BibTeX finished successfully. "
 1777              "Run LaTeX again to get citations right."))))
 1778   ;; In any case, run the default next command.
 1779   (setq TeX-command-next TeX-command-default))
 1780 
 1781 (defun TeX-Biber-sentinel (_process _name)
 1782   "Cleanup TeX output buffer after running Biber."
 1783   (goto-char (point-max))
 1784   (cond
 1785    ((re-search-backward "^INFO - \\(WARNINGS\\|ERRORS\\): \\([0-9]+\\)" nil t)
 1786     (message (concat "Biber finished with %s %s. "
 1787                      "Type `%s' to display output.")
 1788              (match-string 2) (downcase (match-string 1))
 1789              (substitute-command-keys
 1790               "\\<TeX-mode-map>\\[TeX-recenter-output-buffer]"))
 1791     (setq TeX-command-next TeX-command-default))
 1792    ((re-search-backward "^FATAL" nil t)
 1793     (message (concat "Biber had a fatal error and did not finish! "
 1794                      "Type `%s' to display output.")
 1795              (substitute-command-keys
 1796               "\\<TeX-mode-map>\\[TeX-recenter-output-buffer]"))
 1797     (setq TeX-command-next TeX-command-Biber))
 1798    (t
 1799     (message (concat "Biber finished successfully. "
 1800                      "Run LaTeX again to get citations right."))
 1801     (setq TeX-command-next TeX-command-default))))
 1802 
 1803 (defun TeX-dvips-sentinel (_process _name)
 1804   "Cleanup TeX output buffer after running dvips."
 1805   (goto-char (point-max))
 1806   (cond
 1807    ((search-backward "TeX Output exited abnormally" nil t)
 1808     (message "Dvips failed.  Type `%s' to display output."
 1809          (substitute-command-keys
 1810               "\\<TeX-mode-map>\\[TeX-recenter-output-buffer]")))
 1811    (t
 1812     (if (with-current-buffer TeX-command-buffer
 1813       (and (equal (TeX-PDF-from-DVI) "Dvips") TeX-PDF-mode))
 1814     (setq TeX-output-extension "ps"
 1815           TeX-command-next "Ps2pdf"))
 1816     (message "Dvips finished successfully. "))))
 1817 
 1818 (defun TeX-dvipdfmx-sentinel (_process _name)
 1819   "Cleanup TeX output buffer after running dvipdfmx."
 1820   (goto-char (point-max))
 1821   (cond
 1822    ((search-backward "TeX Output exited abnormally" nil t)
 1823     (message "Dvipdfmx failed.  Type `%s' to display output."
 1824          (substitute-command-keys
 1825               "\\<TeX-mode-map>\\[TeX-recenter-output-buffer]")))
 1826    (t
 1827     (if (with-current-buffer TeX-command-buffer
 1828       (and (equal (TeX-PDF-from-DVI) "Dvipdfmx") TeX-PDF-mode))
 1829     (setq TeX-output-extension "pdf"
 1830           TeX-command-next TeX-command-Show))
 1831     (message "Dvipdfmx finished successfully. "))))
 1832 
 1833 (defun TeX-ps2pdf-sentinel (_process _name)
 1834   "Cleanup TeX output buffer after running ps2pdf."
 1835   (goto-char (point-max))
 1836   (cond
 1837    ((search-backward "TeX Output exited abnormally" nil t)
 1838     (message "ps2pdf failed.  Type `%s' to display output."
 1839          (substitute-command-keys
 1840               "\\<TeX-mode-map>\\[TeX-recenter-output-buffer]")))
 1841    (t
 1842     (if (with-current-buffer TeX-command-buffer
 1843       (and (equal (TeX-PDF-from-DVI) "Dvips") TeX-PDF-mode))
 1844     (setq TeX-command-next TeX-command-Show
 1845           TeX-output-extension "pdf"))
 1846     (message "ps2pdf finished successfully. "))))
 1847 
 1848 (defun TeX-index-sentinel (_process _name)
 1849   "Cleanup TeX output buffer after compiling index."
 1850   (goto-char (point-max))
 1851   (cond
 1852    ((search-backward "TeX Output exited abnormally" nil t)
 1853     (message "Index failed.  Type `%s' to display output."
 1854          (substitute-command-keys
 1855               "\\<TeX-mode-map>\\[TeX-recenter-output-buffer]")))
 1856    (t
 1857     (setq TeX-command-next TeX-command-default)
 1858     (message (concat "Index finished successfully. "
 1859              "Run LaTeX again to get index right.")))))
 1860 
 1861 (defun TeX-command-sequence-sentinel (process string)
 1862   "Call the appropriate sentinel for the current process.
 1863 
 1864 If there are no errors, call back `TeX-command-sequence' using
 1865 `TeX-command-sequence-command' as command argument, unless this
 1866 variable is nil."
 1867   (with-current-buffer (process-buffer process)
 1868     (funcall TeX-command-sequence-sentinel process string)
 1869     (if (string-match "\\(finished\\|exited\\)" string)
 1870     (with-current-buffer TeX-command-buffer
 1871       (unless
 1872           (or
 1873            (TeX-error-report-has-errors-p)
 1874            (null TeX-command-sequence-command))
 1875         (TeX-command-sequence TeX-command-sequence-command nil
 1876                   TeX-command-sequence-file-function))))))
 1877 
 1878 ;;; Process Control
 1879 
 1880 
 1881 ;; This variable is shared with `compile.el'.
 1882 ;; FIXME: Then it should not be defvar'd here!
 1883 (defvar compilation-in-progress nil
 1884   "List of compilation processes now running.")
 1885 
 1886 ;; COMPATIBILITY for emacs < 27
 1887 (if (< emacs-major-version 27)
 1888     (or (assq 'compilation-in-progress minor-mode-alist)
 1889     (setq minor-mode-alist (cons '(compilation-in-progress " Compiling")
 1890                      minor-mode-alist))))
 1891 
 1892 (defun TeX-process-get-variable (name symbol &optional default)
 1893   "Return the value in the process buffer for NAME of SYMBOL.
 1894 
 1895 Return DEFAULT if the process buffer does not exist or SYMBOL is not
 1896 defined."
 1897   (let ((buffer (TeX-process-buffer name)))
 1898     (if (and buffer
 1899          (local-variable-p symbol buffer))
 1900     (with-current-buffer buffer
 1901       (symbol-value symbol))
 1902       default)))
 1903 
 1904 (defun TeX-process-set-variable (name symbol value)
 1905   "Set the variable SYMBOL in the process buffer to VALUE.
 1906 Return nil iff no process buffer exist."
 1907   (let ((buffer (TeX-process-buffer name)))
 1908     (if buffer
 1909     (with-current-buffer buffer
 1910       (set symbol value)
 1911       t)
 1912       nil)))
 1913 
 1914 (defun TeX-process-check (name)
 1915   "Check if a process for the TeX document NAME already exist.
 1916 If so, give the user the choice of aborting the process or the current
 1917 command."
 1918   (let (process)
 1919     (while (and (setq process (TeX-process name))
 1920         (eq (process-status process) 'run))
 1921       (cond
 1922        ((yes-or-no-p (concat "Process `"
 1923                  (process-name process)
 1924                  "' for document `"
 1925                  name
 1926                  "' running, kill it? "))
 1927     (delete-process process))
 1928        ((eq (process-status process) 'run)
 1929        (error "Cannot have two processes for the same document"))))))
 1930 
 1931 (defun TeX-process-buffer-name (name)
 1932   "Return name of AUCTeX buffer associated with the document NAME."
 1933   (concat "*" (abbreviate-file-name (expand-file-name name)) " output*"))
 1934 
 1935 (defun TeX-process-buffer (name)
 1936   "Return the AUCTeX buffer associated with the document NAME."
 1937   (get-buffer (TeX-process-buffer-name name)))
 1938 
 1939 (defun TeX-process (name)
 1940   "Return AUCTeX process associated with the document NAME."
 1941   (and TeX-process-asynchronous
 1942        (get-buffer-process (TeX-process-buffer name))))
 1943 
 1944 ;;; Process Filters
 1945 
 1946 (defun TeX-command-mode-line (process)
 1947   "Format the mode line for a buffer containing output from PROCESS."
 1948     (setq mode-line-process (concat ": "
 1949                     (symbol-name (process-status process))))
 1950     (force-mode-line-update))
 1951 
 1952 (defun TeX-command-filter (process string)
 1953   "Filter to process normal output."
 1954   (with-current-buffer (process-buffer process)
 1955     (save-excursion
 1956       (goto-char (process-mark process))
 1957       (insert-before-markers string)
 1958       (set-marker (process-mark process) (point)))))
 1959 
 1960 (defvar TeX-current-page nil
 1961   "The page number currently being formatted, enclosed in brackets.")
 1962 
 1963  (make-variable-buffer-local 'TeX-current-page)
 1964 
 1965 (defun TeX-format-mode-line (process)
 1966   "Format the mode line for a buffer containing TeX output from PROCESS."
 1967     (setq mode-line-process (concat " " TeX-current-page ": "
 1968                     (symbol-name (process-status process))))
 1969     (force-mode-line-update))
 1970 
 1971 (defun TeX-format-filter (process string)
 1972   "Filter to process TeX output."
 1973   (with-current-buffer (process-buffer process)
 1974     (let (str pos end (pt (marker-position (process-mark process))))
 1975       (save-excursion
 1976     (goto-char pt)
 1977     (insert-before-markers string)
 1978     (set-marker (process-mark process) (point))
 1979     ;; Remove line breaks at columns 79 and 80
 1980     (while (> (point) pt)
 1981       (end-of-line 0)
 1982       (when (and (memq (- (point) (line-beginning-position)) '(79 80))
 1983              ;; Heuristic: Don't delete the linebreak if the next line
 1984              ;; is empty or starts with an opening parenthesis, or if
 1985              ;; point is located after a period and in the next line no
 1986              ;; word char follows.
 1987              (not (memq (char-after (1+ (point))) '(?\n ?\()))
 1988              (not (and (eq (char-before) ?.)
 1989                    (char-after (1+ (point)))
 1990                    (not (eq ?w (char-syntax (char-after (1+ (point)))))))))
 1991         (delete-char 1)))
 1992     (goto-char (marker-position (process-mark process)))
 1993     ;; Determine current page
 1994     (while (and pt
 1995             (skip-chars-backward "^]" pt)
 1996             (> (point) pt))
 1997       (setq end (point))
 1998       (backward-char)
 1999       (skip-chars-backward "-0-9\n." (max (point-min) (- pt 128)))
 2000       (when (and (eq ?\[ (char-before))
 2001              (not (eq ?\] (char-after)))
 2002              (progn
 2003                (setq str (buffer-substring (1- (point)) end)
 2004                  pos nil)
 2005                (while (setq pos (string-match "\n" str pos))
 2006              (setq str (replace-match "" t t str)))
 2007                (string-match
 2008             "\\`\\[-?[0-9]+\\(\\.-?[0-9]+\\)\\{0,9\\}\\]\\'"
 2009             str)))
 2010         (setq TeX-current-page str
 2011           pt nil)
 2012         (TeX-format-mode-line process)))))))
 2013 
 2014 (defvar TeX-parse-function nil
 2015   "Function to call to parse content of TeX output buffer.")
 2016 (make-variable-buffer-local 'TeX-parse-function)
 2017 
 2018 (defun TeX-background-filter (_process string)
 2019   "Filter to process background output."
 2020   (let ((old-window (selected-window))
 2021     (pop-up-windows t))
 2022     (TeX-pop-to-buffer "*TeX background*" nil t)
 2023     (goto-char (point-max))
 2024     (insert string)
 2025     (select-window old-window)))
 2026 
 2027 ;; Copy and adaption of `comint-postoutput-scroll-to-bottom' from CVS
 2028 ;; Emacs of 2005-04-24.
 2029 (defun TeX-interactive-goto-prompt (string)
 2030   "Move point to prompt of an interactive TeX run."
 2031   (let* ((selected (selected-window))
 2032      (current (current-buffer))
 2033      (process (get-buffer-process current)))
 2034     (unwind-protect
 2035     (when process
 2036       (walk-windows
 2037        (lambda (window)
 2038          (when (eq (window-buffer window) current)
 2039            (select-window window)
 2040            (when (and (< (point) (process-mark process))
 2041               (string-match "^\\? $" string))
 2042          (goto-char (process-mark process)))
 2043            (select-window selected)))
 2044        nil t))
 2045       (set-buffer current))))
 2046 
 2047 
 2048 ;;; Active Process
 2049 
 2050 (defvar TeX-current-process-region-p nil
 2051   "This variable is set to t iff the last TeX command is on a region.")
 2052 
 2053 (defun TeX-active-process ()
 2054   "Return the active process for the current buffer."
 2055   (TeX-process (TeX-active-master)))
 2056 
 2057 (defun TeX-active-buffer ()
 2058   "Return the buffer of the active process for this buffer."
 2059   (and TeX-command-buffer
 2060        (with-current-buffer TeX-command-buffer
 2061      (TeX-process-buffer (TeX-active-master)))))
 2062 
 2063 (defun TeX-active-master (&optional extension nondirectory _ignore)
 2064   "The master file currently being compiled.
 2065 
 2066 If optional argument EXTENSION is non-nil, add that file extension to
 2067 the name.  Special value t means use `TeX-default-extension'.
 2068 
 2069 If optional second argument NONDIRECTORY is non-nil, do not include
 2070 the directory.
 2071 
 2072 The compatibility argument IGNORE is ignored."
 2073   ;; The third argument `_ignore' is kept for symmetry with
 2074   ;; `TeX-master-file's third argument `ask'.  For example, it's used
 2075   ;; in `TeX--master-or-region-file-with-extra-quotes', where we don't
 2076   ;; know which function has to be called.  Keep this in mind should
 2077   ;; you want to use another argument here.
 2078   ;; See also the similar comment in `TeX-region-file'.
 2079   (if TeX-current-process-region-p
 2080       (TeX-region-file extension nondirectory)
 2081     (TeX-master-file extension nondirectory)))
 2082 
 2083 (defvar TeX-command-buffer nil
 2084   "The buffer from where the last TeX command was issued.")
 2085 
 2086 ;;; Region File
 2087 
 2088 (defcustom TeX-region-extra ""
 2089   "*String to insert in the region file between the header and the text."
 2090   :group 'TeX-command
 2091   :type 'string)
 2092 
 2093 ;; This was "{\\makeatletter\\gdef\\AucTeX@cite#1[#2]#3{[#3#1#2]}\
 2094 ;;           \\gdef\\cite{\\@ifnextchar[{\\AucTeX@cite{, }}\
 2095 ;;           {\\AucTeX@cite{}[]}}}\n"
 2096 ;; However, that string is inappropriate for plain TeX and ConTeXt.
 2097 ;; This needs reconsideration.
 2098 
 2099 
 2100 (defvar TeX-region-hook nil
 2101   "List of hooks to run before the region file is saved.
 2102 The hooks are run in the region buffer, you may use the variable
 2103 `master-buffer' to access the buffer of the master file and
 2104 `orig-buffer' to access the buffer where \\[TeX-command-region] or
 2105 \\[TeX-command-buffer] is invoked from.")
 2106 
 2107 (defun TeX-quote-filename (file)
 2108   "Convert file name into a form acceptable to TeX."
 2109   (let (pos)
 2110     (while (setq pos (string-match "\\\\" file pos))
 2111       (setq file (replace-match "/" t t file 0)
 2112         pos (1+ pos)))
 2113     (while (setq pos (string-match "[~#]" file pos))
 2114       (setq file (replace-match "\\\\string\\&" t nil file 0)
 2115         pos (+ pos 8))))
 2116   ;; Use \unexpanded so that \message outputs the raw file name.
 2117   ;; When \usepackage[utf8]{inputenc} is used in standard (pdf)latex,
 2118   ;; \message does not output non-ascii file name in raw form without
 2119   ;; \unexpanded, which makes AUCTeX to fail to recognize the file
 2120   ;; names right when analysing the process output buffer.
 2121   ;; Note that \usepackage[utf8]{inputenc} is enabled by default in
 2122   ;; standard (pdf)latex since TeXLive 2018.
 2123   (if (and (memq major-mode '(latex-mode doctex-mode))
 2124        ;; Japanese upLaTeX requires the same treatment with
 2125        ;; respect to non-ascii characters other than Japanese, in
 2126        ;; file names within \message{}.
 2127        ;; However, pLaTeX (non u- version) does not support
 2128        ;; non-ascii file name encoded in UTF-8.  So considering
 2129        ;; `ptex' doesn't make sense here.  We cater for only
 2130        ;; `default' and `uptex' engines.
 2131        (memq TeX-engine '(default uptex)))
 2132       ;; It would fail to put entire `file' inside \unexpanded{} when
 2133       ;; the above loop injects \string before "#" and "~".  So put
 2134       ;; only multibyte characters inside \unexpanded{}.
 2135       ;; It is safe in upLaTeX to use \unexpanded{} on Japanese
 2136       ;; characters though they are handled by upLaTeX in a totally
 2137       ;; different way from inputenc.
 2138       ;; Thus put all multibyte characters, without considering
 2139       ;; whether they are Japanese or not, inside \unexpanded{}.
 2140       (replace-regexp-in-string "[[:multibyte:]]+"
 2141                 "\\\\unexpanded{\\&}" file t)
 2142     file))
 2143 
 2144 (defvar font-lock-mode-enable-list)
 2145 (defvar font-lock-auto-fontify)
 2146 (defvar font-lock-defaults-alist)
 2147 
 2148 (defvar TeX-region-orig-buffer nil
 2149   "The original buffer in which the TeX-region was created.")
 2150 (make-variable-buffer-local 'TeX-region-orig-buffer)
 2151 
 2152 (defun TeX-region-create (file region original offset)
 2153   "Create a new file named FILE with the string REGION.
 2154 The region is taken from ORIGINAL starting at line OFFSET.
 2155 
 2156 The current buffer and master file is searched, in order to ensure
 2157 that the TeX header and trailer information is also included.
 2158 
 2159 The OFFSET is used to provide the debugger with information about the
 2160 original file."
 2161   (let* (;; We shift buffer a lot, so we must keep track of the buffer
 2162      ;; local variables.
 2163      (header-end TeX-header-end)
 2164      (trailer-start TeX-trailer-start)
 2165 
 2166      ;; We seach for header and trailer in the master file.
 2167      (orig-buffer (current-buffer))
 2168      (master-name (TeX-master-file TeX-default-extension))
 2169      (master-buffer (find-file-noselect master-name))
 2170 
 2171      ;; Attempt to disable font lock.
 2172      (font-lock-defaults-alist nil)
 2173      (font-lock-defaults nil)
 2174      (font-lock-maximum-size 0)
 2175      (font-lock-mode-hook nil)
 2176      (font-lock-auto-fontify nil)
 2177      (font-lock-mode-enable-list nil)
 2178      ;; And insert them into the FILE buffer.
 2179      (file-buffer (let (;; Don't query for master file
 2180                 (TeX-transient-master t)
 2181                 ;; Don't choose a special mode (and call its hooks)
 2182                 (auto-mode-alist nil)
 2183                 (magic-mode-alist nil)
 2184                 (enable-local-variables nil)
 2185                 ;; Don't run any f-f hooks
 2186                 (find-file-hook nil))
 2187             (find-file-noselect file)))
 2188      ;; But remember original content.
 2189      original-content
 2190 
 2191      ;; We search for the header from the master file, if it is
 2192      ;; not present in the region.
 2193      (header (if (string-match header-end region)
 2194              ""
 2195            (save-excursion
 2196              (save-restriction
 2197                (set-buffer master-buffer)
 2198                (save-excursion
 2199              (save-restriction
 2200                (widen)
 2201                (goto-char (point-min))
 2202                ;; NOTE: We use the local value of
 2203                ;; TeX-header-end from the master file.
 2204                (if (not (re-search-forward TeX-header-end nil t))
 2205                    ""
 2206                  (re-search-forward "[\r\n]" nil t)
 2207                  (buffer-substring-no-properties
 2208                   (point-min) (point)))))))))
 2209      (header-offset 0)
 2210      first-line
 2211      ;; We search for the trailer from the master file, if it is
 2212      ;; not present in the region.
 2213      (trailer-offset 0)
 2214      (trailer (if (string-match trailer-start region)
 2215               ""
 2216             (save-excursion
 2217               (save-restriction
 2218             (set-buffer master-buffer)
 2219             (save-excursion
 2220               (save-restriction
 2221                 (widen)
 2222                 (goto-char (point-max))
 2223                 ;; NOTE: We use the local value of
 2224                 ;; TeX-trailer-start from the master file.
 2225                 (if (not (re-search-backward TeX-trailer-start nil t))
 2226                 ""
 2227                   ;;(beginning-of-line 1)
 2228                   (re-search-backward "[\r\n]" nil t)
 2229                   (setq trailer-offset (TeX-current-offset))
 2230                   (buffer-substring-no-properties
 2231                    (point) (point-max))))))))))
 2232     ;; file name should be relative to master
 2233     (setq original (TeX-quote-filename (file-relative-name
 2234                     original (TeX-master-directory)))
 2235       master-name (TeX-quote-filename master-name))
 2236 
 2237     ;; If the first line begins with "%&", put that line separately on
 2238     ;; the very first line of the region file so that the first line
 2239     ;; parsing will work.
 2240     (setq first-line (if (and (> (length header) 1)
 2241                   (string= (substring header 0 2) "%&"))
 2242              ;; This would work even if header has no newline.
 2243              (substring header 0 (string-match "\n" header))
 2244                ""))
 2245     (unless (string= first-line "")
 2246       ;; Remove first-line from header.
 2247       (setq header (substring header (length first-line)))
 2248       (setq first-line (concat first-line "\n")))
 2249 
 2250     (with-current-buffer file-buffer
 2251       (setq buffer-read-only t
 2252         buffer-undo-list t)
 2253       (setq original-content (buffer-string))
 2254       (let ((inhibit-read-only t))
 2255     (erase-buffer)
 2256     (setq buffer-file-coding-system
 2257           (with-current-buffer master-buffer buffer-file-coding-system))
 2258     (insert first-line
 2259         "\\message{ !name(" master-name ")}"
 2260         header
 2261         TeX-region-extra
 2262         "\n\\message{ !name(" original ") !offset(")
 2263     (setq header-offset (- offset
 2264                    (1+ (TeX-current-offset))))
 2265     (insert (int-to-string header-offset)
 2266         ") }\n"
 2267         region
 2268         "\n\\message{ !name("  master-name ") !offset(")
 2269     (insert (int-to-string (- trailer-offset
 2270                   (1+ (TeX-current-offset))))
 2271         ") }\n"
 2272         trailer)
 2273     (setq TeX-region-orig-buffer orig-buffer)
 2274     (run-hooks 'TeX-region-hook)
 2275     (if (string-equal (buffer-string) original-content)
 2276         (set-buffer-modified-p nil)
 2277       (save-buffer 0))))))
 2278 
 2279 (defun TeX-region-file (&optional extension nondirectory _ignore)
 2280   "Return TeX-region file name with EXTENSION.
 2281 If optional second argument NONDIRECTORY is non-nil, do not include
 2282 the directory.
 2283 
 2284 The compatibility argument IGNORE is ignored."
 2285   ;; The third argument `_ignore' is kept for symmetry with `TeX-master-file's
 2286   ;; third argument `ask'.  For example, it's used in `TeX-command-sequence',
 2287   ;; where we don't know which function has to be called.  Keep this in mind
 2288   ;; should you want to use another argument here.
 2289   (concat (if nondirectory "" (TeX-master-directory))
 2290       (cond ((eq extension t)
 2291          (concat TeX-region "." TeX-default-extension))
 2292         (extension
 2293          (concat TeX-region "." extension))
 2294         (t
 2295          TeX-region))))
 2296 
 2297 (defcustom TeX-region "_region_"
 2298   "*Base name of temporary file for `TeX-command-region' and `TeX-command-buffer'."
 2299   :group 'TeX-command
 2300   :type 'string)
 2301 
 2302 (defvar LaTeX-command-section-level nil
 2303   "The section level used for `LaTeX-command-section'.
 2304 Will be initialized to `LaTeX-largest-level' buffer-locally.")
 2305 (make-variable-buffer-local 'LaTeX-command-section-level)
 2306 
 2307 (defun LaTeX-command-section-level ()
 2308   "Return the value of `LaTeX-command-section-level'.
 2309 Initialize it to `LaTeX-largest-level' if needed."
 2310   (unless LaTeX-command-section-level
 2311     (setq LaTeX-command-section-level LaTeX-largest-level))
 2312   LaTeX-command-section-level)
 2313 
 2314 
 2315 (defun LaTeX-command-section-change-level (arg)
 2316   "Change `LaTeX-command-section-level' by ARG.
 2317 `LaTeX-command-section-level' is the sectioning level used to
 2318 determine the current section by `LaTeX-command-section'.
 2319 The levels are defined by `LaTeX-section-list'."
 2320   (interactive "p")
 2321   (let ((old-level (car (rassoc (list (LaTeX-command-section-level))
 2322                 LaTeX-section-list))))
 2323     (setq LaTeX-command-section-level (+ LaTeX-command-section-level arg))
 2324     (cond
 2325      ((> LaTeX-command-section-level 6)
 2326       (setq LaTeX-command-section-level 6)
 2327       (message "Cannot shrink LaTeX-command-section-level below subparagraph."))
 2328      ((< LaTeX-command-section-level 0)
 2329       (setq LaTeX-command-section-level 0)
 2330       (message "Cannot enlarge LaTeX-command-section-level above part."))
 2331      (t (message "Changed level from %s to %s."
 2332          old-level (car (rassoc (list LaTeX-command-section-level)
 2333                     LaTeX-section-list)))))))
 2334 
 2335 (defun LaTeX-command-section-boundaries ()
 2336   "Return the boundaries of the current section as (start . end).
 2337 The section is determined by `LaTeX-command-section-level'."
 2338   (let* ((case-fold-search nil)
 2339      (rx (concat "\\\\" (regexp-opt
 2340                  (mapcar
 2341                   (lambda (level)
 2342                 (car (rassoc (list level) LaTeX-section-list)))
 2343                   (let (r)
 2344                 (dotimes (i (1+ (LaTeX-command-section-level)))
 2345                   (push i r))
 2346                 r)))
 2347              "{")))
 2348     (cons (save-excursion
 2349         (re-search-backward rx nil t)
 2350         (point))
 2351       (save-excursion
 2352         (re-search-forward (concat rx "\\|\\\\end{document}") nil t)
 2353         (forward-line 0)
 2354         (point)))))
 2355 
 2356 (defun LaTeX-command-section (&optional override-confirm)
 2357   "Run a command on the current section.
 2358 
 2359 What makes the current section is defined by
 2360 `LaTeX-command-section-level' which can be enlarged or shrunken
 2361 with `LaTeX-command-section-change-level'.
 2362 
 2363 Query the user for a command to run on the temporary file
 2364 specified by the variable `TeX-region'.  The region file will be
 2365 recreated from current section.
 2366 
 2367 If a prefix argument OVERRIDE-CONFIRM is given, confirmation will
 2368 depend on it being positive instead of the entry in
 2369 `TeX-command-list'."
 2370   (interactive "P")
 2371   (if (eq major-mode 'latex-mode)
 2372       (let* ((bounds (LaTeX-command-section-boundaries))
 2373          (TeX-command-region-begin (car bounds))
 2374          (TeX-command-region-end (cdr bounds)))
 2375     (TeX-command-region override-confirm))
 2376     (error "LaTeX-command-section can only be run on LaTeX documents")))
 2377 
 2378 (defun TeX-command-run-all-region ()
 2379   "Compile the current region until an error occurs or it is finished."
 2380   (interactive)
 2381   (TeX-region-update)
 2382   (TeX-command-sequence t t #'TeX-region-file))
 2383 
 2384 (defun LaTeX-command-run-all-section ()
 2385   "Compile the current section until an error occurs or it is finished."
 2386   (interactive)
 2387   (if (eq major-mode 'latex-mode)
 2388       (let* ((bounds (LaTeX-command-section-boundaries))
 2389          (TeX-command-region-begin (car bounds))
 2390          (TeX-command-region-end (cdr bounds)))
 2391     (TeX-region-update)
 2392     (TeX-command-sequence t t #'TeX-region-file))
 2393     (error "LaTeX-command-run-all-section can only be run on LaTeX documents")))
 2394 
 2395 (defun TeX-command-run-all (arg)
 2396   "Compile the current document until an error occurs or it is finished.
 2397 With a prefix ARG (`\\[universal-argument] \\[TeX-command-run-all]'),
 2398 compile the current region instead, e.g, call
 2399 `TeX-command-run-all-region'.  With multiple prefix
 2400 arguments (`\\[universal-argument] \\[universal-argument] \\[TeX-command-run-all]'),
 2401 compile the current section instead, e.g. call
 2402 `LaTeX-command-run-all-section'."
 2403   (interactive "P")
 2404   (cond
 2405    ((null arg)       (TeX-command-sequence t t))
 2406    ((= 4 (car arg))  (TeX-command-run-all-region))
 2407    (t                (LaTeX-command-run-all-section))))
 2408 
 2409 ;;; Parsing
 2410 
 2411 ;;; - Global Parser Variables
 2412 
 2413 (defvar TeX-error-point nil
 2414   "How far we have parsed until now.")
 2415 
 2416 (make-variable-buffer-local 'TeX-error-point)
 2417 
 2418 (defvar TeX-error-file nil
 2419   "Stack of files in which errors have occurred.")
 2420 
 2421 (make-variable-buffer-local 'TeX-error-file)
 2422 
 2423 (defvar TeX-error-offset nil
 2424   "Add this to any line numbers from TeX.  Stack like `TeX-error-file'.")
 2425 
 2426 (make-variable-buffer-local 'TeX-error-offset)
 2427 
 2428 (defun TeX-parse-reset (&optional reparse)
 2429   "Reset all variables used for parsing TeX output.
 2430 If optional argument REPARSE is non-nil, reparse the output log."
 2431   (setq TeX-error-point (point-min)
 2432     TeX-error-offset nil
 2433     TeX-error-file nil
 2434     TeX-error-list nil
 2435     TeX-error-last-visited -1)
 2436   (if reparse
 2437       (TeX-parse-all-errors)))
 2438 
 2439 ;;; - Parsers Hooks
 2440 
 2441 ;; All this parsers hooks should have the same arguments even though they will
 2442 ;; be ignored, because `TeX-next-error' can call any of these functions.
 2443 (defun TeX-parse-command (_arg _reparse)
 2444   "We can't parse anything but TeX."
 2445   (error "I cannot parse %s output, sorry"
 2446      (if (TeX-active-process)
 2447          (process-name (TeX-active-process))
 2448        "this")))
 2449 
 2450 (defun TeX-error-list-skip-warning-p (type ignore)
 2451   "Decide if a warning of `TeX-error-list' should be skipped.
 2452 
 2453 TYPE is one of the types listed in `TeX-error-list', IGNORE
 2454 is the flag to choose if the warning should be skipped."
 2455   ;; The warning should be skipped if it...
 2456   (or
 2457    ;; ...is a warning and we want to ignore all warnings, or...
 2458    (and (null TeX-debug-warnings)
 2459     (equal type 'warning))
 2460    ;; ...is a bad-box and we want to ignore all bad-boxes, or...
 2461    (and (null TeX-debug-bad-boxes)
 2462     (equal type 'bad-box))
 2463    ;; ...is a warning to be ignored.
 2464    (and TeX-suppress-ignored-warnings
 2465     ignore)))
 2466 
 2467 (defun TeX-parse-TeX (arg reparse)
 2468   "Find the next error produced by running TeX.
 2469 
 2470 ARG specifies how many error messages to move, when possible;
 2471 negative means move back to previous error messages.
 2472 
 2473 If REPARSE is non-nil, reparse the output log.
 2474 
 2475 If the file occurs in an included file, the file is loaded (if not
 2476 already in an Emacs buffer) and the cursor is placed at the error."
 2477   (let ((old-buffer (current-buffer))
 2478     (default-major-mode major-mode)
 2479     max-index item)
 2480 
 2481     ;; Switch to the output buffer.
 2482     (with-current-buffer (TeX-active-buffer)
 2483       (if reparse
 2484       (TeX-parse-reset reparse))
 2485       (if TeX-parse-all-errors
 2486       (progn
 2487         (setq arg (or arg 1)
 2488           max-index (length TeX-error-list))
 2489         ;; This loop is needed to skip ignored warnings, when
 2490         ;; `TeX-suppress-ignored-warnings' is non-nil and there are ignore
 2491         ;; warnings.
 2492         (while (null (zerop arg))
 2493           (setq TeX-error-last-visited
 2494             ;; Increase or decrese `TeX-error-last-visited' depending on
 2495             ;; the sign of `arg'.  Note: `signum' is a function from
 2496             ;; `cl' library, do not be tempted to use it.
 2497             (if (> arg 0)
 2498             (1+ TeX-error-last-visited)
 2499               (1- TeX-error-last-visited))
 2500             item (nth TeX-error-last-visited TeX-error-list))
 2501           ;; Increase or decrease `arg' only if the warning isn't to be
 2502           ;; skipped.
 2503           (unless (TeX-error-list-skip-warning-p (nth 0 item) (nth 10 item))
 2504         ;; Note: `signum' is a function from `cl' library, do not be
 2505         ;; tempted to use it.
 2506         (setq arg (if (> arg 0)
 2507                   (1- arg)
 2508                 (1+ arg)))))
 2509         (if (< TeX-error-last-visited -1)
 2510         (setq TeX-error-last-visited -1))
 2511         (cond ((or (null item)
 2512                (< TeX-error-last-visited 0))
 2513            (if (> TeX-error-last-visited max-index)
 2514                (setq TeX-error-last-visited max-index))
 2515            (message "No more errors.")
 2516            (beep)
 2517            (TeX-pop-to-buffer old-buffer))
 2518           (t
 2519            (apply #'TeX-find-display-help item))))
 2520 
 2521     (goto-char TeX-error-point)
 2522     (TeX-parse-error old-buffer)))))
 2523 
 2524 ;;; - Parsing (La)TeX
 2525 
 2526 (defvar TeX-translate-location-hook nil
 2527   "List of functions to be called before showing an error or warning.
 2528 
 2529 You might want to examine and modify the free variables `file',
 2530 `offset', `line', `string', `error', and `context' from this hook.")
 2531 
 2532 ;; `ignore' flag should be the always the last one in the list of information
 2533 ;; for each error/warning, because it can be set within `TeX-warning' by a
 2534 ;; custom function taking as argument all information present in
 2535 ;; `TeX-error-list' but `ignore', see `TeX-ignore-warnings'.
 2536 (defvar TeX-error-list nil
 2537   "List of warnings and errors.
 2538 
 2539 Each element of the list is a list of information for a specific
 2540 error or warning.  This is the structure of each element:
 2541  *  0: type (error, warning, bad-box)
 2542  *  1: file
 2543  *  2: line
 2544  *  3: message of the error or warning
 2545  *  4: offset
 2546  *  5: context, to be displayed in the help window
 2547  *  6: string to search in the buffer, in order to find location
 2548        of the error or warning
 2549  *  7: for warnings referring to multiple lines (e.g. bad boxes),
 2550        the last line mentioned in the warning message
 2551  *  8: t if it is a bad-box, nil otherwise
 2552  *  9: value of `TeX-error-point'
 2553  * 10: whether the warning should be ignored
 2554 
 2555 This variable is intended to be set only in output buffer so it
 2556 will be shared among all files of the same document.")
 2557 (make-variable-buffer-local 'TeX-error-list)
 2558 
 2559 (defcustom TeX-parse-all-errors t
 2560   "Whether to automatically collect all warning and errors after running TeX.
 2561 
 2562 If t, it makes it possible to use `TeX-previous-error' with TeX
 2563 commands."
 2564   :group 'TeX-command
 2565   :type 'boolean)
 2566 
 2567 (defun TeX-parse-all-errors ()
 2568   "Parse TeX output buffer to collect all warnings and errors."
 2569   ;; Reset error list.
 2570   (setq TeX-error-list nil)
 2571   (save-excursion
 2572     (goto-char (point-min))
 2573     (while (TeX-parse-error nil t)))
 2574   ;; Reset last visited error.
 2575   (setq TeX-error-last-visited -1))
 2576 
 2577 (defun TeX-parse-error (old &optional store)
 2578   "Goto next error.  Pop to OLD buffer if no more errors are found.
 2579 
 2580 If the optional argument STORE is non-nil, the function will
 2581 store the found warning or error in `TeX-error-list' instead of
 2582 displaying the issue.
 2583 
 2584 Return non-nil if an error or warning is found."
 2585   (let ((regexp
 2586      (concat
 2587       ;; TeX error
 2588       "^\\(!\\|\\(.*?\\):[0-9]+:\\) \\|"
 2589       ;; New file
 2590       "(\n?\\([^\n()]+\\)\\|"
 2591       ;; End of file.
 2592       "\\()\\)\\|"
 2593       ;; Hook to change line numbers
 2594       " !\\(?:offset(\\([---0-9]+\\))\\|"
 2595       ;; Hook to change file name
 2596       "name(\\([^)]+\\))\\)\\|"
 2597       ;; Start of LaTeX bad box
 2598       "^\\(\\(?:Overfull\\|Underfull\\|Tight\\|Loose\\) "
 2599       ;;   Horizontal bad box
 2600       "\\(?:\\\\hbox.* at lines? [0-9]+\\(?:--[0-9]+\\)?$\\|"
 2601       ;;   Vertical bad box.  See also `TeX-warning'.
 2602       "\\\\vbox ([ a-z0-9]+) has occurred while \\\\output is active \\[[^]]+\\]\\)\\)\\|"
 2603       ;; LaTeX warning
 2604       "^\\(" LaTeX-warnings-regexp ".*\\)"))
 2605     (error-found nil))
 2606     (while
 2607     (cond
 2608      ((null
 2609        (re-search-forward regexp nil t))
 2610       ;; No more errors.
 2611       (unless store
 2612         (message "No more errors.")
 2613         (beep)
 2614         (TeX-pop-to-buffer old))
 2615       nil)
 2616      ;; TeX error
 2617      ((match-beginning 1)
 2618       (when (match-beginning 2)
 2619         (unless TeX-error-file
 2620           (push nil TeX-error-file)
 2621           (push nil TeX-error-offset))
 2622         (unless (car TeX-error-offset)
 2623           (rplaca TeX-error-file (TeX-match-buffer 2))))
 2624       (setq error-found t)
 2625       (if (looking-at "Preview ")
 2626           t
 2627         (TeX-error store)
 2628         nil))
 2629      ;; LaTeX bad box
 2630      ((match-beginning 7)
 2631       ;; In `TeX-error-list' we collect all warnings, also if they're going
 2632       ;; to be actually skipped.
 2633       (if (or store TeX-debug-bad-boxes)
 2634           (progn
 2635         (setq error-found t)
 2636         (TeX-warning (TeX-match-buffer 7) (match-beginning 7) t store)
 2637         nil)
 2638         (re-search-forward "\r?\n\
 2639 \\(?:.\\{79\\}\r?\n\
 2640 \\)*.*\r?$")
 2641         t))
 2642      ;; LaTeX warning
 2643      ((match-beginning 8)
 2644       ;; In `TeX-error-list' we collect all warnings, also if they're going
 2645       ;; to be actually skipped.
 2646       (if (or store TeX-debug-warnings)
 2647           (progn
 2648         (setq error-found t)
 2649         (TeX-warning (TeX-match-buffer 8) (match-beginning 8) nil store)
 2650         nil)
 2651         t))
 2652 
 2653      ;; New file -- Push on stack
 2654      ((match-beginning 3)
 2655       (let ((file (TeX-match-buffer 3))
 2656         (end (match-end 3)))
 2657         ;; Strip quotation marks and remove newlines if necessary
 2658         (when (or (eq (string-to-char file) ?\")
 2659               (string-match "[ \t\n]" file))
 2660           (setq file (mapconcat #'identity (split-string file "[\"\n]+") "")))
 2661         ;; Polish `file' string
 2662         (setq file
 2663           (let ((string file))
 2664             (setq string
 2665               (if (string-match "\\`[ \t\n\r]+" string)
 2666                   (replace-match "" t t string)
 2667                 string))
 2668             ;; Sometimes `file' is something like
 2669             ;;     "./path/to/file.tex [9] [10 <./path/to/file>] "
 2670             ;; where "[9]" and "[10 <./path/to/file>]" are pages of the
 2671             ;; output file, with path to an included file.  Remove these
 2672             ;; numbers together with whitespaces at the end of the
 2673             ;; string.
 2674             (if (string-match "\\( *\\(\\[[^]]+\\]\\)? *\\)*\\'" string)
 2675             (replace-match "" t t string)
 2676               string)))
 2677         (push file TeX-error-file)
 2678         (push nil TeX-error-offset)
 2679         (goto-char end))
 2680       t)
 2681 
 2682      ;; End of file -- Pop from stack
 2683      ((match-beginning 4)
 2684       (when (> (length TeX-error-file) 0)
 2685         (pop TeX-error-file)
 2686         (pop TeX-error-offset))
 2687       (goto-char (match-end 4))
 2688       t)
 2689 
 2690      ;; Hook to change line numbers
 2691      ((match-beginning 5)
 2692       (setq TeX-error-offset
 2693         (list (string-to-number (TeX-match-buffer 5))))
 2694       t)
 2695 
 2696      ;; Hook to change file name
 2697      ((match-beginning 6)
 2698       (setq TeX-error-file
 2699         (list (TeX-match-buffer 6)))
 2700       t)))
 2701     error-found))
 2702 
 2703 (defun TeX-find-display-help (type file line error offset context string
 2704                    line-end _bad-box error-point _ignore)
 2705   "Find the error and display the help.
 2706 
 2707 For a description of arguments, see `TeX-error-list'.  IGNORE
 2708 value is not used here."
 2709   ;; Go back to TeX-buffer
 2710   (let ((runbuf (TeX-active-buffer))
 2711     (master (with-current-buffer TeX-command-buffer
 2712           (expand-file-name (TeX-master-file))))
 2713     (command-buffer TeX-command-buffer)
 2714     error-file-buffer start)
 2715     (run-hooks 'TeX-translate-location-hook)
 2716 
 2717     (if file
 2718     (progn
 2719       (setq error-file-buffer
 2720         (find-file
 2721          (expand-file-name file (file-name-directory master))))
 2722       ;; Set the value of `TeX-command-buffer' in the next file with an
 2723       ;; error to be displayed to the value it has in the current buffer.
 2724       (with-current-buffer error-file-buffer
 2725         (set (make-local-variable 'TeX-command-buffer) command-buffer))
 2726 
 2727       ;; Find the location of the error or warning.
 2728       (when line
 2729         (goto-char (point-min))
 2730         (forward-line (+ offset line -1))
 2731         (cond
 2732          ;; Error.
 2733          ((equal type 'error)
 2734           (if (not (string= string " "))
 2735           (search-forward string nil t)))
 2736          ;; Warning or bad box.
 2737          (t
 2738           (beginning-of-line 0)
 2739           (setq start (point))
 2740           (goto-char (point-min))
 2741           (forward-line (+ offset line-end -1))
 2742           (end-of-line)
 2743           (when string
 2744         (search-backward string start t)
 2745         (search-forward string nil t))))))
 2746       ;; When the file cannot be determined stay here but issue a warning.
 2747       (message (concat "Could not determine file for "
 2748                (cond ((equal type 'error) "error")
 2749                  (t "warning"))))
 2750       (beep))
 2751 
 2752     ;; Display the help.
 2753     (cond ((eq TeX-display-help 'expert)
 2754        (TeX-pop-to-buffer runbuf nil t)
 2755        (goto-char error-point)
 2756        (TeX-pop-to-buffer error-file-buffer nil t))
 2757       (TeX-display-help
 2758        (TeX-help-error
 2759         error
 2760         (if (equal type 'warning) (concat "\n" context) context)
 2761         runbuf type))
 2762       (t
 2763        (message (concat "! " error))))))
 2764 
 2765 (defun TeX-error (&optional store)
 2766   "Display an error.
 2767 
 2768 If optional argument STORE is non-nil, store the error
 2769 information in `TeX-error-list' instead of displaying the error."
 2770 
 2771   (let* ( ;; We need the error message to show the user.
 2772      (error (progn
 2773           (re-search-forward "\\(.*\\)")
 2774           (TeX-match-buffer 1)))
 2775 
 2776      ;; And the context for the help window.
 2777      (context-start (point))
 2778      context-available
 2779 
 2780      ;; And the line number to position the cursor.
 2781      (line (cond
 2782         ;; regular style
 2783         ((re-search-forward "l\\.\\([0-9]+\\)" nil t)
 2784          (setq context-available t)
 2785          (string-to-number (TeX-match-buffer 1)))
 2786         ;; file:line:error style
 2787         ((save-excursion
 2788            (re-search-backward ":\\([0-9]+\\): "
 2789                        (line-beginning-position) t))
 2790          (string-to-number (TeX-match-buffer 1)))
 2791         ;; nothing found
 2792         (t 1)))
 2793 
 2794      ;; And a string of the context to search for.
 2795      (string (progn
 2796            (beginning-of-line)
 2797            (re-search-forward " \\(\\([^ \t]*$\\)\\|\\($\\)\\)")
 2798            (TeX-match-buffer 1)))
 2799 
 2800      ;; And we have now found to the end of the context.
 2801      (context (if context-available
 2802               (buffer-substring context-start (progn (forward-line 1)
 2803                                  (end-of-line)
 2804                                  (point)))
 2805             ;; There is no real context available, so we
 2806             ;; simply show the line with the error message.
 2807             (buffer-substring (1- (line-beginning-position))
 2808                       context-start)))
 2809      ;; We may use these in another buffer.
 2810      (offset (or (car TeX-error-offset) 0))
 2811      (file (car TeX-error-file))
 2812      info-list)
 2813 
 2814     ;; Remember where we was.
 2815     (setq TeX-error-point (point)
 2816       info-list (list 'error file line error offset context string nil nil
 2817               TeX-error-point nil))
 2818     (if store
 2819     ;; Store the error information.
 2820     (add-to-list 'TeX-error-list info-list t)
 2821       ;; Find the error point and display the help.
 2822       (apply #'TeX-find-display-help info-list))))
 2823 
 2824 (defun TeX-warning (warning warning-start bad-box &optional store)
 2825   "Display a warning for WARNING.
 2826 
 2827 WARNING-START is the position where WARNING starts.  If BAD-BOX
 2828 is non-nil, the warning refers to a bad-box, otherwise it is a
 2829 generic warning.
 2830 
 2831 If optional argument STORE is non-nil, store the warning
 2832 information in `TeX-error-list' instead of displaying the
 2833 warning."
 2834 
 2835   (let* ( ;; line-string: match 1 is beginning line, match 2 is end line
 2836      (line-string (if bad-box
 2837               "at lines? \\([0-9]*\\)\\(?:--\\([0-9]*\\)\\)?"
 2838             "on input line \\([0-9]*\\)\\."))
 2839      ;; word-string: match 1 is the word
 2840      (word-string (if bad-box "[][\\W() ---]\\(\\w+\\)[][\\W() ---]*$"
 2841             ;; Match "ref" in both "Reference `ref' on page NN
 2842             ;; undefined" and "Citation 'ref' on page NN undefined".
 2843             "\\(?:`\\|'\\)\\([-a-zA-Z0-9:]+\\)'"))
 2844 
 2845      ;; Get error-line (warning).  Don't search before `warning-start' to
 2846      ;; avoid catching completely unrelated line numbers.
 2847      (line (when (save-excursion (re-search-backward line-string
 2848                              warning-start t))
 2849          (string-to-number (TeX-match-buffer 1))))
 2850      ;; If this is a bad box and the warning ends with "...at lines MM--NN"
 2851      ;; we can use "NN" as `line-end', in any other case (including bad
 2852      ;; boxes ending with "...at line NN") just use `line'.
 2853      (line-end (if (and bad-box (match-beginning 2))
 2854                (string-to-number (TeX-match-buffer 2))
 2855              line))
 2856 
 2857      ;; Find the context
 2858      (context-start (progn (cond
 2859                 ((and bad-box (string-match "\\\\hbox" warning))
 2860                  ;; Horizontal bad box
 2861                  (end-of-line))
 2862                 (bad-box
 2863                  ;; Vertical bad box (by exclusion), don't move
 2864                  ;; point.  In the output buffer, unlike in the
 2865                  ;; actual *.log file, these warnings do not end
 2866                  ;; with "...is active []", but in the same line
 2867                  ;; there may be something else, including a new
 2868                  ;; file opened.  Thus, point shouldn't move
 2869                  ;; from the end of the actual bad box warning.
 2870                  ;; This is why the corresponding regexp in
 2871                  ;; `TeX-parse-error' doesn't match everything
 2872                  ;; until the end of the line.
 2873                  nil)
 2874                 (t
 2875                  ;; Generic warning.
 2876                  (beginning-of-line)))
 2877                    (point)))
 2878 
 2879      (context (cond ((string-match LaTeX-warnings-regexp warning)
 2880              ;; The warnings matching `LaTeX-warnings-regexp' are
 2881              ;; emitted by \GenericWarning macro, or macros based on
 2882              ;; it (\ClassWarning, \PackageWarning, etc).  After
 2883              ;; such warnings there is an empty line, just look for
 2884              ;; it to find the end.
 2885              (beginning-of-line)
 2886              (while (null (eolp))
 2887                (forward-line 1))
 2888              (buffer-substring context-start (progn (end-of-line)
 2889                                 (point))))
 2890 
 2891             ((and bad-box (string-match "\\\\vbox" warning))
 2892              ;; Vertical bad boxes don't provide any additional
 2893              ;; information.  In this case, reuse the `warning' as
 2894              ;; `context' and don't move point, so that we avoid
 2895              ;; eating the next line that may contain another
 2896              ;; warning.  See also comment for `context-start'.
 2897              (concat "\n" warning))
 2898 
 2899             (t
 2900              ;; Horizontal bad boxes.
 2901              (forward-line 1)
 2902              (end-of-line)
 2903              (while (equal (current-column) 79)
 2904                (forward-line 1)
 2905                (end-of-line))
 2906              (buffer-substring context-start (point)))))
 2907 
 2908      ;; This is where we want to be.
 2909      (error-point (point))
 2910 
 2911      ;; Now find the error word.
 2912      (string (when (save-excursion
 2913              (re-search-backward word-string context-start t))
 2914            (TeX-match-buffer 1)))
 2915 
 2916      ;; We might use these in another file.
 2917      (offset (or (car TeX-error-offset) 0))
 2918      (file (car TeX-error-file))
 2919      info-list ignore)
 2920 
 2921     ;; Second chance to get line number right.  If `line' is nil, check whether
 2922     ;; the reference to the line number is in `context'.  For example, this is
 2923     ;; the case for warnings emitted with \ClassWarning and \PackageWarning.
 2924     ;; XXX: maybe it suffices to evaluate `line' after `context' above, but I
 2925     ;; don't know if there are cases in which it's important to get `line'
 2926     ;; before `context'.
 2927     (and (null line)
 2928      (string-match line-string context)
 2929      (setq line-end
 2930            (setq line (and (match-beginning 1)
 2931                    (string-to-number (match-string 1 context))))))
 2932 
 2933     ;; This is where we start next time.
 2934     (goto-char error-point)
 2935     (setq TeX-error-point (point))
 2936 
 2937     ;; Explanation of what follows: we add the warning to `TeX-error-list' even
 2938     ;; if it has to be ignored, with a flag specifying whether it is ignored.
 2939     ;; We do so in order to be able to change between "ignore" and "dont-ignore"
 2940     ;; behavior by just looking to the flag, without the need to reparse the
 2941     ;; output log.
 2942 
 2943     ;; Store the list of information about the warning.
 2944     (setq info-list (list (if bad-box 'bad-box 'warning) file line warning
 2945               offset context string line-end bad-box
 2946               TeX-error-point)
 2947       ;; Decide whether it should be ignored.
 2948       ignore (and TeX-ignore-warnings
 2949               (cond
 2950                ((stringp TeX-ignore-warnings)
 2951             (string-match TeX-ignore-warnings warning))
 2952                ((fboundp TeX-ignore-warnings)
 2953             (apply TeX-ignore-warnings info-list))))
 2954       ;; Update `info-list'.
 2955       info-list (append info-list (list ignore)))
 2956 
 2957     (if store
 2958     ;; Store the warning information.
 2959     (add-to-list 'TeX-error-list info-list t)
 2960       ;; Find the warning point and display the help.
 2961       (apply #'TeX-find-display-help info-list))))
 2962 
 2963 ;;; Error Messages
 2964 
 2965 (defcustom TeX-error-description-list
 2966   '(("\\(?:Package Preview Error\\|Preview\\):.*" .
 2967      "The `auctex' option to `preview' should not be applied manually.
 2968 If you see this error message outside of a preview run, either
 2969 you did something too clever, or AUCTeX something too stupid.")
 2970 
 2971     ("Bad \\\\line or \\\\vector argument.*" .
 2972      "The first argument of a \\line or \\vector command, which specifies the
 2973 slope, is illegal\.")
 2974 
 2975     ("Bad math environment delimiter.*" .
 2976      "TeX has found either a math-mode-starting command such as \\[ or \\(
 2977 when it is already in math mode, or else a math-mode-ending command
 2978 such as \\) or \\] while in LR or paragraph mode.  The problem is caused
 2979 by either unmatched math mode delimiters or unbalanced braces\.")
 2980 
 2981     ("Bad use of \\\\\\\\.*" .
 2982      "A \\\\ command appears between paragraphs, where it makes no sense. This
 2983 error message occurs when the \\\\ is used in a centering or flushing
 2984 environment or else in the scope of a centering or flushing
 2985 declaration.")
 2986 
 2987     ("\\\\begin{[^ ]*} ended by \\\\end{[^ ]*}." .
 2988      "LaTeX has found an \\end command that doesn't match the corresponding
 2989 \\begin command. You probably misspelled the environment name in the
 2990 \\end command, have an extra \\begin, or else forgot an \\end.")
 2991 
 2992     ("Can be used only in preamble." .
 2993      "LaTeX has encountered, after the \\begin{document}, one of the
 2994 following commands that should appear only in the preamble:
 2995 \\documentclass, \\nofiles, \\includeonly, \\makeindex, or
 2996 \\makeglossary.  The error is also caused by an extra \\begin{document}
 2997 command.")
 2998 
 2999     ("Command name [^ ]* already used.*" .
 3000      "You are using \\newcommand, \\newenvironment, \\newlength, \\newsavebox,
 3001 or \\newtheorem to define a command or environment name that is
 3002 already defined, or \\newcounter to define a counter that already
 3003 exists. (Defining an environment named gnu automatically defines the
 3004 command \\gnu.) You'll have to choose a new name or, in the case of
 3005 \\newcommand or \\newenvironment, switch to the \\renew ...  command.")
 3006 
 3007     ("Counter too large." .
 3008      "1. Some object that is numbered with letters, probably an item in a
 3009 enumerated list, has received a number greater than 26. Either you're
 3010 making a very long list or you've been resetting counter values.
 3011 
 3012 2. Footnotes are being ``numbered'' with letters or footnote symbols
 3013 and LaTeX has run out of letters or symbols. This is probably caused
 3014 by too many \\thanks commands.")
 3015 
 3016     ("Environment [^ ]* undefined." .
 3017      "LaTeX has encountered a \\begin command for a nonexistent environment.
 3018 You probably misspelled the environment name. ")
 3019 
 3020     ("Float(s) lost." .
 3021      "You put a figure or table environment or a \\marginpar command inside a
 3022 parbox---either one made with a minipage environment or \\parbox
 3023 command, or one constructed by LaTeX in making a footnote, figure,
 3024 etc. This is an outputting error, and the offending environment or
 3025 command may be quite a way back from the point where LaTeX discovered
 3026 the problem. One or more figures, tables, and/or marginal notes have
 3027 been lost, but not necessarily the one that caused the error.")
 3028 
 3029     ("Illegal character in array arg." .
 3030      "There is an illegal character in the argument of an array or tabular
 3031 environment, or in the second argument of a \\multicolumn command.")
 3032 
 3033     ("Missing \\\\begin{document}." .
 3034      "LaTeX produced printed output before encountering a \\begin{document}
 3035 command. Either you forgot the \\begin{document} command or there is
 3036 something wrong in the preamble. The problem may be a stray character
 3037 or an error in a declaration---for example, omitting the braces around
 3038 an argument or forgetting the \\ in a command name.")
 3039 
 3040     ("Missing p-arg in array arg.*" .
 3041      "There is a p that is not followed by an expression in braces in the
 3042 argument of an array or tabular environment, or in the second argument
 3043 of a \\multicolumn command.")
 3044 
 3045     ("Missing @-exp in array arg." .
 3046      "There is an @ character not followed by an @-expression in the
 3047 argument of an array or tabular environment, or in the second argument
 3048 of a \\multicolumn command.")
 3049 
 3050     ("No such counter." .
 3051      "You have specified a nonexistent counter in a \\setcounter or
 3052 \\addtocounter command. This is probably caused by a simple typing
 3053 error.  However, if the error occurred while a file with the extension
 3054 aux is being read, then you probably used a \\newcounter command
 3055 outside the preamble.")
 3056 
 3057     ("Not in outer par mode." .
 3058      "You had a figure or table environment or a \\marginpar command in math
 3059 mode or inside a parbox.")
 3060 
 3061     ("\\\\pushtabs and \\\\poptabs don't match." .
 3062      "LaTeX found a \\poptabs with no matching \\pushtabs, or has come to the
 3063 \\end{tabbing} command with one or more unmatched \\pushtabs commands.")
 3064 
 3065     ("Something's wrong--perhaps a missing \\\\item." .
 3066      "The most probable cause is an omitted \\item command in a list-making
 3067 environment. It is also caused by forgetting the argument of a
 3068 thebibliography environment.")
 3069 
 3070     ("Tab overflow." .
 3071      "A \\= command has exceeded the maximum number of tab stops that LaTeX
 3072 permits.")
 3073 
 3074     ("There's no line here to end." .
 3075      "A \\newline or \\\\ command appears between paragraphs, where it makes no
 3076 sense. If you're trying to ``leave a blank line'', use a \\vspace
 3077 command.")
 3078 
 3079     ("This may be a LaTeX bug." .
 3080      "LaTeX has become thoroughly confused. This is probably due to a
 3081 previously detected error, but it is possible that you have found an
 3082 error in LaTeX itself. If this is the first error message produced by
 3083 the input file and you can't find anything wrong, save the file and
 3084 contact the person listed in your Local Guide.")
 3085 
 3086     ("Too deeply nested." .
 3087      "There are too many list-making environments nested within one another.
 3088 How many levels of nesting are permitted may depend upon what computer
 3089 you are using, but at least four levels are provided, which should be
 3090 enough.")
 3091 
 3092     ("Too many unprocessed floats." .
 3093      "While this error can result from having too many \\marginpar commands
 3094 on a page, a more likely cause is forcing LaTeX to save more figures
 3095 and tables than it has room for.  When typesetting its continuous
 3096 scroll, LaTeX saves figures and tables separately and inserts them as
 3097 it cuts off pages. This error occurs when LaTeX finds too many figure
 3098 and/or table environments before it is time to cut off a page, a
 3099 problem that is solved by moving some of the environments farther
 3100 towards the end of the input file. The error can also be caused by a
 3101 ``logjam''---a figure or table that cannot be printed causing others
 3102 to pile up behind it, since LaTeX will not print figures or tables out
 3103 of order. The jam can be started by a figure or table that either is
 3104 too large to fit on a page or won't fit where its optional placement
 3105 argument says it must go. This is likely to happen if the argument
 3106 does not contain a p option.")
 3107 
 3108     ("Undefined tab position." .
 3109      "A \\>, \\+, \\-, or \\< command is trying to go to a nonexistent tab
 3110 position---one not defined by a \\= command.")
 3111 
 3112     ("\\\\< in mid line." .
 3113      "A \\< command appears in the middle of a line in a tabbing environment.
 3114 This command should come only at the beginning of a line.")
 3115 
 3116     ("Double subscript." .
 3117      "There are two subscripts in a row in a mathematical
 3118 formula---something like x_{2}_{3}, which makes no sense.")
 3119 
 3120     ("Double superscript." .
 3121      "There are two superscripts in a row in a mathematical
 3122 formula---something like x^{2}^{3}, which makes no sense.")
 3123 
 3124     ("Extra alignment tab has been changed to \\\\cr." .
 3125      "There are too many separate items (column entries) in a single row of
 3126 an array or tabular environment. In other words, there were too many &
 3127 's before the end of the row. You probably forgot the \\\\ at the end of
 3128 the preceding row.")
 3129 
 3130     ("Extra \\}, or forgotten \\$." .
 3131      "The braces or math mode delimiters don't match properly. You probably
 3132 forgot a {, \\[, \\(, or $.")
 3133 
 3134     ("Font [^ ]* not loaded: Not enough room left." .
 3135      "The document uses more fonts than TeX has room for. If different parts
 3136 of the document use different fonts, then you can get around the
 3137 problem by processing it in parts.")
 3138 
 3139     ("I can't find file `.*'." .
 3140      "TeX can't find a file that it needs. If the name of the missing file
 3141 has the extension tex, then it is looking for an input file that you
 3142 specified---either your main file or another file inserted with an
 3143 \\input or \\include command. If the missing file has the extension sty
 3144 , then you have specified a nonexistent document style or style
 3145 option.")
 3146 
 3147     ("Illegal parameter number in definition of .*" .
 3148      "This is probably caused by a \\newcommand, \\renewcommand,
 3149 \\newenvironment, or \\renewenvironment command in which a # is used
 3150 incorrectly.  A # character, except as part of the command name \\#,
 3151 can be used only to indicate an argument parameter, as in #2, which
 3152 denotes the second argument. This error is also caused by nesting one
 3153 of the above four commands inside another, or by putting a parameter
 3154 like #2 in the last argument of a \\newenvironment or \\renewenvironment
 3155 command.")
 3156 
 3157     ("Illegal unit of measure ([^ ]* inserted)." .
 3158      "If you just got a
 3159 
 3160       ! Missing number, treated as zero.
 3161 
 3162 error, then this is part of the same problem.  If not, it means that
 3163 LaTeX was expecting a length as an argument and found a number
 3164 instead.  The most common cause of this error is writing 0 instead of
 3165 something like 0in for a length of zero, in which case typing return
 3166 should result in correct output. However, the error can also be caused
 3167 by omitting a command argument.")
 3168 
 3169     ("Misplaced alignment tab character \\&." .
 3170      "The special character &, which should be used only to separate items
 3171 in an array or tabular environment, appeared in ordinary text. You
 3172 probably meant to type \\&.")
 3173 
 3174     ("Missing control sequence inserted." .
 3175      "This is probably caused by a \\newcommand, \\renewcommand, \\newlength,
 3176 or \\newsavebox command whose first argument is not a command name.")
 3177 
 3178     ("Missing number, treated as zero." .
 3179      "This is usually caused by a LaTeX command expecting but not finding
 3180 either a number or a length as an argument. You may have omitted an
 3181 argument, or a square bracket in the text may have been mistaken for
 3182 the beginning of an optional argument. This error is also caused by
 3183 putting \\protect in front of either a length command or a command such
 3184 as \\value that produces a number.")
 3185 
 3186     ("Missing [{}] inserted." .
 3187      "TeX has become confused. The position indicated by the error locator
 3188 is probably beyond the point where the incorrect input is.")
 3189 
 3190     ("Missing \\$ inserted." .
 3191      "TeX probably found a command that can be used only in math mode when
 3192 it wasn't in math mode.  Remember that unless stated otherwise, all
 3193 all the commands of Section 3.3 in LaTeX Book (Lamport) can be used
 3194 only in math mode. TeX is not in math mode when it begins processing
 3195 the argument of a box-making command, even if that command is inside a
 3196 math environment. This error also occurs if TeX encounters a blank
 3197 line when it is in math mode.")
 3198 
 3199     ("Not a letter." .
 3200      "Something appears in the argument of a \\hyphenation command that
 3201 doesn't belong there.")
 3202 
 3203     ("Paragraph ended before [^ ]* was complete." .
 3204      "A blank line occurred in a command argument that shouldn't contain
 3205 one. You probably forgot the right brace at the end of an argument.")
 3206 
 3207     ("\\\\[^ ]*font [^ ]* is undefined .*" .
 3208      "These errors occur when an uncommon font is used in math mode---for
 3209 example, if you use a \\sc command in a formula inside a footnote,
 3210 calling for a footnote-sized small caps font.  This problem is solved
 3211 by using a \\load command.")
 3212 
 3213     ("Font .* not found." .
 3214      "You requested a family/series/shape/size combination that is totally
 3215 unknown.  There are two cases in which this error can occur:
 3216   1) You used the \\size macro to select a size that is not available.
 3217   2) If you did not do that, go to your local `wizard' and
 3218      complain fiercely that the font selection tables are corrupted!")
 3219 
 3220     ("TeX capacity exceeded, sorry .*" .
 3221      "TeX has just run out of space and aborted its execution. Before you
 3222 panic, remember that the least likely cause of this error is TeX not
 3223 having the capacity to process your document.  It was probably an
 3224 error in your input file that caused TeX to run out of room. The
 3225 following discussion explains how to decide whether you've really
 3226 exceeded TeX's capacity and, if so, what to do. If the problem is an
 3227 error in the input, you may have to use the divide and conquer method
 3228 described previously to locate it. LaTeX seldom runs out of space on a
 3229 short input file, so if running it on the last few pages before the
 3230 error indicator's position still produces the error, then there's
 3231 almost certainly something wrong in the input file.
 3232 
 3233 The end of the error indicator tells what kind of space TeX ran out
 3234 of. The more common ones are listed below, with an explanation of
 3235 their probable causes.
 3236 
 3237 buffer size
 3238 ===========
 3239 Can be caused by too long a piece of text as the argument
 3240 of a sectioning, \\caption, \\addcontentsline, or \\addtocontents
 3241 command. This error will probably occur when the \\end{document} is
 3242 being processed, but it could happen when a \\tableofcontents,
 3243 \\listoffigures, or \\listoftables command is executed. To solve this
 3244 problem, use a shorter optional argument. Even if you're producing a
 3245 table of contents or a list of figures or tables, such a long entry
 3246 won't help the reader.
 3247 
 3248 exception dictionary
 3249 ====================
 3250 You have used \\hyphenation commands to give TeX
 3251 more hyphenation information than it has room for. Remove some of the
 3252 less frequently used words from the \\hyphenation commands and insert
 3253 \\- commands instead.
 3254 
 3255 hash size
 3256 =========
 3257 Your input file defines too many command names and/or uses
 3258 too many cross-ref- erencing labels.
 3259 
 3260 input stack size
 3261 ================
 3262 This is probably caused by an error in a command
 3263 definition. For example, the following command makes a circular
 3264 definition, defining \\gnu in terms of itself:
 3265 
 3266       \\newcommand{\\gnu}{a \\gnu} % This is wrong!
 3267 
 3268 When TeX encounters this \\gnu command, it will keep chasing its tail
 3269 trying to figure out what \\gnu should produce, and eventually run out
 3270 of ``input stack''.
 3271 
 3272 main memory size
 3273 ================
 3274 This is one kind of space that TeX can run out of when processing a
 3275 short file. There are three ways you can run TeX out of main memory
 3276 space: (1) defining a lot of very long, complicated commands, (2)
 3277 making an index or glossary and having too many \\index or \\glossary
 3278 commands on a single page, and (3) creating so complicated a page of
 3279 output that TeX can't hold all the information needed to generate it.
 3280 The solution to the first two problems is obvious: define fewer
 3281 commands or use fewer \\index and \\glossary commands. The third problem
 3282 is nastier. It can be caused by large tabbing, tabular, array, and
 3283 picture environments. TeX's space may also be filled up with figures
 3284 and tables waiting for a place to go.  To find out if you've really
 3285 exceeded TeX's capacity in this way, put a \\clearpage command in your
 3286 input file right before the place where TeX ran out of room and try
 3287 running it again. If it doesn't run out of room with the \\clearpage
 3288 command there, then you did exceed TeX's capacity.  If it still runs
 3289 out of room, then there's probably an error in your file.  If TeX is
 3290 really out of room, you must give it some help. Remember that TeX
 3291 processes a complete paragraph before deciding whether to cut the
 3292 page. Inserting a \\newpage command in the middle of the paragraph,
 3293 where TeX should break the page, may save the day by letting TeX write
 3294 the current page before processing the rest of the paragraph. (A
 3295 \\pagebreak command won't help.) If the problem is caused by
 3296 accumulated figures and tables, you can try to prevent them from
 3297 accumulating---either by moving them further towards the end of the
 3298 document or by trying to get them to come out sooner.  If you are
 3299 still writing the document, simply add a \\clearpage command and forget
 3300 about the problem until you're ready to produce the final version.
 3301 Changes to the input file are likely to make the problem go away.
 3302 
 3303 pool size
 3304 =========
 3305 You probably used too many cross-ref-erencing \\labels and/or defined
 3306 too many new command names. More precisely, the labels and command
 3307 names that you define have too many characters, so this problem can be
 3308 solved by using shorter names. However, the error can also be caused
 3309 by omitting the right brace that ends the argument of either a counter
 3310 command such as \\setcounter, or a \\newenvironment or \\newtheorem
 3311 command.
 3312 
 3313 save size
 3314 =========
 3315 This occurs when commands, environments, and the scopes of
 3316 declarations are nested too deeply---for example, by having the
 3317 argument of a \\multiput command contain a picture environment that in
 3318 turn has a \\footnotesize declaration whose scope contains a \\multiput
 3319 command containing a ....")
 3320 
 3321     ("Text line contains an invalid character." .
 3322      "The input contains some strange character that it shouldn't. A mistake
 3323 when creating the file probably caused your text editor to insert this
 3324 character. Exactly what could have happened depends upon what text
 3325 editor you used. If examining the input file doesn't reveal the
 3326 offending character, consult the Local Guide for suggestions.")
 3327 
 3328     ("Undefined control sequence."   .
 3329      "TeX encountered an unknown command name. You probably misspelled the
 3330 name. If this message occurs when a LaTeX command is being processed,
 3331 the command is probably in the wrong place---for example, the error
 3332 can be produced by an \\item command that's not inside a list-making
 3333 environment. The error can also be caused by a missing \\documentclass
 3334 command.")
 3335 
 3336     ("Use of [^ ]* doesn't match its definition." .
 3337      "It's probably one of the picture-drawing commands, and you have used
 3338 the wrong syntax for specifying an argument. If it's \\@array that
 3339 doesn't match its definition, then there is something wrong in an
 3340 @-expression in the argument of an array or tabular
 3341 environment---perhaps a fragile command that is not \\protect'ed.")
 3342 
 3343     ("You can't use `macro parameter character \\#' in [^ ]* mode." .
 3344      "The special character # has appeared in ordinary text. You probably
 3345 meant to type \\#.")
 3346 
 3347     ("Overfull \\\\hbox .*" .
 3348      "Because it couldn't find a good place for a line break, TeX put more
 3349 on this line than it should.")
 3350 
 3351     ("Overfull \\\\vbox .*" .
 3352      "Because it couldn't find a good place for a page break, TeX put more
 3353 on the page than it should. ")
 3354 
 3355     ("Underfull \\\\hbox .*" .
 3356      "Check your output for extra vertical space.  If you find some, it was
 3357 probably caused by a problem with a \\\\ or \\newline command---for
 3358 example, two \\\\ commands in succession. This warning can also be
 3359 caused by using the sloppypar environment or \\sloppy declaration, or
 3360 by inserting a \\linebreak command.")
 3361 
 3362     ("Underfull \\\\vbox .*" .
 3363      "TeX could not find a good place to break the page, so it produced a
 3364 page without enough text on it. ")
 3365 
 3366     ;; New list items should be placed here
 3367     ;;
 3368     ;; ("err-regexp" . "context")
 3369     ;;
 3370     ;; the err-regexp item should match anything
 3371 
 3372     (".*" . "No help available"))   ; end definition
 3373   "A list of the form (\"err-regexp\" . \"context\") used by function
 3374 `TeX-help-error' to display help-text on an error message or warning.
 3375 err-regexp should be a regular expression matching the error message
 3376 given from TeX/LaTeX, and context should be some lines describing that
 3377 error."
 3378   :group 'TeX-output
 3379   :type '(repeat (cons :tag "Entry"
 3380                (regexp :tag "Match")
 3381                (string :format "Description:\n%v"))))
 3382 
 3383 ;;; - Help
 3384 
 3385 (defgroup TeX-error-description-faces nil
 3386   "Faces used in error descriptions."
 3387   :prefix "TeX-error-description-"
 3388   :group 'TeX-output)
 3389 
 3390 (defface TeX-error-description-error
 3391   ;; This is the same as `error' face in latest GNU Emacs versions.
 3392   '((((class color) (min-colors 88) (background light))
 3393      :foreground "Red1" :weight bold)
 3394     (((class color) (min-colors 88) (background dark))
 3395      :foreground "Pink" :weight bold)
 3396     (((class color) (min-colors 16) (background light))
 3397      :foreground "Red1" :weight bold)
 3398     (((class color) (min-colors 16) (background dark))
 3399      :foreground "Pink" :weight bold)
 3400     (((class color) (min-colors 8))
 3401      :foreground "red" :weight bold)
 3402     (t (:inverse-video t :weight bold)))
 3403   "Face for \"Error\" string in error descriptions.")
 3404 
 3405 (defface TeX-error-description-warning
 3406   ;; This is the same as `warning' face in latest GNU Emacs versions.
 3407   '((((class color) (min-colors 16)) :foreground "DarkOrange" :weight bold)
 3408     (((class color)) :foreground "yellow" :weight bold))
 3409   "Face for \"Warning\" string in error descriptions.")
 3410 
 3411 (defface TeX-error-description-tex-said
 3412   ;; This is the same as `font-lock-function-name-face' face in latest GNU
 3413   ;; Emacs versions.
 3414   '((((class color) (min-colors 88) (background light))
 3415      :foreground "Blue1")
 3416     (((class color) (min-colors 88) (background dark))
 3417      :foreground "LightSkyBlue")
 3418     (((class color) (min-colors 16) (background light))
 3419      :foreground "Blue")
 3420     (((class color) (min-colors 16) (background dark))
 3421      :foreground "LightSkyBlue")
 3422     (((class color) (min-colors 8))
 3423      :foreground "blue" :weight bold)
 3424     (t (:inverse-video t :weight bold)))
 3425   "Face for \"TeX said\" string in error descriptions.")
 3426 
 3427 (defface TeX-error-description-help
 3428   '((t (:inherit TeX-error-description-tex-said)))
 3429   "Face for \"Help\" string in error descriptions.")
 3430 
 3431 (defun TeX-help-error (error output runbuffer type)
 3432   "Print ERROR in context OUTPUT from RUNBUFFER in another window.
 3433 TYPE is a symbol specifing if ERROR is a real error, a warning or
 3434 a bad box."
 3435 
 3436   (let ((old-buffer (current-buffer))
 3437     (log-file (with-current-buffer runbuffer
 3438             (with-current-buffer TeX-command-buffer
 3439               (expand-file-name (TeX-active-master "log")))))
 3440     (TeX-error-pointer 0))
 3441 
 3442     ;; Find help text entry.
 3443     (while (not (string-match (car (nth TeX-error-pointer
 3444                     TeX-error-description-list))
 3445                   error))
 3446       (setq TeX-error-pointer (+ TeX-error-pointer 1)))
 3447 
 3448     (TeX-pop-to-buffer (get-buffer-create "*TeX Help*") nil t)
 3449     (let ((inhibit-read-only t))
 3450       (erase-buffer)
 3451       (insert
 3452        (cond
 3453     ((equal type 'error)
 3454      (propertize "ERROR" 'font-lock-face 'TeX-error-description-error))
 3455     ((equal type 'warning)
 3456      (propertize "WARNING" 'font-lock-face 'TeX-error-description-warning))
 3457     ((equal type 'bad-box)
 3458      (propertize "BAD BOX" 'font-lock-face 'TeX-error-description-warning)))
 3459        ": " error
 3460        (propertize "\n\n--- TeX said ---" 'font-lock-face
 3461            'TeX-error-description-tex-said)
 3462        output
 3463        (propertize "\n--- HELP ---\n" 'font-lock-face
 3464            'TeX-error-description-help)
 3465        (let ((help (cdr (nth TeX-error-pointer
 3466                  TeX-error-description-list))))
 3467      (save-excursion
 3468        (if (and (= (1+ TeX-error-pointer)
 3469                (length TeX-error-description-list))
 3470             (let* ((log-buffer (find-buffer-visiting log-file)))
 3471               (if log-buffer
 3472               (progn
 3473                 (set-buffer log-buffer)
 3474                 (revert-buffer t t))
 3475             (setq log-buffer
 3476                   (find-file-noselect log-file))
 3477             (set-buffer log-buffer))
 3478               (auto-save-mode nil)
 3479               (setq buffer-read-only t)
 3480               (goto-char (point-min))
 3481               (search-forward error nil t 1))
 3482             (re-search-forward "^l\\." nil t)
 3483             (re-search-forward "^ [^\n]+$" nil t))
 3484            (let ((start (1+ (point))))
 3485          (forward-char 1)
 3486          (re-search-forward "^$")
 3487          (concat "From the .log file...\n\n"
 3488              (buffer-substring start (point))))
 3489          help)))))
 3490     (goto-char (point-min))
 3491     (TeX-special-mode)
 3492     (TeX-pop-to-buffer old-buffer nil t)))
 3493 
 3494 ;;; Error Overview
 3495 
 3496 (defvar TeX-error-overview-active-buffer nil
 3497   "The active buffer for the current error overview.")
 3498 
 3499 (defvar TeX-error-overview-orig-frame nil
 3500   "Frame from which the error overview has been launched.")
 3501 
 3502 (defvar TeX-error-overview-orig-window nil
 3503   "Window from which the error overview has been launched.")
 3504 
 3505 (defcustom TeX-error-overview-setup nil
 3506   "The frame setup of the error overview.
 3507 
 3508 The possible value is: `separate-frame' (error oveview in a
 3509 separate frame); with a nil value the current frame is used.
 3510 
 3511 If the display does not support multi frame, the current frame
 3512 will be used regardless of the value of this variable."
 3513   :group 'TeX-output
 3514   :type '(choice
 3515           (const :tag "Error overview in separate frame" separate-frame)
 3516           (const :tag "Use current frame" nil)))
 3517 
 3518 (defun TeX-error-overview-setup ()
 3519   "Return the frame setup of the error overview for the current display."
 3520   (and (display-multi-frame-p) TeX-error-overview-setup))
 3521 
 3522 (defun TeX-error-overview-goto-source (&optional button)
 3523   "Go to the error point in the source.
 3524 If optional argument BUTTON is non-nil, go to source associated
 3525 to the selected error."
 3526   (interactive)
 3527   (let ((index (if button (button-get button 'id) (tabulated-list-get-id)))
 3528     item window)
 3529     (if index
 3530     (progn
 3531       ;; Select the source frame/window, if still live.
 3532       (if (TeX-error-overview-setup)
 3533           (if (frame-live-p TeX-error-overview-orig-frame)
 3534           (select-frame TeX-error-overview-orig-frame)
 3535         (error "You have deleted a vital frame---\
 3536 please restart TeX error overview"))
 3537         (if (window-live-p TeX-error-overview-orig-window)
 3538         (select-window TeX-error-overview-orig-window)
 3539           (error "You have deleted a vital window---\
 3540 please restart TeX error overview")))
 3541       ;; Get the error details.
 3542       (with-current-buffer TeX-error-overview-active-buffer
 3543         (setq item (nth index TeX-error-list)
 3544           TeX-error-last-visited index))
 3545       ;; Find the error and display the help.
 3546       (with-current-buffer TeX-command-buffer
 3547         ;; For consistency with `TeX-parse-TeX', use the major mode of
 3548         ;; `TeX-command-buffer' when visiting the error point.
 3549         (let ((default-major-mode major-mode))
 3550           ;; Find the error and display the help.
 3551           (apply #'TeX-find-display-help item)))
 3552       ;; Return to the error overview.
 3553       (if (TeX-error-overview-setup)
 3554           (select-frame TeX-error-overview-frame)
 3555         (if (setq window
 3556               (get-buffer-window TeX-error-overview-buffer-name))
 3557         ;; If error overview window is visible just select it.
 3558         (select-window window)
 3559           ;; Otherwise, split the help window and display the error overview
 3560           ;; near to it.  This should be the only reason for the error
 3561           ;; overview window not being still visible after the beginning of
 3562           ;; the function.
 3563           (select-window
 3564            (get-buffer-window (cond
 3565                    ((eq TeX-display-help 'expert)
 3566                     TeX-error-overview-active-buffer)
 3567                    (TeX-display-help  "*TeX Help*"))))
 3568           (if (window-splittable-p (selected-window) t)
 3569           (split-window-horizontally)
 3570         (split-window-vertically))
 3571           (switch-to-buffer TeX-error-overview-buffer-name))))
 3572       (message "No more errors.")
 3573       (beep))))
 3574 
 3575 (defun TeX-error-overview-make-entries (&optional master-dir active-buffer)
 3576   "Generate the list of errors to be printed using `tabulated-list-entries'.
 3577 Write file names relative to MASTER-DIR when they are not absolute.
 3578 
 3579 ACTIVE-BUFFER is used as buffer from which to extract the list of
 3580 errors.  If nil, defaults to `TeX-error-overview-active-buffer'."
 3581   (with-current-buffer (or active-buffer TeX-error-overview-active-buffer)
 3582     (let ((id 0)
 3583       type file line msg entries)
 3584       (mapc
 3585        (lambda (entry)
 3586      (setq type (nth 0 entry)
 3587            file (nth 1 entry)
 3588            line (nth 2 entry)
 3589            msg  (nth 3 entry))
 3590      ;; Add the entry only if it isn't to be skipped.
 3591      (unless (TeX-error-list-skip-warning-p type (nth 10 entry))
 3592        (push
 3593         (list
 3594          ;; ID.
 3595          id
 3596          (vector
 3597           ;; File.
 3598           (if (stringp file)
 3599           (if (file-name-absolute-p file)
 3600               file
 3601             (file-relative-name file master-dir))
 3602         "")
 3603           ;; Line.
 3604           (if (numberp line)
 3605           (number-to-string line)
 3606         "")
 3607           ;; Type.
 3608           (cond
 3609            ((equal type 'error)
 3610         (propertize "Error" 'font-lock-face 'TeX-error-description-error))
 3611            ((equal type 'warning)
 3612         (propertize "Warning" 'font-lock-face
 3613                 'TeX-error-description-warning))
 3614            ((equal type 'bad-box)
 3615         (propertize "Bad box" 'font-lock-face
 3616                 'TeX-error-description-warning))
 3617            (t
 3618         ""))
 3619           ;; Message.
 3620           (list (if (stringp msg)
 3621             ;; Sometimes, the message can be longer than one line,
 3622             ;; but print here only the first one.
 3623             (progn
 3624               (string-match "^.*" msg)
 3625               (match-string 0 msg))
 3626               "")
 3627             'face 'link
 3628             'follow-link t
 3629             'id id
 3630             'action 'TeX-error-overview-goto-source)))
 3631         entries))
 3632      ;; Increase the `id' counter in any case.
 3633      (setq id (1+ id)))
 3634        TeX-error-list)
 3635       (reverse entries))))
 3636 
 3637 (defun TeX-error-overview-next-error (&optional arg)
 3638   "Move to the next line and find the associated error.
 3639 
 3640 A prefix ARG specifies how many error messages to move; negative
 3641 means move back to previous error messages."
 3642   (interactive "p")
 3643   (if (= (forward-line arg) 0)
 3644       (TeX-error-overview-goto-source)
 3645     ;; If there are lines left to move we are at the beginning or at the end of
 3646     ;; the buffer and there are no more errors.
 3647     (message "No more errors.")
 3648     (beep)))
 3649 
 3650 (defun TeX-error-overview-previous-error (&optional arg)
 3651   "Move to the previous line and find the associated error.
 3652 
 3653 Prefix arg N says how many error messages to move backward (or
 3654 forward, if negative)."
 3655   (interactive "p")
 3656   (TeX-error-overview-next-error (- arg)))
 3657 
 3658 (defun TeX-error-overview-jump-to-source ()
 3659   "Display the help and move point to the error source."
 3660   (interactive)
 3661   (TeX-error-overview-goto-source)
 3662   (pop-to-buffer
 3663    (save-window-excursion
 3664      (select-window TeX-error-overview-orig-window)
 3665      (current-buffer))))
 3666 
 3667 (defun TeX-error-overview-goto-log ()
 3668   "Display the current error in log buffer."
 3669   (interactive)
 3670   (let ((TeX-display-help 'expert))
 3671     (TeX-error-overview-goto-source)))
 3672 
 3673 (defun TeX-error-overview-toggle-debug-bad-boxes ()
 3674   "Run `TeX-toggle-debug-bad-boxes' and update entries list."
 3675   (interactive)
 3676   (TeX-toggle-debug-bad-boxes)
 3677   (setq tabulated-list-entries
 3678     (TeX-error-overview-make-entries
 3679      (with-current-buffer TeX-command-buffer (TeX-master-directory))))
 3680   (tabulated-list-init-header)
 3681   (tabulated-list-print))
 3682 
 3683 (defun TeX-error-overview-toggle-debug-warnings ()
 3684   "Run `TeX-toggle-debug-warnings' and update entries list."
 3685   (interactive)
 3686   (TeX-toggle-debug-warnings)
 3687   (setq tabulated-list-entries
 3688     (TeX-error-overview-make-entries
 3689      (with-current-buffer TeX-command-buffer (TeX-master-directory))))
 3690   (tabulated-list-init-header)
 3691   (tabulated-list-print))
 3692 
 3693 (defun TeX-error-overview-toggle-suppress-ignored-warnings ()
 3694   "Toggle visibility of ignored warnings and update entries list."
 3695   (interactive)
 3696   (TeX-toggle-suppress-ignored-warnings)
 3697   (setq tabulated-list-entries
 3698     (TeX-error-overview-make-entries
 3699      (with-current-buffer TeX-command-buffer (TeX-master-directory))))
 3700   (tabulated-list-init-header)
 3701   (tabulated-list-print))
 3702 
 3703 (defun TeX-error-overview-quit ()
 3704   "Delete the window or the frame of the error overview."
 3705   (interactive)
 3706   (if (TeX-error-overview-setup)
 3707       (delete-frame TeX-error-overview-frame)
 3708     (delete-window))
 3709   (setq TeX-error-overview-orig-frame nil))
 3710 
 3711 (defvar TeX-error-overview-mode-map
 3712   (let ((map (make-sparse-keymap)))
 3713     (define-key map "b"    'TeX-error-overview-toggle-debug-bad-boxes)
 3714     (define-key map "j"    'TeX-error-overview-jump-to-source)
 3715     (define-key map "l"    'TeX-error-overview-goto-log)
 3716     (define-key map "n"    'TeX-error-overview-next-error)
 3717     (define-key map "p"    'TeX-error-overview-previous-error)
 3718     (define-key map "q"    'TeX-error-overview-quit)
 3719     (define-key map "w"    'TeX-error-overview-toggle-debug-warnings)
 3720     (define-key map "x"    'TeX-error-overview-toggle-suppress-ignored-warnings)
 3721     (define-key map "\C-m" 'TeX-error-overview-goto-source)
 3722     map)
 3723   "Local keymap for `TeX-error-overview-mode' buffers.")
 3724 
 3725 (easy-menu-define TeX-error-overview-menu
 3726   TeX-error-overview-mode-map
 3727   "Menu used in TeX error overview mode."
 3728   '("TeX errors"
 3729     ["Next error" TeX-error-overview-next-error
 3730      :help "Jump to the next error"]
 3731     ["Previous error" TeX-error-overview-previous-error
 3732      :help "Jump to the previous error"]
 3733     ["Go to source" TeX-error-overview-goto-source
 3734      :help "Show the error in the source"]
 3735     ["Jump to source" TeX-error-overview-jump-to-source
 3736      :help "Move point to the error in the source"]
 3737     ["Go to log" TeX-error-overview-goto-log
 3738      :help "Show the error in the log buffer"]
 3739     "-"
 3740     ["Debug Bad Boxes" TeX-error-overview-toggle-debug-bad-boxes
 3741      :style toggle :selected TeX-debug-bad-boxes
 3742      :help "Show overfull and underfull boxes"]
 3743     ["Debug Warnings" TeX-error-overview-toggle-debug-warnings
 3744      :style toggle :selected TeX-debug-warnings
 3745      :help "Show warnings"]
 3746     ["Ignore Unimportant Warnings"
 3747      TeX-error-overview-toggle-suppress-ignored-warnings
 3748      :style toggle :selected TeX-suppress-ignored-warnings
 3749      :help "Hide specified warnings"]
 3750     "-"
 3751     ["Quit" TeX-error-overview-quit
 3752      :help "Quit"]))
 3753 
 3754 (defvar TeX-error-overview-list-entries nil
 3755   "List of errors to be used in the error overview.")
 3756 
 3757 (define-derived-mode TeX-error-overview-mode tabulated-list-mode
 3758              "TeX errors"
 3759   "Major mode for listing TeX errors."
 3760   (setq tabulated-list-format [("File" 25 nil)
 3761                                ("Line" 4 nil :right-align t)
 3762                                ("Type" 7 nil)
 3763                                ("Message" 0 nil)]
 3764         tabulated-list-padding 1
 3765         tabulated-list-entries TeX-error-overview-list-entries)
 3766   (tabulated-list-init-header)
 3767   (tabulated-list-print)
 3768   (easy-menu-add TeX-error-overview-menu TeX-error-overview-mode-map))
 3769 
 3770 (defcustom TeX-error-overview-frame-parameters
 3771   '((name . "TeX errors")
 3772     (title . "TeX errors")
 3773     (height . 10)
 3774     (width . 80)
 3775     (top . (- 0))
 3776     (left . (- 0))
 3777     (unsplittable . t)
 3778     (minibuffer . nil)
 3779     (vertical-scroll-bars . t)
 3780     (tool-bar-lines . 0))
 3781   "Parameters of the error overview frame."
 3782   :group 'TeX-output
 3783   :type 'alist
 3784   :options '((name string) (title string) (height integer) (width integer)
 3785          (top integer) (left integer) (unsplittable boolean)
 3786          (minibuffer boolean) (vertical-scroll-bars boolean)
 3787          (tool-bar-lines integer)))
 3788 
 3789 (defcustom TeX-error-overview-open-after-TeX-run nil
 3790   "Whether to open automatically the error overview after running TeX."
 3791   :group 'TeX-output
 3792   :type 'boolean)
 3793 
 3794 (defun TeX-error-overview ()
 3795   "Show an overview of the errors occurred in the last TeX run."
 3796   (interactive)
 3797   ;; Check requirements before start.
 3798   (if (setq TeX-error-overview-active-buffer (TeX-active-buffer))
 3799       ;; `TeX-error-overview-list-entries' is going to be used only as value
 3800       ;; of `tabulated-list-entries' in `TeX-error-overview-mode'.  In
 3801       ;; principle, we don't need `TeX-error-overview-list-entries', but
 3802       ;; `tabulated-list-entries' is buffer-local and we need the list of
 3803       ;; entries before creating the error overview buffer in order to
 3804       ;; decide whether we need to show anything.
 3805       (if (setq TeX-error-overview-list-entries
 3806         (TeX-error-overview-make-entries
 3807          (TeX-master-directory)))
 3808       (progn
 3809         (setq TeX-error-overview-orig-window (selected-window)
 3810           TeX-error-overview-orig-frame
 3811           (window-frame TeX-error-overview-orig-window))
 3812         ;; Create the error overview buffer.  This is
 3813         ;; automatically killed before running TeX commands, so if
 3814         ;; exists it is up-to-date and doesn't need to be
 3815         ;; re-created.
 3816         (unless (get-buffer TeX-error-overview-buffer-name)
 3817           (with-current-buffer
 3818           (get-buffer-create TeX-error-overview-buffer-name)
 3819         (TeX-error-overview-mode)))
 3820         ;; Move point to the line associated to the last visited
 3821         ;; error.
 3822         (with-current-buffer TeX-error-overview-buffer-name
 3823           (goto-char (point-min))
 3824           (forward-line (with-current-buffer
 3825                 TeX-error-overview-active-buffer
 3826                   TeX-error-last-visited))
 3827           ;; Create a new frame for the error overview or display the
 3828           ;; buffer in the same frame, depending on the setup.
 3829           (if (TeX-error-overview-setup)
 3830           (if (frame-live-p TeX-error-overview-frame)
 3831               ;; Do not create a duplicate frame if there is
 3832               ;; already one, just select it.
 3833               (select-frame-set-input-focus
 3834                TeX-error-overview-frame)
 3835             ;; Create a new frame and store its name.
 3836             (select-frame
 3837              (setq TeX-error-overview-frame
 3838                (make-frame
 3839                 TeX-error-overview-frame-parameters)))
 3840             (set-window-buffer (selected-window)
 3841                        TeX-error-overview-buffer-name)
 3842             (set-window-dedicated-p (selected-window) t))
 3843         (TeX-pop-to-buffer TeX-error-overview-buffer-name))))
 3844     (error (concat "No error or warning to show"
 3845                ;; Suggest to display warnings and bad boxes with the
 3846                ;; appropriate key-bindings if there are such
 3847                ;; messages in the output buffer.  Rationale of the
 3848                ;; test: `TeX-error-overview-list-entries' is nil,
 3849                ;; but if `TeX-error-list' is not nil it means that
 3850                ;; there are hidden warnings/bad boxes.
 3851                (when (TeX-process-get-variable (TeX-active-master)
 3852                                'TeX-error-list)
 3853              (format ".  Type `%s' and `%s' to display \
 3854 warnings and bad boxes"
 3855                  (substitute-command-keys
 3856                   "\\<TeX-mode-map>\\[TeX-toggle-debug-warnings]")
 3857                  (substitute-command-keys
 3858                   "\\<TeX-mode-map>\\[TeX-toggle-debug-bad-boxes]"))))))
 3859     (error "No process for this document")))
 3860 
 3861 ;;; Output mode
 3862 
 3863 (define-derived-mode TeX-special-mode special-mode "TeX")
 3864 
 3865 (defvar TeX-output-mode-map
 3866   (let ((map (make-sparse-keymap)))
 3867     (set-keymap-parent map TeX-special-mode-map)
 3868     (define-key map "n" 'TeX-next-error)
 3869     (define-key map "p" 'TeX-previous-error)
 3870     (define-key map "b" 'TeX-toggle-debug-bad-boxes)
 3871     (define-key map "w" 'TeX-toggle-debug-warnings)
 3872     (define-key map "i" (lambda ()
 3873                           (interactive)
 3874                           (with-current-buffer TeX-command-buffer
 3875                             (TeX-interactive-mode (if TeX-interactive-mode -1 1)))))
 3876     (define-key map "s" (lambda ()
 3877                           (interactive)
 3878                           (with-current-buffer TeX-command-buffer
 3879                             (TeX-source-correlate-mode (if TeX-source-correlate-mode -1 1)))))
 3880     map)
 3881   "Keymap for `TeX-output-mode'.")
 3882 
 3883 (define-derived-mode TeX-output-mode TeX-special-mode "TeX Output"
 3884   "Major mode for viewing TeX output.
 3885 \\{TeX-output-mode-map} "
 3886   :syntax-table nil
 3887   (set (make-local-variable 'revert-buffer-function)
 3888        #'TeX-output-revert-buffer)
 3889   ;; special-mode makes it read-only which prevents input from TeX.
 3890   (setq buffer-read-only nil))
 3891 
 3892 (defun TeX-output-revert-buffer (_ignore-auto _noconfirm)
 3893   "Rerun the TeX command which of which this buffer was the output."
 3894   (goto-char (point-min))
 3895   (if (looking-at "Running `\\(.*\\)' on `\\(.*\\)' with ``\\(.*\\)''$")
 3896       (let ((name (match-string 1))
 3897             (file (match-string 2)))
 3898         (with-current-buffer TeX-command-buffer
 3899           (TeX-command name (if (string-match TeX-region file)
 3900                                 #'TeX-region-file
 3901                               #'TeX-master-file))))
 3902     (error "Unable to find what command to run")))
 3903 
 3904 (provide 'tex-buf)
 3905 
 3906 ;;; tex-buf.el ends here