|
|
1.1 root 1: ;; Copyright (C) 1985 Richard M. Stallman
2: ;; Rewritten following contributions by William F. Schelter
3: ;; and Dick King (king@kestrel).
4:
5: ;; This file is part of GNU Emacs.
6:
7: ;; GNU Emacs is distributed in the hope that it will be useful,
8: ;; but WITHOUT ANY WARRANTY. No author or distributor
9: ;; accepts responsibility to anyone for the consequences of using it
10: ;; or for whether it serves any particular purpose or works at all,
11: ;; unless he says so in writing. Refer to the GNU Emacs General Public
12: ;; License for full details.
13:
14: ;; Everyone is granted permission to copy, modify and redistribute
15: ;; GNU Emacs, but only under the conditions described in the
16: ;; GNU Emacs General Public License. A copy of this license is
17: ;; supposed to have been given to you along with GNU Emacs so you
18: ;; can know your rights and responsibilities. It should be in a
19: ;; file named COPYING. Among other things, the copyright notice
20: ;; and this notice must be preserved on all copies.
21:
22:
23: (defvar TeX-mode-syntax-table nil
24: "Syntax table used while in TeX mode.")
25:
26: (defvar TeX-zap-file nil
27: "Temporary file name used for text being sent as input to TeX.")
28: (defvar TeX-command "cd /tmp; tex"
29: "The command to run TeX on a file in /tmp, to make output in /tmp.")
30: (defvar TeX-dvi-print-command "lpr -d"
31: "Command string used to print a .dvi file.")
32: (defvar TeX-trailer "\\bye\n"
33: "TeX input supplied after the end of a region sent to TeX by M-x TeX-region.")
34:
35: (defvar TeX-mode-map nil)
36: (if TeX-mode-map
37: nil
38: (setq TeX-mode-map (make-sparse-keymap))
39: (define-key TeX-mode-map "\C-j" 'TeX-terminate-paragraph)
40: (define-key TeX-mode-map "\e{" 'TeX-insert-braces)
41: (define-key TeX-mode-map "\e}" 'up-list)
42: (define-key TeX-mode-map "\"" 'TeX-insert-quote)
43: (define-key TeX-mode-map "\C-c\C-r" 'TeX-region)
44: (define-key TeX-mode-map "\C-c\C-b" 'TeX-buffer)
45: (define-key TeX-mode-map "\C-c\C-p" 'TeX-print))
46:
47: (defun TeX-insert-quote (count)
48: "Insert ``, '' or \" according to preceding character.
49: With numeric arg N, always insert N \" characters."
50: (interactive "P")
51: (if count
52: (self-insert-command count)
53: (insert
54: (cond
55: ((or (bobp)
56: (save-excursion
57: (forward-char -1)
58: (looking-at "[ \t\n]\\|\\s(")))
59: "``")
60: ((= (preceding-char) ?\\)
61: ?\")
62: (t "''")))))
63:
64: (defun validate-TeX-buffer ()
65: "Check current buffer for paragraphs containing mismatched $'s.
66: As each such paragraph is found, a mark is pushed at its beginning,
67: and the location is displayed for a few seconds."
68: (interactive)
69: (let ((opoint (point)))
70: (goto-char (point-max))
71: ;; Does not use save-excursion
72: ;; because we do not want to save the mark.
73: (unwind-protect
74: (while (and (not (input-pending-p)) (not (bobp)))
75: (let ((end (point)))
76: (search-backward "\n\n" nil 'move)
77: (or (TeX-validate-paragraph (point) end)
78: (progn
79: (push-mark (point))
80: (message "Mismatch found in pararaph starting here")
81: (sit-for 4)))))
82: (goto-char opoint))))
83:
84: (defun TeX-validate-paragraph (start end)
85: (condition-case ()
86: (save-excursion
87: (save-restriction
88: (narrow-to-region start end)
89: (goto-char start)
90: (forward-sexp (- end start))
91: t))
92: (error nil)))
93:
94: (defun TeX-terminate-paragraph (inhibit-validation)
95: "Insert two newlines, breaking a paragraph for TeX.
96: Check for mismatched braces/$'s in paragraph being terminated.
97: A prefix arg inhibits the checking."
98: (interactive "P")
99: (or inhibit-validation
100: (TeX-validate-paragraph
101: (save-excursion
102: (search-backward "\n\n" nil 'move)
103: (point))
104: (point))
105: (message "Paragraph being closed appears to contain a mismatch"))
106: (insert "\n\n"))
107:
108: (defun TeX-insert-braces ()
109: "Make a pair of braces and be poised to type inside of them."
110: (interactive)
111: (insert ?\{)
112: (save-excursion
113: (insert ?})))
114:
115: ;(fset 'TeX-mode 'tex-mode) in loaddefs.
116: (defun tex-mode ()
117: "Major mode for editing files of input for TeX.
118: Makes $ and } display the characters they match.
119: Makes \" insert `` when it seems to be the beginning of a quotation,
120: and '' when it appears to be the end; it inserts \" only after a \\.
121:
122: Use M-x validate-TeX-buffer to check buffer for paragraphs containing
123: mismatched $'s or braces.
124:
125: Use C-c C-r to run TeX on the current region, plus a \"header\"
126: copied from the top of the file (containing macro definitions, etc.),
127: running TeX under a special subshell. C-c C-b does the whole buffer.
128: C-c C-p prints the .dvi file made by either of those.
129:
130: Special commands:
131: \\{TeX-mode-map}
132:
133: Entering TeX mode calls the value of text-mode-hook,
134: and then the value of TeX-mode-hook."
135: (interactive)
136: (kill-all-local-variables)
137: (use-local-map TeX-mode-map)
138: (setq mode-name "TeX")
139: (setq major-mode 'TeX-mode)
140: (setq local-abbrev-table text-mode-abbrev-table)
141: (if (null TeX-mode-syntax-table)
142: (progn
143: (setq TeX-mode-syntax-table (make-syntax-table))
144: (set-syntax-table TeX-mode-syntax-table)
145: (modify-syntax-entry ?\\ "\\ ")
146: (modify-syntax-entry ?\$ "$$ ")
147: (modify-syntax-entry ?\% "< ")
148: (modify-syntax-entry ?\f "> ")
149: (modify-syntax-entry ?\n "> ")
150: (modify-syntax-entry ?' "w "))
151: (set-syntax-table TeX-mode-syntax-table))
152: (make-local-variable 'paragraph-start)
153: (setq paragraph-start "^\n")
154: (make-local-variable 'paragraph-separate)
155: (setq paragraph-separate paragraph-start)
156: (make-local-variable 'comment-start)
157: (setq comment-start "%")
158: (make-local-variable 'comment-start-skip)
159: (setq comment-start-skip "[^\\]\\(\\\\\\\\\\)*%+ *")
160: (make-local-variable 'comment-indent-hook)
161: (setq comment-indent-hook 'TeX-comment-indent)
162: (run-hooks 'text-mode-hook 'TeX-mode-hook))
163:
164: (defun TeX-comment-indent ()
165: (if (looking-at "%%%")
166: (current-column)
167: (skip-chars-backward " \t")
168: (max (1+ (current-column)) comment-column)))
169:
170: ;; Invoking TeX in an inferior shell.
171:
172: (defun TeX-region (beg end)
173: "Run TeX on current region. Optionally process buffer's header first.
174: The buffer's header is everything up to a line saying \"%**end of header\".
175: It is processed as input by TeX before the region itself.
176: The file has a header if one of the first ten lines says \"%**start of header\".
177: The value of TeX-trailer is supplied as input to TeX after the region.
178: It defaults to \"\\bye\\n\"."
179: (interactive "r")
180: (or (get-buffer "*TeX-shell*")
181: (progn
182: (require 'shell)
183: (make-shell "TeX-shell" "csh")))
184: (or TeX-zap-file (setq TeX-zap-file (make-temp-name "/tmp/tz")))
185: (let ((tex-out-file (concat TeX-zap-file ".tex")))
186: (save-excursion
187: (goto-char (point-min))
188: (forward-line 10)
189: (let ((search-end (point))
190: hbeg)
191: (goto-char (point-min))
192: ;; Initialize the temp file with either the header or nothing
193: (if (and (search-forward "%**start of header" search-end t)
194: (< (point) beg))
195: (progn
196: (forward-line 1)
197: (setq hbeg (point))
198: (search-forward "%**end of header")
199: (beginning-of-line)
200: (write-region hbeg (point) tex-out-file))
201: (write-region (point) (point) tex-out-file))
202: ;; Append the region to be printed.
203: (write-region beg end tex-out-file t)))
204: (send-string "TeX-shell" (concat TeX-command " " tex-out-file "\n")))
205: (if TeX-trailer
206: (send-string "TeX-shell" TeX-trailer))
207: (pop-to-buffer "*TeX-shell*"))
208:
209: (defun TeX-buffer ()
210: "Run TeX on current buffer."
211: (interactive)
212: (let (TeX-trailer)
213: (TeX-region (point-min) (point-max))))
214:
215: (defun TeX-print ()
216: "Print the .dvi file made by \\[TeX-region] or \\[TeX-buffer]."
217: (interactive)
218: (send-string "TeX-shell"
219: (concat TeX-dvi-print-command " " TeX-zap-file ".dvi\n")))
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.