(grep-regexp-alist): Set 5th arg `TYPE' to
[emacs.git] / lisp / tempo.el
blob282885e2d6557284a2359b4fa5bb391bb6972c13
1 ;;; tempo.el --- Flexible template insertion
3 ;; Copyright (C) 1994, 1995, 2004 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., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, 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 ;;; User options
112 (defgroup tempo nil
113 "Flexible template insertion."
114 :prefix "tempo-"
115 :group 'tools)
117 (defcustom tempo-interactive nil
118 "*Prompt user for strings in templates.
119 If this variable is non-nil, `tempo-insert' prompts the
120 user for text to insert in the templates."
121 :type 'boolean
122 :group 'tempo)
124 (defcustom tempo-insert-region nil
125 "*Automatically insert current region when there is a `r' in the template
126 If this variable is nil, `r' elements will be treated just like `p'
127 elements, unless the template function is given a prefix (or a non-nil
128 argument). If this variable is non-nil, the behavior is reversed.
130 In Transient Mark mode, this option is unused."
131 :type 'boolean
132 :group 'tempo)
134 (defcustom tempo-show-completion-buffer t
135 "*If non-nil, show a buffer with possible completions, when only
136 a partial completion can be found."
137 :type 'boolean
138 :group 'tempo)
140 (defcustom tempo-leave-completion-buffer nil
141 "*If nil, a completion buffer generated by \\[tempo-complete-tag]
142 disappears at the next keypress; otherwise, it remains forever."
143 :type 'boolean
144 :group 'tempo)
146 ;;; Internal variables
148 (defvar tempo-insert-string-functions nil
149 "List of functions to run when inserting a string.
150 Each function is called with a single arg, STRING and should return
151 another string. This could be used for making all strings upcase by
152 setting it to '(upcase), for example.")
154 (defvar tempo-tags nil
155 "An association list with tags and corresponding templates.")
157 (defvar tempo-local-tags '((tempo-tags . nil))
158 "A list of locally installed tag completion lists.
159 It is a association list where the car of every element is a symbol
160 whose variable value is a template list. The cdr part, if non-nil,
161 is a function or a regexp that defines the string to match. See the
162 documentation for the function `tempo-complete-tag' for more info.
164 `tempo-tags' is always in the last position in this list.")
166 (defvar tempo-collection nil
167 "A collection of all the tags defined for the current buffer.")
169 (defvar tempo-dirty-collection t
170 "Indicates if the tag collection needs to be rebuilt.")
172 (defvar tempo-marks nil
173 "A list of marks to jump to with `\\[tempo-forward-mark]' and `\\[tempo-backward-mark]'.")
175 (defvar tempo-match-finder "\\b\\([[:word:]]+\\)\\="
176 "The regexp or function used to find the string to match against tags.
178 If `tempo-match-finder' is a string, it should contain a regular
179 expression with at least one \\( \\) pair. When searching for tags,
180 `tempo-complete-tag' calls `re-search-backward' with this string, and
181 the string between the first \\( and \\) is used for matching against
182 each string in the tag list. If one is found, the whole text between
183 the first \\( and the point is replaced with the inserted template.
185 You will probably want to include \\=\\= at the end of the regexp to
186 make sure that the string is matched only against text adjacent to the
187 point.
189 If `tempo-match-finder' is a symbol, it should be a function that
190 returns a pair of the form (STRING . POS), where STRING is the string
191 used for matching and POS is the buffer position after which text
192 should be replaced with a template.")
194 (defvar tempo-user-elements nil
195 "Element handlers for user-defined elements.
196 A list of symbols which are bound to functions that take one argument.
197 This function should return something to be sent to `tempo-insert' if
198 it recognizes the argument, and nil otherwise.")
200 (defvar tempo-named-insertions nil
201 "Temporary storage for named insertions.")
203 (defvar tempo-region-start (make-marker)
204 "Region start when inserting around the region.")
206 (defvar tempo-region-stop (make-marker)
207 "Region stop when inserting around the region.")
209 ;; Make some variables local to every buffer
211 (make-variable-buffer-local 'tempo-marks)
212 (make-variable-buffer-local 'tempo-local-tags)
213 (make-variable-buffer-local 'tempo-match-finder)
214 (make-variable-buffer-local 'tempo-collection)
215 (make-variable-buffer-local 'tempo-dirty-collection)
217 ;;; Functions
220 ;; tempo-define-template
222 (defun tempo-define-template (name elements &optional tag documentation taglist)
223 "Define a template.
224 This function creates a template variable `tempo-template-NAME' and an
225 interactive function `tempo-template-NAME' that inserts the template
226 at the point. The created function is returned.
228 NAME is a string that contains the name of the template, ELEMENTS is a
229 list of elements in the template, TAG is the tag used for completion,
230 DOCUMENTATION is the documentation string for the insertion command
231 created, and TAGLIST (a symbol) is the tag list that TAG (if provided)
232 should be added to. If TAGLIST is nil and TAG is non-nil, TAG is
233 added to `tempo-tags'.
235 The elements in ELEMENTS can be of several types:
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'.
242 - (p PROMPT <NAME> <NOINSERT>): If `tempo-interactive' is non-nil, the
243 user is prompted in the minibuffer with PROMPT for a string to be
244 inserted. If the optional parameter NAME is non-nil, the text is
245 saved for later insertion with the `s' tag. If there already is
246 something saved under NAME that value is used instead and no
247 prompting is made. If NOINSERT is provided and non-nil, nothing is
248 inserted, but text is still saved when a NAME is provided. For
249 clarity, the symbol `noinsert' should be used as argument.
250 - (P PROMPT <NAME> <NOINSERT>): Works just like the previous tag, but
251 forces `tempo-interactive' to be true.
252 - (r PROMPT <NAME> <NOINSERT>): Like the previous tag, but if
253 `tempo-interactive' is nil and `tempo-insert' is called with
254 ON-REGION non-nil, the current region is placed here. This usually
255 happens when you call the template function with a prefix argument.
256 - (s NAME): Inserts text previously read with the (p ..) construct.
257 Finds the insertion saved under NAME and inserts it. Acts like `p'
258 if tempo-interactive is nil.
259 - `&': If there is only whitespace between the line start and point,
260 nothing happens. Otherwise a newline is inserted.
261 - `%': If there is only whitespace between point and end of line,
262 nothing happens. Otherwise a newline is inserted.
263 - `n': Inserts a newline.
264 - `>': The line is indented using `indent-according-to-mode'. Note
265 that you often should place this item after the text you want on
266 the line.
267 - `r>': Like `r', but it also indents the region.
268 - `n>': Inserts a newline and indents line.
269 - `o': Like `%' but leaves the point before the newline.
270 - nil: It is ignored.
271 - Anything else: It is evaluated and the result is treated as an
272 element to be inserted. One additional tag is useful for these
273 cases. If an expression returns a list '(l foo bar), the elements
274 after `l' will be inserted according to the usual rules. This makes
275 it possible to return several elements from one expression."
276 (let* ((template-name (intern (concat "tempo-template-"
277 name)))
278 (command-name template-name))
279 (set template-name elements)
280 (fset command-name (list 'lambda (list '&optional 'arg)
281 (or documentation
282 (concat "Insert a " name "."))
283 (list 'interactive "*P")
284 (list 'tempo-insert-template (list 'quote
285 template-name)
286 (list 'if 'tempo-insert-region
287 (list 'not 'arg) 'arg))))
288 (and tag
289 (tempo-add-tag tag template-name taglist))
290 command-name))
293 ;;; tempo-insert-template
295 (defun tempo-insert-template (template on-region)
296 "Insert a template.
297 TEMPLATE is the template to be inserted. If ON-REGION is non-nil the
298 `r' elements are replaced with the current region. In Transient Mark
299 mode, ON-REGION is ignored and assumed true if the region is active."
300 (unwind-protect
301 (progn
302 (if (or (and (boundp 'transient-mark-mode) ; For Emacs
303 transient-mark-mode
304 mark-active)
305 (if (featurep 'xemacs)
306 (and zmacs-regions (mark))))
307 (setq on-region t))
308 (and on-region
309 (set-marker tempo-region-start (min (mark) (point)))
310 (set-marker tempo-region-stop (max (mark) (point))))
311 (if on-region
312 (goto-char tempo-region-start))
313 (save-excursion
314 (tempo-insert-mark (point-marker))
315 (mapcar (function (lambda (elt)
316 (tempo-insert elt on-region)))
317 (symbol-value template))
318 (tempo-insert-mark (point-marker)))
319 (tempo-forward-mark))
320 (tempo-forget-insertions)
321 ;; Should I check for zmacs here too???
322 (and (boundp 'transient-mark-mode)
323 transient-mark-mode
324 (deactivate-mark))))
327 ;;; tempo-insert
329 (defun tempo-insert (element on-region)
330 "Insert a template element.
331 Insert one element from a template. If ON-REGION is non-nil the `r'
332 elements are replaced with the current region.
334 See documentation for `tempo-define-template' for the kind of elements
335 possible."
336 (cond ((stringp element) (tempo-process-and-insert-string element))
337 ((and (consp element)
338 (eq (car element) 'p)) (tempo-insert-prompt-compat
339 (cdr element)))
340 ((and (consp element)
341 (eq (car element) 'P)) (let ((tempo-interactive t))
342 (tempo-insert-prompt-compat
343 (cdr element))))
344 ;;; ((and (consp element)
345 ;;; (eq (car element) 'v)) (tempo-save-named
346 ;;; (nth 1 element)
347 ;;; nil
348 ;;; (nth 2 element)))
349 ((and (consp element)
350 (eq (car element) 'r)) (if on-region
351 (goto-char tempo-region-stop)
352 (tempo-insert-prompt-compat
353 (cdr element))))
354 ((and (consp element)
355 (eq (car element) 's)) (tempo-insert-named (car (cdr element))))
356 ((and (consp element)
357 (eq (car element) 'l)) (mapcar (function
358 (lambda (elt)
359 (tempo-insert elt on-region)))
360 (cdr element)))
361 ((eq element 'p) (tempo-insert-mark (point-marker)))
362 ((eq element 'r) (if on-region
363 (goto-char tempo-region-stop)
364 (tempo-insert-mark (point-marker))))
365 ((eq element 'r>) (if on-region
366 (progn
367 (goto-char tempo-region-stop)
368 (indent-region (mark) (point) nil))
369 (tempo-insert-mark (point-marker))))
370 ((eq element '>) (indent-according-to-mode))
371 ((eq element '&) (if (not (or (= (current-column) 0)
372 (save-excursion
373 (re-search-backward
374 "^\\s-*\\=" nil t))))
375 (insert "\n")))
376 ((eq element '%) (if (not (or (eolp)
377 (save-excursion
378 (re-search-forward
379 "\\=\\s-*$" nil t))))
380 (insert "\n")))
381 ((eq element 'n) (insert "\n"))
382 ((eq element 'n>) (insert "\n") (indent-according-to-mode))
383 ;; Bug: If the 'o is the first element in a template, strange
384 ;; things can happen when the template is inserted at the
385 ;; beginning of a line.
386 ((eq element 'o) (if (not (or on-region
387 (eolp)
388 (save-excursion
389 (re-search-forward
390 "\\=\\s-*$" nil t))))
391 (open-line 1)))
392 ((null element))
393 (t (tempo-insert (or (tempo-is-user-element element)
394 (eval element))
395 on-region))))
398 ;;; tempo-insert-prompt
400 (defun tempo-insert-prompt-compat (prompt)
401 "Compatibility hack for `tempo-insert-prompt'.
402 PROMPT can be either a prompt string, or a list of arguments to
403 `tempo-insert-prompt', or nil."
404 (if (consp prompt) ; not nil either
405 (apply 'tempo-insert-prompt prompt)
406 (tempo-insert-prompt prompt)))
408 (defun tempo-insert-prompt (prompt &optional save-name no-insert)
409 "Prompt for a text string and insert it in the current buffer.
410 If the variable `tempo-interactive' is non-nil the user is prompted
411 for a string in the minibuffer, which is then inserted in the current
412 buffer. If `tempo-interactive' is nil, the current point is placed on
413 `tempo-mark'.
415 PROMPT is the prompt string, SAVE-NAME is a name to save the inserted
416 text under. If the optional argument NO-INSERT is non-nil, no text is
417 inserted. This can be useful when there is a SAVE-NAME.
419 If there already is a value for SAVE-NAME, it is used and the user is
420 never prompted."
421 (let (insertion
422 (previous (and save-name
423 (tempo-lookup-named save-name))))
424 (cond
425 ;; Insert previous value, unless no-insert is non-nil
426 ((and previous
427 (not no-insert))
428 (tempo-insert-named save-name)) ; A double lookup here, but who
429 ; cares
430 ;; If no-insert is non-nil, don't insert the previous value. Just
431 ;; keep it
432 (previous
433 nil)
434 ;; No previous value. Prompt or insert mark
435 (tempo-interactive
436 (if (not (stringp prompt))
437 (error "tempo: The prompt (%s) is not a string" prompt))
438 (setq insertion (read-string prompt))
439 (or no-insert
440 (insert insertion))
441 (if save-name
442 (tempo-save-named save-name insertion)))
444 (tempo-insert-mark (point-marker))))))
447 ;;; tempo-is-user-element
449 (defun tempo-is-user-element (element)
450 "Tries all the user-defined element handlers in `tempo-user-elements'."
451 ;; Sigh... I need (some list)
452 (catch 'found
453 (mapcar (function (lambda (handler)
454 (let ((result (funcall handler element)))
455 (if result (throw 'found result)))))
456 tempo-user-elements)
457 (throw 'found nil)))
460 ;;; tempo-forget-insertions
462 (defun tempo-forget-insertions ()
463 "Forget all the saved named insertions."
464 (setq tempo-named-insertions nil))
467 ;;; tempo-save-named
469 (defun tempo-save-named (name data) ; Had an optional prompt for 'v
470 "Save some data for later insertion
471 The contents of DATA is saved under the name NAME.
473 The data can later be retrieved with `tempo-lookup-named'.
475 This function returns nil, so it can be used in a template without
476 inserting anything."
477 (setq tempo-named-insertions
478 (cons (cons name data)
479 tempo-named-insertions))
480 nil)
483 ;;; tempo-lookup-named
485 (defun tempo-lookup-named (name)
486 "Lookup some saved data under the name NAME.
487 Returns the data if NAME was found, and nil otherwise."
488 (cdr (assq name tempo-named-insertions)))
491 ;;; tempo-insert-named
493 (defun tempo-insert-named (name)
494 "Insert the previous insertion saved under a named specified in NAME.
495 If there is no such name saved, a tempo mark is inserted.
497 Note that if the data is a string, it will not be run through the string
498 processor."
499 (let* ((insertion (tempo-lookup-named name)))
500 (cond ((null insertion)
501 (tempo-insert-mark (point-marker)))
502 ((stringp insertion)
503 (insert insertion))
505 (tempo-insert insertion nil)))))
509 ;;; tempo-process-and-insert-string
511 (defun tempo-process-and-insert-string (string)
512 "Insert a string from a template.
513 Run a string through the preprocessors in `tempo-insert-string-functions'
514 and insert the results."
515 (cond ((null tempo-insert-string-functions)
516 nil)
517 ((symbolp tempo-insert-string-functions)
518 (setq string
519 (funcall tempo-insert-string-functions string)))
520 ((listp tempo-insert-string-functions)
521 (dolist (fn tempo-insert-string-functions)
522 (setq string (funcall fn string))))
524 (error "Bogus value in tempo-insert-string-functions: %s"
525 tempo-insert-string-functions)))
526 (insert string))
529 ;;; tempo-insert-mark
531 (defun tempo-insert-mark (mark)
532 "Insert a mark `tempo-marks' while keeping it sorted."
533 (cond ((null tempo-marks) (setq tempo-marks (list mark)))
534 ((< mark (car tempo-marks)) (setq tempo-marks (cons mark tempo-marks)))
535 (t (let ((lp tempo-marks))
536 (while (and (cdr lp)
537 (<= (car (cdr lp)) mark))
538 (setq lp (cdr lp)))
539 (if (not (= mark (car lp)))
540 (setcdr lp (cons mark (cdr lp))))))))
543 ;;; tempo-forward-mark
545 (defun tempo-forward-mark ()
546 "Jump to the next mark in `tempo-forward-mark-list'."
547 (interactive)
548 (let ((next-mark (catch 'found
549 (mapcar
550 (function
551 (lambda (mark)
552 (if (< (point) mark)
553 (throw 'found mark))))
554 tempo-marks)
555 ;; return nil if not found
556 nil)))
557 (if next-mark
558 (goto-char next-mark))))
561 ;;; tempo-backward-mark
563 (defun tempo-backward-mark ()
564 "Jump to the previous mark in `tempo-back-mark-list'."
565 (interactive)
566 (let ((prev-mark (catch 'found
567 (let (last)
568 (mapcar
569 (function
570 (lambda (mark)
571 (if (<= (point) mark)
572 (throw 'found last))
573 (setq last mark)))
574 tempo-marks)
575 last))))
576 (if prev-mark
577 (goto-char prev-mark))))
580 ;;; tempo-add-tag
582 (defun tempo-add-tag (tag template &optional tag-list)
583 "Add a template tag.
584 Add the TAG, that should complete to TEMPLATE to the list in TAG-LIST,
585 or to `tempo-tags' if TAG-LIST is nil."
587 (interactive "sTag: \nCTemplate: ")
588 (if (null tag-list)
589 (setq tag-list 'tempo-tags))
590 (if (not (assoc tag (symbol-value tag-list)))
591 (set tag-list (cons (cons tag template) (symbol-value tag-list))))
592 (tempo-invalidate-collection))
595 ;;; tempo-use-tag-list
597 (defun tempo-use-tag-list (tag-list &optional completion-function)
598 "Install TAG-LIST to be used for template completion in the current buffer.
599 TAG-LIST is a symbol whose variable value is a tag list created with
600 `tempo-add-tag'.
602 COMPLETION-FUNCTION is an obsolete option for specifying an optional
603 function or string that is used by `\\[tempo-complete-tag]' to find a
604 string to match the tag against. It has the same definition as the
605 variable `tempo-match-finder'. In this version, supplying a
606 COMPLETION-FUNCTION just sets `tempo-match-finder' locally."
607 (let ((old (assq tag-list tempo-local-tags)))
608 (if old
609 (setcdr old completion-function)
610 (setq tempo-local-tags (cons (cons tag-list completion-function)
611 tempo-local-tags))))
612 (if completion-function
613 (setq tempo-match-finder completion-function))
614 (tempo-invalidate-collection))
617 ;;; tempo-invalidate-collection
619 (defun tempo-invalidate-collection ()
620 "Marks the tag collection as obsolete.
621 Whenever it is needed again it will be rebuilt."
622 (setq tempo-dirty-collection t))
625 ;;; tempo-build-collection
627 (defun tempo-build-collection ()
628 "Build a collection of all the tags and return it.
629 If `tempo-dirty-collection' is nil, the old collection is reused."
630 (prog1
631 (or (and (not tempo-dirty-collection)
632 tempo-collection)
633 (setq tempo-collection
634 (apply (function append)
635 (mapcar (function (lambda (tag-list)
636 ; If the format for
637 ; tempo-local-tags changes,
638 ; change this
639 (eval (car tag-list))))
640 tempo-local-tags))))
641 (setq tempo-dirty-collection nil)))
644 ;;; tempo-find-match-string
646 (defun tempo-find-match-string (finder)
647 "Find a string to be matched against a tag list.
648 FINDER is a function or a string. Returns (STRING . POS), or nil
649 if no reasonable string is found."
650 (cond ((stringp finder)
651 (let (successful)
652 (save-excursion
653 (or (setq successful (re-search-backward finder nil t))
655 (if successful
656 (cons (buffer-substring (match-beginning 1)
657 (match-end 1)) ; This seems to be a
658 ; bug in emacs
659 (match-beginning 1))
660 nil)))
662 (funcall finder))))
665 ;;; tempo-complete-tag
667 (defun tempo-complete-tag (&optional silent)
668 "Look for a tag and expand it.
669 All the tags in the tag lists in `tempo-local-tags'
670 \(this includes `tempo-tags') are searched for a match for the text
671 before the point. The way the string to match for is determined can
672 be altered with the variable `tempo-match-finder'. If
673 `tempo-match-finder' returns nil, then the results are the same as
674 no match at all.
676 If a single match is found, the corresponding template is expanded in
677 place of the matching string.
679 If a partial completion or no match at all is found, and SILENT is
680 non-nil, the function will give a signal.
682 If a partial completion is found and `tempo-show-completion-buffer' is
683 non-nil, a buffer containing possible completions is displayed."
685 ;; This function may look like a hack, but this is how I want it to
686 ;; work.
687 (interactive "*")
688 (let* ((collection (tempo-build-collection))
689 (match-info (tempo-find-match-string tempo-match-finder))
690 (match-string (car match-info))
691 (match-start (cdr match-info))
692 (exact (assoc match-string collection))
693 (compl (or (car exact)
694 (and match-info (try-completion match-string collection)))))
695 (if compl (delete-region match-start (point)))
696 (cond ((null match-info) (or silent (ding)))
697 ((null compl) (or silent (ding)))
698 ((eq compl t) (tempo-insert-template
699 (cdr (assoc match-string
700 collection))
701 nil))
702 (t (if (setq exact (assoc compl collection))
703 (tempo-insert-template (cdr exact) nil)
704 (insert compl)
705 (or silent (ding))
706 (if tempo-show-completion-buffer
707 (tempo-display-completions match-string
708 collection)))))))
712 ;;; tempo-display-completions
714 (defun tempo-display-completions (string tag-list)
715 "Show a buffer containing possible completions for STRING."
716 (if tempo-leave-completion-buffer
717 (with-output-to-temp-buffer "*Completions*"
718 (display-completion-list
719 (all-completions string tag-list)))
720 (save-window-excursion
721 (with-output-to-temp-buffer "*Completions*"
722 (display-completion-list
723 (all-completions string tag-list)))
724 (sit-for 32767))))
727 ;;; tempo-expand-if-complete
729 (defun tempo-expand-if-complete ()
730 "Expand the tag before point if it is complete.
731 Returns non-nil if an expansion was made and nil otherwise.
733 This could as an example be used in a command that is bound to the
734 space bar, and looks something like this:
736 \(defun tempo-space ()
737 (interactive \"*\")
738 (or (tempo-expand-if-complete)
739 (insert \" \")))"
741 (interactive "*")
742 (let* ((collection (tempo-build-collection))
743 (match-info (tempo-find-match-string tempo-match-finder))
744 (match-string (car match-info))
745 (match-start (cdr match-info))
746 (exact (assoc match-string collection)))
747 (if exact
748 (progn
749 (delete-region match-start (point))
750 (tempo-insert-template (cdr exact) nil)
752 nil)))
754 (provide 'tempo)
756 ;;; arch-tag: b3c0ee36-db3b-47bc-875f-091b4e27a063
757 ;;; tempo.el ends here