yatex

view yatexadd.el @ 12:a7f397790cdc

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