yatex

view yatexprc.el @ 256:b1e70dc23208

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