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