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