yatex

view yatexmth.el @ 84:73cba5ddd111

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