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