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