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