yatex

view yatexprc.el @ 611:e87c3271b8fd

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