yatex

view yatexprc.el @ 261:8c555014f933

Auto-rerun supports natbib.
author HIROSE Yuuji <yuuji@gentei.org>
date Mon, 13 Feb 2012 21:59:32 +0900
parents e2899f13e8a4
children 15ec86ff549c
line source
1 ;;; -*- Emacs-Lisp -*-
2 ;;; YaTeX process handler.
3 ;;; yatexprc.el
4 ;;; (c)1993-2012 by HIROSE Yuuji.[yuuji@yatex.org]
5 ;;; Last modified Mon Feb 13 21:58:24 2012 on firestorm
6 ;;; $Id$
8 ;(require 'yatex)
9 (require 'yatexlib)
11 (defvar YaTeX-typeset-process nil
12 "Process identifier for jlatex")
14 (defvar YaTeX-typeset-buffer "*YaTeX-typesetting*"
15 "Process buffer for jlatex")
17 (defvar YaTeX-typeset-buffer-syntax nil
18 "*Syntax table for typesetting buffer")
20 (defvar YaTeX-current-TeX-buffer nil
21 "Keeps the buffer on which recently typeset run.")
23 (defvar YaTeX-shell-command-option
24 (or (and (boundp 'shell-command-option) shell-command-option)
25 (and (boundp 'shell-command-switch) shell-command-switch)
26 (if YaTeX-dos "/c" "-c"))
27 "Shell option for command execution.")
29 (defvar YaTeX-latex-message-code
30 ;; (cond
31 ;; (YaTeX-dos (cdr (assq 1 YaTeX-kanji-code-alist)))
32 ;; ((and YaTeX-emacs-20 (member 'undecided (coding-system-list))
33 ;; 'undecided))
34 ;; ((featurep 'mule)
35 ;; (or (and (boundp '*autoconv*) *autoconv*)
36 ;; (and (fboundp 'coding-system-list) 'automatic-conversion)))
37 ;; ((boundp 'NEMACS)
38 ;; (cdr (assq (if YaTeX-dos 1 2) YaTeX-kanji-code-alist))))
39 (cond
40 (YaTeX-dos (cdr (assq 1 YaTeX-kanji-code-alist)))
41 (YaTeX-emacs-20
42 (cdr (assoc latex-message-kanji-code YaTeX-kanji-code-alist)))
43 ((boundp 'MULE)
44 (symbol-value
45 (cdr (assoc latex-message-kanji-code YaTeX-kanji-code-alist))))
46 ((boundp 'NEMACS)
47 latex-message-kanji-code))
48 "Process coding system for LaTeX.")
50 (if YaTeX-typeset-buffer-syntax nil
51 (setq YaTeX-typeset-buffer-syntax
52 (make-syntax-table (standard-syntax-table)))
53 (modify-syntax-entry ?\{ "w" YaTeX-typeset-buffer-syntax)
54 (modify-syntax-entry ?\} "w" YaTeX-typeset-buffer-syntax)
55 (modify-syntax-entry ?\[ "w" YaTeX-typeset-buffer-syntax)
56 (modify-syntax-entry ?\] "w" YaTeX-typeset-buffer-syntax))
58 (defvar YaTeX-typeset-marker nil)
59 (defun YaTeX-typeset (command buffer &optional prcname modename ppcmd)
60 "Execute jlatex (or other) to LaTeX typeset."
61 (interactive)
62 (save-excursion
63 (let ((p (point)) (window (selected-window)) execdir (cb (current-buffer))
64 (map YaTeX-typesetting-mode-map)
65 (outcode
66 (cond ((eq major-mode 'yatex-mode) YaTeX-coding-system)
67 ((eq major-mode 'yahtml-mode) yahtml-kanji-code))))
68 (if (and YaTeX-typeset-process
69 (eq (process-status YaTeX-typeset-process) 'run))
70 ;; if tex command is halting.
71 (YaTeX-kill-typeset-process YaTeX-typeset-process))
72 (YaTeX-put-nonstopmode)
73 (setq prcname (or prcname "LaTeX")
74 modename (or modename "typeset"))
75 (if (eq major-mode 'yatex-mode) (YaTeX-visit-main t)) ;;execution dir
76 (setq execdir default-directory)
77 ;;Select lower-most window if there are more than 2 windows and
78 ;;typeset buffer not seen.
79 (YaTeX-showup-buffer
80 buffer (function (lambda (x) (nth 3 (window-edges x)))))
81 (set-buffer (get-buffer-create buffer))
82 (setq default-directory execdir)
83 (cd execdir)
84 (erase-buffer)
85 (cond
86 ((not (fboundp 'start-process)) ;YaTeX-dos;if MS-DOS
87 (call-process
88 shell-file-name nil buffer nil YaTeX-shell-command-option command))
89 (t ;if UNIX
90 (set-process-buffer
91 (setq YaTeX-typeset-process
92 (start-process prcname buffer shell-file-name
93 YaTeX-shell-command-option command))
94 (get-buffer buffer))
95 (set-process-sentinel YaTeX-typeset-process 'YaTeX-typeset-sentinel)
96 (put 'YaTeX-typeset-process 'thiscmd command)
97 (put 'YaTeX-typeset-process 'name prcname)
98 (let ((ppprop (get 'YaTeX-typeset-process 'ppcmd)))
99 (setq ppprop (delq (assq YaTeX-typeset-process ppprop) ppprop))
100 (if ppcmd
101 (setq ppprop (cons (cons YaTeX-typeset-process ppcmd) ppprop)))
102 (put 'YaTeX-typeset-process 'ppcmd ppprop))
103 (if (and (boundp 'bibcmd) bibcmd)
104 (let ((bcprop (get 'YaTeX-typeset-process 'bibcmd)))
105 (setq bcprop (cons
106 (cons YaTeX-typeset-process bibcmd)
107 (delq (assq YaTeX-typeset-process bcprop) bcprop)))
108 (put 'YaTeX-typeset-process 'bibcmd bcprop)))))
109 (message (format "Calling `%s'..." command))
110 (setq YaTeX-current-TeX-buffer (buffer-name))
111 (use-local-map map) ;map may be localized
112 (set-syntax-table YaTeX-typeset-buffer-syntax)
113 (setq mode-name modename)
114 (if YaTeX-typeset-process ;if process is running (maybe on UNIX)
115 (cond ((fboundp 'set-current-process-coding-system)
116 (set-current-process-coding-system
117 YaTeX-latex-message-code outcode))
118 ((fboundp 'set-process-coding-system)
119 (set-process-coding-system
120 YaTeX-typeset-process YaTeX-latex-message-code outcode))
121 (YaTeX-emacs-20
122 (set-buffer-process-coding-system
123 YaTeX-latex-message-code outcode))
124 ((boundp 'NEMACS)
125 (set-kanji-process-code YaTeX-latex-message-code))))
126 (set-marker (or YaTeX-typeset-marker
127 (setq YaTeX-typeset-marker (make-marker)))
128 (point))
129 (insert (format "Call `%s'\n" command))
130 (if YaTeX-dos (message "Done.")
131 (insert " ")
132 (set-marker (process-mark YaTeX-typeset-process) (1- (point))))
133 (if (bolp) (forward-line -1)) ;what for?
134 (if (and YaTeX-emacs-19 window-system)
135 (let ((win (get-buffer-window buffer t)) owin)
136 (select-frame (window-frame win))
137 (setq owin (selected-window))
138 (select-window win)
139 (goto-char (point-max))
140 (recenter -1)
141 (select-window owin))
142 (select-window (get-buffer-window buffer))
143 (goto-char (point-max))
144 (recenter -1))
145 (select-window window)
146 (switch-to-buffer cb)
147 (YaTeX-remove-nonstopmode))))
149 (defvar YaTeX-typeset-rerun-msg "Rerun to get cross-references right.")
150 (defvar YaTeX-typeset-citation-msg
151 "Warning: Citation \`")
152 (defvar YaTeX-typeset-auto-rerun t
153 "*Non-nil automatically reruns typesetter when cross-refs update found.
154 This is mechanism is ")
155 (defun YaTeX-typeset-sentinel (proc mes)
156 (cond ((null (buffer-name (process-buffer proc)))
157 ;; buffer killed
158 (set-process-buffer proc nil))
159 ((memq (process-status proc) '(signal exit))
160 (let* ((obuf (current-buffer)) (pbuf (process-buffer proc))
161 (pwin (get-buffer-window pbuf))
162 (owin (selected-window)) win
163 tobecalled shortname
164 (thiscmd (get 'YaTeX-typeset-process 'thiscmd))
165 (ppprop (get 'YaTeX-typeset-process 'ppcmd))
166 (ppcmd (cdr (assq proc ppprop)))
167 (bcprop (get 'YaTeX-typeset-process 'bibcmd))
168 (bibcmd (cdr (assq proc bcprop))))
169 (put 'YaTeX-typeset-process 'ppcmd ;erase ppcmd
170 (delq (assq proc ppprop) ppprop))
171 (put 'YaTeX-typeset-process 'bibcmd ;erase bibcmd
172 (delq (assq proc bcprop) bcprop))
173 ;; save-excursion isn't the right thing if
174 ;; process-buffer is current-buffer
175 (unwind-protect
176 (progn
177 ;; Write something in *typesetting* and hack its mode line
178 (if pwin
179 (select-window pwin)
180 (set-buffer pbuf))
181 ;;(YaTeX-showup-buffer pbuf nil t)
182 (goto-char (point-max))
183 (if pwin (recenter -3))
184 (insert ?\n mode-name " " mes)
185 (forward-char -1)
186 (insert " at " (substring (current-time-string) 0 -5) "\n")
187 (setq mode-line-process
188 (concat ": "
189 (symbol-name (process-status proc))))
190 (message "%s %s" mode-name
191 (if (eq (process-status proc) 'exit)
192 "done" "ceased"))
193 ;; If buffer and mode line shows that the process
194 ;; is dead, we can delete it now. Otherwise it
195 ;; will stay around until M-x list-processes.
196 (delete-process proc)
197 (if (cond
198 ((null YaTeX-typeset-auto-rerun) nil)
199 ((and bibcmd
200 (save-excursion
201 (re-search-backward
202 YaTeX-typeset-citation-msg
203 YaTeX-typeset-marker t))
204 (save-excursion
205 (search-backward
206 ".bbl" YaTeX-typeset-marker t)))
207 (insert "\n" YaTeX-typeset-rerun-msg "\n")
208 (setq tobecalled bibcmd shortname "+bibtex"))
209 ((or
210 (save-excursion
211 (search-backward
212 YaTeX-typeset-rerun-msg YaTeX-typeset-marker t))
213 (save-excursion
214 (re-search-backward
215 "natbib.*Rerun to get citations correct"
216 YaTeX-typeset-marker t)))
217 (if bibcmd
218 (put 'YaTeX-typeset-process 'bibcmd
219 (cons (cons (get-buffer-process pbuf) bibcmd)
220 bcprop)))
221 (setq tobecalled thiscmd shortname "+typeset"))
222 (t
223 nil)) ;no need to call any process
224 (progn
225 (insert
226 (format
227 "===!!! %s !!!===\n"
228 (message "Rerun `%s' to get cross-references right"
229 tobecalled)))
230 (if (equal tobecalled thiscmd)
231 (set-marker YaTeX-typeset-marker (point)))
232 (set-process-sentinel
233 (start-process
234 (setq mode-name (concat mode-name shortname))
235 pbuf
236 shell-file-name YaTeX-shell-command-option tobecalled)
237 'YaTeX-typeset-sentinel)
238 (if ppcmd
239 (put 'YaTeX-typeset-process 'ppcmd
240 (cons (cons (get-buffer-process pbuf) ppcmd)
241 ppprop)))
242 (if thiscmd
243 (put 'YaTeX-typeset-process 'thiscmd thiscmd)))
244 ;; If ppcmd is active, call it.
245 (cond
246 ((and ppcmd (string-match "finish" mes))
247 (insert (format "=======> Success! Calling %s\n" ppcmd))
248 (setq mode-name ; set process name
249 (concat
250 mode-name "+"
251 (substring ppcmd 0 (string-match " " ppcmd))))
252 ; to reach here, 'start-process exists on this emacsen
253 (set-process-sentinel
254 (start-process
255 mode-name
256 pbuf ; Use this buffer twice.
257 shell-file-name YaTeX-shell-command-option
258 ppcmd)
259 'YaTeX-typeset-sentinel))
260 (t ;pull back original name
261 (setq mode-name "typeset"))))
262 (forward-char 1))
263 (setq YaTeX-typeset-process nil)
264 ;; Force mode line redisplay soon
265 (set-buffer-modified-p (buffer-modified-p))
266 )
267 (select-window owin)
268 (set-buffer obuf)))))
270 (defvar YaTeX-texput-file "texput.tex"
271 "*File name for temporary file of typeset-region.")
273 (defun YaTeX-typeset-region ()
274 "Paste the region to the file `texput.tex' and execute typesetter.
275 The region is specified by the rule:
276 (1)If keyword `%#BEGIN' is found in the upper direction from (point).
277 (1-1)if the keyword `%#END' is found after `%#BEGIN',
278 ->Assume the text between `%#BEGIN' and `%#END' as region.
279 (1-2)if the keyword `%#END' is not found anywhere after `%#BEGIN',
280 ->Assume the text after `%#BEGIN' as region.
281 (2)If no `%#BEGIN' usage is found before the (point),
282 ->Assume the text between current (point) and (mark) as region.
283 DON'T forget to eliminate the `%#BEGIN/%#END' notation after editing
284 operation to the region."
285 (interactive)
286 (save-excursion
287 (let*
288 ((end "") typeout ;Type out message that tells the method of cutting.
289 (texput YaTeX-texput-file)
290 (cmd (concat (YaTeX-get-latex-command nil) " " texput))
291 (buffer (current-buffer)) opoint preamble (subpreamble "") main
292 (hilit-auto-highlight nil) ;for Emacs19 with hilit19
293 reg-begin reg-end lineinfo)
295 (save-excursion
296 (if (search-backward "%#BEGIN" nil t)
297 (progn
298 (setq typeout "--- Region from BEGIN to "
299 end "the end of the buffer ---"
300 reg-begin (match-end 0))
301 (if (search-forward "%#END" nil t)
302 (setq reg-end (match-beginning 0)
303 end "END ---")
304 (setq reg-end (point-max))))
305 (setq typeout "=== Region from (point) to (mark) ==="
306 reg-begin (point) reg-end (mark)))
307 (goto-char (min reg-begin reg-end))
308 (setq lineinfo (count-lines (point-min) (point-end-of-line)))
309 (goto-char (point-min))
310 (while (search-forward "%#REQUIRE" nil t)
311 (setq subpreamble
312 (concat subpreamble
313 (cond
314 ((eolp)
315 (buffer-substring
316 (match-beginning 0)
317 (point-beginning-of-line)))
318 (t (buffer-substring
319 (match-end 0)
320 (point-end-of-line))))
321 "\n"))
322 (goto-char (match-end 0))))
323 (YaTeX-visit-main t)
324 (setq main (current-buffer))
325 (setq opoint (point))
326 (goto-char (point-min))
327 (setq
328 preamble
329 (if (re-search-forward "^[ ]*\\\\begin.*{document}" nil t)
330 (buffer-substring (point-min) (match-end 0))
331 (concat
332 (if YaTeX-use-LaTeX2e "\\documentclass{" "\\documentstyle{")
333 YaTeX-default-document-style "}\n"
334 "\\begin{document}")))
335 (goto-char opoint)
336 ;;(set-buffer buffer) ;for clarity
337 (let ((hilit-auto-highlight nil) (auto-mode-alist nil)
338 (magic-mode-alist nil)) ;Do not activate yatex-mode here
339 (set-buffer (find-file-noselect texput)))
340 ;;(find-file YaTeX-texput-file)
341 (erase-buffer)
342 (if (and (eq major-mode 'yatex-mode) YaTeX-need-nonstop)
343 (insert "\\nonstopmode{}\n"))
344 (insert preamble "\n" subpreamble "\n")
345 (setq lineinfo (list (count-lines 1 (point-end-of-line)) lineinfo))
346 (insert-buffer-substring buffer reg-begin reg-end)
347 (insert "\\typeout{" typeout end "}\n") ;Notice the selected method.
348 (insert "\n\\end{document}\n")
349 (basic-save-buffer)
350 (kill-buffer (current-buffer))
351 (set-buffer main) ;return to parent file or itself.
352 (YaTeX-typeset cmd YaTeX-typeset-buffer)
353 (switch-to-buffer buffer) ;for Emacs-19
354 (put 'dvi2-command 'region t)
355 (put 'dvi2-command 'file buffer)
356 (put 'dvi2-command 'offset lineinfo))))
358 (defun YaTeX-typeset-environment ()
359 "Typeset current math environment"
360 (interactive)
361 (save-excursion
362 (YaTeX-mark-environment)
363 (YaTeX-typeset-region)))
365 (defun YaTeX-typeset-buffer (&optional pp)
366 "Typeset whole buffer.
367 If %#! usage says other buffer is main text,
368 visit main buffer to confirm if its includeonly list contains current
369 buffer's file. And if it doesn't contain editing text, ask user which
370 action wants to be done, A:Add list, R:Replace list, %:comment-out list.
371 If optional argument PP given as string, PP is considered as post-process
372 command and call it with the same command argument as typesetter without
373 last extension.
374 eg. if PP is \"dvipdfmx\", called commands as follows.
375 platex foo.tex
376 dvipdfmx foo
377 PP command will be called iff typeset command exit successfully"
378 (interactive)
379 (YaTeX-save-buffers)
380 (let*((me (substring (buffer-name) 0 (rindex (buffer-name) ?.)))
381 (mydir (file-name-directory (buffer-file-name)))
382 (cmd (YaTeX-get-latex-command t)) pparg ppcmd bibcmd
383 (cb (current-buffer)))
384 (setq pparg (substring cmd 0 (string-match "[;&]" cmd)) ;rm multistmt
385 pparg (substring pparg (rindex pparg ? )) ;get last arg
386 pparg (substring pparg 0 (rindex pparg ?.)) ;rm ext
387 bibcmd (concat bibtex-command pparg))
388 (and pp
389 (stringp pp)
390 (setq ppcmd (concat pp pparg)))
391 (if (YaTeX-main-file-p) nil
392 (save-excursion
393 (YaTeX-visit-main t) ;search into main buffer
394 (save-excursion
395 (push-mark (point) t)
396 (goto-char (point-min))
397 (if (and (re-search-forward "^[ ]*\\\\begin{document}" nil t)
398 (re-search-backward "^[ ]*\\\\includeonly{" nil t))
399 (let*
400 ((b (progn (skip-chars-forward "^{") (point)))
401 (e (progn (skip-chars-forward "^}") (1+ (point))))
402 (s (buffer-substring b e)) c
403 (pardir (file-name-directory (buffer-file-name))))
404 (if (string-match (concat "[{,/]" me "[,}]") s)
405 nil ; Nothing to do when it's already in includeonly.
406 (ding)
407 (switch-to-buffer (current-buffer));Display this buffer.
408 (setq
409 me ;;Rewrite my name(me) to contain sub directory name.
410 (concat
411 (if (string-match pardir mydir) ;if mydir is child of main
412 (substring mydir (length pardir)) ;cut absolute path
413 mydir) ;else concat absolute path name.
414 me))
415 (message
416 "`%s' is not in \\includeonly. A)dd R)eplace %%)comment? "
417 me)
418 (setq c (read-char))
419 (cond
420 ((= c ?a)
421 (goto-char (1+ b))
422 (insert me (if (string= s "{}") "" ",")))
423 ((= c ?r)
424 (delete-region (1+ b) (1- e)) (insert me))
425 ((= c ?%)
426 (beginning-of-line) (insert "%"))
427 (t nil))
428 (basic-save-buffer))))
429 (exchange-point-and-mark)))
430 (switch-to-buffer cb)) ;for 19
431 (YaTeX-typeset cmd YaTeX-typeset-buffer nil nil ppcmd)
432 (put 'dvi2-command 'region nil)))
434 (defvar YaTeX-call-command-history nil
435 "Holds history list of YaTeX-call-command-on-file.")
436 (put 'YaTeX-call-command-history 'no-default t)
437 (defun YaTeX-call-command-on-file (base-cmd buffer &optional file)
438 "Call external command BASE-CMD in the BUFFER.
439 By default, pass the basename of current file. Optional 3rd argument
440 FILE changes the default file name."
441 (YaTeX-save-buffers)
442 (let ((default (concat base-cmd " "
443 (let ((me (file-name-nondirectory
444 (or file buffer-file-name))))
445 (if (string-match "\\.tex" me)
446 (substring me 0 (match-beginning 0))
447 me)))))
448 (or YaTeX-call-command-history
449 (setq YaTeX-call-command-history (list default)))
450 (YaTeX-typeset
451 (read-string-with-history
452 "Call command: "
453 (car YaTeX-call-command-history)
454 'YaTeX-call-command-history)
455 buffer)))
457 (defvar YaTeX-call-builtin-on-file)
458 (make-variable-buffer-local 'YaTeX-call-builtin-on-file)
459 (defun YaTeX-call-builtin-on-file (builtin-type &optional default update)
460 "Call command on file specified by BUILTIN-TYPE."
461 (YaTeX-save-buffers)
462 (let*((main (or YaTeX-parent-file
463 (save-excursion (YaTeX-visit-main t) buffer-file-name)))
464 (mainroot (file-name-nondirectory (substring main 0 (rindex main ?.))))
465 (alist YaTeX-call-builtin-on-file)
466 (b-in (or (YaTeX-get-builtin builtin-type)
467 (cdr (assoc builtin-type alist))))
468 (command b-in))
469 (if (or update (null b-in))
470 (progn
471 (setq command (read-string-with-history
472 (format "%s command: " builtin-type)
473 (or b-in
474 (format "%s %s" default mainroot))
475 'YaTeX-call-command-history))
476 (if (or update (null b-in))
477 (if (y-or-n-p "Use this command line in the future? ")
478 (YaTeX-getset-builtin builtin-type command) ;keep in a file
479 (setq YaTeX-call-builtin-on-file ;keep in memory
480 (cons (cons builtin-type command)
481 (delete (assoc builtin-type alist) alist)))))))
482 (YaTeX-typeset
483 command
484 (format " *YaTeX-%s*" (downcase builtin-type)))))
486 (defun YaTeX-kill-typeset-process (proc)
487 "Kill process PROC after sending signal to PROC.
488 PROC should be process identifier."
489 (cond
490 ((not (fboundp 'start-process))
491 (error "This system can't have concurrent process."))
492 ((or (null proc) (not (eq (process-status proc) 'run)))
493 (message "Typesetting process is not running."))
494 (t
495 (save-excursion
496 (set-buffer (process-buffer proc))
497 (save-excursion
498 (goto-char (point-max))
499 (beginning-of-line)
500 (if (looking-at "\\? +$")
501 (let ((mp (point-max)))
502 (process-send-string proc "x\n")
503 (while (= mp (point-max)) (sit-for 1))))))
504 (if (eq (process-status proc) 'run)
505 (progn
506 (interrupt-process proc)
507 (delete-process proc))))))
509 (defun YaTeX-system (command buffer)
510 "Execute some command on buffer. Not a official function."
511 (save-excursion
512 (YaTeX-showup-buffer
513 buffer (function (lambda (x) (nth 3 (window-edges x)))))
514 (let ((df default-directory)) ;preserve current buf's pwd
515 (set-buffer (get-buffer-create buffer)) ;1.61.3
516 (setq default-directory df)
517 (cd df))
518 (erase-buffer)
519 (if (not (fboundp 'start-process))
520 (call-process
521 shell-file-name nil buffer nil YaTeX-shell-command-option command)
522 (if (and (get-buffer-process buffer)
523 (eq (process-status (get-buffer-process buffer)) 'run)
524 (not
525 (y-or-n-p (format "Process %s is running. Continue?" buffer))))
526 nil
527 (set-process-buffer
528 (start-process
529 "system" buffer shell-file-name YaTeX-shell-command-option command)
530 (get-buffer buffer))))))
532 (defvar YaTeX-default-paper-type "a4"
533 "*Default paper type.")
534 (defconst YaTeX-paper-type-alist
535 '(("a4paper" . "a4")
536 ("a5paper" . "a5")
537 ("b4paper" . "b4")
538 ("b5paper" . "b5"))
539 "Holds map of options and paper types.")
540 (defconst YaTeX-dvips-paper-option-alist
541 '(("a4" . "-t a4")
542 ("a5" . "-t a5")
543 ("b4" . "-t b4")
544 ("b5" . "-t b5")
545 ("a4r" . "-t landscape"); Can't specify options, `-t a4' and `-t landscape', at the same time.
546 ("a5r" . "-t landscape")
547 ("b4r" . "-t landscape")
548 ("b5r" . "-t landscape"))
549 "Holds map of dvips options and paper types.")
550 (defun YaTeX-get-paper-type ()
551 "Search options in header and return a paper type, such as \"a4\", \"a4r\", etc."
552 (save-excursion
553 (YaTeX-visit-main t)
554 (goto-char (point-min))
555 (let ((opts
556 (if (re-search-forward
557 "^[ \t]*\\\\document\\(style\\|class\\)[ \t]*\\[\\([^]]*\\)\\]" nil t)
558 (YaTeX-split-string (YaTeX-match-string 2) "[ \t]*,[ \t]*"))))
559 (concat
560 (catch 'found-paper
561 (mapcar (lambda (pair)
562 (if (YaTeX-member (car pair) opts)
563 (throw 'found-paper (cdr pair))))
564 YaTeX-paper-type-alist)
565 YaTeX-default-paper-type)
566 (if (YaTeX-member "landscape" opts) (if YaTeX-dos "L" "r") "")))))
568 (defvar YaTeX-preview-command-history nil
569 "Holds minibuffer history of preview command.")
570 (put 'YaTeX-preview-command-history 'no-default t)
571 (defvar YaTeX-preview-file-history nil
572 "Holds minibuffer history of file to preview.")
573 (put 'YaTeX-preview-file-history 'no-default t)
574 (defun YaTeX-preview (preview-command preview-file)
575 "Execute xdvi (or other) to tex-preview."
576 (interactive
577 (let* ((command (read-string-with-history
578 "Preview command: "
579 (YaTeX-replace-format
580 (or (YaTeX-get-builtin "PREVIEW") dvi2-command)
581 "p" (format (cond
582 (YaTeX-dos "-y:%s")
583 (t "-paper %s"))
584 (YaTeX-get-paper-type)))
585 'YaTeX-preview-command-history))
586 (file (read-string-with-history
587 "Preview file: "
588 (if (get 'dvi2-command 'region)
589 (substring YaTeX-texput-file
590 0 (rindex YaTeX-texput-file ?.))
591 (YaTeX-get-preview-file-name command))
592 'YaTeX-preview-file-history)))
593 (list command file)))
594 (setq dvi2-command preview-command) ;`dvi2command' is buffer local
595 (save-excursion
596 (YaTeX-visit-main t)
597 (if YaTeX-dos (setq preview-file (expand-file-name preview-file)))
598 (let ((pbuffer "*dvi-preview*") (dir default-directory))
599 (YaTeX-showup-buffer
600 pbuffer (function (lambda (x) (nth 3 (window-edges x)))))
601 (set-buffer (get-buffer-create pbuffer))
602 (erase-buffer)
603 (setq default-directory dir) ;for 18
604 (cd dir) ;for 19
605 (cond
606 ((not (fboundp 'start-process)) ;if MS-DOS
607 (send-string-to-terminal "\e[2J\e[>5h") ;CLS & hide cursor
608 (call-process shell-file-name "con" "*dvi-preview*" nil
609 YaTeX-shell-command-option
610 (concat preview-command " " preview-file))
611 (send-string-to-terminal "\e[>5l") ;show cursor
612 (redraw-display))
613 ((and (string-match "dviout" preview-command) ;maybe on `kon'
614 (stringp (getenv "TERM"))
615 (string-match "^kon" (getenv "TERM")))
616 (call-process shell-file-name "con" "*dvi-preview*" nil
617 YaTeX-shell-command-option
618 (concat preview-command " " preview-file)))
619 (t ;if UNIX
620 (set-process-buffer
621 (let ((process-connection-type nil))
622 (start-process "preview" "*dvi-preview*" shell-file-name
623 YaTeX-shell-command-option
624 (concat preview-command " " preview-file)))
625 (get-buffer pbuffer))
626 (message
627 (concat "Starting " preview-command
628 " to preview " preview-file)))))))
630 (defvar YaTeX-xdvi-remote-program "xdvi")
631 (defun YaTeX-xdvi-remote-search (&optional region-mode)
632 "Search string at the point on xdvi -remote window.
633 Non-nil for optional argument REGION-MODE specifies the search string
634 by region."
635 (interactive "P")
636 (let ((pb " *xdvi*") str proc)
637 (save-excursion
638 (if region-mode
639 (setq str (buffer-substring (region-beginning) (region-end)))
640 (setq str (buffer-substring
641 (point)
642 (progn (skip-chars-forward "^\n\\\\}") (point)))))
643 (message "Searching `%s'..." str)
644 (if (boundp 'MULE)
645 (define-program-coding-system
646 (regexp-quote pb) (regexp-quote YaTeX-xdvi-remote-program)
647 *euc-japan*))
648 (setq proc
649 (start-process
650 "xdvi" pb YaTeX-xdvi-remote-program
651 "-remote" (format "SloppySearch(%s) " str)
652 (concat (YaTeX-get-preview-file-name) ".dvi")))
653 (message "Searching `%s'...Done" str))))
655 (defun YaTeX-set-virtual-error-position (file-sym line-sym)
656 "Replace the value of FILE-SYM, LINE-SYM by virtual error position."
657 (cond
658 ((and (get 'dvi2-command 'region)
659 (> (symbol-value line-sym) (car (get 'dvi2-command 'offset))))
660 (set file-sym (get 'dvi2-command 'file))
661 (set line-sym
662 (+ (- (apply '- (get 'dvi2-command 'offset)))
663 (symbol-value line-sym)
664 -1)))))
666 (defun YaTeX-prev-error ()
667 "Visit position of previous typeset error or warning.
668 To avoid making confliction of line numbers by editing, jump to
669 error or warning lines in reverse order."
670 (interactive)
671 (let ((cur-buf (save-excursion (YaTeX-visit-main t) (buffer-name)))
672 (cur-win (selected-window))
673 b0 bound errorp error-line typeset-win error-buffer error-win)
674 (if (null (get-buffer YaTeX-typeset-buffer))
675 (error "There is no typesetting buffer."))
676 (YaTeX-showup-buffer YaTeX-typeset-buffer nil t)
677 (if (and (markerp YaTeX-typeset-marker)
678 (eq (marker-buffer YaTeX-typeset-marker) (current-buffer)))
679 (setq bound YaTeX-typeset-marker))
680 (setq typeset-win (selected-window))
681 (if (re-search-backward
682 (concat "\\(" latex-error-regexp "\\)\\|\\("
683 latex-warning-regexp "\\)")
684 bound t)
685 (setq errorp (match-beginning 1))
686 (select-window cur-win)
687 (error "No more errors on %s" cur-buf))
688 (goto-char (setq b0 (match-beginning 0)))
689 (skip-chars-forward "^0-9" (match-end 0))
690 (setq error-line
691 (string-to-int
692 (buffer-substring
693 (point)
694 (progn (skip-chars-forward "0-9" (match-end 0)) (point))))
695 error-buffer (expand-file-name (YaTeX-get-error-file cur-buf)))
696 (if (or (null error-line) (equal 0 error-line))
697 (error "Can't detect error position."))
698 (YaTeX-set-virtual-error-position 'error-buffer 'error-line)
699 (setq error-win (get-buffer-window error-buffer))
700 (select-window cur-win)
701 (cond
702 (error-win (select-window error-win))
703 ((eq (get-lru-window) typeset-win)
704 (YaTeX-switch-to-buffer error-buffer))
705 (t (select-window (get-lru-window))
706 (YaTeX-switch-to-buffer error-buffer)))
707 (setq error-win (selected-window))
708 (goto-line error-line)
709 (message "LaTeX %s in `%s' on line: %d."
710 (if errorp "error" "warning")
711 error-buffer error-line)
712 (select-window typeset-win)
713 (skip-chars-backward "0-9")
714 (recenter (/ (window-height) 2))
715 (sit-for 1)
716 (goto-char b0)
717 (select-window error-win)))
719 (defun YaTeX-jump-error-line ()
720 "Jump to corresponding line on latex command's error message."
721 (interactive)
722 (let (error-line error-file error-buf)
723 (save-excursion
724 (beginning-of-line)
725 (setq error-line (re-search-forward "l[ ines]*\\.?\\([1-9][0-9]*\\)"
726 (point-end-of-line) t)))
727 (if (null error-line)
728 (if (eobp) (insert (this-command-keys))
729 (error "No line number expression."))
730 (goto-char (match-beginning 0))
731 (setq error-line (string-to-int
732 (buffer-substring (match-beginning 1) (match-end 1)))
733 error-file (expand-file-name
734 (YaTeX-get-error-file YaTeX-current-TeX-buffer)))
735 (YaTeX-set-virtual-error-position 'error-file 'error-line)
736 (setq error-buf (YaTeX-switch-to-buffer error-file t)))
737 (if (null error-buf)
738 (error "`%s' is not found in this directory." error-file))
739 (YaTeX-showup-buffer error-buf nil t)
740 (goto-line error-line)))
742 (defun YaTeX-send-string ()
743 "Send string to current typeset process."
744 (interactive)
745 (if (and (eq (process-status YaTeX-typeset-process) 'run)
746 (>= (point) (process-mark YaTeX-typeset-process)))
747 (let ((b (process-mark YaTeX-typeset-process))
748 (e (point-end-of-line)))
749 (goto-char b)
750 (skip-chars-forward " \t" e)
751 (setq b (point))
752 (process-send-string
753 YaTeX-typeset-process (concat (buffer-substring b e) "\n"))
754 (goto-char e)
755 (insert "\n")
756 (set-marker (process-mark YaTeX-typeset-process) (point))
757 (insert " "))
758 (ding)))
760 (defun YaTeX-view-error ()
761 (interactive)
762 (if (null (get-buffer YaTeX-typeset-buffer))
763 (message "No typeset buffer found.")
764 (let ((win (selected-window)))
765 (YaTeX-showup-buffer YaTeX-typeset-buffer nil t)
766 ;; Next 3 lines are obsolete because YaTeX-typesetting-buffer is
767 ;; automatically scrolled up at typesetting.
768 ;;(goto-char (point-max))
769 ;;(forward-line -1)
770 ;;(recenter -1)
771 (select-window win))))
773 (defun YaTeX-get-error-file (default)
774 "Get current processing file from typesetting log."
775 (save-excursion
776 (let(s)
777 (condition-case () (up-list -1)
778 (error
779 (let ((list 0) found)
780 (while
781 (and (<= list 0) (not found)
782 (re-search-backward "\\((\\)\\|\\()\\)" nil t))
783 (if (equal (match-beginning 0) (match-beginning 2)) ;close paren.
784 (setq list (1- list)) ;open paren
785 (setq list (1+ list))
786 (if (= list 1)
787 (if (looking-at "\\([^,{}%]+\.\\)tex\\|sty")
788 (setq found t)
789 (setq list (1- list)))))))))
790 (setq s
791 (buffer-substring
792 (progn (forward-char 1) (point))
793 (progn (skip-chars-forward "^ \n" (point-end-of-line))
794 (point))))
795 (if (string= "" s) default s))))
797 (defun YaTeX-put-nonstopmode ()
798 (if (and (eq major-mode 'yatex-mode) YaTeX-need-nonstop)
799 (if (re-search-backward "\\\\nonstopmode{}" (point-min) t)
800 nil ;if already written in text then do nothing
801 (save-excursion
802 (YaTeX-visit-main t)
803 (goto-char (point-min))
804 (insert "\\nonstopmode{}%_YaTeX_%\n")
805 (if (buffer-file-name) (basic-save-buffer))))))
807 (defun YaTeX-remove-nonstopmode ()
808 (if (and (eq major-mode 'yatex-mode) YaTeX-need-nonstop) ;for speed
809 (save-excursion
810 (YaTeX-visit-main t)
811 (goto-char (point-min))
812 (forward-line 1)
813 (narrow-to-region (point-min) (point))
814 (goto-char (point-min))
815 (delete-matching-lines "^\\\\nonstopmode\\{\\}%_YaTeX_%$")
816 (widen))))
818 (defvar YaTeX-dvi2-command-ext-alist
819 '(("[agx]dvi\\|dviout" . ".dvi")
820 ("ghostview\\|gv" . ".ps")
821 ("acroread\\|pdf\\|Preview\\|TeXShop\\|Skim\\|evince" . ".pdf")))
823 (defun YaTeX-get-preview-file-name (&optional preview-command)
824 "Get file name to preview by inquiring YaTeX-get-latex-command"
825 (if (null preview-command) (setq preview-command dvi2-command))
826 (let* ((latex-cmd (YaTeX-get-latex-command t))
827 (rin (rindex latex-cmd ? ))
828 (fname (if rin (substring latex-cmd (1+ rin)) ""))
829 (r (YaTeX-assoc-regexp preview-command YaTeX-dvi2-command-ext-alist))
830 (ext (if r (cdr r) "")))
831 (concat
832 (if (string= fname "")
833 (setq fname (substring (file-name-nondirectory
834 (buffer-file-name))
835 0 -4))
836 (setq fname (substring fname 0 (rindex fname ?.))))
837 ext)))
839 (defun YaTeX-get-latex-command (&optional switch)
840 "Specify the latex-command name and its argument.
841 If there is a line which begins with string: \"%#!\", the following
842 strings are assumed to be the latex-command and arguments. The
843 default value of latex-command is:
844 tex-command FileName
845 and if you write \"%#!jlatex\" in the beginning of certain line.
846 \"jlatex \" FileName
847 will be the latex-command,
848 and you write \"%#!jlatex main.tex\" on some line and argument SWITCH
849 is non-nil, then
850 \"jlatex main.tex\"
852 will be given to the shell."
853 (let (parent tparent magic)
854 (setq parent
855 (cond
856 (YaTeX-parent-file
857 (if YaTeX-dos (expand-file-name YaTeX-parent-file)
858 YaTeX-parent-file))
859 (t (save-excursion
860 (YaTeX-visit-main t)
861 (file-name-nondirectory (buffer-file-name)))))
862 magic (YaTeX-get-builtin "!")
863 tparent (file-name-nondirectory parent))
864 (YaTeX-replace-formats
865 (cond
866 (magic
867 (cond
868 (switch (if (string-match "\\s " magic) magic
869 (concat magic " " parent)))
870 (t (concat (substring magic 0 (string-match "\\s " magic)) " "))))
871 (t (concat tex-command " " (if switch parent))))
872 (list (cons "f" tparent)
873 (cons "r" (substring tparent 0 (rindex tparent ?.)))))))
875 (defvar YaTeX-lpr-command-history nil
876 "Holds command line history of YaTeX-lpr.")
877 (put 'YaTeX-lpr-command-history 'no-default t)
878 (defvar YaTeX-lpr-ask-page-range-default t)
879 (defun YaTeX-lpr (arg)
880 "Print out.
881 If prefix arg ARG is non nil, call print driver without
882 page range description."
883 (interactive "P")
884 (or YaTeX-lpr-ask-page-range-default (setq arg (not arg)))
885 (let*((cmd (or (YaTeX-get-builtin "LPR") dviprint-command-format))
886 from to (lbuffer "*dvi-printing*") dir)
887 (setq
888 cmd
889 (YaTeX-replace-format
890 cmd "f"
891 (if (or arg (not (string-match "%f" cmd)))
892 ""
893 (YaTeX-replace-format
894 dviprint-from-format
895 "b"
896 (if (string=
897 (setq from (read-string "From page(default 1): ")) "")
898 "1" from))))
899 )
900 (setq
901 cmd
902 (YaTeX-replace-format
903 cmd "t"
904 (if (or arg (not (string-match "%t" cmd))
905 (string=
906 (setq to (read-string "To page(default none): ")) ""))
907 ""
908 (YaTeX-replace-format dviprint-to-format "e" to)))
909 )
910 (setq
911 cmd
912 (YaTeX-replace-format
913 cmd "p"
914 (cdr (assoc (YaTeX-get-paper-type) YaTeX-dvips-paper-option-alist))))
915 (setq cmd
916 (read-string-with-history
917 "Edit command line: "
918 (format cmd
919 (if (get 'dvi2-command 'region)
920 (substring YaTeX-texput-file
921 0 (rindex YaTeX-texput-file ?.))
922 (YaTeX-get-preview-file-name)))
923 'YaTeX-lpr-command-history))
924 (save-excursion
925 (YaTeX-visit-main t) ;;change execution directory
926 (setq dir default-directory)
927 (YaTeX-showup-buffer
928 lbuffer (function (lambda (x) (nth 3 (window-edges x)))))
929 (set-buffer (get-buffer-create lbuffer))
930 (erase-buffer)
931 (cd dir) ;for 19
932 (cond
933 ((not (fboundp 'start-process))
934 (call-process shell-file-name "con" "*dvi-printing*" nil
935 YaTeX-shell-command-option cmd))
936 (t
937 (set-process-buffer
938 (let ((process-connection-type nil))
939 (start-process "print" "*dvi-printing*" shell-file-name
940 YaTeX-shell-command-option cmd))
941 (get-buffer lbuffer))
942 (message "Starting printing command: %s..." cmd))))))
944 (defun YaTeX-main-file-p ()
945 "Return if current buffer is main LaTeX source."
946 (cond
947 (YaTeX-parent-file
948 (eq (get-file-buffer YaTeX-parent-file) (current-buffer)))
949 ((YaTeX-get-builtin "!")
950 (string-match
951 (concat "^" (YaTeX-guess-parent (YaTeX-get-builtin "!")))
952 (buffer-name)))
953 (t
954 (save-excursion
955 (let ((latex-main-id
956 (concat "^\\s *" YaTeX-ec-regexp "document\\(style\\|class\\)")))
957 (or (re-search-backward latex-main-id nil t)
958 (re-search-forward latex-main-id nil t)))))))
960 (defun YaTeX-visit-main (&optional setbuf)
961 "Switch buffer to main LaTeX source.
962 Use set-buffer instead of switch-to-buffer if the optional argument
963 SETBUF is t(Use it only from Emacs-Lisp program)."
964 (interactive "P")
965 (if (and (interactive-p) setbuf) (setq YaTeX-parent-file nil))
966 (let ((ff (function (lambda (f)
967 (if setbuf (set-buffer (find-file-noselect f))
968 (find-file f)))))
969 b-in main-file YaTeX-create-file-prefix-g
970 (hilit-auto-highlight (not setbuf)))
971 (if (setq b-in (YaTeX-get-builtin "!"))
972 (setq main-file (YaTeX-guess-parent b-in)))
973 (if YaTeX-parent-file
974 (setq main-file ;;(get-file-buffer YaTeX-parent-file)
975 YaTeX-parent-file))
976 (if (YaTeX-main-file-p)
977 (if (interactive-p) (message "I think this is main LaTeX source.") nil)
978 (cond
979 ((and ;;(interactive-p)
980 main-file
981 (cond ((get-file-buffer main-file)
982 (cond
983 (setbuf (set-buffer (get-file-buffer main-file)))
984 ((get-buffer-window (get-file-buffer main-file))
985 (select-window
986 (get-buffer-window (get-file-buffer main-file))))
987 (t (switch-to-buffer (get-file-buffer main-file)))))
988 ((file-exists-p main-file)
989 (funcall ff main-file)))))
990 ;;((and main-file (YaTeX-switch-to-buffer main-file setbuf)))
991 ((and main-file
992 (file-exists-p (setq main-file (concat "../" main-file)))
993 (or b-in
994 (y-or-n-p (concat (expand-file-name main-file)
995 " is main file?:"))))
996 (setq YaTeX-parent-file main-file)
997 ;(YaTeX-switch-to-buffer main-file setbuf)
998 (funcall ff main-file)
999 )
1000 (t (setq main-file (read-file-name "Enter your main text: " nil nil 1))
1001 (setq YaTeX-parent-file main-file)
1002 ; (YaTeX-switch-to-buffer main-file setbuf))
1003 (funcall ff main-file))
1004 )))
1005 nil)
1007 (defun YaTeX-guess-parent (command-line)
1008 (setq command-line
1009 (if (string-match ".*\\s " command-line)
1010 (substring command-line (match-end 0))
1011 (file-name-nondirectory (buffer-file-name)))
1012 command-line
1013 (concat (if (string-match "\\(.*\\)\\." command-line)
1014 (substring command-line (match-beginning 1) (match-end 1))
1015 command-line)
1016 ".tex")))
1018 (defun YaTeX-visit-main-other-window ()
1019 "Switch to buffer main LaTeX source in other window."
1020 (interactive)
1021 (if (YaTeX-main-file-p) (message "I think this is main LaTeX source.")
1022 (YaTeX-switch-to-buffer-other-window
1023 (concat (YaTeX-get-preview-file-name) ".tex"))))
1025 (defun YaTeX-save-buffers ()
1026 "Save buffers whose major-mode is equal to current major-mode."
1027 (basic-save-buffer)
1028 (let ((cmm major-mode))
1029 (save-excursion
1030 (mapcar '(lambda (buf)
1031 (set-buffer buf)
1032 (if (and (buffer-file-name buf)
1033 (eq major-mode cmm)
1034 (buffer-modified-p buf)
1035 (y-or-n-p (format "Save %s" (buffer-name buf))))
1036 (save-buffer buf)))
1037 (buffer-list)))))
1039 (provide 'yatexprc)