yatex

view yatexadd.el @ 259:7d4f8f3d540d

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