Commit | Line | Data |
---|---|---|
95a045bd | 1 | ;;; tex-mode.el --- TeX, LaTeX, and SliTeX mode commands -*- coding: utf-8 -*- |
d501f516 | 2 | |
1b741d9e | 3 | ;; Copyright (C) 1985, 86, 89, 92, 94, 95, 96, 97, 98, 1999, 2002 |
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 | |
528415e7 | 144 | (defvar standard-latex-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. |
d61140e8 RS |
159 | Combined with `standard-latex-block-names' for minibuffer completion." |
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 . " ") | |
288 | (?~ . " ") | |
289 | (?$ . "$$") | |
290 | (?\\ . "/") | |
291 | (?\" . ".") | |
292 | (?& . ".") | |
15ca8de7 SM |
293 | (?_ . ".") |
294 | (?^ . ".")) | |
869bff31 | 295 | "Syntax table used while in TeX mode.") |
53c4fe47 SM |
296 | \f |
297 | ;;;; | |
298 | ;;;; Imenu support | |
299 | ;;;; | |
869bff31 | 300 | |
d6709b80 | 301 | (defcustom latex-imenu-indent-string ". " |
a53c647b RS |
302 | "*String to add repeated in front of nested sectional units for Imenu. |
303 | An alternative value is \" . \", if you use a font with a narrow period." | |
304 | :type 'string | |
305 | :group 'tex) | |
306 | ||
d6709b80 SM |
307 | (defvar latex-section-alist |
308 | '(("part" . 0) ("chapter" . 1) | |
309 | ("section" . 2) ("subsection" . 3) | |
310 | ("subsubsection" . 4) | |
311 | ("paragraph" . 5) ("subparagraph" . 6))) | |
312 | ||
53c4fe47 SM |
313 | (defvar latex-metasection-list |
314 | '("documentstyle" "documentclass" | |
315 | "begin{document}" "end{document}" | |
316 | "appendix" "frontmatter" "mainmatter" "backmatter")) | |
317 | ||
a5e2ae01 | 318 | (defun latex-imenu-create-index () |
53c4fe47 SM |
319 | "Generate an alist for imenu from a LaTeX buffer." |
320 | (let ((section-regexp | |
321 | (concat "\\\\" (regexp-opt (mapcar 'car latex-section-alist) t) | |
322 | "\\*?[ \t]*{")) | |
323 | (metasection-regexp | |
324 | (concat "\\\\" (regexp-opt latex-metasection-list t))) | |
325 | i0 menu case-fold-search) | |
a53c647b RS |
326 | (save-excursion |
327 | ;; Find the top-most level in this file but don't allow it to be | |
328 | ;; any deeper than "section" (which is top-level in an article). | |
329 | (goto-char (point-min)) | |
330 | (if (search-forward-regexp "\\\\part\\*?[ \t]*{" nil t) | |
331 | (setq i0 0) | |
332 | (if (search-forward-regexp "\\\\chapter\\*?[ \t]*{" nil t) | |
333 | (setq i0 1) | |
334 | (setq i0 2))) | |
335 | ||
336 | ;; Look for chapters and sections. | |
337 | (goto-char (point-min)) | |
53c4fe47 | 338 | (while (search-forward-regexp section-regexp nil t) |
a53c647b RS |
339 | (let ((start (match-beginning 0)) |
340 | (here (point)) | |
341 | (i (cdr (assoc (buffer-substring-no-properties | |
342 | (match-beginning 1) | |
343 | (match-end 1)) | |
d6709b80 | 344 | latex-section-alist)))) |
a53c647b RS |
345 | (backward-char 1) |
346 | (condition-case err | |
347 | (progn | |
348 | ;; Using sexps allows some use of matching {...} inside | |
349 | ;; titles. | |
350 | (forward-sexp 1) | |
d6709b80 SM |
351 | (push (cons (concat (apply 'concat |
352 | (make-list | |
353 | (max 0 (- i i0)) | |
354 | latex-imenu-indent-string)) | |
355 | (buffer-substring-no-properties | |
356 | here (1- (point)))) | |
357 | start) | |
358 | menu)) | |
a53c647b RS |
359 | (error nil)))) |
360 | ||
361 | ;; Look for included material. | |
362 | (goto-char (point-min)) | |
363 | (while (search-forward-regexp | |
364 | "\\\\\\(include\\|input\\|verbatiminput\\|bibliography\\)\ | |
53c4fe47 | 365 | \[ \t]*{\\([^}\n]+\\)}" |
a53c647b | 366 | nil t) |
53c4fe47 SM |
367 | (push (cons (concat "<<" (buffer-substring-no-properties |
368 | (match-beginning 2) | |
369 | (match-end 2)) | |
370 | (if (= (char-after (match-beginning 1)) ?b) | |
371 | ".bbl" | |
372 | ".tex")) | |
373 | (match-beginning 0)) | |
374 | menu)) | |
a53c647b RS |
375 | |
376 | ;; Look for \frontmatter, \mainmatter, \backmatter, and \appendix. | |
377 | (goto-char (point-min)) | |
53c4fe47 SM |
378 | (while (search-forward-regexp metasection-regexp nil t) |
379 | (push (cons "--" (match-beginning 0)) menu)) | |
a53c647b RS |
380 | |
381 | ;; Sort in increasing buffer position order. | |
382 | (sort menu (function (lambda (a b) (< (cdr a) (cdr b)))))))) | |
53c4fe47 SM |
383 | \f |
384 | ;;;; | |
385 | ;;;; Outline support | |
386 | ;;;; | |
387 | ||
388 | (defvar latex-outline-regexp | |
389 | (concat "\\\\" | |
390 | (regexp-opt (append latex-metasection-list | |
391 | (mapcar 'car latex-section-alist)) t))) | |
392 | ||
393 | (defun latex-outline-level () | |
394 | (if (looking-at latex-outline-regexp) | |
395 | (1+ (or (cdr (assoc (match-string 1) latex-section-alist)) -1)) | |
396 | 1000)) | |
397 | \f | |
398 | ;;;; | |
399 | ;;;; Font-Lock support | |
400 | ;;;; | |
401 | ||
402 | ;(defvar tex-font-lock-keywords | |
403 | ; ;; Regexps updated with help from Ulrik Dickow <dickow@nbi.dk>. | |
404 | ; '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}" | |
405 | ; 2 font-lock-function-name-face) | |
406 | ; ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}" | |
407 | ; 2 font-lock-constant-face) | |
408 | ; ;; It seems a bit dubious to use `bold' and `italic' faces since we might | |
409 | ; ;; not be able to display those fonts. | |
410 | ; ("{\\\\bf\\([^}]+\\)}" 1 'bold keep) | |
411 | ; ("{\\\\\\(em\\|it\\|sl\\)\\([^}]+\\)}" 2 'italic keep) | |
412 | ; ("\\\\\\([a-zA-Z@]+\\|.\\)" . font-lock-keyword-face) | |
413 | ; ("^[ \t\n]*\\\\def[\\\\@]\\(\\w+\\)" 1 font-lock-function-name-face keep)) | |
414 | ; ;; Rewritten and extended for LaTeX2e by Ulrik Dickow <dickow@nbi.dk>. | |
415 | ; '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}" | |
416 | ; 2 font-lock-function-name-face) | |
417 | ; ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}" | |
418 | ; 2 font-lock-constant-face) | |
419 | ; ("^[ \t]*\\\\def\\\\\\(\\(\\w\\|@\\)+\\)" 1 font-lock-function-name-face) | |
420 | ; "\\\\\\([a-zA-Z@]+\\|.\\)" | |
421 | ; ;; It seems a bit dubious to use `bold' and `italic' faces since we might | |
422 | ; ;; not be able to display those fonts. | |
423 | ; ;; LaTeX2e: \emph{This is emphasized}. | |
424 | ; ("\\\\emph{\\([^}]+\\)}" 1 'italic keep) | |
425 | ; ;; LaTeX2e: \textbf{This is bold}, \textit{...}, \textsl{...} | |
426 | ; ("\\\\text\\(\\(bf\\)\\|it\\|sl\\){\\([^}]+\\)}" | |
427 | ; 3 (if (match-beginning 2) 'bold 'italic) keep) | |
428 | ; ;; Old-style bf/em/it/sl. Stop at `\\' and un-escaped `&', for tables. | |
429 | ; ("\\\\\\(\\(bf\\)\\|em\\|it\\|sl\\)\\>\\(\\([^}&\\]\\|\\\\[^\\]\\)+\\)" | |
430 | ; 3 (if (match-beginning 2) 'bold 'italic) keep)) | |
431 | ||
432 | ;; Rewritten with the help of Alexandra Bac <abac@welcome.disi.unige.it>. | |
433 | (defconst tex-font-lock-keywords-1 | |
434 | (eval-when-compile | |
435 | (let* (;; Names of commands whose arg should be fontified as heading, etc. | |
436 | (headings (regexp-opt | |
437 | '("title" "begin" "end" "chapter" "part" | |
438 | "section" "subsection" "subsubsection" | |
439 | "paragraph" "subparagraph" "subsubparagraph" | |
440 | "newcommand" "renewcommand" "newenvironment" | |
441 | "newtheorem") | |
442 | t)) | |
443 | (variables (regexp-opt | |
444 | '("newcounter" "newcounter*" "setcounter" "addtocounter" | |
445 | "setlength" "addtolength" "settowidth") | |
446 | t)) | |
447 | (includes (regexp-opt | |
448 | '("input" "include" "includeonly" "bibliography" | |
449 | "epsfig" "psfig" "epsf" "nofiles" "usepackage" | |
15ca8de7 | 450 | "documentstyle" "documentclass" "verbatiminput" |
53c4fe47 SM |
451 | "includegraphics" "includegraphics*") |
452 | t)) | |
453 | ;; Miscellany. | |
454 | (slash "\\\\") | |
15ca8de7 SM |
455 | (opt " *\\(\\[[^]]*\\] *\\)*") |
456 | ;; This would allow highlighting \newcommand\CMD but requires | |
457 | ;; adapting subgroup numbers below. | |
458 | ;; (arg "\\(?:{\\(\\(?:[^{}\\]+\\|\\\\.\\|{[^}]*}\\)+\\)\\|\\\\[a-z*]+\\)")) | |
1598a961 | 459 | (arg "{\\(\\(?:[^{}\\]+\\|\\\\.\\|{[^}]*}\\)+\\)")) |
53c4fe47 | 460 | (list |
95a045bd SM |
461 | ;; display $$ math $$ |
462 | ;; We only mark the match between $$ and $$ because the $$ delimiters | |
463 | ;; themselves have already been marked (along with $..$) by syntactic | |
464 | ;; fontification. Also this is done at the very beginning so as to | |
465 | ;; interact with the other keywords in the same way as $...$ does. | |
466 | (list "\\$\\$\\([^$]+\\)\\$\\$" 1 'tex-math-face) | |
53c4fe47 SM |
467 | ;; Heading args. |
468 | (list (concat slash headings "\\*?" opt arg) | |
bdbd9606 SM |
469 | ;; If ARG ends up matching too much (if the {} don't match, f.ex) |
470 | ;; jit-lock will do funny things: when updating the buffer | |
471 | ;; the re-highlighting is only done locally so it will just | |
472 | ;; match the local line, but defer-contextually will | |
473 | ;; match more lines at a time, so ARG will end up matching | |
474 | ;; a lot more, which might suddenly include a comment | |
475 | ;; so you get things highlighted bold when you type them | |
476 | ;; but they get turned back to normal a little while later | |
477 | ;; because "there's already a face there". | |
478 | ;; Using `keep' works around this un-intuitive behavior as well | |
15ca8de7 SM |
479 | ;; as improves the behavior in the very rare case where you do |
480 | ;; have a comment in ARG. | |
bdbd9606 | 481 | 3 'font-lock-function-name-face 'keep) |
15ca8de7 SM |
482 | (list (concat slash "\\(re\\)?newcommand\\** *\\(\\\\[A-Za-z@]+\\)") |
483 | 2 'font-lock-function-name-face 'keep) | |
53c4fe47 | 484 | ;; Variable args. |
15ca8de7 | 485 | (list (concat slash variables " *" arg) 2 'font-lock-variable-name-face) |
53c4fe47 SM |
486 | ;; Include args. |
487 | (list (concat slash includes opt arg) 3 'font-lock-builtin-face) | |
488 | ;; Definitions. I think. | |
15ca8de7 | 489 | '("^[ \t]*\\\\def *\\\\\\(\\(\\w\\|@\\)+\\)" |
bdbd9606 | 490 | 1 font-lock-function-name-face)))) |
53c4fe47 SM |
491 | "Subdued expressions to highlight in TeX modes.") |
492 | ||
493 | (defconst tex-font-lock-keywords-2 | |
494 | (append tex-font-lock-keywords-1 | |
495 | (eval-when-compile | |
496 | (let* (;; | |
497 | ;; Names of commands whose arg should be fontified with fonts. | |
1598a961 | 498 | (bold (regexp-opt '("textbf" "textsc" "textup" |
53c4fe47 | 499 | "boldsymbol" "pmb") t)) |
1598a961 | 500 | (italic (regexp-opt '("textit" "textsl" "emph") t)) |
53c4fe47 SM |
501 | (type (regexp-opt '("texttt" "textmd" "textrm" "textsf") t)) |
502 | ;; | |
503 | ;; Names of commands whose arg should be fontified as a citation. | |
504 | (citations (regexp-opt | |
505 | '("label" "ref" "pageref" "vref" "eqref" | |
15ca8de7 | 506 | "cite" "nocite" "index" "glossary" "bibitem" |
1598a961 SM |
507 | ;; These are text, rather than citations. |
508 | ;; "caption" "footnote" "footnotemark" "footnotetext" | |
509 | ) | |
53c4fe47 SM |
510 | t)) |
511 | ;; | |
512 | ;; Names of commands that should be fontified. | |
1b741d9e AS |
513 | (specials-1 (regexp-opt '("\\" "\\*") t)) ;; "-" |
514 | (specials-2 (regexp-opt | |
515 | '("linebreak" "nolinebreak" "pagebreak" "nopagebreak" | |
516 | "newline" "newpage" "clearpage" "cleardoublepage" | |
517 | "displaybreak" "allowdisplaybreaks" | |
518 | "enlargethispage") t)) | |
53c4fe47 SM |
519 | (general "\\([a-zA-Z@]+\\**\\|[^ \t\n]\\)") |
520 | ;; | |
521 | ;; Miscellany. | |
522 | (slash "\\\\") | |
15ca8de7 | 523 | (opt " *\\(\\[[^]]*\\] *\\)*") |
1598a961 | 524 | (arg "{\\(\\(?:[^{}\\]+\\|\\\\.\\|{[^}]*}\\)+\\)")) |
53c4fe47 SM |
525 | (list |
526 | ;; | |
527 | ;; Citation args. | |
528 | (list (concat slash citations opt arg) 3 'font-lock-constant-face) | |
529 | ;; | |
15ca8de7 | 530 | ;; Text between `` quotes ''. |
95a045bd | 531 | (cons (concat (regexp-opt `("``" "\"<" "\"`" "<<" "«") t) |
7abc9add | 532 | "[^'\">{]+" ;a bit pessimistic |
95a045bd | 533 | (regexp-opt `("''" "\">" "\"'" ">>" "»") t)) |
15ca8de7 SM |
534 | 'font-lock-string-face) |
535 | ;; | |
53c4fe47 | 536 | ;; Command names, special and general. |
1b741d9e AS |
537 | (cons (concat slash specials-1) 'font-lock-warning-face) |
538 | (list (concat "\\(" slash specials-2 "\\)\\([^a-zA-Z@]\\|\\'\\)") | |
539 | 1 'font-lock-warning-face) | |
53c4fe47 SM |
540 | (concat slash general) |
541 | ;; | |
542 | ;; Font environments. It seems a bit dubious to use `bold' etc. faces | |
543 | ;; since we might not be able to display those fonts. | |
15ca8de7 SM |
544 | (list (concat slash bold " *" arg) 2 '(quote bold) 'append) |
545 | (list (concat slash italic " *" arg) 2 '(quote italic) 'append) | |
1598a961 | 546 | ;; (list (concat slash type arg) 2 '(quote bold-italic) 'append) |
53c4fe47 SM |
547 | ;; |
548 | ;; Old-style bf/em/it/sl. Stop at `\\' and un-escaped `&', for tables. | |
549 | (list (concat "\\\\\\(\\(bf\\)\\|em\\|it\\|sl\\)\\>" | |
550 | "\\(\\([^}&\\]\\|\\\\[^\\]\\)+\\)") | |
bdbd9606 | 551 | 3 '(if (match-beginning 2) 'bold 'italic) 'append))))) |
53c4fe47 SM |
552 | "Gaudy expressions to highlight in TeX modes.") |
553 | ||
554 | (defvar tex-font-lock-keywords tex-font-lock-keywords-1 | |
555 | "Default expressions to highlight in TeX modes.") | |
0fb4f245 | 556 | |
bdbd9606 SM |
557 | |
558 | (defface tex-math-face | |
559 | '((t :inherit font-lock-string-face)) | |
560 | "Face used to highlight TeX math expressions.") | |
561 | (defvar tex-math-face 'tex-math-face) | |
562 | ||
563 | ;; Use string syntax but math face for $...$. | |
564 | (defun tex-font-lock-syntactic-face-function (state) | |
565 | (if (nth 3 state) tex-math-face font-lock-comment-face)) | |
566 | ||
53c4fe47 | 567 | \f |
869bff31 | 568 | (defun tex-define-common-keys (keymap) |
81c735c0 | 569 | "Define the keys that we want defined both in TeX mode and in the TeX shell." |
869bff31 | 570 | (define-key keymap "\C-c\C-k" 'tex-kill-job) |
571 | (define-key keymap "\C-c\C-l" 'tex-recenter-output-buffer) | |
572 | (define-key keymap "\C-c\C-q" 'tex-show-print-queue) | |
573 | (define-key keymap "\C-c\C-p" 'tex-print) | |
574 | (define-key keymap "\C-c\C-v" 'tex-view) | |
62a24cb5 RS |
575 | |
576 | (define-key keymap [menu-bar tex] (cons "TeX" (make-sparse-keymap "TeX"))) | |
577 | ||
bdbd9606 SM |
578 | (define-key keymap [menu-bar tex tex-kill-job] |
579 | '(menu-item "Tex Kill" tex-kill-job :enable (tex-shell-running))) | |
cf6d6e8a | 580 | (define-key keymap [menu-bar tex tex-recenter-output-buffer] |
bdbd9606 SM |
581 | '(menu-item "Tex Recenter" tex-recenter-output-buffer |
582 | :enable (get-buffer "*tex-shell*"))) | |
62a24cb5 RS |
583 | (define-key keymap [menu-bar tex tex-show-print-queue] |
584 | '("Show Print Queue" . tex-show-print-queue)) | |
cf6d6e8a | 585 | (define-key keymap [menu-bar tex tex-alt-print] |
bdbd9606 SM |
586 | '(menu-item "Tex Print (alt printer)" tex-alt-print |
587 | :enable (stringp tex-print-file))) | |
588 | (define-key keymap [menu-bar tex tex-print] | |
589 | '(menu-item "Tex Print" tex-print :enable (stringp tex-print-file))) | |
590 | (define-key keymap [menu-bar tex tex-view] | |
591 | '(menu-item "Tex View" tex-view :enable (stringp tex-print-file)))) | |
592 | ||
593 | (defvar tex-mode-map | |
594 | (let ((map (make-sparse-keymap))) | |
15ca8de7 | 595 | (set-keymap-parent map text-mode-map) |
bdbd9606 SM |
596 | (tex-define-common-keys map) |
597 | (define-key map "\"" 'tex-insert-quote) | |
598 | (define-key map "(" 'skeleton-pair-insert-maybe) | |
599 | (define-key map "{" 'skeleton-pair-insert-maybe) | |
600 | (define-key map "[" 'skeleton-pair-insert-maybe) | |
601 | (define-key map "$" 'skeleton-pair-insert-maybe) | |
602 | (define-key map "\n" 'tex-terminate-paragraph) | |
603 | (define-key map "\M-\r" 'latex-insert-item) | |
604 | (define-key map "\C-c}" 'up-list) | |
605 | (define-key map "\C-c{" 'tex-insert-braces) | |
606 | (define-key map "\C-c\C-r" 'tex-region) | |
607 | (define-key map "\C-c\C-b" 'tex-buffer) | |
608 | (define-key map "\C-c\C-f" 'tex-file) | |
bdbd9606 SM |
609 | (define-key map "\C-c\C-i" 'tex-bibtex-file) |
610 | (define-key map "\C-c\C-o" 'tex-latex-block) | |
611 | (define-key map "\C-c\C-e" 'tex-close-latex-block) | |
612 | (define-key map "\C-c\C-u" 'tex-goto-last-unclosed-latex-block) | |
613 | (define-key map "\C-c\C-m" 'tex-feed-input) | |
614 | (define-key map [(control return)] 'tex-feed-input) | |
615 | (define-key map [menu-bar tex tex-bibtex-file] | |
616 | '("BibTeX File" . tex-bibtex-file)) | |
617 | (define-key map [menu-bar tex tex-validate-region] | |
618 | '(menu-item "Validate Region" tex-validate-region :enable mark-active)) | |
619 | (define-key map [menu-bar tex tex-validate-buffer] | |
620 | '("Validate Buffer" . tex-validate-buffer)) | |
621 | (define-key map [menu-bar tex tex-region] | |
622 | '(menu-item "TeX Region" tex-region :enable mark-active)) | |
623 | (define-key map [menu-bar tex tex-buffer] | |
624 | '("TeX Buffer" . tex-buffer)) | |
625 | (define-key map [menu-bar tex tex-file] '("TeX File" . tex-file)) | |
626 | map) | |
15ca8de7 SM |
627 | "Keymap shared by TeX modes.") |
628 | ||
629 | (defvar latex-mode-map | |
630 | (let ((map (make-sparse-keymap))) | |
631 | (set-keymap-parent map tex-mode-map) | |
632 | map) | |
633 | "Keymap for `latex-mode'. See also `tex-mode-map'.") | |
634 | ||
635 | (defvar plain-tex-mode-map | |
636 | (let ((map (make-sparse-keymap))) | |
637 | (set-keymap-parent map tex-mode-map) | |
638 | map) | |
639 | "Keymap for `plain-tex-mode'. See also `tex-mode-map'.") | |
cf6d6e8a | 640 | |
d6709b80 SM |
641 | (defvar tex-shell-map |
642 | (let ((m (make-sparse-keymap))) | |
643 | (set-keymap-parent m shell-mode-map) | |
644 | (tex-define-common-keys m) | |
645 | m) | |
81c735c0 | 646 | "Keymap for the TeX shell. |
dca5ea48 | 647 | Inherits `shell-mode-map' with a few additions.") |
869bff31 | 648 | |
6da9bbd6 RS |
649 | (defvar tex-face-alist |
650 | '((bold . "{\\bf ") | |
651 | (italic . "{\\it ") | |
652 | (bold-italic . "{\\bi ") ; hypothetical | |
653 | (underline . "\\underline{") | |
654 | (default . "{\\rm ")) | |
655 | "Alist of face and TeX font name for facemenu.") | |
656 | ||
657 | (defvar tex-latex-face-alist | |
658 | `((italic . "{\\em ") | |
659 | ,@tex-face-alist) | |
660 | "Alist of face and LaTeX font name for facemenu.") | |
661 | ||
15ca8de7 SM |
662 | ;; This would be a lot simpler if we just used a regexp search, |
663 | ;; but then it would be too slow. | |
7229064d | 664 | ;;;###autoload |
869bff31 | 665 | (defun tex-mode () |
666 | "Major mode for editing files of input for TeX, LaTeX, or SliTeX. | |
667 | Tries to determine (by looking at the beginning of the file) whether | |
81c735c0 RS |
668 | this file is for plain TeX, LaTeX, or SliTeX and calls `plain-tex-mode', |
669 | `latex-mode', or `slitex-mode', respectively. If it cannot be determined, | |
670 | such as if there are no commands in the file, the value of `tex-default-mode' | |
671 | says which mode to use." | |
869bff31 | 672 | (interactive) |
d6709b80 | 673 | (let ((mode tex-default-mode) slash comment) |
869bff31 | 674 | (save-excursion |
675 | (goto-char (point-min)) | |
676 | (while (and (setq slash (search-forward "\\" nil t)) | |
677 | (setq comment (let ((search-end (point))) | |
678 | (save-excursion | |
679 | (beginning-of-line) | |
680 | (search-forward "%" search-end t)))))) | |
bdbd9606 SM |
681 | (when (and slash (not comment)) |
682 | (setq mode | |
683 | (if (looking-at | |
684 | (eval-when-compile | |
685 | (concat | |
686 | (regexp-opt '("documentstyle" "documentclass" | |
95a045bd SM |
687 | "begin" "subsection" "section" |
688 | "part" "chapter" "newcommand") 'words) | |
bdbd9606 SM |
689 | "\\|NeedsTeXFormat{LaTeX"))) |
690 | (if (looking-at | |
691 | "document\\(style\\|class\\)\\(\\[.*\\]\\)?{slides}") | |
692 | 'slitex-mode | |
693 | 'latex-mode) | |
694 | 'plain-tex-mode)))) | |
d6709b80 | 695 | (funcall mode))) |
e4c8c838 | 696 | |
6503cec3 | 697 | ;;;###autoload |
31e1d920 | 698 | (defalias 'TeX-mode 'tex-mode) |
6503cec3 | 699 | ;;;###autoload |
c3ce7bf4 RS |
700 | (defalias 'plain-TeX-mode 'plain-tex-mode) |
701 | ;;;###autoload | |
31e1d920 | 702 | (defalias 'LaTeX-mode 'latex-mode) |
869bff31 | 703 | |
7229064d | 704 | ;;;###autoload |
d6709b80 | 705 | (define-derived-mode plain-tex-mode text-mode "TeX" |
869bff31 | 706 | "Major mode for editing files of input for plain TeX. |
707 | Makes $ and } display the characters they match. | |
708 | Makes \" insert `` when it seems to be the beginning of a quotation, | |
709 | and '' when it appears to be the end; it inserts \" only after a \\. | |
710 | ||
711 | Use \\[tex-region] to run TeX on the current region, plus a \"header\" | |
712 | copied from the top of the file (containing macro definitions, etc.), | |
713 | running TeX under a special subshell. \\[tex-buffer] does the whole buffer. | |
714 | \\[tex-file] saves the buffer and then processes the file. | |
715 | \\[tex-print] prints the .dvi file made by any of these. | |
716 | \\[tex-view] previews the .dvi file made by any of these. | |
717 | \\[tex-bibtex-file] runs bibtex on the file of the current buffer. | |
718 | ||
e6b0b773 | 719 | Use \\[tex-validate-buffer] to check buffer for paragraphs containing |
869bff31 | 720 | mismatched $'s or braces. |
721 | ||
722 | Special commands: | |
15ca8de7 | 723 | \\{plain-tex-mode-map} |
869bff31 | 724 | |
725 | Mode variables: | |
726 | tex-run-command | |
727 | Command string used by \\[tex-region] or \\[tex-buffer]. | |
728 | tex-directory | |
729 | Directory in which to create temporary files for TeX jobs | |
730 | run by \\[tex-region] or \\[tex-buffer]. | |
731 | tex-dvi-print-command | |
732 | Command string used by \\[tex-print] to print a .dvi file. | |
528415e7 RS |
733 | tex-alt-dvi-print-command |
734 | Alternative command string used by \\[tex-print] (when given a prefix | |
735 | argument) to print a .dvi file. | |
869bff31 | 736 | tex-dvi-view-command |
737 | Command string used by \\[tex-view] to preview a .dvi file. | |
738 | tex-show-queue-command | |
739 | Command string used by \\[tex-show-print-queue] to show the print | |
740 | queue that \\[tex-print] put your job on. | |
741 | ||
81c735c0 RS |
742 | Entering Plain-tex mode runs the hook `text-mode-hook', then the hook |
743 | `tex-mode-hook', and finally the hook `plain-tex-mode-hook'. When the | |
744 | special subshell is initiated, the hook `tex-shell-hook' is run." | |
869bff31 | 745 | (tex-common-initialization) |
869bff31 | 746 | (setq tex-command tex-run-command) |
898b9ac1 RS |
747 | (setq tex-start-of-header "%\\*\\*start of header") |
748 | (setq tex-end-of-header "%\\*\\*end of header") | |
869bff31 | 749 | (setq tex-trailer "\\bye\n") |
d6709b80 | 750 | (run-hooks 'tex-mode-hook)) |
869bff31 | 751 | |
7229064d | 752 | ;;;###autoload |
d6709b80 | 753 | (define-derived-mode latex-mode text-mode "LaTeX" |
869bff31 | 754 | "Major mode for editing files of input for LaTeX. |
755 | Makes $ and } display the characters they match. | |
756 | Makes \" insert `` when it seems to be the beginning of a quotation, | |
757 | and '' when it appears to be the end; it inserts \" only after a \\. | |
758 | ||
759 | Use \\[tex-region] to run LaTeX on the current region, plus the preamble | |
760 | copied from the top of the file (containing \\documentstyle, etc.), | |
761 | running LaTeX under a special subshell. \\[tex-buffer] does the whole buffer. | |
762 | \\[tex-file] saves the buffer and then processes the file. | |
763 | \\[tex-print] prints the .dvi file made by any of these. | |
764 | \\[tex-view] previews the .dvi file made by any of these. | |
765 | \\[tex-bibtex-file] runs bibtex on the file of the current buffer. | |
766 | ||
e6b0b773 | 767 | Use \\[tex-validate-buffer] to check buffer for paragraphs containing |
869bff31 | 768 | mismatched $'s or braces. |
769 | ||
770 | Special commands: | |
15ca8de7 | 771 | \\{latex-mode-map} |
869bff31 | 772 | |
773 | Mode variables: | |
774 | latex-run-command | |
775 | Command string used by \\[tex-region] or \\[tex-buffer]. | |
776 | tex-directory | |
777 | Directory in which to create temporary files for LaTeX jobs | |
778 | run by \\[tex-region] or \\[tex-buffer]. | |
779 | tex-dvi-print-command | |
780 | Command string used by \\[tex-print] to print a .dvi file. | |
528415e7 RS |
781 | tex-alt-dvi-print-command |
782 | Alternative command string used by \\[tex-print] (when given a prefix | |
783 | argument) to print a .dvi file. | |
869bff31 | 784 | tex-dvi-view-command |
785 | Command string used by \\[tex-view] to preview a .dvi file. | |
786 | tex-show-queue-command | |
787 | Command string used by \\[tex-show-print-queue] to show the print | |
788 | queue that \\[tex-print] put your job on. | |
789 | ||
bed1b0c8 | 790 | Entering Latex mode runs the hook `text-mode-hook', then |
81c735c0 RS |
791 | `tex-mode-hook', and finally `latex-mode-hook'. When the special |
792 | subshell is initiated, `tex-shell-hook' is run." | |
869bff31 | 793 | (tex-common-initialization) |
869bff31 | 794 | (setq tex-command latex-run-command) |
dd459839 SM |
795 | (setq tex-start-of-header "\\\\document\\(style\\|class\\)") |
796 | (setq tex-end-of-header "\\\\begin\\s-*{document}") | |
797 | (setq tex-trailer "\\end\\s-*{document}\n") | |
ab2c9f54 RS |
798 | ;; A line containing just $$ is treated as a paragraph separator. |
799 | ;; A line starting with $$ starts a paragraph, | |
800 | ;; but does not separate paragraphs if it has more stuff on it. | |
d6709b80 | 801 | (setq paragraph-start |
dd459839 | 802 | (concat "[\f%]\\|[ \t]*\\($\\|\\$\\$\\|" |
d6709b80 SM |
803 | "\\\\[][]\\|" |
804 | "\\\\" (regexp-opt (append | |
805 | (mapcar 'car latex-section-alist) | |
806 | '("begin" "label" "end" | |
807 | "item" "bibitem" "newline" "noindent" | |
808 | "newpage" "footnote" "marginpar" | |
809 | "parbox" "caption")) t) | |
810 | "\\>\\|\\\\[a-z]*" (regexp-opt '("space" "skip" "page") t) | |
dd459839 | 811 | "\\>\\)")) |
d6709b80 | 812 | (setq paragraph-separate |
dd459839 | 813 | (concat "[\f%]\\|[ \t]*\\($\\|" |
d6709b80 SM |
814 | "\\\\[][]\\|" |
815 | "\\\\" (regexp-opt (append | |
816 | (mapcar 'car latex-section-alist) | |
817 | '("begin" "label" "end" )) t) | |
818 | "\\>\\|\\\\\\(" (regexp-opt '("item" "bibitem" "newline" | |
819 | "noindent" "newpage" "footnote" | |
820 | "marginpar" "parbox" "caption")) | |
dd459839 SM |
821 | "\\|\\$\\$\\|[a-z]*\\(space\\|skip\\|page[a-z]*\\)" |
822 | "\\>\\)[ \t]*\\($\\|%\\)\\)")) | |
d6709b80 SM |
823 | (set (make-local-variable 'imenu-create-index-function) |
824 | 'latex-imenu-create-index) | |
825 | (set (make-local-variable 'tex-face-alist) tex-latex-face-alist) | |
95a045bd | 826 | (add-hook 'fill-nobreak-predicate 'latex-fill-nobreak-predicate nil t) |
ea590e1c | 827 | (set (make-local-variable 'indent-line-function) 'latex-indent) |
dd459839 | 828 | (set (make-local-variable 'fill-indent-according-to-mode) t) |
d6709b80 SM |
829 | (set (make-local-variable 'outline-regexp) latex-outline-regexp) |
830 | (set (make-local-variable 'outline-level) 'latex-outline-level) | |
53c4fe47 | 831 | (set (make-local-variable 'forward-sexp-function) 'latex-forward-sexp) |
15ca8de7 | 832 | (set (make-local-variable 'skeleton-end-hook) nil) |
d6709b80 | 833 | (run-hooks 'tex-mode-hook)) |
869bff31 | 834 | |
bd2f2323 | 835 | ;;;###autoload |
d6709b80 | 836 | (define-derived-mode slitex-mode latex-mode "SliTeX" |
869bff31 | 837 | "Major mode for editing files of input for SliTeX. |
838 | Makes $ and } display the characters they match. | |
839 | Makes \" insert `` when it seems to be the beginning of a quotation, | |
840 | and '' when it appears to be the end; it inserts \" only after a \\. | |
841 | ||
842 | Use \\[tex-region] to run SliTeX on the current region, plus the preamble | |
843 | copied from the top of the file (containing \\documentstyle, etc.), | |
844 | running SliTeX under a special subshell. \\[tex-buffer] does the whole buffer. | |
845 | \\[tex-file] saves the buffer and then processes the file. | |
846 | \\[tex-print] prints the .dvi file made by any of these. | |
847 | \\[tex-view] previews the .dvi file made by any of these. | |
848 | \\[tex-bibtex-file] runs bibtex on the file of the current buffer. | |
849 | ||
e6b0b773 | 850 | Use \\[tex-validate-buffer] to check buffer for paragraphs containing |
869bff31 | 851 | mismatched $'s or braces. |
852 | ||
853 | Special commands: | |
15ca8de7 | 854 | \\{slitex-mode-map} |
869bff31 | 855 | |
856 | Mode variables: | |
857 | slitex-run-command | |
858 | Command string used by \\[tex-region] or \\[tex-buffer]. | |
859 | tex-directory | |
860 | Directory in which to create temporary files for SliTeX jobs | |
861 | run by \\[tex-region] or \\[tex-buffer]. | |
862 | tex-dvi-print-command | |
863 | Command string used by \\[tex-print] to print a .dvi file. | |
528415e7 RS |
864 | tex-alt-dvi-print-command |
865 | Alternative command string used by \\[tex-print] (when given a prefix | |
866 | argument) to print a .dvi file. | |
869bff31 | 867 | tex-dvi-view-command |
868 | Command string used by \\[tex-view] to preview a .dvi file. | |
869 | tex-show-queue-command | |
870 | Command string used by \\[tex-show-print-queue] to show the print | |
871 | queue that \\[tex-print] put your job on. | |
872 | ||
81c735c0 RS |
873 | Entering SliTeX mode runs the hook `text-mode-hook', then the hook |
874 | `tex-mode-hook', then the hook `latex-mode-hook', and finally the hook | |
875 | `slitex-mode-hook'. When the special subshell is initiated, the hook | |
876 | `tex-shell-hook' is run." | |
869bff31 | 877 | (setq tex-command slitex-run-command) |
d6709b80 | 878 | (setq tex-start-of-header "\\\\documentstyle{slides}\\|\\\\documentclass{slides}")) |
869bff31 | 879 | |
880 | (defun tex-common-initialization () | |
ea590e1c | 881 | (set-syntax-table tex-mode-syntax-table) |
822eddf4 | 882 | ;; Regexp isearch should accept newline and formfeed as whitespace. |
d6709b80 | 883 | (set (make-local-variable 'search-whitespace-regexp) "[ \t\r\n\f]+") |
64db2461 | 884 | ;; A line containing just $$ is treated as a paragraph separator. |
d6709b80 SM |
885 | (set (make-local-variable 'paragraph-start) |
886 | "[ \t]*$\\|[\f\\\\%]\\|[ \t]*\\$\\$") | |
ab2c9f54 RS |
887 | ;; A line starting with $$ starts a paragraph, |
888 | ;; but does not separate paragraphs if it has more stuff on it. | |
d6709b80 SM |
889 | (set (make-local-variable 'paragraph-separate) |
890 | "[ \t]*$\\|[\f\\\\%]\\|[ \t]*\\$\\$[ \t]*$") | |
891 | (set (make-local-variable 'comment-start) "%") | |
892 | (set (make-local-variable 'comment-add) 1) | |
893 | (set (make-local-variable 'comment-start-skip) | |
5ae0d697 | 894 | "\\(\\(^\\|[^\\\n]\\)\\(\\\\\\\\\\)*\\)\\(%+ *\\)") |
d6709b80 SM |
895 | (set (make-local-variable 'parse-sexp-ignore-comments) t) |
896 | (set (make-local-variable 'compare-windows-whitespace) | |
897 | 'tex-categorize-whitespace) | |
898 | (set (make-local-variable 'facemenu-add-face-function) | |
899 | (lambda (face end) | |
900 | (let ((face-text (cdr (assq face tex-face-alist)))) | |
901 | (if face-text | |
902 | face-text | |
903 | (error "Face %s not configured for %s mode" face mode-name))))) | |
904 | (set (make-local-variable 'facemenu-end-add-face) "}") | |
905 | (set (make-local-variable 'facemenu-remove-face-function) t) | |
906 | (set (make-local-variable 'font-lock-defaults) | |
907 | '((tex-font-lock-keywords | |
908 | tex-font-lock-keywords-1 tex-font-lock-keywords-2) | |
909 | nil nil ((?$ . "\"")) nil | |
910 | ;; Who ever uses that anyway ??? | |
bdbd9606 SM |
911 | (font-lock-mark-block-function . mark-paragraph) |
912 | (font-lock-syntactic-face-function | |
913 | . tex-font-lock-syntactic-face-function))) | |
95a045bd SM |
914 | ;; TABs in verbatim environments don't do what you think. |
915 | (set (make-local-variable 'indent-tabs-mode) nil) | |
869bff31 | 916 | (make-local-variable 'tex-command) |
917 | (make-local-variable 'tex-start-of-header) | |
918 | (make-local-variable 'tex-end-of-header) | |
919 | (make-local-variable 'tex-trailer)) | |
920 | ||
869bff31 | 921 | (defun tex-categorize-whitespace (backward-limit) |
922 | ;; compare-windows-whitespace is set to this. | |
923 | ;; This is basically a finite-state machine. | |
924 | ;; Returns a symbol telling how TeX would treat | |
925 | ;; the whitespace we are looking at: null, space, or par. | |
926 | (let ((category 'null) | |
927 | (not-finished t)) | |
928 | (skip-chars-backward " \t\n\f" backward-limit) | |
929 | (while not-finished | |
930 | (cond ((looking-at "[ \t]+") | |
931 | (goto-char (match-end 0)) | |
2be5fefb | 932 | (if (eq category 'null) |
869bff31 | 933 | (setq category 'space))) |
934 | ((looking-at "\n") | |
2be5fefb | 935 | (cond ((eq category 'newline) |
869bff31 | 936 | (setq category 'par) |
937 | (setq not-finished nil)) | |
938 | (t | |
939 | (setq category 'newline) ;a strictly internal state | |
940 | (goto-char (match-end 0))))) | |
941 | ((looking-at "\f+") | |
942 | (setq category 'par) | |
943 | (setq not-finished nil)) | |
944 | (t | |
945 | (setq not-finished nil)))) | |
946 | (skip-chars-forward " \t\n\f") | |
2be5fefb | 947 | (if (eq category 'newline) |
869bff31 | 948 | 'space ;TeX doesn't distinguish |
949 | category))) | |
950 | ||
951 | (defun tex-insert-quote (arg) | |
952 | "Insert the appropriate quote marks for TeX. | |
08348502 RS |
953 | Inserts the value of `tex-open-quote' (normally ``) or `tex-close-quote' |
954 | \(normally '') depending on the context. With prefix argument, always | |
869bff31 | 955 | inserts \" characters." |
528415e7 | 956 | (interactive "*P") |
95a045bd SM |
957 | (if (or arg (memq (char-syntax (preceding-char)) '(?/ ?\\)) |
958 | (eq (get-text-property (point) 'face) 'tex-verbatim-face) | |
959 | (save-excursion | |
960 | (backward-char (length tex-open-quote)) | |
961 | (when (or (looking-at (regexp-quote tex-open-quote)) | |
962 | (looking-at (regexp-quote tex-close-quote))) | |
963 | (delete-char (length tex-open-quote)) | |
964 | t))) | |
869bff31 | 965 | (self-insert-command (prefix-numeric-value arg)) |
95a045bd SM |
966 | (insert (if (memq (char-syntax (preceding-char)) '(?\( ?> ?\ )) |
967 | tex-open-quote tex-close-quote)))) | |
869bff31 | 968 | |
e6b0b773 MR |
969 | (defun tex-validate-buffer () |
970 | "Check current buffer for paragraphs containing mismatched braces or $s. | |
bbd93e41 | 971 | Their positions are recorded in the buffer `*Occur*'. |
e6b0b773 MR |
972 | To find a particular invalidity from `*Occur*', switch to that buffer |
973 | and type C-c C-c or click with mouse-2 | |
974 | on the line for the invalidity you want to see." | |
869bff31 | 975 | (interactive) |
bbd93e41 RS |
976 | (let ((buffer (current-buffer)) |
977 | (prevpos (point-min)) | |
e6b0b773 MR |
978 | (linenum nil) |
979 | (num-matches 0)) | |
bbd93e41 RS |
980 | (with-output-to-temp-buffer "*Occur*" |
981 | (princ "Mismatches:\n") | |
982 | (save-excursion | |
983 | (set-buffer standard-output) | |
984 | (occur-mode) | |
52ab062c CW |
985 | ;; This won't actually work...Really, this whole thing should |
986 | ;; be rewritten instead of being a hack on top of occur. | |
987 | (setq occur-revert-arguments (list nil 0 (list buffer)))) | |
bbd93e41 RS |
988 | (save-excursion |
989 | (goto-char (point-max)) | |
52ab062c | 990 | (while (and (not (bobp))) |
710a0f53 KH |
991 | (let ((end (point)) |
992 | prev-end) | |
bbd93e41 | 993 | ;; Scan the previous paragraph for invalidities. |
710a0f53 KH |
994 | (if (search-backward "\n\n" nil t) |
995 | (progn | |
996 | (setq prev-end (point)) | |
997 | (forward-char 2)) | |
998 | (goto-char (setq prev-end (point-min)))) | |
869bff31 | 999 | (or (tex-validate-region (point) end) |
710a0f53 KH |
1000 | (let* ((oend end) |
1001 | (end (save-excursion (forward-line 1) (point))) | |
bbd93e41 RS |
1002 | start tem) |
1003 | (beginning-of-line) | |
1004 | (setq start (point)) | |
1005 | ;; Keep track of line number as we scan, | |
1006 | ;; in a cumulative fashion. | |
1007 | (if linenum | |
1008 | (setq linenum (- linenum (count-lines prevpos (point)))) | |
1009 | (setq linenum (1+ (count-lines 1 start)))) | |
1010 | (setq prevpos (point)) | |
bed1b0c8 | 1011 | ;; Mention this mismatch in *Occur*. |
bbd93e41 | 1012 | ;; Since we scan from end of buffer to beginning, |
e6b0b773 | 1013 | ;; add each mismatch at the beginning of *Occur*. |
bbd93e41 RS |
1014 | (save-excursion |
1015 | (setq tem (point-marker)) | |
1016 | (set-buffer standard-output) | |
1017 | (goto-char (point-min)) | |
1018 | ;; Skip "Mismatches:" header line. | |
1019 | (forward-line 1) | |
e6b0b773 | 1020 | (setq num-matches (1+ num-matches)) |
bbd93e41 | 1021 | (insert-buffer-substring buffer start end) |
e6b0b773 MR |
1022 | (let (text-beg (text-end (point-marker))) |
1023 | (forward-char (- start end)) | |
1024 | (setq text-beg (point-marker)) | |
1025 | (insert (format "%3d: " linenum)) | |
a865de85 EZ |
1026 | (add-text-properties |
1027 | text-beg (- text-end 1) | |
1028 | '(mouse-face highlight | |
1029 | help-echo "mouse-2: go to this invalidity")) | |
1598a961 | 1030 | (put-text-property text-beg (- text-end 1) |
52ab062c | 1031 | 'occur-target tem))))) |
710a0f53 | 1032 | (goto-char prev-end)))) |
bdbd9606 | 1033 | (with-current-buffer standard-output |
e6b0b773 | 1034 | (if (eq num-matches 0) |
bbd93e41 RS |
1035 | (insert "None!\n")) |
1036 | (if (interactive-p) | |
e6b0b773 | 1037 | (message "%d mismatches found" num-matches)))))) |
869bff31 | 1038 | |
1039 | (defun tex-validate-region (start end) | |
1040 | "Check for mismatched braces or $'s in region. | |
1041 | Returns t if no mismatches. Returns nil and moves point to suspect | |
1042 | area if a mismatch is found." | |
1043 | (interactive "r") | |
1044 | (let ((failure-point nil) (max-possible-sexps (- end start))) | |
1045 | (save-excursion | |
1046 | (condition-case () | |
1047 | (save-restriction | |
1048 | (narrow-to-region start end) | |
2ed2806c | 1049 | ;; First check that the open and close parens balance in numbers. |
869bff31 | 1050 | (goto-char start) |
e6b0b773 | 1051 | (while (<= 0 (setq max-possible-sexps (1- max-possible-sexps))) |
2ed2806c KH |
1052 | (forward-sexp 1)) |
1053 | ;; Now check that like matches like. | |
1054 | (goto-char start) | |
1055 | (while (progn (skip-syntax-forward "^(") | |
1056 | (not (eobp))) | |
1057 | (let ((match (matching-paren (following-char)))) | |
1058 | (save-excursion | |
1059 | (forward-sexp 1) | |
1060 | (or (= (preceding-char) match) | |
1061 | (error "Mismatched parentheses")))) | |
1062 | (forward-char 1))) | |
869bff31 | 1063 | (error |
bed1b0c8 RS |
1064 | (skip-syntax-forward " .>") |
1065 | (setq failure-point (point))))) | |
bdbd9606 SM |
1066 | (if failure-point (goto-char failure-point)) |
1067 | (not failure-point))) | |
869bff31 | 1068 | |
1069 | (defun tex-terminate-paragraph (inhibit-validation) | |
1070 | "Insert two newlines, breaking a paragraph for TeX. | |
81c735c0 | 1071 | Check for mismatched braces or $s in paragraph being terminated. |
869bff31 | 1072 | A prefix arg inhibits the checking." |
528415e7 | 1073 | (interactive "*P") |
869bff31 | 1074 | (or inhibit-validation |
1075 | (save-excursion | |
1076 | (tex-validate-region | |
bed1b0c8 RS |
1077 | (save-excursion |
1078 | (search-backward "\n\n" nil 'move) | |
1079 | (point)) | |
1080 | (point))) | |
869bff31 | 1081 | (message "Paragraph being closed appears to contain a mismatch")) |
1082 | (insert "\n\n")) | |
1083 | ||
1084 | (defun tex-insert-braces () | |
1085 | "Make a pair of braces and be poised to type inside of them." | |
528415e7 | 1086 | (interactive "*") |
869bff31 | 1087 | (insert ?\{) |
1088 | (save-excursion | |
1089 | (insert ?}))) | |
1090 | ||
9bd4f69c RS |
1091 | ;; This function is used as the value of fill-nobreak-predicate |
1092 | ;; in LaTeX mode. Its job is to prevent line-breaking inside | |
1093 | ;; of a \verb construct. | |
1094 | (defun latex-fill-nobreak-predicate () | |
1095 | (let ((opoint (point)) | |
1096 | inside) | |
bed1b0c8 | 1097 | (save-excursion |
95a045bd SM |
1098 | (beginning-of-line) |
1099 | (while (re-search-forward "\\\\verb\\(.\\)" opoint t) | |
1100 | (unless (re-search-forward (regexp-quote (match-string 1)) opoint t) | |
1101 | (setq inside t)))) | |
9bd4f69c RS |
1102 | inside)) |
1103 | ||
53c4fe47 SM |
1104 | (defvar latex-block-default "enumerate") |
1105 | ||
15ca8de7 | 1106 | ;; Like tex-insert-braces, but for LaTeX. |
cf149bdf RS |
1107 | (define-skeleton tex-latex-block |
1108 | "Create a matching pair of lines \\begin[OPT]{NAME} and \\end{NAME} at point. | |
869bff31 | 1109 | Puts point on a blank line between them." |
53c4fe47 SM |
1110 | (let ((choice (completing-read (format "LaTeX block name [%s]: " |
1111 | latex-block-default) | |
95a045bd SM |
1112 | (append latex-block-names |
1113 | standard-latex-block-names) | |
53c4fe47 SM |
1114 | nil nil nil nil latex-block-default))) |
1115 | (setq latex-block-default choice) | |
1116 | (unless (or (member choice standard-latex-block-names) | |
1117 | (member choice latex-block-names)) | |
1118 | ;; Remember new block names for later completion. | |
1119 | (push choice latex-block-names)) | |
1120 | choice) | |
95a045bd | 1121 | \n "\\begin{" str "}" |
53c4fe47 | 1122 | ?\[ (skeleton-read "[options]: ") & ?\] | -1 |
dd459839 | 1123 | > \n _ \n |
95a045bd | 1124 | "\\end{" str "}" > \n) |
53c4fe47 | 1125 | |
ea590e1c SM |
1126 | (define-skeleton latex-insert-item |
1127 | "Insert a \item macro." | |
1128 | nil | |
15ca8de7 | 1129 | \n "\\item " >) |
ea590e1c | 1130 | |
53c4fe47 SM |
1131 | \f |
1132 | ;;;; | |
1133 | ;;;; LaTeX syntax navigation | |
1134 | ;;;; | |
869bff31 | 1135 | |
1136 | (defun tex-last-unended-begin () | |
81c735c0 | 1137 | "Leave point at the beginning of the last `\\begin{...}' that is unended." |
95a045bd SM |
1138 | (condition-case nil |
1139 | (while (and (re-search-backward "\\\\\\(begin\\|end\\)\\s *{") | |
1140 | (looking-at "\\\\end")) | |
1141 | (tex-last-unended-begin)) | |
1142 | (search-failed (error "Couldn't find unended \\begin")))) | |
869bff31 | 1143 | |
53c4fe47 SM |
1144 | (defun tex-next-unmatched-end () |
1145 | "Leave point at the end of the next `\\end' that is unended." | |
1146 | (while (and (re-search-forward "\\\\\\(begin\\|end\\)\\s *{[^}]+}") | |
ea590e1c SM |
1147 | (save-excursion (goto-char (match-beginning 0)) |
1148 | (looking-at "\\\\begin"))) | |
53c4fe47 SM |
1149 | (tex-next-unmatched-end))) |
1150 | ||
6a900cf1 ER |
1151 | (defun tex-goto-last-unclosed-latex-block () |
1152 | "Move point to the last unclosed \\begin{...}. | |
1153 | Mark is left at original location." | |
1154 | (interactive) | |
1155 | (let ((spot)) | |
1156 | (save-excursion | |
95a045bd | 1157 | (tex-last-unended-begin) |
6a900cf1 ER |
1158 | (setq spot (point))) |
1159 | (push-mark) | |
1160 | (goto-char spot))) | |
1161 | ||
53c4fe47 SM |
1162 | (defun latex-backward-sexp-1 () |
1163 | "Like (backward-sexp 1) but aware of multi-char elements." | |
1164 | (let ((pos (point)) | |
1165 | (forward-sexp-function)) | |
1166 | (backward-sexp 1) | |
1167 | (if (looking-at "\\\\begin\\>") | |
1168 | (signal 'scan-error | |
1169 | (list "Containing expression ends prematurely" | |
1170 | (point) (prog1 (point) (goto-char pos)))) | |
1171 | (when (eq (char-after) ?{) | |
1172 | (let ((newpos (point))) | |
1173 | (when (ignore-errors (backward-sexp 1) t) | |
1174 | (if (looking-at "\\\\end\\>") | |
1175 | (tex-last-unended-begin) | |
1176 | (goto-char newpos)))))))) | |
1177 | ||
1178 | (defun latex-forward-sexp-1 () | |
1179 | "Like (forward-sexp 1) but aware of multi-char elements." | |
1180 | (let ((pos (point)) | |
1181 | (forward-sexp-function)) | |
1182 | (forward-sexp 1) | |
1183 | (let ((newpos (point))) | |
1184 | (skip-syntax-backward "/w") | |
1185 | (cond | |
1186 | ((looking-at "\\\\end\\>") | |
1187 | (signal 'scan-error | |
1188 | (list "Containing expression ends prematurely" | |
1189 | (point) | |
1190 | (prog1 | |
1191 | (progn (ignore-errors (forward-sexp 2)) (point)) | |
1192 | (goto-char pos))))) | |
1193 | ((looking-at "\\\\begin\\>") | |
1194 | (goto-char (match-end 0)) | |
1195 | (tex-next-unmatched-end)) | |
1196 | (t (goto-char newpos)))))) | |
1197 | ||
1198 | (defun latex-forward-sexp (&optional arg) | |
1199 | "Like `forward-sexp' but aware of multi-char elements." | |
1200 | (interactive "P") | |
1201 | (unless arg (setq arg 1)) | |
1202 | (let ((pos (point))) | |
1203 | (condition-case err | |
1204 | (while (/= arg 0) | |
1205 | (setq arg | |
1206 | (if (> arg 0) | |
1207 | (progn (latex-forward-sexp-1) (1- arg)) | |
1208 | (progn (latex-backward-sexp-1) (1+ arg))))) | |
1209 | (scan-error | |
1210 | (goto-char pos) | |
1211 | (signal (car err) (cdr err)))))) | |
1212 | ||
ea590e1c SM |
1213 | (defun latex-syntax-after () |
1214 | "Like (char-syntax (char-after)) but aware of multi-char elements." | |
15ca8de7 | 1215 | (if (looking-at "\\\\end\\>") ?\) (char-syntax (following-char)))) |
ea590e1c SM |
1216 | |
1217 | (defun latex-skip-close-parens () | |
1218 | "Like (skip-syntax-forward \" )\") but aware of multi-char elements." | |
1219 | (let ((forward-sexp-function nil)) | |
1220 | (while (progn (skip-syntax-forward " )") | |
1221 | (looking-at "\\\\end\\>")) | |
1222 | (forward-sexp 2)))) | |
1223 | ||
1224 | (defun latex-down-list () | |
1225 | "Like (down-list 1) but aware of multi-char elements." | |
1226 | (forward-comment (point-max)) | |
1227 | (let ((forward-sexp-function nil)) | |
1228 | (if (not (looking-at "\\\\begin\\>")) | |
1229 | (down-list 1) | |
1230 | (forward-sexp 1) | |
1231 | ;; Skip arguments. | |
1232 | (while (looking-at "[ \t]*\\s(") (forward-sexp))))) | |
1233 | ||
869bff31 | 1234 | (defun tex-close-latex-block () |
1235 | "Creates an \\end{...} to match the last unclosed \\begin{...}." | |
1236 | (interactive "*") | |
1237 | (let ((new-line-needed (bolp)) | |
1238 | text indentation) | |
1239 | (save-excursion | |
528415e7 | 1240 | (condition-case nil |
869bff31 | 1241 | (tex-last-unended-begin) |
1242 | (error (error "Couldn't find unended \\begin"))) | |
1243 | (setq indentation (current-column)) | |
1244 | (re-search-forward "\\\\begin\\(\\s *{[^}\n]*}\\)") | |
1245 | (setq text (buffer-substring (match-beginning 1) (match-end 1)))) | |
1246 | (indent-to indentation) | |
1247 | (insert "\\end" text) | |
1248 | (if new-line-needed (insert ?\n)))) | |
15ca8de7 SM |
1249 | |
1250 | (defconst tex-discount-args-cmds | |
1251 | '("begin" "end" "input" "special" "cite" "ref" "include" "includeonly" | |
1252 | "documentclass" "usepackage" "label") | |
1253 | "TeX commands whose arguments should not be counted as text.") | |
1254 | ||
1255 | (defun tex-count-words (begin end) | |
1256 | "Count the number of words in the buffer." | |
1257 | (interactive | |
1258 | (if (and transient-mark-mode mark-active) | |
1259 | (list (region-beginning) (region-end)) | |
1260 | (list (point-min) (point-max)))) | |
1261 | ;; TODO: skip comments and math and maybe some environments. | |
1262 | (save-excursion | |
1263 | (goto-char begin) | |
1264 | (let ((count 0)) | |
1265 | (while (and (< (point) end) (re-search-forward "\\<" end t)) | |
1266 | (if (not (eq (char-syntax (preceding-char)) ?/)) | |
1267 | (progn | |
1268 | ;; Don't count single-char words. | |
1269 | (unless (looking-at ".\\>") (incf count)) | |
1270 | (forward-char 1)) | |
1271 | (let ((cmd | |
1272 | (buffer-substring-no-properties | |
1273 | (point) (progn (when (zerop (skip-chars-forward "a-zA-Z@")) | |
1274 | (forward-char 1)) | |
1275 | (point))))) | |
1276 | (when (member cmd tex-discount-args-cmds) | |
1277 | (skip-chars-forward "*") | |
1278 | (forward-comment (point-max)) | |
1279 | (when (looking-at "\\[") | |
1280 | (forward-sexp 1) | |
1281 | (forward-comment (point-max))) | |
1282 | (if (not (looking-at "{")) | |
1283 | (forward-char 1) | |
1284 | (forward-sexp 1)))))) | |
1285 | (message "%s words" count)))) | |
b781e739 SS |
1286 | |
1287 | ||
869bff31 | 1288 | \f |
1289 | ;;; Invoking TeX in an inferior shell. | |
1290 | ||
15ca8de7 SM |
1291 | ;; Why use a shell instead of running TeX directly? Because if TeX |
1292 | ;; gets stuck, the user can switch to the shell window and type at it. | |
869bff31 | 1293 | |
15ca8de7 | 1294 | ;; The utility functions: |
869bff31 | 1295 | |
bdbd9606 SM |
1296 | (define-derived-mode tex-shell shell-mode "TeX-Shell" |
1297 | (compilation-shell-minor-mode t)) | |
1298 | ||
d974af30 | 1299 | ;;;###autoload |
869bff31 | 1300 | (defun tex-start-shell () |
d6709b80 SM |
1301 | (with-current-buffer |
1302 | (make-comint | |
1303 | "tex-shell" | |
07c16ec4 | 1304 | (or tex-shell-file-name (getenv "ESHELL") shell-file-name) |
d6709b80 | 1305 | nil) |
528415e7 RS |
1306 | (let ((proc (get-process "tex-shell"))) |
1307 | (set-process-sentinel proc 'tex-shell-sentinel) | |
1308 | (process-kill-without-query proc) | |
bdbd9606 | 1309 | (tex-shell) |
528415e7 | 1310 | (while (zerop (buffer-size)) |
51b2c841 RS |
1311 | (sleep-for 1))))) |
1312 | ||
033306e3 RS |
1313 | (defun tex-feed-input () |
1314 | "Send input to the tex shell process. | |
1315 | In the tex buffer this can be used to continue an interactive tex run. | |
bed1b0c8 | 1316 | In the tex shell buffer this command behaves like `comint-send-input'." |
033306e3 | 1317 | (interactive) |
b781e739 | 1318 | (set-buffer (tex-shell-buf)) |
033306e3 RS |
1319 | (comint-send-input) |
1320 | (tex-recenter-output-buffer nil)) | |
1321 | ||
51b2c841 RS |
1322 | (defun tex-display-shell () |
1323 | "Make the TeX shell buffer visible in a window." | |
b781e739 | 1324 | (display-buffer (tex-shell-buf)) |
51b2c841 | 1325 | (tex-recenter-output-buffer nil)) |
528415e7 RS |
1326 | |
1327 | (defun tex-shell-sentinel (proc msg) | |
1328 | (cond ((null (buffer-name (process-buffer proc))) | |
1329 | ;; buffer killed | |
1330 | (set-process-buffer proc nil) | |
1331 | (tex-delete-last-temp-files)) | |
1332 | ((memq (process-status proc) '(signal exit)) | |
1333 | (tex-delete-last-temp-files)))) | |
1334 | ||
1335 | (defun tex-set-buffer-directory (buffer directory) | |
869bff31 | 1336 | "Set BUFFER's default directory to be DIRECTORY." |
1337 | (setq directory (file-name-as-directory (expand-file-name directory))) | |
1338 | (if (not (file-directory-p directory)) | |
1339 | (error "%s is not a directory" directory) | |
1340 | (save-excursion | |
1341 | (set-buffer buffer) | |
1342 | (setq default-directory directory)))) | |
1343 | ||
30803a05 RS |
1344 | (defvar tex-send-command-modified-tick 0) |
1345 | (make-variable-buffer-local 'tex-send-command-modified-tick) | |
1346 | ||
bdbd9606 | 1347 | (defun tex-shell-proc () |
b781e739 | 1348 | (or (tex-shell-running) (error "No TeX subprocess"))) |
bdbd9606 SM |
1349 | (defun tex-shell-buf () |
1350 | (process-buffer (tex-shell-proc))) | |
b781e739 SS |
1351 | (defun tex-shell-buf-no-error () |
1352 | (let ((proc (tex-shell-running))) | |
1353 | (and proc (process-buffer proc)))) | |
bdbd9606 | 1354 | |
528415e7 | 1355 | (defun tex-send-command (command &optional file background) |
4cdc1d4b | 1356 | "Send COMMAND to TeX shell process, substituting optional FILE for *. |
8241d7b9 ER |
1357 | Do this in background if optional BACKGROUND is t. If COMMAND has no *, |
1358 | FILE will be appended, preceded by a blank, to COMMAND. If FILE is nil, no | |
1359 | substitution will be made in COMMAND. COMMAND can be any expression that | |
460e1b7d RS |
1360 | evaluates to a command string. |
1361 | ||
1362 | Return the process in which TeX is running." | |
528415e7 RS |
1363 | (save-excursion |
1364 | (let* ((cmd (eval command)) | |
bdbd9606 | 1365 | (proc (tex-shell-proc)) |
64db2461 | 1366 | (buf (process-buffer proc)) |
4f45adda | 1367 | (star (string-match "\\*" cmd)) |
4cdc1d4b RS |
1368 | (string |
1369 | (concat | |
1370 | (if file | |
1371 | (if star (concat (substring cmd 0 star) | |
1372 | file (substring cmd (1+ star))) | |
1373 | (concat cmd " " file)) | |
1374 | cmd) | |
1375 | (if background "&" "")))) | |
64db2461 RS |
1376 | ;; Switch to buffer before checking for subproc output in it. |
1377 | (set-buffer buf) | |
30803a05 RS |
1378 | ;; If text is unchanged since previous tex-send-command, |
1379 | ;; we haven't got any output. So wait for output now. | |
64db2461 | 1380 | (if (= (buffer-modified-tick buf) tex-send-command-modified-tick) |
30803a05 | 1381 | (accept-process-output proc)) |
4cdc1d4b RS |
1382 | (goto-char (process-mark proc)) |
1383 | (insert string) | |
30803a05 | 1384 | (comint-send-input) |
460e1b7d RS |
1385 | (setq tex-send-command-modified-tick (buffer-modified-tick buf)) |
1386 | proc))) | |
528415e7 | 1387 | |
a15849cb RS |
1388 | (defun tex-delete-last-temp-files (&optional not-all) |
1389 | "Delete any junk files from last temp file. | |
1390 | If NOT-ALL is non-nil, save the `.dvi' file." | |
528415e7 RS |
1391 | (if tex-last-temp-file |
1392 | (let* ((dir (file-name-directory tex-last-temp-file)) | |
adf6b7f9 KH |
1393 | (list (and (file-directory-p dir) |
1394 | (file-name-all-completions | |
02fd229c RS |
1395 | (file-name-sans-extension |
1396 | (file-name-nondirectory tex-last-temp-file)) | |
1397 | dir)))) | |
adf6b7f9 | 1398 | (while list |
a15849cb RS |
1399 | (if not-all |
1400 | (and | |
1401 | ;; If arg is non-nil, don't delete the .dvi file. | |
1402 | (not (string-match "\\.dvi$" (car list))) | |
1403 | (delete-file (concat dir (car list)))) | |
1404 | (delete-file (concat dir (car list)))) | |
528415e7 RS |
1405 | (setq list (cdr list)))))) |
1406 | ||
99621a14 | 1407 | (add-hook 'kill-emacs-hook 'tex-delete-last-temp-files) |
869bff31 | 1408 | |
dd459839 SM |
1409 | (defun tex-guess-main-file (&optional all) |
1410 | "Find a likely `tex-main-file'. | |
1411 | Looks for hints in other buffers in the same directory or in | |
1412 | ALL other buffers." | |
1413 | (let ((dir default-directory) | |
1414 | (header-re tex-start-of-header)) | |
1415 | (catch 'found | |
1416 | ;; Look for a buffer with `tex-main-file' set. | |
1417 | (dolist (buf (if (consp all) all (buffer-list))) | |
1418 | (with-current-buffer buf | |
1419 | (when (and (or all (equal dir default-directory)) | |
1420 | (stringp tex-main-file)) | |
1421 | (throw 'found (expand-file-name tex-main-file))))) | |
1422 | ;; Look for a buffer containing the magic `tex-start-of-header'. | |
1423 | (dolist (buf (if (consp all) all (buffer-list))) | |
1424 | (with-current-buffer buf | |
1425 | (when (and (or all (equal dir default-directory)) | |
1426 | buffer-file-name | |
1427 | ;; (or (easy-mmode-derived-mode-p 'latex-mode) | |
1428 | ;; (easy-mmode-derived-mode-p 'plain-tex-mode)) | |
1429 | (save-excursion | |
cb30255a SM |
1430 | (save-restriction |
1431 | (widen) | |
1432 | (goto-char (point-min)) | |
95a045bd SM |
1433 | (re-search-forward |
1434 | header-re (+ (point) 10000) t)))) | |
dd459839 SM |
1435 | (throw 'found (expand-file-name buffer-file-name)))))))) |
1436 | ||
07c16ec4 SM |
1437 | (defun tex-main-file () |
1438 | "Return the relative name of the main file." | |
bdbd9606 SM |
1439 | (let* ((file (or tex-main-file |
1440 | ;; Compatibility with AUCTeX. | |
1441 | (and (boundp 'TeX-master) (stringp TeX-master) | |
1442 | (set (make-local-variable 'tex-main-file) TeX-master)) | |
1443 | ;; Try to guess the main file. | |
1444 | (if (not buffer-file-name) | |
1445 | (error "Buffer is not associated with any file") | |
1446 | (file-relative-name | |
1447 | (if (save-excursion | |
1448 | (goto-char (point-min)) | |
95a045bd SM |
1449 | (re-search-forward tex-start-of-header |
1450 | (+ (point) 10000) t)) | |
bdbd9606 SM |
1451 | ;; This is the main file. |
1452 | buffer-file-name | |
1453 | ;; This isn't the main file, let's try to find better, | |
1454 | (or (tex-guess-main-file) | |
1455 | ;; (tex-guess-main-file t) | |
07c16ec4 SM |
1456 | buffer-file-name))))))) |
1457 | (if (file-exists-p file) file (concat file ".tex")))) | |
dd459839 | 1458 | |
d6709b80 SM |
1459 | |
1460 | (defun tex-start-tex (command file &optional dir) | |
460e1b7d | 1461 | "Start a TeX run, using COMMAND on FILE." |
6633b891 | 1462 | (let* ((star (string-match "\\*" command)) |
460e1b7d | 1463 | (compile-command |
6633b891 KH |
1464 | (if star |
1465 | (concat (substring command 0 star) | |
1466 | (comint-quote-filename file) | |
1467 | (substring command (1+ star))) | |
1468 | (concat command " " | |
95a045bd | 1469 | tex-start-options |
9e0ad27a | 1470 | (if (< 0 (length tex-start-commands)) |
c3ce7bf4 | 1471 | (concat |
9e0ad27a | 1472 | (shell-quote-argument tex-start-commands) " ")) |
6633b891 | 1473 | (comint-quote-filename file))))) |
bdbd9606 SM |
1474 | (tex-send-tex-command compile-command dir))) |
1475 | ||
1476 | (defun tex-send-tex-command (cmd &optional dir) | |
b781e739 SS |
1477 | (unless (or (equal dir (let ((buf (tex-shell-buf-no-error))) |
1478 | (and buf (with-current-buffer buf | |
1479 | default-directory)))) | |
bdbd9606 SM |
1480 | (not dir)) |
1481 | (let (shell-dirtrack-verbose) | |
1482 | (tex-send-command tex-shell-cd-command dir))) | |
1483 | (with-current-buffer (process-buffer (tex-send-command cmd)) | |
bdbd9606 SM |
1484 | (make-local-variable 'compilation-parse-errors-function) |
1485 | (setq compilation-parse-errors-function 'tex-compilation-parse-errors) | |
1486 | (setq compilation-last-buffer (current-buffer)) | |
1487 | (compilation-forget-errors) | |
1598a961 | 1488 | ;; Don't parse previous compilations. |
bdbd9606 SM |
1489 | (set-marker compilation-parsing-end (1- (point-max)))) |
1490 | (tex-display-shell) | |
1491 | (setq tex-last-buffer-texed (current-buffer))) | |
460e1b7d | 1492 | \f |
1598a961 SM |
1493 | (defvar tex-error-parse-syntax-table |
1494 | (let ((st (make-syntax-table))) | |
1495 | (modify-syntax-entry ?\( "()" st) | |
1496 | (modify-syntax-entry ?\) ")(" st) | |
1497 | (modify-syntax-entry ?\\ "\\" st) | |
1498 | (modify-syntax-entry ?\{ "_" st) | |
1499 | (modify-syntax-entry ?\} "_" st) | |
1500 | (modify-syntax-entry ?\[ "_" st) | |
1501 | (modify-syntax-entry ?\] "_" st) | |
1502 | ;; Single quotations may appear in errors | |
1503 | (modify-syntax-entry ?\" "_" st) | |
1504 | st) | |
1505 | "Syntax-table used while parsing TeX error messages.") | |
1506 | ||
460e1b7d | 1507 | (defun tex-compilation-parse-errors (limit-search find-at-least) |
f05bd645 RS |
1508 | "Parse the current buffer as TeX error messages. |
1509 | See the variable `compilation-parse-errors-function' for the interface it uses. | |
1510 | ||
1511 | This function parses only the last TeX compilation. | |
1512 | It works on TeX compilations only. It is necessary for that purpose, | |
1513 | since TeX does not put file names and line numbers on the same line as | |
1514 | for the error messages." | |
460e1b7d RS |
1515 | (require 'thingatpt) |
1516 | (setq compilation-error-list nil) | |
1517 | (message "Parsing error messages...") | |
f05bd645 RS |
1518 | (let ((default-directory ; Perhaps dir has changed meanwhile. |
1519 | (file-name-directory (buffer-file-name tex-last-buffer-texed))) | |
f05bd645 RS |
1520 | found-desired (num-errors-found 0) |
1521 | last-filename last-linenum last-position | |
1522 | begin-of-error end-of-error) | |
f05bd645 RS |
1523 | ;; Don't reparse messages already seen at last parse. |
1524 | (goto-char compilation-parsing-end) | |
1525 | ;; Parse messages. | |
1526 | (while (and (not (or found-desired (eobp))) | |
1527 | (prog1 (re-search-forward "^! " nil 'move) | |
1528 | (setq begin-of-error (match-beginning 0) | |
1529 | end-of-error (match-end 0))) | |
1530 | (re-search-forward | |
1531 | "^l\\.\\([0-9]+\\) \\(\\.\\.\\.\\)?\\(.*\\)$" nil 'move)) | |
1532 | (let* ((this-error (set-marker (make-marker) begin-of-error)) | |
1533 | (linenum (string-to-int (match-string 1))) | |
1534 | (error-text (regexp-quote (match-string 3))) | |
1535 | (filename | |
1536 | (save-excursion | |
ea590e1c SM |
1537 | (with-syntax-table tex-error-parse-syntax-table |
1538 | (backward-up-list 1) | |
1539 | (skip-syntax-forward "(_") | |
1540 | (while (not (file-readable-p (thing-at-point 'filename))) | |
1541 | (skip-syntax-backward "(_") | |
1542 | (backward-up-list 1) | |
1543 | (skip-syntax-forward "(_")) | |
1544 | (thing-at-point 'filename)))) | |
f05bd645 RS |
1545 | (new-file |
1546 | (or (null last-filename) | |
1547 | (not (string-equal last-filename filename)))) | |
1548 | (error-location | |
1549 | (save-excursion | |
1550 | (if (equal filename (concat tex-zap-file ".tex")) | |
1551 | (set-buffer tex-last-buffer-texed) | |
1552 | (set-buffer (find-file-noselect filename))) | |
1553 | (if new-file | |
1598a961 | 1554 | (progn (goto-line linenum) (setq last-position nil)) |
f05bd645 RS |
1555 | (goto-char last-position) |
1556 | (forward-line (- linenum last-linenum))) | |
1557 | ;; first try a forward search for the error text, | |
1558 | ;; then a backward search limited by the last error. | |
1559 | (let ((starting-point (point))) | |
1560 | (or (re-search-forward error-text nil t) | |
1598a961 | 1561 | (re-search-backward error-text last-position t) |
f05bd645 RS |
1562 | (goto-char starting-point))) |
1563 | (point-marker)))) | |
1564 | (goto-char this-error) | |
1565 | (if (and compilation-error-list | |
1566 | (or (and find-at-least | |
1567 | (>= num-errors-found | |
1568 | find-at-least)) | |
1569 | (and limit-search | |
1570 | (>= end-of-error limit-search))) | |
1571 | new-file) | |
1572 | (setq found-desired t) | |
1573 | (setq num-errors-found (1+ num-errors-found) | |
1574 | last-filename filename | |
1575 | last-linenum linenum | |
1576 | last-position error-location | |
1577 | compilation-error-list ; Add the new error | |
1578 | (cons (cons this-error error-location) | |
1579 | compilation-error-list)) | |
1580 | (goto-char end-of-error))))) | |
ee13a145 | 1581 | (set-marker compilation-parsing-end (point)) |
f05bd645 | 1582 | (setq compilation-error-list (nreverse compilation-error-list)) |
460e1b7d RS |
1583 | (message "Parsing error messages...done")) |
1584 | \f | |
528415e7 | 1585 | ;;; The commands: |
869bff31 | 1586 | |
1587 | (defun tex-region (beg end) | |
1588 | "Run TeX on the current region, via a temporary file. | |
1589 | The file's name comes from the variable `tex-zap-file' and the | |
1590 | variable `tex-directory' says where to put it. | |
1591 | ||
1592 | If the buffer has a header, the header is given to TeX before the | |
1593 | region itself. The buffer's header is all lines between the strings | |
1594 | defined by `tex-start-of-header' and `tex-end-of-header' inclusive. | |
1595 | The header must start in the first 100 lines of the buffer. | |
1596 | ||
1597 | The value of `tex-trailer' is given to TeX as input after the region. | |
1598 | ||
1599 | The value of `tex-command' specifies the command to use to run TeX." | |
1600 | (interactive "r") | |
1601 | (if (tex-shell-running) | |
1602 | (tex-kill-job) | |
1603 | (tex-start-shell)) | |
1604 | (or tex-zap-file | |
1605 | (setq tex-zap-file (tex-generate-zap-file-name))) | |
6d34c59f RS |
1606 | ;; Temp file will be written and TeX will be run in zap-directory. |
1607 | ;; If the TEXINPUTS file has relative directories or if the region has | |
1608 | ;; \input of files, this must be the same directory as the file for | |
1609 | ;; TeX to access the correct inputs. That's why it's safest if | |
1610 | ;; tex-directory is ".". | |
1611 | (let* ((zap-directory | |
528415e7 | 1612 | (file-name-as-directory (expand-file-name tex-directory))) |
d6709b80 SM |
1613 | (tex-out-file (expand-file-name (concat tex-zap-file ".tex") |
1614 | zap-directory))) | |
0d548e5d RS |
1615 | ;; Don't delete temp files if we do the same buffer twice in a row. |
1616 | (or (eq (current-buffer) tex-last-buffer-texed) | |
1617 | (tex-delete-last-temp-files t)) | |
869bff31 | 1618 | ;; Write the new temp file. |
1619 | (save-excursion | |
1620 | (save-restriction | |
1621 | (widen) | |
1622 | (goto-char (point-min)) | |
1623 | (forward-line 100) | |
1624 | (let ((search-end (point)) | |
6d34c59f RS |
1625 | (default-directory zap-directory) |
1626 | (already-output 0)) | |
869bff31 | 1627 | (goto-char (point-min)) |
6d34c59f | 1628 | |
725b7428 RS |
1629 | ;; Maybe copy first line, such as `\input texinfo', to temp file. |
1630 | (and tex-first-line-header-regexp | |
1631 | (looking-at tex-first-line-header-regexp) | |
bed1b0c8 | 1632 | (write-region (point) |
6d34c59f RS |
1633 | (progn (forward-line 1) |
1634 | (setq already-output (point))) | |
725b7428 RS |
1635 | tex-out-file nil nil)) |
1636 | ||
6d34c59f RS |
1637 | ;; Write out the header, if there is one, |
1638 | ;; and any of the specified region which extends before it. | |
1639 | ;; But don't repeat anything already written. | |
898b9ac1 | 1640 | (if (re-search-forward tex-start-of-header search-end t) |
6d34c59f | 1641 | (let (hbeg) |
869bff31 | 1642 | (beginning-of-line) |
1643 | (setq hbeg (point)) ;mark beginning of header | |
898b9ac1 | 1644 | (if (re-search-forward tex-end-of-header nil t) |
6d34c59f RS |
1645 | (let (hend) |
1646 | (forward-line 1) | |
bed1b0c8 | 1647 | (setq hend (point)) ;mark end of header |
6d34c59f RS |
1648 | (write-region (max (min hbeg beg) already-output) |
1649 | hend | |
1650 | tex-out-file | |
1651 | (not (zerop already-output)) nil) | |
1652 | (setq already-output hend))))) | |
1653 | ||
1654 | ;; Write out the specified region | |
1655 | ;; (but don't repeat anything already written). | |
1656 | (write-region (max beg already-output) end | |
1657 | tex-out-file | |
1658 | (not (zerop already-output)) nil)) | |
1659 | ;; Write the trailer, if any. | |
1660 | ;; Precede it with a newline to make sure it | |
1661 | ;; is not hidden in a comment. | |
1662 | (if tex-trailer | |
1663 | (write-region (concat "\n" tex-trailer) nil | |
1664 | tex-out-file t nil)))) | |
528415e7 RS |
1665 | ;; Record the file name to be deleted afterward. |
1666 | (setq tex-last-temp-file tex-out-file) | |
f05bd645 RS |
1667 | ;; Use a relative file name here because (1) the proper dir |
1668 | ;; is already current, and (2) the abs file name is sometimes | |
1669 | ;; too long and can make tex crash. | |
d6709b80 SM |
1670 | (tex-start-tex tex-command (concat tex-zap-file ".tex") zap-directory) |
1671 | (setq tex-print-file tex-out-file))) | |
869bff31 | 1672 | |
1673 | (defun tex-buffer () | |
1674 | "Run TeX on current buffer. See \\[tex-region] for more information. | |
528415e7 RS |
1675 | Does not save the buffer, so it's useful for trying experimental versions. |
1676 | See \\[tex-file] for an alternative." | |
869bff31 | 1677 | (interactive) |
1678 | (tex-region (point-min) (point-max))) | |
1679 | ||
1680 | (defun tex-file () | |
1681 | "Prompt to save all buffers and run TeX (or LaTeX) on current buffer's file. | |
1682 | This function is more useful than \\[tex-buffer] when you need the | |
1683 | `.aux' file of LaTeX to have the correct name." | |
1684 | (interactive) | |
7abc9add SS |
1685 | (when tex-offer-save |
1686 | (save-some-buffers)) | |
d6709b80 | 1687 | (let* ((source-file (tex-main-file)) |
07c16ec4 | 1688 | (file-dir (file-name-directory (expand-file-name source-file)))) |
869bff31 | 1689 | (if (tex-shell-running) |
1690 | (tex-kill-job) | |
1691 | (tex-start-shell)) | |
d6709b80 | 1692 | (tex-start-tex tex-command source-file file-dir) |
f05bd645 | 1693 | (setq tex-print-file (expand-file-name source-file)))) |
869bff31 | 1694 | |
1695 | (defun tex-generate-zap-file-name () | |
1696 | "Generate a unique name suitable for use as a file name." | |
1697 | ;; Include the shell process number and host name | |
1698 | ;; in case there are multiple shells (for same or different user). | |
f5cdb851 KH |
1699 | ;; Dec 1998: There is a report that some versions of xdvi |
1700 | ;; don't work with file names that start with #. | |
5616ee46 | 1701 | (format "_TZ_%d-%s" |
869bff31 | 1702 | (process-id (get-buffer-process "*tex-shell*")) |
07c16ec4 | 1703 | (subst-char-in-string ?. ?- (system-name)))) |
869bff31 | 1704 | |
1705 | ;; This will perhaps be useful for modifying TEXINPUTS. | |
1706 | ;; Expand each file name, separated by colons, in the string S. | |
1707 | (defun tex-expand-files (s) | |
1708 | (let (elts (start 0)) | |
1709 | (while (string-match ":" s start) | |
1710 | (setq elts (cons (substring s start (match-beginning 0)) elts)) | |
1711 | (setq start (match-end 0))) | |
1712 | (or (= start 0) | |
1713 | (setq elts (cons (substring s start) elts))) | |
c0df1972 SM |
1714 | (mapconcat (lambda (elt) |
1715 | (if (= (length elt) 0) elt (expand-file-name elt))) | |
5616ee46 | 1716 | (nreverse elts) ":"))) |
869bff31 | 1717 | |
1718 | (defun tex-shell-running () | |
713f7b15 | 1719 | (let ((proc (get-process "tex-shell"))) |
b781e739 SS |
1720 | (when proc |
1721 | (if (and (eq (process-status proc) 'run) | |
1722 | (buffer-live-p (process-buffer proc))) | |
1723 | ;; return the TeX process on success | |
1724 | proc | |
1725 | ;; get rid of the process permanently | |
1726 | ;; this should get rid of the annoying w32 problem with | |
1727 | ;; dead tex-shell buffer and live process | |
1728 | (delete-process proc))))) | |
869bff31 | 1729 | |
1730 | (defun tex-kill-job () | |
1731 | "Kill the currently running TeX job." | |
1732 | (interactive) | |
b781e739 | 1733 | ;; `quit-process' leads to core dumps of the tex process (except if |
bed1b0c8 RS |
1734 | ;; coredumpsize has limit 0kb as on many environments). One would |
1735 | ;; like to use (kill-process proc 'lambda), however that construct | |
1736 | ;; does not work on some systems and kills the shell itself. | |
b781e739 SS |
1737 | (let ((proc (get-process "tex-shell"))) |
1738 | (when proc (quit-process proc t)))) | |
869bff31 | 1739 | |
1740 | (defun tex-recenter-output-buffer (linenum) | |
1741 | "Redisplay buffer of TeX job output so that most recent output can be seen. | |
1742 | The last line of the buffer is displayed on | |
1743 | line LINE of the window, or centered if LINE is nil." | |
1744 | (interactive "P") | |
1745 | (let ((tex-shell (get-buffer "*tex-shell*")) | |
23b64225 RS |
1746 | (old-buffer (current-buffer)) |
1747 | (window)) | |
869bff31 | 1748 | (if (null tex-shell) |
1749 | (message "No TeX output buffer") | |
23b64225 RS |
1750 | (setq window (display-buffer tex-shell)) |
1751 | (save-selected-window | |
1752 | (select-window window) | |
1753 | (bury-buffer tex-shell) | |
1754 | (goto-char (point-max)) | |
1755 | (recenter (if linenum | |
1756 | (prefix-numeric-value linenum) | |
1757 | (/ (window-height) 2))))))) | |
869bff31 | 1758 | |
528415e7 | 1759 | (defun tex-print (&optional alt) |
869bff31 | 1760 | "Print the .dvi file made by \\[tex-region], \\[tex-buffer] or \\[tex-file]. |
1433a222 CZ |
1761 | Runs the shell command defined by `tex-dvi-print-command'. If prefix argument |
1762 | is provided, use the alternative command, `tex-alt-dvi-print-command'." | |
528415e7 | 1763 | (interactive "P") |
869bff31 | 1764 | (let ((print-file-name-dvi (tex-append tex-print-file ".dvi")) |
1765 | test-name) | |
1766 | (if (and (not (equal (current-buffer) tex-last-buffer-texed)) | |
45c3304d RS |
1767 | (buffer-file-name) |
1768 | ;; Check that this buffer's printed file is up to date. | |
869bff31 | 1769 | (file-newer-than-file-p |
1770 | (setq test-name (tex-append (buffer-file-name) ".dvi")) | |
45c3304d | 1771 | (buffer-file-name))) |
869bff31 | 1772 | (setq print-file-name-dvi test-name)) |
528415e7 RS |
1773 | (if (not (file-exists-p print-file-name-dvi)) |
1774 | (error "No appropriate `.dvi' file could be found") | |
460e1b7d RS |
1775 | (if (tex-shell-running) |
1776 | (tex-kill-job) | |
1777 | (tex-start-shell)) | |
528415e7 | 1778 | (tex-send-command |
bed1b0c8 RS |
1779 | (if alt tex-alt-dvi-print-command tex-dvi-print-command) |
1780 | print-file-name-dvi t)))) | |
869bff31 | 1781 | |
cf6d6e8a RS |
1782 | (defun tex-alt-print () |
1783 | "Print the .dvi file made by \\[tex-region], \\[tex-buffer] or \\[tex-file]. | |
002b0d00 | 1784 | Runs the shell command defined by `tex-alt-dvi-print-command'." |
cf6d6e8a RS |
1785 | (interactive) |
1786 | (tex-print t)) | |
1787 | ||
869bff31 | 1788 | (defun tex-view () |
1789 | "Preview the last `.dvi' file made by running TeX under Emacs. | |
1790 | This means, made using \\[tex-region], \\[tex-buffer] or \\[tex-file]. | |
2b7971c9 RS |
1791 | The variable `tex-dvi-view-command' specifies the shell command for preview. |
1792 | You must set that variable yourself before using this command, | |
1793 | because there is no standard value that would generally work." | |
869bff31 | 1794 | (interactive) |
2b7971c9 RS |
1795 | (or tex-dvi-view-command |
1796 | (error "You must set `tex-dvi-view-command'")) | |
746c30e2 | 1797 | (let ((tex-dvi-print-command (eval tex-dvi-view-command))) |
869bff31 | 1798 | (tex-print))) |
1799 | ||
1800 | (defun tex-append (file-name suffix) | |
1801 | "Append to FILENAME the suffix SUFFIX, using same algorithm TeX uses. | |
cf6d6e8a | 1802 | Pascal-based TeX scans for the first period, C TeX uses the last. |
869bff31 | 1803 | No period is retained immediately before SUFFIX, |
1804 | so normally SUFFIX starts with one." | |
1805 | (if (stringp file-name) | |
cf6d6e8a RS |
1806 | (let ((file (file-name-nondirectory file-name)) |
1807 | trial-name) | |
6da9bbd6 | 1808 | ;; Try splitting on last period. |
7d0ca249 RS |
1809 | ;; The first-period split can get fooled when two files |
1810 | ;; named a.tex and a.b.tex are both tex'd; | |
1811 | ;; the last-period split must be right if it matches at all. | |
cf6d6e8a RS |
1812 | (setq trial-name |
1813 | (concat (file-name-directory file-name) | |
1814 | (substring file 0 | |
7d0ca249 | 1815 | (string-match "\\.[^.]*$" file)) |
cf6d6e8a RS |
1816 | suffix)) |
1817 | (if (or (file-exists-p trial-name) | |
1818 | (file-exists-p (concat trial-name ".aux"))) ;for BibTeX files | |
1819 | trial-name | |
7d0ca249 | 1820 | ;; Not found, so split on first period. |
cf6d6e8a RS |
1821 | (concat (file-name-directory file-name) |
1822 | (substring file 0 | |
7d0ca249 | 1823 | (string-match "\\." file)) |
cf6d6e8a | 1824 | suffix))) |
869bff31 | 1825 | " ")) |
1826 | ||
1827 | (defun tex-show-print-queue () | |
1828 | "Show the print queue that \\[tex-print] put your job on. | |
1433a222 | 1829 | Runs the shell command defined by `tex-show-queue-command'." |
869bff31 | 1830 | (interactive) |
1831 | (if (tex-shell-running) | |
1832 | (tex-kill-job) | |
1833 | (tex-start-shell)) | |
51b2c841 RS |
1834 | (tex-send-command tex-show-queue-command) |
1835 | (tex-display-shell)) | |
869bff31 | 1836 | |
1837 | (defun tex-bibtex-file () | |
1838 | "Run BibTeX on the current buffer's file." | |
1839 | (interactive) | |
1840 | (if (tex-shell-running) | |
1841 | (tex-kill-job) | |
1842 | (tex-start-shell)) | |
e6b0b773 MR |
1843 | (let (shell-dirtrack-verbose |
1844 | (tex-out-file | |
869bff31 | 1845 | (tex-append (file-name-nondirectory (buffer-file-name)) "")) |
1846 | (file-dir (file-name-directory (buffer-file-name)))) | |
528415e7 | 1847 | (tex-send-command tex-shell-cd-command file-dir) |
51b2c841 RS |
1848 | (tex-send-command tex-bibtex-command tex-out-file)) |
1849 | (tex-display-shell)) | |
ea590e1c SM |
1850 | \f |
1851 | ;;;; | |
1852 | ;;;; LaTeX indentation | |
1853 | ;;;; | |
1854 | ||
1855 | (defvar tex-indent-allhanging t) | |
1856 | (defvar tex-indent-arg 4) | |
1857 | (defvar tex-indent-basic 2) | |
1858 | (defvar tex-indent-item tex-indent-basic) | |
1859 | (defvar tex-indent-item-re "\\\\\\(bib\\)?item\\>") | |
1860 | ||
1861 | (easy-mmode-defsyntax tex-latex-indent-syntax-table | |
dd459839 SM |
1862 | '((?$ . ".") |
1863 | (?\( . ".") | |
1864 | (?\) . ".")) | |
ea590e1c SM |
1865 | "Syntax table used while computing indentation." |
1866 | :copy tex-mode-syntax-table) | |
1867 | ||
1868 | (defun latex-indent (&optional arg) | |
1869 | (with-syntax-table tex-latex-indent-syntax-table | |
1870 | ;; TODO: Rather than ignore $, we should try to be more clever about it. | |
1871 | (let ((indent | |
1872 | (save-excursion | |
1873 | (beginning-of-line) | |
1874 | (latex-find-indent)))) | |
1875 | (if (< indent 0) (setq indent 0)) | |
1876 | (if (<= (current-column) (current-indentation)) | |
1877 | (indent-line-to indent) | |
1878 | (save-excursion (indent-line-to indent)))))) | |
1879 | ||
1880 | (defun latex-find-indent (&optional virtual) | |
1881 | "Find the proper indentation of text after point. | |
1882 | VIRTUAL if non-nil indicates that we're only trying to find the indentation | |
1883 | in order to determine the indentation of something else. | |
1884 | There might be text before point." | |
1885 | (save-excursion | |
1886 | (skip-chars-forward " \t") | |
1887 | (or | |
1888 | ;; Trust the current indentation, if such info is applicable. | |
1889 | (and virtual (>= (current-indentation) (current-column)) | |
1890 | (current-indentation)) | |
1891 | ;; Put leading close-paren where the matching open brace would be. | |
1892 | (and (eq (latex-syntax-after) ?\)) | |
1893 | (ignore-errors | |
1894 | (save-excursion | |
1895 | (latex-skip-close-parens) | |
1896 | (latex-backward-sexp-1) | |
1897 | (latex-find-indent 'virtual)))) | |
1898 | ;; Default (maybe an argument) | |
1899 | (let ((pos (point)) | |
1900 | (char (char-after)) | |
1901 | ;; Outdent \item if necessary. | |
1902 | (indent (if (looking-at tex-indent-item-re) (- tex-indent-item) 0)) | |
1903 | up-list-pos) | |
1904 | ;; Find the previous point which determines our current indentation. | |
1905 | (condition-case err | |
1906 | (progn | |
1907 | (latex-backward-sexp-1) | |
1908 | (while (> (current-column) (current-indentation)) | |
1909 | (latex-backward-sexp-1))) | |
1910 | (scan-error | |
1911 | (setq up-list-pos (nth 2 err)))) | |
8908cf40 SM |
1912 | (cond |
1913 | ((= (point-min) pos) 0) ; We're really just indenting the first line. | |
1914 | ((integerp up-list-pos) | |
1915 | ;; Have to indent relative to the open-paren. | |
1916 | (goto-char up-list-pos) | |
1917 | (if (and (not tex-indent-allhanging) | |
1918 | (> pos (progn (latex-down-list) | |
1919 | (forward-comment (point-max)) | |
1920 | (point)))) | |
1921 | ;; Align with the first element after the open-paren. | |
1922 | (current-column) | |
1923 | ;; We're the first element after a hanging brace. | |
1924 | (goto-char up-list-pos) | |
1925 | (+ indent tex-indent-basic (latex-find-indent 'virtual)))) | |
1926 | ;; We're now at the "beginning" of a line. | |
1927 | ((not (and (not virtual) (eq (char-after) ?\\))) | |
1928 | ;; Nothing particular here: just keep the same indentation. | |
1929 | (+ indent (current-column))) | |
1930 | ;; We're now looking at a macro call. | |
1931 | ((looking-at tex-indent-item-re) | |
1932 | ;; Indenting relative to an item, have to re-add the outdenting. | |
1933 | (+ indent (current-column) tex-indent-item)) | |
1934 | (t | |
1935 | (let ((col (current-column))) | |
1936 | (if (not (eq (char-syntax char) ?\()) | |
1937 | ;; If the first char was not an open-paren, there's | |
1938 | ;; a risk that this is really not an argument to the | |
1939 | ;; macro at all. | |
1940 | (+ indent col) | |
1941 | (forward-sexp 1) | |
1942 | (if (< (line-end-position) | |
1943 | (save-excursion (forward-comment (point-max)) | |
1944 | (point))) | |
1945 | ;; we're indenting the first argument. | |
1946 | (min (current-column) (+ tex-indent-arg col)) | |
1947 | (skip-syntax-forward " ") | |
1948 | (current-column)))))))))) | |
528415e7 RS |
1949 | |
1950 | (run-hooks 'tex-mode-load-hook) | |
869bff31 | 1951 | |
49116ac0 JB |
1952 | (provide 'tex-mode) |
1953 | ||
d501f516 | 1954 | ;;; tex-mode.el ends here |