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