yatex

view yatexadd.el @ 254:45ea6e6e5b26

Add-in for \includegraphics offers "clip" option.
author HIROSE Yuuji <yuuji@gentei.org>
date Sun, 12 Feb 2012 10:28:54 +0900
parents 73fef09ebc15
children 214702e4df54
line source
1 ;;; -*- Emacs-Lisp -*-
2 ;;; YaTeX add-in functions.
3 ;;; yatexadd.el rev.20
4 ;;; (c)1991-2012 by HIROSE Yuuji.[yuuji@yatex.org]
5 ;;; Last modified Sun Feb 12 10:23:23 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 ((looking-at YaTeX-sectioning-regexp)
610 (setq command (YaTeX-match-string 0))
611 (skip-chars-forward "^{")
612 (setq arg (buffer-substring
613 (1+ (point))
614 (progn (forward-list 1) (1- (point)))))
615 (skip-chars-forward " \t\n")
616 ;(setq boundary "[^\\]")
617 (setq inspoint (point))
618 (setq boundary
619 (save-excursion
620 (if (YaTeX-re-search-active-forward
621 (concat YaTeX-ec-regexp
622 "\\(" YaTeX-sectioning-regexp "\\|"
623 "begin\\|item\\)")
624 r-escape nil 1)
625 (match-beginning 0)
626 (1- (point))))))
627 ((looking-at "item\\s ")
628 (setq command "item"
629 cc (+ cc 6))
630 ;(setq boundary (concat YaTeX-ec-regexp "\\(item\\|begin\\|end\\)\\b"))
631 (setq boundary
632 (save-excursion
633 (if (YaTeX-re-search-active-forward
634 (concat YaTeX-ec-regexp "\\(item\\|begin\\|end\\)\\b")
635 r-escape nil 1)
636 (match-beginning 0)
637 (1- (point))))
638 inspoint boundary))
639 ((looking-at "bibitem")
640 (setq labelholder "bibitem" ; label holder is bibitem itself
641 command "bibitem")
642 (setq boundary
643 (save-excursion
644 (if (YaTeX-re-search-active-forward
645 (concat YaTeX-ec-regexp "\\(bibitem\\|end\\)\\b")
646 r-escape nil 1)
647 (match-beginning 0)
648 (1- (point))))
649 inspoint boundary))
650 ((string-match YaTeX::ref-nestable-counter-regexp
651 (setq env (or (YaTeX-inner-environment t) "document")))
652 (let ((curtop (get 'YaTeX-inner-environment 'point))
653 (end (point-max)) label)
654 (skip-chars-forward " \t\n")
655 (setq inspoint (point) ;initial candidate
656 cc (current-column)
657 command env
658 alreadysought t)
659 (if (condition-case nil
660 (progn
661 (goto-char curtop)
662 (YaTeX-goto-corresponding-environment))
663 (error nil))
664 (setq end (point)))
665 (goto-char inspoint)
666 (while (YaTeX-re-search-active-forward
667 (concat YaTeX-ec-regexp "label{\\([^}]+\\)}" )
668 r-escape end t)
669 (setq label (YaTeX-match-string 1))
670 (if (and (equal env (YaTeX-inner-environment t))
671 (= curtop (get 'YaTeX-inner-environment 'point)))
672 ;;I found the label
673 (setq alreadysought label
674 foundpoint (match-end 0))))
675 ))
676 ((string-match YaTeX::ref-mathenv-regexp env) ;env is set in above case
677 (setq command env
678 mathp t
679 exp1 (string-match YaTeX::ref-mathenv-exp1-regexp env))
680 ;;(setq boundary (concat YaTeX-ec-regexp "\\(\\\\\\|end{" env "}\\)"))
681 (setq boundary
682 (save-excursion
683 (if (YaTeX-re-search-active-forward
684 (concat
685 YaTeX-ec-regexp "\\("
686 (if exp1 "" "\\\\\\|")
687 "end{" env "}\\)")
688 r-escape nil 1)
689 (match-beginning 0)
690 (1- (point))))
691 inspoint boundary))
692 ((looking-at "footnote\\s *{")
693 (setq command "footnote")
694 (skip-chars-forward "^{") ;move onto `{'
695 (setq boundary
696 (save-excursion
697 (condition-case err
698 (forward-list 1)
699 (error (error "\\\\footnote at point %s's brace not closed"
700 (point))))
701 (1- (point)))
702 inspoint boundary))
703 ((looking-at "caption\\|\\(begin\\)")
704 (setq command (YaTeX-match-string 0))
705 (skip-chars-forward "^{")
706 ;;;;;;(if (match-beginning 1) (forward-list 1))
707 ;; caption can be treated as mathenv, is it right??
708 (setq arg (buffer-substring
709 (1+ (point))
710 (progn (forward-list 1) (1- (point)))))
711 ;;(setq boundary (concat YaTeX-ec-regexp "\\(begin\\|end\\)\\b"))
712 (setq inspoint (point))
713 (setq boundary
714 (save-excursion
715 (if (YaTeX-re-search-active-forward
716 (concat YaTeX-ec-regexp "\\(begin\\|end\\)\\b")
717 r-escape nil 1)
718 (match-beginning 0)
719 (1- (point))))))
720 (t ))
721 (if (save-excursion (skip-chars-forward " \t") (looking-at "%"))
722 (forward-line 1))
723 (cond
724 ((stringp alreadysought)
725 (put 'YaTeX::ref-getset-label 'foundpoint foundpoint) ;ugly...
726 alreadysought)
727 ((and (null alreadysought)
728 (> boundary (point))
729 (save-excursion
730 (YaTeX-re-search-active-forward
731 ;;(concat "\\(" labelholder "\\)\\|\\(" boundary "\\)")
732 labelholder
733 (regexp-quote YaTeX-comment-prefix)
734 boundary 1))
735 (match-beginning 0))
736 ;; if \label{hoge} found, return it
737 (put 'YaTeX::ref-getset-label 'foundpoint (1- (match-beginning 0)))
738 (buffer-substring
739 (progn
740 (goto-char (match-end 0))
741 (skip-chars-forward "^{") (1+ (point)))
742 (progn
743 (forward-sexp 1) (1- (point)))))
744 ;;else make a label
745 ;(goto-char (match-beginning 0))
746 (noset nil) ;do not set label if noset
747 (t
748 (goto-char inspoint)
749 (skip-chars-backward " \t\n")
750 (save-excursion
751 (setq newlabel
752 (funcall YaTeX-ref-generate-label-function command arg)))
753 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
754 (if mathp nil
755 (insert "\n")
756 (YaTeX-reindent cc))
757 (insert (format "\\label{%s}" newlabel))
758 newlabel)))))
760 (defvar YaTeX::ref-labeling-regexp-alist-default
761 '(("\\\\begin{\\(java\\|program\\)}{\\([^}]+\\)}" . 2)
762 ("\\\\label{\\([^}]+\\)}" . 1))
763 "Alist of labeling regexp vs. its group number points to label string.
764 This alist is used in \\ref's argument's completion.")
765 (defvar YaTeX::ref-labeling-regexp-alist-private nil
766 "*Private extension to YaTeX::ref-labeling-regexp-alist.
767 See the documetation of YaTeX::ref-labeling-regexp-alist.")
768 (defvar YaTeX::ref-labeling-regexp-alist
769 (append YaTeX::ref-labeling-regexp-alist-default
770 YaTeX::ref-labeling-regexp-alist-private))
771 (defvar YaTeX::ref-labeling-regexp
772 (mapconcat 'car YaTeX::ref-labeling-regexp-alist "\\|"))
773 (defvar YaTeX::ref-mathenv-regexp
774 ;; See also YaTeX-ams-math-begin-alist in yatex.el
775 ;; Define only envs which has counter.(without *)
776 "equation\\|eqnarray\\|align\\(at\\)?\\|flalign\\|gather\\|xx?alignat\\|multline")
777 (defvar YaTeX::ref-mathenv-exp1-regexp
778 "\\(equation\\|multline\\)\\b"
779 "*Regexp of math-envname which has only one math-expression.")
780 (defvar YaTeX::ref-enumerateenv-regexp
781 "enumerate")
782 (defvar YaTeX::ref-nestable-counter-regexp
783 "subequations")
785 (defvar YaTeX::ref-labeling-section-level 2
786 "*ref補完で収集するセクショニングコマンドの下限レベル
787 YaTeX-sectioning-levelの数値で指定.")
789 (defun YaTeX::ref (argp &optional labelcmd refcmd predf)
790 (setplist 'YaTeX::ref-labeling-regexp nil) ;erase memory cache
791 (require 'yatexsec)
792 (cond
793 ((= argp 1)
794 (let*((lnum 0) m0 e0 x cmd label match-point point-list boundary
795 (buf (current-buffer))
796 (llv YaTeX::ref-labeling-section-level)
797 (mathenvs YaTeX::ref-mathenv-regexp) envname endrx
798 (enums YaTeX::ref-enumerateenv-regexp)
799 (counter
800 (or labelcmd
801 (concat
802 YaTeX-ec-regexp "\\(\\("
803 (mapconcat
804 'concat
805 (delq nil
806 (mapcar
807 (function
808 (lambda (s)
809 (if (>= llv (cdr s))
810 (car s))))
811 YaTeX-sectioning-level))
812 "\\|")
813 "\\|caption\\(\\[[^]]+\\]\\)?\\|footnote\\){"
814 "\\|\\(begin{\\(" mathenvs "\\|" enums "\\)}\\)"
815 (if YaTeX-use-AMS-LaTeX
816 (concat
817 "\\|\\(begin{"
818 YaTeX::ref-nestable-counter-regexp "}\\)"))
819 "\\)")))
820 (regexp (concat "\\(" counter
821 "\\)\\|\\(" YaTeX::ref-labeling-regexp "\\)"))
822 (itemsep (concat YaTeX-ec-regexp
823 "\\(\\(bib\\)?item\\|begin\\|end\\)"))
824 (refcmd (or refcmd "\\(page\\)?ref"))
825 (p (point)) initl line cf
826 (percent (regexp-quote YaTeX-comment-prefix))
827 (output
828 (function
829 (lambda (label p)
830 (while (setq x (string-match "[\n\t]" label))
831 (aset label x ? ))
832 (while (setq x (string-match " +" label))
833 (setq label (concat
834 (substring label 0 (1+ (match-beginning 0)))
835 (substring label (match-end 0)))))
836 (princ (format "%c: <<%s>>\n" (+ (% lnum 26) ?A) label))
837 (setq point-list (cons p point-list))
838 (message "Collecting labels... %d" lnum)
839 (setq lnum (1+ lnum)))))
840 (me (if (boundp 'me) me 'YaTeX::ref))
841 )
842 (message "Collecting labels...")
843 (save-window-excursion
844 (YaTeX-showup-buffer
845 YaTeX-label-buffer (function (lambda (x) (window-width x))))
846 (if (fboundp 'select-frame) (setq cf (selected-frame)))
847 (if (eq (window-buffer (minibuffer-window)) buf)
848 (progn
849 (other-window 1)
850 (setq buf (current-buffer))
851 (set-buffer buf)))
852 (save-excursion
853 (set-buffer (get-buffer-create YaTeX-label-buffer))
854 (condition-case ()
855 (if (and YaTeX-use-font-lock (fboundp 'font-lock-mode))
856 (font-lock-mode 1))
857 (error nil))
858 (setq buffer-read-only nil)
859 (erase-buffer))
860 (save-excursion
861 (set-buffer buf)
862 (goto-char (point-min))
863 (let ((standard-output (get-buffer YaTeX-label-buffer)) existlabel)
864 (princ (format "=== LABELS in [%s] ===\n" (buffer-name buf)))
865 (while (YaTeX-re-search-active-forward
866 regexp ;;counter
867 percent nil t)
868 ;(goto-char (match-beginning 0))
869 (setq e0 (match-end 0))
870 (cond
871 ;;
872 ;;2005/10/21 Skip it if predicate function returns nil
873 ((and predf
874 (let ((md (match-data)))
875 (prog1
876 (condition-case nil
877 (not (funcall predf))
878 (error nil))
879 (store-match-data md)))))
880 ((YaTeX-literal-p) nil)
881 ((YaTeX-match-string 1)
882 ;;if standard counter commands found
883 (setq cmd (YaTeX-match-string 2)
884 m0 (match-beginning 0))
885 (setq match-point (match-beginning 0))
886 (or initl
887 (if (< p (point)) (setq initl lnum)))
888 (cond
889 ;; In any case, variables e0 should be set
890 ((and YaTeX-use-AMS-LaTeX
891 (string-match YaTeX::ref-nestable-counter-regexp cmd))
892 (let (label)
893 (skip-chars-forward "}")
894 (setq label (buffer-substring
895 (point) (min (+ 80 (point)) (point-max))))
896 ;; to skip (maybe)auto-generated comment
897 (skip-chars-forward " \t")
898 (if (looking-at YaTeX-comment-prefix)
899 (forward-line 1))
900 (setq e0 (point))
901 (skip-chars-forward " \t\n")
902 (if (looking-at "\\\\label{\\([^}]+\\)}")
903 (setq label (format "(labe:%s)" (YaTeX-match-string 1))
904 e0 (match-end 1)))
905 (funcall output (format "--subequation--%s" label) e0)))
906 ((string-match mathenvs cmd) ;;if matches mathematical env
907 ;(skip-chars-forward "} \t\n")
908 ;(forward-line 1) ;2004/1/25
909 (skip-chars-forward "}")
910 (setq x (point)
911 envname (substring
912 cmd (match-beginning 0) (match-end 0)))
913 (save-restriction
914 (narrow-to-region
915 m0
916 (save-excursion
917 (YaTeX-re-search-active-forward
918 (setq endrx (format "%send{%s}" YaTeX-ec-regexp
919 (regexp-quote envname)))
920 percent nil t)))
921 (catch 'scan
922 (while (YaTeX-re-search-active-forward
923 (concat
924 "\\\\end{\\(" (regexp-quote envname) "\\)";;(1)
925 (if YaTeX-use-AMS-LaTeX
926 "\\|\\\\\\(notag\\)") ;;2
927 (if (string-match
928 YaTeX::ref-mathenv-exp1-regexp cmd)
929 "" "\\|\\\\\\\\$")
930 )
931 percent nil t)
932 (let*((quit (match-beginning 1))
933 (notag (match-beginning 2))
934 (label ".......................") l2
935 (e (point)) (m0 (match-beginning 0))
936 (ln (YaTeX-string-width label)))
937 (cond
938 (notag
939 (YaTeX-re-search-active-forward
940 "\\\\\\\\" percent nil 1)
941 (setq x (point)))
942 (t
943 (if (YaTeX-re-search-active-backward
944 YaTeX::ref-labeling-regexp
945 percent x t)
946 ;; if \label{x} in math-expression, display it
947 ;; because formula source is hard to recognize
948 (progn
949 (goto-char (match-end 0))
950 (setq l2 (format "\"label:%s\""
951 (buffer-substring
952 (1- (point))
953 (progn (forward-sexp -1)
954 (1+ (point))))))
955 (setq label
956 (if (< (YaTeX-string-width l2) ln)
957 (concat
958 l2
959 (substring
960 label
961 0 (- ln (YaTeX-string-width l2))))
962 l2))
963 (goto-char e)))
964 (funcall output
965 (concat
966 label " "
967 (buffer-substring x m0))
968 x)
969 (cond
970 ((YaTeX-quick-in-environment-p
971 YaTeX-math-gathering-list)
972 ;; if here is inner split/cases/gathered env.,
973 ;; counter for here is only one.
974 ;; Go out this environment and,
975 (YaTeX-end-of-environment)
976 ;; search next expression unit boundary.
977 (YaTeX-re-search-active-forward
978 (concat endrx "\\|\\\\begin{")
979 percent nil 1)
980 (end-of-line)))
981 (if quit (throw 'scan t)))))
982 (setq x (point)))))
983 (setq e0 (point)))
984 ((string-match enums cmd)
985 ;(skip-chars-forward "} \t\n")
986 (save-restriction
987 (narrow-to-region
988 (point)
989 (save-excursion
990 (YaTeX-goto-corresponding-environment) (point)))
991 (forward-line 1)
992 (while (YaTeX-re-search-active-forward
993 (concat YaTeX-ec-regexp "item\\s ")
994 percent nil t)
995 (setq x (match-beginning 0))
996 (funcall
997 output
998 (concat
999 existlabel
1000 (buffer-substring
1001 (match-beginning 0)
1002 (if (re-search-forward itemsep nil t)
1003 (progn (goto-char (match-beginning 0))
1004 (skip-chars-backward " \t")
1005 (1- (point)))
1006 (point-end-of-line))))
1007 x))
1008 (setq e0 (point-max))))
1009 ((string-match "bibitem" cmd) ;maybe generated by myself
1010 (setq label "")
1011 (skip-chars-forward " \t")
1012 (if (looking-at "{") ;sure to be true!!
1013 (forward-list 1))
1014 (let ((list '(30 10 65))
1015 (delim ";") q lim len l str)
1016 (save-excursion
1017 (setq lim (if (re-search-forward itemsep nil 1)
1018 (match-beginning 0) (point))))
1019 (while list
1020 (skip-chars-forward " \t\n\\")
1021 (setq q (looking-at "[\"'{]")
1022 len (car list)
1023 str
1024 (buffer-substring
1025 (point)
1026 (progn
1027 (if q (forward-sexp 1)
1028 (search-forward delim lim 1)
1029 (forward-char -1))
1030 (point))))
1031 (if (> (setq l (YaTeX-string-width str)) len)
1032 (setq str (concat
1033 (YaTeX-truncate-string-width
1034 str (- len (if q 5 4)))
1035 "... "
1036 (if q (substring str -1)))))
1037 (if (< (setq l (YaTeX-string-width str)) len)
1038 (setq str (concat str (make-string (- len l) ? ))))
1039 (if (looking-at delim) (goto-char (match-end 0)))
1040 (setq label (concat label " " str)
1041 list (cdr list)))
1042 (funcall output label match-point)))
1043 ;;else, simple section-type counter
1044 ((= (char-after (1- (point))) ?{)
1045 (setq label (buffer-substring
1046 (match-beginning 0)
1047 (progn (forward-char -1)
1048 (forward-list 1)
1049 (point))))
1050 (funcall output label match-point)
1051 ;; Skip preceding label if exists
1052 (if (YaTeX::ref-getset-label (current-buffer) match-point t)
1053 (goto-char (get 'YaTeX::ref-getset-label 'foundpoint)))
1054 (if (save-excursion
1055 (skip-chars-forward "\t \n")
1056 (looking-at YaTeX::ref-labeling-regexp))
1057 (setq e0 (match-end 0))))
1058 (t
1059 (skip-chars-forward " \t")
1060 (setq label (buffer-substring
1061 (match-beginning 0)
1062 (if (re-search-forward
1063 itemsep
1064 nil t)
1065 (progn
1066 (goto-char (match-beginning 0))
1067 (skip-chars-backward " \t")
1068 (1- (point)))
1069 (point-end-of-line))))
1070 (funcall output label match-point)
1071 (if (save-excursion
1072 (skip-chars-forward "\t \n")
1073 (looking-at YaTeX::ref-labeling-regexp))
1074 (setq e0 (match-end 0)))))
1075 ) ;;put label buffer
1076 ;;
1077 ;; if user defined label found
1078 (t
1079 ;; memorize line number and label into property
1080 (goto-char (match-beginning 0))
1081 (let ((list YaTeX::ref-labeling-regexp-alist)
1082 (cache (symbol-plist 'YaTeX::ref-labeling-regexp)))
1083 (while list
1084 (if (looking-at (car (car list)))
1085 (progn
1086 (setq label (YaTeX-match-string 0))
1087 (put 'YaTeX::ref-labeling-regexp lnum
1088 (YaTeX-match-string (cdr (car list))))
1089 (funcall output label 0) ;;0 is dummy, never used
1090 (setq list nil)))
1091 (setq list (cdr list))))
1092 ))
1093 (goto-char e0))
1094 (princ YaTeX-label-menu-other)
1095 (princ YaTeX-label-menu-repeat)
1096 (princ YaTeX-label-menu-any)
1097 );standard-output
1098 (goto-char p)
1099 (or initl (setq initl lnum))
1100 (message "Collecting labels...Done")
1101 (if (fboundp 'select-frame) (select-frame cf))
1102 (YaTeX-showup-buffer YaTeX-label-buffer nil t)
1103 (YaTeX::label-setup-key-map)
1104 (setq truncate-lines t)
1105 (setq buffer-read-only t)
1106 (use-local-map YaTeX-label-select-map)
1107 (message YaTeX-label-guide-msg)
1108 (goto-line (1+ initl)) ;goto recently defined label line
1109 (switch-to-buffer (current-buffer))
1110 (unwind-protect
1111 (progn
1112 (recursive-edit)
1114 (set-buffer (get-buffer YaTeX-label-buffer)) ;assertion
1115 (beginning-of-line)
1116 (setq line (1- (count-lines (point-min)(point))))
1117 (cond
1118 ((= line -1) (setq label ""))
1119 ((= line lnum) (setq label (YaTeX-label-other)))
1120 ((= line (1+ lnum))
1121 (save-excursion
1122 (switch-to-buffer buf)
1123 (goto-char p)
1124 (if (re-search-backward
1125 (concat "\\\\" refcmd "{") nil t)
1126 (setq label (YaTeX-buffer-substring
1127 (progn (goto-char (1- (match-end 0)))
1128 (1+ (point)))
1129 (progn (forward-list 1)
1130 (1- (point)))))
1131 (setq label ""))))
1132 ((>= line (+ lnum 2))
1133 (setq label (read-string (format "\\%s{???}: " refcmd))))
1134 (t ;(setq label (nth (- lnum line 1) label-list))
1135 (setq label
1136 (or (get 'YaTeX::ref-labeling-regexp line)
1137 (YaTeX::ref-getset-label
1138 buf (nth (- lnum line 1) point-list))))
1139 )))
1140 (bury-buffer YaTeX-label-buffer)))
1141 label)))))
1143 (defun YaTeX::label-rename-refs (old new &optional def ref)
1144 "Rename reference tag from OLD to NEW.
1145 Optional arguments DEF and REF specify defining command and
1146 referring command respectively.
1147 ---------------------------------------------------------
1148 CONTROL KEYS - キーの説明
1149 y Replace 置換する
1150 n Do not replace 置換しない
1151 ! Replace All w/o query 残る全部を確認なしで置換
1152 r Enter Recursive-edit 再帰編集モードへ
1153 q Quit from replacing ここまでで置換をやめる
1155 Don't forget to exit from recursive edit by typing \\[exit-recursive-edit]
1156 再帰編集に入ったら \\[exit-recursive-edit] で抜け忘れなきよう。"
1157 (save-window-excursion
1158 (catch 'exit
1159 (let*((bufs (YaTeX-yatex-buffer-list)) buf b e
1160 (oldptn (regexp-quote old))
1161 (sw (selected-window))
1162 (ptn (concat
1163 "\\(" YaTeX-refcommand-ref-regexp "\\)"
1164 "\\s *{" oldptn "}"))
1165 (repface (and (fboundp 'make-overlay)
1166 (fboundp 'internal-find-face)
1167 (if (internal-find-face 'isearch) 'isearch 'region)))
1168 ov
1169 (qmsg "Replace to `%s'? [yn!rq?]")
1170 continue ch)
1171 (while bufs
1172 (set-buffer (setq buf (car bufs)))
1173 (save-excursion
1174 (goto-char (point-min))
1175 (while (re-search-forward ptn nil t)
1176 (goto-char (match-end 1))
1177 (skip-chars-forward " \t\n{")
1178 (unwind-protect
1179 (if (and
1180 (looking-at oldptn)
1181 (setq b (match-beginning 0)
1182 e (match-end 0))
1183 (or continue
1184 (catch 'query
1185 (if repface
1186 (if ov (move-overlay ov b e)
1187 (overlay-put
1188 (setq ov (make-overlay b e))
1189 'face repface)))
1190 (switch-to-buffer buf)
1191 (while t
1192 (message qmsg new)
1193 (setq ch (read-char))
1194 (cond
1195 ((= ch ?q) (throw 'exit t))
1196 ((= ch ?r)
1197 (message
1198 "Don't forget to exit recursive-edit by `%s'"
1199 (key-description
1200 (where-is-internal
1201 'exit-recursive-edit '(keymap) t)))
1202 (sleep-for 2)
1203 (recursive-edit))
1204 ((= ch ?y) (throw 'query t))
1205 ((= ch ?!) (throw 'query (setq continue t)))
1206 ((= ch ??)
1207 (describe-function
1208 'YaTeX::label-rename-refs)
1209 (select-window (get-buffer-window "*Help*"))
1210 (search-forward "----")
1211 (forward-line 1)
1212 (set-window-start (selected-window) (point))
1213 (sit-for 0)
1214 (select-window sw))
1215 ((= ch ?n) (throw 'query nil)))))))
1216 (replace-match new))
1217 (and ov (delete-overlay ov)))))
1218 (setq bufs (cdr bufs)))))))
1220 (defun YaTeX::label (argp &optional labname refname)
1221 "Read label name and return it with copying \\ref{LABEL-NAME} to kill-ring."
1222 (cond
1223 ((= argp 1)
1224 (let*((chmode (boundp (intern-soft "old")))
1225 (dlab (if chmode old ;if called via YaTeX-change-section (tricky...)
1226 (YaTeX::ref-default-label)))
1227 (label (read-string
1228 (format "New %s name: " (or labname "label"))
1229 (cons dlab 1))))
1230 (if (string< "" label)
1231 (let ((refstr (format "\\%s{%s}" (or refname "ref") label)))
1232 (YaTeX-push-to-kill-ring refstr)
1233 (and chmode
1234 (not (equal old label))
1235 (YaTeX::label-rename-refs old label))))
1236 label))))
1239 (fset 'YaTeX::pageref 'YaTeX::ref)
1240 (defun YaTeX::tabref (argp) ; For the style file of IPSJ journal
1241 (YaTeX::ref
1242 argp nil nil
1243 (function
1244 (lambda ()
1245 (YaTeX-quick-in-environment-p "table")))))
1246 (defun YaTeX::figref (argp) ; For the style file of IPSJ journal
1247 (YaTeX::ref
1248 argp nil nil
1249 (function
1250 (lambda ()
1251 (YaTeX-quick-in-environment-p "figure")))))
1252 (defun YaTeX::eqref (argp)
1253 (YaTeX::ref
1254 argp nil nil
1255 (function
1256 (lambda ()
1257 (YaTeX-in-math-mode-p)))))
1259 (defun YaTeX::cite-collect-bibs-external (bibptn &rest files)
1260 "Collect bibentry from FILES(variable length argument) ;
1261 and print them to standard output."
1262 ;;Thanks; http://icarus.ilcs.hokudai.ac.jp/comp/biblio.html
1263 (let*((tb (get-buffer-create " *bibtmp*"))
1264 (bibitemsep "^\\s *@[A-Za-z]")
1265 (target (if (string< "" bibptn) bibptn bibitemsep))
1266 (checkrx (concat "\\(" bibptn "\\)\\|" bibitemsep))
1267 beg
1268 (searchnext
1269 (if (string< "" bibptn)
1270 (function
1271 (lambda()
1272 (setq beg (point))
1273 (and
1274 (prog1
1275 (re-search-forward target nil t)
1276 (end-of-line))
1277 (re-search-backward bibitemsep beg t))))
1278 (function
1279 (lambda()
1280 (re-search-forward target nil t)))))
1282 (save-excursion
1283 (set-buffer tb)
1284 (princ (format "%sbegin{thebibliography}\n" YaTeX-ec))
1285 (while files
1286 (erase-buffer)
1287 (cond
1288 ((file-exists-p (car files))
1289 (insert-file-contents (car files)))
1290 ((file-exists-p (concat (car files) ".bib"))
1291 (insert-file-contents (concat (car files) ".bib"))))
1292 (save-excursion
1293 (goto-char (point-min))
1294 (while (funcall searchnext)
1295 (skip-chars-forward "^{,")
1296 (setq beg (point))
1297 (if (= (char-after (point)) ?{)
1298 (princ (format "%sbibitem{%s}%s\n"
1299 YaTeX-ec
1300 (buffer-substring
1301 (1+ (point))
1302 (progn (skip-chars-forward "^,\n")
1303 (point)))
1304 (mapconcat
1305 (function
1306 (lambda (kwd)
1307 (goto-char beg)
1308 (if (re-search-forward
1309 (concat kwd "\\s *=") nil t)
1310 (buffer-substring
1311 (progn
1312 (goto-char (match-end 0))
1313 (skip-chars-forward " \t\n")
1314 (point))
1315 (progn
1316 (if (looking-at "[{\"]")
1317 (forward-sexp 1)
1318 (forward-char 1)
1319 (skip-chars-forward "^,}"))
1320 (point))))))
1321 '("author" "year" "title" )
1322 ";"))))
1323 (and (re-search-forward bibitemsep nil t)
1324 (forward-line -1))))
1325 (setq files (cdr files)))
1326 (princ (format "%sbegin{thebibliography}\n" YaTeX-ec)))))
1328 (defvar YaTeX::cite-bibitem-macro-regexp "bibitem\\|harvarditem"
1329 "*Regexp of macro name of bibitem definition")
1331 (defun YaTeX::cite-collect-bibs-internal (bibptn)
1332 "Collect bibentry in the current buffer and print them to standard output."
1333 (let ((ptn (concat YaTeX-ec-regexp
1334 "\\(" YaTeX::cite-bibitem-macro-regexp "\\)\\b"))
1335 (lim (concat YaTeX-ec-regexp
1336 "\\(" YaTeX::cite-bibitem-macro-regexp "\\b\\)"
1337 "\\|\\(end{\\)"))
1338 (pcnt (regexp-quote YaTeX-comment-prefix)))
1339 ;; Using bibptn not yet implemented.
1340 ;; Do you need it?? 2005/11/22
1341 (save-excursion
1342 (while (YaTeX-re-search-active-forward ptn pcnt nil t)
1343 (skip-chars-forward "^{\n")
1344 (or (eolp)
1345 (princ (format "%sbibitem%s %s\n"
1346 YaTeX-ec
1347 (buffer-substring
1348 (point)
1349 (progn (forward-sexp 1) (point)))
1350 (buffer-substring
1351 (progn (skip-chars-forward "\n \t") (point))
1352 (save-excursion
1353 (if (YaTeX-re-search-active-forward
1354 lim pcnt nil t)
1355 (progn
1356 (goto-char (match-beginning 0))
1357 (skip-chars-backward "\n \t")
1358 (point))
1359 (point-end-of-line)))))))))))
1361 (defun YaTeX::cite (argp &rest dummy)
1362 (cond
1363 ((eq argp 1)
1364 (let* ((cb (current-buffer))
1365 (f (file-name-nondirectory buffer-file-name))
1366 (d default-directory)
1367 (hilit-auto-highlight nil)
1368 (pcnt (regexp-quote YaTeX-comment-prefix))
1369 (bibrx (concat YaTeX-ec-regexp "bibliography{\\([^}]+\\)}"))
1370 (bibptn (read-string "Pattern: "))
1371 (bbuf (get-buffer-create " *bibitems*"))
1372 (standard-output bbuf)
1373 (me 'YaTeX::cite) ;shuld set this for using YaTeX::ref
1374 bibs files)
1375 (set-buffer bbuf)(erase-buffer)(set-buffer cb)
1376 (save-excursion
1377 (goto-char (point-min))
1378 ;;(1)search external bibdata
1379 (while (YaTeX-re-search-active-forward bibrx pcnt nil t)
1380 (apply 'YaTeX::cite-collect-bibs-external
1381 bibptn
1382 (YaTeX-split-string
1383 (YaTeX-match-string 1) ",")))
1384 ;;(2)search direct \bibitem usage
1385 (YaTeX::cite-collect-bibs-internal bibptn)
1386 (if (progn
1387 (YaTeX-visit-main t)
1388 (not (eq (current-buffer) cb)))
1389 (save-excursion
1390 (goto-char (point-min))
1391 ;;(1)search external bibdata
1392 (while (YaTeX-re-search-active-forward bibrx pcnt nil t)
1393 (apply 'YaTeX::cite-collect-bibs-external
1394 bibptn
1395 (YaTeX-split-string
1396 (YaTeX-match-string 1) ",")))
1397 ;;(2)search internal
1398 (YaTeX::cite-collect-bibs-internal bibptn)))
1399 ;;Now bbuf holds the list of bibitem
1400 (set-buffer bbuf)
1401 ;;;(switch-to-buffer bbuf)
1402 (if (fboundp 'font-lock-fontify-buffer) (font-lock-fontify-buffer))
1403 (YaTeX::ref
1404 argp
1405 (concat "\\\\\\("
1406 YaTeX::cite-bibitem-macro-regexp
1407 "\\)\\(\\[.*\\]\\)?")
1408 "cite"))))
1410 (t nil)))
1412 (defun YaTeX::bibitem (argp)
1413 "Add-in function to insert argument of \\bibitem."
1414 (YaTeX::label argp "label" "cite"))
1416 ;;; for Harvard citation style
1417 (fset 'YaTeX::citeasnoun 'YaTeX::cite)
1418 (fset 'YaTeX::possessivecite 'YaTeX::cite)
1419 (fset 'YaTeX::citeyear 'YaTeX::cite)
1420 (fset 'YaTeX::citename 'YaTeX::cite)
1421 (fset 'YaTeX::citep 'YaTeX::cite)
1422 (fset 'YaTeX::citet 'YaTeX::cite)
1424 (defun YaTeX-select-other-yatex-buffer ()
1425 "Select buffer from all yatex-mode's buffers interactivelly."
1426 (interactive)
1427 (let ((lbuf "*YaTeX mode buffers*") (blist (YaTeX-yatex-buffer-list))
1428 (lnum -1) buf rv
1429 (ff "**find-file**"))
1430 (YaTeX-showup-buffer
1431 lbuf (function (lambda (x) 1))) ;;Select next window surely.
1432 (save-excursion
1433 (set-buffer (get-buffer lbuf))
1434 (setq buffer-read-only nil)
1435 (erase-buffer))
1436 (let ((standard-output (get-buffer lbuf)))
1437 (while blist
1438 (princ
1439 (format "%c:{%s}\n" (+ (% (setq lnum (1+ lnum)) 26) ?A)
1440 (buffer-name (car blist))))
1441 (setq blist (cdr blist)))
1442 (princ (format "':{%s}" ff)))
1443 (YaTeX-showup-buffer lbuf nil t)
1444 (YaTeX::label-setup-key-map)
1445 (setq buffer-read-only t)
1446 (use-local-map YaTeX-label-select-map)
1447 (message YaTeX-label-guide-msg)
1448 (unwind-protect
1449 (progn
1450 (recursive-edit)
1451 (set-buffer lbuf)
1452 (beginning-of-line)
1453 (setq rv
1454 (if (re-search-forward "{\\([^\\}]+\\)}" (point-end-of-line) t)
1455 (buffer-substring (match-beginning 1) (match-end 1)) nil)))
1456 (kill-buffer lbuf))
1457 (if (string= rv ff)
1458 (progn
1459 (call-interactively 'find-file)
1460 (current-buffer))
1461 rv)))
1463 (defun YaTeX-label-other ()
1464 (let ((rv (YaTeX-select-other-yatex-buffer)))
1465 (cond
1466 ((null rv) "")
1467 (t
1468 (set-buffer rv)
1469 (funcall me argp labelcmd refcmd)))))
1471 ;;
1472 ; completion for the arguments of \newcommand
1473 ;;
1474 (defun YaTeX::newcommand (&optional argp)
1475 (cond
1476 ((= argp 1)
1477 (let ((command (read-string "Define newcommand: " "\\")))
1478 (put 'YaTeX::newcommand 'command (substring command 1))
1479 command))
1480 ((= argp 2)
1481 (let ((argc
1482 (string-to-int (read-string "Number of arguments(Default 0): ")))
1483 (def (read-string "Definition: "))
1484 (command (get 'YaTeX::newcommand 'command)))
1485 ;;!!! It's illegal to insert string in the add-in function !!!
1486 (if (> argc 0) (insert (format "[%d]" argc)))
1487 (if (and (stringp command)
1488 (string< "" command)
1489 (y-or-n-p "Update dictionary?"))
1490 (cond
1491 ((= argc 0)
1492 (YaTeX-update-table
1493 (list command)
1494 'singlecmd-table 'user-singlecmd-table 'tmp-singlecmd-table))
1495 ((= argc 1)
1496 (YaTeX-update-table
1497 (list command)
1498 'section-table 'user-section-table 'tmp-section-table))
1499 (t (YaTeX-update-table
1500 (list command argc)
1501 'section-table 'user-section-table 'tmp-section-table))))
1502 (message "")
1503 def ;return command name
1504 ))
1505 (t "")))
1507 (defun YaTeX::newcounter (&optional argp)
1508 (cond
1509 ((= argp 1)
1510 (read-string "New counter name: "))
1511 (t "")))
1513 ;;
1514 ; completion for the arguments of \pagestyle
1515 ;;
1516 (defun YaTeX::pagestyle (&optional argp)
1517 "Read the pagestyle with completion."
1518 (completing-read
1519 "Page style: "
1520 '(("plain") ("empty") ("headings") ("myheadings") ("normal") nil)))
1522 (fset 'YaTeX::thispagestyle 'YaTeX::pagestyle)
1524 ;;
1525 ; completion for the arguments of \pagenumbering
1526 ;;
1527 (defun YaTeX::pagenumbering (&optional argp)
1528 "Read the numbering style."
1529 (completing-read
1530 "Page numbering style: "
1531 '(("arabic") ("Alpha") ("alpha") ("Roman") ("roman"))))
1533 ;;
1534 ; Length
1535 ;;
1536 (defvar YaTeX:style-parameters-default
1537 '(("\\arraycolsep")
1538 ("\\arrayrulewidth")
1539 ("\\baselineskip")
1540 ("\\columnsep")
1541 ("\\columnseprule")
1542 ("\\doublerulesep")
1543 ("\\evensidemargin")
1544 ("\\footheight")
1545 ("\\footskip")
1546 ("\\headheight")
1547 ("\\headsep")
1548 ("\\itemindent")
1549 ("\\itemsep")
1550 ("\\labelsep")
1551 ("\\labelwidth")
1552 ("\\leftmargin")
1553 ("\\linewidth")
1554 ("\\listparindent")
1555 ("\\marginparsep")
1556 ("\\marginparwidth")
1557 ("\\mathindent")
1558 ("\\oddsidemargin")
1559 ("\\parindent")
1560 ("\\parsep")
1561 ("\\parskip")
1562 ("\\partopsep")
1563 ("\\rightmargin")
1564 ("\\tabcolsep")
1565 ("\\textheight")
1566 ("\\textwidth")
1567 ("\\topmargin")
1568 ("\\topsep")
1569 ("\\topskip")
1571 "Alist of LaTeX style parameters.")
1572 (defvar YaTeX:style-parameters-private nil
1573 "*User definable alist of style parameters.")
1574 (defvar YaTeX:style-parameters-local nil
1575 "*User definable alist of local style parameters.")
1577 (defvar YaTeX:length-history nil "Holds history of length.")
1578 (put 'YaTeX:length-history 'no-default t)
1579 (defun YaTeX::setlength (&optional argp)
1580 "YaTeX add-in function for arguments of \\setlength."
1581 (cond
1582 ((equal 1 argp)
1583 ;;(completing-read "Length variable: " YaTeX:style-parameters nil nil "\\")
1584 (YaTeX-cplread-with-learning
1585 "Length variable: "
1586 'YaTeX:style-parameters-default
1587 'YaTeX:style-parameters-private
1588 'YaTeX:style-parameters-local
1589 nil nil "\\")
1591 ((equal 2 argp)
1592 (read-string-with-history "Length: " nil 'YaTeX:length-history))))
1594 (fset 'YaTeX::addtolength 'YaTeX::setlength)
1596 (defun YaTeX::settowidth (&optional argp)
1597 "YaTeX add-in function for arguments of \\settowidth."
1598 (cond
1599 ((equal 1 argp)
1600 (YaTeX-cplread-with-learning
1601 "Length variable: "
1602 'YaTeX:style-parameters-default
1603 'YaTeX:style-parameters-private
1604 'YaTeX:style-parameters-local
1605 nil nil "\\"))
1606 ((equal 2 argp)
1607 (read-string "Text: "))))
1609 (defun YaTeX::newlength (&optional argp)
1610 "YaTeX add-in function for arguments of \\newlength"
1611 (cond
1612 ((equal argp 1)
1613 (let ((length (read-string "Length variable: " "\\")))
1614 (if (string< "" length)
1615 (YaTeX-update-table
1616 (list length)
1617 'YaTeX:style-parameters-default
1618 'YaTeX:style-parameters-private
1619 'YaTeX:style-parameters-local))
1620 length))))
1622 ;; \multicolumn's arguments
1623 (defun YaTeX::multicolumn (&optional argp)
1624 "YaTeX add-in function for arguments of \\multicolumn."
1625 (cond
1626 ((equal 1 argp)
1627 (read-string "Number of columns: "))
1628 ((equal 2 argp)
1629 (YaTeX:read-oneof "|lrc" nil t))
1630 ((equal 3 argp)
1631 (read-string "Item: "))))
1633 (defvar YaTeX:documentstyles-default
1634 '(("article") ("jarticle") ("j-article")
1635 ("book") ("jbook") ("j-book")
1636 ("report") ("jreport") ("j-report")
1637 ("letter") ("ascjletter"))
1638 "List of LaTeX documentstyles.")
1639 (defvar YaTeX:documentstyles-private nil
1640 "*User defined list of LaTeX documentstyles.")
1641 (defvar YaTeX:documentstyles-local nil
1642 "*User defined list of local LaTeX documentstyles.")
1643 (defvar YaTeX:documentstyle-options-default
1644 '(("a4j") ("a5j") ("b4j") ("b5j")
1645 ("twocolumn") ("jtwocolumn") ("epsf") ("epsfig") ("epsbox") ("nfig"))
1646 "List of LaTeX documentstyle options.")
1647 (defvar YaTeX:documentstyle-options-private nil
1648 "*User defined list of LaTeX documentstyle options.")
1649 (defvar YaTeX:documentstyle-options-local nil
1650 "List of LaTeX local documentstyle options.")
1652 (defun YaTeX:documentstyle ()
1653 (let*((delim ",")
1654 (dt (append YaTeX:documentstyle-options-local
1655 YaTeX:documentstyle-options-private
1656 YaTeX:documentstyle-options-default))
1657 (minibuffer-completion-table dt)
1658 (opt (read-from-minibuffer
1659 "Style options ([opt1,opt2,...]): "
1660 nil YaTeX-minibuffer-completion-map nil))
1661 (substr opt) o)
1662 (if (string< "" opt)
1663 (progn
1664 (while substr
1665 (setq o (substring substr 0 (string-match delim substr)))
1666 (or (assoc o dt)
1667 (YaTeX-update-table
1668 (list o)
1669 'YaTeX:documentstyle-options-default
1670 'YaTeX:documentstyle-options-private
1671 'YaTeX:documentstyle-options-local))
1672 (setq substr
1673 (if (string-match delim substr)
1674 (substring substr (1+ (string-match delim substr))))))
1675 (concat "[" opt "]"))
1676 "")))
1678 (defun YaTeX::documentstyle (&optional argp)
1679 "YaTeX add-in function for arguments of \\documentstyle."
1680 (cond
1681 ((equal argp 1)
1682 (setq YaTeX-env-name "document")
1683 (let ((sname
1684 (YaTeX-cplread-with-learning
1685 (format "Documentstyle (default %s): "
1686 YaTeX-default-document-style)
1687 'YaTeX:documentstyles-default
1688 'YaTeX:documentstyles-private
1689 'YaTeX:documentstyles-local)))
1690 (if (string= "" sname) (setq sname YaTeX-default-document-style))
1691 (setq YaTeX-default-document-style sname)))))
1693 (defun YaTeX::include (argp &optional prompt)
1694 "Read file name setting default directory to that of main file."
1695 (cond
1696 ((= argp 1)
1697 (save-excursion
1698 (YaTeX-visit-main t)
1699 (let*((insert-default-directory)
1700 (file (read-file-name (or prompt "Input file: ") "")))
1701 (setq file (substring file 0 (string-match "\\.tex$" file))))))))
1703 (fset 'YaTeX::input 'YaTeX::include)
1706 ;;; -------------------- LaTeX2e stuff --------------------
1707 (defvar YaTeX:documentclass-options-default
1708 '(("a4paper") ("a5paper") ("b4paper") ("b5paper") ("10pt") ("11pt") ("12pt")
1709 ("latterpaper") ("legalpaper") ("executivepaper") ("landscape")
1710 ("oneside") ("twoside") ("draft") ("final") ("leqno") ("fleqn") ("openbib")
1711 ("tombow") ("titlepage") ("notitlepage") ("dvips")
1712 ("mingoth") ;for jsarticle
1713 ("clock") ;for slides class only
1715 "Default options list for documentclass")
1716 (defvar YaTeX:documentclass-options-private nil
1717 "*User defined options list for documentclass")
1718 (defvar YaTeX:documentclass-options-local nil
1719 "*User defined options list for local documentclass")
1721 (defun YaTeX:documentclass ()
1722 (let*((delim ",")
1723 (dt (append YaTeX:documentclass-options-local
1724 YaTeX:documentclass-options-private
1725 YaTeX:documentclass-options-default))
1726 (minibuffer-completion-table dt)
1727 (opt (read-from-minibuffer
1728 "Documentclass options ([opt1,opt2,...]): "
1729 nil YaTeX-minibuffer-completion-map nil))
1730 (substr opt) o)
1731 (if (string< "" opt)
1732 (progn
1733 (while substr
1735 (setq o (substring substr 0 (string-match delim substr)))
1736 (or (assoc o dt)
1737 (YaTeX-update-table
1738 (list o)
1739 'YaTeX:documentclass-options-default
1740 'YaTeX:documentclass-options-private
1741 'YaTeX:documentclass-options-local))
1742 (setq substr
1743 (if (string-match delim substr)
1744 (substring substr (1+ (string-match delim substr))))))
1745 (concat "[" opt "]"))
1746 "")))
1748 (defvar YaTeX:documentclasses-default
1749 '(("article") ("jarticle") ("report") ("jreport") ("book") ("jbook")
1750 ("jsarticle") ("jsbook")
1751 ("j-article") ("j-report") ("j-book")
1752 ("letter") ("slides") ("ltxdoc") ("ltxguide") ("ltnews") ("proc"))
1753 "Default documentclass alist")
1754 (defvar YaTeX:documentclasses-private nil
1755 "*User defined documentclass alist")
1756 (defvar YaTeX:documentclasses-local nil
1757 "*User defined local documentclass alist")
1758 (defvar YaTeX-default-documentclass (if YaTeX-japan "jarticle" "article")
1759 "*Default documentclass")
1761 (defun YaTeX::documentclass (&optional argp)
1762 (cond
1763 ((equal argp 1)
1764 (setq YaTeX-env-name "document")
1765 (let ((sname
1766 (YaTeX-cplread-with-learning
1767 (format "Documentclass (default %s): " YaTeX-default-documentclass)
1768 'YaTeX:documentclasses-default
1769 'YaTeX:documentclasses-private
1770 'YaTeX:documentclasses-local)))
1771 (if (string= "" sname) (setq sname YaTeX-default-documentclass))
1772 (setq YaTeX-section-name "title"
1773 YaTeX-default-documentclass sname)))))
1775 (defun YaTeX::title (&optional argp)
1776 (prog1 (read-string "Document Title: ")
1777 (setq YaTeX-section-name "author"
1778 YaTeX-single-command "maketitle")))
1780 (defun YaTeX::author (&optional argp)
1781 (prog1 (read-string "Document Author: ")
1782 (setq YaTeX-section-name "date"
1783 YaTeX-single-command "maketitle")))
1785 (defun YaTeX:document ()
1786 (setq YaTeX-section-name
1787 (if (string-match "book\\|bk" YaTeX-default-documentclass)
1788 "chapter"
1789 "section"))
1790 "")
1793 (defvar YaTeX:latex2e-named-color-alist
1794 '(("GreenYellow") ("Yellow") ("Goldenrod") ("Dandelion") ("Apricot")
1795 ("Peach") ("Melon") ("YellowOrange") ("Orange") ("BurntOrange")
1796 ("Bittersweet") ("RedOrange") ("Mahogany") ("Maroon") ("BrickRed")
1797 ("Red") ("OrangeRed") ("RubineRed") ("WildStrawberry") ("Salmon")
1798 ("CarnationPink") ("Magenta") ("VioletRed") ("Rhodamine") ("Mulberry")
1799 ("RedViolet") ("Fuchsia") ("Lavender") ("Thistle") ("Orchid")("DarkOrchid")
1800 ("Purple") ("Plum") ("Violet") ("RoyalPurple") ("BlueViolet")
1801 ("Periwinkle") ("CadetBlue") ("CornflowerBlue") ("MidnightBlue")
1802 ("NavyBlue") ("RoyalBlue") ("Blue") ("Cerulean") ("Cyan") ("ProcessBlue")
1803 ("SkyBlue") ("Turquoise") ("TealBlue") ("Aquamarine") ("BlueGreen")
1804 ("Emerald") ("JungleGreen") ("SeaGreen") ("Green") ("ForestGreen")
1805 ("PineGreen") ("LimeGreen") ("YellowGreen") ("SpringGreen") ("OliveGreen")
1806 ("RawSienna") ("Sepia") ("Brown") ("Tan") ("Gray") ("Black") ("White"))
1807 "Colors defined in $TEXMF/tex/plain/dvips/colordvi.tex")
1809 (defvar YaTeX:latex2e-basic-color-alist
1810 '(("black") ("white") ("red") ("blue") ("yellow") ("green") ("cyan")
1811 ("magenta"))
1812 "Basic colors")
1814 (defun YaTeX:textcolor ()
1815 "Add-in for \\color's option"
1816 (if (y-or-n-p "Use `named' color? ")
1817 "[named]"))
1819 (defun YaTeX::color-completing-read (prompt)
1820 (let ((completion-ignore-case t)
1821 (namedp (save-excursion
1822 (skip-chars-backward "^\n\\[\\\\")
1823 (looking-at "named"))))
1824 (completing-read
1825 prompt
1826 (if namedp
1827 YaTeX:latex2e-named-color-alist
1828 YaTeX:latex2e-basic-color-alist)
1829 nil t)))
1831 (defun YaTeX::textcolor (argp)
1832 "Add-in for \\color's argument"
1833 (cond
1834 ((= argp 1) (YaTeX::color-completing-read "Color: "))
1835 ((= argp 2) (read-string "Colored string: "))))
1837 (fset 'YaTeX:color 'YaTeX:textcolor)
1838 (fset 'YaTeX::color 'YaTeX::textcolor)
1839 (fset 'YaTeX:colorbox 'YaTeX:textcolor)
1840 (fset 'YaTeX::colorbox 'YaTeX::textcolor)
1841 (fset 'YaTeX:fcolorbox 'YaTeX:textcolor)
1842 (fset 'YaTeX:pagecolor 'YaTeX:textcolor)
1843 (fset 'YaTeX::pagecolor 'YaTeX::textcolor)
1845 (defun YaTeX::fcolorbox (argp)
1846 (cond
1847 ((= argp 1) (YaTeX::color-completing-read "Frame color: "))
1848 ((= argp 2) (YaTeX::color-completing-read "Inner color: "))
1849 ((= argp 3) (read-string "Colored string: "))))
1851 (defun YaTeX:scalebox ()
1852 "Add-in for \\scalebox"
1853 (let ((vmag (read-string
1854 (if YaTeX-japan "倍率(負で反転): "
1855 "Magnification(Negative for flipped): ")))
1856 (hmag (read-string (if YaTeX-japan "縦倍率(省略可): "
1857 "Vertical magnification(Optional): "))))
1858 (if (and hmag (string< "" hmag))
1859 (format "{%s}[%s]" vmag hmag)
1860 (format "{%s}" vmag))))
1862 (defun YaTeX:rotatebox ()
1863 "Optional argument add-in for \\rotatebox"
1864 (message "Rotate origin? (N)one (O)rigin (X)-Y: ")
1865 (let ((c (read-char)) r (defx "x=mm") x (defy "y=mm") y something)
1866 (cond
1867 ((memq c '(?O ?o))
1868 (if (string< "" (setq r (YaTeX:read-oneof "htbpB")))
1869 (concat "[origin=" r "]")))
1870 ((memq c '(?X ?x ?Y ?y))
1871 (setq r (read-string "" (if YaTeX-emacs-19 (cons defx 3) defx))
1872 x (if (string< "x=" r) r)
1873 r (read-string "" (if YaTeX-emacs-19 (cons defy 3) defy))
1874 y (if (string< "y=" r) r)
1875 something (or x y))
1876 (format "%s%s%s%s%s"
1877 (if something "[" "")
1878 (if x x "")
1879 (if (and x y) "," "")
1880 (if y y "")
1881 (if something "]" ""))))))
1883 (defun YaTeX::rotatebox (argp)
1884 "Argument add-in for \\rotatebox"
1885 (cond
1886 ((= argp 1)
1887 (read-string (if YaTeX-japan "回転角(度; 左回り): "
1888 "Angle in degree(unclockwise): ")))
1889 ((= argp 2)
1890 (read-string (if YaTeX-japan "テキスト: " "Text: ")))))
1892 (defun YaTeX:includegraphics ()
1893 "Add-in for \\includegraphics's option"
1894 (let (width height (scale "") angle str)
1895 (setq width (YaTeX-read-string-or-skip "Width: ")
1896 height (YaTeX-read-string-or-skip "Height: "))
1897 (or (string< "" width) (string< "" height)
1898 (setq scale (YaTeX-read-string-or-skip "Scale: ")))
1899 (setq angle (YaTeX-read-string-or-skip "Angle(0-359): "))
1900 (setq str
1901 (mapconcat
1902 'concat
1903 (delq nil
1904 (mapcar '(lambda (s)
1905 (and (stringp (symbol-value s))
1906 (string< "" (symbol-value s))
1907 (format "%s=%s" s (symbol-value s))))
1908 '(width height scale angle)))
1909 ","))
1910 (if (string= "" str) ""
1911 (concat "[" str "]"))))
1913 (defun YaTeX::includegraphics (argp)
1914 "Add-in for \\includegraphics"
1915 (let ((imgfile (YaTeX::include argp "Image File: "))
1916 (case-fold-search t) info bb noupdate needclose c)
1917 (and (string-match "\\.\\(jpe?g\\|png\\|gif\\|bmp\\)$" imgfile)
1918 (file-exists-p imgfile)
1919 (or (fboundp 'yahtml-get-image-info)
1920 (progn
1921 (load "yahtml" t) (featurep 'yahtml))) ;(require 'yahtml nil t)
1922 (setq info (yahtml-get-image-info imgfile))
1923 (car info) ;if has width value
1924 (car (cdr info)) ;if has height value
1925 (setq bb (format "bb=%d %d %d %d" 0 0 (car info) (car (cdr info))))
1926 (save-excursion
1927 (cond
1928 ((and (save-excursion
1929 (YaTeX-re-search-active-backward
1930 "\\\\\\(includegraphics\\)\\|\\(bb=[-+ \t0-9]+\\)"
1931 YaTeX-comment-prefix nil t))
1932 (match-beginning 2)
1933 (not (setq noupdate (equal (YaTeX-match-string 2) bb)))
1934 (y-or-n-p (format "Update `bb=' line to `%s'?: " bb)))
1935 (message "")
1936 (replace-match bb))
1937 (noupdate nil)
1938 ((and (match-beginning 1)
1939 (prog2
1940 (message "Insert `%s'? Y)es N)o C)yes+`clip': " bb)
1941 (memq (setq c (read-char)) '(?y ?Y ?\ ?c ?C))
1942 (message "")))
1943 (goto-char (match-end 0))
1944 (message "")
1945 (if (looking-at "\\[") (forward-char 1)
1946 (insert-before-markers "[")
1947 (setq needclose t))
1948 (insert-before-markers bb)
1949 (if (memq c '(?c ?C)) (insert-before-markers ",clip"))
1950 (if needclose (insert-before-markers "]")
1951 (or (looking-at "\\]") (insert-before-markers ","))))
1952 (t (YaTeX-push-to-kill-ring bb)))))
1953 (setq YaTeX-section-name "caption")
1954 imgfile))
1956 (defun YaTeX::verbfile (argp)
1957 "Add-in for \\verbfile"
1958 (YaTeX::include argp "Virbatim File: "))
1960 (defun YaTeX:caption ()
1961 (setq YaTeX-section-name "label")
1962 nil)
1965 (defvar YaTeX::usepackage-alist-default
1966 '(("version") ("plext") ("url") ("fancybox") ("pifont") ("longtable")
1967 ("ascmac") ("bm") ("graphics") ("graphicx") ("alltt") ("misc") ("eclbkbox")
1968 ("amsmath") ("amssymb") ("xymtex") ("chemist")
1969 ("a4j") ("array") ("epsf") ("color") ("epsfig") ("floatfig")
1970 ("landscape") ("path") ("supertabular") ("twocolumn")
1971 ("latexsym") ("times") ("makeidx"))
1972 "Default completion table for arguments of \\usepackage")
1974 (defvar YaTeX::usepackage-alist-private nil
1975 "*Private completion list of the argument for usepackage")
1977 (defvar YaTeX::usepackage-alist-local nil
1978 "Directory local completion list of the argument for usepackage")
1980 (defun YaTeX::usepackage (&optional argp)
1981 (cond
1982 ((equal argp 1)
1983 (setq YaTeX-env-name "document")
1984 (let ((minibuffer-local-completion-map YaTeX-minibuffer-completion-map)
1985 (delim ","))
1986 (YaTeX-cplread-with-learning
1987 (if YaTeX-japan "Use package(カンマで区切ってOK): "
1988 "Use package(delimitable by comma): ")
1989 'YaTeX::usepackage-alist-default
1990 'YaTeX::usepackage-alist-private
1991 'YaTeX::usepackage-alist-local)))))
1993 (defun YaTeX::mask (argp)
1994 (cond
1995 ((equal argp 1)
1996 (read-string "String: "))
1997 ((equal argp 2)
1998 (let (c)
1999 (while (not (memq c '(?A ?B ?C ?D ?E ?F ?G ?H ?I ?J ?K)))
2000 (message "Mask type(A..K): ")
2001 (setq c (upcase (read-char))))
2002 (format "%c" c)))))
2004 (defun YaTeX::maskbox (argp)
2005 (cond
2006 ((equal argp 1)
2007 (read-string "Width: "))
2008 ((equal argp 2)
2009 (read-string "Height: "))
2010 ((equal argp 3)
2011 (let (c)
2012 (while (not (memq c '(?A ?B ?C ?D ?E ?F ?G ?H ?I ?J ?K)))
2013 (message "Mask type(A..K): ")
2014 (setq c (upcase (read-char))))
2015 (format "%c" c)))
2016 ((equal argp 4)
2017 (YaTeX:read-oneof "lcr" 'quick))
2018 ((equal argp 5)
2019 (read-string "String: "))))
2021 (defun YaTeX::textcircled (argp)
2022 (cond
2023 ((equal argp 1)
2024 (let ((char (read-string "Circled char: "))
2025 (left "") (right "") c)
2026 (setq c (read-char
2027 "Enclose also with (s)mall (t)iny s(C)riptsize (N)one:"))
2028 (cond
2029 ((memq c '(?s ?S)) (setq left "{\\small " right "}"))
2030 ((memq c '(?t ?T)) (setq left "{\\tiny " right "}"))
2031 ((memq c '(?c ?C)) (setq left "{\\scriptsize " right "}")))
2032 (format "%s%s%s" left char right)))))
2034 ;;; -------------------- math-mode stuff --------------------
2035 (defun YaTeX::tilde (&optional pos)
2036 "For accent macros in mathmode"
2037 (cond
2038 ((equal pos 1)
2039 (message "Put accent on variable: ")
2040 (let ((v (char-to-string (read-char))) (case-fold-search nil))
2041 (message "")
2042 (cond
2043 ((string-match "i\\|j" v)
2044 (concat "\\" v "math"))
2045 ((string-match "[\r\n\t ]" v)
2046 "")
2047 (t v))))
2048 (nil "")))
2050 (fset 'YaTeX::hat 'YaTeX::tilde)
2051 (fset 'YaTeX::check 'YaTeX::tilde)
2052 (fset 'YaTeX::bar 'YaTeX::tilde)
2053 (fset 'YaTeX::dot 'YaTeX::tilde)
2054 (fset 'YaTeX::ddot 'YaTeX::tilde)
2055 (fset 'YaTeX::vec 'YaTeX::tilde)
2057 (defun YaTeX::widetilde (&optional pos)
2058 "For multichar accent macros in mathmode"
2059 (cond
2060 ((equal pos 1)
2061 (let ((m "Put over chars[%s ]: ") v v2)
2062 (message m " ")
2063 (setq v (char-to-string (read-char)))
2064 (message "")
2065 (if (string-match "[\r\n\t ]" v)
2066 ""
2067 (message m v)
2068 (setq v2 (char-to-string (read-char)))
2069 (message "")
2070 (if (string-match "[\r\n\t ]" v2)
2072 (concat v v2)))))
2073 (nil "")))
2075 (fset 'YaTeX::widehat 'YaTeX::widetilde)
2076 (fset 'YaTeX::overline 'YaTeX::widetilde)
2077 (fset 'YaTeX::overrightarrow 'YaTeX::widetilde)
2080 ; for \frac{}{} region
2081 (defun YaTeX::frac-region (beg end)
2082 (if (catch 'done
2083 (while (re-search-forward "\\s *\\(\\\\over\\|/\\)\\s *" end t)
2084 (goto-char (match-beginning 0))
2085 (if (y-or-n-p
2086 (format "Replace this `%s' with `}{'" (YaTeX-match-string 0)))
2087 (throw 'done t))
2088 (goto-char (match-end 0))))
2089 (let (p (b0 (match-beginning 0)) e0)
2090 (replace-match "}{")
2091 (setq e0 (point))
2092 (save-restriction
2093 (narrow-to-region beg end)
2094 (goto-char e0)
2095 (skip-chars-forward " \t")
2096 (setq p (point))
2097 (YaTeX-goto-corresponding-paren)
2098 (forward-char 1)
2099 (skip-chars-forward " \t\r\n")
2100 (if (= end (1+ (point)))
2101 (progn
2102 (goto-char p)
2103 (if (looking-at "\\\\") (forward-char 1))
2104 (YaTeX-kill-paren nil)))
2105 (goto-char beg)
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 (>= (point) b0)
2112 (progn
2113 (goto-char p)
2114 (if (looking-at "\\\\") (forward-char 1))
2115 (YaTeX-kill-paren nil))))))
2116 (message ""))
2118 (defun YaTeX::DeclareMathOperator (argp)
2119 (cond
2120 ((equal argp 1)
2121 (read-string "Operator: " "\\"))))
2123 ;;;
2124 ;; Add-in functions for large-type command.
2125 ;;;
2126 (defun YaTeX:em ()
2127 (cond
2128 ((eq YaTeX-current-completion-type 'large) "\\/")
2129 (t nil)))
2130 (fset 'YaTeX:it 'YaTeX:em)
2132 ;;; -------------------- End of yatexadd --------------------
2133 (provide 'yatexadd)
2134 ; Local variables:
2135 ; fill-prefix: ";;; "
2136 ; paragraph-start: "^$\\| \\|;;;$"
2137 ; paragraph-separate: "^$\\| \\|;;;$"
2138 ; coding: sjis
2139 ; End: