yatex

view yatexadd.el @ 16:cb9afa9c1213

Auto-indentation at begin-type completion works correctly. Hack for gmhist&gmhist-mh. Fix the bug on \ref-completion. YaTeX-help is now available.
author yuuji
date Fri, 06 May 1994 21:14:11 +0000
parents b7b023a74293
children adc2f1472409
line source
1 ;;; -*- Emacs-Lisp -*-
2 ;;; YaTeX add-in functions.
3 ;;; yatexadd.el rev.8
4 ;;; (c )1991-1994 by HIROSE Yuuji.[yuuji@ae.keio.ac.jp]
5 ;;; Last modified Sat May 7 06:09:39 1994 on pajero
6 ;;; $Id$
8 (provide 'yatexadd)
10 ;;;
11 ;;Sample functions for LaTeX environment.
12 ;;;
13 (defvar YaTeX:tabular-default-rule
14 "@{\\vrule width 1pt\\ }c|c|c@{\\ \\vrule width 1pt}"
15 "*Your favorite default rule format."
16 )
17 (defun YaTeX:tabular ()
18 "YaTeX add-in function for tabular environment."
19 (let (bars (rule "") (j 0) (loc (YaTeX:read-position "tb")))
20 (setq bars (string-to-int (read-string "Number of `|': ")))
21 (if (> bars 0)
22 (while (< j bars) (setq rule (concat rule "|")) (setq j (1+ j)))
23 (setq rule YaTeX:tabular-default-rule))
24 (setq rule (read-string "rule format: " rule))
26 (message "")
27 (format "%s{%s}" loc rule))
28 )
30 (defun YaTeX:read-position (oneof)
31 (let ((pos "") loc)
32 (while (not (string-match
33 (setq loc (read-key-sequence
34 (format "Position (`%s') [%s]: " oneof pos)))
35 "\r\^g\n"))
36 (cond
37 ((string-match loc oneof)
38 (if (not (string-match loc pos))
39 (setq pos (concat pos loc))))
40 ((and (string-match loc "\C-h\C-?") (> (length pos) 0))
41 (setq pos (substring pos 0 (1- (length pos)))))
42 (t
43 (ding)
44 (message "Please input one of `%s'." oneof)
45 (sit-for 3))))
46 (message "")
47 (if (string= pos "") ""
48 (concat "[" pos "]")))
49 )
51 (defun YaTeX:table ()
52 "YaTeX add-in function for table environment."
53 (YaTeX:read-position "htbp")
54 )
56 (defun YaTeX:description ()
57 "Truly poor service:-)"
58 (setq single-command "item[]")
59 ""
60 )
62 (defun YaTeX:itemize ()
63 "It's also poor service."
64 (setq single-command "item")
65 ""
66 )
68 (fset 'YaTeX:enumerate 'YaTeX:itemize)
70 (defun YaTeX:picture ()
71 "Ask the size of coordinates of picture environment."
72 (concat (YaTeX:read-coordinates "Picture size")
73 (YaTeX:read-coordinates "Initial position"))
74 )
76 (defun YaTeX:equation ()
77 (if (fboundp 'YaTeX-toggle-math-mode)
78 (YaTeX-toggle-math-mode t)) ;force math-mode ON.
79 )
80 (fset 'YaTeX:eqnarray 'YaTeX:equation)
81 (fset 'YaTeX:displaymath 'YaTeX:equation)
83 (defun YaTeX:list ()
84 "%\n{} %default label\n{} %formatting parameter"
85 )
87 ;;;
88 ;;Sample functions for section-type command.
89 ;;;
90 (defun YaTeX:multiput ()
91 (concat (YaTeX:read-coordinates "Pos")
92 (YaTeX:read-coordinates "Step")
93 "{" (read-string "How many times: ") "}")
94 )
96 (defun YaTeX:put ()
97 (YaTeX:read-coordinates "Pos")
98 )
100 (defun YaTeX:makebox ()
101 (concat (YaTeX:read-coordinates "Dimension")
102 (YaTeX:read-position "lrtb"))
103 )
105 (defun YaTeX:framebox ()
106 (if (YaTeX-quick-in-environment-p "picture")
107 (YaTeX:makebox))
108 )
110 (defun YaTeX:dashbox ()
111 (concat "{" (read-string "Dash dimension: ") "}"
112 (YaTeX:read-coordinates "Dimension"))
113 )
115 (defun YaTeX:read-coordinates (&optional mes varX varY)
116 (concat
117 "("
118 (read-string (format "%s %s: " (or mes "Dimension") (or varX "X")))
119 ","
120 (read-string (format "%s %s: " (or mes "Dimension") (or varY "Y")))
121 ")")
122 )
124 ;;;
125 ;;Sample functions for maketitle-type command.
126 ;;;
127 (defun YaTeX:sum ()
128 "Read range of summation."
129 (YaTeX:check-completion-type 'maketitle)
130 (concat (YaTeX:read-boundary "_") (YaTeX:read-boundary "^"))
131 )
133 (fset 'YaTeX:int 'YaTeX:sum)
135 (defun YaTeX:lim ()
136 "Insert limit notation of \\lim."
137 (YaTeX:check-completion-type 'maketitle)
138 (let ((var (read-string "Variable: ")) limit)
139 (if (string= "" var) ""
140 (setq limit (read-string "Limit ($ means infinity): "))
141 (if (string= "$" limit) (setq limit "\\infty"))
142 (concat "_{" var " \\rightarrow " limit "}")))
143 )
145 (defun YaTeX:gcd ()
146 "Add-in function for \\gcd(m,n)."
147 (YaTeX:check-completion-type 'maketitle)
148 (YaTeX:read-coordinates "\\gcd" "(?,)" "(,?)")
149 )
151 (defun YaTeX:read-boundary (ULchar)
152 "Read boundary usage by _ or ^. _ or ^ is indicated by argument ULchar."
153 (let ((bndry (read-string (concat ULchar "{???} ($ for infinity): "))))
154 (if (string= bndry "") ""
155 (if (string= bndry "$") (setq bndry "\\infty"))
156 (concat ULchar "{" bndry "}")))
157 )
159 (defun YaTeX:verb ()
160 "Enclose \\verb's contents with the same characters."
161 (let ((quote-char (read-string "Quoting char: " "|"))
162 (contents (read-string "Quoted contents: ")))
163 (concat quote-char contents quote-char))
164 )
166 ;;;
167 ;;Subroutine
168 ;;;
170 (defun YaTeX:check-completion-type (type)
171 "Check valid completion type."
172 (if (not (eq type YaTeX-current-completion-type))
173 (error "This should be completed with %s-type completion." type))
174 )
177 ;;;
178 ;;; [[Add-in functions for reading section arguments]]
179 ;;;
180 ;; All of add-in functions for reading sections arguments should
181 ;; take an argument ARGP that specify the argument position.
182 ;; If argument position is out of range, nil should be returned,
183 ;; else nil should NOT be returned.
185 ;;
186 ; Label selection
187 ;;
188 (defvar YaTeX-label-menu-other
189 (if YaTeX-japan "':他のバッファのラベル\n" "':LABEL IN OTHER BUFFER.\n"))
190 (defvar YaTeX-label-menu-any
191 (if YaTeX-japan "*:任意の文字列\n" "*:ANY STRING.\n"))
192 (defvar YaTeX-label-buffer "*Label completions*")
193 (defvar YaTeX-label-guide-msg "Select label and hit RETURN.")
194 (defvar YaTeX-label-select-map nil
195 "Key map used in label selection buffer.")
196 (defun YaTeX::label-setup-key-map ()
197 (if YaTeX-label-select-map nil
198 (message "Setting up label selection mode map...")
199 (setq YaTeX-label-select-map (copy-keymap global-map))
200 (suppress-keymap YaTeX-label-select-map)
201 (substitute-all-key-definition
202 'previous-line 'YaTeX::label-previous YaTeX-label-select-map)
203 (substitute-all-key-definition
204 'next-line 'YaTeX::label-next YaTeX-label-select-map)
205 (define-key YaTeX-label-select-map "\C-n" 'YaTeX::label-next)
206 (define-key YaTeX-label-select-map "\C-p" 'YaTeX::label-previous)
207 (define-key YaTeX-label-select-map "<" 'beginning-of-buffer)
208 (define-key YaTeX-label-select-map ">" 'end-of-buffer)
209 (define-key YaTeX-label-select-map "\C-m" 'exit-recursive-edit)
210 (define-key YaTeX-label-select-map "\C-j" 'exit-recursive-edit)
211 (define-key YaTeX-label-select-map " " 'exit-recursive-edit)
212 (define-key YaTeX-label-select-map "\C-g" 'abort-recursive-edit)
213 (define-key YaTeX-label-select-map "/" 'isearch-forward)
214 (define-key YaTeX-label-select-map "?" 'isearch-backward)
215 (define-key YaTeX-label-select-map "'" 'YaTeX::label-search-tag)
216 (define-key YaTeX-label-select-map "*" 'YaTeX::label-search-tag)
217 (message "Setting up label selection mode map...Done")
218 (let ((key ?A))
219 (while (<= key ?Z)
220 (define-key YaTeX-label-select-map (char-to-string key)
221 'YaTeX::label-search-tag)
222 (define-key YaTeX-label-select-map (char-to-string (+ key (- ?a ?A)))
223 'YaTeX::label-search-tag)
224 (setq key (1+ key)))))
225 )
226 (defun YaTeX::label-next ()
227 (interactive) (forward-line 1) (message YaTeX-label-guide-msg))
228 (defun YaTeX::label-previous ()
229 (interactive) (forward-line -1) (message YaTeX-label-guide-msg))
230 (defun YaTeX::label-search-tag ()
231 (interactive)
232 (let ((case-fold-search t))
233 (cond
234 ((save-excursion
235 (forward-char 1)
236 (re-search-forward (concat "^" (this-command-keys)) nil t))
237 (goto-char (match-beginning 0)))
238 ((save-excursion
239 (goto-char (point-min))
240 (re-search-forward (concat "^" (this-command-keys)) nil t))
241 (goto-char (match-beginning 0))))
242 (message YaTeX-label-guide-msg))
243 )
244 (defun YaTeX::ref (argp)
245 (cond
246 ((= argp 1)
247 (save-excursion
248 (let ((lnum 0) e0 m1 e1 label label-list (buf (current-buffer))
249 (p (point)) initl line)
250 (goto-char (point-min))
251 (message "Collecting labels...")
252 (save-window-excursion
253 (YaTeX-showup-buffer
254 YaTeX-label-buffer (function (lambda (x) (window-width x))))
255 (with-output-to-temp-buffer YaTeX-label-buffer
256 (while (re-search-forward "\\label{\\([^}]+\\)}" nil t)
257 (setq e0 (match-end 0) m1 (match-beginning 1) e1 (match-end 1))
258 (if (search-backward
259 YaTeX-comment-prefix (point-beginning-of-line) t) nil
260 (setq label (buffer-substring m1 e1)
261 label-list (cons label label-list))
262 (or initl
263 (if (< p (point)) (setq initl lnum)))
264 (beginning-of-line)
265 (skip-chars-forward " \t\n" nil)
266 (princ (format "%c:{%s}\t<<%s>>\n" (+ (% lnum 26) ?A) label
267 (buffer-substring (point) (point-end-of-line))))
268 (setq lnum (1+ lnum))
269 (message "Collecting \\label{}... %d" lnum))
270 (goto-char e0))
271 (princ YaTeX-label-menu-other)
272 (princ YaTeX-label-menu-any)
273 );with
274 (goto-char p)
275 (message "Collecting labels...Done")
276 (pop-to-buffer YaTeX-label-buffer)
277 (YaTeX::label-setup-key-map)
278 (setq truncate-lines t)
279 (setq buffer-read-only t)
280 (use-local-map YaTeX-label-select-map)
281 (message YaTeX-label-guide-msg)
282 (goto-line (or initl lnum)) ;goto recently defined label line
283 (unwind-protect
284 (progn
285 (recursive-edit)
286 (set-buffer (get-buffer YaTeX-label-buffer)) ;assertion
287 (beginning-of-line)
288 (setq line (count-lines (point-min)(point)))
289 (cond
290 ((= line lnum) (setq label (YaTeX-label-other)))
291 ((>= line (1+ lnum ))
292 (setq label (read-string "\\ref{???}: ")))
293 (t (setq label (nth (- lnum line 1) label-list)))))
294 (bury-buffer YaTeX-label-buffer)))
295 label
296 ))
297 ))
298 )
299 (fset 'YaTeX::pageref 'YaTeX::ref)
301 (defun YaTeX-label-other ()
302 (let ((lbuf "*YaTeX mode buffers*") (blist (buffer-list)) (lnum -1) buf rv
303 (ff "**find-file**"))
304 (YaTeX-showup-buffer
305 lbuf (function (lambda (x) 1))) ;;Select next window surely.
306 (with-output-to-temp-buffer lbuf
307 (while blist
308 (if (and (buffer-file-name (setq buf (car blist)))
309 (progn (set-buffer buf) (eq major-mode 'yatex-mode)))
310 (princ
311 (format "%c:{%s}\n" (+ (% (setq lnum (1+ lnum)) 26) ?A)
312 (buffer-name buf))))
313 (setq blist (cdr blist)))
314 (princ (format "':{%s}" ff)))
315 (pop-to-buffer lbuf)
316 (YaTeX::label-setup-key-map)
317 (setq buffer-read-only t)
318 (use-local-map YaTeX-label-select-map)
319 (message YaTeX-label-guide-msg)
320 (unwind-protect
321 (progn
322 (recursive-edit)
323 (set-buffer lbuf)
324 (beginning-of-line)
325 (setq rv
326 (if (re-search-forward "{\\([^\\}]+\\)}" (point-end-of-line) t)
327 (buffer-substring (match-beginning 1) (match-end 1)) nil)))
328 (kill-buffer lbuf))
329 (cond
330 ((null rv) "")
331 ((string= rv ff)
332 (call-interactively 'find-file)
333 (YaTeX::ref argp))
334 (t
335 (set-buffer rv)
336 (YaTeX::ref argp)))
337 )
338 )
340 ;;
341 ; completion for the arguments of \newcommand
342 ;;
343 (defun YaTeX::newcommand (&optional argp)
344 (cond
345 ((= argp 1)
346 (let ((command (read-string "Define newcommand: " "\\")))
347 (put 'YaTeX::newcommand 'command (substring command 1))
348 command))
349 ((= argp 2)
350 (let ((argc
351 (string-to-int (read-string "Number of arguments(Default 0): ")))
352 (def (read-string "Definition: "))
353 (command (get 'YaTeX::newcommand 'command)))
354 ;;!!! It's illegal to insert string in the add-in function !!!
355 (if (> argc 0) (insert (format "[%d]" argc)))
356 (if (and (stringp command)
357 (string< "" command)
358 (y-or-n-p "Update user completion table?"))
359 (YaTeX-update-table
360 (if (> argc 1) (list command argc) (list command))
361 'section-table 'user-section-table 'tmp-section-table))
362 (message "")
363 def ;return command name
364 ))
365 (t ""))
366 )
368 ;;
369 ; completion for the arguments of \pagestyle
370 ;;
371 (defun YaTeX::pagestyle (&optional argp)
372 "Read the pagestyle with completion."
373 (completing-read
374 "Page style: "
375 '(("plain") ("empty") ("headings") ("myheadings") ("normal") nil))
376 )
377 ;;
378 ; completion for the arguments of \pagenumbering
379 ;;
380 (defun YaTeX::pagenumbering (&optional argp)
381 "Read the numbering style."
382 (completing-read
383 "Page numbering style: "
384 '(("arabic") ("Alpha") ("alpha") ("Roman") ("roman")))
385 )
387 ;;;
388 ;; global subroutines
389 ;;;
390 (defun substitute-all-key-definition (olddef newdef keymap)
391 "Replace recursively OLDDEF with NEWDEF for any keys in KEYMAP now
392 defined as OLDDEF. In other words, OLDDEF is replaced with NEWDEF
393 where ever it appears."
394 (if (arrayp keymap)
395 (let ((len (length keymap))
396 (i 0))
397 (while (< i len)
398 (let ((map (aref keymap i)))
399 (cond
400 ((arrayp map) (substitute-key-definition olddef newdef map))
401 ((equal map olddef)
402 (aset keymap i newdef)))
403 (setq i (1+ i)))))
404 (while keymap
405 (if (equal (cdr-safe (car-safe keymap)) olddef)
406 (setcdr (car keymap) newdef))
407 (setq keymap (cdr keymap)))))