Fix rounding errors in <, =, etc.
[emacs.git] / src / data.c
blob88d86697e426111246a1f74fc76fef23ea9e1623
1 /* Primitive operations on Lisp data types for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985-1986, 1988, 1993-1995, 1997-2017 Free Software
3 Foundation, Inc.
5 This file is part of GNU Emacs.
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 3 of the License, or (at
10 your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
22 #include <stdio.h>
24 #include <byteswap.h>
25 #include <count-one-bits.h>
26 #include <count-trailing-zeros.h>
27 #include <intprops.h>
29 #include "lisp.h"
30 #include "puresize.h"
31 #include "character.h"
32 #include "buffer.h"
33 #include "keyboard.h"
34 #include "process.h"
35 #include "frame.h"
36 #include "keymap.h"
38 static void swap_in_symval_forwarding (struct Lisp_Symbol *,
39 struct Lisp_Buffer_Local_Value *);
41 static bool
42 BOOLFWDP (union Lisp_Fwd *a)
44 return XFWDTYPE (a) == Lisp_Fwd_Bool;
46 static bool
47 INTFWDP (union Lisp_Fwd *a)
49 return XFWDTYPE (a) == Lisp_Fwd_Int;
51 static bool
52 KBOARD_OBJFWDP (union Lisp_Fwd *a)
54 return XFWDTYPE (a) == Lisp_Fwd_Kboard_Obj;
56 static bool
57 OBJFWDP (union Lisp_Fwd *a)
59 return XFWDTYPE (a) == Lisp_Fwd_Obj;
62 static struct Lisp_Boolfwd *
63 XBOOLFWD (union Lisp_Fwd *a)
65 eassert (BOOLFWDP (a));
66 return &a->u_boolfwd;
68 static struct Lisp_Kboard_Objfwd *
69 XKBOARD_OBJFWD (union Lisp_Fwd *a)
71 eassert (KBOARD_OBJFWDP (a));
72 return &a->u_kboard_objfwd;
74 static struct Lisp_Intfwd *
75 XINTFWD (union Lisp_Fwd *a)
77 eassert (INTFWDP (a));
78 return &a->u_intfwd;
80 static struct Lisp_Objfwd *
81 XOBJFWD (union Lisp_Fwd *a)
83 eassert (OBJFWDP (a));
84 return &a->u_objfwd;
87 static void
88 CHECK_SUBR (Lisp_Object x)
90 CHECK_TYPE (SUBRP (x), Qsubrp, x);
93 static void
94 set_blv_found (struct Lisp_Buffer_Local_Value *blv, int found)
96 eassert (found == !EQ (blv->defcell, blv->valcell));
97 blv->found = found;
100 static Lisp_Object
101 blv_value (struct Lisp_Buffer_Local_Value *blv)
103 return XCDR (blv->valcell);
106 static void
107 set_blv_value (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
109 XSETCDR (blv->valcell, val);
112 static void
113 set_blv_where (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
115 blv->where = val;
118 static void
119 set_blv_defcell (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
121 blv->defcell = val;
124 static void
125 set_blv_valcell (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
127 blv->valcell = val;
130 static _Noreturn void
131 wrong_length_argument (Lisp_Object a1, Lisp_Object a2, Lisp_Object a3)
133 Lisp_Object size1 = make_number (bool_vector_size (a1));
134 Lisp_Object size2 = make_number (bool_vector_size (a2));
135 if (NILP (a3))
136 xsignal2 (Qwrong_length_argument, size1, size2);
137 else
138 xsignal3 (Qwrong_length_argument, size1, size2,
139 make_number (bool_vector_size (a3)));
142 _Noreturn void
143 wrong_type_argument (register Lisp_Object predicate, register Lisp_Object value)
145 /* If VALUE is not even a valid Lisp object, we'd want to abort here
146 where we can get a backtrace showing where it came from. We used
147 to try and do that by checking the tagbits, but nowadays all
148 tagbits are potentially valid. */
149 /* if ((unsigned int) XTYPE (value) >= Lisp_Type_Limit)
150 * emacs_abort (); */
152 xsignal2 (Qwrong_type_argument, predicate, value);
155 void
156 pure_write_error (Lisp_Object obj)
158 xsignal2 (Qerror, build_string ("Attempt to modify read-only object"), obj);
161 void
162 args_out_of_range (Lisp_Object a1, Lisp_Object a2)
164 xsignal2 (Qargs_out_of_range, a1, a2);
167 void
168 args_out_of_range_3 (Lisp_Object a1, Lisp_Object a2, Lisp_Object a3)
170 xsignal3 (Qargs_out_of_range, a1, a2, a3);
173 void
174 circular_list (Lisp_Object list)
176 xsignal1 (Qcircular_list, list);
180 /* Data type predicates. */
182 DEFUN ("eq", Feq, Seq, 2, 2, 0,
183 doc: /* Return t if the two args are the same Lisp object. */
184 attributes: const)
185 (Lisp_Object obj1, Lisp_Object obj2)
187 if (EQ (obj1, obj2))
188 return Qt;
189 return Qnil;
192 DEFUN ("null", Fnull, Snull, 1, 1, 0,
193 doc: /* Return t if OBJECT is nil, and return nil otherwise. */
194 attributes: const)
195 (Lisp_Object object)
197 if (NILP (object))
198 return Qt;
199 return Qnil;
202 DEFUN ("type-of", Ftype_of, Stype_of, 1, 1, 0,
203 doc: /* Return a symbol representing the type of OBJECT.
204 The symbol returned names the object's basic type;
205 for example, (type-of 1) returns `integer'. */)
206 (Lisp_Object object)
208 switch (XTYPE (object))
210 case_Lisp_Int:
211 return Qinteger;
213 case Lisp_Symbol:
214 return Qsymbol;
216 case Lisp_String:
217 return Qstring;
219 case Lisp_Cons:
220 return Qcons;
222 case Lisp_Misc:
223 switch (XMISCTYPE (object))
225 case Lisp_Misc_Marker:
226 return Qmarker;
227 case Lisp_Misc_Overlay:
228 return Qoverlay;
229 case Lisp_Misc_Float:
230 return Qfloat;
231 case Lisp_Misc_Finalizer:
232 return Qfinalizer;
233 #ifdef HAVE_MODULES
234 case Lisp_Misc_User_Ptr:
235 return Quser_ptr;
236 #endif
237 default:
238 emacs_abort ();
241 case Lisp_Vectorlike:
242 if (WINDOW_CONFIGURATIONP (object))
243 return Qwindow_configuration;
244 if (PROCESSP (object))
245 return Qprocess;
246 if (WINDOWP (object))
247 return Qwindow;
248 if (SUBRP (object))
249 return Qsubr;
250 if (COMPILEDP (object))
251 return Qcompiled_function;
252 if (BUFFERP (object))
253 return Qbuffer;
254 if (CHAR_TABLE_P (object))
255 return Qchar_table;
256 if (BOOL_VECTOR_P (object))
257 return Qbool_vector;
258 if (FRAMEP (object))
259 return Qframe;
260 if (HASH_TABLE_P (object))
261 return Qhash_table;
262 if (FONT_SPEC_P (object))
263 return Qfont_spec;
264 if (FONT_ENTITY_P (object))
265 return Qfont_entity;
266 if (FONT_OBJECT_P (object))
267 return Qfont_object;
268 if (THREADP (object))
269 return Qthread;
270 if (MUTEXP (object))
271 return Qmutex;
272 if (CONDVARP (object))
273 return Qcondition_variable;
274 return Qvector;
276 case Lisp_Float:
277 return Qfloat;
279 default:
280 emacs_abort ();
284 DEFUN ("consp", Fconsp, Sconsp, 1, 1, 0,
285 doc: /* Return t if OBJECT is a cons cell. */
286 attributes: const)
287 (Lisp_Object object)
289 if (CONSP (object))
290 return Qt;
291 return Qnil;
294 DEFUN ("atom", Fatom, Satom, 1, 1, 0,
295 doc: /* Return t if OBJECT is not a cons cell. This includes nil. */
296 attributes: const)
297 (Lisp_Object object)
299 if (CONSP (object))
300 return Qnil;
301 return Qt;
304 DEFUN ("listp", Flistp, Slistp, 1, 1, 0,
305 doc: /* Return t if OBJECT is a list, that is, a cons cell or nil.
306 Otherwise, return nil. */
307 attributes: const)
308 (Lisp_Object object)
310 if (CONSP (object) || NILP (object))
311 return Qt;
312 return Qnil;
315 DEFUN ("nlistp", Fnlistp, Snlistp, 1, 1, 0,
316 doc: /* Return t if OBJECT is not a list. Lists include nil. */
317 attributes: const)
318 (Lisp_Object object)
320 if (CONSP (object) || NILP (object))
321 return Qnil;
322 return Qt;
325 DEFUN ("symbolp", Fsymbolp, Ssymbolp, 1, 1, 0,
326 doc: /* Return t if OBJECT is a symbol. */
327 attributes: const)
328 (Lisp_Object object)
330 if (SYMBOLP (object))
331 return Qt;
332 return Qnil;
335 /* Define this in C to avoid unnecessarily consing up the symbol
336 name. */
337 DEFUN ("keywordp", Fkeywordp, Skeywordp, 1, 1, 0,
338 doc: /* Return t if OBJECT is a keyword.
339 This means that it is a symbol with a print name beginning with `:'
340 interned in the initial obarray. */)
341 (Lisp_Object object)
343 if (SYMBOLP (object)
344 && SREF (SYMBOL_NAME (object), 0) == ':'
345 && SYMBOL_INTERNED_IN_INITIAL_OBARRAY_P (object))
346 return Qt;
347 return Qnil;
350 DEFUN ("vectorp", Fvectorp, Svectorp, 1, 1, 0,
351 doc: /* Return t if OBJECT is a vector. */)
352 (Lisp_Object object)
354 if (VECTORP (object))
355 return Qt;
356 return Qnil;
359 DEFUN ("stringp", Fstringp, Sstringp, 1, 1, 0,
360 doc: /* Return t if OBJECT is a string. */
361 attributes: const)
362 (Lisp_Object object)
364 if (STRINGP (object))
365 return Qt;
366 return Qnil;
369 DEFUN ("multibyte-string-p", Fmultibyte_string_p, Smultibyte_string_p,
370 1, 1, 0,
371 doc: /* Return t if OBJECT is a multibyte string.
372 Return nil if OBJECT is either a unibyte string, or not a string. */)
373 (Lisp_Object object)
375 if (STRINGP (object) && STRING_MULTIBYTE (object))
376 return Qt;
377 return Qnil;
380 DEFUN ("char-table-p", Fchar_table_p, Schar_table_p, 1, 1, 0,
381 doc: /* Return t if OBJECT is a char-table. */)
382 (Lisp_Object object)
384 if (CHAR_TABLE_P (object))
385 return Qt;
386 return Qnil;
389 DEFUN ("vector-or-char-table-p", Fvector_or_char_table_p,
390 Svector_or_char_table_p, 1, 1, 0,
391 doc: /* Return t if OBJECT is a char-table or vector. */)
392 (Lisp_Object object)
394 if (VECTORP (object) || CHAR_TABLE_P (object))
395 return Qt;
396 return Qnil;
399 DEFUN ("bool-vector-p", Fbool_vector_p, Sbool_vector_p, 1, 1, 0,
400 doc: /* Return t if OBJECT is a bool-vector. */)
401 (Lisp_Object object)
403 if (BOOL_VECTOR_P (object))
404 return Qt;
405 return Qnil;
408 DEFUN ("arrayp", Farrayp, Sarrayp, 1, 1, 0,
409 doc: /* Return t if OBJECT is an array (string or vector). */)
410 (Lisp_Object object)
412 if (ARRAYP (object))
413 return Qt;
414 return Qnil;
417 DEFUN ("sequencep", Fsequencep, Ssequencep, 1, 1, 0,
418 doc: /* Return t if OBJECT is a sequence (list or array). */)
419 (register Lisp_Object object)
421 if (CONSP (object) || NILP (object) || ARRAYP (object))
422 return Qt;
423 return Qnil;
426 DEFUN ("bufferp", Fbufferp, Sbufferp, 1, 1, 0,
427 doc: /* Return t if OBJECT is an editor buffer. */)
428 (Lisp_Object object)
430 if (BUFFERP (object))
431 return Qt;
432 return Qnil;
435 DEFUN ("markerp", Fmarkerp, Smarkerp, 1, 1, 0,
436 doc: /* Return t if OBJECT is a marker (editor pointer). */)
437 (Lisp_Object object)
439 if (MARKERP (object))
440 return Qt;
441 return Qnil;
444 #ifdef HAVE_MODULES
445 DEFUN ("user-ptrp", Fuser_ptrp, Suser_ptrp, 1, 1, 0,
446 doc: /* Return t if OBJECT is a module user pointer. */)
447 (Lisp_Object object)
449 if (USER_PTRP (object))
450 return Qt;
451 return Qnil;
453 #endif
455 DEFUN ("subrp", Fsubrp, Ssubrp, 1, 1, 0,
456 doc: /* Return t if OBJECT is a built-in function. */)
457 (Lisp_Object object)
459 if (SUBRP (object))
460 return Qt;
461 return Qnil;
464 DEFUN ("byte-code-function-p", Fbyte_code_function_p, Sbyte_code_function_p,
465 1, 1, 0,
466 doc: /* Return t if OBJECT is a byte-compiled function object. */)
467 (Lisp_Object object)
469 if (COMPILEDP (object))
470 return Qt;
471 return Qnil;
474 DEFUN ("char-or-string-p", Fchar_or_string_p, Schar_or_string_p, 1, 1, 0,
475 doc: /* Return t if OBJECT is a character or a string. */
476 attributes: const)
477 (register Lisp_Object object)
479 if (CHARACTERP (object) || STRINGP (object))
480 return Qt;
481 return Qnil;
484 DEFUN ("integerp", Fintegerp, Sintegerp, 1, 1, 0,
485 doc: /* Return t if OBJECT is an integer. */
486 attributes: const)
487 (Lisp_Object object)
489 if (INTEGERP (object))
490 return Qt;
491 return Qnil;
494 DEFUN ("integer-or-marker-p", Finteger_or_marker_p, Sinteger_or_marker_p, 1, 1, 0,
495 doc: /* Return t if OBJECT is an integer or a marker (editor pointer). */)
496 (register Lisp_Object object)
498 if (MARKERP (object) || INTEGERP (object))
499 return Qt;
500 return Qnil;
503 DEFUN ("natnump", Fnatnump, Snatnump, 1, 1, 0,
504 doc: /* Return t if OBJECT is a nonnegative integer. */
505 attributes: const)
506 (Lisp_Object object)
508 if (NATNUMP (object))
509 return Qt;
510 return Qnil;
513 DEFUN ("numberp", Fnumberp, Snumberp, 1, 1, 0,
514 doc: /* Return t if OBJECT is a number (floating point or integer). */
515 attributes: const)
516 (Lisp_Object object)
518 if (NUMBERP (object))
519 return Qt;
520 else
521 return Qnil;
524 DEFUN ("number-or-marker-p", Fnumber_or_marker_p,
525 Snumber_or_marker_p, 1, 1, 0,
526 doc: /* Return t if OBJECT is a number or a marker. */)
527 (Lisp_Object object)
529 if (NUMBERP (object) || MARKERP (object))
530 return Qt;
531 return Qnil;
534 DEFUN ("floatp", Ffloatp, Sfloatp, 1, 1, 0,
535 doc: /* Return t if OBJECT is a floating point number. */
536 attributes: const)
537 (Lisp_Object object)
539 if (FLOATP (object))
540 return Qt;
541 return Qnil;
544 DEFUN ("threadp", Fthreadp, Sthreadp, 1, 1, 0,
545 doc: /* Return t if OBJECT is a thread. */)
546 (Lisp_Object object)
548 if (THREADP (object))
549 return Qt;
550 return Qnil;
553 DEFUN ("mutexp", Fmutexp, Smutexp, 1, 1, 0,
554 doc: /* Return t if OBJECT is a mutex. */)
555 (Lisp_Object object)
557 if (MUTEXP (object))
558 return Qt;
559 return Qnil;
562 DEFUN ("condition-variable-p", Fcondition_variable_p, Scondition_variable_p,
563 1, 1, 0,
564 doc: /* Return t if OBJECT is a condition variable. */)
565 (Lisp_Object object)
567 if (CONDVARP (object))
568 return Qt;
569 return Qnil;
572 /* Extract and set components of lists. */
574 DEFUN ("car", Fcar, Scar, 1, 1, 0,
575 doc: /* Return the car of LIST. If arg is nil, return nil.
576 Error if arg is not nil and not a cons cell. See also `car-safe'.
578 See Info node `(elisp)Cons Cells' for a discussion of related basic
579 Lisp concepts such as car, cdr, cons cell and list. */)
580 (register Lisp_Object list)
582 return CAR (list);
585 DEFUN ("car-safe", Fcar_safe, Scar_safe, 1, 1, 0,
586 doc: /* Return the car of OBJECT if it is a cons cell, or else nil. */)
587 (Lisp_Object object)
589 return CAR_SAFE (object);
592 DEFUN ("cdr", Fcdr, Scdr, 1, 1, 0,
593 doc: /* Return the cdr of LIST. If arg is nil, return nil.
594 Error if arg is not nil and not a cons cell. See also `cdr-safe'.
596 See Info node `(elisp)Cons Cells' for a discussion of related basic
597 Lisp concepts such as cdr, car, cons cell and list. */)
598 (register Lisp_Object list)
600 return CDR (list);
603 DEFUN ("cdr-safe", Fcdr_safe, Scdr_safe, 1, 1, 0,
604 doc: /* Return the cdr of OBJECT if it is a cons cell, or else nil. */)
605 (Lisp_Object object)
607 return CDR_SAFE (object);
610 DEFUN ("setcar", Fsetcar, Ssetcar, 2, 2, 0,
611 doc: /* Set the car of CELL to be NEWCAR. Returns NEWCAR. */)
612 (register Lisp_Object cell, Lisp_Object newcar)
614 CHECK_CONS (cell);
615 CHECK_IMPURE (cell, XCONS (cell));
616 XSETCAR (cell, newcar);
617 return newcar;
620 DEFUN ("setcdr", Fsetcdr, Ssetcdr, 2, 2, 0,
621 doc: /* Set the cdr of CELL to be NEWCDR. Returns NEWCDR. */)
622 (register Lisp_Object cell, Lisp_Object newcdr)
624 CHECK_CONS (cell);
625 CHECK_IMPURE (cell, XCONS (cell));
626 XSETCDR (cell, newcdr);
627 return newcdr;
630 /* Extract and set components of symbols. */
632 DEFUN ("boundp", Fboundp, Sboundp, 1, 1, 0,
633 doc: /* Return t if SYMBOL's value is not void.
634 Note that if `lexical-binding' is in effect, this refers to the
635 global value outside of any lexical scope. */)
636 (register Lisp_Object symbol)
638 Lisp_Object valcontents;
639 struct Lisp_Symbol *sym;
640 CHECK_SYMBOL (symbol);
641 sym = XSYMBOL (symbol);
643 start:
644 switch (sym->redirect)
646 case SYMBOL_PLAINVAL: valcontents = SYMBOL_VAL (sym); break;
647 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
648 case SYMBOL_LOCALIZED:
650 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
651 if (blv->fwd)
652 /* In set_internal, we un-forward vars when their value is
653 set to Qunbound. */
654 return Qt;
655 else
657 swap_in_symval_forwarding (sym, blv);
658 valcontents = blv_value (blv);
660 break;
662 case SYMBOL_FORWARDED:
663 /* In set_internal, we un-forward vars when their value is
664 set to Qunbound. */
665 return Qt;
666 default: emacs_abort ();
669 return (EQ (valcontents, Qunbound) ? Qnil : Qt);
672 /* FIXME: It has been previously suggested to make this function an
673 alias for symbol-function, but upon discussion at Bug#23957,
674 there is a risk breaking backward compatibility, as some users of
675 fboundp may expect `t' in particular, rather than any true
676 value. An alias is still welcome so long as the compatibility
677 issues are addressed. */
678 DEFUN ("fboundp", Ffboundp, Sfboundp, 1, 1, 0,
679 doc: /* Return t if SYMBOL's function definition is not void. */)
680 (register Lisp_Object symbol)
682 CHECK_SYMBOL (symbol);
683 return NILP (XSYMBOL (symbol)->function) ? Qnil : Qt;
686 DEFUN ("makunbound", Fmakunbound, Smakunbound, 1, 1, 0,
687 doc: /* Make SYMBOL's value be void.
688 Return SYMBOL. */)
689 (register Lisp_Object symbol)
691 CHECK_SYMBOL (symbol);
692 if (SYMBOL_CONSTANT_P (symbol))
693 xsignal1 (Qsetting_constant, symbol);
694 Fset (symbol, Qunbound);
695 return symbol;
698 DEFUN ("fmakunbound", Ffmakunbound, Sfmakunbound, 1, 1, 0,
699 doc: /* Make SYMBOL's function definition be nil.
700 Return SYMBOL. */)
701 (register Lisp_Object symbol)
703 CHECK_SYMBOL (symbol);
704 if (NILP (symbol) || EQ (symbol, Qt))
705 xsignal1 (Qsetting_constant, symbol);
706 set_symbol_function (symbol, Qnil);
707 return symbol;
710 DEFUN ("symbol-function", Fsymbol_function, Ssymbol_function, 1, 1, 0,
711 doc: /* Return SYMBOL's function definition, or nil if that is void. */)
712 (register Lisp_Object symbol)
714 CHECK_SYMBOL (symbol);
715 return XSYMBOL (symbol)->function;
718 DEFUN ("symbol-plist", Fsymbol_plist, Ssymbol_plist, 1, 1, 0,
719 doc: /* Return SYMBOL's property list. */)
720 (register Lisp_Object symbol)
722 CHECK_SYMBOL (symbol);
723 return XSYMBOL (symbol)->plist;
726 DEFUN ("symbol-name", Fsymbol_name, Ssymbol_name, 1, 1, 0,
727 doc: /* Return SYMBOL's name, a string. */)
728 (register Lisp_Object symbol)
730 register Lisp_Object name;
732 CHECK_SYMBOL (symbol);
733 name = SYMBOL_NAME (symbol);
734 return name;
737 DEFUN ("fset", Ffset, Sfset, 2, 2, 0,
738 doc: /* Set SYMBOL's function definition to DEFINITION, and return DEFINITION. */)
739 (register Lisp_Object symbol, Lisp_Object definition)
741 register Lisp_Object function;
742 CHECK_SYMBOL (symbol);
743 /* Perhaps not quite the right error signal, but seems good enough. */
744 if (NILP (symbol))
745 xsignal1 (Qsetting_constant, symbol);
747 function = XSYMBOL (symbol)->function;
749 if (!NILP (Vautoload_queue) && !NILP (function))
750 Vautoload_queue = Fcons (Fcons (symbol, function), Vautoload_queue);
752 if (AUTOLOADP (function))
753 Fput (symbol, Qautoload, XCDR (function));
755 /* Convert to eassert or remove after GC bug is found. In the
756 meantime, check unconditionally, at a slight perf hit. */
757 if (! valid_lisp_object_p (definition))
758 emacs_abort ();
760 set_symbol_function (symbol, definition);
762 return definition;
765 DEFUN ("defalias", Fdefalias, Sdefalias, 2, 3, 0,
766 doc: /* Set SYMBOL's function definition to DEFINITION.
767 Associates the function with the current load file, if any.
768 The optional third argument DOCSTRING specifies the documentation string
769 for SYMBOL; if it is omitted or nil, SYMBOL uses the documentation string
770 determined by DEFINITION.
772 Internally, this normally uses `fset', but if SYMBOL has a
773 `defalias-fset-function' property, the associated value is used instead.
775 The return value is undefined. */)
776 (register Lisp_Object symbol, Lisp_Object definition, Lisp_Object docstring)
778 CHECK_SYMBOL (symbol);
779 if (!NILP (Vpurify_flag)
780 /* If `definition' is a keymap, immutable (and copying) is wrong. */
781 && !KEYMAPP (definition))
782 definition = Fpurecopy (definition);
785 bool autoload = AUTOLOADP (definition);
786 if (NILP (Vpurify_flag) || !autoload)
787 { /* Only add autoload entries after dumping, because the ones before are
788 not useful and else we get loads of them from the loaddefs.el. */
790 if (AUTOLOADP (XSYMBOL (symbol)->function))
791 /* Remember that the function was already an autoload. */
792 LOADHIST_ATTACH (Fcons (Qt, symbol));
793 LOADHIST_ATTACH (Fcons (autoload ? Qautoload : Qdefun, symbol));
797 { /* Handle automatic advice activation. */
798 Lisp_Object hook = Fget (symbol, Qdefalias_fset_function);
799 if (!NILP (hook))
800 call2 (hook, symbol, definition);
801 else
802 Ffset (symbol, definition);
805 if (!NILP (docstring))
806 Fput (symbol, Qfunction_documentation, docstring);
807 /* We used to return `definition', but now that `defun' and `defmacro' expand
808 to a call to `defalias', we return `symbol' for backward compatibility
809 (bug#11686). */
810 return symbol;
813 DEFUN ("setplist", Fsetplist, Ssetplist, 2, 2, 0,
814 doc: /* Set SYMBOL's property list to NEWPLIST, and return NEWPLIST. */)
815 (register Lisp_Object symbol, Lisp_Object newplist)
817 CHECK_SYMBOL (symbol);
818 set_symbol_plist (symbol, newplist);
819 return newplist;
822 DEFUN ("subr-arity", Fsubr_arity, Ssubr_arity, 1, 1, 0,
823 doc: /* Return minimum and maximum number of args allowed for SUBR.
824 SUBR must be a built-in function.
825 The returned value is a pair (MIN . MAX). MIN is the minimum number
826 of args. MAX is the maximum number or the symbol `many', for a
827 function with `&rest' args, or `unevalled' for a special form. */)
828 (Lisp_Object subr)
830 short minargs, maxargs;
831 CHECK_SUBR (subr);
832 minargs = XSUBR (subr)->min_args;
833 maxargs = XSUBR (subr)->max_args;
834 return Fcons (make_number (minargs),
835 maxargs == MANY ? Qmany
836 : maxargs == UNEVALLED ? Qunevalled
837 : make_number (maxargs));
840 DEFUN ("subr-name", Fsubr_name, Ssubr_name, 1, 1, 0,
841 doc: /* Return name of subroutine SUBR.
842 SUBR must be a built-in function. */)
843 (Lisp_Object subr)
845 const char *name;
846 CHECK_SUBR (subr);
847 name = XSUBR (subr)->symbol_name;
848 return build_string (name);
851 DEFUN ("interactive-form", Finteractive_form, Sinteractive_form, 1, 1, 0,
852 doc: /* Return the interactive form of CMD or nil if none.
853 If CMD is not a command, the return value is nil.
854 Value, if non-nil, is a list (interactive SPEC). */)
855 (Lisp_Object cmd)
857 Lisp_Object fun = indirect_function (cmd); /* Check cycles. */
859 if (NILP (fun))
860 return Qnil;
862 /* Use an `interactive-form' property if present, analogous to the
863 function-documentation property. */
864 fun = cmd;
865 while (SYMBOLP (fun))
867 Lisp_Object tmp = Fget (fun, Qinteractive_form);
868 if (!NILP (tmp))
869 return tmp;
870 else
871 fun = Fsymbol_function (fun);
874 if (SUBRP (fun))
876 const char *spec = XSUBR (fun)->intspec;
877 if (spec)
878 return list2 (Qinteractive,
879 (*spec != '(') ? build_string (spec) :
880 Fcar (Fread_from_string (build_string (spec), Qnil, Qnil)));
882 else if (COMPILEDP (fun))
884 if ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE)
885 return list2 (Qinteractive, AREF (fun, COMPILED_INTERACTIVE));
887 else if (AUTOLOADP (fun))
888 return Finteractive_form (Fautoload_do_load (fun, cmd, Qnil));
889 else if (CONSP (fun))
891 Lisp_Object funcar = XCAR (fun);
892 if (EQ (funcar, Qclosure))
893 return Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun))));
894 else if (EQ (funcar, Qlambda))
895 return Fassq (Qinteractive, Fcdr (XCDR (fun)));
897 return Qnil;
901 /***********************************************************************
902 Getting and Setting Values of Symbols
903 ***********************************************************************/
905 /* Return the symbol holding SYMBOL's value. Signal
906 `cyclic-variable-indirection' if SYMBOL's chain of variable
907 indirections contains a loop. */
909 struct Lisp_Symbol *
910 indirect_variable (struct Lisp_Symbol *symbol)
912 struct Lisp_Symbol *tortoise, *hare;
914 hare = tortoise = symbol;
916 while (hare->redirect == SYMBOL_VARALIAS)
918 hare = SYMBOL_ALIAS (hare);
919 if (hare->redirect != SYMBOL_VARALIAS)
920 break;
922 hare = SYMBOL_ALIAS (hare);
923 tortoise = SYMBOL_ALIAS (tortoise);
925 if (hare == tortoise)
927 Lisp_Object tem;
928 XSETSYMBOL (tem, symbol);
929 xsignal1 (Qcyclic_variable_indirection, tem);
933 return hare;
937 DEFUN ("indirect-variable", Findirect_variable, Sindirect_variable, 1, 1, 0,
938 doc: /* Return the variable at the end of OBJECT's variable chain.
939 If OBJECT is a symbol, follow its variable indirections (if any), and
940 return the variable at the end of the chain of aliases. See Info node
941 `(elisp)Variable Aliases'.
943 If OBJECT is not a symbol, just return it. If there is a loop in the
944 chain of aliases, signal a `cyclic-variable-indirection' error. */)
945 (Lisp_Object object)
947 if (SYMBOLP (object))
949 struct Lisp_Symbol *sym = indirect_variable (XSYMBOL (object));
950 XSETSYMBOL (object, sym);
952 return object;
956 /* Given the raw contents of a symbol value cell,
957 return the Lisp value of the symbol.
958 This does not handle buffer-local variables; use
959 swap_in_symval_forwarding for that. */
961 Lisp_Object
962 do_symval_forwarding (register union Lisp_Fwd *valcontents)
964 register Lisp_Object val;
965 switch (XFWDTYPE (valcontents))
967 case Lisp_Fwd_Int:
968 XSETINT (val, *XINTFWD (valcontents)->intvar);
969 return val;
971 case Lisp_Fwd_Bool:
972 return (*XBOOLFWD (valcontents)->boolvar ? Qt : Qnil);
974 case Lisp_Fwd_Obj:
975 return *XOBJFWD (valcontents)->objvar;
977 case Lisp_Fwd_Buffer_Obj:
978 return per_buffer_value (current_buffer,
979 XBUFFER_OBJFWD (valcontents)->offset);
981 case Lisp_Fwd_Kboard_Obj:
982 /* We used to simply use current_kboard here, but from Lisp
983 code, its value is often unexpected. It seems nicer to
984 allow constructions like this to work as intuitively expected:
986 (with-selected-frame frame
987 (define-key local-function-map "\eOP" [f1]))
989 On the other hand, this affects the semantics of
990 last-command and real-last-command, and people may rely on
991 that. I took a quick look at the Lisp codebase, and I
992 don't think anything will break. --lorentey */
993 return *(Lisp_Object *)(XKBOARD_OBJFWD (valcontents)->offset
994 + (char *)FRAME_KBOARD (SELECTED_FRAME ()));
995 default: emacs_abort ();
999 /* Used to signal a user-friendly error when symbol WRONG is
1000 not a member of CHOICE, which should be a list of symbols. */
1002 void
1003 wrong_choice (Lisp_Object choice, Lisp_Object wrong)
1005 ptrdiff_t i = 0, len = XINT (Flength (choice));
1006 Lisp_Object obj, *args;
1007 AUTO_STRING (one_of, "One of ");
1008 AUTO_STRING (comma, ", ");
1009 AUTO_STRING (or, " or ");
1010 AUTO_STRING (should_be_specified, " should be specified");
1012 USE_SAFE_ALLOCA;
1013 SAFE_ALLOCA_LISP (args, len * 2 + 1);
1015 args[i++] = one_of;
1017 for (obj = choice; !NILP (obj); obj = XCDR (obj))
1019 args[i++] = SYMBOL_NAME (XCAR (obj));
1020 args[i++] = (NILP (XCDR (obj)) ? should_be_specified
1021 : NILP (XCDR (XCDR (obj))) ? or : comma);
1024 obj = Fconcat (i, args);
1025 SAFE_FREE ();
1026 xsignal2 (Qerror, obj, wrong);
1029 /* Used to signal a user-friendly error if WRONG is not a number or
1030 integer/floating-point number outsize of inclusive MIN..MAX range. */
1032 static void
1033 wrong_range (Lisp_Object min, Lisp_Object max, Lisp_Object wrong)
1035 AUTO_STRING (value_should_be_from, "Value should be from ");
1036 AUTO_STRING (to, " to ");
1037 xsignal2 (Qerror,
1038 CALLN (Fconcat, value_should_be_from, Fnumber_to_string (min),
1039 to, Fnumber_to_string (max)),
1040 wrong);
1043 /* Store NEWVAL into SYMBOL, where VALCONTENTS is found in the value cell
1044 of SYMBOL. If SYMBOL is buffer-local, VALCONTENTS should be the
1045 buffer-independent contents of the value cell: forwarded just one
1046 step past the buffer-localness.
1048 BUF non-zero means set the value in buffer BUF instead of the
1049 current buffer. This only plays a role for per-buffer variables. */
1051 static void
1052 store_symval_forwarding (union Lisp_Fwd *valcontents, register Lisp_Object newval, struct buffer *buf)
1054 switch (XFWDTYPE (valcontents))
1056 case Lisp_Fwd_Int:
1057 CHECK_NUMBER (newval);
1058 *XINTFWD (valcontents)->intvar = XINT (newval);
1059 break;
1061 case Lisp_Fwd_Bool:
1062 *XBOOLFWD (valcontents)->boolvar = !NILP (newval);
1063 break;
1065 case Lisp_Fwd_Obj:
1066 *XOBJFWD (valcontents)->objvar = newval;
1068 /* If this variable is a default for something stored
1069 in the buffer itself, such as default-fill-column,
1070 find the buffers that don't have local values for it
1071 and update them. */
1072 if (XOBJFWD (valcontents)->objvar > (Lisp_Object *) &buffer_defaults
1073 && XOBJFWD (valcontents)->objvar < (Lisp_Object *) (&buffer_defaults + 1))
1075 int offset = ((char *) XOBJFWD (valcontents)->objvar
1076 - (char *) &buffer_defaults);
1077 int idx = PER_BUFFER_IDX (offset);
1079 Lisp_Object tail, buf;
1081 if (idx <= 0)
1082 break;
1084 FOR_EACH_LIVE_BUFFER (tail, buf)
1086 struct buffer *b = XBUFFER (buf);
1088 if (! PER_BUFFER_VALUE_P (b, idx))
1089 set_per_buffer_value (b, offset, newval);
1092 break;
1094 case Lisp_Fwd_Buffer_Obj:
1096 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1097 Lisp_Object predicate = XBUFFER_OBJFWD (valcontents)->predicate;
1099 if (!NILP (newval))
1101 if (SYMBOLP (predicate))
1103 Lisp_Object prop;
1105 if ((prop = Fget (predicate, Qchoice), !NILP (prop)))
1107 if (NILP (Fmemq (newval, prop)))
1108 wrong_choice (prop, newval);
1110 else if ((prop = Fget (predicate, Qrange), !NILP (prop)))
1112 Lisp_Object min = XCAR (prop), max = XCDR (prop);
1114 if (!NUMBERP (newval)
1115 || !NILP (arithcompare (newval, min, ARITH_LESS))
1116 || !NILP (arithcompare (newval, max, ARITH_GRTR)))
1117 wrong_range (min, max, newval);
1119 else if (FUNCTIONP (predicate))
1121 if (NILP (call1 (predicate, newval)))
1122 wrong_type_argument (predicate, newval);
1126 if (buf == NULL)
1127 buf = current_buffer;
1128 set_per_buffer_value (buf, offset, newval);
1130 break;
1132 case Lisp_Fwd_Kboard_Obj:
1134 char *base = (char *) FRAME_KBOARD (SELECTED_FRAME ());
1135 char *p = base + XKBOARD_OBJFWD (valcontents)->offset;
1136 *(Lisp_Object *) p = newval;
1138 break;
1140 default:
1141 emacs_abort (); /* goto def; */
1145 /* Set up SYMBOL to refer to its global binding. This makes it safe
1146 to alter the status of other bindings. BEWARE: this may be called
1147 during the mark phase of GC, where we assume that Lisp_Object slots
1148 of BLV are marked after this function has changed them. */
1150 void
1151 swap_in_global_binding (struct Lisp_Symbol *symbol)
1153 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (symbol);
1155 /* Unload the previously loaded binding. */
1156 if (blv->fwd)
1157 set_blv_value (blv, do_symval_forwarding (blv->fwd));
1159 /* Select the global binding in the symbol. */
1160 set_blv_valcell (blv, blv->defcell);
1161 if (blv->fwd)
1162 store_symval_forwarding (blv->fwd, XCDR (blv->defcell), NULL);
1164 /* Indicate that the global binding is set up now. */
1165 set_blv_where (blv, Qnil);
1166 set_blv_found (blv, 0);
1169 /* Set up the buffer-local symbol SYMBOL for validity in the current buffer.
1170 VALCONTENTS is the contents of its value cell,
1171 which points to a struct Lisp_Buffer_Local_Value.
1173 Return the value forwarded one step past the buffer-local stage.
1174 This could be another forwarding pointer. */
1176 static void
1177 swap_in_symval_forwarding (struct Lisp_Symbol *symbol, struct Lisp_Buffer_Local_Value *blv)
1179 register Lisp_Object tem1;
1181 eassert (blv == SYMBOL_BLV (symbol));
1183 tem1 = blv->where;
1185 if (NILP (tem1)
1186 || current_buffer != XBUFFER (tem1))
1189 /* Unload the previously loaded binding. */
1190 tem1 = blv->valcell;
1191 if (blv->fwd)
1192 set_blv_value (blv, do_symval_forwarding (blv->fwd));
1193 /* Choose the new binding. */
1195 Lisp_Object var;
1196 XSETSYMBOL (var, symbol);
1197 tem1 = assq_no_quit (var, BVAR (current_buffer, local_var_alist));
1198 set_blv_where (blv, Fcurrent_buffer ());
1200 if (!(blv->found = !NILP (tem1)))
1201 tem1 = blv->defcell;
1203 /* Load the new binding. */
1204 set_blv_valcell (blv, tem1);
1205 if (blv->fwd)
1206 store_symval_forwarding (blv->fwd, blv_value (blv), NULL);
1210 /* Find the value of a symbol, returning Qunbound if it's not bound.
1211 This is helpful for code which just wants to get a variable's value
1212 if it has one, without signaling an error.
1213 Note that it must not be possible to quit
1214 within this function. Great care is required for this. */
1216 Lisp_Object
1217 find_symbol_value (Lisp_Object symbol)
1219 struct Lisp_Symbol *sym;
1221 CHECK_SYMBOL (symbol);
1222 sym = XSYMBOL (symbol);
1224 start:
1225 switch (sym->redirect)
1227 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1228 case SYMBOL_PLAINVAL: return SYMBOL_VAL (sym);
1229 case SYMBOL_LOCALIZED:
1231 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1232 swap_in_symval_forwarding (sym, blv);
1233 return blv->fwd ? do_symval_forwarding (blv->fwd) : blv_value (blv);
1235 /* FALLTHROUGH */
1236 case SYMBOL_FORWARDED:
1237 return do_symval_forwarding (SYMBOL_FWD (sym));
1238 default: emacs_abort ();
1242 DEFUN ("symbol-value", Fsymbol_value, Ssymbol_value, 1, 1, 0,
1243 doc: /* Return SYMBOL's value. Error if that is void.
1244 Note that if `lexical-binding' is in effect, this returns the
1245 global value outside of any lexical scope. */)
1246 (Lisp_Object symbol)
1248 Lisp_Object val;
1250 val = find_symbol_value (symbol);
1251 if (!EQ (val, Qunbound))
1252 return val;
1254 xsignal1 (Qvoid_variable, symbol);
1257 DEFUN ("set", Fset, Sset, 2, 2, 0,
1258 doc: /* Set SYMBOL's value to NEWVAL, and return NEWVAL. */)
1259 (register Lisp_Object symbol, Lisp_Object newval)
1261 set_internal (symbol, newval, Qnil, SET_INTERNAL_SET);
1262 return newval;
1265 /* Store the value NEWVAL into SYMBOL.
1266 If buffer-locality is an issue, WHERE specifies which context to use.
1267 (nil stands for the current buffer/frame).
1269 If BINDFLAG is SET_INTERNAL_SET, then if this symbol is supposed to
1270 become local in every buffer where it is set, then we make it
1271 local. If BINDFLAG is SET_INTERNAL_BIND or SET_INTERNAL_UNBIND, we
1272 don't do that. */
1274 void
1275 set_internal (Lisp_Object symbol, Lisp_Object newval, Lisp_Object where,
1276 enum Set_Internal_Bind bindflag)
1278 bool voide = EQ (newval, Qunbound);
1279 struct Lisp_Symbol *sym;
1280 Lisp_Object tem1;
1282 /* If restoring in a dead buffer, do nothing. */
1283 /* if (BUFFERP (where) && NILP (XBUFFER (where)->name))
1284 return; */
1286 CHECK_SYMBOL (symbol);
1287 sym = XSYMBOL (symbol);
1288 switch (sym->trapped_write)
1290 case SYMBOL_NOWRITE:
1291 if (NILP (Fkeywordp (symbol))
1292 || !EQ (newval, Fsymbol_value (symbol)))
1293 xsignal1 (Qsetting_constant, symbol);
1294 else
1295 /* Allow setting keywords to their own value. */
1296 return;
1298 case SYMBOL_TRAPPED_WRITE:
1299 /* Setting due to thread-switching doesn't count. */
1300 if (bindflag != SET_INTERNAL_THREAD_SWITCH)
1301 notify_variable_watchers (symbol, voide? Qnil : newval,
1302 (bindflag == SET_INTERNAL_BIND? Qlet :
1303 bindflag == SET_INTERNAL_UNBIND? Qunlet :
1304 voide? Qmakunbound : Qset),
1305 where);
1306 /* FALLTHROUGH! */
1307 case SYMBOL_UNTRAPPED_WRITE:
1308 break;
1310 default: emacs_abort ();
1313 start:
1314 switch (sym->redirect)
1316 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1317 case SYMBOL_PLAINVAL: SET_SYMBOL_VAL (sym , newval); return;
1318 case SYMBOL_LOCALIZED:
1320 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1321 if (NILP (where))
1322 XSETBUFFER (where, current_buffer);
1324 /* If the current buffer is not the buffer whose binding is
1325 loaded, or if it's a Lisp_Buffer_Local_Value and
1326 the default binding is loaded, the loaded binding may be the
1327 wrong one. */
1328 if (!EQ (blv->where, where)
1329 /* Also unload a global binding (if the var is local_if_set). */
1330 || (EQ (blv->valcell, blv->defcell)))
1332 /* The currently loaded binding is not necessarily valid.
1333 We need to unload it, and choose a new binding. */
1335 /* Write out `realvalue' to the old loaded binding. */
1336 if (blv->fwd)
1337 set_blv_value (blv, do_symval_forwarding (blv->fwd));
1339 /* Find the new binding. */
1340 XSETSYMBOL (symbol, sym); /* May have changed via aliasing. */
1341 tem1 = assq_no_quit (symbol,
1342 BVAR (XBUFFER (where), local_var_alist));
1343 set_blv_where (blv, where);
1344 blv->found = 1;
1346 if (NILP (tem1))
1348 /* This buffer still sees the default value. */
1350 /* If the variable is a Lisp_Some_Buffer_Local_Value,
1351 or if this is `let' rather than `set',
1352 make CURRENT-ALIST-ELEMENT point to itself,
1353 indicating that we're seeing the default value.
1354 Likewise if the variable has been let-bound
1355 in the current buffer. */
1356 if (bindflag || !blv->local_if_set
1357 || let_shadows_buffer_binding_p (sym))
1359 blv->found = 0;
1360 tem1 = blv->defcell;
1362 /* If it's a local_if_set, being set not bound,
1363 and we're not within a let that was made for this buffer,
1364 create a new buffer-local binding for the variable.
1365 That means, give this buffer a new assoc for a local value
1366 and load that binding. */
1367 else
1369 tem1 = Fcons (symbol, XCDR (blv->defcell));
1370 bset_local_var_alist
1371 (XBUFFER (where),
1372 Fcons (tem1, BVAR (XBUFFER (where), local_var_alist)));
1376 /* Record which binding is now loaded. */
1377 set_blv_valcell (blv, tem1);
1380 /* Store the new value in the cons cell. */
1381 set_blv_value (blv, newval);
1383 if (blv->fwd)
1385 if (voide)
1386 /* If storing void (making the symbol void), forward only through
1387 buffer-local indicator, not through Lisp_Objfwd, etc. */
1388 blv->fwd = NULL;
1389 else
1390 store_symval_forwarding (blv->fwd, newval,
1391 BUFFERP (where)
1392 ? XBUFFER (where) : current_buffer);
1394 break;
1396 case SYMBOL_FORWARDED:
1398 struct buffer *buf
1399 = BUFFERP (where) ? XBUFFER (where) : current_buffer;
1400 union Lisp_Fwd *innercontents = SYMBOL_FWD (sym);
1401 if (BUFFER_OBJFWDP (innercontents))
1403 int offset = XBUFFER_OBJFWD (innercontents)->offset;
1404 int idx = PER_BUFFER_IDX (offset);
1405 if (idx > 0
1406 && bindflag == SET_INTERNAL_SET
1407 && !let_shadows_buffer_binding_p (sym))
1408 SET_PER_BUFFER_VALUE_P (buf, idx, 1);
1411 if (voide)
1412 { /* If storing void (making the symbol void), forward only through
1413 buffer-local indicator, not through Lisp_Objfwd, etc. */
1414 sym->redirect = SYMBOL_PLAINVAL;
1415 SET_SYMBOL_VAL (sym, newval);
1417 else
1418 store_symval_forwarding (/* sym, */ innercontents, newval, buf);
1419 break;
1421 default: emacs_abort ();
1423 return;
1426 static void
1427 set_symbol_trapped_write (Lisp_Object symbol, enum symbol_trapped_write trap)
1429 struct Lisp_Symbol *sym = XSYMBOL (symbol);
1430 if (sym->trapped_write == SYMBOL_NOWRITE)
1431 xsignal1 (Qtrapping_constant, symbol);
1432 sym->trapped_write = trap;
1435 static void
1436 restore_symbol_trapped_write (Lisp_Object symbol)
1438 set_symbol_trapped_write (symbol, SYMBOL_TRAPPED_WRITE);
1441 static void
1442 harmonize_variable_watchers (Lisp_Object alias, Lisp_Object base_variable)
1444 if (!EQ (base_variable, alias)
1445 && EQ (base_variable, Findirect_variable (alias)))
1446 set_symbol_trapped_write
1447 (alias, XSYMBOL (base_variable)->trapped_write);
1450 DEFUN ("add-variable-watcher", Fadd_variable_watcher, Sadd_variable_watcher,
1451 2, 2, 0,
1452 doc: /* Cause WATCH-FUNCTION to be called when SYMBOL is set.
1454 It will be called with 4 arguments: (SYMBOL NEWVAL OPERATION WHERE).
1455 SYMBOL is the variable being changed.
1456 NEWVAL is the value it will be changed to.
1457 OPERATION is a symbol representing the kind of change, one of: `set',
1458 `let', `unlet', `makunbound', and `defvaralias'.
1459 WHERE is a buffer if the buffer-local value of the variable being
1460 changed, nil otherwise.
1462 All writes to aliases of SYMBOL will call WATCH-FUNCTION too. */)
1463 (Lisp_Object symbol, Lisp_Object watch_function)
1465 symbol = Findirect_variable (symbol);
1466 set_symbol_trapped_write (symbol, SYMBOL_TRAPPED_WRITE);
1467 map_obarray (Vobarray, harmonize_variable_watchers, symbol);
1469 Lisp_Object watchers = Fget (symbol, Qwatchers);
1470 Lisp_Object member = Fmember (watch_function, watchers);
1471 if (NILP (member))
1472 Fput (symbol, Qwatchers, Fcons (watch_function, watchers));
1473 return Qnil;
1476 DEFUN ("remove-variable-watcher", Fremove_variable_watcher, Sremove_variable_watcher,
1477 2, 2, 0,
1478 doc: /* Undo the effect of `add-variable-watcher'.
1479 Remove WATCH-FUNCTION from the list of functions to be called when
1480 SYMBOL (or its aliases) are set. */)
1481 (Lisp_Object symbol, Lisp_Object watch_function)
1483 symbol = Findirect_variable (symbol);
1484 Lisp_Object watchers = Fget (symbol, Qwatchers);
1485 watchers = Fdelete (watch_function, watchers);
1486 if (NILP (watchers))
1488 set_symbol_trapped_write (symbol, SYMBOL_UNTRAPPED_WRITE);
1489 map_obarray (Vobarray, harmonize_variable_watchers, symbol);
1491 Fput (symbol, Qwatchers, watchers);
1492 return Qnil;
1495 DEFUN ("get-variable-watchers", Fget_variable_watchers, Sget_variable_watchers,
1496 1, 1, 0,
1497 doc: /* Return a list of SYMBOL's active watchers. */)
1498 (Lisp_Object symbol)
1500 return (SYMBOL_TRAPPED_WRITE_P (symbol) == SYMBOL_TRAPPED_WRITE)
1501 ? Fget (Findirect_variable (symbol), Qwatchers)
1502 : Qnil;
1505 void
1506 notify_variable_watchers (Lisp_Object symbol,
1507 Lisp_Object newval,
1508 Lisp_Object operation,
1509 Lisp_Object where)
1511 symbol = Findirect_variable (symbol);
1513 ptrdiff_t count = SPECPDL_INDEX ();
1514 record_unwind_protect (restore_symbol_trapped_write, symbol);
1515 /* Avoid recursion. */
1516 set_symbol_trapped_write (symbol, SYMBOL_UNTRAPPED_WRITE);
1518 if (NILP (where)
1519 && !EQ (operation, Qset_default) && !EQ (operation, Qmakunbound)
1520 && !NILP (Flocal_variable_if_set_p (symbol, Fcurrent_buffer ())))
1522 XSETBUFFER (where, current_buffer);
1525 if (EQ (operation, Qset_default))
1526 operation = Qset;
1528 for (Lisp_Object watchers = Fget (symbol, Qwatchers);
1529 CONSP (watchers);
1530 watchers = XCDR (watchers))
1532 Lisp_Object watcher = XCAR (watchers);
1533 /* Call subr directly to avoid gc. */
1534 if (SUBRP (watcher))
1536 Lisp_Object args[] = { symbol, newval, operation, where };
1537 funcall_subr (XSUBR (watcher), ARRAYELTS (args), args);
1539 else
1540 CALLN (Ffuncall, watcher, symbol, newval, operation, where);
1543 unbind_to (count, Qnil);
1547 /* Access or set a buffer-local symbol's default value. */
1549 /* Return the default value of SYMBOL, but don't check for voidness.
1550 Return Qunbound if it is void. */
1552 static Lisp_Object
1553 default_value (Lisp_Object symbol)
1555 struct Lisp_Symbol *sym;
1557 CHECK_SYMBOL (symbol);
1558 sym = XSYMBOL (symbol);
1560 start:
1561 switch (sym->redirect)
1563 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1564 case SYMBOL_PLAINVAL: return SYMBOL_VAL (sym);
1565 case SYMBOL_LOCALIZED:
1567 /* If var is set up for a buffer that lacks a local value for it,
1568 the current value is nominally the default value.
1569 But the `realvalue' slot may be more up to date, since
1570 ordinary setq stores just that slot. So use that. */
1571 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1572 if (blv->fwd && EQ (blv->valcell, blv->defcell))
1573 return do_symval_forwarding (blv->fwd);
1574 else
1575 return XCDR (blv->defcell);
1577 case SYMBOL_FORWARDED:
1579 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
1581 /* For a built-in buffer-local variable, get the default value
1582 rather than letting do_symval_forwarding get the current value. */
1583 if (BUFFER_OBJFWDP (valcontents))
1585 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1586 if (PER_BUFFER_IDX (offset) != 0)
1587 return per_buffer_default (offset);
1590 /* For other variables, get the current value. */
1591 return do_symval_forwarding (valcontents);
1593 default: emacs_abort ();
1597 DEFUN ("default-boundp", Fdefault_boundp, Sdefault_boundp, 1, 1, 0,
1598 doc: /* Return t if SYMBOL has a non-void default value.
1599 This is the value that is seen in buffers that do not have their own values
1600 for this variable. */)
1601 (Lisp_Object symbol)
1603 register Lisp_Object value;
1605 value = default_value (symbol);
1606 return (EQ (value, Qunbound) ? Qnil : Qt);
1609 DEFUN ("default-value", Fdefault_value, Sdefault_value, 1, 1, 0,
1610 doc: /* Return SYMBOL's default value.
1611 This is the value that is seen in buffers that do not have their own values
1612 for this variable. The default value is meaningful for variables with
1613 local bindings in certain buffers. */)
1614 (Lisp_Object symbol)
1616 Lisp_Object value = default_value (symbol);
1617 if (!EQ (value, Qunbound))
1618 return value;
1620 xsignal1 (Qvoid_variable, symbol);
1623 void
1624 set_default_internal (Lisp_Object symbol, Lisp_Object value,
1625 enum Set_Internal_Bind bindflag)
1627 struct Lisp_Symbol *sym;
1629 CHECK_SYMBOL (symbol);
1630 sym = XSYMBOL (symbol);
1631 switch (sym->trapped_write)
1633 case SYMBOL_NOWRITE:
1634 if (NILP (Fkeywordp (symbol))
1635 || !EQ (value, Fsymbol_value (symbol)))
1636 xsignal1 (Qsetting_constant, symbol);
1637 else
1638 /* Allow setting keywords to their own value. */
1639 return;
1641 case SYMBOL_TRAPPED_WRITE:
1642 /* Don't notify here if we're going to call Fset anyway. */
1643 if (sym->redirect != SYMBOL_PLAINVAL
1644 /* Setting due to thread switching doesn't count. */
1645 && bindflag != SET_INTERNAL_THREAD_SWITCH)
1646 notify_variable_watchers (symbol, value, Qset_default, Qnil);
1647 /* FALLTHROUGH! */
1648 case SYMBOL_UNTRAPPED_WRITE:
1649 break;
1651 default: emacs_abort ();
1654 start:
1655 switch (sym->redirect)
1657 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1658 case SYMBOL_PLAINVAL: set_internal (symbol, value, Qnil, bindflag); return;
1659 case SYMBOL_LOCALIZED:
1661 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1663 /* Store new value into the DEFAULT-VALUE slot. */
1664 XSETCDR (blv->defcell, value);
1666 /* If the default binding is now loaded, set the REALVALUE slot too. */
1667 if (blv->fwd && EQ (blv->defcell, blv->valcell))
1668 store_symval_forwarding (blv->fwd, value, NULL);
1669 return;
1671 case SYMBOL_FORWARDED:
1673 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
1675 /* Handle variables like case-fold-search that have special slots
1676 in the buffer.
1677 Make them work apparently like Lisp_Buffer_Local_Value variables. */
1678 if (BUFFER_OBJFWDP (valcontents))
1680 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1681 int idx = PER_BUFFER_IDX (offset);
1683 set_per_buffer_default (offset, value);
1685 /* If this variable is not always local in all buffers,
1686 set it in the buffers that don't nominally have a local value. */
1687 if (idx > 0)
1689 struct buffer *b;
1691 FOR_EACH_BUFFER (b)
1692 if (!PER_BUFFER_VALUE_P (b, idx))
1693 set_per_buffer_value (b, offset, value);
1696 else
1697 set_internal (symbol, value, Qnil, bindflag);
1698 return;
1700 default: emacs_abort ();
1704 DEFUN ("set-default", Fset_default, Sset_default, 2, 2, 0,
1705 doc: /* Set SYMBOL's default value to VALUE. SYMBOL and VALUE are evaluated.
1706 The default value is seen in buffers that do not have their own values
1707 for this variable. */)
1708 (Lisp_Object symbol, Lisp_Object value)
1710 set_default_internal (symbol, value, SET_INTERNAL_SET);
1711 return value;
1714 DEFUN ("setq-default", Fsetq_default, Ssetq_default, 0, UNEVALLED, 0,
1715 doc: /* Set the default value of variable VAR to VALUE.
1716 VAR, the variable name, is literal (not evaluated);
1717 VALUE is an expression: it is evaluated and its value returned.
1718 The default value of a variable is seen in buffers
1719 that do not have their own values for the variable.
1721 More generally, you can use multiple variables and values, as in
1722 (setq-default VAR VALUE VAR VALUE...)
1723 This sets each VAR's default value to the corresponding VALUE.
1724 The VALUE for the Nth VAR can refer to the new default values
1725 of previous VARs.
1726 usage: (setq-default [VAR VALUE]...) */)
1727 (Lisp_Object args)
1729 Lisp_Object args_left, symbol, val;
1731 args_left = val = args;
1733 while (CONSP (args_left))
1735 val = eval_sub (Fcar (XCDR (args_left)));
1736 symbol = XCAR (args_left);
1737 Fset_default (symbol, val);
1738 args_left = Fcdr (XCDR (args_left));
1741 return val;
1744 /* Lisp functions for creating and removing buffer-local variables. */
1746 union Lisp_Val_Fwd
1748 Lisp_Object value;
1749 union Lisp_Fwd *fwd;
1752 static struct Lisp_Buffer_Local_Value *
1753 make_blv (struct Lisp_Symbol *sym, bool forwarded,
1754 union Lisp_Val_Fwd valcontents)
1756 struct Lisp_Buffer_Local_Value *blv = xmalloc (sizeof *blv);
1757 Lisp_Object symbol;
1758 Lisp_Object tem;
1760 XSETSYMBOL (symbol, sym);
1761 tem = Fcons (symbol, (forwarded
1762 ? do_symval_forwarding (valcontents.fwd)
1763 : valcontents.value));
1765 /* Buffer_Local_Values cannot have as realval a buffer-local
1766 or keyboard-local forwarding. */
1767 eassert (!(forwarded && BUFFER_OBJFWDP (valcontents.fwd)));
1768 eassert (!(forwarded && KBOARD_OBJFWDP (valcontents.fwd)));
1769 blv->fwd = forwarded ? valcontents.fwd : NULL;
1770 set_blv_where (blv, Qnil);
1771 blv->local_if_set = 0;
1772 set_blv_defcell (blv, tem);
1773 set_blv_valcell (blv, tem);
1774 set_blv_found (blv, 0);
1775 return blv;
1778 DEFUN ("make-variable-buffer-local", Fmake_variable_buffer_local,
1779 Smake_variable_buffer_local, 1, 1, "vMake Variable Buffer Local: ",
1780 doc: /* Make VARIABLE become buffer-local whenever it is set.
1781 At any time, the value for the current buffer is in effect,
1782 unless the variable has never been set in this buffer,
1783 in which case the default value is in effect.
1784 Note that binding the variable with `let', or setting it while
1785 a `let'-style binding made in this buffer is in effect,
1786 does not make the variable buffer-local. Return VARIABLE.
1788 This globally affects all uses of this variable, so it belongs together with
1789 the variable declaration, rather than with its uses (if you just want to make
1790 a variable local to the current buffer for one particular use, use
1791 `make-local-variable'). Buffer-local bindings are normally cleared
1792 while setting up a new major mode, unless they have a `permanent-local'
1793 property.
1795 The function `default-value' gets the default value and `set-default' sets it. */)
1796 (register Lisp_Object variable)
1798 struct Lisp_Symbol *sym;
1799 struct Lisp_Buffer_Local_Value *blv = NULL;
1800 union Lisp_Val_Fwd valcontents;
1801 bool forwarded;
1803 CHECK_SYMBOL (variable);
1804 sym = XSYMBOL (variable);
1806 start:
1807 switch (sym->redirect)
1809 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1810 case SYMBOL_PLAINVAL:
1811 forwarded = 0; valcontents.value = SYMBOL_VAL (sym);
1812 if (EQ (valcontents.value, Qunbound))
1813 valcontents.value = Qnil;
1814 break;
1815 case SYMBOL_LOCALIZED:
1816 blv = SYMBOL_BLV (sym);
1817 break;
1818 case SYMBOL_FORWARDED:
1819 forwarded = 1; valcontents.fwd = SYMBOL_FWD (sym);
1820 if (KBOARD_OBJFWDP (valcontents.fwd))
1821 error ("Symbol %s may not be buffer-local",
1822 SDATA (SYMBOL_NAME (variable)));
1823 else if (BUFFER_OBJFWDP (valcontents.fwd))
1824 return variable;
1825 break;
1826 default: emacs_abort ();
1829 if (SYMBOL_CONSTANT_P (variable))
1830 error ("Symbol %s may not be buffer-local", SDATA (SYMBOL_NAME (variable)));
1832 if (!blv)
1834 blv = make_blv (sym, forwarded, valcontents);
1835 sym->redirect = SYMBOL_LOCALIZED;
1836 SET_SYMBOL_BLV (sym, blv);
1839 blv->local_if_set = 1;
1840 return variable;
1843 DEFUN ("make-local-variable", Fmake_local_variable, Smake_local_variable,
1844 1, 1, "vMake Local Variable: ",
1845 doc: /* Make VARIABLE have a separate value in the current buffer.
1846 Other buffers will continue to share a common default value.
1847 \(The buffer-local value of VARIABLE starts out as the same value
1848 VARIABLE previously had. If VARIABLE was void, it remains void.)
1849 Return VARIABLE.
1851 If the variable is already arranged to become local when set,
1852 this function causes a local value to exist for this buffer,
1853 just as setting the variable would do.
1855 This function returns VARIABLE, and therefore
1856 (set (make-local-variable \\='VARIABLE) VALUE-EXP)
1857 works.
1859 See also `make-variable-buffer-local'.
1861 Do not use `make-local-variable' to make a hook variable buffer-local.
1862 Instead, use `add-hook' and specify t for the LOCAL argument. */)
1863 (Lisp_Object variable)
1865 Lisp_Object tem;
1866 bool forwarded;
1867 union Lisp_Val_Fwd valcontents;
1868 struct Lisp_Symbol *sym;
1869 struct Lisp_Buffer_Local_Value *blv = NULL;
1871 CHECK_SYMBOL (variable);
1872 sym = XSYMBOL (variable);
1874 start:
1875 switch (sym->redirect)
1877 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1878 case SYMBOL_PLAINVAL:
1879 forwarded = 0; valcontents.value = SYMBOL_VAL (sym); break;
1880 case SYMBOL_LOCALIZED:
1881 blv = SYMBOL_BLV (sym);
1882 break;
1883 case SYMBOL_FORWARDED:
1884 forwarded = 1; valcontents.fwd = SYMBOL_FWD (sym);
1885 if (KBOARD_OBJFWDP (valcontents.fwd))
1886 error ("Symbol %s may not be buffer-local",
1887 SDATA (SYMBOL_NAME (variable)));
1888 break;
1889 default: emacs_abort ();
1892 if (sym->trapped_write == SYMBOL_NOWRITE)
1893 error ("Symbol %s may not be buffer-local",
1894 SDATA (SYMBOL_NAME (variable)));
1896 if (blv ? blv->local_if_set
1897 : (forwarded && BUFFER_OBJFWDP (valcontents.fwd)))
1899 tem = Fboundp (variable);
1900 /* Make sure the symbol has a local value in this particular buffer,
1901 by setting it to the same value it already has. */
1902 Fset (variable, (EQ (tem, Qt) ? Fsymbol_value (variable) : Qunbound));
1903 return variable;
1905 if (!blv)
1907 blv = make_blv (sym, forwarded, valcontents);
1908 sym->redirect = SYMBOL_LOCALIZED;
1909 SET_SYMBOL_BLV (sym, blv);
1912 /* Make sure this buffer has its own value of symbol. */
1913 XSETSYMBOL (variable, sym); /* Update in case of aliasing. */
1914 tem = Fassq (variable, BVAR (current_buffer, local_var_alist));
1915 if (NILP (tem))
1917 if (let_shadows_buffer_binding_p (sym))
1919 AUTO_STRING (format,
1920 "Making %s buffer-local while locally let-bound!");
1921 CALLN (Fmessage, format, SYMBOL_NAME (variable));
1924 /* Swap out any local binding for some other buffer, and make
1925 sure the current value is permanently recorded, if it's the
1926 default value. */
1927 find_symbol_value (variable);
1929 bset_local_var_alist
1930 (current_buffer,
1931 Fcons (Fcons (variable, XCDR (blv->defcell)),
1932 BVAR (current_buffer, local_var_alist)));
1934 /* Make sure symbol does not think it is set up for this buffer;
1935 force it to look once again for this buffer's value. */
1936 if (current_buffer == XBUFFER (blv->where))
1937 set_blv_where (blv, Qnil);
1938 set_blv_found (blv, 0);
1941 /* If the symbol forwards into a C variable, then load the binding
1942 for this buffer now. If C code modifies the variable before we
1943 load the binding in, then that new value will clobber the default
1944 binding the next time we unload it. */
1945 if (blv->fwd)
1946 swap_in_symval_forwarding (sym, blv);
1948 return variable;
1951 DEFUN ("kill-local-variable", Fkill_local_variable, Skill_local_variable,
1952 1, 1, "vKill Local Variable: ",
1953 doc: /* Make VARIABLE no longer have a separate value in the current buffer.
1954 From now on the default value will apply in this buffer. Return VARIABLE. */)
1955 (register Lisp_Object variable)
1957 register Lisp_Object tem;
1958 struct Lisp_Buffer_Local_Value *blv;
1959 struct Lisp_Symbol *sym;
1961 CHECK_SYMBOL (variable);
1962 sym = XSYMBOL (variable);
1964 start:
1965 switch (sym->redirect)
1967 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1968 case SYMBOL_PLAINVAL: return variable;
1969 case SYMBOL_FORWARDED:
1971 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
1972 if (BUFFER_OBJFWDP (valcontents))
1974 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1975 int idx = PER_BUFFER_IDX (offset);
1977 if (idx > 0)
1979 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 0);
1980 set_per_buffer_value (current_buffer, offset,
1981 per_buffer_default (offset));
1984 return variable;
1986 case SYMBOL_LOCALIZED:
1987 blv = SYMBOL_BLV (sym);
1988 break;
1989 default: emacs_abort ();
1992 if (sym->trapped_write == SYMBOL_TRAPPED_WRITE)
1993 notify_variable_watchers (variable, Qnil, Qmakunbound, Fcurrent_buffer ());
1995 /* Get rid of this buffer's alist element, if any. */
1996 XSETSYMBOL (variable, sym); /* Propagate variable indirection. */
1997 tem = Fassq (variable, BVAR (current_buffer, local_var_alist));
1998 if (!NILP (tem))
1999 bset_local_var_alist
2000 (current_buffer,
2001 Fdelq (tem, BVAR (current_buffer, local_var_alist)));
2003 /* If the symbol is set up with the current buffer's binding
2004 loaded, recompute its value. We have to do it now, or else
2005 forwarded objects won't work right. */
2007 Lisp_Object buf; XSETBUFFER (buf, current_buffer);
2008 if (EQ (buf, blv->where))
2010 set_blv_where (blv, Qnil);
2011 blv->found = 0;
2012 find_symbol_value (variable);
2016 return variable;
2019 /* Lisp functions for creating and removing buffer-local variables. */
2021 DEFUN ("local-variable-p", Flocal_variable_p, Slocal_variable_p,
2022 1, 2, 0,
2023 doc: /* Non-nil if VARIABLE has a local binding in buffer BUFFER.
2024 BUFFER defaults to the current buffer. */)
2025 (Lisp_Object variable, Lisp_Object buffer)
2027 struct buffer *buf = decode_buffer (buffer);
2028 struct Lisp_Symbol *sym;
2030 CHECK_SYMBOL (variable);
2031 sym = XSYMBOL (variable);
2033 start:
2034 switch (sym->redirect)
2036 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
2037 case SYMBOL_PLAINVAL: return Qnil;
2038 case SYMBOL_LOCALIZED:
2040 Lisp_Object tail, elt, tmp;
2041 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
2042 XSETBUFFER (tmp, buf);
2043 XSETSYMBOL (variable, sym); /* Update in case of aliasing. */
2045 if (EQ (blv->where, tmp)) /* The binding is already loaded. */
2046 return blv_found (blv) ? Qt : Qnil;
2047 else
2048 for (tail = BVAR (buf, local_var_alist); CONSP (tail); tail = XCDR (tail))
2050 elt = XCAR (tail);
2051 if (EQ (variable, XCAR (elt)))
2052 return Qt;
2054 return Qnil;
2056 case SYMBOL_FORWARDED:
2058 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
2059 if (BUFFER_OBJFWDP (valcontents))
2061 int offset = XBUFFER_OBJFWD (valcontents)->offset;
2062 int idx = PER_BUFFER_IDX (offset);
2063 if (idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
2064 return Qt;
2066 return Qnil;
2068 default: emacs_abort ();
2072 DEFUN ("local-variable-if-set-p", Flocal_variable_if_set_p, Slocal_variable_if_set_p,
2073 1, 2, 0,
2074 doc: /* Non-nil if VARIABLE is local in buffer BUFFER when set there.
2075 BUFFER defaults to the current buffer.
2077 More precisely, return non-nil if either VARIABLE already has a local
2078 value in BUFFER, or if VARIABLE is automatically buffer-local (see
2079 `make-variable-buffer-local'). */)
2080 (register Lisp_Object variable, Lisp_Object buffer)
2082 struct Lisp_Symbol *sym;
2084 CHECK_SYMBOL (variable);
2085 sym = XSYMBOL (variable);
2087 start:
2088 switch (sym->redirect)
2090 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
2091 case SYMBOL_PLAINVAL: return Qnil;
2092 case SYMBOL_LOCALIZED:
2094 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
2095 if (blv->local_if_set)
2096 return Qt;
2097 XSETSYMBOL (variable, sym); /* Update in case of aliasing. */
2098 return Flocal_variable_p (variable, buffer);
2100 case SYMBOL_FORWARDED:
2101 /* All BUFFER_OBJFWD slots become local if they are set. */
2102 return (BUFFER_OBJFWDP (SYMBOL_FWD (sym)) ? Qt : Qnil);
2103 default: emacs_abort ();
2107 DEFUN ("variable-binding-locus", Fvariable_binding_locus, Svariable_binding_locus,
2108 1, 1, 0,
2109 doc: /* Return a value indicating where VARIABLE's current binding comes from.
2110 If the current binding is buffer-local, the value is the current buffer.
2111 If the current binding is global (the default), the value is nil. */)
2112 (register Lisp_Object variable)
2114 struct Lisp_Symbol *sym;
2116 CHECK_SYMBOL (variable);
2117 sym = XSYMBOL (variable);
2119 /* Make sure the current binding is actually swapped in. */
2120 find_symbol_value (variable);
2122 start:
2123 switch (sym->redirect)
2125 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
2126 case SYMBOL_PLAINVAL: return Qnil;
2127 case SYMBOL_FORWARDED:
2129 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
2130 if (KBOARD_OBJFWDP (valcontents))
2131 return Fframe_terminal (selected_frame);
2132 else if (!BUFFER_OBJFWDP (valcontents))
2133 return Qnil;
2135 /* FALLTHROUGH */
2136 case SYMBOL_LOCALIZED:
2137 /* For a local variable, record both the symbol and which
2138 buffer's or frame's value we are saving. */
2139 if (!NILP (Flocal_variable_p (variable, Qnil)))
2140 return Fcurrent_buffer ();
2141 else if (sym->redirect == SYMBOL_LOCALIZED
2142 && blv_found (SYMBOL_BLV (sym)))
2143 return SYMBOL_BLV (sym)->where;
2144 else
2145 return Qnil;
2146 default: emacs_abort ();
2150 /* This code is disabled now that we use the selected frame to return
2151 keyboard-local-values. */
2152 #if 0
2153 extern struct terminal *get_terminal (Lisp_Object display, int);
2155 DEFUN ("terminal-local-value", Fterminal_local_value,
2156 Sterminal_local_value, 2, 2, 0,
2157 doc: /* Return the terminal-local value of SYMBOL on TERMINAL.
2158 If SYMBOL is not a terminal-local variable, then return its normal
2159 value, like `symbol-value'.
2161 TERMINAL may be a terminal object, a frame, or nil (meaning the
2162 selected frame's terminal device). */)
2163 (Lisp_Object symbol, Lisp_Object terminal)
2165 Lisp_Object result;
2166 struct terminal *t = get_terminal (terminal, 1);
2167 push_kboard (t->kboard);
2168 result = Fsymbol_value (symbol);
2169 pop_kboard ();
2170 return result;
2173 DEFUN ("set-terminal-local-value", Fset_terminal_local_value,
2174 Sset_terminal_local_value, 3, 3, 0,
2175 doc: /* Set the terminal-local binding of SYMBOL on TERMINAL to VALUE.
2176 If VARIABLE is not a terminal-local variable, then set its normal
2177 binding, like `set'.
2179 TERMINAL may be a terminal object, a frame, or nil (meaning the
2180 selected frame's terminal device). */)
2181 (Lisp_Object symbol, Lisp_Object terminal, Lisp_Object value)
2183 Lisp_Object result;
2184 struct terminal *t = get_terminal (terminal, 1);
2185 push_kboard (d->kboard);
2186 result = Fset (symbol, value);
2187 pop_kboard ();
2188 return result;
2190 #endif
2192 /* Find the function at the end of a chain of symbol function indirections. */
2194 /* If OBJECT is a symbol, find the end of its function chain and
2195 return the value found there. If OBJECT is not a symbol, just
2196 return it. If there is a cycle in the function chain, signal a
2197 cyclic-function-indirection error.
2199 This is like Findirect_function, except that it doesn't signal an
2200 error if the chain ends up unbound. */
2201 Lisp_Object
2202 indirect_function (register Lisp_Object object)
2204 Lisp_Object tortoise, hare;
2206 hare = tortoise = object;
2208 for (;;)
2210 if (!SYMBOLP (hare) || NILP (hare))
2211 break;
2212 hare = XSYMBOL (hare)->function;
2213 if (!SYMBOLP (hare) || NILP (hare))
2214 break;
2215 hare = XSYMBOL (hare)->function;
2217 tortoise = XSYMBOL (tortoise)->function;
2219 if (EQ (hare, tortoise))
2220 xsignal1 (Qcyclic_function_indirection, object);
2223 return hare;
2226 DEFUN ("indirect-function", Findirect_function, Sindirect_function, 1, 2, 0,
2227 doc: /* Return the function at the end of OBJECT's function chain.
2228 If OBJECT is not a symbol, just return it. Otherwise, follow all
2229 function indirections to find the final function binding and return it.
2230 Signal a cyclic-function-indirection error if there is a loop in the
2231 function chain of symbols. */)
2232 (register Lisp_Object object, Lisp_Object noerror)
2234 Lisp_Object result;
2236 /* Optimize for no indirection. */
2237 result = object;
2238 if (SYMBOLP (result) && !NILP (result)
2239 && (result = XSYMBOL (result)->function, SYMBOLP (result)))
2240 result = indirect_function (result);
2241 if (!NILP (result))
2242 return result;
2244 return Qnil;
2247 /* Extract and set vector and string elements. */
2249 DEFUN ("aref", Faref, Saref, 2, 2, 0,
2250 doc: /* Return the element of ARRAY at index IDX.
2251 ARRAY may be a vector, a string, a char-table, a bool-vector,
2252 or a byte-code object. IDX starts at 0. */)
2253 (register Lisp_Object array, Lisp_Object idx)
2255 register EMACS_INT idxval;
2257 CHECK_NUMBER (idx);
2258 idxval = XINT (idx);
2259 if (STRINGP (array))
2261 int c;
2262 ptrdiff_t idxval_byte;
2264 if (idxval < 0 || idxval >= SCHARS (array))
2265 args_out_of_range (array, idx);
2266 if (! STRING_MULTIBYTE (array))
2267 return make_number ((unsigned char) SREF (array, idxval));
2268 idxval_byte = string_char_to_byte (array, idxval);
2270 c = STRING_CHAR (SDATA (array) + idxval_byte);
2271 return make_number (c);
2273 else if (BOOL_VECTOR_P (array))
2275 if (idxval < 0 || idxval >= bool_vector_size (array))
2276 args_out_of_range (array, idx);
2277 return bool_vector_ref (array, idxval);
2279 else if (CHAR_TABLE_P (array))
2281 CHECK_CHARACTER (idx);
2282 return CHAR_TABLE_REF (array, idxval);
2284 else
2286 ptrdiff_t size = 0;
2287 if (VECTORP (array))
2288 size = ASIZE (array);
2289 else if (COMPILEDP (array))
2290 size = ASIZE (array) & PSEUDOVECTOR_SIZE_MASK;
2291 else
2292 wrong_type_argument (Qarrayp, array);
2294 if (idxval < 0 || idxval >= size)
2295 args_out_of_range (array, idx);
2296 return AREF (array, idxval);
2300 DEFUN ("aset", Faset, Saset, 3, 3, 0,
2301 doc: /* Store into the element of ARRAY at index IDX the value NEWELT.
2302 Return NEWELT. ARRAY may be a vector, a string, a char-table or a
2303 bool-vector. IDX starts at 0. */)
2304 (register Lisp_Object array, Lisp_Object idx, Lisp_Object newelt)
2306 register EMACS_INT idxval;
2308 CHECK_NUMBER (idx);
2309 idxval = XINT (idx);
2310 CHECK_ARRAY (array, Qarrayp);
2312 if (VECTORP (array))
2314 CHECK_IMPURE (array, XVECTOR (array));
2315 if (idxval < 0 || idxval >= ASIZE (array))
2316 args_out_of_range (array, idx);
2317 ASET (array, idxval, newelt);
2319 else if (BOOL_VECTOR_P (array))
2321 if (idxval < 0 || idxval >= bool_vector_size (array))
2322 args_out_of_range (array, idx);
2323 bool_vector_set (array, idxval, !NILP (newelt));
2325 else if (CHAR_TABLE_P (array))
2327 CHECK_CHARACTER (idx);
2328 CHAR_TABLE_SET (array, idxval, newelt);
2330 else
2332 int c;
2334 CHECK_IMPURE (array, XSTRING (array));
2335 if (idxval < 0 || idxval >= SCHARS (array))
2336 args_out_of_range (array, idx);
2337 CHECK_CHARACTER (newelt);
2338 c = XFASTINT (newelt);
2340 if (STRING_MULTIBYTE (array))
2342 ptrdiff_t idxval_byte, nbytes;
2343 int prev_bytes, new_bytes;
2344 unsigned char workbuf[MAX_MULTIBYTE_LENGTH], *p0 = workbuf, *p1;
2346 nbytes = SBYTES (array);
2347 idxval_byte = string_char_to_byte (array, idxval);
2348 p1 = SDATA (array) + idxval_byte;
2349 prev_bytes = BYTES_BY_CHAR_HEAD (*p1);
2350 new_bytes = CHAR_STRING (c, p0);
2351 if (prev_bytes != new_bytes)
2353 /* We must relocate the string data. */
2354 ptrdiff_t nchars = SCHARS (array);
2355 USE_SAFE_ALLOCA;
2356 unsigned char *str = SAFE_ALLOCA (nbytes);
2358 memcpy (str, SDATA (array), nbytes);
2359 allocate_string_data (XSTRING (array), nchars,
2360 nbytes + new_bytes - prev_bytes);
2361 memcpy (SDATA (array), str, idxval_byte);
2362 p1 = SDATA (array) + idxval_byte;
2363 memcpy (p1 + new_bytes, str + idxval_byte + prev_bytes,
2364 nbytes - (idxval_byte + prev_bytes));
2365 SAFE_FREE ();
2366 clear_string_char_byte_cache ();
2368 while (new_bytes--)
2369 *p1++ = *p0++;
2371 else
2373 if (! SINGLE_BYTE_CHAR_P (c))
2375 ptrdiff_t i;
2377 for (i = SBYTES (array) - 1; i >= 0; i--)
2378 if (SREF (array, i) >= 0x80)
2379 args_out_of_range (array, newelt);
2380 /* ARRAY is an ASCII string. Convert it to a multibyte
2381 string, and try `aset' again. */
2382 STRING_SET_MULTIBYTE (array);
2383 return Faset (array, idx, newelt);
2385 SSET (array, idxval, c);
2389 return newelt;
2392 /* Arithmetic functions */
2394 Lisp_Object
2395 arithcompare (Lisp_Object num1, Lisp_Object num2,
2396 enum Arith_Comparison comparison)
2398 double f1, f2;
2399 EMACS_INT i1, i2;
2400 bool fneq;
2401 bool test;
2403 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num1);
2404 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num2);
2406 /* If either arg is floating point, set F1 and F2 to the 'double'
2407 approximations of the two arguments. Regardless, set I1 and I2
2408 to integers that break ties if the floating point comparison is
2409 either not done or reports equality. */
2411 if (FLOATP (num1))
2413 f1 = XFLOAT_DATA (num1);
2414 if (FLOATP (num2))
2416 i1 = i2 = 0;
2417 f2 = XFLOAT_DATA (num2);
2419 else
2420 i1 = f2 = i2 = XINT (num2);
2421 fneq = f1 != f2;
2423 else
2425 i1 = XINT (num1);
2426 if (FLOATP (num2))
2428 i2 = f1 = i1;
2429 f2 = XFLOAT_DATA (num2);
2430 fneq = f1 != f2;
2432 else
2434 i2 = XINT (num2);
2435 fneq = false;
2439 switch (comparison)
2441 case ARITH_EQUAL:
2442 test = !fneq && i1 == i2;
2443 break;
2445 case ARITH_NOTEQUAL:
2446 test = fneq || i1 != i2;
2447 break;
2449 case ARITH_LESS:
2450 test = fneq ? f1 < f2 : i1 < i2;
2451 break;
2453 case ARITH_LESS_OR_EQUAL:
2454 test = fneq ? f1 <= f2 : i1 <= i2;
2455 break;
2457 case ARITH_GRTR:
2458 test = fneq ? f1 > f2 : i1 > i2;
2459 break;
2461 case ARITH_GRTR_OR_EQUAL:
2462 test = fneq ? f1 >= f2 : i1 >= i2;
2463 break;
2465 default:
2466 eassume (false);
2469 return test ? Qt : Qnil;
2472 static Lisp_Object
2473 arithcompare_driver (ptrdiff_t nargs, Lisp_Object *args,
2474 enum Arith_Comparison comparison)
2476 for (ptrdiff_t i = 1; i < nargs; i++)
2477 if (NILP (arithcompare (args[i - 1], args[i], comparison)))
2478 return Qnil;
2479 return Qt;
2482 DEFUN ("=", Feqlsign, Seqlsign, 1, MANY, 0,
2483 doc: /* Return t if args, all numbers or markers, are equal.
2484 usage: (= NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2485 (ptrdiff_t nargs, Lisp_Object *args)
2487 return arithcompare_driver (nargs, args, ARITH_EQUAL);
2490 DEFUN ("<", Flss, Slss, 1, MANY, 0,
2491 doc: /* Return t if each arg (a number or marker), is less than the next arg.
2492 usage: (< NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2493 (ptrdiff_t nargs, Lisp_Object *args)
2495 return arithcompare_driver (nargs, args, ARITH_LESS);
2498 DEFUN (">", Fgtr, Sgtr, 1, MANY, 0,
2499 doc: /* Return t if each arg (a number or marker) is greater than the next arg.
2500 usage: (> NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2501 (ptrdiff_t nargs, Lisp_Object *args)
2503 return arithcompare_driver (nargs, args, ARITH_GRTR);
2506 DEFUN ("<=", Fleq, Sleq, 1, MANY, 0,
2507 doc: /* Return t if each arg (a number or marker) is less than or equal to the next.
2508 usage: (<= NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2509 (ptrdiff_t nargs, Lisp_Object *args)
2511 return arithcompare_driver (nargs, args, ARITH_LESS_OR_EQUAL);
2514 DEFUN (">=", Fgeq, Sgeq, 1, MANY, 0,
2515 doc: /* Return t if each arg (a number or marker) is greater than or equal to the next.
2516 usage: (>= NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2517 (ptrdiff_t nargs, Lisp_Object *args)
2519 return arithcompare_driver (nargs, args, ARITH_GRTR_OR_EQUAL);
2522 DEFUN ("/=", Fneq, Sneq, 2, 2, 0,
2523 doc: /* Return t if first arg is not equal to second arg. Both must be numbers or markers. */)
2524 (register Lisp_Object num1, Lisp_Object num2)
2526 return arithcompare (num1, num2, ARITH_NOTEQUAL);
2529 /* Convert the integer I to a cons-of-integers, where I is not in
2530 fixnum range. */
2532 #define INTBIG_TO_LISP(i, extremum) \
2533 (eassert (FIXNUM_OVERFLOW_P (i)), \
2534 (! (FIXNUM_OVERFLOW_P ((extremum) >> 16) \
2535 && FIXNUM_OVERFLOW_P ((i) >> 16)) \
2536 ? Fcons (make_number ((i) >> 16), make_number ((i) & 0xffff)) \
2537 : ! (FIXNUM_OVERFLOW_P ((extremum) >> 16 >> 24) \
2538 && FIXNUM_OVERFLOW_P ((i) >> 16 >> 24)) \
2539 ? Fcons (make_number ((i) >> 16 >> 24), \
2540 Fcons (make_number ((i) >> 16 & 0xffffff), \
2541 make_number ((i) & 0xffff))) \
2542 : make_float (i)))
2544 Lisp_Object
2545 intbig_to_lisp (intmax_t i)
2547 return INTBIG_TO_LISP (i, INTMAX_MIN);
2550 Lisp_Object
2551 uintbig_to_lisp (uintmax_t i)
2553 return INTBIG_TO_LISP (i, UINTMAX_MAX);
2556 /* Convert the cons-of-integers, integer, or float value C to an
2557 unsigned value with maximum value MAX. Signal an error if C does not
2558 have a valid format or is out of range. */
2559 uintmax_t
2560 cons_to_unsigned (Lisp_Object c, uintmax_t max)
2562 bool valid = 0;
2563 uintmax_t val;
2564 if (INTEGERP (c))
2566 valid = 0 <= XINT (c);
2567 val = XINT (c);
2569 else if (FLOATP (c))
2571 double d = XFLOAT_DATA (c);
2572 if (0 <= d
2573 && d < (max == UINTMAX_MAX ? (double) UINTMAX_MAX + 1 : max + 1))
2575 val = d;
2576 valid = 1;
2579 else if (CONSP (c) && NATNUMP (XCAR (c)))
2581 uintmax_t top = XFASTINT (XCAR (c));
2582 Lisp_Object rest = XCDR (c);
2583 if (top <= UINTMAX_MAX >> 24 >> 16
2584 && CONSP (rest)
2585 && NATNUMP (XCAR (rest)) && XFASTINT (XCAR (rest)) < 1 << 24
2586 && NATNUMP (XCDR (rest)) && XFASTINT (XCDR (rest)) < 1 << 16)
2588 uintmax_t mid = XFASTINT (XCAR (rest));
2589 val = top << 24 << 16 | mid << 16 | XFASTINT (XCDR (rest));
2590 valid = 1;
2592 else if (top <= UINTMAX_MAX >> 16)
2594 if (CONSP (rest))
2595 rest = XCAR (rest);
2596 if (NATNUMP (rest) && XFASTINT (rest) < 1 << 16)
2598 val = top << 16 | XFASTINT (rest);
2599 valid = 1;
2604 if (! (valid && val <= max))
2605 error ("Not an in-range integer, float, or cons of integers");
2606 return val;
2609 /* Convert the cons-of-integers, integer, or float value C to a signed
2610 value with extrema MIN and MAX. Signal an error if C does not have
2611 a valid format or is out of range. */
2612 intmax_t
2613 cons_to_signed (Lisp_Object c, intmax_t min, intmax_t max)
2615 bool valid = 0;
2616 intmax_t val;
2617 if (INTEGERP (c))
2619 val = XINT (c);
2620 valid = 1;
2622 else if (FLOATP (c))
2624 double d = XFLOAT_DATA (c);
2625 if (min <= d
2626 && d < (max == INTMAX_MAX ? (double) INTMAX_MAX + 1 : max + 1))
2628 val = d;
2629 valid = 1;
2632 else if (CONSP (c) && INTEGERP (XCAR (c)))
2634 intmax_t top = XINT (XCAR (c));
2635 Lisp_Object rest = XCDR (c);
2636 if (INTMAX_MIN >> 24 >> 16 <= top && top <= INTMAX_MAX >> 24 >> 16
2637 && CONSP (rest)
2638 && NATNUMP (XCAR (rest)) && XFASTINT (XCAR (rest)) < 1 << 24
2639 && NATNUMP (XCDR (rest)) && XFASTINT (XCDR (rest)) < 1 << 16)
2641 intmax_t mid = XFASTINT (XCAR (rest));
2642 val = top << 24 << 16 | mid << 16 | XFASTINT (XCDR (rest));
2643 valid = 1;
2645 else if (INTMAX_MIN >> 16 <= top && top <= INTMAX_MAX >> 16)
2647 if (CONSP (rest))
2648 rest = XCAR (rest);
2649 if (NATNUMP (rest) && XFASTINT (rest) < 1 << 16)
2651 val = top << 16 | XFASTINT (rest);
2652 valid = 1;
2657 if (! (valid && min <= val && val <= max))
2658 error ("Not an in-range integer, float, or cons of integers");
2659 return val;
2662 DEFUN ("number-to-string", Fnumber_to_string, Snumber_to_string, 1, 1, 0,
2663 doc: /* Return the decimal representation of NUMBER as a string.
2664 Uses a minus sign if negative.
2665 NUMBER may be an integer or a floating point number. */)
2666 (Lisp_Object number)
2668 char buffer[max (FLOAT_TO_STRING_BUFSIZE, INT_BUFSIZE_BOUND (EMACS_INT))];
2669 int len;
2671 CHECK_NUMBER_OR_FLOAT (number);
2673 if (FLOATP (number))
2674 len = float_to_string (buffer, XFLOAT_DATA (number));
2675 else
2676 len = sprintf (buffer, "%"pI"d", XINT (number));
2678 return make_unibyte_string (buffer, len);
2681 DEFUN ("string-to-number", Fstring_to_number, Sstring_to_number, 1, 2, 0,
2682 doc: /* Parse STRING as a decimal number and return the number.
2683 Ignore leading spaces and tabs, and all trailing chars. Return 0 if
2684 STRING cannot be parsed as an integer or floating point number.
2686 If BASE, interpret STRING as a number in that base. If BASE isn't
2687 present, base 10 is used. BASE must be between 2 and 16 (inclusive).
2688 If the base used is not 10, STRING is always parsed as an integer. */)
2689 (register Lisp_Object string, Lisp_Object base)
2691 register char *p;
2692 register int b;
2693 Lisp_Object val;
2695 CHECK_STRING (string);
2697 if (NILP (base))
2698 b = 10;
2699 else
2701 CHECK_NUMBER (base);
2702 if (! (2 <= XINT (base) && XINT (base) <= 16))
2703 xsignal1 (Qargs_out_of_range, base);
2704 b = XINT (base);
2707 p = SSDATA (string);
2708 while (*p == ' ' || *p == '\t')
2709 p++;
2711 val = string_to_number (p, b, 1);
2712 return NILP (val) ? make_number (0) : val;
2715 enum arithop
2717 Aadd,
2718 Asub,
2719 Amult,
2720 Adiv,
2721 Alogand,
2722 Alogior,
2723 Alogxor,
2724 Amax,
2725 Amin
2728 static Lisp_Object float_arith_driver (double, ptrdiff_t, enum arithop,
2729 ptrdiff_t, Lisp_Object *);
2730 static Lisp_Object
2731 arith_driver (enum arithop code, ptrdiff_t nargs, Lisp_Object *args)
2733 Lisp_Object val;
2734 ptrdiff_t argnum, ok_args;
2735 EMACS_INT accum = 0;
2736 EMACS_INT next, ok_accum;
2737 bool overflow = 0;
2739 switch (code)
2741 case Alogior:
2742 case Alogxor:
2743 case Aadd:
2744 case Asub:
2745 accum = 0;
2746 break;
2747 case Amult:
2748 case Adiv:
2749 accum = 1;
2750 break;
2751 case Alogand:
2752 accum = -1;
2753 break;
2754 default:
2755 break;
2758 for (argnum = 0; argnum < nargs; argnum++)
2760 if (! overflow)
2762 ok_args = argnum;
2763 ok_accum = accum;
2766 /* Using args[argnum] as argument to CHECK_NUMBER_... */
2767 val = args[argnum];
2768 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val);
2770 if (FLOATP (val))
2771 return float_arith_driver (ok_accum, ok_args, code,
2772 nargs, args);
2773 args[argnum] = val;
2774 next = XINT (args[argnum]);
2775 switch (code)
2777 case Aadd:
2778 overflow |= INT_ADD_WRAPV (accum, next, &accum);
2779 break;
2780 case Asub:
2781 if (! argnum)
2782 accum = nargs == 1 ? - next : next;
2783 else
2784 overflow |= INT_SUBTRACT_WRAPV (accum, next, &accum);
2785 break;
2786 case Amult:
2787 overflow |= INT_MULTIPLY_WRAPV (accum, next, &accum);
2788 break;
2789 case Adiv:
2790 if (! (argnum || nargs == 1))
2791 accum = next;
2792 else
2794 if (next == 0)
2795 xsignal0 (Qarith_error);
2796 if (INT_DIVIDE_OVERFLOW (accum, next))
2797 overflow = true;
2798 else
2799 accum /= next;
2801 break;
2802 case Alogand:
2803 accum &= next;
2804 break;
2805 case Alogior:
2806 accum |= next;
2807 break;
2808 case Alogxor:
2809 accum ^= next;
2810 break;
2811 case Amax:
2812 if (!argnum || next > accum)
2813 accum = next;
2814 break;
2815 case Amin:
2816 if (!argnum || next < accum)
2817 accum = next;
2818 break;
2822 XSETINT (val, accum);
2823 return val;
2826 #undef isnan
2827 #define isnan(x) ((x) != (x))
2829 static Lisp_Object
2830 float_arith_driver (double accum, ptrdiff_t argnum, enum arithop code,
2831 ptrdiff_t nargs, Lisp_Object *args)
2833 register Lisp_Object val;
2834 double next;
2836 for (; argnum < nargs; argnum++)
2838 val = args[argnum]; /* using args[argnum] as argument to CHECK_NUMBER_... */
2839 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val);
2841 if (FLOATP (val))
2843 next = XFLOAT_DATA (val);
2845 else
2847 args[argnum] = val; /* runs into a compiler bug. */
2848 next = XINT (args[argnum]);
2850 switch (code)
2852 case Aadd:
2853 accum += next;
2854 break;
2855 case Asub:
2856 accum = argnum ? accum - next : nargs == 1 ? - next : next;
2857 break;
2858 case Amult:
2859 accum *= next;
2860 break;
2861 case Adiv:
2862 if (! (argnum || nargs == 1))
2863 accum = next;
2864 else
2866 if (! IEEE_FLOATING_POINT && next == 0)
2867 xsignal0 (Qarith_error);
2868 accum /= next;
2870 break;
2871 case Alogand:
2872 case Alogior:
2873 case Alogxor:
2874 wrong_type_argument (Qinteger_or_marker_p, val);
2875 case Amax:
2876 if (!argnum || isnan (next) || next > accum)
2877 accum = next;
2878 break;
2879 case Amin:
2880 if (!argnum || isnan (next) || next < accum)
2881 accum = next;
2882 break;
2886 return make_float (accum);
2890 DEFUN ("+", Fplus, Splus, 0, MANY, 0,
2891 doc: /* Return sum of any number of arguments, which are numbers or markers.
2892 usage: (+ &rest NUMBERS-OR-MARKERS) */)
2893 (ptrdiff_t nargs, Lisp_Object *args)
2895 return arith_driver (Aadd, nargs, args);
2898 DEFUN ("-", Fminus, Sminus, 0, MANY, 0,
2899 doc: /* Negate number or subtract numbers or markers and return the result.
2900 With one arg, negates it. With more than one arg,
2901 subtracts all but the first from the first.
2902 usage: (- &optional NUMBER-OR-MARKER &rest MORE-NUMBERS-OR-MARKERS) */)
2903 (ptrdiff_t nargs, Lisp_Object *args)
2905 return arith_driver (Asub, nargs, args);
2908 DEFUN ("*", Ftimes, Stimes, 0, MANY, 0,
2909 doc: /* Return product of any number of arguments, which are numbers or markers.
2910 usage: (* &rest NUMBERS-OR-MARKERS) */)
2911 (ptrdiff_t nargs, Lisp_Object *args)
2913 return arith_driver (Amult, nargs, args);
2916 DEFUN ("/", Fquo, Squo, 1, MANY, 0,
2917 doc: /* Divide number by divisors and return the result.
2918 With two or more arguments, return first argument divided by the rest.
2919 With one argument, return 1 divided by the argument.
2920 The arguments must be numbers or markers.
2921 usage: (/ NUMBER &rest DIVISORS) */)
2922 (ptrdiff_t nargs, Lisp_Object *args)
2924 ptrdiff_t argnum;
2925 for (argnum = 2; argnum < nargs; argnum++)
2926 if (FLOATP (args[argnum]))
2927 return float_arith_driver (0, 0, Adiv, nargs, args);
2928 return arith_driver (Adiv, nargs, args);
2931 DEFUN ("%", Frem, Srem, 2, 2, 0,
2932 doc: /* Return remainder of X divided by Y.
2933 Both must be integers or markers. */)
2934 (register Lisp_Object x, Lisp_Object y)
2936 Lisp_Object val;
2938 CHECK_NUMBER_COERCE_MARKER (x);
2939 CHECK_NUMBER_COERCE_MARKER (y);
2941 if (XINT (y) == 0)
2942 xsignal0 (Qarith_error);
2944 XSETINT (val, XINT (x) % XINT (y));
2945 return val;
2948 DEFUN ("mod", Fmod, Smod, 2, 2, 0,
2949 doc: /* Return X modulo Y.
2950 The result falls between zero (inclusive) and Y (exclusive).
2951 Both X and Y must be numbers or markers. */)
2952 (register Lisp_Object x, Lisp_Object y)
2954 Lisp_Object val;
2955 EMACS_INT i1, i2;
2957 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (x);
2958 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (y);
2960 if (FLOATP (x) || FLOATP (y))
2961 return fmod_float (x, y);
2963 i1 = XINT (x);
2964 i2 = XINT (y);
2966 if (i2 == 0)
2967 xsignal0 (Qarith_error);
2969 i1 %= i2;
2971 /* If the "remainder" comes out with the wrong sign, fix it. */
2972 if (i2 < 0 ? i1 > 0 : i1 < 0)
2973 i1 += i2;
2975 XSETINT (val, i1);
2976 return val;
2979 DEFUN ("max", Fmax, Smax, 1, MANY, 0,
2980 doc: /* Return largest of all the arguments (which must be numbers or markers).
2981 The value is always a number; markers are converted to numbers.
2982 usage: (max NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2983 (ptrdiff_t nargs, Lisp_Object *args)
2985 return arith_driver (Amax, nargs, args);
2988 DEFUN ("min", Fmin, Smin, 1, MANY, 0,
2989 doc: /* Return smallest of all the arguments (which must be numbers or markers).
2990 The value is always a number; markers are converted to numbers.
2991 usage: (min NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2992 (ptrdiff_t nargs, Lisp_Object *args)
2994 return arith_driver (Amin, nargs, args);
2997 DEFUN ("logand", Flogand, Slogand, 0, MANY, 0,
2998 doc: /* Return bitwise-and of all the arguments.
2999 Arguments may be integers, or markers converted to integers.
3000 usage: (logand &rest INTS-OR-MARKERS) */)
3001 (ptrdiff_t nargs, Lisp_Object *args)
3003 return arith_driver (Alogand, nargs, args);
3006 DEFUN ("logior", Flogior, Slogior, 0, MANY, 0,
3007 doc: /* Return bitwise-or of all the arguments.
3008 Arguments may be integers, or markers converted to integers.
3009 usage: (logior &rest INTS-OR-MARKERS) */)
3010 (ptrdiff_t nargs, Lisp_Object *args)
3012 return arith_driver (Alogior, nargs, args);
3015 DEFUN ("logxor", Flogxor, Slogxor, 0, MANY, 0,
3016 doc: /* Return bitwise-exclusive-or of all the arguments.
3017 Arguments may be integers, or markers converted to integers.
3018 usage: (logxor &rest INTS-OR-MARKERS) */)
3019 (ptrdiff_t nargs, Lisp_Object *args)
3021 return arith_driver (Alogxor, nargs, args);
3024 static Lisp_Object
3025 ash_lsh_impl (register Lisp_Object value, Lisp_Object count, bool lsh)
3027 register Lisp_Object val;
3029 CHECK_NUMBER (value);
3030 CHECK_NUMBER (count);
3032 if (XINT (count) >= EMACS_INT_WIDTH)
3033 XSETINT (val, 0);
3034 else if (XINT (count) > 0)
3035 XSETINT (val, XUINT (value) << XFASTINT (count));
3036 else if (XINT (count) <= -EMACS_INT_WIDTH)
3037 XSETINT (val, lsh ? 0 : XINT (value) < 0 ? -1 : 0);
3038 else
3039 XSETINT (val, lsh ? XUINT (value) >> -XINT (count) : \
3040 XINT (value) >> -XINT (count));
3041 return val;
3044 DEFUN ("ash", Fash, Sash, 2, 2, 0,
3045 doc: /* Return VALUE with its bits shifted left by COUNT.
3046 If COUNT is negative, shifting is actually to the right.
3047 In this case, the sign bit is duplicated. */)
3048 (register Lisp_Object value, Lisp_Object count)
3050 return ash_lsh_impl (value, count, false);
3053 DEFUN ("lsh", Flsh, Slsh, 2, 2, 0,
3054 doc: /* Return VALUE with its bits shifted left by COUNT.
3055 If COUNT is negative, shifting is actually to the right.
3056 In this case, zeros are shifted in on the left. */)
3057 (register Lisp_Object value, Lisp_Object count)
3059 return ash_lsh_impl (value, count, true);
3062 DEFUN ("1+", Fadd1, Sadd1, 1, 1, 0,
3063 doc: /* Return NUMBER plus one. NUMBER may be a number or a marker.
3064 Markers are converted to integers. */)
3065 (register Lisp_Object number)
3067 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number);
3069 if (FLOATP (number))
3070 return (make_float (1.0 + XFLOAT_DATA (number)));
3072 XSETINT (number, XINT (number) + 1);
3073 return number;
3076 DEFUN ("1-", Fsub1, Ssub1, 1, 1, 0,
3077 doc: /* Return NUMBER minus one. NUMBER may be a number or a marker.
3078 Markers are converted to integers. */)
3079 (register Lisp_Object number)
3081 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number);
3083 if (FLOATP (number))
3084 return (make_float (-1.0 + XFLOAT_DATA (number)));
3086 XSETINT (number, XINT (number) - 1);
3087 return number;
3090 DEFUN ("lognot", Flognot, Slognot, 1, 1, 0,
3091 doc: /* Return the bitwise complement of NUMBER. NUMBER must be an integer. */)
3092 (register Lisp_Object number)
3094 CHECK_NUMBER (number);
3095 XSETINT (number, ~XINT (number));
3096 return number;
3099 DEFUN ("byteorder", Fbyteorder, Sbyteorder, 0, 0, 0,
3100 doc: /* Return the byteorder for the machine.
3101 Returns 66 (ASCII uppercase B) for big endian machines or 108 (ASCII
3102 lowercase l) for small endian machines. */
3103 attributes: const)
3104 (void)
3106 unsigned i = 0x04030201;
3107 int order = *(char *)&i == 1 ? 108 : 66;
3109 return make_number (order);
3112 /* Because we round up the bool vector allocate size to word_size
3113 units, we can safely read past the "end" of the vector in the
3114 operations below. These extra bits are always zero. */
3116 static bits_word
3117 bool_vector_spare_mask (EMACS_INT nr_bits)
3119 return (((bits_word) 1) << (nr_bits % BITS_PER_BITS_WORD)) - 1;
3122 /* Info about unsigned long long, falling back on unsigned long
3123 if unsigned long long is not available. */
3125 #if HAVE_UNSIGNED_LONG_LONG_INT && defined ULLONG_WIDTH
3126 enum { ULL_WIDTH = ULLONG_WIDTH };
3127 # define ULL_MAX ULLONG_MAX
3128 #else
3129 enum { ULL_WIDTH = ULONG_WIDTH };
3130 # define ULL_MAX ULONG_MAX
3131 # define count_one_bits_ll count_one_bits_l
3132 # define count_trailing_zeros_ll count_trailing_zeros_l
3133 #endif
3135 /* Shift VAL right by the width of an unsigned long long.
3136 ULL_WIDTH must be less than BITS_PER_BITS_WORD. */
3138 static bits_word
3139 shift_right_ull (bits_word w)
3141 /* Pacify bogus GCC warning about shift count exceeding type width. */
3142 int shift = ULL_WIDTH - BITS_PER_BITS_WORD < 0 ? ULL_WIDTH : 0;
3143 return w >> shift;
3146 /* Return the number of 1 bits in W. */
3148 static int
3149 count_one_bits_word (bits_word w)
3151 if (BITS_WORD_MAX <= UINT_MAX)
3152 return count_one_bits (w);
3153 else if (BITS_WORD_MAX <= ULONG_MAX)
3154 return count_one_bits_l (w);
3155 else
3157 int i = 0, count = 0;
3158 while (count += count_one_bits_ll (w),
3159 (i += ULL_WIDTH) < BITS_PER_BITS_WORD)
3160 w = shift_right_ull (w);
3161 return count;
3165 enum bool_vector_op { bool_vector_exclusive_or,
3166 bool_vector_union,
3167 bool_vector_intersection,
3168 bool_vector_set_difference,
3169 bool_vector_subsetp };
3171 static Lisp_Object
3172 bool_vector_binop_driver (Lisp_Object a,
3173 Lisp_Object b,
3174 Lisp_Object dest,
3175 enum bool_vector_op op)
3177 EMACS_INT nr_bits;
3178 bits_word *adata, *bdata, *destdata;
3179 ptrdiff_t i = 0;
3180 ptrdiff_t nr_words;
3182 CHECK_BOOL_VECTOR (a);
3183 CHECK_BOOL_VECTOR (b);
3185 nr_bits = bool_vector_size (a);
3186 if (bool_vector_size (b) != nr_bits)
3187 wrong_length_argument (a, b, dest);
3189 nr_words = bool_vector_words (nr_bits);
3190 adata = bool_vector_data (a);
3191 bdata = bool_vector_data (b);
3193 if (NILP (dest))
3195 dest = make_uninit_bool_vector (nr_bits);
3196 destdata = bool_vector_data (dest);
3198 else
3200 CHECK_BOOL_VECTOR (dest);
3201 destdata = bool_vector_data (dest);
3202 if (bool_vector_size (dest) != nr_bits)
3203 wrong_length_argument (a, b, dest);
3205 switch (op)
3207 case bool_vector_exclusive_or:
3208 for (; i < nr_words; i++)
3209 if (destdata[i] != (adata[i] ^ bdata[i]))
3210 goto set_dest;
3211 break;
3213 case bool_vector_subsetp:
3214 for (; i < nr_words; i++)
3215 if (adata[i] &~ bdata[i])
3216 return Qnil;
3217 return Qt;
3219 case bool_vector_union:
3220 for (; i < nr_words; i++)
3221 if (destdata[i] != (adata[i] | bdata[i]))
3222 goto set_dest;
3223 break;
3225 case bool_vector_intersection:
3226 for (; i < nr_words; i++)
3227 if (destdata[i] != (adata[i] & bdata[i]))
3228 goto set_dest;
3229 break;
3231 case bool_vector_set_difference:
3232 for (; i < nr_words; i++)
3233 if (destdata[i] != (adata[i] &~ bdata[i]))
3234 goto set_dest;
3235 break;
3238 return Qnil;
3241 set_dest:
3242 switch (op)
3244 case bool_vector_exclusive_or:
3245 for (; i < nr_words; i++)
3246 destdata[i] = adata[i] ^ bdata[i];
3247 break;
3249 case bool_vector_union:
3250 for (; i < nr_words; i++)
3251 destdata[i] = adata[i] | bdata[i];
3252 break;
3254 case bool_vector_intersection:
3255 for (; i < nr_words; i++)
3256 destdata[i] = adata[i] & bdata[i];
3257 break;
3259 case bool_vector_set_difference:
3260 for (; i < nr_words; i++)
3261 destdata[i] = adata[i] &~ bdata[i];
3262 break;
3264 default:
3265 eassume (0);
3268 return dest;
3271 /* PRECONDITION must be true. Return VALUE. This odd construction
3272 works around a bogus GCC diagnostic "shift count >= width of type". */
3274 static int
3275 pre_value (bool precondition, int value)
3277 eassume (precondition);
3278 return precondition ? value : 0;
3281 /* Compute the number of trailing zero bits in val. If val is zero,
3282 return the number of bits in val. */
3283 static int
3284 count_trailing_zero_bits (bits_word val)
3286 if (BITS_WORD_MAX == UINT_MAX)
3287 return count_trailing_zeros (val);
3288 if (BITS_WORD_MAX == ULONG_MAX)
3289 return count_trailing_zeros_l (val);
3290 if (BITS_WORD_MAX == ULL_MAX)
3291 return count_trailing_zeros_ll (val);
3293 /* The rest of this code is for the unlikely platform where bits_word differs
3294 in width from unsigned int, unsigned long, and unsigned long long. */
3295 val |= ~ BITS_WORD_MAX;
3296 if (BITS_WORD_MAX <= UINT_MAX)
3297 return count_trailing_zeros (val);
3298 if (BITS_WORD_MAX <= ULONG_MAX)
3299 return count_trailing_zeros_l (val);
3300 else
3302 int count;
3303 for (count = 0;
3304 count < BITS_PER_BITS_WORD - ULL_WIDTH;
3305 count += ULL_WIDTH)
3307 if (val & ULL_MAX)
3308 return count + count_trailing_zeros_ll (val);
3309 val = shift_right_ull (val);
3312 if (BITS_PER_BITS_WORD % ULL_WIDTH != 0
3313 && BITS_WORD_MAX == (bits_word) -1)
3314 val |= (bits_word) 1 << pre_value (ULONG_MAX < BITS_WORD_MAX,
3315 BITS_PER_BITS_WORD % ULL_WIDTH);
3316 return count + count_trailing_zeros_ll (val);
3320 static bits_word
3321 bits_word_to_host_endian (bits_word val)
3323 #ifndef WORDS_BIGENDIAN
3324 return val;
3325 #else
3326 if (BITS_WORD_MAX >> 31 == 1)
3327 return bswap_32 (val);
3328 # if HAVE_UNSIGNED_LONG_LONG
3329 if (BITS_WORD_MAX >> 31 >> 31 >> 1 == 1)
3330 return bswap_64 (val);
3331 # endif
3333 int i;
3334 bits_word r = 0;
3335 for (i = 0; i < sizeof val; i++)
3337 r = ((r << 1 << (CHAR_BIT - 1))
3338 | (val & ((1u << 1 << (CHAR_BIT - 1)) - 1)));
3339 val = val >> 1 >> (CHAR_BIT - 1);
3341 return r;
3343 #endif
3346 DEFUN ("bool-vector-exclusive-or", Fbool_vector_exclusive_or,
3347 Sbool_vector_exclusive_or, 2, 3, 0,
3348 doc: /* Return A ^ B, bitwise exclusive or.
3349 If optional third argument C is given, store result into C.
3350 A, B, and C must be bool vectors of the same length.
3351 Return the destination vector if it changed or nil otherwise. */)
3352 (Lisp_Object a, Lisp_Object b, Lisp_Object c)
3354 return bool_vector_binop_driver (a, b, c, bool_vector_exclusive_or);
3357 DEFUN ("bool-vector-union", Fbool_vector_union,
3358 Sbool_vector_union, 2, 3, 0,
3359 doc: /* Return A | B, bitwise or.
3360 If optional third argument C is given, store result into C.
3361 A, B, and C must be bool vectors of the same length.
3362 Return the destination vector if it changed or nil otherwise. */)
3363 (Lisp_Object a, Lisp_Object b, Lisp_Object c)
3365 return bool_vector_binop_driver (a, b, c, bool_vector_union);
3368 DEFUN ("bool-vector-intersection", Fbool_vector_intersection,
3369 Sbool_vector_intersection, 2, 3, 0,
3370 doc: /* Return A & B, bitwise and.
3371 If optional third argument C is given, store result into C.
3372 A, B, and C must be bool vectors of the same length.
3373 Return the destination vector if it changed or nil otherwise. */)
3374 (Lisp_Object a, Lisp_Object b, Lisp_Object c)
3376 return bool_vector_binop_driver (a, b, c, bool_vector_intersection);
3379 DEFUN ("bool-vector-set-difference", Fbool_vector_set_difference,
3380 Sbool_vector_set_difference, 2, 3, 0,
3381 doc: /* Return A &~ B, set difference.
3382 If optional third argument C is given, store result into C.
3383 A, B, and C must be bool vectors of the same length.
3384 Return the destination vector if it changed or nil otherwise. */)
3385 (Lisp_Object a, Lisp_Object b, Lisp_Object c)
3387 return bool_vector_binop_driver (a, b, c, bool_vector_set_difference);
3390 DEFUN ("bool-vector-subsetp", Fbool_vector_subsetp,
3391 Sbool_vector_subsetp, 2, 2, 0,
3392 doc: /* Return t if every t value in A is also t in B, nil otherwise.
3393 A and B must be bool vectors of the same length. */)
3394 (Lisp_Object a, Lisp_Object b)
3396 return bool_vector_binop_driver (a, b, b, bool_vector_subsetp);
3399 DEFUN ("bool-vector-not", Fbool_vector_not,
3400 Sbool_vector_not, 1, 2, 0,
3401 doc: /* Compute ~A, set complement.
3402 If optional second argument B is given, store result into B.
3403 A and B must be bool vectors of the same length.
3404 Return the destination vector. */)
3405 (Lisp_Object a, Lisp_Object b)
3407 EMACS_INT nr_bits;
3408 bits_word *bdata, *adata;
3409 ptrdiff_t i;
3411 CHECK_BOOL_VECTOR (a);
3412 nr_bits = bool_vector_size (a);
3414 if (NILP (b))
3415 b = make_uninit_bool_vector (nr_bits);
3416 else
3418 CHECK_BOOL_VECTOR (b);
3419 if (bool_vector_size (b) != nr_bits)
3420 wrong_length_argument (a, b, Qnil);
3423 bdata = bool_vector_data (b);
3424 adata = bool_vector_data (a);
3426 for (i = 0; i < nr_bits / BITS_PER_BITS_WORD; i++)
3427 bdata[i] = BITS_WORD_MAX & ~adata[i];
3429 if (nr_bits % BITS_PER_BITS_WORD)
3431 bits_word mword = bits_word_to_host_endian (adata[i]);
3432 mword = ~mword;
3433 mword &= bool_vector_spare_mask (nr_bits);
3434 bdata[i] = bits_word_to_host_endian (mword);
3437 return b;
3440 DEFUN ("bool-vector-count-population", Fbool_vector_count_population,
3441 Sbool_vector_count_population, 1, 1, 0,
3442 doc: /* Count how many elements in A are t.
3443 A is a bool vector. To count A's nil elements, subtract the return
3444 value from A's length. */)
3445 (Lisp_Object a)
3447 EMACS_INT count;
3448 EMACS_INT nr_bits;
3449 bits_word *adata;
3450 ptrdiff_t i, nwords;
3452 CHECK_BOOL_VECTOR (a);
3454 nr_bits = bool_vector_size (a);
3455 nwords = bool_vector_words (nr_bits);
3456 count = 0;
3457 adata = bool_vector_data (a);
3459 for (i = 0; i < nwords; i++)
3460 count += count_one_bits_word (adata[i]);
3462 return make_number (count);
3465 DEFUN ("bool-vector-count-consecutive", Fbool_vector_count_consecutive,
3466 Sbool_vector_count_consecutive, 3, 3, 0,
3467 doc: /* Count how many consecutive elements in A equal B starting at I.
3468 A is a bool vector, B is t or nil, and I is an index into A. */)
3469 (Lisp_Object a, Lisp_Object b, Lisp_Object i)
3471 EMACS_INT count;
3472 EMACS_INT nr_bits;
3473 int offset;
3474 bits_word *adata;
3475 bits_word twiddle;
3476 bits_word mword; /* Machine word. */
3477 ptrdiff_t pos, pos0;
3478 ptrdiff_t nr_words;
3480 CHECK_BOOL_VECTOR (a);
3481 CHECK_NATNUM (i);
3483 nr_bits = bool_vector_size (a);
3484 if (XFASTINT (i) > nr_bits) /* Allow one past the end for convenience */
3485 args_out_of_range (a, i);
3487 adata = bool_vector_data (a);
3488 nr_words = bool_vector_words (nr_bits);
3489 pos = XFASTINT (i) / BITS_PER_BITS_WORD;
3490 offset = XFASTINT (i) % BITS_PER_BITS_WORD;
3491 count = 0;
3493 /* By XORing with twiddle, we transform the problem of "count
3494 consecutive equal values" into "count the zero bits". The latter
3495 operation usually has hardware support. */
3496 twiddle = NILP (b) ? 0 : BITS_WORD_MAX;
3498 /* Scan the remainder of the mword at the current offset. */
3499 if (pos < nr_words && offset != 0)
3501 mword = bits_word_to_host_endian (adata[pos]);
3502 mword ^= twiddle;
3503 mword >>= offset;
3505 /* Do not count the pad bits. */
3506 mword |= (bits_word) 1 << (BITS_PER_BITS_WORD - offset);
3508 count = count_trailing_zero_bits (mword);
3509 pos++;
3510 if (count + offset < BITS_PER_BITS_WORD)
3511 return make_number (count);
3514 /* Scan whole words until we either reach the end of the vector or
3515 find an mword that doesn't completely match. twiddle is
3516 endian-independent. */
3517 pos0 = pos;
3518 while (pos < nr_words && adata[pos] == twiddle)
3519 pos++;
3520 count += (pos - pos0) * BITS_PER_BITS_WORD;
3522 if (pos < nr_words)
3524 /* If we stopped because of a mismatch, see how many bits match
3525 in the current mword. */
3526 mword = bits_word_to_host_endian (adata[pos]);
3527 mword ^= twiddle;
3528 count += count_trailing_zero_bits (mword);
3530 else if (nr_bits % BITS_PER_BITS_WORD != 0)
3532 /* If we hit the end, we might have overshot our count. Reduce
3533 the total by the number of spare bits at the end of the
3534 vector. */
3535 count -= BITS_PER_BITS_WORD - nr_bits % BITS_PER_BITS_WORD;
3538 return make_number (count);
3542 void
3543 syms_of_data (void)
3545 Lisp_Object error_tail, arith_tail;
3547 DEFSYM (Qquote, "quote");
3548 DEFSYM (Qlambda, "lambda");
3549 DEFSYM (Qsubr, "subr");
3550 DEFSYM (Qerror_conditions, "error-conditions");
3551 DEFSYM (Qerror_message, "error-message");
3552 DEFSYM (Qtop_level, "top-level");
3554 DEFSYM (Qerror, "error");
3555 DEFSYM (Quser_error, "user-error");
3556 DEFSYM (Qquit, "quit");
3557 DEFSYM (Qwrong_length_argument, "wrong-length-argument");
3558 DEFSYM (Qwrong_type_argument, "wrong-type-argument");
3559 DEFSYM (Qargs_out_of_range, "args-out-of-range");
3560 DEFSYM (Qvoid_function, "void-function");
3561 DEFSYM (Qcyclic_function_indirection, "cyclic-function-indirection");
3562 DEFSYM (Qcyclic_variable_indirection, "cyclic-variable-indirection");
3563 DEFSYM (Qvoid_variable, "void-variable");
3564 DEFSYM (Qsetting_constant, "setting-constant");
3565 DEFSYM (Qtrapping_constant, "trapping-constant");
3566 DEFSYM (Qinvalid_read_syntax, "invalid-read-syntax");
3568 DEFSYM (Qinvalid_function, "invalid-function");
3569 DEFSYM (Qwrong_number_of_arguments, "wrong-number-of-arguments");
3570 DEFSYM (Qno_catch, "no-catch");
3571 DEFSYM (Qend_of_file, "end-of-file");
3572 DEFSYM (Qarith_error, "arith-error");
3573 DEFSYM (Qbeginning_of_buffer, "beginning-of-buffer");
3574 DEFSYM (Qend_of_buffer, "end-of-buffer");
3575 DEFSYM (Qbuffer_read_only, "buffer-read-only");
3576 DEFSYM (Qtext_read_only, "text-read-only");
3577 DEFSYM (Qmark_inactive, "mark-inactive");
3579 DEFSYM (Qlistp, "listp");
3580 DEFSYM (Qconsp, "consp");
3581 DEFSYM (Qsymbolp, "symbolp");
3582 DEFSYM (Qintegerp, "integerp");
3583 DEFSYM (Qnatnump, "natnump");
3584 DEFSYM (Qwholenump, "wholenump");
3585 DEFSYM (Qstringp, "stringp");
3586 DEFSYM (Qarrayp, "arrayp");
3587 DEFSYM (Qsequencep, "sequencep");
3588 DEFSYM (Qbufferp, "bufferp");
3589 DEFSYM (Qvectorp, "vectorp");
3590 DEFSYM (Qbool_vector_p, "bool-vector-p");
3591 DEFSYM (Qchar_or_string_p, "char-or-string-p");
3592 DEFSYM (Qmarkerp, "markerp");
3593 #ifdef HAVE_MODULES
3594 DEFSYM (Quser_ptrp, "user-ptrp");
3595 #endif
3596 DEFSYM (Qbuffer_or_string_p, "buffer-or-string-p");
3597 DEFSYM (Qinteger_or_marker_p, "integer-or-marker-p");
3598 DEFSYM (Qfboundp, "fboundp");
3600 DEFSYM (Qfloatp, "floatp");
3601 DEFSYM (Qnumberp, "numberp");
3602 DEFSYM (Qnumber_or_marker_p, "number-or-marker-p");
3604 DEFSYM (Qchar_table_p, "char-table-p");
3605 DEFSYM (Qvector_or_char_table_p, "vector-or-char-table-p");
3607 DEFSYM (Qsubrp, "subrp");
3608 DEFSYM (Qunevalled, "unevalled");
3609 DEFSYM (Qmany, "many");
3611 DEFSYM (Qcdr, "cdr");
3613 error_tail = pure_cons (Qerror, Qnil);
3615 /* ERROR is used as a signaler for random errors for which nothing else is
3616 right. */
3618 Fput (Qerror, Qerror_conditions,
3619 error_tail);
3620 Fput (Qerror, Qerror_message,
3621 build_pure_c_string ("error"));
3623 #define PUT_ERROR(sym, tail, msg) \
3624 Fput (sym, Qerror_conditions, pure_cons (sym, tail)); \
3625 Fput (sym, Qerror_message, build_pure_c_string (msg))
3627 PUT_ERROR (Qquit, Qnil, "Quit");
3629 PUT_ERROR (Quser_error, error_tail, "");
3630 PUT_ERROR (Qwrong_length_argument, error_tail, "Wrong length argument");
3631 PUT_ERROR (Qwrong_type_argument, error_tail, "Wrong type argument");
3632 PUT_ERROR (Qargs_out_of_range, error_tail, "Args out of range");
3633 PUT_ERROR (Qvoid_function, error_tail,
3634 "Symbol's function definition is void");
3635 PUT_ERROR (Qcyclic_function_indirection, error_tail,
3636 "Symbol's chain of function indirections contains a loop");
3637 PUT_ERROR (Qcyclic_variable_indirection, error_tail,
3638 "Symbol's chain of variable indirections contains a loop");
3639 DEFSYM (Qcircular_list, "circular-list");
3640 PUT_ERROR (Qcircular_list, error_tail, "List contains a loop");
3641 PUT_ERROR (Qvoid_variable, error_tail, "Symbol's value as variable is void");
3642 PUT_ERROR (Qsetting_constant, error_tail,
3643 "Attempt to set a constant symbol");
3644 PUT_ERROR (Qtrapping_constant, error_tail,
3645 "Attempt to trap writes to a constant symbol");
3646 PUT_ERROR (Qinvalid_read_syntax, error_tail, "Invalid read syntax");
3647 PUT_ERROR (Qinvalid_function, error_tail, "Invalid function");
3648 PUT_ERROR (Qwrong_number_of_arguments, error_tail,
3649 "Wrong number of arguments");
3650 PUT_ERROR (Qno_catch, error_tail, "No catch for tag");
3651 PUT_ERROR (Qend_of_file, error_tail, "End of file during parsing");
3653 arith_tail = pure_cons (Qarith_error, error_tail);
3654 Fput (Qarith_error, Qerror_conditions, arith_tail);
3655 Fput (Qarith_error, Qerror_message, build_pure_c_string ("Arithmetic error"));
3657 PUT_ERROR (Qbeginning_of_buffer, error_tail, "Beginning of buffer");
3658 PUT_ERROR (Qend_of_buffer, error_tail, "End of buffer");
3659 PUT_ERROR (Qbuffer_read_only, error_tail, "Buffer is read-only");
3660 PUT_ERROR (Qtext_read_only, pure_cons (Qbuffer_read_only, error_tail),
3661 "Text is read-only");
3663 DEFSYM (Qrange_error, "range-error");
3664 DEFSYM (Qdomain_error, "domain-error");
3665 DEFSYM (Qsingularity_error, "singularity-error");
3666 DEFSYM (Qoverflow_error, "overflow-error");
3667 DEFSYM (Qunderflow_error, "underflow-error");
3669 PUT_ERROR (Qdomain_error, arith_tail, "Arithmetic domain error");
3671 PUT_ERROR (Qrange_error, arith_tail, "Arithmetic range error");
3673 PUT_ERROR (Qsingularity_error, Fcons (Qdomain_error, arith_tail),
3674 "Arithmetic singularity error");
3676 PUT_ERROR (Qoverflow_error, Fcons (Qdomain_error, arith_tail),
3677 "Arithmetic overflow error");
3678 PUT_ERROR (Qunderflow_error, Fcons (Qdomain_error, arith_tail),
3679 "Arithmetic underflow error");
3681 /* Types that type-of returns. */
3682 DEFSYM (Qinteger, "integer");
3683 DEFSYM (Qsymbol, "symbol");
3684 DEFSYM (Qstring, "string");
3685 DEFSYM (Qcons, "cons");
3686 DEFSYM (Qmarker, "marker");
3687 DEFSYM (Qoverlay, "overlay");
3688 DEFSYM (Qfinalizer, "finalizer");
3689 #ifdef HAVE_MODULES
3690 DEFSYM (Quser_ptr, "user-ptr");
3691 #endif
3692 DEFSYM (Qfloat, "float");
3693 DEFSYM (Qwindow_configuration, "window-configuration");
3694 DEFSYM (Qprocess, "process");
3695 DEFSYM (Qwindow, "window");
3696 DEFSYM (Qcompiled_function, "compiled-function");
3697 DEFSYM (Qbuffer, "buffer");
3698 DEFSYM (Qframe, "frame");
3699 DEFSYM (Qvector, "vector");
3700 DEFSYM (Qchar_table, "char-table");
3701 DEFSYM (Qbool_vector, "bool-vector");
3702 DEFSYM (Qhash_table, "hash-table");
3703 DEFSYM (Qthread, "thread");
3704 DEFSYM (Qmutex, "mutex");
3705 DEFSYM (Qcondition_variable, "condition-variable");
3707 DEFSYM (Qdefun, "defun");
3709 DEFSYM (Qfont_spec, "font-spec");
3710 DEFSYM (Qfont_entity, "font-entity");
3711 DEFSYM (Qfont_object, "font-object");
3713 DEFSYM (Qinteractive_form, "interactive-form");
3714 DEFSYM (Qdefalias_fset_function, "defalias-fset-function");
3716 defsubr (&Sindirect_variable);
3717 defsubr (&Sinteractive_form);
3718 defsubr (&Seq);
3719 defsubr (&Snull);
3720 defsubr (&Stype_of);
3721 defsubr (&Slistp);
3722 defsubr (&Snlistp);
3723 defsubr (&Sconsp);
3724 defsubr (&Satom);
3725 defsubr (&Sintegerp);
3726 defsubr (&Sinteger_or_marker_p);
3727 defsubr (&Snumberp);
3728 defsubr (&Snumber_or_marker_p);
3729 defsubr (&Sfloatp);
3730 defsubr (&Snatnump);
3731 defsubr (&Ssymbolp);
3732 defsubr (&Skeywordp);
3733 defsubr (&Sstringp);
3734 defsubr (&Smultibyte_string_p);
3735 defsubr (&Svectorp);
3736 defsubr (&Schar_table_p);
3737 defsubr (&Svector_or_char_table_p);
3738 defsubr (&Sbool_vector_p);
3739 defsubr (&Sarrayp);
3740 defsubr (&Ssequencep);
3741 defsubr (&Sbufferp);
3742 defsubr (&Smarkerp);
3743 defsubr (&Ssubrp);
3744 defsubr (&Sbyte_code_function_p);
3745 defsubr (&Schar_or_string_p);
3746 defsubr (&Sthreadp);
3747 defsubr (&Smutexp);
3748 defsubr (&Scondition_variable_p);
3749 defsubr (&Scar);
3750 defsubr (&Scdr);
3751 defsubr (&Scar_safe);
3752 defsubr (&Scdr_safe);
3753 defsubr (&Ssetcar);
3754 defsubr (&Ssetcdr);
3755 defsubr (&Ssymbol_function);
3756 defsubr (&Sindirect_function);
3757 defsubr (&Ssymbol_plist);
3758 defsubr (&Ssymbol_name);
3759 defsubr (&Smakunbound);
3760 defsubr (&Sfmakunbound);
3761 defsubr (&Sboundp);
3762 defsubr (&Sfboundp);
3763 defsubr (&Sfset);
3764 defsubr (&Sdefalias);
3765 defsubr (&Ssetplist);
3766 defsubr (&Ssymbol_value);
3767 defsubr (&Sset);
3768 defsubr (&Sdefault_boundp);
3769 defsubr (&Sdefault_value);
3770 defsubr (&Sset_default);
3771 defsubr (&Ssetq_default);
3772 defsubr (&Smake_variable_buffer_local);
3773 defsubr (&Smake_local_variable);
3774 defsubr (&Skill_local_variable);
3775 defsubr (&Slocal_variable_p);
3776 defsubr (&Slocal_variable_if_set_p);
3777 defsubr (&Svariable_binding_locus);
3778 #if 0 /* XXX Remove this. --lorentey */
3779 defsubr (&Sterminal_local_value);
3780 defsubr (&Sset_terminal_local_value);
3781 #endif
3782 defsubr (&Saref);
3783 defsubr (&Saset);
3784 defsubr (&Snumber_to_string);
3785 defsubr (&Sstring_to_number);
3786 defsubr (&Seqlsign);
3787 defsubr (&Slss);
3788 defsubr (&Sgtr);
3789 defsubr (&Sleq);
3790 defsubr (&Sgeq);
3791 defsubr (&Sneq);
3792 defsubr (&Splus);
3793 defsubr (&Sminus);
3794 defsubr (&Stimes);
3795 defsubr (&Squo);
3796 defsubr (&Srem);
3797 defsubr (&Smod);
3798 defsubr (&Smax);
3799 defsubr (&Smin);
3800 defsubr (&Slogand);
3801 defsubr (&Slogior);
3802 defsubr (&Slogxor);
3803 defsubr (&Slsh);
3804 defsubr (&Sash);
3805 defsubr (&Sadd1);
3806 defsubr (&Ssub1);
3807 defsubr (&Slognot);
3808 defsubr (&Sbyteorder);
3809 defsubr (&Ssubr_arity);
3810 defsubr (&Ssubr_name);
3811 #ifdef HAVE_MODULES
3812 defsubr (&Suser_ptrp);
3813 #endif
3815 defsubr (&Sbool_vector_exclusive_or);
3816 defsubr (&Sbool_vector_union);
3817 defsubr (&Sbool_vector_intersection);
3818 defsubr (&Sbool_vector_set_difference);
3819 defsubr (&Sbool_vector_not);
3820 defsubr (&Sbool_vector_subsetp);
3821 defsubr (&Sbool_vector_count_consecutive);
3822 defsubr (&Sbool_vector_count_population);
3824 set_symbol_function (Qwholenump, XSYMBOL (Qnatnump)->function);
3826 DEFVAR_LISP ("most-positive-fixnum", Vmost_positive_fixnum,
3827 doc: /* The largest value that is representable in a Lisp integer. */);
3828 Vmost_positive_fixnum = make_number (MOST_POSITIVE_FIXNUM);
3829 make_symbol_constant (intern_c_string ("most-positive-fixnum"));
3831 DEFVAR_LISP ("most-negative-fixnum", Vmost_negative_fixnum,
3832 doc: /* The smallest value that is representable in a Lisp integer. */);
3833 Vmost_negative_fixnum = make_number (MOST_NEGATIVE_FIXNUM);
3834 make_symbol_constant (intern_c_string ("most-negative-fixnum"));
3836 DEFSYM (Qwatchers, "watchers");
3837 DEFSYM (Qmakunbound, "makunbound");
3838 DEFSYM (Qunlet, "unlet");
3839 DEFSYM (Qset, "set");
3840 DEFSYM (Qset_default, "set-default");
3841 defsubr (&Sadd_variable_watcher);
3842 defsubr (&Sremove_variable_watcher);
3843 defsubr (&Sget_variable_watchers);