yatex

view yatexprc.el @ 366:77a2fd4fd634

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