]> code.delx.au - gnu-emacs/blob - lispref/intro.texi
Add index item.
[gnu-emacs] / lispref / intro.texi
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4 @c See the file elisp.texi for copying conditions.
5 @setfilename ../info/intro
6
7 @c Versino of the manual.
8 @set VERSION 2.8
9
10 @node Introduction, Lisp Data Types, Top, Top
11 @comment node-name, next, previous, up
12 @chapter Introduction
13
14 Most of the GNU Emacs text editor is written in the programming
15 language called Emacs Lisp. You can write new code in Emacs Lisp and
16 install it as an extension to the editor. However, Emacs Lisp is more
17 than a mere ``extension language''; it is a full computer programming
18 language in its own right. You can use it as you would any other
19 programming language.
20
21 Because Emacs Lisp is designed for use in an editor, it has special
22 features for scanning and parsing text as well as features for handling
23 files, buffers, displays, subprocesses, and so on. Emacs Lisp is
24 closely integrated with the editing facilities; thus, editing commands
25 are functions that can also conveniently be called from Lisp programs,
26 and parameters for customization are ordinary Lisp variables.
27
28 This manual attempts to be a full description of Emacs Lisp. For a
29 beginner's introduction to Emacs Lisp, see @cite{An Introduction to
30 Emacs Lisp Programming}, by Bob Chassell, also published by the Free
31 Software Foundation. This manual presumes considerable familiarity with
32 the use of Emacs for editing; see @cite{The GNU Emacs Manual} for this
33 basic information.
34
35 Generally speaking, the earlier chapters describe features of Emacs
36 Lisp that have counterparts in many programming languages, and later
37 chapters describe features that are peculiar to Emacs Lisp or relate
38 specifically to editing.
39
40 This is edition @value{VERSION}.
41
42 @menu
43 * Caveats:: Flaws and a request for help.
44 * Lisp History:: Emacs Lisp is descended from Maclisp.
45 * Conventions:: How the manual is formatted.
46 * Version Info:: Which Emacs version is running?
47 * Acknowledgements:: The authors, editors, and sponsors of this manual.
48 @end menu
49
50 @node Caveats
51 @section Caveats
52 @cindex bugs in this manual
53
54 This manual has gone through numerous drafts. It is nearly complete
55 but not flawless. There are a few topics that are not covered, either
56 because we consider them secondary (such as most of the individual
57 modes) or because they are yet to be written. Because we are not able
58 to deal with them completely, we have left out several parts
59 intentionally. This includes most information about usage on VMS.
60
61 The manual should be fully correct in what it does cover, and it is
62 therefore open to criticism on anything it says---from specific examples
63 and descriptive text, to the ordering of chapters and sections. If
64 something is confusing, or you find that you have to look at the sources
65 or experiment to learn something not covered in the manual, then perhaps
66 the manual should be fixed. Please let us know.
67
68 @iftex
69 As you use this manual, we ask that you mark pages with corrections so
70 you can later look them up and send them to us. If you think of a simple,
71 real-life example for a function or group of functions, please make an
72 effort to write it up and send it in. Please reference any comments to
73 the chapter name, section name, and function name, as appropriate, since
74 page numbers and chapter and section numbers will change and we may have
75 trouble finding the text you are talking about. Also state the number
76 of the edition you are criticizing.
77 @end iftex
78 @ifnottex
79
80 As you use this manual, we ask that you send corrections as soon as you
81 find them. If you think of a simple, real life example for a function
82 or group of functions, please make an effort to write it up and send it
83 in. Please reference any comments to the node name and function or
84 variable name, as appropriate. Also state the number of the edition
85 you are criticizing.
86 @end ifnottex
87
88 Please mail comments and corrections to
89
90 @example
91 bug-lisp-manual@@gnu.org
92 @end example
93
94 @noindent
95 We let mail to this list accumulate unread until someone decides to
96 apply the corrections. Months, and sometimes years, go by between
97 updates. So please attach no significance to the lack of a reply---your
98 mail @emph{will} be acted on in due time. If you want to contact the
99 Emacs maintainers more quickly, send mail to
100 @code{bug-gnu-emacs@@gnu.org}.
101
102 @node Lisp History
103 @section Lisp History
104 @cindex Lisp history
105
106 Lisp (LISt Processing language) was first developed in the late 1950s
107 at the Massachusetts Institute of Technology for research in artificial
108 intelligence. The great power of the Lisp language makes it ideal
109 for other purposes as well, such as writing editing commands.
110
111 @cindex Maclisp
112 @cindex Common Lisp
113 Dozens of Lisp implementations have been built over the years, each
114 with its own idiosyncrasies. Many of them were inspired by Maclisp,
115 which was written in the 1960s at MIT's Project MAC. Eventually the
116 implementors of the descendants of Maclisp came together and developed a
117 standard for Lisp systems, called Common Lisp. In the meantime, Gerry
118 Sussman and Guy Steele at MIT developed a simplified but very powerful
119 dialect of Lisp, called Scheme.
120
121 GNU Emacs Lisp is largely inspired by Maclisp, and a little by Common
122 Lisp. If you know Common Lisp, you will notice many similarities.
123 However, many features of Common Lisp have been omitted or
124 simplified in order to reduce the memory requirements of GNU Emacs.
125 Sometimes the simplifications are so drastic that a Common Lisp user
126 might be very confused. We will occasionally point out how GNU Emacs
127 Lisp differs from Common Lisp. If you don't know Common Lisp, don't
128 worry about it; this manual is self-contained.
129
130 @pindex cl
131 A certain amount of Common Lisp emulation is available via the
132 @file{cl} library. @xref{Top,, Common Lisp Extension, cl, Common Lisp
133 Extensions}.
134
135 Emacs Lisp is not at all influenced by Scheme; but the GNU project has
136 an implementation of Scheme, called Guile. We use Guile in all new GNU
137 software that calls for extensibility.
138
139 @node Conventions
140 @section Conventions
141
142 This section explains the notational conventions that are used in this
143 manual. You may want to skip this section and refer back to it later.
144
145 @menu
146 * Some Terms:: Explanation of terms we use in this manual.
147 * nil and t:: How the symbols @code{nil} and @code{t} are used.
148 * Evaluation Notation:: The format we use for examples of evaluation.
149 * Printing Notation:: The format we use when examples print text.
150 * Error Messages:: The format we use for examples of errors.
151 * Buffer Text Notation:: The format we use for buffer contents in examples.
152 * Format of Descriptions:: Notation for describing functions, variables, etc.
153 @end menu
154
155 @node Some Terms
156 @subsection Some Terms
157
158 Throughout this manual, the phrases ``the Lisp reader'' and ``the Lisp
159 printer'' refer to those routines in Lisp that convert textual
160 representations of Lisp objects into actual Lisp objects, and vice
161 versa. @xref{Printed Representation}, for more details. You, the
162 person reading this manual, are thought of as ``the programmer'' and are
163 addressed as ``you''. ``The user'' is the person who uses Lisp
164 programs, including those you write.
165
166 @cindex fonts
167 Examples of Lisp code are formatted like this: @code{(list 1 2 3)}.
168 Names that represent metasyntactic variables, or arguments to a function
169 being described, are formatted like this: @var{first-number}.
170
171 @node nil and t
172 @subsection @code{nil} and @code{t}
173 @cindex @code{nil}, uses of
174 @cindex truth value
175 @cindex boolean
176 @cindex false
177
178 In Lisp, the symbol @code{nil} has three separate meanings: it
179 is a symbol with the name @samp{nil}; it is the logical truth value
180 @var{false}; and it is the empty list---the list of zero elements.
181 When used as a variable, @code{nil} always has the value @code{nil}.
182
183 As far as the Lisp reader is concerned, @samp{()} and @samp{nil} are
184 identical: they stand for the same object, the symbol @code{nil}. The
185 different ways of writing the symbol are intended entirely for human
186 readers. After the Lisp reader has read either @samp{()} or @samp{nil},
187 there is no way to determine which representation was actually written
188 by the programmer.
189
190 In this manual, we use @code{()} when we wish to emphasize that it
191 means the empty list, and we use @code{nil} when we wish to emphasize
192 that it means the truth value @var{false}. That is a good convention to use
193 in Lisp programs also.
194
195 @example
196 (cons 'foo ()) ; @r{Emphasize the empty list}
197 (not nil) ; @r{Emphasize the truth value @var{false}}
198 @end example
199
200 @cindex @code{t} and truth
201 @cindex true
202 In contexts where a truth value is expected, any non-@code{nil} value
203 is considered to be @var{true}. However, @code{t} is the preferred way
204 to represent the truth value @var{true}. When you need to choose a
205 value which represents @var{true}, and there is no other basis for
206 choosing, use @code{t}. The symbol @code{t} always has the value
207 @code{t}.
208
209 In Emacs Lisp, @code{nil} and @code{t} are special symbols that always
210 evaluate to themselves. This is so that you do not need to quote them
211 to use them as constants in a program. An attempt to change their
212 values results in a @code{setting-constant} error. The same is true of
213 any symbol whose name starts with a colon (@samp{:}). @xref{Constant
214 Variables}.
215
216 @node Evaluation Notation
217 @subsection Evaluation Notation
218 @cindex evaluation notation
219 @cindex documentation notation
220
221 A Lisp expression that you can evaluate is called a @dfn{form}.
222 Evaluating a form always produces a result, which is a Lisp object. In
223 the examples in this manual, this is indicated with @samp{@result{}}:
224
225 @example
226 (car '(1 2))
227 @result{} 1
228 @end example
229
230 @noindent
231 You can read this as ``@code{(car '(1 2))} evaluates to 1''.
232
233 When a form is a macro call, it expands into a new form for Lisp to
234 evaluate. We show the result of the expansion with
235 @samp{@expansion{}}. We may or may not show the result of the
236 evaluation of the expanded form.
237
238 @example
239 (third '(a b c))
240 @expansion{} (car (cdr (cdr '(a b c))))
241 @result{} c
242 @end example
243
244 Sometimes to help describe one form we show another form that
245 produces identical results. The exact equivalence of two forms is
246 indicated with @samp{@equiv{}}.
247
248 @example
249 (make-sparse-keymap) @equiv{} (list 'keymap)
250 @end example
251
252 @node Printing Notation
253 @subsection Printing Notation
254 @cindex printing notation
255
256 Many of the examples in this manual print text when they are
257 evaluated. If you execute example code in a Lisp Interaction buffer
258 (such as the buffer @samp{*scratch*}), the printed text is inserted into
259 the buffer. If you execute the example by other means (such as by
260 evaluating the function @code{eval-region}), the printed text is
261 displayed in the echo area.
262
263 Examples in this manual indicate printed text with @samp{@print{}},
264 irrespective of where that text goes. The value returned by evaluating
265 the form (here @code{bar}) follows on a separate line.
266
267 @example
268 @group
269 (progn (print 'foo) (print 'bar))
270 @print{} foo
271 @print{} bar
272 @result{} bar
273 @end group
274 @end example
275
276 @node Error Messages
277 @subsection Error Messages
278 @cindex error message notation
279
280 Some examples signal errors. This normally displays an error message
281 in the echo area. We show the error message on a line starting with
282 @samp{@error{}}. Note that @samp{@error{}} itself does not appear in
283 the echo area.
284
285 @example
286 (+ 23 'x)
287 @error{} Wrong type argument: number-or-marker-p, x
288 @end example
289
290 @node Buffer Text Notation
291 @subsection Buffer Text Notation
292 @cindex buffer text notation
293
294 Some examples describe modifications to the contents of a buffer, by
295 showing the ``before'' and ``after'' versions of the text. These
296 examples show the contents of the buffer in question between two lines
297 of dashes containing the buffer name. In addition, @samp{@point{}}
298 indicates the location of point. (The symbol for point, of course, is
299 not part of the text in the buffer; it indicates the place
300 @emph{between} two characters where point is currently located.)
301
302 @example
303 ---------- Buffer: foo ----------
304 This is the @point{}contents of foo.
305 ---------- Buffer: foo ----------
306
307 (insert "changed ")
308 @result{} nil
309 ---------- Buffer: foo ----------
310 This is the changed @point{}contents of foo.
311 ---------- Buffer: foo ----------
312 @end example
313
314 @node Format of Descriptions
315 @subsection Format of Descriptions
316 @cindex description format
317
318 Functions, variables, macros, commands, user options, and special
319 forms are described in this manual in a uniform format. The first
320 line of a description contains the name of the item followed by its
321 arguments, if any.
322 @ifnottex
323 The category---function, variable, or whatever---appears at the
324 beginning of the line.
325 @end ifnottex
326 @iftex
327 The category---function, variable, or whatever---is printed next to the
328 right margin.
329 @end iftex
330 The description follows on succeeding lines, sometimes with examples.
331
332 @menu
333 * A Sample Function Description:: A description of an imaginary
334 function, @code{foo}.
335 * A Sample Variable Description:: A description of an imaginary
336 variable,
337 @code{electric-future-map}.
338 @end menu
339
340 @node A Sample Function Description
341 @subsubsection A Sample Function Description
342 @cindex function descriptions
343 @cindex command descriptions
344 @cindex macro descriptions
345 @cindex special form descriptions
346
347 In a function description, the name of the function being described
348 appears first. It is followed on the same line by a list of argument
349 names. These names are also used in the body of the description, to
350 stand for the values of the arguments.
351
352 The appearance of the keyword @code{&optional} in the argument list
353 indicates that the subsequent arguments may be omitted (omitted
354 arguments default to @code{nil}). Do not write @code{&optional} when
355 you call the function.
356
357 The keyword @code{&rest} (which must be followed by a single argument
358 name) indicates that any number of arguments can follow. The single
359 following argument name will have a value, as a variable, which is a
360 list of all these remaining arguments. Do not write @code{&rest} when
361 you call the function.
362
363 Here is a description of an imaginary function @code{foo}:
364
365 @defun foo integer1 &optional integer2 &rest integers
366 The function @code{foo} subtracts @var{integer1} from @var{integer2},
367 then adds all the rest of the arguments to the result. If @var{integer2}
368 is not supplied, then the number 19 is used by default.
369
370 @example
371 (foo 1 5 3 9)
372 @result{} 16
373 (foo 5)
374 @result{} 14
375 @end example
376
377 @need 1500
378 More generally,
379
380 @example
381 (foo @var{w} @var{x} @var{y}@dots{})
382 @equiv{}
383 (+ (- @var{x} @var{w}) @var{y}@dots{})
384 @end example
385 @end defun
386
387 Any argument whose name contains the name of a type (e.g.,
388 @var{integer}, @var{integer1} or @var{buffer}) is expected to be of that
389 type. A plural of a type (such as @var{buffers}) often means a list of
390 objects of that type. Arguments named @var{object} may be of any type.
391 (@xref{Lisp Data Types}, for a list of Emacs object types.) Arguments
392 with other sorts of names (e.g., @var{new-file}) are discussed
393 specifically in the description of the function. In some sections,
394 features common to the arguments of several functions are described at
395 the beginning.
396
397 @xref{Lambda Expressions}, for a more complete description of optional
398 and rest arguments.
399
400 Command, macro, and special form descriptions have the same format,
401 but the word `Function' is replaced by `Command', `Macro', or `Special
402 Form', respectively. Commands are simply functions that may be called
403 interactively; macros process their arguments differently from functions
404 (the arguments are not evaluated), but are presented the same way.
405
406 Special form descriptions use a more complex notation to specify
407 optional and repeated arguments because they can break the argument
408 list down into separate arguments in more complicated ways.
409 @samp{@r{[}@var{optional-arg}@r{]}} means that @var{optional-arg} is
410 optional and @samp{@var{repeated-args}@dots{}} stands for zero or more
411 arguments. Parentheses are used when several arguments are grouped into
412 additional levels of list structure. Here is an example:
413
414 @defspec count-loop (@var{var} [@var{from} @var{to} [@var{inc}]]) @var{body}@dots{}
415 This imaginary special form implements a loop that executes the
416 @var{body} forms and then increments the variable @var{var} on each
417 iteration. On the first iteration, the variable has the value
418 @var{from}; on subsequent iterations, it is incremented by one (or by
419 @var{inc} if that is given). The loop exits before executing @var{body}
420 if @var{var} equals @var{to}. Here is an example:
421
422 @example
423 (count-loop (i 0 10)
424 (prin1 i) (princ " ")
425 (prin1 (aref vector i))
426 (terpri))
427 @end example
428
429 If @var{from} and @var{to} are omitted, @var{var} is bound to
430 @code{nil} before the loop begins, and the loop exits if @var{var} is
431 non-@code{nil} at the beginning of an iteration. Here is an example:
432
433 @example
434 (count-loop (done)
435 (if (pending)
436 (fixit)
437 (setq done t)))
438 @end example
439
440 In this special form, the arguments @var{from} and @var{to} are
441 optional, but must both be present or both absent. If they are present,
442 @var{inc} may optionally be specified as well. These arguments are
443 grouped with the argument @var{var} into a list, to distinguish them
444 from @var{body}, which includes all remaining elements of the form.
445 @end defspec
446
447 @node A Sample Variable Description
448 @subsubsection A Sample Variable Description
449 @cindex variable descriptions
450 @cindex option descriptions
451
452 A @dfn{variable} is a name that can hold a value. Although any
453 variable can be set by the user, certain variables that exist
454 specifically so that users can change them are called @dfn{user
455 options}. Ordinary variables and user options are described using a
456 format like that for functions except that there are no arguments.
457
458 Here is a description of the imaginary @code{electric-future-map}
459 variable.@refill
460
461 @defvar electric-future-map
462 The value of this variable is a full keymap used by Electric Command
463 Future mode. The functions in this map allow you to edit commands you
464 have not yet thought about executing.
465 @end defvar
466
467 User option descriptions have the same format, but `Variable' is
468 replaced by `User Option'.
469
470 @node Version Info
471 @section Version Information
472
473 These facilities provide information about which version of Emacs is
474 in use.
475
476 @deffn Command emacs-version
477 This function returns a string describing the version of Emacs that is
478 running. It is useful to include this string in bug reports.
479
480 @smallexample
481 @group
482 (emacs-version)
483 @result{} "GNU Emacs 20.3.5 (i486-pc-linux-gnulibc1, X toolkit)
484 of Sat Feb 14 1998 on psilocin.gnu.org"
485 @end group
486 @end smallexample
487
488 Called interactively, the function prints the same information in the
489 echo area.
490 @end deffn
491
492 @defvar emacs-build-time
493 The value of this variable indicates the time at which Emacs was built
494 at the local site. It is a list of three integers, like the value
495 of @code{current-time} (@pxref{Time of Day}).
496
497 @example
498 @group
499 emacs-build-time
500 @result{} (13623 62065 344633)
501 @end group
502 @end example
503 @end defvar
504
505 @defvar emacs-version
506 The value of this variable is the version of Emacs being run. It is a
507 string such as @code{"20.3.1"}. The last number in this string is not
508 really part of the Emacs release version number; it is incremented each
509 time you build Emacs in any given directory. A value with four numeric
510 components, such as @code{"20.3.9.1"}, indicates an unreleased test
511 version.
512 @end defvar
513
514 The following two variables have existed since Emacs version 19.23:
515
516 @defvar emacs-major-version
517 The major version number of Emacs, as an integer. For Emacs version
518 20.3, the value is 20.
519 @end defvar
520
521 @defvar emacs-minor-version
522 The minor version number of Emacs, as an integer. For Emacs version
523 20.3, the value is 3.
524 @end defvar
525
526 @node Acknowledgements
527 @section Acknowledgements
528
529 This manual was written by Robert Krawitz, Bil Lewis, Dan LaLiberte,
530 Richard M. Stallman and Chris Welty, the volunteers of the GNU manual
531 group, in an effort extending over several years. Robert J. Chassell
532 helped to review and edit the manual, with the support of the Defense
533 Advanced Research Projects Agency, ARPA Order 6082, arranged by Warren
534 A. Hunt, Jr.@: of Computational Logic, Inc.
535
536 Corrections were supplied by Karl Berry, Jim Blandy, Bard Bloom,
537 Stephane Boucher, David Boyes, Alan Carroll, Richard Davis, Lawrence
538 R. Dodd, Peter Doornbosch, David A. Duff, Chris Eich, Beverly
539 Erlebacher, David Eckelkamp, Ralf Fassel, Eirik Fuller, Stephen Gildea,
540 Bob Glickstein, Eric Hanchrow, George Hartzell, Nathan Hess, Masayuki
541 Ida, Dan Jacobson, Jak Kirman, Bob Knighten, Frederick M. Korz, Joe
542 Lammens, Glenn M. Lewis, K. Richard Magill, Brian Marick, Roland
543 McGrath, Skip Montanaro, John Gardiner Myers, Thomas A. Peterson,
544 Francesco Potorti, Friedrich Pukelsheim, Arnold D. Robbins, Raul
545 Rockwell, Per Starb@"ack, Shinichirou Sugou, Kimmo Suominen, Edward Tharp,
546 Bill Trost, Rickard Westman, Jean White, Matthew Wilding, Carl Witty,
547 Dale Worley, Rusty Wright, and David D. Zuhn.