yatex

view yatexprc.el @ 283:95e8bb2a5c5f

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