yatex

view yatexmth.el @ 77:1b172d26b55e

Fix auto-fill movement on Emacs21. Fix YaTeX:ref. Support jump/change on AMS-LaTeX's parentheses.
author yuuji
date Thu, 01 May 2003 13:38:27 +0000
parents 0aaebd07dad0
children 9b4354af748c
line source
1 ;;; -*- Emacs-Lisp -*-
2 ;;; YaTeX math-mode-specific functions.
3 ;;; yatexmth.el
4 ;;; (c)1993-2003 by HIROSE Yuuji [yuuji@yatex.org]
5 ;;; Last modified Sun Mar 30 19:56:00 2003 on firestorm
6 ;;; $Id$
8 ;;; [Customization guide]
9 ;;;
10 ;;; By default, you can use two completion groups in YaTeX math
11 ;;; mode, `;' for mathematical signs and `:' for greek letters. But
12 ;;; you can add other completion groups by defining the alist of
13 ;;; `prefix key' vs `completion list' into the variable
14 ;;; YaTeX-math-key-list-private. If you wish to accomplish the
15 ;;; completion as follows(prefix key is `,'):
16 ;;;
17 ;;; KEY COMPLETION
18 ;;; s \sin
19 ;;; S \arcsin
20 ;;; c \cos
21 ;;; C \arccos
22 ;;; :
23 ;;; T \arctan
24 ;;; l \log
25 ;;; hs \sinh
26 ;;;
27 ;;; Typing `s' after `,' makes `\sin', `hs' after `,' makes `\sinh'
28 ;;; and so on. First, you have to define list of key-completion
29 ;;; pairs. Variable name(YaTeX-math-funcs-list) is arbitrary.
30 ;;;
31 ;;; (setq YaTeX-math-funcs-list
32 ;;; '(("s" "sin")
33 ;;; ("S" "arcsin")
34 ;;; :
35 ;;; ("hs" "sinh")))
36 ;;;
37 ;;; Next, define the list of prefix-key vs completion list(defined
38 ;;; above) into the variable YaTeX-math-key-list-private.
39 ;;;
40 ;;; (setq YaTeX-math-key-list-private
41 ;;; '(("," . YaTeX-math-funcs-list)
42 ;;; ("'" . Other-List-if-any)))
43 ;;;
44 ;;; Put these expressions into your ~/.emacs, and you can use this
45 ;;; completion in the math-mode.
46 ;;;
47 ;;; And you can add your favorite completion sequences to the
48 ;;; default completion list invoked with `;', by defining those lists
49 ;;; into variable YaTeX-math-sign-alist-private.
51 ;;; 【イメージ補完の追加方法】
52 ;;;
53 ;;; 標準のイメージ補完では、「;」で始まる数式記号補完と、「:」で始
54 ;;; まるギリシャ文字補完が使用可能ですが、これ以外の文字で始まる補完
55 ;;; シリーズも定義することができます。例えば、「,」で始まる次のよう
56 ;;; な補完シリーズを定義する場合を考えてみます。
57 ;;;
58 ;;; 補完キー 補完結果
59 ;;; s \sin
60 ;;; S \arcsin
61 ;;; c \cos
62 ;;; C \arccos
63 ;;; :
64 ;;; T \arctan
65 ;;; l \log
66 ;;; hs \sinh
67 ;;;
68 ;;; 「,」のあとに s を押すと \sin が、hs を押すと \sinh が入力されま
69 ;;; す。このような補完リストの登録は以下のようにします(変数名は任意)。
70 ;;;
71 ;;; (setq YaTeX-math-funcs-list
72 ;;; '(("s" "sin")
73 ;;; ("S" "arcsin")
74 ;;; :
75 ;;; ("hs" "sinh")))
76 ;;;
77 ;;; さらに、「,」を押した時にイメージ補完が始まるよう次の変数に、起動キー
78 ;;; と上で定義した補完用変数の登録を行ないます。
79 ;;;
80 ;;; (setq YaTeX-math-key-list-private
81 ;;; '(("," . YaTeX-math-funcs-list)
82 ;;; ("'" . ほかに定義したいシリーズがあれば…)))
83 ;;;
84 ;;; これらを ~/.emacs に書いておけば、math-mode で自分専用のイメージ
85 ;;; 補完が利用できます。
87 (defvar YaTeX-jisold
88 (and (boundp 'dos-machine-type)
89 (eq dos-machine-type 'pc98)))
91 (defmacro YaTeX-setq-math-sym (sym old new)
92 (list 'setq sym (list 'if 'YaTeX-jisold old new)))
94 (YaTeX-setq-math-sym YaTeX-image-in "E" "∈")
95 (YaTeX-setq-math-sym YaTeX-image-ni "ヨ" "∋")
96 (YaTeX-setq-math-sym YaTeX-image-subset " _\n(\n ~" "⊂")
97 (YaTeX-setq-math-sym YaTeX-image-subseteq " _\n(_\n ~" "⊆")
98 (YaTeX-setq-math-sym YaTeX-image-supset "_\n )\n~" "⊃")
99 (YaTeX-setq-math-sym YaTeX-image-supseteq "_\n_)\n~" "⊇")
100 (YaTeX-setq-math-sym YaTeX-image-nabla "___\n\\\\/" "∇")
101 (YaTeX-setq-math-sym YaTeX-image-partial " -+\n+-+\n+-+" "∂")
102 (YaTeX-setq-math-sym YaTeX-image-dagger "+\n|" "†")
103 (YaTeX-setq-math-sym YaTeX-image-ddagger "+\n+\n|" "‡")
104 (YaTeX-setq-math-sym YaTeX-image-equiv "=\n ̄" "≡")
105 (YaTeX-setq-math-sym YaTeX-image-int " /\\\n \\\n\\/" "∫")
106 (YaTeX-setq-math-sym YaTeX-image-bot "|\n ̄" "⊥")
107 (YaTeX-setq-math-sym YaTeX-image-neg "イ" "¬")
108 (YaTeX-setq-math-sym YaTeX-image-flat "b" "♭")
109 (YaTeX-setq-math-sym YaTeX-image-sqrt "" "√")
111 (defvar
112 YaTeX-math-sign-alist-default
113 '(
114 ;frequently used
115 ("||" "|" ("||" "‖"))
116 ("sum" "sum" ("\\-+\n >\n/-+" "Σ"))
117 ("sigma" "sum" ("\\-+\n >\n/-+" "Σ"))
118 ("integral" "int" (" /\\\n \\\n\\/" YaTeX-image-int))
119 ("ointegral" "oint" " /\\\n(\\)\n\\/")
120 ("sqrt" "sqrt" (" __\n,/" YaTeX-image-sqrt))
121 ("root" "sqrt" (" __\n,/" YaTeX-image-sqrt))
122 ("A" "forall" "|_|\nV")
123 ("E" "exists" "-+\n-+\n-+")
124 ("!" "neg" ("--+\n |" YaTeX-image-neg))
125 ("oo" "infty" ("oo" "∞"))
126 ("\\" "backslash" ("\\" "\"))
127 ("..." "cdots" ("..." "…"))
129 ;;binary operators
130 ("+-" "pm" ("+\n-" "±"))
131 ("-+" "mp" "-\n+")
132 ("x" "times" ("x" "×"))
133 ("/" "div" (",\n-\n'" "÷"))
134 ("f" "frac" "xxx\n---\nyyy" "÷")
135 ("*" "ast" "*")
136 ("#" "star" ("_/\\_\n\\ /\n//\\\\" "★"))
137 ("o" "circ" "o")
138 ("o*" "bullet" " _\n(*)\n ~")
139 ("." "cdot" ".")
140 ("cap" "cap" "/-\\\n| |")
141 ("cup" "cup" "| |\n\\-/")
142 ("u+" "uplus" "|+|\n\\-/")
143 ("|~|" "sqcap" "|~|")
144 ("|_|" "sqcup" "|_|")
145 ("v" "vee" "v")
146 ("^" "wedge" "^")
147 ("\\\\" "setminus" "\\")
148 (")(" "wr" " )\n(")
149 ("<>" "diamond" "<>")
150 ("/\-" "bigtriangleup" ("/\\\n~~" "△"))
151 ("-\\/" "bigtriangledown" ("__\n\\/" "▽"))
152 ("<|" "triangleleft" "<|")
153 ("|>" "triangleright" "|>")
154 ("<||" "lhd" "/|\n\\|")
155 ("||>" "rhd" "|\\\n|/")
156 ("<|-" "unlhd" "<|\n~~")
157 ("|>-" "unrhd" "|>\n~~")
158 ("o+" "oplus" " _\n(+)\n ~")
159 ("o-" "ominus" " _\n(-)\n ~")
160 ("ox" "otimes" " _\n(x)\n ~")
161 ("o/" "oslash" " _\n(/)\n ~")
162 ("o." "odot" "(.)")
163 ("O" "bigcirc" "O")
164 ("t" "dagger" ("+\n|" YaTeX-image-dagger))
165 ("tt" "ddagger" ("+\n+\n|" YaTeX-image-ddagger))
166 ("II" "amalg" "II")
167 ; :
168 ;;relational operators
169 ("<" "leq" ("<\n-" "≦"))
170 (">" "geq" (">\n-" "≧"))
171 ("-=" "equiv" ("=\n-" YaTeX-image-equiv))
172 ("=-" "equiv" ("=\n-" YaTeX-image-equiv))
173 ("---" "equiv" ("=\n-" YaTeX-image-equiv))
174 ("(" "subset" (" _\n(\n ~" YaTeX-image-subset))
175 ("(-" "subseteq" (" _\n(_\n ~" YaTeX-image-subseteq))
176 (")" "supset" ("_\n )\n~" YaTeX-image-supset))
177 (")-" "supseteq" ("_\n_)\n~" YaTeX-image-supseteq))
178 ("[" "sqsubset" "[")
179 ("[-" "sqsubseteq" "[\n~")
180 ("]" "sqsupset" "]")
181 ("]-" "sqsupseteq" "]\n~")
182 ("{" "in" ("(-" YaTeX-image-in))
183 ("}" "ni" ("-)" YaTeX-image-ni))
184 ("|-" "vdash" "|-")
185 ("-|" "dashv" "-|")
186 ("~" "sim" "~(tild)")
187 ("~-" "simeq" "~\n-")
188 ("asymp" "asymp" "v\n^")
189 ("~~" "approx" "~\n~")
190 ("~=" "cong" "~\n=")
191 ("=/" "neq" ("=/=" "≠"))
192 (".=" "doteq" ".\n=")
193 ("o<" "propto" "o<")
194 ("|=" "models" "|=")
195 ("_|_" "perp" "_|_")
196 ("|" "mid" "|")
197 ("||" "parallel" "||")
198 ("bowtie" "bowtie" "|><|(wide)")
199 ("|><|" "join" "|><|")
200 ("\\_/" "smile" "\\_/")
201 ("/~\\" "frown" "/~~\\")
202 ("-<" "prec" ("-<" "く"))
203 ("-<=" "preceq" ("-<\n-" "く\n="))
204 ("<<" "ll" ("<<" "《"))
205 ; :
206 ;;arrows
207 ("<-" "leftarrow" ("<-" "←"))
208 ("\C-b" "leftarrow" ("<-" "←"))
209 ("<--" "longleftarrow" ("<--" "←--"))
210 ("<=" "Leftarrow" "<=")
211 ("<==" "Longleftarrow" "<==")
212 ("->" "rightarrow" ("->" "→"))
213 ("\C-f" "rightarrow" ("->" "→"))
214 ("-->" "longrightarrow" ("-->" "--→"))
215 ("=>" "Rightarrow" "=>")
216 ("==>" "Longrightarrow" "==>")
217 ("<->" "leftrightarrow" ("<->" "←→"))
218 ("<-->" "longleftrightarrow" ("<---->" "←--→"))
219 ("<=>" "Leftrightarrow" "<=>")
220 ("<==>" "Longleftrightarrow" "<==>")
221 ("^|" "uparrow" ("^\n|" "↑"))
222 ("\C-p" "uparrow" ("^\n|" "↑"))
223 ("^||" "Uparrow" "/\\\n||")
224 ("\C-n" "downarrow" ("|\nv" "↓"))
225 ("v|" "downarrow" ("|\nv" "↓"))
226 ("v||" "Downarrow" "||\n\\/")
227 ("|->" "mapsto" ("|->" "|→"))
228 ("<-)" "hookleftarrow" (" ,\n<--+" " ヽ\n<--/"))
229 ("(->" "hookrightarrow" ("`\n+-->" "/\n\-->"))
230 ("/-" "leftharpoonup" "/\n~~~")
231 ("\\-" "leftharpoondown" "__\n\\")
232 ("-/" "rightharpoondown" "__\n/")
233 ("-\\" "rightharpoonup" "~~\n\\")
234 ;;left and right
235 ("left" "left" "(leftmark)")
236 ("right" "right" "(rightmark)")
237 ;;accent marks
238 ("tilde" "tilde" "~\n?")
239 ("T" "tilde" "~\n?")
240 ("wtilde" "widetilde" "~\n?")
241 ("hat" "hat" "^\n?")
242 ("what" "widehat" "/\\\n??")
243 ("w^" "widehat" "/\\\n?")
244 ("check" "check" "v\n?")
245 ("bar" "bar" "_\n?")
246 ("overline" "overline" "_\n?")
247 ("wbar" "overline" "--\n??")
248 ("dot" "dot" ".\n?")
249 ("ddot" "ddot" "..\n??")
250 ("vec" "vec" ("->\n??" "→\n??"))
251 ("~>" "overrightarrow" ("-->\nAB" "→\nAB"))
252 ("VEC" "overrightarrow" ("-->\nAB" "→\nAB"))
253 ;;rage-aware stuffs
254 ("prod" "prod" ("-+--+-\n | |" "Π"))
255 ("CUP" "bigcup" "|~~|\n| |\n| |")
256 ("union" "bigcup" "|~~|\n| |\n| |")
257 ("CAP" "bigcap" "| |\n| |\n|__|")
258 ("isc" "bigcap" "| |\n| |\n|__|")
259 ("O+" "bigoplus" "/~~~\\\n| + |\n\\___/")
260 ("Ox" "bigotimes" "/~~~\\\n| X |\n\\___/")
261 ;;other marks
262 ("Z" "aleph" "|\\|")
263 ("|\\|" "aleph" "|\\|")
264 ("h-" "hbar" "_\nh")
265 ; ("i" "imath" "i") ;These chars are appeared only
266 ; ("j" "jmath" "j") ;as section-type arguments
267 ("l" "ell" "l")
268 ("wp" "wp" "???")
269 ("R" "Re" ")R")
270 ("Im" "Im" "???")
271 ("mho" "mho" "~|_|~")
272 ("'" "prime" "'")
273 ("0" "emptyset" "0")
274 ("nabla" "nabla" ("___\n\\\\/" YaTeX-image-nabla))
275 ("\\/" "surd" "-\\/")
276 ("surd" "surd" "-\\/")
277 ("top" "top" "T")
278 ("bot" "bot" ("_|_" YaTeX-image-bot))
279 ("b" "flat" ("b" YaTeX-image-flat))
280 ("LT" "natural" "|\nLT\n |")
281 ("6" "partial" (" -+\n+-+\n+-+" YaTeX-image-partial))
282 ("partial" "partial" (" -+\n+-+\n+-+" YaTeX-image-partial))
283 ("round" "partial" (" -+\n+-+\n+-+" YaTeX-image-partial))
284 ("[]" "Box" "[]")
285 ("Diamond" "Diamond" "/\\\n\\/")
286 ("3" "triangle" "/\\\n~~")
287 ("C" "clubsuit" " o\no+o\n |")
288 ("D" "diamondsuit" "/\\\n\\/")
289 ("H" "heartsuit" "<^^>\n \\/")
290 ("S" "spadesuit" " /\\\n<++>\n /\\")
291 )
292 "Default LaTeX-math-command alist.")
294 (defvar YaTeX-math-sign-alist-private nil
295 "*User definable key vs LaTeX-math-command alist.")
297 (defvar YaTeX-math-quit-with-strict-match nil
298 "*T for quitting completion as soon as strict-match is found.")
299 (defvar YaTeX-math-sign-alist
300 (append YaTeX-math-sign-alist-private YaTeX-math-sign-alist-default))
302 ;;(defun YaTeX-math-alist2array (alist array)
303 ;; (set array
304 ;; (let ((array (make-vector (length alist) "")) (list alist) (i 0))
305 ;; (while list
306 ;; (aset array i (car (car list)))
307 ;; (setq i (1+ i) list (cdr list)))
308 ;; array))
309 ;;)
311 (defvar YaTeX-greek-key-alist-default
312 '(
313 ("a" "alpha" ("a" "α"))
314 ("b" "beta" ("|>\n|>\n|" "β"))
315 ("g" "gamma" ("~r" "γ"))
316 ("G" "Gamma" ("|~" "Γ"))
317 ("d" "delta" ("<~\n<>" "δ"))
318 ("D" "Delta" ("/\\\n~~" "Δ"))
319 ("e" "epsilon" "<\n<~")
320 ("e-" "varepsilon" ("(\n(~" "ε"))
321 ("z" "zeta" ("(~\n >" "ζ"))
322 ("et" "eta" ("n\n/" "η"))
323 ("th" "theta" ("8" "θ"))
324 ("Th" "Theta" ("(8)" "Θ"))
325 ("th-" "vartheta" ("-8" "-θ"))
326 ("i" "iota" ("i\n\\_/" "ι"))
327 ("k" "kappa" ("k" "κ"))
328 ("l" "lambda" ("\\n/\\" "λ"))
329 ("L" "Lambda" ("/\\" "Λ"))
330 ("m" "mu" (" u_\n/" "μ"))
331 ("n" "nu" ("|/" "ν"))
332 ("x" "xi" ("E\n >" "ξ"))
333 ("X" "Xi" ("---\n -\n---" "Ξ"))
334 ("p" "pi" ("__\n)(" "π"))
335 ("P" "Pi" ("__\n||" "Π"))
336 ("p-" "varpi" ("_\nw" "__\nω"))
337 ("r" "rho" ("/O" "ρ"))
338 ("r-" "varrho" ("/O\n~~" "ρ\n~~"))
339 ("s" "sigma" ("o~" "σ"))
340 ("S" "Sigma" ("\\-+\n >\n/-+" "Σ"))
341 ("s-" "varsigma" "(~~ \n>")
342 ("t" "tau" ("__\n(" "τ"))
343 ("u" "upsilon" ("~v" "υ"))
344 ("y" "upsilon" ("~v" "υ"))
345 ("U" "Upsilon" ("~Y~" "Υ"))
346 ("Y" "Upsilon" ("~Y~" "Υ"))
347 ("ph" "phi" (" /\n(/)\n/" "φ"))
348 ("Ph" "Phi" (" _\n(|)\n ~" "Φ"))
349 ("ph-" "varphi" "\\O\n|")
350 ("c" "chi" ("x" "χ"))
351 ("ps" "psi" ("\\|/\\n |" "ψ"))
352 ("Ps" "Psi" (" ~\n\\|/\\n |" "Ψ"))
353 ("o" "omega" ("w" "ω"))
354 ("w" "omega" ("w" "ω"))
355 ("O" "Omega" ("(~)\n~ ~" "Ω"))
356 ("W" "Omega" ("(~)\n~ ~" "Ω"))
357 ("f" "foo")
358 )
359 "Default LaTeX-math-command alist.")
361 (defvar YaTeX-greek-key-alist-private nil
362 "*User definable key vs LaTeX-math-command alist.")
364 (defvar YaTeX-greek-key-alist
365 (append YaTeX-greek-key-alist-private YaTeX-greek-key-alist-default))
367 ;;(mapcar (function (lambda (x) (YaTeX-math-alist2array x)))
368 ;; YaTeX-math-key-list)
370 (defvar YaTeX-math-indicator "KEY\tLaTeX sequence\t\tsign")
372 (defvar YaTeX-math-need-image t
373 "*T for displaying pseudo image momentarily.")
374 (defvar YaTeX-math-max-key 8)
375 (defvar YaTeX-math-max-seq
376 (* 8 (1+ (/ (length "\\longleftrightarrow") 8))))
377 (defvar YaTeX-math-max-sign 5)
378 (defvar YaTeX-math-sign-width
379 (+ YaTeX-math-max-key YaTeX-math-max-seq YaTeX-math-max-sign))
380 (defvar YaTeX-math-display-width
381 (* 8 (1+ (/ YaTeX-math-sign-width 8))))
382 (defvar YaTeX-math-menu-map nil
383 "Keymap used in YaTeX mathematical sign menu mode."
384 )
385 (if YaTeX-math-menu-map nil
386 (setq YaTeX-math-menu-map (make-sparse-keymap))
387 (define-key YaTeX-math-menu-map "n" 'next-line)
388 (define-key YaTeX-math-menu-map "p" 'previous-line)
389 (define-key YaTeX-math-menu-map "f" 'YaTeX-math-forward)
390 (define-key YaTeX-math-menu-map "b" 'YaTeX-math-backward)
391 (define-key YaTeX-math-menu-map "v" 'scroll-up)
392 (define-key YaTeX-math-menu-map " " 'scroll-up)
393 (define-key YaTeX-math-menu-map "c" 'scroll-up)
394 (define-key YaTeX-math-menu-map "V" 'scroll-down)
395 (define-key YaTeX-math-menu-map "r" 'scroll-down)
396 (define-key YaTeX-math-menu-map "\^h" 'scroll-down)
397 (define-key YaTeX-math-menu-map "<" 'beginning-of-buffer)
398 (define-key YaTeX-math-menu-map ">" 'end-of-buffer)
399 (define-key YaTeX-math-menu-map "\^m" 'exit-recursive-edit)
400 (define-key YaTeX-math-menu-map "q" 'abort-recursive-edit))
402 (defvar YaTeX-math-exit-key "\e"
403 "*Key sequence after prefix key of YaTeX-math-mode to exit from math-mode."
404 )
406 (defmacro YaTeX-math-japanese-sign (list)
407 (list 'nth 1 list))
409 (defvar YaTeX-math-cmd-regexp (concat (regexp-quote YaTeX-ec) "[A-z|]"))
410 (defvar YaTeX-math-verbatim-environments
411 '("alltt")
412 "*List of environments in which LaTeX math mode is disabled.
413 This value is appended with YaTeX-verbatim-environments.")
415 ;;;
416 ;;YaTeX math-mode functions
417 ;;;
418 ;;;###autoload from yatex.el
419 (defun YaTeX-toggle-math-mode (&optional arg)
420 (interactive "P")
421 (or (memq 'YaTeX-math-mode mode-line-format) nil
422 (setq mode-line-format
423 (append (list "" 'YaTeX-math-mode) mode-line-format)))
424 (if YaTeX-auto-math-mode nil ;Only effective on manual mode.
425 (if (or arg (null YaTeX-math-mode))
426 (let (keys)
427 (setq YaTeX-math-mode "math:")
428 (message "Turn on math mode. Prefix keys are %s"
429 (mapconcat 'car YaTeX-math-key-list " "))
430 (sit-for 3)
431 (message
432 (concat "To exit from math-mode, type `ESC' after prefix, "
433 "or type `"
434 (key-description
435 (car
436 (where-is-internal
437 'YaTeX-switch-mode-menu YaTeX-mode-map)))
438 " $'")))
439 (setq YaTeX-math-mode nil)
440 (message "Exit from math mode."))
441 (set-buffer-modified-p (buffer-modified-p)))
442 )
444 (defun YaTeX-math-forward (arg)
445 (interactive "p")
446 (re-search-forward YaTeX-math-cmd-regexp nil t arg))
448 (defun YaTeX-math-backward (arg)
449 (interactive "p")
450 (re-search-backward YaTeX-math-cmd-regexp nil t arg))
452 (defun YaTeX-math-gets (sign)
453 (cond
454 ((null sign) nil)
455 ((listp sign)
456 (setq sign
457 (cond
458 (YaTeX-japan (YaTeX-math-japanese-sign sign))
459 (t (car sign))))
460 (YaTeX-math-gets sign))
461 ((symbolp sign)
462 (YaTeX-math-gets (symbol-value sign)))
463 (t sign))
464 )
466 (defun YaTeX-math-get-sign (list)
467 (YaTeX-math-gets (car (cdr-safe (cdr-safe list))))
468 )
469 (defvar YaTeX-math-section-type-regexp
470 "eqn\\\\\\sw+\\b"
471 "*Regexp of section-type math-mode macro")
473 (defun YaTeX-in-math-mode-p ()
474 "If current position is supposed to be in LaTeX-math-mode, return t."
475 (or (YaTeX-quick-in-environment-p
476 (append
477 '("math" "eqnarray" "equation" "eqnarray*" "displaymath");LaTeX
478 (if YaTeX-use-AMS-LaTeX
479 ;; And math modes of AMS-LaTeX
480 '("align" "align*" "split" "multline" "multline*" "gather"
481 "gather*" "aligned*" "gathered" "gathered*" "alignat"
482 "equation*" "cases"
483 "alignat*" "xalignat" "xalignat*" "xxalignat" "xxalignat*"))))
484 (let*((p (point)) (nest 0) me0 r
485 (delim (concat YaTeX-sectioning-regexp "\\|^$\\|^\C-l"))
486 (boundary
487 (save-excursion
488 (if (looking-at delim)
489 (goto-char (max (point-min) (1- (point)))))
490 (re-search-backward delim nil 1)
491 (point))))
492 (save-excursion
493 (cond
494 ((catch 'open
495 (save-excursion
496 (while (and (>= nest 0)
497 (re-search-backward
498 (concat YaTeX-ec-regexp ;\
499 "\\([()]\\|[][]\\)") boundary t))
500 (setq me0 (match-end 0))
501 (if (or (YaTeX-on-comment-p)
502 (YaTeX-literal-p)) nil
503 (if (or (= (char-after (1- me0)) ?\))
504 (= (char-after (1- me0)) ?\]))
505 (setq nest (1+ nest))
506 (if (= (preceding-char) ?\\ ) nil;;\\[5pt]
507 (setq nest (1- nest))))))
508 (if (< nest 0) (throw 'open t))))
509 t)
510 ((and (setq r (YaTeX-on-section-command-p
511 YaTeX-math-section-type-regexp))
512 (numberp r)
513 (> r 0))
514 t)
515 (t (catch 'dollar
516 (while ;(search-backward "$" boundary t);little bit fast.
517 (YaTeX-re-search-active-backward ;;;;;; Too slow???
518 "\\$" (concat "[^\\\\]" YaTeX-comment-prefix) boundary t)
519 (cond
520 ((equal (char-after (1- (point))) ?$) ; $$ equation $$
521 (backward-char 1)
522 (setq nest (1+ nest)))
523 ((let ((YaTeX-verbatim-environments
524 (append YaTeX-math-verbatim-environments
525 YaTeX-verbatim-environments)))
526 (YaTeX-literal-p))
527 nil)
528 ((and (equal (char-after (1- (point))) ?\\ )
529 (not (equal (char-after (- (point) 3)) ?\\ )))
530 nil) ;\$
531 (t (setq nest (1+ nest)))))
532 (if (= (% nest 2) 1) (throw 'dollar t))))))))
533 )
535 (defun YaTeX-math-display-list (list cols)
536 (goto-char (point-max))
537 (if (= cols 0) (if (not (eolp)) (newline 1))
538 (forward-line -1)
539 (while (looking-at "[ \t\n]") (forward-line -1)))
540 (end-of-line)
541 (let ((indent (* YaTeX-math-display-width cols)) sign str to)
542 (indent-to indent)
543 (insert (car list))
544 (indent-to (setq indent (+ indent YaTeX-math-max-key)))
545 (insert "\\" (car (cdr list)))
546 (setq indent (+ indent YaTeX-math-max-seq))
547 (setq sign (YaTeX-math-get-sign list))
548 (while (and sign (not (string= "" sign)))
549 (setq to (string-match "\n" sign)
550 str (if to (substring sign 0 to) sign))
551 (end-of-line)
552 (indent-to indent)
553 (insert str)
554 (cond ((eobp) (newline 1))
555 ((> cols 0) (forward-line 1)))
556 (setq sign (if to (substring sign (1+ to)) "")))))
558 (defvar YaTeX-math-menu-buffer "*math-mode-signs*")
560 (defun YaTeX-math-show-menu (match-str)
561 (save-window-excursion
562 (YaTeX-showup-buffer YaTeX-math-menu-buffer nil t)
563 (let ((maxcols (max 1 (/ (YaTeX-screen-width) YaTeX-math-sign-width)))
564 (case-fold-search nil)
565 (cols 0) (list alist) command)
566 (erase-buffer)
567 (insert
568 "Candidates of sign. [n:next p:prev f:forw b:back q:quit RET:select]\n")
569 (insert YaTeX-math-indicator "\t")
570 (insert YaTeX-math-indicator)
571 (newline 1)
572 (insert-char ?- (1- (YaTeX-screen-width)))
573 (newline 1)
574 (while list
575 (if (string-match match-str (car (car list)))
576 (progn (YaTeX-math-display-list (car list) cols)
577 (setq cols (% (1+ cols) maxcols))))
578 (setq list (cdr list)))
579 (goto-line 4)
580 (use-local-map YaTeX-math-menu-map)
581 (setq buffer-read-only t)
582 (unwind-protect
583 (recursive-edit)
584 (skip-chars-backward "^ \t\n")
585 (setq command
586 (if (re-search-forward YaTeX-math-cmd-regexp nil t)
587 (buffer-substring
588 (match-beginning 0)
589 (prog2 (skip-chars-forward "^ \t\n") (point)))
590 nil))
591 (kill-buffer YaTeX-math-menu-buffer))
592 command))
593 )
595 ;
596 (defun YaTeX-math-show-image (image &optional exit-char)
597 "Momentarily display IMAGE at the beginning of the next line;
598 erase it on the next keystroke. The window is recentered if necessary
599 to make the whole string visible. If the window isn't large enough,
600 at least you get to read the beginning."
601 (if (and image (not (string= image "")))
602 (let ((buffer-read-only nil)
603 (modified (buffer-modified-p))
604 (name buffer-file-name)
605 insert-start
606 insert-end)
607 (unwind-protect
608 (progn
609 (save-excursion
610 ;; defeat file locking... don't try this at home, kids!
611 (setq buffer-file-name nil)
612 (forward-line 1)
613 (setq insert-start (point))
614 (if (eobp) (newline))
615 (insert image)
616 (setq insert-end (point)))
617 ; make sure the whole string is visible
618 (if (not (pos-visible-in-window-p insert-end))
619 (recenter (max 0
620 (- (window-height)
621 (count-lines insert-start insert-end)
622 2))))
623 (let ((char (read-char)))
624 (or (eq char exit-char)
625 (setq unread-command-char char))))
626 (if insert-end
627 (save-excursion
628 (delete-region insert-start insert-end)))
629 (setq buffer-file-name name)
630 (set-buffer-modified-p modified)))))
632 (defun YaTeX-math-insert-sequence (&optional force initial)
633 "Insert math-mode sequence with image completion."
634 (interactive "P")
635 (let*((key (or initial "")) regkey str last-char list i
636 (case-fold-search nil) match sign
637 (this-key (char-to-string last-command-char))
638 (alistsym (cdr (assoc this-key YaTeX-math-key-list)))
639 (alistname (symbol-name alistsym))
640 (alist (symbol-value alistsym))
641 (n (length alist)) (beg (point)) result)
642 (if initial (insert YaTeX-ec (car (cdr (assoc initial alist)))))
643 (if (string-match "^YaTeX-" alistname)
644 (setq alistname (substring alistname (length "YaTeX-"))))
645 (setq alistname (substring alistname 0 (string-match "-" alistname)))
646 (setq result
647 (catch 'complete
648 (if (and (not force)
649 (if YaTeX-auto-math-mode
650 (not (YaTeX-in-math-mode-p))
651 (not YaTeX-math-mode)))
652 (throw 'complete 'escape));this tag should be exit, but...
653 (while t
654 (message "%ssequence%s: %s"
655 (if YaTeX-simple-messages "" (concat alistname " "))
656 (if YaTeX-simple-messages "" "(TAB for menu)") key)
657 (setq last-char (read-char)
658 key (concat key (char-to-string last-char))
659 i 0)
660 (cond
661 ((string= key this-key) ;;invoke key itself
662 (throw 'complete 'escape))
663 ((string= key YaTeX-math-exit-key) ;;exit from math-mode
664 (throw 'complete 'exit))
665 ((string-match "\r" key) ;;RET = kakutei
666 (throw 'complete 'select))
667 ((string-match "[\C-g\C-c]" key) ;;C-g = abort
668 (throw 'complete 'abort))
669 ((string-match "[\t\n]" key) ;;TAB, LFD = menu
670 (throw 'complete 'menu))
671 ((string-match "[\C-h\C-?]" key) ;;BS, DEL = BS
672 (if (< (length key) 2) (throw 'complete 'abort))
673 (setq key (substring key 0 -2))))
675 (setq regkey (concat "^" (regexp-quote key)))
676 (message "Sequence%s: %s"
677 (if YaTeX-simple-messages "" "(TAB for menu)") key)
678 (if
679 (catch 'found
680 ;;(1)input string strictly matches with alist
681 (setq match (assoc key alist))
682 ;;remember previous match
684 ;;(2)search partial match into alist
685 (setq list alist)
686 (while (< i n)
687 (if (string-match
688 regkey
689 ;;(aref array i)
690 ;;(car (nth i alist))
691 (car (car list)))
692 (progn
693 (or match
694 ;;(setq match (nth i alist))
695 (setq match (car list)))
696 (throw 'found t)))
697 (setq i (1+ i) list (cdr list)))) ;catch 'found
698 nil ;;if any match, continue reading
699 ;;else reading of sequence has been done.
700 (message "complete.")
701 (throw 'complete t))
703 (if match
704 (progn (delete-region beg (point))
705 (setq single-command (car (cdr match)))
706 (insert YaTeX-ec single-command)
707 (if (and YaTeX-math-need-image
708 (setq sign (YaTeX-math-get-sign match)))
709 (YaTeX-math-show-image (concat sign "\n")))
710 )
711 nil)
712 )))
713 (delete-region beg (point))
714 (cond
715 ((memq result '(t select))
716 (if (eq result t)
717 (setq unread-command-char last-char)
718 (message "Done."))
719 (if (assoc single-command section-table)
720 (YaTeX-make-section nil nil nil single-command)
721 (setq YaTeX-current-completion-type 'maketitle)
722 (YaTeX-make-singlecmd single-command)))
723 ((eq result 'abort)
724 (message "Abort."))
725 ((eq result 'escape)
726 (call-interactively (global-key-binding this-key)))
727 ((eq result 'exit)
728 (YaTeX-toggle-math-mode))
729 ((eq result 'menu)
730 (setq key (concat "^" (regexp-quote (substring key 0 -1))))
731 (insert (YaTeX-math-show-menu key))))))
733 ;; ----- Change image completion types -----
734 (defun YaTeX-math-member-p (item)
735 "Check if ITEM is a member of image completion.
736 If so return the cons of its invocation key and image-string."
737 (let ((lists YaTeX-math-key-list) list)
738 (catch 'found
739 (while lists
740 (setq list (symbol-value (cdr (car lists))))
741 (while list
742 (if (string= item (nth 1 (car list)))
743 (throw 'found (cons (car (car lists)) (nth 0 (car list)))))
744 (setq list (cdr list)))
745 (setq lists (cdr lists))))))
747 ;;; ----- for AMS LaTeX (by matsu@math.s.chiba-u.ac.jp) -----
748 (defvar YaTeX-ams-paren-modifier
749 '(("Biggl" . "Biggr") ("biggl" . "biggr")
750 ("Bigl" . "Bigr") ("bigl" . "bigr")
751 ("left" . "right") ("" . ""))
752 "Alist of modifier of parentheses.")
754 (defvar YaTeX-left-paren "(\\|\\[\\|\\\\{")
755 (defvar YaTeX-right-paren ")\\|\\]\\|\\\\}")
756 (defvar YaTeX-paren
757 (concat YaTeX-left-paren "\\|" YaTeX-right-paren))
759 (defun YaTeX-on-parenthesis-p ()
760 "If cursor is on an (AMS-LaTeX) parenthesis, return the parenthesis."
761 (interactive)
762 (let* ((list YaTeX-ams-paren-modifier)
763 (longest 0) ;; the longest length of parenthesis command strings
764 (flag t) ;; flag for whether on braces not following \
765 (point (point))
766 (move 0)
767 (paren))
768 (while list
769 (setq longest
770 (max longest (length (car (car list))) (length (cdr (car list)))))
771 (setq list (cdr list)))
772 (save-excursion
773 ;; search {} and, if it does not follow `\', set flag nil.
774 ;; if it is right after `\', set flag t and move to the position of \.
775 ;; mmmmm.
776 (if (looking-at "{\\|}")
777 (if (not (equal (char-after (1- (point))) 92))
778 (setq flag nil)
779 (forward-char -1)))
780 ;; if flag is nil, do nothing.
781 (if (and flag (re-search-forward YaTeX-paren
782 (+ (point) 3 longest) t))
783 (progn
784 (setq move (- (point) point))
785 (setq paren (match-string 0))
786 (setq list YaTeX-ams-paren-modifier)
787 ;; criterion for whether on [] () \{\} or not.
788 (if (string-match YaTeX-left-paren paren)
789 (while (and list flag)
790 (let* ((mod (car (car list)))
791 (mod-length 0) ;; length of modifier
792 paren-regexp ;; regexp of paren.
793 mod-regexp) ;; regexp of modifier.
794 (if (> (length mod) 0)
795 (setq mod-regexp (concat "\\\\" mod)
796 mod-length (1+ (length mod))))
797 (cond ((string= paren "\\{")
798 (setq paren-regexp (concat "\\" paren)))
799 ((string= paren "[")
800 (setq paren-regexp "\\["))
801 (t (setq paren-regexp paren)))
802 (save-excursion
803 (if (and (>= (- (point) (point-min))
804 (+ mod-length (length paren)))
805 (not (forward-char
806 (- 0 mod-length (length paren))))
807 (looking-at (concat "\\(" mod-regexp "\\)\\("
808 paren-regexp "\\)")))
809 (setq flag nil)))
810 (setq list (cdr list))))
811 (while (and list flag)
812 (let* ((mod (cdr (car list)))
813 (mod-length 0)
814 paren-regexp
815 mod-regexp)
816 (if (> (length mod) 0)
817 (setq mod-regexp (concat "\\\\" mod)
818 mod-length (1+ (length mod))))
819 (cond ((string= paren "\\}")
820 (setq paren-regexp (concat "\\" paren)))
821 ((string= paren "]")
822 (setq paren-regexp "\\]"))
823 (t (setq paren-regexp paren)))
824 (save-excursion
825 (if (and (>= (- (point) (point-min))
826 (+ mod-length (length paren)))
827 (not (forward-char
828 (- 0 mod-length (length paren))))
829 (looking-at (concat "\\(" mod-regexp "\\)\\("
830 paren-regexp "\\)")))
831 (setq flag nil)))
832 (setq list (cdr list)))))
833 (if (<= move (length (match-string 0)))
834 (match-string 0)))))))
836 (defun YaTeX-goto-open-paren (&optional jumpto-co)
837 "Jump to the exact position of open parenthesis.
838 If optional argument JUMPTO-CO is non-nil, goto corresponding parentheses."
839 (interactive)
840 (let ((paren)
841 (backslash-syntax (char-to-string (char-syntax ?\\))))
842 (if (setq paren (YaTeX-on-parenthesis-p))
843 (if (string-match "(\\|{\\|\\[" paren (1- (length paren)))
844 (progn
845 (re-search-forward "(\\|{\\|\\[" (+ (point) (length paren)) t)
846 (backward-char)
847 (if jumpto-co
848 (unwind-protect
849 (progn
850 (modify-syntax-entry ?\\ " ")
851 (forward-list)
852 (backward-char))
853 (modify-syntax-entry ?\\ backslash-syntax)))
854 (point))
855 (re-search-forward ")\\|}\\|\\]" (+ (point) (length paren)) t)
856 (unwind-protect
857 (progn
858 (modify-syntax-entry ?\\ " ")
859 (backward-list)
860 (point))
861 (modify-syntax-entry ?\\ backslash-syntax))))))
863 ;;;###autoload
864 (defun YaTeX-goto-corresponding-paren ()
865 "Go to corresponding mathematical parentheses."
866 (if (YaTeX-on-parenthesis-p)
867 (YaTeX-goto-open-paren t)
868 nil))
870 (defun YaTeX-change-parentheses ()
871 "Change the size of parentheses, braces, and brackets of AMS-LaTeX."
872 (interactive)
873 (if (not (and YaTeX-use-AMS-LaTeX (YaTeX-on-parenthesis-p)))
874 nil
875 (let* ((mod (match-string 1)) ;; modifier
876 (paren (if mod (match-string 2) (match-string 0))) ;; paren
877 (mod-length (if (or (string= mod "\\left") (string= mod "\\right"))
878 5 ;; 5 in case left or right
879 (length mod))) ;; length of modifier
880 (paren-length (length paren)) ;; length of paren
881 (length (+ mod-length paren-length)) ;; length of whole string
882 (big-p t) ;; flag whether new modifier is "[Bb]ig+" or not.
883 size ;; left, big, Big etc.
884 type ;; parentheses type
885 lr ;; "l" or "r".
886 char newsize newsize-length
887 (backslash-syntax (char-to-string (char-syntax ?\\)))
888 (case-fold-search))
889 ;; decide lr and size from mod and paren.
890 (cond ((string-match "\\(\\\\[Bb]ig+\\)[lr]" mod)
891 (setq size (substring mod 1 (match-end 1))
892 lr (substring mod (match-end 1) (match-end 0))))
893 ((string-match "\\\\left" mod)
894 (setq size "left-right" lr "l"))
895 ((string-match "\\\\right" mod)
896 (setq size "left-right" lr "r"))
897 ((string-match "(\\|\\[\\|\\\\{" paren)
898 (setq size "null" lr "l"))
899 ((string-match ")\\|\\]\\|\\\\}" paren)
900 (setq size "null" lr "r"))
901 (t
902 (setq size nil lr nil)))
903 (while (not newsize)
904 (message (format (concat "Change from %s: "
905 "l(big) L(Big) h(bigg) H(Bigg) "
906 "r(left-right) n(NONE) ( { [") size))
907 (setq char (read-char)
908 newsize (cond ((char-equal char ?l) "\\big")
909 ((char-equal char ?L) "\\Big")
910 ((char-equal char ?h) "\\bigg")
911 ((char-equal char ?H) "\\Bigg")
912 ((char-equal char ?r)
913 (setq big-p nil) "\\left")
914 ((memq char '(?\( ?\)))
915 (setq big-p nil type '("(" . ")")) "")
916 ((memq char '(?\{ ?\}))
917 (setq big-p nil type '("\\{" . "\\}")) "")
918 ((memq char '(?\[ ?\]))
919 (setq big-p nil type '("[" . "]")) "")
920 ((char-equal char ?n)
921 (setq big-p nil) "")
922 (t nil))
923 newsize-length (length newsize)))
924 (YaTeX-goto-open-paren)
925 (forward-char)
926 (cond
927 (type
928 (delete-region (point) (- (point) paren-length))
929 (save-excursion (insert (car type))))
930 (t
931 (delete-region (- (point) length) (- (point) paren-length))
932 (backward-char paren-length)))
933 (insert-string newsize)
934 (if big-p (insert ?l))
935 (unwind-protect
936 (progn
937 (modify-syntax-entry ?\\ " ")
938 (forward-list)
939 (if (string= size "left-right") (setq length (1+ length)))
940 (if (eq char ?r) (setq newsize "\\right"))
941 (cond
942 (type
943 (delete-region (point) (- (point) paren-length))
944 (insert (cdr type)))
945 (t
946 (delete-region (- (point) length) (- (point) paren-length))
947 (backward-char paren-length)
948 (insert-string newsize)
949 (if big-p (insert ?r))
950 (forward-char paren-length)))
951 (if (string= lr "l") (backward-list)))
952 (modify-syntax-entry ?\\ backslash-syntax))
953 t)))
955 (defun YaTeX-insert-amsparens-region (beg end char)
956 (interactive "r\ncWhich one ? l(big) L(Big) h(bigg) H(Bigg): ")
957 (let* ((case-fold-search)
958 (st (cond ((char-equal char ?l) "big")
959 ((char-equal char ?L) "Big")
960 ((char-equal char ?h) "bigg")
961 ((char-equal char ?H) "Bigg"))))
962 (if st
963 (YaTeX-insert-braces-region
964 beg end (concat "\\" st "l(") (concat "\\" st "r)"))
965 (YaTeX-insert-braces-region beg end "(" ")"))))
967 (defun YaTeX-insert-amsbraces-region (beg end char)
968 (interactive "r\ncWhich one ? l(big) L(Big) h(bigg) H(Bigg): ")
969 (let* ((case-fold-search)
970 (st (cond ((char-equal char ?l) "big")
971 ((char-equal char ?L) "Big")
972 ((char-equal char ?h) "bigg")
973 ((char-equal char ?H) "Bigg"))))
974 (if st
975 (YaTeX-insert-braces-region
976 beg end (concat "\\" st "l\\{") (concat "\\" st "r\\}"))
977 (YaTeX-insert-braces-region beg end "\\{" "\\}"))))
979 (defun YaTeX-insert-amsbrackets-region (beg end char)
980 (interactive "r\ncWhich one ? l(big) L(Big) h(bigg) H(Bigg): ")
981 (let* ((case-fold-search)
982 (st (cond ((char-equal char ?l) "big")
983 ((char-equal char ?L) "Big")
984 ((char-equal char ?h) "bigg")
985 ((char-equal char ?H) "Bigg"))))
986 (if st
987 (YaTeX-insert-braces-region
988 beg end (concat "\\" st "l[") (concat "\\" st "r]"))
989 (YaTeX-insert-braces-region beg end "[" "]"))))
992 ;;
993 (provide 'yatexmth)
995 ; Local variables:
996 ; fill-prefix: ";;; "
997 ; paragraph-start: "^$\\| \\|;;;$"
998 ; paragraph-separate: "^$\\| \\|;;;$"
999 ; End: