Commit | Line | Data |
---|---|---|
d501f516 ER |
1 | ;;; tex-mode.el --- TeX, LaTeX, and SliTeX mode commands. |
2 | ||
d61140e8 RS |
3 | ;; Copyright (C) 1985, 86, 89, 92, 94, 95, 96, 1997 |
4 | ;; Free Software Foundation, Inc. | |
eea8d4ef | 5 | |
70f20973 | 6 | ;; Maintainer: FSF |
d7b4d18f | 7 | ;; Keywords: tex |
e5167999 | 8 | |
528415e7 | 9 | ;; Contributions over the years by William F. Schelter, Dick King, |
70f20973 | 10 | ;; Stephen Gildea, Michael Prange, Jacob Gore, and Edward M. Reingold. |
528415e7 | 11 | |
869bff31 | 12 | ;; This file is part of GNU Emacs. |
13 | ||
14 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
15 | ;; it under the terms of the GNU General Public License as published by | |
e5167999 | 16 | ;; the Free Software Foundation; either version 2, or (at your option) |
869bff31 | 17 | ;; any later version. |
18 | ||
19 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
20 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | ;; GNU General Public License for more details. | |
23 | ||
24 | ;; You should have received a copy of the GNU General Public License | |
6da9bbd6 RS |
25 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
26 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
27 | ;; Boston, MA 02111-1307, USA. | |
869bff31 | 28 | |
e5167999 ER |
29 | ;;; Code: |
30 | ||
a0e9c22a | 31 | (require 'shell) |
2f3067de | 32 | (require 'compile) |
528415e7 | 33 | |
d61140e8 RS |
34 | (defgroup tex-file nil |
35 | "TeX files and directories" | |
36 | :prefix "tex-" | |
37 | :group 'tex) | |
38 | ||
39 | (defgroup tex-run nil | |
40 | "Running external commands from TeX mode" | |
41 | :prefix "tex-" | |
42 | :group 'tex) | |
43 | ||
44 | (defgroup tex-view nil | |
45 | "Viewing and printing TeX files" | |
46 | :prefix "tex-" | |
47 | :group 'tex) | |
48 | ||
7e1dae73 | 49 | ;;;###autoload |
d61140e8 RS |
50 | (defcustom tex-shell-file-name nil |
51 | "*If non-nil, the shell file name to run in the subshell used to run TeX." | |
52 | :type '(choice (const :tag "None" nil) | |
53 | string) | |
54 | :group 'tex-run) | |
528415e7 | 55 | |
7e1dae73 | 56 | ;;;###autoload |
d61140e8 | 57 | (defcustom tex-directory "." |
f73741fc | 58 | "*Directory in which temporary files are written. |
81c735c0 | 59 | You can make this `/tmp' if your TEXINPUTS has no relative directories in it |
528415e7 | 60 | and you don't try to apply \\[tex-region] or \\[tex-buffer] when there are |
d61140e8 RS |
61 | `\\input' commands with relative directories." |
62 | :type 'directory | |
63 | :group 'tex-file) | |
869bff31 | 64 | |
725b7428 | 65 | ;;;###autoload |
d61140e8 | 66 | (defcustom tex-first-line-header-regexp nil |
725b7428 RS |
67 | "Regexp for matching a first line which `tex-region' should include. |
68 | If this is non-nil, it should be a regular expression string; | |
69 | if it matches the first line of the file, | |
d61140e8 RS |
70 | `tex-region' always includes the first line in the TeX run." |
71 | :type '(choice (const :tag "None" nil) | |
72 | regexp) | |
73 | :group 'tex-file) | |
725b7428 | 74 | |
f73741fc | 75 | ;;;###autoload |
d61140e8 | 76 | (defcustom tex-main-file nil |
f73741fc | 77 | "*The main TeX source file which includes this buffer's file. |
be4a962d RS |
78 | The command `tex-file' runs TeX on the file specified by `tex-main-file' |
79 | if the variable is non-nil." | |
d61140e8 RS |
80 | :type '(choice (const :tag "None" nil) |
81 | file) | |
82 | :group 'tex-file) | |
f73741fc | 83 | |
7e1dae73 | 84 | ;;;###autoload |
d61140e8 RS |
85 | (defcustom tex-offer-save t |
86 | "*If non-nil, ask about saving modified buffers before \\[tex-file] is run." | |
87 | :type 'boolean | |
88 | :group 'tex-file) | |
869bff31 | 89 | |
7e1dae73 | 90 | ;;;###autoload |
d61140e8 | 91 | (defcustom tex-run-command "tex" |
869bff31 | 92 | "*Command used to run TeX subjob. |
81c735c0 | 93 | If this string contains an asterisk (`*'), that is replaced by the file name; |
6633b891 KH |
94 | otherwise the value of tex-start-options-string and the file name are added |
95 | at the end, with blanks as separators." | |
d61140e8 RS |
96 | :type 'string |
97 | :group 'tex-run) | |
869bff31 | 98 | |
c0fdc5b7 | 99 | (defcustom tex-start-options-string "\\\\nonstopmode\\\\input" |
1f882e21 KH |
100 | "*TeX options to use when running TeX. |
101 | These precede the input file name." | |
102 | :type 'string | |
103 | :group 'tex-run | |
104 | :version "20.4") | |
105 | ||
7e1dae73 | 106 | ;;;###autoload |
d61140e8 | 107 | (defcustom latex-run-command "latex" |
869bff31 | 108 | "*Command used to run LaTeX subjob. |
81c735c0 | 109 | If this string contains an asterisk (`*'), that is replaced by the file name; |
d61140e8 RS |
110 | otherwise, the file name, preceded by blank, is added at the end." |
111 | :type 'string | |
112 | :group 'tex-run) | |
528415e7 RS |
113 | |
114 | (defvar standard-latex-block-names | |
115 | '("abstract" "array" "center" "description" | |
116 | "displaymath" "document" "enumerate" "eqnarray" | |
117 | "eqnarray*" "equation" "figure" "figure*" | |
118 | "flushleft" "flushright" "itemize" "letter" | |
119 | "list" "minipage" "picture" "quotation" | |
120 | "quote" "slide" "sloppypar" "tabbing" | |
121 | "table" "table*" "tabular" "tabular*" | |
122 | "thebibliography" "theindex*" "titlepage" "trivlist" | |
123 | "verbatim" "verbatim*" "verse") | |
124 | "Standard LaTeX block names.") | |
125 | ||
7e1dae73 | 126 | ;;;###autoload |
d61140e8 | 127 | (defcustom latex-block-names nil |
528415e7 | 128 | "*User defined LaTeX block names. |
d61140e8 RS |
129 | Combined with `standard-latex-block-names' for minibuffer completion." |
130 | :type '(repeat string) | |
131 | :group 'tex-run) | |
869bff31 | 132 | |
7e1dae73 | 133 | ;;;###autoload |
d61140e8 | 134 | (defcustom slitex-run-command "slitex" |
869bff31 | 135 | "*Command used to run SliTeX subjob. |
81c735c0 | 136 | If this string contains an asterisk (`*'), that is replaced by the file name; |
d61140e8 RS |
137 | otherwise, the file name, preceded by blank, is added at the end." |
138 | :type 'string | |
139 | :group 'tex-run) | |
869bff31 | 140 | |
7e1dae73 | 141 | ;;;###autoload |
d61140e8 | 142 | (defcustom tex-bibtex-command "bibtex" |
528415e7 | 143 | "*Command used by `tex-bibtex-file' to gather bibliographic data. |
81c735c0 | 144 | If this string contains an asterisk (`*'), that is replaced by the file name; |
d61140e8 RS |
145 | otherwise, the file name, preceded by blank, is added at the end." |
146 | :type 'string | |
147 | :group 'tex-run) | |
869bff31 | 148 | |
7e1dae73 | 149 | ;;;###autoload |
d61140e8 | 150 | (defcustom tex-dvi-print-command "lpr -d" |
528415e7 | 151 | "*Command used by \\[tex-print] to print a .dvi file. |
81c735c0 | 152 | If this string contains an asterisk (`*'), that is replaced by the file name; |
d61140e8 RS |
153 | otherwise, the file name, preceded by blank, is added at the end." |
154 | :type 'string | |
155 | :group 'tex-view) | |
528415e7 | 156 | |
7e1dae73 | 157 | ;;;###autoload |
d61140e8 | 158 | (defcustom tex-alt-dvi-print-command "lpr -d" |
528415e7 | 159 | "*Command used by \\[tex-print] with a prefix arg to print a .dvi file. |
81c735c0 RS |
160 | If this string contains an asterisk (`*'), that is replaced by the file name; |
161 | otherwise, the file name, preceded by blank, is added at the end. | |
528415e7 | 162 | |
81c735c0 RS |
163 | If two printers are not enough of a choice, you can set the variable |
164 | `tex-alt-dvi-print-command' to an expression that asks what you want; | |
528415e7 RS |
165 | for example, |
166 | ||
167 | (setq tex-alt-dvi-print-command | |
168 | '(format \"lpr -P%s\" (read-string \"Use printer: \"))) | |
169 | ||
170 | would tell \\[tex-print] with a prefix argument to ask you which printer to | |
d61140e8 RS |
171 | use." |
172 | :type '(choice (string :tag "Command") | |
173 | (sexp :tag "Expression")) | |
174 | :group 'tex-view) | |
869bff31 | 175 | |
7e1dae73 | 176 | ;;;###autoload |
d61140e8 | 177 | (defcustom tex-dvi-view-command nil |
81c735c0 RS |
178 | "*Command used by \\[tex-view] to display a `.dvi' file. |
179 | If this string contains an asterisk (`*'), that is replaced by the file name; | |
180 | otherwise, the file name, preceded by blank, is added at the end. | |
528415e7 RS |
181 | |
182 | This can be set conditionally so that the previewer used is suitable for the | |
183 | window system being used. For example, | |
184 | ||
185 | (setq tex-dvi-view-command | |
186 | (if (eq window-system 'x) \"xdvi\" \"dvi2tty * | cat -s\")) | |
187 | ||
81c735c0 | 188 | would tell \\[tex-view] to use xdvi under X windows and to use dvi2tty |
d61140e8 RS |
189 | otherwise." |
190 | :type '(choice (const nil) string) | |
191 | :group 'tex-view) | |
869bff31 | 192 | |
7e1dae73 | 193 | ;;;###autoload |
d61140e8 | 194 | (defcustom tex-show-queue-command "lpq" |
528415e7 | 195 | "*Command used by \\[tex-show-print-queue] to show the print queue. |
d61140e8 RS |
196 | Should show the queue(s) that \\[tex-print] puts jobs on." |
197 | :type 'string | |
198 | :group 'tex-view) | |
869bff31 | 199 | |
7e1dae73 | 200 | ;;;###autoload |
d61140e8 | 201 | (defcustom tex-default-mode 'plain-tex-mode |
869bff31 | 202 | "*Mode to enter for a new file that might be either TeX or LaTeX. |
203 | This variable is used when it can't be determined whether the file | |
204 | is plain TeX or LaTeX or what because the file contains no commands. | |
d61140e8 RS |
205 | Normally set to either `plain-tex-mode' or `latex-mode'." |
206 | :type 'function | |
207 | :group 'tex) | |
869bff31 | 208 | |
7e1dae73 | 209 | ;;;###autoload |
d61140e8 RS |
210 | (defcustom tex-open-quote "``" |
211 | "*String inserted by typing \\[tex-insert-quote] to open a quotation." | |
212 | :type 'string | |
213 | :group 'tex) | |
869bff31 | 214 | |
7e1dae73 | 215 | ;;;###autoload |
d61140e8 RS |
216 | (defcustom tex-close-quote "''" |
217 | "*String inserted by typing \\[tex-insert-quote] to close a quotation." | |
218 | :type 'string | |
219 | :group 'tex) | |
869bff31 | 220 | |
528415e7 RS |
221 | (defvar tex-last-temp-file nil |
222 | "Latest temporary file generated by \\[tex-region] and \\[tex-buffer]. | |
223 | Deleted when the \\[tex-region] or \\[tex-buffer] is next run, or when the | |
81c735c0 | 224 | tex shell terminates.") |
528415e7 | 225 | |
869bff31 | 226 | (defvar tex-command nil |
227 | "Command to run TeX. | |
528415e7 | 228 | The name of the file, preceded by a blank, will be added to this string.") |
869bff31 | 229 | |
230 | (defvar tex-trailer nil | |
231 | "String appended after the end of a region sent to TeX by \\[tex-region].") | |
232 | ||
233 | (defvar tex-start-of-header nil | |
898b9ac1 | 234 | "Regular expression used by \\[tex-region] to find start of file's header.") |
869bff31 | 235 | |
236 | (defvar tex-end-of-header nil | |
898b9ac1 | 237 | "Regular expression used by \\[tex-region] to find end of file's header.") |
869bff31 | 238 | |
239 | (defvar tex-shell-cd-command "cd" | |
240 | "Command to give to shell running TeX to change directory. | |
81c735c0 | 241 | The value of `tex-directory' is appended to this, separated by a space.") |
869bff31 | 242 | |
243 | (defvar tex-zap-file nil | |
244 | "Temporary file name used for text being sent as input to TeX. | |
245 | Should be a simple file name with no extension or directory specification.") | |
246 | ||
247 | (defvar tex-last-buffer-texed nil | |
248 | "Buffer which was last TeXed.") | |
249 | ||
250 | (defvar tex-print-file nil | |
251 | "File name that \\[tex-print] prints. | |
252 | Set by \\[tex-region], \\[tex-buffer], and \\[tex-file].") | |
253 | ||
254 | (defvar tex-mode-syntax-table nil | |
255 | "Syntax table used while in TeX mode.") | |
256 | ||
a53c647b RS |
257 | (defcustom latex-imenu-indent-string "." |
258 | "*String to add repeated in front of nested sectional units for Imenu. | |
259 | An alternative value is \" . \", if you use a font with a narrow period." | |
260 | :type 'string | |
261 | :group 'tex) | |
262 | ||
a5e2ae01 RS |
263 | (defun latex-imenu-create-index () |
264 | "Generates an alist for imenu from a LaTeX buffer." | |
a53c647b RS |
265 | (let (i0 menu case-fold-search) |
266 | (save-excursion | |
267 | ;; Find the top-most level in this file but don't allow it to be | |
268 | ;; any deeper than "section" (which is top-level in an article). | |
269 | (goto-char (point-min)) | |
270 | (if (search-forward-regexp "\\\\part\\*?[ \t]*{" nil t) | |
271 | (setq i0 0) | |
272 | (if (search-forward-regexp "\\\\chapter\\*?[ \t]*{" nil t) | |
273 | (setq i0 1) | |
274 | (setq i0 2))) | |
275 | ||
276 | ;; Look for chapters and sections. | |
277 | (goto-char (point-min)) | |
278 | (while (search-forward-regexp | |
279 | "\\\\\\(part\\|chapter\\|section\\|subsection\\|\ | |
280 | subsubsection\\|paragraph\\|subparagraph\\)\\*?[ \t]*{" nil t) | |
281 | (let ((start (match-beginning 0)) | |
282 | (here (point)) | |
283 | (i (cdr (assoc (buffer-substring-no-properties | |
284 | (match-beginning 1) | |
285 | (match-end 1)) | |
286 | '(("part" . 0) ("chapter" . 1) | |
287 | ("section" . 2) ("subsection" . 3) | |
288 | ("subsubsection" . 4) | |
289 | ("paragraph" . 5) ("subparagraph" . 6)))))) | |
290 | (backward-char 1) | |
291 | (condition-case err | |
292 | (progn | |
293 | ;; Using sexps allows some use of matching {...} inside | |
294 | ;; titles. | |
295 | (forward-sexp 1) | |
296 | (setq menu | |
297 | (cons (cons (concat (apply 'concat | |
298 | (make-list | |
299 | (max 0 (- i i0)) | |
300 | latex-imenu-indent-string)) | |
301 | (buffer-substring-no-properties | |
302 | here (1- (point)))) | |
303 | start) | |
304 | menu)) | |
305 | ) | |
306 | (error nil)))) | |
307 | ||
308 | ;; Look for included material. | |
309 | (goto-char (point-min)) | |
310 | (while (search-forward-regexp | |
311 | "\\\\\\(include\\|input\\|verbatiminput\\|bibliography\\)\ | |
312 | [ \t]*{\\([^}\n]+\\)}" | |
313 | nil t) | |
314 | (setq menu | |
315 | (cons (cons (concat "<<" (buffer-substring-no-properties | |
316 | (match-beginning 2) | |
317 | (match-end 2)) | |
318 | (if (= (char-after (match-beginning 1)) ?b) | |
319 | ".bbl" | |
320 | ".tex")) | |
321 | (match-beginning 0)) | |
322 | menu))) | |
323 | ||
324 | ;; Look for \frontmatter, \mainmatter, \backmatter, and \appendix. | |
325 | (goto-char (point-min)) | |
326 | (while (search-forward-regexp | |
327 | "\\\\\\(frontmatter\\|mainmatter\\|backmatter\\|appendix\\)\\b" | |
328 | nil t) | |
329 | (setq menu | |
330 | (cons (cons "--" (match-beginning 0)) | |
331 | menu))) | |
332 | ||
333 | ;; Sort in increasing buffer position order. | |
334 | (sort menu (function (lambda (a b) (< (cdr a) (cdr b)))))))) | |
0fb4f245 | 335 | |
869bff31 | 336 | (defun tex-define-common-keys (keymap) |
81c735c0 | 337 | "Define the keys that we want defined both in TeX mode and in the TeX shell." |
869bff31 | 338 | (define-key keymap "\C-c\C-k" 'tex-kill-job) |
339 | (define-key keymap "\C-c\C-l" 'tex-recenter-output-buffer) | |
340 | (define-key keymap "\C-c\C-q" 'tex-show-print-queue) | |
341 | (define-key keymap "\C-c\C-p" 'tex-print) | |
342 | (define-key keymap "\C-c\C-v" 'tex-view) | |
62a24cb5 RS |
343 | |
344 | (define-key keymap [menu-bar tex] (cons "TeX" (make-sparse-keymap "TeX"))) | |
345 | ||
346 | (define-key keymap [menu-bar tex tex-kill-job] '("Tex Kill" . tex-kill-job)) | |
cf6d6e8a RS |
347 | (define-key keymap [menu-bar tex tex-recenter-output-buffer] |
348 | '("Tex Recenter" . tex-recenter-output-buffer)) | |
62a24cb5 RS |
349 | (define-key keymap [menu-bar tex tex-show-print-queue] |
350 | '("Show Print Queue" . tex-show-print-queue)) | |
cf6d6e8a RS |
351 | (define-key keymap [menu-bar tex tex-alt-print] |
352 | '("Tex Print (alt printer)" . tex-alt-print)) | |
62a24cb5 | 353 | (define-key keymap [menu-bar tex tex-print] '("Tex Print" . tex-print)) |
cf6d6e8a | 354 | (define-key keymap [menu-bar tex tex-view] '("Tex View" . tex-view)) |
869bff31 | 355 | ) |
356 | ||
357 | (defvar tex-mode-map nil "Keymap for TeX mode.") | |
358 | ||
359 | (if tex-mode-map | |
360 | nil | |
361 | (setq tex-mode-map (make-sparse-keymap)) | |
362 | (tex-define-common-keys tex-mode-map) | |
363 | (define-key tex-mode-map "\"" 'tex-insert-quote) | |
364 | (define-key tex-mode-map "\n" 'tex-terminate-paragraph) | |
365 | (define-key tex-mode-map "\C-c}" 'up-list) | |
366 | (define-key tex-mode-map "\C-c{" 'tex-insert-braces) | |
367 | (define-key tex-mode-map "\C-c\C-r" 'tex-region) | |
368 | (define-key tex-mode-map "\C-c\C-b" 'tex-buffer) | |
369 | (define-key tex-mode-map "\C-c\C-f" 'tex-file) | |
370 | (define-key tex-mode-map "\C-c\C-i" 'tex-bibtex-file) | |
371 | (define-key tex-mode-map "\C-c\C-o" 'tex-latex-block) | |
cf6d6e8a | 372 | (define-key tex-mode-map "\C-c\C-e" 'tex-close-latex-block) |
6a900cf1 | 373 | (define-key tex-mode-map "\C-c\C-u" 'tex-goto-last-unclosed-latex-block) |
002b0d00 RS |
374 | (define-key tex-mode-map [menu-bar tex tex-bibtex-file] |
375 | '("BibTeX File" . tex-bibtex-file)) | |
cf6d6e8a RS |
376 | (define-key tex-mode-map [menu-bar tex tex-validate-region] |
377 | '("Validate Region" . tex-validate-region)) | |
378 | (define-key tex-mode-map [menu-bar tex validate-tex-buffer] | |
379 | '("Validate Buffer" . validate-tex-buffer)) | |
380 | (define-key tex-mode-map [menu-bar tex tex-region] | |
002b0d00 | 381 | '("TeX Region" . tex-region)) |
cf6d6e8a | 382 | (define-key tex-mode-map [menu-bar tex tex-buffer] |
002b0d00 RS |
383 | '("TeX Buffer" . tex-buffer)) |
384 | (define-key tex-mode-map [menu-bar tex tex-file] '("TeX File" . tex-file))) | |
cf6d6e8a RS |
385 | |
386 | (put 'tex-region 'menu-enable 'mark-active) | |
387 | (put 'tex-validate-region 'menu-enable 'mark-active) | |
388 | (put 'tex-print 'menu-enable '(stringp tex-print-file)) | |
389 | (put 'tex-alt-print 'menu-enable '(stringp tex-print-file)) | |
390 | (put 'tex-view 'menu-enable '(stringp tex-print-file)) | |
391 | (put 'tex-recenter-output-buffer 'menu-enable '(get-buffer "*tex-shell*")) | |
392 | (put 'tex-kill-job 'menu-enable '(tex-shell-running)) | |
393 | ||
869bff31 | 394 | |
395 | (defvar tex-shell-map nil | |
81c735c0 | 396 | "Keymap for the TeX shell. |
dca5ea48 | 397 | Inherits `shell-mode-map' with a few additions.") |
869bff31 | 398 | |
6da9bbd6 RS |
399 | (defvar tex-face-alist |
400 | '((bold . "{\\bf ") | |
401 | (italic . "{\\it ") | |
402 | (bold-italic . "{\\bi ") ; hypothetical | |
403 | (underline . "\\underline{") | |
404 | (default . "{\\rm ")) | |
405 | "Alist of face and TeX font name for facemenu.") | |
406 | ||
407 | (defvar tex-latex-face-alist | |
408 | `((italic . "{\\em ") | |
409 | ,@tex-face-alist) | |
410 | "Alist of face and LaTeX font name for facemenu.") | |
411 | ||
412 | ||
0d548e5d | 413 | (defvar compare-windows-whitespace) ; Pacify the byte-compiler |
8241d7b9 | 414 | |
869bff31 | 415 | ;;; This would be a lot simpler if we just used a regexp search, |
416 | ;;; but then it would be too slow. | |
7229064d | 417 | ;;;###autoload |
869bff31 | 418 | (defun tex-mode () |
419 | "Major mode for editing files of input for TeX, LaTeX, or SliTeX. | |
420 | Tries to determine (by looking at the beginning of the file) whether | |
81c735c0 RS |
421 | this file is for plain TeX, LaTeX, or SliTeX and calls `plain-tex-mode', |
422 | `latex-mode', or `slitex-mode', respectively. If it cannot be determined, | |
423 | such as if there are no commands in the file, the value of `tex-default-mode' | |
424 | says which mode to use." | |
869bff31 | 425 | (interactive) |
426 | (let (mode slash comment) | |
427 | (save-excursion | |
428 | (goto-char (point-min)) | |
429 | (while (and (setq slash (search-forward "\\" nil t)) | |
430 | (setq comment (let ((search-end (point))) | |
431 | (save-excursion | |
432 | (beginning-of-line) | |
433 | (search-forward "%" search-end t)))))) | |
434 | (if (and slash (not comment)) | |
c0bcb239 | 435 | (setq mode (if (looking-at "documentstyle\\|documentclass\\|begin\\b\\|NeedsTeXFormat{LaTeX") |
0edb9815 RS |
436 | (if (looking-at |
437 | "document\\(style\\|class\\)\\(\\[.*\\]\\)?{slides}") | |
869bff31 | 438 | 'slitex-mode |
439 | 'latex-mode) | |
440 | 'plain-tex-mode)))) | |
441 | (if mode (funcall mode) | |
442 | (funcall tex-default-mode)))) | |
e4c8c838 | 443 | |
6503cec3 | 444 | ;;;###autoload |
31e1d920 | 445 | (defalias 'TeX-mode 'tex-mode) |
6503cec3 | 446 | ;;;###autoload |
31e1d920 | 447 | (defalias 'LaTeX-mode 'latex-mode) |
869bff31 | 448 | |
7229064d | 449 | ;;;###autoload |
869bff31 | 450 | (defun plain-tex-mode () |
451 | "Major mode for editing files of input for plain TeX. | |
452 | Makes $ and } display the characters they match. | |
453 | Makes \" insert `` when it seems to be the beginning of a quotation, | |
454 | and '' when it appears to be the end; it inserts \" only after a \\. | |
455 | ||
456 | Use \\[tex-region] to run TeX on the current region, plus a \"header\" | |
457 | copied from the top of the file (containing macro definitions, etc.), | |
458 | running TeX under a special subshell. \\[tex-buffer] does the whole buffer. | |
459 | \\[tex-file] saves the buffer and then processes the file. | |
460 | \\[tex-print] prints the .dvi file made by any of these. | |
461 | \\[tex-view] previews the .dvi file made by any of these. | |
462 | \\[tex-bibtex-file] runs bibtex on the file of the current buffer. | |
463 | ||
464 | Use \\[validate-tex-buffer] to check buffer for paragraphs containing | |
465 | mismatched $'s or braces. | |
466 | ||
467 | Special commands: | |
468 | \\{tex-mode-map} | |
469 | ||
470 | Mode variables: | |
471 | tex-run-command | |
472 | Command string used by \\[tex-region] or \\[tex-buffer]. | |
473 | tex-directory | |
474 | Directory in which to create temporary files for TeX jobs | |
475 | run by \\[tex-region] or \\[tex-buffer]. | |
476 | tex-dvi-print-command | |
477 | Command string used by \\[tex-print] to print a .dvi file. | |
528415e7 RS |
478 | tex-alt-dvi-print-command |
479 | Alternative command string used by \\[tex-print] (when given a prefix | |
480 | argument) to print a .dvi file. | |
869bff31 | 481 | tex-dvi-view-command |
482 | Command string used by \\[tex-view] to preview a .dvi file. | |
483 | tex-show-queue-command | |
484 | Command string used by \\[tex-show-print-queue] to show the print | |
485 | queue that \\[tex-print] put your job on. | |
486 | ||
81c735c0 RS |
487 | Entering Plain-tex mode runs the hook `text-mode-hook', then the hook |
488 | `tex-mode-hook', and finally the hook `plain-tex-mode-hook'. When the | |
489 | special subshell is initiated, the hook `tex-shell-hook' is run." | |
8241d7b9 | 490 | |
869bff31 | 491 | (interactive) |
492 | (tex-common-initialization) | |
493 | (setq mode-name "TeX") | |
494 | (setq major-mode 'plain-tex-mode) | |
495 | (setq tex-command tex-run-command) | |
898b9ac1 RS |
496 | (setq tex-start-of-header "%\\*\\*start of header") |
497 | (setq tex-end-of-header "%\\*\\*end of header") | |
869bff31 | 498 | (setq tex-trailer "\\bye\n") |
499 | (run-hooks 'text-mode-hook 'tex-mode-hook 'plain-tex-mode-hook)) | |
6503cec3 | 500 | ;;;###autoload |
31e1d920 | 501 | (defalias 'plain-TeX-mode 'plain-tex-mode) |
869bff31 | 502 | |
7229064d | 503 | ;;;###autoload |
869bff31 | 504 | (defun latex-mode () |
505 | "Major mode for editing files of input for LaTeX. | |
506 | Makes $ and } display the characters they match. | |
507 | Makes \" insert `` when it seems to be the beginning of a quotation, | |
508 | and '' when it appears to be the end; it inserts \" only after a \\. | |
509 | ||
510 | Use \\[tex-region] to run LaTeX on the current region, plus the preamble | |
511 | copied from the top of the file (containing \\documentstyle, etc.), | |
512 | running LaTeX under a special subshell. \\[tex-buffer] does the whole buffer. | |
513 | \\[tex-file] saves the buffer and then processes the file. | |
514 | \\[tex-print] prints the .dvi file made by any of these. | |
515 | \\[tex-view] previews the .dvi file made by any of these. | |
516 | \\[tex-bibtex-file] runs bibtex on the file of the current buffer. | |
517 | ||
518 | Use \\[validate-tex-buffer] to check buffer for paragraphs containing | |
519 | mismatched $'s or braces. | |
520 | ||
521 | Special commands: | |
522 | \\{tex-mode-map} | |
523 | ||
524 | Mode variables: | |
525 | latex-run-command | |
526 | Command string used by \\[tex-region] or \\[tex-buffer]. | |
527 | tex-directory | |
528 | Directory in which to create temporary files for LaTeX jobs | |
529 | run by \\[tex-region] or \\[tex-buffer]. | |
530 | tex-dvi-print-command | |
531 | Command string used by \\[tex-print] to print a .dvi file. | |
528415e7 RS |
532 | tex-alt-dvi-print-command |
533 | Alternative command string used by \\[tex-print] (when given a prefix | |
534 | argument) to print a .dvi file. | |
869bff31 | 535 | tex-dvi-view-command |
536 | Command string used by \\[tex-view] to preview a .dvi file. | |
537 | tex-show-queue-command | |
538 | Command string used by \\[tex-show-print-queue] to show the print | |
539 | queue that \\[tex-print] put your job on. | |
540 | ||
81c735c0 RS |
541 | Entering Latex mode runs the hook `text-mode-hook', then |
542 | `tex-mode-hook', and finally `latex-mode-hook'. When the special | |
543 | subshell is initiated, `tex-shell-hook' is run." | |
869bff31 | 544 | (interactive) |
545 | (tex-common-initialization) | |
546 | (setq mode-name "LaTeX") | |
547 | (setq major-mode 'latex-mode) | |
548 | (setq tex-command latex-run-command) | |
898b9ac1 RS |
549 | (setq tex-start-of-header "\\\\documentstyle\\|\\\\documentclass") |
550 | (setq tex-end-of-header "\\\\begin{document}") | |
869bff31 | 551 | (setq tex-trailer "\\end{document}\n") |
ab2c9f54 RS |
552 | ;; A line containing just $$ is treated as a paragraph separator. |
553 | ;; A line starting with $$ starts a paragraph, | |
554 | ;; but does not separate paragraphs if it has more stuff on it. | |
02202839 BG |
555 | (setq paragraph-start "[ \t]*$\\|[\f%]\\|[ \t]*\\$\\$\\|\ |
556 | \\\\begin\\>\\|\\\\label\\>\\|\\\\end\\>\\|\\\\\\[\\|\\\\\\]\\|\ | |
557 | \\\\chapter\\>\\|\\\\section\\>\\|\ | |
558 | \\\\subsection\\>\\|\\\\subsubsection\\>\\|\ | |
559 | \\\\paragraph\\>\\|\\\\subparagraph\\>\\|\ | |
560 | \\\\item\\>\\|\\\\bibitem\\>\\|\\\\newline\\>\\|\\\\noindent\\>\\|\ | |
561 | \\\\[a-z]*space\\>\\|\\\\[a-z]*skip\\>\\|\ | |
562 | \\\\newpage\\>\\|\\\\[a-z]*page\\|\\\\footnote\\>\\|\ | |
563 | \\\\marginpar\\>\\|\\\\parbox\\>\\|\\\\caption\\>") | |
564 | (setq paragraph-separate "[ \t]*$\\|[\f%]\\|[ \t]*\\$\\$[ \t]*$\\|\ | |
565 | \\\\begin\\>\\|\\\\label\\>\\|\\\\end\\>\\|\\\\\\[\\|\\\\\\]\\|\ | |
566 | \\\\chapter\\>\\|\\\\section\\>\\|\ | |
567 | \\\\subsection\\>\\|\\\\subsubsection\\>\\|\ | |
568 | \\\\paragraph\\>\\|\\\\subparagraph\\>\\|\ | |
569 | \\(\\\\item\\|\\\\bibitem\\|\\\\newline\\|\\\\noindent\\|\ | |
570 | \\\\[a-z]*space\\|\\\\[a-z]*skip\\|\ | |
571 | \\\\newpage\\|\\\\[a-z]*page[a-z]*\\|\\\\footnote\\|\ | |
572 | \\\\marginpar\\|\\\\parbox\\|\\\\caption\\)[ \t]*\\($\\|%\\)") | |
a5e2ae01 RS |
573 | (make-local-variable 'imenu-create-index-function) |
574 | (setq imenu-create-index-function 'latex-imenu-create-index) | |
6da9bbd6 RS |
575 | (make-local-variable 'tex-face-alist) |
576 | (setq tex-face-alist tex-latex-face-alist) | |
9bd4f69c RS |
577 | (make-local-variable 'fill-nobreak-predicate) |
578 | (setq fill-nobreak-predicate 'latex-fill-nobreak-predicate) | |
869bff31 | 579 | (run-hooks 'text-mode-hook 'tex-mode-hook 'latex-mode-hook)) |
580 | ||
bd2f2323 | 581 | ;;;###autoload |
869bff31 | 582 | (defun slitex-mode () |
583 | "Major mode for editing files of input for SliTeX. | |
584 | Makes $ and } display the characters they match. | |
585 | Makes \" insert `` when it seems to be the beginning of a quotation, | |
586 | and '' when it appears to be the end; it inserts \" only after a \\. | |
587 | ||
588 | Use \\[tex-region] to run SliTeX on the current region, plus the preamble | |
589 | copied from the top of the file (containing \\documentstyle, etc.), | |
590 | running SliTeX under a special subshell. \\[tex-buffer] does the whole buffer. | |
591 | \\[tex-file] saves the buffer and then processes the file. | |
592 | \\[tex-print] prints the .dvi file made by any of these. | |
593 | \\[tex-view] previews the .dvi file made by any of these. | |
594 | \\[tex-bibtex-file] runs bibtex on the file of the current buffer. | |
595 | ||
596 | Use \\[validate-tex-buffer] to check buffer for paragraphs containing | |
597 | mismatched $'s or braces. | |
598 | ||
599 | Special commands: | |
600 | \\{tex-mode-map} | |
601 | ||
602 | Mode variables: | |
603 | slitex-run-command | |
604 | Command string used by \\[tex-region] or \\[tex-buffer]. | |
605 | tex-directory | |
606 | Directory in which to create temporary files for SliTeX jobs | |
607 | run by \\[tex-region] or \\[tex-buffer]. | |
608 | tex-dvi-print-command | |
609 | Command string used by \\[tex-print] to print a .dvi file. | |
528415e7 RS |
610 | tex-alt-dvi-print-command |
611 | Alternative command string used by \\[tex-print] (when given a prefix | |
612 | argument) to print a .dvi file. | |
869bff31 | 613 | tex-dvi-view-command |
614 | Command string used by \\[tex-view] to preview a .dvi file. | |
615 | tex-show-queue-command | |
616 | Command string used by \\[tex-show-print-queue] to show the print | |
617 | queue that \\[tex-print] put your job on. | |
618 | ||
81c735c0 RS |
619 | Entering SliTeX mode runs the hook `text-mode-hook', then the hook |
620 | `tex-mode-hook', then the hook `latex-mode-hook', and finally the hook | |
621 | `slitex-mode-hook'. When the special subshell is initiated, the hook | |
622 | `tex-shell-hook' is run." | |
869bff31 | 623 | (interactive) |
624 | (tex-common-initialization) | |
625 | (setq mode-name "SliTeX") | |
626 | (setq major-mode 'slitex-mode) | |
627 | (setq tex-command slitex-run-command) | |
6da9bbd6 | 628 | (setq tex-start-of-header "\\\\documentstyle{slides}\\|\\\\documentclass{slides}") |
898b9ac1 | 629 | (setq tex-end-of-header "\\\\begin{document}") |
869bff31 | 630 | (setq tex-trailer "\\end{document}\n") |
ab2c9f54 RS |
631 | ;; A line containing just $$ is treated as a paragraph separator. |
632 | ;; A line starting with $$ starts a paragraph, | |
633 | ;; but does not separate paragraphs if it has more stuff on it. | |
02202839 BG |
634 | (setq paragraph-start "[ \t]*$\\|[\f%]\\|[ \t]*\\$\\$\\|\ |
635 | \\\\begin\\>\\|\\\\label\\>\\|\\\\end\\>\\|\\\\\\[\\|\\\\\\]\\|\ | |
636 | \\\\chapter\\>\\|\\\\section\\>\\|\ | |
637 | \\\\subsection\\>\\|\\\\subsubsection\\>\\|\ | |
638 | \\\\paragraph\\>\\|\\\\subparagraph\\>\\|\ | |
639 | \\\\item\\>\\|\\\\bibitem\\>\\|\\\\newline\\>\\|\\\\noindent\\>\\|\ | |
640 | \\\\[a-z]*space\\>\\|\\\\[a-z]*skip\\>\\|\ | |
641 | \\\\newpage\\>\\|\\\\[a-z]*page\\|\\\\footnote\\>\\|\ | |
642 | \\\\marginpar\\>\\|\\\\parbox\\>\\|\\\\caption\\>") | |
643 | (setq paragraph-separate "[ \t]*$\\|[\f%]\\|[ \t]*\\$\\$[ \t]*$\\|\ | |
644 | \\\\begin\\>\\|\\\\label\\>\\|\\\\end\\>\\|\\\\\\[\\|\\\\\\]\\|\ | |
645 | \\\\chapter\\>\\|\\\\section\\>\\|\ | |
646 | \\\\subsection\\>\\|\\\\subsubsection\\>\\|\ | |
647 | \\\\paragraph\\>\\|\\\\subparagraph\\>\\|\ | |
648 | \\\\item[ \t]*$\\|\\\\bibitem[ \t]*$\\|\\\\newline[ \t]*$\\|\\\\noindent[ \t]*$\\|\ | |
649 | \\\\[a-z]*space[ \t]*$\\|\\\\[a-z]*skip[ \t]*$\\|\ | |
650 | \\\\newpage[ \t]*$\\|\\\\[a-z]*page[a-z]*[ \t]*$\\|\\\\footnote[ \t]*$\\|\ | |
651 | \\\\marginpar[ \t]*$\\|\\\\parbox[ \t]*$\\|\\\\caption[ \t]*$") | |
9bd4f69c RS |
652 | (make-local-variable 'imenu-create-index-function) |
653 | (setq imenu-create-index-function 'latex-imenu-create-index) | |
654 | (make-local-variable 'tex-face-alist) | |
655 | (setq tex-face-alist tex-latex-face-alist) | |
656 | (make-local-variable 'fill-nobreak-predicate) | |
657 | (setq fill-nobreak-predicate 'latex-fill-nobreak-predicate) | |
869bff31 | 658 | (run-hooks |
659 | 'text-mode-hook 'tex-mode-hook 'latex-mode-hook 'slitex-mode-hook)) | |
660 | ||
661 | (defun tex-common-initialization () | |
662 | (kill-all-local-variables) | |
663 | (use-local-map tex-mode-map) | |
664 | (setq local-abbrev-table text-mode-abbrev-table) | |
665 | (if (null tex-mode-syntax-table) | |
666 | (let ((char 0)) | |
667 | (setq tex-mode-syntax-table (make-syntax-table)) | |
668 | (set-syntax-table tex-mode-syntax-table) | |
669 | (while (< char ? ) | |
670 | (modify-syntax-entry char ".") | |
671 | (setq char (1+ char))) | |
672 | (modify-syntax-entry ?\C-@ "w") | |
673 | (modify-syntax-entry ?\t " ") | |
674 | (modify-syntax-entry ?\n ">") | |
675 | (modify-syntax-entry ?\f ">") | |
676 | (modify-syntax-entry ?$ "$$") | |
677 | (modify-syntax-entry ?% "<") | |
678 | (modify-syntax-entry ?\\ "/") | |
679 | (modify-syntax-entry ?\" ".") | |
680 | (modify-syntax-entry ?& ".") | |
681 | (modify-syntax-entry ?_ ".") | |
682 | (modify-syntax-entry ?@ "_") | |
683 | (modify-syntax-entry ?~ " ") | |
684 | (modify-syntax-entry ?' "w")) | |
685 | (set-syntax-table tex-mode-syntax-table)) | |
822eddf4 RS |
686 | ;; Regexp isearch should accept newline and formfeed as whitespace. |
687 | (make-local-variable 'search-whitespace-regexp) | |
688 | (setq search-whitespace-regexp "[ \t\r\n\f]+") | |
869bff31 | 689 | (make-local-variable 'paragraph-start) |
64db2461 | 690 | ;; A line containing just $$ is treated as a paragraph separator. |
02202839 | 691 | (setq paragraph-start "[ \t]*$\\|[\f\\\\%]\\|[ \t]*\\$\\$") |
869bff31 | 692 | (make-local-variable 'paragraph-separate) |
ab2c9f54 RS |
693 | ;; A line starting with $$ starts a paragraph, |
694 | ;; but does not separate paragraphs if it has more stuff on it. | |
02202839 | 695 | (setq paragraph-separate "[ \t]*$\\|[\f\\\\%]\\|[ \t]*\\$\\$[ \t]*$") |
869bff31 | 696 | (make-local-variable 'comment-start) |
697 | (setq comment-start "%") | |
698 | (make-local-variable 'comment-start-skip) | |
699 | (setq comment-start-skip "\\(\\(^\\|[^\\]\\)\\(\\\\\\\\\\)*\\)\\(%+ *\\)") | |
e41b2db1 ER |
700 | (make-local-variable 'comment-indent-function) |
701 | (setq comment-indent-function 'tex-comment-indent) | |
2e0b53ad RS |
702 | (make-local-variable 'parse-sexp-ignore-comments) |
703 | (setq parse-sexp-ignore-comments t) | |
869bff31 | 704 | (make-local-variable 'compare-windows-whitespace) |
705 | (setq compare-windows-whitespace 'tex-categorize-whitespace) | |
cf149bdf RS |
706 | (make-local-variable 'skeleton-further-elements) |
707 | (setq skeleton-further-elements | |
708 | '((indent-line-function 'indent-relative-maybe))) | |
6da9bbd6 RS |
709 | (make-local-variable 'facemenu-add-face-function) |
710 | (make-local-variable 'facemenu-end-add-face) | |
711 | (make-local-variable 'facemenu-remove-face-function) | |
712 | (setq facemenu-add-face-function | |
713 | (lambda (face end) | |
714 | (let ((face-text (cdr (assq face tex-face-alist)))) | |
715 | (if face-text | |
716 | face-text | |
717 | (error "Face %s not configured for %s mode" face mode-name)))) | |
718 | facemenu-end-add-face "}" | |
719 | facemenu-remove-face-function t) | |
869bff31 | 720 | (make-local-variable 'tex-command) |
721 | (make-local-variable 'tex-start-of-header) | |
722 | (make-local-variable 'tex-end-of-header) | |
723 | (make-local-variable 'tex-trailer)) | |
724 | ||
725 | (defun tex-comment-indent () | |
726 | (if (looking-at "%%%") | |
727 | (current-column) | |
728 | (skip-chars-backward " \t") | |
729 | (max (if (bolp) 0 (1+ (current-column))) | |
730 | comment-column))) | |
731 | ||
732 | (defun tex-categorize-whitespace (backward-limit) | |
733 | ;; compare-windows-whitespace is set to this. | |
734 | ;; This is basically a finite-state machine. | |
735 | ;; Returns a symbol telling how TeX would treat | |
736 | ;; the whitespace we are looking at: null, space, or par. | |
737 | (let ((category 'null) | |
738 | (not-finished t)) | |
739 | (skip-chars-backward " \t\n\f" backward-limit) | |
740 | (while not-finished | |
741 | (cond ((looking-at "[ \t]+") | |
742 | (goto-char (match-end 0)) | |
2be5fefb | 743 | (if (eq category 'null) |
869bff31 | 744 | (setq category 'space))) |
745 | ((looking-at "\n") | |
2be5fefb | 746 | (cond ((eq category 'newline) |
869bff31 | 747 | (setq category 'par) |
748 | (setq not-finished nil)) | |
749 | (t | |
750 | (setq category 'newline) ;a strictly internal state | |
751 | (goto-char (match-end 0))))) | |
752 | ((looking-at "\f+") | |
753 | (setq category 'par) | |
754 | (setq not-finished nil)) | |
755 | (t | |
756 | (setq not-finished nil)))) | |
757 | (skip-chars-forward " \t\n\f") | |
2be5fefb | 758 | (if (eq category 'newline) |
869bff31 | 759 | 'space ;TeX doesn't distinguish |
760 | category))) | |
761 | ||
762 | (defun tex-insert-quote (arg) | |
763 | "Insert the appropriate quote marks for TeX. | |
08348502 RS |
764 | Inserts the value of `tex-open-quote' (normally ``) or `tex-close-quote' |
765 | \(normally '') depending on the context. With prefix argument, always | |
869bff31 | 766 | inserts \" characters." |
528415e7 | 767 | (interactive "*P") |
869bff31 | 768 | (if arg |
769 | (self-insert-command (prefix-numeric-value arg)) | |
770 | (insert | |
771 | (cond ((or (bobp) | |
772 | (save-excursion | |
773 | (forward-char -1) | |
774 | (looking-at "\\s(\\|\\s \\|\\s>"))) | |
775 | tex-open-quote) | |
776 | ((= (preceding-char) ?\\) | |
777 | ?\") | |
778 | (t | |
779 | tex-close-quote))))) | |
780 | ||
781 | (defun validate-tex-buffer () | |
8241d7b9 | 782 | "Check current buffer for paragraphs containing mismatched $s. |
bbd93e41 RS |
783 | Their positions are recorded in the buffer `*Occur*'. |
784 | To find a particular invalidity from `*Occur*', | |
785 | switch to to that buffer and type C-c C-c on the line | |
786 | for the invalidity you want to see." | |
869bff31 | 787 | (interactive) |
bbd93e41 RS |
788 | (let ((buffer (current-buffer)) |
789 | (prevpos (point-min)) | |
790 | (linenum nil)) | |
791 | (with-output-to-temp-buffer "*Occur*" | |
792 | (princ "Mismatches:\n") | |
793 | (save-excursion | |
794 | (set-buffer standard-output) | |
795 | (occur-mode) | |
796 | (setq occur-buffer buffer) | |
797 | (setq occur-nlines 0) | |
798 | (setq occur-pos-list nil)) | |
799 | (save-excursion | |
800 | (goto-char (point-max)) | |
869bff31 | 801 | (while (and (not (input-pending-p)) (not (bobp))) |
710a0f53 KH |
802 | (let ((end (point)) |
803 | prev-end) | |
bbd93e41 | 804 | ;; Scan the previous paragraph for invalidities. |
710a0f53 KH |
805 | (if (search-backward "\n\n" nil t) |
806 | (progn | |
807 | (setq prev-end (point)) | |
808 | (forward-char 2)) | |
809 | (goto-char (setq prev-end (point-min)))) | |
869bff31 | 810 | (or (tex-validate-region (point) end) |
710a0f53 KH |
811 | (let* ((oend end) |
812 | (end (save-excursion (forward-line 1) (point))) | |
bbd93e41 RS |
813 | start tem) |
814 | (beginning-of-line) | |
815 | (setq start (point)) | |
816 | ;; Keep track of line number as we scan, | |
817 | ;; in a cumulative fashion. | |
818 | (if linenum | |
819 | (setq linenum (- linenum (count-lines prevpos (point)))) | |
820 | (setq linenum (1+ (count-lines 1 start)))) | |
821 | (setq prevpos (point)) | |
822 | ;; Mention this mismatch in *Occur*. | |
823 | ;; Since we scan from end of buffer to beginning, | |
824 | ;; add each mismatch at the beginning of *Occur* | |
825 | ;; and at the beginning of occur-pos-list. | |
826 | (save-excursion | |
827 | (setq tem (point-marker)) | |
828 | (set-buffer standard-output) | |
829 | (goto-char (point-min)) | |
830 | ;; Skip "Mismatches:" header line. | |
831 | (forward-line 1) | |
832 | (setq occur-pos-list (cons tem occur-pos-list)) | |
833 | (insert-buffer-substring buffer start end) | |
834 | (forward-char (- start end)) | |
710a0f53 KH |
835 | (insert (format "%3d: " linenum))))) |
836 | (goto-char prev-end)))) | |
bbd93e41 RS |
837 | (save-excursion |
838 | (set-buffer standard-output) | |
839 | (if (null occur-pos-list) | |
840 | (insert "None!\n")) | |
841 | (if (interactive-p) | |
842 | (message "%d mismatches found" (length occur-pos-list))))))) | |
869bff31 | 843 | |
844 | (defun tex-validate-region (start end) | |
845 | "Check for mismatched braces or $'s in region. | |
846 | Returns t if no mismatches. Returns nil and moves point to suspect | |
847 | area if a mismatch is found." | |
848 | (interactive "r") | |
849 | (let ((failure-point nil) (max-possible-sexps (- end start))) | |
850 | (save-excursion | |
851 | (condition-case () | |
852 | (save-restriction | |
853 | (narrow-to-region start end) | |
2ed2806c | 854 | ;; First check that the open and close parens balance in numbers. |
869bff31 | 855 | (goto-char start) |
856 | (while (< 0 (setq max-possible-sexps (1- max-possible-sexps))) | |
2ed2806c KH |
857 | (forward-sexp 1)) |
858 | ;; Now check that like matches like. | |
859 | (goto-char start) | |
860 | (while (progn (skip-syntax-forward "^(") | |
861 | (not (eobp))) | |
862 | (let ((match (matching-paren (following-char)))) | |
863 | (save-excursion | |
864 | (forward-sexp 1) | |
865 | (or (= (preceding-char) match) | |
866 | (error "Mismatched parentheses")))) | |
867 | (forward-char 1))) | |
869bff31 | 868 | (error |
710a0f53 | 869 | (skip-syntax-forward " .>") |
869bff31 | 870 | (setq failure-point (point))))) |
871 | (if failure-point | |
872 | (progn | |
873 | (goto-char failure-point) | |
874 | nil) | |
875 | t))) | |
876 | ||
877 | (defun tex-terminate-paragraph (inhibit-validation) | |
878 | "Insert two newlines, breaking a paragraph for TeX. | |
81c735c0 | 879 | Check for mismatched braces or $s in paragraph being terminated. |
869bff31 | 880 | A prefix arg inhibits the checking." |
528415e7 | 881 | (interactive "*P") |
869bff31 | 882 | (or inhibit-validation |
883 | (save-excursion | |
884 | (tex-validate-region | |
885 | (save-excursion | |
886 | (search-backward "\n\n" nil 'move) | |
887 | (point)) | |
888 | (point))) | |
889 | (message "Paragraph being closed appears to contain a mismatch")) | |
890 | (insert "\n\n")) | |
891 | ||
892 | (defun tex-insert-braces () | |
893 | "Make a pair of braces and be poised to type inside of them." | |
528415e7 | 894 | (interactive "*") |
869bff31 | 895 | (insert ?\{) |
896 | (save-excursion | |
897 | (insert ?}))) | |
898 | ||
9bd4f69c RS |
899 | ;; This function is used as the value of fill-nobreak-predicate |
900 | ;; in LaTeX mode. Its job is to prevent line-breaking inside | |
901 | ;; of a \verb construct. | |
902 | (defun latex-fill-nobreak-predicate () | |
903 | (let ((opoint (point)) | |
904 | inside) | |
905 | (save-excursion | |
906 | (save-restriction | |
907 | (beginning-of-line) | |
908 | (narrow-to-region (point) opoint) | |
909 | (while (re-search-forward "\\\\verb\\(.\\)" nil t) | |
910 | (unless (re-search-forward (regexp-quote (match-string 1)) nil t) | |
911 | (setq inside t))))) | |
912 | inside)) | |
913 | ||
869bff31 | 914 | ;;; Like tex-insert-braces, but for LaTeX. |
cf149bdf RS |
915 | (define-skeleton tex-latex-block |
916 | "Create a matching pair of lines \\begin[OPT]{NAME} and \\end{NAME} at point. | |
869bff31 | 917 | Puts point on a blank line between them." |
cf149bdf RS |
918 | (completing-read "LaTeX block name: " |
919 | (mapcar 'list | |
920 | (append standard-latex-block-names | |
921 | latex-block-names))) | |
922 | "\\begin[" | |
923 | (skeleton-read "[options]: ") & ?\] | -1 | |
924 | ?\{ | |
925 | str | |
926 | ?\} \n | |
927 | _ \n | |
928 | "\\end{" str ?\}) | |
869bff31 | 929 | |
930 | (defun tex-last-unended-begin () | |
81c735c0 | 931 | "Leave point at the beginning of the last `\\begin{...}' that is unended." |
869bff31 | 932 | (while (and (re-search-backward "\\(\\\\begin\\s *{\\)\\|\\(\\\\end\\s *{\\)") |
933 | (looking-at "\\\\end{")) | |
934 | (tex-last-unended-begin))) | |
935 | ||
6a900cf1 ER |
936 | (defun tex-goto-last-unclosed-latex-block () |
937 | "Move point to the last unclosed \\begin{...}. | |
938 | Mark is left at original location." | |
939 | (interactive) | |
940 | (let ((spot)) | |
941 | (save-excursion | |
942 | (condition-case nil | |
943 | (tex-last-unended-begin) | |
944 | (error (error "Couldn't find unended \\begin"))) | |
945 | (setq spot (point))) | |
946 | (push-mark) | |
947 | (goto-char spot))) | |
948 | ||
869bff31 | 949 | (defun tex-close-latex-block () |
950 | "Creates an \\end{...} to match the last unclosed \\begin{...}." | |
951 | (interactive "*") | |
952 | (let ((new-line-needed (bolp)) | |
953 | text indentation) | |
954 | (save-excursion | |
528415e7 | 955 | (condition-case nil |
869bff31 | 956 | (tex-last-unended-begin) |
957 | (error (error "Couldn't find unended \\begin"))) | |
958 | (setq indentation (current-column)) | |
959 | (re-search-forward "\\\\begin\\(\\s *{[^}\n]*}\\)") | |
960 | (setq text (buffer-substring (match-beginning 1) (match-end 1)))) | |
961 | (indent-to indentation) | |
962 | (insert "\\end" text) | |
963 | (if new-line-needed (insert ?\n)))) | |
964 | \f | |
965 | ;;; Invoking TeX in an inferior shell. | |
966 | ||
967 | ;;; Why use a shell instead of running TeX directly? Because if TeX | |
968 | ;;; gets stuck, the user can switch to the shell window and type at it. | |
969 | ||
970 | ;;; The utility functions: | |
971 | ||
d974af30 | 972 | ;;;###autoload |
869bff31 | 973 | (defun tex-start-shell () |
974 | (save-excursion | |
528415e7 RS |
975 | (set-buffer |
976 | (make-comint | |
977 | "tex-shell" | |
978 | (or tex-shell-file-name (getenv "ESHELL") (getenv "SHELL") "/bin/sh") | |
4cdc1d4b | 979 | nil)) |
528415e7 RS |
980 | (let ((proc (get-process "tex-shell"))) |
981 | (set-process-sentinel proc 'tex-shell-sentinel) | |
982 | (process-kill-without-query proc) | |
e8c04c88 | 983 | (setq comint-prompt-regexp shell-prompt-pattern) |
191cda94 | 984 | (setq tex-shell-map (nconc (make-sparse-keymap) shell-mode-map)) |
528415e7 RS |
985 | (tex-define-common-keys tex-shell-map) |
986 | (use-local-map tex-shell-map) | |
2974ad46 | 987 | (compilation-minor-mode) |
528415e7 | 988 | (run-hooks 'tex-shell-hook) |
f1143679 | 989 | (setq comint-input-filter-functions 'shell-directory-tracker) |
528415e7 | 990 | (while (zerop (buffer-size)) |
51b2c841 RS |
991 | (sleep-for 1))))) |
992 | ||
993 | (defun tex-display-shell () | |
994 | "Make the TeX shell buffer visible in a window." | |
995 | (display-buffer (process-buffer (get-process "tex-shell"))) | |
996 | (tex-recenter-output-buffer nil)) | |
528415e7 RS |
997 | |
998 | (defun tex-shell-sentinel (proc msg) | |
999 | (cond ((null (buffer-name (process-buffer proc))) | |
1000 | ;; buffer killed | |
1001 | (set-process-buffer proc nil) | |
1002 | (tex-delete-last-temp-files)) | |
1003 | ((memq (process-status proc) '(signal exit)) | |
1004 | (tex-delete-last-temp-files)))) | |
1005 | ||
1006 | (defun tex-set-buffer-directory (buffer directory) | |
869bff31 | 1007 | "Set BUFFER's default directory to be DIRECTORY." |
1008 | (setq directory (file-name-as-directory (expand-file-name directory))) | |
1009 | (if (not (file-directory-p directory)) | |
1010 | (error "%s is not a directory" directory) | |
1011 | (save-excursion | |
1012 | (set-buffer buffer) | |
1013 | (setq default-directory directory)))) | |
1014 | ||
30803a05 RS |
1015 | (defvar tex-send-command-modified-tick 0) |
1016 | (make-variable-buffer-local 'tex-send-command-modified-tick) | |
1017 | ||
528415e7 | 1018 | (defun tex-send-command (command &optional file background) |
4cdc1d4b | 1019 | "Send COMMAND to TeX shell process, substituting optional FILE for *. |
8241d7b9 ER |
1020 | Do this in background if optional BACKGROUND is t. If COMMAND has no *, |
1021 | FILE will be appended, preceded by a blank, to COMMAND. If FILE is nil, no | |
1022 | substitution will be made in COMMAND. COMMAND can be any expression that | |
460e1b7d RS |
1023 | evaluates to a command string. |
1024 | ||
1025 | Return the process in which TeX is running." | |
528415e7 RS |
1026 | (save-excursion |
1027 | (let* ((cmd (eval command)) | |
c93931c3 | 1028 | (proc (or (get-process "tex-shell") (error "No TeX subprocess"))) |
64db2461 | 1029 | (buf (process-buffer proc)) |
4f45adda | 1030 | (star (string-match "\\*" cmd)) |
4cdc1d4b RS |
1031 | (string |
1032 | (concat | |
1033 | (if file | |
1034 | (if star (concat (substring cmd 0 star) | |
1035 | file (substring cmd (1+ star))) | |
1036 | (concat cmd " " file)) | |
1037 | cmd) | |
1038 | (if background "&" "")))) | |
64db2461 RS |
1039 | ;; Switch to buffer before checking for subproc output in it. |
1040 | (set-buffer buf) | |
30803a05 RS |
1041 | ;; If text is unchanged since previous tex-send-command, |
1042 | ;; we haven't got any output. So wait for output now. | |
64db2461 | 1043 | (if (= (buffer-modified-tick buf) tex-send-command-modified-tick) |
30803a05 | 1044 | (accept-process-output proc)) |
4cdc1d4b RS |
1045 | (goto-char (process-mark proc)) |
1046 | (insert string) | |
30803a05 | 1047 | (comint-send-input) |
460e1b7d RS |
1048 | (setq tex-send-command-modified-tick (buffer-modified-tick buf)) |
1049 | proc))) | |
528415e7 | 1050 | |
a15849cb RS |
1051 | (defun tex-delete-last-temp-files (&optional not-all) |
1052 | "Delete any junk files from last temp file. | |
1053 | If NOT-ALL is non-nil, save the `.dvi' file." | |
528415e7 RS |
1054 | (if tex-last-temp-file |
1055 | (let* ((dir (file-name-directory tex-last-temp-file)) | |
adf6b7f9 KH |
1056 | (list (and (file-directory-p dir) |
1057 | (file-name-all-completions | |
02fd229c RS |
1058 | (file-name-sans-extension |
1059 | (file-name-nondirectory tex-last-temp-file)) | |
1060 | dir)))) | |
adf6b7f9 | 1061 | (while list |
a15849cb RS |
1062 | (if not-all |
1063 | (and | |
1064 | ;; If arg is non-nil, don't delete the .dvi file. | |
1065 | (not (string-match "\\.dvi$" (car list))) | |
1066 | (delete-file (concat dir (car list)))) | |
1067 | (delete-file (concat dir (car list)))) | |
528415e7 RS |
1068 | (setq list (cdr list)))))) |
1069 | ||
99621a14 | 1070 | (add-hook 'kill-emacs-hook 'tex-delete-last-temp-files) |
869bff31 | 1071 | |
460e1b7d RS |
1072 | (defvar tex-start-tex-marker nil |
1073 | "Marker pointing after last TeX-running command in the TeX shell buffer.") | |
1074 | ||
1075 | (defun tex-start-tex (command file) | |
1076 | "Start a TeX run, using COMMAND on FILE." | |
6633b891 | 1077 | (let* ((star (string-match "\\*" command)) |
460e1b7d | 1078 | (compile-command |
6633b891 KH |
1079 | (if star |
1080 | (concat (substring command 0 star) | |
1081 | (comint-quote-filename file) | |
1082 | (substring command (1+ star))) | |
1083 | (concat command " " | |
1084 | (comint-quote-filename tex-start-options-string) " " | |
1085 | (comint-quote-filename file))))) | |
460e1b7d RS |
1086 | (with-current-buffer (process-buffer (tex-send-command compile-command)) |
1087 | (save-excursion | |
1088 | (forward-line -1) | |
1089 | (setq tex-start-tex-marker (point-marker))) | |
1090 | (make-local-variable 'compilation-parse-errors-function) | |
1091 | (setq compilation-parse-errors-function 'tex-compilation-parse-errors)))) | |
1092 | \f | |
1093 | (defun tex-compilation-parse-errors (limit-search find-at-least) | |
1094 | "Parse the current buffer as error messages. | |
1095 | This makes a list of error descriptors, `compilation-error-list'. | |
1096 | For each source-file, line-number pair in the buffer, | |
1097 | the source file is read in, and the text location is saved in | |
1098 | `compilation-error-list'. The function `next-error', assigned to | |
1099 | \\[next-error], takes the next error off the list and visits its location. | |
1100 | ||
1101 | If LIMIT-SEARCH is non-nil, don't bother parsing past that location. | |
1102 | If FIND-AT-LEAST is non-nil, don't bother parsing after finding that | |
1103 | ||
1104 | This function works on TeX compilations only. It is necessary for | |
1105 | that purpose, since TeX does not put file names on the same line as | |
1106 | line numbers for the errors." | |
1107 | (require 'thingatpt) | |
1108 | (setq compilation-error-list nil) | |
1109 | (message "Parsing error messages...") | |
1110 | (let ((old-lc-syntax (char-syntax ?\{)) | |
1111 | (old-rc-syntax (char-syntax ?\})) | |
1112 | (old-lb-syntax (char-syntax ?\[)) | |
1113 | (old-rb-syntax (char-syntax ?\])) | |
1114 | (num-found 0) last-filename last-linenum last-position) | |
1115 | (unwind-protect | |
1116 | (progn | |
1117 | (modify-syntax-entry ?\{ "_") | |
1118 | (modify-syntax-entry ?\} "_") | |
1119 | (modify-syntax-entry ?\[ "_") | |
1120 | (modify-syntax-entry ?\] "_") | |
1121 | ;; Don't reparse messages already seen at last parse. | |
1122 | (goto-char (max (or compilation-parsing-end 0) | |
1123 | tex-start-tex-marker)) | |
1124 | ;; Don't parse the first two lines as error messages. | |
1125 | ;; This matters for grep. | |
1126 | (if (bobp) (forward-line 2)) | |
1127 | (while (re-search-forward | |
1128 | "^l\\.\\([0-9]+\\) \\(\\.\\.\\.\\)?\\(.*\\)$" | |
1129 | (and (or (null find-at-least) | |
1130 | (>= num-found find-at-least)) limit-search) t) | |
1131 | ;; Extract file name and line number from error message. | |
1132 | ;; Line number is 2 away from beginning of line: "l.23" | |
1133 | ;; The file is the one that was opened last and is still open. | |
1134 | ;; We need to find the last open parenthesis. | |
1135 | (let* ((linenum (string-to-int (match-string 1))) | |
1136 | (error-text (regexp-quote (match-string 3))) | |
1137 | (filename | |
1138 | (save-excursion | |
1139 | (backward-up-list 1) | |
1140 | (skip-syntax-forward "(_") | |
1141 | (while (not (file-readable-p | |
1142 | (thing-at-point 'filename))) | |
1143 | (skip-syntax-backward "(_") | |
1144 | (backward-up-list 1) | |
1145 | (skip-syntax-forward "(_")) | |
1146 | (thing-at-point 'filename))) | |
1147 | (error-marker | |
1148 | (save-excursion | |
1149 | (re-search-backward "^! " nil t) | |
1150 | (point-marker))) | |
1151 | (new-file (or (null last-filename) | |
1152 | (not (string-equal last-filename filename)))) | |
1153 | (error-location | |
1154 | (save-excursion | |
691d39b7 | 1155 | (if (equal filename tex-last-temp-file) |
460e1b7d RS |
1156 | (set-buffer tex-last-buffer-texed) |
1157 | (set-buffer (find-file-noselect filename))) | |
1158 | (if new-file | |
1159 | (goto-line linenum) | |
1160 | (goto-char last-position) | |
1161 | (forward-line (- linenum last-linenum))) | |
1162 | ;first try a forward search | |
1163 | ;for the error text, then a | |
1164 | ;backward search limited by | |
1165 | ;the last error | |
1166 | (let ((starting-point (point))) | |
1167 | (or (re-search-forward error-text nil t) | |
1168 | (re-search-backward | |
1169 | error-text | |
1170 | (marker-position last-position) t) | |
1171 | (goto-char starting-point))) | |
1172 | (point-marker)))) | |
1173 | (setq last-filename filename) | |
1174 | (if (or new-file | |
1175 | (not (= last-position error-location))) | |
1176 | (progn | |
1177 | (setq num-found (1+ num-found)) | |
1178 | (setq last-position error-location) | |
1179 | (setq last-linenum linenum) | |
1180 | (setq compilation-error-list | |
1181 | (nconc compilation-error-list | |
1182 | (list (cons error-marker | |
1183 | error-location))))))))) | |
1184 | (modify-syntax-entry ?\{ (char-to-string old-lc-syntax)) | |
1185 | (modify-syntax-entry ?\} (char-to-string old-rc-syntax)) | |
1186 | (modify-syntax-entry ?\[ (char-to-string old-lb-syntax)) | |
1187 | (modify-syntax-entry ?\] (char-to-string old-rb-syntax)))) | |
1188 | (setq compilation-parsing-end (point)) | |
1189 | (message "Parsing error messages...done")) | |
1190 | \f | |
528415e7 | 1191 | ;;; The commands: |
869bff31 | 1192 | |
1193 | (defun tex-region (beg end) | |
1194 | "Run TeX on the current region, via a temporary file. | |
1195 | The file's name comes from the variable `tex-zap-file' and the | |
1196 | variable `tex-directory' says where to put it. | |
1197 | ||
1198 | If the buffer has a header, the header is given to TeX before the | |
1199 | region itself. The buffer's header is all lines between the strings | |
1200 | defined by `tex-start-of-header' and `tex-end-of-header' inclusive. | |
1201 | The header must start in the first 100 lines of the buffer. | |
1202 | ||
1203 | The value of `tex-trailer' is given to TeX as input after the region. | |
1204 | ||
1205 | The value of `tex-command' specifies the command to use to run TeX." | |
1206 | (interactive "r") | |
1207 | (if (tex-shell-running) | |
1208 | (tex-kill-job) | |
1209 | (tex-start-shell)) | |
1210 | (or tex-zap-file | |
1211 | (setq tex-zap-file (tex-generate-zap-file-name))) | |
6d34c59f RS |
1212 | ;; Temp file will be written and TeX will be run in zap-directory. |
1213 | ;; If the TEXINPUTS file has relative directories or if the region has | |
1214 | ;; \input of files, this must be the same directory as the file for | |
1215 | ;; TeX to access the correct inputs. That's why it's safest if | |
1216 | ;; tex-directory is ".". | |
1217 | (let* ((zap-directory | |
528415e7 | 1218 | (file-name-as-directory (expand-file-name tex-directory))) |
6d34c59f | 1219 | (tex-out-file (concat zap-directory tex-zap-file ".tex"))) |
0d548e5d RS |
1220 | ;; Don't delete temp files if we do the same buffer twice in a row. |
1221 | (or (eq (current-buffer) tex-last-buffer-texed) | |
1222 | (tex-delete-last-temp-files t)) | |
869bff31 | 1223 | ;; Write the new temp file. |
1224 | (save-excursion | |
1225 | (save-restriction | |
1226 | (widen) | |
1227 | (goto-char (point-min)) | |
1228 | (forward-line 100) | |
1229 | (let ((search-end (point)) | |
6d34c59f RS |
1230 | (default-directory zap-directory) |
1231 | (already-output 0)) | |
869bff31 | 1232 | (goto-char (point-min)) |
6d34c59f | 1233 | |
725b7428 RS |
1234 | ;; Maybe copy first line, such as `\input texinfo', to temp file. |
1235 | (and tex-first-line-header-regexp | |
1236 | (looking-at tex-first-line-header-regexp) | |
1237 | (write-region (point) | |
6d34c59f RS |
1238 | (progn (forward-line 1) |
1239 | (setq already-output (point))) | |
725b7428 RS |
1240 | tex-out-file nil nil)) |
1241 | ||
6d34c59f RS |
1242 | ;; Write out the header, if there is one, |
1243 | ;; and any of the specified region which extends before it. | |
1244 | ;; But don't repeat anything already written. | |
898b9ac1 | 1245 | (if (re-search-forward tex-start-of-header search-end t) |
6d34c59f | 1246 | (let (hbeg) |
869bff31 | 1247 | (beginning-of-line) |
1248 | (setq hbeg (point)) ;mark beginning of header | |
898b9ac1 | 1249 | (if (re-search-forward tex-end-of-header nil t) |
6d34c59f RS |
1250 | (let (hend) |
1251 | (forward-line 1) | |
1252 | (setq hend (point)) ;mark end of header | |
1253 | (write-region (max (min hbeg beg) already-output) | |
1254 | hend | |
1255 | tex-out-file | |
1256 | (not (zerop already-output)) nil) | |
1257 | (setq already-output hend))))) | |
1258 | ||
1259 | ;; Write out the specified region | |
1260 | ;; (but don't repeat anything already written). | |
1261 | (write-region (max beg already-output) end | |
1262 | tex-out-file | |
1263 | (not (zerop already-output)) nil)) | |
1264 | ;; Write the trailer, if any. | |
1265 | ;; Precede it with a newline to make sure it | |
1266 | ;; is not hidden in a comment. | |
1267 | (if tex-trailer | |
1268 | (write-region (concat "\n" tex-trailer) nil | |
1269 | tex-out-file t nil)))) | |
528415e7 RS |
1270 | ;; Record the file name to be deleted afterward. |
1271 | (setq tex-last-temp-file tex-out-file) | |
1272 | (tex-send-command tex-shell-cd-command zap-directory) | |
460e1b7d | 1273 | (tex-start-tex tex-command tex-out-file) |
51b2c841 | 1274 | (tex-display-shell) |
528415e7 RS |
1275 | (setq tex-print-file tex-out-file) |
1276 | (setq tex-last-buffer-texed (current-buffer)))) | |
869bff31 | 1277 | |
1278 | (defun tex-buffer () | |
1279 | "Run TeX on current buffer. See \\[tex-region] for more information. | |
528415e7 RS |
1280 | Does not save the buffer, so it's useful for trying experimental versions. |
1281 | See \\[tex-file] for an alternative." | |
869bff31 | 1282 | (interactive) |
1283 | (tex-region (point-min) (point-max))) | |
1284 | ||
1285 | (defun tex-file () | |
1286 | "Prompt to save all buffers and run TeX (or LaTeX) on current buffer's file. | |
1287 | This function is more useful than \\[tex-buffer] when you need the | |
1288 | `.aux' file of LaTeX to have the correct name." | |
1289 | (interactive) | |
f73741fc RS |
1290 | (let ((source-file |
1291 | (or tex-main-file | |
1292 | (if (buffer-file-name) | |
1293 | (file-name-nondirectory (buffer-file-name)) | |
1294 | (error "Buffer does not seem to be associated with any file")))) | |
869bff31 | 1295 | (file-dir (file-name-directory (buffer-file-name)))) |
7047ec77 | 1296 | (if tex-offer-save |
99621a14 | 1297 | (save-some-buffers)) |
869bff31 | 1298 | (if (tex-shell-running) |
1299 | (tex-kill-job) | |
1300 | (tex-start-shell)) | |
528415e7 | 1301 | (tex-send-command tex-shell-cd-command file-dir) |
460e1b7d | 1302 | (tex-start-tex tex-command source-file) |
23b64225 RS |
1303 | (tex-display-shell) |
1304 | (setq tex-last-buffer-texed (current-buffer)) | |
1305 | (setq tex-print-file source-file))) | |
869bff31 | 1306 | |
1307 | (defun tex-generate-zap-file-name () | |
1308 | "Generate a unique name suitable for use as a file name." | |
1309 | ;; Include the shell process number and host name | |
1310 | ;; in case there are multiple shells (for same or different user). | |
1311 | (format "#tz%d%s" | |
1312 | (process-id (get-buffer-process "*tex-shell*")) | |
1313 | (tex-strip-dots (system-name)))) | |
1314 | ||
1315 | (defun tex-strip-dots (s) | |
1316 | (setq s (copy-sequence s)) | |
1317 | (while (string-match "\\." s) | |
1318 | (aset s (match-beginning 0) ?-)) | |
1319 | s) | |
1320 | ||
1321 | ;; This will perhaps be useful for modifying TEXINPUTS. | |
1322 | ;; Expand each file name, separated by colons, in the string S. | |
1323 | (defun tex-expand-files (s) | |
1324 | (let (elts (start 0)) | |
1325 | (while (string-match ":" s start) | |
1326 | (setq elts (cons (substring s start (match-beginning 0)) elts)) | |
1327 | (setq start (match-end 0))) | |
1328 | (or (= start 0) | |
1329 | (setq elts (cons (substring s start) elts))) | |
1330 | (mapconcat 'expand-file-name (nreverse elts) ":"))) | |
1331 | ||
1332 | (defun tex-shell-running () | |
1333 | (and (get-process "tex-shell") | |
1334 | (eq (process-status (get-process "tex-shell")) 'run))) | |
1335 | ||
1336 | (defun tex-kill-job () | |
1337 | "Kill the currently running TeX job." | |
1338 | (interactive) | |
528415e7 | 1339 | (quit-process (get-process "tex-shell") t)) |
869bff31 | 1340 | |
1341 | (defun tex-recenter-output-buffer (linenum) | |
1342 | "Redisplay buffer of TeX job output so that most recent output can be seen. | |
1343 | The last line of the buffer is displayed on | |
1344 | line LINE of the window, or centered if LINE is nil." | |
1345 | (interactive "P") | |
1346 | (let ((tex-shell (get-buffer "*tex-shell*")) | |
23b64225 RS |
1347 | (old-buffer (current-buffer)) |
1348 | (window)) | |
869bff31 | 1349 | (if (null tex-shell) |
1350 | (message "No TeX output buffer") | |
23b64225 RS |
1351 | (setq window (display-buffer tex-shell)) |
1352 | (save-selected-window | |
1353 | (select-window window) | |
1354 | (bury-buffer tex-shell) | |
1355 | (goto-char (point-max)) | |
1356 | (recenter (if linenum | |
1357 | (prefix-numeric-value linenum) | |
1358 | (/ (window-height) 2))))))) | |
869bff31 | 1359 | |
528415e7 | 1360 | (defun tex-print (&optional alt) |
869bff31 | 1361 | "Print the .dvi file made by \\[tex-region], \\[tex-buffer] or \\[tex-file]. |
1433a222 CZ |
1362 | Runs the shell command defined by `tex-dvi-print-command'. If prefix argument |
1363 | is provided, use the alternative command, `tex-alt-dvi-print-command'." | |
528415e7 | 1364 | (interactive "P") |
869bff31 | 1365 | (let ((print-file-name-dvi (tex-append tex-print-file ".dvi")) |
1366 | test-name) | |
1367 | (if (and (not (equal (current-buffer) tex-last-buffer-texed)) | |
45c3304d RS |
1368 | (buffer-file-name) |
1369 | ;; Check that this buffer's printed file is up to date. | |
869bff31 | 1370 | (file-newer-than-file-p |
1371 | (setq test-name (tex-append (buffer-file-name) ".dvi")) | |
45c3304d | 1372 | (buffer-file-name))) |
869bff31 | 1373 | (setq print-file-name-dvi test-name)) |
528415e7 RS |
1374 | (if (not (file-exists-p print-file-name-dvi)) |
1375 | (error "No appropriate `.dvi' file could be found") | |
460e1b7d RS |
1376 | (if (tex-shell-running) |
1377 | (tex-kill-job) | |
1378 | (tex-start-shell)) | |
528415e7 RS |
1379 | (tex-send-command |
1380 | (if alt tex-alt-dvi-print-command tex-dvi-print-command) | |
1381 | print-file-name-dvi t)))) | |
869bff31 | 1382 | |
cf6d6e8a RS |
1383 | (defun tex-alt-print () |
1384 | "Print the .dvi file made by \\[tex-region], \\[tex-buffer] or \\[tex-file]. | |
002b0d00 | 1385 | Runs the shell command defined by `tex-alt-dvi-print-command'." |
cf6d6e8a RS |
1386 | (interactive) |
1387 | (tex-print t)) | |
1388 | ||
869bff31 | 1389 | (defun tex-view () |
1390 | "Preview the last `.dvi' file made by running TeX under Emacs. | |
1391 | This means, made using \\[tex-region], \\[tex-buffer] or \\[tex-file]. | |
2b7971c9 RS |
1392 | The variable `tex-dvi-view-command' specifies the shell command for preview. |
1393 | You must set that variable yourself before using this command, | |
1394 | because there is no standard value that would generally work." | |
869bff31 | 1395 | (interactive) |
2b7971c9 RS |
1396 | (or tex-dvi-view-command |
1397 | (error "You must set `tex-dvi-view-command'")) | |
869bff31 | 1398 | (let ((tex-dvi-print-command tex-dvi-view-command)) |
1399 | (tex-print))) | |
1400 | ||
1401 | (defun tex-append (file-name suffix) | |
1402 | "Append to FILENAME the suffix SUFFIX, using same algorithm TeX uses. | |
cf6d6e8a | 1403 | Pascal-based TeX scans for the first period, C TeX uses the last. |
869bff31 | 1404 | No period is retained immediately before SUFFIX, |
1405 | so normally SUFFIX starts with one." | |
1406 | (if (stringp file-name) | |
cf6d6e8a RS |
1407 | (let ((file (file-name-nondirectory file-name)) |
1408 | trial-name) | |
6da9bbd6 | 1409 | ;; Try splitting on last period. |
7d0ca249 RS |
1410 | ;; The first-period split can get fooled when two files |
1411 | ;; named a.tex and a.b.tex are both tex'd; | |
1412 | ;; the last-period split must be right if it matches at all. | |
cf6d6e8a RS |
1413 | (setq trial-name |
1414 | (concat (file-name-directory file-name) | |
1415 | (substring file 0 | |
7d0ca249 | 1416 | (string-match "\\.[^.]*$" file)) |
cf6d6e8a RS |
1417 | suffix)) |
1418 | (if (or (file-exists-p trial-name) | |
1419 | (file-exists-p (concat trial-name ".aux"))) ;for BibTeX files | |
1420 | trial-name | |
7d0ca249 | 1421 | ;; Not found, so split on first period. |
cf6d6e8a RS |
1422 | (concat (file-name-directory file-name) |
1423 | (substring file 0 | |
7d0ca249 | 1424 | (string-match "\\." file)) |
cf6d6e8a | 1425 | suffix))) |
869bff31 | 1426 | " ")) |
1427 | ||
1428 | (defun tex-show-print-queue () | |
1429 | "Show the print queue that \\[tex-print] put your job on. | |
1433a222 | 1430 | Runs the shell command defined by `tex-show-queue-command'." |
869bff31 | 1431 | (interactive) |
1432 | (if (tex-shell-running) | |
1433 | (tex-kill-job) | |
1434 | (tex-start-shell)) | |
51b2c841 RS |
1435 | (tex-send-command tex-show-queue-command) |
1436 | (tex-display-shell)) | |
869bff31 | 1437 | |
1438 | (defun tex-bibtex-file () | |
1439 | "Run BibTeX on the current buffer's file." | |
1440 | (interactive) | |
1441 | (if (tex-shell-running) | |
1442 | (tex-kill-job) | |
1443 | (tex-start-shell)) | |
1444 | (let ((tex-out-file | |
1445 | (tex-append (file-name-nondirectory (buffer-file-name)) "")) | |
1446 | (file-dir (file-name-directory (buffer-file-name)))) | |
528415e7 | 1447 | (tex-send-command tex-shell-cd-command file-dir) |
51b2c841 RS |
1448 | (tex-send-command tex-bibtex-command tex-out-file)) |
1449 | (tex-display-shell)) | |
528415e7 RS |
1450 | |
1451 | (run-hooks 'tex-mode-load-hook) | |
869bff31 | 1452 | |
49116ac0 JB |
1453 | (provide 'tex-mode) |
1454 | ||
d501f516 | 1455 | ;;; tex-mode.el ends here |