Convert consecutive FSF copyright years to ranges.
[bpt/emacs.git] / doc / lispref / intro.texi
CommitLineData
b8d4c8d0
GM
1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
73b0cd50 3@c Copyright (C) 1990-1994, 2001-2011 Free Software Foundation, Inc.
b8d4c8d0 4@c See the file elisp.texi for copying conditions.
6336d8c3 5@setfilename ../../info/intro
b8d4c8d0
GM
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
12language called Emacs Lisp. You can write new code in Emacs Lisp and
13install it as an extension to the editor. However, Emacs Lisp is more
14than a mere ``extension language''; it is a full computer programming
15language in its own right. You can use it as you would any other
16programming language.
17
18 Because Emacs Lisp is designed for use in an editor, it has special
19features for scanning and parsing text as well as features for handling
20files, buffers, displays, subprocesses, and so on. Emacs Lisp is
21closely integrated with the editing facilities; thus, editing commands
22are functions that can also conveniently be called from Lisp programs,
23and parameters for customization are ordinary Lisp variables.
24
25 This manual attempts to be a full description of Emacs Lisp. For a
26beginner's introduction to Emacs Lisp, see @cite{An Introduction to
27Emacs Lisp Programming}, by Bob Chassell, also published by the Free
28Software Foundation. This manual presumes considerable familiarity with
29the use of Emacs for editing; see @cite{The GNU Emacs Manual} for this
30basic information.
31
32 Generally speaking, the earlier chapters describe features of Emacs
33Lisp that have counterparts in many programming languages, and later
34chapters describe features that are peculiar to Emacs Lisp or relate
35specifically to editing.
36
37 This is edition @value{VERSION} of the GNU Emacs Lisp Reference
38Manual, 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
53but not flawless. There are a few topics that are not covered, either
54because we consider them secondary (such as most of the individual
55modes) or because they are yet to be written. Because we are not able
56to deal with them completely, we have left out several parts
7c2fb837 57intentionally.
b8d4c8d0
GM
58
59 The manual should be fully correct in what it does cover, and it is
60therefore open to criticism on anything it says---from specific examples
61and descriptive text, to the ordering of chapters and sections. If
62something is confusing, or you find that you have to look at the sources
63or experiment to learn something not covered in the manual, then perhaps
64the 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
68you can later look them up and send them to us. If you think of a simple,
69real-life example for a function or group of functions, please make an
70effort to write it up and send it in. Please reference any comments to
71the chapter name, section name, and function name, as appropriate, since
72page numbers and chapter and section numbers will change and we may have
73trouble finding the text you are talking about. Also state the number
74of the edition you are criticizing.
75@end iftex
76@ifnottex
77
78As you use this manual, we ask that you send corrections as soon as you
79find them. If you think of a simple, real life example for a function
80or group of functions, please make an effort to write it up and send it
81in. Please reference any comments to the node name and function or
82variable name, as appropriate. Also state the number of the edition
83you are criticizing.
84@end ifnottex
85
86@cindex bugs
87@cindex suggestions
d794edf2 88Please send comments and corrections using @kbd{M-x report-emacs-bug}.
b8d4c8d0
GM
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
95at the Massachusetts Institute of Technology for research in artificial
96intelligence. The great power of the Lisp language makes it ideal
97for 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
102with its own idiosyncrasies. Many of them were inspired by Maclisp,
103which was written in the 1960s at MIT's Project MAC. Eventually the
104implementors of the descendants of Maclisp came together and developed a
105standard for Lisp systems, called Common Lisp. In the meantime, Gerry
106Sussman and Guy Steele at MIT developed a simplified but very powerful
107dialect of Lisp, called Scheme.
108
109 GNU Emacs Lisp is largely inspired by Maclisp, and a little by Common
110Lisp. If you know Common Lisp, you will notice many similarities.
111However, many features of Common Lisp have been omitted or
112simplified in order to reduce the memory requirements of GNU Emacs.
113Sometimes the simplifications are so drastic that a Common Lisp user
114might be very confused. We will occasionally point out how GNU Emacs
115Lisp differs from Common Lisp. If you don't know Common Lisp, don't
116worry 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
123an implementation of Scheme, called Guile. We use Guile in all new GNU
124software that calls for extensibility.
125
126@node Conventions
127@section Conventions
128
129This section explains the notational conventions that are used in this
130manual. 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
146printer'' refer to those routines in Lisp that convert textual
147representations of Lisp objects into actual Lisp objects, and vice
148versa. @xref{Printed Representation}, for more details. You, the
149person reading this manual, are thought of as ``the programmer'' and are
150addressed as ``you.'' ``The user'' is the person who uses Lisp
151programs, including those you write.
152
b731690e 153@cindex typographic conventions
b8d4c8d0
GM
154 Examples of Lisp code are formatted like this: @code{(list 1 2 3)}.
155Names that represent metasyntactic variables, or arguments to a function
156being 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
166is 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.
168When 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
171identical: they stand for the same object, the symbol @code{nil}. The
172different ways of writing the symbol are intended entirely for human
173readers. After the Lisp reader has read either @samp{()} or @samp{nil},
174there is no way to determine which representation was actually written
175by the programmer.
176
177 In this manual, we write @code{()} when we wish to emphasize that it
178means the empty list, and we write @code{nil} when we wish to emphasize
179that it means the truth value @var{false}. That is a good convention to use
180in 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
190is considered to be @var{true}. However, @code{t} is the preferred way
191to represent the truth value @var{true}. When you need to choose a
192value which represents @var{true}, and there is no other basis for
193choosing, 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
197evaluate to themselves. This is so that you do not need to quote them
198to use them as constants in a program. An attempt to change their
199values results in a @code{setting-constant} error. @xref{Constant
200Variables}.
201
202@defun booleanp object
762ed4ad
CY
203Return non-@code{nil} if @var{object} is one of the two canonical
204boolean values: @code{t} or @code{nil}.
b8d4c8d0
GM
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}.
214Evaluating a form always produces a result, which is a Lisp object. In
215the 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
223You 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
226evaluate. We show the result of the expansion with
227@samp{@expansion{}}. We may or may not show the result of the
228evaluation 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
237produces identical results. The exact equivalence of two forms is
238indicated 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
249evaluated. If you execute example code in a Lisp Interaction buffer
250(such as the buffer @samp{*scratch*}), the printed text is inserted into
251the buffer. If you execute the example by other means (such as by
252evaluating the function @code{eval-region}), the printed text is
253displayed in the echo area.
254
255 Examples in this manual indicate printed text with @samp{@print{}},
256irrespective of where that text goes. The value returned by
257evaluating 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
274in 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
276the 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
288showing the ``before'' and ``after'' versions of the text. These
289examples show the contents of the buffer in question between two lines
290of dashes containing the buffer name. In addition, @samp{@point{}}
291indicates the location of point. (The symbol for point, of course, is
292not 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 ----------
297This is the @point{}contents of foo.
298---------- Buffer: foo ----------
299
300(insert "changed ")
301 @result{} nil
302---------- Buffer: foo ----------
303This 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
312forms are described in this manual in a uniform format. The first
313line of a description contains the name of the item followed by its
314arguments, if any.
315@ifnottex
316The category---function, variable, or whatever---appears at the
317beginning of the line.
318@end ifnottex
319@iftex
320The category---function, variable, or whatever---is printed next to the
321right margin.
322@end iftex
323The 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
341appears first. It is followed on the same line by a list of argument
342names. These names are also used in the body of the description, to
343stand for the values of the arguments.
344
345 The appearance of the keyword @code{&optional} in the argument list
346indicates that the subsequent arguments may be omitted (omitted
347arguments default to @code{nil}). Do not write @code{&optional} when
348you call the function.
349
350 The keyword @code{&rest} (which must be followed by a single
351argument name) indicates that any number of arguments can follow. The
352single argument name following @code{&rest} will receive, as its
353value, a list of all the remaining arguments passed to the function.
354Do 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
359The function @code{foo} subtracts @var{integer1} from @var{integer2},
360then adds all the rest of the arguments to the result. If @var{integer2}
361is 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
371More 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
382type. A plural of a type (such as @var{buffers}) often means a list of
383objects 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
385with other sorts of names (e.g., @var{new-file}) are discussed
386specifically in the description of the function. In some sections,
387features common to the arguments of several functions are described at
388the beginning.
389
390 @xref{Lambda Expressions}, for a more complete description of optional
391and rest arguments.
392
393 Command, macro, and special form descriptions have the same format,
394but the word `Function' is replaced by `Command', `Macro', or `Special
395Form', respectively. Commands are simply functions that may be called
396interactively; 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
400optional and repeated arguments because they can break the argument
401list down into separate arguments in more complicated ways.
402@samp{@r{[}@var{optional-arg}@r{]}} means that @var{optional-arg} is
403optional and @samp{@var{repeated-args}@dots{}} stands for zero or more
404arguments. Parentheses are used when several arguments are grouped into
405additional levels of list structure. Here is an example:
406
407@defspec count-loop (@var{var} [@var{from} @var{to} [@var{inc}]]) @var{body}@dots{}
408This imaginary special form implements a loop that executes the
409@var{body} forms and then increments the variable @var{var} on each
410iteration. 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}
413if @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
422If @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
424non-@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
433In this special form, the arguments @var{from} and @var{to} are
434optional, but must both be present or both absent. If they are present,
435@var{inc} may optionally be specified as well. These arguments are
436grouped with the argument @var{var} into a list, to distinguish them
437from @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
446all variables can be set by the user, certain variables exist
447specifically so that users can change them; these are called @dfn{user
448options}. Ordinary variables and user options are described using a
449format like that for functions except that there are no arguments.
450
451 Here is a description of the imaginary @code{electric-future-map}
452variable.@refill
453
454@defvar electric-future-map
455The value of this variable is a full keymap used by Electric Command
456Future mode. The functions in this map allow you to edit commands you
457have not yet thought about executing.
458@end defvar
459
460 User option descriptions have the same format, but `Variable' is
461replaced by `User Option'.
462
463@node Version Info
464@section Version Information
465
466 These facilities provide information about which version of Emacs is
467in use.
468
469@deffn Command emacs-version &optional here
470This function returns a string describing the version of Emacs that is
471running. It is useful to include this string in bug reports.
472
473@smallexample
474@group
475(emacs-version)
43d02cfd
CY
476 @result{} "GNU Emacs 23.1 (i686-pc-linux-gnu, GTK+ Version 2.14.4)
477 of 2009-06-01 on cyd.mit.edu"
b8d4c8d0
GM
478@end group
479@end smallexample
480
481If @var{here} is non-@code{nil}, it inserts the text in the buffer
43d02cfd
CY
482before point, and returns @code{nil}. When this function is called
483interactively, it prints the same information in the echo area, but
484giving a prefix argument makes @var{here} non-@code{nil}.
b8d4c8d0
GM
485@end deffn
486
487@defvar emacs-build-time
488The value of this variable indicates the time at which Emacs was built
489at the local site. It is a list of three integers, like the value
490of @code{current-time} (@pxref{Time of Day}).
491
492@example
493@group
494emacs-build-time
43d02cfd 495 @result{} (18846 52016 156039)
b8d4c8d0
GM
496@end group
497@end example
498@end defvar
499
500@defvar emacs-version
501The value of this variable is the version of Emacs being run. It is a
43d02cfd
CY
502string such as @code{"23.1.1"}. The last number in this string is not
503really part of the Emacs release version number; it is incremented
504each time you build Emacs in any given directory. A value with four
505numeric components, such as @code{"22.0.91.1"}, indicates an
506unreleased test version.
b8d4c8d0
GM
507@end defvar
508
509 The following two variables have existed since Emacs version 19.23:
510
511@defvar emacs-major-version
512The major version number of Emacs, as an integer. For Emacs version
43d02cfd 51323.1, the value is 23.
b8d4c8d0
GM
514@end defvar
515
516@defvar emacs-minor-version
517The minor version number of Emacs, as an integer. For Emacs version
43d02cfd 51823.1, the value is 1.
b8d4c8d0
GM
519@end defvar
520
521@node Acknowledgements
522@section Acknowledgements
523
524 This manual was written by Robert Krawitz, Bil Lewis, Dan LaLiberte,
525Richard@tie{}M. Stallman and Chris Welty, the volunteers of the GNU
526manual group, in an effort extending over several years.
527Robert@tie{}J. Chassell helped to review and edit the manual, with the
528support of the Defense Advanced Research Projects Agency, ARPA Order
5296082, arranged by Warren@tie{}A. Hunt, Jr.@: of Computational Logic,
43d02cfd
CY
530Inc. Additional sections were written by Miles Bader, Lars Brinkhoff,
531Chong Yidong, Kenichi Handa, Lute Kamstra, Juri Linkov, Glenn Morris,
532Thien-Thi Nguyen, Dan Nicolaescu, Martin Rudalics, Kim F. Storm, Luc
533Teirlinck, and Eli Zaretskii.
534
535 Corrections were supplied by Drew Adams, Juanma Barranquero, Karl
536Berry, Jim Blandy, Bard Bloom, Stephane Boucher, David Boyes, Alan
537Carroll, Richard Davis, Lawrence R. Dodd, Peter Doornbosch, David
538A. Duff, Chris Eich, Beverly Erlebacher, David Eckelkamp, Ralf Fassel,
539Eirik Fuller, Stephen Gildea, Bob Glickstein, Eric Hanchrow, Jesper
540Harder, George Hartzell, Nathan Hess, Masayuki Ida, Dan Jacobson, Jak
541Kirman, Bob Knighten, Frederick M. Korz, Joe Lammens, Glenn M. Lewis,
542K. Richard Magill, Brian Marick, Roland McGrath, Stefan Monnier, Skip
543Montanaro, John Gardiner Myers, Thomas A. Peterson, Francesco Potorti,
544Friedrich Pukelsheim, Arnold D. Robbins, Raul Rockwell, Jason Rumney,
545Per Starb@"ack, Shinichirou Sugou, Kimmo Suominen, Edward Tharp, Bill
546Trost, Rickard Westman, Jean White, Eduard Wiebe, Matthew Wilding,
547Carl Witty, Dale Worley, Rusty Wright, and David D. Zuhn.