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