yatex

view yatexprc.el @ 361:41a05a44826a

[pdftex] option for pdflatex
author HIROSE Yuuji <yuuji@gentei.org>
date Tue, 23 Dec 2014 15:34:31 +0900
parents 6c67a826f266
children b1e6c8c03253
line source
1 ;;; yatexprc.el --- YaTeX process handler
2 ;;;
3 ;;; (c)1993-2013 by HIROSE Yuuji.[yuuji@yatex.org]
4 ;;; Last modified Tue Dec 23 11:53:48 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-preview-image-mode-map nil
401 "Keymap used in YaTeX-preview-image-mode")
402 (defun YaTeX-preview-image-mode ()
403 (interactive)
404 (if YaTeX-preview-image-mode-map
405 nil
406 (let ((map (setq YaTeX-preview-image-mode-map (make-sparse-keymap))))
407 (define-key map "q" (lambda()(interactive)
408 (kill-buffer)
409 (select-window
410 (or (get 'YaTeX-typeset-process 'win)
411 (selected-window)))))
412 (define-key map "j" (lambda()(interactive) (scroll-up 1)))
413 (define-key map "k" (lambda()(interactive) (scroll-up -1)))))
414 (use-local-map YaTeX-preview-image-mode-map))
416 (defvar YaTeX-typeset-conv2image-process nil "Process of conv2image chain")
417 (defun YaTeX-typeset-conv2image-chain ()
418 (let*((proc (or YaTeX-typeset-process YaTeX-typeset-conv2image-process))
419 (prevname (process-name proc))
420 (target "texput.png")
421 (math (get 'YaTeX-typeset-conv2image-chain 'math))
422 ;(conv (format "convert -density %d - %s" (if math 250 100) target))
423 ;(chain (list (format "dvips -E -o - texput|%s" conv)))
424 (conv (format "convert -alpha off - %s" target))
425 (chain (list (format "dvips -x 2000 -E -o - texput|%s" conv)))
426 (curproc (member prevname chain))
427 (w (get 'YaTeX-typeset-conv2image-chain 'win))
428 (pwd default-directory)
429 img)
430 (if (not (= (process-exit-status proc) 0))
431 (progn
432 (YaTeX-showup-buffer ;nevers comes here(?)
433 (current-buffer) 'YaTeX-showup-buffer-bottom-most)
434 (message "Region typesetting FAILED"))
435 (setq command
436 (if curproc (car (cdr-safe curproc)) (car chain)))
437 (if command
438 (progn
439 (insert (format "Calling `%s'...\n" command))
440 (set-process-sentinel
441 (setq YaTeX-typeset-conv2image-process
442 (start-process
443 command
444 (current-buffer)
445 shell-file-name YaTeX-shell-command-option command))
446 'YaTeX-typeset-sentinel)
447 (put 'YaTeX-typeset-process 'ppcmd
448 (cons (cons (get-buffer-process (current-buffer))
449 'YaTeX-typeset-conv2image-chain)
450 (get 'YaTeX-typeset-process 'ppcmd))))
451 ;; After all chain executed, display image in current window
452 (select-window w)
453 (setq foo (selected-window))
454 (YaTeX-showup-buffer
455 (get-buffer-create " *YaTeX-region-image*")
456 'YaTeX-showup-buffer-bottom-most t)
457 (remove-images (point-min) (point-max))
458 (erase-buffer)
459 (cd pwd) ;when reuse from other source
460 ;(put-image (create-image (expand-file-name target)) (point))
461 (insert-image-file target)
462 (setq img (plist-get (text-properties-at (point)) 'intangible))
463 (YaTeX-preview-image-mode)
464 (if img
465 (let ((height (cdr (image-size img))))
466 (enlarge-window
467 (- (ceiling (min height (/ (frame-height) 2)))
468 (window-height)))))))))
470 (defun YaTeX-typeset-environment ()
471 "Typeset current math environment"
472 (interactive)
473 (save-excursion
474 (let ((math (YaTeX-in-math-mode-p)))
475 (or (equal (YaTeX-inner-environment t) "document")
476 (YaTeX-mark-environment))
477 (if (and (featurep 'image) window-system)
478 (let ((YaTeX-typeset-buffer (concat "*bg:" YaTeX-typeset-buffer)))
479 (put 'YaTeX-typeset-conv2image-chain 'math math)
480 (put 'YaTeX-typeset-conv2image-chain 'win (selected-window))
481 (YaTeX-typeset-region 'YaTeX-typeset-conv2image-chain))
482 (YaTeX-typeset-region)))))
484 (defun YaTeX-typeset-buffer (&optional pp)
485 "Typeset whole buffer.
486 If %#! usage says other buffer is main text,
487 visit main buffer to confirm if its includeonly list contains current
488 buffer's file. And if it doesn't contain editing text, ask user which
489 action wants to be done, A:Add list, R:Replace list, %:comment-out list.
490 If optional argument PP given as string, PP is considered as post-process
491 command and call it with the same command argument as typesetter without
492 last extension.
493 eg. if PP is \"dvipdfmx\", called commands as follows.
494 platex foo.tex
495 dvipdfmx foo
496 PP command will be called iff typeset command exit successfully"
497 (interactive)
498 (YaTeX-save-buffers)
499 (let*((me (substring (buffer-name) 0 (rindex (buffer-name) ?.)))
500 (mydir (file-name-directory (buffer-file-name)))
501 (cmd (YaTeX-get-latex-command t)) pparg ppcmd bibcmd
502 (cb (current-buffer)))
503 (setq pparg (substring cmd 0 (string-match "[;&]" cmd)) ;rm multistmt
504 pparg (substring pparg (rindex pparg ? )) ;get last arg
505 pparg (substring pparg 0 (rindex pparg ?.)) ;rm ext
506 bibcmd (or (YaTeX-get-builtin "BIBTEX") bibtex-command))
507 (or (string-match "\\s " bibcmd) ;if bibcmd has no spaces,
508 (setq bibcmd (concat bibcmd pparg))) ;append argument(== %#!)
509 (and pp
510 (stringp pp)
511 (setq ppcmd (concat pp pparg)))
512 (if (YaTeX-main-file-p) nil
513 (save-excursion
514 (YaTeX-visit-main t) ;search into main buffer
515 (save-excursion
516 (push-mark (point) t)
517 (goto-char (point-min))
518 (if (and (re-search-forward "^[ ]*\\\\begin{document}" nil t)
519 (re-search-backward "^[ ]*\\\\includeonly{" nil t))
520 (let*
521 ((b (progn (skip-chars-forward "^{") (point)))
522 (e (progn (skip-chars-forward "^}") (1+ (point))))
523 (s (buffer-substring b e)) c
524 (pardir (file-name-directory (buffer-file-name))))
525 (if (string-match (concat "[{,/]" me "[,}]") s)
526 nil ; Nothing to do when it's already in includeonly.
527 (ding)
528 (switch-to-buffer (current-buffer));Display this buffer.
529 (setq
530 me ;;Rewrite my name(me) to contain sub directory name.
531 (concat
532 (if (string-match pardir mydir) ;if mydir is child of main
533 (substring mydir (length pardir)) ;cut absolute path
534 mydir) ;else concat absolute path name.
535 me))
536 (message
537 "`%s' is not in \\includeonly. A)dd R)eplace %%)comment? "
538 me)
539 (setq c (read-char))
540 (cond
541 ((= c ?a)
542 (goto-char (1+ b))
543 (insert me (if (string= s "{}") "" ",")))
544 ((= c ?r)
545 (delete-region (1+ b) (1- e)) (insert me))
546 ((= c ?%)
547 (beginning-of-line) (insert "%"))
548 (t nil))
549 (basic-save-buffer))))
550 (exchange-point-and-mark)))
551 (switch-to-buffer cb)) ;for 19
552 (YaTeX-typeset cmd YaTeX-typeset-buffer nil nil ppcmd)
553 (put 'dvi2-command 'region nil)))
555 (defvar YaTeX-call-command-history nil
556 "Holds history list of YaTeX-call-command-on-file.")
557 (put 'YaTeX-call-command-history 'no-default t)
558 (defun YaTeX-call-command-on-file (base-cmd buffer &optional file)
559 "Call external command BASE-CMD in the BUFFER.
560 By default, pass the basename of current file. Optional 3rd argument
561 FILE changes the default file name."
562 (YaTeX-save-buffers)
563 (let ((default (concat base-cmd " "
564 (let ((me (file-name-nondirectory
565 (or file buffer-file-name))))
566 (if (string-match "\\.tex" me)
567 (substring me 0 (match-beginning 0))
568 me)))))
569 (or YaTeX-call-command-history
570 (setq YaTeX-call-command-history (list default)))
571 (YaTeX-typeset
572 (read-string-with-history
573 "Call command: "
574 (car YaTeX-call-command-history)
575 'YaTeX-call-command-history)
576 buffer)))
578 (defvar YaTeX-call-builtin-on-file)
579 (make-variable-buffer-local 'YaTeX-call-builtin-on-file)
580 (defun YaTeX-call-builtin-on-file (builtin-type &optional default update)
581 "Call command on file specified by BUILTIN-TYPE."
582 (YaTeX-save-buffers)
583 (let*((main (or YaTeX-parent-file
584 (save-excursion (YaTeX-visit-main t) buffer-file-name)))
585 (mainroot (file-name-nondirectory (substring main 0 (rindex main ?.))))
586 (alist YaTeX-call-builtin-on-file)
587 (b-in (or (YaTeX-get-builtin builtin-type)
588 (cdr (assoc builtin-type alist))))
589 (command b-in))
590 (if (or update (null b-in))
591 (progn
592 (setq command (read-string-with-history
593 (format "%s command: " builtin-type)
594 (or b-in
595 (format "%s %s" default mainroot))
596 'YaTeX-call-command-history))
597 (if (or update (null b-in))
598 (if (y-or-n-p "Use this command line in the future? ")
599 (YaTeX-getset-builtin builtin-type command) ;keep in a file
600 (setq YaTeX-call-builtin-on-file ;keep in memory
601 (cons (cons builtin-type command)
602 (delete (assoc builtin-type alist) alist)))))))
603 (YaTeX-typeset
604 command
605 (format " *YaTeX-%s*" (downcase builtin-type)))))
607 (defun YaTeX-kill-typeset-process (proc)
608 "Kill process PROC after sending signal to PROC.
609 PROC should be process identifier."
610 (cond
611 ((not (fboundp 'start-process))
612 (error "This system can't have concurrent process."))
613 ((or (null proc) (not (eq (process-status proc) 'run)))
614 (message "Typesetting process is not running."))
615 (t
616 (save-excursion
617 (set-buffer (process-buffer proc))
618 (save-excursion
619 (goto-char (point-max))
620 (beginning-of-line)
621 (if (looking-at "\\? +$")
622 (let ((mp (point-max)))
623 (process-send-string proc "x\n")
624 (while (= mp (point-max)) (sit-for 1))))))
625 (if (eq (process-status proc) 'run)
626 (progn
627 (interrupt-process proc)
628 (delete-process proc))))))
630 (defun YaTeX-system (command name &optional noask basedir)
631 "Execute some COMMAND with process name `NAME'. Not a official function.
632 Optional second argument NOASK skip query when privious process running.
633 Optional third argument BASEDIR changes default-directory there."
634 (save-excursion
635 (let ((df default-directory)
636 (buffer (get-buffer-create (format " *%s*" name)))
637 proc status)
638 (set-buffer buffer)
639 (setq default-directory (cd (or basedir df)))
640 (erase-buffer)
641 (insert (format "Calling `%s'..." command))
642 (YaTeX-showup-buffer buffer 'YaTeX-showup-buffer-bottom-most)
643 (if (not (fboundp 'start-process))
644 (call-process
645 shell-file-name nil buffer nil YaTeX-shell-command-option command)
646 (if (and (setq proc (get-buffer-process buffer))
647 (setq status (process-status proc))
648 (eq status 'run)
649 (not noask)
650 (not
651 (y-or-n-p (format "Process %s is running. Continue?" buffer))))
652 nil
653 (if (eq status 'run)
654 (progn (interrupt-process proc) (delete-process proc)))
655 (set-process-buffer
656 (start-process
657 name buffer shell-file-name YaTeX-shell-command-option command)
658 (get-buffer buffer)))))))
660 (defvar YaTeX-default-paper-type "a4"
661 "*Default paper type.")
662 (defconst YaTeX-paper-type-alist
663 '(("a4paper" . "a4")
664 ("a5paper" . "a5")
665 ("b4paper" . "b4")
666 ("b5paper" . "b5"))
667 "Holds map of options and paper types.")
668 (defconst YaTeX-dvips-paper-option-alist
669 '(("a4" . "-t a4")
670 ("a5" . "-t a5")
671 ("b4" . "-t b4")
672 ("b5" . "-t b5")
673 ("a4r" . "-t landscape"); Can't specify options, `-t a4' and `-t landscape', at the same time.
674 ("a5r" . "-t landscape")
675 ("b4r" . "-t landscape")
676 ("b5r" . "-t landscape"))
677 "Holds map of dvips options and paper types.")
678 (defun YaTeX-get-paper-type ()
679 "Search options in header and return a paper type, such as \"a4\", \"a4r\", etc."
680 (save-excursion
681 (YaTeX-visit-main t)
682 (goto-char (point-min))
683 (let ((opts
684 (if (re-search-forward
685 "^[ \t]*\\\\document\\(style\\|class\\)[ \t]*\\[\\([^]]*\\)\\]" nil t)
686 (YaTeX-split-string (YaTeX-match-string 2) "[ \t]*,[ \t]*"))))
687 (concat
688 (catch 'found-paper
689 (mapcar (lambda (pair)
690 (if (YaTeX-member (car pair) opts)
691 (throw 'found-paper (cdr pair))))
692 YaTeX-paper-type-alist)
693 YaTeX-default-paper-type)
694 (if (YaTeX-member "landscape" opts) (if YaTeX-dos "L" "r") "")))))
696 (defvar YaTeX-preview-command-history nil
697 "Holds minibuffer history of preview command.")
698 (put 'YaTeX-preview-command-history 'no-default t)
699 (defvar YaTeX-preview-file-history nil
700 "Holds minibuffer history of file to preview.")
701 (put 'YaTeX-preview-file-history 'no-default t)
702 (defun YaTeX-preview-default-previewer ()
703 "Return default previewer for this document"
704 (YaTeX-replace-format
705 (or (YaTeX-get-builtin "PREVIEW")
706 (if (eq (get 'dvi2-command 'format) 'pdf)
707 tex-pdfview-command
708 dvi2-command))
709 "p" (format (cond
710 (YaTeX-dos "-y:%s")
711 (t "-paper %s"))
712 (YaTeX-get-paper-type))))
713 (defun YaTeX-preview-default-main (command)
714 "Return default preview target file"
715 (if (get 'dvi2-command 'region)
716 (substring YaTeX-texput-file
717 0 (rindex YaTeX-texput-file ?.))
718 (YaTeX-get-preview-file-name command)))
719 (defun YaTeX-preview (preview-command preview-file &optional as-default)
720 "Execute xdvi (or other) to tex-preview."
721 (interactive
722 (let* ((previewer (YaTeX-preview-default-previewer))
723 (as-default current-prefix-arg)
724 (command (if as-default
725 previewer
726 (read-string-with-history
727 "Preview command: "
728 previewer
729 'YaTeX-preview-command-history)))
730 (target (YaTeX-preview-default-main command))
731 (file (if as-default
732 target
733 (read-string-with-history
734 "Preview file: "
735 target
736 'YaTeX-preview-file-history))))
737 (list command file)))
738 (setq dvi2-command preview-command) ;`dvi2-command' is buffer local
739 (save-excursion
740 (YaTeX-visit-main t)
741 (if YaTeX-dos (setq preview-file (expand-file-name preview-file)))
742 (let ((pbuffer "*dvi-preview*") (dir default-directory))
743 (YaTeX-showup-buffer
744 pbuffer 'YaTeX-showup-buffer-bottom-most)
745 (set-buffer (get-buffer-create pbuffer))
746 (erase-buffer)
747 (setq default-directory dir) ;for 18
748 (cd dir) ;for 19
749 (cond
750 ((not (fboundp 'start-process)) ;if MS-DOS
751 (send-string-to-terminal "\e[2J\e[>5h") ;CLS & hide cursor
752 (call-process shell-file-name "con" "*dvi-preview*" nil
753 YaTeX-shell-command-option
754 (concat preview-command " " preview-file))
755 (send-string-to-terminal "\e[>5l") ;show cursor
756 (redraw-display))
757 ((and (string-match "dviout" preview-command) ;maybe on `kon'
758 (stringp (getenv "TERM"))
759 (string-match "^kon" (getenv "TERM")))
760 (call-process shell-file-name "con" "*dvi-preview*" nil
761 YaTeX-shell-command-option
762 (concat preview-command " " preview-file)))
763 (t ;if UNIX
764 (set-process-buffer
765 (let ((process-connection-type nil))
766 (start-process "preview" "*dvi-preview*" shell-file-name
767 YaTeX-shell-command-option
768 (concat preview-command " " preview-file)))
769 (get-buffer pbuffer))
770 (message
771 (concat "Starting " preview-command
772 " to preview " preview-file)))))))
774 (defvar YaTeX-xdvi-remote-program "xdvi")
775 (defun YaTeX-xdvi-remote-search (&optional region-mode)
776 "Search string at the point on xdvi -remote window.
777 Non-nil for optional argument REGION-MODE specifies the search string
778 by region."
779 (interactive "P")
780 (let ((pb " *xdvi*") str proc)
781 (save-excursion
782 (if region-mode
783 (setq str (buffer-substring (region-beginning) (region-end)))
784 (setq str (buffer-substring
785 (point)
786 (progn (skip-chars-forward "^\n\\\\}") (point)))))
787 (message "Searching `%s'..." str)
788 (if (boundp 'MULE)
789 (define-program-coding-system
790 (regexp-quote pb) (regexp-quote YaTeX-xdvi-remote-program)
791 *euc-japan*))
792 (setq proc
793 (start-process
794 "xdvi" pb YaTeX-xdvi-remote-program
795 "-remote" (format "SloppySearch(%s) " str)
796 (concat (YaTeX-get-preview-file-name) ".dvi")))
797 (message "Searching `%s'...Done" str))))
799 (defun YaTeX-preview-jlfmt-xdvi ()
800 "Call xdvi -sourceposition to DVI corresponding to current main file"
801 (interactive))
803 (defun YaTeX-preview-jump-line ()
804 "Call jump-line function of various previewer on current main file"
805 (interactive)
806 (save-excursion
807 (save-restriction
808 (widen)
809 (let*((pf (or YaTeX-parent-file
810 (save-excursion (YaTeX-visit-main t) (buffer-file-name))))
811 (pdir (file-name-directory pf))
812 (bnr (substring pf 0 (string-match "\\....$" pf)))
813 (cf (file-relative-name (buffer-file-name) pdir))
814 (buffer (get-buffer-create " *preview-jump-line*"))
815 (line (count-lines (point-min) (point-end-of-line)))
816 (previewer (YaTeX-preview-default-previewer))
817 (cmd (cond
818 ((string-match "xdvi" previewer)
819 (format "%s -nofork -sourceposition '%d %s' %s.dvi"
820 YaTeX-xdvi-remote-program
821 line cf bnr))
822 ((string-match "Skim" previewer)
823 (format "%s %d '%s.pdf' '%s'"
824 YaTeX-cmd-displayline line bnr cf))
825 ((string-match "sumatra" previewer) ;;??
826 (format "%s \"%s.pdf\" -forward-search \"%s\" %d %s"
827 previewer bnr cf line))
828 ((string-match "evince" previewer)
829 (format "%s '%s.pdf' %d '%s'"
830 "fwdevince" bnr line cf))
831 ((string-match "qpdfview" previewer) ;;??
832 (format "%s '%s.pdf' '#src:%s:%d:0'"
833 previewer bnr cf line))
834 ((string-match "okular" previewer) ;;??
835 (format "%s '%s.pdf' '#src:%d' '%s'"
836 previewer bnr line cf)))))
837 (YaTeX-system cmd "jump-line" 'noask pdir)))))
839 (defun YaTeX-goto-corresponding-viewer ()
840 (let ((cmd (or (YaTeX-get-builtin "!") tex-command)))
841 (if (string-match "-src\\|synctex=" cmd)
842 (progn
843 (YaTeX-preview-jump-line)
844 t) ;for YaTeX-goto-corresponding-*
845 nil)))
847 (defun YaTeX-set-virtual-error-position (file-sym line-sym)
848 "Replace the value of FILE-SYM, LINE-SYM by virtual error position."
849 (cond
850 ((and (get 'dvi2-command 'region)
851 (> (symbol-value line-sym) (car (get 'dvi2-command 'offset))))
852 (set file-sym (get 'dvi2-command 'file))
853 (set line-sym
854 (+ (- (apply '- (get 'dvi2-command 'offset)))
855 (symbol-value line-sym)
856 -1)))))
858 (defun YaTeX-prev-error ()
859 "Visit position of previous typeset error or warning.
860 To avoid making confliction of line numbers by editing, jump to
861 error or warning lines in reverse order."
862 (interactive)
863 (let ((cur-buf (save-excursion (YaTeX-visit-main t) (buffer-name)))
864 (cur-win (selected-window))
865 b0 bound errorp error-line typeset-win error-buffer error-win)
866 (if (null (get-buffer YaTeX-typeset-buffer))
867 (error "There is no typesetting buffer."))
868 (YaTeX-showup-buffer YaTeX-typeset-buffer nil t)
869 (if (and (markerp YaTeX-typeset-marker)
870 (eq (marker-buffer YaTeX-typeset-marker) (current-buffer)))
871 (setq bound YaTeX-typeset-marker))
872 (setq typeset-win (selected-window))
873 (if (re-search-backward
874 (concat "\\(" latex-error-regexp "\\)\\|\\("
875 latex-warning-regexp "\\)")
876 bound t)
877 (setq errorp (match-beginning 1))
878 (select-window cur-win)
879 (error "No more errors on %s" cur-buf))
880 (goto-char (setq b0 (match-beginning 0)))
881 (skip-chars-forward "^0-9" (match-end 0))
882 (setq error-line
883 (string-to-int
884 (buffer-substring
885 (point)
886 (progn (skip-chars-forward "0-9" (match-end 0)) (point))))
887 error-buffer (expand-file-name (YaTeX-get-error-file cur-buf)))
888 (if (or (null error-line) (equal 0 error-line))
889 (error "Can't detect error position."))
890 (YaTeX-set-virtual-error-position 'error-buffer 'error-line)
891 (setq error-win (get-buffer-window error-buffer))
892 (select-window cur-win)
893 (cond
894 (error-win (select-window error-win))
895 ((eq (get-lru-window) typeset-win)
896 (YaTeX-switch-to-buffer error-buffer))
897 (t (select-window (get-lru-window))
898 (YaTeX-switch-to-buffer error-buffer)))
899 (setq error-win (selected-window))
900 (goto-line error-line)
901 (message "LaTeX %s in `%s' on line: %d."
902 (if errorp "error" "warning")
903 error-buffer error-line)
904 (select-window typeset-win)
905 (skip-chars-backward "0-9")
906 (recenter (/ (window-height) 2))
907 (sit-for 1)
908 (goto-char b0)
909 (select-window error-win)))
911 (defun YaTeX-jump-error-line ()
912 "Jump to corresponding line on latex command's error message."
913 (interactive)
914 (let (error-line error-file error-buf)
915 (save-excursion
916 (beginning-of-line)
917 (setq error-line (re-search-forward "l[ ines]*\\.?\\([1-9][0-9]*\\)"
918 (point-end-of-line) t)))
919 (if (null error-line)
920 (if (eobp) (insert (this-command-keys))
921 (error "No line number expression."))
922 (goto-char (match-beginning 0))
923 (setq error-line (string-to-int
924 (buffer-substring (match-beginning 1) (match-end 1)))
925 error-file (expand-file-name
926 (YaTeX-get-error-file YaTeX-current-TeX-buffer)))
927 (YaTeX-set-virtual-error-position 'error-file 'error-line)
928 (setq error-buf (YaTeX-switch-to-buffer error-file t)))
929 (if (null error-buf)
930 (error "`%s' is not found in this directory." error-file))
931 (YaTeX-showup-buffer error-buf nil t)
932 (goto-line error-line)))
934 (defun YaTeX-send-string ()
935 "Send string to current typeset process."
936 (interactive)
937 (if (and (eq (process-status YaTeX-typeset-process) 'run)
938 (>= (point) (process-mark YaTeX-typeset-process)))
939 (let ((b (process-mark YaTeX-typeset-process))
940 (e (point-end-of-line)))
941 (goto-char b)
942 (skip-chars-forward " \t" e)
943 (setq b (point))
944 (process-send-string
945 YaTeX-typeset-process (concat (buffer-substring b e) "\n"))
946 (goto-char e)
947 (insert "\n")
948 (set-marker (process-mark YaTeX-typeset-process) (point))
949 (insert " "))
950 (ding)))
952 (defun YaTeX-view-error ()
953 (interactive)
954 (if (null (get-buffer YaTeX-typeset-buffer))
955 (message "No typeset buffer found.")
956 (let ((win (selected-window)))
957 (YaTeX-showup-buffer YaTeX-typeset-buffer nil t)
958 ;; Next 3 lines are obsolete because YaTeX-typesetting-buffer is
959 ;; automatically scrolled up at typesetting.
960 ;;(goto-char (point-max))
961 ;;(forward-line -1)
962 ;;(recenter -1)
963 (select-window win))))
965 (defun YaTeX-get-error-file (default)
966 "Get current processing file from typesetting log."
967 (save-excursion
968 (let(s)
969 (condition-case () (up-list -1)
970 (error
971 (let ((list 0) found)
972 (while
973 (and (<= list 0) (not found)
974 (re-search-backward "\\((\\)\\|\\()\\)" nil t))
975 (if (equal (match-beginning 0) (match-beginning 2)) ;close paren.
976 (setq list (1- list)) ;open paren
977 (setq list (1+ list))
978 (if (= list 1)
979 (if (looking-at "\\([^,{}%]+\.\\)tex\\|sty")
980 (setq found t)
981 (setq list (1- list)))))))))
982 (setq s
983 (buffer-substring
984 (progn (forward-char 1) (point))
985 (progn (skip-chars-forward "^ \n" (point-end-of-line))
986 (point))))
987 (if (string= "" s) default s))))
989 (defun YaTeX-put-nonstopmode ()
990 (if (and (eq major-mode 'yatex-mode) YaTeX-need-nonstop)
991 (if (re-search-backward "\\\\nonstopmode{}" (point-min) t)
992 nil ;if already written in text then do nothing
993 (save-excursion
994 (YaTeX-visit-main t)
995 (goto-char (point-min))
996 (insert "\\nonstopmode{}%_YaTeX_%\n")
997 (if (buffer-file-name) (basic-save-buffer))))))
999 (defun YaTeX-remove-nonstopmode ()
1000 (if (and (eq major-mode 'yatex-mode) YaTeX-need-nonstop) ;for speed
1001 (save-excursion
1002 (YaTeX-visit-main t)
1003 (goto-char (point-min))
1004 (forward-line 1)
1005 (narrow-to-region (point-min) (point))
1006 (goto-char (point-min))
1007 (delete-matching-lines "^\\\\nonstopmode\\{\\}%_YaTeX_%$")
1008 (widen))))
1010 (defvar YaTeX-dvi2-command-ext-alist
1011 '(("[agx]dvi\\|dviout" . ".dvi")
1012 ("ghostview\\|gv" . ".ps")
1013 ("acroread\\|xpdf\\|pdfopen\\|Preview\\|TeXShop\\|Skim\\|evince\\|mupdf\\|zathura\\|okular" . ".pdf")))
1015 (defun YaTeX-get-preview-file-name (&optional preview-command)
1016 "Get file name to preview by inquiring YaTeX-get-latex-command"
1017 (if (null preview-command) (setq preview-command dvi2-command))
1018 (let* ((latex-cmd (YaTeX-get-latex-command t))
1019 (rin (rindex latex-cmd ? ))
1020 (fname (if rin (substring latex-cmd (1+ rin)) ""))
1021 (r (YaTeX-assoc-regexp preview-command YaTeX-dvi2-command-ext-alist))
1022 (ext (if r (cdr r) "")))
1023 (and (null r)
1024 (eq (get 'dvi2-command 'format) 'pdf)
1025 (setq ext "pdf"))
1026 (concat
1027 (if (string= fname "")
1028 (setq fname (substring (file-name-nondirectory
1029 (buffer-file-name))
1030 0 -4))
1031 (setq fname (substring fname 0 (rindex fname ?.))))
1032 ext)))
1034 (defun YaTeX-get-latex-command (&optional switch)
1035 "Specify the latex-command name and its argument.
1036 If there is a line which begins with string: \"%#!\", the following
1037 strings are assumed to be the latex-command and arguments. The
1038 default value of latex-command is:
1039 tex-command FileName
1040 and if you write \"%#!jlatex\" in the beginning of certain line.
1041 \"jlatex \" FileName
1042 will be the latex-command,
1043 and you write \"%#!jlatex main.tex\" on some line and argument SWITCH
1044 is non-nil, then
1045 \"jlatex main.tex\"
1047 will be given to the shell."
1048 (let (parent tparent magic)
1049 (setq parent
1050 (cond
1051 (YaTeX-parent-file
1052 (if YaTeX-dos (expand-file-name YaTeX-parent-file)
1053 YaTeX-parent-file))
1054 (t (save-excursion
1055 (YaTeX-visit-main t)
1056 (file-name-nondirectory (buffer-file-name)))))
1057 magic (YaTeX-get-builtin "!")
1058 tparent (file-name-nondirectory parent))
1059 (YaTeX-replace-formats
1060 (cond
1061 (magic
1062 (cond
1063 (switch (if (string-match "\\s [^-]\\S *$" magic) magic
1064 (concat magic " " parent)))
1065 (t (concat (substring magic 0 (string-match "\\s [^-]\\S *$" magic)) " "))))
1066 (t (concat tex-command " " (if switch parent))))
1067 (list (cons "f" tparent)
1068 (cons "r" (substring tparent 0 (rindex tparent ?.)))))))
1070 (defvar YaTeX-lpr-command-history nil
1071 "Holds command line history of YaTeX-lpr.")
1072 (put 'YaTeX-lpr-command-history 'no-default t)
1073 (defvar YaTeX-lpr-ask-page-range-default t)
1074 (defun YaTeX-lpr (arg)
1075 "Print out.
1076 If prefix arg ARG is non nil, call print driver without
1077 page range description."
1078 (interactive "P")
1079 (or YaTeX-lpr-ask-page-range-default (setq arg (not arg)))
1080 (let*((cmd (or (YaTeX-get-builtin "LPR") dviprint-command-format))
1081 from to (lbuffer "*dvi-printing*") dir)
1082 (setq
1083 cmd
1084 (YaTeX-replace-format
1085 cmd "f"
1086 (if (or arg (not (string-match "%f" cmd)))
1087 ""
1088 (YaTeX-replace-format
1089 dviprint-from-format
1090 "b"
1091 (if (string=
1092 (setq from (read-string "From page(default 1): ")) "")
1093 "1" from))))
1095 (setq
1096 cmd
1097 (YaTeX-replace-format
1098 cmd "t"
1099 (if (or arg (not (string-match "%t" cmd))
1100 (string=
1101 (setq to (read-string "To page(default none): ")) ""))
1102 ""
1103 (YaTeX-replace-format dviprint-to-format "e" to)))
1105 (setq
1106 cmd
1107 (YaTeX-replace-format
1108 cmd "p"
1109 (cdr (assoc (YaTeX-get-paper-type) YaTeX-dvips-paper-option-alist))))
1110 (setq cmd
1111 (read-string-with-history
1112 "Edit command line: "
1113 (format cmd
1114 (if (get 'dvi2-command 'region)
1115 (substring YaTeX-texput-file
1116 0 (rindex YaTeX-texput-file ?.))
1117 (YaTeX-get-preview-file-name)))
1118 'YaTeX-lpr-command-history))
1119 (save-excursion
1120 (YaTeX-visit-main t) ;;change execution directory
1121 (setq dir default-directory)
1122 (YaTeX-showup-buffer
1123 lbuffer 'YaTeX-showup-buffer-bottom-most)
1124 (set-buffer (get-buffer-create lbuffer))
1125 (erase-buffer)
1126 (cd dir) ;for 19
1127 (cond
1128 ((not (fboundp 'start-process))
1129 (call-process shell-file-name "con" "*dvi-printing*" nil
1130 YaTeX-shell-command-option cmd))
1131 (t
1132 (set-process-buffer
1133 (let ((process-connection-type nil))
1134 (start-process "print" "*dvi-printing*" shell-file-name
1135 YaTeX-shell-command-option cmd))
1136 (get-buffer lbuffer))
1137 (message "Starting printing command: %s..." cmd))))))
1139 (defun YaTeX-main-file-p ()
1140 "Return if current buffer is main LaTeX source."
1141 (cond
1142 (YaTeX-parent-file
1143 (eq (get-file-buffer YaTeX-parent-file) (current-buffer)))
1144 ((YaTeX-get-builtin "!")
1145 (string-match
1146 (concat "^" (YaTeX-guess-parent (YaTeX-get-builtin "!")))
1147 (buffer-name)))
1148 (t
1149 (save-excursion
1150 (let ((latex-main-id
1151 (concat "^\\s *" YaTeX-ec-regexp "document\\(style\\|class\\)")))
1152 (or (re-search-backward latex-main-id nil t)
1153 (re-search-forward latex-main-id nil t)))))))
1155 (defun YaTeX-visit-main (&optional setbuf)
1156 "Switch buffer to main LaTeX source.
1157 Use set-buffer instead of switch-to-buffer if the optional argument
1158 SETBUF is t(Use it only from Emacs-Lisp program)."
1159 (interactive "P")
1160 (if (and (interactive-p) setbuf) (setq YaTeX-parent-file nil))
1161 (let ((ff (function (lambda (f)
1162 (if setbuf (set-buffer (find-file-noselect f))
1163 (find-file f)))))
1164 b-in main-file mfa YaTeX-create-file-prefix-g
1165 (hilit-auto-highlight (not setbuf)))
1166 (if (setq b-in (YaTeX-get-builtin "!"))
1167 (setq main-file (YaTeX-guess-parent b-in)))
1168 (if YaTeX-parent-file
1169 (setq main-file ;;(get-file-buffer YaTeX-parent-file)
1170 YaTeX-parent-file))
1171 (if (YaTeX-main-file-p)
1172 (if (interactive-p) (message "I think this is main LaTeX source.") nil)
1173 (cond
1174 ((and ;;(interactive-p)
1175 main-file
1176 (cond ((get-file-buffer main-file)
1177 (cond
1178 (setbuf (set-buffer (get-file-buffer main-file)))
1179 ((get-buffer-window (get-file-buffer main-file))
1180 (select-window
1181 (get-buffer-window (get-file-buffer main-file))))
1182 (t (switch-to-buffer (get-file-buffer main-file)))))
1183 ((file-exists-p main-file)
1184 (funcall ff main-file)))))
1185 ;;((and main-file (YaTeX-switch-to-buffer main-file setbuf)))
1186 ((and main-file
1187 (file-exists-p (setq main-file (concat "../" main-file)))
1188 (or b-in
1189 (y-or-n-p (concat (setq mfa (expand-file-name main-file))
1190 " is main file?:"))))
1191 (setq YaTeX-parent-file mfa)
1192 ;(YaTeX-switch-to-buffer main-file setbuf)
1193 (funcall ff main-file)
1195 (t (setq main-file (read-file-name "Enter your main text: " nil nil 1))
1196 (setq YaTeX-parent-file (expand-file-name main-file))
1197 ; (YaTeX-switch-to-buffer main-file setbuf))
1198 (funcall ff main-file))
1199 )))
1200 nil)
1202 (defun YaTeX-guess-parent (command-line)
1203 (setq command-line
1204 (if (string-match "\\s \\([^-]\\S *\\)$" command-line)
1205 (substring command-line (match-beginning 1))
1206 (file-name-nondirectory (buffer-file-name)))
1207 command-line
1208 (concat (if (string-match "\\(.*\\)\\." command-line)
1209 (substring command-line (match-beginning 1) (match-end 1))
1210 command-line)
1211 ".tex")))
1213 (defun YaTeX-visit-main-other-window ()
1214 "Switch to buffer main LaTeX source in other window."
1215 (interactive)
1216 (if (YaTeX-main-file-p) (message "I think this is main LaTeX source.")
1217 (YaTeX-switch-to-buffer-other-window
1218 (concat (YaTeX-get-preview-file-name) ".tex"))))
1220 (defun YaTeX-save-buffers ()
1221 "Save buffers whose major-mode is equal to current major-mode."
1222 (basic-save-buffer)
1223 (let ((cmm major-mode))
1224 (save-excursion
1225 (mapcar (function
1226 (lambda (buf)
1227 (set-buffer buf)
1228 (if (and (buffer-file-name buf)
1229 (eq major-mode cmm)
1230 (buffer-modified-p buf)
1231 (y-or-n-p (format "Save %s" (buffer-name buf))))
1232 (save-buffer buf))))
1233 (buffer-list)))))
1235 (provide 'yatexprc)