1 ;;; tempo.el --- Flexible template insertion
2 ;; Copyright (C) 1994 Free Software Foundation, Inc.
4 ;; Author: David K}gedal <davidk@lysator.liu.se >
5 ;; Created: 16 Feb 1994
7 ;; Keywords: extensions, languages, tools
9 ;; This file is part of GNU Emacs.
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)
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.
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.
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
31 ;; a HTML editing mode written by Nelson Minar <nelson@reed.edu>, and
32 ;; his html-helper-mode.el is probably the best example of how to use
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.
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.
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).
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.
56 ;; See the documentation for tempo-define-template for the different
57 ;; items that can be used to define a tempo template.
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.
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.
74 ;; The latest tempo.el distribution can be fetched from
75 ;; ftp.lysator.liu.se in the directory /pub/emacs
79 ;; If the 'o is the first element in a template, strange things can
80 ;; happen when the template is inserted at the beginning of a
81 ;; line. This is due to strange behaviour in open-line. But it should
84 ;; The 'o tag is also a problem when including the region. This will
87 ;; Clicking mouse-2 in the completion buffer gives strange results.
89 ;; There is a bug in some emacs versions that prevents completion from
90 ;; working. If it doesn't work for you, send me a note indicating your
91 ;; emacs version and your problems.
99 (defvar tempo-interactive nil
100 "*Prompt user for strings in templates.
101 If this variable is non-nil, `tempo-insert' prompts the
102 user for text to insert in the templates")
104 (defvar tempo-insert-region nil
105 "*Automatically insert current region when there is a `r' in the template
106 If this variable is NIL, `r' elements will be treated just like `p'
107 elements, unless the template function is given a prefix (or a non-nil
108 argument). If this variable is non-NIL, the behaviour is reversed.
110 In Transient Mark mode, this option is unused.")
112 (defvar tempo-show-completion-buffer t
113 "*If non-NIL, show a buffer with possible completions, when only
114 a partial completion can be found")
116 (defvar tempo-leave-completion-buffer nil
117 "*If NIL, a completion buffer generated by \\[tempo-complete-tag]
118 disappears at the next keypress; otherwise, it remains forever.")
120 ;;; Internal variables
122 (defvar tempo-insert-string-functions nil
123 "List of functions to run when inserting a string.
124 Each function is called with a single arg, STRING." )
126 (defvar tempo-tags nil
127 "An association list with tags and corresponding templates")
129 (defvar tempo-local-tags
'((tempo-tags . nil
))
130 "A list of locally installed tag completion lists.
131 It is a association list where the car of every element is a symbol
132 whose varable value is a template list. The cdr part, if non-nil, is a
133 function or a regexp that defines the string to match. See the
134 documentation for the function `tempo-complete-tag' for more info.
136 `tempo-tags' is always in the last position in this list.")
138 (defvar tempo-collection nil
139 "A collection of all the tags defined for the current buffer.")
141 (defvar tempo-dirty-collection t
142 "Indicates if the tag collection needs to be rebuilt.")
144 (defvar tempo-marks nil
145 "A list of marks to jump to with `\\[tempo-forward-mark]' and `\\[tempo-backward-mark]'.")
147 (defvar tempo-match-finder
"\\b\\([^\\b]+\\)\\="
148 "The regexp or function used to find the string to match against tags.
150 If `tempo-match-finder is a string, it should contain a regular
151 expression with at least one \\( \\) pair. When searching for tags,
152 `tempo-complete-tag' calls `re-search-backward' with this string, and
153 the string between the first \\( and \\) is used for matching against
154 each string in the tag list. If one is found, the whole text between
155 the first \\( and the point is replaced with the inserted template.
157 You will probably want to include \\ \= at the end of the regexp to
158 make sure that the string is matched only against text adjacent to the
161 If `tempo-match-finder' is a symbol, it should be a function that
162 returns a pair of the form (STRING . POS), where STRING is the string
163 used for matching and POS is the buffer position after which text
164 should be replaced with a template.")
166 (defvar tempo-user-elements nil
167 "Element handlers for user-defined elements.
168 A list of symbols which are bound to functions that take one argument.
169 This function should return somthing to be sent to `tempo-insert' if
170 it recognizes the argument, and NIL otherwise")
172 (defvar tempo-named-insertions nil
173 "Temporary storage for named insertions")
175 (defvar tempo-region-start
(make-marker)
176 "Region start when inserting around the region")
178 (defvar tempo-region-stop
(make-marker)
179 "Region stop when inserting around the region")
181 ;; Make some variables local to every buffer
183 (make-variable-buffer-local 'tempo-marks
)
184 (make-variable-buffer-local 'tempo-local-tags
)
185 (make-variable-buffer-local 'tempo-match-finder
)
186 (make-variable-buffer-local 'tempo-collection
)
187 (make-variable-buffer-local 'tempo-dirty-collection
)
192 ;; tempo-define-template
194 (defun tempo-define-template (name elements
&optional tag documentation taglist
)
196 This function creates a template variable `tempo-template-NAME' and an
197 interactive function `tempo-template-NAME' that inserts the template
198 at the point. The created function is returned.
200 NAME is a string that contains the name of the template, ELEMENTS is a
201 list of elements in the template, TAG is the tag used for completion,
202 DOCUMENTATION is the documentation string for the insertion command
203 created, and TAGLIST (a symbol) is the tag list that TAG (if provided)
204 should be added to). If TAGLIST is nil and TAG is non-nil, TAG is
205 added to `tempo-tags'
207 The elements in ELEMENTS can be of several types:
209 - A string. It is sent to the hooks in `tempo-insert-string-functions',
210 and the result is inserted.
211 - The symbol 'p. This position is saved in `tempo-marks'.
212 - The symbol 'r. If `tempo-insert' is called with ON-REGION non-nil
213 the current region is placed here. Otherwise it works like 'p.
214 - (p PROMPT <NAME>) If `tempo-interactive' is non-nil, the user is
215 prompted in the minbuffer with PROMPT for a string to be inserted.
216 If the optional parameter NAME is non-nil, the text is saved for
217 later insertion with the `s' tag.
218 If `tempo-interactive' is nil, it works like 'p.
219 - (r PROMPT) like the previous, but if `tempo-interactive' is nil
220 and `tempo-insert' is called with ON-REGION non-nil, the current
221 region is placed here. This usually happens when you call the
222 template function with a prefix argument.
223 - (s NAME) Inserts text previously read with the (p ..) construct.
224 Finds the insertion saved under NAME and inserts it. Acts like 'p
225 if tempo-interactive is nil.
226 - '& If there is only whitespace between the line start and point,
227 nothing happens. Otherwise a newline is inserted.
228 - '% If there is only whitespace between point and end-of-line
229 nothing happens. Otherwise a newline is inserted.
230 - 'n inserts a newline.
231 - '> The line is indented using `indent-according-to-mode'. Note that
232 you often should place this item after the text you want on the
234 - 'n> Inserts a newline and indents line.
235 - 'o Like '% but leaves the point before the newline.
236 - nil. It is ignored.
237 - Anything else. It is evaluated and the result is parsed again."
239 (let* ((template-name (intern (concat "tempo-template-"
241 (command-name template-name
))
242 (set template-name elements
)
243 (fset command-name
(list 'lambda
(list '&optional
'arg
)
245 (concat "Insert a " name
"."))
246 (list 'interactive
"*P")
247 (list 'tempo-insert-template
(list 'quote
249 (list 'if
'tempo-insert-region
250 (list 'not
'arg
) 'arg
))))
252 (tempo-add-tag tag template-name taglist
))
256 ;;; tempo-insert-template
258 (defun tempo-insert-template (template on-region
)
260 TEMPLATE is the template to be inserted. If ON-REGION is non-nil the
261 `r' elements are replaced with the current region. In Transient Mark
262 mode, ON-REGION is ignored and assumed true if the region is active."
263 (if (and (boundp 'transient-mark-mode
)
268 (set-marker tempo-region-start
(min (mark) (point)))
269 (set-marker tempo-region-stop
(max (mark) (point))))
271 (goto-char tempo-region-start
))
273 (tempo-insert-mark (point-marker))
274 (mapcar (function (lambda (elt)
275 (tempo-insert elt on-region
)))
276 (symbol-value template
))
277 (tempo-insert-mark (point-marker)))
279 (tempo-forget-insertions)
280 (and (boundp 'transient-mark-mode
)
287 (defun tempo-insert (element on-region
)
288 "Insert a template element.
289 Insert one element from a template. If ON-REGION is non-nil the `r'
290 elements are replaced with the current region.
292 See documentation for `tempo-define-template' for the kind of elements
294 (cond ((stringp element
) (tempo-process-and-insert-string element
))
295 ((and (consp element
) (eq (car element
) 'p
))
296 (tempo-insert-prompt (cdr element
)))
297 ((and (consp element
) (eq (car element
) 'r
))
299 (goto-char tempo-region-stop
)
300 (tempo-insert-prompt (cdr element
))))
301 ((and (consp element
) (eq (car element
) 's
))
302 (if tempo-interactive
303 (tempo-insert-named (cdr element
))
304 (tempo-insert-mark (point-marker))))
305 ((and (consp element
) (eq (car element
) 'l
))
306 (mapcar (function (lambda (elt) (tempo-insert elt on-region
)))
308 ((eq element
'p
) (tempo-insert-mark (point-marker)))
309 ((eq element
'r
) (if on-region
310 (goto-char tempo-region-stop
)
311 (tempo-insert-mark (point-marker))))
312 ((eq element
'r
>) (if on-region
314 (goto-char tempo-region-stop
)
315 (indent-region (mark) (point) nil
))
316 (tempo-insert-mark (point-marker))))
317 ((eq element
'>) (indent-according-to-mode))
318 ((eq element
'&) (if (not (or (= (current-column) 0)
321 "^\\s-*\\=" nil t
))))
323 ((eq element
'%
) (if (not (or (eolp)
326 "\\=\\s-*$" nil t
))))
328 ((eq element
'n
) (insert "\n"))
329 ((eq element
'n
>) (insert "\n") (indent-according-to-mode))
330 ;; Bug: If the 'o is the first element in a template, strange
331 ;; things can happen when the template is inserted at the
332 ;; beginning of a line.
333 ((eq element
'o
) (if (not (or on-region
337 "\\=\\s-*$" nil t
))))
340 (t (tempo-insert (or (tempo-is-user-element element
)
345 ;;; tempo-insert-prompt
347 (defun tempo-insert-prompt (prompt)
348 "Prompt for a text string and insert it in the current buffer.
349 If the variable `tempo-interactive' is non-nil the user is prompted
350 for a string in the minibuffer, which is then inserted in the current
351 buffer. If `tempo-interactive' is nil, the current point is placed on
354 PROMPT is the prompt string or a list containing the prompt string and
355 a name to save the inserted text under."
356 (if tempo-interactive
357 (let ((prompt-string (if (listp prompt
)
360 (save-name (and (listp prompt
) (nth 1 prompt
)))
364 (setq inserted-text
(read-string prompt-string
))
365 (insert inserted-text
)
367 (tempo-remember-insertion save-name inserted-text
))))
368 (tempo-insert-mark (point-marker))))
371 ;;; tempo-is-user-element
373 (defun tempo-is-user-element (element)
374 "Tries all the user-defined element handlers in
375 `tempo-user-elements'"
376 ;; Sigh... I need (some list)
378 (mapcar (function (lambda (handler)
379 (let ((result (funcall handler element
)))
380 (if result
(throw 'found result
)))))
385 ;;; tempo-remember-insertion
387 (defun tempo-remember-insertion (save-name string
)
388 "Save the text in STRING under the name SAVE-NAME for later retrieval."
389 (setq tempo-named-insertions
(cons (cons save-name string
)
390 tempo-named-insertions
)))
393 ;;; tempo-forget-insertions
395 (defun tempo-forget-insertions ()
396 "Forget all the saved named insertions."
397 (setq tempo-named-insertions nil
))
400 ;;; tempo-insert-named
402 (defun tempo-insert-named (elt)
403 "Insert the previous insertion saved under a named specified in ELT.
404 The name is in the car of ELT."
405 (let* ((name (car elt
))
406 (insertion (cdr (assq name tempo-named-insertions
))))
409 (error "Named insertion not found"))))
412 ;;; tempo-process-and-insert-string
414 (defun tempo-process-and-insert-string (string)
415 "Insert a string from a template.
416 Run a string through the preprocessors in `tempo-insert-string-functions'
417 and insert the results."
418 (cond ((null tempo-insert-string-functions
)
420 ((symbolp tempo-insert-string-functions
)
422 (apply tempo-insert-string-functions
(list string
))))
423 ((listp tempo-insert-string-functions
)
424 (mapcar (function (lambda (fn)
425 (setq string
(apply fn string
))))
426 tempo-insert-string-functions
))
428 (error "Bogus value in tempo-insert-string-functions: %s"
429 tempo-insert-string-functions
)))
433 ;;; tempo-insert-mark
435 (defun tempo-insert-mark (mark)
436 "Insert a mark `tempo-marks' while keeping it sorted"
437 (cond ((null tempo-marks
) (setq tempo-marks
(list mark
)))
438 ((< mark
(car tempo-marks
)) (setq tempo-marks
(cons mark tempo-marks
)))
439 (t (let ((lp tempo-marks
))
441 (<= (car (cdr lp
)) mark
))
443 (if (not (= mark
(car lp
)))
444 (setcdr lp
(cons mark
(cdr lp
))))))))
447 ;;; tempo-forward-mark
449 (defun tempo-forward-mark ()
450 "Jump to the next mark in `tempo-forward-mark-list'."
452 (let ((next-mark (catch 'found
457 (throw 'found mark
))))
459 ;; return nil if not found
462 (goto-char next-mark
))))
465 ;;; tempo-backward-mark
467 (defun tempo-backward-mark ()
468 "Jump to the previous mark in `tempo-back-mark-list'."
470 (let ((prev-mark (catch 'found
475 (if (<= (point) mark
)
481 (goto-char prev-mark
))))
486 (defun tempo-add-tag (tag template
&optional tag-list
)
488 Add the TAG, that should complete to TEMPLATE to the list in TAG-LIST,
489 or to `tempo-tags' if TAG-LIST is nil."
491 (interactive "sTag: \nCTemplate: ")
493 (setq tag-list
'tempo-tags
))
494 (if (not (assoc tag
(symbol-value tag-list
)))
495 (set tag-list
(cons (cons tag template
) (symbol-value tag-list
))))
496 (tempo-invalidate-collection))
499 ;;; tempo-use-tag-list
501 (defun tempo-use-tag-list (tag-list &optional completion-function
)
502 "Install TAG-LIST to be used for template completion in the current buffer.
503 TAG-LIST is a symbol whose variable value is a tag list created with
506 COMPLETION-FUNCTION is an obsolete option for specifyingis an optional
507 function or string that is used by `\\[tempo-complete-tag]' to find a
508 string to match the tag against. It has the same definition as the
509 variable `tempo-match-finder'. In this version, supplying a
510 COMPLETION-FUNCTION just sets `tempo-match-finder' locally."
511 (let ((old (assq tag-list tempo-local-tags
)))
513 (setcdr old completion-function
)
514 (setq tempo-local-tags
(cons (cons tag-list completion-function
)
516 (if completion-function
517 (setq tempo-match-finder completion-function
))
518 (tempo-invalidate-collection))
521 ;;; tempo-invalidate-collection
523 (defun tempo-invalidate-collection ()
524 "Marks the tag collection as obsolete.
525 Whenever it is needed again it will be rebuilt."
526 (setq tempo-dirty-collection t
))
529 ;;; tempo-build-collection
531 (defun tempo-build-collection ()
532 "Build a collection of all the tags and return it.
533 If `tempo-dirty-collection' is NIL, the old collection is reused."
534 (setq tempo-dirty nil
)
535 (or (and (not tempo-dirty-collection
)
537 (setq tempo-collection
538 (apply (function append
)
539 (mapcar (function (lambda (tag-list)
541 ; tempo-local-tags changes,
543 (eval (car tag-list
))))
544 tempo-local-tags
)))))
547 ;;; tempo-find-match-string
549 (defun tempo-find-match-string (finder)
550 "Find a string to be matched against a tag list.
551 FINDER is a function or a string. Returns (STRING . POS)."
552 (cond ((stringp finder
)
554 (or (re-search-backward finder nil t
)
556 (cons (buffer-substring (match-beginning 1)
557 (match-end 1)) ; This seems to be a
559 (match-beginning 1)))
564 ;;; tempo-complete-tag
566 (defun tempo-complete-tag (&optional silent
)
567 "Look for a tag and expand it.
568 All the tags in the tag lists in `tempo-local-tags' (this includes
569 `tempo-tags') are searched for a match for the text before the point.
570 The way the string to match for is determined can be altered with the
571 variable `tempo-match-finder'
573 If a single match is found, the corresponding template is expanded in
574 place of the matching string.
576 If a partial completion or no match at all is found, and SILENT is
577 non-NIL, the function will give a signal.
579 If a partial completion is found and `tempo-show-completion-buffer' is
580 non-NIL, a buffer containing possible completions is displayed."
582 ;; This function may look like a hack, but this is how I want it to
585 (let* ((collection (tempo-build-collection))
586 (match-info (tempo-find-match-string tempo-match-finder
))
587 (match-string (car match-info
))
588 (match-start (cdr match-info
))
589 (exact (assoc match-string collection
))
590 (compl (or (car exact
)
591 (try-completion match-string collection
))))
592 (if compl
(delete-region match-start
(point)))
593 (cond ((null compl
) (or silent
(ding)))
594 ((eq compl t
) (tempo-insert-template
595 (cdr (assoc match-string
598 (t (if (setq exact
(assoc compl collection
))
599 (tempo-insert-template (cdr exact
) nil
)
602 (if tempo-show-completion-buffer
603 (tempo-display-completions match-string
608 ;;; tempo-display-completions
610 (defun tempo-display-completions (string tag-list
)
611 "Show a buffer containing possible completions for STRING."
612 (if tempo-leave-completion-buffer
613 (with-output-to-temp-buffer "*Completions*"
614 (display-completion-list
615 (all-completions string tag-list
)))
616 (save-window-excursion
617 (with-output-to-temp-buffer "*Completions*"
618 (display-completion-list
619 (all-completions string tag-list
)))
624 ;;; tempo.el ends here