changeset 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 49be9ccb0b65
children c746646cecf5
files docs/yatex.ref yatex.el yatex.new yatex.prj yatexprc.el
diffstat 5 files changed, 752 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/docs/yatex.ref	Tue May 04 12:57:27 1993 +0000
@@ -0,0 +1,119 @@
+----------------------------------------------------------------------------
+                                 『野鳥』
+                           クイックリファレンス
+----------------------------------------------------------------------------
+
+
+【注意】
+
+	  野鳥では、prefix キーをカスタマイズできるため(標準ではC-c)これを
+	[prefix] と表記します。C-c のまま変更していない場合、[prefix] a は
+	C-c a を表わします。
+
+
+【補完】
+
+	◆アクセント補完		[prefix] a
+	◆begin型補完			[prefix] b SPC
+	◆begin型補完(領域指定)		[prefix] B SPC
+	◆begin型補完(即時)		[prefix] b 環境の頭文字
+	◆begin型補完(〃領域指定)	[prefix] B 環境の頭文字
+	◆end補完			[prefix] e
+	◆large型補完			[prefix] l
+	◆large型補完(領域指定)		[prefix] L
+	◆maketitle型補完		[prefix] m
+	◆section型補完			[prefix] s
+	◆随時補完			[prefix] SPC
+
+	  即時begin型補完の、環境名の頭文字の対応については、yatex.doc を
+	ご覧ください。領域指定モードでは、マークした位置とポイントの間を
+	\begin{center}…\end{center} や {\large } などで括ります。
+
+
+【プロセス起動】
+
+	◆jbibtex起動			[prefix] tb
+	◆jlatex起動			[prefix] tj
+	◆jlatex停止			[prefix] tk	※
+	◆プリントアウト		[prefix] tl
+	◆プリントアウト(全ページ)	C-u [prefix] tl
+	◆プレビューア起動		[prefix] tp
+	◆jlatex起動(領域指定)		[prefix] tr
+
+	  [prefix] t を押せばメニューが出るので、[prefix] t の次に押すキー
+	は覚えなくて構いません。
+
+	  ※は、MS-DOS では無効です。
+
+	  [prefix] tr の領域指定は、文書中の、 %#BEGIN と書いてある行から、
+	%#END と書いてある行までです。%#END を省略すると、%#BEGINから文書
+	末まで、両方省略すると、マークした位置から、ポイントまでが指定領域
+	となります。
+
+
+【カーソルジャンプ】
+
+	◆タイプセットエラー箇所へ	[prefix] '
+
+	  タイプセットエラー発生箇所へ(末尾から)遡って順次ジャンプします。
+	もちろん jlatex でエラーが生じた場合のみ有効です。
+
+	◆対応するオブジェクトジャンプ	[prefix] g
+
+	  カーソルジャンプは、[prefix] g を押す時のカーソルの位置により、
+	ジャンプすべき場所を判断します。
+
+	・\begin{}, \end{}の上		対応する \end{},\begin{} へ
+	・%#BEGIN, %#END の上		対応する %#BEGIN, %#END へ
+	・\include(only), \input の上	対応するファイル
+
+	◆メインファイルへジャンプ	[prefix] ^
+	◆別ウィンドウで   〃		[prefix] 4^
+
+	  メインファイルを野鳥に知らせるために、作成ファイルの任意の位置に、
+			%#!jlatex メインファイル名
+	のような行を書いておいて下さい。
+
+
+【%によるコメントアウト】
+
+	◆領域/環境のコメントアウト	[prefix] >
+	◆領域/環境のコメント除去	[prefix] <
+
+	  カーソルが、\begin{} または \end{} の行にある時は、その環境に含
+	まれる文章全てをコメントアウト/除去し、それ以外の時は、マークとポ
+	イント間の文章をコメントアウト/除去します。
+
+	◆段落/環境宣言コメントアウト	[prefix] .
+	◆段落/環境宣言コメント除去	[prefix] ,
+
+	  カーソルが、\begin{} または \end{} の行にある時は、それら二つの
+	行をコメントアウト/除去し、それ以外の時は、カーソルの属する段落を
+	コメントアウト/除去します。
+
+
+【その他】
+	
+	◆itemの桁揃え			[prefix] i
+	◆LaTeXコマンド変更		[prefix] c
+
+	  \begin{}, \end{} の環境名を同時に変更します。
+
+	◆コマンド削除			[prefix] k
+
+	  \begin{}, \end{} のペア、または %#BEGIN, %#END のペアを一挙に削
+	除します。
+
+	◆領域を{}で括る		[prefix] }
+	◆領域を[]で括る		[prefix] ]
+
+
+								   広瀬雄二
+							yuuji@ae.keio.ac.jp
+							pcs39334(ASCII-NET)
+
+
+Local variables:
+mode: text
+fill-prefix: "	"
+End:
Binary file yatex.el has changed
--- a/yatex.new	Mon Feb 22 11:04:53 1993 +0000
+++ b/yatex.new	Tue May 04 12:57:27 1993 +0000
@@ -2,6 +2,31 @@
 	Yet Another tex-mode for Emacs
 	yatex.el 各バージョンの変更点について。
 
+1.42:	fill-paragraph と、(un)comment-paragraph の適正化。
+	[prefix] k での削除範囲の適正化。
+	YaTeX-end-environment などで verb(atim)中のbegin/endは数えない。
+	LaTeX error 発生ファイルの検出の確実化。
+	begin型補完のインデントは必ず \begin の桁に合わせるようにした。
+	対応する \label{} または \ref{} へのジャンプ。
+
+1.41:	アドイン関数の自動作成モード追加。
+	アドイン関数のmaketitle型補完への対応と、現在の補完形式を変数
+	YaTeX-current-completion-type に記憶。
+	タイプセットバッファで jlatex に文字列を送る処理の完全化。
+
+1.40:	編集ファイルをサブディレクトリ毎に分けた場合に対応。
+	アドイン関数をsection型補完にも対応し、仕様を固めた。
+	領域指定のタイプセット直後のプレビューファイル候補は texput に。
+	プロセス処理関係の関数を別ファイルに分けた。
+	section型補完で、セクションタイトル(chapter, section, ...) は
+	read-string で読み込むようにした。また、引数0を付加することにより
+	明示的に read-string での読み込みを指定可能。
+	yatexadd.el が load-path 中にある場合のみ自動的にロードする。
+
+1.39:	編集しながらタイプセット画面をスクロール。
+	BibTeX 起動の追加。タイプセットプロセスの中断。
+	jlatex の ? プロンプトに対して、文字列を送れるようにした。
+
 1.38:	YaTeX-nervous が t の時、一時的、またはカレントディレクトリでのみ
 	有効なテンポラリ辞書が利用可能。
 	section型補完に引数の数も埋め込み、これも学習する。
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/yatex.prj	Tue May 04 12:57:27 1993 +0000
@@ -0,0 +1,14 @@
+//今後の拡張予定//
+
+【section型補完の引数の候補】
+
+	(setq section-table
+	      '(("part" 0) ;...段落コマンドなど...
+		("documentstyle" 1 YaTeX:documentstyle)	;候補シンボル
+		("setlength" 2 )))
+
+
+Local Variables:
+mode: text
+fill-prefix: "	"
+End:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/yatexprc.el	Tue May 04 12:57:27 1993 +0000
@@ -0,0 +1,594 @@
+;;; -*- Emacs-Lisp -*-
+;;; YaTeX process handler.
+;;; yatexprc.el rev.1.42
+;;; (c)1993 by HIROSE Yuuji.[yuuji@ae.keio.ac.jp]
+;;; Last modified Tue May  4 21:49:59 1993 on figaro
+;;; $Id$
+
+(require 'yatex)
+
+(defvar YaTeX-typeset-process nil
+  "Process identifier for jlatex"
+)
+(defvar YaTeX-typeset-buffer "*YaTeX-typesetting*"
+  "Process buffer for jlatex")
+
+(defun YaTeX-typeset (command buffer)
+  "Execute jlatex (or other) to LaTeX typeset."
+  (interactive)
+  (if (and YaTeX-typeset-process
+	   (eq (process-status YaTeX-typeset-process) 'run))
+      ;; if tex command is halting.
+      (YaTeX-kill-typeset-process YaTeX-typeset-process))
+  (YaTeX-visit-main t)  ;;execution directory
+  (with-output-to-temp-buffer buffer
+    (if (eq system-type 'ms-dos)			;if MS-DOS
+	(progn
+	  (message (concat "Typesetting " (buffer-name) "..."))
+	  (YaTeX-put-nonstopmode)
+	  (call-process shell-file-name
+			nil buffer nil "/c" command)
+	  (YaTeX-remove-nonstopmode))
+      (setq YaTeX-typeset-process			;if UNIX
+	      (start-process "LaTeX" buffer shell-file-name "-c"
+			     command))
+      (set-process-sentinel YaTeX-typeset-process 'YaTeX-typeset-sentinel)))
+  (setq current-TeX-buffer (buffer-name))
+  (other-window 1)
+  (use-local-map YaTeX-typesetting-mode-map)
+  (setq mode-name "typeset")
+  (if YaTeX-typeset-process ; if process is running (maybe on UNIX)
+      (cond ((boundp 'MULE)
+	     (set-current-process-coding-system
+	      YaTeX-latex-message-code YaTeX-coding-system))
+	    ((boundp 'NEMACS)
+	     (set-kanji-process-code YaTeX-latex-message-code))))
+  (message "Type SPC to continue.")
+  (goto-char (point-max))
+  (if (eq system-type 'ms-dos) (message "Done.")
+    (while (bobp) (message "Invoking process. wait...") (sleep-for 1))
+    (insert (message " ")))
+  (if (bolp) (forward-line -1))
+  (recenter -1)
+  (other-window -1)
+)
+
+(defun YaTeX-typeset-sentinel (proc mes)
+  (cond ((null (buffer-name (process-buffer proc)))
+         ;; buffer killed
+         (set-process-buffer proc nil))
+        ((memq (process-status proc) '(signal exit))
+         (let* ((obuf (current-buffer)))
+           ;; save-excursion isn't the right thing if
+           ;;  process-buffer is current-buffer
+           (unwind-protect
+               (progn
+                 ;; Write something in *typesetting* and hack its mode line
+		 (if (equal (current-buffer) (process-buffer proc))
+		     nil
+		   (other-window 1)
+		   (switch-to-buffer (process-buffer proc))
+		   (goto-char (point-max))
+		   (recenter -3)
+		   (other-window -1))
+		 (set-buffer (process-buffer proc))
+                 (goto-char (point-max))
+                 (insert ?\n "latex typesetting " mes)
+                 (forward-char -1)
+                 (insert " at " (substring (current-time-string) 0 -5) "\n")
+                 (forward-char 1)
+                 (setq mode-line-process
+                       (concat ": "
+                               (symbol-name (process-status proc))))
+		 (message "latex typesetting done.")
+                 ;; If buffer and mode line will show that the process
+                 ;; is dead, we can delete it now.  Otherwise it
+                 ;; will stay around until M-x list-processes.
+                 (delete-process proc)
+		 )
+             (setq YaTeX-typesetting-process nil)
+             ;; Force mode line redisplay soon
+             (set-buffer-modified-p (buffer-modified-p))
+	     )
+	   (set-buffer obuf))))
+)
+
+(defvar YaTeX-texput-file "texput.tex"
+  "*File name for temporary file of typeset-region."
+)
+
+(defun YaTeX-typeset-region ()
+  "Paste the region to the file `texput.tex' and execute jlatex (or other)
+to LaTeX typeset.  The region is specified by the rule:
+	(1)If keyword `%#BEGIN' is found in the upper direction from (point).
+	  (1-1)if the keyword `%#END' is found after `%#BEGIN',
+		->Assume the text between `%#BEGIN' and `%#END' as region.
+	  (1-2)if the keyword `%#END' is not found anywhere after `%#BEGIN',
+		->Assume the text after `%#BEGIN' as region.
+	(2)If no `%#BEGIN' usage is found before the (point),
+		->Assume the text between current (point) and (mark) as region.
+DON'T forget to eliminate the `%#BEGIN/%#END' notation after editing
+operation to the region."
+  (interactive)
+  (save-excursion
+    (let*
+	((end "") typeout ;Type out message that tells the method of cutting.
+	 (cmd (concat (YaTeX-get-latex-command nil) " " YaTeX-texput-file))
+	 (buffer (current-buffer)) opoint preamble
+	 (region
+	  (if (re-search-backward
+	       "%#BEGIN" nil t)
+	      (progn
+		(setq typeout "--- Region from BEGIN to " end "END ---")
+		(buffer-substring
+		 (match-end 0)
+		 (if (re-search-forward "%#END" nil t)
+		     (match-beginning 0)
+		   (setq end "end of buffer ---")
+		   (point-max))))
+	    (setq typeout "=== Region from (point) to (mark) ===")
+	    (buffer-substring (point) (mark)))))
+      (YaTeX-visit-main t)
+      (setq opoint (point))
+      (goto-char (point-min))
+      (setq
+       preamble
+       (if (re-search-forward "^[ 	]*\\\\begin.*{document}" nil t)
+	   (buffer-substring (point-min) (match-end 0))
+	 (concat "\\documentstyle{" YaTeX-default-document-style "}\n"
+		 "\\begin{document}")))
+      (goto-char opoint)
+      ;;(set-buffer buffer)		;for clarity
+      (find-file YaTeX-texput-file)
+      (erase-buffer)
+      (if YaTeX-need-nonstop
+	  (insert "\\nonstopmode{}\n"))
+      (insert preamble "\n")
+      (insert region)
+      (insert "\\typeout{" typeout end "}\n") ;Notice the selected method.
+      (insert "\n\\end{document}\n")
+      (basic-save-buffer)
+      (kill-buffer (current-buffer))
+      (YaTeX-visit-main t)
+      (YaTeX-typeset cmd YaTeX-typeset-buffer)
+      (put 'dvi2-command 'region t)))
+)
+
+(defun YaTeX-typeset-buffer ()
+  "Typeset whole buffer.  If %#! usage says other buffer is main text,
+visit main buffer to confirm if its includeonly list contains current
+buffer's file.  And if it doesn't contain editing text, ask user which
+action want to be done, A:Add list, R:Replace list, %:comment-out list."
+  (interactive)
+  (YaTeX-save-buffers)
+  (let*((me (substring (buffer-name) 0 (rindex (buffer-name) ?.)))
+	(mydir (file-name-directory (buffer-file-name)))
+	(cmd (YaTeX-get-latex-command t)))
+    (if (YaTeX-main-file-p) nil
+      (save-excursion
+	(YaTeX-visit-main t)	;search into main buffer
+	(save-excursion
+	  (push-mark (point) t)
+	  (goto-char (point-min))
+	  (if (and (re-search-forward "^[ 	]*\\\\begin{document}" nil t)
+		   (re-search-backward "^[ 	]*\\\\includeonly{" nil t))
+	      (let*
+		  ((b (progn (skip-chars-forward "^{") (point)))
+		   (e (progn (skip-chars-forward "^}") (1+ (point))))
+		   (s (buffer-substring b e)) c
+		   (pardir (file-name-directory (buffer-file-name))))
+		(if (string-match (concat "[{,/]" me "[,}]") s)
+		    nil ; Nothing to do when it's already in includeonly.
+		  (ding)
+		  (switch-to-buffer (current-buffer));Display this buffer.
+		  (setq
+		   me	  ;;Rewrite my name(me) to contain sub directory name.
+		   (concat
+		    (if (string-match pardir mydir) ;if mydir is child of main
+			(substring mydir (length pardir)) ;cut absolute path
+		      mydir) ;else concat absolute path name.
+		    me))
+		  (message
+		   "`%s' is not in \\includeonly. A)dd R)eplace %%)comment? "
+		   me)
+		  (setq c (read-char))
+		  (cond
+		   ((= c ?a)
+		    (goto-char (1+ b))
+		    (insert me (if (string= s "{}") "" ",")))
+		   ((= c ?r)
+		    (delete-region (1+ b) (1- e)) (insert me))
+		   ((= c ?%)
+		    (beginning-of-line) (insert "%"))
+		   (t nil))
+		  (basic-save-buffer))))
+	  (exchange-point-and-mark))
+	))
+    (YaTeX-typeset cmd YaTeX-typeset-buffer)
+    (put 'dvi2-command 'region nil))
+)
+
+(defun YaTeX-bibtex-buffer ()
+  "Pass the bibliography data of editing file to bibtex."
+  (interactive)
+  (YaTeX-save-buffers)
+  (YaTeX-typeset
+   (read-string "BibTeX command: "
+		(concat bibtex-command " " (YaTeX-get-preview-file-name)))
+   "*YaTeX-bibtex*" )
+)
+
+(defun YaTeX-kill-typeset-process (proc)
+  "Kill process PROC after sending signal to PROC.
+PROC should be process identifier."
+  (cond
+   ((eq system-type 'ms-dos)
+    (error "MS-DOS can't have concurrent process."))
+   ((or (null proc) (not (eq (process-status proc) 'run)))
+    (error "No typesetting process."))
+   (t (interrupt-process proc)
+      (delete-process proc)))
+)
+
+(defun YaTeX-system (command buffer)
+  "Execute some command on buffer.  Not a official function."
+  (save-excursion
+    (with-output-to-temp-buffer buffer
+      (if (eq system-type 'ms-dos)
+	  (call-process shell-file-name nil buffer nil "/c " command)
+	(start-process "system" buffer shell-file-name "-c " command))))
+)
+
+(defun YaTeX-preview (preview-command preview-file)
+  "Execute xdvi (or other) to tex-preview."
+  (interactive
+   (list (read-string "Preview command: " dvi2-command)
+	 (read-string "Prefiew file[.dvi]: "
+		      ;;(substring (buffer-name) 0 -4)
+		      (if (get 'dvi2-command 'region)
+			  (substring YaTeX-texput-file
+				     0 (rindex YaTeX-texput-file ?.))
+			(YaTeX-get-preview-file-name))
+		      )))
+  (setq dvi2-command preview-command)
+  (save-excursion
+    (YaTeX-visit-main t)
+    (with-output-to-temp-buffer "*dvi-preview*"
+      (if (eq system-type 'ms-dos)
+	  (progn (send-string-to-terminal "\e[2J")	;if MS-DOS
+		 (call-process shell-file-name "con" "*dvi-preview*" nil
+			       "/c " dvi2-command preview-file)
+		 (redraw-display))
+	(start-process "preview" "*dvi-preview*" shell-file-name "-c"
+		       (concat dvi2-command " " preview-file)) ;if UNIX
+	(message
+	 (concat "Starting " dvi2-command " to preview " preview-file)))))
+)
+
+(defun YaTeX-prev-error ()
+  "Visit previous error.  The reason why not NEXT-error is to
+avoid make confliction of line numbers by editing."
+  (interactive)
+  (let ((cur-buf (buffer-name))
+	YaTeX-error-line error-buffer)
+    (if (null (get-buffer YaTeX-typeset-buffer))
+	(message "There is no output buffer of typesetting.")
+      (pop-to-buffer YaTeX-typeset-buffer)
+      (if (eq system-type 'ms-dos)
+	  (if (search-backward latex-dos-emergency-message nil t)
+	      (progn (goto-char (point-max))
+		     (setq error-regexp latex-error-regexp))
+	    (beginning-of-line)
+	    (forward-char -1)
+	    (setq error-regexp latex-warning-regexp))
+	(if YaTeX-typeset-process      ; if jlatex on UNIX
+	    (if (eq (process-status YaTeX-typeset-process) 'run)
+		(progn
+		  (goto-char (point-max))
+		  (setq error-regexp latex-error-regexp))
+	      (beginning-of-line)
+	      (setq error-regexp latex-warning-regexp))))
+      (if (re-search-backward error-regexp nil t)
+	  (save-restriction
+	    (set-mark-command nil)
+	    (end-of-line)
+	    (narrow-to-region (point) (mark))
+	    (goto-char (point-min))
+	    (re-search-forward "[0-9]")
+	    (forward-char -1)
+	    (set-mark (point))
+	    (skip-chars-forward "0-9")
+	    (narrow-to-region (point) (mark))
+	    (goto-char (point-min))
+	    (setq YaTeX-error-line (read (current-buffer))))
+	(message "No more error on %s" cur-buf)
+	(ding)
+	)
+      (setq error-buffer (YaTeX-get-error-file cur-buf))
+      (other-window -1)
+      (switch-to-buffer cur-buf)
+      (if (null YaTeX-error-line)
+	  nil
+	;; if warning or error found
+	(YaTeX-switch-to-buffer error-buffer)
+	(goto-line YaTeX-error-line)
+	(message "latex error or warning in '%s' at line: %d"
+		 error-buffer YaTeX-error-line)
+	(other-window 1)
+	(skip-chars-backward "[0-9]")
+	(recenter (/ (window-height) 2))
+	(sit-for 3)
+	(forward-char -1)
+	(other-window -1)
+	)))
+)
+
+(defun YaTeX-jump-error-line ()
+  "Jump corresponding line on latex command's error message."
+  (interactive)
+  (let ((p (point))
+	(end (progn (end-of-line) (point)))
+	(begin (progn (beginning-of-line)(point))))
+    (if (null (re-search-forward "l[ ines]*\\.*[1-9][0-9]*" end t))
+	(if (save-excursion (end-of-line) (eobp))
+	    (progn (goto-char p) (insert (this-command-keys)))
+	  (message "No line number expression"))
+      (goto-char (match-beginning 0))
+      (re-search-forward "[1-9][0-9]*" end t)
+      (save-restriction
+	(let ((error-line
+	       (string-to-int (buffer-substring (match-beginning 0)
+						(match-end 0))))
+	      (error-file (YaTeX-get-error-file current-TeX-buffer)))
+	  ;;(goto-char (match-beginning 0))
+	  (other-window -1)
+	  (message "errors in %s" error-file)
+	  ;(switch-to-buffer current-TeX-buffer)
+	  (if (not (YaTeX-switch-to-buffer error-file))
+	      (error "%s is not found in this directory."))
+	  (goto-line error-line)))))
+)
+
+(defun YaTeX-send-string ()
+  "Send string to current typeset process."
+  (interactive)
+  (if (and (eq (process-status YaTeX-typeset-process) 'run)
+	   (>= (point) (process-mark YaTeX-typeset-process)))
+      (let ((b (process-mark YaTeX-typeset-process))
+	    (e (point-end-of-line)))
+	(goto-char b)
+	(skip-chars-forward " \t" e)
+	(setq b (point))
+	(process-send-string
+	 YaTeX-typeset-process (concat (buffer-substring b e) "\n"))
+	(goto-char e)
+	(insert "\n")
+	(set-marker (process-mark YaTeX-typeset-process) (point))
+	(insert " "))
+    (ding))
+)
+
+(defun YaTeX-view-error ()
+  (interactive)
+  (if (null (get-buffer YaTeX-typeset-buffer))
+      (message "No typeset buffer found.")
+    (pop-to-buffer YaTeX-typeset-buffer)
+    (goto-char (point-max))
+    (recenter -1)
+    (other-window -1))
+)
+
+(defun YaTeX-get-error-file (default)
+  "Get current processing file from typesetting log."
+  (save-excursion
+    (let(s)
+      (condition-case () (up-list -1)
+	(error
+	 (let ((list 0) found)
+	   (while
+	       (and (<= list 0) (not found)
+		    (re-search-backward "\\((\\)\\|\\()\\)" nil t))
+	     (if (equal (match-beginning 0) (match-beginning 2)) ;close paren.
+		 (setq list (1- list)) ;open paren
+	       (setq list (1+ list))
+	       (if (= list 1)
+		   (if (looking-at "\\([^,{}%]+\.\\)tex\\|sty")
+		       (setq found t)
+		     (setq list (1- list)))))))))
+      (setq s
+	    (buffer-substring
+	     (progn (forward-char 1) (point))
+	     (progn (skip-chars-forward "-A-Za-z0-9_/\.\\" (point-end-of-line))
+		    (point))))
+      (if (string= "" s) default s)))
+)
+      
+(defun YaTeX-put-nonstopmode ()
+  (if YaTeX-need-nonstop
+      (if (re-search-backward "\\nonstopmode{}" (point-min) t)
+	  nil                    ;if already written in text then do nothing
+	(save-excursion
+	  (YaTeX-visit-main t)
+	  (goto-char (point-min))
+	  (insert "\\nonstopmode{}%_YaTeX_%\n")))
+    )
+)
+
+(defun YaTeX-remove-nonstopmode ()
+  (if YaTeX-need-nonstop ;for speed
+      (save-excursion
+	(YaTeX-visit-main t)
+	(goto-char (point-min))
+	(forward-line 1)
+	(narrow-to-region (point-min) (point))
+	(goto-char (point-min))
+	(delete-matching-lines "^\\\\nonstopmode\\{\\}%_YaTeX_%$")
+	(widen)))
+)
+
+(defun YaTeX-get-preview-file-name ()
+  "Get file name to preview by inquiring YaTeX-get-latex-command"
+  (let* ((latex-cmd (YaTeX-get-latex-command t))
+	 (rin (rindex latex-cmd ? ))
+	 (fname (if (> rin -1) (substring latex-cmd (1+ rin)) ""))
+	 (period))
+    (if (string= fname "")
+	(setq fname (substring (buffer-name) 0 -4))
+      (setq period (rindex fname ?.))
+      (setq fname (substring fname 0 (if (eq -1 period) nil period)))
+      ))
+)
+
+(defun YaTeX-get-latex-command (&optional switch)
+  "Specify the latex-command name and its argument.
+If there is a line which begins by string: \"%#!\", the following
+strings are assumed to be the latex-command and arguments.  The
+default value of latex-command is:
+	tex-command (buffer-name)
+and if you write \"%#!jlatex\" in the beginning of certain line.
+	\"jlatex \" (buffer-name)
+will be the latex-command,
+and you write \"%#!jlatex main.tex\" on some line and argument SWITCH
+is t, then
+	\"jlatex main.tex\"
+will be given to the shell."
+  (let*
+      ((default-command
+	 (concat tex-command " "
+		 (if switch (buffer-name) ""))));default value
+    (save-excursion
+      (goto-char (point-min))
+      (if (null (re-search-forward "^%#!" (point-max) t))
+	  default-command
+	(skip-chars-forward "%#! 	")
+	(if (eolp)
+	    default-command
+	  (let ((s (point)))
+	    (skip-chars-forward "^ 	" (point-end-of-line)) ;Skip command
+	    (skip-chars-forward " 	" (point-end-of-line))
+	    ;(setq YaTeX-latex-command (buffer-substring s (point)))
+	    (cond
+	     ((null switch)
+	      (buffer-substring s (point)))
+	     ((eolp)			 ;Only return command name
+	      (concat (buffer-substring s (point)) " " (buffer-name)))
+	     (t(end-of-line)		   ;Change entire command name
+	       (buffer-substring s (point))) ;including arguments.
+	    ))
+	))))
+)
+
+(defun YaTeX-replace-format (string format repl)
+  "In STRING, replace first appearance of FORMAT to REPL as if
+function `format' does.  FORMAT does not contain `%'"
+  (let ((beg (or (string-match (concat "^\\(%" format "\\)") string)
+		 (string-match (concat "[^%]\\(%" format "\\)") string)))
+	(len (length format)))
+    (if (null beg) string ;no conversion
+      (concat
+       (substring string 0 (match-beginning 1)) repl
+       (substring string (match-end 1)))))
+)
+
+(defun YaTeX-lpr (arg)
+  "Print out.  If prefix arg ARG is non nil, call print driver without
+page range description."
+  (interactive "P")
+  (let*(from to (cmd (or (YaTeX-get-builtin "LPR") dviprint-command-format)))
+    (setq
+     cmd 
+     (YaTeX-replace-format
+      cmd "f"
+      (if (or arg (not (string-match "%f" cmd)))
+	      ""
+	    (YaTeX-replace-format
+	     dviprint-from-format
+	     "b"
+	     (if (string=
+		  (setq from (read-string "From page(default 1): ")) "")
+		 "1" from))))
+       )
+    (setq
+     cmd
+     (YaTeX-replace-format
+      cmd "t"
+      (if (or arg (not (string-match "%t" cmd))
+	      (string= 
+	       (setq to (read-string "To page(default none): ")) ""))
+	  ""
+	(YaTeX-replace-format dviprint-to-format "e" to)))
+     )
+    (setq cmd (read-string "Edit command line: "
+			   (format cmd (YaTeX-get-preview-file-name))))
+    (save-excursion
+      (YaTeX-visit-main t) ;;change execution directory
+      (with-output-to-temp-buffer "*dvi-printing*"
+	(if (eq system-type 'ms-dos)
+	    (call-process shell-file-name "con" "*dvi-printing*" nil
+			  "/c " cmd)
+	  (start-process "print" "*dvi-printing*" shell-file-name "-c" cmd)
+	  (message (concat "Starting " cmd " to printing "
+			   (YaTeX-get-preview-file-name))))
+    )))
+)
+
+(defun YaTeX-main-file-p ()
+  "Return if current buffer is main LaTeX source."
+  (string-match (concat "^" (YaTeX-get-preview-file-name) ".tex")(buffer-name))
+)
+
+(defun YaTeX-visit-main (&optional setbuf)
+  "Switch to buffer main LaTeX source.  Use set-buffer instead of
+switch-to-buffer if optional second argument SETBUF is t(Use it only
+in Emacs-Lisp program)."
+  (interactive)
+  (let ((main-file (YaTeX-get-preview-file-name)))
+    (if (string-match (concat "^" main-file ".tex") (buffer-name))
+	(if (interactive-p) (message "I think this is main LaTeX source.") nil)
+      (cond
+       ((YaTeX-switch-to-buffer (setq main-file (concat main-file ".tex"))
+				setbuf))
+       ((and (file-exists-p (setq main-file (concat "../" main-file)))
+	     (y-or-n-p (concat (expand-file-name main-file)
+			       " is main file?:")))
+	(YaTeX-switch-to-buffer main-file setbuf))
+       (t (find-file (read-file-name "Enter your main text: " nil nil 1)))
+	)))
+  nil
+)
+
+(defun YaTeX-visit-main-other-window ()
+  "Switch to buffer main LaTeX source in other window."
+  (interactive)
+  (if (YaTeX-main-file-p) (message "I think this is main LaTeX source.")
+      (YaTeX-switch-to-buffer-other-window
+       (concat (YaTeX-get-preview-file-name) ".tex")))
+)
+
+(defun YaTeX-get-builtin (key)
+  "Read source built-in command of %# usage."
+  (save-excursion
+    (goto-char (point-min))
+    (if (and (search-forward (concat "%#" key) nil t)
+	     (not (eolp)))
+	(buffer-substring
+	 (progn (skip-chars-forward " 	" (point-end-of-line))(point))
+	 (point-end-of-line))
+      nil))
+)
+
+(defun YaTeX-save-buffers ()
+  "Save buffers which is in yatex-mode."
+  (basic-save-buffer)
+  (save-excursion
+    (mapcar '(lambda (buf)
+	       (set-buffer buf)
+	       (if (and (buffer-file-name buf)
+			(eq major-mode 'yatex-mode)
+			(buffer-modified-p buf)
+			(y-or-n-p (format "Save %s" (buffer-name buf))))
+		   (save-buffer buf)))
+	    (buffer-list)))
+)
+
+(provide 'yatexprc)

yatex.org