yatex

view yatexadd.el @ 234:b75390dd4260

C-j can skip further parameter input for table/figure environment.
author yuuji@gentei.org
date Sat, 28 Jan 2012 11:16:11 +0900
parents b46b927914c8
children af1e6c48eac0
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 Sat Jan 28 11:03:05 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 (or (YaTeX-skip-next-reader-p)
115 (read-string (concat tl " Label: "))))
116 (if (string= "" label)
117 (setq YaTeX-section-name "label")
118 (setq opts (format "\n\\label{%s}" label)))
119 (setq caption (or (YaTeX-skip-next-reader-p)
120 (read-string (concat tl " Caption: "))))
121 (if (string= "" caption)
122 (setq YaTeX-section-name "caption")
123 (setq caption (format "\\caption{%s}" caption)
124 opts (format "%s\n%s\n%s"
125 opts
126 (if top caption heremsg)
127 (if top heremsg caption)))
128 (format "\\caption{%s}" caption))
129 (concat pos opts)))
131 (fset 'YaTeX:figure* 'YaTeX:figure)
133 ;;;
134 ;; Functions for table environemnt
135 ;;;
136 (defvar YaTeX:table-caption-first t
137 "*Non-nil indicates put caption before tabular.")
138 (defun YaTeX:table ()
139 "YaTeX add-in function for table environment."
140 (cond
141 ((eq major-mode 'yatex-mode)
142 (setq YaTeX-env-name "tabular")
143 (YaTeX:figure "Table" YaTeX:table-caption-first))
144 ((eq major-mode 'texinfo-mode)
145 (concat " "
146 (completing-read
147 "Highlights with: "
148 '(("@samp")("@kbd")("@code")("@asis")("@file")("@var"))
149 nil nil "@")))))
150 (fset 'YaTeX:table* 'YaTeX:table)
152 (defun YaTeX:description ()
153 "Truly poor service:-)"
154 (setq YaTeX-single-command "item[]")
155 "")
157 (defun YaTeX:itemize ()
158 "It's also poor service."
159 (setq YaTeX-single-command "item")
160 "")
162 (fset 'YaTeX:enumerate 'YaTeX:itemize)
164 (defun YaTeX:picture ()
165 "Ask the size of coordinates of picture environment."
166 (concat (YaTeX:read-coordinates "Picture size")
167 (YaTeX:read-coordinates "Initial position")))
169 (defun YaTeX:equation ()
170 (YaTeX-jmode-off)
171 (if (fboundp 'YaTeX-toggle-math-mode)
172 (YaTeX-toggle-math-mode t))) ;force math-mode ON.
174 (mapcar '(lambda (f) (fset f 'YaTeX:equation))
175 '(YaTeX:eqnarray YaTeX:eqnarray* YaTeX:align YaTeX:align*
176 YaTeX:split YaTeX:multline YaTeX:multline* YaTeX:gather YaTeX:gather*
177 YaTeX:aligned* YaTeX:gathered YaTeX:gathered*
178 YaTeX:alignat YaTeX:alignat* YaTeX:xalignat YaTeX:xalignat*
179 YaTeX:xxalignat YaTeX:xxalignat*))
181 (defun YaTeX:alignat ()
182 (YaTeX:equation)
183 (concat "{" (read-string "Number of cols: ") "}"))
187 (defun YaTeX:list ()
188 "%\n{} %default label\n{} %formatting parameter")
190 (defun YaTeX:minipage ()
191 (concat (YaTeX:read-position "cbt")
192 "{" (read-string "Width: ") "}"))
194 (defun YaTeX:thebibliography ()
195 (setq YaTeX-section-name "bibitem")
196 (concat "{" (read-string "Longest label: ") "}"))
198 (defun YaTeX:multicols ()
199 (concat "{" (read-string "Number of columns: ") "}"))
201 ;;;
202 ;;Sample functions for section-type command.
203 ;;;
204 (defun YaTeX:multiput ()
205 (concat (YaTeX:read-coordinates "Pos")
206 (YaTeX:read-coordinates "Step")
207 "{" (read-string "How many times: ") "}"))
209 (defun YaTeX:put ()
210 (YaTeX:read-coordinates "Pos"))
212 (defun YaTeX:makebox ()
213 (cond
214 ((YaTeX-in-environment-p "picture")
215 (concat (YaTeX:read-coordinates "Dimension")
216 (YaTeX:read-position "lsrtb")))
217 (t
218 (let ((width (read-string "Width: ")))
219 (if (string< "" width)
220 (progn
221 (or (equal (aref width 0) ?\[)
222 (setq width (concat "[" width "]")))
223 (concat width (YaTeX:read-position
224 (if YaTeX-use-LaTeX2e "lrs" "lr")))))))))
226 ;; (defun YaTeX:framebox ()
227 ;; (if (YaTeX-quick-in-environment-p "picture")
228 ;; (YaTeX:makebox)))
229 (fset 'YaTeX:framebox 'YaTeX:makebox)
231 (defun YaTeX:parbox ()
232 (YaTeX:read-position "tbc"))
233 (defun YaTeX::parbox (argp)
234 (cond
235 ((= argp 1) (read-string "Width: "))
236 ((= argp 2) (read-string "Text: "))))
238 (defun YaTeX::dashbox ()
239 (concat "{" (read-string "Dash dimension: ") "}"
240 (YaTeX:read-coordinates "Dimension")))
242 (defun YaTeX::savebox (argp)
243 (cond
244 ((= argp 1) (read-string "Saved into name: " "\\"))
245 ((= argp 2) (read-string "Text: "))))
247 (defvar YaTeX-minibuffer-quick-map nil)
248 (if YaTeX-minibuffer-quick-map nil
249 (setq YaTeX-minibuffer-quick-map
250 (copy-keymap minibuffer-local-completion-map))
251 (let ((ch (1+ ? )))
252 (while (< ch 127)
253 (define-key YaTeX-minibuffer-quick-map (char-to-string ch)
254 'YaTeX-minibuffer-quick-complete)
255 (setq ch (1+ ch)))))
257 (defvar YaTeX:left-right-delimiters
258 '(("(" . ")") (")" . "(") ("[" . "]") ("]" . "[")
259 ("\\{" . "\\}") ("\\}" . "\\{") ("|") ("\\|")
260 ("\\lfloor" . "\\rfloor") ("\\lceil" . "\\rceil")
261 ("\\langle" . "\\rangle") ("/") (".")
262 ("\\rfloor" . "\\rfloor") ("\\rceil" . "\\lceil")
263 ("\\rangle" . "\\langle") ("\\backslash")
264 ("\\uparrow") ("\\downarrow") ("\\updownarrow") ("\\Updownarrow"))
265 "TeX math delimiter, which can be completed after \\right or \\left.")
267 (defvar YaTeX:left-right-default nil "Default string of YaTeX:right.")
269 (defun YaTeX:left ()
270 (let ((minibuffer-completion-table YaTeX:left-right-delimiters)
271 delimiter (leftp (string= YaTeX-single-command "left")))
272 (setq delimiter
273 (read-from-minibuffer
274 (format "Delimiter%s: "
275 (if YaTeX:left-right-default
276 (format "(default=`%s')" YaTeX:left-right-default)
277 "(SPC for menu)"))
278 nil YaTeX-minibuffer-quick-map))
279 (if (string= "" delimiter) (setq delimiter YaTeX:left-right-default))
280 (setq YaTeX-single-command (if leftp "right" "left")
281 YaTeX:left-right-default
282 (or (cdr (assoc delimiter YaTeX:left-right-delimiters)) delimiter))
283 delimiter))
285 (fset 'YaTeX:right 'YaTeX:left)
287 (defun YaTeX:langle ()
288 (setq YaTeX-single-command "rangle")
289 nil)
291 (defun YaTeX:read-coordinates (&optional mes varX varY)
292 (concat
293 "("
294 (read-string (format "%s %s: " (or mes "Dimension") (or varX "X")))
295 ","
296 (read-string (format "%s %s: " (or mes "Dimension") (or varY "Y")))
297 ")"))
299 (defun YaTeX:itembox ()
300 (concat "{" (read-string "Item heading string: ") "}"))
302 ;;;
303 ;;Sample functions for maketitle-type command.
304 ;;;
305 (defun YaTeX:sum ()
306 "Read range of summation."
307 (YaTeX:check-completion-type 'maketitle)
308 (concat (YaTeX:read-boundary "_") (YaTeX:read-boundary "^")))
310 (fset 'YaTeX:int 'YaTeX:sum)
312 (defun YaTeX:lim ()
313 "Insert limit notation of \\lim."
314 (YaTeX:check-completion-type 'maketitle)
315 (let ((var (read-string "Variable: ")) limit)
316 (if (string= "" var) ""
317 (setq limit (read-string "Limit ($ means infinity): "))
318 (if (string= "$" limit) (setq limit "\\infty"))
319 (concat "_{" var " \\rightarrow " limit "}"))))
321 (defun YaTeX:gcd ()
322 "Add-in function for \\gcd(m,n)."
323 (YaTeX:check-completion-type 'maketitle)
324 (YaTeX:read-coordinates "\\gcd" "(?,)" "(,?)"))
326 (defun YaTeX:read-boundary (ULchar)
327 "Read boundary usage by _ or ^. _ or ^ is indicated by argument ULchar."
328 (let ((bndry (read-string (concat ULchar "{???} ($ for infinity): "))))
329 (if (string= bndry "") ""
330 (if (string= bndry "$") (setq bndry "\\infty"))
331 (concat ULchar "{" bndry "}"))))
333 (defun YaTeX:verb ()
334 "Enclose \\verb's contents with the same characters."
335 (let ((quote-char (read-string "Quoting char: " "|"))
336 (contents (read-string "Quoted contents: ")))
337 (concat quote-char contents quote-char)))
339 (fset 'YaTeX:verb* 'YaTeX:verb)
341 (defun YaTeX:footnotemark ()
342 (setq YaTeX-section-name "footnotetext")
343 nil)
345 (defun YaTeX:cite ()
346 (let ((comment (read-string "Comment for citation: ")))
347 (if (string= comment "") ""
348 (concat "[" comment "]"))))
350 (defun YaTeX:bibitem ()
351 (let ((label (read-string "Citation label for bibitem: ")))
352 (if (string= label "") ""
353 (concat "[" label "]"))))
355 (defun YaTeX:item ()
356 (cond
357 ((eq major-mode 'yatex-mode)
358 (YaTeX-indent-line)
359 (setq YaTeX-section-name "label"))
360 ((eq major-mode 'texinfo-mode)
361 (setq YaTeX-section-name "dots"))) ;??
362 " ")
363 (fset 'YaTeX:item\[\] 'YaTeX:item)
364 (fset 'YaTeX:subitem 'YaTeX:item)
365 (fset 'YaTeX:subsubitem 'YaTeX:item)
367 (defun YaTeX:linebreak ()
368 (let (obl)
369 (message "Break strength 0,1,2,3,4 (default: 4): ")
370 (setq obl (char-to-string (read-char)))
371 (if (string-match "[0-4]" obl)
372 (concat "[" obl "]")
373 "")))
374 (fset 'YaTeX:pagebreak 'YaTeX:linebreak)
376 ;;;
377 ;;Subroutine
378 ;;;
380 (defun YaTeX:check-completion-type (type)
381 "Check valid completion type."
382 (if (not (eq type YaTeX-current-completion-type))
383 (error "This should be completed with %s-type completion." type)))
386 ;;;
387 ;;; [[Add-in functions for reading section arguments]]
388 ;;;
389 ;; All of add-in functions for reading sections arguments should
390 ;; take an argument ARGP that specify the argument position.
391 ;; If argument position is out of range, nil should be returned,
392 ;; else nil should NOT be returned.
394 ;;
395 ; Label selection
396 ;;
397 (defvar YaTeX-label-menu-other
398 (if YaTeX-japan "':他のバッファのラベル\n" "':LABEL IN OTHER BUFFER.\n"))
399 (defvar YaTeX-label-menu-repeat
400 (if YaTeX-japan ".:直前の\\refと同じ\n" "/:REPEAT LAST \ref{}\n"))
401 (defvar YaTeX-label-menu-any
402 (if YaTeX-japan "*:任意の文字列\n" "*:ANY STRING.\n"))
403 (defvar YaTeX-label-buffer "*Label completions*")
404 (defvar YaTeX-label-guide-msg "Select label and hit RETURN.")
405 (defvar YaTeX-label-select-map nil
406 "Key map used in label selection buffer.")
407 (defun YaTeX::label-setup-key-map ()
408 (if YaTeX-label-select-map nil
409 (message "Setting up label selection mode map...")
410 ;(setq YaTeX-label-select-map (copy-keymap global-map))
411 (setq YaTeX-label-select-map (make-keymap))
412 (suppress-keymap YaTeX-label-select-map)
413 (substitute-all-key-definition
414 'previous-line 'YaTeX::label-previous YaTeX-label-select-map)
415 (substitute-all-key-definition
416 'next-line 'YaTeX::label-next YaTeX-label-select-map)
417 (define-key YaTeX-label-select-map "\C-n" 'YaTeX::label-next)
418 (define-key YaTeX-label-select-map "\C-p" 'YaTeX::label-previous)
419 (define-key YaTeX-label-select-map "<" 'beginning-of-buffer)
420 (define-key YaTeX-label-select-map ">" 'end-of-buffer)
421 (define-key YaTeX-label-select-map "\C-m" 'exit-recursive-edit)
422 (define-key YaTeX-label-select-map "\C-j" 'exit-recursive-edit)
423 (define-key YaTeX-label-select-map " " 'exit-recursive-edit)
424 (define-key YaTeX-label-select-map "\C-g" 'abort-recursive-edit)
425 (define-key YaTeX-label-select-map "/" 'isearch-forward)
426 (define-key YaTeX-label-select-map "?" 'isearch-backward)
427 (define-key YaTeX-label-select-map "'" 'YaTeX::label-search-tag)
428 (define-key YaTeX-label-select-map "." 'YaTeX::label-search-tag)
429 (define-key YaTeX-label-select-map "*" 'YaTeX::label-search-tag)
430 (message "Setting up label selection mode map...Done")
431 (let ((key ?A))
432 (while (<= key ?Z)
433 (define-key YaTeX-label-select-map (char-to-string key)
434 'YaTeX::label-search-tag)
435 (define-key YaTeX-label-select-map (char-to-string (+ key (- ?a ?A)))
436 'YaTeX::label-search-tag)
437 (setq key (1+ key))))))
439 (defun YaTeX::label-next ()
440 (interactive) (forward-line 1) (message YaTeX-label-guide-msg))
441 (defun YaTeX::label-previous ()
442 (interactive) (forward-line -1) (message YaTeX-label-guide-msg))
443 (defun YaTeX::label-search-tag ()
444 (interactive)
445 (let ((case-fold-search t)
446 (tag (regexp-quote (char-to-string last-command-char))))
447 (cond
448 ((save-excursion
449 (forward-char 1)
450 (re-search-forward (concat "^" tag) nil t))
451 (goto-char (match-beginning 0)))
452 ((save-excursion
453 (goto-char (point-min))
454 (re-search-forward (concat "^" tag) nil t))
455 (goto-char (match-beginning 0))))
456 (message YaTeX-label-guide-msg)))
458 ; (defun YaTeX::ref (argp &optional labelcmd refcmd)
459 ; (cond
460 ; ((= argp 1)
461 ; (let ((lnum 0) e0 label label-list (buf (current-buffer))
462 ; (labelcmd (or labelcmd "label")) (refcmd (or refcmd "ref"))
463 ; (p (point)) initl line cf)
464 ; (message "Collecting labels...")
465 ; (save-window-excursion
466 ; (YaTeX-showup-buffer
467 ; YaTeX-label-buffer (function (lambda (x) (window-width x))))
468 ; (if (fboundp 'select-frame) (setq cf (selected-frame)))
469 ; (if (eq (window-buffer (minibuffer-window)) buf)
470 ; (progn
471 ; (other-window 1)
472 ; (setq buf (current-buffer))
473 ; (set-buffer buf)
474 ; ;(message "cb=%s" buf)(sit-for 3)
475 ; ))
476 ; (save-excursion
477 ; (set-buffer (get-buffer-create YaTeX-label-buffer))
478 ; (setq buffer-read-only nil)
479 ; (erase-buffer))
480 ; (save-excursion
481 ; (goto-char (point-min))
482 ; (let ((standard-output (get-buffer YaTeX-label-buffer)))
483 ; (princ (format "=== LABELS in [%s] ===\n" (buffer-name buf)))
484 ; (while (YaTeX-re-search-active-forward
485 ; (concat "\\\\" labelcmd "\\b")
486 ; (regexp-quote YaTeX-comment-prefix) nil t)
487 ; (goto-char (match-beginning 0))
488 ; (skip-chars-forward "^{")
489 ; (setq label
490 ; (buffer-substring
491 ; (1+ (point))
492 ; (prog2 (forward-list 1) (setq e0 (1- (point)))))
493 ; label-list (cons label label-list))
494 ; (or initl
495 ; (if (< p (point)) (setq initl lnum)))
496 ; (beginning-of-line)
497 ; (skip-chars-forward " \t\n" nil)
498 ; (princ (format "%c:{%s}\t<<%s>>\n" (+ (% lnum 26) ?A) label
499 ; (buffer-substring (point) (point-end-of-line))))
500 ; (setq lnum (1+ lnum))
501 ; (message "Collecting \\%s{}... %d" labelcmd lnum)
502 ; (goto-char e0))
503 ; (princ YaTeX-label-menu-other)
504 ; (princ YaTeX-label-menu-repeat)
505 ; (princ YaTeX-label-menu-any)
506 ; );standard-output
507 ; (goto-char p)
508 ; (or initl (setq initl lnum))
509 ; (message "Collecting %s...Done" labelcmd)
510 ; (if (fboundp 'select-frame) (select-frame cf))
511 ; (YaTeX-showup-buffer YaTeX-label-buffer nil t)
512 ; (YaTeX::label-setup-key-map)
513 ; (setq truncate-lines t)
514 ; (setq buffer-read-only t)
515 ; (use-local-map YaTeX-label-select-map)
516 ; (message YaTeX-label-guide-msg)
517 ; (goto-line (1+ initl)) ;goto recently defined label line
518 ; (switch-to-buffer (current-buffer))
519 ; (unwind-protect
520 ; (progn
521 ; (recursive-edit)
522 ; (set-buffer (get-buffer YaTeX-label-buffer)) ;assertion
523 ; (beginning-of-line)
524 ; (setq line (1- (count-lines (point-min)(point))))
525 ; (cond
526 ; ((= line -1) (setq label ""))
527 ; ((= line lnum) (setq label (YaTeX-label-other)))
528 ; ((= line (1+ lnum))
529 ; (save-excursion
530 ; (switch-to-buffer buf)
531 ; (goto-char p)
532 ; (if (re-search-backward
533 ; (concat "\\\\" refcmd "{\\([^}]+\\)}") nil t)
534 ; (setq label (YaTeX-match-string 1))
535 ; (setq label ""))))
536 ; ((>= line (+ lnum 2))
537 ; (setq label (read-string (format "\\%s{???}: " refcmd))))
538 ; (t (setq label (nth (- lnum line 1) label-list)))))
539 ; (bury-buffer YaTeX-label-buffer)))
540 ; label)))))
542 (defvar YaTeX-ref-default-label-string "%H%M%S_%d%b%y"
543 "*Default \\ref time string format.
544 This format is like strftime(3) but allowed conversion char are as follows;
545 %y -> Last 2 digit of year, %b -> Month name, %m -> Monthe number(1-12),
546 %d -> Day, %H -> Hour, %M -> Minute, %S -> Second,
547 %qx -> alphabetical-decimal conversion of yymmdd.
548 %qX -> alphabetical-decimal conversion of HHMMSS.
549 Beware defualt label-string should be always unique. So this format string
550 should have both time part (%H+%M+%S or %qX) and date
551 part (%y+(%b|%m)+%d or %qx).")
553 (defun YaTeX::ref-alphabex (n)
554 (let ((alphabex ""))
555 (while (> n 0)
556 (setq alphabex (concat (char-to-string (+ ?a (% n 26))) alphabex)
557 n (/ n 26)))
558 alphabex))
560 (defun YaTeX::ref-default-label (&optional format)
561 "Default auto-genarated label string."
562 ;; We do not use (format-time-string) for emacs-19
563 (let*((ts (substring (current-time-string) 4))
564 (y (substring ts -2))
565 (b (substring ts 0 3))
566 (d (format "%d" (string-to-int (substring ts 4 6))))
567 (H (substring ts 7 9))
568 (M (substring ts 10 12))
569 (S (substring ts 13 15))
570 (HMS (+ (* 10000 (string-to-int H))
571 (* 100 (string-to-int M))
572 (string-to-int S)))
573 (talphabex (YaTeX::ref-alphabex HMS))
574 (mnames "JanFebMarAprMayJunJulAugSepOctNovDec")
575 (m (format "%02d" (/ (string-match b mnames) 3)))
576 (ymd (+ (* 10000 (string-to-int y))
577 (* 100 (string-to-int m))
578 (string-to-int d)))
579 (dalphabex (YaTeX::ref-alphabex ymd)))
580 (YaTeX-replace-formats
581 (or format YaTeX-ref-default-label-string)
582 (list (cons "y" y)
583 (cons "b" b)
584 (cons "m" m)
585 (cons "d" d)
586 (cons "H" H)
587 (cons "M" M)
588 (cons "S" S)
589 (cons "qX" talphabex)
590 (cons "qx" dalphabex)))))
592 (defvar YaTeX-ref-generate-label-function 'YaTeX::ref-generate-label
593 "*Function to generate default label for unnamed \\label{}s.
594 The function pointed to this value should take two arguments.
595 First argument is LaTeX macro's name, second is macro's argument.")
597 (defun YaTeX::ref-generate-label (command arg)
598 "Generate a label string which is unique in current buffer."
599 (let ((default (condition-case nil
600 (YaTeX::ref-default-label)
601 (error (substring (current-time-string) 4)))))
602 (read-string "Give a label for this line: "
603 (if YaTeX-emacs-19 (cons default 1) default))))
605 (defun YaTeX::ref-getset-label (buffer point &optional noset)
606 "Get label string in the BUFFER near the POINT.
607 Make \\label{xx} if no label.
608 If optional third argument NOSET is non-nil, do not generate new label."
609 ;;Here, we rewrite the LaTeX source. Therefore we should be careful
610 ;;to decide the location suitable for \label. Do straightforward!
611 (let (boundary inspoint cc newlabel (labelholder "label") mathp exp1 env
612 (r-escape (regexp-quote YaTeX-comment-prefix))
613 command arg alreadysought foundpoint)
614 (set-buffer buffer)
615 (save-excursion
616 (goto-char point)
617 (setq cc (current-column))
618 (if (= (char-after (point)) ?\\) (forward-char 1))
619 (cond
620 ((looking-at YaTeX-sectioning-regexp)
621 (setq command (YaTeX-match-string 0))
622 (skip-chars-forward "^{")
623 (setq arg (buffer-substring
624 (1+ (point))
625 (progn (forward-list 1) (1- (point)))))
626 (skip-chars-forward " \t\n")
627 ;(setq boundary "[^\\]")
628 (setq inspoint (point))
629 (setq boundary
630 (save-excursion
631 (if (YaTeX-re-search-active-forward
632 (concat YaTeX-ec-regexp
633 "\\(" YaTeX-sectioning-regexp "\\|"
634 "begin\\|item\\)")
635 r-escape nil 1)
636 (match-beginning 0)
637 (1- (point))))))
638 ((looking-at "item\\s ")
639 (setq command "item"
640 cc (+ cc 6))
641 ;(setq boundary (concat YaTeX-ec-regexp "\\(item\\|begin\\|end\\)\\b"))
642 (setq boundary
643 (save-excursion
644 (if (YaTeX-re-search-active-forward
645 (concat YaTeX-ec-regexp "\\(item\\|begin\\|end\\)\\b")
646 r-escape nil 1)
647 (match-beginning 0)
648 (1- (point))))
649 inspoint boundary))
650 ((looking-at "bibitem")
651 (setq labelholder "bibitem" ; label holder is bibitem itself
652 command "bibitem")
653 (setq boundary
654 (save-excursion
655 (if (YaTeX-re-search-active-forward
656 (concat YaTeX-ec-regexp "\\(bibitem\\|end\\)\\b")
657 r-escape nil 1)
658 (match-beginning 0)
659 (1- (point))))
660 inspoint boundary))
661 ((string-match YaTeX::ref-nestable-counter-regexp
662 (setq env (or (YaTeX-inner-environment t) "document")))
663 (let ((curtop (get 'YaTeX-inner-environment 'point))
664 (end (point-max)) label)
665 (skip-chars-forward " \t\n")
666 (setq inspoint (point) ;initial candidate
667 cc (current-column)
668 command env
669 alreadysought t)
670 (if (condition-case nil
671 (progn
672 (goto-char curtop)
673 (YaTeX-goto-corresponding-environment))
674 (error nil))
675 (setq end (point)))
676 (goto-char inspoint)
677 (while (YaTeX-re-search-active-forward
678 (concat YaTeX-ec-regexp "label{\\([^}]+\\)}" )
679 r-escape end t)
680 (setq label (YaTeX-match-string 1))
681 (if (and (equal env (YaTeX-inner-environment t))
682 (= curtop (get 'YaTeX-inner-environment 'point)))
683 ;;I found the label
684 (setq alreadysought label
685 foundpoint (match-end 0))))
686 ))
687 ((string-match YaTeX::ref-mathenv-regexp env) ;env is set in above case
688 (setq command env
689 mathp t
690 exp1 (string-match YaTeX::ref-mathenv-exp1-regexp env))
691 ;;(setq boundary (concat YaTeX-ec-regexp "\\(\\\\\\|end{" env "}\\)"))
692 (setq boundary
693 (save-excursion
694 (if (YaTeX-re-search-active-forward
695 (concat
696 YaTeX-ec-regexp "\\("
697 (if exp1 "" "\\\\\\|")
698 "end{" env "}\\)")
699 r-escape nil 1)
700 (match-beginning 0)
701 (1- (point))))
702 inspoint boundary))
703 ((looking-at "footnote\\s *{")
704 (setq command "footnote")
705 (skip-chars-forward "^{") ;move onto `{'
706 (setq boundary
707 (save-excursion
708 (condition-case err
709 (forward-list 1)
710 (error (error "\\\\footnote at point %s's brace not closed"
711 (point))))
712 (1- (point)))
713 inspoint boundary))
714 ((looking-at "caption\\|\\(begin\\)")
715 (setq command (YaTeX-match-string 0))
716 (skip-chars-forward "^{")
717 ;;;;;;(if (match-beginning 1) (forward-list 1))
718 ;; caption can be treated as mathenv, is it right??
719 (setq arg (buffer-substring
720 (1+ (point))
721 (progn (forward-list 1) (1- (point)))))
722 ;;(setq boundary (concat YaTeX-ec-regexp "\\(begin\\|end\\)\\b"))
723 (setq inspoint (point))
724 (setq boundary
725 (save-excursion
726 (if (YaTeX-re-search-active-forward
727 (concat YaTeX-ec-regexp "\\(begin\\|end\\)\\b")
728 r-escape nil 1)
729 (match-beginning 0)
730 (1- (point))))))
731 (t ))
732 (if (save-excursion (skip-chars-forward " \t") (looking-at "%"))
733 (forward-line 1))
734 (cond
735 ((stringp alreadysought)
736 (put 'YaTeX::ref-getset-label 'foundpoint foundpoint) ;ugly...
737 alreadysought)
738 ((and (null alreadysought)
739 (> boundary (point))
740 (save-excursion
741 (YaTeX-re-search-active-forward
742 ;;(concat "\\(" labelholder "\\)\\|\\(" boundary "\\)")
743 labelholder
744 (regexp-quote YaTeX-comment-prefix)
745 boundary 1))
746 (match-beginning 0))
747 ;; if \label{hoge} found, return it
748 (put 'YaTeX::ref-getset-label 'foundpoint (1- (match-beginning 0)))
749 (buffer-substring
750 (progn
751 (goto-char (match-end 0))
752 (skip-chars-forward "^{") (1+ (point)))
753 (progn
754 (forward-sexp 1) (1- (point)))))
755 ;;else make a label
756 ;(goto-char (match-beginning 0))
757 (noset nil) ;do not set label if noset
758 (t
759 (goto-char inspoint)
760 (skip-chars-backward " \t\n")
761 (save-excursion
762 (setq newlabel
763 (funcall YaTeX-ref-generate-label-function command arg)))
764 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
765 (if mathp nil
766 (insert "\n")
767 (YaTeX-reindent cc))
768 (insert (format "\\label{%s}" newlabel))
769 newlabel)))))
771 (defvar YaTeX::ref-labeling-regexp-alist-default
772 '(("\\\\begin{\\(java\\|program\\)}{\\([^}]+\\)}" . 2)
773 ("\\\\label{\\([^}]+\\)}" . 1))
774 "Alist of labeling regexp vs. its group number points to label string.
775 This alist is used in \\ref's argument's completion.")
776 (defvar YaTeX::ref-labeling-regexp-alist-private nil
777 "*Private extension to YaTeX::ref-labeling-regexp-alist.
778 See the documetation of YaTeX::ref-labeling-regexp-alist.")
779 (defvar YaTeX::ref-labeling-regexp-alist
780 (append YaTeX::ref-labeling-regexp-alist-default
781 YaTeX::ref-labeling-regexp-alist-private))
782 (defvar YaTeX::ref-labeling-regexp
783 (mapconcat 'car YaTeX::ref-labeling-regexp-alist "\\|"))
784 (defvar YaTeX::ref-mathenv-regexp
785 ;; See also YaTeX-ams-math-begin-alist in yatex.el
786 ;; Define only envs which has counter.(without *)
787 "equation\\|eqnarray\\|align\\(at\\)?\\|flalign\\|gather\\|xx?alignat\\|multline")
788 (defvar YaTeX::ref-mathenv-exp1-regexp
789 "\\(equation\\|multline\\)\\b"
790 "*Regexp of math-envname which has only one math-expression.")
791 (defvar YaTeX::ref-enumerateenv-regexp
792 "enumerate")
793 (defvar YaTeX::ref-nestable-counter-regexp
794 "subequations")
796 (defvar YaTeX::ref-labeling-section-level 2
797 "*ref補完で収集するセクショニングコマンドの下限レベル
798 YaTeX-sectioning-levelの数値で指定.")
800 (defun YaTeX::ref (argp &optional labelcmd refcmd predf)
801 (setplist 'YaTeX::ref-labeling-regexp nil) ;erase memory cache
802 (require 'yatexsec)
803 (cond
804 ((= argp 1)
805 (let*((lnum 0) m0 e0 x cmd label match-point point-list boundary
806 (buf (current-buffer))
807 (llv YaTeX::ref-labeling-section-level)
808 (mathenvs YaTeX::ref-mathenv-regexp) envname endrx
809 (enums YaTeX::ref-enumerateenv-regexp)
810 (counter
811 (or labelcmd
812 (concat
813 YaTeX-ec-regexp "\\(\\("
814 (mapconcat
815 'concat
816 (delq nil
817 (mapcar
818 (function
819 (lambda (s)
820 (if (>= llv (cdr s))
821 (car s))))
822 YaTeX-sectioning-level))
823 "\\|")
824 "\\|caption\\(\\[[^]]+\\]\\)?\\|footnote\\){"
825 "\\|\\(begin{\\(" mathenvs "\\|" enums "\\)}\\)"
826 (if YaTeX-use-AMS-LaTeX
827 (concat
828 "\\|\\(begin{"
829 YaTeX::ref-nestable-counter-regexp "}\\)"))
830 "\\)")))
831 (regexp (concat "\\(" counter
832 "\\)\\|\\(" YaTeX::ref-labeling-regexp "\\)"))
833 (itemsep (concat YaTeX-ec-regexp
834 "\\(\\(bib\\)?item\\|begin\\|end\\)"))
835 (refcmd (or refcmd "\\(page\\)?ref"))
836 (p (point)) initl line cf
837 (percent (regexp-quote YaTeX-comment-prefix))
838 (output
839 (function
840 (lambda (label p)
841 (while (setq x (string-match "[\n\t]" label))
842 (aset label x ? ))
843 (while (setq x (string-match " +" label))
844 (setq label (concat
845 (substring label 0 (1+ (match-beginning 0)))
846 (substring label (match-end 0)))))
847 (princ (format "%c: <<%s>>\n" (+ (% lnum 26) ?A) label))
848 (setq point-list (cons p point-list))
849 (message "Collecting labels... %d" lnum)
850 (setq lnum (1+ lnum)))))
851 (me (if (boundp 'me) me 'YaTeX::ref))
852 )
853 (message "Collecting labels...")
854 (save-window-excursion
855 (YaTeX-showup-buffer
856 YaTeX-label-buffer (function (lambda (x) (window-width x))))
857 (if (fboundp 'select-frame) (setq cf (selected-frame)))
858 (if (eq (window-buffer (minibuffer-window)) buf)
859 (progn
860 (other-window 1)
861 (setq buf (current-buffer))
862 (set-buffer buf)))
863 (save-excursion
864 (set-buffer (get-buffer-create YaTeX-label-buffer))
865 (condition-case ()
866 (if (and YaTeX-use-font-lock (fboundp 'font-lock-mode))
867 (font-lock-mode 1))
868 (error nil))
869 (setq buffer-read-only nil)
870 (erase-buffer))
871 (save-excursion
872 (set-buffer buf)
873 (goto-char (point-min))
874 (let ((standard-output (get-buffer YaTeX-label-buffer)) existlabel)
875 (princ (format "=== LABELS in [%s] ===\n" (buffer-name buf)))
876 (while (YaTeX-re-search-active-forward
877 regexp ;;counter
878 percent nil t)
879 ;(goto-char (match-beginning 0))
880 (setq e0 (match-end 0))
881 (cond
882 ;;
883 ;;2005/10/21 Skip it if predicate function returns nil
884 ((and predf
885 (let ((md (match-data)))
886 (prog1
887 (condition-case nil
888 (not (funcall predf))
889 (error nil))
890 (store-match-data md)))))
891 ((YaTeX-literal-p) nil)
892 ((YaTeX-match-string 1)
893 ;;if standard counter commands found
894 (setq cmd (YaTeX-match-string 2)
895 m0 (match-beginning 0))
896 (setq match-point (match-beginning 0))
897 (or initl
898 (if (< p (point)) (setq initl lnum)))
899 (cond
900 ;; In any case, variables e0 should be set
901 ((and YaTeX-use-AMS-LaTeX
902 (string-match YaTeX::ref-nestable-counter-regexp cmd))
903 (let (label)
904 (skip-chars-forward "}")
905 (setq label (buffer-substring
906 (point) (min (+ 80 (point)) (point-max))))
907 ;; to skip (maybe)auto-generated comment
908 (skip-chars-forward " \t")
909 (if (looking-at YaTeX-comment-prefix)
910 (forward-line 1))
911 (setq e0 (point))
912 (skip-chars-forward " \t\n")
913 (if (looking-at "\\\\label{\\([^}]+\\)}")
914 (setq label (format "(labe:%s)" (YaTeX-match-string 1))
915 e0 (match-end 1)))
916 (funcall output (format "--subequation--%s" label) e0)))
917 ((string-match mathenvs cmd) ;;if matches mathematical env
918 ;(skip-chars-forward "} \t\n")
919 ;(forward-line 1) ;2004/1/25
920 (skip-chars-forward "}")
921 (setq x (point)
922 envname (substring
923 cmd (match-beginning 0) (match-end 0)))
924 (save-restriction
925 (narrow-to-region
926 m0
927 (save-excursion
928 (YaTeX-re-search-active-forward
929 (setq endrx (format "%send{%s}" YaTeX-ec-regexp
930 (regexp-quote envname)))
931 percent nil t)))
932 (catch 'scan
933 (while (YaTeX-re-search-active-forward
934 (concat
935 "\\\\end{\\(" (regexp-quote envname) "\\)";;(1)
936 (if YaTeX-use-AMS-LaTeX
937 "\\|\\\\\\(notag\\)") ;;2
938 (if (string-match
939 YaTeX::ref-mathenv-exp1-regexp cmd)
940 "" "\\|\\\\\\\\$")
941 )
942 percent nil t)
943 (let*((quit (match-beginning 1))
944 (notag (match-beginning 2))
945 (label ".......................") l2
946 (e (point)) (m0 (match-beginning 0))
947 (ln (YaTeX-string-width label)))
948 (cond
949 (notag
950 (YaTeX-re-search-active-forward
951 "\\\\\\\\" percent nil 1)
952 (setq x (point)))
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 ((= 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 (key (key-description (where-is-internal 'yank nil t)))
1244 (msg
1245 (if YaTeX-japan
1246 "をkill-ringに入れました。yank(%s)で取り出せます。"
1247 " is stored into kill-ring. Paste it by yank(%s).")))
1248 (kill-new refstr)
1249 (and chmode
1250 (not (equal old label))
1251 (YaTeX::label-rename-refs old label))
1252 (message (concat "`%s'" msg) refstr key)))
1253 label))))
1256 (fset 'YaTeX::pageref 'YaTeX::ref)
1257 (defun YaTeX::tabref (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 "table")))))
1263 (defun YaTeX::figref (argp) ; For the style file of IPSJ journal
1264 (YaTeX::ref
1265 argp nil nil
1266 (function
1267 (lambda ()
1268 (YaTeX-quick-in-environment-p "figure")))))
1269 (defun YaTeX::eqref (argp)
1270 (YaTeX::ref
1271 argp nil nil
1272 (function
1273 (lambda ()
1274 (YaTeX-in-math-mode-p)))))
1276 (defun YaTeX::cite-collect-bibs-external (bibptn &rest files)
1277 "Collect bibentry from FILES(variable length argument) ;
1278 and print them to standard output."
1279 ;;Thanks; http://icarus.ilcs.hokudai.ac.jp/comp/biblio.html
1280 (let*((tb (get-buffer-create " *bibtmp*"))
1281 (bibitemsep "^\\s *@[A-Za-z]")
1282 (target (if (string< "" bibptn) bibptn bibitemsep))
1283 (checkrx (concat "\\(" bibptn "\\)\\|" bibitemsep))
1284 beg
1285 (searchnext
1286 (if (string< "" bibptn)
1287 (function
1288 (lambda()
1289 (setq beg (point))
1290 (and
1291 (prog1
1292 (re-search-forward target nil t)
1293 (end-of-line))
1294 (re-search-backward bibitemsep beg t))))
1295 (function
1296 (lambda()
1297 (re-search-forward target nil t)))))
1299 (save-excursion
1300 (set-buffer tb)
1301 (princ (format "%sbegin{thebibliography}\n" YaTeX-ec))
1302 (while files
1303 (erase-buffer)
1304 (cond
1305 ((file-exists-p (car files))
1306 (insert-file-contents (car files)))
1307 ((file-exists-p (concat (car files) ".bib"))
1308 (insert-file-contents (concat (car files) ".bib"))))
1309 (save-excursion
1310 (goto-char (point-min))
1311 (while (funcall searchnext)
1312 (skip-chars-forward "^{,")
1313 (setq beg (point))
1314 (if (= (char-after (point)) ?{)
1315 (princ (format "%sbibitem{%s}%s\n"
1316 YaTeX-ec
1317 (buffer-substring
1318 (1+ (point))
1319 (progn (skip-chars-forward "^,\n")
1320 (point)))
1321 (mapconcat
1322 (function
1323 (lambda (kwd)
1324 (goto-char beg)
1325 (if (re-search-forward
1326 (concat kwd "\\s *=") nil t)
1327 (buffer-substring
1328 (progn
1329 (goto-char (match-end 0))
1330 (skip-chars-forward " \t\n")
1331 (point))
1332 (progn
1333 (if (looking-at "[{\"]")
1334 (forward-sexp 1)
1335 (forward-char 1)
1336 (skip-chars-forward "^,}"))
1337 (point))))))
1338 '("author" "year" "title" )
1339 ";"))))
1340 (and (re-search-forward bibitemsep nil t)
1341 (forward-line -1))))
1342 (setq files (cdr files)))
1343 (princ (format "%sbegin{thebibliography}\n" YaTeX-ec)))))
1345 (defvar YaTeX::cite-bibitem-macro-regexp "bibitem\\|harvarditem"
1346 "*Regexp of macro name of bibitem definition")
1348 (defun YaTeX::cite-collect-bibs-internal (bibptn)
1349 "Collect bibentry in the current buffer and print them to standard output."
1350 (let ((ptn (concat YaTeX-ec-regexp
1351 "\\(" YaTeX::cite-bibitem-macro-regexp "\\)\\b"))
1352 (lim (concat YaTeX-ec-regexp
1353 "\\(" YaTeX::cite-bibitem-macro-regexp "\\b\\)"
1354 "\\|\\(end{\\)"))
1355 (pcnt (regexp-quote YaTeX-comment-prefix)))
1356 ;; Using bibptn not yet implemented.
1357 ;; Do you need it?? 2005/11/22
1358 (save-excursion
1359 (while (YaTeX-re-search-active-forward ptn pcnt nil t)
1360 (skip-chars-forward "^{\n")
1361 (or (eolp)
1362 (princ (format "%sbibitem%s %s\n"
1363 YaTeX-ec
1364 (buffer-substring
1365 (point)
1366 (progn (forward-sexp 1) (point)))
1367 (buffer-substring
1368 (progn (skip-chars-forward "\n \t") (point))
1369 (save-excursion
1370 (if (YaTeX-re-search-active-forward
1371 lim pcnt nil t)
1372 (progn
1373 (goto-char (match-beginning 0))
1374 (skip-chars-backward "\n \t")
1375 (point))
1376 (point-end-of-line)))))))))))
1378 (defun YaTeX::cite (argp &rest dummy)
1379 (cond
1380 ((eq argp 1)
1381 (let* ((cb (current-buffer))
1382 (f (file-name-nondirectory buffer-file-name))
1383 (d default-directory)
1384 (hilit-auto-highlight nil)
1385 (pcnt (regexp-quote YaTeX-comment-prefix))
1386 (bibrx (concat YaTeX-ec-regexp "bibliography{\\([^}]+\\)}"))
1387 (bibptn (read-string "Pattern: "))
1388 (bbuf (get-buffer-create " *bibitems*"))
1389 (standard-output bbuf)
1390 (me 'YaTeX::cite) ;shuld set this for using YaTeX::ref
1391 bibs files)
1392 (set-buffer bbuf)(erase-buffer)(set-buffer cb)
1393 (save-excursion
1394 (goto-char (point-min))
1395 ;;(1)search external bibdata
1396 (while (YaTeX-re-search-active-forward bibrx pcnt nil t)
1397 (apply 'YaTeX::cite-collect-bibs-external
1398 bibptn
1399 (YaTeX-split-string
1400 (YaTeX-match-string 1) ",")))
1401 ;;(2)search direct \bibitem usage
1402 (YaTeX::cite-collect-bibs-internal bibptn)
1403 (if (progn
1404 (YaTeX-visit-main t)
1405 (not (eq (current-buffer) cb)))
1406 (save-excursion
1407 (goto-char (point-min))
1408 ;;(1)search external bibdata
1409 (while (YaTeX-re-search-active-forward bibrx pcnt nil t)
1410 (apply 'YaTeX::cite-collect-bibs-external
1411 bibptn
1412 (YaTeX-split-string
1413 (YaTeX-match-string 1) ",")))
1414 ;;(2)search internal
1415 (YaTeX::cite-collect-bibs-internal bibptn)))
1416 ;;Now bbuf holds the list of bibitem
1417 (set-buffer bbuf)
1418 ;;;(switch-to-buffer bbuf)
1419 (if (fboundp 'font-lock-fontify-buffer) (font-lock-fontify-buffer))
1420 (YaTeX::ref
1421 argp
1422 (concat "\\\\\\("
1423 YaTeX::cite-bibitem-macro-regexp
1424 "\\)\\(\\[.*\\]\\)?")
1425 "cite"))))
1427 (t nil)))
1429 (defun YaTeX::bibitem (argp)
1430 "Add-in function to insert argument of \\bibitem."
1431 (YaTeX::label argp "label" "cite"))
1433 ;;; for Harvard citation style
1434 (fset 'YaTeX::citeasnoun 'YaTeX::cite)
1435 (fset 'YaTeX::possessivecite 'YaTeX::cite)
1436 (fset 'YaTeX::citeyear 'YaTeX::cite)
1437 (fset 'YaTeX::citename 'YaTeX::cite)
1438 (fset 'YaTeX::citep 'YaTeX::cite)
1439 (fset 'YaTeX::citet 'YaTeX::cite)
1441 (defun YaTeX-select-other-yatex-buffer ()
1442 "Select buffer from all yatex-mode's buffers interactivelly."
1443 (interactive)
1444 (let ((lbuf "*YaTeX mode buffers*") (blist (YaTeX-yatex-buffer-list))
1445 (lnum -1) buf rv
1446 (ff "**find-file**"))
1447 (YaTeX-showup-buffer
1448 lbuf (function (lambda (x) 1))) ;;Select next window surely.
1449 (save-excursion
1450 (set-buffer (get-buffer lbuf))
1451 (setq buffer-read-only nil)
1452 (erase-buffer))
1453 (let ((standard-output (get-buffer lbuf)))
1454 (while blist
1455 (princ
1456 (format "%c:{%s}\n" (+ (% (setq lnum (1+ lnum)) 26) ?A)
1457 (buffer-name (car blist))))
1458 (setq blist (cdr blist)))
1459 (princ (format "':{%s}" ff)))
1460 (YaTeX-showup-buffer lbuf nil t)
1461 (YaTeX::label-setup-key-map)
1462 (setq buffer-read-only t)
1463 (use-local-map YaTeX-label-select-map)
1464 (message YaTeX-label-guide-msg)
1465 (unwind-protect
1466 (progn
1467 (recursive-edit)
1468 (set-buffer lbuf)
1469 (beginning-of-line)
1470 (setq rv
1471 (if (re-search-forward "{\\([^\\}]+\\)}" (point-end-of-line) t)
1472 (buffer-substring (match-beginning 1) (match-end 1)) nil)))
1473 (kill-buffer lbuf))
1474 (if (string= rv ff)
1475 (progn
1476 (call-interactively 'find-file)
1477 (current-buffer))
1478 rv)))
1480 (defun YaTeX-label-other ()
1481 (let ((rv (YaTeX-select-other-yatex-buffer)))
1482 (cond
1483 ((null rv) "")
1484 (t
1485 (set-buffer rv)
1486 (funcall me argp labelcmd refcmd)))))
1488 ;;
1489 ; completion for the arguments of \newcommand
1490 ;;
1491 (defun YaTeX::newcommand (&optional argp)
1492 (cond
1493 ((= argp 1)
1494 (let ((command (read-string "Define newcommand: " "\\")))
1495 (put 'YaTeX::newcommand 'command (substring command 1))
1496 command))
1497 ((= argp 2)
1498 (let ((argc
1499 (string-to-int (read-string "Number of arguments(Default 0): ")))
1500 (def (read-string "Definition: "))
1501 (command (get 'YaTeX::newcommand 'command)))
1502 ;;!!! It's illegal to insert string in the add-in function !!!
1503 (if (> argc 0) (insert (format "[%d]" argc)))
1504 (if (and (stringp command)
1505 (string< "" command)
1506 (y-or-n-p "Update dictionary?"))
1507 (cond
1508 ((= argc 0)
1509 (YaTeX-update-table
1510 (list command)
1511 'singlecmd-table 'user-singlecmd-table 'tmp-singlecmd-table))
1512 ((= argc 1)
1513 (YaTeX-update-table
1514 (list command)
1515 'section-table 'user-section-table 'tmp-section-table))
1516 (t (YaTeX-update-table
1517 (list command argc)
1518 'section-table 'user-section-table 'tmp-section-table))))
1519 (message "")
1520 def ;return command name
1521 ))
1522 (t "")))
1524 (defun YaTeX::newcounter (&optional argp)
1525 (cond
1526 ((= argp 1)
1527 (read-string "New counter name: "))
1528 (t "")))
1530 ;;
1531 ; completion for the arguments of \pagestyle
1532 ;;
1533 (defun YaTeX::pagestyle (&optional argp)
1534 "Read the pagestyle with completion."
1535 (completing-read
1536 "Page style: "
1537 '(("plain") ("empty") ("headings") ("myheadings") ("normal") nil)))
1539 (fset 'YaTeX::thispagestyle 'YaTeX::pagestyle)
1541 ;;
1542 ; completion for the arguments of \pagenumbering
1543 ;;
1544 (defun YaTeX::pagenumbering (&optional argp)
1545 "Read the numbering style."
1546 (completing-read
1547 "Page numbering style: "
1548 '(("arabic") ("Alpha") ("alpha") ("Roman") ("roman"))))
1550 ;;
1551 ; Length
1552 ;;
1553 (defvar YaTeX:style-parameters-default
1554 '(("\\arraycolsep")
1555 ("\\arrayrulewidth")
1556 ("\\baselineskip")
1557 ("\\columnsep")
1558 ("\\columnseprule")
1559 ("\\doublerulesep")
1560 ("\\evensidemargin")
1561 ("\\footheight")
1562 ("\\footskip")
1563 ("\\headheight")
1564 ("\\headsep")
1565 ("\\itemindent")
1566 ("\\itemsep")
1567 ("\\labelsep")
1568 ("\\labelwidth")
1569 ("\\leftmargin")
1570 ("\\linewidth")
1571 ("\\listparindent")
1572 ("\\marginparsep")
1573 ("\\marginparwidth")
1574 ("\\mathindent")
1575 ("\\oddsidemargin")
1576 ("\\parindent")
1577 ("\\parsep")
1578 ("\\parskip")
1579 ("\\partopsep")
1580 ("\\rightmargin")
1581 ("\\tabcolsep")
1582 ("\\textheight")
1583 ("\\textwidth")
1584 ("\\topmargin")
1585 ("\\topsep")
1586 ("\\topskip")
1588 "Alist of LaTeX style parameters.")
1589 (defvar YaTeX:style-parameters-private nil
1590 "*User definable alist of style parameters.")
1591 (defvar YaTeX:style-parameters-local nil
1592 "*User definable alist of local style parameters.")
1594 (defvar YaTeX:length-history nil "Holds history of length.")
1595 (put 'YaTeX:length-history 'no-default t)
1596 (defun YaTeX::setlength (&optional argp)
1597 "YaTeX add-in function for arguments of \\setlength."
1598 (cond
1599 ((equal 1 argp)
1600 ;;(completing-read "Length variable: " YaTeX:style-parameters nil nil "\\")
1601 (YaTeX-cplread-with-learning
1602 "Length variable: "
1603 'YaTeX:style-parameters-default
1604 'YaTeX:style-parameters-private
1605 'YaTeX:style-parameters-local
1606 nil nil "\\")
1608 ((equal 2 argp)
1609 (read-string-with-history "Length: " nil 'YaTeX:length-history))))
1611 (fset 'YaTeX::addtolength 'YaTeX::setlength)
1613 (defun YaTeX::settowidth (&optional argp)
1614 "YaTeX add-in function for arguments of \\settowidth."
1615 (cond
1616 ((equal 1 argp)
1617 (YaTeX-cplread-with-learning
1618 "Length variable: "
1619 'YaTeX:style-parameters-default
1620 'YaTeX:style-parameters-private
1621 'YaTeX:style-parameters-local
1622 nil nil "\\"))
1623 ((equal 2 argp)
1624 (read-string "Text: "))))
1626 (defun YaTeX::newlength (&optional argp)
1627 "YaTeX add-in function for arguments of \\newlength"
1628 (cond
1629 ((equal argp 1)
1630 (let ((length (read-string "Length variable: " "\\")))
1631 (if (string< "" length)
1632 (YaTeX-update-table
1633 (list length)
1634 'YaTeX:style-parameters-default
1635 'YaTeX:style-parameters-private
1636 'YaTeX:style-parameters-local))
1637 length))))
1639 ;; \multicolumn's arguments
1640 (defun YaTeX::multicolumn (&optional argp)
1641 "YaTeX add-in function for arguments of \\multicolumn."
1642 (cond
1643 ((equal 1 argp)
1644 (read-string "Number of columns: "))
1645 ((equal 2 argp)
1646 (YaTeX:read-oneof "|lrc" nil t))
1647 ((equal 3 argp)
1648 (read-string "Item: "))))
1650 (defvar YaTeX:documentstyles-default
1651 '(("article") ("jarticle") ("j-article")
1652 ("book") ("jbook") ("j-book")
1653 ("report") ("jreport") ("j-report")
1654 ("letter") ("ascjletter"))
1655 "List of LaTeX documentstyles.")
1656 (defvar YaTeX:documentstyles-private nil
1657 "*User defined list of LaTeX documentstyles.")
1658 (defvar YaTeX:documentstyles-local nil
1659 "*User defined list of local LaTeX documentstyles.")
1660 (defvar YaTeX:documentstyle-options-default
1661 '(("a4j") ("a5j") ("b4j") ("b5j")
1662 ("twocolumn") ("jtwocolumn") ("epsf") ("epsfig") ("epsbox") ("nfig"))
1663 "List of LaTeX documentstyle options.")
1664 (defvar YaTeX:documentstyle-options-private nil
1665 "*User defined list of LaTeX documentstyle options.")
1666 (defvar YaTeX:documentstyle-options-local nil
1667 "List of LaTeX local documentstyle options.")
1669 (defun YaTeX:documentstyle ()
1670 (let*((delim ",")
1671 (dt (append YaTeX:documentstyle-options-local
1672 YaTeX:documentstyle-options-private
1673 YaTeX:documentstyle-options-default))
1674 (minibuffer-completion-table dt)
1675 (opt (read-from-minibuffer
1676 "Style options ([opt1,opt2,...]): "
1677 nil YaTeX-minibuffer-completion-map nil))
1678 (substr opt) o)
1679 (if (string< "" opt)
1680 (progn
1681 (while substr
1682 (setq o (substring substr 0 (string-match delim substr)))
1683 (or (assoc o dt)
1684 (YaTeX-update-table
1685 (list o)
1686 'YaTeX:documentstyle-options-default
1687 'YaTeX:documentstyle-options-private
1688 'YaTeX:documentstyle-options-local))
1689 (setq substr
1690 (if (string-match delim substr)
1691 (substring substr (1+ (string-match delim substr))))))
1692 (concat "[" opt "]"))
1693 "")))
1695 (defun YaTeX::documentstyle (&optional argp)
1696 "YaTeX add-in function for arguments of \\documentstyle."
1697 (cond
1698 ((equal argp 1)
1699 (setq YaTeX-env-name "document")
1700 (let ((sname
1701 (YaTeX-cplread-with-learning
1702 (format "Documentstyle (default %s): "
1703 YaTeX-default-document-style)
1704 'YaTeX:documentstyles-default
1705 'YaTeX:documentstyles-private
1706 'YaTeX:documentstyles-local)))
1707 (if (string= "" sname) (setq sname YaTeX-default-document-style))
1708 (setq YaTeX-default-document-style sname)))))
1710 (defun YaTeX::include (argp &optional prompt)
1711 "Read file name setting default directory to that of main file."
1712 (cond
1713 ((= argp 1)
1714 (save-excursion
1715 (YaTeX-visit-main t)
1716 (let*((insert-default-directory)
1717 (file (read-file-name (or prompt "Input file: ") "")))
1718 (setq file (substring file 0 (string-match "\\.tex$" file))))))))
1720 (fset 'YaTeX::input 'YaTeX::include)
1723 ;;; -------------------- LaTeX2e stuff --------------------
1724 (defvar YaTeX:documentclass-options-default
1725 '(("a4paper") ("a5paper") ("b4paper") ("b5paper") ("10pt") ("11pt") ("12pt")
1726 ("latterpaper") ("legalpaper") ("executivepaper") ("landscape")
1727 ("oneside") ("twoside") ("draft") ("final") ("leqno") ("fleqn") ("openbib")
1728 ("tombow") ("titlepage") ("notitlepage") ("dvips")
1729 ("mingoth") ;for jsarticle
1730 ("clock") ;for slides class only
1732 "Default options list for documentclass")
1733 (defvar YaTeX:documentclass-options-private nil
1734 "*User defined options list for documentclass")
1735 (defvar YaTeX:documentclass-options-local nil
1736 "*User defined options list for local documentclass")
1738 (defun YaTeX:documentclass ()
1739 (let*((delim ",")
1740 (dt (append YaTeX:documentclass-options-local
1741 YaTeX:documentclass-options-private
1742 YaTeX:documentclass-options-default))
1743 (minibuffer-completion-table dt)
1744 (opt (read-from-minibuffer
1745 "Documentclass options ([opt1,opt2,...]): "
1746 nil YaTeX-minibuffer-completion-map nil))
1747 (substr opt) o)
1748 (if (string< "" opt)
1749 (progn
1750 (while substr
1752 (setq o (substring substr 0 (string-match delim substr)))
1753 (or (assoc o dt)
1754 (YaTeX-update-table
1755 (list o)
1756 'YaTeX:documentclass-options-default
1757 'YaTeX:documentclass-options-private
1758 'YaTeX:documentclass-options-local))
1759 (setq substr
1760 (if (string-match delim substr)
1761 (substring substr (1+ (string-match delim substr))))))
1762 (concat "[" opt "]"))
1763 "")))
1765 (defvar YaTeX:documentclasses-default
1766 '(("article") ("jarticle") ("report") ("jreport") ("book") ("jbook")
1767 ("jsarticle") ("jsbook")
1768 ("j-article") ("j-report") ("j-book")
1769 ("letter") ("slides") ("ltxdoc") ("ltxguide") ("ltnews") ("proc"))
1770 "Default documentclass alist")
1771 (defvar YaTeX:documentclasses-private nil
1772 "*User defined documentclass alist")
1773 (defvar YaTeX:documentclasses-local nil
1774 "*User defined local documentclass alist")
1775 (defvar YaTeX-default-documentclass (if YaTeX-japan "jarticle" "article")
1776 "*Default documentclass")
1778 (defun YaTeX::documentclass (&optional argp)
1779 (cond
1780 ((equal argp 1)
1781 (setq YaTeX-env-name "document")
1782 (let ((sname
1783 (YaTeX-cplread-with-learning
1784 (format "Documentclass (default %s): " YaTeX-default-documentclass)
1785 'YaTeX:documentclasses-default
1786 'YaTeX:documentclasses-private
1787 'YaTeX:documentclasses-local)))
1788 (if (string= "" sname) (setq sname YaTeX-default-documentclass))
1789 (setq YaTeX-default-documentclass sname)))))
1791 (defvar YaTeX:latex2e-named-color-alist
1792 '(("GreenYellow") ("Yellow") ("Goldenrod") ("Dandelion") ("Apricot")
1793 ("Peach") ("Melon") ("YellowOrange") ("Orange") ("BurntOrange")
1794 ("Bittersweet") ("RedOrange") ("Mahogany") ("Maroon") ("BrickRed")
1795 ("Red") ("OrangeRed") ("RubineRed") ("WildStrawberry") ("Salmon")
1796 ("CarnationPink") ("Magenta") ("VioletRed") ("Rhodamine") ("Mulberry")
1797 ("RedViolet") ("Fuchsia") ("Lavender") ("Thistle") ("Orchid")("DarkOrchid")
1798 ("Purple") ("Plum") ("Violet") ("RoyalPurple") ("BlueViolet")
1799 ("Periwinkle") ("CadetBlue") ("CornflowerBlue") ("MidnightBlue")
1800 ("NavyBlue") ("RoyalBlue") ("Blue") ("Cerulean") ("Cyan") ("ProcessBlue")
1801 ("SkyBlue") ("Turquoise") ("TealBlue") ("Aquamarine") ("BlueGreen")
1802 ("Emerald") ("JungleGreen") ("SeaGreen") ("Green") ("ForestGreen")
1803 ("PineGreen") ("LimeGreen") ("YellowGreen") ("SpringGreen") ("OliveGreen")
1804 ("RawSienna") ("Sepia") ("Brown") ("Tan") ("Gray") ("Black") ("White"))
1805 "Colors defined in $TEXMF/tex/plain/dvips/colordvi.tex")
1807 (defvar YaTeX:latex2e-basic-color-alist
1808 '(("black") ("white") ("red") ("blue") ("yellow") ("green") ("cyan")
1809 ("magenta"))
1810 "Basic colors")
1812 (defun YaTeX:textcolor ()
1813 "Add-in for \\color's option"
1814 (if (y-or-n-p "Use `named' color? ")
1815 "[named]"))
1817 (defun YaTeX::color-completing-read (prompt)
1818 (let ((completion-ignore-case t)
1819 (namedp (save-excursion
1820 (skip-chars-backward "^\n\\[\\\\")
1821 (looking-at "named"))))
1822 (completing-read
1823 prompt
1824 (if namedp
1825 YaTeX:latex2e-named-color-alist
1826 YaTeX:latex2e-basic-color-alist)
1827 nil t)))
1829 (defun YaTeX::textcolor (argp)
1830 "Add-in for \\color's argument"
1831 (cond
1832 ((= argp 1) (YaTeX::color-completing-read "Color: "))
1833 ((= argp 2) (read-string "Colored string: "))))
1835 (fset 'YaTeX:color 'YaTeX:textcolor)
1836 (fset 'YaTeX::color 'YaTeX::textcolor)
1837 (fset 'YaTeX:colorbox 'YaTeX:textcolor)
1838 (fset 'YaTeX::colorbox 'YaTeX::textcolor)
1839 (fset 'YaTeX:fcolorbox 'YaTeX:textcolor)
1840 (fset 'YaTeX:pagecolor 'YaTeX:textcolor)
1841 (fset 'YaTeX::pagecolor 'YaTeX::textcolor)
1843 (defun YaTeX::fcolorbox (argp)
1844 (cond
1845 ((= argp 1) (YaTeX::color-completing-read "Frame color: "))
1846 ((= argp 2) (YaTeX::color-completing-read "Inner color: "))
1847 ((= argp 3) (read-string "Colored string: "))))
1849 (defun YaTeX:scalebox ()
1850 "Add-in for \\scalebox"
1851 (let ((vmag (read-string
1852 (if YaTeX-japan "倍率(負で反転): "
1853 "Magnification(Negative for flipped): ")))
1854 (hmag (read-string (if YaTeX-japan "縦倍率(省略可): "
1855 "Vertical magnification(Optional): "))))
1856 (if (and hmag (string< "" hmag))
1857 (format "{%s}[%s]" vmag hmag)
1858 (format "{%s}" vmag))))
1860 (defun YaTeX:rotatebox ()
1861 "Optional argument add-in for \\rotatebox"
1862 (message "Rotate origin? (N)one (O)rigin (X)-Y: ")
1863 (let ((c (read-char)) r (defx "x=mm") x (defy "y=mm") y something)
1864 (cond
1865 ((memq c '(?O ?o))
1866 (if (string< "" (setq r (YaTeX:read-oneof "htbpB")))
1867 (concat "[origin=" r "]")))
1868 ((memq c '(?X ?x ?Y ?y))
1869 (setq r (read-string "" (if YaTeX-emacs-19 (cons defx 3) defx))
1870 x (if (string< "x=" r) r)
1871 r (read-string "" (if YaTeX-emacs-19 (cons defy 3) defy))
1872 y (if (string< "y=" r) r)
1873 something (or x y))
1874 (format "%s%s%s%s%s"
1875 (if something "[" "")
1876 (if x x "")
1877 (if (and x y) "," "")
1878 (if y y "")
1879 (if something "]" ""))))))
1881 (defun YaTeX::rotatebox (argp)
1882 "Argument add-in for \\rotatebox"
1883 (cond
1884 ((= argp 1)
1885 (read-string (if YaTeX-japan "回転角(度; 左回り): "
1886 "Angle in degree(unclockwise): ")))
1887 ((= argp 2)
1888 (read-string (if YaTeX-japan "テキスト: " "Text: ")))))
1890 (defun YaTeX:includegraphics ()
1891 "Add-in for \\includegraphics's option"
1892 (let (width height (scale "") angle str)
1893 (setq width (read-string "Width: ")
1894 height (read-string "Height: "))
1895 (or (string< width "") (string< "" height)
1896 (setq scale (read-string "Scale: ")))
1897 (setq angle (read-string "Angle(0-359): "))
1898 (setq str
1899 (mapconcat
1900 'concat
1901 (delq nil
1902 (mapcar '(lambda (s)
1903 (and (stringp (symbol-value s))
1904 (string< "" (symbol-value s))
1905 (format "%s=%s" s (symbol-value s))))
1906 '(width height scale angle)))
1907 ","))
1908 (if (string= "" str) ""
1909 (concat "[" str "]"))))
1911 (defun YaTeX::includegraphics (argp)
1912 "Add-in for \\includegraphics"
1913 (YaTeX::include argp "Image File: "))
1915 (defun YaTeX::verbfile (argp)
1916 "Add-in for \\verbfile"
1917 (YaTeX::include argp "Virbatim File: "))
1919 (defun YaTeX:caption ()
1920 (setq YaTeX-section-name "label")
1921 nil)
1924 (defvar YaTeX::usepackage-alist-default
1925 '(("version") ("plext") ("url") ("fancybox") ("pifont") ("longtable")
1926 ("ascmac") ("bm") ("graphics") ("graphicx") ("alltt") ("misc") ("eclbkbox")
1927 ("amsmath") ("amssymb") ("xymtex") ("chemist")
1928 ("a4j") ("array") ("epsf") ("color") ("epsfig") ("floatfig")
1929 ("landscape") ("path") ("supertabular") ("twocolumn")
1930 ("latexsym") ("times") ("makeidx"))
1931 "Default completion table for arguments of \\usepackage")
1933 (defvar YaTeX::usepackage-alist-private nil
1934 "*Private completion list of the argument for usepackage")
1936 (defvar YaTeX::usepackage-alist-local nil
1937 "Directory local completion list of the argument for usepackage")
1939 (defun YaTeX::usepackage (&optional argp)
1940 (cond
1941 ((equal argp 1)
1942 (setq YaTeX-env-name "document")
1943 (let ((minibuffer-local-completion-map YaTeX-minibuffer-completion-map)
1944 (delim ","))
1945 (YaTeX-cplread-with-learning
1946 (if YaTeX-japan "Use package(カンマで区切ってOK): "
1947 "Use package(delimitable by comma): ")
1948 'YaTeX::usepackage-alist-default
1949 'YaTeX::usepackage-alist-private
1950 'YaTeX::usepackage-alist-local)))))
1952 (defun YaTeX::mask (argp)
1953 (cond
1954 ((equal argp 1)
1955 (read-string "String: "))
1956 ((equal argp 2)
1957 (let (c)
1958 (while (not (memq c '(?A ?B ?C ?D ?E ?F ?G ?H ?I ?J ?K)))
1959 (message "Mask type(A..K): ")
1960 (setq c (upcase (read-char))))
1961 (format "%c" c)))))
1963 (defun YaTeX::maskbox (argp)
1964 (cond
1965 ((equal argp 1)
1966 (read-string "Width: "))
1967 ((equal argp 2)
1968 (read-string "Height: "))
1969 ((equal argp 3)
1970 (let (c)
1971 (while (not (memq c '(?A ?B ?C ?D ?E ?F ?G ?H ?I ?J ?K)))
1972 (message "Mask type(A..K): ")
1973 (setq c (upcase (read-char))))
1974 (format "%c" c)))
1975 ((equal argp 4)
1976 (YaTeX:read-oneof "lcr" 'quick))
1977 ((equal argp 5)
1978 (read-string "String: "))))
1980 (defun YaTeX::textcircled (argp)
1981 (cond
1982 ((equal argp 1)
1983 (let ((char (read-string "Circled char: "))
1984 (left "") (right "") c)
1985 (setq c (read-char
1986 "Enclose also with (s)mall (t)iny s(C)riptsize (N)one:"))
1987 (cond
1988 ((memq c '(?s ?S)) (setq left "{\\small " right "}"))
1989 ((memq c '(?t ?T)) (setq left "{\\tiny " right "}"))
1990 ((memq c '(?c ?C)) (setq left "{\\scriptsize " right "}")))
1991 (format "%s%s%s" left char right)))))
1993 ;;; -------------------- math-mode stuff --------------------
1994 (defun YaTeX::tilde (&optional pos)
1995 "For accent macros in mathmode"
1996 (cond
1997 ((equal pos 1)
1998 (message "Put accent on variable: ")
1999 (let ((v (char-to-string (read-char))) (case-fold-search nil))
2000 (message "")
2001 (cond
2002 ((string-match "i\\|j" v)
2003 (concat "\\" v "math"))
2004 ((string-match "[\r\n\t ]" v)
2005 "")
2006 (t v))))
2007 (nil "")))
2009 (fset 'YaTeX::hat 'YaTeX::tilde)
2010 (fset 'YaTeX::check 'YaTeX::tilde)
2011 (fset 'YaTeX::bar 'YaTeX::tilde)
2012 (fset 'YaTeX::dot 'YaTeX::tilde)
2013 (fset 'YaTeX::ddot 'YaTeX::tilde)
2014 (fset 'YaTeX::vec 'YaTeX::tilde)
2016 (defun YaTeX::widetilde (&optional pos)
2017 "For multichar accent macros in mathmode"
2018 (cond
2019 ((equal pos 1)
2020 (let ((m "Put over chars[%s ]: ") v v2)
2021 (message m " ")
2022 (setq v (char-to-string (read-char)))
2023 (message "")
2024 (if (string-match "[\r\n\t ]" v)
2025 ""
2026 (message m v)
2027 (setq v2 (char-to-string (read-char)))
2028 (message "")
2029 (if (string-match "[\r\n\t ]" v2)
2031 (concat v v2)))))
2032 (nil "")))
2034 (fset 'YaTeX::widehat 'YaTeX::widetilde)
2035 (fset 'YaTeX::overline 'YaTeX::widetilde)
2036 (fset 'YaTeX::overrightarrow 'YaTeX::widetilde)
2039 ; for \frac{}{} region
2040 (defun YaTeX::frac-region (beg end)
2041 (if (catch 'done
2042 (while (re-search-forward "\\s *\\(\\\\over\\|/\\)\\s *" end t)
2043 (goto-char (match-beginning 0))
2044 (if (y-or-n-p
2045 (format "Replace this `%s' with `}{'" (YaTeX-match-string 0)))
2046 (throw 'done t))
2047 (goto-char (match-end 0))))
2048 (let (p (b0 (match-beginning 0)) e0)
2049 (replace-match "}{")
2050 (setq e0 (point))
2051 (save-restriction
2052 (narrow-to-region beg end)
2053 (goto-char e0)
2054 (skip-chars-forward " \t")
2055 (setq p (point))
2056 (YaTeX-goto-corresponding-paren)
2057 (forward-char 1)
2058 (skip-chars-forward " \t\r\n")
2059 (if (= end (1+ (point)))
2060 (progn
2061 (goto-char p)
2062 (if (looking-at "\\\\") (forward-char 1))
2063 (YaTeX-kill-paren nil)))
2064 (goto-char beg)
2065 (skip-chars-forward " \t")
2066 (setq p (point))
2067 (YaTeX-goto-corresponding-paren)
2068 (forward-char 1)
2069 (skip-chars-forward " \t\r\n")
2070 (if (>= (point) b0)
2071 (progn
2072 (goto-char p)
2073 (if (looking-at "\\\\") (forward-char 1))
2074 (YaTeX-kill-paren nil))))))
2075 (message ""))
2077 (defun YaTeX::DeclareMathOperator (argp)
2078 (cond
2079 ((equal argp 1)
2080 (read-string "Operator: " "\\"))))
2082 ;;;
2083 ;; Add-in functions for large-type command.
2084 ;;;
2085 (defun YaTeX:em ()
2086 (cond
2087 ((eq YaTeX-current-completion-type 'large) "\\/")
2088 (t nil)))
2089 (fset 'YaTeX:it 'YaTeX:em)
2091 ;;; -------------------- End of yatexadd --------------------
2092 (provide 'yatexadd)
2093 ; Local variables:
2094 ; fill-prefix: ";;; "
2095 ; paragraph-start: "^$\\| \\|;;;$"
2096 ; paragraph-separate: "^$\\| \\|;;;$"
2097 ; coding: sjis
2098 ; End: