25848
|
1 @c This is part of the Emacs manual.
|
|
2 @c Copyright (C) 1994, 1995 Free Software Foundation, Inc.
|
|
3 @c Author: Daniel.Pfeiffer@Informatik.START.dbp.de, fax (+49 69) 7588-2389
|
|
4 @c See file emacs.texi for copying conditions.
|
|
5 @node Autotypist, Picture, Abbrevs, Top
|
|
6 @chapter Features for Automatic Typing
|
|
7 @cindex text
|
|
8 @cindex selfinserting text
|
|
9 @cindex autotypist
|
|
10
|
|
11 @dircategory Editors
|
|
12 @direntry
|
|
13 * Autotype: (autotype). Convenient features for text that you enter frequently
|
|
14 in Emacs.
|
|
15 @end direntry
|
|
16
|
|
17 Under certain circumstances you will find yourself typing similar things
|
|
18 over and over again. This is especially true of form letters and programming
|
|
19 language constructs. Project-specific header comments, flow-control
|
|
20 constructs or magic numbers are essentially the same every time. Emacs has
|
|
21 various features for doing tedious and repetitive typing chores for you.
|
|
22
|
|
23 One solution is using skeletons, flexible rules that say what to
|
|
24 insert, and how to do it. Various programming language modes offer some
|
|
25 ready-to-use skeletons, and you can adapt them to suit your needs or
|
|
26 taste, or define new ones.
|
|
27
|
|
28 Another feature is automatic insertion of what you want into empty files,
|
|
29 depending on the file-name or the mode as appropriate. You can have a file or
|
|
30 a skeleton inserted, or you can call a function. Then there is the
|
|
31 possibility to have Un*x interpreter scripts automatically take on a magic
|
|
32 number and be executable as soon as they are saved. Or you can have a
|
|
33 copyright notice's year updated, if necessary, every time you save a file.
|
|
34
|
|
35 @menu
|
|
36 * Using Skeletons:: How to insert a skeleton into your text.
|
|
37 * Wrapping Skeletons:: Putting existing text within a skeleton.
|
|
38 * Skeletons as Abbrevs:: An alternative for issuing skeleton commands.
|
|
39 * Skeleton Language:: Making skeleton commands insert what you want.
|
|
40 * Inserting Pairs:: Typing one character and getting another after point.
|
|
41 * Autoinserting:: Filling up empty files as soon as you visit them.
|
|
42 * Copyrights:: Inserting and updating copyrights.
|
|
43 * Executables:: Turning interpreter scripts into executables.
|
|
44 @end menu
|
|
45
|
|
46
|
|
47
|
|
48 @node Using Skeletons
|
|
49 @section Using Skeletons
|
|
50 @cindex skeletons
|
|
51 @cindex using skeletons
|
|
52
|
|
53 When you want Emacs to insert a form letter or a typical construct of the
|
|
54 programming language you are using, skeletons are a means of accomplishing
|
|
55 this. Normally skeletons each have a command of their own, that, when called,
|
|
56 will insert the skeleton. These commands can be issued in the usual ways
|
|
57 (@xref{Commands}). Modes that offer various skeletons will often bind these
|
|
58 to key-sequences on the @kbd{C-c} prefix, as well as having an @cite{Insert}
|
|
59 menu and maybe even predefined abbrevs for them (@xref{Skeletons as Abbrevs}).
|
|
60
|
|
61 The simplest kind of skeleton will simply insert some text indented
|
|
62 according to the major mode and leave the cursor at a likely place in the
|
|
63 middle. Interactive skeletons may prompt you for a string that will be part
|
|
64 of the inserted text.
|
|
65
|
|
66 Skeletons may ask for input several times. They even have a looping
|
|
67 mechanism in which you will be asked for input as long as you are willing to
|
|
68 furnish it. An example would be multiple ``else if'' conditions. You can
|
|
69 recognize this situation by a prompt ending in ``RET, C-g or C-h''. This
|
|
70 means that entering an empty string will simply assume that you are finished.
|
|
71 Typing quit on the other hand terminates the loop but also the rest of the
|
|
72 skeleton, e.g. an ``else'' clause is skipped. Only a syntactically necessary
|
|
73 termination still gets inserted.
|
|
74
|
|
75
|
|
76
|
|
77 @node Wrapping Skeletons
|
|
78 @section Wrapping Skeletons Around Existing Test
|
|
79 @cindex wrapping skeletons
|
|
80
|
|
81 Often you will find yourself with some code that for whatever reason
|
|
82 suddenly becomes conditional. Or you have written a bit of text and want to
|
|
83 put it in the middle of a form letter. Skeletons provide a means for
|
|
84 accomplishing this, and can even, in the case of programming languages,
|
|
85 reindent the wrapped code for you.
|
|
86
|
|
87 Skeleton commands take an optional numeric prefix argument
|
|
88 (@xref{Arguments}). This is interpreted in two different ways depending
|
|
89 on whether the prefix is positive, i.e. forwards oriented or negative,
|
|
90 i.e. backwards oriented.
|
|
91
|
|
92 A positive prefix means to wrap the skeleton around that many following
|
|
93 words. This is accomplished by putting the words there where the point is
|
|
94 normally left after that skeleton is inserted (@xref{Using Skeletons}). The
|
|
95 point (@xref{Point}) is left at the next interesting spot in the skeleton
|
|
96 instead.
|
|
97
|
|
98 A negative prefix means to do something similar with that many precedingly
|
|
99 marked interregions (@xref{Mark}). In the simplest case, if you type
|
|
100 @kbd{M--} just before issuing the skeleton command, that will wrap the
|
|
101 skeleton around the current region, just like a positive argument would have
|
|
102 wrapped it around a number of words.
|
|
103
|
|
104 Smaller negative arguments will wrap that many interregions into successive
|
|
105 interesting spots within the skeleton, again leaving the point at the next one.
|
|
106 We speak about interregions rather than regions here, because we treat them in
|
|
107 the order they appear in the buffer, which coincides with successive regions
|
|
108 only if they were marked in order.
|
|
109
|
|
110 That is, if you marked in alphabetical order the points A B C [] (where []
|
|
111 represents the point) and call a skeleton command with @kbd{M-- 3}, you will
|
|
112 wrap the text from A to B into the first interesting spot of the skeleton, the
|
|
113 text from B to C into the next one, the text from C to the point into the
|
|
114 third one, and leave the point in the fourth one. If there are less marks in
|
|
115 the buffer, or if the skeleton defines less interesting points, the surplus is
|
|
116 ignored.
|
|
117
|
|
118 If, on the other hand, you marked in alphabetical order the points [] A C B,
|
|
119 and call a skeleton command with @kbd{M-- 3}, you will wrap the text from
|
|
120 point to A, then the text from A to C and finally the text from C to B. This
|
|
121 is done because the regions overlap and Emacs would be helplessly lost if it
|
|
122 tried to follow the order in which you marked these points.
|
|
123
|
|
124
|
|
125
|
|
126 @node Skeletons as Abbrevs
|
|
127 @section Skeletons as Abbrev Expansions
|
|
128 @cindex skeletons as abbrevs
|
|
129
|
|
130 Rather than use a keybinding for every skeleton command, you can also define
|
|
131 an abbreviation (@xref{Defining Abbrevs}) that will expand (@xref{Expanding
|
|
132 Abbrevs}) into the skeleton.
|
|
133
|
|
134 Say you want @samp{ifst} to be an abbreviation for the C language if
|
|
135 statement. You will tell Emacs that @samp{ifst} expands to the empty string
|
|
136 and then calls the skeleton command. In Emacs-lisp you can say something like
|
|
137 @code{(define-abbrev c-mode-abbrev-table "ifst" "" 'c-if)}. Or you can edit
|
|
138 the output from @kbd{M-x list-abbrevs} to make it look like this:
|
|
139
|
|
140 @example
|
|
141 (c-mode-abbrev-table)
|
|
142 "if" 0 "" c-if
|
|
143 @end example
|
|
144
|
|
145 @noindent
|
|
146 (Some blank lines of no semantic significance, and other abbrev tables,
|
|
147 have been omitted.)
|
|
148
|
|
149
|
|
150
|
|
151 @node Skeleton Language
|
|
152 @section Skeleton Language
|
|
153 @cindex skeleton language
|
|
154
|
|
155 @findex skeleton-insert
|
|
156 Skeletons are an shorthand extension to the Lisp language, where various
|
|
157 atoms directly perform either actions on the current buffer or rudimentary
|
|
158 flow control mechanisms. Skeletons are interpreted by the function
|
|
159 @code{skeleton-insert}.
|
|
160
|
|
161 A skeleton is a list starting with an interactor, which is usually a
|
|
162 prompt-string, or @code{nil} when not needed, but can also be a Lisp
|
|
163 expression for complex read functions or for returning some calculated value.
|
|
164 The rest of the list are any number of elements as described in the following
|
|
165 table:
|
|
166
|
|
167 @table @code
|
|
168 @item "string", ?c, ?\c
|
|
169 @vindex skeleton-transformation
|
|
170 Insert string or character. Literal strings and characters are passed through
|
|
171 @code{skeleton-transformation} when that is non-@code{nil}.
|
|
172 @item \n
|
|
173 Insert a newline and align under current line. Use newline character
|
|
174 @code{?\n} to prevent alignment.
|
|
175 @item _
|
|
176 Interesting point. When wrapping skeletons around successive regions, they are
|
|
177 put at these places. Point is left at first @code{_} where nothing is wrapped.
|
|
178 @item >
|
|
179 Indent line according to major mode. When following element is @code{_}, and
|
|
180 there is a interregion that will be wrapped here, indent that interregion.
|
|
181 @item &
|
|
182 Logical and. Iff preceding element moved point, i.e. usually inserted
|
|
183 something, do following element.
|
|
184 @item |
|
|
185 Logical xor. Iff preceding element didn't move point, i.e. usually inserted
|
|
186 nothing, do following element.
|
|
187 @item -number
|
|
188 Delete preceding number characters. Depends on value of
|
|
189 @code{skeleton-untabify}.
|
|
190 @item (), nil
|
|
191 Ignored.
|
|
192 @item lisp expression
|
|
193 Evaluated, and the return value is again interpreted as a skeleton element.
|
|
194 @item str
|
|
195 A special variable that, when evaluated the first time, usually prompts
|
|
196 for input according to the skeleton's interactor. It is then set to the
|
|
197 return value resulting from the interactor. Each subskeleton has its local
|
|
198 copy of this variable.
|
|
199 @item v1, v2
|
|
200 Skeleton-local user variables.
|
|
201 @item '
|
|
202 Evaluate following lisp expression for its side-effect, but prevent it from
|
|
203 being interpreted as a skeleton element.
|
|
204 @item skeleton
|
|
205 Subskeletons are inserted recursively, not once, but as often as the user
|
|
206 enters something at the subskeletons interactor. Thus there must be a
|
|
207 @code{str} in the subskeleton. They can also be used non-interactively, when
|
|
208 prompt is a lisp-expression that returns successive list-elements.
|
|
209 @item resume:
|
|
210 Ignored. Execution resumes here when the user quit during skeleton
|
|
211 interpretation.
|
|
212 @item quit
|
|
213 A constant which is non-@code{nil} when the @code{resume:} section was entered
|
|
214 because the user quit.
|
|
215 @end table
|
|
216
|
|
217 @findex skeleton-further-elements
|
|
218 Some modes also use other skeleton elements they themselves defined. For
|
|
219 example in shell script mode's skeletons you will find @code{<} which does a
|
|
220 rigid indentation backwards, or in cc-mode's skeletons you find the
|
|
221 self-inserting elements @code{@{} and @code{@}}. These are defined by the
|
|
222 buffer-local variable @code{skeleton-further-elements} which is a list of
|
|
223 variables bound while interpreting a skeleton.
|
|
224
|
|
225 @findex define-skeleton
|
|
226 The macro @code{define-skeleton} defines a command for interpreting a
|
|
227 skeleton. The first argument is the command name, the second is a
|
|
228 documentation string, and the rest is an interactor and any number of skeleton
|
|
229 elements together forming a skeleton. This skeleton is assigned to a variable
|
|
230 of the same name as the command and can thus be overridden from your
|
|
231 @file{~/.emacs} file (@xref{Init File}).
|
|
232
|
|
233
|
|
234
|
|
235 @node Inserting Pairs
|
|
236 @section Inserting Matching Pairs of Characters
|
|
237 @cindex inserting pairs
|
|
238 @cindex pairs
|
|
239
|
|
240 Various characters usually appear in pairs. When, for example, you insert
|
|
241 an open parenthesis, no matter whether you are programming or writing prose,
|
|
242 you will surely enter a closing one later. By entering both at the same time
|
|
243 and leaving the cursor inbetween, Emacs can guarantee you that such
|
|
244 parentheses are always balanced. And if you have a non-qwerty keyboard, where
|
|
245 typing some of the stranger programming language symbols makes you bend your
|
|
246 fingers backwards, this can be quite relieving too.
|
|
247
|
|
248 @findex pair-insert-maybe
|
|
249 @vindex pair
|
|
250 This is done by binding the first key (@xref{Rebinding}) of the pair to
|
|
251 @code{pair-insert-maybe} instead of @code{self-insert-command}. The maybe
|
|
252 comes from the fact that this at first surprising behaviour is initially
|
|
253 turned off. To enable it, you must set @code{pair} to some non-@code{nil}
|
|
254 value. And even then, a positive argument (@xref{Arguments}) will make this
|
|
255 key behave like a self inserting key (@xref{Inserting Text}).
|
|
256
|
|
257 @findex pair-on-word
|
|
258 While this breaks with the stated intention of always balancing pairs, it
|
|
259 turns out that one often doesn't want pairing to occur, when the following
|
|
260 character is part of a word. If you want pairing to occur even then, set
|
|
261 @code{pair-on-word} to some non-@code{nil} value.
|
|
262
|
|
263 @vindex pair-alist
|
|
264 Pairing is possible for all visible characters. By default the parenthesis
|
|
265 `(', the square bracket `[', the brace `@{', the pointed bracket `<' and the
|
|
266 backquote ``' will all pair to the symmetrical character. All other
|
|
267 characters will pair themselves. This behaviour can be modified by the
|
|
268 variable @code{pair-alist}. This is in fact an alist of skeletons
|
|
269 (@xref{Skeleton Language}), with the first part of each sublist matching the
|
|
270 typed character. This is the position of the interactor, but since pairs
|
|
271 don't need the @code{str} element, this is ignored.
|
|
272
|
|
273 Some modes have bound the command @code{pair-insert-maybe} to relevant keys.
|
|
274 These modes also configure the pairs as appropriate. For example, when typing
|
|
275 english prose, you'd expect the backquote (`) to pair to the quote (') while
|
|
276 in Shell script mode it must pair to itself. They can also inhibit pairing
|
|
277 in certain contexts. For example an escaped character will stand for itself.
|
|
278
|
|
279
|
|
280
|
|
281 @node Autoinserting
|
|
282 @section Autoinserting Text in Empty Files
|
|
283 @cindex autoinserting
|
|
284
|
|
285 @findex auto-insert
|
|
286 @kbd{M-x auto-insert} will put some predefined text at the beginning of
|
|
287 the buffer. The main application for this function, as its name suggests,
|
|
288 is to have it be called automatically every time an empty, and only an
|
|
289 empty file is visited. This is accomplished by putting @code{(add-hook
|
|
290 'find-file-hooks 'auto-insert)} into your @file{~/.emacs} file (@xref{Init
|
|
291 File}).
|
|
292
|
|
293 @vindex auto-insert-alist
|
|
294 What gets inserted, if anything, is determined by the variable
|
|
295 @code{auto-insert-alist}. The @code{car}s of this list are each either a mode
|
|
296 name, making an element applicable when a buffer is in that mode. Or they
|
|
297 can be a string, which is a regexp matched against the buffer's file name.
|
|
298 In that way different kinds of files that have the same mode in Emacs can be
|
|
299 distinguished. The @code{car}s may also be @code{cons}-cells consisting of
|
|
300 mode name or regexp as above and an additional descriptive string.
|
|
301
|
|
302 When a matching element is found, the @code{cdr} says what to do. It may
|
|
303 be a string, which is a file name, whose contents are to be inserted, if
|
|
304 that file is found in the directory @code{auto-insert-directory} or under a
|
|
305 absolute file name. Or it can be a skeleton (@xref{Skeleton Language}) to
|
|
306 be inserted.
|
|
307
|
|
308 It can also be a function, which allows doing various things. The function
|
|
309 can simply insert some text, indeed, it can be skeleton command (@xref{Using
|
|
310 Skeletons}). It can be a lambda function which will for example conditionally
|
|
311 call another function. Or it can even reset the mode for the buffer. If you
|
|
312 want to perform several such actions in order, you use a vector, i.e. several
|
|
313 of the above elements between square brackets ([...]).
|
|
314
|
|
315 By default C and C++ headers insert a definition of a symbol derived from
|
|
316 the filename to prevent multiple inclusions. C and C++ sources insert an
|
|
317 include of the header. Makefiles insert the file makefile.inc if it exists.
|
|
318
|
|
319 TeX and bibTeX mode files insert the file tex-insert.tex if it exists, while
|
|
320 LaTeX mode files insert insert a typical @code{\documentclass} frame. Html
|
|
321 files insert a skeleton with the usual frame.
|
|
322
|
|
323 Ada mode files call the Ada header skeleton command. Emacs lisp source
|
|
324 files insert the usual header, with a copyright of your environment variable
|
|
325 @code{$ORGANIZATION} or else the FSF, and prompt for valid keywords describing
|
|
326 the contents. Files in a @code{bin/} directory for which Emacs could
|
|
327 determine no specialised mode (@xref{Choosing Modes}) are set to Shell script
|
|
328 mode.
|
|
329
|
|
330 @findex define-auto-insert
|
|
331 In Lisp (@xref{Init File}) you can use the function @code{define-auto-insert}
|
|
332 to add to or modify @code{auto-insert-alist}. See its documentation with
|
|
333 @kbd{C-h f auto-insert-alist}.
|
|
334
|
|
335 @vindex auto-insert
|
|
336 The variable @code{auto-insert} says what to do when @code{auto-insert} is
|
|
337 called non-interactively, e.g. when a newly found file is empty (see above):
|
|
338 @table @code
|
|
339 @item nil
|
|
340 Do nothing.
|
|
341 @item t
|
|
342 Insert something if possible, i.e. there is a matching entry in
|
|
343 @code{auto-insert-alist}.
|
|
344 @item other
|
|
345 Insert something if possible, but mark as unmodified.
|
|
346 @end table
|
|
347
|
|
348 @vindex auto-insert-query
|
|
349 The variable @code{auto-insert-query} controls whether to ask about
|
|
350 inserting something. When this is @code{nil} inserting is only done with
|
|
351 @kbd{M-x auto-insert}. When this is @code{'function} you are queried
|
|
352 whenever @code{auto-insert} is called as a function, such as when Emacs
|
|
353 visits an empty file and you have set the above-mentioned hook. Otherwise
|
|
354 you are alway queried.
|
|
355
|
|
356 @vindex auto-insert-prompt
|
|
357 When querying, the variable @code{auto-insert-prompt}'s value is used as a
|
|
358 prompt for a y-or-n-type question. If this includes a @code{%s} construct,
|
|
359 that is replaced by what caused the insertion rule to be chosen. This is
|
|
360 either a descriptive text, the mode-name of the buffer or the regular
|
|
361 expression that matched the filename.
|
|
362
|
|
363
|
|
364
|
|
365 @node Copyrights
|
|
366 @section Inserting and Updating Copyrights
|
|
367 @cindex copyrights
|
|
368
|
|
369 @findex copyright
|
|
370 @kbd{M-x copyright} is a skeleton inserting command, that adds a copyright
|
|
371 notice at the point. The ``by'' part is taken from your environment variable
|
|
372 @code{$ORGANIZATION} or if that isn't set you are prompted for it. If the
|
|
373 buffer has a comment syntax (@xref{Comments}), this is inserted as a comment.
|
|
374
|
|
375 @findex copyright-update
|
|
376 @vindex copyright-limit
|
|
377 @vindex copyright-current-year
|
|
378 @kbd{M-x copyright-update} looks for a copyright notice in the first
|
|
379 @code{copyright-limit} characters of the buffer and updates it when necessary.
|
|
380 The current year (variable @code{copyright-current-year}) is added to the
|
|
381 existing ones, in the same format as the preceding year, i.e. 1994, '94 or 94.
|
|
382 If a dash-separated year list up to last year is found, that is extended to
|
|
383 current year, else the year is added separated by a comma. Or it replaces
|
|
384 them when this is called with a prefix argument. If a header referring to a
|
|
385 wrong version of the GNU General Public License (@xref{Copying}) is found,
|
|
386 that is updated too.
|
|
387
|
|
388 An interesting application for this function is to have it be called
|
|
389 automatically every time a file is saved. This is accomplished by putting
|
|
390 @code{(add-hook 'write-file-hooks 'copyright-update)} into your @file{~/.emacs}
|
|
391 file (@xref{Init File}).
|
|
392
|
|
393 @vindex copyright-query
|
|
394 The variable @code{copyright-query} controls whether to update the
|
|
395 copyright or whether to ask about it. When this is @code{nil} updating is
|
|
396 only done with @kbd{M-x copyright-update}. When this is @code{'function}
|
|
397 you are queried whenever @code{copyright-update} is called as a function,
|
|
398 such as in the @code{write-file-hooks} feature mentioned above. Otherwise
|
|
399 you are always queried.
|
|
400
|
|
401
|
|
402
|
|
403 @node Executables
|
|
404 @section Making Interpreter Scripts Executable
|
|
405 @cindex executables
|
|
406
|
|
407 @vindex executable-prefix
|
|
408 @vindex executable-chmod
|
|
409 Various Un*x interpreter modes such as Shell script mode or AWK mode
|
|
410 will automatically insert or update the buffer's magic number, a special
|
|
411 comment on the first line that makes the @code{exec()} systemcall know how
|
|
412 to execute the script. To this end the script is automatically made
|
|
413 executable upon saving, with @code{executable-chmod} as argument to the
|
|
414 system @code{chmod} command. The magic number is prefixed by the value of
|
|
415 @code{executable-prefix}.
|
|
416
|
|
417 @vindex executable-magicless-file-regexp
|
|
418 Any file whos name matches @code{executable-magicless-file-regexp} is not
|
|
419 furnished with a magic number, nor is it made executable. This is mainly
|
|
420 intended for resource files, which are only meant to be read in.
|
|
421
|
|
422 @vindex executable-insert
|
|
423 The variable @code{executable-insert} says what to do when
|
|
424 @code{executable-set-magic} is called non-interactively, e.g. when file has no
|
|
425 or the wrong magic number:
|
|
426 @table @code
|
|
427 @item nil
|
|
428 Do nothing.
|
|
429 @item t
|
|
430 Insert or update magic number.
|
|
431 @item other
|
|
432 Insert or update magic number, but mark as unmodified.
|
|
433 @end table
|
|
434
|
|
435 @findex executable-set-magic
|
|
436 @vindex executable-query
|
|
437 The variable @code{executable-query} controls whether to ask about
|
|
438 inserting or updating the magic number. When this is @code{nil} updating
|
|
439 is only done with @kbd{M-x executable-set-magic}. When this is
|
|
440 @code{'function} you are queried whenever @code{executable-set-magic} is
|
|
441 called as a function, such as when Emacs puts a buffer in Shell script
|
|
442 mode. Otherwise you are alway queried.
|
|
443
|
|
444 @findex executable-self-display
|
|
445 @kbd{M-x executable-self-display} adds a magic number to the buffer, which
|
|
446 will turn it into a self displaying text file, when called as a Un*x command.
|
|
447 The ``interpreter'' used is @code{executable-self-display} with argument
|
|
448 @code{+2}.
|