annotate lisp/textmodes/tex-mode.el @ 85:253ec6b277ee

Initial revision
author root <root>
date Tue, 28 Aug 1990 08:01:56 +0000
parents
children 7e4c7ef44243
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.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
143 (defun tex-mode ()
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
144 "Major mode for editing files of input for TeX, LaTeX, or SliTeX.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
145 Tries to determine (by looking at the beginning of the file) whether
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
146 this file is for plain TeX, LaTeX, or SliTeX and calls plain-tex-mode,
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
147 latex-mode, or slitex-mode, respectively. If it cannot be determined,
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
148 such as if there are no commands in the file, the value of tex-default-mode
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
149 is used."
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
150 (interactive)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
151 (let (mode slash comment)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
152 (save-excursion
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
153 (goto-char (point-min))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
154 (while (and (setq slash (search-forward "\\" nil t))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
155 (setq comment (let ((search-end (point)))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
156 (save-excursion
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
157 (beginning-of-line)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
158 (search-forward "%" search-end t))))))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
159 (if (and slash (not comment))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
160 (setq mode (if (looking-at "documentstyle")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
161 (if (looking-at "documentstyle{slides}")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
162 'slitex-mode
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
163 'latex-mode)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
164 'plain-tex-mode))))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
165 (if mode (funcall mode)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
166 (funcall tex-default-mode))))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
167
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
168 (fset 'plain-TeX-mode 'plain-tex-mode)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
169 (fset 'LaTeX-mode 'latex-mode)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
170
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
171 (defun plain-tex-mode ()
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
172 "Major mode for editing files of input for plain TeX.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
173 Makes $ and } display the characters they match.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
174 Makes \" insert `` when it seems to be the beginning of a quotation,
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
175 and '' when it appears to be the end; it inserts \" only after a \\.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
176
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
177 Use \\[tex-region] to run TeX on the current region, plus a \"header\"
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
178 copied from the top of the file (containing macro definitions, etc.),
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
179 running TeX under a special subshell. \\[tex-buffer] does the whole buffer.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
180 \\[tex-file] saves the buffer and then processes the file.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
181 \\[tex-print] prints the .dvi file made by any of these.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
182 \\[tex-view] previews the .dvi file made by any of these.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
183 \\[tex-bibtex-file] runs bibtex on the file of the current buffer.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
184
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
185 Use \\[validate-tex-buffer] to check buffer for paragraphs containing
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
186 mismatched $'s or braces.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
187
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
188 Special commands:
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
189 \\{tex-mode-map}
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
190
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
191 Mode variables:
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
192 tex-run-command
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
193 Command string used by \\[tex-region] or \\[tex-buffer].
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
194 tex-directory
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
195 Directory in which to create temporary files for TeX jobs
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
196 run by \\[tex-region] or \\[tex-buffer].
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
197 tex-dvi-print-command
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
198 Command string used by \\[tex-print] to print a .dvi file.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
199 tex-dvi-view-command
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
200 Command string used by \\[tex-view] to preview a .dvi file.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
201 tex-show-queue-command
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
202 Command string used by \\[tex-show-print-queue] to show the print
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
203 queue that \\[tex-print] put your job on.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
204
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
205 Entering Plain-tex mode calls the value of text-mode-hook, then the value of
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
206 tex-mode-hook, and then the value of plain-tex-mode-hook. When the special
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
207 subshell is initiated, the value of tex-shell-hook is called."
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
208 (interactive)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
209 (tex-common-initialization)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
210 (setq mode-name "TeX")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
211 (setq major-mode 'plain-tex-mode)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
212 (setq tex-command tex-run-command)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
213 (setq tex-start-of-header "%**start of header")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
214 (setq tex-end-of-header "%**end of header")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
215 (setq tex-trailer "\\bye\n")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
216 (run-hooks 'text-mode-hook 'tex-mode-hook 'plain-tex-mode-hook))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
217
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
218 (defun latex-mode ()
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
219 "Major mode for editing files of input for LaTeX.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
220 Makes $ and } display the characters they match.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
221 Makes \" insert `` when it seems to be the beginning of a quotation,
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
222 and '' when it appears to be the end; it inserts \" only after a \\.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
223
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
224 Use \\[tex-region] to run LaTeX on the current region, plus the preamble
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
225 copied from the top of the file (containing \\documentstyle, etc.),
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
226 running LaTeX under a special subshell. \\[tex-buffer] does the whole buffer.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
227 \\[tex-file] saves the buffer and then processes the file.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
228 \\[tex-print] prints the .dvi file made by any of these.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
229 \\[tex-view] previews the .dvi file made by any of these.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
230 \\[tex-bibtex-file] runs bibtex on the file of the current buffer.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
231
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
232 Use \\[validate-tex-buffer] to check buffer for paragraphs containing
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
233 mismatched $'s or braces.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
234
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
235 Special commands:
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
236 \\{tex-mode-map}
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
237
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
238 Mode variables:
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
239 latex-run-command
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
240 Command string used by \\[tex-region] or \\[tex-buffer].
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
241 tex-directory
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
242 Directory in which to create temporary files for LaTeX jobs
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
243 run by \\[tex-region] or \\[tex-buffer].
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
244 tex-dvi-print-command
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
245 Command string used by \\[tex-print] to print a .dvi file.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
246 tex-dvi-view-command
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
247 Command string used by \\[tex-view] to preview a .dvi file.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
248 tex-show-queue-command
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
249 Command string used by \\[tex-show-print-queue] to show the print
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
250 queue that \\[tex-print] put your job on.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
251
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
252 Entering Latex mode calls the value of text-mode-hook, then the value of
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
253 tex-mode-hook, and then the value of latex-mode-hook. When the special
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
254 subshell is initiated, the value of tex-shell-hook is called."
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
255 (interactive)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
256 (tex-common-initialization)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
257 (setq mode-name "LaTeX")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
258 (setq major-mode 'latex-mode)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
259 (setq tex-command latex-run-command)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
260 (setq tex-start-of-header "\\documentstyle")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
261 (setq tex-end-of-header "\\begin{document}")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
262 (setq tex-trailer "\\end{document}\n")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
263 (run-hooks 'text-mode-hook 'tex-mode-hook 'latex-mode-hook))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
264
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
265 (defun slitex-mode ()
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
266 "Major mode for editing files of input for SliTeX.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
267 Makes $ and } display the characters they match.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
268 Makes \" insert `` when it seems to be the beginning of a quotation,
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
269 and '' when it appears to be the end; it inserts \" only after a \\.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
270
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
271 Use \\[tex-region] to run SliTeX on the current region, plus the preamble
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
272 copied from the top of the file (containing \\documentstyle, etc.),
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
273 running SliTeX under a special subshell. \\[tex-buffer] does the whole buffer.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
274 \\[tex-file] saves the buffer and then processes the file.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
275 \\[tex-print] prints the .dvi file made by any of these.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
276 \\[tex-view] previews the .dvi file made by any of these.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
277 \\[tex-bibtex-file] runs bibtex on the file of the current buffer.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
278
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
279 Use \\[validate-tex-buffer] to check buffer for paragraphs containing
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
280 mismatched $'s or braces.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
281
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
282 Special commands:
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
283 \\{tex-mode-map}
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
284
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
285 Mode variables:
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
286 slitex-run-command
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
287 Command string used by \\[tex-region] or \\[tex-buffer].
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
288 tex-directory
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
289 Directory in which to create temporary files for SliTeX jobs
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
290 run by \\[tex-region] or \\[tex-buffer].
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
291 tex-dvi-print-command
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
292 Command string used by \\[tex-print] to print a .dvi file.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
293 tex-dvi-view-command
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
294 Command string used by \\[tex-view] to preview a .dvi file.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
295 tex-show-queue-command
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
296 Command string used by \\[tex-show-print-queue] to show the print
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
297 queue that \\[tex-print] put your job on.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
298
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
299 Entering SliTex mode calls the value of text-mode-hook, then the value of
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
300 tex-mode-hook, then the value of latex-mode-hook, and then the value of
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
301 slitex-mode-hook. When the special subshell is initiated, the value of
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
302 tex-shell-hook is called."
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
303 (interactive)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
304 (tex-common-initialization)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
305 (setq mode-name "SliTeX")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
306 (setq major-mode 'slitex-mode)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
307 (setq tex-command slitex-run-command)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
308 (setq tex-start-of-header "\\documentstyle{slides}")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
309 (setq tex-end-of-header "\\begin{document}")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
310 (setq tex-trailer "\\end{document}\n")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
311 (run-hooks
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
312 'text-mode-hook 'tex-mode-hook 'latex-mode-hook 'slitex-mode-hook))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
313
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
314 (defun tex-common-initialization ()
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
315 (kill-all-local-variables)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
316 (use-local-map tex-mode-map)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
317 (setq local-abbrev-table text-mode-abbrev-table)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
318 (if (null tex-mode-syntax-table)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
319 (let ((char 0))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
320 (setq tex-mode-syntax-table (make-syntax-table))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
321 (set-syntax-table tex-mode-syntax-table)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
322 (while (< char ? )
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
323 (modify-syntax-entry char ".")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
324 (setq char (1+ char)))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
325 (modify-syntax-entry ?\C-@ "w")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
326 (modify-syntax-entry ?\t " ")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
327 (modify-syntax-entry ?\n ">")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
328 (modify-syntax-entry ?\f ">")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
329 (modify-syntax-entry ?$ "$$")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
330 (modify-syntax-entry ?% "<")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
331 (modify-syntax-entry ?\\ "/")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
332 (modify-syntax-entry ?\" ".")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
333 (modify-syntax-entry ?& ".")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
334 (modify-syntax-entry ?_ ".")
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 ?' "w"))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
338 (set-syntax-table tex-mode-syntax-table))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
339 (make-local-variable 'paragraph-start)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
340 (setq paragraph-start "^[ \t]*$\\|^[\f\\\\%]")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
341 (make-local-variable 'paragraph-separate)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
342 (setq paragraph-separate paragraph-start)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
343 (make-local-variable 'comment-start)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
344 (setq comment-start "%")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
345 (make-local-variable 'comment-start-skip)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
346 (setq comment-start-skip "\\(\\(^\\|[^\\]\\)\\(\\\\\\\\\\)*\\)\\(%+ *\\)")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
347 (make-local-variable 'comment-indent-hook)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
348 (setq comment-indent-hook 'tex-comment-indent)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
349 (make-local-variable 'compare-windows-whitespace)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
350 (setq compare-windows-whitespace 'tex-categorize-whitespace)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
351 (make-local-variable 'tex-command)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
352 (make-local-variable 'tex-start-of-header)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
353 (make-local-variable 'tex-end-of-header)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
354 (make-local-variable 'tex-trailer))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
355
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
356 (defun tex-comment-indent ()
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
357 (if (looking-at "%%%")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
358 (current-column)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
359 (skip-chars-backward " \t")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
360 (max (if (bolp) 0 (1+ (current-column)))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
361 comment-column)))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
362
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
363 (defun tex-categorize-whitespace (backward-limit)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
364 ;; compare-windows-whitespace is set to this.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
365 ;; This is basically a finite-state machine.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
366 ;; Returns a symbol telling how TeX would treat
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
367 ;; the whitespace we are looking at: null, space, or par.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
368 (let ((category 'null)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
369 (not-finished t))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
370 (skip-chars-backward " \t\n\f" backward-limit)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
371 (while not-finished
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
372 (cond ((looking-at "[ \t]+")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
373 (goto-char (match-end 0))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
374 (if (eql category 'null)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
375 (setq category 'space)))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
376 ((looking-at "\n")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
377 (cond ((eql category 'newline)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
378 (setq category 'par)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
379 (setq not-finished nil))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
380 (t
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
381 (setq category 'newline) ;a strictly internal state
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
382 (goto-char (match-end 0)))))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
383 ((looking-at "\f+")
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 not-finished nil))))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
388 (skip-chars-forward " \t\n\f")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
389 (if (eql category 'newline)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
390 'space ;TeX doesn't distinguish
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
391 category)))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
392
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
393 (defun tex-insert-quote (arg)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
394 "Insert the appropriate quote marks for TeX.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
395 Inserts the value of tex-open-quote (normally ``) or tex-close-quote
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
396 (normally '') depending on the context. With prefix argument, always
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
397 inserts \" characters."
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
398 (interactive "P")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
399 (if arg
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
400 (self-insert-command (prefix-numeric-value arg))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
401 (insert
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
402 (cond ((or (bobp)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
403 (save-excursion
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
404 (forward-char -1)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
405 (looking-at "\\s(\\|\\s \\|\\s>")))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
406 tex-open-quote)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
407 ((= (preceding-char) ?\\)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
408 ?\")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
409 (t
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
410 tex-close-quote)))))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
411
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
412 (defun validate-tex-buffer ()
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
413 "Check current buffer for paragraphs containing mismatched $'s.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
414 As each such paragraph is found, a mark is pushed at its beginning,
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
415 and the location is displayed for a few seconds."
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
416 (interactive)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
417 (let ((opoint (point)))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
418 (goto-char (point-max))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
419 ;; Does not use save-excursion
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
420 ;; because we do not want to save the mark.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
421 (unwind-protect
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
422 (while (and (not (input-pending-p)) (not (bobp)))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
423 (let ((end (point)))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
424 (search-backward "\n\n" nil 'move)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
425 (or (tex-validate-region (point) end)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
426 (progn
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
427 (push-mark (point))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
428 (message "Mismatch found in pararaph starting here")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
429 (sit-for 4)))))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
430 (goto-char opoint))))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
431
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
432 (defun tex-validate-region (start end)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
433 "Check for mismatched braces or $'s in region.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
434 Returns t if no mismatches. Returns nil and moves point to suspect
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
435 area if a mismatch is found."
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
436 (interactive "r")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
437 (let ((failure-point nil) (max-possible-sexps (- end start)))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
438 (save-excursion
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
439 (condition-case ()
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
440 (save-restriction
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
441 (narrow-to-region start end)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
442 (goto-char start)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
443 (while (< 0 (setq max-possible-sexps (1- max-possible-sexps)))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
444 (forward-sexp 1)))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
445 (error
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
446 (setq failure-point (point)))))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
447 (if failure-point
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
448 (progn
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
449 (goto-char failure-point)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
450 nil)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
451 t)))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
452
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
453 (defun tex-terminate-paragraph (inhibit-validation)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
454 "Insert two newlines, breaking a paragraph for TeX.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
455 Check for mismatched braces/$'s in paragraph being terminated.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
456 A prefix arg inhibits the checking."
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
457 (interactive "P")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
458 (or inhibit-validation
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
459 (save-excursion
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
460 (tex-validate-region
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
461 (save-excursion
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
462 (search-backward "\n\n" nil 'move)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
463 (point))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
464 (point)))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
465 (message "Paragraph being closed appears to contain a mismatch"))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
466 (insert "\n\n"))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
467
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
468 (defun tex-insert-braces ()
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
469 "Make a pair of braces and be poised to type inside of them."
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
470 (interactive)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
471 (insert ?\{)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
472 (save-excursion
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
473 (insert ?})))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
474
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
475 ;;; Like tex-insert-braces, but for LaTeX.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
476 (defun tex-latex-block (name)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
477 "Creates a matching pair of lines \\begin{NAME} and \\end{NAME} at point.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
478 Puts point on a blank line between them."
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
479 (interactive "*sLaTeX block name: ")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
480 (let ((col (current-column)))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
481 (insert (format "\\begin{%s}\n" name))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
482 (indent-to col)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
483 (save-excursion
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
484 (insert ?\n)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
485 (indent-to col)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
486 (insert-string (format "\\end{%s}" name))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
487 (if (eobp) (insert ?\n)))))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
488
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
489 (defun tex-last-unended-begin ()
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
490 "Leave point at the beginning of the last \\begin{...} that is unended."
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
491 (while (and (re-search-backward "\\(\\\\begin\\s *{\\)\\|\\(\\\\end\\s *{\\)")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
492 (looking-at "\\\\end{"))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
493 (tex-last-unended-begin)))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
494
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
495 (defun tex-close-latex-block ()
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
496 "Creates an \\end{...} to match the last unclosed \\begin{...}."
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
497 (interactive "*")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
498 (let ((new-line-needed (bolp))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
499 text indentation)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
500 (save-excursion
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
501 (condition-case ERR
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
502 (tex-last-unended-begin)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
503 (error (error "Couldn't find unended \\begin")))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
504 (setq indentation (current-column))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
505 (re-search-forward "\\\\begin\\(\\s *{[^}\n]*}\\)")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
506 (setq text (buffer-substring (match-beginning 1) (match-end 1))))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
507 (indent-to indentation)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
508 (insert "\\end" text)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
509 (if new-line-needed (insert ?\n))))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
510
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
511 ;;; Invoking TeX in an inferior shell.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
512
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
513 ;;; Why use a shell instead of running TeX directly? Because if TeX
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
514 ;;; gets stuck, the user can switch to the shell window and type at it.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
515
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
516 ;;; The utility functions:
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
517
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
518 (defun tex-start-shell ()
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
519 (save-excursion
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
520 (set-buffer (make-shell "tex-shell" nil nil "-v"))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
521 (setq tex-shell-map (copy-keymap shell-mode-map))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
522 (tex-define-common-keys tex-shell-map)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
523 (use-local-map tex-shell-map)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
524 (run-hooks 'tex-shell-hook)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
525 (if (zerop (buffer-size))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
526 (sleep-for 1))))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
527
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
528 (defun set-buffer-directory (buffer directory)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
529 "Set BUFFER's default directory to be DIRECTORY."
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
530 (setq directory (file-name-as-directory (expand-file-name directory)))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
531 (if (not (file-directory-p directory))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
532 (error "%s is not a directory" directory)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
533 (save-excursion
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
534 (set-buffer buffer)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
535 (setq default-directory directory))))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
536
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
537 ;;; The commands:
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
538
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
539 ;;; It's a kludge that we have to create a special buffer just
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
540 ;;; to write out the tex-trailer. It would nice if there were a
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
541 ;;; function like write-region that would write literal strings.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
542
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
543 (defun tex-region (beg end)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
544 "Run TeX on the current region, via a temporary file.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
545 The file's name comes from the variable `tex-zap-file' and the
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
546 variable `tex-directory' says where to put it.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
547
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
548 If the buffer has a header, the header is given to TeX before the
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
549 region itself. The buffer's header is all lines between the strings
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
550 defined by `tex-start-of-header' and `tex-end-of-header' inclusive.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
551 The header must start in the first 100 lines of the buffer.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
552
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
553 The value of `tex-trailer' is given to TeX as input after the region.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
554
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
555 The value of `tex-command' specifies the command to use to run TeX."
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
556 (interactive "r")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
557 (if (tex-shell-running)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
558 (tex-kill-job)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
559 (tex-start-shell))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
560 (or tex-zap-file
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
561 (setq tex-zap-file (tex-generate-zap-file-name)))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
562 (let ((tex-out-file (concat tex-zap-file ".tex"))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
563 (temp-buffer (get-buffer-create " TeX-Output-Buffer"))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
564 (file-dir (if (buffer-file-name)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
565 (file-name-directory (buffer-file-name))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
566 default-directory))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
567 (zap-directory
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
568 (file-name-as-directory (expand-file-name tex-directory))))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
569 ;; Delete any junk files or memory files from this temp file,
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
570 ;; since the contents were probably different last time anyway.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
571 ;; This may also delete the old temp file if any.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
572 (let ((list (file-name-all-completions (tex-append tex-out-file ".")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
573 zap-directory)))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
574 (while list
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
575 (delete-file (expand-file-name (car list) zap-directory))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
576 (setq list (cdr list))))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
577 ;; Write the new temp file.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
578 (save-excursion
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
579 (save-restriction
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
580 (widen)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
581 (goto-char (point-min))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
582 (forward-line 100)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
583 (let ((search-end (point))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
584 (hbeg (point-min)) (hend (point-min))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
585 (default-directory zap-directory))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
586 (goto-char (point-min))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
587 ;; Initialize the temp file with either the header or nothing
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
588 (if (search-forward tex-start-of-header search-end t)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
589 (progn
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
590 (beginning-of-line)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
591 (setq hbeg (point)) ;mark beginning of header
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
592 (if (search-forward tex-end-of-header nil t)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
593 (progn (forward-line 1)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
594 (setq hend (point))) ;mark end of header
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
595 (setq hbeg (point-min))))) ;no header
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
596 (write-region (min hbeg beg) hend tex-out-file nil nil)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
597 (write-region (max beg hend) end tex-out-file t nil))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
598 (let ((local-tex-trailer tex-trailer))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
599 (set-buffer temp-buffer)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
600 (erase-buffer)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
601 ;; make sure trailer isn't hidden by a comment
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
602 (insert-string "\n")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
603 (if local-tex-trailer (insert-string local-tex-trailer))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
604 (set-buffer-directory temp-buffer zap-directory)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
605 (write-region (point-min) (point-max) tex-out-file t nil))))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
606 ;; Record in the shell buffer the file name to delete afterward.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
607 (save-excursion
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
608 (set-buffer (get-buffer "*tex-shell*"))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
609 (make-local-variable 'tex-last-temp-file)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
610 (setq tex-last-temp-file tex-out-file))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
611 (set-process-filter "tex-shell" 'tex-filter)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
612 (set-buffer-directory "*tex-shell*" zap-directory)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
613 ;; Run TeX in source file's dir, in case TEXINPUTS uses current dir.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
614 (send-string "tex-shell" (concat tex-shell-cd-command " " file-dir "\n"))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
615 (send-string "tex-shell" (concat tex-command " \""
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
616 zap-directory
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
617 tex-out-file "\"\n")))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
618 (setq tex-last-buffer-texed (current-buffer))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
619 (setq tex-print-file
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
620 (concat (file-name-as-directory (expand-file-name tex-directory))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
621 tex-zap-file))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
622 (tex-recenter-output-buffer 0))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
623
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
624 ;; This filter is used in the TeX shell buffer
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
625 ;; while TeX is running for a tex-region command.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
626 (defun tex-filter (process string)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
627 (let ((old (current-buffer)))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
628 (set-buffer (process-buffer proc))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
629 (unwind-protect
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
630 (progn (if (= (process-mark proc) (point-max))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
631 (insert string)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
632 (save-excursion
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
633 (goto-char (process-mark proc))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
634 (insert string)))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
635 (set-marker (process-mark proc) (point))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
636 ;; Delete the temporary file
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
637 ;; when TeX finishes.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
638 ;; And stop using this filter.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
639 (save-excursion
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
640 (forward-line -1)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
641 (if (looking-at "^Output written on ")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
642 (progn
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
643 (set-process-filter process nil)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
644 ;; Delete the temp file just processed
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
645 ;; and any related junk files made by TeX.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
646 (let ((list (file-name-all-completions
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
647 (tex-append tex-last-temp-file ".")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
648 zap-directory)))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
649 (while list
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
650 (delete-file (expand-file-name
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
651 (car list) zap-directory))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
652 (setq list (cdr list))))))))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
653 (or (eq old (current-buffer))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
654 (set-buffer old)))))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
655
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
656 (defun tex-buffer ()
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
657 "Run TeX on current buffer. See \\[tex-region] for more information.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
658 Does not save the buffer, so it's useful for trying
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
659 experimental versions. See \\[tex-file] for an alternative."
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
660 (interactive)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
661 (tex-region (point-min) (point-max)))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
662
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
663 (defun tex-file ()
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
664 "Prompt to save all buffers and run TeX (or LaTeX) on current buffer's file.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
665 This function is more useful than \\[tex-buffer] when you need the
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
666 `.aux' file of LaTeX to have the correct name."
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
667 (interactive)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
668 (let ((tex-out-file
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
669 (if (buffer-file-name)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
670 (file-name-nondirectory (buffer-file-name))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
671 (error "Buffer does not seem to be associated with any file")))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
672 (file-dir (file-name-directory (buffer-file-name))))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
673 (save-some-buffers)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
674 (if (tex-shell-running)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
675 (tex-kill-job)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
676 (tex-start-shell))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
677 (set-buffer-directory "*tex-shell*" file-dir)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
678 (send-string "tex-shell" (concat tex-shell-cd-command " " file-dir "\n"))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
679 (send-string "tex-shell"
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
680 (concat tex-command " \"" tex-out-file "\"\n")))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
681 (setq tex-last-buffer-texed (current-buffer))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
682 (setq tex-print-file (buffer-file-name))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
683 (tex-recenter-output-buffer 0))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
684
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
685 (defun tex-generate-zap-file-name ()
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
686 "Generate a unique name suitable for use as a file name."
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
687 ;; Include the shell process number and host name
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
688 ;; in case there are multiple shells (for same or different user).
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
689 (format "#tz%d%s"
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
690 (process-id (get-buffer-process "*tex-shell*"))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
691 (tex-strip-dots (system-name))))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
692
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
693 (defun tex-strip-dots (s)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
694 (setq s (copy-sequence s))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
695 (while (string-match "\\." s)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
696 (aset s (match-beginning 0) ?-))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
697 s)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
698
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
699 ;; This will perhaps be useful for modifying TEXINPUTS.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
700 ;; Expand each file name, separated by colons, in the string S.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
701 (defun tex-expand-files (s)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
702 (let (elts (start 0))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
703 (while (string-match ":" s start)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
704 (setq elts (cons (substring s start (match-beginning 0)) elts))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
705 (setq start (match-end 0)))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
706 (or (= start 0)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
707 (setq elts (cons (substring s start) elts)))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
708 (mapconcat 'expand-file-name (nreverse elts) ":")))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
709
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
710 (defun tex-shell-running ()
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
711 (and (get-process "tex-shell")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
712 (eq (process-status (get-process "tex-shell")) 'run)))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
713
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
714 (defun tex-kill-job ()
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
715 "Kill the currently running TeX job."
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
716 (interactive)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
717 (if (get-process "tex-shell")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
718 (quit-process "tex-shell" t)))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
719
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
720 (defun tex-recenter-output-buffer (linenum)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
721 "Redisplay buffer of TeX job output so that most recent output can be seen.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
722 The last line of the buffer is displayed on
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
723 line LINE of the window, or centered if LINE is nil."
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
724 (interactive "P")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
725 (let ((tex-shell (get-buffer "*tex-shell*"))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
726 (old-buffer (current-buffer)))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
727 (if (null tex-shell)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
728 (message "No TeX output buffer")
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
729 (pop-to-buffer tex-shell)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
730 (bury-buffer tex-shell)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
731 (goto-char (point-max))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
732 (recenter (if linenum
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
733 (prefix-numeric-value linenum)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
734 (/ (window-height) 2)))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
735 (pop-to-buffer old-buffer)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
736 )))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
737
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
738 (defun tex-print ()
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
739 "Print the .dvi file made by \\[tex-region], \\[tex-buffer] or \\[tex-file].
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
740 Runs the shell command defined by tex-dvi-print-command."
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
741 (interactive)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
742 (let ((print-file-name-dvi (tex-append tex-print-file ".dvi"))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
743 test-name)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
744 (if (and (not (equal (current-buffer) tex-last-buffer-texed))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
745 (file-newer-than-file-p
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
746 (setq test-name (tex-append (buffer-file-name) ".dvi"))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
747 (tex-append tex-print-file ".dvi")))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
748 (setq print-file-name-dvi test-name))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
749 (if (file-exists-p print-file-name-dvi)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
750 (shell-command
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
751 (concat tex-dvi-print-command " \"" print-file-name-dvi "&\"\n"))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
752 (error "No appropriate `.dvi' file could be found"))))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
753
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
754 (defun tex-view ()
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
755 "Preview the last `.dvi' file made by running TeX under Emacs.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
756 This means, made using \\[tex-region], \\[tex-buffer] or \\[tex-file].
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
757 The variable `tex-dvi-view-command' specifies the shell command for preview."
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
758 (interactive)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
759 (let ((tex-dvi-print-command tex-dvi-view-command))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
760 (tex-print)))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
761
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
762 (defun tex-append (file-name suffix)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
763 "Append to FILENAME the suffix SUFFIX, using same algorithm TeX uses.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
764 Scans for the first (not last) period.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
765 No period is retained immediately before SUFFIX,
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
766 so normally SUFFIX starts with one."
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
767 (if (stringp file-name)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
768 (let ((file (file-name-nondirectory file-name)))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
769 (concat (file-name-directory file-name)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
770 (substring file 0
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
771 (string-match "\\." file))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
772 suffix))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
773 " "))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
774
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
775 (defun tex-show-print-queue ()
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
776 "Show the print queue that \\[tex-print] put your job on.
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
777 Runs the shell command defined by tex-show-queue-command."
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
778 (interactive)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
779 (if (tex-shell-running)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
780 (tex-kill-job)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
781 (tex-start-shell))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
782 (send-string "tex-shell" (concat tex-show-queue-command "\n"))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
783 (tex-recenter-output-buffer nil))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
784
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
785 (defun tex-bibtex-file ()
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
786 "Run BibTeX on the current buffer's file."
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
787 (interactive)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
788 (if (tex-shell-running)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
789 (tex-kill-job)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
790 (tex-start-shell))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
791 (let ((tex-out-file
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
792 (tex-append (file-name-nondirectory (buffer-file-name)) ""))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
793 (file-dir (file-name-directory (buffer-file-name))))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
794 (set-buffer-directory "*tex-shell*" file-dir)
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
795 (send-string "tex-shell" (concat tex-shell-cd-command " " file-dir "\n"))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
796 (send-string "tex-shell"
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
797 (concat tex-bibtex-command " \"" tex-out-file "\"\n")))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
798 (tex-recenter-output-buffer 0))
253ec6b277ee Initial revision
root <root>
parents:
diff changeset
799