yatex

view yatexadd.el @ 394:67fa6d791bc9

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