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