yatex

view yatex.el @ 107:d63a7297756a

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