yatex

diff yatexprc.el @ 47:d7e7b4654058

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