yatex

view yatexadd.el @ 13:eafae54794a0

Show message at comment-region on begin/end mode. Greek letters completion in yatexmth. YaTeX-mark environment and YaTeX-%-menu added. Erase cursor at the execution of dviout(DOS). Enable recursive completion at section-type completion.
author yuuji
date Sat, 29 Jan 1994 07:59:59 +0000
parents a7f397790cdc
children b7b023a74293
line source
1 ;;; -*- Emacs-Lisp -*-
2 ;;; YaTeX add-in functions.
3 ;;; yatexadd.el rev.6
4 ;;; (c )1991-1993 by HIROSE Yuuji.[yuuji@ae.keio.ac.jp]
5 ;;; Last modified Sat Jan 29 16:55:09 1994 on gloria
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.
170 ;;
171 ; Label selection
172 ;;
173 (defvar YaTeX-label-menu-other
174 (if YaTeX-japan "':他のバッファのラベル\n" "':LABEL IN OTHER BUFFER.\n"))
175 (defvar YaTeX-label-menu-any
176 (if YaTeX-japan "*:任意の文字列\n" "*:ANY STRING.\n"))
177 (defvar YaTeX-label-buffer "*Label completions*")
178 (defvar YaTeX-label-guide-msg "Select label and hit RETURN.")
179 (defvar YaTeX-label-select-map nil
180 "Key map used in label selection buffer.")
181 (defun YaTeX::label-setup-key-map ()
182 (if YaTeX-label-select-map nil
183 (message "Setting up label selection mode map...")
184 (setq YaTeX-label-select-map (copy-keymap global-map))
185 (suppress-keymap YaTeX-label-select-map)
186 (substitute-all-key-definition
187 'previous-line 'YaTeX::label-previous YaTeX-label-select-map)
188 (substitute-all-key-definition
189 'next-line 'YaTeX::label-next YaTeX-label-select-map)
190 (define-key YaTeX-label-select-map "\C-n" 'YaTeX::label-next)
191 (define-key YaTeX-label-select-map "\C-p" 'YaTeX::label-previous)
192 (define-key YaTeX-label-select-map "<" 'beginning-of-buffer)
193 (define-key YaTeX-label-select-map ">" 'end-of-buffer)
194 (define-key YaTeX-label-select-map "\C-m" 'exit-recursive-edit)
195 (define-key YaTeX-label-select-map "\C-j" 'exit-recursive-edit)
196 (define-key YaTeX-label-select-map " " 'exit-recursive-edit)
197 (define-key YaTeX-label-select-map "\C-g" 'abort-recursive-edit)
198 (define-key YaTeX-label-select-map "/" 'isearch-forward)
199 (define-key YaTeX-label-select-map "?" 'isearch-backward)
200 (define-key YaTeX-label-select-map "'" 'YaTeX::label-search-tag)
201 (define-key YaTeX-label-select-map "*" 'YaTeX::label-search-tag)
202 (message "Setting up label selection mode map...Done")
203 (let ((key ?A))
204 (while (<= key ?Z)
205 (define-key YaTeX-label-select-map (char-to-string key)
206 'YaTeX::label-search-tag)
207 (define-key YaTeX-label-select-map (char-to-string (+ key (- ?a ?A)))
208 'YaTeX::label-search-tag)
209 (setq key (1+ key)))))
210 )
211 (defun YaTeX::label-next ()
212 (interactive) (forward-line 1) (message YaTeX-label-guide-msg))
213 (defun YaTeX::label-previous ()
214 (interactive) (forward-line -1) (message YaTeX-label-guide-msg))
215 (defun YaTeX::label-search-tag ()
216 (interactive)
217 (let ((case-fold-search t))
218 (cond
219 ((save-excursion
220 (forward-char 1)
221 (re-search-forward (concat "^" (this-command-keys)) nil t))
222 (goto-char (match-beginning 0)))
223 ((save-excursion
224 (goto-char (point-min))
225 (re-search-forward (concat "^" (this-command-keys)) nil t))
226 (goto-char (match-beginning 0))))
227 (message YaTeX-label-guide-msg))
228 )
229 (defun YaTeX::ref (argp)
230 (cond
231 ((= argp 1)
232 (save-excursion
233 (let ((lnum 0) e0 m1 e1 label label-list (buf (current-buffer))
234 (p (point)) initl line)
235 (goto-char (point-min))
236 (message "Collecting labels...")
237 (save-window-excursion
238 (YaTeX-showup-buffer
239 YaTeX-label-buffer (function (lambda (x) (window-width x))))
240 (with-output-to-temp-buffer YaTeX-label-buffer
241 (while (re-search-forward "\\label{\\([^}]+\\)}" nil t)
242 (setq e0 (match-end 0) m1 (match-beginning 1) e1 (match-end 1))
243 (if (search-backward
244 YaTeX-comment-prefix (point-beginning-of-line) t) nil
245 (setq label (buffer-substring m1 e1)
246 label-list (cons label label-list))
247 (or initl
248 (if (< p (point)) (setq initl lnum)))
249 (beginning-of-line)
250 (skip-chars-forward " \t\n" nil)
251 (princ (format "%c:{%s}\t<<%s>>\n" (+ (% lnum 26) ?A) label
252 (buffer-substring (point) (point-end-of-line))))
253 (setq lnum (1+ lnum))
254 (message "Collecting \\label{}... %d" lnum))
255 (goto-char e0))
256 (princ YaTeX-label-menu-other)
257 (princ YaTeX-label-menu-any)
258 );with
259 (goto-char p)
260 (message "Collecting labels...Done")
261 (pop-to-buffer YaTeX-label-buffer)
262 (YaTeX::label-setup-key-map)
263 (setq truncate-lines t)
264 (setq buffer-read-only t)
265 (use-local-map YaTeX-label-select-map)
266 (message YaTeX-label-guide-msg)
267 (goto-line (or initl lnum)) ;goto recently defined label line
268 (unwind-protect
269 (progn
270 (recursive-edit)
271 (set-buffer (get-buffer YaTeX-label-buffer)) ;assertion
272 (beginning-of-line)
273 (setq line (count-lines (point-min)(point)))
274 (cond
275 ((= line lnum) (setq label (YaTeX-label-other)))
276 ((>= line (1+ lnum ))
277 (setq label (read-string "\\ref{???}: ")))
278 (t (setq label (nth (- lnum line 1) label-list)))))
279 (bury-buffer YaTeX-label-buffer)))
280 label
281 ))
282 ))
283 )
285 (defun YaTeX-label-other ()
286 (let ((lbuf "*YaTeX mode buffers*") (blist (buffer-list)) (lnum -1) buf rv
287 (ff "**find-file**"))
288 (YaTeX-showup-buffer
289 lbuf (function (lambda (x) 1))) ;;Select next window surely.
290 (with-output-to-temp-buffer lbuf
291 (while blist
292 (if (and (buffer-file-name (setq buf (car blist)))
293 (progn (set-buffer buf) (eq major-mode 'yatex-mode)))
294 (princ
295 (format "%c:{%s}\n" (+ (% (setq lnum (1+ lnum)) 26) ?A)
296 (buffer-name buf))))
297 (setq blist (cdr blist)))
298 (princ (format "':{%s}" ff)))
299 (pop-to-buffer lbuf)
300 (YaTeX::label-setup-key-map)
301 (setq buffer-read-only t)
302 (use-local-map YaTeX-label-select-map)
303 (message YaTeX-label-guide-msg)
304 (unwind-protect
305 (progn
306 (recursive-edit)
307 (set-buffer lbuf)
308 (beginning-of-line)
309 (setq rv
310 (if (re-search-forward "{\\([^\\}]+\\)}" (point-end-of-line) t)
311 (buffer-substring (match-beginning 1) (match-end 1)) nil)))
312 (kill-buffer lbuf))
313 (cond
314 ((null rv) "")
315 ((string= rv ff)
316 (call-interactively 'find-file)
317 (YaTeX::ref argp))
318 (t
319 (set-buffer rv)
320 (YaTeX::ref argp)))
321 )
322 )
324 ;;
325 ; completion for the arguments of \newcommand
326 ;;
327 (defun YaTeX::newcommand (&optional argp)
328 (cond
329 ((= argp 1)
330 (let ((command (read-string "Define newcommand: " "\\")))
331 (put 'YaTeX::newcommand 'command (substring command 1))
332 command))
333 ((= argp 2)
334 (let ((argc
335 (string-to-int (read-string "Number of arguments(Default 0): ")))
336 (def (read-string "Definition: "))
337 (command (get 'YaTeX::newcommand 'command)))
338 ;;!!! It's illegal to insert string in the add-in function !!!
339 (if (> argc 0) (insert (format "[%d]" argc)))
340 (if (and (stringp command)
341 (string< "" command)
342 (y-or-n-p "Update user completion table?"))
343 (YaTeX-update-table
344 (if (> argc 1) (list command argc) (list command))
345 'section-table 'user-section-table 'tmp-section-table))
346 (message "")
347 def ;return command name
348 ))
349 (t ""))
350 )
353 ;;;
354 ;; global subroutines
355 ;;;
356 (defun substitute-all-key-definition (olddef newdef keymap)
357 "Replace recursively OLDDEF with NEWDEF for any keys in KEYMAP now
358 defined as OLDDEF. In other words, OLDDEF is replaced with NEWDEF
359 where ever it appears."
360 (if (arrayp keymap)
361 (let ((len (length keymap))
362 (i 0))
363 (while (< i len)
364 (let ((map (aref keymap i)))
365 (cond
366 ((arrayp map) (substitute-key-definition olddef newdef map))
367 ((equal map olddef)
368 (aset keymap i newdef)))
369 (setq i (1+ i)))))
370 (while keymap
371 (if (equal (cdr-safe (car-safe keymap)) olddef)
372 (setcdr (car keymap) newdef))
373 (setq keymap (cdr keymap)))))