yatex

view yatexprc.el @ 255:214702e4df54

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