yatex

view yatexadd.el @ 395:5aa8208fbef0

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