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