yatex

view yatexadd.el @ 48:a0640ff3f72f

Inter-file ref/label, cite/bibitem jump.
author yuuji
date Mon, 31 Oct 1994 17:49:43 +0000
parents cd1b63102eed
children eb0512bfcb7f
line source
1 ;;; -*- Emacs-Lisp -*-
2 ;;; YaTeX add-in functions.
3 ;;; yatexadd.el rev.9
4 ;;; (c )1991-1994 by HIROSE Yuuji.[yuuji@ae.keio.ac.jp]
5 ;;; Last modified Mon Oct 31 12:55:54 1994 on pajero
6 ;;; $Id$
8 ;;;
9 ;;Sample functions for LaTeX environment.
10 ;;;
11 (defvar YaTeX:tabular-default-rule
12 "@{\\vrule width 1pt\\ }c|c|c@{\\ \\vrule width 1pt}"
13 "*Your favorite default rule format."
14 )
15 (defvar YaTeX:tabular-thick-vrule "\\vrule width %s"
16 "*Vertical thick line format (without @{}). %s'll be replaced by its width."
17 )
18 (defvar YaTeX:tabular-thick-hrule "\\noalign{\\hrule height %s}"
19 "*Horizontal thick line format. %s will be replaced by its width."
20 )
21 (defun YaTeX:tabular ()
22 "YaTeX add-in function for tabular environment.
23 Notice that this function refers the let-variable `env' in
24 YaTeX-make-begin-end."
25 (let ((width "") bars (rule "") (and "") (j 1) loc ans (hline "\\hline"))
26 (if (string= env "tabular*")
27 (setq width (concat "{" (read-string "Width: ") "}")))
28 (setq loc (YaTeX:read-position "tb")
29 bars (string-to-int
30 (read-string "Number of columns(0 for default format): " "3")))
31 (if (<= bars 0)
32 (setq ;if 0, simple format
33 rule YaTeX:tabular-default-rule
34 and "& &")
35 (while (< j bars) ;repeat bars-1 times
36 (setq rule (concat rule "c|")
37 and (concat and "& ")
38 j (1+ j)))
39 (setq rule (concat rule "c"))
40 (message "(N)ormal-frame or (T)hick frame? [nt]")
41 (setq ans (read-char))
42 (cond
43 ((or (equal ans ?t) (equal ans ?T))
44 (setq ans (read-string "Rule width: " "1pt")
45 rule (concat
46 "@{" (format YaTeX:tabular-thick-vrule ans) "}"
47 rule
48 "@{\\ " (format YaTeX:tabular-thick-vrule ans) "}")
49 hline (format YaTeX:tabular-thick-hrule ans)))
50 (t (setq rule (concat "|" rule "|")
51 hline "\\hline"))))
53 (setq rule (read-string "rule format: " rule))
55 (message "Dont forget to remove null line at the end of tabular.")
56 (format "%s%s{%s}%s"
57 width loc rule
58 (if (and (boundp 'region-mode) region-mode)
59 "" ;do nothing in region-mode
60 (format "\n%s\n%s \\\\ \\hline\n%s\n\\\\ %s"
61 hline and and hline))))
62 )
63 (fset 'YaTeX:tabular* 'YaTeX:tabular)
64 (defun YaTeX:array ()
65 (concat (YaTeX:read-position "tb")
66 "{" (read-string "Column format: ") "}")
67 )
69 (defun YaTeX:read-oneof (oneof)
70 (let ((pos "") loc (guide ""))
71 (and (boundp 'name) name (setq guide (format "%s " name)))
72 (while (not (string-match
73 (setq loc (read-key-sequence
74 (format "%s position (`%s') [%s]: "
75 guide oneof pos)));name is in YaTeX-addin
76 "\r\^g\n"))
77 (cond
78 ((string-match loc oneof)
79 (if (not (string-match loc pos))
80 (setq pos (concat pos loc))))
81 ((and (string-match loc "\C-h\C-?") (> (length pos) 0))
82 (setq pos (substring pos 0 (1- (length pos)))))
83 (t
84 (ding)
85 (message "Please input one of `%s'." oneof)
86 (sit-for 3))))
87 (message "")
88 pos)
89 )
91 (defun YaTeX:read-position (oneof)
92 "Read a LaTeX (optional) position format such as `[htbp]'."
93 (let ((pos (YaTeX:read-oneof oneof)))
94 (if (string= pos "") "" (concat "[" pos "]")))
95 )
97 (defun YaTeX:table ()
98 "YaTeX add-in function for table environment."
99 (YaTeX:read-position "htbp")
100 )
102 (fset 'YaTeX:figure 'YaTeX:table)
103 (fset 'YaTeX:figure* 'YaTeX:table)
106 (defun YaTeX:description ()
107 "Truly poor service:-)"
108 (setq single-command "item[]")
109 ""
110 )
112 (defun YaTeX:itemize ()
113 "It's also poor service."
114 (setq single-command "item")
115 ""
116 )
118 (fset 'YaTeX:enumerate 'YaTeX:itemize)
120 (defun YaTeX:picture ()
121 "Ask the size of coordinates of picture environment."
122 (concat (YaTeX:read-coordinates "Picture size")
123 (YaTeX:read-coordinates "Initial position"))
124 )
126 (defun YaTeX:equation ()
127 (if (fboundp 'YaTeX-toggle-math-mode)
128 (YaTeX-toggle-math-mode t)) ;force math-mode ON.
129 )
130 (fset 'YaTeX:eqnarray 'YaTeX:equation)
131 (fset 'YaTeX:displaymath 'YaTeX:equation)
133 (defun YaTeX:list ()
134 "%\n{} %default label\n{} %formatting parameter"
135 )
137 (defun YaTeX:minipage ()
138 (concat (YaTeX:read-position "cbt")
139 "{" (read-string "Width: ") "}")
140 )
142 ;;;
143 ;;Sample functions for section-type command.
144 ;;;
145 (defun YaTeX:multiput ()
146 (concat (YaTeX:read-coordinates "Pos")
147 (YaTeX:read-coordinates "Step")
148 "{" (read-string "How many times: ") "}")
149 )
151 (defun YaTeX:put ()
152 (YaTeX:read-coordinates "Pos")
153 )
155 (defun YaTeX:makebox ()
156 (cond
157 ((YaTeX-in-environment-p "picture")
158 (concat (YaTeX:read-coordinates "Dimension")
159 (YaTeX:read-position "lrtb")))
160 (t
161 (let ((width (read-string "Width: ")))
162 (if (string< "" width)
163 (progn
164 (or (equal (aref width 0) ?\[)
165 (setq width (concat "[" width "]")))
166 (concat width (YaTeX:read-position "lr")))))))
167 )
169 (defun YaTeX:framebox ()
170 (if (YaTeX-quick-in-environment-p "picture")
171 (YaTeX:makebox))
172 )
174 (defun YaTeX:dashbox ()
175 (concat "{" (read-string "Dash dimension: ") "}"
176 (YaTeX:read-coordinates "Dimension"))
177 )
179 (defun YaTeX:left ()
180 (let (c)
181 (while (not (string-match
182 (progn (message "Which parenthesis? One of [{(|)}]: ")
183 (setq c (regexp-quote (char-to-string (read-char)))))
184 "[{(|)}]")))
185 (setq single-command "right")
186 (cond
187 ((string-match c "[(|)]") c)
188 (t (concat "\\" c))))
189 )
190 (fset 'YaTeX:right 'YaTeX:left)
192 (defun YaTeX:read-coordinates (&optional mes varX varY)
193 (concat
194 "("
195 (read-string (format "%s %s: " (or mes "Dimension") (or varX "X")))
196 ","
197 (read-string (format "%s %s: " (or mes "Dimension") (or varY "Y")))
198 ")")
199 )
201 ;;;
202 ;;Sample functions for maketitle-type command.
203 ;;;
204 (defun YaTeX:sum ()
205 "Read range of summation."
206 (YaTeX:check-completion-type 'maketitle)
207 (concat (YaTeX:read-boundary "_") (YaTeX:read-boundary "^"))
208 )
210 (fset 'YaTeX:int 'YaTeX:sum)
212 (defun YaTeX:lim ()
213 "Insert limit notation of \\lim."
214 (YaTeX:check-completion-type 'maketitle)
215 (let ((var (read-string "Variable: ")) limit)
216 (if (string= "" var) ""
217 (setq limit (read-string "Limit ($ means infinity): "))
218 (if (string= "$" limit) (setq limit "\\infty"))
219 (concat "_{" var " \\rightarrow " limit "}")))
220 )
222 (defun YaTeX:gcd ()
223 "Add-in function for \\gcd(m,n)."
224 (YaTeX:check-completion-type 'maketitle)
225 (YaTeX:read-coordinates "\\gcd" "(?,)" "(,?)")
226 )
228 (defun YaTeX:read-boundary (ULchar)
229 "Read boundary usage by _ or ^. _ or ^ is indicated by argument ULchar."
230 (let ((bndry (read-string (concat ULchar "{???} ($ for infinity): "))))
231 (if (string= bndry "") ""
232 (if (string= bndry "$") (setq bndry "\\infty"))
233 (concat ULchar "{" bndry "}")))
234 )
236 (defun YaTeX:verb ()
237 "Enclose \\verb's contents with the same characters."
238 (let ((quote-char (read-string "Quoting char: " "|"))
239 (contents (read-string "Quoted contents: ")))
240 (concat quote-char contents quote-char))
241 )
242 (fset 'YaTeX:verb* 'YaTeX:verb)
244 (defun YaTeX:footnotemark ()
245 (setq section-name "footnotetext")
246 nil
247 )
249 (defun YaTeX:cite ()
250 (let ((comment (read-string "Comment for citation: ")))
251 (if (string= comment "") ""
252 (concat "[" comment "]")))
253 )
255 (defun YaTeX:bibitem ()
256 (let ((label (read-string "Citation label: ")))
257 (if (string= label "") ""
258 (concat "[" label "]")))
259 )
261 ;;;
262 ;;Subroutine
263 ;;;
265 (defun YaTeX:check-completion-type (type)
266 "Check valid completion type."
267 (if (not (eq type YaTeX-current-completion-type))
268 (error "This should be completed with %s-type completion." type))
269 )
272 ;;;
273 ;;; [[Add-in functions for reading section arguments]]
274 ;;;
275 ;; All of add-in functions for reading sections arguments should
276 ;; take an argument ARGP that specify the argument position.
277 ;; If argument position is out of range, nil should be returned,
278 ;; else nil should NOT be returned.
280 ;;
281 ; Label selection
282 ;;
283 (defvar YaTeX-label-menu-other
284 (if YaTeX-japan "':他のバッファのラベル\n" "':LABEL IN OTHER BUFFER.\n"))
285 (defvar YaTeX-label-menu-repeat
286 (if YaTeX-japan ".:直前の\\refと同じ\n" "/:REPEAT LAST \ref{}\n"))
287 (defvar YaTeX-label-menu-any
288 (if YaTeX-japan "*:任意の文字列\n" "*:ANY STRING.\n"))
289 (defvar YaTeX-label-buffer "*Label completions*")
290 (defvar YaTeX-label-guide-msg "Select label and hit RETURN.")
291 (defvar YaTeX-label-select-map nil
292 "Key map used in label selection buffer.")
293 (defun YaTeX::label-setup-key-map ()
294 (if YaTeX-label-select-map nil
295 (message "Setting up label selection mode map...")
296 (setq YaTeX-label-select-map (copy-keymap global-map))
297 (suppress-keymap YaTeX-label-select-map)
298 (substitute-all-key-definition
299 'previous-line 'YaTeX::label-previous YaTeX-label-select-map)
300 (substitute-all-key-definition
301 'next-line 'YaTeX::label-next YaTeX-label-select-map)
302 (define-key YaTeX-label-select-map "\C-n" 'YaTeX::label-next)
303 (define-key YaTeX-label-select-map "\C-p" 'YaTeX::label-previous)
304 (define-key YaTeX-label-select-map "<" 'beginning-of-buffer)
305 (define-key YaTeX-label-select-map ">" 'end-of-buffer)
306 (define-key YaTeX-label-select-map "\C-m" 'exit-recursive-edit)
307 (define-key YaTeX-label-select-map "\C-j" 'exit-recursive-edit)
308 (define-key YaTeX-label-select-map " " 'exit-recursive-edit)
309 (define-key YaTeX-label-select-map "\C-g" 'abort-recursive-edit)
310 (define-key YaTeX-label-select-map "/" 'isearch-forward)
311 (define-key YaTeX-label-select-map "?" 'isearch-backward)
312 (define-key YaTeX-label-select-map "'" 'YaTeX::label-search-tag)
313 (define-key YaTeX-label-select-map "." 'YaTeX::label-search-tag)
314 (define-key YaTeX-label-select-map "*" 'YaTeX::label-search-tag)
315 (message "Setting up label selection mode map...Done")
316 (let ((key ?A))
317 (while (<= key ?Z)
318 (define-key YaTeX-label-select-map (char-to-string key)
319 'YaTeX::label-search-tag)
320 (define-key YaTeX-label-select-map (char-to-string (+ key (- ?a ?A)))
321 'YaTeX::label-search-tag)
322 (setq key (1+ key)))))
323 )
324 (defun YaTeX::label-next ()
325 (interactive) (forward-line 1) (message YaTeX-label-guide-msg))
326 (defun YaTeX::label-previous ()
327 (interactive) (forward-line -1) (message YaTeX-label-guide-msg))
328 (defun YaTeX::label-search-tag ()
329 (interactive)
330 (let ((case-fold-search t) (tag (regexp-quote (this-command-keys))))
331 (cond
332 ((save-excursion
333 (forward-char 1)
334 (re-search-forward (concat "^" tag) nil t))
335 (goto-char (match-beginning 0)))
336 ((save-excursion
337 (goto-char (point-min))
338 (re-search-forward (concat "^" tag) nil t))
339 (goto-char (match-beginning 0))))
340 (message YaTeX-label-guide-msg))
341 )
342 (defun YaTeX::ref (argp &optional labelcmd refcmd)
343 (cond
344 ((= argp 1)
345 (save-excursion
346 (let ((lnum 0) e0 label label-list (buf (current-buffer))
347 (labelcmd (or labelcmd "label")) (refcmd (or refcmd "ref"))
348 (p (point)) initl line)
349 (goto-char (point-min))
350 (message "Collecting labels...")
351 (save-window-excursion
352 (YaTeX-showup-buffer
353 YaTeX-label-buffer (function (lambda (x) (window-width x))))
354 (with-output-to-temp-buffer YaTeX-label-buffer
355 (while (YaTeX-re-search-active-forward
356 (concat "\\\\" labelcmd)
357 (regexp-quote YaTeX-comment-prefix) nil t)
358 (goto-char (match-beginning 0))
359 (skip-chars-forward "^{")
360 (setq label
361 (buffer-substring
362 (1+ (point))
363 (prog2 (forward-list 1) (setq e0 (1- (point)))))
364 label-list (cons label label-list))
365 (or initl
366 (if (< p (point)) (setq initl lnum)))
367 (beginning-of-line)
368 (skip-chars-forward " \t\n" nil)
369 (princ (format "%c:{%s}\t<<%s>>\n" (+ (% lnum 26) ?A) label
370 (buffer-substring (point) (point-end-of-line))))
371 (setq lnum (1+ lnum))
372 (message "Collecting \\%s{}... %d" labelcmd lnum)
373 (goto-char e0))
374 (princ YaTeX-label-menu-other)
375 (princ YaTeX-label-menu-repeat)
376 (princ YaTeX-label-menu-any)
377 );with
378 (goto-char p)
379 (message "Collecting %s...Done" labelcmd)
380 (pop-to-buffer YaTeX-label-buffer)
381 (YaTeX::label-setup-key-map)
382 (setq truncate-lines t)
383 (setq buffer-read-only t)
384 (use-local-map YaTeX-label-select-map)
385 (message YaTeX-label-guide-msg)
386 (goto-line (or initl lnum)) ;goto recently defined label line
387 (unwind-protect
388 (progn
389 (recursive-edit)
390 (set-buffer (get-buffer YaTeX-label-buffer)) ;assertion
391 (beginning-of-line)
392 (setq line (count-lines (point-min)(point)))
393 (cond
394 ((= line lnum) (setq label (YaTeX-label-other)))
395 ((= line (1+ lnum))
396 (save-excursion
397 (switch-to-buffer buf)
398 (goto-char p)
399 (if (re-search-backward
400 (concat "\\\\" refcmd "{\\([^}]+\\)}") nil t)
401 (setq label (YaTeX-match-string 1))
402 (setq label ""))))
403 ((>= line (+ lnum 2))
404 (setq label (read-string (format "\\%s{???}: " refcmd))))
405 (t (setq label (nth (- lnum line 1) label-list)))))
406 (bury-buffer YaTeX-label-buffer)))
407 label
408 ))
409 ))
410 )
411 (fset 'YaTeX::pageref 'YaTeX::ref)
412 (defun YaTeX::cite (argp)
413 (cond
414 ((eq argp 1)
415 (YaTeX::ref argp "bibitem\\(\\[.*\\]\\)?" "cite"))
416 (t nil)))
418 (defun YaTeX-yatex-buffer-list ()
419 (save-excursion
420 (delq nil (mapcar (function (lambda (buf)
421 (set-buffer buf)
422 (if (eq major-mode 'yatex-mode) buf)))
423 (buffer-list))))
424 )
426 (defun YaTeX-select-other-yatex-buffer ()
427 "Select buffer from all yatex-mode's buffers interactivelly."
428 (interactive)
429 (let ((lbuf "*YaTeX mode buffers*") (blist (YaTeX-yatex-buffer-list))
430 (lnum -1) buf rv
431 (ff "**find-file**"))
432 (YaTeX-showup-buffer
433 lbuf (function (lambda (x) 1))) ;;Select next window surely.
434 (with-output-to-temp-buffer lbuf
435 (while blist
436 (princ
437 (format "%c:{%s}\n" (+ (% (setq lnum (1+ lnum)) 26) ?A)
438 (buffer-name (car blist))))
439 (setq blist (cdr blist)))
440 (princ (format "':{%s}" ff)))
441 (pop-to-buffer lbuf)
442 (YaTeX::label-setup-key-map)
443 (setq buffer-read-only t)
444 (use-local-map YaTeX-label-select-map)
445 (message YaTeX-label-guide-msg)
446 (unwind-protect
447 (progn
448 (recursive-edit)
449 (set-buffer lbuf)
450 (beginning-of-line)
451 (setq rv
452 (if (re-search-forward "{\\([^\\}]+\\)}" (point-end-of-line) t)
453 (buffer-substring (match-beginning 1) (match-end 1)) nil)))
454 (kill-buffer lbuf))
455 (if (string= rv ff)
456 (progn
457 (call-interactively 'find-file)
458 (current-buffer))
459 rv))
460 )
462 (defun YaTeX-label-other ()
463 (let ((rv (YaTeX-select-other-yatex-buffer)))
464 (cond
465 ((null rv) "")
466 (t
467 (set-buffer rv)
468 (YaTeX::ref argp labelcmd refcmd)))
469 )
470 )
472 ;;
473 ; completion for the arguments of \newcommand
474 ;;
475 (defun YaTeX::newcommand (&optional argp)
476 (cond
477 ((= argp 1)
478 (let ((command (read-string "Define newcommand: " "\\")))
479 (put 'YaTeX::newcommand 'command (substring command 1))
480 command))
481 ((= argp 2)
482 (let ((argc
483 (string-to-int (read-string "Number of arguments(Default 0): ")))
484 (def (read-string "Definition: "))
485 (command (get 'YaTeX::newcommand 'command)))
486 ;;!!! It's illegal to insert string in the add-in function !!!
487 (if (> argc 0) (insert (format "[%d]" argc)))
488 (if (and (stringp command)
489 (string< "" command)
490 (y-or-n-p "Update user completion table?"))
491 (cond
492 ((= argc 0)
493 (YaTeX-update-table
494 (list command)
495 'singlecmd-table 'user-singlecmd-table 'tmp-singlecmd-table))
496 ((= argc 1)
497 (YaTeX-update-table
498 (list command)
499 'section-table 'user-section-table 'tmp-section-table))
500 (t (YaTeX-update-table
501 (list command argc)
502 'section-table 'user-section-table 'tmp-section-table))))
503 (message "")
504 def ;return command name
505 ))
506 (t ""))
507 )
509 ;;
510 ; completion for the arguments of \pagestyle
511 ;;
512 (defun YaTeX::pagestyle (&optional argp)
513 "Read the pagestyle with completion."
514 (completing-read
515 "Page style: "
516 '(("plain") ("empty") ("headings") ("myheadings") ("normal") nil))
517 )
518 ;;
519 ; completion for the arguments of \pagenumbering
520 ;;
521 (defun YaTeX::pagenumbering (&optional argp)
522 "Read the numbering style."
523 (completing-read
524 "Page numbering style: "
525 '(("arabic") ("Alpha") ("alpha") ("Roman") ("roman")))
526 )
528 ;;
529 ; Length
530 ;;
531 (defvar YaTeX:style-parameters-default
532 '(("\\arraycolsep")
533 ("\\arrayrulewidth")
534 ("\\baselineskip")
535 ("\\columnsep")
536 ("\\columnseprule")
537 ("\\doublerulesep")
538 ("\\evensidemargin")
539 ("\\footheight")
540 ("\\footskip")
541 ("\\headheight")
542 ("\\headsep")
543 ("\\itemindent")
544 ("\\itemsep")
545 ("\\labelsep")
546 ("\\labelwidth")
547 ("\\leftmargin")
548 ("\\linewidth")
549 ("\\listparindent")
550 ("\\marginparsep")
551 ("\\marginparwidth")
552 ("\\mathindent")
553 ("\\oddsidemargin")
554 ("\\parindent")
555 ("\\parsep")
556 ("\\parskip")
557 ("\\partopsep")
558 ("\\rightmargin")
559 ("\\tabcolsep")
560 ("\\textheight")
561 ("\\textwidth")
562 ("\\topmargin")
563 ("\\topsep")
564 ("\\topskip")
565 )
566 "Alist of LaTeX style parameters.")
567 (defvar YaTeX:style-parameters-private nil
568 "*User definable alist of style parameters.")
569 (defvar YaTeX:style-parameters-private nil
570 "Holds the union of LaTeX style parameters.")
571 (setq YaTeX:style-parameters
572 (append YaTeX:style-parameters-private YaTeX:style-parameters-default))
574 (defvar YaTeX:length-history nil "Holds history of length.")
575 (defun YaTeX::setlength (&optional argp)
576 "YaTeX add-in function for arguments of \\setlength."
577 (cond
578 ((equal 1 argp)
579 (completing-read "Length variable: " YaTeX:style-parameters nil nil "\\"))
580 ((equal 2 argp)
581 (let ((minibuffer-history-symbol 'YaTeX:length-history))
582 (read-string "Length: "))))
583 )
584 (fset 'YaTeX::addtolength 'YaTeX::setlength)
586 (defun YaTeX::settowidth (&optional argp)
587 "YaTeX add-in function for arguments of \\settowidth."
588 (cond
589 ((equal 1 argp)
590 (completing-read "Length variable: " YaTeX:style-parameters nil nil "\\"))
591 ((equal 2 argp)
592 (read-string "Text: ")))
593 )
594 (defun YaTeX::newlength (&optional argp)
595 "YaTeX add-in function for arguments of \\newlength"
596 (cond
597 ((equal argp 1)
598 (let ((length (read-string "Length variable: " "\\")))
599 (or (assoc length YaTeX:style-parameters-private)
600 (setq YaTeX:style-parameters-private
601 (cons (list length) YaTeX:style-parameters-private)
602 YaTeX:style-parameters
603 (cons (list length) YaTeX:style-parameters)))
604 length)))
605 )
607 ;; \multicolumn's arguments
608 (defun YaTeX::multicolumn (&optional argp)
609 "YaTeX add-in function for arguments of \\multicolumn."
610 (cond
611 ((equal 1 argp)
612 (read-string "Number of columns: "))
613 ((equal 2 argp)
614 (let (c)
615 (while (not (string-match
616 (progn (message "Format(one of l,r,c): ")
617 (setq c (char-to-string (read-char))))
618 "lrc")))
619 c))
620 ((equal 3 argp)
621 (read-string "Item: ")))
622 )
624 ;;; -------------------- End of yatexadd --------------------
625 (provide 'yatexadd)