yatex

view yatexprc.el @ 375:afb8cb06b1d4

First trial of on-the-fly image-preview
author HIROSE Yuuji <yuuji@gentei.org>
date Wed, 31 Dec 2014 23:00:01 +0900
parents 9b3093c0c181
children 1bbd0c2b340f
line source
1 ;;; yatexprc.el --- YaTeX process handler
2 ;;;
3 ;;; (c)1993-2014 by HIROSE Yuuji.[yuuji@yatex.org]
4 ;;; Last modified Wed Dec 31 22:42:56 2014 on sdr
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 (cond
140 ((fboundp 'start-process)
141 (insert " ")
142 (set-marker (process-mark YaTeX-typeset-process) (1- (point))))
143 (t (message "Done.")))
144 (if (bolp) (forward-line -1)) ;what for?
145 (cond
146 (background nil)
147 ((and YaTeX-emacs-19 window-system)
148 (let ((win (get-buffer-window buffer t)) owin)
149 (select-frame (window-frame win))
150 (setq owin (selected-window))
151 (select-window win)
152 (goto-char (point-max))
153 (recenter -1)
154 (select-window owin)))
155 (t (select-window (get-buffer-window buffer))
156 (goto-char (point-max))
157 (recenter -1)))
158 (select-window window)
159 (switch-to-buffer cb)
160 (YaTeX-remove-nonstopmode))))
162 (defvar YaTeX-typeset-auto-rerun t
163 "*Non-nil automatically reruns typesetter when cross-refs update found.
164 This is a toy mechanism. DO NOT RELY ON THIS MECHANISM.
165 You SHOULD check the integrity of cross-references with your eyes!!
166 Supplying an integer to this variable inhibit compulsory call of bibtex,
167 thus, it call bibtex only if warning messages about citation are seen.")
168 (defvar YaTeX-typeset-rerun-msg "Rerun to get cross-references right.")
169 (defvar YaTeX-typeset-citation-msg
170 "Warning: Citation \`")
171 (defun YaTeX-typeset-sentinel (proc mes)
172 (cond ((null (buffer-name (process-buffer proc)))
173 ;; buffer killed
174 (set-process-buffer proc nil))
175 ((memq (process-status proc) '(signal exit))
176 (let* ((obuf (current-buffer)) (pbuf (process-buffer proc))
177 (pwin (get-buffer-window pbuf))
178 (owin (selected-window)) win
179 tobecalled shortname
180 (thiscmd (get 'YaTeX-typeset-process 'thiscmd))
181 (ppprop (get 'YaTeX-typeset-process 'ppcmd))
182 z (ppcmd (cdr (assq proc ppprop)))
183 (bcprop (get 'YaTeX-typeset-process 'bibcmd))
184 (bibcmd (cdr (assq proc bcprop))))
185 (put 'YaTeX-typeset-process 'ppcmd ;erase ppcmd
186 (delq (assq proc ppprop) ppprop))
187 (put 'YaTeX-typeset-process 'bibcmd ;erase bibcmd
188 (delq (assq proc bcprop) bcprop))
189 ;; save-excursion isn't the right thing if
190 ;; process-buffer is current-buffer
191 (unwind-protect
192 (progn
193 ;; Write something in *typesetting* and hack its mode line
194 (if pwin
195 (select-window pwin)
196 (set-buffer pbuf))
197 ;;(YaTeX-showup-buffer pbuf nil t)
198 (goto-char (point-max))
199 (if pwin (recenter -3))
200 (insert ?\n mode-name " " mes)
201 (forward-char -1)
202 (insert
203 (format " at %s%s\n"
204 (substring (current-time-string) 0 -5)
205 (if (and (fboundp 'current-time) (fboundp 'float)
206 (assq 'time YaTeX-typeset-consumption))
207 (format
208 " (%.2f secs)"
209 (YaTeX-elapsed-time
210 (cdr (assq 'time YaTeX-typeset-consumption))
211 (current-time))))))
212 (setq mode-line-process
213 (concat ": "
214 (symbol-name (process-status proc))))
215 (message "%s %s" mode-name
216 (if (eq (process-status proc) 'exit)
217 "done" "ceased"))
218 ;; If buffer and mode line shows that the process
219 ;; is dead, we can delete it now. Otherwise it
220 ;; will stay around until M-x list-processes.
221 (delete-process proc)
222 (if (cond
223 ((or (not YaTeX-typeset-auto-rerun)
224 (string-match "latexmk" thiscmd))
225 nil)
226 ((and bibcmd ;Call bibtex if bibcmd defined &&
227 (or ; (1st call || warning found)
228 (and (not (numberp YaTeX-typeset-auto-rerun))
229 ; cancel call at 1st, if value is a number.
230 (not (string-match "bibtex" mode-name)))
231 (re-search-backward
232 YaTeX-typeset-citation-msg
233 YaTeX-typeset-marker t))
234 (save-excursion ; && using .bbl files.
235 (search-backward
236 ".bbl" YaTeX-typeset-marker t)))
237 ;; Always call bibtex after the first typesetting,
238 ;; because bibtex doesn't warn disappeared \cite.
239 ;; (Suggested by ryseto. 2012)
240 ;; It is more efficient to call bibtex directly than
241 ;; to call it after deep inspection on the balance
242 ;; of \cite vs. \bib*'s referring all *.aux files.
243 (insert "\n" YaTeX-typeset-rerun-msg "\n")
244 (setq tobecalled bibcmd shortname "+bibtex"))
245 ((or
246 (save-excursion
247 (search-backward
248 YaTeX-typeset-rerun-msg YaTeX-typeset-marker t))
249 (save-excursion
250 (re-search-backward
251 "natbib.*Rerun to get citations correct"
252 YaTeX-typeset-marker t)))
253 (if bibcmd
254 (put 'YaTeX-typeset-process 'bibcmd
255 (cons (cons (get-buffer-process pbuf) bibcmd)
256 bcprop)))
257 (setq tobecalled thiscmd shortname "+typeset"))
258 (t
259 nil)) ;no need to call any process
260 (progn ;;Something occurs to call next command
261 (insert
262 (format
263 "===!!! %s !!!===\n"
264 (message "Rerun `%s' to get cross-references right"
265 tobecalled)))
266 (if (equal tobecalled thiscmd)
267 (set-marker YaTeX-typeset-marker (point)))
268 (set-process-sentinel
269 (start-process
270 (setq mode-name (concat mode-name shortname))
271 pbuf
272 shell-file-name YaTeX-shell-command-option tobecalled)
273 'YaTeX-typeset-sentinel)
274 (if ppcmd
275 (put 'YaTeX-typeset-process 'ppcmd
276 (cons (cons (get-buffer-process pbuf) ppcmd)
277 ppprop)))
278 (if thiscmd
279 (put 'YaTeX-typeset-process 'thiscmd thiscmd)))
280 ;; If ppcmd is active, call it.
281 (cond
282 ((and ppcmd (symbolp ppcmd) (fboundp ppcmd))
283 ;; If ppcmd is set and it is a function symbol,
284 ;; call it whenever command succeeded or not
285 (funcall ppcmd))
286 ((and ppcmd (string-match "finish" mes))
287 (insert (format "=======> Success! Calling %s\n" ppcmd))
288 (setq mode-name ; set process name
289 (concat
290 mode-name "+"
291 (substring ppcmd 0 (string-match " " ppcmd))))
292 ; to reach here, 'start-process exists on this emacsen
293 (set-process-sentinel
294 (start-process
295 mode-name
296 pbuf ; Use this buffer twice.
297 shell-file-name YaTeX-shell-command-option
298 ppcmd)
299 'YaTeX-typeset-sentinel))
300 (t ;pull back original mode-name
301 ;;Confirm process buffer to be shown when error occured
302 (YaTeX-showup-buffer pbuf 'YaTeX-showup-buffer-bottom-most)
303 (message "Command FAILED!")
304 (setq mode-name "typeset"))))
305 (forward-char 1))
306 (setq YaTeX-typeset-process nil)
307 ;; Force mode line redisplay soon
308 (set-buffer-modified-p (buffer-modified-p))
309 )
310 (select-window owin)
311 (set-buffer obuf)))))
313 (defvar YaTeX-texput-file "texput.tex"
314 "*File name for temporary file of typeset-region.")
316 (defun YaTeX-typeset-region (&optional pp)
317 "Paste the region to the file `texput.tex' and execute typesetter.
318 The region is specified by the rule:
319 (1)If keyword `%#BEGIN' is found in the upper direction from (point).
320 (1-1)if the keyword `%#END' is found after `%#BEGIN',
321 ->Assume the text between `%#BEGIN' and `%#END' as region.
322 (1-2)if the keyword `%#END' is not found anywhere after `%#BEGIN',
323 ->Assume the text after `%#BEGIN' as region.
324 (2)If no `%#BEGIN' usage is found before the (point),
325 ->Assume the text between current (point) and (mark) as region.
326 DON'T forget to eliminate the `%#BEGIN/%#END' notation after editing
327 operation to the region.
328 Optional second argument PP specifies post-processor command which will be
329 called with one argument of current file name whitout extension."
330 (interactive)
331 (save-excursion
332 (let*
333 ((end "") typeout ;Type out message that tells the method of cutting.
334 (texput YaTeX-texput-file)
335 (texputroot (substring texput 0 (string-match "\\.tex$" texput)))
336 (cmd (concat (YaTeX-get-latex-command nil) " " texput))
337 (buffer (current-buffer)) opoint preamble (subpreamble "") main
338 (hilit-auto-highlight nil) ;for Emacs19 with hilit19
339 ppcmd
340 reg-begin reg-end lineinfo)
341 (setq ppcmd (if (stringp pp) (concat pp " " texputroot) pp))
342 (save-excursion
343 (if (search-backward "%#BEGIN" nil t)
344 (progn
345 (setq typeout "--- Region from BEGIN to "
346 end "the end of the buffer ---"
347 reg-begin (match-end 0))
348 (if (search-forward "%#END" nil t)
349 (setq reg-end (match-beginning 0)
350 end "END ---")
351 (setq reg-end (point-max))))
352 (setq typeout "=== Region from (point) to (mark) ==="
353 reg-begin (point) reg-end (mark)))
354 (goto-char (min reg-begin reg-end))
355 (setq lineinfo (count-lines (point-min) (point-end-of-line)))
356 (goto-char (point-min))
357 (while (search-forward "%#REQUIRE" nil t)
358 (setq subpreamble
359 (concat subpreamble
360 (cond
361 ((eolp)
362 (buffer-substring
363 (match-beginning 0)
364 (point-beginning-of-line)))
365 (t (buffer-substring
366 (match-end 0)
367 (point-end-of-line))))
368 "\n"))
369 (goto-char (match-end 0))))
370 (YaTeX-visit-main t)
371 (setq main (current-buffer))
372 (setq opoint (point))
373 (goto-char (point-min))
374 (setq
375 preamble
376 (if (re-search-forward "^[ ]*\\\\begin.*{document}" nil t)
377 (buffer-substring (point-min) (match-end 0))
378 (concat
379 (if YaTeX-use-LaTeX2e "\\documentclass{" "\\documentstyle{")
380 YaTeX-default-document-style "}\n"
381 "\\begin{document}")))
382 (goto-char opoint)
383 ;;(set-buffer buffer) ;for clarity
384 (let ((hilit-auto-highlight nil) (auto-mode-alist nil)
385 (magic-mode-alist nil)) ;Do not activate yatex-mode here
386 (set-buffer (find-file-noselect texput)))
387 ;;(find-file YaTeX-texput-file)
388 (erase-buffer)
389 (YaTeX-set-file-coding-system YaTeX-kanji-code YaTeX-coding-system)
390 (if (and (eq major-mode 'yatex-mode) YaTeX-need-nonstop)
391 (insert "\\nonstopmode{}\n"))
392 (insert preamble "\n" subpreamble "\n"
393 "\\pagestyle{empty}\n\\thispagestyle{empty}\n")
394 (setq lineinfo (list (count-lines 1 (point-end-of-line)) lineinfo))
395 (insert-buffer-substring buffer reg-begin reg-end)
396 (insert "\\typeout{" typeout end "}\n") ;Notice the selected method.
397 (insert "\n\\end{document}\n")
398 (basic-save-buffer)
399 (kill-buffer (current-buffer))
400 (set-buffer main) ;return to parent file or itself.
401 (YaTeX-typeset cmd YaTeX-typeset-buffer nil nil ppcmd)
402 (switch-to-buffer buffer) ;for Emacs-19
403 (put 'dvi2-command 'region t)
404 (put 'dvi2-command 'file buffer)
405 (put 'dvi2-command 'offset lineinfo))))
407 (defvar YaTeX-use-image-preview t
408 "*Use or else view graphic preview image via [prefix] t e.")
409 (defvar YaTeX-preview-image-mode-map nil
410 "Keymap used in YaTeX-preview-image-mode")
411 (defun YaTeX-preview-image-mode ()
412 (interactive)
413 (if YaTeX-preview-image-mode-map
414 nil
415 (let ((map (setq YaTeX-preview-image-mode-map (make-sparse-keymap))))
416 (define-key map "q" (lambda()(interactive)
417 (kill-buffer)
418 (select-window
419 (or (get 'YaTeX-typeset-process 'win)
420 (selected-window)))))
421 (define-key map "j" (lambda()(interactive) (scroll-up 1)))
422 (define-key map "k" (lambda()(interactive) (scroll-up -1)))))
423 (use-local-map YaTeX-preview-image-mode-map))
425 (defvar YaTeX-typeset-pdf2image-chain
426 (cond
427 ((YaTeX-executable-find "pdfcrop") ;Mac OS X
428 (list
429 "pdfcrop --clip %b.pdf tmp.pdf"
430 (if (YaTeX-executable-find "convert")
431 "convert -density %d tmp.pdf %b.%f"
432 "sips -s format %f -s dpiWidth %d -s dpiHeight %d %b.pdf --out %b.%f")
433 "rm -f tmp.pdf")))
434 "*Pipe line of command as a list to create image file from PDF.
435 See also doc-string of YaTeX-typeset-dvi2image-chain.")
437 (defvar YaTeX-typeset-dvi2image-chain
438 (cond
439 ((YaTeX-executable-find "dvipng")
440 (list "dvipng %b.dvi"))
441 ((YaTeX-executable-find YaTeX-cmd-dvips)
442 (list
443 (format "%s -E -o %%b.eps %%b.dvi" YaTeX-cmd-dvips)
444 "convert -alpha off -density %d %b.eps %b.%f")))
445 "*Pipe line of command as a list to create png file from DVI or PDF.
446 %-notation rewritten list:
447 %b basename of target file(\"texput\")
448 %f Output format(\"png\")
449 %d DPI
450 ")
452 (defvar YaTeX-typeset-conv2image-chain-alist
453 (list (cons 'pdf YaTeX-typeset-pdf2image-chain)
454 (cons 'dvi YaTeX-typeset-dvi2image-chain))
455 "Default alist for creating image files from DVI/PDF.
456 The value is generated from YaTeX-typeset-pdf2image-chain and
457 YaTeX-typeset-dvi2image-chain.")
459 (defvar YaTeX-typeset-conv2image-process nil "Process of conv2image chain")
460 (defun YaTeX-typeset-conv2image-chain ()
461 (let*((proc (or YaTeX-typeset-process YaTeX-typeset-conv2image-process))
462 (prevname (process-name proc))
463 (texput "texput") (format "jpg")
464 (target (concat texput "." format))
465 (math (get 'YaTeX-typeset-conv2image-chain 'math))
466 (srctype (or (get 'YaTeX-typeset-conv2image-chain 'srctype)
467 (if (file-newer-than-file-p
468 (concat texput ".pdf")
469 (concat texput ".dvi"))
470 'pdf 'dvi)))
471 (chain (cdr (assq srctype YaTeX-typeset-conv2image-chain-alist)))
472 ;; This function is the first evaluation code.
473 ;; If you find these command line does not work
474 ;; on your system, please tell the author
475 ;; which commands should be taken to achieve
476 ;; one-shot png previewing on your system
477 ;; before publishing patch on the Web.
478 ;; Please please please please please.
479 (curproc (member prevname chain))
480 (w (get 'YaTeX-typeset-conv2image-chain 'win))
481 (pwd default-directory)
482 img)
483 (if (not (= (process-exit-status proc) 0))
484 (progn
485 (YaTeX-showup-buffer ;never comes here(?)
486 (current-buffer) 'YaTeX-showup-buffer-bottom-most)
487 (message "Region typesetting FAILED"))
488 (setq command
489 (if curproc (car (cdr-safe curproc)) (car chain)))
490 (if command
491 (let ((cmdline (YaTeX-replace-formats
492 command
493 (list (cons "b" "texput")
494 (cons "f" format)
495 (cons "d" (if math "300" "200"))))))
496 (insert (format "Calling `%s'...\n" cmdline))
497 (set-process-sentinel
498 (setq YaTeX-typeset-conv2image-process
499 (start-process
500 command
501 (current-buffer)
502 shell-file-name YaTeX-shell-command-option
503 cmdline))
504 'YaTeX-typeset-sentinel)
505 (put 'YaTeX-typeset-process 'ppcmd
506 (cons (cons (get-buffer-process (current-buffer))
507 'YaTeX-typeset-conv2image-chain)
508 (get 'YaTeX-typeset-process 'ppcmd))))
509 ;; After all chain executed, display image in current window
510 (cond
511 ((and (featurep 'image) window-system)
512 ;; If direct image displaying available in running Emacs,
513 ;; display target image into the next window in Emacs.
514 (select-window w)
515 ;(setq foo (selected-window))
516 (YaTeX-showup-buffer
517 (get-buffer-create " *YaTeX-region-image*")
518 'YaTeX-showup-buffer-bottom-most t)
519 (remove-images (point-min) (point-max))
520 (erase-buffer)
521 (cd pwd) ;when reuse from other source
522 ;(put-image (create-image (expand-file-name target)) (point))
523 (insert-image-file target)
524 (setq img (plist-get (text-properties-at (point)) 'intangible))
525 (YaTeX-preview-image-mode)
526 (if img
527 (let ((height (cdr (image-size img))))
528 (enlarge-window
529 (- (ceiling (min height (/ (frame-height) 2)))
530 (window-height)))))
531 ;; Remember elapsed time, which will be threshold in auto-preview
532 (put 'YaTeX-typeset-conv2image-chain 'elapse
533 (YaTeX-elapsed-time
534 (get 'YaTeX-typeset-conv2image-chain 'start) (current-time))))
535 (t
536 ;; Without direct image, display image with image viewer
537 (YaTeX-system
538 (format "%s %s" YaTeX-cmd-view-images target)
539 "YaTeX-region-image"
540 'noask)
541 )
542 )))))
545 (defvar YaTeX-typeset-environment-timer nil)
546 (defun YaTeX-typeset-environment-timer ()
547 "Update preview image in the
548 Plist: '(buf begPoint endPoint precedingChar 2precedingChar Substring time)"
549 (let*((plist (get 'YaTeX-typeset-environment-timer 'laststate))
550 (b (nth 0 plist))
551 (s (nth 1 plist))
552 (e (nth 2 plist))
553 (p (nth 3 plist))
554 (q (nth 4 plist))
555 (st (nth 5 plist))
556 (tm (nth 6 plist))
557 (thresh (* 2 (or (get 'YaTeX-typeset-conv2image-chain 'elapse) 1))))
558 (cond
559 ;; In minibuffer, do nothing
560 ((minibuffer-window-active-p (selected-window)) nil)
561 ;; If point went out from last environment, cancel timer
562 ;;;((and (message "s=%d, e=%d, p=%d" s e (point)) nil))
563 ((or (not (eq b (window-buffer (selected-window))))
564 (< (point) s)
565 (> (point) e))
566 (YaTeX-typeset-environment-cancel-auto))
567 ;;;((and (message "e=%d, p=%d t=%s" e (point) (current-time)) nil))
568 ;; If recently called, hold
569 ((< (YaTeX-elapsed-time tm (current-time)) thresh)
570 nil)
571 ;; If condition changed from last call, do it
572 ((and (/= p (preceding-char))
573 (/= q (char-after (- (point) 1)))
574 (not (string= st (YaTeX-buffer-substring s e))))
575 (YaTeX-typeset-environment)))))
577 (defun YaTeX-typeset-environment ()
578 "Typeset current environment or paragraph.
579 If region activated, use it."
580 (interactive)
581 (save-excursion
582 (let ((math (YaTeX-in-math-mode-p)) usetimer)
583 (cond
584 ((and (fboundp 'region-active-p) (region-active-p))
585 nil) ;if region is active, use it
586 (math (setq usetimer t) (YaTeX-mark-environment))
587 ((equal (or (YaTeX-inner-environment t) "document") "document")
588 (mark-paragraph))
589 (t (setq usetimer t) (YaTeX-mark-environment)))
590 (if YaTeX-use-image-preview
591 (let ((YaTeX-typeset-buffer (concat "*bg:" YaTeX-typeset-buffer))
592 (b (region-beginning)) (e (region-end)))
593 (put 'YaTeX-typeset-conv2image-chain 'math math)
594 (put 'YaTeX-typeset-conv2image-chain 'srctype nil)
595 (put 'YaTeX-typeset-conv2image-chain 'win (selected-window))
596 (put 'YaTeX-typeset-conv2image-chain 'start (current-time))
597 (put 'YaTeX-typeset-environment-timer 'laststate
598 (list (current-buffer) b e (preceding-char)
599 (char-after (- (point) 2))
600 (YaTeX-buffer-substring b e)
601 (current-time)))
602 (YaTeX-typeset-region 'YaTeX-typeset-conv2image-chain)
603 (if usetimer (YaTeX-typeset-environment-auto)))
604 (YaTeX-typeset-region)))))
606 (defvar YaTeX-on-the-fly-preview-image (string-to-number "0.1")
607 "*Control the on-the-fly update of preview environment by an image.
608 Nil disables on-the-fly update. Otherwise on-the-fly update is enabled
609 with update interval specified by this value.")
611 (defun YaTeX-typeset-environment-auto ()
612 "Turn on on-the-fly preview-image"
613 (if YaTeX-typeset-environment-timer
614 nil
615 (setq YaTeX-typeset-environment-timer
616 (run-with-idle-timer
617 (max (string-to-number "0.1")
618 (cond
619 ((numberp YaTeX-on-the-fly-preview-image)
620 YaTeX-on-the-fly-preview-image)
621 ((stringp YaTeX-on-the-fly-preview-image)
622 (string-to-number YaTeX-on-the-fly-preview-image))
623 (t 1)))
624 t 'YaTeX-typeset-environment-timer))))
626 (defun YaTeX-typeset-environment-cancel-auto ()
627 "Cancel typeset-environment timer."
628 (interactive)
629 (cancel-timer YaTeX-typeset-environment-timer)
630 (setq YaTeX-typeset-environment-timer nil)
631 (message "Auto-preview canceled"))
634 (defun YaTeX-typeset-buffer (&optional pp)
635 "Typeset whole buffer.
636 If %#! usage says other buffer is main text,
637 visit main buffer to confirm if its includeonly list contains current
638 buffer's file. And if it doesn't contain editing text, ask user which
639 action wants to be done, A:Add list, R:Replace list, %:comment-out list.
640 If optional argument PP given as string, PP is considered as post-process
641 command and call it with the same command argument as typesetter without
642 last extension.
643 eg. if PP is \"dvipdfmx\", called commands as follows.
644 platex foo.tex
645 dvipdfmx foo
646 PP command will be called iff typeset command exit successfully"
647 (interactive)
648 (YaTeX-save-buffers)
649 (let*((me (substring (buffer-name) 0 (rindex (buffer-name) ?.)))
650 (mydir (file-name-directory (buffer-file-name)))
651 (cmd (YaTeX-get-latex-command t)) pparg ppcmd bibcmd
652 (cb (current-buffer)))
653 (setq pparg (substring cmd 0 (string-match "[;&]" cmd)) ;rm multistmt
654 pparg (substring pparg (rindex pparg ? )) ;get last arg
655 pparg (substring pparg 0 (rindex pparg ?.)) ;rm ext
656 bibcmd (or (YaTeX-get-builtin "BIBTEX") bibtex-command))
657 (or (string-match "\\s " bibcmd) ;if bibcmd has no spaces,
658 (setq bibcmd (concat bibcmd pparg))) ;append argument(== %#!)
659 (and pp
660 (stringp pp)
661 (setq ppcmd (concat pp pparg)))
662 (if (YaTeX-main-file-p) nil
663 (save-excursion
664 (YaTeX-visit-main t) ;search into main buffer
665 (save-excursion
666 (push-mark (point) t)
667 (goto-char (point-min))
668 (if (and (re-search-forward "^[ ]*\\\\begin{document}" nil t)
669 (re-search-backward "^[ ]*\\\\includeonly{" nil t))
670 (let*
671 ((b (progn (skip-chars-forward "^{") (point)))
672 (e (progn (skip-chars-forward "^}") (1+ (point))))
673 (s (buffer-substring b e)) c
674 (pardir (file-name-directory (buffer-file-name))))
675 (if (string-match (concat "[{,/]" me "[,}]") s)
676 nil ; Nothing to do when it's already in includeonly.
677 (ding)
678 (switch-to-buffer (current-buffer));Display this buffer.
679 (setq
680 me ;;Rewrite my name(me) to contain sub directory name.
681 (concat
682 (if (string-match pardir mydir) ;if mydir is child of main
683 (substring mydir (length pardir)) ;cut absolute path
684 mydir) ;else concat absolute path name.
685 me))
686 (message
687 "`%s' is not in \\includeonly. A)dd R)eplace %%)comment? "
688 me)
689 (setq c (read-char))
690 (cond
691 ((= c ?a)
692 (goto-char (1+ b))
693 (insert me (if (string= s "{}") "" ",")))
694 ((= c ?r)
695 (delete-region (1+ b) (1- e)) (insert me))
696 ((= c ?%)
697 (beginning-of-line) (insert "%"))
698 (t nil))
699 (basic-save-buffer))))
700 (exchange-point-and-mark)))
701 (switch-to-buffer cb)) ;for 19
702 (YaTeX-typeset cmd YaTeX-typeset-buffer nil nil ppcmd)
703 (put 'dvi2-command 'region nil)))
705 (defvar YaTeX-call-command-history nil
706 "Holds history list of YaTeX-call-command-on-file.")
707 (put 'YaTeX-call-command-history 'no-default t)
708 (defun YaTeX-call-command-on-file (base-cmd buffer &optional file)
709 "Call external command BASE-CMD in the BUFFER.
710 By default, pass the basename of current file. Optional 3rd argument
711 FILE changes the default file name."
712 (YaTeX-save-buffers)
713 (let ((default (concat base-cmd " "
714 (let ((me (file-name-nondirectory
715 (or file buffer-file-name))))
716 (if (string-match "\\.tex" me)
717 (substring me 0 (match-beginning 0))
718 me)))))
719 (or YaTeX-call-command-history
720 (setq YaTeX-call-command-history (list default)))
721 (YaTeX-typeset
722 (read-string-with-history
723 "Call command: "
724 (car YaTeX-call-command-history)
725 'YaTeX-call-command-history)
726 buffer)))
728 (defvar YaTeX-call-builtin-on-file)
729 (make-variable-buffer-local 'YaTeX-call-builtin-on-file)
730 (defun YaTeX-call-builtin-on-file (builtin-type &optional default update)
731 "Call command on file specified by BUILTIN-TYPE."
732 (YaTeX-save-buffers)
733 (let*((main (or YaTeX-parent-file
734 (save-excursion (YaTeX-visit-main t) buffer-file-name)))
735 (mainroot (file-name-nondirectory (substring main 0 (rindex main ?.))))
736 (alist YaTeX-call-builtin-on-file)
737 (b-in (or (YaTeX-get-builtin builtin-type)
738 (cdr (assoc builtin-type alist))))
739 (command b-in))
740 (if (or update (null b-in))
741 (progn
742 (setq command (read-string-with-history
743 (format "%s command: " builtin-type)
744 (or b-in
745 (format "%s %s" default mainroot))
746 'YaTeX-call-command-history))
747 (if (or update (null b-in))
748 (if (y-or-n-p "Memorize this command line in this file? ")
749 (YaTeX-getset-builtin builtin-type command) ;keep in a file
750 (setq YaTeX-call-builtin-on-file ;keep in memory
751 (cons (cons builtin-type command)
752 (delete (assoc builtin-type alist) alist)))
753 (message "`%s' kept in memory. Type `%s %s' to override."
754 command
755 (key-description
756 (car (where-is-internal 'universal-argument)))
757 (key-description (this-command-keys)))
758 (sit-for 2)))))
759 (YaTeX-typeset
760 command
761 (format " *YaTeX-%s*" (downcase builtin-type))
762 builtin-type builtin-type)))
764 (defun YaTeX-kill-typeset-process (proc)
765 "Kill process PROC after sending signal to PROC.
766 PROC should be process identifier."
767 (cond
768 ((not (fboundp 'start-process))
769 (error "This system can't have concurrent process."))
770 ((or (null proc) (not (eq (process-status proc) 'run)))
771 (message "Typesetting process is not running."))
772 (t
773 (save-excursion
774 (set-buffer (process-buffer proc))
775 (save-excursion
776 (goto-char (point-max))
777 (beginning-of-line)
778 (if (looking-at "\\? +$")
779 (let ((mp (point-max)))
780 (process-send-string proc "x\n")
781 (while (= mp (point-max)) (sit-for 1))))))
782 (if (eq (process-status proc) 'run)
783 (progn
784 (interrupt-process proc)
785 (delete-process proc))))))
787 (defun YaTeX-system (command name &optional noask basedir)
788 "Execute some COMMAND with process name `NAME'. Not a official function.
789 Optional second argument NOASK skip query when privious process running.
790 Optional third argument BASEDIR changes default-directory there."
791 (save-excursion
792 (let ((df default-directory)
793 (buffer (get-buffer-create (format " *%s*" name)))
794 proc status)
795 (set-buffer buffer)
796 (setq default-directory (cd (or basedir df)))
797 (erase-buffer)
798 (insert (format "Calling `%s'...\n" command)
799 "==Kill this buffer to STOP process==")
800 (YaTeX-showup-buffer buffer 'YaTeX-showup-buffer-bottom-most)
801 (if (not (fboundp 'start-process))
802 (call-process
803 shell-file-name nil buffer nil YaTeX-shell-command-option command)
804 (if (and (setq proc (get-buffer-process buffer))
805 (setq status (process-status proc))
806 (eq status 'run)
807 (not noask)
808 (not
809 (y-or-n-p (format "Process %s is running. Continue?" buffer))))
810 nil
811 (if (eq status 'run)
812 (progn (interrupt-process proc) (delete-process proc)))
813 (set-process-buffer
814 (start-process
815 name buffer shell-file-name YaTeX-shell-command-option command)
816 (get-buffer buffer)))))))
818 (defvar YaTeX-default-paper-type "a4"
819 "*Default paper type.")
820 (defconst YaTeX-paper-type-alist
821 '(("a4paper" . "a4")
822 ("a5paper" . "a5")
823 ("b4paper" . "b4")
824 ("b5paper" . "b5"))
825 "Holds map of options and paper types.")
826 (defconst YaTeX-dvips-paper-option-alist
827 '(("a4" . "-t a4")
828 ("a5" . "-t a5")
829 ("b4" . "-t b4")
830 ("b5" . "-t b5")
831 ("a4r" . "-t landscape"); Can't specify options, `-t a4' and `-t landscape', at the same time.
832 ("a5r" . "-t landscape")
833 ("b4r" . "-t landscape")
834 ("b5r" . "-t landscape"))
835 "Holds map of dvips options and paper types.")
836 (defun YaTeX-get-paper-type ()
837 "Search options in header and return a paper type, such as \"a4\", \"a4r\", etc."
838 (save-excursion
839 (YaTeX-visit-main t)
840 (goto-char (point-min))
841 (let ((opts
842 (if (re-search-forward
843 "^[ \t]*\\\\document\\(style\\|class\\)[ \t]*\\[\\([^]]*\\)\\]" nil t)
844 (YaTeX-split-string (YaTeX-match-string 2) "[ \t]*,[ \t]*"))))
845 (concat
846 (catch 'found-paper
847 (mapcar (lambda (pair)
848 (if (YaTeX-member (car pair) opts)
849 (throw 'found-paper (cdr pair))))
850 YaTeX-paper-type-alist)
851 YaTeX-default-paper-type)
852 (if (YaTeX-member "landscape" opts) (if YaTeX-dos "L" "r") "")))))
854 (defvar YaTeX-preview-command-history nil
855 "Holds minibuffer history of preview command.")
856 (put 'YaTeX-preview-command-history 'no-default t)
857 (defvar YaTeX-preview-file-history nil
858 "Holds minibuffer history of file to preview.")
859 (put 'YaTeX-preview-file-history 'no-default t)
860 (defun YaTeX-preview-default-previewer ()
861 "Return default previewer for this document"
862 (YaTeX-replace-format
863 (or (YaTeX-get-builtin "PREVIEW")
864 (if (eq (get 'dvi2-command 'format) 'pdf)
865 tex-pdfview-command
866 dvi2-command))
867 "p" (format (cond
868 (YaTeX-dos "-y:%s")
869 (t "-paper %s"))
870 (YaTeX-get-paper-type))))
871 (defun YaTeX-preview-default-main (command)
872 "Return default preview target file"
873 (if (get 'dvi2-command 'region)
874 (substring YaTeX-texput-file
875 0 (rindex YaTeX-texput-file ?.))
876 (YaTeX-get-preview-file-name command)))
877 (defun YaTeX-preview (preview-command preview-file &optional as-default)
878 "Execute xdvi (or other) to tex-preview."
879 (interactive
880 (let* ((previewer (YaTeX-preview-default-previewer))
881 (as-default current-prefix-arg)
882 (command (if as-default
883 previewer
884 (read-string-with-history
885 "Preview command: "
886 previewer
887 'YaTeX-preview-command-history)))
888 (target (YaTeX-preview-default-main command))
889 (file (if as-default
890 target
891 (read-string-with-history
892 "Preview file: "
893 target
894 'YaTeX-preview-file-history))))
895 (list command file)))
896 (setq dvi2-command preview-command) ;`dvi2-command' is buffer local
897 (save-excursion
898 (YaTeX-visit-main t)
899 (if YaTeX-dos (setq preview-file (expand-file-name preview-file)))
900 (let ((pbuffer "*dvi-preview*") (dir default-directory))
901 (YaTeX-showup-buffer
902 pbuffer 'YaTeX-showup-buffer-bottom-most)
903 (set-buffer (get-buffer-create pbuffer))
904 (erase-buffer)
905 (setq default-directory dir) ;for 18
906 (cd dir) ;for 19
907 (cond
908 ((not (fboundp 'start-process)) ;if MS-DOS
909 (send-string-to-terminal "\e[2J\e[>5h") ;CLS & hide cursor
910 (call-process shell-file-name "con" "*dvi-preview*" nil
911 YaTeX-shell-command-option
912 (concat preview-command " " preview-file))
913 (send-string-to-terminal "\e[>5l") ;show cursor
914 (redraw-display))
915 ((and (string-match "dviout" preview-command) ;maybe on `kon'
916 (stringp (getenv "TERM"))
917 (string-match "^kon" (getenv "TERM")))
918 (call-process shell-file-name "con" "*dvi-preview*" nil
919 YaTeX-shell-command-option
920 (concat preview-command " " preview-file)))
921 (t ;if UNIX
922 (set-process-buffer
923 (let ((process-connection-type nil))
924 (start-process "preview" "*dvi-preview*" shell-file-name
925 YaTeX-shell-command-option
926 (concat preview-command " " preview-file)))
927 (get-buffer pbuffer))
928 (message
929 (concat "Starting " preview-command
930 " to preview " preview-file)))))))
932 (defvar YaTeX-xdvi-remote-program "xdvi")
933 (defun YaTeX-xdvi-remote-search (&optional region-mode)
934 "Search string at the point on xdvi -remote window.
935 Non-nil for optional argument REGION-MODE specifies the search string
936 by region."
937 (interactive "P")
938 (let ((pb " *xdvi*") str proc)
939 (save-excursion
940 (if region-mode
941 (setq str (buffer-substring (region-beginning) (region-end)))
942 (setq str (buffer-substring
943 (point)
944 (progn (skip-chars-forward "^\n\\\\}") (point)))))
945 (message "Searching `%s'..." str)
946 (if (boundp 'MULE)
947 (define-program-coding-system
948 (regexp-quote pb) (regexp-quote YaTeX-xdvi-remote-program)
949 *euc-japan*))
950 (setq proc
951 (start-process
952 "xdvi" pb YaTeX-xdvi-remote-program
953 "-remote" (format "SloppySearch(%s) " str)
954 (concat (YaTeX-get-preview-file-name) ".dvi")))
955 (message "Searching `%s'...Done" str))))
957 (defun YaTeX-preview-jlfmt-xdvi ()
958 "Call xdvi -sourceposition to DVI corresponding to current main file"
959 (interactive))
961 (defun YaTeX-preview-jump-line ()
962 "Call jump-line function of various previewer on current main file"
963 (interactive)
964 (save-excursion
965 (save-restriction
966 (widen)
967 (let*((pf (or YaTeX-parent-file
968 (save-excursion (YaTeX-visit-main t) (buffer-file-name))))
969 (pdir (file-name-directory pf))
970 (bnr (substring pf 0 (string-match "\\....$" pf)))
971 (cf (file-relative-name (buffer-file-name) pdir))
972 (buffer (get-buffer-create " *preview-jump-line*"))
973 (line (count-lines (point-min) (point-end-of-line)))
974 (previewer (YaTeX-preview-default-previewer))
975 (cmd (cond
976 ((string-match "xdvi" previewer)
977 (format "%s -nofork -sourceposition '%d %s' %s.dvi"
978 YaTeX-xdvi-remote-program
979 line cf bnr))
980 ((string-match "Skim" previewer)
981 (format "%s %d '%s.pdf' '%s'"
982 YaTeX-cmd-displayline line bnr cf))
983 ((string-match "evince" previewer)
984 (format "%s '%s.pdf' %d '%s'"
985 "fwdevince" bnr line cf))
986 ;;
987 ;; These lines below for other PDF viewer is not confirmed
988 ;; yet. If you find correct command line, PLEASE TELL
989 ;; IT TO THE AUTHOR before publishing patch on the web.
990 ;; ..PLEASE PLEASE PLEASE PLEASE PLEASE PLEASE PLEASE..
991 ((string-match "sumatra" previewer) ;;??
992 (format "%s \"%s.pdf\" -forward-search \"%s\" %d"
993 ;;Send patch to the author, please
994 previewer bnr cf line))
995 ((string-match "qpdfview" previewer) ;;??
996 (format "%s '%s.pdf#src:%s:%d:0'"
997 ;;Send patch to the author, please
998 previewer bnr cf line))
999 ((string-match "okular" previewer) ;;??
1000 (format "%s '%s.pdf#src:%d' '%s'"
1001 ;;Send patch to the author, please
1002 previewer bnr line cf))
1003 )))
1004 (YaTeX-system cmd "jump-line" 'noask pdir)))))
1006 (defun YaTeX-goto-corresponding-viewer ()
1007 (let ((cmd (or (YaTeX-get-builtin "!") tex-command)))
1008 (if (string-match "-src\\|synctex=" cmd)
1009 (progn
1010 (YaTeX-preview-jump-line)
1011 t) ;for YaTeX-goto-corresponding-*
1012 nil)))
1014 (defun YaTeX-set-virtual-error-position (file-sym line-sym)
1015 "Replace the value of FILE-SYM, LINE-SYM by virtual error position."
1016 (cond
1017 ((and (get 'dvi2-command 'region)
1018 (> (symbol-value line-sym) (car (get 'dvi2-command 'offset))))
1019 (set file-sym (get 'dvi2-command 'file))
1020 (set line-sym
1021 (+ (- (apply '- (get 'dvi2-command 'offset)))
1022 (symbol-value line-sym)
1023 -1)))))
1025 (defun YaTeX-prev-error ()
1026 "Visit position of previous typeset error or warning.
1027 To avoid making confliction of line numbers by editing, jump to
1028 error or warning lines in reverse order."
1029 (interactive)
1030 (let ((cur-buf (save-excursion (YaTeX-visit-main t) (buffer-name)))
1031 (cur-win (selected-window))
1032 b0 bound errorp error-line typeset-win error-buffer error-win)
1033 (if (null (get-buffer YaTeX-typeset-buffer))
1034 (error "There is no typesetting buffer."))
1035 (YaTeX-showup-buffer YaTeX-typeset-buffer nil t)
1036 (if (and (markerp YaTeX-typeset-marker)
1037 (eq (marker-buffer YaTeX-typeset-marker) (current-buffer)))
1038 (setq bound YaTeX-typeset-marker))
1039 (setq typeset-win (selected-window))
1040 (if (re-search-backward
1041 (concat "\\(" latex-error-regexp "\\)\\|\\("
1042 latex-warning-regexp "\\)")
1043 bound t)
1044 (setq errorp (match-beginning 1))
1045 (select-window cur-win)
1046 (error "No more errors on %s" cur-buf))
1047 (goto-char (setq b0 (match-beginning 0)))
1048 (skip-chars-forward "^0-9" (match-end 0))
1049 (setq error-line
1050 (string-to-int
1051 (buffer-substring
1052 (point)
1053 (progn (skip-chars-forward "0-9" (match-end 0)) (point))))
1054 error-buffer (expand-file-name (YaTeX-get-error-file cur-buf)))
1055 (if (or (null error-line) (equal 0 error-line))
1056 (error "Can't detect error position."))
1057 (YaTeX-set-virtual-error-position 'error-buffer 'error-line)
1058 (setq error-win (get-buffer-window error-buffer))
1059 (select-window cur-win)
1060 (cond
1061 (error-win (select-window error-win))
1062 ((eq (get-lru-window) typeset-win)
1063 (YaTeX-switch-to-buffer error-buffer))
1064 (t (select-window (get-lru-window))
1065 (YaTeX-switch-to-buffer error-buffer)))
1066 (setq error-win (selected-window))
1067 (goto-line error-line)
1068 (message "LaTeX %s in `%s' on line: %d."
1069 (if errorp "error" "warning")
1070 error-buffer error-line)
1071 (select-window typeset-win)
1072 (skip-chars-backward "0-9")
1073 (recenter (/ (window-height) 2))
1074 (sit-for 1)
1075 (goto-char b0)
1076 (select-window error-win)))
1078 (defun YaTeX-jump-error-line ()
1079 "Jump to corresponding line on latex command's error message."
1080 (interactive)
1081 (let (error-line error-file error-buf)
1082 (save-excursion
1083 (beginning-of-line)
1084 (setq error-line (re-search-forward "l[ ines]*\\.?\\([1-9][0-9]*\\)"
1085 (point-end-of-line) t)))
1086 (if (null error-line)
1087 (if (eobp) (insert (this-command-keys))
1088 (error "No line number expression."))
1089 (goto-char (match-beginning 0))
1090 (setq error-line (string-to-int
1091 (buffer-substring (match-beginning 1) (match-end 1)))
1092 error-file (expand-file-name
1093 (YaTeX-get-error-file YaTeX-current-TeX-buffer)))
1094 (YaTeX-set-virtual-error-position 'error-file 'error-line)
1095 (setq error-buf (YaTeX-switch-to-buffer error-file t)))
1096 (if (null error-buf)
1097 (error "`%s' is not found in this directory." error-file))
1098 (YaTeX-showup-buffer error-buf nil t)
1099 (goto-line error-line)))
1101 (defun YaTeX-send-string ()
1102 "Send string to current typeset process."
1103 (interactive)
1104 (if (and (eq (process-status YaTeX-typeset-process) 'run)
1105 (>= (point) (process-mark YaTeX-typeset-process)))
1106 (let ((b (process-mark YaTeX-typeset-process))
1107 (e (point-end-of-line)))
1108 (goto-char b)
1109 (skip-chars-forward " \t" e)
1110 (setq b (point))
1111 (process-send-string
1112 YaTeX-typeset-process (concat (buffer-substring b e) "\n"))
1113 (goto-char e)
1114 (insert "\n")
1115 (set-marker (process-mark YaTeX-typeset-process) (point))
1116 (insert " "))
1117 (ding)))
1119 (defun YaTeX-view-error ()
1120 (interactive)
1121 (if (null (get-buffer YaTeX-typeset-buffer))
1122 (message "No typeset buffer found.")
1123 (let ((win (selected-window)))
1124 (YaTeX-showup-buffer YaTeX-typeset-buffer nil t)
1125 ;; Next 3 lines are obsolete because YaTeX-typesetting-buffer is
1126 ;; automatically scrolled up at typesetting.
1127 ;;(goto-char (point-max))
1128 ;;(forward-line -1)
1129 ;;(recenter -1)
1130 (select-window win))))
1132 (defun YaTeX-get-error-file (default)
1133 "Get current processing file from typesetting log."
1134 (save-excursion
1135 (let(s)
1136 (condition-case () (up-list -1)
1137 (error
1138 (let ((list 0) found)
1139 (while
1140 (and (<= list 0) (not found)
1141 (re-search-backward "\\((\\)\\|\\()\\)" nil t))
1142 (if (equal (match-beginning 0) (match-beginning 2)) ;close paren.
1143 (setq list (1- list)) ;open paren
1144 (setq list (1+ list))
1145 (if (= list 1)
1146 (if (looking-at "\\([^,{}%]+\.\\)tex\\|sty")
1147 (setq found t)
1148 (setq list (1- list)))))))))
1149 (setq s
1150 (buffer-substring
1151 (progn (forward-char 1) (point))
1152 (progn (skip-chars-forward "^ \n" (point-end-of-line))
1153 (point))))
1154 (if (string= "" s) default s))))
1156 (defun YaTeX-put-nonstopmode ()
1157 (if (and (eq major-mode 'yatex-mode) YaTeX-need-nonstop)
1158 (if (re-search-backward "\\\\nonstopmode{}" (point-min) t)
1159 nil ;if already written in text then do nothing
1160 (save-excursion
1161 (YaTeX-visit-main t)
1162 (goto-char (point-min))
1163 (insert "\\nonstopmode{}%_YaTeX_%\n")
1164 (if (buffer-file-name) (basic-save-buffer))))))
1166 (defun YaTeX-remove-nonstopmode ()
1167 (if (and (eq major-mode 'yatex-mode) YaTeX-need-nonstop) ;for speed
1168 (save-excursion
1169 (YaTeX-visit-main t)
1170 (goto-char (point-min))
1171 (forward-line 1)
1172 (narrow-to-region (point-min) (point))
1173 (goto-char (point-min))
1174 (delete-matching-lines "^\\\\nonstopmode\\{\\}%_YaTeX_%$")
1175 (widen))))
1177 (defvar YaTeX-dvi2-command-ext-alist
1178 '(("[agx]dvi\\|dviout" . ".dvi")
1179 ("ghostview\\|gv" . ".ps")
1180 ("acroread\\|xpdf\\|pdfopen\\|Preview\\|TeXShop\\|Skim\\|evince\\|mupdf\\|zathura\\|okular" . ".pdf")))
1182 (defun YaTeX-get-preview-file-name (&optional preview-command)
1183 "Get file name to preview by inquiring YaTeX-get-latex-command"
1184 (if (null preview-command) (setq preview-command dvi2-command))
1185 (let* ((latex-cmd (YaTeX-get-latex-command t))
1186 (rin (rindex latex-cmd ? ))
1187 (fname (if rin (substring latex-cmd (1+ rin)) ""))
1188 (r (YaTeX-assoc-regexp preview-command YaTeX-dvi2-command-ext-alist))
1189 (ext (if r (cdr r) "")))
1190 (and (null r)
1191 (eq (get 'dvi2-command 'format) 'pdf)
1192 (setq ext "pdf"))
1193 (concat
1194 (if (string= fname "")
1195 (setq fname (substring (file-name-nondirectory
1196 (buffer-file-name))
1197 0 -4))
1198 (setq fname (substring fname 0 (rindex fname ?.))))
1199 ext)))
1201 (defun YaTeX-get-latex-command (&optional switch)
1202 "Specify the latex-command name and its argument.
1203 If there is a line which begins with string: \"%#!\", the following
1204 strings are assumed to be the latex-command and arguments. The
1205 default value of latex-command is:
1206 tex-command FileName
1207 and if you write \"%#!jlatex\" in the beginning of certain line.
1208 \"jlatex \" FileName
1209 will be the latex-command,
1210 and you write \"%#!jlatex main.tex\" on some line and argument SWITCH
1211 is non-nil, then
1212 \"jlatex main.tex\"
1214 will be given to the shell."
1215 (let (parent tparent magic)
1216 (setq parent
1217 (cond
1218 (YaTeX-parent-file
1219 (if YaTeX-dos (expand-file-name YaTeX-parent-file)
1220 YaTeX-parent-file))
1221 (t (save-excursion
1222 (YaTeX-visit-main t)
1223 (file-name-nondirectory (buffer-file-name)))))
1224 magic (YaTeX-get-builtin "!")
1225 tparent (file-name-nondirectory parent))
1226 (YaTeX-replace-formats
1227 (cond
1228 (magic
1229 (cond
1230 (switch (if (string-match "\\s [^-]\\S *$" magic) magic
1231 (concat magic " " parent)))
1232 (t (concat (substring magic 0 (string-match "\\s [^-]\\S *$" magic)) " "))))
1233 (t (concat tex-command " " (if switch parent))))
1234 (list (cons "f" tparent)
1235 (cons "r" (substring tparent 0 (rindex tparent ?.)))))))
1237 (defvar YaTeX-lpr-command-history nil
1238 "Holds command line history of YaTeX-lpr.")
1239 (put 'YaTeX-lpr-command-history 'no-default t)
1240 (defvar YaTeX-lpr-ask-page-range-default t)
1241 (defun YaTeX-lpr (arg)
1242 "Print out.
1243 If prefix arg ARG is non nil, call print driver without
1244 page range description."
1245 (interactive "P")
1246 (or YaTeX-lpr-ask-page-range-default (setq arg (not arg)))
1247 (let*((cmd (or (YaTeX-get-builtin "LPR") dviprint-command-format))
1248 from to (lbuffer "*dvi-printing*") dir)
1249 (setq
1250 cmd
1251 (YaTeX-replace-format
1252 cmd "f"
1253 (if (or arg (not (string-match "%f" cmd)))
1254 ""
1255 (YaTeX-replace-format
1256 dviprint-from-format
1257 "b"
1258 (if (string=
1259 (setq from (read-string "From page(default 1): ")) "")
1260 "1" from))))
1262 (setq
1263 cmd
1264 (YaTeX-replace-format
1265 cmd "t"
1266 (if (or arg (not (string-match "%t" cmd))
1267 (string=
1268 (setq to (read-string "To page(default none): ")) ""))
1269 ""
1270 (YaTeX-replace-format dviprint-to-format "e" to)))
1272 (setq
1273 cmd
1274 (YaTeX-replace-format
1275 cmd "p"
1276 (cdr (assoc (YaTeX-get-paper-type) YaTeX-dvips-paper-option-alist))))
1277 (setq cmd
1278 (read-string-with-history
1279 "Edit command line: "
1280 (format cmd
1281 (if (get 'dvi2-command 'region)
1282 (substring YaTeX-texput-file
1283 0 (rindex YaTeX-texput-file ?.))
1284 (YaTeX-get-preview-file-name)))
1285 'YaTeX-lpr-command-history))
1286 (save-excursion
1287 (YaTeX-visit-main t) ;;change execution directory
1288 (setq dir default-directory)
1289 (YaTeX-showup-buffer
1290 lbuffer 'YaTeX-showup-buffer-bottom-most)
1291 (set-buffer (get-buffer-create lbuffer))
1292 (erase-buffer)
1293 (cd dir) ;for 19
1294 (cond
1295 ((not (fboundp 'start-process))
1296 (call-process shell-file-name "con" "*dvi-printing*" nil
1297 YaTeX-shell-command-option cmd))
1298 (t
1299 (set-process-buffer
1300 (let ((process-connection-type nil))
1301 (start-process "print" "*dvi-printing*" shell-file-name
1302 YaTeX-shell-command-option cmd))
1303 (get-buffer lbuffer))
1304 (message "Starting printing command: %s..." cmd))))))
1306 (defun YaTeX-main-file-p ()
1307 "Return if current buffer is main LaTeX source."
1308 (cond
1309 (YaTeX-parent-file
1310 (eq (get-file-buffer YaTeX-parent-file) (current-buffer)))
1311 ((YaTeX-get-builtin "!")
1312 (string-match
1313 (concat "^" (YaTeX-guess-parent (YaTeX-get-builtin "!")))
1314 (buffer-name)))
1315 (t
1316 (save-excursion
1317 (let ((latex-main-id
1318 (concat "^\\s *" YaTeX-ec-regexp "document\\(style\\|class\\)")))
1319 (or (re-search-backward latex-main-id nil t)
1320 (re-search-forward latex-main-id nil t)))))))
1322 (defun YaTeX-visit-main (&optional setbuf)
1323 "Switch buffer to main LaTeX source.
1324 Use set-buffer instead of switch-to-buffer if the optional argument
1325 SETBUF is t(Use it only from Emacs-Lisp program)."
1326 (interactive "P")
1327 (if (and (interactive-p) setbuf) (setq YaTeX-parent-file nil))
1328 (let ((ff (function (lambda (f)
1329 (if setbuf (set-buffer (find-file-noselect f))
1330 (find-file f)))))
1331 b-in main-file mfa YaTeX-create-file-prefix-g
1332 (hilit-auto-highlight (not setbuf)))
1333 (if (setq b-in (YaTeX-get-builtin "!"))
1334 (setq main-file (YaTeX-guess-parent b-in)))
1335 (if YaTeX-parent-file
1336 (setq main-file ;;(get-file-buffer YaTeX-parent-file)
1337 YaTeX-parent-file))
1338 (if (YaTeX-main-file-p)
1339 (if (interactive-p) (message "I think this is main LaTeX source.") nil)
1340 (cond
1341 ((and ;;(interactive-p)
1342 main-file
1343 (cond ((get-file-buffer main-file)
1344 (cond
1345 (setbuf (set-buffer (get-file-buffer main-file)))
1346 ((get-buffer-window (get-file-buffer main-file))
1347 (select-window
1348 (get-buffer-window (get-file-buffer main-file))))
1349 (t (switch-to-buffer (get-file-buffer main-file)))))
1350 ((file-exists-p main-file)
1351 (funcall ff main-file)))))
1352 ;;((and main-file (YaTeX-switch-to-buffer main-file setbuf)))
1353 ((and main-file
1354 (file-exists-p (setq main-file (concat "../" main-file)))
1355 (or b-in
1356 (y-or-n-p (concat (setq mfa (expand-file-name main-file))
1357 " is main file?:"))))
1358 (setq YaTeX-parent-file mfa)
1359 ;(YaTeX-switch-to-buffer main-file setbuf)
1360 (funcall ff main-file)
1362 (t (setq main-file (read-file-name "Enter your main text: " nil nil 1))
1363 (setq YaTeX-parent-file (expand-file-name main-file))
1364 ; (YaTeX-switch-to-buffer main-file setbuf))
1365 (funcall ff main-file))
1366 )))
1367 nil)
1369 (defun YaTeX-guess-parent (command-line)
1370 (setq command-line
1371 (if (string-match "\\s \\([^-]\\S *\\)$" command-line)
1372 (substring command-line (match-beginning 1))
1373 (file-name-nondirectory (buffer-file-name)))
1374 command-line
1375 (concat (if (string-match "\\(.*\\)\\." command-line)
1376 (substring command-line (match-beginning 1) (match-end 1))
1377 command-line)
1378 ".tex")))
1380 (defun YaTeX-visit-main-other-window ()
1381 "Switch to buffer main LaTeX source in other window."
1382 (interactive)
1383 (if (YaTeX-main-file-p) (message "I think this is main LaTeX source.")
1384 (YaTeX-switch-to-buffer-other-window
1385 (concat (YaTeX-get-preview-file-name) ".tex"))))
1387 (defun YaTeX-save-buffers ()
1388 "Save buffers whose major-mode is equal to current major-mode."
1389 (basic-save-buffer)
1390 (let ((cmm major-mode))
1391 (save-excursion
1392 (mapcar (function
1393 (lambda (buf)
1394 (set-buffer buf)
1395 (if (and (buffer-file-name buf)
1396 (eq major-mode cmm)
1397 (buffer-modified-p buf)
1398 (y-or-n-p (format "Save %s" (buffer-name buf))))
1399 (save-buffer buf))))
1400 (buffer-list)))))
1402 (provide 'yatexprc)