yatex

view yatexadd.el @ 425:3c29f87e383b

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