yatex

view yatexadd.el @ 219:ad4e0a008972

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