yatex

view yatexmth.el @ 72:0aaebd07dad0

Support font-lock on XEmacs-21, Emacs-20, Emacs-21. Support Emacs-21.
author yuuji
date Mon, 25 Dec 2000 10:19:28 +0000
parents 44e3a5e1e883
children 1b172d26b55e
line source
1 ;;; -*- Emacs-Lisp -*-
2 ;;; YaTeX math-mode-specific functions.
3 ;;; yatexmth.el
4 ;;; (c)1993-2000 by HIROSE Yuuji [yuuji@yatex.org]
5 ;;; Last modified Tue Dec 19 11:03:51 2000 on buell
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 (defun YaTeX-in-math-mode-p ()
470 "If current position is supposed to be in LaTeX-math-mode, return t."
471 (or (YaTeX-quick-in-environment-p
472 (append
473 '("math" "eqnarray" "equation" "eqnarray*" "displaymath");LaTeX
474 (if YaTeX-use-AMS-LaTeX
475 ;; And math modes of AMS-LaTeX
476 '("align" "align*" "split" "multline" "multline*" "gather"
477 "gather*" "aligned*" "gathered" "gathered*" "alignat"
478 "equation*" "cases"
479 "alignat*" "xalignat" "xalignat*" "xxalignat" "xxalignat*"))))
480 (let*((p (point)) (nest 0) me0
481 (delim (concat YaTeX-sectioning-regexp "\\|^$\\|^\C-l"))
482 (boundary
483 (save-excursion
484 (if (looking-at delim)
485 (goto-char (max (point-min) (1- (point)))))
486 (re-search-backward delim nil 1)
487 (point))))
488 (save-excursion
489 (cond
490 ((catch 'open
491 (save-excursion
492 (while (and (>= nest 0)
493 (re-search-backward
494 (concat YaTeX-ec-regexp ;\
495 "\\([()]\\|[][]\\)") boundary t))
496 (setq me0 (match-end 0))
497 (if (or (YaTeX-on-comment-p)
498 (YaTeX-literal-p)) nil
499 (if (or (= (char-after (1- me0)) ?\))
500 (= (char-after (1- me0)) ?\]))
501 (setq nest (1+ nest))
502 (if (= (preceding-char) ?\\ ) nil;;\\[5pt]
503 (setq nest (1- nest))))))
504 (if (< nest 0) (throw 'open t))))
505 t)
506 (t (catch 'dollar
507 (while ;(search-backward "$" boundary t);little bit fast.
508 (YaTeX-re-search-active-backward ;;;;;; Too slow???
509 "\\$" (concat "[^\\\\]" YaTeX-comment-prefix) boundary t)
510 (cond
511 ((equal (char-after (1- (point))) ?$) ; $$ equation $$
512 (backward-char 1)
513 (setq nest (1+ nest)))
514 ((let ((YaTeX-verbatim-environments
515 (append YaTeX-math-verbatim-environments
516 YaTeX-verbatim-environments)))
517 (YaTeX-literal-p))
518 nil)
519 ((and (equal (char-after (1- (point))) ?\\ )
520 (not (equal (char-after (- (point) 3)) ?\\ )))
521 nil) ;\$
522 (t (setq nest (1+ nest)))))
523 (if (= (% nest 2) 1) (throw 'dollar t))))))))
524 )
526 (defun YaTeX-math-display-list (list cols)
527 (goto-char (point-max))
528 (if (= cols 0) (if (not (eolp)) (newline 1))
529 (forward-line -1)
530 (while (looking-at "[ \t\n]") (forward-line -1)))
531 (end-of-line)
532 (let ((indent (* YaTeX-math-display-width cols)) sign str to)
533 (indent-to indent)
534 (insert (car list))
535 (indent-to (setq indent (+ indent YaTeX-math-max-key)))
536 (insert "\\" (car (cdr list)))
537 (setq indent (+ indent YaTeX-math-max-seq))
538 (setq sign (YaTeX-math-get-sign list))
539 (while (and sign (not (string= "" sign)))
540 (setq to (string-match "\n" sign)
541 str (if to (substring sign 0 to) sign))
542 (end-of-line)
543 (indent-to indent)
544 (insert str)
545 (cond ((eobp) (newline 1))
546 ((> cols 0) (forward-line 1)))
547 (setq sign (if to (substring sign (1+ to)) "")))))
549 (defvar YaTeX-math-menu-buffer "*math-mode-signs*")
551 (defun YaTeX-math-show-menu (match-str)
552 (save-window-excursion
553 (YaTeX-showup-buffer YaTeX-math-menu-buffer nil t)
554 (let ((maxcols (max 1 (/ (YaTeX-screen-width) YaTeX-math-sign-width)))
555 (case-fold-search nil)
556 (cols 0) (list alist) command)
557 (erase-buffer)
558 (insert
559 "Candidates of sign. [n:next p:prev f:forw b:back q:quit RET:select]\n")
560 (insert YaTeX-math-indicator "\t")
561 (insert YaTeX-math-indicator)
562 (newline 1)
563 (insert-char ?- (1- (YaTeX-screen-width)))
564 (newline 1)
565 (while list
566 (if (string-match match-str (car (car list)))
567 (progn (YaTeX-math-display-list (car list) cols)
568 (setq cols (% (1+ cols) maxcols))))
569 (setq list (cdr list)))
570 (goto-line 4)
571 (use-local-map YaTeX-math-menu-map)
572 (setq buffer-read-only t)
573 (unwind-protect
574 (recursive-edit)
575 (skip-chars-backward "^ \t\n")
576 (setq command
577 (if (re-search-forward YaTeX-math-cmd-regexp nil t)
578 (buffer-substring
579 (match-beginning 0)
580 (prog2 (skip-chars-forward "^ \t\n") (point)))
581 nil))
582 (kill-buffer YaTeX-math-menu-buffer))
583 command))
584 )
586 ;
587 (defun YaTeX-math-show-image (image &optional exit-char)
588 "Momentarily display IMAGE at the beginning of the next line;
589 erase it on the next keystroke. The window is recentered if necessary
590 to make the whole string visible. If the window isn't large enough,
591 at least you get to read the beginning."
592 (if (and image (not (string= image "")))
593 (let ((buffer-read-only nil)
594 (modified (buffer-modified-p))
595 (name buffer-file-name)
596 insert-start
597 insert-end)
598 (unwind-protect
599 (progn
600 (save-excursion
601 ;; defeat file locking... don't try this at home, kids!
602 (setq buffer-file-name nil)
603 (forward-line 1)
604 (setq insert-start (point))
605 (if (eobp) (newline))
606 (insert image)
607 (setq insert-end (point)))
608 ; make sure the whole string is visible
609 (if (not (pos-visible-in-window-p insert-end))
610 (recenter (max 0
611 (- (window-height)
612 (count-lines insert-start insert-end)
613 2))))
614 (let ((char (read-char)))
615 (or (eq char exit-char)
616 (setq unread-command-char char))))
617 (if insert-end
618 (save-excursion
619 (delete-region insert-start insert-end)))
620 (setq buffer-file-name name)
621 (set-buffer-modified-p modified)))))
623 (defun YaTeX-math-insert-sequence (&optional force initial)
624 "Insert math-mode sequence with image completion."
625 (interactive "P")
626 (let*((key (or initial "")) regkey str last-char list i
627 (case-fold-search nil) match sign
628 (this-key (char-to-string last-command-char))
629 (alistsym (cdr (assoc this-key YaTeX-math-key-list)))
630 (alistname (symbol-name alistsym))
631 (alist (symbol-value alistsym))
632 (n (length alist)) (beg (point)) result)
633 (if initial (insert YaTeX-ec (car (cdr (assoc initial alist)))))
634 (if (string-match "^YaTeX-" alistname)
635 (setq alistname (substring alistname (length "YaTeX-"))))
636 (setq alistname (substring alistname 0 (string-match "-" alistname)))
637 (setq result
638 (catch 'complete
639 (if (and (not force)
640 (if YaTeX-auto-math-mode
641 (not (YaTeX-in-math-mode-p))
642 (not YaTeX-math-mode)))
643 (throw 'complete 'escape));this tag should be exit, but...
644 (while t
645 (message "%ssequence%s: %s"
646 (if YaTeX-simple-messages "" (concat alistname " "))
647 (if YaTeX-simple-messages "" "(TAB for menu)") key)
648 (setq last-char (read-char)
649 key (concat key (char-to-string last-char))
650 i 0)
651 (cond
652 ((string= key this-key) ;;invoke key itself
653 (throw 'complete 'escape))
654 ((string= key YaTeX-math-exit-key) ;;exit from math-mode
655 (throw 'complete 'exit))
656 ((string-match "\r" key) ;;RET = kakutei
657 (throw 'complete 'select))
658 ((string-match "[\C-g\C-c]" key) ;;C-g = abort
659 (throw 'complete 'abort))
660 ((string-match "[\t\n]" key) ;;TAB, LFD = menu
661 (throw 'complete 'menu))
662 ((string-match "[\C-h\C-?]" key) ;;BS, DEL = BS
663 (if (< (length key) 2) (throw 'complete 'abort))
664 (setq key (substring key 0 -2))))
666 (setq regkey (concat "^" (regexp-quote key)))
667 (message "Sequence%s: %s"
668 (if YaTeX-simple-messages "" "(TAB for menu)") key)
669 (if
670 (catch 'found
671 ;;(1)input string strictly matches with alist
672 (setq match (assoc key alist))
673 ;;remember previous match
675 ;;(2)search partial match into alist
676 (setq list alist)
677 (while (< i n)
678 (if (string-match
679 regkey
680 ;;(aref array i)
681 ;;(car (nth i alist))
682 (car (car list)))
683 (progn
684 (or match
685 ;;(setq match (nth i alist))
686 (setq match (car list)))
687 (throw 'found t)))
688 (setq i (1+ i) list (cdr list)))) ;catch 'found
689 nil ;;if any match, continue reading
690 ;;else reading of sequence has been done.
691 (message "complete.")
692 (throw 'complete t))
694 (if match
695 (progn (delete-region beg (point))
696 (setq single-command (car (cdr match)))
697 (insert YaTeX-ec single-command)
698 (if (and YaTeX-math-need-image
699 (setq sign (YaTeX-math-get-sign match)))
700 (YaTeX-math-show-image (concat sign "\n")))
701 )
702 nil)
703 )))
704 (delete-region beg (point))
705 (cond
706 ((memq result '(t select))
707 (if (eq result t)
708 (setq unread-command-char last-char)
709 (message "Done."))
710 (if (assoc single-command section-table)
711 (YaTeX-make-section nil nil nil single-command)
712 (setq YaTeX-current-completion-type 'maketitle)
713 (YaTeX-make-singlecmd single-command)))
714 ((eq result 'abort)
715 (message "Abort."))
716 ((eq result 'escape)
717 (call-interactively (global-key-binding this-key)))
718 ((eq result 'exit)
719 (YaTeX-toggle-math-mode))
720 ((eq result 'menu)
721 (setq key (concat "^" (regexp-quote (substring key 0 -1))))
722 (insert (YaTeX-math-show-menu key))))))
724 ;; ----- Change image completion types -----
725 (defun YaTeX-math-member-p (item)
726 "Check if ITEM is a member of image completion.
727 If so return the cons of its invocation key and image-string."
728 (let ((lists YaTeX-math-key-list) list)
729 (catch 'found
730 (while lists
731 (setq list (symbol-value (cdr (car lists))))
732 (while list
733 (if (string= item (nth 1 (car list)))
734 (throw 'found (cons (car (car lists)) (nth 0 (car list)))))
735 (setq list (cdr list)))
736 (setq lists (cdr lists))))))
738 ;;; ----- for AMS LaTeX (by matsu@math.s.chiba-u.ac.jp) -----
739 (defvar YaTeX-ams-paren-modifier
740 '(("Biggl" . "Biggr") ("biggl" . "biggr")
741 ("Bigl" . "Bigr") ("bigl" . "bigr")
742 ("left" . "right") ("" . ""))
743 "Alist of modifier of parentheses.")
745 (defvar YaTeX-left-paren "(\\|\\[\\|\\\\{")
746 (defvar YaTeX-right-paren ")\\|\\]\\|\\\\}")
747 (defvar YaTeX-paren
748 (concat YaTeX-left-paren "\\|" YaTeX-right-paren))
750 (defun YaTeX-on-parenthesis-p ()
751 "If cursor is on an (AMS-LaTeX) parenthesis, return the parenthesis."
752 (interactive)
753 (let* ((list YaTeX-ams-paren-modifier)
754 (longest 0) ;; the longest length of parenthesis command strings
755 (flag t) ;; flag for whether on braces not following \
756 (point (point))
757 (move 0)
758 (paren))
759 (while list
760 (setq longest
761 (max longest (length (car (car list))) (length (cdr (car list)))))
762 (setq list (cdr list)))
763 (save-excursion
764 ;; search {} and, if it does not follow `\', set flag nil.
765 ;; if it is right after `\', set flag t and move to the position of \.
766 ;; mmmmm.
767 (if (looking-at "{\\|}")
768 (if (not (equal (char-after (1- (point))) 92))
769 (setq flag nil)
770 (forward-char -1)))
771 ;; if flag is nil, do nothing.
772 (if (and flag (re-search-forward YaTeX-paren
773 (+ (point) 3 longest) t))
774 (progn
775 (setq move (- (point) point))
776 (setq paren (match-string 0))
777 (setq list YaTeX-ams-paren-modifier)
778 ;; criterion for whether on [] () \{\} or not.
779 (if (string-match YaTeX-left-paren paren)
780 (while (and list flag)
781 (let* ((mod (car (car list)))
782 (mod-length 0) ;; length of modifier
783 paren-regexp ;; regexp of paren.
784 mod-regexp) ;; regexp of modifier.
785 (if (> (length mod) 0)
786 (setq mod-regexp (concat "\\\\" mod)
787 mod-length (1+ (length mod))))
788 (cond ((string= paren "\\{")
789 (setq paren-regexp (concat "\\" paren)))
790 ((string= paren "[")
791 (setq paren-regexp "\\["))
792 (t (setq paren-regexp paren)))
793 (save-excursion
794 (if (and (>= (- (point) (point-min))
795 (+ mod-length (length paren)))
796 (not (forward-char
797 (- 0 mod-length (length paren))))
798 (looking-at (concat "\\(" mod-regexp "\\)\\("
799 paren-regexp "\\)")))
800 (setq flag nil)))
801 (setq list (cdr list))))
802 (while (and list flag)
803 (let* ((mod (cdr (car list)))
804 (mod-length 0)
805 paren-regexp
806 mod-regexp)
807 (if (> (length mod) 0)
808 (setq mod-regexp (concat "\\\\" mod)
809 mod-length (1+ (length mod))))
810 (cond ((string= paren "\\}")
811 (setq paren-regexp (concat "\\" paren)))
812 ((string= paren "]")
813 (setq paren-regexp "\\]"))
814 (t (setq paren-regexp paren)))
815 (save-excursion
816 (if (and (>= (- (point) (point-min))
817 (+ mod-length (length paren)))
818 (not (forward-char
819 (- 0 mod-length (length paren))))
820 (looking-at (concat "\\(" mod-regexp "\\)\\("
821 paren-regexp "\\)")))
822 (setq flag nil)))
823 (setq list (cdr list)))))
824 (if (<= move (length (match-string 0)))
825 (match-string 0)))))))
827 (defun YaTeX-goto-open-paren ()
828 "Jump to the exact position of open parenthesis"
829 (interactive)
830 (let ((paren)
831 (backslash-syntax (char-to-string (char-syntax ?\\))))
832 (if (setq paren (YaTeX-on-parenthesis-p))
833 (if (string-match "(\\|{\\|\\[" paren (1- (length paren)))
834 (progn
835 (re-search-forward "(\\|{\\|\\[" (+ (point) (length paren)) t)
836 (backward-char))
837 (re-search-forward ")\\|}\\|\\]" (+ (point) (length paren)) t)
838 (unwind-protect
839 (progn
840 (modify-syntax-entry ?\\ " ")
841 (backward-list))
842 (modify-syntax-entry ?\\ backslash-syntax))))))
844 (defun YaTeX-change-parentheses ()
845 "Change the size of parentheses, braces, and brackets of AMS-LaTeX."
846 (interactive)
847 (if (not (and YaTeX-use-AMS-LaTeX (YaTeX-on-parenthesis-p)))
848 nil
849 (let* ((mod (match-string 1)) ;; modifier
850 (paren (if mod (match-string 2) (match-string 0))) ;; paren
851 (mod-length (if (or (string= mod "\\left") (string= mod "\\right"))
852 5 ;; 5 in case left or right
853 (length mod))) ;; length of modifier
854 (paren-length (length paren)) ;; length of paren
855 (length (+ mod-length paren-length)) ;; length of whole string
856 (big-p t) ;; flag whether new modifier is "[Bb]ig+" or not.
857 size ;; left, big, Big etc.
858 lr ;; "l" or "r".
859 char newsize newsize-length
860 (backslash-syntax (char-to-string (char-syntax ?\\)))
861 (case-fold-search))
862 ;; decide lr and size from mod and paren.
863 (cond ((string-match "\\(\\\\[Bb]ig+\\)[lr]" mod)
864 (setq size (substring mod 1 (match-end 1))
865 lr (substring mod (match-end 1) (match-end 0))))
866 ((string-match "\\\\left" mod)
867 (setq size "left-right" lr "l"))
868 ((string-match "\\\\right" mod)
869 (setq size "left-right" lr "r"))
870 ((string-match "(\\|\\[\\|\\\\{" paren)
871 (setq size "null" lr "l"))
872 ((string-match ")\\|\\]\\|\\\\}" paren)
873 (setq size "null" lr "r"))
874 (t
875 (setq size nil lr nil)))
876 (while (not newsize)
877 (message (format (concat "Change from %s: "
878 "l(big) L(Big) h(bigg) H(Bigg) "
879 "r(left-right) n(NONE) ") size))
880 (setq char (read-char)
881 newsize (cond ((char-equal char ?l) "\\big")
882 ((char-equal char ?L) "\\Big")
883 ((char-equal char ?h) "\\bigg")
884 ((char-equal char ?H) "\\Bigg")
885 ((char-equal char ?r)
886 (setq big-p nil) "\\left")
887 ((char-equal char ?n)
888 (setq big-p nil) "")
889 (t nil))
890 newsize-length (length newsize)))
891 (YaTeX-goto-open-paren)
892 (forward-char)
893 (delete-region (- (point) length) (- (point) paren-length))
894 (backward-char paren-length)
895 (insert-string newsize)
896 (if big-p (insert ?l))
897 (forward-char (1- paren-length))
898 (unwind-protect
899 (progn
900 (modify-syntax-entry ?\\ " ")
901 (forward-list)
902 (if (string= size "left-right") (setq length (1+ length)))
903 (if (eq char ?r) (setq newsize "\\right"))
904 (delete-region (- (point) length) (- (point) paren-length))
905 (backward-char paren-length)
906 (insert-string newsize)
907 (if big-p (insert ?r))
908 (forward-char paren-length)
909 (if (string= lr "l") (backward-list)))
910 (modify-syntax-entry ?\\ backslash-syntax))
911 t)))
913 (defun YaTeX-insert-amsparens-region (beg end char)
914 (interactive "r\ncWhich one ? l(big) L(Big) h(bigg) H(Bigg): ")
915 (let* ((case-fold-search)
916 (st (cond ((char-equal char ?l) "big")
917 ((char-equal char ?L) "Big")
918 ((char-equal char ?h) "bigg")
919 ((char-equal char ?H) "Bigg"))))
920 (if st
921 (YaTeX-insert-braces-region
922 beg end (concat "\\" st "l(") (concat "\\" st "r)"))
923 (YaTeX-insert-braces-region beg end "(" ")"))))
925 (defun YaTeX-insert-amsbraces-region (beg end char)
926 (interactive "r\ncWhich one ? l(big) L(Big) h(bigg) H(Bigg): ")
927 (let* ((case-fold-search)
928 (st (cond ((char-equal char ?l) "big")
929 ((char-equal char ?L) "Big")
930 ((char-equal char ?h) "bigg")
931 ((char-equal char ?H) "Bigg"))))
932 (if st
933 (YaTeX-insert-braces-region
934 beg end (concat "\\" st "l\\{") (concat "\\" st "r\\}"))
935 (YaTeX-insert-braces-region beg end "\\{" "\\}"))))
937 (defun YaTeX-insert-amsbrackets-region (beg end char)
938 (interactive "r\ncWhich one ? l(big) L(Big) h(bigg) H(Bigg): ")
939 (let* ((case-fold-search)
940 (st (cond ((char-equal char ?l) "big")
941 ((char-equal char ?L) "Big")
942 ((char-equal char ?h) "bigg")
943 ((char-equal char ?H) "Bigg"))))
944 (if st
945 (YaTeX-insert-braces-region
946 beg end (concat "\\" st "l[") (concat "\\" st "r]"))
947 (YaTeX-insert-braces-region beg end "[" "]"))))
950 ;;
951 (provide 'yatexmth)
953 ; Local variables:
954 ; fill-prefix: ";;; "
955 ; paragraph-start: "^$\\| \\|;;;$"
956 ; paragraph-separate: "^$\\| \\|;;;$"
957 ; End: