1 This is Info file ../../info/lispref.info, produced by Makeinfo version
2 1.68 from the input file lispref.texi.
4 INFO-DIR-SECTION XEmacs Editor
6 * Lispref: (lispref). XEmacs Lisp Reference Manual.
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
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.
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.
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.
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.
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.
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.
53 File: lispref.info, Node: Sequence Functions, Next: Arrays, Up: Sequences Arrays Vectors
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.
63 - Function: sequencep OBJECT
64 Returns `t' if OBJECT is a list, vector, bit vector, or string,
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
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
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::.
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.
92 (setq x (vector 'foo bar))
94 (setq y (copy-sequence x))
101 (eq (elt x 1) (elt y 1))
104 ;; Replacing an element of one sequence.
109 ;; Modifying the inside of a shared element.
110 (setcar (aref x 1) 69)
114 ;; Creating a bit vector.
115 (bit-vector 1 0 1 1 0 1 0 0)
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.
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.
145 (char-to-string (elt "1234" 2))
150 error-->Args out of range: [1 2 3 4], 4
152 error-->Args out of range: [1 2 3 4], -1
154 This function generalizes `aref' (*note Array Functions::.) and
155 `nth' (*note List Elements::.).
158 File: lispref.info, Node: Arrays, Next: Array Functions, Prev: Sequence Functions, Up: Sequences Arrays Vectors
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
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.
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::.
183 All kinds of array share these characteristics:
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.
189 * The elements of an array may be referenced or changed with the
190 functions `aref' and `aset', respectively (*note Array
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:
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.)
202 * Strings are printed in a way that shows the contents more clearly
205 * Strings can hold extent and text properties. *Note Extents::,
206 *Note Text Properties::.
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::.
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::.
218 Similarly, when representing an array of bits, a bit vector has the
219 following advantages over a regular vector:
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.)
224 * Bit vectors are printed in a way that shows the contents more
228 File: lispref.info, Node: Array Functions, Next: Vectors, Prev: Arrays, Up: Sequences Arrays Vectors
230 Functions that Operate on Arrays
231 ================================
233 In this section, we describe the functions that accept strings,
234 vectors, and bit vectors.
236 - Function: arrayp OBJECT
237 This function returns `t' if OBJECT is an array (i.e., a string,
238 vector, or bit vector).
247 - Function: aref ARRAY INDEX
248 This function returns the INDEXth element of ARRAY. The first
249 element is at index zero.
251 (setq primes [2 3 5 7 11 13])
264 See also the function `elt', in *Note Sequence Functions::.
266 - Function: aset ARRAY INDEX OBJECT
267 This function sets the INDEXth element of ARRAY to be OBJECT. It
270 (setq w [foo bar baz])
291 If ARRAY is a string and OBJECT is not a character, a
292 `wrong-type-argument' error results.
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.
298 (setq a [a b c d e f g])
305 (setq s "When in the course")
306 => "When in the course"
308 => "------------------"
315 If ARRAY is a string and OBJECT is not a character, a
316 `wrong-type-argument' error results.
318 The general sequence functions `copy-sequence' and `length' are
319 often useful for objects known to be arrays. *Note Sequence
323 File: lispref.info, Node: Vectors, Next: Vector Functions, Prev: Array Functions, Up: Sequences Arrays Vectors
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.
337 In XEmacs Lisp, the indices of the elements of a vector start from
338 zero and count up from there.
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
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::.
350 Here are examples of these principles:
352 (setq avector [1 two '(three) "four" [five]])
353 => [1 two (quote (three)) "four" [five]]
355 => [1 two (quote (three)) "four" [five]]
356 (eq avector (eval avector))
360 File: lispref.info, Node: Vector Functions, Next: Bit Vectors, Prev: Vectors, Up: Sequences Arrays Vectors
362 Functions That Operate on Vectors
363 =================================
365 Here are some functions that relate to vectors:
367 - Function: vectorp OBJECT
368 This function returns `t' if OBJECT is a vector.
375 - Function: vector &rest OBJECTS
376 This function creates and returns a vector whose elements are the
379 (vector 'foo 23 [bar baz] "rats")
380 => [foo 23 [bar baz] "rats"]
384 - Function: make-vector LENGTH OBJECT
385 This function returns a new vector consisting of LENGTH elements,
386 each initialized to OBJECT.
388 (setq sleepy (make-vector 9 'Z))
389 => [Z Z Z Z Z Z Z Z Z]
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.
396 The value is a newly constructed vector that is not `eq' to any
399 (setq a (vconcat '(A B C) '(D E F)))
405 (vconcat [A B C] "aa" '(foo (6 7)))
406 => [A B C 97 97 foo (6 7)]
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::.).
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
422 The `append' function provides a way to convert a vector into a list
423 with the same elements (*note Building Lists::.):
425 (setq avector [1 two (quote (three)) "four" [five]])
426 => [1 two (quote (three)) "four" [five]]
428 => (1 two (quote (three)) "four" [five])
431 File: lispref.info, Node: Bit Vectors, Next: Bit Vector Functions, Prev: Vector Functions, Up: Sequences Arrays Vectors
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.
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
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
450 Bit vectors are considered constants for evaluation, like vectors,
451 strings, and numbers. *Note Self-Evaluating Forms::.
454 File: lispref.info, Node: Bit Vector Functions, Prev: Bit Vectors, Up: Sequences Arrays Vectors
456 Functions That Operate on Bit Vectors
457 =====================================
459 Here are some functions that relate to bit vectors:
461 - Function: bit-vector-p OBJECT
462 This function returns `t' if OBJECT is a bit vector.
471 - Function: bitp OBJECT
472 This function returns `t' if OBJECT is either 0 or 1.
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
479 (bit-vector 0 0 0 1 0 0 0 0 1 0)
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.
488 (setq picket-fence (make-bit-vector 9 1))
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.
497 The value is a newly constructed bit vector that is not `eq' to any
500 (setq a (bvconcat '(1 1 0) '(0 0 1)))
506 (bvconcat [1 0 0 0 0] #*111 '(0 0 0 0 1))
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
514 The `append' function provides a way to convert a bit vector into a
515 list with the same elements (*note Building Lists::.):
523 File: lispref.info, Node: Symbols, Next: Evaluation, Prev: Sequences Arrays Vectors, Up: Top
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
535 You can test whether an arbitrary Lisp object is a symbol with
538 - Function: symbolp OBJECT
539 This function returns `t' if OBJECT is a symbol, `nil' otherwise.
543 * Symbol Components:: Symbols have names, values, function definitions
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.
551 File: lispref.info, Node: Symbol Components, Next: Definitions, Up: Symbols
556 Each symbol has four components (or "cells"), each of which
557 references another object:
560 The "print name cell" holds a string that names the symbol for
561 reading and printing. See `symbol-name' in *Note Creating
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::.
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::.
580 The "property list cell" holds the property list of the symbol.
581 See `symbol-plist' in *Note Symbol Properties::.
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
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
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
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.
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'.
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':
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>
628 Because this symbol is the variable which holds the name of the file
629 being visited in the current buffer, the value cell contents we see are
630 the name of the source file of this chapter of the XEmacs Lisp Manual.
631 The property list cell contains the list `(variable-documentation
632 29529)' which tells the documentation functions where to find the
633 documentation string for the variable `buffer-file-name' in the `DOC'
634 file. (29529 is the offset from the beginning of the `DOC' file to
635 where that documentation string begins.) The function cell contains
636 the function for returning the name of the file. `buffer-file-name'
637 names a primitive function, which has no read syntax and prints in hash
638 notation (*note Primitive Function Type::.). A symbol naming a
639 function written in Lisp would have a lambda expression (or a byte-code
640 object) in this cell.
643 File: lispref.info, Node: Definitions, Next: Creating Symbols, Prev: Symbol Components, Up: Symbols
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.
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
659 `defvar' and `defconst' are special forms that define a symbol as a
660 global variable. They are documented in detail in *Note Defining
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::.
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::.
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::.
688 File: lispref.info, Node: Creating Symbols, Next: Symbol Properties, Prev: Definitions, Up: Symbols
690 Creating and Interning Symbols
691 ==============================
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.
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.
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".
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.
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
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.
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.
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.
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
749 Common Lisp note: In Common Lisp, a single symbol may be interned
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.
756 - Function: symbol-name SYMBOL
757 This function returns the string that is SYMBOL's name. For
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
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'.
774 (setq sym (make-symbol "foo"))
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.
785 (setq sym (intern "foo"))
790 (setq sym1 (intern "foo" other-obarray))
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.
802 (intern-soft "frazzle") ; No such symbol exists.
804 (make-symbol "frazzle") ; Create an uninterned one.
806 (intern-soft "frazzle") ; That one cannot be found.
809 (setq sym (intern "frazzle")) ; Create an interned one.
812 (intern-soft "frazzle") ; That one can be found!
815 (eq sym 'frazzle) ; And it is the same one.
819 This variable is the standard obarray for use by `intern' and
822 - Function: mapatoms FUNCTION &optional OBARRAY
823 This function calls FUNCTION for each symbol in the obarray
824 OBARRAY. It returns `nil'. If OBARRAY is omitted, it defaults to
825 the value of `obarray', the standard obarray for ordinary symbols.
829 (defun count-syms (s)
830 (setq count (1+ count)))
832 (mapatoms 'count-syms)
837 See `documentation' in *Note Accessing Documentation::, for another
838 example using `mapatoms'.
840 - Function: unintern SYMBOL &optional OBARRAY
841 This function deletes SYMBOL from the obarray OBARRAY. If
842 `symbol' is not actually in the obarray, `unintern' does nothing.
843 If OBARRAY is `nil', the current obarray is used.
845 If you provide a string instead of a symbol as SYMBOL, it stands
846 for a symbol name. Then `unintern' deletes the symbol (if any) in
847 the obarray which has that name. If there is no such symbol,
848 `unintern' does nothing.
850 If `unintern' does delete a symbol, it returns `t'. Otherwise it
854 File: lispref.info, Node: Symbol Properties, Prev: Creating Symbols, Up: Symbols
859 A "property list" ("plist" for short) is a list of paired elements
860 stored in the property list cell of a symbol. Each of the pairs
861 associates a property name (usually a symbol) with a property or value.
862 Property lists are generally used to record information about a
863 symbol, such as its documentation as a variable, the name of the file
864 where it was defined, or perhaps even the grammatical class of the
865 symbol (representing a word) in a language-understanding system.
867 Many objects other than symbols can have property lists associated
868 with them, and XEmacs provides a full complement of functions for
869 working with property lists. *Note Property Lists::.
871 The property names and values in a property list can be any Lisp
872 objects, but the names are usually symbols. They are compared using
873 `eq'. Here is an example of a property list, found on the symbol
874 `progn' when the compiler is loaded:
876 (lisp-indent-function 0 byte-compile byte-compile-progn)
878 Here `lisp-indent-function' and `byte-compile' are property names, and
879 the other two elements are the corresponding values.
883 * Plists and Alists:: Comparison of the advantages of property
884 lists and association lists.
885 * Symbol Plists:: Functions to access symbols' property lists.
886 * Other Plists:: Accessing property lists stored elsewhere.
889 File: lispref.info, Node: Plists and Alists, Next: Symbol Plists, Up: Symbol Properties
891 Property Lists and Association Lists
892 ------------------------------------
894 Association lists (*note Association Lists::.) are very similar to
895 property lists. In contrast to association lists, the order of the
896 pairs in the property list is not significant since the property names
899 Property lists are better than association lists for attaching
900 information to various Lisp function names or variables. If all the
901 associations are recorded in one association list, the program will need
902 to search that entire list each time a function or variable is to be
903 operated on. By contrast, if the information is recorded in the
904 property lists of the function names or variables themselves, each
905 search will scan only the length of one property list, which is usually
906 short. This is why the documentation for a variable is recorded in a
907 property named `variable-documentation'. The byte compiler likewise
908 uses properties to record those functions needing special treatment.
910 However, association lists have their own advantages. Depending on
911 your application, it may be faster to add an association to the front of
912 an association list than to update a property. All properties for a
913 symbol are stored in the same property list, so there is a possibility
914 of a conflict between different uses of a property name. (For this
915 reason, it is a good idea to choose property names that are probably
916 unique, such as by including the name of the library in the property
917 name.) An association list may be used like a stack where associations
918 are pushed on the front of the list and later discarded; this is not
919 possible with a property list.
922 File: lispref.info, Node: Symbol Plists, Next: Other Plists, Prev: Plists and Alists, Up: Symbol Properties
924 Property List Functions for Symbols
925 -----------------------------------
927 - Function: symbol-plist SYMBOL
928 This function returns the property list of SYMBOL.
930 - Function: setplist SYMBOL PLIST
931 This function sets SYMBOL's property list to PLIST. Normally,
932 PLIST should be a well-formed property list, but this is not
935 (setplist 'foo '(a 1 b (2 3) c nil))
936 => (a 1 b (2 3) c nil)
938 => (a 1 b (2 3) c nil)
940 For symbols in special obarrays, which are not used for ordinary
941 purposes, it may make sense to use the property list cell in a
942 nonstandard fashion; in fact, the abbrev mechanism does so (*note
945 - Function: get SYMBOL PROPERTY
946 This function finds the value of the property named PROPERTY in
947 SYMBOL's property list. If there is no such property, `nil' is
948 returned. Thus, there is no distinction between a value of `nil'
949 and the absence of the property.
951 The name PROPERTY is compared with the existing property names
952 using `eq', so any object is a legitimate property.
954 See `put' for an example.
956 - Function: put SYMBOL PROPERTY VALUE
957 This function puts VALUE onto SYMBOL's property list under the
958 property name PROPERTY, replacing any previous property value.
959 The `put' function returns VALUE.
961 (put 'fly 'verb 'transitive)
963 (put 'fly 'noun '(a buzzing little bug))
964 => (a buzzing little bug)
968 => (verb transitive noun (a buzzing little bug))
971 File: lispref.info, Node: Other Plists, Prev: Symbol Plists, Up: Symbol Properties
973 Property Lists Outside Symbols
974 ------------------------------
976 These functions are useful for manipulating property lists that are
977 stored in places other than symbols:
979 - Function: getf PLIST PROPERTY &optional DEFAULT
980 This returns the value of the PROPERTY property stored in the
981 property list PLIST. For example,
986 - Function: putf PLIST PROPERTY VALUE
987 This stores VALUE as the value of the PROPERTY property in the
988 property list PLIST. It may modify PLIST destructively, or it may
989 construct a new list structure without altering the old. The
990 function returns the modified property list, so you can store that
991 back in the place where you got PLIST. For example,
993 (setq my-plist '(bar t foo 4))
995 (setq my-plist (putf my-plist 'foo 69))
997 (setq my-plist (putf my-plist 'quux '(a)))
998 => (quux (a) bar t foo 5)
1000 - Function: plists-eq A B
1001 This function returns non-`nil' if property lists A and B are
1002 `eq'. This means that the property lists have the same values for
1003 all the same properties, where comparison between values is done
1006 - Function: plists-equal A B
1007 This function returns non-`nil' if property lists A and B are
1010 Both of the above functions do order-insensitive comparisons.
1012 (plists-eq '(a 1 b 2 c nil) '(b 2 a 1))
1014 (plists-eq '(foo "hello" bar "goodbye") '(bar "goodbye" foo "hello"))
1016 (plists-equal '(foo "hello" bar "goodbye") '(bar "goodbye" foo "hello"))
1020 File: lispref.info, Node: Evaluation, Next: Control Structures, Prev: Symbols, Up: Top
1025 The "evaluation" of expressions in XEmacs Lisp is performed by the
1026 "Lisp interpreter"--a program that receives a Lisp object as input and
1027 computes its "value as an expression". How it does this depends on the
1028 data type of the object, according to rules described in this chapter.
1029 The interpreter runs automatically to evaluate portions of your
1030 program, but can also be called explicitly via the Lisp primitive
1035 * Intro Eval:: Evaluation in the scheme of things.
1036 * Eval:: How to invoke the Lisp interpreter explicitly.
1037 * Forms:: How various sorts of objects are evaluated.
1038 * Quoting:: Avoiding evaluation (to put constants in the program).
1041 File: lispref.info, Node: Intro Eval, Next: Eval, Up: Evaluation
1043 Introduction to Evaluation
1044 ==========================
1046 The Lisp interpreter, or evaluator, is the program that computes the
1047 value of an expression that is given to it. When a function written in
1048 Lisp is called, the evaluator computes the value of the function by
1049 evaluating the expressions in the function body. Thus, running any
1050 Lisp program really means running the Lisp interpreter.
1052 How the evaluator handles an object depends primarily on the data
1055 A Lisp object that is intended for evaluation is called an
1056 "expression" or a "form". The fact that expressions are data objects
1057 and not merely text is one of the fundamental differences between
1058 Lisp-like languages and typical programming languages. Any object can
1059 be evaluated, but in practice only numbers, symbols, lists and strings
1060 are evaluated very often.
1062 It is very common to read a Lisp expression and then evaluate the
1063 expression, but reading and evaluation are separate activities, and
1064 either can be performed alone. Reading per se does not evaluate
1065 anything; it converts the printed representation of a Lisp object to the
1066 object itself. It is up to the caller of `read' whether this object is
1067 a form to be evaluated, or serves some entirely different purpose.
1068 *Note Input Functions::.
1070 Do not confuse evaluation with command key interpretation. The
1071 editor command loop translates keyboard input into a command (an
1072 interactively callable function) using the active keymaps, and then
1073 uses `call-interactively' to invoke the command. The execution of the
1074 command itself involves evaluation if the command is written in Lisp,
1075 but that is not a part of command key interpretation itself. *Note
1078 Evaluation is a recursive process. That is, evaluation of a form may
1079 call `eval' to evaluate parts of the form. For example, evaluation of
1080 a function call first evaluates each argument of the function call, and
1081 then evaluates each form in the function body. Consider evaluation of
1082 the form `(car x)': the subform `x' must first be evaluated
1083 recursively, so that its value can be passed as an argument to the
1086 Evaluation of a function call ultimately calls the function specified
1087 in it. *Note Functions::. The execution of the function may itself
1088 work by evaluating the function definition; or the function may be a
1089 Lisp primitive implemented in C, or it may be a byte-compiled function
1090 (*note Byte Compilation::.).
1092 The evaluation of forms takes place in a context called the
1093 "environment", which consists of the current values and bindings of all
1094 Lisp variables.(1) Whenever the form refers to a variable without
1095 creating a new binding for it, the value of the binding in the current
1096 environment is used. *Note Variables::.
1098 Evaluation of a form may create new environments for recursive
1099 evaluation by binding variables (*note Local Variables::.). These
1100 environments are temporary and vanish by the time evaluation of the form
1101 is complete. The form may also make changes that persist; these changes
1102 are called "side effects". An example of a form that produces side
1103 effects is `(setq foo 1)'.
1105 The details of what evaluation means for each kind of form are
1106 described below (*note Forms::.).
1108 ---------- Footnotes ----------
1110 (1) This definition of "environment" is specifically not intended to
1111 include all the data that can affect the result of a program.
1114 File: lispref.info, Node: Eval, Next: Forms, Prev: Intro Eval, Up: Evaluation
1119 Most often, forms are evaluated automatically, by virtue of their
1120 occurrence in a program being run. On rare occasions, you may need to
1121 write code that evaluates a form that is computed at run time, such as
1122 after reading a form from text being edited or getting one from a
1123 property list. On these occasions, use the `eval' function.
1125 *Please note:* it is generally cleaner and more flexible to call
1126 functions that are stored in data structures, rather than to evaluate
1127 expressions stored in data structures. Using functions provides the
1128 ability to pass information to them as arguments.
1130 The functions and variables described in this section evaluate forms,
1131 specify limits to the evaluation process, or record recently returned
1132 values. Loading a file also does evaluation (*note Loading::.).
1134 - Function: eval FORM
1135 This is the basic function for performing evaluation. It evaluates
1136 FORM in the current environment and returns the result. How the
1137 evaluation proceeds depends on the type of the object (*note
1140 Since `eval' is a function, the argument expression that appears
1141 in a call to `eval' is evaluated twice: once as preparation before
1142 `eval' is called, and again by the `eval' function itself. Here
1149 ;; `eval' receives argument `bar', which is the value of `foo'
1155 The number of currently active calls to `eval' is limited to
1156 `max-lisp-eval-depth' (see below).
1158 - Command: eval-region START END &optional STREAM
1159 This function evaluates the forms in the current buffer in the
1160 region defined by the positions START and END. It reads forms from
1161 the region and calls `eval' on them until the end of the region is
1162 reached, or until an error is signaled and not handled.
1164 If STREAM is supplied, `standard-output' is bound to it during the
1167 You can use the variable `load-read-function' to specify a function
1168 for `eval-region' to use instead of `read' for reading
1169 expressions. *Note How Programs Do Loading::.
1171 `eval-region' always returns `nil'.
1173 - Command: eval-buffer BUFFER &optional STREAM
1174 This is like `eval-region' except that it operates on the whole
1177 - Variable: max-lisp-eval-depth
1178 This variable defines the maximum depth allowed in calls to `eval',
1179 `apply', and `funcall' before an error is signaled (with error
1180 message `"Lisp nesting exceeds max-lisp-eval-depth"'). This counts
1181 internal uses of those functions, such as for calling the functions
1182 mentioned in Lisp expressions, and recursive evaluation of
1183 function call arguments and function body forms.
1185 This limit, with the associated error when it is exceeded, is one
1186 way that Lisp avoids infinite recursion on an ill-defined function.
1188 The default value of this variable is 500. If you set it to a
1189 value less than 100, Lisp will reset it to 100 if the given value
1192 `max-specpdl-size' provides another limit on nesting. *Note Local
1196 The value of this variable is a list of the values returned by all
1197 the expressions that were read from buffers (including the
1198 minibuffer), evaluated, and printed. The elements are ordered
1203 (list 'A (1+ 2) auto-save-default)
1208 This variable is useful for referring back to values of forms
1209 recently evaluated. It is generally a bad idea to print the value
1210 of `values' itself, since this may be very long. Instead, examine
1211 particular elements, like this:
1213 ;; Refer to the most recent evaluation result.
1216 ;; That put a new element on,
1217 ;; so all elements move back one.
1220 ;; This gets the element that was next-to-most-recent
1221 ;; before this example.
1226 File: lispref.info, Node: Forms, Next: Quoting, Prev: Eval, Up: Evaluation
1231 A Lisp object that is intended to be evaluated is called a "form".
1232 How XEmacs evaluates a form depends on its data type. XEmacs has three
1233 different kinds of form that are evaluated differently: symbols, lists,
1234 and "all other types". This section describes all three kinds,
1235 starting with "all other types" which are self-evaluating forms.
1239 * Self-Evaluating Forms:: Forms that evaluate to themselves.
1240 * Symbol Forms:: Symbols evaluate as variables.
1241 * Classifying Lists:: How to distinguish various sorts of list forms.
1242 * Function Indirection:: When a symbol appears as the car of a list,
1243 we find the real function via the symbol.
1244 * Function Forms:: Forms that call functions.
1245 * Macro Forms:: Forms that call macros.
1246 * Special Forms:: "Special forms" are idiosyncratic primitives,
1247 most of them extremely important.
1248 * Autoloading:: Functions set up to load files
1249 containing their real definitions.
1252 File: lispref.info, Node: Self-Evaluating Forms, Next: Symbol Forms, Up: Forms
1254 Self-Evaluating Forms
1255 ---------------------
1257 A "self-evaluating form" is any form that is not a list or symbol.
1258 Self-evaluating forms evaluate to themselves: the result of evaluation
1259 is the same object that was evaluated. Thus, the number 25 evaluates to
1260 25, and the string `"foo"' evaluates to the string `"foo"'. Likewise,
1261 evaluation of a vector does not cause evaluation of the elements of the
1262 vector--it returns the same vector with its contents unchanged.
1264 '123 ; An object, shown without evaluation.
1266 123 ; Evaluated as usual--result is the same.
1268 (eval '123) ; Evaluated "by hand"--result is the same.
1270 (eval (eval '123)) ; Evaluating twice changes nothing.
1273 It is common to write numbers, characters, strings, and even vectors
1274 in Lisp code, taking advantage of the fact that they self-evaluate.
1275 However, it is quite unusual to do this for types that lack a read
1276 syntax, because there's no way to write them textually. It is possible
1277 to construct Lisp expressions containing these types by means of a Lisp
1278 program. Here is an example:
1280 ;; Build an expression containing a buffer object.
1281 (setq buffer (list 'print (current-buffer)))
1282 => (print #<buffer eval.texi>)
1285 -| #<buffer eval.texi>
1286 => #<buffer eval.texi>