yatex

view yatexprc.el @ 262:15ec86ff549c

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