Commit | Line | Data |
---|---|---|
5b359918 RS |
1 | @c -*-texinfo-*- |
2 | @c This is part of the GNU Emacs Lisp Reference Manual. | |
f9f59935 | 3 | @c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998 Free Software Foundation, Inc. |
5b359918 RS |
4 | @c See the file elisp.texi for copying conditions. |
5 | @setfilename ../info/objects | |
2b3fc6c3 | 6 | @node Lisp Data Types, Numbers, Introduction, Top |
5b359918 RS |
7 | @chapter Lisp Data Types |
8 | @cindex object | |
9 | @cindex Lisp object | |
10 | @cindex type | |
11 | @cindex data type | |
12 | ||
13 | A Lisp @dfn{object} is a piece of data used and manipulated by Lisp | |
14 | programs. For our purposes, a @dfn{type} or @dfn{data type} is a set of | |
15 | possible objects. | |
16 | ||
17 | Every object belongs to at least one type. Objects of the same type | |
18 | have similar structures and may usually be used in the same contexts. | |
19 | Types can overlap, and objects can belong to two or more types. | |
20 | Consequently, we can ask whether an object belongs to a particular type, | |
21 | but not for ``the'' type of an object. | |
22 | ||
23 | @cindex primitive type | |
24 | A few fundamental object types are built into Emacs. These, from | |
25 | which all other types are constructed, are called @dfn{primitive | |
26 | types}. Each object belongs to one and only one primitive type. These | |
27 | types include @dfn{integer}, @dfn{float}, @dfn{cons}, @dfn{symbol}, | |
969fe9b5 | 28 | @dfn{string}, @dfn{vector}, @dfn{subr}, @dfn{byte-code function}, plus |
5b359918 RS |
29 | several special types, such as @dfn{buffer}, that are related to |
30 | editing. (@xref{Editing Types}.) | |
31 | ||
32 | Each primitive type has a corresponding Lisp function that checks | |
33 | whether an object is a member of that type. | |
34 | ||
35 | Note that Lisp is unlike many other languages in that Lisp objects are | |
36 | @dfn{self-typing}: the primitive type of the object is implicit in the | |
37 | object itself. For example, if an object is a vector, nothing can treat | |
38 | it as a number; Lisp knows it is a vector, not a number. | |
39 | ||
40 | In most languages, the programmer must declare the data type of each | |
41 | variable, and the type is known by the compiler but not represented in | |
42 | the data. Such type declarations do not exist in Emacs Lisp. A Lisp | |
2b3fc6c3 RS |
43 | variable can have any type of value, and it remembers whatever value |
44 | you store in it, type and all. | |
5b359918 RS |
45 | |
46 | This chapter describes the purpose, printed representation, and read | |
47 | syntax of each of the standard types in GNU Emacs Lisp. Details on how | |
48 | to use these types can be found in later chapters. | |
49 | ||
50 | @menu | |
51 | * Printed Representation:: How Lisp objects are represented as text. | |
52 | * Comments:: Comments and their formatting conventions. | |
53 | * Programming Types:: Types found in all Lisp systems. | |
54 | * Editing Types:: Types specific to Emacs. | |
55 | * Type Predicates:: Tests related to types. | |
56 | * Equality Predicates:: Tests of equality between any two objects. | |
57 | @end menu | |
58 | ||
59 | @node Printed Representation | |
60 | @comment node-name, next, previous, up | |
61 | @section Printed Representation and Read Syntax | |
62 | @cindex printed representation | |
63 | @cindex read syntax | |
64 | ||
65 | The @dfn{printed representation} of an object is the format of the | |
66 | output generated by the Lisp printer (the function @code{prin1}) for | |
67 | that object. The @dfn{read syntax} of an object is the format of the | |
68 | input accepted by the Lisp reader (the function @code{read}) for that | |
f9f59935 RS |
69 | object. @xref{Read and Print}. |
70 | ||
71 | Most objects have more than one possible read syntax. Some types of | |
969fe9b5 RS |
72 | object have no read syntax, since it may not make sense to enter objects |
73 | of these types directly in a Lisp program. Except for these cases, the | |
74 | printed representation of an object is also a read syntax for it. | |
5b359918 RS |
75 | |
76 | In other languages, an expression is text; it has no other form. In | |
77 | Lisp, an expression is primarily a Lisp object and only secondarily the | |
78 | text that is the object's read syntax. Often there is no need to | |
79 | emphasize this distinction, but you must keep it in the back of your | |
80 | mind, or you will occasionally be very confused. | |
81 | ||
82 | @cindex hash notation | |
83 | Every type has a printed representation. Some types have no read | |
969fe9b5 RS |
84 | syntax---for example, the buffer type has none. Objects of these types |
85 | are printed in @dfn{hash notation}: the characters @samp{#<} followed by | |
86 | a descriptive string (typically the type name followed by the name of | |
87 | the object), and closed with a matching @samp{>}. Hash notation cannot | |
88 | be read at all, so the Lisp reader signals the error | |
89 | @code{invalid-read-syntax} whenever it encounters @samp{#<}. | |
5b359918 RS |
90 | @kindex invalid-read-syntax |
91 | ||
92 | @example | |
93 | (current-buffer) | |
94 | @result{} #<buffer objects.texi> | |
95 | @end example | |
96 | ||
97 | When you evaluate an expression interactively, the Lisp interpreter | |
98 | first reads the textual representation of it, producing a Lisp object, | |
99 | and then evaluates that object (@pxref{Evaluation}). However, | |
100 | evaluation and reading are separate activities. Reading returns the | |
101 | Lisp object represented by the text that is read; the object may or may | |
102 | not be evaluated later. @xref{Input Functions}, for a description of | |
103 | @code{read}, the basic function for reading objects. | |
104 | ||
105 | @node Comments | |
106 | @comment node-name, next, previous, up | |
107 | @section Comments | |
108 | @cindex comments | |
109 | @cindex @samp{;} in comment | |
110 | ||
111 | A @dfn{comment} is text that is written in a program only for the sake | |
112 | of humans that read the program, and that has no effect on the meaning | |
113 | of the program. In Lisp, a semicolon (@samp{;}) starts a comment if it | |
114 | is not within a string or character constant. The comment continues to | |
115 | the end of line. The Lisp reader discards comments; they do not become | |
116 | part of the Lisp objects which represent the program within the Lisp | |
117 | system. | |
118 | ||
bfe721d1 KH |
119 | The @samp{#@@@var{count}} construct, which skips the next @var{count} |
120 | characters, is useful for program-generated comments containing binary | |
121 | data. The Emacs Lisp byte compiler uses this in its output files | |
122 | (@pxref{Byte Compilation}). It isn't meant for source files, however. | |
123 | ||
5b359918 RS |
124 | @xref{Comment Tips}, for conventions for formatting comments. |
125 | ||
126 | @node Programming Types | |
127 | @section Programming Types | |
128 | @cindex programming types | |
129 | ||
130 | There are two general categories of types in Emacs Lisp: those having | |
131 | to do with Lisp programming, and those having to do with editing. The | |
132 | former exist in many Lisp implementations, in one form or another. The | |
133 | latter are unique to Emacs Lisp. | |
134 | ||
135 | @menu | |
136 | * Integer Type:: Numbers without fractional parts. | |
137 | * Floating Point Type:: Numbers with fractional parts and with a large range. | |
138 | * Character Type:: The representation of letters, numbers and | |
139 | control characters. | |
3e099569 RS |
140 | * Symbol Type:: A multi-use object that refers to a function, |
141 | variable, or property list, and has a unique identity. | |
5b359918 | 142 | * Sequence Type:: Both lists and arrays are classified as sequences. |
2b3fc6c3 | 143 | * Cons Cell Type:: Cons cells, and lists (which are made from cons cells). |
5b359918 RS |
144 | * Array Type:: Arrays include strings and vectors. |
145 | * String Type:: An (efficient) array of characters. | |
146 | * Vector Type:: One-dimensional arrays. | |
f9f59935 RS |
147 | * Char-Table Type:: One-dimensional sparse arrays indexed by characters. |
148 | * Bool-Vector Type:: One-dimensional arrays of @code{t} or @code{nil}. | |
3e099569 RS |
149 | * Function Type:: A piece of executable code you can call from elsewhere. |
150 | * Macro Type:: A method of expanding an expression into another | |
5b359918 RS |
151 | expression, more fundamental but less pretty. |
152 | * Primitive Function Type:: A function written in C, callable from Lisp. | |
153 | * Byte-Code Type:: A function written in Lisp, then compiled. | |
154 | * Autoload Type:: A type used for automatically loading seldom-used | |
155 | functions. | |
156 | @end menu | |
157 | ||
158 | @node Integer Type | |
159 | @subsection Integer Type | |
160 | ||
0fddfa72 RS |
161 | The range of values for integers in Emacs Lisp is @minus{}134217728 to |
162 | 134217727 (28 bits; i.e., | |
5b359918 | 163 | @ifinfo |
0fddfa72 | 164 | -2**27 |
5b359918 RS |
165 | @end ifinfo |
166 | @tex | |
0fddfa72 | 167 | $-2^{27}$ |
5b359918 RS |
168 | @end tex |
169 | to | |
170 | @ifinfo | |
0fddfa72 | 171 | 2**27 - 1) |
5b359918 RS |
172 | @end ifinfo |
173 | @tex | |
0fddfa72 | 174 | $2^{28}-1$) |
5b359918 | 175 | @end tex |
0fddfa72 RS |
176 | on most machines. (Some machines may provide a wider range.) It is |
177 | important to note that the Emacs Lisp arithmetic functions do not check | |
178 | for overflow. Thus @code{(1+ 134217727)} is @minus{}134217728 on most | |
179 | machines. | |
5b359918 | 180 | |
2b3fc6c3 | 181 | The read syntax for integers is a sequence of (base ten) digits with an |
5b359918 RS |
182 | optional sign at the beginning and an optional period at the end. The |
183 | printed representation produced by the Lisp interpreter never has a | |
184 | leading @samp{+} or a final @samp{.}. | |
185 | ||
186 | @example | |
187 | @group | |
188 | -1 ; @r{The integer -1.} | |
189 | 1 ; @r{The integer 1.} | |
190 | 1. ; @r{Also The integer 1.} | |
191 | +1 ; @r{Also the integer 1.} | |
969fe9b5 | 192 | 268435457 ; @r{Also the integer 1 on a 28-bit implementation.} |
5b359918 RS |
193 | @end group |
194 | @end example | |
195 | ||
196 | @xref{Numbers}, for more information. | |
197 | ||
198 | @node Floating Point Type | |
199 | @subsection Floating Point Type | |
200 | ||
f9f59935 RS |
201 | Emacs supports floating point numbers (though there is a compilation |
202 | option to disable them). The precise range of floating point numbers is | |
203 | machine-specific. | |
5b359918 RS |
204 | |
205 | The printed representation for floating point numbers requires either | |
206 | a decimal point (with at least one digit following), an exponent, or | |
207 | both. For example, @samp{1500.0}, @samp{15e2}, @samp{15.0e2}, | |
208 | @samp{1.5e3}, and @samp{.15e4} are five ways of writing a floating point | |
209 | number whose value is 1500. They are all equivalent. | |
210 | ||
211 | @xref{Numbers}, for more information. | |
212 | ||
213 | @node Character Type | |
214 | @subsection Character Type | |
215 | @cindex @sc{ASCII} character codes | |
216 | ||
217 | A @dfn{character} in Emacs Lisp is nothing more than an integer. In | |
218 | other words, characters are represented by their character codes. For | |
219 | example, the character @kbd{A} is represented as the @w{integer 65}. | |
220 | ||
221 | Individual characters are not often used in programs. It is far more | |
222 | common to work with @emph{strings}, which are sequences composed of | |
223 | characters. @xref{String Type}. | |
224 | ||
225 | Characters in strings, buffers, and files are currently limited to the | |
f9f59935 | 226 | range of 0 to 524287---nineteen bits. But not all values in that range |
969fe9b5 RS |
227 | are valid character codes. Codes 0 through 127 are ASCII codes; the |
228 | rest are non-ASCII (@pxref{Non-ASCII Characters}). Characters that represent | |
229 | keyboard input have a much wider range, to encode modifier keys such as | |
230 | Control, Meta and Shift. | |
5b359918 RS |
231 | |
232 | @cindex read syntax for characters | |
233 | @cindex printed representation for characters | |
234 | @cindex syntax for characters | |
b6954afd RS |
235 | @cindex @samp{?} in character constant |
236 | @cindex question mark in character constant | |
5b359918 RS |
237 | Since characters are really integers, the printed representation of a |
238 | character is a decimal number. This is also a possible read syntax for | |
239 | a character, but writing characters that way in Lisp programs is a very | |
240 | bad idea. You should @emph{always} use the special read syntax formats | |
241 | that Emacs Lisp provides for characters. These syntax formats start | |
242 | with a question mark. | |
243 | ||
244 | The usual read syntax for alphanumeric characters is a question mark | |
245 | followed by the character; thus, @samp{?A} for the character | |
246 | @kbd{A}, @samp{?B} for the character @kbd{B}, and @samp{?a} for the | |
247 | character @kbd{a}. | |
248 | ||
249 | For example: | |
250 | ||
251 | @example | |
252 | ?Q @result{} 81 ?q @result{} 113 | |
253 | @end example | |
254 | ||
255 | You can use the same syntax for punctuation characters, but it is | |
2b3fc6c3 RS |
256 | often a good idea to add a @samp{\} so that the Emacs commands for |
257 | editing Lisp code don't get confused. For example, @samp{?\ } is the | |
258 | way to write the space character. If the character is @samp{\}, you | |
259 | @emph{must} use a second @samp{\} to quote it: @samp{?\\}. | |
5b359918 RS |
260 | |
261 | @cindex whitespace | |
262 | @cindex bell character | |
263 | @cindex @samp{\a} | |
264 | @cindex backspace | |
265 | @cindex @samp{\b} | |
266 | @cindex tab | |
267 | @cindex @samp{\t} | |
268 | @cindex vertical tab | |
269 | @cindex @samp{\v} | |
270 | @cindex formfeed | |
271 | @cindex @samp{\f} | |
272 | @cindex newline | |
273 | @cindex @samp{\n} | |
274 | @cindex return | |
275 | @cindex @samp{\r} | |
276 | @cindex escape | |
277 | @cindex @samp{\e} | |
278 | You can express the characters Control-g, backspace, tab, newline, | |
279 | vertical tab, formfeed, return, and escape as @samp{?\a}, @samp{?\b}, | |
280 | @samp{?\t}, @samp{?\n}, @samp{?\v}, @samp{?\f}, @samp{?\r}, @samp{?\e}, | |
f9f59935 | 281 | respectively. Thus, |
5b359918 RS |
282 | |
283 | @example | |
284 | ?\a @result{} 7 ; @r{@kbd{C-g}} | |
285 | ?\b @result{} 8 ; @r{backspace, @key{BS}, @kbd{C-h}} | |
286 | ?\t @result{} 9 ; @r{tab, @key{TAB}, @kbd{C-i}} | |
969fe9b5 | 287 | ?\n @result{} 10 ; @r{newline, @kbd{C-j}} |
5b359918 RS |
288 | ?\v @result{} 11 ; @r{vertical tab, @kbd{C-k}} |
289 | ?\f @result{} 12 ; @r{formfeed character, @kbd{C-l}} | |
290 | ?\r @result{} 13 ; @r{carriage return, @key{RET}, @kbd{C-m}} | |
291 | ?\e @result{} 27 ; @r{escape character, @key{ESC}, @kbd{C-[}} | |
292 | ?\\ @result{} 92 ; @r{backslash character, @kbd{\}} | |
293 | @end example | |
294 | ||
295 | @cindex escape sequence | |
296 | These sequences which start with backslash are also known as | |
297 | @dfn{escape sequences}, because backslash plays the role of an escape | |
298 | character; this usage has nothing to do with the character @key{ESC}. | |
299 | ||
300 | @cindex control characters | |
301 | Control characters may be represented using yet another read syntax. | |
302 | This consists of a question mark followed by a backslash, caret, and the | |
303 | corresponding non-control character, in either upper or lower case. For | |
304 | example, both @samp{?\^I} and @samp{?\^i} are valid read syntax for the | |
305 | character @kbd{C-i}, the character whose value is 9. | |
306 | ||
307 | Instead of the @samp{^}, you can use @samp{C-}; thus, @samp{?\C-i} is | |
308 | equivalent to @samp{?\^I} and to @samp{?\^i}: | |
309 | ||
310 | @example | |
311 | ?\^I @result{} 9 ?\C-I @result{} 9 | |
312 | @end example | |
313 | ||
f9f59935 RS |
314 | In strings and buffers, the only control characters allowed are those |
315 | that exist in @sc{ASCII}; but for keyboard input purposes, you can turn | |
316 | any character into a control character with @samp{C-}. The character | |
317 | codes for these non-@sc{ASCII} control characters include the | |
969fe9b5 | 318 | @tex |
bfe721d1 | 319 | $2^{26}$ |
969fe9b5 | 320 | @end tex |
bfe721d1 KH |
321 | @ifinfo |
322 | 2**26 | |
323 | @end ifinfo | |
324 | bit as well as the code for the corresponding non-control | |
5b359918 | 325 | character. Ordinary terminals have no way of generating non-@sc{ASCII} |
ebc6903b RS |
326 | control characters, but you can generate them straightforwardly using X |
327 | and other window systems. | |
5b359918 | 328 | |
bfe721d1 KH |
329 | For historical reasons, Emacs treats the @key{DEL} character as |
330 | the control equivalent of @kbd{?}: | |
5b359918 RS |
331 | |
332 | @example | |
333 | ?\^? @result{} 127 ?\C-? @result{} 127 | |
334 | @end example | |
335 | ||
bfe721d1 KH |
336 | @noindent |
337 | As a result, it is currently not possible to represent the character | |
969fe9b5 RS |
338 | @kbd{Control-?}, which is a meaningful input character under X, using |
339 | @samp{\C-}. It is not easy to change this, as various Lisp files refer | |
340 | to @key{DEL} in this way. | |
bfe721d1 | 341 | |
5b359918 RS |
342 | For representing control characters to be found in files or strings, |
343 | we recommend the @samp{^} syntax; for control characters in keyboard | |
969fe9b5 RS |
344 | input, we prefer the @samp{C-} syntax. Which one you use does not |
345 | affect the meaning of the program, but may guide the understanding of | |
346 | people who read it. | |
5b359918 RS |
347 | |
348 | @cindex meta characters | |
349 | A @dfn{meta character} is a character typed with the @key{META} | |
350 | modifier key. The integer that represents such a character has the | |
969fe9b5 | 351 | @tex |
bfe721d1 | 352 | $2^{27}$ |
969fe9b5 | 353 | @end tex |
bfe721d1 KH |
354 | @ifinfo |
355 | 2**27 | |
356 | @end ifinfo | |
357 | bit set (which on most machines makes it a negative number). We | |
5b359918 RS |
358 | use high bits for this and other modifiers to make possible a wide range |
359 | of basic character codes. | |
360 | ||
bfe721d1 | 361 | In a string, the |
969fe9b5 | 362 | @tex |
bfe721d1 | 363 | $2^{7}$ |
969fe9b5 | 364 | @end tex |
bfe721d1 KH |
365 | @ifinfo |
366 | 2**7 | |
367 | @end ifinfo | |
f9f59935 RS |
368 | bit attached to an ASCII character indicates a meta character; thus, the |
369 | meta characters that can fit in a string have codes in the range from | |
370 | 128 to 255, and are the meta versions of the ordinary @sc{ASCII} | |
371 | characters. (In Emacs versions 18 and older, this convention was used | |
372 | for characters outside of strings as well.) | |
5b359918 RS |
373 | |
374 | The read syntax for meta characters uses @samp{\M-}. For example, | |
375 | @samp{?\M-A} stands for @kbd{M-A}. You can use @samp{\M-} together with | |
2b3fc6c3 RS |
376 | octal character codes (see below), with @samp{\C-}, or with any other |
377 | syntax for a character. Thus, you can write @kbd{M-A} as @samp{?\M-A}, | |
378 | or as @samp{?\M-\101}. Likewise, you can write @kbd{C-M-b} as | |
379 | @samp{?\M-\C-b}, @samp{?\C-\M-b}, or @samp{?\M-\002}. | |
5b359918 | 380 | |
f9f59935 RS |
381 | The case of a graphic character is indicated by its character code; |
382 | for example, @sc{ASCII} distinguishes between the characters @samp{a} | |
383 | and @samp{A}. But @sc{ASCII} has no way to represent whether a control | |
384 | character is upper case or lower case. Emacs uses the | |
969fe9b5 | 385 | @tex |
bfe721d1 | 386 | $2^{25}$ |
969fe9b5 | 387 | @end tex |
bfe721d1 KH |
388 | @ifinfo |
389 | 2**25 | |
390 | @end ifinfo | |
969fe9b5 | 391 | bit to indicate that the shift key was used in typing a control |
bfe721d1 | 392 | character. This distinction is possible only when you use X terminals |
969fe9b5 | 393 | or other special terminals; ordinary terminals do not report the |
b6954afd RS |
394 | distinction to the computer in any way. The Lisp syntax for |
395 | the shift bit is @samp{\S-}; thus, @samp{?\C-\S-o} or @samp{?\C-\S-O} | |
396 | represents the shifted-control-o character. | |
5b359918 RS |
397 | |
398 | @cindex hyper characters | |
399 | @cindex super characters | |
400 | @cindex alt characters | |
401 | The X Window System defines three other modifier bits that can be set | |
402 | in a character: @dfn{hyper}, @dfn{super} and @dfn{alt}. The syntaxes | |
a9f0a989 RS |
403 | for these bits are @samp{\H-}, @samp{\s-} and @samp{\A-}. (Case is |
404 | significant in these prefixes.) Thus, @samp{?\H-\M-\A-x} represents | |
405 | @kbd{Alt-Hyper-Meta-x}. | |
969fe9b5 | 406 | @tex |
bfe721d1 KH |
407 | Numerically, the |
408 | bit values are $2^{22}$ for alt, $2^{23}$ for super and $2^{24}$ for hyper. | |
969fe9b5 | 409 | @end tex |
bfe721d1 KH |
410 | @ifinfo |
411 | Numerically, the | |
412 | bit values are 2**22 for alt, 2**23 for super and 2**24 for hyper. | |
413 | @end ifinfo | |
5b359918 | 414 | |
5b359918 RS |
415 | @cindex @samp{\} in character constant |
416 | @cindex backslash in character constant | |
417 | @cindex octal character code | |
f9f59935 RS |
418 | Finally, the most general read syntax for a character represents the |
419 | character code in either octal or hex. To use octal, write a question | |
420 | mark followed by a backslash and the octal character code (up to three | |
5b359918 RS |
421 | octal digits); thus, @samp{?\101} for the character @kbd{A}, |
422 | @samp{?\001} for the character @kbd{C-a}, and @code{?\002} for the | |
423 | character @kbd{C-b}. Although this syntax can represent any @sc{ASCII} | |
424 | character, it is preferred only when the precise octal value is more | |
425 | important than the @sc{ASCII} representation. | |
426 | ||
427 | @example | |
428 | @group | |
429 | ?\012 @result{} 10 ?\n @result{} 10 ?\C-j @result{} 10 | |
430 | ?\101 @result{} 65 ?A @result{} 65 | |
431 | @end group | |
432 | @end example | |
433 | ||
f9f59935 RS |
434 | To use hex, write a question mark followed by a backslash, @samp{x}, |
435 | and the hexadecimal character code. You can use any number of hex | |
436 | digits, so you can represent any character code in this way. | |
437 | Thus, @samp{?\x41} for the character @kbd{A}, @samp{?\x1} for the | |
ebc6903b | 438 | character @kbd{C-a}, and @code{?\x8e0} for the character |
f9f59935 | 439 | @iftex |
969fe9b5 | 440 | @samp{@`a}. |
f9f59935 RS |
441 | @end iftex |
442 | @ifinfo | |
443 | @samp{a} with grave accent. | |
444 | @end ifinfo | |
445 | ||
5b359918 RS |
446 | A backslash is allowed, and harmless, preceding any character without |
447 | a special escape meaning; thus, @samp{?\+} is equivalent to @samp{?+}. | |
448 | There is no reason to add a backslash before most characters. However, | |
449 | you should add a backslash before any of the characters | |
450 | @samp{()\|;'`"#.,} to avoid confusing the Emacs commands for editing | |
451 | Lisp code. Also add a backslash before whitespace characters such as | |
452 | space, tab, newline and formfeed. However, it is cleaner to use one of | |
453 | the easily readable escape sequences, such as @samp{\t}, instead of an | |
454 | actual whitespace character such as a tab. | |
455 | ||
2b3fc6c3 RS |
456 | @node Symbol Type |
457 | @subsection Symbol Type | |
458 | ||
459 | A @dfn{symbol} in GNU Emacs Lisp is an object with a name. The symbol | |
460 | name serves as the printed representation of the symbol. In ordinary | |
461 | use, the name is unique---no two symbols have the same name. | |
462 | ||
463 | A symbol can serve as a variable, as a function name, or to hold a | |
464 | property list. Or it may serve only to be distinct from all other Lisp | |
465 | objects, so that its presence in a data structure may be recognized | |
466 | reliably. In a given context, usually only one of these uses is | |
467 | intended. But you can use one symbol in all of these ways, | |
468 | independently. | |
469 | ||
470 | @cindex @samp{\} in symbols | |
471 | @cindex backslash in symbols | |
472 | A symbol name can contain any characters whatever. Most symbol names | |
473 | are written with letters, digits, and the punctuation characters | |
474 | @samp{-+=*/}. Such names require no special punctuation; the characters | |
475 | of the name suffice as long as the name does not look like a number. | |
476 | (If it does, write a @samp{\} at the beginning of the name to force | |
477 | interpretation as a symbol.) The characters @samp{_~!@@$%^&:<>@{@}} are | |
478 | less often used but also require no special punctuation. Any other | |
479 | characters may be included in a symbol's name by escaping them with a | |
480 | backslash. In contrast to its use in strings, however, a backslash in | |
481 | the name of a symbol simply quotes the single character that follows the | |
482 | backslash. For example, in a string, @samp{\t} represents a tab | |
483 | character; in the name of a symbol, however, @samp{\t} merely quotes the | |
969fe9b5 | 484 | letter @samp{t}. To have a symbol with a tab character in its name, you |
2b3fc6c3 RS |
485 | must actually use a tab (preceded with a backslash). But it's rare to |
486 | do such a thing. | |
487 | ||
488 | @cindex CL note---case of letters | |
489 | @quotation | |
ec221d13 | 490 | @b{Common Lisp note:} In Common Lisp, lower case letters are always |
bfe721d1 KH |
491 | ``folded'' to upper case, unless they are explicitly escaped. In Emacs |
492 | Lisp, upper case and lower case letters are distinct. | |
2b3fc6c3 RS |
493 | @end quotation |
494 | ||
495 | Here are several examples of symbol names. Note that the @samp{+} in | |
496 | the fifth example is escaped to prevent it from being read as a number. | |
bfe721d1 | 497 | This is not necessary in the sixth example because the rest of the name |
2b3fc6c3 RS |
498 | makes it invalid as a number. |
499 | ||
500 | @example | |
501 | @group | |
502 | foo ; @r{A symbol named @samp{foo}.} | |
503 | FOO ; @r{A symbol named @samp{FOO}, different from @samp{foo}.} | |
504 | char-to-string ; @r{A symbol named @samp{char-to-string}.} | |
505 | @end group | |
506 | @group | |
507 | 1+ ; @r{A symbol named @samp{1+}} | |
508 | ; @r{(not @samp{+1}, which is an integer).} | |
509 | @end group | |
510 | @group | |
511 | \+1 ; @r{A symbol named @samp{+1}} | |
512 | ; @r{(not a very readable name).} | |
513 | @end group | |
514 | @group | |
515 | \(*\ 1\ 2\) ; @r{A symbol named @samp{(* 1 2)} (a worse name).} | |
516 | @c the @'s in this next line use up three characters, hence the | |
517 | @c apparent misalignment of the comment. | |
518 | +-*/_~!@@$%^&=:<>@{@} ; @r{A symbol named @samp{+-*/_~!@@$%^&=:<>@{@}}.} | |
519 | ; @r{These characters need not be escaped.} | |
520 | @end group | |
521 | @end example | |
522 | ||
5b359918 RS |
523 | @node Sequence Type |
524 | @subsection Sequence Types | |
525 | ||
526 | A @dfn{sequence} is a Lisp object that represents an ordered set of | |
527 | elements. There are two kinds of sequence in Emacs Lisp, lists and | |
528 | arrays. Thus, an object of type list or of type array is also | |
529 | considered a sequence. | |
530 | ||
969fe9b5 RS |
531 | Arrays are further subdivided into strings, vectors, char-tables and |
532 | bool-vectors. Vectors can hold elements of any type, but string | |
533 | elements must be characters, and bool-vector elements must be @code{t} | |
534 | or @code{nil}. The characters in a string can have text properties like | |
535 | characters in a buffer (@pxref{Text Properties}); vectors and | |
536 | bool-vectors do not support text properties even when their elements | |
537 | happen to be characters. Char-tables are like vectors except that they | |
538 | are indexed by any valid character code. | |
539 | ||
540 | Lists, strings and the other array types are different, but they have | |
541 | important similarities. For example, all have a length @var{l}, and all | |
542 | have elements which can be indexed from zero to @var{l} minus one. | |
543 | Several functions, called sequence functions, accept any kind of | |
5b359918 RS |
544 | sequence. For example, the function @code{elt} can be used to extract |
545 | an element of a sequence, given its index. @xref{Sequences Arrays | |
546 | Vectors}. | |
547 | ||
969fe9b5 RS |
548 | It is generally impossible to read the same sequence twice, since |
549 | sequences are always created anew upon reading. If you read the read | |
550 | syntax for a sequence twice, you get two sequences with equal contents. | |
551 | There is one exception: the empty list @code{()} always stands for the | |
552 | same object, @code{nil}. | |
5b359918 | 553 | |
2b3fc6c3 RS |
554 | @node Cons Cell Type |
555 | @subsection Cons Cell and List Types | |
5b359918 RS |
556 | @cindex address field of register |
557 | @cindex decrement field of register | |
ebc6903b | 558 | @cindex pointers |
5b359918 | 559 | |
b6954afd RS |
560 | A @dfn{cons cell} is an object that consists of two slots, called the |
561 | @sc{car} slot and the @sc{cdr} slot. Each slot can @dfn{hold} or | |
562 | @dfn{refer to} any Lisp object. We also say that the ``the @sc{car} of | |
563 | this cons cell is'' whatever object its @sc{car} slot currently holds, | |
564 | and likewise for the @sc{cdr}. | |
565 | ||
566 | @quotation | |
567 | A note to C programmers: in Lisp, we do not distinguish between | |
568 | ``holding'' a value and ``pointing to'' the value, because pointers in | |
569 | Lisp are implicit. | |
570 | @end quotation | |
2b3fc6c3 RS |
571 | |
572 | A @dfn{list} is a series of cons cells, linked together so that the | |
ebc6903b | 573 | @sc{cdr} slot of each cons cell holds either the next cons cell or the |
2b3fc6c3 RS |
574 | empty list. @xref{Lists}, for functions that work on lists. Because |
575 | most cons cells are used as part of lists, the phrase @dfn{list | |
576 | structure} has come to refer to any structure made out of cons cells. | |
5b359918 | 577 | |
ebc6903b | 578 | The names @sc{car} and @sc{cdr} derive from the history of Lisp. The |
5b359918 RS |
579 | original Lisp implementation ran on an @w{IBM 704} computer which |
580 | divided words into two parts, called the ``address'' part and the | |
581 | ``decrement''; @sc{car} was an instruction to extract the contents of | |
582 | the address part of a register, and @sc{cdr} an instruction to extract | |
583 | the contents of the decrement. By contrast, ``cons cells'' are named | |
b6954afd | 584 | for the function @code{cons} that creates them, which in turn was named |
5b359918 RS |
585 | for its purpose, the construction of cells. |
586 | ||
587 | @cindex atom | |
588 | Because cons cells are so central to Lisp, we also have a word for | |
589 | ``an object which is not a cons cell''. These objects are called | |
590 | @dfn{atoms}. | |
591 | ||
592 | @cindex parenthesis | |
593 | The read syntax and printed representation for lists are identical, and | |
594 | consist of a left parenthesis, an arbitrary number of elements, and a | |
595 | right parenthesis. | |
596 | ||
597 | Upon reading, each object inside the parentheses becomes an element | |
598 | of the list. That is, a cons cell is made for each element. The | |
b6954afd RS |
599 | @sc{car} slot of the cons cell holds the element, and its @sc{cdr} |
600 | slot refers to the next cons cell of the list, which holds the next | |
ebc6903b | 601 | element in the list. The @sc{cdr} slot of the last cons cell is set to |
b6954afd | 602 | hold @code{nil}. |
5b359918 RS |
603 | |
604 | @cindex box diagrams, for lists | |
605 | @cindex diagrams, boxed, for lists | |
606 | A list can be illustrated by a diagram in which the cons cells are | |
ebc6903b RS |
607 | shown as pairs of boxes, like dominoes. (The Lisp reader cannot read |
608 | such an illustration; unlike the textual notation, which can be | |
609 | understood by both humans and computers, the box illustrations can be | |
610 | understood only by humans.) This picture represents the three-element | |
611 | list @code{(rose violet buttercup)}: | |
5b359918 RS |
612 | |
613 | @example | |
614 | @group | |
969fe9b5 RS |
615 | --- --- --- --- --- --- |
616 | | | |--> | | |--> | | |--> nil | |
617 | --- --- --- --- --- --- | |
5b359918 RS |
618 | | | | |
619 | | | | | |
620 | --> rose --> violet --> buttercup | |
621 | @end group | |
622 | @end example | |
623 | ||
b6954afd RS |
624 | In this diagram, each box represents a slot that can hold or refer to |
625 | any Lisp object. Each pair of boxes represents a cons cell. Each arrow | |
626 | represents a reference to a Lisp object, either an atom or another cons | |
627 | cell. | |
5b359918 | 628 | |
ebc6903b | 629 | In this example, the first box, which holds the @sc{car} of the first |
b6954afd RS |
630 | cons cell, refers to or ``holds'' @code{rose} (a symbol). The second |
631 | box, holding the @sc{cdr} of the first cons cell, refers to the next | |
ebc6903b RS |
632 | pair of boxes, the second cons cell. The @sc{car} of the second cons |
633 | cell is @code{violet}, and its @sc{cdr} is the third cons cell. The | |
634 | @sc{cdr} of the third (and last) cons cell is @code{nil}. | |
5b359918 | 635 | |
ebc6903b | 636 | Here is another diagram of the same list, @code{(rose violet |
5b359918 RS |
637 | buttercup)}, sketched in a different manner: |
638 | ||
639 | @smallexample | |
640 | @group | |
641 | --------------- ---------------- ------------------- | |
642 | | car | cdr | | car | cdr | | car | cdr | | |
643 | | rose | o-------->| violet | o-------->| buttercup | nil | | |
644 | | | | | | | | | | | |
645 | --------------- ---------------- ------------------- | |
646 | @end group | |
647 | @end smallexample | |
648 | ||
649 | @cindex @samp{(@dots{})} in lists | |
650 | @cindex @code{nil} in lists | |
651 | @cindex empty list | |
652 | A list with no elements in it is the @dfn{empty list}; it is identical | |
653 | to the symbol @code{nil}. In other words, @code{nil} is both a symbol | |
654 | and a list. | |
655 | ||
656 | Here are examples of lists written in Lisp syntax: | |
657 | ||
658 | @example | |
659 | (A 2 "A") ; @r{A list of three elements.} | |
660 | () ; @r{A list of no elements (the empty list).} | |
661 | nil ; @r{A list of no elements (the empty list).} | |
662 | ("A ()") ; @r{A list of one element: the string @code{"A ()"}.} | |
663 | (A ()) ; @r{A list of two elements: @code{A} and the empty list.} | |
664 | (A nil) ; @r{Equivalent to the previous.} | |
665 | ((A B C)) ; @r{A list of one element} | |
666 | ; @r{(which is a list of three elements).} | |
667 | @end example | |
668 | ||
669 | Here is the list @code{(A ())}, or equivalently @code{(A nil)}, | |
670 | depicted with boxes and arrows: | |
671 | ||
672 | @example | |
673 | @group | |
969fe9b5 RS |
674 | --- --- --- --- |
675 | | | |--> | | |--> nil | |
676 | --- --- --- --- | |
5b359918 RS |
677 | | | |
678 | | | | |
679 | --> A --> nil | |
680 | @end group | |
681 | @end example | |
682 | ||
683 | @menu | |
684 | * Dotted Pair Notation:: An alternative syntax for lists. | |
685 | * Association List Type:: A specially constructed list. | |
686 | @end menu | |
687 | ||
688 | @node Dotted Pair Notation | |
689 | @comment node-name, next, previous, up | |
690 | @subsubsection Dotted Pair Notation | |
691 | @cindex dotted pair notation | |
692 | @cindex @samp{.} in lists | |
693 | ||
694 | @dfn{Dotted pair notation} is an alternative syntax for cons cells | |
695 | that represents the @sc{car} and @sc{cdr} explicitly. In this syntax, | |
696 | @code{(@var{a} .@: @var{b})} stands for a cons cell whose @sc{car} is | |
697 | the object @var{a}, and whose @sc{cdr} is the object @var{b}. Dotted | |
698 | pair notation is therefore more general than list syntax. In the dotted | |
699 | pair notation, the list @samp{(1 2 3)} is written as @samp{(1 . (2 . (3 | |
ebc6903b RS |
700 | . nil)))}. For @code{nil}-terminated lists, you can use either |
701 | notation, but list notation is usually clearer and more convenient. | |
702 | When printing a list, the dotted pair notation is only used if the | |
703 | @sc{cdr} of a cons cell is not a list. | |
5b359918 | 704 | |
ebc6903b RS |
705 | Here's an example using boxes to illustrate dotted pair notation. |
706 | This example shows the pair @code{(rose . violet)}: | |
5b359918 RS |
707 | |
708 | @example | |
709 | @group | |
969fe9b5 RS |
710 | --- --- |
711 | | | |--> violet | |
712 | --- --- | |
5b359918 RS |
713 | | |
714 | | | |
715 | --> rose | |
716 | @end group | |
717 | @end example | |
718 | ||
ebc6903b RS |
719 | You can combine dotted pair notation with list notation to represent |
720 | conveniently a chain of cons cells with a non-@code{nil} final @sc{cdr}. | |
721 | You write a dot after the last element of the list, followed by the | |
722 | @sc{cdr} of the final cons cell. For example, @code{(rose violet | |
723 | . buttercup)} is equivalent to @code{(rose . (violet . buttercup))}. | |
724 | The object looks like this: | |
5b359918 RS |
725 | |
726 | @example | |
727 | @group | |
969fe9b5 RS |
728 | --- --- --- --- |
729 | | | |--> | | |--> buttercup | |
730 | --- --- --- --- | |
5b359918 RS |
731 | | | |
732 | | | | |
733 | --> rose --> violet | |
734 | @end group | |
735 | @end example | |
736 | ||
ebc6903b RS |
737 | The syntax @code{(rose .@: violet .@: buttercup)} is invalid because |
738 | there is nothing that it could mean. If anything, it would say to put | |
739 | @code{buttercup} in the @sc{cdr} of a cons cell whose @sc{cdr} is already | |
740 | used for @code{violet}. | |
5b359918 | 741 | |
ebc6903b | 742 | The list @code{(rose violet)} is equivalent to @code{(rose . (violet))}, |
5b359918 RS |
743 | and looks like this: |
744 | ||
745 | @example | |
746 | @group | |
969fe9b5 RS |
747 | --- --- --- --- |
748 | | | |--> | | |--> nil | |
749 | --- --- --- --- | |
5b359918 RS |
750 | | | |
751 | | | | |
752 | --> rose --> violet | |
753 | @end group | |
754 | @end example | |
755 | ||
756 | Similarly, the three-element list @code{(rose violet buttercup)} | |
757 | is equivalent to @code{(rose . (violet . (buttercup)))}. | |
758 | @ifinfo | |
759 | It looks like this: | |
760 | ||
761 | @example | |
762 | @group | |
969fe9b5 RS |
763 | --- --- --- --- --- --- |
764 | | | |--> | | |--> | | |--> nil | |
765 | --- --- --- --- --- --- | |
5b359918 RS |
766 | | | | |
767 | | | | | |
768 | --> rose --> violet --> buttercup | |
769 | @end group | |
770 | @end example | |
771 | @end ifinfo | |
772 | ||
773 | @node Association List Type | |
774 | @comment node-name, next, previous, up | |
775 | @subsubsection Association List Type | |
776 | ||
777 | An @dfn{association list} or @dfn{alist} is a specially-constructed | |
778 | list whose elements are cons cells. In each element, the @sc{car} is | |
779 | considered a @dfn{key}, and the @sc{cdr} is considered an | |
780 | @dfn{associated value}. (In some cases, the associated value is stored | |
781 | in the @sc{car} of the @sc{cdr}.) Association lists are often used as | |
782 | stacks, since it is easy to add or remove associations at the front of | |
783 | the list. | |
784 | ||
785 | For example, | |
786 | ||
787 | @example | |
788 | (setq alist-of-colors | |
789 | '((rose . red) (lily . white) (buttercup . yellow))) | |
790 | @end example | |
791 | ||
792 | @noindent | |
793 | sets the variable @code{alist-of-colors} to an alist of three elements. In the | |
794 | first element, @code{rose} is the key and @code{red} is the value. | |
795 | ||
796 | @xref{Association Lists}, for a further explanation of alists and for | |
797 | functions that work on alists. | |
798 | ||
799 | @node Array Type | |
800 | @subsection Array Type | |
801 | ||
802 | An @dfn{array} is composed of an arbitrary number of slots for | |
b6954afd | 803 | holding or referring to other Lisp objects, arranged in a contiguous block of |
969fe9b5 RS |
804 | memory. Accessing any element of an array takes approximately the same |
805 | amount of time. In contrast, accessing an element of a list requires | |
806 | time proportional to the position of the element in the list. (Elements | |
807 | at the end of a list take longer to access than elements at the | |
808 | beginning of a list.) | |
809 | ||
810 | Emacs defines four types of array: strings, vectors, bool-vectors, and | |
811 | char-tables. | |
812 | ||
813 | A string is an array of characters and a vector is an array of | |
814 | arbitrary objects. A bool-vector can hold only @code{t} or @code{nil}. | |
815 | These kinds of array may have any length up to the largest integer. | |
816 | Char-tables are sparse arrays indexed by any valid character code; they | |
817 | can hold arbitrary objects. | |
818 | ||
819 | The first element of an array has index zero, the second element has | |
820 | index 1, and so on. This is called @dfn{zero-origin} indexing. For | |
821 | example, an array of four elements has indices 0, 1, 2, @w{and 3}. The | |
822 | largest possible index value is one less than the length of the array. | |
823 | Once an array is created, its length is fixed. | |
824 | ||
825 | All Emacs Lisp arrays are one-dimensional. (Most other programming | |
826 | languages support multidimensional arrays, but they are not essential; | |
827 | you can get the same effect with an array of arrays.) Each type of | |
828 | array has its own read syntax; see the following sections for details. | |
829 | ||
830 | The array type is contained in the sequence type and | |
831 | contains the string type, the vector type, the bool-vector type, and the | |
832 | char-table type. | |
5b359918 RS |
833 | |
834 | @node String Type | |
835 | @subsection String Type | |
836 | ||
837 | A @dfn{string} is an array of characters. Strings are used for many | |
838 | purposes in Emacs, as can be expected in a text editor; for example, as | |
839 | the names of Lisp symbols, as messages for the user, and to represent | |
840 | text extracted from buffers. Strings in Lisp are constants: evaluation | |
841 | of a string returns the same string. | |
842 | ||
f9f59935 RS |
843 | @xref{Strings and Characters}, for functions that operate on strings. |
844 | ||
845 | @menu | |
846 | * Syntax for Strings:: | |
847 | * Non-ASCII in Strings:: | |
848 | * Nonprinting Characters:: | |
849 | * Text Props and Strings:: | |
850 | @end menu | |
851 | ||
852 | @node Syntax for Strings | |
853 | @subsubsection Syntax for Strings | |
854 | ||
5b359918 RS |
855 | @cindex @samp{"} in strings |
856 | @cindex double-quote in strings | |
857 | @cindex @samp{\} in strings | |
858 | @cindex backslash in strings | |
859 | The read syntax for strings is a double-quote, an arbitrary number of | |
f9f59935 RS |
860 | characters, and another double-quote, @code{"like this"}. To include a |
861 | double-quote in a string, precede it with a backslash; thus, @code{"\""} | |
862 | is a string containing just a single double-quote character. Likewise, | |
863 | you can include a backslash by preceding it with another backslash, like | |
864 | this: @code{"this \\ is a single embedded backslash"}. | |
2b3fc6c3 | 865 | |
f9f59935 | 866 | @cindex newline in strings |
2b3fc6c3 RS |
867 | The newline character is not special in the read syntax for strings; |
868 | if you write a new line between the double-quotes, it becomes a | |
869 | character in the string. But an escaped newline---one that is preceded | |
870 | by @samp{\}---does not become part of the string; i.e., the Lisp reader | |
f9f59935 RS |
871 | ignores an escaped newline while reading a string. An escaped space |
872 | @w{@samp{\ }} is likewise ignored. | |
5b359918 RS |
873 | |
874 | @example | |
875 | "It is useful to include newlines | |
876 | in documentation strings, | |
877 | but the newline is \ | |
878 | ignored if escaped." | |
879 | @result{} "It is useful to include newlines | |
880 | in documentation strings, | |
881 | but the newline is ignored if escaped." | |
882 | @end example | |
883 | ||
f9f59935 RS |
884 | @node Non-ASCII in Strings |
885 | @subsubsection Non-ASCII Characters in Strings | |
886 | ||
887 | You can include a non-@sc{ASCII} international character in a string | |
888 | constant by writing it literally. There are two text representations | |
889 | for non-@sc{ASCII} characters in Emacs strings (and in buffers): unibyte | |
890 | and multibyte. If the string constant is read from a multibyte source, | |
969fe9b5 RS |
891 | such as a multibyte buffer or string, or a file that would be visited as |
892 | multibyte, then the character is read as a multibyte character, and that | |
893 | makes the string multibyte. If the string constant is read from a | |
894 | unibyte source, then the character is read as unibyte and that makes the | |
895 | string unibyte. | |
f9f59935 RS |
896 | |
897 | You can also represent a multibyte non-@sc{ASCII} character with its | |
898 | character code, using a hex escape, @samp{\x@var{nnnnnnn}}, with as many | |
899 | digits as necessary. (Multibyte non-@sc{ASCII} character codes are all | |
900 | greater than 256.) Any character which is not a valid hex digit | |
b6954afd RS |
901 | terminates this construct. If the next character in the string could be |
902 | interpreted as a hex digit, write @w{@samp{\ }} (backslash and space) to | |
903 | terminate the hex escape---for example, @w{@samp{\x8e0\ }} represents | |
904 | one character, @samp{a} with grave accent. @w{@samp{\ }} in a string | |
905 | constant is just like backslash-newline; it does not contribute any | |
906 | character to the string, but it does terminate the preceding hex escape. | |
f9f59935 RS |
907 | |
908 | Using a multibyte hex escape forces the string to multibyte. You can | |
909 | represent a unibyte non-@sc{ASCII} character with its character code, | |
910 | which must be in the range from 128 (0200 octal) to 255 (0377 octal). | |
911 | This forces a unibyte string. | |
912 | ||
913 | @xref{Text Representations}, for more information about the two | |
914 | text representations. | |
915 | ||
916 | @node Nonprinting Characters | |
917 | @subsubsection Nonprinting Characters in Strings | |
918 | ||
f9f59935 RS |
919 | You can use the same backslash escape-sequences in a string constant |
920 | as in character literals (but do not use the question mark that begins a | |
921 | character constant). For example, you can write a string containing the | |
a9f0a989 RS |
922 | nonprinting characters tab and @kbd{C-a}, with commas and spaces between |
923 | them, like this: @code{"\t, \C-a"}. @xref{Character Type}, for a | |
924 | description of the read syntax for characters. | |
925 | ||
926 | However, not all of the characters you can write with backslash | |
927 | escape-sequences are valid in strings. The only control characters that | |
928 | a string can hold are the @sc{ASCII} control characters. Strings do not | |
929 | distinguish case in @sc{ASCII} control characters. | |
930 | ||
931 | Properly speaking, strings cannot hold meta characters; but when a | |
932 | string is to be used as a key sequence, there is a special convention | |
ebc6903b | 933 | that provides a way to represent meta versions of @sc{ASCII} characters in a |
a9f0a989 RS |
934 | string. If you use the @samp{\M-} syntax to indicate a meta character |
935 | in a string constant, this sets the | |
969fe9b5 | 936 | @tex |
f9f59935 | 937 | $2^{7}$ |
969fe9b5 | 938 | @end tex |
f9f59935 RS |
939 | @ifinfo |
940 | 2**7 | |
941 | @end ifinfo | |
a9f0a989 RS |
942 | bit of the character in the string. If the string is used in |
943 | @code{define-key} or @code{lookup-key}, this numeric code is translated | |
944 | into the equivalent meta character. @xref{Character Type}. | |
945 | ||
946 | Strings cannot hold characters that have the hyper, super, or alt | |
947 | modifiers. | |
f9f59935 RS |
948 | |
949 | @node Text Props and Strings | |
950 | @subsubsection Text Properties in Strings | |
951 | ||
952 | A string can hold properties for the characters it contains, in | |
953 | addition to the characters themselves. This enables programs that copy | |
954 | text between strings and buffers to copy the text's properties with no | |
955 | special effort. @xref{Text Properties}, for an explanation of what text | |
956 | properties mean. Strings with text properties use a special read and | |
957 | print syntax: | |
5b359918 RS |
958 | |
959 | @example | |
960 | #("@var{characters}" @var{property-data}...) | |
961 | @end example | |
962 | ||
963 | @noindent | |
964 | where @var{property-data} consists of zero or more elements, in groups | |
965 | of three as follows: | |
966 | ||
967 | @example | |
968 | @var{beg} @var{end} @var{plist} | |
969 | @end example | |
970 | ||
971 | @noindent | |
972 | The elements @var{beg} and @var{end} are integers, and together specify | |
973 | a range of indices in the string; @var{plist} is the property list for | |
f9f59935 | 974 | that range. For example, |
5b359918 | 975 | |
f9f59935 RS |
976 | @example |
977 | #("foo bar" 0 3 (face bold) 3 4 nil 4 7 (face italic)) | |
978 | @end example | |
979 | ||
980 | @noindent | |
981 | represents a string whose textual contents are @samp{foo bar}, in which | |
982 | the first three characters have a @code{face} property with value | |
983 | @code{bold}, and the last three have a @code{face} property with value | |
ebc6903b | 984 | @code{italic}. (The fourth character has no text properties, so its |
a9f0a989 RS |
985 | property list is @code{nil}. It is not actually necessary to mention |
986 | ranges with @code{nil} as the property list, since any characters not | |
987 | mentioned in any range will default to having no properties.) | |
5b359918 RS |
988 | |
989 | @node Vector Type | |
990 | @subsection Vector Type | |
991 | ||
992 | A @dfn{vector} is a one-dimensional array of elements of any type. It | |
993 | takes a constant amount of time to access any element of a vector. (In | |
994 | a list, the access time of an element is proportional to the distance of | |
995 | the element from the beginning of the list.) | |
996 | ||
997 | The printed representation of a vector consists of a left square | |
998 | bracket, the elements, and a right square bracket. This is also the | |
999 | read syntax. Like numbers and strings, vectors are considered constants | |
1000 | for evaluation. | |
1001 | ||
1002 | @example | |
1003 | [1 "two" (three)] ; @r{A vector of three elements.} | |
1004 | @result{} [1 "two" (three)] | |
1005 | @end example | |
1006 | ||
1007 | @xref{Vectors}, for functions that work with vectors. | |
1008 | ||
f9f59935 RS |
1009 | @node Char-Table Type |
1010 | @subsection Char-Table Type | |
1011 | ||
1012 | A @dfn{char-table} is a one-dimensional array of elements of any type, | |
1013 | indexed by character codes. Char-tables have certain extra features to | |
1014 | make them more useful for many jobs that involve assigning information | |
1015 | to character codes---for example, a char-table can have a parent to | |
1016 | inherit from, a default value, and a small number of extra slots to use for | |
1017 | special purposes. A char-table can also specify a single value for | |
1018 | a whole character set. | |
1019 | ||
1020 | The printed representation of a char-table is like a vector | |
969fe9b5 | 1021 | except that there is an extra @samp{#^} at the beginning. |
f9f59935 RS |
1022 | |
1023 | @xref{Char-Tables}, for special functions to operate on char-tables. | |
969fe9b5 RS |
1024 | Uses of char-tables include: |
1025 | ||
1026 | @itemize @bullet | |
1027 | @item | |
1028 | Case tables (@pxref{Case Tables}). | |
1029 | ||
1030 | @item | |
1031 | Character category tables (@pxref{Categories}). | |
1032 | ||
1033 | @item | |
1034 | Display Tables (@pxref{Display Tables}). | |
1035 | ||
1036 | @item | |
1037 | Syntax tables (@pxref{Syntax Tables}). | |
1038 | @end itemize | |
f9f59935 RS |
1039 | |
1040 | @node Bool-Vector Type | |
1041 | @subsection Bool-Vector Type | |
1042 | ||
1043 | A @dfn{bool-vector} is a one-dimensional array of elements that | |
1044 | must be @code{t} or @code{nil}. | |
1045 | ||
1046 | The printed representation of a Bool-vector is like a string, except | |
1047 | that it begins with @samp{#&} followed by the length. The string | |
1048 | constant that follows actually specifies the contents of the bool-vector | |
1049 | as a bitmap---each ``character'' in the string contains 8 bits, which | |
1050 | specify the next 8 elements of the bool-vector (1 stands for @code{t}, | |
ebc6903b RS |
1051 | and 0 for @code{nil}). The least significant bits of the character |
1052 | correspond to the lowest indices in the bool-vector. If the length is not a | |
a9f0a989 RS |
1053 | multiple of 8, the printed representation shows extra elements, but |
1054 | these extras really make no difference. | |
f9f59935 RS |
1055 | |
1056 | @example | |
1057 | (make-bool-vector 3 t) | |
a9f0a989 | 1058 | @result{} #&3"\007" |
f9f59935 | 1059 | (make-bool-vector 3 nil) |
a9f0a989 | 1060 | @result{} #&3"\0" |
969fe9b5 | 1061 | ;; @r{These are equal since only the first 3 bits are used.} |
a9f0a989 | 1062 | (equal #&3"\377" #&3"\007") |
969fe9b5 | 1063 | @result{} t |
f9f59935 RS |
1064 | @end example |
1065 | ||
2b3fc6c3 RS |
1066 | @node Function Type |
1067 | @subsection Function Type | |
5b359918 RS |
1068 | |
1069 | Just as functions in other programming languages are executable, | |
1070 | @dfn{Lisp function} objects are pieces of executable code. However, | |
1071 | functions in Lisp are primarily Lisp objects, and only secondarily the | |
1072 | text which represents them. These Lisp objects are lambda expressions: | |
1073 | lists whose first element is the symbol @code{lambda} (@pxref{Lambda | |
1074 | Expressions}). | |
1075 | ||
1076 | In most programming languages, it is impossible to have a function | |
1077 | without a name. In Lisp, a function has no intrinsic name. A lambda | |
1078 | expression is also called an @dfn{anonymous function} (@pxref{Anonymous | |
1079 | Functions}). A named function in Lisp is actually a symbol with a valid | |
1080 | function in its function cell (@pxref{Defining Functions}). | |
1081 | ||
1082 | Most of the time, functions are called when their names are written in | |
1083 | Lisp expressions in Lisp programs. However, you can construct or obtain | |
1084 | a function object at run time and then call it with the primitive | |
1085 | functions @code{funcall} and @code{apply}. @xref{Calling Functions}. | |
1086 | ||
2b3fc6c3 RS |
1087 | @node Macro Type |
1088 | @subsection Macro Type | |
5b359918 RS |
1089 | |
1090 | A @dfn{Lisp macro} is a user-defined construct that extends the Lisp | |
1091 | language. It is represented as an object much like a function, but with | |
969fe9b5 | 1092 | different argument-passing semantics. A Lisp macro has the form of a |
5b359918 RS |
1093 | list whose first element is the symbol @code{macro} and whose @sc{cdr} |
1094 | is a Lisp function object, including the @code{lambda} symbol. | |
1095 | ||
1096 | Lisp macro objects are usually defined with the built-in | |
1097 | @code{defmacro} function, but any list that begins with @code{macro} is | |
1098 | a macro as far as Emacs is concerned. @xref{Macros}, for an explanation | |
1099 | of how to write a macro. | |
1100 | ||
f9f59935 RS |
1101 | @strong{Warning}: Lisp macros and keyboard macros (@pxref{Keyboard |
1102 | Macros}) are entirely different things. When we use the word ``macro'' | |
1103 | without qualification, we mean a Lisp macro, not a keyboard macro. | |
1104 | ||
5b359918 RS |
1105 | @node Primitive Function Type |
1106 | @subsection Primitive Function Type | |
1107 | @cindex special forms | |
1108 | ||
1109 | A @dfn{primitive function} is a function callable from Lisp but | |
1110 | written in the C programming language. Primitive functions are also | |
1111 | called @dfn{subrs} or @dfn{built-in functions}. (The word ``subr'' is | |
1112 | derived from ``subroutine''.) Most primitive functions evaluate all | |
1113 | their arguments when they are called. A primitive function that does | |
1114 | not evaluate all its arguments is called a @dfn{special form} | |
1115 | (@pxref{Special Forms}).@refill | |
1116 | ||
1117 | It does not matter to the caller of a function whether the function is | |
969fe9b5 RS |
1118 | primitive. However, this does matter if you try to redefine a primitive |
1119 | with a function written in Lisp. The reason is that the primitive | |
1120 | function may be called directly from C code. Calls to the redefined | |
1121 | function from Lisp will use the new definition, but calls from C code | |
1122 | may still use the built-in definition. Therefore, @strong{we discourage | |
1123 | redefinition of primitive functions}. | |
5b359918 RS |
1124 | |
1125 | The term @dfn{function} refers to all Emacs functions, whether written | |
2b3fc6c3 RS |
1126 | in Lisp or C. @xref{Function Type}, for information about the |
1127 | functions written in Lisp. | |
5b359918 RS |
1128 | |
1129 | Primitive functions have no read syntax and print in hash notation | |
1130 | with the name of the subroutine. | |
1131 | ||
1132 | @example | |
1133 | @group | |
1134 | (symbol-function 'car) ; @r{Access the function cell} | |
1135 | ; @r{of the symbol.} | |
1136 | @result{} #<subr car> | |
1137 | (subrp (symbol-function 'car)) ; @r{Is this a primitive function?} | |
1138 | @result{} t ; @r{Yes.} | |
1139 | @end group | |
1140 | @end example | |
1141 | ||
1142 | @node Byte-Code Type | |
1143 | @subsection Byte-Code Function Type | |
1144 | ||
1145 | The byte compiler produces @dfn{byte-code function objects}. | |
1146 | Internally, a byte-code function object is much like a vector; however, | |
1147 | the evaluator handles this data type specially when it appears as a | |
1148 | function to be called. @xref{Byte Compilation}, for information about | |
1149 | the byte compiler. | |
1150 | ||
bfe721d1 KH |
1151 | The printed representation and read syntax for a byte-code function |
1152 | object is like that for a vector, with an additional @samp{#} before the | |
1153 | opening @samp{[}. | |
5b359918 RS |
1154 | |
1155 | @node Autoload Type | |
1156 | @subsection Autoload Type | |
1157 | ||
1158 | An @dfn{autoload object} is a list whose first element is the symbol | |
1159 | @code{autoload}. It is stored as the function definition of a symbol as | |
1160 | a placeholder for the real definition; it says that the real definition | |
1161 | is found in a file of Lisp code that should be loaded when necessary. | |
1162 | The autoload object contains the name of the file, plus some other | |
1163 | information about the real definition. | |
1164 | ||
1165 | After the file has been loaded, the symbol should have a new function | |
1166 | definition that is not an autoload object. The new definition is then | |
1167 | called as if it had been there to begin with. From the user's point of | |
1168 | view, the function call works as expected, using the function definition | |
1169 | in the loaded file. | |
1170 | ||
1171 | An autoload object is usually created with the function | |
1172 | @code{autoload}, which stores the object in the function cell of a | |
1173 | symbol. @xref{Autoload}, for more details. | |
1174 | ||
1175 | @node Editing Types | |
1176 | @section Editing Types | |
1177 | @cindex editing types | |
1178 | ||
a9f0a989 | 1179 | The types in the previous section are used for general programming |
969fe9b5 RS |
1180 | purposes, and most of them are common to most Lisp dialects. Emacs Lisp |
1181 | provides several additional data types for purposes connected with | |
1182 | editing. | |
5b359918 RS |
1183 | |
1184 | @menu | |
1185 | * Buffer Type:: The basic object of editing. | |
1186 | * Marker Type:: A position in a buffer. | |
1187 | * Window Type:: Buffers are displayed in windows. | |
1188 | * Frame Type:: Windows subdivide frames. | |
1189 | * Window Configuration Type:: Recording the way a frame is subdivided. | |
969fe9b5 | 1190 | * Frame Configuration Type:: Recording the status of all frames. |
5b359918 RS |
1191 | * Process Type:: A process running on the underlying OS. |
1192 | * Stream Type:: Receive or send characters. | |
1193 | * Keymap Type:: What function a keystroke invokes. | |
5b359918 RS |
1194 | * Overlay Type:: How an overlay is represented. |
1195 | @end menu | |
1196 | ||
1197 | @node Buffer Type | |
1198 | @subsection Buffer Type | |
1199 | ||
1200 | A @dfn{buffer} is an object that holds text that can be edited | |
1201 | (@pxref{Buffers}). Most buffers hold the contents of a disk file | |
1202 | (@pxref{Files}) so they can be edited, but some are used for other | |
1203 | purposes. Most buffers are also meant to be seen by the user, and | |
1204 | therefore displayed, at some time, in a window (@pxref{Windows}). But a | |
1205 | buffer need not be displayed in any window. | |
1206 | ||
1207 | The contents of a buffer are much like a string, but buffers are not | |
1208 | used like strings in Emacs Lisp, and the available operations are | |
969fe9b5 RS |
1209 | different. For example, you can insert text efficiently into an |
1210 | existing buffer, whereas ``inserting'' text into a string requires | |
5b359918 RS |
1211 | concatenating substrings, and the result is an entirely new string |
1212 | object. | |
1213 | ||
1214 | Each buffer has a designated position called @dfn{point} | |
1215 | (@pxref{Positions}). At any time, one buffer is the @dfn{current | |
1216 | buffer}. Most editing commands act on the contents of the current | |
2b3fc6c3 RS |
1217 | buffer in the neighborhood of point. Many of the standard Emacs |
1218 | functions manipulate or test the characters in the current buffer; a | |
1219 | whole chapter in this manual is devoted to describing these functions | |
1220 | (@pxref{Text}). | |
5b359918 RS |
1221 | |
1222 | Several other data structures are associated with each buffer: | |
1223 | ||
1224 | @itemize @bullet | |
1225 | @item | |
1226 | a local syntax table (@pxref{Syntax Tables}); | |
1227 | ||
1228 | @item | |
1229 | a local keymap (@pxref{Keymaps}); and, | |
1230 | ||
1231 | @item | |
969fe9b5 | 1232 | a list of buffer-local variable bindings (@pxref{Buffer-Local Variables}). |
bfe721d1 KH |
1233 | |
1234 | @item | |
969fe9b5 | 1235 | overlays (@pxref{Overlays}). |
bfe721d1 KH |
1236 | |
1237 | @item | |
1238 | text properties for the text in the buffer (@pxref{Text Properties}). | |
5b359918 RS |
1239 | @end itemize |
1240 | ||
1241 | @noindent | |
2b3fc6c3 | 1242 | The local keymap and variable list contain entries that individually |
5b359918 RS |
1243 | override global bindings or values. These are used to customize the |
1244 | behavior of programs in different buffers, without actually changing the | |
1245 | programs. | |
1246 | ||
bfe721d1 | 1247 | A buffer may be @dfn{indirect}, which means it shares the text |
969fe9b5 | 1248 | of another buffer, but presents it differently. @xref{Indirect Buffers}. |
bfe721d1 KH |
1249 | |
1250 | Buffers have no read syntax. They print in hash notation, showing the | |
5b359918 RS |
1251 | buffer name. |
1252 | ||
1253 | @example | |
1254 | @group | |
1255 | (current-buffer) | |
1256 | @result{} #<buffer objects.texi> | |
1257 | @end group | |
1258 | @end example | |
1259 | ||
1260 | @node Marker Type | |
1261 | @subsection Marker Type | |
1262 | ||
1263 | A @dfn{marker} denotes a position in a specific buffer. Markers | |
1264 | therefore have two components: one for the buffer, and one for the | |
1265 | position. Changes in the buffer's text automatically relocate the | |
1266 | position value as necessary to ensure that the marker always points | |
1267 | between the same two characters in the buffer. | |
1268 | ||
1269 | Markers have no read syntax. They print in hash notation, giving the | |
1270 | current character position and the name of the buffer. | |
1271 | ||
1272 | @example | |
1273 | @group | |
1274 | (point-marker) | |
1275 | @result{} #<marker at 10779 in objects.texi> | |
1276 | @end group | |
1277 | @end example | |
1278 | ||
1279 | @xref{Markers}, for information on how to test, create, copy, and move | |
1280 | markers. | |
1281 | ||
1282 | @node Window Type | |
1283 | @subsection Window Type | |
1284 | ||
1285 | A @dfn{window} describes the portion of the terminal screen that Emacs | |
1286 | uses to display a buffer. Every window has one associated buffer, whose | |
1287 | contents appear in the window. By contrast, a given buffer may appear | |
1288 | in one window, no window, or several windows. | |
1289 | ||
1290 | Though many windows may exist simultaneously, at any time one window | |
1291 | is designated the @dfn{selected window}. This is the window where the | |
1292 | cursor is (usually) displayed when Emacs is ready for a command. The | |
1293 | selected window usually displays the current buffer, but this is not | |
1294 | necessarily the case. | |
1295 | ||
1296 | Windows are grouped on the screen into frames; each window belongs to | |
1297 | one and only one frame. @xref{Frame Type}. | |
1298 | ||
1299 | Windows have no read syntax. They print in hash notation, giving the | |
1300 | window number and the name of the buffer being displayed. The window | |
1301 | numbers exist to identify windows uniquely, since the buffer displayed | |
1302 | in any given window can change frequently. | |
1303 | ||
1304 | @example | |
1305 | @group | |
1306 | (selected-window) | |
1307 | @result{} #<window 1 on objects.texi> | |
1308 | @end group | |
1309 | @end example | |
1310 | ||
1311 | @xref{Windows}, for a description of the functions that work on windows. | |
1312 | ||
1313 | @node Frame Type | |
1314 | @subsection Frame Type | |
1315 | ||
a9f0a989 | 1316 | A @dfn{frame} is a rectangle on the screen that contains one or more |
5b359918 RS |
1317 | Emacs windows. A frame initially contains a single main window (plus |
1318 | perhaps a minibuffer window) which you can subdivide vertically or | |
1319 | horizontally into smaller windows. | |
1320 | ||
1321 | Frames have no read syntax. They print in hash notation, giving the | |
1322 | frame's title, plus its address in core (useful to identify the frame | |
1323 | uniquely). | |
1324 | ||
1325 | @example | |
1326 | @group | |
1327 | (selected-frame) | |
a9f0a989 | 1328 | @result{} #<frame emacs@@psilocin.gnu.org 0xdac80> |
5b359918 RS |
1329 | @end group |
1330 | @end example | |
1331 | ||
1332 | @xref{Frames}, for a description of the functions that work on frames. | |
1333 | ||
1334 | @node Window Configuration Type | |
1335 | @subsection Window Configuration Type | |
1336 | @cindex screen layout | |
1337 | ||
1338 | A @dfn{window configuration} stores information about the positions, | |
1339 | sizes, and contents of the windows in a frame, so you can recreate the | |
1340 | same arrangement of windows later. | |
1341 | ||
9e2b495b RS |
1342 | Window configurations do not have a read syntax; their print syntax |
1343 | looks like @samp{#<window-configuration>}. @xref{Window | |
1344 | Configurations}, for a description of several functions related to | |
1345 | window configurations. | |
5b359918 | 1346 | |
969fe9b5 RS |
1347 | @node Frame Configuration Type |
1348 | @subsection Frame Configuration Type | |
1349 | @cindex screen layout | |
1350 | ||
1351 | A @dfn{frame configuration} stores information about the positions, | |
1352 | sizes, and contents of the windows in all frames. It is actually | |
1353 | a list whose @sc{car} is @code{frame-configuration} and whose | |
1354 | @sc{cdr} is an alist. Each alist element describes one frame, | |
1355 | which appears as the @sc{car} of that element. | |
1356 | ||
1357 | @xref{Frame Configurations}, for a description of several functions | |
1358 | related to frame configurations. | |
1359 | ||
5b359918 RS |
1360 | @node Process Type |
1361 | @subsection Process Type | |
1362 | ||
1363 | The word @dfn{process} usually means a running program. Emacs itself | |
1364 | runs in a process of this sort. However, in Emacs Lisp, a process is a | |
1365 | Lisp object that designates a subprocess created by the Emacs process. | |
1366 | Programs such as shells, GDB, ftp, and compilers, running in | |
1367 | subprocesses of Emacs, extend the capabilities of Emacs. | |
1368 | ||
1369 | An Emacs subprocess takes textual input from Emacs and returns textual | |
1370 | output to Emacs for further manipulation. Emacs can also send signals | |
1371 | to the subprocess. | |
1372 | ||
1373 | Process objects have no read syntax. They print in hash notation, | |
1374 | giving the name of the process: | |
1375 | ||
1376 | @example | |
1377 | @group | |
1378 | (process-list) | |
1379 | @result{} (#<process shell>) | |
1380 | @end group | |
1381 | @end example | |
1382 | ||
1383 | @xref{Processes}, for information about functions that create, delete, | |
1384 | return information about, send input or signals to, and receive output | |
1385 | from processes. | |
1386 | ||
1387 | @node Stream Type | |
1388 | @subsection Stream Type | |
1389 | ||
1390 | A @dfn{stream} is an object that can be used as a source or sink for | |
1391 | characters---either to supply characters for input or to accept them as | |
1392 | output. Many different types can be used this way: markers, buffers, | |
1393 | strings, and functions. Most often, input streams (character sources) | |
1394 | obtain characters from the keyboard, a buffer, or a file, and output | |
1395 | streams (character sinks) send characters to a buffer, such as a | |
1396 | @file{*Help*} buffer, or to the echo area. | |
1397 | ||
1398 | The object @code{nil}, in addition to its other meanings, may be used | |
1399 | as a stream. It stands for the value of the variable | |
1400 | @code{standard-input} or @code{standard-output}. Also, the object | |
1401 | @code{t} as a stream specifies input using the minibuffer | |
1402 | (@pxref{Minibuffers}) or output in the echo area (@pxref{The Echo | |
1403 | Area}). | |
1404 | ||
1405 | Streams have no special printed representation or read syntax, and | |
1406 | print as whatever primitive type they are. | |
1407 | ||
3e099569 | 1408 | @xref{Read and Print}, for a description of functions |
2b3fc6c3 | 1409 | related to streams, including parsing and printing functions. |
5b359918 RS |
1410 | |
1411 | @node Keymap Type | |
1412 | @subsection Keymap Type | |
1413 | ||
1414 | A @dfn{keymap} maps keys typed by the user to commands. This mapping | |
1415 | controls how the user's command input is executed. A keymap is actually | |
1416 | a list whose @sc{car} is the symbol @code{keymap}. | |
1417 | ||
1418 | @xref{Keymaps}, for information about creating keymaps, handling prefix | |
1419 | keys, local as well as global keymaps, and changing key bindings. | |
1420 | ||
5b359918 RS |
1421 | @node Overlay Type |
1422 | @subsection Overlay Type | |
1423 | ||
f9f59935 RS |
1424 | An @dfn{overlay} specifies properties that apply to a part of a |
1425 | buffer. Each overlay applies to a specified range of the buffer, and | |
1426 | contains a property list (a list whose elements are alternating property | |
1427 | names and values). Overlay properties are used to present parts of the | |
1428 | buffer temporarily in a different display style. Overlays have no read | |
1429 | syntax, and print in hash notation, giving the buffer name and range of | |
1430 | positions. | |
5b359918 | 1431 | |
bfe721d1 KH |
1432 | @xref{Overlays}, for how to create and use overlays. |
1433 | ||
5b359918 RS |
1434 | @node Type Predicates |
1435 | @section Type Predicates | |
1436 | @cindex predicates | |
1437 | @cindex type checking | |
1438 | @kindex wrong-type-argument | |
1439 | ||
1440 | The Emacs Lisp interpreter itself does not perform type checking on | |
1441 | the actual arguments passed to functions when they are called. It could | |
1442 | not do so, since function arguments in Lisp do not have declared data | |
1443 | types, as they do in other programming languages. It is therefore up to | |
1444 | the individual function to test whether each actual argument belongs to | |
1445 | a type that the function can use. | |
1446 | ||
1447 | All built-in functions do check the types of their actual arguments | |
1448 | when appropriate, and signal a @code{wrong-type-argument} error if an | |
1449 | argument is of the wrong type. For example, here is what happens if you | |
2b3fc6c3 | 1450 | pass an argument to @code{+} that it cannot handle: |
5b359918 RS |
1451 | |
1452 | @example | |
1453 | @group | |
1454 | (+ 2 'a) | |
f9f59935 | 1455 | @error{} Wrong type argument: number-or-marker-p, a |
5b359918 RS |
1456 | @end group |
1457 | @end example | |
1458 | ||
1459 | @cindex type predicates | |
1460 | @cindex testing types | |
22697dac KH |
1461 | If you want your program to handle different types differently, you |
1462 | must do explicit type checking. The most common way to check the type | |
1463 | of an object is to call a @dfn{type predicate} function. Emacs has a | |
1464 | type predicate for each type, as well as some predicates for | |
1465 | combinations of types. | |
5b359918 | 1466 | |
22697dac KH |
1467 | A type predicate function takes one argument; it returns @code{t} if |
1468 | the argument belongs to the appropriate type, and @code{nil} otherwise. | |
1469 | Following a general Lisp convention for predicate functions, most type | |
1470 | predicates' names end with @samp{p}. | |
1471 | ||
1472 | Here is an example which uses the predicates @code{listp} to check for | |
1473 | a list and @code{symbolp} to check for a symbol. | |
1474 | ||
1475 | @example | |
1476 | (defun add-on (x) | |
1477 | (cond ((symbolp x) | |
1478 | ;; If X is a symbol, put it on LIST. | |
1479 | (setq list (cons x list))) | |
1480 | ((listp x) | |
1481 | ;; If X is a list, add its elements to LIST. | |
1482 | (setq list (append x list))) | |
1483 | (t | |
969fe9b5 | 1484 | ;; We handle only symbols and lists. |
22697dac KH |
1485 | (error "Invalid argument %s in add-on" x)))) |
1486 | @end example | |
1487 | ||
1488 | Here is a table of predefined type predicates, in alphabetical order, | |
5b359918 RS |
1489 | with references to further information. |
1490 | ||
1491 | @table @code | |
1492 | @item atom | |
1493 | @xref{List-related Predicates, atom}. | |
1494 | ||
1495 | @item arrayp | |
1496 | @xref{Array Functions, arrayp}. | |
1497 | ||
969fe9b5 RS |
1498 | @item bool-vector-p |
1499 | @xref{Bool-Vectors, bool-vector-p}. | |
1500 | ||
5b359918 RS |
1501 | @item bufferp |
1502 | @xref{Buffer Basics, bufferp}. | |
1503 | ||
1504 | @item byte-code-function-p | |
1505 | @xref{Byte-Code Type, byte-code-function-p}. | |
1506 | ||
1507 | @item case-table-p | |
969fe9b5 | 1508 | @xref{Case Tables, case-table-p}. |
5b359918 RS |
1509 | |
1510 | @item char-or-string-p | |
1511 | @xref{Predicates for Strings, char-or-string-p}. | |
1512 | ||
969fe9b5 RS |
1513 | @item char-table-p |
1514 | @xref{Char-Tables, char-table-p}. | |
1515 | ||
5b359918 RS |
1516 | @item commandp |
1517 | @xref{Interactive Call, commandp}. | |
1518 | ||
1519 | @item consp | |
1520 | @xref{List-related Predicates, consp}. | |
1521 | ||
969fe9b5 RS |
1522 | @item display-table-p |
1523 | @xref{Display Tables, display-table-p}. | |
1524 | ||
5b359918 RS |
1525 | @item floatp |
1526 | @xref{Predicates on Numbers, floatp}. | |
1527 | ||
969fe9b5 RS |
1528 | @item frame-configuration-p |
1529 | @xref{Frame Configurations, frame-configuration-p}. | |
1530 | ||
5b359918 RS |
1531 | @item frame-live-p |
1532 | @xref{Deleting Frames, frame-live-p}. | |
1533 | ||
1534 | @item framep | |
1535 | @xref{Frames, framep}. | |
1536 | ||
f9f59935 RS |
1537 | @item functionp |
1538 | @xref{Functions, functionp}. | |
1539 | ||
5b359918 RS |
1540 | @item integer-or-marker-p |
1541 | @xref{Predicates on Markers, integer-or-marker-p}. | |
1542 | ||
1543 | @item integerp | |
1544 | @xref{Predicates on Numbers, integerp}. | |
1545 | ||
1546 | @item keymapp | |
1547 | @xref{Creating Keymaps, keymapp}. | |
1548 | ||
1549 | @item listp | |
1550 | @xref{List-related Predicates, listp}. | |
1551 | ||
1552 | @item markerp | |
1553 | @xref{Predicates on Markers, markerp}. | |
1554 | ||
2b3fc6c3 RS |
1555 | @item wholenump |
1556 | @xref{Predicates on Numbers, wholenump}. | |
5b359918 RS |
1557 | |
1558 | @item nlistp | |
1559 | @xref{List-related Predicates, nlistp}. | |
1560 | ||
1561 | @item numberp | |
1562 | @xref{Predicates on Numbers, numberp}. | |
1563 | ||
1564 | @item number-or-marker-p | |
1565 | @xref{Predicates on Markers, number-or-marker-p}. | |
1566 | ||
1567 | @item overlayp | |
1568 | @xref{Overlays, overlayp}. | |
1569 | ||
1570 | @item processp | |
1571 | @xref{Processes, processp}. | |
1572 | ||
1573 | @item sequencep | |
1574 | @xref{Sequence Functions, sequencep}. | |
1575 | ||
1576 | @item stringp | |
1577 | @xref{Predicates for Strings, stringp}. | |
1578 | ||
1579 | @item subrp | |
1580 | @xref{Function Cells, subrp}. | |
1581 | ||
1582 | @item symbolp | |
1583 | @xref{Symbols, symbolp}. | |
1584 | ||
1585 | @item syntax-table-p | |
1586 | @xref{Syntax Tables, syntax-table-p}. | |
1587 | ||
1588 | @item user-variable-p | |
1589 | @xref{Defining Variables, user-variable-p}. | |
1590 | ||
1591 | @item vectorp | |
1592 | @xref{Vectors, vectorp}. | |
1593 | ||
1594 | @item window-configuration-p | |
1595 | @xref{Window Configurations, window-configuration-p}. | |
1596 | ||
1597 | @item window-live-p | |
1598 | @xref{Deleting Windows, window-live-p}. | |
1599 | ||
1600 | @item windowp | |
1601 | @xref{Basic Windows, windowp}. | |
1602 | @end table | |
1603 | ||
22697dac KH |
1604 | The most general way to check the type of an object is to call the |
1605 | function @code{type-of}. Recall that each object belongs to one and | |
1606 | only one primitive type; @code{type-of} tells you which one (@pxref{Lisp | |
1607 | Data Types}). But @code{type-of} knows nothing about non-primitive | |
1608 | types. In most cases, it is more convenient to use type predicates than | |
1609 | @code{type-of}. | |
1610 | ||
1611 | @defun type-of object | |
1612 | This function returns a symbol naming the primitive type of | |
9e2b495b RS |
1613 | @var{object}. The value is one of the symbols @code{symbol}, |
1614 | @code{integer}, @code{float}, @code{string}, @code{cons}, @code{vector}, | |
969fe9b5 RS |
1615 | @code{char-table}, @code{bool-vector}, @code{subr}, |
1616 | @code{compiled-function}, @code{marker}, @code{overlay}, @code{window}, | |
1617 | @code{buffer}, @code{frame}, @code{process}, or | |
9e2b495b | 1618 | @code{window-configuration}. |
22697dac KH |
1619 | |
1620 | @example | |
1621 | (type-of 1) | |
1622 | @result{} integer | |
1623 | (type-of 'nil) | |
1624 | @result{} symbol | |
1625 | (type-of '()) ; @r{@code{()} is @code{nil}.} | |
1626 | @result{} symbol | |
1627 | (type-of '(x)) | |
1628 | @result{} cons | |
1629 | @end example | |
1630 | @end defun | |
1631 | ||
5b359918 RS |
1632 | @node Equality Predicates |
1633 | @section Equality Predicates | |
1634 | @cindex equality | |
1635 | ||
1636 | Here we describe two functions that test for equality between any two | |
1637 | objects. Other functions test equality between objects of specific | |
2b3fc6c3 RS |
1638 | types, e.g., strings. For these predicates, see the appropriate chapter |
1639 | describing the data type. | |
5b359918 RS |
1640 | |
1641 | @defun eq object1 object2 | |
1642 | This function returns @code{t} if @var{object1} and @var{object2} are | |
1643 | the same object, @code{nil} otherwise. The ``same object'' means that a | |
1644 | change in one will be reflected by the same change in the other. | |
1645 | ||
1646 | @code{eq} returns @code{t} if @var{object1} and @var{object2} are | |
1647 | integers with the same value. Also, since symbol names are normally | |
1648 | unique, if the arguments are symbols with the same name, they are | |
1649 | @code{eq}. For other types (e.g., lists, vectors, strings), two | |
1650 | arguments with the same contents or elements are not necessarily | |
1651 | @code{eq} to each other: they are @code{eq} only if they are the same | |
1652 | object. | |
1653 | ||
5b359918 RS |
1654 | @example |
1655 | @group | |
1656 | (eq 'foo 'foo) | |
1657 | @result{} t | |
1658 | @end group | |
1659 | ||
1660 | @group | |
1661 | (eq 456 456) | |
1662 | @result{} t | |
1663 | @end group | |
1664 | ||
1665 | @group | |
1666 | (eq "asdf" "asdf") | |
1667 | @result{} nil | |
1668 | @end group | |
1669 | ||
1670 | @group | |
1671 | (eq '(1 (2 (3))) '(1 (2 (3)))) | |
1672 | @result{} nil | |
1673 | @end group | |
1674 | ||
1675 | @group | |
1676 | (setq foo '(1 (2 (3)))) | |
1677 | @result{} (1 (2 (3))) | |
1678 | (eq foo foo) | |
1679 | @result{} t | |
1680 | (eq foo '(1 (2 (3)))) | |
1681 | @result{} nil | |
1682 | @end group | |
1683 | ||
1684 | @group | |
1685 | (eq [(1 2) 3] [(1 2) 3]) | |
1686 | @result{} nil | |
1687 | @end group | |
1688 | ||
1689 | @group | |
1690 | (eq (point-marker) (point-marker)) | |
1691 | @result{} nil | |
1692 | @end group | |
1693 | @end example | |
1694 | ||
a9f0a989 RS |
1695 | The @code{make-symbol} function returns an uninterned symbol, distinct |
1696 | from the symbol that is used if you write the name in a Lisp expression. | |
1697 | Distinct symbols with the same name are not @code{eq}. @xref{Creating | |
1698 | Symbols}. | |
969fe9b5 RS |
1699 | |
1700 | @example | |
1701 | @group | |
1702 | (eq (make-symbol "foo") 'foo) | |
1703 | @result{} nil | |
1704 | @end group | |
1705 | @end example | |
5b359918 RS |
1706 | @end defun |
1707 | ||
1708 | @defun equal object1 object2 | |
1709 | This function returns @code{t} if @var{object1} and @var{object2} have | |
1710 | equal components, @code{nil} otherwise. Whereas @code{eq} tests if its | |
1711 | arguments are the same object, @code{equal} looks inside nonidentical | |
1712 | arguments to see if their elements are the same. So, if two objects are | |
1713 | @code{eq}, they are @code{equal}, but the converse is not always true. | |
1714 | ||
1715 | @example | |
1716 | @group | |
1717 | (equal 'foo 'foo) | |
1718 | @result{} t | |
1719 | @end group | |
1720 | ||
1721 | @group | |
1722 | (equal 456 456) | |
1723 | @result{} t | |
1724 | @end group | |
1725 | ||
1726 | @group | |
1727 | (equal "asdf" "asdf") | |
1728 | @result{} t | |
1729 | @end group | |
1730 | @group | |
1731 | (eq "asdf" "asdf") | |
1732 | @result{} nil | |
1733 | @end group | |
1734 | ||
1735 | @group | |
1736 | (equal '(1 (2 (3))) '(1 (2 (3)))) | |
1737 | @result{} t | |
1738 | @end group | |
1739 | @group | |
1740 | (eq '(1 (2 (3))) '(1 (2 (3)))) | |
1741 | @result{} nil | |
1742 | @end group | |
1743 | ||
1744 | @group | |
1745 | (equal [(1 2) 3] [(1 2) 3]) | |
1746 | @result{} t | |
1747 | @end group | |
1748 | @group | |
1749 | (eq [(1 2) 3] [(1 2) 3]) | |
1750 | @result{} nil | |
1751 | @end group | |
1752 | ||
1753 | @group | |
1754 | (equal (point-marker) (point-marker)) | |
1755 | @result{} t | |
1756 | @end group | |
1757 | ||
1758 | @group | |
1759 | (eq (point-marker) (point-marker)) | |
1760 | @result{} nil | |
1761 | @end group | |
1762 | @end example | |
1763 | ||
f9f59935 RS |
1764 | Comparison of strings is case-sensitive, but does not take account of |
1765 | text properties---it compares only the characters in the strings. | |
1766 | A unibyte string never equals a multibyte string unless the | |
1767 | contents are entirely @sc{ASCII} (@pxref{Text Representations}). | |
5b359918 RS |
1768 | |
1769 | @example | |
1770 | @group | |
1771 | (equal "asdf" "ASDF") | |
1772 | @result{} nil | |
1773 | @end group | |
1774 | @end example | |
bfe721d1 KH |
1775 | |
1776 | Two distinct buffers are never @code{equal}, even if their contents | |
1777 | are the same. | |
5b359918 RS |
1778 | @end defun |
1779 | ||
1780 | The test for equality is implemented recursively, and circular lists may | |
1781 | therefore cause infinite recursion (leading to an error). |