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