]> code.delx.au - gnu-emacs/blob - src/data.c
(scmp): Use unsigned chars, to avoid confusing DOWNCASE.
[gnu-emacs] / src / data.c
1 /* Primitive operations on Lisp data types for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 1986, 1988, 1993, 1994 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
9 any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21 #include <signal.h>
22
23 #include <config.h>
24 #include "lisp.h"
25 #include "puresize.h"
26
27 #ifndef standalone
28 #include "buffer.h"
29 #endif
30
31 #include "syssignal.h"
32
33 #ifdef MSDOS
34 /* These are redefined (correctly, but differently) in values.h. */
35 #undef INTBITS
36 #undef LONGBITS
37 #undef SHORTBITS
38 #endif
39
40 #ifdef LISP_FLOAT_TYPE
41
42 #ifdef STDC_HEADERS
43 #include <stdlib.h>
44 #endif
45
46 /* Work around a problem that happens because math.h on hpux 7
47 defines two static variables--which, in Emacs, are not really static,
48 because `static' is defined as nothing. The problem is that they are
49 here, in floatfns.c, and in lread.c.
50 These macros prevent the name conflict. */
51 #if defined (HPUX) && !defined (HPUX8)
52 #define _MAXLDBL data_c_maxldbl
53 #define _NMAXLDBL data_c_nmaxldbl
54 #endif
55
56 #include <math.h>
57 #endif /* LISP_FLOAT_TYPE */
58
59 #if !defined (atof)
60 extern double atof ();
61 #endif /* !atof */
62
63 Lisp_Object Qnil, Qt, Qquote, Qlambda, Qsubr, Qunbound;
64 Lisp_Object Qerror_conditions, Qerror_message, Qtop_level;
65 Lisp_Object Qerror, Qquit, Qwrong_type_argument, Qargs_out_of_range;
66 Lisp_Object Qvoid_variable, Qvoid_function, Qcyclic_function_indirection;
67 Lisp_Object Qsetting_constant, Qinvalid_read_syntax;
68 Lisp_Object Qinvalid_function, Qwrong_number_of_arguments, Qno_catch;
69 Lisp_Object Qend_of_file, Qarith_error, Qmark_inactive;
70 Lisp_Object Qbeginning_of_buffer, Qend_of_buffer, Qbuffer_read_only;
71 Lisp_Object Qintegerp, Qnatnump, Qwholenump, Qsymbolp, Qlistp, Qconsp;
72 Lisp_Object Qstringp, Qarrayp, Qsequencep, Qbufferp;
73 Lisp_Object Qchar_or_string_p, Qmarkerp, Qinteger_or_marker_p, Qvectorp;
74 Lisp_Object Qbuffer_or_string_p;
75 Lisp_Object Qboundp, Qfboundp;
76 Lisp_Object Qcdr;
77 Lisp_Object Qad_advice_info, Qad_activate;
78
79 Lisp_Object Qrange_error, Qdomain_error, Qsingularity_error;
80 Lisp_Object Qoverflow_error, Qunderflow_error;
81
82 #ifdef LISP_FLOAT_TYPE
83 Lisp_Object Qfloatp;
84 Lisp_Object Qnumberp, Qnumber_or_marker_p;
85 #endif
86
87 static Lisp_Object swap_in_symval_forwarding ();
88
89 Lisp_Object
90 wrong_type_argument (predicate, value)
91 register Lisp_Object predicate, value;
92 {
93 register Lisp_Object tem;
94 do
95 {
96 if (!EQ (Vmocklisp_arguments, Qt))
97 {
98 if (STRINGP (value) &&
99 (EQ (predicate, Qintegerp) || EQ (predicate, Qinteger_or_marker_p)))
100 return Fstring_to_number (value);
101 if (INTEGERP (value) && EQ (predicate, Qstringp))
102 return Fnumber_to_string (value);
103 }
104 value = Fsignal (Qwrong_type_argument, Fcons (predicate, Fcons (value, Qnil)));
105 tem = call1 (predicate, value);
106 }
107 while (NILP (tem));
108 return value;
109 }
110
111 pure_write_error ()
112 {
113 error ("Attempt to modify read-only object");
114 }
115
116 void
117 args_out_of_range (a1, a2)
118 Lisp_Object a1, a2;
119 {
120 while (1)
121 Fsignal (Qargs_out_of_range, Fcons (a1, Fcons (a2, Qnil)));
122 }
123
124 void
125 args_out_of_range_3 (a1, a2, a3)
126 Lisp_Object a1, a2, a3;
127 {
128 while (1)
129 Fsignal (Qargs_out_of_range, Fcons (a1, Fcons (a2, Fcons (a3, Qnil))));
130 }
131
132 Lisp_Object
133 make_number (num)
134 int num;
135 {
136 register Lisp_Object val;
137 XSET (val, Lisp_Int, num);
138 return val;
139 }
140
141 /* On some machines, XINT needs a temporary location.
142 Here it is, in case it is needed. */
143
144 int sign_extend_temp;
145
146 /* On a few machines, XINT can only be done by calling this. */
147
148 int
149 sign_extend_lisp_int (num)
150 EMACS_INT num;
151 {
152 if (num & (((EMACS_INT) 1) << (VALBITS - 1)))
153 return num | (((EMACS_INT) (-1)) << VALBITS);
154 else
155 return num & ((((EMACS_INT) 1) << VALBITS) - 1);
156 }
157 \f
158 /* Data type predicates */
159
160 DEFUN ("eq", Feq, Seq, 2, 2, 0,
161 "T if the two args are the same Lisp object.")
162 (obj1, obj2)
163 Lisp_Object obj1, obj2;
164 {
165 if (EQ (obj1, obj2))
166 return Qt;
167 return Qnil;
168 }
169
170 DEFUN ("null", Fnull, Snull, 1, 1, 0, "T if OBJECT is nil.")
171 (obj)
172 Lisp_Object obj;
173 {
174 if (NILP (obj))
175 return Qt;
176 return Qnil;
177 }
178
179 DEFUN ("consp", Fconsp, Sconsp, 1, 1, 0, "T if OBJECT is a cons cell.")
180 (obj)
181 Lisp_Object obj;
182 {
183 if (CONSP (obj))
184 return Qt;
185 return Qnil;
186 }
187
188 DEFUN ("atom", Fatom, Satom, 1, 1, 0, "T if OBJECT is not a cons cell. This includes nil.")
189 (obj)
190 Lisp_Object obj;
191 {
192 if (CONSP (obj))
193 return Qnil;
194 return Qt;
195 }
196
197 DEFUN ("listp", Flistp, Slistp, 1, 1, 0, "T if OBJECT is a list. This includes nil.")
198 (obj)
199 Lisp_Object obj;
200 {
201 if (CONSP (obj) || NILP (obj))
202 return Qt;
203 return Qnil;
204 }
205
206 DEFUN ("nlistp", Fnlistp, Snlistp, 1, 1, 0, "T if OBJECT is not a list. Lists include nil.")
207 (obj)
208 Lisp_Object obj;
209 {
210 if (CONSP (obj) || NILP (obj))
211 return Qnil;
212 return Qt;
213 }
214 \f
215 DEFUN ("symbolp", Fsymbolp, Ssymbolp, 1, 1, 0, "T if OBJECT is a symbol.")
216 (obj)
217 Lisp_Object obj;
218 {
219 if (SYMBOLP (obj))
220 return Qt;
221 return Qnil;
222 }
223
224 DEFUN ("vectorp", Fvectorp, Svectorp, 1, 1, 0, "T if OBJECT is a vector.")
225 (obj)
226 Lisp_Object obj;
227 {
228 if (VECTORP (obj))
229 return Qt;
230 return Qnil;
231 }
232
233 DEFUN ("stringp", Fstringp, Sstringp, 1, 1, 0, "T if OBJECT is a string.")
234 (obj)
235 Lisp_Object obj;
236 {
237 if (STRINGP (obj))
238 return Qt;
239 return Qnil;
240 }
241
242 DEFUN ("arrayp", Farrayp, Sarrayp, 1, 1, 0, "T if OBJECT is an array (string or vector).")
243 (obj)
244 Lisp_Object obj;
245 {
246 if (VECTORP (obj) || STRINGP (obj))
247 return Qt;
248 return Qnil;
249 }
250
251 DEFUN ("sequencep", Fsequencep, Ssequencep, 1, 1, 0,
252 "T if OBJECT is a sequence (list or array).")
253 (obj)
254 register Lisp_Object obj;
255 {
256 if (CONSP (obj) || NILP (obj) || VECTORP (obj) || STRINGP (obj))
257 return Qt;
258 return Qnil;
259 }
260
261 DEFUN ("bufferp", Fbufferp, Sbufferp, 1, 1, 0, "T if OBJECT is an editor buffer.")
262 (obj)
263 Lisp_Object obj;
264 {
265 if (BUFFERP (obj))
266 return Qt;
267 return Qnil;
268 }
269
270 DEFUN ("markerp", Fmarkerp, Smarkerp, 1, 1, 0, "T if OBJECT is a marker (editor pointer).")
271 (obj)
272 Lisp_Object obj;
273 {
274 if (MARKERP (obj))
275 return Qt;
276 return Qnil;
277 }
278
279 DEFUN ("subrp", Fsubrp, Ssubrp, 1, 1, 0, "T if OBJECT is a built-in function.")
280 (obj)
281 Lisp_Object obj;
282 {
283 if (SUBRP (obj))
284 return Qt;
285 return Qnil;
286 }
287
288 DEFUN ("byte-code-function-p", Fbyte_code_function_p, Sbyte_code_function_p,
289 1, 1, 0, "T if OBJECT is a byte-compiled function object.")
290 (obj)
291 Lisp_Object obj;
292 {
293 if (COMPILEDP (obj))
294 return Qt;
295 return Qnil;
296 }
297
298 DEFUN ("char-or-string-p", Fchar_or_string_p, Schar_or_string_p, 1, 1, 0,
299 "T if OBJECT is a character (an integer) or a string.")
300 (obj)
301 register Lisp_Object obj;
302 {
303 if (INTEGERP (obj) || STRINGP (obj))
304 return Qt;
305 return Qnil;
306 }
307 \f
308 DEFUN ("integerp", Fintegerp, Sintegerp, 1, 1, 0, "T if OBJECT is an integer.")
309 (obj)
310 Lisp_Object obj;
311 {
312 if (INTEGERP (obj))
313 return Qt;
314 return Qnil;
315 }
316
317 DEFUN ("integer-or-marker-p", Finteger_or_marker_p, Sinteger_or_marker_p, 1, 1, 0,
318 "T if OBJECT is an integer or a marker (editor pointer).")
319 (obj)
320 register Lisp_Object obj;
321 {
322 if (MARKERP (obj) || INTEGERP (obj))
323 return Qt;
324 return Qnil;
325 }
326
327 DEFUN ("natnump", Fnatnump, Snatnump, 1, 1, 0,
328 "T if OBJECT is a nonnegative integer.")
329 (obj)
330 Lisp_Object obj;
331 {
332 if (INTEGERP (obj) && XINT (obj) >= 0)
333 return Qt;
334 return Qnil;
335 }
336
337 DEFUN ("numberp", Fnumberp, Snumberp, 1, 1, 0,
338 "T if OBJECT is a number (floating point or integer).")
339 (obj)
340 Lisp_Object obj;
341 {
342 if (NUMBERP (obj))
343 return Qt;
344 else
345 return Qnil;
346 }
347
348 DEFUN ("number-or-marker-p", Fnumber_or_marker_p,
349 Snumber_or_marker_p, 1, 1, 0,
350 "T if OBJECT is a number or a marker.")
351 (obj)
352 Lisp_Object obj;
353 {
354 if (NUMBERP (obj) || MARKERP (obj))
355 return Qt;
356 return Qnil;
357 }
358
359 #ifdef LISP_FLOAT_TYPE
360 DEFUN ("floatp", Ffloatp, Sfloatp, 1, 1, 0,
361 "T if OBJECT is a floating point number.")
362 (obj)
363 Lisp_Object obj;
364 {
365 if (FLOATP (obj))
366 return Qt;
367 return Qnil;
368 }
369 #endif /* LISP_FLOAT_TYPE */
370 \f
371 /* Extract and set components of lists */
372
373 DEFUN ("car", Fcar, Scar, 1, 1, 0,
374 "Return the car of CONSCELL. If arg is nil, return nil.\n\
375 Error if arg is not nil and not a cons cell. See also `car-safe'.")
376 (list)
377 register Lisp_Object list;
378 {
379 while (1)
380 {
381 if (CONSP (list))
382 return XCONS (list)->car;
383 else if (EQ (list, Qnil))
384 return Qnil;
385 else
386 list = wrong_type_argument (Qlistp, list);
387 }
388 }
389
390 DEFUN ("car-safe", Fcar_safe, Scar_safe, 1, 1, 0,
391 "Return the car of OBJECT if it is a cons cell, or else nil.")
392 (object)
393 Lisp_Object object;
394 {
395 if (CONSP (object))
396 return XCONS (object)->car;
397 else
398 return Qnil;
399 }
400
401 DEFUN ("cdr", Fcdr, Scdr, 1, 1, 0,
402 "Return the cdr of CONSCELL. If arg is nil, return nil.\n\
403 Error if arg is not nil and not a cons cell. See also `cdr-safe'.")
404
405 (list)
406 register Lisp_Object list;
407 {
408 while (1)
409 {
410 if (CONSP (list))
411 return XCONS (list)->cdr;
412 else if (EQ (list, Qnil))
413 return Qnil;
414 else
415 list = wrong_type_argument (Qlistp, list);
416 }
417 }
418
419 DEFUN ("cdr-safe", Fcdr_safe, Scdr_safe, 1, 1, 0,
420 "Return the cdr of OBJECT if it is a cons cell, or else nil.")
421 (object)
422 Lisp_Object object;
423 {
424 if (CONSP (object))
425 return XCONS (object)->cdr;
426 else
427 return Qnil;
428 }
429
430 DEFUN ("setcar", Fsetcar, Ssetcar, 2, 2, 0,
431 "Set the car of CONSCELL to be NEWCAR. Returns NEWCAR.")
432 (cell, newcar)
433 register Lisp_Object cell, newcar;
434 {
435 if (!CONSP (cell))
436 cell = wrong_type_argument (Qconsp, cell);
437
438 CHECK_IMPURE (cell);
439 XCONS (cell)->car = newcar;
440 return newcar;
441 }
442
443 DEFUN ("setcdr", Fsetcdr, Ssetcdr, 2, 2, 0,
444 "Set the cdr of CONSCELL to be NEWCDR. Returns NEWCDR.")
445 (cell, newcdr)
446 register Lisp_Object cell, newcdr;
447 {
448 if (!CONSP (cell))
449 cell = wrong_type_argument (Qconsp, cell);
450
451 CHECK_IMPURE (cell);
452 XCONS (cell)->cdr = newcdr;
453 return newcdr;
454 }
455 \f
456 /* Extract and set components of symbols */
457
458 DEFUN ("boundp", Fboundp, Sboundp, 1, 1, 0, "T if SYMBOL's value is not void.")
459 (sym)
460 register Lisp_Object sym;
461 {
462 Lisp_Object valcontents;
463 CHECK_SYMBOL (sym, 0);
464
465 valcontents = XSYMBOL (sym)->value;
466
467 #ifdef SWITCH_ENUM_BUG
468 switch ((int) XTYPE (valcontents))
469 #else
470 switch (XTYPE (valcontents))
471 #endif
472 {
473 case Lisp_Buffer_Local_Value:
474 case Lisp_Some_Buffer_Local_Value:
475 valcontents = swap_in_symval_forwarding (sym, valcontents);
476 }
477
478 return (VOIDP (valcontents) || EQ (valcontents, Qunbound)
479 ? Qnil : Qt);
480 }
481
482 DEFUN ("fboundp", Ffboundp, Sfboundp, 1, 1, 0, "T if SYMBOL's function definition is not void.")
483 (sym)
484 register Lisp_Object sym;
485 {
486 CHECK_SYMBOL (sym, 0);
487 return ((VOIDP (XSYMBOL (sym)->function)
488 || EQ (XSYMBOL (sym)->function, Qunbound))
489 ? Qnil : Qt);
490 }
491
492 DEFUN ("makunbound", Fmakunbound, Smakunbound, 1, 1, 0, "Make SYMBOL's value be void.")
493 (sym)
494 register Lisp_Object sym;
495 {
496 CHECK_SYMBOL (sym, 0);
497 if (NILP (sym) || EQ (sym, Qt))
498 return Fsignal (Qsetting_constant, Fcons (sym, Qnil));
499 Fset (sym, Qunbound);
500 return sym;
501 }
502
503 DEFUN ("fmakunbound", Ffmakunbound, Sfmakunbound, 1, 1, 0, "Make SYMBOL's function definition be void.")
504 (sym)
505 register Lisp_Object sym;
506 {
507 CHECK_SYMBOL (sym, 0);
508 if (NILP (sym) || EQ (sym, Qt))
509 return Fsignal (Qsetting_constant, Fcons (sym, Qnil));
510 XSYMBOL (sym)->function = Qunbound;
511 return sym;
512 }
513
514 DEFUN ("symbol-function", Fsymbol_function, Ssymbol_function, 1, 1, 0,
515 "Return SYMBOL's function definition. Error if that is void.")
516 (symbol)
517 register Lisp_Object symbol;
518 {
519 CHECK_SYMBOL (symbol, 0);
520 if (EQ (XSYMBOL (symbol)->function, Qunbound))
521 return Fsignal (Qvoid_function, Fcons (symbol, Qnil));
522 return XSYMBOL (symbol)->function;
523 }
524
525 DEFUN ("symbol-plist", Fsymbol_plist, Ssymbol_plist, 1, 1, 0, "Return SYMBOL's property list.")
526 (sym)
527 register Lisp_Object sym;
528 {
529 CHECK_SYMBOL (sym, 0);
530 return XSYMBOL (sym)->plist;
531 }
532
533 DEFUN ("symbol-name", Fsymbol_name, Ssymbol_name, 1, 1, 0, "Return SYMBOL's name, a string.")
534 (sym)
535 register Lisp_Object sym;
536 {
537 register Lisp_Object name;
538
539 CHECK_SYMBOL (sym, 0);
540 XSET (name, Lisp_String, XSYMBOL (sym)->name);
541 return name;
542 }
543
544 DEFUN ("fset", Ffset, Sfset, 2, 2, 0,
545 "Set SYMBOL's function definition to NEWVAL, and return NEWVAL.")
546 (sym, newdef)
547 register Lisp_Object sym, newdef;
548 {
549 CHECK_SYMBOL (sym, 0);
550 if (NILP (sym) || EQ (sym, Qt))
551 return Fsignal (Qsetting_constant, Fcons (sym, Qnil));
552 if (!NILP (Vautoload_queue) && !EQ (XSYMBOL (sym)->function, Qunbound))
553 Vautoload_queue = Fcons (Fcons (sym, XSYMBOL (sym)->function),
554 Vautoload_queue);
555 XSYMBOL (sym)->function = newdef;
556 /* Handle automatic advice activation */
557 if (CONSP (XSYMBOL (sym)->plist) && !NILP (Fget (sym, Qad_advice_info)))
558 {
559 call2 (Qad_activate, sym, Qnil);
560 newdef = XSYMBOL (sym)->function;
561 }
562 return newdef;
563 }
564
565 /* This name should be removed once it is eliminated from elsewhere. */
566
567 DEFUN ("defalias", Fdefalias, Sdefalias, 2, 2, 0,
568 "Set SYMBOL's function definition to NEWVAL, and return NEWVAL.\n\
569 Associates the function with the current load file, if any.")
570 (sym, newdef)
571 register Lisp_Object sym, newdef;
572 {
573 CHECK_SYMBOL (sym, 0);
574 if (!NILP (Vautoload_queue) && !EQ (XSYMBOL (sym)->function, Qunbound))
575 Vautoload_queue = Fcons (Fcons (sym, XSYMBOL (sym)->function),
576 Vautoload_queue);
577 XSYMBOL (sym)->function = newdef;
578 /* Handle automatic advice activation */
579 if (CONSP (XSYMBOL (sym)->plist) && !NILP (Fget (sym, Qad_advice_info)))
580 {
581 call2 (Qad_activate, sym, Qnil);
582 newdef = XSYMBOL (sym)->function;
583 }
584 LOADHIST_ATTACH (sym);
585 return newdef;
586 }
587
588 DEFUN ("define-function", Fdefine_function, Sdefine_function, 2, 2, 0,
589 "Set SYMBOL's function definition to NEWVAL, and return NEWVAL.\n\
590 Associates the function with the current load file, if any.")
591 (sym, newdef)
592 register Lisp_Object sym, newdef;
593 {
594 CHECK_SYMBOL (sym, 0);
595 if (!NILP (Vautoload_queue) && !EQ (XSYMBOL (sym)->function, Qunbound))
596 Vautoload_queue = Fcons (Fcons (sym, XSYMBOL (sym)->function),
597 Vautoload_queue);
598 XSYMBOL (sym)->function = newdef;
599 /* Handle automatic advice activation */
600 if (CONSP (XSYMBOL (sym)->plist) && !NILP (Fget (sym, Qad_advice_info)))
601 {
602 call2 (Qad_activate, sym, Qnil);
603 newdef = XSYMBOL (sym)->function;
604 }
605 LOADHIST_ATTACH (sym);
606 return newdef;
607 }
608
609 DEFUN ("setplist", Fsetplist, Ssetplist, 2, 2, 0,
610 "Set SYMBOL's property list to NEWVAL, and return NEWVAL.")
611 (sym, newplist)
612 register Lisp_Object sym, newplist;
613 {
614 CHECK_SYMBOL (sym, 0);
615 XSYMBOL (sym)->plist = newplist;
616 return newplist;
617 }
618
619 \f
620 /* Getting and setting values of symbols */
621
622 /* Given the raw contents of a symbol value cell,
623 return the Lisp value of the symbol.
624 This does not handle buffer-local variables; use
625 swap_in_symval_forwarding for that. */
626
627 Lisp_Object
628 do_symval_forwarding (valcontents)
629 register Lisp_Object valcontents;
630 {
631 register Lisp_Object val;
632 #ifdef SWITCH_ENUM_BUG
633 switch ((int) XTYPE (valcontents))
634 #else
635 switch (XTYPE (valcontents))
636 #endif
637 {
638 case Lisp_Intfwd:
639 XSET (val, Lisp_Int, *XINTPTR (valcontents));
640 return val;
641
642 case Lisp_Boolfwd:
643 if (*XINTPTR (valcontents))
644 return Qt;
645 return Qnil;
646
647 case Lisp_Objfwd:
648 return *XOBJFWD (valcontents);
649
650 case Lisp_Buffer_Objfwd:
651 return *(Lisp_Object *)(XUINT (valcontents) + (char *)current_buffer);
652 }
653 return valcontents;
654 }
655
656 /* Store NEWVAL into SYM, where VALCONTENTS is found in the value cell
657 of SYM. If SYM is buffer-local, VALCONTENTS should be the
658 buffer-independent contents of the value cell: forwarded just one
659 step past the buffer-localness. */
660
661 void
662 store_symval_forwarding (sym, valcontents, newval)
663 Lisp_Object sym;
664 register Lisp_Object valcontents, newval;
665 {
666 #ifdef SWITCH_ENUM_BUG
667 switch ((int) XTYPE (valcontents))
668 #else
669 switch (XTYPE (valcontents))
670 #endif
671 {
672 case Lisp_Intfwd:
673 CHECK_NUMBER (newval, 1);
674 *XINTPTR (valcontents) = XINT (newval);
675 break;
676
677 case Lisp_Boolfwd:
678 *XINTPTR (valcontents) = NILP(newval) ? 0 : 1;
679 break;
680
681 case Lisp_Objfwd:
682 *XOBJFWD (valcontents) = newval;
683 break;
684
685 case Lisp_Buffer_Objfwd:
686 {
687 unsigned int offset = XUINT (valcontents);
688 Lisp_Object type;
689
690 type = *(Lisp_Object *)(offset + (char *)&buffer_local_types);
691 if (! NILP (type) && ! NILP (newval)
692 && XTYPE (newval) != XINT (type))
693 buffer_slot_type_mismatch (valcontents, newval);
694
695 *(Lisp_Object *)(XUINT (valcontents) + (char *)current_buffer)
696 = newval;
697 break;
698 }
699
700 default:
701 valcontents = XSYMBOL (sym)->value;
702 if (BUFFER_LOCAL_VALUEP (valcontents)
703 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
704 XCONS (XSYMBOL (sym)->value)->car = newval;
705 else
706 XSYMBOL (sym)->value = newval;
707 }
708 }
709
710 /* Set up the buffer-local symbol SYM for validity in the current
711 buffer. VALCONTENTS is the contents of its value cell.
712 Return the value forwarded one step past the buffer-local indicator. */
713
714 static Lisp_Object
715 swap_in_symval_forwarding (sym, valcontents)
716 Lisp_Object sym, valcontents;
717 {
718 /* valcontents is a list
719 (REALVALUE BUFFER CURRENT-ALIST-ELEMENT . DEFAULT-VALUE)).
720
721 CURRENT-ALIST-ELEMENT is a pointer to an element of BUFFER's
722 local_var_alist, that being the element whose car is this
723 variable. Or it can be a pointer to the
724 (CURRENT-ALIST-ELEMENT . DEFAULT-VALUE), if BUFFER does not have
725 an element in its alist for this variable.
726
727 If the current buffer is not BUFFER, we store the current
728 REALVALUE value into CURRENT-ALIST-ELEMENT, then find the
729 appropriate alist element for the buffer now current and set up
730 CURRENT-ALIST-ELEMENT. Then we set REALVALUE out of that
731 element, and store into BUFFER.
732
733 Note that REALVALUE can be a forwarding pointer. */
734
735 register Lisp_Object tem1;
736 tem1 = XCONS (XCONS (valcontents)->cdr)->car;
737
738 if (NILP (tem1) || current_buffer != XBUFFER (tem1))
739 {
740 tem1 = XCONS (XCONS (XCONS (valcontents)->cdr)->cdr)->car;
741 Fsetcdr (tem1, do_symval_forwarding (XCONS (valcontents)->car));
742 tem1 = assq_no_quit (sym, current_buffer->local_var_alist);
743 if (NILP (tem1))
744 tem1 = XCONS (XCONS (valcontents)->cdr)->cdr;
745 XCONS (XCONS (XCONS (valcontents)->cdr)->cdr)->car = tem1;
746 XSET (XCONS (XCONS (valcontents)->cdr)->car, Lisp_Buffer, current_buffer);
747 store_symval_forwarding (sym, XCONS (valcontents)->car, Fcdr (tem1));
748 }
749 return XCONS (valcontents)->car;
750 }
751 \f
752 /* Find the value of a symbol, returning Qunbound if it's not bound.
753 This is helpful for code which just wants to get a variable's value
754 if it has one, without signalling an error.
755 Note that it must not be possible to quit
756 within this function. Great care is required for this. */
757
758 Lisp_Object
759 find_symbol_value (sym)
760 Lisp_Object sym;
761 {
762 register Lisp_Object valcontents, tem1;
763 register Lisp_Object val;
764 CHECK_SYMBOL (sym, 0);
765 valcontents = XSYMBOL (sym)->value;
766
767 retry:
768 #ifdef SWITCH_ENUM_BUG
769 switch ((int) XTYPE (valcontents))
770 #else
771 switch (XTYPE (valcontents))
772 #endif
773 {
774 case Lisp_Buffer_Local_Value:
775 case Lisp_Some_Buffer_Local_Value:
776 valcontents = swap_in_symval_forwarding (sym, valcontents);
777 goto retry;
778
779 case Lisp_Intfwd:
780 XSET (val, Lisp_Int, *XINTPTR (valcontents));
781 return val;
782
783 case Lisp_Boolfwd:
784 if (*XINTPTR (valcontents))
785 return Qt;
786 return Qnil;
787
788 case Lisp_Objfwd:
789 return *XOBJFWD (valcontents);
790
791 case Lisp_Buffer_Objfwd:
792 return *(Lisp_Object *)(XUINT (valcontents) + (char *)current_buffer);
793
794 case Lisp_Void:
795 return Qunbound;
796 }
797
798 return valcontents;
799 }
800
801 DEFUN ("symbol-value", Fsymbol_value, Ssymbol_value, 1, 1, 0,
802 "Return SYMBOL's value. Error if that is void.")
803 (sym)
804 Lisp_Object sym;
805 {
806 Lisp_Object val;
807
808 val = find_symbol_value (sym);
809 if (EQ (val, Qunbound))
810 return Fsignal (Qvoid_variable, Fcons (sym, Qnil));
811 else
812 return val;
813 }
814
815 DEFUN ("set", Fset, Sset, 2, 2, 0,
816 "Set SYMBOL's value to NEWVAL, and return NEWVAL.")
817 (sym, newval)
818 register Lisp_Object sym, newval;
819 {
820 int voide = (VOIDP (newval) || EQ (newval, Qunbound));
821
822 register Lisp_Object valcontents, tem1, current_alist_element;
823
824 CHECK_SYMBOL (sym, 0);
825 if (NILP (sym) || EQ (sym, Qt))
826 return Fsignal (Qsetting_constant, Fcons (sym, Qnil));
827 valcontents = XSYMBOL (sym)->value;
828
829 if (BUFFER_OBJFWDP (valcontents))
830 {
831 register int idx = XUINT (valcontents);
832 register int mask = *(int *)(idx + (char *) &buffer_local_flags);
833 if (mask > 0)
834 current_buffer->local_var_flags |= mask;
835 }
836
837 else if (BUFFER_LOCAL_VALUEP (valcontents)
838 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
839 {
840 /* valcontents is actually a pointer to a cons heading something like:
841 (REALVALUE BUFFER CURRENT-ALIST-ELEMENT . DEFAULT-VALUE).
842
843 BUFFER is the last buffer for which this symbol's value was
844 made up to date.
845
846 CURRENT-ALIST-ELEMENT is a pointer to an element of BUFFER's
847 local_var_alist, that being the element whose car is this
848 variable. Or it can be a pointer to the
849 (CURRENT-ALIST-ELEMENT . DEFAULT-VALUE), if BUFFER does not
850 have an element in its alist for this variable (that is, if
851 BUFFER sees the default value of this variable).
852
853 If we want to examine or set the value and BUFFER is current,
854 we just examine or set REALVALUE. If BUFFER is not current, we
855 store the current REALVALUE value into CURRENT-ALIST-ELEMENT,
856 then find the appropriate alist element for the buffer now
857 current and set up CURRENT-ALIST-ELEMENT. Then we set
858 REALVALUE out of that element, and store into BUFFER.
859
860 If we are setting the variable and the current buffer does
861 not have an alist entry for this variable, an alist entry is
862 created.
863
864 Note that REALVALUE can be a forwarding pointer. Each time
865 it is examined or set, forwarding must be done. */
866
867 /* What value are we caching right now? */
868 current_alist_element =
869 XCONS (XCONS (XCONS (valcontents)->cdr)->cdr)->car;
870
871 /* If the current buffer is not the buffer whose binding is
872 currently cached, or if it's a Lisp_Buffer_Local_Value and
873 we're looking at the default value, the cache is invalid; we
874 need to write it out, and find the new CURRENT-ALIST-ELEMENT. */
875 if ((current_buffer
876 != XBUFFER (XCONS (XCONS (valcontents)->cdr)->car))
877 || (BUFFER_LOCAL_VALUEP (valcontents)
878 && EQ (XCONS (current_alist_element)->car,
879 current_alist_element)))
880 {
881 /* Write out the cached value for the old buffer; copy it
882 back to its alist element. This works if the current
883 buffer only sees the default value, too. */
884 Fsetcdr (current_alist_element,
885 do_symval_forwarding (XCONS (valcontents)->car));
886
887 /* Find the new value for CURRENT-ALIST-ELEMENT. */
888 tem1 = Fassq (sym, current_buffer->local_var_alist);
889 if (NILP (tem1))
890 {
891 /* This buffer still sees the default value. */
892
893 /* If the variable is a Lisp_Some_Buffer_Local_Value,
894 make CURRENT-ALIST-ELEMENT point to itself,
895 indicating that we're seeing the default value. */
896 if (SOME_BUFFER_LOCAL_VALUEP (valcontents))
897 tem1 = XCONS (XCONS (valcontents)->cdr)->cdr;
898
899 /* If it's a Lisp_Buffer_Local_Value, give this buffer a
900 new assoc for a local value and set
901 CURRENT-ALIST-ELEMENT to point to that. */
902 else
903 {
904 tem1 = Fcons (sym, Fcdr (current_alist_element));
905 current_buffer->local_var_alist =
906 Fcons (tem1, current_buffer->local_var_alist);
907 }
908 }
909 /* Cache the new buffer's assoc in CURRENT-ALIST-ELEMENT. */
910 XCONS (XCONS (XCONS (valcontents)->cdr)->cdr)->car = tem1;
911
912 /* Set BUFFER, now that CURRENT-ALIST-ELEMENT is accurate. */
913 XSET (XCONS (XCONS (valcontents)->cdr)->car,
914 Lisp_Buffer, current_buffer);
915 }
916 valcontents = XCONS (valcontents)->car;
917 }
918
919 /* If storing void (making the symbol void), forward only through
920 buffer-local indicator, not through Lisp_Objfwd, etc. */
921 if (voide)
922 store_symval_forwarding (sym, Qnil, newval);
923 else
924 store_symval_forwarding (sym, valcontents, newval);
925
926 return newval;
927 }
928 \f
929 /* Access or set a buffer-local symbol's default value. */
930
931 /* Return the default value of SYM, but don't check for voidness.
932 Return Qunbound or a Lisp_Void object if it is void. */
933
934 Lisp_Object
935 default_value (sym)
936 Lisp_Object sym;
937 {
938 register Lisp_Object valcontents;
939
940 CHECK_SYMBOL (sym, 0);
941 valcontents = XSYMBOL (sym)->value;
942
943 /* For a built-in buffer-local variable, get the default value
944 rather than letting do_symval_forwarding get the current value. */
945 if (BUFFER_OBJFWDP (valcontents))
946 {
947 register int idx = XUINT (valcontents);
948
949 if (*(int *) (idx + (char *) &buffer_local_flags) != 0)
950 return *(Lisp_Object *)(idx + (char *) &buffer_defaults);
951 }
952
953 /* Handle user-created local variables. */
954 if (BUFFER_LOCAL_VALUEP (valcontents)
955 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
956 {
957 /* If var is set up for a buffer that lacks a local value for it,
958 the current value is nominally the default value.
959 But the current value slot may be more up to date, since
960 ordinary setq stores just that slot. So use that. */
961 Lisp_Object current_alist_element, alist_element_car;
962 current_alist_element
963 = XCONS (XCONS (XCONS (valcontents)->cdr)->cdr)->car;
964 alist_element_car = XCONS (current_alist_element)->car;
965 if (EQ (alist_element_car, current_alist_element))
966 return do_symval_forwarding (XCONS (valcontents)->car);
967 else
968 return XCONS (XCONS (XCONS (valcontents)->cdr)->cdr)->cdr;
969 }
970 /* For other variables, get the current value. */
971 return do_symval_forwarding (valcontents);
972 }
973
974 DEFUN ("default-boundp", Fdefault_boundp, Sdefault_boundp, 1, 1, 0,
975 "Return T if SYMBOL has a non-void default value.\n\
976 This is the value that is seen in buffers that do not have their own values\n\
977 for this variable.")
978 (sym)
979 Lisp_Object sym;
980 {
981 register Lisp_Object value;
982
983 value = default_value (sym);
984 return (VOIDP (value) || EQ (value, Qunbound)
985 ? Qnil : Qt);
986 }
987
988 DEFUN ("default-value", Fdefault_value, Sdefault_value, 1, 1, 0,
989 "Return SYMBOL's default value.\n\
990 This is the value that is seen in buffers that do not have their own values\n\
991 for this variable. The default value is meaningful for variables with\n\
992 local bindings in certain buffers.")
993 (sym)
994 Lisp_Object sym;
995 {
996 register Lisp_Object value;
997
998 value = default_value (sym);
999 if (VOIDP (value) || EQ (value, Qunbound))
1000 return Fsignal (Qvoid_variable, Fcons (sym, Qnil));
1001 return value;
1002 }
1003
1004 DEFUN ("set-default", Fset_default, Sset_default, 2, 2, 0,
1005 "Set SYMBOL's default value to VAL. SYMBOL and VAL are evaluated.\n\
1006 The default value is seen in buffers that do not have their own values\n\
1007 for this variable.")
1008 (sym, value)
1009 Lisp_Object sym, value;
1010 {
1011 register Lisp_Object valcontents, current_alist_element, alist_element_buffer;
1012
1013 CHECK_SYMBOL (sym, 0);
1014 valcontents = XSYMBOL (sym)->value;
1015
1016 /* Handle variables like case-fold-search that have special slots
1017 in the buffer. Make them work apparently like Lisp_Buffer_Local_Value
1018 variables. */
1019 if (BUFFER_OBJFWDP (valcontents))
1020 {
1021 register int idx = XUINT (valcontents);
1022 register struct buffer *b;
1023 register int mask = *(int *) (idx + (char *) &buffer_local_flags);
1024
1025 if (mask > 0)
1026 {
1027 *(Lisp_Object *)(idx + (char *) &buffer_defaults) = value;
1028 for (b = all_buffers; b; b = b->next)
1029 if (!(b->local_var_flags & mask))
1030 *(Lisp_Object *)(idx + (char *) b) = value;
1031 }
1032 return value;
1033 }
1034
1035 if (!BUFFER_LOCAL_VALUEP (valcontents)
1036 && !SOME_BUFFER_LOCAL_VALUEP (valcontents))
1037 return Fset (sym, value);
1038
1039 /* Store new value into the DEFAULT-VALUE slot */
1040 XCONS (XCONS (XCONS (valcontents)->cdr)->cdr)->cdr = value;
1041
1042 /* If that slot is current, we must set the REALVALUE slot too */
1043 current_alist_element = XCONS (XCONS (XCONS (valcontents)->cdr)->cdr)->car;
1044 alist_element_buffer = Fcar (current_alist_element);
1045 if (EQ (alist_element_buffer, current_alist_element))
1046 store_symval_forwarding (sym, XCONS (valcontents)->car, value);
1047
1048 return value;
1049 }
1050
1051 DEFUN ("setq-default", Fsetq_default, Ssetq_default, 2, UNEVALLED, 0,
1052 "Set the default value of variable VAR to VALUE.\n\
1053 VAR, the variable name, is literal (not evaluated);\n\
1054 VALUE is an expression and it is evaluated.\n\
1055 The default value of a variable is seen in buffers\n\
1056 that do not have their own values for the variable.\n\
1057 \n\
1058 More generally, you can use multiple variables and values, as in\n\
1059 (setq-default SYM VALUE SYM VALUE...)\n\
1060 This sets each SYM's default value to the corresponding VALUE.\n\
1061 The VALUE for the Nth SYM can refer to the new default values\n\
1062 of previous SYMs.")
1063 (args)
1064 Lisp_Object args;
1065 {
1066 register Lisp_Object args_left;
1067 register Lisp_Object val, sym;
1068 struct gcpro gcpro1;
1069
1070 if (NILP (args))
1071 return Qnil;
1072
1073 args_left = args;
1074 GCPRO1 (args);
1075
1076 do
1077 {
1078 val = Feval (Fcar (Fcdr (args_left)));
1079 sym = Fcar (args_left);
1080 Fset_default (sym, val);
1081 args_left = Fcdr (Fcdr (args_left));
1082 }
1083 while (!NILP (args_left));
1084
1085 UNGCPRO;
1086 return val;
1087 }
1088 \f
1089 /* Lisp functions for creating and removing buffer-local variables. */
1090
1091 DEFUN ("make-variable-buffer-local", Fmake_variable_buffer_local, Smake_variable_buffer_local,
1092 1, 1, "vMake Variable Buffer Local: ",
1093 "Make VARIABLE have a separate value for each buffer.\n\
1094 At any time, the value for the current buffer is in effect.\n\
1095 There is also a default value which is seen in any buffer which has not yet\n\
1096 set its own value.\n\
1097 Using `set' or `setq' to set the variable causes it to have a separate value\n\
1098 for the current buffer if it was previously using the default value.\n\
1099 The function `default-value' gets the default value and `set-default' sets it.")
1100 (sym)
1101 register Lisp_Object sym;
1102 {
1103 register Lisp_Object tem, valcontents;
1104
1105 CHECK_SYMBOL (sym, 0);
1106
1107 if (EQ (sym, Qnil) || EQ (sym, Qt))
1108 error ("Symbol %s may not be buffer-local", XSYMBOL (sym)->name->data);
1109
1110 valcontents = XSYMBOL (sym)->value;
1111 if (BUFFER_LOCAL_VALUEP (valcontents) || BUFFER_OBJFWDP (valcontents))
1112 return sym;
1113 if (SOME_BUFFER_LOCAL_VALUEP (valcontents))
1114 {
1115 XSETTYPE (XSYMBOL (sym)->value, Lisp_Buffer_Local_Value);
1116 return sym;
1117 }
1118 if (EQ (valcontents, Qunbound))
1119 XSYMBOL (sym)->value = Qnil;
1120 tem = Fcons (Qnil, Fsymbol_value (sym));
1121 XCONS (tem)->car = tem;
1122 XSYMBOL (sym)->value = Fcons (XSYMBOL (sym)->value, Fcons (Fcurrent_buffer (), tem));
1123 XSETTYPE (XSYMBOL (sym)->value, Lisp_Buffer_Local_Value);
1124 return sym;
1125 }
1126
1127 DEFUN ("make-local-variable", Fmake_local_variable, Smake_local_variable,
1128 1, 1, "vMake Local Variable: ",
1129 "Make VARIABLE have a separate value in the current buffer.\n\
1130 Other buffers will continue to share a common default value.\n\
1131 \(The buffer-local value of VARIABLE starts out as the same value\n\
1132 VARIABLE previously had. If VARIABLE was void, it remains void.\)\n\
1133 See also `make-variable-buffer-local'.\n\n\
1134 If the variable is already arranged to become local when set,\n\
1135 this function causes a local value to exist for this buffer,\n\
1136 just as if the variable were set.")
1137 (sym)
1138 register Lisp_Object sym;
1139 {
1140 register Lisp_Object tem, valcontents;
1141
1142 CHECK_SYMBOL (sym, 0);
1143
1144 if (EQ (sym, Qnil) || EQ (sym, Qt))
1145 error ("Symbol %s may not be buffer-local", XSYMBOL (sym)->name->data);
1146
1147 valcontents = XSYMBOL (sym)->value;
1148 if (BUFFER_LOCAL_VALUEP (valcontents) || BUFFER_OBJFWDP (valcontents))
1149 {
1150 tem = Fboundp (sym);
1151
1152 /* Make sure the symbol has a local value in this particular buffer,
1153 by setting it to the same value it already has. */
1154 Fset (sym, (EQ (tem, Qt) ? Fsymbol_value (sym) : Qunbound));
1155 return sym;
1156 }
1157 /* Make sure sym is set up to hold per-buffer values */
1158 if (!SOME_BUFFER_LOCAL_VALUEP (valcontents))
1159 {
1160 tem = Fcons (Qnil, do_symval_forwarding (valcontents));
1161 XCONS (tem)->car = tem;
1162 XSYMBOL (sym)->value = Fcons (XSYMBOL (sym)->value, Fcons (Qnil, tem));
1163 XSETTYPE (XSYMBOL (sym)->value, Lisp_Some_Buffer_Local_Value);
1164 }
1165 /* Make sure this buffer has its own value of sym */
1166 tem = Fassq (sym, current_buffer->local_var_alist);
1167 if (NILP (tem))
1168 {
1169 current_buffer->local_var_alist
1170 = Fcons (Fcons (sym, XCONS (XCONS (XCONS (XSYMBOL (sym)->value)->cdr)->cdr)->cdr),
1171 current_buffer->local_var_alist);
1172
1173 /* Make sure symbol does not think it is set up for this buffer;
1174 force it to look once again for this buffer's value */
1175 {
1176 /* This local variable avoids "expression too complex" on IBM RT. */
1177 Lisp_Object xs;
1178
1179 xs = XSYMBOL (sym)->value;
1180 if (current_buffer == XBUFFER (XCONS (XCONS (xs)->cdr)->car))
1181 XCONS (XCONS (XSYMBOL (sym)->value)->cdr)->car = Qnil;
1182 }
1183 }
1184
1185 /* If the symbol forwards into a C variable, then swap in the
1186 variable for this buffer immediately. If C code modifies the
1187 variable before we swap in, then that new value will clobber the
1188 default value the next time we swap. */
1189 valcontents = XCONS (XSYMBOL (sym)->value)->car;
1190 if (INTFWDP (valcontents) || BOOLFWDP (valcontents) || OBJFWDP (valcontents))
1191 swap_in_symval_forwarding (sym, XSYMBOL (sym)->value);
1192
1193 return sym;
1194 }
1195
1196 DEFUN ("kill-local-variable", Fkill_local_variable, Skill_local_variable,
1197 1, 1, "vKill Local Variable: ",
1198 "Make VARIABLE no longer have a separate value in the current buffer.\n\
1199 From now on the default value will apply in this buffer.")
1200 (sym)
1201 register Lisp_Object sym;
1202 {
1203 register Lisp_Object tem, valcontents;
1204
1205 CHECK_SYMBOL (sym, 0);
1206
1207 valcontents = XSYMBOL (sym)->value;
1208
1209 if (BUFFER_OBJFWDP (valcontents))
1210 {
1211 register int idx = XUINT (valcontents);
1212 register int mask = *(int *) (idx + (char *) &buffer_local_flags);
1213
1214 if (mask > 0)
1215 {
1216 *(Lisp_Object *)(idx + (char *) current_buffer)
1217 = *(Lisp_Object *)(idx + (char *) &buffer_defaults);
1218 current_buffer->local_var_flags &= ~mask;
1219 }
1220 return sym;
1221 }
1222
1223 if (!BUFFER_LOCAL_VALUEP (valcontents)
1224 && !SOME_BUFFER_LOCAL_VALUEP (valcontents))
1225 return sym;
1226
1227 /* Get rid of this buffer's alist element, if any */
1228
1229 tem = Fassq (sym, current_buffer->local_var_alist);
1230 if (!NILP (tem))
1231 current_buffer->local_var_alist = Fdelq (tem, current_buffer->local_var_alist);
1232
1233 /* Make sure symbol does not think it is set up for this buffer;
1234 force it to look once again for this buffer's value */
1235 {
1236 Lisp_Object sv;
1237 sv = XSYMBOL (sym)->value;
1238 if (current_buffer == XBUFFER (XCONS (XCONS (sv)->cdr)->car))
1239 XCONS (XCONS (sv)->cdr)->car = Qnil;
1240 }
1241
1242 return sym;
1243 }
1244 \f
1245 /* Find the function at the end of a chain of symbol function indirections. */
1246
1247 /* If OBJECT is a symbol, find the end of its function chain and
1248 return the value found there. If OBJECT is not a symbol, just
1249 return it. If there is a cycle in the function chain, signal a
1250 cyclic-function-indirection error.
1251
1252 This is like Findirect_function, except that it doesn't signal an
1253 error if the chain ends up unbound. */
1254 Lisp_Object
1255 indirect_function (object)
1256 register Lisp_Object object;
1257 {
1258 Lisp_Object tortoise, hare;
1259
1260 hare = tortoise = object;
1261
1262 for (;;)
1263 {
1264 if (!SYMBOLP (hare) || EQ (hare, Qunbound))
1265 break;
1266 hare = XSYMBOL (hare)->function;
1267 if (!SYMBOLP (hare) || EQ (hare, Qunbound))
1268 break;
1269 hare = XSYMBOL (hare)->function;
1270
1271 tortoise = XSYMBOL (tortoise)->function;
1272
1273 if (EQ (hare, tortoise))
1274 Fsignal (Qcyclic_function_indirection, Fcons (object, Qnil));
1275 }
1276
1277 return hare;
1278 }
1279
1280 DEFUN ("indirect-function", Findirect_function, Sindirect_function, 1, 1, 0,
1281 "Return the function at the end of OBJECT's function chain.\n\
1282 If OBJECT is a symbol, follow all function indirections and return the final\n\
1283 function binding.\n\
1284 If OBJECT is not a symbol, just return it.\n\
1285 Signal a void-function error if the final symbol is unbound.\n\
1286 Signal a cyclic-function-indirection error if there is a loop in the\n\
1287 function chain of symbols.")
1288 (object)
1289 register Lisp_Object object;
1290 {
1291 Lisp_Object result;
1292
1293 result = indirect_function (object);
1294
1295 if (EQ (result, Qunbound))
1296 return Fsignal (Qvoid_function, Fcons (object, Qnil));
1297 return result;
1298 }
1299 \f
1300 /* Extract and set vector and string elements */
1301
1302 DEFUN ("aref", Faref, Saref, 2, 2, 0,
1303 "Return the element of ARRAY at index INDEX.\n\
1304 ARRAY may be a vector or a string, or a byte-code object. INDEX starts at 0.")
1305 (array, idx)
1306 register Lisp_Object array;
1307 Lisp_Object idx;
1308 {
1309 register int idxval;
1310
1311 CHECK_NUMBER (idx, 1);
1312 idxval = XINT (idx);
1313 if (!VECTORP (array) && !STRINGP (array) && !COMPILEDP (array))
1314 array = wrong_type_argument (Qarrayp, array);
1315 if (idxval < 0 || idxval >= XVECTOR (array)->size)
1316 args_out_of_range (array, idx);
1317 if (STRINGP (array))
1318 {
1319 Lisp_Object val;
1320 XFASTINT (val) = (unsigned char) XSTRING (array)->data[idxval];
1321 return val;
1322 }
1323 else
1324 return XVECTOR (array)->contents[idxval];
1325 }
1326
1327 DEFUN ("aset", Faset, Saset, 3, 3, 0,
1328 "Store into the element of ARRAY at index IDX the value NEWELT.\n\
1329 ARRAY may be a vector or a string. IDX starts at 0.")
1330 (array, idx, newelt)
1331 register Lisp_Object array;
1332 Lisp_Object idx, newelt;
1333 {
1334 register int idxval;
1335
1336 CHECK_NUMBER (idx, 1);
1337 idxval = XINT (idx);
1338 if (!VECTORP (array) && !STRINGP (array))
1339 array = wrong_type_argument (Qarrayp, array);
1340 if (idxval < 0 || idxval >= XVECTOR (array)->size)
1341 args_out_of_range (array, idx);
1342 CHECK_IMPURE (array);
1343
1344 if (VECTORP (array))
1345 XVECTOR (array)->contents[idxval] = newelt;
1346 else
1347 {
1348 CHECK_NUMBER (newelt, 2);
1349 XSTRING (array)->data[idxval] = XINT (newelt);
1350 }
1351
1352 return newelt;
1353 }
1354
1355 Lisp_Object
1356 Farray_length (array)
1357 register Lisp_Object array;
1358 {
1359 register Lisp_Object size;
1360 if (!VECTORP (array) && !STRINGP (array) && !COMPILEDP (array))
1361 array = wrong_type_argument (Qarrayp, array);
1362 XFASTINT (size) = XVECTOR (array)->size;
1363 return size;
1364 }
1365 \f
1366 /* Arithmetic functions */
1367
1368 enum comparison { equal, notequal, less, grtr, less_or_equal, grtr_or_equal };
1369
1370 Lisp_Object
1371 arithcompare (num1, num2, comparison)
1372 Lisp_Object num1, num2;
1373 enum comparison comparison;
1374 {
1375 double f1, f2;
1376 int floatp = 0;
1377
1378 #ifdef LISP_FLOAT_TYPE
1379 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num1, 0);
1380 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num2, 0);
1381
1382 if (FLOATP (num1) || FLOATP (num2))
1383 {
1384 floatp = 1;
1385 f1 = (FLOATP (num1)) ? XFLOAT (num1)->data : XINT (num1);
1386 f2 = (FLOATP (num2)) ? XFLOAT (num2)->data : XINT (num2);
1387 }
1388 #else
1389 CHECK_NUMBER_COERCE_MARKER (num1, 0);
1390 CHECK_NUMBER_COERCE_MARKER (num2, 0);
1391 #endif /* LISP_FLOAT_TYPE */
1392
1393 switch (comparison)
1394 {
1395 case equal:
1396 if (floatp ? f1 == f2 : XINT (num1) == XINT (num2))
1397 return Qt;
1398 return Qnil;
1399
1400 case notequal:
1401 if (floatp ? f1 != f2 : XINT (num1) != XINT (num2))
1402 return Qt;
1403 return Qnil;
1404
1405 case less:
1406 if (floatp ? f1 < f2 : XINT (num1) < XINT (num2))
1407 return Qt;
1408 return Qnil;
1409
1410 case less_or_equal:
1411 if (floatp ? f1 <= f2 : XINT (num1) <= XINT (num2))
1412 return Qt;
1413 return Qnil;
1414
1415 case grtr:
1416 if (floatp ? f1 > f2 : XINT (num1) > XINT (num2))
1417 return Qt;
1418 return Qnil;
1419
1420 case grtr_or_equal:
1421 if (floatp ? f1 >= f2 : XINT (num1) >= XINT (num2))
1422 return Qt;
1423 return Qnil;
1424
1425 default:
1426 abort ();
1427 }
1428 }
1429
1430 DEFUN ("=", Feqlsign, Seqlsign, 2, 2, 0,
1431 "T if two args, both numbers or markers, are equal.")
1432 (num1, num2)
1433 register Lisp_Object num1, num2;
1434 {
1435 return arithcompare (num1, num2, equal);
1436 }
1437
1438 DEFUN ("<", Flss, Slss, 2, 2, 0,
1439 "T if first arg is less than second arg. Both must be numbers or markers.")
1440 (num1, num2)
1441 register Lisp_Object num1, num2;
1442 {
1443 return arithcompare (num1, num2, less);
1444 }
1445
1446 DEFUN (">", Fgtr, Sgtr, 2, 2, 0,
1447 "T if first arg is greater than second arg. Both must be numbers or markers.")
1448 (num1, num2)
1449 register Lisp_Object num1, num2;
1450 {
1451 return arithcompare (num1, num2, grtr);
1452 }
1453
1454 DEFUN ("<=", Fleq, Sleq, 2, 2, 0,
1455 "T if first arg is less than or equal to second arg.\n\
1456 Both must be numbers or markers.")
1457 (num1, num2)
1458 register Lisp_Object num1, num2;
1459 {
1460 return arithcompare (num1, num2, less_or_equal);
1461 }
1462
1463 DEFUN (">=", Fgeq, Sgeq, 2, 2, 0,
1464 "T if first arg is greater than or equal to second arg.\n\
1465 Both must be numbers or markers.")
1466 (num1, num2)
1467 register Lisp_Object num1, num2;
1468 {
1469 return arithcompare (num1, num2, grtr_or_equal);
1470 }
1471
1472 DEFUN ("/=", Fneq, Sneq, 2, 2, 0,
1473 "T if first arg is not equal to second arg. Both must be numbers or markers.")
1474 (num1, num2)
1475 register Lisp_Object num1, num2;
1476 {
1477 return arithcompare (num1, num2, notequal);
1478 }
1479
1480 DEFUN ("zerop", Fzerop, Szerop, 1, 1, 0, "T if NUMBER is zero.")
1481 (num)
1482 register Lisp_Object num;
1483 {
1484 #ifdef LISP_FLOAT_TYPE
1485 CHECK_NUMBER_OR_FLOAT (num, 0);
1486
1487 if (FLOATP (num))
1488 {
1489 if (XFLOAT(num)->data == 0.0)
1490 return Qt;
1491 return Qnil;
1492 }
1493 #else
1494 CHECK_NUMBER (num, 0);
1495 #endif /* LISP_FLOAT_TYPE */
1496
1497 if (!XINT (num))
1498 return Qt;
1499 return Qnil;
1500 }
1501 \f
1502 /* Convert between 32-bit values and pairs of lispy 24-bit values. */
1503
1504 Lisp_Object
1505 long_to_cons (i)
1506 unsigned long i;
1507 {
1508 unsigned int top = i >> 16;
1509 unsigned int bot = i & 0xFFFF;
1510 if (top == 0)
1511 return make_number (bot);
1512 if (top == 0xFFFF)
1513 return Fcons (make_number (-1), make_number (bot));
1514 return Fcons (make_number (top), make_number (bot));
1515 }
1516
1517 unsigned long
1518 cons_to_long (c)
1519 Lisp_Object c;
1520 {
1521 Lisp_Object top, bot;
1522 if (INTEGERP (c))
1523 return XINT (c);
1524 top = XCONS (c)->car;
1525 bot = XCONS (c)->cdr;
1526 if (CONSP (bot))
1527 bot = XCONS (bot)->car;
1528 return ((XINT (top) << 16) | XINT (bot));
1529 }
1530 \f
1531 DEFUN ("number-to-string", Fnumber_to_string, Snumber_to_string, 1, 1, 0,
1532 "Convert NUM to a string by printing it in decimal.\n\
1533 Uses a minus sign if negative.\n\
1534 NUM may be an integer or a floating point number.")
1535 (num)
1536 Lisp_Object num;
1537 {
1538 char buffer[20];
1539
1540 #ifndef LISP_FLOAT_TYPE
1541 CHECK_NUMBER (num, 0);
1542 #else
1543 CHECK_NUMBER_OR_FLOAT (num, 0);
1544
1545 if (FLOATP (num))
1546 {
1547 char pigbuf[350]; /* see comments in float_to_string */
1548
1549 float_to_string (pigbuf, XFLOAT(num)->data);
1550 return build_string (pigbuf);
1551 }
1552 #endif /* LISP_FLOAT_TYPE */
1553
1554 sprintf (buffer, "%d", XINT (num));
1555 return build_string (buffer);
1556 }
1557
1558 DEFUN ("string-to-number", Fstring_to_number, Sstring_to_number, 1, 1, 0,
1559 "Convert STRING to a number by parsing it as a decimal number.\n\
1560 This parses both integers and floating point numbers.\n\
1561 It ignores leading spaces and tabs.")
1562 (str)
1563 register Lisp_Object str;
1564 {
1565 unsigned char *p;
1566
1567 CHECK_STRING (str, 0);
1568
1569 p = XSTRING (str)->data;
1570
1571 /* Skip any whitespace at the front of the number. Some versions of
1572 atoi do this anyway, so we might as well make Emacs lisp consistent. */
1573 while (*p == ' ' || *p == '\t')
1574 p++;
1575
1576 #ifdef LISP_FLOAT_TYPE
1577 if (isfloat_string (p))
1578 return make_float (atof (p));
1579 #endif /* LISP_FLOAT_TYPE */
1580
1581 return make_number (atoi (p));
1582 }
1583 \f
1584 enum arithop
1585 { Aadd, Asub, Amult, Adiv, Alogand, Alogior, Alogxor, Amax, Amin };
1586
1587 extern Lisp_Object float_arith_driver ();
1588
1589 Lisp_Object
1590 arith_driver (code, nargs, args)
1591 enum arithop code;
1592 int nargs;
1593 register Lisp_Object *args;
1594 {
1595 register Lisp_Object val;
1596 register int argnum;
1597 register int accum;
1598 register int next;
1599
1600 #ifdef SWITCH_ENUM_BUG
1601 switch ((int) code)
1602 #else
1603 switch (code)
1604 #endif
1605 {
1606 case Alogior:
1607 case Alogxor:
1608 case Aadd:
1609 case Asub:
1610 accum = 0; break;
1611 case Amult:
1612 accum = 1; break;
1613 case Alogand:
1614 accum = -1; break;
1615 }
1616
1617 for (argnum = 0; argnum < nargs; argnum++)
1618 {
1619 val = args[argnum]; /* using args[argnum] as argument to CHECK_NUMBER_... */
1620 #ifdef LISP_FLOAT_TYPE
1621 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val, argnum);
1622
1623 if (FLOATP (val)) /* time to do serious math */
1624 return (float_arith_driver ((double) accum, argnum, code,
1625 nargs, args));
1626 #else
1627 CHECK_NUMBER_COERCE_MARKER (val, argnum);
1628 #endif /* LISP_FLOAT_TYPE */
1629 args[argnum] = val; /* runs into a compiler bug. */
1630 next = XINT (args[argnum]);
1631 #ifdef SWITCH_ENUM_BUG
1632 switch ((int) code)
1633 #else
1634 switch (code)
1635 #endif
1636 {
1637 case Aadd: accum += next; break;
1638 case Asub:
1639 if (!argnum && nargs != 1)
1640 next = - next;
1641 accum -= next;
1642 break;
1643 case Amult: accum *= next; break;
1644 case Adiv:
1645 if (!argnum) accum = next;
1646 else
1647 {
1648 if (next == 0)
1649 Fsignal (Qarith_error, Qnil);
1650 accum /= next;
1651 }
1652 break;
1653 case Alogand: accum &= next; break;
1654 case Alogior: accum |= next; break;
1655 case Alogxor: accum ^= next; break;
1656 case Amax: if (!argnum || next > accum) accum = next; break;
1657 case Amin: if (!argnum || next < accum) accum = next; break;
1658 }
1659 }
1660
1661 XSET (val, Lisp_Int, accum);
1662 return val;
1663 }
1664
1665 #ifdef LISP_FLOAT_TYPE
1666
1667 #undef isnan
1668 #define isnan(x) ((x) != (x))
1669
1670 Lisp_Object
1671 float_arith_driver (accum, argnum, code, nargs, args)
1672 double accum;
1673 register int argnum;
1674 enum arithop code;
1675 int nargs;
1676 register Lisp_Object *args;
1677 {
1678 register Lisp_Object val;
1679 double next;
1680
1681 for (; argnum < nargs; argnum++)
1682 {
1683 val = args[argnum]; /* using args[argnum] as argument to CHECK_NUMBER_... */
1684 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val, argnum);
1685
1686 if (FLOATP (val))
1687 {
1688 next = XFLOAT (val)->data;
1689 }
1690 else
1691 {
1692 args[argnum] = val; /* runs into a compiler bug. */
1693 next = XINT (args[argnum]);
1694 }
1695 #ifdef SWITCH_ENUM_BUG
1696 switch ((int) code)
1697 #else
1698 switch (code)
1699 #endif
1700 {
1701 case Aadd:
1702 accum += next;
1703 break;
1704 case Asub:
1705 if (!argnum && nargs != 1)
1706 next = - next;
1707 accum -= next;
1708 break;
1709 case Amult:
1710 accum *= next;
1711 break;
1712 case Adiv:
1713 if (!argnum)
1714 accum = next;
1715 else
1716 {
1717 if (next == 0)
1718 Fsignal (Qarith_error, Qnil);
1719 accum /= next;
1720 }
1721 break;
1722 case Alogand:
1723 case Alogior:
1724 case Alogxor:
1725 return wrong_type_argument (Qinteger_or_marker_p, val);
1726 case Amax:
1727 if (!argnum || isnan (next) || next > accum)
1728 accum = next;
1729 break;
1730 case Amin:
1731 if (!argnum || isnan (next) || next < accum)
1732 accum = next;
1733 break;
1734 }
1735 }
1736
1737 return make_float (accum);
1738 }
1739 #endif /* LISP_FLOAT_TYPE */
1740
1741 DEFUN ("+", Fplus, Splus, 0, MANY, 0,
1742 "Return sum of any number of arguments, which are numbers or markers.")
1743 (nargs, args)
1744 int nargs;
1745 Lisp_Object *args;
1746 {
1747 return arith_driver (Aadd, nargs, args);
1748 }
1749
1750 DEFUN ("-", Fminus, Sminus, 0, MANY, 0,
1751 "Negate number or subtract numbers or markers.\n\
1752 With one arg, negates it. With more than one arg,\n\
1753 subtracts all but the first from the first.")
1754 (nargs, args)
1755 int nargs;
1756 Lisp_Object *args;
1757 {
1758 return arith_driver (Asub, nargs, args);
1759 }
1760
1761 DEFUN ("*", Ftimes, Stimes, 0, MANY, 0,
1762 "Returns product of any number of arguments, which are numbers or markers.")
1763 (nargs, args)
1764 int nargs;
1765 Lisp_Object *args;
1766 {
1767 return arith_driver (Amult, nargs, args);
1768 }
1769
1770 DEFUN ("/", Fquo, Squo, 2, MANY, 0,
1771 "Returns first argument divided by all the remaining arguments.\n\
1772 The arguments must be numbers or markers.")
1773 (nargs, args)
1774 int nargs;
1775 Lisp_Object *args;
1776 {
1777 return arith_driver (Adiv, nargs, args);
1778 }
1779
1780 DEFUN ("%", Frem, Srem, 2, 2, 0,
1781 "Returns remainder of first arg divided by second.\n\
1782 Both must be integers or markers.")
1783 (num1, num2)
1784 register Lisp_Object num1, num2;
1785 {
1786 Lisp_Object val;
1787
1788 CHECK_NUMBER_COERCE_MARKER (num1, 0);
1789 CHECK_NUMBER_COERCE_MARKER (num2, 1);
1790
1791 if (XFASTINT (num2) == 0)
1792 Fsignal (Qarith_error, Qnil);
1793
1794 XSET (val, Lisp_Int, XINT (num1) % XINT (num2));
1795 return val;
1796 }
1797
1798 #ifndef HAVE_FMOD
1799 double
1800 fmod (f1, f2)
1801 double f1, f2;
1802 {
1803 #ifdef HAVE_DREM /* Some systems use this non-standard name. */
1804 return (drem (f1, f2));
1805 #else /* Other systems don't seem to have it at all. */
1806 return (f1 - f2 * floor (f1/f2));
1807 #endif
1808 }
1809 #endif /* ! HAVE_FMOD */
1810
1811 DEFUN ("mod", Fmod, Smod, 2, 2, 0,
1812 "Returns X modulo Y.\n\
1813 The result falls between zero (inclusive) and Y (exclusive).\n\
1814 Both X and Y must be numbers or markers.")
1815 (num1, num2)
1816 register Lisp_Object num1, num2;
1817 {
1818 Lisp_Object val;
1819 int i1, i2;
1820
1821 #ifdef LISP_FLOAT_TYPE
1822 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num1, 0);
1823 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num2, 1);
1824
1825 if (FLOATP (num1) || FLOATP (num2))
1826 {
1827 double f1, f2;
1828
1829 f1 = FLOATP (num1) ? XFLOAT (num1)->data : XINT (num1);
1830 f2 = FLOATP (num2) ? XFLOAT (num2)->data : XINT (num2);
1831 if (f2 == 0)
1832 Fsignal (Qarith_error, Qnil);
1833
1834 f1 = fmod (f1, f2);
1835 /* If the "remainder" comes out with the wrong sign, fix it. */
1836 if ((f1 < 0) != (f2 < 0))
1837 f1 += f2;
1838 return (make_float (f1));
1839 }
1840 #else /* not LISP_FLOAT_TYPE */
1841 CHECK_NUMBER_COERCE_MARKER (num1, 0);
1842 CHECK_NUMBER_COERCE_MARKER (num2, 1);
1843 #endif /* not LISP_FLOAT_TYPE */
1844
1845 i1 = XINT (num1);
1846 i2 = XINT (num2);
1847
1848 if (i2 == 0)
1849 Fsignal (Qarith_error, Qnil);
1850
1851 i1 %= i2;
1852
1853 /* If the "remainder" comes out with the wrong sign, fix it. */
1854 if ((i1 < 0) != (i2 < 0))
1855 i1 += i2;
1856
1857 XSET (val, Lisp_Int, i1);
1858 return val;
1859 }
1860
1861 DEFUN ("max", Fmax, Smax, 1, MANY, 0,
1862 "Return largest of all the arguments (which must be numbers or markers).\n\
1863 The value is always a number; markers are converted to numbers.")
1864 (nargs, args)
1865 int nargs;
1866 Lisp_Object *args;
1867 {
1868 return arith_driver (Amax, nargs, args);
1869 }
1870
1871 DEFUN ("min", Fmin, Smin, 1, MANY, 0,
1872 "Return smallest of all the arguments (which must be numbers or markers).\n\
1873 The value is always a number; markers are converted to numbers.")
1874 (nargs, args)
1875 int nargs;
1876 Lisp_Object *args;
1877 {
1878 return arith_driver (Amin, nargs, args);
1879 }
1880
1881 DEFUN ("logand", Flogand, Slogand, 0, MANY, 0,
1882 "Return bitwise-and of all the arguments.\n\
1883 Arguments may be integers, or markers converted to integers.")
1884 (nargs, args)
1885 int nargs;
1886 Lisp_Object *args;
1887 {
1888 return arith_driver (Alogand, nargs, args);
1889 }
1890
1891 DEFUN ("logior", Flogior, Slogior, 0, MANY, 0,
1892 "Return bitwise-or of all the arguments.\n\
1893 Arguments may be integers, or markers converted to integers.")
1894 (nargs, args)
1895 int nargs;
1896 Lisp_Object *args;
1897 {
1898 return arith_driver (Alogior, nargs, args);
1899 }
1900
1901 DEFUN ("logxor", Flogxor, Slogxor, 0, MANY, 0,
1902 "Return bitwise-exclusive-or of all the arguments.\n\
1903 Arguments may be integers, or markers converted to integers.")
1904 (nargs, args)
1905 int nargs;
1906 Lisp_Object *args;
1907 {
1908 return arith_driver (Alogxor, nargs, args);
1909 }
1910
1911 DEFUN ("ash", Fash, Sash, 2, 2, 0,
1912 "Return VALUE with its bits shifted left by COUNT.\n\
1913 If COUNT is negative, shifting is actually to the right.\n\
1914 In this case, the sign bit is duplicated.")
1915 (num1, num2)
1916 register Lisp_Object num1, num2;
1917 {
1918 register Lisp_Object val;
1919
1920 CHECK_NUMBER (num1, 0);
1921 CHECK_NUMBER (num2, 1);
1922
1923 if (XINT (num2) > 0)
1924 XSET (val, Lisp_Int, XINT (num1) << XFASTINT (num2));
1925 else
1926 XSET (val, Lisp_Int, XINT (num1) >> -XINT (num2));
1927 return val;
1928 }
1929
1930 DEFUN ("lsh", Flsh, Slsh, 2, 2, 0,
1931 "Return VALUE with its bits shifted left by COUNT.\n\
1932 If COUNT is negative, shifting is actually to the right.\n\
1933 In this case, zeros are shifted in on the left.")
1934 (num1, num2)
1935 register Lisp_Object num1, num2;
1936 {
1937 register Lisp_Object val;
1938
1939 CHECK_NUMBER (num1, 0);
1940 CHECK_NUMBER (num2, 1);
1941
1942 if (XINT (num2) > 0)
1943 XSET (val, Lisp_Int, (unsigned) XFASTINT (num1) << XFASTINT (num2));
1944 else
1945 XSET (val, Lisp_Int, (unsigned) XFASTINT (num1) >> -XINT (num2));
1946 return val;
1947 }
1948
1949 DEFUN ("1+", Fadd1, Sadd1, 1, 1, 0,
1950 "Return NUMBER plus one. NUMBER may be a number or a marker.\n\
1951 Markers are converted to integers.")
1952 (num)
1953 register Lisp_Object num;
1954 {
1955 #ifdef LISP_FLOAT_TYPE
1956 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num, 0);
1957
1958 if (FLOATP (num))
1959 return (make_float (1.0 + XFLOAT (num)->data));
1960 #else
1961 CHECK_NUMBER_COERCE_MARKER (num, 0);
1962 #endif /* LISP_FLOAT_TYPE */
1963
1964 XSETINT (num, XFASTINT (num) + 1);
1965 return num;
1966 }
1967
1968 DEFUN ("1-", Fsub1, Ssub1, 1, 1, 0,
1969 "Return NUMBER minus one. NUMBER may be a number or a marker.\n\
1970 Markers are converted to integers.")
1971 (num)
1972 register Lisp_Object num;
1973 {
1974 #ifdef LISP_FLOAT_TYPE
1975 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num, 0);
1976
1977 if (FLOATP (num))
1978 return (make_float (-1.0 + XFLOAT (num)->data));
1979 #else
1980 CHECK_NUMBER_COERCE_MARKER (num, 0);
1981 #endif /* LISP_FLOAT_TYPE */
1982
1983 XSETINT (num, XFASTINT (num) - 1);
1984 return num;
1985 }
1986
1987 DEFUN ("lognot", Flognot, Slognot, 1, 1, 0,
1988 "Return the bitwise complement of ARG. ARG must be an integer.")
1989 (num)
1990 register Lisp_Object num;
1991 {
1992 CHECK_NUMBER (num, 0);
1993 XSETINT (num, ~XFASTINT (num));
1994 return num;
1995 }
1996 \f
1997 void
1998 syms_of_data ()
1999 {
2000 Lisp_Object error_tail, arith_tail;
2001
2002 Qquote = intern ("quote");
2003 Qlambda = intern ("lambda");
2004 Qsubr = intern ("subr");
2005 Qerror_conditions = intern ("error-conditions");
2006 Qerror_message = intern ("error-message");
2007 Qtop_level = intern ("top-level");
2008
2009 Qerror = intern ("error");
2010 Qquit = intern ("quit");
2011 Qwrong_type_argument = intern ("wrong-type-argument");
2012 Qargs_out_of_range = intern ("args-out-of-range");
2013 Qvoid_function = intern ("void-function");
2014 Qcyclic_function_indirection = intern ("cyclic-function-indirection");
2015 Qvoid_variable = intern ("void-variable");
2016 Qsetting_constant = intern ("setting-constant");
2017 Qinvalid_read_syntax = intern ("invalid-read-syntax");
2018
2019 Qinvalid_function = intern ("invalid-function");
2020 Qwrong_number_of_arguments = intern ("wrong-number-of-arguments");
2021 Qno_catch = intern ("no-catch");
2022 Qend_of_file = intern ("end-of-file");
2023 Qarith_error = intern ("arith-error");
2024 Qbeginning_of_buffer = intern ("beginning-of-buffer");
2025 Qend_of_buffer = intern ("end-of-buffer");
2026 Qbuffer_read_only = intern ("buffer-read-only");
2027 Qmark_inactive = intern ("mark-inactive");
2028
2029 Qlistp = intern ("listp");
2030 Qconsp = intern ("consp");
2031 Qsymbolp = intern ("symbolp");
2032 Qintegerp = intern ("integerp");
2033 Qnatnump = intern ("natnump");
2034 Qwholenump = intern ("wholenump");
2035 Qstringp = intern ("stringp");
2036 Qarrayp = intern ("arrayp");
2037 Qsequencep = intern ("sequencep");
2038 Qbufferp = intern ("bufferp");
2039 Qvectorp = intern ("vectorp");
2040 Qchar_or_string_p = intern ("char-or-string-p");
2041 Qmarkerp = intern ("markerp");
2042 Qbuffer_or_string_p = intern ("buffer-or-string-p");
2043 Qinteger_or_marker_p = intern ("integer-or-marker-p");
2044 Qboundp = intern ("boundp");
2045 Qfboundp = intern ("fboundp");
2046
2047 #ifdef LISP_FLOAT_TYPE
2048 Qfloatp = intern ("floatp");
2049 Qnumberp = intern ("numberp");
2050 Qnumber_or_marker_p = intern ("number-or-marker-p");
2051 #endif /* LISP_FLOAT_TYPE */
2052
2053 Qcdr = intern ("cdr");
2054
2055 /* Handle automatic advice activation */
2056 Qad_advice_info = intern ("ad-advice-info");
2057 Qad_activate = intern ("ad-activate");
2058
2059 error_tail = Fcons (Qerror, Qnil);
2060
2061 /* ERROR is used as a signaler for random errors for which nothing else is right */
2062
2063 Fput (Qerror, Qerror_conditions,
2064 error_tail);
2065 Fput (Qerror, Qerror_message,
2066 build_string ("error"));
2067
2068 Fput (Qquit, Qerror_conditions,
2069 Fcons (Qquit, Qnil));
2070 Fput (Qquit, Qerror_message,
2071 build_string ("Quit"));
2072
2073 Fput (Qwrong_type_argument, Qerror_conditions,
2074 Fcons (Qwrong_type_argument, error_tail));
2075 Fput (Qwrong_type_argument, Qerror_message,
2076 build_string ("Wrong type argument"));
2077
2078 Fput (Qargs_out_of_range, Qerror_conditions,
2079 Fcons (Qargs_out_of_range, error_tail));
2080 Fput (Qargs_out_of_range, Qerror_message,
2081 build_string ("Args out of range"));
2082
2083 Fput (Qvoid_function, Qerror_conditions,
2084 Fcons (Qvoid_function, error_tail));
2085 Fput (Qvoid_function, Qerror_message,
2086 build_string ("Symbol's function definition is void"));
2087
2088 Fput (Qcyclic_function_indirection, Qerror_conditions,
2089 Fcons (Qcyclic_function_indirection, error_tail));
2090 Fput (Qcyclic_function_indirection, Qerror_message,
2091 build_string ("Symbol's chain of function indirections contains a loop"));
2092
2093 Fput (Qvoid_variable, Qerror_conditions,
2094 Fcons (Qvoid_variable, error_tail));
2095 Fput (Qvoid_variable, Qerror_message,
2096 build_string ("Symbol's value as variable is void"));
2097
2098 Fput (Qsetting_constant, Qerror_conditions,
2099 Fcons (Qsetting_constant, error_tail));
2100 Fput (Qsetting_constant, Qerror_message,
2101 build_string ("Attempt to set a constant symbol"));
2102
2103 Fput (Qinvalid_read_syntax, Qerror_conditions,
2104 Fcons (Qinvalid_read_syntax, error_tail));
2105 Fput (Qinvalid_read_syntax, Qerror_message,
2106 build_string ("Invalid read syntax"));
2107
2108 Fput (Qinvalid_function, Qerror_conditions,
2109 Fcons (Qinvalid_function, error_tail));
2110 Fput (Qinvalid_function, Qerror_message,
2111 build_string ("Invalid function"));
2112
2113 Fput (Qwrong_number_of_arguments, Qerror_conditions,
2114 Fcons (Qwrong_number_of_arguments, error_tail));
2115 Fput (Qwrong_number_of_arguments, Qerror_message,
2116 build_string ("Wrong number of arguments"));
2117
2118 Fput (Qno_catch, Qerror_conditions,
2119 Fcons (Qno_catch, error_tail));
2120 Fput (Qno_catch, Qerror_message,
2121 build_string ("No catch for tag"));
2122
2123 Fput (Qend_of_file, Qerror_conditions,
2124 Fcons (Qend_of_file, error_tail));
2125 Fput (Qend_of_file, Qerror_message,
2126 build_string ("End of file during parsing"));
2127
2128 arith_tail = Fcons (Qarith_error, error_tail);
2129 Fput (Qarith_error, Qerror_conditions,
2130 arith_tail);
2131 Fput (Qarith_error, Qerror_message,
2132 build_string ("Arithmetic error"));
2133
2134 Fput (Qbeginning_of_buffer, Qerror_conditions,
2135 Fcons (Qbeginning_of_buffer, error_tail));
2136 Fput (Qbeginning_of_buffer, Qerror_message,
2137 build_string ("Beginning of buffer"));
2138
2139 Fput (Qend_of_buffer, Qerror_conditions,
2140 Fcons (Qend_of_buffer, error_tail));
2141 Fput (Qend_of_buffer, Qerror_message,
2142 build_string ("End of buffer"));
2143
2144 Fput (Qbuffer_read_only, Qerror_conditions,
2145 Fcons (Qbuffer_read_only, error_tail));
2146 Fput (Qbuffer_read_only, Qerror_message,
2147 build_string ("Buffer is read-only"));
2148
2149 #ifdef LISP_FLOAT_TYPE
2150 Qrange_error = intern ("range-error");
2151 Qdomain_error = intern ("domain-error");
2152 Qsingularity_error = intern ("singularity-error");
2153 Qoverflow_error = intern ("overflow-error");
2154 Qunderflow_error = intern ("underflow-error");
2155
2156 Fput (Qdomain_error, Qerror_conditions,
2157 Fcons (Qdomain_error, arith_tail));
2158 Fput (Qdomain_error, Qerror_message,
2159 build_string ("Arithmetic domain error"));
2160
2161 Fput (Qrange_error, Qerror_conditions,
2162 Fcons (Qrange_error, arith_tail));
2163 Fput (Qrange_error, Qerror_message,
2164 build_string ("Arithmetic range error"));
2165
2166 Fput (Qsingularity_error, Qerror_conditions,
2167 Fcons (Qsingularity_error, Fcons (Qdomain_error, arith_tail)));
2168 Fput (Qsingularity_error, Qerror_message,
2169 build_string ("Arithmetic singularity error"));
2170
2171 Fput (Qoverflow_error, Qerror_conditions,
2172 Fcons (Qoverflow_error, Fcons (Qdomain_error, arith_tail)));
2173 Fput (Qoverflow_error, Qerror_message,
2174 build_string ("Arithmetic overflow error"));
2175
2176 Fput (Qunderflow_error, Qerror_conditions,
2177 Fcons (Qunderflow_error, Fcons (Qdomain_error, arith_tail)));
2178 Fput (Qunderflow_error, Qerror_message,
2179 build_string ("Arithmetic underflow error"));
2180
2181 staticpro (&Qrange_error);
2182 staticpro (&Qdomain_error);
2183 staticpro (&Qsingularity_error);
2184 staticpro (&Qoverflow_error);
2185 staticpro (&Qunderflow_error);
2186 #endif /* LISP_FLOAT_TYPE */
2187
2188 staticpro (&Qnil);
2189 staticpro (&Qt);
2190 staticpro (&Qquote);
2191 staticpro (&Qlambda);
2192 staticpro (&Qsubr);
2193 staticpro (&Qunbound);
2194 staticpro (&Qerror_conditions);
2195 staticpro (&Qerror_message);
2196 staticpro (&Qtop_level);
2197
2198 staticpro (&Qerror);
2199 staticpro (&Qquit);
2200 staticpro (&Qwrong_type_argument);
2201 staticpro (&Qargs_out_of_range);
2202 staticpro (&Qvoid_function);
2203 staticpro (&Qcyclic_function_indirection);
2204 staticpro (&Qvoid_variable);
2205 staticpro (&Qsetting_constant);
2206 staticpro (&Qinvalid_read_syntax);
2207 staticpro (&Qwrong_number_of_arguments);
2208 staticpro (&Qinvalid_function);
2209 staticpro (&Qno_catch);
2210 staticpro (&Qend_of_file);
2211 staticpro (&Qarith_error);
2212 staticpro (&Qbeginning_of_buffer);
2213 staticpro (&Qend_of_buffer);
2214 staticpro (&Qbuffer_read_only);
2215 staticpro (&Qmark_inactive);
2216
2217 staticpro (&Qlistp);
2218 staticpro (&Qconsp);
2219 staticpro (&Qsymbolp);
2220 staticpro (&Qintegerp);
2221 staticpro (&Qnatnump);
2222 staticpro (&Qwholenump);
2223 staticpro (&Qstringp);
2224 staticpro (&Qarrayp);
2225 staticpro (&Qsequencep);
2226 staticpro (&Qbufferp);
2227 staticpro (&Qvectorp);
2228 staticpro (&Qchar_or_string_p);
2229 staticpro (&Qmarkerp);
2230 staticpro (&Qbuffer_or_string_p);
2231 staticpro (&Qinteger_or_marker_p);
2232 #ifdef LISP_FLOAT_TYPE
2233 staticpro (&Qfloatp);
2234 staticpro (&Qnumberp);
2235 staticpro (&Qnumber_or_marker_p);
2236 #endif /* LISP_FLOAT_TYPE */
2237
2238 staticpro (&Qboundp);
2239 staticpro (&Qfboundp);
2240 staticpro (&Qcdr);
2241 staticpro (&Qad_advice_info);
2242 staticpro (&Qad_activate);
2243
2244 defsubr (&Seq);
2245 defsubr (&Snull);
2246 defsubr (&Slistp);
2247 defsubr (&Snlistp);
2248 defsubr (&Sconsp);
2249 defsubr (&Satom);
2250 defsubr (&Sintegerp);
2251 defsubr (&Sinteger_or_marker_p);
2252 defsubr (&Snumberp);
2253 defsubr (&Snumber_or_marker_p);
2254 #ifdef LISP_FLOAT_TYPE
2255 defsubr (&Sfloatp);
2256 #endif /* LISP_FLOAT_TYPE */
2257 defsubr (&Snatnump);
2258 defsubr (&Ssymbolp);
2259 defsubr (&Sstringp);
2260 defsubr (&Svectorp);
2261 defsubr (&Sarrayp);
2262 defsubr (&Ssequencep);
2263 defsubr (&Sbufferp);
2264 defsubr (&Smarkerp);
2265 defsubr (&Ssubrp);
2266 defsubr (&Sbyte_code_function_p);
2267 defsubr (&Schar_or_string_p);
2268 defsubr (&Scar);
2269 defsubr (&Scdr);
2270 defsubr (&Scar_safe);
2271 defsubr (&Scdr_safe);
2272 defsubr (&Ssetcar);
2273 defsubr (&Ssetcdr);
2274 defsubr (&Ssymbol_function);
2275 defsubr (&Sindirect_function);
2276 defsubr (&Ssymbol_plist);
2277 defsubr (&Ssymbol_name);
2278 defsubr (&Smakunbound);
2279 defsubr (&Sfmakunbound);
2280 defsubr (&Sboundp);
2281 defsubr (&Sfboundp);
2282 defsubr (&Sfset);
2283 defsubr (&Sdefalias);
2284 defsubr (&Sdefine_function);
2285 defsubr (&Ssetplist);
2286 defsubr (&Ssymbol_value);
2287 defsubr (&Sset);
2288 defsubr (&Sdefault_boundp);
2289 defsubr (&Sdefault_value);
2290 defsubr (&Sset_default);
2291 defsubr (&Ssetq_default);
2292 defsubr (&Smake_variable_buffer_local);
2293 defsubr (&Smake_local_variable);
2294 defsubr (&Skill_local_variable);
2295 defsubr (&Saref);
2296 defsubr (&Saset);
2297 defsubr (&Snumber_to_string);
2298 defsubr (&Sstring_to_number);
2299 defsubr (&Seqlsign);
2300 defsubr (&Slss);
2301 defsubr (&Sgtr);
2302 defsubr (&Sleq);
2303 defsubr (&Sgeq);
2304 defsubr (&Sneq);
2305 defsubr (&Szerop);
2306 defsubr (&Splus);
2307 defsubr (&Sminus);
2308 defsubr (&Stimes);
2309 defsubr (&Squo);
2310 defsubr (&Srem);
2311 defsubr (&Smod);
2312 defsubr (&Smax);
2313 defsubr (&Smin);
2314 defsubr (&Slogand);
2315 defsubr (&Slogior);
2316 defsubr (&Slogxor);
2317 defsubr (&Slsh);
2318 defsubr (&Sash);
2319 defsubr (&Sadd1);
2320 defsubr (&Ssub1);
2321 defsubr (&Slognot);
2322
2323 Fset (Qwholenump, Qnatnump);
2324 }
2325
2326 SIGTYPE
2327 arith_error (signo)
2328 int signo;
2329 {
2330 #ifdef USG
2331 /* USG systems forget handlers when they are used;
2332 must reestablish each time */
2333 signal (signo, arith_error);
2334 #endif /* USG */
2335 #ifdef VMS
2336 /* VMS systems are like USG. */
2337 signal (signo, arith_error);
2338 #endif /* VMS */
2339 #ifdef BSD4_1
2340 sigrelse (SIGFPE);
2341 #else /* not BSD4_1 */
2342 sigsetmask (SIGEMPTYMASK);
2343 #endif /* not BSD4_1 */
2344
2345 Fsignal (Qarith_error, Qnil);
2346 }
2347
2348 init_data ()
2349 {
2350 /* Don't do this if just dumping out.
2351 We don't want to call `signal' in this case
2352 so that we don't have trouble with dumping
2353 signal-delivering routines in an inconsistent state. */
2354 #ifndef CANNOT_DUMP
2355 if (!initialized)
2356 return;
2357 #endif /* CANNOT_DUMP */
2358 signal (SIGFPE, arith_error);
2359
2360 #ifdef uts
2361 signal (SIGEMT, arith_error);
2362 #endif /* uts */
2363 }