yatex

view yatexadd.el @ 46:cd1b63102eed

Support Mule2
author yuuji
date Mon, 19 Sep 1994 16:54:19 +0000
parents ef686a35472d
children a0640ff3f72f
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 Aug 22 14:19:39 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 (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 ;;;
250 ;;Subroutine
251 ;;;
253 (defun YaTeX:check-completion-type (type)
254 "Check valid completion type."
255 (if (not (eq type YaTeX-current-completion-type))
256 (error "This should be completed with %s-type completion." type))
257 )
260 ;;;
261 ;;; [[Add-in functions for reading section arguments]]
262 ;;;
263 ;; All of add-in functions for reading sections arguments should
264 ;; take an argument ARGP that specify the argument position.
265 ;; If argument position is out of range, nil should be returned,
266 ;; else nil should NOT be returned.
268 ;;
269 ; Label selection
270 ;;
271 (defvar YaTeX-label-menu-other
272 (if YaTeX-japan "':他のバッファのラベル\n" "':LABEL IN OTHER BUFFER.\n"))
273 (defvar YaTeX-label-menu-repeat
274 (if YaTeX-japan ".:直前の\\refと同じ\n" "/:REPEAT LAST \ref{}\n"))
275 (defvar YaTeX-label-menu-any
276 (if YaTeX-japan "*:任意の文字列\n" "*:ANY STRING.\n"))
277 (defvar YaTeX-label-buffer "*Label completions*")
278 (defvar YaTeX-label-guide-msg "Select label and hit RETURN.")
279 (defvar YaTeX-label-select-map nil
280 "Key map used in label selection buffer.")
281 (defun YaTeX::label-setup-key-map ()
282 (if YaTeX-label-select-map nil
283 (message "Setting up label selection mode map...")
284 (setq YaTeX-label-select-map (copy-keymap global-map))
285 (suppress-keymap YaTeX-label-select-map)
286 (substitute-all-key-definition
287 'previous-line 'YaTeX::label-previous YaTeX-label-select-map)
288 (substitute-all-key-definition
289 'next-line 'YaTeX::label-next YaTeX-label-select-map)
290 (define-key YaTeX-label-select-map "\C-n" 'YaTeX::label-next)
291 (define-key YaTeX-label-select-map "\C-p" 'YaTeX::label-previous)
292 (define-key YaTeX-label-select-map "<" 'beginning-of-buffer)
293 (define-key YaTeX-label-select-map ">" 'end-of-buffer)
294 (define-key YaTeX-label-select-map "\C-m" 'exit-recursive-edit)
295 (define-key YaTeX-label-select-map "\C-j" 'exit-recursive-edit)
296 (define-key YaTeX-label-select-map " " 'exit-recursive-edit)
297 (define-key YaTeX-label-select-map "\C-g" 'abort-recursive-edit)
298 (define-key YaTeX-label-select-map "/" 'isearch-forward)
299 (define-key YaTeX-label-select-map "?" 'isearch-backward)
300 (define-key YaTeX-label-select-map "'" 'YaTeX::label-search-tag)
301 (define-key YaTeX-label-select-map "." 'YaTeX::label-search-tag)
302 (define-key YaTeX-label-select-map "*" 'YaTeX::label-search-tag)
303 (message "Setting up label selection mode map...Done")
304 (let ((key ?A))
305 (while (<= key ?Z)
306 (define-key YaTeX-label-select-map (char-to-string key)
307 'YaTeX::label-search-tag)
308 (define-key YaTeX-label-select-map (char-to-string (+ key (- ?a ?A)))
309 'YaTeX::label-search-tag)
310 (setq key (1+ key)))))
311 )
312 (defun YaTeX::label-next ()
313 (interactive) (forward-line 1) (message YaTeX-label-guide-msg))
314 (defun YaTeX::label-previous ()
315 (interactive) (forward-line -1) (message YaTeX-label-guide-msg))
316 (defun YaTeX::label-search-tag ()
317 (interactive)
318 (let ((case-fold-search t) (tag (regexp-quote (this-command-keys))))
319 (cond
320 ((save-excursion
321 (forward-char 1)
322 (re-search-forward (concat "^" tag) nil t))
323 (goto-char (match-beginning 0)))
324 ((save-excursion
325 (goto-char (point-min))
326 (re-search-forward (concat "^" tag) nil t))
327 (goto-char (match-beginning 0))))
328 (message YaTeX-label-guide-msg))
329 )
330 (defun YaTeX::ref (argp)
331 (cond
332 ((= argp 1)
333 (save-excursion
334 (let ((lnum 0) e0 m1 e1 label label-list (buf (current-buffer))
335 (p (point)) initl line)
336 (goto-char (point-min))
337 (message "Collecting labels...")
338 (save-window-excursion
339 (YaTeX-showup-buffer
340 YaTeX-label-buffer (function (lambda (x) (window-width x))))
341 (with-output-to-temp-buffer YaTeX-label-buffer
342 (while (re-search-forward "\\label{\\([^}]+\\)}" nil t)
343 (setq e0 (match-end 0) m1 (match-beginning 1) e1 (match-end 1))
344 (if (search-backward
345 YaTeX-comment-prefix (point-beginning-of-line) t) nil
346 (setq label (buffer-substring m1 e1)
347 label-list (cons label label-list))
348 (or initl
349 (if (< p (point)) (setq initl lnum)))
350 (beginning-of-line)
351 (skip-chars-forward " \t\n" nil)
352 (princ (format "%c:{%s}\t<<%s>>\n" (+ (% lnum 26) ?A) label
353 (buffer-substring (point) (point-end-of-line))))
354 (setq lnum (1+ lnum))
355 (message "Collecting \\label{}... %d" lnum))
356 (goto-char e0))
357 (princ YaTeX-label-menu-other)
358 (princ YaTeX-label-menu-repeat)
359 (princ YaTeX-label-menu-any)
360 );with
361 (goto-char p)
362 (message "Collecting labels...Done")
363 (pop-to-buffer YaTeX-label-buffer)
364 (YaTeX::label-setup-key-map)
365 (setq truncate-lines t)
366 (setq buffer-read-only t)
367 (use-local-map YaTeX-label-select-map)
368 (message YaTeX-label-guide-msg)
369 (goto-line (or initl lnum)) ;goto recently defined label line
370 (unwind-protect
371 (progn
372 (recursive-edit)
373 (set-buffer (get-buffer YaTeX-label-buffer)) ;assertion
374 (beginning-of-line)
375 (setq line (count-lines (point-min)(point)))
376 (cond
377 ((= line lnum) (setq label (YaTeX-label-other)))
378 ((= line (1+ lnum))
379 (save-excursion
380 (switch-to-buffer buf)
381 (goto-char p)
382 (if (re-search-backward "\\\\ref{\\([^}]+\\)}" nil t)
383 (setq label (buffer-substring
384 (match-beginning 1) (match-end 1)))
385 (setq label ""))))
386 ((>= line (+ lnum 2))
387 (setq label (read-string "\\ref{???}: ")))
388 (t (setq label (nth (- lnum line 1) label-list)))))
389 (bury-buffer YaTeX-label-buffer)))
390 label
391 ))
392 ))
393 )
394 (fset 'YaTeX::pageref 'YaTeX::ref)
396 (defun YaTeX-label-other ()
397 (let ((lbuf "*YaTeX mode buffers*") (blist (buffer-list)) (lnum -1) buf rv
398 (ff "**find-file**"))
399 (YaTeX-showup-buffer
400 lbuf (function (lambda (x) 1))) ;;Select next window surely.
401 (with-output-to-temp-buffer lbuf
402 (while blist
403 (if (and (buffer-file-name (setq buf (car blist)))
404 (progn (set-buffer buf) (eq major-mode 'yatex-mode)))
405 (princ
406 (format "%c:{%s}\n" (+ (% (setq lnum (1+ lnum)) 26) ?A)
407 (buffer-name buf))))
408 (setq blist (cdr blist)))
409 (princ (format "':{%s}" ff)))
410 (pop-to-buffer lbuf)
411 (YaTeX::label-setup-key-map)
412 (setq buffer-read-only t)
413 (use-local-map YaTeX-label-select-map)
414 (message YaTeX-label-guide-msg)
415 (unwind-protect
416 (progn
417 (recursive-edit)
418 (set-buffer lbuf)
419 (beginning-of-line)
420 (setq rv
421 (if (re-search-forward "{\\([^\\}]+\\)}" (point-end-of-line) t)
422 (buffer-substring (match-beginning 1) (match-end 1)) nil)))
423 (kill-buffer lbuf))
424 (cond
425 ((null rv) "")
426 ((string= rv ff)
427 (call-interactively 'find-file)
428 (YaTeX::ref argp))
429 (t
430 (set-buffer rv)
431 (YaTeX::ref argp)))
432 )
433 )
435 ;;
436 ; completion for the arguments of \newcommand
437 ;;
438 (defun YaTeX::newcommand (&optional argp)
439 (cond
440 ((= argp 1)
441 (let ((command (read-string "Define newcommand: " "\\")))
442 (put 'YaTeX::newcommand 'command (substring command 1))
443 command))
444 ((= argp 2)
445 (let ((argc
446 (string-to-int (read-string "Number of arguments(Default 0): ")))
447 (def (read-string "Definition: "))
448 (command (get 'YaTeX::newcommand 'command)))
449 ;;!!! It's illegal to insert string in the add-in function !!!
450 (if (> argc 0) (insert (format "[%d]" argc)))
451 (if (and (stringp command)
452 (string< "" command)
453 (y-or-n-p "Update user completion table?"))
454 (cond
455 ((= argc 0)
456 (YaTeX-update-table
457 (list command)
458 'singlecmd-table 'user-singlecmd-table 'tmp-singlecmd-table))
459 ((= argc 1)
460 (YaTeX-update-table
461 (list command)
462 'section-table 'user-section-table 'tmp-section-table))
463 (t (YaTeX-update-table
464 (list command argc)
465 'section-table 'user-section-table 'tmp-section-table))))
466 (message "")
467 def ;return command name
468 ))
469 (t ""))
470 )
472 ;;
473 ; completion for the arguments of \pagestyle
474 ;;
475 (defun YaTeX::pagestyle (&optional argp)
476 "Read the pagestyle with completion."
477 (completing-read
478 "Page style: "
479 '(("plain") ("empty") ("headings") ("myheadings") ("normal") nil))
480 )
481 ;;
482 ; completion for the arguments of \pagenumbering
483 ;;
484 (defun YaTeX::pagenumbering (&optional argp)
485 "Read the numbering style."
486 (completing-read
487 "Page numbering style: "
488 '(("arabic") ("Alpha") ("alpha") ("Roman") ("roman")))
489 )
491 ;;
492 ; Length
493 ;;
494 (defvar YaTeX:style-parameters-default
495 '(("\\arraycolsep")
496 ("\\arrayrulewidth")
497 ("\\baselineskip")
498 ("\\columnsep")
499 ("\\columnseprule")
500 ("\\doublerulesep")
501 ("\\evensidemargin")
502 ("\\footheight")
503 ("\\footskip")
504 ("\\headheight")
505 ("\\headsep")
506 ("\\itemindent")
507 ("\\itemsep")
508 ("\\labelsep")
509 ("\\labelwidth")
510 ("\\leftmargin")
511 ("\\linewidth")
512 ("\\listparindent")
513 ("\\marginparsep")
514 ("\\marginparwidth")
515 ("\\mathindent")
516 ("\\oddsidemargin")
517 ("\\parindent")
518 ("\\parsep")
519 ("\\parskip")
520 ("\\partopsep")
521 ("\\rightmargin")
522 ("\\tabcolsep")
523 ("\\textheight")
524 ("\\textwidth")
525 ("\\topmargin")
526 ("\\topsep")
527 ("\\topskip")
528 )
529 "Alist of LaTeX style parameters.")
530 (defvar YaTeX:style-parameters-private nil
531 "*User definable alist of style parameters.")
532 (defvar YaTeX:style-parameters-private nil
533 "Holds the union of LaTeX style parameters.")
534 (setq YaTeX:style-parameters
535 (append YaTeX:style-parameters-private YaTeX:style-parameters-default))
537 (defvar YaTeX:length-history nil "Holds history of length.")
538 (defun YaTeX::setlength (&optional argp)
539 "YaTeX add-in function for arguments of \\setlength."
540 (cond
541 ((equal 1 argp)
542 (completing-read "Length variable: " YaTeX:style-parameters nil nil "\\"))
543 ((equal 2 argp)
544 (let ((minibuffer-history-symbol 'YaTeX:length-history))
545 (read-string "Length: "))))
546 )
547 (fset 'YaTeX::addtolength 'YaTeX::setlength)
549 (defun YaTeX::settowidth (&optional argp)
550 "YaTeX add-in function for arguments of \\settowidth."
551 (cond
552 ((equal 1 argp)
553 (completing-read "Length variable: " YaTeX:style-parameters nil nil "\\"))
554 ((equal 2 argp)
555 (read-string "Text: ")))
556 )
557 (defun YaTeX::newlength (&optional argp)
558 "YaTeX add-in function for arguments of \\newlength"
559 (cond
560 ((equal argp 1)
561 (let ((length (read-string "Length variable: " "\\")))
562 (or (assoc length YaTeX:style-parameters-private)
563 (setq YaTeX:style-parameters-private
564 (cons (list length) YaTeX:style-parameters-private)
565 YaTeX:style-parameters
566 (cons (list length) YaTeX:style-parameters)))
567 length)))
568 )
570 ;; \multicolumn's arguments
571 (defun YaTeX::multicolumn (&optional argp)
572 "YaTeX add-in function for arguments of \\multicolumn."
573 (cond
574 ((equal 1 argp)
575 (read-string "Number of columns: "))
576 ((equal 2 argp)
577 (let (c)
578 (while (not (string-match
579 (progn (message "Format(one of l,r,c): ")
580 (setq c (char-to-string (read-char))))
581 "lrc")))
582 c))
583 ((equal 3 argp)
584 (read-string "Item: ")))
585 )
587 ;;; -------------------- End of yatexadd --------------------
588 (provide 'yatexadd)