Fix autorevert-tests on MS-Windows
[emacs.git] / src / data.c
blob8e07bf01b44d2e5032771ac31ff22865d51c3927
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);
174 /* Data type predicates. */
176 DEFUN ("eq", Feq, Seq, 2, 2, 0,
177 doc: /* Return t if the two args are the same Lisp object. */
178 attributes: const)
179 (Lisp_Object obj1, Lisp_Object obj2)
181 if (EQ (obj1, obj2))
182 return Qt;
183 return Qnil;
186 DEFUN ("null", Fnull, Snull, 1, 1, 0,
187 doc: /* Return t if OBJECT is nil, and return nil otherwise. */
188 attributes: const)
189 (Lisp_Object object)
191 if (NILP (object))
192 return Qt;
193 return Qnil;
196 DEFUN ("type-of", Ftype_of, Stype_of, 1, 1, 0,
197 doc: /* Return a symbol representing the type of OBJECT.
198 The symbol returned names the object's basic type;
199 for example, (type-of 1) returns `integer'. */)
200 (Lisp_Object object)
202 switch (XTYPE (object))
204 case_Lisp_Int:
205 return Qinteger;
207 case Lisp_Symbol:
208 return Qsymbol;
210 case Lisp_String:
211 return Qstring;
213 case Lisp_Cons:
214 return Qcons;
216 case Lisp_Misc:
217 switch (XMISCTYPE (object))
219 case Lisp_Misc_Marker:
220 return Qmarker;
221 case Lisp_Misc_Overlay:
222 return Qoverlay;
223 case Lisp_Misc_Float:
224 return Qfloat;
225 case Lisp_Misc_Finalizer:
226 return Qfinalizer;
227 #ifdef HAVE_MODULES
228 case Lisp_Misc_User_Ptr:
229 return Quser_ptr;
230 #endif
231 default:
232 emacs_abort ();
235 case Lisp_Vectorlike:
236 if (WINDOW_CONFIGURATIONP (object))
237 return Qwindow_configuration;
238 if (PROCESSP (object))
239 return Qprocess;
240 if (WINDOWP (object))
241 return Qwindow;
242 if (SUBRP (object))
243 return Qsubr;
244 if (COMPILEDP (object))
245 return Qcompiled_function;
246 if (BUFFERP (object))
247 return Qbuffer;
248 if (CHAR_TABLE_P (object))
249 return Qchar_table;
250 if (BOOL_VECTOR_P (object))
251 return Qbool_vector;
252 if (FRAMEP (object))
253 return Qframe;
254 if (HASH_TABLE_P (object))
255 return Qhash_table;
256 if (FONT_SPEC_P (object))
257 return Qfont_spec;
258 if (FONT_ENTITY_P (object))
259 return Qfont_entity;
260 if (FONT_OBJECT_P (object))
261 return Qfont_object;
262 if (THREADP (object))
263 return Qthread;
264 if (MUTEXP (object))
265 return Qmutex;
266 if (CONDVARP (object))
267 return Qcondition_variable;
268 return Qvector;
270 case Lisp_Float:
271 return Qfloat;
273 default:
274 emacs_abort ();
278 DEFUN ("consp", Fconsp, Sconsp, 1, 1, 0,
279 doc: /* Return t if OBJECT is a cons cell. */
280 attributes: const)
281 (Lisp_Object object)
283 if (CONSP (object))
284 return Qt;
285 return Qnil;
288 DEFUN ("atom", Fatom, Satom, 1, 1, 0,
289 doc: /* Return t if OBJECT is not a cons cell. This includes nil. */
290 attributes: const)
291 (Lisp_Object object)
293 if (CONSP (object))
294 return Qnil;
295 return Qt;
298 DEFUN ("listp", Flistp, Slistp, 1, 1, 0,
299 doc: /* Return t if OBJECT is a list, that is, a cons cell or nil.
300 Otherwise, return nil. */
301 attributes: const)
302 (Lisp_Object object)
304 if (CONSP (object) || NILP (object))
305 return Qt;
306 return Qnil;
309 DEFUN ("nlistp", Fnlistp, Snlistp, 1, 1, 0,
310 doc: /* Return t if OBJECT is not a list. Lists include nil. */
311 attributes: const)
312 (Lisp_Object object)
314 if (CONSP (object) || NILP (object))
315 return Qnil;
316 return Qt;
319 DEFUN ("symbolp", Fsymbolp, Ssymbolp, 1, 1, 0,
320 doc: /* Return t if OBJECT is a symbol. */
321 attributes: const)
322 (Lisp_Object object)
324 if (SYMBOLP (object))
325 return Qt;
326 return Qnil;
329 /* Define this in C to avoid unnecessarily consing up the symbol
330 name. */
331 DEFUN ("keywordp", Fkeywordp, Skeywordp, 1, 1, 0,
332 doc: /* Return t if OBJECT is a keyword.
333 This means that it is a symbol with a print name beginning with `:'
334 interned in the initial obarray. */)
335 (Lisp_Object object)
337 if (SYMBOLP (object)
338 && SREF (SYMBOL_NAME (object), 0) == ':'
339 && SYMBOL_INTERNED_IN_INITIAL_OBARRAY_P (object))
340 return Qt;
341 return Qnil;
344 DEFUN ("vectorp", Fvectorp, Svectorp, 1, 1, 0,
345 doc: /* Return t if OBJECT is a vector. */)
346 (Lisp_Object object)
348 if (VECTORP (object))
349 return Qt;
350 return Qnil;
353 DEFUN ("stringp", Fstringp, Sstringp, 1, 1, 0,
354 doc: /* Return t if OBJECT is a string. */
355 attributes: const)
356 (Lisp_Object object)
358 if (STRINGP (object))
359 return Qt;
360 return Qnil;
363 DEFUN ("multibyte-string-p", Fmultibyte_string_p, Smultibyte_string_p,
364 1, 1, 0,
365 doc: /* Return t if OBJECT is a multibyte string.
366 Return nil if OBJECT is either a unibyte string, or not a string. */)
367 (Lisp_Object object)
369 if (STRINGP (object) && STRING_MULTIBYTE (object))
370 return Qt;
371 return Qnil;
374 DEFUN ("char-table-p", Fchar_table_p, Schar_table_p, 1, 1, 0,
375 doc: /* Return t if OBJECT is a char-table. */)
376 (Lisp_Object object)
378 if (CHAR_TABLE_P (object))
379 return Qt;
380 return Qnil;
383 DEFUN ("vector-or-char-table-p", Fvector_or_char_table_p,
384 Svector_or_char_table_p, 1, 1, 0,
385 doc: /* Return t if OBJECT is a char-table or vector. */)
386 (Lisp_Object object)
388 if (VECTORP (object) || CHAR_TABLE_P (object))
389 return Qt;
390 return Qnil;
393 DEFUN ("bool-vector-p", Fbool_vector_p, Sbool_vector_p, 1, 1, 0,
394 doc: /* Return t if OBJECT is a bool-vector. */)
395 (Lisp_Object object)
397 if (BOOL_VECTOR_P (object))
398 return Qt;
399 return Qnil;
402 DEFUN ("arrayp", Farrayp, Sarrayp, 1, 1, 0,
403 doc: /* Return t if OBJECT is an array (string or vector). */)
404 (Lisp_Object object)
406 if (ARRAYP (object))
407 return Qt;
408 return Qnil;
411 DEFUN ("sequencep", Fsequencep, Ssequencep, 1, 1, 0,
412 doc: /* Return t if OBJECT is a sequence (list or array). */)
413 (register Lisp_Object object)
415 if (CONSP (object) || NILP (object) || ARRAYP (object))
416 return Qt;
417 return Qnil;
420 DEFUN ("bufferp", Fbufferp, Sbufferp, 1, 1, 0,
421 doc: /* Return t if OBJECT is an editor buffer. */)
422 (Lisp_Object object)
424 if (BUFFERP (object))
425 return Qt;
426 return Qnil;
429 DEFUN ("markerp", Fmarkerp, Smarkerp, 1, 1, 0,
430 doc: /* Return t if OBJECT is a marker (editor pointer). */)
431 (Lisp_Object object)
433 if (MARKERP (object))
434 return Qt;
435 return Qnil;
438 #ifdef HAVE_MODULES
439 DEFUN ("user-ptrp", Fuser_ptrp, Suser_ptrp, 1, 1, 0,
440 doc: /* Return t if OBJECT is a module user pointer. */)
441 (Lisp_Object object)
443 if (USER_PTRP (object))
444 return Qt;
445 return Qnil;
447 #endif
449 DEFUN ("subrp", Fsubrp, Ssubrp, 1, 1, 0,
450 doc: /* Return t if OBJECT is a built-in function. */)
451 (Lisp_Object object)
453 if (SUBRP (object))
454 return Qt;
455 return Qnil;
458 DEFUN ("byte-code-function-p", Fbyte_code_function_p, Sbyte_code_function_p,
459 1, 1, 0,
460 doc: /* Return t if OBJECT is a byte-compiled function object. */)
461 (Lisp_Object object)
463 if (COMPILEDP (object))
464 return Qt;
465 return Qnil;
468 DEFUN ("char-or-string-p", Fchar_or_string_p, Schar_or_string_p, 1, 1, 0,
469 doc: /* Return t if OBJECT is a character or a string. */
470 attributes: const)
471 (register Lisp_Object object)
473 if (CHARACTERP (object) || STRINGP (object))
474 return Qt;
475 return Qnil;
478 DEFUN ("integerp", Fintegerp, Sintegerp, 1, 1, 0,
479 doc: /* Return t if OBJECT is an integer. */
480 attributes: const)
481 (Lisp_Object object)
483 if (INTEGERP (object))
484 return Qt;
485 return Qnil;
488 DEFUN ("integer-or-marker-p", Finteger_or_marker_p, Sinteger_or_marker_p, 1, 1, 0,
489 doc: /* Return t if OBJECT is an integer or a marker (editor pointer). */)
490 (register Lisp_Object object)
492 if (MARKERP (object) || INTEGERP (object))
493 return Qt;
494 return Qnil;
497 DEFUN ("natnump", Fnatnump, Snatnump, 1, 1, 0,
498 doc: /* Return t if OBJECT is a nonnegative integer. */
499 attributes: const)
500 (Lisp_Object object)
502 if (NATNUMP (object))
503 return Qt;
504 return Qnil;
507 DEFUN ("numberp", Fnumberp, Snumberp, 1, 1, 0,
508 doc: /* Return t if OBJECT is a number (floating point or integer). */
509 attributes: const)
510 (Lisp_Object object)
512 if (NUMBERP (object))
513 return Qt;
514 else
515 return Qnil;
518 DEFUN ("number-or-marker-p", Fnumber_or_marker_p,
519 Snumber_or_marker_p, 1, 1, 0,
520 doc: /* Return t if OBJECT is a number or a marker. */)
521 (Lisp_Object object)
523 if (NUMBERP (object) || MARKERP (object))
524 return Qt;
525 return Qnil;
528 DEFUN ("floatp", Ffloatp, Sfloatp, 1, 1, 0,
529 doc: /* Return t if OBJECT is a floating point number. */
530 attributes: const)
531 (Lisp_Object object)
533 if (FLOATP (object))
534 return Qt;
535 return Qnil;
538 DEFUN ("threadp", Fthreadp, Sthreadp, 1, 1, 0,
539 doc: /* Return t if OBJECT is a thread. */)
540 (Lisp_Object object)
542 if (THREADP (object))
543 return Qt;
544 return Qnil;
547 DEFUN ("mutexp", Fmutexp, Smutexp, 1, 1, 0,
548 doc: /* Return t if OBJECT is a mutex. */)
549 (Lisp_Object object)
551 if (MUTEXP (object))
552 return Qt;
553 return Qnil;
556 DEFUN ("condition-variable-p", Fcondition_variable_p, Scondition_variable_p,
557 1, 1, 0,
558 doc: /* Return t if OBJECT is a condition variable. */)
559 (Lisp_Object object)
561 if (CONDVARP (object))
562 return Qt;
563 return Qnil;
566 /* Extract and set components of lists. */
568 DEFUN ("car", Fcar, Scar, 1, 1, 0,
569 doc: /* Return the car of LIST. If arg is nil, return nil.
570 Error if arg is not nil and not a cons cell. See also `car-safe'.
572 See Info node `(elisp)Cons Cells' for a discussion of related basic
573 Lisp concepts such as car, cdr, cons cell and list. */)
574 (register Lisp_Object list)
576 return CAR (list);
579 DEFUN ("car-safe", Fcar_safe, Scar_safe, 1, 1, 0,
580 doc: /* Return the car of OBJECT if it is a cons cell, or else nil. */)
581 (Lisp_Object object)
583 return CAR_SAFE (object);
586 DEFUN ("cdr", Fcdr, Scdr, 1, 1, 0,
587 doc: /* Return the cdr of LIST. If arg is nil, return nil.
588 Error if arg is not nil and not a cons cell. See also `cdr-safe'.
590 See Info node `(elisp)Cons Cells' for a discussion of related basic
591 Lisp concepts such as cdr, car, cons cell and list. */)
592 (register Lisp_Object list)
594 return CDR (list);
597 DEFUN ("cdr-safe", Fcdr_safe, Scdr_safe, 1, 1, 0,
598 doc: /* Return the cdr of OBJECT if it is a cons cell, or else nil. */)
599 (Lisp_Object object)
601 return CDR_SAFE (object);
604 DEFUN ("setcar", Fsetcar, Ssetcar, 2, 2, 0,
605 doc: /* Set the car of CELL to be NEWCAR. Returns NEWCAR. */)
606 (register Lisp_Object cell, Lisp_Object newcar)
608 CHECK_CONS (cell);
609 CHECK_IMPURE (cell, XCONS (cell));
610 XSETCAR (cell, newcar);
611 return newcar;
614 DEFUN ("setcdr", Fsetcdr, Ssetcdr, 2, 2, 0,
615 doc: /* Set the cdr of CELL to be NEWCDR. Returns NEWCDR. */)
616 (register Lisp_Object cell, Lisp_Object newcdr)
618 CHECK_CONS (cell);
619 CHECK_IMPURE (cell, XCONS (cell));
620 XSETCDR (cell, newcdr);
621 return newcdr;
624 /* Extract and set components of symbols. */
626 DEFUN ("boundp", Fboundp, Sboundp, 1, 1, 0,
627 doc: /* Return t if SYMBOL's value is not void.
628 Note that if `lexical-binding' is in effect, this refers to the
629 global value outside of any lexical scope. */)
630 (register Lisp_Object symbol)
632 Lisp_Object valcontents;
633 struct Lisp_Symbol *sym;
634 CHECK_SYMBOL (symbol);
635 sym = XSYMBOL (symbol);
637 start:
638 switch (sym->redirect)
640 case SYMBOL_PLAINVAL: valcontents = SYMBOL_VAL (sym); break;
641 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
642 case SYMBOL_LOCALIZED:
644 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
645 if (blv->fwd)
646 /* In set_internal, we un-forward vars when their value is
647 set to Qunbound. */
648 return Qt;
649 else
651 swap_in_symval_forwarding (sym, blv);
652 valcontents = blv_value (blv);
654 break;
656 case SYMBOL_FORWARDED:
657 /* In set_internal, we un-forward vars when their value is
658 set to Qunbound. */
659 return Qt;
660 default: emacs_abort ();
663 return (EQ (valcontents, Qunbound) ? Qnil : Qt);
666 /* FIXME: It has been previously suggested to make this function an
667 alias for symbol-function, but upon discussion at Bug#23957,
668 there is a risk breaking backward compatibility, as some users of
669 fboundp may expect `t' in particular, rather than any true
670 value. An alias is still welcome so long as the compatibility
671 issues are addressed. */
672 DEFUN ("fboundp", Ffboundp, Sfboundp, 1, 1, 0,
673 doc: /* Return t if SYMBOL's function definition is not void. */)
674 (register Lisp_Object symbol)
676 CHECK_SYMBOL (symbol);
677 return NILP (XSYMBOL (symbol)->function) ? Qnil : Qt;
680 DEFUN ("makunbound", Fmakunbound, Smakunbound, 1, 1, 0,
681 doc: /* Make SYMBOL's value be void.
682 Return SYMBOL. */)
683 (register Lisp_Object symbol)
685 CHECK_SYMBOL (symbol);
686 if (SYMBOL_CONSTANT_P (symbol))
687 xsignal1 (Qsetting_constant, symbol);
688 Fset (symbol, Qunbound);
689 return symbol;
692 DEFUN ("fmakunbound", Ffmakunbound, Sfmakunbound, 1, 1, 0,
693 doc: /* Make SYMBOL's function definition be nil.
694 Return SYMBOL. */)
695 (register Lisp_Object symbol)
697 CHECK_SYMBOL (symbol);
698 if (NILP (symbol) || EQ (symbol, Qt))
699 xsignal1 (Qsetting_constant, symbol);
700 set_symbol_function (symbol, Qnil);
701 return symbol;
704 DEFUN ("symbol-function", Fsymbol_function, Ssymbol_function, 1, 1, 0,
705 doc: /* Return SYMBOL's function definition, or nil if that is void. */)
706 (register Lisp_Object symbol)
708 CHECK_SYMBOL (symbol);
709 return XSYMBOL (symbol)->function;
712 DEFUN ("symbol-plist", Fsymbol_plist, Ssymbol_plist, 1, 1, 0,
713 doc: /* Return SYMBOL's property list. */)
714 (register Lisp_Object symbol)
716 CHECK_SYMBOL (symbol);
717 return XSYMBOL (symbol)->plist;
720 DEFUN ("symbol-name", Fsymbol_name, Ssymbol_name, 1, 1, 0,
721 doc: /* Return SYMBOL's name, a string. */)
722 (register Lisp_Object symbol)
724 register Lisp_Object name;
726 CHECK_SYMBOL (symbol);
727 name = SYMBOL_NAME (symbol);
728 return name;
731 DEFUN ("fset", Ffset, Sfset, 2, 2, 0,
732 doc: /* Set SYMBOL's function definition to DEFINITION, and return DEFINITION. */)
733 (register Lisp_Object symbol, Lisp_Object definition)
735 register Lisp_Object function;
736 CHECK_SYMBOL (symbol);
737 /* Perhaps not quite the right error signal, but seems good enough. */
738 if (NILP (symbol))
739 xsignal1 (Qsetting_constant, symbol);
741 function = XSYMBOL (symbol)->function;
743 if (!NILP (Vautoload_queue) && !NILP (function))
744 Vautoload_queue = Fcons (Fcons (symbol, function), Vautoload_queue);
746 if (AUTOLOADP (function))
747 Fput (symbol, Qautoload, XCDR (function));
749 /* Convert to eassert or remove after GC bug is found. In the
750 meantime, check unconditionally, at a slight perf hit. */
751 if (! valid_lisp_object_p (definition))
752 emacs_abort ();
754 set_symbol_function (symbol, definition);
756 return definition;
759 DEFUN ("defalias", Fdefalias, Sdefalias, 2, 3, 0,
760 doc: /* Set SYMBOL's function definition to DEFINITION.
761 Associates the function with the current load file, if any.
762 The optional third argument DOCSTRING specifies the documentation string
763 for SYMBOL; if it is omitted or nil, SYMBOL uses the documentation string
764 determined by DEFINITION.
766 Internally, this normally uses `fset', but if SYMBOL has a
767 `defalias-fset-function' property, the associated value is used instead.
769 The return value is undefined. */)
770 (register Lisp_Object symbol, Lisp_Object definition, Lisp_Object docstring)
772 CHECK_SYMBOL (symbol);
773 if (!NILP (Vpurify_flag)
774 /* If `definition' is a keymap, immutable (and copying) is wrong. */
775 && !KEYMAPP (definition))
776 definition = Fpurecopy (definition);
779 bool autoload = AUTOLOADP (definition);
780 if (NILP (Vpurify_flag) || !autoload)
781 { /* Only add autoload entries after dumping, because the ones before are
782 not useful and else we get loads of them from the loaddefs.el. */
784 if (AUTOLOADP (XSYMBOL (symbol)->function))
785 /* Remember that the function was already an autoload. */
786 LOADHIST_ATTACH (Fcons (Qt, symbol));
787 LOADHIST_ATTACH (Fcons (autoload ? Qautoload : Qdefun, symbol));
791 { /* Handle automatic advice activation. */
792 Lisp_Object hook = Fget (symbol, Qdefalias_fset_function);
793 if (!NILP (hook))
794 call2 (hook, symbol, definition);
795 else
796 Ffset (symbol, definition);
799 if (!NILP (docstring))
800 Fput (symbol, Qfunction_documentation, docstring);
801 /* We used to return `definition', but now that `defun' and `defmacro' expand
802 to a call to `defalias', we return `symbol' for backward compatibility
803 (bug#11686). */
804 return symbol;
807 DEFUN ("setplist", Fsetplist, Ssetplist, 2, 2, 0,
808 doc: /* Set SYMBOL's property list to NEWPLIST, and return NEWPLIST. */)
809 (register Lisp_Object symbol, Lisp_Object newplist)
811 CHECK_SYMBOL (symbol);
812 set_symbol_plist (symbol, newplist);
813 return newplist;
816 DEFUN ("subr-arity", Fsubr_arity, Ssubr_arity, 1, 1, 0,
817 doc: /* Return minimum and maximum number of args allowed for SUBR.
818 SUBR must be a built-in function.
819 The returned value is a pair (MIN . MAX). MIN is the minimum number
820 of args. MAX is the maximum number or the symbol `many', for a
821 function with `&rest' args, or `unevalled' for a special form. */)
822 (Lisp_Object subr)
824 short minargs, maxargs;
825 CHECK_SUBR (subr);
826 minargs = XSUBR (subr)->min_args;
827 maxargs = XSUBR (subr)->max_args;
828 return Fcons (make_number (minargs),
829 maxargs == MANY ? Qmany
830 : maxargs == UNEVALLED ? Qunevalled
831 : make_number (maxargs));
834 DEFUN ("subr-name", Fsubr_name, Ssubr_name, 1, 1, 0,
835 doc: /* Return name of subroutine SUBR.
836 SUBR must be a built-in function. */)
837 (Lisp_Object subr)
839 const char *name;
840 CHECK_SUBR (subr);
841 name = XSUBR (subr)->symbol_name;
842 return build_string (name);
845 DEFUN ("interactive-form", Finteractive_form, Sinteractive_form, 1, 1, 0,
846 doc: /* Return the interactive form of CMD or nil if none.
847 If CMD is not a command, the return value is nil.
848 Value, if non-nil, is a list (interactive SPEC). */)
849 (Lisp_Object cmd)
851 Lisp_Object fun = indirect_function (cmd); /* Check cycles. */
853 if (NILP (fun))
854 return Qnil;
856 /* Use an `interactive-form' property if present, analogous to the
857 function-documentation property. */
858 fun = cmd;
859 while (SYMBOLP (fun))
861 Lisp_Object tmp = Fget (fun, Qinteractive_form);
862 if (!NILP (tmp))
863 return tmp;
864 else
865 fun = Fsymbol_function (fun);
868 if (SUBRP (fun))
870 const char *spec = XSUBR (fun)->intspec;
871 if (spec)
872 return list2 (Qinteractive,
873 (*spec != '(') ? build_string (spec) :
874 Fcar (Fread_from_string (build_string (spec), Qnil, Qnil)));
876 else if (COMPILEDP (fun))
878 if ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE)
879 return list2 (Qinteractive, AREF (fun, COMPILED_INTERACTIVE));
881 else if (AUTOLOADP (fun))
882 return Finteractive_form (Fautoload_do_load (fun, cmd, Qnil));
883 else if (CONSP (fun))
885 Lisp_Object funcar = XCAR (fun);
886 if (EQ (funcar, Qclosure))
887 return Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun))));
888 else if (EQ (funcar, Qlambda))
889 return Fassq (Qinteractive, Fcdr (XCDR (fun)));
891 return Qnil;
895 /***********************************************************************
896 Getting and Setting Values of Symbols
897 ***********************************************************************/
899 /* Return the symbol holding SYMBOL's value. Signal
900 `cyclic-variable-indirection' if SYMBOL's chain of variable
901 indirections contains a loop. */
903 struct Lisp_Symbol *
904 indirect_variable (struct Lisp_Symbol *symbol)
906 struct Lisp_Symbol *tortoise, *hare;
908 hare = tortoise = symbol;
910 while (hare->redirect == SYMBOL_VARALIAS)
912 hare = SYMBOL_ALIAS (hare);
913 if (hare->redirect != SYMBOL_VARALIAS)
914 break;
916 hare = SYMBOL_ALIAS (hare);
917 tortoise = SYMBOL_ALIAS (tortoise);
919 if (hare == tortoise)
921 Lisp_Object tem;
922 XSETSYMBOL (tem, symbol);
923 xsignal1 (Qcyclic_variable_indirection, tem);
927 return hare;
931 DEFUN ("indirect-variable", Findirect_variable, Sindirect_variable, 1, 1, 0,
932 doc: /* Return the variable at the end of OBJECT's variable chain.
933 If OBJECT is a symbol, follow its variable indirections (if any), and
934 return the variable at the end of the chain of aliases. See Info node
935 `(elisp)Variable Aliases'.
937 If OBJECT is not a symbol, just return it. If there is a loop in the
938 chain of aliases, signal a `cyclic-variable-indirection' error. */)
939 (Lisp_Object object)
941 if (SYMBOLP (object))
943 struct Lisp_Symbol *sym = indirect_variable (XSYMBOL (object));
944 XSETSYMBOL (object, sym);
946 return object;
950 /* Given the raw contents of a symbol value cell,
951 return the Lisp value of the symbol.
952 This does not handle buffer-local variables; use
953 swap_in_symval_forwarding for that. */
955 Lisp_Object
956 do_symval_forwarding (register union Lisp_Fwd *valcontents)
958 register Lisp_Object val;
959 switch (XFWDTYPE (valcontents))
961 case Lisp_Fwd_Int:
962 XSETINT (val, *XINTFWD (valcontents)->intvar);
963 return val;
965 case Lisp_Fwd_Bool:
966 return (*XBOOLFWD (valcontents)->boolvar ? Qt : Qnil);
968 case Lisp_Fwd_Obj:
969 return *XOBJFWD (valcontents)->objvar;
971 case Lisp_Fwd_Buffer_Obj:
972 return per_buffer_value (current_buffer,
973 XBUFFER_OBJFWD (valcontents)->offset);
975 case Lisp_Fwd_Kboard_Obj:
976 /* We used to simply use current_kboard here, but from Lisp
977 code, its value is often unexpected. It seems nicer to
978 allow constructions like this to work as intuitively expected:
980 (with-selected-frame frame
981 (define-key local-function-map "\eOP" [f1]))
983 On the other hand, this affects the semantics of
984 last-command and real-last-command, and people may rely on
985 that. I took a quick look at the Lisp codebase, and I
986 don't think anything will break. --lorentey */
987 return *(Lisp_Object *)(XKBOARD_OBJFWD (valcontents)->offset
988 + (char *)FRAME_KBOARD (SELECTED_FRAME ()));
989 default: emacs_abort ();
993 /* Used to signal a user-friendly error when symbol WRONG is
994 not a member of CHOICE, which should be a list of symbols. */
996 void
997 wrong_choice (Lisp_Object choice, Lisp_Object wrong)
999 ptrdiff_t i = 0, len = XINT (Flength (choice));
1000 Lisp_Object obj, *args;
1001 AUTO_STRING (one_of, "One of ");
1002 AUTO_STRING (comma, ", ");
1003 AUTO_STRING (or, " or ");
1004 AUTO_STRING (should_be_specified, " should be specified");
1006 USE_SAFE_ALLOCA;
1007 SAFE_ALLOCA_LISP (args, len * 2 + 1);
1009 args[i++] = one_of;
1011 for (obj = choice; !NILP (obj); obj = XCDR (obj))
1013 args[i++] = SYMBOL_NAME (XCAR (obj));
1014 args[i++] = (NILP (XCDR (obj)) ? should_be_specified
1015 : NILP (XCDR (XCDR (obj))) ? or : comma);
1018 obj = Fconcat (i, args);
1019 SAFE_FREE ();
1020 xsignal2 (Qerror, obj, wrong);
1023 /* Used to signal a user-friendly error if WRONG is not a number or
1024 integer/floating-point number outsize of inclusive MIN..MAX range. */
1026 static void
1027 wrong_range (Lisp_Object min, Lisp_Object max, Lisp_Object wrong)
1029 AUTO_STRING (value_should_be_from, "Value should be from ");
1030 AUTO_STRING (to, " to ");
1031 xsignal2 (Qerror,
1032 CALLN (Fconcat, value_should_be_from, Fnumber_to_string (min),
1033 to, Fnumber_to_string (max)),
1034 wrong);
1037 /* Store NEWVAL into SYMBOL, where VALCONTENTS is found in the value cell
1038 of SYMBOL. If SYMBOL is buffer-local, VALCONTENTS should be the
1039 buffer-independent contents of the value cell: forwarded just one
1040 step past the buffer-localness.
1042 BUF non-zero means set the value in buffer BUF instead of the
1043 current buffer. This only plays a role for per-buffer variables. */
1045 static void
1046 store_symval_forwarding (union Lisp_Fwd *valcontents, register Lisp_Object newval, struct buffer *buf)
1048 switch (XFWDTYPE (valcontents))
1050 case Lisp_Fwd_Int:
1051 CHECK_NUMBER (newval);
1052 *XINTFWD (valcontents)->intvar = XINT (newval);
1053 break;
1055 case Lisp_Fwd_Bool:
1056 *XBOOLFWD (valcontents)->boolvar = !NILP (newval);
1057 break;
1059 case Lisp_Fwd_Obj:
1060 *XOBJFWD (valcontents)->objvar = newval;
1062 /* If this variable is a default for something stored
1063 in the buffer itself, such as default-fill-column,
1064 find the buffers that don't have local values for it
1065 and update them. */
1066 if (XOBJFWD (valcontents)->objvar > (Lisp_Object *) &buffer_defaults
1067 && XOBJFWD (valcontents)->objvar < (Lisp_Object *) (&buffer_defaults + 1))
1069 int offset = ((char *) XOBJFWD (valcontents)->objvar
1070 - (char *) &buffer_defaults);
1071 int idx = PER_BUFFER_IDX (offset);
1073 Lisp_Object tail, buf;
1075 if (idx <= 0)
1076 break;
1078 FOR_EACH_LIVE_BUFFER (tail, buf)
1080 struct buffer *b = XBUFFER (buf);
1082 if (! PER_BUFFER_VALUE_P (b, idx))
1083 set_per_buffer_value (b, offset, newval);
1086 break;
1088 case Lisp_Fwd_Buffer_Obj:
1090 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1091 Lisp_Object predicate = XBUFFER_OBJFWD (valcontents)->predicate;
1093 if (!NILP (newval))
1095 if (SYMBOLP (predicate))
1097 Lisp_Object prop;
1099 if ((prop = Fget (predicate, Qchoice), !NILP (prop)))
1101 if (NILP (Fmemq (newval, prop)))
1102 wrong_choice (prop, newval);
1104 else if ((prop = Fget (predicate, Qrange), !NILP (prop)))
1106 Lisp_Object min = XCAR (prop), max = XCDR (prop);
1108 if (!NUMBERP (newval)
1109 || !NILP (arithcompare (newval, min, ARITH_LESS))
1110 || !NILP (arithcompare (newval, max, ARITH_GRTR)))
1111 wrong_range (min, max, newval);
1113 else if (FUNCTIONP (predicate))
1115 if (NILP (call1 (predicate, newval)))
1116 wrong_type_argument (predicate, newval);
1120 if (buf == NULL)
1121 buf = current_buffer;
1122 set_per_buffer_value (buf, offset, newval);
1124 break;
1126 case Lisp_Fwd_Kboard_Obj:
1128 char *base = (char *) FRAME_KBOARD (SELECTED_FRAME ());
1129 char *p = base + XKBOARD_OBJFWD (valcontents)->offset;
1130 *(Lisp_Object *) p = newval;
1132 break;
1134 default:
1135 emacs_abort (); /* goto def; */
1139 /* Set up SYMBOL to refer to its global binding. This makes it safe
1140 to alter the status of other bindings. BEWARE: this may be called
1141 during the mark phase of GC, where we assume that Lisp_Object slots
1142 of BLV are marked after this function has changed them. */
1144 void
1145 swap_in_global_binding (struct Lisp_Symbol *symbol)
1147 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (symbol);
1149 /* Unload the previously loaded binding. */
1150 if (blv->fwd)
1151 set_blv_value (blv, do_symval_forwarding (blv->fwd));
1153 /* Select the global binding in the symbol. */
1154 set_blv_valcell (blv, blv->defcell);
1155 if (blv->fwd)
1156 store_symval_forwarding (blv->fwd, XCDR (blv->defcell), NULL);
1158 /* Indicate that the global binding is set up now. */
1159 set_blv_where (blv, Qnil);
1160 set_blv_found (blv, 0);
1163 /* Set up the buffer-local symbol SYMBOL for validity in the current buffer.
1164 VALCONTENTS is the contents of its value cell,
1165 which points to a struct Lisp_Buffer_Local_Value.
1167 Return the value forwarded one step past the buffer-local stage.
1168 This could be another forwarding pointer. */
1170 static void
1171 swap_in_symval_forwarding (struct Lisp_Symbol *symbol, struct Lisp_Buffer_Local_Value *blv)
1173 register Lisp_Object tem1;
1175 eassert (blv == SYMBOL_BLV (symbol));
1177 tem1 = blv->where;
1179 if (NILP (tem1)
1180 || current_buffer != XBUFFER (tem1))
1183 /* Unload the previously loaded binding. */
1184 tem1 = blv->valcell;
1185 if (blv->fwd)
1186 set_blv_value (blv, do_symval_forwarding (blv->fwd));
1187 /* Choose the new binding. */
1189 Lisp_Object var;
1190 XSETSYMBOL (var, symbol);
1191 tem1 = assq_no_quit (var, BVAR (current_buffer, local_var_alist));
1192 set_blv_where (blv, Fcurrent_buffer ());
1194 if (!(blv->found = !NILP (tem1)))
1195 tem1 = blv->defcell;
1197 /* Load the new binding. */
1198 set_blv_valcell (blv, tem1);
1199 if (blv->fwd)
1200 store_symval_forwarding (blv->fwd, blv_value (blv), NULL);
1204 /* Find the value of a symbol, returning Qunbound if it's not bound.
1205 This is helpful for code which just wants to get a variable's value
1206 if it has one, without signaling an error.
1207 Note that it must not be possible to quit
1208 within this function. Great care is required for this. */
1210 Lisp_Object
1211 find_symbol_value (Lisp_Object symbol)
1213 struct Lisp_Symbol *sym;
1215 CHECK_SYMBOL (symbol);
1216 sym = XSYMBOL (symbol);
1218 start:
1219 switch (sym->redirect)
1221 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1222 case SYMBOL_PLAINVAL: return SYMBOL_VAL (sym);
1223 case SYMBOL_LOCALIZED:
1225 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1226 swap_in_symval_forwarding (sym, blv);
1227 return blv->fwd ? do_symval_forwarding (blv->fwd) : blv_value (blv);
1229 /* FALLTHROUGH */
1230 case SYMBOL_FORWARDED:
1231 return do_symval_forwarding (SYMBOL_FWD (sym));
1232 default: emacs_abort ();
1236 DEFUN ("symbol-value", Fsymbol_value, Ssymbol_value, 1, 1, 0,
1237 doc: /* Return SYMBOL's value. Error if that is void.
1238 Note that if `lexical-binding' is in effect, this returns the
1239 global value outside of any lexical scope. */)
1240 (Lisp_Object symbol)
1242 Lisp_Object val;
1244 val = find_symbol_value (symbol);
1245 if (!EQ (val, Qunbound))
1246 return val;
1248 xsignal1 (Qvoid_variable, symbol);
1251 DEFUN ("set", Fset, Sset, 2, 2, 0,
1252 doc: /* Set SYMBOL's value to NEWVAL, and return NEWVAL. */)
1253 (register Lisp_Object symbol, Lisp_Object newval)
1255 set_internal (symbol, newval, Qnil, SET_INTERNAL_SET);
1256 return newval;
1259 /* Store the value NEWVAL into SYMBOL.
1260 If buffer-locality is an issue, WHERE specifies which context to use.
1261 (nil stands for the current buffer/frame).
1263 If BINDFLAG is SET_INTERNAL_SET, then if this symbol is supposed to
1264 become local in every buffer where it is set, then we make it
1265 local. If BINDFLAG is SET_INTERNAL_BIND or SET_INTERNAL_UNBIND, we
1266 don't do that. */
1268 void
1269 set_internal (Lisp_Object symbol, Lisp_Object newval, Lisp_Object where,
1270 enum Set_Internal_Bind bindflag)
1272 bool voide = EQ (newval, Qunbound);
1273 struct Lisp_Symbol *sym;
1274 Lisp_Object tem1;
1276 /* If restoring in a dead buffer, do nothing. */
1277 /* if (BUFFERP (where) && NILP (XBUFFER (where)->name))
1278 return; */
1280 CHECK_SYMBOL (symbol);
1281 sym = XSYMBOL (symbol);
1282 switch (sym->trapped_write)
1284 case SYMBOL_NOWRITE:
1285 if (NILP (Fkeywordp (symbol))
1286 || !EQ (newval, Fsymbol_value (symbol)))
1287 xsignal1 (Qsetting_constant, symbol);
1288 else
1289 /* Allow setting keywords to their own value. */
1290 return;
1292 case SYMBOL_TRAPPED_WRITE:
1293 /* Setting due to thread-switching doesn't count. */
1294 if (bindflag != SET_INTERNAL_THREAD_SWITCH)
1295 notify_variable_watchers (symbol, voide? Qnil : newval,
1296 (bindflag == SET_INTERNAL_BIND? Qlet :
1297 bindflag == SET_INTERNAL_UNBIND? Qunlet :
1298 voide? Qmakunbound : Qset),
1299 where);
1300 /* FALLTHROUGH! */
1301 case SYMBOL_UNTRAPPED_WRITE:
1302 break;
1304 default: emacs_abort ();
1307 start:
1308 switch (sym->redirect)
1310 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1311 case SYMBOL_PLAINVAL: SET_SYMBOL_VAL (sym , newval); return;
1312 case SYMBOL_LOCALIZED:
1314 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1315 if (NILP (where))
1316 XSETBUFFER (where, current_buffer);
1318 /* If the current buffer is not the buffer whose binding is
1319 loaded, or if it's a Lisp_Buffer_Local_Value and
1320 the default binding is loaded, the loaded binding may be the
1321 wrong one. */
1322 if (!EQ (blv->where, where)
1323 /* Also unload a global binding (if the var is local_if_set). */
1324 || (EQ (blv->valcell, blv->defcell)))
1326 /* The currently loaded binding is not necessarily valid.
1327 We need to unload it, and choose a new binding. */
1329 /* Write out `realvalue' to the old loaded binding. */
1330 if (blv->fwd)
1331 set_blv_value (blv, do_symval_forwarding (blv->fwd));
1333 /* Find the new binding. */
1334 XSETSYMBOL (symbol, sym); /* May have changed via aliasing. */
1335 tem1 = assq_no_quit (symbol,
1336 BVAR (XBUFFER (where), local_var_alist));
1337 set_blv_where (blv, where);
1338 blv->found = 1;
1340 if (NILP (tem1))
1342 /* This buffer still sees the default value. */
1344 /* If the variable is a Lisp_Some_Buffer_Local_Value,
1345 or if this is `let' rather than `set',
1346 make CURRENT-ALIST-ELEMENT point to itself,
1347 indicating that we're seeing the default value.
1348 Likewise if the variable has been let-bound
1349 in the current buffer. */
1350 if (bindflag || !blv->local_if_set
1351 || let_shadows_buffer_binding_p (sym))
1353 blv->found = 0;
1354 tem1 = blv->defcell;
1356 /* If it's a local_if_set, being set not bound,
1357 and we're not within a let that was made for this buffer,
1358 create a new buffer-local binding for the variable.
1359 That means, give this buffer a new assoc for a local value
1360 and load that binding. */
1361 else
1363 tem1 = Fcons (symbol, XCDR (blv->defcell));
1364 bset_local_var_alist
1365 (XBUFFER (where),
1366 Fcons (tem1, BVAR (XBUFFER (where), local_var_alist)));
1370 /* Record which binding is now loaded. */
1371 set_blv_valcell (blv, tem1);
1374 /* Store the new value in the cons cell. */
1375 set_blv_value (blv, newval);
1377 if (blv->fwd)
1379 if (voide)
1380 /* If storing void (making the symbol void), forward only through
1381 buffer-local indicator, not through Lisp_Objfwd, etc. */
1382 blv->fwd = NULL;
1383 else
1384 store_symval_forwarding (blv->fwd, newval,
1385 BUFFERP (where)
1386 ? XBUFFER (where) : current_buffer);
1388 break;
1390 case SYMBOL_FORWARDED:
1392 struct buffer *buf
1393 = BUFFERP (where) ? XBUFFER (where) : current_buffer;
1394 union Lisp_Fwd *innercontents = SYMBOL_FWD (sym);
1395 if (BUFFER_OBJFWDP (innercontents))
1397 int offset = XBUFFER_OBJFWD (innercontents)->offset;
1398 int idx = PER_BUFFER_IDX (offset);
1399 if (idx > 0
1400 && bindflag == SET_INTERNAL_SET
1401 && !let_shadows_buffer_binding_p (sym))
1402 SET_PER_BUFFER_VALUE_P (buf, idx, 1);
1405 if (voide)
1406 { /* If storing void (making the symbol void), forward only through
1407 buffer-local indicator, not through Lisp_Objfwd, etc. */
1408 sym->redirect = SYMBOL_PLAINVAL;
1409 SET_SYMBOL_VAL (sym, newval);
1411 else
1412 store_symval_forwarding (/* sym, */ innercontents, newval, buf);
1413 break;
1415 default: emacs_abort ();
1417 return;
1420 static void
1421 set_symbol_trapped_write (Lisp_Object symbol, enum symbol_trapped_write trap)
1423 struct Lisp_Symbol* sym = XSYMBOL (symbol);
1424 if (sym->trapped_write == SYMBOL_NOWRITE)
1425 xsignal1 (Qtrapping_constant, symbol);
1426 sym->trapped_write = trap;
1429 static void
1430 restore_symbol_trapped_write (Lisp_Object symbol)
1432 set_symbol_trapped_write (symbol, SYMBOL_TRAPPED_WRITE);
1435 static void
1436 harmonize_variable_watchers (Lisp_Object alias, Lisp_Object base_variable)
1438 if (!EQ (base_variable, alias)
1439 && EQ (base_variable, Findirect_variable (alias)))
1440 set_symbol_trapped_write
1441 (alias, XSYMBOL (base_variable)->trapped_write);
1444 DEFUN ("add-variable-watcher", Fadd_variable_watcher, Sadd_variable_watcher,
1445 2, 2, 0,
1446 doc: /* Cause WATCH-FUNCTION to be called when SYMBOL is set.
1448 It will be called with 4 arguments: (SYMBOL NEWVAL OPERATION WHERE).
1449 SYMBOL is the variable being changed.
1450 NEWVAL is the value it will be changed to.
1451 OPERATION is a symbol representing the kind of change, one of: `set',
1452 `let', `unlet', `makunbound', and `defvaralias'.
1453 WHERE is a buffer if the buffer-local value of the variable being
1454 changed, nil otherwise.
1456 All writes to aliases of SYMBOL will call WATCH-FUNCTION too. */)
1457 (Lisp_Object symbol, Lisp_Object watch_function)
1459 symbol = Findirect_variable (symbol);
1460 set_symbol_trapped_write (symbol, SYMBOL_TRAPPED_WRITE);
1461 map_obarray (Vobarray, harmonize_variable_watchers, symbol);
1463 Lisp_Object watchers = Fget (symbol, Qwatchers);
1464 Lisp_Object member = Fmember (watch_function, watchers);
1465 if (NILP (member))
1466 Fput (symbol, Qwatchers, Fcons (watch_function, watchers));
1467 return Qnil;
1470 DEFUN ("remove-variable-watcher", Fremove_variable_watcher, Sremove_variable_watcher,
1471 2, 2, 0,
1472 doc: /* Undo the effect of `add-variable-watcher'.
1473 Remove WATCH-FUNCTION from the list of functions to be called when
1474 SYMBOL (or its aliases) are set. */)
1475 (Lisp_Object symbol, Lisp_Object watch_function)
1477 symbol = Findirect_variable (symbol);
1478 Lisp_Object watchers = Fget (symbol, Qwatchers);
1479 watchers = Fdelete (watch_function, watchers);
1480 if (NILP (watchers))
1482 set_symbol_trapped_write (symbol, SYMBOL_UNTRAPPED_WRITE);
1483 map_obarray (Vobarray, harmonize_variable_watchers, symbol);
1485 Fput (symbol, Qwatchers, watchers);
1486 return Qnil;
1489 DEFUN ("get-variable-watchers", Fget_variable_watchers, Sget_variable_watchers,
1490 1, 1, 0,
1491 doc: /* Return a list of SYMBOL's active watchers. */)
1492 (Lisp_Object symbol)
1494 return (SYMBOL_TRAPPED_WRITE_P (symbol) == SYMBOL_TRAPPED_WRITE)
1495 ? Fget (Findirect_variable (symbol), Qwatchers)
1496 : Qnil;
1499 void
1500 notify_variable_watchers (Lisp_Object symbol,
1501 Lisp_Object newval,
1502 Lisp_Object operation,
1503 Lisp_Object where)
1505 symbol = Findirect_variable (symbol);
1507 ptrdiff_t count = SPECPDL_INDEX ();
1508 record_unwind_protect (restore_symbol_trapped_write, symbol);
1509 /* Avoid recursion. */
1510 set_symbol_trapped_write (symbol, SYMBOL_UNTRAPPED_WRITE);
1512 if (NILP (where)
1513 && !EQ (operation, Qset_default) && !EQ (operation, Qmakunbound)
1514 && !NILP (Flocal_variable_if_set_p (symbol, Fcurrent_buffer ())))
1516 XSETBUFFER (where, current_buffer);
1519 if (EQ (operation, Qset_default))
1520 operation = Qset;
1522 for (Lisp_Object watchers = Fget (symbol, Qwatchers);
1523 CONSP (watchers);
1524 watchers = XCDR (watchers))
1526 Lisp_Object watcher = XCAR (watchers);
1527 /* Call subr directly to avoid gc. */
1528 if (SUBRP (watcher))
1530 Lisp_Object args[] = { symbol, newval, operation, where };
1531 funcall_subr (XSUBR (watcher), ARRAYELTS (args), args);
1533 else
1534 CALLN (Ffuncall, watcher, symbol, newval, operation, where);
1537 unbind_to (count, Qnil);
1541 /* Access or set a buffer-local symbol's default value. */
1543 /* Return the default value of SYMBOL, but don't check for voidness.
1544 Return Qunbound if it is void. */
1546 static Lisp_Object
1547 default_value (Lisp_Object symbol)
1549 struct Lisp_Symbol *sym;
1551 CHECK_SYMBOL (symbol);
1552 sym = XSYMBOL (symbol);
1554 start:
1555 switch (sym->redirect)
1557 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1558 case SYMBOL_PLAINVAL: return SYMBOL_VAL (sym);
1559 case SYMBOL_LOCALIZED:
1561 /* If var is set up for a buffer that lacks a local value for it,
1562 the current value is nominally the default value.
1563 But the `realvalue' slot may be more up to date, since
1564 ordinary setq stores just that slot. So use that. */
1565 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1566 if (blv->fwd && EQ (blv->valcell, blv->defcell))
1567 return do_symval_forwarding (blv->fwd);
1568 else
1569 return XCDR (blv->defcell);
1571 case SYMBOL_FORWARDED:
1573 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
1575 /* For a built-in buffer-local variable, get the default value
1576 rather than letting do_symval_forwarding get the current value. */
1577 if (BUFFER_OBJFWDP (valcontents))
1579 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1580 if (PER_BUFFER_IDX (offset) != 0)
1581 return per_buffer_default (offset);
1584 /* For other variables, get the current value. */
1585 return do_symval_forwarding (valcontents);
1587 default: emacs_abort ();
1591 DEFUN ("default-boundp", Fdefault_boundp, Sdefault_boundp, 1, 1, 0,
1592 doc: /* Return t if SYMBOL has a non-void default value.
1593 This is the value that is seen in buffers that do not have their own values
1594 for this variable. */)
1595 (Lisp_Object symbol)
1597 register Lisp_Object value;
1599 value = default_value (symbol);
1600 return (EQ (value, Qunbound) ? Qnil : Qt);
1603 DEFUN ("default-value", Fdefault_value, Sdefault_value, 1, 1, 0,
1604 doc: /* Return SYMBOL's default value.
1605 This is the value that is seen in buffers that do not have their own values
1606 for this variable. The default value is meaningful for variables with
1607 local bindings in certain buffers. */)
1608 (Lisp_Object symbol)
1610 Lisp_Object value = default_value (symbol);
1611 if (!EQ (value, Qunbound))
1612 return value;
1614 xsignal1 (Qvoid_variable, symbol);
1617 void
1618 set_default_internal (Lisp_Object symbol, Lisp_Object value,
1619 enum Set_Internal_Bind bindflag)
1621 struct Lisp_Symbol *sym;
1623 CHECK_SYMBOL (symbol);
1624 sym = XSYMBOL (symbol);
1625 switch (sym->trapped_write)
1627 case SYMBOL_NOWRITE:
1628 if (NILP (Fkeywordp (symbol))
1629 || !EQ (value, Fsymbol_value (symbol)))
1630 xsignal1 (Qsetting_constant, symbol);
1631 else
1632 /* Allow setting keywords to their own value. */
1633 return;
1635 case SYMBOL_TRAPPED_WRITE:
1636 /* Don't notify here if we're going to call Fset anyway. */
1637 if (sym->redirect != SYMBOL_PLAINVAL
1638 /* Setting due to thread switching doesn't count. */
1639 && bindflag != SET_INTERNAL_THREAD_SWITCH)
1640 notify_variable_watchers (symbol, value, Qset_default, Qnil);
1641 /* FALLTHROUGH! */
1642 case SYMBOL_UNTRAPPED_WRITE:
1643 break;
1645 default: emacs_abort ();
1648 start:
1649 switch (sym->redirect)
1651 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1652 case SYMBOL_PLAINVAL: set_internal (symbol, value, Qnil, bindflag); return;
1653 case SYMBOL_LOCALIZED:
1655 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1657 /* Store new value into the DEFAULT-VALUE slot. */
1658 XSETCDR (blv->defcell, value);
1660 /* If the default binding is now loaded, set the REALVALUE slot too. */
1661 if (blv->fwd && EQ (blv->defcell, blv->valcell))
1662 store_symval_forwarding (blv->fwd, value, NULL);
1663 return;
1665 case SYMBOL_FORWARDED:
1667 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
1669 /* Handle variables like case-fold-search that have special slots
1670 in the buffer.
1671 Make them work apparently like Lisp_Buffer_Local_Value variables. */
1672 if (BUFFER_OBJFWDP (valcontents))
1674 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1675 int idx = PER_BUFFER_IDX (offset);
1677 set_per_buffer_default (offset, value);
1679 /* If this variable is not always local in all buffers,
1680 set it in the buffers that don't nominally have a local value. */
1681 if (idx > 0)
1683 struct buffer *b;
1685 FOR_EACH_BUFFER (b)
1686 if (!PER_BUFFER_VALUE_P (b, idx))
1687 set_per_buffer_value (b, offset, value);
1690 else
1691 set_internal (symbol, value, Qnil, bindflag);
1692 return;
1694 default: emacs_abort ();
1698 DEFUN ("set-default", Fset_default, Sset_default, 2, 2, 0,
1699 doc: /* Set SYMBOL's default value to VALUE. SYMBOL and VALUE are evaluated.
1700 The default value is seen in buffers that do not have their own values
1701 for this variable. */)
1702 (Lisp_Object symbol, Lisp_Object value)
1704 set_default_internal (symbol, value, SET_INTERNAL_SET);
1705 return value;
1708 DEFUN ("setq-default", Fsetq_default, Ssetq_default, 0, UNEVALLED, 0,
1709 doc: /* Set the default value of variable VAR to VALUE.
1710 VAR, the variable name, is literal (not evaluated);
1711 VALUE is an expression: it is evaluated and its value returned.
1712 The default value of a variable is seen in buffers
1713 that do not have their own values for the variable.
1715 More generally, you can use multiple variables and values, as in
1716 (setq-default VAR VALUE VAR VALUE...)
1717 This sets each VAR's default value to the corresponding VALUE.
1718 The VALUE for the Nth VAR can refer to the new default values
1719 of previous VARs.
1720 usage: (setq-default [VAR VALUE]...) */)
1721 (Lisp_Object args)
1723 Lisp_Object args_left, symbol, val;
1725 args_left = val = args;
1727 while (CONSP (args_left))
1729 val = eval_sub (Fcar (XCDR (args_left)));
1730 symbol = XCAR (args_left);
1731 Fset_default (symbol, val);
1732 args_left = Fcdr (XCDR (args_left));
1735 return val;
1738 /* Lisp functions for creating and removing buffer-local variables. */
1740 union Lisp_Val_Fwd
1742 Lisp_Object value;
1743 union Lisp_Fwd *fwd;
1746 static struct Lisp_Buffer_Local_Value *
1747 make_blv (struct Lisp_Symbol *sym, bool forwarded,
1748 union Lisp_Val_Fwd valcontents)
1750 struct Lisp_Buffer_Local_Value *blv = xmalloc (sizeof *blv);
1751 Lisp_Object symbol;
1752 Lisp_Object tem;
1754 XSETSYMBOL (symbol, sym);
1755 tem = Fcons (symbol, (forwarded
1756 ? do_symval_forwarding (valcontents.fwd)
1757 : valcontents.value));
1759 /* Buffer_Local_Values cannot have as realval a buffer-local
1760 or keyboard-local forwarding. */
1761 eassert (!(forwarded && BUFFER_OBJFWDP (valcontents.fwd)));
1762 eassert (!(forwarded && KBOARD_OBJFWDP (valcontents.fwd)));
1763 blv->fwd = forwarded ? valcontents.fwd : NULL;
1764 set_blv_where (blv, Qnil);
1765 blv->local_if_set = 0;
1766 set_blv_defcell (blv, tem);
1767 set_blv_valcell (blv, tem);
1768 set_blv_found (blv, 0);
1769 return blv;
1772 DEFUN ("make-variable-buffer-local", Fmake_variable_buffer_local,
1773 Smake_variable_buffer_local, 1, 1, "vMake Variable Buffer Local: ",
1774 doc: /* Make VARIABLE become buffer-local whenever it is set.
1775 At any time, the value for the current buffer is in effect,
1776 unless the variable has never been set in this buffer,
1777 in which case the default value is in effect.
1778 Note that binding the variable with `let', or setting it while
1779 a `let'-style binding made in this buffer is in effect,
1780 does not make the variable buffer-local. Return VARIABLE.
1782 This globally affects all uses of this variable, so it belongs together with
1783 the variable declaration, rather than with its uses (if you just want to make
1784 a variable local to the current buffer for one particular use, use
1785 `make-local-variable'). Buffer-local bindings are normally cleared
1786 while setting up a new major mode, unless they have a `permanent-local'
1787 property.
1789 The function `default-value' gets the default value and `set-default' sets it. */)
1790 (register Lisp_Object variable)
1792 struct Lisp_Symbol *sym;
1793 struct Lisp_Buffer_Local_Value *blv = NULL;
1794 union Lisp_Val_Fwd valcontents;
1795 bool forwarded;
1797 CHECK_SYMBOL (variable);
1798 sym = XSYMBOL (variable);
1800 start:
1801 switch (sym->redirect)
1803 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1804 case SYMBOL_PLAINVAL:
1805 forwarded = 0; valcontents.value = SYMBOL_VAL (sym);
1806 if (EQ (valcontents.value, Qunbound))
1807 valcontents.value = Qnil;
1808 break;
1809 case SYMBOL_LOCALIZED:
1810 blv = SYMBOL_BLV (sym);
1811 break;
1812 case SYMBOL_FORWARDED:
1813 forwarded = 1; valcontents.fwd = SYMBOL_FWD (sym);
1814 if (KBOARD_OBJFWDP (valcontents.fwd))
1815 error ("Symbol %s may not be buffer-local",
1816 SDATA (SYMBOL_NAME (variable)));
1817 else if (BUFFER_OBJFWDP (valcontents.fwd))
1818 return variable;
1819 break;
1820 default: emacs_abort ();
1823 if (SYMBOL_CONSTANT_P (variable))
1824 error ("Symbol %s may not be buffer-local", SDATA (SYMBOL_NAME (variable)));
1826 if (!blv)
1828 blv = make_blv (sym, forwarded, valcontents);
1829 sym->redirect = SYMBOL_LOCALIZED;
1830 SET_SYMBOL_BLV (sym, blv);
1832 Lisp_Object symbol;
1833 XSETSYMBOL (symbol, sym); /* In case `variable' is aliased. */
1834 if (let_shadows_global_binding_p (symbol))
1836 AUTO_STRING (format, "Making %s buffer-local while let-bound!");
1837 CALLN (Fmessage, format, SYMBOL_NAME (variable));
1842 blv->local_if_set = 1;
1843 return variable;
1846 DEFUN ("make-local-variable", Fmake_local_variable, Smake_local_variable,
1847 1, 1, "vMake Local Variable: ",
1848 doc: /* Make VARIABLE have a separate value in the current buffer.
1849 Other buffers will continue to share a common default value.
1850 \(The buffer-local value of VARIABLE starts out as the same value
1851 VARIABLE previously had. If VARIABLE was void, it remains void.)
1852 Return VARIABLE.
1854 If the variable is already arranged to become local when set,
1855 this function causes a local value to exist for this buffer,
1856 just as setting the variable would do.
1858 This function returns VARIABLE, and therefore
1859 (set (make-local-variable \\='VARIABLE) VALUE-EXP)
1860 works.
1862 See also `make-variable-buffer-local'.
1864 Do not use `make-local-variable' to make a hook variable buffer-local.
1865 Instead, use `add-hook' and specify t for the LOCAL argument. */)
1866 (Lisp_Object variable)
1868 Lisp_Object tem;
1869 bool forwarded;
1870 union Lisp_Val_Fwd valcontents;
1871 struct Lisp_Symbol *sym;
1872 struct Lisp_Buffer_Local_Value *blv = NULL;
1874 CHECK_SYMBOL (variable);
1875 sym = XSYMBOL (variable);
1877 start:
1878 switch (sym->redirect)
1880 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1881 case SYMBOL_PLAINVAL:
1882 forwarded = 0; valcontents.value = SYMBOL_VAL (sym); break;
1883 case SYMBOL_LOCALIZED:
1884 blv = SYMBOL_BLV (sym);
1885 break;
1886 case SYMBOL_FORWARDED:
1887 forwarded = 1; valcontents.fwd = SYMBOL_FWD (sym);
1888 if (KBOARD_OBJFWDP (valcontents.fwd))
1889 error ("Symbol %s may not be buffer-local",
1890 SDATA (SYMBOL_NAME (variable)));
1891 break;
1892 default: emacs_abort ();
1895 if (sym->trapped_write == SYMBOL_NOWRITE)
1896 error ("Symbol %s may not be buffer-local",
1897 SDATA (SYMBOL_NAME (variable)));
1899 if (blv ? blv->local_if_set
1900 : (forwarded && BUFFER_OBJFWDP (valcontents.fwd)))
1902 tem = Fboundp (variable);
1903 /* Make sure the symbol has a local value in this particular buffer,
1904 by setting it to the same value it already has. */
1905 Fset (variable, (EQ (tem, Qt) ? Fsymbol_value (variable) : Qunbound));
1906 return variable;
1908 if (!blv)
1910 blv = make_blv (sym, forwarded, valcontents);
1911 sym->redirect = SYMBOL_LOCALIZED;
1912 SET_SYMBOL_BLV (sym, blv);
1914 Lisp_Object symbol;
1915 XSETSYMBOL (symbol, sym); /* In case `variable' is aliased. */
1916 if (let_shadows_global_binding_p (symbol))
1918 AUTO_STRING (format, "Making %s local to %s while let-bound!");
1919 CALLN (Fmessage, format, SYMBOL_NAME (variable),
1920 BVAR (current_buffer, name));
1925 /* Make sure this buffer has its own value of symbol. */
1926 XSETSYMBOL (variable, sym); /* Update in case of aliasing. */
1927 tem = Fassq (variable, BVAR (current_buffer, local_var_alist));
1928 if (NILP (tem))
1930 if (let_shadows_buffer_binding_p (sym))
1932 AUTO_STRING (format,
1933 "Making %s buffer-local while locally let-bound!");
1934 CALLN (Fmessage, format, SYMBOL_NAME (variable));
1937 /* Swap out any local binding for some other buffer, and make
1938 sure the current value is permanently recorded, if it's the
1939 default value. */
1940 find_symbol_value (variable);
1942 bset_local_var_alist
1943 (current_buffer,
1944 Fcons (Fcons (variable, XCDR (blv->defcell)),
1945 BVAR (current_buffer, local_var_alist)));
1947 /* Make sure symbol does not think it is set up for this buffer;
1948 force it to look once again for this buffer's value. */
1949 if (current_buffer == XBUFFER (blv->where))
1950 set_blv_where (blv, Qnil);
1951 set_blv_found (blv, 0);
1954 /* If the symbol forwards into a C variable, then load the binding
1955 for this buffer now. If C code modifies the variable before we
1956 load the binding in, then that new value will clobber the default
1957 binding the next time we unload it. */
1958 if (blv->fwd)
1959 swap_in_symval_forwarding (sym, blv);
1961 return variable;
1964 DEFUN ("kill-local-variable", Fkill_local_variable, Skill_local_variable,
1965 1, 1, "vKill Local Variable: ",
1966 doc: /* Make VARIABLE no longer have a separate value in the current buffer.
1967 From now on the default value will apply in this buffer. Return VARIABLE. */)
1968 (register Lisp_Object variable)
1970 register Lisp_Object tem;
1971 struct Lisp_Buffer_Local_Value *blv;
1972 struct Lisp_Symbol *sym;
1974 CHECK_SYMBOL (variable);
1975 sym = XSYMBOL (variable);
1977 start:
1978 switch (sym->redirect)
1980 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1981 case SYMBOL_PLAINVAL: return variable;
1982 case SYMBOL_FORWARDED:
1984 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
1985 if (BUFFER_OBJFWDP (valcontents))
1987 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1988 int idx = PER_BUFFER_IDX (offset);
1990 if (idx > 0)
1992 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 0);
1993 set_per_buffer_value (current_buffer, offset,
1994 per_buffer_default (offset));
1997 return variable;
1999 case SYMBOL_LOCALIZED:
2000 blv = SYMBOL_BLV (sym);
2001 break;
2002 default: emacs_abort ();
2005 if (sym->trapped_write == SYMBOL_TRAPPED_WRITE)
2006 notify_variable_watchers (variable, Qnil, Qmakunbound, Fcurrent_buffer ());
2008 /* Get rid of this buffer's alist element, if any. */
2009 XSETSYMBOL (variable, sym); /* Propagate variable indirection. */
2010 tem = Fassq (variable, BVAR (current_buffer, local_var_alist));
2011 if (!NILP (tem))
2012 bset_local_var_alist
2013 (current_buffer,
2014 Fdelq (tem, BVAR (current_buffer, local_var_alist)));
2016 /* If the symbol is set up with the current buffer's binding
2017 loaded, recompute its value. We have to do it now, or else
2018 forwarded objects won't work right. */
2020 Lisp_Object buf; XSETBUFFER (buf, current_buffer);
2021 if (EQ (buf, blv->where))
2023 set_blv_where (blv, Qnil);
2024 blv->found = 0;
2025 find_symbol_value (variable);
2029 return variable;
2032 /* Lisp functions for creating and removing buffer-local variables. */
2034 DEFUN ("local-variable-p", Flocal_variable_p, Slocal_variable_p,
2035 1, 2, 0,
2036 doc: /* Non-nil if VARIABLE has a local binding in buffer BUFFER.
2037 BUFFER defaults to the current buffer. */)
2038 (Lisp_Object variable, Lisp_Object buffer)
2040 struct buffer *buf = decode_buffer (buffer);
2041 struct Lisp_Symbol *sym;
2043 CHECK_SYMBOL (variable);
2044 sym = XSYMBOL (variable);
2046 start:
2047 switch (sym->redirect)
2049 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
2050 case SYMBOL_PLAINVAL: return Qnil;
2051 case SYMBOL_LOCALIZED:
2053 Lisp_Object tail, elt, tmp;
2054 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
2055 XSETBUFFER (tmp, buf);
2056 XSETSYMBOL (variable, sym); /* Update in case of aliasing. */
2058 if (EQ (blv->where, tmp)) /* The binding is already loaded. */
2059 return blv_found (blv) ? Qt : Qnil;
2060 else
2061 for (tail = BVAR (buf, local_var_alist); CONSP (tail); tail = XCDR (tail))
2063 elt = XCAR (tail);
2064 if (EQ (variable, XCAR (elt)))
2065 return Qt;
2067 return Qnil;
2069 case SYMBOL_FORWARDED:
2071 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
2072 if (BUFFER_OBJFWDP (valcontents))
2074 int offset = XBUFFER_OBJFWD (valcontents)->offset;
2075 int idx = PER_BUFFER_IDX (offset);
2076 if (idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
2077 return Qt;
2079 return Qnil;
2081 default: emacs_abort ();
2085 DEFUN ("local-variable-if-set-p", Flocal_variable_if_set_p, Slocal_variable_if_set_p,
2086 1, 2, 0,
2087 doc: /* Non-nil if VARIABLE is local in buffer BUFFER when set there.
2088 BUFFER defaults to the current buffer.
2090 More precisely, return non-nil if either VARIABLE already has a local
2091 value in BUFFER, or if VARIABLE is automatically buffer-local (see
2092 `make-variable-buffer-local'). */)
2093 (register Lisp_Object variable, Lisp_Object buffer)
2095 struct Lisp_Symbol *sym;
2097 CHECK_SYMBOL (variable);
2098 sym = XSYMBOL (variable);
2100 start:
2101 switch (sym->redirect)
2103 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
2104 case SYMBOL_PLAINVAL: return Qnil;
2105 case SYMBOL_LOCALIZED:
2107 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
2108 if (blv->local_if_set)
2109 return Qt;
2110 XSETSYMBOL (variable, sym); /* Update in case of aliasing. */
2111 return Flocal_variable_p (variable, buffer);
2113 case SYMBOL_FORWARDED:
2114 /* All BUFFER_OBJFWD slots become local if they are set. */
2115 return (BUFFER_OBJFWDP (SYMBOL_FWD (sym)) ? Qt : Qnil);
2116 default: emacs_abort ();
2120 DEFUN ("variable-binding-locus", Fvariable_binding_locus, Svariable_binding_locus,
2121 1, 1, 0,
2122 doc: /* Return a value indicating where VARIABLE's current binding comes from.
2123 If the current binding is buffer-local, the value is the current buffer.
2124 If the current binding is global (the default), the value is nil. */)
2125 (register Lisp_Object variable)
2127 struct Lisp_Symbol *sym;
2129 CHECK_SYMBOL (variable);
2130 sym = XSYMBOL (variable);
2132 /* Make sure the current binding is actually swapped in. */
2133 find_symbol_value (variable);
2135 start:
2136 switch (sym->redirect)
2138 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
2139 case SYMBOL_PLAINVAL: return Qnil;
2140 case SYMBOL_FORWARDED:
2142 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
2143 if (KBOARD_OBJFWDP (valcontents))
2144 return Fframe_terminal (selected_frame);
2145 else if (!BUFFER_OBJFWDP (valcontents))
2146 return Qnil;
2148 /* FALLTHROUGH */
2149 case SYMBOL_LOCALIZED:
2150 /* For a local variable, record both the symbol and which
2151 buffer's or frame's value we are saving. */
2152 if (!NILP (Flocal_variable_p (variable, Qnil)))
2153 return Fcurrent_buffer ();
2154 else if (sym->redirect == SYMBOL_LOCALIZED
2155 && blv_found (SYMBOL_BLV (sym)))
2156 return SYMBOL_BLV (sym)->where;
2157 else
2158 return Qnil;
2159 default: emacs_abort ();
2163 /* This code is disabled now that we use the selected frame to return
2164 keyboard-local-values. */
2165 #if 0
2166 extern struct terminal *get_terminal (Lisp_Object display, int);
2168 DEFUN ("terminal-local-value", Fterminal_local_value,
2169 Sterminal_local_value, 2, 2, 0,
2170 doc: /* Return the terminal-local value of SYMBOL on TERMINAL.
2171 If SYMBOL is not a terminal-local variable, then return its normal
2172 value, like `symbol-value'.
2174 TERMINAL may be a terminal object, a frame, or nil (meaning the
2175 selected frame's terminal device). */)
2176 (Lisp_Object symbol, Lisp_Object terminal)
2178 Lisp_Object result;
2179 struct terminal *t = get_terminal (terminal, 1);
2180 push_kboard (t->kboard);
2181 result = Fsymbol_value (symbol);
2182 pop_kboard ();
2183 return result;
2186 DEFUN ("set-terminal-local-value", Fset_terminal_local_value,
2187 Sset_terminal_local_value, 3, 3, 0,
2188 doc: /* Set the terminal-local binding of SYMBOL on TERMINAL to VALUE.
2189 If VARIABLE is not a terminal-local variable, then set its normal
2190 binding, like `set'.
2192 TERMINAL may be a terminal object, a frame, or nil (meaning the
2193 selected frame's terminal device). */)
2194 (Lisp_Object symbol, Lisp_Object terminal, Lisp_Object value)
2196 Lisp_Object result;
2197 struct terminal *t = get_terminal (terminal, 1);
2198 push_kboard (d->kboard);
2199 result = Fset (symbol, value);
2200 pop_kboard ();
2201 return result;
2203 #endif
2205 /* Find the function at the end of a chain of symbol function indirections. */
2207 /* If OBJECT is a symbol, find the end of its function chain and
2208 return the value found there. If OBJECT is not a symbol, just
2209 return it. If there is a cycle in the function chain, signal a
2210 cyclic-function-indirection error.
2212 This is like Findirect_function, except that it doesn't signal an
2213 error if the chain ends up unbound. */
2214 Lisp_Object
2215 indirect_function (register Lisp_Object object)
2217 Lisp_Object tortoise, hare;
2219 hare = tortoise = object;
2221 for (;;)
2223 if (!SYMBOLP (hare) || NILP (hare))
2224 break;
2225 hare = XSYMBOL (hare)->function;
2226 if (!SYMBOLP (hare) || NILP (hare))
2227 break;
2228 hare = XSYMBOL (hare)->function;
2230 tortoise = XSYMBOL (tortoise)->function;
2232 if (EQ (hare, tortoise))
2233 xsignal1 (Qcyclic_function_indirection, object);
2236 return hare;
2239 DEFUN ("indirect-function", Findirect_function, Sindirect_function, 1, 2, 0,
2240 doc: /* Return the function at the end of OBJECT's function chain.
2241 If OBJECT is not a symbol, just return it. Otherwise, follow all
2242 function indirections to find the final function binding and return it.
2243 Signal a cyclic-function-indirection error if there is a loop in the
2244 function chain of symbols. */)
2245 (register Lisp_Object object, Lisp_Object noerror)
2247 Lisp_Object result;
2249 /* Optimize for no indirection. */
2250 result = object;
2251 if (SYMBOLP (result) && !NILP (result)
2252 && (result = XSYMBOL (result)->function, SYMBOLP (result)))
2253 result = indirect_function (result);
2254 if (!NILP (result))
2255 return result;
2257 return Qnil;
2260 /* Extract and set vector and string elements. */
2262 DEFUN ("aref", Faref, Saref, 2, 2, 0,
2263 doc: /* Return the element of ARRAY at index IDX.
2264 ARRAY may be a vector, a string, a char-table, a bool-vector,
2265 or a byte-code object. IDX starts at 0. */)
2266 (register Lisp_Object array, Lisp_Object idx)
2268 register EMACS_INT idxval;
2270 CHECK_NUMBER (idx);
2271 idxval = XINT (idx);
2272 if (STRINGP (array))
2274 int c;
2275 ptrdiff_t idxval_byte;
2277 if (idxval < 0 || idxval >= SCHARS (array))
2278 args_out_of_range (array, idx);
2279 if (! STRING_MULTIBYTE (array))
2280 return make_number ((unsigned char) SREF (array, idxval));
2281 idxval_byte = string_char_to_byte (array, idxval);
2283 c = STRING_CHAR (SDATA (array) + idxval_byte);
2284 return make_number (c);
2286 else if (BOOL_VECTOR_P (array))
2288 if (idxval < 0 || idxval >= bool_vector_size (array))
2289 args_out_of_range (array, idx);
2290 return bool_vector_ref (array, idxval);
2292 else if (CHAR_TABLE_P (array))
2294 CHECK_CHARACTER (idx);
2295 return CHAR_TABLE_REF (array, idxval);
2297 else
2299 ptrdiff_t size = 0;
2300 if (VECTORP (array))
2301 size = ASIZE (array);
2302 else if (COMPILEDP (array))
2303 size = ASIZE (array) & PSEUDOVECTOR_SIZE_MASK;
2304 else
2305 wrong_type_argument (Qarrayp, array);
2307 if (idxval < 0 || idxval >= size)
2308 args_out_of_range (array, idx);
2309 return AREF (array, idxval);
2313 DEFUN ("aset", Faset, Saset, 3, 3, 0,
2314 doc: /* Store into the element of ARRAY at index IDX the value NEWELT.
2315 Return NEWELT. ARRAY may be a vector, a string, a char-table or a
2316 bool-vector. IDX starts at 0. */)
2317 (register Lisp_Object array, Lisp_Object idx, Lisp_Object newelt)
2319 register EMACS_INT idxval;
2321 CHECK_NUMBER (idx);
2322 idxval = XINT (idx);
2323 CHECK_ARRAY (array, Qarrayp);
2325 if (VECTORP (array))
2327 CHECK_IMPURE (array, XVECTOR (array));
2328 if (idxval < 0 || idxval >= ASIZE (array))
2329 args_out_of_range (array, idx);
2330 ASET (array, idxval, newelt);
2332 else if (BOOL_VECTOR_P (array))
2334 if (idxval < 0 || idxval >= bool_vector_size (array))
2335 args_out_of_range (array, idx);
2336 bool_vector_set (array, idxval, !NILP (newelt));
2338 else if (CHAR_TABLE_P (array))
2340 CHECK_CHARACTER (idx);
2341 CHAR_TABLE_SET (array, idxval, newelt);
2343 else
2345 int c;
2347 CHECK_IMPURE (array, XSTRING (array));
2348 if (idxval < 0 || idxval >= SCHARS (array))
2349 args_out_of_range (array, idx);
2350 CHECK_CHARACTER (newelt);
2351 c = XFASTINT (newelt);
2353 if (STRING_MULTIBYTE (array))
2355 ptrdiff_t idxval_byte, nbytes;
2356 int prev_bytes, new_bytes;
2357 unsigned char workbuf[MAX_MULTIBYTE_LENGTH], *p0 = workbuf, *p1;
2359 nbytes = SBYTES (array);
2360 idxval_byte = string_char_to_byte (array, idxval);
2361 p1 = SDATA (array) + idxval_byte;
2362 prev_bytes = BYTES_BY_CHAR_HEAD (*p1);
2363 new_bytes = CHAR_STRING (c, p0);
2364 if (prev_bytes != new_bytes)
2366 /* We must relocate the string data. */
2367 ptrdiff_t nchars = SCHARS (array);
2368 USE_SAFE_ALLOCA;
2369 unsigned char *str = SAFE_ALLOCA (nbytes);
2371 memcpy (str, SDATA (array), nbytes);
2372 allocate_string_data (XSTRING (array), nchars,
2373 nbytes + new_bytes - prev_bytes);
2374 memcpy (SDATA (array), str, idxval_byte);
2375 p1 = SDATA (array) + idxval_byte;
2376 memcpy (p1 + new_bytes, str + idxval_byte + prev_bytes,
2377 nbytes - (idxval_byte + prev_bytes));
2378 SAFE_FREE ();
2379 clear_string_char_byte_cache ();
2381 while (new_bytes--)
2382 *p1++ = *p0++;
2384 else
2386 if (! SINGLE_BYTE_CHAR_P (c))
2388 ptrdiff_t i;
2390 for (i = SBYTES (array) - 1; i >= 0; i--)
2391 if (SREF (array, i) >= 0x80)
2392 args_out_of_range (array, newelt);
2393 /* ARRAY is an ASCII string. Convert it to a multibyte
2394 string, and try `aset' again. */
2395 STRING_SET_MULTIBYTE (array);
2396 return Faset (array, idx, newelt);
2398 SSET (array, idxval, c);
2402 return newelt;
2405 /* Arithmetic functions */
2407 Lisp_Object
2408 arithcompare (Lisp_Object num1, Lisp_Object num2, enum Arith_Comparison comparison)
2410 double f1 = 0, f2 = 0;
2411 bool floatp = 0;
2413 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num1);
2414 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num2);
2416 if (FLOATP (num1) || FLOATP (num2))
2418 floatp = 1;
2419 f1 = (FLOATP (num1)) ? XFLOAT_DATA (num1) : XINT (num1);
2420 f2 = (FLOATP (num2)) ? XFLOAT_DATA (num2) : XINT (num2);
2423 switch (comparison)
2425 case ARITH_EQUAL:
2426 if (floatp ? f1 == f2 : XINT (num1) == XINT (num2))
2427 return Qt;
2428 return Qnil;
2430 case ARITH_NOTEQUAL:
2431 if (floatp ? f1 != f2 : XINT (num1) != XINT (num2))
2432 return Qt;
2433 return Qnil;
2435 case ARITH_LESS:
2436 if (floatp ? f1 < f2 : XINT (num1) < XINT (num2))
2437 return Qt;
2438 return Qnil;
2440 case ARITH_LESS_OR_EQUAL:
2441 if (floatp ? f1 <= f2 : XINT (num1) <= XINT (num2))
2442 return Qt;
2443 return Qnil;
2445 case ARITH_GRTR:
2446 if (floatp ? f1 > f2 : XINT (num1) > XINT (num2))
2447 return Qt;
2448 return Qnil;
2450 case ARITH_GRTR_OR_EQUAL:
2451 if (floatp ? f1 >= f2 : XINT (num1) >= XINT (num2))
2452 return Qt;
2453 return Qnil;
2455 default:
2456 emacs_abort ();
2460 static Lisp_Object
2461 arithcompare_driver (ptrdiff_t nargs, Lisp_Object *args,
2462 enum Arith_Comparison comparison)
2464 ptrdiff_t argnum;
2465 for (argnum = 1; argnum < nargs; ++argnum)
2467 if (EQ (Qnil, arithcompare (args[argnum - 1], args[argnum], comparison)))
2468 return Qnil;
2470 return Qt;
2473 DEFUN ("=", Feqlsign, Seqlsign, 1, MANY, 0,
2474 doc: /* Return t if args, all numbers or markers, are equal.
2475 usage: (= NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2476 (ptrdiff_t nargs, Lisp_Object *args)
2478 return arithcompare_driver (nargs, args, ARITH_EQUAL);
2481 DEFUN ("<", Flss, Slss, 1, MANY, 0,
2482 doc: /* Return t if each arg (a number or marker), is less than the next arg.
2483 usage: (< NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2484 (ptrdiff_t nargs, Lisp_Object *args)
2486 return arithcompare_driver (nargs, args, ARITH_LESS);
2489 DEFUN (">", Fgtr, Sgtr, 1, MANY, 0,
2490 doc: /* Return t if each arg (a number or marker) is greater than the next arg.
2491 usage: (> NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2492 (ptrdiff_t nargs, Lisp_Object *args)
2494 return arithcompare_driver (nargs, args, ARITH_GRTR);
2497 DEFUN ("<=", Fleq, Sleq, 1, MANY, 0,
2498 doc: /* Return t if each arg (a number or marker) is less than or equal to the next.
2499 usage: (<= NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2500 (ptrdiff_t nargs, Lisp_Object *args)
2502 return arithcompare_driver (nargs, args, ARITH_LESS_OR_EQUAL);
2505 DEFUN (">=", Fgeq, Sgeq, 1, MANY, 0,
2506 doc: /* Return t if each arg (a number or marker) is greater than or equal to the next.
2507 usage: (>= NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2508 (ptrdiff_t nargs, Lisp_Object *args)
2510 return arithcompare_driver (nargs, args, ARITH_GRTR_OR_EQUAL);
2513 DEFUN ("/=", Fneq, Sneq, 2, 2, 0,
2514 doc: /* Return t if first arg is not equal to second arg. Both must be numbers or markers. */)
2515 (register Lisp_Object num1, Lisp_Object num2)
2517 return arithcompare (num1, num2, ARITH_NOTEQUAL);
2520 /* Convert the integer I to a cons-of-integers, where I is not in
2521 fixnum range. */
2523 #define INTBIG_TO_LISP(i, extremum) \
2524 (eassert (FIXNUM_OVERFLOW_P (i)), \
2525 (! (FIXNUM_OVERFLOW_P ((extremum) >> 16) \
2526 && FIXNUM_OVERFLOW_P ((i) >> 16)) \
2527 ? Fcons (make_number ((i) >> 16), make_number ((i) & 0xffff)) \
2528 : ! (FIXNUM_OVERFLOW_P ((extremum) >> 16 >> 24) \
2529 && FIXNUM_OVERFLOW_P ((i) >> 16 >> 24)) \
2530 ? Fcons (make_number ((i) >> 16 >> 24), \
2531 Fcons (make_number ((i) >> 16 & 0xffffff), \
2532 make_number ((i) & 0xffff))) \
2533 : make_float (i)))
2535 Lisp_Object
2536 intbig_to_lisp (intmax_t i)
2538 return INTBIG_TO_LISP (i, INTMAX_MIN);
2541 Lisp_Object
2542 uintbig_to_lisp (uintmax_t i)
2544 return INTBIG_TO_LISP (i, UINTMAX_MAX);
2547 /* Convert the cons-of-integers, integer, or float value C to an
2548 unsigned value with maximum value MAX. Signal an error if C does not
2549 have a valid format or is out of range. */
2550 uintmax_t
2551 cons_to_unsigned (Lisp_Object c, uintmax_t max)
2553 bool valid = 0;
2554 uintmax_t val;
2555 if (INTEGERP (c))
2557 valid = 0 <= XINT (c);
2558 val = XINT (c);
2560 else if (FLOATP (c))
2562 double d = XFLOAT_DATA (c);
2563 if (0 <= d
2564 && d < (max == UINTMAX_MAX ? (double) UINTMAX_MAX + 1 : max + 1))
2566 val = d;
2567 valid = 1;
2570 else if (CONSP (c) && NATNUMP (XCAR (c)))
2572 uintmax_t top = XFASTINT (XCAR (c));
2573 Lisp_Object rest = XCDR (c);
2574 if (top <= UINTMAX_MAX >> 24 >> 16
2575 && CONSP (rest)
2576 && NATNUMP (XCAR (rest)) && XFASTINT (XCAR (rest)) < 1 << 24
2577 && NATNUMP (XCDR (rest)) && XFASTINT (XCDR (rest)) < 1 << 16)
2579 uintmax_t mid = XFASTINT (XCAR (rest));
2580 val = top << 24 << 16 | mid << 16 | XFASTINT (XCDR (rest));
2581 valid = 1;
2583 else if (top <= UINTMAX_MAX >> 16)
2585 if (CONSP (rest))
2586 rest = XCAR (rest);
2587 if (NATNUMP (rest) && XFASTINT (rest) < 1 << 16)
2589 val = top << 16 | XFASTINT (rest);
2590 valid = 1;
2595 if (! (valid && val <= max))
2596 error ("Not an in-range integer, float, or cons of integers");
2597 return val;
2600 /* Convert the cons-of-integers, integer, or float value C to a signed
2601 value with extrema MIN and MAX. Signal an error if C does not have
2602 a valid format or is out of range. */
2603 intmax_t
2604 cons_to_signed (Lisp_Object c, intmax_t min, intmax_t max)
2606 bool valid = 0;
2607 intmax_t val;
2608 if (INTEGERP (c))
2610 val = XINT (c);
2611 valid = 1;
2613 else if (FLOATP (c))
2615 double d = XFLOAT_DATA (c);
2616 if (min <= d
2617 && d < (max == INTMAX_MAX ? (double) INTMAX_MAX + 1 : max + 1))
2619 val = d;
2620 valid = 1;
2623 else if (CONSP (c) && INTEGERP (XCAR (c)))
2625 intmax_t top = XINT (XCAR (c));
2626 Lisp_Object rest = XCDR (c);
2627 if (INTMAX_MIN >> 24 >> 16 <= top && top <= INTMAX_MAX >> 24 >> 16
2628 && CONSP (rest)
2629 && NATNUMP (XCAR (rest)) && XFASTINT (XCAR (rest)) < 1 << 24
2630 && NATNUMP (XCDR (rest)) && XFASTINT (XCDR (rest)) < 1 << 16)
2632 intmax_t mid = XFASTINT (XCAR (rest));
2633 val = top << 24 << 16 | mid << 16 | XFASTINT (XCDR (rest));
2634 valid = 1;
2636 else if (INTMAX_MIN >> 16 <= top && top <= INTMAX_MAX >> 16)
2638 if (CONSP (rest))
2639 rest = XCAR (rest);
2640 if (NATNUMP (rest) && XFASTINT (rest) < 1 << 16)
2642 val = top << 16 | XFASTINT (rest);
2643 valid = 1;
2648 if (! (valid && min <= val && val <= max))
2649 error ("Not an in-range integer, float, or cons of integers");
2650 return val;
2653 DEFUN ("number-to-string", Fnumber_to_string, Snumber_to_string, 1, 1, 0,
2654 doc: /* Return the decimal representation of NUMBER as a string.
2655 Uses a minus sign if negative.
2656 NUMBER may be an integer or a floating point number. */)
2657 (Lisp_Object number)
2659 char buffer[max (FLOAT_TO_STRING_BUFSIZE, INT_BUFSIZE_BOUND (EMACS_INT))];
2660 int len;
2662 CHECK_NUMBER_OR_FLOAT (number);
2664 if (FLOATP (number))
2665 len = float_to_string (buffer, XFLOAT_DATA (number));
2666 else
2667 len = sprintf (buffer, "%"pI"d", XINT (number));
2669 return make_unibyte_string (buffer, len);
2672 DEFUN ("string-to-number", Fstring_to_number, Sstring_to_number, 1, 2, 0,
2673 doc: /* Parse STRING as a decimal number and return the number.
2674 Ignore leading spaces and tabs, and all trailing chars. Return 0 if
2675 STRING cannot be parsed as an integer or floating point number.
2677 If BASE, interpret STRING as a number in that base. If BASE isn't
2678 present, base 10 is used. BASE must be between 2 and 16 (inclusive).
2679 If the base used is not 10, STRING is always parsed as an integer. */)
2680 (register Lisp_Object string, Lisp_Object base)
2682 register char *p;
2683 register int b;
2684 Lisp_Object val;
2686 CHECK_STRING (string);
2688 if (NILP (base))
2689 b = 10;
2690 else
2692 CHECK_NUMBER (base);
2693 if (! (2 <= XINT (base) && XINT (base) <= 16))
2694 xsignal1 (Qargs_out_of_range, base);
2695 b = XINT (base);
2698 p = SSDATA (string);
2699 while (*p == ' ' || *p == '\t')
2700 p++;
2702 val = string_to_number (p, b, 1);
2703 return NILP (val) ? make_number (0) : val;
2706 enum arithop
2708 Aadd,
2709 Asub,
2710 Amult,
2711 Adiv,
2712 Alogand,
2713 Alogior,
2714 Alogxor,
2715 Amax,
2716 Amin
2719 static Lisp_Object float_arith_driver (double, ptrdiff_t, enum arithop,
2720 ptrdiff_t, Lisp_Object *);
2721 static Lisp_Object
2722 arith_driver (enum arithop code, ptrdiff_t nargs, Lisp_Object *args)
2724 Lisp_Object val;
2725 ptrdiff_t argnum, ok_args;
2726 EMACS_INT accum = 0;
2727 EMACS_INT next, ok_accum;
2728 bool overflow = 0;
2730 switch (code)
2732 case Alogior:
2733 case Alogxor:
2734 case Aadd:
2735 case Asub:
2736 accum = 0;
2737 break;
2738 case Amult:
2739 case Adiv:
2740 accum = 1;
2741 break;
2742 case Alogand:
2743 accum = -1;
2744 break;
2745 default:
2746 break;
2749 for (argnum = 0; argnum < nargs; argnum++)
2751 if (! overflow)
2753 ok_args = argnum;
2754 ok_accum = accum;
2757 /* Using args[argnum] as argument to CHECK_NUMBER_... */
2758 val = args[argnum];
2759 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val);
2761 if (FLOATP (val))
2762 return float_arith_driver (ok_accum, ok_args, code,
2763 nargs, args);
2764 args[argnum] = val;
2765 next = XINT (args[argnum]);
2766 switch (code)
2768 case Aadd:
2769 overflow |= INT_ADD_WRAPV (accum, next, &accum);
2770 break;
2771 case Asub:
2772 if (! argnum)
2773 accum = nargs == 1 ? - next : next;
2774 else
2775 overflow |= INT_SUBTRACT_WRAPV (accum, next, &accum);
2776 break;
2777 case Amult:
2778 overflow |= INT_MULTIPLY_WRAPV (accum, next, &accum);
2779 break;
2780 case Adiv:
2781 if (! (argnum || nargs == 1))
2782 accum = next;
2783 else
2785 if (next == 0)
2786 xsignal0 (Qarith_error);
2787 if (INT_DIVIDE_OVERFLOW (accum, next))
2788 overflow = true;
2789 else
2790 accum /= next;
2792 break;
2793 case Alogand:
2794 accum &= next;
2795 break;
2796 case Alogior:
2797 accum |= next;
2798 break;
2799 case Alogxor:
2800 accum ^= next;
2801 break;
2802 case Amax:
2803 if (!argnum || next > accum)
2804 accum = next;
2805 break;
2806 case Amin:
2807 if (!argnum || next < accum)
2808 accum = next;
2809 break;
2813 XSETINT (val, accum);
2814 return val;
2817 #undef isnan
2818 #define isnan(x) ((x) != (x))
2820 static Lisp_Object
2821 float_arith_driver (double accum, ptrdiff_t argnum, enum arithop code,
2822 ptrdiff_t nargs, Lisp_Object *args)
2824 register Lisp_Object val;
2825 double next;
2827 for (; argnum < nargs; argnum++)
2829 val = args[argnum]; /* using args[argnum] as argument to CHECK_NUMBER_... */
2830 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val);
2832 if (FLOATP (val))
2834 next = XFLOAT_DATA (val);
2836 else
2838 args[argnum] = val; /* runs into a compiler bug. */
2839 next = XINT (args[argnum]);
2841 switch (code)
2843 case Aadd:
2844 accum += next;
2845 break;
2846 case Asub:
2847 accum = argnum ? accum - next : nargs == 1 ? - next : next;
2848 break;
2849 case Amult:
2850 accum *= next;
2851 break;
2852 case Adiv:
2853 if (! (argnum || nargs == 1))
2854 accum = next;
2855 else
2857 if (! IEEE_FLOATING_POINT && next == 0)
2858 xsignal0 (Qarith_error);
2859 accum /= next;
2861 break;
2862 case Alogand:
2863 case Alogior:
2864 case Alogxor:
2865 wrong_type_argument (Qinteger_or_marker_p, val);
2866 case Amax:
2867 if (!argnum || isnan (next) || next > accum)
2868 accum = next;
2869 break;
2870 case Amin:
2871 if (!argnum || isnan (next) || next < accum)
2872 accum = next;
2873 break;
2877 return make_float (accum);
2881 DEFUN ("+", Fplus, Splus, 0, MANY, 0,
2882 doc: /* Return sum of any number of arguments, which are numbers or markers.
2883 usage: (+ &rest NUMBERS-OR-MARKERS) */)
2884 (ptrdiff_t nargs, Lisp_Object *args)
2886 return arith_driver (Aadd, nargs, args);
2889 DEFUN ("-", Fminus, Sminus, 0, MANY, 0,
2890 doc: /* Negate number or subtract numbers or markers and return the result.
2891 With one arg, negates it. With more than one arg,
2892 subtracts all but the first from the first.
2893 usage: (- &optional NUMBER-OR-MARKER &rest MORE-NUMBERS-OR-MARKERS) */)
2894 (ptrdiff_t nargs, Lisp_Object *args)
2896 return arith_driver (Asub, nargs, args);
2899 DEFUN ("*", Ftimes, Stimes, 0, MANY, 0,
2900 doc: /* Return product of any number of arguments, which are numbers or markers.
2901 usage: (* &rest NUMBERS-OR-MARKERS) */)
2902 (ptrdiff_t nargs, Lisp_Object *args)
2904 return arith_driver (Amult, nargs, args);
2907 DEFUN ("/", Fquo, Squo, 1, MANY, 0,
2908 doc: /* Divide number by divisors and return the result.
2909 With two or more arguments, return first argument divided by the rest.
2910 With one argument, return 1 divided by the argument.
2911 The arguments must be numbers or markers.
2912 usage: (/ NUMBER &rest DIVISORS) */)
2913 (ptrdiff_t nargs, Lisp_Object *args)
2915 ptrdiff_t argnum;
2916 for (argnum = 2; argnum < nargs; argnum++)
2917 if (FLOATP (args[argnum]))
2918 return float_arith_driver (0, 0, Adiv, nargs, args);
2919 return arith_driver (Adiv, nargs, args);
2922 DEFUN ("%", Frem, Srem, 2, 2, 0,
2923 doc: /* Return remainder of X divided by Y.
2924 Both must be integers or markers. */)
2925 (register Lisp_Object x, Lisp_Object y)
2927 Lisp_Object val;
2929 CHECK_NUMBER_COERCE_MARKER (x);
2930 CHECK_NUMBER_COERCE_MARKER (y);
2932 if (XINT (y) == 0)
2933 xsignal0 (Qarith_error);
2935 XSETINT (val, XINT (x) % XINT (y));
2936 return val;
2939 DEFUN ("mod", Fmod, Smod, 2, 2, 0,
2940 doc: /* Return X modulo Y.
2941 The result falls between zero (inclusive) and Y (exclusive).
2942 Both X and Y must be numbers or markers. */)
2943 (register Lisp_Object x, Lisp_Object y)
2945 Lisp_Object val;
2946 EMACS_INT i1, i2;
2948 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (x);
2949 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (y);
2951 if (FLOATP (x) || FLOATP (y))
2952 return fmod_float (x, y);
2954 i1 = XINT (x);
2955 i2 = XINT (y);
2957 if (i2 == 0)
2958 xsignal0 (Qarith_error);
2960 i1 %= i2;
2962 /* If the "remainder" comes out with the wrong sign, fix it. */
2963 if (i2 < 0 ? i1 > 0 : i1 < 0)
2964 i1 += i2;
2966 XSETINT (val, i1);
2967 return val;
2970 DEFUN ("max", Fmax, Smax, 1, MANY, 0,
2971 doc: /* Return largest of all the arguments (which must be numbers or markers).
2972 The value is always a number; markers are converted to numbers.
2973 usage: (max NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2974 (ptrdiff_t nargs, Lisp_Object *args)
2976 return arith_driver (Amax, nargs, args);
2979 DEFUN ("min", Fmin, Smin, 1, MANY, 0,
2980 doc: /* Return smallest of all the arguments (which must be numbers or markers).
2981 The value is always a number; markers are converted to numbers.
2982 usage: (min NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2983 (ptrdiff_t nargs, Lisp_Object *args)
2985 return arith_driver (Amin, nargs, args);
2988 DEFUN ("logand", Flogand, Slogand, 0, MANY, 0,
2989 doc: /* Return bitwise-and of all the arguments.
2990 Arguments may be integers, or markers converted to integers.
2991 usage: (logand &rest INTS-OR-MARKERS) */)
2992 (ptrdiff_t nargs, Lisp_Object *args)
2994 return arith_driver (Alogand, nargs, args);
2997 DEFUN ("logior", Flogior, Slogior, 0, MANY, 0,
2998 doc: /* Return bitwise-or of all the arguments.
2999 Arguments may be integers, or markers converted to integers.
3000 usage: (logior &rest INTS-OR-MARKERS) */)
3001 (ptrdiff_t nargs, Lisp_Object *args)
3003 return arith_driver (Alogior, nargs, args);
3006 DEFUN ("logxor", Flogxor, Slogxor, 0, MANY, 0,
3007 doc: /* Return bitwise-exclusive-or of all the arguments.
3008 Arguments may be integers, or markers converted to integers.
3009 usage: (logxor &rest INTS-OR-MARKERS) */)
3010 (ptrdiff_t nargs, Lisp_Object *args)
3012 return arith_driver (Alogxor, nargs, args);
3015 static Lisp_Object
3016 ash_lsh_impl (register Lisp_Object value, Lisp_Object count, bool lsh)
3018 register Lisp_Object val;
3020 CHECK_NUMBER (value);
3021 CHECK_NUMBER (count);
3023 if (XINT (count) >= EMACS_INT_WIDTH)
3024 XSETINT (val, 0);
3025 else if (XINT (count) > 0)
3026 XSETINT (val, XUINT (value) << XFASTINT (count));
3027 else if (XINT (count) <= -EMACS_INT_WIDTH)
3028 XSETINT (val, lsh ? 0 : XINT (value) < 0 ? -1 : 0);
3029 else
3030 XSETINT (val, lsh ? XUINT (value) >> -XINT (count) : \
3031 XINT (value) >> -XINT (count));
3032 return val;
3035 DEFUN ("ash", Fash, Sash, 2, 2, 0,
3036 doc: /* Return VALUE with its bits shifted left by COUNT.
3037 If COUNT is negative, shifting is actually to the right.
3038 In this case, the sign bit is duplicated. */)
3039 (register Lisp_Object value, Lisp_Object count)
3041 return ash_lsh_impl (value, count, false);
3044 DEFUN ("lsh", Flsh, Slsh, 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, zeros are shifted in on the left. */)
3048 (register Lisp_Object value, Lisp_Object count)
3050 return ash_lsh_impl (value, count, true);
3053 DEFUN ("1+", Fadd1, Sadd1, 1, 1, 0,
3054 doc: /* Return NUMBER plus one. NUMBER may be a number or a marker.
3055 Markers are converted to integers. */)
3056 (register Lisp_Object number)
3058 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number);
3060 if (FLOATP (number))
3061 return (make_float (1.0 + XFLOAT_DATA (number)));
3063 XSETINT (number, XINT (number) + 1);
3064 return number;
3067 DEFUN ("1-", Fsub1, Ssub1, 1, 1, 0,
3068 doc: /* Return NUMBER minus one. NUMBER may be a number or a marker.
3069 Markers are converted to integers. */)
3070 (register Lisp_Object number)
3072 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number);
3074 if (FLOATP (number))
3075 return (make_float (-1.0 + XFLOAT_DATA (number)));
3077 XSETINT (number, XINT (number) - 1);
3078 return number;
3081 DEFUN ("lognot", Flognot, Slognot, 1, 1, 0,
3082 doc: /* Return the bitwise complement of NUMBER. NUMBER must be an integer. */)
3083 (register Lisp_Object number)
3085 CHECK_NUMBER (number);
3086 XSETINT (number, ~XINT (number));
3087 return number;
3090 DEFUN ("byteorder", Fbyteorder, Sbyteorder, 0, 0, 0,
3091 doc: /* Return the byteorder for the machine.
3092 Returns 66 (ASCII uppercase B) for big endian machines or 108 (ASCII
3093 lowercase l) for small endian machines. */
3094 attributes: const)
3095 (void)
3097 unsigned i = 0x04030201;
3098 int order = *(char *)&i == 1 ? 108 : 66;
3100 return make_number (order);
3103 /* Because we round up the bool vector allocate size to word_size
3104 units, we can safely read past the "end" of the vector in the
3105 operations below. These extra bits are always zero. */
3107 static bits_word
3108 bool_vector_spare_mask (EMACS_INT nr_bits)
3110 return (((bits_word) 1) << (nr_bits % BITS_PER_BITS_WORD)) - 1;
3113 /* Info about unsigned long long, falling back on unsigned long
3114 if unsigned long long is not available. */
3116 #if HAVE_UNSIGNED_LONG_LONG_INT && defined ULLONG_WIDTH
3117 enum { ULL_WIDTH = ULLONG_WIDTH };
3118 # define ULL_MAX ULLONG_MAX
3119 #else
3120 enum { ULL_WIDTH = ULONG_WIDTH };
3121 # define ULL_MAX ULONG_MAX
3122 # define count_one_bits_ll count_one_bits_l
3123 # define count_trailing_zeros_ll count_trailing_zeros_l
3124 #endif
3126 /* Shift VAL right by the width of an unsigned long long.
3127 ULL_WIDTH must be less than BITS_PER_BITS_WORD. */
3129 static bits_word
3130 shift_right_ull (bits_word w)
3132 /* Pacify bogus GCC warning about shift count exceeding type width. */
3133 int shift = ULL_WIDTH - BITS_PER_BITS_WORD < 0 ? ULL_WIDTH : 0;
3134 return w >> shift;
3137 /* Return the number of 1 bits in W. */
3139 static int
3140 count_one_bits_word (bits_word w)
3142 if (BITS_WORD_MAX <= UINT_MAX)
3143 return count_one_bits (w);
3144 else if (BITS_WORD_MAX <= ULONG_MAX)
3145 return count_one_bits_l (w);
3146 else
3148 int i = 0, count = 0;
3149 while (count += count_one_bits_ll (w),
3150 (i += ULL_WIDTH) < BITS_PER_BITS_WORD)
3151 w = shift_right_ull (w);
3152 return count;
3156 enum bool_vector_op { bool_vector_exclusive_or,
3157 bool_vector_union,
3158 bool_vector_intersection,
3159 bool_vector_set_difference,
3160 bool_vector_subsetp };
3162 static Lisp_Object
3163 bool_vector_binop_driver (Lisp_Object a,
3164 Lisp_Object b,
3165 Lisp_Object dest,
3166 enum bool_vector_op op)
3168 EMACS_INT nr_bits;
3169 bits_word *adata, *bdata, *destdata;
3170 ptrdiff_t i = 0;
3171 ptrdiff_t nr_words;
3173 CHECK_BOOL_VECTOR (a);
3174 CHECK_BOOL_VECTOR (b);
3176 nr_bits = bool_vector_size (a);
3177 if (bool_vector_size (b) != nr_bits)
3178 wrong_length_argument (a, b, dest);
3180 nr_words = bool_vector_words (nr_bits);
3181 adata = bool_vector_data (a);
3182 bdata = bool_vector_data (b);
3184 if (NILP (dest))
3186 dest = make_uninit_bool_vector (nr_bits);
3187 destdata = bool_vector_data (dest);
3189 else
3191 CHECK_BOOL_VECTOR (dest);
3192 destdata = bool_vector_data (dest);
3193 if (bool_vector_size (dest) != nr_bits)
3194 wrong_length_argument (a, b, dest);
3196 switch (op)
3198 case bool_vector_exclusive_or:
3199 for (; i < nr_words; i++)
3200 if (destdata[i] != (adata[i] ^ bdata[i]))
3201 goto set_dest;
3202 break;
3204 case bool_vector_subsetp:
3205 for (; i < nr_words; i++)
3206 if (adata[i] &~ bdata[i])
3207 return Qnil;
3208 return Qt;
3210 case bool_vector_union:
3211 for (; i < nr_words; i++)
3212 if (destdata[i] != (adata[i] | bdata[i]))
3213 goto set_dest;
3214 break;
3216 case bool_vector_intersection:
3217 for (; i < nr_words; i++)
3218 if (destdata[i] != (adata[i] & bdata[i]))
3219 goto set_dest;
3220 break;
3222 case bool_vector_set_difference:
3223 for (; i < nr_words; i++)
3224 if (destdata[i] != (adata[i] &~ bdata[i]))
3225 goto set_dest;
3226 break;
3229 return Qnil;
3232 set_dest:
3233 switch (op)
3235 case bool_vector_exclusive_or:
3236 for (; i < nr_words; i++)
3237 destdata[i] = adata[i] ^ bdata[i];
3238 break;
3240 case bool_vector_union:
3241 for (; i < nr_words; i++)
3242 destdata[i] = adata[i] | bdata[i];
3243 break;
3245 case bool_vector_intersection:
3246 for (; i < nr_words; i++)
3247 destdata[i] = adata[i] & bdata[i];
3248 break;
3250 case bool_vector_set_difference:
3251 for (; i < nr_words; i++)
3252 destdata[i] = adata[i] &~ bdata[i];
3253 break;
3255 default:
3256 eassume (0);
3259 return dest;
3262 /* PRECONDITION must be true. Return VALUE. This odd construction
3263 works around a bogus GCC diagnostic "shift count >= width of type". */
3265 static int
3266 pre_value (bool precondition, int value)
3268 eassume (precondition);
3269 return precondition ? value : 0;
3272 /* Compute the number of trailing zero bits in val. If val is zero,
3273 return the number of bits in val. */
3274 static int
3275 count_trailing_zero_bits (bits_word val)
3277 if (BITS_WORD_MAX == UINT_MAX)
3278 return count_trailing_zeros (val);
3279 if (BITS_WORD_MAX == ULONG_MAX)
3280 return count_trailing_zeros_l (val);
3281 if (BITS_WORD_MAX == ULL_MAX)
3282 return count_trailing_zeros_ll (val);
3284 /* The rest of this code is for the unlikely platform where bits_word differs
3285 in width from unsigned int, unsigned long, and unsigned long long. */
3286 val |= ~ BITS_WORD_MAX;
3287 if (BITS_WORD_MAX <= UINT_MAX)
3288 return count_trailing_zeros (val);
3289 if (BITS_WORD_MAX <= ULONG_MAX)
3290 return count_trailing_zeros_l (val);
3291 else
3293 int count;
3294 for (count = 0;
3295 count < BITS_PER_BITS_WORD - ULL_WIDTH;
3296 count += ULL_WIDTH)
3298 if (val & ULL_MAX)
3299 return count + count_trailing_zeros_ll (val);
3300 val = shift_right_ull (val);
3303 if (BITS_PER_BITS_WORD % ULL_WIDTH != 0
3304 && BITS_WORD_MAX == (bits_word) -1)
3305 val |= (bits_word) 1 << pre_value (ULONG_MAX < BITS_WORD_MAX,
3306 BITS_PER_BITS_WORD % ULL_WIDTH);
3307 return count + count_trailing_zeros_ll (val);
3311 static bits_word
3312 bits_word_to_host_endian (bits_word val)
3314 #ifndef WORDS_BIGENDIAN
3315 return val;
3316 #else
3317 if (BITS_WORD_MAX >> 31 == 1)
3318 return bswap_32 (val);
3319 # if HAVE_UNSIGNED_LONG_LONG
3320 if (BITS_WORD_MAX >> 31 >> 31 >> 1 == 1)
3321 return bswap_64 (val);
3322 # endif
3324 int i;
3325 bits_word r = 0;
3326 for (i = 0; i < sizeof val; i++)
3328 r = ((r << 1 << (CHAR_BIT - 1))
3329 | (val & ((1u << 1 << (CHAR_BIT - 1)) - 1)));
3330 val = val >> 1 >> (CHAR_BIT - 1);
3332 return r;
3334 #endif
3337 DEFUN ("bool-vector-exclusive-or", Fbool_vector_exclusive_or,
3338 Sbool_vector_exclusive_or, 2, 3, 0,
3339 doc: /* Return A ^ B, bitwise exclusive or.
3340 If optional third argument C is given, store result into C.
3341 A, B, and C must be bool vectors of the same length.
3342 Return the destination vector if it changed or nil otherwise. */)
3343 (Lisp_Object a, Lisp_Object b, Lisp_Object c)
3345 return bool_vector_binop_driver (a, b, c, bool_vector_exclusive_or);
3348 DEFUN ("bool-vector-union", Fbool_vector_union,
3349 Sbool_vector_union, 2, 3, 0,
3350 doc: /* Return A | B, bitwise or.
3351 If optional third argument C is given, store result into C.
3352 A, B, and C must be bool vectors of the same length.
3353 Return the destination vector if it changed or nil otherwise. */)
3354 (Lisp_Object a, Lisp_Object b, Lisp_Object c)
3356 return bool_vector_binop_driver (a, b, c, bool_vector_union);
3359 DEFUN ("bool-vector-intersection", Fbool_vector_intersection,
3360 Sbool_vector_intersection, 2, 3, 0,
3361 doc: /* Return A & B, bitwise and.
3362 If optional third argument C is given, store result into C.
3363 A, B, and C must be bool vectors of the same length.
3364 Return the destination vector if it changed or nil otherwise. */)
3365 (Lisp_Object a, Lisp_Object b, Lisp_Object c)
3367 return bool_vector_binop_driver (a, b, c, bool_vector_intersection);
3370 DEFUN ("bool-vector-set-difference", Fbool_vector_set_difference,
3371 Sbool_vector_set_difference, 2, 3, 0,
3372 doc: /* Return A &~ B, set difference.
3373 If optional third argument C is given, store result into C.
3374 A, B, and C must be bool vectors of the same length.
3375 Return the destination vector if it changed or nil otherwise. */)
3376 (Lisp_Object a, Lisp_Object b, Lisp_Object c)
3378 return bool_vector_binop_driver (a, b, c, bool_vector_set_difference);
3381 DEFUN ("bool-vector-subsetp", Fbool_vector_subsetp,
3382 Sbool_vector_subsetp, 2, 2, 0,
3383 doc: /* Return t if every t value in A is also t in B, nil otherwise.
3384 A and B must be bool vectors of the same length. */)
3385 (Lisp_Object a, Lisp_Object b)
3387 return bool_vector_binop_driver (a, b, b, bool_vector_subsetp);
3390 DEFUN ("bool-vector-not", Fbool_vector_not,
3391 Sbool_vector_not, 1, 2, 0,
3392 doc: /* Compute ~A, set complement.
3393 If optional second argument B is given, store result into B.
3394 A and B must be bool vectors of the same length.
3395 Return the destination vector. */)
3396 (Lisp_Object a, Lisp_Object b)
3398 EMACS_INT nr_bits;
3399 bits_word *bdata, *adata;
3400 ptrdiff_t i;
3402 CHECK_BOOL_VECTOR (a);
3403 nr_bits = bool_vector_size (a);
3405 if (NILP (b))
3406 b = make_uninit_bool_vector (nr_bits);
3407 else
3409 CHECK_BOOL_VECTOR (b);
3410 if (bool_vector_size (b) != nr_bits)
3411 wrong_length_argument (a, b, Qnil);
3414 bdata = bool_vector_data (b);
3415 adata = bool_vector_data (a);
3417 for (i = 0; i < nr_bits / BITS_PER_BITS_WORD; i++)
3418 bdata[i] = BITS_WORD_MAX & ~adata[i];
3420 if (nr_bits % BITS_PER_BITS_WORD)
3422 bits_word mword = bits_word_to_host_endian (adata[i]);
3423 mword = ~mword;
3424 mword &= bool_vector_spare_mask (nr_bits);
3425 bdata[i] = bits_word_to_host_endian (mword);
3428 return b;
3431 DEFUN ("bool-vector-count-population", Fbool_vector_count_population,
3432 Sbool_vector_count_population, 1, 1, 0,
3433 doc: /* Count how many elements in A are t.
3434 A is a bool vector. To count A's nil elements, subtract the return
3435 value from A's length. */)
3436 (Lisp_Object a)
3438 EMACS_INT count;
3439 EMACS_INT nr_bits;
3440 bits_word *adata;
3441 ptrdiff_t i, nwords;
3443 CHECK_BOOL_VECTOR (a);
3445 nr_bits = bool_vector_size (a);
3446 nwords = bool_vector_words (nr_bits);
3447 count = 0;
3448 adata = bool_vector_data (a);
3450 for (i = 0; i < nwords; i++)
3451 count += count_one_bits_word (adata[i]);
3453 return make_number (count);
3456 DEFUN ("bool-vector-count-consecutive", Fbool_vector_count_consecutive,
3457 Sbool_vector_count_consecutive, 3, 3, 0,
3458 doc: /* Count how many consecutive elements in A equal B starting at I.
3459 A is a bool vector, B is t or nil, and I is an index into A. */)
3460 (Lisp_Object a, Lisp_Object b, Lisp_Object i)
3462 EMACS_INT count;
3463 EMACS_INT nr_bits;
3464 int offset;
3465 bits_word *adata;
3466 bits_word twiddle;
3467 bits_word mword; /* Machine word. */
3468 ptrdiff_t pos, pos0;
3469 ptrdiff_t nr_words;
3471 CHECK_BOOL_VECTOR (a);
3472 CHECK_NATNUM (i);
3474 nr_bits = bool_vector_size (a);
3475 if (XFASTINT (i) > nr_bits) /* Allow one past the end for convenience */
3476 args_out_of_range (a, i);
3478 adata = bool_vector_data (a);
3479 nr_words = bool_vector_words (nr_bits);
3480 pos = XFASTINT (i) / BITS_PER_BITS_WORD;
3481 offset = XFASTINT (i) % BITS_PER_BITS_WORD;
3482 count = 0;
3484 /* By XORing with twiddle, we transform the problem of "count
3485 consecutive equal values" into "count the zero bits". The latter
3486 operation usually has hardware support. */
3487 twiddle = NILP (b) ? 0 : BITS_WORD_MAX;
3489 /* Scan the remainder of the mword at the current offset. */
3490 if (pos < nr_words && offset != 0)
3492 mword = bits_word_to_host_endian (adata[pos]);
3493 mword ^= twiddle;
3494 mword >>= offset;
3496 /* Do not count the pad bits. */
3497 mword |= (bits_word) 1 << (BITS_PER_BITS_WORD - offset);
3499 count = count_trailing_zero_bits (mword);
3500 pos++;
3501 if (count + offset < BITS_PER_BITS_WORD)
3502 return make_number (count);
3505 /* Scan whole words until we either reach the end of the vector or
3506 find an mword that doesn't completely match. twiddle is
3507 endian-independent. */
3508 pos0 = pos;
3509 while (pos < nr_words && adata[pos] == twiddle)
3510 pos++;
3511 count += (pos - pos0) * BITS_PER_BITS_WORD;
3513 if (pos < nr_words)
3515 /* If we stopped because of a mismatch, see how many bits match
3516 in the current mword. */
3517 mword = bits_word_to_host_endian (adata[pos]);
3518 mword ^= twiddle;
3519 count += count_trailing_zero_bits (mword);
3521 else if (nr_bits % BITS_PER_BITS_WORD != 0)
3523 /* If we hit the end, we might have overshot our count. Reduce
3524 the total by the number of spare bits at the end of the
3525 vector. */
3526 count -= BITS_PER_BITS_WORD - nr_bits % BITS_PER_BITS_WORD;
3529 return make_number (count);
3533 void
3534 syms_of_data (void)
3536 Lisp_Object error_tail, arith_tail;
3538 DEFSYM (Qquote, "quote");
3539 DEFSYM (Qlambda, "lambda");
3540 DEFSYM (Qsubr, "subr");
3541 DEFSYM (Qerror_conditions, "error-conditions");
3542 DEFSYM (Qerror_message, "error-message");
3543 DEFSYM (Qtop_level, "top-level");
3545 DEFSYM (Qerror, "error");
3546 DEFSYM (Quser_error, "user-error");
3547 DEFSYM (Qquit, "quit");
3548 DEFSYM (Qwrong_length_argument, "wrong-length-argument");
3549 DEFSYM (Qwrong_type_argument, "wrong-type-argument");
3550 DEFSYM (Qargs_out_of_range, "args-out-of-range");
3551 DEFSYM (Qvoid_function, "void-function");
3552 DEFSYM (Qcyclic_function_indirection, "cyclic-function-indirection");
3553 DEFSYM (Qcyclic_variable_indirection, "cyclic-variable-indirection");
3554 DEFSYM (Qvoid_variable, "void-variable");
3555 DEFSYM (Qsetting_constant, "setting-constant");
3556 DEFSYM (Qtrapping_constant, "trapping-constant");
3557 DEFSYM (Qinvalid_read_syntax, "invalid-read-syntax");
3559 DEFSYM (Qinvalid_function, "invalid-function");
3560 DEFSYM (Qwrong_number_of_arguments, "wrong-number-of-arguments");
3561 DEFSYM (Qno_catch, "no-catch");
3562 DEFSYM (Qend_of_file, "end-of-file");
3563 DEFSYM (Qarith_error, "arith-error");
3564 DEFSYM (Qbeginning_of_buffer, "beginning-of-buffer");
3565 DEFSYM (Qend_of_buffer, "end-of-buffer");
3566 DEFSYM (Qbuffer_read_only, "buffer-read-only");
3567 DEFSYM (Qtext_read_only, "text-read-only");
3568 DEFSYM (Qmark_inactive, "mark-inactive");
3570 DEFSYM (Qlistp, "listp");
3571 DEFSYM (Qconsp, "consp");
3572 DEFSYM (Qsymbolp, "symbolp");
3573 DEFSYM (Qintegerp, "integerp");
3574 DEFSYM (Qnatnump, "natnump");
3575 DEFSYM (Qwholenump, "wholenump");
3576 DEFSYM (Qstringp, "stringp");
3577 DEFSYM (Qarrayp, "arrayp");
3578 DEFSYM (Qsequencep, "sequencep");
3579 DEFSYM (Qbufferp, "bufferp");
3580 DEFSYM (Qvectorp, "vectorp");
3581 DEFSYM (Qbool_vector_p, "bool-vector-p");
3582 DEFSYM (Qchar_or_string_p, "char-or-string-p");
3583 DEFSYM (Qmarkerp, "markerp");
3584 #ifdef HAVE_MODULES
3585 DEFSYM (Quser_ptrp, "user-ptrp");
3586 #endif
3587 DEFSYM (Qbuffer_or_string_p, "buffer-or-string-p");
3588 DEFSYM (Qinteger_or_marker_p, "integer-or-marker-p");
3589 DEFSYM (Qfboundp, "fboundp");
3591 DEFSYM (Qfloatp, "floatp");
3592 DEFSYM (Qnumberp, "numberp");
3593 DEFSYM (Qnumber_or_marker_p, "number-or-marker-p");
3595 DEFSYM (Qchar_table_p, "char-table-p");
3596 DEFSYM (Qvector_or_char_table_p, "vector-or-char-table-p");
3598 DEFSYM (Qsubrp, "subrp");
3599 DEFSYM (Qunevalled, "unevalled");
3600 DEFSYM (Qmany, "many");
3602 DEFSYM (Qcdr, "cdr");
3604 error_tail = pure_cons (Qerror, Qnil);
3606 /* ERROR is used as a signaler for random errors for which nothing else is
3607 right. */
3609 Fput (Qerror, Qerror_conditions,
3610 error_tail);
3611 Fput (Qerror, Qerror_message,
3612 build_pure_c_string ("error"));
3614 #define PUT_ERROR(sym, tail, msg) \
3615 Fput (sym, Qerror_conditions, pure_cons (sym, tail)); \
3616 Fput (sym, Qerror_message, build_pure_c_string (msg))
3618 PUT_ERROR (Qquit, Qnil, "Quit");
3620 PUT_ERROR (Quser_error, error_tail, "");
3621 PUT_ERROR (Qwrong_length_argument, error_tail, "Wrong length argument");
3622 PUT_ERROR (Qwrong_type_argument, error_tail, "Wrong type argument");
3623 PUT_ERROR (Qargs_out_of_range, error_tail, "Args out of range");
3624 PUT_ERROR (Qvoid_function, error_tail,
3625 "Symbol's function definition is void");
3626 PUT_ERROR (Qcyclic_function_indirection, error_tail,
3627 "Symbol's chain of function indirections contains a loop");
3628 PUT_ERROR (Qcyclic_variable_indirection, error_tail,
3629 "Symbol's chain of variable indirections contains a loop");
3630 DEFSYM (Qcircular_list, "circular-list");
3631 PUT_ERROR (Qcircular_list, error_tail, "List contains a loop");
3632 PUT_ERROR (Qvoid_variable, error_tail, "Symbol's value as variable is void");
3633 PUT_ERROR (Qsetting_constant, error_tail,
3634 "Attempt to set a constant symbol");
3635 PUT_ERROR (Qtrapping_constant, error_tail,
3636 "Attempt to trap writes to a constant symbol");
3637 PUT_ERROR (Qinvalid_read_syntax, error_tail, "Invalid read syntax");
3638 PUT_ERROR (Qinvalid_function, error_tail, "Invalid function");
3639 PUT_ERROR (Qwrong_number_of_arguments, error_tail,
3640 "Wrong number of arguments");
3641 PUT_ERROR (Qno_catch, error_tail, "No catch for tag");
3642 PUT_ERROR (Qend_of_file, error_tail, "End of file during parsing");
3644 arith_tail = pure_cons (Qarith_error, error_tail);
3645 Fput (Qarith_error, Qerror_conditions, arith_tail);
3646 Fput (Qarith_error, Qerror_message, build_pure_c_string ("Arithmetic error"));
3648 PUT_ERROR (Qbeginning_of_buffer, error_tail, "Beginning of buffer");
3649 PUT_ERROR (Qend_of_buffer, error_tail, "End of buffer");
3650 PUT_ERROR (Qbuffer_read_only, error_tail, "Buffer is read-only");
3651 PUT_ERROR (Qtext_read_only, pure_cons (Qbuffer_read_only, error_tail),
3652 "Text is read-only");
3654 DEFSYM (Qrange_error, "range-error");
3655 DEFSYM (Qdomain_error, "domain-error");
3656 DEFSYM (Qsingularity_error, "singularity-error");
3657 DEFSYM (Qoverflow_error, "overflow-error");
3658 DEFSYM (Qunderflow_error, "underflow-error");
3660 PUT_ERROR (Qdomain_error, arith_tail, "Arithmetic domain error");
3662 PUT_ERROR (Qrange_error, arith_tail, "Arithmetic range error");
3664 PUT_ERROR (Qsingularity_error, Fcons (Qdomain_error, arith_tail),
3665 "Arithmetic singularity error");
3667 PUT_ERROR (Qoverflow_error, Fcons (Qdomain_error, arith_tail),
3668 "Arithmetic overflow error");
3669 PUT_ERROR (Qunderflow_error, Fcons (Qdomain_error, arith_tail),
3670 "Arithmetic underflow error");
3672 /* Types that type-of returns. */
3673 DEFSYM (Qinteger, "integer");
3674 DEFSYM (Qsymbol, "symbol");
3675 DEFSYM (Qstring, "string");
3676 DEFSYM (Qcons, "cons");
3677 DEFSYM (Qmarker, "marker");
3678 DEFSYM (Qoverlay, "overlay");
3679 DEFSYM (Qfinalizer, "finalizer");
3680 #ifdef HAVE_MODULES
3681 DEFSYM (Quser_ptr, "user-ptr");
3682 #endif
3683 DEFSYM (Qfloat, "float");
3684 DEFSYM (Qwindow_configuration, "window-configuration");
3685 DEFSYM (Qprocess, "process");
3686 DEFSYM (Qwindow, "window");
3687 DEFSYM (Qcompiled_function, "compiled-function");
3688 DEFSYM (Qbuffer, "buffer");
3689 DEFSYM (Qframe, "frame");
3690 DEFSYM (Qvector, "vector");
3691 DEFSYM (Qchar_table, "char-table");
3692 DEFSYM (Qbool_vector, "bool-vector");
3693 DEFSYM (Qhash_table, "hash-table");
3694 DEFSYM (Qthread, "thread");
3695 DEFSYM (Qmutex, "mutex");
3696 DEFSYM (Qcondition_variable, "condition-variable");
3698 DEFSYM (Qdefun, "defun");
3700 DEFSYM (Qfont_spec, "font-spec");
3701 DEFSYM (Qfont_entity, "font-entity");
3702 DEFSYM (Qfont_object, "font-object");
3704 DEFSYM (Qinteractive_form, "interactive-form");
3705 DEFSYM (Qdefalias_fset_function, "defalias-fset-function");
3707 defsubr (&Sindirect_variable);
3708 defsubr (&Sinteractive_form);
3709 defsubr (&Seq);
3710 defsubr (&Snull);
3711 defsubr (&Stype_of);
3712 defsubr (&Slistp);
3713 defsubr (&Snlistp);
3714 defsubr (&Sconsp);
3715 defsubr (&Satom);
3716 defsubr (&Sintegerp);
3717 defsubr (&Sinteger_or_marker_p);
3718 defsubr (&Snumberp);
3719 defsubr (&Snumber_or_marker_p);
3720 defsubr (&Sfloatp);
3721 defsubr (&Snatnump);
3722 defsubr (&Ssymbolp);
3723 defsubr (&Skeywordp);
3724 defsubr (&Sstringp);
3725 defsubr (&Smultibyte_string_p);
3726 defsubr (&Svectorp);
3727 defsubr (&Schar_table_p);
3728 defsubr (&Svector_or_char_table_p);
3729 defsubr (&Sbool_vector_p);
3730 defsubr (&Sarrayp);
3731 defsubr (&Ssequencep);
3732 defsubr (&Sbufferp);
3733 defsubr (&Smarkerp);
3734 defsubr (&Ssubrp);
3735 defsubr (&Sbyte_code_function_p);
3736 defsubr (&Schar_or_string_p);
3737 defsubr (&Sthreadp);
3738 defsubr (&Smutexp);
3739 defsubr (&Scondition_variable_p);
3740 defsubr (&Scar);
3741 defsubr (&Scdr);
3742 defsubr (&Scar_safe);
3743 defsubr (&Scdr_safe);
3744 defsubr (&Ssetcar);
3745 defsubr (&Ssetcdr);
3746 defsubr (&Ssymbol_function);
3747 defsubr (&Sindirect_function);
3748 defsubr (&Ssymbol_plist);
3749 defsubr (&Ssymbol_name);
3750 defsubr (&Smakunbound);
3751 defsubr (&Sfmakunbound);
3752 defsubr (&Sboundp);
3753 defsubr (&Sfboundp);
3754 defsubr (&Sfset);
3755 defsubr (&Sdefalias);
3756 defsubr (&Ssetplist);
3757 defsubr (&Ssymbol_value);
3758 defsubr (&Sset);
3759 defsubr (&Sdefault_boundp);
3760 defsubr (&Sdefault_value);
3761 defsubr (&Sset_default);
3762 defsubr (&Ssetq_default);
3763 defsubr (&Smake_variable_buffer_local);
3764 defsubr (&Smake_local_variable);
3765 defsubr (&Skill_local_variable);
3766 defsubr (&Slocal_variable_p);
3767 defsubr (&Slocal_variable_if_set_p);
3768 defsubr (&Svariable_binding_locus);
3769 #if 0 /* XXX Remove this. --lorentey */
3770 defsubr (&Sterminal_local_value);
3771 defsubr (&Sset_terminal_local_value);
3772 #endif
3773 defsubr (&Saref);
3774 defsubr (&Saset);
3775 defsubr (&Snumber_to_string);
3776 defsubr (&Sstring_to_number);
3777 defsubr (&Seqlsign);
3778 defsubr (&Slss);
3779 defsubr (&Sgtr);
3780 defsubr (&Sleq);
3781 defsubr (&Sgeq);
3782 defsubr (&Sneq);
3783 defsubr (&Splus);
3784 defsubr (&Sminus);
3785 defsubr (&Stimes);
3786 defsubr (&Squo);
3787 defsubr (&Srem);
3788 defsubr (&Smod);
3789 defsubr (&Smax);
3790 defsubr (&Smin);
3791 defsubr (&Slogand);
3792 defsubr (&Slogior);
3793 defsubr (&Slogxor);
3794 defsubr (&Slsh);
3795 defsubr (&Sash);
3796 defsubr (&Sadd1);
3797 defsubr (&Ssub1);
3798 defsubr (&Slognot);
3799 defsubr (&Sbyteorder);
3800 defsubr (&Ssubr_arity);
3801 defsubr (&Ssubr_name);
3802 #ifdef HAVE_MODULES
3803 defsubr (&Suser_ptrp);
3804 #endif
3806 defsubr (&Sbool_vector_exclusive_or);
3807 defsubr (&Sbool_vector_union);
3808 defsubr (&Sbool_vector_intersection);
3809 defsubr (&Sbool_vector_set_difference);
3810 defsubr (&Sbool_vector_not);
3811 defsubr (&Sbool_vector_subsetp);
3812 defsubr (&Sbool_vector_count_consecutive);
3813 defsubr (&Sbool_vector_count_population);
3815 set_symbol_function (Qwholenump, XSYMBOL (Qnatnump)->function);
3817 DEFVAR_LISP ("most-positive-fixnum", Vmost_positive_fixnum,
3818 doc: /* The largest value that is representable in a Lisp integer. */);
3819 Vmost_positive_fixnum = make_number (MOST_POSITIVE_FIXNUM);
3820 make_symbol_constant (intern_c_string ("most-positive-fixnum"));
3822 DEFVAR_LISP ("most-negative-fixnum", Vmost_negative_fixnum,
3823 doc: /* The smallest value that is representable in a Lisp integer. */);
3824 Vmost_negative_fixnum = make_number (MOST_NEGATIVE_FIXNUM);
3825 make_symbol_constant (intern_c_string ("most-negative-fixnum"));
3827 DEFSYM (Qwatchers, "watchers");
3828 DEFSYM (Qmakunbound, "makunbound");
3829 DEFSYM (Qunlet, "unlet");
3830 DEFSYM (Qset, "set");
3831 DEFSYM (Qset_default, "set-default");
3832 defsubr (&Sadd_variable_watcher);
3833 defsubr (&Sremove_variable_watcher);
3834 defsubr (&Sget_variable_watchers);