yatex

view yatexprc.el @ 73:f41b01fef5d6

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