yatex

view yatexadd.el @ 45:b0fc9c2950cd

Prepare for supporting Emacs-19.
author yuuji
date Sun, 24 Jul 1994 15:07:23 +0000
parents b00c74813e56
children cd1b63102eed
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 Jul 25 00:04:43 1994 on figaro
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 (defun YaTeX:description ()
103 "Truly poor service:-)"
104 (setq single-command "item[]")
105 ""
106 )
108 (defun YaTeX:itemize ()
109 "It's also poor service."
110 (setq single-command "item")
111 ""
112 )
114 (fset 'YaTeX:enumerate 'YaTeX:itemize)
116 (defun YaTeX:picture ()
117 "Ask the size of coordinates of picture environment."
118 (concat (YaTeX:read-coordinates "Picture size")
119 (YaTeX:read-coordinates "Initial position"))
120 )
122 (defun YaTeX:equation ()
123 (if (fboundp 'YaTeX-toggle-math-mode)
124 (YaTeX-toggle-math-mode t)) ;force math-mode ON.
125 )
126 (fset 'YaTeX:eqnarray 'YaTeX:equation)
127 (fset 'YaTeX:displaymath 'YaTeX:equation)
129 (defun YaTeX:list ()
130 "%\n{} %default label\n{} %formatting parameter"
131 )
133 (defun YaTeX:minipage ()
134 (concat (YaTeX:read-position "cbt")
135 "{" (read-string "Width: ") "}")
136 )
138 ;;;
139 ;;Sample functions for section-type command.
140 ;;;
141 (defun YaTeX:multiput ()
142 (concat (YaTeX:read-coordinates "Pos")
143 (YaTeX:read-coordinates "Step")
144 "{" (read-string "How many times: ") "}")
145 )
147 (defun YaTeX:put ()
148 (YaTeX:read-coordinates "Pos")
149 )
151 (defun YaTeX:makebox ()
152 (cond
153 ((YaTeX-in-environment-p "picture")
154 (concat (YaTeX:read-coordinates "Dimension")
155 (YaTeX:read-position "lrtb")))
156 (t
157 (let ((width (read-string "Width: ")))
158 (if (string< "" width)
159 (progn
160 (or (equal (aref width 0) ?\[)
161 (setq width (concat "[" width "]")))
162 (concat width (YaTeX:read-position "lr")))))))
163 )
165 (defun YaTeX:framebox ()
166 (if (YaTeX-quick-in-environment-p "picture")
167 (YaTeX:makebox))
168 )
170 (defun YaTeX:dashbox ()
171 (concat "{" (read-string "Dash dimension: ") "}"
172 (YaTeX:read-coordinates "Dimension"))
173 )
175 (defun YaTeX:left ()
176 (let (c)
177 (while (not (string-match
178 (progn (message "Which parenthesis? One of [{(|)}]: ")
179 (setq c (regexp-quote (char-to-string (read-char)))))
180 "[{(|)}]")))
181 (setq single-command "right")
182 (cond
183 ((string-match c "[(|)]") c)
184 (t (concat "\\" c))))
185 )
186 (fset 'YaTeX:right 'YaTeX:left)
188 (defun YaTeX:read-coordinates (&optional mes varX varY)
189 (concat
190 "("
191 (read-string (format "%s %s: " (or mes "Dimension") (or varX "X")))
192 ","
193 (read-string (format "%s %s: " (or mes "Dimension") (or varY "Y")))
194 ")")
195 )
197 ;;;
198 ;;Sample functions for maketitle-type command.
199 ;;;
200 (defun YaTeX:sum ()
201 "Read range of summation."
202 (YaTeX:check-completion-type 'maketitle)
203 (concat (YaTeX:read-boundary "_") (YaTeX:read-boundary "^"))
204 )
206 (fset 'YaTeX:int 'YaTeX:sum)
208 (defun YaTeX:lim ()
209 "Insert limit notation of \\lim."
210 (YaTeX:check-completion-type 'maketitle)
211 (let ((var (read-string "Variable: ")) limit)
212 (if (string= "" var) ""
213 (setq limit (read-string "Limit ($ means infinity): "))
214 (if (string= "$" limit) (setq limit "\\infty"))
215 (concat "_{" var " \\rightarrow " limit "}")))
216 )
218 (defun YaTeX:gcd ()
219 "Add-in function for \\gcd(m,n)."
220 (YaTeX:check-completion-type 'maketitle)
221 (YaTeX:read-coordinates "\\gcd" "(?,)" "(,?)")
222 )
224 (defun YaTeX:read-boundary (ULchar)
225 "Read boundary usage by _ or ^. _ or ^ is indicated by argument ULchar."
226 (let ((bndry (read-string (concat ULchar "{???} ($ for infinity): "))))
227 (if (string= bndry "") ""
228 (if (string= bndry "$") (setq bndry "\\infty"))
229 (concat ULchar "{" bndry "}")))
230 )
232 (defun YaTeX:verb ()
233 "Enclose \\verb's contents with the same characters."
234 (let ((quote-char (read-string "Quoting char: " "|"))
235 (contents (read-string "Quoted contents: ")))
236 (concat quote-char contents quote-char))
237 )
238 (fset 'YaTeX:verb* 'YaTeX:verb)
240 (defun YaTeX:footnotemark ()
241 (setq section-name "footnotetext")
242 nil
243 )
245 ;;;
246 ;;Subroutine
247 ;;;
249 (defun YaTeX:check-completion-type (type)
250 "Check valid completion type."
251 (if (not (eq type YaTeX-current-completion-type))
252 (error "This should be completed with %s-type completion." type))
253 )
256 ;;;
257 ;;; [[Add-in functions for reading section arguments]]
258 ;;;
259 ;; All of add-in functions for reading sections arguments should
260 ;; take an argument ARGP that specify the argument position.
261 ;; If argument position is out of range, nil should be returned,
262 ;; else nil should NOT be returned.
264 ;;
265 ; Label selection
266 ;;
267 (defvar YaTeX-label-menu-other
268 (if YaTeX-japan "':他のバッファのラベル\n" "':LABEL IN OTHER BUFFER.\n"))
269 (defvar YaTeX-label-menu-repeat
270 (if YaTeX-japan ".:直前の\\refと同じ\n" "/:REPEAT LAST \ref{}\n"))
271 (defvar YaTeX-label-menu-any
272 (if YaTeX-japan "*:任意の文字列\n" "*:ANY STRING.\n"))
273 (defvar YaTeX-label-buffer "*Label completions*")
274 (defvar YaTeX-label-guide-msg "Select label and hit RETURN.")
275 (defvar YaTeX-label-select-map nil
276 "Key map used in label selection buffer.")
277 (defun YaTeX::label-setup-key-map ()
278 (if YaTeX-label-select-map nil
279 (message "Setting up label selection mode map...")
280 (setq YaTeX-label-select-map (copy-keymap global-map))
281 (suppress-keymap YaTeX-label-select-map)
282 (substitute-all-key-definition
283 'previous-line 'YaTeX::label-previous YaTeX-label-select-map)
284 (substitute-all-key-definition
285 'next-line 'YaTeX::label-next YaTeX-label-select-map)
286 (define-key YaTeX-label-select-map "\C-n" 'YaTeX::label-next)
287 (define-key YaTeX-label-select-map "\C-p" 'YaTeX::label-previous)
288 (define-key YaTeX-label-select-map "<" 'beginning-of-buffer)
289 (define-key YaTeX-label-select-map ">" 'end-of-buffer)
290 (define-key YaTeX-label-select-map "\C-m" 'exit-recursive-edit)
291 (define-key YaTeX-label-select-map "\C-j" 'exit-recursive-edit)
292 (define-key YaTeX-label-select-map " " 'exit-recursive-edit)
293 (define-key YaTeX-label-select-map "\C-g" 'abort-recursive-edit)
294 (define-key YaTeX-label-select-map "/" 'isearch-forward)
295 (define-key YaTeX-label-select-map "?" 'isearch-backward)
296 (define-key YaTeX-label-select-map "'" 'YaTeX::label-search-tag)
297 (define-key YaTeX-label-select-map "." 'YaTeX::label-search-tag)
298 (define-key YaTeX-label-select-map "*" 'YaTeX::label-search-tag)
299 (message "Setting up label selection mode map...Done")
300 (let ((key ?A))
301 (while (<= key ?Z)
302 (define-key YaTeX-label-select-map (char-to-string key)
303 'YaTeX::label-search-tag)
304 (define-key YaTeX-label-select-map (char-to-string (+ key (- ?a ?A)))
305 'YaTeX::label-search-tag)
306 (setq key (1+ key)))))
307 )
308 (defun YaTeX::label-next ()
309 (interactive) (forward-line 1) (message YaTeX-label-guide-msg))
310 (defun YaTeX::label-previous ()
311 (interactive) (forward-line -1) (message YaTeX-label-guide-msg))
312 (defun YaTeX::label-search-tag ()
313 (interactive)
314 (let ((case-fold-search t) (tag (regexp-quote (this-command-keys))))
315 (cond
316 ((save-excursion
317 (forward-char 1)
318 (re-search-forward (concat "^" tag) nil t))
319 (goto-char (match-beginning 0)))
320 ((save-excursion
321 (goto-char (point-min))
322 (re-search-forward (concat "^" tag) nil t))
323 (goto-char (match-beginning 0))))
324 (message YaTeX-label-guide-msg))
325 )
326 (defun YaTeX::ref (argp)
327 (cond
328 ((= argp 1)
329 (save-excursion
330 (let ((lnum 0) e0 m1 e1 label label-list (buf (current-buffer))
331 (p (point)) initl line)
332 (goto-char (point-min))
333 (message "Collecting labels...")
334 (save-window-excursion
335 (YaTeX-showup-buffer
336 YaTeX-label-buffer (function (lambda (x) (window-width x))))
337 (with-output-to-temp-buffer YaTeX-label-buffer
338 (while (re-search-forward "\\label{\\([^}]+\\)}" nil t)
339 (setq e0 (match-end 0) m1 (match-beginning 1) e1 (match-end 1))
340 (if (search-backward
341 YaTeX-comment-prefix (point-beginning-of-line) t) nil
342 (setq label (buffer-substring m1 e1)
343 label-list (cons label label-list))
344 (or initl
345 (if (< p (point)) (setq initl lnum)))
346 (beginning-of-line)
347 (skip-chars-forward " \t\n" nil)
348 (princ (format "%c:{%s}\t<<%s>>\n" (+ (% lnum 26) ?A) label
349 (buffer-substring (point) (point-end-of-line))))
350 (setq lnum (1+ lnum))
351 (message "Collecting \\label{}... %d" lnum))
352 (goto-char e0))
353 (princ YaTeX-label-menu-other)
354 (princ YaTeX-label-menu-repeat)
355 (princ YaTeX-label-menu-any)
356 );with
357 (goto-char p)
358 (message "Collecting labels...Done")
359 (pop-to-buffer YaTeX-label-buffer)
360 (YaTeX::label-setup-key-map)
361 (setq truncate-lines t)
362 (setq buffer-read-only t)
363 (use-local-map YaTeX-label-select-map)
364 (message YaTeX-label-guide-msg)
365 (goto-line (or initl lnum)) ;goto recently defined label line
366 (unwind-protect
367 (progn
368 (recursive-edit)
369 (set-buffer (get-buffer YaTeX-label-buffer)) ;assertion
370 (beginning-of-line)
371 (setq line (count-lines (point-min)(point)))
372 (cond
373 ((= line lnum) (setq label (YaTeX-label-other)))
374 ((= line (1+ lnum))
375 (save-excursion
376 (switch-to-buffer buf)
377 (goto-char p)
378 (if (re-search-backward "\\\\ref{\\([^}]+\\)}" nil t)
379 (setq label (buffer-substring
380 (match-beginning 1) (match-end 1)))
381 (setq label ""))))
382 ((>= line (+ lnum 2))
383 (setq label (read-string "\\ref{???}: ")))
384 (t (setq label (nth (- lnum line 1) label-list)))))
385 (bury-buffer YaTeX-label-buffer)))
386 label
387 ))
388 ))
389 )
390 (fset 'YaTeX::pageref 'YaTeX::ref)
392 (defun YaTeX-label-other ()
393 (let ((lbuf "*YaTeX mode buffers*") (blist (buffer-list)) (lnum -1) buf rv
394 (ff "**find-file**"))
395 (YaTeX-showup-buffer
396 lbuf (function (lambda (x) 1))) ;;Select next window surely.
397 (with-output-to-temp-buffer lbuf
398 (while blist
399 (if (and (buffer-file-name (setq buf (car blist)))
400 (progn (set-buffer buf) (eq major-mode 'yatex-mode)))
401 (princ
402 (format "%c:{%s}\n" (+ (% (setq lnum (1+ lnum)) 26) ?A)
403 (buffer-name buf))))
404 (setq blist (cdr blist)))
405 (princ (format "':{%s}" ff)))
406 (pop-to-buffer lbuf)
407 (YaTeX::label-setup-key-map)
408 (setq buffer-read-only t)
409 (use-local-map YaTeX-label-select-map)
410 (message YaTeX-label-guide-msg)
411 (unwind-protect
412 (progn
413 (recursive-edit)
414 (set-buffer lbuf)
415 (beginning-of-line)
416 (setq rv
417 (if (re-search-forward "{\\([^\\}]+\\)}" (point-end-of-line) t)
418 (buffer-substring (match-beginning 1) (match-end 1)) nil)))
419 (kill-buffer lbuf))
420 (cond
421 ((null rv) "")
422 ((string= rv ff)
423 (call-interactively 'find-file)
424 (YaTeX::ref argp))
425 (t
426 (set-buffer rv)
427 (YaTeX::ref argp)))
428 )
429 )
431 ;;
432 ; completion for the arguments of \newcommand
433 ;;
434 (defun YaTeX::newcommand (&optional argp)
435 (cond
436 ((= argp 1)
437 (let ((command (read-string "Define newcommand: " "\\")))
438 (put 'YaTeX::newcommand 'command (substring command 1))
439 command))
440 ((= argp 2)
441 (let ((argc
442 (string-to-int (read-string "Number of arguments(Default 0): ")))
443 (def (read-string "Definition: "))
444 (command (get 'YaTeX::newcommand 'command)))
445 ;;!!! It's illegal to insert string in the add-in function !!!
446 (if (> argc 0) (insert (format "[%d]" argc)))
447 (if (and (stringp command)
448 (string< "" command)
449 (y-or-n-p "Update user completion table?"))
450 (cond
451 ((= argc 0)
452 (YaTeX-update-table
453 (list command)
454 'singlecmd-table 'user-singlecmd-table 'tmp-singlecmd-table))
455 ((= argc 1)
456 (YaTeX-update-table
457 (list command)
458 'section-table 'user-section-table 'tmp-section-table))
459 (t (YaTeX-update-table
460 (list command argc)
461 'section-table 'user-section-table 'tmp-section-table))))
462 (message "")
463 def ;return command name
464 ))
465 (t ""))
466 )
468 ;;
469 ; completion for the arguments of \pagestyle
470 ;;
471 (defun YaTeX::pagestyle (&optional argp)
472 "Read the pagestyle with completion."
473 (completing-read
474 "Page style: "
475 '(("plain") ("empty") ("headings") ("myheadings") ("normal") nil))
476 )
477 ;;
478 ; completion for the arguments of \pagenumbering
479 ;;
480 (defun YaTeX::pagenumbering (&optional argp)
481 "Read the numbering style."
482 (completing-read
483 "Page numbering style: "
484 '(("arabic") ("Alpha") ("alpha") ("Roman") ("roman")))
485 )
487 ;;
488 ; Length
489 ;;
490 (defvar YaTeX:style-parameters-default
491 '(("\\arraycolsep")
492 ("\\arrayrulewidth")
493 ("\\baselineskip")
494 ("\\columnsep")
495 ("\\columnseprule")
496 ("\\doublerulesep")
497 ("\\evensidemargin")
498 ("\\footheight")
499 ("\\footskip")
500 ("\\headheight")
501 ("\\headsep")
502 ("\\itemindent")
503 ("\\itemsep")
504 ("\\labelsep")
505 ("\\labelwidth")
506 ("\\leftmargin")
507 ("\\linewidth")
508 ("\\listparindent")
509 ("\\marginparsep")
510 ("\\marginparwidth")
511 ("\\mathindent")
512 ("\\oddsidemargin")
513 ("\\parindent")
514 ("\\parsep")
515 ("\\parskip")
516 ("\\partopsep")
517 ("\\rightmargin")
518 ("\\tabcolsep")
519 ("\\textheight")
520 ("\\textwidth")
521 ("\\topmargin")
522 ("\\topsep")
523 ("\\topskip")
524 )
525 "Alist of LaTeX style parameters.")
526 (defvar YaTeX:style-parameters-private nil
527 "*User definable alist of style parameters.")
528 (defvar YaTeX:style-parameters-private nil
529 "Holds the union of LaTeX style parameters.")
530 (setq YaTeX:style-parameters
531 (append YaTeX:style-parameters-private YaTeX:style-parameters-default))
533 (defvar YaTeX:length-history nil "Holds history of length.")
534 (defun YaTeX::setlength (&optional argp)
535 "YaTeX add-in function for arguments of \\setlength."
536 (cond
537 ((equal 1 argp)
538 (completing-read "Length variable: " YaTeX:style-parameters nil nil "\\"))
539 ((equal 2 argp)
540 (let ((minibuffer-history-symbol 'YaTeX:length-history))
541 (read-string "Length: "))))
542 )
543 (fset 'YaTeX::addtolength 'YaTeX::setlength)
545 (defun YaTeX::settowidth (&optional argp)
546 "YaTeX add-in function for arguments of \\settowidth."
547 (cond
548 ((equal 1 argp)
549 (completing-read "Length variable: " YaTeX:style-parameters nil nil "\\"))
550 ((equal 2 argp)
551 (read-string "Text: ")))
552 )
553 (defun YaTeX::newlength (&optional argp)
554 "YaTeX add-in function for arguments of \\newlength"
555 (cond
556 ((equal argp 1)
557 (let ((length (read-string "Length variable: " "\\")))
558 (or (assoc length YaTeX:style-parameters-private)
559 (setq YaTeX:style-parameters-private
560 (cons (list length) YaTeX:style-parameters-private)
561 YaTeX:style-parameters
562 (cons (list length) YaTeX:style-parameters)))
563 length)))
564 )
566 ;; \multicolumn's arguments
567 (defun YaTeX::multicolumn (&optional argp)
568 "YaTeX add-in function for arguments of \\multicolumn."
569 (cond
570 ((equal 1 argp)
571 (read-string "Number of columns: "))
572 ((equal 2 argp)
573 (let (c)
574 (while (not (string-match
575 (progn (message "Format(one of l,r,c): ")
576 (setq c (char-to-string (read-char))))
577 "lrc")))
578 c))
579 ((equal 3 argp)
580 (read-string "Item: ")))
581 )
583 ;;; -------------------- End of yatexadd --------------------
584 (provide 'yatexadd)