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