XEmacs 21.2.29 "Hestia".
[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 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 function
639 written in Lisp would have a lambda expression (or a byte-code object)
640 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           (setq sym (intern "frazzle"))  ; Create an interned one.
809                => frazzle
810           (intern-soft "frazzle")        ; That one can be found!
811                => frazzle
812           (eq sym 'frazzle)              ; And it is the same one.
813                => t
814
815  - Variable: obarray
816      This variable is the standard obarray for use by `intern' and
817      `read'.
818
819  - Function: mapatoms function &optional obarray
820      This function calls FUNCTION for each symbol in the obarray
821      OBARRAY.  It returns `nil'.  If OBARRAY is omitted, it defaults to
822      the value of `obarray', the standard obarray for ordinary symbols.
823
824           (setq count 0)
825                => 0
826           (defun count-syms (s)
827             (setq count (1+ count)))
828                => count-syms
829           (mapatoms 'count-syms)
830                => nil
831           count
832                => 1871
833
834      See `documentation' in *Note Accessing Documentation::, for another
835      example using `mapatoms'.
836
837  - Function: unintern symbol &optional obarray
838      This function deletes SYMBOL from the obarray OBARRAY.  If
839      `symbol' is not actually in the obarray, `unintern' does nothing.
840      If OBARRAY is `nil', the current obarray is used.
841
842      If you provide a string instead of a symbol as SYMBOL, it stands
843      for a symbol name.  Then `unintern' deletes the symbol (if any) in
844      the obarray which has that name.  If there is no such symbol,
845      `unintern' does nothing.
846
847      If `unintern' does delete a symbol, it returns `t'.  Otherwise it
848      returns `nil'.
849
850 \1f
851 File: lispref.info,  Node: Symbol Properties,  Prev: Creating Symbols,  Up: Symbols
852
853 Symbol Properties
854 =================
855
856    A "property list" ("plist" for short) is a list of paired elements,
857 often stored in the property list cell of a symbol.  Each of the pairs
858 associates a property name (usually a symbol) with a property or value.
859 Property lists are generally used to record information about a
860 symbol, such as its documentation as a variable, the name of the file
861 where it was defined, or perhaps even the grammatical class of the
862 symbol (representing a word) in a language-understanding system.
863
864    Some objects which are not symbols also have property lists
865 associated with them, and XEmacs provides a full complement of
866 functions for working with property lists.  *Note Property Lists::.
867
868    The property names and values in a property list can be any Lisp
869 objects, but the names are usually symbols.  They are compared using
870 `eq'.  Here is an example of a property list, found on the symbol
871 `progn' when the compiler is loaded:
872
873      (lisp-indent-function 0 byte-compile byte-compile-progn)
874
875 Here `lisp-indent-function' and `byte-compile' are property names, and
876 the other two elements are the corresponding values.
877
878 * Menu:
879
880 * Plists and Alists::           Comparison of the advantages of property
881                                   lists and association lists.
882 * Object Plists::               Functions to access objects' property lists.
883 * Other Plists::                Accessing property lists stored elsewhere.
884
885 \1f
886 File: lispref.info,  Node: Plists and Alists,  Next: Object Plists,  Up: Symbol Properties
887
888 Property Lists and Association Lists
889 ------------------------------------
890
891    Association lists (*note Association Lists::) are very similar to
892 property lists.  In contrast to association lists, the order of the
893 pairs in the property list is not significant since the property names
894 must be distinct.
895
896    Property lists are better than association lists for attaching
897 information to various Lisp function names or variables.  If all the
898 associations are recorded in one association list, the program will need
899 to search that entire list each time a function or variable is to be
900 operated on.  By contrast, if the information is recorded in the
901 property lists of the function names or variables themselves, each
902 search will scan only the length of one property list, which is usually
903 short.  This is why the documentation for a variable is recorded in a
904 property named `variable-documentation'.  The byte compiler likewise
905 uses properties to record those functions needing special treatment.
906
907    However, association lists have their own advantages.  Depending on
908 your application, it may be faster to add an association to the front of
909 an association list than to update a property.  All properties for a
910 symbol are stored in the same property list, so there is a possibility
911 of a conflict between different uses of a property name.  (For this
912 reason, it is a good idea to choose property names that are probably
913 unique, such as by including the name of the library in the property
914 name.)  An association list may be used like a stack where associations
915 are pushed on the front of the list and later discarded; this is not
916 possible with a property list.
917
918 \1f
919 File: lispref.info,  Node: Object Plists,  Next: Other Plists,  Prev: Plists and Alists,  Up: Symbol Properties
920
921 Property List Functions for Objects
922 -----------------------------------
923
924    Once upon a time, only symbols had property lists.  Now, several
925 other object types, including strings, extents, faces and glyphs also
926 have property lists.
927
928  - Function: symbol-plist symbol
929      This function returns the property list of SYMBOL.
930
931  - Function: object-plist object
932      This function returns the property list of OBJECT.  If OBJECT is a
933      symbol, this is identical to `symbol-plist'.
934
935  - Function: setplist symbol plist
936      This function sets SYMBOL's property list to PLIST.  Normally,
937      PLIST should be a well-formed property list, but this is not
938      enforced.
939
940           (setplist 'foo '(a 1 b (2 3) c nil))
941                => (a 1 b (2 3) c nil)
942           (symbol-plist 'foo)
943                => (a 1 b (2 3) c nil)
944
945      For symbols in special obarrays, which are not used for ordinary
946      purposes, it may make sense to use the property list cell in a
947      nonstandard fashion; in fact, the abbrev mechanism does so (*note
948      Abbrevs::).  But generally, its use is discouraged.  Use `put'
949      instead.  `setplist' can only be used with symbols, not other
950      object types.
951
952  - Function: get object property &optional default
953      This function finds the value of the property named PROPERTY in
954      OBJECT's property list.  If there is no such property, `default'
955      (which itself defaults to `nil') is returned.
956
957      PROPERTY is compared with the existing properties using `eq', so
958      any object is a legitimate property.
959
960      See `put' for an example.
961
962  - Function: put object property value
963      This function puts VALUE onto OBJECT's property list under the
964      property name PROPERTY, replacing any previous property value.
965      The `put' function returns VALUE.
966
967           (put 'fly 'verb 'transitive)
968                =>'transitive
969           (put 'fly 'noun '(a buzzing little bug))
970                => (a buzzing little bug)
971           (get 'fly 'verb)
972                => transitive
973           (object-plist 'fly)
974                => (verb transitive noun (a buzzing little bug))
975
976  - Function: remprop object property
977      This function removes the entry for PROPERTY from the property
978      list of OBJECT.  It returns `t' if the property was indeed found
979      and removed, or `nil' if there was no such property.  (This
980      function was probably omitted from Emacs originally because, since
981      `get' did not allow a DEFAULT, it was very difficult to
982      distinguish between a missing property and a property whose value
983      was `nil'; thus, setting a property to `nil' was close enough to
984      `remprop' for most purposes.)
985
986 \1f
987 File: lispref.info,  Node: Other Plists,  Prev: Object Plists,  Up: Symbol Properties
988
989 Property Lists Not Associated with Objects
990 ------------------------------------------
991
992    These functions are useful for manipulating property lists that are
993 stored in places other than symbols:
994
995  - Function: getf plist property &optional default
996      This returns the value of the PROPERTY property stored in the
997      property list PLIST.  For example,
998
999           (getf '(foo 4) 'foo)
1000                => 4
1001
1002  - Function: putf plist property value
1003      This stores VALUE as the value of the PROPERTY property in the
1004      property list PLIST.  It may modify PLIST destructively, or it may
1005      construct a new list structure without altering the old.  The
1006      function returns the modified property list, so you can store that
1007      back in the place where you got PLIST.  For example,
1008
1009           (setq my-plist '(bar t foo 4))
1010                => (bar t foo 4)
1011           (setq my-plist (putf my-plist 'foo 69))
1012                => (bar t foo 69)
1013           (setq my-plist (putf my-plist 'quux '(a)))
1014                => (quux (a) bar t foo 5)
1015
1016  - Function: plists-eq a b
1017      This function returns non-`nil' if property lists A and B are
1018      `eq'.  This means that the property lists have the same values for
1019      all the same properties, where comparison between values is done
1020      using `eq'.
1021
1022  - Function: plists-equal a b
1023      This function returns non-`nil' if property lists A and B are
1024      `equal'.
1025
1026    Both of the above functions do order-insensitive comparisons.
1027
1028      (plists-eq '(a 1 b 2 c nil) '(b 2 a 1))
1029           => t
1030      (plists-eq '(foo "hello" bar "goodbye") '(bar "goodbye" foo "hello"))
1031           => nil
1032      (plists-equal '(foo "hello" bar "goodbye") '(bar "goodbye" foo "hello"))
1033           => t
1034
1035 \1f
1036 File: lispref.info,  Node: Evaluation,  Next: Control Structures,  Prev: Symbols,  Up: Top
1037
1038 Evaluation
1039 **********
1040
1041    The "evaluation" of expressions in XEmacs Lisp is performed by the
1042 "Lisp interpreter"--a program that receives a Lisp object as input and
1043 computes its "value as an expression".  How it does this depends on the
1044 data type of the object, according to rules described in this chapter.
1045 The interpreter runs automatically to evaluate portions of your
1046 program, but can also be called explicitly via the Lisp primitive
1047 function `eval'.
1048
1049 * Menu:
1050
1051 * Intro Eval::  Evaluation in the scheme of things.
1052 * Eval::        How to invoke the Lisp interpreter explicitly.
1053 * Forms::       How various sorts of objects are evaluated.
1054 * Quoting::     Avoiding evaluation (to put constants in the program).
1055
1056 \1f
1057 File: lispref.info,  Node: Intro Eval,  Next: Eval,  Up: Evaluation
1058
1059 Introduction to Evaluation
1060 ==========================
1061
1062    The Lisp interpreter, or evaluator, is the program that computes the
1063 value of an expression that is given to it.  When a function written in
1064 Lisp is called, the evaluator computes the value of the function by
1065 evaluating the expressions in the function body.  Thus, running any
1066 Lisp program really means running the Lisp interpreter.
1067
1068    How the evaluator handles an object depends primarily on the data
1069 type of the object.
1070
1071    A Lisp object that is intended for evaluation is called an
1072 "expression" or a "form".  The fact that expressions are data objects
1073 and not merely text is one of the fundamental differences between
1074 Lisp-like languages and typical programming languages.  Any object can
1075 be evaluated, but in practice only numbers, symbols, lists and strings
1076 are evaluated very often.
1077
1078    It is very common to read a Lisp expression and then evaluate the
1079 expression, but reading and evaluation are separate activities, and
1080 either can be performed alone.  Reading per se does not evaluate
1081 anything; it converts the printed representation of a Lisp object to the
1082 object itself.  It is up to the caller of `read' whether this object is
1083 a form to be evaluated, or serves some entirely different purpose.
1084 *Note Input Functions::.
1085
1086    Do not confuse evaluation with command key interpretation.  The
1087 editor command loop translates keyboard input into a command (an
1088 interactively callable function) using the active keymaps, and then
1089 uses `call-interactively' to invoke the command.  The execution of the
1090 command itself involves evaluation if the command is written in Lisp,
1091 but that is not a part of command key interpretation itself.  *Note
1092 Command Loop::.
1093
1094    Evaluation is a recursive process.  That is, evaluation of a form may
1095 call `eval' to evaluate parts of the form.  For example, evaluation of
1096 a function call first evaluates each argument of the function call, and
1097 then evaluates each form in the function body.  Consider evaluation of
1098 the form `(car x)': the subform `x' must first be evaluated
1099 recursively, so that its value can be passed as an argument to the
1100 function `car'.
1101
1102    Evaluation of a function call ultimately calls the function specified
1103 in it.  *Note Functions::.  The execution of the function may itself
1104 work by evaluating the function definition; or the function may be a
1105 Lisp primitive implemented in C, or it may be a byte-compiled function
1106 (*note Byte Compilation::).
1107
1108    The evaluation of forms takes place in a context called the
1109 "environment", which consists of the current values and bindings of all
1110 Lisp variables.(1)  Whenever the form refers to a variable without
1111 creating a new binding for it, the value of the binding in the current
1112 environment is used.  *Note Variables::.
1113
1114    Evaluation of a form may create new environments for recursive
1115 evaluation by binding variables (*note Local Variables::).  These
1116 environments are temporary and vanish by the time evaluation of the form
1117 is complete.  The form may also make changes that persist; these changes
1118 are called "side effects".  An example of a form that produces side
1119 effects is `(setq foo 1)'.
1120
1121    The details of what evaluation means for each kind of form are
1122 described below (*note Forms::).
1123
1124    ---------- Footnotes ----------
1125
1126    (1) This definition of "environment" is specifically not intended to
1127 include all the data that can affect the result of a program.
1128
1129 \1f
1130 File: lispref.info,  Node: Eval,  Next: Forms,  Prev: Intro Eval,  Up: Evaluation
1131
1132 Eval
1133 ====
1134
1135    Most often, forms are evaluated automatically, by virtue of their
1136 occurrence in a program being run.  On rare occasions, you may need to
1137 write code that evaluates a form that is computed at run time, such as
1138 after reading a form from text being edited or getting one from a
1139 property list.  On these occasions, use the `eval' function.
1140
1141    *Please note:* it is generally cleaner and more flexible to call
1142 functions that are stored in data structures, rather than to evaluate
1143 expressions stored in data structures.  Using functions provides the
1144 ability to pass information to them as arguments.
1145
1146    The functions and variables described in this section evaluate forms,
1147 specify limits to the evaluation process, or record recently returned
1148 values.  Loading a file also does evaluation (*note Loading::).
1149
1150  - Function: eval form
1151      This is the basic function for performing evaluation.  It evaluates
1152      FORM in the current environment and returns the result.  How the
1153      evaluation proceeds depends on the type of the object (*note
1154      Forms::).
1155
1156      Since `eval' is a function, the argument expression that appears
1157      in a call to `eval' is evaluated twice: once as preparation before
1158      `eval' is called, and again by the `eval' function itself.  Here
1159      is an example:
1160
1161           (setq foo 'bar)
1162                => bar
1163           (setq bar 'baz)
1164                => baz
1165           ;; `eval' receives argument `bar', which is the value of `foo'
1166           (eval foo)
1167                => baz
1168           (eval 'foo)
1169                => bar
1170
1171      The number of currently active calls to `eval' is limited to
1172      `max-lisp-eval-depth' (see below).
1173
1174  - Command: eval-region start end &optional stream
1175      This function evaluates the forms in the current buffer in the
1176      region defined by the positions START and END.  It reads forms from
1177      the region and calls `eval' on them until the end of the region is
1178      reached, or until an error is signaled and not handled.
1179
1180      If STREAM is supplied, `standard-output' is bound to it during the
1181      evaluation.
1182
1183      You can use the variable `load-read-function' to specify a function
1184      for `eval-region' to use instead of `read' for reading
1185      expressions.  *Note How Programs Do Loading::.
1186
1187      `eval-region' always returns `nil'.
1188
1189  - Command: eval-buffer buffer &optional stream
1190      This is like `eval-region' except that it operates on the whole
1191      contents of BUFFER.
1192
1193  - Variable: max-lisp-eval-depth
1194      This variable defines the maximum depth allowed in calls to `eval',
1195      `apply', and `funcall' before an error is signaled (with error
1196      message `"Lisp nesting exceeds max-lisp-eval-depth"').  This counts
1197      internal uses of those functions, such as for calling the functions
1198      mentioned in Lisp expressions, and recursive evaluation of
1199      function call arguments and function body forms.
1200
1201      This limit, with the associated error when it is exceeded, is one
1202      way that Lisp avoids infinite recursion on an ill-defined function.
1203
1204      The default value of this variable is 500.  If you set it to a
1205      value less than 100, Lisp will reset it to 100 if the given value
1206      is reached.
1207
1208      `max-specpdl-size' provides another limit on nesting.  *Note Local
1209      Variables::.
1210
1211  - Variable: values
1212      The value of this variable is a list of the values returned by all
1213      the expressions that were read from buffers (including the
1214      minibuffer), evaluated, and printed.  The elements are ordered
1215      most recent first.
1216
1217           (setq x 1)
1218                => 1
1219           (list 'A (1+ 2) auto-save-default)
1220                => (A 3 t)
1221           values
1222                => ((A 3 t) 1 ...)
1223
1224      This variable is useful for referring back to values of forms
1225      recently evaluated.  It is generally a bad idea to print the value
1226      of `values' itself, since this may be very long.  Instead, examine
1227      particular elements, like this:
1228
1229           ;; Refer to the most recent evaluation result.
1230           (nth 0 values)
1231                => (A 3 t)
1232           ;; That put a new element on,
1233           ;;   so all elements move back one.
1234           (nth 1 values)
1235                => (A 3 t)
1236           ;; This gets the element that was next-to-most-recent
1237           ;;   before this example.
1238           (nth 3 values)
1239                => 1
1240
1241 \1f
1242 File: lispref.info,  Node: Forms,  Next: Quoting,  Prev: Eval,  Up: Evaluation
1243
1244 Kinds of Forms
1245 ==============
1246
1247    A Lisp object that is intended to be evaluated is called a "form".
1248 How XEmacs evaluates a form depends on its data type.  XEmacs has three
1249 different kinds of form that are evaluated differently: symbols, lists,
1250 and "all other types".  This section describes all three kinds,
1251 starting with "all other types" which are self-evaluating forms.
1252
1253 * Menu:
1254
1255 * Self-Evaluating Forms::   Forms that evaluate to themselves.
1256 * Symbol Forms::            Symbols evaluate as variables.
1257 * Classifying Lists::       How to distinguish various sorts of list forms.
1258 * Function Indirection::    When a symbol appears as the car of a list,
1259                               we find the real function via the symbol.
1260 * Function Forms::          Forms that call functions.
1261 * Macro Forms::             Forms that call macros.
1262 * Special Forms::           ``Special forms'' are idiosyncratic primitives,
1263                               most of them extremely important.
1264 * Autoloading::             Functions set up to load files
1265                               containing their real definitions.
1266