yatex

view yatex.el @ 185:85e57c4b27a2

merged
author HIROSE Yuuji <yuuji@koeki-u.ac.jp>
date Wed, 30 Nov 2011 14:22:46 +0900
parents bdc00b25045d 67b1e094e151
children 61996cd55816
line source
1 ;;; -*- Emacs-Lisp -*-
2 ;;; Yet Another tex-mode for emacs - //野鳥//
3 ;;; yatex.el rev. 1.74.5
4 ;;; (c)1991-2011 by HIROSE Yuuji.[yuuji@yatex.org]
5 ;;; Last modified Wed Nov 30 14:22:06 2011 on duke
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.5"
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*" "alltt")
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)
464 (insert (if (YaTeX-in-math-mode-p) "\\backslash" "\\textbackslash"))))
465 (if YaTeX-no-begend-shortcut
466 (progn
467 (YaTeX-define-key "B" 'YaTeX-make-begin-end-region)
468 (YaTeX-define-key "b" 'YaTeX-make-begin-end))
469 (YaTeX-define-begend-key "bc" "center")
470 (YaTeX-define-begend-key "bd" "document")
471 (YaTeX-define-begend-key "bD" "description")
472 (YaTeX-define-begend-key "be" "enumerate")
473 (YaTeX-define-begend-key "bE" "equation")
474 (YaTeX-define-begend-key "bi" "itemize")
475 (YaTeX-define-begend-key "bl" "flushleft")
476 (YaTeX-define-begend-key "bm" "minipage")
477 (YaTeX-define-begend-key "bt" "tabbing")
478 (YaTeX-define-begend-key "bT" "tabular")
479 (YaTeX-define-begend-key "b\^t" "table")
480 (YaTeX-define-begend-key "bp" "picture")
481 (YaTeX-define-begend-key "bq" "quote")
482 (YaTeX-define-begend-key "bQ" "quotation")
483 (YaTeX-define-begend-key "br" "flushright")
484 (YaTeX-define-begend-key "bv" "verbatim")
485 (YaTeX-define-begend-key "bV" "verse")
486 (YaTeX-define-key "B " 'YaTeX-make-begin-end-region)
487 (YaTeX-define-key "b " 'YaTeX-make-begin-end))
488 (YaTeX-define-key "e" 'YaTeX-end-environment)
489 (YaTeX-define-key "S" 'YaTeX-make-section-region)
490 (YaTeX-define-key "s" 'YaTeX-make-section)
491 (YaTeX-define-key "L" 'YaTeX-make-fontsize-region)
492 (YaTeX-define-key "l" 'YaTeX-make-fontsize)
493 (YaTeX-define-key "m" 'YaTeX-make-singlecmd)
494 (YaTeX-define-key "." 'YaTeX-comment-paragraph)
495 (YaTeX-define-key "," 'YaTeX-uncomment-paragraph)
496 (YaTeX-define-key ">" 'YaTeX-comment-region)
497 (YaTeX-define-key "<" 'YaTeX-uncomment-region)
498 (YaTeX-define-key "g" 'YaTeX-goto-corresponding-*)
499 (YaTeX-define-key "k" 'YaTeX-kill-*)
500 (YaTeX-define-key "c" 'YaTeX-change-*)
501 (YaTeX-define-key "a" 'YaTeX-make-accent)
502 (YaTeX-define-key "?" 'YaTeX-help)
503 (YaTeX-define-key "/" 'YaTeX-apropos)
504 (YaTeX-define-key "&" 'YaTeX-what-column)
505 (YaTeX-define-key "d" 'YaTeX-display-hierarchy)
506 (YaTeX-define-key "x" YaTeX-user-extensional-map)
507 (YaTeX-define-key "n"
508 '(lambda () (interactive)
509 (insert "\\" (if (YaTeX-on-section-command-p "o?oalign") "crcr" "\\"))))
510 (if YaTeX-dos
511 (define-key YaTeX-prefix-map "\C-r"
512 '(lambda () (interactive)
513 (YaTeX-set-screen-height YaTeX-saved-screen-height) (recenter)))))
515 (defvar YaTeX-section-completion-map nil
516 "*Key map used at YaTeX completion in the minibuffer.")
517 (if YaTeX-section-completion-map nil
518 (setq YaTeX-section-completion-map
519 (copy-keymap (or (and (boundp 'gmhist-completion-map)
520 gmhist-completion-map)
521 minibuffer-local-completion-map)))
522 (define-key YaTeX-section-completion-map
523 " " 'YaTeX-minibuffer-complete)
524 (define-key YaTeX-section-completion-map
525 "\C-i" 'YaTeX-minibuffer-complete)
526 (define-key YaTeX-section-completion-map
527 "\C-v" 'YaTeX-read-section-with-overview))
529 (defvar YaTeX-recursive-map nil
530 "*Key map used at YaTeX reading arguments in the minibuffer.")
531 (if YaTeX-recursive-map nil
532 (setq YaTeX-recursive-map (copy-keymap global-map))
533 (define-key YaTeX-recursive-map YaTeX-prefix YaTeX-prefix-map)
534 (mapcar
535 (function
536 (lambda (key)
537 (define-key YaTeX-mode-map (car key) 'YaTeX-math-insert-sequence)
538 (define-key YaTeX-recursive-map (car key) 'YaTeX-math-insert-sequence)))
539 YaTeX-math-key-list))
540 ;---------- Define other variable ----------
541 (defvar YaTeX-env-name "document" "*Initial tex-environment completion")
542 (defvar YaTeX-section-name
543 (if YaTeX-use-LaTeX2e "documentclass" "documentstyle")
544 "*Initial tex-section completion")
545 (defvar YaTeX-fontsize-name "large" "*Initial fontsize completion")
546 (defvar YaTeX-single-command "maketitle" "*Initial LaTeX single command")
547 (defvar YaTeX-kanji-code (if YaTeX-dos 1 2)
548 "*File kanji code used by Japanese TeX.
549 nil: Do not care (Preserve coding-system)
550 0: no-converion (mule)
551 1: Shift JIS
552 2: JIS
553 3: EUC
554 4: UTF-8")
556 (defvar YaTeX-coding-system nil "File coding system used by Japanese TeX.")
557 (cond
558 (YaTeX-emacs-20
559 (setq YaTeX-coding-system
560 (cdr (assoc YaTeX-kanji-code YaTeX-kanji-code-alist))))
561 ((boundp 'MULE)
562 (setq YaTeX-coding-system
563 (symbol-value (cdr (assoc YaTeX-kanji-code YaTeX-kanji-code-alist))))))
565 (defvar YaTeX-mode-syntax-table nil
566 "*Syntax table for yatex-mode")
568 (if YaTeX-mode-syntax-table nil
569 (setq YaTeX-mode-syntax-table (make-syntax-table (standard-syntax-table)))
570 (modify-syntax-entry ?\n " " YaTeX-mode-syntax-table)
571 (modify-syntax-entry ?\{ "(}" YaTeX-mode-syntax-table)
572 (modify-syntax-entry ?\} "){" YaTeX-mode-syntax-table)
573 (modify-syntax-entry ?\t " " YaTeX-mode-syntax-table)
574 (modify-syntax-entry ?\f ">" YaTeX-mode-syntax-table)
575 (modify-syntax-entry ?\n ">" 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)
579 (modify-syntax-entry ?~ " " YaTeX-mode-syntax-table))
581 ;---------- Provide YaTeX-mode ----------
582 ;;;
583 ;; Major mode definition
584 ;;;
585 (defun yatex-mode ()
586 " Yet Another LaTeX mode: Major mode for editing input files of LaTeX.
587 -You can invoke processes concerning LaTeX typesetting by
588 \\[YaTeX-typeset-menu]
589 -Complete LaTeX environment form of `\\begin{env} ... \\end{env}' by
590 \\[YaTeX-make-begin-end]
591 -Enclose region into some environment by
592 \\[universal-argument] \\[YaTeX-make-begin-end]
593 -Complete LaTeX command which takes argument like `\\section{}' by
594 \\[YaTeX-make-section]
595 -Put LaTeX command which takes no arguments like `\\maketitle' by
596 \\[YaTeX-make-singlecmd]
597 -Complete font or character size descriptor like `{\\large }' by
598 \\[YaTeX-make-fontsize]
599 -Enclose region into those descriptors above by
600 \\[universal-argument] \\[YaTeX-make-fontsize]
601 -Enter European accent notations by
602 \\[YaTeX-make-accent]
603 -Toggle various modes of YaTeX by
604 \\[YaTeX-switch-mode-menu]
605 -Change environt name (on the begin/end line) by
606 \\[YaTeX-change-*]
607 -Kill LaTeX command/environment sequences by
608 \\[YaTeX-kill-*]
609 -Kill LaTeX command/environment with its contents
610 \\[universal-argument] \\[YaTeX-kill-*]
611 -Go to corresponding object (begin/end, file, labels) by
612 \\[YaTeX-goto-corresponding-*] or
613 \\[YaTeX-goto-corresponding-*-other-window] (in other window)
614 \\[YaTeX-goto-corresponding-*-other-frame] (in other frame)
615 -Go to main LaTeX source text by
616 \\[YaTeX-visit-main] or
617 \\[YaTeX-visit-main-other-window] (in other window)
618 \\[YaTeX-visit-main-other-frame] (in other frame)
619 -Comment out or uncomment region by
620 \\[YaTeX-comment-region] or \\[YaTeX-uncomment-region]
621 -Comment out or uncomment paragraph by
622 \\[YaTeX-comment-paragraph] or \\[YaTeX-uncomment-paragraph]
623 -Make an \\item entry hang-indented by
624 \\[YaTeX-fill-item]
625 -Enclose the region with parentheses by
626 \\[YaTeX-insert-parens-region]
627 \\[YaTeX-insert-braces-region]
628 \\[YaTeX-insert-brackets-region]
629 \\[YaTeX-insert-dollars-region]
630 -Look up the corresponding column header of tabular environment by
631 \\[YaTeX-what-column]
632 -Enter a newline and an entry suitable for environment by
633 \\[YaTeX-intelligent-newline]
634 -View the structure of file inclusion by
635 \\[YaTeX-display-hierarchy]
636 -Refer the online help of popular LaTeX commands by
637 \\[YaTeX-help] (help)
638 \\[YaTeX-apropos] (apropos)
639 -Edit `%# notation' by
640 \\[YaTeX-%-menu]
642 Those are enough for fastening your editing of LaTeX source. But further
643 more features are available and they are documented in the manual.
644 "
645 (interactive)
646 (kill-all-local-variables)
647 (setq major-mode 'yatex-mode)
648 (setq mode-name (if YaTeX-japan "やてふ" "YaTeX"))
649 (mapcar 'make-local-variable
650 '(dvi2-command fill-column fill-prefix
651 tmp-env-table tmp-section-table tmp-fontsize-table
652 tmp-singlecmd-table paragraph-start paragraph-separate
653 YaTeX-math-mode indent-line-function comment-line-break-function
654 comment-start comment-start-skip
655 ))
656 (cond ((null YaTeX-kanji-code)
657 nil)
658 ((boundp 'MULE)
659 (set-file-coding-system YaTeX-coding-system))
660 ((and YaTeX-emacs-20 (boundp 'buffer-file-coding-system))
661 (setq buffer-file-coding-system
662 (or (and (fboundp 'set-auto-coding) buffer-file-name
663 (save-excursion
664 (goto-char (point-min))
665 (set-auto-coding buffer-file-name (buffer-size))))
666 YaTeX-coding-system)))
667 ((featurep 'mule)
668 (set-file-coding-system YaTeX-coding-system))
669 ((boundp 'NEMACS)
670 (make-local-variable 'kanji-fileio-code)
671 (setq kanji-fileio-code YaTeX-kanji-code)))
672 (setq fill-column YaTeX-fill-column
673 fill-prefix YaTeX-fill-prefix
674 paragraph-start YaTeX-paragraph-start
675 paragraph-separate YaTeX-paragraph-separate
676 indent-line-function 'YaTeX-indent-line
677 comment-start YaTeX-comment-prefix
678 comment-end ""
679 comment-start-skip "[^\\\\]%+[ \t]*"
680 local-abbrev-table yatex-mode-abbrev-table)
681 (if (fboundp 'comment-indent-new-line) ;for Emacs21
682 (setq comment-line-break-function 'YaTeX-comment-line-break))
684 (if (and YaTeX-use-font-lock (featurep 'font-lock))
685 (progn
686 (require 'yatex19)
687 (YaTeX-font-lock-set-default-keywords)
688 (or (featurep 'xemacs)
689 (set (make-local-variable 'font-lock-defaults)
690 (get 'yatex-mode 'font-lock-defaults)))
691 ;;(font-lock-mode 1)
692 ))
693 (use-local-map YaTeX-mode-map)
694 (set-syntax-table YaTeX-mode-syntax-table)
695 (if YaTeX-dos (setq YaTeX-saved-screen-height (YaTeX-screen-height)))
696 (YaTeX-read-user-completion-table)
697 (and (fboundp 'YaTeX-hilit-setup-alist) (YaTeX-hilit-setup-alist))
698 (makunbound 'inenv)
699 (turn-on-auto-fill) ;1.63
700 (and (= 0 (buffer-size)) (file-exists-p YaTeX-template-file)
701 (y-or-n-p (format "Insert %s?" YaTeX-template-file))
702 (insert-file-contents (expand-file-name YaTeX-template-file)))
703 (run-hooks 'text-mode-hook 'yatex-mode-hook))
705 ;---------- Define YaTeX-mode functions ----------
706 (defvar YaTeX-ec "\\" "Escape character of current mark-up language.")
707 (defvar YaTeX-ec-regexp (regexp-quote YaTeX-ec))
708 (defvar YaTeX-struct-begin
709 (concat YaTeX-ec "begin{%1}%2")
710 "Keyword format of begin-environment.")
711 (defvar YaTeX-struct-end
712 (concat YaTeX-ec "end{%1}")
713 "Keyword format of end-environment.")
714 (defvar YaTeX-struct-name-regexp "[^}]+"
715 "Environment name regexp.")
716 (defvar YaTeX-TeX-token-regexp
717 (cond (YaTeX-japan "[A-Za-z*ぁ-ん亜-龠]+")
718 (t "[A-Za-z*]+"))
719 "Regexp of characters which can be a member of TeX command's name.")
720 (defvar YaTeX-kanji-regexp "[ぁ-ん亜-龠]"
721 "Generic regexp of Japanese Kanji (and symbol) characters.")
722 (defvar YaTeX-command-token-regexp YaTeX-TeX-token-regexp
723 "Regexp of characters which can be a member of current mark up language's command name.")
725 ;;(defvar YaTeX-struct-section
726 ;; (concat YaTeX-ec "%1{%2}")
727 ;; "Keyword to make section.")
729 ;;;
730 ;; autoload section
731 ;;;
733 ;;autoload from yatexprc.el
734 (autoload 'YaTeX-visit-main "yatexprc" "Visit main LaTeX file." t)
735 (autoload 'YaTeX-visit-main-other-window "yatexprc"
736 "Visit main other window." t)
737 (autoload 'YaTeX-main-file-p "yatexprc" "Check if the file is main." t)
738 (autoload 'YaTeX-get-builtin "yatexprc" "Get %# built-in." t)
739 (autoload 'YaTeX-system "yatexprc" "Call system command" t)
740 (autoload 'YaTeX-save-buffers "yatexprc" "Save buffers of same major mode" t)
742 ;;autoload from yatexmth.el
743 (autoload 'YaTeX-math-insert-sequence "yatexmth" "Image input." t)
744 (autoload 'YaTeX-in-math-mode-p "yatexmth" "Check if in math-env." t)
745 (autoload 'YaTeX-toggle-math-mode "yatexmth" "YaTeX math-mode interfaces." t)
746 (autoload 'YaTeX-math-member-p "yatexmth" "Check if a word is math command." t)
747 (autoload 'YaTeX-insert-amsparens-region "yatexmth" "AMS parens region" t)
748 (autoload 'YaTeX-insert-amsbraces-region "yatexmth" "AMS braces region" t)
749 (autoload 'YaTeX-insert-amsbrackets-region "yatexmth" "AMS brackets region" t)
750 (autoload 'YaTeX-on-parenthesis-p "yatexmth" "Check if on math-parens" t)
751 (autoload 'YaTeX-goto-open-paren "yatexmth" "Goto opening paren" t)
752 (autoload 'YaTeX-change-parentheses "yatexmth" "Change corresponding parens" t)
753 (autoload 'YaTeX-goto-corresponding-paren "yatexmth" "\bigl\bigr jumps" t)
754 (autoload 'YaTeX-typeset-math-region "yatexmth" "Typeset math-region" t)
756 ;;autoload from yatexhlp.el
757 (autoload 'YaTeX-help "yatexhlp" "YaTeX helper with LaTeX commands." t)
758 (autoload 'YaTeX-apropos "yatexhlp" "Apropos for (La)TeX commands." t)
760 ;;autoload from yatexgen.el
761 (autoload 'YaTeX-generate "yatexgen" "YaTeX add-in function generator." t)
762 (autoload 'YaTeX-generate-simple "yatexgen" "YaTeX add-in support." t)
764 ;;autoload from yatexsec.el
765 (autoload 'YaTeX-section-overview "yatexsec" "YaTeX sectioning(view)" t)
766 (autoload 'YaTeX-read-section-in-minibuffer "yatexsec" "YaTeX sectioning" t)
767 (autoload 'YaTeX-make-section-with-overview "yatexsec" "YaTeX sectioning" t)
769 ;;autoload from yatexenv.el
770 (autoload 'YaTeX-what-column "yatexenv" "YaTeX env. specific funcs" t)
771 (autoload 'YaTeX-intelligent-newline "yatexenv" "YaTeX env. specific funcs" t)
772 (autoload 'YaTeX-indent-line-equation "yatexenv" "Indent equation lines." t)
773 (autoload 'YaTeX-goto-corresponding-leftright "yatexenv" "\left\right jumps" t)
775 ;;autoload from yatexhie.el
776 (autoload 'YaTeX-display-hierarchy "yatexhie"
777 "YaTeX document hierarchy browser" t)
778 (autoload 'YaTeX-display-hierarchy-directly "yatexhie"
779 "Same as YaTeX-display-hierarchy. Call from mouse." t)
781 ;;autoload from yatexpkg.el
782 (autoload 'YaTeX-package-auto-usepackage "yatexpkg" "Auto \\usepackage" t)
784 ;;;
785 ;; YaTeX-mode functions
786 ;;;
787 (defun YaTeX-insert-begin-end (env region-mode)
788 "Insert \\begin{mode-name} and \\end{mode-name}.
789 This works also for other defined begin/end tokens to define the structure."
790 (setq YaTeX-current-completion-type 'begin)
791 (let*((ccol (current-column)) beg beg2 exchange
792 (arg region-mode) ;for old compatibility
793 (indent-column (+ ccol YaTeX-environment-indent))(i 1) func)
794 (if (and region-mode (> (point) (mark)))
795 (progn (exchange-point-and-mark)
796 (setq exchange t
797 ccol (current-column)
798 indent-column (+ ccol YaTeX-environment-indent))))
799 ;;VER2 (insert "\\begin{" env "}" (YaTeX-addin env))
800 (setq beg (point))
801 (YaTeX-insert-struc 'begin env)
802 (setq beg2 (point))
803 (insert "\n")
804 (indent-to indent-column)
805 (save-excursion
806 ;;indent optional argument of \begin{env}, if any
807 (while (> (point-beginning-of-line) beg)
808 (skip-chars-forward "\\s " (point-end-of-line))
809 (indent-to indent-column)
810 (forward-line -1)))
811 (require 'yatexenv)
812 (if region-mode
813 ;;if region-mode, indent all text in the region
814 (save-excursion
815 (if (fboundp (intern-soft (concat "YaTeX-enclose-" env)))
816 (funcall (intern-soft (concat "YaTeX-enclose-" env))
817 (point) (mark))
818 (while (< (progn (forward-line 1) (point)) (mark))
819 (if (eolp) nil
820 (skip-chars-forward " \t\n")
821 (indent-to indent-column))))))
822 (if region-mode (exchange-point-and-mark))
823 (indent-to ccol)
824 ;;VER2 (insert "\\end{" env "}\n")
825 (YaTeX-insert-struc 'end env)
826 (YaTeX-reindent ccol)
827 (if region-mode
828 (progn
829 (insert "\n")
830 (or exchange (exchange-point-and-mark)))
831 (goto-char beg2)
832 (YaTeX-intelligent-newline nil)
833 (YaTeX-indent-line))
834 (YaTeX-package-auto-usepackage env 'env)
835 (if YaTeX-current-position-register
836 (point-to-register YaTeX-current-position-register))))
838 (defun YaTeX-make-begin-end (arg)
839 "Make LaTeX environment command of \\begin{env.} ... \\end{env.}
840 by completing read.
841 If you invoke this command with universal argument,
842 \(key binding for universal-argument is \\[universal-argument]\)
843 you can put REGION into that environment between \\begin and \\end."
844 (interactive "P")
845 (let*
846 ((mode (if arg " region" ""))
847 (env
848 (YaTeX-read-environment
849 (format "Begin environment%s(default %s): " mode YaTeX-env-name))))
850 (if (string= env "")
851 (setq env YaTeX-env-name))
852 (setq YaTeX-env-name env)
853 (YaTeX-update-table
854 (list YaTeX-env-name) 'env-table 'user-env-table 'tmp-env-table)
855 (YaTeX-insert-begin-end YaTeX-env-name arg)))
857 (defun YaTeX-make-begin-end-region ()
858 "Call YaTeX-make-begin-end with ARG to specify region mode."
859 (interactive)
860 (YaTeX-make-begin-end t))
862 (defun YaTeX-guess-section-type ()
863 (if (eq major-mode 'yatex-mode)
864 (save-excursion
865 (cond
866 ((save-excursion (not (search-backward YaTeX-ec nil t)))
867 (if YaTeX-use-LaTeX2e "documentclass" "documentstyle"))
868 ((progn
869 (if (= (char-after (1- (point))) ?~) (forward-char -1))
870 (forward-char -1) (looking-at "表\\|図\\|式\\|第"))
871 "ref")
872 ((and (looking-at "[a-z \t]")
873 (progn (skip-chars-backward "a-z \t")
874 (looking-at "table\\|figure\\|formula")))
875 "ref")
876 ((save-excursion
877 (skip-chars-backward "[^ア-ン]")
878 (looking-at "プログラム\\|リスト"))
879 "ref")
880 ((YaTeX-re-search-active-backward
881 (concat YaTeX-ec-regexp "begin{\\([^}]+\\)}")
882 (regexp-quote YaTeX-comment-prefix)
883 (save-excursion (forward-line -1) (point))
884 t)
885 (let ((env (YaTeX-match-string 1)))
886 (cdr (assoc env
887 '(("table" . "caption"))))))
888 ))))
890 (defun YaTeX-make-section (arg &optional beg end cmd)
891 "Make LaTeX \\section{} type command with completing read.
892 With numeric ARG, you can specify the number of arguments of
893 LaTeX command.
894 For example, if you want to produce LaTeX command
896 \\addtolength{\\topmargin}{8mm}
898 which has two arguments. You can produce that sequence by typing...
899 ESC 2 C-c s add SPC RET \\topm SPC RET 8mm RET
900 \(by default\)
901 Then yatex will automatically complete `addtolength' with two arguments
902 next time.
903 You can complete symbol at LaTeX command and the 1st argument.
905 If the optional 2nd and 3rd argument BEG END are specified, enclose
906 the region from BEG to END into the first argument of the LaTeX sequence.
907 Optional 4th arg CMD is LaTeX command name, for non-interactive use."
908 (interactive "P")
909 (setq YaTeX-current-completion-type 'section)
910 (if (equal arg '(4)) (setq beg (region-beginning) end (region-end)))
911 (unwind-protect
912 (let*
913 ((source-window (selected-window))
914 guess
915 (section
916 (or cmd
917 (progn
918 (setq guess
919 (or (YaTeX-guess-section-type) YaTeX-section-name))
920 (YaTeX-read-section
921 (if YaTeX-simple-messages
922 (format "Section-type (default %s): " guess)
923 (if (> (minibuffer-depth) 0)
924 (format "%s???{} (default %s)%s: "
925 YaTeX-ec guess
926 (format "[level:%d]" (minibuffer-depth)))
927 (format "(C-v for view-section) %s???{%s} (default %s): "
928 YaTeX-ec (if beg "region" "") guess)))
929 nil))))
930 (section (if (string= section "") guess section))
931 (numarg ;; The number of section-type command's argument
932 (or (and (numberp arg) arg)
933 (nth 1 (YaTeX-lookup-table section 'section))
934 1))
935 (arg-reader (intern-soft (concat "YaTeX::" section)))
936 (addin-args (and arg-reader (fboundp arg-reader)))
937 (title "")
938 (j 1)
939 (after-change-functions nil) ;inhibit font-locking temporarily
940 (enable-recursive-minibuffers t)
941 (mkarg-func
942 (function
943 (lambda (n)
944 (while (<= j n)
945 (insert
946 (concat ;to allow nil return value
947 "{"
948 (setq title
949 (cond
950 (addin-args (funcall arg-reader j))
951 (YaTeX-skip-default-reader "")
952 (t
953 (read-string
954 (format "Argument %d of %s: " j section)))))
955 "}"))
956 (setq j (1+ j))))))
957 );;let
958 (setq YaTeX-section-name section)
959 (if beg
960 (let*((e (make-marker))
961 (ar2 (intern-soft (concat "YaTeX::" section "-region")))
962 (arp (and ar2 (fboundp ar2))))
963 (goto-char end)
964 (insert "}")
965 (set-marker e (point))
966 (goto-char beg)
967 (insert YaTeX-ec YaTeX-section-name
968 (YaTeX-addin YaTeX-section-name))
969 (if (> numarg 1) (funcall mkarg-func (1- numarg)))
970 (insert "{")
971 (if arp (funcall ar2 (point) e))
972 (goto-char e)
973 (set-marker e nil))
974 (use-global-map YaTeX-recursive-map)
975 (if (= numarg 0) (YaTeX-make-singlecmd YaTeX-section-name)
976 (progn (insert YaTeX-ec YaTeX-section-name)
977 (insert (YaTeX-addin YaTeX-section-name))))
978 ;;read arguments with add-in
979 (funcall mkarg-func numarg))
980 (YaTeX-update-table
981 (if (/= numarg 1) (list section numarg)
982 (list section))
983 'section-table 'user-section-table 'tmp-section-table)
984 (if YaTeX-current-position-register
985 (point-to-register YaTeX-current-position-register))
986 (if (string= (YaTeX-buffer-substring (- (point) 2) (point))
987 "{}")
988 (forward-char -1))
989 (while (string= (YaTeX-buffer-substring (- (point) 3) (1- (point)))
990 "{}")
991 (forward-char -2))
992 (YaTeX-package-auto-usepackage section 'section))
993 (if (<= (minibuffer-depth) 0) (use-global-map global-map))
994 (insert ""))) ;insert dummy string to fontify(Emacs20)
996 (defun YaTeX-make-section-region (args beg end)
997 "Call YaTeX-make-section with arguments to specify region mode."
998 (interactive "P\nr")
999 (YaTeX-make-section args beg end))
1001 (defun YaTeX-make-fontsize (arg &optional fontsize)
1002 "Make completion like {\\large ...} or {\\slant ...} in minibuffer.
1003 If you invoke this command with universal argument, you can put region
1004 into {\\xxx } braces.
1005 \(key binding for universal-argument is \\[universal-argument]\)"
1006 (interactive "P")
1007 (YaTeX-sync-local-table 'tmp-fontsize-table)
1008 (let* ((mode (if arg "region" ""))
1009 (fontsize
1010 (or fontsize
1011 (YaTeX-read-fontsize
1012 (if YaTeX-simple-messages
1013 (format "Font or size (default %s): " YaTeX-fontsize-name)
1014 (format "{\\??? %s} (default %s)%s: " mode YaTeX-fontsize-name
1015 (if (> (minibuffer-depth) 0)
1016 (format "[level:%d]" (minibuffer-depth)) "")))
1017 nil nil))))
1018 (if (string= fontsize "")
1019 (setq fontsize YaTeX-fontsize-name))
1020 (setq YaTeX-current-completion-type 'large)
1021 (setq YaTeX-fontsize-name fontsize)
1022 (YaTeX-update-table
1023 (list YaTeX-fontsize-name)
1024 'fontsize-table 'user-fontsize-table 'tmp-fontsize-table)
1025 (and YaTeX-use-LaTeX2e
1026 (YaTeX-latex2e-p)
1027 (setq fontsize
1028 (cdr (assoc YaTeX-fontsize-name LaTeX2e-fontstyle-alist)))
1029 (setq YaTeX-fontsize-name fontsize))
1030 (if arg
1031 (save-excursion
1032 (if (> (point) (mark)) (exchange-point-and-mark))
1033 (insert "{\\" YaTeX-fontsize-name " ")
1034 (exchange-point-and-mark)
1035 (insert "}"))
1036 (insert (concat "{\\" YaTeX-fontsize-name " }"))
1037 (forward-char -1)
1038 (if YaTeX-current-position-register
1039 (point-to-register YaTeX-current-position-register))
1040 (save-excursion
1041 (insert (YaTeX-addin YaTeX-fontsize-name)))
1042 (YaTeX-package-auto-usepackage YaTeX-fontsize-name 'large))))
1044 (defun YaTeX-make-fontsize-region ()
1045 "Call function:YaTeX-make-fontsize with ARG to specify region mode."
1046 (interactive)
1047 (YaTeX-make-fontsize t))
1049 (defvar YaTeX-singlecmd-suffix "" "*Suffix for maketitle-type commands.")
1050 (defvar YaTeX-read-singlecmd-history nil "Holds maketitle-type history.")
1051 (put 'YaTeX-read-singlecmd-history 'no-default t)
1052 (defun YaTeX-make-singlecmd (single)
1053 (interactive
1054 (list (YaTeX-cplread-with-learning
1055 (if YaTeX-simple-messages
1056 (format "maketitle-type (default %s): " YaTeX-single-command)
1057 (format "%s??? (default %s)%s: " YaTeX-ec YaTeX-single-command
1058 (if (> (minibuffer-depth) 0)
1059 (format "[level:%d]" (minibuffer-depth)) "")))
1060 'singlecmd-table 'user-singlecmd-table 'tmp-singlecmd-table
1061 nil nil nil 'YaTeX-read-singlecmd-history)))
1062 (if (string= single "")
1063 (setq single YaTeX-single-command))
1064 (setq YaTeX-single-command single)
1065 (setq YaTeX-current-completion-type 'maketitle)
1066 (let ((dollar (and (not (YaTeX-in-math-mode-p))
1067 (YaTeX-math-member-p YaTeX-single-command)))
1068 p q)
1069 (if dollar (insert "$"))
1070 (insert YaTeX-ec YaTeX-single-command)
1071 (setq p (point))
1072 (insert (YaTeX-addin single) YaTeX-singlecmd-suffix)
1073 (if dollar (insert "$"))
1074 (setq q (point))
1075 (goto-char p)
1076 (forward-char -2)
1077 (if (looking-at "\\[\\]") (forward-char 1) (goto-char q)))
1078 (YaTeX-package-auto-usepackage YaTeX-single-command 'maketitle)
1079 (if YaTeX-current-position-register
1080 (point-to-register YaTeX-current-position-register)))
1082 (defvar YaTeX-completion-begin-regexp "[{\\]"
1083 "Regular expression of limit where LaTeX command's completion begins.")
1085 (defun YaTeX-do-completion ()
1086 "Try completion on LaTeX command preceding point."
1087 (interactive)
1088 (if
1089 (or (eq (preceding-char) ? )
1090 (eq (preceding-char) ?\t)
1091 (eq (preceding-char) ?\n)
1092 (bobp))
1093 (message "Nothing to complete.") ;Do not complete
1094 (let* ((end (point))
1095 (limit (point-beginning-of-line))
1096 (completion-begin
1097 (progn (re-search-backward "[ \t\n]" limit 1) (point)))
1098 (begin (progn
1099 (goto-char end)
1100 (if (re-search-backward YaTeX-completion-begin-regexp
1101 completion-begin t)
1102 (1+ (point))
1103 nil))))
1104 (goto-char end)
1105 (cond
1106 ((null begin)
1107 (message "I think it is not a LaTeX sequence."))
1108 (t
1109 (mapcar 'YaTeX-sync-local-table
1110 '(tmp-section-table tmp-env-table tmp-singlecmd-table))
1111 (let*((pattern (YaTeX-buffer-substring begin end))
1112 (all-table
1113 (append
1114 section-table user-section-table tmp-section-table
1115 env-table user-env-table tmp-env-table
1116 singlecmd-table user-singlecmd-table tmp-singlecmd-table))
1117 ;; First,
1118 ;; search completion without backslash.
1119 (completion (try-completion pattern all-table)))
1120 (if
1121 (eq completion nil)
1122 ;; Next,
1123 ;; search completion with backslash
1124 (setq completion
1125 (try-completion
1126 (YaTeX-buffer-substring (1- begin) end)
1127 all-table nil)
1128 begin (1- begin)))
1129 (cond
1130 ((null completion)
1131 (message (concat "Can't find completion for '" pattern "'"))
1132 (ding))
1133 ((eq completion t) (message "Sole completion."))
1134 ((not (string= completion pattern))
1135 (delete-region begin end)
1136 (insert completion)
1138 (t
1139 (message "Making completion list...")
1140 (with-output-to-temp-buffer "*Help*"
1141 (display-completion-list
1142 (all-completions pattern all-table)))))))))))
1144 (defun YaTeX-toggle-modify-mode (&optional arg)
1145 (interactive "P")
1146 (or (memq 'YaTeX-modify-mode mode-line-format)
1147 (setq mode-line-format
1148 (append (list "" 'YaTeX-modify-mode) mode-line-format)))
1149 (if (or arg (null YaTeX-modify-mode))
1150 (progn
1151 (setq YaTeX-modify-mode "*m*")
1152 (message "Modify mode"))
1153 (setq YaTeX-modify-mode nil)
1154 (message "Cancel modify mode."))
1155 (set-buffer-modified-p (buffer-modified-p))) ;redraw mode-line
1157 (defun YaTeX-switch-mode-menu (arg &optional char)
1158 (interactive "P")
1159 (message "Toggle: (M)odify-mode ma(T)h-mode")
1160 (let ((c (or char (read-char))))
1161 (cond
1162 ((= c ?m) (YaTeX-toggle-modify-mode arg))
1163 ((or (= c ?$) (= c ?t))
1164 (if YaTeX-auto-math-mode
1165 (message "Makes no sense in YaTeX-auto-math-mode.")
1166 (YaTeX-toggle-math-mode arg))))))
1168 (defun YaTeX-insert-quote ()
1169 (interactive)
1170 (insert
1171 (cond
1172 ((YaTeX-literal-p) ?\")
1173 ((= (preceding-char) ?\\ ) ?\")
1174 ;((= (preceding-char) ?\( ) ?\")
1175 ((or (= (preceding-char) 32)
1176 (= (preceding-char) 9)
1177 (= (preceding-char) ?\n)
1178 (bobp)
1179 (string-match
1180 (regexp-quote (char-to-string (preceding-char)))
1181 "、。,.?!「」『』【】()"))
1182 "``")
1183 (t "''"))))
1185 (defun YaTeX-closable-p ()
1186 (and (not YaTeX-modify-mode)
1187 (not (eq YaTeX-close-paren-always 'never))
1188 (or YaTeX-close-paren-always (eolp))
1189 (not (input-pending-p))
1190 (not (YaTeX-literal-p)))
1191 ;;(or YaTeX-modify-mode
1192 ;; (and (not YaTeX-close-paren-always) (not (eolp)))
1193 ;; (input-pending-p)
1194 ;; (YaTeX-quick-in-environment-p "verbatim"))
1197 (defun YaTeX-insert-braces-region (beg end &optional open close)
1198 (interactive "r")
1199 (save-excursion
1200 (goto-char end)
1201 (YaTeX-insert-inherit (or close "}"))
1202 (goto-char beg)
1203 (YaTeX-insert-inherit (or open "{"))))
1205 (defun YaTeX-insert-braces (arg &optional open close)
1206 (interactive "p")
1207 (let (env)
1208 (cond
1209 ((YaTeX-jmode) (YaTeX-self-insert arg))
1210 ((not (YaTeX-closable-p)) (YaTeX-self-insert arg))
1211 ((save-excursion
1212 (and (> (- (point) (point-min)) 6)
1213 (condition-case () (forward-char -6) (error nil)))
1214 (looking-at "\\\\left\\\\"))
1215 (insert "{\\right\\}")
1216 (forward-char -8))
1217 ((save-excursion ;from matsu@math.s.chiba-u.ac.jp
1218 (and (> (- (point) (point-min)) 6) (forward-char -6))
1219 (looking-at "\\\\[bB]igl\\\\"))
1220 (insert
1221 (concat
1222 "{" (buffer-substring (match-beginning 0) (- (match-end 0) 2)) "r\\}"))
1223 (forward-char -7))
1224 ((save-excursion
1225 (and (> (- (point) (point-min)) 7)
1226 (condition-case () (forward-char -7) (error nil)))
1227 (looking-at "\\\\[bB]iggl\\\\"))
1228 (insert
1229 (concat
1230 "{" (buffer-substring (match-beginning 0) (- (match-end 0) 2)) "r\\}"))
1231 (forward-char -8))
1232 ((= (preceding-char) ?\\ )
1233 (insert "{\\}")
1234 (forward-char -2)) ;matsu's hack ends here
1235 ((and (> (point) (+ (point-min) 4))
1236 (save-excursion (backward-char 4) (looking-at "\\\\end"))
1237 (not (YaTeX-literal-p))
1238 (setq env (YaTeX-inner-environment)))
1239 (momentary-string-display
1240 (concat
1241 "{"
1242 (cond
1243 (YaTeX-japan
1244 (format "今度からはちゃんと %s b を使いましょう" YaTeX-prefix))
1245 (t (format "You don't understand Zen of `%s b':p" YaTeX-prefix)))
1246 "}")
1247 (point))
1248 (insert (or open "{") env (or close "}")))
1249 (t
1250 (insert (or open "{") (or close "}"))
1251 (forward-char -1)
1252 (if (and (eq (char-after (point)) ?\})
1253 (eq (char-after (- (point) 2)) ?\\ ))
1254 (progn (insert "\\") (forward-char -1)))
1255 ))))
1257 (defun YaTeX-jmode ()
1258 (or (and (boundp 'canna:*japanese-mode*) canna:*japanese-mode*)
1259 (and (boundp 'egg:*mode-on*) egg:*mode-on* egg:*input-mode*)
1260 (and (boundp 'skk-mode) skk-mode)
1261 (and (boundp 'default-input-method) default-input-method
1262 current-input-method)))
1264 (defun YaTeX-jmode-off ()
1265 (if (cond
1266 ((and (boundp 'canna:*japanese-mode*) canna:*japanese-mode*)
1267 (canna-toggle-japanese-mode) t)
1268 ((and (boundp 'egg:*mode-on*) egg:*mode-on* egg:*input-mode*)
1269 (egg:toggle-egg-mode-on-off) t)
1270 ((and (fboundp 'skk-mode) (boundp 'skk-mode) skk-mode)
1271 (cond
1272 ((fboundp 'skk-latin-mode) (skk-latin-mode t))
1273 ((fboundp 'skk-mode-off) (skk-mode-off))
1274 (t (j-mode-off)))
1275 t)
1276 ((and (fboundp 'toggle-input-method) current-input-method)
1277 (toggle-input-method) t)
1278 ((and (fboundp 'fep-force-off) (fep-force-off))))
1279 (put 'YaTeX-jmode 'jmode t)))
1281 (defun YaTeX-jmode-on ()
1282 (cond
1283 ((boundp 'canna:*japanese-mode*)
1284 (if (not canna:*japanese-mode*) (canna-toggle-japanese-mode)))
1285 ((boundp 'egg:*mode-on*)
1286 (and (not egg:*mode-on*) (not egg:*input-mode*)
1287 (egg:toggle-egg-mode-on-off)))
1288 ((and (fboundp 'skk-mode) (boundp 'skk-mode))
1289 (if (not skk-mode) (skk-mode 1)))
1290 ((fboundp 'toggle-input-method)
1291 (if (not current-input-method) (toggle-input-method)))
1292 ((and (fboundp 'fep-force-on) (fep-force-on)))))
1294 (defun YaTeX-jmode-back ()
1295 (if (get 'YaTeX-jmode 'jmode)
1296 (YaTeX-jmode-on))
1297 (setplist 'YaTeX-jmode nil))
1299 (defun YaTeX-self-insert (arg)
1300 (call-interactively (global-key-binding (char-to-string last-command-char))))
1301 (defun YaTeX-insert-inherit (&rest args)
1302 (apply (if (fboundp 'insert-and-inherit) 'insert-and-inherit 'insert)
1303 args))
1305 (defun YaTeX-insert-brackets (arg)
1306 "Insert Kagi-kakko or \\ [ \\] pair or simply \[."
1307 (interactive "p")
1308 (let ((col (1- (current-column))))
1309 (cond
1310 ((YaTeX-jmode) (YaTeX-self-insert arg))
1311 ((not (YaTeX-closable-p))
1312 (YaTeX-self-insert arg))
1313 ((save-excursion
1314 (and (> (- (point) (point-min)) 5) (forward-char -5))
1315 (looking-at "\\\\left"))
1316 (YaTeX-insert-inherit "[\\right]")
1317 (forward-char -7))
1318 ((save-excursion ;from matsu@math.s.chiba-u.ac.jp
1319 (and (> (- (point) (point-min)) 5) (forward-char -5))
1320 (looking-at "\\\\[bB]igl"))
1321 (YaTeX-insert-inherit
1322 (concat
1323 "[" (buffer-substring (match-beginning 0) (- (match-end 0) 1)) "r]"))
1324 (forward-char -6))
1325 ((save-excursion
1326 (and (> (- (point) (point-min)) 6) (forward-char -6))
1327 (looking-at "\\\\[bB]iggl"))
1328 (YaTeX-insert-inherit
1329 (concat
1330 "[" (buffer-substring (match-beginning 0) (- (match-end 0) 1)) "r]"))
1331 (forward-char -7)) ;matsu's hack ends here
1332 ((and (= (preceding-char) ?\\ )
1333 (/= (char-after (- (point) 2)) ?\\ )
1334 (not (YaTeX-in-math-mode-p)))
1335 (YaTeX-insert-inherit last-command-char "\n")
1336 (indent-to (max 0 col))
1337 (YaTeX-insert-inherit "\\]")
1338 (beginning-of-line)
1339 (open-line 1)
1340 (delete-region (point) (progn (beginning-of-line) (point)))
1341 (indent-to (+ YaTeX-environment-indent (max 0 col)))
1342 (or YaTeX-auto-math-mode YaTeX-math-mode (YaTeX-toggle-math-mode 1)))
1343 ((YaTeX-closable-p)
1344 (YaTeX-insert-inherit "[]")
1345 (backward-char 1))
1346 (t (YaTeX-self-insert arg)))))
1348 (defun YaTeX-insert-brackets-region (beg end)
1349 (interactive "r")
1350 (YaTeX-insert-braces-region beg end "[" "]"))
1352 (defun YaTeX-insert-parens (arg)
1353 "Insert parenthesis pair."
1354 (interactive "p")
1355 (cond
1356 ((YaTeX-jmode) (YaTeX-self-insert arg))
1357 ((not (YaTeX-closable-p)) (YaTeX-self-insert arg))
1358 ((save-excursion
1359 (and (> (- (point) (point-min)) 5) (forward-char -5))
1360 (looking-at "\\\\left"))
1361 (YaTeX-insert-inherit "(\\right)")
1362 (forward-char -7))
1363 ((save-excursion ;from matsu@math.s.chiba-u.ac.jp
1364 (and (> (- (point) (point-min)) 5) (forward-char -5))
1365 (looking-at "\\\\[bB]igl"))
1366 (YaTeX-insert-inherit
1367 (concat
1368 "(" (buffer-substring (match-beginning 0) (- (match-end 0) 1)) "r)"))
1369 (forward-char -6))
1370 ((save-excursion
1371 (and (> (- (point) (point-min)) 6) (forward-char -6))
1372 (looking-at "\\\\[bB]iggl"))
1373 (YaTeX-insert-inherit
1374 (concat
1375 "(" (buffer-substring (match-beginning 0) (- (match-end 0) 1)) "r)"))
1376 (forward-char -7))
1377 ((= (preceding-char) ?\\ ) ;matsu's hack ends here
1378 (YaTeX-insert-inherit "(\\)")
1379 (backward-char 2))
1380 ((YaTeX-closable-p)
1381 (YaTeX-insert-inherit "()")
1382 (backward-char 1))
1383 (t (YaTeX-self-insert arg))))
1385 (defun YaTeX-insert-parens-region (beg end)
1386 (interactive "r")
1387 (YaTeX-insert-braces-region beg end "(" ")"))
1389 (defun YaTeX-insert-bar (arg)
1390 "Insert bar pair."
1391 (interactive "p")
1392 (cond
1393 ((YaTeX-jmode) (YaTeX-self-insert arg))
1394 ((not (YaTeX-closable-p)) (YaTeX-self-insert arg))
1395 ((save-excursion
1396 (and (> (- (point) (point-min)) 5) (forward-char -5))
1397 (looking-at "\\\\left"))
1398 (YaTeX-insert-inherit "|\\right|")
1399 (forward-char -7))
1400 ((save-excursion ;from matsu@math.s.chiba-u.ac.jp
1401 (and (> (- (point) (point-min)) 5) (forward-char -5))
1402 (looking-at "\\\\[bB]igl"))
1403 (insert
1404 (concat
1405 "|" (buffer-substring (match-beginning 0) (- (match-end 0) 1)) "r|"))
1406 (forward-char -6))
1407 ((save-excursion
1408 (and (> (- (point) (point-min)) 6) (forward-char -6))
1409 (looking-at "\\\\[bB]iggl"))
1410 (insert
1411 (concat
1412 "|" (buffer-substring (match-beginning 0) (- (match-end 0) 1)) "r|"))
1413 (forward-char -7))
1414 ((save-excursion ; added by Jin <MAF01011@nifty.ne.jp>
1415 (and (> (- (point) (point-min)) 6) (forward-char -6))
1416 (looking-at "\\\\left\\\\"))
1417 (YaTeX-insert-inherit "|\\right\\|")
1418 (forward-char -8))
1419 ((save-excursion
1420 (and (> (- (point) (point-min)) 6) (forward-char -6))
1421 (looking-at "\\\\[bB]igl\\\\"))
1422 (insert
1423 (concat
1424 "|" (buffer-substring (match-beginning 0) (- (match-end 0) 2)) "r\\|"))
1425 (forward-char -7))
1426 ((save-excursion
1427 (and (> (- (point) (point-min)) 7) (forward-char -7))
1428 (looking-at "\\\\[bB]iggl\\\\"))
1429 (insert
1430 (concat
1431 "|" (buffer-substring (match-beginning 0) (- (match-end 0) 2)) "r\\|"))
1432 (forward-char -8)) ; added by Jin up to here.
1433 ((= (preceding-char) ?\\ )
1434 (YaTeX-insert-inherit "|\\|")
1435 (backward-char 2))
1436 ; ((and (YaTeX-closable-p)
1437 ; (/= (preceding-char) ?|)
1438 ; (/= (following-char) ?|))
1439 ; (YaTeX-insert-inherit "||")
1440 ; (backward-char 1))
1441 (t (YaTeX-self-insert arg))))
1443 (defun YaTeX-jmode-hook (old new)
1444 "A hook controling jmode on/off."
1445 (let ((inhibit-point-motion-hooks t)
1446 (oldp (plist-get (text-properties-at old) 'point-left))
1447 (newp (plist-get (text-properties-at new) 'point-left))
1448 (lnew (plist-get (text-properties-at new) 'last-new))
1449 (bmp (buffer-modified-p)) ;(endc ?x)
1450 (jm (YaTeX-jmode)) b e)
1451 ;;(if (eq 'YaTeX-jmode-hook newp)
1452 ;; (setq endc (char-after (next-single-property-change new 'point-left))))
1453 ;;(message "n[%c]=%s o[%c]=%s end=[%c] jm=%s"
1454 ;; (char-after new) newp (char-after old) oldp endc jm)
1455 (unwind-protect
1456 (cond
1457 ((eq lnew new) nil) ;Do nothing if continuous entry
1458 ((and (not (eq newp 'YaTeX-jmode-hook))
1459 (eq oldp 'YaTeX-jmode-hook))
1460 ;; leave
1461 (remove-text-properties
1462 (1+ (or (previous-single-property-change old 'point-left)
1463 (1- (point))))
1464 (1- (or (next-single-property-change old 'point-left)
1465 (1+ (point))))
1466 (list 'last-new nil))
1467 (if (plist-get (text-properties-at old) 'jmode)
1468 (YaTeX-jmode-on)))
1469 ((and (not (eq oldp 'YaTeX-jmode-hook))
1470 (eq newp 'YaTeX-jmode-hook))
1471 ;; enter
1472 (add-text-properties
1473 (1+ (or (previous-single-property-change new 'point-left)
1474 (1- (point))))
1475 (1- (or (next-single-property-change new 'point-left)
1476 (1+ (point))))
1477 (list 'jmode jm 'last-new new))
1478 (YaTeX-jmode-off)))
1479 ;;unwind job
1480 (set-buffer-modified-p bmp))))
1482 (defun YaTeX-insert-dollar ()
1483 (interactive)
1484 (if (or (not (YaTeX-closable-p))
1485 (= (preceding-char) 92)
1486 (and (YaTeX-in-math-mode-p)
1487 (or (/= (preceding-char) ?$) (/= (following-char) ?$))))
1488 (insert "$")
1489 (insert "$$")
1490 (forward-char -1)
1491 (if (fboundp 'add-text-properties)
1492 (add-text-properties
1493 (1- (point)) (1+ (point))
1494 (list 'point-left 'YaTeX-jmode-hook
1495 'point-entered 'YaTeX-jmode-hook
1496 'front-sticky t
1497 'rear-nonsticky t
1498 'jmode (YaTeX-jmode))))
1499 (YaTeX-jmode-off)
1500 (or YaTeX-auto-math-mode YaTeX-math-mode (YaTeX-toggle-math-mode 1))))
1502 (defun YaTeX-insert-dollars-region (beg end)
1503 (interactive "r")
1504 (YaTeX-insert-braces-region beg end "$" "$"))
1506 (defun YaTeX-insert-amper ()
1507 (interactive)
1508 (if (or (string-match YaTeX-array-env-regexp
1509 (or (YaTeX-inner-environment t) "document"))
1510 (= (preceding-char) 92)
1511 (YaTeX-literal-p)
1512 (YaTeX-in-math-mode-p))
1513 (insert "&")
1514 (insert "\\&")))
1516 (defun YaTeX-version ()
1517 "Return string of the version of running YaTeX."
1518 (interactive)
1519 (message
1520 (concat "Yet Another tex-mode "
1521 (if YaTeX-japan "「野鳥」" "`Wild Bird'")
1522 " Revision "
1523 YaTeX-revision-number)))
1525 (defun YaTeX-typeset-menu (arg &optional char)
1526 "Typeset, preview, visit error and miscellaneous convenient menu.
1527 Optional second argument CHAR is for non-interactive call from menu."
1528 (interactive "P")
1529 (message
1530 (concat "J)latex R)egion E)nv B)ibtex mk(I)dx "
1531 "latex+p(D)f "
1532 (if (fboundp 'start-process) "K)ill ")
1533 "P)review "
1534 (and (boundp 'window-system) window-system "S)earch ")
1535 "V)iewErr L)pr"))
1536 (let ((sw (selected-window)) (c (or char (read-char))))
1537 (require 'yatexprc) ;for Nemacs's bug
1538 (select-window sw)
1539 (cond
1540 ((= c ?j) (YaTeX-typeset-buffer))
1541 ((= c ?r) (YaTeX-typeset-region))
1542 ((= c ?e) (YaTeX-typeset-environment))
1543 ((= c ?b) (YaTeX-call-command-on-file
1544 bibtex-command "*YaTeX-bibtex*" YaTeX-parent-file))
1545 ((= c ?i) (YaTeX-call-command-on-file
1546 makeindex-command "*YaTeX-makeindex*" YaTeX-parent-file))
1547 ((= c ?k) (YaTeX-kill-typeset-process YaTeX-typeset-process))
1548 ((= c ?p) (call-interactively 'YaTeX-preview))
1549 ((= c ?q) (YaTeX-system "lpq" "*Printer queue*"))
1550 ((= c ?d) (YaTeX-typeset-buffer YaTeX-dvipdf-command))
1551 ((= c ?v) (YaTeX-view-error))
1552 ((= c ?l) (YaTeX-lpr arg))
1553 ((= c ?m) (YaTeX-switch-mode-menu arg))
1554 ((= c ?b) (YaTeX-insert-string "\\"))
1555 ((= c ?s) (YaTeX-xdvi-remote-search arg)))))
1557 (if (fboundp 'wrap-function-to-control-ime)
1558 (wrap-function-to-control-ime 'YaTeX-typeset-menu t "P"))
1561 (defun YaTeX-%-menu (&optional beg end char)
1562 "Operate %# notation."
1563 ;;Do not use interactive"r" for the functions which require no mark
1564 (interactive)
1565 (message "!)Edit-%%#! B)EGIN-END-region L)Edit-%%#LPR")
1566 (let ((c (or char (read-char))) (string "") key
1567 (b (make-marker)) (e (make-marker)))
1568 (save-excursion
1569 (cond
1570 ((or (= c ?!) (= c ?l)) ;Edit `%#!'
1571 (goto-char (point-min))
1572 (setq key (cond ((= c ?!) "%#!")
1573 ((= c ?l) "%#LPR")))
1574 (if (re-search-forward key nil t)
1575 (progn
1576 (setq string (YaTeX-buffer-substring
1577 (point) (point-end-of-line)))
1578 (delete-region (point) (progn (end-of-line) (point))))
1579 (open-line 1)
1580 (delete-region (point) (progn (beginning-of-line)(point)));for 19 :-<
1581 (insert key))
1582 (unwind-protect
1583 (setq string (read-string (concat key ": ") string))
1584 (insert string)))
1586 ((= c ?b) ;%#BEGIN %#END region
1587 (or end (setq beg (min (point) (mark)) end (max (point) (mark))))
1588 (set-marker b beg)
1589 (set-marker e end)
1590 (goto-char (point-min))
1591 (while (re-search-forward "^%#\\(BEGIN\\)\\|\\(END\\)$" nil t)
1592 (beginning-of-line)
1593 (delete-region (point) (progn (forward-line 1) (point))))
1594 (goto-char b)
1595 (open-line 1)
1596 (delete-region (point) (progn (beginning-of-line)(point)));for 19 :-<
1597 (insert "%#BEGIN")
1598 (goto-char e)
1599 (insert "%#END\n")
1600 (set-marker b nil)
1601 (set-marker e nil))
1602 ))))
1604 (defvar YaTeX-refcommand-def-regexp-default
1605 "label\\|bibitem")
1606 (defvar YaTeX-refcommand-def-regexp-private nil
1607 "*Regexp of defining label commands")
1608 (defvar YaTeX-refcommand-def-regexp
1609 (concat (if YaTeX-refcommand-def-regexp-private
1610 (concat YaTeX-refcommand-def-regexp-private "\\|"))
1611 YaTeX-refcommand-def-regexp-default))
1613 (defvar YaTeX-refcommand-ref-regexp-default
1614 "\\(page\\|eq\\|fig\\)?ref\\|cite")
1615 (defvar YaTeX-refcommand-ref-regexp-private nil
1616 "*Regexp of referring label commands")
1617 (defvar YaTeX-refcommand-ref-regexp
1618 (concat (if YaTeX-refcommand-ref-regexp-private
1619 (concat YaTeX-refcommand-ref-regexp-private "\\|"))
1620 YaTeX-refcommand-ref-regexp-default))
1622 (defvar YaTeX-refcommand-regexp
1623 (concat YaTeX-refcommand-def-regexp
1624 "\\|" YaTeX-refcommand-ref-regexp)
1625 "Regexp of label defining/referring command name.")
1627 (defun YaTeX-goto-corresponding-label (reverse &optional otherwin)
1628 "Jump to corresponding \\label{} and \\ref{} or \\cite and \\bibitem.
1629 The default search direction depends on the command at the cursor position.
1630 When the cursor is on \\ref(\\cite), YaTeX will try to search the
1631 corresponding \\label(\\bibitem) backward,
1632 and if it fails search forward again. And when the cursor is
1633 on \\label(\\bibitem), YaTeX will search the corresponding \\ref(\\cite)
1634 forward at first and secondary backward.
1635 Argument REVERSE non-nil makes the default
1636 direction rule reverse. Since Search string is automatically set in
1637 search-last-string, you can repeat search the same label/ref by typing
1638 \\[isearch-forward] or \\[isearch-backward].
1639 If optional second argument OTHERWIN is non-nil, move to other window."
1641 (let ((scmd "") label direc string blist (p (point)) (cb (current-buffer))
1642 (refcommands YaTeX-refcommand-regexp)
1643 (foundmsg (format "Type %s %c to return to original position."
1644 (key-description
1645 (car
1646 (or (where-is-internal 'register-to-point)
1647 (where-is-internal 'jump-to-register))))
1648 YaTeX-current-position-register))
1649 (func (function (lambda (string sfunc)
1650 (or
1651 (funcall sfunc string nil t)
1652 (funcall (if (eq sfunc 're-search-forward)
1653 're-search-backward 're-search-forward)
1654 string nil t))))))
1655 (cond
1656 ((YaTeX-on-section-command-p refcommands)
1657 (setq scmd
1658 (cdr
1659 (assoc
1660 (YaTeX-match-string 1)
1661 '(("label" . "\\\\\\(page\\|eq\\)?ref{%k}")
1662 ("ref" . "\\\\label{%k}")
1663 ("eqref" . "\\\\label{%k}")
1664 ("pageref" . "\\\\label{%k}")
1665 ("cite" .
1666 "\\\\bibitem\\(\\[[^]]+\\]\\)?{%k}\\|^\\s *@[a-z]+{%k,")
1667 ("bibitem" . "\\\\cite\\(\\[[^]]+\\]\\)?")))))
1668 (goto-char (match-end 0))
1669 (let ((label (YaTeX-buffer-substring
1670 (1- (point)) (progn (backward-list 1) (1+ (point)))))
1671 (fp (make-marker))fl fn
1672 (goother (function (lambda (buffer point)
1673 (goto-char point)
1674 (if (one-window-p)
1675 (split-window-calculate-height
1676 YaTeX-default-pop-window-height))
1677 (select-window (get-lru-window))
1678 (switch-to-buffer buffer)))))
1679 ;(setq string (concat "\\" scmd "{" label "}"))
1680 ;(setq string (concat "\\\\" scmd "{" (regexp-quote label) "}"))
1681 (setq string (YaTeX-replace-format scmd "k" (regexp-quote label)))
1682 (setq direc (if (string-match "ref\\|cite" scmd)
1683 're-search-forward 're-search-backward))
1684 (if YaTeX-current-position-register
1685 (point-to-register YaTeX-current-position-register))
1686 (if reverse (setq direc (if (eq direc 're-search-forward)
1687 're-search-backward 're-search-forward)))
1688 (if (funcall func string direc) ;label/ref found!
1689 (progn
1690 (if otherwin (funcall goother cb p))
1691 (goto-char (match-beginning 0))
1692 (push-mark p))
1693 ;;if label/ref not found, search through all yatex buffers.
1694 (goto-char p) ;resume position of current buffer
1695 (catch 'found
1696 (setq blist (YaTeX-yatex-buffer-list))
1697 (while blist
1698 ;; search for corresponding keyword
1699 (set-buffer (car blist))
1700 (if (YaTeX-on-section-command-p refcommands)
1701 (goto-char (match-beginning 0)))
1702 (cond
1703 ; cond1
1704 ((funcall func string direc)
1705 (cond
1706 (otherwin
1707 (set-buffer cb)
1708 (funcall goother (car blist) p))
1709 ((or (get-buffer-window (car blist))
1710 (and YaTeX-emacs-19
1711 (get-buffer-window (car blist) t)))
1712 (goto-buffer-window (car blist)))
1713 (t
1714 (switch-to-buffer (car blist))
1715 (message foundmsg)))
1716 (goto-char (match-beginning 0))
1717 (throw 'found t))
1718 ; cond2
1719 ((and
1720 (string-match "bibitem" scmd)
1721 (catch 'found2
1722 (save-excursion
1723 (goto-char (point-min))
1724 (while (YaTeX-re-search-active-forward
1725 "\\\\bibliography{\\([^}]*\\)}" "%" nil t)
1726 (setq fl (YaTeX-split-string (YaTeX-match-string 1) ","))
1727 (while fl
1728 (if (or (file-exists-p (setq fn (car fl)))
1729 (file-exists-p (setq fn (concat fn ".bib"))))
1730 (progn
1731 (set-buffer (find-file-noselect fn))
1732 (save-excursion
1733 (goto-char (point-min))
1734 (if (YaTeX-re-search-active-forward
1735 string "%" nil t)
1736 (throw 'found2
1737 (set-marker fp (point)))))))
1738 (setq fl (cdr fl)))))))
1739 (if otherwin
1740 (funcall goother (marker-buffer fp) fp)
1741 (switch-to-buffer (marker-buffer fp))
1742 (goto-char fp))
1743 (set-marker fp nil)
1744 (message foundmsg)
1745 (throw 'found t)))
1746 (setq blist (cdr blist)))
1747 ;; search for bibliography
1748 )))
1749 (if YaTeX-emacs-19
1750 (setq regexp-search-ring
1751 (cons string (delete string regexp-search-ring)))
1752 (setq search-last-regexp string)))
1753 (t nil))))
1755 ;;YaTeX-goto-corresponding-environment was moved to yatexlib
1757 (defun YaTeX-goto-corresponding-file (&optional other)
1758 "Visit or switch buffer of corresponding file,
1759 looking at \\input or \\include or \\includeonly on current line."
1760 (if (not (YaTeX-on-includes-p)) nil
1761 (let ((parent buffer-file-name) input-file b)
1762 (save-excursion
1763 (if (and (re-search-forward "[{%]" (point-end-of-line) t)
1764 (= ?{ (char-after (match-beginning 0))))
1765 nil
1766 (skip-chars-backward "^,{"))
1767 (setq input-file
1768 (YaTeX-buffer-substring
1769 (point) (progn (skip-chars-forward "^ ,}") (point))))
1770 (if (not (string-match "\\.\\(tex\\|sty\\)$" input-file))
1771 (setq input-file (concat input-file ".tex"))))
1772 (cond
1773 (other (YaTeX-switch-to-buffer-other-window input-file))
1774 ((setq b (YaTeX-get-file-buffer input-file))
1775 (goto-buffer-window b))
1776 (t (YaTeX-switch-to-buffer input-file)))
1777 (or (YaTeX-get-builtin "!")
1778 YaTeX-parent-file
1779 (setq YaTeX-parent-file parent)))))
1781 (defun YaTeX-goto-corresponding-BEGIN-END ()
1782 (if (not (YaTeX-on-BEGIN-END-p)) nil
1783 (if (cond
1784 ((equal (match-beginning 0) (match-beginning 1)) ;if on %#BEGIN
1785 (not (search-forward "%#END" nil t)))
1786 (t ; if on %#END
1787 (not (search-backward "%#BEGIN" nil t))))
1788 (error "Corresponding %%#BEGIN/END not found."))
1789 (beginning-of-line)
1790 t))
1792 (defvar YaTeX-processed-file-regexp-alist nil
1793 "Alist of regexp of processed file regexp vs. its file name part;
1794 For example, if you include image file with `\\epsfile{file=FILE}' where
1795 `FILE' is processed file. You might want to view FILE with other previewer
1796 such as ghostview, or want to preview its source which was drawn with
1797 other drawing tool, tgif for example. Then you should set entire regexp
1798 of including expression and enclose its file name part with \\\\( and \\\\).
1800 Ex. (\"\\\\\\\\epsfile{[^}]*file=\\\\([^,} ]+\\\\)\\\\(\\\\.e?ps\\\\)?[^}]*}\" 1)
1802 Where the first group surrounded by \\\\( and \\\\) is the file name part
1803 of expression. So you should set 1 to second element. And the first
1804 matching group is sent to (image) processor defined by the variable
1805 YaTeX-file-processor-alist. See also the documentation of
1806 YaTeX-file-processor-alist.
1808 ↑じゃ良くわかんないすね。例えば tgif hoge.obj して hoge.eps を
1809 \\epsfile{file=hoge.eps} でインクルードしているとしよう。その行で
1810 \[prefix\] g を押した時に tgif を起動して欲しかったら、まず上のような
1811 正規表現を設定する。\\\\(と\\\\)で囲んだところがファイル名になるように
1812 注意する。でファイル名部分が何番目の\\\\(\\\\)になるかをリストの2番目に書く。
1813 すると、その部分が変数 YaTeX-file-processor-alist で定義された
1814 処理プログラムに渡される。というわけ。
1815 ん〜やっぱりむずかしいね。分からない時は隣の Lisper に聞くか、
1816 fj野鳥の会で聞こう!
1817 ")
1819 (defvar YaTeX-processed-file-regexp-alist-default
1820 '(("\\\\epsfile\\(\\[[^]]+\\]\\)?{[^},]*file=\\(\\([^,} ]*/\\)?[^,}. ]+\\)\\(\\.e?ps\\)?[^}]*}" 2)
1821 ("\\\\epsfig{[^},]*fi\\(le\\|gure\\)=\\(\\([^,} ]*/\\)?[^,}. ]+\\)\\(\\.e?ps\\)?[^}]*}" 2)
1822 ("\\\\postscriptbox{[^}]*}{[^}]*}{\\(\\([^,} ]*/\\)?[^}. ]+\\)\\(\\.e?ps\\)?}" 1)
1823 ("\\\\\\(epsfbox\\|includegraphics\\|epsfig\\)\\*?{\\(\\([^,} ]*/\\)?[^}. ]+\\)\\(\\.e?ps\\)?}" 2) ;\epsfbox{hoge.ps} or \includegraphics{hoge.eps}
1824 ("\\\\\\(psbox\\)\\(\\[[^]]+\\]\\)?{\\(\\([^,} ]*/\\)?[^} ]+\\)\\(\\.e?ps\\)}" 3) ;\psbox[options...]{hoge.eps} (97/1/11)
1825 ("\\\\input{\\([^} ]+\\)\\(\\.tps\\)}" 1) ;tgif2tex (1998/9/16)
1827 "See the documentation of YaTeX-processed-file-regexp-alist.")
1829 (defvar YaTeX-file-processor-alist nil
1830 "*Alist of files' processor vs. its extension;
1831 See also the documentation of YaTeX-processed-file-regexp-alist.")
1833 (defvar YaTeX-file-processor-alist-default
1834 '(("tgif" . ".obj")
1835 ("ghostview" . ".ps")
1836 ("ghostview" . ".eps")
1837 (t . ".tex")
1838 (t . ".sty")
1839 (t . ""))
1840 "See the documentation of YaTeX-file-processor-alist.")
1842 (defun YaTeX-goto-corresponding-file-processor (&optional other)
1843 "Execute corresponding file processor."
1844 (save-excursion
1845 (or (looking-at YaTeX-ec-regexp)
1846 (skip-chars-backward (concat "^" YaTeX-ec) (point-beginning-of-line)))
1847 (let ((list (append YaTeX-processed-file-regexp-alist
1848 YaTeX-processed-file-regexp-alist-default))
1849 (p (point)) flist file
1850 (peol (point-end-of-line))
1851 (basedir
1852 (if YaTeX-search-file-from-top-directory
1853 (save-excursion (YaTeX-visit-main t) default-directory)
1854 ".")))
1855 (setq flist (catch 'found
1856 (while list
1857 (goto-char p)
1858 (if (re-search-forward (car (car list)) peol t)
1859 (progn
1860 (setq file (YaTeX-match-string
1861 (car (cdr (car list)))))
1862 (throw 'found (cdr (car list)))))
1863 (setq list (cdr list)))))
1864 (if flist ;if pattern and file name found
1865 (let*((plist (append YaTeX-file-processor-alist
1866 YaTeX-file-processor-alist-default))
1867 (plist0 plist)
1868 ext cmd src buf (alt (car (cdr flist))))
1869 (if (and (re-search-forward
1870 (concat YaTeX-comment-prefix "\\s *\\(.*\\)$") peol t)
1871 (assoc (setq cmd (YaTeX-match-string 1))
1872 YaTeX-file-processor-alist))
1873 (setq src ;if processor is specified
1874 (concat file
1875 (cdr (assoc cmd YaTeX-file-processor-alist))))
1876 (while plist ;if processor is not specified
1877 (setq ext (cdr (car plist)))
1878 (if (and (string< "" (concat file ext))
1879 (file-exists-p
1880 (expand-file-name (concat file ext) basedir)))
1881 (setq cmd (car (car plist))
1882 src (concat file ext) plist nil))
1883 (setq plist (cdr plist)))
1884 (if (and (null src) alt YaTeX-create-file-prefix-g)
1885 (setq cmd alt
1886 src (concat file (cdr (assoc alt plist0))))))
1887 (if src ;if processor and src file found
1888 (let ((default-directory basedir))
1889 (cond
1890 ((stringp cmd)
1891 (let ((buf (concat "* " cmd " " src " *")))
1892 (YaTeX-system (concat cmd " " src) buf)
1893 t))
1894 ((eq t cmd)
1895 (let ((parent buffer-file-name))
1896 (funcall
1897 (cond
1898 (other 'YaTeX-switch-to-buffer-other-window)
1899 ((get-file-buffer src) 'goto-buffer-window)
1900 (t 'YaTeX-switch-to-buffer))
1901 src)
1902 (or (YaTeX-get-builtin "!")
1903 YaTeX-parent-file
1904 (setq YaTeX-parent-file parent))
1905 t))
1906 ((symbolp cmd)
1907 (cond
1908 ((symbol-function cmd)
1909 (funcall cmd src other)))
1910 t)))))))))
1912 (defun YaTeX-on-section-command-p (command)
1913 "Check if point is on the LaTeX command: COMMAND(regexp).
1914 Return nil if point is not on it. Otherwise return the
1915 number of argument position.
1916 Section command name is stored in match-data #1.
1917 Parsing information is stored to plist.
1918 Macros name stored to propname 'command.
1919 Macro's argument number stored to propname 'argc."
1920 (let ((p (point)) md (parg 0) (argc 1) word (grouping 0) (i 0)
1921 (ec+command (concat YaTeX-ec-regexp "\\(" command "\\)")))
1922 (setplist 'YaTeX-on-section-command-p nil)
1923 (while (setq i (string-match "\\\\(" command i))
1924 (setq grouping (1+ grouping) i (+ i 2)))
1925 (save-excursion
1926 (if (looking-at ec+command) nil
1927 (catch 'found ;caught value has no meaning
1928 ;;(1) looking at current position
1929 (and (looking-at command)
1930 (save-excursion
1931 (while (and (not (bobp)) (looking-at command))
1932 (forward-char -1))
1933 (looking-at ec+command))
1934 (goto-char (match-beginning 0))
1935 (throw 'found t))
1936 ;;(2) search command directly
1937 (skip-chars-forward "^{}[]")
1938 (and (YaTeX-re-search-active-backward
1939 ec+command
1940 YaTeX-comment-prefix nil t)
1941 (>= p (match-beginning 0))
1942 (throw 'found (goto-char (match-beginning 0))))
1943 ;;(3) search token
1944 (goto-char p)
1945 (while t
1946 (if (bobp) (throw 'found nil))
1947 (cond
1948 ((looking-at YaTeX-ec-regexp) (throw 'found t))
1949 ((looking-at "[[{]") nil)
1950 ((looking-at "[]}]")(condition-case nil (up-list -1) (error nil)))
1951 (t (skip-chars-backward " \t\r\n")))
1952 (skip-chars-backward (concat "^ \t\r\n{}[]" YaTeX-ec-regexp))
1953 (or (bobp) (forward-char -1)))))
1954 (if (and
1955 (looking-at (concat ec+command
1956 "\\(\\(\\[[^]]+\\]\\|([0-9,]+)\\)*\\)" ;optional arg
1957 ;"[ \t\n\r]*{[^}]+}")) ;arg braces
1958 "[ \t\n\r]*{[^}]*}")) ;arg braces
1959 (not (YaTeX-lookup-table
1960 (setq word (YaTeX-match-string 1)) 'singlecmd)))
1961 (progn
1962 (setq md (match-data))
1963 (skip-chars-forward "^{")
1964 (if (<= (point) p) (setq parg (1+ parg)))
1965 (setq argc
1966 (or (car (cdr (YaTeX-lookup-table word 'section)))
1967 argc))
1968 (put 'YaTeX-on-section-command-p 'argc argc)
1969 (put 'YaTeX-on-section-command-p 'command argc)
1970 (while (and (>= (setq argc (1- argc)) 0)
1971 (progn (skip-chars-forward " \t\n\r")
1972 (looking-at "{")))
1973 (forward-list 1)
1974 (if (<= (point) p) (setq parg (1+ parg))))
1975 (store-match-data md)
1976 (setq i (+ 2 grouping))
1977 (if (and (match-beginning i)
1978 (>= p (match-beginning i)) (< p (match-end i)))
1979 -1 ;return -1 if point is on optional arg
1980 (if (< p (point)) parg))
1981 )))))
1983 (defun YaTeX-on-maketitle-p ()
1984 "Check if point is on maketitle type commands.
1985 Call this function after YaTeX-on-section-command-p."
1986 (let ((p (point)))
1987 (save-excursion
1988 (or (= (char-after (point)) ?\\ )
1989 (progn
1990 (skip-chars-backward
1991 (concat "^" YaTeX-ec-regexp) (point-beginning-of-line))
1992 (or (bobp) (bolp) (backward-char 1))))
1993 (and (looking-at (concat YaTeX-ec-regexp YaTeX-TeX-token-regexp))
1994 (<= (match-beginning 0) p)
1995 (> (match-end 0) p)))))
1997 (defun YaTeX-on-begin-end-p ()
1998 (save-excursion
1999 (if (and (boundp 'in-leftright-p) in-leftright-p)
2000 ;; Dirty workaround for YaTeX-goto-corresponding-leftright 2003/3/28
2001 (let ((md (match-data))) ; for safety
2002 (if (looking-at YaTeX-ec-regexp)
2003 nil ; stay here
2004 (cond
2005 ((looking-at "\\w") (skip-chars-backward "A-Za-z"))
2006 ((looking-at "\\.()\\[\\]|") (forward-char -1)))
2007 (if (equal (char-after (1- (point)))
2008 (string-to-char YaTeX-ec))
2009 (forward-char -1))))
2010 ;(beginning-of-line)
2011 (if (equal (char-after (point)) ?\\) nil ;stay here
2012 (skip-chars-backward "^\n\\\\")
2013 (or (bolp) (forward-char -1))))
2014 (re-search-forward
2015 ;;"\\\\begin{\\([^}]+\\)}\\|\\\\end{\\([^}]+\\)}"
2016 (concat
2017 (YaTeX-replace-format-args
2018 (regexp-quote YaTeX-struct-begin)
2019 (concat "\\(" YaTeX-struct-name-regexp "\\)") "" "" "")
2020 "\\|"
2021 (YaTeX-replace-format-args
2022 (regexp-quote YaTeX-struct-end)
2023 (concat "\\(" YaTeX-struct-name-regexp "\\)") "" "" "")
2024 "\\|\\("
2025 YaTeX-ec-regexp ;;"[][()]\\)"
2026 "[][]\\)"
2028 (point-end-of-line) t)))
2030 (defun YaTeX-on-includes-p ()
2031 (save-excursion
2032 (beginning-of-line)
2033 (re-search-forward "\\(\\(include[^}]*\\)\\|\\(input\\)\\){[^}]*}"
2034 (point-end-of-line) t)))
2036 (defun YaTeX-on-comment-p (&optional sw)
2037 "Return t if current line is commented out.
2038 Optional argument SW t to treat all `%' lines as comment,
2039 even if on `%#' notation."
2040 (save-excursion
2041 (beginning-of-line)
2042 (skip-chars-forward "\\s ")
2043 (looking-at (if sw "%" "%[^#]"))))
2045 (defun YaTeX-on-BEGIN-END-p ()
2046 (save-excursion
2047 (let ((case-fold-search nil))
2048 (beginning-of-line)
2049 (re-search-forward
2050 "\\(%#BEGIN\\)\\|\\(%#END\\)" (point-end-of-line) t))))
2052 (defun YaTeX-goto-corresponding-* (arg)
2053 "Parse current line and call suitable function."
2054 (interactive "P")
2055 (let (mm)
2056 (cond
2057 ((YaTeX-goto-corresponding-label arg))
2058 ((YaTeX-goto-corresponding-environment))
2059 ((YaTeX-goto-corresponding-file-processor arg))
2060 ((YaTeX-goto-corresponding-file arg))
2061 ((YaTeX-goto-corresponding-BEGIN-END))
2062 ((and (setq mm (YaTeX-in-math-mode-p))
2063 (YaTeX-goto-corresponding-leftright)))
2064 ((and ;;mm YaTeX-use-AMS-LaTeX
2065 (YaTeX-goto-corresponding-paren)))
2066 ;;((and (string-match
2067 ;; YaTeX-equation-env-regexp ;to delay loading
2068 ;; (or (YaTeX-inner-environment t) "document"))
2069 ;; (YaTeX-goto-corresponding-leftright)))
2070 (t (message "I don't know where to go.")))))
2072 (defun YaTeX-goto-corresponding-*-other-window (arg)
2073 "Parse current line and call suitable function."
2074 (interactive "P")
2075 (cond
2076 ((YaTeX-goto-corresponding-label arg t))
2077 ;;((YaTeX-goto-corresponding-environment))
2078 ((YaTeX-goto-corresponding-file t))
2079 ;;((YaTeX-goto-corresponding-BEGIN-END))
2080 (t (message "I don't know where to go."))))
2082 (defun YaTeX-comment-region (alt-prefix)
2083 "Comment out region by '%'.
2084 If you call this function on the 'begin{}' or 'end{}' line,
2085 it comments out whole environment"
2086 (interactive "P")
2087 (if (not (YaTeX-on-begin-end-p))
2088 (comment-out-region
2089 (if alt-prefix
2090 (read-string "Insert prefix: ")
2091 YaTeX-comment-prefix))
2092 (YaTeX-comment-uncomment-env 'comment-out-region)))
2094 (defun YaTeX-uncomment-region (alt-prefix)
2095 "Uncomment out region by '%'."
2096 (interactive "P")
2097 (if (not (YaTeX-on-begin-end-p))
2098 (uncomment-out-region
2099 (if alt-prefix (read-string "Remove prefix: ")
2100 YaTeX-comment-prefix)
2101 (region-beginning) (region-end) YaTeX-uncomment-once)
2102 (YaTeX-comment-uncomment-env 'uncomment-out-region)))
2104 (defun YaTeX-comment-uncomment-env (func)
2105 "Comment or uncomment out one LaTeX environment switching function by FUNC."
2106 (let (beg (p (point)))
2107 (save-excursion
2108 (beginning-of-line)
2109 (setq beg (point))
2110 (YaTeX-goto-corresponding-environment)
2111 (beginning-of-line)
2112 (if (> p (point)) (setq beg (1+ beg)) (forward-char 1))
2113 (funcall func YaTeX-comment-prefix beg (point) YaTeX-uncomment-once)))
2114 (message "%sommented out current environment."
2115 (if (eq func 'comment-out-region) "C" "Un-c")))
2117 (defun YaTeX-comment-paragraph ()
2118 "Comment out current paragraph."
2119 (interactive)
2120 (save-excursion
2121 (cond
2122 ((YaTeX-on-begin-end-p)
2123 (beginning-of-line)
2124 (insert YaTeX-comment-prefix)
2125 (YaTeX-goto-corresponding-environment)
2126 (beginning-of-line)
2127 (insert YaTeX-comment-prefix))
2128 ((YaTeX-on-comment-p)
2129 (message "Already commented out."))
2130 (t
2131 (mark-paragraph)
2132 (if (looking-at paragraph-separate) (forward-line 1))
2133 (comment-out-region "%")))))
2135 (defun YaTeX-uncomment-paragraph ()
2136 "Uncomment current paragraph."
2137 (interactive)
2138 (save-excursion
2139 (if (YaTeX-on-begin-end-p)
2140 (let ((p (point-marker)))
2141 (YaTeX-goto-corresponding-environment)
2142 (YaTeX-remove-prefix YaTeX-comment-prefix YaTeX-uncomment-once)
2143 (goto-char p)
2144 (YaTeX-remove-prefix YaTeX-comment-prefix YaTeX-uncomment-once)
2145 (set-marker p nil))
2146 (if (YaTeX-on-comment-p)
2147 (let*((fill-prefix "")
2148 ;;append `^%' to head of paragraph delimiter.
2149 (paragraph-start
2150 (concat
2151 "^$\\|^%\\(" YaTeX-paragraph-separate "\\)"))
2152 (paragraph-separate paragraph-start))
2153 (mark-paragraph)
2154 (if (not (bobp)) (forward-line 1))
2155 (uncomment-out-region "%" nil nil YaTeX-uncomment-once))
2156 (message "This line is not a comment line.")))))
2158 (defun YaTeX-remove-prefix (prefix &optional once)
2159 "Remove prefix on current line as far as prefix detected. But
2160 optional argument ONCE makes deletion once."
2161 (interactive "sPrefix:")
2162 (beginning-of-line)
2163 (while (re-search-forward (concat "^" prefix) (point-end-of-line) t)
2164 (replace-match "")
2165 (if once (end-of-line))))
2167 (defun YaTeX-kill-some-pairs (predicate gofunc kill-contents)
2168 "Kill some matching pair.
2169 This function assumes that pairs occupy whole of each line where they resid."
2170 (if (not (funcall predicate)) nil
2171 (let ((b1 (match-beginning 0)) (e1 (match-end 0))
2172 b2 e2)
2173 (save-excursion
2174 (funcall gofunc)
2175 (funcall predicate) ;get match data
2176 (if (< (point) e1) ;if currently on begin-line
2177 (progn
2178 (setq b2 b1 e2 e1
2179 b1 (match-beginning 0) e1 (match-end 0))
2180 (goto-char e2)) ;goto end-line's end
2181 (setq b2 (match-beginning 0)
2182 e2 (match-end 0))
2183 (goto-char e2)) ;now e2 has surely end-line's end
2184 (skip-chars-forward " \t")
2185 (and (eolp)
2186 (not (eobp))
2187 (setq e2 (1+ (point))))
2188 (if (not kill-contents)
2189 (kill-region
2190 (progn
2191 (goto-char b2)
2192 (skip-chars-backward " \t%")
2193 (if (bolp) (point) b2))
2194 e2))
2195 (goto-char b1)
2196 (skip-chars-backward " \t%")
2197 (if (not kill-contents)
2198 (progn
2199 (kill-append
2200 (buffer-substring
2201 (setq b1 (if (bolp) (point) b1))
2202 (setq e1
2203 (progn
2204 (goto-char e1)
2205 (while (looking-at "{\\| \t")
2206 (forward-list 1))
2207 (skip-chars-forward " \t")
2208 (if (and (eolp) (not (eobp)))
2209 (1+ (point))
2210 (point)))))
2211 t)
2212 (delete-region b1 e1))
2213 (kill-region
2214 (if (bolp) (point) b1)
2215 e2)))
2216 t)))
2218 (defun YaTeX-kill-section-command (point kill-all)
2219 "Kill section-type command at POINT leaving its last argument.
2220 Non-nil for the second argument kill its last argument too."
2221 (let ((cmd (get 'YaTeX-on-section-command-p 'command))
2222 (argc (get 'YaTeX-on-section-command-p 'argc))
2223 beg (end (make-marker)))
2224 (save-excursion
2225 (goto-char point)
2226 (or (looking-at YaTeX-ec-regexp)
2227 (progn
2228 (skip-chars-backward (concat "^" YaTeX-ec-regexp))
2229 (forward-char -1)))
2230 (setq beg (point))
2231 (skip-chars-forward "^{")
2232 (while (> (setq argc (1- argc)) 0)
2233 (skip-chars-forward "^{")
2234 (forward-list 1))
2235 (kill-region beg (point))
2236 (forward-list 1)
2237 (set-marker end (point))
2238 (if kill-all
2239 (progn
2240 (kill-append (buffer-substring beg end) nil)
2241 (delete-region beg end))
2242 (goto-char beg)
2243 (kill-append
2244 (buffer-substring
2245 (point) (progn (skip-chars-forward "^{" end) (1+ (point))))
2246 nil)
2247 (delete-region beg (1+ (point)))
2248 (goto-char end)
2249 (set-marker end nil)
2250 (kill-append (buffer-substring (point) (1- (point))) nil)
2251 (delete-backward-char 1)))))
2253 (defun YaTeX-kill-paren (kill-contents)
2254 "Kill parentheses leaving its contents.
2255 But kill its contents if the argument KILL-CONTENTS is non-nil."
2256 (interactive "P")
2257 (let (p bsl (backslash-syntax (char-to-string (char-syntax ?\\)))
2258 (md (match-data)))
2259 (unwind-protect
2260 (save-excursion
2261 (modify-syntax-entry ?\\ " ")
2262 (if (looking-at "\\s(\\|\\(\\s)\\)")
2263 (progn
2264 (if (match-beginning 1)
2265 (up-list -1))
2266 (if (and (> (point) (point-min))
2267 (= (char-after (1- (point))) ?\\ ))
2268 (setq p (1- (point)) bsl t)
2269 (setq p (point)))
2270 (forward-list 1)
2271 ;(YaTeX-goto-open-paren t)
2272 (if kill-contents (delete-region p (point))
2273 (backward-delete-char 1)
2274 (cond
2275 ((save-excursion
2276 (forward-char -2)
2277 (looking-at (concat YaTeX-ec-regexp "/")))
2278 (backward-delete-char 2))
2279 ((= (char-after (1- (point))) ?\\)
2280 (backward-delete-char 1)))
2281 (goto-char p)
2282 (if (looking-at
2283 (concat "{" YaTeX-ec-regexp
2284 YaTeX-command-token-regexp "+"
2285 "\\s +"))
2286 (delete-region (point) (match-end 0))
2287 (delete-char 1)
2288 (if bsl (delete-char 1))))
2289 t)))
2290 (modify-syntax-entry ?\\ backslash-syntax)
2291 (store-match-data md))))
2293 (defvar YaTeX-read-environment-history nil "Holds history of environments.")
2294 (put 'YaTeX-read-environment-history 'no-default t)
2295 (defun YaTeX-read-environment (prompt &optional predicate must-match initial)
2296 "Read a LaTeX environment name with completion."
2297 (YaTeX-sync-local-table 'tmp-env-table)
2298 (completing-read-with-history
2299 prompt
2300 (append tmp-env-table user-env-table env-table)
2301 predicate must-match initial
2302 'YaTeX-read-environment-history))
2304 (defvar YaTeX-read-section-history nil "Holds history of section-types.")
2305 (put 'YaTeX-read-section-history 'no-default t)
2306 (defun YaTeX-read-section (prompt &optional predicate initial)
2307 "Read a LaTeX section-type command with completion."
2308 (YaTeX-sync-local-table 'tmp-section-table)
2309 (let ((minibuffer-completion-table
2310 (append tmp-section-table user-section-table section-table)))
2311 (read-from-minibuffer-with-history
2312 prompt initial YaTeX-section-completion-map nil
2313 'YaTeX-read-section-history)))
2315 (defun YaTeX-read-section-with-overview ()
2316 "Read sectioning command with overview.
2317 This function refers a local variable `source-window' in YaTeX-make-section,
2318 because this function is called with no argument."
2319 (interactive)
2320 (require 'yatexsec) ;some case needs this
2321 (if (> (minibuffer-depth) 1)
2322 (error "Too many minibuffer levels for overview."))
2323 (let ((sw (selected-window))
2324 (minibuffer-max-depth nil) ; for XEmacs20
2325 (enable-recursive-minibuffers t) sect)
2326 (unwind-protect
2327 (progn
2328 (select-window source-window)
2329 (setq sect (YaTeX-read-section-in-minibuffer
2330 "Sectioning(Up=C-p, Down=C-n, Help=?): "
2331 YaTeX-sectioning-level (YaTeX-section-overview))))
2332 (select-window sw))
2333 (YaTeX-minibuffer-erase)
2334 (insert sect)
2335 (exit-minibuffer)))
2337 (defvar YaTeX-read-fontsize-history nil "Holds history of font designator.")
2338 (put 'YaTeX-read-fontsize-history 'no-default t)
2339 (defun YaTeX-read-fontsize (prompt &optional predicate must-match initial)
2340 "Read a LaTeX font changing command with completion."
2341 (YaTeX-sync-local-table 'tmp-fontsize-table)
2342 (completing-read-with-history
2343 prompt (append tmp-fontsize-table user-fontsize-table fontsize-table)
2344 predicate must-match initial 'YaTeX-read-fontsize-history))
2346 (defun YaTeX-change-environment ()
2347 "Change the name of environment."
2348 (interactive)
2349 (if (not (YaTeX-on-begin-end-p)) nil
2350 (save-excursion
2351 (let (p env newenv (m1 (match-beginning 1)) (m2 (match-beginning 2)))
2352 (setq env (if m1 (YaTeX-buffer-substring m1 (match-end 1))
2353 (YaTeX-buffer-substring m2 (match-end 2))))
2354 (goto-char (match-beginning 0))
2355 (set-mark-command nil)
2356 (YaTeX-goto-corresponding-environment)
2357 (setq newenv (YaTeX-read-environment
2358 (format "Change environment `%s' to: " env)))
2359 (cond
2360 ((string= newenv "") (message "Change environment cancelled."))
2361 ((string= newenv env) (message "No need to change."))
2362 (t
2363 (search-forward (concat "{" env) (point-end-of-line) t)
2364 (replace-match (concat "{" newenv) t)
2365 (exchange-point-and-mark)
2366 (search-forward (concat "{" env) (point-end-of-line) t)
2367 (replace-match (concat "{" newenv) t)))
2368 t))))
2370 (defun YaTeX-change-section ()
2371 "Change section-type command."
2372 (interactive)
2373 (let*((where (YaTeX-on-section-command-p YaTeX-command-token-regexp))
2374 (p (point)) (cmd (YaTeX-match-string 1))
2375 (beg (make-marker)) (end (make-marker)) old new)
2376 (if (null where) nil
2377 (unwind-protect
2378 (let ((source-window (selected-window)))
2379 (cond
2380 ((equal where 0);;if point is on section command
2381 (set-marker beg (match-beginning 1))
2382 (set-marker end (match-end 1))
2383 (goto-char beg) ;beginning of the command
2384 (setq new (YaTeX-read-section
2385 (format "Change `%s' to: " cmd) nil)
2386 old cmd))
2388 ((= where -1);;if point is on a optional parameter
2389 (set-marker beg (match-beginning 2))
2390 (skip-chars-forward "^{")
2391 (set-marker end (point))
2392 (goto-char p)
2393 (setq new
2394 (if (fboundp (intern-soft (concat YaTeX-addin-prefix cmd)))
2395 (YaTeX-addin cmd)
2396 (concat "["
2397 (read-string
2398 (format "Change `%s' to: "
2399 (setq old (YaTeX-buffer-substring
2400 (1+ beg) (1- end)))))
2401 "]"))))
2403 ((> where 0);;if point is in arguments' braces
2404 (or (looking-at "{")
2405 (progn (skip-chars-backward "^{") (forward-char -1)))
2406 (set-marker beg (1+ (point)))
2407 (forward-list 1)
2408 (forward-char -1)
2409 (set-marker end (point))
2410 (setq old (YaTeX-buffer-substring beg end))
2411 (goto-char p)
2412 (if (> (length old) 40)
2413 (setq old (concat (substring old 0 12) "..."
2414 (substring old -12))))
2415 (setq new
2416 (if (intern-soft (concat "YaTeX::" cmd))
2417 (funcall (intern-soft (concat "YaTeX::" cmd)) where)
2418 (read-string (format "Change `%s' to: " old)))))
2419 ) ;cond
2420 (if (string= old new)
2421 nil ;do not replace
2422 (delete-region beg end)
2423 (goto-char beg)
2424 (insert-before-markers new)))
2425 (set-marker beg nil)
2426 (set-marker end nil))
2427 ;;(goto-char (marker-position p))
2428 new)))
2430 (defun YaTeX-change-fontsize ()
2431 "Change large-type command."
2432 (let ((lt (append tmp-fontsize-table user-fontsize-table fontsize-table))
2433 (p (point)) large old new beg end)
2434 ;;(and (looking-at "}") (up-list -1))
2435 ;;(and (looking-at "{") (forward-char 1))
2436 ;;Is above convenient?
2437 (save-excursion
2438 (or (looking-at YaTeX-ec-regexp)
2439 (progn
2440 (skip-chars-backward (concat "^" YaTeX-ec-regexp))
2441 (forward-char -1)))
2442 (cond
2443 ((and
2444 (looking-at
2445 (concat YaTeX-ec-regexp "\\(" YaTeX-TeX-token-regexp "\\)"))
2446 (< p (match-end 0))
2447 (assoc (setq old (YaTeX-match-string 1)) lt))
2448 (goto-char p)
2449 (setq beg (match-beginning 1) end (match-end 1) ;save match position
2450 new (completing-read
2451 (format "Change font/size `%s' to : " old) lt))
2452 (delete-region beg end)
2453 (goto-char beg)
2454 (insert-before-markers new)
2455 new)
2456 (t nil)
2457 ))))
2459 (defun YaTeX-change-math-image ()
2460 "Change with image completion."
2461 (let (maketitle memberp beg end)
2462 (if (and (YaTeX-on-maketitle-p)
2463 (progn
2464 (setq maketitle (substring (YaTeX-match-string 0) 1))
2465 (setq memberp (YaTeX-math-member-p maketitle))))
2466 (let ((last-command-char (string-to-char (car memberp))))
2467 (setq beg (match-beginning 0) end (match-end 0))
2468 (delete-region beg end)
2469 (YaTeX-math-insert-sequence t (cdr memberp))))))
2471 (defun YaTeX-kill-* (&optional arg)
2472 "Parse current line and call suitable function.
2473 Non-nil for ARG kills its contents too."
2474 (interactive "P")
2475 (cond
2476 ((YaTeX-kill-some-pairs 'YaTeX-on-begin-end-p
2477 'YaTeX-goto-corresponding-environment arg))
2478 ((YaTeX-kill-some-pairs 'YaTeX-on-BEGIN-END-p
2479 'YaTeX-goto-corresponding-BEGIN-END arg))
2480 ((YaTeX-on-section-command-p YaTeX-command-token-regexp);on any command
2481 (YaTeX-kill-section-command (match-beginning 0) arg))
2482 ((YaTeX-kill-paren arg))
2483 (t (message "I don't know what to kill."))))
2485 (defun YaTeX-change-* ()
2486 "Parse current line and call suitable function."
2487 (interactive)
2488 (cond
2489 ((YaTeX-change-parentheses))
2490 ((YaTeX-change-environment))
2491 ((YaTeX-change-section))
2492 ((YaTeX-change-fontsize))
2493 ((YaTeX-change-math-image))
2494 (t (message "I don't know what to change."))))
2496 ;;;
2497 ;Check availability of add-in functions
2498 ;;;
2499 (cond
2500 ((featurep 'yatexadd) nil) ;Already provided.
2501 ((progn (load "yatexadd" t) (featurep 'yatexadd)) nil)
2502 (t (message "YaTeX add-in functions not supplied.")))
2504 (defun YaTeX-addin (name)
2505 "Check availability of addin function and call it if exists."
2506 (if (and (not (get 'YaTeX-generate 'disabled))
2507 (intern-soft (concat YaTeX-addin-prefix name))
2508 (fboundp (intern-soft (concat YaTeX-addin-prefix name))))
2509 (let ((s (funcall (intern (concat YaTeX-addin-prefix name)))))
2510 (if (stringp s) s ""))
2511 "")) ;Add in function is not bound.
2514 (defun YaTeX-on-item-p (&optional point)
2515 "Return t if POINT (default is (point)) is on \\item."
2516 (let ((p (or point (point))))
2517 (save-excursion
2518 (goto-char p)
2519 (end-of-line)
2520 (setq p (point))
2521 (re-search-backward YaTeX-paragraph-delimiter nil t)
2522 (re-search-forward YaTeX-item-regexp p t))))
2524 (defun YaTeX-in-verb-p (&optional point)
2525 "Check if POINT is in verb or verb*. Default of POINT is (point)."
2526 (setq point (or point (point)))
2527 (save-excursion
2528 (let ((md (match-data)))
2529 (goto-char point)
2530 (unwind-protect
2531 (if (not (re-search-backward
2532 (concat YaTeX-ec-regexp
2533 "\\(" YaTeX-verb-regexp "\\)"
2534 "\\([^-A-Za-z_*]\\)")
2535 (point-beginning-of-line) t))
2536 nil
2537 (goto-char (match-end 2))
2538 (skip-chars-forward
2539 (concat "^" (YaTeX-buffer-substring
2540 (match-beginning 2) (match-end 2))))
2541 (and (< (match-beginning 2) point) (< (1- point) (point))))
2542 (store-match-data md)))))
2544 (defun YaTeX-literal-p (&optional point)
2545 "Check if POINT is in verb or verb* or verbatime environment family.
2546 Default of POINT is (point)."
2547 (let ((md (match-data)))
2548 (unwind-protect
2549 (cond
2550 ((equal YaTeX-ec "\\") ;maybe LaTeX
2551 (save-excursion
2552 (and point (goto-char point))
2553 (or (YaTeX-in-verb-p (point))
2554 (and (not (looking-at "\\\\end{verb"))
2555 (YaTeX-quick-in-environment-p
2556 YaTeX-verbatim-environments))))))
2557 (store-match-data md))))
2559 (defun YaTeX-in-environment-p (env)
2560 "Return if current LaTeX environment is ENV.
2561 ENV is given in the form of environment's name or its list."
2562 (let ((md (match-data)) (nest 0) p envrx)
2563 (cond
2564 ((atom env)
2565 (setq envrx
2566 (concat "\\("
2567 (regexp-quote
2568 (YaTeX-replace-format-args
2569 YaTeX-struct-begin env "" ""))
2570 "\\)\\|\\("
2571 (regexp-quote
2572 (YaTeX-replace-format-args
2573 YaTeX-struct-end env "" ""))
2574 "\\)"))
2575 (save-excursion
2576 (setq p (catch 'open
2577 (while (YaTeX-re-search-active-backward
2578 envrx YaTeX-comment-prefix nil t)
2579 (if (match-beginning 2)
2580 (setq nest (1+ nest))
2581 (setq nest (1- nest)))
2582 (if (< nest 0)
2583 (throw 'open (cons env (match-beginning 0)))))))))
2584 ((listp env)
2585 (setq p
2586 (or (YaTeX-in-environment-p (car env))
2587 (and (cdr env) (YaTeX-in-environment-p (cdr env)))))))
2588 (store-match-data md)
2589 p;(or p (YaTeX-in-verb-p (match-beginning 0)))
2590 ))
2592 (defun YaTeX-quick-in-environment-p (env)
2593 "Check quickly but unsure if current environment is ENV.
2594 ENV is given in the form of environment's name or its list.
2595 This function returns correct result only if ENV is NOT nested."
2596 (save-excursion
2597 (let ((md (match-data)) m0 (p (point)) rc clfound)
2598 (cond
2599 ((listp env)
2600 (or (YaTeX-quick-in-environment-p (car env))
2601 (and (cdr env) (YaTeX-quick-in-environment-p (cdr env)))))
2602 (t
2603 (unwind-protect
2604 (if (prog1
2605 (YaTeX-search-active-backward
2606 (YaTeX-replace-format-args YaTeX-struct-begin env "" "")
2607 YaTeX-comment-prefix nil t)
2608 (setq m0 (match-beginning 0)))
2609 (if (YaTeX-search-active-forward
2610 (YaTeX-replace-format-args
2611 YaTeX-struct-end env)
2612 YaTeX-comment-prefix p t nil)
2613 nil ;if \end{env} found, return nil
2614 (cons env m0))) ;else, return meaningful values
2615 (store-match-data md)))))))
2617 ;; Filling \item
2618 (defun YaTeX-remove-trailing-comment (start end)
2619 "Remove trailing comment from START to end."
2620 (save-excursion
2621 (let ((trcom (concat YaTeX-comment-prefix "$")))
2622 (goto-char start)
2623 (while (re-search-forward trcom end t)
2624 (if (/= (char-after (1- (match-beginning 0))) ?\\ )
2625 (replace-match "\\1"))))))
2627 (defvar YaTeX-itemize-withlabel-max-indent-depth 8)
2628 (defun YaTeX-get-item-info (&optional recent thisenv)
2629 "Return the list of the beginning of \\item and column of its item.
2630 If it seems to be outside of itemizing environment, just return nil.
2631 Non-nil for optional argument RECENT refers recent \\item.
2632 Optional second argument THISENV omits calling YaTeX-inner-environment."
2633 (save-excursion
2634 (let* ((p (point)) env e0 c cc md
2635 (bndry (and (setq env (or thisenv (YaTeX-inner-environment t)))
2636 (get 'YaTeX-inner-environment 'point))))
2637 (end-of-line)
2638 (if (if recent
2639 (catch 'found
2640 (while (YaTeX-re-search-active-backward
2641 YaTeX-item-regexp YaTeX-comment-prefix bndry t)
2642 (setq md (match-data))
2643 (YaTeX-inner-environment t)
2644 (store-match-data md)
2645 (if (= bndry (get 'YaTeX-inner-environment 'point))
2646 (throw 'found t))))
2647 (goto-char bndry)
2648 (YaTeX-re-search-active-forward
2649 YaTeX-item-regexp YaTeX-comment-prefix p t))
2650 (progn
2651 (goto-char (match-end 0))
2652 ;(setq c (current-column))
2653 (if (string-match "desc" env)
2654 (setq c 6)
2655 (setq cc (current-column))
2656 (if (equal (following-char) ?\[) (forward-list 1))
2657 (if (< (- (current-column) cc)
2658 YaTeX-itemize-withlabel-max-indent-depth)
2659 (setq c 0)
2660 (move-to-column cc)
2661 (setq c YaTeX-itemize-withlabel-max-indent-depth)))
2662 (skip-chars-forward " \t" (point-end-of-line))
2663 (list (point-beginning-of-line) (+ c (current-column))))))))
2665 (defun YaTeX-fill-item ()
2666 "Fill item in itemize environment."
2667 (interactive)
2668 (save-excursion
2669 (let* ((p (point))
2670 (item-term (concat
2671 "\\(^[ \t]*$\\)\\|" YaTeX-item-regexp "\\|\\("
2672 YaTeX-ec-regexp "\\(begin\\|end\\)\\)"))
2673 ;;This value depends on LaTeX.
2674 fill-prefix start col
2675 (info (YaTeX-get-item-info t)))
2676 (if (null info) nil ;not on \item, do nothing
2677 (setq start (car info)
2678 col (car (cdr info)))
2679 (save-excursion
2680 (if (re-search-backward "^\\s *$" start t)
2681 ;;if separated from \item line, isolate this block
2682 (progn
2683 (setq start (1+ (match-end 0)))
2684 (goto-char start)
2685 (skip-chars-forward " \t")
2686 (delete-region (point) start) ;is this your favor???
2687 (indent-to col))))
2688 (beginning-of-line)
2689 (if (<= (save-excursion
2690 (re-search-forward
2691 (concat "\\\\end{\\|\\\\begin{\\|^[ \t]*$") nil t)
2692 (match-beginning 0))
2693 p)
2694 (progn (message "Not on itemize.") nil)
2695 (end-of-line)
2696 (newline)
2697 (indent-to col)
2698 (setq fill-prefix
2699 (YaTeX-buffer-substring (point-beginning-of-line)(point)))
2700 (beginning-of-line)
2701 (delete-region (point) (progn (forward-line 1) (point)))
2702 (re-search-forward item-term nil 1)
2703 (YaTeX-remove-trailing-comment start (point))
2704 (beginning-of-line)
2705 (push-mark (point) t)
2706 (goto-char start)
2707 (forward-line 1)
2708 (while (< (point) (mark))
2709 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
2710 (forward-line 1))
2711 (fill-region-as-paragraph start (mark))
2712 (if NTT-jTeX
2713 (while (progn(forward-line -1)(end-of-line) (> (point) start))
2714 (insert ?%)))
2715 (pop-mark))))))
2717 (defun YaTeX-fill-paragraph (arg)
2718 "YaTeX adjustment function for fill-paragraph.
2719 *Protect \\verb from unexpected broken up."
2720 (interactive "P")
2721 (cond
2722 ((not (eq major-mode 'yatex-mode)) (fill-paragraph arg))
2723 ((YaTeX-quick-in-environment-p YaTeX-fill-inhibit-environments) nil)
2724 ((YaTeX-in-math-mode-p) nil)
2725 (t
2726 (save-excursion
2727 (let*((verbrex (concat YaTeX-ec-regexp
2728 "\\(" YaTeX-verb-regexp "\\)" ;match#1
2729 "\\(.\\).*\\(\\2\\)")) ;match #2 and #3
2730 (tilderex (concat "\\("
2731 YaTeX-kanji-regexp "~"
2732 "\\)" YaTeX-ec-regexp
2733 "\\|\\("
2734 "~" YaTeX-kanji-regexp
2735 "\\)"))
2736 (p (point)) ii end poslist spacelist lenlist b e n
2737 (fill-prefix fill-prefix)
2738 (inenv (or (YaTeX-inner-environment t) "document"))
2739 (border (get 'YaTeX-inner-environment 'point)))
2740 (cond
2741 ((save-excursion (beginning-of-line) ;if point is on the first
2742 (setq end (point)) ;non-whitespace char
2743 (skip-chars-forward " \t")
2744 (equal (point) p))
2745 (setq fill-prefix (YaTeX-buffer-substring p end)))
2746 ((and ;;(not YaTeX-emacs-19)
2747 (string-match YaTeX-itemizing-env-regexp inenv)
2748 (setq ii (YaTeX-get-item-info)))
2749 (save-excursion
2750 (beginning-of-line)
2751 (indent-to-column (car (cdr ii)))
2752 (setq fill-prefix
2753 (YaTeX-buffer-substring (point) (point-beginning-of-line)))
2754 (delete-region (point) (progn (beginning-of-line) (point))))))
2755 (cond
2756 ((string-match "tabular" inenv)
2757 (let ((b (point-beginning-of-line))
2758 (e (point-end-of-line)))
2759 (if (re-search-backward
2760 "&\\|\\\\\\\\\\|\\\\\\(begin\\|end\\){" border t)
2761 (setq b (if (match-beginning 1)
2762 (progn (forward-line 1) (point))
2763 (point-beginning-of-line))))
2764 (goto-char p)
2765 (if (re-search-forward
2766 "&\\|\\\\\\\\\\|\\\\\\(end\\|begin\\){" nil t)
2767 (setq e (if (match-beginning 1)
2768 (progn (forward-line -1)
2769 (point-end-of-line))
2770 (match-beginning 0))))
2771 (set-mark e)
2772 (goto-char b)))
2773 (t
2774 (mark-paragraph)))
2775 (save-restriction
2776 (narrow-to-region (region-beginning) (region-end))
2777 (YaTeX-remove-trailing-comment (point-min) (point-max))
2778 ;; First, replace spaces in verb to _ temporarily.
2779 (goto-char (point-min))
2780 (while (YaTeX-re-search-active-forward
2781 verbrex YaTeX-comment-prefix (point-max) t)
2782 (setq end (match-beginning 3))
2783 (goto-char (match-beginning 2))
2784 (while (re-search-forward "\\s " end t)
2785 (setq poslist (cons (make-marker) poslist)
2786 spacelist (cons (preceding-char) spacelist)
2787 lenlist (cons 1 lenlist))
2788 (replace-match "_")
2789 (set-marker (car poslist) (match-beginning 0))))
2790 ;; Second, replace "表~\ref{...}" to "\\\ref{...}"
2791 (goto-char (point-min))
2792 (while (YaTeX-re-search-active-forward
2793 tilderex YaTeX-comment-prefix (point-max) t)
2794 (if (match-beginning 1)
2795 (setq b (match-beginning 1) e (match-end 1) n 1)
2796 (setq b (match-beginning 2) e (match-end 2) n 2))
2797 (setq poslist (cons (make-marker) poslist)
2798 spacelist (cons (YaTeX-match-string n) spacelist)
2799 lenlist (cons 2 lenlist))
2800 (goto-char (match-beginning 0))
2801 (delete-region (point) e)
2802 (insert YaTeX-ec YaTeX-ec) ;set-marker should be here
2803 (set-marker (car poslist) b))
2804 ;;(fill-paragraph arg)
2805 (fill-region-as-paragraph (point-min) (point-max) arg)
2806 (while spacelist
2807 (goto-char (car poslist))
2808 (set-marker (car poslist) nil)
2809 (and (eolp) (skip-chars-forward "\n\t "))
2810 (delete-char (car lenlist))
2811 (insert (car spacelist))
2812 (setq spacelist (cdr spacelist)
2813 poslist (cdr poslist)
2814 lenlist (cdr lenlist)))
2815 (goto-char (point-min))
2816 (forward-word 1)
2817 (beginning-of-line)
2818 (while (re-search-forward "\\\\\\([a-z]*ref\\|cite\\){" nil t)
2819 (if (< (point-end-of-line)
2820 (save-excursion (forward-char -1) (forward-list 1) (point)))
2821 (progn (end-of-line)
2822 (if (save-excursion
2823 (backward-word 1)
2824 (looking-at "[^0-9A-z!-)]"))
2825 (insert YaTeX-comment-prefix)))))
2826 ;; Nonbreak space `~'
2827 (goto-char (point-min))
2828 (while (YaTeX-re-search-active-forward
2829 "~\\(\\s *\\)$" YaTeX-comment-prefix (point-max) t)
2830 (delete-region (match-beginning 1) (match-end 1))
2831 (insert YaTeX-comment-prefix))
2832 (goto-char (point-min))
2833 (if (and NTT-jTeX (looking-at "[ \t]\\|^$"))
2834 (progn
2835 (goto-char (point-min))
2836 (while (not (eobp))
2837 (end-of-line)
2838 (or (bolp)
2839 (save-excursion
2840 (backward-word 1)
2841 (looking-at "[0-9A-z!-)]")) ;is not japanese string
2842 (progn (setq p (point)) (insert YaTeX-comment-prefix)))
2843 (forward-line 1))
2844 (goto-char p)
2845 (if (looking-at "%") (delete-char 1)) ;remove last inserted `%'
2846 ))))))))
2848 (if (fboundp 'YaTeX-saved-indent-new-comment-line) nil
2849 (fset 'YaTeX-saved-indent-new-comment-line
2850 (symbol-function 'indent-new-comment-line))
2851 (fset 'indent-new-comment-line 'YaTeX-indent-new-comment-line))
2853 (defun YaTeX-indent-new-comment-line (&optional soft)
2854 "Tuned `indent-new-comment-line' function for yatex.
2855 See the documentation of `YaTeX-saved-indent-new-comment-line'."
2856 (interactive)
2857 (cond
2858 ((or (not (memq major-mode '(yatex-mode yahtml-mode)))
2859 (string-match
2860 "document"
2861 (or (and (boundp 'inenv) inenv)
2862 (or (YaTeX-inner-environment t) "document"))))
2863 (apply 'YaTeX-saved-indent-new-comment-line (if soft (list soft))))
2864 ; ((and (eq major-mode 'yahtml-mode)
2865 ; (string-match
2866 ; "^[Pp][Rr][Ee]" (yahtml-inner-environment-but "^[Aa]\\b" t)))
2867 ; (yahtml-indent-new-commnet-line))
2868 ((and (eq major-mode 'yatex-mode) ;1997/2/4
2869 (YaTeX-in-math-mode-p)) nil) ;1996/12/30
2870 (t (let (fill-prefix)
2871 (apply 'YaTeX-saved-indent-new-comment-line (if soft (list soft)))))))
2873 (defun YaTeX-fill-* ()
2874 "Fill paragraph according to its condition."
2875 (interactive)
2876 (cond
2877 ((YaTeX-fill-item))
2878 ))
2880 ;; Accent completion
2881 (defun YaTeX-read-accent-char (x)
2882 "Read char in accent braces."
2883 (let ((c (read-char)))
2884 (concat
2885 (if (and (or (= c ?i) (= c ?j))
2886 (not (string-match (regexp-quote x) "cdb")))
2887 "\\" "")
2888 (char-to-string c))))
2890 (defun YaTeX-make-accent ()
2891 "Make accent usage."
2892 (interactive)
2893 (message "1:` 2:' 3:^ 4:\" 5:~ 6:= 7:. u v H t c d b")
2894 (let ((c (read-char))(case-fold-search nil))
2895 (setq c (cond ((and (> c ?0) (< c ?8))
2896 (substring "`'^\"~=." (1- (- c ?0)) (- c ?0)))
2897 ((= c ?h) "H")
2898 (t (char-to-string c))))
2899 (if (not (string-match c "`'^\"~=.uvHtcdb")) nil
2900 (insert "\\" c "{}")
2901 (backward-char 1)
2902 (insert (YaTeX-read-accent-char c))
2903 (if (string= c "t") (insert (YaTeX-read-accent-char c)))
2904 (forward-char 1))))
2906 ;; Indentation
2907 (defun YaTeX-current-indentation ()
2908 "Return the indentation of current environment."
2909 (save-excursion
2910 ;;(beginning-of-line)
2911 (if (YaTeX-beginning-of-environment t)
2912 (goto-char (get 'YaTeX-inner-environment 'point))
2913 (forward-line -1)
2914 (beginning-of-line)
2915 (skip-chars-forward " \t"))
2916 (current-column)))
2918 (defun YaTeX-previous-line-indentation ()
2919 (save-excursion
2920 (forward-line -1)
2921 (skip-chars-forward " \t")
2922 (current-column)))
2924 (defvar YaTeX-noindent-env-regexp "verbatim\\*?\\|alltt"
2925 "*Regexp of environment names that should begin with no indentation.
2926 All verbatime-like environment name should match with.")
2927 (defun YaTeX-indent-line ()
2928 "Indent corrent line referrin current environment."
2929 (interactive)
2930 (let ((indent-relative
2931 (function
2932 (lambda (&optional additional)
2933 (YaTeX-reindent
2934 (+ (YaTeX-current-indentation)
2935 (or additional 0)
2936 YaTeX-environment-indent)))))
2937 sect depth iteminfo (p (point)) pp (peol (point-end-of-line)) begend
2938 ;;inenv below is sometimes defined in YaTeX-indent-new-comment-line
2939 (inenv (or (and (boundp 'inenv) inenv) (YaTeX-inner-environment t))))
2940 ;;(if NTT-jTeX ;;Do you need this section?
2941 ;; (save-excursion
2942 ;; (end-of-line)
2943 ;; (let ((p (point)))
2944 ;; (forward-line -1)
2945 ;; (end-of-line)
2946 ;; (or (= p (point))
2947 ;; (progn (backward-char (length YaTeX-comment-prefix))
2948 ;; (not (looking-at (regexp-quote YaTeX-comment-prefix))))
2949 ;; (progn
2950 ;; (skip-chars-backward YaTeX-comment-prefix)
2951 ;; (kill-line))))))
2952 (or inenv (setq inenv "document")) ;is the default environment
2953 (cond
2954 ((and
2955 (prog1 (YaTeX-on-begin-end-p)
2956 (setq begend (match-beginning 0)))
2957 (or (match-beginning 2) ;if \end
2958 (and (match-beginning 3) ;if \) \]
2959 (= (char-syntax (char-after (1+ (match-beginning 3)))) ?\)))))
2960 (YaTeX-reindent
2961 (save-excursion
2962 (YaTeX-goto-corresponding-environment)
2963 (current-column))))
2964 ;; on the begining of verbatime line, remove all indentation
2965 ((and begend ;; match-beginning 0 of \begin
2966 YaTeX-noindent-env-regexp
2967 (stringp YaTeX-noindent-env-regexp)
2968 (save-excursion
2969 (and ;; if the \begin is the first declaration of this line
2970 (progn (beginning-of-line) (skip-chars-forward " \t")
2971 (= begend (point)))
2972 (progn
2973 (goto-char begend)
2974 (looking-at
2975 (concat YaTeX-ec-regexp
2976 "begin{\\(" YaTeX-noindent-env-regexp "\\)}"))))))
2977 (save-excursion
2978 (goto-char begend)
2979 (delete-region (point) (point-beginning-of-line))))
2980 ((string-match YaTeX-equation-env-regexp inenv)
2981 (YaTeX-indent-line-equation)) ;autoload-ed from yatexenv
2982 (;(YaTeX-in-environment-p '("itemize" "enumerate" "description" "list"))
2983 (string-match YaTeX-itemizing-env-regexp inenv)
2984 ;;(YaTeX-on-item-p) ??
2985 ;;(setq iteminfo (YaTeX-get-item-info t))
2986 (if (save-excursion
2987 (beginning-of-line)
2988 (re-search-forward YaTeX-item-regexp peol t))
2989 (progn
2990 (save-excursion
2991 (goto-char (1+ (match-beginning 0)))
2992 (setq depth
2993 (* YaTeX-environment-indent
2994 (cond
2995 ((looking-at "subsubsub") 3)
2996 ((looking-at "subsub") 2)
2997 ((looking-at "sub") 1)
2998 (t 0)))))
2999 (funcall indent-relative depth))
3000 (YaTeX-reindent (or (car (cdr (YaTeX-get-item-info t inenv)))
3001 (+ (save-excursion
3002 (beginning-of-line)
3003 (YaTeX-current-indentation))
3004 YaTeX-environment-indent))))
3006 ((YaTeX-literal-p) ;verbatims
3007 (tab-to-tab-stop))
3008 ((string-match "\\(tabular\\|array\\)" inenv) ;1.73
3009 (let ((n 1))
3010 (condition-case err
3011 (save-excursion
3012 (beginning-of-line)
3013 (skip-chars-forward "[ \t]")
3014 ;;(if (looking-at "&") (forward-char 1))
3015 (require 'yatexenv)
3016 (setq n (car (YaTeX-array-what-column-internal))))
3017 (error nil))
3018 (YaTeX-reindent
3019 (+ (YaTeX-current-indentation)
3020 YaTeX-environment-indent
3021 (* (1- n) YaTeX-tabular-indentation)))))
3022 ((and inenv (not (equal "document" inenv)))
3023 (funcall indent-relative))
3024 ((YaTeX-on-section-command-p YaTeX-sectioning-regexp)
3025 (require 'yatexsec) ;to know YaTeX-sectioning-level
3026 (setq sect (YaTeX-match-string 1))
3027 (if (string-match "\\*$" sect)
3028 (setq sect (substring sect 0 -1)))
3029 (YaTeX-reindent
3030 (* (max
3031 (1- ;I want chapter to have indentation 0
3032 (or (cdr (assoc sect YaTeX-sectioning-level))
3033 0))
3034 0)
3035 YaTeX-environment-indent)))
3036 ;;Default movement
3037 ((and (bolp) fill-prefix) (insert fill-prefix))
3038 (t (save-excursion
3039 (beginning-of-line)
3040 (if fill-prefix
3041 (progn
3042 (delete-region (point)
3043 (progn (skip-chars-forward " \t")
3044 (point)))
3045 (insert fill-prefix))
3046 (skip-chars-forward " \t")
3047 (if (bobp)
3048 nil
3049 (indent-relative-maybe))))
3050 (skip-chars-forward " \t")))
3051 ;;if current line is \begin, re-indent \end too
3052 (if (and (YaTeX-on-begin-end-p) (match-beginning 1))
3053 (save-excursion
3054 ;;(beginning-of-line)
3055 ;;(search-forward "\\begin")
3056 (goto-char (match-beginning 0))
3057 (setq depth (current-column))
3058 (YaTeX-goto-corresponding-environment)
3059 (YaTeX-reindent depth)))
3060 (if (or
3061 (and NTT-jTeX
3062 (save-excursion (beginning-of-line) (looking-at "[ \t]")))
3063 (save-excursion
3064 (beginning-of-line)
3065 (and
3066 (not (bobp))
3067 (progn
3068 (backward-char 1)
3069 (re-search-backward
3070 "\\\\\\(\\(page\\)?ref\\|cite\\){" (point-beginning-of-line) t))
3071 (goto-char (1- (match-end 0)))
3072 (> (save-excursion
3073 (condition-case ()
3074 (progn (forward-list 1) (point))
3075 (error (point-max))))
3076 (point-end-of-line)))))
3077 (save-excursion
3078 (end-of-line)
3079 (let ((p (point)))
3080 (forward-line -1)
3081 (end-of-line)
3082 (or (= p (point))
3083 (looking-at (regexp-quote YaTeX-comment-prefix))
3084 (bobp) (bolp)
3085 (save-excursion
3086 (backward-word 1)
3087 (looking-at "\\sw+")) ;is not japanese string
3088 (insert YaTeX-comment-prefix)))))))
3090 (defun YaTeX-comment-line-break (&optional soft)
3091 "Call comment-indent-new-line and YaTeX-indent-line"
3092 (comment-indent-new-line soft)
3093 (YaTeX-indent-line))
3095 (defun YaTeX-latex2e-p ()
3096 (let ((b (current-buffer))
3097 (ptn (concat YaTeX-ec "documentclass")))
3098 (unwind-protect
3099 (or (save-excursion (search-backward ptn nil t))
3100 (progn
3101 (YaTeX-visit-main t)
3102 (save-excursion (search-backward ptn nil t))))
3103 (set-buffer b))))
3105 (provide 'yatex)
3106 (defvar yatex-mode-load-hook nil
3107 "*List of functions to be called when yatex.el is loaded.")
3108 (if (and YaTeX-emacs-19 YaTeX-display-color-p (not (featurep 'yatex19)))
3109 (load "yatex19"))
3110 (load "yatexhks" t)
3112 ;;-------------------- Final hook jobs --------------------
3113 (substitute-all-key-definition
3114 'fill-paragraph 'YaTeX-fill-paragraph YaTeX-mode-map)
3115 (substitute-all-key-definition
3116 'kill-buffer 'YaTeX-kill-buffer YaTeX-mode-map)
3117 (run-hooks 'yatex-mode-load-hook)
3119 ;; `History' was moved to ChangeLog
3120 ;----------------------------- End of yatex.el -----------------------------
3122 ; Local variables:
3123 ; coding: sjis
3124 ; End: