Merged from emacs@sv.gnu.org.
[bpt/emacs.git] / src / data.c
1 /* Primitive operations on Lisp data types for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 1986, 1988, 1993, 1994, 1995, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
21
22
23 #include <config.h>
24 #include <signal.h>
25 #include <stdio.h>
26 #include "lisp.h"
27 #include "puresize.h"
28 #include "charset.h"
29 #include "buffer.h"
30 #include "keyboard.h"
31 #include "frame.h"
32 #include "syssignal.h"
33 #include "termhooks.h" /* For FRAME_KBOARD reference in y-or-n-p. */
34
35 #ifdef STDC_HEADERS
36 #include <float.h>
37 #endif
38
39 /* If IEEE_FLOATING_POINT isn't defined, default it from FLT_*. */
40 #ifndef IEEE_FLOATING_POINT
41 #if (FLT_RADIX == 2 && FLT_MANT_DIG == 24 \
42 && FLT_MIN_EXP == -125 && FLT_MAX_EXP == 128)
43 #define IEEE_FLOATING_POINT 1
44 #else
45 #define IEEE_FLOATING_POINT 0
46 #endif
47 #endif
48
49 /* Work around a problem that happens because math.h on hpux 7
50 defines two static variables--which, in Emacs, are not really static,
51 because `static' is defined as nothing. The problem is that they are
52 here, in floatfns.c, and in lread.c.
53 These macros prevent the name conflict. */
54 #if defined (HPUX) && !defined (HPUX8)
55 #define _MAXLDBL data_c_maxldbl
56 #define _NMAXLDBL data_c_nmaxldbl
57 #endif
58
59 #include <math.h>
60
61 #if !defined (atof)
62 extern double atof ();
63 #endif /* !atof */
64
65 Lisp_Object Qnil, Qt, Qquote, Qlambda, Qsubr, Qunbound;
66 Lisp_Object Qerror_conditions, Qerror_message, Qtop_level;
67 Lisp_Object Qerror, Qquit, Qwrong_type_argument, Qargs_out_of_range;
68 Lisp_Object Qvoid_variable, Qvoid_function, Qcyclic_function_indirection;
69 Lisp_Object Qcyclic_variable_indirection, Qcircular_list;
70 Lisp_Object Qsetting_constant, Qinvalid_read_syntax;
71 Lisp_Object Qinvalid_function, Qwrong_number_of_arguments, Qno_catch;
72 Lisp_Object Qend_of_file, Qarith_error, Qmark_inactive;
73 Lisp_Object Qbeginning_of_buffer, Qend_of_buffer, Qbuffer_read_only;
74 Lisp_Object Qtext_read_only;
75
76 Lisp_Object Qintegerp, Qnatnump, Qwholenump, Qsymbolp, Qlistp, Qconsp;
77 Lisp_Object Qstringp, Qarrayp, Qsequencep, Qbufferp;
78 Lisp_Object Qchar_or_string_p, Qmarkerp, Qinteger_or_marker_p, Qvectorp;
79 Lisp_Object Qbuffer_or_string_p, Qkeywordp;
80 Lisp_Object Qboundp, Qfboundp;
81 Lisp_Object Qchar_table_p, Qvector_or_char_table_p;
82
83 Lisp_Object Qcdr;
84 Lisp_Object Qad_advice_info, Qad_activate_internal;
85
86 Lisp_Object Qrange_error, Qdomain_error, Qsingularity_error;
87 Lisp_Object Qoverflow_error, Qunderflow_error;
88
89 Lisp_Object Qfloatp;
90 Lisp_Object Qnumberp, Qnumber_or_marker_p;
91
92 Lisp_Object Qinteger;
93 static Lisp_Object Qsymbol, Qstring, Qcons, Qmarker, Qoverlay;
94 static Lisp_Object Qfloat, Qwindow_configuration, Qwindow;
95 Lisp_Object Qprocess;
96 static Lisp_Object Qcompiled_function, Qbuffer, Qframe, Qvector;
97 static Lisp_Object Qchar_table, Qbool_vector, Qhash_table;
98 static Lisp_Object Qsubrp, Qmany, Qunevalled;
99
100 static Lisp_Object swap_in_symval_forwarding P_ ((Lisp_Object, Lisp_Object));
101
102 Lisp_Object Vmost_positive_fixnum, Vmost_negative_fixnum;
103
104
105 void
106 circular_list_error (list)
107 Lisp_Object list;
108 {
109 Fsignal (Qcircular_list, list);
110 }
111
112
113 Lisp_Object
114 wrong_type_argument (predicate, value)
115 register Lisp_Object predicate, value;
116 {
117 /* If VALUE is not even a valid Lisp object, abort here
118 where we can get a backtrace showing where it came from. */
119 if ((unsigned int) XGCTYPE (value) >= Lisp_Type_Limit)
120 abort ();
121
122 Fsignal (Qwrong_type_argument, list2 (predicate, value));
123
124 /* This function is marked as NO_RETURN, gcc would warn if it has a
125 return statement or if falls off the function. Other compilers
126 warn if no return statement is present. */
127 #ifndef __GNUC__
128 return value;
129 #else
130 abort ();
131 #endif
132 }
133
134 void
135 pure_write_error ()
136 {
137 error ("Attempt to modify read-only object");
138 }
139
140 void
141 args_out_of_range (a1, a2)
142 Lisp_Object a1, a2;
143 {
144 while (1)
145 Fsignal (Qargs_out_of_range, Fcons (a1, Fcons (a2, Qnil)));
146 }
147
148 void
149 args_out_of_range_3 (a1, a2, a3)
150 Lisp_Object a1, a2, a3;
151 {
152 while (1)
153 Fsignal (Qargs_out_of_range, Fcons (a1, Fcons (a2, Fcons (a3, Qnil))));
154 }
155
156 /* On some machines, XINT needs a temporary location.
157 Here it is, in case it is needed. */
158
159 int sign_extend_temp;
160
161 /* On a few machines, XINT can only be done by calling this. */
162
163 int
164 sign_extend_lisp_int (num)
165 EMACS_INT num;
166 {
167 if (num & (((EMACS_INT) 1) << (VALBITS - 1)))
168 return num | (((EMACS_INT) (-1)) << VALBITS);
169 else
170 return num & ((((EMACS_INT) 1) << VALBITS) - 1);
171 }
172 \f
173 /* Data type predicates */
174
175 DEFUN ("eq", Feq, Seq, 2, 2, 0,
176 doc: /* Return t if the two args are the same Lisp object. */)
177 (obj1, obj2)
178 Lisp_Object obj1, obj2;
179 {
180 if (EQ (obj1, obj2))
181 return Qt;
182 return Qnil;
183 }
184
185 DEFUN ("null", Fnull, Snull, 1, 1, 0,
186 doc: /* Return t if OBJECT is nil. */)
187 (object)
188 Lisp_Object object;
189 {
190 if (NILP (object))
191 return Qt;
192 return Qnil;
193 }
194
195 DEFUN ("type-of", Ftype_of, Stype_of, 1, 1, 0,
196 doc: /* Return a symbol representing the type of OBJECT.
197 The symbol returned names the object's basic type;
198 for example, (type-of 1) returns `integer'. */)
199 (object)
200 Lisp_Object object;
201 {
202 switch (XGCTYPE (object))
203 {
204 case Lisp_Int:
205 return Qinteger;
206
207 case Lisp_Symbol:
208 return Qsymbol;
209
210 case Lisp_String:
211 return Qstring;
212
213 case Lisp_Cons:
214 return Qcons;
215
216 case Lisp_Misc:
217 switch (XMISCTYPE (object))
218 {
219 case Lisp_Misc_Marker:
220 return Qmarker;
221 case Lisp_Misc_Overlay:
222 return Qoverlay;
223 case Lisp_Misc_Float:
224 return Qfloat;
225 }
226 abort ();
227
228 case Lisp_Vectorlike:
229 if (GC_WINDOW_CONFIGURATIONP (object))
230 return Qwindow_configuration;
231 if (GC_PROCESSP (object))
232 return Qprocess;
233 if (GC_WINDOWP (object))
234 return Qwindow;
235 if (GC_SUBRP (object))
236 return Qsubr;
237 if (GC_COMPILEDP (object))
238 return Qcompiled_function;
239 if (GC_BUFFERP (object))
240 return Qbuffer;
241 if (GC_CHAR_TABLE_P (object))
242 return Qchar_table;
243 if (GC_BOOL_VECTOR_P (object))
244 return Qbool_vector;
245 if (GC_FRAMEP (object))
246 return Qframe;
247 if (GC_HASH_TABLE_P (object))
248 return Qhash_table;
249 return Qvector;
250
251 case Lisp_Float:
252 return Qfloat;
253
254 default:
255 abort ();
256 }
257 }
258
259 DEFUN ("consp", Fconsp, Sconsp, 1, 1, 0,
260 doc: /* Return t if OBJECT is a cons cell. */)
261 (object)
262 Lisp_Object object;
263 {
264 if (CONSP (object))
265 return Qt;
266 return Qnil;
267 }
268
269 DEFUN ("atom", Fatom, Satom, 1, 1, 0,
270 doc: /* Return t if OBJECT is not a cons cell. This includes nil. */)
271 (object)
272 Lisp_Object object;
273 {
274 if (CONSP (object))
275 return Qnil;
276 return Qt;
277 }
278
279 DEFUN ("listp", Flistp, Slistp, 1, 1, 0,
280 doc: /* Return t if OBJECT is a list, that is, a cons cell or nil.
281 Otherwise, return nil. */)
282 (object)
283 Lisp_Object object;
284 {
285 if (CONSP (object) || NILP (object))
286 return Qt;
287 return Qnil;
288 }
289
290 DEFUN ("nlistp", Fnlistp, Snlistp, 1, 1, 0,
291 doc: /* 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,
301 doc: /* Return t if OBJECT is a symbol. */)
302 (object)
303 Lisp_Object object;
304 {
305 if (SYMBOLP (object))
306 return Qt;
307 return Qnil;
308 }
309
310 /* Define this in C to avoid unnecessarily consing up the symbol
311 name. */
312 DEFUN ("keywordp", Fkeywordp, Skeywordp, 1, 1, 0,
313 doc: /* Return t if OBJECT is a keyword.
314 This means that it is a symbol with a print name beginning with `:'
315 interned in the initial obarray. */)
316 (object)
317 Lisp_Object object;
318 {
319 if (SYMBOLP (object)
320 && SREF (SYMBOL_NAME (object), 0) == ':'
321 && SYMBOL_INTERNED_IN_INITIAL_OBARRAY_P (object))
322 return Qt;
323 return Qnil;
324 }
325
326 DEFUN ("vectorp", Fvectorp, Svectorp, 1, 1, 0,
327 doc: /* Return t if OBJECT is a vector. */)
328 (object)
329 Lisp_Object object;
330 {
331 if (VECTORP (object))
332 return Qt;
333 return Qnil;
334 }
335
336 DEFUN ("stringp", Fstringp, Sstringp, 1, 1, 0,
337 doc: /* Return t if OBJECT is a string. */)
338 (object)
339 Lisp_Object object;
340 {
341 if (STRINGP (object))
342 return Qt;
343 return Qnil;
344 }
345
346 DEFUN ("multibyte-string-p", Fmultibyte_string_p, Smultibyte_string_p,
347 1, 1, 0,
348 doc: /* Return t if OBJECT is a multibyte string. */)
349 (object)
350 Lisp_Object object;
351 {
352 if (STRINGP (object) && STRING_MULTIBYTE (object))
353 return Qt;
354 return Qnil;
355 }
356
357 DEFUN ("char-table-p", Fchar_table_p, Schar_table_p, 1, 1, 0,
358 doc: /* Return t if OBJECT is a char-table. */)
359 (object)
360 Lisp_Object object;
361 {
362 if (CHAR_TABLE_P (object))
363 return Qt;
364 return Qnil;
365 }
366
367 DEFUN ("vector-or-char-table-p", Fvector_or_char_table_p,
368 Svector_or_char_table_p, 1, 1, 0,
369 doc: /* Return t if OBJECT is a char-table or vector. */)
370 (object)
371 Lisp_Object object;
372 {
373 if (VECTORP (object) || CHAR_TABLE_P (object))
374 return Qt;
375 return Qnil;
376 }
377
378 DEFUN ("bool-vector-p", Fbool_vector_p, Sbool_vector_p, 1, 1, 0,
379 doc: /* Return t if OBJECT is a bool-vector. */)
380 (object)
381 Lisp_Object object;
382 {
383 if (BOOL_VECTOR_P (object))
384 return Qt;
385 return Qnil;
386 }
387
388 DEFUN ("arrayp", Farrayp, Sarrayp, 1, 1, 0,
389 doc: /* Return t if OBJECT is an array (string or vector). */)
390 (object)
391 Lisp_Object object;
392 {
393 if (ARRAYP (object))
394 return Qt;
395 return Qnil;
396 }
397
398 DEFUN ("sequencep", Fsequencep, Ssequencep, 1, 1, 0,
399 doc: /* Return t if OBJECT is a sequence (list or array). */)
400 (object)
401 register Lisp_Object object;
402 {
403 if (CONSP (object) || NILP (object) || ARRAYP (object))
404 return Qt;
405 return Qnil;
406 }
407
408 DEFUN ("bufferp", Fbufferp, Sbufferp, 1, 1, 0,
409 doc: /* Return t if OBJECT is an editor buffer. */)
410 (object)
411 Lisp_Object object;
412 {
413 if (BUFFERP (object))
414 return Qt;
415 return Qnil;
416 }
417
418 DEFUN ("markerp", Fmarkerp, Smarkerp, 1, 1, 0,
419 doc: /* Return t if OBJECT is a marker (editor pointer). */)
420 (object)
421 Lisp_Object object;
422 {
423 if (MARKERP (object))
424 return Qt;
425 return Qnil;
426 }
427
428 DEFUN ("subrp", Fsubrp, Ssubrp, 1, 1, 0,
429 doc: /* Return t if OBJECT is a built-in function. */)
430 (object)
431 Lisp_Object object;
432 {
433 if (SUBRP (object))
434 return Qt;
435 return Qnil;
436 }
437
438 DEFUN ("byte-code-function-p", Fbyte_code_function_p, Sbyte_code_function_p,
439 1, 1, 0,
440 doc: /* Return t if OBJECT is a byte-compiled function object. */)
441 (object)
442 Lisp_Object object;
443 {
444 if (COMPILEDP (object))
445 return Qt;
446 return Qnil;
447 }
448
449 DEFUN ("char-or-string-p", Fchar_or_string_p, Schar_or_string_p, 1, 1, 0,
450 doc: /* Return t if OBJECT is a character (an integer) or a string. */)
451 (object)
452 register Lisp_Object object;
453 {
454 if (INTEGERP (object) || STRINGP (object))
455 return Qt;
456 return Qnil;
457 }
458 \f
459 DEFUN ("integerp", Fintegerp, Sintegerp, 1, 1, 0,
460 doc: /* Return t if OBJECT is an integer. */)
461 (object)
462 Lisp_Object object;
463 {
464 if (INTEGERP (object))
465 return Qt;
466 return Qnil;
467 }
468
469 DEFUN ("integer-or-marker-p", Finteger_or_marker_p, Sinteger_or_marker_p, 1, 1, 0,
470 doc: /* Return t if OBJECT is an integer or a marker (editor pointer). */)
471 (object)
472 register Lisp_Object object;
473 {
474 if (MARKERP (object) || INTEGERP (object))
475 return Qt;
476 return Qnil;
477 }
478
479 DEFUN ("natnump", Fnatnump, Snatnump, 1, 1, 0,
480 doc: /* Return t if OBJECT is a nonnegative integer. */)
481 (object)
482 Lisp_Object object;
483 {
484 if (NATNUMP (object))
485 return Qt;
486 return Qnil;
487 }
488
489 DEFUN ("numberp", Fnumberp, Snumberp, 1, 1, 0,
490 doc: /* Return t if OBJECT is a number (floating point or integer). */)
491 (object)
492 Lisp_Object object;
493 {
494 if (NUMBERP (object))
495 return Qt;
496 else
497 return Qnil;
498 }
499
500 DEFUN ("number-or-marker-p", Fnumber_or_marker_p,
501 Snumber_or_marker_p, 1, 1, 0,
502 doc: /* Return t if OBJECT is a number or a marker. */)
503 (object)
504 Lisp_Object object;
505 {
506 if (NUMBERP (object) || MARKERP (object))
507 return Qt;
508 return Qnil;
509 }
510
511 DEFUN ("floatp", Ffloatp, Sfloatp, 1, 1, 0,
512 doc: /* Return t if OBJECT is a floating point number. */)
513 (object)
514 Lisp_Object object;
515 {
516 if (FLOATP (object))
517 return Qt;
518 return Qnil;
519 }
520
521 \f
522 /* Extract and set components of lists */
523
524 DEFUN ("car", Fcar, Scar, 1, 1, 0,
525 doc: /* Return the car of LIST. If arg is nil, return nil.
526 Error if arg is not nil and not a cons cell. See also `car-safe'.
527
528 See Info node `(elisp)Cons Cells' for a discussion of related basic
529 Lisp concepts such as car, cdr, cons cell and list. */)
530 (list)
531 register Lisp_Object list;
532 {
533 return CAR (list);
534 }
535
536 DEFUN ("car-safe", Fcar_safe, Scar_safe, 1, 1, 0,
537 doc: /* Return the car of OBJECT if it is a cons cell, or else nil. */)
538 (object)
539 Lisp_Object object;
540 {
541 return CAR_SAFE (object);
542 }
543
544 DEFUN ("cdr", Fcdr, Scdr, 1, 1, 0,
545 doc: /* Return the cdr of LIST. If arg is nil, return nil.
546 Error if arg is not nil and not a cons cell. See also `cdr-safe'.
547
548 See Info node `(elisp)Cons Cells' for a discussion of related basic
549 Lisp concepts such as cdr, car, cons cell and list. */)
550 (list)
551 register Lisp_Object list;
552 {
553 return CDR (list);
554 }
555
556 DEFUN ("cdr-safe", Fcdr_safe, Scdr_safe, 1, 1, 0,
557 doc: /* Return the cdr of OBJECT if it is a cons cell, or else nil. */)
558 (object)
559 Lisp_Object object;
560 {
561 return CDR_SAFE (object);
562 }
563
564 DEFUN ("setcar", Fsetcar, Ssetcar, 2, 2, 0,
565 doc: /* Set the car of CELL to be NEWCAR. Returns NEWCAR. */)
566 (cell, newcar)
567 register Lisp_Object cell, newcar;
568 {
569 CHECK_CONS (cell);
570 CHECK_IMPURE (cell);
571 XSETCAR (cell, newcar);
572 return newcar;
573 }
574
575 DEFUN ("setcdr", Fsetcdr, Ssetcdr, 2, 2, 0,
576 doc: /* Set the cdr of CELL to be NEWCDR. Returns NEWCDR. */)
577 (cell, newcdr)
578 register Lisp_Object cell, newcdr;
579 {
580 CHECK_CONS (cell);
581 CHECK_IMPURE (cell);
582 XSETCDR (cell, newcdr);
583 return newcdr;
584 }
585 \f
586 /* Extract and set components of symbols */
587
588 DEFUN ("boundp", Fboundp, Sboundp, 1, 1, 0,
589 doc: /* Return t if SYMBOL's value is not void. */)
590 (symbol)
591 register Lisp_Object symbol;
592 {
593 Lisp_Object valcontents;
594 CHECK_SYMBOL (symbol);
595
596 valcontents = SYMBOL_VALUE (symbol);
597
598 if (BUFFER_LOCAL_VALUEP (valcontents)
599 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
600 valcontents = swap_in_symval_forwarding (symbol, valcontents);
601
602 return (EQ (valcontents, Qunbound) ? Qnil : Qt);
603 }
604
605 DEFUN ("fboundp", Ffboundp, Sfboundp, 1, 1, 0,
606 doc: /* Return t if SYMBOL's function definition is not void. */)
607 (symbol)
608 register Lisp_Object symbol;
609 {
610 CHECK_SYMBOL (symbol);
611 return (EQ (XSYMBOL (symbol)->function, Qunbound) ? Qnil : Qt);
612 }
613
614 DEFUN ("makunbound", Fmakunbound, Smakunbound, 1, 1, 0,
615 doc: /* Make SYMBOL's value be void.
616 Return SYMBOL. */)
617 (symbol)
618 register Lisp_Object symbol;
619 {
620 CHECK_SYMBOL (symbol);
621 if (XSYMBOL (symbol)->constant)
622 return Fsignal (Qsetting_constant, Fcons (symbol, Qnil));
623 Fset (symbol, Qunbound);
624 return symbol;
625 }
626
627 DEFUN ("fmakunbound", Ffmakunbound, Sfmakunbound, 1, 1, 0,
628 doc: /* Make SYMBOL's function definition be void.
629 Return SYMBOL. */)
630 (symbol)
631 register Lisp_Object symbol;
632 {
633 CHECK_SYMBOL (symbol);
634 if (NILP (symbol) || EQ (symbol, Qt))
635 return Fsignal (Qsetting_constant, Fcons (symbol, Qnil));
636 XSYMBOL (symbol)->function = Qunbound;
637 return symbol;
638 }
639
640 DEFUN ("symbol-function", Fsymbol_function, Ssymbol_function, 1, 1, 0,
641 doc: /* Return SYMBOL's function definition. Error if that is void. */)
642 (symbol)
643 register Lisp_Object symbol;
644 {
645 CHECK_SYMBOL (symbol);
646 if (EQ (XSYMBOL (symbol)->function, Qunbound))
647 return Fsignal (Qvoid_function, Fcons (symbol, Qnil));
648 return XSYMBOL (symbol)->function;
649 }
650
651 DEFUN ("symbol-plist", Fsymbol_plist, Ssymbol_plist, 1, 1, 0,
652 doc: /* Return SYMBOL's property list. */)
653 (symbol)
654 register Lisp_Object symbol;
655 {
656 CHECK_SYMBOL (symbol);
657 return XSYMBOL (symbol)->plist;
658 }
659
660 DEFUN ("symbol-name", Fsymbol_name, Ssymbol_name, 1, 1, 0,
661 doc: /* Return SYMBOL's name, a string. */)
662 (symbol)
663 register Lisp_Object symbol;
664 {
665 register Lisp_Object name;
666
667 CHECK_SYMBOL (symbol);
668 name = SYMBOL_NAME (symbol);
669 return name;
670 }
671
672 DEFUN ("fset", Ffset, Sfset, 2, 2, 0,
673 doc: /* Set SYMBOL's function definition to DEFINITION, and return DEFINITION. */)
674 (symbol, definition)
675 register Lisp_Object symbol, definition;
676 {
677 CHECK_SYMBOL (symbol);
678 if (NILP (symbol) || EQ (symbol, Qt))
679 return Fsignal (Qsetting_constant, Fcons (symbol, Qnil));
680 if (!NILP (Vautoload_queue) && !EQ (XSYMBOL (symbol)->function, Qunbound))
681 Vautoload_queue = Fcons (Fcons (symbol, XSYMBOL (symbol)->function),
682 Vautoload_queue);
683 XSYMBOL (symbol)->function = definition;
684 /* Handle automatic advice activation */
685 if (CONSP (XSYMBOL (symbol)->plist) && !NILP (Fget (symbol, Qad_advice_info)))
686 {
687 call2 (Qad_activate_internal, symbol, Qnil);
688 definition = XSYMBOL (symbol)->function;
689 }
690 return definition;
691 }
692
693 extern Lisp_Object Qfunction_documentation;
694
695 DEFUN ("defalias", Fdefalias, Sdefalias, 2, 3, 0,
696 doc: /* Set SYMBOL's function definition to DEFINITION, and return DEFINITION.
697 Associates the function with the current load file, if any.
698 The optional third argument DOCSTRING specifies the documentation string
699 for SYMBOL; if it is omitted or nil, SYMBOL uses the documentation string
700 determined by DEFINITION. */)
701 (symbol, definition, docstring)
702 register Lisp_Object symbol, definition, docstring;
703 {
704 CHECK_SYMBOL (symbol);
705 if (CONSP (XSYMBOL (symbol)->function)
706 && EQ (XCAR (XSYMBOL (symbol)->function), Qautoload))
707 LOADHIST_ATTACH (Fcons (Qt, symbol));
708 definition = Ffset (symbol, definition);
709 LOADHIST_ATTACH (Fcons (Qdefun, symbol));
710 if (!NILP (docstring))
711 Fput (symbol, Qfunction_documentation, docstring);
712 return definition;
713 }
714
715 DEFUN ("setplist", Fsetplist, Ssetplist, 2, 2, 0,
716 doc: /* Set SYMBOL's property list to NEWPLIST, and return NEWPLIST. */)
717 (symbol, newplist)
718 register Lisp_Object symbol, newplist;
719 {
720 CHECK_SYMBOL (symbol);
721 XSYMBOL (symbol)->plist = newplist;
722 return newplist;
723 }
724
725 DEFUN ("subr-arity", Fsubr_arity, Ssubr_arity, 1, 1, 0,
726 doc: /* Return minimum and maximum number of args allowed for SUBR.
727 SUBR must be a built-in function.
728 The returned value is a pair (MIN . MAX). MIN is the minimum number
729 of args. MAX is the maximum number or the symbol `many', for a
730 function with `&rest' args, or `unevalled' for a special form. */)
731 (subr)
732 Lisp_Object subr;
733 {
734 short minargs, maxargs;
735 CHECK_SUBR (subr);
736 minargs = XSUBR (subr)->min_args;
737 maxargs = XSUBR (subr)->max_args;
738 if (maxargs == MANY)
739 return Fcons (make_number (minargs), Qmany);
740 else if (maxargs == UNEVALLED)
741 return Fcons (make_number (minargs), Qunevalled);
742 else
743 return Fcons (make_number (minargs), make_number (maxargs));
744 }
745
746 DEFUN ("subr-name", Fsubr_name, Ssubr_name, 1, 1, 0,
747 doc: /* Return name of subroutine SUBR.
748 SUBR must be a built-in function. */)
749 (subr)
750 Lisp_Object subr;
751 {
752 const char *name;
753 CHECK_SUBR (subr);
754 name = XSUBR (subr)->symbol_name;
755 return make_string (name, strlen (name));
756 }
757
758 DEFUN ("interactive-form", Finteractive_form, Sinteractive_form, 1, 1, 0,
759 doc: /* Return the interactive form of CMD or nil if none.
760 If CMD is not a command, the return value is nil.
761 Value, if non-nil, is a list \(interactive SPEC). */)
762 (cmd)
763 Lisp_Object cmd;
764 {
765 Lisp_Object fun = indirect_function (cmd);
766
767 if (SUBRP (fun))
768 {
769 if (XSUBR (fun)->prompt)
770 return list2 (Qinteractive, build_string (XSUBR (fun)->prompt));
771 }
772 else if (COMPILEDP (fun))
773 {
774 if ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE)
775 return list2 (Qinteractive, AREF (fun, COMPILED_INTERACTIVE));
776 }
777 else if (CONSP (fun))
778 {
779 Lisp_Object funcar = XCAR (fun);
780 if (EQ (funcar, Qlambda))
781 return Fassq (Qinteractive, Fcdr (XCDR (fun)));
782 else if (EQ (funcar, Qautoload))
783 {
784 struct gcpro gcpro1;
785 GCPRO1 (cmd);
786 do_autoload (fun, cmd);
787 UNGCPRO;
788 return Finteractive_form (cmd);
789 }
790 }
791 return Qnil;
792 }
793
794 \f
795 /***********************************************************************
796 Getting and Setting Values of Symbols
797 ***********************************************************************/
798
799 /* Return the symbol holding SYMBOL's value. Signal
800 `cyclic-variable-indirection' if SYMBOL's chain of variable
801 indirections contains a loop. */
802
803 Lisp_Object
804 indirect_variable (symbol)
805 Lisp_Object symbol;
806 {
807 Lisp_Object tortoise, hare;
808
809 hare = tortoise = symbol;
810
811 while (XSYMBOL (hare)->indirect_variable)
812 {
813 hare = XSYMBOL (hare)->value;
814 if (!XSYMBOL (hare)->indirect_variable)
815 break;
816
817 hare = XSYMBOL (hare)->value;
818 tortoise = XSYMBOL (tortoise)->value;
819
820 if (EQ (hare, tortoise))
821 Fsignal (Qcyclic_variable_indirection, Fcons (symbol, Qnil));
822 }
823
824 return hare;
825 }
826
827
828 DEFUN ("indirect-variable", Findirect_variable, Sindirect_variable, 1, 1, 0,
829 doc: /* Return the variable at the end of OBJECT's variable chain.
830 If OBJECT is a symbol, follow all variable indirections and return the final
831 variable. If OBJECT is not a symbol, just return it.
832 Signal a cyclic-variable-indirection error if there is a loop in the
833 variable chain of symbols. */)
834 (object)
835 Lisp_Object object;
836 {
837 if (SYMBOLP (object))
838 object = indirect_variable (object);
839 return object;
840 }
841
842
843 /* Given the raw contents of a symbol value cell,
844 return the Lisp value of the symbol.
845 This does not handle buffer-local variables; use
846 swap_in_symval_forwarding for that. */
847
848 Lisp_Object
849 do_symval_forwarding (valcontents)
850 register Lisp_Object valcontents;
851 {
852 register Lisp_Object val;
853 int offset;
854 if (MISCP (valcontents))
855 switch (XMISCTYPE (valcontents))
856 {
857 case Lisp_Misc_Intfwd:
858 XSETINT (val, *XINTFWD (valcontents)->intvar);
859 return val;
860
861 case Lisp_Misc_Boolfwd:
862 return (*XBOOLFWD (valcontents)->boolvar ? Qt : Qnil);
863
864 case Lisp_Misc_Objfwd:
865 return *XOBJFWD (valcontents)->objvar;
866
867 case Lisp_Misc_Buffer_Objfwd:
868 offset = XBUFFER_OBJFWD (valcontents)->offset;
869 return PER_BUFFER_VALUE (current_buffer, offset);
870
871 case Lisp_Misc_Kboard_Objfwd:
872 offset = XKBOARD_OBJFWD (valcontents)->offset;
873 /* We used to simply use current_kboard here, but from Lisp
874 code, it's value is often unexpected. It seems nicer to
875 allow constructions like this to work as intuitively expected:
876
877 (with-selected-frame frame
878 (define-key local-function-map "\eOP" [f1]))
879
880 On the other hand, this affects the semantics of
881 last-command and real-last-command, and people may rely on
882 that. I took a quick look at the Lisp codebase, and I
883 don't think anything will break. --lorentey */
884 return *(Lisp_Object *)(offset + (char *)FRAME_KBOARD (SELECTED_FRAME ()));
885 }
886 return valcontents;
887 }
888
889 /* Store NEWVAL into SYMBOL, where VALCONTENTS is found in the value cell
890 of SYMBOL. If SYMBOL is buffer-local, VALCONTENTS should be the
891 buffer-independent contents of the value cell: forwarded just one
892 step past the buffer-localness.
893
894 BUF non-zero means set the value in buffer BUF instead of the
895 current buffer. This only plays a role for per-buffer variables. */
896
897 void
898 store_symval_forwarding (symbol, valcontents, newval, buf)
899 Lisp_Object symbol;
900 register Lisp_Object valcontents, newval;
901 struct buffer *buf;
902 {
903 switch (SWITCH_ENUM_CAST (XTYPE (valcontents)))
904 {
905 case Lisp_Misc:
906 switch (XMISCTYPE (valcontents))
907 {
908 case Lisp_Misc_Intfwd:
909 CHECK_NUMBER (newval);
910 *XINTFWD (valcontents)->intvar = XINT (newval);
911 if (*XINTFWD (valcontents)->intvar != XINT (newval))
912 error ("Value out of range for variable `%s'",
913 SDATA (SYMBOL_NAME (symbol)));
914 break;
915
916 case Lisp_Misc_Boolfwd:
917 *XBOOLFWD (valcontents)->boolvar = NILP (newval) ? 0 : 1;
918 break;
919
920 case Lisp_Misc_Objfwd:
921 *XOBJFWD (valcontents)->objvar = newval;
922
923 /* If this variable is a default for something stored
924 in the buffer itself, such as default-fill-column,
925 find the buffers that don't have local values for it
926 and update them. */
927 if (XOBJFWD (valcontents)->objvar > (Lisp_Object *) &buffer_defaults
928 && XOBJFWD (valcontents)->objvar < (Lisp_Object *) (&buffer_defaults + 1))
929 {
930 int offset = ((char *) XOBJFWD (valcontents)->objvar
931 - (char *) &buffer_defaults);
932 int idx = PER_BUFFER_IDX (offset);
933
934 Lisp_Object tail;
935
936 if (idx <= 0)
937 break;
938
939 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
940 {
941 Lisp_Object buf;
942 struct buffer *b;
943
944 buf = Fcdr (XCAR (tail));
945 if (!BUFFERP (buf)) continue;
946 b = XBUFFER (buf);
947
948 if (! PER_BUFFER_VALUE_P (b, idx))
949 PER_BUFFER_VALUE (b, offset) = newval;
950 }
951 }
952 break;
953
954 case Lisp_Misc_Buffer_Objfwd:
955 {
956 int offset = XBUFFER_OBJFWD (valcontents)->offset;
957 Lisp_Object type;
958
959 type = PER_BUFFER_TYPE (offset);
960 if (! NILP (type) && ! NILP (newval)
961 && XTYPE (newval) != XINT (type))
962 buffer_slot_type_mismatch (offset);
963
964 if (buf == NULL)
965 buf = current_buffer;
966 PER_BUFFER_VALUE (buf, offset) = newval;
967 }
968 break;
969
970 case Lisp_Misc_Kboard_Objfwd:
971 {
972 char *base = (char *) FRAME_KBOARD (SELECTED_FRAME ());
973 char *p = base + XKBOARD_OBJFWD (valcontents)->offset;
974 *(Lisp_Object *) p = newval;
975 }
976 break;
977
978 default:
979 goto def;
980 }
981 break;
982
983 default:
984 def:
985 valcontents = SYMBOL_VALUE (symbol);
986 if (BUFFER_LOCAL_VALUEP (valcontents)
987 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
988 XBUFFER_LOCAL_VALUE (valcontents)->realvalue = newval;
989 else
990 SET_SYMBOL_VALUE (symbol, newval);
991 }
992 }
993
994 /* Set up SYMBOL to refer to its global binding.
995 This makes it safe to alter the status of other bindings. */
996
997 void
998 swap_in_global_binding (symbol)
999 Lisp_Object symbol;
1000 {
1001 Lisp_Object valcontents, cdr;
1002
1003 valcontents = SYMBOL_VALUE (symbol);
1004 if (!BUFFER_LOCAL_VALUEP (valcontents)
1005 && !SOME_BUFFER_LOCAL_VALUEP (valcontents))
1006 abort ();
1007 cdr = XBUFFER_LOCAL_VALUE (valcontents)->cdr;
1008
1009 /* Unload the previously loaded binding. */
1010 Fsetcdr (XCAR (cdr),
1011 do_symval_forwarding (XBUFFER_LOCAL_VALUE (valcontents)->realvalue));
1012
1013 /* Select the global binding in the symbol. */
1014 XSETCAR (cdr, cdr);
1015 store_symval_forwarding (symbol, valcontents, XCDR (cdr), NULL);
1016
1017 /* Indicate that the global binding is set up now. */
1018 XBUFFER_LOCAL_VALUE (valcontents)->frame = Qnil;
1019 XBUFFER_LOCAL_VALUE (valcontents)->buffer = Qnil;
1020 XBUFFER_LOCAL_VALUE (valcontents)->found_for_frame = 0;
1021 XBUFFER_LOCAL_VALUE (valcontents)->found_for_buffer = 0;
1022 }
1023
1024 /* Set up the buffer-local symbol SYMBOL for validity in the current buffer.
1025 VALCONTENTS is the contents of its value cell,
1026 which points to a struct Lisp_Buffer_Local_Value.
1027
1028 Return the value forwarded one step past the buffer-local stage.
1029 This could be another forwarding pointer. */
1030
1031 static Lisp_Object
1032 swap_in_symval_forwarding (symbol, valcontents)
1033 Lisp_Object symbol, valcontents;
1034 {
1035 register Lisp_Object tem1;
1036
1037 tem1 = XBUFFER_LOCAL_VALUE (valcontents)->buffer;
1038
1039 if (NILP (tem1)
1040 || current_buffer != XBUFFER (tem1)
1041 || (XBUFFER_LOCAL_VALUE (valcontents)->check_frame
1042 && ! EQ (selected_frame, XBUFFER_LOCAL_VALUE (valcontents)->frame)))
1043 {
1044 if (XSYMBOL (symbol)->indirect_variable)
1045 symbol = indirect_variable (symbol);
1046
1047 /* Unload the previously loaded binding. */
1048 tem1 = XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
1049 Fsetcdr (tem1,
1050 do_symval_forwarding (XBUFFER_LOCAL_VALUE (valcontents)->realvalue));
1051 /* Choose the new binding. */
1052 tem1 = assq_no_quit (symbol, current_buffer->local_var_alist);
1053 XBUFFER_LOCAL_VALUE (valcontents)->found_for_frame = 0;
1054 XBUFFER_LOCAL_VALUE (valcontents)->found_for_buffer = 0;
1055 if (NILP (tem1))
1056 {
1057 if (XBUFFER_LOCAL_VALUE (valcontents)->check_frame)
1058 tem1 = assq_no_quit (symbol, XFRAME (selected_frame)->param_alist);
1059 if (! NILP (tem1))
1060 XBUFFER_LOCAL_VALUE (valcontents)->found_for_frame = 1;
1061 else
1062 tem1 = XBUFFER_LOCAL_VALUE (valcontents)->cdr;
1063 }
1064 else
1065 XBUFFER_LOCAL_VALUE (valcontents)->found_for_buffer = 1;
1066
1067 /* Load the new binding. */
1068 XSETCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr, tem1);
1069 XSETBUFFER (XBUFFER_LOCAL_VALUE (valcontents)->buffer, current_buffer);
1070 XBUFFER_LOCAL_VALUE (valcontents)->frame = selected_frame;
1071 store_symval_forwarding (symbol,
1072 XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1073 Fcdr (tem1), NULL);
1074 }
1075 return XBUFFER_LOCAL_VALUE (valcontents)->realvalue;
1076 }
1077 \f
1078 /* Find the value of a symbol, returning Qunbound if it's not bound.
1079 This is helpful for code which just wants to get a variable's value
1080 if it has one, without signaling an error.
1081 Note that it must not be possible to quit
1082 within this function. Great care is required for this. */
1083
1084 Lisp_Object
1085 find_symbol_value (symbol)
1086 Lisp_Object symbol;
1087 {
1088 register Lisp_Object valcontents;
1089 register Lisp_Object val;
1090
1091 CHECK_SYMBOL (symbol);
1092 valcontents = SYMBOL_VALUE (symbol);
1093
1094 if (BUFFER_LOCAL_VALUEP (valcontents)
1095 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
1096 valcontents = swap_in_symval_forwarding (symbol, valcontents);
1097
1098 if (MISCP (valcontents))
1099 {
1100 switch (XMISCTYPE (valcontents))
1101 {
1102 case Lisp_Misc_Intfwd:
1103 XSETINT (val, *XINTFWD (valcontents)->intvar);
1104 return val;
1105
1106 case Lisp_Misc_Boolfwd:
1107 return (*XBOOLFWD (valcontents)->boolvar ? Qt : Qnil);
1108
1109 case Lisp_Misc_Objfwd:
1110 return *XOBJFWD (valcontents)->objvar;
1111
1112 case Lisp_Misc_Buffer_Objfwd:
1113 return PER_BUFFER_VALUE (current_buffer,
1114 XBUFFER_OBJFWD (valcontents)->offset);
1115
1116 case Lisp_Misc_Kboard_Objfwd:
1117 return *(Lisp_Object *)(XKBOARD_OBJFWD (valcontents)->offset
1118 + (char *)FRAME_KBOARD (SELECTED_FRAME ()));
1119 }
1120 }
1121
1122 return valcontents;
1123 }
1124
1125 DEFUN ("symbol-value", Fsymbol_value, Ssymbol_value, 1, 1, 0,
1126 doc: /* Return SYMBOL's value. Error if that is void. */)
1127 (symbol)
1128 Lisp_Object symbol;
1129 {
1130 Lisp_Object val;
1131
1132 val = find_symbol_value (symbol);
1133 if (EQ (val, Qunbound))
1134 return Fsignal (Qvoid_variable, Fcons (symbol, Qnil));
1135 else
1136 return val;
1137 }
1138
1139 DEFUN ("set", Fset, Sset, 2, 2, 0,
1140 doc: /* Set SYMBOL's value to NEWVAL, and return NEWVAL. */)
1141 (symbol, newval)
1142 register Lisp_Object symbol, newval;
1143 {
1144 return set_internal (symbol, newval, current_buffer, 0);
1145 }
1146
1147 /* Return 1 if SYMBOL currently has a let-binding
1148 which was made in the buffer that is now current. */
1149
1150 static int
1151 let_shadows_buffer_binding_p (symbol)
1152 Lisp_Object symbol;
1153 {
1154 volatile struct specbinding *p;
1155
1156 for (p = specpdl_ptr - 1; p >= specpdl; p--)
1157 if (p->func == NULL
1158 && CONSP (p->symbol))
1159 {
1160 Lisp_Object let_bound_symbol = XCAR (p->symbol);
1161 if ((EQ (symbol, let_bound_symbol)
1162 || (XSYMBOL (let_bound_symbol)->indirect_variable
1163 && EQ (symbol, indirect_variable (let_bound_symbol))))
1164 && XBUFFER (XCDR (XCDR (p->symbol))) == current_buffer)
1165 break;
1166 }
1167
1168 return p >= specpdl;
1169 }
1170
1171 /* Store the value NEWVAL into SYMBOL.
1172 If buffer-locality is an issue, BUF specifies which buffer to use.
1173 (0 stands for the current buffer.)
1174
1175 If BINDFLAG is zero, then if this symbol is supposed to become
1176 local in every buffer where it is set, then we make it local.
1177 If BINDFLAG is nonzero, we don't do that. */
1178
1179 Lisp_Object
1180 set_internal (symbol, newval, buf, bindflag)
1181 register Lisp_Object symbol, newval;
1182 struct buffer *buf;
1183 int bindflag;
1184 {
1185 int voide = EQ (newval, Qunbound);
1186
1187 register Lisp_Object valcontents, innercontents, tem1, current_alist_element;
1188
1189 if (buf == 0)
1190 buf = current_buffer;
1191
1192 /* If restoring in a dead buffer, do nothing. */
1193 if (NILP (buf->name))
1194 return newval;
1195
1196 CHECK_SYMBOL (symbol);
1197 if (SYMBOL_CONSTANT_P (symbol)
1198 && (NILP (Fkeywordp (symbol))
1199 || !EQ (newval, SYMBOL_VALUE (symbol))))
1200 return Fsignal (Qsetting_constant, Fcons (symbol, Qnil));
1201
1202 innercontents = valcontents = SYMBOL_VALUE (symbol);
1203
1204 if (BUFFER_OBJFWDP (valcontents))
1205 {
1206 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1207 int idx = PER_BUFFER_IDX (offset);
1208 if (idx > 0
1209 && !bindflag
1210 && !let_shadows_buffer_binding_p (symbol))
1211 SET_PER_BUFFER_VALUE_P (buf, idx, 1);
1212 }
1213 else if (BUFFER_LOCAL_VALUEP (valcontents)
1214 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
1215 {
1216 /* valcontents is a struct Lisp_Buffer_Local_Value. */
1217 if (XSYMBOL (symbol)->indirect_variable)
1218 symbol = indirect_variable (symbol);
1219
1220 /* What binding is loaded right now? */
1221 current_alist_element
1222 = XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
1223
1224 /* If the current buffer is not the buffer whose binding is
1225 loaded, or if there may be frame-local bindings and the frame
1226 isn't the right one, or if it's a Lisp_Buffer_Local_Value and
1227 the default binding is loaded, the loaded binding may be the
1228 wrong one. */
1229 if (!BUFFERP (XBUFFER_LOCAL_VALUE (valcontents)->buffer)
1230 || buf != XBUFFER (XBUFFER_LOCAL_VALUE (valcontents)->buffer)
1231 || (XBUFFER_LOCAL_VALUE (valcontents)->check_frame
1232 && !EQ (selected_frame, XBUFFER_LOCAL_VALUE (valcontents)->frame))
1233 || (BUFFER_LOCAL_VALUEP (valcontents)
1234 && EQ (XCAR (current_alist_element),
1235 current_alist_element)))
1236 {
1237 /* The currently loaded binding is not necessarily valid.
1238 We need to unload it, and choose a new binding. */
1239
1240 /* Write out `realvalue' to the old loaded binding. */
1241 Fsetcdr (current_alist_element,
1242 do_symval_forwarding (XBUFFER_LOCAL_VALUE (valcontents)->realvalue));
1243
1244 /* Find the new binding. */
1245 tem1 = Fassq (symbol, buf->local_var_alist);
1246 XBUFFER_LOCAL_VALUE (valcontents)->found_for_buffer = 1;
1247 XBUFFER_LOCAL_VALUE (valcontents)->found_for_frame = 0;
1248
1249 if (NILP (tem1))
1250 {
1251 /* This buffer still sees the default value. */
1252
1253 /* If the variable is a Lisp_Some_Buffer_Local_Value,
1254 or if this is `let' rather than `set',
1255 make CURRENT-ALIST-ELEMENT point to itself,
1256 indicating that we're seeing the default value.
1257 Likewise if the variable has been let-bound
1258 in the current buffer. */
1259 if (bindflag || SOME_BUFFER_LOCAL_VALUEP (valcontents)
1260 || let_shadows_buffer_binding_p (symbol))
1261 {
1262 XBUFFER_LOCAL_VALUE (valcontents)->found_for_buffer = 0;
1263
1264 if (XBUFFER_LOCAL_VALUE (valcontents)->check_frame)
1265 tem1 = Fassq (symbol,
1266 XFRAME (selected_frame)->param_alist);
1267
1268 if (! NILP (tem1))
1269 XBUFFER_LOCAL_VALUE (valcontents)->found_for_frame = 1;
1270 else
1271 tem1 = XBUFFER_LOCAL_VALUE (valcontents)->cdr;
1272 }
1273 /* If it's a Lisp_Buffer_Local_Value, being set not bound,
1274 and we're not within a let that was made for this buffer,
1275 create a new buffer-local binding for the variable.
1276 That means, give this buffer a new assoc for a local value
1277 and load that binding. */
1278 else
1279 {
1280 tem1 = Fcons (symbol, XCDR (current_alist_element));
1281 buf->local_var_alist
1282 = Fcons (tem1, buf->local_var_alist);
1283 }
1284 }
1285
1286 /* Record which binding is now loaded. */
1287 XSETCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr,
1288 tem1);
1289
1290 /* Set `buffer' and `frame' slots for thebinding now loaded. */
1291 XSETBUFFER (XBUFFER_LOCAL_VALUE (valcontents)->buffer, buf);
1292 XBUFFER_LOCAL_VALUE (valcontents)->frame = selected_frame;
1293 }
1294 innercontents = XBUFFER_LOCAL_VALUE (valcontents)->realvalue;
1295 }
1296
1297 /* If storing void (making the symbol void), forward only through
1298 buffer-local indicator, not through Lisp_Objfwd, etc. */
1299 if (voide)
1300 store_symval_forwarding (symbol, Qnil, newval, buf);
1301 else
1302 store_symval_forwarding (symbol, innercontents, newval, buf);
1303
1304 /* If we just set a variable whose current binding is frame-local,
1305 store the new value in the frame parameter too. */
1306
1307 if (BUFFER_LOCAL_VALUEP (valcontents)
1308 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
1309 {
1310 /* What binding is loaded right now? */
1311 current_alist_element
1312 = XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
1313
1314 /* If the current buffer is not the buffer whose binding is
1315 loaded, or if there may be frame-local bindings and the frame
1316 isn't the right one, or if it's a Lisp_Buffer_Local_Value and
1317 the default binding is loaded, the loaded binding may be the
1318 wrong one. */
1319 if (XBUFFER_LOCAL_VALUE (valcontents)->found_for_frame)
1320 XSETCDR (current_alist_element, newval);
1321 }
1322
1323 return newval;
1324 }
1325 \f
1326 /* Access or set a buffer-local symbol's default value. */
1327
1328 /* Return the default value of SYMBOL, but don't check for voidness.
1329 Return Qunbound if it is void. */
1330
1331 Lisp_Object
1332 default_value (symbol)
1333 Lisp_Object symbol;
1334 {
1335 register Lisp_Object valcontents;
1336
1337 CHECK_SYMBOL (symbol);
1338 valcontents = SYMBOL_VALUE (symbol);
1339
1340 /* For a built-in buffer-local variable, get the default value
1341 rather than letting do_symval_forwarding get the current value. */
1342 if (BUFFER_OBJFWDP (valcontents))
1343 {
1344 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1345 if (PER_BUFFER_IDX (offset) != 0)
1346 return PER_BUFFER_DEFAULT (offset);
1347 }
1348
1349 /* Handle user-created local variables. */
1350 if (BUFFER_LOCAL_VALUEP (valcontents)
1351 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
1352 {
1353 /* If var is set up for a buffer that lacks a local value for it,
1354 the current value is nominally the default value.
1355 But the `realvalue' slot may be more up to date, since
1356 ordinary setq stores just that slot. So use that. */
1357 Lisp_Object current_alist_element, alist_element_car;
1358 current_alist_element
1359 = XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
1360 alist_element_car = XCAR (current_alist_element);
1361 if (EQ (alist_element_car, current_alist_element))
1362 return do_symval_forwarding (XBUFFER_LOCAL_VALUE (valcontents)->realvalue);
1363 else
1364 return XCDR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
1365 }
1366 /* For other variables, get the current value. */
1367 return do_symval_forwarding (valcontents);
1368 }
1369
1370 DEFUN ("default-boundp", Fdefault_boundp, Sdefault_boundp, 1, 1, 0,
1371 doc: /* Return t if SYMBOL has a non-void default value.
1372 This is the value that is seen in buffers that do not have their own values
1373 for this variable. */)
1374 (symbol)
1375 Lisp_Object symbol;
1376 {
1377 register Lisp_Object value;
1378
1379 value = default_value (symbol);
1380 return (EQ (value, Qunbound) ? Qnil : Qt);
1381 }
1382
1383 DEFUN ("default-value", Fdefault_value, Sdefault_value, 1, 1, 0,
1384 doc: /* Return SYMBOL's default value.
1385 This is the value that is seen in buffers that do not have their own values
1386 for this variable. The default value is meaningful for variables with
1387 local bindings in certain buffers. */)
1388 (symbol)
1389 Lisp_Object symbol;
1390 {
1391 register Lisp_Object value;
1392
1393 value = default_value (symbol);
1394 if (EQ (value, Qunbound))
1395 return Fsignal (Qvoid_variable, Fcons (symbol, Qnil));
1396 return value;
1397 }
1398
1399 DEFUN ("set-default", Fset_default, Sset_default, 2, 2, 0,
1400 doc: /* Set SYMBOL's default value to VALUE. SYMBOL and VALUE are evaluated.
1401 The default value is seen in buffers that do not have their own values
1402 for this variable. */)
1403 (symbol, value)
1404 Lisp_Object symbol, value;
1405 {
1406 register Lisp_Object valcontents, current_alist_element, alist_element_buffer;
1407
1408 CHECK_SYMBOL (symbol);
1409 valcontents = SYMBOL_VALUE (symbol);
1410
1411 /* Handle variables like case-fold-search that have special slots
1412 in the buffer. Make them work apparently like Lisp_Buffer_Local_Value
1413 variables. */
1414 if (BUFFER_OBJFWDP (valcontents))
1415 {
1416 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1417 int idx = PER_BUFFER_IDX (offset);
1418
1419 PER_BUFFER_DEFAULT (offset) = value;
1420
1421 /* If this variable is not always local in all buffers,
1422 set it in the buffers that don't nominally have a local value. */
1423 if (idx > 0)
1424 {
1425 struct buffer *b;
1426
1427 for (b = all_buffers; b; b = b->next)
1428 if (!PER_BUFFER_VALUE_P (b, idx))
1429 PER_BUFFER_VALUE (b, offset) = value;
1430 }
1431 return value;
1432 }
1433
1434 if (!BUFFER_LOCAL_VALUEP (valcontents)
1435 && !SOME_BUFFER_LOCAL_VALUEP (valcontents))
1436 return Fset (symbol, value);
1437
1438 /* Store new value into the DEFAULT-VALUE slot. */
1439 XSETCDR (XBUFFER_LOCAL_VALUE (valcontents)->cdr, value);
1440
1441 /* If the default binding is now loaded, set the REALVALUE slot too. */
1442 current_alist_element
1443 = XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
1444 alist_element_buffer = Fcar (current_alist_element);
1445 if (EQ (alist_element_buffer, current_alist_element))
1446 store_symval_forwarding (symbol,
1447 XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1448 value, NULL);
1449
1450 return value;
1451 }
1452
1453 DEFUN ("setq-default", Fsetq_default, Ssetq_default, 0, UNEVALLED, 0,
1454 doc: /* Set the default value of variable VAR to VALUE.
1455 VAR, the variable name, is literal (not evaluated);
1456 VALUE is an expression: it is evaluated and its value returned.
1457 The default value of a variable is seen in buffers
1458 that do not have their own values for the variable.
1459
1460 More generally, you can use multiple variables and values, as in
1461 (setq-default VAR VALUE VAR VALUE...)
1462 This sets each VAR's default value to the corresponding VALUE.
1463 The VALUE for the Nth VAR can refer to the new default values
1464 of previous VARs.
1465 usage: (setq-default [VAR VALUE...]) */)
1466 (args)
1467 Lisp_Object args;
1468 {
1469 register Lisp_Object args_left;
1470 register Lisp_Object val, symbol;
1471 struct gcpro gcpro1;
1472
1473 if (NILP (args))
1474 return Qnil;
1475
1476 args_left = args;
1477 GCPRO1 (args);
1478
1479 do
1480 {
1481 val = Feval (Fcar (Fcdr (args_left)));
1482 symbol = XCAR (args_left);
1483 Fset_default (symbol, val);
1484 args_left = Fcdr (XCDR (args_left));
1485 }
1486 while (!NILP (args_left));
1487
1488 UNGCPRO;
1489 return val;
1490 }
1491 \f
1492 /* Lisp functions for creating and removing buffer-local variables. */
1493
1494 DEFUN ("make-variable-buffer-local", Fmake_variable_buffer_local, Smake_variable_buffer_local,
1495 1, 1, "vMake Variable Buffer Local: ",
1496 doc: /* Make VARIABLE become buffer-local whenever it is set.
1497 At any time, the value for the current buffer is in effect,
1498 unless the variable has never been set in this buffer,
1499 in which case the default value is in effect.
1500 Note that binding the variable with `let', or setting it while
1501 a `let'-style binding made in this buffer is in effect,
1502 does not make the variable buffer-local. Return VARIABLE.
1503
1504 In most cases it is better to use `make-local-variable',
1505 which makes a variable local in just one buffer.
1506
1507 The function `default-value' gets the default value and `set-default' sets it. */)
1508 (variable)
1509 register Lisp_Object variable;
1510 {
1511 register Lisp_Object tem, valcontents, newval;
1512
1513 CHECK_SYMBOL (variable);
1514 variable = indirect_variable (variable);
1515
1516 valcontents = SYMBOL_VALUE (variable);
1517 if (EQ (variable, Qnil) || EQ (variable, Qt) || KBOARD_OBJFWDP (valcontents))
1518 error ("Symbol %s may not be buffer-local", SDATA (SYMBOL_NAME (variable)));
1519
1520 if (BUFFER_LOCAL_VALUEP (valcontents) || BUFFER_OBJFWDP (valcontents))
1521 return variable;
1522 if (SOME_BUFFER_LOCAL_VALUEP (valcontents))
1523 {
1524 XMISCTYPE (SYMBOL_VALUE (variable)) = Lisp_Misc_Buffer_Local_Value;
1525 return variable;
1526 }
1527 if (EQ (valcontents, Qunbound))
1528 SET_SYMBOL_VALUE (variable, Qnil);
1529 tem = Fcons (Qnil, Fsymbol_value (variable));
1530 XSETCAR (tem, tem);
1531 newval = allocate_misc ();
1532 XMISCTYPE (newval) = Lisp_Misc_Buffer_Local_Value;
1533 XBUFFER_LOCAL_VALUE (newval)->realvalue = SYMBOL_VALUE (variable);
1534 XBUFFER_LOCAL_VALUE (newval)->buffer = Fcurrent_buffer ();
1535 XBUFFER_LOCAL_VALUE (newval)->frame = Qnil;
1536 XBUFFER_LOCAL_VALUE (newval)->found_for_buffer = 0;
1537 XBUFFER_LOCAL_VALUE (newval)->found_for_frame = 0;
1538 XBUFFER_LOCAL_VALUE (newval)->check_frame = 0;
1539 XBUFFER_LOCAL_VALUE (newval)->cdr = tem;
1540 SET_SYMBOL_VALUE (variable, newval);
1541 return variable;
1542 }
1543
1544 DEFUN ("make-local-variable", Fmake_local_variable, Smake_local_variable,
1545 1, 1, "vMake Local Variable: ",
1546 doc: /* Make VARIABLE have a separate value in the current buffer.
1547 Other buffers will continue to share a common default value.
1548 \(The buffer-local value of VARIABLE starts out as the same value
1549 VARIABLE previously had. If VARIABLE was void, it remains void.\)
1550 Return VARIABLE.
1551
1552 If the variable is already arranged to become local when set,
1553 this function causes a local value to exist for this buffer,
1554 just as setting the variable would do.
1555
1556 This function returns VARIABLE, and therefore
1557 (set (make-local-variable 'VARIABLE) VALUE-EXP)
1558 works.
1559
1560 See also `make-variable-buffer-local'.
1561
1562 Do not use `make-local-variable' to make a hook variable buffer-local.
1563 Instead, use `add-hook' and specify t for the LOCAL argument. */)
1564 (variable)
1565 register Lisp_Object variable;
1566 {
1567 register Lisp_Object tem, valcontents;
1568
1569 CHECK_SYMBOL (variable);
1570 variable = indirect_variable (variable);
1571
1572 valcontents = SYMBOL_VALUE (variable);
1573 if (EQ (variable, Qnil) || EQ (variable, Qt) || KBOARD_OBJFWDP (valcontents))
1574 error ("Symbol %s may not be buffer-local", SDATA (SYMBOL_NAME (variable)));
1575
1576 if (BUFFER_LOCAL_VALUEP (valcontents) || BUFFER_OBJFWDP (valcontents))
1577 {
1578 tem = Fboundp (variable);
1579
1580 /* Make sure the symbol has a local value in this particular buffer,
1581 by setting it to the same value it already has. */
1582 Fset (variable, (EQ (tem, Qt) ? Fsymbol_value (variable) : Qunbound));
1583 return variable;
1584 }
1585 /* Make sure symbol is set up to hold per-buffer values. */
1586 if (!SOME_BUFFER_LOCAL_VALUEP (valcontents))
1587 {
1588 Lisp_Object newval;
1589 tem = Fcons (Qnil, do_symval_forwarding (valcontents));
1590 XSETCAR (tem, tem);
1591 newval = allocate_misc ();
1592 XMISCTYPE (newval) = Lisp_Misc_Some_Buffer_Local_Value;
1593 XBUFFER_LOCAL_VALUE (newval)->realvalue = SYMBOL_VALUE (variable);
1594 XBUFFER_LOCAL_VALUE (newval)->buffer = Qnil;
1595 XBUFFER_LOCAL_VALUE (newval)->frame = Qnil;
1596 XBUFFER_LOCAL_VALUE (newval)->found_for_buffer = 0;
1597 XBUFFER_LOCAL_VALUE (newval)->found_for_frame = 0;
1598 XBUFFER_LOCAL_VALUE (newval)->check_frame = 0;
1599 XBUFFER_LOCAL_VALUE (newval)->cdr = tem;
1600 SET_SYMBOL_VALUE (variable, newval);;
1601 }
1602 /* Make sure this buffer has its own value of symbol. */
1603 tem = Fassq (variable, current_buffer->local_var_alist);
1604 if (NILP (tem))
1605 {
1606 /* Swap out any local binding for some other buffer, and make
1607 sure the current value is permanently recorded, if it's the
1608 default value. */
1609 find_symbol_value (variable);
1610
1611 current_buffer->local_var_alist
1612 = Fcons (Fcons (variable, XCDR (XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (variable))->cdr)),
1613 current_buffer->local_var_alist);
1614
1615 /* Make sure symbol does not think it is set up for this buffer;
1616 force it to look once again for this buffer's value. */
1617 {
1618 Lisp_Object *pvalbuf;
1619
1620 valcontents = SYMBOL_VALUE (variable);
1621
1622 pvalbuf = &XBUFFER_LOCAL_VALUE (valcontents)->buffer;
1623 if (current_buffer == XBUFFER (*pvalbuf))
1624 *pvalbuf = Qnil;
1625 XBUFFER_LOCAL_VALUE (valcontents)->found_for_buffer = 0;
1626 }
1627 }
1628
1629 /* If the symbol forwards into a C variable, then load the binding
1630 for this buffer now. If C code modifies the variable before we
1631 load the binding in, then that new value will clobber the default
1632 binding the next time we unload it. */
1633 valcontents = XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (variable))->realvalue;
1634 if (INTFWDP (valcontents) || BOOLFWDP (valcontents) || OBJFWDP (valcontents))
1635 swap_in_symval_forwarding (variable, SYMBOL_VALUE (variable));
1636
1637 return variable;
1638 }
1639
1640 DEFUN ("kill-local-variable", Fkill_local_variable, Skill_local_variable,
1641 1, 1, "vKill Local Variable: ",
1642 doc: /* Make VARIABLE no longer have a separate value in the current buffer.
1643 From now on the default value will apply in this buffer. Return VARIABLE. */)
1644 (variable)
1645 register Lisp_Object variable;
1646 {
1647 register Lisp_Object tem, valcontents;
1648
1649 CHECK_SYMBOL (variable);
1650 variable = indirect_variable (variable);
1651
1652 valcontents = SYMBOL_VALUE (variable);
1653
1654 if (BUFFER_OBJFWDP (valcontents))
1655 {
1656 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1657 int idx = PER_BUFFER_IDX (offset);
1658
1659 if (idx > 0)
1660 {
1661 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 0);
1662 PER_BUFFER_VALUE (current_buffer, offset)
1663 = PER_BUFFER_DEFAULT (offset);
1664 }
1665 return variable;
1666 }
1667
1668 if (!BUFFER_LOCAL_VALUEP (valcontents)
1669 && !SOME_BUFFER_LOCAL_VALUEP (valcontents))
1670 return variable;
1671
1672 /* Get rid of this buffer's alist element, if any. */
1673
1674 tem = Fassq (variable, current_buffer->local_var_alist);
1675 if (!NILP (tem))
1676 current_buffer->local_var_alist
1677 = Fdelq (tem, current_buffer->local_var_alist);
1678
1679 /* If the symbol is set up with the current buffer's binding
1680 loaded, recompute its value. We have to do it now, or else
1681 forwarded objects won't work right. */
1682 {
1683 Lisp_Object *pvalbuf, buf;
1684 valcontents = SYMBOL_VALUE (variable);
1685 pvalbuf = &XBUFFER_LOCAL_VALUE (valcontents)->buffer;
1686 XSETBUFFER (buf, current_buffer);
1687 if (EQ (buf, *pvalbuf))
1688 {
1689 *pvalbuf = Qnil;
1690 XBUFFER_LOCAL_VALUE (valcontents)->found_for_buffer = 0;
1691 find_symbol_value (variable);
1692 }
1693 }
1694
1695 return variable;
1696 }
1697
1698 /* Lisp functions for creating and removing buffer-local variables. */
1699
1700 DEFUN ("make-variable-frame-local", Fmake_variable_frame_local, Smake_variable_frame_local,
1701 1, 1, "vMake Variable Frame Local: ",
1702 doc: /* Enable VARIABLE to have frame-local bindings.
1703 This does not create any frame-local bindings for VARIABLE,
1704 it just makes them possible.
1705
1706 A frame-local binding is actually a frame parameter value.
1707 If a frame F has a value for the frame parameter named VARIABLE,
1708 that also acts as a frame-local binding for VARIABLE in F--
1709 provided this function has been called to enable VARIABLE
1710 to have frame-local bindings at all.
1711
1712 The only way to create a frame-local binding for VARIABLE in a frame
1713 is to set the VARIABLE frame parameter of that frame. See
1714 `modify-frame-parameters' for how to set frame parameters.
1715
1716 Buffer-local bindings take precedence over frame-local bindings. */)
1717 (variable)
1718 register Lisp_Object variable;
1719 {
1720 register Lisp_Object tem, valcontents, newval;
1721
1722 CHECK_SYMBOL (variable);
1723 variable = indirect_variable (variable);
1724
1725 valcontents = SYMBOL_VALUE (variable);
1726 if (EQ (variable, Qnil) || EQ (variable, Qt) || KBOARD_OBJFWDP (valcontents)
1727 || BUFFER_OBJFWDP (valcontents))
1728 error ("Symbol %s may not be frame-local", SDATA (SYMBOL_NAME (variable)));
1729
1730 if (BUFFER_LOCAL_VALUEP (valcontents)
1731 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
1732 {
1733 XBUFFER_LOCAL_VALUE (valcontents)->check_frame = 1;
1734 return variable;
1735 }
1736
1737 if (EQ (valcontents, Qunbound))
1738 SET_SYMBOL_VALUE (variable, Qnil);
1739 tem = Fcons (Qnil, Fsymbol_value (variable));
1740 XSETCAR (tem, tem);
1741 newval = allocate_misc ();
1742 XMISCTYPE (newval) = Lisp_Misc_Some_Buffer_Local_Value;
1743 XBUFFER_LOCAL_VALUE (newval)->realvalue = SYMBOL_VALUE (variable);
1744 XBUFFER_LOCAL_VALUE (newval)->buffer = Qnil;
1745 XBUFFER_LOCAL_VALUE (newval)->frame = Qnil;
1746 XBUFFER_LOCAL_VALUE (newval)->found_for_buffer = 0;
1747 XBUFFER_LOCAL_VALUE (newval)->found_for_frame = 0;
1748 XBUFFER_LOCAL_VALUE (newval)->check_frame = 1;
1749 XBUFFER_LOCAL_VALUE (newval)->cdr = tem;
1750 SET_SYMBOL_VALUE (variable, newval);
1751 return variable;
1752 }
1753
1754 DEFUN ("local-variable-p", Flocal_variable_p, Slocal_variable_p,
1755 1, 2, 0,
1756 doc: /* Non-nil if VARIABLE has a local binding in buffer BUFFER.
1757 BUFFER defaults to the current buffer. */)
1758 (variable, buffer)
1759 register Lisp_Object variable, buffer;
1760 {
1761 Lisp_Object valcontents;
1762 register struct buffer *buf;
1763
1764 if (NILP (buffer))
1765 buf = current_buffer;
1766 else
1767 {
1768 CHECK_BUFFER (buffer);
1769 buf = XBUFFER (buffer);
1770 }
1771
1772 CHECK_SYMBOL (variable);
1773 variable = indirect_variable (variable);
1774
1775 valcontents = SYMBOL_VALUE (variable);
1776 if (BUFFER_LOCAL_VALUEP (valcontents)
1777 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
1778 {
1779 Lisp_Object tail, elt;
1780
1781 for (tail = buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
1782 {
1783 elt = XCAR (tail);
1784 if (EQ (variable, XCAR (elt)))
1785 return Qt;
1786 }
1787 }
1788 if (BUFFER_OBJFWDP (valcontents))
1789 {
1790 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1791 int idx = PER_BUFFER_IDX (offset);
1792 if (idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
1793 return Qt;
1794 }
1795 return Qnil;
1796 }
1797
1798 DEFUN ("local-variable-if-set-p", Flocal_variable_if_set_p, Slocal_variable_if_set_p,
1799 1, 2, 0,
1800 doc: /* Non-nil if VARIABLE will be local in buffer BUFFER when set there.
1801 More precisely, this means that setting the variable \(with `set' or`setq'),
1802 while it does not have a `let'-style binding that was made in BUFFER,
1803 will produce a buffer local binding. See Info node
1804 `(elisp)Creating Buffer-Local'.
1805 BUFFER defaults to the current buffer. */)
1806 (variable, buffer)
1807 register Lisp_Object variable, buffer;
1808 {
1809 Lisp_Object valcontents;
1810 register struct buffer *buf;
1811
1812 if (NILP (buffer))
1813 buf = current_buffer;
1814 else
1815 {
1816 CHECK_BUFFER (buffer);
1817 buf = XBUFFER (buffer);
1818 }
1819
1820 CHECK_SYMBOL (variable);
1821 variable = indirect_variable (variable);
1822
1823 valcontents = SYMBOL_VALUE (variable);
1824
1825 /* This means that make-variable-buffer-local was done. */
1826 if (BUFFER_LOCAL_VALUEP (valcontents))
1827 return Qt;
1828 /* All these slots become local if they are set. */
1829 if (BUFFER_OBJFWDP (valcontents))
1830 return Qt;
1831 if (SOME_BUFFER_LOCAL_VALUEP (valcontents))
1832 {
1833 Lisp_Object tail, elt;
1834 for (tail = buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
1835 {
1836 elt = XCAR (tail);
1837 if (EQ (variable, XCAR (elt)))
1838 return Qt;
1839 }
1840 }
1841 return Qnil;
1842 }
1843
1844 DEFUN ("variable-binding-locus", Fvariable_binding_locus, Svariable_binding_locus,
1845 1, 1, 0,
1846 doc: /* Return a value indicating where VARIABLE's current binding comes from.
1847 If the current binding is buffer-local, the value is the current buffer.
1848 If the current binding is frame-local, the value is the selected frame.
1849 If the current binding is global (the default), the value is nil. */)
1850 (variable)
1851 register Lisp_Object variable;
1852 {
1853 Lisp_Object valcontents;
1854
1855 CHECK_SYMBOL (variable);
1856 variable = indirect_variable (variable);
1857
1858 /* Make sure the current binding is actually swapped in. */
1859 find_symbol_value (variable);
1860
1861 valcontents = XSYMBOL (variable)->value;
1862
1863 if (BUFFER_LOCAL_VALUEP (valcontents)
1864 || SOME_BUFFER_LOCAL_VALUEP (valcontents)
1865 || BUFFER_OBJFWDP (valcontents))
1866 {
1867 /* For a local variable, record both the symbol and which
1868 buffer's or frame's value we are saving. */
1869 if (!NILP (Flocal_variable_p (variable, Qnil)))
1870 return Fcurrent_buffer ();
1871 else if (!BUFFER_OBJFWDP (valcontents)
1872 && XBUFFER_LOCAL_VALUE (valcontents)->found_for_frame)
1873 return XBUFFER_LOCAL_VALUE (valcontents)->frame;
1874 }
1875
1876 return Qnil;
1877 }
1878
1879 /* This code is disabled now that we use the selected frame to return
1880 keyboard-local-values. */
1881 #if 0
1882 extern struct terminal *get_terminal P_ ((Lisp_Object display, int));
1883
1884 DEFUN ("terminal-local-value", Fterminal_local_value, Sterminal_local_value, 2, 2, 0,
1885 doc: /* Return the terminal-local value of SYMBOL on TERMINAL.
1886 If SYMBOL is not a terminal-local variable, then return its normal
1887 value, like `symbol-value'.
1888
1889 TERMINAL may be a terminal id, a frame, or nil (meaning the
1890 selected frame's terminal device). */)
1891 (symbol, terminal)
1892 Lisp_Object symbol;
1893 Lisp_Object terminal;
1894 {
1895 Lisp_Object result;
1896 struct terminal *t = get_terminal (terminal, 1);
1897 push_kboard (t->kboard);
1898 result = Fsymbol_value (symbol);
1899 pop_kboard ();
1900 return result;
1901 }
1902
1903 DEFUN ("set-terminal-local-value", Fset_terminal_local_value, Sset_terminal_local_value, 3, 3, 0,
1904 doc: /* Set the terminal-local binding of SYMBOL on TERMINAL to VALUE.
1905 If VARIABLE is not a terminal-local variable, then set its normal
1906 binding, like `set'.
1907
1908 TERMINAL may be a terminal id, a frame, or nil (meaning the
1909 selected frame's terminal device). */)
1910 (symbol, terminal, value)
1911 Lisp_Object symbol;
1912 Lisp_Object terminal;
1913 Lisp_Object value;
1914 {
1915 Lisp_Object result;
1916 struct terminal *t = get_terminal (terminal, 1);
1917 push_kboard (d->kboard);
1918 result = Fset (symbol, value);
1919 pop_kboard ();
1920 return result;
1921 }
1922 #endif
1923 \f
1924 /* Find the function at the end of a chain of symbol function indirections. */
1925
1926 /* If OBJECT is a symbol, find the end of its function chain and
1927 return the value found there. If OBJECT is not a symbol, just
1928 return it. If there is a cycle in the function chain, signal a
1929 cyclic-function-indirection error.
1930
1931 This is like Findirect_function, except that it doesn't signal an
1932 error if the chain ends up unbound. */
1933 Lisp_Object
1934 indirect_function (object)
1935 register Lisp_Object object;
1936 {
1937 Lisp_Object tortoise, hare;
1938
1939 hare = tortoise = object;
1940
1941 for (;;)
1942 {
1943 if (!SYMBOLP (hare) || EQ (hare, Qunbound))
1944 break;
1945 hare = XSYMBOL (hare)->function;
1946 if (!SYMBOLP (hare) || EQ (hare, Qunbound))
1947 break;
1948 hare = XSYMBOL (hare)->function;
1949
1950 tortoise = XSYMBOL (tortoise)->function;
1951
1952 if (EQ (hare, tortoise))
1953 Fsignal (Qcyclic_function_indirection, Fcons (object, Qnil));
1954 }
1955
1956 return hare;
1957 }
1958
1959 DEFUN ("indirect-function", Findirect_function, Sindirect_function, 1, 2, 0,
1960 doc: /* Return the function at the end of OBJECT's function chain.
1961 If OBJECT is not a symbol, just return it. Otherwise, follow all
1962 function indirections to find the final function binding and return it.
1963 If the final symbol in the chain is unbound, signal a void-function error.
1964 Optional arg NOERROR non-nil means to return nil instead of signalling.
1965 Signal a cyclic-function-indirection error if there is a loop in the
1966 function chain of symbols. */)
1967 (object, noerror)
1968 register Lisp_Object object;
1969 Lisp_Object noerror;
1970 {
1971 Lisp_Object result;
1972
1973 /* Optimize for no indirection. */
1974 result = object;
1975 if (SYMBOLP (result) && !EQ (result, Qunbound)
1976 && (result = XSYMBOL (result)->function, SYMBOLP (result)))
1977 result = indirect_function (result);
1978 if (!EQ (result, Qunbound))
1979 return result;
1980
1981 if (NILP (noerror))
1982 Fsignal (Qvoid_function, Fcons (object, Qnil));
1983
1984 return Qnil;
1985 }
1986 \f
1987 /* Extract and set vector and string elements */
1988
1989 DEFUN ("aref", Faref, Saref, 2, 2, 0,
1990 doc: /* Return the element of ARRAY at index IDX.
1991 ARRAY may be a vector, a string, a char-table, a bool-vector,
1992 or a byte-code object. IDX starts at 0. */)
1993 (array, idx)
1994 register Lisp_Object array;
1995 Lisp_Object idx;
1996 {
1997 register int idxval;
1998
1999 CHECK_NUMBER (idx);
2000 idxval = XINT (idx);
2001 if (STRINGP (array))
2002 {
2003 int c, idxval_byte;
2004
2005 if (idxval < 0 || idxval >= SCHARS (array))
2006 args_out_of_range (array, idx);
2007 if (! STRING_MULTIBYTE (array))
2008 return make_number ((unsigned char) SREF (array, idxval));
2009 idxval_byte = string_char_to_byte (array, idxval);
2010
2011 c = STRING_CHAR (SDATA (array) + idxval_byte,
2012 SBYTES (array) - idxval_byte);
2013 return make_number (c);
2014 }
2015 else if (BOOL_VECTOR_P (array))
2016 {
2017 int val;
2018
2019 if (idxval < 0 || idxval >= XBOOL_VECTOR (array)->size)
2020 args_out_of_range (array, idx);
2021
2022 val = (unsigned char) XBOOL_VECTOR (array)->data[idxval / BOOL_VECTOR_BITS_PER_CHAR];
2023 return (val & (1 << (idxval % BOOL_VECTOR_BITS_PER_CHAR)) ? Qt : Qnil);
2024 }
2025 else if (CHAR_TABLE_P (array))
2026 {
2027 Lisp_Object val;
2028
2029 val = Qnil;
2030
2031 if (idxval < 0)
2032 args_out_of_range (array, idx);
2033 if (idxval < CHAR_TABLE_ORDINARY_SLOTS)
2034 {
2035 if (! SINGLE_BYTE_CHAR_P (idxval))
2036 args_out_of_range (array, idx);
2037 /* For ASCII and 8-bit European characters, the element is
2038 stored in the top table. */
2039 val = XCHAR_TABLE (array)->contents[idxval];
2040 if (NILP (val))
2041 {
2042 int default_slot
2043 = (idxval < 0x80 ? CHAR_TABLE_DEFAULT_SLOT_ASCII
2044 : idxval < 0xA0 ? CHAR_TABLE_DEFAULT_SLOT_8_BIT_CONTROL
2045 : CHAR_TABLE_DEFAULT_SLOT_8_BIT_GRAPHIC);
2046 val = XCHAR_TABLE (array)->contents[default_slot];
2047 }
2048 if (NILP (val))
2049 val = XCHAR_TABLE (array)->defalt;
2050 while (NILP (val)) /* Follow parents until we find some value. */
2051 {
2052 array = XCHAR_TABLE (array)->parent;
2053 if (NILP (array))
2054 return Qnil;
2055 val = XCHAR_TABLE (array)->contents[idxval];
2056 if (NILP (val))
2057 val = XCHAR_TABLE (array)->defalt;
2058 }
2059 return val;
2060 }
2061 else
2062 {
2063 int code[4], i;
2064 Lisp_Object sub_table;
2065 Lisp_Object current_default;
2066
2067 SPLIT_CHAR (idxval, code[0], code[1], code[2]);
2068 if (code[1] < 32) code[1] = -1;
2069 else if (code[2] < 32) code[2] = -1;
2070
2071 /* Here, the possible range of CODE[0] (== charset ID) is
2072 128..MAX_CHARSET. Since the top level char table contains
2073 data for multibyte characters after 256th element, we must
2074 increment CODE[0] by 128 to get a correct index. */
2075 code[0] += 128;
2076 code[3] = -1; /* anchor */
2077
2078 try_parent_char_table:
2079 current_default = XCHAR_TABLE (array)->defalt;
2080 sub_table = array;
2081 for (i = 0; code[i] >= 0; i++)
2082 {
2083 val = XCHAR_TABLE (sub_table)->contents[code[i]];
2084 if (SUB_CHAR_TABLE_P (val))
2085 {
2086 sub_table = val;
2087 if (! NILP (XCHAR_TABLE (sub_table)->defalt))
2088 current_default = XCHAR_TABLE (sub_table)->defalt;
2089 }
2090 else
2091 {
2092 if (NILP (val))
2093 val = current_default;
2094 if (NILP (val))
2095 {
2096 array = XCHAR_TABLE (array)->parent;
2097 if (!NILP (array))
2098 goto try_parent_char_table;
2099 }
2100 return val;
2101 }
2102 }
2103 /* Reaching here means IDXVAL is a generic character in
2104 which each character or a group has independent value.
2105 Essentially it's nonsense to get a value for such a
2106 generic character, but for backward compatibility, we try
2107 the default value and parent. */
2108 val = current_default;
2109 if (NILP (val))
2110 {
2111 array = XCHAR_TABLE (array)->parent;
2112 if (!NILP (array))
2113 goto try_parent_char_table;
2114 }
2115 return val;
2116 }
2117 }
2118 else
2119 {
2120 int size = 0;
2121 if (VECTORP (array))
2122 size = XVECTOR (array)->size;
2123 else if (COMPILEDP (array))
2124 size = XVECTOR (array)->size & PSEUDOVECTOR_SIZE_MASK;
2125 else
2126 wrong_type_argument (Qarrayp, array);
2127
2128 if (idxval < 0 || idxval >= size)
2129 args_out_of_range (array, idx);
2130 return XVECTOR (array)->contents[idxval];
2131 }
2132 }
2133
2134 DEFUN ("aset", Faset, Saset, 3, 3, 0,
2135 doc: /* Store into the element of ARRAY at index IDX the value NEWELT.
2136 Return NEWELT. ARRAY may be a vector, a string, a char-table or a
2137 bool-vector. IDX starts at 0. */)
2138 (array, idx, newelt)
2139 register Lisp_Object array;
2140 Lisp_Object idx, newelt;
2141 {
2142 register int idxval;
2143
2144 CHECK_NUMBER (idx);
2145 idxval = XINT (idx);
2146 CHECK_ARRAY (array, Qarrayp);
2147 CHECK_IMPURE (array);
2148
2149 if (VECTORP (array))
2150 {
2151 if (idxval < 0 || idxval >= XVECTOR (array)->size)
2152 args_out_of_range (array, idx);
2153 XVECTOR (array)->contents[idxval] = newelt;
2154 }
2155 else if (BOOL_VECTOR_P (array))
2156 {
2157 int val;
2158
2159 if (idxval < 0 || idxval >= XBOOL_VECTOR (array)->size)
2160 args_out_of_range (array, idx);
2161
2162 val = (unsigned char) XBOOL_VECTOR (array)->data[idxval / BOOL_VECTOR_BITS_PER_CHAR];
2163
2164 if (! NILP (newelt))
2165 val |= 1 << (idxval % BOOL_VECTOR_BITS_PER_CHAR);
2166 else
2167 val &= ~(1 << (idxval % BOOL_VECTOR_BITS_PER_CHAR));
2168 XBOOL_VECTOR (array)->data[idxval / BOOL_VECTOR_BITS_PER_CHAR] = val;
2169 }
2170 else if (CHAR_TABLE_P (array))
2171 {
2172 if (idxval < 0)
2173 args_out_of_range (array, idx);
2174 if (idxval < CHAR_TABLE_ORDINARY_SLOTS)
2175 {
2176 if (! SINGLE_BYTE_CHAR_P (idxval))
2177 args_out_of_range (array, idx);
2178 XCHAR_TABLE (array)->contents[idxval] = newelt;
2179 }
2180 else
2181 {
2182 int code[4], i;
2183 Lisp_Object val;
2184
2185 SPLIT_CHAR (idxval, code[0], code[1], code[2]);
2186 if (code[1] < 32) code[1] = -1;
2187 else if (code[2] < 32) code[2] = -1;
2188
2189 /* See the comment of the corresponding part in Faref. */
2190 code[0] += 128;
2191 code[3] = -1; /* anchor */
2192 for (i = 0; code[i + 1] >= 0; i++)
2193 {
2194 val = XCHAR_TABLE (array)->contents[code[i]];
2195 if (SUB_CHAR_TABLE_P (val))
2196 array = val;
2197 else
2198 {
2199 Lisp_Object temp;
2200
2201 /* VAL is a leaf. Create a sub char table with the
2202 initial value VAL and look into it. */
2203
2204 temp = make_sub_char_table (val);
2205 XCHAR_TABLE (array)->contents[code[i]] = temp;
2206 array = temp;
2207 }
2208 }
2209 XCHAR_TABLE (array)->contents[code[i]] = newelt;
2210 }
2211 }
2212 else if (STRING_MULTIBYTE (array))
2213 {
2214 int idxval_byte, prev_bytes, new_bytes, nbytes;
2215 unsigned char workbuf[MAX_MULTIBYTE_LENGTH], *p0 = workbuf, *p1;
2216
2217 if (idxval < 0 || idxval >= SCHARS (array))
2218 args_out_of_range (array, idx);
2219 CHECK_NUMBER (newelt);
2220
2221 nbytes = SBYTES (array);
2222
2223 idxval_byte = string_char_to_byte (array, idxval);
2224 p1 = SDATA (array) + idxval_byte;
2225 PARSE_MULTIBYTE_SEQ (p1, nbytes - idxval_byte, prev_bytes);
2226 new_bytes = CHAR_STRING (XINT (newelt), p0);
2227 if (prev_bytes != new_bytes)
2228 {
2229 /* We must relocate the string data. */
2230 int nchars = SCHARS (array);
2231 unsigned char *str;
2232 USE_SAFE_ALLOCA;
2233
2234 SAFE_ALLOCA (str, unsigned char *, nbytes);
2235 bcopy (SDATA (array), str, nbytes);
2236 allocate_string_data (XSTRING (array), nchars,
2237 nbytes + new_bytes - prev_bytes);
2238 bcopy (str, SDATA (array), idxval_byte);
2239 p1 = SDATA (array) + idxval_byte;
2240 bcopy (str + idxval_byte + prev_bytes, p1 + new_bytes,
2241 nbytes - (idxval_byte + prev_bytes));
2242 SAFE_FREE ();
2243 clear_string_char_byte_cache ();
2244 }
2245 while (new_bytes--)
2246 *p1++ = *p0++;
2247 }
2248 else
2249 {
2250 if (idxval < 0 || idxval >= SCHARS (array))
2251 args_out_of_range (array, idx);
2252 CHECK_NUMBER (newelt);
2253
2254 if (XINT (newelt) < 0 || SINGLE_BYTE_CHAR_P (XINT (newelt)))
2255 SSET (array, idxval, XINT (newelt));
2256 else
2257 {
2258 /* We must relocate the string data while converting it to
2259 multibyte. */
2260 int idxval_byte, prev_bytes, new_bytes;
2261 unsigned char workbuf[MAX_MULTIBYTE_LENGTH], *p0 = workbuf, *p1;
2262 unsigned char *origstr = SDATA (array), *str;
2263 int nchars, nbytes;
2264 USE_SAFE_ALLOCA;
2265
2266 nchars = SCHARS (array);
2267 nbytes = idxval_byte = count_size_as_multibyte (origstr, idxval);
2268 nbytes += count_size_as_multibyte (origstr + idxval,
2269 nchars - idxval);
2270 SAFE_ALLOCA (str, unsigned char *, nbytes);
2271 copy_text (SDATA (array), str, nchars, 0, 1);
2272 PARSE_MULTIBYTE_SEQ (str + idxval_byte, nbytes - idxval_byte,
2273 prev_bytes);
2274 new_bytes = CHAR_STRING (XINT (newelt), p0);
2275 allocate_string_data (XSTRING (array), nchars,
2276 nbytes + new_bytes - prev_bytes);
2277 bcopy (str, SDATA (array), idxval_byte);
2278 p1 = SDATA (array) + idxval_byte;
2279 while (new_bytes--)
2280 *p1++ = *p0++;
2281 bcopy (str + idxval_byte + prev_bytes, p1,
2282 nbytes - (idxval_byte + prev_bytes));
2283 SAFE_FREE ();
2284 clear_string_char_byte_cache ();
2285 }
2286 }
2287
2288 return newelt;
2289 }
2290 \f
2291 /* Arithmetic functions */
2292
2293 enum comparison { equal, notequal, less, grtr, less_or_equal, grtr_or_equal };
2294
2295 Lisp_Object
2296 arithcompare (num1, num2, comparison)
2297 Lisp_Object num1, num2;
2298 enum comparison comparison;
2299 {
2300 double f1 = 0, f2 = 0;
2301 int floatp = 0;
2302
2303 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num1);
2304 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num2);
2305
2306 if (FLOATP (num1) || FLOATP (num2))
2307 {
2308 floatp = 1;
2309 f1 = (FLOATP (num1)) ? XFLOAT_DATA (num1) : XINT (num1);
2310 f2 = (FLOATP (num2)) ? XFLOAT_DATA (num2) : XINT (num2);
2311 }
2312
2313 switch (comparison)
2314 {
2315 case equal:
2316 if (floatp ? f1 == f2 : XINT (num1) == XINT (num2))
2317 return Qt;
2318 return Qnil;
2319
2320 case notequal:
2321 if (floatp ? f1 != f2 : XINT (num1) != XINT (num2))
2322 return Qt;
2323 return Qnil;
2324
2325 case less:
2326 if (floatp ? f1 < f2 : XINT (num1) < XINT (num2))
2327 return Qt;
2328 return Qnil;
2329
2330 case less_or_equal:
2331 if (floatp ? f1 <= f2 : XINT (num1) <= XINT (num2))
2332 return Qt;
2333 return Qnil;
2334
2335 case grtr:
2336 if (floatp ? f1 > f2 : XINT (num1) > XINT (num2))
2337 return Qt;
2338 return Qnil;
2339
2340 case grtr_or_equal:
2341 if (floatp ? f1 >= f2 : XINT (num1) >= XINT (num2))
2342 return Qt;
2343 return Qnil;
2344
2345 default:
2346 abort ();
2347 }
2348 }
2349
2350 DEFUN ("=", Feqlsign, Seqlsign, 2, 2, 0,
2351 doc: /* Return t if two args, both numbers or markers, are equal. */)
2352 (num1, num2)
2353 register Lisp_Object num1, num2;
2354 {
2355 return arithcompare (num1, num2, equal);
2356 }
2357
2358 DEFUN ("<", Flss, Slss, 2, 2, 0,
2359 doc: /* Return t if first arg is less than second arg. Both must be numbers or markers. */)
2360 (num1, num2)
2361 register Lisp_Object num1, num2;
2362 {
2363 return arithcompare (num1, num2, less);
2364 }
2365
2366 DEFUN (">", Fgtr, Sgtr, 2, 2, 0,
2367 doc: /* Return t if first arg is greater than second arg. Both must be numbers or markers. */)
2368 (num1, num2)
2369 register Lisp_Object num1, num2;
2370 {
2371 return arithcompare (num1, num2, grtr);
2372 }
2373
2374 DEFUN ("<=", Fleq, Sleq, 2, 2, 0,
2375 doc: /* Return t if first arg is less than or equal to second arg.
2376 Both must be numbers or markers. */)
2377 (num1, num2)
2378 register Lisp_Object num1, num2;
2379 {
2380 return arithcompare (num1, num2, less_or_equal);
2381 }
2382
2383 DEFUN (">=", Fgeq, Sgeq, 2, 2, 0,
2384 doc: /* Return t if first arg is greater than or equal to second arg.
2385 Both must be numbers or markers. */)
2386 (num1, num2)
2387 register Lisp_Object num1, num2;
2388 {
2389 return arithcompare (num1, num2, grtr_or_equal);
2390 }
2391
2392 DEFUN ("/=", Fneq, Sneq, 2, 2, 0,
2393 doc: /* Return t if first arg is not equal to second arg. Both must be numbers or markers. */)
2394 (num1, num2)
2395 register Lisp_Object num1, num2;
2396 {
2397 return arithcompare (num1, num2, notequal);
2398 }
2399
2400 DEFUN ("zerop", Fzerop, Szerop, 1, 1, 0,
2401 doc: /* Return t if NUMBER is zero. */)
2402 (number)
2403 register Lisp_Object number;
2404 {
2405 CHECK_NUMBER_OR_FLOAT (number);
2406
2407 if (FLOATP (number))
2408 {
2409 if (XFLOAT_DATA (number) == 0.0)
2410 return Qt;
2411 return Qnil;
2412 }
2413
2414 if (!XINT (number))
2415 return Qt;
2416 return Qnil;
2417 }
2418 \f
2419 /* Convert between long values and pairs of Lisp integers. */
2420
2421 Lisp_Object
2422 long_to_cons (i)
2423 unsigned long i;
2424 {
2425 unsigned long top = i >> 16;
2426 unsigned int bot = i & 0xFFFF;
2427 if (top == 0)
2428 return make_number (bot);
2429 if (top == (unsigned long)-1 >> 16)
2430 return Fcons (make_number (-1), make_number (bot));
2431 return Fcons (make_number (top), make_number (bot));
2432 }
2433
2434 unsigned long
2435 cons_to_long (c)
2436 Lisp_Object c;
2437 {
2438 Lisp_Object top, bot;
2439 if (INTEGERP (c))
2440 return XINT (c);
2441 top = XCAR (c);
2442 bot = XCDR (c);
2443 if (CONSP (bot))
2444 bot = XCAR (bot);
2445 return ((XINT (top) << 16) | XINT (bot));
2446 }
2447 \f
2448 DEFUN ("number-to-string", Fnumber_to_string, Snumber_to_string, 1, 1, 0,
2449 doc: /* Return the decimal representation of NUMBER as a string.
2450 Uses a minus sign if negative.
2451 NUMBER may be an integer or a floating point number. */)
2452 (number)
2453 Lisp_Object number;
2454 {
2455 char buffer[VALBITS];
2456
2457 CHECK_NUMBER_OR_FLOAT (number);
2458
2459 if (FLOATP (number))
2460 {
2461 char pigbuf[350]; /* see comments in float_to_string */
2462
2463 float_to_string (pigbuf, XFLOAT_DATA (number));
2464 return build_string (pigbuf);
2465 }
2466
2467 if (sizeof (int) == sizeof (EMACS_INT))
2468 sprintf (buffer, "%d", XINT (number));
2469 else if (sizeof (long) == sizeof (EMACS_INT))
2470 sprintf (buffer, "%ld", (long) XINT (number));
2471 else
2472 abort ();
2473 return build_string (buffer);
2474 }
2475
2476 INLINE static int
2477 digit_to_number (character, base)
2478 int character, base;
2479 {
2480 int digit;
2481
2482 if (character >= '0' && character <= '9')
2483 digit = character - '0';
2484 else if (character >= 'a' && character <= 'z')
2485 digit = character - 'a' + 10;
2486 else if (character >= 'A' && character <= 'Z')
2487 digit = character - 'A' + 10;
2488 else
2489 return -1;
2490
2491 if (digit >= base)
2492 return -1;
2493 else
2494 return digit;
2495 }
2496
2497 DEFUN ("string-to-number", Fstring_to_number, Sstring_to_number, 1, 2, 0,
2498 doc: /* Parse STRING as a decimal number and return the number.
2499 This parses both integers and floating point numbers.
2500 It ignores leading spaces and tabs.
2501
2502 If BASE, interpret STRING as a number in that base. If BASE isn't
2503 present, base 10 is used. BASE must be between 2 and 16 (inclusive).
2504 If the base used is not 10, floating point is not recognized. */)
2505 (string, base)
2506 register Lisp_Object string, base;
2507 {
2508 register unsigned char *p;
2509 register int b;
2510 int sign = 1;
2511 Lisp_Object val;
2512
2513 CHECK_STRING (string);
2514
2515 if (NILP (base))
2516 b = 10;
2517 else
2518 {
2519 CHECK_NUMBER (base);
2520 b = XINT (base);
2521 if (b < 2 || b > 16)
2522 Fsignal (Qargs_out_of_range, Fcons (base, Qnil));
2523 }
2524
2525 /* Skip any whitespace at the front of the number. Some versions of
2526 atoi do this anyway, so we might as well make Emacs lisp consistent. */
2527 p = SDATA (string);
2528 while (*p == ' ' || *p == '\t')
2529 p++;
2530
2531 if (*p == '-')
2532 {
2533 sign = -1;
2534 p++;
2535 }
2536 else if (*p == '+')
2537 p++;
2538
2539 if (isfloat_string (p) && b == 10)
2540 val = make_float (sign * atof (p));
2541 else
2542 {
2543 double v = 0;
2544
2545 while (1)
2546 {
2547 int digit = digit_to_number (*p++, b);
2548 if (digit < 0)
2549 break;
2550 v = v * b + digit;
2551 }
2552
2553 val = make_fixnum_or_float (sign * v);
2554 }
2555
2556 return val;
2557 }
2558
2559 \f
2560 enum arithop
2561 {
2562 Aadd,
2563 Asub,
2564 Amult,
2565 Adiv,
2566 Alogand,
2567 Alogior,
2568 Alogxor,
2569 Amax,
2570 Amin
2571 };
2572
2573 static Lisp_Object float_arith_driver P_ ((double, int, enum arithop,
2574 int, Lisp_Object *));
2575 extern Lisp_Object fmod_float ();
2576
2577 Lisp_Object
2578 arith_driver (code, nargs, args)
2579 enum arithop code;
2580 int nargs;
2581 register Lisp_Object *args;
2582 {
2583 register Lisp_Object val;
2584 register int argnum;
2585 register EMACS_INT accum = 0;
2586 register EMACS_INT next;
2587
2588 switch (SWITCH_ENUM_CAST (code))
2589 {
2590 case Alogior:
2591 case Alogxor:
2592 case Aadd:
2593 case Asub:
2594 accum = 0;
2595 break;
2596 case Amult:
2597 accum = 1;
2598 break;
2599 case Alogand:
2600 accum = -1;
2601 break;
2602 default:
2603 break;
2604 }
2605
2606 for (argnum = 0; argnum < nargs; argnum++)
2607 {
2608 /* Using args[argnum] as argument to CHECK_NUMBER_... */
2609 val = args[argnum];
2610 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val);
2611
2612 if (FLOATP (val))
2613 return float_arith_driver ((double) accum, argnum, code,
2614 nargs, args);
2615 args[argnum] = val;
2616 next = XINT (args[argnum]);
2617 switch (SWITCH_ENUM_CAST (code))
2618 {
2619 case Aadd:
2620 accum += next;
2621 break;
2622 case Asub:
2623 accum = argnum ? accum - next : nargs == 1 ? - next : next;
2624 break;
2625 case Amult:
2626 accum *= next;
2627 break;
2628 case Adiv:
2629 if (!argnum)
2630 accum = next;
2631 else
2632 {
2633 if (next == 0)
2634 Fsignal (Qarith_error, Qnil);
2635 accum /= next;
2636 }
2637 break;
2638 case Alogand:
2639 accum &= next;
2640 break;
2641 case Alogior:
2642 accum |= next;
2643 break;
2644 case Alogxor:
2645 accum ^= next;
2646 break;
2647 case Amax:
2648 if (!argnum || next > accum)
2649 accum = next;
2650 break;
2651 case Amin:
2652 if (!argnum || next < accum)
2653 accum = next;
2654 break;
2655 }
2656 }
2657
2658 XSETINT (val, accum);
2659 return val;
2660 }
2661
2662 #undef isnan
2663 #define isnan(x) ((x) != (x))
2664
2665 static Lisp_Object
2666 float_arith_driver (accum, argnum, code, nargs, args)
2667 double accum;
2668 register int argnum;
2669 enum arithop code;
2670 int nargs;
2671 register Lisp_Object *args;
2672 {
2673 register Lisp_Object val;
2674 double next;
2675
2676 for (; argnum < nargs; argnum++)
2677 {
2678 val = args[argnum]; /* using args[argnum] as argument to CHECK_NUMBER_... */
2679 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val);
2680
2681 if (FLOATP (val))
2682 {
2683 next = XFLOAT_DATA (val);
2684 }
2685 else
2686 {
2687 args[argnum] = val; /* runs into a compiler bug. */
2688 next = XINT (args[argnum]);
2689 }
2690 switch (SWITCH_ENUM_CAST (code))
2691 {
2692 case Aadd:
2693 accum += next;
2694 break;
2695 case Asub:
2696 accum = argnum ? accum - next : nargs == 1 ? - next : next;
2697 break;
2698 case Amult:
2699 accum *= next;
2700 break;
2701 case Adiv:
2702 if (!argnum)
2703 accum = next;
2704 else
2705 {
2706 if (! IEEE_FLOATING_POINT && next == 0)
2707 Fsignal (Qarith_error, Qnil);
2708 accum /= next;
2709 }
2710 break;
2711 case Alogand:
2712 case Alogior:
2713 case Alogxor:
2714 return wrong_type_argument (Qinteger_or_marker_p, val);
2715 case Amax:
2716 if (!argnum || isnan (next) || next > accum)
2717 accum = next;
2718 break;
2719 case Amin:
2720 if (!argnum || isnan (next) || next < accum)
2721 accum = next;
2722 break;
2723 }
2724 }
2725
2726 return make_float (accum);
2727 }
2728
2729
2730 DEFUN ("+", Fplus, Splus, 0, MANY, 0,
2731 doc: /* Return sum of any number of arguments, which are numbers or markers.
2732 usage: (+ &rest NUMBERS-OR-MARKERS) */)
2733 (nargs, args)
2734 int nargs;
2735 Lisp_Object *args;
2736 {
2737 return arith_driver (Aadd, nargs, args);
2738 }
2739
2740 DEFUN ("-", Fminus, Sminus, 0, MANY, 0,
2741 doc: /* Negate number or subtract numbers or markers and return the result.
2742 With one arg, negates it. With more than one arg,
2743 subtracts all but the first from the first.
2744 usage: (- &optional NUMBER-OR-MARKER &rest MORE-NUMBERS-OR-MARKERS) */)
2745 (nargs, args)
2746 int nargs;
2747 Lisp_Object *args;
2748 {
2749 return arith_driver (Asub, nargs, args);
2750 }
2751
2752 DEFUN ("*", Ftimes, Stimes, 0, MANY, 0,
2753 doc: /* Return product of any number of arguments, which are numbers or markers.
2754 usage: (* &rest NUMBERS-OR-MARKERS) */)
2755 (nargs, args)
2756 int nargs;
2757 Lisp_Object *args;
2758 {
2759 return arith_driver (Amult, nargs, args);
2760 }
2761
2762 DEFUN ("/", Fquo, Squo, 2, MANY, 0,
2763 doc: /* Return first argument divided by all the remaining arguments.
2764 The arguments must be numbers or markers.
2765 usage: (/ DIVIDEND DIVISOR &rest DIVISORS) */)
2766 (nargs, args)
2767 int nargs;
2768 Lisp_Object *args;
2769 {
2770 int argnum;
2771 for (argnum = 2; argnum < nargs; argnum++)
2772 if (FLOATP (args[argnum]))
2773 return float_arith_driver (0, 0, Adiv, nargs, args);
2774 return arith_driver (Adiv, nargs, args);
2775 }
2776
2777 DEFUN ("%", Frem, Srem, 2, 2, 0,
2778 doc: /* Return remainder of X divided by Y.
2779 Both must be integers or markers. */)
2780 (x, y)
2781 register Lisp_Object x, y;
2782 {
2783 Lisp_Object val;
2784
2785 CHECK_NUMBER_COERCE_MARKER (x);
2786 CHECK_NUMBER_COERCE_MARKER (y);
2787
2788 if (XFASTINT (y) == 0)
2789 Fsignal (Qarith_error, Qnil);
2790
2791 XSETINT (val, XINT (x) % XINT (y));
2792 return val;
2793 }
2794
2795 #ifndef HAVE_FMOD
2796 double
2797 fmod (f1, f2)
2798 double f1, f2;
2799 {
2800 double r = f1;
2801
2802 if (f2 < 0.0)
2803 f2 = -f2;
2804
2805 /* If the magnitude of the result exceeds that of the divisor, or
2806 the sign of the result does not agree with that of the dividend,
2807 iterate with the reduced value. This does not yield a
2808 particularly accurate result, but at least it will be in the
2809 range promised by fmod. */
2810 do
2811 r -= f2 * floor (r / f2);
2812 while (f2 <= (r < 0 ? -r : r) || ((r < 0) != (f1 < 0) && ! isnan (r)));
2813
2814 return r;
2815 }
2816 #endif /* ! HAVE_FMOD */
2817
2818 DEFUN ("mod", Fmod, Smod, 2, 2, 0,
2819 doc: /* Return X modulo Y.
2820 The result falls between zero (inclusive) and Y (exclusive).
2821 Both X and Y must be numbers or markers. */)
2822 (x, y)
2823 register Lisp_Object x, y;
2824 {
2825 Lisp_Object val;
2826 EMACS_INT i1, i2;
2827
2828 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (x);
2829 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (y);
2830
2831 if (FLOATP (x) || FLOATP (y))
2832 return fmod_float (x, y);
2833
2834 i1 = XINT (x);
2835 i2 = XINT (y);
2836
2837 if (i2 == 0)
2838 Fsignal (Qarith_error, Qnil);
2839
2840 i1 %= i2;
2841
2842 /* If the "remainder" comes out with the wrong sign, fix it. */
2843 if (i2 < 0 ? i1 > 0 : i1 < 0)
2844 i1 += i2;
2845
2846 XSETINT (val, i1);
2847 return val;
2848 }
2849
2850 DEFUN ("max", Fmax, Smax, 1, MANY, 0,
2851 doc: /* Return largest of all the arguments (which must be numbers or markers).
2852 The value is always a number; markers are converted to numbers.
2853 usage: (max NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2854 (nargs, args)
2855 int nargs;
2856 Lisp_Object *args;
2857 {
2858 return arith_driver (Amax, nargs, args);
2859 }
2860
2861 DEFUN ("min", Fmin, Smin, 1, MANY, 0,
2862 doc: /* Return smallest of all the arguments (which must be numbers or markers).
2863 The value is always a number; markers are converted to numbers.
2864 usage: (min NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2865 (nargs, args)
2866 int nargs;
2867 Lisp_Object *args;
2868 {
2869 return arith_driver (Amin, nargs, args);
2870 }
2871
2872 DEFUN ("logand", Flogand, Slogand, 0, MANY, 0,
2873 doc: /* Return bitwise-and of all the arguments.
2874 Arguments may be integers, or markers converted to integers.
2875 usage: (logand &rest INTS-OR-MARKERS) */)
2876 (nargs, args)
2877 int nargs;
2878 Lisp_Object *args;
2879 {
2880 return arith_driver (Alogand, nargs, args);
2881 }
2882
2883 DEFUN ("logior", Flogior, Slogior, 0, MANY, 0,
2884 doc: /* Return bitwise-or of all the arguments.
2885 Arguments may be integers, or markers converted to integers.
2886 usage: (logior &rest INTS-OR-MARKERS) */)
2887 (nargs, args)
2888 int nargs;
2889 Lisp_Object *args;
2890 {
2891 return arith_driver (Alogior, nargs, args);
2892 }
2893
2894 DEFUN ("logxor", Flogxor, Slogxor, 0, MANY, 0,
2895 doc: /* Return bitwise-exclusive-or of all the arguments.
2896 Arguments may be integers, or markers converted to integers.
2897 usage: (logxor &rest INTS-OR-MARKERS) */)
2898 (nargs, args)
2899 int nargs;
2900 Lisp_Object *args;
2901 {
2902 return arith_driver (Alogxor, nargs, args);
2903 }
2904
2905 DEFUN ("ash", Fash, Sash, 2, 2, 0,
2906 doc: /* Return VALUE with its bits shifted left by COUNT.
2907 If COUNT is negative, shifting is actually to the right.
2908 In this case, the sign bit is duplicated. */)
2909 (value, count)
2910 register Lisp_Object value, count;
2911 {
2912 register Lisp_Object val;
2913
2914 CHECK_NUMBER (value);
2915 CHECK_NUMBER (count);
2916
2917 if (XINT (count) >= BITS_PER_EMACS_INT)
2918 XSETINT (val, 0);
2919 else if (XINT (count) > 0)
2920 XSETINT (val, XINT (value) << XFASTINT (count));
2921 else if (XINT (count) <= -BITS_PER_EMACS_INT)
2922 XSETINT (val, XINT (value) < 0 ? -1 : 0);
2923 else
2924 XSETINT (val, XINT (value) >> -XINT (count));
2925 return val;
2926 }
2927
2928 DEFUN ("lsh", Flsh, Slsh, 2, 2, 0,
2929 doc: /* Return VALUE with its bits shifted left by COUNT.
2930 If COUNT is negative, shifting is actually to the right.
2931 In this case, zeros are shifted in on the left. */)
2932 (value, count)
2933 register Lisp_Object value, count;
2934 {
2935 register Lisp_Object val;
2936
2937 CHECK_NUMBER (value);
2938 CHECK_NUMBER (count);
2939
2940 if (XINT (count) >= BITS_PER_EMACS_INT)
2941 XSETINT (val, 0);
2942 else if (XINT (count) > 0)
2943 XSETINT (val, (EMACS_UINT) XUINT (value) << XFASTINT (count));
2944 else if (XINT (count) <= -BITS_PER_EMACS_INT)
2945 XSETINT (val, 0);
2946 else
2947 XSETINT (val, (EMACS_UINT) XUINT (value) >> -XINT (count));
2948 return val;
2949 }
2950
2951 DEFUN ("1+", Fadd1, Sadd1, 1, 1, 0,
2952 doc: /* Return NUMBER plus one. NUMBER may be a number or a marker.
2953 Markers are converted to integers. */)
2954 (number)
2955 register Lisp_Object number;
2956 {
2957 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number);
2958
2959 if (FLOATP (number))
2960 return (make_float (1.0 + XFLOAT_DATA (number)));
2961
2962 XSETINT (number, XINT (number) + 1);
2963 return number;
2964 }
2965
2966 DEFUN ("1-", Fsub1, Ssub1, 1, 1, 0,
2967 doc: /* Return NUMBER minus one. NUMBER may be a number or a marker.
2968 Markers are converted to integers. */)
2969 (number)
2970 register Lisp_Object number;
2971 {
2972 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number);
2973
2974 if (FLOATP (number))
2975 return (make_float (-1.0 + XFLOAT_DATA (number)));
2976
2977 XSETINT (number, XINT (number) - 1);
2978 return number;
2979 }
2980
2981 DEFUN ("lognot", Flognot, Slognot, 1, 1, 0,
2982 doc: /* Return the bitwise complement of NUMBER. NUMBER must be an integer. */)
2983 (number)
2984 register Lisp_Object number;
2985 {
2986 CHECK_NUMBER (number);
2987 XSETINT (number, ~XINT (number));
2988 return number;
2989 }
2990
2991 DEFUN ("byteorder", Fbyteorder, Sbyteorder, 0, 0, 0,
2992 doc: /* Return the byteorder for the machine.
2993 Returns 66 (ASCII uppercase B) for big endian machines or 108 (ASCII
2994 lowercase l) for small endian machines. */)
2995 ()
2996 {
2997 unsigned i = 0x04030201;
2998 int order = *(char *)&i == 1 ? 108 : 66;
2999
3000 return make_number (order);
3001 }
3002
3003
3004 \f
3005 void
3006 syms_of_data ()
3007 {
3008 Lisp_Object error_tail, arith_tail;
3009
3010 Qquote = intern ("quote");
3011 Qlambda = intern ("lambda");
3012 Qsubr = intern ("subr");
3013 Qerror_conditions = intern ("error-conditions");
3014 Qerror_message = intern ("error-message");
3015 Qtop_level = intern ("top-level");
3016
3017 Qerror = intern ("error");
3018 Qquit = intern ("quit");
3019 Qwrong_type_argument = intern ("wrong-type-argument");
3020 Qargs_out_of_range = intern ("args-out-of-range");
3021 Qvoid_function = intern ("void-function");
3022 Qcyclic_function_indirection = intern ("cyclic-function-indirection");
3023 Qcyclic_variable_indirection = intern ("cyclic-variable-indirection");
3024 Qvoid_variable = intern ("void-variable");
3025 Qsetting_constant = intern ("setting-constant");
3026 Qinvalid_read_syntax = intern ("invalid-read-syntax");
3027
3028 Qinvalid_function = intern ("invalid-function");
3029 Qwrong_number_of_arguments = intern ("wrong-number-of-arguments");
3030 Qno_catch = intern ("no-catch");
3031 Qend_of_file = intern ("end-of-file");
3032 Qarith_error = intern ("arith-error");
3033 Qbeginning_of_buffer = intern ("beginning-of-buffer");
3034 Qend_of_buffer = intern ("end-of-buffer");
3035 Qbuffer_read_only = intern ("buffer-read-only");
3036 Qtext_read_only = intern ("text-read-only");
3037 Qmark_inactive = intern ("mark-inactive");
3038
3039 Qlistp = intern ("listp");
3040 Qconsp = intern ("consp");
3041 Qsymbolp = intern ("symbolp");
3042 Qkeywordp = intern ("keywordp");
3043 Qintegerp = intern ("integerp");
3044 Qnatnump = intern ("natnump");
3045 Qwholenump = intern ("wholenump");
3046 Qstringp = intern ("stringp");
3047 Qarrayp = intern ("arrayp");
3048 Qsequencep = intern ("sequencep");
3049 Qbufferp = intern ("bufferp");
3050 Qvectorp = intern ("vectorp");
3051 Qchar_or_string_p = intern ("char-or-string-p");
3052 Qmarkerp = intern ("markerp");
3053 Qbuffer_or_string_p = intern ("buffer-or-string-p");
3054 Qinteger_or_marker_p = intern ("integer-or-marker-p");
3055 Qboundp = intern ("boundp");
3056 Qfboundp = intern ("fboundp");
3057
3058 Qfloatp = intern ("floatp");
3059 Qnumberp = intern ("numberp");
3060 Qnumber_or_marker_p = intern ("number-or-marker-p");
3061
3062 Qchar_table_p = intern ("char-table-p");
3063 Qvector_or_char_table_p = intern ("vector-or-char-table-p");
3064
3065 Qsubrp = intern ("subrp");
3066 Qunevalled = intern ("unevalled");
3067 Qmany = intern ("many");
3068
3069 Qcdr = intern ("cdr");
3070
3071 /* Handle automatic advice activation */
3072 Qad_advice_info = intern ("ad-advice-info");
3073 Qad_activate_internal = intern ("ad-activate-internal");
3074
3075 error_tail = Fcons (Qerror, Qnil);
3076
3077 /* ERROR is used as a signaler for random errors for which nothing else is right */
3078
3079 Fput (Qerror, Qerror_conditions,
3080 error_tail);
3081 Fput (Qerror, Qerror_message,
3082 build_string ("error"));
3083
3084 Fput (Qquit, Qerror_conditions,
3085 Fcons (Qquit, Qnil));
3086 Fput (Qquit, Qerror_message,
3087 build_string ("Quit"));
3088
3089 Fput (Qwrong_type_argument, Qerror_conditions,
3090 Fcons (Qwrong_type_argument, error_tail));
3091 Fput (Qwrong_type_argument, Qerror_message,
3092 build_string ("Wrong type argument"));
3093
3094 Fput (Qargs_out_of_range, Qerror_conditions,
3095 Fcons (Qargs_out_of_range, error_tail));
3096 Fput (Qargs_out_of_range, Qerror_message,
3097 build_string ("Args out of range"));
3098
3099 Fput (Qvoid_function, Qerror_conditions,
3100 Fcons (Qvoid_function, error_tail));
3101 Fput (Qvoid_function, Qerror_message,
3102 build_string ("Symbol's function definition is void"));
3103
3104 Fput (Qcyclic_function_indirection, Qerror_conditions,
3105 Fcons (Qcyclic_function_indirection, error_tail));
3106 Fput (Qcyclic_function_indirection, Qerror_message,
3107 build_string ("Symbol's chain of function indirections contains a loop"));
3108
3109 Fput (Qcyclic_variable_indirection, Qerror_conditions,
3110 Fcons (Qcyclic_variable_indirection, error_tail));
3111 Fput (Qcyclic_variable_indirection, Qerror_message,
3112 build_string ("Symbol's chain of variable indirections contains a loop"));
3113
3114 Qcircular_list = intern ("circular-list");
3115 staticpro (&Qcircular_list);
3116 Fput (Qcircular_list, Qerror_conditions,
3117 Fcons (Qcircular_list, error_tail));
3118 Fput (Qcircular_list, Qerror_message,
3119 build_string ("List contains a loop"));
3120
3121 Fput (Qvoid_variable, Qerror_conditions,
3122 Fcons (Qvoid_variable, error_tail));
3123 Fput (Qvoid_variable, Qerror_message,
3124 build_string ("Symbol's value as variable is void"));
3125
3126 Fput (Qsetting_constant, Qerror_conditions,
3127 Fcons (Qsetting_constant, error_tail));
3128 Fput (Qsetting_constant, Qerror_message,
3129 build_string ("Attempt to set a constant symbol"));
3130
3131 Fput (Qinvalid_read_syntax, Qerror_conditions,
3132 Fcons (Qinvalid_read_syntax, error_tail));
3133 Fput (Qinvalid_read_syntax, Qerror_message,
3134 build_string ("Invalid read syntax"));
3135
3136 Fput (Qinvalid_function, Qerror_conditions,
3137 Fcons (Qinvalid_function, error_tail));
3138 Fput (Qinvalid_function, Qerror_message,
3139 build_string ("Invalid function"));
3140
3141 Fput (Qwrong_number_of_arguments, Qerror_conditions,
3142 Fcons (Qwrong_number_of_arguments, error_tail));
3143 Fput (Qwrong_number_of_arguments, Qerror_message,
3144 build_string ("Wrong number of arguments"));
3145
3146 Fput (Qno_catch, Qerror_conditions,
3147 Fcons (Qno_catch, error_tail));
3148 Fput (Qno_catch, Qerror_message,
3149 build_string ("No catch for tag"));
3150
3151 Fput (Qend_of_file, Qerror_conditions,
3152 Fcons (Qend_of_file, error_tail));
3153 Fput (Qend_of_file, Qerror_message,
3154 build_string ("End of file during parsing"));
3155
3156 arith_tail = Fcons (Qarith_error, error_tail);
3157 Fput (Qarith_error, Qerror_conditions,
3158 arith_tail);
3159 Fput (Qarith_error, Qerror_message,
3160 build_string ("Arithmetic error"));
3161
3162 Fput (Qbeginning_of_buffer, Qerror_conditions,
3163 Fcons (Qbeginning_of_buffer, error_tail));
3164 Fput (Qbeginning_of_buffer, Qerror_message,
3165 build_string ("Beginning of buffer"));
3166
3167 Fput (Qend_of_buffer, Qerror_conditions,
3168 Fcons (Qend_of_buffer, error_tail));
3169 Fput (Qend_of_buffer, Qerror_message,
3170 build_string ("End of buffer"));
3171
3172 Fput (Qbuffer_read_only, Qerror_conditions,
3173 Fcons (Qbuffer_read_only, error_tail));
3174 Fput (Qbuffer_read_only, Qerror_message,
3175 build_string ("Buffer is read-only"));
3176
3177 Fput (Qtext_read_only, Qerror_conditions,
3178 Fcons (Qtext_read_only, error_tail));
3179 Fput (Qtext_read_only, Qerror_message,
3180 build_string ("Text is read-only"));
3181
3182 Qrange_error = intern ("range-error");
3183 Qdomain_error = intern ("domain-error");
3184 Qsingularity_error = intern ("singularity-error");
3185 Qoverflow_error = intern ("overflow-error");
3186 Qunderflow_error = intern ("underflow-error");
3187
3188 Fput (Qdomain_error, Qerror_conditions,
3189 Fcons (Qdomain_error, arith_tail));
3190 Fput (Qdomain_error, Qerror_message,
3191 build_string ("Arithmetic domain error"));
3192
3193 Fput (Qrange_error, Qerror_conditions,
3194 Fcons (Qrange_error, arith_tail));
3195 Fput (Qrange_error, Qerror_message,
3196 build_string ("Arithmetic range error"));
3197
3198 Fput (Qsingularity_error, Qerror_conditions,
3199 Fcons (Qsingularity_error, Fcons (Qdomain_error, arith_tail)));
3200 Fput (Qsingularity_error, Qerror_message,
3201 build_string ("Arithmetic singularity error"));
3202
3203 Fput (Qoverflow_error, Qerror_conditions,
3204 Fcons (Qoverflow_error, Fcons (Qdomain_error, arith_tail)));
3205 Fput (Qoverflow_error, Qerror_message,
3206 build_string ("Arithmetic overflow error"));
3207
3208 Fput (Qunderflow_error, Qerror_conditions,
3209 Fcons (Qunderflow_error, Fcons (Qdomain_error, arith_tail)));
3210 Fput (Qunderflow_error, Qerror_message,
3211 build_string ("Arithmetic underflow error"));
3212
3213 staticpro (&Qrange_error);
3214 staticpro (&Qdomain_error);
3215 staticpro (&Qsingularity_error);
3216 staticpro (&Qoverflow_error);
3217 staticpro (&Qunderflow_error);
3218
3219 staticpro (&Qnil);
3220 staticpro (&Qt);
3221 staticpro (&Qquote);
3222 staticpro (&Qlambda);
3223 staticpro (&Qsubr);
3224 staticpro (&Qunbound);
3225 staticpro (&Qerror_conditions);
3226 staticpro (&Qerror_message);
3227 staticpro (&Qtop_level);
3228
3229 staticpro (&Qerror);
3230 staticpro (&Qquit);
3231 staticpro (&Qwrong_type_argument);
3232 staticpro (&Qargs_out_of_range);
3233 staticpro (&Qvoid_function);
3234 staticpro (&Qcyclic_function_indirection);
3235 staticpro (&Qcyclic_variable_indirection);
3236 staticpro (&Qvoid_variable);
3237 staticpro (&Qsetting_constant);
3238 staticpro (&Qinvalid_read_syntax);
3239 staticpro (&Qwrong_number_of_arguments);
3240 staticpro (&Qinvalid_function);
3241 staticpro (&Qno_catch);
3242 staticpro (&Qend_of_file);
3243 staticpro (&Qarith_error);
3244 staticpro (&Qbeginning_of_buffer);
3245 staticpro (&Qend_of_buffer);
3246 staticpro (&Qbuffer_read_only);
3247 staticpro (&Qtext_read_only);
3248 staticpro (&Qmark_inactive);
3249
3250 staticpro (&Qlistp);
3251 staticpro (&Qconsp);
3252 staticpro (&Qsymbolp);
3253 staticpro (&Qkeywordp);
3254 staticpro (&Qintegerp);
3255 staticpro (&Qnatnump);
3256 staticpro (&Qwholenump);
3257 staticpro (&Qstringp);
3258 staticpro (&Qarrayp);
3259 staticpro (&Qsequencep);
3260 staticpro (&Qbufferp);
3261 staticpro (&Qvectorp);
3262 staticpro (&Qchar_or_string_p);
3263 staticpro (&Qmarkerp);
3264 staticpro (&Qbuffer_or_string_p);
3265 staticpro (&Qinteger_or_marker_p);
3266 staticpro (&Qfloatp);
3267 staticpro (&Qnumberp);
3268 staticpro (&Qnumber_or_marker_p);
3269 staticpro (&Qchar_table_p);
3270 staticpro (&Qvector_or_char_table_p);
3271 staticpro (&Qsubrp);
3272 staticpro (&Qmany);
3273 staticpro (&Qunevalled);
3274
3275 staticpro (&Qboundp);
3276 staticpro (&Qfboundp);
3277 staticpro (&Qcdr);
3278 staticpro (&Qad_advice_info);
3279 staticpro (&Qad_activate_internal);
3280
3281 /* Types that type-of returns. */
3282 Qinteger = intern ("integer");
3283 Qsymbol = intern ("symbol");
3284 Qstring = intern ("string");
3285 Qcons = intern ("cons");
3286 Qmarker = intern ("marker");
3287 Qoverlay = intern ("overlay");
3288 Qfloat = intern ("float");
3289 Qwindow_configuration = intern ("window-configuration");
3290 Qprocess = intern ("process");
3291 Qwindow = intern ("window");
3292 /* Qsubr = intern ("subr"); */
3293 Qcompiled_function = intern ("compiled-function");
3294 Qbuffer = intern ("buffer");
3295 Qframe = intern ("frame");
3296 Qvector = intern ("vector");
3297 Qchar_table = intern ("char-table");
3298 Qbool_vector = intern ("bool-vector");
3299 Qhash_table = intern ("hash-table");
3300
3301 staticpro (&Qinteger);
3302 staticpro (&Qsymbol);
3303 staticpro (&Qstring);
3304 staticpro (&Qcons);
3305 staticpro (&Qmarker);
3306 staticpro (&Qoverlay);
3307 staticpro (&Qfloat);
3308 staticpro (&Qwindow_configuration);
3309 staticpro (&Qprocess);
3310 staticpro (&Qwindow);
3311 /* staticpro (&Qsubr); */
3312 staticpro (&Qcompiled_function);
3313 staticpro (&Qbuffer);
3314 staticpro (&Qframe);
3315 staticpro (&Qvector);
3316 staticpro (&Qchar_table);
3317 staticpro (&Qbool_vector);
3318 staticpro (&Qhash_table);
3319
3320 defsubr (&Sindirect_variable);
3321 defsubr (&Sinteractive_form);
3322 defsubr (&Seq);
3323 defsubr (&Snull);
3324 defsubr (&Stype_of);
3325 defsubr (&Slistp);
3326 defsubr (&Snlistp);
3327 defsubr (&Sconsp);
3328 defsubr (&Satom);
3329 defsubr (&Sintegerp);
3330 defsubr (&Sinteger_or_marker_p);
3331 defsubr (&Snumberp);
3332 defsubr (&Snumber_or_marker_p);
3333 defsubr (&Sfloatp);
3334 defsubr (&Snatnump);
3335 defsubr (&Ssymbolp);
3336 defsubr (&Skeywordp);
3337 defsubr (&Sstringp);
3338 defsubr (&Smultibyte_string_p);
3339 defsubr (&Svectorp);
3340 defsubr (&Schar_table_p);
3341 defsubr (&Svector_or_char_table_p);
3342 defsubr (&Sbool_vector_p);
3343 defsubr (&Sarrayp);
3344 defsubr (&Ssequencep);
3345 defsubr (&Sbufferp);
3346 defsubr (&Smarkerp);
3347 defsubr (&Ssubrp);
3348 defsubr (&Sbyte_code_function_p);
3349 defsubr (&Schar_or_string_p);
3350 defsubr (&Scar);
3351 defsubr (&Scdr);
3352 defsubr (&Scar_safe);
3353 defsubr (&Scdr_safe);
3354 defsubr (&Ssetcar);
3355 defsubr (&Ssetcdr);
3356 defsubr (&Ssymbol_function);
3357 defsubr (&Sindirect_function);
3358 defsubr (&Ssymbol_plist);
3359 defsubr (&Ssymbol_name);
3360 defsubr (&Smakunbound);
3361 defsubr (&Sfmakunbound);
3362 defsubr (&Sboundp);
3363 defsubr (&Sfboundp);
3364 defsubr (&Sfset);
3365 defsubr (&Sdefalias);
3366 defsubr (&Ssetplist);
3367 defsubr (&Ssymbol_value);
3368 defsubr (&Sset);
3369 defsubr (&Sdefault_boundp);
3370 defsubr (&Sdefault_value);
3371 defsubr (&Sset_default);
3372 defsubr (&Ssetq_default);
3373 defsubr (&Smake_variable_buffer_local);
3374 defsubr (&Smake_local_variable);
3375 defsubr (&Skill_local_variable);
3376 defsubr (&Smake_variable_frame_local);
3377 defsubr (&Slocal_variable_p);
3378 defsubr (&Slocal_variable_if_set_p);
3379 defsubr (&Svariable_binding_locus);
3380 #if 0 /* XXX Remove this. --lorentey */
3381 defsubr (&Sterminal_local_value);
3382 defsubr (&Sset_terminal_local_value);
3383 #endif
3384 defsubr (&Saref);
3385 defsubr (&Saset);
3386 defsubr (&Snumber_to_string);
3387 defsubr (&Sstring_to_number);
3388 defsubr (&Seqlsign);
3389 defsubr (&Slss);
3390 defsubr (&Sgtr);
3391 defsubr (&Sleq);
3392 defsubr (&Sgeq);
3393 defsubr (&Sneq);
3394 defsubr (&Szerop);
3395 defsubr (&Splus);
3396 defsubr (&Sminus);
3397 defsubr (&Stimes);
3398 defsubr (&Squo);
3399 defsubr (&Srem);
3400 defsubr (&Smod);
3401 defsubr (&Smax);
3402 defsubr (&Smin);
3403 defsubr (&Slogand);
3404 defsubr (&Slogior);
3405 defsubr (&Slogxor);
3406 defsubr (&Slsh);
3407 defsubr (&Sash);
3408 defsubr (&Sadd1);
3409 defsubr (&Ssub1);
3410 defsubr (&Slognot);
3411 defsubr (&Sbyteorder);
3412 defsubr (&Ssubr_arity);
3413 defsubr (&Ssubr_name);
3414
3415 XSYMBOL (Qwholenump)->function = XSYMBOL (Qnatnump)->function;
3416
3417 DEFVAR_LISP ("most-positive-fixnum", &Vmost_positive_fixnum,
3418 doc: /* The largest value that is representable in a Lisp integer. */);
3419 Vmost_positive_fixnum = make_number (MOST_POSITIVE_FIXNUM);
3420
3421 DEFVAR_LISP ("most-negative-fixnum", &Vmost_negative_fixnum,
3422 doc: /* The smallest value that is representable in a Lisp integer. */);
3423 Vmost_negative_fixnum = make_number (MOST_NEGATIVE_FIXNUM);
3424 }
3425
3426 SIGTYPE
3427 arith_error (signo)
3428 int signo;
3429 {
3430 #if defined(USG) && !defined(POSIX_SIGNALS)
3431 /* USG systems forget handlers when they are used;
3432 must reestablish each time */
3433 signal (signo, arith_error);
3434 #endif /* USG */
3435 #ifdef VMS
3436 /* VMS systems are like USG. */
3437 signal (signo, arith_error);
3438 #endif /* VMS */
3439 #ifdef BSD4_1
3440 sigrelse (SIGFPE);
3441 #else /* not BSD4_1 */
3442 sigsetmask (SIGEMPTYMASK);
3443 #endif /* not BSD4_1 */
3444
3445 SIGNAL_THREAD_CHECK (signo);
3446 Fsignal (Qarith_error, Qnil);
3447 }
3448
3449 void
3450 init_data ()
3451 {
3452 /* Don't do this if just dumping out.
3453 We don't want to call `signal' in this case
3454 so that we don't have trouble with dumping
3455 signal-delivering routines in an inconsistent state. */
3456 #ifndef CANNOT_DUMP
3457 if (!initialized)
3458 return;
3459 #endif /* CANNOT_DUMP */
3460 signal (SIGFPE, arith_error);
3461
3462 #ifdef uts
3463 signal (SIGEMT, arith_error);
3464 #endif /* uts */
3465 }
3466
3467 /* arch-tag: 25879798-b84d-479a-9c89-7d148e2109f7
3468 (do not change this comment) */