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-2011 Free Software Foundation, Inc.
4 @c See the file elisp.texi for copying conditions.
5 @setfilename ../../info/intro
7 @node Introduction, Lisp Data Types, Top, Top
8 @comment node-name, next, previous, up
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
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.
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
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.
37 This is edition @value{VERSION} of the GNU Emacs Lisp Reference
38 Manual, corresponding to Emacs version @value{EMACSVER}.
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.
50 @cindex bugs in this manual
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
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.
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.
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
88 Please send comments and corrections using @kbd{M-x report-emacs-bug}.
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.
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 implementors 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.
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.
119 A certain amount of Common Lisp emulation is available via the
120 @file{cl} library. @inforef{Top, Overview, cl}.
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.
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.
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.
143 @subsection Some Terms
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.
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}.
159 @subsection @code{nil} and @code{t}
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}.
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
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.
183 (cons 'foo ()) ; @r{Emphasize the empty list}
184 (setq foo-flag nil) ; @r{Emphasize the truth value @var{false}}
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
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
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}.
207 @node Evaluation Notation
208 @subsection Evaluation Notation
209 @cindex evaluation notation
210 @cindex documentation notation
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{}}:
223 You can read this as ``@code{(car '(1 2))} evaluates to 1.''
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.
232 @expansion{} (car (cdr (cdr '(a b c))))
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{}}.
241 (make-sparse-keymap) @equiv{} (list 'keymap)
244 @node Printing Notation
245 @subsection Printing Notation
246 @cindex printing notation
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.
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
262 (progn (prin1 'foo) (princ "\n") (prin1 'bar))
270 @subsection Error Messages
271 @cindex error message notation
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
280 @error{} Wrong type argument: number-or-marker-p, x
283 @node Buffer Text Notation
284 @subsection Buffer Text Notation
285 @cindex buffer text notation
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.)
296 ---------- Buffer: foo ----------
297 This is the @point{}contents of foo.
298 ---------- Buffer: foo ----------
302 ---------- Buffer: foo ----------
303 This is the changed @point{}contents of foo.
304 ---------- Buffer: foo ----------
307 @node Format of Descriptions
308 @subsection Format of Descriptions
309 @cindex description format
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
316 The category---function, variable, or whatever---appears at the
317 beginning of the line.
320 The category---function, variable, or whatever---is printed next to the
323 The description follows on succeeding lines, sometimes with examples.
326 * A Sample Function Description:: A description of an imaginary
327 function, @code{foo}.
328 * A Sample Variable Description:: A description of an imaginary
330 @code{electric-future-map}.
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
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.
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.
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.
356 Here is a description of an imaginary function @code{foo}:
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.
374 (foo @var{w} @var{x} @var{y}@dots{})
376 (+ (- @var{x} @var{w}) @var{y}@dots{})
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
390 @xref{Lambda Expressions}, for a more complete description of optional
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.
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:
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:
417 (prin1 i) (princ " ")
418 (prin1 (aref vector i))
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:
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.
440 @node A Sample Variable Description
441 @subsubsection A Sample Variable Description
442 @cindex variable descriptions
443 @cindex option descriptions
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.
451 Here is a description of the imaginary @code{electric-future-map}
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.
460 User option descriptions have the same format, but `Variable' is
461 replaced by `User Option'.
464 @section Version Information
466 These facilities provide information about which version of Emacs is
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.
476 @result{} "GNU Emacs 23.1 (i686-pc-linux-gnu, GTK+ Version 2.14.4)
477 of 2009-06-01 on cyd.mit.edu"
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}.
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}).
495 @result{} (18846 52016 156039)
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.
509 The following two variables have existed since Emacs version 19.23:
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.
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.
521 @node Acknowledgements
522 @section Acknowledgements
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.
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.