yatex

view yatex.el @ 4:78dfe28b6a35

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