yatex

view yatex.el @ 68:0eb6997bee16

More adjustment for Emacs20 and XEmacs [prefix] g for <applet> <!--#include> <!--#exec>
author yuuji
date Mon, 26 Oct 1998 12:05:32 +0000
parents 36a48185b95a
children 807c1e7e68b7
line source
1 ;;; -*- Emacs-Lisp -*-
2 ;;; Yet Another tex-mode for emacs.
3 ;;; yatex.el rev. 1.66
4 ;;; (c )1991-1998 by HIROSE Yuuji.[yuuji@ae.keio.ac.jp]
5 ;;; Last modified Mon Oct 26 21:05:14 1998 on firestorm
6 ;;; $Id$
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program; see the file COPYING. If not, write to the
20 ;; Software Foundation Inc., 59 Temple Place - Suite 330, Boston, MA
21 ;; 02111-1307, USA.
23 (require 'comment)
24 (require 'yatexlib)
25 (defconst YaTeX-revision-number "1.66"
26 "Revision number of running yatex.el"
27 )
28 ;---------- Local variables ----------
29 (defvar YaTeX-prefix "\C-c"
30 "*Prefix key to call YaTeX functions.
31 You can select favorite prefix key by setq in your ~/.emacs."
32 )
33 (defvar YaTeX-environment-indent 1
34 "*Indentation depth at column width in LaTeX environments."
35 )
36 (defvar YaTeX-fill-prefix nil
37 "*fill-prefix used for auto-fill-mode.
38 The default value is nil."
39 )
40 (defvar YaTeX-fill-column 72
41 "*fill-column used for auto-fill-mode."
42 )
43 (defvar YaTeX-comment-prefix "%"
44 "TeX comment prefix."
45 )
46 (defvar YaTeX-current-position-register ?3
47 "*Position register to keep where the last completion was done.
48 All of YaTeX completing input store the current position into
49 the register YaTeX-current-position-register. So every time you
50 make a trip to any other part of text other than you are writing, you can
51 return to the editing paragraph by calling register-to-point with argument
52 YaTeX-current-position-register."
53 )
54 ;;(defvar YaTeX-tmp-dic-unit 'main-file
55 ;; "*Default switching unit of temporary dictionary.
56 ;;There are two switching unit:
57 ;;'main-file : switch tmp-dic according to main-file directory.
58 ;;'directory : switch tmp-dic dir by dir."
59 ;;)
60 (defvar tex-command (if YaTeX-japan "jlatex" "latex")
61 "*Default command for typesetting LaTeX text."
62 )
63 (defvar bibtex-command (if YaTeX-japan "jbibtex" "bibtex")
64 "*Default command of BibTeX."
65 )
66 (defvar dvi2-command ;previewer command for your site
67 (if YaTeX-dos "dviout -wait=0"
68 "xdvi -geo +0+0 -s 4")
69 "*Default previewer command including its option.
70 This default value is for X window system."
71 )
72 (defvar makeindex-command (if YaTeX-dos "makeind" "makeindex")
73 "*Default makeindex command."
74 )
75 (defvar dviprint-command-format
76 (if YaTeX-dos "dviprt %s %f%t"
77 "dvi2ps %f %t %s | lpr")
78 "*Command line string to print out current file.
79 Format string %s will be replaced by the filename. Do not forget to
80 specify the `from usage' and `to usage' with their option by format string
81 %f and %t.
82 See also documentation of dviprint-from-format and dviprint-to-format."
83 )
84 (defvar dviprint-from-format
85 (if YaTeX-dos "%b-" "-f %b")
86 "*`From' page format of dvi filter. %b will turn to beginning page number."
87 )
88 (defvar dviprint-to-format
89 (if YaTeX-dos "%e" "-t %e")
90 "*`To' page format of dvi filter. %e will turn to end page number."
91 )
92 (defvar YaTeX-default-document-style
93 (concat (if YaTeX-japan "j") "article")
94 "*Default LaTeX Documentstyle for YaTeX-typeset-region."
95 )
96 (defvar YaTeX-need-nonstop nil
97 "*T for adding `\\nonstopmode{}' to text before invoking latex command."
98 )
99 (defvar latex-warning-regexp "line.* [0-9]*"
100 "*Regular expression of line number of warning message by latex command."
101 )
102 (defvar latex-error-regexp "l\\.[1-9][0-9]*"
103 "*Regular expression of line number of latex error.
104 Perhaps your latex command stops at this error message with line number of
105 LaTeX source text."
106 )
107 (defvar latex-dos-emergency-message
108 "Emergency stop" ;<- for Micro tex, ASCII-pTeX 1.6
109 "Message pattern of emergency stop of typesetting.
110 Because Demacs (GNU Emacs on DOS) cannot have concurrent process, the
111 latex command which is stopping on a LaTeX error, is terminated by Demacs.
112 Many latex command on DOS display some messages when it is terminated by
113 other process, user or OS. Define to this variable a message string of your
114 latex command on DOS shown at abnormal termination.
115 Remember Demacs's call-process function is not oriented for interactive
116 process."
117 )
118 (defvar latex-message-kanji-code 2
119 "*Kanji coding system latex command types out.
120 1 = Shift JIS, 2 = JIS, 3 = EUC."
121 )
122 (defvar NTT-jTeX nil
123 "*T for using NTT-jTeX for latex command.
124 More precisely, setting t to this variables inhibits inter-word break on
125 typeset document by line-break of source text. That is, YaTeX automatically
126 put % after each line at filling.
127 改行+インデントによって、タイプセット後の字間が空いてしまうのを抑制する場合に
128 tにする(古いNTT-jTeXで顕著に現れる)。具体的には、fillするときに各行の終わりに
129 %を付加する。"
130 )
131 (defvar YaTeX-item-regexp
132 (concat (regexp-quote "\\") "\\(sub\\|bib\\)*item")
133 "*Regular expression of item command."
134 )
135 (defvar YaTeX-sectioning-regexp
136 "part\\|chapter\\*?\\|\\(sub\\)*\\(section\\|paragraph\\)\\(\\*?\\|\\b\\)"
137 "*LaTeX sectioning commands regexp."
138 )
139 (defvar YaTeX-paragraph-start
140 (concat "^[ \t]*%\\|^[ \t]*$\\|\\'\\|^\C-l\\|\\\\\\\\$\\|^[ \t]*\\\\\\("
141 YaTeX-sectioning-regexp ;sectioning commands
142 "\\|[A-z]*item\\|begin{\\|end{" ;special declaration
143 "\\|newpage\\b\\|vspace\\b"
144 "\\)")
145 "*Paragraph starting regexp of common LaTeX source. Use this value
146 for YaTeX-uncomment-paragraph."
147 )
148 (defvar YaTeX-paragraph-separate
149 (concat "^[ \t]*%\\|^[ \t]*$\\|^\C-l\\|\\\\\\\\$\\|^[ \t]*\\\\\\("
150 YaTeX-sectioning-regexp ;sectioning commands
151 "\\|begin{\\|end{" ;special declaration
152 "\\|\\[\\|\\]"
153 "\\|newpage\\b\\|vspace\\b"
154 "\\)")
155 "*Paragraph delimiter regexp of common LaTeX source. Use this value
156 for YaTeX-uncomment-paragraph."
157 )
158 (defvar YaTeX-verbatim-environments
159 '("verbatim" "verbatim*")
160 "*Assume these environments of this variable disable LaTeX commands.")
161 (defvar YaTeX-verb-regexp "verb\\*?\\|path"
162 "*Regexp of verb family. Do not contain preceding \\\\ nor \\(\\).")
163 (defvar YaTeX-fill-inhibit-environments
164 (append '("tabular" "tabular*" "array" "picture" "eqnarray" "eqnarray*"
165 "equation" "math" "displaymath")
166 YaTeX-verbatim-environments)
167 "*In these environments, YaTeX inhibits fill-paragraph from formatting.
168 Define those environments as a form of list."
169 )
170 (defvar YaTeX-itemizing-env-regexp
171 "itemize\\|enumerate\\|description\\|list\\|thebibliography"
172 "*Regexp of itemizing environments")
173 (defvar YaTeX-equation-env-regexp
174 "array\\*?\\|equation\\*?"
175 "*Regexp of environments for equations")
176 (defvar YaTeX-array-env-regexp
177 (concat
178 "array\\*?\\|eqnarray\\*?\\|tabbing\\|tabular\\*?\\|" ;LaTeX
179 "align\\*?\\|split\\*?\\|aligned\\*?\\|alignat\\*?\\|" ;AMS-LaTeX
180 "[bpvV]?matrix\\|smallmatrix\\|cases\\|" ;AMS-LaTeX
181 "xalignat\\*?\\|xxalignat\\*?") ;AMS-LaTeX
182 "*Regexp of environments where `&' becomes field delimiter.")
183 (defvar YaTeX-uncomment-once t
184 "*T for removing all continuous commenting character(%).
185 Nil for removing only one commenting character at the beginning-of-line."
186 )
187 (defvar YaTeX-close-paren-always t
188 "Close parenthesis always when YaTeX-modify-mode is nil."
189 )
190 (defvar YaTeX-greek-by-maketitle-completion nil
191 "*T for greek letters completion by maketitle-type completion."
192 )
193 (defvar YaTeX-auto-math-mode t
194 "*T for changing YaTeX-math mode automatically.")
195 (defvar YaTeX-use-AMS-LaTeX nil
196 "*T for using AMS-LaTeX"
197 )
198 (defvar yatex-mode-hook nil
199 "*List of functions to be called at the end of yatex-mode initializations."
200 )
201 ;;-- Math mode values --
203 (defvar YaTeX-math-key-list-default
204 '((";" . YaTeX-math-sign-alist)
205 (":" . YaTeX-greek-key-alist))
206 "Default key sequence to invoke math-mode's image completion."
207 )
208 (defvar YaTeX-math-key-list-private nil
209 "*User defined alist, math-mode-prefix vs completion alist."
210 )
211 (defvar YaTeX-math-key-list
212 (append YaTeX-math-key-list-private YaTeX-math-key-list-default)
213 "Key sequence to invoke math-mode's image completion."
214 )
215 (defvar YaTeX-skip-default-reader nil
216 "Non-nil skips default argument reader of section-type completion."
217 )
218 (defvar YaTeX-simple-messages nil
219 "Non-nil makes minibuffer messages simpler."
220 )
221 (defvar YaTeX-template-file "~/work/template.tex"
222 "*Template TeX source file. This will be inserted to empty file."
223 )
224 (defvar YaTeX-addin-prefix "YaTeX:")
225 ;------------ Completion table ------------
226 ; Set tex-section-like command possible completion
227 (defvar section-table
228 '(("part") ("chapter") ("section") ("subsection")
229 ("subsubsection") ("paragraph") ("subparagraph")
230 ("author") ("thanks") ("documentstyle") ("documentclass") ("pagestyle")
231 ("title") ("underline") ("label") ("makebox")
232 ("footnote") ("footnotetext")
233 ("hspace*") ("vspace*") ("bibliography") ("bibitem") ("cite")
234 ("input") ("include") ("includeonly") ("mbox") ("hbox") ("caption")
235 ("newlength") ("setlength" 2) ("addtolength" 2) ("settowidth" 2)
236 ("setcounter" 2) ("addtocounter" 2) ("stepcounter" 2)
237 ("newcommand" 2) ("renewcommand" 2)
238 ("setcounter" 2) ("newenvironment" 3) ("newtheorem" 2)
239 ("cline") ("framebox") ("savebox" 2) ("sbox" 2) ("newsavebox") ("usebox")
240 ("date") ("put") ("ref") ("pageref")
241 ("multicolumn" 3) ("shortstack")
242 ;; for mathmode accent
243 ("tilde") ("hat") ("check") ("bar") ("dot") ("ddot") ("vec")
244 ("widetilde") ("widehat") ("overline") ("overrightarrow")
245 ;; section types in mathmode
246 ("frac" 2) ("sqrt") ("mathrm") ("mathbf") ("mathit")
247 )
248 "Default completion table for section-type completion."
249 )
250 (defvar user-section-table nil)
251 (defvar tmp-section-table nil)
253 ; Set tex-environment possible completion
254 (defvar env-table
255 '(("quote") ("quotation") ("center") ("verse") ("document")
256 ("verbatim") ("itemize") ("enumerate") ("description")
257 ("list") ("tabular") ("tabular*") ("table") ("tabbing") ("titlepage")
258 ("sloppypar") ("quotation") ("picture") ("displaymath")
259 ("eqnarray") ("figure") ("equation") ("abstract") ("array")
260 ("thebibliography") ("theindex") ("flushleft") ("flushright")
261 ("minipage")
262 )
263 "Default completion table for begin-type completion."
264 )
265 (defvar user-env-table nil)
266 (defvar tmp-env-table nil)
268 ; Set {\Large }-like completion
269 (defvar fontsize-table
270 '(("rm") ("em") ("bf") ("boldmath") ("it") ("sl") ("sf") ("sc") ("tt")
271 ("dg") ("dm")
272 ("tiny") ("scriptsize") ("footnotesize") ("small")("normalsize")
273 ("large") ("Large") ("LARGE") ("huge") ("Huge")
274 )
275 "Default completion table for large-type completion."
276 )
277 (defvar user-fontsize-table nil)
278 (defvar tmp-fontsize-table nil)
280 (defvar singlecmd-table
281 (append
282 '(("maketitle") ("makeindex") ("sloppy") ("protect")
283 ("LaTeX") ("TeX") ("item") ("item[]") ("appendix") ("hline")
284 ;;("rightarrow") ("Rightarrow") ("leftarrow") ("Leftarrow")
285 ("pagebreak") ("nopagebreak")
286 ("newpage") ("clearpage") ("cleardoublepage")
287 ("footnotemark") ("verb") ("verb*")
288 ("linebreak") ("pagebreak") ("noindent") ("indent")
289 ("left") ("right") ("dots")
290 )
291 (if YaTeX-greek-by-maketitle-completion
292 '(("alpha") ("beta") ("gamma") ("delta") ("epsilon")
293 ("varepsilon") ("zeta") ("eta") ("theta")("vartheta")
294 ("iota") ("kappa") ("lambda") ("mu") ("nu") ("xi") ("pi")
295 ("varpi") ("rho") ("varrho") ("sigma") ("varsigma") ("tau")
296 ("upsilon") ("phi") ("varphi") ("chi") ("psi") ("omega")
297 ("Gamma") ("Delta") ("Theta") ("Lambda")("Xi") ("Pi")
298 ("Sigma") ("Upsilon") ("Phi") ("Psi") ("Omega"))))
299 "Default completion table for maketitle-type completion."
300 )
301 (defvar user-singlecmd-table nil)
302 (defvar tmp-singlecmd-table nil)
304 ;---------- Key mode map ----------
305 ;;;
306 ;; Create new key map: YaTeX-mode-map
307 ;; Do not change this section.
308 ;;;
309 (defvar YaTeX-mode-map nil
310 "Keymap used in YaTeX mode"
311 )
312 (defvar YaTeX-prefix-map nil
313 "Keymap used when YaTeX-prefix key pushed"
314 )
315 (defvar YaTeX-user-extensional-map (make-sparse-keymap)
316 "*Keymap used for the user's customization")
317 (defvar YaTeX-current-completion-type nil
318 "Has current completion type. This may be used in YaTeX addin functions."
319 )
320 (defvar YaTeX-modify-mode nil
321 "*Current editing mode.
322 When non-nil, each opening parentheses only opens,
323 nil enters both open/close parentheses when opening parentheses key pressed."
324 )
325 (defvar YaTeX-math-mode nil
326 "Holds whether current mode is math-mode."
327 )
328 ;;;
329 ;; Define key table
330 ;;;
331 (if YaTeX-mode-map
332 nil
333 (setq YaTeX-mode-map (make-sparse-keymap))
334 (setq YaTeX-prefix-map (make-sparse-keymap))
335 (define-key YaTeX-mode-map "\"" 'YaTeX-insert-quote)
336 (define-key YaTeX-mode-map "{" 'YaTeX-insert-braces)
337 (define-key YaTeX-mode-map "(" 'YaTeX-insert-parens)
338 (define-key YaTeX-mode-map "$" 'YaTeX-insert-dollar)
339 (define-key YaTeX-mode-map "&" 'YaTeX-insert-amper)
340 (define-key YaTeX-mode-map "[" 'YaTeX-insert-brackets)
341 (define-key YaTeX-mode-map YaTeX-prefix YaTeX-prefix-map)
342 (define-key YaTeX-mode-map "\M-\C-@" 'YaTeX-mark-environment)
343 (define-key YaTeX-mode-map "\M-\C-a" 'YaTeX-beginning-of-environment)
344 (define-key YaTeX-mode-map "\M-\C-e" 'YaTeX-end-of-environment)
345 (define-key YaTeX-mode-map "\M-\C-m" 'YaTeX-intelligent-newline)
346 (define-key YaTeX-mode-map "\C-i" 'YaTeX-indent-line)
347 (YaTeX-define-key "%" 'YaTeX-%-menu)
348 (YaTeX-define-key "t" 'YaTeX-typeset-menu)
349 (YaTeX-define-key "w" 'YaTeX-switch-mode-menu)
350 (YaTeX-define-key "'" 'YaTeX-prev-error)
351 (YaTeX-define-key "^" 'YaTeX-visit-main)
352 (YaTeX-define-key "4^" 'YaTeX-visit-main-other-window)
353 (YaTeX-define-key "4g" 'YaTeX-goto-corresponding-*-other-window)
354 (YaTeX-define-key "44" 'YaTeX-switch-to-window)
355 (and YaTeX-emacs-19 window-system
356 (progn
357 (YaTeX-define-key "5^" 'YaTeX-visit-main-other-frame)
358 (YaTeX-define-key "5g" 'YaTeX-goto-corresponding-*-other-frame)
359 (YaTeX-define-key "55" 'YaTeX-switch-to-window)))
360 (YaTeX-define-key " " 'YaTeX-do-completion)
361 (YaTeX-define-key "v" 'YaTeX-version)
363 (YaTeX-define-key "}" 'YaTeX-insert-braces-region)
364 (YaTeX-define-key "]" 'YaTeX-insert-brackets-region)
365 (YaTeX-define-key ")" 'YaTeX-insert-parens-region)
366 (YaTeX-define-key "$" 'YaTeX-insert-dollars-region)
367 (YaTeX-define-key "i" 'YaTeX-fill-item)
368 (YaTeX-define-key
369 "\\" '(lambda () (interactive) (insert "$\\backslash$")))
370 (if YaTeX-no-begend-shortcut
371 (progn
372 (YaTeX-define-key "B" 'YaTeX-make-begin-end-region)
373 (YaTeX-define-key "b" 'YaTeX-make-begin-end))
374 (YaTeX-define-begend-key "bc" "center")
375 (YaTeX-define-begend-key "bd" "document")
376 (YaTeX-define-begend-key "bD" "description")
377 (YaTeX-define-begend-key "be" "enumerate")
378 (YaTeX-define-begend-key "bE" "equation")
379 (YaTeX-define-begend-key "bi" "itemize")
380 (YaTeX-define-begend-key "bl" "flushleft")
381 (YaTeX-define-begend-key "bm" "minipage")
382 (YaTeX-define-begend-key "bt" "tabbing")
383 (YaTeX-define-begend-key "bT" "tabular")
384 (YaTeX-define-begend-key "b\^t" "table")
385 (YaTeX-define-begend-key "bp" "picture")
386 (YaTeX-define-begend-key "bq" "quote")
387 (YaTeX-define-begend-key "bQ" "quotation")
388 (YaTeX-define-begend-key "br" "flushright")
389 (YaTeX-define-begend-key "bv" "verbatim")
390 (YaTeX-define-begend-key "bV" "verse")
391 (YaTeX-define-key "B " 'YaTeX-make-begin-end-region)
392 (YaTeX-define-key "b " 'YaTeX-make-begin-end))
393 (YaTeX-define-key "e" 'YaTeX-end-environment)
394 (YaTeX-define-key "S" 'YaTeX-make-section-region)
395 (YaTeX-define-key "s" 'YaTeX-make-section)
396 (YaTeX-define-key "L" 'YaTeX-make-fontsize-region)
397 (YaTeX-define-key "l" 'YaTeX-make-fontsize)
398 (YaTeX-define-key "m" 'YaTeX-make-singlecmd)
399 (YaTeX-define-key "." 'YaTeX-comment-paragraph)
400 (YaTeX-define-key "," 'YaTeX-uncomment-paragraph)
401 (YaTeX-define-key ">" 'YaTeX-comment-region)
402 (YaTeX-define-key "<" 'YaTeX-uncomment-region)
403 (YaTeX-define-key "g" 'YaTeX-goto-corresponding-*)
404 (YaTeX-define-key "k" 'YaTeX-kill-*)
405 (YaTeX-define-key "c" 'YaTeX-change-*)
406 (YaTeX-define-key "a" 'YaTeX-make-accent)
407 (YaTeX-define-key "?" 'YaTeX-help)
408 (YaTeX-define-key "/" 'YaTeX-apropos)
409 (YaTeX-define-key "&" 'YaTeX-what-column)
410 (YaTeX-define-key "d" 'YaTeX-display-hierarchy)
411 (YaTeX-define-key "x" YaTeX-user-extensional-map)
412 (YaTeX-define-key "n"
413 '(lambda () (interactive) (insert "\\\\")))
414 (if YaTeX-dos
415 (define-key YaTeX-prefix-map "\C-r"
416 '(lambda () (interactive)
417 (set-screen-height YaTeX-saved-screen-height) (recenter))))
418 (mapcar
419 (function
420 (lambda (key)
421 (define-key YaTeX-mode-map (car key) 'YaTeX-math-insert-sequence)))
422 YaTeX-math-key-list)
423 )
425 (defvar YaTeX-section-completion-map nil
426 "*Key map used at YaTeX completion in the minibuffer.")
427 (if YaTeX-section-completion-map nil
428 (setq YaTeX-section-completion-map
429 (copy-keymap (or (and (boundp 'gmhist-completion-map)
430 gmhist-completion-map)
431 minibuffer-local-completion-map)))
432 (define-key YaTeX-section-completion-map
433 " " 'YaTeX-minibuffer-complete)
434 (define-key YaTeX-section-completion-map
435 "\C-i" 'YaTeX-minibuffer-complete)
436 (define-key YaTeX-section-completion-map
437 "\C-v" 'YaTeX-read-section-with-overview))
439 (defvar YaTeX-recursive-map nil
440 "*Key map used at YaTeX reading arguments in the minibuffer.")
441 (if YaTeX-recursive-map nil
442 (setq YaTeX-recursive-map (copy-keymap global-map))
443 (define-key YaTeX-recursive-map YaTeX-prefix YaTeX-prefix-map))
445 ;---------- Define other variable ----------
446 (defvar env-name "document" "*Initial tex-environment completion")
447 (defvar section-name "documentstyle" "*Initial tex-section completion")
448 (defvar fontsize-name "large" "*Initial fontsize completion")
449 (defvar single-command "maketitle" "*Initial LaTeX single command")
450 (defvar YaTeX-kanji-code (if YaTeX-dos 1 2)
451 "*File kanji code used by Japanese TeX."
452 )
453 (defvar YaTeX-coding-system nil "File coding system used by Japanese TeX.")
454 (cond
455 (YaTeX-emacs-20
456 (setq YaTeX-coding-system
457 (cdr (assoc YaTeX-kanji-code YaTeX-kanji-code-alist))))
458 ((boundp 'MULE)
459 (setq YaTeX-coding-system
460 (symbol-value (cdr (assoc YaTeX-kanji-code YaTeX-kanji-code-alist)))))
461 ((boundp 'NEMACS)
462 (setq YaTeX-latex-message-code latex-message-kanji-code)))
464 (defvar YaTeX-mode-syntax-table nil
466 "*Syntax table for yatex-mode")
468 (if YaTeX-mode-syntax-table nil
469 (setq YaTeX-mode-syntax-table (make-syntax-table (standard-syntax-table)))
470 (modify-syntax-entry ?\n " " YaTeX-mode-syntax-table)
471 (modify-syntax-entry ?\{ "(}" YaTeX-mode-syntax-table)
472 (modify-syntax-entry ?\} "){" YaTeX-mode-syntax-table)
473 )
475 ;---------- Provide YaTeX-mode ----------
476 ;;;
477 ;; Major mode definition
478 ;;;
479 (defun yatex-mode ()
480 " Yet Another LaTeX mode: Major mode for editing input files of LaTeX.
481 -You can invoke processes concerning LaTeX typesetting by
482 \\[YaTeX-typeset-menu]
483 -Complete LaTeX environment form of `\\begin{env} ... \\end{env}' by
484 \\[YaTeX-make-begin-end]
485 -Enclose region into some environment by
486 \\[universal-argument] \\[YaTeX-make-begin-end]
487 -Complete LaTeX command which takes argument like `\\section{}' by
488 \\[YaTeX-make-section]
489 -Put LaTeX command which takes no arguments like `\\maketitle' by
490 \\[YaTeX-make-singlecmd]
491 -Complete font or character size descriptor like `{\\large }' by
492 \\[YaTeX-make-fontsize]
493 -Enclose region into those descriptors above by
494 \\[universal-argument] \\[YaTeX-make-fontsize]
495 -Enter European accent notations by
496 \\[YaTeX-make-accent]
497 -Toggle various modes of YaTeX by
498 \\[YaTeX-switch-mode-menu]
499 -Change environt name (on the begin/end line) by
500 \\[YaTeX-change-*]
501 -Kill LaTeX command/environment sequences by
502 \\[YaTeX-kill-*]
503 -Kill LaTeX command/environment with its contents
504 \\[universal-argument] \\[YaTeX-kill-*]
505 -Go to corresponding object (begin/end, file, labels) by
506 \\[YaTeX-goto-corresponding-*] or
507 \\[YaTeX-goto-corresponding-*-other-window] (in other window)
508 \\[YaTeX-goto-corresponding-*-other-frame] (in other frame)
509 -Go to main LaTeX source text by
510 \\[YaTeX-visit-main] or
511 \\[YaTeX-visit-main-other-window] (in other window)
512 \\[YaTeX-visit-main-other-frame] (in other frame)
513 -Comment out or uncomment region by
514 \\[YaTeX-comment-region] or \\[YaTeX-uncomment-region]
515 -Comment out or uncomment paragraph by
516 \\[YaTeX-comment-paragraph] or \\[YaTeX-uncomment-paragraph]
517 -Make an \\item entry hang-indented by
518 \\[YaTeX-fill-item]
519 -Enclose the region with parentheses by
520 \\[YaTeX-insert-parens-region]
521 \\[YaTeX-insert-braces-region]
522 \\[YaTeX-insert-brackets-region]
523 \\[YaTeX-insert-dollars-region]
524 -Look up the corresponding column header of tabular environment by
525 \\[YaTeX-what-column]
526 -Enter a newline and an entry suitable for environment by
527 \\[YaTeX-intelligent-newline]
528 -View the structure of file inclusion by
529 \\[YaTeX-display-hierarchy]
530 -Refer the online help of popular LaTeX commands by
531 \\[YaTeX-help] (help)
532 \\[YaTeX-apropos] (apropos)
533 -Edit `%# notation' by
534 \\[YaTeX-%-menu]
536 Those are enough for fastening your editing of LaTeX source. But further
537 more features are available and they are documented in the manual.
538 "
539 (interactive)
540 (kill-all-local-variables)
541 (setq major-mode 'yatex-mode)
542 (setq mode-name (if YaTeX-japan "やてふ" "YaTeX"))
543 (mapcar 'make-local-variable
544 '(dvi2-command fill-column fill-prefix
545 tmp-env-table tmp-section-table tmp-fontsize-table
546 tmp-singlecmd-table paragraph-start paragraph-separate
547 YaTeX-math-mode indent-line-function
548 comment-start comment-start-skip
549 ))
550 (cond ((boundp 'MULE)
551 (set-file-coding-system YaTeX-coding-system))
552 ((and YaTeX-emacs-20 (fboundp 'coding-system-equal))
553 (let ((mp (buffer-modified-p))
554 (ud (memq
555 buffer-file-coding-system
556 '(undecided undecided-unix undecided-dos undecided-mac))))
557 (if (coding-system-equal
558 YaTeX-coding-system buffer-file-coding-system)
559 nil ; if coding-system is the same, do nothing
560 (set-buffer-file-coding-system YaTeX-coding-system)
561 (if ud (set-buffer-modified-p mp)))))
562 ((featurep 'mule)
563 (set-file-coding-system YaTeX-coding-system))
564 ((boundp 'NEMACS)
565 (make-local-variable 'kanji-fileio-code)
566 (setq kanji-fileio-code YaTeX-kanji-code)))
567 (setq fill-column YaTeX-fill-column
568 fill-prefix YaTeX-fill-prefix
569 paragraph-start YaTeX-paragraph-start
570 paragraph-separate YaTeX-paragraph-separate
571 indent-line-function 'YaTeX-indent-line
572 comment-start YaTeX-comment-prefix
573 comment-end ""
574 ;;comment-start-skip "[^\\\\]%+[ \t]*"
575 )
576 (use-local-map YaTeX-mode-map)
577 (set-syntax-table YaTeX-mode-syntax-table)
578 (if YaTeX-dos (setq YaTeX-saved-screen-height (screen-height)))
579 (YaTeX-read-user-completion-table)
580 (and (fboundp 'YaTeX-19-collect-macros) (YaTeX-19-collect-macros))
581 (turn-on-auto-fill) ;1.63
582 (and (= 0 (buffer-size)) (file-exists-p YaTeX-template-file)
583 (y-or-n-p (format "Insert %s?" YaTeX-template-file))
584 (insert-file-contents (expand-file-name YaTeX-template-file)))
585 (run-hooks 'text-mode-hook 'yatex-mode-hook)
586 )
588 ;---------- Define YaTeX-mode functions ----------
589 (defvar YaTeX-ec "\\" "Escape character of current mark-up language.")
590 (defvar YaTeX-ec-regexp (regexp-quote YaTeX-ec))
591 (defvar YaTeX-struct-begin
592 (concat YaTeX-ec "begin{%1}%2")
593 "Keyword format of begin-environment.")
594 (defvar YaTeX-struct-end
595 (concat YaTeX-ec "end{%1}")
596 "Keyword format of end-environment.")
597 (defvar YaTeX-struct-name-regexp "[^}]+"
598 "Environment name regexp.")
599 (defvar YaTeX-TeX-token-regexp
600 (cond (YaTeX-japan "[A-Za-z*あ-ん亜-龠]+")
601 (t "[A-Za-z*]+"))
602 "Regexp of characters which can be a member of TeX command's name.")
603 (defvar YaTeX-command-token-regexp YaTeX-TeX-token-regexp
604 "Regexp of characters which can be a member of current mark up language's command name.")
606 ;;(defvar YaTeX-struct-section
607 ;; (concat YaTeX-ec "%1{%2}")
608 ;; "Keyword to make section.")
610 ;;;
611 ;; autoload section
612 ;;;
614 ;;autoload from yatexprc.el
615 (autoload 'YaTeX-visit-main "yatexprc" "Visit main LaTeX file." t)
616 (autoload 'YaTeX-visit-main-other-window "yatexprc"
617 "Visit main other window." t)
618 (autoload 'YaTeX-main-file-p "yatexprc" "Check if the file is main." t)
619 (autoload 'YaTeX-get-builtin "yatexprc" "Get %# built-in." t)
620 (autoload 'YaTeX-system "yatexprc" "Call system command" t)
621 (autoload 'YaTeX-save-buffers "yatexprc" "Save buffers of same major mode" t)
623 ;;autoload from yatexmth.el
624 (autoload 'YaTeX-math-insert-sequence "yatexmth" "Image input." t)
625 (autoload 'YaTeX-in-math-mode-p "yatexmth" "Check if in math-env." t)
626 (autoload 'YaTeX-toggle-math-mode "yatexmth" "YaTeX math-mode interfaces." t)
627 (autoload 'YaTeX-math-member-p "yatexmth" "Check if a word is math command." t)
629 ;;autoload from yatexhlp.el
630 (autoload 'YaTeX-help "yatexhlp" "YaTeX helper with LaTeX commands." t)
631 (autoload 'YaTeX-apropos "yatexhlp" "Apropos for (La)TeX commands." t)
633 ;;autoload from yatexgen.el
634 (autoload 'YaTeX-generate "yatexgen" "YaTeX add-in function generator." t)
635 (autoload 'YaTeX-generate-simple "yatexgen" "YaTeX add-in support." t)
637 ;;autoload from yatexsec.el
638 (autoload 'YaTeX-read-section-in-minibuffer "yatexsec" "YaTeX sectioning" t)
639 (autoload 'YaTeX-make-section-with-overview "yatexsec" "YaTeX sectioning" t)
641 ;;autoload from yatexenv.el
642 (autoload 'YaTeX-what-column "yatexenv" "YaTeX env. specific funcs" t)
643 (autoload 'YaTeX-intelligent-newline "yatexenv" "YaTeX env. specific funcs" t)
644 (autoload 'YaTeX-indent-line-equation "yatexenv" "Indent equation lines." t)
645 (autoload 'YaTeX-goto-corresponding-leftright "yatexenv" "\left\right jumps" t)
647 ;;autoload from yatexhie.el
648 (autoload 'YaTeX-display-hierarchy "yatexhie"
649 "YaTeX document hierarchy browser" t)
650 (autoload 'YaTeX-display-hierarchy-directly "yatexhie"
651 "Same as YaTeX-display-hierarchy. Call from mouse." t)
653 ;; autoload from yahtml.el
654 (autoload 'yahtml-inner-environment-but "yahtml" "yahtml internal func." t)
656 ;;;
657 ;; YaTeX-mode functions
658 ;;;
659 (defun YaTeX-insert-begin-end (env region-mode)
660 "Insert \\begin{mode-name} and \\end{mode-name}.
661 This works also for other defined begin/end tokens to define the structure."
662 (setq YaTeX-current-completion-type 'begin)
663 (let*((ccol (current-column)) beg beg2 exchange
664 (arg region-mode) ;for old compatibility
665 (indent-column (+ ccol YaTeX-environment-indent))(i 1) func)
666 (if (and region-mode (> (point) (mark)))
667 (progn (exchange-point-and-mark)
668 (setq exchange t
669 ccol (current-column)
670 indent-column (+ ccol YaTeX-environment-indent))))
671 ;;VER2 (insert "\\begin{" env "}" (YaTeX-addin env))
672 (setq beg (point))
673 (YaTeX-insert-struc 'begin env)
674 (setq beg2 (point))
675 (insert "\n")
676 (indent-to indent-column)
677 (save-excursion
678 ;;indent optional argument of \begin{env}, if any
679 (while (> (point-beginning-of-line) beg)
680 (skip-chars-forward "\\s " (point-end-of-line))
681 (indent-to indent-column)
682 (forward-line -1)))
683 (require 'yatexenv)
684 (if region-mode
685 ;;if region-mode, indent all text in the region
686 (save-excursion
687 (if (fboundp (intern-soft (concat "YaTeX-enclose-" env)))
688 (funcall (intern-soft (concat "YaTeX-enclose-" env))
689 (point) (mark))
690 (while (< (progn (forward-line 1) (point)) (mark))
691 (if (eolp) nil
692 (skip-chars-forward " \t\n")
693 (indent-to indent-column))))))
694 (if region-mode (exchange-point-and-mark))
695 (indent-to ccol)
696 ;;VER2 (insert "\\end{" env "}\n")
697 (YaTeX-insert-struc 'end env)
698 (YaTeX-reindent ccol)
699 (if region-mode
700 (progn
701 (insert "\n")
702 (or exchange (exchange-point-and-mark)))
703 (goto-char beg2)
704 (YaTeX-intelligent-newline nil)
705 (YaTeX-indent-line))
706 (if YaTeX-current-position-register
707 (point-to-register YaTeX-current-position-register)))
708 )
710 (defun YaTeX-make-begin-end (arg)
711 "Make LaTeX environment command of \\begin{env.} ... \\end{env.}
712 by completing read.
713 If you invoke this command with universal argument,
714 \(key binding for universal-argument is \\[universal-argument]\)
715 you can put REGION into that environment between \\begin and \\end."
716 (interactive "P")
717 (let*
718 ((mode (if arg " region" ""))
719 (env
720 (YaTeX-read-environment
721 (format "Begin environment%s(default %s): " mode env-name))))
722 (if (string= env "")
723 (setq env env-name))
724 (setq env-name env)
725 (YaTeX-update-table
726 (list env-name) 'env-table 'user-env-table 'tmp-env-table)
727 (YaTeX-insert-begin-end env-name arg))
728 )
730 (defun YaTeX-make-begin-end-region ()
731 "Call YaTeX-make-begin-end with ARG to specify region mode."
732 (interactive)
733 (YaTeX-make-begin-end t)
734 )
736 (defun YaTeX-make-section (arg &optional beg end cmd)
737 "Make LaTeX \\section{} type command with completing read.
738 With numeric ARG, you can specify the number of arguments of
739 LaTeX command.
740 For example, if you want to produce LaTeX command
742 \\addtolength{\\topmargin}{8mm}
744 which has two arguments. You can produce that sequence by typing...
745 ESC 2 C-c s add SPC RET \\topm SPC RET 8mm RET
746 \(by default\)
747 Then yatex will automatically complete `addtolength' with two arguments
748 next time.
749 You can complete symbol at LaTeX command and the 1st argument.
751 If the optional 2nd and 3rd argument BEG END are specified, enclose
752 the region from BEG to END into the first argument of the LaTeX sequence.
753 Optional 4th arg CMD is LaTeX command name, for non-interactive use."
754 (interactive "P")
755 (setq YaTeX-current-completion-type 'section)
756 (if (equal arg '(4)) (setq beg (region-beginning) end (region-end)))
757 (unwind-protect
758 (let*
759 ((source-window (selected-window))
760 (section
761 (or cmd
762 (YaTeX-read-section
763 (if YaTeX-simple-messages
764 (format "Section-type (default %s): " section-name)
765 (if (> (minibuffer-depth) 0)
766 (format "%s???{} (default %s)%s: " YaTeX-ec section-name
767 (format "[level:%d]" (minibuffer-depth)))
768 (format "(C-v for view-section) %s???{%s} (default %s): "
769 YaTeX-ec (if beg "region" "") section-name)))
770 nil)))
771 (section (if (string= section "") section-name section))
772 (numarg ;; The number of section-type command's argument
773 (or (and (numberp arg) arg)
774 (nth 1 (YaTeX-lookup-table section 'section))
775 1))
776 (arg-reader (intern-soft (concat "YaTeX::" section)))
777 (addin-args (and arg-reader (fboundp arg-reader)))
778 (title "")
779 (j 1)
780 (enable-recursive-minibuffers t));;let
781 (setq section-name section)
782 (if beg
783 (let ((e (make-marker)))
784 (goto-char end)
785 (insert "}")
786 (set-marker e (point))
787 (goto-char beg)
788 (insert YaTeX-ec section-name "{")
789 (goto-char e)
790 (set-marker e nil))
791 (use-global-map YaTeX-recursive-map)
792 (if (= numarg 0) (YaTeX-make-singlecmd section-name)
793 (progn (insert YaTeX-ec section-name)
794 (insert (YaTeX-addin section-name))))
795 (while (<= j numarg)
796 (insert
797 "{"
798 (setq title
799 (cond
800 (addin-args (funcall arg-reader j))
801 (YaTeX-skip-default-reader "")
802 (t
803 (read-string (format "Argument %d of %s: " j section)))))
804 "}")
805 (setq j (1+ j))))
806 (YaTeX-update-table
807 (if (/= numarg 1) (list section numarg)
808 (list section))
809 'section-table 'user-section-table 'tmp-section-table)
810 (if YaTeX-current-position-register
811 (point-to-register YaTeX-current-position-register))
812 (if (string= (buffer-substring (- (point) 2) (point)) "{}")
813 (forward-char -1))
814 (while (string= (buffer-substring (- (point) 3) (1- (point))) "{}")
815 (forward-char -2)))
816 (if (<= (minibuffer-depth) 0) (use-global-map global-map)))
817 )
819 (defun YaTeX-make-section-region (args beg end)
820 "Call YaTeX-make-section with arguments to specify region mode."
821 (interactive "P\nr")
822 (YaTeX-make-section args beg end)
823 )
825 (defun YaTeX-make-fontsize (arg &optional fontsize)
826 "Make completion like {\\large ...} or {\\slant ...} in minibuffer.
827 If you invoke this command with universal argument, you can put region
828 into {\\xxx } braces.
829 \(key binding for universal-argument is \\[universal-argument]\)"
830 (interactive "P")
831 (YaTeX-sync-local-table 'tmp-fontsize-table)
832 (let* ((mode (if arg "region" ""))
833 (fontsize
834 (or fontsize
835 (YaTeX-read-fontsize
836 (if YaTeX-simple-messages
837 (format "Font or size (default %s): " fontsize-name)
838 (format "{\\??? %s} (default %s)%s: " mode fontsize-name
839 (if (> (minibuffer-depth) 0)
840 (format "[level:%d]" (minibuffer-depth)) "")))
841 nil nil))))
842 (if (string= fontsize "")
843 (setq fontsize fontsize-name))
844 (setq YaTeX-current-completion-type 'large)
845 (setq fontsize-name fontsize)
846 (YaTeX-update-table
847 (list fontsize-name)
848 'fontsize-table 'user-fontsize-table 'tmp-fontsize-table)
849 (if arg
850 (save-excursion
851 (if (> (point) (mark)) (exchange-point-and-mark))
852 (insert "{\\" fontsize-name " ")
853 (exchange-point-and-mark)
854 (insert "}"))
855 (insert "{\\" fontsize-name " ")
856 (if YaTeX-current-position-register
857 (point-to-register YaTeX-current-position-register))
858 (save-excursion
859 (insert (YaTeX-addin fontsize-name) "}"))))
860 )
862 (defun YaTeX-make-fontsize-region ()
863 "Call function:YaTeX-make-fontsize with ARG to specify region mode."
864 (interactive)
865 (YaTeX-make-fontsize t)
866 )
868 (defvar YaTeX-singlecmd-suffix "" "*Suffix for maketitle-type commands.")
869 (defvar YaTeX-read-singlecmd-history nil "Holds maketitle-type history.")
870 (put 'YaTeX-read-singlecmd-history 'no-default t)
871 (defun YaTeX-make-singlecmd (single)
872 (interactive
873 (list (YaTeX-cplread-with-learning
874 (if YaTeX-simple-messages
875 (format "maketitle-type (default %s): " single-command)
876 (format "%s??? (default %s)%s: " YaTeX-ec single-command
877 (if (> (minibuffer-depth) 0)
878 (format "[level:%d]" (minibuffer-depth)) "")))
879 'singlecmd-table 'user-singlecmd-table 'tmp-singlecmd-table
880 nil nil nil 'YaTeX-read-singlecmd-history)))
881 (if (string= single "")
882 (setq single single-command))
883 (setq single-command single)
884 (setq YaTeX-current-completion-type 'maketitle)
885 (let ((dollar (and (not (YaTeX-in-math-mode-p))
886 (YaTeX-math-member-p single-command)))
887 p q)
888 (if dollar (insert "$"))
889 (insert YaTeX-ec single-command)
890 (setq p (point))
891 (insert (YaTeX-addin single) YaTeX-singlecmd-suffix)
892 (if dollar (insert "$"))
893 (setq q (point))
894 (goto-char p)
895 (forward-char -2)
896 (if (looking-at "\\[\\]") (forward-char 1) (goto-char q)))
897 (if YaTeX-current-position-register
898 (point-to-register YaTeX-current-position-register))
899 )
901 (defvar YaTeX-completion-begin-regexp "[{\\]"
902 "Regular expression of limit where LaTeX command's completion begins.")
904 (defun YaTeX-do-completion ()
905 "Try completion on LaTeX command preceding point."
906 (interactive)
907 (if
908 (or (eq (preceding-char) ? )
909 (eq (preceding-char) ?\t)
910 (eq (preceding-char) ?\n)
911 (bobp))
912 (message "Nothing to complete.") ;Do not complete
913 (let* ((end (point))
914 (limit (point-beginning-of-line))
915 (completion-begin
916 (progn (re-search-backward "[ \t\n]" limit 1) (point)))
917 (begin (progn
918 (goto-char end)
919 (if (re-search-backward YaTeX-completion-begin-regexp
920 completion-begin t)
921 (1+ (point))
922 nil))))
923 (goto-char end)
924 (cond
925 ((null begin)
926 (message "I think it is not a LaTeX sequence."))
927 (t
928 (mapcar 'YaTeX-sync-local-table
929 '(tmp-section-table tmp-env-table tmp-singlecmd-table))
930 (let*((pattern (buffer-substring begin end))
931 (all-table
932 (append
933 section-table user-section-table tmp-section-table
934 env-table user-env-table tmp-env-table
935 singlecmd-table user-singlecmd-table tmp-singlecmd-table))
936 ;; First,
937 ;; search completion without backslash.
938 (completion (try-completion pattern all-table)))
939 (if
940 (eq completion nil)
941 ;; Next,
942 ;; search completion with backslash
943 (setq completion
944 (try-completion (buffer-substring (1- begin) end)
945 all-table nil)
946 begin (1- begin)))
947 (cond
948 ((null completion)
949 (message (concat "Can't find completion for '" pattern "'"))
950 (ding))
951 ((eq completion t) (message "Sole completion."))
952 ((not (string= completion pattern))
953 (kill-region begin end)
954 (insert completion)
955 )
956 (t
957 (message "Making completion list...")
958 (with-output-to-temp-buffer "*Help*"
959 (display-completion-list
960 (all-completions pattern all-table))))))))))
961 )
963 (defun YaTeX-toggle-modify-mode (&optional arg)
964 (interactive "P")
965 (or (memq 'YaTeX-modify-mode mode-line-format)
966 (setq mode-line-format
967 (append (list "" 'YaTeX-modify-mode) mode-line-format)))
968 (if (or arg (null YaTeX-modify-mode))
969 (progn
970 (setq YaTeX-modify-mode "*m*")
971 (message "Modify mode"))
972 (setq YaTeX-modify-mode nil)
973 (message "Cancel modify mode."))
974 (set-buffer-modified-p (buffer-modified-p)) ;redraw mode-line
975 )
977 (defun YaTeX-switch-mode-menu (arg &optional char)
978 (interactive "P")
979 (message "Toggle: (M)odify-mode ma(T)h-mode")
980 (let ((c (or char (read-char))))
981 (cond
982 ((= c ?m) (YaTeX-toggle-modify-mode arg))
983 ((or (= c ?$) (= c ?t))
984 (if YaTeX-auto-math-mode
985 (message "Makes no sense in YaTeX-auto-math-mode.")
986 (YaTeX-toggle-math-mode arg)))))
987 )
989 (defun YaTeX-insert-quote ()
990 (interactive)
991 (insert
992 (cond
993 ((YaTeX-literal-p) ?\")
994 ((= (preceding-char) ?\\ ) ?\")
995 ;((= (preceding-char) ?\( ) ?\")
996 ((or (= (preceding-char) 32)
997 (= (preceding-char) 9)
998 (= (preceding-char) ?\n)
999 (bobp)
1000 (string-match
1001 (regexp-quote (char-to-string (preceding-char)))
1002 "、。,.?!「」『』【】()"))
1003 "``")
1004 (t "''")))
1007 (defun YaTeX-closable-p ()
1008 (and (not YaTeX-modify-mode)
1009 (not (eq YaTeX-close-paren-always 'never))
1010 (or YaTeX-close-paren-always (eolp))
1011 (not (input-pending-p))
1012 (not (YaTeX-literal-p)))
1013 ;;(or YaTeX-modify-mode
1014 ;; (and (not YaTeX-close-paren-always) (not (eolp)))
1015 ;; (input-pending-p)
1016 ;; (YaTeX-quick-in-environment-p "verbatim"))
1019 (defun YaTeX-insert-braces-region (beg end &optional open close)
1020 (interactive "r")
1021 (save-excursion
1022 (goto-char end)
1023 (insert (or close "}"))
1024 (goto-char beg)
1025 (insert (or open "{")))
1028 (defun YaTeX-insert-braces (arg &optional open close)
1029 (interactive "p")
1030 (let (env)
1031 (cond
1032 ((YaTeX-jmode) (YaTeX-self-insert arg))
1033 ((not (YaTeX-closable-p)) (YaTeX-self-insert arg))
1034 ((save-excursion
1035 (and (> (- (point) (point-min)) 6)
1036 (condition-case () (forward-char -6) (error nil)))
1037 (looking-at "\\\\left\\\\"))
1038 (insert "{\\right\\}")
1039 (forward-char -8))
1040 ((and (> (point) (+ (point-min) 4))
1041 (save-excursion (backward-char 4) (looking-at "\\\\end"))
1042 (not (YaTeX-literal-p))
1043 (setq env (YaTeX-inner-environment)))
1044 (momentary-string-display
1045 (concat
1046 "{"
1047 (cond
1048 (YaTeX-japan
1049 (format "今度からはちゃんと %s b を使いましょう" YaTeX-prefix))
1050 (t (format "You don't understand Zen of `%s b':p" YaTeX-prefix)))
1051 "}")
1052 (point))
1053 (insert (or open "{") env (or close "}")))
1054 (t
1055 (insert (or open "{") (or close "}"))
1056 (forward-char -1)
1057 (if (and (eq (char-after (point)) ?\})
1058 (eq (char-after (- (point) 2)) ?\\ ))
1059 (progn (insert "\\") (forward-char -1)))
1060 )))
1063 (defun YaTeX-jmode ()
1064 (or (and (boundp 'canna:*japanese-mode*) canna:*japanese-mode*)
1065 (and (boundp 'egg:*mode-on*) egg:*mode-on* egg:*input-mode*))
1068 (defun YaTeX-jmode-off ()
1069 (cond
1070 ((and (boundp 'canna:*japanese-mode*) canna:*japanese-mode*)
1071 (canna-toggle-japanese-mode))
1072 ((and (boundp 'egg:*mode-on*) egg:*mode-on* egg:*input-mode*)
1073 (egg:toggle-egg-mode-on-off))
1074 ((and (fboundp 'skk-mode) (boundp 'skk-mode) skk-mode)
1075 (if (fboundp 'skk-mode-off) (skk-mode-off) (j-mode-off)))
1076 ((and (fboundp 'fep-force-off) (fep-force-off))))
1079 (defun YaTeX-self-insert (arg)
1080 (call-interactively (global-key-binding (char-to-string last-command-char))))
1082 (defun YaTeX-insert-brackets (arg)
1083 "Insert Kagi-kakko or \\ [ \\] pair or simply \[."
1084 (interactive "p")
1085 (let ((col (1- (current-column))))
1086 (cond
1087 ((YaTeX-jmode) (YaTeX-self-insert arg))
1088 ((not (YaTeX-closable-p))
1089 (YaTeX-self-insert arg))
1090 ((save-excursion
1091 (and (> (- (point) (point-min)) 5) (forward-char -5))
1092 (looking-at "\\\\left"))
1093 (insert "[\\right]")
1094 (forward-char -7))
1095 ((and (= (preceding-char) ?\\ )
1096 (/= (char-after (- (point) 2)) ?\\ )
1097 (not (YaTeX-in-math-mode-p)))
1098 (insert last-command-char "\n")
1099 (indent-to (max 0 col))
1100 (insert "\\]")
1101 (beginning-of-line)
1102 (open-line 1)
1103 (delete-region (point) (progn (beginning-of-line) (point)))
1104 (indent-to (+ YaTeX-environment-indent (max 0 col)))
1105 (or YaTeX-auto-math-mode YaTeX-math-mode (YaTeX-toggle-math-mode 1)))
1106 ((YaTeX-closable-p)
1107 (insert "[]")
1108 (backward-char 1))
1109 (t (YaTeX-self-insert arg)))
1113 (defun YaTeX-insert-brackets-region (beg end)
1114 (interactive "r")
1115 (YaTeX-insert-braces-region beg end "[" "]")
1118 (defun YaTeX-insert-parens (arg)
1119 "Insert parenthesis pair."
1120 (interactive "p")
1121 (cond
1122 ((YaTeX-jmode) (YaTeX-self-insert arg))
1123 ((not (YaTeX-closable-p)) (YaTeX-self-insert arg))
1124 ((save-excursion
1125 (and (> (- (point) (point-min)) 5) (forward-char -5))
1126 (looking-at "\\\\left"))
1127 (insert "(\\right)")
1128 (forward-char -7))
1129 ((and (= (preceding-char) ?\\ ) (not (YaTeX-in-math-mode-p)))
1130 (insert "(\\)")
1131 (backward-char 2))
1132 ((YaTeX-closable-p)
1133 (insert "()")
1134 (backward-char 1))
1135 (t (YaTeX-self-insert arg)))
1138 (defun YaTeX-insert-parens-region (beg end)
1139 (interactive "r")
1140 (YaTeX-insert-braces-region beg end "(" ")")
1143 (defun YaTeX-insert-dollar ()
1144 (interactive)
1145 (if (or (not (YaTeX-closable-p))
1146 (= (preceding-char) 92)
1147 (and (YaTeX-in-math-mode-p)
1148 (or (/= (preceding-char) ?$) (/= (following-char) ?$))))
1149 (insert "$")
1150 (insert "$$")
1151 (forward-char -1)
1152 (YaTeX-jmode-off)
1153 (or YaTeX-auto-math-mode YaTeX-math-mode (YaTeX-toggle-math-mode 1)))
1156 (defun YaTeX-insert-dollars-region (beg end)
1157 (interactive "r")
1158 (YaTeX-insert-braces-region beg end "$" "$")
1161 (defun YaTeX-insert-amper ()
1162 (interactive)
1163 (if (or (string-match YaTeX-array-env-regexp
1164 (or (YaTeX-inner-environment t) "document"))
1165 (= (preceding-char) 92)
1166 (YaTeX-literal-p))
1167 (insert "&")
1168 (insert "\\&"))
1171 (defun YaTeX-version ()
1172 "Return string of the version of running YaTeX."
1173 (interactive)
1174 (message
1175 (concat "Yet Another tex-mode "
1176 (if YaTeX-japan "「野鳥」" "`Wild Bird'")
1177 " Revision "
1178 YaTeX-revision-number))
1181 (defun YaTeX-typeset-menu (arg &optional char)
1182 "Typeset, preview, visit error and miscellaneous convenient menu.
1183 Optional second argument CHAR is for non-interactive call from menu."
1184 (interactive "P")
1185 (message
1186 (concat "J)latex R)egion B)ibtex mk(I)ndex "
1187 (if (fboundp 'start-process) "K)ill-latex ")
1188 "P)review "
1189 (and (boundp 'window-system) window-system "S)earch ")
1190 "V)iewerr L)pr"))
1191 (let ((sw (selected-window)) (c (or char (read-char))))
1192 (require 'yatexprc) ;for Nemacs's bug
1193 (select-window sw)
1194 (cond
1195 ((= c ?j) (YaTeX-typeset-buffer))
1196 ((= c ?r) (YaTeX-typeset-region))
1197 ((= c ?b) (YaTeX-call-command-on-file
1198 bibtex-command "*YaTeX-bibtex*"))
1199 ((= c ?i) (YaTeX-call-command-on-file
1200 makeindex-command "*YaTeX-makeindex*"))
1201 ((= c ?k) (YaTeX-kill-typeset-process YaTeX-typeset-process))
1202 ((= c ?p) (call-interactively 'YaTeX-preview))
1203 ((= c ?q) (YaTeX-system "lpq" "*Printer queue*"))
1204 ((= c ?v) (YaTeX-view-error))
1205 ((= c ?l) (YaTeX-lpr arg))
1206 ((= c ?m) (YaTeX-switch-mode-menu arg))
1207 ((= c ?b) (YaTeX-insert-string "\\"))
1208 ((= c ?s) (YaTeX-xdvi-remote-search arg))))
1211 (defun YaTeX-%-menu (&optional beg end char)
1212 "Operate %# notation."
1213 ;;Do not use interactive"r" for the functions which require no mark
1214 (interactive)
1215 (message "!)Edit-%%#! B)EGIN-END-region L)Edit-%%#LPR")
1216 (let ((c (or char (read-char))) (string "") key
1217 (b (make-marker)) (e (make-marker)))
1218 (save-excursion
1219 (cond
1220 ((or (= c ?!) (= c ?l)) ;Edit `%#!'
1221 (goto-char (point-min))
1222 (setq key (cond ((= c ?!) "%#!")
1223 ((= c ?l) "%#LPR")))
1224 (if (re-search-forward key nil t)
1225 (progn
1226 (setq string (buffer-substring (point) (point-end-of-line)))
1227 (delete-region (point) (progn (end-of-line) (point))))
1228 (open-line 1)
1229 (delete-region (point) (progn (beginning-of-line)(point)));for 19 :-<
1230 (insert key))
1231 (unwind-protect
1232 (setq string (read-string (concat key ": ") string))
1233 (insert string)))
1235 ((= c ?b) ;%#BEGIN %#END region
1236 (or end (setq beg (min (point) (mark)) end (max (point) (mark))))
1237 (set-marker b beg)
1238 (set-marker e end)
1239 (goto-char (point-min))
1240 (while (re-search-forward "^%#\\(BEGIN\\)\\|\\(END\\)$" nil t)
1241 (beginning-of-line)
1242 (delete-region (point) (progn (forward-line 1) (point))))
1243 (goto-char b)
1244 (open-line 1)
1245 (delete-region (point) (progn (beginning-of-line)(point)));for 19 :-<
1246 (insert "%#BEGIN")
1247 (goto-char e)
1248 (insert "%#END\n")
1249 (set-marker b nil)
1250 (set-marker e nil))
1251 )))
1254 (defun YaTeX-goto-corresponding-label (reverse &optional otherwin)
1255 "Jump to corresponding \\label{} and \\ref{} or \\cite and \\bibitem.
1256 The default search direction depends on the command at the cursor position.
1257 When the cursor is on \\ref(\\cite), YaTeX will try to search the
1258 corresponding \\label(\\bibitem) backward,
1259 and if it fails search forward again. And when the cursor is
1260 on \\label(\\bibitem), YaTeX will search the corresponding \\ref(\\cite)
1261 forward at first and secondary backward.
1262 Argument REVERSE non-nil makes the default
1263 direction rule reverse. Since Search string is automatically set in
1264 search-last-string, you can repeat search the same label/ref by typing
1265 \\[isearch-forward] or \\[isearch-backward].
1266 If optional second argument OTHERWIN is non-nil, move to other window."
1268 (let ((scmd "") label direc string blist (p (point)) (cb (current-buffer))
1269 (refcommands "label\\|\\(page\\)?ref\\|cite\\|bibitem")
1270 (func (function (lambda (string sfunc)
1271 (or
1272 (funcall sfunc string nil t)
1273 (funcall (if (eq sfunc 're-search-forward)
1274 're-search-backward 're-search-forward)
1275 string nil t))))))
1276 (cond
1277 ((YaTeX-on-section-command-p refcommands)
1278 (setq scmd (cdr (assoc (YaTeX-match-string 1)
1279 '(("label" . "\\(page\\)?ref") ("ref" . "label")
1280 ("pageref" . "label")
1281 ("cite" . "bibitem") ("bibitem" . "cite")))))
1282 (goto-char (match-end 0))
1283 (let ((label (buffer-substring
1284 (1- (point)) (progn (backward-list 1) (1+ (point))))))
1285 ;(setq string (concat "\\" scmd "{" label "}"))
1286 (setq string (concat "\\\\" scmd "{" (regexp-quote label) "}"))
1287 (setq direc (if (string-match "ref\\|cite" scmd)
1288 're-search-forward 're-search-backward))
1289 (if YaTeX-current-position-register
1290 (point-to-register YaTeX-current-position-register))
1291 (if reverse (setq direc (if (eq direc 're-search-forward)
1292 're-search-backward 're-search-forward)))
1293 (if (funcall func string direc) ;label/ref found!
1294 (progn
1295 (if otherwin
1296 (progn
1297 (goto-char p)
1298 (if (one-window-p)
1299 (split-window-calculate-height
1300 YaTeX-default-pop-window-height))
1301 (select-window (get-lru-window))
1302 (switch-to-buffer cb)))
1303 (goto-char (match-beginning 0))
1304 (push-mark p))
1305 ;;if label/ref not found, search through all yatex buffers.
1306 (goto-char p) ;resume position of current buffer
1307 (setq blist (YaTeX-yatex-buffer-list))
1308 (catch 'found
1309 (while blist
1310 (set-buffer (car blist))
1311 (if (YaTeX-on-section-command-p refcommands)
1312 (goto-char (match-beginning 0)))
1313 (if (funcall func string direc)
1314 (progn
1315 (cond
1316 (otherwin
1317 (set-buffer cb)
1318 (goto-char p)
1319 (if (one-window-p)
1320 (split-window-calculate-height
1321 YaTeX-default-pop-window-height))
1322 (select-window (get-lru-window))
1323 (switch-to-buffer (car blist)))
1324 ((or (get-buffer-window (car blist))
1325 (and YaTeX-emacs-19
1326 (get-buffer-window (car blist) t)))
1327 (goto-buffer-window (car blist)))
1328 (t
1329 (switch-to-buffer (car blist))
1330 (message
1331 (format "Type %s %c to return to original position."
1332 (key-description
1333 (car
1334 (or (where-is-internal 'register-to-point)
1335 (where-is-internal 'jump-to-register))))
1336 YaTeX-current-position-register))))
1337 (goto-char (match-beginning 0))
1338 (throw 'found t)))
1339 (setq blist (cdr blist)))))
1341 (if YaTeX-emacs-19
1342 (setq regexp-search-ring
1343 (cons string (delete string regexp-search-ring)))
1344 (setq search-last-regexp string)))
1345 (t nil)))
1348 (defun YaTeX-goto-corresponding-environment (&optional allow-mismatch noerr)
1349 "Go to corresponding begin/end enclosure.
1350 Optional argument ALLOW-MISMATCH allows mismatch open/clese. Use this
1351 for \left(, \right).
1352 Optional third argument NOERR causes no error for unballanced environment."
1353 (interactive)
1354 (if (not (YaTeX-on-begin-end-p)) nil
1355 (let ((p (match-end 0)) b0 b1 env (nest 0) regexp re-s (op (point))
1356 (m0 (match-beginning 0)) ;whole matching
1357 (m1 (match-beginning 1)) ;environment in \begin{}
1358 (m2 (match-beginning 2))) ;environment in \end{}
1359 ;(setq env (regexp-quote (buffer-substring p (match-beginning 0))))
1360 (if (cond
1361 (m1 ;if begin{xxx}
1362 (setq env
1363 (if allow-mismatch YaTeX-struct-name-regexp
1364 (regexp-quote (buffer-substring m1 (match-end 1)))))
1365 ; (setq regexp (concat "\\(\\\\end{" env "}\\)\\|"
1366 ; "\\(\\\\begin{" env "}\\)"))
1367 (setq regexp
1368 (concat
1369 "\\("
1370 (YaTeX-replace-format-args
1371 (regexp-quote YaTeX-struct-end) env "" "")
1372 "\\)\\|\\("
1373 (YaTeX-replace-format-args
1374 (regexp-quote YaTeX-struct-begin) env "" "")
1375 "\\)"))
1376 (setq re-s 're-search-forward))
1377 (m2 ;if end{xxx}
1378 (setq env
1379 (if allow-mismatch YaTeX-struct-name-regexp
1380 (regexp-quote (buffer-substring m2 (match-end 2)))))
1381 ; (setq regexp (concat "\\(\\\\begin{" env "}\\)\\|"
1382 ; "\\(\\\\end{" env "}\\)"))
1383 (setq regexp
1384 (concat
1385 "\\("
1386 (YaTeX-replace-format-args
1387 (regexp-quote YaTeX-struct-begin) env "" "")
1388 "\\)\\|\\("
1389 (YaTeX-replace-format-args
1390 (regexp-quote YaTeX-struct-end) env "" "")
1391 "\\)"))
1392 (setq re-s 're-search-backward))
1393 (t (if noerr nil (error "Corresponding environment not found."))))
1394 (progn
1395 (while (and (>= nest 0) (funcall re-s regexp nil t))
1396 (setq b0 (match-beginning 0) b1 (match-beginning 1))
1397 (if (or (equal b0 m0)
1398 (YaTeX-literal-p b0))
1399 nil
1400 (setq nest (if (equal b0 b1)
1401 (1- nest) (1+ nest)))))
1402 (if (< nest 0)
1403 (goto-char (match-beginning 0)) ;found.
1404 (goto-char op)
1405 (funcall
1406 (if noerr 'message 'error)
1407 "Corresponding environment `%s' not found." env)
1408 (sit-for 1)
1409 nil)))))
1412 (defun YaTeX-goto-corresponding-file (&optional other)
1413 "Visit or switch buffer of corresponding file,
1414 looking at \\input or \\include or \includeonly on current line."
1415 (if (not (YaTeX-on-includes-p)) nil
1416 (let ((parent buffer-file-name) input-file)
1417 (save-excursion
1418 (if (search-forward "{" (point-end-of-line) t)
1419 nil
1420 (skip-chars-backward "^,{"))
1421 (setq input-file
1422 (buffer-substring
1423 (point) (progn (skip-chars-forward "^ ,}") (point))))
1424 (if (not (string-match "\\.\\(tex\\|sty\\)$" input-file))
1425 (setq input-file (concat input-file ".tex"))))
1426 (cond
1427 (other (YaTeX-switch-to-buffer-other-window input-file))
1428 ((get-file-buffer input-file) (goto-buffer-window input-file))
1429 (t (YaTeX-switch-to-buffer input-file)))
1430 (or (YaTeX-get-builtin "!")
1431 YaTeX-parent-file
1432 (setq YaTeX-parent-file parent))))
1435 (defun YaTeX-goto-corresponding-BEGIN-END ()
1436 (if (not (YaTeX-on-BEGIN-END-p)) nil
1437 (if (cond
1438 ((equal (match-beginning 0) (match-beginning 1)) ;if on %#BEGIN
1439 (not (search-forward "%#END" nil t)))
1440 (t ; if on %#END
1441 (not (search-backward "%#BEGIN" nil t))))
1442 (error "Corresponding %%#BEGIN/END not found."))
1443 (beginning-of-line)
1444 t)
1447 (defvar YaTeX-processed-file-regexp-alist nil
1448 "Alist of regexp of processed file regexp vs. its file name part;
1449 For example, if you include image file with `\\epsfile{file=FILE}' where
1450 `FILE' is processed file. You might want to view FILE with other previewer
1451 such as ghostview, or want to preview its source which was drawn with
1452 other drawing tool, tgif for example. Then you should set entire regexp
1453 of including expression and enclose its file name part with \\\\( and \\\\).
1455 Ex. (\"\\\\\\\\epsfile{[^}]*file=\\\\([^,} ]+\\\\)\\\\(\\\\.e?ps\\\\)?[^}]*}\" 1)
1457 Where the first group surrounded by \\\\( and \\\\) is the file name part
1458 of expression. So you should set 1 to second element. And the first
1459 matching group is sent to (image) processor defined by the variable
1460 YaTeX-file-processor-alist. See also the documentation of
1461 YaTeX-file-processor-alist.
1463 ↑じゃ良くわかんないすね。例えば tgif hoge.obj して hoge.eps を
1464 \\epsfile{file=hoge.eps} でインクルードしているとしよう。その行で
1465 \[prefix\] g を押した時に tgif を起動して欲しかったら、まず上のような
1466 正規表現を設定する。\\\\(と\\\\)で囲んだところがファイル名になるように
1467 注意する。でファイル名部分が何番目の\\\\(\\\\)になるかをリストの2番目に書く。
1468 すると、その部分が変数 YaTeX-file-processor-alist で定義された
1469 処理プログラムに渡される。というわけ。
1470 ん〜やっぱりむずかしいね。分からない時は隣の Lisper に聞くか、
1471 fj野鳥の会で聞こう!
1472 ")
1474 (defvar YaTeX-processed-file-regexp-alist-default
1475 '(("\\\\epsfile\\(\\[[^]]+\\]\\)?{[^},]*file=\\(\\([^,} ]*/\\)?[^,}. ]+\\)\\(\\.e?ps\\)?[^}]*}" 2)
1476 ("\\\\epsfig{[^},]*fi\\(le\\|gure\\)=\\(\\([^,} ]*/\\)?[^,}. ]+\\)\\(\\.e?ps\\)?[^}]*}" 2)
1477 ("\\\\postscriptbox{[^}]*}{[^}]*}{\\(\\([^,} ]*/\\)?[^}. ]+\\)\\(\\.e?ps\\)?}" 1)
1478 ("\\\\\\(epsfbox\\|includegraphics\\|epsfig\\)\\*?{\\(\\([^,} ]*/\\)?[^}. ]+\\)\\(\\.e?ps\\)?}" 2) ;\epsfbox{hoge.ps} or \includegraphics{hoge.eps}
1479 ("\\\\\\(psbox\\)\\(\\[[^]]+\\]\\)?{\\(\\([^,} ]*/\\)?[^} ]+\\)\\(\\.e?ps\\)}" 3) ;\psbox[options...]{hoge.eps} (97/1/11)
1480 ("\\\\input{\\([^} ]+\\)\\(\\.tps\\)}" 1) ;tgif2tex (1998/9/16)
1482 "See the documentation of YaTeX-processed-file-regexp-alist."
1485 (defvar YaTeX-file-processor-alist nil
1486 "*Alist of files' processor vs. its extension;
1487 See also the documentation of YaTeX-processed-file-regexp-alist."
1489 (defvar YaTeX-file-processor-alist-default
1490 '(("tgif" . ".obj")
1491 ("ghostview" . ".ps")
1492 ("ghostview" . ".eps")
1493 (t . ".tex")
1494 (t . ".sty")
1495 (t . ""))
1496 "See the documentation of YaTeX-file-processor-alist."
1499 (defun YaTeX-goto-corresponding-file-processor (&optional other)
1500 "Execute corresponding file processor."
1501 (save-excursion
1502 (or (looking-at YaTeX-ec-regexp)
1503 (skip-chars-backward (concat "^" YaTeX-ec) (point-beginning-of-line)))
1504 (let ((list (append YaTeX-processed-file-regexp-alist
1505 YaTeX-processed-file-regexp-alist-default))
1506 (p (point)) flist file
1507 (peol (point-end-of-line)))
1508 (setq flist (catch 'found
1509 (while list
1510 (goto-char p)
1511 (if (re-search-forward (car (car list)) peol t)
1512 (progn
1513 (setq file (YaTeX-match-string
1514 (car (cdr (car list)))))
1515 (throw 'found (cdr (car list)))))
1516 (setq list (cdr list)))))
1517 (if flist ;if pattern and file name found
1518 (let*((plist (append YaTeX-file-processor-alist
1519 YaTeX-file-processor-alist-default))
1520 (plist0 plist)
1521 ext cmd src buf (alt (car (cdr flist))))
1522 (if (and (re-search-forward
1523 (concat YaTeX-comment-prefix "\\s *\\(.*\\)$") peol t)
1524 (assoc (setq cmd (YaTeX-match-string 1))
1525 YaTeX-file-processor-alist))
1526 (setq src ;if processor is specified
1527 (concat file
1528 (cdr (assoc cmd YaTeX-file-processor-alist))))
1529 (while plist ;if processor is not specified
1530 (setq ext (cdr (car plist)))
1531 (if (and (string< "" (concat file ext))
1532 (file-exists-p (concat file ext)))
1533 (setq cmd (car (car plist))
1534 src (concat file ext) plist nil))
1535 (setq plist (cdr plist)))
1536 (if (and (null src) alt YaTeX-create-file-prefix-g)
1537 (setq cmd alt
1538 src (concat file (cdr (assoc alt plist0))))))
1539 (if src ;if processor and src file found
1540 (cond
1541 ((stringp cmd)
1542 (let ((buf (concat "* " cmd " " src " *")))
1543 (YaTeX-system (concat cmd " " src) buf)
1544 t))
1545 ((eq t cmd)
1546 (let ((parent buffer-file-name))
1547 (funcall
1548 (cond
1549 (other 'YaTeX-switch-to-buffer-other-window)
1550 ((get-file-buffer src) 'goto-buffer-window)
1551 (t 'YaTeX-switch-to-buffer))
1552 src)
1553 (or (YaTeX-get-builtin "!")
1554 YaTeX-parent-file
1555 (setq YaTeX-parent-file parent))
1556 t))
1557 ((symbolp cmd)
1558 (cond
1559 ((symbol-function cmd)
1560 (funcall cmd src other)))
1561 t)))))))
1564 (defun YaTeX-on-section-command-p (command)
1565 "Check if point is on the LaTeX command: COMMAND(regexp).
1566 Return nil if point is not on it. Otherwise return the
1567 number of argument position.
1568 Section command name is stored in match-data #1."
1569 (let ((p (point)) md (parg 0) (argc 1) word (grouping 0) (i 0))
1570 (while (setq i (string-match "\\\\(" command i))
1571 (setq grouping (1+ grouping) i (+ i 2)))
1572 (save-excursion
1573 (if (looking-at YaTeX-ec-regexp) nil
1574 (catch 'found ;caught value has no meaning
1575 (while t
1576 (if (bobp) (throw 'found nil))
1577 (cond
1578 ((looking-at YaTeX-ec-regexp) (throw 'found t))
1579 ((looking-at "[[{]") nil)
1580 ((looking-at "[]}]")(condition-case nil (up-list -1) (error nil)))
1581 (t (skip-chars-backward " \t\r\n")))
1582 (skip-chars-backward (concat "^ \t\r\n{}[]" YaTeX-ec-regexp))
1583 (or (bobp) (forward-char -1)))))
1584 (if (and
1585 (looking-at (concat YaTeX-ec-regexp "\\(" command "\\)"
1586 "\\(\\(\\[[^]]+\\]\\)*\\)" ;optional arg
1587 ;"[ \t\n\r]*{[^}]+}")) ;arg braces
1588 "[ \t\n\r]*{[^}]*}")) ;arg braces
1589 (not (YaTeX-lookup-table
1590 (setq word (YaTeX-match-string 1)) 'singlecmd)))
1591 (progn
1592 (setq md (match-data))
1593 (skip-chars-forward "^{")
1594 (if (<= (point) p) (setq parg (1+ parg)))
1595 (setq argc
1596 (or (car (cdr (YaTeX-lookup-table word 'section)))
1597 argc))
1598 (while (and (>= (setq argc (1- argc)) 0)
1599 (progn (skip-chars-forward " \t\n\r")
1600 (looking-at "{")))
1601 (forward-list 1)
1602 (if (<= (point) p) (setq parg (1+ parg))))
1603 (store-match-data md)
1604 (setq i (+ 2 grouping))
1605 (if (and (match-beginning i)
1606 (>= p (match-beginning i)) (< p (match-end i)))
1607 -1 ;return -1 if point is on optional arg
1608 (if (< p (point)) parg))
1609 ))))
1612 (defun YaTeX-on-maketitle-p ()
1613 "Check if point is on maketitle type commands.
1614 Call this function after YaTeX-on-section-command-p."
1615 (let ((p (point)))
1616 (save-excursion
1617 (or (= (char-after (point)) ?\\ )
1618 (progn
1619 (skip-chars-backward
1620 (concat "^" YaTeX-ec-regexp) (point-beginning-of-line))
1621 (or (bobp) (bolp) (backward-char 1))))
1622 (and (looking-at (concat YaTeX-ec-regexp YaTeX-TeX-token-regexp))
1623 (<= (match-beginning 0) p)
1624 (> (match-end 0) p)))))
1626 (defun YaTeX-on-begin-end-p ()
1627 (save-excursion
1628 (beginning-of-line)
1629 (re-search-forward
1630 ;;"\\\\begin{\\([^}]+\\)}\\|\\\\end{\\([^}]+\\)}"
1631 (concat
1632 (YaTeX-replace-format-args
1633 (regexp-quote YaTeX-struct-begin)
1634 (concat "\\(" YaTeX-struct-name-regexp "\\)") "" "" "")
1635 "\\|"
1636 (YaTeX-replace-format-args
1637 (regexp-quote YaTeX-struct-end)
1638 (concat "\\(" YaTeX-struct-name-regexp "\\)") "" "" ""))
1639 (point-end-of-line) t))
1642 (defun YaTeX-on-includes-p ()
1643 (save-excursion
1644 (beginning-of-line)
1645 (re-search-forward "\\(\\(include.*\\)\\|\\(input\\)\\){.*}"
1646 (point-end-of-line) t))
1649 (defun YaTeX-on-comment-p (&optional sw)
1650 "Return t if current line is commented out.
1651 Optional argument SW t to treat all `%' lines as comment,
1652 even if on `%#' notation."
1653 (save-excursion
1654 (beginning-of-line)
1655 (skip-chars-forward "\\s ")
1656 (looking-at (if sw "%" "%[^#]")))
1659 (defun YaTeX-on-BEGIN-END-p ()
1660 (save-excursion
1661 (let ((case-fold-search nil))
1662 (beginning-of-line)
1663 (re-search-forward "\\(%#BEGIN\\)\\|\\(%#END\\)" (point-end-of-line) t)))
1666 (defun YaTeX-goto-corresponding-* (arg)
1667 "Parse current line and call suitable function."
1668 (interactive "P")
1669 (cond
1670 ((YaTeX-goto-corresponding-label arg))
1671 ((YaTeX-goto-corresponding-environment))
1672 ((YaTeX-goto-corresponding-file-processor arg))
1673 ((YaTeX-goto-corresponding-file arg))
1674 ((YaTeX-goto-corresponding-BEGIN-END))
1675 ((and (string-match
1676 YaTeX-equation-env-regexp ;to delay loading
1677 (or (YaTeX-inner-environment t) "document"))
1678 (YaTeX-goto-corresponding-leftright)))
1679 (t (message "I don't know where to go.")))
1682 (defun YaTeX-goto-corresponding-*-other-window (arg)
1683 "Parse current line and call suitable function."
1684 (interactive "P")
1685 (cond
1686 ((YaTeX-goto-corresponding-label arg t))
1687 ;;((YaTeX-goto-corresponding-environment))
1688 ((YaTeX-goto-corresponding-file t))
1689 ;;((YaTeX-goto-corresponding-BEGIN-END))
1690 (t (message "I don't know where to go.")))
1693 (defun YaTeX-comment-region (alt-prefix)
1694 "Comment out region by '%'.
1695 If you call this function on the 'begin{}' or 'end{}' line,
1696 it comments out whole environment"
1697 (interactive "P")
1698 (if (not (YaTeX-on-begin-end-p))
1699 (comment-out-region
1700 (if alt-prefix
1701 (read-string "Insert prefix: ")
1702 YaTeX-comment-prefix))
1703 (YaTeX-comment-uncomment-env 'comment-out-region))
1706 (defun YaTeX-uncomment-region (alt-prefix)
1707 "Uncomment out region by '%'."
1708 (interactive "P")
1709 (if (not (YaTeX-on-begin-end-p))
1710 (uncomment-region
1711 (if alt-prefix (read-string "Remove prefix: ")
1712 YaTeX-comment-prefix)
1713 (region-beginning) (region-end) YaTeX-uncomment-once)
1714 (YaTeX-comment-uncomment-env 'uncomment-region))
1717 (defun YaTeX-comment-uncomment-env (func)
1718 "Comment or uncomment out one LaTeX environment switching function by FUNC."
1719 (let (beg (p (point)))
1720 (save-excursion
1721 (beginning-of-line)
1722 (setq beg (point))
1723 (YaTeX-goto-corresponding-environment)
1724 (beginning-of-line)
1725 (if (> p (point)) (setq beg (1+ beg)) (forward-char 1))
1726 (funcall func YaTeX-comment-prefix beg (point) YaTeX-uncomment-once)))
1727 (message "%sommented out current environment."
1728 (if (eq func 'comment-out-region) "C" "Un-c"))
1731 (defun YaTeX-beginning-of-environment (&optional limit-search-bound end)
1732 "Goto the beginning of the current environment.
1733 Optional argument LIMIT-SEARCH-BOUND non-nil limits the search bound to
1734 most recent sectioning command. Non-nil for optional third argument END
1735 goes to end of environment."
1736 (interactive)
1737 (let ((op (point)))
1738 (if (YaTeX-inner-environment limit-search-bound)
1739 (progn
1740 (goto-char (get 'YaTeX-inner-environment 'point))
1741 (and end (YaTeX-goto-corresponding-environment))
1742 (if (interactive-p) (push-mark op))
1743 t)))
1746 (defun YaTeX-end-of-environment (&optional limit-search-bound)
1747 "Goto the end of the current environment.
1748 Optional argument LIMIT-SEARCH-BOUND non-nil limits the search bound
1749 to most recent sectioning command."
1750 (interactive)
1751 (YaTeX-beginning-of-environment limit-search-bound t)
1754 (defun YaTeX-mark-environment ()
1755 "Mark current position and move point to end of environment."
1756 (interactive)
1757 (let ((curp (point)))
1758 (if (and (YaTeX-on-begin-end-p) (match-beginning 1)) ;if on \\begin
1759 (forward-line 1)
1760 (beginning-of-line))
1761 (if (not (YaTeX-end-of-environment)) ;arg1 turns to match-beginning 1
1762 (progn
1763 (goto-char curp)
1764 (error "Cannot found the end of current environment."))
1765 (YaTeX-goto-corresponding-environment)
1766 (beginning-of-line) ;for confirmation
1767 (if (< curp (point))
1768 (progn
1769 (message "Mark this environment?(y or n): ")
1770 (if (= (read-char) ?y) nil
1771 (goto-char curp)
1772 (error "Abort. Please call again at more proper position."))))
1773 (set-mark-command nil)
1774 (YaTeX-goto-corresponding-environment)
1775 (end-of-line)
1776 (if (eobp) nil (forward-char 1))))
1779 (defun YaTeX-comment-paragraph ()
1780 "Comment out current paragraph."
1781 (interactive)
1782 (save-excursion
1783 (cond
1784 ((YaTeX-on-begin-end-p)
1785 (beginning-of-line)
1786 (insert YaTeX-comment-prefix)
1787 (YaTeX-goto-corresponding-environment)
1788 (beginning-of-line)
1789 (insert YaTeX-comment-prefix))
1790 ((YaTeX-on-comment-p)
1791 (message "Already commented out."))
1792 (t
1793 (mark-paragraph)
1794 (if (looking-at paragraph-separate) (forward-line 1))
1795 (comment-out-region "%"))))
1798 (defun YaTeX-uncomment-paragraph ()
1799 "Uncomment current paragraph."
1800 (interactive)
1801 (save-excursion
1802 (if (YaTeX-on-begin-end-p)
1803 (let ((p (point-marker)))
1804 (YaTeX-goto-corresponding-environment)
1805 (YaTeX-remove-prefix YaTeX-comment-prefix YaTeX-uncomment-once)
1806 (goto-char p)
1807 (YaTeX-remove-prefix YaTeX-comment-prefix YaTeX-uncomment-once)
1808 (set-marker p nil))
1809 (if (YaTeX-on-comment-p)
1810 (let*((fill-prefix "")
1811 ;;append `^%' to head of paragraph delimiter.
1812 (paragraph-start
1813 (concat
1814 "^$\\|^%\\(" YaTeX-paragraph-separate "\\)"))
1815 (paragraph-separate paragraph-start))
1816 (mark-paragraph)
1817 (if (not (bobp)) (forward-line 1))
1818 (uncomment-region "%" nil nil YaTeX-uncomment-once))
1819 (message "This line is not a comment line."))))
1822 (defun YaTeX-remove-prefix (prefix &optional once)
1823 "Remove prefix on current line as far as prefix detected. But
1824 optional argument ONCE makes deletion once."
1825 (interactive "sPrefix:")
1826 (beginning-of-line)
1827 (while (re-search-forward (concat "^" prefix) (point-end-of-line) t)
1828 (replace-match "")
1829 (if once (end-of-line)))
1832 (defun YaTeX-kill-some-pairs (predicate gofunc kill-contents)
1833 "Kill some matching pair.
1834 This function assumes that pairs occupy whole of each line where they resid."
1835 (if (not (funcall predicate)) nil
1836 (let ((b1 (match-beginning 0)) (e1 (match-end 0))
1837 b2 e2)
1838 (save-excursion
1839 (funcall gofunc)
1840 (funcall predicate) ;get match data
1841 (if (< (point) e1) ;if currently on begin-line
1842 (progn
1843 (setq b2 b1 e2 e1
1844 b1 (match-beginning 0) e1 (match-end 0))
1845 (goto-char e2)) ;goto end-line's end
1846 (setq b2 (match-beginning 0)
1847 e2 (match-end 0))
1848 (goto-char e2)) ;now e2 has surely end-line's end
1849 (skip-chars-forward " \t")
1850 (and (eolp)
1851 (not (eobp))
1852 (setq e2 (1+ (point))))
1853 (if (not kill-contents)
1854 (kill-region
1855 (progn
1856 (goto-char b2)
1857 (skip-chars-backward " \t%")
1858 (if (bolp) (point) b2))
1859 e2))
1860 (goto-char b1)
1861 (skip-chars-backward " \t%")
1862 (if (not kill-contents)
1863 (progn
1864 (kill-append
1865 (buffer-substring
1866 (setq b1 (if (bolp) (point) b1))
1867 (setq e1
1868 (progn
1869 (goto-char e1)
1870 (while (looking-at "{\\| \t")
1871 (forward-list 1))
1872 (skip-chars-forward " \t")
1873 (if (and (eolp) (not (eobp)))
1874 (1+ (point))
1875 (point)))))
1876 t)
1877 (delete-region b1 e1))
1878 (kill-region
1879 (if (bolp) (point) b1)
1880 e2)))
1881 t))
1884 (defun YaTeX-kill-section-command (point kill-all)
1885 "Kill section-type command at POINT leaving its argument.
1886 Non-nil for the second argument kill its argument too."
1887 (let (beg (end (make-marker)))
1888 (save-excursion
1889 (goto-char point)
1890 (or (looking-at YaTeX-ec-regexp)
1891 (progn
1892 (skip-chars-backward (concat "^" YaTeX-ec-regexp))
1893 (forward-char -1)))
1894 (setq beg (point))
1895 (skip-chars-forward "^{")
1896 (forward-list 1)
1897 (set-marker end (point))
1898 (if kill-all (kill-region beg end)
1899 (goto-char beg)
1900 (kill-region
1901 (point) (progn (skip-chars-forward "^{" end) (1+ (point))))
1902 (goto-char end)
1903 (set-marker end nil)
1904 (kill-append (buffer-substring (point) (1- (point))) nil)
1905 (delete-backward-char 1))))
1908 (defun YaTeX-kill-paren (kill-contents)
1909 "Kill parentheses leaving its contents.
1910 But kill its contents if the argument KILL-CONTENTS is non-nil."
1911 (save-excursion
1912 (let (p)
1913 (if (looking-at "\\s(\\|\\(\\s)\\)")
1914 (progn
1915 (if (match-beginning 1)
1916 (up-list -1))
1917 (setq p (point))
1918 (forward-list 1)
1919 (if kill-contents (delete-region p (point))
1920 (backward-delete-char 1)
1921 (goto-char p)
1922 (if (looking-at
1923 (concat "{" YaTeX-ec-regexp
1924 YaTeX-command-token-regexp "+"
1925 "\\s +"))
1926 (delete-region
1927 (point)
1928 (progn (re-search-forward "\\s +" nil t) (point)))
1929 (delete-char 1)))
1930 t))))
1933 (defvar YaTeX-read-environment-history nil "Holds history of environments.")
1934 (put 'YaTeX-read-environment-history 'no-default t)
1935 (defun YaTeX-read-environment (prompt &optional predicate must-match initial)
1936 "Read a LaTeX environment name with completion."
1937 (YaTeX-sync-local-table 'tmp-env-table)
1938 (completing-read-with-history
1939 prompt
1940 (append tmp-env-table user-env-table env-table)
1941 predicate must-match initial
1942 'YaTeX-read-environment-history)
1945 (defvar YaTeX-read-section-history nil "Holds history of section-types.")
1946 (put 'YaTeX-read-section-history 'no-default t)
1947 (defun YaTeX-read-section (prompt &optional predicate initial)
1948 "Read a LaTeX section-type command with completion."
1949 (YaTeX-sync-local-table 'tmp-section-table)
1950 (let ((minibuffer-completion-table
1951 (append tmp-section-table user-section-table section-table)))
1952 (read-from-minibuffer-with-history
1953 prompt initial YaTeX-section-completion-map nil
1954 'YaTeX-read-section-history))
1957 (defun YaTeX-read-section-with-overview ()
1958 "Read sectioning command with overview.
1959 This function refers a local variable `source-window' in YaTeX-make-section"
1960 (interactive)
1961 (require 'yatexsec) ;some case needs this
1962 (if (> (minibuffer-depth) 1)
1963 (error "Too many minibuffer levels for overview."))
1964 (let ((sw (selected-window))
1965 (minibuffer-max-depth nil) ; for XEmacs20
1966 (enable-recursive-minibuffers t) sect)
1967 (unwind-protect
1968 (progn
1969 (select-window source-window)
1970 (setq sect (YaTeX-read-section-in-minibuffer
1971 "Sectioning(Up=C-p, Down=C-n, Help=?): "
1972 YaTeX-sectioning-level (YaTeX-section-overview))))
1973 (select-window sw))
1974 (if (eq (selected-window) (minibuffer-window))
1975 (erase-buffer))
1976 (insert sect)
1977 (exit-minibuffer)
1981 (defvar YaTeX-read-fontsize-history nil "Holds history of font designator.")
1982 (put 'YaTeX-read-fontsize-history 'no-default t)
1983 (defun YaTeX-read-fontsize (prompt &optional predicate must-match initial)
1984 "Read a LaTeX font changing command with completion."
1985 (YaTeX-sync-local-table 'tmp-fontsize-table)
1986 (completing-read-with-history
1987 prompt (append tmp-fontsize-table user-fontsize-table fontsize-table)
1988 predicate must-match initial 'YaTeX-read-fontsize-history)
1991 (defun YaTeX-change-environment ()
1992 "Change the name of environment."
1993 (interactive)
1994 (if (not (YaTeX-on-begin-end-p)) nil
1995 (save-excursion
1996 (let (p env (m1 (match-beginning 1)) (m2 (match-beginning 2)))
1997 (setq env (if m1 (buffer-substring m1 (match-end 1))
1998 (buffer-substring m2 (match-end 2))))
1999 (goto-char (match-beginning 0))
2000 (set-mark-command nil)
2001 (YaTeX-goto-corresponding-environment)
2002 (setq newenv (YaTeX-read-environment
2003 (format "Change environment `%s' to: " env)))
2004 (cond
2005 ((string= newenv "") (message "Change environment cancelled."))
2006 ((string= newenv env) (message "No need to change."))
2007 (t
2008 (search-forward (concat "{" env) (point-end-of-line) t)
2009 (replace-match (concat "{" newenv) t)
2010 (exchange-point-and-mark)
2011 (search-forward (concat "{" env) (point-end-of-line) t)
2012 (replace-match (concat "{" newenv) t)))
2013 t)))
2016 (defun YaTeX-change-section ()
2017 "Change section-type command."
2018 (interactive)
2019 (let*((where (YaTeX-on-section-command-p YaTeX-command-token-regexp))
2020 (p (point)) (cmd (YaTeX-match-string 1)) beg end old new)
2021 (if (null where) nil
2022 (cond
2024 ((equal where 0);;if point is on section command
2025 (setq beg (match-beginning 1)
2026 end (match-end 1))
2027 (goto-char beg) ;beginning of the command
2028 (setq new (YaTeX-read-section (format "Change `%s' to: " cmd) nil)))
2030 ((= where -1);;if point is on a optional parameter
2031 (setq beg (match-beginning 2))
2032 (skip-chars-forward "^{")
2033 (setq end (point))
2034 (goto-char p)
2035 (setq new
2036 (if (fboundp (intern-soft (concat YaTeX-addin-prefix cmd)))
2037 (YaTeX-addin cmd)
2038 (concat "["
2039 (read-string (format "Change `%s' to: "
2040 (buffer-substring
2041 (1+ beg) (1- end))))
2042 "]"))))
2044 ((> where 0);;if point is in arguments' braces
2045 (or (looking-at "{")
2046 (progn (skip-chars-backward "^{") (forward-char -1)))
2047 (setq beg (1+ (point)))
2048 (forward-list 1)
2049 (forward-char -1)
2050 (setq end (point)
2051 old (buffer-substring beg end))
2052 (goto-char p)
2053 (if (> (length old) 40)
2054 (setq old (concat (substring old 0 12) "..." (substring old -12))))
2055 (setq new
2056 (if (intern-soft (concat "YaTeX::" cmd))
2057 (funcall (intern-soft (concat "YaTeX::" cmd)) where)
2058 (read-string (format "Change `%s' to: " old)))))
2059 );cond
2060 (delete-region beg end)
2061 (goto-char beg)
2062 (insert-before-markers new)
2063 ;;(goto-char (marker-position p))
2064 new))
2067 (defun YaTeX-change-fontsize ()
2068 "Change large-type command."
2069 (let ((lt (append tmp-fontsize-table user-fontsize-table fontsize-table))
2070 (p (point)) large old new beg end)
2071 ;;(and (looking-at "}") (up-list -1))
2072 ;;(and (looking-at "{") (forward-char 1))
2073 ;;Is above convenient?
2074 (save-excursion
2075 (or (looking-at YaTeX-ec-regexp)
2076 (progn
2077 (skip-chars-backward (concat "^" YaTeX-ec-regexp))
2078 (forward-char -1)))
2079 (cond
2080 ((and
2081 (looking-at
2082 (concat YaTeX-ec-regexp "\\(" YaTeX-TeX-token-regexp "\\)"))
2083 (< p (match-end 0))
2084 (assoc (setq old (YaTeX-match-string 1)) lt))
2085 (goto-char p)
2086 (setq beg (match-beginning 1) end (match-end 1) ;save match position
2087 new (completing-read
2088 (format "Change font/size `%s' to : " old) lt))
2089 (delete-region beg end)
2090 (goto-char beg)
2091 (insert-before-markers new)
2092 new)
2093 (t nil)
2094 )))
2097 (defun YaTeX-change-math-image ()
2098 "Change with image completion."
2099 (let (maketitle memberp beg end)
2100 (if (and (YaTeX-on-maketitle-p)
2101 (progn
2102 (setq maketitle (substring (YaTeX-match-string 0) 1))
2103 (setq memberp (YaTeX-math-member-p maketitle))))
2104 (let ((last-command-char (string-to-char (car memberp))))
2105 (setq beg (match-beginning 0) end (match-end 0))
2106 (delete-region beg end)
2107 (YaTeX-math-insert-sequence t (cdr memberp))))))
2109 (defun YaTeX-kill-* (&optional arg)
2110 "Parse current line and call suitable function.
2111 Non-nil for ARG kills its contents too."
2112 (interactive "P")
2113 (cond
2114 ((YaTeX-kill-some-pairs 'YaTeX-on-begin-end-p
2115 'YaTeX-goto-corresponding-environment arg))
2116 ((YaTeX-kill-some-pairs 'YaTeX-on-BEGIN-END-p
2117 'YaTeX-goto-corresponding-BEGIN-END arg))
2118 ((YaTeX-on-section-command-p YaTeX-command-token-regexp);on any command
2119 (YaTeX-kill-section-command (match-beginning 0) arg))
2120 ((YaTeX-kill-paren arg))
2121 (t (message "I don't know what to kill.")))
2124 (defun YaTeX-change-* ()
2125 "Parse current line and call suitable function."
2126 (interactive)
2127 (cond
2128 ((YaTeX-change-environment))
2129 ((YaTeX-change-section))
2130 ((YaTeX-change-fontsize))
2131 ((YaTeX-change-math-image))
2132 (t (message "I don't know what to change.")))
2135 ;;;
2136 ;Check availability of add-in functions
2137 ;;;
2138 (cond
2139 ((featurep 'yatexadd) nil) ;Already provided.
2140 ((progn (load "yatexadd" t) (featurep 'yatexadd)) nil)
2141 (t (message "YaTeX add-in functions not supplied.")))
2143 (defun YaTeX-addin (name)
2144 "Check availability of addin function and call it if exists."
2145 (if (and (not (get 'YaTeX-generate 'disabled))
2146 (intern-soft (concat YaTeX-addin-prefix name))
2147 (fboundp (intern-soft (concat YaTeX-addin-prefix name))))
2148 (let ((s (funcall (intern (concat YaTeX-addin-prefix name)))))
2149 (if (stringp s) s ""))
2150 "") ;Add in function is not bound.
2153 (defun YaTeX-on-item-p (&optional point)
2154 "Return t if POINT (default is (point)) is on \\item."
2155 (let ((p (or point (point))))
2156 (save-excursion
2157 (goto-char p)
2158 (end-of-line)
2159 (setq p (point))
2160 (re-search-backward YaTeX-paragraph-delimiter nil t)
2161 (re-search-forward YaTeX-item-regexp p t)))
2164 (defun YaTeX-in-verb-p (&optional point)
2165 "Check if POINT is in verb or verb*. Default of POINT is (point)."
2166 (setq point (or point (point)))
2167 (save-excursion
2168 (goto-char point)
2169 (if (not (re-search-backward
2170 (concat YaTeX-ec-regexp
2171 "\\(" YaTeX-verb-regexp "\\)"
2172 "\\([^-A-Za-z_*]\\)")
2173 (point-beginning-of-line) t))
2174 nil
2175 (goto-char (match-end 2))
2176 (skip-chars-forward
2177 (concat "^" (buffer-substring (match-beginning 2) (match-end 2))))
2178 (and (< (match-beginning 2) point) (< (1- point) (point)))))
2181 (defun YaTeX-literal-p (&optional point)
2182 "Check if POINT is in verb or verb* or verbatime environment family.
2183 Default of POINT is (point)."
2184 (cond
2185 ((equal YaTeX-ec "\\") ;maybe LaTeX
2186 (save-excursion
2187 (and point (goto-char point))
2188 (or (YaTeX-in-verb-p (point))
2189 (and (not (looking-at "\\\\end{verb"))
2190 (YaTeX-quick-in-environment-p YaTeX-verbatim-environments))))))
2193 (defun YaTeX-in-environment-p (env)
2194 "Return if current LaTeX environment is ENV.
2195 ENV is given in the form of environment's name or its list."
2196 (let ((md (match-data)) (nest 0) p envrx)
2197 (cond
2198 ((atom env)
2199 (setq envrx
2200 (concat "\\("
2201 (regexp-quote
2202 (YaTeX-replace-format-args
2203 YaTeX-struct-begin env "" ""))
2204 "\\)\\|\\("
2205 (regexp-quote
2206 (YaTeX-replace-format-args
2207 YaTeX-struct-end env "" ""))
2208 "\\)"))
2209 (save-excursion
2210 (setq p (catch 'open
2211 (while (YaTeX-re-search-active-backward
2212 envrx YaTeX-comment-prefix nil t)
2213 (if (match-beginning 2)
2214 (setq nest (1+ nest))
2215 (setq nest (1- nest)))
2216 (if (< nest 0) (throw 'open t)))))))
2217 ((listp env)
2218 (setq p
2219 (or (YaTeX-in-environment-p (car env))
2220 (and (cdr env) (YaTeX-in-environment-p (cdr env)))))))
2221 (store-match-data md)
2222 p;(or p (YaTeX-in-verb-p (match-beginning 0)))
2226 (defun YaTeX-quick-in-environment-p (env)
2227 "Check quickly but unsure if current environment is ENV.
2228 ENV is given in the form of environment's name or its list.
2229 This function returns correct result only if ENV is NOT nested."
2230 (save-excursion
2231 (let ((md (match-data)) (p (point)) rc clfound)
2232 (cond
2233 ((listp env)
2234 (or (YaTeX-quick-in-environment-p (car env))
2235 (and (cdr env) (YaTeX-quick-in-environment-p (cdr env)))))
2236 (t
2237 (if (YaTeX-search-active-backward
2238 (YaTeX-replace-format-args YaTeX-struct-begin env "" "")
2239 YaTeX-comment-prefix nil t)
2240 (setq rc (not (YaTeX-search-active-forward
2241 (YaTeX-replace-format-args
2242 YaTeX-struct-end env)
2243 YaTeX-comment-prefix p t nil))))
2244 (store-match-data md)
2245 rc))))
2248 ;; Filling \item
2249 (defun YaTeX-remove-trailing-comment (start end)
2250 "Remove trailing comment from START to end."
2251 (save-excursion
2252 (let ((trcom (concat YaTeX-comment-prefix "$")))
2253 (goto-char start)
2254 (while (re-search-forward trcom end t)
2255 (if (/= (char-after (1- (match-beginning 0))) ?\\ )
2256 (replace-match "\\1")))))
2259 (defun YaTeX-get-item-info (&optional recent thisenv)
2260 "Return the list of the beginning of \\item and column of its item.
2261 If it seems to be outside of itemizing environment, just return nil.
2262 Non-nil for optional argument RECENT refers recent \\item.
2263 Optional second argument THISENV omits calling YaTeX-inner-environment."
2264 (save-excursion
2265 (let* ((p (point)) env e0 c
2266 (bndry (and (setq env (or thisenv (YaTeX-inner-environment t)))
2267 (get 'YaTeX-inner-environment 'point)
2268 )))
2269 (end-of-line)
2270 (if (if recent
2271 (YaTeX-re-search-active-backward
2272 YaTeX-item-regexp YaTeX-comment-prefix bndry t)
2273 (goto-char bndry)
2274 (YaTeX-re-search-active-forward
2275 YaTeX-item-regexp YaTeX-comment-prefix p t))
2276 (progn
2277 (goto-char (match-end 0))
2278 ;(setq c (current-column))
2279 (if (string-match "desc" env)
2280 (setq c 6)
2281 (if (equal (following-char) ?\[) (forward-list 1))
2282 (setq c 0))
2283 (skip-chars-forward " \t" (point-end-of-line))
2284 (list (point-beginning-of-line) (+ c (current-column)))))))
2287 (defun YaTeX-fill-item ()
2288 "Fill item in itemize environment."
2289 (interactive)
2290 (save-excursion
2291 (let* ((p (point))
2292 (item-term (concat
2293 "\\(^[ \t]*$\\)\\|" YaTeX-item-regexp "\\|\\("
2294 YaTeX-ec-regexp "\\(begin\\|end\\)\\)"))
2295 ;;This value depends on LaTeX.
2296 fill-prefix start col
2297 (info (YaTeX-get-item-info t)))
2298 (if (null info) nil ;not on \item, do nothing
2299 (setq start (car info)
2300 col (car (cdr info)))
2301 (save-excursion
2302 (if (re-search-backward "^\\s *$" start t)
2303 ;;if separated from \item line, isolate this block
2304 (progn
2305 (setq start (1+ (match-end 0)))
2306 (goto-char start)
2307 (skip-chars-forward " \t")
2308 (delete-region (point) start) ;is this your favor???
2309 (indent-to col))))
2310 (beginning-of-line)
2311 (if (<= (save-excursion
2312 (re-search-forward
2313 (concat "\\\\end{\\|\\\\begin{\\|^[ \t]*$") nil t)
2314 (match-beginning 0))
2315 p)
2316 (progn (message "Not on itemize.") nil)
2317 (end-of-line)
2318 (newline)
2319 (indent-to col)
2320 (setq fill-prefix
2321 (buffer-substring (point-beginning-of-line)(point)))
2322 (beginning-of-line)
2323 (delete-region (point) (progn (forward-line 1) (point)))
2324 (re-search-forward item-term nil 1)
2325 (YaTeX-remove-trailing-comment start (point))
2326 (beginning-of-line)
2327 (push-mark (point) t)
2328 (goto-char start)
2329 (forward-line 1)
2330 (while (< (point) (mark))
2331 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
2332 (forward-line 1))
2333 (fill-region-as-paragraph start (mark))
2334 (if NTT-jTeX
2335 (while (progn(forward-line -1)(end-of-line) (> (point) start))
2336 (insert ?%)))
2337 (pop-mark)))))
2340 (defun YaTeX-fill-paragraph (arg)
2341 "YaTeX adjustment function for fill-paragraph.
2342 *Protect \\verb from unexpected broken up."
2343 (interactive "P")
2344 (cond
2345 ((not (eq major-mode 'yatex-mode)) (fill-paragraph arg))
2346 ((YaTeX-quick-in-environment-p YaTeX-fill-inhibit-environments) nil)
2347 ((YaTeX-in-math-mode-p) nil)
2348 (t
2349 (save-excursion
2350 (let ((verbrex (concat YaTeX-ec-regexp
2351 "\\(" YaTeX-verb-regexp "\\)" ;match#1
2352 "\\(.\\).*\\(\\2\\)")) ;match #2 and #3
2353 (p (point)) ii end poslist spacelist (fill-prefix fill-prefix))
2354 (cond
2355 ((save-excursion (beginning-of-line) ;if point is on the first
2356 (setq end (point)) ;non-whitespace char
2357 (skip-chars-forward " \t")
2358 (equal (point) p))
2359 (setq fill-prefix (buffer-substring p end)))
2360 ((and ;;(not YaTeX-emacs-19)
2361 (string-match YaTeX-itemizing-env-regexp
2362 (or (YaTeX-inner-environment t) "document"))
2363 (setq ii (YaTeX-get-item-info)))
2364 (save-excursion
2365 (beginning-of-line)
2366 (indent-to-column (car (cdr ii)))
2367 (setq fill-prefix
2368 (buffer-substring (point) (point-beginning-of-line)))
2369 (delete-region (point) (progn (beginning-of-line) (point))))))
2370 (mark-paragraph)
2371 (save-restriction
2372 (narrow-to-region (region-beginning) (region-end))
2373 (YaTeX-remove-trailing-comment (point-min) (point-max))
2374 (goto-char (point-min))
2375 (while (YaTeX-re-search-active-forward
2376 verbrex YaTeX-comment-prefix (point-max) t)
2377 (setq end (match-beginning 3))
2378 (goto-char (match-beginning 2))
2379 (while (re-search-forward "\\s " end t)
2380 (setq poslist (cons (make-marker) poslist)
2381 spacelist (cons (preceding-char) spacelist))
2382 (replace-match "_")
2383 (set-marker (car poslist) (match-beginning 0))))
2384 ;;(fill-paragraph arg)
2385 (fill-region-as-paragraph (point-min) (point-max) arg)
2386 (while spacelist
2387 (goto-char (car poslist))
2388 (set-marker (car poslist) nil)
2389 (delete-char 1)
2390 (insert (car spacelist))
2391 (setq spacelist (cdr spacelist) poslist (cdr poslist)))
2392 (goto-char (point-min))
2393 (forward-word 1)
2394 (beginning-of-line)
2395 (while (re-search-forward "\\\\\\(\\(page\\)?ref\\|cite\\){" nil t)
2396 (if (< (point-end-of-line)
2397 (save-excursion (forward-char -1) (forward-list 1) (point)))
2398 (progn (end-of-line) (insert YaTeX-comment-prefix))))
2399 (goto-char (point-min))
2400 (if (and NTT-jTeX (looking-at "[ \t]"))
2401 (progn
2402 (goto-char (point-min))
2403 (while (not (eobp))
2404 (end-of-line)
2405 (or (bolp)
2406 (save-excursion
2407 (backward-word 1)
2408 (looking-at "\\sw+")) ;is not japanese string
2409 (progn (setq p (point)) (insert YaTeX-comment-prefix)))
2410 (forward-line 1))
2411 (goto-char p)
2412 (if (looking-at "%") (delete-char 1)) ;remove last inserted `%'
2413 )))))))
2416 (if (fboundp 'YaTeX-saved-indent-new-comment-line) nil
2417 (fset 'YaTeX-saved-indent-new-comment-line
2418 (symbol-function 'indent-new-comment-line))
2419 (fset 'indent-new-comment-line 'YaTeX-indent-new-comment-line))
2421 (defun YaTeX-indent-new-comment-line (&optional soft)
2422 "Tuned `indent-new-comment-line' function for yatex.
2423 See the documentation of `YaTeX-saved-indent-new-comment-line'."
2424 (cond
2425 ((or (not (memq major-mode '(yatex-mode yahtml-mode)))
2426 (string-match
2427 "document"
2428 (or (and (boundp 'inenv) inenv)
2429 (or (YaTeX-inner-environment t) "document"))))
2430 (apply 'YaTeX-saved-indent-new-comment-line (if soft (list soft))))
2431 ; ((and (eq major-mode 'yahtml-mode)
2432 ; (string-match
2433 ; "^[Pp][Rr][Ee]" (yahtml-inner-environment-but "^[Aa]\\b" t)))
2434 ; (yahtml-indent-new-commnet-line))
2435 ((and (eq major-mode 'yatex-mode) ;1997/2/4
2436 (YaTeX-in-math-mode-p)) nil) ;1996/12/30
2437 (t (let (fill-prefix)
2438 (apply 'YaTeX-saved-indent-new-comment-line (if soft (list soft))))))
2441 (defun YaTeX-fill-* ()
2442 "Fill paragraph according to its condition."
2443 (interactive)
2444 (cond
2445 ((YaTeX-fill-item))
2449 ;; Accent completion
2450 (defun YaTeX-read-accent-char (x)
2451 "Read char in accent braces."
2452 (let ((c (read-char)))
2453 (concat
2454 (if (and (or (= c ?i) (= c ?j))
2455 (not (string-match (regexp-quote x) "cdb")))
2456 "\\" "")
2457 (char-to-string c)))
2460 (defun YaTeX-make-accent ()
2461 "Make accent usage."
2462 (interactive)
2463 (message "1:` 2:' 3:^ 4:\" 5:~ 6:= 7:. u v H t c d b")
2464 (let ((c (read-char))(case-fold-search nil))
2465 (setq c (cond ((and (> c ?0) (< c ?8))
2466 (substring "`'^\"~=." (1- (- c ?0)) (- c ?0)))
2467 ((= c ?h) "H")
2468 (t (char-to-string c))))
2469 (if (not (string-match c "`'^\"~=.uvHtcdb")) nil
2470 (insert "\\" c "{}")
2471 (backward-char 1)
2472 (insert (YaTeX-read-accent-char c))
2473 (if (string= c "t") (insert (YaTeX-read-accent-char c)))
2474 (forward-char 1)))
2477 ;; Indentation
2478 (defun YaTeX-current-indentation ()
2479 "Return the indentation of current environment."
2480 (save-excursion
2481 ;;(beginning-of-line)
2482 (if (YaTeX-beginning-of-environment t)
2483 (goto-char (get 'YaTeX-inner-environment 'point))
2484 (forward-line -1)
2485 (beginning-of-line)
2486 (skip-chars-forward " \t"))
2487 (current-column))
2490 (defun YaTeX-previous-line-indentation ()
2491 (save-excursion
2492 (forward-line -1)
2493 (skip-chars-forward " \t")
2494 (current-column)))
2496 (defun YaTeX-indent-line ()
2497 "Indent corrent line referrin current environment."
2498 (interactive)
2499 (let ((indent-relative
2500 (function
2501 (lambda (&optional additional)
2502 (YaTeX-reindent
2503 (+ (YaTeX-current-indentation)
2504 (or additional 0)
2505 YaTeX-environment-indent)))))
2506 sect depth iteminfo (p (point)) pp (peol (point-end-of-line))
2507 ;;inenv below is sometimes defined in YaTeX-indent-new-comment-line
2508 (inenv (or (and (boundp 'inenv) inenv) (YaTeX-inner-environment t))))
2509 ;;(if NTT-jTeX ;;Do you need this section?
2510 ;; (save-excursion
2511 ;; (end-of-line)
2512 ;; (let ((p (point)))
2513 ;; (forward-line -1)
2514 ;; (end-of-line)
2515 ;; (or (= p (point))
2516 ;; (progn (backward-char (length YaTeX-comment-prefix))
2517 ;; (not (looking-at (regexp-quote YaTeX-comment-prefix))))
2518 ;; (progn
2519 ;; (skip-chars-backward YaTeX-comment-prefix)
2520 ;; (kill-line))))))
2521 (or inenv (setq inenv "document")) ;is the default environment
2522 (cond
2523 ((and (YaTeX-on-begin-end-p) (match-beginning 2)) ;if \end
2524 (YaTeX-reindent (YaTeX-current-indentation)))
2525 ((string-match YaTeX-equation-env-regexp inenv)
2526 (YaTeX-indent-line-equation)) ;autoload-ed from yatexenv
2527 (;(YaTeX-in-environment-p '("itemize" "enumerate" "description" "list"))
2528 (string-match YaTeX-itemizing-env-regexp inenv)
2529 ;;(YaTeX-on-item-p) ??
2530 ;;(setq iteminfo (YaTeX-get-item-info t))
2531 (if (save-excursion
2532 (beginning-of-line)
2533 (re-search-forward YaTeX-item-regexp peol t))
2534 (progn
2535 (save-excursion
2536 (goto-char (1+ (match-beginning 0)))
2537 (setq depth
2538 (* YaTeX-environment-indent
2539 (cond
2540 ((looking-at "subsubsub") 3)
2541 ((looking-at "subsub") 2)
2542 ((looking-at "sub") 1)
2543 (t 0)))))
2544 (funcall indent-relative depth))
2545 (YaTeX-reindent (or (car (cdr (YaTeX-get-item-info nil inenv)))
2546 (+ (save-excursion
2547 (beginning-of-line)
2548 (YaTeX-current-indentation))
2549 YaTeX-environment-indent))))
2551 ((YaTeX-literal-p) ;verbatims
2552 (tab-to-tab-stop))
2553 ((and inenv (not (equal "document" inenv)))
2554 (funcall indent-relative))
2555 ((YaTeX-on-section-command-p YaTeX-sectioning-regexp)
2556 (require 'yatexsec) ;to know YaTeX-sectioning-level
2557 (setq sect (YaTeX-match-string 1))
2558 (if (string-match "\\*$" sect)
2559 (setq sect (substring sect 0 -1)))
2560 (YaTeX-reindent
2561 (* (max
2562 (1- ;I want chapter to have indentation 0
2563 (or (cdr (assoc sect YaTeX-sectioning-level))
2564 0))
2565 0)
2566 YaTeX-environment-indent)))
2567 ;;Default movement
2568 ((and (bolp) fill-prefix) (insert fill-prefix))
2569 (t (save-excursion
2570 (beginning-of-line)
2571 (if fill-prefix
2572 (progn
2573 (delete-region (point)
2574 (progn (skip-chars-forward " \t")
2575 (point)))
2576 (insert fill-prefix))
2577 (skip-chars-forward " \t")
2578 (indent-relative-maybe)))
2579 (skip-chars-forward " \t")))
2580 ;;if current line is \begin, re-indent \end too
2581 (if (and (YaTeX-on-begin-end-p) (match-beginning 1))
2582 (save-excursion
2583 ;;(beginning-of-line)
2584 ;;(search-forward "\\begin")
2585 (goto-char (match-beginning 0))
2586 (setq depth (current-column))
2587 (YaTeX-goto-corresponding-environment)
2588 (YaTeX-reindent depth)))
2589 (if (or
2590 (and NTT-jTeX
2591 (save-excursion (beginning-of-line) (looking-at "[ \t]")))
2592 (save-excursion
2593 (beginning-of-line)
2594 (backward-char 1)
2595 (and
2596 (re-search-backward
2597 "\\\\\\(\\(page\\)?ref\\|cite\\){" (point-beginning-of-line) t)
2598 (goto-char (1- (match-end 0)))
2599 (> (save-excursion
2600 (condition-case ()
2601 (progn (forward-list 1) (point))
2602 (error (point-max))))
2603 (point-end-of-line)))))
2604 (save-excursion
2605 (end-of-line)
2606 (let ((p (point)))
2607 (forward-line -1)
2608 (end-of-line)
2609 (or (= p (point))
2610 (looking-at (regexp-quote YaTeX-comment-prefix))
2611 (bobp) (bolp)
2612 (save-excursion
2613 (backward-word 1)
2614 (looking-at "\\sw+")) ;is not japanese string
2615 (insert YaTeX-comment-prefix))))))
2618 (provide 'yatex)
2619 (defvar yatex-mode-load-hook nil
2620 "*List of functions to be called when yatex.el is loaded.")
2621 (if (and YaTeX-emacs-19 window-system (not (featurep 'yatex19)))
2622 (load "yatex19"))
2623 (load "yatexhks" t)
2625 ;;-------------------- Final hook jobs --------------------
2626 (substitute-all-key-definition
2627 'fill-paragraph 'YaTeX-fill-paragraph YaTeX-mode-map)
2628 (run-hooks 'yatex-mode-load-hook)
2630 ;; `History' was moved to ChangeLog
2631 ;----------------------------- End of yatex.el -----------------------------