(longlines-mode): Add defvar.
[bpt/emacs.git] / lisp / tempo.el
CommitLineData
785c4478 1;;; tempo.el --- Flexible template insertion
b578f267 2
803a05c2 3;; Copyright (C) 1994, 1995, 2004 Free Software Foundation, Inc.
813f532d 4
4924ca44 5;; Author: David K}gedal <davidk@lysator.liu.se>
813f532d 6;; Created: 16 Feb 1994
2e792253 7;; K}gedal's last version number: 1.2.4
813f532d
RS
8;; Keywords: extensions, languages, tools
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
b578f267 23;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
24;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25;; Boston, MA 02110-1301, USA.
813f532d
RS
26
27;;; Commentary:
28
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
34da6b16
RS
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.
813f532d
RS
36
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.
41
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.
49
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).
53
54;; More flexible templates can be created by including lisp symbols,
a47ecf6c 55;; which will be evaluated as variables, or lists, which will be
813f532d
RS
56;; evaluated as lisp expressions.
57
58;; See the documentation for tempo-define-template for the different
59;; items that can be used to define a tempo template.
60
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.
70
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.
75
813f532d
RS
76;; The latest tempo.el distribution can be fetched from
77;; ftp.lysator.liu.se in the directory /pub/emacs
78
34059250
RS
79;; There is also a WWW page at
80;; http://www.lysator.liu.se/~davidk/elisp/ which has some information
81
785c4478
RS
82;;; Known bugs:
83
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.
88
89;; The 'o tag is also a problem when including the region. This will
90;; be looked into.
91
92;; Clicking mouse-2 in the completion buffer gives strange results.
93
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.
97
34da6b16
RS
98;;; Contributors:
99
92373b84 100;; These people have given me important feedback and new ideas for
34da6b16
RS
101;; tempo.el. Thanks.
102
103;; Nelson Minar <nelson@santafe.edu>
5762abec 104;; Richard Stallman <rms@gnu.org>
34da6b16
RS
105;; Lars Lindberg <Lars.Lindberg@sypro.cap.se>
106;; Glen Whitney <Glen.Whitney@math.lsa.umich.edu>
107
813f532d
RS
108;;; Code:
109
785c4478 110;;; User options
813f532d 111
4bef9110
SE
112(defgroup tempo nil
113 "Flexible template insertion."
114 :prefix "tempo-"
115 :group 'tools)
116
117(defcustom tempo-interactive nil
813f532d
RS
118 "*Prompt user for strings in templates.
119If this variable is non-nil, `tempo-insert' prompts the
4bef9110
SE
120user for text to insert in the templates"
121 :type 'boolean
122 :group 'tempo)
813f532d 123
4bef9110 124(defcustom tempo-insert-region nil
70b5dc25 125 "*Automatically insert current region when there is a `r' in the template
0ff9b955 126If this variable is nil, `r' elements will be treated just like `p'
70b5dc25 127elements, unless the template function is given a prefix (or a non-nil
5f94d2d0 128argument). If this variable is non-nil, the behavior is reversed.
785c4478 129
4bef9110
SE
130In Transient Mark mode, this option is unused."
131 :type 'boolean
132 :group 'tempo)
70b5dc25 133
4bef9110 134(defcustom tempo-show-completion-buffer t
0ff9b955 135 "*If non-nil, show a buffer with possible completions, when only
4bef9110
SE
136a partial completion can be found"
137 :type 'boolean
138 :group 'tempo)
70b5dc25 139
4bef9110 140(defcustom tempo-leave-completion-buffer nil
0ff9b955 141 "*If nil, a completion buffer generated by \\[tempo-complete-tag]
4bef9110
SE
142disappears at the next keypress; otherwise, it remains forever."
143 :type 'boolean
144 :group 'tempo)
70b5dc25 145
785c4478
RS
146;;; Internal variables
147
813f532d
RS
148(defvar tempo-insert-string-functions nil
149 "List of functions to run when inserting a string.
34059250
RS
150Each function is called with a single arg, STRING and should return
151another string. This could be used for making all strings upcase by
152setting it to '(upcase), for example.")
813f532d
RS
153
154(defvar tempo-tags nil
155 "An association list with tags and corresponding templates")
156
157(defvar tempo-local-tags '((tempo-tags . nil))
158 "A list of locally installed tag completion lists.
813f532d 159It is a association list where the car of every element is a symbol
92373b84 160whose variable value is a template list. The cdr part, if non-nil, is a
813f532d
RS
161function or a regexp that defines the string to match. See the
162documentation for the function `tempo-complete-tag' for more info.
163
164`tempo-tags' is always in the last position in this list.")
165
785c4478
RS
166(defvar tempo-collection nil
167 "A collection of all the tags defined for the current buffer.")
168
169(defvar tempo-dirty-collection t
170 "Indicates if the tag collection needs to be rebuilt.")
171
813f532d
RS
172(defvar tempo-marks nil
173 "A list of marks to jump to with `\\[tempo-forward-mark]' and `\\[tempo-backward-mark]'.")
174
16d24ae8 175(defvar tempo-match-finder "\\b\\([[:word:]]+\\)\\="
785c4478
RS
176 "The regexp or function used to find the string to match against tags.
177
178If `tempo-match-finder is a string, it should contain a regular
179expression with at least one \\( \\) pair. When searching for tags,
180`tempo-complete-tag' calls `re-search-backward' with this string, and
181the string between the first \\( and \\) is used for matching against
182each string in the tag list. If one is found, the whole text between
183the first \\( and the point is replaced with the inserted template.
184
803a05c2 185You will probably want to include \\=\\= at the end of the regexp to
785c4478
RS
186make sure that the string is matched only against text adjacent to the
187point.
188
189If `tempo-match-finder' is a symbol, it should be a function that
190returns a pair of the form (STRING . POS), where STRING is the string
191used for matching and POS is the buffer position after which text
192should be replaced with a template.")
193
194(defvar tempo-user-elements nil
195 "Element handlers for user-defined elements.
196A list of symbols which are bound to functions that take one argument.
92373b84 197This function should return something to be sent to `tempo-insert' if
0ff9b955 198it recognizes the argument, and nil otherwise.")
813f532d 199
70b5dc25 200(defvar tempo-named-insertions nil
0ff9b955 201 "Temporary storage for named insertions.")
70b5dc25 202
785c4478 203(defvar tempo-region-start (make-marker)
0ff9b955 204 "Region start when inserting around the region.")
785c4478
RS
205
206(defvar tempo-region-stop (make-marker)
0ff9b955 207 "Region stop when inserting around the region.")
785c4478 208
813f532d
RS
209;; Make some variables local to every buffer
210
211(make-variable-buffer-local 'tempo-marks)
212(make-variable-buffer-local 'tempo-local-tags)
785c4478
RS
213(make-variable-buffer-local 'tempo-match-finder)
214(make-variable-buffer-local 'tempo-collection)
215(make-variable-buffer-local 'tempo-dirty-collection)
813f532d
RS
216
217;;; Functions
218
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.
34059250 242 - (p PROMPT <NAME> <NOINSERT>) If `tempo-interactive' is non-nil, the
ff2ed6c7 243 user is prompted in the minibuffer with PROMPT for a string to be
34059250
RS
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, 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.
70b5dc25
RS
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.
813f532d
RS
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 that
265 you often should place this item after the text you want on the
266 line.
34059250 267 - 'r> Like r, but it also indents the region.
785c4478
RS
268 - 'n> Inserts a newline and indents line.
269 - 'o Like '% but leaves the point before the newline.
813f532d 270 - nil. It is ignored.
34059250
RS
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."
813f532d
RS
276
277 (let* ((template-name (intern (concat "tempo-template-"
278 name)))
279 (command-name template-name))
280 (set template-name elements)
281 (fset command-name (list 'lambda (list '&optional 'arg)
f1180544 282 (or documentation
813f532d
RS
283 (concat "Insert a " name "."))
284 (list 'interactive "*P")
285 (list 'tempo-insert-template (list 'quote
70b5dc25
RS
286 template-name)
287 (list 'if 'tempo-insert-region
288 (list 'not 'arg) 'arg))))
813f532d
RS
289 (and tag
290 (tempo-add-tag tag template-name taglist))
291 command-name))
292
293;;;
294;;; tempo-insert-template
295
296(defun tempo-insert-template (template on-region)
297 "Insert a template.
298TEMPLATE is the template to be inserted. If ON-REGION is non-nil the
785c4478
RS
299`r' elements are replaced with the current region. In Transient Mark
300mode, ON-REGION is ignored and assumed true if the region is active."
34059250
RS
301 (unwind-protect
302 (progn
2e792253 303 (if (or (and (boundp 'transient-mark-mode) ; For Emacs
34059250
RS
304 transient-mark-mode
305 mark-active)
2e792253 306 (and (boundp 'zmacs-regions) ; For XEmacs
34059250
RS
307 zmacs-regions (mark)))
308 (setq on-region t))
309 (and on-region
310 (set-marker tempo-region-start (min (mark) (point)))
311 (set-marker tempo-region-stop (max (mark) (point))))
312 (if on-region
313 (goto-char tempo-region-start))
314 (save-excursion
315 (tempo-insert-mark (point-marker))
316 (mapcar (function (lambda (elt)
317 (tempo-insert elt on-region)))
318 (symbol-value template))
319 (tempo-insert-mark (point-marker)))
320 (tempo-forward-mark))
321 (tempo-forget-insertions)
322 ;; Should I check for zmacs here too???
323 (and (boundp 'transient-mark-mode)
324 transient-mark-mode
325 (deactivate-mark))))
813f532d
RS
326
327;;;
328;;; tempo-insert
329
785c4478 330(defun tempo-insert (element on-region)
813f532d 331 "Insert a template element.
785c4478
RS
332Insert one element from a template. If ON-REGION is non-nil the `r'
333elements are replaced with the current region.
334
335See documentation for `tempo-define-template' for the kind of elements
336possible."
813f532d 337 (cond ((stringp element) (tempo-process-and-insert-string element))
34059250
RS
338 ((and (consp element)
339 (eq (car element) 'p)) (tempo-insert-prompt-compat
340 (cdr element)))
341 ((and (consp element)
342 (eq (car element) 'P)) (let ((tempo-interactive t))
343 (tempo-insert-prompt-compat
344 (cdr element))))
345;;; ((and (consp element)
346;;; (eq (car element) 'v)) (tempo-save-named
347;;; (nth 1 element)
348;;; nil
349;;; (nth 2 element)))
350 ((and (consp element)
351 (eq (car element) 'r)) (if on-region
352 (goto-char tempo-region-stop)
353 (tempo-insert-prompt-compat
354 (cdr element))))
355 ((and (consp element)
356 (eq (car element) 's)) (tempo-insert-named (car (cdr element))))
357 ((and (consp element)
358 (eq (car element) 'l)) (mapcar (function
359 (lambda (elt)
360 (tempo-insert elt on-region)))
361 (cdr element)))
813f532d
RS
362 ((eq element 'p) (tempo-insert-mark (point-marker)))
363 ((eq element 'r) (if on-region
785c4478 364 (goto-char tempo-region-stop)
813f532d 365 (tempo-insert-mark (point-marker))))
785c4478
RS
366 ((eq element 'r>) (if on-region
367 (progn
368 (goto-char tempo-region-stop)
369 (indent-region (mark) (point) nil))
370 (tempo-insert-mark (point-marker))))
813f532d
RS
371 ((eq element '>) (indent-according-to-mode))
372 ((eq element '&) (if (not (or (= (current-column) 0)
373 (save-excursion
374 (re-search-backward
375 "^\\s-*\\=" nil t))))
376 (insert "\n")))
377 ((eq element '%) (if (not (or (eolp)
378 (save-excursion
379 (re-search-forward
380 "\\=\\s-*$" nil t))))
381 (insert "\n")))
382 ((eq element 'n) (insert "\n"))
383 ((eq element 'n>) (insert "\n") (indent-according-to-mode))
785c4478
RS
384 ;; Bug: If the 'o is the first element in a template, strange
385 ;; things can happen when the template is inserted at the
386 ;; beginning of a line.
387 ((eq element 'o) (if (not (or on-region
388 (eolp)
389 (save-excursion
390 (re-search-forward
391 "\\=\\s-*$" nil t))))
392 (open-line 1)))
813f532d 393 ((null element))
785c4478
RS
394 (t (tempo-insert (or (tempo-is-user-element element)
395 (eval element))
396 on-region))))
813f532d
RS
397
398;;;
399;;; tempo-insert-prompt
400
34059250
RS
401(defun tempo-insert-prompt-compat (prompt)
402 "Compatibility hack for tempo-insert-prompt.
403PROMPT can be either a prompt string, or a list of arguments to
404tempo-insert-prompt, or nil."
0ff9b955 405 (if (consp prompt) ; not nil either
34059250
RS
406 (apply 'tempo-insert-prompt prompt)
407 (tempo-insert-prompt prompt)))
408
409(defun tempo-insert-prompt (prompt &optional save-name no-insert)
813f532d
RS
410 "Prompt for a text string and insert it in the current buffer.
411If the variable `tempo-interactive' is non-nil the user is prompted
412for a string in the minibuffer, which is then inserted in the current
413buffer. If `tempo-interactive' is nil, the current point is placed on
70b5dc25 414`tempo-mark'.
813f532d 415
34059250
RS
416PROMPT is the prompt string, SAVE-NAME is a name to save the inserted
417text under. If the optional argument NO-INSERT is non-nil, no text i
418inserted. This can be useful when there is a SAVE-NAME.
419
420If there already is a value for SAVE-NAME, it is used and the user is
421never prompted."
422 (let (insertion
423 (previous (and save-name
424 (tempo-lookup-named save-name))))
425 (cond
426 ;; Insert previous value, unless no-insert is non-nil
427 ((and previous
428 (not no-insert))
429 (tempo-insert-named save-name)) ; A double lookup here, but who
430 ; cares
431 ;; If no-insert is non-nil, don't insert the previous value. Just
432 ;; keep it
433 (previous
434 nil)
435 ;; No previous value. Prompt or insert mark
436 (tempo-interactive
437 (if (not (stringp prompt))
438 (error "tempo: The prompt (%s) is not a string" prompt))
439 (setq insertion (read-string prompt))
440 (or no-insert
441 (insert insertion))
442 (if save-name
443 (tempo-save-named save-name insertion)))
444 (t
445 (tempo-insert-mark (point-marker))))))
813f532d 446
785c4478
RS
447;;;
448;;; tempo-is-user-element
449
450(defun tempo-is-user-element (element)
451 "Tries all the user-defined element handlers in
452`tempo-user-elements'"
453 ;; Sigh... I need (some list)
454 (catch 'found
455 (mapcar (function (lambda (handler)
456 (let ((result (funcall handler element)))
457 (if result (throw 'found result)))))
458 tempo-user-elements)
459 (throw 'found nil)))
460
70b5dc25
RS
461;;;
462;;; tempo-forget-insertions
463
464(defun tempo-forget-insertions ()
465 "Forget all the saved named insertions."
466 (setq tempo-named-insertions nil))
467
34059250
RS
468;;;
469;;; tempo-save-named
470
471(defun tempo-save-named (name data) ; Had an optional prompt for 'v
472 "Save some data for later insertion
473The contents of DATA is saved under the name NAME.
474
475The data can later be retrieved with `tempo-lookup-named'.
476
477This function returns nil, so it can be used in a template without
478inserting anything."
479 (setq tempo-named-insertions
480 (cons (cons name data)
481 tempo-named-insertions))
482 nil)
483
484;;;
485;;; tempo-lookup-named
486
487(defun tempo-lookup-named (name)
488 "Lookup some saved data under the name NAME.
489Returns the data if NAME was found, and nil otherwise."
490 (cdr (assq name tempo-named-insertions)))
491
70b5dc25
RS
492;;;
493;;; tempo-insert-named
494
34da6b16
RS
495(defun tempo-insert-named (name)
496 "Insert the previous insertion saved under a named specified in NAME.
34059250
RS
497If there is no such name saved, a tempo mark is inserted.
498
499Note that if the data is a string, it will not be run through the string
500processor."
501 (let* ((insertion (tempo-lookup-named name)))
502 (cond ((null insertion)
503 (tempo-insert-mark (point-marker)))
504 ((stringp insertion)
505 (insert insertion))
506 (t
507 (tempo-insert insertion nil)))))
508
70b5dc25 509
813f532d
RS
510;;;
511;;; tempo-process-and-insert-string
512
513(defun tempo-process-and-insert-string (string)
514 "Insert a string from a template.
515Run a string through the preprocessors in `tempo-insert-string-functions'
516and insert the results."
813f532d
RS
517 (cond ((null tempo-insert-string-functions)
518 nil)
519 ((symbolp tempo-insert-string-functions)
520 (setq string
34da6b16 521 (funcall tempo-insert-string-functions string)))
813f532d 522 ((listp tempo-insert-string-functions)
2b706b50 523 (dolist (fn tempo-insert-string-functions)
34da6b16 524 (setq string (funcall fn string))))
813f532d
RS
525 (t
526 (error "Bogus value in tempo-insert-string-functions: %s"
527 tempo-insert-string-functions)))
528 (insert string))
529
530;;;
531;;; tempo-insert-mark
532
533(defun tempo-insert-mark (mark)
534 "Insert a mark `tempo-marks' while keeping it sorted"
535 (cond ((null tempo-marks) (setq tempo-marks (list mark)))
536 ((< mark (car tempo-marks)) (setq tempo-marks (cons mark tempo-marks)))
537 (t (let ((lp tempo-marks))
538 (while (and (cdr lp)
539 (<= (car (cdr lp)) mark))
540 (setq lp (cdr lp)))
541 (if (not (= mark (car lp)))
542 (setcdr lp (cons mark (cdr lp))))))))
f1180544 543
813f532d
RS
544;;;
545;;; tempo-forward-mark
546
547(defun tempo-forward-mark ()
548 "Jump to the next mark in `tempo-forward-mark-list'."
549 (interactive)
550 (let ((next-mark (catch 'found
551 (mapcar
552 (function
553 (lambda (mark)
554 (if (< (point) mark)
555 (throw 'found mark))))
556 tempo-marks)
557 ;; return nil if not found
558 nil)))
559 (if next-mark
560 (goto-char next-mark))))
561
562;;;
563;;; tempo-backward-mark
564
565(defun tempo-backward-mark ()
566 "Jump to the previous mark in `tempo-back-mark-list'."
567 (interactive)
568 (let ((prev-mark (catch 'found
569 (let (last)
570 (mapcar
571 (function
572 (lambda (mark)
573 (if (<= (point) mark)
574 (throw 'found last))
575 (setq last mark)))
576 tempo-marks)
577 last))))
578 (if prev-mark
579 (goto-char prev-mark))))
f1180544 580
813f532d
RS
581;;;
582;;; tempo-add-tag
583
584(defun tempo-add-tag (tag template &optional tag-list)
585 "Add a template tag.
813f532d
RS
586Add the TAG, that should complete to TEMPLATE to the list in TAG-LIST,
587or to `tempo-tags' if TAG-LIST is nil."
588
589 (interactive "sTag: \nCTemplate: ")
590 (if (null tag-list)
591 (setq tag-list 'tempo-tags))
592 (if (not (assoc tag (symbol-value tag-list)))
785c4478
RS
593 (set tag-list (cons (cons tag template) (symbol-value tag-list))))
594 (tempo-invalidate-collection))
813f532d
RS
595
596;;;
597;;; tempo-use-tag-list
598
599(defun tempo-use-tag-list (tag-list &optional completion-function)
600 "Install TAG-LIST to be used for template completion in the current buffer.
813f532d 601TAG-LIST is a symbol whose variable value is a tag list created with
785c4478 602`tempo-add-tag'.
813f532d 603
92373b84 604COMPLETION-FUNCTION is an obsolete option for specifying an optional
785c4478
RS
605function or string that is used by `\\[tempo-complete-tag]' to find a
606string to match the tag against. It has the same definition as the
607variable `tempo-match-finder'. In this version, supplying a
608COMPLETION-FUNCTION just sets `tempo-match-finder' locally."
813f532d
RS
609 (let ((old (assq tag-list tempo-local-tags)))
610 (if old
611 (setcdr old completion-function)
612 (setq tempo-local-tags (cons (cons tag-list completion-function)
785c4478
RS
613 tempo-local-tags))))
614 (if completion-function
615 (setq tempo-match-finder completion-function))
616 (tempo-invalidate-collection))
617
618;;;
619;;; tempo-invalidate-collection
620
621(defun tempo-invalidate-collection ()
622 "Marks the tag collection as obsolete.
623Whenever it is needed again it will be rebuilt."
624 (setq tempo-dirty-collection t))
625
626;;;
627;;; tempo-build-collection
628
629(defun tempo-build-collection ()
630 "Build a collection of all the tags and return it.
0ff9b955 631If `tempo-dirty-collection' is nil, the old collection is reused."
35622e5f
KH
632 (prog1
633 (or (and (not tempo-dirty-collection)
634 tempo-collection)
635 (setq tempo-collection
636 (apply (function append)
637 (mapcar (function (lambda (tag-list)
785c4478
RS
638 ; If the format for
639 ; tempo-local-tags changes,
640 ; change this
35622e5f
KH
641 (eval (car tag-list))))
642 tempo-local-tags))))
643 (setq tempo-dirty-collection nil)))
813f532d
RS
644
645;;;
646;;; tempo-find-match-string
647
648(defun tempo-find-match-string (finder)
649 "Find a string to be matched against a tag list.
34da6b16
RS
650FINDER is a function or a string. Returns (STRING . POS), or nil
651if no reasonable string is found."
813f532d 652 (cond ((stringp finder)
34da6b16
RS
653 (let (successful)
654 (save-excursion
655 (or (setq successful (re-search-backward finder nil t))
656 0))
657 (if successful
658 (cons (buffer-substring (match-beginning 1)
659 (match-end 1)) ; This seems to be a
660 ; bug in emacs
661 (match-beginning 1))
662 nil)))
813f532d
RS
663 (t
664 (funcall finder))))
665
666;;;
667;;; tempo-complete-tag
668
669(defun tempo-complete-tag (&optional silent)
70b5dc25 670 "Look for a tag and expand it.
34da6b16 671All the tags in the tag lists in `tempo-local-tags'
35622e5f 672\(this includes `tempo-tags') are searched for a match for the text
34da6b16
RS
673before the point. The way the string to match for is determined can
674be altered with the variable `tempo-match-finder'. If
675`tempo-match-finder' returns nil, then the results are the same as
676no match at all.
785c4478
RS
677
678If a single match is found, the corresponding template is expanded in
679place of the matching string.
680
681If a partial completion or no match at all is found, and SILENT is
0ff9b955 682non-nil, the function will give a signal.
785c4478
RS
683
684If a partial completion is found and `tempo-show-completion-buffer' is
0ff9b955 685non-nil, a buffer containing possible completions is displayed."
785c4478
RS
686
687 ;; This function may look like a hack, but this is how I want it to
688 ;; work.
689 (interactive "*")
690 (let* ((collection (tempo-build-collection))
691 (match-info (tempo-find-match-string tempo-match-finder))
692 (match-string (car match-info))
693 (match-start (cdr match-info))
694 (exact (assoc match-string collection))
695 (compl (or (car exact)
34da6b16 696 (and match-info (try-completion match-string collection)))))
785c4478 697 (if compl (delete-region match-start (point)))
34da6b16
RS
698 (cond ((null match-info) (or silent (ding)))
699 ((null compl) (or silent (ding)))
785c4478
RS
700 ((eq compl t) (tempo-insert-template
701 (cdr (assoc match-string
702 collection))
703 nil))
704 (t (if (setq exact (assoc compl collection))
705 (tempo-insert-template (cdr exact) nil)
706 (insert compl)
707 (or silent (ding))
708 (if tempo-show-completion-buffer
709 (tempo-display-completions match-string
710 collection)))))))
813f532d 711
813f532d 712
70b5dc25
RS
713;;;
714;;; tempo-display-completions
715
716(defun tempo-display-completions (string tag-list)
717 "Show a buffer containing possible completions for STRING."
718 (if tempo-leave-completion-buffer
719 (with-output-to-temp-buffer "*Completions*"
720 (display-completion-list
721 (all-completions string tag-list)))
722 (save-window-excursion
723 (with-output-to-temp-buffer "*Completions*"
724 (display-completion-list
725 (all-completions string tag-list)))
726 (sit-for 32767))))
727
34da6b16
RS
728;;;
729;;; tempo-expand-if-complete
730
731(defun tempo-expand-if-complete ()
732 "Expand the tag before point if it is complete.
733Returns non-nil if an expansion was made and nil otherwise.
734
735This could as an example be used in a command that is bound to the
736space bar, and looks something like this:
737
a47ecf6c 738\(defun tempo-space ()
34da6b16
RS
739 (interactive \"*\")
740 (or (tempo-expand-if-complete)
741 (insert \" \")))"
742
743 (interactive "*")
744 (let* ((collection (tempo-build-collection))
745 (match-info (tempo-find-match-string tempo-match-finder))
746 (match-string (car match-info))
747 (match-start (cdr match-info))
748 (exact (assoc match-string collection)))
749 (if exact
750 (progn
751 (delete-region match-start (point))
752 (tempo-insert-template (cdr exact) nil)
753 t)
754 nil)))
755
785c4478
RS
756(provide 'tempo)
757
ab5796a9 758;;; arch-tag: b3c0ee36-db3b-47bc-875f-091b4e27a063
813f532d 759;;; tempo.el ends here