yatex

view yatexadd.el @ 243:74229d191b17

C-j skip can be used at <ol> and <input>.
author yuuji@gentei.org
date Thu, 09 Feb 2012 08:41:53 +0900
parents b75390dd4260
children 9105918e45a2
line source
1 ;;; -*- Emacs-Lisp -*-
2 ;;; YaTeX add-in functions.
3 ;;; yatexadd.el rev.20
4 ;;; (c)1991-2012 by HIROSE Yuuji.[yuuji@yatex.org]
5 ;;; Last modified Sun Jan 29 15:56:28 2012 on firestorm
6 ;;; $Id$
8 ;;;
9 ;;Sample functions for LaTeX environment.
10 ;;;
11 (defvar YaTeX:tabular-default-rule
12 "@{\\vrule width 1pt\\ }c|c|c@{\\ \\vrule width 1pt}"
13 "*Your favorite default rule format.")
15 (defvar YaTeX:tabular-thick-vrule "\\vrule width %s"
16 "*Vertical thick line format (without @{}). %s'll be replaced by its width.")
18 (defvar YaTeX:tabular-thick-hrule "\\noalign{\\hrule height %s}"
19 "*Horizontal thick line format. %s will be replaced by its width.")
21 (defun YaTeX:tabular ()
22 "YaTeX add-in function for tabular environment.
23 Notice that this function refers the let-variable `env' in
24 YaTeX-make-begin-end."
25 (let ((width "") bars (rule "") (and "") (j 1) loc ans (hline "\\hline"))
26 (if (string= YaTeX-env-name "tabular*")
27 (setq width (concat "{" (read-string "Width: ") "}")))
28 (setq loc (YaTeX:read-position "tb")
29 bars (string-to-int
30 (read-string "Number of columns(0 for default format): " "3")))
31 (if (<= bars 0)
32 (setq ;if 0, simple format
33 rule YaTeX:tabular-default-rule
34 and "& &")
35 (while (< j bars) ;repeat bars-1 times
36 (setq rule (concat rule "c|")
37 and (concat and "& ")
38 j (1+ j)))
39 (setq rule (concat rule "c"))
40 (message "(N)ormal-frame or (T)hick frame? [nt]")
41 (setq ans (read-char))
42 (cond
43 ((or (equal ans ?t) (equal ans ?T))
44 (setq ans (read-string "Rule width: " "1pt")
45 rule (concat
46 "@{" (format YaTeX:tabular-thick-vrule ans) "}"
47 rule
48 "@{\\ " (format YaTeX:tabular-thick-vrule ans) "}")
49 hline (format YaTeX:tabular-thick-hrule ans)))
50 (t (setq rule (concat "|" rule "|")
51 hline "\\hline"))))
53 (setq rule (read-string "rule format: " rule))
54 (setq YaTeX-single-command "hline")
56 (format "%s%s{%s}" width loc rule)))
58 (fset 'YaTeX:tabular* 'YaTeX:tabular)
59 (fset 'YaTeX:supertabular 'YaTeX:tabular)
60 (defun YaTeX:alignat ()
61 (concat "{" (read-string "Number of columns: ") "}"))
62 (defun YaTeX:array ()
63 (concat (YaTeX:read-position "tb")
64 "{" (read-string "Column format: ") "}"))
65 (defun YaTeX:subequations ()
66 (message (if YaTeX-japan "分かりやすいコメントに変えるとref補完が楽よ"
67 "Changing comment string reduces effort at `ref' completion"))
68 (concat " " YaTeX-comment-prefix
69 (YaTeX::ref-default-label "%H:%M")
70 (if YaTeX-japan "の式群" "equations")))
72 (defun YaTeX:read-oneof (oneof &optional quick allow-dup)
73 (let ((pos "") loc (guide ""))
74 (and (boundp 'name) name (setq guide (format "%s " name)))
75 (catch 'quick
76 (while (not (string-match
77 (setq loc (read-key-sequence
78 (format "%s position (`%s') [%s]: "
79 guide oneof pos));name is in YaTeX-addin
80 loc (if (fboundp 'events-to-keys)
81 (events-to-keys loc) loc))
82 "\r\^g\n"))
83 (cond
84 ((string-match loc oneof)
85 (if (or allow-dup (not (string-match loc pos)))
86 (setq pos (concat pos loc)))
87 (if quick (throw 'quick t)))
88 ((and (string-match loc "\C-h\C-?") (> (length pos) 0))
89 (setq pos (substring pos 0 (1- (length pos)))))
90 (t
91 (ding)
92 (message "Please input one of `%s'." oneof)
93 (sit-for 3)))))
94 (message "")
95 pos))
97 (defun YaTeX:read-position (oneof)
98 "Read a LaTeX (optional) position format such as `[htbp]'."
99 (let ((pos (YaTeX:read-oneof oneof)))
100 (if (string= pos "") "" (concat "[" pos "]"))))
102 ;;;
103 ;; Functions for figure environemnt
104 ;;;
105 (defvar YaTeX:figure-caption-first nil
106 "Non-nil indicates put caption before figure.")
107 (defun YaTeX:figure (&optional type firstp)
108 "YaTeX add-in function for figure(*) environment."
109 (let*((caption "") (label "") (opts "")
110 (top (if type firstp YaTeX:figure-caption-first))
111 (tl (or type "Figure"))
112 (heremsg (format "%% %s here" tl))
113 (pos (YaTeX:read-position "htbp")))
114 (setq label (YaTeX-read-string-or-skip (concat tl " Label: ")))
115 (if (string= "" label)
116 (setq YaTeX-section-name "label")
117 (setq opts (format "\n\\label{%s}" label)))
118 (setq caption (YaTeX-read-string-or-skip (concat tl " Caption: ")))
119 (if (string= "" caption)
120 (setq YaTeX-section-name "caption")
121 (setq caption (format "\\caption{%s}" caption)
122 opts (format "%s\n%s\n%s"
123 opts
124 (if top caption heremsg)
125 (if top heremsg caption)))
126 (format "\\caption{%s}" caption))
127 (concat pos opts)))
129 (fset 'YaTeX:figure* 'YaTeX:figure)
131 ;;;
132 ;; Functions for table environemnt
133 ;;;
134 (defvar YaTeX:table-caption-first t
135 "*Non-nil indicates put caption before tabular.")
136 (defun YaTeX:table ()
137 "YaTeX add-in function for table environment."
138 (cond
139 ((eq major-mode 'yatex-mode)
140 (setq YaTeX-env-name "tabular")
141 (YaTeX:figure "Table" YaTeX:table-caption-first))
142 ((eq major-mode 'texinfo-mode)
143 (concat " "
144 (completing-read
145 "Highlights with: "
146 '(("@samp")("@kbd")("@code")("@asis")("@file")("@var"))
147 nil nil "@")))))
148 (fset 'YaTeX:table* 'YaTeX:table)
150 (defun YaTeX:description ()
151 "Truly poor service:-)"
152 (setq YaTeX-single-command "item[]")
153 "")
155 (defun YaTeX:itemize ()
156 "It's also poor service."
157 (setq YaTeX-single-command "item")
158 "")
160 (fset 'YaTeX:enumerate 'YaTeX:itemize)
162 (defun YaTeX:picture ()
163 "Ask the size of coordinates of picture environment."
164 (concat (YaTeX:read-coordinates "Picture size")
165 (YaTeX:read-coordinates "Initial position")))
167 (defun YaTeX:equation ()
168 (YaTeX-jmode-off)
169 (if (fboundp 'YaTeX-toggle-math-mode)
170 (YaTeX-toggle-math-mode t))) ;force math-mode ON.
172 (mapcar '(lambda (f) (fset f 'YaTeX:equation))
173 '(YaTeX:eqnarray YaTeX:eqnarray* YaTeX:align YaTeX:align*
174 YaTeX:split YaTeX:multline YaTeX:multline* YaTeX:gather YaTeX:gather*
175 YaTeX:aligned* YaTeX:gathered YaTeX:gathered*
176 YaTeX:alignat YaTeX:alignat* YaTeX:xalignat YaTeX:xalignat*
177 YaTeX:xxalignat YaTeX:xxalignat*))
179 (defun YaTeX:alignat ()
180 (YaTeX:equation)
181 (concat "{" (read-string "Number of cols: ") "}"))
185 (defun YaTeX:list ()
186 "%\n{} %default label\n{} %formatting parameter")
188 (defun YaTeX:minipage ()
189 (concat (YaTeX:read-position "cbt")
190 "{" (read-string "Width: ") "}"))
192 (defun YaTeX:thebibliography ()
193 (setq YaTeX-section-name "bibitem")
194 (concat "{" (read-string "Longest label: ") "}"))
196 (defun YaTeX:multicols ()
197 (concat "{" (read-string "Number of columns: ") "}"))
199 ;;;
200 ;;Sample functions for section-type command.
201 ;;;
202 (defun YaTeX:multiput ()
203 (concat (YaTeX:read-coordinates "Pos")
204 (YaTeX:read-coordinates "Step")
205 "{" (read-string "How many times: ") "}"))
207 (defun YaTeX:put ()
208 (YaTeX:read-coordinates "Pos"))
210 (defun YaTeX:makebox ()
211 (cond
212 ((YaTeX-in-environment-p "picture")
213 (concat (YaTeX:read-coordinates "Dimension")
214 (YaTeX:read-position "lsrtb")))
215 (t
216 (let ((width (read-string "Width: ")))
217 (if (string< "" width)
218 (progn
219 (or (equal (aref width 0) ?\[)
220 (setq width (concat "[" width "]")))
221 (concat width (YaTeX:read-position
222 (if YaTeX-use-LaTeX2e "lrs" "lr")))))))))
224 ;; (defun YaTeX:framebox ()
225 ;; (if (YaTeX-quick-in-environment-p "picture")
226 ;; (YaTeX:makebox)))
227 (fset 'YaTeX:framebox 'YaTeX:makebox)
229 (defun YaTeX:parbox ()
230 (YaTeX:read-position "tbc"))
231 (defun YaTeX::parbox (argp)
232 (cond
233 ((= argp 1) (read-string "Width: "))
234 ((= argp 2) (read-string "Text: "))))
236 (defun YaTeX::dashbox ()
237 (concat "{" (read-string "Dash dimension: ") "}"
238 (YaTeX:read-coordinates "Dimension")))
240 (defun YaTeX::savebox (argp)
241 (cond
242 ((= argp 1) (read-string "Saved into name: " "\\"))
243 ((= argp 2) (read-string "Text: "))))
245 (defvar YaTeX-minibuffer-quick-map nil)
246 (if YaTeX-minibuffer-quick-map nil
247 (setq YaTeX-minibuffer-quick-map
248 (copy-keymap minibuffer-local-completion-map))
249 (let ((ch (1+ ? )))
250 (while (< ch 127)
251 (define-key YaTeX-minibuffer-quick-map (char-to-string ch)
252 'YaTeX-minibuffer-quick-complete)
253 (setq ch (1+ ch)))))
255 (defvar YaTeX:left-right-delimiters
256 '(("(" . ")") (")" . "(") ("[" . "]") ("]" . "[")
257 ("\\{" . "\\}") ("\\}" . "\\{") ("|") ("\\|")
258 ("\\lfloor" . "\\rfloor") ("\\lceil" . "\\rceil")
259 ("\\langle" . "\\rangle") ("/") (".")
260 ("\\rfloor" . "\\rfloor") ("\\rceil" . "\\lceil")
261 ("\\rangle" . "\\langle") ("\\backslash")
262 ("\\uparrow") ("\\downarrow") ("\\updownarrow") ("\\Updownarrow"))
263 "TeX math delimiter, which can be completed after \\right or \\left.")
265 (defvar YaTeX:left-right-default nil "Default string of YaTeX:right.")
267 (defun YaTeX:left ()
268 (let ((minibuffer-completion-table YaTeX:left-right-delimiters)
269 delimiter (leftp (string= YaTeX-single-command "left")))
270 (setq delimiter
271 (read-from-minibuffer
272 (format "Delimiter%s: "
273 (if YaTeX:left-right-default
274 (format "(default=`%s')" YaTeX:left-right-default)
275 "(SPC for menu)"))
276 nil YaTeX-minibuffer-quick-map))
277 (if (string= "" delimiter) (setq delimiter YaTeX:left-right-default))
278 (setq YaTeX-single-command (if leftp "right" "left")
279 YaTeX:left-right-default
280 (or (cdr (assoc delimiter YaTeX:left-right-delimiters)) delimiter))
281 delimiter))
283 (fset 'YaTeX:right 'YaTeX:left)
285 (defun YaTeX:langle ()
286 (setq YaTeX-single-command "rangle")
287 nil)
289 (defun YaTeX:read-coordinates (&optional mes varX varY)
290 (concat
291 "("
292 (read-string (format "%s %s: " (or mes "Dimension") (or varX "X")))
293 ","
294 (read-string (format "%s %s: " (or mes "Dimension") (or varY "Y")))
295 ")"))
297 (defun YaTeX:itembox ()
298 (concat "{" (read-string "Item heading string: ") "}"))
300 ;;;
301 ;;Sample functions for maketitle-type command.
302 ;;;
303 (defun YaTeX:sum ()
304 "Read range of summation."
305 (YaTeX:check-completion-type 'maketitle)
306 (concat (YaTeX:read-boundary "_") (YaTeX:read-boundary "^")))
308 (fset 'YaTeX:int 'YaTeX:sum)
310 (defun YaTeX:lim ()
311 "Insert limit notation of \\lim."
312 (YaTeX:check-completion-type 'maketitle)
313 (let ((var (read-string "Variable: ")) limit)
314 (if (string= "" var) ""
315 (setq limit (read-string "Limit ($ means infinity): "))
316 (if (string= "$" limit) (setq limit "\\infty"))
317 (concat "_{" var " \\rightarrow " limit "}"))))
319 (defun YaTeX:gcd ()
320 "Add-in function for \\gcd(m,n)."
321 (YaTeX:check-completion-type 'maketitle)
322 (YaTeX:read-coordinates "\\gcd" "(?,)" "(,?)"))
324 (defun YaTeX:read-boundary (ULchar)
325 "Read boundary usage by _ or ^. _ or ^ is indicated by argument ULchar."
326 (let ((bndry (read-string (concat ULchar "{???} ($ for infinity): "))))
327 (if (string= bndry "") ""
328 (if (string= bndry "$") (setq bndry "\\infty"))
329 (concat ULchar "{" bndry "}"))))
331 (defun YaTeX:verb ()
332 "Enclose \\verb's contents with the same characters."
333 (let ((quote-char (read-string "Quoting char: " "|"))
334 (contents (read-string "Quoted contents: ")))
335 (concat quote-char contents quote-char)))
337 (fset 'YaTeX:verb* 'YaTeX:verb)
339 (defun YaTeX:footnotemark ()
340 (setq YaTeX-section-name "footnotetext")
341 nil)
343 (defun YaTeX:cite ()
344 (let ((comment (read-string "Comment for citation: ")))
345 (if (string= comment "") ""
346 (concat "[" comment "]"))))
348 (defun YaTeX:bibitem ()
349 (let ((label (read-string "Citation label for bibitem: ")))
350 (if (string= label "") ""
351 (concat "[" label "]"))))
353 (defun YaTeX:item ()
354 (cond
355 ((eq major-mode 'yatex-mode)
356 (YaTeX-indent-line)
357 (setq YaTeX-section-name "label"))
358 ((eq major-mode 'texinfo-mode)
359 (setq YaTeX-section-name "dots"))) ;??
360 " ")
361 (fset 'YaTeX:item\[\] 'YaTeX:item)
362 (fset 'YaTeX:subitem 'YaTeX:item)
363 (fset 'YaTeX:subsubitem 'YaTeX:item)
365 (defun YaTeX:linebreak ()
366 (let (obl)
367 (message "Break strength 0,1,2,3,4 (default: 4): ")
368 (setq obl (char-to-string (read-char)))
369 (if (string-match "[0-4]" obl)
370 (concat "[" obl "]")
371 "")))
372 (fset 'YaTeX:pagebreak 'YaTeX:linebreak)
374 ;;;
375 ;;Subroutine
376 ;;;
378 (defun YaTeX:check-completion-type (type)
379 "Check valid completion type."
380 (if (not (eq type YaTeX-current-completion-type))
381 (error "This should be completed with %s-type completion." type)))
384 ;;;
385 ;;; [[Add-in functions for reading section arguments]]
386 ;;;
387 ;; All of add-in functions for reading sections arguments should
388 ;; take an argument ARGP that specify the argument position.
389 ;; If argument position is out of range, nil should be returned,
390 ;; else nil should NOT be returned.
392 ;;
393 ; Label selection
394 ;;
395 (defvar YaTeX-label-menu-other
396 (if YaTeX-japan "':他のバッファのラベル\n" "':LABEL IN OTHER BUFFER.\n"))
397 (defvar YaTeX-label-menu-repeat
398 (if YaTeX-japan ".:直前の\\refと同じ\n" "/:REPEAT LAST \ref{}\n"))
399 (defvar YaTeX-label-menu-any
400 (if YaTeX-japan "*:任意の文字列\n" "*:ANY STRING.\n"))
401 (defvar YaTeX-label-buffer "*Label completions*")
402 (defvar YaTeX-label-guide-msg "Select label and hit RETURN.")
403 (defvar YaTeX-label-select-map nil
404 "Key map used in label selection buffer.")
405 (defun YaTeX::label-setup-key-map ()
406 (if YaTeX-label-select-map nil
407 (message "Setting up label selection mode map...")
408 ;(setq YaTeX-label-select-map (copy-keymap global-map))
409 (setq YaTeX-label-select-map (make-keymap))
410 (suppress-keymap YaTeX-label-select-map)
411 (substitute-all-key-definition
412 'previous-line 'YaTeX::label-previous YaTeX-label-select-map)
413 (substitute-all-key-definition
414 'next-line 'YaTeX::label-next YaTeX-label-select-map)
415 (define-key YaTeX-label-select-map "\C-n" 'YaTeX::label-next)
416 (define-key YaTeX-label-select-map "\C-p" 'YaTeX::label-previous)
417 (define-key YaTeX-label-select-map "<" 'beginning-of-buffer)
418 (define-key YaTeX-label-select-map ">" 'end-of-buffer)
419 (define-key YaTeX-label-select-map "\C-m" 'exit-recursive-edit)
420 (define-key YaTeX-label-select-map "\C-j" 'exit-recursive-edit)
421 (define-key YaTeX-label-select-map " " 'exit-recursive-edit)
422 (define-key YaTeX-label-select-map "\C-g" 'abort-recursive-edit)
423 (define-key YaTeX-label-select-map "/" 'isearch-forward)
424 (define-key YaTeX-label-select-map "?" 'isearch-backward)
425 (define-key YaTeX-label-select-map "'" 'YaTeX::label-search-tag)
426 (define-key YaTeX-label-select-map "." 'YaTeX::label-search-tag)
427 (define-key YaTeX-label-select-map "*" 'YaTeX::label-search-tag)
428 (message "Setting up label selection mode map...Done")
429 (let ((key ?A))
430 (while (<= key ?Z)
431 (define-key YaTeX-label-select-map (char-to-string key)
432 'YaTeX::label-search-tag)
433 (define-key YaTeX-label-select-map (char-to-string (+ key (- ?a ?A)))
434 'YaTeX::label-search-tag)
435 (setq key (1+ key))))))
437 (defun YaTeX::label-next ()
438 (interactive) (forward-line 1) (message YaTeX-label-guide-msg))
439 (defun YaTeX::label-previous ()
440 (interactive) (forward-line -1) (message YaTeX-label-guide-msg))
441 (defun YaTeX::label-search-tag ()
442 (interactive)
443 (let ((case-fold-search t)
444 (tag (regexp-quote (char-to-string last-command-char))))
445 (cond
446 ((save-excursion
447 (forward-char 1)
448 (re-search-forward (concat "^" tag) nil t))
449 (goto-char (match-beginning 0)))
450 ((save-excursion
451 (goto-char (point-min))
452 (re-search-forward (concat "^" tag) nil t))
453 (goto-char (match-beginning 0))))
454 (message YaTeX-label-guide-msg)))
456 ; (defun YaTeX::ref (argp &optional labelcmd refcmd)
457 ; (cond
458 ; ((= argp 1)
459 ; (let ((lnum 0) e0 label label-list (buf (current-buffer))
460 ; (labelcmd (or labelcmd "label")) (refcmd (or refcmd "ref"))
461 ; (p (point)) initl line cf)
462 ; (message "Collecting labels...")
463 ; (save-window-excursion
464 ; (YaTeX-showup-buffer
465 ; YaTeX-label-buffer (function (lambda (x) (window-width x))))
466 ; (if (fboundp 'select-frame) (setq cf (selected-frame)))
467 ; (if (eq (window-buffer (minibuffer-window)) buf)
468 ; (progn
469 ; (other-window 1)
470 ; (setq buf (current-buffer))
471 ; (set-buffer buf)
472 ; ;(message "cb=%s" buf)(sit-for 3)
473 ; ))
474 ; (save-excursion
475 ; (set-buffer (get-buffer-create YaTeX-label-buffer))
476 ; (setq buffer-read-only nil)
477 ; (erase-buffer))
478 ; (save-excursion
479 ; (goto-char (point-min))
480 ; (let ((standard-output (get-buffer YaTeX-label-buffer)))
481 ; (princ (format "=== LABELS in [%s] ===\n" (buffer-name buf)))
482 ; (while (YaTeX-re-search-active-forward
483 ; (concat "\\\\" labelcmd "\\b")
484 ; (regexp-quote YaTeX-comment-prefix) nil t)
485 ; (goto-char (match-beginning 0))
486 ; (skip-chars-forward "^{")
487 ; (setq label
488 ; (buffer-substring
489 ; (1+ (point))
490 ; (prog2 (forward-list 1) (setq e0 (1- (point)))))
491 ; label-list (cons label label-list))
492 ; (or initl
493 ; (if (< p (point)) (setq initl lnum)))
494 ; (beginning-of-line)
495 ; (skip-chars-forward " \t\n" nil)
496 ; (princ (format "%c:{%s}\t<<%s>>\n" (+ (% lnum 26) ?A) label
497 ; (buffer-substring (point) (point-end-of-line))))
498 ; (setq lnum (1+ lnum))
499 ; (message "Collecting \\%s{}... %d" labelcmd lnum)
500 ; (goto-char e0))
501 ; (princ YaTeX-label-menu-other)
502 ; (princ YaTeX-label-menu-repeat)
503 ; (princ YaTeX-label-menu-any)
504 ; );standard-output
505 ; (goto-char p)
506 ; (or initl (setq initl lnum))
507 ; (message "Collecting %s...Done" labelcmd)
508 ; (if (fboundp 'select-frame) (select-frame cf))
509 ; (YaTeX-showup-buffer YaTeX-label-buffer nil t)
510 ; (YaTeX::label-setup-key-map)
511 ; (setq truncate-lines t)
512 ; (setq buffer-read-only t)
513 ; (use-local-map YaTeX-label-select-map)
514 ; (message YaTeX-label-guide-msg)
515 ; (goto-line (1+ initl)) ;goto recently defined label line
516 ; (switch-to-buffer (current-buffer))
517 ; (unwind-protect
518 ; (progn
519 ; (recursive-edit)
520 ; (set-buffer (get-buffer YaTeX-label-buffer)) ;assertion
521 ; (beginning-of-line)
522 ; (setq line (1- (count-lines (point-min)(point))))
523 ; (cond
524 ; ((= line -1) (setq label ""))
525 ; ((= line lnum) (setq label (YaTeX-label-other)))
526 ; ((= line (1+ lnum))
527 ; (save-excursion
528 ; (switch-to-buffer buf)
529 ; (goto-char p)
530 ; (if (re-search-backward
531 ; (concat "\\\\" refcmd "{\\([^}]+\\)}") nil t)
532 ; (setq label (YaTeX-match-string 1))
533 ; (setq label ""))))
534 ; ((>= line (+ lnum 2))
535 ; (setq label (read-string (format "\\%s{???}: " refcmd))))
536 ; (t (setq label (nth (- lnum line 1) label-list)))))
537 ; (bury-buffer YaTeX-label-buffer)))
538 ; label)))))
540 (defvar YaTeX-ref-default-label-string "%H%M%S_%d%b%y"
541 "*Default \\ref time string format.
542 This format is like strftime(3) but allowed conversion char are as follows;
543 %y -> Last 2 digit of year, %b -> Month name, %m -> Monthe number(1-12),
544 %d -> Day, %H -> Hour, %M -> Minute, %S -> Second,
545 %qx -> alphabetical-decimal conversion of yymmdd.
546 %qX -> alphabetical-decimal conversion of HHMMSS.
547 Beware defualt label-string should be always unique. So this format string
548 should have both time part (%H+%M+%S or %qX) and date
549 part (%y+(%b|%m)+%d or %qx).")
551 (defun YaTeX::ref-alphabex (n)
552 (let ((alphabex ""))
553 (while (> n 0)
554 (setq alphabex (concat (char-to-string (+ ?a (% n 26))) alphabex)
555 n (/ n 26)))
556 alphabex))
558 (defun YaTeX::ref-default-label (&optional format)
559 "Default auto-genarated label string."
560 ;; We do not use (format-time-string) for emacs-19
561 (let*((ts (substring (current-time-string) 4))
562 (y (substring ts -2))
563 (b (substring ts 0 3))
564 (d (format "%d" (string-to-int (substring ts 4 6))))
565 (H (substring ts 7 9))
566 (M (substring ts 10 12))
567 (S (substring ts 13 15))
568 (HMS (+ (* 10000 (string-to-int H))
569 (* 100 (string-to-int M))
570 (string-to-int S)))
571 (talphabex (YaTeX::ref-alphabex HMS))
572 (mnames "JanFebMarAprMayJunJulAugSepOctNovDec")
573 (m (format "%02d" (/ (string-match b mnames) 3)))
574 (ymd (+ (* 10000 (string-to-int y))
575 (* 100 (string-to-int m))
576 (string-to-int d)))
577 (dalphabex (YaTeX::ref-alphabex ymd)))
578 (YaTeX-replace-formats
579 (or format YaTeX-ref-default-label-string)
580 (list (cons "y" y)
581 (cons "b" b)
582 (cons "m" m)
583 (cons "d" d)
584 (cons "H" H)
585 (cons "M" M)
586 (cons "S" S)
587 (cons "qX" talphabex)
588 (cons "qx" dalphabex)))))
590 (defvar YaTeX-ref-generate-label-function 'YaTeX::ref-generate-label
591 "*Function to generate default label for unnamed \\label{}s.
592 The function pointed to this value should take two arguments.
593 First argument is LaTeX macro's name, second is macro's argument.")
595 (defun YaTeX::ref-generate-label (command arg)
596 "Generate a label string which is unique in current buffer."
597 (let ((default (condition-case nil
598 (YaTeX::ref-default-label)
599 (error (substring (current-time-string) 4)))))
600 (read-string "Give a label for this line: "
601 (if YaTeX-emacs-19 (cons default 1) default))))
603 (defun YaTeX::ref-getset-label (buffer point &optional noset)
604 "Get label string in the BUFFER near the POINT.
605 Make \\label{xx} if no label.
606 If optional third argument NOSET is non-nil, do not generate new label."
607 ;;Here, we rewrite the LaTeX source. Therefore we should be careful
608 ;;to decide the location suitable for \label. Do straightforward!
609 (let (boundary inspoint cc newlabel (labelholder "label") mathp exp1 env
610 (r-escape (regexp-quote YaTeX-comment-prefix))
611 command arg alreadysought foundpoint)
612 (set-buffer buffer)
613 (save-excursion
614 (goto-char point)
615 (setq cc (current-column))
616 (if (= (char-after (point)) ?\\) (forward-char 1))
617 (cond
618 ((looking-at YaTeX-sectioning-regexp)
619 (setq command (YaTeX-match-string 0))
620 (skip-chars-forward "^{")
621 (setq arg (buffer-substring
622 (1+ (point))
623 (progn (forward-list 1) (1- (point)))))
624 (skip-chars-forward " \t\n")
625 ;(setq boundary "[^\\]")
626 (setq inspoint (point))
627 (setq boundary
628 (save-excursion
629 (if (YaTeX-re-search-active-forward
630 (concat YaTeX-ec-regexp
631 "\\(" YaTeX-sectioning-regexp "\\|"
632 "begin\\|item\\)")
633 r-escape nil 1)
634 (match-beginning 0)
635 (1- (point))))))
636 ((looking-at "item\\s ")
637 (setq command "item"
638 cc (+ cc 6))
639 ;(setq boundary (concat YaTeX-ec-regexp "\\(item\\|begin\\|end\\)\\b"))
640 (setq boundary
641 (save-excursion
642 (if (YaTeX-re-search-active-forward
643 (concat YaTeX-ec-regexp "\\(item\\|begin\\|end\\)\\b")
644 r-escape nil 1)
645 (match-beginning 0)
646 (1- (point))))
647 inspoint boundary))
648 ((looking-at "bibitem")
649 (setq labelholder "bibitem" ; label holder is bibitem itself
650 command "bibitem")
651 (setq boundary
652 (save-excursion
653 (if (YaTeX-re-search-active-forward
654 (concat YaTeX-ec-regexp "\\(bibitem\\|end\\)\\b")
655 r-escape nil 1)
656 (match-beginning 0)
657 (1- (point))))
658 inspoint boundary))
659 ((string-match YaTeX::ref-nestable-counter-regexp
660 (setq env (or (YaTeX-inner-environment t) "document")))
661 (let ((curtop (get 'YaTeX-inner-environment 'point))
662 (end (point-max)) label)
663 (skip-chars-forward " \t\n")
664 (setq inspoint (point) ;initial candidate
665 cc (current-column)
666 command env
667 alreadysought t)
668 (if (condition-case nil
669 (progn
670 (goto-char curtop)
671 (YaTeX-goto-corresponding-environment))
672 (error nil))
673 (setq end (point)))
674 (goto-char inspoint)
675 (while (YaTeX-re-search-active-forward
676 (concat YaTeX-ec-regexp "label{\\([^}]+\\)}" )
677 r-escape end t)
678 (setq label (YaTeX-match-string 1))
679 (if (and (equal env (YaTeX-inner-environment t))
680 (= curtop (get 'YaTeX-inner-environment 'point)))
681 ;;I found the label
682 (setq alreadysought label
683 foundpoint (match-end 0))))
684 ))
685 ((string-match YaTeX::ref-mathenv-regexp env) ;env is set in above case
686 (setq command env
687 mathp t
688 exp1 (string-match YaTeX::ref-mathenv-exp1-regexp env))
689 ;;(setq boundary (concat YaTeX-ec-regexp "\\(\\\\\\|end{" env "}\\)"))
690 (setq boundary
691 (save-excursion
692 (if (YaTeX-re-search-active-forward
693 (concat
694 YaTeX-ec-regexp "\\("
695 (if exp1 "" "\\\\\\|")
696 "end{" env "}\\)")
697 r-escape nil 1)
698 (match-beginning 0)
699 (1- (point))))
700 inspoint boundary))
701 ((looking-at "footnote\\s *{")
702 (setq command "footnote")
703 (skip-chars-forward "^{") ;move onto `{'
704 (setq boundary
705 (save-excursion
706 (condition-case err
707 (forward-list 1)
708 (error (error "\\\\footnote at point %s's brace not closed"
709 (point))))
710 (1- (point)))
711 inspoint boundary))
712 ((looking-at "caption\\|\\(begin\\)")
713 (setq command (YaTeX-match-string 0))
714 (skip-chars-forward "^{")
715 ;;;;;;(if (match-beginning 1) (forward-list 1))
716 ;; caption can be treated as mathenv, is it right??
717 (setq arg (buffer-substring
718 (1+ (point))
719 (progn (forward-list 1) (1- (point)))))
720 ;;(setq boundary (concat YaTeX-ec-regexp "\\(begin\\|end\\)\\b"))
721 (setq inspoint (point))
722 (setq boundary
723 (save-excursion
724 (if (YaTeX-re-search-active-forward
725 (concat YaTeX-ec-regexp "\\(begin\\|end\\)\\b")
726 r-escape nil 1)
727 (match-beginning 0)
728 (1- (point))))))
729 (t ))
730 (if (save-excursion (skip-chars-forward " \t") (looking-at "%"))
731 (forward-line 1))
732 (cond
733 ((stringp alreadysought)
734 (put 'YaTeX::ref-getset-label 'foundpoint foundpoint) ;ugly...
735 alreadysought)
736 ((and (null alreadysought)
737 (> boundary (point))
738 (save-excursion
739 (YaTeX-re-search-active-forward
740 ;;(concat "\\(" labelholder "\\)\\|\\(" boundary "\\)")
741 labelholder
742 (regexp-quote YaTeX-comment-prefix)
743 boundary 1))
744 (match-beginning 0))
745 ;; if \label{hoge} found, return it
746 (put 'YaTeX::ref-getset-label 'foundpoint (1- (match-beginning 0)))
747 (buffer-substring
748 (progn
749 (goto-char (match-end 0))
750 (skip-chars-forward "^{") (1+ (point)))
751 (progn
752 (forward-sexp 1) (1- (point)))))
753 ;;else make a label
754 ;(goto-char (match-beginning 0))
755 (noset nil) ;do not set label if noset
756 (t
757 (goto-char inspoint)
758 (skip-chars-backward " \t\n")
759 (save-excursion
760 (setq newlabel
761 (funcall YaTeX-ref-generate-label-function command arg)))
762 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
763 (if mathp nil
764 (insert "\n")
765 (YaTeX-reindent cc))
766 (insert (format "\\label{%s}" newlabel))
767 newlabel)))))
769 (defvar YaTeX::ref-labeling-regexp-alist-default
770 '(("\\\\begin{\\(java\\|program\\)}{\\([^}]+\\)}" . 2)
771 ("\\\\label{\\([^}]+\\)}" . 1))
772 "Alist of labeling regexp vs. its group number points to label string.
773 This alist is used in \\ref's argument's completion.")
774 (defvar YaTeX::ref-labeling-regexp-alist-private nil
775 "*Private extension to YaTeX::ref-labeling-regexp-alist.
776 See the documetation of YaTeX::ref-labeling-regexp-alist.")
777 (defvar YaTeX::ref-labeling-regexp-alist
778 (append YaTeX::ref-labeling-regexp-alist-default
779 YaTeX::ref-labeling-regexp-alist-private))
780 (defvar YaTeX::ref-labeling-regexp
781 (mapconcat 'car YaTeX::ref-labeling-regexp-alist "\\|"))
782 (defvar YaTeX::ref-mathenv-regexp
783 ;; See also YaTeX-ams-math-begin-alist in yatex.el
784 ;; Define only envs which has counter.(without *)
785 "equation\\|eqnarray\\|align\\(at\\)?\\|flalign\\|gather\\|xx?alignat\\|multline")
786 (defvar YaTeX::ref-mathenv-exp1-regexp
787 "\\(equation\\|multline\\)\\b"
788 "*Regexp of math-envname which has only one math-expression.")
789 (defvar YaTeX::ref-enumerateenv-regexp
790 "enumerate")
791 (defvar YaTeX::ref-nestable-counter-regexp
792 "subequations")
794 (defvar YaTeX::ref-labeling-section-level 2
795 "*ref補完で収集するセクショニングコマンドの下限レベル
796 YaTeX-sectioning-levelの数値で指定.")
798 (defun YaTeX::ref (argp &optional labelcmd refcmd predf)
799 (setplist 'YaTeX::ref-labeling-regexp nil) ;erase memory cache
800 (require 'yatexsec)
801 (cond
802 ((= argp 1)
803 (let*((lnum 0) m0 e0 x cmd label match-point point-list boundary
804 (buf (current-buffer))
805 (llv YaTeX::ref-labeling-section-level)
806 (mathenvs YaTeX::ref-mathenv-regexp) envname endrx
807 (enums YaTeX::ref-enumerateenv-regexp)
808 (counter
809 (or labelcmd
810 (concat
811 YaTeX-ec-regexp "\\(\\("
812 (mapconcat
813 'concat
814 (delq nil
815 (mapcar
816 (function
817 (lambda (s)
818 (if (>= llv (cdr s))
819 (car s))))
820 YaTeX-sectioning-level))
821 "\\|")
822 "\\|caption\\(\\[[^]]+\\]\\)?\\|footnote\\){"
823 "\\|\\(begin{\\(" mathenvs "\\|" enums "\\)}\\)"
824 (if YaTeX-use-AMS-LaTeX
825 (concat
826 "\\|\\(begin{"
827 YaTeX::ref-nestable-counter-regexp "}\\)"))
828 "\\)")))
829 (regexp (concat "\\(" counter
830 "\\)\\|\\(" YaTeX::ref-labeling-regexp "\\)"))
831 (itemsep (concat YaTeX-ec-regexp
832 "\\(\\(bib\\)?item\\|begin\\|end\\)"))
833 (refcmd (or refcmd "\\(page\\)?ref"))
834 (p (point)) initl line cf
835 (percent (regexp-quote YaTeX-comment-prefix))
836 (output
837 (function
838 (lambda (label p)
839 (while (setq x (string-match "[\n\t]" label))
840 (aset label x ? ))
841 (while (setq x (string-match " +" label))
842 (setq label (concat
843 (substring label 0 (1+ (match-beginning 0)))
844 (substring label (match-end 0)))))
845 (princ (format "%c: <<%s>>\n" (+ (% lnum 26) ?A) label))
846 (setq point-list (cons p point-list))
847 (message "Collecting labels... %d" lnum)
848 (setq lnum (1+ lnum)))))
849 (me (if (boundp 'me) me 'YaTeX::ref))
850 )
851 (message "Collecting labels...")
852 (save-window-excursion
853 (YaTeX-showup-buffer
854 YaTeX-label-buffer (function (lambda (x) (window-width x))))
855 (if (fboundp 'select-frame) (setq cf (selected-frame)))
856 (if (eq (window-buffer (minibuffer-window)) buf)
857 (progn
858 (other-window 1)
859 (setq buf (current-buffer))
860 (set-buffer buf)))
861 (save-excursion
862 (set-buffer (get-buffer-create YaTeX-label-buffer))
863 (condition-case ()
864 (if (and YaTeX-use-font-lock (fboundp 'font-lock-mode))
865 (font-lock-mode 1))
866 (error nil))
867 (setq buffer-read-only nil)
868 (erase-buffer))
869 (save-excursion
870 (set-buffer buf)
871 (goto-char (point-min))
872 (let ((standard-output (get-buffer YaTeX-label-buffer)) existlabel)
873 (princ (format "=== LABELS in [%s] ===\n" (buffer-name buf)))
874 (while (YaTeX-re-search-active-forward
875 regexp ;;counter
876 percent nil t)
877 ;(goto-char (match-beginning 0))
878 (setq e0 (match-end 0))
879 (cond
880 ;;
881 ;;2005/10/21 Skip it if predicate function returns nil
882 ((and predf
883 (let ((md (match-data)))
884 (prog1
885 (condition-case nil
886 (not (funcall predf))
887 (error nil))
888 (store-match-data md)))))
889 ((YaTeX-literal-p) nil)
890 ((YaTeX-match-string 1)
891 ;;if standard counter commands found
892 (setq cmd (YaTeX-match-string 2)
893 m0 (match-beginning 0))
894 (setq match-point (match-beginning 0))
895 (or initl
896 (if (< p (point)) (setq initl lnum)))
897 (cond
898 ;; In any case, variables e0 should be set
899 ((and YaTeX-use-AMS-LaTeX
900 (string-match YaTeX::ref-nestable-counter-regexp cmd))
901 (let (label)
902 (skip-chars-forward "}")
903 (setq label (buffer-substring
904 (point) (min (+ 80 (point)) (point-max))))
905 ;; to skip (maybe)auto-generated comment
906 (skip-chars-forward " \t")
907 (if (looking-at YaTeX-comment-prefix)
908 (forward-line 1))
909 (setq e0 (point))
910 (skip-chars-forward " \t\n")
911 (if (looking-at "\\\\label{\\([^}]+\\)}")
912 (setq label (format "(labe:%s)" (YaTeX-match-string 1))
913 e0 (match-end 1)))
914 (funcall output (format "--subequation--%s" label) e0)))
915 ((string-match mathenvs cmd) ;;if matches mathematical env
916 ;(skip-chars-forward "} \t\n")
917 ;(forward-line 1) ;2004/1/25
918 (skip-chars-forward "}")
919 (setq x (point)
920 envname (substring
921 cmd (match-beginning 0) (match-end 0)))
922 (save-restriction
923 (narrow-to-region
924 m0
925 (save-excursion
926 (YaTeX-re-search-active-forward
927 (setq endrx (format "%send{%s}" YaTeX-ec-regexp
928 (regexp-quote envname)))
929 percent nil t)))
930 (catch 'scan
931 (while (YaTeX-re-search-active-forward
932 (concat
933 "\\\\end{\\(" (regexp-quote envname) "\\)";;(1)
934 (if YaTeX-use-AMS-LaTeX
935 "\\|\\\\\\(notag\\)") ;;2
936 (if (string-match
937 YaTeX::ref-mathenv-exp1-regexp cmd)
938 "" "\\|\\\\\\\\$")
939 )
940 percent nil t)
941 (let*((quit (match-beginning 1))
942 (notag (match-beginning 2))
943 (label ".......................") l2
944 (e (point)) (m0 (match-beginning 0))
945 (ln (YaTeX-string-width label)))
946 (cond
947 (notag
948 (YaTeX-re-search-active-forward
949 "\\\\\\\\" percent nil 1)
950 (setq x (point)))
951 (t
952 (if (YaTeX-re-search-active-backward
953 YaTeX::ref-labeling-regexp
954 percent x t)
955 ;; if \label{x} in math-expression, display it
956 ;; because formula source is hard to recognize
957 (progn
958 (goto-char (match-end 0))
959 (setq l2 (format "\"label:%s\""
960 (buffer-substring
961 (1- (point))
962 (progn (forward-sexp -1)
963 (1+ (point))))))
964 (setq label
965 (if (< (YaTeX-string-width l2) ln)
966 (concat
967 l2
968 (substring
969 label
970 0 (- ln (YaTeX-string-width l2))))
971 l2))
972 (goto-char e)))
973 (funcall output
974 (concat
975 label " "
976 (buffer-substring x m0))
977 x)
978 (cond
979 ((YaTeX-quick-in-environment-p
980 YaTeX-math-gathering-list)
981 ;; if here is inner split/cases/gathered env.,
982 ;; counter for here is only one.
983 ;; Go out this environment and,
984 (YaTeX-end-of-environment)
985 ;; search next expression unit boundary.
986 (YaTeX-re-search-active-forward
987 (concat endrx "\\|\\\\begin{")
988 percent nil 1)
989 (end-of-line)))
990 (if quit (throw 'scan t)))))
991 (setq x (point)))))
992 (setq e0 (point)))
993 ((string-match enums cmd)
994 ;(skip-chars-forward "} \t\n")
995 (save-restriction
996 (narrow-to-region
997 (point)
998 (save-excursion
999 (YaTeX-goto-corresponding-environment) (point)))
1000 (forward-line 1)
1001 (while (YaTeX-re-search-active-forward
1002 (concat YaTeX-ec-regexp "item\\s ")
1003 percent nil t)
1004 (setq x (match-beginning 0))
1005 (funcall
1006 output
1007 (concat
1008 existlabel
1009 (buffer-substring
1010 (match-beginning 0)
1011 (if (re-search-forward itemsep nil t)
1012 (progn (goto-char (match-beginning 0))
1013 (skip-chars-backward " \t")
1014 (1- (point)))
1015 (point-end-of-line))))
1016 x))
1017 (setq e0 (point-max))))
1018 ((string-match "bibitem" cmd) ;maybe generated by myself
1019 (setq label "")
1020 (skip-chars-forward " \t")
1021 (if (looking-at "{") ;sure to be true!!
1022 (forward-list 1))
1023 (let ((list '(30 10 65))
1024 (delim ";") q lim len l str)
1025 (save-excursion
1026 (setq lim (if (re-search-forward itemsep nil 1)
1027 (match-beginning 0) (point))))
1028 (while list
1029 (skip-chars-forward " \t\n\\")
1030 (setq q (looking-at "[\"'{]")
1031 len (car list)
1032 str
1033 (buffer-substring
1034 (point)
1035 (progn
1036 (if q (forward-sexp 1)
1037 (search-forward delim lim 1)
1038 (forward-char -1))
1039 (point))))
1040 (if (> (setq l (YaTeX-string-width str)) len)
1041 (setq str (concat
1042 (YaTeX-truncate-string-width
1043 str (- len (if q 5 4)))
1044 "... "
1045 (if q (substring str -1)))))
1046 (if (< (setq l (YaTeX-string-width str)) len)
1047 (setq str (concat str (make-string (- len l) ? ))))
1048 (if (looking-at delim) (goto-char (match-end 0)))
1049 (setq label (concat label " " str)
1050 list (cdr list)))
1051 (funcall output label match-point)))
1052 ;;else, simple section-type counter
1053 ((= (char-after (1- (point))) ?{)
1054 (setq label (buffer-substring
1055 (match-beginning 0)
1056 (progn (forward-char -1)
1057 (forward-list 1)
1058 (point))))
1059 (funcall output label match-point)
1060 ;; Skip preceding label if exists
1061 (if (YaTeX::ref-getset-label (current-buffer) match-point t)
1062 (goto-char (get 'YaTeX::ref-getset-label 'foundpoint)))
1063 (if (save-excursion
1064 (skip-chars-forward "\t \n")
1065 (looking-at YaTeX::ref-labeling-regexp))
1066 (setq e0 (match-end 0))))
1067 (t
1068 (skip-chars-forward " \t")
1069 (setq label (buffer-substring
1070 (match-beginning 0)
1071 (if (re-search-forward
1072 itemsep
1073 nil t)
1074 (progn
1075 (goto-char (match-beginning 0))
1076 (skip-chars-backward " \t")
1077 (1- (point)))
1078 (point-end-of-line))))
1079 (funcall output label match-point)
1080 (if (save-excursion
1081 (skip-chars-forward "\t \n")
1082 (looking-at YaTeX::ref-labeling-regexp))
1083 (setq e0 (match-end 0)))))
1084 ) ;;put label buffer
1085 ;;
1086 ;; if user defined label found
1087 (t
1088 ;; memorize line number and label into property
1089 (goto-char (match-beginning 0))
1090 (let ((list YaTeX::ref-labeling-regexp-alist)
1091 (cache (symbol-plist 'YaTeX::ref-labeling-regexp)))
1092 (while list
1093 (if (looking-at (car (car list)))
1094 (progn
1095 (setq label (YaTeX-match-string 0))
1096 (put 'YaTeX::ref-labeling-regexp lnum
1097 (YaTeX-match-string (cdr (car list))))
1098 (funcall output label 0) ;;0 is dummy, never used
1099 (setq list nil)))
1100 (setq list (cdr list))))
1101 ))
1102 (goto-char e0))
1103 (princ YaTeX-label-menu-other)
1104 (princ YaTeX-label-menu-repeat)
1105 (princ YaTeX-label-menu-any)
1106 );standard-output
1107 (goto-char p)
1108 (or initl (setq initl lnum))
1109 (message "Collecting labels...Done")
1110 (if (fboundp 'select-frame) (select-frame cf))
1111 (YaTeX-showup-buffer YaTeX-label-buffer nil t)
1112 (YaTeX::label-setup-key-map)
1113 (setq truncate-lines t)
1114 (setq buffer-read-only t)
1115 (use-local-map YaTeX-label-select-map)
1116 (message YaTeX-label-guide-msg)
1117 (goto-line (1+ initl)) ;goto recently defined label line
1118 (switch-to-buffer (current-buffer))
1119 (unwind-protect
1120 (progn
1121 (recursive-edit)
1123 (set-buffer (get-buffer YaTeX-label-buffer)) ;assertion
1124 (beginning-of-line)
1125 (setq line (1- (count-lines (point-min)(point))))
1126 (cond
1127 ((= line -1) (setq label ""))
1128 ((= line lnum) (setq label (YaTeX-label-other)))
1129 ((= line (1+ lnum))
1130 (save-excursion
1131 (switch-to-buffer buf)
1132 (goto-char p)
1133 (if (re-search-backward
1134 (concat "\\\\" refcmd "{") nil t)
1135 (setq label (YaTeX-buffer-substring
1136 (progn (goto-char (1- (match-end 0)))
1137 (1+ (point)))
1138 (progn (forward-list 1)
1139 (1- (point)))))
1140 (setq label ""))))
1141 ((>= line (+ lnum 2))
1142 (setq label (read-string (format "\\%s{???}: " refcmd))))
1143 (t ;(setq label (nth (- lnum line 1) label-list))
1144 (setq label
1145 (or (get 'YaTeX::ref-labeling-regexp line)
1146 (YaTeX::ref-getset-label
1147 buf (nth (- lnum line 1) point-list))))
1148 )))
1149 (bury-buffer YaTeX-label-buffer)))
1150 label)))))
1152 (defun YaTeX::label-rename-refs (old new &optional def ref)
1153 "Rename reference tag from OLD to NEW.
1154 Optional arguments DEF and REF specify defining command and
1155 referring command respectively.
1156 ---------------------------------------------------------
1157 CONTROL KEYS - キーの説明
1158 y Replace 置換する
1159 n Do not replace 置換しない
1160 ! Replace All w/o query 残る全部を確認なしで置換
1161 r Enter Recursive-edit 再帰編集モードへ
1162 q Quit from replacing ここまでで置換をやめる
1164 Don't forget to exit from recursive edit by typing \\[exit-recursive-edit]
1165 再帰編集に入ったら \\[exit-recursive-edit] で抜け忘れなきよう。"
1166 (save-window-excursion
1167 (catch 'exit
1168 (let*((bufs (YaTeX-yatex-buffer-list)) buf b e
1169 (oldptn (regexp-quote old))
1170 (sw (selected-window))
1171 (ptn (concat
1172 "\\(" YaTeX-refcommand-ref-regexp "\\)"
1173 "\\s *{" oldptn "}"))
1174 (repface (and (fboundp 'make-overlay)
1175 (fboundp 'internal-find-face)
1176 (if (internal-find-face 'isearch) 'isearch 'region)))
1177 ov
1178 (qmsg "Replace to `%s'? [yn!rq?]")
1179 continue ch)
1180 (while bufs
1181 (set-buffer (setq buf (car bufs)))
1182 (save-excursion
1183 (goto-char (point-min))
1184 (while (re-search-forward ptn nil t)
1185 (goto-char (match-end 1))
1186 (skip-chars-forward " \t\n{")
1187 (unwind-protect
1188 (if (and
1189 (looking-at oldptn)
1190 (setq b (match-beginning 0)
1191 e (match-end 0))
1192 (or continue
1193 (catch 'query
1194 (if repface
1195 (if ov (move-overlay ov b e)
1196 (overlay-put
1197 (setq ov (make-overlay b e))
1198 'face repface)))
1199 (switch-to-buffer buf)
1200 (while t
1201 (message qmsg new)
1202 (setq ch (read-char))
1203 (cond
1204 ((= ch ?q) (throw 'exit t))
1205 ((= ch ?r)
1206 (message
1207 "Don't forget to exit recursive-edit by `%s'"
1208 (key-description
1209 (where-is-internal
1210 'exit-recursive-edit '(keymap) t)))
1211 (sleep-for 2)
1212 (recursive-edit))
1213 ((= ch ?y) (throw 'query t))
1214 ((= ch ?!) (throw 'query (setq continue t)))
1215 ((= ch ??)
1216 (describe-function
1217 'YaTeX::label-rename-refs)
1218 (select-window (get-buffer-window "*Help*"))
1219 (search-forward "----")
1220 (forward-line 1)
1221 (set-window-start (selected-window) (point))
1222 (sit-for 0)
1223 (select-window sw))
1224 ((= ch ?n) (throw 'query nil)))))))
1225 (replace-match new))
1226 (and ov (delete-overlay ov)))))
1227 (setq bufs (cdr bufs)))))))
1229 (defun YaTeX::label (argp &optional labname refname)
1230 "Read label name and return it with copying \\ref{LABEL-NAME} to kill-ring."
1231 (cond
1232 ((= argp 1)
1233 (let*((chmode (boundp (intern-soft "old")))
1234 (dlab (if chmode old ;if called via YaTeX-change-section (tricky...)
1235 (YaTeX::ref-default-label)))
1236 (label (read-string
1237 (format "New %s name: " (or labname "label"))
1238 (cons dlab 1))))
1239 (if (string< "" label)
1240 (let ((refstr (format "\\%s{%s}" (or refname "ref") label))
1241 (key (key-description (where-is-internal 'yank nil t)))
1242 (msg
1243 (if YaTeX-japan
1244 "をkill-ringに入れました。yank(%s)で取り出せます。"
1245 " is stored into kill-ring. Paste it by yank(%s).")))
1246 (kill-new refstr)
1247 (and chmode
1248 (not (equal old label))
1249 (YaTeX::label-rename-refs old label))
1250 (message (concat "`%s'" msg) refstr key)))
1251 label))))
1254 (fset 'YaTeX::pageref 'YaTeX::ref)
1255 (defun YaTeX::tabref (argp) ; For the style file of IPSJ journal
1256 (YaTeX::ref
1257 argp nil nil
1258 (function
1259 (lambda ()
1260 (YaTeX-quick-in-environment-p "table")))))
1261 (defun YaTeX::figref (argp) ; For the style file of IPSJ journal
1262 (YaTeX::ref
1263 argp nil nil
1264 (function
1265 (lambda ()
1266 (YaTeX-quick-in-environment-p "figure")))))
1267 (defun YaTeX::eqref (argp)
1268 (YaTeX::ref
1269 argp nil nil
1270 (function
1271 (lambda ()
1272 (YaTeX-in-math-mode-p)))))
1274 (defun YaTeX::cite-collect-bibs-external (bibptn &rest files)
1275 "Collect bibentry from FILES(variable length argument) ;
1276 and print them to standard output."
1277 ;;Thanks; http://icarus.ilcs.hokudai.ac.jp/comp/biblio.html
1278 (let*((tb (get-buffer-create " *bibtmp*"))
1279 (bibitemsep "^\\s *@[A-Za-z]")
1280 (target (if (string< "" bibptn) bibptn bibitemsep))
1281 (checkrx (concat "\\(" bibptn "\\)\\|" bibitemsep))
1282 beg
1283 (searchnext
1284 (if (string< "" bibptn)
1285 (function
1286 (lambda()
1287 (setq beg (point))
1288 (and
1289 (prog1
1290 (re-search-forward target nil t)
1291 (end-of-line))
1292 (re-search-backward bibitemsep beg t))))
1293 (function
1294 (lambda()
1295 (re-search-forward target nil t)))))
1297 (save-excursion
1298 (set-buffer tb)
1299 (princ (format "%sbegin{thebibliography}\n" YaTeX-ec))
1300 (while files
1301 (erase-buffer)
1302 (cond
1303 ((file-exists-p (car files))
1304 (insert-file-contents (car files)))
1305 ((file-exists-p (concat (car files) ".bib"))
1306 (insert-file-contents (concat (car files) ".bib"))))
1307 (save-excursion
1308 (goto-char (point-min))
1309 (while (funcall searchnext)
1310 (skip-chars-forward "^{,")
1311 (setq beg (point))
1312 (if (= (char-after (point)) ?{)
1313 (princ (format "%sbibitem{%s}%s\n"
1314 YaTeX-ec
1315 (buffer-substring
1316 (1+ (point))
1317 (progn (skip-chars-forward "^,\n")
1318 (point)))
1319 (mapconcat
1320 (function
1321 (lambda (kwd)
1322 (goto-char beg)
1323 (if (re-search-forward
1324 (concat kwd "\\s *=") nil t)
1325 (buffer-substring
1326 (progn
1327 (goto-char (match-end 0))
1328 (skip-chars-forward " \t\n")
1329 (point))
1330 (progn
1331 (if (looking-at "[{\"]")
1332 (forward-sexp 1)
1333 (forward-char 1)
1334 (skip-chars-forward "^,}"))
1335 (point))))))
1336 '("author" "year" "title" )
1337 ";"))))
1338 (and (re-search-forward bibitemsep nil t)
1339 (forward-line -1))))
1340 (setq files (cdr files)))
1341 (princ (format "%sbegin{thebibliography}\n" YaTeX-ec)))))
1343 (defvar YaTeX::cite-bibitem-macro-regexp "bibitem\\|harvarditem"
1344 "*Regexp of macro name of bibitem definition")
1346 (defun YaTeX::cite-collect-bibs-internal (bibptn)
1347 "Collect bibentry in the current buffer and print them to standard output."
1348 (let ((ptn (concat YaTeX-ec-regexp
1349 "\\(" YaTeX::cite-bibitem-macro-regexp "\\)\\b"))
1350 (lim (concat YaTeX-ec-regexp
1351 "\\(" YaTeX::cite-bibitem-macro-regexp "\\b\\)"
1352 "\\|\\(end{\\)"))
1353 (pcnt (regexp-quote YaTeX-comment-prefix)))
1354 ;; Using bibptn not yet implemented.
1355 ;; Do you need it?? 2005/11/22
1356 (save-excursion
1357 (while (YaTeX-re-search-active-forward ptn pcnt nil t)
1358 (skip-chars-forward "^{\n")
1359 (or (eolp)
1360 (princ (format "%sbibitem%s %s\n"
1361 YaTeX-ec
1362 (buffer-substring
1363 (point)
1364 (progn (forward-sexp 1) (point)))
1365 (buffer-substring
1366 (progn (skip-chars-forward "\n \t") (point))
1367 (save-excursion
1368 (if (YaTeX-re-search-active-forward
1369 lim pcnt nil t)
1370 (progn
1371 (goto-char (match-beginning 0))
1372 (skip-chars-backward "\n \t")
1373 (point))
1374 (point-end-of-line)))))))))))
1376 (defun YaTeX::cite (argp &rest dummy)
1377 (cond
1378 ((eq argp 1)
1379 (let* ((cb (current-buffer))
1380 (f (file-name-nondirectory buffer-file-name))
1381 (d default-directory)
1382 (hilit-auto-highlight nil)
1383 (pcnt (regexp-quote YaTeX-comment-prefix))
1384 (bibrx (concat YaTeX-ec-regexp "bibliography{\\([^}]+\\)}"))
1385 (bibptn (read-string "Pattern: "))
1386 (bbuf (get-buffer-create " *bibitems*"))
1387 (standard-output bbuf)
1388 (me 'YaTeX::cite) ;shuld set this for using YaTeX::ref
1389 bibs files)
1390 (set-buffer bbuf)(erase-buffer)(set-buffer cb)
1391 (save-excursion
1392 (goto-char (point-min))
1393 ;;(1)search external bibdata
1394 (while (YaTeX-re-search-active-forward bibrx pcnt nil t)
1395 (apply 'YaTeX::cite-collect-bibs-external
1396 bibptn
1397 (YaTeX-split-string
1398 (YaTeX-match-string 1) ",")))
1399 ;;(2)search direct \bibitem usage
1400 (YaTeX::cite-collect-bibs-internal bibptn)
1401 (if (progn
1402 (YaTeX-visit-main t)
1403 (not (eq (current-buffer) cb)))
1404 (save-excursion
1405 (goto-char (point-min))
1406 ;;(1)search external bibdata
1407 (while (YaTeX-re-search-active-forward bibrx pcnt nil t)
1408 (apply 'YaTeX::cite-collect-bibs-external
1409 bibptn
1410 (YaTeX-split-string
1411 (YaTeX-match-string 1) ",")))
1412 ;;(2)search internal
1413 (YaTeX::cite-collect-bibs-internal bibptn)))
1414 ;;Now bbuf holds the list of bibitem
1415 (set-buffer bbuf)
1416 ;;;(switch-to-buffer bbuf)
1417 (if (fboundp 'font-lock-fontify-buffer) (font-lock-fontify-buffer))
1418 (YaTeX::ref
1419 argp
1420 (concat "\\\\\\("
1421 YaTeX::cite-bibitem-macro-regexp
1422 "\\)\\(\\[.*\\]\\)?")
1423 "cite"))))
1425 (t nil)))
1427 (defun YaTeX::bibitem (argp)
1428 "Add-in function to insert argument of \\bibitem."
1429 (YaTeX::label argp "label" "cite"))
1431 ;;; for Harvard citation style
1432 (fset 'YaTeX::citeasnoun 'YaTeX::cite)
1433 (fset 'YaTeX::possessivecite 'YaTeX::cite)
1434 (fset 'YaTeX::citeyear 'YaTeX::cite)
1435 (fset 'YaTeX::citename 'YaTeX::cite)
1436 (fset 'YaTeX::citep 'YaTeX::cite)
1437 (fset 'YaTeX::citet 'YaTeX::cite)
1439 (defun YaTeX-select-other-yatex-buffer ()
1440 "Select buffer from all yatex-mode's buffers interactivelly."
1441 (interactive)
1442 (let ((lbuf "*YaTeX mode buffers*") (blist (YaTeX-yatex-buffer-list))
1443 (lnum -1) buf rv
1444 (ff "**find-file**"))
1445 (YaTeX-showup-buffer
1446 lbuf (function (lambda (x) 1))) ;;Select next window surely.
1447 (save-excursion
1448 (set-buffer (get-buffer lbuf))
1449 (setq buffer-read-only nil)
1450 (erase-buffer))
1451 (let ((standard-output (get-buffer lbuf)))
1452 (while blist
1453 (princ
1454 (format "%c:{%s}\n" (+ (% (setq lnum (1+ lnum)) 26) ?A)
1455 (buffer-name (car blist))))
1456 (setq blist (cdr blist)))
1457 (princ (format "':{%s}" ff)))
1458 (YaTeX-showup-buffer lbuf nil t)
1459 (YaTeX::label-setup-key-map)
1460 (setq buffer-read-only t)
1461 (use-local-map YaTeX-label-select-map)
1462 (message YaTeX-label-guide-msg)
1463 (unwind-protect
1464 (progn
1465 (recursive-edit)
1466 (set-buffer lbuf)
1467 (beginning-of-line)
1468 (setq rv
1469 (if (re-search-forward "{\\([^\\}]+\\)}" (point-end-of-line) t)
1470 (buffer-substring (match-beginning 1) (match-end 1)) nil)))
1471 (kill-buffer lbuf))
1472 (if (string= rv ff)
1473 (progn
1474 (call-interactively 'find-file)
1475 (current-buffer))
1476 rv)))
1478 (defun YaTeX-label-other ()
1479 (let ((rv (YaTeX-select-other-yatex-buffer)))
1480 (cond
1481 ((null rv) "")
1482 (t
1483 (set-buffer rv)
1484 (funcall me argp labelcmd refcmd)))))
1486 ;;
1487 ; completion for the arguments of \newcommand
1488 ;;
1489 (defun YaTeX::newcommand (&optional argp)
1490 (cond
1491 ((= argp 1)
1492 (let ((command (read-string "Define newcommand: " "\\")))
1493 (put 'YaTeX::newcommand 'command (substring command 1))
1494 command))
1495 ((= argp 2)
1496 (let ((argc
1497 (string-to-int (read-string "Number of arguments(Default 0): ")))
1498 (def (read-string "Definition: "))
1499 (command (get 'YaTeX::newcommand 'command)))
1500 ;;!!! It's illegal to insert string in the add-in function !!!
1501 (if (> argc 0) (insert (format "[%d]" argc)))
1502 (if (and (stringp command)
1503 (string< "" command)
1504 (y-or-n-p "Update dictionary?"))
1505 (cond
1506 ((= argc 0)
1507 (YaTeX-update-table
1508 (list command)
1509 'singlecmd-table 'user-singlecmd-table 'tmp-singlecmd-table))
1510 ((= argc 1)
1511 (YaTeX-update-table
1512 (list command)
1513 'section-table 'user-section-table 'tmp-section-table))
1514 (t (YaTeX-update-table
1515 (list command argc)
1516 'section-table 'user-section-table 'tmp-section-table))))
1517 (message "")
1518 def ;return command name
1519 ))
1520 (t "")))
1522 (defun YaTeX::newcounter (&optional argp)
1523 (cond
1524 ((= argp 1)
1525 (read-string "New counter name: "))
1526 (t "")))
1528 ;;
1529 ; completion for the arguments of \pagestyle
1530 ;;
1531 (defun YaTeX::pagestyle (&optional argp)
1532 "Read the pagestyle with completion."
1533 (completing-read
1534 "Page style: "
1535 '(("plain") ("empty") ("headings") ("myheadings") ("normal") nil)))
1537 (fset 'YaTeX::thispagestyle 'YaTeX::pagestyle)
1539 ;;
1540 ; completion for the arguments of \pagenumbering
1541 ;;
1542 (defun YaTeX::pagenumbering (&optional argp)
1543 "Read the numbering style."
1544 (completing-read
1545 "Page numbering style: "
1546 '(("arabic") ("Alpha") ("alpha") ("Roman") ("roman"))))
1548 ;;
1549 ; Length
1550 ;;
1551 (defvar YaTeX:style-parameters-default
1552 '(("\\arraycolsep")
1553 ("\\arrayrulewidth")
1554 ("\\baselineskip")
1555 ("\\columnsep")
1556 ("\\columnseprule")
1557 ("\\doublerulesep")
1558 ("\\evensidemargin")
1559 ("\\footheight")
1560 ("\\footskip")
1561 ("\\headheight")
1562 ("\\headsep")
1563 ("\\itemindent")
1564 ("\\itemsep")
1565 ("\\labelsep")
1566 ("\\labelwidth")
1567 ("\\leftmargin")
1568 ("\\linewidth")
1569 ("\\listparindent")
1570 ("\\marginparsep")
1571 ("\\marginparwidth")
1572 ("\\mathindent")
1573 ("\\oddsidemargin")
1574 ("\\parindent")
1575 ("\\parsep")
1576 ("\\parskip")
1577 ("\\partopsep")
1578 ("\\rightmargin")
1579 ("\\tabcolsep")
1580 ("\\textheight")
1581 ("\\textwidth")
1582 ("\\topmargin")
1583 ("\\topsep")
1584 ("\\topskip")
1586 "Alist of LaTeX style parameters.")
1587 (defvar YaTeX:style-parameters-private nil
1588 "*User definable alist of style parameters.")
1589 (defvar YaTeX:style-parameters-local nil
1590 "*User definable alist of local style parameters.")
1592 (defvar YaTeX:length-history nil "Holds history of length.")
1593 (put 'YaTeX:length-history 'no-default t)
1594 (defun YaTeX::setlength (&optional argp)
1595 "YaTeX add-in function for arguments of \\setlength."
1596 (cond
1597 ((equal 1 argp)
1598 ;;(completing-read "Length variable: " YaTeX:style-parameters nil nil "\\")
1599 (YaTeX-cplread-with-learning
1600 "Length variable: "
1601 'YaTeX:style-parameters-default
1602 'YaTeX:style-parameters-private
1603 'YaTeX:style-parameters-local
1604 nil nil "\\")
1606 ((equal 2 argp)
1607 (read-string-with-history "Length: " nil 'YaTeX:length-history))))
1609 (fset 'YaTeX::addtolength 'YaTeX::setlength)
1611 (defun YaTeX::settowidth (&optional argp)
1612 "YaTeX add-in function for arguments of \\settowidth."
1613 (cond
1614 ((equal 1 argp)
1615 (YaTeX-cplread-with-learning
1616 "Length variable: "
1617 'YaTeX:style-parameters-default
1618 'YaTeX:style-parameters-private
1619 'YaTeX:style-parameters-local
1620 nil nil "\\"))
1621 ((equal 2 argp)
1622 (read-string "Text: "))))
1624 (defun YaTeX::newlength (&optional argp)
1625 "YaTeX add-in function for arguments of \\newlength"
1626 (cond
1627 ((equal argp 1)
1628 (let ((length (read-string "Length variable: " "\\")))
1629 (if (string< "" length)
1630 (YaTeX-update-table
1631 (list length)
1632 'YaTeX:style-parameters-default
1633 'YaTeX:style-parameters-private
1634 'YaTeX:style-parameters-local))
1635 length))))
1637 ;; \multicolumn's arguments
1638 (defun YaTeX::multicolumn (&optional argp)
1639 "YaTeX add-in function for arguments of \\multicolumn."
1640 (cond
1641 ((equal 1 argp)
1642 (read-string "Number of columns: "))
1643 ((equal 2 argp)
1644 (YaTeX:read-oneof "|lrc" nil t))
1645 ((equal 3 argp)
1646 (read-string "Item: "))))
1648 (defvar YaTeX:documentstyles-default
1649 '(("article") ("jarticle") ("j-article")
1650 ("book") ("jbook") ("j-book")
1651 ("report") ("jreport") ("j-report")
1652 ("letter") ("ascjletter"))
1653 "List of LaTeX documentstyles.")
1654 (defvar YaTeX:documentstyles-private nil
1655 "*User defined list of LaTeX documentstyles.")
1656 (defvar YaTeX:documentstyles-local nil
1657 "*User defined list of local LaTeX documentstyles.")
1658 (defvar YaTeX:documentstyle-options-default
1659 '(("a4j") ("a5j") ("b4j") ("b5j")
1660 ("twocolumn") ("jtwocolumn") ("epsf") ("epsfig") ("epsbox") ("nfig"))
1661 "List of LaTeX documentstyle options.")
1662 (defvar YaTeX:documentstyle-options-private nil
1663 "*User defined list of LaTeX documentstyle options.")
1664 (defvar YaTeX:documentstyle-options-local nil
1665 "List of LaTeX local documentstyle options.")
1667 (defun YaTeX:documentstyle ()
1668 (let*((delim ",")
1669 (dt (append YaTeX:documentstyle-options-local
1670 YaTeX:documentstyle-options-private
1671 YaTeX:documentstyle-options-default))
1672 (minibuffer-completion-table dt)
1673 (opt (read-from-minibuffer
1674 "Style options ([opt1,opt2,...]): "
1675 nil YaTeX-minibuffer-completion-map nil))
1676 (substr opt) o)
1677 (if (string< "" opt)
1678 (progn
1679 (while substr
1680 (setq o (substring substr 0 (string-match delim substr)))
1681 (or (assoc o dt)
1682 (YaTeX-update-table
1683 (list o)
1684 'YaTeX:documentstyle-options-default
1685 'YaTeX:documentstyle-options-private
1686 'YaTeX:documentstyle-options-local))
1687 (setq substr
1688 (if (string-match delim substr)
1689 (substring substr (1+ (string-match delim substr))))))
1690 (concat "[" opt "]"))
1691 "")))
1693 (defun YaTeX::documentstyle (&optional argp)
1694 "YaTeX add-in function for arguments of \\documentstyle."
1695 (cond
1696 ((equal argp 1)
1697 (setq YaTeX-env-name "document")
1698 (let ((sname
1699 (YaTeX-cplread-with-learning
1700 (format "Documentstyle (default %s): "
1701 YaTeX-default-document-style)
1702 'YaTeX:documentstyles-default
1703 'YaTeX:documentstyles-private
1704 'YaTeX:documentstyles-local)))
1705 (if (string= "" sname) (setq sname YaTeX-default-document-style))
1706 (setq YaTeX-default-document-style sname)))))
1708 (defun YaTeX::include (argp &optional prompt)
1709 "Read file name setting default directory to that of main file."
1710 (cond
1711 ((= argp 1)
1712 (save-excursion
1713 (YaTeX-visit-main t)
1714 (let*((insert-default-directory)
1715 (file (read-file-name (or prompt "Input file: ") "")))
1716 (setq file (substring file 0 (string-match "\\.tex$" file))))))))
1718 (fset 'YaTeX::input 'YaTeX::include)
1721 ;;; -------------------- LaTeX2e stuff --------------------
1722 (defvar YaTeX:documentclass-options-default
1723 '(("a4paper") ("a5paper") ("b4paper") ("b5paper") ("10pt") ("11pt") ("12pt")
1724 ("latterpaper") ("legalpaper") ("executivepaper") ("landscape")
1725 ("oneside") ("twoside") ("draft") ("final") ("leqno") ("fleqn") ("openbib")
1726 ("tombow") ("titlepage") ("notitlepage") ("dvips")
1727 ("mingoth") ;for jsarticle
1728 ("clock") ;for slides class only
1730 "Default options list for documentclass")
1731 (defvar YaTeX:documentclass-options-private nil
1732 "*User defined options list for documentclass")
1733 (defvar YaTeX:documentclass-options-local nil
1734 "*User defined options list for local documentclass")
1736 (defun YaTeX:documentclass ()
1737 (let*((delim ",")
1738 (dt (append YaTeX:documentclass-options-local
1739 YaTeX:documentclass-options-private
1740 YaTeX:documentclass-options-default))
1741 (minibuffer-completion-table dt)
1742 (opt (read-from-minibuffer
1743 "Documentclass options ([opt1,opt2,...]): "
1744 nil YaTeX-minibuffer-completion-map nil))
1745 (substr opt) o)
1746 (if (string< "" opt)
1747 (progn
1748 (while substr
1750 (setq o (substring substr 0 (string-match delim substr)))
1751 (or (assoc o dt)
1752 (YaTeX-update-table
1753 (list o)
1754 'YaTeX:documentclass-options-default
1755 'YaTeX:documentclass-options-private
1756 'YaTeX:documentclass-options-local))
1757 (setq substr
1758 (if (string-match delim substr)
1759 (substring substr (1+ (string-match delim substr))))))
1760 (concat "[" opt "]"))
1761 "")))
1763 (defvar YaTeX:documentclasses-default
1764 '(("article") ("jarticle") ("report") ("jreport") ("book") ("jbook")
1765 ("jsarticle") ("jsbook")
1766 ("j-article") ("j-report") ("j-book")
1767 ("letter") ("slides") ("ltxdoc") ("ltxguide") ("ltnews") ("proc"))
1768 "Default documentclass alist")
1769 (defvar YaTeX:documentclasses-private nil
1770 "*User defined documentclass alist")
1771 (defvar YaTeX:documentclasses-local nil
1772 "*User defined local documentclass alist")
1773 (defvar YaTeX-default-documentclass (if YaTeX-japan "jarticle" "article")
1774 "*Default documentclass")
1776 (defun YaTeX::documentclass (&optional argp)
1777 (cond
1778 ((equal argp 1)
1779 (setq YaTeX-env-name "document")
1780 (let ((sname
1781 (YaTeX-cplread-with-learning
1782 (format "Documentclass (default %s): " YaTeX-default-documentclass)
1783 'YaTeX:documentclasses-default
1784 'YaTeX:documentclasses-private
1785 'YaTeX:documentclasses-local)))
1786 (if (string= "" sname) (setq sname YaTeX-default-documentclass))
1787 (setq YaTeX-default-documentclass sname)))))
1789 (defvar YaTeX:latex2e-named-color-alist
1790 '(("GreenYellow") ("Yellow") ("Goldenrod") ("Dandelion") ("Apricot")
1791 ("Peach") ("Melon") ("YellowOrange") ("Orange") ("BurntOrange")
1792 ("Bittersweet") ("RedOrange") ("Mahogany") ("Maroon") ("BrickRed")
1793 ("Red") ("OrangeRed") ("RubineRed") ("WildStrawberry") ("Salmon")
1794 ("CarnationPink") ("Magenta") ("VioletRed") ("Rhodamine") ("Mulberry")
1795 ("RedViolet") ("Fuchsia") ("Lavender") ("Thistle") ("Orchid")("DarkOrchid")
1796 ("Purple") ("Plum") ("Violet") ("RoyalPurple") ("BlueViolet")
1797 ("Periwinkle") ("CadetBlue") ("CornflowerBlue") ("MidnightBlue")
1798 ("NavyBlue") ("RoyalBlue") ("Blue") ("Cerulean") ("Cyan") ("ProcessBlue")
1799 ("SkyBlue") ("Turquoise") ("TealBlue") ("Aquamarine") ("BlueGreen")
1800 ("Emerald") ("JungleGreen") ("SeaGreen") ("Green") ("ForestGreen")
1801 ("PineGreen") ("LimeGreen") ("YellowGreen") ("SpringGreen") ("OliveGreen")
1802 ("RawSienna") ("Sepia") ("Brown") ("Tan") ("Gray") ("Black") ("White"))
1803 "Colors defined in $TEXMF/tex/plain/dvips/colordvi.tex")
1805 (defvar YaTeX:latex2e-basic-color-alist
1806 '(("black") ("white") ("red") ("blue") ("yellow") ("green") ("cyan")
1807 ("magenta"))
1808 "Basic colors")
1810 (defun YaTeX:textcolor ()
1811 "Add-in for \\color's option"
1812 (if (y-or-n-p "Use `named' color? ")
1813 "[named]"))
1815 (defun YaTeX::color-completing-read (prompt)
1816 (let ((completion-ignore-case t)
1817 (namedp (save-excursion
1818 (skip-chars-backward "^\n\\[\\\\")
1819 (looking-at "named"))))
1820 (completing-read
1821 prompt
1822 (if namedp
1823 YaTeX:latex2e-named-color-alist
1824 YaTeX:latex2e-basic-color-alist)
1825 nil t)))
1827 (defun YaTeX::textcolor (argp)
1828 "Add-in for \\color's argument"
1829 (cond
1830 ((= argp 1) (YaTeX::color-completing-read "Color: "))
1831 ((= argp 2) (read-string "Colored string: "))))
1833 (fset 'YaTeX:color 'YaTeX:textcolor)
1834 (fset 'YaTeX::color 'YaTeX::textcolor)
1835 (fset 'YaTeX:colorbox 'YaTeX:textcolor)
1836 (fset 'YaTeX::colorbox 'YaTeX::textcolor)
1837 (fset 'YaTeX:fcolorbox 'YaTeX:textcolor)
1838 (fset 'YaTeX:pagecolor 'YaTeX:textcolor)
1839 (fset 'YaTeX::pagecolor 'YaTeX::textcolor)
1841 (defun YaTeX::fcolorbox (argp)
1842 (cond
1843 ((= argp 1) (YaTeX::color-completing-read "Frame color: "))
1844 ((= argp 2) (YaTeX::color-completing-read "Inner color: "))
1845 ((= argp 3) (read-string "Colored string: "))))
1847 (defun YaTeX:scalebox ()
1848 "Add-in for \\scalebox"
1849 (let ((vmag (read-string
1850 (if YaTeX-japan "倍率(負で反転): "
1851 "Magnification(Negative for flipped): ")))
1852 (hmag (read-string (if YaTeX-japan "縦倍率(省略可): "
1853 "Vertical magnification(Optional): "))))
1854 (if (and hmag (string< "" hmag))
1855 (format "{%s}[%s]" vmag hmag)
1856 (format "{%s}" vmag))))
1858 (defun YaTeX:rotatebox ()
1859 "Optional argument add-in for \\rotatebox"
1860 (message "Rotate origin? (N)one (O)rigin (X)-Y: ")
1861 (let ((c (read-char)) r (defx "x=mm") x (defy "y=mm") y something)
1862 (cond
1863 ((memq c '(?O ?o))
1864 (if (string< "" (setq r (YaTeX:read-oneof "htbpB")))
1865 (concat "[origin=" r "]")))
1866 ((memq c '(?X ?x ?Y ?y))
1867 (setq r (read-string "" (if YaTeX-emacs-19 (cons defx 3) defx))
1868 x (if (string< "x=" r) r)
1869 r (read-string "" (if YaTeX-emacs-19 (cons defy 3) defy))
1870 y (if (string< "y=" r) r)
1871 something (or x y))
1872 (format "%s%s%s%s%s"
1873 (if something "[" "")
1874 (if x x "")
1875 (if (and x y) "," "")
1876 (if y y "")
1877 (if something "]" ""))))))
1879 (defun YaTeX::rotatebox (argp)
1880 "Argument add-in for \\rotatebox"
1881 (cond
1882 ((= argp 1)
1883 (read-string (if YaTeX-japan "回転角(度; 左回り): "
1884 "Angle in degree(unclockwise): ")))
1885 ((= argp 2)
1886 (read-string (if YaTeX-japan "テキスト: " "Text: ")))))
1888 (defun YaTeX:includegraphics ()
1889 "Add-in for \\includegraphics's option"
1890 (let (width height (scale "") angle str)
1891 (setq width (YaTeX-read-string-or-skip "Width: ")
1892 height (YaTeX-read-string-or-skip "Height: "))
1893 (or (string< width "") (string< "" height)
1894 (setq scale (YaTeX-read-string-or-skip "Scale: ")))
1895 (setq angle (YaTeX-read-string-or-skip "Angle(0-359): "))
1896 (setq str
1897 (mapconcat
1898 'concat
1899 (delq nil
1900 (mapcar '(lambda (s)
1901 (and (stringp (symbol-value s))
1902 (string< "" (symbol-value s))
1903 (format "%s=%s" s (symbol-value s))))
1904 '(width height scale angle)))
1905 ","))
1906 (if (string= "" str) ""
1907 (concat "[" str "]"))))
1909 (defun YaTeX::includegraphics (argp)
1910 "Add-in for \\includegraphics"
1911 (YaTeX::include argp "Image File: "))
1913 (defun YaTeX::verbfile (argp)
1914 "Add-in for \\verbfile"
1915 (YaTeX::include argp "Virbatim File: "))
1917 (defun YaTeX:caption ()
1918 (setq YaTeX-section-name "label")
1919 nil)
1922 (defvar YaTeX::usepackage-alist-default
1923 '(("version") ("plext") ("url") ("fancybox") ("pifont") ("longtable")
1924 ("ascmac") ("bm") ("graphics") ("graphicx") ("alltt") ("misc") ("eclbkbox")
1925 ("amsmath") ("amssymb") ("xymtex") ("chemist")
1926 ("a4j") ("array") ("epsf") ("color") ("epsfig") ("floatfig")
1927 ("landscape") ("path") ("supertabular") ("twocolumn")
1928 ("latexsym") ("times") ("makeidx"))
1929 "Default completion table for arguments of \\usepackage")
1931 (defvar YaTeX::usepackage-alist-private nil
1932 "*Private completion list of the argument for usepackage")
1934 (defvar YaTeX::usepackage-alist-local nil
1935 "Directory local completion list of the argument for usepackage")
1937 (defun YaTeX::usepackage (&optional argp)
1938 (cond
1939 ((equal argp 1)
1940 (setq YaTeX-env-name "document")
1941 (let ((minibuffer-local-completion-map YaTeX-minibuffer-completion-map)
1942 (delim ","))
1943 (YaTeX-cplread-with-learning
1944 (if YaTeX-japan "Use package(カンマで区切ってOK): "
1945 "Use package(delimitable by comma): ")
1946 'YaTeX::usepackage-alist-default
1947 'YaTeX::usepackage-alist-private
1948 'YaTeX::usepackage-alist-local)))))
1950 (defun YaTeX::mask (argp)
1951 (cond
1952 ((equal argp 1)
1953 (read-string "String: "))
1954 ((equal argp 2)
1955 (let (c)
1956 (while (not (memq c '(?A ?B ?C ?D ?E ?F ?G ?H ?I ?J ?K)))
1957 (message "Mask type(A..K): ")
1958 (setq c (upcase (read-char))))
1959 (format "%c" c)))))
1961 (defun YaTeX::maskbox (argp)
1962 (cond
1963 ((equal argp 1)
1964 (read-string "Width: "))
1965 ((equal argp 2)
1966 (read-string "Height: "))
1967 ((equal argp 3)
1968 (let (c)
1969 (while (not (memq c '(?A ?B ?C ?D ?E ?F ?G ?H ?I ?J ?K)))
1970 (message "Mask type(A..K): ")
1971 (setq c (upcase (read-char))))
1972 (format "%c" c)))
1973 ((equal argp 4)
1974 (YaTeX:read-oneof "lcr" 'quick))
1975 ((equal argp 5)
1976 (read-string "String: "))))
1978 (defun YaTeX::textcircled (argp)
1979 (cond
1980 ((equal argp 1)
1981 (let ((char (read-string "Circled char: "))
1982 (left "") (right "") c)
1983 (setq c (read-char
1984 "Enclose also with (s)mall (t)iny s(C)riptsize (N)one:"))
1985 (cond
1986 ((memq c '(?s ?S)) (setq left "{\\small " right "}"))
1987 ((memq c '(?t ?T)) (setq left "{\\tiny " right "}"))
1988 ((memq c '(?c ?C)) (setq left "{\\scriptsize " right "}")))
1989 (format "%s%s%s" left char right)))))
1991 ;;; -------------------- math-mode stuff --------------------
1992 (defun YaTeX::tilde (&optional pos)
1993 "For accent macros in mathmode"
1994 (cond
1995 ((equal pos 1)
1996 (message "Put accent on variable: ")
1997 (let ((v (char-to-string (read-char))) (case-fold-search nil))
1998 (message "")
1999 (cond
2000 ((string-match "i\\|j" v)
2001 (concat "\\" v "math"))
2002 ((string-match "[\r\n\t ]" v)
2003 "")
2004 (t v))))
2005 (nil "")))
2007 (fset 'YaTeX::hat 'YaTeX::tilde)
2008 (fset 'YaTeX::check 'YaTeX::tilde)
2009 (fset 'YaTeX::bar 'YaTeX::tilde)
2010 (fset 'YaTeX::dot 'YaTeX::tilde)
2011 (fset 'YaTeX::ddot 'YaTeX::tilde)
2012 (fset 'YaTeX::vec 'YaTeX::tilde)
2014 (defun YaTeX::widetilde (&optional pos)
2015 "For multichar accent macros in mathmode"
2016 (cond
2017 ((equal pos 1)
2018 (let ((m "Put over chars[%s ]: ") v v2)
2019 (message m " ")
2020 (setq v (char-to-string (read-char)))
2021 (message "")
2022 (if (string-match "[\r\n\t ]" v)
2023 ""
2024 (message m v)
2025 (setq v2 (char-to-string (read-char)))
2026 (message "")
2027 (if (string-match "[\r\n\t ]" v2)
2029 (concat v v2)))))
2030 (nil "")))
2032 (fset 'YaTeX::widehat 'YaTeX::widetilde)
2033 (fset 'YaTeX::overline 'YaTeX::widetilde)
2034 (fset 'YaTeX::overrightarrow 'YaTeX::widetilde)
2037 ; for \frac{}{} region
2038 (defun YaTeX::frac-region (beg end)
2039 (if (catch 'done
2040 (while (re-search-forward "\\s *\\(\\\\over\\|/\\)\\s *" end t)
2041 (goto-char (match-beginning 0))
2042 (if (y-or-n-p
2043 (format "Replace this `%s' with `}{'" (YaTeX-match-string 0)))
2044 (throw 'done t))
2045 (goto-char (match-end 0))))
2046 (let (p (b0 (match-beginning 0)) e0)
2047 (replace-match "}{")
2048 (setq e0 (point))
2049 (save-restriction
2050 (narrow-to-region beg end)
2051 (goto-char e0)
2052 (skip-chars-forward " \t")
2053 (setq p (point))
2054 (YaTeX-goto-corresponding-paren)
2055 (forward-char 1)
2056 (skip-chars-forward " \t\r\n")
2057 (if (= end (1+ (point)))
2058 (progn
2059 (goto-char p)
2060 (if (looking-at "\\\\") (forward-char 1))
2061 (YaTeX-kill-paren nil)))
2062 (goto-char beg)
2063 (skip-chars-forward " \t")
2064 (setq p (point))
2065 (YaTeX-goto-corresponding-paren)
2066 (forward-char 1)
2067 (skip-chars-forward " \t\r\n")
2068 (if (>= (point) b0)
2069 (progn
2070 (goto-char p)
2071 (if (looking-at "\\\\") (forward-char 1))
2072 (YaTeX-kill-paren nil))))))
2073 (message ""))
2075 (defun YaTeX::DeclareMathOperator (argp)
2076 (cond
2077 ((equal argp 1)
2078 (read-string "Operator: " "\\"))))
2080 ;;;
2081 ;; Add-in functions for large-type command.
2082 ;;;
2083 (defun YaTeX:em ()
2084 (cond
2085 ((eq YaTeX-current-completion-type 'large) "\\/")
2086 (t nil)))
2087 (fset 'YaTeX:it 'YaTeX:em)
2089 ;;; -------------------- End of yatexadd --------------------
2090 (provide 'yatexadd)
2091 ; Local variables:
2092 ; fill-prefix: ";;; "
2093 ; paragraph-start: "^$\\| \\|;;;$"
2094 ; paragraph-separate: "^$\\| \\|;;;$"
2095 ; coding: sjis
2096 ; End: