(ispell-personal-dictionary): Add autoload.
[bpt/emacs.git] / lisp / tempo.el
CommitLineData
785c4478 1;;; tempo.el --- Flexible template insertion
813f532d
RS
2;; Copyright (C) 1994 Free Software Foundation, Inc.
3
8f41fd64 4;; Author: David K}gedal <davidk@lysator.liu.se >
813f532d 5;; Created: 16 Feb 1994
34da6b16 6;; Version: 1.2.1
813f532d 7;; Keywords: extensions, languages, tools
7f4d3657 8;; $Revision: 1.5 $
813f532d
RS
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software; you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation; either version 2, or (at your option)
15;; any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
23;; along with GNU Emacs; see the file COPYING. If not, write to
24;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25
26;;; Commentary:
27
28;; This file provides a simple way to define powerful templates, or
29;; macros, if you wish. It is mainly intended for, but not limited to,
30;; other programmers to be used for creating shortcuts for editing
31;; certain kind of documents. It was originally written to be used by
34da6b16
RS
32;; a HTML editing mode written by Nelson Minar <nelson@santafe.edu>,
33;; and his html-helper-mode.el is probably the best example of how to
34;; use this program.
813f532d
RS
35
36;; A template is defined as a list of items to be inserted in the
37;; current buffer at point. Some of the items can be simple strings,
38;; while other can control formatting or define special points of
39;; interest in the inserted text.
40
41;; If a template defines a "point of interest" that point is inserted
42;; in a buffer-local list of "points of interest" that the user can
43;; jump between with the commands `tempo-backward-mark' and
44;; `tempo-forward-mark'. If the template definer provides a prompt for
45;; the point, and the variable `tempo-interactive' is non-nil, the
46;; user will be prompted for a string to be inserted in the buffer,
47;; using the minibuffer.
48
49;; The template can also define one point to be replaced with the
50;; current region if the template command is called with a prefix (or
51;; a non-nil argument).
52
53;; More flexible templates can be created by including lisp symbols,
54;; which will be evaluated as variables, or lists, which will will be
55;; evaluated as lisp expressions.
56
57;; See the documentation for tempo-define-template for the different
58;; items that can be used to define a tempo template.
59
60;; One of the more powerful features of tempo templates are automatic
61;; completion. With every template can be assigned a special tag that
62;; should be recognized by `tempo-complete-tag' and expanded to the
63;; complete template. By default the tags are added to a global list
64;; of template tags, and are matched against the last word before
65;; point. But if you assign your tags to a specific list, you can also
66;; specify another method for matching text in the buffer against the
67;; tags. In the HTML mode, for instance, the tags are matched against
68;; the text between the last `<' and point.
69
70;; When defining a template named `foo', a symbol named
71;; `tempo-template-foo' will be created whose value as a variable will
72;; be the template definition, and its function value will be an
73;; interactive function that inserts the template at the point.
74
813f532d
RS
75;; The latest tempo.el distribution can be fetched from
76;; ftp.lysator.liu.se in the directory /pub/emacs
77
785c4478
RS
78;;; Known bugs:
79
80;; If the 'o is the first element in a template, strange things can
81;; happen when the template is inserted at the beginning of a
82;; line. This is due to strange behaviour in open-line. But it should
83;; be easily avoided.
84
85;; The 'o tag is also a problem when including the region. This will
86;; be looked into.
87
88;; Clicking mouse-2 in the completion buffer gives strange results.
89
90;; There is a bug in some emacs versions that prevents completion from
91;; working. If it doesn't work for you, send me a note indicating your
92;; emacs version and your problems.
93
34da6b16
RS
94;;; Contributors:
95
96;; These people have given me importand feedback and new ideas for
97;; tempo.el. Thanks.
98
99;; Nelson Minar <nelson@santafe.edu>
100;; Richard Stallman <rms@gnu.ai.mit.edu>
101;; Lars Lindberg <Lars.Lindberg@sypro.cap.se>
102;; Glen Whitney <Glen.Whitney@math.lsa.umich.edu>
103
813f532d
RS
104;;; Code:
105
785c4478 106;; (provide 'tempo)
813f532d 107
785c4478 108;;; User options
813f532d
RS
109
110(defvar tempo-interactive nil
111 "*Prompt user for strings in templates.
112If this variable is non-nil, `tempo-insert' prompts the
113user for text to insert in the templates")
114
70b5dc25
RS
115(defvar tempo-insert-region nil
116 "*Automatically insert current region when there is a `r' in the template
117If this variable is NIL, `r' elements will be treated just like `p'
118elements, unless the template function is given a prefix (or a non-nil
785c4478
RS
119argument). If this variable is non-NIL, the behaviour is reversed.
120
121In Transient Mark mode, this option is unused.")
70b5dc25
RS
122
123(defvar tempo-show-completion-buffer t
124 "*If non-NIL, show a buffer with possible completions, when only
125a partial completion can be found")
126
127(defvar tempo-leave-completion-buffer nil
128 "*If NIL, a completion buffer generated by \\[tempo-complete-tag]
129disappears at the next keypress; otherwise, it remains forever.")
130
785c4478
RS
131;;; Internal variables
132
813f532d
RS
133(defvar tempo-insert-string-functions nil
134 "List of functions to run when inserting a string.
135Each function is called with a single arg, STRING." )
136
137(defvar tempo-tags nil
138 "An association list with tags and corresponding templates")
139
140(defvar tempo-local-tags '((tempo-tags . nil))
141 "A list of locally installed tag completion lists.
813f532d
RS
142It is a association list where the car of every element is a symbol
143whose varable value is a template list. The cdr part, if non-nil, is a
144function or a regexp that defines the string to match. See the
145documentation for the function `tempo-complete-tag' for more info.
146
147`tempo-tags' is always in the last position in this list.")
148
785c4478
RS
149(defvar tempo-collection nil
150 "A collection of all the tags defined for the current buffer.")
151
152(defvar tempo-dirty-collection t
153 "Indicates if the tag collection needs to be rebuilt.")
154
813f532d
RS
155(defvar tempo-marks nil
156 "A list of marks to jump to with `\\[tempo-forward-mark]' and `\\[tempo-backward-mark]'.")
157
785c4478
RS
158(defvar tempo-match-finder "\\b\\([^\\b]+\\)\\="
159 "The regexp or function used to find the string to match against tags.
160
161If `tempo-match-finder is a string, it should contain a regular
162expression with at least one \\( \\) pair. When searching for tags,
163`tempo-complete-tag' calls `re-search-backward' with this string, and
164the string between the first \\( and \\) is used for matching against
165each string in the tag list. If one is found, the whole text between
166the first \\( and the point is replaced with the inserted template.
167
168You will probably want to include \\ \= at the end of the regexp to
169make sure that the string is matched only against text adjacent to the
170point.
171
172If `tempo-match-finder' is a symbol, it should be a function that
173returns a pair of the form (STRING . POS), where STRING is the string
174used for matching and POS is the buffer position after which text
175should be replaced with a template.")
176
177(defvar tempo-user-elements nil
178 "Element handlers for user-defined elements.
179A list of symbols which are bound to functions that take one argument.
180This function should return somthing to be sent to `tempo-insert' if
181it recognizes the argument, and NIL otherwise")
813f532d 182
70b5dc25
RS
183(defvar tempo-named-insertions nil
184 "Temporary storage for named insertions")
185
785c4478
RS
186(defvar tempo-region-start (make-marker)
187 "Region start when inserting around the region")
188
189(defvar tempo-region-stop (make-marker)
190 "Region stop when inserting around the region")
191
813f532d
RS
192;; Make some variables local to every buffer
193
194(make-variable-buffer-local 'tempo-marks)
195(make-variable-buffer-local 'tempo-local-tags)
785c4478
RS
196(make-variable-buffer-local 'tempo-match-finder)
197(make-variable-buffer-local 'tempo-collection)
198(make-variable-buffer-local 'tempo-dirty-collection)
813f532d
RS
199
200;;; Functions
201
34da6b16
RS
202;;; First some useful functions and macros
203
204(defun tempo-mapc (fun lst)
205 (if (null lst) nil
206 (funcall fun (car lst))
207 (tempo-mapc fun (cdr lst))))
208
209(defmacro tempo-dolist (il &rest forms)
210 (let ((i (car il))
211 (l (car (cdr il))))
212 (list 'tempo-mapc
213 (list 'function (append (list 'lambda
7f4d3657 214 (list i))
34da6b16 215 forms))
7f4d3657 216 l)))
34da6b16
RS
217(put 'tempo-dolist 'lisp-indent-function 1)
218
813f532d
RS
219;;
220;; tempo-define-template
221
222(defun tempo-define-template (name elements &optional tag documentation taglist)
223 "Define a template.
224This function creates a template variable `tempo-template-NAME' and an
225interactive function `tempo-template-NAME' that inserts the template
226at the point. The created function is returned.
227
228NAME is a string that contains the name of the template, ELEMENTS is a
229list of elements in the template, TAG is the tag used for completion,
230DOCUMENTATION is the documentation string for the insertion command
231created, and TAGLIST (a symbol) is the tag list that TAG (if provided)
232should be added to). If TAGLIST is nil and TAG is non-nil, TAG is
233added to `tempo-tags'
234
235The elements in ELEMENTS can be of several types:
236
237 - A string. It is sent to the hooks in `tempo-insert-string-functions',
238 and the result is inserted.
239 - The symbol 'p. This position is saved in `tempo-marks'.
240 - The symbol 'r. If `tempo-insert' is called with ON-REGION non-nil
241 the current region is placed here. Otherwise it works like 'p.
70b5dc25 242 - (p PROMPT <NAME>) If `tempo-interactive' is non-nil, the user is
813f532d 243 prompted in the minbuffer with PROMPT for a string to be inserted.
70b5dc25
RS
244 If the optional parameter NAME is non-nil, the text is saved for
245 later insertion with the `s' tag.
785c4478 246 If `tempo-interactive' is nil, it works like 'p.
70b5dc25 247 - (r PROMPT) like the previous, but if `tempo-interactive' is nil
813f532d 248 and `tempo-insert' is called with ON-REGION non-nil, the current
785c4478
RS
249 region is placed here. This usually happens when you call the
250 template function with a prefix argument.
70b5dc25
RS
251 - (s NAME) Inserts text previously read with the (p ..) construct.
252 Finds the insertion saved under NAME and inserts it. Acts like 'p
253 if tempo-interactive is nil.
813f532d
RS
254 - '& If there is only whitespace between the line start and point,
255 nothing happens. Otherwise a newline is inserted.
256 - '% If there is only whitespace between point and end-of-line
257 nothing happens. Otherwise a newline is inserted.
258 - 'n inserts a newline.
259 - '> The line is indented using `indent-according-to-mode'. Note that
260 you often should place this item after the text you want on the
261 line.
785c4478
RS
262 - 'n> Inserts a newline and indents line.
263 - 'o Like '% but leaves the point before the newline.
813f532d
RS
264 - nil. It is ignored.
265 - Anything else. It is evaluated and the result is parsed again."
266
267 (let* ((template-name (intern (concat "tempo-template-"
268 name)))
269 (command-name template-name))
270 (set template-name elements)
271 (fset command-name (list 'lambda (list '&optional 'arg)
272 (or documentation
273 (concat "Insert a " name "."))
274 (list 'interactive "*P")
275 (list 'tempo-insert-template (list 'quote
70b5dc25
RS
276 template-name)
277 (list 'if 'tempo-insert-region
278 (list 'not 'arg) 'arg))))
813f532d
RS
279 (and tag
280 (tempo-add-tag tag template-name taglist))
281 command-name))
282
283;;;
284;;; tempo-insert-template
285
286(defun tempo-insert-template (template on-region)
287 "Insert a template.
288TEMPLATE is the template to be inserted. If ON-REGION is non-nil the
785c4478
RS
289`r' elements are replaced with the current region. In Transient Mark
290mode, ON-REGION is ignored and assumed true if the region is active."
291 (if (and (boundp 'transient-mark-mode)
292 transient-mark-mode
293 mark-active)
294 (setq on-region t))
813f532d 295 (and on-region
785c4478
RS
296 (set-marker tempo-region-start (min (mark) (point)))
297 (set-marker tempo-region-stop (max (mark) (point))))
298 (if on-region
299 (goto-char tempo-region-start))
813f532d
RS
300 (save-excursion
301 (tempo-insert-mark (point-marker))
785c4478
RS
302 (mapcar (function (lambda (elt)
303 (tempo-insert elt on-region)))
813f532d
RS
304 (symbol-value template))
305 (tempo-insert-mark (point-marker)))
70b5dc25 306 (tempo-forward-mark)
785c4478
RS
307 (tempo-forget-insertions)
308 (and (boundp 'transient-mark-mode)
309 transient-mark-mode
310 (deactivate-mark)))
813f532d
RS
311
312;;;
313;;; tempo-insert
314
785c4478 315(defun tempo-insert (element on-region)
813f532d 316 "Insert a template element.
785c4478
RS
317Insert one element from a template. If ON-REGION is non-nil the `r'
318elements are replaced with the current region.
319
320See documentation for `tempo-define-template' for the kind of elements
321possible."
813f532d
RS
322 (cond ((stringp element) (tempo-process-and-insert-string element))
323 ((and (consp element) (eq (car element) 'p))
324 (tempo-insert-prompt (cdr element)))
34da6b16
RS
325 ((and (consp element) (eq (car element) 'P))
326 (let ((tempo-interactive t))
327 (tempo-insert-prompt (cdr element))))
813f532d
RS
328 ((and (consp element) (eq (car element) 'r))
329 (if on-region
785c4478 330 (goto-char tempo-region-stop)
813f532d 331 (tempo-insert-prompt (cdr element))))
70b5dc25 332 ((and (consp element) (eq (car element) 's))
34da6b16 333 (tempo-insert-named (car (cdr element))))
785c4478
RS
334 ((and (consp element) (eq (car element) 'l))
335 (mapcar (function (lambda (elt) (tempo-insert elt on-region)))
336 (cdr element)))
813f532d
RS
337 ((eq element 'p) (tempo-insert-mark (point-marker)))
338 ((eq element 'r) (if on-region
785c4478 339 (goto-char tempo-region-stop)
813f532d 340 (tempo-insert-mark (point-marker))))
785c4478
RS
341 ((eq element 'r>) (if on-region
342 (progn
343 (goto-char tempo-region-stop)
344 (indent-region (mark) (point) nil))
345 (tempo-insert-mark (point-marker))))
813f532d
RS
346 ((eq element '>) (indent-according-to-mode))
347 ((eq element '&) (if (not (or (= (current-column) 0)
348 (save-excursion
349 (re-search-backward
350 "^\\s-*\\=" nil t))))
351 (insert "\n")))
352 ((eq element '%) (if (not (or (eolp)
353 (save-excursion
354 (re-search-forward
355 "\\=\\s-*$" nil t))))
356 (insert "\n")))
357 ((eq element 'n) (insert "\n"))
358 ((eq element 'n>) (insert "\n") (indent-according-to-mode))
785c4478
RS
359 ;; Bug: If the 'o is the first element in a template, strange
360 ;; things can happen when the template is inserted at the
361 ;; beginning of a line.
362 ((eq element 'o) (if (not (or on-region
363 (eolp)
364 (save-excursion
365 (re-search-forward
366 "\\=\\s-*$" nil t))))
367 (open-line 1)))
813f532d 368 ((null element))
785c4478
RS
369 (t (tempo-insert (or (tempo-is-user-element element)
370 (eval element))
371 on-region))))
813f532d
RS
372
373;;;
374;;; tempo-insert-prompt
375
376(defun tempo-insert-prompt (prompt)
377 "Prompt for a text string and insert it in the current buffer.
378If the variable `tempo-interactive' is non-nil the user is prompted
379for a string in the minibuffer, which is then inserted in the current
380buffer. If `tempo-interactive' is nil, the current point is placed on
70b5dc25 381`tempo-mark'.
813f532d 382
70b5dc25
RS
383PROMPT is the prompt string or a list containing the prompt string and
384a name to save the inserted text under."
813f532d 385 (if tempo-interactive
70b5dc25
RS
386 (let ((prompt-string (if (listp prompt)
387 (car prompt)
388 prompt))
389 (save-name (and (listp prompt) (nth 1 prompt)))
390 inserted-text)
391
392 (progn
393 (setq inserted-text (read-string prompt-string))
394 (insert inserted-text)
395 (if save-name
396 (tempo-remember-insertion save-name inserted-text))))
813f532d
RS
397 (tempo-insert-mark (point-marker))))
398
785c4478
RS
399;;;
400;;; tempo-is-user-element
401
402(defun tempo-is-user-element (element)
403 "Tries all the user-defined element handlers in
404`tempo-user-elements'"
405 ;; Sigh... I need (some list)
406 (catch 'found
407 (mapcar (function (lambda (handler)
408 (let ((result (funcall handler element)))
409 (if result (throw 'found result)))))
410 tempo-user-elements)
411 (throw 'found nil)))
412
70b5dc25
RS
413;;;
414;;; tempo-remember-insertion
415
416(defun tempo-remember-insertion (save-name string)
417 "Save the text in STRING under the name SAVE-NAME for later retrieval."
418 (setq tempo-named-insertions (cons (cons save-name string)
419 tempo-named-insertions)))
420
421;;;
422;;; tempo-forget-insertions
423
424(defun tempo-forget-insertions ()
425 "Forget all the saved named insertions."
426 (setq tempo-named-insertions nil))
427
428;;;
429;;; tempo-insert-named
430
34da6b16
RS
431(defun tempo-insert-named (name)
432 "Insert the previous insertion saved under a named specified in NAME.
433If there is no such name saved, a tempo mark is inserted."
434 (let* ((insertion (cdr (assq name tempo-named-insertions))))
70b5dc25
RS
435 (if insertion
436 (insert insertion)
34da6b16 437 (tempo-insert-mark (point-marker)))))
70b5dc25 438
813f532d
RS
439;;;
440;;; tempo-process-and-insert-string
441
442(defun tempo-process-and-insert-string (string)
443 "Insert a string from a template.
444Run a string through the preprocessors in `tempo-insert-string-functions'
445and insert the results."
813f532d
RS
446 (cond ((null tempo-insert-string-functions)
447 nil)
448 ((symbolp tempo-insert-string-functions)
449 (setq string
34da6b16 450 (funcall tempo-insert-string-functions string)))
813f532d 451 ((listp tempo-insert-string-functions)
34da6b16
RS
452 (tempo-dolist (fn tempo-insert-string-functions)
453 (setq string (funcall fn string))))
813f532d
RS
454 (t
455 (error "Bogus value in tempo-insert-string-functions: %s"
456 tempo-insert-string-functions)))
457 (insert string))
458
459;;;
460;;; tempo-insert-mark
461
462(defun tempo-insert-mark (mark)
463 "Insert a mark `tempo-marks' while keeping it sorted"
464 (cond ((null tempo-marks) (setq tempo-marks (list mark)))
465 ((< mark (car tempo-marks)) (setq tempo-marks (cons mark tempo-marks)))
466 (t (let ((lp tempo-marks))
467 (while (and (cdr lp)
468 (<= (car (cdr lp)) mark))
469 (setq lp (cdr lp)))
470 (if (not (= mark (car lp)))
471 (setcdr lp (cons mark (cdr lp))))))))
472
473;;;
474;;; tempo-forward-mark
475
476(defun tempo-forward-mark ()
477 "Jump to the next mark in `tempo-forward-mark-list'."
478 (interactive)
479 (let ((next-mark (catch 'found
480 (mapcar
481 (function
482 (lambda (mark)
483 (if (< (point) mark)
484 (throw 'found mark))))
485 tempo-marks)
486 ;; return nil if not found
487 nil)))
488 (if next-mark
489 (goto-char next-mark))))
490
491;;;
492;;; tempo-backward-mark
493
494(defun tempo-backward-mark ()
495 "Jump to the previous mark in `tempo-back-mark-list'."
496 (interactive)
497 (let ((prev-mark (catch 'found
498 (let (last)
499 (mapcar
500 (function
501 (lambda (mark)
502 (if (<= (point) mark)
503 (throw 'found last))
504 (setq last mark)))
505 tempo-marks)
506 last))))
507 (if prev-mark
508 (goto-char prev-mark))))
509
510;;;
511;;; tempo-add-tag
512
513(defun tempo-add-tag (tag template &optional tag-list)
514 "Add a template tag.
813f532d
RS
515Add the TAG, that should complete to TEMPLATE to the list in TAG-LIST,
516or to `tempo-tags' if TAG-LIST is nil."
517
518 (interactive "sTag: \nCTemplate: ")
519 (if (null tag-list)
520 (setq tag-list 'tempo-tags))
521 (if (not (assoc tag (symbol-value tag-list)))
785c4478
RS
522 (set tag-list (cons (cons tag template) (symbol-value tag-list))))
523 (tempo-invalidate-collection))
813f532d
RS
524
525;;;
526;;; tempo-use-tag-list
527
528(defun tempo-use-tag-list (tag-list &optional completion-function)
529 "Install TAG-LIST to be used for template completion in the current buffer.
813f532d 530TAG-LIST is a symbol whose variable value is a tag list created with
785c4478 531`tempo-add-tag'.
813f532d 532
785c4478
RS
533COMPLETION-FUNCTION is an obsolete option for specifyingis an optional
534function or string that is used by `\\[tempo-complete-tag]' to find a
535string to match the tag against. It has the same definition as the
536variable `tempo-match-finder'. In this version, supplying a
537COMPLETION-FUNCTION just sets `tempo-match-finder' locally."
813f532d
RS
538 (let ((old (assq tag-list tempo-local-tags)))
539 (if old
540 (setcdr old completion-function)
541 (setq tempo-local-tags (cons (cons tag-list completion-function)
785c4478
RS
542 tempo-local-tags))))
543 (if completion-function
544 (setq tempo-match-finder completion-function))
545 (tempo-invalidate-collection))
546
547;;;
548;;; tempo-invalidate-collection
549
550(defun tempo-invalidate-collection ()
551 "Marks the tag collection as obsolete.
552Whenever it is needed again it will be rebuilt."
553 (setq tempo-dirty-collection t))
554
555;;;
556;;; tempo-build-collection
557
558(defun tempo-build-collection ()
559 "Build a collection of all the tags and return it.
560If `tempo-dirty-collection' is NIL, the old collection is reused."
561 (setq tempo-dirty nil)
562 (or (and (not tempo-dirty-collection)
563 tempo-collection)
564 (setq tempo-collection
565 (apply (function append)
566 (mapcar (function (lambda (tag-list)
567 ; If the format for
568 ; tempo-local-tags changes,
569 ; change this
570 (eval (car tag-list))))
571 tempo-local-tags)))))
813f532d
RS
572
573;;;
574;;; tempo-find-match-string
575
576(defun tempo-find-match-string (finder)
577 "Find a string to be matched against a tag list.
34da6b16
RS
578FINDER is a function or a string. Returns (STRING . POS), or nil
579if no reasonable string is found."
813f532d 580 (cond ((stringp finder)
34da6b16
RS
581 (let (successful)
582 (save-excursion
583 (or (setq successful (re-search-backward finder nil t))
584 0))
585 (if successful
586 (cons (buffer-substring (match-beginning 1)
587 (match-end 1)) ; This seems to be a
588 ; bug in emacs
589 (match-beginning 1))
590 nil)))
813f532d
RS
591 (t
592 (funcall finder))))
593
594;;;
595;;; tempo-complete-tag
596
597(defun tempo-complete-tag (&optional silent)
70b5dc25 598 "Look for a tag and expand it.
34da6b16
RS
599All the tags in the tag lists in `tempo-local-tags'
600(this includes `tempo-tags') are searched for a match for the text
601before the point. The way the string to match for is determined can
602be altered with the variable `tempo-match-finder'. If
603`tempo-match-finder' returns nil, then the results are the same as
604no match at all.
785c4478
RS
605
606If a single match is found, the corresponding template is expanded in
607place of the matching string.
608
609If a partial completion or no match at all is found, and SILENT is
610non-NIL, the function will give a signal.
611
612If a partial completion is found and `tempo-show-completion-buffer' is
613non-NIL, a buffer containing possible completions is displayed."
614
615 ;; This function may look like a hack, but this is how I want it to
616 ;; work.
617 (interactive "*")
618 (let* ((collection (tempo-build-collection))
619 (match-info (tempo-find-match-string tempo-match-finder))
620 (match-string (car match-info))
621 (match-start (cdr match-info))
622 (exact (assoc match-string collection))
623 (compl (or (car exact)
34da6b16 624 (and match-info (try-completion match-string collection)))))
785c4478 625 (if compl (delete-region match-start (point)))
34da6b16
RS
626 (cond ((null match-info) (or silent (ding)))
627 ((null compl) (or silent (ding)))
785c4478
RS
628 ((eq compl t) (tempo-insert-template
629 (cdr (assoc match-string
630 collection))
631 nil))
632 (t (if (setq exact (assoc compl collection))
633 (tempo-insert-template (cdr exact) nil)
634 (insert compl)
635 (or silent (ding))
636 (if tempo-show-completion-buffer
637 (tempo-display-completions match-string
638 collection)))))))
813f532d 639
813f532d 640
70b5dc25
RS
641;;;
642;;; tempo-display-completions
643
644(defun tempo-display-completions (string tag-list)
645 "Show a buffer containing possible completions for STRING."
646 (if tempo-leave-completion-buffer
647 (with-output-to-temp-buffer "*Completions*"
648 (display-completion-list
649 (all-completions string tag-list)))
650 (save-window-excursion
651 (with-output-to-temp-buffer "*Completions*"
652 (display-completion-list
653 (all-completions string tag-list)))
654 (sit-for 32767))))
655
34da6b16
RS
656;;;
657;;; tempo-expand-if-complete
658
659(defun tempo-expand-if-complete ()
660 "Expand the tag before point if it is complete.
661Returns non-nil if an expansion was made and nil otherwise.
662
663This could as an example be used in a command that is bound to the
664space bar, and looks something like this:
665
666(defun tempo-space ()
667 (interactive \"*\")
668 (or (tempo-expand-if-complete)
669 (insert \" \")))"
670
671 (interactive "*")
672 (let* ((collection (tempo-build-collection))
673 (match-info (tempo-find-match-string tempo-match-finder))
674 (match-string (car match-info))
675 (match-start (cdr match-info))
676 (exact (assoc match-string collection)))
677 (if exact
678 (progn
679 (delete-region match-start (point))
680 (tempo-insert-template (cdr exact) nil)
681 t)
682 nil)))
683
785c4478
RS
684(provide 'tempo)
685
813f532d 686;;; tempo.el ends here