yatex

view yatexprc.el @ 369:a536acc07a75

Re-activate forward-search code.
author HIROSE Yuuji <yuuji@gentei.org>
date Sun, 28 Dec 2014 16:18:40 +0900
parents d4413f5043dc
children 037f8d4c3c70
line source
1 ;;; yatexprc.el --- YaTeX process handler
2 ;;;
3 ;;; (c)1993-2014 by HIROSE Yuuji.[yuuji@yatex.org]
4 ;;; Last modified Fri Dec 26 16:14: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-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 ;;Send patch to the author, please
872 previewer bnr cf line))
873 ((string-match "qpdfview" previewer) ;;??
874 (format "%s '%s.pdf#src:%s:%d:0'"
875 ;;Send patch to the author, please
876 previewer bnr cf line))
877 ((string-match "okular" previewer) ;;??
878 (format "%s '%s.pdf#src:%d' '%s'"
879 ;;Send patch to the author, please
880 previewer bnr line cf))
881 )))
882 (YaTeX-system cmd "jump-line" 'noask pdir)))))
884 (defun YaTeX-goto-corresponding-viewer ()
885 (let ((cmd (or (YaTeX-get-builtin "!") tex-command)))
886 (if (string-match "-src\\|synctex=" cmd)
887 (progn
888 (YaTeX-preview-jump-line)
889 t) ;for YaTeX-goto-corresponding-*
890 nil)))
892 (defun YaTeX-set-virtual-error-position (file-sym line-sym)
893 "Replace the value of FILE-SYM, LINE-SYM by virtual error position."
894 (cond
895 ((and (get 'dvi2-command 'region)
896 (> (symbol-value line-sym) (car (get 'dvi2-command 'offset))))
897 (set file-sym (get 'dvi2-command 'file))
898 (set line-sym
899 (+ (- (apply '- (get 'dvi2-command 'offset)))
900 (symbol-value line-sym)
901 -1)))))
903 (defun YaTeX-prev-error ()
904 "Visit position of previous typeset error or warning.
905 To avoid making confliction of line numbers by editing, jump to
906 error or warning lines in reverse order."
907 (interactive)
908 (let ((cur-buf (save-excursion (YaTeX-visit-main t) (buffer-name)))
909 (cur-win (selected-window))
910 b0 bound errorp error-line typeset-win error-buffer error-win)
911 (if (null (get-buffer YaTeX-typeset-buffer))
912 (error "There is no typesetting buffer."))
913 (YaTeX-showup-buffer YaTeX-typeset-buffer nil t)
914 (if (and (markerp YaTeX-typeset-marker)
915 (eq (marker-buffer YaTeX-typeset-marker) (current-buffer)))
916 (setq bound YaTeX-typeset-marker))
917 (setq typeset-win (selected-window))
918 (if (re-search-backward
919 (concat "\\(" latex-error-regexp "\\)\\|\\("
920 latex-warning-regexp "\\)")
921 bound t)
922 (setq errorp (match-beginning 1))
923 (select-window cur-win)
924 (error "No more errors on %s" cur-buf))
925 (goto-char (setq b0 (match-beginning 0)))
926 (skip-chars-forward "^0-9" (match-end 0))
927 (setq error-line
928 (string-to-int
929 (buffer-substring
930 (point)
931 (progn (skip-chars-forward "0-9" (match-end 0)) (point))))
932 error-buffer (expand-file-name (YaTeX-get-error-file cur-buf)))
933 (if (or (null error-line) (equal 0 error-line))
934 (error "Can't detect error position."))
935 (YaTeX-set-virtual-error-position 'error-buffer 'error-line)
936 (setq error-win (get-buffer-window error-buffer))
937 (select-window cur-win)
938 (cond
939 (error-win (select-window error-win))
940 ((eq (get-lru-window) typeset-win)
941 (YaTeX-switch-to-buffer error-buffer))
942 (t (select-window (get-lru-window))
943 (YaTeX-switch-to-buffer error-buffer)))
944 (setq error-win (selected-window))
945 (goto-line error-line)
946 (message "LaTeX %s in `%s' on line: %d."
947 (if errorp "error" "warning")
948 error-buffer error-line)
949 (select-window typeset-win)
950 (skip-chars-backward "0-9")
951 (recenter (/ (window-height) 2))
952 (sit-for 1)
953 (goto-char b0)
954 (select-window error-win)))
956 (defun YaTeX-jump-error-line ()
957 "Jump to corresponding line on latex command's error message."
958 (interactive)
959 (let (error-line error-file error-buf)
960 (save-excursion
961 (beginning-of-line)
962 (setq error-line (re-search-forward "l[ ines]*\\.?\\([1-9][0-9]*\\)"
963 (point-end-of-line) t)))
964 (if (null error-line)
965 (if (eobp) (insert (this-command-keys))
966 (error "No line number expression."))
967 (goto-char (match-beginning 0))
968 (setq error-line (string-to-int
969 (buffer-substring (match-beginning 1) (match-end 1)))
970 error-file (expand-file-name
971 (YaTeX-get-error-file YaTeX-current-TeX-buffer)))
972 (YaTeX-set-virtual-error-position 'error-file 'error-line)
973 (setq error-buf (YaTeX-switch-to-buffer error-file t)))
974 (if (null error-buf)
975 (error "`%s' is not found in this directory." error-file))
976 (YaTeX-showup-buffer error-buf nil t)
977 (goto-line error-line)))
979 (defun YaTeX-send-string ()
980 "Send string to current typeset process."
981 (interactive)
982 (if (and (eq (process-status YaTeX-typeset-process) 'run)
983 (>= (point) (process-mark YaTeX-typeset-process)))
984 (let ((b (process-mark YaTeX-typeset-process))
985 (e (point-end-of-line)))
986 (goto-char b)
987 (skip-chars-forward " \t" e)
988 (setq b (point))
989 (process-send-string
990 YaTeX-typeset-process (concat (buffer-substring b e) "\n"))
991 (goto-char e)
992 (insert "\n")
993 (set-marker (process-mark YaTeX-typeset-process) (point))
994 (insert " "))
995 (ding)))
997 (defun YaTeX-view-error ()
998 (interactive)
999 (if (null (get-buffer YaTeX-typeset-buffer))
1000 (message "No typeset buffer found.")
1001 (let ((win (selected-window)))
1002 (YaTeX-showup-buffer YaTeX-typeset-buffer nil t)
1003 ;; Next 3 lines are obsolete because YaTeX-typesetting-buffer is
1004 ;; automatically scrolled up at typesetting.
1005 ;;(goto-char (point-max))
1006 ;;(forward-line -1)
1007 ;;(recenter -1)
1008 (select-window win))))
1010 (defun YaTeX-get-error-file (default)
1011 "Get current processing file from typesetting log."
1012 (save-excursion
1013 (let(s)
1014 (condition-case () (up-list -1)
1015 (error
1016 (let ((list 0) found)
1017 (while
1018 (and (<= list 0) (not found)
1019 (re-search-backward "\\((\\)\\|\\()\\)" nil t))
1020 (if (equal (match-beginning 0) (match-beginning 2)) ;close paren.
1021 (setq list (1- list)) ;open paren
1022 (setq list (1+ list))
1023 (if (= list 1)
1024 (if (looking-at "\\([^,{}%]+\.\\)tex\\|sty")
1025 (setq found t)
1026 (setq list (1- list)))))))))
1027 (setq s
1028 (buffer-substring
1029 (progn (forward-char 1) (point))
1030 (progn (skip-chars-forward "^ \n" (point-end-of-line))
1031 (point))))
1032 (if (string= "" s) default s))))
1034 (defun YaTeX-put-nonstopmode ()
1035 (if (and (eq major-mode 'yatex-mode) YaTeX-need-nonstop)
1036 (if (re-search-backward "\\\\nonstopmode{}" (point-min) t)
1037 nil ;if already written in text then do nothing
1038 (save-excursion
1039 (YaTeX-visit-main t)
1040 (goto-char (point-min))
1041 (insert "\\nonstopmode{}%_YaTeX_%\n")
1042 (if (buffer-file-name) (basic-save-buffer))))))
1044 (defun YaTeX-remove-nonstopmode ()
1045 (if (and (eq major-mode 'yatex-mode) YaTeX-need-nonstop) ;for speed
1046 (save-excursion
1047 (YaTeX-visit-main t)
1048 (goto-char (point-min))
1049 (forward-line 1)
1050 (narrow-to-region (point-min) (point))
1051 (goto-char (point-min))
1052 (delete-matching-lines "^\\\\nonstopmode\\{\\}%_YaTeX_%$")
1053 (widen))))
1055 (defvar YaTeX-dvi2-command-ext-alist
1056 '(("[agx]dvi\\|dviout" . ".dvi")
1057 ("ghostview\\|gv" . ".ps")
1058 ("acroread\\|xpdf\\|pdfopen\\|Preview\\|TeXShop\\|Skim\\|evince\\|mupdf\\|zathura\\|okular" . ".pdf")))
1060 (defun YaTeX-get-preview-file-name (&optional preview-command)
1061 "Get file name to preview by inquiring YaTeX-get-latex-command"
1062 (if (null preview-command) (setq preview-command dvi2-command))
1063 (let* ((latex-cmd (YaTeX-get-latex-command t))
1064 (rin (rindex latex-cmd ? ))
1065 (fname (if rin (substring latex-cmd (1+ rin)) ""))
1066 (r (YaTeX-assoc-regexp preview-command YaTeX-dvi2-command-ext-alist))
1067 (ext (if r (cdr r) "")))
1068 (and (null r)
1069 (eq (get 'dvi2-command 'format) 'pdf)
1070 (setq ext "pdf"))
1071 (concat
1072 (if (string= fname "")
1073 (setq fname (substring (file-name-nondirectory
1074 (buffer-file-name))
1075 0 -4))
1076 (setq fname (substring fname 0 (rindex fname ?.))))
1077 ext)))
1079 (defun YaTeX-get-latex-command (&optional switch)
1080 "Specify the latex-command name and its argument.
1081 If there is a line which begins with string: \"%#!\", the following
1082 strings are assumed to be the latex-command and arguments. The
1083 default value of latex-command is:
1084 tex-command FileName
1085 and if you write \"%#!jlatex\" in the beginning of certain line.
1086 \"jlatex \" FileName
1087 will be the latex-command,
1088 and you write \"%#!jlatex main.tex\" on some line and argument SWITCH
1089 is non-nil, then
1090 \"jlatex main.tex\"
1092 will be given to the shell."
1093 (let (parent tparent magic)
1094 (setq parent
1095 (cond
1096 (YaTeX-parent-file
1097 (if YaTeX-dos (expand-file-name YaTeX-parent-file)
1098 YaTeX-parent-file))
1099 (t (save-excursion
1100 (YaTeX-visit-main t)
1101 (file-name-nondirectory (buffer-file-name)))))
1102 magic (YaTeX-get-builtin "!")
1103 tparent (file-name-nondirectory parent))
1104 (YaTeX-replace-formats
1105 (cond
1106 (magic
1107 (cond
1108 (switch (if (string-match "\\s [^-]\\S *$" magic) magic
1109 (concat magic " " parent)))
1110 (t (concat (substring magic 0 (string-match "\\s [^-]\\S *$" magic)) " "))))
1111 (t (concat tex-command " " (if switch parent))))
1112 (list (cons "f" tparent)
1113 (cons "r" (substring tparent 0 (rindex tparent ?.)))))))
1115 (defvar YaTeX-lpr-command-history nil
1116 "Holds command line history of YaTeX-lpr.")
1117 (put 'YaTeX-lpr-command-history 'no-default t)
1118 (defvar YaTeX-lpr-ask-page-range-default t)
1119 (defun YaTeX-lpr (arg)
1120 "Print out.
1121 If prefix arg ARG is non nil, call print driver without
1122 page range description."
1123 (interactive "P")
1124 (or YaTeX-lpr-ask-page-range-default (setq arg (not arg)))
1125 (let*((cmd (or (YaTeX-get-builtin "LPR") dviprint-command-format))
1126 from to (lbuffer "*dvi-printing*") dir)
1127 (setq
1128 cmd
1129 (YaTeX-replace-format
1130 cmd "f"
1131 (if (or arg (not (string-match "%f" cmd)))
1132 ""
1133 (YaTeX-replace-format
1134 dviprint-from-format
1135 "b"
1136 (if (string=
1137 (setq from (read-string "From page(default 1): ")) "")
1138 "1" from))))
1140 (setq
1141 cmd
1142 (YaTeX-replace-format
1143 cmd "t"
1144 (if (or arg (not (string-match "%t" cmd))
1145 (string=
1146 (setq to (read-string "To page(default none): ")) ""))
1147 ""
1148 (YaTeX-replace-format dviprint-to-format "e" to)))
1150 (setq
1151 cmd
1152 (YaTeX-replace-format
1153 cmd "p"
1154 (cdr (assoc (YaTeX-get-paper-type) YaTeX-dvips-paper-option-alist))))
1155 (setq cmd
1156 (read-string-with-history
1157 "Edit command line: "
1158 (format cmd
1159 (if (get 'dvi2-command 'region)
1160 (substring YaTeX-texput-file
1161 0 (rindex YaTeX-texput-file ?.))
1162 (YaTeX-get-preview-file-name)))
1163 'YaTeX-lpr-command-history))
1164 (save-excursion
1165 (YaTeX-visit-main t) ;;change execution directory
1166 (setq dir default-directory)
1167 (YaTeX-showup-buffer
1168 lbuffer 'YaTeX-showup-buffer-bottom-most)
1169 (set-buffer (get-buffer-create lbuffer))
1170 (erase-buffer)
1171 (cd dir) ;for 19
1172 (cond
1173 ((not (fboundp 'start-process))
1174 (call-process shell-file-name "con" "*dvi-printing*" nil
1175 YaTeX-shell-command-option cmd))
1176 (t
1177 (set-process-buffer
1178 (let ((process-connection-type nil))
1179 (start-process "print" "*dvi-printing*" shell-file-name
1180 YaTeX-shell-command-option cmd))
1181 (get-buffer lbuffer))
1182 (message "Starting printing command: %s..." cmd))))))
1184 (defun YaTeX-main-file-p ()
1185 "Return if current buffer is main LaTeX source."
1186 (cond
1187 (YaTeX-parent-file
1188 (eq (get-file-buffer YaTeX-parent-file) (current-buffer)))
1189 ((YaTeX-get-builtin "!")
1190 (string-match
1191 (concat "^" (YaTeX-guess-parent (YaTeX-get-builtin "!")))
1192 (buffer-name)))
1193 (t
1194 (save-excursion
1195 (let ((latex-main-id
1196 (concat "^\\s *" YaTeX-ec-regexp "document\\(style\\|class\\)")))
1197 (or (re-search-backward latex-main-id nil t)
1198 (re-search-forward latex-main-id nil t)))))))
1200 (defun YaTeX-visit-main (&optional setbuf)
1201 "Switch buffer to main LaTeX source.
1202 Use set-buffer instead of switch-to-buffer if the optional argument
1203 SETBUF is t(Use it only from Emacs-Lisp program)."
1204 (interactive "P")
1205 (if (and (interactive-p) setbuf) (setq YaTeX-parent-file nil))
1206 (let ((ff (function (lambda (f)
1207 (if setbuf (set-buffer (find-file-noselect f))
1208 (find-file f)))))
1209 b-in main-file mfa YaTeX-create-file-prefix-g
1210 (hilit-auto-highlight (not setbuf)))
1211 (if (setq b-in (YaTeX-get-builtin "!"))
1212 (setq main-file (YaTeX-guess-parent b-in)))
1213 (if YaTeX-parent-file
1214 (setq main-file ;;(get-file-buffer YaTeX-parent-file)
1215 YaTeX-parent-file))
1216 (if (YaTeX-main-file-p)
1217 (if (interactive-p) (message "I think this is main LaTeX source.") nil)
1218 (cond
1219 ((and ;;(interactive-p)
1220 main-file
1221 (cond ((get-file-buffer main-file)
1222 (cond
1223 (setbuf (set-buffer (get-file-buffer main-file)))
1224 ((get-buffer-window (get-file-buffer main-file))
1225 (select-window
1226 (get-buffer-window (get-file-buffer main-file))))
1227 (t (switch-to-buffer (get-file-buffer main-file)))))
1228 ((file-exists-p main-file)
1229 (funcall ff main-file)))))
1230 ;;((and main-file (YaTeX-switch-to-buffer main-file setbuf)))
1231 ((and main-file
1232 (file-exists-p (setq main-file (concat "../" main-file)))
1233 (or b-in
1234 (y-or-n-p (concat (setq mfa (expand-file-name main-file))
1235 " is main file?:"))))
1236 (setq YaTeX-parent-file mfa)
1237 ;(YaTeX-switch-to-buffer main-file setbuf)
1238 (funcall ff main-file)
1240 (t (setq main-file (read-file-name "Enter your main text: " nil nil 1))
1241 (setq YaTeX-parent-file (expand-file-name main-file))
1242 ; (YaTeX-switch-to-buffer main-file setbuf))
1243 (funcall ff main-file))
1244 )))
1245 nil)
1247 (defun YaTeX-guess-parent (command-line)
1248 (setq command-line
1249 (if (string-match "\\s \\([^-]\\S *\\)$" command-line)
1250 (substring command-line (match-beginning 1))
1251 (file-name-nondirectory (buffer-file-name)))
1252 command-line
1253 (concat (if (string-match "\\(.*\\)\\." command-line)
1254 (substring command-line (match-beginning 1) (match-end 1))
1255 command-line)
1256 ".tex")))
1258 (defun YaTeX-visit-main-other-window ()
1259 "Switch to buffer main LaTeX source in other window."
1260 (interactive)
1261 (if (YaTeX-main-file-p) (message "I think this is main LaTeX source.")
1262 (YaTeX-switch-to-buffer-other-window
1263 (concat (YaTeX-get-preview-file-name) ".tex"))))
1265 (defun YaTeX-save-buffers ()
1266 "Save buffers whose major-mode is equal to current major-mode."
1267 (basic-save-buffer)
1268 (let ((cmm major-mode))
1269 (save-excursion
1270 (mapcar (function
1271 (lambda (buf)
1272 (set-buffer buf)
1273 (if (and (buffer-file-name buf)
1274 (eq major-mode cmm)
1275 (buffer-modified-p buf)
1276 (y-or-n-p (format "Save %s" (buffer-name buf))))
1277 (save-buffer buf))))
1278 (buffer-list)))))
1280 (provide 'yatexprc)