Remove CVS merge cookie left in.
[emacs.git] / lisp / tempo.el
blobae9c7731b445cea67bf169cd79e4ae082dc4c2e4
1 ;;; tempo.el --- Flexible template insertion
3 ;; Copyright (C) 1994, 1995 Free Software Foundation, Inc.
5 ;; Author: David K}gedal <davidk@lysator.liu.se >
6 ;; Created: 16 Feb 1994
7 ;; K}gedal's last version number: 1.2.4
8 ;; Keywords: extensions, languages, tools
10 ;; This file is part of GNU Emacs.
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.
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.
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 the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
29 ;; This file provides a simple way to define powerful templates, or
30 ;; macros, if you wish. It is mainly intended for, but not limited to,
31 ;; other programmers to be used for creating shortcuts for editing
32 ;; certain kind of documents. It was originally written to be used by
33 ;; a HTML editing mode written by Nelson Minar <nelson@santafe.edu>,
34 ;; and his html-helper-mode.el is probably the best example of how to
35 ;; use this program.
37 ;; A template is defined as a list of items to be inserted in the
38 ;; current buffer at point. Some of the items can be simple strings,
39 ;; while other can control formatting or define special points of
40 ;; interest in the inserted text.
42 ;; If a template defines a "point of interest" that point is inserted
43 ;; in a buffer-local list of "points of interest" that the user can
44 ;; jump between with the commands `tempo-backward-mark' and
45 ;; `tempo-forward-mark'. If the template definer provides a prompt for
46 ;; the point, and the variable `tempo-interactive' is non-nil, the
47 ;; user will be prompted for a string to be inserted in the buffer,
48 ;; using the minibuffer.
50 ;; The template can also define one point to be replaced with the
51 ;; current region if the template command is called with a prefix (or
52 ;; a non-nil argument).
54 ;; More flexible templates can be created by including lisp symbols,
55 ;; which will be evaluated as variables, or lists, which will be
56 ;; evaluated as lisp expressions.
58 ;; See the documentation for tempo-define-template for the different
59 ;; items that can be used to define a tempo template.
61 ;; One of the more powerful features of tempo templates are automatic
62 ;; completion. With every template can be assigned a special tag that
63 ;; should be recognized by `tempo-complete-tag' and expanded to the
64 ;; complete template. By default the tags are added to a global list
65 ;; of template tags, and are matched against the last word before
66 ;; point. But if you assign your tags to a specific list, you can also
67 ;; specify another method for matching text in the buffer against the
68 ;; tags. In the HTML mode, for instance, the tags are matched against
69 ;; the text between the last `<' and point.
71 ;; When defining a template named `foo', a symbol named
72 ;; `tempo-template-foo' will be created whose value as a variable will
73 ;; be the template definition, and its function value will be an
74 ;; interactive function that inserts the template at the point.
76 ;; The latest tempo.el distribution can be fetched from
77 ;; ftp.lysator.liu.se in the directory /pub/emacs
79 ;; There is also a WWW page at
80 ;; http://www.lysator.liu.se/~davidk/elisp/ which has some information
82 ;;; Known bugs:
84 ;; If the 'o is the first element in a template, strange things can
85 ;; happen when the template is inserted at the beginning of a
86 ;; line. This is due to strange behaviour in open-line. But it should
87 ;; be easily avoided.
89 ;; The 'o tag is also a problem when including the region. This will
90 ;; be looked into.
92 ;; Clicking mouse-2 in the completion buffer gives strange results.
94 ;; There is a bug in some emacs versions that prevents completion from
95 ;; working. If it doesn't work for you, send me a note indicating your
96 ;; emacs version and your problems.
98 ;;; Contributors:
100 ;; These people have given me important feedback and new ideas for
101 ;; tempo.el. Thanks.
103 ;; Nelson Minar <nelson@santafe.edu>
104 ;; Richard Stallman <rms@gnu.org>
105 ;; Lars Lindberg <Lars.Lindberg@sypro.cap.se>
106 ;; Glen Whitney <Glen.Whitney@math.lsa.umich.edu>
108 ;;; Code:
110 ;; (provide 'tempo)
112 ;;; User options
114 (defgroup tempo nil
115 "Flexible template insertion."
116 :prefix "tempo-"
117 :group 'tools)
119 (defcustom tempo-interactive nil
120 "*Prompt user for strings in templates.
121 If this variable is non-nil, `tempo-insert' prompts the
122 user for text to insert in the templates"
123 :type 'boolean
124 :group 'tempo)
126 (defcustom tempo-insert-region nil
127 "*Automatically insert current region when there is a `r' in the template
128 If this variable is NIL, `r' elements will be treated just like `p'
129 elements, unless the template function is given a prefix (or a non-nil
130 argument). If this variable is non-NIL, the behaviour is reversed.
132 In Transient Mark mode, this option is unused."
133 :type 'boolean
134 :group 'tempo)
136 (defcustom tempo-show-completion-buffer t
137 "*If non-NIL, show a buffer with possible completions, when only
138 a partial completion can be found"
139 :type 'boolean
140 :group 'tempo)
142 (defcustom tempo-leave-completion-buffer nil
143 "*If NIL, a completion buffer generated by \\[tempo-complete-tag]
144 disappears at the next keypress; otherwise, it remains forever."
145 :type 'boolean
146 :group 'tempo)
148 ;;; Internal variables
150 (defvar tempo-insert-string-functions nil
151 "List of functions to run when inserting a string.
152 Each function is called with a single arg, STRING and should return
153 another string. This could be used for making all strings upcase by
154 setting it to '(upcase), for example.")
156 (defvar tempo-tags nil
157 "An association list with tags and corresponding templates")
159 (defvar tempo-local-tags '((tempo-tags . nil))
160 "A list of locally installed tag completion lists.
161 It is a association list where the car of every element is a symbol
162 whose variable value is a template list. The cdr part, if non-nil, is a
163 function or a regexp that defines the string to match. See the
164 documentation for the function `tempo-complete-tag' for more info.
166 `tempo-tags' is always in the last position in this list.")
168 (defvar tempo-collection nil
169 "A collection of all the tags defined for the current buffer.")
171 (defvar tempo-dirty-collection t
172 "Indicates if the tag collection needs to be rebuilt.")
174 (defvar tempo-marks nil
175 "A list of marks to jump to with `\\[tempo-forward-mark]' and `\\[tempo-backward-mark]'.")
177 (defvar tempo-match-finder "\\b\\([^\\b]+\\)\\="
178 "The regexp or function used to find the string to match against tags.
180 If `tempo-match-finder is a string, it should contain a regular
181 expression with at least one \\( \\) pair. When searching for tags,
182 `tempo-complete-tag' calls `re-search-backward' with this string, and
183 the string between the first \\( and \\) is used for matching against
184 each string in the tag list. If one is found, the whole text between
185 the first \\( and the point is replaced with the inserted template.
187 You will probably want to include \\ \= at the end of the regexp to
188 make sure that the string is matched only against text adjacent to the
189 point.
191 If `tempo-match-finder' is a symbol, it should be a function that
192 returns a pair of the form (STRING . POS), where STRING is the string
193 used for matching and POS is the buffer position after which text
194 should be replaced with a template.")
196 (defvar tempo-user-elements nil
197 "Element handlers for user-defined elements.
198 A list of symbols which are bound to functions that take one argument.
199 This function should return something to be sent to `tempo-insert' if
200 it recognizes the argument, and NIL otherwise")
202 (defvar tempo-named-insertions nil
203 "Temporary storage for named insertions")
205 (defvar tempo-region-start (make-marker)
206 "Region start when inserting around the region")
208 (defvar tempo-region-stop (make-marker)
209 "Region stop when inserting around the region")
211 ;; Make some variables local to every buffer
213 (make-variable-buffer-local 'tempo-marks)
214 (make-variable-buffer-local 'tempo-local-tags)
215 (make-variable-buffer-local 'tempo-match-finder)
216 (make-variable-buffer-local 'tempo-collection)
217 (make-variable-buffer-local 'tempo-dirty-collection)
219 ;;; Functions
222 ;; tempo-define-template
224 (defun tempo-define-template (name elements &optional tag documentation taglist)
225 "Define a template.
226 This function creates a template variable `tempo-template-NAME' and an
227 interactive function `tempo-template-NAME' that inserts the template
228 at the point. The created function is returned.
230 NAME is a string that contains the name of the template, ELEMENTS is a
231 list of elements in the template, TAG is the tag used for completion,
232 DOCUMENTATION is the documentation string for the insertion command
233 created, and TAGLIST (a symbol) is the tag list that TAG (if provided)
234 should be added to). If TAGLIST is nil and TAG is non-nil, TAG is
235 added to `tempo-tags'
237 The elements in ELEMENTS can be of several types:
239 - A string. It is sent to the hooks in `tempo-insert-string-functions',
240 and the result is inserted.
241 - The symbol 'p. This position is saved in `tempo-marks'.
242 - The symbol 'r. If `tempo-insert' is called with ON-REGION non-nil
243 the current region is placed here. Otherwise it works like 'p.
244 - (p PROMPT <NAME> <NOINSERT>) If `tempo-interactive' is non-nil, the
245 user is prompted in the minbuffer with PROMPT for a string to be
246 inserted. If the optional parameter NAME is non-nil, the text is
247 saved for later insertion with the `s' tag. If there already is
248 something saved under NAME that value is used instead and no
249 prompting is made. If NOINSERT is provided and non-nil, nothing is
250 inserted, but text is still saved when a NAME is provided. For
251 clarity, the symbol 'noinsert should be used as argument.
252 - (P PROMPT <NAME> <NOINSERT>) Works just like the previous tag, but
253 forces tempo-interactive to be true.
254 - (r PROMPT <NAME> <NOINSERT>) like the previous, but if
255 `tempo-interactive' is nil and `tempo-insert' is called with
256 ON-REGION non-nil, the current region is placed here. This usually
257 happens when you call the template function with a prefix argument.
258 - (s NAME) Inserts text previously read with the (p ..) construct.
259 Finds the insertion saved under NAME and inserts it. Acts like 'p
260 if tempo-interactive is nil.
261 - '& If there is only whitespace between the line start and point,
262 nothing happens. Otherwise a newline is inserted.
263 - '% If there is only whitespace between point and end-of-line
264 nothing happens. Otherwise a newline is inserted.
265 - 'n inserts a newline.
266 - '> The line is indented using `indent-according-to-mode'. Note that
267 you often should place this item after the text you want on the
268 line.
269 - 'r> Like r, but it also indents the region.
270 - 'n> Inserts a newline and indents line.
271 - 'o Like '% but leaves the point before the newline.
272 - nil. It is ignored.
273 - Anything else. It is evaluated and the result is treated as an
274 element to be inserted. One additional tag is useful for these
275 cases. If an expression returns a list '(l foo bar), the elements
276 after 'l will be inserted according to the usual rules. This makes
277 it possible to return several elements from one expression."
279 (let* ((template-name (intern (concat "tempo-template-"
280 name)))
281 (command-name template-name))
282 (set template-name elements)
283 (fset command-name (list 'lambda (list '&optional 'arg)
284 (or documentation
285 (concat "Insert a " name "."))
286 (list 'interactive "*P")
287 (list 'tempo-insert-template (list 'quote
288 template-name)
289 (list 'if 'tempo-insert-region
290 (list 'not 'arg) 'arg))))
291 (and tag
292 (tempo-add-tag tag template-name taglist))
293 command-name))
296 ;;; tempo-insert-template
298 (defun tempo-insert-template (template on-region)
299 "Insert a template.
300 TEMPLATE is the template to be inserted. If ON-REGION is non-nil the
301 `r' elements are replaced with the current region. In Transient Mark
302 mode, ON-REGION is ignored and assumed true if the region is active."
303 (unwind-protect
304 (progn
305 (if (or (and (boundp 'transient-mark-mode) ; For Emacs
306 transient-mark-mode
307 mark-active)
308 (and (boundp 'zmacs-regions) ; For XEmacs
309 zmacs-regions (mark)))
310 (setq on-region t))
311 (and on-region
312 (set-marker tempo-region-start (min (mark) (point)))
313 (set-marker tempo-region-stop (max (mark) (point))))
314 (if on-region
315 (goto-char tempo-region-start))
316 (save-excursion
317 (tempo-insert-mark (point-marker))
318 (mapcar (function (lambda (elt)
319 (tempo-insert elt on-region)))
320 (symbol-value template))
321 (tempo-insert-mark (point-marker)))
322 (tempo-forward-mark))
323 (tempo-forget-insertions)
324 ;; Should I check for zmacs here too???
325 (and (boundp 'transient-mark-mode)
326 transient-mark-mode
327 (deactivate-mark))))
330 ;;; tempo-insert
332 (defun tempo-insert (element on-region)
333 "Insert a template element.
334 Insert one element from a template. If ON-REGION is non-nil the `r'
335 elements are replaced with the current region.
337 See documentation for `tempo-define-template' for the kind of elements
338 possible."
339 (cond ((stringp element) (tempo-process-and-insert-string element))
340 ((and (consp element)
341 (eq (car element) 'p)) (tempo-insert-prompt-compat
342 (cdr element)))
343 ((and (consp element)
344 (eq (car element) 'P)) (let ((tempo-interactive t))
345 (tempo-insert-prompt-compat
346 (cdr element))))
347 ;;; ((and (consp element)
348 ;;; (eq (car element) 'v)) (tempo-save-named
349 ;;; (nth 1 element)
350 ;;; nil
351 ;;; (nth 2 element)))
352 ((and (consp element)
353 (eq (car element) 'r)) (if on-region
354 (goto-char tempo-region-stop)
355 (tempo-insert-prompt-compat
356 (cdr element))))
357 ((and (consp element)
358 (eq (car element) 's)) (tempo-insert-named (car (cdr element))))
359 ((and (consp element)
360 (eq (car element) 'l)) (mapcar (function
361 (lambda (elt)
362 (tempo-insert elt on-region)))
363 (cdr element)))
364 ((eq element 'p) (tempo-insert-mark (point-marker)))
365 ((eq element 'r) (if on-region
366 (goto-char tempo-region-stop)
367 (tempo-insert-mark (point-marker))))
368 ((eq element 'r>) (if on-region
369 (progn
370 (goto-char tempo-region-stop)
371 (indent-region (mark) (point) nil))
372 (tempo-insert-mark (point-marker))))
373 ((eq element '>) (indent-according-to-mode))
374 ((eq element '&) (if (not (or (= (current-column) 0)
375 (save-excursion
376 (re-search-backward
377 "^\\s-*\\=" nil t))))
378 (insert "\n")))
379 ((eq element '%) (if (not (or (eolp)
380 (save-excursion
381 (re-search-forward
382 "\\=\\s-*$" nil t))))
383 (insert "\n")))
384 ((eq element 'n) (insert "\n"))
385 ((eq element 'n>) (insert "\n") (indent-according-to-mode))
386 ;; Bug: If the 'o is the first element in a template, strange
387 ;; things can happen when the template is inserted at the
388 ;; beginning of a line.
389 ((eq element 'o) (if (not (or on-region
390 (eolp)
391 (save-excursion
392 (re-search-forward
393 "\\=\\s-*$" nil t))))
394 (open-line 1)))
395 ((null element))
396 (t (tempo-insert (or (tempo-is-user-element element)
397 (eval element))
398 on-region))))
401 ;;; tempo-insert-prompt
403 (defun tempo-insert-prompt-compat (prompt)
404 "Compatibility hack for tempo-insert-prompt.
405 PROMPT can be either a prompt string, or a list of arguments to
406 tempo-insert-prompt, or nil."
407 (if (consp prompt) ; not NIL either
408 (apply 'tempo-insert-prompt prompt)
409 (tempo-insert-prompt prompt)))
411 (defun tempo-insert-prompt (prompt &optional save-name no-insert)
412 "Prompt for a text string and insert it in the current buffer.
413 If the variable `tempo-interactive' is non-nil the user is prompted
414 for a string in the minibuffer, which is then inserted in the current
415 buffer. If `tempo-interactive' is nil, the current point is placed on
416 `tempo-mark'.
418 PROMPT is the prompt string, SAVE-NAME is a name to save the inserted
419 text under. If the optional argument NO-INSERT is non-nil, no text i
420 inserted. This can be useful when there is a SAVE-NAME.
422 If there already is a value for SAVE-NAME, it is used and the user is
423 never prompted."
424 (let (insertion
425 (previous (and save-name
426 (tempo-lookup-named save-name))))
427 (cond
428 ;; Insert previous value, unless no-insert is non-nil
429 ((and previous
430 (not no-insert))
431 (tempo-insert-named save-name)) ; A double lookup here, but who
432 ; cares
433 ;; If no-insert is non-nil, don't insert the previous value. Just
434 ;; keep it
435 (previous
436 nil)
437 ;; No previous value. Prompt or insert mark
438 (tempo-interactive
439 (if (not (stringp prompt))
440 (error "tempo: The prompt (%s) is not a string" prompt))
441 (setq insertion (read-string prompt))
442 (or no-insert
443 (insert insertion))
444 (if save-name
445 (tempo-save-named save-name insertion)))
447 (tempo-insert-mark (point-marker))))))
450 ;;; tempo-is-user-element
452 (defun tempo-is-user-element (element)
453 "Tries all the user-defined element handlers in
454 `tempo-user-elements'"
455 ;; Sigh... I need (some list)
456 (catch 'found
457 (mapcar (function (lambda (handler)
458 (let ((result (funcall handler element)))
459 (if result (throw 'found result)))))
460 tempo-user-elements)
461 (throw 'found nil)))
464 ;;; tempo-forget-insertions
466 (defun tempo-forget-insertions ()
467 "Forget all the saved named insertions."
468 (setq tempo-named-insertions nil))
471 ;;; tempo-save-named
473 (defun tempo-save-named (name data) ; Had an optional prompt for 'v
474 "Save some data for later insertion
475 The contents of DATA is saved under the name NAME.
477 The data can later be retrieved with `tempo-lookup-named'.
479 This function returns nil, so it can be used in a template without
480 inserting anything."
481 (setq tempo-named-insertions
482 (cons (cons name data)
483 tempo-named-insertions))
484 nil)
487 ;;; tempo-lookup-named
489 (defun tempo-lookup-named (name)
490 "Lookup some saved data under the name NAME.
491 Returns the data if NAME was found, and nil otherwise."
492 (cdr (assq name tempo-named-insertions)))
495 ;;; tempo-insert-named
497 (defun tempo-insert-named (name)
498 "Insert the previous insertion saved under a named specified in NAME.
499 If there is no such name saved, a tempo mark is inserted.
501 Note that if the data is a string, it will not be run through the string
502 processor."
503 (let* ((insertion (tempo-lookup-named name)))
504 (cond ((null insertion)
505 (tempo-insert-mark (point-marker)))
506 ((stringp insertion)
507 (insert insertion))
509 (tempo-insert insertion nil)))))
513 ;;; tempo-process-and-insert-string
515 (defun tempo-process-and-insert-string (string)
516 "Insert a string from a template.
517 Run a string through the preprocessors in `tempo-insert-string-functions'
518 and insert the results."
519 (cond ((null tempo-insert-string-functions)
520 nil)
521 ((symbolp tempo-insert-string-functions)
522 (setq string
523 (funcall tempo-insert-string-functions string)))
524 ((listp tempo-insert-string-functions)
525 (dolist (fn tempo-insert-string-functions)
526 (setq string (funcall fn string))))
528 (error "Bogus value in tempo-insert-string-functions: %s"
529 tempo-insert-string-functions)))
530 (insert string))
533 ;;; tempo-insert-mark
535 (defun tempo-insert-mark (mark)
536 "Insert a mark `tempo-marks' while keeping it sorted"
537 (cond ((null tempo-marks) (setq tempo-marks (list mark)))
538 ((< mark (car tempo-marks)) (setq tempo-marks (cons mark tempo-marks)))
539 (t (let ((lp tempo-marks))
540 (while (and (cdr lp)
541 (<= (car (cdr lp)) mark))
542 (setq lp (cdr lp)))
543 (if (not (= mark (car lp)))
544 (setcdr lp (cons mark (cdr lp))))))))
547 ;;; tempo-forward-mark
549 (defun tempo-forward-mark ()
550 "Jump to the next mark in `tempo-forward-mark-list'."
551 (interactive)
552 (let ((next-mark (catch 'found
553 (mapcar
554 (function
555 (lambda (mark)
556 (if (< (point) mark)
557 (throw 'found mark))))
558 tempo-marks)
559 ;; return nil if not found
560 nil)))
561 (if next-mark
562 (goto-char next-mark))))
565 ;;; tempo-backward-mark
567 (defun tempo-backward-mark ()
568 "Jump to the previous mark in `tempo-back-mark-list'."
569 (interactive)
570 (let ((prev-mark (catch 'found
571 (let (last)
572 (mapcar
573 (function
574 (lambda (mark)
575 (if (<= (point) mark)
576 (throw 'found last))
577 (setq last mark)))
578 tempo-marks)
579 last))))
580 (if prev-mark
581 (goto-char prev-mark))))
584 ;;; tempo-add-tag
586 (defun tempo-add-tag (tag template &optional tag-list)
587 "Add a template tag.
588 Add the TAG, that should complete to TEMPLATE to the list in TAG-LIST,
589 or to `tempo-tags' if TAG-LIST is nil."
591 (interactive "sTag: \nCTemplate: ")
592 (if (null tag-list)
593 (setq tag-list 'tempo-tags))
594 (if (not (assoc tag (symbol-value tag-list)))
595 (set tag-list (cons (cons tag template) (symbol-value tag-list))))
596 (tempo-invalidate-collection))
599 ;;; tempo-use-tag-list
601 (defun tempo-use-tag-list (tag-list &optional completion-function)
602 "Install TAG-LIST to be used for template completion in the current buffer.
603 TAG-LIST is a symbol whose variable value is a tag list created with
604 `tempo-add-tag'.
606 COMPLETION-FUNCTION is an obsolete option for specifying an optional
607 function or string that is used by `\\[tempo-complete-tag]' to find a
608 string to match the tag against. It has the same definition as the
609 variable `tempo-match-finder'. In this version, supplying a
610 COMPLETION-FUNCTION just sets `tempo-match-finder' locally."
611 (let ((old (assq tag-list tempo-local-tags)))
612 (if old
613 (setcdr old completion-function)
614 (setq tempo-local-tags (cons (cons tag-list completion-function)
615 tempo-local-tags))))
616 (if completion-function
617 (setq tempo-match-finder completion-function))
618 (tempo-invalidate-collection))
621 ;;; tempo-invalidate-collection
623 (defun tempo-invalidate-collection ()
624 "Marks the tag collection as obsolete.
625 Whenever it is needed again it will be rebuilt."
626 (setq tempo-dirty-collection t))
629 ;;; tempo-build-collection
631 (defun tempo-build-collection ()
632 "Build a collection of all the tags and return it.
633 If `tempo-dirty-collection' is NIL, the old collection is reused."
634 (prog1
635 (or (and (not tempo-dirty-collection)
636 tempo-collection)
637 (setq tempo-collection
638 (apply (function append)
639 (mapcar (function (lambda (tag-list)
640 ; If the format for
641 ; tempo-local-tags changes,
642 ; change this
643 (eval (car tag-list))))
644 tempo-local-tags))))
645 (setq tempo-dirty-collection nil)))
648 ;;; tempo-find-match-string
650 (defun tempo-find-match-string (finder)
651 "Find a string to be matched against a tag list.
652 FINDER is a function or a string. Returns (STRING . POS), or nil
653 if no reasonable string is found."
654 (cond ((stringp finder)
655 (let (successful)
656 (save-excursion
657 (or (setq successful (re-search-backward finder nil t))
659 (if successful
660 (cons (buffer-substring (match-beginning 1)
661 (match-end 1)) ; This seems to be a
662 ; bug in emacs
663 (match-beginning 1))
664 nil)))
666 (funcall finder))))
669 ;;; tempo-complete-tag
671 (defun tempo-complete-tag (&optional silent)
672 "Look for a tag and expand it.
673 All the tags in the tag lists in `tempo-local-tags'
674 \(this includes `tempo-tags') are searched for a match for the text
675 before the point. The way the string to match for is determined can
676 be altered with the variable `tempo-match-finder'. If
677 `tempo-match-finder' returns nil, then the results are the same as
678 no match at all.
680 If a single match is found, the corresponding template is expanded in
681 place of the matching string.
683 If a partial completion or no match at all is found, and SILENT is
684 non-NIL, the function will give a signal.
686 If a partial completion is found and `tempo-show-completion-buffer' is
687 non-NIL, a buffer containing possible completions is displayed."
689 ;; This function may look like a hack, but this is how I want it to
690 ;; work.
691 (interactive "*")
692 (let* ((collection (tempo-build-collection))
693 (match-info (tempo-find-match-string tempo-match-finder))
694 (match-string (car match-info))
695 (match-start (cdr match-info))
696 (exact (assoc match-string collection))
697 (compl (or (car exact)
698 (and match-info (try-completion match-string collection)))))
699 (if compl (delete-region match-start (point)))
700 (cond ((null match-info) (or silent (ding)))
701 ((null compl) (or silent (ding)))
702 ((eq compl t) (tempo-insert-template
703 (cdr (assoc match-string
704 collection))
705 nil))
706 (t (if (setq exact (assoc compl collection))
707 (tempo-insert-template (cdr exact) nil)
708 (insert compl)
709 (or silent (ding))
710 (if tempo-show-completion-buffer
711 (tempo-display-completions match-string
712 collection)))))))
716 ;;; tempo-display-completions
718 (defun tempo-display-completions (string tag-list)
719 "Show a buffer containing possible completions for STRING."
720 (if tempo-leave-completion-buffer
721 (with-output-to-temp-buffer "*Completions*"
722 (display-completion-list
723 (all-completions string tag-list)))
724 (save-window-excursion
725 (with-output-to-temp-buffer "*Completions*"
726 (display-completion-list
727 (all-completions string tag-list)))
728 (sit-for 32767))))
731 ;;; tempo-expand-if-complete
733 (defun tempo-expand-if-complete ()
734 "Expand the tag before point if it is complete.
735 Returns non-nil if an expansion was made and nil otherwise.
737 This could as an example be used in a command that is bound to the
738 space bar, and looks something like this:
740 \(defun tempo-space ()
741 (interactive \"*\")
742 (or (tempo-expand-if-complete)
743 (insert \" \")))"
745 (interactive "*")
746 (let* ((collection (tempo-build-collection))
747 (match-info (tempo-find-match-string tempo-match-finder))
748 (match-string (car match-info))
749 (match-start (cdr match-info))
750 (exact (assoc match-string collection)))
751 (if exact
752 (progn
753 (delete-region match-start (point))
754 (tempo-insert-template (cdr exact) nil)
756 nil)))
758 (provide 'tempo)
760 ;;; tempo.el ends here