yatex

view yatexprc.el @ 83:680d92784c1a

Initial revision
author yuuji
date Sun, 27 Sep 2009 13:04:14 +0000
parents 0734be649cb8
children f14ec50103d0
line source
1 ;;; -*- Emacs-Lisp -*-
2 ;;; YaTeX process handler.
3 ;;; yatexprc.el
4 ;;; (c)1993-2006 by HIROSE Yuuji.[yuuji@yatex.org]
5 ;;; Last modified Sun Dec 24 15:12:50 2006 on firestorm
6 ;;; $Id$
8 ;(require 'yatex)
9 (require 'yatexlib)
11 (defvar YaTeX-typeset-process nil
12 "Process identifier for jlatex")
14 (defvar YaTeX-typeset-buffer "*YaTeX-typesetting*"
15 "Process buffer for jlatex")
17 (defvar YaTeX-typeset-buffer-syntax nil
18 "*Syntax table for typesetting buffer")
20 (defvar YaTeX-current-TeX-buffer nil
21 "Keeps the buffer on which recently typeset run.")
23 (defvar YaTeX-shell-command-option
24 (or (and (boundp 'shell-command-option) shell-command-option)
25 (and (boundp 'shell-command-switch) shell-command-switch)
26 (if YaTeX-dos "/c" "-c"))
27 "Shell option for command execution.")
29 (defvar YaTeX-latex-message-code
30 ;; (cond
31 ;; (YaTeX-dos (cdr (assq 1 YaTeX-kanji-code-alist)))
32 ;; ((and YaTeX-emacs-20 (member 'undecided (coding-system-list))
33 ;; 'undecided))
34 ;; ((featurep 'mule)
35 ;; (or (and (boundp '*autoconv*) *autoconv*)
36 ;; (and (fboundp 'coding-system-list) 'automatic-conversion)))
37 ;; ((boundp 'NEMACS)
38 ;; (cdr (assq (if YaTeX-dos 1 2) YaTeX-kanji-code-alist))))
39 (cond
40 (YaTeX-dos (cdr (assq 1 YaTeX-kanji-code-alist)))
41 (YaTeX-emacs-20
42 (cdr (assoc latex-message-kanji-code YaTeX-kanji-code-alist)))
43 ((boundp 'MULE)
44 (symbol-value
45 (cdr (assoc latex-message-kanji-code YaTeX-kanji-code-alist))))
46 ((boundp 'NEMACS)
47 latex-message-kanji-code))
48 "Process coding system for LaTeX.")
50 (if YaTeX-typeset-buffer-syntax nil
51 (setq YaTeX-typeset-buffer-syntax
52 (make-syntax-table (standard-syntax-table)))
53 (modify-syntax-entry ?\{ "w" YaTeX-typeset-buffer-syntax)
54 (modify-syntax-entry ?\} "w" YaTeX-typeset-buffer-syntax)
55 (modify-syntax-entry ?\[ "w" YaTeX-typeset-buffer-syntax)
56 (modify-syntax-entry ?\] "w" YaTeX-typeset-buffer-syntax))
58 (defun YaTeX-typeset (command buffer &optional prcname modename)
59 "Execute jlatex (or other) to LaTeX typeset."
60 (interactive)
61 (save-excursion
62 (let ((p (point)) (window (selected-window)) execdir (cb (current-buffer))
63 (map YaTeX-typesetting-mode-map)
64 (outcode
65 (cond ((eq major-mode 'yatex-mode) YaTeX-coding-system)
66 ((eq major-mode 'yahtml-mode) yahtml-kanji-code))))
67 (if (and YaTeX-typeset-process
68 (eq (process-status YaTeX-typeset-process) 'run))
69 ;; if tex command is halting.
70 (YaTeX-kill-typeset-process YaTeX-typeset-process))
71 (YaTeX-put-nonstopmode)
72 (setq prcname (or prcname "LaTeX")
73 modename (or modename "typeset"))
74 (if (eq major-mode 'yatex-mode) (YaTeX-visit-main t)) ;;execution dir
75 (setq execdir default-directory)
76 ;;Select lower-most window if there are more than 2 windows and
77 ;;typeset buffer not seen.
78 (YaTeX-showup-buffer
79 buffer (function (lambda (x) (nth 3 (window-edges x)))))
80 (set-buffer (get-buffer-create buffer))
81 (setq default-directory execdir)
82 (cd execdir)
83 (erase-buffer)
84 (cond
85 ((not (fboundp 'start-process)) ;YaTeX-dos;if MS-DOS
86 (call-process
87 shell-file-name nil buffer nil YaTeX-shell-command-option command))
88 (t ;if UNIX
89 (set-process-buffer
90 (setq YaTeX-typeset-process
91 (start-process prcname buffer shell-file-name
92 YaTeX-shell-command-option command))
93 (get-buffer buffer))
94 (set-process-sentinel YaTeX-typeset-process 'YaTeX-typeset-sentinel)))
95 (message (format "Calling `%s'..." command))
96 (setq YaTeX-current-TeX-buffer (buffer-name))
97 (use-local-map map) ;map may be localized
98 (set-syntax-table YaTeX-typeset-buffer-syntax)
99 (setq mode-name modename)
100 (if YaTeX-typeset-process ;if process is running (maybe on UNIX)
101 (cond ((fboundp 'set-current-process-coding-system)
102 (set-current-process-coding-system
103 YaTeX-latex-message-code outcode))
104 ((fboundp 'set-process-coding-system)
105 (set-process-coding-system
106 YaTeX-typeset-process YaTeX-latex-message-code outcode))
107 (YaTeX-emacs-20
108 (set-buffer-process-coding-system
109 YaTeX-latex-message-code outcode))
110 ((boundp 'NEMACS)
111 (set-kanji-process-code YaTeX-latex-message-code))))
112 (if YaTeX-dos (message "Done.")
113 (insert " ")
114 (set-marker (process-mark YaTeX-typeset-process) (1- (point))))
115 (if (bolp) (forward-line -1)) ;what for?
116 (if (and YaTeX-emacs-19 window-system)
117 (let ((win (get-buffer-window buffer t)) owin)
118 (select-frame (window-frame win))
119 (setq owin (selected-window))
120 (select-window win)
121 (goto-char (point-max))
122 (recenter -1)
123 (select-window owin))
124 (select-window (get-buffer-window buffer))
125 (goto-char (point-max))
126 (recenter -1))
127 (select-window window)
128 (switch-to-buffer cb)
129 (YaTeX-remove-nonstopmode))))
131 (defun YaTeX-typeset-sentinel (proc mes)
132 (cond ((null (buffer-name (process-buffer proc)))
133 ;; buffer killed
134 (set-process-buffer proc nil))
135 ((memq (process-status proc) '(signal exit))
136 (let* ((obuf (current-buffer)) (pbuf (process-buffer proc))
137 (pwin (get-buffer-window pbuf))
138 (owin (selected-window)) win)
139 ;; save-excursion isn't the right thing if
140 ;; process-buffer is current-buffer
141 (unwind-protect
142 (progn
143 ;; Write something in *typesetting* and hack its mode line
144 (if pwin
145 (select-window pwin)
146 (set-buffer pbuf))
147 ;;(YaTeX-showup-buffer pbuf nil t)
148 (goto-char (point-max))
149 (if pwin (recenter -3))
150 (insert ?\n mode-name " " mes)
151 (forward-char -1)
152 (insert " at " (substring (current-time-string) 0 -5) "\n")
153 (forward-char 1)
154 (setq mode-line-process
155 (concat ": "
156 (symbol-name (process-status proc))))
157 (message mode-name " %s."
158 (if (eq (process-status proc) 'exit)
159 "done" "ceased"))
160 ;; If buffer and mode line shows that the process
161 ;; is dead, we can delete it now. Otherwise it
162 ;; will stay around until M-x list-processes.
163 (delete-process proc)
164 )
165 (setq YaTeX-typeset-process nil)
166 ;; Force mode line redisplay soon
167 (set-buffer-modified-p (buffer-modified-p))
168 )
169 (select-window owin)
170 (set-buffer obuf)))))
172 (defvar YaTeX-texput-file "texput.tex"
173 "*File name for temporary file of typeset-region.")
175 (defun YaTeX-typeset-region ()
176 "Paste the region to the file `texput.tex' and execute typesetter.
177 The region is specified by the rule:
178 (1)If keyword `%#BEGIN' is found in the upper direction from (point).
179 (1-1)if the keyword `%#END' is found after `%#BEGIN',
180 ->Assume the text between `%#BEGIN' and `%#END' as region.
181 (1-2)if the keyword `%#END' is not found anywhere after `%#BEGIN',
182 ->Assume the text after `%#BEGIN' as region.
183 (2)If no `%#BEGIN' usage is found before the (point),
184 ->Assume the text between current (point) and (mark) as region.
185 DON'T forget to eliminate the `%#BEGIN/%#END' notation after editing
186 operation to the region."
187 (interactive)
188 (save-excursion
189 (let*
190 ((end "") typeout ;Type out message that tells the method of cutting.
191 (texput YaTeX-texput-file)
192 (cmd (concat (YaTeX-get-latex-command nil) " " texput))
193 (buffer (current-buffer)) opoint preamble (subpreamble "") main
194 (hilit-auto-highlight nil) ;for Emacs19 with hilit19
195 reg-begin reg-end lineinfo)
197 (save-excursion
198 (if (search-backward "%#BEGIN" nil t)
199 (progn
200 (setq typeout "--- Region from BEGIN to "
201 end "the end of the buffer ---"
202 reg-begin (match-end 0))
203 (if (search-forward "%#END" nil t)
204 (setq reg-end (match-beginning 0)
205 end "END ---")
206 (setq reg-end (point-max))))
207 (setq typeout "=== Region from (point) to (mark) ==="
208 reg-begin (point) reg-end (mark)))
209 (goto-char (min reg-begin reg-end))
210 (setq lineinfo (count-lines (point-min) (point-end-of-line)))
211 (goto-char (point-min))
212 (while (search-forward "%#REQUIRE" nil t)
213 (setq subpreamble
214 (concat subpreamble
215 (cond
216 ((eolp)
217 (buffer-substring
218 (match-beginning 0)
219 (point-beginning-of-line)))
220 (t (buffer-substring
221 (match-end 0)
222 (point-end-of-line))))
223 "\n"))
224 (goto-char (match-end 0))))
225 (YaTeX-visit-main t)
226 (setq main (current-buffer))
227 (setq opoint (point))
228 (goto-char (point-min))
229 (setq
230 preamble
231 (if (re-search-forward "^[ ]*\\\\begin.*{document}" nil t)
232 (buffer-substring (point-min) (match-end 0))
233 (concat
234 (if YaTeX-use-LaTeX2e "\\documentclass{" "\\documentstyle{")
235 YaTeX-default-document-style "}\n"
236 "\\begin{document}")))
237 (goto-char opoint)
238 ;;(set-buffer buffer) ;for clarity
239 (let ((hilit-auto-highlight nil))
240 (set-buffer (find-file-noselect texput)))
241 ;;(find-file YaTeX-texput-file)
242 (erase-buffer)
243 (if (and (eq major-mode 'yatex-mode) YaTeX-need-nonstop)
244 (insert "\\nonstopmode{}\n"))
245 (insert preamble "\n" subpreamble "\n")
246 (setq lineinfo (list (count-lines 1 (point-end-of-line)) lineinfo))
247 (insert-buffer-substring buffer reg-begin reg-end)
248 (insert "\\typeout{" typeout end "}\n") ;Notice the selected method.
249 (insert "\n\\end{document}\n")
250 (basic-save-buffer)
251 (kill-buffer (current-buffer))
252 (set-buffer main) ;return to parent file or itself.
253 (YaTeX-typeset cmd YaTeX-typeset-buffer)
254 (switch-to-buffer buffer) ;for Emacs-19
255 (put 'dvi2-command 'region t)
256 (put 'dvi2-command 'file buffer)
257 (put 'dvi2-command 'offset lineinfo))))
259 (defun YaTeX-typeset-buffer ()
260 "Typeset whole buffer.
261 If %#! usage says other buffer is main text,
262 visit main buffer to confirm if its includeonly list contains current
263 buffer's file. And if it doesn't contain editing text, ask user which
264 action wants to be done, A:Add list, R:Replace list, %:comment-out list."
265 (interactive)
266 (YaTeX-save-buffers)
267 (let*((me (substring (buffer-name) 0 (rindex (buffer-name) ?.)))
268 (mydir (file-name-directory (buffer-file-name)))
269 (cmd (YaTeX-get-latex-command t)) (cb (current-buffer)))
270 (if (YaTeX-main-file-p) nil
271 (save-excursion
272 (YaTeX-visit-main t) ;search into main buffer
273 (save-excursion
274 (push-mark (point) t)
275 (goto-char (point-min))
276 (if (and (re-search-forward "^[ ]*\\\\begin{document}" nil t)
277 (re-search-backward "^[ ]*\\\\includeonly{" nil t))
278 (let*
279 ((b (progn (skip-chars-forward "^{") (point)))
280 (e (progn (skip-chars-forward "^}") (1+ (point))))
281 (s (buffer-substring b e)) c
282 (pardir (file-name-directory (buffer-file-name))))
283 (if (string-match (concat "[{,/]" me "[,}]") s)
284 nil ; Nothing to do when it's already in includeonly.
285 (ding)
286 (switch-to-buffer (current-buffer));Display this buffer.
287 (setq
288 me ;;Rewrite my name(me) to contain sub directory name.
289 (concat
290 (if (string-match pardir mydir) ;if mydir is child of main
291 (substring mydir (length pardir)) ;cut absolute path
292 mydir) ;else concat absolute path name.
293 me))
294 (message
295 "`%s' is not in \\includeonly. A)dd R)eplace %%)comment? "
296 me)
297 (setq c (read-char))
298 (cond
299 ((= c ?a)
300 (goto-char (1+ b))
301 (insert me (if (string= s "{}") "" ",")))
302 ((= c ?r)
303 (delete-region (1+ b) (1- e)) (insert me))
304 ((= c ?%)
305 (beginning-of-line) (insert "%"))
306 (t nil))
307 (basic-save-buffer))))
308 (exchange-point-and-mark)))
309 (switch-to-buffer cb)) ;for 19
310 (YaTeX-typeset cmd YaTeX-typeset-buffer)
311 (put 'dvi2-command 'region nil)))
313 (defvar YaTeX-call-command-history nil
314 "Holds history list of YaTeX-call-command-on-file.")
315 (put 'YaTeX-call-command-history 'no-default t)
316 (defun YaTeX-call-command-on-file (base-cmd buffer)
317 (YaTeX-save-buffers)
318 (YaTeX-typeset
319 (read-string-with-history
320 "Call command: "
321 (concat base-cmd " "
322 (let ((me (file-name-nondirectory buffer-file-name)))
323 (if (string-match "\\.tex" me)
324 (substring me 0 (match-beginning 0))
325 me)))
326 'YaTeX-call-command-history)
327 buffer))
329 (defun YaTeX-bibtex-buffer (cmd)
330 "Pass the bibliography data of editing file to bibtex."
331 (interactive)
332 (YaTeX-save-buffers)
333 (YaTeX-call-command-on-file cmd "*YaTeX-bibtex*" ))
335 (defun YaTeX-kill-typeset-process (proc)
336 "Kill process PROC after sending signal to PROC.
337 PROC should be process identifier."
338 (cond
339 ((not (fboundp 'start-process))
340 (error "This system can't have concurrent process."))
341 ((or (null proc) (not (eq (process-status proc) 'run)))
342 (message "Typesetting process is not running."))
343 (t
344 (save-excursion
345 (set-buffer (process-buffer proc))
346 (save-excursion
347 (goto-char (point-max))
348 (beginning-of-line)
349 (if (looking-at "\\? +$")
350 (let ((mp (point-max)))
351 (process-send-string proc "x\n")
352 (while (= mp (point-max)) (sit-for 1))))))
353 (if (eq (process-status proc) 'run)
354 (progn
355 (interrupt-process proc)
356 (delete-process proc))))))
358 (defun YaTeX-system (command buffer)
359 "Execute some command on buffer. Not a official function."
360 (save-excursion
361 (YaTeX-showup-buffer
362 buffer (function (lambda (x) (nth 3 (window-edges x)))))
363 (let ((df default-directory)) ;preserve current buf's pwd
364 (set-buffer (get-buffer-create buffer)) ;1.61.3
365 (setq default-directory df)
366 (cd df))
367 (erase-buffer)
368 (if (not (fboundp 'start-process))
369 (call-process
370 shell-file-name nil buffer nil YaTeX-shell-command-option command)
371 (if (and (get-buffer-process buffer)
372 (eq (process-status (get-buffer-process buffer)) 'run)
373 (not
374 (y-or-n-p (format "Process %s is running. Continue?" buffer))))
375 nil
376 (set-process-buffer
377 (start-process
378 "system" buffer shell-file-name YaTeX-shell-command-option command)
379 (get-buffer buffer))))))
381 (defvar YaTeX-default-paper-type "a4"
382 "*Default paper type.")
383 (defconst YaTeX-paper-type-alist
384 '(("a4paper" . "a4")
385 ("a5paper" . "a5")
386 ("b4paper" . "b4")
387 ("b5paper" . "b5"))
388 "Holds map of options and paper types.")
389 (defconst YaTeX-dvips-paper-option-alist
390 '(("a4" . "-t a4")
391 ("a5" . "-t a5")
392 ("b4" . "-t b4")
393 ("b5" . "-t b5")
394 ("a4r" . "-t landscape"); Can't specify options, `-t a4' and `-t landscape', at the same time.
395 ("a5r" . "-t landscape")
396 ("b4r" . "-t landscape")
397 ("b5r" . "-t landscape"))
398 "Holds map of dvips options and paper types.")
399 (defun YaTeX-get-paper-type ()
400 "Search options in header and return a paper type, such as \"a4\", \"a4r\", etc."
401 (save-excursion
402 (YaTeX-visit-main t)
403 (goto-char (point-min))
404 (let ((opts
405 (if (re-search-forward
406 "^[ \t]*\\\\document\\(style\\|class\\)[ \t]*\\[\\([^]]*\\)\\]" nil t)
407 (YaTeX-split-string (YaTeX-match-string 2) "[ \t]*,[ \t]*"))))
408 (concat
409 (catch 'found-paper
410 (mapcar (lambda (pair)
411 (if (YaTeX-member (car pair) opts)
412 (throw 'found-paper (cdr pair))))
413 YaTeX-paper-type-alist)
414 YaTeX-default-paper-type)
415 (if (YaTeX-member "landscape" opts) (if YaTeX-dos "L" "r") "")))))
417 (defvar YaTeX-preview-command-history nil
418 "Holds minibuffer history of preview command.")
419 (put 'YaTeX-preview-command-history 'no-default t)
420 (defvar YaTeX-preview-file-history nil
421 "Holds minibuffer history of file to preview.")
422 (put 'YaTeX-preview-file-history 'no-default t)
423 (defun YaTeX-preview (preview-command preview-file)
424 "Execute xdvi (or other) to tex-preview."
425 (interactive
426 (list
427 (read-string-with-history
428 "Preview command: "
429 (YaTeX-replace-format
430 (or (YaTeX-get-builtin "PREVIEW") dvi2-command)
431 "p" (format (cond
432 (YaTeX-dos "-y:%s")
433 (t "-paper %s"))
434 (YaTeX-get-paper-type)))
435 'YaTeX-preview-command-history)
436 (read-string-with-history
437 "Preview file: "
438 (if (get 'dvi2-command 'region)
439 (substring YaTeX-texput-file
440 0 (rindex YaTeX-texput-file ?.))
441 (YaTeX-get-preview-file-name))
442 'YaTeX-preview-file-history)))
443 (if YaTeX-dos (setq preview-file (expand-file-name preview-file)))
444 (setq dvi2-command preview-command) ;`dvi2command' is buffer local
445 (save-excursion
446 (YaTeX-visit-main t)
447 (let ((pbuffer "*dvi-preview*") (dir default-directory))
448 (YaTeX-showup-buffer
449 pbuffer (function (lambda (x) (nth 3 (window-edges x)))))
450 (set-buffer (get-buffer-create pbuffer))
451 (erase-buffer)
452 (setq default-directory dir) ;for 18
453 (cd dir) ;for 19
454 (cond
455 ((not (fboundp 'start-process)) ;if MS-DOS
456 (send-string-to-terminal "\e[2J\e[>5h") ;CLS & hide cursor
457 (call-process shell-file-name "con" "*dvi-preview*" nil
458 YaTeX-shell-command-option
459 (concat preview-command " " preview-file))
460 (send-string-to-terminal "\e[>5l") ;show cursor
461 (redraw-display))
462 ((and (string-match "dviout" preview-command) ;maybe on `kon'
463 (stringp (getenv "TERM"))
464 (string-match "^kon" (getenv "TERM")))
465 (call-process shell-file-name "con" "*dvi-preview*" nil
466 YaTeX-shell-command-option
467 (concat preview-command " " preview-file)))
468 (t ;if UNIX
469 (set-process-buffer
470 (start-process "preview" "*dvi-preview*" shell-file-name
471 YaTeX-shell-command-option
472 (concat preview-command " " preview-file))
473 (get-buffer pbuffer))
474 (message
475 (concat "Starting " preview-command
476 " to preview " preview-file)))))))
478 (defvar YaTeX-xdvi-remote-program "xdvi")
479 (defun YaTeX-xdvi-remote-search (&optional region-mode)
480 "Search string at the point on xdvi -remote window.
481 Non-nil for optional argument REGION-MODE specifies the search string
482 by region."
483 (interactive "P")
484 (let ((pb " *xdvi*") str proc)
485 (save-excursion
486 (if region-mode
487 (setq str (buffer-substring (region-beginning) (region-end)))
488 (setq str (buffer-substring
489 (point)
490 (progn (skip-chars-forward "^\n\\\\}") (point)))))
491 (message "Searching `%s'..." str)
492 (if (boundp 'MULE)
493 (define-program-coding-system
494 (regexp-quote pb) (regexp-quote YaTeX-xdvi-remote-program)
495 *euc-japan*))
496 (setq proc
497 (start-process
498 "xdvi" pb YaTeX-xdvi-remote-program
499 "-remote" (format "SloppySearch(%s) " str)
500 (concat (YaTeX-get-preview-file-name) ".dvi")))
501 (message "Searching `%s'...Done" str))))
503 (defun YaTeX-set-virtual-error-position (file-sym line-sym)
504 "Replace the value of FILE-SYM, LINE-SYM by virtual error position."
505 (cond
506 ((and (get 'dvi2-command 'region)
507 (> (symbol-value line-sym) (car (get 'dvi2-command 'offset))))
508 (set file-sym (get 'dvi2-command 'file))
509 (set line-sym
510 (+ (- (apply '- (get 'dvi2-command 'offset)))
511 (symbol-value line-sym)
512 -1)))))
514 (defun YaTeX-prev-error ()
515 "Visit previous typeset error.
516 To avoid making confliction of line numbers by editing, jump to
517 error or warning lines in reverse order."
518 (interactive)
519 (let ((cur-buf (buffer-name)) (cur-win (selected-window))
520 b0 errorp error-line typeset-win error-buffer error-win)
521 (if (null (get-buffer YaTeX-typeset-buffer))
522 (error "There is no typesetting buffer."))
523 (YaTeX-showup-buffer YaTeX-typeset-buffer nil t)
524 (setq typeset-win (selected-window))
525 (if (re-search-backward
526 (concat "\\(" latex-error-regexp "\\)\\|\\("
527 latex-warning-regexp "\\)")
528 nil t)
529 (setq errorp (match-beginning 1))
530 (select-window cur-win)
531 (error "No more errors on %s" cur-buf))
532 (goto-char (setq b0 (match-beginning 0)))
533 (skip-chars-forward "^0-9" (match-end 0))
534 (setq error-line
535 (string-to-int
536 (buffer-substring
537 (point)
538 (progn (skip-chars-forward "0-9" (match-end 0)) (point))))
539 error-buffer (expand-file-name (YaTeX-get-error-file cur-buf)))
540 (if (or (null error-line) (equal 0 error-line))
541 (error "Can't detect error position."))
542 (YaTeX-set-virtual-error-position 'error-buffer 'error-line)
543 (setq error-win (get-buffer-window error-buffer))
544 (select-window cur-win)
545 (cond
546 (error-win (select-window error-win))
547 ((eq (get-lru-window) typeset-win)
548 (YaTeX-switch-to-buffer error-buffer))
549 (t (select-window (get-lru-window))
550 (YaTeX-switch-to-buffer error-buffer)))
551 (setq error-win (selected-window))
552 (goto-line error-line)
553 (message "LaTeX %s in `%s' on line: %d."
554 (if errorp "error" "warning")
555 error-buffer error-line)
556 (select-window typeset-win)
557 (skip-chars-backward "0-9")
558 (recenter (/ (window-height) 2))
559 (sit-for 1)
560 (goto-char b0)
561 (select-window error-win)))
563 (defun YaTeX-jump-error-line ()
564 "Jump to corresponding line on latex command's error message."
565 (interactive)
566 (let (error-line error-file error-buf)
567 (save-excursion
568 (beginning-of-line)
569 (setq error-line (re-search-forward "l[ ines]*\\.?\\([1-9][0-9]*\\)"
570 (point-end-of-line) t)))
571 (if (null error-line)
572 (if (eobp) (insert (this-command-keys))
573 (error "No line number expression."))
574 (goto-char (match-beginning 0))
575 (setq error-line (string-to-int
576 (buffer-substring (match-beginning 1) (match-end 1)))
577 error-file (expand-file-name
578 (YaTeX-get-error-file YaTeX-current-TeX-buffer)))
579 (YaTeX-set-virtual-error-position 'error-file 'error-line)
580 (setq error-buf (YaTeX-switch-to-buffer error-file t)))
581 (if (null error-buf)
582 (error "`%s' is not found in this directory." error-file))
583 (YaTeX-showup-buffer error-buf nil t)
584 (goto-line error-line)))
586 (defun YaTeX-send-string ()
587 "Send string to current typeset process."
588 (interactive)
589 (if (and (eq (process-status YaTeX-typeset-process) 'run)
590 (>= (point) (process-mark YaTeX-typeset-process)))
591 (let ((b (process-mark YaTeX-typeset-process))
592 (e (point-end-of-line)))
593 (goto-char b)
594 (skip-chars-forward " \t" e)
595 (setq b (point))
596 (process-send-string
597 YaTeX-typeset-process (concat (buffer-substring b e) "\n"))
598 (goto-char e)
599 (insert "\n")
600 (set-marker (process-mark YaTeX-typeset-process) (point))
601 (insert " "))
602 (ding)))
604 (defun YaTeX-view-error ()
605 (interactive)
606 (if (null (get-buffer YaTeX-typeset-buffer))
607 (message "No typeset buffer found.")
608 (let ((win (selected-window)))
609 (YaTeX-showup-buffer YaTeX-typeset-buffer nil t)
610 ;; Next 3 lines are obsolete because YaTeX-typesetting-buffer is
611 ;; automatically scrolled up at typesetting.
612 ;;(goto-char (point-max))
613 ;;(forward-line -1)
614 ;;(recenter -1)
615 (select-window win))))
617 (defun YaTeX-get-error-file (default)
618 "Get current processing file from typesetting log."
619 (save-excursion
620 (let(s)
621 (condition-case () (up-list -1)
622 (error
623 (let ((list 0) found)
624 (while
625 (and (<= list 0) (not found)
626 (re-search-backward "\\((\\)\\|\\()\\)" nil t))
627 (if (equal (match-beginning 0) (match-beginning 2)) ;close paren.
628 (setq list (1- list)) ;open paren
629 (setq list (1+ list))
630 (if (= list 1)
631 (if (looking-at "\\([^,{}%]+\.\\)tex\\|sty")
632 (setq found t)
633 (setq list (1- list)))))))))
634 (setq s
635 (buffer-substring
636 (progn (forward-char 1) (point))
637 (progn (skip-chars-forward "^ \n" (point-end-of-line))
638 (point))))
639 (if (string= "" s) default s))))
641 (defun YaTeX-put-nonstopmode ()
642 (if (and (eq major-mode 'yatex-mode) YaTeX-need-nonstop)
643 (if (re-search-backward "\\\\nonstopmode{}" (point-min) t)
644 nil ;if already written in text then do nothing
645 (save-excursion
646 (YaTeX-visit-main t)
647 (goto-char (point-min))
648 (insert "\\nonstopmode{}%_YaTeX_%\n")
649 (if (buffer-file-name) (basic-save-buffer))))))
651 (defun YaTeX-remove-nonstopmode ()
652 (if (and (eq major-mode 'yatex-mode) YaTeX-need-nonstop) ;for speed
653 (save-excursion
654 (YaTeX-visit-main t)
655 (goto-char (point-min))
656 (forward-line 1)
657 (narrow-to-region (point-min) (point))
658 (goto-char (point-min))
659 (delete-matching-lines "^\\\\nonstopmode\\{\\}%_YaTeX_%$")
660 (widen))))
662 (defvar YaTeX-dvi2-command-ext-alist
663 '(("[agx]dvi\\|dviout" . ".dvi")
664 ("ghostview\\|gv" . ".ps")
665 ("acroread\\|pdf\\|Preview\\|TeXShop" . ".pdf")))
667 (defun YaTeX-get-preview-file-name ()
668 "Get file name to preview by inquiring YaTeX-get-latex-command"
669 (let* ((latex-cmd (YaTeX-get-latex-command t))
670 (rin (rindex latex-cmd ? ))
671 (fname (if (> rin -1) (substring latex-cmd (1+ rin)) ""))
672 (r (YaTeX-assoc-regexp dvi2-command YaTeX-dvi2-command-ext-alist))
673 (ext (if r (cdr r) ""))
674 (period))
675 (concat
676 (if (string= fname "")
677 (setq fname (substring (file-name-nondirectory
678 (buffer-file-name))
679 0 -4))
680 (setq period (rindex fname ?.))
681 (setq fname (substring fname 0 (if (eq -1 period) nil period))))
682 ext)))
684 (defun YaTeX-get-latex-command (&optional switch)
685 "Specify the latex-command name and its argument.
686 If there is a line which begins with string: \"%#!\", the following
687 strings are assumed to be the latex-command and arguments. The
688 default value of latex-command is:
689 tex-command FileName
690 and if you write \"%#!jlatex\" in the beginning of certain line.
691 \"jlatex \" FileName
692 will be the latex-command,
693 and you write \"%#!jlatex main.tex\" on some line and argument SWITCH
694 is non-nil, then
695 \"jlatex main.tex\"
697 will be given to the shell."
698 (let (parent tparent magic)
699 (setq parent
700 (cond
701 (YaTeX-parent-file
702 (if YaTeX-dos (expand-file-name YaTeX-parent-file)
703 YaTeX-parent-file))
704 (t (save-excursion
705 (YaTeX-visit-main t)
706 (file-name-nondirectory (buffer-file-name)))))
707 magic (YaTeX-get-builtin "!")
708 tparent (file-name-nondirectory parent))
709 (YaTeX-replace-formats
710 (cond
711 (magic
712 (cond
713 (switch (if (string-match "\\s " magic) magic
714 (concat magic " " parent)))
715 (t (concat (substring magic 0 (string-match "\\s " magic)) " "))))
716 (t (concat tex-command " " (if switch parent))))
717 (list (cons "f" tparent)
718 (cons "r" (substring tparent 0 (rindex tparent ?.)))))))
720 (defvar YaTeX-lpr-command-history nil
721 "Holds command line history of YaTeX-lpr.")
722 (put 'YaTeX-lpr-command-history 'no-default t)
723 (defvar YaTeX-lpr-ask-page-range-default t)
724 (defun YaTeX-lpr (arg)
725 "Print out.
726 If prefix arg ARG is non nil, call print driver without
727 page range description."
728 (interactive "P")
729 (or YaTeX-lpr-ask-page-range-default (setq arg (not arg)))
730 (let*((cmd (or (YaTeX-get-builtin "LPR") dviprint-command-format))
731 from to (lbuffer "*dvi-printing*") dir)
732 (setq
733 cmd
734 (YaTeX-replace-format
735 cmd "f"
736 (if (or arg (not (string-match "%f" cmd)))
737 ""
738 (YaTeX-replace-format
739 dviprint-from-format
740 "b"
741 (if (string=
742 (setq from (read-string "From page(default 1): ")) "")
743 "1" from))))
744 )
745 (setq
746 cmd
747 (YaTeX-replace-format
748 cmd "t"
749 (if (or arg (not (string-match "%t" cmd))
750 (string=
751 (setq to (read-string "To page(default none): ")) ""))
752 ""
753 (YaTeX-replace-format dviprint-to-format "e" to)))
754 )
755 (setq
756 cmd
757 (YaTeX-replace-format
758 cmd "p"
759 (cdr (assoc (YaTeX-get-paper-type) YaTeX-dvips-paper-option-alist))))
760 (setq cmd
761 (read-string-with-history
762 "Edit command line: "
763 (format cmd
764 (if (get 'dvi2-command 'region)
765 (substring YaTeX-texput-file
766 0 (rindex YaTeX-texput-file ?.))
767 (YaTeX-get-preview-file-name)))
768 'YaTeX-lpr-command-history))
769 (save-excursion
770 (YaTeX-visit-main t) ;;change execution directory
771 (setq dir default-directory)
772 (YaTeX-showup-buffer
773 lbuffer (function (lambda (x) (nth 3 (window-edges x)))))
774 (set-buffer (get-buffer-create lbuffer))
775 (erase-buffer)
776 (cd dir) ;for 19
777 (cond
778 ((not (fboundp 'start-process))
779 (call-process shell-file-name "con" "*dvi-printing*" nil
780 YaTeX-shell-command-option cmd))
781 (t
782 (set-process-buffer
783 (start-process "print" "*dvi-printing*" shell-file-name
784 YaTeX-shell-command-option cmd)
785 (get-buffer lbuffer))
786 (message "Starting printing command: %s..." cmd))))))
788 (defun YaTeX-main-file-p ()
789 "Return if current buffer is main LaTeX source."
790 (cond
791 (YaTeX-parent-file
792 (eq (get-file-buffer YaTeX-parent-file) (current-buffer)))
793 ((YaTeX-get-builtin "!")
794 (string-match
795 (concat "^" (YaTeX-guess-parent (YaTeX-get-builtin "!")))
796 (buffer-name)))
797 (t
798 (save-excursion
799 (let ((latex-main-id
800 (concat "^\\s *" YaTeX-ec-regexp "document\\(style\\|class\\)")))
801 (or (re-search-backward latex-main-id nil t)
802 (re-search-forward latex-main-id nil t)))))))
804 (defun YaTeX-visit-main (&optional setbuf)
805 "Switch buffer to main LaTeX source.
806 Use set-buffer instead of switch-to-buffer if the optional second argument
807 SETBUF is t(Use it only from Emacs-Lisp program)."
808 (interactive "P")
809 (if (and (interactive-p) setbuf) (setq YaTeX-parent-file nil))
810 (let ((ff (function (lambda (f)
811 (if setbuf (set-buffer (find-file-noselect f))
812 (find-file f)))))
813 b-in main-file YaTeX-create-file-prefix-g
814 (hilit-auto-highlight (not setbuf)))
815 (if (setq b-in (YaTeX-get-builtin "!"))
816 (setq main-file (YaTeX-guess-parent b-in)))
817 (if YaTeX-parent-file
818 (setq main-file ;;(get-file-buffer YaTeX-parent-file)
819 YaTeX-parent-file))
820 (if (YaTeX-main-file-p)
821 (if (interactive-p) (message "I think this is main LaTeX source.") nil)
822 (cond
823 ((and ;;(interactive-p)
824 main-file
825 (cond ((get-file-buffer main-file)
826 (cond
827 (setbuf (set-buffer (get-file-buffer main-file)))
828 ((get-buffer-window (get-file-buffer main-file))
829 (select-window
830 (get-buffer-window (get-file-buffer main-file))))
831 (t (switch-to-buffer (get-file-buffer main-file)))))
832 ((file-exists-p main-file)
833 (funcall ff main-file)))))
834 ;;((and main-file (YaTeX-switch-to-buffer main-file setbuf)))
835 ((and main-file
836 (file-exists-p (setq main-file (concat "../" main-file)))
837 (or b-in
838 (y-or-n-p (concat (expand-file-name main-file)
839 " is main file?:"))))
840 (setq YaTeX-parent-file main-file)
841 ;(YaTeX-switch-to-buffer main-file setbuf)
842 (funcall ff main-file)
843 )
844 (t (setq main-file (read-file-name "Enter your main text: " nil nil 1))
845 (setq YaTeX-parent-file main-file)
846 ; (YaTeX-switch-to-buffer main-file setbuf))
847 (funcall ff main-file))
848 )))
849 nil)
851 (defun YaTeX-guess-parent (command-line)
852 (setq command-line
853 (if (string-match ".*\\s " command-line)
854 (substring command-line (match-end 0))
855 (file-name-nondirectory (buffer-file-name)))
856 command-line
857 (concat (if (string-match "\\(.*\\)\\." command-line)
858 (substring command-line (match-beginning 1) (match-end 1))
859 command-line)
860 ".tex")))
862 (defun YaTeX-visit-main-other-window ()
863 "Switch to buffer main LaTeX source in other window."
864 (interactive)
865 (if (YaTeX-main-file-p) (message "I think this is main LaTeX source.")
866 (YaTeX-switch-to-buffer-other-window
867 (concat (YaTeX-get-preview-file-name) ".tex"))))
869 (defun YaTeX-get-builtin (key)
870 "Read source built-in command of %# usage."
871 (save-excursion
872 (goto-char (point-min))
873 (if (and (re-search-forward
874 (concat "^" (regexp-quote (concat "%#" key))) nil t)
875 (not (eolp)))
876 (YaTeX-buffer-substring
877 (progn (skip-chars-forward " " (point-end-of-line))(point))
878 (point-end-of-line))
879 nil)))
881 (defun YaTeX-save-buffers ()
882 "Save buffers whose major-mode is equal to current major-mode."
883 (basic-save-buffer)
884 (let ((cmm major-mode))
885 (save-excursion
886 (mapcar '(lambda (buf)
887 (set-buffer buf)
888 (if (and (buffer-file-name buf)
889 (eq major-mode cmm)
890 (buffer-modified-p buf)
891 (y-or-n-p (format "Save %s" (buffer-name buf))))
892 (save-buffer buf)))
893 (buffer-list)))))
895 (provide 'yatexprc)