780fd807c1d9b79c76371bdcb1292cb9f2a71b91
[chise/xemacs-chise.git.1] / info / lispref.info-7
1 This is Info file ../../info/lispref.info, produced by Makeinfo version
2 1.68 from the input file lispref.texi.
3
4 INFO-DIR-SECTION XEmacs Editor
5 START-INFO-DIR-ENTRY
6 * Lispref: (lispref).           XEmacs Lisp Reference Manual.
7 END-INFO-DIR-ENTRY
8
9    Edition History:
10
11    GNU Emacs Lisp Reference Manual Second Edition (v2.01), May 1993 GNU
12 Emacs Lisp Reference Manual Further Revised (v2.02), August 1993 Lucid
13 Emacs Lisp Reference Manual (for 19.10) First Edition, March 1994
14 XEmacs Lisp Programmer's Manual (for 19.12) Second Edition, April 1995
15 GNU Emacs Lisp Reference Manual v2.4, June 1995 XEmacs Lisp
16 Programmer's Manual (for 19.13) Third Edition, July 1995 XEmacs Lisp
17 Reference Manual (for 19.14 and 20.0) v3.1, March 1996 XEmacs Lisp
18 Reference Manual (for 19.15 and 20.1, 20.2, 20.3) v3.2, April, May,
19 November 1997 XEmacs Lisp Reference Manual (for 21.0) v3.3, April 1998
20
21    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995 Free Software
22 Foundation, Inc.  Copyright (C) 1994, 1995 Sun Microsystems, Inc.
23 Copyright (C) 1995, 1996 Ben Wing.
24
25    Permission is granted to make and distribute verbatim copies of this
26 manual provided the copyright notice and this permission notice are
27 preserved on all copies.
28
29    Permission is granted to copy and distribute modified versions of
30 this manual under the conditions for verbatim copying, provided that the
31 entire resulting derived work is distributed under the terms of a
32 permission notice identical to this one.
33
34    Permission is granted to copy and distribute translations of this
35 manual into another language, under the above conditions for modified
36 versions, except that this permission notice may be stated in a
37 translation approved by the Foundation.
38
39    Permission is granted to copy and distribute modified versions of
40 this manual under the conditions for verbatim copying, provided also
41 that the section entitled "GNU General Public License" is included
42 exactly as in the original, and provided that the entire resulting
43 derived work is distributed under the terms of a permission notice
44 identical to this one.
45
46    Permission is granted to copy and distribute translations of this
47 manual into another language, under the above conditions for modified
48 versions, except that the section entitled "GNU General Public License"
49 may be included in a translation approved by the Free Software
50 Foundation instead of in the original English.
51
52 \1f
53 File: lispref.info,  Node: Sequence Functions,  Next: Arrays,  Up: Sequences Arrays Vectors
54
55 Sequences
56 =========
57
58    In XEmacs Lisp, a "sequence" is either a list, a vector, a bit
59 vector, or a string.  The common property that all sequences have is
60 that each is an ordered collection of elements.  This section describes
61 functions that accept any kind of sequence.
62
63  - Function: sequencep OBJECT
64      Returns `t' if OBJECT is a list, vector, bit vector, or string,
65      `nil' otherwise.
66
67  - Function: copy-sequence SEQUENCE
68      Returns a copy of SEQUENCE.  The copy is the same type of object
69      as the original sequence, and it has the same elements in the same
70      order.
71
72      Storing a new element into the copy does not affect the original
73      SEQUENCE, and vice versa.  However, the elements of the new
74      sequence are not copies; they are identical (`eq') to the elements
75      of the original.  Therefore, changes made within these elements, as
76      found via the copied sequence, are also visible in the original
77      sequence.
78
79      If the sequence is a string with extents or text properties, the
80      extents and text properties in the copy are also copied, not
81      shared with the original. (This means that modifying the extents
82      or text properties of the original will not affect the copy.)
83      However, the actual values of the properties are shared.  *Note
84      Extents::, *Note Text Properties::.
85
86      See also `append' in *Note Building Lists::, `concat' in *Note
87      Creating Strings::, `vconcat' in *Note Vectors::, and `bvconcat'
88      in *Note Bit Vectors::, for other ways to copy sequences.
89
90           (setq bar '(1 2))
91                => (1 2)
92           (setq x (vector 'foo bar))
93                => [foo (1 2)]
94           (setq y (copy-sequence x))
95                => [foo (1 2)]
96           
97           (eq x y)
98                => nil
99           (equal x y)
100                => t
101           (eq (elt x 1) (elt y 1))
102                => t
103           
104           ;; Replacing an element of one sequence.
105           (aset x 0 'quux)
106           x => [quux (1 2)]
107           y => [foo (1 2)]
108           
109           ;; Modifying the inside of a shared element.
110           (setcar (aref x 1) 69)
111           x => [quux (69 2)]
112           y => [foo (69 2)]
113           
114           ;; Creating a bit vector.
115           (bit-vector 1 0 1 1 0 1 0 0)
116                => #*10110100
117
118  - Function: length SEQUENCE
119      Returns the number of elements in SEQUENCE.  If SEQUENCE is a cons
120      cell that is not a list (because the final CDR is not `nil'), a
121      `wrong-type-argument' error is signaled.
122
123           (length '(1 2 3))
124               => 3
125           (length ())
126               => 0
127           (length "foobar")
128               => 6
129           (length [1 2 3])
130               => 3
131           (length #*01101)
132               => 5
133
134  - Function: elt SEQUENCE INDEX
135      This function returns the element of SEQUENCE indexed by INDEX.
136      Legitimate values of INDEX are integers ranging from 0 up to one
137      less than the length of SEQUENCE.  If SEQUENCE is a list, then
138      out-of-range values of INDEX return `nil'; otherwise, they trigger
139      an `args-out-of-range' error.
140
141           (elt [1 2 3 4] 2)
142                => 3
143           (elt '(1 2 3 4) 2)
144                => 3
145           (char-to-string (elt "1234" 2))
146                => "3"
147           (elt #*00010000 3)
148                => 1
149           (elt [1 2 3 4] 4)
150                error-->Args out of range: [1 2 3 4], 4
151           (elt [1 2 3 4] -1)
152                error-->Args out of range: [1 2 3 4], -1
153
154      This function generalizes `aref' (*note Array Functions::.) and
155      `nth' (*note List Elements::.).
156
157 \1f
158 File: lispref.info,  Node: Arrays,  Next: Array Functions,  Prev: Sequence Functions,  Up: Sequences Arrays Vectors
159
160 Arrays
161 ======
162
163    An "array" object has slots that hold a number of other Lisp
164 objects, called the elements of the array.  Any element of an array may
165 be accessed in constant time.  In contrast, an element of a list
166 requires access time that is proportional to the position of the element
167 in the list.
168
169    When you create an array, you must specify how many elements it has.
170 The amount of space allocated depends on the number of elements.
171 Therefore, it is impossible to change the size of an array once it is
172 created; you cannot add or remove elements.  However, you can replace an
173 element with a different value.
174
175    XEmacs defines three types of array, all of which are
176 one-dimensional: "strings", "vectors", and "bit vectors".  A vector is a
177 general array; its elements can be any Lisp objects.  A string is a
178 specialized array; its elements must be characters.  A bit vector is
179 another specialized array; its elements must be bits (an integer, either
180 0 or 1).  Each type of array has its own read syntax.  *Note String
181 Type::, *Note Vector Type::, and *Note Bit Vector Type::.
182
183    All kinds of array share these characteristics:
184
185    * The first element of an array has index zero, the second element
186      has index 1, and so on.  This is called "zero-origin" indexing.
187      For example, an array of four elements has indices 0, 1, 2, and 3.
188
189    * The elements of an array may be referenced or changed with the
190      functions `aref' and `aset', respectively (*note Array
191      Functions::.).
192
193    In principle, if you wish to have an array of text characters, you
194 could use either a string or a vector.  In practice, we always choose
195 strings for such applications, for four reasons:
196
197    * They usually occupy one-fourth the space of a vector of the same
198      elements.  (This is one-eighth the space for 64-bit machines such
199      as the DEC Alpha, and may also be different when MULE support is
200      compiled into XEmacs.)
201
202    * Strings are printed in a way that shows the contents more clearly
203      as characters.
204
205    * Strings can hold extent and text properties.  *Note Extents::,
206      *Note Text Properties::.
207
208    * Many of the specialized editing and I/O facilities of XEmacs
209      accept only strings.  For example, you cannot insert a vector of
210      characters into a buffer the way you can insert a string.  *Note
211      Strings and Characters::.
212
213    By contrast, for an array of keyboard input characters (such as a key
214 sequence), a vector may be necessary, because many keyboard input
215 characters are non-printable and are represented with symbols rather
216 than with characters.  *Note Key Sequence Input::.
217
218    Similarly, when representing an array of bits, a bit vector has the
219 following advantages over a regular vector:
220
221    * They occupy 1/32nd the space of a vector of the same elements.
222      (1/64th on 64-bit machines such as the DEC Alpha.)
223
224    * Bit vectors are printed in a way that shows the contents more
225      clearly as bits.
226
227 \1f
228 File: lispref.info,  Node: Array Functions,  Next: Vectors,  Prev: Arrays,  Up: Sequences Arrays Vectors
229
230 Functions that Operate on Arrays
231 ================================
232
233    In this section, we describe the functions that accept strings,
234 vectors, and bit vectors.
235
236  - Function: arrayp OBJECT
237      This function returns `t' if OBJECT is an array (i.e., a string,
238      vector, or bit vector).
239
240           (arrayp "asdf")
241           => t
242           (arrayp [a])
243           => t
244           (arrayp #*101)
245           => t
246
247  - Function: aref ARRAY INDEX
248      This function returns the INDEXth element of ARRAY.  The first
249      element is at index zero.
250
251           (setq primes [2 3 5 7 11 13])
252                => [2 3 5 7 11 13]
253           (aref primes 4)
254                => 11
255           (elt primes 4)
256                => 11
257           
258           (aref "abcdefg" 1)
259                => ?b
260           
261           (aref #*1101 2)
262                => 0
263
264      See also the function `elt', in *Note Sequence Functions::.
265
266  - Function: aset ARRAY INDEX OBJECT
267      This function sets the INDEXth element of ARRAY to be OBJECT.  It
268      returns OBJECT.
269
270           (setq w [foo bar baz])
271                => [foo bar baz]
272           (aset w 0 'fu)
273                => fu
274           w
275                => [fu bar baz]
276           
277           (setq x "asdfasfd")
278                => "asdfasfd"
279           (aset x 3 ?Z)
280                => ?Z
281           x
282                => "asdZasfd"
283           
284           (setq bv #*1111)
285                => #*1111
286           (aset bv 2 0)
287                => 0
288           bv
289                => #*1101
290
291      If ARRAY is a string and OBJECT is not a character, a
292      `wrong-type-argument' error results.
293
294  - Function: fillarray ARRAY OBJECT
295      This function fills the array ARRAY with OBJECT, so that each
296      element of ARRAY is OBJECT.  It returns ARRAY.
297
298           (setq a [a b c d e f g])
299                => [a b c d e f g]
300           (fillarray a 0)
301                => [0 0 0 0 0 0 0]
302           a
303                => [0 0 0 0 0 0 0]
304           
305           (setq s "When in the course")
306                => "When in the course"
307           (fillarray s ?-)
308                => "------------------"
309           
310           (setq bv #*1101)
311                => #*1101
312           (fillarray bv 0)
313                => #*0000
314
315      If ARRAY is a string and OBJECT is not a character, a
316      `wrong-type-argument' error results.
317
318    The general sequence functions `copy-sequence' and `length' are
319 often useful for objects known to be arrays.  *Note Sequence
320 Functions::.
321
322 \1f
323 File: lispref.info,  Node: Vectors,  Next: Vector Functions,  Prev: Array Functions,  Up: Sequences Arrays Vectors
324
325 Vectors
326 =======
327
328    Arrays in Lisp, like arrays in most languages, are blocks of memory
329 whose elements can be accessed in constant time.  A "vector" is a
330 general-purpose array; its elements can be any Lisp objects.  (The other
331 kind of array in XEmacs Lisp is the "string", whose elements must be
332 characters.)  Vectors in XEmacs serve as obarrays (vectors of symbols),
333 although this is a shortcoming that should be fixed.  They are also used
334 internally as part of the representation of a byte-compiled function; if
335 you print such a function, you will see a vector in it.
336
337    In XEmacs Lisp, the indices of the elements of a vector start from
338 zero and count up from there.
339
340    Vectors are printed with square brackets surrounding the elements.
341 Thus, a vector whose elements are the symbols `a', `b' and `a' is
342 printed as `[a b a]'.  You can write vectors in the same way in Lisp
343 input.
344
345    A vector, like a string or a number, is considered a constant for
346 evaluation: the result of evaluating it is the same vector.  This does
347 not evaluate or even examine the elements of the vector.  *Note
348 Self-Evaluating Forms::.
349
350    Here are examples of these principles:
351
352      (setq avector [1 two '(three) "four" [five]])
353           => [1 two (quote (three)) "four" [five]]
354      (eval avector)
355           => [1 two (quote (three)) "four" [five]]
356      (eq avector (eval avector))
357           => t
358
359 \1f
360 File: lispref.info,  Node: Vector Functions,  Next: Bit Vectors,  Prev: Vectors,  Up: Sequences Arrays Vectors
361
362 Functions That Operate on Vectors
363 =================================
364
365    Here are some functions that relate to vectors:
366
367  - Function: vectorp OBJECT
368      This function returns `t' if OBJECT is a vector.
369
370           (vectorp [a])
371                => t
372           (vectorp "asdf")
373                => nil
374
375  - Function: vector &rest OBJECTS
376      This function creates and returns a vector whose elements are the
377      arguments, OBJECTS.
378
379           (vector 'foo 23 [bar baz] "rats")
380                => [foo 23 [bar baz] "rats"]
381           (vector)
382                => []
383
384  - Function: make-vector LENGTH OBJECT
385      This function returns a new vector consisting of LENGTH elements,
386      each initialized to OBJECT.
387
388           (setq sleepy (make-vector 9 'Z))
389                => [Z Z Z Z Z Z Z Z Z]
390
391  - Function: vconcat &rest SEQUENCES
392      This function returns a new vector containing all the elements of
393      the SEQUENCES.  The arguments SEQUENCES may be lists, vectors, or
394      strings.  If no SEQUENCES are given, an empty vector is returned.
395
396      The value is a newly constructed vector that is not `eq' to any
397      existing vector.
398
399           (setq a (vconcat '(A B C) '(D E F)))
400                => [A B C D E F]
401           (eq a (vconcat a))
402                => nil
403           (vconcat)
404                => []
405           (vconcat [A B C] "aa" '(foo (6 7)))
406                => [A B C 97 97 foo (6 7)]
407
408      The `vconcat' function also allows integers as arguments.  It
409      converts them to strings of digits, making up the decimal print
410      representation of the integer, and then uses the strings instead
411      of the original integers.  *Don't use this feature; we plan to
412      eliminate it.  If you already use this feature, change your
413      programs now!*  The proper way to convert an integer to a decimal
414      number in this way is with `format' (*note Formatting Strings::.)
415      or `number-to-string' (*note String Conversion::.).
416
417      For other concatenation functions, see `mapconcat' in *Note
418      Mapping Functions::, `concat' in *Note Creating Strings::, `append'
419      in *Note Building Lists::, and `bvconcat' in *Note Bit Vector
420      Functions::.
421
422    The `append' function provides a way to convert a vector into a list
423 with the same elements (*note Building Lists::.):
424
425      (setq avector [1 two (quote (three)) "four" [five]])
426           => [1 two (quote (three)) "four" [five]]
427      (append avector nil)
428           => (1 two (quote (three)) "four" [five])
429
430 \1f
431 File: lispref.info,  Node: Bit Vectors,  Next: Bit Vector Functions,  Prev: Vector Functions,  Up: Sequences Arrays Vectors
432
433 Bit Vectors
434 ===========
435
436    Bit vectors are specialized vectors that can only represent arrays
437 of 1's and 0's.  Bit vectors have a very efficient representation and
438 are useful for representing sets of boolean (true or false) values.
439
440    There is no limit on the size of a bit vector.  You could, for
441 example, create a bit vector with 100,000 elements if you really wanted
442 to.
443
444    Bit vectors have a special printed representation consisting of `#*'
445 followed by the bits of the vector.  For example, a bit vector whose
446 elements are 0, 1, 1, 0, and 1, respectively, is printed as
447
448      #*01101
449
450    Bit vectors are considered constants for evaluation, like vectors,
451 strings, and numbers.  *Note Self-Evaluating Forms::.
452
453 \1f
454 File: lispref.info,  Node: Bit Vector Functions,  Prev: Bit Vectors,  Up: Sequences Arrays Vectors
455
456 Functions That Operate on Bit Vectors
457 =====================================
458
459    Here are some functions that relate to bit vectors:
460
461  - Function: bit-vector-p OBJECT
462      This function returns `t' if OBJECT is a bit vector.
463
464           (bit-vector-p #*01)
465                => t
466           (bit-vector-p [0 1])
467                => nil
468           (bit-vector-p "01")
469                => nil
470
471  - Function: bitp OBJECT
472      This function returns `t' if OBJECT is either 0 or 1.
473
474  - Function: bit-vector &rest OBJECTS
475      This function creates and returns a bit vector whose elements are
476      the arguments OBJECTS.  The elements must be either of the two
477      integers 0 or 1.
478
479           (bit-vector 0 0 0 1 0 0 0 0 1 0)
480                => #*0001000010
481           (bit-vector)
482                => #*
483
484  - Function: make-bit-vector LENGTH OBJECT
485      This function creates and returns a bit vector consisting of
486      LENGTH elements, each initialized to OBJECT.
487
488           (setq picket-fence (make-bit-vector 9 1))
489                => #*111111111
490
491  - Function: bvconcat &rest SEQUENCES
492      This function returns a new bit vector containing all the elements
493      of the SEQUENCES.  The arguments SEQUENCES may be lists, vectors,
494      or bit vectors, all of whose elements are the integers 0 or 1.  If
495      no SEQUENCES are given, an empty bit vector is returned.
496
497      The value is a newly constructed bit vector that is not `eq' to any
498      existing bit vector.
499
500           (setq a (bvconcat '(1 1 0) '(0 0 1)))
501                => #*110001
502           (eq a (bvconcat a))
503                => nil
504           (bvconcat)
505                => #*
506           (bvconcat [1 0 0 0 0] #*111 '(0 0 0 0 1))
507                => #*1000011100001
508
509      For other concatenation functions, see `mapconcat' in *Note
510      Mapping Functions::, `concat' in *Note Creating Strings::,
511      `vconcat' in *Note Vector Functions::, and `append' in *Note
512      Building Lists::.
513
514    The `append' function provides a way to convert a bit vector into a
515 list with the same elements (*note Building Lists::.):
516
517      (setq bv #*00001110)
518           => #*00001110
519      (append bv nil)
520           => (0 0 0 0 1 1 1 0)
521
522 \1f
523 File: lispref.info,  Node: Symbols,  Next: Evaluation,  Prev: Sequences Arrays Vectors,  Up: Top
524
525 Symbols
526 *******
527
528    A "symbol" is an object with a unique name.  This chapter describes
529 symbols, their components, their property lists, and how they are
530 created and interned.  Separate chapters describe the use of symbols as
531 variables and as function names; see *Note Variables::, and *Note
532 Functions::.  For the precise read syntax for symbols, see *Note Symbol
533 Type::.
534
535    You can test whether an arbitrary Lisp object is a symbol with
536 `symbolp':
537
538  - Function: symbolp OBJECT
539      This function returns `t' if OBJECT is a symbol, `nil' otherwise.
540
541 * Menu:
542
543 * Symbol Components::       Symbols have names, values, function definitions
544                               and property lists.
545 * Definitions::             A definition says how a symbol will be used.
546 * Creating Symbols::        How symbols are kept unique.
547 * Symbol Properties::       Each symbol has a property list
548                               for recording miscellaneous information.
549
550 \1f
551 File: lispref.info,  Node: Symbol Components,  Next: Definitions,  Up: Symbols
552
553 Symbol Components
554 =================
555
556    Each symbol has four components (or "cells"), each of which
557 references another object:
558
559 Print name
560      The "print name cell" holds a string that names the symbol for
561      reading and printing.  See `symbol-name' in *Note Creating
562      Symbols::.
563
564 Value
565      The "value cell" holds the current value of the symbol as a
566      variable.  When a symbol is used as a form, the value of the form
567      is the contents of the symbol's value cell.  See `symbol-value' in
568      *Note Accessing Variables::.
569
570 Function
571      The "function cell" holds the function definition of the symbol.
572      When a symbol is used as a function, its function definition is
573      used in its place.  This cell is also used to make a symbol stand
574      for a keymap or a keyboard macro, for editor command execution.
575      Because each symbol has separate value and function cells,
576      variables and function names do not conflict.  See
577      `symbol-function' in *Note Function Cells::.
578
579 Property list
580      The "property list cell" holds the property list of the symbol.
581      See `symbol-plist' in *Note Symbol Properties::.
582
583    The print name cell always holds a string, and cannot be changed.
584 The other three cells can be set individually to any specified Lisp
585 object.
586
587    The print name cell holds the string that is the name of the symbol.
588 Since symbols are represented textually by their names, it is important
589 not to have two symbols with the same name.  The Lisp reader ensures
590 this: every time it reads a symbol, it looks for an existing symbol with
591 the specified name before it creates a new one.  (In XEmacs Lisp, this
592 lookup uses a hashing algorithm and an obarray; see *Note Creating
593 Symbols::.)
594
595    In normal usage, the function cell usually contains a function or
596 macro, as that is what the Lisp interpreter expects to see there (*note
597 Evaluation::.).  Keyboard macros (*note Keyboard Macros::.), keymaps
598 (*note Keymaps::.) and autoload objects (*note Autoloading::.) are also
599 sometimes stored in the function cell of symbols.  We often refer to
600 "the function `foo'" when we really mean the function stored in the
601 function cell of the symbol `foo'.  We make the distinction only when
602 necessary.
603
604    The property list cell normally should hold a correctly formatted
605 property list (*note Property Lists::.), as a number of functions expect
606 to see a property list there.
607
608    The function cell or the value cell may be "void", which means that
609 the cell does not reference any object.  (This is not the same thing as
610 holding the symbol `void', nor the same as holding the symbol `nil'.)
611 Examining a cell that is void results in an error, such as `Symbol's
612 value as variable is void'.
613
614    The four functions `symbol-name', `symbol-value', `symbol-plist',
615 and `symbol-function' return the contents of the four cells of a
616 symbol.  Here as an example we show the contents of the four cells of
617 the symbol `buffer-file-name':
618
619      (symbol-name 'buffer-file-name)
620           => "buffer-file-name"
621      (symbol-value 'buffer-file-name)
622           => "/gnu/elisp/symbols.texi"
623      (symbol-plist 'buffer-file-name)
624           => (variable-documentation 29529)
625      (symbol-function 'buffer-file-name)
626           => #<subr buffer-file-name>
627
628 Because this symbol is the variable which holds the name of the file
629 being visited in the current buffer, the value cell contents we see are
630 the name of the source file of this chapter of the XEmacs Lisp Manual.
631 The property list cell contains the list `(variable-documentation
632 29529)' which tells the documentation functions where to find the
633 documentation string for the variable `buffer-file-name' in the `DOC'
634 file.  (29529 is the offset from the beginning of the `DOC' file to
635 where that documentation string begins.)  The function cell contains
636 the function for returning the name of the file.  `buffer-file-name'
637 names a primitive function, which has no read syntax and prints in hash
638 notation (*note Primitive Function Type::.).  A symbol naming a
639 function written in Lisp would have a lambda expression (or a byte-code
640 object) in this cell.
641
642 \1f
643 File: lispref.info,  Node: Definitions,  Next: Creating Symbols,  Prev: Symbol Components,  Up: Symbols
644
645 Defining Symbols
646 ================
647
648    A "definition" in Lisp is a special form that announces your
649 intention to use a certain symbol in a particular way.  In XEmacs Lisp,
650 you can define a symbol as a variable, or define it as a function (or
651 macro), or both independently.
652
653    A definition construct typically specifies a value or meaning for the
654 symbol for one kind of use, plus documentation for its meaning when used
655 in this way.  Thus, when you define a symbol as a variable, you can
656 supply an initial value for the variable, plus documentation for the
657 variable.
658
659    `defvar' and `defconst' are special forms that define a symbol as a
660 global variable.  They are documented in detail in *Note Defining
661 Variables::.
662
663    `defun' defines a symbol as a function, creating a lambda expression
664 and storing it in the function cell of the symbol.  This lambda
665 expression thus becomes the function definition of the symbol.  (The
666 term "function definition", meaning the contents of the function cell,
667 is derived from the idea that `defun' gives the symbol its definition
668 as a function.)  `defsubst', `define-function' and `defalias' are other
669 ways of defining a function.  *Note Functions::.
670
671    `defmacro' defines a symbol as a macro.  It creates a macro object
672 and stores it in the function cell of the symbol.  Note that a given
673 symbol can be a macro or a function, but not both at once, because both
674 macro and function definitions are kept in the function cell, and that
675 cell can hold only one Lisp object at any given time.  *Note Macros::.
676
677    In XEmacs Lisp, a definition is not required in order to use a symbol
678 as a variable or function.  Thus, you can make a symbol a global
679 variable with `setq', whether you define it first or not.  The real
680 purpose of definitions is to guide programmers and programming tools.
681 They inform programmers who read the code that certain symbols are
682 *intended* to be used as variables, or as functions.  In addition,
683 utilities such as `etags' and `make-docfile' recognize definitions, and
684 add appropriate information to tag tables and the `DOC' file. *Note
685 Accessing Documentation::.
686
687 \1f
688 File: lispref.info,  Node: Creating Symbols,  Next: Symbol Properties,  Prev: Definitions,  Up: Symbols
689
690 Creating and Interning Symbols
691 ==============================
692
693    To understand how symbols are created in XEmacs Lisp, you must know
694 how Lisp reads them.  Lisp must ensure that it finds the same symbol
695 every time it reads the same set of characters.  Failure to do so would
696 cause complete confusion.
697
698    When the Lisp reader encounters a symbol, it reads all the characters
699 of the name.  Then it "hashes" those characters to find an index in a
700 table called an "obarray".  Hashing is an efficient method of looking
701 something up.  For example, instead of searching a telephone book cover
702 to cover when looking up Jan Jones, you start with the J's and go from
703 there.  That is a simple version of hashing.  Each element of the
704 obarray is a "bucket" which holds all the symbols with a given hash
705 code; to look for a given name, it is sufficient to look through all
706 the symbols in the bucket for that name's hash code.
707
708    If a symbol with the desired name is found, the reader uses that
709 symbol.  If the obarray does not contain a symbol with that name, the
710 reader makes a new symbol and adds it to the obarray.  Finding or adding
711 a symbol with a certain name is called "interning" it, and the symbol
712 is then called an "interned symbol".
713
714    Interning ensures that each obarray has just one symbol with any
715 particular name.  Other like-named symbols may exist, but not in the
716 same obarray.  Thus, the reader gets the same symbols for the same
717 names, as long as you keep reading with the same obarray.
718
719    No obarray contains all symbols; in fact, some symbols are not in any
720 obarray.  They are called "uninterned symbols".  An uninterned symbol
721 has the same four cells as other symbols; however, the only way to gain
722 access to it is by finding it in some other object or as the value of a
723 variable.
724
725    In XEmacs Lisp, an obarray is actually a vector.  Each element of the
726 vector is a bucket; its value is either an interned symbol whose name
727 hashes to that bucket, or 0 if the bucket is empty.  Each interned
728 symbol has an internal link (invisible to the user) to the next symbol
729 in the bucket.  Because these links are invisible, there is no way to
730 find all the symbols in an obarray except using `mapatoms' (below).
731 The order of symbols in a bucket is not significant.
732
733    In an empty obarray, every element is 0, and you can create an
734 obarray with `(make-vector LENGTH 0)'.  *This is the only valid way to
735 create an obarray.*  Prime numbers as lengths tend to result in good
736 hashing; lengths one less than a power of two are also good.
737
738    *Do not try to put symbols in an obarray yourself.*  This does not
739 work--only `intern' can enter a symbol in an obarray properly.  *Do not
740 try to intern one symbol in two obarrays.*  This would garble both
741 obarrays, because a symbol has just one slot to hold the following
742 symbol in the obarray bucket.  The results would be unpredictable.
743
744    It is possible for two different symbols to have the same name in
745 different obarrays; these symbols are not `eq' or `equal'.  However,
746 this normally happens only as part of the abbrev mechanism (*note
747 Abbrevs::.).
748
749      Common Lisp note: In Common Lisp, a single symbol may be interned
750      in several obarrays.
751
752    Most of the functions below take a name and sometimes an obarray as
753 arguments.  A `wrong-type-argument' error is signaled if the name is
754 not a string, or if the obarray is not a vector.
755
756  - Function: symbol-name SYMBOL
757      This function returns the string that is SYMBOL's name.  For
758      example:
759
760           (symbol-name 'foo)
761                => "foo"
762
763      Changing the string by substituting characters, etc, does change
764      the name of the symbol, but fails to update the obarray, so don't
765      do it!
766
767  - Function: make-symbol NAME
768      This function returns a newly-allocated, uninterned symbol whose
769      name is NAME (which must be a string).  Its value and function
770      definition are void, and its property list is `nil'.  In the
771      example below, the value of `sym' is not `eq' to `foo' because it
772      is a distinct uninterned symbol whose name is also `foo'.
773
774           (setq sym (make-symbol "foo"))
775                => foo
776           (eq sym 'foo)
777                => nil
778
779  - Function: intern NAME &optional OBARRAY
780      This function returns the interned symbol whose name is NAME.  If
781      there is no such symbol in the obarray OBARRAY, `intern' creates a
782      new one, adds it to the obarray, and returns it.  If OBARRAY is
783      omitted, the value of the global variable `obarray' is used.
784
785           (setq sym (intern "foo"))
786                => foo
787           (eq sym 'foo)
788                => t
789           
790           (setq sym1 (intern "foo" other-obarray))
791                => foo
792           (eq sym 'foo)
793                => nil
794
795  - Function: intern-soft NAME &optional OBARRAY
796      This function returns the symbol in OBARRAY whose name is NAME, or
797      `nil' if OBARRAY has no symbol with that name.  Therefore, you can
798      use `intern-soft' to test whether a symbol with a given name is
799      already interned.  If OBARRAY is omitted, the value of the global
800      variable `obarray' is used.
801
802           (intern-soft "frazzle")        ; No such symbol exists.
803                => nil
804           (make-symbol "frazzle")        ; Create an uninterned one.
805                => frazzle
806           (intern-soft "frazzle")        ; That one cannot be found.
807                => nil
808
809           (setq sym (intern "frazzle"))  ; Create an interned one.
810                => frazzle
811
812           (intern-soft "frazzle")        ; That one can be found!
813                => frazzle
814
815           (eq sym 'frazzle)              ; And it is the same one.
816                => t
817
818  - Variable: obarray
819      This variable is the standard obarray for use by `intern' and
820      `read'.
821
822  - Function: mapatoms FUNCTION &optional OBARRAY
823      This function calls FUNCTION for each symbol in the obarray
824      OBARRAY.  It returns `nil'.  If OBARRAY is omitted, it defaults to
825      the value of `obarray', the standard obarray for ordinary symbols.
826
827           (setq count 0)
828                => 0
829           (defun count-syms (s)
830             (setq count (1+ count)))
831                => count-syms
832           (mapatoms 'count-syms)
833                => nil
834           count
835                => 1871
836
837      See `documentation' in *Note Accessing Documentation::, for another
838      example using `mapatoms'.
839
840  - Function: unintern SYMBOL &optional OBARRAY
841      This function deletes SYMBOL from the obarray OBARRAY.  If
842      `symbol' is not actually in the obarray, `unintern' does nothing.
843      If OBARRAY is `nil', the current obarray is used.
844
845      If you provide a string instead of a symbol as SYMBOL, it stands
846      for a symbol name.  Then `unintern' deletes the symbol (if any) in
847      the obarray which has that name.  If there is no such symbol,
848      `unintern' does nothing.
849
850      If `unintern' does delete a symbol, it returns `t'.  Otherwise it
851      returns `nil'.
852
853 \1f
854 File: lispref.info,  Node: Symbol Properties,  Prev: Creating Symbols,  Up: Symbols
855
856 Symbol Properties
857 =================
858
859    A "property list" ("plist" for short) is a list of paired elements
860 stored in the property list cell of a symbol.  Each of the pairs
861 associates a property name (usually a symbol) with a property or value.
862 Property lists are generally used to record information about a
863 symbol, such as its documentation as a variable, the name of the file
864 where it was defined, or perhaps even the grammatical class of the
865 symbol (representing a word) in a language-understanding system.
866
867    Many objects other than symbols can have property lists associated
868 with them, and XEmacs provides a full complement of functions for
869 working with property lists.  *Note Property Lists::.
870
871    The property names and values in a property list can be any Lisp
872 objects, but the names are usually symbols.  They are compared using
873 `eq'.  Here is an example of a property list, found on the symbol
874 `progn' when the compiler is loaded:
875
876      (lisp-indent-function 0 byte-compile byte-compile-progn)
877
878 Here `lisp-indent-function' and `byte-compile' are property names, and
879 the other two elements are the corresponding values.
880
881 * Menu:
882
883 * Plists and Alists::           Comparison of the advantages of property
884                                   lists and association lists.
885 * Symbol Plists::               Functions to access symbols' property lists.
886 * Other Plists::                Accessing property lists stored elsewhere.
887
888 \1f
889 File: lispref.info,  Node: Plists and Alists,  Next: Symbol Plists,  Up: Symbol Properties
890
891 Property Lists and Association Lists
892 ------------------------------------
893
894    Association lists (*note Association Lists::.) are very similar to
895 property lists.  In contrast to association lists, the order of the
896 pairs in the property list is not significant since the property names
897 must be distinct.
898
899    Property lists are better than association lists for attaching
900 information to various Lisp function names or variables.  If all the
901 associations are recorded in one association list, the program will need
902 to search that entire list each time a function or variable is to be
903 operated on.  By contrast, if the information is recorded in the
904 property lists of the function names or variables themselves, each
905 search will scan only the length of one property list, which is usually
906 short.  This is why the documentation for a variable is recorded in a
907 property named `variable-documentation'.  The byte compiler likewise
908 uses properties to record those functions needing special treatment.
909
910    However, association lists have their own advantages.  Depending on
911 your application, it may be faster to add an association to the front of
912 an association list than to update a property.  All properties for a
913 symbol are stored in the same property list, so there is a possibility
914 of a conflict between different uses of a property name.  (For this
915 reason, it is a good idea to choose property names that are probably
916 unique, such as by including the name of the library in the property
917 name.)  An association list may be used like a stack where associations
918 are pushed on the front of the list and later discarded; this is not
919 possible with a property list.
920
921 \1f
922 File: lispref.info,  Node: Symbol Plists,  Next: Other Plists,  Prev: Plists and Alists,  Up: Symbol Properties
923
924 Property List Functions for Symbols
925 -----------------------------------
926
927  - Function: symbol-plist SYMBOL
928      This function returns the property list of SYMBOL.
929
930  - Function: setplist SYMBOL PLIST
931      This function sets SYMBOL's property list to PLIST.  Normally,
932      PLIST should be a well-formed property list, but this is not
933      enforced.
934
935           (setplist 'foo '(a 1 b (2 3) c nil))
936                => (a 1 b (2 3) c nil)
937           (symbol-plist 'foo)
938                => (a 1 b (2 3) c nil)
939
940      For symbols in special obarrays, which are not used for ordinary
941      purposes, it may make sense to use the property list cell in a
942      nonstandard fashion; in fact, the abbrev mechanism does so (*note
943      Abbrevs::.).
944
945  - Function: get SYMBOL PROPERTY
946      This function finds the value of the property named PROPERTY in
947      SYMBOL's property list.  If there is no such property, `nil' is
948      returned.  Thus, there is no distinction between a value of `nil'
949      and the absence of the property.
950
951      The name PROPERTY is compared with the existing property names
952      using `eq', so any object is a legitimate property.
953
954      See `put' for an example.
955
956  - Function: put SYMBOL PROPERTY VALUE
957      This function puts VALUE onto SYMBOL's property list under the
958      property name PROPERTY, replacing any previous property value.
959      The `put' function returns VALUE.
960
961           (put 'fly 'verb 'transitive)
962                =>'transitive
963           (put 'fly 'noun '(a buzzing little bug))
964                => (a buzzing little bug)
965           (get 'fly 'verb)
966                => transitive
967           (symbol-plist 'fly)
968                => (verb transitive noun (a buzzing little bug))
969
970 \1f
971 File: lispref.info,  Node: Other Plists,  Prev: Symbol Plists,  Up: Symbol Properties
972
973 Property Lists Outside Symbols
974 ------------------------------
975
976    These functions are useful for manipulating property lists that are
977 stored in places other than symbols:
978
979  - Function: getf PLIST PROPERTY &optional DEFAULT
980      This returns the value of the PROPERTY property stored in the
981      property list PLIST.  For example,
982
983           (getf '(foo 4) 'foo)
984                => 4
985
986  - Function: putf PLIST PROPERTY VALUE
987      This stores VALUE as the value of the PROPERTY property in the
988      property list PLIST.  It may modify PLIST destructively, or it may
989      construct a new list structure without altering the old.  The
990      function returns the modified property list, so you can store that
991      back in the place where you got PLIST.  For example,
992
993           (setq my-plist '(bar t foo 4))
994                => (bar t foo 4)
995           (setq my-plist (putf my-plist 'foo 69))
996                => (bar t foo 69)
997           (setq my-plist (putf my-plist 'quux '(a)))
998                => (quux (a) bar t foo 5)
999
1000  - Function: plists-eq A B
1001      This function returns non-`nil' if property lists A and B are
1002      `eq'.  This means that the property lists have the same values for
1003      all the same properties, where comparison between values is done
1004      using `eq'.
1005
1006  - Function: plists-equal A B
1007      This function returns non-`nil' if property lists A and B are
1008      `equal'.
1009
1010    Both of the above functions do order-insensitive comparisons.
1011
1012      (plists-eq '(a 1 b 2 c nil) '(b 2 a 1))
1013           => t
1014      (plists-eq '(foo "hello" bar "goodbye") '(bar "goodbye" foo "hello"))
1015           => nil
1016      (plists-equal '(foo "hello" bar "goodbye") '(bar "goodbye" foo "hello"))
1017           => t
1018
1019 \1f
1020 File: lispref.info,  Node: Evaluation,  Next: Control Structures,  Prev: Symbols,  Up: Top
1021
1022 Evaluation
1023 **********
1024
1025    The "evaluation" of expressions in XEmacs Lisp is performed by the
1026 "Lisp interpreter"--a program that receives a Lisp object as input and
1027 computes its "value as an expression".  How it does this depends on the
1028 data type of the object, according to rules described in this chapter.
1029 The interpreter runs automatically to evaluate portions of your
1030 program, but can also be called explicitly via the Lisp primitive
1031 function `eval'.
1032
1033 * Menu:
1034
1035 * Intro Eval::  Evaluation in the scheme of things.
1036 * Eval::        How to invoke the Lisp interpreter explicitly.
1037 * Forms::       How various sorts of objects are evaluated.
1038 * Quoting::     Avoiding evaluation (to put constants in the program).
1039
1040 \1f
1041 File: lispref.info,  Node: Intro Eval,  Next: Eval,  Up: Evaluation
1042
1043 Introduction to Evaluation
1044 ==========================
1045
1046    The Lisp interpreter, or evaluator, is the program that computes the
1047 value of an expression that is given to it.  When a function written in
1048 Lisp is called, the evaluator computes the value of the function by
1049 evaluating the expressions in the function body.  Thus, running any
1050 Lisp program really means running the Lisp interpreter.
1051
1052    How the evaluator handles an object depends primarily on the data
1053 type of the object.
1054
1055    A Lisp object that is intended for evaluation is called an
1056 "expression" or a "form".  The fact that expressions are data objects
1057 and not merely text is one of the fundamental differences between
1058 Lisp-like languages and typical programming languages.  Any object can
1059 be evaluated, but in practice only numbers, symbols, lists and strings
1060 are evaluated very often.
1061
1062    It is very common to read a Lisp expression and then evaluate the
1063 expression, but reading and evaluation are separate activities, and
1064 either can be performed alone.  Reading per se does not evaluate
1065 anything; it converts the printed representation of a Lisp object to the
1066 object itself.  It is up to the caller of `read' whether this object is
1067 a form to be evaluated, or serves some entirely different purpose.
1068 *Note Input Functions::.
1069
1070    Do not confuse evaluation with command key interpretation.  The
1071 editor command loop translates keyboard input into a command (an
1072 interactively callable function) using the active keymaps, and then
1073 uses `call-interactively' to invoke the command.  The execution of the
1074 command itself involves evaluation if the command is written in Lisp,
1075 but that is not a part of command key interpretation itself.  *Note
1076 Command Loop::.
1077
1078    Evaluation is a recursive process.  That is, evaluation of a form may
1079 call `eval' to evaluate parts of the form.  For example, evaluation of
1080 a function call first evaluates each argument of the function call, and
1081 then evaluates each form in the function body.  Consider evaluation of
1082 the form `(car x)': the subform `x' must first be evaluated
1083 recursively, so that its value can be passed as an argument to the
1084 function `car'.
1085
1086    Evaluation of a function call ultimately calls the function specified
1087 in it.  *Note Functions::.  The execution of the function may itself
1088 work by evaluating the function definition; or the function may be a
1089 Lisp primitive implemented in C, or it may be a byte-compiled function
1090 (*note Byte Compilation::.).
1091
1092    The evaluation of forms takes place in a context called the
1093 "environment", which consists of the current values and bindings of all
1094 Lisp variables.(1)  Whenever the form refers to a variable without
1095 creating a new binding for it, the value of the binding in the current
1096 environment is used.  *Note Variables::.
1097
1098    Evaluation of a form may create new environments for recursive
1099 evaluation by binding variables (*note Local Variables::.).  These
1100 environments are temporary and vanish by the time evaluation of the form
1101 is complete.  The form may also make changes that persist; these changes
1102 are called "side effects".  An example of a form that produces side
1103 effects is `(setq foo 1)'.
1104
1105    The details of what evaluation means for each kind of form are
1106 described below (*note Forms::.).
1107
1108    ---------- Footnotes ----------
1109
1110    (1) This definition of "environment" is specifically not intended to
1111 include all the data that can affect the result of a program.
1112
1113 \1f
1114 File: lispref.info,  Node: Eval,  Next: Forms,  Prev: Intro Eval,  Up: Evaluation
1115
1116 Eval
1117 ====
1118
1119    Most often, forms are evaluated automatically, by virtue of their
1120 occurrence in a program being run.  On rare occasions, you may need to
1121 write code that evaluates a form that is computed at run time, such as
1122 after reading a form from text being edited or getting one from a
1123 property list.  On these occasions, use the `eval' function.
1124
1125    *Please note:* it is generally cleaner and more flexible to call
1126 functions that are stored in data structures, rather than to evaluate
1127 expressions stored in data structures.  Using functions provides the
1128 ability to pass information to them as arguments.
1129
1130    The functions and variables described in this section evaluate forms,
1131 specify limits to the evaluation process, or record recently returned
1132 values.  Loading a file also does evaluation (*note Loading::.).
1133
1134  - Function: eval FORM
1135      This is the basic function for performing evaluation.  It evaluates
1136      FORM in the current environment and returns the result.  How the
1137      evaluation proceeds depends on the type of the object (*note
1138      Forms::.).
1139
1140      Since `eval' is a function, the argument expression that appears
1141      in a call to `eval' is evaluated twice: once as preparation before
1142      `eval' is called, and again by the `eval' function itself.  Here
1143      is an example:
1144
1145           (setq foo 'bar)
1146                => bar
1147           (setq bar 'baz)
1148                => baz
1149           ;; `eval' receives argument `bar', which is the value of `foo'
1150           (eval foo)
1151                => baz
1152           (eval 'foo)
1153                => bar
1154
1155      The number of currently active calls to `eval' is limited to
1156      `max-lisp-eval-depth' (see below).
1157
1158  - Command: eval-region START END &optional STREAM
1159      This function evaluates the forms in the current buffer in the
1160      region defined by the positions START and END.  It reads forms from
1161      the region and calls `eval' on them until the end of the region is
1162      reached, or until an error is signaled and not handled.
1163
1164      If STREAM is supplied, `standard-output' is bound to it during the
1165      evaluation.
1166
1167      You can use the variable `load-read-function' to specify a function
1168      for `eval-region' to use instead of `read' for reading
1169      expressions.  *Note How Programs Do Loading::.
1170
1171      `eval-region' always returns `nil'.
1172
1173  - Command: eval-buffer BUFFER &optional STREAM
1174      This is like `eval-region' except that it operates on the whole
1175      contents of BUFFER.
1176
1177  - Variable: max-lisp-eval-depth
1178      This variable defines the maximum depth allowed in calls to `eval',
1179      `apply', and `funcall' before an error is signaled (with error
1180      message `"Lisp nesting exceeds max-lisp-eval-depth"').  This counts
1181      internal uses of those functions, such as for calling the functions
1182      mentioned in Lisp expressions, and recursive evaluation of
1183      function call arguments and function body forms.
1184
1185      This limit, with the associated error when it is exceeded, is one
1186      way that Lisp avoids infinite recursion on an ill-defined function.
1187
1188      The default value of this variable is 500.  If you set it to a
1189      value less than 100, Lisp will reset it to 100 if the given value
1190      is reached.
1191
1192      `max-specpdl-size' provides another limit on nesting.  *Note Local
1193      Variables::.
1194
1195  - Variable: values
1196      The value of this variable is a list of the values returned by all
1197      the expressions that were read from buffers (including the
1198      minibuffer), evaluated, and printed.  The elements are ordered
1199      most recent first.
1200
1201           (setq x 1)
1202                => 1
1203           (list 'A (1+ 2) auto-save-default)
1204                => (A 3 t)
1205           values
1206                => ((A 3 t) 1 ...)
1207
1208      This variable is useful for referring back to values of forms
1209      recently evaluated.  It is generally a bad idea to print the value
1210      of `values' itself, since this may be very long.  Instead, examine
1211      particular elements, like this:
1212
1213           ;; Refer to the most recent evaluation result.
1214           (nth 0 values)
1215                => (A 3 t)
1216           ;; That put a new element on,
1217           ;;   so all elements move back one.
1218           (nth 1 values)
1219                => (A 3 t)
1220           ;; This gets the element that was next-to-most-recent
1221           ;;   before this example.
1222           (nth 3 values)
1223                => 1
1224
1225 \1f
1226 File: lispref.info,  Node: Forms,  Next: Quoting,  Prev: Eval,  Up: Evaluation
1227
1228 Kinds of Forms
1229 ==============
1230
1231    A Lisp object that is intended to be evaluated is called a "form".
1232 How XEmacs evaluates a form depends on its data type.  XEmacs has three
1233 different kinds of form that are evaluated differently: symbols, lists,
1234 and "all other types".  This section describes all three kinds,
1235 starting with "all other types" which are self-evaluating forms.
1236
1237 * Menu:
1238
1239 * Self-Evaluating Forms::   Forms that evaluate to themselves.
1240 * Symbol Forms::            Symbols evaluate as variables.
1241 * Classifying Lists::       How to distinguish various sorts of list forms.
1242 * Function Indirection::    When a symbol appears as the car of a list,
1243                               we find the real function via the symbol.
1244 * Function Forms::          Forms that call functions.
1245 * Macro Forms::             Forms that call macros.
1246 * Special Forms::           "Special forms" are idiosyncratic primitives,
1247                               most of them extremely important.
1248 * Autoloading::             Functions set up to load files
1249                               containing their real definitions.
1250
1251 \1f
1252 File: lispref.info,  Node: Self-Evaluating Forms,  Next: Symbol Forms,  Up: Forms
1253
1254 Self-Evaluating Forms
1255 ---------------------
1256
1257    A "self-evaluating form" is any form that is not a list or symbol.
1258 Self-evaluating forms evaluate to themselves: the result of evaluation
1259 is the same object that was evaluated.  Thus, the number 25 evaluates to
1260 25, and the string `"foo"' evaluates to the string `"foo"'.  Likewise,
1261 evaluation of a vector does not cause evaluation of the elements of the
1262 vector--it returns the same vector with its contents unchanged.
1263
1264      '123               ; An object, shown without evaluation.
1265           => 123
1266      123                ; Evaluated as usual--result is the same.
1267           => 123
1268      (eval '123)        ; Evaluated "by hand"--result is the same.
1269           => 123
1270      (eval (eval '123)) ; Evaluating twice changes nothing.
1271           => 123
1272
1273    It is common to write numbers, characters, strings, and even vectors
1274 in Lisp code, taking advantage of the fact that they self-evaluate.
1275 However, it is quite unusual to do this for types that lack a read
1276 syntax, because there's no way to write them textually.  It is possible
1277 to construct Lisp expressions containing these types by means of a Lisp
1278 program.  Here is an example:
1279
1280      ;; Build an expression containing a buffer object.
1281      (setq buffer (list 'print (current-buffer)))
1282           => (print #<buffer eval.texi>)
1283      ;; Evaluate it.
1284      (eval buffer)
1285           -| #<buffer eval.texi>
1286           => #<buffer eval.texi>
1287