changeset 5:370a391533fd

Post fj.sources.
author yuuji
date Tue, 16 Feb 1993 08:51:15 +0000
parents 78dfe28b6a35
children 49be9ccb0b65
files yatex.el yatex.new
diffstat 2 files changed, 886 insertions(+), 222 deletions(-) [+]
line wrap: on
line diff
--- a/yatex.el	Mon Nov 30 07:29:11 1992 +0000
+++ b/yatex.el	Tue Feb 16 08:51:15 1993 +0000
@@ -1,12 +1,27 @@
 ;;; -*- Emacs-Lisp -*-
 ;;; Yet Another tex-mode for emacs.
-;;; yatex.el rev.1.33
-;;; (c)1991 by Hirose Yuuji.[yuuji@ae.keio.ac.jp]
-;;; Last modified Sun Nov 29 16:04:57 1992 on 98fa
+;;; yatex.el rev.1.37
+;;; (c)1991-1993 by HIROSE Yuuji.[yuuji@ae.keio.ac.jp]
+;;; Last modified Fri Feb 12 16:05:54 1993 on VFR
+
+;; This software is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY.  No author or distributor
+;; accepts responsibility to anyone for the consequences of using it
+;; or for whether it serves any particular purpose or works at all,
+;; unless he says so in writing.  Refer to the GNU Emacs General Public
+;; License for full details.
 
-(provide 'yatex-mode)
+;; Everyone is granted permission to copy, modify and redistribute
+;; this software, but only under the conditions described in the
+;; GNU Emacs General Public License.   A copy of this license is
+;; supposed to have been given to you along with this software so you
+;; can know your rights and responsibilities.  It should be in a
+;; file named COPYING.  Among other things, the copyright notice
+;; and this notice must be preserved on all copies.
+
+(provide 'yatex)
 (require 'comment)
-(defconst YaTeX-revision-number "1.33"
+(defconst YaTeX-revision-number "1.37"
   "Revision number of running yatex.el"
 )
 
@@ -16,51 +31,84 @@
 ;; Preserving user preferred definitions.
 ;; ** Check all of these defvar-ed values **
 ;; ** and setq other values more suitable **
-;; ** for your site, if nedded.           **
+;; ** for your site, if needed.           **
 ;;;
 (defvar YaTeX-prefix "\^C"
-  "Prefix key to trigger YaTeX functions.
+  "*Prefix key to trigger YaTeX functions.
 You can select favorite prefix key by setq in your ~/.emacs."
 )
 (defvar YaTeX-open-lines 1
-  "Blank lines between text and \??{??}"
+  "*Blank lines between text and \??{??}"
 )
 (defvar YaTeX-fill-prefix ""
-  "fill-prefix used for auto-fill-mode.
-The defalut value is single TAB."
+  "*fill-prefix used for auto-fill-mode.
+The defalut value is null string."
+)
+(defvar YaTeX-fill-column 72
+  "*fill-column used for auto-fill-mode."
 )
 (defvar YaTeX-comment-prefix "%"
   "TeX comment prefix."
 )
+(defvar YaTeX-current-position-register ?3
+  "*All of YaTeX completing input store the current position into
+the register YaTeX-current-position-register.  So every time you
+make a trip to any other part of text than you writing, you can
+return to editing paragraph by calling register-to-point with argument
+YaTeX-current-position-register."
+)
 (defvar YaTeX-user-completion-table
-  (if (eq system-type 'ms-dos) "~/_yatexrc"
-    "~/.yatexrc")
-  "Default filename in which user completion table is saved."
+  (if (eq system-type 'ms-dos) "~/_yatexrc" "~/.yatexrc")
+  "*Default filename in which user completion table is saved."
 )
 (defvar tex-command "jlatex"
-  "Default command for compiling LaTeX text."
+  "*Default command for compiling LaTeX text."
 )
 (defvar dvi2-command		;previewer command for your site
-  (if (eq system-type 'ms-dos)
-      "dviout"
-    (concat
-     "xdvi -geo +0+0 -s 4 -display "
-     (getenv "DISPLAY")))
-    "Default previewer command including its option.
+  (if (eq system-type 'ms-dos) "dviout"
+    (concat "xdvi -geo +0+0 -s 4 -display " (getenv "DISPLAY")))
+  "*Default previewer command including its option.
 This default value is for X window system.  If you want to use this
 default, you have to make sure the environment variable DISPLAY is
 correctly set."
 )
+(defvar dviprint-command-format
+  (if (eq system-type 'ms-dos) "dviprt %s %f%t"
+      "dvi2ps %f %t %s | lpr")
+  "*Command line string to print out current file.  Format string %s
+will be replaced by the filename.  Do not forget to specify the
+`from usage' and `to usage' with their option by format string %f and %t.
+See also documentation of dviprint-from-format and dviprint-to-format."
+)
+(defvar dviprint-from-format
+  (if (eq system-type 'ms-dos) "%b-" "-f %b")
+  "*From page format of dvi filter.  %b will turn to beginning page number."
+)
+(defvar dviprint-to-format
+  (if (eq system-type 'ms-dos) "%e" "-t %e")
+  "*To page format of dvi filter.  %e will turn to end page number."
+)
+(defvar YaTeX-japan (or (boundp 'NEMACS) (boundp 'MULE))
+  "Whether yatex mode is running on Japanese environment or not."
+)
+(defvar YaTeX-default-document-style
+  (concat (if YaTeX-japan "j") "article")
+  "*Default LaTeX Documentstyle for YaTeX-typeset-region."
+)
+(defvar YaTeX-need-nonstop nil
+  "*If t yatex automatically put `\nonstopmode{}' in current buffer
+before invoke latex command."
+)
 (defvar latex-warning-regexp "line.* [0-9]*"
-  "Regular expression of line number of warning message by latex command."
+  "*Regular expression of line number of warning message by latex command."
 )
 (defvar latex-error-regexp "l\\.[1-9][0-9]*"
-  "Regular expression of line number of latex error.  Perhaps your latex
+  "*Regular expression of line number of latex error.  Perhaps your latex
 command stops at this error message with line number of LaTeX source text."
 )
 (defvar latex-dos-emergency-message
   "Emergency stop"      ;<- for Micro tex, ASCII-pTeX 1.6
-  "Because Demacs (GNU Emacs on DOS) cannot have pararell process, the
+  "Because Demacs (GNU Emacs on DOS) cannot have concurrent process, the
 latex command which is stopping on a LaTeX error, is terminated by Demacs.
 Many latex command on DOS display some message when it is terminated by
 other process, user or OS.  Define this variable a message string of your
@@ -68,6 +116,13 @@
   Remember Demacs's call-process function is not oriented for interactive
 process."
 )
+(defvar latex-message-kanji-code 2
+  "*Kanji coding system latex command types out.
+1 = Shift JIS, 2 = JIS, 3 = EUC."
+)
+(defvar NTT-jTeX nil
+  "*Use NTT-jTeX for latex command."
+)
 
 ;------------ Completion table ------------
 ; Set tex-section-like command possible completion
@@ -82,6 +137,7 @@
 	("nocite") ("input") ("include") ("includeonly") ("mbox") ("hbox")
 	("caption") ("newcommand") ("setlength") ("addtolength")
 	("newenvironment") ("newtheorem")
+	("cline") ("framebox")
 ))
 (defvar user-section-table nil)
 
@@ -93,6 +149,7 @@
 	("\\oddsidemargin") ("\\evensidemargin")
 	("\\textheight") ("\\topmargin")
 	("\\bottommargin") ("\\footskip") ("\\footheight")
+	("\\baselineskip") ("\\baselinestretch") ("normalbaselineskip")
 ))
 (defvar user-article-table nil)
 
@@ -100,10 +157,10 @@
 (setq env-table
       '(("quote") ("quotation") ("center") ("verse") ("document")
 	("verbatim") ("itemize") ("enumerate") ("description")
-	("list{}") ("tabular") ("table") ("titlepage")
+	("list{}") ("tabular") ("table") ("tabbing") ("titlepage")
 	("sloppypar") ("ref") ("quotation") ("quote") ("picture")
 	("eqnarray") ("figure") ("equation") ("abstract") ("array")
-	("thebibliography") ("theindex")
+	("thebibliography") ("theindex") ("flushleft") ("flushright")
 ))
 (defvar user-env-table nil)
 
@@ -117,14 +174,16 @@
 (defvar user-fontsize-table nil)
 
 (setq singlecmd-table
-      '(("maketitle") ("sloppy")
+      '(("maketitle") ("sloppy") ("protect")
 	("alpha") ("beta") ("gamma") ("delta") ("epsilon") ("varepsilon")
 	("zeta") ("eta") ("theta")("vartheta") ("iota") ("kappa")
 	("lambda") ("mu") ("nu") ("xi") ("pi") ("varpi") ("rho") ("varrho")
 	("sigma") ("varsigma") ("tau") ("upsilon") ("phi") ("varphi")
 	("chi") ("psi") ("omega") ("Gamma") ("Delta") ("Theta") ("Lambda")
 	("Xi") ("Pi") ("Sigma") ("Upsilon") ("Phi") ("Psi") ("Omega")
-	("LaTeX") ("TeX") ("item[]")
+	("LaTeX") ("TeX") ("item[]") ("appendix") ("hline")
+	("rightarrow") ("Rightarrow") ("leftarrow") ("Leftarrow")
+	("pagebreak")
 ))
 (defvar user-singlecmd-table nil)
 
@@ -134,7 +193,7 @@
 ;; Do not change this section.
 ;;;
 (defvar YaTeX-inhibit-prefix-letter nil
-  "Switch which determin whether inhibit yatex.el from defining
+  "*Switch which determins whether inhibit yatex.el from defining
 key sequence \"C-c letter\" or not."
 )
 (defvar YaTeX-mode-map nil
@@ -178,15 +237,22 @@
   (setq YaTeX-mode-map (make-sparse-keymap))
   (setq YaTeX-prefix-map (make-sparse-keymap))
   (define-key YaTeX-mode-map "\"" 'YaTeX-insert-quote)
+  (define-key YaTeX-mode-map "{" 'YaTeX-insert-braces)
+  (define-key YaTeX-mode-map "(" '(lambda () (interactive)
+				    (insert "()") (backward-char 1)))
+  ;(define-key YaTeX-mode-map "[" 'YaTeX-insert-brackets)
   (define-key YaTeX-mode-map YaTeX-prefix YaTeX-prefix-map)
   (YaTeX-define-key "t" 'YaTeX-typeset-menu)
-  (define-key YaTeX-prefix-map "'" 'YaTeX-prev-error)
-  (define-key YaTeX-prefix-map " " 'YaTeX-do-completion)
+  (YaTeX-define-key "'" 'YaTeX-prev-error)
+  (YaTeX-define-key "^" 'YaTeX-visit-main)
+  (YaTeX-define-key "4^" 'YaTeX-visit-main-other-window)
+  (YaTeX-define-key " " 'YaTeX-do-completion)
   (YaTeX-define-key "v" 'YaTeX-version)
 
-  (define-key YaTeX-prefix-map "{" 'YaTeX-insert-braces)
-  (define-key YaTeX-prefix-map "}" 'YaTeX-insert-braces-region)
+  (YaTeX-define-key "}" 'YaTeX-insert-braces-region)
+  (YaTeX-define-key "]" 'YaTeX-insert-brackets-region)
   (YaTeX-define-key "d" 'YaTeX-insert-dollar)
+  (YaTeX-define-key "i" 'YaTeX-fill-item)
   (YaTeX-define-key
    "\\" '(lambda () (interactive) (YaTeX-insert-string "$\\backslash$")))
   (YaTeX-define-begend-region-key "Bd" "document")
@@ -216,11 +282,14 @@
   (YaTeX-define-key "L" 'YaTeX-make-fontsize-region)
   (YaTeX-define-key "l" 'YaTeX-make-fontsize)
   (YaTeX-define-key "m" 'YaTeX-make-singlecmd)
-  (YaTeX-define-key "g" 'YaTeX-goto-corresponding-environment)
-  (YaTeX-define-key "\C-m"
+  (YaTeX-define-key "g" 'YaTeX-goto-corresponding-*)
+  (YaTeX-define-key "k" 'YaTeX-kill-*)
+  (YaTeX-define-key "c" 'YaTeX-change-*)
+  (YaTeX-define-key "a" 'YaTeX-make-accent)
+  (YaTeX-define-key "n"
     '(lambda () (interactive) (YaTeX-insert-string "\\\\")))
   (if (eq system-type 'ms-dos)
-      (define-key YaTeX-prefix-map "\^L"
+      (define-key YaTeX-prefix-map "L"
 	'(lambda () (interactive)
 	   (set-screen-height YaTeX-saved-screen-height) (recenter))))
 )
@@ -240,15 +309,31 @@
 (defvar fontsize-name "large")		;Initial fontsize completion
 (defvar single-command "maketitle")	;Initial LaTeX single command
 (defvar YaTeX-user-table-has-read nil
-  "Flag that means whether user completion table has read or not."
+  "Flag that means whether user completion table has been read or not."
 )
 (defvar YaTeX-user-table-modified nil
   "Flag that means whether user completion table has modified or not."
 )
 (defvar yatex-mode-hook nil
-  "List of functions to be called after .tex file is read
-and yatex-mode starts.")
-
+  "*List of functions to be called after .tex file is read
+and yatex-mode starts."
+)
+(defvar YaTeX-kanji-code-alist
+  '((1 . *sjis*) (2 . *junet*) (3 . *euc-japan*))
+)
+(defvar YaTeX-kanji-code (if (eq system-type 'ms-dos) 1 2)
+  "*File kanji code used by Japanese TeX."
+)
+(cond
+ ((boundp 'MULE)
+  (defvar YaTeX-coding-system
+    (symbol-value (cdr (assoc YaTeX-kanji-code YaTeX-kanji-code-alist)))
+    "File coding system used by Japanese TeX")
+  (if (not (eq system-type 'ms-dos))
+      (defvar YaTeX-latex-message-code *autoconv*)))
+ ((boundp 'NEMACS)
+  (defvar YaTeX-latex-message-code latex-message-kanji-code))
+)
 ;---------- Produce YaTeX-mode ----------
 ;;;
 ;; Major mode definition
@@ -257,19 +342,17 @@
   (interactive)
   (kill-all-local-variables)
   (setq major-mode 'YaTeX-mode)
-  (setq mode-name "やてふもーど")
-  (turn-on-auto-fill)
+  (setq mode-name (if YaTeX-japan "やてふ" "YaTeX"))
   (make-local-variable 'dvi2-command)
-  (make-local-variable 'kanji-display-code)
-  (make-local-variable 'kanji-fileio-code)
-  (if (eq system-type 'ms-dos)
-      (setq YaTeX-kanji-code 1)
-    (defvar YaTeX-kanji-code 2))
-  (setq kanji-display-code YaTeX-kanji-code
-	kanji-fileio-code  YaTeX-kanji-code)
+  (cond ((boundp 'MULE) ;;1992/12/21 by NIIMI Satoshi.
+	 ;; file-coding-system is buffer local variable
+	 (set-file-coding-system  YaTeX-coding-system))
+  	((boundp 'NEMACS)
+	 (make-local-variable 'kanji-fileio-code)
+	 (setq kanji-fileio-code YaTeX-kanji-code)))
   (make-local-variable 'fill-column)
   (make-local-variable 'fill-prefix)
-  (setq fill-column 72
+  (setq fill-column YaTeX-fill-column
 	fill-prefix YaTeX-fill-prefix)
   (use-local-map YaTeX-mode-map)
   (if (eq system-type 'ms-dos)
@@ -277,7 +360,16 @@
   (if YaTeX-user-table-has-read nil
     (YaTeX-read-user-completion-table)
     (setq YaTeX-user-table-has-read t))
-  (run-hooks 'yatex-mode-hook)
+  (run-hooks 'text-mode-hook 'yatex-mode-hook)
+)
+
+;---------- Define macro ---------
+(defmacro point-beginning-of-line ()
+  (save-excursion (beginning-of-line)(point))
+)
+
+(defmacro point-end-of-line ()
+  (save-excursion (end-of-line)(point))
 )
 
 ;---------- Define YaTeX-mode functions ----------
@@ -289,29 +381,42 @@
   (if arg
       (save-excursion
 	(if (> (point) (mark)) (exchange-point-and-mark))
-	(insert "\\begin{" env "}\n")
+	(insert "\\begin{" env "}")
+	(YaTeX-addin env)
+	(insert "\n")
+	(indent-relative-maybe)
 	(exchange-point-and-mark)
-	(insert "\\end{" env "}\n"))
-    (delete-blank-lines)
-    (insert "\\begin{" env "}\n")
-    (newline (1+ (* 2 YaTeX-open-lines)))
-    (insert "\\end{" env "}\n")
-    (previous-line (+ 2 YaTeX-open-lines)))
+	(insert "\\end{" env "}\n")
+	(indent-relative-maybe))
+    ;(delete-blank-lines)
+    (let ((i 1))
+      (insert "\\begin{" env "}")
+      (YaTeX-addin env)
+      (insert "\n")
+      (indent-relative-maybe)
+      ;;(newline (1+ (* 2 YaTeX-open-lines)))
+      (while (<= i (1+ (* 2 YaTeX-open-lines)))
+	(insert "\n")
+	(indent-relative-maybe)
+	(setq i (1+ i)))
+      (insert "\\end{" env "}")
+      (previous-line (+ 1 YaTeX-open-lines)));let i
+    (if YaTeX-current-position-register
+	(point-to-register YaTeX-current-position-register)))
 )
 
 (defun YaTeX-make-begin-end (arg)
   "Make LaTeX environment command of \\begin{env.} ... \\end{env.}
 by completing read.
  If you invoke this command with universal argument,
-\(C-u or ESC-1 is typical prefix to invoke commands with ARG.\)
+\(key binding for universal-argument is \\[universal-argument]\)
 you can put REGION into that environment between \\begin and \\end."
   (interactive "P")
   (let*
       ((mode (if arg " region" ""))
        (env
-	(completing-read
-	 (format "Begin environment%s(default %s): " mode env-name)
-	 (append user-env-table env-table) nil nil)))
+	(YaTeX-read-environment
+	 (format "Begin environment%s(default %s): " mode env-name))))
     (if (string= env "")
 	(setq env env-name))
     (setq env-name env)
@@ -327,12 +432,10 @@
   (YaTeX-make-begin-end t)
 )
 
-(defun YaTeX-end-environment ()
-  "Close opening environment"
-  (interactive)
-  (let ((curp (point))
-	s env (nest 0))
-    (save-excursion
+(defun YaTeX-inner-environment ()
+  "Return current inner environment."
+  (save-excursion
+    (let ((nest 0) s)
       (while
 	  (and
 	   (>= nest 0)
@@ -343,14 +446,21 @@
 	  (setq nest (if (eq (match-beginning 0) (match-beginning 1))
 			 (1- nest) (1+ nest)))))
       (if (>= nest 0)
-	  (message "No premature environment")
+	  nil
 	(goto-char (match-end 1))
 	(setq s (point))
 	(skip-chars-forward "^}")
-	(setq env (buffer-substring s (point)))
-	;;(recursive-edit)
-	))
-    (if (not env) nil
+	(buffer-substring s (point))
+      )))
+)
+
+(defun YaTeX-end-environment ()
+  "Close opening environment"
+  (interactive)
+  (let ((curp (point))
+	(env (YaTeX-inner-environment)))
+
+    (if (not env) (error "No premature environment")
       (save-excursion
 	(if (and (re-search-forward "^[^\\%]*\\\\end{.*}" (point-max) t)
 		 (progn (goto-char (match-beginning 0))
@@ -363,12 +473,12 @@
       (message "")			;Erase (y or n) message.
       (insert "\\end{" env "}")
       (setq curp (point))
-      (goto-char s)
+      (goto-char (match-end 0))
       (if (pos-visible-in-window-p)
 	  (sit-for 1)
 	(message (concat "Matches \\begin{" env
 			 (format "} at line %d"
-				   (count-lines (point-min) s)))))
+				   (count-lines (point-min) (match-end 0))))))
       (goto-char curp))
     )
 )
@@ -381,7 +491,7 @@
 
 	\\addtolength{\\topmargin}{8mm}
 
-which has two argument.  You can produce that sequence by typing...
+which has two arguments.  You can produce that sequence by typing...
 	ESC 2 C-c s add SPC RET \\topm SPC RET 8mm RET
 \(by default\)
 You can complete symbol at LaTeX command and 1st argument."
@@ -403,6 +513,8 @@
 	      (cons (list section-name) user-section-table)
 	      YaTeX-user-table-modified t))
     (insert "\\" section-name "{" title "}")
+    (if YaTeX-current-position-register
+	(point-to-register YaTeX-current-position-register))
     (let ((j 2))
       (while (<= j arg)
 	(insert (concat "{" (read-string (format "Argument %d: " j))))
@@ -423,7 +535,7 @@
   "Make completion like {\\large ...} or {\\slant ...} in minibuffer.
 If you invoke this command with universal argument, you can put region
 into {\\xxx } braces.
-\(C-u or ESC-1 are default key bindings of universal-argument.\)"
+\(key binding for universal-argument is \\[universal-argument]\)"
   (interactive "P")
   (let* ((mode (if arg "region" ""))
 	 (fontsize
@@ -445,11 +557,13 @@
 	  (exchange-point-and-mark)
 	  (insert "}"))
       (insert "{\\" fontsize-name " }")
+      (if YaTeX-current-position-register
+	  (point-to-register YaTeX-current-position-register))
       (forward-char -1)))
 )
 
 (defun YaTeX-make-fontsize-region ()
-  "Call functino:YaTeX-make-fontsize with ARG to specify region mode."
+  "Call function:YaTeX-make-fontsize with ARG to specify region mode."
   (interactive)
   (YaTeX-make-fontsize t)
 )
@@ -469,6 +583,8 @@
 	    (cons (list single-command) user-singlecmd-table)
 	    YaTeX-user-table-modified t))
   (insert "\\" single-command " ")
+  (if YaTeX-current-position-register
+      (point-to-register YaTeX-current-position-register))
 )
 
 (defvar YaTeX-completion-begin-regexp "[{\\]"
@@ -537,6 +653,7 @@
   (interactive)
   (insert
    (cond
+    ((YaTeX-quick-in-environment-p "verbatim") ?\")
     ((= (preceding-char) ?\\ ) ?\")
     ((= (preceding-char) ?\( ) ?\")
     ((= (preceding-char) 32)  "``")
@@ -546,14 +663,13 @@
     (t  "''")
 )))
 
-
-(defun YaTeX-insert-braces-region (beg end)
+(defun YaTeX-insert-braces-region (beg end &optional open close)
   (interactive "r")
   (save-excursion
     (goto-char end)
-    (insert "}")
+    (insert (or close "}"))
     (goto-char beg)
-    (insert "{"))
+    (insert (or open "{")))
 )
 
 (defun YaTeX-insert-braces ()
@@ -562,6 +678,12 @@
   (forward-char -1)
 )
 
+(defun YaTeX-insert-brackets-region (beg end)
+  (interactive "r")
+  (save-excursion
+    (YaTeX-insert-braces-region beg end "[" "]"))
+)
+
 (defun YaTeX-insert-dollar ()
   (interactive)
   (insert "$$")
@@ -576,7 +698,9 @@
   "Return string of the version of running YaTeX."
   (interactive)
   (message
-   (concat "Yet Another TeX mode 「野鳥」 Revision "
+   (concat "Yet Another tex-mode "
+	   (if YaTeX-japan "「野鳥」" "Wild Bird")
+	   "Revision "
 	   YaTeX-revision-number))
 )
 
@@ -591,9 +715,16 @@
            (unwind-protect
                (progn
                  ;; Write something in *typesetting* and hack its mode line
-                 (set-buffer (process-buffer proc))
+		 (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 "jlatex typesetting " mes)
+                 (insert ?\n "latex typesetting " mes)
                  (forward-char -1)
                  (insert " at "
                          (substring (current-time-string) 0 -5))
@@ -602,6 +733,7 @@
                  (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.
@@ -618,50 +750,143 @@
 (defvar YaTeX-typesetting-process nil
   "Process identifier for jlatex"
 )
+(defvar YaTeX-typeset-buffer "*YaTeX-typesetting*"
+  "Process buffer for jlatex")
 
-(defun YaTeX-typeset ()
+(defun YaTeX-typeset (command)
   "Execute jlatex (or other) to LaTeX typeset."
   (interactive)
   (if YaTeX-typesetting-process
    (if (eq (process-status YaTeX-typesetting-process) 'run)
 	(progn (interrupt-process YaTeX-typesetting-process)
-	       (sit-for 1)
+	       ;(sit-for 1)
 	       (delete-process YaTeX-typesetting-process))
       nil) nil)
-;  (compile1 (concat tex-command " " (buffer-name))
-;	    "TeX error" "*TeX typesetting*")
   (setq YaTeX-typesetting-process nil)
   (if (eq system-type 'ms-dos)				;if MS-DOS
-      (with-output-to-temp-buffer "*YaTeX-typesetting*"
-	(message (concat "Compiling " (buffer-name) "..."))
+      (with-output-to-temp-buffer YaTeX-typeset-buffer
+	(message (concat "Typesetting " (buffer-name) "..."))
 	(YaTeX-put-nonstopmode)
-	(basic-save-buffer)
 	(call-process shell-file-name
-		      nil
-		      "*YaTeX-typesetting*" nil
-		      "/c" (YaTeX-get-latex-command))
+		      nil YaTeX-typeset-buffer nil "/c" command)
 	(YaTeX-remove-nonstopmode))
     (setq YaTeX-typesetting-process			;if UNIX
-	  (with-output-to-temp-buffer "*YaTeX-typesetting*"
-	    (basic-save-buffer)
-	    (start-process "LaTeX" "*YaTeX-typesetting*" shell-file-name "-c"
-			   (YaTeX-get-latex-command))
+	  (with-output-to-temp-buffer YaTeX-typeset-buffer
+	    (start-process "LaTeX" YaTeX-typeset-buffer shell-file-name "-c"
+			   command)
 	    ))
     (set-process-sentinel YaTeX-typesetting-process 'YaTeX-typeset-sentinel))
   (setq current-TeX-buffer (buffer-name))
   (other-window 1)
   (use-local-map YaTeX-typesetting-mode-map)
-  (set-kanji-process-code YaTeX-kanji-code)
+  (setq mode-name "typeset")
+  (if YaTeX-typesetting-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))
-  (sit-for 30)
-  (read-char)	;hit any key
-  (if (not (= (window-start) (point-min)))
-      (while (eq (point) (point-max))
-	(scroll-down 1)))
+  (sit-for 1)
+  (if (eq system-type 'ms-dos) (message "") (read-char));hit any key
+  (forward-line -1)
+  (recenter -1)
   (other-window -1)
 )
 
+(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.
+	 (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)
+      (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)
+      (switch-to-buffer buffer)		;for clarity
+      (find-file "texput.tex")
+      (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-typeset (concat (YaTeX-get-latex-command nil) " texput.tex"))))
+)
+
+(defun YaTeX-typeset-buffer ()
+  "Typeset whole buffer."
+  (interactive)
+  (if (YaTeX-main-file-p) nil
+    (let*((me (substring (buffer-name) 0 (rindex (buffer-name) ?.))))
+      (save-excursion		;save excursion of current buffer.
+	(YaTeX-visit-main)
+	(save-excursion		;save excursion of main .tex buffer
+	  (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)
+		(if (string-match (concat "[{,]" me "[,}]") s)
+		    nil
+		  (ding)
+		  (message
+  "File:`%s' is not in includeonly list. A)ppend 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-save-buffers)
+  (YaTeX-typeset (YaTeX-get-latex-command t))
+)
+
 (defun YaTeX-preview (preview-command preview-file)
   "Execute xdvi (or other) to tex-preview."
   (interactive
@@ -687,54 +912,58 @@
   "Visit previous error.  The reason why not NEXT-error is to
 avoid make confliction of line numbers by editing."
   (interactive)
-  (setq cur-buf (buffer-name)
-	YaTeX-error-line nil)
-  (if (null (get-buffer "*YaTeX-typesetting*"))
-      (message "There is no output buffer of typesetting.")
-    (pop-to-buffer "*YaTeX-typesetting*")
-    (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))
+  (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-typesetting-process      ; if jlatex on UNIX
+	    (if (eq (process-status YaTeX-typesetting-process) 'run)
+		(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-typesetting-process      ; if jlatex on UNIX
-	  (if (eq (process-status YaTeX-typesetting-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)
-      )
-    (other-window -1)
-    (switch-to-buffer cur-buf)
-    (if (null YaTeX-error-line)
-	nil
-      (goto-line YaTeX-error-line)
-      (message "latex error or warning at line: %d" YaTeX-error-line)
-      (other-window 1)
-      (skip-chars-backward "[0-9]")
-      (recenter (/ (window-height) 2))
-      (sit-for 3)
-      (forward-line -1)
+	  (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-line -1)
+	(other-window -1)
+	)))
 )
 
 (defun YaTeX-jump-error-line ()
@@ -747,23 +976,40 @@
       (goto-char (match-beginning 0))
       (re-search-forward "[1-9][0-9]*" end t)
       (save-restriction
-	(narrow-to-region (match-beginning 0) (match-end 0))
-	(goto-char (point-min))
-	(let ((error-line (read (current-buffer))))
+	(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)
-	  (switch-to-buffer current-TeX-buffer)
+	  (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-view-error ()
   (interactive)
-  (other-window 1)
-  (goto-char (point-max))
-  (other-window -1)
+  (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 by tex message."
+  (let (file-name)
+    (save-excursion
+      (if (re-search-backward "([-A-Z_a-z0-9]+.tex" (point-min) t)
+	  (buffer-substring (1+ (match-beginning 0)) (match-end 0))
+	default)))
+)
+      
 (defun YaTeX-put-nonstopmode ()
-  (if (boundp 'YaTeX-need-nonstop)
+  (if YaTeX-need-nonstop
       (if (re-search-backward "\\nonstopmode{}" (point-min) t)
 	  nil                    ;if already written in text then do nothing
 	(save-excursion
@@ -773,7 +1019,7 @@
 )
 
 (defun YaTeX-remove-nonstopmode ()
-  (if (boundp 'YaTeX-need-nonstop) ;for speed
+  (if YaTeX-need-nonstop ;for speed
       (save-excursion
 	(goto-char (point-min))
 	(forward-line 1)
@@ -783,21 +1029,23 @@
 	(widen)))
 )
 
-(defun YaTeX-typeset-menu ()
+(defun YaTeX-typeset-menu (arg)
   "Typeset, preview, visit error and miscellaneous convinient menu."
-  (interactive)
-  (message "J)latex P)review V)iewerror")
+  (interactive "P")
+  (message "J)latex R)egion P)review V)iewerror L)pr")
   (let ((c (read-char)))
     (cond
-     ((= c ?j) (YaTeX-typeset))
+     ((= c ?j) (YaTeX-typeset-buffer))
+     ((= c ?r) (YaTeX-typeset-region))
      ((= c ?p) (call-interactively 'YaTeX-preview))
      ((= c ?v) (YaTeX-view-error))
+     ((= c ?l) (YaTeX-lpr arg))
      ((= c ?b) (YaTeX-insert-string "\\"))))
 )
 
 (defun YaTeX-get-preview-file-name ()
   "Get file name to preview by inquiring YaTeX-get-latex-command"
-  (let* ((latex-cmd (YaTeX-get-latex-command))
+  (let* ((latex-cmd (YaTeX-get-latex-command t))
 	 (fname (substring latex-cmd (1+ (rindex latex-cmd ? ))))
 	 (period))
     (if (eq fname "")
@@ -807,7 +1055,7 @@
       ))
 )
 
-(defun YaTeX-get-latex-command ()
+(defun YaTeX-get-latex-command (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
@@ -816,69 +1064,199 @@
 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\"
+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 " " (buffer-name)))) ;default value
+	 (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)1z
+	(if (eolp)
 	    default-command
 	  (let ((s (point)))
 	    (skip-chars-forward "A-z")	;Skip command name
 	    ;(setq YaTeX-latex-command (buffer-substring s (point)))
-	    (if (eolp)			;Only change command name
-		(concat (buffer-substring s (point)) " " (buffer-name))
-	      (end-of-line)		   ;Change entire command name
-	      (buffer-substring s (point)) ;including arguments.
+	    (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-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-goto-corresponding-environment ()
   "Go to corresponding begin/end enclosure."
   (interactive)
-  (if (not (YaTeX-on-begin-end-p))
-      (error "No environment declaration"))
-  (let ((p  (match-end 0) env)
-	(m0 (match-beginning 0))
-	(m1 (match-beginning 1))
-	(m2 (match-beginning 2)))
-    (if (not
-	 (save-excursion
-	   (goto-char p)
-	   (search-forward "}" (point-end-of-line) t)))
-	(error "Unterminated brackets for begin/end"))
-    (setq env (buffer-substring p (match-beginning 0))) ;get current env
-    (cond
-     ((equal m0 m1)		;if begin{xxx}
-      (search-forward (concat "end{" env "}")))
-     ((equal m0 m2)		;if end{xxx}
-      (search-backward (concat "begin{" env "}")))
-     )
+  (if (not (YaTeX-on-begin-end-p)) nil
+    (let ((p  (match-end 0)) env (nest 0) regexp re-s
+	  (m0 (match-beginning 0))
+	  (m1 (match-beginning 1))
+	  (m2 (match-beginning 2)))
+      (if (not
+	   (save-excursion
+	     (goto-char p)
+	     (search-forward "}" (point-end-of-line) t)))
+	  (error "Unterminated brackets for begin/end"))
+      (setq env (buffer-substring p (match-beginning 0))) ;get current env
+      (if (cond
+	   ((equal m0 m1)		;if begin{xxx}
+	    (setq regexp (concat "\\(\\\\end{" env "}\\)\\|"
+				 "\\(\\\\begin{" env "}\\)"))
+	    (fset re-s 're-search-forward))
+	   ((equal m0 m2)		;if end{xxx}
+	    (setq regexp (concat "\\(\\\\begin{" env "}\\)\\|"
+				 "\\(\\\\end{" env "}\\)"))
+	    (fset re-s 're-search-backward))
+	   (error "Corresponding environment not found."))
+	(while (and (>= nest 0) (funcall re-s regexp nil t))
+	  (if (eq (match-beginning 0) m0) nil
+	    (setq nest (if (eq (match-beginning 0) (match-beginning 1))
+			   (1- nest) (1+ nest))))))
+      (beginning-of-line));let
+    t); if on begin/end line
+)
+
+(defun YaTeX-goto-corresponding-file ()
+  "Visit or switch buffer of corresponding file, looking at \\input or
+\\include or \includeonly on current line."
+  (if (not (YaTeX-on-includes-p)) nil
     (beginning-of-line)
-    );let
+    (skip-chars-forward "^{")
+    (let ((input-file
+	   (concat
+	    (buffer-substring (1+ (point))
+			      (progn (skip-chars-forward "^ ,}") (point)))
+	    ".tex")))
+      (YaTeX-switch-to-buffer input-file)
+      )
+    t);if on \input or \include line.
+)
+
+(defun YaTeX-goto-corresponding-BEGIN-END ()
+  (if (not (YaTeX-on-BEGIN-END-p)) nil
+    (if (cond
+	 ((equal (match-beginning 0) (match-beginning 1)) ;if on %#BEGIN
+	  (not (search-forward "%#END" nil t)))
+	 (t ; if on %#END
+	  (not (search-backward "%#BEGIN" nil t))))
+	(error "Corresponding %#BEGIN/END not found."))
+    (beginning-of-line)
+    t)
+)
+
+(defun YaTeX-switch-to-buffer (file)
+  "Switch to buffer if buffer exists, find file if not."
+  (interactive "Fswitch to file: ")
+  (if (get-buffer file)
+      (progn (switch-to-buffer file) t)
+    (if (file-exists-p file)
+	(progn (find-file file) t)
+      (message "%s was not found in this directory." file)
+      nil))
 )
 
-(defun YaTeX-comment-region ()
+(defun YaTeX-switch-to-buffer-other-window (file)
+  "Switch to buffer if buffer exists, find file if not."
+  (interactive "Fswitch to file: ")
+  (if (get-buffer file)
+      (progn (switch-to-buffer-other-window file) t)
+    (if (file-exists-p file)
+	(progn (find-file-other-window file) t)
+      (message "%s was not found in this directory." file)
+      nil))
+)
+
+(defmacro 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 ()
+  "Switch to buffer main LaTeX source."
+  (interactive)
+  (let ((main-file (YaTeX-get-preview-file-name)))
+    (if (string-match (concat "^" main-file ".tex") (buffer-name))
+	(message "I think this is main LaTeX source.")
+      (YaTeX-switch-to-buffer (concat main-file ".tex"))))
+  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-on-begin-end-p ()
+  (save-excursion
+    (beginning-of-line)
+    (re-search-forward
+     "\\(\\\\begin{\\)\\|\\(\\\\end{\\)" (point-end-of-line) t))
+)
+(defun YaTeX-on-includes-p ()
+  (save-excursion
+    (beginning-of-line)
+    (re-search-forward "\\(\\(include.*\\)\\|\\(input\\)\\){.*}"
+		       (point-end-of-line) t))
+)
+(defun YaTeX-on-BEGIN-END-p ()
+  (save-excursion
+    (let ((case-fold-sea nil))
+      (beginning-of-line)
+      (re-search-forward "\\(%#BEGIN\\)\\|\\(%#END\\)" (point-end-of-line) t)))
+)
+(defun YaTeX-goto-corresponding-* ()
+  "Parse current line and call suitable function."
+  (interactive)
+  (cond
+   ((YaTeX-goto-corresponding-environment))
+   ((YaTeX-goto-corresponding-file))
+   ((YaTeX-goto-corresponding-BEGIN-END))
+   (t (message "I don't know where to go.")))
+)
+
+(defun YaTeX-comment-region (alt-prefix)
   "Comment out region by '%'. If you call this function on the 'begin{}' or
 'end{}' line, it comments out whole environment"
-  (interactive)
+  (interactive "P")
   (if (not (YaTeX-on-begin-end-p))
-      (comment-region YaTeX-comment-prefix)
+      (comment-region
+       (if alt-prefix
+	   (read-string "Insert prefix: ")
+	 YaTeX-comment-prefix))
     (YaTeX-comment-uncomment-env 'comment-region))
 )
 
-(defun YaTeX-uncomment-region ()
+(defun YaTeX-uncomment-region (alt-prefix)
   "Uncomment out region by '%'."
-  (interactive)
+  (interactive "P")
   (if (not (YaTeX-on-begin-end-p))
-      (uncomment-region YaTeX-comment-prefix)
+      (uncomment-region
+       (if alt-prefix (read-string "Remove prefix: ")
+	 YaTeX-comment-prefix))
     (YaTeX-comment-uncomment-env 'uncomment-region))
 )
 
@@ -888,11 +1266,10 @@
     (if (eq (match-beginning 0) (match-beginning 2)) ; if on the '\end{}' line
 	(YaTeX-goto-corresponding-environment)) ; goto '\begin{}' line
     (beginning-of-line)
-    (push-mark)
+    (push-mark (point) t)
     (YaTeX-goto-corresponding-environment)
     (forward-line 1)
     (funcall func YaTeX-comment-prefix t) ; t makes uncomment once
-    (pop-mark)
     )
 )
 
@@ -900,12 +1277,6 @@
   "Not implemented yet."
 )
 
-(defun YaTeX-on-begin-end-p ()
-  (save-excursion
-    (beginning-of-line)
-    (re-search-forward "\\(begin{\\)\\|\\(end{\\)" (point-end-of-line) t))
-)
-
 (defun YaTeX-comment-paragraph ()
   "Comment out current paragraph."
   (interactive)
@@ -949,6 +1320,259 @@
     (if once (end-of-line)))
 )
 
+(defun YaTeX-kill-some-pairs (predicate gofunc)
+  "Kill some matching pair."
+  (interactive)
+  (if ;(not (YaTeX-on-begin-end-p)) nil
+      (not (funcall predicate)) nil
+    (save-excursion
+      (push-mark (point) t)
+      ;(YaTeX-goto-corresponding-environment)
+      (funcall gofunc)
+      (beginning-of-line)
+      (kill-line 1)
+      (exchange-point-and-mark)
+      (beginning-of-line)
+      (kill-line 1)
+    t))
+)
+
+(defun YaTeX-read-environment (prompt)
+  "Read the LaTeX environment name with completion."
+  (let ((env
+	 (completing-read prompt (append user-env-table env-table) nil nil)))
+    (if (not (assoc env (append user-env-table env-table)))
+	(setq user-env-table (cons (list env) user-env-table)
+	      YaTeX-user-table-modified t))
+  env)
+)
+
+(defun YaTeX-change-environment ()
+  "Change the name of environment."
+  (interactive)
+  (if (not (YaTeX-on-begin-end-p)) nil
+    (save-excursion
+      (let (p env)
+	(beginning-of-line)
+	(skip-chars-forward "^{")
+	(forward-char 1)
+	(setq p (point))
+	(skip-chars-forward "^}")
+	(setq env (buffer-substring p (point)))
+	(beginning-of-line)
+	(set-mark-command nil)
+	(YaTeX-goto-corresponding-environment)
+	(setq newenv (YaTeX-read-environment
+		      (format "Change environment `%s' to: " env)))
+	(if (string= newenv "")
+	    (message "Change environment cancelled.")
+	  (search-forward (concat "{" env) (point-end-of-line) t)
+	  (replace-match (concat "{" newenv))
+	  (exchange-point-and-mark)
+	  (search-forward (concat "{" env) (point-end-of-line) t)
+	  (replace-match (concat "{" newenv)))
+	t)))
+)
+
+(defun YaTeX-kill-* ()
+  "Parse current line and call suitable function."
+  (interactive)
+  (cond
+   ((YaTeX-kill-some-pairs 'YaTeX-on-begin-end-p
+			   'YaTeX-goto-corresponding-environment))
+   ((YaTeX-kill-some-pairs 'YaTeX-on-BEGIN-END-p
+			   'YaTeX-goto-corresponding-BEGIN-END))
+   (t (message "I don't know what to kill.")))
+)
+
+(defun YaTeX-change-* ()
+  "Parse current line and call suitable function."
+  (interactive)
+  (cond
+   ((YaTeX-change-environment))
+   (t (message "I don't know what to change.")))
+)
+
+(defun YaTeX-addin (name)
+  "Check availability of addin function and call it if exists."
+  (if (fboundp (intern-soft (concat "YaTeX:" name)))
+      (funcall (intern (concat "YaTeX:" name))))
+)
+
+(defun YaTeX-in-environment-p (env)
+  "Return if current LaTeX environment is ENV."
+  (let ((cur-env (YaTeX-inner-environment)) p)
+    (cond
+     ((atom env) (equal env cur-env))
+     ((listp env)
+      (while (and env (not p))
+	(setq p (equal (car env) cur-env))
+	(setq env (cdr env)))
+      p)))
+)
+
+(defun YaTeX-quick-in-environment-p (env)
+  "Check quickly but unsure if current environment is ENV."
+  (let ((p (point))q)
+    (while (and (not q) (search-backward (concat "\\begin{" env "}")nil t))
+      ;;(goto-char (match-beginning 0))
+      (if (search-backward "%" (point-beginning-of-line) t) nil
+	(setq q t)))
+    (if q (setq q (not (re-search-forward
+			(concat "^[ 	]*\\\\end{" env "}") p t))))
+    (goto-char p)
+    q)
+)
+
+(defun YaTeX-remove-trailing-comment ()
+  "Remove trailing comment in current line."
+  (if (re-search-forward "[^\\\\]\\(%\\)" (point-end-of-line) t)
+      (delete-region (match-beginning 1) (point-end-of-line)))
+)
+
+(defun YaTeX-fill-item ()
+  "Fill item in itemize environment."
+  (interactive)
+  (save-excursion
+      (let* ((p (point))
+	     (bndry (prog2 (search-backward "\\begin{" nil t) (point)
+			   (goto-char p)))
+	     (item-term "\\(^$\\)\\|\\(\\\\item\\)\\|\\(\\\\end\\)")
+	     fill-prefix start s2 col)
+	(end-of-line)
+	(if (not (re-search-backward "\\\\item" bndry t))
+	    (error "\\item not found."))
+	(skip-chars-forward "^ 	" (point-end-of-line))
+	(skip-chars-forward " 	" (point-end-of-line))
+	(if (not (eolp))  nil
+	  (forward-line 1)
+	  (skip-chars-forward "	 "))
+	(setq start (point-beginning-of-line))
+	(setq col (current-column))
+	(YaTeX-remove-trailing-comment)	;should restrict to NTT-jTeX?
+	(forward-line 1)
+	(skip-chars-forward " 	")
+	(if (looking-at item-term) nil
+	  (delete-region (point) (point-beginning-of-line))
+	  (indent-to col)
+	  (setq s2 (point))
+	  (setq fill-prefix
+		(buffer-substring (point-beginning-of-line)(point)))
+	  (YaTeX-remove-trailing-comment);should restrict to NTT-jTeX?
+	  (re-search-forward item-term nil 1)
+	  (beginning-of-line)
+	  (push-mark (point) t)
+	  (while (> (point) s2)
+	    (forward-line -1)
+	    (skip-chars-forward "	 ")
+	    (delete-region (point) (point-beginning-of-line))
+	    (YaTeX-remove-trailing-comment))
+	  (fill-region-as-paragraph start (mark))
+	  (if NTT-jTeX
+	      (while (progn(forward-line -1)(end-of-line) (> (point) start))
+		(insert ?%)))
+	  (pop-mark))
+	))
+)
+
+(defun YaTeX-fill-* ()
+  "Fill paragraph according to its condition."
+  (interactive)
+  (cond
+   ((YaTeX-fill-item))
+   )
+)
+
+(defun YaTeX-save-buffers ()
+  "Save buffers with `.tex' extension."
+  (basic-save-buffer)
+  (save-excursion
+    (mapcar '(lambda (buf)
+	       (set-buffer buf)
+	       (if (and (buffer-file-name buf)
+			(string-match "\\.tex$" (buffer-file-name buf))
+			(buffer-modified-p buf)
+			(y-or-n-p (format "Save %s" (buffer-name buf))))
+		   (save-buffer buf)))
+	    (buffer-list)))
+)
+
+(defun YaTeX-read-accent-char (x)
+  "Read char in accent braces."
+  (let ((c (read-char)))
+    (concat
+     (if (and (or (= c ?i) (= c ?j))
+	      (not (string-match (regexp-quote x) "cdb")))
+	 "\\" "")
+     (char-to-string c)))
+)
+
+(defun YaTeX-make-accent ()
+  "Make accent usage."
+  (interactive)
+  (message "1:` 2:' 3:^ 4:\" 5:~ 6:= 7:. u v H t c d b")
+  (let ((c (read-char))(case-fold-search nil))
+    (setq c (cond ((and (> c ?0) (< c ?8))
+		   (substring "`'^\"~=." (1- (- c ?0)) (- c ?0)))
+		  ((= c ?h) "H")
+		  (t (char-to-string c))))
+    (if (not (string-match c "`'^\"~=.uvHtcdb")) nil
+      (insert "\\" c "{}")
+      (backward-char 1)
+      (insert (YaTeX-read-accent-char c))
+      (if (string= c "t") (insert (YaTeX-read-accent-char c)))
+      (forward-char 1)))
+)
+
+(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 arg
+	      ""
+	    (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
+		  (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))))
+    (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-read-user-completion-table ()
   "Append user completion table of LaTeX word"
   (message "Loading personal completion table")
@@ -1032,27 +1656,19 @@
     index)
 )
 
-(defun point-beginning-of-line ()
-  (save-excursion (beginning-of-line)(point))
-)
-
-(defun point-end-of-line ()
-  (save-excursion (end-of-line)(point))
-)
-
 (defun append-to-hook (hook hook-list)
   "Add hook-list to certain emacs's hook correctly.
 Argument hook-list is the list of function int the form to be called
 Call this function with argument as next example,
 	(append-to-hook '((ding))) ;If one function to add.
 	(append-to-hook '((func1)(func2 arg)))."
-  (if (null (eval hook))   			;Not defined
+  (if (null (symbol-value hook))   			;Not defined
       (set hook
 	   (append '(lambda ()) hook-list))
-    (if (listp (eval hook))
-	(if (eq (car (eval hook)) 'lambda)	;'(lambda () ....)
+    (if (listp (symbol-value hook))
+	(if (eq (car (symbol-value hook)) 'lambda)	;'(lambda () ....)
 	    (set hook
-		 (append (eval hook) hook-list))
+		 (append (symbol-value hook) hook-list))
 	  (if (eq hook 'kill-emacs-hook)	;'(hook1 hook2 ...)
 	      (progn				; this format is not
 		(ding)				; for kill-emacs-hook
@@ -1060,12 +1676,12 @@
 		 "Caution!! you have wrong format of kill-emacs-hook"))
 	    (while (not (null hook-list))
 	      (set hook
-		   (append (eval hook) (car hook-list)))
+		   (append (symbol-value hook) (car hook-list)))
 	      (setq hook-list (cdr hook-list))))
 	  )
       (set hook					;'hook
 	   (append '(lambda ())
-		   (cons (list (eval hook)) hook-list)))))
+		   (cons (list (symbol-value hook)) hook-list)))))
 )
 (append-to-hook 'kill-emacs-hook '((YaTeX-save-table)))
 
@@ -1086,7 +1702,7 @@
 ; 1.23 | 92/ 1/ 8 | Enable latex and preview command on DOS.
 ; 1.24 |     1/ 9 | Add YaTeX-save-table to kill-emacs-hook automatically.
 ; 1.25 |     1/16 | YaTeX-do-completion (prefix+SPC) and argument
-;      |          | acceptable YaTeX-make-section works. Put region into
+;      |          | acceptable YaTeX-make-section work. Put region into
 ;      |          | \begin...\end by calling YaTeX-make-begin-end with ARG.
 ;      |          | append-kill-emacs-hook was revised to append-to-hook.
 ; 1.26 |     1/18 | Region mode is added to {\large }. Default fontsize.
@@ -1100,10 +1716,25 @@
 ; 1.32 |    11/16 | YaTeX-goto-corresponding-environment.
 ;      |          | Comment out region/paragraph added.
 ; 1.33 |    11/29 | Variable default value, on DOS and other OS.
-;      |          | make dvi2-command buffer local.  Change the behavior of
+;      |          | Make dvi2-command buffer local.  Change the behavior of
 ;      |          | comment out region/paragraph on the \begin{} or \end{}
-;      |          | line.  Make faster YaTeX-end-environment. Add YaTeX-
+;      |          | line.  Make YaTeX-end-environment faster. Add YaTeX-
 ;      |          | define-key, YaTeX-define-begend-(region-)key.
+; 1.34 |    12/26 | YaTeX-goto-corresponding-* automatically choose its move.
+;      |          | YaTeX-prev-error supports separate typesetting.
+; 1.35 | 93/ 1/25 | YaTeX-kill-environment erases pair of begin/end.
+;      |          | YaTeX-change-environment change the environment name.
+;      |          | Auto indent at YaTeX-make-begin-end.
+; 1.36 |     1/27 | YaTeX-typeset-region typesets the region from %#BEGIN to
+;      |          | %#END, or simple region between point and mark.
+; 1.37 |     2/12 | YaTeX-kill-environment turns YaTeX-kill-some-pairs and
+;      |          | now it can kill %#BEGIN and %#END pairs.
+;      |          | Now YaTeX-goto-corresponding-environment detects nested
+;      |          | environment.  Put " by " in verbatim.  Auto save buffers
+;      |          | with quiery.  Add current file to includeonly list
+;      |          | automatically.  Support YaTeX-fill-item, YaTeX-make-
+;      |          | accent, YaTeX-visit-main-other-window.
+;      |          | [prefix] tl for lpr.  Revise YaTeX-view-error.
 ;------+----------+---------------------------------------------------------
 ;
 ;----------------------------- End of yatex.el -----------------------------
--- a/yatex.new	Mon Nov 30 07:29:11 1992 +0000
+++ b/yatex.new	Tue Feb 16 08:51:15 1993 +0000
@@ -2,6 +2,39 @@
 	Yet Another tex-mode for Emacs
 	yatex.el 各バージョンの変更点について。
 
+1.37:	YaTeX-kill-* の、%#BEGIN/%#END対応。
+	対応する環境へのジャンプで、ネストした環境を正しく検出。
+	verbatim 環境では " は " のまま入力。
+	タイプセット時に未保存の .tex バッファのセーブを確認。
+	カレントファイルが、メインファイルの includeonly からもれている場
+	合には、確認後自動的に includeonly に追加。
+	[prefix] i の \item 項目の桁揃え(fill-prefix自動調整)追加。
+	[prefix] a のアクセント記号入力メニュー追加。
+	[prefix] 4^ で別ウィンドウでメインファイルを選択。
+	[prefix] tl で lpr メニュー。view-error の見直し。
+	Demacs でのタイプセット時には Hit Any Key を出さずに終了。
+	修論が終わった:-)。
+
+1.36:	リジョン指定のタイプセットのサポート。%#BEGINと%#ENDで囲まれた領域、
+	%#END が無ければ %#BEGIN 以降、両方なければマークとポイントの間を
+	texput.tex に保存し、jlatex を起動する。
+
+1.35:	[prefix] k でカーソル上にあるオブジェクトの消去。まずは、対応する
+	begin/end の消去(YaTeX-kill-environment)のみ。
+	[prefix] c でカーソル上にあるオブジェクトの変更。まずは、LaTeX の
+	環境名の変更(YaTeX-change-environment)のみ。
+	アドイン関数のサポート。まずは、begin型補完時に呼び出せる関数があ
+	るかチェック。
+	補完入力した位置を YaTeX-current-position-register に保存。
+	begin型補完を入力した位置と同じインデントで補完。
+
+1.34:	Mule 対応(漢字コード関係の見直し)。
+	[prefix] g を押した時の行の内容により、動作を変えるようにした。
+	YaTeX-prev-error で tex コマンドのログからどのファイルから出された
+	エラーか調べ、そのファイルに switch-to-buffer するようにした。
+	typeset 中に何かキーを押して、編集バッファに戻った場合、typeset が
+	終了したら自動的に、typeset バッファの一番下を表示するようにした。
+
 1.33:	コマンド名のデフォルト値を OS によって自動的に選択。
 	\begin{} \end{} の行でコメント操作したときの動作を変更。
 	dvi2-command をバッファローカルに。

yatex.org