yatex

view yatexprc.el @ 423:e1e67b1b70e6

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