yatex

view yatex.el @ 130:8703f090c628

`[prefix] t e' for YaTeX-typeset-environment. It automatically mark current inner environment or portion of math mode, then call typeset-region on that region saving it in texput.tex.
author yuuji@gentei.org
date Thu, 27 May 2010 16:37:44 +0900
parents b11d6271207e
children b9f0c694f30e
line source
1 ;;; -*- Emacs-Lisp -*-
2 ;;; Yet Another tex-mode for emacs - //野鳥//
3 ;;; yatex.el rev. 1.74.3
4 ;;; (c)1991-2010 by HIROSE Yuuji.[yuuji@yatex.org]
5 ;;; Last modified Thu May 27 14:55:07 2010 on firestorm
6 ;;; $Id$
7 ;;; The latest version of this software is always available at;
8 ;;; http://www.yatex.org/
10 (require 'comment)
11 (require 'yatexlib)
12 (defconst YaTeX-revision-number "1.74.3"
13 "Revision number of running yatex.el")
15 ;---------- Local variables ----------
16 (defvar YaTeX-prefix "\C-c"
17 "*Prefix key to call YaTeX functions.
18 You can select favorite prefix key by setq in your ~/.emacs.")
20 (defvar YaTeX-environment-indent 1
21 "*Indentation depth at column width in LaTeX environments.")
23 (defvar YaTeX-fill-prefix nil
24 "*fill-prefix used for auto-fill-mode.
25 The default value is nil.")
27 (defvar YaTeX-fill-column 72
28 "*fill-column used for auto-fill-mode.")
30 (defvar YaTeX-comment-prefix "%"
31 "TeX comment prefix.")
33 (defvar YaTeX-current-position-register ?3
34 "*Position register to keep where the last completion was done.
35 All of YaTeX completing input store the current position into
36 the register YaTeX-current-position-register. So every time you
37 make a trip to any other part of text other than you are writing, you can
38 return to the editing paragraph by calling register-to-point with argument
39 YaTeX-current-position-register.")
41 ;;(defvar YaTeX-tmp-dic-unit 'main-file
42 ;; "*Default switching unit of temporary dictionary.
43 ;;There are two switching unit:
44 ;;'main-file : switch tmp-dic according to main-file directory.
45 ;;'directory : switch tmp-dic dir by dir."
46 ;;)
47 (defvar YaTeX-use-LaTeX2e t "*Use LaTeX2e or not. Nil meas latex 2.09")
49 (defvar tex-command
50 (cond
51 (YaTeX-use-LaTeX2e "platex")
52 (YaTeX-japan "jlatex")
53 (t "latex"))
54 "*Default command for typesetting LaTeX text.")
56 (defvar bibtex-command (if YaTeX-japan "jbibtex" "bibtex")
57 "*Default command of BibTeX.")
59 (defvar dvi2-command ;previewer command for your site
60 (if YaTeX-dos "dviout -wait=0"
61 "xdvi -geo +0+0 -s 4")
62 "*Default previewer command including its option.
63 This default value is for X window system.")
65 (defvar makeindex-command (if YaTeX-dos "makeind" "makeindex")
66 "*Default makeindex command.")
68 (defvar dviprint-command-format
69 (if YaTeX-dos "dviprt %s %f%t"
70 "dvi2ps %f %t %s | lpr")
71 "*Command line string to print out current file.
72 Format string %s will be replaced by the filename. Do not forget to
73 specify the `from usage' and `to usage' with their option by format string
74 %f and %t.
75 See also documentation of dviprint-from-format and dviprint-to-format.")
77 (defvar dviprint-from-format
78 (if YaTeX-dos "%b-" "-f %b")
79 "*`From' page format of dvi filter. %b will turn to beginning page number.")
81 (defvar dviprint-to-format
82 (if YaTeX-dos "%e" "-t %e")
83 "*`To' page format of dvi filter. %e will turn to end page number.")
85 (defvar YaTeX-dvipdf-command
86 "dvipdfmx"
87 "*Command name to convert dvi file to PDF.")
89 (defvar YaTeX-default-document-style
90 (concat (if YaTeX-japan "j") "article")
91 "*Default LaTeX Documentstyle for YaTeX-typeset-region.")
93 (defvar YaTeX-need-nonstop nil
94 "*T for adding `\\nonstopmode{}' to text before invoking latex command.")
96 (defvar latex-warning-regexp "line.* [0-9]*"
97 "*Regular expression of line number of warning message by latex command.")
99 (defvar latex-error-regexp "l\\.[1-9][0-9]*"
100 "*Regular expression of line number of latex error.
101 Perhaps your latex command stops at this error message with line number of
102 LaTeX source text.")
104 (defvar latex-dos-emergency-message
105 "Emergency stop" ;<- for Micro tex, ASCII-pTeX 1.6
106 "Message pattern of emergency stop of typesetting.
107 Because Demacs (GNU Emacs on DOS) cannot have concurrent process, the
108 latex command which is stopping on a LaTeX error, is terminated by Demacs.
109 Many latex command on DOS display some messages when it is terminated by
110 other process, user or OS. Define to this variable a message string of your
111 latex command on DOS shown at abnormal termination.
112 Remember Demacs's call-process function is not oriented for interactive
113 process.")
115 (defvar NTT-jTeX nil
116 "*T for using NTT-jTeX for latex command.
117 More precisely, setting t to this variables inhibits inter-word break on
118 typeset document by line-break of source text. That is, YaTeX automatically
119 put % after each line at filling.
120 改行+インデントによって、タイプセット後の字間が空いてしまうのを抑制する場合に
121 tにする(古いNTT-jTeXで顕著に現れる)。具体的には、fillするときに各行の終わりに
122 %を付加する。")
125 (defvar YaTeX-item-regexp
126 (concat (regexp-quote "\\") "\\(sub\\|bib\\)*item")
127 "*Regular expression of item command.")
129 (defvar YaTeX-sectioning-regexp
130 "\\(part\\|chapter\\*?\\|\\(sub\\)*\\(section\\|paragraph\\)\\)\\(\\*\\|\\b\\)"
131 "*LaTeX sectioning commands regexp.")
133 (defvar YaTeX-paragraph-start
134 (concat "^[ \t]*%\\|^[ \t]*$\\|\\'\\|^\C-l\\|\\\\\\\\$\\|^[ \t]*\\\\\\("
135 YaTeX-sectioning-regexp ;sectioning commands
136 "\\|[A-z]*item\\|begin{\\|end{" ;special declaration
137 "\\|\\[\\|\\]"
138 "\\|newpage\\b\\|vspace\\b"
139 "\\)")
140 "*Paragraph starting regexp of common LaTeX source. Use this value
141 for YaTeX-uncomment-paragraph.")
143 (defvar YaTeX-paragraph-separate
144 (concat "^[ \t]*%\\|^[ \t]*$\\|^\C-l\\|\\\\\\\\$\\|^[ \t]*\\\\\\("
145 YaTeX-sectioning-regexp ;sectioning commands
146 "\\|begin{\\|end{" ;special declaration
147 "\\|\\[\\|\\]"
148 "\\|newpage\\b\\|vspace\\b"
149 "\\)")
150 "*Paragraph delimiter regexp of common LaTeX source. Use this value
151 for YaTeX-uncomment-paragraph.")
153 (defvar YaTeX-verbatim-environments
154 '("verbatim" "verbatim*")
155 "*Assume these environments of this variable disable LaTeX commands.")
156 (defvar YaTeX-verb-regexp "verb\\*?\\|path"
157 "*Regexp of verb family. Do not contain preceding \\\\ nor \\(\\).")
158 (defvar YaTeX-fill-inhibit-environments
159 (append '("tabular" "tabular*" "array" "picture" "eqnarray" "eqnarray*"
160 "equation" "equation*" "math" "displaymath")
161 YaTeX-verbatim-environments)
162 "*In these environments, YaTeX inhibits fill-paragraph from formatting.
163 Define those environments as a form of list.")
165 (defvar YaTeX-itemizing-env-regexp
166 "itemize\\|enumerate\\|description\\|list\\|thebibliography"
167 "*Regexp of itemizing environments")
168 (defvar YaTeX-equation-env-regexp
169 "array\\*?\\|equation\\*?"
170 "*Regexp of environments for equations")
171 (defvar YaTeX-array-env-regexp
172 (concat
173 "array\\*?\\|eqnarray\\*?\\|tabbing\\|tabular\\*?\\|" ;LaTeX
174 "matrix\\|pmatrix\\|bmatrix\\|vmatrix\\|Vmatrix\\|" ;AMS-LaTeX
175 "align\\*?\\|split\\*?\\|aligned\\*?\\|alignat\\*?\\|" ;AMS-LaTeX
176 "[bpvV]?matrix\\|smallmatrix\\|cases\\|" ;AMS-LaTeX
177 "xalignat\\*?\\|xxalignat\\*?") ;AMS-LaTeX
178 "*Regexp of environments where `&' becomes field delimiter.")
179 (defvar YaTeX-uncomment-once t
180 "*T for removing all continuous commenting character(%).
181 Nil for removing only one commenting character at the beginning-of-line.")
183 (defvar YaTeX-close-paren-always t
184 "*Close parenthesis always when YaTeX-modify-mode is nil.")
186 (defvar YaTeX-greek-by-maketitle-completion nil
187 "*T for greek letters completion by maketitle-type completion.")
189 (defvar YaTeX-auto-math-mode t
190 "*T for changing YaTeX-math mode automatically.")
191 (defvar YaTeX-use-AMS-LaTeX nil
192 "*T for using AMS-LaTeX")
194 (defvar yatex-mode-hook nil
195 "*List of functions to be called at the end of yatex-mode initializations.")
197 (defvar YaTeX-search-file-from-top-directory t
198 "*Non-nil means to search input-files from the directory where main file exists.")
200 (defvar YaTeX-use-font-lock (and (featurep 'font-lock)
201 (fboundp 'x-color-values)
202 (fboundp 'font-lock-fontify-region))
203 "*Use font-lock to fontify buffer or not.")
205 (defvar YaTeX-use-hilit19 (and (featurep 'hilit19) (fboundp 'x-color-values)
206 (fboundp 'hilit-translate)
207 (not YaTeX-use-font-lock))
208 "*Use hilit19 to highlight buffer or not.")
210 (defvar YaTeX-tabular-indentation 4
211 "*Indentation column-depth of continueing line in tabular environment.")
213 ;;-- Math mode values --
215 (defvar YaTeX-math-key-list-default
216 '((";" . YaTeX-math-sign-alist)
217 (":" . YaTeX-greek-key-alist))
218 "Default key sequence to invoke math-mode's image completion.")
220 (defvar YaTeX-math-key-list-private nil
221 "*User defined alist, math-mode-prefix vs completion alist.")
223 (defvar YaTeX-math-key-list
224 (append YaTeX-math-key-list-private YaTeX-math-key-list-default)
225 "Key sequence to invoke math-mode's image completion.")
227 (defvar YaTeX-skip-default-reader nil
228 "Non-nil skips default argument reader of section-type completion.")
230 (defvar YaTeX-simple-messages nil
231 "Non-nil makes minibuffer messages simpler.")
233 (defvar YaTeX-template-file "~/work/template.tex"
234 "*Template TeX source file. This will be inserted to empty file.")
236 (defvar YaTeX-addin-prefix "YaTeX:")
238 (defvar yatex-mode-abbrev-table nil
239 "*Abbrev table in use in yatex-mode buffers.")
240 (define-abbrev-table 'yatex-mode-abbrev-table ())
243 ;------------ Completion table ------------
244 ; Set tex-section-like command possible completion
245 (defvar section-table
246 (append
247 '(("part") ("chapter") ("chapter*") ("section") ("section*")
248 ("subsection") ("subsection*")
249 ("subsubsection") ("paragraph") ("subparagraph")
250 ("author") ("thanks") ("documentstyle") ("pagestyle") ("thispagestyle")
251 ("title") ("underline") ("label") ("makebox")
252 ("footnote") ("footnotetext") ("index")
253 ("hspace*") ("vspace*") ("bibliography") ("bibitem") ("cite")
254 ("input") ("include") ("includeonly") ("mbox") ("hbox") ("caption")
255 ("arabic")
256 ("newcounter")
257 ("newlength") ("setlength" 2) ("addtolength" 2) ("settowidth" 2)
258 ("setcounter" 2) ("addtocounter" 2) ("stepcounter" 2)
259 ("newcommand" 2) ("renewcommand" 2)
260 ("newenvironment" 3) ("newtheorem" 2)
261 ("cline") ("framebox") ("savebox" 2) ("sbox" 2) ("newsavebox") ("usebox")
262 ("date") ("put") ("ref") ("pageref") ("tabref") ("figref") ("raisebox" 2)
263 ("multicolumn" 3) ("shortstack") ("parbox" 2)
264 ;; for mathmode accent
265 ("tilde") ("hat") ("check") ("bar") ("dot") ("ddot") ("vec")
266 ("widetilde") ("widehat") ("overline") ("overrightarrow")
267 ;; section types in mathmode
268 ("frac" 2) ("sqrt") ("mathrm") ("mathbf") ("mathit")
270 )
271 (if YaTeX-use-LaTeX2e
272 '(("documentclass") ("usepackage")
273 ("textbf") ("textgt") ("textit") ("textmc") ("textmd") ("textnormal")
274 ("textrm") ("textsc") ("textsf") ("textsl") ("texttt") ("textup")
275 ("mathbf") ("mathcal") ("mathit") ("mathnormal") ("mathrm")
276 ("mathsf") ("mathtt")
277 ("textcircled")
278 ("scalebox" 1) ;is faking of argument position
279 ("rotatebox" 2) ("resizebox" 3) ("reflectbox")
280 ("colorbox" 2) ("fcolorbox" 3) ("textcolor" 2) ("color") ("pagecolor")
281 ("includegraphics") ("includegraphics*")
282 ("bou") ;defined in plext
283 ("url") ;defined in url
284 ("shadowbox") ("doublebox") ("ovalbox") ("Ovalbox")
285 ("fancyoval") ;defined in fancybox
286 ("keytop") ("mask" 2) ("maskbox" 5) ;defined in ascmac
287 ("bm") ;deined in bm
288 ("verbfile") ("listing") ;defined in misc
289 ("slashbox" 2) ("backslashbox" 2) ;defined in slashbox
290 ))
291 (if YaTeX-use-AMS-LaTeX
292 '(("DeclareMathOperator" 2) ("boldsymbol") ("pmb") ("eqref")
293 ("tag") ("tag*"))))
294 "Default completion table for section-type completion.")
296 (defvar user-section-table nil)
297 (defvar tmp-section-table nil)
298 (defvar YaTeX-ams-math-begin-alist
299 '(("align") ("align*") ("multline") ("multline*") ("gather") ("gather*")
300 ("alignat") ("alignat*") ("xalignat") ("xalignat*")
301 ("xxalignat") ("xxalignat*") ("flalign") ("flalign*") ("equation*")))
302 (defvar YaTeX-ams-math-gathering-alist
303 '(("matrix") ("pmatrix") ("bmatrix") ("Bmatrix") ("vmatrix") ("Vmatrix")
304 ("split") ("split*") ("aligned") ("aligned*") ("alignedat") ("gathered")
305 ("smallmatrix") ("cases") ("subequations")))
306 ;; Prepare list(not alist) for YaTeX::ref in yatexadd.el
307 (defvar YaTeX-math-begin-list
308 (mapcar 'car YaTeX-ams-math-begin-alist))
309 (defvar YaTeX-math-gathering-list ;used in yatexadd.el#yatex::ref
310 (mapcar 'car YaTeX-ams-math-gathering-alist))
313 (defvar YaTeX-ams-env-table
314 (append YaTeX-ams-math-begin-alist YaTeX-ams-math-gathering-alist)
315 "*Standard AMS-LaTeX(2e) environment completion table.")
317 ; Set tex-environment possible completion
318 (defvar env-table
319 (append
320 '(("quote") ("quotation") ("center") ("verse") ("document")
321 ("verbatim") ("itemize") ("enumerate") ("description")
322 ("list") ("tabular") ("tabular*") ("table") ("tabbing") ("titlepage")
323 ("sloppypar") ("picture") ("displaymath")
324 ("eqnarray") ("figure") ("equation") ("abstract") ("array")
325 ("thebibliography") ("theindex") ("flushleft") ("flushright")
326 ("minipage")
327 ("supertabular")
328 )
329 (if YaTeX-use-LaTeX2e
330 '(("comment") ;defined in version
331 ("longtable") ;defined in longtable
332 ("screen") ("boxnote") ("shadebox") ;; ("itembox") ;in ascmac
333 ("alltt") ;defined in alltt
334 ("multicols") ;defined in multicol
335 ("breakbox"))) ;defined in eclbkbox
336 (if YaTeX-use-AMS-LaTeX YaTeX-ams-env-table))
337 "Default completion table for begin-type completion.")
339 (defvar user-env-table nil)
340 (defvar tmp-env-table nil)
342 ; Set {\Large }-like completion
343 (defvar fontsize-table
344 '(("rm") ("em") ("bf") ("boldmath") ("it") ("sl") ("sf") ("sc") ("tt")
345 ("dg") ("dm")
346 ("tiny") ("scriptsize") ("footnotesize") ("small")("normalsize")
347 ("large") ("Large") ("LARGE") ("huge") ("Huge")
348 ("rmfamily") ("sffamily") ("ttfamily")
349 ("mdseries") ("bfseries") ("upshape")
350 ("itshape") ("slshape") ("scshape")
351 )
352 "Default completion table for large-type completion.")
354 (defvar LaTeX2e-fontstyle-alist
355 '(("rm" . "rmfamily")
356 ("sf" . "sffamily")
357 ("tt" . "ttfamily")
358 ("md" . "mdseries")
359 ("bf" . "bfseries")
360 ("up" . "upshape")
361 ("it" . "itshape")
362 ("sl" . "slshape")
363 ("sc" . "scshape")))
365 (defvar user-fontsize-table nil)
366 (defvar tmp-fontsize-table nil)
368 (defvar singlecmd-table
369 (append
370 '(("maketitle") ("makeindex") ("sloppy") ("protect") ("par")
371 ("LaTeX") ("TeX") ("item") ("item[]") ("appendix") ("hline") ("kill")
372 ;;("rightarrow") ("Rightarrow") ("leftarrow") ("Leftarrow")
373 ("pagebreak") ("nopagebreak") ("tableofcontents")
374 ("newpage") ("clearpage") ("cleardoublepage")
375 ("footnotemark") ("verb") ("verb*")
376 ("linebreak") ("pagebreak") ("noindent") ("indent")
377 ("left") ("right") ("dots") ("smallskip") ("medskip") ("bigskip")
378 ("displaystyle")
379 )
380 (if YaTeX-greek-by-maketitle-completion
381 '(("alpha") ("beta") ("gamma") ("delta") ("epsilon")
382 ("varepsilon") ("zeta") ("eta") ("theta")("vartheta")
383 ("iota") ("kappa") ("lambda") ("mu") ("nu") ("xi") ("pi")
384 ("varpi") ("rho") ("varrho") ("sigma") ("varsigma") ("tau")
385 ("upsilon") ("phi") ("varphi") ("chi") ("psi") ("omega")
386 ("Gamma") ("Delta") ("Theta") ("Lambda")("Xi") ("Pi")
387 ("Sigma") ("Upsilon") ("Phi") ("Psi") ("Omega")))
388 (if YaTeX-use-LaTeX2e
389 '(("return") ("Return") ("yen"))) ;defined in ascmac
390 (if YaTeX-use-AMS-LaTeX
391 '(("nonumber")))
392 )
393 "Default completion table for maketitle-type completion.")
395 (defvar user-singlecmd-table nil)
396 (defvar tmp-singlecmd-table nil)
398 ;---------- Key mode map ----------
399 ;;;
400 ;; Create new key map: YaTeX-mode-map
401 ;; Do not change this section.
402 ;;;
403 (defvar YaTeX-mode-map nil
404 "Keymap used in YaTeX mode")
406 (defvar YaTeX-prefix-map nil
407 "Keymap used when YaTeX-prefix key pushed")
409 (defvar YaTeX-user-extensional-map (make-sparse-keymap)
410 "*Keymap used for the user's customization")
411 (defvar YaTeX-current-completion-type nil
412 "Has current completion type. This may be used in YaTeX addin functions.")
414 (defvar YaTeX-modify-mode nil
415 "*Current editing mode.
416 When non-nil, each opening parentheses only opens,
417 nil enters both open/close parentheses when opening parentheses key pressed.")
419 (defvar YaTeX-math-mode nil
420 "Holds whether current mode is math-mode.")
421 ;;;
422 ;; Define key table
423 ;;;
424 (if YaTeX-mode-map
425 nil
426 (setq YaTeX-mode-map (make-sparse-keymap))
427 (setq YaTeX-prefix-map (make-sparse-keymap))
428 (define-key YaTeX-mode-map "\"" 'YaTeX-insert-quote)
429 (define-key YaTeX-mode-map "{" 'YaTeX-insert-braces)
430 (define-key YaTeX-mode-map "(" 'YaTeX-insert-parens)
431 (define-key YaTeX-mode-map "$" 'YaTeX-insert-dollar)
432 (define-key YaTeX-mode-map "|" 'YaTeX-insert-bar)
433 (define-key YaTeX-mode-map "&" 'YaTeX-insert-amper)
434 (define-key YaTeX-mode-map "[" 'YaTeX-insert-brackets)
435 (define-key YaTeX-mode-map YaTeX-prefix YaTeX-prefix-map)
436 (define-key YaTeX-mode-map "\M-\C-@" 'YaTeX-mark-environment)
437 (define-key YaTeX-mode-map "\M-\C-a" 'YaTeX-beginning-of-environment)
438 (define-key YaTeX-mode-map "\M-\C-e" 'YaTeX-end-of-environment)
439 (define-key YaTeX-mode-map "\M-\C-m" 'YaTeX-intelligent-newline)
440 (define-key YaTeX-mode-map "\C-i" 'YaTeX-indent-line)
441 (YaTeX-define-key "%" 'YaTeX-%-menu)
442 (YaTeX-define-key "t" 'YaTeX-typeset-menu)
443 (YaTeX-define-key "w" 'YaTeX-switch-mode-menu)
444 (YaTeX-define-key "'" 'YaTeX-prev-error)
445 (YaTeX-define-key "^" 'YaTeX-visit-main)
446 (YaTeX-define-key "4^" 'YaTeX-visit-main-other-window)
447 (YaTeX-define-key "4g" 'YaTeX-goto-corresponding-*-other-window)
448 (YaTeX-define-key "44" 'YaTeX-switch-to-window)
449 (and YaTeX-emacs-19 window-system
450 (progn
451 (YaTeX-define-key "5^" 'YaTeX-visit-main-other-frame)
452 (YaTeX-define-key "5g" 'YaTeX-goto-corresponding-*-other-frame)
453 (YaTeX-define-key "55" 'YaTeX-switch-to-window)))
454 (YaTeX-define-key " " 'YaTeX-do-completion)
455 (YaTeX-define-key "v" 'YaTeX-version)
457 (YaTeX-define-key "}" 'YaTeX-insert-braces-region)
458 (YaTeX-define-key "]" 'YaTeX-insert-brackets-region)
459 (YaTeX-define-key ")" 'YaTeX-insert-parens-region)
460 (YaTeX-define-key "$" 'YaTeX-insert-dollars-region)
461 (YaTeX-define-key "i" 'YaTeX-fill-item)
462 (YaTeX-define-key
463 "\\" '(lambda () (interactive) (insert "$\\backslash$")))
464 (if YaTeX-no-begend-shortcut
465 (progn
466 (YaTeX-define-key "B" 'YaTeX-make-begin-end-region)
467 (YaTeX-define-key "b" 'YaTeX-make-begin-end))
468 (YaTeX-define-begend-key "bc" "center")
469 (YaTeX-define-begend-key "bd" "document")
470 (YaTeX-define-begend-key "bD" "description")
471 (YaTeX-define-begend-key "be" "enumerate")
472 (YaTeX-define-begend-key "bE" "equation")
473 (YaTeX-define-begend-key "bi" "itemize")
474 (YaTeX-define-begend-key "bl" "flushleft")
475 (YaTeX-define-begend-key "bm" "minipage")
476 (YaTeX-define-begend-key "bt" "tabbing")
477 (YaTeX-define-begend-key "bT" "tabular")
478 (YaTeX-define-begend-key "b\^t" "table")
479 (YaTeX-define-begend-key "bp" "picture")
480 (YaTeX-define-begend-key "bq" "quote")
481 (YaTeX-define-begend-key "bQ" "quotation")
482 (YaTeX-define-begend-key "br" "flushright")
483 (YaTeX-define-begend-key "bv" "verbatim")
484 (YaTeX-define-begend-key "bV" "verse")
485 (YaTeX-define-key "B " 'YaTeX-make-begin-end-region)
486 (YaTeX-define-key "b " 'YaTeX-make-begin-end))
487 (YaTeX-define-key "e" 'YaTeX-end-environment)
488 (YaTeX-define-key "S" 'YaTeX-make-section-region)
489 (YaTeX-define-key "s" 'YaTeX-make-section)
490 (YaTeX-define-key "L" 'YaTeX-make-fontsize-region)
491 (YaTeX-define-key "l" 'YaTeX-make-fontsize)
492 (YaTeX-define-key "m" 'YaTeX-make-singlecmd)
493 (YaTeX-define-key "." 'YaTeX-comment-paragraph)
494 (YaTeX-define-key "," 'YaTeX-uncomment-paragraph)
495 (YaTeX-define-key ">" 'YaTeX-comment-region)
496 (YaTeX-define-key "<" 'YaTeX-uncomment-region)
497 (YaTeX-define-key "g" 'YaTeX-goto-corresponding-*)
498 (YaTeX-define-key "k" 'YaTeX-kill-*)
499 (YaTeX-define-key "c" 'YaTeX-change-*)
500 (YaTeX-define-key "a" 'YaTeX-make-accent)
501 (YaTeX-define-key "?" 'YaTeX-help)
502 (YaTeX-define-key "/" 'YaTeX-apropos)
503 (YaTeX-define-key "&" 'YaTeX-what-column)
504 (YaTeX-define-key "d" 'YaTeX-display-hierarchy)
505 (YaTeX-define-key "x" YaTeX-user-extensional-map)
506 (YaTeX-define-key "n"
507 '(lambda () (interactive)
508 (insert "\\" (if (YaTeX-on-section-command-p "o?oalign") "crcr" "\\"))))
509 (if YaTeX-dos
510 (define-key YaTeX-prefix-map "\C-r"
511 '(lambda () (interactive)
512 (set-screen-height YaTeX-saved-screen-height) (recenter)))))
514 (defvar YaTeX-section-completion-map nil
515 "*Key map used at YaTeX completion in the minibuffer.")
516 (if YaTeX-section-completion-map nil
517 (setq YaTeX-section-completion-map
518 (copy-keymap (or (and (boundp 'gmhist-completion-map)
519 gmhist-completion-map)
520 minibuffer-local-completion-map)))
521 (define-key YaTeX-section-completion-map
522 " " 'YaTeX-minibuffer-complete)
523 (define-key YaTeX-section-completion-map
524 "\C-i" 'YaTeX-minibuffer-complete)
525 (define-key YaTeX-section-completion-map
526 "\C-v" 'YaTeX-read-section-with-overview))
528 (defvar YaTeX-recursive-map nil
529 "*Key map used at YaTeX reading arguments in the minibuffer.")
530 (if YaTeX-recursive-map nil
531 (setq YaTeX-recursive-map (copy-keymap global-map))
532 (define-key YaTeX-recursive-map YaTeX-prefix YaTeX-prefix-map)
533 (mapcar
534 (function
535 (lambda (key)
536 (define-key YaTeX-mode-map (car key) 'YaTeX-math-insert-sequence)
537 (define-key YaTeX-recursive-map (car key) 'YaTeX-math-insert-sequence)))
538 YaTeX-math-key-list))
539 ;---------- Define other variable ----------
540 (defvar YaTeX-env-name "document" "*Initial tex-environment completion")
541 (defvar YaTeX-section-name
542 (if YaTeX-use-LaTeX2e "documentclass" "documentstyle")
543 "*Initial tex-section completion")
544 (defvar YaTeX-fontsize-name "large" "*Initial fontsize completion")
545 (defvar YaTeX-single-command "maketitle" "*Initial LaTeX single command")
546 (defvar YaTeX-kanji-code (if YaTeX-dos 1 2)
547 "*File kanji code used by Japanese TeX.
548 nil: Do not care (Preserve coding-system)
549 0: no-converion (mule)
550 1: Shift JIS
551 2: JIS
552 3: EUC
553 4: UTF-8")
555 (defvar YaTeX-coding-system nil "File coding system used by Japanese TeX.")
556 (cond
557 (YaTeX-emacs-20
558 (setq YaTeX-coding-system
559 (cdr (assoc YaTeX-kanji-code YaTeX-kanji-code-alist))))
560 ((boundp 'MULE)
561 (setq YaTeX-coding-system
562 (symbol-value (cdr (assoc YaTeX-kanji-code YaTeX-kanji-code-alist))))))
564 (defvar YaTeX-mode-syntax-table nil
565 "*Syntax table for yatex-mode")
567 (if YaTeX-mode-syntax-table nil
568 (setq YaTeX-mode-syntax-table (make-syntax-table (standard-syntax-table)))
569 (modify-syntax-entry ?\n " " YaTeX-mode-syntax-table)
570 (modify-syntax-entry ?\{ "(}" YaTeX-mode-syntax-table)
571 (modify-syntax-entry ?\} "){" YaTeX-mode-syntax-table)
572 (modify-syntax-entry ?\t " " YaTeX-mode-syntax-table)
573 (modify-syntax-entry ?\f ">" YaTeX-mode-syntax-table)
574 (modify-syntax-entry ?\n ">" YaTeX-mode-syntax-table)
575 (modify-syntax-entry ?$ "$$" YaTeX-mode-syntax-table)
576 (modify-syntax-entry ?% "<" YaTeX-mode-syntax-table)
577 (modify-syntax-entry ?\\ "/" YaTeX-mode-syntax-table)
578 (modify-syntax-entry ?~ " " YaTeX-mode-syntax-table))
580 ;---------- Provide YaTeX-mode ----------
581 ;;;
582 ;; Major mode definition
583 ;;;
584 (defun yatex-mode ()
585 " Yet Another LaTeX mode: Major mode for editing input files of LaTeX.
586 -You can invoke processes concerning LaTeX typesetting by
587 \\[YaTeX-typeset-menu]
588 -Complete LaTeX environment form of `\\begin{env} ... \\end{env}' by
589 \\[YaTeX-make-begin-end]
590 -Enclose region into some environment by
591 \\[universal-argument] \\[YaTeX-make-begin-end]
592 -Complete LaTeX command which takes argument like `\\section{}' by
593 \\[YaTeX-make-section]
594 -Put LaTeX command which takes no arguments like `\\maketitle' by
595 \\[YaTeX-make-singlecmd]
596 -Complete font or character size descriptor like `{\\large }' by
597 \\[YaTeX-make-fontsize]
598 -Enclose region into those descriptors above by
599 \\[universal-argument] \\[YaTeX-make-fontsize]
600 -Enter European accent notations by
601 \\[YaTeX-make-accent]
602 -Toggle various modes of YaTeX by
603 \\[YaTeX-switch-mode-menu]
604 -Change environt name (on the begin/end line) by
605 \\[YaTeX-change-*]
606 -Kill LaTeX command/environment sequences by
607 \\[YaTeX-kill-*]
608 -Kill LaTeX command/environment with its contents
609 \\[universal-argument] \\[YaTeX-kill-*]
610 -Go to corresponding object (begin/end, file, labels) by
611 \\[YaTeX-goto-corresponding-*] or
612 \\[YaTeX-goto-corresponding-*-other-window] (in other window)
613 \\[YaTeX-goto-corresponding-*-other-frame] (in other frame)
614 -Go to main LaTeX source text by
615 \\[YaTeX-visit-main] or
616 \\[YaTeX-visit-main-other-window] (in other window)
617 \\[YaTeX-visit-main-other-frame] (in other frame)
618 -Comment out or uncomment region by
619 \\[YaTeX-comment-region] or \\[YaTeX-uncomment-region]
620 -Comment out or uncomment paragraph by
621 \\[YaTeX-comment-paragraph] or \\[YaTeX-uncomment-paragraph]
622 -Make an \\item entry hang-indented by
623 \\[YaTeX-fill-item]
624 -Enclose the region with parentheses by
625 \\[YaTeX-insert-parens-region]
626 \\[YaTeX-insert-braces-region]
627 \\[YaTeX-insert-brackets-region]
628 \\[YaTeX-insert-dollars-region]
629 -Look up the corresponding column header of tabular environment by
630 \\[YaTeX-what-column]
631 -Enter a newline and an entry suitable for environment by
632 \\[YaTeX-intelligent-newline]
633 -View the structure of file inclusion by
634 \\[YaTeX-display-hierarchy]
635 -Refer the online help of popular LaTeX commands by
636 \\[YaTeX-help] (help)
637 \\[YaTeX-apropos] (apropos)
638 -Edit `%# notation' by
639 \\[YaTeX-%-menu]
641 Those are enough for fastening your editing of LaTeX source. But further
642 more features are available and they are documented in the manual.
643 "
644 (interactive)
645 (kill-all-local-variables)
646 (setq major-mode 'yatex-mode)
647 (setq mode-name (if YaTeX-japan "やてふ" "YaTeX"))
648 (mapcar 'make-local-variable
649 '(dvi2-command fill-column fill-prefix
650 tmp-env-table tmp-section-table tmp-fontsize-table
651 tmp-singlecmd-table paragraph-start paragraph-separate
652 YaTeX-math-mode indent-line-function comment-line-break-function
653 comment-start comment-start-skip
654 ))
655 (cond ((null YaTeX-kanji-code)
656 nil)
657 ((boundp 'MULE)
658 (set-file-coding-system YaTeX-coding-system))
659 ((and YaTeX-emacs-20 (boundp 'buffer-file-coding-system))
660 (setq buffer-file-coding-system
661 (or (and (fboundp 'set-auto-coding) buffer-file-name
662 (save-excursion
663 (goto-char (point-min))
664 (set-auto-coding buffer-file-name (buffer-size))))
665 YaTeX-coding-system)))
666 ((featurep 'mule)
667 (set-file-coding-system YaTeX-coding-system))
668 ((boundp 'NEMACS)
669 (make-local-variable 'kanji-fileio-code)
670 (setq kanji-fileio-code YaTeX-kanji-code)))
671 (setq fill-column YaTeX-fill-column
672 fill-prefix YaTeX-fill-prefix
673 paragraph-start YaTeX-paragraph-start
674 paragraph-separate YaTeX-paragraph-separate
675 indent-line-function 'YaTeX-indent-line
676 comment-start YaTeX-comment-prefix
677 comment-end ""
678 comment-start-skip "[^\\\\]%+[ \t]*"
679 local-abbrev-table yatex-mode-abbrev-table)
680 (if (fboundp 'comment-indent-new-line) ;for Emacs21
681 (setq comment-line-break-function 'YaTeX-comment-line-break))
683 (if (and YaTeX-use-font-lock (featurep 'font-lock))
684 (progn
685 (require 'yatex19)
686 (YaTeX-font-lock-set-default-keywords)
687 (or (featurep 'xemacs)
688 (set (make-local-variable 'font-lock-defaults)
689 (get 'yatex-mode 'font-lock-defaults)))
690 ;;(font-lock-mode 1)
691 ))
692 (use-local-map YaTeX-mode-map)
693 (set-syntax-table YaTeX-mode-syntax-table)
694 (if YaTeX-dos (setq YaTeX-saved-screen-height (screen-height)))
695 (YaTeX-read-user-completion-table)
696 (and (fboundp 'YaTeX-hilit-setup-alist) (YaTeX-hilit-setup-alist))
697 (makunbound 'inenv)
698 (turn-on-auto-fill) ;1.63
699 (and (= 0 (buffer-size)) (file-exists-p YaTeX-template-file)
700 (y-or-n-p (format "Insert %s?" YaTeX-template-file))
701 (insert-file-contents (expand-file-name YaTeX-template-file)))
702 (run-hooks 'text-mode-hook 'yatex-mode-hook))
704 ;---------- Define YaTeX-mode functions ----------
705 (defvar YaTeX-ec "\\" "Escape character of current mark-up language.")
706 (defvar YaTeX-ec-regexp (regexp-quote YaTeX-ec))
707 (defvar YaTeX-struct-begin
708 (concat YaTeX-ec "begin{%1}%2")
709 "Keyword format of begin-environment.")
710 (defvar YaTeX-struct-end
711 (concat YaTeX-ec "end{%1}")
712 "Keyword format of end-environment.")
713 (defvar YaTeX-struct-name-regexp "[^}]+"
714 "Environment name regexp.")
715 (defvar YaTeX-TeX-token-regexp
716 (cond (YaTeX-japan "[A-Za-z*ぁ-ん亜-龠]+")
717 (t "[A-Za-z*]+"))
718 "Regexp of characters which can be a member of TeX command's name.")
719 (defvar YaTeX-kanji-regexp "[ぁ-ん亜-龠]"
720 "Generic regexp of Japanese Kanji (and symbol) characters.")
721 (defvar YaTeX-command-token-regexp YaTeX-TeX-token-regexp
722 "Regexp of characters which can be a member of current mark up language's command name.")
724 ;;(defvar YaTeX-struct-section
725 ;; (concat YaTeX-ec "%1{%2}")
726 ;; "Keyword to make section.")
728 ;;;
729 ;; autoload section
730 ;;;
732 ;;autoload from yatexprc.el
733 (autoload 'YaTeX-visit-main "yatexprc" "Visit main LaTeX file." t)
734 (autoload 'YaTeX-visit-main-other-window "yatexprc"
735 "Visit main other window." t)
736 (autoload 'YaTeX-main-file-p "yatexprc" "Check if the file is main." t)
737 (autoload 'YaTeX-get-builtin "yatexprc" "Get %# built-in." t)
738 (autoload 'YaTeX-system "yatexprc" "Call system command" t)
739 (autoload 'YaTeX-save-buffers "yatexprc" "Save buffers of same major mode" t)
741 ;;autoload from yatexmth.el
742 (autoload 'YaTeX-math-insert-sequence "yatexmth" "Image input." t)
743 (autoload 'YaTeX-in-math-mode-p "yatexmth" "Check if in math-env." t)
744 (autoload 'YaTeX-toggle-math-mode "yatexmth" "YaTeX math-mode interfaces." t)
745 (autoload 'YaTeX-math-member-p "yatexmth" "Check if a word is math command." t)
746 (autoload 'YaTeX-insert-amsparens-region "yatexmth" "AMS parens region" t)
747 (autoload 'YaTeX-insert-amsbraces-region "yatexmth" "AMS braces region" t)
748 (autoload 'YaTeX-insert-amsbrackets-region "yatexmth" "AMS brackets region" t)
749 (autoload 'YaTeX-on-parenthesis-p "yatexmth" "Check if on math-parens" t)
750 (autoload 'YaTeX-goto-open-paren "yatexmth" "Goto opening paren" t)
751 (autoload 'YaTeX-change-parentheses "yatexmth" "Change corresponding parens" t)
752 (autoload 'YaTeX-goto-corresponding-paren "yatexmth" "\bigl\bigr jumps" t)
753 (autoload 'YaTeX-typeset-math-region "yatexmth" "Typeset math-region" t)
755 ;;autoload from yatexhlp.el
756 (autoload 'YaTeX-help "yatexhlp" "YaTeX helper with LaTeX commands." t)
757 (autoload 'YaTeX-apropos "yatexhlp" "Apropos for (La)TeX commands." t)
759 ;;autoload from yatexgen.el
760 (autoload 'YaTeX-generate "yatexgen" "YaTeX add-in function generator." t)
761 (autoload 'YaTeX-generate-simple "yatexgen" "YaTeX add-in support." t)
763 ;;autoload from yatexsec.el
764 (autoload 'YaTeX-section-overview "yatexsec" "YaTeX sectioning(view)" t)
765 (autoload 'YaTeX-read-section-in-minibuffer "yatexsec" "YaTeX sectioning" t)
766 (autoload 'YaTeX-make-section-with-overview "yatexsec" "YaTeX sectioning" t)
768 ;;autoload from yatexenv.el
769 (autoload 'YaTeX-what-column "yatexenv" "YaTeX env. specific funcs" t)
770 (autoload 'YaTeX-intelligent-newline "yatexenv" "YaTeX env. specific funcs" t)
771 (autoload 'YaTeX-indent-line-equation "yatexenv" "Indent equation lines." t)
772 (autoload 'YaTeX-goto-corresponding-leftright "yatexenv" "\left\right jumps" t)
774 ;;autoload from yatexhie.el
775 (autoload 'YaTeX-display-hierarchy "yatexhie"
776 "YaTeX document hierarchy browser" t)
777 (autoload 'YaTeX-display-hierarchy-directly "yatexhie"
778 "Same as YaTeX-display-hierarchy. Call from mouse." t)
780 ;;autoload from yatexpkg.el
781 (autoload 'YaTeX-package-auto-usepackage "yatexpkg" "Auto \\usepackage" t)
783 ;;;
784 ;; YaTeX-mode functions
785 ;;;
786 (defun YaTeX-insert-begin-end (env region-mode)
787 "Insert \\begin{mode-name} and \\end{mode-name}.
788 This works also for other defined begin/end tokens to define the structure."
789 (setq YaTeX-current-completion-type 'begin)
790 (let*((ccol (current-column)) beg beg2 exchange
791 (arg region-mode) ;for old compatibility
792 (indent-column (+ ccol YaTeX-environment-indent))(i 1) func)
793 (if (and region-mode (> (point) (mark)))
794 (progn (exchange-point-and-mark)
795 (setq exchange t
796 ccol (current-column)
797 indent-column (+ ccol YaTeX-environment-indent))))
798 ;;VER2 (insert "\\begin{" env "}" (YaTeX-addin env))
799 (setq beg (point))
800 (YaTeX-insert-struc 'begin env)
801 (setq beg2 (point))
802 (insert "\n")
803 (indent-to indent-column)
804 (save-excursion
805 ;;indent optional argument of \begin{env}, if any
806 (while (> (point-beginning-of-line) beg)
807 (skip-chars-forward "\\s " (point-end-of-line))
808 (indent-to indent-column)
809 (forward-line -1)))
810 (require 'yatexenv)
811 (if region-mode
812 ;;if region-mode, indent all text in the region
813 (save-excursion
814 (if (fboundp (intern-soft (concat "YaTeX-enclose-" env)))
815 (funcall (intern-soft (concat "YaTeX-enclose-" env))
816 (point) (mark))
817 (while (< (progn (forward-line 1) (point)) (mark))
818 (if (eolp) nil
819 (skip-chars-forward " \t\n")
820 (indent-to indent-column))))))
821 (if region-mode (exchange-point-and-mark))
822 (indent-to ccol)
823 ;;VER2 (insert "\\end{" env "}\n")
824 (YaTeX-insert-struc 'end env)
825 (YaTeX-reindent ccol)
826 (if region-mode
827 (progn
828 (insert "\n")
829 (or exchange (exchange-point-and-mark)))
830 (goto-char beg2)
831 (YaTeX-intelligent-newline nil)
832 (YaTeX-indent-line))
833 (YaTeX-package-auto-usepackage env 'env)
834 (if YaTeX-current-position-register
835 (point-to-register YaTeX-current-position-register))))
837 (defun YaTeX-make-begin-end (arg)
838 "Make LaTeX environment command of \\begin{env.} ... \\end{env.}
839 by completing read.
840 If you invoke this command with universal argument,
841 \(key binding for universal-argument is \\[universal-argument]\)
842 you can put REGION into that environment between \\begin and \\end."
843 (interactive "P")
844 (let*
845 ((mode (if arg " region" ""))
846 (env
847 (YaTeX-read-environment
848 (format "Begin environment%s(default %s): " mode YaTeX-env-name))))
849 (if (string= env "")
850 (setq env YaTeX-env-name))
851 (setq YaTeX-env-name env)
852 (YaTeX-update-table
853 (list YaTeX-env-name) 'env-table 'user-env-table 'tmp-env-table)
854 (YaTeX-insert-begin-end YaTeX-env-name arg)))
856 (defun YaTeX-make-begin-end-region ()
857 "Call YaTeX-make-begin-end with ARG to specify region mode."
858 (interactive)
859 (YaTeX-make-begin-end t))
861 (defun YaTeX-guess-section-type ()
862 (if (eq major-mode 'yatex-mode)
863 (save-excursion
864 (cond
865 ((save-excursion (not (search-backward YaTeX-ec nil t)))
866 (if YaTeX-use-LaTeX2e "documentclass" "documentstyle"))
867 ((progn
868 (if (= (char-after (1- (point))) ?~) (forward-char -1))
869 (forward-char -1) (looking-at "表\\|図\\|式\\|第"))
870 "ref")
871 ((and (looking-at "[a-z \t]")
872 (progn (skip-chars-backward "a-z \t")
873 (looking-at "table\\|figure\\|formula")))
874 "ref")
875 ((save-excursion
876 (skip-chars-backward "[^ア-ン]")
877 (looking-at "プログラム\\|リスト"))
878 "ref")
879 ((YaTeX-re-search-active-backward
880 (concat YaTeX-ec-regexp "begin{\\([^}]+\\)}")
881 (regexp-quote YaTeX-comment-prefix)
882 (save-excursion (forward-line -1) (point))
883 t)
884 (let ((env (YaTeX-match-string 1)))
885 (cdr (assoc env
886 '(("table" . "caption"))))))
887 ))))
889 (defun YaTeX-make-section (arg &optional beg end cmd)
890 "Make LaTeX \\section{} type command with completing read.
891 With numeric ARG, you can specify the number of arguments of
892 LaTeX command.
893 For example, if you want to produce LaTeX command
895 \\addtolength{\\topmargin}{8mm}
897 which has two arguments. You can produce that sequence by typing...
898 ESC 2 C-c s add SPC RET \\topm SPC RET 8mm RET
899 \(by default\)
900 Then yatex will automatically complete `addtolength' with two arguments
901 next time.
902 You can complete symbol at LaTeX command and the 1st argument.
904 If the optional 2nd and 3rd argument BEG END are specified, enclose
905 the region from BEG to END into the first argument of the LaTeX sequence.
906 Optional 4th arg CMD is LaTeX command name, for non-interactive use."
907 (interactive "P")
908 (setq YaTeX-current-completion-type 'section)
909 (if (equal arg '(4)) (setq beg (region-beginning) end (region-end)))
910 (unwind-protect
911 (let*
912 ((source-window (selected-window))
913 guess
914 (section
915 (or cmd
916 (progn
917 (setq guess
918 (or (YaTeX-guess-section-type) YaTeX-section-name))
919 (YaTeX-read-section
920 (if YaTeX-simple-messages
921 (format "Section-type (default %s): " guess)
922 (if (> (minibuffer-depth) 0)
923 (format "%s???{} (default %s)%s: "
924 YaTeX-ec guess
925 (format "[level:%d]" (minibuffer-depth)))
926 (format "(C-v for view-section) %s???{%s} (default %s): "
927 YaTeX-ec (if beg "region" "") guess)))
928 nil))))
929 (section (if (string= section "") guess section))
930 (numarg ;; The number of section-type command's argument
931 (or (and (numberp arg) arg)
932 (nth 1 (YaTeX-lookup-table section 'section))
933 1))
934 (arg-reader (intern-soft (concat "YaTeX::" section)))
935 (addin-args (and arg-reader (fboundp arg-reader)))
936 (title "")
937 (j 1)
938 (after-change-functions nil) ;inhibit font-locking temporarily
939 (enable-recursive-minibuffers t)
940 (mkarg-func
941 (function
942 (lambda (n)
943 (while (<= j n)
944 (insert
945 (concat ;to allow nil return value
946 "{"
947 (setq title
948 (cond
949 (addin-args (funcall arg-reader j))
950 (YaTeX-skip-default-reader "")
951 (t
952 (read-string
953 (format "Argument %d of %s: " j section)))))
954 "}"))
955 (setq j (1+ j))))))
956 );;let
957 (setq YaTeX-section-name section)
958 (if beg
959 (let*((e (make-marker))
960 (ar2 (intern-soft (concat "YaTeX::" section "-region")))
961 (arp (and ar2 (fboundp ar2))))
962 (goto-char end)
963 (insert "}")
964 (set-marker e (point))
965 (goto-char beg)
966 (insert YaTeX-ec YaTeX-section-name
967 (YaTeX-addin YaTeX-section-name))
968 (if (> numarg 1) (funcall mkarg-func (1- numarg)))
969 (insert "{")
970 (if arp (funcall ar2 (point) e))
971 (goto-char e)
972 (set-marker e nil))
973 (use-global-map YaTeX-recursive-map)
974 (if (= numarg 0) (YaTeX-make-singlecmd YaTeX-section-name)
975 (progn (insert YaTeX-ec YaTeX-section-name)
976 (insert (YaTeX-addin YaTeX-section-name))))
977 ;;read arguments with add-in
978 (funcall mkarg-func numarg))
979 (YaTeX-update-table
980 (if (/= numarg 1) (list section numarg)
981 (list section))
982 'section-table 'user-section-table 'tmp-section-table)
983 (if YaTeX-current-position-register
984 (point-to-register YaTeX-current-position-register))
985 (if (string= (buffer-substring (- (point) 2) (point)) "{}")
986 (forward-char -1))
987 (while (string= (buffer-substring (- (point) 3) (1- (point))) "{}")
988 (forward-char -2))
989 (YaTeX-package-auto-usepackage section 'section))
990 (if (<= (minibuffer-depth) 0) (use-global-map global-map))
991 (insert ""))) ;insert dummy string to fontify(Emacs20)
993 (defun YaTeX-make-section-region (args beg end)
994 "Call YaTeX-make-section with arguments to specify region mode."
995 (interactive "P\nr")
996 (YaTeX-make-section args beg end))
998 (defun YaTeX-make-fontsize (arg &optional fontsize)
999 "Make completion like {\\large ...} or {\\slant ...} in minibuffer.
1000 If you invoke this command with universal argument, you can put region
1001 into {\\xxx } braces.
1002 \(key binding for universal-argument is \\[universal-argument]\)"
1003 (interactive "P")
1004 (YaTeX-sync-local-table 'tmp-fontsize-table)
1005 (let* ((mode (if arg "region" ""))
1006 (fontsize
1007 (or fontsize
1008 (YaTeX-read-fontsize
1009 (if YaTeX-simple-messages
1010 (format "Font or size (default %s): " YaTeX-fontsize-name)
1011 (format "{\\??? %s} (default %s)%s: " mode YaTeX-fontsize-name
1012 (if (> (minibuffer-depth) 0)
1013 (format "[level:%d]" (minibuffer-depth)) "")))
1014 nil nil))))
1015 (if (string= fontsize "")
1016 (setq fontsize YaTeX-fontsize-name))
1017 (setq YaTeX-current-completion-type 'large)
1018 (setq YaTeX-fontsize-name fontsize)
1019 (YaTeX-update-table
1020 (list YaTeX-fontsize-name)
1021 'fontsize-table 'user-fontsize-table 'tmp-fontsize-table)
1022 (and YaTeX-use-LaTeX2e
1023 (YaTeX-latex2e-p)
1024 (setq fontsize
1025 (cdr (assoc YaTeX-fontsize-name LaTeX2e-fontstyle-alist)))
1026 (setq YaTeX-fontsize-name fontsize))
1027 (if arg
1028 (save-excursion
1029 (if (> (point) (mark)) (exchange-point-and-mark))
1030 (insert "{\\" YaTeX-fontsize-name " ")
1031 (exchange-point-and-mark)
1032 (insert "}"))
1033 (insert (concat "{\\" YaTeX-fontsize-name " }"))
1034 (forward-char -1)
1035 (if YaTeX-current-position-register
1036 (point-to-register YaTeX-current-position-register))
1037 (save-excursion
1038 (insert (YaTeX-addin YaTeX-fontsize-name)))
1039 (YaTeX-package-auto-usepackage YaTeX-fontsize-name 'large))))
1041 (defun YaTeX-make-fontsize-region ()
1042 "Call function:YaTeX-make-fontsize with ARG to specify region mode."
1043 (interactive)
1044 (YaTeX-make-fontsize t))
1046 (defvar YaTeX-singlecmd-suffix "" "*Suffix for maketitle-type commands.")
1047 (defvar YaTeX-read-singlecmd-history nil "Holds maketitle-type history.")
1048 (put 'YaTeX-read-singlecmd-history 'no-default t)
1049 (defun YaTeX-make-singlecmd (single)
1050 (interactive
1051 (list (YaTeX-cplread-with-learning
1052 (if YaTeX-simple-messages
1053 (format "maketitle-type (default %s): " YaTeX-single-command)
1054 (format "%s??? (default %s)%s: " YaTeX-ec YaTeX-single-command
1055 (if (> (minibuffer-depth) 0)
1056 (format "[level:%d]" (minibuffer-depth)) "")))
1057 'singlecmd-table 'user-singlecmd-table 'tmp-singlecmd-table
1058 nil nil nil 'YaTeX-read-singlecmd-history)))
1059 (if (string= single "")
1060 (setq single YaTeX-single-command))
1061 (setq YaTeX-single-command single)
1062 (setq YaTeX-current-completion-type 'maketitle)
1063 (let ((dollar (and (not (YaTeX-in-math-mode-p))
1064 (YaTeX-math-member-p YaTeX-single-command)))
1065 p q)
1066 (if dollar (insert "$"))
1067 (insert YaTeX-ec YaTeX-single-command)
1068 (setq p (point))
1069 (insert (YaTeX-addin single) YaTeX-singlecmd-suffix)
1070 (if dollar (insert "$"))
1071 (setq q (point))
1072 (goto-char p)
1073 (forward-char -2)
1074 (if (looking-at "\\[\\]") (forward-char 1) (goto-char q)))
1075 (YaTeX-package-auto-usepackage YaTeX-single-command 'maketitle)
1076 (if YaTeX-current-position-register
1077 (point-to-register YaTeX-current-position-register)))
1079 (defvar YaTeX-completion-begin-regexp "[{\\]"
1080 "Regular expression of limit where LaTeX command's completion begins.")
1082 (defun YaTeX-do-completion ()
1083 "Try completion on LaTeX command preceding point."
1084 (interactive)
1085 (if
1086 (or (eq (preceding-char) ? )
1087 (eq (preceding-char) ?\t)
1088 (eq (preceding-char) ?\n)
1089 (bobp))
1090 (message "Nothing to complete.") ;Do not complete
1091 (let* ((end (point))
1092 (limit (point-beginning-of-line))
1093 (completion-begin
1094 (progn (re-search-backward "[ \t\n]" limit 1) (point)))
1095 (begin (progn
1096 (goto-char end)
1097 (if (re-search-backward YaTeX-completion-begin-regexp
1098 completion-begin t)
1099 (1+ (point))
1100 nil))))
1101 (goto-char end)
1102 (cond
1103 ((null begin)
1104 (message "I think it is not a LaTeX sequence."))
1105 (t
1106 (mapcar 'YaTeX-sync-local-table
1107 '(tmp-section-table tmp-env-table tmp-singlecmd-table))
1108 (let*((pattern (buffer-substring begin end))
1109 (all-table
1110 (append
1111 section-table user-section-table tmp-section-table
1112 env-table user-env-table tmp-env-table
1113 singlecmd-table user-singlecmd-table tmp-singlecmd-table))
1114 ;; First,
1115 ;; search completion without backslash.
1116 (completion (try-completion pattern all-table)))
1117 (if
1118 (eq completion nil)
1119 ;; Next,
1120 ;; search completion with backslash
1121 (setq completion
1122 (try-completion (buffer-substring (1- begin) end)
1123 all-table nil)
1124 begin (1- begin)))
1125 (cond
1126 ((null completion)
1127 (message (concat "Can't find completion for '" pattern "'"))
1128 (ding))
1129 ((eq completion t) (message "Sole completion."))
1130 ((not (string= completion pattern))
1131 (delete-region begin end)
1132 (insert completion)
1134 (t
1135 (message "Making completion list...")
1136 (with-output-to-temp-buffer "*Help*"
1137 (display-completion-list
1138 (all-completions pattern all-table)))))))))))
1140 (defun YaTeX-toggle-modify-mode (&optional arg)
1141 (interactive "P")
1142 (or (memq 'YaTeX-modify-mode mode-line-format)
1143 (setq mode-line-format
1144 (append (list "" 'YaTeX-modify-mode) mode-line-format)))
1145 (if (or arg (null YaTeX-modify-mode))
1146 (progn
1147 (setq YaTeX-modify-mode "*m*")
1148 (message "Modify mode"))
1149 (setq YaTeX-modify-mode nil)
1150 (message "Cancel modify mode."))
1151 (set-buffer-modified-p (buffer-modified-p))) ;redraw mode-line
1153 (defun YaTeX-switch-mode-menu (arg &optional char)
1154 (interactive "P")
1155 (message "Toggle: (M)odify-mode ma(T)h-mode")
1156 (let ((c (or char (read-char))))
1157 (cond
1158 ((= c ?m) (YaTeX-toggle-modify-mode arg))
1159 ((or (= c ?$) (= c ?t))
1160 (if YaTeX-auto-math-mode
1161 (message "Makes no sense in YaTeX-auto-math-mode.")
1162 (YaTeX-toggle-math-mode arg))))))
1164 (defun YaTeX-insert-quote ()
1165 (interactive)
1166 (insert
1167 (cond
1168 ((YaTeX-literal-p) ?\")
1169 ((= (preceding-char) ?\\ ) ?\")
1170 ;((= (preceding-char) ?\( ) ?\")
1171 ((or (= (preceding-char) 32)
1172 (= (preceding-char) 9)
1173 (= (preceding-char) ?\n)
1174 (bobp)
1175 (string-match
1176 (regexp-quote (char-to-string (preceding-char)))
1177 "、。,.?!「」『』【】()"))
1178 "``")
1179 (t "''"))))
1181 (defun YaTeX-closable-p ()
1182 (and (not YaTeX-modify-mode)
1183 (not (eq YaTeX-close-paren-always 'never))
1184 (or YaTeX-close-paren-always (eolp))
1185 (not (input-pending-p))
1186 (not (YaTeX-literal-p)))
1187 ;;(or YaTeX-modify-mode
1188 ;; (and (not YaTeX-close-paren-always) (not (eolp)))
1189 ;; (input-pending-p)
1190 ;; (YaTeX-quick-in-environment-p "verbatim"))
1193 (defun YaTeX-insert-braces-region (beg end &optional open close)
1194 (interactive "r")
1195 (save-excursion
1196 (goto-char end)
1197 (insert (or close "}"))
1198 (goto-char beg)
1199 (insert (or open "{"))))
1201 (defun YaTeX-insert-braces (arg &optional open close)
1202 (interactive "p")
1203 (let (env)
1204 (cond
1205 ((YaTeX-jmode) (YaTeX-self-insert arg))
1206 ((not (YaTeX-closable-p)) (YaTeX-self-insert arg))
1207 ((save-excursion
1208 (and (> (- (point) (point-min)) 6)
1209 (condition-case () (forward-char -6) (error nil)))
1210 (looking-at "\\\\left\\\\"))
1211 (insert "{\\right\\}")
1212 (forward-char -8))
1213 ((save-excursion ;from matsu@math.s.chiba-u.ac.jp
1214 (and (> (- (point) (point-min)) 6) (forward-char -6))
1215 (looking-at "\\\\[bB]igl\\\\"))
1216 (insert
1217 (concat
1218 "{" (buffer-substring (match-beginning 0) (- (match-end 0) 2)) "r\\}"))
1219 (forward-char -7))
1220 ((save-excursion
1221 (and (> (- (point) (point-min)) 7)
1222 (condition-case () (forward-char -7) (error nil)))
1223 (looking-at "\\\\[bB]iggl\\\\"))
1224 (insert
1225 (concat
1226 "{" (buffer-substring (match-beginning 0) (- (match-end 0) 2)) "r\\}"))
1227 (forward-char -8))
1228 ((= (preceding-char) ?\\ )
1229 (insert "{\\}")
1230 (forward-char -2)) ;matsu's hack ends here
1231 ((and (> (point) (+ (point-min) 4))
1232 (save-excursion (backward-char 4) (looking-at "\\\\end"))
1233 (not (YaTeX-literal-p))
1234 (setq env (YaTeX-inner-environment)))
1235 (momentary-string-display
1236 (concat
1237 "{"
1238 (cond
1239 (YaTeX-japan
1240 (format "今度からはちゃんと %s b を使いましょう" YaTeX-prefix))
1241 (t (format "You don't understand Zen of `%s b':p" YaTeX-prefix)))
1242 "}")
1243 (point))
1244 (insert (or open "{") env (or close "}")))
1245 (t
1246 (insert (or open "{") (or close "}"))
1247 (forward-char -1)
1248 (if (and (eq (char-after (point)) ?\})
1249 (eq (char-after (- (point) 2)) ?\\ ))
1250 (progn (insert "\\") (forward-char -1)))
1251 ))))
1253 (defun YaTeX-jmode ()
1254 (or (and (boundp 'canna:*japanese-mode*) canna:*japanese-mode*)
1255 (and (boundp 'egg:*mode-on*) egg:*mode-on* egg:*input-mode*)))
1257 (defun YaTeX-jmode-off ()
1258 (cond
1259 ((and (boundp 'canna:*japanese-mode*) canna:*japanese-mode*)
1260 (canna-toggle-japanese-mode))
1261 ((and (boundp 'egg:*mode-on*) egg:*mode-on* egg:*input-mode*)
1262 (egg:toggle-egg-mode-on-off))
1263 ((and (fboundp 'skk-mode) (boundp 'skk-mode) skk-mode)
1264 (cond
1265 ((fboundp 'skk-latin-mode) (skk-latin-mode t))
1266 ((fboundp 'skk-mode-off) (skk-mode-off))
1267 (t (j-mode-off))))
1268 ((and (fboundp 'toggle-input-method) current-input-method)
1269 (toggle-input-method))
1270 ((and (fboundp 'fep-force-off) (fep-force-off)))))
1272 (defun YaTeX-self-insert (arg)
1273 (call-interactively (global-key-binding (char-to-string last-command-char))))
1275 (defun YaTeX-insert-brackets (arg)
1276 "Insert Kagi-kakko or \\ [ \\] pair or simply \[."
1277 (interactive "p")
1278 (let ((col (1- (current-column))))
1279 (cond
1280 ((YaTeX-jmode) (YaTeX-self-insert arg))
1281 ((not (YaTeX-closable-p))
1282 (YaTeX-self-insert arg))
1283 ((save-excursion
1284 (and (> (- (point) (point-min)) 5) (forward-char -5))
1285 (looking-at "\\\\left"))
1286 (insert "[\\right]")
1287 (forward-char -7))
1288 ((save-excursion ;from matsu@math.s.chiba-u.ac.jp
1289 (and (> (- (point) (point-min)) 5) (forward-char -5))
1290 (looking-at "\\\\[bB]igl"))
1291 (insert
1292 (concat
1293 "[" (buffer-substring (match-beginning 0) (- (match-end 0) 1)) "r]"))
1294 (forward-char -6))
1295 ((save-excursion
1296 (and (> (- (point) (point-min)) 6) (forward-char -6))
1297 (looking-at "\\\\[bB]iggl"))
1298 (insert
1299 (concat
1300 "[" (buffer-substring (match-beginning 0) (- (match-end 0) 1)) "r]"))
1301 (forward-char -7)) ;matsu's hack ends here
1302 ((and (= (preceding-char) ?\\ )
1303 (/= (char-after (- (point) 2)) ?\\ )
1304 (not (YaTeX-in-math-mode-p)))
1305 (insert last-command-char "\n")
1306 (indent-to (max 0 col))
1307 (insert "\\]")
1308 (beginning-of-line)
1309 (open-line 1)
1310 (delete-region (point) (progn (beginning-of-line) (point)))
1311 (indent-to (+ YaTeX-environment-indent (max 0 col)))
1312 (or YaTeX-auto-math-mode YaTeX-math-mode (YaTeX-toggle-math-mode 1)))
1313 ((YaTeX-closable-p)
1314 (insert "[]")
1315 (backward-char 1))
1316 (t (YaTeX-self-insert arg)))))
1318 (defun YaTeX-insert-brackets-region (beg end)
1319 (interactive "r")
1320 (YaTeX-insert-braces-region beg end "[" "]"))
1322 (defun YaTeX-insert-parens (arg)
1323 "Insert parenthesis pair."
1324 (interactive "p")
1325 (cond
1326 ((YaTeX-jmode) (YaTeX-self-insert arg))
1327 ((not (YaTeX-closable-p)) (YaTeX-self-insert arg))
1328 ((save-excursion
1329 (and (> (- (point) (point-min)) 5) (forward-char -5))
1330 (looking-at "\\\\left"))
1331 (insert "(\\right)")
1332 (forward-char -7))
1333 ((save-excursion ;from matsu@math.s.chiba-u.ac.jp
1334 (and (> (- (point) (point-min)) 5) (forward-char -5))
1335 (looking-at "\\\\[bB]igl"))
1336 (insert
1337 (concat
1338 "(" (buffer-substring (match-beginning 0) (- (match-end 0) 1)) "r)"))
1339 (forward-char -6))
1340 ((save-excursion
1341 (and (> (- (point) (point-min)) 6) (forward-char -6))
1342 (looking-at "\\\\[bB]iggl"))
1343 (insert
1344 (concat
1345 "(" (buffer-substring (match-beginning 0) (- (match-end 0) 1)) "r)"))
1346 (forward-char -7))
1347 ((= (preceding-char) ?\\ ) ;matsu's hack ends here
1348 (insert "(\\)")
1349 (backward-char 2))
1350 ((YaTeX-closable-p)
1351 (insert "()")
1352 (backward-char 1))
1353 (t (YaTeX-self-insert arg))))
1355 (defun YaTeX-insert-parens-region (beg end)
1356 (interactive "r")
1357 (YaTeX-insert-braces-region beg end "(" ")"))
1359 (defun YaTeX-insert-bar (arg)
1360 "Insert bar pair."
1361 (interactive "p")
1362 (cond
1363 ((YaTeX-jmode) (YaTeX-self-insert arg))
1364 ((not (YaTeX-closable-p)) (YaTeX-self-insert arg))
1365 ((save-excursion
1366 (and (> (- (point) (point-min)) 5) (forward-char -5))
1367 (looking-at "\\\\left"))
1368 (insert "|\\right|")
1369 (forward-char -7))
1370 ((save-excursion ;from matsu@math.s.chiba-u.ac.jp
1371 (and (> (- (point) (point-min)) 5) (forward-char -5))
1372 (looking-at "\\\\[bB]igl"))
1373 (insert
1374 (concat
1375 "|" (buffer-substring (match-beginning 0) (- (match-end 0) 1)) "r|"))
1376 (forward-char -6))
1377 ((save-excursion
1378 (and (> (- (point) (point-min)) 6) (forward-char -6))
1379 (looking-at "\\\\[bB]iggl"))
1380 (insert
1381 (concat
1382 "|" (buffer-substring (match-beginning 0) (- (match-end 0) 1)) "r|"))
1383 (forward-char -7))
1384 ((save-excursion ; added by Jin <MAF01011@nifty.ne.jp>
1385 (and (> (- (point) (point-min)) 6) (forward-char -6))
1386 (looking-at "\\\\left\\\\"))
1387 (insert "|\\right\\|")
1388 (forward-char -8))
1389 ((save-excursion
1390 (and (> (- (point) (point-min)) 6) (forward-char -6))
1391 (looking-at "\\\\[bB]igl\\\\"))
1392 (insert
1393 (concat
1394 "|" (buffer-substring (match-beginning 0) (- (match-end 0) 2)) "r\\|"))
1395 (forward-char -7))
1396 ((save-excursion
1397 (and (> (- (point) (point-min)) 7) (forward-char -7))
1398 (looking-at "\\\\[bB]iggl\\\\"))
1399 (insert
1400 (concat
1401 "|" (buffer-substring (match-beginning 0) (- (match-end 0) 2)) "r\\|"))
1402 (forward-char -8)) ; added by Jin up to here.
1403 ((= (preceding-char) ?\\ )
1404 (insert "|\\|")
1405 (backward-char 2))
1406 ; ((and (YaTeX-closable-p)
1407 ; (/= (preceding-char) ?|)
1408 ; (/= (following-char) ?|))
1409 ; (insert "||")
1410 ; (backward-char 1))
1411 (t (YaTeX-self-insert arg))))
1413 (defun YaTeX-insert-dollar ()
1414 (interactive)
1415 (if (or (not (YaTeX-closable-p))
1416 (= (preceding-char) 92)
1417 (and (YaTeX-in-math-mode-p)
1418 (or (/= (preceding-char) ?$) (/= (following-char) ?$))))
1419 (insert "$")
1420 (insert "$$")
1421 (forward-char -1)
1422 (YaTeX-jmode-off)
1423 (or YaTeX-auto-math-mode YaTeX-math-mode (YaTeX-toggle-math-mode 1))))
1425 (defun YaTeX-insert-dollars-region (beg end)
1426 (interactive "r")
1427 (YaTeX-insert-braces-region beg end "$" "$"))
1429 (defun YaTeX-insert-amper ()
1430 (interactive)
1431 (if (or (string-match YaTeX-array-env-regexp
1432 (or (YaTeX-inner-environment t) "document"))
1433 (= (preceding-char) 92)
1434 (YaTeX-literal-p)
1435 (YaTeX-in-math-mode-p))
1436 (insert "&")
1437 (insert "\\&")))
1439 (defun YaTeX-version ()
1440 "Return string of the version of running YaTeX."
1441 (interactive)
1442 (message
1443 (concat "Yet Another tex-mode "
1444 (if YaTeX-japan "「野鳥」" "`Wild Bird'")
1445 " Revision "
1446 YaTeX-revision-number)))
1448 (defun YaTeX-typeset-menu (arg &optional char)
1449 "Typeset, preview, visit error and miscellaneous convenient menu.
1450 Optional second argument CHAR is for non-interactive call from menu."
1451 (interactive "P")
1452 (message
1453 (concat "J)latex R)egion E)nv B)ibtex mk(I)dx "
1454 "latex+p(D)f "
1455 (if (fboundp 'start-process) "K)ill ")
1456 "P)review "
1457 (and (boundp 'window-system) window-system "S)earch ")
1458 "V)iewErr L)pr"))
1459 (let ((sw (selected-window)) (c (or char (read-char))))
1460 (require 'yatexprc) ;for Nemacs's bug
1461 (select-window sw)
1462 (cond
1463 ((= c ?j) (YaTeX-typeset-buffer))
1464 ((= c ?r) (YaTeX-typeset-region))
1465 ((= c ?e) (YaTeX-typeset-environment))
1466 ((= c ?b) (YaTeX-call-command-on-file
1467 bibtex-command "*YaTeX-bibtex*" YaTeX-parent-file))
1468 ((= c ?i) (YaTeX-call-command-on-file
1469 makeindex-command "*YaTeX-makeindex*" YaTeX-parent-file))
1470 ((= c ?k) (YaTeX-kill-typeset-process YaTeX-typeset-process))
1471 ((= c ?p) (call-interactively 'YaTeX-preview))
1472 ((= c ?q) (YaTeX-system "lpq" "*Printer queue*"))
1473 ((= c ?d) (YaTeX-typeset-buffer YaTeX-dvipdf-command))
1474 ((= c ?v) (YaTeX-view-error))
1475 ((= c ?l) (YaTeX-lpr arg))
1476 ((= c ?m) (YaTeX-switch-mode-menu arg))
1477 ((= c ?b) (YaTeX-insert-string "\\"))
1478 ((= c ?s) (YaTeX-xdvi-remote-search arg)))))
1480 (if (fboundp 'wrap-function-to-control-ime)
1481 (wrap-function-to-control-ime 'YaTeX-typeset-menu t "P"))
1484 (defun YaTeX-%-menu (&optional beg end char)
1485 "Operate %# notation."
1486 ;;Do not use interactive"r" for the functions which require no mark
1487 (interactive)
1488 (message "!)Edit-%%#! B)EGIN-END-region L)Edit-%%#LPR")
1489 (let ((c (or char (read-char))) (string "") key
1490 (b (make-marker)) (e (make-marker)))
1491 (save-excursion
1492 (cond
1493 ((or (= c ?!) (= c ?l)) ;Edit `%#!'
1494 (goto-char (point-min))
1495 (setq key (cond ((= c ?!) "%#!")
1496 ((= c ?l) "%#LPR")))
1497 (if (re-search-forward key nil t)
1498 (progn
1499 (setq string (buffer-substring (point) (point-end-of-line)))
1500 (delete-region (point) (progn (end-of-line) (point))))
1501 (open-line 1)
1502 (delete-region (point) (progn (beginning-of-line)(point)));for 19 :-<
1503 (insert key))
1504 (unwind-protect
1505 (setq string (read-string (concat key ": ") string))
1506 (insert string)))
1508 ((= c ?b) ;%#BEGIN %#END region
1509 (or end (setq beg (min (point) (mark)) end (max (point) (mark))))
1510 (set-marker b beg)
1511 (set-marker e end)
1512 (goto-char (point-min))
1513 (while (re-search-forward "^%#\\(BEGIN\\)\\|\\(END\\)$" nil t)
1514 (beginning-of-line)
1515 (delete-region (point) (progn (forward-line 1) (point))))
1516 (goto-char b)
1517 (open-line 1)
1518 (delete-region (point) (progn (beginning-of-line)(point)));for 19 :-<
1519 (insert "%#BEGIN")
1520 (goto-char e)
1521 (insert "%#END\n")
1522 (set-marker b nil)
1523 (set-marker e nil))
1524 ))))
1526 (defun YaTeX-goto-corresponding-label (reverse &optional otherwin)
1527 "Jump to corresponding \\label{} and \\ref{} or \\cite and \\bibitem.
1528 The default search direction depends on the command at the cursor position.
1529 When the cursor is on \\ref(\\cite), YaTeX will try to search the
1530 corresponding \\label(\\bibitem) backward,
1531 and if it fails search forward again. And when the cursor is
1532 on \\label(\\bibitem), YaTeX will search the corresponding \\ref(\\cite)
1533 forward at first and secondary backward.
1534 Argument REVERSE non-nil makes the default
1535 direction rule reverse. Since Search string is automatically set in
1536 search-last-string, you can repeat search the same label/ref by typing
1537 \\[isearch-forward] or \\[isearch-backward].
1538 If optional second argument OTHERWIN is non-nil, move to other window."
1540 (let ((scmd "") label direc string blist (p (point)) (cb (current-buffer))
1541 (refcommands "label\\|\\(page\\|eq\\)?ref\\|cite\\|bibitem")
1542 (foundmsg (format "Type %s %c to return to original position."
1543 (key-description
1544 (car
1545 (or (where-is-internal 'register-to-point)
1546 (where-is-internal 'jump-to-register))))
1547 YaTeX-current-position-register))
1548 (func (function (lambda (string sfunc)
1549 (or
1550 (funcall sfunc string nil t)
1551 (funcall (if (eq sfunc 're-search-forward)
1552 're-search-backward 're-search-forward)
1553 string nil t))))))
1554 (cond
1555 ((YaTeX-on-section-command-p refcommands)
1556 (setq scmd
1557 (cdr
1558 (assoc
1559 (YaTeX-match-string 1)
1560 '(("label" . "\\\\\\(page\\|eq\\)?ref{%k}")
1561 ("ref" . "\\\\label{%k}")
1562 ("eqref" . "\\\\label{%k}")
1563 ("pageref" . "\\\\label{%k}")
1564 ("cite" .
1565 "\\\\bibitem\\(\\[[^]]+\\]\\)?{%k}\\|^\\s *@[a-z]+{%k,")
1566 ("bibitem" . "\\\\cite\\(\\[[^]]+\\]\\)?")))))
1567 (goto-char (match-end 0))
1568 (let ((label (buffer-substring
1569 (1- (point)) (progn (backward-list 1) (1+ (point)))))
1570 (fp (make-marker))fl fn
1571 (goother (function (lambda (buffer point)
1572 (goto-char point)
1573 (if (one-window-p)
1574 (split-window-calculate-height
1575 YaTeX-default-pop-window-height))
1576 (select-window (get-lru-window))
1577 (switch-to-buffer buffer)))))
1578 ;(setq string (concat "\\" scmd "{" label "}"))
1579 ;(setq string (concat "\\\\" scmd "{" (regexp-quote label) "}"))
1580 (setq string (YaTeX-replace-format scmd "k" (regexp-quote label)))
1581 (setq direc (if (string-match "ref\\|cite" scmd)
1582 're-search-forward 're-search-backward))
1583 (if YaTeX-current-position-register
1584 (point-to-register YaTeX-current-position-register))
1585 (if reverse (setq direc (if (eq direc 're-search-forward)
1586 're-search-backward 're-search-forward)))
1587 (if (funcall func string direc) ;label/ref found!
1588 (progn
1589 (if otherwin (funcall goother cb p))
1590 (goto-char (match-beginning 0))
1591 (push-mark p))
1592 ;;if label/ref not found, search through all yatex buffers.
1593 (goto-char p) ;resume position of current buffer
1594 (catch 'found
1595 (setq blist (YaTeX-yatex-buffer-list))
1596 (while blist
1597 ;; search for corresponding keyword
1598 (set-buffer (car blist))
1599 (if (YaTeX-on-section-command-p refcommands)
1600 (goto-char (match-beginning 0)))
1601 (cond
1602 ; cond1
1603 ((funcall func string direc)
1604 (cond
1605 (otherwin
1606 (set-buffer cb)
1607 (funcall goother (car blist) p))
1608 ((or (get-buffer-window (car blist))
1609 (and YaTeX-emacs-19
1610 (get-buffer-window (car blist) t)))
1611 (goto-buffer-window (car blist)))
1612 (t
1613 (switch-to-buffer (car blist))
1614 (message foundmsg)))
1615 (goto-char (match-beginning 0))
1616 (throw 'found t))
1617 ; cond2
1618 ((and
1619 (string-match "bibitem" scmd)
1620 (catch 'found2
1621 (save-excursion
1622 (goto-char (point-min))
1623 (while (YaTeX-re-search-active-forward
1624 "\\\\bibliography{\\([^}]*\\)}" "%" nil t)
1625 (setq fl (YaTeX-split-string (YaTeX-match-string 1) ","))
1626 (while fl
1627 (if (or (file-exists-p (setq fn (car fl)))
1628 (file-exists-p (setq fn (concat fn ".bib"))))
1629 (progn
1630 (set-buffer (find-file-noselect fn))
1631 (save-excursion
1632 (goto-char (point-min))
1633 (if (YaTeX-re-search-active-forward
1634 string "%" nil t)
1635 (throw 'found2
1636 (set-marker fp (point)))))))
1637 (setq fl (cdr fl)))))))
1638 (if otherwin
1639 (funcall goother (marker-buffer fp) fp)
1640 (switch-to-buffer (marker-buffer fp))
1641 (goto-char fp))
1642 (set-marker fp nil)
1643 (message foundmsg)
1644 (throw 'found t)))
1645 (setq blist (cdr blist)))
1646 ;; search for bibliography
1647 )))
1648 (if YaTeX-emacs-19
1649 (setq regexp-search-ring
1650 (cons string (delete string regexp-search-ring)))
1651 (setq search-last-regexp string)))
1652 (t nil))))
1654 ;;YaTeX-goto-corresponding-environment was moved to yatexlib
1656 (defun YaTeX-goto-corresponding-file (&optional other)
1657 "Visit or switch buffer of corresponding file,
1658 looking at \\input or \\include or \includeonly on current line."
1659 (if (not (YaTeX-on-includes-p)) nil
1660 (let ((parent buffer-file-name) input-file b)
1661 (save-excursion
1662 (if (and (re-search-forward "[{%]" (point-end-of-line) t)
1663 (= ?{ (char-after (match-beginning 0))))
1664 nil
1665 (skip-chars-backward "^,{"))
1666 (setq input-file
1667 (buffer-substring
1668 (point) (progn (skip-chars-forward "^ ,}") (point))))
1669 (if (not (string-match "\\.\\(tex\\|sty\\)$" input-file))
1670 (setq input-file (concat input-file ".tex"))))
1671 (cond
1672 (other (YaTeX-switch-to-buffer-other-window input-file))
1673 ((setq b (YaTeX-get-file-buffer input-file))
1674 (goto-buffer-window b))
1675 (t (YaTeX-switch-to-buffer input-file)))
1676 (or (YaTeX-get-builtin "!")
1677 YaTeX-parent-file
1678 (setq YaTeX-parent-file parent)))))
1680 (defun YaTeX-goto-corresponding-BEGIN-END ()
1681 (if (not (YaTeX-on-BEGIN-END-p)) nil
1682 (if (cond
1683 ((equal (match-beginning 0) (match-beginning 1)) ;if on %#BEGIN
1684 (not (search-forward "%#END" nil t)))
1685 (t ; if on %#END
1686 (not (search-backward "%#BEGIN" nil t))))
1687 (error "Corresponding %%#BEGIN/END not found."))
1688 (beginning-of-line)
1689 t))
1691 (defvar YaTeX-processed-file-regexp-alist nil
1692 "Alist of regexp of processed file regexp vs. its file name part;
1693 For example, if you include image file with `\\epsfile{file=FILE}' where
1694 `FILE' is processed file. You might want to view FILE with other previewer
1695 such as ghostview, or want to preview its source which was drawn with
1696 other drawing tool, tgif for example. Then you should set entire regexp
1697 of including expression and enclose its file name part with \\\\( and \\\\).
1699 Ex. (\"\\\\\\\\epsfile{[^}]*file=\\\\([^,} ]+\\\\)\\\\(\\\\.e?ps\\\\)?[^}]*}\" 1)
1701 Where the first group surrounded by \\\\( and \\\\) is the file name part
1702 of expression. So you should set 1 to second element. And the first
1703 matching group is sent to (image) processor defined by the variable
1704 YaTeX-file-processor-alist. See also the documentation of
1705 YaTeX-file-processor-alist.
1707 ↑じゃ良くわかんないすね。例えば tgif hoge.obj して hoge.eps を
1708 \\epsfile{file=hoge.eps} でインクルードしているとしよう。その行で
1709 \[prefix\] g を押した時に tgif を起動して欲しかったら、まず上のような
1710 正規表現を設定する。\\\\(と\\\\)で囲んだところがファイル名になるように
1711 注意する。でファイル名部分が何番目の\\\\(\\\\)になるかをリストの2番目に書く。
1712 すると、その部分が変数 YaTeX-file-processor-alist で定義された
1713 処理プログラムに渡される。というわけ。
1714 ん〜やっぱりむずかしいね。分からない時は隣の Lisper に聞くか、
1715 fj野鳥の会で聞こう!
1716 ")
1718 (defvar YaTeX-processed-file-regexp-alist-default
1719 '(("\\\\epsfile\\(\\[[^]]+\\]\\)?{[^},]*file=\\(\\([^,} ]*/\\)?[^,}. ]+\\)\\(\\.e?ps\\)?[^}]*}" 2)
1720 ("\\\\epsfig{[^},]*fi\\(le\\|gure\\)=\\(\\([^,} ]*/\\)?[^,}. ]+\\)\\(\\.e?ps\\)?[^}]*}" 2)
1721 ("\\\\postscriptbox{[^}]*}{[^}]*}{\\(\\([^,} ]*/\\)?[^}. ]+\\)\\(\\.e?ps\\)?}" 1)
1722 ("\\\\\\(epsfbox\\|includegraphics\\|epsfig\\)\\*?{\\(\\([^,} ]*/\\)?[^}. ]+\\)\\(\\.e?ps\\)?}" 2) ;\epsfbox{hoge.ps} or \includegraphics{hoge.eps}
1723 ("\\\\\\(psbox\\)\\(\\[[^]]+\\]\\)?{\\(\\([^,} ]*/\\)?[^} ]+\\)\\(\\.e?ps\\)}" 3) ;\psbox[options...]{hoge.eps} (97/1/11)
1724 ("\\\\input{\\([^} ]+\\)\\(\\.tps\\)}" 1) ;tgif2tex (1998/9/16)
1726 "See the documentation of YaTeX-processed-file-regexp-alist.")
1728 (defvar YaTeX-file-processor-alist nil
1729 "*Alist of files' processor vs. its extension;
1730 See also the documentation of YaTeX-processed-file-regexp-alist.")
1732 (defvar YaTeX-file-processor-alist-default
1733 '(("tgif" . ".obj")
1734 ("ghostview" . ".ps")
1735 ("ghostview" . ".eps")
1736 (t . ".tex")
1737 (t . ".sty")
1738 (t . ""))
1739 "See the documentation of YaTeX-file-processor-alist.")
1741 (defun YaTeX-goto-corresponding-file-processor (&optional other)
1742 "Execute corresponding file processor."
1743 (save-excursion
1744 (or (looking-at YaTeX-ec-regexp)
1745 (skip-chars-backward (concat "^" YaTeX-ec) (point-beginning-of-line)))
1746 (let ((list (append YaTeX-processed-file-regexp-alist
1747 YaTeX-processed-file-regexp-alist-default))
1748 (p (point)) flist file
1749 (peol (point-end-of-line))
1750 (basedir
1751 (if YaTeX-search-file-from-top-directory
1752 (save-excursion (YaTeX-visit-main t) default-directory)
1753 ".")))
1754 (setq flist (catch 'found
1755 (while list
1756 (goto-char p)
1757 (if (re-search-forward (car (car list)) peol t)
1758 (progn
1759 (setq file (YaTeX-match-string
1760 (car (cdr (car list)))))
1761 (throw 'found (cdr (car list)))))
1762 (setq list (cdr list)))))
1763 (if flist ;if pattern and file name found
1764 (let*((plist (append YaTeX-file-processor-alist
1765 YaTeX-file-processor-alist-default))
1766 (plist0 plist)
1767 ext cmd src buf (alt (car (cdr flist))))
1768 (if (and (re-search-forward
1769 (concat YaTeX-comment-prefix "\\s *\\(.*\\)$") peol t)
1770 (assoc (setq cmd (YaTeX-match-string 1))
1771 YaTeX-file-processor-alist))
1772 (setq src ;if processor is specified
1773 (concat file
1774 (cdr (assoc cmd YaTeX-file-processor-alist))))
1775 (while plist ;if processor is not specified
1776 (setq ext (cdr (car plist)))
1777 (if (and (string< "" (concat file ext))
1778 (file-exists-p
1779 (expand-file-name (concat file ext) basedir)))
1780 (setq cmd (car (car plist))
1781 src (concat file ext) plist nil))
1782 (setq plist (cdr plist)))
1783 (if (and (null src) alt YaTeX-create-file-prefix-g)
1784 (setq cmd alt
1785 src (concat file (cdr (assoc alt plist0))))))
1786 (if src ;if processor and src file found
1787 (let ((default-directory basedir))
1788 (cond
1789 ((stringp cmd)
1790 (let ((buf (concat "* " cmd " " src " *")))
1791 (YaTeX-system (concat cmd " " src) buf)
1792 t))
1793 ((eq t cmd)
1794 (let ((parent buffer-file-name))
1795 (funcall
1796 (cond
1797 (other 'YaTeX-switch-to-buffer-other-window)
1798 ((get-file-buffer src) 'goto-buffer-window)
1799 (t 'YaTeX-switch-to-buffer))
1800 src)
1801 (or (YaTeX-get-builtin "!")
1802 YaTeX-parent-file
1803 (setq YaTeX-parent-file parent))
1804 t))
1805 ((symbolp cmd)
1806 (cond
1807 ((symbol-function cmd)
1808 (funcall cmd src other)))
1809 t)))))))))
1811 (defun YaTeX-on-section-command-p (command)
1812 "Check if point is on the LaTeX command: COMMAND(regexp).
1813 Return nil if point is not on it. Otherwise return the
1814 number of argument position.
1815 Section command name is stored in match-data #1.
1816 Parsing information is stored to plist.
1817 Macros name stored to propname 'command.
1818 Macro's argument number stored to propname 'argc."
1819 (let ((p (point)) md (parg 0) (argc 1) word (grouping 0) (i 0)
1820 (ec+command (concat YaTeX-ec-regexp "\\(" command "\\)")))
1821 (setplist 'YaTeX-on-section-command-p nil)
1822 (while (setq i (string-match "\\\\(" command i))
1823 (setq grouping (1+ grouping) i (+ i 2)))
1824 (save-excursion
1825 (if (looking-at ec+command) nil
1826 (catch 'found ;caught value has no meaning
1827 ;;(1) looking at current position
1828 (and (looking-at command)
1829 (save-excursion
1830 (while (and (not (bobp)) (looking-at command))
1831 (forward-char -1))
1832 (looking-at ec+command))
1833 (goto-char (match-beginning 0))
1834 (throw 'found t))
1835 ;;(2) search command directly
1836 (skip-chars-forward "^{}[]")
1837 (and (YaTeX-re-search-active-backward
1838 ec+command
1839 YaTeX-comment-prefix nil t)
1840 (>= p (match-beginning 0))
1841 (throw 'found (goto-char (match-beginning 0))))
1842 ;;(3) search token
1843 (goto-char p)
1844 (while t
1845 (if (bobp) (throw 'found nil))
1846 (cond
1847 ((looking-at YaTeX-ec-regexp) (throw 'found t))
1848 ((looking-at "[[{]") nil)
1849 ((looking-at "[]}]")(condition-case nil (up-list -1) (error nil)))
1850 (t (skip-chars-backward " \t\r\n")))
1851 (skip-chars-backward (concat "^ \t\r\n{}[]" YaTeX-ec-regexp))
1852 (or (bobp) (forward-char -1)))))
1853 (if (and
1854 (looking-at (concat ec+command
1855 "\\(\\(\\[[^]]+\\]\\|([0-9,]+)\\)*\\)" ;optional arg
1856 ;"[ \t\n\r]*{[^}]+}")) ;arg braces
1857 "[ \t\n\r]*{[^}]*}")) ;arg braces
1858 (not (YaTeX-lookup-table
1859 (setq word (YaTeX-match-string 1)) 'singlecmd)))
1860 (progn
1861 (setq md (match-data))
1862 (skip-chars-forward "^{")
1863 (if (<= (point) p) (setq parg (1+ parg)))
1864 (setq argc
1865 (or (car (cdr (YaTeX-lookup-table word 'section)))
1866 argc))
1867 (put 'YaTeX-on-section-command-p 'argc argc)
1868 (put 'YaTeX-on-section-command-p 'command argc)
1869 (while (and (>= (setq argc (1- argc)) 0)
1870 (progn (skip-chars-forward " \t\n\r")
1871 (looking-at "{")))
1872 (forward-list 1)
1873 (if (<= (point) p) (setq parg (1+ parg))))
1874 (store-match-data md)
1875 (setq i (+ 2 grouping))
1876 (if (and (match-beginning i)
1877 (>= p (match-beginning i)) (< p (match-end i)))
1878 -1 ;return -1 if point is on optional arg
1879 (if (< p (point)) parg))
1880 )))))
1882 (defun YaTeX-on-maketitle-p ()
1883 "Check if point is on maketitle type commands.
1884 Call this function after YaTeX-on-section-command-p."
1885 (let ((p (point)))
1886 (save-excursion
1887 (or (= (char-after (point)) ?\\ )
1888 (progn
1889 (skip-chars-backward
1890 (concat "^" YaTeX-ec-regexp) (point-beginning-of-line))
1891 (or (bobp) (bolp) (backward-char 1))))
1892 (and (looking-at (concat YaTeX-ec-regexp YaTeX-TeX-token-regexp))
1893 (<= (match-beginning 0) p)
1894 (> (match-end 0) p)))))
1896 (defun YaTeX-on-begin-end-p ()
1897 (save-excursion
1898 (if (and (boundp 'in-leftright-p) in-leftright-p)
1899 ;; Dirty workaround for YaTeX-goto-corresponding-leftright 2003/3/28
1900 (let ((md (match-data))) ; for safety
1901 (if (looking-at YaTeX-ec-regexp)
1902 nil ; stay here
1903 (cond
1904 ((looking-at "\\w") (skip-chars-backward "A-Za-z"))
1905 ((looking-at "\\.()\\[\\]|") (forward-char -1)))
1906 (if (equal (char-after (1- (point)))
1907 (string-to-char YaTeX-ec))
1908 (forward-char -1))))
1909 ;(beginning-of-line)
1910 (if (equal (char-after (point)) ?\\) nil ;stay here
1911 (skip-chars-backward "^\n\\\\")
1912 (or (bolp) (forward-char -1))))
1913 (re-search-forward
1914 ;;"\\\\begin{\\([^}]+\\)}\\|\\\\end{\\([^}]+\\)}"
1915 (concat
1916 (YaTeX-replace-format-args
1917 (regexp-quote YaTeX-struct-begin)
1918 (concat "\\(" YaTeX-struct-name-regexp "\\)") "" "" "")
1919 "\\|"
1920 (YaTeX-replace-format-args
1921 (regexp-quote YaTeX-struct-end)
1922 (concat "\\(" YaTeX-struct-name-regexp "\\)") "" "" "")
1923 "\\|\\("
1924 YaTeX-ec-regexp ;;"[][()]\\)"
1925 "[][]\\)"
1927 (point-end-of-line) t)))
1929 (defun YaTeX-on-includes-p ()
1930 (save-excursion
1931 (beginning-of-line)
1932 (re-search-forward "\\(\\(include[^}]*\\)\\|\\(input\\)\\){[^}]*}"
1933 (point-end-of-line) t)))
1935 (defun YaTeX-on-comment-p (&optional sw)
1936 "Return t if current line is commented out.
1937 Optional argument SW t to treat all `%' lines as comment,
1938 even if on `%#' notation."
1939 (save-excursion
1940 (beginning-of-line)
1941 (skip-chars-forward "\\s ")
1942 (looking-at (if sw "%" "%[^#]"))))
1944 (defun YaTeX-on-BEGIN-END-p ()
1945 (save-excursion
1946 (let ((case-fold-search nil))
1947 (beginning-of-line)
1948 (re-search-forward
1949 "\\(%#BEGIN\\)\\|\\(%#END\\)" (point-end-of-line) t))))
1951 (defun YaTeX-goto-corresponding-* (arg)
1952 "Parse current line and call suitable function."
1953 (interactive "P")
1954 (let (mm)
1955 (cond
1956 ((YaTeX-goto-corresponding-label arg))
1957 ((YaTeX-goto-corresponding-environment))
1958 ((YaTeX-goto-corresponding-file-processor arg))
1959 ((YaTeX-goto-corresponding-file arg))
1960 ((YaTeX-goto-corresponding-BEGIN-END))
1961 ((and (setq mm (YaTeX-in-math-mode-p))
1962 (YaTeX-goto-corresponding-leftright)))
1963 ((and ;;mm YaTeX-use-AMS-LaTeX
1964 (YaTeX-goto-corresponding-paren)))
1965 ;;((and (string-match
1966 ;; YaTeX-equation-env-regexp ;to delay loading
1967 ;; (or (YaTeX-inner-environment t) "document"))
1968 ;; (YaTeX-goto-corresponding-leftright)))
1969 (t (message "I don't know where to go.")))))
1971 (defun YaTeX-goto-corresponding-*-other-window (arg)
1972 "Parse current line and call suitable function."
1973 (interactive "P")
1974 (cond
1975 ((YaTeX-goto-corresponding-label arg t))
1976 ;;((YaTeX-goto-corresponding-environment))
1977 ((YaTeX-goto-corresponding-file t))
1978 ;;((YaTeX-goto-corresponding-BEGIN-END))
1979 (t (message "I don't know where to go."))))
1981 (defun YaTeX-comment-region (alt-prefix)
1982 "Comment out region by '%'.
1983 If you call this function on the 'begin{}' or 'end{}' line,
1984 it comments out whole environment"
1985 (interactive "P")
1986 (if (not (YaTeX-on-begin-end-p))
1987 (comment-out-region
1988 (if alt-prefix
1989 (read-string "Insert prefix: ")
1990 YaTeX-comment-prefix))
1991 (YaTeX-comment-uncomment-env 'comment-out-region)))
1993 (defun YaTeX-uncomment-region (alt-prefix)
1994 "Uncomment out region by '%'."
1995 (interactive "P")
1996 (if (not (YaTeX-on-begin-end-p))
1997 (uncomment-out-region
1998 (if alt-prefix (read-string "Remove prefix: ")
1999 YaTeX-comment-prefix)
2000 (region-beginning) (region-end) YaTeX-uncomment-once)
2001 (YaTeX-comment-uncomment-env 'uncomment-out-region)))
2003 (defun YaTeX-comment-uncomment-env (func)
2004 "Comment or uncomment out one LaTeX environment switching function by FUNC."
2005 (let (beg (p (point)))
2006 (save-excursion
2007 (beginning-of-line)
2008 (setq beg (point))
2009 (YaTeX-goto-corresponding-environment)
2010 (beginning-of-line)
2011 (if (> p (point)) (setq beg (1+ beg)) (forward-char 1))
2012 (funcall func YaTeX-comment-prefix beg (point) YaTeX-uncomment-once)))
2013 (message "%sommented out current environment."
2014 (if (eq func 'comment-out-region) "C" "Un-c")))
2016 (defun YaTeX-comment-paragraph ()
2017 "Comment out current paragraph."
2018 (interactive)
2019 (save-excursion
2020 (cond
2021 ((YaTeX-on-begin-end-p)
2022 (beginning-of-line)
2023 (insert YaTeX-comment-prefix)
2024 (YaTeX-goto-corresponding-environment)
2025 (beginning-of-line)
2026 (insert YaTeX-comment-prefix))
2027 ((YaTeX-on-comment-p)
2028 (message "Already commented out."))
2029 (t
2030 (mark-paragraph)
2031 (if (looking-at paragraph-separate) (forward-line 1))
2032 (comment-out-region "%")))))
2034 (defun YaTeX-uncomment-paragraph ()
2035 "Uncomment current paragraph."
2036 (interactive)
2037 (save-excursion
2038 (if (YaTeX-on-begin-end-p)
2039 (let ((p (point-marker)))
2040 (YaTeX-goto-corresponding-environment)
2041 (YaTeX-remove-prefix YaTeX-comment-prefix YaTeX-uncomment-once)
2042 (goto-char p)
2043 (YaTeX-remove-prefix YaTeX-comment-prefix YaTeX-uncomment-once)
2044 (set-marker p nil))
2045 (if (YaTeX-on-comment-p)
2046 (let*((fill-prefix "")
2047 ;;append `^%' to head of paragraph delimiter.
2048 (paragraph-start
2049 (concat
2050 "^$\\|^%\\(" YaTeX-paragraph-separate "\\)"))
2051 (paragraph-separate paragraph-start))
2052 (mark-paragraph)
2053 (if (not (bobp)) (forward-line 1))
2054 (uncomment-out-region "%" nil nil YaTeX-uncomment-once))
2055 (message "This line is not a comment line.")))))
2057 (defun YaTeX-remove-prefix (prefix &optional once)
2058 "Remove prefix on current line as far as prefix detected. But
2059 optional argument ONCE makes deletion once."
2060 (interactive "sPrefix:")
2061 (beginning-of-line)
2062 (while (re-search-forward (concat "^" prefix) (point-end-of-line) t)
2063 (replace-match "")
2064 (if once (end-of-line))))
2066 (defun YaTeX-kill-some-pairs (predicate gofunc kill-contents)
2067 "Kill some matching pair.
2068 This function assumes that pairs occupy whole of each line where they resid."
2069 (if (not (funcall predicate)) nil
2070 (let ((b1 (match-beginning 0)) (e1 (match-end 0))
2071 b2 e2)
2072 (save-excursion
2073 (funcall gofunc)
2074 (funcall predicate) ;get match data
2075 (if (< (point) e1) ;if currently on begin-line
2076 (progn
2077 (setq b2 b1 e2 e1
2078 b1 (match-beginning 0) e1 (match-end 0))
2079 (goto-char e2)) ;goto end-line's end
2080 (setq b2 (match-beginning 0)
2081 e2 (match-end 0))
2082 (goto-char e2)) ;now e2 has surely end-line's end
2083 (skip-chars-forward " \t")
2084 (and (eolp)
2085 (not (eobp))
2086 (setq e2 (1+ (point))))
2087 (if (not kill-contents)
2088 (kill-region
2089 (progn
2090 (goto-char b2)
2091 (skip-chars-backward " \t%")
2092 (if (bolp) (point) b2))
2093 e2))
2094 (goto-char b1)
2095 (skip-chars-backward " \t%")
2096 (if (not kill-contents)
2097 (progn
2098 (kill-append
2099 (buffer-substring
2100 (setq b1 (if (bolp) (point) b1))
2101 (setq e1
2102 (progn
2103 (goto-char e1)
2104 (while (looking-at "{\\| \t")
2105 (forward-list 1))
2106 (skip-chars-forward " \t")
2107 (if (and (eolp) (not (eobp)))
2108 (1+ (point))
2109 (point)))))
2110 t)
2111 (delete-region b1 e1))
2112 (kill-region
2113 (if (bolp) (point) b1)
2114 e2)))
2115 t)))
2117 (defun YaTeX-kill-section-command (point kill-all)
2118 "Kill section-type command at POINT leaving its last argument.
2119 Non-nil for the second argument kill its last argument too."
2120 (let ((cmd (get 'YaTeX-on-section-command-p 'command))
2121 (argc (get 'YaTeX-on-section-command-p 'argc))
2122 beg (end (make-marker)))
2123 (save-excursion
2124 (goto-char point)
2125 (or (looking-at YaTeX-ec-regexp)
2126 (progn
2127 (skip-chars-backward (concat "^" YaTeX-ec-regexp))
2128 (forward-char -1)))
2129 (setq beg (point))
2130 (skip-chars-forward "^{")
2131 (while (> (setq argc (1- argc)) 0)
2132 (skip-chars-forward "^{")
2133 (forward-list 1))
2134 (kill-region beg (point))
2135 (forward-list 1)
2136 (set-marker end (point))
2137 (if kill-all
2138 (progn
2139 (kill-append (buffer-substring beg end) nil)
2140 (delete-region beg end))
2141 (goto-char beg)
2142 (kill-append
2143 (buffer-substring
2144 (point) (progn (skip-chars-forward "^{" end) (1+ (point))))
2145 nil)
2146 (delete-region beg (1+ (point)))
2147 (goto-char end)
2148 (set-marker end nil)
2149 (kill-append (buffer-substring (point) (1- (point))) nil)
2150 (delete-backward-char 1)))))
2152 (defun YaTeX-kill-paren (kill-contents)
2153 "Kill parentheses leaving its contents.
2154 But kill its contents if the argument KILL-CONTENTS is non-nil."
2155 (interactive "P")
2156 (let (p bsl (backslash-syntax (char-to-string (char-syntax ?\\)))
2157 (md (match-data)))
2158 (unwind-protect
2159 (save-excursion
2160 (modify-syntax-entry ?\\ " ")
2161 (if (looking-at "\\s(\\|\\(\\s)\\)")
2162 (progn
2163 (if (match-beginning 1)
2164 (up-list -1))
2165 (if (and (> (point) (point-min))
2166 (= (char-after (1- (point))) ?\\ ))
2167 (setq p (1- (point)) bsl t)
2168 (setq p (point)))
2169 (forward-list 1)
2170 ;(YaTeX-goto-open-paren t)
2171 (if kill-contents (delete-region p (point))
2172 (backward-delete-char 1)
2173 (cond
2174 ((save-excursion
2175 (forward-char -2)
2176 (looking-at (concat YaTeX-ec-regexp "/")))
2177 (backward-delete-char 2))
2178 ((= (char-after (1- (point))) ?\\)
2179 (backward-delete-char 1)))
2180 (goto-char p)
2181 (if (looking-at
2182 (concat "{" YaTeX-ec-regexp
2183 YaTeX-command-token-regexp "+"
2184 "\\s +"))
2185 (delete-region (point) (match-end 0))
2186 (delete-char 1)
2187 (if bsl (delete-char 1))))
2188 t)))
2189 (modify-syntax-entry ?\\ backslash-syntax)
2190 (store-match-data md))))
2192 (defvar YaTeX-read-environment-history nil "Holds history of environments.")
2193 (put 'YaTeX-read-environment-history 'no-default t)
2194 (defun YaTeX-read-environment (prompt &optional predicate must-match initial)
2195 "Read a LaTeX environment name with completion."
2196 (YaTeX-sync-local-table 'tmp-env-table)
2197 (completing-read-with-history
2198 prompt
2199 (append tmp-env-table user-env-table env-table)
2200 predicate must-match initial
2201 'YaTeX-read-environment-history))
2203 (defvar YaTeX-read-section-history nil "Holds history of section-types.")
2204 (put 'YaTeX-read-section-history 'no-default t)
2205 (defun YaTeX-read-section (prompt &optional predicate initial)
2206 "Read a LaTeX section-type command with completion."
2207 (YaTeX-sync-local-table 'tmp-section-table)
2208 (let ((minibuffer-completion-table
2209 (append tmp-section-table user-section-table section-table)))
2210 (read-from-minibuffer-with-history
2211 prompt initial YaTeX-section-completion-map nil
2212 'YaTeX-read-section-history)))
2214 (defun YaTeX-read-section-with-overview ()
2215 "Read sectioning command with overview.
2216 This function refers a local variable `source-window' in YaTeX-make-section,
2217 because this function is called with no argument."
2218 (interactive)
2219 (require 'yatexsec) ;some case needs this
2220 (if (> (minibuffer-depth) 1)
2221 (error "Too many minibuffer levels for overview."))
2222 (let ((sw (selected-window))
2223 (minibuffer-max-depth nil) ; for XEmacs20
2224 (enable-recursive-minibuffers t) sect)
2225 (unwind-protect
2226 (progn
2227 (select-window source-window)
2228 (setq sect (YaTeX-read-section-in-minibuffer
2229 "Sectioning(Up=C-p, Down=C-n, Help=?): "
2230 YaTeX-sectioning-level (YaTeX-section-overview))))
2231 (select-window sw))
2232 (YaTeX-minibuffer-erase)
2233 (insert sect)
2234 (exit-minibuffer)))
2236 (defvar YaTeX-read-fontsize-history nil "Holds history of font designator.")
2237 (put 'YaTeX-read-fontsize-history 'no-default t)
2238 (defun YaTeX-read-fontsize (prompt &optional predicate must-match initial)
2239 "Read a LaTeX font changing command with completion."
2240 (YaTeX-sync-local-table 'tmp-fontsize-table)
2241 (completing-read-with-history
2242 prompt (append tmp-fontsize-table user-fontsize-table fontsize-table)
2243 predicate must-match initial 'YaTeX-read-fontsize-history))
2245 (defun YaTeX-change-environment ()
2246 "Change the name of environment."
2247 (interactive)
2248 (if (not (YaTeX-on-begin-end-p)) nil
2249 (save-excursion
2250 (let (p env (m1 (match-beginning 1)) (m2 (match-beginning 2)))
2251 (setq env (if m1 (buffer-substring m1 (match-end 1))
2252 (buffer-substring m2 (match-end 2))))
2253 (goto-char (match-beginning 0))
2254 (set-mark-command nil)
2255 (YaTeX-goto-corresponding-environment)
2256 (setq newenv (YaTeX-read-environment
2257 (format "Change environment `%s' to: " env)))
2258 (cond
2259 ((string= newenv "") (message "Change environment cancelled."))
2260 ((string= newenv env) (message "No need to change."))
2261 (t
2262 (search-forward (concat "{" env) (point-end-of-line) t)
2263 (replace-match (concat "{" newenv) t)
2264 (exchange-point-and-mark)
2265 (search-forward (concat "{" env) (point-end-of-line) t)
2266 (replace-match (concat "{" newenv) t)))
2267 t))))
2269 (defun YaTeX-change-section ()
2270 "Change section-type command."
2271 (interactive)
2272 (let*((where (YaTeX-on-section-command-p YaTeX-command-token-regexp))
2273 (p (point)) (cmd (YaTeX-match-string 1))
2274 (beg (make-marker)) (end (make-marker)) old new)
2275 (if (null where) nil
2276 (unwind-protect
2277 (let ((source-window (selected-window)))
2278 (cond
2279 ((equal where 0);;if point is on section command
2280 (set-marker beg (match-beginning 1))
2281 (set-marker end (match-end 1))
2282 (goto-char beg) ;beginning of the command
2283 (setq new (YaTeX-read-section
2284 (format "Change `%s' to: " cmd) nil)))
2286 ((= where -1);;if point is on a optional parameter
2287 (set-marker beg (match-beginning 2))
2288 (skip-chars-forward "^{")
2289 (set-marker end (point))
2290 (goto-char p)
2291 (setq new
2292 (if (fboundp (intern-soft (concat YaTeX-addin-prefix cmd)))
2293 (YaTeX-addin cmd)
2294 (concat "["
2295 (read-string (format "Change `%s' to: "
2296 (buffer-substring
2297 (1+ beg) (1- end))))
2298 "]"))))
2300 ((> where 0);;if point is in arguments' braces
2301 (or (looking-at "{")
2302 (progn (skip-chars-backward "^{") (forward-char -1)))
2303 (set-marker beg (1+ (point)))
2304 (forward-list 1)
2305 (forward-char -1)
2306 (set-marker end (point))
2307 (setq old (buffer-substring beg end))
2308 (goto-char p)
2309 (if (> (length old) 40)
2310 (setq old (concat (substring old 0 12) "..."
2311 (substring old -12))))
2312 (setq new
2313 (if (intern-soft (concat "YaTeX::" cmd))
2314 (funcall (intern-soft (concat "YaTeX::" cmd)) where)
2315 (read-string (format "Change `%s' to: " old)))))
2316 ) ;cond
2317 (delete-region beg end)
2318 (goto-char beg)
2319 (insert-before-markers new))
2320 (set-marker beg nil)
2321 (set-marker end nil))
2322 ;;(goto-char (marker-position p))
2323 new)))
2325 (defun YaTeX-change-fontsize ()
2326 "Change large-type command."
2327 (let ((lt (append tmp-fontsize-table user-fontsize-table fontsize-table))
2328 (p (point)) large old new beg end)
2329 ;;(and (looking-at "}") (up-list -1))
2330 ;;(and (looking-at "{") (forward-char 1))
2331 ;;Is above convenient?
2332 (save-excursion
2333 (or (looking-at YaTeX-ec-regexp)
2334 (progn
2335 (skip-chars-backward (concat "^" YaTeX-ec-regexp))
2336 (forward-char -1)))
2337 (cond
2338 ((and
2339 (looking-at
2340 (concat YaTeX-ec-regexp "\\(" YaTeX-TeX-token-regexp "\\)"))
2341 (< p (match-end 0))
2342 (assoc (setq old (YaTeX-match-string 1)) lt))
2343 (goto-char p)
2344 (setq beg (match-beginning 1) end (match-end 1) ;save match position
2345 new (completing-read
2346 (format "Change font/size `%s' to : " old) lt))
2347 (delete-region beg end)
2348 (goto-char beg)
2349 (insert-before-markers new)
2350 new)
2351 (t nil)
2352 ))))
2354 (defun YaTeX-change-math-image ()
2355 "Change with image completion."
2356 (let (maketitle memberp beg end)
2357 (if (and (YaTeX-on-maketitle-p)
2358 (progn
2359 (setq maketitle (substring (YaTeX-match-string 0) 1))
2360 (setq memberp (YaTeX-math-member-p maketitle))))
2361 (let ((last-command-char (string-to-char (car memberp))))
2362 (setq beg (match-beginning 0) end (match-end 0))
2363 (delete-region beg end)
2364 (YaTeX-math-insert-sequence t (cdr memberp))))))
2366 (defun YaTeX-kill-* (&optional arg)
2367 "Parse current line and call suitable function.
2368 Non-nil for ARG kills its contents too."
2369 (interactive "P")
2370 (cond
2371 ((YaTeX-kill-some-pairs 'YaTeX-on-begin-end-p
2372 'YaTeX-goto-corresponding-environment arg))
2373 ((YaTeX-kill-some-pairs 'YaTeX-on-BEGIN-END-p
2374 'YaTeX-goto-corresponding-BEGIN-END arg))
2375 ((YaTeX-on-section-command-p YaTeX-command-token-regexp);on any command
2376 (YaTeX-kill-section-command (match-beginning 0) arg))
2377 ((YaTeX-kill-paren arg))
2378 (t (message "I don't know what to kill."))))
2380 (defun YaTeX-change-* ()
2381 "Parse current line and call suitable function."
2382 (interactive)
2383 (cond
2384 ((YaTeX-change-parentheses))
2385 ((YaTeX-change-environment))
2386 ((YaTeX-change-section))
2387 ((YaTeX-change-fontsize))
2388 ((YaTeX-change-math-image))
2389 (t (message "I don't know what to change."))))
2391 ;;;
2392 ;Check availability of add-in functions
2393 ;;;
2394 (cond
2395 ((featurep 'yatexadd) nil) ;Already provided.
2396 ((progn (load "yatexadd" t) (featurep 'yatexadd)) nil)
2397 (t (message "YaTeX add-in functions not supplied.")))
2399 (defun YaTeX-addin (name)
2400 "Check availability of addin function and call it if exists."
2401 (if (and (not (get 'YaTeX-generate 'disabled))
2402 (intern-soft (concat YaTeX-addin-prefix name))
2403 (fboundp (intern-soft (concat YaTeX-addin-prefix name))))
2404 (let ((s (funcall (intern (concat YaTeX-addin-prefix name)))))
2405 (if (stringp s) s ""))
2406 "")) ;Add in function is not bound.
2409 (defun YaTeX-on-item-p (&optional point)
2410 "Return t if POINT (default is (point)) is on \\item."
2411 (let ((p (or point (point))))
2412 (save-excursion
2413 (goto-char p)
2414 (end-of-line)
2415 (setq p (point))
2416 (re-search-backward YaTeX-paragraph-delimiter nil t)
2417 (re-search-forward YaTeX-item-regexp p t))))
2419 (defun YaTeX-in-verb-p (&optional point)
2420 "Check if POINT is in verb or verb*. Default of POINT is (point)."
2421 (setq point (or point (point)))
2422 (save-excursion
2423 (let ((md (match-data)))
2424 (goto-char point)
2425 (unwind-protect
2426 (if (not (re-search-backward
2427 (concat YaTeX-ec-regexp
2428 "\\(" YaTeX-verb-regexp "\\)"
2429 "\\([^-A-Za-z_*]\\)")
2430 (point-beginning-of-line) t))
2431 nil
2432 (goto-char (match-end 2))
2433 (skip-chars-forward
2434 (concat "^" (buffer-substring (match-beginning 2) (match-end 2))))
2435 (and (< (match-beginning 2) point) (< (1- point) (point))))
2436 (store-match-data md)))))
2438 (defun YaTeX-literal-p (&optional point)
2439 "Check if POINT is in verb or verb* or verbatime environment family.
2440 Default of POINT is (point)."
2441 (let ((md (match-data)))
2442 (unwind-protect
2443 (cond
2444 ((equal YaTeX-ec "\\") ;maybe LaTeX
2445 (save-excursion
2446 (and point (goto-char point))
2447 (or (YaTeX-in-verb-p (point))
2448 (and (not (looking-at "\\\\end{verb"))
2449 (YaTeX-quick-in-environment-p
2450 YaTeX-verbatim-environments))))))
2451 (store-match-data md))))
2453 (defun YaTeX-in-environment-p (env)
2454 "Return if current LaTeX environment is ENV.
2455 ENV is given in the form of environment's name or its list."
2456 (let ((md (match-data)) (nest 0) p envrx)
2457 (cond
2458 ((atom env)
2459 (setq envrx
2460 (concat "\\("
2461 (regexp-quote
2462 (YaTeX-replace-format-args
2463 YaTeX-struct-begin env "" ""))
2464 "\\)\\|\\("
2465 (regexp-quote
2466 (YaTeX-replace-format-args
2467 YaTeX-struct-end env "" ""))
2468 "\\)"))
2469 (save-excursion
2470 (setq p (catch 'open
2471 (while (YaTeX-re-search-active-backward
2472 envrx YaTeX-comment-prefix nil t)
2473 (if (match-beginning 2)
2474 (setq nest (1+ nest))
2475 (setq nest (1- nest)))
2476 (if (< nest 0)
2477 (throw 'open (cons env (match-beginning 0)))))))))
2478 ((listp env)
2479 (setq p
2480 (or (YaTeX-in-environment-p (car env))
2481 (and (cdr env) (YaTeX-in-environment-p (cdr env)))))))
2482 (store-match-data md)
2483 p;(or p (YaTeX-in-verb-p (match-beginning 0)))
2484 ))
2486 (defun YaTeX-quick-in-environment-p (env)
2487 "Check quickly but unsure if current environment is ENV.
2488 ENV is given in the form of environment's name or its list.
2489 This function returns correct result only if ENV is NOT nested."
2490 (save-excursion
2491 (let ((md (match-data)) m0 (p (point)) rc clfound)
2492 (cond
2493 ((listp env)
2494 (or (YaTeX-quick-in-environment-p (car env))
2495 (and (cdr env) (YaTeX-quick-in-environment-p (cdr env)))))
2496 (t
2497 (unwind-protect
2498 (if (prog1
2499 (YaTeX-search-active-backward
2500 (YaTeX-replace-format-args YaTeX-struct-begin env "" "")
2501 YaTeX-comment-prefix nil t)
2502 (setq m0 (match-beginning 0)))
2503 (if (YaTeX-search-active-forward
2504 (YaTeX-replace-format-args
2505 YaTeX-struct-end env)
2506 YaTeX-comment-prefix p t nil)
2507 nil ;if \end{env} found, return nil
2508 (cons env m0))) ;else, return meaningful values
2509 (store-match-data md)))))))
2511 ;; Filling \item
2512 (defun YaTeX-remove-trailing-comment (start end)
2513 "Remove trailing comment from START to end."
2514 (save-excursion
2515 (let ((trcom (concat YaTeX-comment-prefix "$")))
2516 (goto-char start)
2517 (while (re-search-forward trcom end t)
2518 (if (/= (char-after (1- (match-beginning 0))) ?\\ )
2519 (replace-match "\\1"))))))
2521 (defvar YaTeX-itemize-withlabel-max-indent-depth 8)
2522 (defun YaTeX-get-item-info (&optional recent thisenv)
2523 "Return the list of the beginning of \\item and column of its item.
2524 If it seems to be outside of itemizing environment, just return nil.
2525 Non-nil for optional argument RECENT refers recent \\item.
2526 Optional second argument THISENV omits calling YaTeX-inner-environment."
2527 (save-excursion
2528 (let* ((p (point)) env e0 c cc md
2529 (bndry (and (setq env (or thisenv (YaTeX-inner-environment t)))
2530 (get 'YaTeX-inner-environment 'point))))
2531 (end-of-line)
2532 (if (if recent
2533 (catch 'found
2534 (while (YaTeX-re-search-active-backward
2535 YaTeX-item-regexp YaTeX-comment-prefix bndry t)
2536 (setq md (match-data))
2537 (YaTeX-inner-environment t)
2538 (store-match-data md)
2539 (if (= bndry (get 'YaTeX-inner-environment 'point))
2540 (throw 'found t))))
2541 (goto-char bndry)
2542 (YaTeX-re-search-active-forward
2543 YaTeX-item-regexp YaTeX-comment-prefix p t))
2544 (progn
2545 (goto-char (match-end 0))
2546 ;(setq c (current-column))
2547 (if (string-match "desc" env)
2548 (setq c 6)
2549 (setq cc (current-column))
2550 (if (equal (following-char) ?\[) (forward-list 1))
2551 (if (< (- (current-column) cc)
2552 YaTeX-itemize-withlabel-max-indent-depth)
2553 (setq c 0)
2554 (move-to-column cc)
2555 (setq c YaTeX-itemize-withlabel-max-indent-depth)))
2556 (skip-chars-forward " \t" (point-end-of-line))
2557 (list (point-beginning-of-line) (+ c (current-column))))))))
2559 (defun YaTeX-fill-item ()
2560 "Fill item in itemize environment."
2561 (interactive)
2562 (save-excursion
2563 (let* ((p (point))
2564 (item-term (concat
2565 "\\(^[ \t]*$\\)\\|" YaTeX-item-regexp "\\|\\("
2566 YaTeX-ec-regexp "\\(begin\\|end\\)\\)"))
2567 ;;This value depends on LaTeX.
2568 fill-prefix start col
2569 (info (YaTeX-get-item-info t)))
2570 (if (null info) nil ;not on \item, do nothing
2571 (setq start (car info)
2572 col (car (cdr info)))
2573 (save-excursion
2574 (if (re-search-backward "^\\s *$" start t)
2575 ;;if separated from \item line, isolate this block
2576 (progn
2577 (setq start (1+ (match-end 0)))
2578 (goto-char start)
2579 (skip-chars-forward " \t")
2580 (delete-region (point) start) ;is this your favor???
2581 (indent-to col))))
2582 (beginning-of-line)
2583 (if (<= (save-excursion
2584 (re-search-forward
2585 (concat "\\\\end{\\|\\\\begin{\\|^[ \t]*$") nil t)
2586 (match-beginning 0))
2587 p)
2588 (progn (message "Not on itemize.") nil)
2589 (end-of-line)
2590 (newline)
2591 (indent-to col)
2592 (setq fill-prefix
2593 (buffer-substring (point-beginning-of-line)(point)))
2594 (beginning-of-line)
2595 (delete-region (point) (progn (forward-line 1) (point)))
2596 (re-search-forward item-term nil 1)
2597 (YaTeX-remove-trailing-comment start (point))
2598 (beginning-of-line)
2599 (push-mark (point) t)
2600 (goto-char start)
2601 (forward-line 1)
2602 (while (< (point) (mark))
2603 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
2604 (forward-line 1))
2605 (fill-region-as-paragraph start (mark))
2606 (if NTT-jTeX
2607 (while (progn(forward-line -1)(end-of-line) (> (point) start))
2608 (insert ?%)))
2609 (pop-mark))))))
2611 (defun YaTeX-fill-paragraph (arg)
2612 "YaTeX adjustment function for fill-paragraph.
2613 *Protect \\verb from unexpected broken up."
2614 (interactive "P")
2615 (cond
2616 ((not (eq major-mode 'yatex-mode)) (fill-paragraph arg))
2617 ((YaTeX-quick-in-environment-p YaTeX-fill-inhibit-environments) nil)
2618 ((YaTeX-in-math-mode-p) nil)
2619 (t
2620 (save-excursion
2621 (let*((verbrex (concat YaTeX-ec-regexp
2622 "\\(" YaTeX-verb-regexp "\\)" ;match#1
2623 "\\(.\\).*\\(\\2\\)")) ;match #2 and #3
2624 (tilderex (concat "\\("
2625 YaTeX-kanji-regexp "~"
2626 "\\)" YaTeX-ec-regexp
2627 "\\|\\("
2628 "~" YaTeX-kanji-regexp
2629 "\\)"))
2630 (p (point)) ii end poslist spacelist lenlist b e n
2631 (fill-prefix fill-prefix)
2632 (inenv (or (YaTeX-inner-environment t) "document"))
2633 (border (get 'YaTeX-inner-environment 'point)))
2634 (cond
2635 ((save-excursion (beginning-of-line) ;if point is on the first
2636 (setq end (point)) ;non-whitespace char
2637 (skip-chars-forward " \t")
2638 (equal (point) p))
2639 (setq fill-prefix (buffer-substring p end)))
2640 ((and ;;(not YaTeX-emacs-19)
2641 (string-match YaTeX-itemizing-env-regexp inenv)
2642 (setq ii (YaTeX-get-item-info)))
2643 (save-excursion
2644 (beginning-of-line)
2645 (indent-to-column (car (cdr ii)))
2646 (setq fill-prefix
2647 (buffer-substring (point) (point-beginning-of-line)))
2648 (delete-region (point) (progn (beginning-of-line) (point))))))
2649 (cond
2650 ((string-match "tabular" inenv)
2651 (let ((b (point-beginning-of-line))
2652 (e (point-end-of-line)))
2653 (if (re-search-backward
2654 "&\\|\\\\\\\\\\|\\\\\\(begin\\|end\\){" border t)
2655 (setq b (if (match-beginning 1)
2656 (progn (forward-line 1) (point))
2657 (point-beginning-of-line))))
2658 (goto-char p)
2659 (if (re-search-forward
2660 "&\\|\\\\\\\\\\|\\\\\\(end\\|begin\\){" nil t)
2661 (setq e (if (match-beginning 1)
2662 (progn (forward-line -1)
2663 (point-end-of-line))
2664 (match-beginning 0))))
2665 (set-mark e)
2666 (goto-char b)))
2667 (t
2668 (mark-paragraph)))
2669 (save-restriction
2670 (narrow-to-region (region-beginning) (region-end))
2671 (YaTeX-remove-trailing-comment (point-min) (point-max))
2672 ;; First, replace spaces in verb to _ temporarily.
2673 (goto-char (point-min))
2674 (while (YaTeX-re-search-active-forward
2675 verbrex YaTeX-comment-prefix (point-max) t)
2676 (setq end (match-beginning 3))
2677 (goto-char (match-beginning 2))
2678 (while (re-search-forward "\\s " end t)
2679 (setq poslist (cons (make-marker) poslist)
2680 spacelist (cons (preceding-char) spacelist)
2681 lenlist (cons 1 lenlist))
2682 (replace-match "_")
2683 (set-marker (car poslist) (match-beginning 0))))
2684 ;; Second, replace "表~\ref{...}" to "\\\ref{...}"
2685 (goto-char (point-min))
2686 (while (YaTeX-re-search-active-forward
2687 tilderex YaTeX-comment-prefix (point-max) t)
2688 (if (match-beginning 1)
2689 (setq b (match-beginning 1) e (match-end 1) n 1)
2690 (setq b (match-beginning 2) e (match-end 2) n 2))
2691 (setq poslist (cons (make-marker) poslist)
2692 spacelist (cons (YaTeX-match-string n) spacelist)
2693 lenlist (cons 2 lenlist))
2694 (goto-char (match-beginning 0))
2695 (delete-region (point) e)
2696 (insert YaTeX-ec YaTeX-ec) ;set-marker should be here
2697 (set-marker (car poslist) b))
2698 ;;(fill-paragraph arg)
2699 (fill-region-as-paragraph (point-min) (point-max) arg)
2700 (while spacelist
2701 (goto-char (car poslist))
2702 (set-marker (car poslist) nil)
2703 (and (eolp) (skip-chars-forward "\n\t "))
2704 (delete-char (car lenlist))
2705 (insert (car spacelist))
2706 (setq spacelist (cdr spacelist)
2707 poslist (cdr poslist)
2708 lenlist (cdr lenlist)))
2709 (goto-char (point-min))
2710 (forward-word 1)
2711 (beginning-of-line)
2712 (while (re-search-forward "\\\\\\([a-z]*ref\\|cite\\){" nil t)
2713 (if (< (point-end-of-line)
2714 (save-excursion (forward-char -1) (forward-list 1) (point)))
2715 (progn (end-of-line)
2716 (if (save-excursion
2717 (backward-word 1)
2718 (looking-at "[^0-9A-z!-)]"))
2719 (insert YaTeX-comment-prefix)))))
2720 ;; Nonbreak space `~'
2721 (goto-char (point-min))
2722 (while (YaTeX-re-search-active-forward
2723 "~\\(\\s *\\)$" YaTeX-comment-prefix (point-max) t)
2724 (delete-region (match-beginning 1) (match-end 1))
2725 (insert YaTeX-comment-prefix))
2726 (goto-char (point-min))
2727 (if (and NTT-jTeX (looking-at "[ \t]\\|^$"))
2728 (progn
2729 (goto-char (point-min))
2730 (while (not (eobp))
2731 (end-of-line)
2732 (or (bolp)
2733 (save-excursion
2734 (backward-word 1)
2735 (looking-at "[0-9A-z!-)]")) ;is not japanese string
2736 (progn (setq p (point)) (insert YaTeX-comment-prefix)))
2737 (forward-line 1))
2738 (goto-char p)
2739 (if (looking-at "%") (delete-char 1)) ;remove last inserted `%'
2740 ))))))))
2742 (if (fboundp 'YaTeX-saved-indent-new-comment-line) nil
2743 (fset 'YaTeX-saved-indent-new-comment-line
2744 (symbol-function 'indent-new-comment-line))
2745 (fset 'indent-new-comment-line 'YaTeX-indent-new-comment-line))
2747 (defun YaTeX-indent-new-comment-line (&optional soft)
2748 "Tuned `indent-new-comment-line' function for yatex.
2749 See the documentation of `YaTeX-saved-indent-new-comment-line'."
2750 (interactive)
2751 (cond
2752 ((or (not (memq major-mode '(yatex-mode yahtml-mode)))
2753 (string-match
2754 "document"
2755 (or (and (boundp 'inenv) inenv)
2756 (or (YaTeX-inner-environment t) "document"))))
2757 (apply 'YaTeX-saved-indent-new-comment-line (if soft (list soft))))
2758 ; ((and (eq major-mode 'yahtml-mode)
2759 ; (string-match
2760 ; "^[Pp][Rr][Ee]" (yahtml-inner-environment-but "^[Aa]\\b" t)))
2761 ; (yahtml-indent-new-commnet-line))
2762 ((and (eq major-mode 'yatex-mode) ;1997/2/4
2763 (YaTeX-in-math-mode-p)) nil) ;1996/12/30
2764 (t (let (fill-prefix)
2765 (apply 'YaTeX-saved-indent-new-comment-line (if soft (list soft)))))))
2767 (defun YaTeX-fill-* ()
2768 "Fill paragraph according to its condition."
2769 (interactive)
2770 (cond
2771 ((YaTeX-fill-item))
2772 ))
2774 ;; Accent completion
2775 (defun YaTeX-read-accent-char (x)
2776 "Read char in accent braces."
2777 (let ((c (read-char)))
2778 (concat
2779 (if (and (or (= c ?i) (= c ?j))
2780 (not (string-match (regexp-quote x) "cdb")))
2781 "\\" "")
2782 (char-to-string c))))
2784 (defun YaTeX-make-accent ()
2785 "Make accent usage."
2786 (interactive)
2787 (message "1:` 2:' 3:^ 4:\" 5:~ 6:= 7:. u v H t c d b")
2788 (let ((c (read-char))(case-fold-search nil))
2789 (setq c (cond ((and (> c ?0) (< c ?8))
2790 (substring "`'^\"~=." (1- (- c ?0)) (- c ?0)))
2791 ((= c ?h) "H")
2792 (t (char-to-string c))))
2793 (if (not (string-match c "`'^\"~=.uvHtcdb")) nil
2794 (insert "\\" c "{}")
2795 (backward-char 1)
2796 (insert (YaTeX-read-accent-char c))
2797 (if (string= c "t") (insert (YaTeX-read-accent-char c)))
2798 (forward-char 1))))
2800 ;; Indentation
2801 (defun YaTeX-current-indentation ()
2802 "Return the indentation of current environment."
2803 (save-excursion
2804 ;;(beginning-of-line)
2805 (if (YaTeX-beginning-of-environment t)
2806 (goto-char (get 'YaTeX-inner-environment 'point))
2807 (forward-line -1)
2808 (beginning-of-line)
2809 (skip-chars-forward " \t"))
2810 (current-column)))
2812 (defun YaTeX-previous-line-indentation ()
2813 (save-excursion
2814 (forward-line -1)
2815 (skip-chars-forward " \t")
2816 (current-column)))
2818 (defvar YaTeX-noindent-env-regexp "verbatim\\*?\\|alltt"
2819 "*Regexp of environment names that should begin with no indentation.
2820 All verbatime-like environment name should match with.")
2821 (defun YaTeX-indent-line ()
2822 "Indent corrent line referrin current environment."
2823 (interactive)
2824 (let ((indent-relative
2825 (function
2826 (lambda (&optional additional)
2827 (YaTeX-reindent
2828 (+ (YaTeX-current-indentation)
2829 (or additional 0)
2830 YaTeX-environment-indent)))))
2831 sect depth iteminfo (p (point)) pp (peol (point-end-of-line)) begend
2832 ;;inenv below is sometimes defined in YaTeX-indent-new-comment-line
2833 (inenv (or (and (boundp 'inenv) inenv) (YaTeX-inner-environment t))))
2834 ;;(if NTT-jTeX ;;Do you need this section?
2835 ;; (save-excursion
2836 ;; (end-of-line)
2837 ;; (let ((p (point)))
2838 ;; (forward-line -1)
2839 ;; (end-of-line)
2840 ;; (or (= p (point))
2841 ;; (progn (backward-char (length YaTeX-comment-prefix))
2842 ;; (not (looking-at (regexp-quote YaTeX-comment-prefix))))
2843 ;; (progn
2844 ;; (skip-chars-backward YaTeX-comment-prefix)
2845 ;; (kill-line))))))
2846 (or inenv (setq inenv "document")) ;is the default environment
2847 (cond
2848 ((and
2849 (prog1 (YaTeX-on-begin-end-p)
2850 (setq begend (match-beginning 0)))
2851 (or (match-beginning 2) ;if \end
2852 (and (match-beginning 3) ;if \) \]
2853 (= (char-syntax (char-after (1+ (match-beginning 3)))) ?\)))))
2854 (YaTeX-reindent
2855 (save-excursion
2856 (YaTeX-goto-corresponding-environment)
2857 (current-column))))
2858 ;; on the begining of verbatime line, remove all indentation
2859 ((and begend ;; match-beginning 0 of \begin
2860 YaTeX-noindent-env-regexp
2861 (stringp YaTeX-noindent-env-regexp)
2862 (save-excursion
2863 (and ;; if the \begin is the first declaration of this line
2864 (progn (beginning-of-line) (skip-chars-forward " \t")
2865 (= begend (point)))
2866 (progn
2867 (goto-char begend)
2868 (looking-at
2869 (concat YaTeX-ec-regexp
2870 "begin{\\(" YaTeX-noindent-env-regexp "\\)}"))))))
2871 (save-excursion
2872 (goto-char begend)
2873 (delete-region (point) (point-beginning-of-line))))
2874 ((string-match YaTeX-equation-env-regexp inenv)
2875 (YaTeX-indent-line-equation)) ;autoload-ed from yatexenv
2876 (;(YaTeX-in-environment-p '("itemize" "enumerate" "description" "list"))
2877 (string-match YaTeX-itemizing-env-regexp inenv)
2878 ;;(YaTeX-on-item-p) ??
2879 ;;(setq iteminfo (YaTeX-get-item-info t))
2880 (if (save-excursion
2881 (beginning-of-line)
2882 (re-search-forward YaTeX-item-regexp peol t))
2883 (progn
2884 (save-excursion
2885 (goto-char (1+ (match-beginning 0)))
2886 (setq depth
2887 (* YaTeX-environment-indent
2888 (cond
2889 ((looking-at "subsubsub") 3)
2890 ((looking-at "subsub") 2)
2891 ((looking-at "sub") 1)
2892 (t 0)))))
2893 (funcall indent-relative depth))
2894 (YaTeX-reindent (or (car (cdr (YaTeX-get-item-info t inenv)))
2895 (+ (save-excursion
2896 (beginning-of-line)
2897 (YaTeX-current-indentation))
2898 YaTeX-environment-indent))))
2900 ((YaTeX-literal-p) ;verbatims
2901 (tab-to-tab-stop))
2902 ((string-match "\\(tabular\\|array\\)" inenv) ;1.73
2903 (let ((n 1))
2904 (condition-case err
2905 (save-excursion
2906 (beginning-of-line)
2907 (skip-chars-forward "[ \t]")
2908 ;;(if (looking-at "&") (forward-char 1))
2909 (require 'yatexenv)
2910 (setq n (car (YaTeX-array-what-column-internal))))
2911 (error nil))
2912 (YaTeX-reindent
2913 (+ (YaTeX-current-indentation)
2914 YaTeX-environment-indent
2915 (* (1- n) YaTeX-tabular-indentation)))))
2916 ((and inenv (not (equal "document" inenv)))
2917 (funcall indent-relative))
2918 ((YaTeX-on-section-command-p YaTeX-sectioning-regexp)
2919 (require 'yatexsec) ;to know YaTeX-sectioning-level
2920 (setq sect (YaTeX-match-string 1))
2921 (if (string-match "\\*$" sect)
2922 (setq sect (substring sect 0 -1)))
2923 (YaTeX-reindent
2924 (* (max
2925 (1- ;I want chapter to have indentation 0
2926 (or (cdr (assoc sect YaTeX-sectioning-level))
2927 0))
2928 0)
2929 YaTeX-environment-indent)))
2930 ;;Default movement
2931 ((and (bolp) fill-prefix) (insert fill-prefix))
2932 (t (save-excursion
2933 (beginning-of-line)
2934 (if fill-prefix
2935 (progn
2936 (delete-region (point)
2937 (progn (skip-chars-forward " \t")
2938 (point)))
2939 (insert fill-prefix))
2940 (skip-chars-forward " \t")
2941 (if (bobp)
2942 nil
2943 (indent-relative-maybe))))
2944 (skip-chars-forward " \t")))
2945 ;;if current line is \begin, re-indent \end too
2946 (if (and (YaTeX-on-begin-end-p) (match-beginning 1))
2947 (save-excursion
2948 ;;(beginning-of-line)
2949 ;;(search-forward "\\begin")
2950 (goto-char (match-beginning 0))
2951 (setq depth (current-column))
2952 (YaTeX-goto-corresponding-environment)
2953 (YaTeX-reindent depth)))
2954 (if (or
2955 (and NTT-jTeX
2956 (save-excursion (beginning-of-line) (looking-at "[ \t]")))
2957 (save-excursion
2958 (beginning-of-line)
2959 (and
2960 (not (bobp))
2961 (progn
2962 (backward-char 1)
2963 (re-search-backward
2964 "\\\\\\(\\(page\\)?ref\\|cite\\){" (point-beginning-of-line) t))
2965 (goto-char (1- (match-end 0)))
2966 (> (save-excursion
2967 (condition-case ()
2968 (progn (forward-list 1) (point))
2969 (error (point-max))))
2970 (point-end-of-line)))))
2971 (save-excursion
2972 (end-of-line)
2973 (let ((p (point)))
2974 (forward-line -1)
2975 (end-of-line)
2976 (or (= p (point))
2977 (looking-at (regexp-quote YaTeX-comment-prefix))
2978 (bobp) (bolp)
2979 (save-excursion
2980 (backward-word 1)
2981 (looking-at "\\sw+")) ;is not japanese string
2982 (insert YaTeX-comment-prefix)))))))
2984 (defun YaTeX-comment-line-break (&optional soft)
2985 "Call comment-indent-new-line and YaTeX-indent-line"
2986 (comment-indent-new-line soft)
2987 (YaTeX-indent-line))
2989 (defun YaTeX-latex2e-p ()
2990 (let ((b (current-buffer))
2991 (ptn (concat YaTeX-ec "documentclass")))
2992 (unwind-protect
2993 (or (save-excursion (search-backward ptn nil t))
2994 (progn
2995 (YaTeX-visit-main t)
2996 (save-excursion (search-backward ptn nil t))))
2997 (set-buffer b))))
2999 (provide 'yatex)
3000 (defvar yatex-mode-load-hook nil
3001 "*List of functions to be called when yatex.el is loaded.")
3002 (if (and YaTeX-emacs-19 YaTeX-display-color-p (not (featurep 'yatex19)))
3003 (load "yatex19"))
3004 (load "yatexhks" t)
3006 ;;-------------------- Final hook jobs --------------------
3007 (substitute-all-key-definition
3008 'fill-paragraph 'YaTeX-fill-paragraph YaTeX-mode-map)
3009 (substitute-all-key-definition
3010 'kill-buffer 'YaTeX-kill-buffer YaTeX-mode-map)
3011 (run-hooks 'yatex-mode-load-hook)
3013 ;; `History' was moved to ChangeLog
3014 ;----------------------------- End of yatex.el -----------------------------
3016 ; Local variables:
3017 ; coding: sjis
3018 ; End: