yatex

diff yatexprc.el @ 51:b0371b6ed799

Created `ChangeLog'. Log hereafter will be written in `ChangeLog'.
author yuuji
date Tue, 20 Dec 1994 21:00:21 +0000
parents eb0512bfcb7f
children 5d94deabb9f9
line diff
     1.1 --- a/yatexprc.el	Fri Nov 25 08:26:13 1994 +0000
     1.2 +++ b/yatexprc.el	Tue Dec 20 21:00:21 1994 +0000
     1.3 @@ -1,709 +1,727 @@
     1.4 -;;; -*- Emacs-Lisp -*-
     1.5 -;;; YaTeX process handler.
     1.6 -;;; yatexprc.el
     1.7 -;;; (c )1993-1994 by HIROSE Yuuji.[yuuji@ae.keio.ac.jp]
     1.8 -;;; Last modified Fri Nov 25 03:31:46 1994 on VFR
     1.9 -;;; $Id$
    1.10 -
    1.11 -(require 'yatex)
    1.12 -
    1.13 -(defvar YaTeX-typeset-process nil
    1.14 -  "Process identifier for jlatex"
    1.15 -)
    1.16 -(defvar YaTeX-typeset-buffer "*YaTeX-typesetting*"
    1.17 -  "Process buffer for jlatex")
    1.18 -
    1.19 -(defvar YaTeX-typeset-buffer-syntax nil
    1.20 -  "*Syntax table for typesetting buffer")
    1.21 -
    1.22 -(defvar YaTeX-current-TeX-buffer nil
    1.23 -  "Keeps the buffer on which recently typeset run.")
    1.24 -
    1.25 -(defvar YaTeX-shell-command-option
    1.26 -  (or (and (boundp 'shell-command-option) shell-command-option)
    1.27 -      (if YaTeX-dos "/c" "-c"))
    1.28 -  "Shell option for command execution.")
    1.29 -
    1.30 -(if YaTeX-typeset-buffer-syntax nil
    1.31 -  (setq YaTeX-typeset-buffer-syntax
    1.32 -	(make-syntax-table (standard-syntax-table)))
    1.33 -  (modify-syntax-entry ?\{ "w" YaTeX-typeset-buffer-syntax)
    1.34 -  (modify-syntax-entry ?\} "w" YaTeX-typeset-buffer-syntax)
    1.35 -  (modify-syntax-entry ?\[ "w" YaTeX-typeset-buffer-syntax)
    1.36 -  (modify-syntax-entry ?\] "w" YaTeX-typeset-buffer-syntax)
    1.37 -)
    1.38 -
    1.39 -(defun YaTeX-typeset (command buffer)
    1.40 -  "Execute jlatex (or other) to LaTeX typeset."
    1.41 -  (interactive)
    1.42 -  (save-excursion
    1.43 -    (let ((p (point)) (window (selected-window)) execdir)
    1.44 -      (if (and YaTeX-typeset-process
    1.45 -	       (eq (process-status YaTeX-typeset-process) 'run))
    1.46 -	  ;; if tex command is halting.
    1.47 -	  (YaTeX-kill-typeset-process YaTeX-typeset-process))
    1.48 -      (YaTeX-visit-main t);;execution directory
    1.49 -      (setq execdir default-directory)
    1.50 -      ;;Select lower-most window if there are more than 2 windows and
    1.51 -      ;;typeset buffer not seen.
    1.52 -      (YaTeX-showup-buffer
    1.53 -       buffer (function (lambda (x) (nth 3 (window-edges x)))))
    1.54 -      (set-buffer (get-buffer-create buffer))
    1.55 -      (setq default-directory execdir)
    1.56 -      (cd execdir)
    1.57 -      (erase-buffer)
    1.58 -      (cond
    1.59 -       (YaTeX-dos			;if MS-DOS
    1.60 -	(YaTeX-put-nonstopmode)
    1.61 -	(call-process
    1.62 -	 shell-file-name nil buffer nil YaTeX-shell-command-option command)
    1.63 -	(YaTeX-remove-nonstopmode))
    1.64 -       (t				;if UNIX
    1.65 -	(set-process-buffer
    1.66 -	 (setq YaTeX-typeset-process
    1.67 -	       (start-process "LaTeX" buffer shell-file-name
    1.68 -			      YaTeX-shell-command-option command))
    1.69 -	 (get-buffer buffer))
    1.70 -	(set-process-sentinel YaTeX-typeset-process 'YaTeX-typeset-sentinel)))
    1.71 -      (message (format "Calling `%s'..." command))
    1.72 -      (setq YaTeX-current-TeX-buffer (buffer-name))
    1.73 -      (use-local-map YaTeX-typesetting-mode-map)
    1.74 -      (set-syntax-table YaTeX-typeset-buffer-syntax)
    1.75 -      (setq mode-name "typeset")
    1.76 -      (if YaTeX-typeset-process		; if process is running (maybe on UNIX)
    1.77 -	  (cond ((boundp 'MULE)
    1.78 -		 (set-current-process-coding-system
    1.79 -		  YaTeX-latex-message-code YaTeX-coding-system))
    1.80 -		((boundp 'NEMACS)
    1.81 -		 (set-kanji-process-code YaTeX-latex-message-code))))
    1.82 -      (if YaTeX-dos (message "Done.")
    1.83 -	(insert " ")
    1.84 -	(set-marker (process-mark YaTeX-typeset-process) (1- (point))))
    1.85 -      (if (bolp) (forward-line -1))	;what for?
    1.86 -      (if YaTeX-emacs-19
    1.87 -	  (let ((win (get-buffer-window buffer t)) owin)
    1.88 -	    (select-frame (window-frame win))
    1.89 -	    (setq owin (selected-window))
    1.90 -	    (select-window win)
    1.91 -	    (goto-char (point-max))
    1.92 -	    (recenter -1)
    1.93 -	    (select-window owin))
    1.94 -	(select-window (get-buffer-window buffer))
    1.95 -	(goto-char (point-max))
    1.96 -	(recenter -1))
    1.97 -      (select-window window)))
    1.98 -)
    1.99 -
   1.100 -(defun YaTeX-typeset-sentinel (proc mes)
   1.101 -  (cond ((null (buffer-name (process-buffer proc)))
   1.102 -         ;; buffer killed
   1.103 -         (set-process-buffer proc nil))
   1.104 -        ((memq (process-status proc) '(signal exit))
   1.105 -         (let* ((obuf (current-buffer)) (pbuf (process-buffer proc))
   1.106 -		(pwin (get-buffer-window pbuf))
   1.107 -		(owin (selected-window)) win)
   1.108 -           ;; save-excursion isn't the right thing if
   1.109 -           ;;  process-buffer is current-buffer
   1.110 -           (unwind-protect
   1.111 -               (progn
   1.112 -                 ;; Write something in *typesetting* and hack its mode line
   1.113 -		 (if pwin
   1.114 -		     (select-window pwin)
   1.115 -		   (set-buffer pbuf))
   1.116 -		 ;;(YaTeX-showup-buffer pbuf nil t)
   1.117 -                 (goto-char (point-max))
   1.118 -		 (if pwin (recenter -3))
   1.119 -                 (insert ?\n "latex typesetting " mes)
   1.120 -                 (forward-char -1)
   1.121 -                 (insert " at " (substring (current-time-string) 0 -5) "\n")
   1.122 -                 (forward-char 1)
   1.123 -                 (setq mode-line-process
   1.124 -                       (concat ": "
   1.125 -                               (symbol-name (process-status proc))))
   1.126 -		 (message "latex typesetting %s."
   1.127 -			  (if (eq (process-status proc) 'exit)
   1.128 -			      "done" "ceased"))
   1.129 -                 ;; If buffer and mode line shows that the process
   1.130 -                 ;; is dead, we can delete it now.  Otherwise it
   1.131 -                 ;; will stay around until M-x list-processes.
   1.132 -                 (delete-process proc)
   1.133 -		 )
   1.134 -             (setq YaTeX-typesetting-process nil)
   1.135 -             ;; Force mode line redisplay soon
   1.136 -             (set-buffer-modified-p (buffer-modified-p))
   1.137 -	     )
   1.138 -	   (select-window owin)
   1.139 -	   (set-buffer obuf))))
   1.140 -)
   1.141 -
   1.142 -(defvar YaTeX-texput-file "texput.tex"
   1.143 -  "*File name for temporary file of typeset-region."
   1.144 -)
   1.145 -
   1.146 -(defun YaTeX-typeset-region ()
   1.147 -  "Paste the region to the file `texput.tex' and execute jlatex (or other)
   1.148 -to LaTeX typeset.  The region is specified by the rule:
   1.149 -	(1)If keyword `%#BEGIN' is found in the upper direction from (point).
   1.150 -	  (1-1)if the keyword `%#END' is found after `%#BEGIN',
   1.151 -		->Assume the text between `%#BEGIN' and `%#END' as region.
   1.152 -	  (1-2)if the keyword `%#END' is not found anywhere after `%#BEGIN',
   1.153 -		->Assume the text after `%#BEGIN' as region.
   1.154 -	(2)If no `%#BEGIN' usage is found before the (point),
   1.155 -		->Assume the text between current (point) and (mark) as region.
   1.156 -DON'T forget to eliminate the `%#BEGIN/%#END' notation after editing
   1.157 -operation to the region."
   1.158 -  (interactive)
   1.159 -  (save-excursion
   1.160 -    (let*
   1.161 -	((end "") typeout ;Type out message that tells the method of cutting.
   1.162 -	 (cmd (concat (YaTeX-get-latex-command nil) " " YaTeX-texput-file))
   1.163 -	 (buffer (current-buffer)) opoint preamble (subpreamble "") main
   1.164 -	 (hilit-auto-highlight nil)	;for Emacs19 with hilit19
   1.165 -	 reg-begin reg-end)
   1.166 -
   1.167 -      (save-excursion
   1.168 -	(if (search-backward "%#BEGIN" nil t)
   1.169 -	    (progn
   1.170 -	      (setq typeout "--- Region from BEGIN to "
   1.171 -		    end "the end of the buffer ---"
   1.172 -		    reg-begin (match-end 0))
   1.173 -	      (if (search-forward "%#END" nil t)
   1.174 -		  (setq reg-end (match-beginning 0)
   1.175 -			end "END ---")
   1.176 -		(setq reg-end (point-max))))
   1.177 -	  (setq typeout "=== Region from (point) to (mark) ===")
   1.178 -	  (setq reg-begin (point) reg-end (mark)))
   1.179 -	(goto-char (point-min))
   1.180 -	(while (search-forward "%#REQUIRE" nil t)
   1.181 -	  (setq subpreamble
   1.182 -		(concat subpreamble
   1.183 -			(cond
   1.184 -			 ((eolp)
   1.185 -			  (buffer-substring
   1.186 -			   (match-beginning 0)
   1.187 -			   (point-beginning-of-line)))
   1.188 -			 (t (buffer-substring
   1.189 -			     (match-end 0)
   1.190 -			     (point-end-of-line))))
   1.191 -			"\n"))
   1.192 -	  (goto-char (match-end 0))))
   1.193 -      (YaTeX-visit-main t)
   1.194 -      (setq main (current-buffer))
   1.195 -      (setq opoint (point))
   1.196 -      (goto-char (point-min))
   1.197 -      (setq
   1.198 -       preamble
   1.199 -       (if (re-search-forward "^[ 	]*\\\\begin.*{document}" nil t)
   1.200 -	   (buffer-substring (point-min) (match-end 0))
   1.201 -	 (concat "\\documentstyle{" YaTeX-default-document-style "}\n"
   1.202 -		 "\\begin{document}")))
   1.203 -      (goto-char opoint)
   1.204 -      ;;(set-buffer buffer)		;for clarity
   1.205 -      (set-buffer (find-file-noselect YaTeX-texput-file))
   1.206 -      ;;(find-file YaTeX-texput-file)
   1.207 -      (erase-buffer)
   1.208 -      (if YaTeX-need-nonstop
   1.209 -	  (insert "\\nonstopmode{}\n"))
   1.210 -      (insert preamble "\n" subpreamble "\n")
   1.211 -      (insert-buffer-substring buffer reg-begin reg-end)
   1.212 -      (insert "\\typeout{" typeout end "}\n") ;Notice the selected method.
   1.213 -      (insert "\n\\end{document}\n")
   1.214 -      (basic-save-buffer)
   1.215 -      (kill-buffer (current-buffer))
   1.216 -      (set-buffer main)		;return to parent file or itself.
   1.217 -      (YaTeX-typeset cmd YaTeX-typeset-buffer)
   1.218 -      (switch-to-buffer buffer)		;for Emacs-19
   1.219 -      (put 'dvi2-command 'region t)))
   1.220 -)
   1.221 -
   1.222 -(defun YaTeX-typeset-buffer ()
   1.223 -  "Typeset whole buffer.  If %#! usage says other buffer is main text,
   1.224 -visit main buffer to confirm if its includeonly list contains current
   1.225 -buffer's file.  And if it doesn't contain editing text, ask user which
   1.226 -action wants to be done, A:Add list, R:Replace list, %:comment-out list."
   1.227 -  (interactive)
   1.228 -  (YaTeX-save-buffers)
   1.229 -  (let*((me (substring (buffer-name) 0 (rindex (buffer-name) ?.)))
   1.230 -	(mydir (file-name-directory (buffer-file-name)))
   1.231 -	(cmd (YaTeX-get-latex-command t)))
   1.232 -    (if (YaTeX-main-file-p) nil
   1.233 -      (save-excursion
   1.234 -	(YaTeX-visit-main t)	;search into main buffer
   1.235 -	(save-excursion
   1.236 -	  (push-mark (point) t)
   1.237 -	  (goto-char (point-min))
   1.238 -	  (if (and (re-search-forward "^[ 	]*\\\\begin{document}" nil t)
   1.239 -		   (re-search-backward "^[ 	]*\\\\includeonly{" nil t))
   1.240 -	      (let*
   1.241 -		  ((b (progn (skip-chars-forward "^{") (point)))
   1.242 -		   (e (progn (skip-chars-forward "^}") (1+ (point))))
   1.243 -		   (s (buffer-substring b e)) c
   1.244 -		   (pardir (file-name-directory (buffer-file-name))))
   1.245 -		(if (string-match (concat "[{,/]" me "[,}]") s)
   1.246 -		    nil ; Nothing to do when it's already in includeonly.
   1.247 -		  (ding)
   1.248 -		  (switch-to-buffer (current-buffer));Display this buffer.
   1.249 -		  (setq
   1.250 -		   me	  ;;Rewrite my name(me) to contain sub directory name.
   1.251 -		   (concat
   1.252 -		    (if (string-match pardir mydir) ;if mydir is child of main
   1.253 -			(substring mydir (length pardir)) ;cut absolute path
   1.254 -		      mydir) ;else concat absolute path name.
   1.255 -		    me))
   1.256 -		  (message
   1.257 -		   "`%s' is not in \\includeonly. A)dd R)eplace %%)comment? "
   1.258 -		   me)
   1.259 -		  (setq c (read-char))
   1.260 -		  (cond
   1.261 -		   ((= c ?a)
   1.262 -		    (goto-char (1+ b))
   1.263 -		    (insert me (if (string= s "{}") "" ",")))
   1.264 -		   ((= c ?r)
   1.265 -		    (delete-region (1+ b) (1- e)) (insert me))
   1.266 -		   ((= c ?%)
   1.267 -		    (beginning-of-line) (insert "%"))
   1.268 -		   (t nil))
   1.269 -		  (basic-save-buffer))))
   1.270 -	  (exchange-point-and-mark))
   1.271 -	))
   1.272 -    (YaTeX-typeset cmd YaTeX-typeset-buffer)
   1.273 -    (put 'dvi2-command 'region nil))
   1.274 -)
   1.275 -
   1.276 -(defvar YaTeX-call-command-history nil
   1.277 -  "Holds history list of YaTeX-call-command-on-file.")
   1.278 -(put 'YaTeX-call-command-history 'no-default t)
   1.279 -(defun YaTeX-call-command-on-file (base-cmd buffer)
   1.280 -  (YaTeX-save-buffers)
   1.281 -  (YaTeX-typeset
   1.282 -   (let ((minibufer-history-symbol 'YaTeX-call-command-history))
   1.283 -     (read-string "Call command: "
   1.284 -		  (concat base-cmd " " (YaTeX-get-preview-file-name))))
   1.285 -   buffer)
   1.286 -)
   1.287 -
   1.288 -(defun YaTeX-bibtex-buffer (cmd)
   1.289 -  "Pass the bibliography data of editing file to bibtex."
   1.290 -  (interactive)
   1.291 -  (YaTeX-save-buffers)
   1.292 -  (YaTeX-call-command-on-file cmd "*YaTeX-bibtex*" )
   1.293 -)
   1.294 -
   1.295 -(defun YaTeX-kill-typeset-process (proc)
   1.296 -  "Kill process PROC after sending signal to PROC.
   1.297 -PROC should be process identifier."
   1.298 -  (cond
   1.299 -   (YaTeX-dos
   1.300 -    (error "MS-DOS can't have concurrent process."))
   1.301 -   ((or (null proc) (not (eq (process-status proc) 'run)))
   1.302 -    (error "No typesetting process."))
   1.303 -   (t (interrupt-process proc)
   1.304 -      (delete-process proc)))
   1.305 -)
   1.306 -
   1.307 -(defun YaTeX-system (command buffer)
   1.308 -  "Execute some command on buffer.  Not a official function."
   1.309 -  (save-excursion
   1.310 -    (YaTeX-showup-buffer
   1.311 -     buffer (function (lambda (x) (nth 3 (window-edges x)))))
   1.312 -    (set-buffer (get-buffer-create buffer))
   1.313 -    (erase-buffer)
   1.314 -    (if YaTeX-dos
   1.315 -	(call-process
   1.316 -	 shell-file-name nil buffer nil YaTeX-shell-command-option command)
   1.317 -      (set-process-buffer
   1.318 -       (start-process
   1.319 -	"system" buffer shell-file-name YaTeX-shell-command-option command)
   1.320 -       (get-buffer buffer))))
   1.321 -)
   1.322 -
   1.323 -(defvar YaTeX-preview-command-history nil
   1.324 -  "Holds minibuffer history of preview command.")
   1.325 -(put 'YaTeX-preview-command-history 'no-default t)
   1.326 -(defvar YaTeX-preview-file-history nil
   1.327 -  "Holds minibuffer history of file to preview.")
   1.328 -(put 'YaTeX-preview-file-history 'no-default t)
   1.329 -(defun YaTeX-preview (preview-command preview-file)
   1.330 -  "Execute xdvi (or other) to tex-preview."
   1.331 -  (interactive
   1.332 -   (list
   1.333 -    (let ((minibuffer-history-symbol 'YaTeX-preview-command-history))
   1.334 -      (read-string "Preview command: " dvi2-command))
   1.335 -    (let ((minibuffer-history-symbol 'YaTeX-preview-file-history))
   1.336 -      (read-string "Preview file[.dvi]: "
   1.337 -		   (if (get 'dvi2-command 'region)
   1.338 -		       (substring YaTeX-texput-file
   1.339 -				  0 (rindex YaTeX-texput-file ?.))
   1.340 -		     (YaTeX-get-preview-file-name))
   1.341 -		   ))))
   1.342 -  (setq dvi2-command preview-command)	;`dvi2command' is buffer local
   1.343 -  (save-excursion
   1.344 -    (YaTeX-visit-main t)
   1.345 -    (let ((pbuffer "*dvi-preview*") (dir default-directory))
   1.346 -      (YaTeX-showup-buffer
   1.347 -       pbuffer (function (lambda (x) (nth 3 (window-edges x)))))
   1.348 -      (set-buffer (get-buffer-create pbuffer))
   1.349 -      (erase-buffer)
   1.350 -      (setq default-directory dir)	;for 18
   1.351 -      (cd dir)				;for 19
   1.352 -      (cond
   1.353 -       (YaTeX-dos			;if MS-DOS
   1.354 -	(send-string-to-terminal "\e[2J\e[>5h") ;CLS & hide cursor
   1.355 -	(call-process shell-file-name "con" "*dvi-preview*" nil
   1.356 -		      YaTeX-shell-command-option preview-command preview-file)
   1.357 -	(send-string-to-terminal "\e[>5l") ;show cursor
   1.358 -	(redraw-display))
   1.359 -       (t				;if UNIX
   1.360 -	(set-process-buffer
   1.361 -	 (start-process "preview" "*dvi-preview*" shell-file-name
   1.362 -			YaTeX-shell-command-option
   1.363 -			(concat preview-command " " preview-file))
   1.364 -	 (get-buffer pbuffer))
   1.365 -	(message
   1.366 -	 (concat "Starting " preview-command
   1.367 -		 " to preview " preview-file))))))
   1.368 -)
   1.369 -
   1.370 -(defun YaTeX-prev-error ()
   1.371 -  "Visit previous typeset error.
   1.372 -  To avoid making confliction of line numbers by editing, jump to
   1.373 -error or warning lines in reverse order."
   1.374 -  (interactive)
   1.375 -  (let ((cur-buf (buffer-name)) (cur-win (selected-window))
   1.376 -	error-line typeset-win error-buffer error-win)
   1.377 -    (if (null (get-buffer YaTeX-typeset-buffer))
   1.378 -	(error "There is no typesetting buffer."))
   1.379 -    (YaTeX-showup-buffer YaTeX-typeset-buffer nil t)
   1.380 -    (setq typeset-win (selected-window))
   1.381 -    (if (re-search-backward
   1.382 -	 (concat "\\(" latex-error-regexp "\\)\\|\\("
   1.383 -		 latex-warning-regexp "\\)")
   1.384 -	 nil t)
   1.385 -	nil
   1.386 -      (select-window cur-win)
   1.387 -      (error "No more erros on %s" cur-buf))
   1.388 -    (goto-char (match-beginning 0))
   1.389 -    (skip-chars-forward "^0-9" (match-end 0))
   1.390 -    (setq error-line
   1.391 -	  (string-to-int
   1.392 -	   (buffer-substring
   1.393 -	    (point)
   1.394 -	    (progn (skip-chars-forward "0-9" (match-end 0)) (point))))
   1.395 -	  error-buffer (YaTeX-get-error-file cur-buf)
   1.396 -	  error-win (get-buffer-window error-buffer))
   1.397 -    (if (or (null error-line) (equal 0 error-line))
   1.398 -	(error "Can't detect error position."))
   1.399 -    (select-window cur-win)
   1.400 -    (cond
   1.401 -     (error-win (select-window error-win))
   1.402 -     ((eq (get-lru-window) typeset-win)
   1.403 -      (YaTeX-switch-to-buffer error-buffer))
   1.404 -     (t (select-window (get-lru-window))
   1.405 -	(YaTeX-switch-to-buffer error-buffer)))
   1.406 -    (setq error-win (selected-window))
   1.407 -    (goto-line error-line)
   1.408 -    (message "LaTeX %s in `%s' on line: %d."
   1.409 -	     (if (match-beginning 1) "error" "warning")
   1.410 -	     error-buffer error-line)
   1.411 -    (select-window typeset-win)
   1.412 -    (skip-chars-backward "0-9")
   1.413 -    (recenter (/ (window-height) 2))
   1.414 -    (sit-for 3)
   1.415 -    (goto-char (match-beginning 0))
   1.416 -    (select-window error-win))
   1.417 -)
   1.418 -
   1.419 -(defun YaTeX-jump-error-line ()
   1.420 -  "Jump to corresponding line on latex command's error message."
   1.421 -  (interactive)
   1.422 -  (let (error-line error-file error-buf)
   1.423 -    (save-excursion
   1.424 -      (beginning-of-line)
   1.425 -      (setq error-line (re-search-forward "l[ ines]*\\.?\\([1-9][0-9]*\\)"
   1.426 -					  (point-end-of-line) t)))
   1.427 -    (if (null error-line)
   1.428 -	(if (eobp) (insert (this-command-keys))
   1.429 -	  (error "No line number expression."))
   1.430 -      (goto-char (match-beginning 0))
   1.431 -      (setq error-line (string-to-int
   1.432 -			(buffer-substring (match-beginning 1) (match-end 1)))
   1.433 -	    error-file (YaTeX-get-error-file YaTeX-current-TeX-buffer)
   1.434 -	    error-buf (YaTeX-switch-to-buffer error-file t))
   1.435 -      (if (null error-buf)
   1.436 -	  (error "`%s' is not found in this directory." error-file))
   1.437 -      (YaTeX-showup-buffer error-buf nil t)
   1.438 -      (goto-line error-line)))
   1.439 -)
   1.440 -
   1.441 -(defun YaTeX-send-string ()
   1.442 -  "Send string to current typeset process."
   1.443 -  (interactive)
   1.444 -  (if (and (eq (process-status YaTeX-typeset-process) 'run)
   1.445 -	   (>= (point) (process-mark YaTeX-typeset-process)))
   1.446 -      (let ((b (process-mark YaTeX-typeset-process))
   1.447 -	    (e (point-end-of-line)))
   1.448 -	(goto-char b)
   1.449 -	(skip-chars-forward " \t" e)
   1.450 -	(setq b (point))
   1.451 -	(process-send-string
   1.452 -	 YaTeX-typeset-process (concat (buffer-substring b e) "\n"))
   1.453 -	(goto-char e)
   1.454 -	(insert "\n")
   1.455 -	(set-marker (process-mark YaTeX-typeset-process) (point))
   1.456 -	(insert " "))
   1.457 -    (ding))
   1.458 -)
   1.459 -
   1.460 -(defun YaTeX-view-error ()
   1.461 -  (interactive)
   1.462 -  (if (null (get-buffer YaTeX-typeset-buffer))
   1.463 -      (message "No typeset buffer found.")
   1.464 -    (let ((win (selected-window)))
   1.465 -      (YaTeX-showup-buffer YaTeX-typeset-buffer nil t)
   1.466 -      ;; Next 3 lines are obsolete because YaTeX-typesetting-buffer is
   1.467 -      ;; automatically scrolled up at typesetting.
   1.468 -      ;;(goto-char (point-max))
   1.469 -      ;;(forward-line -1)
   1.470 -      ;;(recenter -1)
   1.471 -      (select-window win)))
   1.472 -)
   1.473 -
   1.474 -(defun YaTeX-get-error-file (default)
   1.475 -  "Get current processing file from typesetting log."
   1.476 -  (save-excursion
   1.477 -    (let(s)
   1.478 -      (condition-case () (up-list -1)
   1.479 -	(error
   1.480 -	 (let ((list 0) found)
   1.481 -	   (while
   1.482 -	       (and (<= list 0) (not found)
   1.483 -		    (re-search-backward "\\((\\)\\|\\()\\)" nil t))
   1.484 -	     (if (equal (match-beginning 0) (match-beginning 2)) ;close paren.
   1.485 -		 (setq list (1- list)) ;open paren
   1.486 -	       (setq list (1+ list))
   1.487 -	       (if (= list 1)
   1.488 -		   (if (looking-at "\\([^,{}%]+\.\\)tex\\|sty")
   1.489 -		       (setq found t)
   1.490 -		     (setq list (1- list)))))))))
   1.491 -      (setq s
   1.492 -	    (buffer-substring
   1.493 -	     (progn (forward-char 1) (point))
   1.494 -	     (progn (skip-chars-forward "-A-Za-z0-9_/\.\\" (point-end-of-line))
   1.495 -		    (point))))
   1.496 -      (if (string= "" s) default s)))
   1.497 -)
   1.498 -      
   1.499 -(defun YaTeX-put-nonstopmode ()
   1.500 -  (if YaTeX-need-nonstop
   1.501 -      (if (re-search-backward "\\\\nonstopmode{}" (point-min) t)
   1.502 -	  nil                    ;if already written in text then do nothing
   1.503 -	(save-excursion
   1.504 -	  (YaTeX-visit-main t)
   1.505 -	  (goto-char (point-min))
   1.506 -	  (insert "\\nonstopmode{}%_YaTeX_%\n")))
   1.507 -    )
   1.508 -)
   1.509 -
   1.510 -(defun YaTeX-remove-nonstopmode ()
   1.511 -  (if YaTeX-need-nonstop ;for speed
   1.512 -      (save-excursion
   1.513 -	(YaTeX-visit-main t)
   1.514 -	(goto-char (point-min))
   1.515 -	(forward-line 1)
   1.516 -	(narrow-to-region (point-min) (point))
   1.517 -	(goto-char (point-min))
   1.518 -	(delete-matching-lines "^\\\\nonstopmode\\{\\}%_YaTeX_%$")
   1.519 -	(widen)))
   1.520 -)
   1.521 -
   1.522 -(defun YaTeX-get-preview-file-name ()
   1.523 -  "Get file name to preview by inquiring YaTeX-get-latex-command"
   1.524 -  (let* ((latex-cmd (YaTeX-get-latex-command t))
   1.525 -	 (rin (rindex latex-cmd ? ))
   1.526 -	 (fname (if (> rin -1) (substring latex-cmd (1+ rin)) ""))
   1.527 -	 (period))
   1.528 -    (if (string= fname "")
   1.529 -	(setq fname (substring (file-name-nondirectory
   1.530 -				(buffer-file-name))
   1.531 -			       0 -4))
   1.532 -      (setq period (rindex fname ?.))
   1.533 -      (setq fname (substring fname 0 (if (eq -1 period) nil period)))
   1.534 -      ))
   1.535 -)
   1.536 -
   1.537 -(defun YaTeX-get-latex-command (&optional switch)
   1.538 -  "Specify the latex-command name and its argument.
   1.539 -If there is a line which begins with string: \"%#!\", the following
   1.540 -strings are assumed to be the latex-command and arguments.  The
   1.541 -default value of latex-command is:
   1.542 -	tex-command FileName
   1.543 -and if you write \"%#!jlatex\" in the beginning of certain line.
   1.544 -	\"jlatex \" FileName
   1.545 -will be the latex-command,
   1.546 -and you write \"%#!jlatex main.tex\" on some line and argument SWITCH
   1.547 -is non-nil, then
   1.548 -	\"jlatex main.tex\"
   1.549 -
   1.550 -will be given to the shell."
   1.551 -  (let (magic command target)
   1.552 -    (setq parent
   1.553 -	  (cond
   1.554 -	   (YaTeX-parent-file YaTeX-parent-file)
   1.555 -	   (t (save-excursion
   1.556 -		(YaTeX-visit-main t)
   1.557 -		(file-name-nondirectory (buffer-file-name)))))
   1.558 -	  magic (YaTeX-get-builtin "!"))
   1.559 -    (cond
   1.560 -     (magic
   1.561 -      (cond
   1.562 -       (switch (if (string-match "\\s " magic) magic
   1.563 -		 (concat magic " " parent)))
   1.564 -       (t (concat (substring magic 0 (string-match "\\s " magic)) " "))))
   1.565 -     (t (concat tex-command " " (if switch parent)))))
   1.566 -)
   1.567 -
   1.568 -(defvar YaTeX-lpr-command-history nil
   1.569 -  "Holds command line history of YaTeX-lpr.")
   1.570 -(put 'YaTeX-lpr-command-history 'no-default t)
   1.571 -(defun YaTeX-lpr (arg)
   1.572 -  "Print out.  If prefix arg ARG is non nil, call print driver without
   1.573 -page range description."
   1.574 -  (interactive "P")
   1.575 -  (let*((cmd (or (YaTeX-get-builtin "LPR") dviprint-command-format))
   1.576 -	from to (lbuffer "*dvi-printing*"))
   1.577 -    (setq
   1.578 -     cmd 
   1.579 -     (YaTeX-replace-format
   1.580 -      cmd "f"
   1.581 -      (if (or arg (not (string-match "%f" cmd)))
   1.582 -	      ""
   1.583 -	    (YaTeX-replace-format
   1.584 -	     dviprint-from-format
   1.585 -	     "b"
   1.586 -	     (if (string=
   1.587 -		  (setq from (read-string "From page(default 1): ")) "")
   1.588 -		 "1" from))))
   1.589 -       )
   1.590 -    (setq
   1.591 -     cmd
   1.592 -     (YaTeX-replace-format
   1.593 -      cmd "t"
   1.594 -      (if (or arg (not (string-match "%t" cmd))
   1.595 -	      (string= 
   1.596 -	       (setq to (read-string "To page(default none): ")) ""))
   1.597 -	  ""
   1.598 -	(YaTeX-replace-format dviprint-to-format "e" to)))
   1.599 -     )
   1.600 -    (setq cmd
   1.601 -	  (let ((minibuffer-history-symbol 'YaTeX-lpr-command-history))
   1.602 -	    (read-string "Edit command line: "
   1.603 -			 (format cmd (YaTeX-get-preview-file-name)))))
   1.604 -    (save-excursion
   1.605 -      (YaTeX-visit-main t) ;;change execution directory
   1.606 -      (YaTeX-showup-buffer
   1.607 -       lbuffer (function (lambda (x) (nth 3 (window-edges x)))))
   1.608 -      (set-buffer (get-buffer-create lbuffer))
   1.609 -      (erase-buffer)
   1.610 -      (cond
   1.611 -       (YaTeX-dos
   1.612 -	(call-process shell-file-name "con" "*dvi-printing*" nil
   1.613 -		      YaTeX-shell-command-option cmd))
   1.614 -       (t
   1.615 -	(set-process-buffer
   1.616 -	 (start-process "print" "*dvi-printing*" shell-file-name
   1.617 -			YaTeX-shell-command-option cmd)
   1.618 -	 (get-buffer lbuffer))
   1.619 -	(message "Starting printing command: %s..." cmd)))
   1.620 -      ))
   1.621 -)
   1.622 -
   1.623 -(defun YaTeX-main-file-p ()
   1.624 -  "Return if current buffer is main LaTeX source."
   1.625 -  (cond
   1.626 -   ((YaTeX-get-builtin "!")
   1.627 -    (string-match (YaTeX-guess-parent (YaTeX-get-builtin "!")) (buffer-name)))
   1.628 -   (t
   1.629 -    (save-excursion
   1.630 -      (let ((latex-main-id (concat "^\\s *" YaTeX-ec-regexp "documentstyle")))
   1.631 -	(or (re-search-backward latex-main-id nil t)
   1.632 -	    (re-search-forward latex-main-id nil t))))))
   1.633 -)
   1.634 -
   1.635 -(defun YaTeX-visit-main (&optional setbuf)
   1.636 -  "Switch buffer to main LaTeX source.
   1.637 -Use set-buffer instead of switch-to-buffer if the optional second argument
   1.638 -SETBUF is t(Use it only from Emacs-Lisp program)."
   1.639 -  (interactive)
   1.640 -  (let (b-in main-file)
   1.641 -    (if (setq b-in (YaTeX-get-builtin "!"))
   1.642 -	(setq main-file (YaTeX-guess-parent b-in)))
   1.643 -    (if YaTeX-parent-file
   1.644 -	(setq main-file ;;(get-file-buffer YaTeX-parent-file)
   1.645 -	      YaTeX-parent-file))
   1.646 -    (if (YaTeX-main-file-p)
   1.647 -	(if (interactive-p) (message "I think this is main LaTeX source.") nil)
   1.648 -      (cond
   1.649 -       ((and (interactive-p) main-file (get-buffer-window main-file))
   1.650 -	(select-window (get-buffer-window main-file)))
   1.651 -       ((and main-file (YaTeX-switch-to-buffer main-file setbuf)))
   1.652 -       ((and main-file
   1.653 -	     (file-exists-p (setq main-file (concat "../" main-file)))
   1.654 -	     (y-or-n-p (concat (expand-file-name main-file)
   1.655 -			       " is main file?:")))
   1.656 -	(YaTeX-switch-to-buffer main-file setbuf))
   1.657 -       (t (setq main-file (read-file-name "Enter your main text: " nil nil 1))
   1.658 -	  (setq YaTeX-parent-file main-file)
   1.659 -	  (find-file main-file))
   1.660 -       )))
   1.661 -  nil
   1.662 -)
   1.663 -
   1.664 -
   1.665 -(defun YaTeX-guess-parent (command-line)
   1.666 -  (setq command-line
   1.667 -	(if (string-match ".*\\s " command-line)
   1.668 -	    (substring command-line (match-end 0))
   1.669 -	  (file-name-nondirectory (buffer-file-name)))
   1.670 -	command-line
   1.671 -	(concat (if (string-match "\\(.*\\)\\." command-line)
   1.672 -		    (substring command-line (match-beginning 1) (match-end 1))
   1.673 -		  command-line)
   1.674 -		".tex"))
   1.675 -)
   1.676 -
   1.677 -(defun YaTeX-visit-main-other-window ()
   1.678 -  "Switch to buffer main LaTeX source in other window."
   1.679 -  (interactive)
   1.680 -  (if (YaTeX-main-file-p) (message "I think this is main LaTeX source.")
   1.681 -      (YaTeX-switch-to-buffer-other-window
   1.682 -       (concat (YaTeX-get-preview-file-name) ".tex")))
   1.683 -)
   1.684 -
   1.685 -(defun YaTeX-get-builtin (key)
   1.686 -  "Read source built-in command of %# usage."
   1.687 -  (save-excursion
   1.688 -    (goto-char (point-min))
   1.689 -    (if (and (re-search-forward
   1.690 -	      (concat "^" (regexp-quote (concat "%#" key))) nil t)
   1.691 -	     (not (eolp)))
   1.692 -	(buffer-substring
   1.693 -	 (progn (skip-chars-forward " 	" (point-end-of-line))(point))
   1.694 -	 (point-end-of-line))
   1.695 -      nil))
   1.696 -)
   1.697 -
   1.698 -(defun YaTeX-save-buffers ()
   1.699 -  "Save buffers which is in yatex-mode."
   1.700 -  (basic-save-buffer)
   1.701 -  (save-excursion
   1.702 -    (mapcar '(lambda (buf)
   1.703 -	       (set-buffer buf)
   1.704 -	       (if (and (buffer-file-name buf)
   1.705 -			(eq major-mode 'yatex-mode)
   1.706 -			(buffer-modified-p buf)
   1.707 -			(y-or-n-p (format "Save %s" (buffer-name buf))))
   1.708 -		   (save-buffer buf)))
   1.709 -	    (buffer-list)))
   1.710 -)
   1.711 -
   1.712 -(provide 'yatexprc)
   1.713 +;;; -*- Emacs-Lisp -*-
   1.714 +;;; YaTeX process handler.
   1.715 +;;; yatexprc.el
   1.716 +;;; (c )1993-1994 by HIROSE Yuuji.[yuuji@ae.keio.ac.jp]
   1.717 +;;; Last modified Wed Dec 14 16:58:15 1994 on 98fa
   1.718 +;;; $Id$
   1.719 +
   1.720 +(require 'yatex)
   1.721 +
   1.722 +(defvar YaTeX-typeset-process nil
   1.723 +  "Process identifier for jlatex"
   1.724 +)
   1.725 +(defvar YaTeX-typeset-buffer "*YaTeX-typesetting*"
   1.726 +  "Process buffer for jlatex")
   1.727 +
   1.728 +(defvar YaTeX-typeset-buffer-syntax nil
   1.729 +  "*Syntax table for typesetting buffer")
   1.730 +
   1.731 +(defvar YaTeX-current-TeX-buffer nil
   1.732 +  "Keeps the buffer on which recently typeset run.")
   1.733 +
   1.734 +(defvar YaTeX-shell-command-option
   1.735 +  (or (and (boundp 'shell-command-option) shell-command-option)
   1.736 +      (if YaTeX-dos "/c" "-c"))
   1.737 +  "Shell option for command execution.")
   1.738 +
   1.739 +(if YaTeX-typeset-buffer-syntax nil
   1.740 +  (setq YaTeX-typeset-buffer-syntax
   1.741 +	(make-syntax-table (standard-syntax-table)))
   1.742 +  (modify-syntax-entry ?\{ "w" YaTeX-typeset-buffer-syntax)
   1.743 +  (modify-syntax-entry ?\} "w" YaTeX-typeset-buffer-syntax)
   1.744 +  (modify-syntax-entry ?\[ "w" YaTeX-typeset-buffer-syntax)
   1.745 +  (modify-syntax-entry ?\] "w" YaTeX-typeset-buffer-syntax)
   1.746 +)
   1.747 +
   1.748 +(defun YaTeX-typeset (command buffer)
   1.749 +  "Execute jlatex (or other) to LaTeX typeset."
   1.750 +  (interactive)
   1.751 +  (save-excursion
   1.752 +    (let ((p (point)) (window (selected-window)) execdir (cb (current-buffer)))
   1.753 +      (if (and YaTeX-typeset-process
   1.754 +	       (eq (process-status YaTeX-typeset-process) 'run))
   1.755 +	  ;; if tex command is halting.
   1.756 +	  (YaTeX-kill-typeset-process YaTeX-typeset-process))
   1.757 +      (YaTeX-visit-main t) ;;execution directory
   1.758 +      (setq execdir default-directory)
   1.759 +      ;;Select lower-most window if there are more than 2 windows and
   1.760 +      ;;typeset buffer not seen.
   1.761 +      (YaTeX-showup-buffer
   1.762 +       buffer (function (lambda (x) (nth 3 (window-edges x)))))
   1.763 +      (set-buffer (get-buffer-create buffer))
   1.764 +      (setq default-directory execdir)
   1.765 +      (cd execdir)
   1.766 +      (erase-buffer)
   1.767 +      (cond
   1.768 +       (YaTeX-dos			;if MS-DOS
   1.769 +	(YaTeX-put-nonstopmode)
   1.770 +	(call-process
   1.771 +	 shell-file-name nil buffer nil YaTeX-shell-command-option command)
   1.772 +	(YaTeX-remove-nonstopmode))
   1.773 +       (t				;if UNIX
   1.774 +	(set-process-buffer
   1.775 +	 (setq YaTeX-typeset-process
   1.776 +	       (start-process "LaTeX" buffer shell-file-name
   1.777 +			      YaTeX-shell-command-option command))
   1.778 +	 (get-buffer buffer))
   1.779 +	(set-process-sentinel YaTeX-typeset-process 'YaTeX-typeset-sentinel)))
   1.780 +      (message (format "Calling `%s'..." command))
   1.781 +      (setq YaTeX-current-TeX-buffer (buffer-name))
   1.782 +      (use-local-map YaTeX-typesetting-mode-map)
   1.783 +      (set-syntax-table YaTeX-typeset-buffer-syntax)
   1.784 +      (setq mode-name "typeset")
   1.785 +      (if YaTeX-typeset-process		; if process is running (maybe on UNIX)
   1.786 +	  (cond ((boundp 'MULE)
   1.787 +		 (set-current-process-coding-system
   1.788 +		  YaTeX-latex-message-code YaTeX-coding-system))
   1.789 +		((boundp 'NEMACS)
   1.790 +		 (set-kanji-process-code YaTeX-latex-message-code))))
   1.791 +      (if YaTeX-dos (message "Done.")
   1.792 +	(insert " ")
   1.793 +	(set-marker (process-mark YaTeX-typeset-process) (1- (point))))
   1.794 +      (if (bolp) (forward-line -1))	;what for?
   1.795 +      (if (and YaTeX-emacs-19 window-system)
   1.796 +	  (let ((win (get-buffer-window buffer t)) owin)
   1.797 +	    (select-frame (window-frame win))
   1.798 +	    (setq owin (selected-window))
   1.799 +	    (select-window win)
   1.800 +	    (goto-char (point-max))
   1.801 +	    (recenter -1)
   1.802 +	    (select-window owin))
   1.803 +	(select-window (get-buffer-window buffer))
   1.804 +	(goto-char (point-max))
   1.805 +	(recenter -1))
   1.806 +      (select-window window)
   1.807 +      (switch-to-buffer cb)))
   1.808 +)
   1.809 +
   1.810 +(defun YaTeX-typeset-sentinel (proc mes)
   1.811 +  (cond ((null (buffer-name (process-buffer proc)))
   1.812 +         ;; buffer killed
   1.813 +         (set-process-buffer proc nil))
   1.814 +        ((memq (process-status proc) '(signal exit))
   1.815 +         (let* ((obuf (current-buffer)) (pbuf (process-buffer proc))
   1.816 +		(pwin (get-buffer-window pbuf))
   1.817 +		(owin (selected-window)) win)
   1.818 +           ;; save-excursion isn't the right thing if
   1.819 +           ;;  process-buffer is current-buffer
   1.820 +           (unwind-protect
   1.821 +               (progn
   1.822 +                 ;; Write something in *typesetting* and hack its mode line
   1.823 +		 (if pwin
   1.824 +		     (select-window pwin)
   1.825 +		   (set-buffer pbuf))
   1.826 +		 ;;(YaTeX-showup-buffer pbuf nil t)
   1.827 +                 (goto-char (point-max))
   1.828 +		 (if pwin (recenter -3))
   1.829 +                 (insert ?\n "latex typesetting " mes)
   1.830 +                 (forward-char -1)
   1.831 +                 (insert " at " (substring (current-time-string) 0 -5) "\n")
   1.832 +                 (forward-char 1)
   1.833 +                 (setq mode-line-process
   1.834 +                       (concat ": "
   1.835 +                               (symbol-name (process-status proc))))
   1.836 +		 (message "latex typesetting %s."
   1.837 +			  (if (eq (process-status proc) 'exit)
   1.838 +			      "done" "ceased"))
   1.839 +                 ;; If buffer and mode line shows that the process
   1.840 +                 ;; is dead, we can delete it now.  Otherwise it
   1.841 +                 ;; will stay around until M-x list-processes.
   1.842 +                 (delete-process proc)
   1.843 +		 )
   1.844 +             (setq YaTeX-typeset-process nil)
   1.845 +             ;; Force mode line redisplay soon
   1.846 +             (set-buffer-modified-p (buffer-modified-p))
   1.847 +	     )
   1.848 +	   (select-window owin)
   1.849 +	   (set-buffer obuf))))
   1.850 +)
   1.851 +
   1.852 +(defvar YaTeX-texput-file "texput.tex"
   1.853 +  "*File name for temporary file of typeset-region."
   1.854 +)
   1.855 +
   1.856 +(defun YaTeX-typeset-region ()
   1.857 +  "Paste the region to the file `texput.tex' and execute jlatex (or other)
   1.858 +to LaTeX typeset.  The region is specified by the rule:
   1.859 +	(1)If keyword `%#BEGIN' is found in the upper direction from (point).
   1.860 +	  (1-1)if the keyword `%#END' is found after `%#BEGIN',
   1.861 +		->Assume the text between `%#BEGIN' and `%#END' as region.
   1.862 +	  (1-2)if the keyword `%#END' is not found anywhere after `%#BEGIN',
   1.863 +		->Assume the text after `%#BEGIN' as region.
   1.864 +	(2)If no `%#BEGIN' usage is found before the (point),
   1.865 +		->Assume the text between current (point) and (mark) as region.
   1.866 +DON'T forget to eliminate the `%#BEGIN/%#END' notation after editing
   1.867 +operation to the region."
   1.868 +  (interactive)
   1.869 +  (save-excursion
   1.870 +    (let*
   1.871 +	((end "") typeout ;Type out message that tells the method of cutting.
   1.872 +	 (texput YaTeX-texput-file)
   1.873 +	 (cmd (concat (YaTeX-get-latex-command nil) " " texput))
   1.874 +	 (buffer (current-buffer)) opoint preamble (subpreamble "") main
   1.875 +	 (hilit-auto-highlight nil)	;for Emacs19 with hilit19
   1.876 +	 reg-begin reg-end)
   1.877 +
   1.878 +      (save-excursion
   1.879 +	(if (search-backward "%#BEGIN" nil t)
   1.880 +	    (progn
   1.881 +	      (setq typeout "--- Region from BEGIN to "
   1.882 +		    end "the end of the buffer ---"
   1.883 +		    reg-begin (match-end 0))
   1.884 +	      (if (search-forward "%#END" nil t)
   1.885 +		  (setq reg-end (match-beginning 0)
   1.886 +			end "END ---")
   1.887 +		(setq reg-end (point-max))))
   1.888 +	  (setq typeout "=== Region from (point) to (mark) ===")
   1.889 +	  (setq reg-begin (point) reg-end (mark)))
   1.890 +	(goto-char (point-min))
   1.891 +	(while (search-forward "%#REQUIRE" nil t)
   1.892 +	  (setq subpreamble
   1.893 +		(concat subpreamble
   1.894 +			(cond
   1.895 +			 ((eolp)
   1.896 +			  (buffer-substring
   1.897 +			   (match-beginning 0)
   1.898 +			   (point-beginning-of-line)))
   1.899 +			 (t (buffer-substring
   1.900 +			     (match-end 0)
   1.901 +			     (point-end-of-line))))
   1.902 +			"\n"))
   1.903 +	  (goto-char (match-end 0))))
   1.904 +      (YaTeX-visit-main t)
   1.905 +      (setq main (current-buffer))
   1.906 +      (setq opoint (point))
   1.907 +      (goto-char (point-min))
   1.908 +      (setq
   1.909 +       preamble
   1.910 +       (if (re-search-forward "^[ 	]*\\\\begin.*{document}" nil t)
   1.911 +	   (buffer-substring (point-min) (match-end 0))
   1.912 +	 (concat "\\documentstyle{" YaTeX-default-document-style "}\n"
   1.913 +		 "\\begin{document}")))
   1.914 +      (goto-char opoint)
   1.915 +      ;;(set-buffer buffer)		;for clarity
   1.916 +      (set-buffer (find-file-noselect texput))
   1.917 +      ;;(find-file YaTeX-texput-file)
   1.918 +      (erase-buffer)
   1.919 +      (if YaTeX-need-nonstop
   1.920 +	  (insert "\\nonstopmode{}\n"))
   1.921 +      (insert preamble "\n" subpreamble "\n")
   1.922 +      (insert-buffer-substring buffer reg-begin reg-end)
   1.923 +      (insert "\\typeout{" typeout end "}\n") ;Notice the selected method.
   1.924 +      (insert "\n\\end{document}\n")
   1.925 +      (basic-save-buffer)
   1.926 +      (kill-buffer (current-buffer))
   1.927 +      (set-buffer main)		;return to parent file or itself.
   1.928 +      (YaTeX-typeset cmd YaTeX-typeset-buffer)
   1.929 +      (switch-to-buffer buffer)		;for Emacs-19
   1.930 +      (put 'dvi2-command 'region t)))
   1.931 +)
   1.932 +
   1.933 +(defun YaTeX-typeset-buffer ()
   1.934 +  "Typeset whole buffer.  If %#! usage says other buffer is main text,
   1.935 +visit main buffer to confirm if its includeonly list contains current
   1.936 +buffer's file.  And if it doesn't contain editing text, ask user which
   1.937 +action wants to be done, A:Add list, R:Replace list, %:comment-out list."
   1.938 +  (interactive)
   1.939 +  (YaTeX-save-buffers)
   1.940 +  (let*((me (substring (buffer-name) 0 (rindex (buffer-name) ?.)))
   1.941 +	(mydir (file-name-directory (buffer-file-name)))
   1.942 +	(cmd (YaTeX-get-latex-command t)) (cb (current-buffer)))
   1.943 +    (if (YaTeX-main-file-p) nil
   1.944 +      (save-excursion
   1.945 +	(YaTeX-visit-main t)	;search into main buffer
   1.946 +	(save-excursion
   1.947 +	  (push-mark (point) t)
   1.948 +	  (goto-char (point-min))
   1.949 +	  (if (and (re-search-forward "^[ 	]*\\\\begin{document}" nil t)
   1.950 +		   (re-search-backward "^[ 	]*\\\\includeonly{" nil t))
   1.951 +	      (let*
   1.952 +		  ((b (progn (skip-chars-forward "^{") (point)))
   1.953 +		   (e (progn (skip-chars-forward "^}") (1+ (point))))
   1.954 +		   (s (buffer-substring b e)) c
   1.955 +		   (pardir (file-name-directory (buffer-file-name))))
   1.956 +		(if (string-match (concat "[{,/]" me "[,}]") s)
   1.957 +		    nil ; Nothing to do when it's already in includeonly.
   1.958 +		  (ding)
   1.959 +		  (switch-to-buffer (current-buffer));Display this buffer.
   1.960 +		  (setq
   1.961 +		   me	  ;;Rewrite my name(me) to contain sub directory name.
   1.962 +		   (concat
   1.963 +		    (if (string-match pardir mydir) ;if mydir is child of main
   1.964 +			(substring mydir (length pardir)) ;cut absolute path
   1.965 +		      mydir) ;else concat absolute path name.
   1.966 +		    me))
   1.967 +		  (message
   1.968 +		   "`%s' is not in \\includeonly. A)dd R)eplace %%)comment? "
   1.969 +		   me)
   1.970 +		  (setq c (read-char))
   1.971 +		  (cond
   1.972 +		   ((= c ?a)
   1.973 +		    (goto-char (1+ b))
   1.974 +		    (insert me (if (string= s "{}") "" ",")))
   1.975 +		   ((= c ?r)
   1.976 +		    (delete-region (1+ b) (1- e)) (insert me))
   1.977 +		   ((= c ?%)
   1.978 +		    (beginning-of-line) (insert "%"))
   1.979 +		   (t nil))
   1.980 +		  (basic-save-buffer))))
   1.981 +	  (exchange-point-and-mark)))
   1.982 +      (switch-to-buffer cb))		;for 19
   1.983 +    (YaTeX-typeset cmd YaTeX-typeset-buffer)
   1.984 +    (put 'dvi2-command 'region nil))
   1.985 +)
   1.986 +
   1.987 +(defvar YaTeX-call-command-history nil
   1.988 +  "Holds history list of YaTeX-call-command-on-file.")
   1.989 +(put 'YaTeX-call-command-history 'no-default t)
   1.990 +(defun YaTeX-call-command-on-file (base-cmd buffer)
   1.991 +  (YaTeX-save-buffers)
   1.992 +  (YaTeX-typeset
   1.993 +   (read-string-with-history
   1.994 +    "Call command: "
   1.995 +    (concat base-cmd " " (YaTeX-get-preview-file-name))
   1.996 +    'YaTeX-call-command-history)
   1.997 +   buffer)
   1.998 +)
   1.999 +
  1.1000 +(defun YaTeX-bibtex-buffer (cmd)
  1.1001 +  "Pass the bibliography data of editing file to bibtex."
  1.1002 +  (interactive)
  1.1003 +  (YaTeX-save-buffers)
  1.1004 +  (YaTeX-call-command-on-file cmd "*YaTeX-bibtex*" )
  1.1005 +)
  1.1006 +
  1.1007 +(defun YaTeX-kill-typeset-process (proc)
  1.1008 +  "Kill process PROC after sending signal to PROC.
  1.1009 +PROC should be process identifier."
  1.1010 +  (cond
  1.1011 +   (YaTeX-dos
  1.1012 +    (error "MS-DOS can't have concurrent process."))
  1.1013 +   ((or (null proc) (not (eq (process-status proc) 'run)))
  1.1014 +    (message "Typesetting process is not running."))
  1.1015 +   (t
  1.1016 +    (save-excursion
  1.1017 +      (set-buffer (process-buffer proc))
  1.1018 +      (save-excursion
  1.1019 +	(goto-char (point-max))
  1.1020 +	(beginning-of-line)
  1.1021 +	(if (looking-at "\\? +$")
  1.1022 +	    (let ((mp (point-max)))
  1.1023 +	      (process-send-string proc "x\n")
  1.1024 +	      (while (= mp (point-max)) (sit-for 1))))))
  1.1025 +    (if (eq (process-status proc) 'run)
  1.1026 +	(progn
  1.1027 +	  (interrupt-process proc)
  1.1028 +	  (delete-process proc)))))
  1.1029 +)
  1.1030 +
  1.1031 +(defun YaTeX-system (command buffer)
  1.1032 +  "Execute some command on buffer.  Not a official function."
  1.1033 +  (save-excursion
  1.1034 +    (YaTeX-showup-buffer
  1.1035 +     buffer (function (lambda (x) (nth 3 (window-edges x)))))
  1.1036 +    (set-buffer (get-buffer-create buffer))
  1.1037 +    (erase-buffer)
  1.1038 +    (if YaTeX-dos
  1.1039 +	(call-process
  1.1040 +	 shell-file-name nil buffer nil YaTeX-shell-command-option command)
  1.1041 +      (set-process-buffer
  1.1042 +       (start-process
  1.1043 +	"system" buffer shell-file-name YaTeX-shell-command-option command)
  1.1044 +       (get-buffer buffer))))
  1.1045 +)
  1.1046 +
  1.1047 +(defvar YaTeX-preview-command-history nil
  1.1048 +  "Holds minibuffer history of preview command.")
  1.1049 +(put 'YaTeX-preview-command-history 'no-default t)
  1.1050 +(defvar YaTeX-preview-file-history nil
  1.1051 +  "Holds minibuffer history of file to preview.")
  1.1052 +(put 'YaTeX-preview-file-history 'no-default t)
  1.1053 +(defun YaTeX-preview (preview-command preview-file)
  1.1054 +  "Execute xdvi (or other) to tex-preview."
  1.1055 +  (interactive
  1.1056 +   (list
  1.1057 +    (read-string-with-history
  1.1058 +     "Preview command: " dvi2-command 'YaTeX-preview-command-history)
  1.1059 +    (read-string-with-history
  1.1060 +     "Preview file[.dvi]: "
  1.1061 +     (if (get 'dvi2-command 'region)
  1.1062 +	 (substring YaTeX-texput-file
  1.1063 +		    0 (rindex YaTeX-texput-file ?.))
  1.1064 +       (YaTeX-get-preview-file-name))
  1.1065 +     'YaTeX-preview-file-history)))
  1.1066 +  (setq dvi2-command preview-command)	;`dvi2command' is buffer local
  1.1067 +  (save-excursion
  1.1068 +    (YaTeX-visit-main t)
  1.1069 +    (let ((pbuffer "*dvi-preview*") (dir default-directory))
  1.1070 +      (YaTeX-showup-buffer
  1.1071 +       pbuffer (function (lambda (x) (nth 3 (window-edges x)))))
  1.1072 +      (set-buffer (get-buffer-create pbuffer))
  1.1073 +      (erase-buffer)
  1.1074 +      (setq default-directory dir)	;for 18
  1.1075 +      (cd dir)				;for 19
  1.1076 +      (cond
  1.1077 +       (YaTeX-dos			;if MS-DOS
  1.1078 +	(send-string-to-terminal "\e[2J\e[>5h") ;CLS & hide cursor
  1.1079 +	(call-process shell-file-name "con" "*dvi-preview*" nil
  1.1080 +		      YaTeX-shell-command-option preview-command preview-file)
  1.1081 +	(send-string-to-terminal "\e[>5l") ;show cursor
  1.1082 +	(redraw-display))
  1.1083 +       (t				;if UNIX
  1.1084 +	(set-process-buffer
  1.1085 +	 (start-process "preview" "*dvi-preview*" shell-file-name
  1.1086 +			YaTeX-shell-command-option
  1.1087 +			(concat preview-command " " preview-file))
  1.1088 +	 (get-buffer pbuffer))
  1.1089 +	(message
  1.1090 +	 (concat "Starting " preview-command
  1.1091 +		 " to preview " preview-file))))))
  1.1092 +)
  1.1093 +
  1.1094 +(defun YaTeX-prev-error ()
  1.1095 +  "Visit previous typeset error.
  1.1096 +  To avoid making confliction of line numbers by editing, jump to
  1.1097 +error or warning lines in reverse order."
  1.1098 +  (interactive)
  1.1099 +  (let ((cur-buf (buffer-name)) (cur-win (selected-window))
  1.1100 +	error-line typeset-win error-buffer error-win)
  1.1101 +    (if (null (get-buffer YaTeX-typeset-buffer))
  1.1102 +	(error "There is no typesetting buffer."))
  1.1103 +    (YaTeX-showup-buffer YaTeX-typeset-buffer nil t)
  1.1104 +    (setq typeset-win (selected-window))
  1.1105 +    (if (re-search-backward
  1.1106 +	 (concat "\\(" latex-error-regexp "\\)\\|\\("
  1.1107 +		 latex-warning-regexp "\\)")
  1.1108 +	 nil t)
  1.1109 +	nil
  1.1110 +      (select-window cur-win)
  1.1111 +      (error "No more erros on %s" cur-buf))
  1.1112 +    (goto-char (match-beginning 0))
  1.1113 +    (skip-chars-forward "^0-9" (match-end 0))
  1.1114 +    (setq error-line
  1.1115 +	  (string-to-int
  1.1116 +	   (buffer-substring
  1.1117 +	    (point)
  1.1118 +	    (progn (skip-chars-forward "0-9" (match-end 0)) (point))))
  1.1119 +	  error-buffer (YaTeX-get-error-file cur-buf)
  1.1120 +	  error-win (get-buffer-window error-buffer))
  1.1121 +    (if (or (null error-line) (equal 0 error-line))
  1.1122 +	(error "Can't detect error position."))
  1.1123 +    (select-window cur-win)
  1.1124 +    (cond
  1.1125 +     (error-win (select-window error-win))
  1.1126 +     ((eq (get-lru-window) typeset-win)
  1.1127 +      (YaTeX-switch-to-buffer error-buffer))
  1.1128 +     (t (select-window (get-lru-window))
  1.1129 +	(YaTeX-switch-to-buffer error-buffer)))
  1.1130 +    (setq error-win (selected-window))
  1.1131 +    (goto-line error-line)
  1.1132 +    (message "LaTeX %s in `%s' on line: %d."
  1.1133 +	     (if (match-beginning 1) "error" "warning")
  1.1134 +	     error-buffer error-line)
  1.1135 +    (select-window typeset-win)
  1.1136 +    (skip-chars-backward "0-9")
  1.1137 +    (recenter (/ (window-height) 2))
  1.1138 +    (sit-for 3)
  1.1139 +    (goto-char (match-beginning 0))
  1.1140 +    (select-window error-win))
  1.1141 +)
  1.1142 +
  1.1143 +(defun YaTeX-jump-error-line ()
  1.1144 +  "Jump to corresponding line on latex command's error message."
  1.1145 +  (interactive)
  1.1146 +  (let (error-line error-file error-buf)
  1.1147 +    (save-excursion
  1.1148 +      (beginning-of-line)
  1.1149 +      (setq error-line (re-search-forward "l[ ines]*\\.?\\([1-9][0-9]*\\)"
  1.1150 +					  (point-end-of-line) t)))
  1.1151 +    (if (null error-line)
  1.1152 +	(if (eobp) (insert (this-command-keys))
  1.1153 +	  (error "No line number expression."))
  1.1154 +      (goto-char (match-beginning 0))
  1.1155 +      (setq error-line (string-to-int
  1.1156 +			(buffer-substring (match-beginning 1) (match-end 1)))
  1.1157 +	    error-file (YaTeX-get-error-file YaTeX-current-TeX-buffer)
  1.1158 +	    error-buf (YaTeX-switch-to-buffer error-file t))
  1.1159 +      (if (null error-buf)
  1.1160 +	  (error "`%s' is not found in this directory." error-file))
  1.1161 +      (YaTeX-showup-buffer error-buf nil t)
  1.1162 +      (goto-line error-line)))
  1.1163 +)
  1.1164 +
  1.1165 +(defun YaTeX-send-string ()
  1.1166 +  "Send string to current typeset process."
  1.1167 +  (interactive)
  1.1168 +  (if (and (eq (process-status YaTeX-typeset-process) 'run)
  1.1169 +	   (>= (point) (process-mark YaTeX-typeset-process)))
  1.1170 +      (let ((b (process-mark YaTeX-typeset-process))
  1.1171 +	    (e (point-end-of-line)))
  1.1172 +	(goto-char b)
  1.1173 +	(skip-chars-forward " \t" e)
  1.1174 +	(setq b (point))
  1.1175 +	(process-send-string
  1.1176 +	 YaTeX-typeset-process (concat (buffer-substring b e) "\n"))
  1.1177 +	(goto-char e)
  1.1178 +	(insert "\n")
  1.1179 +	(set-marker (process-mark YaTeX-typeset-process) (point))
  1.1180 +	(insert " "))
  1.1181 +    (ding))
  1.1182 +)
  1.1183 +
  1.1184 +(defun YaTeX-view-error ()
  1.1185 +  (interactive)
  1.1186 +  (if (null (get-buffer YaTeX-typeset-buffer))
  1.1187 +      (message "No typeset buffer found.")
  1.1188 +    (let ((win (selected-window)))
  1.1189 +      (YaTeX-showup-buffer YaTeX-typeset-buffer nil t)
  1.1190 +      ;; Next 3 lines are obsolete because YaTeX-typesetting-buffer is
  1.1191 +      ;; automatically scrolled up at typesetting.
  1.1192 +      ;;(goto-char (point-max))
  1.1193 +      ;;(forward-line -1)
  1.1194 +      ;;(recenter -1)
  1.1195 +      (select-window win)))
  1.1196 +)
  1.1197 +
  1.1198 +(defun YaTeX-get-error-file (default)
  1.1199 +  "Get current processing file from typesetting log."
  1.1200 +  (save-excursion
  1.1201 +    (let(s)
  1.1202 +      (condition-case () (up-list -1)
  1.1203 +	(error
  1.1204 +	 (let ((list 0) found)
  1.1205 +	   (while
  1.1206 +	       (and (<= list 0) (not found)
  1.1207 +		    (re-search-backward "\\((\\)\\|\\()\\)" nil t))
  1.1208 +	     (if (equal (match-beginning 0) (match-beginning 2)) ;close paren.
  1.1209 +		 (setq list (1- list)) ;open paren
  1.1210 +	       (setq list (1+ list))
  1.1211 +	       (if (= list 1)
  1.1212 +		   (if (looking-at "\\([^,{}%]+\.\\)tex\\|sty")
  1.1213 +		       (setq found t)
  1.1214 +		     (setq list (1- list)))))))))
  1.1215 +      (setq s
  1.1216 +	    (buffer-substring
  1.1217 +	     (progn (forward-char 1) (point))
  1.1218 +	     (progn (skip-chars-forward "^ \n" (point-end-of-line))
  1.1219 +		    (point))))
  1.1220 +      (if (string= "" s) default s)))
  1.1221 +)
  1.1222 +      
  1.1223 +(defun YaTeX-put-nonstopmode ()
  1.1224 +  (if YaTeX-need-nonstop
  1.1225 +      (if (re-search-backward "\\\\nonstopmode{}" (point-min) t)
  1.1226 +	  nil                    ;if already written in text then do nothing
  1.1227 +	(save-excursion
  1.1228 +	  (YaTeX-visit-main t)
  1.1229 +	  (goto-char (point-min))
  1.1230 +	  (insert "\\nonstopmode{}%_YaTeX_%\n")))
  1.1231 +    )
  1.1232 +)
  1.1233 +
  1.1234 +(defun YaTeX-remove-nonstopmode ()
  1.1235 +  (if YaTeX-need-nonstop ;for speed
  1.1236 +      (save-excursion
  1.1237 +	(YaTeX-visit-main t)
  1.1238 +	(goto-char (point-min))
  1.1239 +	(forward-line 1)
  1.1240 +	(narrow-to-region (point-min) (point))
  1.1241 +	(goto-char (point-min))
  1.1242 +	(delete-matching-lines "^\\\\nonstopmode\\{\\}%_YaTeX_%$")
  1.1243 +	(widen)))
  1.1244 +)
  1.1245 +
  1.1246 +(defun YaTeX-get-preview-file-name ()
  1.1247 +  "Get file name to preview by inquiring YaTeX-get-latex-command"
  1.1248 +  (let* ((latex-cmd (YaTeX-get-latex-command t))
  1.1249 +	 (rin (rindex latex-cmd ? ))
  1.1250 +	 (fname (if (> rin -1) (substring latex-cmd (1+ rin)) ""))
  1.1251 +	 (period))
  1.1252 +    (if (string= fname "")
  1.1253 +	(setq fname (substring (file-name-nondirectory
  1.1254 +				(buffer-file-name))
  1.1255 +			       0 -4))
  1.1256 +      (setq period (rindex fname ?.))
  1.1257 +      (setq fname (substring fname 0 (if (eq -1 period) nil period)))
  1.1258 +      ))
  1.1259 +)
  1.1260 +
  1.1261 +(defun YaTeX-get-latex-command (&optional switch)
  1.1262 +  "Specify the latex-command name and its argument.
  1.1263 +If there is a line which begins with string: \"%#!\", the following
  1.1264 +strings are assumed to be the latex-command and arguments.  The
  1.1265 +default value of latex-command is:
  1.1266 +	tex-command FileName
  1.1267 +and if you write \"%#!jlatex\" in the beginning of certain line.
  1.1268 +	\"jlatex \" FileName
  1.1269 +will be the latex-command,
  1.1270 +and you write \"%#!jlatex main.tex\" on some line and argument SWITCH
  1.1271 +is non-nil, then
  1.1272 +	\"jlatex main.tex\"
  1.1273 +
  1.1274 +will be given to the shell."
  1.1275 +  (let (magic command target)
  1.1276 +    (setq parent
  1.1277 +	  (cond
  1.1278 +	   (YaTeX-parent-file YaTeX-parent-file)
  1.1279 +	   (t (save-excursion
  1.1280 +		(YaTeX-visit-main t)
  1.1281 +		(file-name-nondirectory (buffer-file-name)))))
  1.1282 +	  magic (YaTeX-get-builtin "!"))
  1.1283 +    (cond
  1.1284 +     (magic
  1.1285 +      (cond
  1.1286 +       (switch (if (string-match "\\s " magic) magic
  1.1287 +		 (concat magic " " parent)))
  1.1288 +       (t (concat (substring magic 0 (string-match "\\s " magic)) " "))))
  1.1289 +     (t (concat tex-command " " (if switch parent)))))
  1.1290 +)
  1.1291 +
  1.1292 +(defvar YaTeX-lpr-command-history nil
  1.1293 +  "Holds command line history of YaTeX-lpr.")
  1.1294 +(put 'YaTeX-lpr-command-history 'no-default t)
  1.1295 +(defun YaTeX-lpr (arg)
  1.1296 +  "Print out.  If prefix arg ARG is non nil, call print driver without
  1.1297 +page range description."
  1.1298 +  (interactive "P")
  1.1299 +  (let*((cmd (or (YaTeX-get-builtin "LPR") dviprint-command-format))
  1.1300 +	from to (lbuffer "*dvi-printing*"))
  1.1301 +    (setq
  1.1302 +     cmd 
  1.1303 +     (YaTeX-replace-format
  1.1304 +      cmd "f"
  1.1305 +      (if (or arg (not (string-match "%f" cmd)))
  1.1306 +	      ""
  1.1307 +	    (YaTeX-replace-format
  1.1308 +	     dviprint-from-format
  1.1309 +	     "b"
  1.1310 +	     (if (string=
  1.1311 +		  (setq from (read-string "From page(default 1): ")) "")
  1.1312 +		 "1" from))))
  1.1313 +       )
  1.1314 +    (setq
  1.1315 +     cmd
  1.1316 +     (YaTeX-replace-format
  1.1317 +      cmd "t"
  1.1318 +      (if (or arg (not (string-match "%t" cmd))
  1.1319 +	      (string= 
  1.1320 +	       (setq to (read-string "To page(default none): ")) ""))
  1.1321 +	  ""
  1.1322 +	(YaTeX-replace-format dviprint-to-format "e" to)))
  1.1323 +     )
  1.1324 +    (setq cmd
  1.1325 +	  (read-string-with-history
  1.1326 +	   "Edit command line: "
  1.1327 +	   (format cmd (YaTeX-get-preview-file-name))
  1.1328 +	   'YaTeX-lpr-command-history))
  1.1329 +    (save-excursion
  1.1330 +      (YaTeX-visit-main t) ;;change execution directory
  1.1331 +      (YaTeX-showup-buffer
  1.1332 +       lbuffer (function (lambda (x) (nth 3 (window-edges x)))))
  1.1333 +      (set-buffer (get-buffer-create lbuffer))
  1.1334 +      (erase-buffer)
  1.1335 +      (cond
  1.1336 +       (YaTeX-dos
  1.1337 +	(call-process shell-file-name "con" "*dvi-printing*" nil
  1.1338 +		      YaTeX-shell-command-option cmd))
  1.1339 +       (t
  1.1340 +	(set-process-buffer
  1.1341 +	 (start-process "print" "*dvi-printing*" shell-file-name
  1.1342 +			YaTeX-shell-command-option cmd)
  1.1343 +	 (get-buffer lbuffer))
  1.1344 +	(message "Starting printing command: %s..." cmd)))
  1.1345 +      ))
  1.1346 +)
  1.1347 +
  1.1348 +(defun YaTeX-main-file-p ()
  1.1349 +  "Return if current buffer is main LaTeX source."
  1.1350 +  (cond
  1.1351 +   ((YaTeX-get-builtin "!")
  1.1352 +    (string-match (YaTeX-guess-parent (YaTeX-get-builtin "!")) (buffer-name)))
  1.1353 +   (t
  1.1354 +    (save-excursion
  1.1355 +      (let ((latex-main-id (concat "^\\s *" YaTeX-ec-regexp "documentstyle")))
  1.1356 +	(or (re-search-backward latex-main-id nil t)
  1.1357 +	    (re-search-forward latex-main-id nil t))))))
  1.1358 +)
  1.1359 +
  1.1360 +(defun YaTeX-visit-main (&optional setbuf)
  1.1361 +  "Switch buffer to main LaTeX source.
  1.1362 +Use set-buffer instead of switch-to-buffer if the optional second argument
  1.1363 +SETBUF is t(Use it only from Emacs-Lisp program)."
  1.1364 +  (interactive "P")
  1.1365 +  (if (and (interactive-p) setbuf) (setq YaTeX-parent-file nil))
  1.1366 +  (let (b-in main-file)
  1.1367 +    (if (setq b-in (YaTeX-get-builtin "!"))
  1.1368 +	(setq main-file (YaTeX-guess-parent b-in)))
  1.1369 +    (if YaTeX-parent-file
  1.1370 +	(setq main-file ;;(get-file-buffer YaTeX-parent-file)
  1.1371 +	      YaTeX-parent-file))
  1.1372 +    (if (YaTeX-main-file-p)
  1.1373 +	(if (interactive-p) (message "I think this is main LaTeX source.") nil)
  1.1374 +      (cond
  1.1375 +       ((and (interactive-p) main-file (get-file-buffer main-file))
  1.1376 +	(goto-buffer-window main-file))
  1.1377 +       ((and main-file (YaTeX-switch-to-buffer main-file setbuf)))
  1.1378 +       ((and main-file
  1.1379 +	     (file-exists-p (setq main-file (concat "../" main-file)))
  1.1380 +	     (y-or-n-p (concat (expand-file-name main-file)
  1.1381 +			       " is main file?:")))
  1.1382 +	(setq YaTeX-parent-file main-file)
  1.1383 +	(YaTeX-switch-to-buffer main-file setbuf))
  1.1384 +       (t (setq main-file (read-file-name "Enter your main text: " nil nil 1))
  1.1385 +	  (setq YaTeX-parent-file main-file)
  1.1386 +	  (YaTeX-switch-to-buffer main-file setbuf))
  1.1387 +       )))
  1.1388 +  nil
  1.1389 +)
  1.1390 +
  1.1391 +
  1.1392 +(defun YaTeX-guess-parent (command-line)
  1.1393 +  (setq command-line
  1.1394 +	(if (string-match ".*\\s " command-line)
  1.1395 +	    (substring command-line (match-end 0))
  1.1396 +	  (file-name-nondirectory (buffer-file-name)))
  1.1397 +	command-line
  1.1398 +	(concat (if (string-match "\\(.*\\)\\." command-line)
  1.1399 +		    (substring command-line (match-beginning 1) (match-end 1))
  1.1400 +		  command-line)
  1.1401 +		".tex"))
  1.1402 +)
  1.1403 +
  1.1404 +(defun YaTeX-visit-main-other-window ()
  1.1405 +  "Switch to buffer main LaTeX source in other window."
  1.1406 +  (interactive)
  1.1407 +  (if (YaTeX-main-file-p) (message "I think this is main LaTeX source.")
  1.1408 +      (YaTeX-switch-to-buffer-other-window
  1.1409 +       (concat (YaTeX-get-preview-file-name) ".tex")))
  1.1410 +)
  1.1411 +
  1.1412 +(defun YaTeX-get-builtin (key)
  1.1413 +  "Read source built-in command of %# usage."
  1.1414 +  (save-excursion
  1.1415 +    (goto-char (point-min))
  1.1416 +    (if (and (re-search-forward
  1.1417 +	      (concat "^" (regexp-quote (concat "%#" key))) nil t)
  1.1418 +	     (not (eolp)))
  1.1419 +	(buffer-substring
  1.1420 +	 (progn (skip-chars-forward " 	" (point-end-of-line))(point))
  1.1421 +	 (point-end-of-line))
  1.1422 +      nil))
  1.1423 +)
  1.1424 +
  1.1425 +(defun YaTeX-save-buffers ()
  1.1426 +  "Save buffers which is in yatex-mode."
  1.1427 +  (basic-save-buffer)
  1.1428 +  (save-excursion
  1.1429 +    (mapcar '(lambda (buf)
  1.1430 +	       (set-buffer buf)
  1.1431 +	       (if (and (buffer-file-name buf)
  1.1432 +			(eq major-mode 'yatex-mode)
  1.1433 +			(buffer-modified-p buf)
  1.1434 +			(y-or-n-p (format "Save %s" (buffer-name buf))))
  1.1435 +		   (save-buffer buf)))
  1.1436 +	    (buffer-list)))
  1.1437 +)
  1.1438 +
  1.1439 +(provide 'yatexprc)