yatex

view yatexprc.el @ 376:1bbd0c2b340f

When on-the-fly preview activated, overlay indicate its busy state. For overlay visibility, modify some foureground/background faces.
author HIROSE Yuuji <yuuji@gentei.org>
date Sun, 04 Jan 2015 00:39:44 +0900
parents afb8cb06b1d4
children 81413ee9c32d
line source
1 ;;; yatexprc.el --- YaTeX process handler
2 ;;;
3 ;;; (c)1993-2014 by HIROSE Yuuji.[yuuji@yatex.org]
4 ;;; Last modified Sat Jan 3 23:37:49 2015 on firestorm
5 ;;; $Id$
7 ;;; Code:
8 ;(require 'yatex)
9 (require 'yatexlib)
11 (defvar YaTeX-typeset-process nil
12 "Process identifier for jlatex")
14 (defvar YaTeX-typeset-buffer "*YaTeX-typesetting*"
15 "Process buffer for jlatex")
17 (defvar YaTeX-typeset-buffer-syntax nil
18 "*Syntax table for typesetting buffer")
20 (defvar YaTeX-current-TeX-buffer nil
21 "Keeps the buffer on which recently typeset run.")
23 (defvar YaTeX-shell-command-option
24 (or (and (boundp 'shell-command-option) shell-command-option)
25 (and (boundp 'shell-command-switch) shell-command-switch)
26 (if YaTeX-dos "/c" "-c"))
27 "Shell option for command execution.")
29 (defvar YaTeX-latex-message-code
30 ;; (cond
31 ;; (YaTeX-dos (cdr (assq 1 YaTeX-kanji-code-alist)))
32 ;; ((and YaTeX-emacs-20 (member 'undecided (coding-system-list))
33 ;; 'undecided))
34 ;; ((featurep 'mule)
35 ;; (or (and (boundp '*autoconv*) *autoconv*)
36 ;; (and (fboundp 'coding-system-list) 'automatic-conversion)))
37 ;; ((boundp 'NEMACS)
38 ;; (cdr (assq (if YaTeX-dos 1 2) YaTeX-kanji-code-alist))))
39 (cond
40 (YaTeX-dos (cdr (assq 1 YaTeX-kanji-code-alist)))
41 (YaTeX-emacs-20
42 (cdr (assoc latex-message-kanji-code YaTeX-kanji-code-alist)))
43 ((boundp 'MULE)
44 (symbol-value
45 (cdr (assoc latex-message-kanji-code YaTeX-kanji-code-alist))))
46 ((boundp 'NEMACS)
47 latex-message-kanji-code))
48 "Process coding system for LaTeX.")
50 (if YaTeX-typeset-buffer-syntax nil
51 (setq YaTeX-typeset-buffer-syntax
52 (make-syntax-table (standard-syntax-table)))
53 (modify-syntax-entry ?\{ "w" YaTeX-typeset-buffer-syntax)
54 (modify-syntax-entry ?\} "w" YaTeX-typeset-buffer-syntax)
55 (modify-syntax-entry ?\[ "w" YaTeX-typeset-buffer-syntax)
56 (modify-syntax-entry ?\] "w" YaTeX-typeset-buffer-syntax))
58 (defvar YaTeX-typeset-marker nil)
59 (defvar YaTeX-typeset-consumption nil)
60 (make-variable-buffer-local 'YaTeX-typeset-consumption)
61 (defun YaTeX-typeset (command buffer &optional prcname modename ppcmd)
62 "Execute jlatex (or other) to LaTeX typeset."
63 (interactive)
64 (save-excursion
65 (let ((p (point)) (window (selected-window)) execdir (cb (current-buffer))
66 (map YaTeX-typesetting-mode-map)
67 (background (string-match "\\*bg:" buffer))
68 (outcode
69 (cond ((eq major-mode 'yatex-mode) YaTeX-coding-system)
70 ((eq major-mode 'yahtml-mode) yahtml-kanji-code))))
71 (if (and YaTeX-typeset-process
72 (eq (process-status YaTeX-typeset-process) 'run))
73 ;; if tex command is halting.
74 (YaTeX-kill-typeset-process YaTeX-typeset-process))
75 (YaTeX-put-nonstopmode)
76 (setq prcname (or prcname "LaTeX")
77 modename (or modename "typeset"))
78 (if (eq major-mode 'yatex-mode) (YaTeX-visit-main t)) ;;execution dir
79 (setq execdir default-directory)
80 ;;Select lower-most window if there are more than 2 windows and
81 ;;typeset buffer not seen.
82 (if background
83 nil ;do not showup
84 (YaTeX-showup-buffer
85 buffer 'YaTeX-showup-buffer-bottom-most))
86 (set-buffer (get-buffer-create buffer))
87 (setq default-directory execdir)
88 (cd execdir)
89 (erase-buffer)
90 (cond
91 ((not (fboundp 'start-process)) ;YaTeX-dos;if MS-DOS
92 (call-process
93 shell-file-name nil buffer nil YaTeX-shell-command-option command))
94 (t ;if UNIX
95 (set-process-buffer
96 (setq YaTeX-typeset-process
97 (start-process prcname buffer shell-file-name
98 YaTeX-shell-command-option command))
99 (get-buffer buffer))
100 (set-process-sentinel YaTeX-typeset-process 'YaTeX-typeset-sentinel)
101 (put 'YaTeX-typeset-process 'thiscmd command)
102 (put 'YaTeX-typeset-process 'name prcname)
103 (if (fboundp 'current-time)
104 (setq YaTeX-typeset-consumption
105 (cons (cons 'time (current-time))
106 (delq 'time YaTeX-typeset-consumption))))
107 (let ((ppprop (get 'YaTeX-typeset-process 'ppcmd)))
108 (setq ppprop (delq (assq YaTeX-typeset-process ppprop) ppprop))
109 (if ppcmd
110 (setq ppprop (cons (cons YaTeX-typeset-process ppcmd) ppprop)))
111 (put 'YaTeX-typeset-process 'ppcmd ppprop))
112 (if (and (boundp 'bibcmd) bibcmd)
113 (let ((bcprop (get 'YaTeX-typeset-process 'bibcmd)))
114 (setq bcprop (cons
115 (cons YaTeX-typeset-process bibcmd)
116 (delq (assq YaTeX-typeset-process bcprop) bcprop)))
117 (put 'YaTeX-typeset-process 'bibcmd bcprop)))))
118 (message (format "Calling `%s'..." command))
119 (setq YaTeX-current-TeX-buffer (buffer-name))
120 (use-local-map map) ;map may be localized
121 (set-syntax-table YaTeX-typeset-buffer-syntax)
122 (setq mode-name modename)
123 (if YaTeX-typeset-process ;if process is running (maybe on UNIX)
124 (cond ((fboundp 'set-current-process-coding-system)
125 (set-current-process-coding-system
126 YaTeX-latex-message-code outcode))
127 ((fboundp 'set-process-coding-system)
128 (set-process-coding-system
129 YaTeX-typeset-process YaTeX-latex-message-code outcode))
130 (YaTeX-emacs-20
131 (set-buffer-process-coding-system
132 YaTeX-latex-message-code outcode))
133 ((boundp 'NEMACS)
134 (set-kanji-process-code YaTeX-latex-message-code))))
135 (set-marker (or YaTeX-typeset-marker
136 (setq YaTeX-typeset-marker (make-marker)))
137 (point))
138 (insert (format "Call `%s'\n" command))
139 (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 "jpg"
408 "*Nil means not using image preview by [prefix] t e.
409 Acceptable value is one of \"jpg\" or \"png\", which specifies
410 format of preview image.")
411 (defvar YaTeX-preview-image-mode-map nil
412 "Keymap used in YaTeX-preview-image-mode")
413 (defun YaTeX-preview-image-mode ()
414 (interactive)
415 (if YaTeX-preview-image-mode-map
416 nil
417 (let ((map (setq YaTeX-preview-image-mode-map (make-sparse-keymap))))
418 (define-key map "q" (lambda()(interactive)
419 (kill-buffer)
420 (select-window
421 (or (get 'YaTeX-typeset-process 'win)
422 (selected-window)))))
423 (define-key map "j" (lambda()(interactive) (scroll-up 1)))
424 (define-key map "k" (lambda()(interactive) (scroll-up -1)))))
425 (use-local-map YaTeX-preview-image-mode-map))
427 (defvar YaTeX-typeset-pdf2image-chain
428 (cond
429 ((YaTeX-executable-find "pdfcrop") ;Mac OS X
430 (list
431 "pdfcrop --clip %b.pdf tmp.pdf"
432 (if (YaTeX-executable-find "convert")
433 "convert -density %d tmp.pdf %b.%f"
434 "sips -s format %f -s dpiWidth %d -s dpiHeight %d %b.pdf --out %b.%f")
435 "rm -f tmp.pdf")))
436 "*Pipe line of command as a list to create image file from PDF.
437 See also doc-string of YaTeX-typeset-dvi2image-chain.")
439 (defvar YaTeX-typeset-dvi2image-chain
440 (cond
441 ((and (equal YaTeX-use-image-preview "png")
442 (YaTeX-executable-find "dvipng"))
443 (list "dvipng %b.dvi"))
444 ((YaTeX-executable-find YaTeX-cmd-dvips)
445 (list
446 (format "%s -E -o %%b.eps %%b.dvi" YaTeX-cmd-dvips)
447 "convert -alpha off -density %d %b.eps %b.%f")))
448 "*Pipe line of command as a list to create png file from DVI or PDF.
449 %-notation rewritten list:
450 %b basename of target file(\"texput\")
451 %f Output format(\"png\")
452 %d DPI
453 ")
455 (defvar YaTeX-typeset-conv2image-chain-alist
456 (list (cons 'pdf YaTeX-typeset-pdf2image-chain)
457 (cons 'dvi YaTeX-typeset-dvi2image-chain))
458 "Default alist for creating image files from DVI/PDF.
459 The value is generated from YaTeX-typeset-pdf2image-chain and
460 YaTeX-typeset-dvi2image-chain.")
462 (defvar YaTeX-typeset-conv2image-process nil "Process of conv2image chain")
463 (defun YaTeX-typeset-conv2image-chain ()
464 (let*((proc (or YaTeX-typeset-process YaTeX-typeset-conv2image-process))
465 (prevname (process-name proc))
466 (texput "texput")
467 (format YaTeX-use-image-preview)
468 (target (concat texput "." format))
469 (math (get 'YaTeX-typeset-conv2image-chain 'math))
470 (srctype (or (get 'YaTeX-typeset-conv2image-chain 'srctype)
471 (if (file-newer-than-file-p
472 (concat texput ".pdf")
473 (concat texput ".dvi"))
474 'pdf 'dvi)))
475 (chain (cdr (assq srctype YaTeX-typeset-conv2image-chain-alist)))
476 ;; This function is the first evaluation code.
477 ;; If you find these command line does not work
478 ;; on your system, please tell the author
479 ;; which commands should be taken to achieve
480 ;; one-shot png previewing on your system
481 ;; before publishing patch on the Web.
482 ;; Please please please please please.
483 (curproc (member prevname chain))
484 (w (get 'YaTeX-typeset-conv2image-chain 'win))
485 (pwd default-directory)
486 img)
487 (if (not (= (process-exit-status proc) 0))
488 (progn
489 (YaTeX-showup-buffer ;never comes here(?)
490 (current-buffer) 'YaTeX-showup-buffer-bottom-most)
491 (message "Region typesetting FAILED"))
492 (setq command
493 (if curproc (car (cdr-safe curproc)) (car chain)))
494 (if command
495 (let ((cmdline (YaTeX-replace-formats
496 command
497 (list (cons "b" "texput")
498 (cons "f" format)
499 (cons "d" (if math "300" "200"))))))
500 (insert (format "Calling `%s'...\n" cmdline))
501 (set-process-sentinel
502 (setq YaTeX-typeset-conv2image-process
503 (start-process
504 command
505 (current-buffer)
506 shell-file-name YaTeX-shell-command-option
507 cmdline))
508 'YaTeX-typeset-sentinel)
509 (put 'YaTeX-typeset-process 'ppcmd
510 (cons (cons (get-buffer-process (current-buffer))
511 'YaTeX-typeset-conv2image-chain)
512 (get 'YaTeX-typeset-process 'ppcmd))))
513 ;; After all chain executed, display image in current window
514 (cond
515 ((and (featurep 'image) window-system)
516 ;; If direct image displaying available in running Emacs,
517 ;; display target image into the next window in Emacs.
518 (select-window w)
519 ;(setq foo (selected-window))
520 (YaTeX-showup-buffer
521 (get-buffer-create " *YaTeX-region-image*")
522 'YaTeX-showup-buffer-bottom-most t)
523 (remove-images (point-min) (point-max))
524 (erase-buffer)
525 (cd pwd) ;when reuse from other source
526 ;(put-image (create-image (expand-file-name target)) (point))
527 (insert-image-file target)
528 (setq img (plist-get (text-properties-at (point)) 'intangible))
529 (YaTeX-preview-image-mode)
530 (if img
531 (let ((height (cdr (image-size img))))
532 (enlarge-window
533 (- (ceiling (min height (/ (frame-height) 2)))
534 (window-height)))))
535 ;; Remember elapsed time, which will be threshold in auto-preview
536 (put 'YaTeX-typeset-conv2image-chain 'elapse
537 (YaTeX-elapsed-time
538 (get 'YaTeX-typeset-conv2image-chain 'start) (current-time))))
539 (t
540 ;; Without direct image, display image with image viewer
541 (YaTeX-system
542 (format "%s %s" YaTeX-cmd-view-images target)
543 "YaTeX-region-image"
544 'noask)
545 )
546 )))))
549 (defvar YaTeX-typeset-environment-timer nil)
550 (defun YaTeX-typeset-environment-timer ()
551 "Update preview image in the
552 Plist: '(buf begPoint endPoint precedingChar 2precedingChar Substring time)"
553 (let*((plist (get 'YaTeX-typeset-environment-timer 'laststate))
554 (b (nth 0 plist))
555 (s (nth 1 plist))
556 (e (nth 2 plist))
557 (p (nth 3 plist))
558 (q (nth 4 plist))
559 (st (nth 5 plist))
560 (tm (nth 6 plist))
561 (thresh (* 2 (or (get 'YaTeX-typeset-conv2image-chain 'elapse) 1))))
562 (cond
563 ;; In minibuffer, do nothing
564 ((minibuffer-window-active-p (selected-window)) nil)
565 ;; If point went out from last environment, cancel timer
566 ;;;((and (message "s=%d, e=%d, p=%d" s e (point)) nil))
567 ((or (not (eq b (window-buffer (selected-window))))
568 (< (point) s)
569 (> (point) e))
570 (YaTeX-typeset-environment-cancel-auto))
571 ;;;((and (message "e=%d, p=%d t=%s" e (point) (current-time)) nil))
572 ;; If recently called, hold
573 ((< (YaTeX-elapsed-time tm (current-time)) thresh)
574 nil)
575 ;; If condition changed from last call, do it
576 ((and (/= p (preceding-char))
577 (/= q (char-after (- (point) 1)))
578 (not (string= st (YaTeX-buffer-substring s (min e (point-max))))))
579 (YaTeX-typeset-environment)))))
581 (defun YaTeX-typeset-environment ()
582 "Typeset current environment or paragraph.
583 If region activated, use it."
584 (interactive)
585 (save-excursion
586 (let ((math (YaTeX-in-math-mode-p)) usetimer)
587 (cond
588 ((and (fboundp 'region-active-p) (region-active-p))
589 nil) ;if region is active, use it
590 (math (setq usetimer t) (YaTeX-mark-environment))
591 ((equal (or (YaTeX-inner-environment t) "document") "document")
592 (mark-paragraph))
593 (t (setq usetimer t) (YaTeX-mark-environment)))
594 (if YaTeX-use-image-preview
595 (let ((YaTeX-typeset-buffer (concat "*bg:" YaTeX-typeset-buffer))
596 (b (region-beginning)) (e (region-end)))
597 (put 'YaTeX-typeset-conv2image-chain 'math math)
598 (put 'YaTeX-typeset-conv2image-chain 'srctype nil)
599 (put 'YaTeX-typeset-conv2image-chain 'win (selected-window))
600 (put 'YaTeX-typeset-conv2image-chain 'start (current-time))
601 (put 'YaTeX-typeset-environment-timer 'laststate
602 (list (current-buffer) b e (preceding-char)
603 (char-after (- (point) 2))
604 (YaTeX-buffer-substring b e)
605 (current-time)))
606 (YaTeX-typeset-region 'YaTeX-typeset-conv2image-chain)
607 (if usetimer
608 (progn
609 (if YaTeX-on-the-fly-overlay
610 (move-overlay YaTeX-on-the-fly-overlay b e)
611 (overlay-put
612 (setq YaTeX-on-the-fly-overlay (make-overlay b e))
613 'face 'YaTeX-on-the-fly-activated-face))
614 (YaTeX-typeset-environment-auto)
615 )))
616 (YaTeX-typeset-region)))))
618 (defvar YaTeX-on-the-fly-preview-image (string-to-number "0.1")
619 "*Control the on-the-fly update of preview environment by an image.
620 Nil disables on-the-fly update. Otherwise on-the-fly update is enabled
621 with update interval specified by this value.")
623 (defun YaTeX-typeset-environment-auto ()
624 "Turn on on-the-fly preview-image"
625 (if YaTeX-typeset-environment-timer
626 nil
627 (setq YaTeX-typeset-environment-timer
628 (run-with-idle-timer
629 (max (string-to-number "0.1")
630 (cond
631 ((numberp YaTeX-on-the-fly-preview-image)
632 YaTeX-on-the-fly-preview-image)
633 ((stringp YaTeX-on-the-fly-preview-image)
634 (string-to-number YaTeX-on-the-fly-preview-image))
635 (t 1)))
636 t 'YaTeX-typeset-environment-timer))))
638 (defun YaTeX-typeset-environment-cancel-auto ()
639 "Cancel typeset-environment timer."
640 (interactive)
641 (cancel-timer YaTeX-typeset-environment-timer)
642 (setq YaTeX-typeset-environment-timer nil)
643 (delete-overlay YaTeX-on-the-fly-overlay)
644 (message "Auto-preview canceled"))
647 (defun YaTeX-typeset-buffer (&optional pp)
648 "Typeset whole buffer.
649 If %#! usage says other buffer is main text,
650 visit main buffer to confirm if its includeonly list contains current
651 buffer's file. And if it doesn't contain editing text, ask user which
652 action wants to be done, A:Add list, R:Replace list, %:comment-out list.
653 If optional argument PP given as string, PP is considered as post-process
654 command and call it with the same command argument as typesetter without
655 last extension.
656 eg. if PP is \"dvipdfmx\", called commands as follows.
657 platex foo.tex
658 dvipdfmx foo
659 PP command will be called iff typeset command exit successfully"
660 (interactive)
661 (YaTeX-save-buffers)
662 (let*((me (substring (buffer-name) 0 (rindex (buffer-name) ?.)))
663 (mydir (file-name-directory (buffer-file-name)))
664 (cmd (YaTeX-get-latex-command t)) pparg ppcmd bibcmd
665 (cb (current-buffer)))
666 (setq pparg (substring cmd 0 (string-match "[;&]" cmd)) ;rm multistmt
667 pparg (substring pparg (rindex pparg ? )) ;get last arg
668 pparg (substring pparg 0 (rindex pparg ?.)) ;rm ext
669 bibcmd (or (YaTeX-get-builtin "BIBTEX") bibtex-command))
670 (or (string-match "\\s " bibcmd) ;if bibcmd has no spaces,
671 (setq bibcmd (concat bibcmd pparg))) ;append argument(== %#!)
672 (and pp
673 (stringp pp)
674 (setq ppcmd (concat pp pparg)))
675 (if (YaTeX-main-file-p) nil
676 (save-excursion
677 (YaTeX-visit-main t) ;search into main buffer
678 (save-excursion
679 (push-mark (point) t)
680 (goto-char (point-min))
681 (if (and (re-search-forward "^[ ]*\\\\begin{document}" nil t)
682 (re-search-backward "^[ ]*\\\\includeonly{" nil t))
683 (let*
684 ((b (progn (skip-chars-forward "^{") (point)))
685 (e (progn (skip-chars-forward "^}") (1+ (point))))
686 (s (buffer-substring b e)) c
687 (pardir (file-name-directory (buffer-file-name))))
688 (if (string-match (concat "[{,/]" me "[,}]") s)
689 nil ; Nothing to do when it's already in includeonly.
690 (ding)
691 (switch-to-buffer (current-buffer));Display this buffer.
692 (setq
693 me ;;Rewrite my name(me) to contain sub directory name.
694 (concat
695 (if (string-match pardir mydir) ;if mydir is child of main
696 (substring mydir (length pardir)) ;cut absolute path
697 mydir) ;else concat absolute path name.
698 me))
699 (message
700 "`%s' is not in \\includeonly. A)dd R)eplace %%)comment? "
701 me)
702 (setq c (read-char))
703 (cond
704 ((= c ?a)
705 (goto-char (1+ b))
706 (insert me (if (string= s "{}") "" ",")))
707 ((= c ?r)
708 (delete-region (1+ b) (1- e)) (insert me))
709 ((= c ?%)
710 (beginning-of-line) (insert "%"))
711 (t nil))
712 (basic-save-buffer))))
713 (exchange-point-and-mark)))
714 (switch-to-buffer cb)) ;for 19
715 (YaTeX-typeset cmd YaTeX-typeset-buffer nil nil ppcmd)
716 (put 'dvi2-command 'region nil)))
718 (defvar YaTeX-call-command-history nil
719 "Holds history list of YaTeX-call-command-on-file.")
720 (put 'YaTeX-call-command-history 'no-default t)
721 (defun YaTeX-call-command-on-file (base-cmd buffer &optional file)
722 "Call external command BASE-CMD in the BUFFER.
723 By default, pass the basename of current file. Optional 3rd argument
724 FILE changes the default file name."
725 (YaTeX-save-buffers)
726 (let ((default (concat base-cmd " "
727 (let ((me (file-name-nondirectory
728 (or file buffer-file-name))))
729 (if (string-match "\\.tex" me)
730 (substring me 0 (match-beginning 0))
731 me)))))
732 (or YaTeX-call-command-history
733 (setq YaTeX-call-command-history (list default)))
734 (YaTeX-typeset
735 (read-string-with-history
736 "Call command: "
737 (car YaTeX-call-command-history)
738 'YaTeX-call-command-history)
739 buffer)))
741 (defvar YaTeX-call-builtin-on-file)
742 (make-variable-buffer-local 'YaTeX-call-builtin-on-file)
743 (defun YaTeX-call-builtin-on-file (builtin-type &optional default update)
744 "Call command on file specified by BUILTIN-TYPE."
745 (YaTeX-save-buffers)
746 (let*((main (or YaTeX-parent-file
747 (save-excursion (YaTeX-visit-main t) buffer-file-name)))
748 (mainroot (file-name-nondirectory (substring main 0 (rindex main ?.))))
749 (alist YaTeX-call-builtin-on-file)
750 (b-in (or (YaTeX-get-builtin builtin-type)
751 (cdr (assoc builtin-type alist))))
752 (command b-in))
753 (if (or update (null b-in))
754 (progn
755 (setq command (read-string-with-history
756 (format "%s command: " builtin-type)
757 (or b-in
758 (format "%s %s" default mainroot))
759 'YaTeX-call-command-history))
760 (if (or update (null b-in))
761 (if (y-or-n-p "Memorize this command line in this file? ")
762 (YaTeX-getset-builtin builtin-type command) ;keep in a file
763 (setq YaTeX-call-builtin-on-file ;keep in memory
764 (cons (cons builtin-type command)
765 (delete (assoc builtin-type alist) alist)))
766 (message "`%s' kept in memory. Type `%s %s' to override."
767 command
768 (key-description
769 (car (where-is-internal 'universal-argument)))
770 (key-description (this-command-keys)))
771 (sit-for 2)))))
772 (YaTeX-typeset
773 command
774 (format " *YaTeX-%s*" (downcase builtin-type))
775 builtin-type builtin-type)))
777 (defun YaTeX-kill-typeset-process (proc)
778 "Kill process PROC after sending signal to PROC.
779 PROC should be process identifier."
780 (cond
781 ((not (fboundp 'start-process))
782 (error "This system can't have concurrent process."))
783 ((or (null proc) (not (eq (process-status proc) 'run)))
784 (message "Typesetting process is not running."))
785 (t
786 (save-excursion
787 (set-buffer (process-buffer proc))
788 (save-excursion
789 (goto-char (point-max))
790 (beginning-of-line)
791 (if (looking-at "\\? +$")
792 (let ((mp (point-max)))
793 (process-send-string proc "x\n")
794 (while (= mp (point-max)) (sit-for 1))))))
795 (if (eq (process-status proc) 'run)
796 (progn
797 (interrupt-process proc)
798 (delete-process proc))))))
800 (defun YaTeX-system (command name &optional noask basedir)
801 "Execute some COMMAND with process name `NAME'. Not a official function.
802 Optional second argument NOASK skip query when privious process running.
803 Optional third argument BASEDIR changes default-directory there."
804 (save-excursion
805 (let ((df default-directory)
806 (buffer (get-buffer-create (format " *%s*" name)))
807 proc status)
808 (set-buffer buffer)
809 (setq default-directory (cd (or basedir df)))
810 (erase-buffer)
811 (insert (format "Calling `%s'...\n" command)
812 "==Kill this buffer to STOP process==")
813 (YaTeX-showup-buffer buffer 'YaTeX-showup-buffer-bottom-most)
814 (if (not (fboundp 'start-process))
815 (call-process
816 shell-file-name nil buffer nil YaTeX-shell-command-option command)
817 (if (and (setq proc (get-buffer-process buffer))
818 (setq status (process-status proc))
819 (eq status 'run)
820 (not noask)
821 (not
822 (y-or-n-p (format "Process %s is running. Continue?" buffer))))
823 nil
824 (if (eq status 'run)
825 (progn (interrupt-process proc) (delete-process proc)))
826 (set-process-buffer
827 (start-process
828 name buffer shell-file-name YaTeX-shell-command-option command)
829 (get-buffer buffer)))))))
831 (defvar YaTeX-default-paper-type "a4"
832 "*Default paper type.")
833 (defconst YaTeX-paper-type-alist
834 '(("a4paper" . "a4")
835 ("a5paper" . "a5")
836 ("b4paper" . "b4")
837 ("b5paper" . "b5"))
838 "Holds map of options and paper types.")
839 (defconst YaTeX-dvips-paper-option-alist
840 '(("a4" . "-t a4")
841 ("a5" . "-t a5")
842 ("b4" . "-t b4")
843 ("b5" . "-t b5")
844 ("a4r" . "-t landscape"); Can't specify options, `-t a4' and `-t landscape', at the same time.
845 ("a5r" . "-t landscape")
846 ("b4r" . "-t landscape")
847 ("b5r" . "-t landscape"))
848 "Holds map of dvips options and paper types.")
849 (defun YaTeX-get-paper-type ()
850 "Search options in header and return a paper type, such as \"a4\", \"a4r\", etc."
851 (save-excursion
852 (YaTeX-visit-main t)
853 (goto-char (point-min))
854 (let ((opts
855 (if (re-search-forward
856 "^[ \t]*\\\\document\\(style\\|class\\)[ \t]*\\[\\([^]]*\\)\\]" nil t)
857 (YaTeX-split-string (YaTeX-match-string 2) "[ \t]*,[ \t]*"))))
858 (concat
859 (catch 'found-paper
860 (mapcar (lambda (pair)
861 (if (YaTeX-member (car pair) opts)
862 (throw 'found-paper (cdr pair))))
863 YaTeX-paper-type-alist)
864 YaTeX-default-paper-type)
865 (if (YaTeX-member "landscape" opts) (if YaTeX-dos "L" "r") "")))))
867 (defvar YaTeX-preview-command-history nil
868 "Holds minibuffer history of preview command.")
869 (put 'YaTeX-preview-command-history 'no-default t)
870 (defvar YaTeX-preview-file-history nil
871 "Holds minibuffer history of file to preview.")
872 (put 'YaTeX-preview-file-history 'no-default t)
873 (defun YaTeX-preview-default-previewer ()
874 "Return default previewer for this document"
875 (YaTeX-replace-format
876 (or (YaTeX-get-builtin "PREVIEW")
877 (if (eq (get 'dvi2-command 'format) 'pdf)
878 tex-pdfview-command
879 dvi2-command))
880 "p" (format (cond
881 (YaTeX-dos "-y:%s")
882 (t "-paper %s"))
883 (YaTeX-get-paper-type))))
884 (defun YaTeX-preview-default-main (command)
885 "Return default preview target file"
886 (if (get 'dvi2-command 'region)
887 (substring YaTeX-texput-file
888 0 (rindex YaTeX-texput-file ?.))
889 (YaTeX-get-preview-file-name command)))
890 (defun YaTeX-preview (preview-command preview-file &optional as-default)
891 "Execute xdvi (or other) to tex-preview."
892 (interactive
893 (let* ((previewer (YaTeX-preview-default-previewer))
894 (as-default current-prefix-arg)
895 (command (if as-default
896 previewer
897 (read-string-with-history
898 "Preview command: "
899 previewer
900 'YaTeX-preview-command-history)))
901 (target (YaTeX-preview-default-main command))
902 (file (if as-default
903 target
904 (read-string-with-history
905 "Preview file: "
906 target
907 'YaTeX-preview-file-history))))
908 (list command file)))
909 (setq dvi2-command preview-command) ;`dvi2-command' is buffer local
910 (save-excursion
911 (YaTeX-visit-main t)
912 (if YaTeX-dos (setq preview-file (expand-file-name preview-file)))
913 (let ((pbuffer "*dvi-preview*") (dir default-directory))
914 (YaTeX-showup-buffer
915 pbuffer 'YaTeX-showup-buffer-bottom-most)
916 (set-buffer (get-buffer-create pbuffer))
917 (erase-buffer)
918 (setq default-directory dir) ;for 18
919 (cd dir) ;for 19
920 (cond
921 ((not (fboundp 'start-process)) ;if MS-DOS
922 (send-string-to-terminal "\e[2J\e[>5h") ;CLS & hide cursor
923 (call-process shell-file-name "con" "*dvi-preview*" nil
924 YaTeX-shell-command-option
925 (concat preview-command " " preview-file))
926 (send-string-to-terminal "\e[>5l") ;show cursor
927 (redraw-display))
928 ((and (string-match "dviout" preview-command) ;maybe on `kon'
929 (stringp (getenv "TERM"))
930 (string-match "^kon" (getenv "TERM")))
931 (call-process shell-file-name "con" "*dvi-preview*" nil
932 YaTeX-shell-command-option
933 (concat preview-command " " preview-file)))
934 (t ;if UNIX
935 (set-process-buffer
936 (let ((process-connection-type nil))
937 (start-process "preview" "*dvi-preview*" shell-file-name
938 YaTeX-shell-command-option
939 (concat preview-command " " preview-file)))
940 (get-buffer pbuffer))
941 (message
942 (concat "Starting " preview-command
943 " to preview " preview-file)))))))
945 (defvar YaTeX-xdvi-remote-program "xdvi")
946 (defun YaTeX-xdvi-remote-search (&optional region-mode)
947 "Search string at the point on xdvi -remote window.
948 Non-nil for optional argument REGION-MODE specifies the search string
949 by region."
950 (interactive "P")
951 (let ((pb " *xdvi*") str proc)
952 (save-excursion
953 (if region-mode
954 (setq str (buffer-substring (region-beginning) (region-end)))
955 (setq str (buffer-substring
956 (point)
957 (progn (skip-chars-forward "^\n\\\\}") (point)))))
958 (message "Searching `%s'..." str)
959 (if (boundp 'MULE)
960 (define-program-coding-system
961 (regexp-quote pb) (regexp-quote YaTeX-xdvi-remote-program)
962 *euc-japan*))
963 (setq proc
964 (start-process
965 "xdvi" pb YaTeX-xdvi-remote-program
966 "-remote" (format "SloppySearch(%s) " str)
967 (concat (YaTeX-get-preview-file-name) ".dvi")))
968 (message "Searching `%s'...Done" str))))
970 (defun YaTeX-preview-jlfmt-xdvi ()
971 "Call xdvi -sourceposition to DVI corresponding to current main file"
972 (interactive))
974 (defun YaTeX-preview-jump-line ()
975 "Call jump-line function of various previewer on current main file"
976 (interactive)
977 (save-excursion
978 (save-restriction
979 (widen)
980 (let*((pf (or YaTeX-parent-file
981 (save-excursion (YaTeX-visit-main t) (buffer-file-name))))
982 (pdir (file-name-directory pf))
983 (bnr (substring pf 0 (string-match "\\....$" pf)))
984 (cf (file-relative-name (buffer-file-name) pdir))
985 (buffer (get-buffer-create " *preview-jump-line*"))
986 (line (count-lines (point-min) (point-end-of-line)))
987 (previewer (YaTeX-preview-default-previewer))
988 (cmd (cond
989 ((string-match "xdvi" previewer)
990 (format "%s -nofork -sourceposition '%d %s' %s.dvi"
991 YaTeX-xdvi-remote-program
992 line cf bnr))
993 ((string-match "Skim" previewer)
994 (format "%s %d '%s.pdf' '%s'"
995 YaTeX-cmd-displayline line bnr cf))
996 ((string-match "evince" previewer)
997 (format "%s '%s.pdf' %d '%s'"
998 "fwdevince" bnr line cf))
999 ;;
1000 ;; These lines below for other PDF viewer is not confirmed
1001 ;; yet. If you find correct command line, PLEASE TELL
1002 ;; IT TO THE AUTHOR before publishing patch on the web.
1003 ;; ..PLEASE PLEASE PLEASE PLEASE PLEASE PLEASE PLEASE..
1004 ((string-match "sumatra" previewer) ;;??
1005 (format "%s \"%s.pdf\" -forward-search \"%s\" %d"
1006 ;;Send patch to the author, please
1007 previewer bnr cf line))
1008 ((string-match "qpdfview" previewer) ;;??
1009 (format "%s '%s.pdf#src:%s:%d:0'"
1010 ;;Send patch to the author, please
1011 previewer bnr cf line))
1012 ((string-match "okular" previewer) ;;??
1013 (format "%s '%s.pdf#src:%d' '%s'"
1014 ;;Send patch to the author, please
1015 previewer bnr line cf))
1016 )))
1017 (YaTeX-system cmd "jump-line" 'noask pdir)))))
1019 (defun YaTeX-goto-corresponding-viewer ()
1020 (let ((cmd (or (YaTeX-get-builtin "!") tex-command)))
1021 (if (string-match "-src\\|synctex=" cmd)
1022 (progn
1023 (YaTeX-preview-jump-line)
1024 t) ;for YaTeX-goto-corresponding-*
1025 nil)))
1027 (defun YaTeX-set-virtual-error-position (file-sym line-sym)
1028 "Replace the value of FILE-SYM, LINE-SYM by virtual error position."
1029 (cond
1030 ((and (get 'dvi2-command 'region)
1031 (> (symbol-value line-sym) (car (get 'dvi2-command 'offset))))
1032 (set file-sym (get 'dvi2-command 'file))
1033 (set line-sym
1034 (+ (- (apply '- (get 'dvi2-command 'offset)))
1035 (symbol-value line-sym)
1036 -1)))))
1038 (defun YaTeX-prev-error ()
1039 "Visit position of previous typeset error or warning.
1040 To avoid making confliction of line numbers by editing, jump to
1041 error or warning lines in reverse order."
1042 (interactive)
1043 (let ((cur-buf (save-excursion (YaTeX-visit-main t) (buffer-name)))
1044 (cur-win (selected-window))
1045 b0 bound errorp error-line typeset-win error-buffer error-win)
1046 (if (null (get-buffer YaTeX-typeset-buffer))
1047 (error "There is no typesetting buffer."))
1048 (YaTeX-showup-buffer YaTeX-typeset-buffer nil t)
1049 (if (and (markerp YaTeX-typeset-marker)
1050 (eq (marker-buffer YaTeX-typeset-marker) (current-buffer)))
1051 (setq bound YaTeX-typeset-marker))
1052 (setq typeset-win (selected-window))
1053 (if (re-search-backward
1054 (concat "\\(" latex-error-regexp "\\)\\|\\("
1055 latex-warning-regexp "\\)")
1056 bound t)
1057 (setq errorp (match-beginning 1))
1058 (select-window cur-win)
1059 (error "No more errors on %s" cur-buf))
1060 (goto-char (setq b0 (match-beginning 0)))
1061 (skip-chars-forward "^0-9" (match-end 0))
1062 (setq error-line
1063 (string-to-int
1064 (buffer-substring
1065 (point)
1066 (progn (skip-chars-forward "0-9" (match-end 0)) (point))))
1067 error-buffer (expand-file-name (YaTeX-get-error-file cur-buf)))
1068 (if (or (null error-line) (equal 0 error-line))
1069 (error "Can't detect error position."))
1070 (YaTeX-set-virtual-error-position 'error-buffer 'error-line)
1071 (setq error-win (get-buffer-window error-buffer))
1072 (select-window cur-win)
1073 (cond
1074 (error-win (select-window error-win))
1075 ((eq (get-lru-window) typeset-win)
1076 (YaTeX-switch-to-buffer error-buffer))
1077 (t (select-window (get-lru-window))
1078 (YaTeX-switch-to-buffer error-buffer)))
1079 (setq error-win (selected-window))
1080 (goto-line error-line)
1081 (message "LaTeX %s in `%s' on line: %d."
1082 (if errorp "error" "warning")
1083 error-buffer error-line)
1084 (select-window typeset-win)
1085 (skip-chars-backward "0-9")
1086 (recenter (/ (window-height) 2))
1087 (sit-for 1)
1088 (goto-char b0)
1089 (select-window error-win)))
1091 (defun YaTeX-jump-error-line ()
1092 "Jump to corresponding line on latex command's error message."
1093 (interactive)
1094 (let (error-line error-file error-buf)
1095 (save-excursion
1096 (beginning-of-line)
1097 (setq error-line (re-search-forward "l[ ines]*\\.?\\([1-9][0-9]*\\)"
1098 (point-end-of-line) t)))
1099 (if (null error-line)
1100 (if (eobp) (insert (this-command-keys))
1101 (error "No line number expression."))
1102 (goto-char (match-beginning 0))
1103 (setq error-line (string-to-int
1104 (buffer-substring (match-beginning 1) (match-end 1)))
1105 error-file (expand-file-name
1106 (YaTeX-get-error-file YaTeX-current-TeX-buffer)))
1107 (YaTeX-set-virtual-error-position 'error-file 'error-line)
1108 (setq error-buf (YaTeX-switch-to-buffer error-file t)))
1109 (if (null error-buf)
1110 (error "`%s' is not found in this directory." error-file))
1111 (YaTeX-showup-buffer error-buf nil t)
1112 (goto-line error-line)))
1114 (defun YaTeX-send-string ()
1115 "Send string to current typeset process."
1116 (interactive)
1117 (if (and (eq (process-status YaTeX-typeset-process) 'run)
1118 (>= (point) (process-mark YaTeX-typeset-process)))
1119 (let ((b (process-mark YaTeX-typeset-process))
1120 (e (point-end-of-line)))
1121 (goto-char b)
1122 (skip-chars-forward " \t" e)
1123 (setq b (point))
1124 (process-send-string
1125 YaTeX-typeset-process (concat (buffer-substring b e) "\n"))
1126 (goto-char e)
1127 (insert "\n")
1128 (set-marker (process-mark YaTeX-typeset-process) (point))
1129 (insert " "))
1130 (ding)))
1132 (defun YaTeX-view-error ()
1133 (interactive)
1134 (if (null (get-buffer YaTeX-typeset-buffer))
1135 (message "No typeset buffer found.")
1136 (let ((win (selected-window)))
1137 (YaTeX-showup-buffer YaTeX-typeset-buffer nil t)
1138 ;; Next 3 lines are obsolete because YaTeX-typesetting-buffer is
1139 ;; automatically scrolled up at typesetting.
1140 ;;(goto-char (point-max))
1141 ;;(forward-line -1)
1142 ;;(recenter -1)
1143 (select-window win))))
1145 (defun YaTeX-get-error-file (default)
1146 "Get current processing file from typesetting log."
1147 (save-excursion
1148 (let(s)
1149 (condition-case () (up-list -1)
1150 (error
1151 (let ((list 0) found)
1152 (while
1153 (and (<= list 0) (not found)
1154 (re-search-backward "\\((\\)\\|\\()\\)" nil t))
1155 (if (equal (match-beginning 0) (match-beginning 2)) ;close paren.
1156 (setq list (1- list)) ;open paren
1157 (setq list (1+ list))
1158 (if (= list 1)
1159 (if (looking-at "\\([^,{}%]+\.\\)tex\\|sty")
1160 (setq found t)
1161 (setq list (1- list)))))))))
1162 (setq s
1163 (buffer-substring
1164 (progn (forward-char 1) (point))
1165 (progn (skip-chars-forward "^ \n" (point-end-of-line))
1166 (point))))
1167 (if (string= "" s) default s))))
1169 (defun YaTeX-put-nonstopmode ()
1170 (if (and (eq major-mode 'yatex-mode) YaTeX-need-nonstop)
1171 (if (re-search-backward "\\\\nonstopmode{}" (point-min) t)
1172 nil ;if already written in text then do nothing
1173 (save-excursion
1174 (YaTeX-visit-main t)
1175 (goto-char (point-min))
1176 (insert "\\nonstopmode{}%_YaTeX_%\n")
1177 (if (buffer-file-name) (basic-save-buffer))))))
1179 (defun YaTeX-remove-nonstopmode ()
1180 (if (and (eq major-mode 'yatex-mode) YaTeX-need-nonstop) ;for speed
1181 (save-excursion
1182 (YaTeX-visit-main t)
1183 (goto-char (point-min))
1184 (forward-line 1)
1185 (narrow-to-region (point-min) (point))
1186 (goto-char (point-min))
1187 (delete-matching-lines "^\\\\nonstopmode\\{\\}%_YaTeX_%$")
1188 (widen))))
1190 (defvar YaTeX-dvi2-command-ext-alist
1191 '(("[agx]dvi\\|dviout" . ".dvi")
1192 ("ghostview\\|gv" . ".ps")
1193 ("acroread\\|xpdf\\|pdfopen\\|Preview\\|TeXShop\\|Skim\\|evince\\|mupdf\\|zathura\\|okular" . ".pdf")))
1195 (defun YaTeX-get-preview-file-name (&optional preview-command)
1196 "Get file name to preview by inquiring YaTeX-get-latex-command"
1197 (if (null preview-command) (setq preview-command dvi2-command))
1198 (let* ((latex-cmd (YaTeX-get-latex-command t))
1199 (rin (rindex latex-cmd ? ))
1200 (fname (if rin (substring latex-cmd (1+ rin)) ""))
1201 (r (YaTeX-assoc-regexp preview-command YaTeX-dvi2-command-ext-alist))
1202 (ext (if r (cdr r) "")))
1203 (and (null r)
1204 (eq (get 'dvi2-command 'format) 'pdf)
1205 (setq ext "pdf"))
1206 (concat
1207 (if (string= fname "")
1208 (setq fname (substring (file-name-nondirectory
1209 (buffer-file-name))
1210 0 -4))
1211 (setq fname (substring fname 0 (rindex fname ?.))))
1212 ext)))
1214 (defun YaTeX-get-latex-command (&optional switch)
1215 "Specify the latex-command name and its argument.
1216 If there is a line which begins with string: \"%#!\", the following
1217 strings are assumed to be the latex-command and arguments. The
1218 default value of latex-command is:
1219 tex-command FileName
1220 and if you write \"%#!jlatex\" in the beginning of certain line.
1221 \"jlatex \" FileName
1222 will be the latex-command,
1223 and you write \"%#!jlatex main.tex\" on some line and argument SWITCH
1224 is non-nil, then
1225 \"jlatex main.tex\"
1227 will be given to the shell."
1228 (let (parent tparent magic)
1229 (setq parent
1230 (cond
1231 (YaTeX-parent-file
1232 (if YaTeX-dos (expand-file-name YaTeX-parent-file)
1233 YaTeX-parent-file))
1234 (t (save-excursion
1235 (YaTeX-visit-main t)
1236 (file-name-nondirectory (buffer-file-name)))))
1237 magic (YaTeX-get-builtin "!")
1238 tparent (file-name-nondirectory parent))
1239 (YaTeX-replace-formats
1240 (cond
1241 (magic
1242 (cond
1243 (switch (if (string-match "\\s [^-]\\S *$" magic) magic
1244 (concat magic " " parent)))
1245 (t (concat (substring magic 0 (string-match "\\s [^-]\\S *$" magic)) " "))))
1246 (t (concat tex-command " " (if switch parent))))
1247 (list (cons "f" tparent)
1248 (cons "r" (substring tparent 0 (rindex tparent ?.)))))))
1250 (defvar YaTeX-lpr-command-history nil
1251 "Holds command line history of YaTeX-lpr.")
1252 (put 'YaTeX-lpr-command-history 'no-default t)
1253 (defvar YaTeX-lpr-ask-page-range-default t)
1254 (defun YaTeX-lpr (arg)
1255 "Print out.
1256 If prefix arg ARG is non nil, call print driver without
1257 page range description."
1258 (interactive "P")
1259 (or YaTeX-lpr-ask-page-range-default (setq arg (not arg)))
1260 (let*((cmd (or (YaTeX-get-builtin "LPR") dviprint-command-format))
1261 from to (lbuffer "*dvi-printing*") dir)
1262 (setq
1263 cmd
1264 (YaTeX-replace-format
1265 cmd "f"
1266 (if (or arg (not (string-match "%f" cmd)))
1267 ""
1268 (YaTeX-replace-format
1269 dviprint-from-format
1270 "b"
1271 (if (string=
1272 (setq from (read-string "From page(default 1): ")) "")
1273 "1" from))))
1275 (setq
1276 cmd
1277 (YaTeX-replace-format
1278 cmd "t"
1279 (if (or arg (not (string-match "%t" cmd))
1280 (string=
1281 (setq to (read-string "To page(default none): ")) ""))
1282 ""
1283 (YaTeX-replace-format dviprint-to-format "e" to)))
1285 (setq
1286 cmd
1287 (YaTeX-replace-format
1288 cmd "p"
1289 (cdr (assoc (YaTeX-get-paper-type) YaTeX-dvips-paper-option-alist))))
1290 (setq cmd
1291 (read-string-with-history
1292 "Edit command line: "
1293 (format cmd
1294 (if (get 'dvi2-command 'region)
1295 (substring YaTeX-texput-file
1296 0 (rindex YaTeX-texput-file ?.))
1297 (YaTeX-get-preview-file-name)))
1298 'YaTeX-lpr-command-history))
1299 (save-excursion
1300 (YaTeX-visit-main t) ;;change execution directory
1301 (setq dir default-directory)
1302 (YaTeX-showup-buffer
1303 lbuffer 'YaTeX-showup-buffer-bottom-most)
1304 (set-buffer (get-buffer-create lbuffer))
1305 (erase-buffer)
1306 (cd dir) ;for 19
1307 (cond
1308 ((not (fboundp 'start-process))
1309 (call-process shell-file-name "con" "*dvi-printing*" nil
1310 YaTeX-shell-command-option cmd))
1311 (t
1312 (set-process-buffer
1313 (let ((process-connection-type nil))
1314 (start-process "print" "*dvi-printing*" shell-file-name
1315 YaTeX-shell-command-option cmd))
1316 (get-buffer lbuffer))
1317 (message "Starting printing command: %s..." cmd))))))
1319 (defun YaTeX-main-file-p ()
1320 "Return if current buffer is main LaTeX source."
1321 (cond
1322 (YaTeX-parent-file
1323 (eq (get-file-buffer YaTeX-parent-file) (current-buffer)))
1324 ((YaTeX-get-builtin "!")
1325 (string-match
1326 (concat "^" (YaTeX-guess-parent (YaTeX-get-builtin "!")))
1327 (buffer-name)))
1328 (t
1329 (save-excursion
1330 (let ((latex-main-id
1331 (concat "^\\s *" YaTeX-ec-regexp "document\\(style\\|class\\)")))
1332 (or (re-search-backward latex-main-id nil t)
1333 (re-search-forward latex-main-id nil t)))))))
1335 (defun YaTeX-visit-main (&optional setbuf)
1336 "Switch buffer to main LaTeX source.
1337 Use set-buffer instead of switch-to-buffer if the optional argument
1338 SETBUF is t(Use it only from Emacs-Lisp program)."
1339 (interactive "P")
1340 (if (and (interactive-p) setbuf) (setq YaTeX-parent-file nil))
1341 (let ((ff (function (lambda (f)
1342 (if setbuf (set-buffer (find-file-noselect f))
1343 (find-file f)))))
1344 b-in main-file mfa YaTeX-create-file-prefix-g
1345 (hilit-auto-highlight (not setbuf)))
1346 (if (setq b-in (YaTeX-get-builtin "!"))
1347 (setq main-file (YaTeX-guess-parent b-in)))
1348 (if YaTeX-parent-file
1349 (setq main-file ;;(get-file-buffer YaTeX-parent-file)
1350 YaTeX-parent-file))
1351 (if (YaTeX-main-file-p)
1352 (if (interactive-p) (message "I think this is main LaTeX source.") nil)
1353 (cond
1354 ((and ;;(interactive-p)
1355 main-file
1356 (cond ((get-file-buffer main-file)
1357 (cond
1358 (setbuf (set-buffer (get-file-buffer main-file)))
1359 ((get-buffer-window (get-file-buffer main-file))
1360 (select-window
1361 (get-buffer-window (get-file-buffer main-file))))
1362 (t (switch-to-buffer (get-file-buffer main-file)))))
1363 ((file-exists-p main-file)
1364 (funcall ff main-file)))))
1365 ;;((and main-file (YaTeX-switch-to-buffer main-file setbuf)))
1366 ((and main-file
1367 (file-exists-p (setq main-file (concat "../" main-file)))
1368 (or b-in
1369 (y-or-n-p (concat (setq mfa (expand-file-name main-file))
1370 " is main file?:"))))
1371 (setq YaTeX-parent-file mfa)
1372 ;(YaTeX-switch-to-buffer main-file setbuf)
1373 (funcall ff main-file)
1375 (t (setq main-file (read-file-name "Enter your main text: " nil nil 1))
1376 (setq YaTeX-parent-file (expand-file-name main-file))
1377 ; (YaTeX-switch-to-buffer main-file setbuf))
1378 (funcall ff main-file))
1379 )))
1380 nil)
1382 (defun YaTeX-guess-parent (command-line)
1383 (setq command-line
1384 (if (string-match "\\s \\([^-]\\S *\\)$" command-line)
1385 (substring command-line (match-beginning 1))
1386 (file-name-nondirectory (buffer-file-name)))
1387 command-line
1388 (concat (if (string-match "\\(.*\\)\\." command-line)
1389 (substring command-line (match-beginning 1) (match-end 1))
1390 command-line)
1391 ".tex")))
1393 (defun YaTeX-visit-main-other-window ()
1394 "Switch to buffer main LaTeX source in other window."
1395 (interactive)
1396 (if (YaTeX-main-file-p) (message "I think this is main LaTeX source.")
1397 (YaTeX-switch-to-buffer-other-window
1398 (concat (YaTeX-get-preview-file-name) ".tex"))))
1400 (defun YaTeX-save-buffers ()
1401 "Save buffers whose major-mode is equal to current major-mode."
1402 (basic-save-buffer)
1403 (let ((cmm major-mode))
1404 (save-excursion
1405 (mapcar (function
1406 (lambda (buf)
1407 (set-buffer buf)
1408 (if (and (buffer-file-name buf)
1409 (eq major-mode cmm)
1410 (buffer-modified-p buf)
1411 (y-or-n-p (format "Save %s" (buffer-name buf))))
1412 (save-buffer buf))))
1413 (buffer-list)))))
1415 (provide 'yatexprc)