yatex

view yatexprc.el @ 519:a6a80e2b6f5a

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