yatex

view yatex.el @ 3:191610912c8b

Add comment out region/paragraph. Add YaTeX-goto-corresponding-environment.
author yuuji
date Thu, 19 Nov 1992 02:27:25 +0000
parents 912f6e258cba
children 78dfe28b6a35
line source
1 ;;; -*- Emacs-Lisp -*-
2 ;;; Yet Another tex-mode for emacs.
3 ;;; yatex.el rev.1.32
4 ;;; (c)1991 by Hirose Yuuji.[yuuji@ae.keio.ac.jp]
5 ;;; Last modified Tue Nov 17 01:01:12 1992 on 98fa
7 (provide 'yatex-mode)
8 (require 'comment)
9 (defconst YaTeX-revision-number "1.32"
10 "Revision number of running yatex.el"
11 )
13 ;---------- Local variables ----------
14 ;;;
15 ;; Initialize local variable for yatex-mode.
16 ;; Preserving user preferred definitions.
17 ;; ** Check all of these defvar-ed values **
18 ;; ** and setq other values more suitable **
19 ;; ** for your site, if nedded. **
20 ;;;
21 (defvar YaTeX-prefix "\^C"
22 "Prefix key to trigger YaTeX functions.
23 You can select favorite prefix key by setq in your ~/.emacs."
24 )
25 (defvar YaTeX-open-lines 1
26 "Blank lines between text and \??{??}"
27 )
28 (defvar YaTeX-fill-prefix ""
29 "fill-prefix used for auto-fill-mode.
30 The defalut value is single TAB."
31 )
32 (defvar YaTeX-user-completion-table "~/.yatexrc"
33 "Default filename in which user completion table is saved."
34 )
35 (defvar tex-command "jlatex"
36 "Default command for compiling LaTeX text."
37 )
38 (defvar dvi2-command ;previewer command for your site
39 (concat
40 "xdvi -geo +0+0 -s 4 -display "
41 (getenv "DISPLAY"))
42 "Default previewer command including its option.
43 This default value is for X window system. If you want to use this
44 default, you have to make sure the environment variable DISPLAY is
45 correctly set."
46 )
47 (defvar latex-warning-regexp "line.* [0-9]*"
48 "Regular expression of line number of warning message by latex command."
49 )
50 (defvar latex-error-regexp "l\\.[1-9][0-9]*"
51 "Regular expression of line number of latex error. Perhaps your latex
52 command stops at this error message with line number of LaTeX source text."
53 )
54 (defvar latex-dos-emergency-message
55 "Emergency stop" ;<- for Micro tex, ASCII-pTeX 1.6
56 "Because Demacs (GNU Emacs on DOS) cannot have pararell process, the
57 latex command which is stopping on a LaTeX error, is terminated by Demacs.
58 Many latex command on DOS display some message when it is terminated by
59 other process, user or OS. Define this variable a message string of your
60 latex command on DOS shows at abnormal termination.
61 Remember Demacs's call-process function is not oriented for interactive
62 process."
63 )
65 ;------------ Completion table ------------
66 ; Set tex-section-like command possible completion
67 (setq section-table
68 '(("part") ("section") ("subsection") ("subsubsection")
69 ("author") ("documentstyle") ("pagestyle")
70 ("documentstyle[10pt]") ("documentstyle[11pt]")
71 ("documentstyle[12pt]")
72 ("title") ("chapter") ("part") ("paragraph")
73 ("subparagraph") ("underline") ("label") ("footnote")
74 ("hspace*") ("vspace*") ("bibliography") ("bibitem[]") ("cite[]")
75 ("nocite") ("input") ("include") ("includeonly") ("mbox") ("hbox")
76 ("caption") ("newcommand") ("setlength") ("addtolength")
77 ("newenvironment") ("newtheorem")
78 ))
79 (defvar user-section-table nil)
81 ; Set style possible completion
82 (setq article-table
83 '(("article") ("jarticle") ("report") ("jreport") ("jbook")
84 ("4em") ("2ex")
85 ("empty") ("headings") ("\\textwidth")
86 ("\\oddsidemargin") ("\\evensidemargin")
87 ("\\textheight") ("\\topmargin")
88 ("\\bottommargin") ("\\footskip") ("\\footheight")
89 ))
90 (defvar user-article-table nil)
92 ; Set tex-environment possible completion
93 (setq env-table
94 '(("quote") ("quotation") ("center") ("verse") ("document")
95 ("verbatim") ("itemize") ("enumerate") ("description")
96 ("list{}") ("tabular") ("table") ("titlepage")
97 ("sloppypar") ("ref") ("quotation") ("quote") ("picture")
98 ("eqnarray") ("figure") ("equation") ("abstract") ("array")
99 ("thebibliography") ("theindex")
100 ))
101 (defvar user-env-table nil)
103 ; Set {\Large }-like comletion
104 (setq fontsize-table
105 '(("rm") ("em") ("bf") ("boldmath") ("it") ("sl") ("sf") ("sc") ("tt")
106 ("dg") ("dm")
107 ("tiny") ("scriptsize") ("footnotesize") ("small")("normalsize")
108 ("large") ("Large") ("LARGE") ("huge") ("Huge")
109 ))
110 (defvar user-fontsize-table nil)
112 (setq singlecmd-table
113 '(("maketitle") ("sloppy")
114 ("alpha") ("beta") ("gamma") ("delta") ("epsilon") ("varepsilon")
115 ("zeta") ("eta") ("theta")("vartheta") ("iota") ("kappa")
116 ("lambda") ("mu") ("nu") ("xi") ("pi") ("varpi") ("rho") ("varrho")
117 ("sigma") ("varsigma") ("tau") ("upsilon") ("phi") ("varphi")
118 ("chi") ("psi") ("omega") ("Gamma") ("Delta") ("Theta") ("Lambda")
119 ("Xi") ("Pi") ("Sigma") ("Upsilon") ("Phi") ("Psi") ("Omega")
120 ("LaTeX") ("TeX") ("item[]")
121 ))
122 (defvar user-singlecmd-table nil)
124 ;---------- Key mode map ----------
125 ;;;
126 ;; Create new key map: YaTeX-mode-map
127 ;; Do not change this section.
128 ;;;
129 (defvar YaTeX-mode-map nil
130 "Keymap used in YaTeX mode."
131 )
132 (defvar YaTeX-compilation-mode-map nil
133 "Keymap userd in YaTeX compilation buffer."
134 )
136 ;---------- Define deafult key bindings on YaTeX mode map ----------
137 ;;;
138 ;; Define key table
139 ;;;
140 (if YaTeX-mode-map
141 nil
142 (global-unset-key (concat YaTeX-prefix "b"))
143 (setq YaTeX-mode-map (make-sparse-keymap))
144 (define-key YaTeX-mode-map (concat YaTeX-prefix "tj")
145 'YaTeX-compile)
146 (define-key YaTeX-mode-map (concat YaTeX-prefix "tx")
147 'YaTeX-preview)
148 (define-key YaTeX-mode-map (concat YaTeX-prefix "tv")
149 'YaTeX-view-error)
150 (define-key YaTeX-mode-map (concat YaTeX-prefix "tb")
151 '(lambda () (interactive) (YaTeX-insert-string "\\")))
152 (define-key YaTeX-mode-map (concat YaTeX-prefix "'")
153 'YaTeX-prev-error)
154 (define-key YaTeX-mode-map (concat YaTeX-prefix " ")
155 'YaTeX-do-completion)
156 (define-key YaTeX-mode-map (concat YaTeX-prefix "v")
157 'YaTeX-version)
159 (define-key YaTeX-mode-map "\"" 'YaTeX-insert-quote)
160 (define-key YaTeX-mode-map (concat YaTeX-prefix "{")
161 'YaTeX-insert-braces)
162 (define-key YaTeX-mode-map (concat YaTeX-prefix "}")
163 'YaTeX-insert-braces-region)
164 (define-key YaTeX-mode-map (concat YaTeX-prefix "d")
165 'YaTeX-insert-dollar)
166 (define-key YaTeX-mode-map (concat YaTeX-prefix "\\")
167 '(lambda () (interactive) (YaTeX-insert-string "$\\backslash$")))
168 (define-key YaTeX-mode-map (concat YaTeX-prefix "bd")
169 '(lambda (arg) (interactive "P")
170 (YaTeX-insert-begin-end "document" arg)))
171 (define-key YaTeX-mode-map (concat YaTeX-prefix "Bd")
172 '(lambda () (interactive)
173 (YaTeX-insert-begin-end "document" t)))
174 (define-key YaTeX-mode-map (concat YaTeX-prefix "bD")
175 '(lambda (arg) (interactive "P")
176 (YaTeX-insert-begin-end "description" arg)))
177 (define-key YaTeX-mode-map (concat YaTeX-prefix "BD")
178 '(lambda () (interactive)
179 (YaTeX-insert-begin-end "description" t)))
180 (define-key YaTeX-mode-map (concat YaTeX-prefix "be")
181 '(lambda (arg) (interactive "P")
182 (YaTeX-insert-begin-end "enumerate" arg)))
183 (define-key YaTeX-mode-map (concat YaTeX-prefix "Be")
184 '(lambda () (interactive)
185 (YaTeX-insert-begin-end "enumerate" t)))
186 (define-key YaTeX-mode-map (concat YaTeX-prefix "bi")
187 '(lambda (arg) (interactive "P")
188 (YaTeX-insert-begin-end "itemize" arg)))
189 (define-key YaTeX-mode-map (concat YaTeX-prefix "Bi")
190 '(lambda () (interactive)
191 (YaTeX-insert-begin-end "itemize" t)))
192 (define-key YaTeX-mode-map (concat YaTeX-prefix "bt")
193 '(lambda (arg) (interactive "P")
194 (YaTeX-insert-begin-end "tabbing" arg)))
195 (define-key YaTeX-mode-map (concat YaTeX-prefix "Bt")
196 '(lambda () (interactive)
197 (YaTeX-insert-begin-end "tabbing" t)))
198 (define-key YaTeX-mode-map (concat YaTeX-prefix "bT")
199 '(lambda (arg) (interactive "P")
200 (YaTeX-insert-begin-end "tabular" arg)))
201 (define-key YaTeX-mode-map (concat YaTeX-prefix "BT")
202 '(lambda () (interactive)
203 (YaTeX-insert-begin-end "tabular" t)))
204 (define-key YaTeX-mode-map (concat YaTeX-prefix "bq")
205 '(lambda (arg) (interactive "P")
206 (YaTeX-insert-begin-end "quote" arg)))
207 (define-key YaTeX-mode-map (concat YaTeX-prefix "Bq")
208 '(lambda () (interactive)
209 (YaTeX-insert-begin-end "quote" t)))
210 (define-key YaTeX-mode-map (concat YaTeX-prefix "bQ")
211 '(lambda (arg) (interactive "P")
212 (YaTeX-insert-begin-end "quotation" arg)))
213 (define-key YaTeX-mode-map (concat YaTeX-prefix "BQ")
214 '(lambda () (interactive)
215 (YaTeX-insert-begin-end "quotation" t)))
216 (define-key YaTeX-mode-map (concat YaTeX-prefix ".")
217 'YaTeX-comment-paragraph)
218 (define-key YaTeX-mode-map (concat YaTeX-prefix ",")
219 'YaTeX-uncomment-paragraph)
220 (define-key YaTeX-mode-map (concat YaTeX-prefix ">")
221 '(lambda () (interactive) (comment-region "%")))
222 (define-key YaTeX-mode-map (concat YaTeX-prefix "<")
223 '(lambda () (interactive) (uncomment-region "%")))
224 (define-key YaTeX-mode-map (concat YaTeX-prefix "b ")
225 'YaTeX-make-begin-end)
226 (define-key YaTeX-mode-map (concat YaTeX-prefix "B ")
227 'YaTeX-make-begin-end-region)
228 (define-key YaTeX-mode-map (concat YaTeX-prefix "e")
229 'YaTeX-end-environment)
230 (define-key YaTeX-mode-map (concat YaTeX-prefix "s")
231 'YaTeX-make-section)
232 (define-key YaTeX-mode-map (concat YaTeX-prefix "l")
233 'YaTeX-make-fontsize)
234 (define-key YaTeX-mode-map (concat YaTeX-prefix "L")
235 'YaTeX-make-fontsize-region)
236 (define-key YaTeX-mode-map (concat YaTeX-prefix "m")
237 'YaTeX-make-singlecmd)
238 (define-key YaTeX-mode-map (concat YaTeX-prefix "g")
239 'YaTeX-goto-corresponding-environment)
240 (define-key YaTeX-mode-map (concat YaTeX-prefix "\C-m")
241 '(lambda () (interactive) (YaTeX-insert-string "\\\\")))
242 (if (eq system-type 'ms-dos)
243 (define-key YaTeX-mode-map (concat YaTeX-prefix "\^L")
244 '(lambda () (interactive)
245 (set-screen-height YaTeX-saved-screen-height) (recenter))))
246 )
248 (if YaTeX-compilation-mode-map nil
249 (setq YaTeX-compilation-mode-map (make-keymap))
250 (suppress-keymap YaTeX-compilation-mode-map t)
251 (define-key YaTeX-compilation-mode-map " "
252 'YaTeX-jump-error-line)
253 )
255 ;---------- Customize as you like above ----------
257 ;---------- Define other variable ----------
258 (defvar env-name "document") ;Initial tex-environment completion
259 (defvar section-name "documentstyle[12pt]") ;Initial tex-section completion
260 (defvar fontsize-name "large") ;Initial fontsize completion
261 (defvar single-command "maketitle") ;Initial LaTeX single command
262 (defvar YaTeX-user-table-has-read nil
263 "Flag that means whether user completion table has read or not."
264 )
265 (defvar yatex-mode-hook nil
266 "List of functions to be called after .tex file is read
267 and yatex-mode starts.")
269 ;---------- Produce YaTeX-mode ----------
270 ;;;
271 ;; Major mode definition
272 ;;;
273 (defun yatex-mode ()
274 (interactive)
275 (kill-all-local-variables)
276 (setq major-mode 'YaTeX-mode)
277 (setq mode-name "やてふもーど")
278 (turn-on-auto-fill)
279 (make-local-variable 'kanji-display-code)
280 (make-local-variable 'kanji-fileio-code)
281 (if (eq system-type 'ms-dos)
282 (setq YaTeX-kanji-code 1)
283 (defvar YaTeX-kanji-code 2))
284 (setq kanji-display-code YaTeX-kanji-code
285 kanji-fileio-code YaTeX-kanji-code)
286 (make-local-variable 'fill-column)
287 (make-local-variable 'fill-prefix)
288 (setq fill-column 72
289 fill-prefix YaTeX-fill-prefix)
290 (use-local-map YaTeX-mode-map)
291 (if (eq system-type 'ms-dos)
292 (setq YaTeX-saved-screen-height (screen-height)))
293 (if YaTeX-user-table-has-read nil
294 (YaTeX-read-user-completion-table)
295 (setq YaTeX-user-table-has-read t))
296 (run-hooks 'yatex-mode-hook)
297 )
299 ;---------- Define YaTeX-mode functions ----------
300 ;;;
301 ;; YaTeX-mode functions
302 ;;;
303 (defun YaTeX-insert-begin-end (env arg)
304 "Insert \begin{mode-name} and \end{mode-name}."
305 (if arg
306 (save-excursion
307 (if (> (point) (mark)) (exchange-point-and-mark))
308 (insert "\\begin{" env "}\n")
309 (exchange-point-and-mark)
310 (insert "\\end{" env "}\n"))
311 (delete-blank-lines)
312 (insert "\\begin{" env "}\n")
313 (newline (1+ (* 2 YaTeX-open-lines)))
314 (insert "\\end{" env "}\n")
315 (previous-line (+ 2 YaTeX-open-lines)))
316 )
318 (defun YaTeX-exist-completion-table (elm table)
319 "Return nil, if single list element:elm was
320 not found in possible completion table."
321 (while (not (or (null table) (equal elm (car table))))
322 (setq table (cdr table)))
323 table
324 )
326 (defun YaTeX-make-begin-end (arg)
327 "Make LaTeX environment command of \\begin{env.} ... \\end{env.}
328 by completing read.
329 If you invoke this command with universal argument,
330 \(C-u or ESC-1 is typical prefix to invoke commands with ARG.\)
331 you can put REGION into that environment between \\begin and \\end."
332 (interactive "P")
333 (let*
334 ((mode (if arg " region" ""))
335 (env
336 (completing-read
337 (format "Begin environment%s(default %s): " mode env-name)
338 (append user-env-table env-table) nil nil)))
339 (if (string= env "")
340 (setq env env-name))
341 (setq env-name env)
342 (if (not (YaTeX-exist-completion-table
343 (list env-name) (append user-env-table env-table)))
344 (setq user-env-table (cons (list env-name) user-env-table)))
345 (YaTeX-insert-begin-end env-name arg))
346 )
348 (defun YaTeX-make-begin-end-region ()
349 "Call YaTeX-make-begin-end with ARG to specify region mode."
350 (interactive)
351 (YaTeX-make-begin-end t)
352 )
354 (defun YaTeX-end-environment ()
355 "Close opening environment"
356 (interactive)
357 (let ((curp (point))
358 (s)(env nil)(nest 0))
359 (save-excursion
360 (while
361 (and
362 (>= nest 0)
363 (re-search-backward
364 "^[^\\%]*\\(\\\\begin{.*}\\)\\|\\(\\\\end{.*}\\)" (point-min) t))
365 (setq nest (if (re-search-forward "begin{" (match-end 0) t)
366 (1- nest) (1+ nest))))
367 (if (>= nest 0)
368 (message "No premature environment")
369 (goto-char (match-end 0))
370 (setq s (point))
371 (skip-chars-forward "0-9_A-z")
372 (setq env (buffer-substring s (point)))
373 ;;(recursive-edit)
374 ))
375 (if (null env) nil
376 (save-excursion
377 (if (and (re-search-forward "^[^\\%]*\\\\end{.*}" (point-max) t)
378 (progn (goto-char (match-beginning 0))
379 (re-search-forward env (match-end 0) t)))
380 (if (y-or-n-p
381 (concat "Environment `" env
382 "' was already closed. Force close?"))
383 nil
384 (setq env nil))))
385 (message "") ;Erase (y or n) message.
386 (if (null env) nil
387 (insert "\\end{" env "}")
388 (setq curp (point))
389 (goto-char s)
390 (if (pos-visible-in-window-p)
391 (sit-for 1)
392 (message (concat "Matches \\begin{" env
393 (format "} at line %d"
394 (count-lines (point-min) s)))))
395 (goto-char curp)))
396 )
397 )
399 (defun YaTeX-make-section (arg)
400 "Make LaTeX \\section{} type command with completing read.
401 With ARG of numeric, you can specify the number of argument of
402 LaTeX command.
403 For example, if you want to produce LaTeX command
405 \\addtolength{\\topmargin}{8mm}
407 which has two argument. You can produce that sequence by typing...
408 ESC 2 C-c s add SPC RET \\topm SPC RET 8mm RET
409 \(by default\)
410 You can complete symbol at LaTeX command and 1st argument."
411 (interactive "p")
412 (let*
413 ((section
414 (completing-read
415 (format "\\???{} (default %s): " section-name)
416 (append user-section-table section-table)
417 nil nil))
418 (section (if (string= section "") section-name section))
419 (title
420 (completing-read (concat "\\" section "{???}: ")
421 (append user-article-table article-table)
422 nil nil)))
423 (setq section-name section)
424 (if (not (YaTeX-exist-completion-table
425 (list section-name) (append user-section-table section-table)))
426 (setq user-section-table
427 (cons (list section-name) user-section-table)))
428 (insert "\\" section-name "{" title "}")
429 (let ((j 2))
430 (while (<= j arg)
431 (insert (concat "{" (read-string (format "Argument %d: " j))))
432 (insert "}")
433 (setq j (1+ j)))
434 )
435 (if (string= title "") (forward-char -1)
436 nil))
437 )
439 ;(defun YaTeX-make-section-region ()
440 ; "Call YaTeX-make-section with ARG to specify region mode."
441 ; (interactive)
442 ; (YaTeX-make-section t)
443 ;)
445 (defun YaTeX-make-fontsize (arg)
446 "Make completion like {\\large ...} or {\\slant ...} in minibuffer.
447 If you invoke this command with universal argument, you can put region
448 into {\\xxx } braces.
449 \(C-u or ESC-1 are default key bindings of universal-argument.\)"
450 (interactive "P")
451 (let* ((mode (if arg "region" ""))
452 (fontsize
453 (completing-read
454 (format "{\\??? %s} (default %s): " mode fontsize-name)
455 (append user-fontsize-table fontsize-table)
456 nil nil )))
457 (if (string= fontsize "")
458 (setq fontsize fontsize-name))
459 (setq fontsize-name fontsize)
460 (if (not (YaTeX-exist-completion-table
461 (list fontsize-name)
462 (append user-fontsize-table fontsize-table)))
463 (setq user-fontsize-table
464 (cons (list fontsize-name) user-fontsize-table)))
465 (if arg
466 (save-excursion
467 (if (> (point) (mark)) (exchange-point-and-mark))
468 (insert "{\\" fontsize-name " ")
469 (exchange-point-and-mark)
470 (insert "}"))
471 (insert "{\\" fontsize-name " }")
472 (forward-char -1)))
473 )
475 (defun YaTeX-make-fontsize-region ()
476 "Call functino:YaTeX-make-fontsize with ARG to specify region mode."
477 (interactive)
478 (YaTeX-make-fontsize t)
479 )
481 (defun YaTeX-make-singlecmd (single)
482 (interactive
483 (list (completing-read
484 (format "\\??? (default %s): " single-command)
485 (append user-singlecmd-table singlecmd-table)
486 nil nil )))
487 (if (string= single "")
488 (setq single single-command))
489 (setq single-command single)
490 (if (not (YaTeX-exist-completion-table
491 (list single-command)
492 (append user-singlecmd-table singlecmd-table)))
493 (setq user-singlecmd-table
494 (cons (list single-command) user-singlecmd-table)))
495 (insert "\\" single-command " ")
496 )
498 (defvar YaTeX-completion-begin-regexp "[{\\]"
499 "Regular expression of limit where LaTeX command's
500 completion begins.")
502 (defun YaTeX-do-completion ()
503 "Try completion on LaTeX command preceding point."
504 (interactive)
505 (if
506 (or (eq (preceding-char) ? )
507 (eq (preceding-char) ?\t)
508 (eq (preceding-char) ?\n)
509 (bobp))
510 (message "Nothing to complete.") ;Do not complete
511 (let* ((end (point))
512 (limit (save-excursion (beginning-of-line) (point)))
513 (completion-begin
514 (progn (re-search-backward "[ \t\n]" limit 1)
515 (point)))
516 (begin (progn
517 (goto-char end)
518 (if (re-search-backward YaTeX-completion-begin-regexp
519 completion-begin t)
520 (1+ (point))
521 nil))))
522 (goto-char end)
523 (cond
524 ((null begin)
525 (message "I think it is not LaTeX sequence."))
526 (t
527 (let* ((pattern (buffer-substring begin end))
528 (all-table (append section-table user-section-table
529 article-table user-article-table
530 env-table user-env-table
531 singlecmd-table user-singlecmd-table))
532 ;; First,
533 ;; search completion without backslash.
534 (completion (try-completion pattern all-table nil)))
535 (if
536 (eq completion nil)
537 ;; Next,
538 ;; search completion with backslash
539 (setq completion
540 (try-completion (buffer-substring (1- begin) end)
541 all-table nil)
542 begin (1- begin)))
543 (cond
544 ((null completion)
545 (message (concat "Can't find completion for '" pattern "'"))
546 (ding))
547 ((eq completion t) (message "Sole completion."))
548 ((not (string= completion pattern))
549 (kill-region begin end)
550 (insert completion)
551 )
552 (t
553 (message "Making completion list...")
554 (with-output-to-temp-buffer "*Help*"
555 (display-completion-list
556 (all-completions pattern all-table))) )
557 ))))))
558 )
560 (defun YaTeX-insert-quote ()
561 (interactive)
562 (insert
563 (cond
564 ((= (preceding-char) ?\\ ) ?\")
565 ((= (preceding-char) ?\( ) ?\")
566 ((= (preceding-char) 32) "``")
567 ((= (preceding-char) 9) "``")
568 ((= (preceding-char) ?\n) "``")
569 ((bobp) "``")
570 (t "''")
571 )))
574 (defun YaTeX-insert-braces-region (beg end)
575 (interactive "r")
576 (save-excursion
577 (goto-char end)
578 (insert "}")
579 (goto-char beg)
580 (insert "{"))
581 )
583 (defun YaTeX-insert-braces ()
584 (interactive)
585 (insert "{}")
586 (forward-char -1)
587 )
589 (defun YaTeX-insert-dollar ()
590 (interactive)
591 (insert "$$")
592 (forward-char -1)
593 )
595 (defun YaTeX-insert-string (s)
596 (insert s)
597 )
599 (defun YaTeX-version ()
600 "Return string of the version of running YaTeX."
601 (interactive)
602 (message
603 (concat "Yet Another TeX mode 「野鳥」 Revision "
604 YaTeX-revision-number))
605 )
607 (defun YaTeX-compile-sentinel (proc mes)
608 (cond ((null (buffer-name (process-buffer proc)))
609 ;; buffer killed
610 (set-process-buffer proc nil))
611 ((memq (process-status proc) '(signal exit))
612 (let* ((obuf (current-buffer)))
613 ;; save-excursion isn't the right thing if
614 ;; process-buffer is current-buffer
615 (unwind-protect
616 (progn
617 ;; Write something in *compilation* and hack its mode line
618 (set-buffer (process-buffer proc))
619 (goto-char (point-max))
620 (insert ?\n "jlatex compilation " mes)
621 (forward-char -1)
622 (insert " at "
623 (substring (current-time-string) 0 -5))
624 (insert "\n * Hit any key to return * ")
625 (forward-char 1)
626 (setq mode-line-process
627 (concat ": "
628 (symbol-name (process-status proc))))
629 ;; If buffer and mode line will show that the process
630 ;; is dead, we can delete it now. Otherwise it
631 ;; will stay around until M-x list-processes.
632 (delete-process proc)
633 )
634 (setq YaTeX-compilation-process nil)
635 ;; Force mode line redisplay soon
636 (set-buffer-modified-p (buffer-modified-p))
637 )
638 (set-buffer obuf)
639 )))
640 )
642 (defvar YaTeX-compilation-process nil
643 "Process identifier for jlatex"
644 )
646 (defun YaTeX-compile ()
647 "Execute jlatex (or other) to LaTeX compile."
648 (interactive)
649 (if YaTeX-compilation-process
650 (if (eq (process-status YaTeX-compilation-process) 'run)
651 (progn (interrupt-process YaTeX-compilation-process)
652 (sit-for 1)
653 (delete-process YaTeX-compilation-process))
654 nil) nil)
655 ; (compile1 (concat tex-command " " (buffer-name))
656 ; "TeX error" "*TeX compilation*")
657 (setq YaTeX-compilation-process nil)
658 (if (eq system-type 'ms-dos) ;if MS-DOS
659 (with-output-to-temp-buffer "*YaTeX-compilation*"
660 (message (concat "Compiling " (buffer-name) "..."))
661 (YaTeX-put-nonstopmode)
662 (basic-save-buffer)
663 (call-process shell-file-name
664 nil
665 "*YaTeX-compilation*" nil
666 "/c" (YaTeX-get-latex-command))
667 ;;;"/c " tex-command (buffer-name) )
668 (YaTeX-remove-nonstopmode))
669 (setq YaTeX-compilation-process ;if UNIX
670 (with-output-to-temp-buffer "*YaTeX-compilation*"
671 (basic-save-buffer)
672 (start-process "LaTeX" "*YaTeX-compilation*" shell-file-name "-c"
673 (YaTeX-get-latex-command))
674 ;;;tex-command (buffer-name) "")
675 ))
676 (set-process-sentinel YaTeX-compilation-process 'YaTeX-compile-sentinel))
677 (setq current-TeX-buffer (buffer-name))
678 (other-window 1)
679 (use-local-map YaTeX-compilation-mode-map)
680 (set-kanji-process-code YaTeX-kanji-code)
681 (message "Type SPC to continue.")
682 (goto-char (point-max))
683 (sit-for 30)
684 (read-char) ;hit any key
685 (other-window -1)
686 )
688 (defun YaTeX-preview (preview-command preview-file)
689 "Execute xdvi (or other) to tex-preview."
690 (interactive
691 (list (read-string "Preview command: " dvi2-command)
692 (read-string "Prefiew file[.dvi]: "
693 ;;(substring (buffer-name) 0 -4)
694 (YaTeX-get-preview-file-name) ;ver 1.31
695 )))
696 (setq dvi2-command preview-command)
697 (with-output-to-temp-buffer "*dvi-preview*"
698 (if (eq system-type 'ms-dos)
699 (progn (send-string-to-terminal "\e[2J") ;if MS-DOS
700 (call-process shell-file-name "con" "*dvi-preview*" nil
701 "/c " dvi2-command preview-file)
702 (redraw-display))
703 (start-process "xdvi" "*dvi-preview*" shell-file-name "-c"
704 (concat dvi2-command " " preview-file)) ;if UNIX
705 (message (concat "Starting " dvi2-command " to preview " preview-file)))
706 )
707 )
709 (defun YaTeX-prev-error ()
710 "Visit previous error. The reason why not NEXT-error is to
711 avoid make confliction of line numbers by editing."
712 (interactive)
713 (setq cur-buf (buffer-name)
714 YaTeX-error-line nil)
715 (if (null (get-buffer "*YaTeX-compilation*"))
716 (message "There is no output buffer of compilation.")
717 (pop-to-buffer "*YaTeX-compilation*")
718 (if (eq system-type 'ms-dos)
719 (if (search-backward latex-dos-emergency-message nil t)
720 (progn (goto-char (point-max))
721 (setq error-regexp latex-error-regexp))
722 (beginning-of-line)
723 (forward-char -1)
724 (setq error-regexp latex-warning-regexp))
725 (if YaTeX-compilation-process ; if jlatex on UNIX
726 (if (eq (process-status YaTeX-compilation-process) 'run)
727 (progn
728 (goto-char (point-max))
729 (setq error-regexp latex-error-regexp)))
730 (beginning-of-line)
731 (setq error-regexp latex-warning-regexp)))
732 (if (re-search-backward error-regexp nil t)
733 (save-restriction
734 (set-mark-command nil)
735 (end-of-line)
736 (narrow-to-region (point) (mark))
737 (goto-char (point-min))
738 (re-search-forward "[0-9]")
739 (forward-char -1)
740 (set-mark (point))
741 (skip-chars-forward "0-9")
742 (narrow-to-region (point) (mark))
743 (goto-char (point-min))
744 (setq YaTeX-error-line (read (current-buffer))))
745 (message "No more error on %s" cur-buf)
746 (ding)
747 )
748 (other-window -1)
749 (switch-to-buffer cur-buf)
750 (if (null YaTeX-error-line)
751 nil
752 (goto-line YaTeX-error-line)
753 (message "latex error or warning at line: %d" YaTeX-error-line)
754 (other-window 1)
755 (skip-chars-backward "[0-9]")
756 (recenter (/ (window-height) 2))
757 (sit-for 3)
758 (forward-line -1)
759 (other-window -1)
760 ))
761 )
763 (defun YaTeX-jump-error-line ()
764 "Jump corresponding line on latex command's error message."
765 (interactive)
766 (let ((end (progn (end-of-line) (point)))
767 (begin (progn (beginning-of-line)(point))))
768 (if (null (re-search-forward "l[ ines]*\\.*[1-9][0-9]*" end t))
769 (message "No line number expression")
770 (goto-char (match-beginning 0))
771 (re-search-forward "[1-9][0-9]*" end t)
772 (save-restriction
773 (narrow-to-region (match-beginning 0) (match-end 0))
774 (goto-char (point-min))
775 (let ((error-line (read (current-buffer))))
776 (other-window -1)
777 (switch-to-buffer current-TeX-buffer)
778 (goto-line error-line)))))
779 )
781 (defun YaTeX-view-error ()
782 (interactive)
783 (other-window 1)
784 (goto-char (point-max))
785 (other-window -1)
786 )
788 (defun YaTeX-put-nonstopmode ()
789 (if (boundp 'YaTeX-need-nonstop)
790 (if (re-search-backward "\\nonstopmode{}" (point-min) t)
791 nil ;if already written in text then do nothing
792 (save-excursion
793 (goto-char (point-min))
794 (insert "\\nonstopmode{}%_YaTeX_%\n")))
795 )
796 )
798 (defun YaTeX-remove-nonstopmode ()
799 (if (boundp 'YaTeX-need-nonstop) ;for speed
800 (save-excursion
801 (goto-char (point-min))
802 (forward-line 1)
803 (narrow-to-region (point-min) (point))
804 (goto-char (point-min))
805 (delete-matching-lines "^\\\\nonstopmode\\{\\}%_YaTeX_%$")
806 (widen)))
807 )
809 (defun YaTeX-get-preview-file-name ()
810 "Get file name to preview by inquiring YaTeX-get-latex-command"
811 (let* ((latex-cmd (YaTeX-get-latex-command))
812 (fname (substring latex-cmd (1+ (rindex latex-cmd ? ))))
813 (period))
814 (if (eq fname "")
815 (setq fname (substring (buffer-name) 0 -4))
816 (setq period (rindex fname ?.))
817 (setq fname (substring fname 0 (if (eq -1 period) nil period)))
818 ))
819 )
821 (defun YaTeX-get-latex-command ()
822 "Specify the latex-command name and its argument.
823 If there is a line which begins by string: \"%#!\", the following
824 strings are assumed to be the latex-command and arguments. The
825 default value of latex-command is:
826 tex-command (buffer-name)
827 and if you write \"%#!jlatex\" in the beginning of certain line.
828 \"jlatex \" (buffer-name)
829 will be the latex-command,
830 and you write \"%#!jlatex main.tex\"
831 \"jlatex main.tex\"
832 will be given to the shell."
833 (let*
834 ((default-command
835 (concat tex-command " " (buffer-name)))) ;default value
836 (save-excursion
837 (goto-char (point-min))
838 (if (null (re-search-forward "^%#!" (point-max) t))
839 default-command
840 (skip-chars-forward "%#! ")
841 (if (eolp)
842 default-command
843 (let ((s (point)))
844 (skip-chars-forward "A-z") ;Skip command name
845 ;(setq YaTeX-latex-command (buffer-substring s (point)))
846 (if (eolp) ;Only change command name
847 (concat (buffer-substring s (point)) " " (buffer-name))
848 (end-of-line) ;Change entire command name
849 (buffer-substring s (point)) ;including arguments.
850 ))
851 ))))
852 )
854 (defun YaTeX-goto-corresponding-environment ()
855 "Go to corresponding begin/end enclosure."
856 (interactive)
857 (beginning-of-line)
858 (if (not (re-search-forward
859 "\\(begin{\\)\\|\\(end{\\)" (point-end-of-line) t))
860 (error "No environment declaration"))
861 (let ((p (point) env)
862 (m0 (match-beginning 0))
863 (m1 (match-beginning 1))
864 (m2 (match-beginning 2)))
865 (if (not (re-search-forward "}" (point-end-of-line) t))
866 (error "Unterminated brackets for begin/end"))
867 (setq env (buffer-substring p (match-beginning 0))) ;get current env
868 (cond
869 ((equal m0 m1) ;if begin{xxx}
870 (re-search-forward (concat "end{" env "}")))
871 ((equal m0 m2) ;if end{xxx}
872 (re-search-backward (concat "begin{" env "}")))
873 )
874 (beginning-of-line)
875 );let
876 )
878 (defun YaTeX-comment-paragraph ()
879 "Comment out current paragraph."
880 (interactive)
881 (save-excursion
882 (mark-paragraph)
883 (if (not (bobp)) (forward-line 1))
884 (comment-region "%"))
885 )
887 (defun YaTeX-uncomment-paragraph ()
888 "Uncomment current paragraph."
889 (interactive)
890 (save-excursion
891 (let ((prefix fill-prefix))
892 (setq fill-prefix "")
893 (mark-paragraph)
894 (if (not (bobp)) (forward-line 1))
895 (uncomment-region "%")
896 (setq fill-prefix prefix)))
897 )
899 (defun YaTeX-read-user-completion-table ()
900 "Append user completion table of LaTeX word"
901 (message "Loading personal completion table")
902 (let ((user-table (expand-file-name YaTeX-user-completion-table)))
903 (if (file-exists-p user-table)
904 (load-file user-table)
905 (message "Personal completion table not found."))
906 ))
908 (defun YaTeX-save-table ()
909 "Save personal completion table as dictionary."
910 (interactive)
911 (message "Saving user table in %s" YaTeX-user-completion-table)
912 (find-file (expand-file-name YaTeX-user-completion-table))
913 (erase-buffer)
914 ; (prin1-to-string user-section-table)
915 (insert "(setq user-section-table '(\n")
916 (mapcar '(lambda (s)
917 (insert (prin1-to-string s))
918 (insert "\n"))
919 user-section-table)
920 (insert "))\n\n")
922 (insert "(setq user-article-table '(\n")
923 (mapcar '(lambda (s)
924 (insert (prin1-to-string s))
925 (insert "\n"))
926 user-article-table)
927 (insert "))\n\n")
929 (insert "(setq user-env-table '(\n")
930 (mapcar '(lambda (s)
931 (insert (prin1-to-string s))
932 (insert "\n"))
933 user-env-table)
934 (insert "))\n\n")
936 (insert "(setq user-fontsize-table '(\n")
937 (mapcar '(lambda (s)
938 (insert (prin1-to-string s))
939 (insert "\n"))
940 user-fontsize-table)
941 (insert "))\n\n")
943 (insert "(setq user-singlecmd-table '(\n")
944 (mapcar '(lambda (s)
945 (insert (prin1-to-string s))
946 (insert "\n"))
947 user-singlecmd-table)
948 (insert "))\n")
950 (basic-save-buffer)
951 (kill-buffer (current-buffer))
952 (message "")
953 )
955 ;; --------------- General sub functions ---------------
957 ;(defun index (string char)
958 ; (let ((pos 0)(len (1- (length string)))(index -1))
959 ; (while (<= pos len)
960 ; (cond
961 ; ((= (aref string pos) char)
962 ; (setq index pos) (setq pos len))
963 ; (t (setq pos (1+ pos))))
964 ; )
965 ; index)
966 ;)
968 (defun rindex (string char)
969 (let ((pos (1- (length string)))(index -1))
970 (while (>= pos 0)
971 (cond
972 ((= (aref string pos) char)
973 (setq index pos) (setq pos -1))
974 (t (setq pos (1- pos))))
975 )
976 index)
977 )
979 (defun point-beginning-of-line ()
980 (save-excursion (beginning-of-line)(point))
981 )
983 (defun point-end-of-line ()
984 (save-excursion (end-of-line)(point))
985 )
987 (defun append-to-hook (hook hook-list)
988 "Add hook-list to certain emacs's hook correctly.
989 Argument hook-list is the list of function int the form to be called
990 Call this function with argument as next example,
991 (append-to-hook '((ding))) ;If one function to add.
992 (append-to-hook '((func1)(func2 arg)))."
993 (if (null (eval hook)) ;Not defined
994 (set hook
995 (append '(lambda ()) hook-list))
996 (if (listp (eval hook))
997 (if (eq (car (eval hook)) 'lambda) ;'(lambda () ....)
998 (set hook
999 (append (eval hook) hook-list))
1000 (if (eq hook 'kill-emacs-hook) ;'(hook1 hook2 ...)
1001 (progn ; this format is not
1002 (ding) ; for kill-emacs-hook
1003 (message
1004 "Caution!! you have wrong format of kill-emacs-hook"))
1005 (while (not (null hook-list))
1006 (set hook
1007 (append (eval hook) (car hook-list)))
1008 (setq hook-list (cdr hook-list))))
1010 (set hook ;'hook
1011 (append '(lambda ())
1012 (cons (list (eval hook)) hook-list)))))
1014 (append-to-hook 'kill-emacs-hook '((YaTeX-save-table)))
1016 ;--------------------------------- History ---------------------------------
1017 ; Rev. | Date | Contents
1018 ;------+----------+---------------------------------------------------------
1019 ; 1.00 | 91/ 6/13 | Initial version.
1020 ; | | Auto compilation & preview.
1021 ; | | \section{}-type and \begin{}\end{}-type completion.
1022 ; 1.01 | 91/ 6/14 | Add {\large ..} type completion (prefix+l).
1023 ; 1.10 | 6/21 | Add learning feature of completion.
1024 ; 1.11 | 6/27 | Simplify function begin-document etc. using lambda.
1025 ; 1.12 | 7/ 6 | Modify YaTeX-make-section, show section-name.
1026 ; 1.13 | 12/ 4 | Delete blank lines in make begin/end environment.
1027 ; 1.20 | 12/ 5 | Saving learned completion into user file.
1028 ; 1.21 | 12/ 6 | Add \maketitle type completion (prefix+m).
1029 ; 1.22 | 12/30 | Port yatex.el to DOS(Demacs).
1030 ; 1.23 | 92/ 1/ 8 | Enable latex and preview command on DOS.
1031 ; 1.24 | 1/ 9 | Add YaTeX-save-table to kill-emacs-hook automatically.
1032 ; 1.25 | 1/16 | YaTeX-do-completion (prefix+SPC) and argument
1033 ; | | acceptable YaTeX-make-section works. Put region into
1034 ; | | \begin...\end by calling YaTeX-make-begin-end with ARG.
1035 ; | | append-kill-emacs-hook was revised to append-to-hook.
1036 ; 1.26 | 1/18 | Region mode is added to {\large }. Default fontsize.
1037 ; 1.27 | 1/21 | Default name on completing-read.
1038 ; 1.28 | 7/ 2 | Add \nonstopmode{} automatically on DOS.
1039 ; | 7/20 | %#! usage to specify latex command and its arguments.
1040 ; | | Change default fill-prefix from TAB to null string.
1041 ; 1.29 | 7/21 | Add YaTeX-end-environment.
1042 ; 1.30 | 9/26 | Support project 30 lines(other than 25 lines).
1043 ; 1.31 | 10/28 | Variable argument for previewer from %#! usage.
1044 ; 1.32 | 10/16 | YaTeX-goto-corresponding-environment.
1045 ;------+----------+---------------------------------------------------------
1047 ;----------------------------- End of yatex.el -----------------------------