yatex

view yatexprc.el @ 582:f4c2dca86202

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