XEmacs 21.2.42 "Poseidon".
[chise/xemacs-chise.git.1] / info / lispref.info-7
1 This is ../info/lispref.info, produced by makeinfo version 4.0 from
2 lispref/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 bits
475      This function creates and returns a bit vector whose elements are
476      the arguments BITS.  Each argument must be a bit, i.e. one of the
477      two 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 bit
485      This function creates and returns a bit vector consisting of
486      LENGTH elements, each initialized to BIT, which must be one of the
487      two integers 0 or 1.
488
489           (setq picket-fence (make-bit-vector 9 1))
490                => #*111111111
491
492  - Function: bvconcat &rest sequences
493      This function returns a new bit vector containing all the elements
494      of the SEQUENCES.  The arguments SEQUENCES may be lists, vectors,
495      or bit vectors, all of whose elements are the integers 0 or 1.  If
496      no SEQUENCES are given, an empty bit vector is returned.
497
498      The value is a newly constructed bit vector that is not `eq' to any
499      existing bit vector.
500
501           (setq a (bvconcat '(1 1 0) '(0 0 1)))
502                => #*110001
503           (eq a (bvconcat a))
504                => nil
505           (bvconcat)
506                => #*
507           (bvconcat [1 0 0 0 0] #*111 '(0 0 0 0 1))
508                => #*1000011100001
509
510      For other concatenation functions, see `mapconcat' in *Note
511      Mapping Functions::, `concat' in *Note Creating Strings::,
512      `vconcat' in *Note Vector Functions::, and `append' in *Note
513      Building Lists::.
514
515    The `append' function provides a way to convert a bit vector into a
516 list with the same elements (*note Building Lists::):
517
518      (setq bv #*00001110)
519           => #*00001110
520      (append bv nil)
521           => (0 0 0 0 1 1 1 0)
522
523 \1f
524 File: lispref.info,  Node: Symbols,  Next: Evaluation,  Prev: Sequences Arrays Vectors,  Up: Top
525
526 Symbols
527 *******
528
529    A "symbol" is an object with a unique name.  This chapter describes
530 symbols, their components, their property lists, and how they are
531 created and interned.  Separate chapters describe the use of symbols as
532 variables and as function names; see *Note Variables::, and *Note
533 Functions::.  For the precise read syntax for symbols, see *Note Symbol
534 Type::.
535
536    You can test whether an arbitrary Lisp object is a symbol with
537 `symbolp':
538
539  - Function: symbolp object
540      This function returns `t' if OBJECT is a symbol, `nil' otherwise.
541
542 * Menu:
543
544 * Symbol Components::       Symbols have names, values, function definitions
545                               and property lists.
546 * Definitions::             A definition says how a symbol will be used.
547 * Creating Symbols::        How symbols are kept unique.
548 * Symbol Properties::       Each symbol has a property list
549                               for recording miscellaneous information.
550
551 \1f
552 File: lispref.info,  Node: Symbol Components,  Next: Definitions,  Up: Symbols
553
554 Symbol Components
555 =================
556
557    Each symbol has four components (or "cells"), each of which
558 references another object:
559
560 Print name
561      The "print name cell" holds a string that names the symbol for
562      reading and printing.  See `symbol-name' in *Note Creating
563      Symbols::.
564
565 Value
566      The "value cell" holds the current value of the symbol as a
567      variable.  When a symbol is used as a form, the value of the form
568      is the contents of the symbol's value cell.  See `symbol-value' in
569      *Note Accessing Variables::.
570
571 Function
572      The "function cell" holds the function definition of the symbol.
573      When a symbol is used as a function, its function definition is
574      used in its place.  This cell is also used to make a symbol stand
575      for a keymap or a keyboard macro, for editor command execution.
576      Because each symbol has separate value and function cells,
577      variables and function names do not conflict.  See
578      `symbol-function' in *Note Function Cells::.
579
580 Property list
581      The "property list cell" holds the property list of the symbol.
582      See `symbol-plist' in *Note Symbol Properties::.
583
584    The print name cell always holds a string, and cannot be changed.
585 The other three cells can be set individually to any specified Lisp
586 object.
587
588    The print name cell holds the string that is the name of the symbol.
589 Since symbols are represented textually by their names, it is important
590 not to have two symbols with the same name.  The Lisp reader ensures
591 this: every time it reads a symbol, it looks for an existing symbol with
592 the specified name before it creates a new one.  (In XEmacs Lisp, this
593 lookup uses a hashing algorithm and an obarray; see *Note Creating
594 Symbols::.)
595
596    In normal usage, the function cell usually contains a function or
597 macro, as that is what the Lisp interpreter expects to see there (*note
598 Evaluation::).  Keyboard macros (*note Keyboard Macros::), keymaps
599 (*note Keymaps::) and autoload objects (*note Autoloading::) are also
600 sometimes stored in the function cell of symbols.  We often refer to
601 "the function `foo'" when we really mean the function stored in the
602 function cell of the symbol `foo'.  We make the distinction only when
603 necessary.
604
605    The property list cell normally should hold a correctly formatted
606 property list (*note Property Lists::), as a number of functions expect
607 to see a property list there.
608
609    The function cell or the value cell may be "void", which means that
610 the cell does not reference any object.  (This is not the same thing as
611 holding the symbol `void', nor the same as holding the symbol `nil'.)
612 Examining a cell that is void results in an error, such as `Symbol's
613 value as variable is void'.
614
615    The four functions `symbol-name', `symbol-value', `symbol-plist',
616 and `symbol-function' return the contents of the four cells of a
617 symbol.  Here as an example we show the contents of the four cells of
618 the symbol `buffer-file-name':
619
620      (symbol-name 'buffer-file-name)
621           => "buffer-file-name"
622      (symbol-value 'buffer-file-name)
623           => "/gnu/elisp/symbols.texi"
624      (symbol-plist 'buffer-file-name)
625           => (variable-documentation 29529)
626      (symbol-function 'buffer-file-name)
627           => #<subr buffer-file-name>
628
629 Because this symbol is the variable which holds the name of the file
630 being visited in the current buffer, the value cell contents we see are
631 the name of the source file of this chapter of the XEmacs Lisp Reference
632 Manual.  The property list cell contains the list
633 `(variable-documentation 29529)' which tells the documentation
634 functions where to find the documentation string for the variable
635 `buffer-file-name' in the `DOC' file.  (29529 is the offset from the
636 beginning of the `DOC' file to where that documentation string begins.)
637 The function cell contains the function for returning the name of the
638 file.  `buffer-file-name' names a primitive function, which has no read
639 syntax and prints in hash notation (*note Primitive Function Type::).  A
640 symbol naming a function written in Lisp would have a lambda expression
641 (or a byte-code object) in this cell.
642
643 \1f
644 File: lispref.info,  Node: Definitions,  Next: Creating Symbols,  Prev: Symbol Components,  Up: Symbols
645
646 Defining Symbols
647 ================
648
649    A "definition" in Lisp is a special form that announces your
650 intention to use a certain symbol in a particular way.  In XEmacs Lisp,
651 you can define a symbol as a variable, or define it as a function (or
652 macro), or both independently.
653
654    A definition construct typically specifies a value or meaning for the
655 symbol for one kind of use, plus documentation for its meaning when used
656 in this way.  Thus, when you define a symbol as a variable, you can
657 supply an initial value for the variable, plus documentation for the
658 variable.
659
660    `defvar' and `defconst' are special forms that define a symbol as a
661 global variable.  They are documented in detail in *Note Defining
662 Variables::.
663
664    `defun' defines a symbol as a function, creating a lambda expression
665 and storing it in the function cell of the symbol.  This lambda
666 expression thus becomes the function definition of the symbol.  (The
667 term "function definition", meaning the contents of the function cell,
668 is derived from the idea that `defun' gives the symbol its definition
669 as a function.)  `defsubst', `define-function' and `defalias' are other
670 ways of defining a function.  *Note Functions::.
671
672    `defmacro' defines a symbol as a macro.  It creates a macro object
673 and stores it in the function cell of the symbol.  Note that a given
674 symbol can be a macro or a function, but not both at once, because both
675 macro and function definitions are kept in the function cell, and that
676 cell can hold only one Lisp object at any given time.  *Note Macros::.
677
678    In XEmacs Lisp, a definition is not required in order to use a symbol
679 as a variable or function.  Thus, you can make a symbol a global
680 variable with `setq', whether you define it first or not.  The real
681 purpose of definitions is to guide programmers and programming tools.
682 They inform programmers who read the code that certain symbols are
683 _intended_ to be used as variables, or as functions.  In addition,
684 utilities such as `etags' and `make-docfile' recognize definitions, and
685 add appropriate information to tag tables and the `DOC' file. *Note
686 Accessing Documentation::.
687
688 \1f
689 File: lispref.info,  Node: Creating Symbols,  Next: Symbol Properties,  Prev: Definitions,  Up: Symbols
690
691 Creating and Interning Symbols
692 ==============================
693
694    To understand how symbols are created in XEmacs Lisp, you must know
695 how Lisp reads them.  Lisp must ensure that it finds the same symbol
696 every time it reads the same set of characters.  Failure to do so would
697 cause complete confusion.
698
699    When the Lisp reader encounters a symbol, it reads all the characters
700 of the name.  Then it "hashes" those characters to find an index in a
701 table called an "obarray".  Hashing is an efficient method of looking
702 something up.  For example, instead of searching a telephone book cover
703 to cover when looking up Jan Jones, you start with the J's and go from
704 there.  That is a simple version of hashing.  Each element of the
705 obarray is a "bucket" which holds all the symbols with a given hash
706 code; to look for a given name, it is sufficient to look through all
707 the symbols in the bucket for that name's hash code.
708
709    If a symbol with the desired name is found, the reader uses that
710 symbol.  If the obarray does not contain a symbol with that name, the
711 reader makes a new symbol and adds it to the obarray.  Finding or adding
712 a symbol with a certain name is called "interning" it, and the symbol
713 is then called an "interned symbol".
714
715    Interning ensures that each obarray has just one symbol with any
716 particular name.  Other like-named symbols may exist, but not in the
717 same obarray.  Thus, the reader gets the same symbols for the same
718 names, as long as you keep reading with the same obarray.
719
720    No obarray contains all symbols; in fact, some symbols are not in any
721 obarray.  They are called "uninterned symbols".  An uninterned symbol
722 has the same four cells as other symbols; however, the only way to gain
723 access to it is by finding it in some other object or as the value of a
724 variable.
725
726    In XEmacs Lisp, an obarray is actually a vector.  Each element of the
727 vector is a bucket; its value is either an interned symbol whose name
728 hashes to that bucket, or 0 if the bucket is empty.  Each interned
729 symbol has an internal link (invisible to the user) to the next symbol
730 in the bucket.  Because these links are invisible, there is no way to
731 find all the symbols in an obarray except using `mapatoms' (below).
732 The order of symbols in a bucket is not significant.
733
734    In an empty obarray, every element is 0, and you can create an
735 obarray with `(make-vector LENGTH 0)'.  *This is the only valid way to
736 create an obarray.*  Prime numbers as lengths tend to result in good
737 hashing; lengths one less than a power of two are also good.
738
739    *Do not try to put symbols in an obarray yourself.*  This does not
740 work--only `intern' can enter a symbol in an obarray properly.  *Do not
741 try to intern one symbol in two obarrays.*  This would garble both
742 obarrays, because a symbol has just one slot to hold the following
743 symbol in the obarray bucket.  The results would be unpredictable.
744
745    It is possible for two different symbols to have the same name in
746 different obarrays; these symbols are not `eq' or `equal'.  However,
747 this normally happens only as part of the abbrev mechanism (*note
748 Abbrevs::).
749
750      Common Lisp note: In Common Lisp, a single symbol may be interned
751      in several obarrays.
752
753    Most of the functions below take a name and sometimes an obarray as
754 arguments.  A `wrong-type-argument' error is signaled if the name is
755 not a string, or if the obarray is not a vector.
756
757  - Function: symbol-name symbol
758      This function returns the string that is SYMBOL's name.  For
759      example:
760
761           (symbol-name 'foo)
762                => "foo"
763
764      Changing the string by substituting characters, etc, does change
765      the name of the symbol, but fails to update the obarray, so don't
766      do it!
767
768  - Function: make-symbol name
769      This function returns a newly-allocated, uninterned symbol whose
770      name is NAME (which must be a string).  Its value and function
771      definition are void, and its property list is `nil'.  In the
772      example below, the value of `sym' is not `eq' to `foo' because it
773      is a distinct uninterned symbol whose name is also `foo'.
774
775           (setq sym (make-symbol "foo"))
776                => foo
777           (eq sym 'foo)
778                => nil
779
780  - Function: intern name &optional obarray
781      This function returns the interned symbol whose name is NAME.  If
782      there is no such symbol in the obarray OBARRAY, `intern' creates a
783      new one, adds it to the obarray, and returns it.  If OBARRAY is
784      omitted, the value of the global variable `obarray' is used.
785
786           (setq sym (intern "foo"))
787                => foo
788           (eq sym 'foo)
789                => t
790           
791           (setq sym1 (intern "foo" other-obarray))
792                => foo
793           (eq sym 'foo)
794                => nil
795
796  - Function: intern-soft name &optional obarray
797      This function returns the symbol in OBARRAY whose name is NAME, or
798      `nil' if OBARRAY has no symbol with that name.  Therefore, you can
799      use `intern-soft' to test whether a symbol with a given name is
800      already interned.  If OBARRAY is omitted, the value of the global
801      variable `obarray' is used.
802
803           (intern-soft "frazzle")        ; No such symbol exists.
804                => nil
805           (make-symbol "frazzle")        ; Create an uninterned one.
806                => frazzle
807           (intern-soft "frazzle")        ; That one cannot be found.
808                => nil
809           (setq sym (intern "frazzle"))  ; Create an interned one.
810                => frazzle
811           (intern-soft "frazzle")        ; That one can be found!
812                => frazzle
813           (eq sym 'frazzle)              ; And it is the same one.
814                => t
815
816  - Variable: obarray
817      This variable is the standard obarray for use by `intern' and
818      `read'.
819
820  - Function: mapatoms function &optional obarray
821      This function calls FUNCTION for each symbol in the obarray
822      OBARRAY.  It returns `nil'.  If OBARRAY is omitted, it defaults to
823      the value of `obarray', the standard obarray for ordinary symbols.
824
825           (setq count 0)
826                => 0
827           (defun count-syms (s)
828             (setq count (1+ count)))
829                => count-syms
830           (mapatoms 'count-syms)
831                => nil
832           count
833                => 1871
834
835      See `documentation' in *Note Accessing Documentation::, for another
836      example using `mapatoms'.
837
838  - Function: unintern symbol &optional obarray
839      This function deletes SYMBOL from the obarray OBARRAY.  If
840      `symbol' is not actually in the obarray, `unintern' does nothing.
841      If OBARRAY is `nil', the current obarray is used.
842
843      If you provide a string instead of a symbol as SYMBOL, it stands
844      for a symbol name.  Then `unintern' deletes the symbol (if any) in
845      the obarray which has that name.  If there is no such symbol,
846      `unintern' does nothing.
847
848      If `unintern' does delete a symbol, it returns `t'.  Otherwise it
849      returns `nil'.
850
851 \1f
852 File: lispref.info,  Node: Symbol Properties,  Prev: Creating Symbols,  Up: Symbols
853
854 Symbol Properties
855 =================
856
857    A "property list" ("plist" for short) is a list of paired elements,
858 often stored in the property list cell of a symbol.  Each of the pairs
859 associates a property name (usually a symbol) with a property or value.
860 Property lists are generally used to record information about a
861 symbol, such as its documentation as a variable, the name of the file
862 where it was defined, or perhaps even the grammatical class of the
863 symbol (representing a word) in a language-understanding system.
864
865    Some objects which are not symbols also have property lists
866 associated with them, and XEmacs provides a full complement of
867 functions for working with property lists.  *Note Property Lists::.
868
869    The property names and values in a property list can be any Lisp
870 objects, but the names are usually symbols.  They are compared using
871 `eq'.  Here is an example of a property list, found on the symbol
872 `progn' when the compiler is loaded:
873
874      (lisp-indent-function 0 byte-compile byte-compile-progn)
875
876 Here `lisp-indent-function' and `byte-compile' are property names, and
877 the other two elements are the corresponding values.
878
879 * Menu:
880
881 * Plists and Alists::           Comparison of the advantages of property
882                                   lists and association lists.
883 * Object Plists::               Functions to access objects' property lists.
884 * Other Plists::                Accessing property lists stored elsewhere.
885
886 \1f
887 File: lispref.info,  Node: Plists and Alists,  Next: Object Plists,  Up: Symbol Properties
888
889 Property Lists and Association Lists
890 ------------------------------------
891
892    Association lists (*note Association Lists::) are very similar to
893 property lists.  In contrast to association lists, the order of the
894 pairs in the property list is not significant since the property names
895 must be distinct.
896
897    Property lists are better than association lists for attaching
898 information to various Lisp function names or variables.  If all the
899 associations are recorded in one association list, the program will need
900 to search that entire list each time a function or variable is to be
901 operated on.  By contrast, if the information is recorded in the
902 property lists of the function names or variables themselves, each
903 search will scan only the length of one property list, which is usually
904 short.  This is why the documentation for a variable is recorded in a
905 property named `variable-documentation'.  The byte compiler likewise
906 uses properties to record those functions needing special treatment.
907
908    However, association lists have their own advantages.  Depending on
909 your application, it may be faster to add an association to the front of
910 an association list than to update a property.  All properties for a
911 symbol are stored in the same property list, so there is a possibility
912 of a conflict between different uses of a property name.  (For this
913 reason, it is a good idea to choose property names that are probably
914 unique, such as by including the name of the library in the property
915 name.)  An association list may be used like a stack where associations
916 are pushed on the front of the list and later discarded; this is not
917 possible with a property list.
918
919 \1f
920 File: lispref.info,  Node: Object Plists,  Next: Other Plists,  Prev: Plists and Alists,  Up: Symbol Properties
921
922 Property List Functions for Objects
923 -----------------------------------
924
925    Once upon a time, only symbols had property lists.  Now, several
926 other object types, including strings, extents, faces and glyphs also
927 have property lists.
928
929  - Function: symbol-plist symbol
930      This function returns the property list of SYMBOL.
931
932  - Function: object-plist object
933      This function returns the property list of OBJECT.  If OBJECT is a
934      symbol, this is identical to `symbol-plist'.
935
936  - Function: setplist symbol plist
937      This function sets SYMBOL's property list to PLIST.  Normally,
938      PLIST should be a well-formed property list, but this is not
939      enforced.
940
941           (setplist 'foo '(a 1 b (2 3) c nil))
942                => (a 1 b (2 3) c nil)
943           (symbol-plist 'foo)
944                => (a 1 b (2 3) c nil)
945
946      For symbols in special obarrays, which are not used for ordinary
947      purposes, it may make sense to use the property list cell in a
948      nonstandard fashion; in fact, the abbrev mechanism does so (*note
949      Abbrevs::).  But generally, its use is discouraged.  Use `put'
950      instead.  `setplist' can only be used with symbols, not other
951      object types.
952
953  - Function: get object property &optional default
954      This function finds the value of the property named PROPERTY in
955      OBJECT's property list.  If there is no such property, `default'
956      (which itself defaults to `nil') is returned.
957
958      PROPERTY is compared with the existing properties using `eq', so
959      any object is a legitimate property.
960
961      See `put' for an example.
962
963  - Function: put object property value
964      This function puts VALUE onto OBJECT's property list under the
965      property name PROPERTY, replacing any previous property value.
966      The `put' function returns VALUE.
967
968           (put 'fly 'verb 'transitive)
969                =>'transitive
970           (put 'fly 'noun '(a buzzing little bug))
971                => (a buzzing little bug)
972           (get 'fly 'verb)
973                => transitive
974           (object-plist 'fly)
975                => (verb transitive noun (a buzzing little bug))
976
977  - Function: remprop object property
978      This function removes the entry for PROPERTY from the property
979      list of OBJECT.  It returns `t' if the property was indeed found
980      and removed, or `nil' if there was no such property.  (This
981      function was probably omitted from Emacs originally because, since
982      `get' did not allow a DEFAULT, it was very difficult to
983      distinguish between a missing property and a property whose value
984      was `nil'; thus, setting a property to `nil' was close enough to
985      `remprop' for most purposes.)
986
987 \1f
988 File: lispref.info,  Node: Other Plists,  Prev: Object Plists,  Up: Symbol Properties
989
990 Property Lists Not Associated with Objects
991 ------------------------------------------
992
993    These functions are useful for manipulating property lists that are
994 stored in places other than symbols:
995
996  - Function: getf plist property &optional default
997      This returns the value of the PROPERTY property stored in the
998      property list PLIST.  For example,
999
1000           (getf '(foo 4) 'foo)
1001                => 4
1002
1003  - Macro: putf plist property value
1004      This stores VALUE as the value of the PROPERTY property in the
1005      property list PLIST.  It may modify PLIST destructively, or it may
1006      construct a new list structure without altering the old.  The
1007      function returns the modified property list, so you can store that
1008      back in the place where you got PLIST.  For example,
1009
1010           (setq my-plist '(bar t foo 4))
1011                => (bar t foo 4)
1012           (setq my-plist (putf my-plist 'foo 69))
1013                => (bar t foo 69)
1014           (setq my-plist (putf my-plist 'quux '(a)))
1015                => (quux (a) bar t foo 5)
1016
1017  - Function: plists-eq a b
1018      This function returns non-`nil' if property lists A and B are
1019      `eq'.  This means that the property lists have the same values for
1020      all the same properties, where comparison between values is done
1021      using `eq'.
1022
1023  - Function: plists-equal a b
1024      This function returns non-`nil' if property lists A and B are
1025      `equal'.
1026
1027    Both of the above functions do order-insensitive comparisons.
1028
1029      (plists-eq '(a 1 b 2 c nil) '(b 2 a 1))
1030           => t
1031      (plists-eq '(foo "hello" bar "goodbye") '(bar "goodbye" foo "hello"))
1032           => nil
1033      (plists-equal '(foo "hello" bar "goodbye") '(bar "goodbye" foo "hello"))
1034           => t
1035
1036 \1f
1037 File: lispref.info,  Node: Evaluation,  Next: Control Structures,  Prev: Symbols,  Up: Top
1038
1039 Evaluation
1040 **********
1041
1042    The "evaluation" of expressions in XEmacs Lisp is performed by the
1043 "Lisp interpreter"--a program that receives a Lisp object as input and
1044 computes its "value as an expression".  How it does this depends on the
1045 data type of the object, according to rules described in this chapter.
1046 The interpreter runs automatically to evaluate portions of your
1047 program, but can also be called explicitly via the Lisp primitive
1048 function `eval'.
1049
1050 * Menu:
1051
1052 * Intro Eval::  Evaluation in the scheme of things.
1053 * Eval::        How to invoke the Lisp interpreter explicitly.
1054 * Forms::       How various sorts of objects are evaluated.
1055 * Quoting::     Avoiding evaluation (to put constants in the program).
1056
1057 \1f
1058 File: lispref.info,  Node: Intro Eval,  Next: Eval,  Up: Evaluation
1059
1060 Introduction to Evaluation
1061 ==========================
1062
1063    The Lisp interpreter, or evaluator, is the program that computes the
1064 value of an expression that is given to it.  When a function written in
1065 Lisp is called, the evaluator computes the value of the function by
1066 evaluating the expressions in the function body.  Thus, running any
1067 Lisp program really means running the Lisp interpreter.
1068
1069    How the evaluator handles an object depends primarily on the data
1070 type of the object.
1071
1072    A Lisp object that is intended for evaluation is called an
1073 "expression" or a "form".  The fact that expressions are data objects
1074 and not merely text is one of the fundamental differences between
1075 Lisp-like languages and typical programming languages.  Any object can
1076 be evaluated, but in practice only numbers, symbols, lists and strings
1077 are evaluated very often.
1078
1079    It is very common to read a Lisp expression and then evaluate the
1080 expression, but reading and evaluation are separate activities, and
1081 either can be performed alone.  Reading per se does not evaluate
1082 anything; it converts the printed representation of a Lisp object to the
1083 object itself.  It is up to the caller of `read' whether this object is
1084 a form to be evaluated, or serves some entirely different purpose.
1085 *Note Input Functions::.
1086
1087    Do not confuse evaluation with command key interpretation.  The
1088 editor command loop translates keyboard input into a command (an
1089 interactively callable function) using the active keymaps, and then
1090 uses `call-interactively' to invoke the command.  The execution of the
1091 command itself involves evaluation if the command is written in Lisp,
1092 but that is not a part of command key interpretation itself.  *Note
1093 Command Loop::.
1094
1095    Evaluation is a recursive process.  That is, evaluation of a form may
1096 call `eval' to evaluate parts of the form.  For example, evaluation of
1097 a function call first evaluates each argument of the function call, and
1098 then evaluates each form in the function body.  Consider evaluation of
1099 the form `(car x)': the subform `x' must first be evaluated
1100 recursively, so that its value can be passed as an argument to the
1101 function `car'.
1102
1103    Evaluation of a function call ultimately calls the function specified
1104 in it.  *Note Functions::.  The execution of the function may itself
1105 work by evaluating the function definition; or the function may be a
1106 Lisp primitive implemented in C, or it may be a byte-compiled function
1107 (*note Byte Compilation::).
1108
1109    The evaluation of forms takes place in a context called the
1110 "environment", which consists of the current values and bindings of all
1111 Lisp variables.(1)  Whenever the form refers to a variable without
1112 creating a new binding for it, the value of the binding in the current
1113 environment is used.  *Note Variables::.
1114
1115    Evaluation of a form may create new environments for recursive
1116 evaluation by binding variables (*note Local Variables::).  These
1117 environments are temporary and vanish by the time evaluation of the form
1118 is complete.  The form may also make changes that persist; these changes
1119 are called "side effects".  An example of a form that produces side
1120 effects is `(setq foo 1)'.
1121
1122    The details of what evaluation means for each kind of form are
1123 described below (*note Forms::).
1124
1125    ---------- Footnotes ----------
1126
1127    (1) This definition of "environment" is specifically not intended to
1128 include all the data that can affect the result of a program.
1129
1130 \1f
1131 File: lispref.info,  Node: Eval,  Next: Forms,  Prev: Intro Eval,  Up: Evaluation
1132
1133 Eval
1134 ====
1135
1136    Most often, forms are evaluated automatically, by virtue of their
1137 occurrence in a program being run.  On rare occasions, you may need to
1138 write code that evaluates a form that is computed at run time, such as
1139 after reading a form from text being edited or getting one from a
1140 property list.  On these occasions, use the `eval' function.
1141
1142    *Please note:* it is generally cleaner and more flexible to call
1143 functions that are stored in data structures, rather than to evaluate
1144 expressions stored in data structures.  Using functions provides the
1145 ability to pass information to them as arguments.
1146
1147    The functions and variables described in this section evaluate forms,
1148 specify limits to the evaluation process, or record recently returned
1149 values.  Loading a file also does evaluation (*note Loading::).
1150
1151  - Function: eval form
1152      This is the basic function for performing evaluation.  It evaluates
1153      FORM in the current environment and returns the result.  How the
1154      evaluation proceeds depends on the type of the object (*note
1155      Forms::).
1156
1157      Since `eval' is a function, the argument expression that appears
1158      in a call to `eval' is evaluated twice: once as preparation before
1159      `eval' is called, and again by the `eval' function itself.  Here
1160      is an example:
1161
1162           (setq foo 'bar)
1163                => bar
1164           (setq bar 'baz)
1165                => baz
1166           ;; `eval' receives argument `bar', which is the value of `foo'
1167           (eval foo)
1168                => baz
1169           (eval 'foo)
1170                => bar
1171
1172      The number of currently active calls to `eval' is limited to
1173      `max-lisp-eval-depth' (see below).
1174
1175  - Command: eval-region start end &optional stream
1176      This function evaluates the forms in the current buffer in the
1177      region defined by the positions START and END.  It reads forms from
1178      the region and calls `eval' on them until the end of the region is
1179      reached, or until an error is signaled and not handled.
1180
1181      If STREAM is supplied, `standard-output' is bound to it during the
1182      evaluation.
1183
1184      You can use the variable `load-read-function' to specify a function
1185      for `eval-region' to use instead of `read' for reading
1186      expressions.  *Note How Programs Do Loading::.
1187
1188      `eval-region' always returns `nil'.
1189
1190  - Command: eval-buffer buffer &optional stream
1191      This is like `eval-region' except that it operates on the whole
1192      contents of BUFFER.
1193
1194  - Variable: max-lisp-eval-depth
1195      This variable defines the maximum depth allowed in calls to `eval',
1196      `apply', and `funcall' before an error is signaled (with error
1197      message `"Lisp nesting exceeds max-lisp-eval-depth"').  This counts
1198      internal uses of those functions, such as for calling the functions
1199      mentioned in Lisp expressions, and recursive evaluation of
1200      function call arguments and function body forms.
1201
1202      This limit, with the associated error when it is exceeded, is one
1203      way that Lisp avoids infinite recursion on an ill-defined function.
1204
1205      The default value of this variable is 500.  If you set it to a
1206      value less than 100, Lisp will reset it to 100 if the given value
1207      is reached.
1208
1209      `max-specpdl-size' provides another limit on nesting.  *Note Local
1210      Variables::.
1211
1212  - Variable: values
1213      The value of this variable is a list of the values returned by all
1214      the expressions that were read from buffers (including the
1215      minibuffer), evaluated, and printed.  The elements are ordered
1216      most recent first.
1217
1218           (setq x 1)
1219                => 1
1220           (list 'A (1+ 2) auto-save-default)
1221                => (A 3 t)
1222           values
1223                => ((A 3 t) 1 ...)
1224
1225      This variable is useful for referring back to values of forms
1226      recently evaluated.  It is generally a bad idea to print the value
1227      of `values' itself, since this may be very long.  Instead, examine
1228      particular elements, like this:
1229
1230           ;; Refer to the most recent evaluation result.
1231           (nth 0 values)
1232                => (A 3 t)
1233           ;; That put a new element on,
1234           ;;   so all elements move back one.
1235           (nth 1 values)
1236                => (A 3 t)
1237           ;; This gets the element that was next-to-most-recent
1238           ;;   before this example.
1239           (nth 3 values)
1240                => 1
1241
1242 \1f
1243 File: lispref.info,  Node: Forms,  Next: Quoting,  Prev: Eval,  Up: Evaluation
1244
1245 Kinds of Forms
1246 ==============
1247
1248    A Lisp object that is intended to be evaluated is called a "form".
1249 How XEmacs evaluates a form depends on its data type.  XEmacs has three
1250 different kinds of form that are evaluated differently: symbols, lists,
1251 and "all other types".  This section describes all three kinds,
1252 starting with "all other types" which are self-evaluating forms.
1253
1254 * Menu:
1255
1256 * Self-Evaluating Forms::   Forms that evaluate to themselves.
1257 * Symbol Forms::            Symbols evaluate as variables.
1258 * Classifying Lists::       How to distinguish various sorts of list forms.
1259 * Function Indirection::    When a symbol appears as the car of a list,
1260                               we find the real function via the symbol.
1261 * Function Forms::          Forms that call functions.
1262 * Macro Forms::             Forms that call macros.
1263 * Special Forms::           ``Special forms'' are idiosyncratic primitives,
1264                               most of them extremely important.
1265 * Autoloading::             Functions set up to load files
1266                               containing their real definitions.
1267