yatex

view yatexadd.el @ 11:390df0e505da

Label completion works.
author yuuji
date Mon, 20 Sep 1993 08:56:09 +0000
parents c746646cecf5
children a7f397790cdc
line source
1 ;;; -*- Emacs-Lisp -*-
2 ;;; YaTeX add-in functions.
3 ;;; yatexadd.el rev.5
4 ;;; (c)1991-1993 by HIROSE Yuuji.[yuuji@ae.keio.ac.jp]
5 ;;; Last modified Sat Sep 18 04:13:41 1993 on 98fa
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 ;;;
77 ;;Sample functions for section-type command.
78 ;;;
79 (defun YaTeX:multiput ()
80 (concat (YaTeX:read-coordinates "Pos")
81 (YaTeX:read-coordinates "Step")
82 "{" (read-string "How many times: ") "}")
83 )
85 (defun YaTeX:put ()
86 (YaTeX:read-coordinates "Pos")
87 )
89 (defun YaTeX:makebox ()
90 (concat (YaTeX:read-coordinates "Dimension")
91 (YaTeX:read-position "lrtb"))
92 )
94 (defun YaTeX:framebox ()
95 (if (YaTeX-quick-in-environment-p "picture")
96 (YaTeX:makebox))
97 )
99 (defun YaTeX:dashbox ()
100 (concat "{" (read-string "Dash dimension: ") "}"
101 (YaTeX:read-coordinates "Dimension"))
102 )
104 (defun YaTeX:read-coordinates (&optional mes varX varY)
105 (concat
106 "("
107 (read-string (format "%s %s: " (or mes "Dimension") (or varX "X")))
108 ","
109 (read-string (format "%s %s: " (or mes "Dimension") (or varY "Y")))
110 ")")
111 )
113 ;;;
114 ;;Sample functions for maketitle-type command.
115 ;;;
116 (defun YaTeX:sum ()
117 "Read range of summation."
118 (YaTeX:check-completion-type 'maketitle)
119 (concat (YaTeX:read-boundary "_") (YaTeX:read-boundary "^"))
120 )
122 (fset 'YaTeX:int 'YaTeX:sum)
124 (defun YaTeX:lim ()
125 "Insert limit notation of \\lim."
126 (YaTeX:check-completion-type 'maketitle)
127 (let ((var (read-string "Variable: ")) limit)
128 (if (string= "" var) ""
129 (setq limit (read-string "Limit ($ means infinity): "))
130 (if (string= "$" limit) (setq limit "\\infty"))
131 (concat "_{" var " \\rightarrow " limit "}")))
132 )
134 (defun YaTeX:gcd ()
135 "Add-in function for \\gcd(m,n)."
136 (YaTeX:check-completion-type 'maketitle)
137 (YaTeX:read-coordinates "\\gcd" "(?,)" "(,?)")
138 )
140 (defun YaTeX:read-boundary (ULchar)
141 "Read boundary usage by _ or ^. _ or ^ is indicated by argument ULchar."
142 (let ((bndry (read-string (concat ULchar "{???} ($ for infinity): "))))
143 (if (string= bndry "") ""
144 (if (string= bndry "$") (setq bndry "\\infty"))
145 (concat ULchar "{" bndry "}")))
146 )
148 (defun YaTeX:check-completion-type (type)
149 "Check valid completion type."
150 (if (not (eq type YaTeX-current-completion-type))
151 (error "This should be completed with %s-type completion." type))
152 )
155 ;;;
156 ;;; [[Add-in functions for reading section arguments]]
157 ;;;
158 ;; All of add-in functions for reading sections arguments should
159 ;; take an argument ARGP that specify the argument position.
160 ;; If argument position is out of range, nil should be returned,
161 ;; else nil should NOT be returned.
162 (defvar YaTeX-label-menu-other
163 (if YaTeX-japan "':他のバッファのラベル\n" "':LABEL IN OTHER BUFFER.\n"))
164 (defvar YaTeX-label-menu-any
165 (if YaTeX-japan "*:任意の文字列\n" "*:ANY STRING.\n"))
166 (defvar YaTeX-label-buffer "*Label completions*")
167 (defvar YaTeX-label-guide-msg "Select label and hit RETURN.")
168 (defvar YaTeX-label-select-map nil
169 "Key map used in label selection buffer.")
170 (defun YaTeX::label-setup-key-map ()
171 (if YaTeX-label-select-map nil
172 (message "Setting up label selection mode map...")
173 (setq YaTeX-label-select-map (copy-keymap global-map))
174 (suppress-keymap YaTeX-label-select-map)
175 (substitute-all-key-definition
176 'previous-line 'YaTeX::label-previous YaTeX-label-select-map)
177 (substitute-all-key-definition
178 'next-line 'YaTeX::label-next YaTeX-label-select-map)
179 (define-key YaTeX-label-select-map "\C-n" 'YaTeX::label-next)
180 (define-key YaTeX-label-select-map "\C-p" 'YaTeX::label-previous)
181 (define-key YaTeX-label-select-map "<" 'beginning-of-buffer)
182 (define-key YaTeX-label-select-map ">" 'end-of-buffer)
183 (define-key YaTeX-label-select-map "\C-m" 'exit-recursive-edit)
184 (define-key YaTeX-label-select-map "\C-j" 'exit-recursive-edit)
185 (define-key YaTeX-label-select-map " " 'exit-recursive-edit)
186 (define-key YaTeX-label-select-map "\C-g" 'abort-recursive-edit)
187 (define-key YaTeX-label-select-map "/" 'isearch-forward)
188 (define-key YaTeX-label-select-map "?" 'isearch-backward)
189 (define-key YaTeX-label-select-map "'" 'YaTeX::label-search-tag)
190 (define-key YaTeX-label-select-map "*" 'YaTeX::label-search-tag)
191 (message "Setting up label selection mode map...Done")
192 (let ((key ?A))
193 (while (<= key ?Z)
194 (define-key YaTeX-label-select-map (char-to-string key)
195 'YaTeX::label-search-tag)
196 (define-key YaTeX-label-select-map (char-to-string (+ key (- ?a ?A)))
197 'YaTeX::label-search-tag)
198 (setq key (1+ key)))))
199 )
200 (defun YaTeX::label-next ()
201 (interactive) (forward-line 1) (message YaTeX-label-guide-msg))
202 (defun YaTeX::label-previous ()
203 (interactive) (forward-line -1) (message YaTeX-label-guide-msg))
204 (defun YaTeX::label-search-tag ()
205 (interactive)
206 (let ((case-fold-search t))
207 (cond
208 ((save-excursion
209 (forward-char 1)
210 (re-search-forward (concat "^" (this-command-keys)) nil t))
211 (goto-char (match-beginning 0)))
212 ((save-excursion
213 (goto-char (point-min))
214 (re-search-forward (concat "^" (this-command-keys)) nil t))
215 (goto-char (match-beginning 0))))
216 (message YaTeX-label-guide-msg))
217 )
218 (defun YaTeX::ref (argp)
219 (cond
220 ((= argp 1)
221 (save-excursion
222 (let ((lnum 0) e0 m1 e1 label label-list (buf (current-buffer))
223 (p (point)) initl line)
224 (goto-char (point-min))
225 (message "Collecting labels...")
226 (save-window-excursion
227 (with-output-to-temp-buffer YaTeX-label-buffer
228 (while (re-search-forward "\\label{\\([^}]+\\)}" nil t)
229 (setq e0 (match-end 0) m1 (match-beginning 1) e1 (match-end 1))
230 (if (search-backward
231 YaTeX-comment-prefix (point-beginning-of-line) t) nil
232 (setq label (buffer-substring m1 e1)
233 label-list (cons label label-list))
234 (or initl
235 (if (< p (point)) (setq initl lnum)))
236 (beginning-of-line)
237 (skip-chars-forward " \t\n" nil)
238 (princ (format "%c:{%s}\t<<%s>>\n" (+ (% lnum 26) ?A) label
239 (buffer-substring (point) (point-end-of-line))))
240 (setq lnum (1+ lnum))
241 (message "Collecting \\label{}... %d" lnum))
242 (goto-char e0))
243 (princ YaTeX-label-menu-other)
244 (princ YaTeX-label-menu-any)
245 );with
246 (goto-char p)
247 (message "Collecting labels...Done")
248 (pop-to-buffer YaTeX-label-buffer)
249 (YaTeX::label-setup-key-map)
250 (setq truncate-lines t)
251 (setq buffer-read-only t)
252 (use-local-map YaTeX-label-select-map)
253 (message YaTeX-label-guide-msg)
254 (goto-line (or initl lnum)) ;goto recently defined label line
255 (unwind-protect
256 (progn
257 (recursive-edit)
258 (set-buffer (get-buffer YaTeX-label-buffer)) ;assertion
259 (beginning-of-line)
260 (setq line (count-lines (point-min)(point)))
261 (cond
262 ((= line lnum) (setq label (YaTeX-label-other)))
263 ((>= line (1+ lnum ))
264 (setq label (read-string "\\ref{???}: ")))
265 (t (setq label (nth (- lnum line 1) label-list)))))
266 (bury-buffer YaTeX-label-buffer)))
267 label
268 ))
269 ))
270 )
272 (defun YaTeX-label-other ()
273 (let ((lbuf "*YaTeX mode buffers*") (blist (buffer-list)) (lnum -1) buf rv
274 (ff "**find-file**"))
275 (with-output-to-temp-buffer lbuf
276 (while blist
277 (if (and (buffer-file-name (setq buf (car blist)))
278 (progn (set-buffer buf) (eq major-mode 'yatex-mode)))
279 (princ
280 (format "%c:{%s}\n" (+ (% (setq lnum (1+ lnum)) 26) ?A)
281 (buffer-name buf))))
282 (setq blist (cdr blist)))
283 (princ (format "':{%s}" ff)))
284 (pop-to-buffer lbuf)
285 (YaTeX::label-setup-key-map)
286 (setq buffer-read-only t)
287 (use-local-map YaTeX-label-select-map)
288 (message YaTeX-label-guide-msg)
289 (unwind-protect
290 (progn
291 (recursive-edit)
292 (set-buffer lbuf)
293 (beginning-of-line)
294 (setq rv
295 (if (re-search-forward "{\\([^\\}]+\\)}" (point-end-of-line) t)
296 (buffer-substring (match-beginning 1) (match-end 1)) nil)))
297 (kill-buffer lbuf))
298 (cond
299 ((null rv) "")
300 ((string= rv ff)
301 (call-interactively 'find-file)
302 (YaTeX::ref argp))
303 (t
304 (set-buffer rv)
305 (YaTeX::ref argp)))
306 )
307 )
309 ;;;
310 ;; global subroutines
311 ;;;
312 (defun substitute-all-key-definition (olddef newdef keymap)
313 "Replace recursively OLDDEF with NEWDEF for any keys in KEYMAP now
314 defined as OLDDEF. In other words, OLDDEF is replaced with NEWDEF
315 where ever it appears."
316 (if (arrayp keymap)
317 (let ((len (length keymap))
318 (i 0))
319 (while (< i len)
320 (let ((map (aref keymap i)))
321 (cond
322 ((arrayp map) (substitute-key-definition olddef newdef map))
323 ((equal map olddef)
324 (aset keymap i newdef)))
325 (setq i (1+ i)))))
326 (while keymap
327 (if (equal (cdr-safe (car-safe keymap)) olddef)
328 (setcdr (car keymap) newdef))
329 (setq keymap (cdr keymap)))))