yatex

view yatexprc.el @ 596:923a6c0183be

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