yatex

diff yatexprc.el @ 7:9a56acb6c287

Fill-paragraph and (un)comment-paragraph work fine. Fix kill range of YaTeX-kill-some-pairs. Ignore begin/end in verb or verbatim. Indent rigidly initial space between begin/end pairs. Add yatex-mode-load-hook. Go to corresponding \label or \ref.
author yuuji
date Tue, 04 May 1993 12:57:27 +0000
parents
children 390df0e505da
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/yatexprc.el	Tue May 04 12:57:27 1993 +0000
     1.3 @@ -0,0 +1,594 @@
     1.4 +;;; -*- Emacs-Lisp -*-
     1.5 +;;; YaTeX process handler.
     1.6 +;;; yatexprc.el rev.1.42
     1.7 +;;; (c)1993 by HIROSE Yuuji.[yuuji@ae.keio.ac.jp]
     1.8 +;;; Last modified Tue May  4 21:49:59 1993 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 +(defun YaTeX-typeset (command buffer)
    1.20 +  "Execute jlatex (or other) to LaTeX typeset."
    1.21 +  (interactive)
    1.22 +  (if (and YaTeX-typeset-process
    1.23 +	   (eq (process-status YaTeX-typeset-process) 'run))
    1.24 +      ;; if tex command is halting.
    1.25 +      (YaTeX-kill-typeset-process YaTeX-typeset-process))
    1.26 +  (YaTeX-visit-main t)  ;;execution directory
    1.27 +  (with-output-to-temp-buffer buffer
    1.28 +    (if (eq system-type 'ms-dos)			;if MS-DOS
    1.29 +	(progn
    1.30 +	  (message (concat "Typesetting " (buffer-name) "..."))
    1.31 +	  (YaTeX-put-nonstopmode)
    1.32 +	  (call-process shell-file-name
    1.33 +			nil buffer nil "/c" command)
    1.34 +	  (YaTeX-remove-nonstopmode))
    1.35 +      (setq YaTeX-typeset-process			;if UNIX
    1.36 +	      (start-process "LaTeX" buffer shell-file-name "-c"
    1.37 +			     command))
    1.38 +      (set-process-sentinel YaTeX-typeset-process 'YaTeX-typeset-sentinel)))
    1.39 +  (setq current-TeX-buffer (buffer-name))
    1.40 +  (other-window 1)
    1.41 +  (use-local-map YaTeX-typesetting-mode-map)
    1.42 +  (setq mode-name "typeset")
    1.43 +  (if YaTeX-typeset-process ; if process is running (maybe on UNIX)
    1.44 +      (cond ((boundp 'MULE)
    1.45 +	     (set-current-process-coding-system
    1.46 +	      YaTeX-latex-message-code YaTeX-coding-system))
    1.47 +	    ((boundp 'NEMACS)
    1.48 +	     (set-kanji-process-code YaTeX-latex-message-code))))
    1.49 +  (message "Type SPC to continue.")
    1.50 +  (goto-char (point-max))
    1.51 +  (if (eq system-type 'ms-dos) (message "Done.")
    1.52 +    (while (bobp) (message "Invoking process. wait...") (sleep-for 1))
    1.53 +    (insert (message " ")))
    1.54 +  (if (bolp) (forward-line -1))
    1.55 +  (recenter -1)
    1.56 +  (other-window -1)
    1.57 +)
    1.58 +
    1.59 +(defun YaTeX-typeset-sentinel (proc mes)
    1.60 +  (cond ((null (buffer-name (process-buffer proc)))
    1.61 +         ;; buffer killed
    1.62 +         (set-process-buffer proc nil))
    1.63 +        ((memq (process-status proc) '(signal exit))
    1.64 +         (let* ((obuf (current-buffer)))
    1.65 +           ;; save-excursion isn't the right thing if
    1.66 +           ;;  process-buffer is current-buffer
    1.67 +           (unwind-protect
    1.68 +               (progn
    1.69 +                 ;; Write something in *typesetting* and hack its mode line
    1.70 +		 (if (equal (current-buffer) (process-buffer proc))
    1.71 +		     nil
    1.72 +		   (other-window 1)
    1.73 +		   (switch-to-buffer (process-buffer proc))
    1.74 +		   (goto-char (point-max))
    1.75 +		   (recenter -3)
    1.76 +		   (other-window -1))
    1.77 +		 (set-buffer (process-buffer proc))
    1.78 +                 (goto-char (point-max))
    1.79 +                 (insert ?\n "latex typesetting " mes)
    1.80 +                 (forward-char -1)
    1.81 +                 (insert " at " (substring (current-time-string) 0 -5) "\n")
    1.82 +                 (forward-char 1)
    1.83 +                 (setq mode-line-process
    1.84 +                       (concat ": "
    1.85 +                               (symbol-name (process-status proc))))
    1.86 +		 (message "latex typesetting done.")
    1.87 +                 ;; If buffer and mode line will show that the process
    1.88 +                 ;; is dead, we can delete it now.  Otherwise it
    1.89 +                 ;; will stay around until M-x list-processes.
    1.90 +                 (delete-process proc)
    1.91 +		 )
    1.92 +             (setq YaTeX-typesetting-process nil)
    1.93 +             ;; Force mode line redisplay soon
    1.94 +             (set-buffer-modified-p (buffer-modified-p))
    1.95 +	     )
    1.96 +	   (set-buffer obuf))))
    1.97 +)
    1.98 +
    1.99 +(defvar YaTeX-texput-file "texput.tex"
   1.100 +  "*File name for temporary file of typeset-region."
   1.101 +)
   1.102 +
   1.103 +(defun YaTeX-typeset-region ()
   1.104 +  "Paste the region to the file `texput.tex' and execute jlatex (or other)
   1.105 +to LaTeX typeset.  The region is specified by the rule:
   1.106 +	(1)If keyword `%#BEGIN' is found in the upper direction from (point).
   1.107 +	  (1-1)if the keyword `%#END' is found after `%#BEGIN',
   1.108 +		->Assume the text between `%#BEGIN' and `%#END' as region.
   1.109 +	  (1-2)if the keyword `%#END' is not found anywhere after `%#BEGIN',
   1.110 +		->Assume the text after `%#BEGIN' as region.
   1.111 +	(2)If no `%#BEGIN' usage is found before the (point),
   1.112 +		->Assume the text between current (point) and (mark) as region.
   1.113 +DON'T forget to eliminate the `%#BEGIN/%#END' notation after editing
   1.114 +operation to the region."
   1.115 +  (interactive)
   1.116 +  (save-excursion
   1.117 +    (let*
   1.118 +	((end "") typeout ;Type out message that tells the method of cutting.
   1.119 +	 (cmd (concat (YaTeX-get-latex-command nil) " " YaTeX-texput-file))
   1.120 +	 (buffer (current-buffer)) opoint preamble
   1.121 +	 (region
   1.122 +	  (if (re-search-backward
   1.123 +	       "%#BEGIN" nil t)
   1.124 +	      (progn
   1.125 +		(setq typeout "--- Region from BEGIN to " end "END ---")
   1.126 +		(buffer-substring
   1.127 +		 (match-end 0)
   1.128 +		 (if (re-search-forward "%#END" nil t)
   1.129 +		     (match-beginning 0)
   1.130 +		   (setq end "end of buffer ---")
   1.131 +		   (point-max))))
   1.132 +	    (setq typeout "=== Region from (point) to (mark) ===")
   1.133 +	    (buffer-substring (point) (mark)))))
   1.134 +      (YaTeX-visit-main t)
   1.135 +      (setq opoint (point))
   1.136 +      (goto-char (point-min))
   1.137 +      (setq
   1.138 +       preamble
   1.139 +       (if (re-search-forward "^[ 	]*\\\\begin.*{document}" nil t)
   1.140 +	   (buffer-substring (point-min) (match-end 0))
   1.141 +	 (concat "\\documentstyle{" YaTeX-default-document-style "}\n"
   1.142 +		 "\\begin{document}")))
   1.143 +      (goto-char opoint)
   1.144 +      ;;(set-buffer buffer)		;for clarity
   1.145 +      (find-file YaTeX-texput-file)
   1.146 +      (erase-buffer)
   1.147 +      (if YaTeX-need-nonstop
   1.148 +	  (insert "\\nonstopmode{}\n"))
   1.149 +      (insert preamble "\n")
   1.150 +      (insert region)
   1.151 +      (insert "\\typeout{" typeout end "}\n") ;Notice the selected method.
   1.152 +      (insert "\n\\end{document}\n")
   1.153 +      (basic-save-buffer)
   1.154 +      (kill-buffer (current-buffer))
   1.155 +      (YaTeX-visit-main t)
   1.156 +      (YaTeX-typeset cmd YaTeX-typeset-buffer)
   1.157 +      (put 'dvi2-command 'region t)))
   1.158 +)
   1.159 +
   1.160 +(defun YaTeX-typeset-buffer ()
   1.161 +  "Typeset whole buffer.  If %#! usage says other buffer is main text,
   1.162 +visit main buffer to confirm if its includeonly list contains current
   1.163 +buffer's file.  And if it doesn't contain editing text, ask user which
   1.164 +action want to be done, A:Add list, R:Replace list, %:comment-out list."
   1.165 +  (interactive)
   1.166 +  (YaTeX-save-buffers)
   1.167 +  (let*((me (substring (buffer-name) 0 (rindex (buffer-name) ?.)))
   1.168 +	(mydir (file-name-directory (buffer-file-name)))
   1.169 +	(cmd (YaTeX-get-latex-command t)))
   1.170 +    (if (YaTeX-main-file-p) nil
   1.171 +      (save-excursion
   1.172 +	(YaTeX-visit-main t)	;search into main buffer
   1.173 +	(save-excursion
   1.174 +	  (push-mark (point) t)
   1.175 +	  (goto-char (point-min))
   1.176 +	  (if (and (re-search-forward "^[ 	]*\\\\begin{document}" nil t)
   1.177 +		   (re-search-backward "^[ 	]*\\\\includeonly{" nil t))
   1.178 +	      (let*
   1.179 +		  ((b (progn (skip-chars-forward "^{") (point)))
   1.180 +		   (e (progn (skip-chars-forward "^}") (1+ (point))))
   1.181 +		   (s (buffer-substring b e)) c
   1.182 +		   (pardir (file-name-directory (buffer-file-name))))
   1.183 +		(if (string-match (concat "[{,/]" me "[,}]") s)
   1.184 +		    nil ; Nothing to do when it's already in includeonly.
   1.185 +		  (ding)
   1.186 +		  (switch-to-buffer (current-buffer));Display this buffer.
   1.187 +		  (setq
   1.188 +		   me	  ;;Rewrite my name(me) to contain sub directory name.
   1.189 +		   (concat
   1.190 +		    (if (string-match pardir mydir) ;if mydir is child of main
   1.191 +			(substring mydir (length pardir)) ;cut absolute path
   1.192 +		      mydir) ;else concat absolute path name.
   1.193 +		    me))
   1.194 +		  (message
   1.195 +		   "`%s' is not in \\includeonly. A)dd R)eplace %%)comment? "
   1.196 +		   me)
   1.197 +		  (setq c (read-char))
   1.198 +		  (cond
   1.199 +		   ((= c ?a)
   1.200 +		    (goto-char (1+ b))
   1.201 +		    (insert me (if (string= s "{}") "" ",")))
   1.202 +		   ((= c ?r)
   1.203 +		    (delete-region (1+ b) (1- e)) (insert me))
   1.204 +		   ((= c ?%)
   1.205 +		    (beginning-of-line) (insert "%"))
   1.206 +		   (t nil))
   1.207 +		  (basic-save-buffer))))
   1.208 +	  (exchange-point-and-mark))
   1.209 +	))
   1.210 +    (YaTeX-typeset cmd YaTeX-typeset-buffer)
   1.211 +    (put 'dvi2-command 'region nil))
   1.212 +)
   1.213 +
   1.214 +(defun YaTeX-bibtex-buffer ()
   1.215 +  "Pass the bibliography data of editing file to bibtex."
   1.216 +  (interactive)
   1.217 +  (YaTeX-save-buffers)
   1.218 +  (YaTeX-typeset
   1.219 +   (read-string "BibTeX command: "
   1.220 +		(concat bibtex-command " " (YaTeX-get-preview-file-name)))
   1.221 +   "*YaTeX-bibtex*" )
   1.222 +)
   1.223 +
   1.224 +(defun YaTeX-kill-typeset-process (proc)
   1.225 +  "Kill process PROC after sending signal to PROC.
   1.226 +PROC should be process identifier."
   1.227 +  (cond
   1.228 +   ((eq system-type 'ms-dos)
   1.229 +    (error "MS-DOS can't have concurrent process."))
   1.230 +   ((or (null proc) (not (eq (process-status proc) 'run)))
   1.231 +    (error "No typesetting process."))
   1.232 +   (t (interrupt-process proc)
   1.233 +      (delete-process proc)))
   1.234 +)
   1.235 +
   1.236 +(defun YaTeX-system (command buffer)
   1.237 +  "Execute some command on buffer.  Not a official function."
   1.238 +  (save-excursion
   1.239 +    (with-output-to-temp-buffer buffer
   1.240 +      (if (eq system-type 'ms-dos)
   1.241 +	  (call-process shell-file-name nil buffer nil "/c " command)
   1.242 +	(start-process "system" buffer shell-file-name "-c " command))))
   1.243 +)
   1.244 +
   1.245 +(defun YaTeX-preview (preview-command preview-file)
   1.246 +  "Execute xdvi (or other) to tex-preview."
   1.247 +  (interactive
   1.248 +   (list (read-string "Preview command: " dvi2-command)
   1.249 +	 (read-string "Prefiew file[.dvi]: "
   1.250 +		      ;;(substring (buffer-name) 0 -4)
   1.251 +		      (if (get 'dvi2-command 'region)
   1.252 +			  (substring YaTeX-texput-file
   1.253 +				     0 (rindex YaTeX-texput-file ?.))
   1.254 +			(YaTeX-get-preview-file-name))
   1.255 +		      )))
   1.256 +  (setq dvi2-command preview-command)
   1.257 +  (save-excursion
   1.258 +    (YaTeX-visit-main t)
   1.259 +    (with-output-to-temp-buffer "*dvi-preview*"
   1.260 +      (if (eq system-type 'ms-dos)
   1.261 +	  (progn (send-string-to-terminal "\e[2J")	;if MS-DOS
   1.262 +		 (call-process shell-file-name "con" "*dvi-preview*" nil
   1.263 +			       "/c " dvi2-command preview-file)
   1.264 +		 (redraw-display))
   1.265 +	(start-process "preview" "*dvi-preview*" shell-file-name "-c"
   1.266 +		       (concat dvi2-command " " preview-file)) ;if UNIX
   1.267 +	(message
   1.268 +	 (concat "Starting " dvi2-command " to preview " preview-file)))))
   1.269 +)
   1.270 +
   1.271 +(defun YaTeX-prev-error ()
   1.272 +  "Visit previous error.  The reason why not NEXT-error is to
   1.273 +avoid make confliction of line numbers by editing."
   1.274 +  (interactive)
   1.275 +  (let ((cur-buf (buffer-name))
   1.276 +	YaTeX-error-line error-buffer)
   1.277 +    (if (null (get-buffer YaTeX-typeset-buffer))
   1.278 +	(message "There is no output buffer of typesetting.")
   1.279 +      (pop-to-buffer YaTeX-typeset-buffer)
   1.280 +      (if (eq system-type 'ms-dos)
   1.281 +	  (if (search-backward latex-dos-emergency-message nil t)
   1.282 +	      (progn (goto-char (point-max))
   1.283 +		     (setq error-regexp latex-error-regexp))
   1.284 +	    (beginning-of-line)
   1.285 +	    (forward-char -1)
   1.286 +	    (setq error-regexp latex-warning-regexp))
   1.287 +	(if YaTeX-typeset-process      ; if jlatex on UNIX
   1.288 +	    (if (eq (process-status YaTeX-typeset-process) 'run)
   1.289 +		(progn
   1.290 +		  (goto-char (point-max))
   1.291 +		  (setq error-regexp latex-error-regexp))
   1.292 +	      (beginning-of-line)
   1.293 +	      (setq error-regexp latex-warning-regexp))))
   1.294 +      (if (re-search-backward error-regexp nil t)
   1.295 +	  (save-restriction
   1.296 +	    (set-mark-command nil)
   1.297 +	    (end-of-line)
   1.298 +	    (narrow-to-region (point) (mark))
   1.299 +	    (goto-char (point-min))
   1.300 +	    (re-search-forward "[0-9]")
   1.301 +	    (forward-char -1)
   1.302 +	    (set-mark (point))
   1.303 +	    (skip-chars-forward "0-9")
   1.304 +	    (narrow-to-region (point) (mark))
   1.305 +	    (goto-char (point-min))
   1.306 +	    (setq YaTeX-error-line (read (current-buffer))))
   1.307 +	(message "No more error on %s" cur-buf)
   1.308 +	(ding)
   1.309 +	)
   1.310 +      (setq error-buffer (YaTeX-get-error-file cur-buf))
   1.311 +      (other-window -1)
   1.312 +      (switch-to-buffer cur-buf)
   1.313 +      (if (null YaTeX-error-line)
   1.314 +	  nil
   1.315 +	;; if warning or error found
   1.316 +	(YaTeX-switch-to-buffer error-buffer)
   1.317 +	(goto-line YaTeX-error-line)
   1.318 +	(message "latex error or warning in '%s' at line: %d"
   1.319 +		 error-buffer YaTeX-error-line)
   1.320 +	(other-window 1)
   1.321 +	(skip-chars-backward "[0-9]")
   1.322 +	(recenter (/ (window-height) 2))
   1.323 +	(sit-for 3)
   1.324 +	(forward-char -1)
   1.325 +	(other-window -1)
   1.326 +	)))
   1.327 +)
   1.328 +
   1.329 +(defun YaTeX-jump-error-line ()
   1.330 +  "Jump corresponding line on latex command's error message."
   1.331 +  (interactive)
   1.332 +  (let ((p (point))
   1.333 +	(end (progn (end-of-line) (point)))
   1.334 +	(begin (progn (beginning-of-line)(point))))
   1.335 +    (if (null (re-search-forward "l[ ines]*\\.*[1-9][0-9]*" end t))
   1.336 +	(if (save-excursion (end-of-line) (eobp))
   1.337 +	    (progn (goto-char p) (insert (this-command-keys)))
   1.338 +	  (message "No line number expression"))
   1.339 +      (goto-char (match-beginning 0))
   1.340 +      (re-search-forward "[1-9][0-9]*" end t)
   1.341 +      (save-restriction
   1.342 +	(let ((error-line
   1.343 +	       (string-to-int (buffer-substring (match-beginning 0)
   1.344 +						(match-end 0))))
   1.345 +	      (error-file (YaTeX-get-error-file current-TeX-buffer)))
   1.346 +	  ;;(goto-char (match-beginning 0))
   1.347 +	  (other-window -1)
   1.348 +	  (message "errors in %s" error-file)
   1.349 +	  ;(switch-to-buffer current-TeX-buffer)
   1.350 +	  (if (not (YaTeX-switch-to-buffer error-file))
   1.351 +	      (error "%s is not found in this directory."))
   1.352 +	  (goto-line error-line)))))
   1.353 +)
   1.354 +
   1.355 +(defun YaTeX-send-string ()
   1.356 +  "Send string to current typeset process."
   1.357 +  (interactive)
   1.358 +  (if (and (eq (process-status YaTeX-typeset-process) 'run)
   1.359 +	   (>= (point) (process-mark YaTeX-typeset-process)))
   1.360 +      (let ((b (process-mark YaTeX-typeset-process))
   1.361 +	    (e (point-end-of-line)))
   1.362 +	(goto-char b)
   1.363 +	(skip-chars-forward " \t" e)
   1.364 +	(setq b (point))
   1.365 +	(process-send-string
   1.366 +	 YaTeX-typeset-process (concat (buffer-substring b e) "\n"))
   1.367 +	(goto-char e)
   1.368 +	(insert "\n")
   1.369 +	(set-marker (process-mark YaTeX-typeset-process) (point))
   1.370 +	(insert " "))
   1.371 +    (ding))
   1.372 +)
   1.373 +
   1.374 +(defun YaTeX-view-error ()
   1.375 +  (interactive)
   1.376 +  (if (null (get-buffer YaTeX-typeset-buffer))
   1.377 +      (message "No typeset buffer found.")
   1.378 +    (pop-to-buffer YaTeX-typeset-buffer)
   1.379 +    (goto-char (point-max))
   1.380 +    (recenter -1)
   1.381 +    (other-window -1))
   1.382 +)
   1.383 +
   1.384 +(defun YaTeX-get-error-file (default)
   1.385 +  "Get current processing file from typesetting log."
   1.386 +  (save-excursion
   1.387 +    (let(s)
   1.388 +      (condition-case () (up-list -1)
   1.389 +	(error
   1.390 +	 (let ((list 0) found)
   1.391 +	   (while
   1.392 +	       (and (<= list 0) (not found)
   1.393 +		    (re-search-backward "\\((\\)\\|\\()\\)" nil t))
   1.394 +	     (if (equal (match-beginning 0) (match-beginning 2)) ;close paren.
   1.395 +		 (setq list (1- list)) ;open paren
   1.396 +	       (setq list (1+ list))
   1.397 +	       (if (= list 1)
   1.398 +		   (if (looking-at "\\([^,{}%]+\.\\)tex\\|sty")
   1.399 +		       (setq found t)
   1.400 +		     (setq list (1- list)))))))))
   1.401 +      (setq s
   1.402 +	    (buffer-substring
   1.403 +	     (progn (forward-char 1) (point))
   1.404 +	     (progn (skip-chars-forward "-A-Za-z0-9_/\.\\" (point-end-of-line))
   1.405 +		    (point))))
   1.406 +      (if (string= "" s) default s)))
   1.407 +)
   1.408 +      
   1.409 +(defun YaTeX-put-nonstopmode ()
   1.410 +  (if YaTeX-need-nonstop
   1.411 +      (if (re-search-backward "\\nonstopmode{}" (point-min) t)
   1.412 +	  nil                    ;if already written in text then do nothing
   1.413 +	(save-excursion
   1.414 +	  (YaTeX-visit-main t)
   1.415 +	  (goto-char (point-min))
   1.416 +	  (insert "\\nonstopmode{}%_YaTeX_%\n")))
   1.417 +    )
   1.418 +)
   1.419 +
   1.420 +(defun YaTeX-remove-nonstopmode ()
   1.421 +  (if YaTeX-need-nonstop ;for speed
   1.422 +      (save-excursion
   1.423 +	(YaTeX-visit-main t)
   1.424 +	(goto-char (point-min))
   1.425 +	(forward-line 1)
   1.426 +	(narrow-to-region (point-min) (point))
   1.427 +	(goto-char (point-min))
   1.428 +	(delete-matching-lines "^\\\\nonstopmode\\{\\}%_YaTeX_%$")
   1.429 +	(widen)))
   1.430 +)
   1.431 +
   1.432 +(defun YaTeX-get-preview-file-name ()
   1.433 +  "Get file name to preview by inquiring YaTeX-get-latex-command"
   1.434 +  (let* ((latex-cmd (YaTeX-get-latex-command t))
   1.435 +	 (rin (rindex latex-cmd ? ))
   1.436 +	 (fname (if (> rin -1) (substring latex-cmd (1+ rin)) ""))
   1.437 +	 (period))
   1.438 +    (if (string= fname "")
   1.439 +	(setq fname (substring (buffer-name) 0 -4))
   1.440 +      (setq period (rindex fname ?.))
   1.441 +      (setq fname (substring fname 0 (if (eq -1 period) nil period)))
   1.442 +      ))
   1.443 +)
   1.444 +
   1.445 +(defun YaTeX-get-latex-command (&optional switch)
   1.446 +  "Specify the latex-command name and its argument.
   1.447 +If there is a line which begins by string: \"%#!\", the following
   1.448 +strings are assumed to be the latex-command and arguments.  The
   1.449 +default value of latex-command is:
   1.450 +	tex-command (buffer-name)
   1.451 +and if you write \"%#!jlatex\" in the beginning of certain line.
   1.452 +	\"jlatex \" (buffer-name)
   1.453 +will be the latex-command,
   1.454 +and you write \"%#!jlatex main.tex\" on some line and argument SWITCH
   1.455 +is t, then
   1.456 +	\"jlatex main.tex\"
   1.457 +will be given to the shell."
   1.458 +  (let*
   1.459 +      ((default-command
   1.460 +	 (concat tex-command " "
   1.461 +		 (if switch (buffer-name) ""))));default value
   1.462 +    (save-excursion
   1.463 +      (goto-char (point-min))
   1.464 +      (if (null (re-search-forward "^%#!" (point-max) t))
   1.465 +	  default-command
   1.466 +	(skip-chars-forward "%#! 	")
   1.467 +	(if (eolp)
   1.468 +	    default-command
   1.469 +	  (let ((s (point)))
   1.470 +	    (skip-chars-forward "^ 	" (point-end-of-line)) ;Skip command
   1.471 +	    (skip-chars-forward " 	" (point-end-of-line))
   1.472 +	    ;(setq YaTeX-latex-command (buffer-substring s (point)))
   1.473 +	    (cond
   1.474 +	     ((null switch)
   1.475 +	      (buffer-substring s (point)))
   1.476 +	     ((eolp)			 ;Only return command name
   1.477 +	      (concat (buffer-substring s (point)) " " (buffer-name)))
   1.478 +	     (t(end-of-line)		   ;Change entire command name
   1.479 +	       (buffer-substring s (point))) ;including arguments.
   1.480 +	    ))
   1.481 +	))))
   1.482 +)
   1.483 +
   1.484 +(defun YaTeX-replace-format (string format repl)
   1.485 +  "In STRING, replace first appearance of FORMAT to REPL as if
   1.486 +function `format' does.  FORMAT does not contain `%'"
   1.487 +  (let ((beg (or (string-match (concat "^\\(%" format "\\)") string)
   1.488 +		 (string-match (concat "[^%]\\(%" format "\\)") string)))
   1.489 +	(len (length format)))
   1.490 +    (if (null beg) string ;no conversion
   1.491 +      (concat
   1.492 +       (substring string 0 (match-beginning 1)) repl
   1.493 +       (substring string (match-end 1)))))
   1.494 +)
   1.495 +
   1.496 +(defun YaTeX-lpr (arg)
   1.497 +  "Print out.  If prefix arg ARG is non nil, call print driver without
   1.498 +page range description."
   1.499 +  (interactive "P")
   1.500 +  (let*(from to (cmd (or (YaTeX-get-builtin "LPR") dviprint-command-format)))
   1.501 +    (setq
   1.502 +     cmd 
   1.503 +     (YaTeX-replace-format
   1.504 +      cmd "f"
   1.505 +      (if (or arg (not (string-match "%f" cmd)))
   1.506 +	      ""
   1.507 +	    (YaTeX-replace-format
   1.508 +	     dviprint-from-format
   1.509 +	     "b"
   1.510 +	     (if (string=
   1.511 +		  (setq from (read-string "From page(default 1): ")) "")
   1.512 +		 "1" from))))
   1.513 +       )
   1.514 +    (setq
   1.515 +     cmd
   1.516 +     (YaTeX-replace-format
   1.517 +      cmd "t"
   1.518 +      (if (or arg (not (string-match "%t" cmd))
   1.519 +	      (string= 
   1.520 +	       (setq to (read-string "To page(default none): ")) ""))
   1.521 +	  ""
   1.522 +	(YaTeX-replace-format dviprint-to-format "e" to)))
   1.523 +     )
   1.524 +    (setq cmd (read-string "Edit command line: "
   1.525 +			   (format cmd (YaTeX-get-preview-file-name))))
   1.526 +    (save-excursion
   1.527 +      (YaTeX-visit-main t) ;;change execution directory
   1.528 +      (with-output-to-temp-buffer "*dvi-printing*"
   1.529 +	(if (eq system-type 'ms-dos)
   1.530 +	    (call-process shell-file-name "con" "*dvi-printing*" nil
   1.531 +			  "/c " cmd)
   1.532 +	  (start-process "print" "*dvi-printing*" shell-file-name "-c" cmd)
   1.533 +	  (message (concat "Starting " cmd " to printing "
   1.534 +			   (YaTeX-get-preview-file-name))))
   1.535 +    )))
   1.536 +)
   1.537 +
   1.538 +(defun YaTeX-main-file-p ()
   1.539 +  "Return if current buffer is main LaTeX source."
   1.540 +  (string-match (concat "^" (YaTeX-get-preview-file-name) ".tex")(buffer-name))
   1.541 +)
   1.542 +
   1.543 +(defun YaTeX-visit-main (&optional setbuf)
   1.544 +  "Switch to buffer main LaTeX source.  Use set-buffer instead of
   1.545 +switch-to-buffer if optional second argument SETBUF is t(Use it only
   1.546 +in Emacs-Lisp program)."
   1.547 +  (interactive)
   1.548 +  (let ((main-file (YaTeX-get-preview-file-name)))
   1.549 +    (if (string-match (concat "^" main-file ".tex") (buffer-name))
   1.550 +	(if (interactive-p) (message "I think this is main LaTeX source.") nil)
   1.551 +      (cond
   1.552 +       ((YaTeX-switch-to-buffer (setq main-file (concat main-file ".tex"))
   1.553 +				setbuf))
   1.554 +       ((and (file-exists-p (setq main-file (concat "../" main-file)))
   1.555 +	     (y-or-n-p (concat (expand-file-name main-file)
   1.556 +			       " is main file?:")))
   1.557 +	(YaTeX-switch-to-buffer main-file setbuf))
   1.558 +       (t (find-file (read-file-name "Enter your main text: " nil nil 1)))
   1.559 +	)))
   1.560 +  nil
   1.561 +)
   1.562 +
   1.563 +(defun YaTeX-visit-main-other-window ()
   1.564 +  "Switch to buffer main LaTeX source in other window."
   1.565 +  (interactive)
   1.566 +  (if (YaTeX-main-file-p) (message "I think this is main LaTeX source.")
   1.567 +      (YaTeX-switch-to-buffer-other-window
   1.568 +       (concat (YaTeX-get-preview-file-name) ".tex")))
   1.569 +)
   1.570 +
   1.571 +(defun YaTeX-get-builtin (key)
   1.572 +  "Read source built-in command of %# usage."
   1.573 +  (save-excursion
   1.574 +    (goto-char (point-min))
   1.575 +    (if (and (search-forward (concat "%#" key) nil t)
   1.576 +	     (not (eolp)))
   1.577 +	(buffer-substring
   1.578 +	 (progn (skip-chars-forward " 	" (point-end-of-line))(point))
   1.579 +	 (point-end-of-line))
   1.580 +      nil))
   1.581 +)
   1.582 +
   1.583 +(defun YaTeX-save-buffers ()
   1.584 +  "Save buffers which is in yatex-mode."
   1.585 +  (basic-save-buffer)
   1.586 +  (save-excursion
   1.587 +    (mapcar '(lambda (buf)
   1.588 +	       (set-buffer buf)
   1.589 +	       (if (and (buffer-file-name buf)
   1.590 +			(eq major-mode 'yatex-mode)
   1.591 +			(buffer-modified-p buf)
   1.592 +			(y-or-n-p (format "Save %s" (buffer-name buf))))
   1.593 +		   (save-buffer buf)))
   1.594 +	    (buffer-list)))
   1.595 +)
   1.596 +
   1.597 +(provide 'yatexprc)