yatex

view yatexprc.el @ 224:9705855793ca

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