yatex

view yatexmth.el @ 130:8703f090c628

`[prefix] t e' for YaTeX-typeset-environment. It automatically mark current inner environment or portion of math mode, then call typeset-region on that region saving it in texput.tex.
author yuuji@gentei.org
date Thu, 27 May 2010 16:37:44 +0900
parents d5e19b856ca1
children 94a1370956a4
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 Thu May 27 16:33:55 2010 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 firstp dollar
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)
534 (throw 'open (cons (YaTeX-match-string 0) (point)))))))
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 (setq dollar "$")
545 (cond
546 ((equal (char-after (1- (point))) ?$) ; $$ equation $$
547 (setq dollar "$$")
548 (backward-char 1)
549 (setq nest (1+ nest)))
550 ((let ((YaTeX-verbatim-environments
551 (append YaTeX-math-verbatim-environments
552 YaTeX-verbatim-environments)))
553 (YaTeX-literal-p))
554 nil)
555 ((and (equal (char-after (1- (point))) ?\\ )
556 (not (equal (char-after (- (point) 3)) ?\\ )))
557 nil) ;\$
558 (t (setq nest (1+ nest))))
559 (if (and (= nest 1) (null firstp))
560 (setq firstp (cons dollar (point)))))
561 (if (= (% nest 2) 1)
562 (throw 'dollar firstp))))))))))
564 (defun YaTeX-mark-mathenv ()
565 "Mark current mathematic environment."
566 (interactive)
567 (let ((mmp (YaTeX-in-math-mode-p)) type bpt)
568 (if (or (null mmp) (not (stringp (setq type (car mmp)))))
569 nil ;if nil or not string, do nothing
570 (setq bpt (cdr mmp))
571 (goto-char bpt)
572 (cond
573 ((string-match "\\$" type)
574 (set-mark-command nil)
575 (skip-chars-forward "$")
576 (YaTeX-search-active-forward
577 type YaTeX-comment-prefix nil) ;if it cause error, obey it
578 (goto-char (match-end 0)))
579 ;;
580 ((string-match "^\\\\[\\[(]" type)
581 (set-mark-command nil)
582 (YaTeX-goto-corresponding-leftright)
583 (skip-chars-forward "])\\\\"))
584 ;;
585 ((string-match "^[a-z]" type) ; \begin\end type math
586 (set-mark-command nil)
587 (YaTeX-goto-corresponding-environment)
588 (goto-char (match-end 0)))))))
590 (defun YaTeX-math-display-list (list cols)
591 (goto-char (point-max))
592 (if (= cols 0) (if (not (eolp)) (newline 1))
593 (forward-line -1)
594 (while (looking-at "[ \t\n]") (forward-line -1)))
595 (end-of-line)
596 (let ((indent (* YaTeX-math-display-width cols)) sign str to)
597 (indent-to indent)
598 (insert (car list))
599 (indent-to (setq indent (+ indent YaTeX-math-max-key)))
600 (insert "\\" (car (cdr list)))
601 (setq indent (+ indent YaTeX-math-max-seq))
602 (setq sign (YaTeX-math-get-sign list))
603 (while (and sign (not (string= "" sign)))
604 (setq to (string-match "\n" sign)
605 str (if to (substring sign 0 to) sign))
606 (end-of-line)
607 (indent-to indent)
608 (insert str)
609 (cond ((eobp) (newline 1))
610 ((> cols 0) (forward-line 1)))
611 (setq sign (if to (substring sign (1+ to)) "")))))
613 (defvar YaTeX-math-menu-buffer "*math-mode-signs*")
615 (defun YaTeX-math-show-menu (match-str)
616 (save-window-excursion
617 (YaTeX-showup-buffer YaTeX-math-menu-buffer nil t)
618 (let ((maxcols (max 1 (/ (YaTeX-screen-width) YaTeX-math-sign-width)))
619 (case-fold-search nil)
620 (cols 0) (list alist) command)
621 (erase-buffer)
622 (insert
623 "Candidates of sign. [n:next p:prev f:forw b:back q:quit RET:select]\n")
624 (insert YaTeX-math-indicator "\t")
625 (insert YaTeX-math-indicator)
626 (newline 1)
627 (insert-char ?- (1- (YaTeX-screen-width)))
628 (newline 1)
629 (while list
630 (if (string-match match-str (car (car list)))
631 (progn (YaTeX-math-display-list (car list) cols)
632 (setq cols (% (1+ cols) maxcols))))
633 (setq list (cdr list)))
634 (goto-line 4)
635 (use-local-map YaTeX-math-menu-map)
636 (setq buffer-read-only t)
637 (unwind-protect
638 (recursive-edit)
639 (skip-chars-backward "^ \t\n")
640 (setq command
641 (if (re-search-forward YaTeX-math-cmd-regexp nil t)
642 (buffer-substring
643 (match-beginning 0)
644 (prog2 (skip-chars-forward "^ \t\n") (point)))
645 nil))
646 (kill-buffer YaTeX-math-menu-buffer))
647 command)))
649 ;
650 (defun YaTeX-math-show-image (image &optional exit-char)
651 "Momentarily display IMAGE at the beginning of the next line;
652 erase it on the next keystroke. The window is recentered if necessary
653 to make the whole string visible. If the window isn't large enough,
654 at least you get to read the beginning."
655 (if (and image (not (string= image "")))
656 (let ((buffer-read-only nil)
657 (modified (buffer-modified-p))
658 (name buffer-file-name)
659 insert-start
660 insert-end)
661 (unwind-protect
662 (progn
663 (save-excursion
664 ;; defeat file locking... don't try this at home, kids!
665 (setq buffer-file-name nil)
666 (forward-line 1)
667 (setq insert-start (point))
668 (if (eobp) (newline))
669 (insert image)
670 (setq insert-end (point)))
671 ; make sure the whole string is visible
672 (if (not (pos-visible-in-window-p insert-end))
673 (recenter (max 0
674 (- (window-height)
675 (count-lines insert-start insert-end)
676 2))))
677 (let ((char (read-char)))
678 (or (eq char exit-char)
679 (setq unread-command-char char))))
680 (if insert-end
681 (save-excursion
682 (delete-region insert-start insert-end)))
683 (setq buffer-file-name name)
684 (set-buffer-modified-p modified)))))
686 (defun YaTeX-math-insert-sequence (&optional force initial)
687 "Insert math-mode sequence with image completion."
688 (interactive "P")
689 (let*((key (or initial "")) regkey str last-char list i
690 (case-fold-search nil) match sign
691 (this-key (char-to-string last-command-char))
692 (alistsym (cdr (assoc this-key YaTeX-math-key-list)))
693 (alistname (symbol-name alistsym))
694 (alist (symbol-value alistsym))
695 (n (length alist)) (beg (point)) result)
696 (if initial (insert YaTeX-ec (car (cdr (assoc initial alist)))))
697 (if (string-match "^YaTeX-" alistname)
698 (setq alistname (substring alistname (length "YaTeX-"))))
699 (setq alistname (substring alistname 0 (string-match "-" alistname)))
700 (setq result
701 (catch 'complete
702 (if (and (not force)
703 (if YaTeX-auto-math-mode
704 (not (YaTeX-in-math-mode-p))
705 (not YaTeX-math-mode)))
706 (throw 'complete 'escape));this tag should be exit, but...
707 (while t
708 (message "%ssequence%s: %s"
709 (if YaTeX-simple-messages "" (concat alistname " "))
710 (if YaTeX-simple-messages "" "(TAB for menu)") key)
711 (setq last-char (read-char)
712 key (concat key (char-to-string last-char))
713 i 0)
714 (cond
715 ((string= key this-key) ;;invoke key itself
716 (throw 'complete 'escape))
717 ((string= key YaTeX-math-exit-key) ;;exit from math-mode
718 (throw 'complete 'exit))
719 ((string-match "\r" key) ;;RET = kakutei
720 (throw 'complete 'select))
721 ((string-match "[\C-g\C-c]" key) ;;C-g = abort
722 (throw 'complete 'abort))
723 ((string-match "[\t\n]" key) ;;TAB, LFD = menu
724 (throw 'complete 'menu))
725 ((string-match "[\C-h\C-?]" key) ;;BS, DEL = BS
726 (if (< (length key) 2) (throw 'complete 'abort))
727 (setq key (substring key 0 -2))))
729 (setq regkey (concat "^" (regexp-quote key)))
730 (message "Sequence%s: %s"
731 (if YaTeX-simple-messages "" "(TAB for menu)") key)
732 (if
733 (catch 'found
734 ;;(1)input string strictly matches with alist
735 (setq match (assoc key alist))
736 ;;remember previous match
738 ;;(2)search partial match into alist
739 (setq list alist)
740 (while (< i n)
741 (if (string-match
742 regkey
743 ;;(aref array i)
744 ;;(car (nth i alist))
745 (car (car list)))
746 (progn
747 (or match
748 ;;(setq match (nth i alist))
749 (setq match (car list)))
750 (throw 'found t)))
751 (setq i (1+ i) list (cdr list)))) ;catch 'found
752 nil ;;if any match, continue reading
753 ;;else reading of sequence has been done.
754 (message "complete.")
755 (throw 'complete t))
757 (if match
758 (progn (delete-region beg (point))
759 (setq YaTeX-single-command (car (cdr match)))
760 (insert YaTeX-ec YaTeX-single-command)
761 (if (and YaTeX-math-need-image
762 (setq sign (YaTeX-math-get-sign match)))
763 (YaTeX-math-show-image (concat sign "\n")))
764 )
765 nil)
766 )))
767 (delete-region beg (point))
768 (cond
769 ((memq result '(t select))
770 (if (eq result t)
771 (setq unread-command-char last-char)
772 (message "Done."))
773 (if (assoc YaTeX-single-command section-table)
774 (YaTeX-make-section nil nil nil YaTeX-single-command)
775 (setq YaTeX-current-completion-type 'maketitle)
776 (YaTeX-make-singlecmd YaTeX-single-command)))
777 ((eq result 'abort)
778 (message "Abort."))
779 ((eq result 'escape)
780 (call-interactively (lookup-key global-map this-key)))
781 ((eq result 'exit)
782 (YaTeX-toggle-math-mode))
783 ((eq result 'menu)
784 (setq key (concat "^" (regexp-quote (substring key 0 -1))))
785 (insert (YaTeX-math-show-menu key))))))
787 ;; ----- Change image completion types -----
788 (defun YaTeX-math-member-p (item)
789 "Check if ITEM is a member of image completion.
790 If so return the cons of its invocation key and image-string."
791 (let ((lists YaTeX-math-key-list) list)
792 (catch 'found
793 (while lists
794 (setq list (symbol-value (cdr (car lists))))
795 (while list
796 (if (string= item (nth 1 (car list)))
797 (throw 'found (cons (car (car lists)) (nth 0 (car list)))))
798 (setq list (cdr list)))
799 (setq lists (cdr lists))))))
801 ;;; ----- for AMS LaTeX (by matsu@math.s.chiba-u.ac.jp) -----
802 (defvar YaTeX-ams-paren-modifier
803 '(("Biggl" . "Biggr") ("biggl" . "biggr")
804 ("Bigl" . "Bigr") ("bigl" . "bigr")
805 ("left" . "right") ("" . ""))
806 "Alist of modifier of parentheses.")
808 (defvar YaTeX-left-paren "(\\|\\[\\|\\\\{")
809 (defvar YaTeX-right-paren ")\\|\\]\\|\\\\}")
810 (defvar YaTeX-paren
811 (concat YaTeX-left-paren "\\|" YaTeX-right-paren))
813 (defun YaTeX-on-parenthesis-p ()
814 "If cursor is on an (AMS-LaTeX) parenthesis, return the parenthesis."
815 (interactive)
816 (let* ((list YaTeX-ams-paren-modifier)
817 (longest 0) ;; the longest length of parenthesis command strings
818 (flag t) ;; flag for whether on braces not following \
819 (point (point))
820 (move 0)
821 (paren))
822 (while list
823 (setq longest
824 (max longest (length (car (car list))) (length (cdr (car list)))))
825 (setq list (cdr list)))
826 (save-excursion
827 ;; search {} and, if it does not follow `\', set flag nil.
828 ;; if it is right after `\', set flag t and move to the position of \.
829 ;; mmmmm.
830 (if (looking-at "{\\|}")
831 (if (not (equal (char-after (1- (point))) 92))
832 (setq flag nil)
833 (forward-char -1)))
834 ;; if flag is nil, do nothing.
835 (if (and flag (re-search-forward YaTeX-paren
836 (+ (point) 3 longest) t))
837 (progn
838 (setq move (- (point) point))
839 (setq paren (YaTeX-match-string 0))
840 (setq list YaTeX-ams-paren-modifier)
841 ;; criterion for whether on [] () \{\} or not.
842 (if (string-match YaTeX-left-paren paren)
843 (while (and list flag)
844 (let* ((mod (car (car list)))
845 (mod-length 0) ;; length of modifier
846 paren-regexp ;; regexp of paren.
847 mod-regexp) ;; regexp of modifier.
848 (if (> (length mod) 0)
849 (setq mod-regexp (concat "\\\\" mod)
850 mod-length (1+ (length mod))))
851 (cond ((string= paren "\\{")
852 (setq paren-regexp (concat "\\" paren)))
853 ((string= paren "[")
854 (setq paren-regexp "\\["))
855 (t (setq paren-regexp paren)))
856 (save-excursion
857 (if (and (>= (- (point) (point-min))
858 (+ mod-length (length paren)))
859 (not (forward-char
860 (- 0 mod-length (length paren))))
861 (looking-at (concat "\\(" mod-regexp "\\)\\("
862 paren-regexp "\\)")))
863 (setq flag nil)))
864 (setq list (cdr list))))
865 (while (and list flag)
866 (let* ((mod (cdr (car list)))
867 (mod-length 0)
868 paren-regexp
869 mod-regexp)
870 (if (> (length mod) 0)
871 (setq mod-regexp (concat "\\\\" mod)
872 mod-length (1+ (length mod))))
873 (cond ((string= paren "\\}")
874 (setq paren-regexp (concat "\\" paren)))
875 ((string= paren "]")
876 (setq paren-regexp "\\]"))
877 (t (setq paren-regexp paren)))
878 (save-excursion
879 (if (and (>= (- (point) (point-min))
880 (+ mod-length (length paren)))
881 (not (forward-char
882 (- 0 mod-length (length paren))))
883 (looking-at (concat "\\(" mod-regexp "\\)\\("
884 paren-regexp "\\)")))
885 (setq flag nil)))
886 (setq list (cdr list)))))
887 (if (<= move (length (YaTeX-match-string 0)))
888 (YaTeX-match-string 0)))))))
890 (defun YaTeX-goto-open-paren (&optional jumpto-co)
891 "Jump to the exact position of open parenthesis.
892 If optional argument JUMPTO-CO is non-nil, goto corresponding parentheses."
893 (interactive)
894 (let ((paren)
895 (backslash-syntax (char-to-string (char-syntax ?\\))))
896 (if (setq paren (YaTeX-on-parenthesis-p))
897 (if (string-match "(\\|{\\|\\[" paren (1- (length paren)))
898 (progn
899 (re-search-forward "(\\|{\\|\\[" (+ (point) (length paren)) t)
900 (backward-char)
901 (if jumpto-co
902 (unwind-protect
903 (progn
904 (modify-syntax-entry ?\\ " ")
905 (forward-list)
906 (backward-char))
907 (modify-syntax-entry ?\\ backslash-syntax)))
908 (point))
909 (re-search-forward ")\\|}\\|\\]" (+ (point) (length paren)) t)
910 (unwind-protect
911 (progn
912 (modify-syntax-entry ?\\ " ")
913 (backward-list)
914 (point))
915 (modify-syntax-entry ?\\ backslash-syntax))))))
917 ;;;###autoload
918 (defun YaTeX-goto-corresponding-paren ()
919 "Go to corresponding mathematical parentheses."
920 (if (YaTeX-on-parenthesis-p)
921 (YaTeX-goto-open-paren t)
922 nil))
924 (defun YaTeX-change-parentheses ()
925 "Change the size of parentheses, braces, and brackets of AMS-LaTeX."
926 (interactive)
927 (if (not (and YaTeX-use-AMS-LaTeX (YaTeX-on-parenthesis-p)))
928 nil
929 (let* ((mod (YaTeX-match-string 1)) ;; modifier
930 (paren (if mod (YaTeX-match-string 2) (YaTeX-match-string 0))) ;; paren
931 (mod-length (if (or (string= mod "\\left") (string= mod "\\right"))
932 5 ;; 5 in case left or right
933 (length mod))) ;; length of modifier
934 (paren-length (length paren)) ;; length of paren
935 (length (+ mod-length paren-length)) ;; length of whole string
936 (big-p t) ;; flag whether new modifier is "[Bb]ig+" or not.
937 size ;; left, big, Big etc.
938 type ;; parentheses type
939 lr ;; "l" or "r".
940 char newsize newsize-length
941 (backslash-syntax (char-to-string (char-syntax ?\\)))
942 (case-fold-search))
943 ;; decide lr and size from mod and paren.
944 (cond ((string-match "\\(\\\\[Bb]ig+\\)[lr]" mod)
945 (setq size (substring mod 1 (match-end 1))
946 lr (substring mod (match-end 1) (match-end 0))))
947 ((string-match "\\\\left" mod)
948 (setq size "left-right" lr "l"))
949 ((string-match "\\\\right" mod)
950 (setq size "left-right" lr "r"))
951 ((string-match "(\\|\\[\\|\\\\{" paren)
952 (setq size "null" lr "l"))
953 ((string-match ")\\|\\]\\|\\\\}" paren)
954 (setq size "null" lr "r"))
955 (t
956 (setq size nil lr nil)))
957 (while (not newsize)
958 (message (format (concat "Change from %s: "
959 "l(big) L(Big) h(bigg) H(Bigg) "
960 "r(left-right) n(NONE) ( { [") size))
961 (setq char (read-char)
962 newsize (cond ((char-equal char ?l) "\\big")
963 ((char-equal char ?L) "\\Big")
964 ((char-equal char ?h) "\\bigg")
965 ((char-equal char ?H) "\\Bigg")
966 ((char-equal char ?r)
967 (setq big-p nil) "\\left")
968 ((memq char '(?\( ?\)))
969 (setq big-p nil type '("(" . ")")) "")
970 ((memq char '(?\{ ?\}))
971 (setq big-p nil type '("\\{" . "\\}")) "")
972 ((memq char '(?\[ ?\]))
973 (setq big-p nil type '("[" . "]")) "")
974 ((char-equal char ?n)
975 (setq big-p nil) "")
976 (t nil))
977 newsize-length (length newsize)))
978 (YaTeX-goto-open-paren)
979 (forward-char)
980 (cond
981 (type
982 (delete-region (point) (- (point) paren-length))
983 (save-excursion (insert (car type))))
984 (t
985 (delete-region (- (point) length) (- (point) paren-length))
986 (backward-char paren-length)))
987 (insert-string newsize)
988 (if big-p (insert ?l))
989 (unwind-protect
990 (progn
991 (modify-syntax-entry ?\\ " ")
992 (forward-list)
993 (if (string= size "left-right") (setq length (1+ length)))
994 (if (eq char ?r) (setq newsize "\\right"))
995 (cond
996 (type
997 (delete-region (point) (- (point) paren-length))
998 (insert (cdr type)))
999 (t
1000 (delete-region (- (point) length) (- (point) paren-length))
1001 (backward-char paren-length)
1002 (insert-string newsize)
1003 (if big-p (insert ?r))
1004 (forward-char paren-length)))
1005 (if (string= lr "l") (backward-list)))
1006 (modify-syntax-entry ?\\ backslash-syntax))
1007 t)))
1009 (defun YaTeX-insert-amsparens-region (beg end char)
1010 (interactive "r\ncWhich one ? l(big) L(Big) h(bigg) H(Bigg): ")
1011 (let* ((case-fold-search)
1012 (st (cond ((char-equal char ?l) "big")
1013 ((char-equal char ?L) "Big")
1014 ((char-equal char ?h) "bigg")
1015 ((char-equal char ?H) "Bigg"))))
1016 (if st
1017 (YaTeX-insert-braces-region
1018 beg end (concat "\\" st "l(") (concat "\\" st "r)"))
1019 (YaTeX-insert-braces-region beg end "(" ")"))))
1021 (defun YaTeX-insert-amsbraces-region (beg end char)
1022 (interactive "r\ncWhich one ? l(big) L(Big) h(bigg) H(Bigg): ")
1023 (let* ((case-fold-search)
1024 (st (cond ((char-equal char ?l) "big")
1025 ((char-equal char ?L) "Big")
1026 ((char-equal char ?h) "bigg")
1027 ((char-equal char ?H) "Bigg"))))
1028 (if st
1029 (YaTeX-insert-braces-region
1030 beg end (concat "\\" st "l\\{") (concat "\\" st "r\\}"))
1031 (YaTeX-insert-braces-region beg end "\\{" "\\}"))))
1033 (defun YaTeX-insert-amsbrackets-region (beg end char)
1034 (interactive "r\ncWhich one ? l(big) L(Big) h(bigg) H(Bigg): ")
1035 (let* ((case-fold-search)
1036 (st (cond ((char-equal char ?l) "big")
1037 ((char-equal char ?L) "Big")
1038 ((char-equal char ?h) "bigg")
1039 ((char-equal char ?H) "Bigg"))))
1040 (if st
1041 (YaTeX-insert-braces-region
1042 beg end (concat "\\" st "l[") (concat "\\" st "r]"))
1043 (YaTeX-insert-braces-region beg end "[" "]"))))
1046 ;;
1047 (provide 'yatexmth)
1049 ; Local variables:
1050 ; fill-prefix: ";;; "
1051 ; paragraph-start: "^$\\| \\|;;;$"
1052 ; paragraph-separate: "^$\\| \\|;;;$"
1053 ; End: