Commit | Line | Data |
---|---|---|
95a045bd | 1 | ;;; tex-mode.el --- TeX, LaTeX, and SliTeX mode commands -*- coding: utf-8 -*- |
d501f516 | 2 | |
bca7a08b | 3 | ;; Copyright (C) 1985,86,89,92,94,95,96,97,98,1999,2002,2003 |
d61140e8 | 4 | ;; Free Software Foundation, Inc. |
eea8d4ef | 5 | |
70f20973 | 6 | ;; Maintainer: FSF |
d7b4d18f | 7 | ;; Keywords: tex |
e5167999 | 8 | |
528415e7 | 9 | ;; Contributions over the years by William F. Schelter, Dick King, |
70f20973 | 10 | ;; Stephen Gildea, Michael Prange, Jacob Gore, and Edward M. Reingold. |
528415e7 | 11 | |
869bff31 | 12 | ;; This file is part of GNU Emacs. |
13 | ||
14 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
15 | ;; it under the terms of the GNU General Public License as published by | |
e5167999 | 16 | ;; the Free Software Foundation; either version 2, or (at your option) |
869bff31 | 17 | ;; any later version. |
18 | ||
19 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
20 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | ;; GNU General Public License for more details. | |
23 | ||
24 | ;; You should have received a copy of the GNU General Public License | |
6da9bbd6 RS |
25 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
26 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
27 | ;; Boston, MA 02111-1307, USA. | |
869bff31 | 28 | |
55535639 PJ |
29 | ;;; Commentary: |
30 | ||
e5167999 ER |
31 | ;;; Code: |
32 | ||
bed1b0c8 RS |
33 | ;; Pacify the byte-compiler |
34 | (eval-when-compile | |
35 | (require 'compare-w) | |
ea590e1c | 36 | (require 'cl) |
bed1b0c8 RS |
37 | (require 'skeleton)) |
38 | ||
a0e9c22a | 39 | (require 'shell) |
2f3067de | 40 | (require 'compile) |
528415e7 | 41 | |
d61140e8 RS |
42 | (defgroup tex-file nil |
43 | "TeX files and directories" | |
44 | :prefix "tex-" | |
45 | :group 'tex) | |
46 | ||
47 | (defgroup tex-run nil | |
48 | "Running external commands from TeX mode" | |
49 | :prefix "tex-" | |
50 | :group 'tex) | |
51 | ||
52 | (defgroup tex-view nil | |
53 | "Viewing and printing TeX files" | |
54 | :prefix "tex-" | |
55 | :group 'tex) | |
56 | ||
7e1dae73 | 57 | ;;;###autoload |
d61140e8 RS |
58 | (defcustom tex-shell-file-name nil |
59 | "*If non-nil, the shell file name to run in the subshell used to run TeX." | |
60 | :type '(choice (const :tag "None" nil) | |
61 | string) | |
62 | :group 'tex-run) | |
528415e7 | 63 | |
7e1dae73 | 64 | ;;;###autoload |
d61140e8 | 65 | (defcustom tex-directory "." |
f73741fc | 66 | "*Directory in which temporary files are written. |
81c735c0 | 67 | You can make this `/tmp' if your TEXINPUTS has no relative directories in it |
528415e7 | 68 | and you don't try to apply \\[tex-region] or \\[tex-buffer] when there are |
d61140e8 RS |
69 | `\\input' commands with relative directories." |
70 | :type 'directory | |
71 | :group 'tex-file) | |
869bff31 | 72 | |
725b7428 | 73 | ;;;###autoload |
d61140e8 | 74 | (defcustom tex-first-line-header-regexp nil |
725b7428 RS |
75 | "Regexp for matching a first line which `tex-region' should include. |
76 | If this is non-nil, it should be a regular expression string; | |
77 | if it matches the first line of the file, | |
d61140e8 RS |
78 | `tex-region' always includes the first line in the TeX run." |
79 | :type '(choice (const :tag "None" nil) | |
80 | regexp) | |
81 | :group 'tex-file) | |
725b7428 | 82 | |
f73741fc | 83 | ;;;###autoload |
d61140e8 | 84 | (defcustom tex-main-file nil |
f73741fc | 85 | "*The main TeX source file which includes this buffer's file. |
be4a962d RS |
86 | The command `tex-file' runs TeX on the file specified by `tex-main-file' |
87 | if the variable is non-nil." | |
d61140e8 RS |
88 | :type '(choice (const :tag "None" nil) |
89 | file) | |
90 | :group 'tex-file) | |
f73741fc | 91 | |
7e1dae73 | 92 | ;;;###autoload |
d61140e8 RS |
93 | (defcustom tex-offer-save t |
94 | "*If non-nil, ask about saving modified buffers before \\[tex-file] is run." | |
95 | :type 'boolean | |
96 | :group 'tex-file) | |
869bff31 | 97 | |
7e1dae73 | 98 | ;;;###autoload |
d61140e8 | 99 | (defcustom tex-run-command "tex" |
869bff31 | 100 | "*Command used to run TeX subjob. |
c3ce7bf4 RS |
101 | TeX Mode sets `tex-command' to this string. |
102 | See the documentation of that variable." | |
d61140e8 RS |
103 | :type 'string |
104 | :group 'tex-run) | |
869bff31 | 105 | |
7e1dae73 | 106 | ;;;###autoload |
d61140e8 | 107 | (defcustom latex-run-command "latex" |
869bff31 | 108 | "*Command used to run LaTeX subjob. |
c3ce7bf4 RS |
109 | LaTeX Mode sets `tex-command' to this string. |
110 | See the documentation of that variable." | |
d61140e8 RS |
111 | :type 'string |
112 | :group 'tex-run) | |
528415e7 | 113 | |
bed1b0c8 RS |
114 | ;;;###autoload |
115 | (defcustom slitex-run-command "slitex" | |
116 | "*Command used to run SliTeX subjob. | |
c3ce7bf4 RS |
117 | SliTeX Mode sets `tex-command' to this string. |
118 | See the documentation of that variable." | |
bed1b0c8 RS |
119 | :type 'string |
120 | :group 'tex-run) | |
121 | ||
c3ce7bf4 | 122 | ;;;###autoload |
95a045bd | 123 | (defcustom tex-start-options "" |
9e0ad27a | 124 | "*TeX options to use when starting TeX. |
95a045bd SM |
125 | These immediately precede the commands in `tex-start-commands' |
126 | and the input file name, with no separating space and are not shell-quoted. | |
127 | If nil, TeX runs with no options. See the documentation of `tex-command'." | |
128 | :type 'string | |
bed1b0c8 | 129 | :group 'tex-run |
9e0ad27a RS |
130 | :version "21.4") |
131 | ||
132 | ;;;###autoload | |
133 | (defcustom tex-start-commands "\\nonstopmode\\input" | |
134 | "*TeX commands to use when starting TeX. | |
95a045bd SM |
135 | They are shell-quoted and precede the input file name, with a separating space. |
136 | If nil, no commands are used. See the documentation of `tex-command'." | |
9e0ad27a RS |
137 | :type '(radio (const :tag "Interactive \(nil\)" nil) |
138 | (const :tag "Nonstop \(\"\\nonstopmode\\input\"\)" | |
139 | "\\nonstopmode\\input") | |
140 | (string :tag "String at your choice")) | |
141 | :group 'tex-run | |
142 | :version "21.4") | |
bed1b0c8 | 143 | |
7afecb99 | 144 | (defvar latex-standard-block-names |
dd459839 SM |
145 | '("abstract" "array" "center" "description" |
146 | "displaymath" "document" "enumerate" "eqnarray" | |
147 | "eqnarray*" "equation" "figure" "figure*" | |
148 | "flushleft" "flushright" "itemize" "letter" | |
149 | "list" "minipage" "picture" "quotation" | |
150 | "quote" "slide" "sloppypar" "tabbing" | |
151 | "table" "table*" "tabular" "tabular*" | |
152 | "thebibliography" "theindex*" "titlepage" "trivlist" | |
153 | "verbatim" "verbatim*" "verse" "math") | |
528415e7 RS |
154 | "Standard LaTeX block names.") |
155 | ||
7e1dae73 | 156 | ;;;###autoload |
d61140e8 | 157 | (defcustom latex-block-names nil |
528415e7 | 158 | "*User defined LaTeX block names. |
7afecb99 | 159 | Combined with `latex-standard-block-names' for minibuffer completion." |
d61140e8 RS |
160 | :type '(repeat string) |
161 | :group 'tex-run) | |
869bff31 | 162 | |
7e1dae73 | 163 | ;;;###autoload |
d61140e8 | 164 | (defcustom tex-bibtex-command "bibtex" |
528415e7 | 165 | "*Command used by `tex-bibtex-file' to gather bibliographic data. |
81c735c0 | 166 | If this string contains an asterisk (`*'), that is replaced by the file name; |
d61140e8 RS |
167 | otherwise, the file name, preceded by blank, is added at the end." |
168 | :type 'string | |
169 | :group 'tex-run) | |
869bff31 | 170 | |
7e1dae73 | 171 | ;;;###autoload |
d61140e8 | 172 | (defcustom tex-dvi-print-command "lpr -d" |
528415e7 | 173 | "*Command used by \\[tex-print] to print a .dvi file. |
81c735c0 | 174 | If this string contains an asterisk (`*'), that is replaced by the file name; |
d61140e8 RS |
175 | otherwise, the file name, preceded by blank, is added at the end." |
176 | :type 'string | |
177 | :group 'tex-view) | |
528415e7 | 178 | |
7e1dae73 | 179 | ;;;###autoload |
d61140e8 | 180 | (defcustom tex-alt-dvi-print-command "lpr -d" |
528415e7 | 181 | "*Command used by \\[tex-print] with a prefix arg to print a .dvi file. |
81c735c0 RS |
182 | If this string contains an asterisk (`*'), that is replaced by the file name; |
183 | otherwise, the file name, preceded by blank, is added at the end. | |
528415e7 | 184 | |
81c735c0 RS |
185 | If two printers are not enough of a choice, you can set the variable |
186 | `tex-alt-dvi-print-command' to an expression that asks what you want; | |
528415e7 RS |
187 | for example, |
188 | ||
189 | (setq tex-alt-dvi-print-command | |
190 | '(format \"lpr -P%s\" (read-string \"Use printer: \"))) | |
191 | ||
192 | would tell \\[tex-print] with a prefix argument to ask you which printer to | |
d61140e8 RS |
193 | use." |
194 | :type '(choice (string :tag "Command") | |
195 | (sexp :tag "Expression")) | |
196 | :group 'tex-view) | |
869bff31 | 197 | |
7e1dae73 | 198 | ;;;###autoload |
746c30e2 | 199 | (defcustom tex-dvi-view-command '(if (eq window-system 'x) \"xdvi\" \"dvi2tty * | cat -s\") |
81c735c0 | 200 | "*Command used by \\[tex-view] to display a `.dvi' file. |
746c30e2 | 201 | If it is a string, that specifies the command directly. |
81c735c0 | 202 | If this string contains an asterisk (`*'), that is replaced by the file name; |
746c30e2 | 203 | otherwise, the file name, preceded by a space, is added at the end. |
528415e7 | 204 | |
746c30e2 RS |
205 | If the value is a form, it is evaluated to get the command to use." |
206 | :type '(choice (const nil) string sexp) | |
d61140e8 | 207 | :group 'tex-view) |
869bff31 | 208 | |
7e1dae73 | 209 | ;;;###autoload |
d61140e8 | 210 | (defcustom tex-show-queue-command "lpq" |
528415e7 | 211 | "*Command used by \\[tex-show-print-queue] to show the print queue. |
d61140e8 RS |
212 | Should show the queue(s) that \\[tex-print] puts jobs on." |
213 | :type 'string | |
214 | :group 'tex-view) | |
869bff31 | 215 | |
7e1dae73 | 216 | ;;;###autoload |
9920303f | 217 | (defcustom tex-default-mode 'latex-mode |
869bff31 | 218 | "*Mode to enter for a new file that might be either TeX or LaTeX. |
219 | This variable is used when it can't be determined whether the file | |
220 | is plain TeX or LaTeX or what because the file contains no commands. | |
d61140e8 RS |
221 | Normally set to either `plain-tex-mode' or `latex-mode'." |
222 | :type 'function | |
223 | :group 'tex) | |
869bff31 | 224 | |
7e1dae73 | 225 | ;;;###autoload |
d61140e8 RS |
226 | (defcustom tex-open-quote "``" |
227 | "*String inserted by typing \\[tex-insert-quote] to open a quotation." | |
228 | :type 'string | |
95a045bd | 229 | :options '("``" "\"<" "\"`" "<<" "«") |
d61140e8 | 230 | :group 'tex) |
869bff31 | 231 | |
7e1dae73 | 232 | ;;;###autoload |
d61140e8 RS |
233 | (defcustom tex-close-quote "''" |
234 | "*String inserted by typing \\[tex-insert-quote] to close a quotation." | |
235 | :type 'string | |
95a045bd | 236 | :options '("''" "\">" "\"'" ">>" "»") |
d61140e8 | 237 | :group 'tex) |
869bff31 | 238 | |
528415e7 RS |
239 | (defvar tex-last-temp-file nil |
240 | "Latest temporary file generated by \\[tex-region] and \\[tex-buffer]. | |
241 | Deleted when the \\[tex-region] or \\[tex-buffer] is next run, or when the | |
81c735c0 | 242 | tex shell terminates.") |
528415e7 | 243 | |
95a045bd | 244 | (defvar tex-command "tex" |
c3ce7bf4 | 245 | "*Command to run TeX. |
95a045bd | 246 | If this string contains an asterisk \(`*'\), that is replaced by the file name; |
9e0ad27a RS |
247 | otherwise the value of `tex-start-options', the \(shell-quoted\) |
248 | value of `tex-start-commands', and the file name are added at the end | |
249 | with blanks as separators. | |
c3ce7bf4 RS |
250 | |
251 | In TeX, LaTeX, and SliTeX Mode this variable becomes buffer local. | |
252 | In these modes, use \\[set-variable] if you want to change it for the | |
253 | current buffer.") | |
869bff31 | 254 | |
255 | (defvar tex-trailer nil | |
256 | "String appended after the end of a region sent to TeX by \\[tex-region].") | |
257 | ||
258 | (defvar tex-start-of-header nil | |
898b9ac1 | 259 | "Regular expression used by \\[tex-region] to find start of file's header.") |
869bff31 | 260 | |
261 | (defvar tex-end-of-header nil | |
898b9ac1 | 262 | "Regular expression used by \\[tex-region] to find end of file's header.") |
869bff31 | 263 | |
264 | (defvar tex-shell-cd-command "cd" | |
265 | "Command to give to shell running TeX to change directory. | |
81c735c0 | 266 | The value of `tex-directory' is appended to this, separated by a space.") |
869bff31 | 267 | |
268 | (defvar tex-zap-file nil | |
269 | "Temporary file name used for text being sent as input to TeX. | |
270 | Should be a simple file name with no extension or directory specification.") | |
271 | ||
272 | (defvar tex-last-buffer-texed nil | |
273 | "Buffer which was last TeXed.") | |
274 | ||
275 | (defvar tex-print-file nil | |
276 | "File name that \\[tex-print] prints. | |
277 | Set by \\[tex-region], \\[tex-buffer], and \\[tex-file].") | |
278 | ||
ea590e1c SM |
279 | (easy-mmode-defsyntax tex-mode-syntax-table |
280 | '((?% . "<") | |
281 | (?\n . ">") | |
282 | (?\f . ">") | |
283 | (?\C-@ . "w") | |
284 | (?' . "w") | |
285 | (?@ . "_") | |
286 | (?* . "_") | |
287 | (?\t . " ") | |
bca7a08b SM |
288 | ;; ~ is printed by TeX as a space, but it's semantics in the syntax |
289 | ;; of TeX is not `whitespace' (i.e. it's just like \hspace{foo}). | |
290 | (?~ . ".") | |
ea590e1c SM |
291 | (?$ . "$$") |
292 | (?\\ . "/") | |
293 | (?\" . ".") | |
294 | (?& . ".") | |
15ca8de7 SM |
295 | (?_ . ".") |
296 | (?^ . ".")) | |
869bff31 | 297 | "Syntax table used while in TeX mode.") |
53c4fe47 SM |
298 | \f |
299 | ;;;; | |
300 | ;;;; Imenu support | |
301 | ;;;; | |
869bff31 | 302 | |
d6709b80 | 303 | (defcustom latex-imenu-indent-string ". " |
a53c647b RS |
304 | "*String to add repeated in front of nested sectional units for Imenu. |
305 | An alternative value is \" . \", if you use a font with a narrow period." | |
306 | :type 'string | |
307 | :group 'tex) | |
308 | ||
d6709b80 SM |
309 | (defvar latex-section-alist |
310 | '(("part" . 0) ("chapter" . 1) | |
311 | ("section" . 2) ("subsection" . 3) | |
312 | ("subsubsection" . 4) | |
313 | ("paragraph" . 5) ("subparagraph" . 6))) | |
314 | ||
53c4fe47 SM |
315 | (defvar latex-metasection-list |
316 | '("documentstyle" "documentclass" | |
317 | "begin{document}" "end{document}" | |
318 | "appendix" "frontmatter" "mainmatter" "backmatter")) | |
319 | ||
a5e2ae01 | 320 | (defun latex-imenu-create-index () |
53c4fe47 SM |
321 | "Generate an alist for imenu from a LaTeX buffer." |
322 | (let ((section-regexp | |
323 | (concat "\\\\" (regexp-opt (mapcar 'car latex-section-alist) t) | |
324 | "\\*?[ \t]*{")) | |
325 | (metasection-regexp | |
326 | (concat "\\\\" (regexp-opt latex-metasection-list t))) | |
327 | i0 menu case-fold-search) | |
a53c647b RS |
328 | (save-excursion |
329 | ;; Find the top-most level in this file but don't allow it to be | |
330 | ;; any deeper than "section" (which is top-level in an article). | |
331 | (goto-char (point-min)) | |
332 | (if (search-forward-regexp "\\\\part\\*?[ \t]*{" nil t) | |
333 | (setq i0 0) | |
334 | (if (search-forward-regexp "\\\\chapter\\*?[ \t]*{" nil t) | |
335 | (setq i0 1) | |
336 | (setq i0 2))) | |
337 | ||
338 | ;; Look for chapters and sections. | |
339 | (goto-char (point-min)) | |
53c4fe47 | 340 | (while (search-forward-regexp section-regexp nil t) |
a53c647b RS |
341 | (let ((start (match-beginning 0)) |
342 | (here (point)) | |
343 | (i (cdr (assoc (buffer-substring-no-properties | |
344 | (match-beginning 1) | |
345 | (match-end 1)) | |
d6709b80 | 346 | latex-section-alist)))) |
a53c647b RS |
347 | (backward-char 1) |
348 | (condition-case err | |
349 | (progn | |
350 | ;; Using sexps allows some use of matching {...} inside | |
351 | ;; titles. | |
352 | (forward-sexp 1) | |
d6709b80 SM |
353 | (push (cons (concat (apply 'concat |
354 | (make-list | |
355 | (max 0 (- i i0)) | |
356 | latex-imenu-indent-string)) | |
357 | (buffer-substring-no-properties | |
358 | here (1- (point)))) | |
359 | start) | |
360 | menu)) | |
a53c647b RS |
361 | (error nil)))) |
362 | ||
363 | ;; Look for included material. | |
364 | (goto-char (point-min)) | |
365 | (while (search-forward-regexp | |
366 | "\\\\\\(include\\|input\\|verbatiminput\\|bibliography\\)\ | |
53c4fe47 | 367 | \[ \t]*{\\([^}\n]+\\)}" |
a53c647b | 368 | nil t) |
53c4fe47 SM |
369 | (push (cons (concat "<<" (buffer-substring-no-properties |
370 | (match-beginning 2) | |
371 | (match-end 2)) | |
372 | (if (= (char-after (match-beginning 1)) ?b) | |
373 | ".bbl" | |
374 | ".tex")) | |
375 | (match-beginning 0)) | |
376 | menu)) | |
a53c647b RS |
377 | |
378 | ;; Look for \frontmatter, \mainmatter, \backmatter, and \appendix. | |
379 | (goto-char (point-min)) | |
53c4fe47 SM |
380 | (while (search-forward-regexp metasection-regexp nil t) |
381 | (push (cons "--" (match-beginning 0)) menu)) | |
a53c647b RS |
382 | |
383 | ;; Sort in increasing buffer position order. | |
384 | (sort menu (function (lambda (a b) (< (cdr a) (cdr b)))))))) | |
53c4fe47 SM |
385 | \f |
386 | ;;;; | |
387 | ;;;; Outline support | |
388 | ;;;; | |
389 | ||
390 | (defvar latex-outline-regexp | |
391 | (concat "\\\\" | |
392 | (regexp-opt (append latex-metasection-list | |
393 | (mapcar 'car latex-section-alist)) t))) | |
394 | ||
395 | (defun latex-outline-level () | |
396 | (if (looking-at latex-outline-regexp) | |
397 | (1+ (or (cdr (assoc (match-string 1) latex-section-alist)) -1)) | |
398 | 1000)) | |
399 | \f | |
400 | ;;;; | |
401 | ;;;; Font-Lock support | |
402 | ;;;; | |
403 | ||
404 | ;(defvar tex-font-lock-keywords | |
405 | ; ;; Regexps updated with help from Ulrik Dickow <dickow@nbi.dk>. | |
406 | ; '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}" | |
407 | ; 2 font-lock-function-name-face) | |
408 | ; ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}" | |
409 | ; 2 font-lock-constant-face) | |
410 | ; ;; It seems a bit dubious to use `bold' and `italic' faces since we might | |
411 | ; ;; not be able to display those fonts. | |
412 | ; ("{\\\\bf\\([^}]+\\)}" 1 'bold keep) | |
413 | ; ("{\\\\\\(em\\|it\\|sl\\)\\([^}]+\\)}" 2 'italic keep) | |
414 | ; ("\\\\\\([a-zA-Z@]+\\|.\\)" . font-lock-keyword-face) | |
415 | ; ("^[ \t\n]*\\\\def[\\\\@]\\(\\w+\\)" 1 font-lock-function-name-face keep)) | |
416 | ; ;; Rewritten and extended for LaTeX2e by Ulrik Dickow <dickow@nbi.dk>. | |
417 | ; '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}" | |
418 | ; 2 font-lock-function-name-face) | |
419 | ; ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}" | |
420 | ; 2 font-lock-constant-face) | |
421 | ; ("^[ \t]*\\\\def\\\\\\(\\(\\w\\|@\\)+\\)" 1 font-lock-function-name-face) | |
422 | ; "\\\\\\([a-zA-Z@]+\\|.\\)" | |
423 | ; ;; It seems a bit dubious to use `bold' and `italic' faces since we might | |
424 | ; ;; not be able to display those fonts. | |
425 | ; ;; LaTeX2e: \emph{This is emphasized}. | |
426 | ; ("\\\\emph{\\([^}]+\\)}" 1 'italic keep) | |
427 | ; ;; LaTeX2e: \textbf{This is bold}, \textit{...}, \textsl{...} | |
428 | ; ("\\\\text\\(\\(bf\\)\\|it\\|sl\\){\\([^}]+\\)}" | |
429 | ; 3 (if (match-beginning 2) 'bold 'italic) keep) | |
430 | ; ;; Old-style bf/em/it/sl. Stop at `\\' and un-escaped `&', for tables. | |
431 | ; ("\\\\\\(\\(bf\\)\\|em\\|it\\|sl\\)\\>\\(\\([^}&\\]\\|\\\\[^\\]\\)+\\)" | |
432 | ; 3 (if (match-beginning 2) 'bold 'italic) keep)) | |
433 | ||
434 | ;; Rewritten with the help of Alexandra Bac <abac@welcome.disi.unige.it>. | |
435 | (defconst tex-font-lock-keywords-1 | |
436 | (eval-when-compile | |
437 | (let* (;; Names of commands whose arg should be fontified as heading, etc. | |
438 | (headings (regexp-opt | |
439 | '("title" "begin" "end" "chapter" "part" | |
440 | "section" "subsection" "subsubsection" | |
441 | "paragraph" "subparagraph" "subsubparagraph" | |
7afecb99 SM |
442 | "newcommand" "renewcommand" "providecommand" |
443 | "newenvironment" "renewenvironment" | |
444 | "newtheorem" "renewtheorem") | |
53c4fe47 SM |
445 | t)) |
446 | (variables (regexp-opt | |
447 | '("newcounter" "newcounter*" "setcounter" "addtocounter" | |
448 | "setlength" "addtolength" "settowidth") | |
449 | t)) | |
450 | (includes (regexp-opt | |
451 | '("input" "include" "includeonly" "bibliography" | |
452 | "epsfig" "psfig" "epsf" "nofiles" "usepackage" | |
15ca8de7 | 453 | "documentstyle" "documentclass" "verbatiminput" |
53c4fe47 SM |
454 | "includegraphics" "includegraphics*") |
455 | t)) | |
456 | ;; Miscellany. | |
457 | (slash "\\\\") | |
15ca8de7 SM |
458 | (opt " *\\(\\[[^]]*\\] *\\)*") |
459 | ;; This would allow highlighting \newcommand\CMD but requires | |
460 | ;; adapting subgroup numbers below. | |
461 | ;; (arg "\\(?:{\\(\\(?:[^{}\\]+\\|\\\\.\\|{[^}]*}\\)+\\)\\|\\\\[a-z*]+\\)")) | |
1598a961 | 462 | (arg "{\\(\\(?:[^{}\\]+\\|\\\\.\\|{[^}]*}\\)+\\)")) |
53c4fe47 | 463 | (list |
95a045bd SM |
464 | ;; display $$ math $$ |
465 | ;; We only mark the match between $$ and $$ because the $$ delimiters | |
466 | ;; themselves have already been marked (along with $..$) by syntactic | |
467 | ;; fontification. Also this is done at the very beginning so as to | |
468 | ;; interact with the other keywords in the same way as $...$ does. | |
469 | (list "\\$\\$\\([^$]+\\)\\$\\$" 1 'tex-math-face) | |
53c4fe47 SM |
470 | ;; Heading args. |
471 | (list (concat slash headings "\\*?" opt arg) | |
bdbd9606 SM |
472 | ;; If ARG ends up matching too much (if the {} don't match, f.ex) |
473 | ;; jit-lock will do funny things: when updating the buffer | |
474 | ;; the re-highlighting is only done locally so it will just | |
475 | ;; match the local line, but defer-contextually will | |
476 | ;; match more lines at a time, so ARG will end up matching | |
477 | ;; a lot more, which might suddenly include a comment | |
478 | ;; so you get things highlighted bold when you type them | |
479 | ;; but they get turned back to normal a little while later | |
480 | ;; because "there's already a face there". | |
481 | ;; Using `keep' works around this un-intuitive behavior as well | |
15ca8de7 SM |
482 | ;; as improves the behavior in the very rare case where you do |
483 | ;; have a comment in ARG. | |
bdbd9606 | 484 | 3 'font-lock-function-name-face 'keep) |
7afecb99 SM |
485 | (list (concat slash "\\(?:provide\\|\\(?:re\\)?new\\)command\\** *\\(\\\\[A-Za-z@]+\\)") |
486 | 1 'font-lock-function-name-face 'keep) | |
53c4fe47 | 487 | ;; Variable args. |
15ca8de7 | 488 | (list (concat slash variables " *" arg) 2 'font-lock-variable-name-face) |
53c4fe47 SM |
489 | ;; Include args. |
490 | (list (concat slash includes opt arg) 3 'font-lock-builtin-face) | |
491 | ;; Definitions. I think. | |
15ca8de7 | 492 | '("^[ \t]*\\\\def *\\\\\\(\\(\\w\\|@\\)+\\)" |
bdbd9606 | 493 | 1 font-lock-function-name-face)))) |
53c4fe47 SM |
494 | "Subdued expressions to highlight in TeX modes.") |
495 | ||
496 | (defconst tex-font-lock-keywords-2 | |
497 | (append tex-font-lock-keywords-1 | |
498 | (eval-when-compile | |
499 | (let* (;; | |
500 | ;; Names of commands whose arg should be fontified with fonts. | |
1598a961 | 501 | (bold (regexp-opt '("textbf" "textsc" "textup" |
53c4fe47 | 502 | "boldsymbol" "pmb") t)) |
1598a961 | 503 | (italic (regexp-opt '("textit" "textsl" "emph") t)) |
a3d80d4a SM |
504 | ;; FIXME: unimplemented yet. |
505 | ;; (type (regexp-opt '("texttt" "textmd" "textrm" "textsf") t)) | |
53c4fe47 SM |
506 | ;; |
507 | ;; Names of commands whose arg should be fontified as a citation. | |
508 | (citations (regexp-opt | |
509 | '("label" "ref" "pageref" "vref" "eqref" | |
15ca8de7 | 510 | "cite" "nocite" "index" "glossary" "bibitem" |
1598a961 SM |
511 | ;; These are text, rather than citations. |
512 | ;; "caption" "footnote" "footnotemark" "footnotetext" | |
513 | ) | |
53c4fe47 SM |
514 | t)) |
515 | ;; | |
516 | ;; Names of commands that should be fontified. | |
1b741d9e AS |
517 | (specials-1 (regexp-opt '("\\" "\\*") t)) ;; "-" |
518 | (specials-2 (regexp-opt | |
519 | '("linebreak" "nolinebreak" "pagebreak" "nopagebreak" | |
520 | "newline" "newpage" "clearpage" "cleardoublepage" | |
521 | "displaybreak" "allowdisplaybreaks" | |
522 | "enlargethispage") t)) | |
53c4fe47 SM |
523 | (general "\\([a-zA-Z@]+\\**\\|[^ \t\n]\\)") |
524 | ;; | |
525 | ;; Miscellany. | |
526 | (slash "\\\\") | |
15ca8de7 | 527 | (opt " *\\(\\[[^]]*\\] *\\)*") |
7afecb99 | 528 | (args "\\(\\(?:[^{}&\\]+\\|\\\\.\\|{[^}]*}\\)+\\)") |
1598a961 | 529 | (arg "{\\(\\(?:[^{}\\]+\\|\\\\.\\|{[^}]*}\\)+\\)")) |
53c4fe47 SM |
530 | (list |
531 | ;; | |
532 | ;; Citation args. | |
533 | (list (concat slash citations opt arg) 3 'font-lock-constant-face) | |
534 | ;; | |
15ca8de7 | 535 | ;; Text between `` quotes ''. |
95a045bd | 536 | (cons (concat (regexp-opt `("``" "\"<" "\"`" "<<" "«") t) |
7abc9add | 537 | "[^'\">{]+" ;a bit pessimistic |
95a045bd | 538 | (regexp-opt `("''" "\">" "\"'" ">>" "»") t)) |
15ca8de7 SM |
539 | 'font-lock-string-face) |
540 | ;; | |
53c4fe47 | 541 | ;; Command names, special and general. |
1b741d9e AS |
542 | (cons (concat slash specials-1) 'font-lock-warning-face) |
543 | (list (concat "\\(" slash specials-2 "\\)\\([^a-zA-Z@]\\|\\'\\)") | |
544 | 1 'font-lock-warning-face) | |
53c4fe47 SM |
545 | (concat slash general) |
546 | ;; | |
547 | ;; Font environments. It seems a bit dubious to use `bold' etc. faces | |
548 | ;; since we might not be able to display those fonts. | |
15ca8de7 SM |
549 | (list (concat slash bold " *" arg) 2 '(quote bold) 'append) |
550 | (list (concat slash italic " *" arg) 2 '(quote italic) 'append) | |
1598a961 | 551 | ;; (list (concat slash type arg) 2 '(quote bold-italic) 'append) |
53c4fe47 SM |
552 | ;; |
553 | ;; Old-style bf/em/it/sl. Stop at `\\' and un-escaped `&', for tables. | |
7afecb99 SM |
554 | (list (concat "\\\\\\(em\\|it\\|sl\\)\\>" args) |
555 | 2 '(quote italic) 'append) | |
556 | ;; This is separate from the previous one because of cases like | |
557 | ;; {\em foo {\bf bar} bla} where both match. | |
558 | (list (concat "\\\\bf\\>" args) 1 '(quote bold) 'append))))) | |
53c4fe47 SM |
559 | "Gaudy expressions to highlight in TeX modes.") |
560 | ||
7afecb99 SM |
561 | (defun tex-font-lock-suscript (pos) |
562 | (unless (or (memq (get-text-property pos 'face) | |
563 | '(font-lock-constant-face font-lock-builtin-face | |
564 | font-lock-comment-face tex-verbatim-face)) | |
565 | ;; Check for backslash quoting | |
566 | (let ((odd nil) | |
567 | (pos pos)) | |
568 | (while (eq (char-before pos) ?\\) | |
569 | (setq pos (1- pos) odd (not odd))) | |
570 | odd)) | |
571 | (if (eq (char-after pos) ?_) | |
572 | '(face subscript display (raise -0.3)) | |
573 | '(face superscript display (raise +0.3))))) | |
574 | ||
575 | (defconst tex-font-lock-keywords-3 | |
576 | (append tex-font-lock-keywords-2 | |
577 | (eval-when-compile | |
578 | (let ((general "\\([a-zA-Z@]+\\|[^ \t\n]\\)") | |
579 | (slash "\\\\") | |
580 | ;; This is not the same regexp as before: it has a `+' removed. | |
581 | ;; The + makes the matching faster in the above cases (where we can | |
582 | ;; exit as soon as the match fails) but would make this matching | |
583 | ;; degenerate to nasty complexity (because we try to match the | |
584 | ;; closing brace, which forces trying all matching combinations). | |
585 | (arg "{\\(?:[^{}\\]\\|\\\\.\\|{[^}]*}\\)*")) | |
586 | `((,(concat "[_^] *\\([^\n\\{}]\\|" slash general "\\|" arg "}\\)") | |
587 | (1 (tex-font-lock-suscript (match-beginning 0)) | |
588 | append)))))) | |
589 | "Experimental expressions to highlight in TeX modes.") | |
590 | ||
53c4fe47 SM |
591 | (defvar tex-font-lock-keywords tex-font-lock-keywords-1 |
592 | "Default expressions to highlight in TeX modes.") | |
0fb4f245 | 593 | |
7afecb99 SM |
594 | (defvar tex-verbatim-environments |
595 | '("verbatim" "verbatim*")) | |
596 | ||
597 | (defvar tex-font-lock-syntactic-keywords | |
598 | (let ((verbs (regexp-opt tex-verbatim-environments t))) | |
599 | `((,(concat "^\\\\begin *{" verbs "}.*\\(\n\\)") 2 "|") | |
600 | (,(concat "^\\\\end *{" verbs "}\\(.?\\)") 2 | |
601 | (unless (<= (match-beginning 0) (point-min)) | |
602 | (put-text-property (1- (match-beginning 0)) (match-beginning 0) | |
603 | 'syntax-table (string-to-syntax "|")) | |
604 | "<")) | |
605 | ;; ("^\\(\\\\\\)begin *{comment}" 1 "< b") | |
606 | ;; ("^\\\\end *{comment}.*\\(\n\\)" 1 "> b") | |
607 | ("\\\\verb\\**\\([^a-z@*]\\)" 1 "\"")))) | |
608 | ||
609 | (defun tex-font-lock-unfontify-region (beg end) | |
610 | (font-lock-default-unfontify-region beg end) | |
611 | (while (< beg end) | |
612 | (let ((next (next-single-property-change beg 'display nil end)) | |
613 | (prop (get-text-property beg 'display))) | |
614 | (if (and (eq (car-safe prop) 'raise) | |
615 | (member (car-safe (cdr prop)) '(-0.3 +0.3)) | |
616 | (null (cddr prop))) | |
617 | (put-text-property beg next 'display nil)) | |
618 | (setq beg next)))) | |
619 | ||
620 | (defface superscript | |
621 | '((t :height 0.8)) ;; :raise 0.3 | |
622 | "Face used for superscripts.") | |
623 | (defface subscript | |
624 | '((t :height 0.8)) ;; :raise -0.3 | |
625 | "Face used for subscripts.") | |
bdbd9606 SM |
626 | |
627 | (defface tex-math-face | |
628 | '((t :inherit font-lock-string-face)) | |
629 | "Face used to highlight TeX math expressions.") | |
630 | (defvar tex-math-face 'tex-math-face) | |
7afecb99 SM |
631 | (defface tex-verbatim-face |
632 | ;; '((t :inherit font-lock-string-face)) | |
633 | '((t :family "courier")) | |
634 | "Face used to highlight TeX verbatim environments.") | |
635 | (defvar tex-verbatim-face 'tex-verbatim-face) | |
bdbd9606 SM |
636 | |
637 | ;; Use string syntax but math face for $...$. | |
638 | (defun tex-font-lock-syntactic-face-function (state) | |
7afecb99 SM |
639 | (let ((char (nth 3 state))) |
640 | (cond | |
641 | ((not char) font-lock-comment-face) | |
642 | ((eq char ?$) tex-math-face) | |
643 | (t | |
644 | (when (char-valid-p char) | |
645 | ;; This is a \verb?...? construct. Let's find the end and mark it. | |
646 | (save-excursion | |
647 | (skip-chars-forward (string ?^ char)) ;; Use `end' ? | |
648 | (when (eq (char-syntax (preceding-char)) ?/) | |
649 | (put-text-property (1- (point)) (point) 'syntax-table '(1))) | |
650 | (unless (eobp) | |
651 | (put-text-property (point) (1+ (point)) 'syntax-table '(7))))) | |
652 | tex-verbatim-face)))) | |
bdbd9606 | 653 | |
53c4fe47 | 654 | \f |
869bff31 | 655 | (defun tex-define-common-keys (keymap) |
81c735c0 | 656 | "Define the keys that we want defined both in TeX mode and in the TeX shell." |
869bff31 | 657 | (define-key keymap "\C-c\C-k" 'tex-kill-job) |
658 | (define-key keymap "\C-c\C-l" 'tex-recenter-output-buffer) | |
659 | (define-key keymap "\C-c\C-q" 'tex-show-print-queue) | |
660 | (define-key keymap "\C-c\C-p" 'tex-print) | |
661 | (define-key keymap "\C-c\C-v" 'tex-view) | |
62a24cb5 RS |
662 | |
663 | (define-key keymap [menu-bar tex] (cons "TeX" (make-sparse-keymap "TeX"))) | |
664 | ||
bdbd9606 SM |
665 | (define-key keymap [menu-bar tex tex-kill-job] |
666 | '(menu-item "Tex Kill" tex-kill-job :enable (tex-shell-running))) | |
cf6d6e8a | 667 | (define-key keymap [menu-bar tex tex-recenter-output-buffer] |
bdbd9606 SM |
668 | '(menu-item "Tex Recenter" tex-recenter-output-buffer |
669 | :enable (get-buffer "*tex-shell*"))) | |
62a24cb5 RS |
670 | (define-key keymap [menu-bar tex tex-show-print-queue] |
671 | '("Show Print Queue" . tex-show-print-queue)) | |
cf6d6e8a | 672 | (define-key keymap [menu-bar tex tex-alt-print] |
bdbd9606 SM |
673 | '(menu-item "Tex Print (alt printer)" tex-alt-print |
674 | :enable (stringp tex-print-file))) | |
675 | (define-key keymap [menu-bar tex tex-print] | |
676 | '(menu-item "Tex Print" tex-print :enable (stringp tex-print-file))) | |
677 | (define-key keymap [menu-bar tex tex-view] | |
678 | '(menu-item "Tex View" tex-view :enable (stringp tex-print-file)))) | |
679 | ||
680 | (defvar tex-mode-map | |
681 | (let ((map (make-sparse-keymap))) | |
15ca8de7 | 682 | (set-keymap-parent map text-mode-map) |
bdbd9606 SM |
683 | (tex-define-common-keys map) |
684 | (define-key map "\"" 'tex-insert-quote) | |
685 | (define-key map "(" 'skeleton-pair-insert-maybe) | |
686 | (define-key map "{" 'skeleton-pair-insert-maybe) | |
687 | (define-key map "[" 'skeleton-pair-insert-maybe) | |
688 | (define-key map "$" 'skeleton-pair-insert-maybe) | |
689 | (define-key map "\n" 'tex-terminate-paragraph) | |
690 | (define-key map "\M-\r" 'latex-insert-item) | |
691 | (define-key map "\C-c}" 'up-list) | |
692 | (define-key map "\C-c{" 'tex-insert-braces) | |
693 | (define-key map "\C-c\C-r" 'tex-region) | |
694 | (define-key map "\C-c\C-b" 'tex-buffer) | |
695 | (define-key map "\C-c\C-f" 'tex-file) | |
75035a80 | 696 | (define-key map "\C-c\C-c" 'tex-compile) |
bdbd9606 | 697 | (define-key map "\C-c\C-i" 'tex-bibtex-file) |
7afecb99 SM |
698 | (define-key map "\C-c\C-o" 'latex-insert-block) |
699 | (define-key map "\C-c\C-e" 'latex-close-block) | |
bdbd9606 SM |
700 | (define-key map "\C-c\C-u" 'tex-goto-last-unclosed-latex-block) |
701 | (define-key map "\C-c\C-m" 'tex-feed-input) | |
702 | (define-key map [(control return)] 'tex-feed-input) | |
703 | (define-key map [menu-bar tex tex-bibtex-file] | |
704 | '("BibTeX File" . tex-bibtex-file)) | |
705 | (define-key map [menu-bar tex tex-validate-region] | |
706 | '(menu-item "Validate Region" tex-validate-region :enable mark-active)) | |
707 | (define-key map [menu-bar tex tex-validate-buffer] | |
708 | '("Validate Buffer" . tex-validate-buffer)) | |
709 | (define-key map [menu-bar tex tex-region] | |
710 | '(menu-item "TeX Region" tex-region :enable mark-active)) | |
711 | (define-key map [menu-bar tex tex-buffer] | |
712 | '("TeX Buffer" . tex-buffer)) | |
713 | (define-key map [menu-bar tex tex-file] '("TeX File" . tex-file)) | |
714 | map) | |
15ca8de7 SM |
715 | "Keymap shared by TeX modes.") |
716 | ||
717 | (defvar latex-mode-map | |
718 | (let ((map (make-sparse-keymap))) | |
719 | (set-keymap-parent map tex-mode-map) | |
a3d80d4a | 720 | (define-key map "\C-c\C-s" 'latex-split-block) |
15ca8de7 SM |
721 | map) |
722 | "Keymap for `latex-mode'. See also `tex-mode-map'.") | |
723 | ||
724 | (defvar plain-tex-mode-map | |
725 | (let ((map (make-sparse-keymap))) | |
726 | (set-keymap-parent map tex-mode-map) | |
727 | map) | |
728 | "Keymap for `plain-tex-mode'. See also `tex-mode-map'.") | |
cf6d6e8a | 729 | |
d6709b80 SM |
730 | (defvar tex-shell-map |
731 | (let ((m (make-sparse-keymap))) | |
732 | (set-keymap-parent m shell-mode-map) | |
733 | (tex-define-common-keys m) | |
734 | m) | |
81c735c0 | 735 | "Keymap for the TeX shell. |
dca5ea48 | 736 | Inherits `shell-mode-map' with a few additions.") |
869bff31 | 737 | |
6da9bbd6 RS |
738 | (defvar tex-face-alist |
739 | '((bold . "{\\bf ") | |
740 | (italic . "{\\it ") | |
741 | (bold-italic . "{\\bi ") ; hypothetical | |
742 | (underline . "\\underline{") | |
743 | (default . "{\\rm ")) | |
744 | "Alist of face and TeX font name for facemenu.") | |
745 | ||
746 | (defvar tex-latex-face-alist | |
747 | `((italic . "{\\em ") | |
748 | ,@tex-face-alist) | |
749 | "Alist of face and LaTeX font name for facemenu.") | |
750 | ||
15ca8de7 SM |
751 | ;; This would be a lot simpler if we just used a regexp search, |
752 | ;; but then it would be too slow. | |
a3d80d4a | 753 | (defun tex-guess-mode () |
d6709b80 | 754 | (let ((mode tex-default-mode) slash comment) |
869bff31 | 755 | (save-excursion |
756 | (goto-char (point-min)) | |
757 | (while (and (setq slash (search-forward "\\" nil t)) | |
758 | (setq comment (let ((search-end (point))) | |
759 | (save-excursion | |
760 | (beginning-of-line) | |
761 | (search-forward "%" search-end t)))))) | |
bdbd9606 SM |
762 | (when (and slash (not comment)) |
763 | (setq mode | |
764 | (if (looking-at | |
765 | (eval-when-compile | |
766 | (concat | |
767 | (regexp-opt '("documentstyle" "documentclass" | |
95a045bd | 768 | "begin" "subsection" "section" |
bca7a08b SM |
769 | "part" "chapter" "newcommand" |
770 | "renewcommand") 'words) | |
bdbd9606 SM |
771 | "\\|NeedsTeXFormat{LaTeX"))) |
772 | (if (looking-at | |
773 | "document\\(style\\|class\\)\\(\\[.*\\]\\)?{slides}") | |
774 | 'slitex-mode | |
775 | 'latex-mode) | |
776 | 'plain-tex-mode)))) | |
d6709b80 | 777 | (funcall mode))) |
e4c8c838 | 778 | |
a3d80d4a SM |
779 | ;; `tex-mode' plays two roles: it's the parent of several sub-modes |
780 | ;; but it's also the function that chooses between those submodes. | |
781 | ;; To tell the difference between those two cases where the function | |
782 | ;; might be called, we check `delay-mode-hooks'. | |
a3d80d4a SM |
783 | (define-derived-mode tex-mode text-mode "generic-TeX" |
784 | (tex-common-initialization)) | |
bca7a08b SM |
785 | ;; We now move the function and define it again. This gives a warning |
786 | ;; in the byte-compiler :-( but it's difficult to avoid because | |
787 | ;; `define-derived-mode' will necessarily define the function once | |
788 | ;; and we need to define it a second time for `autoload' to get the | |
789 | ;; proper docstring. | |
790 | (defalias 'tex-mode-internal (symbol-function 'tex-mode)) | |
791 | ;;;###autoload | |
792 | (defun tex-mode () | |
793 | "Major mode for editing files of input for TeX, LaTeX, or SliTeX. | |
a3d80d4a SM |
794 | Tries to determine (by looking at the beginning of the file) whether |
795 | this file is for plain TeX, LaTeX, or SliTeX and calls `plain-tex-mode', | |
796 | `latex-mode', or `slitex-mode', respectively. If it cannot be determined, | |
797 | such as if there are no commands in the file, the value of `tex-default-mode' | |
798 | says which mode to use." | |
bca7a08b SM |
799 | (interactive) |
800 | (if delay-mode-hooks | |
801 | ;; We're called from one of the children already. | |
802 | (tex-mode-internal) | |
803 | (tex-guess-mode))) | |
a3d80d4a | 804 | |
6503cec3 | 805 | ;;;###autoload |
31e1d920 | 806 | (defalias 'TeX-mode 'tex-mode) |
6503cec3 | 807 | ;;;###autoload |
c3ce7bf4 RS |
808 | (defalias 'plain-TeX-mode 'plain-tex-mode) |
809 | ;;;###autoload | |
31e1d920 | 810 | (defalias 'LaTeX-mode 'latex-mode) |
869bff31 | 811 | |
7229064d | 812 | ;;;###autoload |
a3d80d4a | 813 | (define-derived-mode plain-tex-mode tex-mode "TeX" |
869bff31 | 814 | "Major mode for editing files of input for plain TeX. |
815 | Makes $ and } display the characters they match. | |
816 | Makes \" insert `` when it seems to be the beginning of a quotation, | |
817 | and '' when it appears to be the end; it inserts \" only after a \\. | |
818 | ||
819 | Use \\[tex-region] to run TeX on the current region, plus a \"header\" | |
820 | copied from the top of the file (containing macro definitions, etc.), | |
821 | running TeX under a special subshell. \\[tex-buffer] does the whole buffer. | |
822 | \\[tex-file] saves the buffer and then processes the file. | |
823 | \\[tex-print] prints the .dvi file made by any of these. | |
824 | \\[tex-view] previews the .dvi file made by any of these. | |
825 | \\[tex-bibtex-file] runs bibtex on the file of the current buffer. | |
826 | ||
e6b0b773 | 827 | Use \\[tex-validate-buffer] to check buffer for paragraphs containing |
869bff31 | 828 | mismatched $'s or braces. |
829 | ||
830 | Special commands: | |
15ca8de7 | 831 | \\{plain-tex-mode-map} |
869bff31 | 832 | |
833 | Mode variables: | |
834 | tex-run-command | |
835 | Command string used by \\[tex-region] or \\[tex-buffer]. | |
836 | tex-directory | |
837 | Directory in which to create temporary files for TeX jobs | |
838 | run by \\[tex-region] or \\[tex-buffer]. | |
839 | tex-dvi-print-command | |
840 | Command string used by \\[tex-print] to print a .dvi file. | |
528415e7 RS |
841 | tex-alt-dvi-print-command |
842 | Alternative command string used by \\[tex-print] (when given a prefix | |
843 | argument) to print a .dvi file. | |
869bff31 | 844 | tex-dvi-view-command |
845 | Command string used by \\[tex-view] to preview a .dvi file. | |
846 | tex-show-queue-command | |
847 | Command string used by \\[tex-show-print-queue] to show the print | |
848 | queue that \\[tex-print] put your job on. | |
849 | ||
81c735c0 RS |
850 | Entering Plain-tex mode runs the hook `text-mode-hook', then the hook |
851 | `tex-mode-hook', and finally the hook `plain-tex-mode-hook'. When the | |
852 | special subshell is initiated, the hook `tex-shell-hook' is run." | |
a3d80d4a SM |
853 | (set (make-local-variable 'tex-command) tex-run-command) |
854 | (set (make-local-variable 'tex-start-of-header) "%\\*\\*start of header") | |
855 | (set (make-local-variable 'tex-end-of-header) "%\\*\\*end of header") | |
856 | (set (make-local-variable 'tex-trailer) "\\bye\n")) | |
869bff31 | 857 | |
7229064d | 858 | ;;;###autoload |
a3d80d4a | 859 | (define-derived-mode latex-mode tex-mode "LaTeX" |
869bff31 | 860 | "Major mode for editing files of input for LaTeX. |
861 | Makes $ and } display the characters they match. | |
862 | Makes \" insert `` when it seems to be the beginning of a quotation, | |
863 | and '' when it appears to be the end; it inserts \" only after a \\. | |
864 | ||
865 | Use \\[tex-region] to run LaTeX on the current region, plus the preamble | |
866 | copied from the top of the file (containing \\documentstyle, etc.), | |
867 | running LaTeX under a special subshell. \\[tex-buffer] does the whole buffer. | |
868 | \\[tex-file] saves the buffer and then processes the file. | |
869 | \\[tex-print] prints the .dvi file made by any of these. | |
870 | \\[tex-view] previews the .dvi file made by any of these. | |
871 | \\[tex-bibtex-file] runs bibtex on the file of the current buffer. | |
872 | ||
e6b0b773 | 873 | Use \\[tex-validate-buffer] to check buffer for paragraphs containing |
869bff31 | 874 | mismatched $'s or braces. |
875 | ||
876 | Special commands: | |
15ca8de7 | 877 | \\{latex-mode-map} |
869bff31 | 878 | |
879 | Mode variables: | |
880 | latex-run-command | |
881 | Command string used by \\[tex-region] or \\[tex-buffer]. | |
882 | tex-directory | |
883 | Directory in which to create temporary files for LaTeX jobs | |
884 | run by \\[tex-region] or \\[tex-buffer]. | |
885 | tex-dvi-print-command | |
886 | Command string used by \\[tex-print] to print a .dvi file. | |
528415e7 RS |
887 | tex-alt-dvi-print-command |
888 | Alternative command string used by \\[tex-print] (when given a prefix | |
889 | argument) to print a .dvi file. | |
869bff31 | 890 | tex-dvi-view-command |
891 | Command string used by \\[tex-view] to preview a .dvi file. | |
892 | tex-show-queue-command | |
893 | Command string used by \\[tex-show-print-queue] to show the print | |
894 | queue that \\[tex-print] put your job on. | |
895 | ||
bed1b0c8 | 896 | Entering Latex mode runs the hook `text-mode-hook', then |
81c735c0 RS |
897 | `tex-mode-hook', and finally `latex-mode-hook'. When the special |
898 | subshell is initiated, `tex-shell-hook' is run." | |
a3d80d4a SM |
899 | (set (make-local-variable 'tex-command) latex-run-command) |
900 | (set (make-local-variable 'tex-start-of-header) | |
901 | "\\\\document\\(style\\|class\\)") | |
902 | (set (make-local-variable 'tex-end-of-header) "\\\\begin\\s-*{document}") | |
903 | (set (make-local-variable 'tex-trailer) "\\end\\s-*{document}\n") | |
ab2c9f54 RS |
904 | ;; A line containing just $$ is treated as a paragraph separator. |
905 | ;; A line starting with $$ starts a paragraph, | |
906 | ;; but does not separate paragraphs if it has more stuff on it. | |
d6709b80 | 907 | (setq paragraph-start |
a3d80d4a | 908 | (concat "[ \t]*\\(\\$\\$\\|" |
d6709b80 SM |
909 | "\\\\[][]\\|" |
910 | "\\\\" (regexp-opt (append | |
911 | (mapcar 'car latex-section-alist) | |
912 | '("begin" "label" "end" | |
913 | "item" "bibitem" "newline" "noindent" | |
914 | "newpage" "footnote" "marginpar" | |
915 | "parbox" "caption")) t) | |
916 | "\\>\\|\\\\[a-z]*" (regexp-opt '("space" "skip" "page") t) | |
dd459839 | 917 | "\\>\\)")) |
d6709b80 | 918 | (setq paragraph-separate |
dd459839 | 919 | (concat "[\f%]\\|[ \t]*\\($\\|" |
d6709b80 SM |
920 | "\\\\[][]\\|" |
921 | "\\\\" (regexp-opt (append | |
922 | (mapcar 'car latex-section-alist) | |
923 | '("begin" "label" "end" )) t) | |
924 | "\\>\\|\\\\\\(" (regexp-opt '("item" "bibitem" "newline" | |
925 | "noindent" "newpage" "footnote" | |
926 | "marginpar" "parbox" "caption")) | |
dd459839 SM |
927 | "\\|\\$\\$\\|[a-z]*\\(space\\|skip\\|page[a-z]*\\)" |
928 | "\\>\\)[ \t]*\\($\\|%\\)\\)")) | |
d6709b80 SM |
929 | (set (make-local-variable 'imenu-create-index-function) |
930 | 'latex-imenu-create-index) | |
931 | (set (make-local-variable 'tex-face-alist) tex-latex-face-alist) | |
95a045bd | 932 | (add-hook 'fill-nobreak-predicate 'latex-fill-nobreak-predicate nil t) |
ea590e1c | 933 | (set (make-local-variable 'indent-line-function) 'latex-indent) |
dd459839 | 934 | (set (make-local-variable 'fill-indent-according-to-mode) t) |
d6709b80 SM |
935 | (set (make-local-variable 'outline-regexp) latex-outline-regexp) |
936 | (set (make-local-variable 'outline-level) 'latex-outline-level) | |
53c4fe47 | 937 | (set (make-local-variable 'forward-sexp-function) 'latex-forward-sexp) |
a3d80d4a | 938 | (set (make-local-variable 'skeleton-end-hook) nil)) |
869bff31 | 939 | |
bd2f2323 | 940 | ;;;###autoload |
d6709b80 | 941 | (define-derived-mode slitex-mode latex-mode "SliTeX" |
869bff31 | 942 | "Major mode for editing files of input for SliTeX. |
943 | Makes $ and } display the characters they match. | |
944 | Makes \" insert `` when it seems to be the beginning of a quotation, | |
945 | and '' when it appears to be the end; it inserts \" only after a \\. | |
946 | ||
947 | Use \\[tex-region] to run SliTeX on the current region, plus the preamble | |
948 | copied from the top of the file (containing \\documentstyle, etc.), | |
949 | running SliTeX under a special subshell. \\[tex-buffer] does the whole buffer. | |
950 | \\[tex-file] saves the buffer and then processes the file. | |
951 | \\[tex-print] prints the .dvi file made by any of these. | |
952 | \\[tex-view] previews the .dvi file made by any of these. | |
953 | \\[tex-bibtex-file] runs bibtex on the file of the current buffer. | |
954 | ||
e6b0b773 | 955 | Use \\[tex-validate-buffer] to check buffer for paragraphs containing |
869bff31 | 956 | mismatched $'s or braces. |
957 | ||
958 | Special commands: | |
15ca8de7 | 959 | \\{slitex-mode-map} |
869bff31 | 960 | |
961 | Mode variables: | |
962 | slitex-run-command | |
963 | Command string used by \\[tex-region] or \\[tex-buffer]. | |
964 | tex-directory | |
965 | Directory in which to create temporary files for SliTeX jobs | |
966 | run by \\[tex-region] or \\[tex-buffer]. | |
967 | tex-dvi-print-command | |
968 | Command string used by \\[tex-print] to print a .dvi file. | |
528415e7 RS |
969 | tex-alt-dvi-print-command |
970 | Alternative command string used by \\[tex-print] (when given a prefix | |
971 | argument) to print a .dvi file. | |
869bff31 | 972 | tex-dvi-view-command |
973 | Command string used by \\[tex-view] to preview a .dvi file. | |
974 | tex-show-queue-command | |
975 | Command string used by \\[tex-show-print-queue] to show the print | |
976 | queue that \\[tex-print] put your job on. | |
977 | ||
81c735c0 RS |
978 | Entering SliTeX mode runs the hook `text-mode-hook', then the hook |
979 | `tex-mode-hook', then the hook `latex-mode-hook', and finally the hook | |
980 | `slitex-mode-hook'. When the special subshell is initiated, the hook | |
981 | `tex-shell-hook' is run." | |
869bff31 | 982 | (setq tex-command slitex-run-command) |
d6709b80 | 983 | (setq tex-start-of-header "\\\\documentstyle{slides}\\|\\\\documentclass{slides}")) |
869bff31 | 984 | |
985 | (defun tex-common-initialization () | |
822eddf4 | 986 | ;; Regexp isearch should accept newline and formfeed as whitespace. |
d6709b80 | 987 | (set (make-local-variable 'search-whitespace-regexp) "[ \t\r\n\f]+") |
64db2461 | 988 | ;; A line containing just $$ is treated as a paragraph separator. |
d6709b80 SM |
989 | (set (make-local-variable 'paragraph-start) |
990 | "[ \t]*$\\|[\f\\\\%]\\|[ \t]*\\$\\$") | |
ab2c9f54 RS |
991 | ;; A line starting with $$ starts a paragraph, |
992 | ;; but does not separate paragraphs if it has more stuff on it. | |
d6709b80 SM |
993 | (set (make-local-variable 'paragraph-separate) |
994 | "[ \t]*$\\|[\f\\\\%]\\|[ \t]*\\$\\$[ \t]*$") | |
995 | (set (make-local-variable 'comment-start) "%") | |
996 | (set (make-local-variable 'comment-add) 1) | |
997 | (set (make-local-variable 'comment-start-skip) | |
5ae0d697 | 998 | "\\(\\(^\\|[^\\\n]\\)\\(\\\\\\\\\\)*\\)\\(%+ *\\)") |
d6709b80 SM |
999 | (set (make-local-variable 'parse-sexp-ignore-comments) t) |
1000 | (set (make-local-variable 'compare-windows-whitespace) | |
1001 | 'tex-categorize-whitespace) | |
1002 | (set (make-local-variable 'facemenu-add-face-function) | |
1003 | (lambda (face end) | |
1004 | (let ((face-text (cdr (assq face tex-face-alist)))) | |
1005 | (if face-text | |
1006 | face-text | |
1007 | (error "Face %s not configured for %s mode" face mode-name))))) | |
1008 | (set (make-local-variable 'facemenu-end-add-face) "}") | |
1009 | (set (make-local-variable 'facemenu-remove-face-function) t) | |
1010 | (set (make-local-variable 'font-lock-defaults) | |
7afecb99 SM |
1011 | '((tex-font-lock-keywords tex-font-lock-keywords-1 |
1012 | tex-font-lock-keywords-2 tex-font-lock-keywords-3) | |
d6709b80 SM |
1013 | nil nil ((?$ . "\"")) nil |
1014 | ;; Who ever uses that anyway ??? | |
bdbd9606 SM |
1015 | (font-lock-mark-block-function . mark-paragraph) |
1016 | (font-lock-syntactic-face-function | |
7afecb99 SM |
1017 | . tex-font-lock-syntactic-face-function) |
1018 | (font-lock-unfontify-region-function | |
1019 | . tex-font-lock-unfontify-region) | |
1020 | (font-lock-syntactic-keywords | |
1021 | . tex-font-lock-syntactic-keywords) | |
1022 | (parse-sexp-lookup-properties . t))) | |
95a045bd SM |
1023 | ;; TABs in verbatim environments don't do what you think. |
1024 | (set (make-local-variable 'indent-tabs-mode) nil) | |
7afecb99 | 1025 | ;; Other vars that should be buffer-local. |
869bff31 | 1026 | (make-local-variable 'tex-command) |
1027 | (make-local-variable 'tex-start-of-header) | |
1028 | (make-local-variable 'tex-end-of-header) | |
1029 | (make-local-variable 'tex-trailer)) | |
1030 | ||
869bff31 | 1031 | (defun tex-categorize-whitespace (backward-limit) |
1032 | ;; compare-windows-whitespace is set to this. | |
1033 | ;; This is basically a finite-state machine. | |
1034 | ;; Returns a symbol telling how TeX would treat | |
1035 | ;; the whitespace we are looking at: null, space, or par. | |
1036 | (let ((category 'null) | |
1037 | (not-finished t)) | |
1038 | (skip-chars-backward " \t\n\f" backward-limit) | |
1039 | (while not-finished | |
1040 | (cond ((looking-at "[ \t]+") | |
1041 | (goto-char (match-end 0)) | |
2be5fefb | 1042 | (if (eq category 'null) |
869bff31 | 1043 | (setq category 'space))) |
1044 | ((looking-at "\n") | |
2be5fefb | 1045 | (cond ((eq category 'newline) |
869bff31 | 1046 | (setq category 'par) |
1047 | (setq not-finished nil)) | |
1048 | (t | |
1049 | (setq category 'newline) ;a strictly internal state | |
1050 | (goto-char (match-end 0))))) | |
1051 | ((looking-at "\f+") | |
1052 | (setq category 'par) | |
1053 | (setq not-finished nil)) | |
1054 | (t | |
1055 | (setq not-finished nil)))) | |
1056 | (skip-chars-forward " \t\n\f") | |
2be5fefb | 1057 | (if (eq category 'newline) |
869bff31 | 1058 | 'space ;TeX doesn't distinguish |
1059 | category))) | |
1060 | ||
1061 | (defun tex-insert-quote (arg) | |
1062 | "Insert the appropriate quote marks for TeX. | |
08348502 RS |
1063 | Inserts the value of `tex-open-quote' (normally ``) or `tex-close-quote' |
1064 | \(normally '') depending on the context. With prefix argument, always | |
869bff31 | 1065 | inserts \" characters." |
528415e7 | 1066 | (interactive "*P") |
95a045bd SM |
1067 | (if (or arg (memq (char-syntax (preceding-char)) '(?/ ?\\)) |
1068 | (eq (get-text-property (point) 'face) 'tex-verbatim-face) | |
1069 | (save-excursion | |
1070 | (backward-char (length tex-open-quote)) | |
1071 | (when (or (looking-at (regexp-quote tex-open-quote)) | |
1072 | (looking-at (regexp-quote tex-close-quote))) | |
1073 | (delete-char (length tex-open-quote)) | |
1074 | t))) | |
869bff31 | 1075 | (self-insert-command (prefix-numeric-value arg)) |
95a045bd SM |
1076 | (insert (if (memq (char-syntax (preceding-char)) '(?\( ?> ?\ )) |
1077 | tex-open-quote tex-close-quote)))) | |
869bff31 | 1078 | |
e6b0b773 MR |
1079 | (defun tex-validate-buffer () |
1080 | "Check current buffer for paragraphs containing mismatched braces or $s. | |
bbd93e41 | 1081 | Their positions are recorded in the buffer `*Occur*'. |
e6b0b773 MR |
1082 | To find a particular invalidity from `*Occur*', switch to that buffer |
1083 | and type C-c C-c or click with mouse-2 | |
1084 | on the line for the invalidity you want to see." | |
869bff31 | 1085 | (interactive) |
bbd93e41 RS |
1086 | (let ((buffer (current-buffer)) |
1087 | (prevpos (point-min)) | |
e6b0b773 MR |
1088 | (linenum nil) |
1089 | (num-matches 0)) | |
bbd93e41 RS |
1090 | (with-output-to-temp-buffer "*Occur*" |
1091 | (princ "Mismatches:\n") | |
a3d80d4a | 1092 | (with-current-buffer standard-output |
bbd93e41 | 1093 | (occur-mode) |
52ab062c CW |
1094 | ;; This won't actually work...Really, this whole thing should |
1095 | ;; be rewritten instead of being a hack on top of occur. | |
1096 | (setq occur-revert-arguments (list nil 0 (list buffer)))) | |
bbd93e41 RS |
1097 | (save-excursion |
1098 | (goto-char (point-max)) | |
52ab062c | 1099 | (while (and (not (bobp))) |
710a0f53 KH |
1100 | (let ((end (point)) |
1101 | prev-end) | |
bbd93e41 | 1102 | ;; Scan the previous paragraph for invalidities. |
710a0f53 KH |
1103 | (if (search-backward "\n\n" nil t) |
1104 | (progn | |
1105 | (setq prev-end (point)) | |
1106 | (forward-char 2)) | |
1107 | (goto-char (setq prev-end (point-min)))) | |
869bff31 | 1108 | (or (tex-validate-region (point) end) |
a3d80d4a | 1109 | (let* ((end (line-beginning-position 2)) |
bbd93e41 RS |
1110 | start tem) |
1111 | (beginning-of-line) | |
1112 | (setq start (point)) | |
1113 | ;; Keep track of line number as we scan, | |
1114 | ;; in a cumulative fashion. | |
1115 | (if linenum | |
1116 | (setq linenum (- linenum (count-lines prevpos (point)))) | |
1117 | (setq linenum (1+ (count-lines 1 start)))) | |
1118 | (setq prevpos (point)) | |
bed1b0c8 | 1119 | ;; Mention this mismatch in *Occur*. |
bbd93e41 | 1120 | ;; Since we scan from end of buffer to beginning, |
e6b0b773 | 1121 | ;; add each mismatch at the beginning of *Occur*. |
bbd93e41 RS |
1122 | (save-excursion |
1123 | (setq tem (point-marker)) | |
1124 | (set-buffer standard-output) | |
1125 | (goto-char (point-min)) | |
1126 | ;; Skip "Mismatches:" header line. | |
1127 | (forward-line 1) | |
e6b0b773 | 1128 | (setq num-matches (1+ num-matches)) |
bbd93e41 | 1129 | (insert-buffer-substring buffer start end) |
e6b0b773 MR |
1130 | (let (text-beg (text-end (point-marker))) |
1131 | (forward-char (- start end)) | |
1132 | (setq text-beg (point-marker)) | |
1133 | (insert (format "%3d: " linenum)) | |
a865de85 EZ |
1134 | (add-text-properties |
1135 | text-beg (- text-end 1) | |
1136 | '(mouse-face highlight | |
1137 | help-echo "mouse-2: go to this invalidity")) | |
1598a961 | 1138 | (put-text-property text-beg (- text-end 1) |
52ab062c | 1139 | 'occur-target tem))))) |
710a0f53 | 1140 | (goto-char prev-end)))) |
bdbd9606 | 1141 | (with-current-buffer standard-output |
e6b0b773 | 1142 | (if (eq num-matches 0) |
bbd93e41 RS |
1143 | (insert "None!\n")) |
1144 | (if (interactive-p) | |
e6b0b773 | 1145 | (message "%d mismatches found" num-matches)))))) |
869bff31 | 1146 | |
1147 | (defun tex-validate-region (start end) | |
1148 | "Check for mismatched braces or $'s in region. | |
1149 | Returns t if no mismatches. Returns nil and moves point to suspect | |
1150 | area if a mismatch is found." | |
1151 | (interactive "r") | |
1152 | (let ((failure-point nil) (max-possible-sexps (- end start))) | |
1153 | (save-excursion | |
1154 | (condition-case () | |
1155 | (save-restriction | |
1156 | (narrow-to-region start end) | |
2ed2806c | 1157 | ;; First check that the open and close parens balance in numbers. |
869bff31 | 1158 | (goto-char start) |
e6b0b773 | 1159 | (while (<= 0 (setq max-possible-sexps (1- max-possible-sexps))) |
2ed2806c KH |
1160 | (forward-sexp 1)) |
1161 | ;; Now check that like matches like. | |
1162 | (goto-char start) | |
1163 | (while (progn (skip-syntax-forward "^(") | |
1164 | (not (eobp))) | |
1165 | (let ((match (matching-paren (following-char)))) | |
1166 | (save-excursion | |
1167 | (forward-sexp 1) | |
1168 | (or (= (preceding-char) match) | |
1169 | (error "Mismatched parentheses")))) | |
1170 | (forward-char 1))) | |
869bff31 | 1171 | (error |
bed1b0c8 RS |
1172 | (skip-syntax-forward " .>") |
1173 | (setq failure-point (point))))) | |
bdbd9606 SM |
1174 | (if failure-point (goto-char failure-point)) |
1175 | (not failure-point))) | |
869bff31 | 1176 | |
1177 | (defun tex-terminate-paragraph (inhibit-validation) | |
1178 | "Insert two newlines, breaking a paragraph for TeX. | |
81c735c0 | 1179 | Check for mismatched braces or $s in paragraph being terminated. |
869bff31 | 1180 | A prefix arg inhibits the checking." |
528415e7 | 1181 | (interactive "*P") |
869bff31 | 1182 | (or inhibit-validation |
1183 | (save-excursion | |
1184 | (tex-validate-region | |
bed1b0c8 RS |
1185 | (save-excursion |
1186 | (search-backward "\n\n" nil 'move) | |
1187 | (point)) | |
1188 | (point))) | |
869bff31 | 1189 | (message "Paragraph being closed appears to contain a mismatch")) |
1190 | (insert "\n\n")) | |
1191 | ||
7afecb99 | 1192 | (define-skeleton tex-insert-braces |
869bff31 | 1193 | "Make a pair of braces and be poised to type inside of them." |
7afecb99 SM |
1194 | nil |
1195 | ?\{ _ ?}) | |
869bff31 | 1196 | |
9bd4f69c RS |
1197 | ;; This function is used as the value of fill-nobreak-predicate |
1198 | ;; in LaTeX mode. Its job is to prevent line-breaking inside | |
1199 | ;; of a \verb construct. | |
1200 | (defun latex-fill-nobreak-predicate () | |
7afecb99 SM |
1201 | (save-excursion |
1202 | (skip-chars-backward " ") | |
1203 | ;; Don't break after \ since `\ ' has special meaning. | |
1204 | (or (and (not (bobp)) (memq (char-syntax (char-before)) '(?\\ ?/))) | |
1205 | (let ((opoint (point)) | |
1206 | inside) | |
1207 | (beginning-of-line) | |
1208 | (while (re-search-forward "\\\\verb\\(.\\)" opoint t) | |
1209 | (unless (re-search-forward (regexp-quote (match-string 1)) opoint t) | |
1210 | (setq inside t))) | |
1211 | inside)))) | |
9bd4f69c | 1212 | |
53c4fe47 SM |
1213 | (defvar latex-block-default "enumerate") |
1214 | ||
8084f5d8 SM |
1215 | (defvar latex-block-args-alist |
1216 | '(("array" nil ?\{ (skeleton-read "[options]: ") ?\}) | |
1217 | ("tabular" nil ?\{ (skeleton-read "[options]: ") ?\})) | |
1218 | "Skeleton element to use for arguments to particular environments. | |
1219 | Every element of the list has the form (NAME . SKEL-ELEM) where NAME is | |
1220 | the name of the environment and SKEL-ELEM is an element to use in | |
1221 | a skeleton (see `skeleton-insert').") | |
1222 | ||
1223 | (defvar latex-block-body-alist | |
1224 | '(("enumerate" nil '(latex-insert-item) > _) | |
1225 | ("itemize" nil '(latex-insert-item) > _) | |
1226 | ("table" nil "\\caption{" > - "}" > \n _) | |
1227 | ("figure" nil > _ \n "\\caption{" > _ "}" >)) | |
1228 | "Skeleton element to use for the body of particular environments. | |
1229 | Every element of the list has the form (NAME . SKEL-ELEM) where NAME is | |
1230 | the name of the environment and SKEL-ELEM is an element to use in | |
1231 | a skeleton (see `skeleton-insert').") | |
1232 | ||
15ca8de7 | 1233 | ;; Like tex-insert-braces, but for LaTeX. |
7afecb99 SM |
1234 | (defalias 'tex-latex-block 'latex-insert-block) |
1235 | (define-skeleton latex-insert-block | |
cf149bdf | 1236 | "Create a matching pair of lines \\begin[OPT]{NAME} and \\end{NAME} at point. |
869bff31 | 1237 | Puts point on a blank line between them." |
53c4fe47 SM |
1238 | (let ((choice (completing-read (format "LaTeX block name [%s]: " |
1239 | latex-block-default) | |
95a045bd | 1240 | (append latex-block-names |
7afecb99 | 1241 | latex-standard-block-names) |
53c4fe47 SM |
1242 | nil nil nil nil latex-block-default))) |
1243 | (setq latex-block-default choice) | |
7afecb99 | 1244 | (unless (or (member choice latex-standard-block-names) |
53c4fe47 SM |
1245 | (member choice latex-block-names)) |
1246 | ;; Remember new block names for later completion. | |
1247 | (push choice latex-block-names)) | |
1248 | choice) | |
95a045bd | 1249 | \n "\\begin{" str "}" |
8084f5d8 SM |
1250 | (cdr (assoc str latex-block-args-alist)) |
1251 | > \n (or (cdr (assoc str latex-block-body-alist)) '(nil > _)) \n | |
95a045bd | 1252 | "\\end{" str "}" > \n) |
53c4fe47 | 1253 | |
ea590e1c SM |
1254 | (define-skeleton latex-insert-item |
1255 | "Insert a \item macro." | |
1256 | nil | |
15ca8de7 | 1257 | \n "\\item " >) |
ea590e1c | 1258 | |
53c4fe47 SM |
1259 | \f |
1260 | ;;;; | |
1261 | ;;;; LaTeX syntax navigation | |
1262 | ;;;; | |
869bff31 | 1263 | |
1264 | (defun tex-last-unended-begin () | |
81c735c0 | 1265 | "Leave point at the beginning of the last `\\begin{...}' that is unended." |
95a045bd SM |
1266 | (condition-case nil |
1267 | (while (and (re-search-backward "\\\\\\(begin\\|end\\)\\s *{") | |
1268 | (looking-at "\\\\end")) | |
1269 | (tex-last-unended-begin)) | |
1270 | (search-failed (error "Couldn't find unended \\begin")))) | |
869bff31 | 1271 | |
53c4fe47 SM |
1272 | (defun tex-next-unmatched-end () |
1273 | "Leave point at the end of the next `\\end' that is unended." | |
1274 | (while (and (re-search-forward "\\\\\\(begin\\|end\\)\\s *{[^}]+}") | |
ea590e1c SM |
1275 | (save-excursion (goto-char (match-beginning 0)) |
1276 | (looking-at "\\\\begin"))) | |
53c4fe47 SM |
1277 | (tex-next-unmatched-end))) |
1278 | ||
6a900cf1 ER |
1279 | (defun tex-goto-last-unclosed-latex-block () |
1280 | "Move point to the last unclosed \\begin{...}. | |
1281 | Mark is left at original location." | |
1282 | (interactive) | |
1283 | (let ((spot)) | |
1284 | (save-excursion | |
95a045bd | 1285 | (tex-last-unended-begin) |
6a900cf1 ER |
1286 | (setq spot (point))) |
1287 | (push-mark) | |
1288 | (goto-char spot))) | |
1289 | ||
53c4fe47 SM |
1290 | (defun latex-backward-sexp-1 () |
1291 | "Like (backward-sexp 1) but aware of multi-char elements." | |
1292 | (let ((pos (point)) | |
1293 | (forward-sexp-function)) | |
1294 | (backward-sexp 1) | |
1295 | (if (looking-at "\\\\begin\\>") | |
1296 | (signal 'scan-error | |
1297 | (list "Containing expression ends prematurely" | |
1298 | (point) (prog1 (point) (goto-char pos)))) | |
1299 | (when (eq (char-after) ?{) | |
1300 | (let ((newpos (point))) | |
1301 | (when (ignore-errors (backward-sexp 1) t) | |
1302 | (if (looking-at "\\\\end\\>") | |
1303 | (tex-last-unended-begin) | |
1304 | (goto-char newpos)))))))) | |
1305 | ||
1306 | (defun latex-forward-sexp-1 () | |
1307 | "Like (forward-sexp 1) but aware of multi-char elements." | |
1308 | (let ((pos (point)) | |
1309 | (forward-sexp-function)) | |
1310 | (forward-sexp 1) | |
1311 | (let ((newpos (point))) | |
1312 | (skip-syntax-backward "/w") | |
1313 | (cond | |
1314 | ((looking-at "\\\\end\\>") | |
1315 | (signal 'scan-error | |
1316 | (list "Containing expression ends prematurely" | |
1317 | (point) | |
1318 | (prog1 | |
1319 | (progn (ignore-errors (forward-sexp 2)) (point)) | |
1320 | (goto-char pos))))) | |
1321 | ((looking-at "\\\\begin\\>") | |
1322 | (goto-char (match-end 0)) | |
1323 | (tex-next-unmatched-end)) | |
1324 | (t (goto-char newpos)))))) | |
1325 | ||
1326 | (defun latex-forward-sexp (&optional arg) | |
1327 | "Like `forward-sexp' but aware of multi-char elements." | |
1328 | (interactive "P") | |
1329 | (unless arg (setq arg 1)) | |
1330 | (let ((pos (point))) | |
1331 | (condition-case err | |
1332 | (while (/= arg 0) | |
1333 | (setq arg | |
1334 | (if (> arg 0) | |
1335 | (progn (latex-forward-sexp-1) (1- arg)) | |
1336 | (progn (latex-backward-sexp-1) (1+ arg))))) | |
1337 | (scan-error | |
1338 | (goto-char pos) | |
1339 | (signal (car err) (cdr err)))))) | |
1340 | ||
ea590e1c SM |
1341 | (defun latex-syntax-after () |
1342 | "Like (char-syntax (char-after)) but aware of multi-char elements." | |
15ca8de7 | 1343 | (if (looking-at "\\\\end\\>") ?\) (char-syntax (following-char)))) |
ea590e1c SM |
1344 | |
1345 | (defun latex-skip-close-parens () | |
1346 | "Like (skip-syntax-forward \" )\") but aware of multi-char elements." | |
1347 | (let ((forward-sexp-function nil)) | |
1348 | (while (progn (skip-syntax-forward " )") | |
1349 | (looking-at "\\\\end\\>")) | |
1350 | (forward-sexp 2)))) | |
1351 | ||
1352 | (defun latex-down-list () | |
1353 | "Like (down-list 1) but aware of multi-char elements." | |
1354 | (forward-comment (point-max)) | |
1355 | (let ((forward-sexp-function nil)) | |
1356 | (if (not (looking-at "\\\\begin\\>")) | |
1357 | (down-list 1) | |
1358 | (forward-sexp 1) | |
1359 | ;; Skip arguments. | |
7afecb99 SM |
1360 | (while (looking-at "[ \t]*[[{(]") |
1361 | (with-syntax-table tex-mode-syntax-table | |
1362 | (forward-sexp)))))) | |
ea590e1c | 1363 | |
7afecb99 SM |
1364 | (defalias 'tex-close-latex-block 'latex-close-block) |
1365 | (define-skeleton latex-close-block | |
1366 | "Create an \\end{...} to match the last unclosed \\begin{...}." | |
1367 | (save-excursion | |
1368 | (tex-last-unended-begin) | |
1369 | (if (not (looking-at "\\\\begin\\(\\s *{[^}\n]*}\\)")) '("{" _ "}") | |
1370 | (match-string 1))) | |
1371 | \n "\\end" str > \n) | |
1372 | ||
1373 | (define-skeleton latex-split-block | |
1374 | "Split the enclosing environment by inserting \\end{..}\\begin{..} at point." | |
1375 | (save-excursion | |
1376 | (tex-last-unended-begin) | |
1377 | (if (not (looking-at "\\\\begin\\(\\s *{[^}\n]*}\\)")) '("{" _ "}") | |
1378 | (prog1 (match-string 1) | |
1379 | (goto-char (match-end 1)) | |
1380 | (setq v1 (buffer-substring (point) | |
1381 | (progn | |
1382 | (while (looking-at "[ \t]*[[{]") | |
1383 | (forward-sexp 1)) | |
1384 | (point))))))) | |
1385 | \n "\\end" str > \n _ \n "\\begin" str v1 > \n) | |
15ca8de7 SM |
1386 | |
1387 | (defconst tex-discount-args-cmds | |
1388 | '("begin" "end" "input" "special" "cite" "ref" "include" "includeonly" | |
1389 | "documentclass" "usepackage" "label") | |
1390 | "TeX commands whose arguments should not be counted as text.") | |
1391 | ||
1392 | (defun tex-count-words (begin end) | |
1393 | "Count the number of words in the buffer." | |
1394 | (interactive | |
1395 | (if (and transient-mark-mode mark-active) | |
1396 | (list (region-beginning) (region-end)) | |
1397 | (list (point-min) (point-max)))) | |
1398 | ;; TODO: skip comments and math and maybe some environments. | |
1399 | (save-excursion | |
1400 | (goto-char begin) | |
1401 | (let ((count 0)) | |
1402 | (while (and (< (point) end) (re-search-forward "\\<" end t)) | |
1403 | (if (not (eq (char-syntax (preceding-char)) ?/)) | |
1404 | (progn | |
1405 | ;; Don't count single-char words. | |
1406 | (unless (looking-at ".\\>") (incf count)) | |
1407 | (forward-char 1)) | |
1408 | (let ((cmd | |
1409 | (buffer-substring-no-properties | |
1410 | (point) (progn (when (zerop (skip-chars-forward "a-zA-Z@")) | |
1411 | (forward-char 1)) | |
1412 | (point))))) | |
1413 | (when (member cmd tex-discount-args-cmds) | |
1414 | (skip-chars-forward "*") | |
1415 | (forward-comment (point-max)) | |
1416 | (when (looking-at "\\[") | |
1417 | (forward-sexp 1) | |
1418 | (forward-comment (point-max))) | |
1419 | (if (not (looking-at "{")) | |
1420 | (forward-char 1) | |
1421 | (forward-sexp 1)))))) | |
1422 | (message "%s words" count)))) | |
b781e739 SS |
1423 | |
1424 | ||
869bff31 | 1425 | \f |
1426 | ;;; Invoking TeX in an inferior shell. | |
1427 | ||
15ca8de7 SM |
1428 | ;; Why use a shell instead of running TeX directly? Because if TeX |
1429 | ;; gets stuck, the user can switch to the shell window and type at it. | |
869bff31 | 1430 | |
15ca8de7 | 1431 | ;; The utility functions: |
869bff31 | 1432 | |
bdbd9606 SM |
1433 | (define-derived-mode tex-shell shell-mode "TeX-Shell" |
1434 | (compilation-shell-minor-mode t)) | |
1435 | ||
d974af30 | 1436 | ;;;###autoload |
869bff31 | 1437 | (defun tex-start-shell () |
d6709b80 SM |
1438 | (with-current-buffer |
1439 | (make-comint | |
1440 | "tex-shell" | |
07c16ec4 | 1441 | (or tex-shell-file-name (getenv "ESHELL") shell-file-name) |
d6709b80 | 1442 | nil) |
528415e7 RS |
1443 | (let ((proc (get-process "tex-shell"))) |
1444 | (set-process-sentinel proc 'tex-shell-sentinel) | |
1445 | (process-kill-without-query proc) | |
bdbd9606 | 1446 | (tex-shell) |
528415e7 | 1447 | (while (zerop (buffer-size)) |
51b2c841 RS |
1448 | (sleep-for 1))))) |
1449 | ||
033306e3 RS |
1450 | (defun tex-feed-input () |
1451 | "Send input to the tex shell process. | |
1452 | In the tex buffer this can be used to continue an interactive tex run. | |
bed1b0c8 | 1453 | In the tex shell buffer this command behaves like `comint-send-input'." |
033306e3 | 1454 | (interactive) |
b781e739 | 1455 | (set-buffer (tex-shell-buf)) |
033306e3 RS |
1456 | (comint-send-input) |
1457 | (tex-recenter-output-buffer nil)) | |
1458 | ||
51b2c841 RS |
1459 | (defun tex-display-shell () |
1460 | "Make the TeX shell buffer visible in a window." | |
b781e739 | 1461 | (display-buffer (tex-shell-buf)) |
51b2c841 | 1462 | (tex-recenter-output-buffer nil)) |
528415e7 RS |
1463 | |
1464 | (defun tex-shell-sentinel (proc msg) | |
1465 | (cond ((null (buffer-name (process-buffer proc))) | |
1466 | ;; buffer killed | |
1467 | (set-process-buffer proc nil) | |
1468 | (tex-delete-last-temp-files)) | |
1469 | ((memq (process-status proc) '(signal exit)) | |
1470 | (tex-delete-last-temp-files)))) | |
1471 | ||
1472 | (defun tex-set-buffer-directory (buffer directory) | |
869bff31 | 1473 | "Set BUFFER's default directory to be DIRECTORY." |
1474 | (setq directory (file-name-as-directory (expand-file-name directory))) | |
1475 | (if (not (file-directory-p directory)) | |
1476 | (error "%s is not a directory" directory) | |
1477 | (save-excursion | |
1478 | (set-buffer buffer) | |
1479 | (setq default-directory directory)))) | |
1480 | ||
30803a05 RS |
1481 | (defvar tex-send-command-modified-tick 0) |
1482 | (make-variable-buffer-local 'tex-send-command-modified-tick) | |
1483 | ||
bdbd9606 | 1484 | (defun tex-shell-proc () |
b781e739 | 1485 | (or (tex-shell-running) (error "No TeX subprocess"))) |
bdbd9606 SM |
1486 | (defun tex-shell-buf () |
1487 | (process-buffer (tex-shell-proc))) | |
b781e739 SS |
1488 | (defun tex-shell-buf-no-error () |
1489 | (let ((proc (tex-shell-running))) | |
1490 | (and proc (process-buffer proc)))) | |
bdbd9606 | 1491 | |
528415e7 | 1492 | (defun tex-send-command (command &optional file background) |
4cdc1d4b | 1493 | "Send COMMAND to TeX shell process, substituting optional FILE for *. |
8241d7b9 ER |
1494 | Do this in background if optional BACKGROUND is t. If COMMAND has no *, |
1495 | FILE will be appended, preceded by a blank, to COMMAND. If FILE is nil, no | |
1496 | substitution will be made in COMMAND. COMMAND can be any expression that | |
460e1b7d RS |
1497 | evaluates to a command string. |
1498 | ||
1499 | Return the process in which TeX is running." | |
528415e7 RS |
1500 | (save-excursion |
1501 | (let* ((cmd (eval command)) | |
bdbd9606 | 1502 | (proc (tex-shell-proc)) |
64db2461 | 1503 | (buf (process-buffer proc)) |
4f45adda | 1504 | (star (string-match "\\*" cmd)) |
4cdc1d4b RS |
1505 | (string |
1506 | (concat | |
1507 | (if file | |
1508 | (if star (concat (substring cmd 0 star) | |
1509 | file (substring cmd (1+ star))) | |
1510 | (concat cmd " " file)) | |
1511 | cmd) | |
1512 | (if background "&" "")))) | |
64db2461 RS |
1513 | ;; Switch to buffer before checking for subproc output in it. |
1514 | (set-buffer buf) | |
30803a05 RS |
1515 | ;; If text is unchanged since previous tex-send-command, |
1516 | ;; we haven't got any output. So wait for output now. | |
64db2461 | 1517 | (if (= (buffer-modified-tick buf) tex-send-command-modified-tick) |
30803a05 | 1518 | (accept-process-output proc)) |
4cdc1d4b RS |
1519 | (goto-char (process-mark proc)) |
1520 | (insert string) | |
30803a05 | 1521 | (comint-send-input) |
460e1b7d RS |
1522 | (setq tex-send-command-modified-tick (buffer-modified-tick buf)) |
1523 | proc))) | |
528415e7 | 1524 | |
a15849cb RS |
1525 | (defun tex-delete-last-temp-files (&optional not-all) |
1526 | "Delete any junk files from last temp file. | |
1527 | If NOT-ALL is non-nil, save the `.dvi' file." | |
528415e7 RS |
1528 | (if tex-last-temp-file |
1529 | (let* ((dir (file-name-directory tex-last-temp-file)) | |
adf6b7f9 KH |
1530 | (list (and (file-directory-p dir) |
1531 | (file-name-all-completions | |
02fd229c RS |
1532 | (file-name-sans-extension |
1533 | (file-name-nondirectory tex-last-temp-file)) | |
1534 | dir)))) | |
adf6b7f9 | 1535 | (while list |
a15849cb RS |
1536 | (if not-all |
1537 | (and | |
1538 | ;; If arg is non-nil, don't delete the .dvi file. | |
1539 | (not (string-match "\\.dvi$" (car list))) | |
1540 | (delete-file (concat dir (car list)))) | |
1541 | (delete-file (concat dir (car list)))) | |
528415e7 RS |
1542 | (setq list (cdr list)))))) |
1543 | ||
99621a14 | 1544 | (add-hook 'kill-emacs-hook 'tex-delete-last-temp-files) |
869bff31 | 1545 | |
75035a80 SM |
1546 | ;; |
1547 | ;; Machinery to guess the command that the user wants to execute. | |
1548 | ;; | |
1549 | ||
1550 | (defvar tex-compile-history nil) | |
1551 | ||
1552 | (defvar tex-input-files-re | |
1553 | (eval-when-compile | |
1554 | (concat "\\." (regexp-opt '("tex" "texi" "texinfo" | |
1555 | "bbl" "ind" "sty" "cls") t) | |
1556 | ;; Include files with no dots (for directories). | |
1557 | "\\'\\|\\`[^.]+\\'"))) | |
1558 | ||
1559 | (defcustom tex-use-reftex t | |
1560 | "If non-nil, use RefTeX's list of files to determine what command to use." | |
1561 | :type 'boolean) | |
1562 | ||
1563 | (defvar tex-compile-commands | |
1564 | '(((concat "pdf" tex-command | |
1565 | " " (shell-quote-argument tex-start-commands) " %f") | |
1566 | t "%r.pdf") | |
1567 | ((concat tex-command | |
1568 | " " (shell-quote-argument tex-start-commands) " %f") | |
1569 | t "%r.dvi") | |
1570 | ("xdvi %r &" "%r.dvi") | |
1571 | ("advi %r &" "%r.dvi") | |
1572 | ("bibtex %r" "%r.aux" "%r.bbl") | |
1573 | ("makeindex %r" "%r.idx" "%r.ind") | |
1574 | ("texindex %r.??") | |
1575 | ("dvipdfm %r" "%r.dvi" "%r.pdf") | |
1576 | ("dvipdf %r" "%r.dvi" "%r.pdf") | |
1577 | ("dvips %r" "%r.dvi" "%r.ps") | |
1578 | ("gv %r.ps &" "%r.ps") | |
1579 | ("gv %r.pdf &" "%r.pdf") | |
1580 | ("xpdf %r.pdf &" "%r.pdf") | |
1581 | ("lpr %r.ps" "%r.ps")) | |
1582 | "List of commands for `tex-compile'. | |
1583 | Each element should be of the form (FORMAT IN OUT) where | |
1584 | FORMAT is an expression that evaluates to a string that can contain | |
1585 | - `%r' the main file name without extension. | |
1586 | - `%f' the main file name. | |
1587 | IN can be either a string (with the same % escapes in it) indicating | |
1588 | the name of the input file, or t to indicate that the input is all | |
1589 | the TeX files of the document, or nil if we don't know. | |
1590 | OUT describes the output file and is either a %-escaped string | |
1591 | or nil to indicate that there is no output file.") | |
1592 | ||
8084f5d8 SM |
1593 | ;; defsubst* gives better byte-code than defsubst. |
1594 | (defsubst* tex-string-prefix-p (str1 str2) | |
1595 | "Return non-nil if STR1 is a prefix of STR2" | |
1596 | (eq t (compare-strings str2 nil (length str1) str1 nil nil))) | |
1597 | ||
dd459839 SM |
1598 | (defun tex-guess-main-file (&optional all) |
1599 | "Find a likely `tex-main-file'. | |
1600 | Looks for hints in other buffers in the same directory or in | |
8084f5d8 SM |
1601 | ALL other buffers. If ALL is `sub' only look at buffers in parent directories |
1602 | of the current buffer." | |
dd459839 SM |
1603 | (let ((dir default-directory) |
1604 | (header-re tex-start-of-header)) | |
1605 | (catch 'found | |
1606 | ;; Look for a buffer with `tex-main-file' set. | |
1607 | (dolist (buf (if (consp all) all (buffer-list))) | |
1608 | (with-current-buffer buf | |
8084f5d8 SM |
1609 | (when (and (cond |
1610 | ((null all) (equal dir default-directory)) | |
1611 | ((eq all 'sub) (tex-string-prefix-p default-directory dir)) | |
1612 | (t)) | |
dd459839 SM |
1613 | (stringp tex-main-file)) |
1614 | (throw 'found (expand-file-name tex-main-file))))) | |
1615 | ;; Look for a buffer containing the magic `tex-start-of-header'. | |
1616 | (dolist (buf (if (consp all) all (buffer-list))) | |
1617 | (with-current-buffer buf | |
8084f5d8 SM |
1618 | (when (and (cond |
1619 | ((null all) (equal dir default-directory)) | |
1620 | ((eq all 'sub) (tex-string-prefix-p default-directory dir)) | |
1621 | (t)) | |
dd459839 SM |
1622 | buffer-file-name |
1623 | ;; (or (easy-mmode-derived-mode-p 'latex-mode) | |
1624 | ;; (easy-mmode-derived-mode-p 'plain-tex-mode)) | |
1625 | (save-excursion | |
cb30255a SM |
1626 | (save-restriction |
1627 | (widen) | |
1628 | (goto-char (point-min)) | |
95a045bd SM |
1629 | (re-search-forward |
1630 | header-re (+ (point) 10000) t)))) | |
dd459839 SM |
1631 | (throw 'found (expand-file-name buffer-file-name)))))))) |
1632 | ||
07c16ec4 SM |
1633 | (defun tex-main-file () |
1634 | "Return the relative name of the main file." | |
bdbd9606 SM |
1635 | (let* ((file (or tex-main-file |
1636 | ;; Compatibility with AUCTeX. | |
1637 | (and (boundp 'TeX-master) (stringp TeX-master) | |
1638 | (set (make-local-variable 'tex-main-file) TeX-master)) | |
1639 | ;; Try to guess the main file. | |
1640 | (if (not buffer-file-name) | |
1641 | (error "Buffer is not associated with any file") | |
1642 | (file-relative-name | |
1643 | (if (save-excursion | |
1644 | (goto-char (point-min)) | |
95a045bd SM |
1645 | (re-search-forward tex-start-of-header |
1646 | (+ (point) 10000) t)) | |
bdbd9606 SM |
1647 | ;; This is the main file. |
1648 | buffer-file-name | |
1649 | ;; This isn't the main file, let's try to find better, | |
1650 | (or (tex-guess-main-file) | |
8084f5d8 | 1651 | (tex-guess-main-file 'sub) |
bdbd9606 | 1652 | ;; (tex-guess-main-file t) |
07c16ec4 SM |
1653 | buffer-file-name))))))) |
1654 | (if (file-exists-p file) file (concat file ".tex")))) | |
dd459839 | 1655 | |
75035a80 SM |
1656 | (defun tex-summarize-command (cmd) |
1657 | (if (not (stringp cmd)) "" | |
1658 | (mapconcat 'identity | |
1659 | (mapcar (lambda (s) (car (split-string s))) | |
1660 | (split-string cmd "\\s-*\\(?:;\\|&&\\)\\s-*")) | |
1661 | "&"))) | |
1662 | ||
1663 | (defun tex-uptodate-p (file) | |
1664 | "Return non-nil if FILE is not uptodate w.r.t the document source files. | |
1665 | FILE is typically the output DVI or PDF file." | |
1666 | ;; We should check all the files included !!! | |
1667 | (and | |
1668 | ;; Clearly, the target must exist. | |
1669 | (file-exists-p file) | |
1670 | ;; And the last run must not have asked for a rerun. | |
1671 | ;; FIXME: this should check that the last run was done on the same file. | |
1672 | (let ((buf (condition-case nil (tex-shell-buf) (error nil)))) | |
1673 | (when buf | |
1674 | (with-current-buffer buf | |
1675 | (save-excursion | |
1676 | (goto-char (point-max)) | |
1677 | (and (re-search-backward | |
1678 | "(see the transcript file for additional information)" nil t) | |
1679 | (> (save-excursion | |
1680 | (or (re-search-backward "\\[[0-9]+\\]" nil t) | |
1681 | (point-min))) | |
1682 | (save-excursion | |
1683 | (or (re-search-backward "Rerun" nil t) | |
1684 | (point-min))))))))) | |
1685 | ;; And the input files must not have been changed in the meantime. | |
1686 | (let ((files (if (and tex-use-reftex | |
1687 | (fboundp 'reftex-scanning-info-available-p) | |
1688 | (reftex-scanning-info-available-p)) | |
1689 | (reftex-all-document-files) | |
1690 | (list (file-name-directory (expand-file-name file))))) | |
1691 | (ignored-dirs-re | |
1692 | (concat | |
1693 | (regexp-opt | |
1694 | (delq nil (mapcar (lambda (s) (if (eq (aref s (1- (length s))) ?/) | |
1695 | (substring s 0 (1- (length s))))) | |
1696 | completion-ignored-extensions)) | |
1697 | t) "\\'")) | |
1698 | (uptodate t)) | |
1699 | (while (and files uptodate) | |
1700 | (let ((f (pop files))) | |
1701 | (if (file-directory-p f) | |
1702 | (unless (string-match ignored-dirs-re f) | |
1703 | (setq files (nconc | |
1704 | (directory-files f t tex-input-files-re) | |
1705 | files))) | |
1706 | (when (file-newer-than-file-p f file) | |
1707 | (setq uptodate nil))))) | |
1708 | uptodate))) | |
1709 | ||
1710 | ||
1711 | (autoload 'format-spec "format-spec") | |
1712 | ||
1713 | (defvar tex-executable-cache nil) | |
1714 | (defun tex-executable-exists-p (name) | |
1715 | "Like `executable-find' but with a cache." | |
1716 | (let ((cache (assoc name tex-executable-cache))) | |
1717 | (if cache (cdr cache) | |
1718 | (let ((executable (executable-find name))) | |
1719 | (push (cons name executable) tex-executable-cache) | |
1720 | executable)))) | |
1721 | ||
1722 | (defun tex-command-executable (cmd) | |
1723 | (let ((s (if (stringp cmd) cmd (eval (car cmd))))) | |
1724 | (substring s 0 (string-match "[ \t]\\|\\'" s)))) | |
1725 | ||
1726 | (defun tex-command-active-p (cmd fspec) | |
1727 | "Return non-nil if the CMD spec might need to be run." | |
1728 | (let ((in (nth 1 cmd)) | |
1729 | (out (nth 2 cmd))) | |
1730 | (if (stringp in) | |
1731 | (let ((file (format-spec in fspec))) | |
1732 | (when (file-exists-p file) | |
1733 | (or (not out) | |
1734 | (file-newer-than-file-p | |
1735 | file (format-spec out fspec))))) | |
1736 | (when (and (eq in t) (stringp out)) | |
1737 | (not (tex-uptodate-p (format-spec out fspec))))))) | |
1738 | ||
8084f5d8 SM |
1739 | (defun tex-compile-default (fspec) |
1740 | "Guess a default command given the format-spec FSPEC." | |
75035a80 SM |
1741 | ;; TODO: Learn to do latex+dvips! |
1742 | (let ((cmds nil) | |
1743 | (unchanged-in nil)) | |
1744 | ;; Only consider active commands. | |
1745 | (dolist (cmd tex-compile-commands) | |
1746 | (when (tex-executable-exists-p (tex-command-executable cmd)) | |
1747 | (if (tex-command-active-p cmd fspec) | |
1748 | (push cmd cmds) | |
1749 | (push (nth 1 cmd) unchanged-in)))) | |
1750 | ;; Remove those commands whose input was considered stable for | |
1751 | ;; some other command (typically if (t . "%.pdf") is inactive | |
1752 | ;; then we're using pdflatex and the fact that the dvi file | |
1753 | ;; is inexistent doesn't matter). | |
1754 | (let ((tmp nil)) | |
1755 | (dolist (cmd cmds) | |
1756 | (unless (member (nth 1 cmd) unchanged-in) | |
1757 | (push cmd tmp))) | |
8084f5d8 | 1758 | ;; Only remove if there's something left. |
75035a80 | 1759 | (if tmp (setq cmds tmp))) |
8084f5d8 SM |
1760 | ;; Remove commands whose input is not uptodate either. |
1761 | (let ((outs (delq nil (mapcar (lambda (x) (nth 2 x)) cmds))) | |
1762 | (tmp nil)) | |
1763 | (dolist (cmd cmds) | |
75035a80 | 1764 | (unless (member (nth 1 cmd) outs) |
8084f5d8 SM |
1765 | (push cmd tmp))) |
1766 | ;; Only remove if there's something left. | |
1767 | (if tmp (setq cmds tmp))) | |
75035a80 SM |
1768 | ;; Select which file we're going to operate on (the latest). |
1769 | (let ((latest (nth 1 (car cmds)))) | |
1770 | (dolist (cmd (prog1 (cdr cmds) (setq cmds (list (car cmds))))) | |
1771 | (if (equal latest (nth 1 cmd)) | |
1772 | (push cmd cmds) | |
1773 | (unless (eq latest t) ;Can't beat that! | |
1774 | (if (or (not (stringp latest)) | |
1775 | (eq (nth 1 cmd) t) | |
1776 | (and (stringp (nth 1 cmd)) | |
1777 | (file-newer-than-file-p | |
1778 | (format-spec (nth 1 cmd) fspec) | |
1779 | (format-spec latest fspec)))) | |
1780 | (setq latest (nth 1 cmd) cmds (list cmd))))))) | |
1781 | ;; Expand the command spec into the actual text. | |
1782 | (dolist (cmd (prog1 cmds (setq cmds nil))) | |
1783 | (push (cons (eval (car cmd)) (cdr cmd)) cmds)) | |
1784 | ;; Select the favorite command from the history. | |
1785 | (let ((hist tex-compile-history) | |
8084f5d8 | 1786 | re hist-cmd) |
75035a80 | 1787 | (while hist |
8084f5d8 | 1788 | (setq hist-cmd (pop hist)) |
75035a80 | 1789 | (setq re (concat "\\`" |
8084f5d8 | 1790 | (regexp-quote (tex-command-executable hist-cmd)) |
75035a80 SM |
1791 | "\\([ \t]\\|\\'\\)")) |
1792 | (dolist (cmd cmds) | |
8084f5d8 SM |
1793 | ;; If the hist entry uses the same command and applies to a file |
1794 | ;; of the same type (e.g. `gv %r.pdf' vs `gv %r.ps'), select cmd. | |
1795 | (and (string-match re (car cmd)) | |
1796 | (or (not (string-match "%[fr]\\([-._[:alnum:]]+\\)" (car cmd))) | |
1797 | (string-match (regexp-quote (match-string 1 (car cmd))) | |
1798 | hist-cmd)) | |
1799 | (setq hist nil cmds (list cmd))))) | |
1800 | ;; Substitute and return. | |
1801 | (if (and hist-cmd | |
1802 | (string-match (concat "[' \t\"]" (format-spec "%r" fspec) | |
1803 | "\\([;&' \t\"]\\|\\'\\)") hist-cmd)) | |
1804 | ;; The history command was already applied to the same file, | |
1805 | ;; so just reuse it. | |
1806 | hist-cmd | |
1807 | (if cmds (format-spec (caar cmds) fspec)))))) | |
75035a80 SM |
1808 | |
1809 | (defun tex-compile (dir cmd) | |
1810 | "Run a command CMD on current TeX buffer's file in DIR." | |
1811 | ;; FIXME: Use time-stamps on files to decide the next op. | |
1812 | (interactive | |
1813 | (let* ((file (tex-main-file)) | |
8084f5d8 SM |
1814 | (dir (prog1 (file-name-directory (expand-file-name file)) |
1815 | (setq file (file-name-nondirectory file)))) | |
75035a80 | 1816 | (root (file-name-sans-extension file)) |
75035a80 SM |
1817 | (fspec (list (cons ?r (comint-quote-filename root)) |
1818 | (cons ?f (comint-quote-filename file)))) | |
8084f5d8 | 1819 | (default (tex-compile-default fspec))) |
75035a80 SM |
1820 | (list dir |
1821 | (completing-read | |
1822 | (format "Command [%s]: " (tex-summarize-command default)) | |
1823 | (mapcar (lambda (x) | |
1824 | (list (format-spec (eval (car x)) fspec))) | |
1825 | tex-compile-commands) | |
1826 | nil nil nil 'tex-compile-history default)))) | |
1827 | (save-some-buffers (not compilation-ask-about-save) nil) | |
1828 | (if (tex-shell-running) | |
1829 | (tex-kill-job) | |
1830 | (tex-start-shell)) | |
1831 | (tex-send-tex-command cmd dir)) | |
d6709b80 SM |
1832 | |
1833 | (defun tex-start-tex (command file &optional dir) | |
460e1b7d | 1834 | "Start a TeX run, using COMMAND on FILE." |
6633b891 | 1835 | (let* ((star (string-match "\\*" command)) |
460e1b7d | 1836 | (compile-command |
6633b891 KH |
1837 | (if star |
1838 | (concat (substring command 0 star) | |
1839 | (comint-quote-filename file) | |
1840 | (substring command (1+ star))) | |
1841 | (concat command " " | |
95a045bd | 1842 | tex-start-options |
9e0ad27a | 1843 | (if (< 0 (length tex-start-commands)) |
c3ce7bf4 | 1844 | (concat |
9e0ad27a | 1845 | (shell-quote-argument tex-start-commands) " ")) |
6633b891 | 1846 | (comint-quote-filename file))))) |
bdbd9606 SM |
1847 | (tex-send-tex-command compile-command dir))) |
1848 | ||
1849 | (defun tex-send-tex-command (cmd &optional dir) | |
b781e739 SS |
1850 | (unless (or (equal dir (let ((buf (tex-shell-buf-no-error))) |
1851 | (and buf (with-current-buffer buf | |
1852 | default-directory)))) | |
bdbd9606 SM |
1853 | (not dir)) |
1854 | (let (shell-dirtrack-verbose) | |
1855 | (tex-send-command tex-shell-cd-command dir))) | |
1856 | (with-current-buffer (process-buffer (tex-send-command cmd)) | |
bdbd9606 SM |
1857 | (make-local-variable 'compilation-parse-errors-function) |
1858 | (setq compilation-parse-errors-function 'tex-compilation-parse-errors) | |
1859 | (setq compilation-last-buffer (current-buffer)) | |
1860 | (compilation-forget-errors) | |
1598a961 | 1861 | ;; Don't parse previous compilations. |
bdbd9606 SM |
1862 | (set-marker compilation-parsing-end (1- (point-max)))) |
1863 | (tex-display-shell) | |
1864 | (setq tex-last-buffer-texed (current-buffer))) | |
460e1b7d | 1865 | \f |
1598a961 SM |
1866 | (defvar tex-error-parse-syntax-table |
1867 | (let ((st (make-syntax-table))) | |
1868 | (modify-syntax-entry ?\( "()" st) | |
1869 | (modify-syntax-entry ?\) ")(" st) | |
1870 | (modify-syntax-entry ?\\ "\\" st) | |
1871 | (modify-syntax-entry ?\{ "_" st) | |
1872 | (modify-syntax-entry ?\} "_" st) | |
1873 | (modify-syntax-entry ?\[ "_" st) | |
1874 | (modify-syntax-entry ?\] "_" st) | |
1875 | ;; Single quotations may appear in errors | |
1876 | (modify-syntax-entry ?\" "_" st) | |
1877 | st) | |
1878 | "Syntax-table used while parsing TeX error messages.") | |
1879 | ||
460e1b7d | 1880 | (defun tex-compilation-parse-errors (limit-search find-at-least) |
f05bd645 RS |
1881 | "Parse the current buffer as TeX error messages. |
1882 | See the variable `compilation-parse-errors-function' for the interface it uses. | |
1883 | ||
1884 | This function parses only the last TeX compilation. | |
1885 | It works on TeX compilations only. It is necessary for that purpose, | |
1886 | since TeX does not put file names and line numbers on the same line as | |
1887 | for the error messages." | |
460e1b7d RS |
1888 | (require 'thingatpt) |
1889 | (setq compilation-error-list nil) | |
1890 | (message "Parsing error messages...") | |
f05bd645 RS |
1891 | (let ((default-directory ; Perhaps dir has changed meanwhile. |
1892 | (file-name-directory (buffer-file-name tex-last-buffer-texed))) | |
f05bd645 RS |
1893 | found-desired (num-errors-found 0) |
1894 | last-filename last-linenum last-position | |
1895 | begin-of-error end-of-error) | |
f05bd645 RS |
1896 | ;; Don't reparse messages already seen at last parse. |
1897 | (goto-char compilation-parsing-end) | |
1898 | ;; Parse messages. | |
1899 | (while (and (not (or found-desired (eobp))) | |
1900 | (prog1 (re-search-forward "^! " nil 'move) | |
1901 | (setq begin-of-error (match-beginning 0) | |
1902 | end-of-error (match-end 0))) | |
1903 | (re-search-forward | |
1904 | "^l\\.\\([0-9]+\\) \\(\\.\\.\\.\\)?\\(.*\\)$" nil 'move)) | |
1905 | (let* ((this-error (set-marker (make-marker) begin-of-error)) | |
1906 | (linenum (string-to-int (match-string 1))) | |
1907 | (error-text (regexp-quote (match-string 3))) | |
1908 | (filename | |
1909 | (save-excursion | |
ea590e1c SM |
1910 | (with-syntax-table tex-error-parse-syntax-table |
1911 | (backward-up-list 1) | |
1912 | (skip-syntax-forward "(_") | |
1913 | (while (not (file-readable-p (thing-at-point 'filename))) | |
1914 | (skip-syntax-backward "(_") | |
1915 | (backward-up-list 1) | |
1916 | (skip-syntax-forward "(_")) | |
1917 | (thing-at-point 'filename)))) | |
f05bd645 RS |
1918 | (new-file |
1919 | (or (null last-filename) | |
1920 | (not (string-equal last-filename filename)))) | |
1921 | (error-location | |
1922 | (save-excursion | |
1923 | (if (equal filename (concat tex-zap-file ".tex")) | |
1924 | (set-buffer tex-last-buffer-texed) | |
1925 | (set-buffer (find-file-noselect filename))) | |
1926 | (if new-file | |
1598a961 | 1927 | (progn (goto-line linenum) (setq last-position nil)) |
f05bd645 RS |
1928 | (goto-char last-position) |
1929 | (forward-line (- linenum last-linenum))) | |
1930 | ;; first try a forward search for the error text, | |
1931 | ;; then a backward search limited by the last error. | |
1932 | (let ((starting-point (point))) | |
1933 | (or (re-search-forward error-text nil t) | |
1598a961 | 1934 | (re-search-backward error-text last-position t) |
f05bd645 RS |
1935 | (goto-char starting-point))) |
1936 | (point-marker)))) | |
1937 | (goto-char this-error) | |
1938 | (if (and compilation-error-list | |
1939 | (or (and find-at-least | |
1940 | (>= num-errors-found | |
1941 | find-at-least)) | |
1942 | (and limit-search | |
1943 | (>= end-of-error limit-search))) | |
1944 | new-file) | |
1945 | (setq found-desired t) | |
1946 | (setq num-errors-found (1+ num-errors-found) | |
1947 | last-filename filename | |
1948 | last-linenum linenum | |
1949 | last-position error-location | |
1950 | compilation-error-list ; Add the new error | |
1951 | (cons (cons this-error error-location) | |
1952 | compilation-error-list)) | |
1953 | (goto-char end-of-error))))) | |
ee13a145 | 1954 | (set-marker compilation-parsing-end (point)) |
f05bd645 | 1955 | (setq compilation-error-list (nreverse compilation-error-list)) |
460e1b7d RS |
1956 | (message "Parsing error messages...done")) |
1957 | \f | |
528415e7 | 1958 | ;;; The commands: |
869bff31 | 1959 | |
1960 | (defun tex-region (beg end) | |
1961 | "Run TeX on the current region, via a temporary file. | |
1962 | The file's name comes from the variable `tex-zap-file' and the | |
1963 | variable `tex-directory' says where to put it. | |
1964 | ||
1965 | If the buffer has a header, the header is given to TeX before the | |
1966 | region itself. The buffer's header is all lines between the strings | |
1967 | defined by `tex-start-of-header' and `tex-end-of-header' inclusive. | |
1968 | The header must start in the first 100 lines of the buffer. | |
1969 | ||
1970 | The value of `tex-trailer' is given to TeX as input after the region. | |
1971 | ||
1972 | The value of `tex-command' specifies the command to use to run TeX." | |
1973 | (interactive "r") | |
1974 | (if (tex-shell-running) | |
1975 | (tex-kill-job) | |
1976 | (tex-start-shell)) | |
1977 | (or tex-zap-file | |
1978 | (setq tex-zap-file (tex-generate-zap-file-name))) | |
6d34c59f RS |
1979 | ;; Temp file will be written and TeX will be run in zap-directory. |
1980 | ;; If the TEXINPUTS file has relative directories or if the region has | |
1981 | ;; \input of files, this must be the same directory as the file for | |
1982 | ;; TeX to access the correct inputs. That's why it's safest if | |
1983 | ;; tex-directory is ".". | |
1984 | (let* ((zap-directory | |
528415e7 | 1985 | (file-name-as-directory (expand-file-name tex-directory))) |
d6709b80 SM |
1986 | (tex-out-file (expand-file-name (concat tex-zap-file ".tex") |
1987 | zap-directory))) | |
0d548e5d RS |
1988 | ;; Don't delete temp files if we do the same buffer twice in a row. |
1989 | (or (eq (current-buffer) tex-last-buffer-texed) | |
1990 | (tex-delete-last-temp-files t)) | |
869bff31 | 1991 | ;; Write the new temp file. |
1992 | (save-excursion | |
1993 | (save-restriction | |
1994 | (widen) | |
1995 | (goto-char (point-min)) | |
1996 | (forward-line 100) | |
1997 | (let ((search-end (point)) | |
6d34c59f RS |
1998 | (default-directory zap-directory) |
1999 | (already-output 0)) | |
869bff31 | 2000 | (goto-char (point-min)) |
6d34c59f | 2001 | |
725b7428 RS |
2002 | ;; Maybe copy first line, such as `\input texinfo', to temp file. |
2003 | (and tex-first-line-header-regexp | |
2004 | (looking-at tex-first-line-header-regexp) | |
bed1b0c8 | 2005 | (write-region (point) |
6d34c59f RS |
2006 | (progn (forward-line 1) |
2007 | (setq already-output (point))) | |
725b7428 RS |
2008 | tex-out-file nil nil)) |
2009 | ||
6d34c59f RS |
2010 | ;; Write out the header, if there is one, |
2011 | ;; and any of the specified region which extends before it. | |
2012 | ;; But don't repeat anything already written. | |
898b9ac1 | 2013 | (if (re-search-forward tex-start-of-header search-end t) |
6d34c59f | 2014 | (let (hbeg) |
869bff31 | 2015 | (beginning-of-line) |
2016 | (setq hbeg (point)) ;mark beginning of header | |
898b9ac1 | 2017 | (if (re-search-forward tex-end-of-header nil t) |
6d34c59f RS |
2018 | (let (hend) |
2019 | (forward-line 1) | |
bed1b0c8 | 2020 | (setq hend (point)) ;mark end of header |
6d34c59f RS |
2021 | (write-region (max (min hbeg beg) already-output) |
2022 | hend | |
2023 | tex-out-file | |
2024 | (not (zerop already-output)) nil) | |
2025 | (setq already-output hend))))) | |
2026 | ||
2027 | ;; Write out the specified region | |
2028 | ;; (but don't repeat anything already written). | |
2029 | (write-region (max beg already-output) end | |
2030 | tex-out-file | |
2031 | (not (zerop already-output)) nil)) | |
2032 | ;; Write the trailer, if any. | |
2033 | ;; Precede it with a newline to make sure it | |
2034 | ;; is not hidden in a comment. | |
2035 | (if tex-trailer | |
2036 | (write-region (concat "\n" tex-trailer) nil | |
2037 | tex-out-file t nil)))) | |
528415e7 RS |
2038 | ;; Record the file name to be deleted afterward. |
2039 | (setq tex-last-temp-file tex-out-file) | |
f05bd645 RS |
2040 | ;; Use a relative file name here because (1) the proper dir |
2041 | ;; is already current, and (2) the abs file name is sometimes | |
2042 | ;; too long and can make tex crash. | |
d6709b80 SM |
2043 | (tex-start-tex tex-command (concat tex-zap-file ".tex") zap-directory) |
2044 | (setq tex-print-file tex-out-file))) | |
869bff31 | 2045 | |
2046 | (defun tex-buffer () | |
2047 | "Run TeX on current buffer. See \\[tex-region] for more information. | |
528415e7 RS |
2048 | Does not save the buffer, so it's useful for trying experimental versions. |
2049 | See \\[tex-file] for an alternative." | |
869bff31 | 2050 | (interactive) |
2051 | (tex-region (point-min) (point-max))) | |
2052 | ||
2053 | (defun tex-file () | |
2054 | "Prompt to save all buffers and run TeX (or LaTeX) on current buffer's file. | |
2055 | This function is more useful than \\[tex-buffer] when you need the | |
2056 | `.aux' file of LaTeX to have the correct name." | |
2057 | (interactive) | |
7abc9add SS |
2058 | (when tex-offer-save |
2059 | (save-some-buffers)) | |
d6709b80 | 2060 | (let* ((source-file (tex-main-file)) |
07c16ec4 | 2061 | (file-dir (file-name-directory (expand-file-name source-file)))) |
869bff31 | 2062 | (if (tex-shell-running) |
2063 | (tex-kill-job) | |
2064 | (tex-start-shell)) | |
d6709b80 | 2065 | (tex-start-tex tex-command source-file file-dir) |
f05bd645 | 2066 | (setq tex-print-file (expand-file-name source-file)))) |
869bff31 | 2067 | |
2068 | (defun tex-generate-zap-file-name () | |
2069 | "Generate a unique name suitable for use as a file name." | |
2070 | ;; Include the shell process number and host name | |
2071 | ;; in case there are multiple shells (for same or different user). | |
f5cdb851 KH |
2072 | ;; Dec 1998: There is a report that some versions of xdvi |
2073 | ;; don't work with file names that start with #. | |
5616ee46 | 2074 | (format "_TZ_%d-%s" |
869bff31 | 2075 | (process-id (get-buffer-process "*tex-shell*")) |
07c16ec4 | 2076 | (subst-char-in-string ?. ?- (system-name)))) |
869bff31 | 2077 | |
2078 | ;; This will perhaps be useful for modifying TEXINPUTS. | |
2079 | ;; Expand each file name, separated by colons, in the string S. | |
2080 | (defun tex-expand-files (s) | |
2081 | (let (elts (start 0)) | |
2082 | (while (string-match ":" s start) | |
2083 | (setq elts (cons (substring s start (match-beginning 0)) elts)) | |
2084 | (setq start (match-end 0))) | |
2085 | (or (= start 0) | |
2086 | (setq elts (cons (substring s start) elts))) | |
c0df1972 SM |
2087 | (mapconcat (lambda (elt) |
2088 | (if (= (length elt) 0) elt (expand-file-name elt))) | |
5616ee46 | 2089 | (nreverse elts) ":"))) |
869bff31 | 2090 | |
2091 | (defun tex-shell-running () | |
713f7b15 | 2092 | (let ((proc (get-process "tex-shell"))) |
b781e739 SS |
2093 | (when proc |
2094 | (if (and (eq (process-status proc) 'run) | |
2095 | (buffer-live-p (process-buffer proc))) | |
2096 | ;; return the TeX process on success | |
2097 | proc | |
2098 | ;; get rid of the process permanently | |
2099 | ;; this should get rid of the annoying w32 problem with | |
2100 | ;; dead tex-shell buffer and live process | |
2101 | (delete-process proc))))) | |
869bff31 | 2102 | |
2103 | (defun tex-kill-job () | |
2104 | "Kill the currently running TeX job." | |
2105 | (interactive) | |
b781e739 | 2106 | ;; `quit-process' leads to core dumps of the tex process (except if |
bed1b0c8 RS |
2107 | ;; coredumpsize has limit 0kb as on many environments). One would |
2108 | ;; like to use (kill-process proc 'lambda), however that construct | |
2109 | ;; does not work on some systems and kills the shell itself. | |
b781e739 SS |
2110 | (let ((proc (get-process "tex-shell"))) |
2111 | (when proc (quit-process proc t)))) | |
869bff31 | 2112 | |
2113 | (defun tex-recenter-output-buffer (linenum) | |
2114 | "Redisplay buffer of TeX job output so that most recent output can be seen. | |
2115 | The last line of the buffer is displayed on | |
2116 | line LINE of the window, or centered if LINE is nil." | |
2117 | (interactive "P") | |
2118 | (let ((tex-shell (get-buffer "*tex-shell*")) | |
23b64225 | 2119 | (window)) |
869bff31 | 2120 | (if (null tex-shell) |
2121 | (message "No TeX output buffer") | |
23b64225 RS |
2122 | (setq window (display-buffer tex-shell)) |
2123 | (save-selected-window | |
2124 | (select-window window) | |
2125 | (bury-buffer tex-shell) | |
2126 | (goto-char (point-max)) | |
2127 | (recenter (if linenum | |
2128 | (prefix-numeric-value linenum) | |
2129 | (/ (window-height) 2))))))) | |
869bff31 | 2130 | |
528415e7 | 2131 | (defun tex-print (&optional alt) |
869bff31 | 2132 | "Print the .dvi file made by \\[tex-region], \\[tex-buffer] or \\[tex-file]. |
1433a222 CZ |
2133 | Runs the shell command defined by `tex-dvi-print-command'. If prefix argument |
2134 | is provided, use the alternative command, `tex-alt-dvi-print-command'." | |
528415e7 | 2135 | (interactive "P") |
869bff31 | 2136 | (let ((print-file-name-dvi (tex-append tex-print-file ".dvi")) |
2137 | test-name) | |
2138 | (if (and (not (equal (current-buffer) tex-last-buffer-texed)) | |
45c3304d RS |
2139 | (buffer-file-name) |
2140 | ;; Check that this buffer's printed file is up to date. | |
869bff31 | 2141 | (file-newer-than-file-p |
2142 | (setq test-name (tex-append (buffer-file-name) ".dvi")) | |
45c3304d | 2143 | (buffer-file-name))) |
869bff31 | 2144 | (setq print-file-name-dvi test-name)) |
528415e7 RS |
2145 | (if (not (file-exists-p print-file-name-dvi)) |
2146 | (error "No appropriate `.dvi' file could be found") | |
460e1b7d RS |
2147 | (if (tex-shell-running) |
2148 | (tex-kill-job) | |
2149 | (tex-start-shell)) | |
528415e7 | 2150 | (tex-send-command |
bed1b0c8 | 2151 | (if alt tex-alt-dvi-print-command tex-dvi-print-command) |
602503c5 RS |
2152 | (shell-quote-argument |
2153 | print-file-name-dvi) | |
2154 | t)))) | |
869bff31 | 2155 | |
cf6d6e8a RS |
2156 | (defun tex-alt-print () |
2157 | "Print the .dvi file made by \\[tex-region], \\[tex-buffer] or \\[tex-file]. | |
002b0d00 | 2158 | Runs the shell command defined by `tex-alt-dvi-print-command'." |
cf6d6e8a RS |
2159 | (interactive) |
2160 | (tex-print t)) | |
2161 | ||
869bff31 | 2162 | (defun tex-view () |
2163 | "Preview the last `.dvi' file made by running TeX under Emacs. | |
2164 | This means, made using \\[tex-region], \\[tex-buffer] or \\[tex-file]. | |
2b7971c9 RS |
2165 | The variable `tex-dvi-view-command' specifies the shell command for preview. |
2166 | You must set that variable yourself before using this command, | |
2167 | because there is no standard value that would generally work." | |
869bff31 | 2168 | (interactive) |
2b7971c9 RS |
2169 | (or tex-dvi-view-command |
2170 | (error "You must set `tex-dvi-view-command'")) | |
746c30e2 | 2171 | (let ((tex-dvi-print-command (eval tex-dvi-view-command))) |
869bff31 | 2172 | (tex-print))) |
2173 | ||
2174 | (defun tex-append (file-name suffix) | |
2175 | "Append to FILENAME the suffix SUFFIX, using same algorithm TeX uses. | |
cf6d6e8a | 2176 | Pascal-based TeX scans for the first period, C TeX uses the last. |
869bff31 | 2177 | No period is retained immediately before SUFFIX, |
2178 | so normally SUFFIX starts with one." | |
2179 | (if (stringp file-name) | |
cf6d6e8a RS |
2180 | (let ((file (file-name-nondirectory file-name)) |
2181 | trial-name) | |
6da9bbd6 | 2182 | ;; Try splitting on last period. |
7d0ca249 RS |
2183 | ;; The first-period split can get fooled when two files |
2184 | ;; named a.tex and a.b.tex are both tex'd; | |
2185 | ;; the last-period split must be right if it matches at all. | |
cf6d6e8a RS |
2186 | (setq trial-name |
2187 | (concat (file-name-directory file-name) | |
2188 | (substring file 0 | |
7d0ca249 | 2189 | (string-match "\\.[^.]*$" file)) |
cf6d6e8a RS |
2190 | suffix)) |
2191 | (if (or (file-exists-p trial-name) | |
2192 | (file-exists-p (concat trial-name ".aux"))) ;for BibTeX files | |
2193 | trial-name | |
7d0ca249 | 2194 | ;; Not found, so split on first period. |
cf6d6e8a RS |
2195 | (concat (file-name-directory file-name) |
2196 | (substring file 0 | |
7d0ca249 | 2197 | (string-match "\\." file)) |
cf6d6e8a | 2198 | suffix))) |
869bff31 | 2199 | " ")) |
2200 | ||
2201 | (defun tex-show-print-queue () | |
2202 | "Show the print queue that \\[tex-print] put your job on. | |
1433a222 | 2203 | Runs the shell command defined by `tex-show-queue-command'." |
869bff31 | 2204 | (interactive) |
2205 | (if (tex-shell-running) | |
2206 | (tex-kill-job) | |
2207 | (tex-start-shell)) | |
51b2c841 RS |
2208 | (tex-send-command tex-show-queue-command) |
2209 | (tex-display-shell)) | |
869bff31 | 2210 | |
2211 | (defun tex-bibtex-file () | |
2212 | "Run BibTeX on the current buffer's file." | |
2213 | (interactive) | |
2214 | (if (tex-shell-running) | |
2215 | (tex-kill-job) | |
2216 | (tex-start-shell)) | |
e6b0b773 MR |
2217 | (let (shell-dirtrack-verbose |
2218 | (tex-out-file | |
869bff31 | 2219 | (tex-append (file-name-nondirectory (buffer-file-name)) "")) |
2220 | (file-dir (file-name-directory (buffer-file-name)))) | |
528415e7 | 2221 | (tex-send-command tex-shell-cd-command file-dir) |
51b2c841 RS |
2222 | (tex-send-command tex-bibtex-command tex-out-file)) |
2223 | (tex-display-shell)) | |
ea590e1c SM |
2224 | \f |
2225 | ;;;; | |
2226 | ;;;; LaTeX indentation | |
2227 | ;;;; | |
2228 | ||
2229 | (defvar tex-indent-allhanging t) | |
2230 | (defvar tex-indent-arg 4) | |
2231 | (defvar tex-indent-basic 2) | |
2232 | (defvar tex-indent-item tex-indent-basic) | |
2233 | (defvar tex-indent-item-re "\\\\\\(bib\\)?item\\>") | |
2234 | ||
2235 | (easy-mmode-defsyntax tex-latex-indent-syntax-table | |
dd459839 SM |
2236 | '((?$ . ".") |
2237 | (?\( . ".") | |
2238 | (?\) . ".")) | |
ea590e1c SM |
2239 | "Syntax table used while computing indentation." |
2240 | :copy tex-mode-syntax-table) | |
2241 | ||
2242 | (defun latex-indent (&optional arg) | |
7afecb99 SM |
2243 | (if (and (eq (get-text-property (line-beginning-position) 'face) |
2244 | tex-verbatim-face)) | |
a3d80d4a | 2245 | 'noindent |
7afecb99 SM |
2246 | (with-syntax-table tex-latex-indent-syntax-table |
2247 | ;; TODO: Rather than ignore $, we should try to be more clever about it. | |
2248 | (let ((indent | |
2249 | (save-excursion | |
2250 | (beginning-of-line) | |
2251 | (latex-find-indent)))) | |
2252 | (if (< indent 0) (setq indent 0)) | |
2253 | (if (<= (current-column) (current-indentation)) | |
2254 | (indent-line-to indent) | |
2255 | (save-excursion (indent-line-to indent))))))) | |
ea590e1c SM |
2256 | |
2257 | (defun latex-find-indent (&optional virtual) | |
2258 | "Find the proper indentation of text after point. | |
2259 | VIRTUAL if non-nil indicates that we're only trying to find the indentation | |
2260 | in order to determine the indentation of something else. | |
2261 | There might be text before point." | |
2262 | (save-excursion | |
2263 | (skip-chars-forward " \t") | |
2264 | (or | |
7afecb99 SM |
2265 | ;; Stick the first line at column 0. |
2266 | (and (= (point-min) (line-beginning-position)) 0) | |
ea590e1c | 2267 | ;; Trust the current indentation, if such info is applicable. |
7afecb99 SM |
2268 | (and virtual (save-excursion (skip-chars-backward " \t&") (bolp)) |
2269 | (current-column)) | |
2270 | ;; Stick verbatim environments to the left margin. | |
2271 | (and (looking-at "\\\\\\(begin\\|end\\) *{\\([^\n}]+\\)") | |
2272 | (member (match-string 2) tex-verbatim-environments) | |
2273 | 0) | |
ea590e1c SM |
2274 | ;; Put leading close-paren where the matching open brace would be. |
2275 | (and (eq (latex-syntax-after) ?\)) | |
2276 | (ignore-errors | |
2277 | (save-excursion | |
2278 | (latex-skip-close-parens) | |
2279 | (latex-backward-sexp-1) | |
2280 | (latex-find-indent 'virtual)))) | |
2281 | ;; Default (maybe an argument) | |
2282 | (let ((pos (point)) | |
2283 | (char (char-after)) | |
2284 | ;; Outdent \item if necessary. | |
2285 | (indent (if (looking-at tex-indent-item-re) (- tex-indent-item) 0)) | |
2286 | up-list-pos) | |
2287 | ;; Find the previous point which determines our current indentation. | |
2288 | (condition-case err | |
2289 | (progn | |
2290 | (latex-backward-sexp-1) | |
2291 | (while (> (current-column) (current-indentation)) | |
2292 | (latex-backward-sexp-1))) | |
2293 | (scan-error | |
2294 | (setq up-list-pos (nth 2 err)))) | |
8908cf40 SM |
2295 | (cond |
2296 | ((= (point-min) pos) 0) ; We're really just indenting the first line. | |
2297 | ((integerp up-list-pos) | |
2298 | ;; Have to indent relative to the open-paren. | |
2299 | (goto-char up-list-pos) | |
2300 | (if (and (not tex-indent-allhanging) | |
2301 | (> pos (progn (latex-down-list) | |
2302 | (forward-comment (point-max)) | |
2303 | (point)))) | |
7afecb99 | 2304 | ;; Align with the first element after the open-paren. |
8908cf40 SM |
2305 | (current-column) |
2306 | ;; We're the first element after a hanging brace. | |
2307 | (goto-char up-list-pos) | |
2308 | (+ indent tex-indent-basic (latex-find-indent 'virtual)))) | |
2309 | ;; We're now at the "beginning" of a line. | |
2310 | ((not (and (not virtual) (eq (char-after) ?\\))) | |
2311 | ;; Nothing particular here: just keep the same indentation. | |
2312 | (+ indent (current-column))) | |
2313 | ;; We're now looking at a macro call. | |
7afecb99 SM |
2314 | ((looking-at tex-indent-item-re) |
2315 | ;; Indenting relative to an item, have to re-add the outdenting. | |
8908cf40 SM |
2316 | (+ indent (current-column) tex-indent-item)) |
2317 | (t | |
2318 | (let ((col (current-column))) | |
2319 | (if (not (eq (char-syntax char) ?\()) | |
2320 | ;; If the first char was not an open-paren, there's | |
2321 | ;; a risk that this is really not an argument to the | |
2322 | ;; macro at all. | |
7afecb99 SM |
2323 | (+ indent col) |
2324 | (forward-sexp 1) | |
2325 | (if (< (line-end-position) | |
2326 | (save-excursion (forward-comment (point-max)) | |
2327 | (point))) | |
8908cf40 SM |
2328 | ;; we're indenting the first argument. |
2329 | (min (current-column) (+ tex-indent-arg col)) | |
2330 | (skip-syntax-forward " ") | |
2331 | (current-column)))))))))) | |
a3d80d4a SM |
2332 | ;;; DocTeX support |
2333 | ||
2334 | (defun doctex-font-lock-^^A () | |
2335 | (if (eq (char-after (line-beginning-position)) ?\%) | |
2336 | (progn | |
2337 | (put-text-property | |
2338 | (1- (match-beginning 1)) (match-beginning 1) | |
2339 | 'syntax-table | |
2340 | (if (= (1+ (line-beginning-position)) (match-beginning 1)) | |
2341 | ;; The `%' is a single-char comment, which Emacs | |
2342 | ;; syntax-table can't deal with. We could turn it | |
2343 | ;; into a non-comment, or use `\n%' or `%^' as the comment. | |
2344 | ;; Instead, we include it in the ^^A comment. | |
2345 | (eval-when-compile (string-to-syntax "< b")) | |
2346 | (eval-when-compile (string-to-syntax ">")))) | |
2347 | (let ((end (line-end-position))) | |
2348 | (if (< end (point-max)) | |
2349 | (put-text-property | |
2350 | end (1+ end) | |
2351 | 'syntax-table | |
2352 | (eval-when-compile (string-to-syntax "> b"))))) | |
2353 | (eval-when-compile (string-to-syntax "< b"))))) | |
2354 | ||
2355 | (defun doctex-font-lock-syntactic-face-function (state) | |
2356 | ;; Mark DocTeX documentation, which is parsed as a style A comment | |
2357 | ;; starting in column 0. | |
2358 | (if (or (nth 3 state) (nth 7 state) | |
2359 | (not (memq (char-before (nth 8 state)) | |
2360 | '(?\n nil)))) | |
2361 | ;; Anything else is just as for LaTeX. | |
2362 | (tex-font-lock-syntactic-face-function state) | |
2363 | font-lock-doc-face)) | |
2364 | ||
2365 | (defvar doctex-font-lock-syntactic-keywords | |
2366 | (append | |
2367 | tex-font-lock-syntactic-keywords | |
2368 | ;; For DocTeX comment-in-doc. | |
2369 | `(("\\(\\^\\)\\^A" (1 (doctex-font-lock-^^A)))))) | |
2370 | ||
2371 | (defvar doctex-font-lock-keywords | |
2372 | (append tex-font-lock-keywords | |
2373 | '(("^%<[^>]*>" (0 font-lock-preprocessor-face t))))) | |
2374 | ||
2375 | ;;;###autoload | |
2376 | (define-derived-mode doctex-mode latex-mode "DocTeX" | |
2377 | "Major mode to edit DocTeX files." | |
2378 | (setq font-lock-defaults | |
2379 | (cons (append (car font-lock-defaults) '(doctex-font-lock-keywords)) | |
2380 | (mapcar | |
2381 | (lambda (x) | |
2382 | (case (car-safe x) | |
2383 | (font-lock-syntactic-keywords | |
2384 | (cons (car x) 'doctex-font-lock-syntactic-keywords)) | |
2385 | (font-lock-syntactic-face-function | |
2386 | (cons (car x) 'doctex-font-lock-syntactic-face-function)) | |
2387 | (t x))) | |
2388 | (cdr font-lock-defaults))))) | |
528415e7 RS |
2389 | |
2390 | (run-hooks 'tex-mode-load-hook) | |
869bff31 | 2391 | |
49116ac0 JB |
2392 | (provide 'tex-mode) |
2393 | ||
d501f516 | 2394 | ;;; tex-mode.el ends here |