annotate lisp/textmodes/tex-mode.el @ 430:62d4447676c1

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