*** empty log message ***
[emacs.git] / lispref / intro.texi
blobd21a95a7b174db827c4b1306d8431a671fe5d076
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994, 2002
4 @c   Free Software Foundation, Inc. 
5 @c See the file elisp.texi for copying conditions.
6 @setfilename ../info/intro
8 @node Introduction, Lisp Data Types, Top, Top
9 @comment  node-name,  next,  previous,  up
10 @chapter Introduction
12   Most of the GNU Emacs text editor is written in the programming
13 language called Emacs Lisp.  You can write new code in Emacs Lisp and
14 install it as an extension to the editor.  However, Emacs Lisp is more
15 than a mere ``extension language''; it is a full computer programming
16 language in its own right.  You can use it as you would any other
17 programming language.
19   Because Emacs Lisp is designed for use in an editor, it has special
20 features for scanning and parsing text as well as features for handling
21 files, buffers, displays, subprocesses, and so on.  Emacs Lisp is
22 closely integrated with the editing facilities; thus, editing commands
23 are functions that can also conveniently be called from Lisp programs,
24 and parameters for customization are ordinary Lisp variables.
26   This manual attempts to be a full description of Emacs Lisp.  For a
27 beginner's introduction to Emacs Lisp, see @cite{An Introduction to
28 Emacs Lisp Programming}, by Bob Chassell, also published by the Free
29 Software Foundation.  This manual presumes considerable familiarity with
30 the use of Emacs for editing; see @cite{The GNU Emacs Manual} for this
31 basic information.
33   Generally speaking, the earlier chapters describe features of Emacs
34 Lisp that have counterparts in many programming languages, and later
35 chapters describe features that are peculiar to Emacs Lisp or relate
36 specifically to editing.
38   This is edition 2.8.
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
48 @node Caveats
49 @section Caveats
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.  This includes most information about usage on VMS.
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.
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
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
85 Please mail comments and corrections to
87 @example
88 bug-lisp-manual@@gnu.org
89 @end example
91 @noindent
92 We let mail to this list accumulate unread until someone decides to
93 apply the corrections.  Months, and sometimes years, go by between
94 updates.  So please attach no significance to the lack of a reply---your
95 mail @emph{will} be acted on in due time.  If you want to contact the
96 Emacs maintainers more quickly, send mail to
97 @code{bug-gnu-emacs@@gnu.org}.
99 @node Lisp History
100 @section Lisp History
101 @cindex Lisp history
103   Lisp (LISt Processing language) was first developed in the late 1950s
104 at the Massachusetts Institute of Technology for research in artificial
105 intelligence.  The great power of the Lisp language makes it ideal
106 for other purposes as well, such as writing editing commands.
108 @cindex Maclisp
109 @cindex Common Lisp
110   Dozens of Lisp implementations have been built over the years, each
111 with its own idiosyncrasies.  Many of them were inspired by Maclisp,
112 which was written in the 1960s at MIT's Project MAC.  Eventually the
113 implementors of the descendants of Maclisp came together and developed a
114 standard for Lisp systems, called Common Lisp.  In the meantime, Gerry
115 Sussman and Guy Steele at MIT developed a simplified but very powerful
116 dialect of Lisp, called Scheme.
118   GNU Emacs Lisp is largely inspired by Maclisp, and a little by Common
119 Lisp.  If you know Common Lisp, you will notice many similarities.
120 However, many features of Common Lisp have been omitted or
121 simplified in order to reduce the memory requirements of GNU Emacs.
122 Sometimes the simplifications are so drastic that a Common Lisp user
123 might be very confused.  We will occasionally point out how GNU Emacs
124 Lisp differs from Common Lisp.  If you don't know Common Lisp, don't
125 worry about it; this manual is self-contained.
127 @pindex cl
128   A certain amount of Common Lisp emulation is available via the
129 @file{cl} library.  @xref{Top,, Common Lisp Extension, cl, Common Lisp
130 Extensions}.
132   Emacs Lisp is not at all influenced by Scheme; but the GNU project has
133 an implementation of Scheme, called Guile.  We use Guile in all new GNU
134 software that calls for extensibility.
136 @node Conventions
137 @section Conventions
139 This section explains the notational conventions that are used in this
140 manual.  You may want to skip this section and refer back to it later.
142 @menu
143 * Some Terms::               Explanation of terms we use in this manual.
144 * nil and t::                How the symbols @code{nil} and @code{t} are used.
145 * Evaluation Notation::      The format we use for examples of evaluation.
146 * Printing Notation::        The format we use when examples print text.
147 * Error Messages::           The format we use for examples of errors.
148 * Buffer Text Notation::     The format we use for buffer contents in examples.
149 * Format of Descriptions::   Notation for describing functions, variables, etc.
150 @end menu
152 @node Some Terms
153 @subsection Some Terms
155   Throughout this manual, the phrases ``the Lisp reader'' and ``the Lisp
156 printer'' refer to those routines in Lisp that convert textual
157 representations of Lisp objects into actual Lisp objects, and vice
158 versa.  @xref{Printed Representation}, for more details.  You, the
159 person reading this manual, are thought of as ``the programmer'' and are
160 addressed as ``you''.  ``The user'' is the person who uses Lisp
161 programs, including those you write.
163 @cindex fonts
164   Examples of Lisp code are formatted like this: @code{(list 1 2 3)}.
165 Names that represent metasyntactic variables, or arguments to a function
166 being described, are formatted like this: @var{first-number}.
168 @node nil and t
169 @subsection @code{nil} and @code{t}
170 @cindex @code{nil}, uses of
171 @cindex truth value
172 @cindex boolean
173 @cindex false
175   In Lisp, the symbol @code{nil} has three separate meanings: it
176 is a symbol with the name @samp{nil}; it is the logical truth value
177 @var{false}; and it is the empty list---the list of zero elements.
178 When used as a variable, @code{nil} always has the value @code{nil}.
180   As far as the Lisp reader is concerned, @samp{()} and @samp{nil} are
181 identical: they stand for the same object, the symbol @code{nil}.  The
182 different ways of writing the symbol are intended entirely for human
183 readers.  After the Lisp reader has read either @samp{()} or @samp{nil},
184 there is no way to determine which representation was actually written
185 by the programmer.
187   In this manual, we use @code{()} when we wish to emphasize that it
188 means the empty list, and we use @code{nil} when we wish to emphasize
189 that it means the truth value @var{false}.  That is a good convention to use
190 in Lisp programs also.
192 @example
193 (cons 'foo ())                ; @r{Emphasize the empty list}
194 (not nil)                     ; @r{Emphasize the truth value @var{false}}
195 @end example
197 @cindex @code{t} and truth
198 @cindex true
199   In contexts where a truth value is expected, any non-@code{nil} value
200 is considered to be @var{true}.  However, @code{t} is the preferred way
201 to represent the truth value @var{true}.  When you need to choose a
202 value which represents @var{true}, and there is no other basis for
203 choosing, use @code{t}.  The symbol @code{t} always has the value
204 @code{t}.
206   In Emacs Lisp, @code{nil} and @code{t} are special symbols that always
207 evaluate to themselves.  This is so that you do not need to quote them
208 to use them as constants in a program.  An attempt to change their
209 values results in a @code{setting-constant} error.  The same is true of
210 any symbol whose name starts with a colon (@samp{:}).  @xref{Constant
211 Variables}.
213 @node Evaluation Notation
214 @subsection Evaluation Notation
215 @cindex evaluation notation
216 @cindex documentation notation
218   A Lisp expression that you can evaluate is called a @dfn{form}.
219 Evaluating a form always produces a result, which is a Lisp object.  In
220 the examples in this manual, this is indicated with @samp{@result{}}:
222 @example
223 (car '(1 2))
224      @result{} 1
225 @end example
227 @noindent
228 You can read this as ``@code{(car '(1 2))} evaluates to 1''.
230   When a form is a macro call, it expands into a new form for Lisp to
231 evaluate.  We show the result of the expansion with
232 @samp{@expansion{}}.  We may or may not show the result of the
233 evaluation of the expanded form.
235 @example
236 (third '(a b c))
237      @expansion{} (car (cdr (cdr '(a b c))))
238      @result{} c
239 @end example
241   Sometimes to help describe one form we show another form that
242 produces identical results.  The exact equivalence of two forms is
243 indicated with @samp{@equiv{}}.
245 @example
246 (make-sparse-keymap) @equiv{} (list 'keymap)
247 @end example
249 @node Printing Notation
250 @subsection Printing Notation
251 @cindex printing notation
253   Many of the examples in this manual print text when they are
254 evaluated.  If you execute example code in a Lisp Interaction buffer
255 (such as the buffer @samp{*scratch*}), the printed text is inserted into
256 the buffer.  If you execute the example by other means (such as by
257 evaluating the function @code{eval-region}), the printed text is
258 displayed in the echo area.
260   Examples in this manual indicate printed text with @samp{@print{}},
261 irrespective of where that text goes.  The value returned by evaluating
262 the form (here @code{bar}) follows on a separate line.
264 @example
265 @group
266 (progn (print 'foo) (print 'bar))
267      @print{} foo
268      @print{} bar
269      @result{} bar
270 @end group
271 @end example
273 @node Error Messages
274 @subsection Error Messages
275 @cindex error message notation
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.
282 @example
283 (+ 23 'x)
284 @error{} Wrong type argument: number-or-marker-p, x
285 @end example
287 @node Buffer Text Notation
288 @subsection Buffer Text Notation
289 @cindex buffer text notation
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.)
299 @example
300 ---------- Buffer: foo ----------
301 This is the @point{}contents of foo.
302 ---------- Buffer: foo ----------
304 (insert "changed ")
305      @result{} nil
306 ---------- Buffer: foo ----------
307 This is the changed @point{}contents of foo.
308 ---------- Buffer: foo ----------
309 @end example
311 @node Format of Descriptions
312 @subsection Format of Descriptions
313 @cindex description format
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.
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
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
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.
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.
354   The keyword @code{&rest} (which must be followed by a single argument
355 name) indicates that any number of arguments can follow.  The single
356 following argument name will have a value, as a variable, which is a
357 list of all these remaining arguments.  Do not write @code{&rest} when
358 you call the function.
360   Here is a description of an imaginary function @code{foo}:
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.
367 @example
368 (foo 1 5 3 9)
369      @result{} 16
370 (foo 5)
371      @result{} 14
372 @end example
374 @need 1500
375 More generally,
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
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.
394   @xref{Lambda Expressions}, for a more complete description of optional
395 and rest arguments.
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.
403   Special form descriptions use a more complex notation to specify
404 optional and repeated arguments because they can break the argument
405 list down into separate arguments in more complicated ways.
406 @samp{@r{[}@var{optional-arg}@r{]}} means that @var{optional-arg} is
407 optional and @samp{@var{repeated-args}@dots{}} stands for zero or more
408 arguments.  Parentheses are used when several arguments are grouped into
409 additional levels of list structure.  Here is an example:
411 @defspec count-loop (@var{var} [@var{from} @var{to} [@var{inc}]]) @var{body}@dots{}
412 This imaginary special form implements a loop that executes the
413 @var{body} forms and then increments the variable @var{var} on each
414 iteration.  On the first iteration, the variable has the value
415 @var{from}; on subsequent iterations, it is incremented by one (or by
416 @var{inc} if that is given).  The loop exits before executing @var{body}
417 if @var{var} equals @var{to}.  Here is an example:
419 @example
420 (count-loop (i 0 10)
421   (prin1 i) (princ " ")
422   (prin1 (aref vector i))
423   (terpri))
424 @end example
426 If @var{from} and @var{to} are omitted, @var{var} is bound to
427 @code{nil} before the loop begins, and the loop exits if @var{var} is
428 non-@code{nil} at the beginning of an iteration.  Here is an example:
430 @example
431 (count-loop (done)
432   (if (pending)
433       (fixit)
434     (setq done t)))
435 @end example
437 In this special form, the arguments @var{from} and @var{to} are
438 optional, but must both be present or both absent.  If they are present,
439 @var{inc} may optionally be specified as well.  These arguments are
440 grouped with the argument @var{var} into a list, to distinguish them
441 from @var{body}, which includes all remaining elements of the form.
442 @end defspec
444 @node A Sample Variable Description
445 @subsubsection A Sample Variable Description
446 @cindex variable descriptions
447 @cindex option descriptions
449   A @dfn{variable} is a name that can hold a value.  Although any
450 variable can be set by the user, certain variables that exist
451 specifically so that users can change them are called @dfn{user
452 options}.  Ordinary variables and user options are described using a
453 format like that for functions except that there are no arguments.
455   Here is a description of the imaginary @code{electric-future-map}
456 variable.@refill
458 @defvar electric-future-map
459 The value of this variable is a full keymap used by Electric Command
460 Future mode.  The functions in this map allow you to edit commands you
461 have not yet thought about executing.
462 @end defvar
464   User option descriptions have the same format, but `Variable' is
465 replaced by `User Option'.
467 @node Version Info
468 @section Version Information
470   These facilities provide information about which version of Emacs is
471 in use.
473 @deffn Command emacs-version
474 This function returns a string describing the version of Emacs that is
475 running.  It is useful to include this string in bug reports.
477 @smallexample
478 @group
479 (emacs-version)
480   @result{} "GNU Emacs 20.3.5 (i486-pc-linux-gnulibc1, X toolkit)
481  of Sat Feb 14 1998 on psilocin.gnu.org"
482 @end group
483 @end smallexample
485 Called interactively, the function prints the same information in the
486 echo area.
487 @end deffn
489 @defvar emacs-build-time
490 The value of this variable indicates the time at which Emacs was built
491 at the local site.  It is a list of three integers, like the value
492 of @code{current-time} (@pxref{Time of Day}).
494 @example
495 @group
496 emacs-build-time
497      @result{} (13623 62065 344633)
498 @end group
499 @end example
500 @end defvar
502 @defvar emacs-version
503 The value of this variable is the version of Emacs being run.  It is a
504 string such as @code{"20.3.1"}.  The last number in this string is not
505 really part of the Emacs release version number; it is incremented each
506 time you build Emacs in any given directory.  A value with four numeric
507 components, such as @code{"20.3.9.1"}, indicates an unreleased test
508 version.
509 @end defvar
511   The following two variables have existed since Emacs version 19.23:
513 @defvar emacs-major-version
514 The major version number of Emacs, as an integer.  For Emacs version
515 20.3, the value is 20.
516 @end defvar
518 @defvar emacs-minor-version
519 The minor version number of Emacs, as an integer.  For Emacs version
520 20.3, the value is 3.
521 @end defvar
523 @node Acknowledgements
524 @section Acknowledgements
526   This manual was written by Robert Krawitz, Bil Lewis, Dan LaLiberte,
527 Richard M. Stallman and Chris Welty, the volunteers of the GNU manual
528 group, in an effort extending over several years.  Robert J. Chassell
529 helped to review and edit the manual, with the support of the Defense
530 Advanced Research Projects Agency, ARPA Order 6082, arranged by Warren
531 A. Hunt, Jr.@: of Computational Logic, Inc.
533   Corrections were supplied by Karl Berry, Jim Blandy, Bard Bloom,
534 Stephane Boucher, David Boyes, Alan Carroll, Richard Davis, Lawrence
535 R. Dodd, Peter Doornbosch, David A. Duff, Chris Eich, Beverly
536 Erlebacher, David Eckelkamp, Ralf Fassel, Eirik Fuller, Stephen Gildea,
537 Bob Glickstein, Eric Hanchrow, George Hartzell, Nathan Hess, Masayuki
538 Ida, Dan Jacobson, Jak Kirman, Bob Knighten, Frederick M. Korz, Joe
539 Lammens, Glenn M. Lewis, K. Richard Magill, Brian Marick, Roland
540 McGrath, Skip Montanaro, John Gardiner Myers, Thomas A. Peterson,
541 Francesco Potorti, Friedrich Pukelsheim, Arnold D. Robbins, Raul
542 Rockwell, Per Starb@"ack, Shinichirou Sugou, Kimmo Suominen, Edward Tharp,
543 Bill Trost, Rickard Westman, Jean White, Matthew Wilding, Carl Witty,
544 Dale Worley, Rusty Wright, and David D. Zuhn.