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