yatex

view yatexprc.el @ 399:33bde8c52fcc

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