yatex

view yatex.el @ 5:370a391533fd

Post fj.sources.
author yuuji
date Tue, 16 Feb 1993 08:51:15 +0000
parents 78dfe28b6a35
children 49be9ccb0b65
line source
1 ;;; -*- Emacs-Lisp -*-
2 ;;; Yet Another tex-mode for emacs.
3 ;;; yatex.el rev.1.37
4 ;;; (c)1991-1993 by HIROSE Yuuji.[yuuji@ae.keio.ac.jp]
5 ;;; Last modified Fri Feb 12 16:05:54 1993 on VFR
7 ;; This software is distributed in the hope that it will be useful,
8 ;; but WITHOUT ANY WARRANTY. No author or distributor
9 ;; accepts responsibility to anyone for the consequences of using it
10 ;; or for whether it serves any particular purpose or works at all,
11 ;; unless he says so in writing. Refer to the GNU Emacs General Public
12 ;; License for full details.
14 ;; Everyone is granted permission to copy, modify and redistribute
15 ;; this software, but only under the conditions described in the
16 ;; GNU Emacs General Public License. A copy of this license is
17 ;; supposed to have been given to you along with this software so you
18 ;; can know your rights and responsibilities. It should be in a
19 ;; file named COPYING. Among other things, the copyright notice
20 ;; and this notice must be preserved on all copies.
22 (provide 'yatex)
23 (require 'comment)
24 (defconst YaTeX-revision-number "1.37"
25 "Revision number of running yatex.el"
26 )
28 ;---------- Local variables ----------
29 ;;;
30 ;; Initialize local variable for yatex-mode.
31 ;; Preserving user preferred definitions.
32 ;; ** Check all of these defvar-ed values **
33 ;; ** and setq other values more suitable **
34 ;; ** for your site, if needed. **
35 ;;;
36 (defvar YaTeX-prefix "\^C"
37 "*Prefix key to trigger YaTeX functions.
38 You can select favorite prefix key by setq in your ~/.emacs."
39 )
40 (defvar YaTeX-open-lines 1
41 "*Blank lines between text and \??{??}"
42 )
43 (defvar YaTeX-fill-prefix ""
44 "*fill-prefix used for auto-fill-mode.
45 The defalut value is null string."
46 )
47 (defvar YaTeX-fill-column 72
48 "*fill-column used for auto-fill-mode."
49 )
50 (defvar YaTeX-comment-prefix "%"
51 "TeX comment prefix."
52 )
53 (defvar YaTeX-current-position-register ?3
54 "*All of YaTeX completing input store the current position into
55 the register YaTeX-current-position-register. So every time you
56 make a trip to any other part of text than you writing, you can
57 return to editing paragraph by calling register-to-point with argument
58 YaTeX-current-position-register."
59 )
60 (defvar YaTeX-user-completion-table
61 (if (eq system-type 'ms-dos) "~/_yatexrc" "~/.yatexrc")
62 "*Default filename in which user completion table is saved."
63 )
64 (defvar tex-command "jlatex"
65 "*Default command for compiling LaTeX text."
66 )
67 (defvar dvi2-command ;previewer command for your site
68 (if (eq system-type 'ms-dos) "dviout"
69 (concat "xdvi -geo +0+0 -s 4 -display " (getenv "DISPLAY")))
70 "*Default previewer command including its option.
71 This default value is for X window system. If you want to use this
72 default, you have to make sure the environment variable DISPLAY is
73 correctly set."
74 )
75 (defvar dviprint-command-format
76 (if (eq system-type 'ms-dos) "dviprt %s %f%t"
77 "dvi2ps %f %t %s | lpr")
78 "*Command line string to print out current file. Format string %s
79 will be replaced by the filename. Do not forget to specify the
80 `from usage' and `to usage' with their option by format string %f and %t.
81 See also documentation of dviprint-from-format and dviprint-to-format."
82 )
83 (defvar dviprint-from-format
84 (if (eq system-type 'ms-dos) "%b-" "-f %b")
85 "*From page format of dvi filter. %b will turn to beginning page number."
86 )
87 (defvar dviprint-to-format
88 (if (eq system-type 'ms-dos) "%e" "-t %e")
89 "*To page format of dvi filter. %e will turn to end page number."
90 )
91 (defvar YaTeX-japan (or (boundp 'NEMACS) (boundp 'MULE))
92 "Whether yatex mode is running on Japanese environment or not."
93 )
94 (defvar YaTeX-default-document-style
95 (concat (if YaTeX-japan "j") "article")
96 "*Default LaTeX Documentstyle for YaTeX-typeset-region."
97 )
98 (defvar YaTeX-need-nonstop nil
99 "*If t yatex automatically put `\nonstopmode{}' in current buffer
100 before invoke latex command."
101 )
102 (defvar latex-warning-regexp "line.* [0-9]*"
103 "*Regular expression of line number of warning message by latex command."
104 )
105 (defvar latex-error-regexp "l\\.[1-9][0-9]*"
106 "*Regular expression of line number of latex error. Perhaps your latex
107 command stops at this error message with line number of LaTeX source text."
108 )
109 (defvar latex-dos-emergency-message
110 "Emergency stop" ;<- for Micro tex, ASCII-pTeX 1.6
111 "Because Demacs (GNU Emacs on DOS) cannot have concurrent process, the
112 latex command which is stopping on a LaTeX error, is terminated by Demacs.
113 Many latex command on DOS display some message when it is terminated by
114 other process, user or OS. Define this variable a message string of your
115 latex command on DOS shows at abnormal termination.
116 Remember Demacs's call-process function is not oriented for interactive
117 process."
118 )
119 (defvar latex-message-kanji-code 2
120 "*Kanji coding system latex command types out.
121 1 = Shift JIS, 2 = JIS, 3 = EUC."
122 )
123 (defvar NTT-jTeX nil
124 "*Use NTT-jTeX for latex command."
125 )
127 ;------------ Completion table ------------
128 ; Set tex-section-like command possible completion
129 (setq section-table
130 '(("part") ("section") ("subsection") ("subsubsection")
131 ("author") ("documentstyle") ("pagestyle")
132 ("documentstyle[10pt]") ("documentstyle[11pt]")
133 ("documentstyle[12pt]")
134 ("title") ("chapter") ("part") ("paragraph")
135 ("subparagraph") ("underline") ("label") ("footnote")
136 ("hspace*") ("vspace*") ("bibliography") ("bibitem[]") ("cite[]")
137 ("nocite") ("input") ("include") ("includeonly") ("mbox") ("hbox")
138 ("caption") ("newcommand") ("setlength") ("addtolength")
139 ("newenvironment") ("newtheorem")
140 ("cline") ("framebox")
141 ))
142 (defvar user-section-table nil)
144 ; Set style possible completion
145 (setq article-table
146 '(("article") ("jarticle") ("report") ("jreport") ("jbook")
147 ("4em") ("2ex")
148 ("empty") ("headings") ("\\textwidth")
149 ("\\oddsidemargin") ("\\evensidemargin")
150 ("\\textheight") ("\\topmargin")
151 ("\\bottommargin") ("\\footskip") ("\\footheight")
152 ("\\baselineskip") ("\\baselinestretch") ("normalbaselineskip")
153 ))
154 (defvar user-article-table nil)
156 ; Set tex-environment possible completion
157 (setq env-table
158 '(("quote") ("quotation") ("center") ("verse") ("document")
159 ("verbatim") ("itemize") ("enumerate") ("description")
160 ("list{}") ("tabular") ("table") ("tabbing") ("titlepage")
161 ("sloppypar") ("ref") ("quotation") ("quote") ("picture")
162 ("eqnarray") ("figure") ("equation") ("abstract") ("array")
163 ("thebibliography") ("theindex") ("flushleft") ("flushright")
164 ))
165 (defvar user-env-table nil)
167 ; Set {\Large }-like comletion
168 (setq fontsize-table
169 '(("rm") ("em") ("bf") ("boldmath") ("it") ("sl") ("sf") ("sc") ("tt")
170 ("dg") ("dm")
171 ("tiny") ("scriptsize") ("footnotesize") ("small")("normalsize")
172 ("large") ("Large") ("LARGE") ("huge") ("Huge")
173 ))
174 (defvar user-fontsize-table nil)
176 (setq singlecmd-table
177 '(("maketitle") ("sloppy") ("protect")
178 ("alpha") ("beta") ("gamma") ("delta") ("epsilon") ("varepsilon")
179 ("zeta") ("eta") ("theta")("vartheta") ("iota") ("kappa")
180 ("lambda") ("mu") ("nu") ("xi") ("pi") ("varpi") ("rho") ("varrho")
181 ("sigma") ("varsigma") ("tau") ("upsilon") ("phi") ("varphi")
182 ("chi") ("psi") ("omega") ("Gamma") ("Delta") ("Theta") ("Lambda")
183 ("Xi") ("Pi") ("Sigma") ("Upsilon") ("Phi") ("Psi") ("Omega")
184 ("LaTeX") ("TeX") ("item[]") ("appendix") ("hline")
185 ("rightarrow") ("Rightarrow") ("leftarrow") ("Leftarrow")
186 ("pagebreak")
187 ))
188 (defvar user-singlecmd-table nil)
190 ;---------- Key mode map ----------
191 ;;;
192 ;; Create new key map: YaTeX-mode-map
193 ;; Do not change this section.
194 ;;;
195 (defvar YaTeX-inhibit-prefix-letter nil
196 "*Switch which determins whether inhibit yatex.el from defining
197 key sequence \"C-c letter\" or not."
198 )
199 (defvar YaTeX-mode-map nil
200 "Keymap used in YaTeX mode."
201 )
202 (defvar YaTeX-typesetting-mode-map nil
203 "Keymap userd in YaTeX typesetting buffer."
204 )
205 (defvar YaTeX-prefix-map nil
206 "Keymap used when YaTeX-prefix key pushed."
207 )
209 ;---------- Define deafult key bindings on YaTeX mode map ----------
210 (defun YaTeX-define-key (key binding)
211 "Define key on YaTeX-prefix-map"
212 (if YaTeX-inhibit-prefix-letter
213 (let ((c (aref key 0)))
214 (cond
215 ((and (>= c ?a) (<= c ?z)) (aset key 0 (1+ (- c ?a))))
216 ((and (>= c ?A) (<= c ?Z)) (aset key 0 (1+ (- c ?A))))
217 (t nil))))
218 (define-key YaTeX-prefix-map key binding)
219 )
220 (defun YaTeX-define-begend-key (key env)
221 "Define short cut YaTeX-make-begin-end key."
222 (YaTeX-define-key
223 key
224 (list 'lambda '(arg) '(interactive "P")
225 (list 'YaTeX-insert-begin-end env 'arg)))
226 )
227 (defun YaTeX-define-begend-region-key (key env)
228 "Define short cut YaTeX-make-begin-end-region key."
229 (YaTeX-define-key key (list 'lambda nil '(interactive)
230 (list 'YaTeX-insert-begin-end env t)))
231 )
232 ;;;
233 ;; Define key table
234 ;;;
235 (if YaTeX-mode-map
236 nil
237 (setq YaTeX-mode-map (make-sparse-keymap))
238 (setq YaTeX-prefix-map (make-sparse-keymap))
239 (define-key YaTeX-mode-map "\"" 'YaTeX-insert-quote)
240 (define-key YaTeX-mode-map "{" 'YaTeX-insert-braces)
241 (define-key YaTeX-mode-map "(" '(lambda () (interactive)
242 (insert "()") (backward-char 1)))
243 ;(define-key YaTeX-mode-map "[" 'YaTeX-insert-brackets)
244 (define-key YaTeX-mode-map YaTeX-prefix YaTeX-prefix-map)
245 (YaTeX-define-key "t" 'YaTeX-typeset-menu)
246 (YaTeX-define-key "'" 'YaTeX-prev-error)
247 (YaTeX-define-key "^" 'YaTeX-visit-main)
248 (YaTeX-define-key "4^" 'YaTeX-visit-main-other-window)
249 (YaTeX-define-key " " 'YaTeX-do-completion)
250 (YaTeX-define-key "v" 'YaTeX-version)
252 (YaTeX-define-key "}" 'YaTeX-insert-braces-region)
253 (YaTeX-define-key "]" 'YaTeX-insert-brackets-region)
254 (YaTeX-define-key "d" 'YaTeX-insert-dollar)
255 (YaTeX-define-key "i" 'YaTeX-fill-item)
256 (YaTeX-define-key
257 "\\" '(lambda () (interactive) (YaTeX-insert-string "$\\backslash$")))
258 (YaTeX-define-begend-region-key "Bd" "document")
259 (YaTeX-define-begend-key "bd" "document")
260 (YaTeX-define-begend-region-key "BD" "description")
261 (YaTeX-define-begend-key "bD" "description")
262 (YaTeX-define-begend-region-key "Be" "enumerate")
263 (YaTeX-define-begend-key "be" "enumerate")
264 (YaTeX-define-begend-region-key "Bi" "itemize")
265 (YaTeX-define-begend-key "bi" "itemize")
266 (YaTeX-define-begend-region-key "Bt" "tabbing")
267 (YaTeX-define-begend-key "bt" "tabbing")
268 (YaTeX-define-begend-region-key "BT" "tabular")
269 (YaTeX-define-begend-key "bT" "tabular")
270 (YaTeX-define-begend-region-key "Bq" "quote")
271 (YaTeX-define-begend-key "bq" "quote")
272 (YaTeX-define-begend-region-key "BQ" "quotation")
273 (YaTeX-define-begend-key "bQ" "quotation")
274 (YaTeX-define-key "." 'YaTeX-comment-paragraph)
275 (YaTeX-define-key "," 'YaTeX-uncomment-paragraph)
276 (YaTeX-define-key ">" 'YaTeX-comment-region)
277 (YaTeX-define-key "<" 'YaTeX-uncomment-region)
278 (YaTeX-define-key "B " 'YaTeX-make-begin-end-region)
279 (YaTeX-define-key "b " 'YaTeX-make-begin-end)
280 (YaTeX-define-key "e" 'YaTeX-end-environment)
281 (YaTeX-define-key "s" 'YaTeX-make-section)
282 (YaTeX-define-key "L" 'YaTeX-make-fontsize-region)
283 (YaTeX-define-key "l" 'YaTeX-make-fontsize)
284 (YaTeX-define-key "m" 'YaTeX-make-singlecmd)
285 (YaTeX-define-key "g" 'YaTeX-goto-corresponding-*)
286 (YaTeX-define-key "k" 'YaTeX-kill-*)
287 (YaTeX-define-key "c" 'YaTeX-change-*)
288 (YaTeX-define-key "a" 'YaTeX-make-accent)
289 (YaTeX-define-key "n"
290 '(lambda () (interactive) (YaTeX-insert-string "\\\\")))
291 (if (eq system-type 'ms-dos)
292 (define-key YaTeX-prefix-map "L"
293 '(lambda () (interactive)
294 (set-screen-height YaTeX-saved-screen-height) (recenter))))
295 )
297 (if YaTeX-typesetting-mode-map nil
298 (setq YaTeX-typesetting-mode-map (make-keymap))
299 (suppress-keymap YaTeX-typesetting-mode-map t)
300 (define-key YaTeX-typesetting-mode-map " "
301 'YaTeX-jump-error-line)
302 )
304 ;---------- Customize as you like above ----------
306 ;---------- Define other variable ----------
307 (defvar env-name "document") ;Initial tex-environment completion
308 (defvar section-name "documentstyle[12pt]") ;Initial tex-section completion
309 (defvar fontsize-name "large") ;Initial fontsize completion
310 (defvar single-command "maketitle") ;Initial LaTeX single command
311 (defvar YaTeX-user-table-has-read nil
312 "Flag that means whether user completion table has been read or not."
313 )
314 (defvar YaTeX-user-table-modified nil
315 "Flag that means whether user completion table has modified or not."
316 )
317 (defvar yatex-mode-hook nil
318 "*List of functions to be called after .tex file is read
319 and yatex-mode starts."
320 )
321 (defvar YaTeX-kanji-code-alist
322 '((1 . *sjis*) (2 . *junet*) (3 . *euc-japan*))
323 )
324 (defvar YaTeX-kanji-code (if (eq system-type 'ms-dos) 1 2)
325 "*File kanji code used by Japanese TeX."
326 )
327 (cond
328 ((boundp 'MULE)
329 (defvar YaTeX-coding-system
330 (symbol-value (cdr (assoc YaTeX-kanji-code YaTeX-kanji-code-alist)))
331 "File coding system used by Japanese TeX")
332 (if (not (eq system-type 'ms-dos))
333 (defvar YaTeX-latex-message-code *autoconv*)))
334 ((boundp 'NEMACS)
335 (defvar YaTeX-latex-message-code latex-message-kanji-code))
336 )
337 ;---------- Produce YaTeX-mode ----------
338 ;;;
339 ;; Major mode definition
340 ;;;
341 (defun yatex-mode ()
342 (interactive)
343 (kill-all-local-variables)
344 (setq major-mode 'YaTeX-mode)
345 (setq mode-name (if YaTeX-japan "‚â‚Ä‚Ó" "YaTeX"))
346 (make-local-variable 'dvi2-command)
347 (cond ((boundp 'MULE) ;;1992/12/21 by NIIMI Satoshi.
348 ;; file-coding-system is buffer local variable
349 (set-file-coding-system YaTeX-coding-system))
350 ((boundp 'NEMACS)
351 (make-local-variable 'kanji-fileio-code)
352 (setq kanji-fileio-code YaTeX-kanji-code)))
353 (make-local-variable 'fill-column)
354 (make-local-variable 'fill-prefix)
355 (setq fill-column YaTeX-fill-column
356 fill-prefix YaTeX-fill-prefix)
357 (use-local-map YaTeX-mode-map)
358 (if (eq system-type 'ms-dos)
359 (setq YaTeX-saved-screen-height (screen-height)))
360 (if YaTeX-user-table-has-read nil
361 (YaTeX-read-user-completion-table)
362 (setq YaTeX-user-table-has-read t))
363 (run-hooks 'text-mode-hook 'yatex-mode-hook)
364 )
366 ;---------- Define macro ---------
367 (defmacro point-beginning-of-line ()
368 (save-excursion (beginning-of-line)(point))
369 )
371 (defmacro point-end-of-line ()
372 (save-excursion (end-of-line)(point))
373 )
375 ;---------- Define YaTeX-mode functions ----------
376 ;;;
377 ;; YaTeX-mode functions
378 ;;;
379 (defun YaTeX-insert-begin-end (env arg)
380 "Insert \begin{mode-name} and \end{mode-name}."
381 (if arg
382 (save-excursion
383 (if (> (point) (mark)) (exchange-point-and-mark))
384 (insert "\\begin{" env "}")
385 (YaTeX-addin env)
386 (insert "\n")
387 (indent-relative-maybe)
388 (exchange-point-and-mark)
389 (insert "\\end{" env "}\n")
390 (indent-relative-maybe))
391 ;(delete-blank-lines)
392 (let ((i 1))
393 (insert "\\begin{" env "}")
394 (YaTeX-addin env)
395 (insert "\n")
396 (indent-relative-maybe)
397 ;;(newline (1+ (* 2 YaTeX-open-lines)))
398 (while (<= i (1+ (* 2 YaTeX-open-lines)))
399 (insert "\n")
400 (indent-relative-maybe)
401 (setq i (1+ i)))
402 (insert "\\end{" env "}")
403 (previous-line (+ 1 YaTeX-open-lines)));let i
404 (if YaTeX-current-position-register
405 (point-to-register YaTeX-current-position-register)))
406 )
408 (defun YaTeX-make-begin-end (arg)
409 "Make LaTeX environment command of \\begin{env.} ... \\end{env.}
410 by completing read.
411 If you invoke this command with universal argument,
412 \(key binding for universal-argument is \\[universal-argument]\)
413 you can put REGION into that environment between \\begin and \\end."
414 (interactive "P")
415 (let*
416 ((mode (if arg " region" ""))
417 (env
418 (YaTeX-read-environment
419 (format "Begin environment%s(default %s): " mode env-name))))
420 (if (string= env "")
421 (setq env env-name))
422 (setq env-name env)
423 (if (not (assoc env-name (append user-env-table env-table))) ;if not exist
424 (setq user-env-table (cons (list env-name) user-env-table)
425 YaTeX-user-table-modified t))
426 (YaTeX-insert-begin-end env-name arg))
427 )
429 (defun YaTeX-make-begin-end-region ()
430 "Call YaTeX-make-begin-end with ARG to specify region mode."
431 (interactive)
432 (YaTeX-make-begin-end t)
433 )
435 (defun YaTeX-inner-environment ()
436 "Return current inner environment."
437 (save-excursion
438 (let ((nest 0) s)
439 (while
440 (and
441 (>= nest 0)
442 (re-search-backward
443 "\\(\\\\begin{\\).*}\\|\\(\\\\end{\\).*}" (point-min) t))
444 (if (re-search-backward "^[ ]*%" (point-beginning-of-line) t)
445 nil ;ignore TeX comment usage.
446 (setq nest (if (eq (match-beginning 0) (match-beginning 1))
447 (1- nest) (1+ nest)))))
448 (if (>= nest 0)
449 nil
450 (goto-char (match-end 1))
451 (setq s (point))
452 (skip-chars-forward "^}")
453 (buffer-substring s (point))
454 )))
455 )
457 (defun YaTeX-end-environment ()
458 "Close opening environment"
459 (interactive)
460 (let ((curp (point))
461 (env (YaTeX-inner-environment)))
463 (if (not env) (error "No premature environment")
464 (save-excursion
465 (if (and (re-search-forward "^[^\\%]*\\\\end{.*}" (point-max) t)
466 (progn (goto-char (match-beginning 0))
467 (re-search-forward env (match-end 0) t)))
468 (if (y-or-n-p
469 (concat "Environment `" env
470 "' was already closed. Force close?"))
471 nil
472 (error "end environment aborted."))))
473 (message "") ;Erase (y or n) message.
474 (insert "\\end{" env "}")
475 (setq curp (point))
476 (goto-char (match-end 0))
477 (if (pos-visible-in-window-p)
478 (sit-for 1)
479 (message (concat "Matches \\begin{" env
480 (format "} at line %d"
481 (count-lines (point-min) (match-end 0))))))
482 (goto-char curp))
483 )
484 )
486 (defun YaTeX-make-section (arg)
487 "Make LaTeX \\section{} type command with completing read.
488 With ARG of numeric, you can specify the number of argument of
489 LaTeX command.
490 For example, if you want to produce LaTeX command
492 \\addtolength{\\topmargin}{8mm}
494 which has two arguments. You can produce that sequence by typing...
495 ESC 2 C-c s add SPC RET \\topm SPC RET 8mm RET
496 \(by default\)
497 You can complete symbol at LaTeX command and 1st argument."
498 (interactive "p")
499 (let*
500 ((section
501 (completing-read
502 (format "\\???{} (default %s): " section-name)
503 (append user-section-table section-table)
504 nil nil))
505 (section (if (string= section "") section-name section))
506 (title
507 (completing-read (concat "\\" section "{???}: ")
508 (append user-article-table article-table)
509 nil nil)))
510 (setq section-name section)
511 (if (not (assoc section-name (append user-section-table section-table)))
512 (setq user-section-table
513 (cons (list section-name) user-section-table)
514 YaTeX-user-table-modified t))
515 (insert "\\" section-name "{" title "}")
516 (if YaTeX-current-position-register
517 (point-to-register YaTeX-current-position-register))
518 (let ((j 2))
519 (while (<= j arg)
520 (insert (concat "{" (read-string (format "Argument %d: " j))))
521 (insert "}")
522 (setq j (1+ j)))
523 )
524 (if (string= title "") (forward-char -1)
525 nil))
526 )
528 ;(defun YaTeX-make-section-region ()
529 ; "Call YaTeX-make-section with ARG to specify region mode."
530 ; (interactive)
531 ; (YaTeX-make-section t)
532 ;)
534 (defun YaTeX-make-fontsize (arg)
535 "Make completion like {\\large ...} or {\\slant ...} in minibuffer.
536 If you invoke this command with universal argument, you can put region
537 into {\\xxx } braces.
538 \(key binding for universal-argument is \\[universal-argument]\)"
539 (interactive "P")
540 (let* ((mode (if arg "region" ""))
541 (fontsize
542 (completing-read
543 (format "{\\??? %s} (default %s): " mode fontsize-name)
544 (append user-fontsize-table fontsize-table)
545 nil nil )))
546 (if (string= fontsize "")
547 (setq fontsize fontsize-name))
548 (setq fontsize-name fontsize)
549 (if (not (assoc fontsize-name (append user-fontsize-table fontsize-table)))
550 (setq user-fontsize-table
551 (cons (list fontsize-name) user-fontsize-table)
552 YaTeX-user-table-modified t))
553 (if arg
554 (save-excursion
555 (if (> (point) (mark)) (exchange-point-and-mark))
556 (insert "{\\" fontsize-name " ")
557 (exchange-point-and-mark)
558 (insert "}"))
559 (insert "{\\" fontsize-name " }")
560 (if YaTeX-current-position-register
561 (point-to-register YaTeX-current-position-register))
562 (forward-char -1)))
563 )
565 (defun YaTeX-make-fontsize-region ()
566 "Call function:YaTeX-make-fontsize with ARG to specify region mode."
567 (interactive)
568 (YaTeX-make-fontsize t)
569 )
571 (defun YaTeX-make-singlecmd (single)
572 (interactive
573 (list (completing-read
574 (format "\\??? (default %s): " single-command)
575 (append user-singlecmd-table singlecmd-table)
576 nil nil )))
577 (if (string= single "")
578 (setq single single-command))
579 (setq single-command single)
580 (if (not (assoc single-command
581 (append user-singlecmd-table singlecmd-table)))
582 (setq user-singlecmd-table
583 (cons (list single-command) user-singlecmd-table)
584 YaTeX-user-table-modified t))
585 (insert "\\" single-command " ")
586 (if YaTeX-current-position-register
587 (point-to-register YaTeX-current-position-register))
588 )
590 (defvar YaTeX-completion-begin-regexp "[{\\]"
591 "Regular expression of limit where LaTeX command's
592 completion begins.")
594 (defun YaTeX-do-completion ()
595 "Try completion on LaTeX command preceding point."
596 (interactive)
597 (if
598 (or (eq (preceding-char) ? )
599 (eq (preceding-char) ?\t)
600 (eq (preceding-char) ?\n)
601 (bobp))
602 (message "Nothing to complete.") ;Do not complete
603 (let* ((end (point))
604 (limit (save-excursion (beginning-of-line) (point)))
605 (completion-begin
606 (progn (re-search-backward "[ \t\n]" limit 1)
607 (point)))
608 (begin (progn
609 (goto-char end)
610 (if (re-search-backward YaTeX-completion-begin-regexp
611 completion-begin t)
612 (1+ (point))
613 nil))))
614 (goto-char end)
615 (cond
616 ((null begin)
617 (message "I think it is not LaTeX sequence."))
618 (t
619 (let* ((pattern (buffer-substring begin end))
620 (all-table (append section-table user-section-table
621 article-table user-article-table
622 env-table user-env-table
623 singlecmd-table user-singlecmd-table))
624 ;; First,
625 ;; search completion without backslash.
626 (completion (try-completion pattern all-table nil)))
627 (if
628 (eq completion nil)
629 ;; Next,
630 ;; search completion with backslash
631 (setq completion
632 (try-completion (buffer-substring (1- begin) end)
633 all-table nil)
634 begin (1- begin)))
635 (cond
636 ((null completion)
637 (message (concat "Can't find completion for '" pattern "'"))
638 (ding))
639 ((eq completion t) (message "Sole completion."))
640 ((not (string= completion pattern))
641 (kill-region begin end)
642 (insert completion)
643 )
644 (t
645 (message "Making completion list...")
646 (with-output-to-temp-buffer "*Help*"
647 (display-completion-list
648 (all-completions pattern all-table))) )
649 ))))))
650 )
652 (defun YaTeX-insert-quote ()
653 (interactive)
654 (insert
655 (cond
656 ((YaTeX-quick-in-environment-p "verbatim") ?\")
657 ((= (preceding-char) ?\\ ) ?\")
658 ((= (preceding-char) ?\( ) ?\")
659 ((= (preceding-char) 32) "``")
660 ((= (preceding-char) 9) "``")
661 ((= (preceding-char) ?\n) "``")
662 ((bobp) "``")
663 (t "''")
664 )))
666 (defun YaTeX-insert-braces-region (beg end &optional open close)
667 (interactive "r")
668 (save-excursion
669 (goto-char end)
670 (insert (or close "}"))
671 (goto-char beg)
672 (insert (or open "{")))
673 )
675 (defun YaTeX-insert-braces ()
676 (interactive)
677 (insert "{}")
678 (forward-char -1)
679 )
681 (defun YaTeX-insert-brackets-region (beg end)
682 (interactive "r")
683 (save-excursion
684 (YaTeX-insert-braces-region beg end "[" "]"))
685 )
687 (defun YaTeX-insert-dollar ()
688 (interactive)
689 (insert "$$")
690 (forward-char -1)
691 )
693 (defun YaTeX-insert-string (s)
694 (insert s)
695 )
697 (defun YaTeX-version ()
698 "Return string of the version of running YaTeX."
699 (interactive)
700 (message
701 (concat "Yet Another tex-mode "
702 (if YaTeX-japan "u–ì’¹v" "Wild Bird")
703 "Revision "
704 YaTeX-revision-number))
705 )
707 (defun YaTeX-typeset-sentinel (proc mes)
708 (cond ((null (buffer-name (process-buffer proc)))
709 ;; buffer killed
710 (set-process-buffer proc nil))
711 ((memq (process-status proc) '(signal exit))
712 (let* ((obuf (current-buffer)))
713 ;; save-excursion isn't the right thing if
714 ;; process-buffer is current-buffer
715 (unwind-protect
716 (progn
717 ;; Write something in *typesetting* and hack its mode line
718 (if (equal (current-buffer) (process-buffer proc))
719 nil
720 (other-window 1)
721 (switch-to-buffer (process-buffer proc))
722 (goto-char (point-max))
723 (recenter -3)
724 (other-window -1))
725 (set-buffer (process-buffer proc))
726 (goto-char (point-max))
727 (insert ?\n "latex typesetting " mes)
728 (forward-char -1)
729 (insert " at "
730 (substring (current-time-string) 0 -5))
731 (insert "\n * Hit any key to return * ")
732 (forward-char 1)
733 (setq mode-line-process
734 (concat ": "
735 (symbol-name (process-status proc))))
736 (message "latex typesetting done.")
737 ;; If buffer and mode line will show that the process
738 ;; is dead, we can delete it now. Otherwise it
739 ;; will stay around until M-x list-processes.
740 (delete-process proc)
741 )
742 (setq YaTeX-typesetting-process nil)
743 ;; Force mode line redisplay soon
744 (set-buffer-modified-p (buffer-modified-p))
745 )
746 (set-buffer obuf)
747 )))
748 )
750 (defvar YaTeX-typesetting-process nil
751 "Process identifier for jlatex"
752 )
753 (defvar YaTeX-typeset-buffer "*YaTeX-typesetting*"
754 "Process buffer for jlatex")
756 (defun YaTeX-typeset (command)
757 "Execute jlatex (or other) to LaTeX typeset."
758 (interactive)
759 (if YaTeX-typesetting-process
760 (if (eq (process-status YaTeX-typesetting-process) 'run)
761 (progn (interrupt-process YaTeX-typesetting-process)
762 ;(sit-for 1)
763 (delete-process YaTeX-typesetting-process))
764 nil) nil)
765 (setq YaTeX-typesetting-process nil)
766 (if (eq system-type 'ms-dos) ;if MS-DOS
767 (with-output-to-temp-buffer YaTeX-typeset-buffer
768 (message (concat "Typesetting " (buffer-name) "..."))
769 (YaTeX-put-nonstopmode)
770 (call-process shell-file-name
771 nil YaTeX-typeset-buffer nil "/c" command)
772 (YaTeX-remove-nonstopmode))
773 (setq YaTeX-typesetting-process ;if UNIX
774 (with-output-to-temp-buffer YaTeX-typeset-buffer
775 (start-process "LaTeX" YaTeX-typeset-buffer shell-file-name "-c"
776 command)
777 ))
778 (set-process-sentinel YaTeX-typesetting-process 'YaTeX-typeset-sentinel))
779 (setq current-TeX-buffer (buffer-name))
780 (other-window 1)
781 (use-local-map YaTeX-typesetting-mode-map)
782 (setq mode-name "typeset")
783 (if YaTeX-typesetting-process ; if process is running (maybe on UNIX)
784 (cond ((boundp 'MULE)
785 (set-current-process-coding-system
786 YaTeX-latex-message-code YaTeX-coding-system))
787 ((boundp 'NEMACS)
788 (set-kanji-process-code YaTeX-latex-message-code))))
789 (message "Type SPC to continue.")
790 (goto-char (point-max))
791 (sit-for 1)
792 (if (eq system-type 'ms-dos) (message "") (read-char));hit any key
793 (forward-line -1)
794 (recenter -1)
795 (other-window -1)
796 )
798 (defun YaTeX-typeset-region ()
799 "Paste the region to the file `texput.tex' and execute jlatex (or other)
800 to LaTeX typeset. The region is specified by the rule:
801 (1)If keyword `%#BEGIN' is found in the upper direction from (point).
802 (1-1)if the keyword `%#END' is found after `%#BEGIN',
803 ->Assume the text between `%#BEGIN' and `%#END' as region.
804 (1-2)if the keyword `%#END' is not found anywhere after `%#BEGIN',
805 ->Assume the text after `%#BEGIN' as region.
806 (2)If no `%#BEGIN' usage is found before the (point),
807 ->Assume the text between current (point) and (mark) as region.
808 DON'T forget to eliminate the `%#BEGIN/%#END' notation after editing
809 operation to the region."
810 (interactive)
811 (save-excursion
812 (let*
813 ((end "") typeout ;Type out message that tells the method of cutting.
814 (buffer (current-buffer)) opoint preamble
815 (region
816 (if (re-search-backward
817 "%#BEGIN" nil t)
818 (progn
819 (setq typeout "--- Region from BEGIN to " end "END ---")
820 (buffer-substring
821 (match-end 0)
822 (if (re-search-forward "%#END" nil t)
823 (match-beginning 0)
824 (setq end "end of buffer ---")
825 (point-max))))
826 (setq typeout "=== Region from (point) to (mark) ===")
827 (buffer-substring (point) (mark)))))
828 (YaTeX-visit-main)
829 (setq opoint (point))
830 (goto-char (point-min))
831 (setq
832 preamble
833 (if (re-search-forward "^[ ]*\\\\begin.*{document}" nil t)
834 (buffer-substring (point-min) (match-end 0))
835 (concat "\\documentstyle{" YaTeX-default-document-style "}\n"
836 "\\begin{document}")))
837 (goto-char opoint)
838 (switch-to-buffer buffer) ;for clarity
839 (find-file "texput.tex")
840 (erase-buffer)
841 (if YaTeX-need-nonstop
842 (insert "\\nonstopmode{}\n"))
843 (insert preamble "\n")
844 (insert region)
845 (insert "\\typeout{" typeout end "}\n") ;Notice the selected method.
846 (insert "\n\\end{document}\n")
847 (basic-save-buffer)
848 (kill-buffer (current-buffer))
849 (YaTeX-typeset (concat (YaTeX-get-latex-command nil) " texput.tex"))))
850 )
852 (defun YaTeX-typeset-buffer ()
853 "Typeset whole buffer."
854 (interactive)
855 (if (YaTeX-main-file-p) nil
856 (let*((me (substring (buffer-name) 0 (rindex (buffer-name) ?.))))
857 (save-excursion ;save excursion of current buffer.
858 (YaTeX-visit-main)
859 (save-excursion ;save excursion of main .tex buffer
860 (push-mark (point) t)
861 (goto-char (point-min))
862 (if (and (re-search-forward "^[ ]*\\\\begin{document}" nil t)
863 (re-search-backward "^[ ]*\\\\includeonly{" nil t))
864 (let*
865 ((b (progn (skip-chars-forward "^{") (point)))
866 (e (progn (skip-chars-forward "^}") (1+ (point))))
867 (s (buffer-substring b e)) c)
868 (if (string-match (concat "[{,]" me "[,}]") s)
869 nil
870 (ding)
871 (message
872 "File:`%s' is not in includeonly list. A)ppend R)eplace %%)comment? " me)
873 (setq c (read-char))
874 (cond
875 ((= c ?a)
876 (goto-char (1+ b))
877 (insert me (if (string= s "{}") "" ",")))
878 ((= c ?r)
879 (delete-region (1+ b) (1- e)) (insert me))
880 ((= c ?%)
881 (beginning-of-line) (insert "%"))
882 (t nil))
883 (basic-save-buffer))))
884 (exchange-point-and-mark))
885 )))
886 (YaTeX-save-buffers)
887 (YaTeX-typeset (YaTeX-get-latex-command t))
888 )
890 (defun YaTeX-preview (preview-command preview-file)
891 "Execute xdvi (or other) to tex-preview."
892 (interactive
893 (list (read-string "Preview command: " dvi2-command)
894 (read-string "Prefiew file[.dvi]: "
895 ;;(substring (buffer-name) 0 -4)
896 (YaTeX-get-preview-file-name) ;ver 1.31
897 )))
898 (setq dvi2-command preview-command)
899 (with-output-to-temp-buffer "*dvi-preview*"
900 (if (eq system-type 'ms-dos)
901 (progn (send-string-to-terminal "\e[2J") ;if MS-DOS
902 (call-process shell-file-name "con" "*dvi-preview*" nil
903 "/c " dvi2-command preview-file)
904 (redraw-display))
905 (start-process "xdvi" "*dvi-preview*" shell-file-name "-c"
906 (concat dvi2-command " " preview-file)) ;if UNIX
907 (message (concat "Starting " dvi2-command " to preview " preview-file)))
908 )
909 )
911 (defun YaTeX-prev-error ()
912 "Visit previous error. The reason why not NEXT-error is to
913 avoid make confliction of line numbers by editing."
914 (interactive)
915 (let ((cur-buf (buffer-name))
916 YaTeX-error-line error-buffer)
917 (if (null (get-buffer YaTeX-typeset-buffer))
918 (message "There is no output buffer of typesetting.")
919 (pop-to-buffer YaTeX-typeset-buffer)
920 (if (eq system-type 'ms-dos)
921 (if (search-backward latex-dos-emergency-message nil t)
922 (progn (goto-char (point-max))
923 (setq error-regexp latex-error-regexp))
924 (beginning-of-line)
925 (forward-char -1)
926 (setq error-regexp latex-warning-regexp))
927 (if YaTeX-typesetting-process ; if jlatex on UNIX
928 (if (eq (process-status YaTeX-typesetting-process) 'run)
929 (progn
930 (goto-char (point-max))
931 (setq error-regexp latex-error-regexp)))
932 (beginning-of-line)
933 (setq error-regexp latex-warning-regexp)))
934 (if (re-search-backward error-regexp nil t)
935 (save-restriction
936 (set-mark-command nil)
937 (end-of-line)
938 (narrow-to-region (point) (mark))
939 (goto-char (point-min))
940 (re-search-forward "[0-9]")
941 (forward-char -1)
942 (set-mark (point))
943 (skip-chars-forward "0-9")
944 (narrow-to-region (point) (mark))
945 (goto-char (point-min))
946 (setq YaTeX-error-line (read (current-buffer))))
947 (message "No more error on %s" cur-buf)
948 (ding)
949 )
950 (setq error-buffer (YaTeX-get-error-file cur-buf))
951 (other-window -1)
952 (switch-to-buffer cur-buf)
953 (if (null YaTeX-error-line)
954 nil
955 ;; if warning or error found
956 (YaTeX-switch-to-buffer error-buffer)
957 (goto-line YaTeX-error-line)
958 (message "latex error or warning in '%s' at line: %d"
959 error-buffer YaTeX-error-line)
960 (other-window 1)
961 (skip-chars-backward "[0-9]")
962 (recenter (/ (window-height) 2))
963 (sit-for 3)
964 (forward-line -1)
965 (other-window -1)
966 )))
967 )
969 (defun YaTeX-jump-error-line ()
970 "Jump corresponding line on latex command's error message."
971 (interactive)
972 (let ((end (progn (end-of-line) (point)))
973 (begin (progn (beginning-of-line)(point))))
974 (if (null (re-search-forward "l[ ines]*\\.*[1-9][0-9]*" end t))
975 (message "No line number expression")
976 (goto-char (match-beginning 0))
977 (re-search-forward "[1-9][0-9]*" end t)
978 (save-restriction
979 (let ((error-line
980 (string-to-int (buffer-substring (match-beginning 0)
981 (match-end 0))))
982 (error-file (YaTeX-get-error-file current-TeX-buffer)))
983 (goto-char (match-beginning 0))
984 (other-window -1)
985 (message "errors in %s" error-file)
986 ;(switch-to-buffer current-TeX-buffer)
987 (if (not (YaTeX-switch-to-buffer error-file))
988 (error "%s is not found in this directory."))
989 (goto-line error-line)))))
990 )
992 (defun YaTeX-view-error ()
993 (interactive)
994 (if (null (get-buffer YaTeX-typeset-buffer))
995 (message "No typeset buffer found.")
996 (pop-to-buffer YaTeX-typeset-buffer)
997 (goto-char (point-max))
998 (recenter -1)
999 (other-window -1))
1002 (defun YaTeX-get-error-file (default)
1003 "Get current processing file by tex message."
1004 (let (file-name)
1005 (save-excursion
1006 (if (re-search-backward "([-A-Z_a-z0-9]+.tex" (point-min) t)
1007 (buffer-substring (1+ (match-beginning 0)) (match-end 0))
1008 default)))
1011 (defun YaTeX-put-nonstopmode ()
1012 (if YaTeX-need-nonstop
1013 (if (re-search-backward "\\nonstopmode{}" (point-min) t)
1014 nil ;if already written in text then do nothing
1015 (save-excursion
1016 (goto-char (point-min))
1017 (insert "\\nonstopmode{}%_YaTeX_%\n")))
1021 (defun YaTeX-remove-nonstopmode ()
1022 (if YaTeX-need-nonstop ;for speed
1023 (save-excursion
1024 (goto-char (point-min))
1025 (forward-line 1)
1026 (narrow-to-region (point-min) (point))
1027 (goto-char (point-min))
1028 (delete-matching-lines "^\\\\nonstopmode\\{\\}%_YaTeX_%$")
1029 (widen)))
1032 (defun YaTeX-typeset-menu (arg)
1033 "Typeset, preview, visit error and miscellaneous convinient menu."
1034 (interactive "P")
1035 (message "J)latex R)egion P)review V)iewerror L)pr")
1036 (let ((c (read-char)))
1037 (cond
1038 ((= c ?j) (YaTeX-typeset-buffer))
1039 ((= c ?r) (YaTeX-typeset-region))
1040 ((= c ?p) (call-interactively 'YaTeX-preview))
1041 ((= c ?v) (YaTeX-view-error))
1042 ((= c ?l) (YaTeX-lpr arg))
1043 ((= c ?b) (YaTeX-insert-string "\\"))))
1046 (defun YaTeX-get-preview-file-name ()
1047 "Get file name to preview by inquiring YaTeX-get-latex-command"
1048 (let* ((latex-cmd (YaTeX-get-latex-command t))
1049 (fname (substring latex-cmd (1+ (rindex latex-cmd ? ))))
1050 (period))
1051 (if (eq fname "")
1052 (setq fname (substring (buffer-name) 0 -4))
1053 (setq period (rindex fname ?.))
1054 (setq fname (substring fname 0 (if (eq -1 period) nil period)))
1055 ))
1058 (defun YaTeX-get-latex-command (switch)
1059 "Specify the latex-command name and its argument.
1060 If there is a line which begins by string: \"%#!\", the following
1061 strings are assumed to be the latex-command and arguments. The
1062 default value of latex-command is:
1063 tex-command (buffer-name)
1064 and if you write \"%#!jlatex\" in the beginning of certain line.
1065 \"jlatex \" (buffer-name)
1066 will be the latex-command,
1067 and you write \"%#!jlatex main.tex\" on some line and argument SWITCH
1068 is t, then
1069 \"jlatex main.tex\"
1070 will be given to the shell."
1071 (let*
1072 ((default-command
1073 (concat tex-command " "
1074 (if switch (buffer-name) ""))));default value
1075 (save-excursion
1076 (goto-char (point-min))
1077 (if (null (re-search-forward "^%#!" (point-max) t))
1078 default-command
1079 (skip-chars-forward "%#! ")
1080 (if (eolp)
1081 default-command
1082 (let ((s (point)))
1083 (skip-chars-forward "A-z") ;Skip command name
1084 ;(setq YaTeX-latex-command (buffer-substring s (point)))
1085 (cond
1086 ((null switch)
1087 (buffer-substring s (point)))
1088 ((eolp) ;Only return command name
1089 (concat (buffer-substring s (point)) " " (buffer-name)))
1090 (t(end-of-line) ;Change entire command name
1091 (buffer-substring s (point))) ;including arguments.
1092 ))
1093 ))))
1096 (defun YaTeX-get-builtin (key)
1097 "Read source built-in command of %# usage."
1098 (save-excursion
1099 (goto-char (point-min))
1100 (if (and (search-forward (concat "%#" key) nil t)
1101 (not (eolp)))
1102 (buffer-substring
1103 (progn (skip-chars-forward " " (point-end-of-line))(point))
1104 (point-end-of-line))
1105 nil))
1108 (defun YaTeX-goto-corresponding-environment ()
1109 "Go to corresponding begin/end enclosure."
1110 (interactive)
1111 (if (not (YaTeX-on-begin-end-p)) nil
1112 (let ((p (match-end 0)) env (nest 0) regexp re-s
1113 (m0 (match-beginning 0))
1114 (m1 (match-beginning 1))
1115 (m2 (match-beginning 2)))
1116 (if (not
1117 (save-excursion
1118 (goto-char p)
1119 (search-forward "}" (point-end-of-line) t)))
1120 (error "Unterminated brackets for begin/end"))
1121 (setq env (buffer-substring p (match-beginning 0))) ;get current env
1122 (if (cond
1123 ((equal m0 m1) ;if begin{xxx}
1124 (setq regexp (concat "\\(\\\\end{" env "}\\)\\|"
1125 "\\(\\\\begin{" env "}\\)"))
1126 (fset re-s 're-search-forward))
1127 ((equal m0 m2) ;if end{xxx}
1128 (setq regexp (concat "\\(\\\\begin{" env "}\\)\\|"
1129 "\\(\\\\end{" env "}\\)"))
1130 (fset re-s 're-search-backward))
1131 (error "Corresponding environment not found."))
1132 (while (and (>= nest 0) (funcall re-s regexp nil t))
1133 (if (eq (match-beginning 0) m0) nil
1134 (setq nest (if (eq (match-beginning 0) (match-beginning 1))
1135 (1- nest) (1+ nest))))))
1136 (beginning-of-line));let
1137 t); if on begin/end line
1140 (defun YaTeX-goto-corresponding-file ()
1141 "Visit or switch buffer of corresponding file, looking at \\input or
1142 \\include or \includeonly on current line."
1143 (if (not (YaTeX-on-includes-p)) nil
1144 (beginning-of-line)
1145 (skip-chars-forward "^{")
1146 (let ((input-file
1147 (concat
1148 (buffer-substring (1+ (point))
1149 (progn (skip-chars-forward "^ ,}") (point)))
1150 ".tex")))
1151 (YaTeX-switch-to-buffer input-file)
1153 t);if on \input or \include line.
1156 (defun YaTeX-goto-corresponding-BEGIN-END ()
1157 (if (not (YaTeX-on-BEGIN-END-p)) nil
1158 (if (cond
1159 ((equal (match-beginning 0) (match-beginning 1)) ;if on %#BEGIN
1160 (not (search-forward "%#END" nil t)))
1161 (t ; if on %#END
1162 (not (search-backward "%#BEGIN" nil t))))
1163 (error "Corresponding %#BEGIN/END not found."))
1164 (beginning-of-line)
1165 t)
1168 (defun YaTeX-switch-to-buffer (file)
1169 "Switch to buffer if buffer exists, find file if not."
1170 (interactive "Fswitch to file: ")
1171 (if (get-buffer file)
1172 (progn (switch-to-buffer file) t)
1173 (if (file-exists-p file)
1174 (progn (find-file file) t)
1175 (message "%s was not found in this directory." file)
1176 nil))
1179 (defun YaTeX-switch-to-buffer-other-window (file)
1180 "Switch to buffer if buffer exists, find file if not."
1181 (interactive "Fswitch to file: ")
1182 (if (get-buffer file)
1183 (progn (switch-to-buffer-other-window file) t)
1184 (if (file-exists-p file)
1185 (progn (find-file-other-window file) t)
1186 (message "%s was not found in this directory." file)
1187 nil))
1190 (defmacro YaTeX-main-file-p ()
1191 "Return if current buffer is main LaTeX source."
1192 (string-match (concat "^" (YaTeX-get-preview-file-name) ".tex")(buffer-name))
1195 (defun YaTeX-visit-main ()
1196 "Switch to buffer main LaTeX source."
1197 (interactive)
1198 (let ((main-file (YaTeX-get-preview-file-name)))
1199 (if (string-match (concat "^" main-file ".tex") (buffer-name))
1200 (message "I think this is main LaTeX source.")
1201 (YaTeX-switch-to-buffer (concat main-file ".tex"))))
1202 nil
1205 (defun YaTeX-visit-main-other-window ()
1206 "Switch to buffer main LaTeX source in other window."
1207 (interactive)
1208 (if (YaTeX-main-file-p) (message "I think this is main LaTeX source.")
1209 (YaTeX-switch-to-buffer-other-window
1210 (concat (YaTeX-get-preview-file-name) ".tex")))
1213 (defun YaTeX-on-begin-end-p ()
1214 (save-excursion
1215 (beginning-of-line)
1216 (re-search-forward
1217 "\\(\\\\begin{\\)\\|\\(\\\\end{\\)" (point-end-of-line) t))
1219 (defun YaTeX-on-includes-p ()
1220 (save-excursion
1221 (beginning-of-line)
1222 (re-search-forward "\\(\\(include.*\\)\\|\\(input\\)\\){.*}"
1223 (point-end-of-line) t))
1225 (defun YaTeX-on-BEGIN-END-p ()
1226 (save-excursion
1227 (let ((case-fold-sea nil))
1228 (beginning-of-line)
1229 (re-search-forward "\\(%#BEGIN\\)\\|\\(%#END\\)" (point-end-of-line) t)))
1231 (defun YaTeX-goto-corresponding-* ()
1232 "Parse current line and call suitable function."
1233 (interactive)
1234 (cond
1235 ((YaTeX-goto-corresponding-environment))
1236 ((YaTeX-goto-corresponding-file))
1237 ((YaTeX-goto-corresponding-BEGIN-END))
1238 (t (message "I don't know where to go.")))
1241 (defun YaTeX-comment-region (alt-prefix)
1242 "Comment out region by '%'. If you call this function on the 'begin{}' or
1243 'end{}' line, it comments out whole environment"
1244 (interactive "P")
1245 (if (not (YaTeX-on-begin-end-p))
1246 (comment-region
1247 (if alt-prefix
1248 (read-string "Insert prefix: ")
1249 YaTeX-comment-prefix))
1250 (YaTeX-comment-uncomment-env 'comment-region))
1253 (defun YaTeX-uncomment-region (alt-prefix)
1254 "Uncomment out region by '%'."
1255 (interactive "P")
1256 (if (not (YaTeX-on-begin-end-p))
1257 (uncomment-region
1258 (if alt-prefix (read-string "Remove prefix: ")
1259 YaTeX-comment-prefix))
1260 (YaTeX-comment-uncomment-env 'uncomment-region))
1263 (defun YaTeX-comment-uncomment-env (func)
1264 "Comment or uncomment out one LaTeX environment switching function by FUNC."
1265 (save-excursion
1266 (if (eq (match-beginning 0) (match-beginning 2)) ; if on the '\end{}' line
1267 (YaTeX-goto-corresponding-environment)) ; goto '\begin{}' line
1268 (beginning-of-line)
1269 (push-mark (point) t)
1270 (YaTeX-goto-corresponding-environment)
1271 (forward-line 1)
1272 (funcall func YaTeX-comment-prefix t) ; t makes uncomment once
1276 (defun YaTeX-mark-environment ()
1277 "Not implemented yet."
1280 (defun YaTeX-comment-paragraph ()
1281 "Comment out current paragraph."
1282 (interactive)
1283 (save-excursion
1284 (if (YaTeX-on-begin-end-p)
1285 (progn
1286 (beginning-of-line)
1287 (insert YaTeX-comment-prefix)
1288 (YaTeX-goto-corresponding-environment)
1289 (beginning-of-line)
1290 (insert YaTeX-comment-prefix))
1291 (mark-paragraph)
1292 (if (not (bobp)) (forward-line 1))
1293 (comment-region "%")))
1296 (defun YaTeX-uncomment-paragraph ()
1297 "Uncomment current paragraph."
1298 (interactive)
1299 (save-excursion
1300 (if (YaTeX-on-begin-end-p)
1301 (progn
1302 (YaTeX-remove-prefix YaTeX-comment-prefix t)
1303 (YaTeX-goto-corresponding-environment)
1304 (YaTeX-remove-prefix YaTeX-comment-prefix t))
1305 (let ((prefix fill-prefix))
1306 (setq fill-prefix "")
1307 (mark-paragraph)
1308 (if (not (bobp)) (forward-line 1))
1309 (uncomment-region "%")
1310 (setq fill-prefix prefix))))
1313 (defun YaTeX-remove-prefix (prefix &optional once)
1314 "Remove prefix on current line so far as prefix detected. But
1315 optional argument ONCE makes deletion once."
1316 (interactive "sPrefix:")
1317 (beginning-of-line)
1318 (while (re-search-forward (concat "^" prefix) (point-end-of-line) t)
1319 (replace-match "")
1320 (if once (end-of-line)))
1323 (defun YaTeX-kill-some-pairs (predicate gofunc)
1324 "Kill some matching pair."
1325 (interactive)
1326 (if ;(not (YaTeX-on-begin-end-p)) nil
1327 (not (funcall predicate)) nil
1328 (save-excursion
1329 (push-mark (point) t)
1330 ;(YaTeX-goto-corresponding-environment)
1331 (funcall gofunc)
1332 (beginning-of-line)
1333 (kill-line 1)
1334 (exchange-point-and-mark)
1335 (beginning-of-line)
1336 (kill-line 1)
1337 t))
1340 (defun YaTeX-read-environment (prompt)
1341 "Read the LaTeX environment name with completion."
1342 (let ((env
1343 (completing-read prompt (append user-env-table env-table) nil nil)))
1344 (if (not (assoc env (append user-env-table env-table)))
1345 (setq user-env-table (cons (list env) user-env-table)
1346 YaTeX-user-table-modified t))
1347 env)
1350 (defun YaTeX-change-environment ()
1351 "Change the name of environment."
1352 (interactive)
1353 (if (not (YaTeX-on-begin-end-p)) nil
1354 (save-excursion
1355 (let (p env)
1356 (beginning-of-line)
1357 (skip-chars-forward "^{")
1358 (forward-char 1)
1359 (setq p (point))
1360 (skip-chars-forward "^}")
1361 (setq env (buffer-substring p (point)))
1362 (beginning-of-line)
1363 (set-mark-command nil)
1364 (YaTeX-goto-corresponding-environment)
1365 (setq newenv (YaTeX-read-environment
1366 (format "Change environment `%s' to: " env)))
1367 (if (string= newenv "")
1368 (message "Change environment cancelled.")
1369 (search-forward (concat "{" env) (point-end-of-line) t)
1370 (replace-match (concat "{" newenv))
1371 (exchange-point-and-mark)
1372 (search-forward (concat "{" env) (point-end-of-line) t)
1373 (replace-match (concat "{" newenv)))
1374 t)))
1377 (defun YaTeX-kill-* ()
1378 "Parse current line and call suitable function."
1379 (interactive)
1380 (cond
1381 ((YaTeX-kill-some-pairs 'YaTeX-on-begin-end-p
1382 'YaTeX-goto-corresponding-environment))
1383 ((YaTeX-kill-some-pairs 'YaTeX-on-BEGIN-END-p
1384 'YaTeX-goto-corresponding-BEGIN-END))
1385 (t (message "I don't know what to kill.")))
1388 (defun YaTeX-change-* ()
1389 "Parse current line and call suitable function."
1390 (interactive)
1391 (cond
1392 ((YaTeX-change-environment))
1393 (t (message "I don't know what to change.")))
1396 (defun YaTeX-addin (name)
1397 "Check availability of addin function and call it if exists."
1398 (if (fboundp (intern-soft (concat "YaTeX:" name)))
1399 (funcall (intern (concat "YaTeX:" name))))
1402 (defun YaTeX-in-environment-p (env)
1403 "Return if current LaTeX environment is ENV."
1404 (let ((cur-env (YaTeX-inner-environment)) p)
1405 (cond
1406 ((atom env) (equal env cur-env))
1407 ((listp env)
1408 (while (and env (not p))
1409 (setq p (equal (car env) cur-env))
1410 (setq env (cdr env)))
1411 p)))
1414 (defun YaTeX-quick-in-environment-p (env)
1415 "Check quickly but unsure if current environment is ENV."
1416 (let ((p (point))q)
1417 (while (and (not q) (search-backward (concat "\\begin{" env "}")nil t))
1418 ;;(goto-char (match-beginning 0))
1419 (if (search-backward "%" (point-beginning-of-line) t) nil
1420 (setq q t)))
1421 (if q (setq q (not (re-search-forward
1422 (concat "^[ ]*\\\\end{" env "}") p t))))
1423 (goto-char p)
1424 q)
1427 (defun YaTeX-remove-trailing-comment ()
1428 "Remove trailing comment in current line."
1429 (if (re-search-forward "[^\\\\]\\(%\\)" (point-end-of-line) t)
1430 (delete-region (match-beginning 1) (point-end-of-line)))
1433 (defun YaTeX-fill-item ()
1434 "Fill item in itemize environment."
1435 (interactive)
1436 (save-excursion
1437 (let* ((p (point))
1438 (bndry (prog2 (search-backward "\\begin{" nil t) (point)
1439 (goto-char p)))
1440 (item-term "\\(^$\\)\\|\\(\\\\item\\)\\|\\(\\\\end\\)")
1441 fill-prefix start s2 col)
1442 (end-of-line)
1443 (if (not (re-search-backward "\\\\item" bndry t))
1444 (error "\\item not found."))
1445 (skip-chars-forward "^ " (point-end-of-line))
1446 (skip-chars-forward " " (point-end-of-line))
1447 (if (not (eolp)) nil
1448 (forward-line 1)
1449 (skip-chars-forward " "))
1450 (setq start (point-beginning-of-line))
1451 (setq col (current-column))
1452 (YaTeX-remove-trailing-comment) ;should restrict to NTT-jTeX?
1453 (forward-line 1)
1454 (skip-chars-forward " ")
1455 (if (looking-at item-term) nil
1456 (delete-region (point) (point-beginning-of-line))
1457 (indent-to col)
1458 (setq s2 (point))
1459 (setq fill-prefix
1460 (buffer-substring (point-beginning-of-line)(point)))
1461 (YaTeX-remove-trailing-comment);should restrict to NTT-jTeX?
1462 (re-search-forward item-term nil 1)
1463 (beginning-of-line)
1464 (push-mark (point) t)
1465 (while (> (point) s2)
1466 (forward-line -1)
1467 (skip-chars-forward " ")
1468 (delete-region (point) (point-beginning-of-line))
1469 (YaTeX-remove-trailing-comment))
1470 (fill-region-as-paragraph start (mark))
1471 (if NTT-jTeX
1472 (while (progn(forward-line -1)(end-of-line) (> (point) start))
1473 (insert ?%)))
1474 (pop-mark))
1475 ))
1478 (defun YaTeX-fill-* ()
1479 "Fill paragraph according to its condition."
1480 (interactive)
1481 (cond
1482 ((YaTeX-fill-item))
1486 (defun YaTeX-save-buffers ()
1487 "Save buffers with `.tex' extension."
1488 (basic-save-buffer)
1489 (save-excursion
1490 (mapcar '(lambda (buf)
1491 (set-buffer buf)
1492 (if (and (buffer-file-name buf)
1493 (string-match "\\.tex$" (buffer-file-name buf))
1494 (buffer-modified-p buf)
1495 (y-or-n-p (format "Save %s" (buffer-name buf))))
1496 (save-buffer buf)))
1497 (buffer-list)))
1500 (defun YaTeX-read-accent-char (x)
1501 "Read char in accent braces."
1502 (let ((c (read-char)))
1503 (concat
1504 (if (and (or (= c ?i) (= c ?j))
1505 (not (string-match (regexp-quote x) "cdb")))
1506 "\\" "")
1507 (char-to-string c)))
1510 (defun YaTeX-make-accent ()
1511 "Make accent usage."
1512 (interactive)
1513 (message "1:` 2:' 3:^ 4:\" 5:~ 6:= 7:. u v H t c d b")
1514 (let ((c (read-char))(case-fold-search nil))
1515 (setq c (cond ((and (> c ?0) (< c ?8))
1516 (substring "`'^\"~=." (1- (- c ?0)) (- c ?0)))
1517 ((= c ?h) "H")
1518 (t (char-to-string c))))
1519 (if (not (string-match c "`'^\"~=.uvHtcdb")) nil
1520 (insert "\\" c "{}")
1521 (backward-char 1)
1522 (insert (YaTeX-read-accent-char c))
1523 (if (string= c "t") (insert (YaTeX-read-accent-char c)))
1524 (forward-char 1)))
1527 (defun YaTeX-replace-format (string format repl)
1528 "In STRING, replace first appearance of FORMAT to REPL as if
1529 function `format' does. FORMAT does not contain `%'"
1530 (let ((beg (or (string-match (concat "^\\(%" format "\\)") string)
1531 (string-match (concat "[^%]\\(%" format "\\)") string)))
1532 (len (length format)))
1533 (if (null beg) string ;no conversion
1534 (concat
1535 (substring string 0 (match-beginning 1)) repl
1536 (substring string (match-end 1)))))
1538 (defun YaTeX-lpr (arg)
1539 "Print out. If prefix arg ARG is non nil, call print driver without
1540 page range description."
1541 (interactive "P")
1542 (let*(from to (cmd (or (YaTeX-get-builtin "LPR") dviprint-command-format)))
1543 (setq
1544 cmd (YaTeX-replace-format
1545 cmd
1546 "f"
1547 (if arg
1548 ""
1549 (YaTeX-replace-format
1550 dviprint-from-format
1551 "b"
1552 (if (string=
1553 (setq from (read-string "From page(default 1): ")) "")
1554 "1" from)))))
1555 (setq
1556 cmd (YaTeX-replace-format
1557 cmd
1558 "t"
1559 (if (or arg
1560 (string=
1561 (setq to (read-string "To page(default none): ")) ""))
1562 ""
1563 (YaTeX-replace-format dviprint-to-format "e" to))))
1564 (setq cmd (read-string "Edit command line: "
1565 (format cmd (YaTeX-get-preview-file-name))))
1566 (with-output-to-temp-buffer "*dvi-printing*"
1567 (if (eq system-type 'ms-dos)
1568 (call-process shell-file-name "con" "*dvi-printing*" nil
1569 "/c " cmd)
1570 (start-process "print" "*dvi-printing*" shell-file-name "-c" cmd)
1571 (message (concat "Starting " cmd " to printing "
1572 (YaTeX-get-preview-file-name))))
1573 ))
1576 (defun YaTeX-read-user-completion-table ()
1577 "Append user completion table of LaTeX word"
1578 (message "Loading personal completion table")
1579 (let ((user-table (expand-file-name YaTeX-user-completion-table)))
1580 (if (file-exists-p user-table)
1581 (load-file user-table)
1582 (message "Personal completion table not found."))
1583 ))
1585 (defun YaTeX-save-table ()
1586 "Save personal completion table as dictionary."
1587 (interactive)
1588 (if (not YaTeX-user-table-modified)
1589 nil
1590 (message "Saving user table in %s" YaTeX-user-completion-table)
1591 (find-file (expand-file-name YaTeX-user-completion-table))
1592 (erase-buffer)
1593 ;; (prin1-to-string user-section-table)
1594 (insert "(setq user-section-table '(\n")
1595 (mapcar '(lambda (s)
1596 (insert (prin1-to-string s))
1597 (insert "\n"))
1598 user-section-table)
1599 (insert "))\n\n")
1601 (insert "(setq user-article-table '(\n")
1602 (mapcar '(lambda (s)
1603 (insert (prin1-to-string s))
1604 (insert "\n"))
1605 user-article-table)
1606 (insert "))\n\n")
1608 (insert "(setq user-env-table '(\n")
1609 (mapcar '(lambda (s)
1610 (insert (prin1-to-string s))
1611 (insert "\n"))
1612 user-env-table)
1613 (insert "))\n\n")
1615 (insert "(setq user-fontsize-table '(\n")
1616 (mapcar '(lambda (s)
1617 (insert (prin1-to-string s))
1618 (insert "\n"))
1619 user-fontsize-table)
1620 (insert "))\n\n")
1622 (insert "(setq user-singlecmd-table '(\n")
1623 (mapcar '(lambda (s)
1624 (insert (prin1-to-string s))
1625 (insert "\n"))
1626 user-singlecmd-table)
1627 (insert "))\n")
1629 (basic-save-buffer)
1630 (kill-buffer (current-buffer))
1631 (message "")
1632 (setq YaTeX-user-table-modified nil))
1635 ;; --------------- General sub functions ---------------
1637 ;(defun index (string char)
1638 ; (let ((pos 0)(len (1- (length string)))(index -1))
1639 ; (while (<= pos len)
1640 ; (cond
1641 ; ((= (aref string pos) char)
1642 ; (setq index pos) (setq pos len))
1643 ; (t (setq pos (1+ pos))))
1644 ; )
1645 ; index)
1646 ;)
1648 (defun rindex (string char)
1649 (let ((pos (1- (length string)))(index -1))
1650 (while (>= pos 0)
1651 (cond
1652 ((= (aref string pos) char)
1653 (setq index pos) (setq pos -1))
1654 (t (setq pos (1- pos))))
1656 index)
1659 (defun append-to-hook (hook hook-list)
1660 "Add hook-list to certain emacs's hook correctly.
1661 Argument hook-list is the list of function int the form to be called
1662 Call this function with argument as next example,
1663 (append-to-hook '((ding))) ;If one function to add.
1664 (append-to-hook '((func1)(func2 arg)))."
1665 (if (null (symbol-value hook)) ;Not defined
1666 (set hook
1667 (append '(lambda ()) hook-list))
1668 (if (listp (symbol-value hook))
1669 (if (eq (car (symbol-value hook)) 'lambda) ;'(lambda () ....)
1670 (set hook
1671 (append (symbol-value hook) hook-list))
1672 (if (eq hook 'kill-emacs-hook) ;'(hook1 hook2 ...)
1673 (progn ; this format is not
1674 (ding) ; for kill-emacs-hook
1675 (message
1676 "Caution!! you have wrong format of kill-emacs-hook"))
1677 (while (not (null hook-list))
1678 (set hook
1679 (append (symbol-value hook) (car hook-list)))
1680 (setq hook-list (cdr hook-list))))
1682 (set hook ;'hook
1683 (append '(lambda ())
1684 (cons (list (symbol-value hook)) hook-list)))))
1686 (append-to-hook 'kill-emacs-hook '((YaTeX-save-table)))
1688 ;--------------------------------- History ---------------------------------
1689 ; Rev. | Date | Contents
1690 ;------+----------+---------------------------------------------------------
1691 ; 1.00 | 91/ 6/13 | Initial version.
1692 ; | | Auto compilation & preview.
1693 ; | | \section{}-type and \begin{}\end{}-type completion.
1694 ; 1.01 | 91/ 6/14 | Add {\large ..} type completion (prefix+l).
1695 ; 1.10 | 6/21 | Add learning feature of completion.
1696 ; 1.11 | 6/27 | Simplify function begin-document etc. using lambda.
1697 ; 1.12 | 7/ 6 | Modify YaTeX-make-section, show section-name.
1698 ; 1.13 | 12/ 4 | Delete blank lines in make begin/end environment.
1699 ; 1.20 | 12/ 5 | Saving learned completion into user file.
1700 ; 1.21 | 12/ 6 | Add \maketitle type completion (prefix+m).
1701 ; 1.22 | 12/30 | Port yatex.el to DOS(Demacs).
1702 ; 1.23 | 92/ 1/ 8 | Enable latex and preview command on DOS.
1703 ; 1.24 | 1/ 9 | Add YaTeX-save-table to kill-emacs-hook automatically.
1704 ; 1.25 | 1/16 | YaTeX-do-completion (prefix+SPC) and argument
1705 ; | | acceptable YaTeX-make-section work. Put region into
1706 ; | | \begin...\end by calling YaTeX-make-begin-end with ARG.
1707 ; | | append-kill-emacs-hook was revised to append-to-hook.
1708 ; 1.26 | 1/18 | Region mode is added to {\large }. Default fontsize.
1709 ; 1.27 | 1/21 | Default name on completing-read.
1710 ; 1.28 | 7/ 2 | Add \nonstopmode{} automatically on DOS.
1711 ; | 7/20 | %#! usage to specify latex command and its arguments.
1712 ; | | Change default fill-prefix from TAB to null string.
1713 ; 1.29 | 7/21 | Add YaTeX-end-environment.
1714 ; 1.30 | 9/26 | Support project 30 lines(other than 25 lines).
1715 ; 1.31 | 10/28 | Variable argument for previewer from %#! usage.
1716 ; 1.32 | 11/16 | YaTeX-goto-corresponding-environment.
1717 ; | | Comment out region/paragraph added.
1718 ; 1.33 | 11/29 | Variable default value, on DOS and other OS.
1719 ; | | Make dvi2-command buffer local. Change the behavior of
1720 ; | | comment out region/paragraph on the \begin{} or \end{}
1721 ; | | line. Make YaTeX-end-environment faster. Add YaTeX-
1722 ; | | define-key, YaTeX-define-begend-(region-)key.
1723 ; 1.34 | 12/26 | YaTeX-goto-corresponding-* automatically choose its move.
1724 ; | | YaTeX-prev-error supports separate typesetting.
1725 ; 1.35 | 93/ 1/25 | YaTeX-kill-environment erases pair of begin/end.
1726 ; | | YaTeX-change-environment change the environment name.
1727 ; | | Auto indent at YaTeX-make-begin-end.
1728 ; 1.36 | 1/27 | YaTeX-typeset-region typesets the region from %#BEGIN to
1729 ; | | %#END, or simple region between point and mark.
1730 ; 1.37 | 2/12 | YaTeX-kill-environment turns YaTeX-kill-some-pairs and
1731 ; | | now it can kill %#BEGIN and %#END pairs.
1732 ; | | Now YaTeX-goto-corresponding-environment detects nested
1733 ; | | environment. Put " by " in verbatim. Auto save buffers
1734 ; | | with quiery. Add current file to includeonly list
1735 ; | | automatically. Support YaTeX-fill-item, YaTeX-make-
1736 ; | | accent, YaTeX-visit-main-other-window.
1737 ; | | [prefix] tl for lpr. Revise YaTeX-view-error.
1738 ;------+----------+---------------------------------------------------------
1740 ;----------------------------- End of yatex.el -----------------------------