Support Ruby 2.3.0's safe navigation operator
[emacs.git] / src / data.c
blob2574cbbd7645d2f128eef567fa49dfcd37a713aa
1 /* Primitive operations on Lisp data types for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985-1986, 1988, 1993-1995, 1997-2016 Free Software
3 Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or (at
10 your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
22 #include <stdio.h>
24 #include <byteswap.h>
25 #include <count-one-bits.h>
26 #include <count-trailing-zeros.h>
27 #include <intprops.h>
29 #include "lisp.h"
30 #include "puresize.h"
31 #include "character.h"
32 #include "buffer.h"
33 #include "keyboard.h"
34 #include "frame.h"
35 #include "keymap.h"
37 static void swap_in_symval_forwarding (struct Lisp_Symbol *,
38 struct Lisp_Buffer_Local_Value *);
40 static bool
41 BOOLFWDP (union Lisp_Fwd *a)
43 return XFWDTYPE (a) == Lisp_Fwd_Bool;
45 static bool
46 INTFWDP (union Lisp_Fwd *a)
48 return XFWDTYPE (a) == Lisp_Fwd_Int;
50 static bool
51 KBOARD_OBJFWDP (union Lisp_Fwd *a)
53 return XFWDTYPE (a) == Lisp_Fwd_Kboard_Obj;
55 static bool
56 OBJFWDP (union Lisp_Fwd *a)
58 return XFWDTYPE (a) == Lisp_Fwd_Obj;
61 static struct Lisp_Boolfwd *
62 XBOOLFWD (union Lisp_Fwd *a)
64 eassert (BOOLFWDP (a));
65 return &a->u_boolfwd;
67 static struct Lisp_Kboard_Objfwd *
68 XKBOARD_OBJFWD (union Lisp_Fwd *a)
70 eassert (KBOARD_OBJFWDP (a));
71 return &a->u_kboard_objfwd;
73 static struct Lisp_Intfwd *
74 XINTFWD (union Lisp_Fwd *a)
76 eassert (INTFWDP (a));
77 return &a->u_intfwd;
79 static struct Lisp_Objfwd *
80 XOBJFWD (union Lisp_Fwd *a)
82 eassert (OBJFWDP (a));
83 return &a->u_objfwd;
86 static void
87 CHECK_SUBR (Lisp_Object x)
89 CHECK_TYPE (SUBRP (x), Qsubrp, x);
92 static void
93 set_blv_found (struct Lisp_Buffer_Local_Value *blv, int found)
95 eassert (found == !EQ (blv->defcell, blv->valcell));
96 blv->found = found;
99 static Lisp_Object
100 blv_value (struct Lisp_Buffer_Local_Value *blv)
102 return XCDR (blv->valcell);
105 static void
106 set_blv_value (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
108 XSETCDR (blv->valcell, val);
111 static void
112 set_blv_where (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
114 blv->where = val;
117 static void
118 set_blv_defcell (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
120 blv->defcell = val;
123 static void
124 set_blv_valcell (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
126 blv->valcell = val;
129 static _Noreturn void
130 wrong_length_argument (Lisp_Object a1, Lisp_Object a2, Lisp_Object a3)
132 Lisp_Object size1 = make_number (bool_vector_size (a1));
133 Lisp_Object size2 = make_number (bool_vector_size (a2));
134 if (NILP (a3))
135 xsignal2 (Qwrong_length_argument, size1, size2);
136 else
137 xsignal3 (Qwrong_length_argument, size1, size2,
138 make_number (bool_vector_size (a3)));
141 Lisp_Object
142 wrong_type_argument (register Lisp_Object predicate, register Lisp_Object value)
144 /* If VALUE is not even a valid Lisp object, we'd want to abort here
145 where we can get a backtrace showing where it came from. We used
146 to try and do that by checking the tagbits, but nowadays all
147 tagbits are potentially valid. */
148 /* if ((unsigned int) XTYPE (value) >= Lisp_Type_Limit)
149 * emacs_abort (); */
151 xsignal2 (Qwrong_type_argument, predicate, value);
154 void
155 pure_write_error (Lisp_Object obj)
157 xsignal2 (Qerror, build_string ("Attempt to modify read-only object"), obj);
160 void
161 args_out_of_range (Lisp_Object a1, Lisp_Object a2)
163 xsignal2 (Qargs_out_of_range, a1, a2);
166 void
167 args_out_of_range_3 (Lisp_Object a1, Lisp_Object a2, Lisp_Object a3)
169 xsignal3 (Qargs_out_of_range, a1, a2, a3);
173 /* Data type predicates. */
175 DEFUN ("eq", Feq, Seq, 2, 2, 0,
176 doc: /* Return t if the two args are the same Lisp object. */
177 attributes: const)
178 (Lisp_Object obj1, Lisp_Object obj2)
180 if (EQ (obj1, obj2))
181 return Qt;
182 return Qnil;
185 DEFUN ("null", Fnull, Snull, 1, 1, 0,
186 doc: /* Return t if OBJECT is nil, and return nil otherwise. */
187 attributes: const)
188 (Lisp_Object object)
190 if (NILP (object))
191 return Qt;
192 return Qnil;
195 DEFUN ("type-of", Ftype_of, Stype_of, 1, 1, 0,
196 doc: /* Return a symbol representing the type of OBJECT.
197 The symbol returned names the object's basic type;
198 for example, (type-of 1) returns `integer'. */)
199 (Lisp_Object object)
201 switch (XTYPE (object))
203 case_Lisp_Int:
204 return Qinteger;
206 case Lisp_Symbol:
207 return Qsymbol;
209 case Lisp_String:
210 return Qstring;
212 case Lisp_Cons:
213 return Qcons;
215 case Lisp_Misc:
216 switch (XMISCTYPE (object))
218 case Lisp_Misc_Marker:
219 return Qmarker;
220 case Lisp_Misc_Overlay:
221 return Qoverlay;
222 case Lisp_Misc_Float:
223 return Qfloat;
224 case Lisp_Misc_Finalizer:
225 return Qfinalizer;
226 #ifdef HAVE_MODULES
227 case Lisp_Misc_User_Ptr:
228 return Quser_ptr;
229 #endif
230 default:
231 emacs_abort ();
234 case Lisp_Vectorlike:
235 if (WINDOW_CONFIGURATIONP (object))
236 return Qwindow_configuration;
237 if (PROCESSP (object))
238 return Qprocess;
239 if (WINDOWP (object))
240 return Qwindow;
241 if (SUBRP (object))
242 return Qsubr;
243 if (COMPILEDP (object))
244 return Qcompiled_function;
245 if (BUFFERP (object))
246 return Qbuffer;
247 if (CHAR_TABLE_P (object))
248 return Qchar_table;
249 if (BOOL_VECTOR_P (object))
250 return Qbool_vector;
251 if (FRAMEP (object))
252 return Qframe;
253 if (HASH_TABLE_P (object))
254 return Qhash_table;
255 if (FONT_SPEC_P (object))
256 return Qfont_spec;
257 if (FONT_ENTITY_P (object))
258 return Qfont_entity;
259 if (FONT_OBJECT_P (object))
260 return Qfont_object;
261 return Qvector;
263 case Lisp_Float:
264 return Qfloat;
266 default:
267 emacs_abort ();
271 DEFUN ("consp", Fconsp, Sconsp, 1, 1, 0,
272 doc: /* Return t if OBJECT is a cons cell. */
273 attributes: const)
274 (Lisp_Object object)
276 if (CONSP (object))
277 return Qt;
278 return Qnil;
281 DEFUN ("atom", Fatom, Satom, 1, 1, 0,
282 doc: /* Return t if OBJECT is not a cons cell. This includes nil. */
283 attributes: const)
284 (Lisp_Object object)
286 if (CONSP (object))
287 return Qnil;
288 return Qt;
291 DEFUN ("listp", Flistp, Slistp, 1, 1, 0,
292 doc: /* Return t if OBJECT is a list, that is, a cons cell or nil.
293 Otherwise, return nil. */
294 attributes: const)
295 (Lisp_Object object)
297 if (CONSP (object) || NILP (object))
298 return Qt;
299 return Qnil;
302 DEFUN ("nlistp", Fnlistp, Snlistp, 1, 1, 0,
303 doc: /* Return t if OBJECT is not a list. Lists include nil. */
304 attributes: const)
305 (Lisp_Object object)
307 if (CONSP (object) || NILP (object))
308 return Qnil;
309 return Qt;
312 DEFUN ("symbolp", Fsymbolp, Ssymbolp, 1, 1, 0,
313 doc: /* Return t if OBJECT is a symbol. */
314 attributes: const)
315 (Lisp_Object object)
317 if (SYMBOLP (object))
318 return Qt;
319 return Qnil;
322 /* Define this in C to avoid unnecessarily consing up the symbol
323 name. */
324 DEFUN ("keywordp", Fkeywordp, Skeywordp, 1, 1, 0,
325 doc: /* Return t if OBJECT is a keyword.
326 This means that it is a symbol with a print name beginning with `:'
327 interned in the initial obarray. */)
328 (Lisp_Object object)
330 if (SYMBOLP (object)
331 && SREF (SYMBOL_NAME (object), 0) == ':'
332 && SYMBOL_INTERNED_IN_INITIAL_OBARRAY_P (object))
333 return Qt;
334 return Qnil;
337 DEFUN ("vectorp", Fvectorp, Svectorp, 1, 1, 0,
338 doc: /* Return t if OBJECT is a vector. */)
339 (Lisp_Object object)
341 if (VECTORP (object))
342 return Qt;
343 return Qnil;
346 DEFUN ("stringp", Fstringp, Sstringp, 1, 1, 0,
347 doc: /* Return t if OBJECT is a string. */
348 attributes: const)
349 (Lisp_Object object)
351 if (STRINGP (object))
352 return Qt;
353 return Qnil;
356 DEFUN ("multibyte-string-p", Fmultibyte_string_p, Smultibyte_string_p,
357 1, 1, 0,
358 doc: /* Return t if OBJECT is a multibyte string.
359 Return nil if OBJECT is either a unibyte string, or not a string. */)
360 (Lisp_Object object)
362 if (STRINGP (object) && STRING_MULTIBYTE (object))
363 return Qt;
364 return Qnil;
367 DEFUN ("char-table-p", Fchar_table_p, Schar_table_p, 1, 1, 0,
368 doc: /* Return t if OBJECT is a char-table. */)
369 (Lisp_Object object)
371 if (CHAR_TABLE_P (object))
372 return Qt;
373 return Qnil;
376 DEFUN ("vector-or-char-table-p", Fvector_or_char_table_p,
377 Svector_or_char_table_p, 1, 1, 0,
378 doc: /* Return t if OBJECT is a char-table or vector. */)
379 (Lisp_Object object)
381 if (VECTORP (object) || CHAR_TABLE_P (object))
382 return Qt;
383 return Qnil;
386 DEFUN ("bool-vector-p", Fbool_vector_p, Sbool_vector_p, 1, 1, 0,
387 doc: /* Return t if OBJECT is a bool-vector. */)
388 (Lisp_Object object)
390 if (BOOL_VECTOR_P (object))
391 return Qt;
392 return Qnil;
395 DEFUN ("arrayp", Farrayp, Sarrayp, 1, 1, 0,
396 doc: /* Return t if OBJECT is an array (string or vector). */)
397 (Lisp_Object object)
399 if (ARRAYP (object))
400 return Qt;
401 return Qnil;
404 DEFUN ("sequencep", Fsequencep, Ssequencep, 1, 1, 0,
405 doc: /* Return t if OBJECT is a sequence (list or array). */)
406 (register Lisp_Object object)
408 if (CONSP (object) || NILP (object) || ARRAYP (object))
409 return Qt;
410 return Qnil;
413 DEFUN ("bufferp", Fbufferp, Sbufferp, 1, 1, 0,
414 doc: /* Return t if OBJECT is an editor buffer. */)
415 (Lisp_Object object)
417 if (BUFFERP (object))
418 return Qt;
419 return Qnil;
422 DEFUN ("markerp", Fmarkerp, Smarkerp, 1, 1, 0,
423 doc: /* Return t if OBJECT is a marker (editor pointer). */)
424 (Lisp_Object object)
426 if (MARKERP (object))
427 return Qt;
428 return Qnil;
431 #ifdef HAVE_MODULES
432 DEFUN ("user-ptrp", Fuser_ptrp, Suser_ptrp, 1, 1, 0,
433 doc: /* Return t if OBJECT is a module user pointer. */)
434 (Lisp_Object object)
436 if (USER_PTRP (object))
437 return Qt;
438 return Qnil;
440 #endif
442 DEFUN ("subrp", Fsubrp, Ssubrp, 1, 1, 0,
443 doc: /* Return t if OBJECT is a built-in function. */)
444 (Lisp_Object object)
446 if (SUBRP (object))
447 return Qt;
448 return Qnil;
451 DEFUN ("byte-code-function-p", Fbyte_code_function_p, Sbyte_code_function_p,
452 1, 1, 0,
453 doc: /* Return t if OBJECT is a byte-compiled function object. */)
454 (Lisp_Object object)
456 if (COMPILEDP (object))
457 return Qt;
458 return Qnil;
461 DEFUN ("char-or-string-p", Fchar_or_string_p, Schar_or_string_p, 1, 1, 0,
462 doc: /* Return t if OBJECT is a character or a string. */
463 attributes: const)
464 (register Lisp_Object object)
466 if (CHARACTERP (object) || STRINGP (object))
467 return Qt;
468 return Qnil;
471 DEFUN ("integerp", Fintegerp, Sintegerp, 1, 1, 0,
472 doc: /* Return t if OBJECT is an integer. */
473 attributes: const)
474 (Lisp_Object object)
476 if (INTEGERP (object))
477 return Qt;
478 return Qnil;
481 DEFUN ("integer-or-marker-p", Finteger_or_marker_p, Sinteger_or_marker_p, 1, 1, 0,
482 doc: /* Return t if OBJECT is an integer or a marker (editor pointer). */)
483 (register Lisp_Object object)
485 if (MARKERP (object) || INTEGERP (object))
486 return Qt;
487 return Qnil;
490 DEFUN ("natnump", Fnatnump, Snatnump, 1, 1, 0,
491 doc: /* Return t if OBJECT is a nonnegative integer. */
492 attributes: const)
493 (Lisp_Object object)
495 if (NATNUMP (object))
496 return Qt;
497 return Qnil;
500 DEFUN ("numberp", Fnumberp, Snumberp, 1, 1, 0,
501 doc: /* Return t if OBJECT is a number (floating point or integer). */
502 attributes: const)
503 (Lisp_Object object)
505 if (NUMBERP (object))
506 return Qt;
507 else
508 return Qnil;
511 DEFUN ("number-or-marker-p", Fnumber_or_marker_p,
512 Snumber_or_marker_p, 1, 1, 0,
513 doc: /* Return t if OBJECT is a number or a marker. */)
514 (Lisp_Object object)
516 if (NUMBERP (object) || MARKERP (object))
517 return Qt;
518 return Qnil;
521 DEFUN ("floatp", Ffloatp, Sfloatp, 1, 1, 0,
522 doc: /* Return t if OBJECT is a floating point number. */
523 attributes: const)
524 (Lisp_Object object)
526 if (FLOATP (object))
527 return Qt;
528 return Qnil;
532 /* Extract and set components of lists. */
534 DEFUN ("car", Fcar, Scar, 1, 1, 0,
535 doc: /* Return the car of LIST. If arg is nil, return nil.
536 Error if arg is not nil and not a cons cell. See also `car-safe'.
538 See Info node `(elisp)Cons Cells' for a discussion of related basic
539 Lisp concepts such as car, cdr, cons cell and list. */)
540 (register Lisp_Object list)
542 return CAR (list);
545 DEFUN ("car-safe", Fcar_safe, Scar_safe, 1, 1, 0,
546 doc: /* Return the car of OBJECT if it is a cons cell, or else nil. */)
547 (Lisp_Object object)
549 return CAR_SAFE (object);
552 DEFUN ("cdr", Fcdr, Scdr, 1, 1, 0,
553 doc: /* Return the cdr of LIST. If arg is nil, return nil.
554 Error if arg is not nil and not a cons cell. See also `cdr-safe'.
556 See Info node `(elisp)Cons Cells' for a discussion of related basic
557 Lisp concepts such as cdr, car, cons cell and list. */)
558 (register Lisp_Object list)
560 return CDR (list);
563 DEFUN ("cdr-safe", Fcdr_safe, Scdr_safe, 1, 1, 0,
564 doc: /* Return the cdr of OBJECT if it is a cons cell, or else nil. */)
565 (Lisp_Object object)
567 return CDR_SAFE (object);
570 DEFUN ("setcar", Fsetcar, Ssetcar, 2, 2, 0,
571 doc: /* Set the car of CELL to be NEWCAR. Returns NEWCAR. */)
572 (register Lisp_Object cell, Lisp_Object newcar)
574 CHECK_CONS (cell);
575 CHECK_IMPURE (cell, XCONS (cell));
576 XSETCAR (cell, newcar);
577 return newcar;
580 DEFUN ("setcdr", Fsetcdr, Ssetcdr, 2, 2, 0,
581 doc: /* Set the cdr of CELL to be NEWCDR. Returns NEWCDR. */)
582 (register Lisp_Object cell, Lisp_Object newcdr)
584 CHECK_CONS (cell);
585 CHECK_IMPURE (cell, XCONS (cell));
586 XSETCDR (cell, newcdr);
587 return newcdr;
590 /* Extract and set components of symbols. */
592 DEFUN ("boundp", Fboundp, Sboundp, 1, 1, 0,
593 doc: /* Return t if SYMBOL's value is not void.
594 Note that if `lexical-binding' is in effect, this refers to the
595 global value outside of any lexical scope. */)
596 (register Lisp_Object symbol)
598 Lisp_Object valcontents;
599 struct Lisp_Symbol *sym;
600 CHECK_SYMBOL (symbol);
601 sym = XSYMBOL (symbol);
603 start:
604 switch (sym->redirect)
606 case SYMBOL_PLAINVAL: valcontents = SYMBOL_VAL (sym); break;
607 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
608 case SYMBOL_LOCALIZED:
610 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
611 if (blv->fwd)
612 /* In set_internal, we un-forward vars when their value is
613 set to Qunbound. */
614 return Qt;
615 else
617 swap_in_symval_forwarding (sym, blv);
618 valcontents = blv_value (blv);
620 break;
622 case SYMBOL_FORWARDED:
623 /* In set_internal, we un-forward vars when their value is
624 set to Qunbound. */
625 return Qt;
626 default: emacs_abort ();
629 return (EQ (valcontents, Qunbound) ? Qnil : Qt);
632 /* FIXME: Make it an alias for function-symbol! */
633 DEFUN ("fboundp", Ffboundp, Sfboundp, 1, 1, 0,
634 doc: /* Return t if SYMBOL's function definition is not void. */)
635 (register Lisp_Object symbol)
637 CHECK_SYMBOL (symbol);
638 return NILP (XSYMBOL (symbol)->function) ? Qnil : Qt;
641 DEFUN ("makunbound", Fmakunbound, Smakunbound, 1, 1, 0,
642 doc: /* Make SYMBOL's value be void.
643 Return SYMBOL. */)
644 (register Lisp_Object symbol)
646 CHECK_SYMBOL (symbol);
647 if (SYMBOL_CONSTANT_P (symbol))
648 xsignal1 (Qsetting_constant, symbol);
649 Fset (symbol, Qunbound);
650 return symbol;
653 DEFUN ("fmakunbound", Ffmakunbound, Sfmakunbound, 1, 1, 0,
654 doc: /* Make SYMBOL's function definition be nil.
655 Return SYMBOL. */)
656 (register Lisp_Object symbol)
658 CHECK_SYMBOL (symbol);
659 if (NILP (symbol) || EQ (symbol, Qt))
660 xsignal1 (Qsetting_constant, symbol);
661 set_symbol_function (symbol, Qnil);
662 return symbol;
665 DEFUN ("symbol-function", Fsymbol_function, Ssymbol_function, 1, 1, 0,
666 doc: /* Return SYMBOL's function definition, or nil if that is void. */)
667 (register Lisp_Object symbol)
669 CHECK_SYMBOL (symbol);
670 return XSYMBOL (symbol)->function;
673 DEFUN ("symbol-plist", Fsymbol_plist, Ssymbol_plist, 1, 1, 0,
674 doc: /* Return SYMBOL's property list. */)
675 (register Lisp_Object symbol)
677 CHECK_SYMBOL (symbol);
678 return XSYMBOL (symbol)->plist;
681 DEFUN ("symbol-name", Fsymbol_name, Ssymbol_name, 1, 1, 0,
682 doc: /* Return SYMBOL's name, a string. */)
683 (register Lisp_Object symbol)
685 register Lisp_Object name;
687 CHECK_SYMBOL (symbol);
688 name = SYMBOL_NAME (symbol);
689 return name;
692 DEFUN ("fset", Ffset, Sfset, 2, 2, 0,
693 doc: /* Set SYMBOL's function definition to DEFINITION, and return DEFINITION. */)
694 (register Lisp_Object symbol, Lisp_Object definition)
696 register Lisp_Object function;
697 CHECK_SYMBOL (symbol);
699 function = XSYMBOL (symbol)->function;
701 if (!NILP (Vautoload_queue) && !NILP (function))
702 Vautoload_queue = Fcons (Fcons (symbol, function), Vautoload_queue);
704 if (AUTOLOADP (function))
705 Fput (symbol, Qautoload, XCDR (function));
707 /* Convert to eassert or remove after GC bug is found. In the
708 meantime, check unconditionally, at a slight perf hit. */
709 if (! valid_lisp_object_p (definition))
710 emacs_abort ();
712 set_symbol_function (symbol, definition);
714 return definition;
717 DEFUN ("defalias", Fdefalias, Sdefalias, 2, 3, 0,
718 doc: /* Set SYMBOL's function definition to DEFINITION.
719 Associates the function with the current load file, if any.
720 The optional third argument DOCSTRING specifies the documentation string
721 for SYMBOL; if it is omitted or nil, SYMBOL uses the documentation string
722 determined by DEFINITION.
724 Internally, this normally uses `fset', but if SYMBOL has a
725 `defalias-fset-function' property, the associated value is used instead.
727 The return value is undefined. */)
728 (register Lisp_Object symbol, Lisp_Object definition, Lisp_Object docstring)
730 CHECK_SYMBOL (symbol);
731 if (!NILP (Vpurify_flag)
732 /* If `definition' is a keymap, immutable (and copying) is wrong. */
733 && !KEYMAPP (definition))
734 definition = Fpurecopy (definition);
737 bool autoload = AUTOLOADP (definition);
738 if (NILP (Vpurify_flag) || !autoload)
739 { /* Only add autoload entries after dumping, because the ones before are
740 not useful and else we get loads of them from the loaddefs.el. */
742 if (AUTOLOADP (XSYMBOL (symbol)->function))
743 /* Remember that the function was already an autoload. */
744 LOADHIST_ATTACH (Fcons (Qt, symbol));
745 LOADHIST_ATTACH (Fcons (autoload ? Qautoload : Qdefun, symbol));
749 { /* Handle automatic advice activation. */
750 Lisp_Object hook = Fget (symbol, Qdefalias_fset_function);
751 if (!NILP (hook))
752 call2 (hook, symbol, definition);
753 else
754 Ffset (symbol, definition);
757 if (!NILP (docstring))
758 Fput (symbol, Qfunction_documentation, docstring);
759 /* We used to return `definition', but now that `defun' and `defmacro' expand
760 to a call to `defalias', we return `symbol' for backward compatibility
761 (bug#11686). */
762 return symbol;
765 DEFUN ("setplist", Fsetplist, Ssetplist, 2, 2, 0,
766 doc: /* Set SYMBOL's property list to NEWPLIST, and return NEWPLIST. */)
767 (register Lisp_Object symbol, Lisp_Object newplist)
769 CHECK_SYMBOL (symbol);
770 set_symbol_plist (symbol, newplist);
771 return newplist;
774 DEFUN ("subr-arity", Fsubr_arity, Ssubr_arity, 1, 1, 0,
775 doc: /* Return minimum and maximum number of args allowed for SUBR.
776 SUBR must be a built-in function.
777 The returned value is a pair (MIN . MAX). MIN is the minimum number
778 of args. MAX is the maximum number or the symbol `many', for a
779 function with `&rest' args, or `unevalled' for a special form. */)
780 (Lisp_Object subr)
782 short minargs, maxargs;
783 CHECK_SUBR (subr);
784 minargs = XSUBR (subr)->min_args;
785 maxargs = XSUBR (subr)->max_args;
786 return Fcons (make_number (minargs),
787 maxargs == MANY ? Qmany
788 : maxargs == UNEVALLED ? Qunevalled
789 : make_number (maxargs));
792 DEFUN ("subr-name", Fsubr_name, Ssubr_name, 1, 1, 0,
793 doc: /* Return name of subroutine SUBR.
794 SUBR must be a built-in function. */)
795 (Lisp_Object subr)
797 const char *name;
798 CHECK_SUBR (subr);
799 name = XSUBR (subr)->symbol_name;
800 return build_string (name);
803 DEFUN ("interactive-form", Finteractive_form, Sinteractive_form, 1, 1, 0,
804 doc: /* Return the interactive form of CMD or nil if none.
805 If CMD is not a command, the return value is nil.
806 Value, if non-nil, is a list (interactive SPEC). */)
807 (Lisp_Object cmd)
809 Lisp_Object fun = indirect_function (cmd); /* Check cycles. */
811 if (NILP (fun))
812 return Qnil;
814 /* Use an `interactive-form' property if present, analogous to the
815 function-documentation property. */
816 fun = cmd;
817 while (SYMBOLP (fun))
819 Lisp_Object tmp = Fget (fun, Qinteractive_form);
820 if (!NILP (tmp))
821 return tmp;
822 else
823 fun = Fsymbol_function (fun);
826 if (SUBRP (fun))
828 const char *spec = XSUBR (fun)->intspec;
829 if (spec)
830 return list2 (Qinteractive,
831 (*spec != '(') ? build_string (spec) :
832 Fcar (Fread_from_string (build_string (spec), Qnil, Qnil)));
834 else if (COMPILEDP (fun))
836 if ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE)
837 return list2 (Qinteractive, AREF (fun, COMPILED_INTERACTIVE));
839 else if (AUTOLOADP (fun))
840 return Finteractive_form (Fautoload_do_load (fun, cmd, Qnil));
841 else if (CONSP (fun))
843 Lisp_Object funcar = XCAR (fun);
844 if (EQ (funcar, Qclosure))
845 return Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun))));
846 else if (EQ (funcar, Qlambda))
847 return Fassq (Qinteractive, Fcdr (XCDR (fun)));
849 return Qnil;
853 /***********************************************************************
854 Getting and Setting Values of Symbols
855 ***********************************************************************/
857 /* Return the symbol holding SYMBOL's value. Signal
858 `cyclic-variable-indirection' if SYMBOL's chain of variable
859 indirections contains a loop. */
861 struct Lisp_Symbol *
862 indirect_variable (struct Lisp_Symbol *symbol)
864 struct Lisp_Symbol *tortoise, *hare;
866 hare = tortoise = symbol;
868 while (hare->redirect == SYMBOL_VARALIAS)
870 hare = SYMBOL_ALIAS (hare);
871 if (hare->redirect != SYMBOL_VARALIAS)
872 break;
874 hare = SYMBOL_ALIAS (hare);
875 tortoise = SYMBOL_ALIAS (tortoise);
877 if (hare == tortoise)
879 Lisp_Object tem;
880 XSETSYMBOL (tem, symbol);
881 xsignal1 (Qcyclic_variable_indirection, tem);
885 return hare;
889 DEFUN ("indirect-variable", Findirect_variable, Sindirect_variable, 1, 1, 0,
890 doc: /* Return the variable at the end of OBJECT's variable chain.
891 If OBJECT is a symbol, follow its variable indirections (if any), and
892 return the variable at the end of the chain of aliases. See Info node
893 `(elisp)Variable Aliases'.
895 If OBJECT is not a symbol, just return it. If there is a loop in the
896 chain of aliases, signal a `cyclic-variable-indirection' error. */)
897 (Lisp_Object object)
899 if (SYMBOLP (object))
901 struct Lisp_Symbol *sym = indirect_variable (XSYMBOL (object));
902 XSETSYMBOL (object, sym);
904 return object;
908 /* Given the raw contents of a symbol value cell,
909 return the Lisp value of the symbol.
910 This does not handle buffer-local variables; use
911 swap_in_symval_forwarding for that. */
913 Lisp_Object
914 do_symval_forwarding (register union Lisp_Fwd *valcontents)
916 register Lisp_Object val;
917 switch (XFWDTYPE (valcontents))
919 case Lisp_Fwd_Int:
920 XSETINT (val, *XINTFWD (valcontents)->intvar);
921 return val;
923 case Lisp_Fwd_Bool:
924 return (*XBOOLFWD (valcontents)->boolvar ? Qt : Qnil);
926 case Lisp_Fwd_Obj:
927 return *XOBJFWD (valcontents)->objvar;
929 case Lisp_Fwd_Buffer_Obj:
930 return per_buffer_value (current_buffer,
931 XBUFFER_OBJFWD (valcontents)->offset);
933 case Lisp_Fwd_Kboard_Obj:
934 /* We used to simply use current_kboard here, but from Lisp
935 code, its value is often unexpected. It seems nicer to
936 allow constructions like this to work as intuitively expected:
938 (with-selected-frame frame
939 (define-key local-function-map "\eOP" [f1]))
941 On the other hand, this affects the semantics of
942 last-command and real-last-command, and people may rely on
943 that. I took a quick look at the Lisp codebase, and I
944 don't think anything will break. --lorentey */
945 return *(Lisp_Object *)(XKBOARD_OBJFWD (valcontents)->offset
946 + (char *)FRAME_KBOARD (SELECTED_FRAME ()));
947 default: emacs_abort ();
951 /* Used to signal a user-friendly error when symbol WRONG is
952 not a member of CHOICE, which should be a list of symbols. */
954 void
955 wrong_choice (Lisp_Object choice, Lisp_Object wrong)
957 ptrdiff_t i = 0, len = XINT (Flength (choice));
958 Lisp_Object obj, *args;
959 AUTO_STRING (one_of, "One of ");
960 AUTO_STRING (comma, ", ");
961 AUTO_STRING (or, " or ");
962 AUTO_STRING (should_be_specified, " should be specified");
964 USE_SAFE_ALLOCA;
965 SAFE_ALLOCA_LISP (args, len * 2 + 1);
967 args[i++] = one_of;
969 for (obj = choice; !NILP (obj); obj = XCDR (obj))
971 args[i++] = SYMBOL_NAME (XCAR (obj));
972 args[i++] = (NILP (XCDR (obj)) ? should_be_specified
973 : NILP (XCDR (XCDR (obj))) ? or : comma);
976 obj = Fconcat (i, args);
977 SAFE_FREE ();
978 xsignal2 (Qerror, obj, wrong);
981 /* Used to signal a user-friendly error if WRONG is not a number or
982 integer/floating-point number outsize of inclusive MIN..MAX range. */
984 static void
985 wrong_range (Lisp_Object min, Lisp_Object max, Lisp_Object wrong)
987 AUTO_STRING (value_should_be_from, "Value should be from ");
988 AUTO_STRING (to, " to ");
989 xsignal2 (Qerror,
990 CALLN (Fconcat, value_should_be_from, Fnumber_to_string (min),
991 to, Fnumber_to_string (max)),
992 wrong);
995 /* Store NEWVAL into SYMBOL, where VALCONTENTS is found in the value cell
996 of SYMBOL. If SYMBOL is buffer-local, VALCONTENTS should be the
997 buffer-independent contents of the value cell: forwarded just one
998 step past the buffer-localness.
1000 BUF non-zero means set the value in buffer BUF instead of the
1001 current buffer. This only plays a role for per-buffer variables. */
1003 static void
1004 store_symval_forwarding (union Lisp_Fwd *valcontents, register Lisp_Object newval, struct buffer *buf)
1006 switch (XFWDTYPE (valcontents))
1008 case Lisp_Fwd_Int:
1009 CHECK_NUMBER (newval);
1010 *XINTFWD (valcontents)->intvar = XINT (newval);
1011 break;
1013 case Lisp_Fwd_Bool:
1014 *XBOOLFWD (valcontents)->boolvar = !NILP (newval);
1015 break;
1017 case Lisp_Fwd_Obj:
1018 *XOBJFWD (valcontents)->objvar = newval;
1020 /* If this variable is a default for something stored
1021 in the buffer itself, such as default-fill-column,
1022 find the buffers that don't have local values for it
1023 and update them. */
1024 if (XOBJFWD (valcontents)->objvar > (Lisp_Object *) &buffer_defaults
1025 && XOBJFWD (valcontents)->objvar < (Lisp_Object *) (&buffer_defaults + 1))
1027 int offset = ((char *) XOBJFWD (valcontents)->objvar
1028 - (char *) &buffer_defaults);
1029 int idx = PER_BUFFER_IDX (offset);
1031 Lisp_Object tail, buf;
1033 if (idx <= 0)
1034 break;
1036 FOR_EACH_LIVE_BUFFER (tail, buf)
1038 struct buffer *b = XBUFFER (buf);
1040 if (! PER_BUFFER_VALUE_P (b, idx))
1041 set_per_buffer_value (b, offset, newval);
1044 break;
1046 case Lisp_Fwd_Buffer_Obj:
1048 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1049 Lisp_Object predicate = XBUFFER_OBJFWD (valcontents)->predicate;
1051 if (!NILP (newval))
1053 if (SYMBOLP (predicate))
1055 Lisp_Object prop;
1057 if ((prop = Fget (predicate, Qchoice), !NILP (prop)))
1059 if (NILP (Fmemq (newval, prop)))
1060 wrong_choice (prop, newval);
1062 else if ((prop = Fget (predicate, Qrange), !NILP (prop)))
1064 Lisp_Object min = XCAR (prop), max = XCDR (prop);
1066 if (!NUMBERP (newval)
1067 || !NILP (arithcompare (newval, min, ARITH_LESS))
1068 || !NILP (arithcompare (newval, max, ARITH_GRTR)))
1069 wrong_range (min, max, newval);
1071 else if (FUNCTIONP (predicate))
1073 if (NILP (call1 (predicate, newval)))
1074 wrong_type_argument (predicate, newval);
1078 if (buf == NULL)
1079 buf = current_buffer;
1080 set_per_buffer_value (buf, offset, newval);
1082 break;
1084 case Lisp_Fwd_Kboard_Obj:
1086 char *base = (char *) FRAME_KBOARD (SELECTED_FRAME ());
1087 char *p = base + XKBOARD_OBJFWD (valcontents)->offset;
1088 *(Lisp_Object *) p = newval;
1090 break;
1092 default:
1093 emacs_abort (); /* goto def; */
1097 /* Set up SYMBOL to refer to its global binding. This makes it safe
1098 to alter the status of other bindings. BEWARE: this may be called
1099 during the mark phase of GC, where we assume that Lisp_Object slots
1100 of BLV are marked after this function has changed them. */
1102 void
1103 swap_in_global_binding (struct Lisp_Symbol *symbol)
1105 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (symbol);
1107 /* Unload the previously loaded binding. */
1108 if (blv->fwd)
1109 set_blv_value (blv, do_symval_forwarding (blv->fwd));
1111 /* Select the global binding in the symbol. */
1112 set_blv_valcell (blv, blv->defcell);
1113 if (blv->fwd)
1114 store_symval_forwarding (blv->fwd, XCDR (blv->defcell), NULL);
1116 /* Indicate that the global binding is set up now. */
1117 set_blv_where (blv, Qnil);
1118 set_blv_found (blv, 0);
1121 /* Set up the buffer-local symbol SYMBOL for validity in the current buffer.
1122 VALCONTENTS is the contents of its value cell,
1123 which points to a struct Lisp_Buffer_Local_Value.
1125 Return the value forwarded one step past the buffer-local stage.
1126 This could be another forwarding pointer. */
1128 static void
1129 swap_in_symval_forwarding (struct Lisp_Symbol *symbol, struct Lisp_Buffer_Local_Value *blv)
1131 register Lisp_Object tem1;
1133 eassert (blv == SYMBOL_BLV (symbol));
1135 tem1 = blv->where;
1137 if (NILP (tem1)
1138 || (blv->frame_local
1139 ? !EQ (selected_frame, tem1)
1140 : current_buffer != XBUFFER (tem1)))
1143 /* Unload the previously loaded binding. */
1144 tem1 = blv->valcell;
1145 if (blv->fwd)
1146 set_blv_value (blv, do_symval_forwarding (blv->fwd));
1147 /* Choose the new binding. */
1149 Lisp_Object var;
1150 XSETSYMBOL (var, symbol);
1151 if (blv->frame_local)
1153 tem1 = assq_no_quit (var, XFRAME (selected_frame)->param_alist);
1154 set_blv_where (blv, selected_frame);
1156 else
1158 tem1 = assq_no_quit (var, BVAR (current_buffer, local_var_alist));
1159 set_blv_where (blv, Fcurrent_buffer ());
1162 if (!(blv->found = !NILP (tem1)))
1163 tem1 = blv->defcell;
1165 /* Load the new binding. */
1166 set_blv_valcell (blv, tem1);
1167 if (blv->fwd)
1168 store_symval_forwarding (blv->fwd, blv_value (blv), NULL);
1172 /* Find the value of a symbol, returning Qunbound if it's not bound.
1173 This is helpful for code which just wants to get a variable's value
1174 if it has one, without signaling an error.
1175 Note that it must not be possible to quit
1176 within this function. Great care is required for this. */
1178 Lisp_Object
1179 find_symbol_value (Lisp_Object symbol)
1181 struct Lisp_Symbol *sym;
1183 CHECK_SYMBOL (symbol);
1184 sym = XSYMBOL (symbol);
1186 start:
1187 switch (sym->redirect)
1189 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1190 case SYMBOL_PLAINVAL: return SYMBOL_VAL (sym);
1191 case SYMBOL_LOCALIZED:
1193 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1194 swap_in_symval_forwarding (sym, blv);
1195 return blv->fwd ? do_symval_forwarding (blv->fwd) : blv_value (blv);
1197 /* FALLTHROUGH */
1198 case SYMBOL_FORWARDED:
1199 return do_symval_forwarding (SYMBOL_FWD (sym));
1200 default: emacs_abort ();
1204 DEFUN ("symbol-value", Fsymbol_value, Ssymbol_value, 1, 1, 0,
1205 doc: /* Return SYMBOL's value. Error if that is void.
1206 Note that if `lexical-binding' is in effect, this returns the
1207 global value outside of any lexical scope. */)
1208 (Lisp_Object symbol)
1210 Lisp_Object val;
1212 val = find_symbol_value (symbol);
1213 if (!EQ (val, Qunbound))
1214 return val;
1216 xsignal1 (Qvoid_variable, symbol);
1219 DEFUN ("set", Fset, Sset, 2, 2, 0,
1220 doc: /* Set SYMBOL's value to NEWVAL, and return NEWVAL. */)
1221 (register Lisp_Object symbol, Lisp_Object newval)
1223 set_internal (symbol, newval, Qnil, 0);
1224 return newval;
1227 /* Store the value NEWVAL into SYMBOL.
1228 If buffer/frame-locality is an issue, WHERE specifies which context to use.
1229 (nil stands for the current buffer/frame).
1231 If BINDFLAG is false, then if this symbol is supposed to become
1232 local in every buffer where it is set, then we make it local.
1233 If BINDFLAG is true, we don't do that. */
1235 void
1236 set_internal (Lisp_Object symbol, Lisp_Object newval, Lisp_Object where,
1237 bool bindflag)
1239 bool voide = EQ (newval, Qunbound);
1240 struct Lisp_Symbol *sym;
1241 Lisp_Object tem1;
1243 /* If restoring in a dead buffer, do nothing. */
1244 /* if (BUFFERP (where) && NILP (XBUFFER (where)->name))
1245 return; */
1247 CHECK_SYMBOL (symbol);
1248 if (SYMBOL_CONSTANT_P (symbol))
1250 if (NILP (Fkeywordp (symbol))
1251 || !EQ (newval, Fsymbol_value (symbol)))
1252 xsignal1 (Qsetting_constant, symbol);
1253 else
1254 /* Allow setting keywords to their own value. */
1255 return;
1258 maybe_set_redisplay (symbol);
1259 sym = XSYMBOL (symbol);
1261 start:
1262 switch (sym->redirect)
1264 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1265 case SYMBOL_PLAINVAL: SET_SYMBOL_VAL (sym , newval); return;
1266 case SYMBOL_LOCALIZED:
1268 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1269 if (NILP (where))
1271 if (blv->frame_local)
1272 where = selected_frame;
1273 else
1274 XSETBUFFER (where, current_buffer);
1276 /* If the current buffer is not the buffer whose binding is
1277 loaded, or if there may be frame-local bindings and the frame
1278 isn't the right one, or if it's a Lisp_Buffer_Local_Value and
1279 the default binding is loaded, the loaded binding may be the
1280 wrong one. */
1281 if (!EQ (blv->where, where)
1282 /* Also unload a global binding (if the var is local_if_set). */
1283 || (EQ (blv->valcell, blv->defcell)))
1285 /* The currently loaded binding is not necessarily valid.
1286 We need to unload it, and choose a new binding. */
1288 /* Write out `realvalue' to the old loaded binding. */
1289 if (blv->fwd)
1290 set_blv_value (blv, do_symval_forwarding (blv->fwd));
1292 /* Find the new binding. */
1293 XSETSYMBOL (symbol, sym); /* May have changed via aliasing. */
1294 tem1 = assq_no_quit (symbol,
1295 (blv->frame_local
1296 ? XFRAME (where)->param_alist
1297 : BVAR (XBUFFER (where), local_var_alist)));
1298 set_blv_where (blv, where);
1299 blv->found = 1;
1301 if (NILP (tem1))
1303 /* This buffer still sees the default value. */
1305 /* If the variable is a Lisp_Some_Buffer_Local_Value,
1306 or if this is `let' rather than `set',
1307 make CURRENT-ALIST-ELEMENT point to itself,
1308 indicating that we're seeing the default value.
1309 Likewise if the variable has been let-bound
1310 in the current buffer. */
1311 if (bindflag || !blv->local_if_set
1312 || let_shadows_buffer_binding_p (sym))
1314 blv->found = 0;
1315 tem1 = blv->defcell;
1317 /* If it's a local_if_set, being set not bound,
1318 and we're not within a let that was made for this buffer,
1319 create a new buffer-local binding for the variable.
1320 That means, give this buffer a new assoc for a local value
1321 and load that binding. */
1322 else
1324 /* local_if_set is only supported for buffer-local
1325 bindings, not for frame-local bindings. */
1326 eassert (!blv->frame_local);
1327 tem1 = Fcons (symbol, XCDR (blv->defcell));
1328 bset_local_var_alist
1329 (XBUFFER (where),
1330 Fcons (tem1, BVAR (XBUFFER (where), local_var_alist)));
1334 /* Record which binding is now loaded. */
1335 set_blv_valcell (blv, tem1);
1338 /* Store the new value in the cons cell. */
1339 set_blv_value (blv, newval);
1341 if (blv->fwd)
1343 if (voide)
1344 /* If storing void (making the symbol void), forward only through
1345 buffer-local indicator, not through Lisp_Objfwd, etc. */
1346 blv->fwd = NULL;
1347 else
1348 store_symval_forwarding (blv->fwd, newval,
1349 BUFFERP (where)
1350 ? XBUFFER (where) : current_buffer);
1352 break;
1354 case SYMBOL_FORWARDED:
1356 struct buffer *buf
1357 = BUFFERP (where) ? XBUFFER (where) : current_buffer;
1358 union Lisp_Fwd *innercontents = SYMBOL_FWD (sym);
1359 if (BUFFER_OBJFWDP (innercontents))
1361 int offset = XBUFFER_OBJFWD (innercontents)->offset;
1362 int idx = PER_BUFFER_IDX (offset);
1363 if (idx > 0
1364 && !bindflag
1365 && !let_shadows_buffer_binding_p (sym))
1366 SET_PER_BUFFER_VALUE_P (buf, idx, 1);
1369 if (voide)
1370 { /* If storing void (making the symbol void), forward only through
1371 buffer-local indicator, not through Lisp_Objfwd, etc. */
1372 sym->redirect = SYMBOL_PLAINVAL;
1373 SET_SYMBOL_VAL (sym, newval);
1375 else
1376 store_symval_forwarding (/* sym, */ innercontents, newval, buf);
1377 break;
1379 default: emacs_abort ();
1381 return;
1384 /* Access or set a buffer-local symbol's default value. */
1386 /* Return the default value of SYMBOL, but don't check for voidness.
1387 Return Qunbound if it is void. */
1389 static Lisp_Object
1390 default_value (Lisp_Object symbol)
1392 struct Lisp_Symbol *sym;
1394 CHECK_SYMBOL (symbol);
1395 sym = XSYMBOL (symbol);
1397 start:
1398 switch (sym->redirect)
1400 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1401 case SYMBOL_PLAINVAL: return SYMBOL_VAL (sym);
1402 case SYMBOL_LOCALIZED:
1404 /* If var is set up for a buffer that lacks a local value for it,
1405 the current value is nominally the default value.
1406 But the `realvalue' slot may be more up to date, since
1407 ordinary setq stores just that slot. So use that. */
1408 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1409 if (blv->fwd && EQ (blv->valcell, blv->defcell))
1410 return do_symval_forwarding (blv->fwd);
1411 else
1412 return XCDR (blv->defcell);
1414 case SYMBOL_FORWARDED:
1416 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
1418 /* For a built-in buffer-local variable, get the default value
1419 rather than letting do_symval_forwarding get the current value. */
1420 if (BUFFER_OBJFWDP (valcontents))
1422 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1423 if (PER_BUFFER_IDX (offset) != 0)
1424 return per_buffer_default (offset);
1427 /* For other variables, get the current value. */
1428 return do_symval_forwarding (valcontents);
1430 default: emacs_abort ();
1434 DEFUN ("default-boundp", Fdefault_boundp, Sdefault_boundp, 1, 1, 0,
1435 doc: /* Return t if SYMBOL has a non-void default value.
1436 This is the value that is seen in buffers that do not have their own values
1437 for this variable. */)
1438 (Lisp_Object symbol)
1440 register Lisp_Object value;
1442 value = default_value (symbol);
1443 return (EQ (value, Qunbound) ? Qnil : Qt);
1446 DEFUN ("default-value", Fdefault_value, Sdefault_value, 1, 1, 0,
1447 doc: /* Return SYMBOL's default value.
1448 This is the value that is seen in buffers that do not have their own values
1449 for this variable. The default value is meaningful for variables with
1450 local bindings in certain buffers. */)
1451 (Lisp_Object symbol)
1453 Lisp_Object value = default_value (symbol);
1454 if (!EQ (value, Qunbound))
1455 return value;
1457 xsignal1 (Qvoid_variable, symbol);
1460 DEFUN ("set-default", Fset_default, Sset_default, 2, 2, 0,
1461 doc: /* Set SYMBOL's default value to VALUE. SYMBOL and VALUE are evaluated.
1462 The default value is seen in buffers that do not have their own values
1463 for this variable. */)
1464 (Lisp_Object symbol, Lisp_Object value)
1466 struct Lisp_Symbol *sym;
1468 CHECK_SYMBOL (symbol);
1469 if (SYMBOL_CONSTANT_P (symbol))
1471 if (NILP (Fkeywordp (symbol))
1472 || !EQ (value, Fdefault_value (symbol)))
1473 xsignal1 (Qsetting_constant, symbol);
1474 else
1475 /* Allow setting keywords to their own value. */
1476 return value;
1478 sym = XSYMBOL (symbol);
1480 start:
1481 switch (sym->redirect)
1483 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1484 case SYMBOL_PLAINVAL: return Fset (symbol, value);
1485 case SYMBOL_LOCALIZED:
1487 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1489 /* Store new value into the DEFAULT-VALUE slot. */
1490 XSETCDR (blv->defcell, value);
1492 /* If the default binding is now loaded, set the REALVALUE slot too. */
1493 if (blv->fwd && EQ (blv->defcell, blv->valcell))
1494 store_symval_forwarding (blv->fwd, value, NULL);
1495 return value;
1497 case SYMBOL_FORWARDED:
1499 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
1501 /* Handle variables like case-fold-search that have special slots
1502 in the buffer.
1503 Make them work apparently like Lisp_Buffer_Local_Value variables. */
1504 if (BUFFER_OBJFWDP (valcontents))
1506 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1507 int idx = PER_BUFFER_IDX (offset);
1509 set_per_buffer_default (offset, value);
1511 /* If this variable is not always local in all buffers,
1512 set it in the buffers that don't nominally have a local value. */
1513 if (idx > 0)
1515 struct buffer *b;
1517 FOR_EACH_BUFFER (b)
1518 if (!PER_BUFFER_VALUE_P (b, idx))
1519 set_per_buffer_value (b, offset, value);
1521 return value;
1523 else
1524 return Fset (symbol, value);
1526 default: emacs_abort ();
1530 DEFUN ("setq-default", Fsetq_default, Ssetq_default, 0, UNEVALLED, 0,
1531 doc: /* Set the default value of variable VAR to VALUE.
1532 VAR, the variable name, is literal (not evaluated);
1533 VALUE is an expression: it is evaluated and its value returned.
1534 The default value of a variable is seen in buffers
1535 that do not have their own values for the variable.
1537 More generally, you can use multiple variables and values, as in
1538 (setq-default VAR VALUE VAR VALUE...)
1539 This sets each VAR's default value to the corresponding VALUE.
1540 The VALUE for the Nth VAR can refer to the new default values
1541 of previous VARs.
1542 usage: (setq-default [VAR VALUE]...) */)
1543 (Lisp_Object args)
1545 Lisp_Object args_left, symbol, val;
1547 args_left = val = args;
1549 while (CONSP (args_left))
1551 val = eval_sub (Fcar (XCDR (args_left)));
1552 symbol = XCAR (args_left);
1553 Fset_default (symbol, val);
1554 args_left = Fcdr (XCDR (args_left));
1557 return val;
1560 /* Lisp functions for creating and removing buffer-local variables. */
1562 union Lisp_Val_Fwd
1564 Lisp_Object value;
1565 union Lisp_Fwd *fwd;
1568 static struct Lisp_Buffer_Local_Value *
1569 make_blv (struct Lisp_Symbol *sym, bool forwarded,
1570 union Lisp_Val_Fwd valcontents)
1572 struct Lisp_Buffer_Local_Value *blv = xmalloc (sizeof *blv);
1573 Lisp_Object symbol;
1574 Lisp_Object tem;
1576 XSETSYMBOL (symbol, sym);
1577 tem = Fcons (symbol, (forwarded
1578 ? do_symval_forwarding (valcontents.fwd)
1579 : valcontents.value));
1581 /* Buffer_Local_Values cannot have as realval a buffer-local
1582 or keyboard-local forwarding. */
1583 eassert (!(forwarded && BUFFER_OBJFWDP (valcontents.fwd)));
1584 eassert (!(forwarded && KBOARD_OBJFWDP (valcontents.fwd)));
1585 blv->fwd = forwarded ? valcontents.fwd : NULL;
1586 set_blv_where (blv, Qnil);
1587 blv->frame_local = 0;
1588 blv->local_if_set = 0;
1589 set_blv_defcell (blv, tem);
1590 set_blv_valcell (blv, tem);
1591 set_blv_found (blv, 0);
1592 return blv;
1595 DEFUN ("make-variable-buffer-local", Fmake_variable_buffer_local,
1596 Smake_variable_buffer_local, 1, 1, "vMake Variable Buffer Local: ",
1597 doc: /* Make VARIABLE become buffer-local whenever it is set.
1598 At any time, the value for the current buffer is in effect,
1599 unless the variable has never been set in this buffer,
1600 in which case the default value is in effect.
1601 Note that binding the variable with `let', or setting it while
1602 a `let'-style binding made in this buffer is in effect,
1603 does not make the variable buffer-local. Return VARIABLE.
1605 This globally affects all uses of this variable, so it belongs together with
1606 the variable declaration, rather than with its uses (if you just want to make
1607 a variable local to the current buffer for one particular use, use
1608 `make-local-variable'). Buffer-local bindings are normally cleared
1609 while setting up a new major mode, unless they have a `permanent-local'
1610 property.
1612 The function `default-value' gets the default value and `set-default' sets it. */)
1613 (register Lisp_Object variable)
1615 struct Lisp_Symbol *sym;
1616 struct Lisp_Buffer_Local_Value *blv = NULL;
1617 union Lisp_Val_Fwd valcontents IF_LINT (= {LISP_INITIALLY_ZERO});
1618 bool forwarded IF_LINT (= 0);
1620 CHECK_SYMBOL (variable);
1621 sym = XSYMBOL (variable);
1623 start:
1624 switch (sym->redirect)
1626 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1627 case SYMBOL_PLAINVAL:
1628 forwarded = 0; valcontents.value = SYMBOL_VAL (sym);
1629 if (EQ (valcontents.value, Qunbound))
1630 valcontents.value = Qnil;
1631 break;
1632 case SYMBOL_LOCALIZED:
1633 blv = SYMBOL_BLV (sym);
1634 if (blv->frame_local)
1635 error ("Symbol %s may not be buffer-local",
1636 SDATA (SYMBOL_NAME (variable)));
1637 break;
1638 case SYMBOL_FORWARDED:
1639 forwarded = 1; valcontents.fwd = SYMBOL_FWD (sym);
1640 if (KBOARD_OBJFWDP (valcontents.fwd))
1641 error ("Symbol %s may not be buffer-local",
1642 SDATA (SYMBOL_NAME (variable)));
1643 else if (BUFFER_OBJFWDP (valcontents.fwd))
1644 return variable;
1645 break;
1646 default: emacs_abort ();
1649 if (sym->constant)
1650 error ("Symbol %s may not be buffer-local", SDATA (SYMBOL_NAME (variable)));
1652 if (!blv)
1654 blv = make_blv (sym, forwarded, valcontents);
1655 sym->redirect = SYMBOL_LOCALIZED;
1656 SET_SYMBOL_BLV (sym, blv);
1658 Lisp_Object symbol;
1659 XSETSYMBOL (symbol, sym); /* In case `variable' is aliased. */
1660 if (let_shadows_global_binding_p (symbol))
1662 AUTO_STRING (format, "Making %s buffer-local while let-bound!");
1663 CALLN (Fmessage, format, SYMBOL_NAME (variable));
1668 blv->local_if_set = 1;
1669 return variable;
1672 DEFUN ("make-local-variable", Fmake_local_variable, Smake_local_variable,
1673 1, 1, "vMake Local Variable: ",
1674 doc: /* Make VARIABLE have a separate value in the current buffer.
1675 Other buffers will continue to share a common default value.
1676 \(The buffer-local value of VARIABLE starts out as the same value
1677 VARIABLE previously had. If VARIABLE was void, it remains void.)
1678 Return VARIABLE.
1680 If the variable is already arranged to become local when set,
1681 this function causes a local value to exist for this buffer,
1682 just as setting the variable would do.
1684 This function returns VARIABLE, and therefore
1685 (set (make-local-variable \\='VARIABLE) VALUE-EXP)
1686 works.
1688 See also `make-variable-buffer-local'.
1690 Do not use `make-local-variable' to make a hook variable buffer-local.
1691 Instead, use `add-hook' and specify t for the LOCAL argument. */)
1692 (Lisp_Object variable)
1694 Lisp_Object tem;
1695 bool forwarded IF_LINT (= 0);
1696 union Lisp_Val_Fwd valcontents IF_LINT (= {LISP_INITIALLY_ZERO});
1697 struct Lisp_Symbol *sym;
1698 struct Lisp_Buffer_Local_Value *blv = NULL;
1700 CHECK_SYMBOL (variable);
1701 sym = XSYMBOL (variable);
1703 start:
1704 switch (sym->redirect)
1706 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1707 case SYMBOL_PLAINVAL:
1708 forwarded = 0; valcontents.value = SYMBOL_VAL (sym); break;
1709 case SYMBOL_LOCALIZED:
1710 blv = SYMBOL_BLV (sym);
1711 if (blv->frame_local)
1712 error ("Symbol %s may not be buffer-local",
1713 SDATA (SYMBOL_NAME (variable)));
1714 break;
1715 case SYMBOL_FORWARDED:
1716 forwarded = 1; valcontents.fwd = SYMBOL_FWD (sym);
1717 if (KBOARD_OBJFWDP (valcontents.fwd))
1718 error ("Symbol %s may not be buffer-local",
1719 SDATA (SYMBOL_NAME (variable)));
1720 break;
1721 default: emacs_abort ();
1724 if (sym->constant)
1725 error ("Symbol %s may not be buffer-local",
1726 SDATA (SYMBOL_NAME (variable)));
1728 if (blv ? blv->local_if_set
1729 : (forwarded && BUFFER_OBJFWDP (valcontents.fwd)))
1731 tem = Fboundp (variable);
1732 /* Make sure the symbol has a local value in this particular buffer,
1733 by setting it to the same value it already has. */
1734 Fset (variable, (EQ (tem, Qt) ? Fsymbol_value (variable) : Qunbound));
1735 return variable;
1737 if (!blv)
1739 blv = make_blv (sym, forwarded, valcontents);
1740 sym->redirect = SYMBOL_LOCALIZED;
1741 SET_SYMBOL_BLV (sym, blv);
1743 Lisp_Object symbol;
1744 XSETSYMBOL (symbol, sym); /* In case `variable' is aliased. */
1745 if (let_shadows_global_binding_p (symbol))
1747 AUTO_STRING (format, "Making %s local to %s while let-bound!");
1748 CALLN (Fmessage, format, SYMBOL_NAME (variable),
1749 BVAR (current_buffer, name));
1754 /* Make sure this buffer has its own value of symbol. */
1755 XSETSYMBOL (variable, sym); /* Update in case of aliasing. */
1756 tem = Fassq (variable, BVAR (current_buffer, local_var_alist));
1757 if (NILP (tem))
1759 if (let_shadows_buffer_binding_p (sym))
1761 AUTO_STRING (format,
1762 "Making %s buffer-local while locally let-bound!");
1763 CALLN (Fmessage, format, SYMBOL_NAME (variable));
1766 /* Swap out any local binding for some other buffer, and make
1767 sure the current value is permanently recorded, if it's the
1768 default value. */
1769 find_symbol_value (variable);
1771 bset_local_var_alist
1772 (current_buffer,
1773 Fcons (Fcons (variable, XCDR (blv->defcell)),
1774 BVAR (current_buffer, local_var_alist)));
1776 /* Make sure symbol does not think it is set up for this buffer;
1777 force it to look once again for this buffer's value. */
1778 if (current_buffer == XBUFFER (blv->where))
1779 set_blv_where (blv, Qnil);
1780 set_blv_found (blv, 0);
1783 /* If the symbol forwards into a C variable, then load the binding
1784 for this buffer now. If C code modifies the variable before we
1785 load the binding in, then that new value will clobber the default
1786 binding the next time we unload it. */
1787 if (blv->fwd)
1788 swap_in_symval_forwarding (sym, blv);
1790 return variable;
1793 DEFUN ("kill-local-variable", Fkill_local_variable, Skill_local_variable,
1794 1, 1, "vKill Local Variable: ",
1795 doc: /* Make VARIABLE no longer have a separate value in the current buffer.
1796 From now on the default value will apply in this buffer. Return VARIABLE. */)
1797 (register Lisp_Object variable)
1799 register Lisp_Object tem;
1800 struct Lisp_Buffer_Local_Value *blv;
1801 struct Lisp_Symbol *sym;
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: return variable;
1811 case SYMBOL_FORWARDED:
1813 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
1814 if (BUFFER_OBJFWDP (valcontents))
1816 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1817 int idx = PER_BUFFER_IDX (offset);
1819 if (idx > 0)
1821 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 0);
1822 set_per_buffer_value (current_buffer, offset,
1823 per_buffer_default (offset));
1826 return variable;
1828 case SYMBOL_LOCALIZED:
1829 blv = SYMBOL_BLV (sym);
1830 if (blv->frame_local)
1831 return variable;
1832 break;
1833 default: emacs_abort ();
1836 /* Get rid of this buffer's alist element, if any. */
1837 XSETSYMBOL (variable, sym); /* Propagate variable indirection. */
1838 tem = Fassq (variable, BVAR (current_buffer, local_var_alist));
1839 if (!NILP (tem))
1840 bset_local_var_alist
1841 (current_buffer,
1842 Fdelq (tem, BVAR (current_buffer, local_var_alist)));
1844 /* If the symbol is set up with the current buffer's binding
1845 loaded, recompute its value. We have to do it now, or else
1846 forwarded objects won't work right. */
1848 Lisp_Object buf; XSETBUFFER (buf, current_buffer);
1849 if (EQ (buf, blv->where))
1851 set_blv_where (blv, Qnil);
1852 blv->found = 0;
1853 find_symbol_value (variable);
1857 return variable;
1860 /* Lisp functions for creating and removing buffer-local variables. */
1862 /* Obsolete since 22.2. NB adjust doc of modify-frame-parameters
1863 when/if this is removed. */
1865 DEFUN ("make-variable-frame-local", Fmake_variable_frame_local, Smake_variable_frame_local,
1866 1, 1, "vMake Variable Frame Local: ",
1867 doc: /* Enable VARIABLE to have frame-local bindings.
1868 This does not create any frame-local bindings for VARIABLE,
1869 it just makes them possible.
1871 A frame-local binding is actually a frame parameter value.
1872 If a frame F has a value for the frame parameter named VARIABLE,
1873 that also acts as a frame-local binding for VARIABLE in F--
1874 provided this function has been called to enable VARIABLE
1875 to have frame-local bindings at all.
1877 The only way to create a frame-local binding for VARIABLE in a frame
1878 is to set the VARIABLE frame parameter of that frame. See
1879 `modify-frame-parameters' for how to set frame parameters.
1881 Note that since Emacs 23.1, variables cannot be both buffer-local and
1882 frame-local any more (buffer-local bindings used to take precedence over
1883 frame-local bindings). */)
1884 (Lisp_Object variable)
1886 bool forwarded;
1887 union Lisp_Val_Fwd valcontents;
1888 struct Lisp_Symbol *sym;
1889 struct Lisp_Buffer_Local_Value *blv = NULL;
1891 CHECK_SYMBOL (variable);
1892 sym = XSYMBOL (variable);
1894 start:
1895 switch (sym->redirect)
1897 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1898 case SYMBOL_PLAINVAL:
1899 forwarded = 0; valcontents.value = SYMBOL_VAL (sym);
1900 if (EQ (valcontents.value, Qunbound))
1901 valcontents.value = Qnil;
1902 break;
1903 case SYMBOL_LOCALIZED:
1904 if (SYMBOL_BLV (sym)->frame_local)
1905 return variable;
1906 else
1907 error ("Symbol %s may not be frame-local",
1908 SDATA (SYMBOL_NAME (variable)));
1909 case SYMBOL_FORWARDED:
1910 forwarded = 1; valcontents.fwd = SYMBOL_FWD (sym);
1911 if (KBOARD_OBJFWDP (valcontents.fwd) || BUFFER_OBJFWDP (valcontents.fwd))
1912 error ("Symbol %s may not be frame-local",
1913 SDATA (SYMBOL_NAME (variable)));
1914 break;
1915 default: emacs_abort ();
1918 if (sym->constant)
1919 error ("Symbol %s may not be frame-local", SDATA (SYMBOL_NAME (variable)));
1921 blv = make_blv (sym, forwarded, valcontents);
1922 blv->frame_local = 1;
1923 sym->redirect = SYMBOL_LOCALIZED;
1924 SET_SYMBOL_BLV (sym, blv);
1926 Lisp_Object symbol;
1927 XSETSYMBOL (symbol, sym); /* In case `variable' is aliased. */
1928 if (let_shadows_global_binding_p (symbol))
1930 AUTO_STRING (format, "Making %s frame-local while let-bound!");
1931 CALLN (Fmessage, format, SYMBOL_NAME (variable));
1934 return variable;
1937 DEFUN ("local-variable-p", Flocal_variable_p, Slocal_variable_p,
1938 1, 2, 0,
1939 doc: /* Non-nil if VARIABLE has a local binding in buffer BUFFER.
1940 BUFFER defaults to the current buffer. */)
1941 (Lisp_Object variable, Lisp_Object buffer)
1943 struct buffer *buf = decode_buffer (buffer);
1944 struct Lisp_Symbol *sym;
1946 CHECK_SYMBOL (variable);
1947 sym = XSYMBOL (variable);
1949 start:
1950 switch (sym->redirect)
1952 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1953 case SYMBOL_PLAINVAL: return Qnil;
1954 case SYMBOL_LOCALIZED:
1956 Lisp_Object tail, elt, tmp;
1957 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1958 XSETBUFFER (tmp, buf);
1959 XSETSYMBOL (variable, sym); /* Update in case of aliasing. */
1961 if (EQ (blv->where, tmp)) /* The binding is already loaded. */
1962 return blv_found (blv) ? Qt : Qnil;
1963 else
1964 for (tail = BVAR (buf, local_var_alist); CONSP (tail); tail = XCDR (tail))
1966 elt = XCAR (tail);
1967 if (EQ (variable, XCAR (elt)))
1969 eassert (!blv->frame_local);
1970 return Qt;
1973 return Qnil;
1975 case SYMBOL_FORWARDED:
1977 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
1978 if (BUFFER_OBJFWDP (valcontents))
1980 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1981 int idx = PER_BUFFER_IDX (offset);
1982 if (idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
1983 return Qt;
1985 return Qnil;
1987 default: emacs_abort ();
1991 DEFUN ("local-variable-if-set-p", Flocal_variable_if_set_p, Slocal_variable_if_set_p,
1992 1, 2, 0,
1993 doc: /* Non-nil if VARIABLE is local in buffer BUFFER when set there.
1994 BUFFER defaults to the current buffer.
1996 More precisely, return non-nil if either VARIABLE already has a local
1997 value in BUFFER, or if VARIABLE is automatically buffer-local (see
1998 `make-variable-buffer-local'). */)
1999 (register Lisp_Object variable, Lisp_Object buffer)
2001 struct Lisp_Symbol *sym;
2003 CHECK_SYMBOL (variable);
2004 sym = XSYMBOL (variable);
2006 start:
2007 switch (sym->redirect)
2009 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
2010 case SYMBOL_PLAINVAL: return Qnil;
2011 case SYMBOL_LOCALIZED:
2013 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
2014 if (blv->local_if_set)
2015 return Qt;
2016 XSETSYMBOL (variable, sym); /* Update in case of aliasing. */
2017 return Flocal_variable_p (variable, buffer);
2019 case SYMBOL_FORWARDED:
2020 /* All BUFFER_OBJFWD slots become local if they are set. */
2021 return (BUFFER_OBJFWDP (SYMBOL_FWD (sym)) ? Qt : Qnil);
2022 default: emacs_abort ();
2026 DEFUN ("variable-binding-locus", Fvariable_binding_locus, Svariable_binding_locus,
2027 1, 1, 0,
2028 doc: /* Return a value indicating where VARIABLE's current binding comes from.
2029 If the current binding is buffer-local, the value is the current buffer.
2030 If the current binding is frame-local, the value is the selected frame.
2031 If the current binding is global (the default), the value is nil. */)
2032 (register Lisp_Object variable)
2034 struct Lisp_Symbol *sym;
2036 CHECK_SYMBOL (variable);
2037 sym = XSYMBOL (variable);
2039 /* Make sure the current binding is actually swapped in. */
2040 find_symbol_value (variable);
2042 start:
2043 switch (sym->redirect)
2045 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
2046 case SYMBOL_PLAINVAL: return Qnil;
2047 case SYMBOL_FORWARDED:
2049 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
2050 if (KBOARD_OBJFWDP (valcontents))
2051 return Fframe_terminal (selected_frame);
2052 else if (!BUFFER_OBJFWDP (valcontents))
2053 return Qnil;
2055 /* FALLTHROUGH */
2056 case SYMBOL_LOCALIZED:
2057 /* For a local variable, record both the symbol and which
2058 buffer's or frame's value we are saving. */
2059 if (!NILP (Flocal_variable_p (variable, Qnil)))
2060 return Fcurrent_buffer ();
2061 else if (sym->redirect == SYMBOL_LOCALIZED
2062 && blv_found (SYMBOL_BLV (sym)))
2063 return SYMBOL_BLV (sym)->where;
2064 else
2065 return Qnil;
2066 default: emacs_abort ();
2070 /* This code is disabled now that we use the selected frame to return
2071 keyboard-local-values. */
2072 #if 0
2073 extern struct terminal *get_terminal (Lisp_Object display, int);
2075 DEFUN ("terminal-local-value", Fterminal_local_value,
2076 Sterminal_local_value, 2, 2, 0,
2077 doc: /* Return the terminal-local value of SYMBOL on TERMINAL.
2078 If SYMBOL is not a terminal-local variable, then return its normal
2079 value, like `symbol-value'.
2081 TERMINAL may be a terminal object, a frame, or nil (meaning the
2082 selected frame's terminal device). */)
2083 (Lisp_Object symbol, Lisp_Object terminal)
2085 Lisp_Object result;
2086 struct terminal *t = get_terminal (terminal, 1);
2087 push_kboard (t->kboard);
2088 result = Fsymbol_value (symbol);
2089 pop_kboard ();
2090 return result;
2093 DEFUN ("set-terminal-local-value", Fset_terminal_local_value,
2094 Sset_terminal_local_value, 3, 3, 0,
2095 doc: /* Set the terminal-local binding of SYMBOL on TERMINAL to VALUE.
2096 If VARIABLE is not a terminal-local variable, then set its normal
2097 binding, like `set'.
2099 TERMINAL may be a terminal object, a frame, or nil (meaning the
2100 selected frame's terminal device). */)
2101 (Lisp_Object symbol, Lisp_Object terminal, Lisp_Object value)
2103 Lisp_Object result;
2104 struct terminal *t = get_terminal (terminal, 1);
2105 push_kboard (d->kboard);
2106 result = Fset (symbol, value);
2107 pop_kboard ();
2108 return result;
2110 #endif
2112 /* Find the function at the end of a chain of symbol function indirections. */
2114 /* If OBJECT is a symbol, find the end of its function chain and
2115 return the value found there. If OBJECT is not a symbol, just
2116 return it. If there is a cycle in the function chain, signal a
2117 cyclic-function-indirection error.
2119 This is like Findirect_function, except that it doesn't signal an
2120 error if the chain ends up unbound. */
2121 Lisp_Object
2122 indirect_function (register Lisp_Object object)
2124 Lisp_Object tortoise, hare;
2126 hare = tortoise = object;
2128 for (;;)
2130 if (!SYMBOLP (hare) || NILP (hare))
2131 break;
2132 hare = XSYMBOL (hare)->function;
2133 if (!SYMBOLP (hare) || NILP (hare))
2134 break;
2135 hare = XSYMBOL (hare)->function;
2137 tortoise = XSYMBOL (tortoise)->function;
2139 if (EQ (hare, tortoise))
2140 xsignal1 (Qcyclic_function_indirection, object);
2143 return hare;
2146 DEFUN ("indirect-function", Findirect_function, Sindirect_function, 1, 2, 0,
2147 doc: /* Return the function at the end of OBJECT's function chain.
2148 If OBJECT is not a symbol, just return it. Otherwise, follow all
2149 function indirections to find the final function binding and return it.
2150 Signal a cyclic-function-indirection error if there is a loop in the
2151 function chain of symbols. */)
2152 (register Lisp_Object object, Lisp_Object noerror)
2154 Lisp_Object result;
2156 /* Optimize for no indirection. */
2157 result = object;
2158 if (SYMBOLP (result) && !NILP (result)
2159 && (result = XSYMBOL (result)->function, SYMBOLP (result)))
2160 result = indirect_function (result);
2161 if (!NILP (result))
2162 return result;
2164 return Qnil;
2167 /* Extract and set vector and string elements. */
2169 DEFUN ("aref", Faref, Saref, 2, 2, 0,
2170 doc: /* Return the element of ARRAY at index IDX.
2171 ARRAY may be a vector, a string, a char-table, a bool-vector,
2172 or a byte-code object. IDX starts at 0. */)
2173 (register Lisp_Object array, Lisp_Object idx)
2175 register EMACS_INT idxval;
2177 CHECK_NUMBER (idx);
2178 idxval = XINT (idx);
2179 if (STRINGP (array))
2181 int c;
2182 ptrdiff_t idxval_byte;
2184 if (idxval < 0 || idxval >= SCHARS (array))
2185 args_out_of_range (array, idx);
2186 if (! STRING_MULTIBYTE (array))
2187 return make_number ((unsigned char) SREF (array, idxval));
2188 idxval_byte = string_char_to_byte (array, idxval);
2190 c = STRING_CHAR (SDATA (array) + idxval_byte);
2191 return make_number (c);
2193 else if (BOOL_VECTOR_P (array))
2195 if (idxval < 0 || idxval >= bool_vector_size (array))
2196 args_out_of_range (array, idx);
2197 return bool_vector_ref (array, idxval);
2199 else if (CHAR_TABLE_P (array))
2201 CHECK_CHARACTER (idx);
2202 return CHAR_TABLE_REF (array, idxval);
2204 else
2206 ptrdiff_t size = 0;
2207 if (VECTORP (array))
2208 size = ASIZE (array);
2209 else if (COMPILEDP (array))
2210 size = ASIZE (array) & PSEUDOVECTOR_SIZE_MASK;
2211 else
2212 wrong_type_argument (Qarrayp, array);
2214 if (idxval < 0 || idxval >= size)
2215 args_out_of_range (array, idx);
2216 return AREF (array, idxval);
2220 DEFUN ("aset", Faset, Saset, 3, 3, 0,
2221 doc: /* Store into the element of ARRAY at index IDX the value NEWELT.
2222 Return NEWELT. ARRAY may be a vector, a string, a char-table or a
2223 bool-vector. IDX starts at 0. */)
2224 (register Lisp_Object array, Lisp_Object idx, Lisp_Object newelt)
2226 register EMACS_INT idxval;
2228 CHECK_NUMBER (idx);
2229 idxval = XINT (idx);
2230 CHECK_ARRAY (array, Qarrayp);
2232 if (VECTORP (array))
2234 CHECK_IMPURE (array, XVECTOR (array));
2235 if (idxval < 0 || idxval >= ASIZE (array))
2236 args_out_of_range (array, idx);
2237 ASET (array, idxval, newelt);
2239 else if (BOOL_VECTOR_P (array))
2241 if (idxval < 0 || idxval >= bool_vector_size (array))
2242 args_out_of_range (array, idx);
2243 bool_vector_set (array, idxval, !NILP (newelt));
2245 else if (CHAR_TABLE_P (array))
2247 CHECK_CHARACTER (idx);
2248 CHAR_TABLE_SET (array, idxval, newelt);
2250 else
2252 int c;
2254 CHECK_IMPURE (array, XSTRING (array));
2255 if (idxval < 0 || idxval >= SCHARS (array))
2256 args_out_of_range (array, idx);
2257 CHECK_CHARACTER (newelt);
2258 c = XFASTINT (newelt);
2260 if (STRING_MULTIBYTE (array))
2262 ptrdiff_t idxval_byte, nbytes;
2263 int prev_bytes, new_bytes;
2264 unsigned char workbuf[MAX_MULTIBYTE_LENGTH], *p0 = workbuf, *p1;
2266 nbytes = SBYTES (array);
2267 idxval_byte = string_char_to_byte (array, idxval);
2268 p1 = SDATA (array) + idxval_byte;
2269 prev_bytes = BYTES_BY_CHAR_HEAD (*p1);
2270 new_bytes = CHAR_STRING (c, p0);
2271 if (prev_bytes != new_bytes)
2273 /* We must relocate the string data. */
2274 ptrdiff_t nchars = SCHARS (array);
2275 USE_SAFE_ALLOCA;
2276 unsigned char *str = SAFE_ALLOCA (nbytes);
2278 memcpy (str, SDATA (array), nbytes);
2279 allocate_string_data (XSTRING (array), nchars,
2280 nbytes + new_bytes - prev_bytes);
2281 memcpy (SDATA (array), str, idxval_byte);
2282 p1 = SDATA (array) + idxval_byte;
2283 memcpy (p1 + new_bytes, str + idxval_byte + prev_bytes,
2284 nbytes - (idxval_byte + prev_bytes));
2285 SAFE_FREE ();
2286 clear_string_char_byte_cache ();
2288 while (new_bytes--)
2289 *p1++ = *p0++;
2291 else
2293 if (! SINGLE_BYTE_CHAR_P (c))
2295 ptrdiff_t i;
2297 for (i = SBYTES (array) - 1; i >= 0; i--)
2298 if (SREF (array, i) >= 0x80)
2299 args_out_of_range (array, newelt);
2300 /* ARRAY is an ASCII string. Convert it to a multibyte
2301 string, and try `aset' again. */
2302 STRING_SET_MULTIBYTE (array);
2303 return Faset (array, idx, newelt);
2305 SSET (array, idxval, c);
2309 return newelt;
2312 /* Arithmetic functions */
2314 Lisp_Object
2315 arithcompare (Lisp_Object num1, Lisp_Object num2, enum Arith_Comparison comparison)
2317 double f1 = 0, f2 = 0;
2318 bool floatp = 0;
2320 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num1);
2321 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num2);
2323 if (FLOATP (num1) || FLOATP (num2))
2325 floatp = 1;
2326 f1 = (FLOATP (num1)) ? XFLOAT_DATA (num1) : XINT (num1);
2327 f2 = (FLOATP (num2)) ? XFLOAT_DATA (num2) : XINT (num2);
2330 switch (comparison)
2332 case ARITH_EQUAL:
2333 if (floatp ? f1 == f2 : XINT (num1) == XINT (num2))
2334 return Qt;
2335 return Qnil;
2337 case ARITH_NOTEQUAL:
2338 if (floatp ? f1 != f2 : XINT (num1) != XINT (num2))
2339 return Qt;
2340 return Qnil;
2342 case ARITH_LESS:
2343 if (floatp ? f1 < f2 : XINT (num1) < XINT (num2))
2344 return Qt;
2345 return Qnil;
2347 case ARITH_LESS_OR_EQUAL:
2348 if (floatp ? f1 <= f2 : XINT (num1) <= XINT (num2))
2349 return Qt;
2350 return Qnil;
2352 case ARITH_GRTR:
2353 if (floatp ? f1 > f2 : XINT (num1) > XINT (num2))
2354 return Qt;
2355 return Qnil;
2357 case ARITH_GRTR_OR_EQUAL:
2358 if (floatp ? f1 >= f2 : XINT (num1) >= XINT (num2))
2359 return Qt;
2360 return Qnil;
2362 default:
2363 emacs_abort ();
2367 static Lisp_Object
2368 arithcompare_driver (ptrdiff_t nargs, Lisp_Object *args,
2369 enum Arith_Comparison comparison)
2371 ptrdiff_t argnum;
2372 for (argnum = 1; argnum < nargs; ++argnum)
2374 if (EQ (Qnil, arithcompare (args[argnum - 1], args[argnum], comparison)))
2375 return Qnil;
2377 return Qt;
2380 DEFUN ("=", Feqlsign, Seqlsign, 1, MANY, 0,
2381 doc: /* Return t if args, all numbers or markers, are equal.
2382 usage: (= NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2383 (ptrdiff_t nargs, Lisp_Object *args)
2385 return arithcompare_driver (nargs, args, ARITH_EQUAL);
2388 DEFUN ("<", Flss, Slss, 1, MANY, 0,
2389 doc: /* Return t if each arg (a number or marker), is less than the next arg.
2390 usage: (< NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2391 (ptrdiff_t nargs, Lisp_Object *args)
2393 return arithcompare_driver (nargs, args, ARITH_LESS);
2396 DEFUN (">", Fgtr, Sgtr, 1, MANY, 0,
2397 doc: /* Return t if each arg (a number or marker) is greater than the next arg.
2398 usage: (> NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2399 (ptrdiff_t nargs, Lisp_Object *args)
2401 return arithcompare_driver (nargs, args, ARITH_GRTR);
2404 DEFUN ("<=", Fleq, Sleq, 1, MANY, 0,
2405 doc: /* Return t if each arg (a number or marker) is less than or equal to the next.
2406 usage: (<= NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2407 (ptrdiff_t nargs, Lisp_Object *args)
2409 return arithcompare_driver (nargs, args, ARITH_LESS_OR_EQUAL);
2412 DEFUN (">=", Fgeq, Sgeq, 1, MANY, 0,
2413 doc: /* Return t if each arg (a number or marker) is greater than or equal to the next.
2414 usage: (>= NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2415 (ptrdiff_t nargs, Lisp_Object *args)
2417 return arithcompare_driver (nargs, args, ARITH_GRTR_OR_EQUAL);
2420 DEFUN ("/=", Fneq, Sneq, 2, 2, 0,
2421 doc: /* Return t if first arg is not equal to second arg. Both must be numbers or markers. */)
2422 (register Lisp_Object num1, Lisp_Object num2)
2424 return arithcompare (num1, num2, ARITH_NOTEQUAL);
2427 /* Convert the integer I to a cons-of-integers, where I is not in
2428 fixnum range. */
2430 #define INTBIG_TO_LISP(i, extremum) \
2431 (eassert (FIXNUM_OVERFLOW_P (i)), \
2432 (! (FIXNUM_OVERFLOW_P ((extremum) >> 16) \
2433 && FIXNUM_OVERFLOW_P ((i) >> 16)) \
2434 ? Fcons (make_number ((i) >> 16), make_number ((i) & 0xffff)) \
2435 : ! (FIXNUM_OVERFLOW_P ((extremum) >> 16 >> 24) \
2436 && FIXNUM_OVERFLOW_P ((i) >> 16 >> 24)) \
2437 ? Fcons (make_number ((i) >> 16 >> 24), \
2438 Fcons (make_number ((i) >> 16 & 0xffffff), \
2439 make_number ((i) & 0xffff))) \
2440 : make_float (i)))
2442 Lisp_Object
2443 intbig_to_lisp (intmax_t i)
2445 return INTBIG_TO_LISP (i, INTMAX_MIN);
2448 Lisp_Object
2449 uintbig_to_lisp (uintmax_t i)
2451 return INTBIG_TO_LISP (i, UINTMAX_MAX);
2454 /* Convert the cons-of-integers, integer, or float value C to an
2455 unsigned value with maximum value MAX. Signal an error if C does not
2456 have a valid format or is out of range. */
2457 uintmax_t
2458 cons_to_unsigned (Lisp_Object c, uintmax_t max)
2460 bool valid = 0;
2461 uintmax_t val IF_LINT (= 0);
2462 if (INTEGERP (c))
2464 valid = 0 <= XINT (c);
2465 val = XINT (c);
2467 else if (FLOATP (c))
2469 double d = XFLOAT_DATA (c);
2470 if (0 <= d
2471 && d < (max == UINTMAX_MAX ? (double) UINTMAX_MAX + 1 : max + 1))
2473 val = d;
2474 valid = 1;
2477 else if (CONSP (c) && NATNUMP (XCAR (c)))
2479 uintmax_t top = XFASTINT (XCAR (c));
2480 Lisp_Object rest = XCDR (c);
2481 if (top <= UINTMAX_MAX >> 24 >> 16
2482 && CONSP (rest)
2483 && NATNUMP (XCAR (rest)) && XFASTINT (XCAR (rest)) < 1 << 24
2484 && NATNUMP (XCDR (rest)) && XFASTINT (XCDR (rest)) < 1 << 16)
2486 uintmax_t mid = XFASTINT (XCAR (rest));
2487 val = top << 24 << 16 | mid << 16 | XFASTINT (XCDR (rest));
2488 valid = 1;
2490 else if (top <= UINTMAX_MAX >> 16)
2492 if (CONSP (rest))
2493 rest = XCAR (rest);
2494 if (NATNUMP (rest) && XFASTINT (rest) < 1 << 16)
2496 val = top << 16 | XFASTINT (rest);
2497 valid = 1;
2502 if (! (valid && val <= max))
2503 error ("Not an in-range integer, float, or cons of integers");
2504 return val;
2507 /* Convert the cons-of-integers, integer, or float value C to a signed
2508 value with extrema MIN and MAX. Signal an error if C does not have
2509 a valid format or is out of range. */
2510 intmax_t
2511 cons_to_signed (Lisp_Object c, intmax_t min, intmax_t max)
2513 bool valid = 0;
2514 intmax_t val IF_LINT (= 0);
2515 if (INTEGERP (c))
2517 val = XINT (c);
2518 valid = 1;
2520 else if (FLOATP (c))
2522 double d = XFLOAT_DATA (c);
2523 if (min <= d
2524 && d < (max == INTMAX_MAX ? (double) INTMAX_MAX + 1 : max + 1))
2526 val = d;
2527 valid = 1;
2530 else if (CONSP (c) && INTEGERP (XCAR (c)))
2532 intmax_t top = XINT (XCAR (c));
2533 Lisp_Object rest = XCDR (c);
2534 if (INTMAX_MIN >> 24 >> 16 <= top && top <= INTMAX_MAX >> 24 >> 16
2535 && CONSP (rest)
2536 && NATNUMP (XCAR (rest)) && XFASTINT (XCAR (rest)) < 1 << 24
2537 && NATNUMP (XCDR (rest)) && XFASTINT (XCDR (rest)) < 1 << 16)
2539 intmax_t mid = XFASTINT (XCAR (rest));
2540 val = top << 24 << 16 | mid << 16 | XFASTINT (XCDR (rest));
2541 valid = 1;
2543 else if (INTMAX_MIN >> 16 <= top && top <= INTMAX_MAX >> 16)
2545 if (CONSP (rest))
2546 rest = XCAR (rest);
2547 if (NATNUMP (rest) && XFASTINT (rest) < 1 << 16)
2549 val = top << 16 | XFASTINT (rest);
2550 valid = 1;
2555 if (! (valid && min <= val && val <= max))
2556 error ("Not an in-range integer, float, or cons of integers");
2557 return val;
2560 DEFUN ("number-to-string", Fnumber_to_string, Snumber_to_string, 1, 1, 0,
2561 doc: /* Return the decimal representation of NUMBER as a string.
2562 Uses a minus sign if negative.
2563 NUMBER may be an integer or a floating point number. */)
2564 (Lisp_Object number)
2566 char buffer[max (FLOAT_TO_STRING_BUFSIZE, INT_BUFSIZE_BOUND (EMACS_INT))];
2567 int len;
2569 CHECK_NUMBER_OR_FLOAT (number);
2571 if (FLOATP (number))
2572 len = float_to_string (buffer, XFLOAT_DATA (number));
2573 else
2574 len = sprintf (buffer, "%"pI"d", XINT (number));
2576 return make_unibyte_string (buffer, len);
2579 DEFUN ("string-to-number", Fstring_to_number, Sstring_to_number, 1, 2, 0,
2580 doc: /* Parse STRING as a decimal number and return the number.
2581 Ignore leading spaces and tabs, and all trailing chars. Return 0 if
2582 STRING cannot be parsed as an integer or floating point number.
2584 If BASE, interpret STRING as a number in that base. If BASE isn't
2585 present, base 10 is used. BASE must be between 2 and 16 (inclusive).
2586 If the base used is not 10, STRING is always parsed as an integer. */)
2587 (register Lisp_Object string, Lisp_Object base)
2589 register char *p;
2590 register int b;
2591 Lisp_Object val;
2593 CHECK_STRING (string);
2595 if (NILP (base))
2596 b = 10;
2597 else
2599 CHECK_NUMBER (base);
2600 if (! (2 <= XINT (base) && XINT (base) <= 16))
2601 xsignal1 (Qargs_out_of_range, base);
2602 b = XINT (base);
2605 p = SSDATA (string);
2606 while (*p == ' ' || *p == '\t')
2607 p++;
2609 val = string_to_number (p, b, 1);
2610 return NILP (val) ? make_number (0) : val;
2613 enum arithop
2615 Aadd,
2616 Asub,
2617 Amult,
2618 Adiv,
2619 Alogand,
2620 Alogior,
2621 Alogxor,
2622 Amax,
2623 Amin
2626 static Lisp_Object float_arith_driver (double, ptrdiff_t, enum arithop,
2627 ptrdiff_t, Lisp_Object *);
2628 static Lisp_Object
2629 arith_driver (enum arithop code, ptrdiff_t nargs, Lisp_Object *args)
2631 Lisp_Object val;
2632 ptrdiff_t argnum, ok_args;
2633 EMACS_INT accum = 0;
2634 EMACS_INT next, ok_accum;
2635 bool overflow = 0;
2637 switch (code)
2639 case Alogior:
2640 case Alogxor:
2641 case Aadd:
2642 case Asub:
2643 accum = 0;
2644 break;
2645 case Amult:
2646 case Adiv:
2647 accum = 1;
2648 break;
2649 case Alogand:
2650 accum = -1;
2651 break;
2652 default:
2653 break;
2656 for (argnum = 0; argnum < nargs; argnum++)
2658 if (! overflow)
2660 ok_args = argnum;
2661 ok_accum = accum;
2664 /* Using args[argnum] as argument to CHECK_NUMBER_... */
2665 val = args[argnum];
2666 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val);
2668 if (FLOATP (val))
2669 return float_arith_driver (ok_accum, ok_args, code,
2670 nargs, args);
2671 args[argnum] = val;
2672 next = XINT (args[argnum]);
2673 switch (code)
2675 case Aadd:
2676 overflow |= INT_ADD_WRAPV (accum, next, &accum);
2677 break;
2678 case Asub:
2679 if (! argnum)
2680 accum = nargs == 1 ? - next : next;
2681 else
2682 overflow |= INT_SUBTRACT_WRAPV (accum, next, &accum);
2683 break;
2684 case Amult:
2685 overflow |= INT_MULTIPLY_WRAPV (accum, next, &accum);
2686 break;
2687 case Adiv:
2688 if (! (argnum || nargs == 1))
2689 accum = next;
2690 else
2692 if (next == 0)
2693 xsignal0 (Qarith_error);
2694 if (INT_DIVIDE_OVERFLOW (accum, next))
2695 overflow = true;
2696 else
2697 accum /= next;
2699 break;
2700 case Alogand:
2701 accum &= next;
2702 break;
2703 case Alogior:
2704 accum |= next;
2705 break;
2706 case Alogxor:
2707 accum ^= next;
2708 break;
2709 case Amax:
2710 if (!argnum || next > accum)
2711 accum = next;
2712 break;
2713 case Amin:
2714 if (!argnum || next < accum)
2715 accum = next;
2716 break;
2720 XSETINT (val, accum);
2721 return val;
2724 #undef isnan
2725 #define isnan(x) ((x) != (x))
2727 static Lisp_Object
2728 float_arith_driver (double accum, ptrdiff_t argnum, enum arithop code,
2729 ptrdiff_t nargs, Lisp_Object *args)
2731 register Lisp_Object val;
2732 double next;
2734 for (; argnum < nargs; argnum++)
2736 val = args[argnum]; /* using args[argnum] as argument to CHECK_NUMBER_... */
2737 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val);
2739 if (FLOATP (val))
2741 next = XFLOAT_DATA (val);
2743 else
2745 args[argnum] = val; /* runs into a compiler bug. */
2746 next = XINT (args[argnum]);
2748 switch (code)
2750 case Aadd:
2751 accum += next;
2752 break;
2753 case Asub:
2754 accum = argnum ? accum - next : nargs == 1 ? - next : next;
2755 break;
2756 case Amult:
2757 accum *= next;
2758 break;
2759 case Adiv:
2760 if (! (argnum || nargs == 1))
2761 accum = next;
2762 else
2764 if (! IEEE_FLOATING_POINT && next == 0)
2765 xsignal0 (Qarith_error);
2766 accum /= next;
2768 break;
2769 case Alogand:
2770 case Alogior:
2771 case Alogxor:
2772 return wrong_type_argument (Qinteger_or_marker_p, val);
2773 case Amax:
2774 if (!argnum || isnan (next) || next > accum)
2775 accum = next;
2776 break;
2777 case Amin:
2778 if (!argnum || isnan (next) || next < accum)
2779 accum = next;
2780 break;
2784 return make_float (accum);
2788 DEFUN ("+", Fplus, Splus, 0, MANY, 0,
2789 doc: /* Return sum of any number of arguments, which are numbers or markers.
2790 usage: (+ &rest NUMBERS-OR-MARKERS) */)
2791 (ptrdiff_t nargs, Lisp_Object *args)
2793 return arith_driver (Aadd, nargs, args);
2796 DEFUN ("-", Fminus, Sminus, 0, MANY, 0,
2797 doc: /* Negate number or subtract numbers or markers and return the result.
2798 With one arg, negates it. With more than one arg,
2799 subtracts all but the first from the first.
2800 usage: (- &optional NUMBER-OR-MARKER &rest MORE-NUMBERS-OR-MARKERS) */)
2801 (ptrdiff_t nargs, Lisp_Object *args)
2803 return arith_driver (Asub, nargs, args);
2806 DEFUN ("*", Ftimes, Stimes, 0, MANY, 0,
2807 doc: /* Return product of any number of arguments, which are numbers or markers.
2808 usage: (* &rest NUMBERS-OR-MARKERS) */)
2809 (ptrdiff_t nargs, Lisp_Object *args)
2811 return arith_driver (Amult, nargs, args);
2814 DEFUN ("/", Fquo, Squo, 1, MANY, 0,
2815 doc: /* Divide number by divisors and return the result.
2816 With two or more arguments, return first argument divided by the rest.
2817 With one argument, return 1 divided by the argument.
2818 The arguments must be numbers or markers.
2819 usage: (/ NUMBER &rest DIVISORS) */)
2820 (ptrdiff_t nargs, Lisp_Object *args)
2822 ptrdiff_t argnum;
2823 for (argnum = 2; argnum < nargs; argnum++)
2824 if (FLOATP (args[argnum]))
2825 return float_arith_driver (0, 0, Adiv, nargs, args);
2826 return arith_driver (Adiv, nargs, args);
2829 DEFUN ("%", Frem, Srem, 2, 2, 0,
2830 doc: /* Return remainder of X divided by Y.
2831 Both must be integers or markers. */)
2832 (register Lisp_Object x, Lisp_Object y)
2834 Lisp_Object val;
2836 CHECK_NUMBER_COERCE_MARKER (x);
2837 CHECK_NUMBER_COERCE_MARKER (y);
2839 if (XINT (y) == 0)
2840 xsignal0 (Qarith_error);
2842 XSETINT (val, XINT (x) % XINT (y));
2843 return val;
2846 DEFUN ("mod", Fmod, Smod, 2, 2, 0,
2847 doc: /* Return X modulo Y.
2848 The result falls between zero (inclusive) and Y (exclusive).
2849 Both X and Y must be numbers or markers. */)
2850 (register Lisp_Object x, Lisp_Object y)
2852 Lisp_Object val;
2853 EMACS_INT i1, i2;
2855 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (x);
2856 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (y);
2858 if (FLOATP (x) || FLOATP (y))
2859 return fmod_float (x, y);
2861 i1 = XINT (x);
2862 i2 = XINT (y);
2864 if (i2 == 0)
2865 xsignal0 (Qarith_error);
2867 i1 %= i2;
2869 /* If the "remainder" comes out with the wrong sign, fix it. */
2870 if (i2 < 0 ? i1 > 0 : i1 < 0)
2871 i1 += i2;
2873 XSETINT (val, i1);
2874 return val;
2877 DEFUN ("max", Fmax, Smax, 1, MANY, 0,
2878 doc: /* Return largest of all the arguments (which must be numbers or markers).
2879 The value is always a number; markers are converted to numbers.
2880 usage: (max NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2881 (ptrdiff_t nargs, Lisp_Object *args)
2883 return arith_driver (Amax, nargs, args);
2886 DEFUN ("min", Fmin, Smin, 1, MANY, 0,
2887 doc: /* Return smallest of all the arguments (which must be numbers or markers).
2888 The value is always a number; markers are converted to numbers.
2889 usage: (min NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2890 (ptrdiff_t nargs, Lisp_Object *args)
2892 return arith_driver (Amin, nargs, args);
2895 DEFUN ("logand", Flogand, Slogand, 0, MANY, 0,
2896 doc: /* Return bitwise-and of all the arguments.
2897 Arguments may be integers, or markers converted to integers.
2898 usage: (logand &rest INTS-OR-MARKERS) */)
2899 (ptrdiff_t nargs, Lisp_Object *args)
2901 return arith_driver (Alogand, nargs, args);
2904 DEFUN ("logior", Flogior, Slogior, 0, MANY, 0,
2905 doc: /* Return bitwise-or of all the arguments.
2906 Arguments may be integers, or markers converted to integers.
2907 usage: (logior &rest INTS-OR-MARKERS) */)
2908 (ptrdiff_t nargs, Lisp_Object *args)
2910 return arith_driver (Alogior, nargs, args);
2913 DEFUN ("logxor", Flogxor, Slogxor, 0, MANY, 0,
2914 doc: /* Return bitwise-exclusive-or of all the arguments.
2915 Arguments may be integers, or markers converted to integers.
2916 usage: (logxor &rest INTS-OR-MARKERS) */)
2917 (ptrdiff_t nargs, Lisp_Object *args)
2919 return arith_driver (Alogxor, nargs, args);
2922 DEFUN ("ash", Fash, Sash, 2, 2, 0,
2923 doc: /* Return VALUE with its bits shifted left by COUNT.
2924 If COUNT is negative, shifting is actually to the right.
2925 In this case, the sign bit is duplicated. */)
2926 (register Lisp_Object value, Lisp_Object count)
2928 register Lisp_Object val;
2930 CHECK_NUMBER (value);
2931 CHECK_NUMBER (count);
2933 if (XINT (count) >= BITS_PER_EMACS_INT)
2934 XSETINT (val, 0);
2935 else if (XINT (count) > 0)
2936 XSETINT (val, XUINT (value) << XFASTINT (count));
2937 else if (XINT (count) <= -BITS_PER_EMACS_INT)
2938 XSETINT (val, XINT (value) < 0 ? -1 : 0);
2939 else
2940 XSETINT (val, XINT (value) >> -XINT (count));
2941 return val;
2944 DEFUN ("lsh", Flsh, Slsh, 2, 2, 0,
2945 doc: /* Return VALUE with its bits shifted left by COUNT.
2946 If COUNT is negative, shifting is actually to the right.
2947 In this case, zeros are shifted in on the left. */)
2948 (register Lisp_Object value, Lisp_Object count)
2950 register Lisp_Object val;
2952 CHECK_NUMBER (value);
2953 CHECK_NUMBER (count);
2955 if (XINT (count) >= BITS_PER_EMACS_INT)
2956 XSETINT (val, 0);
2957 else if (XINT (count) > 0)
2958 XSETINT (val, XUINT (value) << XFASTINT (count));
2959 else if (XINT (count) <= -BITS_PER_EMACS_INT)
2960 XSETINT (val, 0);
2961 else
2962 XSETINT (val, XUINT (value) >> -XINT (count));
2963 return val;
2966 DEFUN ("1+", Fadd1, Sadd1, 1, 1, 0,
2967 doc: /* Return NUMBER plus one. NUMBER may be a number or a marker.
2968 Markers are converted to integers. */)
2969 (register Lisp_Object number)
2971 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number);
2973 if (FLOATP (number))
2974 return (make_float (1.0 + XFLOAT_DATA (number)));
2976 XSETINT (number, XINT (number) + 1);
2977 return number;
2980 DEFUN ("1-", Fsub1, Ssub1, 1, 1, 0,
2981 doc: /* Return NUMBER minus one. NUMBER may be a number or a marker.
2982 Markers are converted to integers. */)
2983 (register Lisp_Object number)
2985 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number);
2987 if (FLOATP (number))
2988 return (make_float (-1.0 + XFLOAT_DATA (number)));
2990 XSETINT (number, XINT (number) - 1);
2991 return number;
2994 DEFUN ("lognot", Flognot, Slognot, 1, 1, 0,
2995 doc: /* Return the bitwise complement of NUMBER. NUMBER must be an integer. */)
2996 (register Lisp_Object number)
2998 CHECK_NUMBER (number);
2999 XSETINT (number, ~XINT (number));
3000 return number;
3003 DEFUN ("byteorder", Fbyteorder, Sbyteorder, 0, 0, 0,
3004 doc: /* Return the byteorder for the machine.
3005 Returns 66 (ASCII uppercase B) for big endian machines or 108 (ASCII
3006 lowercase l) for small endian machines. */
3007 attributes: const)
3008 (void)
3010 unsigned i = 0x04030201;
3011 int order = *(char *)&i == 1 ? 108 : 66;
3013 return make_number (order);
3016 /* Because we round up the bool vector allocate size to word_size
3017 units, we can safely read past the "end" of the vector in the
3018 operations below. These extra bits are always zero. */
3020 static bits_word
3021 bool_vector_spare_mask (EMACS_INT nr_bits)
3023 return (((bits_word) 1) << (nr_bits % BITS_PER_BITS_WORD)) - 1;
3026 /* Info about unsigned long long, falling back on unsigned long
3027 if unsigned long long is not available. */
3029 #if HAVE_UNSIGNED_LONG_LONG_INT && defined ULLONG_MAX
3030 enum { BITS_PER_ULL = CHAR_BIT * sizeof (unsigned long long) };
3031 # define ULL_MAX ULLONG_MAX
3032 #else
3033 enum { BITS_PER_ULL = CHAR_BIT * sizeof (unsigned long) };
3034 # define ULL_MAX ULONG_MAX
3035 # define count_one_bits_ll count_one_bits_l
3036 # define count_trailing_zeros_ll count_trailing_zeros_l
3037 #endif
3039 /* Shift VAL right by the width of an unsigned long long.
3040 BITS_PER_ULL must be less than BITS_PER_BITS_WORD. */
3042 static bits_word
3043 shift_right_ull (bits_word w)
3045 /* Pacify bogus GCC warning about shift count exceeding type width. */
3046 int shift = BITS_PER_ULL - BITS_PER_BITS_WORD < 0 ? BITS_PER_ULL : 0;
3047 return w >> shift;
3050 /* Return the number of 1 bits in W. */
3052 static int
3053 count_one_bits_word (bits_word w)
3055 if (BITS_WORD_MAX <= UINT_MAX)
3056 return count_one_bits (w);
3057 else if (BITS_WORD_MAX <= ULONG_MAX)
3058 return count_one_bits_l (w);
3059 else
3061 int i = 0, count = 0;
3062 while (count += count_one_bits_ll (w),
3063 (i += BITS_PER_ULL) < BITS_PER_BITS_WORD)
3064 w = shift_right_ull (w);
3065 return count;
3069 enum bool_vector_op { bool_vector_exclusive_or,
3070 bool_vector_union,
3071 bool_vector_intersection,
3072 bool_vector_set_difference,
3073 bool_vector_subsetp };
3075 static Lisp_Object
3076 bool_vector_binop_driver (Lisp_Object a,
3077 Lisp_Object b,
3078 Lisp_Object dest,
3079 enum bool_vector_op op)
3081 EMACS_INT nr_bits;
3082 bits_word *adata, *bdata, *destdata;
3083 ptrdiff_t i = 0;
3084 ptrdiff_t nr_words;
3086 CHECK_BOOL_VECTOR (a);
3087 CHECK_BOOL_VECTOR (b);
3089 nr_bits = bool_vector_size (a);
3090 if (bool_vector_size (b) != nr_bits)
3091 wrong_length_argument (a, b, dest);
3093 nr_words = bool_vector_words (nr_bits);
3094 adata = bool_vector_data (a);
3095 bdata = bool_vector_data (b);
3097 if (NILP (dest))
3099 dest = make_uninit_bool_vector (nr_bits);
3100 destdata = bool_vector_data (dest);
3102 else
3104 CHECK_BOOL_VECTOR (dest);
3105 destdata = bool_vector_data (dest);
3106 if (bool_vector_size (dest) != nr_bits)
3107 wrong_length_argument (a, b, dest);
3109 switch (op)
3111 case bool_vector_exclusive_or:
3112 for (; i < nr_words; i++)
3113 if (destdata[i] != (adata[i] ^ bdata[i]))
3114 goto set_dest;
3115 break;
3117 case bool_vector_subsetp:
3118 for (; i < nr_words; i++)
3119 if (adata[i] &~ bdata[i])
3120 return Qnil;
3121 return Qt;
3123 case bool_vector_union:
3124 for (; i < nr_words; i++)
3125 if (destdata[i] != (adata[i] | bdata[i]))
3126 goto set_dest;
3127 break;
3129 case bool_vector_intersection:
3130 for (; i < nr_words; i++)
3131 if (destdata[i] != (adata[i] & bdata[i]))
3132 goto set_dest;
3133 break;
3135 case bool_vector_set_difference:
3136 for (; i < nr_words; i++)
3137 if (destdata[i] != (adata[i] &~ bdata[i]))
3138 goto set_dest;
3139 break;
3142 return Qnil;
3145 set_dest:
3146 switch (op)
3148 case bool_vector_exclusive_or:
3149 for (; i < nr_words; i++)
3150 destdata[i] = adata[i] ^ bdata[i];
3151 break;
3153 case bool_vector_union:
3154 for (; i < nr_words; i++)
3155 destdata[i] = adata[i] | bdata[i];
3156 break;
3158 case bool_vector_intersection:
3159 for (; i < nr_words; i++)
3160 destdata[i] = adata[i] & bdata[i];
3161 break;
3163 case bool_vector_set_difference:
3164 for (; i < nr_words; i++)
3165 destdata[i] = adata[i] &~ bdata[i];
3166 break;
3168 default:
3169 eassume (0);
3172 return dest;
3175 /* PRECONDITION must be true. Return VALUE. This odd construction
3176 works around a bogus GCC diagnostic "shift count >= width of type". */
3178 static int
3179 pre_value (bool precondition, int value)
3181 eassume (precondition);
3182 return precondition ? value : 0;
3185 /* Compute the number of trailing zero bits in val. If val is zero,
3186 return the number of bits in val. */
3187 static int
3188 count_trailing_zero_bits (bits_word val)
3190 if (BITS_WORD_MAX == UINT_MAX)
3191 return count_trailing_zeros (val);
3192 if (BITS_WORD_MAX == ULONG_MAX)
3193 return count_trailing_zeros_l (val);
3194 if (BITS_WORD_MAX == ULL_MAX)
3195 return count_trailing_zeros_ll (val);
3197 /* The rest of this code is for the unlikely platform where bits_word differs
3198 in width from unsigned int, unsigned long, and unsigned long long. */
3199 val |= ~ BITS_WORD_MAX;
3200 if (BITS_WORD_MAX <= UINT_MAX)
3201 return count_trailing_zeros (val);
3202 if (BITS_WORD_MAX <= ULONG_MAX)
3203 return count_trailing_zeros_l (val);
3204 else
3206 int count;
3207 for (count = 0;
3208 count < BITS_PER_BITS_WORD - BITS_PER_ULL;
3209 count += BITS_PER_ULL)
3211 if (val & ULL_MAX)
3212 return count + count_trailing_zeros_ll (val);
3213 val = shift_right_ull (val);
3216 if (BITS_PER_BITS_WORD % BITS_PER_ULL != 0
3217 && BITS_WORD_MAX == (bits_word) -1)
3218 val |= (bits_word) 1 << pre_value (ULONG_MAX < BITS_WORD_MAX,
3219 BITS_PER_BITS_WORD % BITS_PER_ULL);
3220 return count + count_trailing_zeros_ll (val);
3224 static bits_word
3225 bits_word_to_host_endian (bits_word val)
3227 #ifndef WORDS_BIGENDIAN
3228 return val;
3229 #else
3230 if (BITS_WORD_MAX >> 31 == 1)
3231 return bswap_32 (val);
3232 # if HAVE_UNSIGNED_LONG_LONG
3233 if (BITS_WORD_MAX >> 31 >> 31 >> 1 == 1)
3234 return bswap_64 (val);
3235 # endif
3237 int i;
3238 bits_word r = 0;
3239 for (i = 0; i < sizeof val; i++)
3241 r = ((r << 1 << (CHAR_BIT - 1))
3242 | (val & ((1u << 1 << (CHAR_BIT - 1)) - 1)));
3243 val = val >> 1 >> (CHAR_BIT - 1);
3245 return r;
3247 #endif
3250 DEFUN ("bool-vector-exclusive-or", Fbool_vector_exclusive_or,
3251 Sbool_vector_exclusive_or, 2, 3, 0,
3252 doc: /* Return A ^ B, bitwise exclusive or.
3253 If optional third argument C is given, store result into C.
3254 A, B, and C must be bool vectors of the same length.
3255 Return the destination vector if it changed or nil otherwise. */)
3256 (Lisp_Object a, Lisp_Object b, Lisp_Object c)
3258 return bool_vector_binop_driver (a, b, c, bool_vector_exclusive_or);
3261 DEFUN ("bool-vector-union", Fbool_vector_union,
3262 Sbool_vector_union, 2, 3, 0,
3263 doc: /* Return A | B, bitwise or.
3264 If optional third argument C is given, store result into C.
3265 A, B, and C must be bool vectors of the same length.
3266 Return the destination vector if it changed or nil otherwise. */)
3267 (Lisp_Object a, Lisp_Object b, Lisp_Object c)
3269 return bool_vector_binop_driver (a, b, c, bool_vector_union);
3272 DEFUN ("bool-vector-intersection", Fbool_vector_intersection,
3273 Sbool_vector_intersection, 2, 3, 0,
3274 doc: /* Return A & B, bitwise and.
3275 If optional third argument C is given, store result into C.
3276 A, B, and C must be bool vectors of the same length.
3277 Return the destination vector if it changed or nil otherwise. */)
3278 (Lisp_Object a, Lisp_Object b, Lisp_Object c)
3280 return bool_vector_binop_driver (a, b, c, bool_vector_intersection);
3283 DEFUN ("bool-vector-set-difference", Fbool_vector_set_difference,
3284 Sbool_vector_set_difference, 2, 3, 0,
3285 doc: /* Return A &~ B, set difference.
3286 If optional third argument C is given, store result into C.
3287 A, B, and C must be bool vectors of the same length.
3288 Return the destination vector if it changed or nil otherwise. */)
3289 (Lisp_Object a, Lisp_Object b, Lisp_Object c)
3291 return bool_vector_binop_driver (a, b, c, bool_vector_set_difference);
3294 DEFUN ("bool-vector-subsetp", Fbool_vector_subsetp,
3295 Sbool_vector_subsetp, 2, 2, 0,
3296 doc: /* Return t if every t value in A is also t in B, nil otherwise.
3297 A and B must be bool vectors of the same length. */)
3298 (Lisp_Object a, Lisp_Object b)
3300 return bool_vector_binop_driver (a, b, b, bool_vector_subsetp);
3303 DEFUN ("bool-vector-not", Fbool_vector_not,
3304 Sbool_vector_not, 1, 2, 0,
3305 doc: /* Compute ~A, set complement.
3306 If optional second argument B is given, store result into B.
3307 A and B must be bool vectors of the same length.
3308 Return the destination vector. */)
3309 (Lisp_Object a, Lisp_Object b)
3311 EMACS_INT nr_bits;
3312 bits_word *bdata, *adata;
3313 ptrdiff_t i;
3315 CHECK_BOOL_VECTOR (a);
3316 nr_bits = bool_vector_size (a);
3318 if (NILP (b))
3319 b = make_uninit_bool_vector (nr_bits);
3320 else
3322 CHECK_BOOL_VECTOR (b);
3323 if (bool_vector_size (b) != nr_bits)
3324 wrong_length_argument (a, b, Qnil);
3327 bdata = bool_vector_data (b);
3328 adata = bool_vector_data (a);
3330 for (i = 0; i < nr_bits / BITS_PER_BITS_WORD; i++)
3331 bdata[i] = BITS_WORD_MAX & ~adata[i];
3333 if (nr_bits % BITS_PER_BITS_WORD)
3335 bits_word mword = bits_word_to_host_endian (adata[i]);
3336 mword = ~mword;
3337 mword &= bool_vector_spare_mask (nr_bits);
3338 bdata[i] = bits_word_to_host_endian (mword);
3341 return b;
3344 DEFUN ("bool-vector-count-population", Fbool_vector_count_population,
3345 Sbool_vector_count_population, 1, 1, 0,
3346 doc: /* Count how many elements in A are t.
3347 A is a bool vector. To count A's nil elements, subtract the return
3348 value from A's length. */)
3349 (Lisp_Object a)
3351 EMACS_INT count;
3352 EMACS_INT nr_bits;
3353 bits_word *adata;
3354 ptrdiff_t i, nwords;
3356 CHECK_BOOL_VECTOR (a);
3358 nr_bits = bool_vector_size (a);
3359 nwords = bool_vector_words (nr_bits);
3360 count = 0;
3361 adata = bool_vector_data (a);
3363 for (i = 0; i < nwords; i++)
3364 count += count_one_bits_word (adata[i]);
3366 return make_number (count);
3369 DEFUN ("bool-vector-count-consecutive", Fbool_vector_count_consecutive,
3370 Sbool_vector_count_consecutive, 3, 3, 0,
3371 doc: /* Count how many consecutive elements in A equal B starting at I.
3372 A is a bool vector, B is t or nil, and I is an index into A. */)
3373 (Lisp_Object a, Lisp_Object b, Lisp_Object i)
3375 EMACS_INT count;
3376 EMACS_INT nr_bits;
3377 int offset;
3378 bits_word *adata;
3379 bits_word twiddle;
3380 bits_word mword; /* Machine word. */
3381 ptrdiff_t pos, pos0;
3382 ptrdiff_t nr_words;
3384 CHECK_BOOL_VECTOR (a);
3385 CHECK_NATNUM (i);
3387 nr_bits = bool_vector_size (a);
3388 if (XFASTINT (i) > nr_bits) /* Allow one past the end for convenience */
3389 args_out_of_range (a, i);
3391 adata = bool_vector_data (a);
3392 nr_words = bool_vector_words (nr_bits);
3393 pos = XFASTINT (i) / BITS_PER_BITS_WORD;
3394 offset = XFASTINT (i) % BITS_PER_BITS_WORD;
3395 count = 0;
3397 /* By XORing with twiddle, we transform the problem of "count
3398 consecutive equal values" into "count the zero bits". The latter
3399 operation usually has hardware support. */
3400 twiddle = NILP (b) ? 0 : BITS_WORD_MAX;
3402 /* Scan the remainder of the mword at the current offset. */
3403 if (pos < nr_words && offset != 0)
3405 mword = bits_word_to_host_endian (adata[pos]);
3406 mword ^= twiddle;
3407 mword >>= offset;
3409 /* Do not count the pad bits. */
3410 mword |= (bits_word) 1 << (BITS_PER_BITS_WORD - offset);
3412 count = count_trailing_zero_bits (mword);
3413 pos++;
3414 if (count + offset < BITS_PER_BITS_WORD)
3415 return make_number (count);
3418 /* Scan whole words until we either reach the end of the vector or
3419 find an mword that doesn't completely match. twiddle is
3420 endian-independent. */
3421 pos0 = pos;
3422 while (pos < nr_words && adata[pos] == twiddle)
3423 pos++;
3424 count += (pos - pos0) * BITS_PER_BITS_WORD;
3426 if (pos < nr_words)
3428 /* If we stopped because of a mismatch, see how many bits match
3429 in the current mword. */
3430 mword = bits_word_to_host_endian (adata[pos]);
3431 mword ^= twiddle;
3432 count += count_trailing_zero_bits (mword);
3434 else if (nr_bits % BITS_PER_BITS_WORD != 0)
3436 /* If we hit the end, we might have overshot our count. Reduce
3437 the total by the number of spare bits at the end of the
3438 vector. */
3439 count -= BITS_PER_BITS_WORD - nr_bits % BITS_PER_BITS_WORD;
3442 return make_number (count);
3446 void
3447 syms_of_data (void)
3449 Lisp_Object error_tail, arith_tail;
3451 DEFSYM (Qquote, "quote");
3452 DEFSYM (Qlambda, "lambda");
3453 DEFSYM (Qsubr, "subr");
3454 DEFSYM (Qerror_conditions, "error-conditions");
3455 DEFSYM (Qerror_message, "error-message");
3456 DEFSYM (Qtop_level, "top-level");
3458 DEFSYM (Qerror, "error");
3459 DEFSYM (Quser_error, "user-error");
3460 DEFSYM (Qquit, "quit");
3461 DEFSYM (Qwrong_length_argument, "wrong-length-argument");
3462 DEFSYM (Qwrong_type_argument, "wrong-type-argument");
3463 DEFSYM (Qargs_out_of_range, "args-out-of-range");
3464 DEFSYM (Qvoid_function, "void-function");
3465 DEFSYM (Qcyclic_function_indirection, "cyclic-function-indirection");
3466 DEFSYM (Qcyclic_variable_indirection, "cyclic-variable-indirection");
3467 DEFSYM (Qvoid_variable, "void-variable");
3468 DEFSYM (Qsetting_constant, "setting-constant");
3469 DEFSYM (Qinvalid_read_syntax, "invalid-read-syntax");
3471 DEFSYM (Qinvalid_function, "invalid-function");
3472 DEFSYM (Qwrong_number_of_arguments, "wrong-number-of-arguments");
3473 DEFSYM (Qno_catch, "no-catch");
3474 DEFSYM (Qend_of_file, "end-of-file");
3475 DEFSYM (Qarith_error, "arith-error");
3476 DEFSYM (Qbeginning_of_buffer, "beginning-of-buffer");
3477 DEFSYM (Qend_of_buffer, "end-of-buffer");
3478 DEFSYM (Qbuffer_read_only, "buffer-read-only");
3479 DEFSYM (Qtext_read_only, "text-read-only");
3480 DEFSYM (Qmark_inactive, "mark-inactive");
3482 DEFSYM (Qlistp, "listp");
3483 DEFSYM (Qconsp, "consp");
3484 DEFSYM (Qsymbolp, "symbolp");
3485 DEFSYM (Qintegerp, "integerp");
3486 DEFSYM (Qnatnump, "natnump");
3487 DEFSYM (Qwholenump, "wholenump");
3488 DEFSYM (Qstringp, "stringp");
3489 DEFSYM (Qarrayp, "arrayp");
3490 DEFSYM (Qsequencep, "sequencep");
3491 DEFSYM (Qbufferp, "bufferp");
3492 DEFSYM (Qvectorp, "vectorp");
3493 DEFSYM (Qbool_vector_p, "bool-vector-p");
3494 DEFSYM (Qchar_or_string_p, "char-or-string-p");
3495 DEFSYM (Qmarkerp, "markerp");
3496 #ifdef HAVE_MODULES
3497 DEFSYM (Quser_ptrp, "user-ptrp");
3498 #endif
3499 DEFSYM (Qbuffer_or_string_p, "buffer-or-string-p");
3500 DEFSYM (Qinteger_or_marker_p, "integer-or-marker-p");
3501 DEFSYM (Qfboundp, "fboundp");
3503 DEFSYM (Qfloatp, "floatp");
3504 DEFSYM (Qnumberp, "numberp");
3505 DEFSYM (Qnumber_or_marker_p, "number-or-marker-p");
3507 DEFSYM (Qchar_table_p, "char-table-p");
3508 DEFSYM (Qvector_or_char_table_p, "vector-or-char-table-p");
3510 DEFSYM (Qsubrp, "subrp");
3511 DEFSYM (Qunevalled, "unevalled");
3512 DEFSYM (Qmany, "many");
3514 DEFSYM (Qcdr, "cdr");
3516 error_tail = pure_cons (Qerror, Qnil);
3518 /* ERROR is used as a signaler for random errors for which nothing else is
3519 right. */
3521 Fput (Qerror, Qerror_conditions,
3522 error_tail);
3523 Fput (Qerror, Qerror_message,
3524 build_pure_c_string ("error"));
3526 #define PUT_ERROR(sym, tail, msg) \
3527 Fput (sym, Qerror_conditions, pure_cons (sym, tail)); \
3528 Fput (sym, Qerror_message, build_pure_c_string (msg))
3530 PUT_ERROR (Qquit, Qnil, "Quit");
3532 PUT_ERROR (Quser_error, error_tail, "");
3533 PUT_ERROR (Qwrong_length_argument, error_tail, "Wrong length argument");
3534 PUT_ERROR (Qwrong_type_argument, error_tail, "Wrong type argument");
3535 PUT_ERROR (Qargs_out_of_range, error_tail, "Args out of range");
3536 PUT_ERROR (Qvoid_function, error_tail,
3537 "Symbol's function definition is void");
3538 PUT_ERROR (Qcyclic_function_indirection, error_tail,
3539 "Symbol's chain of function indirections contains a loop");
3540 PUT_ERROR (Qcyclic_variable_indirection, error_tail,
3541 "Symbol's chain of variable indirections contains a loop");
3542 DEFSYM (Qcircular_list, "circular-list");
3543 PUT_ERROR (Qcircular_list, error_tail, "List contains a loop");
3544 PUT_ERROR (Qvoid_variable, error_tail, "Symbol's value as variable is void");
3545 PUT_ERROR (Qsetting_constant, error_tail,
3546 "Attempt to set a constant symbol");
3547 PUT_ERROR (Qinvalid_read_syntax, error_tail, "Invalid read syntax");
3548 PUT_ERROR (Qinvalid_function, error_tail, "Invalid function");
3549 PUT_ERROR (Qwrong_number_of_arguments, error_tail,
3550 "Wrong number of arguments");
3551 PUT_ERROR (Qno_catch, error_tail, "No catch for tag");
3552 PUT_ERROR (Qend_of_file, error_tail, "End of file during parsing");
3554 arith_tail = pure_cons (Qarith_error, error_tail);
3555 Fput (Qarith_error, Qerror_conditions, arith_tail);
3556 Fput (Qarith_error, Qerror_message, build_pure_c_string ("Arithmetic error"));
3558 PUT_ERROR (Qbeginning_of_buffer, error_tail, "Beginning of buffer");
3559 PUT_ERROR (Qend_of_buffer, error_tail, "End of buffer");
3560 PUT_ERROR (Qbuffer_read_only, error_tail, "Buffer is read-only");
3561 PUT_ERROR (Qtext_read_only, pure_cons (Qbuffer_read_only, error_tail),
3562 "Text is read-only");
3564 DEFSYM (Qrange_error, "range-error");
3565 DEFSYM (Qdomain_error, "domain-error");
3566 DEFSYM (Qsingularity_error, "singularity-error");
3567 DEFSYM (Qoverflow_error, "overflow-error");
3568 DEFSYM (Qunderflow_error, "underflow-error");
3570 PUT_ERROR (Qdomain_error, arith_tail, "Arithmetic domain error");
3572 PUT_ERROR (Qrange_error, arith_tail, "Arithmetic range error");
3574 PUT_ERROR (Qsingularity_error, Fcons (Qdomain_error, arith_tail),
3575 "Arithmetic singularity error");
3577 PUT_ERROR (Qoverflow_error, Fcons (Qdomain_error, arith_tail),
3578 "Arithmetic overflow error");
3579 PUT_ERROR (Qunderflow_error, Fcons (Qdomain_error, arith_tail),
3580 "Arithmetic underflow error");
3582 /* Types that type-of returns. */
3583 DEFSYM (Qinteger, "integer");
3584 DEFSYM (Qsymbol, "symbol");
3585 DEFSYM (Qstring, "string");
3586 DEFSYM (Qcons, "cons");
3587 DEFSYM (Qmarker, "marker");
3588 DEFSYM (Qoverlay, "overlay");
3589 DEFSYM (Qfinalizer, "finalizer");
3590 #ifdef HAVE_MODULES
3591 DEFSYM (Quser_ptr, "user-ptr");
3592 #endif
3593 DEFSYM (Qfloat, "float");
3594 DEFSYM (Qwindow_configuration, "window-configuration");
3595 DEFSYM (Qprocess, "process");
3596 DEFSYM (Qwindow, "window");
3597 DEFSYM (Qcompiled_function, "compiled-function");
3598 DEFSYM (Qbuffer, "buffer");
3599 DEFSYM (Qframe, "frame");
3600 DEFSYM (Qvector, "vector");
3601 DEFSYM (Qchar_table, "char-table");
3602 DEFSYM (Qbool_vector, "bool-vector");
3603 DEFSYM (Qhash_table, "hash-table");
3605 DEFSYM (Qdefun, "defun");
3607 DEFSYM (Qfont_spec, "font-spec");
3608 DEFSYM (Qfont_entity, "font-entity");
3609 DEFSYM (Qfont_object, "font-object");
3611 DEFSYM (Qinteractive_form, "interactive-form");
3612 DEFSYM (Qdefalias_fset_function, "defalias-fset-function");
3614 defsubr (&Sindirect_variable);
3615 defsubr (&Sinteractive_form);
3616 defsubr (&Seq);
3617 defsubr (&Snull);
3618 defsubr (&Stype_of);
3619 defsubr (&Slistp);
3620 defsubr (&Snlistp);
3621 defsubr (&Sconsp);
3622 defsubr (&Satom);
3623 defsubr (&Sintegerp);
3624 defsubr (&Sinteger_or_marker_p);
3625 defsubr (&Snumberp);
3626 defsubr (&Snumber_or_marker_p);
3627 defsubr (&Sfloatp);
3628 defsubr (&Snatnump);
3629 defsubr (&Ssymbolp);
3630 defsubr (&Skeywordp);
3631 defsubr (&Sstringp);
3632 defsubr (&Smultibyte_string_p);
3633 defsubr (&Svectorp);
3634 defsubr (&Schar_table_p);
3635 defsubr (&Svector_or_char_table_p);
3636 defsubr (&Sbool_vector_p);
3637 defsubr (&Sarrayp);
3638 defsubr (&Ssequencep);
3639 defsubr (&Sbufferp);
3640 defsubr (&Smarkerp);
3641 defsubr (&Ssubrp);
3642 defsubr (&Sbyte_code_function_p);
3643 defsubr (&Schar_or_string_p);
3644 defsubr (&Scar);
3645 defsubr (&Scdr);
3646 defsubr (&Scar_safe);
3647 defsubr (&Scdr_safe);
3648 defsubr (&Ssetcar);
3649 defsubr (&Ssetcdr);
3650 defsubr (&Ssymbol_function);
3651 defsubr (&Sindirect_function);
3652 defsubr (&Ssymbol_plist);
3653 defsubr (&Ssymbol_name);
3654 defsubr (&Smakunbound);
3655 defsubr (&Sfmakunbound);
3656 defsubr (&Sboundp);
3657 defsubr (&Sfboundp);
3658 defsubr (&Sfset);
3659 defsubr (&Sdefalias);
3660 defsubr (&Ssetplist);
3661 defsubr (&Ssymbol_value);
3662 defsubr (&Sset);
3663 defsubr (&Sdefault_boundp);
3664 defsubr (&Sdefault_value);
3665 defsubr (&Sset_default);
3666 defsubr (&Ssetq_default);
3667 defsubr (&Smake_variable_buffer_local);
3668 defsubr (&Smake_local_variable);
3669 defsubr (&Skill_local_variable);
3670 defsubr (&Smake_variable_frame_local);
3671 defsubr (&Slocal_variable_p);
3672 defsubr (&Slocal_variable_if_set_p);
3673 defsubr (&Svariable_binding_locus);
3674 #if 0 /* XXX Remove this. --lorentey */
3675 defsubr (&Sterminal_local_value);
3676 defsubr (&Sset_terminal_local_value);
3677 #endif
3678 defsubr (&Saref);
3679 defsubr (&Saset);
3680 defsubr (&Snumber_to_string);
3681 defsubr (&Sstring_to_number);
3682 defsubr (&Seqlsign);
3683 defsubr (&Slss);
3684 defsubr (&Sgtr);
3685 defsubr (&Sleq);
3686 defsubr (&Sgeq);
3687 defsubr (&Sneq);
3688 defsubr (&Splus);
3689 defsubr (&Sminus);
3690 defsubr (&Stimes);
3691 defsubr (&Squo);
3692 defsubr (&Srem);
3693 defsubr (&Smod);
3694 defsubr (&Smax);
3695 defsubr (&Smin);
3696 defsubr (&Slogand);
3697 defsubr (&Slogior);
3698 defsubr (&Slogxor);
3699 defsubr (&Slsh);
3700 defsubr (&Sash);
3701 defsubr (&Sadd1);
3702 defsubr (&Ssub1);
3703 defsubr (&Slognot);
3704 defsubr (&Sbyteorder);
3705 defsubr (&Ssubr_arity);
3706 defsubr (&Ssubr_name);
3707 #ifdef HAVE_MODULES
3708 defsubr (&Suser_ptrp);
3709 #endif
3711 defsubr (&Sbool_vector_exclusive_or);
3712 defsubr (&Sbool_vector_union);
3713 defsubr (&Sbool_vector_intersection);
3714 defsubr (&Sbool_vector_set_difference);
3715 defsubr (&Sbool_vector_not);
3716 defsubr (&Sbool_vector_subsetp);
3717 defsubr (&Sbool_vector_count_consecutive);
3718 defsubr (&Sbool_vector_count_population);
3720 set_symbol_function (Qwholenump, XSYMBOL (Qnatnump)->function);
3722 DEFVAR_LISP ("most-positive-fixnum", Vmost_positive_fixnum,
3723 doc: /* The largest value that is representable in a Lisp integer. */);
3724 Vmost_positive_fixnum = make_number (MOST_POSITIVE_FIXNUM);
3725 XSYMBOL (intern_c_string ("most-positive-fixnum"))->constant = 1;
3727 DEFVAR_LISP ("most-negative-fixnum", Vmost_negative_fixnum,
3728 doc: /* The smallest value that is representable in a Lisp integer. */);
3729 Vmost_negative_fixnum = make_number (MOST_NEGATIVE_FIXNUM);
3730 XSYMBOL (intern_c_string ("most-negative-fixnum"))->constant = 1;