yatex

view yatexprc.el @ 354:5465428f5a68

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