Commit | Line | Data |
---|---|---|
d501f516 ER |
1 | ;;; tex-mode.el --- TeX, LaTeX, and SliTeX mode commands. |
2 | ||
e5167999 ER |
3 | ;; Maintainer: Edward M. Reingold <reingold@cs.uiuc.edu> |
4 | ;; Last-Modified: 04 July 1992 | |
5 | ||
528415e7 RS |
6 | ;; Contributions over the years by William F. Schelter, Dick King, |
7 | ;; Stephen Gildea, Michael Prange, and Edward M. Reingold. | |
8 | ||
e5167999 | 9 | ;; Copyright (C) 1985, 1986, 1989, 1992 Free Software Foundation, Inc. |
869bff31 | 10 | |
11 | ;; This file is part of GNU Emacs. | |
12 | ||
13 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
14 | ;; it under the terms of the GNU General Public License as published by | |
e5167999 | 15 | ;; the Free Software Foundation; either version 2, or (at your option) |
869bff31 | 16 | ;; any later version. |
17 | ||
18 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
19 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | ;; GNU General Public License for more details. | |
22 | ||
23 | ;; You should have received a copy of the GNU General Public License | |
24 | ;; along with GNU Emacs; see the file COPYING. If not, write to | |
25 | ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
26 | ||
e5167999 ER |
27 | ;;; Code: |
28 | ||
528415e7 RS |
29 | (require 'comint) |
30 | ||
31 | (defvar tex-shell-file-name nil | |
32 | "*If non-nil, is file name to use for the subshell in which TeX is run.") | |
33 | ||
34 | (defvar tex-directory "." | |
35 | "*Directory in which temporary files are left. | |
36 | You can make this /tmp if your TEXINPUTS has no relative directories in it | |
37 | and you don't try to apply \\[tex-region] or \\[tex-buffer] when there are | |
38 | \\input commands with relative directories.") | |
869bff31 | 39 | |
528415e7 RS |
40 | (defvar tex-offer-save t |
41 | "*If non-nil, ask about saving modified buffers before \\[tex-file] is run.") | |
869bff31 | 42 | |
43 | (defvar tex-run-command "tex" | |
44 | "*Command used to run TeX subjob. | |
528415e7 RS |
45 | If this string contains an asterisk (*), it will be replaced by the |
46 | filename; if not, the name of the file, preceded by blank, will be added to | |
47 | this string.") | |
869bff31 | 48 | |
49 | (defvar latex-run-command "latex" | |
50 | "*Command used to run LaTeX subjob. | |
528415e7 RS |
51 | If this string contains an asterisk (*), it will be replaced by the |
52 | filename; if not, the name of the file, preceded by blank, will be added to | |
53 | this string.") | |
54 | ||
55 | (defvar standard-latex-block-names | |
56 | '("abstract" "array" "center" "description" | |
57 | "displaymath" "document" "enumerate" "eqnarray" | |
58 | "eqnarray*" "equation" "figure" "figure*" | |
59 | "flushleft" "flushright" "itemize" "letter" | |
60 | "list" "minipage" "picture" "quotation" | |
61 | "quote" "slide" "sloppypar" "tabbing" | |
62 | "table" "table*" "tabular" "tabular*" | |
63 | "thebibliography" "theindex*" "titlepage" "trivlist" | |
64 | "verbatim" "verbatim*" "verse") | |
65 | "Standard LaTeX block names.") | |
66 | ||
67 | (defvar latex-block-names nil | |
68 | "*User defined LaTeX block names. | |
69 | Combined with `standard-latex-block-names' for minibuffer completion.") | |
869bff31 | 70 | |
71 | (defvar slitex-run-command "slitex" | |
72 | "*Command used to run SliTeX subjob. | |
528415e7 RS |
73 | If this string contains an asterisk (*), it will be replaced by the |
74 | filename; if not, the name of the file, preceded by blank, will be added to | |
75 | this string.") | |
869bff31 | 76 | |
77 | (defvar tex-bibtex-command "bibtex" | |
528415e7 RS |
78 | "*Command used by `tex-bibtex-file' to gather bibliographic data. |
79 | If this string contains an asterisk (*), it will be replaced by the | |
80 | filename; if not, the name of the file, preceded by blank, will be added to | |
81 | this string.") | |
869bff31 | 82 | |
83 | (defvar tex-dvi-print-command "lpr -d" | |
528415e7 RS |
84 | "*Command used by \\[tex-print] to print a .dvi file. |
85 | If this string contains an asterisk (*), it will be replaced by the | |
86 | filename; if not, the name of the file, preceded by blank, will be added to | |
87 | this string.") | |
88 | ||
89 | (defvar tex-alt-dvi-print-command "lpr -d" | |
90 | "*Command used by \\[tex-print] with a prefix arg to print a .dvi file. | |
91 | If this string contains an asterisk (*), it will be replaced by the | |
92 | filename; if not, the name of the file, preceded by blank, will be added to | |
93 | this string. | |
94 | ||
95 | If two printers are not enough of a choice, you can define the value | |
96 | of tex-alt-dvi-print-command to be an expression that asks what you want; | |
97 | for example, | |
98 | ||
99 | (setq tex-alt-dvi-print-command | |
100 | '(format \"lpr -P%s\" (read-string \"Use printer: \"))) | |
101 | ||
102 | would tell \\[tex-print] with a prefix argument to ask you which printer to | |
103 | use.") | |
869bff31 | 104 | |
105 | (defvar tex-dvi-view-command nil | |
528415e7 RS |
106 | "*Command used by \\[tex-view] to display a .dvi file. |
107 | If this string contains an asterisk (*), it will be replaced by the | |
108 | filename; if not, the name of the file, preceded by blank, will be added to | |
109 | this string. | |
110 | ||
111 | This can be set conditionally so that the previewer used is suitable for the | |
112 | window system being used. For example, | |
113 | ||
114 | (setq tex-dvi-view-command | |
115 | (if (eq window-system 'x) \"xdvi\" \"dvi2tty * | cat -s\")) | |
116 | ||
117 | would tell \\[tex-view] use xdvi under X windows and to use dvi2tty | |
118 | otherwise.") | |
869bff31 | 119 | |
120 | (defvar tex-show-queue-command "lpq" | |
528415e7 RS |
121 | "*Command used by \\[tex-show-print-queue] to show the print queue. |
122 | Should show the queue(s) that \\[tex-print] puts jobs on.") | |
869bff31 | 123 | |
124 | (defvar tex-default-mode 'plain-tex-mode | |
125 | "*Mode to enter for a new file that might be either TeX or LaTeX. | |
126 | This variable is used when it can't be determined whether the file | |
127 | is plain TeX or LaTeX or what because the file contains no commands. | |
128 | Normally set to either 'plain-tex-mode or 'latex-mode.") | |
129 | ||
130 | (defvar tex-open-quote "``" | |
131 | "*String inserted by typing \\[tex-insert-quote] to open a quotation.") | |
132 | ||
133 | (defvar tex-close-quote "''" | |
134 | "*String inserted by typing \\[tex-insert-quote] to close a quotation.") | |
135 | ||
528415e7 RS |
136 | (defvar tex-last-temp-file nil |
137 | "Latest temporary file generated by \\[tex-region] and \\[tex-buffer]. | |
138 | Deleted when the \\[tex-region] or \\[tex-buffer] is next run, or when the | |
139 | tex-shell goes away.") | |
140 | ||
869bff31 | 141 | (defvar tex-command nil |
142 | "Command to run TeX. | |
528415e7 | 143 | The name of the file, preceded by a blank, will be added to this string.") |
869bff31 | 144 | |
145 | (defvar tex-trailer nil | |
146 | "String appended after the end of a region sent to TeX by \\[tex-region].") | |
147 | ||
148 | (defvar tex-start-of-header nil | |
149 | "String used by \\[tex-region] to delimit the start of the file's header.") | |
150 | ||
151 | (defvar tex-end-of-header nil | |
152 | "String used by \\[tex-region] to delimit the end of the file's header.") | |
153 | ||
154 | (defvar tex-shell-cd-command "cd" | |
155 | "Command to give to shell running TeX to change directory. | |
156 | The value of tex-directory will be appended to this, separated by a space.") | |
157 | ||
158 | (defvar tex-zap-file nil | |
159 | "Temporary file name used for text being sent as input to TeX. | |
160 | Should be a simple file name with no extension or directory specification.") | |
161 | ||
162 | (defvar tex-last-buffer-texed nil | |
163 | "Buffer which was last TeXed.") | |
164 | ||
165 | (defvar tex-print-file nil | |
166 | "File name that \\[tex-print] prints. | |
167 | Set by \\[tex-region], \\[tex-buffer], and \\[tex-file].") | |
168 | ||
169 | (defvar tex-mode-syntax-table nil | |
170 | "Syntax table used while in TeX mode.") | |
171 | ||
172 | (defun tex-define-common-keys (keymap) | |
173 | "Define the keys that we want defined both in TeX mode and in the tex-shell." | |
174 | (define-key keymap "\C-c\C-k" 'tex-kill-job) | |
175 | (define-key keymap "\C-c\C-l" 'tex-recenter-output-buffer) | |
176 | (define-key keymap "\C-c\C-q" 'tex-show-print-queue) | |
177 | (define-key keymap "\C-c\C-p" 'tex-print) | |
178 | (define-key keymap "\C-c\C-v" 'tex-view) | |
179 | ) | |
180 | ||
181 | (defvar tex-mode-map nil "Keymap for TeX mode.") | |
182 | ||
183 | (if tex-mode-map | |
184 | nil | |
185 | (setq tex-mode-map (make-sparse-keymap)) | |
186 | (tex-define-common-keys tex-mode-map) | |
187 | (define-key tex-mode-map "\"" 'tex-insert-quote) | |
188 | (define-key tex-mode-map "\n" 'tex-terminate-paragraph) | |
189 | (define-key tex-mode-map "\C-c}" 'up-list) | |
190 | (define-key tex-mode-map "\C-c{" 'tex-insert-braces) | |
191 | (define-key tex-mode-map "\C-c\C-r" 'tex-region) | |
192 | (define-key tex-mode-map "\C-c\C-b" 'tex-buffer) | |
193 | (define-key tex-mode-map "\C-c\C-f" 'tex-file) | |
194 | (define-key tex-mode-map "\C-c\C-i" 'tex-bibtex-file) | |
195 | (define-key tex-mode-map "\C-c\C-o" 'tex-latex-block) | |
196 | (define-key tex-mode-map "\C-c\C-e" 'tex-close-latex-block)) | |
197 | ||
198 | (defvar tex-shell-map nil | |
528415e7 | 199 | "Keymap for the tex-shell. A comint-mode-map with a few additions.") |
869bff31 | 200 | |
201 | ;(fset 'TeX-mode 'tex-mode) ;in loaddefs. | |
202 | ||
203 | ;;; This would be a lot simpler if we just used a regexp search, | |
204 | ;;; but then it would be too slow. | |
7229064d | 205 | ;;;###autoload |
869bff31 | 206 | (defun tex-mode () |
207 | "Major mode for editing files of input for TeX, LaTeX, or SliTeX. | |
208 | Tries to determine (by looking at the beginning of the file) whether | |
209 | this file is for plain TeX, LaTeX, or SliTeX and calls plain-tex-mode, | |
210 | latex-mode, or slitex-mode, respectively. If it cannot be determined, | |
211 | such as if there are no commands in the file, the value of tex-default-mode | |
212 | is used." | |
213 | (interactive) | |
214 | (let (mode slash comment) | |
215 | (save-excursion | |
216 | (goto-char (point-min)) | |
217 | (while (and (setq slash (search-forward "\\" nil t)) | |
218 | (setq comment (let ((search-end (point))) | |
219 | (save-excursion | |
220 | (beginning-of-line) | |
221 | (search-forward "%" search-end t)))))) | |
222 | (if (and slash (not comment)) | |
223 | (setq mode (if (looking-at "documentstyle") | |
224 | (if (looking-at "documentstyle{slides}") | |
225 | 'slitex-mode | |
226 | 'latex-mode) | |
227 | 'plain-tex-mode)))) | |
228 | (if mode (funcall mode) | |
229 | (funcall tex-default-mode)))) | |
6503cec3 JB |
230 | ;;;###autoload |
231 | (fset 'TeX-mode 'tex-mode) | |
232 | ;;;###autoload | |
233 | (fset 'LaTeX-mode 'latex-mode) | |
869bff31 | 234 | |
7229064d | 235 | ;;;###autoload |
869bff31 | 236 | (defun plain-tex-mode () |
237 | "Major mode for editing files of input for plain TeX. | |
238 | Makes $ and } display the characters they match. | |
239 | Makes \" insert `` when it seems to be the beginning of a quotation, | |
240 | and '' when it appears to be the end; it inserts \" only after a \\. | |
241 | ||
242 | Use \\[tex-region] to run TeX on the current region, plus a \"header\" | |
243 | copied from the top of the file (containing macro definitions, etc.), | |
244 | running TeX under a special subshell. \\[tex-buffer] does the whole buffer. | |
245 | \\[tex-file] saves the buffer and then processes the file. | |
246 | \\[tex-print] prints the .dvi file made by any of these. | |
247 | \\[tex-view] previews the .dvi file made by any of these. | |
248 | \\[tex-bibtex-file] runs bibtex on the file of the current buffer. | |
249 | ||
250 | Use \\[validate-tex-buffer] to check buffer for paragraphs containing | |
251 | mismatched $'s or braces. | |
252 | ||
253 | Special commands: | |
254 | \\{tex-mode-map} | |
255 | ||
256 | Mode variables: | |
257 | tex-run-command | |
258 | Command string used by \\[tex-region] or \\[tex-buffer]. | |
259 | tex-directory | |
260 | Directory in which to create temporary files for TeX jobs | |
261 | run by \\[tex-region] or \\[tex-buffer]. | |
262 | tex-dvi-print-command | |
263 | Command string used by \\[tex-print] to print a .dvi file. | |
528415e7 RS |
264 | tex-alt-dvi-print-command |
265 | Alternative command string used by \\[tex-print] (when given a prefix | |
266 | argument) to print a .dvi file. | |
869bff31 | 267 | tex-dvi-view-command |
268 | Command string used by \\[tex-view] to preview a .dvi file. | |
269 | tex-show-queue-command | |
270 | Command string used by \\[tex-show-print-queue] to show the print | |
271 | queue that \\[tex-print] put your job on. | |
272 | ||
273 | Entering Plain-tex mode calls the value of text-mode-hook, then the value of | |
274 | tex-mode-hook, and then the value of plain-tex-mode-hook. When the special | |
275 | subshell is initiated, the value of tex-shell-hook is called." | |
276 | (interactive) | |
277 | (tex-common-initialization) | |
278 | (setq mode-name "TeX") | |
279 | (setq major-mode 'plain-tex-mode) | |
280 | (setq tex-command tex-run-command) | |
281 | (setq tex-start-of-header "%**start of header") | |
282 | (setq tex-end-of-header "%**end of header") | |
283 | (setq tex-trailer "\\bye\n") | |
284 | (run-hooks 'text-mode-hook 'tex-mode-hook 'plain-tex-mode-hook)) | |
6503cec3 JB |
285 | ;;;###autoload |
286 | (fset 'plain-TeX-mode 'plain-tex-mode) | |
869bff31 | 287 | |
7229064d | 288 | ;;;###autoload |
869bff31 | 289 | (defun latex-mode () |
290 | "Major mode for editing files of input for LaTeX. | |
291 | Makes $ and } display the characters they match. | |
292 | Makes \" insert `` when it seems to be the beginning of a quotation, | |
293 | and '' when it appears to be the end; it inserts \" only after a \\. | |
294 | ||
295 | Use \\[tex-region] to run LaTeX on the current region, plus the preamble | |
296 | copied from the top of the file (containing \\documentstyle, etc.), | |
297 | running LaTeX under a special subshell. \\[tex-buffer] does the whole buffer. | |
298 | \\[tex-file] saves the buffer and then processes the file. | |
299 | \\[tex-print] prints the .dvi file made by any of these. | |
300 | \\[tex-view] previews the .dvi file made by any of these. | |
301 | \\[tex-bibtex-file] runs bibtex on the file of the current buffer. | |
302 | ||
303 | Use \\[validate-tex-buffer] to check buffer for paragraphs containing | |
304 | mismatched $'s or braces. | |
305 | ||
306 | Special commands: | |
307 | \\{tex-mode-map} | |
308 | ||
309 | Mode variables: | |
310 | latex-run-command | |
311 | Command string used by \\[tex-region] or \\[tex-buffer]. | |
312 | tex-directory | |
313 | Directory in which to create temporary files for LaTeX jobs | |
314 | run by \\[tex-region] or \\[tex-buffer]. | |
315 | tex-dvi-print-command | |
316 | Command string used by \\[tex-print] to print a .dvi file. | |
528415e7 RS |
317 | tex-alt-dvi-print-command |
318 | Alternative command string used by \\[tex-print] (when given a prefix | |
319 | argument) to print a .dvi file. | |
869bff31 | 320 | tex-dvi-view-command |
321 | Command string used by \\[tex-view] to preview a .dvi file. | |
322 | tex-show-queue-command | |
323 | Command string used by \\[tex-show-print-queue] to show the print | |
324 | queue that \\[tex-print] put your job on. | |
325 | ||
326 | Entering Latex mode calls the value of text-mode-hook, then the value of | |
327 | tex-mode-hook, and then the value of latex-mode-hook. When the special | |
328 | subshell is initiated, the value of tex-shell-hook is called." | |
329 | (interactive) | |
330 | (tex-common-initialization) | |
331 | (setq mode-name "LaTeX") | |
332 | (setq major-mode 'latex-mode) | |
333 | (setq tex-command latex-run-command) | |
334 | (setq tex-start-of-header "\\documentstyle") | |
335 | (setq tex-end-of-header "\\begin{document}") | |
336 | (setq tex-trailer "\\end{document}\n") | |
337 | (run-hooks 'text-mode-hook 'tex-mode-hook 'latex-mode-hook)) | |
338 | ||
339 | (defun slitex-mode () | |
340 | "Major mode for editing files of input for SliTeX. | |
341 | Makes $ and } display the characters they match. | |
342 | Makes \" insert `` when it seems to be the beginning of a quotation, | |
343 | and '' when it appears to be the end; it inserts \" only after a \\. | |
344 | ||
345 | Use \\[tex-region] to run SliTeX on the current region, plus the preamble | |
346 | copied from the top of the file (containing \\documentstyle, etc.), | |
347 | running SliTeX under a special subshell. \\[tex-buffer] does the whole buffer. | |
348 | \\[tex-file] saves the buffer and then processes the file. | |
349 | \\[tex-print] prints the .dvi file made by any of these. | |
350 | \\[tex-view] previews the .dvi file made by any of these. | |
351 | \\[tex-bibtex-file] runs bibtex on the file of the current buffer. | |
352 | ||
353 | Use \\[validate-tex-buffer] to check buffer for paragraphs containing | |
354 | mismatched $'s or braces. | |
355 | ||
356 | Special commands: | |
357 | \\{tex-mode-map} | |
358 | ||
359 | Mode variables: | |
360 | slitex-run-command | |
361 | Command string used by \\[tex-region] or \\[tex-buffer]. | |
362 | tex-directory | |
363 | Directory in which to create temporary files for SliTeX jobs | |
364 | run by \\[tex-region] or \\[tex-buffer]. | |
365 | tex-dvi-print-command | |
366 | Command string used by \\[tex-print] to print a .dvi file. | |
528415e7 RS |
367 | tex-alt-dvi-print-command |
368 | Alternative command string used by \\[tex-print] (when given a prefix | |
369 | argument) to print a .dvi file. | |
869bff31 | 370 | tex-dvi-view-command |
371 | Command string used by \\[tex-view] to preview a .dvi file. | |
372 | tex-show-queue-command | |
373 | Command string used by \\[tex-show-print-queue] to show the print | |
374 | queue that \\[tex-print] put your job on. | |
375 | ||
528415e7 | 376 | Entering SliTeX mode calls the value of text-mode-hook, then the value of |
869bff31 | 377 | tex-mode-hook, then the value of latex-mode-hook, and then the value of |
378 | slitex-mode-hook. When the special subshell is initiated, the value of | |
379 | tex-shell-hook is called." | |
380 | (interactive) | |
381 | (tex-common-initialization) | |
382 | (setq mode-name "SliTeX") | |
383 | (setq major-mode 'slitex-mode) | |
384 | (setq tex-command slitex-run-command) | |
385 | (setq tex-start-of-header "\\documentstyle{slides}") | |
386 | (setq tex-end-of-header "\\begin{document}") | |
387 | (setq tex-trailer "\\end{document}\n") | |
388 | (run-hooks | |
389 | 'text-mode-hook 'tex-mode-hook 'latex-mode-hook 'slitex-mode-hook)) | |
390 | ||
391 | (defun tex-common-initialization () | |
392 | (kill-all-local-variables) | |
393 | (use-local-map tex-mode-map) | |
394 | (setq local-abbrev-table text-mode-abbrev-table) | |
395 | (if (null tex-mode-syntax-table) | |
396 | (let ((char 0)) | |
397 | (setq tex-mode-syntax-table (make-syntax-table)) | |
398 | (set-syntax-table tex-mode-syntax-table) | |
399 | (while (< char ? ) | |
400 | (modify-syntax-entry char ".") | |
401 | (setq char (1+ char))) | |
402 | (modify-syntax-entry ?\C-@ "w") | |
403 | (modify-syntax-entry ?\t " ") | |
404 | (modify-syntax-entry ?\n ">") | |
405 | (modify-syntax-entry ?\f ">") | |
406 | (modify-syntax-entry ?$ "$$") | |
407 | (modify-syntax-entry ?% "<") | |
408 | (modify-syntax-entry ?\\ "/") | |
409 | (modify-syntax-entry ?\" ".") | |
410 | (modify-syntax-entry ?& ".") | |
411 | (modify-syntax-entry ?_ ".") | |
412 | (modify-syntax-entry ?@ "_") | |
413 | (modify-syntax-entry ?~ " ") | |
414 | (modify-syntax-entry ?' "w")) | |
415 | (set-syntax-table tex-mode-syntax-table)) | |
416 | (make-local-variable 'paragraph-start) | |
417 | (setq paragraph-start "^[ \t]*$\\|^[\f\\\\%]") | |
418 | (make-local-variable 'paragraph-separate) | |
419 | (setq paragraph-separate paragraph-start) | |
420 | (make-local-variable 'comment-start) | |
421 | (setq comment-start "%") | |
422 | (make-local-variable 'comment-start-skip) | |
423 | (setq comment-start-skip "\\(\\(^\\|[^\\]\\)\\(\\\\\\\\\\)*\\)\\(%+ *\\)") | |
424 | (make-local-variable 'comment-indent-hook) | |
425 | (setq comment-indent-hook 'tex-comment-indent) | |
426 | (make-local-variable 'compare-windows-whitespace) | |
427 | (setq compare-windows-whitespace 'tex-categorize-whitespace) | |
428 | (make-local-variable 'tex-command) | |
429 | (make-local-variable 'tex-start-of-header) | |
430 | (make-local-variable 'tex-end-of-header) | |
431 | (make-local-variable 'tex-trailer)) | |
432 | ||
433 | (defun tex-comment-indent () | |
434 | (if (looking-at "%%%") | |
435 | (current-column) | |
436 | (skip-chars-backward " \t") | |
437 | (max (if (bolp) 0 (1+ (current-column))) | |
438 | comment-column))) | |
439 | ||
440 | (defun tex-categorize-whitespace (backward-limit) | |
441 | ;; compare-windows-whitespace is set to this. | |
442 | ;; This is basically a finite-state machine. | |
443 | ;; Returns a symbol telling how TeX would treat | |
444 | ;; the whitespace we are looking at: null, space, or par. | |
445 | (let ((category 'null) | |
446 | (not-finished t)) | |
447 | (skip-chars-backward " \t\n\f" backward-limit) | |
448 | (while not-finished | |
449 | (cond ((looking-at "[ \t]+") | |
450 | (goto-char (match-end 0)) | |
451 | (if (eql category 'null) | |
452 | (setq category 'space))) | |
453 | ((looking-at "\n") | |
454 | (cond ((eql category 'newline) | |
455 | (setq category 'par) | |
456 | (setq not-finished nil)) | |
457 | (t | |
458 | (setq category 'newline) ;a strictly internal state | |
459 | (goto-char (match-end 0))))) | |
460 | ((looking-at "\f+") | |
461 | (setq category 'par) | |
462 | (setq not-finished nil)) | |
463 | (t | |
464 | (setq not-finished nil)))) | |
465 | (skip-chars-forward " \t\n\f") | |
466 | (if (eql category 'newline) | |
467 | 'space ;TeX doesn't distinguish | |
468 | category))) | |
469 | ||
470 | (defun tex-insert-quote (arg) | |
471 | "Insert the appropriate quote marks for TeX. | |
472 | Inserts the value of tex-open-quote (normally ``) or tex-close-quote | |
473 | (normally '') depending on the context. With prefix argument, always | |
474 | inserts \" characters." | |
528415e7 | 475 | (interactive "*P") |
869bff31 | 476 | (if arg |
477 | (self-insert-command (prefix-numeric-value arg)) | |
478 | (insert | |
479 | (cond ((or (bobp) | |
480 | (save-excursion | |
481 | (forward-char -1) | |
482 | (looking-at "\\s(\\|\\s \\|\\s>"))) | |
483 | tex-open-quote) | |
484 | ((= (preceding-char) ?\\) | |
485 | ?\") | |
486 | (t | |
487 | tex-close-quote))))) | |
488 | ||
489 | (defun validate-tex-buffer () | |
490 | "Check current buffer for paragraphs containing mismatched $'s. | |
491 | As each such paragraph is found, a mark is pushed at its beginning, | |
492 | and the location is displayed for a few seconds." | |
493 | (interactive) | |
494 | (let ((opoint (point))) | |
495 | (goto-char (point-max)) | |
496 | ;; Does not use save-excursion | |
497 | ;; because we do not want to save the mark. | |
498 | (unwind-protect | |
499 | (while (and (not (input-pending-p)) (not (bobp))) | |
500 | (let ((end (point))) | |
501 | (search-backward "\n\n" nil 'move) | |
502 | (or (tex-validate-region (point) end) | |
503 | (progn | |
504 | (push-mark (point)) | |
505 | (message "Mismatch found in pararaph starting here") | |
506 | (sit-for 4))))) | |
507 | (goto-char opoint)))) | |
508 | ||
509 | (defun tex-validate-region (start end) | |
510 | "Check for mismatched braces or $'s in region. | |
511 | Returns t if no mismatches. Returns nil and moves point to suspect | |
512 | area if a mismatch is found." | |
513 | (interactive "r") | |
514 | (let ((failure-point nil) (max-possible-sexps (- end start))) | |
515 | (save-excursion | |
516 | (condition-case () | |
517 | (save-restriction | |
518 | (narrow-to-region start end) | |
519 | (goto-char start) | |
520 | (while (< 0 (setq max-possible-sexps (1- max-possible-sexps))) | |
521 | (forward-sexp 1))) | |
522 | (error | |
523 | (setq failure-point (point))))) | |
524 | (if failure-point | |
525 | (progn | |
526 | (goto-char failure-point) | |
527 | nil) | |
528 | t))) | |
529 | ||
530 | (defun tex-terminate-paragraph (inhibit-validation) | |
531 | "Insert two newlines, breaking a paragraph for TeX. | |
532 | Check for mismatched braces/$'s in paragraph being terminated. | |
533 | A prefix arg inhibits the checking." | |
528415e7 | 534 | (interactive "*P") |
869bff31 | 535 | (or inhibit-validation |
536 | (save-excursion | |
537 | (tex-validate-region | |
538 | (save-excursion | |
539 | (search-backward "\n\n" nil 'move) | |
540 | (point)) | |
541 | (point))) | |
542 | (message "Paragraph being closed appears to contain a mismatch")) | |
543 | (insert "\n\n")) | |
544 | ||
545 | (defun tex-insert-braces () | |
546 | "Make a pair of braces and be poised to type inside of them." | |
528415e7 | 547 | (interactive "*") |
869bff31 | 548 | (insert ?\{) |
549 | (save-excursion | |
550 | (insert ?}))) | |
551 | ||
552 | ;;; Like tex-insert-braces, but for LaTeX. | |
553 | (defun tex-latex-block (name) | |
554 | "Creates a matching pair of lines \\begin{NAME} and \\end{NAME} at point. | |
555 | Puts point on a blank line between them." | |
528415e7 RS |
556 | (interactive |
557 | (prog2 | |
558 | (barf-if-buffer-read-only) | |
559 | (list | |
560 | (completing-read "LaTeX block name: " | |
561 | (mapcar 'list | |
562 | (append standard-latex-block-names | |
563 | latex-block-names)))))) | |
869bff31 | 564 | (let ((col (current-column))) |
565 | (insert (format "\\begin{%s}\n" name)) | |
566 | (indent-to col) | |
567 | (save-excursion | |
568 | (insert ?\n) | |
569 | (indent-to col) | |
570 | (insert-string (format "\\end{%s}" name)) | |
571 | (if (eobp) (insert ?\n))))) | |
572 | ||
573 | (defun tex-last-unended-begin () | |
574 | "Leave point at the beginning of the last \\begin{...} that is unended." | |
575 | (while (and (re-search-backward "\\(\\\\begin\\s *{\\)\\|\\(\\\\end\\s *{\\)") | |
576 | (looking-at "\\\\end{")) | |
577 | (tex-last-unended-begin))) | |
578 | ||
579 | (defun tex-close-latex-block () | |
580 | "Creates an \\end{...} to match the last unclosed \\begin{...}." | |
581 | (interactive "*") | |
582 | (let ((new-line-needed (bolp)) | |
583 | text indentation) | |
584 | (save-excursion | |
528415e7 | 585 | (condition-case nil |
869bff31 | 586 | (tex-last-unended-begin) |
587 | (error (error "Couldn't find unended \\begin"))) | |
588 | (setq indentation (current-column)) | |
589 | (re-search-forward "\\\\begin\\(\\s *{[^}\n]*}\\)") | |
590 | (setq text (buffer-substring (match-beginning 1) (match-end 1)))) | |
591 | (indent-to indentation) | |
592 | (insert "\\end" text) | |
593 | (if new-line-needed (insert ?\n)))) | |
594 | \f | |
595 | ;;; Invoking TeX in an inferior shell. | |
596 | ||
597 | ;;; Why use a shell instead of running TeX directly? Because if TeX | |
598 | ;;; gets stuck, the user can switch to the shell window and type at it. | |
599 | ||
600 | ;;; The utility functions: | |
601 | ||
602 | (defun tex-start-shell () | |
603 | (save-excursion | |
528415e7 RS |
604 | (set-buffer |
605 | (make-comint | |
606 | "tex-shell" | |
607 | (or tex-shell-file-name (getenv "ESHELL") (getenv "SHELL") "/bin/sh") | |
608 | nil "-v")) | |
609 | (let ((proc (get-process "tex-shell"))) | |
610 | (set-process-sentinel proc 'tex-shell-sentinel) | |
611 | (process-kill-without-query proc) | |
612 | (setq tex-shell-map (copy-keymap comint-mode-map)) | |
613 | (tex-define-common-keys tex-shell-map) | |
614 | (use-local-map tex-shell-map) | |
615 | (run-hooks 'tex-shell-hook) | |
616 | (while (zerop (buffer-size)) | |
617 | (sleep-for 1))))) | |
618 | ||
619 | (defun tex-shell-sentinel (proc msg) | |
620 | (cond ((null (buffer-name (process-buffer proc))) | |
621 | ;; buffer killed | |
622 | (set-process-buffer proc nil) | |
623 | (tex-delete-last-temp-files)) | |
624 | ((memq (process-status proc) '(signal exit)) | |
625 | (tex-delete-last-temp-files)))) | |
626 | ||
627 | (defun tex-set-buffer-directory (buffer directory) | |
869bff31 | 628 | "Set BUFFER's default directory to be DIRECTORY." |
629 | (setq directory (file-name-as-directory (expand-file-name directory))) | |
630 | (if (not (file-directory-p directory)) | |
631 | (error "%s is not a directory" directory) | |
632 | (save-excursion | |
633 | (set-buffer buffer) | |
634 | (setq default-directory directory)))) | |
635 | ||
528415e7 RS |
636 | (defun tex-send-command (command &optional file background) |
637 | "Send COMMAND to tex-shell, substituting optional FILE for *; in background | |
638 | if optional BACKGROUND is t. If COMMAND has no *, FILE will be appended, | |
639 | preceded by a blank, to COMMAND. If FILE is nil, no substitution will be made | |
640 | in COMMAND. COMMAND can be any expression that evaluates to a command string." | |
641 | (save-excursion | |
642 | (let* ((cmd (eval command)) | |
643 | (star (string-match "\\*" cmd))) | |
644 | (comint-proc-query (get-process "tex-shell") | |
645 | (concat (substring cmd 0 star) | |
646 | (if file (concat " " file) "") | |
647 | (if star (substring cmd (1+ star) nil) "") | |
648 | (if background "&\n" "\n")))))) | |
649 | ||
650 | (defun tex-delete-last-temp-files () | |
651 | "Delete any junk files from last temp file." | |
652 | (if tex-last-temp-file | |
653 | (let* ((dir (file-name-directory tex-last-temp-file)) | |
654 | (list (file-name-all-completions | |
655 | (file-name-nondirectory tex-last-temp-file) dir))) | |
656 | (while list | |
657 | (delete-file (concat dir (car list))) | |
658 | (setq list (cdr list)))))) | |
659 | ||
99621a14 | 660 | (add-hook 'kill-emacs-hook 'tex-delete-last-temp-files) |
869bff31 | 661 | |
528415e7 | 662 | ;;; The commands: |
869bff31 | 663 | |
664 | (defun tex-region (beg end) | |
665 | "Run TeX on the current region, via a temporary file. | |
666 | The file's name comes from the variable `tex-zap-file' and the | |
667 | variable `tex-directory' says where to put it. | |
668 | ||
669 | If the buffer has a header, the header is given to TeX before the | |
670 | region itself. The buffer's header is all lines between the strings | |
671 | defined by `tex-start-of-header' and `tex-end-of-header' inclusive. | |
672 | The header must start in the first 100 lines of the buffer. | |
673 | ||
674 | The value of `tex-trailer' is given to TeX as input after the region. | |
675 | ||
676 | The value of `tex-command' specifies the command to use to run TeX." | |
677 | (interactive "r") | |
678 | (if (tex-shell-running) | |
679 | (tex-kill-job) | |
680 | (tex-start-shell)) | |
681 | (or tex-zap-file | |
682 | (setq tex-zap-file (tex-generate-zap-file-name))) | |
528415e7 RS |
683 | (let* ((temp-buffer (get-buffer-create " TeX-Output-Buffer")) |
684 | ; Temp file will be written and TeX will be run in zap-directory. | |
685 | ; If the TEXINPUTS file has relative directories or if the region has | |
686 | ; \input of files, this must be the same directory as the file for | |
687 | ; TeX to access the correct inputs. That's why it's safest if | |
688 | ; tex-directory is ".". | |
689 | (zap-directory | |
690 | (file-name-as-directory (expand-file-name tex-directory))) | |
691 | (tex-out-file (concat zap-directory tex-zap-file))) | |
692 | (tex-delete-last-temp-files) | |
869bff31 | 693 | ;; Write the new temp file. |
694 | (save-excursion | |
695 | (save-restriction | |
696 | (widen) | |
697 | (goto-char (point-min)) | |
698 | (forward-line 100) | |
699 | (let ((search-end (point)) | |
700 | (hbeg (point-min)) (hend (point-min)) | |
701 | (default-directory zap-directory)) | |
702 | (goto-char (point-min)) | |
703 | ;; Initialize the temp file with either the header or nothing | |
704 | (if (search-forward tex-start-of-header search-end t) | |
705 | (progn | |
706 | (beginning-of-line) | |
707 | (setq hbeg (point)) ;mark beginning of header | |
708 | (if (search-forward tex-end-of-header nil t) | |
709 | (progn (forward-line 1) | |
710 | (setq hend (point))) ;mark end of header | |
711 | (setq hbeg (point-min))))) ;no header | |
528415e7 RS |
712 | (write-region (min hbeg beg) hend |
713 | (concat tex-out-file ".tex") nil nil) | |
714 | (write-region (max beg hend) end (concat tex-out-file ".tex") t nil)) | |
869bff31 | 715 | (let ((local-tex-trailer tex-trailer)) |
716 | (set-buffer temp-buffer) | |
717 | (erase-buffer) | |
718 | ;; make sure trailer isn't hidden by a comment | |
719 | (insert-string "\n") | |
720 | (if local-tex-trailer (insert-string local-tex-trailer)) | |
528415e7 RS |
721 | (tex-set-buffer-directory temp-buffer zap-directory) |
722 | (write-region (point-min) (point-max) | |
723 | (concat tex-out-file ".tex") t nil)))) | |
724 | ;; Record the file name to be deleted afterward. | |
725 | (setq tex-last-temp-file tex-out-file) | |
726 | (tex-send-command tex-shell-cd-command zap-directory) | |
727 | (tex-send-command tex-command tex-out-file) | |
728 | (setq tex-print-file tex-out-file) | |
729 | (setq tex-last-buffer-texed (current-buffer)))) | |
869bff31 | 730 | |
731 | (defun tex-buffer () | |
732 | "Run TeX on current buffer. See \\[tex-region] for more information. | |
528415e7 RS |
733 | Does not save the buffer, so it's useful for trying experimental versions. |
734 | See \\[tex-file] for an alternative." | |
869bff31 | 735 | (interactive) |
736 | (tex-region (point-min) (point-max))) | |
737 | ||
738 | (defun tex-file () | |
739 | "Prompt to save all buffers and run TeX (or LaTeX) on current buffer's file. | |
740 | This function is more useful than \\[tex-buffer] when you need the | |
741 | `.aux' file of LaTeX to have the correct name." | |
742 | (interactive) | |
743 | (let ((tex-out-file | |
744 | (if (buffer-file-name) | |
745 | (file-name-nondirectory (buffer-file-name)) | |
746 | (error "Buffer does not seem to be associated with any file"))) | |
747 | (file-dir (file-name-directory (buffer-file-name)))) | |
7047ec77 | 748 | (if tex-offer-save |
99621a14 | 749 | (save-some-buffers)) |
869bff31 | 750 | (if (tex-shell-running) |
751 | (tex-kill-job) | |
752 | (tex-start-shell)) | |
528415e7 RS |
753 | (tex-send-command tex-shell-cd-command file-dir) |
754 | (tex-send-command tex-command tex-out-file)) | |
869bff31 | 755 | (setq tex-last-buffer-texed (current-buffer)) |
528415e7 | 756 | (setq tex-print-file (buffer-file-name))) |
869bff31 | 757 | |
758 | (defun tex-generate-zap-file-name () | |
759 | "Generate a unique name suitable for use as a file name." | |
760 | ;; Include the shell process number and host name | |
761 | ;; in case there are multiple shells (for same or different user). | |
762 | (format "#tz%d%s" | |
763 | (process-id (get-buffer-process "*tex-shell*")) | |
764 | (tex-strip-dots (system-name)))) | |
765 | ||
766 | (defun tex-strip-dots (s) | |
767 | (setq s (copy-sequence s)) | |
768 | (while (string-match "\\." s) | |
769 | (aset s (match-beginning 0) ?-)) | |
770 | s) | |
771 | ||
772 | ;; This will perhaps be useful for modifying TEXINPUTS. | |
773 | ;; Expand each file name, separated by colons, in the string S. | |
774 | (defun tex-expand-files (s) | |
775 | (let (elts (start 0)) | |
776 | (while (string-match ":" s start) | |
777 | (setq elts (cons (substring s start (match-beginning 0)) elts)) | |
778 | (setq start (match-end 0))) | |
779 | (or (= start 0) | |
780 | (setq elts (cons (substring s start) elts))) | |
781 | (mapconcat 'expand-file-name (nreverse elts) ":"))) | |
782 | ||
783 | (defun tex-shell-running () | |
784 | (and (get-process "tex-shell") | |
785 | (eq (process-status (get-process "tex-shell")) 'run))) | |
786 | ||
787 | (defun tex-kill-job () | |
788 | "Kill the currently running TeX job." | |
789 | (interactive) | |
528415e7 | 790 | (quit-process (get-process "tex-shell") t)) |
869bff31 | 791 | |
792 | (defun tex-recenter-output-buffer (linenum) | |
793 | "Redisplay buffer of TeX job output so that most recent output can be seen. | |
794 | The last line of the buffer is displayed on | |
795 | line LINE of the window, or centered if LINE is nil." | |
796 | (interactive "P") | |
797 | (let ((tex-shell (get-buffer "*tex-shell*")) | |
798 | (old-buffer (current-buffer))) | |
799 | (if (null tex-shell) | |
800 | (message "No TeX output buffer") | |
801 | (pop-to-buffer tex-shell) | |
802 | (bury-buffer tex-shell) | |
803 | (goto-char (point-max)) | |
804 | (recenter (if linenum | |
805 | (prefix-numeric-value linenum) | |
806 | (/ (window-height) 2))) | |
528415e7 | 807 | (pop-to-buffer old-buffer)))) |
869bff31 | 808 | |
528415e7 | 809 | (defun tex-print (&optional alt) |
869bff31 | 810 | "Print the .dvi file made by \\[tex-region], \\[tex-buffer] or \\[tex-file]. |
528415e7 RS |
811 | Runs the shell command defined by tex-dvi-print-command. If prefix argument |
812 | is provided, use the alternative command, tex-alt-dvi-print-command." | |
813 | (interactive "P") | |
869bff31 | 814 | (let ((print-file-name-dvi (tex-append tex-print-file ".dvi")) |
815 | test-name) | |
816 | (if (and (not (equal (current-buffer) tex-last-buffer-texed)) | |
817 | (file-newer-than-file-p | |
818 | (setq test-name (tex-append (buffer-file-name) ".dvi")) | |
528415e7 | 819 | print-file-name-dvi)) |
869bff31 | 820 | (setq print-file-name-dvi test-name)) |
528415e7 RS |
821 | (if (not (file-exists-p print-file-name-dvi)) |
822 | (error "No appropriate `.dvi' file could be found") | |
823 | (tex-send-command | |
824 | (if alt tex-alt-dvi-print-command tex-dvi-print-command) | |
825 | print-file-name-dvi t)))) | |
869bff31 | 826 | |
827 | (defun tex-view () | |
828 | "Preview the last `.dvi' file made by running TeX under Emacs. | |
829 | This means, made using \\[tex-region], \\[tex-buffer] or \\[tex-file]. | |
830 | The variable `tex-dvi-view-command' specifies the shell command for preview." | |
831 | (interactive) | |
832 | (let ((tex-dvi-print-command tex-dvi-view-command)) | |
833 | (tex-print))) | |
834 | ||
835 | (defun tex-append (file-name suffix) | |
836 | "Append to FILENAME the suffix SUFFIX, using same algorithm TeX uses. | |
837 | Scans for the first (not last) period. | |
838 | No period is retained immediately before SUFFIX, | |
839 | so normally SUFFIX starts with one." | |
840 | (if (stringp file-name) | |
841 | (let ((file (file-name-nondirectory file-name))) | |
842 | (concat (file-name-directory file-name) | |
843 | (substring file 0 | |
844 | (string-match "\\." file)) | |
845 | suffix)) | |
846 | " ")) | |
847 | ||
848 | (defun tex-show-print-queue () | |
849 | "Show the print queue that \\[tex-print] put your job on. | |
850 | Runs the shell command defined by tex-show-queue-command." | |
851 | (interactive) | |
852 | (if (tex-shell-running) | |
853 | (tex-kill-job) | |
854 | (tex-start-shell)) | |
528415e7 | 855 | (tex-send-command tex-show-queue-command)) |
869bff31 | 856 | |
857 | (defun tex-bibtex-file () | |
858 | "Run BibTeX on the current buffer's file." | |
859 | (interactive) | |
860 | (if (tex-shell-running) | |
861 | (tex-kill-job) | |
862 | (tex-start-shell)) | |
863 | (let ((tex-out-file | |
864 | (tex-append (file-name-nondirectory (buffer-file-name)) "")) | |
865 | (file-dir (file-name-directory (buffer-file-name)))) | |
528415e7 RS |
866 | (tex-send-command tex-shell-cd-command file-dir) |
867 | (tex-send-command bibtex-command tex-out-file))) | |
868 | ||
869 | (run-hooks 'tex-mode-load-hook) | |
869bff31 | 870 | |
49116ac0 JB |
871 | (provide 'tex-mode) |
872 | ||
d501f516 | 873 | ;;; tex-mode.el ends here |
99621a14 | 874 |