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