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