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