Treat incomplete integer literals as errors
[emacs.git] / src / data.c
blob821fc3793749842ff781d1bf65b5e181914eb71e
1 /* Primitive operations on Lisp data types for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985-1986, 1988, 1993-1995, 1997-2016 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 "frame.h"
35 #include "keymap.h"
37 static void swap_in_symval_forwarding (struct Lisp_Symbol *,
38 struct Lisp_Buffer_Local_Value *);
40 static bool
41 BOOLFWDP (union Lisp_Fwd *a)
43 return XFWDTYPE (a) == Lisp_Fwd_Bool;
45 static bool
46 INTFWDP (union Lisp_Fwd *a)
48 return XFWDTYPE (a) == Lisp_Fwd_Int;
50 static bool
51 KBOARD_OBJFWDP (union Lisp_Fwd *a)
53 return XFWDTYPE (a) == Lisp_Fwd_Kboard_Obj;
55 static bool
56 OBJFWDP (union Lisp_Fwd *a)
58 return XFWDTYPE (a) == Lisp_Fwd_Obj;
61 static struct Lisp_Boolfwd *
62 XBOOLFWD (union Lisp_Fwd *a)
64 eassert (BOOLFWDP (a));
65 return &a->u_boolfwd;
67 static struct Lisp_Kboard_Objfwd *
68 XKBOARD_OBJFWD (union Lisp_Fwd *a)
70 eassert (KBOARD_OBJFWDP (a));
71 return &a->u_kboard_objfwd;
73 static struct Lisp_Intfwd *
74 XINTFWD (union Lisp_Fwd *a)
76 eassert (INTFWDP (a));
77 return &a->u_intfwd;
79 static struct Lisp_Objfwd *
80 XOBJFWD (union Lisp_Fwd *a)
82 eassert (OBJFWDP (a));
83 return &a->u_objfwd;
86 static void
87 CHECK_SUBR (Lisp_Object x)
89 CHECK_TYPE (SUBRP (x), Qsubrp, x);
92 static void
93 set_blv_found (struct Lisp_Buffer_Local_Value *blv, int found)
95 eassert (found == !EQ (blv->defcell, blv->valcell));
96 blv->found = found;
99 static Lisp_Object
100 blv_value (struct Lisp_Buffer_Local_Value *blv)
102 return XCDR (blv->valcell);
105 static void
106 set_blv_value (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
108 XSETCDR (blv->valcell, val);
111 static void
112 set_blv_where (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
114 blv->where = val;
117 static void
118 set_blv_defcell (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
120 blv->defcell = val;
123 static void
124 set_blv_valcell (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
126 blv->valcell = val;
129 static _Noreturn void
130 wrong_length_argument (Lisp_Object a1, Lisp_Object a2, Lisp_Object a3)
132 Lisp_Object size1 = make_number (bool_vector_size (a1));
133 Lisp_Object size2 = make_number (bool_vector_size (a2));
134 if (NILP (a3))
135 xsignal2 (Qwrong_length_argument, size1, size2);
136 else
137 xsignal3 (Qwrong_length_argument, size1, size2,
138 make_number (bool_vector_size (a3)));
141 _Noreturn void
142 wrong_type_argument (register Lisp_Object predicate, register Lisp_Object value)
144 /* If VALUE is not even a valid Lisp object, we'd want to abort here
145 where we can get a backtrace showing where it came from. We used
146 to try and do that by checking the tagbits, but nowadays all
147 tagbits are potentially valid. */
148 /* if ((unsigned int) XTYPE (value) >= Lisp_Type_Limit)
149 * emacs_abort (); */
151 xsignal2 (Qwrong_type_argument, predicate, value);
154 void
155 pure_write_error (Lisp_Object obj)
157 xsignal2 (Qerror, build_string ("Attempt to modify read-only object"), obj);
160 void
161 args_out_of_range (Lisp_Object a1, Lisp_Object a2)
163 xsignal2 (Qargs_out_of_range, a1, a2);
166 void
167 args_out_of_range_3 (Lisp_Object a1, Lisp_Object a2, Lisp_Object a3)
169 xsignal3 (Qargs_out_of_range, a1, a2, a3);
173 /* Data type predicates. */
175 DEFUN ("eq", Feq, Seq, 2, 2, 0,
176 doc: /* Return t if the two args are the same Lisp object. */
177 attributes: const)
178 (Lisp_Object obj1, Lisp_Object obj2)
180 if (EQ (obj1, obj2))
181 return Qt;
182 return Qnil;
185 DEFUN ("null", Fnull, Snull, 1, 1, 0,
186 doc: /* Return t if OBJECT is nil, and return nil otherwise. */
187 attributes: const)
188 (Lisp_Object object)
190 if (NILP (object))
191 return Qt;
192 return Qnil;
195 DEFUN ("type-of", Ftype_of, Stype_of, 1, 1, 0,
196 doc: /* Return a symbol representing the type of OBJECT.
197 The symbol returned names the object's basic type;
198 for example, (type-of 1) returns `integer'. */)
199 (Lisp_Object object)
201 switch (XTYPE (object))
203 case_Lisp_Int:
204 return Qinteger;
206 case Lisp_Symbol:
207 return Qsymbol;
209 case Lisp_String:
210 return Qstring;
212 case Lisp_Cons:
213 return Qcons;
215 case Lisp_Misc:
216 switch (XMISCTYPE (object))
218 case Lisp_Misc_Marker:
219 return Qmarker;
220 case Lisp_Misc_Overlay:
221 return Qoverlay;
222 case Lisp_Misc_Float:
223 return Qfloat;
224 case Lisp_Misc_Finalizer:
225 return Qfinalizer;
226 #ifdef HAVE_MODULES
227 case Lisp_Misc_User_Ptr:
228 return Quser_ptr;
229 #endif
230 default:
231 emacs_abort ();
234 case Lisp_Vectorlike:
235 if (WINDOW_CONFIGURATIONP (object))
236 return Qwindow_configuration;
237 if (PROCESSP (object))
238 return Qprocess;
239 if (WINDOWP (object))
240 return Qwindow;
241 if (SUBRP (object))
242 return Qsubr;
243 if (COMPILEDP (object))
244 return Qcompiled_function;
245 if (BUFFERP (object))
246 return Qbuffer;
247 if (CHAR_TABLE_P (object))
248 return Qchar_table;
249 if (BOOL_VECTOR_P (object))
250 return Qbool_vector;
251 if (FRAMEP (object))
252 return Qframe;
253 if (HASH_TABLE_P (object))
254 return Qhash_table;
255 if (FONT_SPEC_P (object))
256 return Qfont_spec;
257 if (FONT_ENTITY_P (object))
258 return Qfont_entity;
259 if (FONT_OBJECT_P (object))
260 return Qfont_object;
261 if (THREADP (object))
262 return Qthread;
263 if (MUTEXP (object))
264 return Qmutex;
265 if (CONDVARP (object))
266 return Qcondition_variable;
267 return Qvector;
269 case Lisp_Float:
270 return Qfloat;
272 default:
273 emacs_abort ();
277 DEFUN ("consp", Fconsp, Sconsp, 1, 1, 0,
278 doc: /* Return t if OBJECT is a cons cell. */
279 attributes: const)
280 (Lisp_Object object)
282 if (CONSP (object))
283 return Qt;
284 return Qnil;
287 DEFUN ("atom", Fatom, Satom, 1, 1, 0,
288 doc: /* Return t if OBJECT is not a cons cell. This includes nil. */
289 attributes: const)
290 (Lisp_Object object)
292 if (CONSP (object))
293 return Qnil;
294 return Qt;
297 DEFUN ("listp", Flistp, Slistp, 1, 1, 0,
298 doc: /* Return t if OBJECT is a list, that is, a cons cell or nil.
299 Otherwise, return nil. */
300 attributes: const)
301 (Lisp_Object object)
303 if (CONSP (object) || NILP (object))
304 return Qt;
305 return Qnil;
308 DEFUN ("nlistp", Fnlistp, Snlistp, 1, 1, 0,
309 doc: /* Return t if OBJECT is not a list. Lists include nil. */
310 attributes: const)
311 (Lisp_Object object)
313 if (CONSP (object) || NILP (object))
314 return Qnil;
315 return Qt;
318 DEFUN ("symbolp", Fsymbolp, Ssymbolp, 1, 1, 0,
319 doc: /* Return t if OBJECT is a symbol. */
320 attributes: const)
321 (Lisp_Object object)
323 if (SYMBOLP (object))
324 return Qt;
325 return Qnil;
328 /* Define this in C to avoid unnecessarily consing up the symbol
329 name. */
330 DEFUN ("keywordp", Fkeywordp, Skeywordp, 1, 1, 0,
331 doc: /* Return t if OBJECT is a keyword.
332 This means that it is a symbol with a print name beginning with `:'
333 interned in the initial obarray. */)
334 (Lisp_Object object)
336 if (SYMBOLP (object)
337 && SREF (SYMBOL_NAME (object), 0) == ':'
338 && SYMBOL_INTERNED_IN_INITIAL_OBARRAY_P (object))
339 return Qt;
340 return Qnil;
343 DEFUN ("vectorp", Fvectorp, Svectorp, 1, 1, 0,
344 doc: /* Return t if OBJECT is a vector. */)
345 (Lisp_Object object)
347 if (VECTORP (object))
348 return Qt;
349 return Qnil;
352 DEFUN ("stringp", Fstringp, Sstringp, 1, 1, 0,
353 doc: /* Return t if OBJECT is a string. */
354 attributes: const)
355 (Lisp_Object object)
357 if (STRINGP (object))
358 return Qt;
359 return Qnil;
362 DEFUN ("multibyte-string-p", Fmultibyte_string_p, Smultibyte_string_p,
363 1, 1, 0,
364 doc: /* Return t if OBJECT is a multibyte string.
365 Return nil if OBJECT is either a unibyte string, or not a string. */)
366 (Lisp_Object object)
368 if (STRINGP (object) && STRING_MULTIBYTE (object))
369 return Qt;
370 return Qnil;
373 DEFUN ("char-table-p", Fchar_table_p, Schar_table_p, 1, 1, 0,
374 doc: /* Return t if OBJECT is a char-table. */)
375 (Lisp_Object object)
377 if (CHAR_TABLE_P (object))
378 return Qt;
379 return Qnil;
382 DEFUN ("vector-or-char-table-p", Fvector_or_char_table_p,
383 Svector_or_char_table_p, 1, 1, 0,
384 doc: /* Return t if OBJECT is a char-table or vector. */)
385 (Lisp_Object object)
387 if (VECTORP (object) || CHAR_TABLE_P (object))
388 return Qt;
389 return Qnil;
392 DEFUN ("bool-vector-p", Fbool_vector_p, Sbool_vector_p, 1, 1, 0,
393 doc: /* Return t if OBJECT is a bool-vector. */)
394 (Lisp_Object object)
396 if (BOOL_VECTOR_P (object))
397 return Qt;
398 return Qnil;
401 DEFUN ("arrayp", Farrayp, Sarrayp, 1, 1, 0,
402 doc: /* Return t if OBJECT is an array (string or vector). */)
403 (Lisp_Object object)
405 if (ARRAYP (object))
406 return Qt;
407 return Qnil;
410 DEFUN ("sequencep", Fsequencep, Ssequencep, 1, 1, 0,
411 doc: /* Return t if OBJECT is a sequence (list or array). */)
412 (register Lisp_Object object)
414 if (CONSP (object) || NILP (object) || ARRAYP (object))
415 return Qt;
416 return Qnil;
419 DEFUN ("bufferp", Fbufferp, Sbufferp, 1, 1, 0,
420 doc: /* Return t if OBJECT is an editor buffer. */)
421 (Lisp_Object object)
423 if (BUFFERP (object))
424 return Qt;
425 return Qnil;
428 DEFUN ("markerp", Fmarkerp, Smarkerp, 1, 1, 0,
429 doc: /* Return t if OBJECT is a marker (editor pointer). */)
430 (Lisp_Object object)
432 if (MARKERP (object))
433 return Qt;
434 return Qnil;
437 #ifdef HAVE_MODULES
438 DEFUN ("user-ptrp", Fuser_ptrp, Suser_ptrp, 1, 1, 0,
439 doc: /* Return t if OBJECT is a module user pointer. */)
440 (Lisp_Object object)
442 if (USER_PTRP (object))
443 return Qt;
444 return Qnil;
446 #endif
448 DEFUN ("subrp", Fsubrp, Ssubrp, 1, 1, 0,
449 doc: /* Return t if OBJECT is a built-in function. */)
450 (Lisp_Object object)
452 if (SUBRP (object))
453 return Qt;
454 return Qnil;
457 DEFUN ("byte-code-function-p", Fbyte_code_function_p, Sbyte_code_function_p,
458 1, 1, 0,
459 doc: /* Return t if OBJECT is a byte-compiled function object. */)
460 (Lisp_Object object)
462 if (COMPILEDP (object))
463 return Qt;
464 return Qnil;
467 DEFUN ("char-or-string-p", Fchar_or_string_p, Schar_or_string_p, 1, 1, 0,
468 doc: /* Return t if OBJECT is a character or a string. */
469 attributes: const)
470 (register Lisp_Object object)
472 if (CHARACTERP (object) || STRINGP (object))
473 return Qt;
474 return Qnil;
477 DEFUN ("integerp", Fintegerp, Sintegerp, 1, 1, 0,
478 doc: /* Return t if OBJECT is an integer. */
479 attributes: const)
480 (Lisp_Object object)
482 if (INTEGERP (object))
483 return Qt;
484 return Qnil;
487 DEFUN ("integer-or-marker-p", Finteger_or_marker_p, Sinteger_or_marker_p, 1, 1, 0,
488 doc: /* Return t if OBJECT is an integer or a marker (editor pointer). */)
489 (register Lisp_Object object)
491 if (MARKERP (object) || INTEGERP (object))
492 return Qt;
493 return Qnil;
496 DEFUN ("natnump", Fnatnump, Snatnump, 1, 1, 0,
497 doc: /* Return t if OBJECT is a nonnegative integer. */
498 attributes: const)
499 (Lisp_Object object)
501 if (NATNUMP (object))
502 return Qt;
503 return Qnil;
506 DEFUN ("numberp", Fnumberp, Snumberp, 1, 1, 0,
507 doc: /* Return t if OBJECT is a number (floating point or integer). */
508 attributes: const)
509 (Lisp_Object object)
511 if (NUMBERP (object))
512 return Qt;
513 else
514 return Qnil;
517 DEFUN ("number-or-marker-p", Fnumber_or_marker_p,
518 Snumber_or_marker_p, 1, 1, 0,
519 doc: /* Return t if OBJECT is a number or a marker. */)
520 (Lisp_Object object)
522 if (NUMBERP (object) || MARKERP (object))
523 return Qt;
524 return Qnil;
527 DEFUN ("floatp", Ffloatp, Sfloatp, 1, 1, 0,
528 doc: /* Return t if OBJECT is a floating point number. */
529 attributes: const)
530 (Lisp_Object object)
532 if (FLOATP (object))
533 return Qt;
534 return Qnil;
537 DEFUN ("threadp", Fthreadp, Sthreadp, 1, 1, 0,
538 doc: /* Return t if OBJECT is a thread. */)
539 (Lisp_Object object)
541 if (THREADP (object))
542 return Qt;
543 return Qnil;
546 DEFUN ("mutexp", Fmutexp, Smutexp, 1, 1, 0,
547 doc: /* Return t if OBJECT is a mutex. */)
548 (Lisp_Object object)
550 if (MUTEXP (object))
551 return Qt;
552 return Qnil;
555 DEFUN ("condition-variable-p", Fcondition_variable_p, Scondition_variable_p,
556 1, 1, 0,
557 doc: /* Return t if OBJECT is a condition variable. */)
558 (Lisp_Object object)
560 if (CONDVARP (object))
561 return Qt;
562 return Qnil;
565 /* Extract and set components of lists. */
567 DEFUN ("car", Fcar, Scar, 1, 1, 0,
568 doc: /* Return the car of LIST. If arg is nil, return nil.
569 Error if arg is not nil and not a cons cell. See also `car-safe'.
571 See Info node `(elisp)Cons Cells' for a discussion of related basic
572 Lisp concepts such as car, cdr, cons cell and list. */)
573 (register Lisp_Object list)
575 return CAR (list);
578 DEFUN ("car-safe", Fcar_safe, Scar_safe, 1, 1, 0,
579 doc: /* Return the car of OBJECT if it is a cons cell, or else nil. */)
580 (Lisp_Object object)
582 return CAR_SAFE (object);
585 DEFUN ("cdr", Fcdr, Scdr, 1, 1, 0,
586 doc: /* Return the cdr of LIST. If arg is nil, return nil.
587 Error if arg is not nil and not a cons cell. See also `cdr-safe'.
589 See Info node `(elisp)Cons Cells' for a discussion of related basic
590 Lisp concepts such as cdr, car, cons cell and list. */)
591 (register Lisp_Object list)
593 return CDR (list);
596 DEFUN ("cdr-safe", Fcdr_safe, Scdr_safe, 1, 1, 0,
597 doc: /* Return the cdr of OBJECT if it is a cons cell, or else nil. */)
598 (Lisp_Object object)
600 return CDR_SAFE (object);
603 DEFUN ("setcar", Fsetcar, Ssetcar, 2, 2, 0,
604 doc: /* Set the car of CELL to be NEWCAR. Returns NEWCAR. */)
605 (register Lisp_Object cell, Lisp_Object newcar)
607 CHECK_CONS (cell);
608 CHECK_IMPURE (cell, XCONS (cell));
609 XSETCAR (cell, newcar);
610 return newcar;
613 DEFUN ("setcdr", Fsetcdr, Ssetcdr, 2, 2, 0,
614 doc: /* Set the cdr of CELL to be NEWCDR. Returns NEWCDR. */)
615 (register Lisp_Object cell, Lisp_Object newcdr)
617 CHECK_CONS (cell);
618 CHECK_IMPURE (cell, XCONS (cell));
619 XSETCDR (cell, newcdr);
620 return newcdr;
623 /* Extract and set components of symbols. */
625 DEFUN ("boundp", Fboundp, Sboundp, 1, 1, 0,
626 doc: /* Return t if SYMBOL's value is not void.
627 Note that if `lexical-binding' is in effect, this refers to the
628 global value outside of any lexical scope. */)
629 (register Lisp_Object symbol)
631 Lisp_Object valcontents;
632 struct Lisp_Symbol *sym;
633 CHECK_SYMBOL (symbol);
634 sym = XSYMBOL (symbol);
636 start:
637 switch (sym->redirect)
639 case SYMBOL_PLAINVAL: valcontents = SYMBOL_VAL (sym); break;
640 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
641 case SYMBOL_LOCALIZED:
643 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
644 if (blv->fwd)
645 /* In set_internal, we un-forward vars when their value is
646 set to Qunbound. */
647 return Qt;
648 else
650 swap_in_symval_forwarding (sym, blv);
651 valcontents = blv_value (blv);
653 break;
655 case SYMBOL_FORWARDED:
656 /* In set_internal, we un-forward vars when their value is
657 set to Qunbound. */
658 return Qt;
659 default: emacs_abort ();
662 return (EQ (valcontents, Qunbound) ? Qnil : Qt);
665 /* FIXME: It has been previously suggested to make this function an
666 alias for symbol-function, but upon discussion at Bug#23957,
667 there is a risk breaking backward compatibility, as some users of
668 fboundp may expect `t' in particular, rather than any true
669 value. An alias is still welcome so long as the compatibility
670 issues are addressed. */
671 DEFUN ("fboundp", Ffboundp, Sfboundp, 1, 1, 0,
672 doc: /* Return t if SYMBOL's function definition is not void. */)
673 (register Lisp_Object symbol)
675 CHECK_SYMBOL (symbol);
676 return NILP (XSYMBOL (symbol)->function) ? Qnil : Qt;
679 DEFUN ("makunbound", Fmakunbound, Smakunbound, 1, 1, 0,
680 doc: /* Make SYMBOL's value be void.
681 Return SYMBOL. */)
682 (register Lisp_Object symbol)
684 CHECK_SYMBOL (symbol);
685 if (SYMBOL_CONSTANT_P (symbol))
686 xsignal1 (Qsetting_constant, symbol);
687 Fset (symbol, Qunbound);
688 return symbol;
691 DEFUN ("fmakunbound", Ffmakunbound, Sfmakunbound, 1, 1, 0,
692 doc: /* Make SYMBOL's function definition be nil.
693 Return SYMBOL. */)
694 (register Lisp_Object symbol)
696 CHECK_SYMBOL (symbol);
697 if (NILP (symbol) || EQ (symbol, Qt))
698 xsignal1 (Qsetting_constant, symbol);
699 set_symbol_function (symbol, Qnil);
700 return symbol;
703 DEFUN ("symbol-function", Fsymbol_function, Ssymbol_function, 1, 1, 0,
704 doc: /* Return SYMBOL's function definition, or nil if that is void. */)
705 (register Lisp_Object symbol)
707 CHECK_SYMBOL (symbol);
708 return XSYMBOL (symbol)->function;
711 DEFUN ("symbol-plist", Fsymbol_plist, Ssymbol_plist, 1, 1, 0,
712 doc: /* Return SYMBOL's property list. */)
713 (register Lisp_Object symbol)
715 CHECK_SYMBOL (symbol);
716 return XSYMBOL (symbol)->plist;
719 DEFUN ("symbol-name", Fsymbol_name, Ssymbol_name, 1, 1, 0,
720 doc: /* Return SYMBOL's name, a string. */)
721 (register Lisp_Object symbol)
723 register Lisp_Object name;
725 CHECK_SYMBOL (symbol);
726 name = SYMBOL_NAME (symbol);
727 return name;
730 DEFUN ("fset", Ffset, Sfset, 2, 2, 0,
731 doc: /* Set SYMBOL's function definition to DEFINITION, and return DEFINITION. */)
732 (register Lisp_Object symbol, Lisp_Object definition)
734 register Lisp_Object function;
735 CHECK_SYMBOL (symbol);
736 /* Perhaps not quite the right error signal, but seems good enough. */
737 if (NILP (symbol))
738 xsignal1 (Qsetting_constant, symbol);
740 function = XSYMBOL (symbol)->function;
742 if (!NILP (Vautoload_queue) && !NILP (function))
743 Vautoload_queue = Fcons (Fcons (symbol, function), Vautoload_queue);
745 if (AUTOLOADP (function))
746 Fput (symbol, Qautoload, XCDR (function));
748 /* Convert to eassert or remove after GC bug is found. In the
749 meantime, check unconditionally, at a slight perf hit. */
750 if (! valid_lisp_object_p (definition))
751 emacs_abort ();
753 set_symbol_function (symbol, definition);
755 return definition;
758 DEFUN ("defalias", Fdefalias, Sdefalias, 2, 3, 0,
759 doc: /* Set SYMBOL's function definition to DEFINITION.
760 Associates the function with the current load file, if any.
761 The optional third argument DOCSTRING specifies the documentation string
762 for SYMBOL; if it is omitted or nil, SYMBOL uses the documentation string
763 determined by DEFINITION.
765 Internally, this normally uses `fset', but if SYMBOL has a
766 `defalias-fset-function' property, the associated value is used instead.
768 The return value is undefined. */)
769 (register Lisp_Object symbol, Lisp_Object definition, Lisp_Object docstring)
771 CHECK_SYMBOL (symbol);
772 if (!NILP (Vpurify_flag)
773 /* If `definition' is a keymap, immutable (and copying) is wrong. */
774 && !KEYMAPP (definition))
775 definition = Fpurecopy (definition);
778 bool autoload = AUTOLOADP (definition);
779 if (NILP (Vpurify_flag) || !autoload)
780 { /* Only add autoload entries after dumping, because the ones before are
781 not useful and else we get loads of them from the loaddefs.el. */
783 if (AUTOLOADP (XSYMBOL (symbol)->function))
784 /* Remember that the function was already an autoload. */
785 LOADHIST_ATTACH (Fcons (Qt, symbol));
786 LOADHIST_ATTACH (Fcons (autoload ? Qautoload : Qdefun, symbol));
790 { /* Handle automatic advice activation. */
791 Lisp_Object hook = Fget (symbol, Qdefalias_fset_function);
792 if (!NILP (hook))
793 call2 (hook, symbol, definition);
794 else
795 Ffset (symbol, definition);
798 if (!NILP (docstring))
799 Fput (symbol, Qfunction_documentation, docstring);
800 /* We used to return `definition', but now that `defun' and `defmacro' expand
801 to a call to `defalias', we return `symbol' for backward compatibility
802 (bug#11686). */
803 return symbol;
806 DEFUN ("setplist", Fsetplist, Ssetplist, 2, 2, 0,
807 doc: /* Set SYMBOL's property list to NEWPLIST, and return NEWPLIST. */)
808 (register Lisp_Object symbol, Lisp_Object newplist)
810 CHECK_SYMBOL (symbol);
811 set_symbol_plist (symbol, newplist);
812 return newplist;
815 DEFUN ("subr-arity", Fsubr_arity, Ssubr_arity, 1, 1, 0,
816 doc: /* Return minimum and maximum number of args allowed for SUBR.
817 SUBR must be a built-in function.
818 The returned value is a pair (MIN . MAX). MIN is the minimum number
819 of args. MAX is the maximum number or the symbol `many', for a
820 function with `&rest' args, or `unevalled' for a special form. */)
821 (Lisp_Object subr)
823 short minargs, maxargs;
824 CHECK_SUBR (subr);
825 minargs = XSUBR (subr)->min_args;
826 maxargs = XSUBR (subr)->max_args;
827 return Fcons (make_number (minargs),
828 maxargs == MANY ? Qmany
829 : maxargs == UNEVALLED ? Qunevalled
830 : make_number (maxargs));
833 DEFUN ("subr-name", Fsubr_name, Ssubr_name, 1, 1, 0,
834 doc: /* Return name of subroutine SUBR.
835 SUBR must be a built-in function. */)
836 (Lisp_Object subr)
838 const char *name;
839 CHECK_SUBR (subr);
840 name = XSUBR (subr)->symbol_name;
841 return build_string (name);
844 DEFUN ("interactive-form", Finteractive_form, Sinteractive_form, 1, 1, 0,
845 doc: /* Return the interactive form of CMD or nil if none.
846 If CMD is not a command, the return value is nil.
847 Value, if non-nil, is a list (interactive SPEC). */)
848 (Lisp_Object cmd)
850 Lisp_Object fun = indirect_function (cmd); /* Check cycles. */
852 if (NILP (fun))
853 return Qnil;
855 /* Use an `interactive-form' property if present, analogous to the
856 function-documentation property. */
857 fun = cmd;
858 while (SYMBOLP (fun))
860 Lisp_Object tmp = Fget (fun, Qinteractive_form);
861 if (!NILP (tmp))
862 return tmp;
863 else
864 fun = Fsymbol_function (fun);
867 if (SUBRP (fun))
869 const char *spec = XSUBR (fun)->intspec;
870 if (spec)
871 return list2 (Qinteractive,
872 (*spec != '(') ? build_string (spec) :
873 Fcar (Fread_from_string (build_string (spec), Qnil, Qnil)));
875 else if (COMPILEDP (fun))
877 if ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE)
878 return list2 (Qinteractive, AREF (fun, COMPILED_INTERACTIVE));
880 else if (AUTOLOADP (fun))
881 return Finteractive_form (Fautoload_do_load (fun, cmd, Qnil));
882 else if (CONSP (fun))
884 Lisp_Object funcar = XCAR (fun);
885 if (EQ (funcar, Qclosure))
886 return Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun))));
887 else if (EQ (funcar, Qlambda))
888 return Fassq (Qinteractive, Fcdr (XCDR (fun)));
890 return Qnil;
894 /***********************************************************************
895 Getting and Setting Values of Symbols
896 ***********************************************************************/
898 /* Return the symbol holding SYMBOL's value. Signal
899 `cyclic-variable-indirection' if SYMBOL's chain of variable
900 indirections contains a loop. */
902 struct Lisp_Symbol *
903 indirect_variable (struct Lisp_Symbol *symbol)
905 struct Lisp_Symbol *tortoise, *hare;
907 hare = tortoise = symbol;
909 while (hare->redirect == SYMBOL_VARALIAS)
911 hare = SYMBOL_ALIAS (hare);
912 if (hare->redirect != SYMBOL_VARALIAS)
913 break;
915 hare = SYMBOL_ALIAS (hare);
916 tortoise = SYMBOL_ALIAS (tortoise);
918 if (hare == tortoise)
920 Lisp_Object tem;
921 XSETSYMBOL (tem, symbol);
922 xsignal1 (Qcyclic_variable_indirection, tem);
926 return hare;
930 DEFUN ("indirect-variable", Findirect_variable, Sindirect_variable, 1, 1, 0,
931 doc: /* Return the variable at the end of OBJECT's variable chain.
932 If OBJECT is a symbol, follow its variable indirections (if any), and
933 return the variable at the end of the chain of aliases. See Info node
934 `(elisp)Variable Aliases'.
936 If OBJECT is not a symbol, just return it. If there is a loop in the
937 chain of aliases, signal a `cyclic-variable-indirection' error. */)
938 (Lisp_Object object)
940 if (SYMBOLP (object))
942 struct Lisp_Symbol *sym = indirect_variable (XSYMBOL (object));
943 XSETSYMBOL (object, sym);
945 return object;
949 /* Given the raw contents of a symbol value cell,
950 return the Lisp value of the symbol.
951 This does not handle buffer-local variables; use
952 swap_in_symval_forwarding for that. */
954 Lisp_Object
955 do_symval_forwarding (register union Lisp_Fwd *valcontents)
957 register Lisp_Object val;
958 switch (XFWDTYPE (valcontents))
960 case Lisp_Fwd_Int:
961 XSETINT (val, *XINTFWD (valcontents)->intvar);
962 return val;
964 case Lisp_Fwd_Bool:
965 return (*XBOOLFWD (valcontents)->boolvar ? Qt : Qnil);
967 case Lisp_Fwd_Obj:
968 return *XOBJFWD (valcontents)->objvar;
970 case Lisp_Fwd_Buffer_Obj:
971 return per_buffer_value (current_buffer,
972 XBUFFER_OBJFWD (valcontents)->offset);
974 case Lisp_Fwd_Kboard_Obj:
975 /* We used to simply use current_kboard here, but from Lisp
976 code, its value is often unexpected. It seems nicer to
977 allow constructions like this to work as intuitively expected:
979 (with-selected-frame frame
980 (define-key local-function-map "\eOP" [f1]))
982 On the other hand, this affects the semantics of
983 last-command and real-last-command, and people may rely on
984 that. I took a quick look at the Lisp codebase, and I
985 don't think anything will break. --lorentey */
986 return *(Lisp_Object *)(XKBOARD_OBJFWD (valcontents)->offset
987 + (char *)FRAME_KBOARD (SELECTED_FRAME ()));
988 default: emacs_abort ();
992 /* Used to signal a user-friendly error when symbol WRONG is
993 not a member of CHOICE, which should be a list of symbols. */
995 void
996 wrong_choice (Lisp_Object choice, Lisp_Object wrong)
998 ptrdiff_t i = 0, len = XINT (Flength (choice));
999 Lisp_Object obj, *args;
1000 AUTO_STRING (one_of, "One of ");
1001 AUTO_STRING (comma, ", ");
1002 AUTO_STRING (or, " or ");
1003 AUTO_STRING (should_be_specified, " should be specified");
1005 USE_SAFE_ALLOCA;
1006 SAFE_ALLOCA_LISP (args, len * 2 + 1);
1008 args[i++] = one_of;
1010 for (obj = choice; !NILP (obj); obj = XCDR (obj))
1012 args[i++] = SYMBOL_NAME (XCAR (obj));
1013 args[i++] = (NILP (XCDR (obj)) ? should_be_specified
1014 : NILP (XCDR (XCDR (obj))) ? or : comma);
1017 obj = Fconcat (i, args);
1018 SAFE_FREE ();
1019 xsignal2 (Qerror, obj, wrong);
1022 /* Used to signal a user-friendly error if WRONG is not a number or
1023 integer/floating-point number outsize of inclusive MIN..MAX range. */
1025 static void
1026 wrong_range (Lisp_Object min, Lisp_Object max, Lisp_Object wrong)
1028 AUTO_STRING (value_should_be_from, "Value should be from ");
1029 AUTO_STRING (to, " to ");
1030 xsignal2 (Qerror,
1031 CALLN (Fconcat, value_should_be_from, Fnumber_to_string (min),
1032 to, Fnumber_to_string (max)),
1033 wrong);
1036 /* Store NEWVAL into SYMBOL, where VALCONTENTS is found in the value cell
1037 of SYMBOL. If SYMBOL is buffer-local, VALCONTENTS should be the
1038 buffer-independent contents of the value cell: forwarded just one
1039 step past the buffer-localness.
1041 BUF non-zero means set the value in buffer BUF instead of the
1042 current buffer. This only plays a role for per-buffer variables. */
1044 static void
1045 store_symval_forwarding (union Lisp_Fwd *valcontents, register Lisp_Object newval, struct buffer *buf)
1047 switch (XFWDTYPE (valcontents))
1049 case Lisp_Fwd_Int:
1050 CHECK_NUMBER (newval);
1051 *XINTFWD (valcontents)->intvar = XINT (newval);
1052 break;
1054 case Lisp_Fwd_Bool:
1055 *XBOOLFWD (valcontents)->boolvar = !NILP (newval);
1056 break;
1058 case Lisp_Fwd_Obj:
1059 *XOBJFWD (valcontents)->objvar = newval;
1061 /* If this variable is a default for something stored
1062 in the buffer itself, such as default-fill-column,
1063 find the buffers that don't have local values for it
1064 and update them. */
1065 if (XOBJFWD (valcontents)->objvar > (Lisp_Object *) &buffer_defaults
1066 && XOBJFWD (valcontents)->objvar < (Lisp_Object *) (&buffer_defaults + 1))
1068 int offset = ((char *) XOBJFWD (valcontents)->objvar
1069 - (char *) &buffer_defaults);
1070 int idx = PER_BUFFER_IDX (offset);
1072 Lisp_Object tail, buf;
1074 if (idx <= 0)
1075 break;
1077 FOR_EACH_LIVE_BUFFER (tail, buf)
1079 struct buffer *b = XBUFFER (buf);
1081 if (! PER_BUFFER_VALUE_P (b, idx))
1082 set_per_buffer_value (b, offset, newval);
1085 break;
1087 case Lisp_Fwd_Buffer_Obj:
1089 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1090 Lisp_Object predicate = XBUFFER_OBJFWD (valcontents)->predicate;
1092 if (!NILP (newval))
1094 if (SYMBOLP (predicate))
1096 Lisp_Object prop;
1098 if ((prop = Fget (predicate, Qchoice), !NILP (prop)))
1100 if (NILP (Fmemq (newval, prop)))
1101 wrong_choice (prop, newval);
1103 else if ((prop = Fget (predicate, Qrange), !NILP (prop)))
1105 Lisp_Object min = XCAR (prop), max = XCDR (prop);
1107 if (!NUMBERP (newval)
1108 || !NILP (arithcompare (newval, min, ARITH_LESS))
1109 || !NILP (arithcompare (newval, max, ARITH_GRTR)))
1110 wrong_range (min, max, newval);
1112 else if (FUNCTIONP (predicate))
1114 if (NILP (call1 (predicate, newval)))
1115 wrong_type_argument (predicate, newval);
1119 if (buf == NULL)
1120 buf = current_buffer;
1121 set_per_buffer_value (buf, offset, newval);
1123 break;
1125 case Lisp_Fwd_Kboard_Obj:
1127 char *base = (char *) FRAME_KBOARD (SELECTED_FRAME ());
1128 char *p = base + XKBOARD_OBJFWD (valcontents)->offset;
1129 *(Lisp_Object *) p = newval;
1131 break;
1133 default:
1134 emacs_abort (); /* goto def; */
1138 /* Set up SYMBOL to refer to its global binding. This makes it safe
1139 to alter the status of other bindings. BEWARE: this may be called
1140 during the mark phase of GC, where we assume that Lisp_Object slots
1141 of BLV are marked after this function has changed them. */
1143 void
1144 swap_in_global_binding (struct Lisp_Symbol *symbol)
1146 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (symbol);
1148 /* Unload the previously loaded binding. */
1149 if (blv->fwd)
1150 set_blv_value (blv, do_symval_forwarding (blv->fwd));
1152 /* Select the global binding in the symbol. */
1153 set_blv_valcell (blv, blv->defcell);
1154 if (blv->fwd)
1155 store_symval_forwarding (blv->fwd, XCDR (blv->defcell), NULL);
1157 /* Indicate that the global binding is set up now. */
1158 set_blv_where (blv, Qnil);
1159 set_blv_found (blv, 0);
1162 /* Set up the buffer-local symbol SYMBOL for validity in the current buffer.
1163 VALCONTENTS is the contents of its value cell,
1164 which points to a struct Lisp_Buffer_Local_Value.
1166 Return the value forwarded one step past the buffer-local stage.
1167 This could be another forwarding pointer. */
1169 static void
1170 swap_in_symval_forwarding (struct Lisp_Symbol *symbol, struct Lisp_Buffer_Local_Value *blv)
1172 register Lisp_Object tem1;
1174 eassert (blv == SYMBOL_BLV (symbol));
1176 tem1 = blv->where;
1178 if (NILP (tem1)
1179 || current_buffer != XBUFFER (tem1))
1182 /* Unload the previously loaded binding. */
1183 tem1 = blv->valcell;
1184 if (blv->fwd)
1185 set_blv_value (blv, do_symval_forwarding (blv->fwd));
1186 /* Choose the new binding. */
1188 Lisp_Object var;
1189 XSETSYMBOL (var, symbol);
1190 tem1 = assq_no_quit (var, BVAR (current_buffer, local_var_alist));
1191 set_blv_where (blv, Fcurrent_buffer ());
1193 if (!(blv->found = !NILP (tem1)))
1194 tem1 = blv->defcell;
1196 /* Load the new binding. */
1197 set_blv_valcell (blv, tem1);
1198 if (blv->fwd)
1199 store_symval_forwarding (blv->fwd, blv_value (blv), NULL);
1203 /* Find the value of a symbol, returning Qunbound if it's not bound.
1204 This is helpful for code which just wants to get a variable's value
1205 if it has one, without signaling an error.
1206 Note that it must not be possible to quit
1207 within this function. Great care is required for this. */
1209 Lisp_Object
1210 find_symbol_value (Lisp_Object symbol)
1212 struct Lisp_Symbol *sym;
1214 CHECK_SYMBOL (symbol);
1215 sym = XSYMBOL (symbol);
1217 start:
1218 switch (sym->redirect)
1220 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1221 case SYMBOL_PLAINVAL: return SYMBOL_VAL (sym);
1222 case SYMBOL_LOCALIZED:
1224 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1225 swap_in_symval_forwarding (sym, blv);
1226 return blv->fwd ? do_symval_forwarding (blv->fwd) : blv_value (blv);
1228 /* FALLTHROUGH */
1229 case SYMBOL_FORWARDED:
1230 return do_symval_forwarding (SYMBOL_FWD (sym));
1231 default: emacs_abort ();
1235 DEFUN ("symbol-value", Fsymbol_value, Ssymbol_value, 1, 1, 0,
1236 doc: /* Return SYMBOL's value. Error if that is void.
1237 Note that if `lexical-binding' is in effect, this returns the
1238 global value outside of any lexical scope. */)
1239 (Lisp_Object symbol)
1241 Lisp_Object val;
1243 val = find_symbol_value (symbol);
1244 if (!EQ (val, Qunbound))
1245 return val;
1247 xsignal1 (Qvoid_variable, symbol);
1250 DEFUN ("set", Fset, Sset, 2, 2, 0,
1251 doc: /* Set SYMBOL's value to NEWVAL, and return NEWVAL. */)
1252 (register Lisp_Object symbol, Lisp_Object newval)
1254 set_internal (symbol, newval, Qnil, SET_INTERNAL_SET);
1255 return newval;
1258 /* Store the value NEWVAL into SYMBOL.
1259 If buffer-locality is an issue, WHERE specifies which context to use.
1260 (nil stands for the current buffer/frame).
1262 If BINDFLAG is SET_INTERNAL_SET, then if this symbol is supposed to
1263 become local in every buffer where it is set, then we make it
1264 local. If BINDFLAG is SET_INTERNAL_BIND or SET_INTERNAL_UNBIND, we
1265 don't do that. */
1267 void
1268 set_internal (Lisp_Object symbol, Lisp_Object newval, Lisp_Object where,
1269 enum Set_Internal_Bind bindflag)
1271 bool voide = EQ (newval, Qunbound);
1272 struct Lisp_Symbol *sym;
1273 Lisp_Object tem1;
1275 /* If restoring in a dead buffer, do nothing. */
1276 /* if (BUFFERP (where) && NILP (XBUFFER (where)->name))
1277 return; */
1279 CHECK_SYMBOL (symbol);
1280 sym = XSYMBOL (symbol);
1281 switch (sym->trapped_write)
1283 case SYMBOL_NOWRITE:
1284 if (NILP (Fkeywordp (symbol))
1285 || !EQ (newval, Fsymbol_value (symbol)))
1286 xsignal1 (Qsetting_constant, symbol);
1287 else
1288 /* Allow setting keywords to their own value. */
1289 return;
1291 case SYMBOL_TRAPPED_WRITE:
1292 /* Setting due to thread-switching doesn't count. */
1293 if (bindflag != SET_INTERNAL_THREAD_SWITCH)
1294 notify_variable_watchers (symbol, voide? Qnil : newval,
1295 (bindflag == SET_INTERNAL_BIND? Qlet :
1296 bindflag == SET_INTERNAL_UNBIND? Qunlet :
1297 voide? Qmakunbound : Qset),
1298 where);
1299 /* FALLTHROUGH! */
1300 case SYMBOL_UNTRAPPED_WRITE:
1301 break;
1303 default: emacs_abort ();
1306 start:
1307 switch (sym->redirect)
1309 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1310 case SYMBOL_PLAINVAL: SET_SYMBOL_VAL (sym , newval); return;
1311 case SYMBOL_LOCALIZED:
1313 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1314 if (NILP (where))
1315 XSETBUFFER (where, current_buffer);
1317 /* If the current buffer is not the buffer whose binding is
1318 loaded, or if it's a Lisp_Buffer_Local_Value and
1319 the default binding is loaded, the loaded binding may be the
1320 wrong one. */
1321 if (!EQ (blv->where, where)
1322 /* Also unload a global binding (if the var is local_if_set). */
1323 || (EQ (blv->valcell, blv->defcell)))
1325 /* The currently loaded binding is not necessarily valid.
1326 We need to unload it, and choose a new binding. */
1328 /* Write out `realvalue' to the old loaded binding. */
1329 if (blv->fwd)
1330 set_blv_value (blv, do_symval_forwarding (blv->fwd));
1332 /* Find the new binding. */
1333 XSETSYMBOL (symbol, sym); /* May have changed via aliasing. */
1334 tem1 = assq_no_quit (symbol,
1335 BVAR (XBUFFER (where), local_var_alist));
1336 set_blv_where (blv, where);
1337 blv->found = 1;
1339 if (NILP (tem1))
1341 /* This buffer still sees the default value. */
1343 /* If the variable is a Lisp_Some_Buffer_Local_Value,
1344 or if this is `let' rather than `set',
1345 make CURRENT-ALIST-ELEMENT point to itself,
1346 indicating that we're seeing the default value.
1347 Likewise if the variable has been let-bound
1348 in the current buffer. */
1349 if (bindflag || !blv->local_if_set
1350 || let_shadows_buffer_binding_p (sym))
1352 blv->found = 0;
1353 tem1 = blv->defcell;
1355 /* If it's a local_if_set, being set not bound,
1356 and we're not within a let that was made for this buffer,
1357 create a new buffer-local binding for the variable.
1358 That means, give this buffer a new assoc for a local value
1359 and load that binding. */
1360 else
1362 tem1 = Fcons (symbol, XCDR (blv->defcell));
1363 bset_local_var_alist
1364 (XBUFFER (where),
1365 Fcons (tem1, BVAR (XBUFFER (where), local_var_alist)));
1369 /* Record which binding is now loaded. */
1370 set_blv_valcell (blv, tem1);
1373 /* Store the new value in the cons cell. */
1374 set_blv_value (blv, newval);
1376 if (blv->fwd)
1378 if (voide)
1379 /* If storing void (making the symbol void), forward only through
1380 buffer-local indicator, not through Lisp_Objfwd, etc. */
1381 blv->fwd = NULL;
1382 else
1383 store_symval_forwarding (blv->fwd, newval,
1384 BUFFERP (where)
1385 ? XBUFFER (where) : current_buffer);
1387 break;
1389 case SYMBOL_FORWARDED:
1391 struct buffer *buf
1392 = BUFFERP (where) ? XBUFFER (where) : current_buffer;
1393 union Lisp_Fwd *innercontents = SYMBOL_FWD (sym);
1394 if (BUFFER_OBJFWDP (innercontents))
1396 int offset = XBUFFER_OBJFWD (innercontents)->offset;
1397 int idx = PER_BUFFER_IDX (offset);
1398 if (idx > 0
1399 && bindflag == SET_INTERNAL_SET
1400 && !let_shadows_buffer_binding_p (sym))
1401 SET_PER_BUFFER_VALUE_P (buf, idx, 1);
1404 if (voide)
1405 { /* If storing void (making the symbol void), forward only through
1406 buffer-local indicator, not through Lisp_Objfwd, etc. */
1407 sym->redirect = SYMBOL_PLAINVAL;
1408 SET_SYMBOL_VAL (sym, newval);
1410 else
1411 store_symval_forwarding (/* sym, */ innercontents, newval, buf);
1412 break;
1414 default: emacs_abort ();
1416 return;
1419 static void
1420 set_symbol_trapped_write (Lisp_Object symbol, enum symbol_trapped_write trap)
1422 struct Lisp_Symbol* sym = XSYMBOL (symbol);
1423 if (sym->trapped_write == SYMBOL_NOWRITE)
1424 xsignal1 (Qtrapping_constant, symbol);
1425 sym->trapped_write = trap;
1428 static void
1429 restore_symbol_trapped_write (Lisp_Object symbol)
1431 set_symbol_trapped_write (symbol, SYMBOL_TRAPPED_WRITE);
1434 static void
1435 harmonize_variable_watchers (Lisp_Object alias, Lisp_Object base_variable)
1437 if (!EQ (base_variable, alias)
1438 && EQ (base_variable, Findirect_variable (alias)))
1439 set_symbol_trapped_write
1440 (alias, XSYMBOL (base_variable)->trapped_write);
1443 DEFUN ("add-variable-watcher", Fadd_variable_watcher, Sadd_variable_watcher,
1444 2, 2, 0,
1445 doc: /* Cause WATCH-FUNCTION to be called when SYMBOL is set.
1447 It will be called with 4 arguments: (SYMBOL NEWVAL OPERATION WHERE).
1448 SYMBOL is the variable being changed.
1449 NEWVAL is the value it will be changed to.
1450 OPERATION is a symbol representing the kind of change, one of: `set',
1451 `let', `unlet', `makunbound', and `defvaralias'.
1452 WHERE is a buffer if the buffer-local value of the variable being
1453 changed, nil otherwise.
1455 All writes to aliases of SYMBOL will call WATCH-FUNCTION too. */)
1456 (Lisp_Object symbol, Lisp_Object watch_function)
1458 symbol = Findirect_variable (symbol);
1459 set_symbol_trapped_write (symbol, SYMBOL_TRAPPED_WRITE);
1460 map_obarray (Vobarray, harmonize_variable_watchers, symbol);
1462 Lisp_Object watchers = Fget (symbol, Qwatchers);
1463 Lisp_Object member = Fmember (watch_function, watchers);
1464 if (NILP (member))
1465 Fput (symbol, Qwatchers, Fcons (watch_function, watchers));
1466 return Qnil;
1469 DEFUN ("remove-variable-watcher", Fremove_variable_watcher, Sremove_variable_watcher,
1470 2, 2, 0,
1471 doc: /* Undo the effect of `add-variable-watcher'.
1472 Remove WATCH-FUNCTION from the list of functions to be called when
1473 SYMBOL (or its aliases) are set. */)
1474 (Lisp_Object symbol, Lisp_Object watch_function)
1476 symbol = Findirect_variable (symbol);
1477 Lisp_Object watchers = Fget (symbol, Qwatchers);
1478 watchers = Fdelete (watch_function, watchers);
1479 if (NILP (watchers))
1481 set_symbol_trapped_write (symbol, SYMBOL_UNTRAPPED_WRITE);
1482 map_obarray (Vobarray, harmonize_variable_watchers, symbol);
1484 Fput (symbol, Qwatchers, watchers);
1485 return Qnil;
1488 DEFUN ("get-variable-watchers", Fget_variable_watchers, Sget_variable_watchers,
1489 1, 1, 0,
1490 doc: /* Return a list of SYMBOL's active watchers. */)
1491 (Lisp_Object symbol)
1493 return (SYMBOL_TRAPPED_WRITE_P (symbol) == SYMBOL_TRAPPED_WRITE)
1494 ? Fget (Findirect_variable (symbol), Qwatchers)
1495 : Qnil;
1498 void
1499 notify_variable_watchers (Lisp_Object symbol,
1500 Lisp_Object newval,
1501 Lisp_Object operation,
1502 Lisp_Object where)
1504 symbol = Findirect_variable (symbol);
1506 ptrdiff_t count = SPECPDL_INDEX ();
1507 record_unwind_protect (restore_symbol_trapped_write, symbol);
1508 /* Avoid recursion. */
1509 set_symbol_trapped_write (symbol, SYMBOL_UNTRAPPED_WRITE);
1511 if (NILP (where)
1512 && !EQ (operation, Qset_default) && !EQ (operation, Qmakunbound)
1513 && !NILP (Flocal_variable_if_set_p (symbol, Fcurrent_buffer ())))
1515 XSETBUFFER (where, current_buffer);
1518 if (EQ (operation, Qset_default))
1519 operation = Qset;
1521 for (Lisp_Object watchers = Fget (symbol, Qwatchers);
1522 CONSP (watchers);
1523 watchers = XCDR (watchers))
1525 Lisp_Object watcher = XCAR (watchers);
1526 /* Call subr directly to avoid gc. */
1527 if (SUBRP (watcher))
1529 Lisp_Object args[] = { symbol, newval, operation, where };
1530 funcall_subr (XSUBR (watcher), ARRAYELTS (args), args);
1532 else
1533 CALLN (Ffuncall, watcher, symbol, newval, operation, where);
1536 unbind_to (count, Qnil);
1540 /* Access or set a buffer-local symbol's default value. */
1542 /* Return the default value of SYMBOL, but don't check for voidness.
1543 Return Qunbound if it is void. */
1545 static Lisp_Object
1546 default_value (Lisp_Object symbol)
1548 struct Lisp_Symbol *sym;
1550 CHECK_SYMBOL (symbol);
1551 sym = XSYMBOL (symbol);
1553 start:
1554 switch (sym->redirect)
1556 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1557 case SYMBOL_PLAINVAL: return SYMBOL_VAL (sym);
1558 case SYMBOL_LOCALIZED:
1560 /* If var is set up for a buffer that lacks a local value for it,
1561 the current value is nominally the default value.
1562 But the `realvalue' slot may be more up to date, since
1563 ordinary setq stores just that slot. So use that. */
1564 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1565 if (blv->fwd && EQ (blv->valcell, blv->defcell))
1566 return do_symval_forwarding (blv->fwd);
1567 else
1568 return XCDR (blv->defcell);
1570 case SYMBOL_FORWARDED:
1572 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
1574 /* For a built-in buffer-local variable, get the default value
1575 rather than letting do_symval_forwarding get the current value. */
1576 if (BUFFER_OBJFWDP (valcontents))
1578 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1579 if (PER_BUFFER_IDX (offset) != 0)
1580 return per_buffer_default (offset);
1583 /* For other variables, get the current value. */
1584 return do_symval_forwarding (valcontents);
1586 default: emacs_abort ();
1590 DEFUN ("default-boundp", Fdefault_boundp, Sdefault_boundp, 1, 1, 0,
1591 doc: /* Return t if SYMBOL has a non-void default value.
1592 This is the value that is seen in buffers that do not have their own values
1593 for this variable. */)
1594 (Lisp_Object symbol)
1596 register Lisp_Object value;
1598 value = default_value (symbol);
1599 return (EQ (value, Qunbound) ? Qnil : Qt);
1602 DEFUN ("default-value", Fdefault_value, Sdefault_value, 1, 1, 0,
1603 doc: /* Return SYMBOL's default value.
1604 This is the value that is seen in buffers that do not have their own values
1605 for this variable. The default value is meaningful for variables with
1606 local bindings in certain buffers. */)
1607 (Lisp_Object symbol)
1609 Lisp_Object value = default_value (symbol);
1610 if (!EQ (value, Qunbound))
1611 return value;
1613 xsignal1 (Qvoid_variable, symbol);
1616 void
1617 set_default_internal (Lisp_Object symbol, Lisp_Object value,
1618 enum Set_Internal_Bind bindflag)
1620 struct Lisp_Symbol *sym;
1622 CHECK_SYMBOL (symbol);
1623 sym = XSYMBOL (symbol);
1624 switch (sym->trapped_write)
1626 case SYMBOL_NOWRITE:
1627 if (NILP (Fkeywordp (symbol))
1628 || !EQ (value, Fsymbol_value (symbol)))
1629 xsignal1 (Qsetting_constant, symbol);
1630 else
1631 /* Allow setting keywords to their own value. */
1632 return;
1634 case SYMBOL_TRAPPED_WRITE:
1635 /* Don't notify here if we're going to call Fset anyway. */
1636 if (sym->redirect != SYMBOL_PLAINVAL
1637 /* Setting due to thread switching doesn't count. */
1638 && bindflag != SET_INTERNAL_THREAD_SWITCH)
1639 notify_variable_watchers (symbol, value, Qset_default, Qnil);
1640 /* FALLTHROUGH! */
1641 case SYMBOL_UNTRAPPED_WRITE:
1642 break;
1644 default: emacs_abort ();
1647 start:
1648 switch (sym->redirect)
1650 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1651 case SYMBOL_PLAINVAL: set_internal (symbol, value, Qnil, bindflag); return;
1652 case SYMBOL_LOCALIZED:
1654 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1656 /* Store new value into the DEFAULT-VALUE slot. */
1657 XSETCDR (blv->defcell, value);
1659 /* If the default binding is now loaded, set the REALVALUE slot too. */
1660 if (blv->fwd && EQ (blv->defcell, blv->valcell))
1661 store_symval_forwarding (blv->fwd, value, NULL);
1662 return;
1664 case SYMBOL_FORWARDED:
1666 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
1668 /* Handle variables like case-fold-search that have special slots
1669 in the buffer.
1670 Make them work apparently like Lisp_Buffer_Local_Value variables. */
1671 if (BUFFER_OBJFWDP (valcontents))
1673 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1674 int idx = PER_BUFFER_IDX (offset);
1676 set_per_buffer_default (offset, value);
1678 /* If this variable is not always local in all buffers,
1679 set it in the buffers that don't nominally have a local value. */
1680 if (idx > 0)
1682 struct buffer *b;
1684 FOR_EACH_BUFFER (b)
1685 if (!PER_BUFFER_VALUE_P (b, idx))
1686 set_per_buffer_value (b, offset, value);
1689 else
1690 set_internal (symbol, value, Qnil, bindflag);
1691 return;
1693 default: emacs_abort ();
1697 DEFUN ("set-default", Fset_default, Sset_default, 2, 2, 0,
1698 doc: /* Set SYMBOL's default value to VALUE. SYMBOL and VALUE are evaluated.
1699 The default value is seen in buffers that do not have their own values
1700 for this variable. */)
1701 (Lisp_Object symbol, Lisp_Object value)
1703 set_default_internal (symbol, value, SET_INTERNAL_SET);
1704 return value;
1707 DEFUN ("setq-default", Fsetq_default, Ssetq_default, 0, UNEVALLED, 0,
1708 doc: /* Set the default value of variable VAR to VALUE.
1709 VAR, the variable name, is literal (not evaluated);
1710 VALUE is an expression: it is evaluated and its value returned.
1711 The default value of a variable is seen in buffers
1712 that do not have their own values for the variable.
1714 More generally, you can use multiple variables and values, as in
1715 (setq-default VAR VALUE VAR VALUE...)
1716 This sets each VAR's default value to the corresponding VALUE.
1717 The VALUE for the Nth VAR can refer to the new default values
1718 of previous VARs.
1719 usage: (setq-default [VAR VALUE]...) */)
1720 (Lisp_Object args)
1722 Lisp_Object args_left, symbol, val;
1724 args_left = val = args;
1726 while (CONSP (args_left))
1728 val = eval_sub (Fcar (XCDR (args_left)));
1729 symbol = XCAR (args_left);
1730 Fset_default (symbol, val);
1731 args_left = Fcdr (XCDR (args_left));
1734 return val;
1737 /* Lisp functions for creating and removing buffer-local variables. */
1739 union Lisp_Val_Fwd
1741 Lisp_Object value;
1742 union Lisp_Fwd *fwd;
1745 static struct Lisp_Buffer_Local_Value *
1746 make_blv (struct Lisp_Symbol *sym, bool forwarded,
1747 union Lisp_Val_Fwd valcontents)
1749 struct Lisp_Buffer_Local_Value *blv = xmalloc (sizeof *blv);
1750 Lisp_Object symbol;
1751 Lisp_Object tem;
1753 XSETSYMBOL (symbol, sym);
1754 tem = Fcons (symbol, (forwarded
1755 ? do_symval_forwarding (valcontents.fwd)
1756 : valcontents.value));
1758 /* Buffer_Local_Values cannot have as realval a buffer-local
1759 or keyboard-local forwarding. */
1760 eassert (!(forwarded && BUFFER_OBJFWDP (valcontents.fwd)));
1761 eassert (!(forwarded && KBOARD_OBJFWDP (valcontents.fwd)));
1762 blv->fwd = forwarded ? valcontents.fwd : NULL;
1763 set_blv_where (blv, Qnil);
1764 blv->local_if_set = 0;
1765 set_blv_defcell (blv, tem);
1766 set_blv_valcell (blv, tem);
1767 set_blv_found (blv, 0);
1768 return blv;
1771 DEFUN ("make-variable-buffer-local", Fmake_variable_buffer_local,
1772 Smake_variable_buffer_local, 1, 1, "vMake Variable Buffer Local: ",
1773 doc: /* Make VARIABLE become buffer-local whenever it is set.
1774 At any time, the value for the current buffer is in effect,
1775 unless the variable has never been set in this buffer,
1776 in which case the default value is in effect.
1777 Note that binding the variable with `let', or setting it while
1778 a `let'-style binding made in this buffer is in effect,
1779 does not make the variable buffer-local. Return VARIABLE.
1781 This globally affects all uses of this variable, so it belongs together with
1782 the variable declaration, rather than with its uses (if you just want to make
1783 a variable local to the current buffer for one particular use, use
1784 `make-local-variable'). Buffer-local bindings are normally cleared
1785 while setting up a new major mode, unless they have a `permanent-local'
1786 property.
1788 The function `default-value' gets the default value and `set-default' sets it. */)
1789 (register Lisp_Object variable)
1791 struct Lisp_Symbol *sym;
1792 struct Lisp_Buffer_Local_Value *blv = NULL;
1793 union Lisp_Val_Fwd valcontents;
1794 bool forwarded;
1796 CHECK_SYMBOL (variable);
1797 sym = XSYMBOL (variable);
1799 start:
1800 switch (sym->redirect)
1802 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1803 case SYMBOL_PLAINVAL:
1804 forwarded = 0; valcontents.value = SYMBOL_VAL (sym);
1805 if (EQ (valcontents.value, Qunbound))
1806 valcontents.value = Qnil;
1807 break;
1808 case SYMBOL_LOCALIZED:
1809 blv = SYMBOL_BLV (sym);
1810 break;
1811 case SYMBOL_FORWARDED:
1812 forwarded = 1; valcontents.fwd = SYMBOL_FWD (sym);
1813 if (KBOARD_OBJFWDP (valcontents.fwd))
1814 error ("Symbol %s may not be buffer-local",
1815 SDATA (SYMBOL_NAME (variable)));
1816 else if (BUFFER_OBJFWDP (valcontents.fwd))
1817 return variable;
1818 break;
1819 default: emacs_abort ();
1822 if (SYMBOL_CONSTANT_P (variable))
1823 error ("Symbol %s may not be buffer-local", SDATA (SYMBOL_NAME (variable)));
1825 if (!blv)
1827 blv = make_blv (sym, forwarded, valcontents);
1828 sym->redirect = SYMBOL_LOCALIZED;
1829 SET_SYMBOL_BLV (sym, blv);
1831 Lisp_Object symbol;
1832 XSETSYMBOL (symbol, sym); /* In case `variable' is aliased. */
1833 if (let_shadows_global_binding_p (symbol))
1835 AUTO_STRING (format, "Making %s buffer-local while let-bound!");
1836 CALLN (Fmessage, format, SYMBOL_NAME (variable));
1841 blv->local_if_set = 1;
1842 return variable;
1845 DEFUN ("make-local-variable", Fmake_local_variable, Smake_local_variable,
1846 1, 1, "vMake Local Variable: ",
1847 doc: /* Make VARIABLE have a separate value in the current buffer.
1848 Other buffers will continue to share a common default value.
1849 \(The buffer-local value of VARIABLE starts out as the same value
1850 VARIABLE previously had. If VARIABLE was void, it remains void.)
1851 Return VARIABLE.
1853 If the variable is already arranged to become local when set,
1854 this function causes a local value to exist for this buffer,
1855 just as setting the variable would do.
1857 This function returns VARIABLE, and therefore
1858 (set (make-local-variable \\='VARIABLE) VALUE-EXP)
1859 works.
1861 See also `make-variable-buffer-local'.
1863 Do not use `make-local-variable' to make a hook variable buffer-local.
1864 Instead, use `add-hook' and specify t for the LOCAL argument. */)
1865 (Lisp_Object variable)
1867 Lisp_Object tem;
1868 bool forwarded;
1869 union Lisp_Val_Fwd valcontents;
1870 struct Lisp_Symbol *sym;
1871 struct Lisp_Buffer_Local_Value *blv = NULL;
1873 CHECK_SYMBOL (variable);
1874 sym = XSYMBOL (variable);
1876 start:
1877 switch (sym->redirect)
1879 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1880 case SYMBOL_PLAINVAL:
1881 forwarded = 0; valcontents.value = SYMBOL_VAL (sym); break;
1882 case SYMBOL_LOCALIZED:
1883 blv = SYMBOL_BLV (sym);
1884 break;
1885 case SYMBOL_FORWARDED:
1886 forwarded = 1; valcontents.fwd = SYMBOL_FWD (sym);
1887 if (KBOARD_OBJFWDP (valcontents.fwd))
1888 error ("Symbol %s may not be buffer-local",
1889 SDATA (SYMBOL_NAME (variable)));
1890 break;
1891 default: emacs_abort ();
1894 if (sym->trapped_write == SYMBOL_NOWRITE)
1895 error ("Symbol %s may not be buffer-local",
1896 SDATA (SYMBOL_NAME (variable)));
1898 if (blv ? blv->local_if_set
1899 : (forwarded && BUFFER_OBJFWDP (valcontents.fwd)))
1901 tem = Fboundp (variable);
1902 /* Make sure the symbol has a local value in this particular buffer,
1903 by setting it to the same value it already has. */
1904 Fset (variable, (EQ (tem, Qt) ? Fsymbol_value (variable) : Qunbound));
1905 return variable;
1907 if (!blv)
1909 blv = make_blv (sym, forwarded, valcontents);
1910 sym->redirect = SYMBOL_LOCALIZED;
1911 SET_SYMBOL_BLV (sym, blv);
1913 Lisp_Object symbol;
1914 XSETSYMBOL (symbol, sym); /* In case `variable' is aliased. */
1915 if (let_shadows_global_binding_p (symbol))
1917 AUTO_STRING (format, "Making %s local to %s while let-bound!");
1918 CALLN (Fmessage, format, SYMBOL_NAME (variable),
1919 BVAR (current_buffer, name));
1924 /* Make sure this buffer has its own value of symbol. */
1925 XSETSYMBOL (variable, sym); /* Update in case of aliasing. */
1926 tem = Fassq (variable, BVAR (current_buffer, local_var_alist));
1927 if (NILP (tem))
1929 if (let_shadows_buffer_binding_p (sym))
1931 AUTO_STRING (format,
1932 "Making %s buffer-local while locally let-bound!");
1933 CALLN (Fmessage, format, SYMBOL_NAME (variable));
1936 /* Swap out any local binding for some other buffer, and make
1937 sure the current value is permanently recorded, if it's the
1938 default value. */
1939 find_symbol_value (variable);
1941 bset_local_var_alist
1942 (current_buffer,
1943 Fcons (Fcons (variable, XCDR (blv->defcell)),
1944 BVAR (current_buffer, local_var_alist)));
1946 /* Make sure symbol does not think it is set up for this buffer;
1947 force it to look once again for this buffer's value. */
1948 if (current_buffer == XBUFFER (blv->where))
1949 set_blv_where (blv, Qnil);
1950 set_blv_found (blv, 0);
1953 /* If the symbol forwards into a C variable, then load the binding
1954 for this buffer now. If C code modifies the variable before we
1955 load the binding in, then that new value will clobber the default
1956 binding the next time we unload it. */
1957 if (blv->fwd)
1958 swap_in_symval_forwarding (sym, blv);
1960 return variable;
1963 DEFUN ("kill-local-variable", Fkill_local_variable, Skill_local_variable,
1964 1, 1, "vKill Local Variable: ",
1965 doc: /* Make VARIABLE no longer have a separate value in the current buffer.
1966 From now on the default value will apply in this buffer. Return VARIABLE. */)
1967 (register Lisp_Object variable)
1969 register Lisp_Object tem;
1970 struct Lisp_Buffer_Local_Value *blv;
1971 struct Lisp_Symbol *sym;
1973 CHECK_SYMBOL (variable);
1974 sym = XSYMBOL (variable);
1976 start:
1977 switch (sym->redirect)
1979 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1980 case SYMBOL_PLAINVAL: return variable;
1981 case SYMBOL_FORWARDED:
1983 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
1984 if (BUFFER_OBJFWDP (valcontents))
1986 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1987 int idx = PER_BUFFER_IDX (offset);
1989 if (idx > 0)
1991 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 0);
1992 set_per_buffer_value (current_buffer, offset,
1993 per_buffer_default (offset));
1996 return variable;
1998 case SYMBOL_LOCALIZED:
1999 blv = SYMBOL_BLV (sym);
2000 break;
2001 default: emacs_abort ();
2004 if (sym->trapped_write == SYMBOL_TRAPPED_WRITE)
2005 notify_variable_watchers (variable, Qnil, Qmakunbound, Fcurrent_buffer ());
2007 /* Get rid of this buffer's alist element, if any. */
2008 XSETSYMBOL (variable, sym); /* Propagate variable indirection. */
2009 tem = Fassq (variable, BVAR (current_buffer, local_var_alist));
2010 if (!NILP (tem))
2011 bset_local_var_alist
2012 (current_buffer,
2013 Fdelq (tem, BVAR (current_buffer, local_var_alist)));
2015 /* If the symbol is set up with the current buffer's binding
2016 loaded, recompute its value. We have to do it now, or else
2017 forwarded objects won't work right. */
2019 Lisp_Object buf; XSETBUFFER (buf, current_buffer);
2020 if (EQ (buf, blv->where))
2022 set_blv_where (blv, Qnil);
2023 blv->found = 0;
2024 find_symbol_value (variable);
2028 return variable;
2031 /* Lisp functions for creating and removing buffer-local variables. */
2033 DEFUN ("local-variable-p", Flocal_variable_p, Slocal_variable_p,
2034 1, 2, 0,
2035 doc: /* Non-nil if VARIABLE has a local binding in buffer BUFFER.
2036 BUFFER defaults to the current buffer. */)
2037 (Lisp_Object variable, Lisp_Object buffer)
2039 struct buffer *buf = decode_buffer (buffer);
2040 struct Lisp_Symbol *sym;
2042 CHECK_SYMBOL (variable);
2043 sym = XSYMBOL (variable);
2045 start:
2046 switch (sym->redirect)
2048 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
2049 case SYMBOL_PLAINVAL: return Qnil;
2050 case SYMBOL_LOCALIZED:
2052 Lisp_Object tail, elt, tmp;
2053 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
2054 XSETBUFFER (tmp, buf);
2055 XSETSYMBOL (variable, sym); /* Update in case of aliasing. */
2057 if (EQ (blv->where, tmp)) /* The binding is already loaded. */
2058 return blv_found (blv) ? Qt : Qnil;
2059 else
2060 for (tail = BVAR (buf, local_var_alist); CONSP (tail); tail = XCDR (tail))
2062 elt = XCAR (tail);
2063 if (EQ (variable, XCAR (elt)))
2064 return Qt;
2066 return Qnil;
2068 case SYMBOL_FORWARDED:
2070 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
2071 if (BUFFER_OBJFWDP (valcontents))
2073 int offset = XBUFFER_OBJFWD (valcontents)->offset;
2074 int idx = PER_BUFFER_IDX (offset);
2075 if (idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
2076 return Qt;
2078 return Qnil;
2080 default: emacs_abort ();
2084 DEFUN ("local-variable-if-set-p", Flocal_variable_if_set_p, Slocal_variable_if_set_p,
2085 1, 2, 0,
2086 doc: /* Non-nil if VARIABLE is local in buffer BUFFER when set there.
2087 BUFFER defaults to the current buffer.
2089 More precisely, return non-nil if either VARIABLE already has a local
2090 value in BUFFER, or if VARIABLE is automatically buffer-local (see
2091 `make-variable-buffer-local'). */)
2092 (register Lisp_Object variable, Lisp_Object buffer)
2094 struct Lisp_Symbol *sym;
2096 CHECK_SYMBOL (variable);
2097 sym = XSYMBOL (variable);
2099 start:
2100 switch (sym->redirect)
2102 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
2103 case SYMBOL_PLAINVAL: return Qnil;
2104 case SYMBOL_LOCALIZED:
2106 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
2107 if (blv->local_if_set)
2108 return Qt;
2109 XSETSYMBOL (variable, sym); /* Update in case of aliasing. */
2110 return Flocal_variable_p (variable, buffer);
2112 case SYMBOL_FORWARDED:
2113 /* All BUFFER_OBJFWD slots become local if they are set. */
2114 return (BUFFER_OBJFWDP (SYMBOL_FWD (sym)) ? Qt : Qnil);
2115 default: emacs_abort ();
2119 DEFUN ("variable-binding-locus", Fvariable_binding_locus, Svariable_binding_locus,
2120 1, 1, 0,
2121 doc: /* Return a value indicating where VARIABLE's current binding comes from.
2122 If the current binding is buffer-local, the value is the current buffer.
2123 If the current binding is global (the default), the value is nil. */)
2124 (register Lisp_Object variable)
2126 struct Lisp_Symbol *sym;
2128 CHECK_SYMBOL (variable);
2129 sym = XSYMBOL (variable);
2131 /* Make sure the current binding is actually swapped in. */
2132 find_symbol_value (variable);
2134 start:
2135 switch (sym->redirect)
2137 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
2138 case SYMBOL_PLAINVAL: return Qnil;
2139 case SYMBOL_FORWARDED:
2141 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
2142 if (KBOARD_OBJFWDP (valcontents))
2143 return Fframe_terminal (selected_frame);
2144 else if (!BUFFER_OBJFWDP (valcontents))
2145 return Qnil;
2147 /* FALLTHROUGH */
2148 case SYMBOL_LOCALIZED:
2149 /* For a local variable, record both the symbol and which
2150 buffer's or frame's value we are saving. */
2151 if (!NILP (Flocal_variable_p (variable, Qnil)))
2152 return Fcurrent_buffer ();
2153 else if (sym->redirect == SYMBOL_LOCALIZED
2154 && blv_found (SYMBOL_BLV (sym)))
2155 return SYMBOL_BLV (sym)->where;
2156 else
2157 return Qnil;
2158 default: emacs_abort ();
2162 /* This code is disabled now that we use the selected frame to return
2163 keyboard-local-values. */
2164 #if 0
2165 extern struct terminal *get_terminal (Lisp_Object display, int);
2167 DEFUN ("terminal-local-value", Fterminal_local_value,
2168 Sterminal_local_value, 2, 2, 0,
2169 doc: /* Return the terminal-local value of SYMBOL on TERMINAL.
2170 If SYMBOL is not a terminal-local variable, then return its normal
2171 value, like `symbol-value'.
2173 TERMINAL may be a terminal object, a frame, or nil (meaning the
2174 selected frame's terminal device). */)
2175 (Lisp_Object symbol, Lisp_Object terminal)
2177 Lisp_Object result;
2178 struct terminal *t = get_terminal (terminal, 1);
2179 push_kboard (t->kboard);
2180 result = Fsymbol_value (symbol);
2181 pop_kboard ();
2182 return result;
2185 DEFUN ("set-terminal-local-value", Fset_terminal_local_value,
2186 Sset_terminal_local_value, 3, 3, 0,
2187 doc: /* Set the terminal-local binding of SYMBOL on TERMINAL to VALUE.
2188 If VARIABLE is not a terminal-local variable, then set its normal
2189 binding, like `set'.
2191 TERMINAL may be a terminal object, a frame, or nil (meaning the
2192 selected frame's terminal device). */)
2193 (Lisp_Object symbol, Lisp_Object terminal, Lisp_Object value)
2195 Lisp_Object result;
2196 struct terminal *t = get_terminal (terminal, 1);
2197 push_kboard (d->kboard);
2198 result = Fset (symbol, value);
2199 pop_kboard ();
2200 return result;
2202 #endif
2204 /* Find the function at the end of a chain of symbol function indirections. */
2206 /* If OBJECT is a symbol, find the end of its function chain and
2207 return the value found there. If OBJECT is not a symbol, just
2208 return it. If there is a cycle in the function chain, signal a
2209 cyclic-function-indirection error.
2211 This is like Findirect_function, except that it doesn't signal an
2212 error if the chain ends up unbound. */
2213 Lisp_Object
2214 indirect_function (register Lisp_Object object)
2216 Lisp_Object tortoise, hare;
2218 hare = tortoise = object;
2220 for (;;)
2222 if (!SYMBOLP (hare) || NILP (hare))
2223 break;
2224 hare = XSYMBOL (hare)->function;
2225 if (!SYMBOLP (hare) || NILP (hare))
2226 break;
2227 hare = XSYMBOL (hare)->function;
2229 tortoise = XSYMBOL (tortoise)->function;
2231 if (EQ (hare, tortoise))
2232 xsignal1 (Qcyclic_function_indirection, object);
2235 return hare;
2238 DEFUN ("indirect-function", Findirect_function, Sindirect_function, 1, 2, 0,
2239 doc: /* Return the function at the end of OBJECT's function chain.
2240 If OBJECT is not a symbol, just return it. Otherwise, follow all
2241 function indirections to find the final function binding and return it.
2242 Signal a cyclic-function-indirection error if there is a loop in the
2243 function chain of symbols. */)
2244 (register Lisp_Object object, Lisp_Object noerror)
2246 Lisp_Object result;
2248 /* Optimize for no indirection. */
2249 result = object;
2250 if (SYMBOLP (result) && !NILP (result)
2251 && (result = XSYMBOL (result)->function, SYMBOLP (result)))
2252 result = indirect_function (result);
2253 if (!NILP (result))
2254 return result;
2256 return Qnil;
2259 /* Extract and set vector and string elements. */
2261 DEFUN ("aref", Faref, Saref, 2, 2, 0,
2262 doc: /* Return the element of ARRAY at index IDX.
2263 ARRAY may be a vector, a string, a char-table, a bool-vector,
2264 or a byte-code object. IDX starts at 0. */)
2265 (register Lisp_Object array, Lisp_Object idx)
2267 register EMACS_INT idxval;
2269 CHECK_NUMBER (idx);
2270 idxval = XINT (idx);
2271 if (STRINGP (array))
2273 int c;
2274 ptrdiff_t idxval_byte;
2276 if (idxval < 0 || idxval >= SCHARS (array))
2277 args_out_of_range (array, idx);
2278 if (! STRING_MULTIBYTE (array))
2279 return make_number ((unsigned char) SREF (array, idxval));
2280 idxval_byte = string_char_to_byte (array, idxval);
2282 c = STRING_CHAR (SDATA (array) + idxval_byte);
2283 return make_number (c);
2285 else if (BOOL_VECTOR_P (array))
2287 if (idxval < 0 || idxval >= bool_vector_size (array))
2288 args_out_of_range (array, idx);
2289 return bool_vector_ref (array, idxval);
2291 else if (CHAR_TABLE_P (array))
2293 CHECK_CHARACTER (idx);
2294 return CHAR_TABLE_REF (array, idxval);
2296 else
2298 ptrdiff_t size = 0;
2299 if (VECTORP (array))
2300 size = ASIZE (array);
2301 else if (COMPILEDP (array))
2302 size = ASIZE (array) & PSEUDOVECTOR_SIZE_MASK;
2303 else
2304 wrong_type_argument (Qarrayp, array);
2306 if (idxval < 0 || idxval >= size)
2307 args_out_of_range (array, idx);
2308 return AREF (array, idxval);
2312 DEFUN ("aset", Faset, Saset, 3, 3, 0,
2313 doc: /* Store into the element of ARRAY at index IDX the value NEWELT.
2314 Return NEWELT. ARRAY may be a vector, a string, a char-table or a
2315 bool-vector. IDX starts at 0. */)
2316 (register Lisp_Object array, Lisp_Object idx, Lisp_Object newelt)
2318 register EMACS_INT idxval;
2320 CHECK_NUMBER (idx);
2321 idxval = XINT (idx);
2322 CHECK_ARRAY (array, Qarrayp);
2324 if (VECTORP (array))
2326 CHECK_IMPURE (array, XVECTOR (array));
2327 if (idxval < 0 || idxval >= ASIZE (array))
2328 args_out_of_range (array, idx);
2329 ASET (array, idxval, newelt);
2331 else if (BOOL_VECTOR_P (array))
2333 if (idxval < 0 || idxval >= bool_vector_size (array))
2334 args_out_of_range (array, idx);
2335 bool_vector_set (array, idxval, !NILP (newelt));
2337 else if (CHAR_TABLE_P (array))
2339 CHECK_CHARACTER (idx);
2340 CHAR_TABLE_SET (array, idxval, newelt);
2342 else
2344 int c;
2346 CHECK_IMPURE (array, XSTRING (array));
2347 if (idxval < 0 || idxval >= SCHARS (array))
2348 args_out_of_range (array, idx);
2349 CHECK_CHARACTER (newelt);
2350 c = XFASTINT (newelt);
2352 if (STRING_MULTIBYTE (array))
2354 ptrdiff_t idxval_byte, nbytes;
2355 int prev_bytes, new_bytes;
2356 unsigned char workbuf[MAX_MULTIBYTE_LENGTH], *p0 = workbuf, *p1;
2358 nbytes = SBYTES (array);
2359 idxval_byte = string_char_to_byte (array, idxval);
2360 p1 = SDATA (array) + idxval_byte;
2361 prev_bytes = BYTES_BY_CHAR_HEAD (*p1);
2362 new_bytes = CHAR_STRING (c, p0);
2363 if (prev_bytes != new_bytes)
2365 /* We must relocate the string data. */
2366 ptrdiff_t nchars = SCHARS (array);
2367 USE_SAFE_ALLOCA;
2368 unsigned char *str = SAFE_ALLOCA (nbytes);
2370 memcpy (str, SDATA (array), nbytes);
2371 allocate_string_data (XSTRING (array), nchars,
2372 nbytes + new_bytes - prev_bytes);
2373 memcpy (SDATA (array), str, idxval_byte);
2374 p1 = SDATA (array) + idxval_byte;
2375 memcpy (p1 + new_bytes, str + idxval_byte + prev_bytes,
2376 nbytes - (idxval_byte + prev_bytes));
2377 SAFE_FREE ();
2378 clear_string_char_byte_cache ();
2380 while (new_bytes--)
2381 *p1++ = *p0++;
2383 else
2385 if (! SINGLE_BYTE_CHAR_P (c))
2387 ptrdiff_t i;
2389 for (i = SBYTES (array) - 1; i >= 0; i--)
2390 if (SREF (array, i) >= 0x80)
2391 args_out_of_range (array, newelt);
2392 /* ARRAY is an ASCII string. Convert it to a multibyte
2393 string, and try `aset' again. */
2394 STRING_SET_MULTIBYTE (array);
2395 return Faset (array, idx, newelt);
2397 SSET (array, idxval, c);
2401 return newelt;
2404 /* Arithmetic functions */
2406 Lisp_Object
2407 arithcompare (Lisp_Object num1, Lisp_Object num2, enum Arith_Comparison comparison)
2409 double f1 = 0, f2 = 0;
2410 bool floatp = 0;
2412 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num1);
2413 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num2);
2415 if (FLOATP (num1) || FLOATP (num2))
2417 floatp = 1;
2418 f1 = (FLOATP (num1)) ? XFLOAT_DATA (num1) : XINT (num1);
2419 f2 = (FLOATP (num2)) ? XFLOAT_DATA (num2) : XINT (num2);
2422 switch (comparison)
2424 case ARITH_EQUAL:
2425 if (floatp ? f1 == f2 : XINT (num1) == XINT (num2))
2426 return Qt;
2427 return Qnil;
2429 case ARITH_NOTEQUAL:
2430 if (floatp ? f1 != f2 : XINT (num1) != XINT (num2))
2431 return Qt;
2432 return Qnil;
2434 case ARITH_LESS:
2435 if (floatp ? f1 < f2 : XINT (num1) < XINT (num2))
2436 return Qt;
2437 return Qnil;
2439 case ARITH_LESS_OR_EQUAL:
2440 if (floatp ? f1 <= f2 : XINT (num1) <= XINT (num2))
2441 return Qt;
2442 return Qnil;
2444 case ARITH_GRTR:
2445 if (floatp ? f1 > f2 : XINT (num1) > XINT (num2))
2446 return Qt;
2447 return Qnil;
2449 case ARITH_GRTR_OR_EQUAL:
2450 if (floatp ? f1 >= f2 : XINT (num1) >= XINT (num2))
2451 return Qt;
2452 return Qnil;
2454 default:
2455 emacs_abort ();
2459 static Lisp_Object
2460 arithcompare_driver (ptrdiff_t nargs, Lisp_Object *args,
2461 enum Arith_Comparison comparison)
2463 ptrdiff_t argnum;
2464 for (argnum = 1; argnum < nargs; ++argnum)
2466 if (EQ (Qnil, arithcompare (args[argnum - 1], args[argnum], comparison)))
2467 return Qnil;
2469 return Qt;
2472 DEFUN ("=", Feqlsign, Seqlsign, 1, MANY, 0,
2473 doc: /* Return t if args, all numbers or markers, are equal.
2474 usage: (= NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2475 (ptrdiff_t nargs, Lisp_Object *args)
2477 return arithcompare_driver (nargs, args, ARITH_EQUAL);
2480 DEFUN ("<", Flss, Slss, 1, MANY, 0,
2481 doc: /* Return t if each arg (a number or marker), is less than the next arg.
2482 usage: (< NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2483 (ptrdiff_t nargs, Lisp_Object *args)
2485 return arithcompare_driver (nargs, args, ARITH_LESS);
2488 DEFUN (">", Fgtr, Sgtr, 1, MANY, 0,
2489 doc: /* Return t if each arg (a number or marker) is greater than the next arg.
2490 usage: (> NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2491 (ptrdiff_t nargs, Lisp_Object *args)
2493 return arithcompare_driver (nargs, args, ARITH_GRTR);
2496 DEFUN ("<=", Fleq, Sleq, 1, MANY, 0,
2497 doc: /* Return t if each arg (a number or marker) is less than or equal to the next.
2498 usage: (<= NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2499 (ptrdiff_t nargs, Lisp_Object *args)
2501 return arithcompare_driver (nargs, args, ARITH_LESS_OR_EQUAL);
2504 DEFUN (">=", Fgeq, Sgeq, 1, MANY, 0,
2505 doc: /* Return t if each arg (a number or marker) is greater than or equal to the next.
2506 usage: (>= NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2507 (ptrdiff_t nargs, Lisp_Object *args)
2509 return arithcompare_driver (nargs, args, ARITH_GRTR_OR_EQUAL);
2512 DEFUN ("/=", Fneq, Sneq, 2, 2, 0,
2513 doc: /* Return t if first arg is not equal to second arg. Both must be numbers or markers. */)
2514 (register Lisp_Object num1, Lisp_Object num2)
2516 return arithcompare (num1, num2, ARITH_NOTEQUAL);
2519 /* Convert the integer I to a cons-of-integers, where I is not in
2520 fixnum range. */
2522 #define INTBIG_TO_LISP(i, extremum) \
2523 (eassert (FIXNUM_OVERFLOW_P (i)), \
2524 (! (FIXNUM_OVERFLOW_P ((extremum) >> 16) \
2525 && FIXNUM_OVERFLOW_P ((i) >> 16)) \
2526 ? Fcons (make_number ((i) >> 16), make_number ((i) & 0xffff)) \
2527 : ! (FIXNUM_OVERFLOW_P ((extremum) >> 16 >> 24) \
2528 && FIXNUM_OVERFLOW_P ((i) >> 16 >> 24)) \
2529 ? Fcons (make_number ((i) >> 16 >> 24), \
2530 Fcons (make_number ((i) >> 16 & 0xffffff), \
2531 make_number ((i) & 0xffff))) \
2532 : make_float (i)))
2534 Lisp_Object
2535 intbig_to_lisp (intmax_t i)
2537 return INTBIG_TO_LISP (i, INTMAX_MIN);
2540 Lisp_Object
2541 uintbig_to_lisp (uintmax_t i)
2543 return INTBIG_TO_LISP (i, UINTMAX_MAX);
2546 /* Convert the cons-of-integers, integer, or float value C to an
2547 unsigned value with maximum value MAX. Signal an error if C does not
2548 have a valid format or is out of range. */
2549 uintmax_t
2550 cons_to_unsigned (Lisp_Object c, uintmax_t max)
2552 bool valid = 0;
2553 uintmax_t val;
2554 if (INTEGERP (c))
2556 valid = 0 <= XINT (c);
2557 val = XINT (c);
2559 else if (FLOATP (c))
2561 double d = XFLOAT_DATA (c);
2562 if (0 <= d
2563 && d < (max == UINTMAX_MAX ? (double) UINTMAX_MAX + 1 : max + 1))
2565 val = d;
2566 valid = 1;
2569 else if (CONSP (c) && NATNUMP (XCAR (c)))
2571 uintmax_t top = XFASTINT (XCAR (c));
2572 Lisp_Object rest = XCDR (c);
2573 if (top <= UINTMAX_MAX >> 24 >> 16
2574 && CONSP (rest)
2575 && NATNUMP (XCAR (rest)) && XFASTINT (XCAR (rest)) < 1 << 24
2576 && NATNUMP (XCDR (rest)) && XFASTINT (XCDR (rest)) < 1 << 16)
2578 uintmax_t mid = XFASTINT (XCAR (rest));
2579 val = top << 24 << 16 | mid << 16 | XFASTINT (XCDR (rest));
2580 valid = 1;
2582 else if (top <= UINTMAX_MAX >> 16)
2584 if (CONSP (rest))
2585 rest = XCAR (rest);
2586 if (NATNUMP (rest) && XFASTINT (rest) < 1 << 16)
2588 val = top << 16 | XFASTINT (rest);
2589 valid = 1;
2594 if (! (valid && val <= max))
2595 error ("Not an in-range integer, float, or cons of integers");
2596 return val;
2599 /* Convert the cons-of-integers, integer, or float value C to a signed
2600 value with extrema MIN and MAX. Signal an error if C does not have
2601 a valid format or is out of range. */
2602 intmax_t
2603 cons_to_signed (Lisp_Object c, intmax_t min, intmax_t max)
2605 bool valid = 0;
2606 intmax_t val;
2607 if (INTEGERP (c))
2609 val = XINT (c);
2610 valid = 1;
2612 else if (FLOATP (c))
2614 double d = XFLOAT_DATA (c);
2615 if (min <= d
2616 && d < (max == INTMAX_MAX ? (double) INTMAX_MAX + 1 : max + 1))
2618 val = d;
2619 valid = 1;
2622 else if (CONSP (c) && INTEGERP (XCAR (c)))
2624 intmax_t top = XINT (XCAR (c));
2625 Lisp_Object rest = XCDR (c);
2626 if (INTMAX_MIN >> 24 >> 16 <= top && top <= INTMAX_MAX >> 24 >> 16
2627 && CONSP (rest)
2628 && NATNUMP (XCAR (rest)) && XFASTINT (XCAR (rest)) < 1 << 24
2629 && NATNUMP (XCDR (rest)) && XFASTINT (XCDR (rest)) < 1 << 16)
2631 intmax_t mid = XFASTINT (XCAR (rest));
2632 val = top << 24 << 16 | mid << 16 | XFASTINT (XCDR (rest));
2633 valid = 1;
2635 else if (INTMAX_MIN >> 16 <= top && top <= INTMAX_MAX >> 16)
2637 if (CONSP (rest))
2638 rest = XCAR (rest);
2639 if (NATNUMP (rest) && XFASTINT (rest) < 1 << 16)
2641 val = top << 16 | XFASTINT (rest);
2642 valid = 1;
2647 if (! (valid && min <= val && val <= max))
2648 error ("Not an in-range integer, float, or cons of integers");
2649 return val;
2652 DEFUN ("number-to-string", Fnumber_to_string, Snumber_to_string, 1, 1, 0,
2653 doc: /* Return the decimal representation of NUMBER as a string.
2654 Uses a minus sign if negative.
2655 NUMBER may be an integer or a floating point number. */)
2656 (Lisp_Object number)
2658 char buffer[max (FLOAT_TO_STRING_BUFSIZE, INT_BUFSIZE_BOUND (EMACS_INT))];
2659 int len;
2661 CHECK_NUMBER_OR_FLOAT (number);
2663 if (FLOATP (number))
2664 len = float_to_string (buffer, XFLOAT_DATA (number));
2665 else
2666 len = sprintf (buffer, "%"pI"d", XINT (number));
2668 return make_unibyte_string (buffer, len);
2671 DEFUN ("string-to-number", Fstring_to_number, Sstring_to_number, 1, 2, 0,
2672 doc: /* Parse STRING as a decimal number and return the number.
2673 Ignore leading spaces and tabs, and all trailing chars. Return 0 if
2674 STRING cannot be parsed as an integer or floating point number.
2676 If BASE, interpret STRING as a number in that base. If BASE isn't
2677 present, base 10 is used. BASE must be between 2 and 16 (inclusive).
2678 If the base used is not 10, STRING is always parsed as an integer. */)
2679 (register Lisp_Object string, Lisp_Object base)
2681 register char *p;
2682 register int b;
2683 Lisp_Object val;
2685 CHECK_STRING (string);
2687 if (NILP (base))
2688 b = 10;
2689 else
2691 CHECK_NUMBER (base);
2692 if (! (2 <= XINT (base) && XINT (base) <= 16))
2693 xsignal1 (Qargs_out_of_range, base);
2694 b = XINT (base);
2697 p = SSDATA (string);
2698 while (*p == ' ' || *p == '\t')
2699 p++;
2701 val = string_to_number (p, b, 1);
2702 return NILP (val) ? make_number (0) : val;
2705 enum arithop
2707 Aadd,
2708 Asub,
2709 Amult,
2710 Adiv,
2711 Alogand,
2712 Alogior,
2713 Alogxor,
2714 Amax,
2715 Amin
2718 static Lisp_Object float_arith_driver (double, ptrdiff_t, enum arithop,
2719 ptrdiff_t, Lisp_Object *);
2720 static Lisp_Object
2721 arith_driver (enum arithop code, ptrdiff_t nargs, Lisp_Object *args)
2723 Lisp_Object val;
2724 ptrdiff_t argnum, ok_args;
2725 EMACS_INT accum = 0;
2726 EMACS_INT next, ok_accum;
2727 bool overflow = 0;
2729 switch (code)
2731 case Alogior:
2732 case Alogxor:
2733 case Aadd:
2734 case Asub:
2735 accum = 0;
2736 break;
2737 case Amult:
2738 case Adiv:
2739 accum = 1;
2740 break;
2741 case Alogand:
2742 accum = -1;
2743 break;
2744 default:
2745 break;
2748 for (argnum = 0; argnum < nargs; argnum++)
2750 if (! overflow)
2752 ok_args = argnum;
2753 ok_accum = accum;
2756 /* Using args[argnum] as argument to CHECK_NUMBER_... */
2757 val = args[argnum];
2758 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val);
2760 if (FLOATP (val))
2761 return float_arith_driver (ok_accum, ok_args, code,
2762 nargs, args);
2763 args[argnum] = val;
2764 next = XINT (args[argnum]);
2765 switch (code)
2767 case Aadd:
2768 overflow |= INT_ADD_WRAPV (accum, next, &accum);
2769 break;
2770 case Asub:
2771 if (! argnum)
2772 accum = nargs == 1 ? - next : next;
2773 else
2774 overflow |= INT_SUBTRACT_WRAPV (accum, next, &accum);
2775 break;
2776 case Amult:
2777 overflow |= INT_MULTIPLY_WRAPV (accum, next, &accum);
2778 break;
2779 case Adiv:
2780 if (! (argnum || nargs == 1))
2781 accum = next;
2782 else
2784 if (next == 0)
2785 xsignal0 (Qarith_error);
2786 if (INT_DIVIDE_OVERFLOW (accum, next))
2787 overflow = true;
2788 else
2789 accum /= next;
2791 break;
2792 case Alogand:
2793 accum &= next;
2794 break;
2795 case Alogior:
2796 accum |= next;
2797 break;
2798 case Alogxor:
2799 accum ^= next;
2800 break;
2801 case Amax:
2802 if (!argnum || next > accum)
2803 accum = next;
2804 break;
2805 case Amin:
2806 if (!argnum || next < accum)
2807 accum = next;
2808 break;
2812 XSETINT (val, accum);
2813 return val;
2816 #undef isnan
2817 #define isnan(x) ((x) != (x))
2819 static Lisp_Object
2820 float_arith_driver (double accum, ptrdiff_t argnum, enum arithop code,
2821 ptrdiff_t nargs, Lisp_Object *args)
2823 register Lisp_Object val;
2824 double next;
2826 for (; argnum < nargs; argnum++)
2828 val = args[argnum]; /* using args[argnum] as argument to CHECK_NUMBER_... */
2829 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val);
2831 if (FLOATP (val))
2833 next = XFLOAT_DATA (val);
2835 else
2837 args[argnum] = val; /* runs into a compiler bug. */
2838 next = XINT (args[argnum]);
2840 switch (code)
2842 case Aadd:
2843 accum += next;
2844 break;
2845 case Asub:
2846 accum = argnum ? accum - next : nargs == 1 ? - next : next;
2847 break;
2848 case Amult:
2849 accum *= next;
2850 break;
2851 case Adiv:
2852 if (! (argnum || nargs == 1))
2853 accum = next;
2854 else
2856 if (! IEEE_FLOATING_POINT && next == 0)
2857 xsignal0 (Qarith_error);
2858 accum /= next;
2860 break;
2861 case Alogand:
2862 case Alogior:
2863 case Alogxor:
2864 wrong_type_argument (Qinteger_or_marker_p, val);
2865 case Amax:
2866 if (!argnum || isnan (next) || next > accum)
2867 accum = next;
2868 break;
2869 case Amin:
2870 if (!argnum || isnan (next) || next < accum)
2871 accum = next;
2872 break;
2876 return make_float (accum);
2880 DEFUN ("+", Fplus, Splus, 0, MANY, 0,
2881 doc: /* Return sum of any number of arguments, which are numbers or markers.
2882 usage: (+ &rest NUMBERS-OR-MARKERS) */)
2883 (ptrdiff_t nargs, Lisp_Object *args)
2885 return arith_driver (Aadd, nargs, args);
2888 DEFUN ("-", Fminus, Sminus, 0, MANY, 0,
2889 doc: /* Negate number or subtract numbers or markers and return the result.
2890 With one arg, negates it. With more than one arg,
2891 subtracts all but the first from the first.
2892 usage: (- &optional NUMBER-OR-MARKER &rest MORE-NUMBERS-OR-MARKERS) */)
2893 (ptrdiff_t nargs, Lisp_Object *args)
2895 return arith_driver (Asub, nargs, args);
2898 DEFUN ("*", Ftimes, Stimes, 0, MANY, 0,
2899 doc: /* Return product of any number of arguments, which are numbers or markers.
2900 usage: (* &rest NUMBERS-OR-MARKERS) */)
2901 (ptrdiff_t nargs, Lisp_Object *args)
2903 return arith_driver (Amult, nargs, args);
2906 DEFUN ("/", Fquo, Squo, 1, MANY, 0,
2907 doc: /* Divide number by divisors and return the result.
2908 With two or more arguments, return first argument divided by the rest.
2909 With one argument, return 1 divided by the argument.
2910 The arguments must be numbers or markers.
2911 usage: (/ NUMBER &rest DIVISORS) */)
2912 (ptrdiff_t nargs, Lisp_Object *args)
2914 ptrdiff_t argnum;
2915 for (argnum = 2; argnum < nargs; argnum++)
2916 if (FLOATP (args[argnum]))
2917 return float_arith_driver (0, 0, Adiv, nargs, args);
2918 return arith_driver (Adiv, nargs, args);
2921 DEFUN ("%", Frem, Srem, 2, 2, 0,
2922 doc: /* Return remainder of X divided by Y.
2923 Both must be integers or markers. */)
2924 (register Lisp_Object x, Lisp_Object y)
2926 Lisp_Object val;
2928 CHECK_NUMBER_COERCE_MARKER (x);
2929 CHECK_NUMBER_COERCE_MARKER (y);
2931 if (XINT (y) == 0)
2932 xsignal0 (Qarith_error);
2934 XSETINT (val, XINT (x) % XINT (y));
2935 return val;
2938 DEFUN ("mod", Fmod, Smod, 2, 2, 0,
2939 doc: /* Return X modulo Y.
2940 The result falls between zero (inclusive) and Y (exclusive).
2941 Both X and Y must be numbers or markers. */)
2942 (register Lisp_Object x, Lisp_Object y)
2944 Lisp_Object val;
2945 EMACS_INT i1, i2;
2947 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (x);
2948 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (y);
2950 if (FLOATP (x) || FLOATP (y))
2951 return fmod_float (x, y);
2953 i1 = XINT (x);
2954 i2 = XINT (y);
2956 if (i2 == 0)
2957 xsignal0 (Qarith_error);
2959 i1 %= i2;
2961 /* If the "remainder" comes out with the wrong sign, fix it. */
2962 if (i2 < 0 ? i1 > 0 : i1 < 0)
2963 i1 += i2;
2965 XSETINT (val, i1);
2966 return val;
2969 DEFUN ("max", Fmax, Smax, 1, MANY, 0,
2970 doc: /* Return largest of all the arguments (which must be numbers or markers).
2971 The value is always a number; markers are converted to numbers.
2972 usage: (max NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2973 (ptrdiff_t nargs, Lisp_Object *args)
2975 return arith_driver (Amax, nargs, args);
2978 DEFUN ("min", Fmin, Smin, 1, MANY, 0,
2979 doc: /* Return smallest of all the arguments (which must be numbers or markers).
2980 The value is always a number; markers are converted to numbers.
2981 usage: (min NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2982 (ptrdiff_t nargs, Lisp_Object *args)
2984 return arith_driver (Amin, nargs, args);
2987 DEFUN ("logand", Flogand, Slogand, 0, MANY, 0,
2988 doc: /* Return bitwise-and of all the arguments.
2989 Arguments may be integers, or markers converted to integers.
2990 usage: (logand &rest INTS-OR-MARKERS) */)
2991 (ptrdiff_t nargs, Lisp_Object *args)
2993 return arith_driver (Alogand, nargs, args);
2996 DEFUN ("logior", Flogior, Slogior, 0, MANY, 0,
2997 doc: /* Return bitwise-or of all the arguments.
2998 Arguments may be integers, or markers converted to integers.
2999 usage: (logior &rest INTS-OR-MARKERS) */)
3000 (ptrdiff_t nargs, Lisp_Object *args)
3002 return arith_driver (Alogior, nargs, args);
3005 DEFUN ("logxor", Flogxor, Slogxor, 0, MANY, 0,
3006 doc: /* Return bitwise-exclusive-or of all the arguments.
3007 Arguments may be integers, or markers converted to integers.
3008 usage: (logxor &rest INTS-OR-MARKERS) */)
3009 (ptrdiff_t nargs, Lisp_Object *args)
3011 return arith_driver (Alogxor, nargs, args);
3014 static Lisp_Object
3015 ash_lsh_impl (register Lisp_Object value, Lisp_Object count, bool lsh)
3017 register Lisp_Object val;
3019 CHECK_NUMBER (value);
3020 CHECK_NUMBER (count);
3022 if (XINT (count) >= EMACS_INT_WIDTH)
3023 XSETINT (val, 0);
3024 else if (XINT (count) > 0)
3025 XSETINT (val, XUINT (value) << XFASTINT (count));
3026 else if (XINT (count) <= -EMACS_INT_WIDTH)
3027 XSETINT (val, lsh ? 0 : XINT (value) < 0 ? -1 : 0);
3028 else
3029 XSETINT (val, lsh ? XUINT (value) >> -XINT (count) : \
3030 XINT (value) >> -XINT (count));
3031 return val;
3034 DEFUN ("ash", Fash, Sash, 2, 2, 0,
3035 doc: /* Return VALUE with its bits shifted left by COUNT.
3036 If COUNT is negative, shifting is actually to the right.
3037 In this case, the sign bit is duplicated. */)
3038 (register Lisp_Object value, Lisp_Object count)
3040 return ash_lsh_impl (value, count, false);
3043 DEFUN ("lsh", Flsh, Slsh, 2, 2, 0,
3044 doc: /* Return VALUE with its bits shifted left by COUNT.
3045 If COUNT is negative, shifting is actually to the right.
3046 In this case, zeros are shifted in on the left. */)
3047 (register Lisp_Object value, Lisp_Object count)
3049 return ash_lsh_impl (value, count, true);
3052 DEFUN ("1+", Fadd1, Sadd1, 1, 1, 0,
3053 doc: /* Return NUMBER plus one. NUMBER may be a number or a marker.
3054 Markers are converted to integers. */)
3055 (register Lisp_Object number)
3057 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number);
3059 if (FLOATP (number))
3060 return (make_float (1.0 + XFLOAT_DATA (number)));
3062 XSETINT (number, XINT (number) + 1);
3063 return number;
3066 DEFUN ("1-", Fsub1, Ssub1, 1, 1, 0,
3067 doc: /* Return NUMBER minus one. NUMBER may be a number or a marker.
3068 Markers are converted to integers. */)
3069 (register Lisp_Object number)
3071 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number);
3073 if (FLOATP (number))
3074 return (make_float (-1.0 + XFLOAT_DATA (number)));
3076 XSETINT (number, XINT (number) - 1);
3077 return number;
3080 DEFUN ("lognot", Flognot, Slognot, 1, 1, 0,
3081 doc: /* Return the bitwise complement of NUMBER. NUMBER must be an integer. */)
3082 (register Lisp_Object number)
3084 CHECK_NUMBER (number);
3085 XSETINT (number, ~XINT (number));
3086 return number;
3089 DEFUN ("byteorder", Fbyteorder, Sbyteorder, 0, 0, 0,
3090 doc: /* Return the byteorder for the machine.
3091 Returns 66 (ASCII uppercase B) for big endian machines or 108 (ASCII
3092 lowercase l) for small endian machines. */
3093 attributes: const)
3094 (void)
3096 unsigned i = 0x04030201;
3097 int order = *(char *)&i == 1 ? 108 : 66;
3099 return make_number (order);
3102 /* Because we round up the bool vector allocate size to word_size
3103 units, we can safely read past the "end" of the vector in the
3104 operations below. These extra bits are always zero. */
3106 static bits_word
3107 bool_vector_spare_mask (EMACS_INT nr_bits)
3109 return (((bits_word) 1) << (nr_bits % BITS_PER_BITS_WORD)) - 1;
3112 /* Info about unsigned long long, falling back on unsigned long
3113 if unsigned long long is not available. */
3115 #if HAVE_UNSIGNED_LONG_LONG_INT && defined ULLONG_WIDTH
3116 enum { ULL_WIDTH = ULLONG_WIDTH };
3117 # define ULL_MAX ULLONG_MAX
3118 #else
3119 enum { ULL_WIDTH = ULONG_WIDTH };
3120 # define ULL_MAX ULONG_MAX
3121 # define count_one_bits_ll count_one_bits_l
3122 # define count_trailing_zeros_ll count_trailing_zeros_l
3123 #endif
3125 /* Shift VAL right by the width of an unsigned long long.
3126 ULL_WIDTH must be less than BITS_PER_BITS_WORD. */
3128 static bits_word
3129 shift_right_ull (bits_word w)
3131 /* Pacify bogus GCC warning about shift count exceeding type width. */
3132 int shift = ULL_WIDTH - BITS_PER_BITS_WORD < 0 ? ULL_WIDTH : 0;
3133 return w >> shift;
3136 /* Return the number of 1 bits in W. */
3138 static int
3139 count_one_bits_word (bits_word w)
3141 if (BITS_WORD_MAX <= UINT_MAX)
3142 return count_one_bits (w);
3143 else if (BITS_WORD_MAX <= ULONG_MAX)
3144 return count_one_bits_l (w);
3145 else
3147 int i = 0, count = 0;
3148 while (count += count_one_bits_ll (w),
3149 (i += ULL_WIDTH) < BITS_PER_BITS_WORD)
3150 w = shift_right_ull (w);
3151 return count;
3155 enum bool_vector_op { bool_vector_exclusive_or,
3156 bool_vector_union,
3157 bool_vector_intersection,
3158 bool_vector_set_difference,
3159 bool_vector_subsetp };
3161 static Lisp_Object
3162 bool_vector_binop_driver (Lisp_Object a,
3163 Lisp_Object b,
3164 Lisp_Object dest,
3165 enum bool_vector_op op)
3167 EMACS_INT nr_bits;
3168 bits_word *adata, *bdata, *destdata;
3169 ptrdiff_t i = 0;
3170 ptrdiff_t nr_words;
3172 CHECK_BOOL_VECTOR (a);
3173 CHECK_BOOL_VECTOR (b);
3175 nr_bits = bool_vector_size (a);
3176 if (bool_vector_size (b) != nr_bits)
3177 wrong_length_argument (a, b, dest);
3179 nr_words = bool_vector_words (nr_bits);
3180 adata = bool_vector_data (a);
3181 bdata = bool_vector_data (b);
3183 if (NILP (dest))
3185 dest = make_uninit_bool_vector (nr_bits);
3186 destdata = bool_vector_data (dest);
3188 else
3190 CHECK_BOOL_VECTOR (dest);
3191 destdata = bool_vector_data (dest);
3192 if (bool_vector_size (dest) != nr_bits)
3193 wrong_length_argument (a, b, dest);
3195 switch (op)
3197 case bool_vector_exclusive_or:
3198 for (; i < nr_words; i++)
3199 if (destdata[i] != (adata[i] ^ bdata[i]))
3200 goto set_dest;
3201 break;
3203 case bool_vector_subsetp:
3204 for (; i < nr_words; i++)
3205 if (adata[i] &~ bdata[i])
3206 return Qnil;
3207 return Qt;
3209 case bool_vector_union:
3210 for (; i < nr_words; i++)
3211 if (destdata[i] != (adata[i] | bdata[i]))
3212 goto set_dest;
3213 break;
3215 case bool_vector_intersection:
3216 for (; i < nr_words; i++)
3217 if (destdata[i] != (adata[i] & bdata[i]))
3218 goto set_dest;
3219 break;
3221 case bool_vector_set_difference:
3222 for (; i < nr_words; i++)
3223 if (destdata[i] != (adata[i] &~ bdata[i]))
3224 goto set_dest;
3225 break;
3228 return Qnil;
3231 set_dest:
3232 switch (op)
3234 case bool_vector_exclusive_or:
3235 for (; i < nr_words; i++)
3236 destdata[i] = adata[i] ^ bdata[i];
3237 break;
3239 case bool_vector_union:
3240 for (; i < nr_words; i++)
3241 destdata[i] = adata[i] | bdata[i];
3242 break;
3244 case bool_vector_intersection:
3245 for (; i < nr_words; i++)
3246 destdata[i] = adata[i] & bdata[i];
3247 break;
3249 case bool_vector_set_difference:
3250 for (; i < nr_words; i++)
3251 destdata[i] = adata[i] &~ bdata[i];
3252 break;
3254 default:
3255 eassume (0);
3258 return dest;
3261 /* PRECONDITION must be true. Return VALUE. This odd construction
3262 works around a bogus GCC diagnostic "shift count >= width of type". */
3264 static int
3265 pre_value (bool precondition, int value)
3267 eassume (precondition);
3268 return precondition ? value : 0;
3271 /* Compute the number of trailing zero bits in val. If val is zero,
3272 return the number of bits in val. */
3273 static int
3274 count_trailing_zero_bits (bits_word val)
3276 if (BITS_WORD_MAX == UINT_MAX)
3277 return count_trailing_zeros (val);
3278 if (BITS_WORD_MAX == ULONG_MAX)
3279 return count_trailing_zeros_l (val);
3280 if (BITS_WORD_MAX == ULL_MAX)
3281 return count_trailing_zeros_ll (val);
3283 /* The rest of this code is for the unlikely platform where bits_word differs
3284 in width from unsigned int, unsigned long, and unsigned long long. */
3285 val |= ~ BITS_WORD_MAX;
3286 if (BITS_WORD_MAX <= UINT_MAX)
3287 return count_trailing_zeros (val);
3288 if (BITS_WORD_MAX <= ULONG_MAX)
3289 return count_trailing_zeros_l (val);
3290 else
3292 int count;
3293 for (count = 0;
3294 count < BITS_PER_BITS_WORD - ULL_WIDTH;
3295 count += ULL_WIDTH)
3297 if (val & ULL_MAX)
3298 return count + count_trailing_zeros_ll (val);
3299 val = shift_right_ull (val);
3302 if (BITS_PER_BITS_WORD % ULL_WIDTH != 0
3303 && BITS_WORD_MAX == (bits_word) -1)
3304 val |= (bits_word) 1 << pre_value (ULONG_MAX < BITS_WORD_MAX,
3305 BITS_PER_BITS_WORD % ULL_WIDTH);
3306 return count + count_trailing_zeros_ll (val);
3310 static bits_word
3311 bits_word_to_host_endian (bits_word val)
3313 #ifndef WORDS_BIGENDIAN
3314 return val;
3315 #else
3316 if (BITS_WORD_MAX >> 31 == 1)
3317 return bswap_32 (val);
3318 # if HAVE_UNSIGNED_LONG_LONG
3319 if (BITS_WORD_MAX >> 31 >> 31 >> 1 == 1)
3320 return bswap_64 (val);
3321 # endif
3323 int i;
3324 bits_word r = 0;
3325 for (i = 0; i < sizeof val; i++)
3327 r = ((r << 1 << (CHAR_BIT - 1))
3328 | (val & ((1u << 1 << (CHAR_BIT - 1)) - 1)));
3329 val = val >> 1 >> (CHAR_BIT - 1);
3331 return r;
3333 #endif
3336 DEFUN ("bool-vector-exclusive-or", Fbool_vector_exclusive_or,
3337 Sbool_vector_exclusive_or, 2, 3, 0,
3338 doc: /* Return A ^ B, bitwise exclusive or.
3339 If optional third argument C is given, store result into C.
3340 A, B, and C must be bool vectors of the same length.
3341 Return the destination vector if it changed or nil otherwise. */)
3342 (Lisp_Object a, Lisp_Object b, Lisp_Object c)
3344 return bool_vector_binop_driver (a, b, c, bool_vector_exclusive_or);
3347 DEFUN ("bool-vector-union", Fbool_vector_union,
3348 Sbool_vector_union, 2, 3, 0,
3349 doc: /* Return A | B, bitwise or.
3350 If optional third argument C is given, store result into C.
3351 A, B, and C must be bool vectors of the same length.
3352 Return the destination vector if it changed or nil otherwise. */)
3353 (Lisp_Object a, Lisp_Object b, Lisp_Object c)
3355 return bool_vector_binop_driver (a, b, c, bool_vector_union);
3358 DEFUN ("bool-vector-intersection", Fbool_vector_intersection,
3359 Sbool_vector_intersection, 2, 3, 0,
3360 doc: /* Return A & B, bitwise and.
3361 If optional third argument C is given, store result into C.
3362 A, B, and C must be bool vectors of the same length.
3363 Return the destination vector if it changed or nil otherwise. */)
3364 (Lisp_Object a, Lisp_Object b, Lisp_Object c)
3366 return bool_vector_binop_driver (a, b, c, bool_vector_intersection);
3369 DEFUN ("bool-vector-set-difference", Fbool_vector_set_difference,
3370 Sbool_vector_set_difference, 2, 3, 0,
3371 doc: /* Return A &~ B, set difference.
3372 If optional third argument C is given, store result into C.
3373 A, B, and C must be bool vectors of the same length.
3374 Return the destination vector if it changed or nil otherwise. */)
3375 (Lisp_Object a, Lisp_Object b, Lisp_Object c)
3377 return bool_vector_binop_driver (a, b, c, bool_vector_set_difference);
3380 DEFUN ("bool-vector-subsetp", Fbool_vector_subsetp,
3381 Sbool_vector_subsetp, 2, 2, 0,
3382 doc: /* Return t if every t value in A is also t in B, nil otherwise.
3383 A and B must be bool vectors of the same length. */)
3384 (Lisp_Object a, Lisp_Object b)
3386 return bool_vector_binop_driver (a, b, b, bool_vector_subsetp);
3389 DEFUN ("bool-vector-not", Fbool_vector_not,
3390 Sbool_vector_not, 1, 2, 0,
3391 doc: /* Compute ~A, set complement.
3392 If optional second argument B is given, store result into B.
3393 A and B must be bool vectors of the same length.
3394 Return the destination vector. */)
3395 (Lisp_Object a, Lisp_Object b)
3397 EMACS_INT nr_bits;
3398 bits_word *bdata, *adata;
3399 ptrdiff_t i;
3401 CHECK_BOOL_VECTOR (a);
3402 nr_bits = bool_vector_size (a);
3404 if (NILP (b))
3405 b = make_uninit_bool_vector (nr_bits);
3406 else
3408 CHECK_BOOL_VECTOR (b);
3409 if (bool_vector_size (b) != nr_bits)
3410 wrong_length_argument (a, b, Qnil);
3413 bdata = bool_vector_data (b);
3414 adata = bool_vector_data (a);
3416 for (i = 0; i < nr_bits / BITS_PER_BITS_WORD; i++)
3417 bdata[i] = BITS_WORD_MAX & ~adata[i];
3419 if (nr_bits % BITS_PER_BITS_WORD)
3421 bits_word mword = bits_word_to_host_endian (adata[i]);
3422 mword = ~mword;
3423 mword &= bool_vector_spare_mask (nr_bits);
3424 bdata[i] = bits_word_to_host_endian (mword);
3427 return b;
3430 DEFUN ("bool-vector-count-population", Fbool_vector_count_population,
3431 Sbool_vector_count_population, 1, 1, 0,
3432 doc: /* Count how many elements in A are t.
3433 A is a bool vector. To count A's nil elements, subtract the return
3434 value from A's length. */)
3435 (Lisp_Object a)
3437 EMACS_INT count;
3438 EMACS_INT nr_bits;
3439 bits_word *adata;
3440 ptrdiff_t i, nwords;
3442 CHECK_BOOL_VECTOR (a);
3444 nr_bits = bool_vector_size (a);
3445 nwords = bool_vector_words (nr_bits);
3446 count = 0;
3447 adata = bool_vector_data (a);
3449 for (i = 0; i < nwords; i++)
3450 count += count_one_bits_word (adata[i]);
3452 return make_number (count);
3455 DEFUN ("bool-vector-count-consecutive", Fbool_vector_count_consecutive,
3456 Sbool_vector_count_consecutive, 3, 3, 0,
3457 doc: /* Count how many consecutive elements in A equal B starting at I.
3458 A is a bool vector, B is t or nil, and I is an index into A. */)
3459 (Lisp_Object a, Lisp_Object b, Lisp_Object i)
3461 EMACS_INT count;
3462 EMACS_INT nr_bits;
3463 int offset;
3464 bits_word *adata;
3465 bits_word twiddle;
3466 bits_word mword; /* Machine word. */
3467 ptrdiff_t pos, pos0;
3468 ptrdiff_t nr_words;
3470 CHECK_BOOL_VECTOR (a);
3471 CHECK_NATNUM (i);
3473 nr_bits = bool_vector_size (a);
3474 if (XFASTINT (i) > nr_bits) /* Allow one past the end for convenience */
3475 args_out_of_range (a, i);
3477 adata = bool_vector_data (a);
3478 nr_words = bool_vector_words (nr_bits);
3479 pos = XFASTINT (i) / BITS_PER_BITS_WORD;
3480 offset = XFASTINT (i) % BITS_PER_BITS_WORD;
3481 count = 0;
3483 /* By XORing with twiddle, we transform the problem of "count
3484 consecutive equal values" into "count the zero bits". The latter
3485 operation usually has hardware support. */
3486 twiddle = NILP (b) ? 0 : BITS_WORD_MAX;
3488 /* Scan the remainder of the mword at the current offset. */
3489 if (pos < nr_words && offset != 0)
3491 mword = bits_word_to_host_endian (adata[pos]);
3492 mword ^= twiddle;
3493 mword >>= offset;
3495 /* Do not count the pad bits. */
3496 mword |= (bits_word) 1 << (BITS_PER_BITS_WORD - offset);
3498 count = count_trailing_zero_bits (mword);
3499 pos++;
3500 if (count + offset < BITS_PER_BITS_WORD)
3501 return make_number (count);
3504 /* Scan whole words until we either reach the end of the vector or
3505 find an mword that doesn't completely match. twiddle is
3506 endian-independent. */
3507 pos0 = pos;
3508 while (pos < nr_words && adata[pos] == twiddle)
3509 pos++;
3510 count += (pos - pos0) * BITS_PER_BITS_WORD;
3512 if (pos < nr_words)
3514 /* If we stopped because of a mismatch, see how many bits match
3515 in the current mword. */
3516 mword = bits_word_to_host_endian (adata[pos]);
3517 mword ^= twiddle;
3518 count += count_trailing_zero_bits (mword);
3520 else if (nr_bits % BITS_PER_BITS_WORD != 0)
3522 /* If we hit the end, we might have overshot our count. Reduce
3523 the total by the number of spare bits at the end of the
3524 vector. */
3525 count -= BITS_PER_BITS_WORD - nr_bits % BITS_PER_BITS_WORD;
3528 return make_number (count);
3532 void
3533 syms_of_data (void)
3535 Lisp_Object error_tail, arith_tail;
3537 DEFSYM (Qquote, "quote");
3538 DEFSYM (Qlambda, "lambda");
3539 DEFSYM (Qsubr, "subr");
3540 DEFSYM (Qerror_conditions, "error-conditions");
3541 DEFSYM (Qerror_message, "error-message");
3542 DEFSYM (Qtop_level, "top-level");
3544 DEFSYM (Qerror, "error");
3545 DEFSYM (Quser_error, "user-error");
3546 DEFSYM (Qquit, "quit");
3547 DEFSYM (Qwrong_length_argument, "wrong-length-argument");
3548 DEFSYM (Qwrong_type_argument, "wrong-type-argument");
3549 DEFSYM (Qargs_out_of_range, "args-out-of-range");
3550 DEFSYM (Qvoid_function, "void-function");
3551 DEFSYM (Qcyclic_function_indirection, "cyclic-function-indirection");
3552 DEFSYM (Qcyclic_variable_indirection, "cyclic-variable-indirection");
3553 DEFSYM (Qvoid_variable, "void-variable");
3554 DEFSYM (Qsetting_constant, "setting-constant");
3555 DEFSYM (Qtrapping_constant, "trapping-constant");
3556 DEFSYM (Qinvalid_read_syntax, "invalid-read-syntax");
3558 DEFSYM (Qinvalid_function, "invalid-function");
3559 DEFSYM (Qwrong_number_of_arguments, "wrong-number-of-arguments");
3560 DEFSYM (Qno_catch, "no-catch");
3561 DEFSYM (Qend_of_file, "end-of-file");
3562 DEFSYM (Qarith_error, "arith-error");
3563 DEFSYM (Qbeginning_of_buffer, "beginning-of-buffer");
3564 DEFSYM (Qend_of_buffer, "end-of-buffer");
3565 DEFSYM (Qbuffer_read_only, "buffer-read-only");
3566 DEFSYM (Qtext_read_only, "text-read-only");
3567 DEFSYM (Qmark_inactive, "mark-inactive");
3569 DEFSYM (Qlistp, "listp");
3570 DEFSYM (Qconsp, "consp");
3571 DEFSYM (Qsymbolp, "symbolp");
3572 DEFSYM (Qintegerp, "integerp");
3573 DEFSYM (Qnatnump, "natnump");
3574 DEFSYM (Qwholenump, "wholenump");
3575 DEFSYM (Qstringp, "stringp");
3576 DEFSYM (Qarrayp, "arrayp");
3577 DEFSYM (Qsequencep, "sequencep");
3578 DEFSYM (Qbufferp, "bufferp");
3579 DEFSYM (Qvectorp, "vectorp");
3580 DEFSYM (Qbool_vector_p, "bool-vector-p");
3581 DEFSYM (Qchar_or_string_p, "char-or-string-p");
3582 DEFSYM (Qmarkerp, "markerp");
3583 #ifdef HAVE_MODULES
3584 DEFSYM (Quser_ptrp, "user-ptrp");
3585 #endif
3586 DEFSYM (Qbuffer_or_string_p, "buffer-or-string-p");
3587 DEFSYM (Qinteger_or_marker_p, "integer-or-marker-p");
3588 DEFSYM (Qfboundp, "fboundp");
3590 DEFSYM (Qfloatp, "floatp");
3591 DEFSYM (Qnumberp, "numberp");
3592 DEFSYM (Qnumber_or_marker_p, "number-or-marker-p");
3594 DEFSYM (Qchar_table_p, "char-table-p");
3595 DEFSYM (Qvector_or_char_table_p, "vector-or-char-table-p");
3597 DEFSYM (Qsubrp, "subrp");
3598 DEFSYM (Qunevalled, "unevalled");
3599 DEFSYM (Qmany, "many");
3601 DEFSYM (Qcdr, "cdr");
3603 error_tail = pure_cons (Qerror, Qnil);
3605 /* ERROR is used as a signaler for random errors for which nothing else is
3606 right. */
3608 Fput (Qerror, Qerror_conditions,
3609 error_tail);
3610 Fput (Qerror, Qerror_message,
3611 build_pure_c_string ("error"));
3613 #define PUT_ERROR(sym, tail, msg) \
3614 Fput (sym, Qerror_conditions, pure_cons (sym, tail)); \
3615 Fput (sym, Qerror_message, build_pure_c_string (msg))
3617 PUT_ERROR (Qquit, Qnil, "Quit");
3619 PUT_ERROR (Quser_error, error_tail, "");
3620 PUT_ERROR (Qwrong_length_argument, error_tail, "Wrong length argument");
3621 PUT_ERROR (Qwrong_type_argument, error_tail, "Wrong type argument");
3622 PUT_ERROR (Qargs_out_of_range, error_tail, "Args out of range");
3623 PUT_ERROR (Qvoid_function, error_tail,
3624 "Symbol's function definition is void");
3625 PUT_ERROR (Qcyclic_function_indirection, error_tail,
3626 "Symbol's chain of function indirections contains a loop");
3627 PUT_ERROR (Qcyclic_variable_indirection, error_tail,
3628 "Symbol's chain of variable indirections contains a loop");
3629 DEFSYM (Qcircular_list, "circular-list");
3630 PUT_ERROR (Qcircular_list, error_tail, "List contains a loop");
3631 PUT_ERROR (Qvoid_variable, error_tail, "Symbol's value as variable is void");
3632 PUT_ERROR (Qsetting_constant, error_tail,
3633 "Attempt to set a constant symbol");
3634 PUT_ERROR (Qtrapping_constant, error_tail,
3635 "Attempt to trap writes to a constant symbol");
3636 PUT_ERROR (Qinvalid_read_syntax, error_tail, "Invalid read syntax");
3637 PUT_ERROR (Qinvalid_function, error_tail, "Invalid function");
3638 PUT_ERROR (Qwrong_number_of_arguments, error_tail,
3639 "Wrong number of arguments");
3640 PUT_ERROR (Qno_catch, error_tail, "No catch for tag");
3641 PUT_ERROR (Qend_of_file, error_tail, "End of file during parsing");
3643 arith_tail = pure_cons (Qarith_error, error_tail);
3644 Fput (Qarith_error, Qerror_conditions, arith_tail);
3645 Fput (Qarith_error, Qerror_message, build_pure_c_string ("Arithmetic error"));
3647 PUT_ERROR (Qbeginning_of_buffer, error_tail, "Beginning of buffer");
3648 PUT_ERROR (Qend_of_buffer, error_tail, "End of buffer");
3649 PUT_ERROR (Qbuffer_read_only, error_tail, "Buffer is read-only");
3650 PUT_ERROR (Qtext_read_only, pure_cons (Qbuffer_read_only, error_tail),
3651 "Text is read-only");
3653 DEFSYM (Qrange_error, "range-error");
3654 DEFSYM (Qdomain_error, "domain-error");
3655 DEFSYM (Qsingularity_error, "singularity-error");
3656 DEFSYM (Qoverflow_error, "overflow-error");
3657 DEFSYM (Qunderflow_error, "underflow-error");
3659 PUT_ERROR (Qdomain_error, arith_tail, "Arithmetic domain error");
3661 PUT_ERROR (Qrange_error, arith_tail, "Arithmetic range error");
3663 PUT_ERROR (Qsingularity_error, Fcons (Qdomain_error, arith_tail),
3664 "Arithmetic singularity error");
3666 PUT_ERROR (Qoverflow_error, Fcons (Qdomain_error, arith_tail),
3667 "Arithmetic overflow error");
3668 PUT_ERROR (Qunderflow_error, Fcons (Qdomain_error, arith_tail),
3669 "Arithmetic underflow error");
3671 /* Types that type-of returns. */
3672 DEFSYM (Qinteger, "integer");
3673 DEFSYM (Qsymbol, "symbol");
3674 DEFSYM (Qstring, "string");
3675 DEFSYM (Qcons, "cons");
3676 DEFSYM (Qmarker, "marker");
3677 DEFSYM (Qoverlay, "overlay");
3678 DEFSYM (Qfinalizer, "finalizer");
3679 #ifdef HAVE_MODULES
3680 DEFSYM (Quser_ptr, "user-ptr");
3681 #endif
3682 DEFSYM (Qfloat, "float");
3683 DEFSYM (Qwindow_configuration, "window-configuration");
3684 DEFSYM (Qprocess, "process");
3685 DEFSYM (Qwindow, "window");
3686 DEFSYM (Qcompiled_function, "compiled-function");
3687 DEFSYM (Qbuffer, "buffer");
3688 DEFSYM (Qframe, "frame");
3689 DEFSYM (Qvector, "vector");
3690 DEFSYM (Qchar_table, "char-table");
3691 DEFSYM (Qbool_vector, "bool-vector");
3692 DEFSYM (Qhash_table, "hash-table");
3693 DEFSYM (Qthread, "thread");
3694 DEFSYM (Qmutex, "mutex");
3695 DEFSYM (Qcondition_variable, "condition-variable");
3697 DEFSYM (Qdefun, "defun");
3699 DEFSYM (Qfont_spec, "font-spec");
3700 DEFSYM (Qfont_entity, "font-entity");
3701 DEFSYM (Qfont_object, "font-object");
3703 DEFSYM (Qinteractive_form, "interactive-form");
3704 DEFSYM (Qdefalias_fset_function, "defalias-fset-function");
3706 defsubr (&Sindirect_variable);
3707 defsubr (&Sinteractive_form);
3708 defsubr (&Seq);
3709 defsubr (&Snull);
3710 defsubr (&Stype_of);
3711 defsubr (&Slistp);
3712 defsubr (&Snlistp);
3713 defsubr (&Sconsp);
3714 defsubr (&Satom);
3715 defsubr (&Sintegerp);
3716 defsubr (&Sinteger_or_marker_p);
3717 defsubr (&Snumberp);
3718 defsubr (&Snumber_or_marker_p);
3719 defsubr (&Sfloatp);
3720 defsubr (&Snatnump);
3721 defsubr (&Ssymbolp);
3722 defsubr (&Skeywordp);
3723 defsubr (&Sstringp);
3724 defsubr (&Smultibyte_string_p);
3725 defsubr (&Svectorp);
3726 defsubr (&Schar_table_p);
3727 defsubr (&Svector_or_char_table_p);
3728 defsubr (&Sbool_vector_p);
3729 defsubr (&Sarrayp);
3730 defsubr (&Ssequencep);
3731 defsubr (&Sbufferp);
3732 defsubr (&Smarkerp);
3733 defsubr (&Ssubrp);
3734 defsubr (&Sbyte_code_function_p);
3735 defsubr (&Schar_or_string_p);
3736 defsubr (&Sthreadp);
3737 defsubr (&Smutexp);
3738 defsubr (&Scondition_variable_p);
3739 defsubr (&Scar);
3740 defsubr (&Scdr);
3741 defsubr (&Scar_safe);
3742 defsubr (&Scdr_safe);
3743 defsubr (&Ssetcar);
3744 defsubr (&Ssetcdr);
3745 defsubr (&Ssymbol_function);
3746 defsubr (&Sindirect_function);
3747 defsubr (&Ssymbol_plist);
3748 defsubr (&Ssymbol_name);
3749 defsubr (&Smakunbound);
3750 defsubr (&Sfmakunbound);
3751 defsubr (&Sboundp);
3752 defsubr (&Sfboundp);
3753 defsubr (&Sfset);
3754 defsubr (&Sdefalias);
3755 defsubr (&Ssetplist);
3756 defsubr (&Ssymbol_value);
3757 defsubr (&Sset);
3758 defsubr (&Sdefault_boundp);
3759 defsubr (&Sdefault_value);
3760 defsubr (&Sset_default);
3761 defsubr (&Ssetq_default);
3762 defsubr (&Smake_variable_buffer_local);
3763 defsubr (&Smake_local_variable);
3764 defsubr (&Skill_local_variable);
3765 defsubr (&Slocal_variable_p);
3766 defsubr (&Slocal_variable_if_set_p);
3767 defsubr (&Svariable_binding_locus);
3768 #if 0 /* XXX Remove this. --lorentey */
3769 defsubr (&Sterminal_local_value);
3770 defsubr (&Sset_terminal_local_value);
3771 #endif
3772 defsubr (&Saref);
3773 defsubr (&Saset);
3774 defsubr (&Snumber_to_string);
3775 defsubr (&Sstring_to_number);
3776 defsubr (&Seqlsign);
3777 defsubr (&Slss);
3778 defsubr (&Sgtr);
3779 defsubr (&Sleq);
3780 defsubr (&Sgeq);
3781 defsubr (&Sneq);
3782 defsubr (&Splus);
3783 defsubr (&Sminus);
3784 defsubr (&Stimes);
3785 defsubr (&Squo);
3786 defsubr (&Srem);
3787 defsubr (&Smod);
3788 defsubr (&Smax);
3789 defsubr (&Smin);
3790 defsubr (&Slogand);
3791 defsubr (&Slogior);
3792 defsubr (&Slogxor);
3793 defsubr (&Slsh);
3794 defsubr (&Sash);
3795 defsubr (&Sadd1);
3796 defsubr (&Ssub1);
3797 defsubr (&Slognot);
3798 defsubr (&Sbyteorder);
3799 defsubr (&Ssubr_arity);
3800 defsubr (&Ssubr_name);
3801 #ifdef HAVE_MODULES
3802 defsubr (&Suser_ptrp);
3803 #endif
3805 defsubr (&Sbool_vector_exclusive_or);
3806 defsubr (&Sbool_vector_union);
3807 defsubr (&Sbool_vector_intersection);
3808 defsubr (&Sbool_vector_set_difference);
3809 defsubr (&Sbool_vector_not);
3810 defsubr (&Sbool_vector_subsetp);
3811 defsubr (&Sbool_vector_count_consecutive);
3812 defsubr (&Sbool_vector_count_population);
3814 set_symbol_function (Qwholenump, XSYMBOL (Qnatnump)->function);
3816 DEFVAR_LISP ("most-positive-fixnum", Vmost_positive_fixnum,
3817 doc: /* The largest value that is representable in a Lisp integer. */);
3818 Vmost_positive_fixnum = make_number (MOST_POSITIVE_FIXNUM);
3819 make_symbol_constant (intern_c_string ("most-positive-fixnum"));
3821 DEFVAR_LISP ("most-negative-fixnum", Vmost_negative_fixnum,
3822 doc: /* The smallest value that is representable in a Lisp integer. */);
3823 Vmost_negative_fixnum = make_number (MOST_NEGATIVE_FIXNUM);
3824 make_symbol_constant (intern_c_string ("most-negative-fixnum"));
3826 DEFSYM (Qwatchers, "watchers");
3827 DEFSYM (Qmakunbound, "makunbound");
3828 DEFSYM (Qunlet, "unlet");
3829 DEFSYM (Qset, "set");
3830 DEFSYM (Qset_default, "set-default");
3831 defsubr (&Sadd_variable_watcher);
3832 defsubr (&Sremove_variable_watcher);
3833 defsubr (&Sget_variable_watchers);