yatex

view yatexadd.el @ 452:2d9589a786d1

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