51
|
1 ;;; -*- Emacs-Lisp -*-
|
|
2 ;;; YaTeX process handler.
|
|
3 ;;; yatexprc.el
|
|
4 ;;; (c )1993-1994 by HIROSE Yuuji.[yuuji@ae.keio.ac.jp]
|
|
5 ;;; Last modified Wed Dec 14 16:58:15 1994 on 98fa
|
|
6 ;;; $Id$
|
|
7
|
|
8 (require 'yatex)
|
|
9
|
|
10 (defvar YaTeX-typeset-process nil
|
|
11 "Process identifier for jlatex"
|
|
12 )
|
|
13 (defvar YaTeX-typeset-buffer "*YaTeX-typesetting*"
|
|
14 "Process buffer for jlatex")
|
|
15
|
|
16 (defvar YaTeX-typeset-buffer-syntax nil
|
|
17 "*Syntax table for typesetting buffer")
|
|
18
|
|
19 (defvar YaTeX-current-TeX-buffer nil
|
|
20 "Keeps the buffer on which recently typeset run.")
|
|
21
|
|
22 (defvar YaTeX-shell-command-option
|
|
23 (or (and (boundp 'shell-command-option) shell-command-option)
|
|
24 (if YaTeX-dos "/c" "-c"))
|
|
25 "Shell option for command execution.")
|
|
26
|
|
27 (if YaTeX-typeset-buffer-syntax nil
|
|
28 (setq YaTeX-typeset-buffer-syntax
|
|
29 (make-syntax-table (standard-syntax-table)))
|
|
30 (modify-syntax-entry ?\{ "w" YaTeX-typeset-buffer-syntax)
|
|
31 (modify-syntax-entry ?\} "w" YaTeX-typeset-buffer-syntax)
|
|
32 (modify-syntax-entry ?\[ "w" YaTeX-typeset-buffer-syntax)
|
|
33 (modify-syntax-entry ?\] "w" YaTeX-typeset-buffer-syntax)
|
|
34 )
|
|
35
|
|
36 (defun YaTeX-typeset (command buffer)
|
|
37 "Execute jlatex (or other) to LaTeX typeset."
|
|
38 (interactive)
|
|
39 (save-excursion
|
|
40 (let ((p (point)) (window (selected-window)) execdir (cb (current-buffer)))
|
|
41 (if (and YaTeX-typeset-process
|
|
42 (eq (process-status YaTeX-typeset-process) 'run))
|
|
43 ;; if tex command is halting.
|
|
44 (YaTeX-kill-typeset-process YaTeX-typeset-process))
|
|
45 (YaTeX-visit-main t) ;;execution directory
|
|
46 (setq execdir default-directory)
|
|
47 ;;Select lower-most window if there are more than 2 windows and
|
|
48 ;;typeset buffer not seen.
|
|
49 (YaTeX-showup-buffer
|
|
50 buffer (function (lambda (x) (nth 3 (window-edges x)))))
|
|
51 (set-buffer (get-buffer-create buffer))
|
|
52 (setq default-directory execdir)
|
|
53 (cd execdir)
|
|
54 (erase-buffer)
|
|
55 (cond
|
|
56 (YaTeX-dos ;if MS-DOS
|
|
57 (YaTeX-put-nonstopmode)
|
|
58 (call-process
|
|
59 shell-file-name nil buffer nil YaTeX-shell-command-option command)
|
|
60 (YaTeX-remove-nonstopmode))
|
|
61 (t ;if UNIX
|
|
62 (set-process-buffer
|
|
63 (setq YaTeX-typeset-process
|
|
64 (start-process "LaTeX" buffer shell-file-name
|
|
65 YaTeX-shell-command-option command))
|
|
66 (get-buffer buffer))
|
|
67 (set-process-sentinel YaTeX-typeset-process 'YaTeX-typeset-sentinel)))
|
|
68 (message (format "Calling `%s'..." command))
|
|
69 (setq YaTeX-current-TeX-buffer (buffer-name))
|
|
70 (use-local-map YaTeX-typesetting-mode-map)
|
|
71 (set-syntax-table YaTeX-typeset-buffer-syntax)
|
|
72 (setq mode-name "typeset")
|
|
73 (if YaTeX-typeset-process ; if process is running (maybe on UNIX)
|
|
74 (cond ((boundp 'MULE)
|
|
75 (set-current-process-coding-system
|
|
76 YaTeX-latex-message-code YaTeX-coding-system))
|
|
77 ((boundp 'NEMACS)
|
|
78 (set-kanji-process-code YaTeX-latex-message-code))))
|
|
79 (if YaTeX-dos (message "Done.")
|
|
80 (insert " ")
|
|
81 (set-marker (process-mark YaTeX-typeset-process) (1- (point))))
|
|
82 (if (bolp) (forward-line -1)) ;what for?
|
|
83 (if (and YaTeX-emacs-19 window-system)
|
|
84 (let ((win (get-buffer-window buffer t)) owin)
|
|
85 (select-frame (window-frame win))
|
|
86 (setq owin (selected-window))
|
|
87 (select-window win)
|
|
88 (goto-char (point-max))
|
|
89 (recenter -1)
|
|
90 (select-window owin))
|
|
91 (select-window (get-buffer-window buffer))
|
|
92 (goto-char (point-max))
|
|
93 (recenter -1))
|
|
94 (select-window window)
|
|
95 (switch-to-buffer cb)))
|
|
96 )
|
|
97
|
|
98 (defun YaTeX-typeset-sentinel (proc mes)
|
|
99 (cond ((null (buffer-name (process-buffer proc)))
|
|
100 ;; buffer killed
|
|
101 (set-process-buffer proc nil))
|
|
102 ((memq (process-status proc) '(signal exit))
|
|
103 (let* ((obuf (current-buffer)) (pbuf (process-buffer proc))
|
|
104 (pwin (get-buffer-window pbuf))
|
|
105 (owin (selected-window)) win)
|
|
106 ;; save-excursion isn't the right thing if
|
|
107 ;; process-buffer is current-buffer
|
|
108 (unwind-protect
|
|
109 (progn
|
|
110 ;; Write something in *typesetting* and hack its mode line
|
|
111 (if pwin
|
|
112 (select-window pwin)
|
|
113 (set-buffer pbuf))
|
|
114 ;;(YaTeX-showup-buffer pbuf nil t)
|
|
115 (goto-char (point-max))
|
|
116 (if pwin (recenter -3))
|
|
117 (insert ?\n "latex typesetting " mes)
|
|
118 (forward-char -1)
|
|
119 (insert " at " (substring (current-time-string) 0 -5) "\n")
|
|
120 (forward-char 1)
|
|
121 (setq mode-line-process
|
|
122 (concat ": "
|
|
123 (symbol-name (process-status proc))))
|
|
124 (message "latex typesetting %s."
|
|
125 (if (eq (process-status proc) 'exit)
|
|
126 "done" "ceased"))
|
|
127 ;; If buffer and mode line shows that the process
|
|
128 ;; is dead, we can delete it now. Otherwise it
|
|
129 ;; will stay around until M-x list-processes.
|
|
130 (delete-process proc)
|
|
131 )
|
|
132 (setq YaTeX-typeset-process nil)
|
|
133 ;; Force mode line redisplay soon
|
|
134 (set-buffer-modified-p (buffer-modified-p))
|
|
135 )
|
|
136 (select-window owin)
|
|
137 (set-buffer obuf))))
|
|
138 )
|
|
139
|
|
140 (defvar YaTeX-texput-file "texput.tex"
|
|
141 "*File name for temporary file of typeset-region."
|
|
142 )
|
|
143
|
|
144 (defun YaTeX-typeset-region ()
|
|
145 "Paste the region to the file `texput.tex' and execute jlatex (or other)
|
|
146 to LaTeX typeset. The region is specified by the rule:
|
|
147 (1)If keyword `%#BEGIN' is found in the upper direction from (point).
|
|
148 (1-1)if the keyword `%#END' is found after `%#BEGIN',
|
|
149 ->Assume the text between `%#BEGIN' and `%#END' as region.
|
|
150 (1-2)if the keyword `%#END' is not found anywhere after `%#BEGIN',
|
|
151 ->Assume the text after `%#BEGIN' as region.
|
|
152 (2)If no `%#BEGIN' usage is found before the (point),
|
|
153 ->Assume the text between current (point) and (mark) as region.
|
|
154 DON'T forget to eliminate the `%#BEGIN/%#END' notation after editing
|
|
155 operation to the region."
|
|
156 (interactive)
|
|
157 (save-excursion
|
|
158 (let*
|
|
159 ((end "") typeout ;Type out message that tells the method of cutting.
|
|
160 (texput YaTeX-texput-file)
|
|
161 (cmd (concat (YaTeX-get-latex-command nil) " " texput))
|
|
162 (buffer (current-buffer)) opoint preamble (subpreamble "") main
|
|
163 (hilit-auto-highlight nil) ;for Emacs19 with hilit19
|
|
164 reg-begin reg-end)
|
|
165
|
|
166 (save-excursion
|
|
167 (if (search-backward "%#BEGIN" nil t)
|
|
168 (progn
|
|
169 (setq typeout "--- Region from BEGIN to "
|
|
170 end "the end of the buffer ---"
|
|
171 reg-begin (match-end 0))
|
|
172 (if (search-forward "%#END" nil t)
|
|
173 (setq reg-end (match-beginning 0)
|
|
174 end "END ---")
|
|
175 (setq reg-end (point-max))))
|
|
176 (setq typeout "=== Region from (point) to (mark) ===")
|
|
177 (setq reg-begin (point) reg-end (mark)))
|
|
178 (goto-char (point-min))
|
|
179 (while (search-forward "%#REQUIRE" nil t)
|
|
180 (setq subpreamble
|
|
181 (concat subpreamble
|
|
182 (cond
|
|
183 ((eolp)
|
|
184 (buffer-substring
|
|
185 (match-beginning 0)
|
|
186 (point-beginning-of-line)))
|
|
187 (t (buffer-substring
|
|
188 (match-end 0)
|
|
189 (point-end-of-line))))
|
|
190 "\n"))
|
|
191 (goto-char (match-end 0))))
|
|
192 (YaTeX-visit-main t)
|
|
193 (setq main (current-buffer))
|
|
194 (setq opoint (point))
|
|
195 (goto-char (point-min))
|
|
196 (setq
|
|
197 preamble
|
|
198 (if (re-search-forward "^[ ]*\\\\begin.*{document}" nil t)
|
|
199 (buffer-substring (point-min) (match-end 0))
|
|
200 (concat "\\documentstyle{" YaTeX-default-document-style "}\n"
|
|
201 "\\begin{document}")))
|
|
202 (goto-char opoint)
|
|
203 ;;(set-buffer buffer) ;for clarity
|
|
204 (set-buffer (find-file-noselect texput))
|
|
205 ;;(find-file YaTeX-texput-file)
|
|
206 (erase-buffer)
|
|
207 (if YaTeX-need-nonstop
|
|
208 (insert "\\nonstopmode{}\n"))
|
|
209 (insert preamble "\n" subpreamble "\n")
|
|
210 (insert-buffer-substring buffer reg-begin reg-end)
|
|
211 (insert "\\typeout{" typeout end "}\n") ;Notice the selected method.
|
|
212 (insert "\n\\end{document}\n")
|
|
213 (basic-save-buffer)
|
|
214 (kill-buffer (current-buffer))
|
|
215 (set-buffer main) ;return to parent file or itself.
|
|
216 (YaTeX-typeset cmd YaTeX-typeset-buffer)
|
|
217 (switch-to-buffer buffer) ;for Emacs-19
|
|
218 (put 'dvi2-command 'region t)))
|
|
219 )
|
|
220
|
|
221 (defun YaTeX-typeset-buffer ()
|
|
222 "Typeset whole buffer. If %#! usage says other buffer is main text,
|
|
223 visit main buffer to confirm if its includeonly list contains current
|
|
224 buffer's file. And if it doesn't contain editing text, ask user which
|
|
225 action wants to be done, A:Add list, R:Replace list, %:comment-out list."
|
|
226 (interactive)
|
|
227 (YaTeX-save-buffers)
|
|
228 (let*((me (substring (buffer-name) 0 (rindex (buffer-name) ?.)))
|
|
229 (mydir (file-name-directory (buffer-file-name)))
|
|
230 (cmd (YaTeX-get-latex-command t)) (cb (current-buffer)))
|
|
231 (if (YaTeX-main-file-p) nil
|
|
232 (save-excursion
|
|
233 (YaTeX-visit-main t) ;search into main buffer
|
|
234 (save-excursion
|
|
235 (push-mark (point) t)
|
|
236 (goto-char (point-min))
|
|
237 (if (and (re-search-forward "^[ ]*\\\\begin{document}" nil t)
|
|
238 (re-search-backward "^[ ]*\\\\includeonly{" nil t))
|
|
239 (let*
|
|
240 ((b (progn (skip-chars-forward "^{") (point)))
|
|
241 (e (progn (skip-chars-forward "^}") (1+ (point))))
|
|
242 (s (buffer-substring b e)) c
|
|
243 (pardir (file-name-directory (buffer-file-name))))
|
|
244 (if (string-match (concat "[{,/]" me "[,}]") s)
|
|
245 nil ; Nothing to do when it's already in includeonly.
|
|
246 (ding)
|
|
247 (switch-to-buffer (current-buffer));Display this buffer.
|
|
248 (setq
|
|
249 me ;;Rewrite my name(me) to contain sub directory name.
|
|
250 (concat
|
|
251 (if (string-match pardir mydir) ;if mydir is child of main
|
|
252 (substring mydir (length pardir)) ;cut absolute path
|
|
253 mydir) ;else concat absolute path name.
|
|
254 me))
|
|
255 (message
|
|
256 "`%s' is not in \\includeonly. A)dd R)eplace %%)comment? "
|
|
257 me)
|
|
258 (setq c (read-char))
|
|
259 (cond
|
|
260 ((= c ?a)
|
|
261 (goto-char (1+ b))
|
|
262 (insert me (if (string= s "{}") "" ",")))
|
|
263 ((= c ?r)
|
|
264 (delete-region (1+ b) (1- e)) (insert me))
|
|
265 ((= c ?%)
|
|
266 (beginning-of-line) (insert "%"))
|
|
267 (t nil))
|
|
268 (basic-save-buffer))))
|
|
269 (exchange-point-and-mark)))
|
|
270 (switch-to-buffer cb)) ;for 19
|
|
271 (YaTeX-typeset cmd YaTeX-typeset-buffer)
|
|
272 (put 'dvi2-command 'region nil))
|
|
273 )
|
|
274
|
|
275 (defvar YaTeX-call-command-history nil
|
|
276 "Holds history list of YaTeX-call-command-on-file.")
|
|
277 (put 'YaTeX-call-command-history 'no-default t)
|
|
278 (defun YaTeX-call-command-on-file (base-cmd buffer)
|
|
279 (YaTeX-save-buffers)
|
|
280 (YaTeX-typeset
|
|
281 (read-string-with-history
|
|
282 "Call command: "
|
|
283 (concat base-cmd " " (YaTeX-get-preview-file-name))
|
|
284 'YaTeX-call-command-history)
|
|
285 buffer)
|
|
286 )
|
|
287
|
|
288 (defun YaTeX-bibtex-buffer (cmd)
|
|
289 "Pass the bibliography data of editing file to bibtex."
|
|
290 (interactive)
|
|
291 (YaTeX-save-buffers)
|
|
292 (YaTeX-call-command-on-file cmd "*YaTeX-bibtex*" )
|
|
293 )
|
|
294
|
|
295 (defun YaTeX-kill-typeset-process (proc)
|
|
296 "Kill process PROC after sending signal to PROC.
|
|
297 PROC should be process identifier."
|
|
298 (cond
|
|
299 (YaTeX-dos
|
|
300 (error "MS-DOS can't have concurrent process."))
|
|
301 ((or (null proc) (not (eq (process-status proc) 'run)))
|
|
302 (message "Typesetting process is not running."))
|
|
303 (t
|
|
304 (save-excursion
|
|
305 (set-buffer (process-buffer proc))
|
|
306 (save-excursion
|
|
307 (goto-char (point-max))
|
|
308 (beginning-of-line)
|
|
309 (if (looking-at "\\? +$")
|
|
310 (let ((mp (point-max)))
|
|
311 (process-send-string proc "x\n")
|
|
312 (while (= mp (point-max)) (sit-for 1))))))
|
|
313 (if (eq (process-status proc) 'run)
|
|
314 (progn
|
|
315 (interrupt-process proc)
|
|
316 (delete-process proc)))))
|
|
317 )
|
|
318
|
|
319 (defun YaTeX-system (command buffer)
|
|
320 "Execute some command on buffer. Not a official function."
|
|
321 (save-excursion
|
|
322 (YaTeX-showup-buffer
|
|
323 buffer (function (lambda (x) (nth 3 (window-edges x)))))
|
|
324 (set-buffer (get-buffer-create buffer))
|
|
325 (erase-buffer)
|
|
326 (if YaTeX-dos
|
|
327 (call-process
|
|
328 shell-file-name nil buffer nil YaTeX-shell-command-option command)
|
|
329 (set-process-buffer
|
|
330 (start-process
|
|
331 "system" buffer shell-file-name YaTeX-shell-command-option command)
|
|
332 (get-buffer buffer))))
|
|
333 )
|
|
334
|
|
335 (defvar YaTeX-preview-command-history nil
|
|
336 "Holds minibuffer history of preview command.")
|
|
337 (put 'YaTeX-preview-command-history 'no-default t)
|
|
338 (defvar YaTeX-preview-file-history nil
|
|
339 "Holds minibuffer history of file to preview.")
|
|
340 (put 'YaTeX-preview-file-history 'no-default t)
|
|
341 (defun YaTeX-preview (preview-command preview-file)
|
|
342 "Execute xdvi (or other) to tex-preview."
|
|
343 (interactive
|
|
344 (list
|
|
345 (read-string-with-history
|
|
346 "Preview command: " dvi2-command 'YaTeX-preview-command-history)
|
|
347 (read-string-with-history
|
|
348 "Preview file[.dvi]: "
|
|
349 (if (get 'dvi2-command 'region)
|
|
350 (substring YaTeX-texput-file
|
|
351 0 (rindex YaTeX-texput-file ?.))
|
|
352 (YaTeX-get-preview-file-name))
|
|
353 'YaTeX-preview-file-history)))
|
|
354 (setq dvi2-command preview-command) ;`dvi2command' is buffer local
|
|
355 (save-excursion
|
|
356 (YaTeX-visit-main t)
|
|
357 (let ((pbuffer "*dvi-preview*") (dir default-directory))
|
|
358 (YaTeX-showup-buffer
|
|
359 pbuffer (function (lambda (x) (nth 3 (window-edges x)))))
|
|
360 (set-buffer (get-buffer-create pbuffer))
|
|
361 (erase-buffer)
|
|
362 (setq default-directory dir) ;for 18
|
|
363 (cd dir) ;for 19
|
|
364 (cond
|
|
365 (YaTeX-dos ;if MS-DOS
|
|
366 (send-string-to-terminal "\e[2J\e[>5h") ;CLS & hide cursor
|
|
367 (call-process shell-file-name "con" "*dvi-preview*" nil
|
|
368 YaTeX-shell-command-option preview-command preview-file)
|
|
369 (send-string-to-terminal "\e[>5l") ;show cursor
|
|
370 (redraw-display))
|
|
371 (t ;if UNIX
|
|
372 (set-process-buffer
|
|
373 (start-process "preview" "*dvi-preview*" shell-file-name
|
|
374 YaTeX-shell-command-option
|
|
375 (concat preview-command " " preview-file))
|
|
376 (get-buffer pbuffer))
|
|
377 (message
|
|
378 (concat "Starting " preview-command
|
|
379 " to preview " preview-file))))))
|
|
380 )
|
|
381
|
|
382 (defun YaTeX-prev-error ()
|
|
383 "Visit previous typeset error.
|
|
384 To avoid making confliction of line numbers by editing, jump to
|
|
385 error or warning lines in reverse order."
|
|
386 (interactive)
|
|
387 (let ((cur-buf (buffer-name)) (cur-win (selected-window))
|
|
388 error-line typeset-win error-buffer error-win)
|
|
389 (if (null (get-buffer YaTeX-typeset-buffer))
|
|
390 (error "There is no typesetting buffer."))
|
|
391 (YaTeX-showup-buffer YaTeX-typeset-buffer nil t)
|
|
392 (setq typeset-win (selected-window))
|
|
393 (if (re-search-backward
|
|
394 (concat "\\(" latex-error-regexp "\\)\\|\\("
|
|
395 latex-warning-regexp "\\)")
|
|
396 nil t)
|
|
397 nil
|
|
398 (select-window cur-win)
|
|
399 (error "No more erros on %s" cur-buf))
|
|
400 (goto-char (match-beginning 0))
|
|
401 (skip-chars-forward "^0-9" (match-end 0))
|
|
402 (setq error-line
|
|
403 (string-to-int
|
|
404 (buffer-substring
|
|
405 (point)
|
|
406 (progn (skip-chars-forward "0-9" (match-end 0)) (point))))
|
|
407 error-buffer (YaTeX-get-error-file cur-buf)
|
|
408 error-win (get-buffer-window error-buffer))
|
|
409 (if (or (null error-line) (equal 0 error-line))
|
|
410 (error "Can't detect error position."))
|
|
411 (select-window cur-win)
|
|
412 (cond
|
|
413 (error-win (select-window error-win))
|
|
414 ((eq (get-lru-window) typeset-win)
|
|
415 (YaTeX-switch-to-buffer error-buffer))
|
|
416 (t (select-window (get-lru-window))
|
|
417 (YaTeX-switch-to-buffer error-buffer)))
|
|
418 (setq error-win (selected-window))
|
|
419 (goto-line error-line)
|
|
420 (message "LaTeX %s in `%s' on line: %d."
|
|
421 (if (match-beginning 1) "error" "warning")
|
|
422 error-buffer error-line)
|
|
423 (select-window typeset-win)
|
|
424 (skip-chars-backward "0-9")
|
|
425 (recenter (/ (window-height) 2))
|
|
426 (sit-for 3)
|
|
427 (goto-char (match-beginning 0))
|
|
428 (select-window error-win))
|
|
429 )
|
|
430
|
|
431 (defun YaTeX-jump-error-line ()
|
|
432 "Jump to corresponding line on latex command's error message."
|
|
433 (interactive)
|
|
434 (let (error-line error-file error-buf)
|
|
435 (save-excursion
|
|
436 (beginning-of-line)
|
|
437 (setq error-line (re-search-forward "l[ ines]*\\.?\\([1-9][0-9]*\\)"
|
|
438 (point-end-of-line) t)))
|
|
439 (if (null error-line)
|
|
440 (if (eobp) (insert (this-command-keys))
|
|
441 (error "No line number expression."))
|
|
442 (goto-char (match-beginning 0))
|
|
443 (setq error-line (string-to-int
|
|
444 (buffer-substring (match-beginning 1) (match-end 1)))
|
|
445 error-file (YaTeX-get-error-file YaTeX-current-TeX-buffer)
|
|
446 error-buf (YaTeX-switch-to-buffer error-file t))
|
|
447 (if (null error-buf)
|
|
448 (error "`%s' is not found in this directory." error-file))
|
|
449 (YaTeX-showup-buffer error-buf nil t)
|
|
450 (goto-line error-line)))
|
|
451 )
|
|
452
|
|
453 (defun YaTeX-send-string ()
|
|
454 "Send string to current typeset process."
|
|
455 (interactive)
|
|
456 (if (and (eq (process-status YaTeX-typeset-process) 'run)
|
|
457 (>= (point) (process-mark YaTeX-typeset-process)))
|
|
458 (let ((b (process-mark YaTeX-typeset-process))
|
|
459 (e (point-end-of-line)))
|
|
460 (goto-char b)
|
|
461 (skip-chars-forward " \t" e)
|
|
462 (setq b (point))
|
|
463 (process-send-string
|
|
464 YaTeX-typeset-process (concat (buffer-substring b e) "\n"))
|
|
465 (goto-char e)
|
|
466 (insert "\n")
|
|
467 (set-marker (process-mark YaTeX-typeset-process) (point))
|
|
468 (insert " "))
|
|
469 (ding))
|
|
470 )
|
|
471
|
|
472 (defun YaTeX-view-error ()
|
|
473 (interactive)
|
|
474 (if (null (get-buffer YaTeX-typeset-buffer))
|
|
475 (message "No typeset buffer found.")
|
|
476 (let ((win (selected-window)))
|
|
477 (YaTeX-showup-buffer YaTeX-typeset-buffer nil t)
|
|
478 ;; Next 3 lines are obsolete because YaTeX-typesetting-buffer is
|
|
479 ;; automatically scrolled up at typesetting.
|
|
480 ;;(goto-char (point-max))
|
|
481 ;;(forward-line -1)
|
|
482 ;;(recenter -1)
|
|
483 (select-window win)))
|
|
484 )
|
|
485
|
|
486 (defun YaTeX-get-error-file (default)
|
|
487 "Get current processing file from typesetting log."
|
|
488 (save-excursion
|
|
489 (let(s)
|
|
490 (condition-case () (up-list -1)
|
|
491 (error
|
|
492 (let ((list 0) found)
|
|
493 (while
|
|
494 (and (<= list 0) (not found)
|
|
495 (re-search-backward "\\((\\)\\|\\()\\)" nil t))
|
|
496 (if (equal (match-beginning 0) (match-beginning 2)) ;close paren.
|
|
497 (setq list (1- list)) ;open paren
|
|
498 (setq list (1+ list))
|
|
499 (if (= list 1)
|
|
500 (if (looking-at "\\([^,{}%]+\.\\)tex\\|sty")
|
|
501 (setq found t)
|
|
502 (setq list (1- list)))))))))
|
|
503 (setq s
|
|
504 (buffer-substring
|
|
505 (progn (forward-char 1) (point))
|
|
506 (progn (skip-chars-forward "^ \n" (point-end-of-line))
|
|
507 (point))))
|
|
508 (if (string= "" s) default s)))
|
|
509 )
|
|
510
|
|
511 (defun YaTeX-put-nonstopmode ()
|
|
512 (if YaTeX-need-nonstop
|
|
513 (if (re-search-backward "\\\\nonstopmode{}" (point-min) t)
|
|
514 nil ;if already written in text then do nothing
|
|
515 (save-excursion
|
|
516 (YaTeX-visit-main t)
|
|
517 (goto-char (point-min))
|
|
518 (insert "\\nonstopmode{}%_YaTeX_%\n")))
|
|
519 )
|
|
520 )
|
|
521
|
|
522 (defun YaTeX-remove-nonstopmode ()
|
|
523 (if YaTeX-need-nonstop ;for speed
|
|
524 (save-excursion
|
|
525 (YaTeX-visit-main t)
|
|
526 (goto-char (point-min))
|
|
527 (forward-line 1)
|
|
528 (narrow-to-region (point-min) (point))
|
|
529 (goto-char (point-min))
|
|
530 (delete-matching-lines "^\\\\nonstopmode\\{\\}%_YaTeX_%$")
|
|
531 (widen)))
|
|
532 )
|
|
533
|
|
534 (defun YaTeX-get-preview-file-name ()
|
|
535 "Get file name to preview by inquiring YaTeX-get-latex-command"
|
|
536 (let* ((latex-cmd (YaTeX-get-latex-command t))
|
|
537 (rin (rindex latex-cmd ? ))
|
|
538 (fname (if (> rin -1) (substring latex-cmd (1+ rin)) ""))
|
|
539 (period))
|
|
540 (if (string= fname "")
|
|
541 (setq fname (substring (file-name-nondirectory
|
|
542 (buffer-file-name))
|
|
543 0 -4))
|
|
544 (setq period (rindex fname ?.))
|
|
545 (setq fname (substring fname 0 (if (eq -1 period) nil period)))
|
|
546 ))
|
|
547 )
|
|
548
|
|
549 (defun YaTeX-get-latex-command (&optional switch)
|
|
550 "Specify the latex-command name and its argument.
|
|
551 If there is a line which begins with string: \"%#!\", the following
|
|
552 strings are assumed to be the latex-command and arguments. The
|
|
553 default value of latex-command is:
|
|
554 tex-command FileName
|
|
555 and if you write \"%#!jlatex\" in the beginning of certain line.
|
|
556 \"jlatex \" FileName
|
|
557 will be the latex-command,
|
|
558 and you write \"%#!jlatex main.tex\" on some line and argument SWITCH
|
|
559 is non-nil, then
|
|
560 \"jlatex main.tex\"
|
|
561
|
|
562 will be given to the shell."
|
|
563 (let (magic command target)
|
|
564 (setq parent
|
|
565 (cond
|
|
566 (YaTeX-parent-file YaTeX-parent-file)
|
|
567 (t (save-excursion
|
|
568 (YaTeX-visit-main t)
|
|
569 (file-name-nondirectory (buffer-file-name)))))
|
|
570 magic (YaTeX-get-builtin "!"))
|
|
571 (cond
|
|
572 (magic
|
|
573 (cond
|
|
574 (switch (if (string-match "\\s " magic) magic
|
|
575 (concat magic " " parent)))
|
|
576 (t (concat (substring magic 0 (string-match "\\s " magic)) " "))))
|
|
577 (t (concat tex-command " " (if switch parent)))))
|
|
578 )
|
|
579
|
|
580 (defvar YaTeX-lpr-command-history nil
|
|
581 "Holds command line history of YaTeX-lpr.")
|
|
582 (put 'YaTeX-lpr-command-history 'no-default t)
|
|
583 (defun YaTeX-lpr (arg)
|
|
584 "Print out. If prefix arg ARG is non nil, call print driver without
|
|
585 page range description."
|
|
586 (interactive "P")
|
|
587 (let*((cmd (or (YaTeX-get-builtin "LPR") dviprint-command-format))
|
|
588 from to (lbuffer "*dvi-printing*"))
|
|
589 (setq
|
|
590 cmd
|
|
591 (YaTeX-replace-format
|
|
592 cmd "f"
|
|
593 (if (or arg (not (string-match "%f" cmd)))
|
|
594 ""
|
|
595 (YaTeX-replace-format
|
|
596 dviprint-from-format
|
|
597 "b"
|
|
598 (if (string=
|
|
599 (setq from (read-string "From page(default 1): ")) "")
|
|
600 "1" from))))
|
|
601 )
|
|
602 (setq
|
|
603 cmd
|
|
604 (YaTeX-replace-format
|
|
605 cmd "t"
|
|
606 (if (or arg (not (string-match "%t" cmd))
|
|
607 (string=
|
|
608 (setq to (read-string "To page(default none): ")) ""))
|
|
609 ""
|
|
610 (YaTeX-replace-format dviprint-to-format "e" to)))
|
|
611 )
|
|
612 (setq cmd
|
|
613 (read-string-with-history
|
|
614 "Edit command line: "
|
|
615 (format cmd (YaTeX-get-preview-file-name))
|
|
616 'YaTeX-lpr-command-history))
|
|
617 (save-excursion
|
|
618 (YaTeX-visit-main t) ;;change execution directory
|
|
619 (YaTeX-showup-buffer
|
|
620 lbuffer (function (lambda (x) (nth 3 (window-edges x)))))
|
|
621 (set-buffer (get-buffer-create lbuffer))
|
|
622 (erase-buffer)
|
|
623 (cond
|
|
624 (YaTeX-dos
|
|
625 (call-process shell-file-name "con" "*dvi-printing*" nil
|
|
626 YaTeX-shell-command-option cmd))
|
|
627 (t
|
|
628 (set-process-buffer
|
|
629 (start-process "print" "*dvi-printing*" shell-file-name
|
|
630 YaTeX-shell-command-option cmd)
|
|
631 (get-buffer lbuffer))
|
|
632 (message "Starting printing command: %s..." cmd)))
|
|
633 ))
|
|
634 )
|
|
635
|
|
636 (defun YaTeX-main-file-p ()
|
|
637 "Return if current buffer is main LaTeX source."
|
|
638 (cond
|
|
639 ((YaTeX-get-builtin "!")
|
|
640 (string-match (YaTeX-guess-parent (YaTeX-get-builtin "!")) (buffer-name)))
|
|
641 (t
|
|
642 (save-excursion
|
|
643 (let ((latex-main-id (concat "^\\s *" YaTeX-ec-regexp "documentstyle")))
|
|
644 (or (re-search-backward latex-main-id nil t)
|
|
645 (re-search-forward latex-main-id nil t))))))
|
|
646 )
|
|
647
|
|
648 (defun YaTeX-visit-main (&optional setbuf)
|
|
649 "Switch buffer to main LaTeX source.
|
|
650 Use set-buffer instead of switch-to-buffer if the optional second argument
|
|
651 SETBUF is t(Use it only from Emacs-Lisp program)."
|
|
652 (interactive "P")
|
|
653 (if (and (interactive-p) setbuf) (setq YaTeX-parent-file nil))
|
|
654 (let (b-in main-file)
|
|
655 (if (setq b-in (YaTeX-get-builtin "!"))
|
|
656 (setq main-file (YaTeX-guess-parent b-in)))
|
|
657 (if YaTeX-parent-file
|
|
658 (setq main-file ;;(get-file-buffer YaTeX-parent-file)
|
|
659 YaTeX-parent-file))
|
|
660 (if (YaTeX-main-file-p)
|
|
661 (if (interactive-p) (message "I think this is main LaTeX source.") nil)
|
|
662 (cond
|
|
663 ((and (interactive-p) main-file (get-file-buffer main-file))
|
|
664 (goto-buffer-window main-file))
|
|
665 ((and main-file (YaTeX-switch-to-buffer main-file setbuf)))
|
|
666 ((and main-file
|
|
667 (file-exists-p (setq main-file (concat "../" main-file)))
|
|
668 (y-or-n-p (concat (expand-file-name main-file)
|
|
669 " is main file?:")))
|
|
670 (setq YaTeX-parent-file main-file)
|
|
671 (YaTeX-switch-to-buffer main-file setbuf))
|
|
672 (t (setq main-file (read-file-name "Enter your main text: " nil nil 1))
|
|
673 (setq YaTeX-parent-file main-file)
|
|
674 (YaTeX-switch-to-buffer main-file setbuf))
|
|
675 )))
|
|
676 nil
|
|
677 )
|
|
678
|
|
679
|
|
680 (defun YaTeX-guess-parent (command-line)
|
|
681 (setq command-line
|
|
682 (if (string-match ".*\\s " command-line)
|
|
683 (substring command-line (match-end 0))
|
|
684 (file-name-nondirectory (buffer-file-name)))
|
|
685 command-line
|
|
686 (concat (if (string-match "\\(.*\\)\\." command-line)
|
|
687 (substring command-line (match-beginning 1) (match-end 1))
|
|
688 command-line)
|
|
689 ".tex"))
|
|
690 )
|
|
691
|
|
692 (defun YaTeX-visit-main-other-window ()
|
|
693 "Switch to buffer main LaTeX source in other window."
|
|
694 (interactive)
|
|
695 (if (YaTeX-main-file-p) (message "I think this is main LaTeX source.")
|
|
696 (YaTeX-switch-to-buffer-other-window
|
|
697 (concat (YaTeX-get-preview-file-name) ".tex")))
|
|
698 )
|
|
699
|
|
700 (defun YaTeX-get-builtin (key)
|
|
701 "Read source built-in command of %# usage."
|
|
702 (save-excursion
|
|
703 (goto-char (point-min))
|
|
704 (if (and (re-search-forward
|
|
705 (concat "^" (regexp-quote (concat "%#" key))) nil t)
|
|
706 (not (eolp)))
|
|
707 (buffer-substring
|
|
708 (progn (skip-chars-forward " " (point-end-of-line))(point))
|
|
709 (point-end-of-line))
|
|
710 nil))
|
|
711 )
|
|
712
|
|
713 (defun YaTeX-save-buffers ()
|
|
714 "Save buffers which is in yatex-mode."
|
|
715 (basic-save-buffer)
|
|
716 (save-excursion
|
|
717 (mapcar '(lambda (buf)
|
|
718 (set-buffer buf)
|
|
719 (if (and (buffer-file-name buf)
|
|
720 (eq major-mode 'yatex-mode)
|
|
721 (buffer-modified-p buf)
|
|
722 (y-or-n-p (format "Save %s" (buffer-name buf))))
|
|
723 (save-buffer buf)))
|
|
724 (buffer-list)))
|
|
725 )
|
|
726
|
|
727 (provide 'yatexprc)
|