(paragraph-start, paragraph-separate): Default values no longer start
[bpt/emacs.git] / lisp / tempo.el
1 ;;; tempo.el --- Flexible template insertion
2 ;; Copyright (C) 1994 Free Software Foundation, Inc.
3
4 ;; Author: David K}gedal <davidk@lysator.liu.se >
5 ;; Created: 16 Feb 1994
6 ;; Version: 1.2
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
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
33 ;; this program.
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
74 ;; The latest tempo.el distribution can be fetched from
75 ;; ftp.lysator.liu.se in the directory /pub/emacs
76
77 ;;; Known bugs:
78
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
82 ;; be easily avoided.
83
84 ;; The 'o tag is also a problem when including the region. This will
85 ;; be looked into.
86
87 ;; Clicking mouse-2 in the completion buffer gives strange results.
88
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.
92
93 ;;; Code:
94
95 ;; (provide 'tempo)
96
97 ;;; User options
98
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")
103
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.
109
110 In Transient Mark mode, this option is unused.")
111
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")
115
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.")
119
120 ;;; Internal variables
121
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." )
125
126 (defvar tempo-tags nil
127 "An association list with tags and corresponding templates")
128
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.
135
136 `tempo-tags' is always in the last position in this list.")
137
138 (defvar tempo-collection nil
139 "A collection of all the tags defined for the current buffer.")
140
141 (defvar tempo-dirty-collection t
142 "Indicates if the tag collection needs to be rebuilt.")
143
144 (defvar tempo-marks nil
145 "A list of marks to jump to with `\\[tempo-forward-mark]' and `\\[tempo-backward-mark]'.")
146
147 (defvar tempo-match-finder "\\b\\([^\\b]+\\)\\="
148 "The regexp or function used to find the string to match against tags.
149
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.
156
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
159 point.
160
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.")
165
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")
171
172 (defvar tempo-named-insertions nil
173 "Temporary storage for named insertions")
174
175 (defvar tempo-region-start (make-marker)
176 "Region start when inserting around the region")
177
178 (defvar tempo-region-stop (make-marker)
179 "Region stop when inserting around the region")
180
181 ;; Make some variables local to every buffer
182
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)
188
189 ;;; Functions
190
191 ;;
192 ;; tempo-define-template
193
194 (defun tempo-define-template (name elements &optional tag documentation taglist)
195 "Define a template.
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.
199
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'
206
207 The elements in ELEMENTS can be of several types:
208
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
233 line.
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."
238
239 (let* ((template-name (intern (concat "tempo-template-"
240 name)))
241 (command-name template-name))
242 (set template-name elements)
243 (fset command-name (list 'lambda (list '&optional 'arg)
244 (or documentation
245 (concat "Insert a " name "."))
246 (list 'interactive "*P")
247 (list 'tempo-insert-template (list 'quote
248 template-name)
249 (list 'if 'tempo-insert-region
250 (list 'not 'arg) 'arg))))
251 (and tag
252 (tempo-add-tag tag template-name taglist))
253 command-name))
254
255 ;;;
256 ;;; tempo-insert-template
257
258 (defun tempo-insert-template (template on-region)
259 "Insert a template.
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)
264 transient-mark-mode
265 mark-active)
266 (setq on-region t))
267 (and on-region
268 (set-marker tempo-region-start (min (mark) (point)))
269 (set-marker tempo-region-stop (max (mark) (point))))
270 (if on-region
271 (goto-char tempo-region-start))
272 (save-excursion
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)))
278 (tempo-forward-mark)
279 (tempo-forget-insertions)
280 (and (boundp 'transient-mark-mode)
281 transient-mark-mode
282 (deactivate-mark)))
283
284 ;;;
285 ;;; tempo-insert
286
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.
291
292 See documentation for `tempo-define-template' for the kind of elements
293 possible."
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))
298 (if on-region
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)))
307 (cdr element)))
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
313 (progn
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)
319 (save-excursion
320 (re-search-backward
321 "^\\s-*\\=" nil t))))
322 (insert "\n")))
323 ((eq element '%) (if (not (or (eolp)
324 (save-excursion
325 (re-search-forward
326 "\\=\\s-*$" nil t))))
327 (insert "\n")))
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
334 (eolp)
335 (save-excursion
336 (re-search-forward
337 "\\=\\s-*$" nil t))))
338 (open-line 1)))
339 ((null element))
340 (t (tempo-insert (or (tempo-is-user-element element)
341 (eval element))
342 on-region))))
343
344 ;;;
345 ;;; tempo-insert-prompt
346
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
352 `tempo-mark'.
353
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)
358 (car prompt)
359 prompt))
360 (save-name (and (listp prompt) (nth 1 prompt)))
361 inserted-text)
362
363 (progn
364 (setq inserted-text (read-string prompt-string))
365 (insert inserted-text)
366 (if save-name
367 (tempo-remember-insertion save-name inserted-text))))
368 (tempo-insert-mark (point-marker))))
369
370 ;;;
371 ;;; tempo-is-user-element
372
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)
377 (catch 'found
378 (mapcar (function (lambda (handler)
379 (let ((result (funcall handler element)))
380 (if result (throw 'found result)))))
381 tempo-user-elements)
382 (throw 'found nil)))
383
384 ;;;
385 ;;; tempo-remember-insertion
386
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)))
391
392 ;;;
393 ;;; tempo-forget-insertions
394
395 (defun tempo-forget-insertions ()
396 "Forget all the saved named insertions."
397 (setq tempo-named-insertions nil))
398
399 ;;;
400 ;;; tempo-insert-named
401
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))))
407 (if insertion
408 (insert insertion)
409 (error "Named insertion not found"))))
410
411 ;;;
412 ;;; tempo-process-and-insert-string
413
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)
419 nil)
420 ((symbolp tempo-insert-string-functions)
421 (setq string
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))
427 (t
428 (error "Bogus value in tempo-insert-string-functions: %s"
429 tempo-insert-string-functions)))
430 (insert string))
431
432 ;;;
433 ;;; tempo-insert-mark
434
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))
440 (while (and (cdr lp)
441 (<= (car (cdr lp)) mark))
442 (setq lp (cdr lp)))
443 (if (not (= mark (car lp)))
444 (setcdr lp (cons mark (cdr lp))))))))
445
446 ;;;
447 ;;; tempo-forward-mark
448
449 (defun tempo-forward-mark ()
450 "Jump to the next mark in `tempo-forward-mark-list'."
451 (interactive)
452 (let ((next-mark (catch 'found
453 (mapcar
454 (function
455 (lambda (mark)
456 (if (< (point) mark)
457 (throw 'found mark))))
458 tempo-marks)
459 ;; return nil if not found
460 nil)))
461 (if next-mark
462 (goto-char next-mark))))
463
464 ;;;
465 ;;; tempo-backward-mark
466
467 (defun tempo-backward-mark ()
468 "Jump to the previous mark in `tempo-back-mark-list'."
469 (interactive)
470 (let ((prev-mark (catch 'found
471 (let (last)
472 (mapcar
473 (function
474 (lambda (mark)
475 (if (<= (point) mark)
476 (throw 'found last))
477 (setq last mark)))
478 tempo-marks)
479 last))))
480 (if prev-mark
481 (goto-char prev-mark))))
482
483 ;;;
484 ;;; tempo-add-tag
485
486 (defun tempo-add-tag (tag template &optional tag-list)
487 "Add a template tag.
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."
490
491 (interactive "sTag: \nCTemplate: ")
492 (if (null tag-list)
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))
497
498 ;;;
499 ;;; tempo-use-tag-list
500
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
504 `tempo-add-tag'.
505
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)))
512 (if old
513 (setcdr old completion-function)
514 (setq tempo-local-tags (cons (cons tag-list completion-function)
515 tempo-local-tags))))
516 (if completion-function
517 (setq tempo-match-finder completion-function))
518 (tempo-invalidate-collection))
519
520 ;;;
521 ;;; tempo-invalidate-collection
522
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))
527
528 ;;;
529 ;;; tempo-build-collection
530
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)
536 tempo-collection)
537 (setq tempo-collection
538 (apply (function append)
539 (mapcar (function (lambda (tag-list)
540 ; If the format for
541 ; tempo-local-tags changes,
542 ; change this
543 (eval (car tag-list))))
544 tempo-local-tags)))))
545
546 ;;;
547 ;;; tempo-find-match-string
548
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)
553 (save-excursion
554 (or (re-search-backward finder nil t)
555 0))
556 (cons (buffer-substring (match-beginning 1)
557 (match-end 1)) ; This seems to be a
558 ; bug in emacs
559 (match-beginning 1)))
560 (t
561 (funcall finder))))
562
563 ;;;
564 ;;; tempo-complete-tag
565
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'
572
573 If a single match is found, the corresponding template is expanded in
574 place of the matching string.
575
576 If a partial completion or no match at all is found, and SILENT is
577 non-NIL, the function will give a signal.
578
579 If a partial completion is found and `tempo-show-completion-buffer' is
580 non-NIL, a buffer containing possible completions is displayed."
581
582 ;; This function may look like a hack, but this is how I want it to
583 ;; work.
584 (interactive "*")
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
596 collection))
597 nil))
598 (t (if (setq exact (assoc compl collection))
599 (tempo-insert-template (cdr exact) nil)
600 (insert compl)
601 (or silent (ding))
602 (if tempo-show-completion-buffer
603 (tempo-display-completions match-string
604 collection)))))))
605
606
607 ;;;
608 ;;; tempo-display-completions
609
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)))
620 (sit-for 32767))))
621
622 (provide 'tempo)
623
624 ;;; tempo.el ends here