rev |
line source |
yuuji@7
|
1 ;;; -*- Emacs-Lisp -*-
|
yuuji@7
|
2 ;;; YaTeX process handler.
|
yuuji@7
|
3 ;;; yatexprc.el rev.1.42
|
yuuji@7
|
4 ;;; (c)1993 by HIROSE Yuuji.[yuuji@ae.keio.ac.jp]
|
yuuji@7
|
5 ;;; Last modified Tue May 4 21:49:59 1993 on figaro
|
yuuji@7
|
6 ;;; $Id$
|
yuuji@7
|
7
|
yuuji@7
|
8 (require 'yatex)
|
yuuji@7
|
9
|
yuuji@7
|
10 (defvar YaTeX-typeset-process nil
|
yuuji@7
|
11 "Process identifier for jlatex"
|
yuuji@7
|
12 )
|
yuuji@7
|
13 (defvar YaTeX-typeset-buffer "*YaTeX-typesetting*"
|
yuuji@7
|
14 "Process buffer for jlatex")
|
yuuji@7
|
15
|
yuuji@7
|
16 (defun YaTeX-typeset (command buffer)
|
yuuji@7
|
17 "Execute jlatex (or other) to LaTeX typeset."
|
yuuji@7
|
18 (interactive)
|
yuuji@7
|
19 (if (and YaTeX-typeset-process
|
yuuji@7
|
20 (eq (process-status YaTeX-typeset-process) 'run))
|
yuuji@7
|
21 ;; if tex command is halting.
|
yuuji@7
|
22 (YaTeX-kill-typeset-process YaTeX-typeset-process))
|
yuuji@7
|
23 (YaTeX-visit-main t) ;;execution directory
|
yuuji@7
|
24 (with-output-to-temp-buffer buffer
|
yuuji@7
|
25 (if (eq system-type 'ms-dos) ;if MS-DOS
|
yuuji@7
|
26 (progn
|
yuuji@7
|
27 (message (concat "Typesetting " (buffer-name) "..."))
|
yuuji@7
|
28 (YaTeX-put-nonstopmode)
|
yuuji@7
|
29 (call-process shell-file-name
|
yuuji@7
|
30 nil buffer nil "/c" command)
|
yuuji@7
|
31 (YaTeX-remove-nonstopmode))
|
yuuji@7
|
32 (setq YaTeX-typeset-process ;if UNIX
|
yuuji@7
|
33 (start-process "LaTeX" buffer shell-file-name "-c"
|
yuuji@7
|
34 command))
|
yuuji@7
|
35 (set-process-sentinel YaTeX-typeset-process 'YaTeX-typeset-sentinel)))
|
yuuji@7
|
36 (setq current-TeX-buffer (buffer-name))
|
yuuji@7
|
37 (other-window 1)
|
yuuji@7
|
38 (use-local-map YaTeX-typesetting-mode-map)
|
yuuji@7
|
39 (setq mode-name "typeset")
|
yuuji@7
|
40 (if YaTeX-typeset-process ; if process is running (maybe on UNIX)
|
yuuji@7
|
41 (cond ((boundp 'MULE)
|
yuuji@7
|
42 (set-current-process-coding-system
|
yuuji@7
|
43 YaTeX-latex-message-code YaTeX-coding-system))
|
yuuji@7
|
44 ((boundp 'NEMACS)
|
yuuji@7
|
45 (set-kanji-process-code YaTeX-latex-message-code))))
|
yuuji@7
|
46 (message "Type SPC to continue.")
|
yuuji@7
|
47 (goto-char (point-max))
|
yuuji@7
|
48 (if (eq system-type 'ms-dos) (message "Done.")
|
yuuji@7
|
49 (while (bobp) (message "Invoking process. wait...") (sleep-for 1))
|
yuuji@7
|
50 (insert (message " ")))
|
yuuji@7
|
51 (if (bolp) (forward-line -1))
|
yuuji@7
|
52 (recenter -1)
|
yuuji@7
|
53 (other-window -1)
|
yuuji@7
|
54 )
|
yuuji@7
|
55
|
yuuji@7
|
56 (defun YaTeX-typeset-sentinel (proc mes)
|
yuuji@7
|
57 (cond ((null (buffer-name (process-buffer proc)))
|
yuuji@7
|
58 ;; buffer killed
|
yuuji@7
|
59 (set-process-buffer proc nil))
|
yuuji@7
|
60 ((memq (process-status proc) '(signal exit))
|
yuuji@7
|
61 (let* ((obuf (current-buffer)))
|
yuuji@7
|
62 ;; save-excursion isn't the right thing if
|
yuuji@7
|
63 ;; process-buffer is current-buffer
|
yuuji@7
|
64 (unwind-protect
|
yuuji@7
|
65 (progn
|
yuuji@7
|
66 ;; Write something in *typesetting* and hack its mode line
|
yuuji@7
|
67 (if (equal (current-buffer) (process-buffer proc))
|
yuuji@7
|
68 nil
|
yuuji@7
|
69 (other-window 1)
|
yuuji@7
|
70 (switch-to-buffer (process-buffer proc))
|
yuuji@7
|
71 (goto-char (point-max))
|
yuuji@7
|
72 (recenter -3)
|
yuuji@7
|
73 (other-window -1))
|
yuuji@7
|
74 (set-buffer (process-buffer proc))
|
yuuji@7
|
75 (goto-char (point-max))
|
yuuji@7
|
76 (insert ?\n "latex typesetting " mes)
|
yuuji@7
|
77 (forward-char -1)
|
yuuji@7
|
78 (insert " at " (substring (current-time-string) 0 -5) "\n")
|
yuuji@7
|
79 (forward-char 1)
|
yuuji@7
|
80 (setq mode-line-process
|
yuuji@7
|
81 (concat ": "
|
yuuji@7
|
82 (symbol-name (process-status proc))))
|
yuuji@7
|
83 (message "latex typesetting done.")
|
yuuji@7
|
84 ;; If buffer and mode line will show that the process
|
yuuji@7
|
85 ;; is dead, we can delete it now. Otherwise it
|
yuuji@7
|
86 ;; will stay around until M-x list-processes.
|
yuuji@7
|
87 (delete-process proc)
|
yuuji@7
|
88 )
|
yuuji@7
|
89 (setq YaTeX-typesetting-process nil)
|
yuuji@7
|
90 ;; Force mode line redisplay soon
|
yuuji@7
|
91 (set-buffer-modified-p (buffer-modified-p))
|
yuuji@7
|
92 )
|
yuuji@7
|
93 (set-buffer obuf))))
|
yuuji@7
|
94 )
|
yuuji@7
|
95
|
yuuji@7
|
96 (defvar YaTeX-texput-file "texput.tex"
|
yuuji@7
|
97 "*File name for temporary file of typeset-region."
|
yuuji@7
|
98 )
|
yuuji@7
|
99
|
yuuji@7
|
100 (defun YaTeX-typeset-region ()
|
yuuji@7
|
101 "Paste the region to the file `texput.tex' and execute jlatex (or other)
|
yuuji@7
|
102 to LaTeX typeset. The region is specified by the rule:
|
yuuji@7
|
103 (1)If keyword `%#BEGIN' is found in the upper direction from (point).
|
yuuji@7
|
104 (1-1)if the keyword `%#END' is found after `%#BEGIN',
|
yuuji@7
|
105 ->Assume the text between `%#BEGIN' and `%#END' as region.
|
yuuji@7
|
106 (1-2)if the keyword `%#END' is not found anywhere after `%#BEGIN',
|
yuuji@7
|
107 ->Assume the text after `%#BEGIN' as region.
|
yuuji@7
|
108 (2)If no `%#BEGIN' usage is found before the (point),
|
yuuji@7
|
109 ->Assume the text between current (point) and (mark) as region.
|
yuuji@7
|
110 DON'T forget to eliminate the `%#BEGIN/%#END' notation after editing
|
yuuji@7
|
111 operation to the region."
|
yuuji@7
|
112 (interactive)
|
yuuji@7
|
113 (save-excursion
|
yuuji@7
|
114 (let*
|
yuuji@7
|
115 ((end "") typeout ;Type out message that tells the method of cutting.
|
yuuji@7
|
116 (cmd (concat (YaTeX-get-latex-command nil) " " YaTeX-texput-file))
|
yuuji@7
|
117 (buffer (current-buffer)) opoint preamble
|
yuuji@7
|
118 (region
|
yuuji@7
|
119 (if (re-search-backward
|
yuuji@7
|
120 "%#BEGIN" nil t)
|
yuuji@7
|
121 (progn
|
yuuji@7
|
122 (setq typeout "--- Region from BEGIN to " end "END ---")
|
yuuji@7
|
123 (buffer-substring
|
yuuji@7
|
124 (match-end 0)
|
yuuji@7
|
125 (if (re-search-forward "%#END" nil t)
|
yuuji@7
|
126 (match-beginning 0)
|
yuuji@7
|
127 (setq end "end of buffer ---")
|
yuuji@7
|
128 (point-max))))
|
yuuji@7
|
129 (setq typeout "=== Region from (point) to (mark) ===")
|
yuuji@7
|
130 (buffer-substring (point) (mark)))))
|
yuuji@7
|
131 (YaTeX-visit-main t)
|
yuuji@7
|
132 (setq opoint (point))
|
yuuji@7
|
133 (goto-char (point-min))
|
yuuji@7
|
134 (setq
|
yuuji@7
|
135 preamble
|
yuuji@7
|
136 (if (re-search-forward "^[ ]*\\\\begin.*{document}" nil t)
|
yuuji@7
|
137 (buffer-substring (point-min) (match-end 0))
|
yuuji@7
|
138 (concat "\\documentstyle{" YaTeX-default-document-style "}\n"
|
yuuji@7
|
139 "\\begin{document}")))
|
yuuji@7
|
140 (goto-char opoint)
|
yuuji@7
|
141 ;;(set-buffer buffer) ;for clarity
|
yuuji@7
|
142 (find-file YaTeX-texput-file)
|
yuuji@7
|
143 (erase-buffer)
|
yuuji@7
|
144 (if YaTeX-need-nonstop
|
yuuji@7
|
145 (insert "\\nonstopmode{}\n"))
|
yuuji@7
|
146 (insert preamble "\n")
|
yuuji@7
|
147 (insert region)
|
yuuji@7
|
148 (insert "\\typeout{" typeout end "}\n") ;Notice the selected method.
|
yuuji@7
|
149 (insert "\n\\end{document}\n")
|
yuuji@7
|
150 (basic-save-buffer)
|
yuuji@7
|
151 (kill-buffer (current-buffer))
|
yuuji@7
|
152 (YaTeX-visit-main t)
|
yuuji@7
|
153 (YaTeX-typeset cmd YaTeX-typeset-buffer)
|
yuuji@7
|
154 (put 'dvi2-command 'region t)))
|
yuuji@7
|
155 )
|
yuuji@7
|
156
|
yuuji@7
|
157 (defun YaTeX-typeset-buffer ()
|
yuuji@7
|
158 "Typeset whole buffer. If %#! usage says other buffer is main text,
|
yuuji@7
|
159 visit main buffer to confirm if its includeonly list contains current
|
yuuji@7
|
160 buffer's file. And if it doesn't contain editing text, ask user which
|
yuuji@7
|
161 action want to be done, A:Add list, R:Replace list, %:comment-out list."
|
yuuji@7
|
162 (interactive)
|
yuuji@7
|
163 (YaTeX-save-buffers)
|
yuuji@7
|
164 (let*((me (substring (buffer-name) 0 (rindex (buffer-name) ?.)))
|
yuuji@7
|
165 (mydir (file-name-directory (buffer-file-name)))
|
yuuji@7
|
166 (cmd (YaTeX-get-latex-command t)))
|
yuuji@7
|
167 (if (YaTeX-main-file-p) nil
|
yuuji@7
|
168 (save-excursion
|
yuuji@7
|
169 (YaTeX-visit-main t) ;search into main buffer
|
yuuji@7
|
170 (save-excursion
|
yuuji@7
|
171 (push-mark (point) t)
|
yuuji@7
|
172 (goto-char (point-min))
|
yuuji@7
|
173 (if (and (re-search-forward "^[ ]*\\\\begin{document}" nil t)
|
yuuji@7
|
174 (re-search-backward "^[ ]*\\\\includeonly{" nil t))
|
yuuji@7
|
175 (let*
|
yuuji@7
|
176 ((b (progn (skip-chars-forward "^{") (point)))
|
yuuji@7
|
177 (e (progn (skip-chars-forward "^}") (1+ (point))))
|
yuuji@7
|
178 (s (buffer-substring b e)) c
|
yuuji@7
|
179 (pardir (file-name-directory (buffer-file-name))))
|
yuuji@7
|
180 (if (string-match (concat "[{,/]" me "[,}]") s)
|
yuuji@7
|
181 nil ; Nothing to do when it's already in includeonly.
|
yuuji@7
|
182 (ding)
|
yuuji@7
|
183 (switch-to-buffer (current-buffer));Display this buffer.
|
yuuji@7
|
184 (setq
|
yuuji@7
|
185 me ;;Rewrite my name(me) to contain sub directory name.
|
yuuji@7
|
186 (concat
|
yuuji@7
|
187 (if (string-match pardir mydir) ;if mydir is child of main
|
yuuji@7
|
188 (substring mydir (length pardir)) ;cut absolute path
|
yuuji@7
|
189 mydir) ;else concat absolute path name.
|
yuuji@7
|
190 me))
|
yuuji@7
|
191 (message
|
yuuji@7
|
192 "`%s' is not in \\includeonly. A)dd R)eplace %%)comment? "
|
yuuji@7
|
193 me)
|
yuuji@7
|
194 (setq c (read-char))
|
yuuji@7
|
195 (cond
|
yuuji@7
|
196 ((= c ?a)
|
yuuji@7
|
197 (goto-char (1+ b))
|
yuuji@7
|
198 (insert me (if (string= s "{}") "" ",")))
|
yuuji@7
|
199 ((= c ?r)
|
yuuji@7
|
200 (delete-region (1+ b) (1- e)) (insert me))
|
yuuji@7
|
201 ((= c ?%)
|
yuuji@7
|
202 (beginning-of-line) (insert "%"))
|
yuuji@7
|
203 (t nil))
|
yuuji@7
|
204 (basic-save-buffer))))
|
yuuji@7
|
205 (exchange-point-and-mark))
|
yuuji@7
|
206 ))
|
yuuji@7
|
207 (YaTeX-typeset cmd YaTeX-typeset-buffer)
|
yuuji@7
|
208 (put 'dvi2-command 'region nil))
|
yuuji@7
|
209 )
|
yuuji@7
|
210
|
yuuji@7
|
211 (defun YaTeX-bibtex-buffer ()
|
yuuji@7
|
212 "Pass the bibliography data of editing file to bibtex."
|
yuuji@7
|
213 (interactive)
|
yuuji@7
|
214 (YaTeX-save-buffers)
|
yuuji@7
|
215 (YaTeX-typeset
|
yuuji@7
|
216 (read-string "BibTeX command: "
|
yuuji@7
|
217 (concat bibtex-command " " (YaTeX-get-preview-file-name)))
|
yuuji@7
|
218 "*YaTeX-bibtex*" )
|
yuuji@7
|
219 )
|
yuuji@7
|
220
|
yuuji@7
|
221 (defun YaTeX-kill-typeset-process (proc)
|
yuuji@7
|
222 "Kill process PROC after sending signal to PROC.
|
yuuji@7
|
223 PROC should be process identifier."
|
yuuji@7
|
224 (cond
|
yuuji@7
|
225 ((eq system-type 'ms-dos)
|
yuuji@7
|
226 (error "MS-DOS can't have concurrent process."))
|
yuuji@7
|
227 ((or (null proc) (not (eq (process-status proc) 'run)))
|
yuuji@7
|
228 (error "No typesetting process."))
|
yuuji@7
|
229 (t (interrupt-process proc)
|
yuuji@7
|
230 (delete-process proc)))
|
yuuji@7
|
231 )
|
yuuji@7
|
232
|
yuuji@7
|
233 (defun YaTeX-system (command buffer)
|
yuuji@7
|
234 "Execute some command on buffer. Not a official function."
|
yuuji@7
|
235 (save-excursion
|
yuuji@7
|
236 (with-output-to-temp-buffer buffer
|
yuuji@7
|
237 (if (eq system-type 'ms-dos)
|
yuuji@7
|
238 (call-process shell-file-name nil buffer nil "/c " command)
|
yuuji@7
|
239 (start-process "system" buffer shell-file-name "-c " command))))
|
yuuji@7
|
240 )
|
yuuji@7
|
241
|
yuuji@7
|
242 (defun YaTeX-preview (preview-command preview-file)
|
yuuji@7
|
243 "Execute xdvi (or other) to tex-preview."
|
yuuji@7
|
244 (interactive
|
yuuji@7
|
245 (list (read-string "Preview command: " dvi2-command)
|
yuuji@7
|
246 (read-string "Prefiew file[.dvi]: "
|
yuuji@7
|
247 ;;(substring (buffer-name) 0 -4)
|
yuuji@7
|
248 (if (get 'dvi2-command 'region)
|
yuuji@7
|
249 (substring YaTeX-texput-file
|
yuuji@7
|
250 0 (rindex YaTeX-texput-file ?.))
|
yuuji@7
|
251 (YaTeX-get-preview-file-name))
|
yuuji@7
|
252 )))
|
yuuji@7
|
253 (setq dvi2-command preview-command)
|
yuuji@7
|
254 (save-excursion
|
yuuji@7
|
255 (YaTeX-visit-main t)
|
yuuji@7
|
256 (with-output-to-temp-buffer "*dvi-preview*"
|
yuuji@7
|
257 (if (eq system-type 'ms-dos)
|
yuuji@7
|
258 (progn (send-string-to-terminal "\e[2J") ;if MS-DOS
|
yuuji@7
|
259 (call-process shell-file-name "con" "*dvi-preview*" nil
|
yuuji@7
|
260 "/c " dvi2-command preview-file)
|
yuuji@7
|
261 (redraw-display))
|
yuuji@7
|
262 (start-process "preview" "*dvi-preview*" shell-file-name "-c"
|
yuuji@7
|
263 (concat dvi2-command " " preview-file)) ;if UNIX
|
yuuji@7
|
264 (message
|
yuuji@7
|
265 (concat "Starting " dvi2-command " to preview " preview-file)))))
|
yuuji@7
|
266 )
|
yuuji@7
|
267
|
yuuji@7
|
268 (defun YaTeX-prev-error ()
|
yuuji@7
|
269 "Visit previous error. The reason why not NEXT-error is to
|
yuuji@7
|
270 avoid make confliction of line numbers by editing."
|
yuuji@7
|
271 (interactive)
|
yuuji@7
|
272 (let ((cur-buf (buffer-name))
|
yuuji@7
|
273 YaTeX-error-line error-buffer)
|
yuuji@7
|
274 (if (null (get-buffer YaTeX-typeset-buffer))
|
yuuji@7
|
275 (message "There is no output buffer of typesetting.")
|
yuuji@7
|
276 (pop-to-buffer YaTeX-typeset-buffer)
|
yuuji@7
|
277 (if (eq system-type 'ms-dos)
|
yuuji@7
|
278 (if (search-backward latex-dos-emergency-message nil t)
|
yuuji@7
|
279 (progn (goto-char (point-max))
|
yuuji@7
|
280 (setq error-regexp latex-error-regexp))
|
yuuji@7
|
281 (beginning-of-line)
|
yuuji@7
|
282 (forward-char -1)
|
yuuji@7
|
283 (setq error-regexp latex-warning-regexp))
|
yuuji@7
|
284 (if YaTeX-typeset-process ; if jlatex on UNIX
|
yuuji@7
|
285 (if (eq (process-status YaTeX-typeset-process) 'run)
|
yuuji@7
|
286 (progn
|
yuuji@7
|
287 (goto-char (point-max))
|
yuuji@7
|
288 (setq error-regexp latex-error-regexp))
|
yuuji@7
|
289 (beginning-of-line)
|
yuuji@7
|
290 (setq error-regexp latex-warning-regexp))))
|
yuuji@7
|
291 (if (re-search-backward error-regexp nil t)
|
yuuji@7
|
292 (save-restriction
|
yuuji@7
|
293 (set-mark-command nil)
|
yuuji@7
|
294 (end-of-line)
|
yuuji@7
|
295 (narrow-to-region (point) (mark))
|
yuuji@7
|
296 (goto-char (point-min))
|
yuuji@7
|
297 (re-search-forward "[0-9]")
|
yuuji@7
|
298 (forward-char -1)
|
yuuji@7
|
299 (set-mark (point))
|
yuuji@7
|
300 (skip-chars-forward "0-9")
|
yuuji@7
|
301 (narrow-to-region (point) (mark))
|
yuuji@7
|
302 (goto-char (point-min))
|
yuuji@7
|
303 (setq YaTeX-error-line (read (current-buffer))))
|
yuuji@7
|
304 (message "No more error on %s" cur-buf)
|
yuuji@7
|
305 (ding)
|
yuuji@7
|
306 )
|
yuuji@7
|
307 (setq error-buffer (YaTeX-get-error-file cur-buf))
|
yuuji@7
|
308 (other-window -1)
|
yuuji@7
|
309 (switch-to-buffer cur-buf)
|
yuuji@7
|
310 (if (null YaTeX-error-line)
|
yuuji@7
|
311 nil
|
yuuji@7
|
312 ;; if warning or error found
|
yuuji@7
|
313 (YaTeX-switch-to-buffer error-buffer)
|
yuuji@7
|
314 (goto-line YaTeX-error-line)
|
yuuji@7
|
315 (message "latex error or warning in '%s' at line: %d"
|
yuuji@7
|
316 error-buffer YaTeX-error-line)
|
yuuji@7
|
317 (other-window 1)
|
yuuji@7
|
318 (skip-chars-backward "[0-9]")
|
yuuji@7
|
319 (recenter (/ (window-height) 2))
|
yuuji@7
|
320 (sit-for 3)
|
yuuji@7
|
321 (forward-char -1)
|
yuuji@7
|
322 (other-window -1)
|
yuuji@7
|
323 )))
|
yuuji@7
|
324 )
|
yuuji@7
|
325
|
yuuji@7
|
326 (defun YaTeX-jump-error-line ()
|
yuuji@7
|
327 "Jump corresponding line on latex command's error message."
|
yuuji@7
|
328 (interactive)
|
yuuji@7
|
329 (let ((p (point))
|
yuuji@7
|
330 (end (progn (end-of-line) (point)))
|
yuuji@7
|
331 (begin (progn (beginning-of-line)(point))))
|
yuuji@7
|
332 (if (null (re-search-forward "l[ ines]*\\.*[1-9][0-9]*" end t))
|
yuuji@7
|
333 (if (save-excursion (end-of-line) (eobp))
|
yuuji@7
|
334 (progn (goto-char p) (insert (this-command-keys)))
|
yuuji@7
|
335 (message "No line number expression"))
|
yuuji@7
|
336 (goto-char (match-beginning 0))
|
yuuji@7
|
337 (re-search-forward "[1-9][0-9]*" end t)
|
yuuji@7
|
338 (save-restriction
|
yuuji@7
|
339 (let ((error-line
|
yuuji@7
|
340 (string-to-int (buffer-substring (match-beginning 0)
|
yuuji@7
|
341 (match-end 0))))
|
yuuji@7
|
342 (error-file (YaTeX-get-error-file current-TeX-buffer)))
|
yuuji@7
|
343 ;;(goto-char (match-beginning 0))
|
yuuji@7
|
344 (other-window -1)
|
yuuji@7
|
345 (message "errors in %s" error-file)
|
yuuji@7
|
346 ;(switch-to-buffer current-TeX-buffer)
|
yuuji@7
|
347 (if (not (YaTeX-switch-to-buffer error-file))
|
yuuji@7
|
348 (error "%s is not found in this directory."))
|
yuuji@7
|
349 (goto-line error-line)))))
|
yuuji@7
|
350 )
|
yuuji@7
|
351
|
yuuji@7
|
352 (defun YaTeX-send-string ()
|
yuuji@7
|
353 "Send string to current typeset process."
|
yuuji@7
|
354 (interactive)
|
yuuji@7
|
355 (if (and (eq (process-status YaTeX-typeset-process) 'run)
|
yuuji@7
|
356 (>= (point) (process-mark YaTeX-typeset-process)))
|
yuuji@7
|
357 (let ((b (process-mark YaTeX-typeset-process))
|
yuuji@7
|
358 (e (point-end-of-line)))
|
yuuji@7
|
359 (goto-char b)
|
yuuji@7
|
360 (skip-chars-forward " \t" e)
|
yuuji@7
|
361 (setq b (point))
|
yuuji@7
|
362 (process-send-string
|
yuuji@7
|
363 YaTeX-typeset-process (concat (buffer-substring b e) "\n"))
|
yuuji@7
|
364 (goto-char e)
|
yuuji@7
|
365 (insert "\n")
|
yuuji@7
|
366 (set-marker (process-mark YaTeX-typeset-process) (point))
|
yuuji@7
|
367 (insert " "))
|
yuuji@7
|
368 (ding))
|
yuuji@7
|
369 )
|
yuuji@7
|
370
|
yuuji@7
|
371 (defun YaTeX-view-error ()
|
yuuji@7
|
372 (interactive)
|
yuuji@7
|
373 (if (null (get-buffer YaTeX-typeset-buffer))
|
yuuji@7
|
374 (message "No typeset buffer found.")
|
yuuji@7
|
375 (pop-to-buffer YaTeX-typeset-buffer)
|
yuuji@7
|
376 (goto-char (point-max))
|
yuuji@7
|
377 (recenter -1)
|
yuuji@7
|
378 (other-window -1))
|
yuuji@7
|
379 )
|
yuuji@7
|
380
|
yuuji@7
|
381 (defun YaTeX-get-error-file (default)
|
yuuji@7
|
382 "Get current processing file from typesetting log."
|
yuuji@7
|
383 (save-excursion
|
yuuji@7
|
384 (let(s)
|
yuuji@7
|
385 (condition-case () (up-list -1)
|
yuuji@7
|
386 (error
|
yuuji@7
|
387 (let ((list 0) found)
|
yuuji@7
|
388 (while
|
yuuji@7
|
389 (and (<= list 0) (not found)
|
yuuji@7
|
390 (re-search-backward "\\((\\)\\|\\()\\)" nil t))
|
yuuji@7
|
391 (if (equal (match-beginning 0) (match-beginning 2)) ;close paren.
|
yuuji@7
|
392 (setq list (1- list)) ;open paren
|
yuuji@7
|
393 (setq list (1+ list))
|
yuuji@7
|
394 (if (= list 1)
|
yuuji@7
|
395 (if (looking-at "\\([^,{}%]+\.\\)tex\\|sty")
|
yuuji@7
|
396 (setq found t)
|
yuuji@7
|
397 (setq list (1- list)))))))))
|
yuuji@7
|
398 (setq s
|
yuuji@7
|
399 (buffer-substring
|
yuuji@7
|
400 (progn (forward-char 1) (point))
|
yuuji@7
|
401 (progn (skip-chars-forward "-A-Za-z0-9_/\.\\" (point-end-of-line))
|
yuuji@7
|
402 (point))))
|
yuuji@7
|
403 (if (string= "" s) default s)))
|
yuuji@7
|
404 )
|
yuuji@7
|
405
|
yuuji@7
|
406 (defun YaTeX-put-nonstopmode ()
|
yuuji@7
|
407 (if YaTeX-need-nonstop
|
yuuji@7
|
408 (if (re-search-backward "\\nonstopmode{}" (point-min) t)
|
yuuji@7
|
409 nil ;if already written in text then do nothing
|
yuuji@7
|
410 (save-excursion
|
yuuji@7
|
411 (YaTeX-visit-main t)
|
yuuji@7
|
412 (goto-char (point-min))
|
yuuji@7
|
413 (insert "\\nonstopmode{}%_YaTeX_%\n")))
|
yuuji@7
|
414 )
|
yuuji@7
|
415 )
|
yuuji@7
|
416
|
yuuji@7
|
417 (defun YaTeX-remove-nonstopmode ()
|
yuuji@7
|
418 (if YaTeX-need-nonstop ;for speed
|
yuuji@7
|
419 (save-excursion
|
yuuji@7
|
420 (YaTeX-visit-main t)
|
yuuji@7
|
421 (goto-char (point-min))
|
yuuji@7
|
422 (forward-line 1)
|
yuuji@7
|
423 (narrow-to-region (point-min) (point))
|
yuuji@7
|
424 (goto-char (point-min))
|
yuuji@7
|
425 (delete-matching-lines "^\\\\nonstopmode\\{\\}%_YaTeX_%$")
|
yuuji@7
|
426 (widen)))
|
yuuji@7
|
427 )
|
yuuji@7
|
428
|
yuuji@7
|
429 (defun YaTeX-get-preview-file-name ()
|
yuuji@7
|
430 "Get file name to preview by inquiring YaTeX-get-latex-command"
|
yuuji@7
|
431 (let* ((latex-cmd (YaTeX-get-latex-command t))
|
yuuji@7
|
432 (rin (rindex latex-cmd ? ))
|
yuuji@7
|
433 (fname (if (> rin -1) (substring latex-cmd (1+ rin)) ""))
|
yuuji@7
|
434 (period))
|
yuuji@7
|
435 (if (string= fname "")
|
yuuji@7
|
436 (setq fname (substring (buffer-name) 0 -4))
|
yuuji@7
|
437 (setq period (rindex fname ?.))
|
yuuji@7
|
438 (setq fname (substring fname 0 (if (eq -1 period) nil period)))
|
yuuji@7
|
439 ))
|
yuuji@7
|
440 )
|
yuuji@7
|
441
|
yuuji@7
|
442 (defun YaTeX-get-latex-command (&optional switch)
|
yuuji@7
|
443 "Specify the latex-command name and its argument.
|
yuuji@7
|
444 If there is a line which begins by string: \"%#!\", the following
|
yuuji@7
|
445 strings are assumed to be the latex-command and arguments. The
|
yuuji@7
|
446 default value of latex-command is:
|
yuuji@7
|
447 tex-command (buffer-name)
|
yuuji@7
|
448 and if you write \"%#!jlatex\" in the beginning of certain line.
|
yuuji@7
|
449 \"jlatex \" (buffer-name)
|
yuuji@7
|
450 will be the latex-command,
|
yuuji@7
|
451 and you write \"%#!jlatex main.tex\" on some line and argument SWITCH
|
yuuji@7
|
452 is t, then
|
yuuji@7
|
453 \"jlatex main.tex\"
|
yuuji@7
|
454 will be given to the shell."
|
yuuji@7
|
455 (let*
|
yuuji@7
|
456 ((default-command
|
yuuji@7
|
457 (concat tex-command " "
|
yuuji@7
|
458 (if switch (buffer-name) ""))));default value
|
yuuji@7
|
459 (save-excursion
|
yuuji@7
|
460 (goto-char (point-min))
|
yuuji@7
|
461 (if (null (re-search-forward "^%#!" (point-max) t))
|
yuuji@7
|
462 default-command
|
yuuji@7
|
463 (skip-chars-forward "%#! ")
|
yuuji@7
|
464 (if (eolp)
|
yuuji@7
|
465 default-command
|
yuuji@7
|
466 (let ((s (point)))
|
yuuji@7
|
467 (skip-chars-forward "^ " (point-end-of-line)) ;Skip command
|
yuuji@7
|
468 (skip-chars-forward " " (point-end-of-line))
|
yuuji@7
|
469 ;(setq YaTeX-latex-command (buffer-substring s (point)))
|
yuuji@7
|
470 (cond
|
yuuji@7
|
471 ((null switch)
|
yuuji@7
|
472 (buffer-substring s (point)))
|
yuuji@7
|
473 ((eolp) ;Only return command name
|
yuuji@7
|
474 (concat (buffer-substring s (point)) " " (buffer-name)))
|
yuuji@7
|
475 (t(end-of-line) ;Change entire command name
|
yuuji@7
|
476 (buffer-substring s (point))) ;including arguments.
|
yuuji@7
|
477 ))
|
yuuji@7
|
478 ))))
|
yuuji@7
|
479 )
|
yuuji@7
|
480
|
yuuji@7
|
481 (defun YaTeX-replace-format (string format repl)
|
yuuji@7
|
482 "In STRING, replace first appearance of FORMAT to REPL as if
|
yuuji@7
|
483 function `format' does. FORMAT does not contain `%'"
|
yuuji@7
|
484 (let ((beg (or (string-match (concat "^\\(%" format "\\)") string)
|
yuuji@7
|
485 (string-match (concat "[^%]\\(%" format "\\)") string)))
|
yuuji@7
|
486 (len (length format)))
|
yuuji@7
|
487 (if (null beg) string ;no conversion
|
yuuji@7
|
488 (concat
|
yuuji@7
|
489 (substring string 0 (match-beginning 1)) repl
|
yuuji@7
|
490 (substring string (match-end 1)))))
|
yuuji@7
|
491 )
|
yuuji@7
|
492
|
yuuji@7
|
493 (defun YaTeX-lpr (arg)
|
yuuji@7
|
494 "Print out. If prefix arg ARG is non nil, call print driver without
|
yuuji@7
|
495 page range description."
|
yuuji@7
|
496 (interactive "P")
|
yuuji@7
|
497 (let*(from to (cmd (or (YaTeX-get-builtin "LPR") dviprint-command-format)))
|
yuuji@7
|
498 (setq
|
yuuji@7
|
499 cmd
|
yuuji@7
|
500 (YaTeX-replace-format
|
yuuji@7
|
501 cmd "f"
|
yuuji@7
|
502 (if (or arg (not (string-match "%f" cmd)))
|
yuuji@7
|
503 ""
|
yuuji@7
|
504 (YaTeX-replace-format
|
yuuji@7
|
505 dviprint-from-format
|
yuuji@7
|
506 "b"
|
yuuji@7
|
507 (if (string=
|
yuuji@7
|
508 (setq from (read-string "From page(default 1): ")) "")
|
yuuji@7
|
509 "1" from))))
|
yuuji@7
|
510 )
|
yuuji@7
|
511 (setq
|
yuuji@7
|
512 cmd
|
yuuji@7
|
513 (YaTeX-replace-format
|
yuuji@7
|
514 cmd "t"
|
yuuji@7
|
515 (if (or arg (not (string-match "%t" cmd))
|
yuuji@7
|
516 (string=
|
yuuji@7
|
517 (setq to (read-string "To page(default none): ")) ""))
|
yuuji@7
|
518 ""
|
yuuji@7
|
519 (YaTeX-replace-format dviprint-to-format "e" to)))
|
yuuji@7
|
520 )
|
yuuji@7
|
521 (setq cmd (read-string "Edit command line: "
|
yuuji@7
|
522 (format cmd (YaTeX-get-preview-file-name))))
|
yuuji@7
|
523 (save-excursion
|
yuuji@7
|
524 (YaTeX-visit-main t) ;;change execution directory
|
yuuji@7
|
525 (with-output-to-temp-buffer "*dvi-printing*"
|
yuuji@7
|
526 (if (eq system-type 'ms-dos)
|
yuuji@7
|
527 (call-process shell-file-name "con" "*dvi-printing*" nil
|
yuuji@7
|
528 "/c " cmd)
|
yuuji@7
|
529 (start-process "print" "*dvi-printing*" shell-file-name "-c" cmd)
|
yuuji@7
|
530 (message (concat "Starting " cmd " to printing "
|
yuuji@7
|
531 (YaTeX-get-preview-file-name))))
|
yuuji@7
|
532 )))
|
yuuji@7
|
533 )
|
yuuji@7
|
534
|
yuuji@7
|
535 (defun YaTeX-main-file-p ()
|
yuuji@7
|
536 "Return if current buffer is main LaTeX source."
|
yuuji@7
|
537 (string-match (concat "^" (YaTeX-get-preview-file-name) ".tex")(buffer-name))
|
yuuji@7
|
538 )
|
yuuji@7
|
539
|
yuuji@7
|
540 (defun YaTeX-visit-main (&optional setbuf)
|
yuuji@7
|
541 "Switch to buffer main LaTeX source. Use set-buffer instead of
|
yuuji@7
|
542 switch-to-buffer if optional second argument SETBUF is t(Use it only
|
yuuji@7
|
543 in Emacs-Lisp program)."
|
yuuji@7
|
544 (interactive)
|
yuuji@7
|
545 (let ((main-file (YaTeX-get-preview-file-name)))
|
yuuji@7
|
546 (if (string-match (concat "^" main-file ".tex") (buffer-name))
|
yuuji@7
|
547 (if (interactive-p) (message "I think this is main LaTeX source.") nil)
|
yuuji@7
|
548 (cond
|
yuuji@7
|
549 ((YaTeX-switch-to-buffer (setq main-file (concat main-file ".tex"))
|
yuuji@7
|
550 setbuf))
|
yuuji@7
|
551 ((and (file-exists-p (setq main-file (concat "../" main-file)))
|
yuuji@7
|
552 (y-or-n-p (concat (expand-file-name main-file)
|
yuuji@7
|
553 " is main file?:")))
|
yuuji@7
|
554 (YaTeX-switch-to-buffer main-file setbuf))
|
yuuji@7
|
555 (t (find-file (read-file-name "Enter your main text: " nil nil 1)))
|
yuuji@7
|
556 )))
|
yuuji@7
|
557 nil
|
yuuji@7
|
558 )
|
yuuji@7
|
559
|
yuuji@7
|
560 (defun YaTeX-visit-main-other-window ()
|
yuuji@7
|
561 "Switch to buffer main LaTeX source in other window."
|
yuuji@7
|
562 (interactive)
|
yuuji@7
|
563 (if (YaTeX-main-file-p) (message "I think this is main LaTeX source.")
|
yuuji@7
|
564 (YaTeX-switch-to-buffer-other-window
|
yuuji@7
|
565 (concat (YaTeX-get-preview-file-name) ".tex")))
|
yuuji@7
|
566 )
|
yuuji@7
|
567
|
yuuji@7
|
568 (defun YaTeX-get-builtin (key)
|
yuuji@7
|
569 "Read source built-in command of %# usage."
|
yuuji@7
|
570 (save-excursion
|
yuuji@7
|
571 (goto-char (point-min))
|
yuuji@7
|
572 (if (and (search-forward (concat "%#" key) nil t)
|
yuuji@7
|
573 (not (eolp)))
|
yuuji@7
|
574 (buffer-substring
|
yuuji@7
|
575 (progn (skip-chars-forward " " (point-end-of-line))(point))
|
yuuji@7
|
576 (point-end-of-line))
|
yuuji@7
|
577 nil))
|
yuuji@7
|
578 )
|
yuuji@7
|
579
|
yuuji@7
|
580 (defun YaTeX-save-buffers ()
|
yuuji@7
|
581 "Save buffers which is in yatex-mode."
|
yuuji@7
|
582 (basic-save-buffer)
|
yuuji@7
|
583 (save-excursion
|
yuuji@7
|
584 (mapcar '(lambda (buf)
|
yuuji@7
|
585 (set-buffer buf)
|
yuuji@7
|
586 (if (and (buffer-file-name buf)
|
yuuji@7
|
587 (eq major-mode 'yatex-mode)
|
yuuji@7
|
588 (buffer-modified-p buf)
|
yuuji@7
|
589 (y-or-n-p (format "Save %s" (buffer-name buf))))
|
yuuji@7
|
590 (save-buffer buf)))
|
yuuji@7
|
591 (buffer-list)))
|
yuuji@7
|
592 )
|
yuuji@7
|
593
|
yuuji@7
|
594 (provide 'yatexprc)
|