ibuffer-decompose-filter: Avoid side effects on error
[emacs.git] / src / data.c
blob64cd8b23b467d670494d4205c8aeafd1bfab5f2f
1 /* Primitive operations on Lisp data types for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985-1986, 1988, 1993-1995, 1997-2016 Free Software
3 Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or (at
10 your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
22 #include <stdio.h>
24 #include <byteswap.h>
25 #include <count-one-bits.h>
26 #include <count-trailing-zeros.h>
27 #include <intprops.h>
29 #include "lisp.h"
30 #include "puresize.h"
31 #include "character.h"
32 #include "buffer.h"
33 #include "keyboard.h"
34 #include "frame.h"
35 #include "keymap.h"
37 static void swap_in_symval_forwarding (struct Lisp_Symbol *,
38 struct Lisp_Buffer_Local_Value *);
40 static bool
41 BOOLFWDP (union Lisp_Fwd *a)
43 return XFWDTYPE (a) == Lisp_Fwd_Bool;
45 static bool
46 INTFWDP (union Lisp_Fwd *a)
48 return XFWDTYPE (a) == Lisp_Fwd_Int;
50 static bool
51 KBOARD_OBJFWDP (union Lisp_Fwd *a)
53 return XFWDTYPE (a) == Lisp_Fwd_Kboard_Obj;
55 static bool
56 OBJFWDP (union Lisp_Fwd *a)
58 return XFWDTYPE (a) == Lisp_Fwd_Obj;
61 static struct Lisp_Boolfwd *
62 XBOOLFWD (union Lisp_Fwd *a)
64 eassert (BOOLFWDP (a));
65 return &a->u_boolfwd;
67 static struct Lisp_Kboard_Objfwd *
68 XKBOARD_OBJFWD (union Lisp_Fwd *a)
70 eassert (KBOARD_OBJFWDP (a));
71 return &a->u_kboard_objfwd;
73 static struct Lisp_Intfwd *
74 XINTFWD (union Lisp_Fwd *a)
76 eassert (INTFWDP (a));
77 return &a->u_intfwd;
79 static struct Lisp_Objfwd *
80 XOBJFWD (union Lisp_Fwd *a)
82 eassert (OBJFWDP (a));
83 return &a->u_objfwd;
86 static void
87 CHECK_SUBR (Lisp_Object x)
89 CHECK_TYPE (SUBRP (x), Qsubrp, x);
92 static void
93 set_blv_found (struct Lisp_Buffer_Local_Value *blv, int found)
95 eassert (found == !EQ (blv->defcell, blv->valcell));
96 blv->found = found;
99 static Lisp_Object
100 blv_value (struct Lisp_Buffer_Local_Value *blv)
102 return XCDR (blv->valcell);
105 static void
106 set_blv_value (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
108 XSETCDR (blv->valcell, val);
111 static void
112 set_blv_where (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
114 blv->where = val;
117 static void
118 set_blv_defcell (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
120 blv->defcell = val;
123 static void
124 set_blv_valcell (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
126 blv->valcell = val;
129 static _Noreturn void
130 wrong_length_argument (Lisp_Object a1, Lisp_Object a2, Lisp_Object a3)
132 Lisp_Object size1 = make_number (bool_vector_size (a1));
133 Lisp_Object size2 = make_number (bool_vector_size (a2));
134 if (NILP (a3))
135 xsignal2 (Qwrong_length_argument, size1, size2);
136 else
137 xsignal3 (Qwrong_length_argument, size1, size2,
138 make_number (bool_vector_size (a3)));
141 _Noreturn void
142 wrong_type_argument (register Lisp_Object predicate, register Lisp_Object value)
144 /* If VALUE is not even a valid Lisp object, we'd want to abort here
145 where we can get a backtrace showing where it came from. We used
146 to try and do that by checking the tagbits, but nowadays all
147 tagbits are potentially valid. */
148 /* if ((unsigned int) XTYPE (value) >= Lisp_Type_Limit)
149 * emacs_abort (); */
151 xsignal2 (Qwrong_type_argument, predicate, value);
154 void
155 pure_write_error (Lisp_Object obj)
157 xsignal2 (Qerror, build_string ("Attempt to modify read-only object"), obj);
160 void
161 args_out_of_range (Lisp_Object a1, Lisp_Object a2)
163 xsignal2 (Qargs_out_of_range, a1, a2);
166 void
167 args_out_of_range_3 (Lisp_Object a1, Lisp_Object a2, Lisp_Object a3)
169 xsignal3 (Qargs_out_of_range, a1, a2, a3);
173 /* Data type predicates. */
175 DEFUN ("eq", Feq, Seq, 2, 2, 0,
176 doc: /* Return t if the two args are the same Lisp object. */
177 attributes: const)
178 (Lisp_Object obj1, Lisp_Object obj2)
180 if (EQ (obj1, obj2))
181 return Qt;
182 return Qnil;
185 DEFUN ("null", Fnull, Snull, 1, 1, 0,
186 doc: /* Return t if OBJECT is nil, and return nil otherwise. */
187 attributes: const)
188 (Lisp_Object object)
190 if (NILP (object))
191 return Qt;
192 return Qnil;
195 DEFUN ("type-of", Ftype_of, Stype_of, 1, 1, 0,
196 doc: /* Return a symbol representing the type of OBJECT.
197 The symbol returned names the object's basic type;
198 for example, (type-of 1) returns `integer'. */)
199 (Lisp_Object object)
201 switch (XTYPE (object))
203 case_Lisp_Int:
204 return Qinteger;
206 case Lisp_Symbol:
207 return Qsymbol;
209 case Lisp_String:
210 return Qstring;
212 case Lisp_Cons:
213 return Qcons;
215 case Lisp_Misc:
216 switch (XMISCTYPE (object))
218 case Lisp_Misc_Marker:
219 return Qmarker;
220 case Lisp_Misc_Overlay:
221 return Qoverlay;
222 case Lisp_Misc_Float:
223 return Qfloat;
224 case Lisp_Misc_Finalizer:
225 return Qfinalizer;
226 #ifdef HAVE_MODULES
227 case Lisp_Misc_User_Ptr:
228 return Quser_ptr;
229 #endif
230 default:
231 emacs_abort ();
234 case Lisp_Vectorlike:
235 if (WINDOW_CONFIGURATIONP (object))
236 return Qwindow_configuration;
237 if (PROCESSP (object))
238 return Qprocess;
239 if (WINDOWP (object))
240 return Qwindow;
241 if (SUBRP (object))
242 return Qsubr;
243 if (COMPILEDP (object))
244 return Qcompiled_function;
245 if (BUFFERP (object))
246 return Qbuffer;
247 if (CHAR_TABLE_P (object))
248 return Qchar_table;
249 if (BOOL_VECTOR_P (object))
250 return Qbool_vector;
251 if (FRAMEP (object))
252 return Qframe;
253 if (HASH_TABLE_P (object))
254 return Qhash_table;
255 if (FONT_SPEC_P (object))
256 return Qfont_spec;
257 if (FONT_ENTITY_P (object))
258 return Qfont_entity;
259 if (FONT_OBJECT_P (object))
260 return Qfont_object;
261 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: It has been previously suggested to make this function an
633 alias for symbol-function, but upon discussion at Bug#23957,
634 there is a risk breaking backward compatibility, as some users of
635 fboundp may expect `t' in particular, rather than any true
636 value. An alias is still welcome so long as the compatibility
637 issues are addressed. */
638 DEFUN ("fboundp", Ffboundp, Sfboundp, 1, 1, 0,
639 doc: /* Return t if SYMBOL's function definition is not void. */)
640 (register Lisp_Object symbol)
642 CHECK_SYMBOL (symbol);
643 return NILP (XSYMBOL (symbol)->function) ? Qnil : Qt;
646 DEFUN ("makunbound", Fmakunbound, Smakunbound, 1, 1, 0,
647 doc: /* Make SYMBOL's value be void.
648 Return SYMBOL. */)
649 (register Lisp_Object symbol)
651 CHECK_SYMBOL (symbol);
652 if (SYMBOL_CONSTANT_P (symbol))
653 xsignal1 (Qsetting_constant, symbol);
654 Fset (symbol, Qunbound);
655 return symbol;
658 DEFUN ("fmakunbound", Ffmakunbound, Sfmakunbound, 1, 1, 0,
659 doc: /* Make SYMBOL's function definition be nil.
660 Return SYMBOL. */)
661 (register Lisp_Object symbol)
663 CHECK_SYMBOL (symbol);
664 if (NILP (symbol) || EQ (symbol, Qt))
665 xsignal1 (Qsetting_constant, symbol);
666 set_symbol_function (symbol, Qnil);
667 return symbol;
670 DEFUN ("symbol-function", Fsymbol_function, Ssymbol_function, 1, 1, 0,
671 doc: /* Return SYMBOL's function definition, or nil if that is void. */)
672 (register Lisp_Object symbol)
674 CHECK_SYMBOL (symbol);
675 return XSYMBOL (symbol)->function;
678 DEFUN ("symbol-plist", Fsymbol_plist, Ssymbol_plist, 1, 1, 0,
679 doc: /* Return SYMBOL's property list. */)
680 (register Lisp_Object symbol)
682 CHECK_SYMBOL (symbol);
683 return XSYMBOL (symbol)->plist;
686 DEFUN ("symbol-name", Fsymbol_name, Ssymbol_name, 1, 1, 0,
687 doc: /* Return SYMBOL's name, a string. */)
688 (register Lisp_Object symbol)
690 register Lisp_Object name;
692 CHECK_SYMBOL (symbol);
693 name = SYMBOL_NAME (symbol);
694 return name;
697 DEFUN ("fset", Ffset, Sfset, 2, 2, 0,
698 doc: /* Set SYMBOL's function definition to DEFINITION, and return DEFINITION. */)
699 (register Lisp_Object symbol, Lisp_Object definition)
701 register Lisp_Object function;
702 CHECK_SYMBOL (symbol);
704 function = XSYMBOL (symbol)->function;
706 if (!NILP (Vautoload_queue) && !NILP (function))
707 Vautoload_queue = Fcons (Fcons (symbol, function), Vautoload_queue);
709 if (AUTOLOADP (function))
710 Fput (symbol, Qautoload, XCDR (function));
712 /* Convert to eassert or remove after GC bug is found. In the
713 meantime, check unconditionally, at a slight perf hit. */
714 if (! valid_lisp_object_p (definition))
715 emacs_abort ();
717 set_symbol_function (symbol, definition);
719 return definition;
722 DEFUN ("defalias", Fdefalias, Sdefalias, 2, 3, 0,
723 doc: /* Set SYMBOL's function definition to DEFINITION.
724 Associates the function with the current load file, if any.
725 The optional third argument DOCSTRING specifies the documentation string
726 for SYMBOL; if it is omitted or nil, SYMBOL uses the documentation string
727 determined by DEFINITION.
729 Internally, this normally uses `fset', but if SYMBOL has a
730 `defalias-fset-function' property, the associated value is used instead.
732 The return value is undefined. */)
733 (register Lisp_Object symbol, Lisp_Object definition, Lisp_Object docstring)
735 CHECK_SYMBOL (symbol);
736 if (!NILP (Vpurify_flag)
737 /* If `definition' is a keymap, immutable (and copying) is wrong. */
738 && !KEYMAPP (definition))
739 definition = Fpurecopy (definition);
742 bool autoload = AUTOLOADP (definition);
743 if (NILP (Vpurify_flag) || !autoload)
744 { /* Only add autoload entries after dumping, because the ones before are
745 not useful and else we get loads of them from the loaddefs.el. */
747 if (AUTOLOADP (XSYMBOL (symbol)->function))
748 /* Remember that the function was already an autoload. */
749 LOADHIST_ATTACH (Fcons (Qt, symbol));
750 LOADHIST_ATTACH (Fcons (autoload ? Qautoload : Qdefun, symbol));
754 { /* Handle automatic advice activation. */
755 Lisp_Object hook = Fget (symbol, Qdefalias_fset_function);
756 if (!NILP (hook))
757 call2 (hook, symbol, definition);
758 else
759 Ffset (symbol, definition);
762 if (!NILP (docstring))
763 Fput (symbol, Qfunction_documentation, docstring);
764 /* We used to return `definition', but now that `defun' and `defmacro' expand
765 to a call to `defalias', we return `symbol' for backward compatibility
766 (bug#11686). */
767 return symbol;
770 DEFUN ("setplist", Fsetplist, Ssetplist, 2, 2, 0,
771 doc: /* Set SYMBOL's property list to NEWPLIST, and return NEWPLIST. */)
772 (register Lisp_Object symbol, Lisp_Object newplist)
774 CHECK_SYMBOL (symbol);
775 set_symbol_plist (symbol, newplist);
776 return newplist;
779 DEFUN ("subr-arity", Fsubr_arity, Ssubr_arity, 1, 1, 0,
780 doc: /* Return minimum and maximum number of args allowed for SUBR.
781 SUBR must be a built-in function.
782 The returned value is a pair (MIN . MAX). MIN is the minimum number
783 of args. MAX is the maximum number or the symbol `many', for a
784 function with `&rest' args, or `unevalled' for a special form. */)
785 (Lisp_Object subr)
787 short minargs, maxargs;
788 CHECK_SUBR (subr);
789 minargs = XSUBR (subr)->min_args;
790 maxargs = XSUBR (subr)->max_args;
791 return Fcons (make_number (minargs),
792 maxargs == MANY ? Qmany
793 : maxargs == UNEVALLED ? Qunevalled
794 : make_number (maxargs));
797 DEFUN ("subr-name", Fsubr_name, Ssubr_name, 1, 1, 0,
798 doc: /* Return name of subroutine SUBR.
799 SUBR must be a built-in function. */)
800 (Lisp_Object subr)
802 const char *name;
803 CHECK_SUBR (subr);
804 name = XSUBR (subr)->symbol_name;
805 return build_string (name);
808 DEFUN ("interactive-form", Finteractive_form, Sinteractive_form, 1, 1, 0,
809 doc: /* Return the interactive form of CMD or nil if none.
810 If CMD is not a command, the return value is nil.
811 Value, if non-nil, is a list (interactive SPEC). */)
812 (Lisp_Object cmd)
814 Lisp_Object fun = indirect_function (cmd); /* Check cycles. */
816 if (NILP (fun))
817 return Qnil;
819 /* Use an `interactive-form' property if present, analogous to the
820 function-documentation property. */
821 fun = cmd;
822 while (SYMBOLP (fun))
824 Lisp_Object tmp = Fget (fun, Qinteractive_form);
825 if (!NILP (tmp))
826 return tmp;
827 else
828 fun = Fsymbol_function (fun);
831 if (SUBRP (fun))
833 const char *spec = XSUBR (fun)->intspec;
834 if (spec)
835 return list2 (Qinteractive,
836 (*spec != '(') ? build_string (spec) :
837 Fcar (Fread_from_string (build_string (spec), Qnil, Qnil)));
839 else if (COMPILEDP (fun))
841 if ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE)
842 return list2 (Qinteractive, AREF (fun, COMPILED_INTERACTIVE));
844 else if (AUTOLOADP (fun))
845 return Finteractive_form (Fautoload_do_load (fun, cmd, Qnil));
846 else if (CONSP (fun))
848 Lisp_Object funcar = XCAR (fun);
849 if (EQ (funcar, Qclosure))
850 return Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun))));
851 else if (EQ (funcar, Qlambda))
852 return Fassq (Qinteractive, Fcdr (XCDR (fun)));
854 return Qnil;
858 /***********************************************************************
859 Getting and Setting Values of Symbols
860 ***********************************************************************/
862 /* Return the symbol holding SYMBOL's value. Signal
863 `cyclic-variable-indirection' if SYMBOL's chain of variable
864 indirections contains a loop. */
866 struct Lisp_Symbol *
867 indirect_variable (struct Lisp_Symbol *symbol)
869 struct Lisp_Symbol *tortoise, *hare;
871 hare = tortoise = symbol;
873 while (hare->redirect == SYMBOL_VARALIAS)
875 hare = SYMBOL_ALIAS (hare);
876 if (hare->redirect != SYMBOL_VARALIAS)
877 break;
879 hare = SYMBOL_ALIAS (hare);
880 tortoise = SYMBOL_ALIAS (tortoise);
882 if (hare == tortoise)
884 Lisp_Object tem;
885 XSETSYMBOL (tem, symbol);
886 xsignal1 (Qcyclic_variable_indirection, tem);
890 return hare;
894 DEFUN ("indirect-variable", Findirect_variable, Sindirect_variable, 1, 1, 0,
895 doc: /* Return the variable at the end of OBJECT's variable chain.
896 If OBJECT is a symbol, follow its variable indirections (if any), and
897 return the variable at the end of the chain of aliases. See Info node
898 `(elisp)Variable Aliases'.
900 If OBJECT is not a symbol, just return it. If there is a loop in the
901 chain of aliases, signal a `cyclic-variable-indirection' error. */)
902 (Lisp_Object object)
904 if (SYMBOLP (object))
906 struct Lisp_Symbol *sym = indirect_variable (XSYMBOL (object));
907 XSETSYMBOL (object, sym);
909 return object;
913 /* Given the raw contents of a symbol value cell,
914 return the Lisp value of the symbol.
915 This does not handle buffer-local variables; use
916 swap_in_symval_forwarding for that. */
918 Lisp_Object
919 do_symval_forwarding (register union Lisp_Fwd *valcontents)
921 register Lisp_Object val;
922 switch (XFWDTYPE (valcontents))
924 case Lisp_Fwd_Int:
925 XSETINT (val, *XINTFWD (valcontents)->intvar);
926 return val;
928 case Lisp_Fwd_Bool:
929 return (*XBOOLFWD (valcontents)->boolvar ? Qt : Qnil);
931 case Lisp_Fwd_Obj:
932 return *XOBJFWD (valcontents)->objvar;
934 case Lisp_Fwd_Buffer_Obj:
935 return per_buffer_value (current_buffer,
936 XBUFFER_OBJFWD (valcontents)->offset);
938 case Lisp_Fwd_Kboard_Obj:
939 /* We used to simply use current_kboard here, but from Lisp
940 code, its value is often unexpected. It seems nicer to
941 allow constructions like this to work as intuitively expected:
943 (with-selected-frame frame
944 (define-key local-function-map "\eOP" [f1]))
946 On the other hand, this affects the semantics of
947 last-command and real-last-command, and people may rely on
948 that. I took a quick look at the Lisp codebase, and I
949 don't think anything will break. --lorentey */
950 return *(Lisp_Object *)(XKBOARD_OBJFWD (valcontents)->offset
951 + (char *)FRAME_KBOARD (SELECTED_FRAME ()));
952 default: emacs_abort ();
956 /* Used to signal a user-friendly error when symbol WRONG is
957 not a member of CHOICE, which should be a list of symbols. */
959 void
960 wrong_choice (Lisp_Object choice, Lisp_Object wrong)
962 ptrdiff_t i = 0, len = XINT (Flength (choice));
963 Lisp_Object obj, *args;
964 AUTO_STRING (one_of, "One of ");
965 AUTO_STRING (comma, ", ");
966 AUTO_STRING (or, " or ");
967 AUTO_STRING (should_be_specified, " should be specified");
969 USE_SAFE_ALLOCA;
970 SAFE_ALLOCA_LISP (args, len * 2 + 1);
972 args[i++] = one_of;
974 for (obj = choice; !NILP (obj); obj = XCDR (obj))
976 args[i++] = SYMBOL_NAME (XCAR (obj));
977 args[i++] = (NILP (XCDR (obj)) ? should_be_specified
978 : NILP (XCDR (XCDR (obj))) ? or : comma);
981 obj = Fconcat (i, args);
982 SAFE_FREE ();
983 xsignal2 (Qerror, obj, wrong);
986 /* Used to signal a user-friendly error if WRONG is not a number or
987 integer/floating-point number outsize of inclusive MIN..MAX range. */
989 static void
990 wrong_range (Lisp_Object min, Lisp_Object max, Lisp_Object wrong)
992 AUTO_STRING (value_should_be_from, "Value should be from ");
993 AUTO_STRING (to, " to ");
994 xsignal2 (Qerror,
995 CALLN (Fconcat, value_should_be_from, Fnumber_to_string (min),
996 to, Fnumber_to_string (max)),
997 wrong);
1000 /* Store NEWVAL into SYMBOL, where VALCONTENTS is found in the value cell
1001 of SYMBOL. If SYMBOL is buffer-local, VALCONTENTS should be the
1002 buffer-independent contents of the value cell: forwarded just one
1003 step past the buffer-localness.
1005 BUF non-zero means set the value in buffer BUF instead of the
1006 current buffer. This only plays a role for per-buffer variables. */
1008 static void
1009 store_symval_forwarding (union Lisp_Fwd *valcontents, register Lisp_Object newval, struct buffer *buf)
1011 switch (XFWDTYPE (valcontents))
1013 case Lisp_Fwd_Int:
1014 CHECK_NUMBER (newval);
1015 *XINTFWD (valcontents)->intvar = XINT (newval);
1016 break;
1018 case Lisp_Fwd_Bool:
1019 *XBOOLFWD (valcontents)->boolvar = !NILP (newval);
1020 break;
1022 case Lisp_Fwd_Obj:
1023 *XOBJFWD (valcontents)->objvar = newval;
1025 /* If this variable is a default for something stored
1026 in the buffer itself, such as default-fill-column,
1027 find the buffers that don't have local values for it
1028 and update them. */
1029 if (XOBJFWD (valcontents)->objvar > (Lisp_Object *) &buffer_defaults
1030 && XOBJFWD (valcontents)->objvar < (Lisp_Object *) (&buffer_defaults + 1))
1032 int offset = ((char *) XOBJFWD (valcontents)->objvar
1033 - (char *) &buffer_defaults);
1034 int idx = PER_BUFFER_IDX (offset);
1036 Lisp_Object tail, buf;
1038 if (idx <= 0)
1039 break;
1041 FOR_EACH_LIVE_BUFFER (tail, buf)
1043 struct buffer *b = XBUFFER (buf);
1045 if (! PER_BUFFER_VALUE_P (b, idx))
1046 set_per_buffer_value (b, offset, newval);
1049 break;
1051 case Lisp_Fwd_Buffer_Obj:
1053 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1054 Lisp_Object predicate = XBUFFER_OBJFWD (valcontents)->predicate;
1056 if (!NILP (newval))
1058 if (SYMBOLP (predicate))
1060 Lisp_Object prop;
1062 if ((prop = Fget (predicate, Qchoice), !NILP (prop)))
1064 if (NILP (Fmemq (newval, prop)))
1065 wrong_choice (prop, newval);
1067 else if ((prop = Fget (predicate, Qrange), !NILP (prop)))
1069 Lisp_Object min = XCAR (prop), max = XCDR (prop);
1071 if (!NUMBERP (newval)
1072 || !NILP (arithcompare (newval, min, ARITH_LESS))
1073 || !NILP (arithcompare (newval, max, ARITH_GRTR)))
1074 wrong_range (min, max, newval);
1076 else if (FUNCTIONP (predicate))
1078 if (NILP (call1 (predicate, newval)))
1079 wrong_type_argument (predicate, newval);
1083 if (buf == NULL)
1084 buf = current_buffer;
1085 set_per_buffer_value (buf, offset, newval);
1087 break;
1089 case Lisp_Fwd_Kboard_Obj:
1091 char *base = (char *) FRAME_KBOARD (SELECTED_FRAME ());
1092 char *p = base + XKBOARD_OBJFWD (valcontents)->offset;
1093 *(Lisp_Object *) p = newval;
1095 break;
1097 default:
1098 emacs_abort (); /* goto def; */
1102 /* Set up SYMBOL to refer to its global binding. This makes it safe
1103 to alter the status of other bindings. BEWARE: this may be called
1104 during the mark phase of GC, where we assume that Lisp_Object slots
1105 of BLV are marked after this function has changed them. */
1107 void
1108 swap_in_global_binding (struct Lisp_Symbol *symbol)
1110 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (symbol);
1112 /* Unload the previously loaded binding. */
1113 if (blv->fwd)
1114 set_blv_value (blv, do_symval_forwarding (blv->fwd));
1116 /* Select the global binding in the symbol. */
1117 set_blv_valcell (blv, blv->defcell);
1118 if (blv->fwd)
1119 store_symval_forwarding (blv->fwd, XCDR (blv->defcell), NULL);
1121 /* Indicate that the global binding is set up now. */
1122 set_blv_where (blv, Qnil);
1123 set_blv_found (blv, 0);
1126 /* Set up the buffer-local symbol SYMBOL for validity in the current buffer.
1127 VALCONTENTS is the contents of its value cell,
1128 which points to a struct Lisp_Buffer_Local_Value.
1130 Return the value forwarded one step past the buffer-local stage.
1131 This could be another forwarding pointer. */
1133 static void
1134 swap_in_symval_forwarding (struct Lisp_Symbol *symbol, struct Lisp_Buffer_Local_Value *blv)
1136 register Lisp_Object tem1;
1138 eassert (blv == SYMBOL_BLV (symbol));
1140 tem1 = blv->where;
1142 if (NILP (tem1)
1143 || (blv->frame_local
1144 ? !EQ (selected_frame, tem1)
1145 : current_buffer != XBUFFER (tem1)))
1148 /* Unload the previously loaded binding. */
1149 tem1 = blv->valcell;
1150 if (blv->fwd)
1151 set_blv_value (blv, do_symval_forwarding (blv->fwd));
1152 /* Choose the new binding. */
1154 Lisp_Object var;
1155 XSETSYMBOL (var, symbol);
1156 if (blv->frame_local)
1158 tem1 = assq_no_quit (var, XFRAME (selected_frame)->param_alist);
1159 set_blv_where (blv, selected_frame);
1161 else
1163 tem1 = assq_no_quit (var, BVAR (current_buffer, local_var_alist));
1164 set_blv_where (blv, Fcurrent_buffer ());
1167 if (!(blv->found = !NILP (tem1)))
1168 tem1 = blv->defcell;
1170 /* Load the new binding. */
1171 set_blv_valcell (blv, tem1);
1172 if (blv->fwd)
1173 store_symval_forwarding (blv->fwd, blv_value (blv), NULL);
1177 /* Find the value of a symbol, returning Qunbound if it's not bound.
1178 This is helpful for code which just wants to get a variable's value
1179 if it has one, without signaling an error.
1180 Note that it must not be possible to quit
1181 within this function. Great care is required for this. */
1183 Lisp_Object
1184 find_symbol_value (Lisp_Object symbol)
1186 struct Lisp_Symbol *sym;
1188 CHECK_SYMBOL (symbol);
1189 sym = XSYMBOL (symbol);
1191 start:
1192 switch (sym->redirect)
1194 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1195 case SYMBOL_PLAINVAL: return SYMBOL_VAL (sym);
1196 case SYMBOL_LOCALIZED:
1198 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1199 swap_in_symval_forwarding (sym, blv);
1200 return blv->fwd ? do_symval_forwarding (blv->fwd) : blv_value (blv);
1202 /* FALLTHROUGH */
1203 case SYMBOL_FORWARDED:
1204 return do_symval_forwarding (SYMBOL_FWD (sym));
1205 default: emacs_abort ();
1209 DEFUN ("symbol-value", Fsymbol_value, Ssymbol_value, 1, 1, 0,
1210 doc: /* Return SYMBOL's value. Error if that is void.
1211 Note that if `lexical-binding' is in effect, this returns the
1212 global value outside of any lexical scope. */)
1213 (Lisp_Object symbol)
1215 Lisp_Object val;
1217 val = find_symbol_value (symbol);
1218 if (!EQ (val, Qunbound))
1219 return val;
1221 xsignal1 (Qvoid_variable, symbol);
1224 DEFUN ("set", Fset, Sset, 2, 2, 0,
1225 doc: /* Set SYMBOL's value to NEWVAL, and return NEWVAL. */)
1226 (register Lisp_Object symbol, Lisp_Object newval)
1228 set_internal (symbol, newval, Qnil, SET_INTERNAL_SET);
1229 return newval;
1232 /* Store the value NEWVAL into SYMBOL.
1233 If buffer/frame-locality is an issue, WHERE specifies which context to use.
1234 (nil stands for the current buffer/frame).
1236 If BINDFLAG is SET_INTERNAL_SET, then if this symbol is supposed to
1237 become local in every buffer where it is set, then we make it
1238 local. If BINDFLAG is SET_INTERNAL_BIND or SET_INTERNAL_UNBIND, we
1239 don't do that. */
1241 void
1242 set_internal (Lisp_Object symbol, Lisp_Object newval, Lisp_Object where,
1243 enum Set_Internal_Bind bindflag)
1245 bool voide = EQ (newval, Qunbound);
1246 struct Lisp_Symbol *sym;
1247 Lisp_Object tem1;
1249 /* If restoring in a dead buffer, do nothing. */
1250 /* if (BUFFERP (where) && NILP (XBUFFER (where)->name))
1251 return; */
1253 CHECK_SYMBOL (symbol);
1254 sym = XSYMBOL (symbol);
1255 switch (sym->trapped_write)
1257 case SYMBOL_NOWRITE:
1258 if (NILP (Fkeywordp (symbol))
1259 || !EQ (newval, Fsymbol_value (symbol)))
1260 xsignal1 (Qsetting_constant, symbol);
1261 else
1262 /* Allow setting keywords to their own value. */
1263 return;
1265 case SYMBOL_TRAPPED_WRITE:
1266 notify_variable_watchers (symbol, voide? Qnil : newval,
1267 (bindflag == SET_INTERNAL_BIND? Qlet :
1268 bindflag == SET_INTERNAL_UNBIND? Qunlet :
1269 voide? Qmakunbound : Qset),
1270 where);
1271 /* FALLTHROUGH! */
1272 case SYMBOL_UNTRAPPED_WRITE:
1273 break;
1275 default: emacs_abort ();
1278 start:
1279 switch (sym->redirect)
1281 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1282 case SYMBOL_PLAINVAL: SET_SYMBOL_VAL (sym , newval); return;
1283 case SYMBOL_LOCALIZED:
1285 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1286 if (NILP (where))
1288 if (blv->frame_local)
1289 where = selected_frame;
1290 else
1291 XSETBUFFER (where, current_buffer);
1293 /* If the current buffer is not the buffer whose binding is
1294 loaded, or if there may be frame-local bindings and the frame
1295 isn't the right one, or if it's a Lisp_Buffer_Local_Value and
1296 the default binding is loaded, the loaded binding may be the
1297 wrong one. */
1298 if (!EQ (blv->where, where)
1299 /* Also unload a global binding (if the var is local_if_set). */
1300 || (EQ (blv->valcell, blv->defcell)))
1302 /* The currently loaded binding is not necessarily valid.
1303 We need to unload it, and choose a new binding. */
1305 /* Write out `realvalue' to the old loaded binding. */
1306 if (blv->fwd)
1307 set_blv_value (blv, do_symval_forwarding (blv->fwd));
1309 /* Find the new binding. */
1310 XSETSYMBOL (symbol, sym); /* May have changed via aliasing. */
1311 tem1 = assq_no_quit (symbol,
1312 (blv->frame_local
1313 ? XFRAME (where)->param_alist
1314 : BVAR (XBUFFER (where), local_var_alist)));
1315 set_blv_where (blv, where);
1316 blv->found = 1;
1318 if (NILP (tem1))
1320 /* This buffer still sees the default value. */
1322 /* If the variable is a Lisp_Some_Buffer_Local_Value,
1323 or if this is `let' rather than `set',
1324 make CURRENT-ALIST-ELEMENT point to itself,
1325 indicating that we're seeing the default value.
1326 Likewise if the variable has been let-bound
1327 in the current buffer. */
1328 if (bindflag || !blv->local_if_set
1329 || let_shadows_buffer_binding_p (sym))
1331 blv->found = 0;
1332 tem1 = blv->defcell;
1334 /* If it's a local_if_set, being set not bound,
1335 and we're not within a let that was made for this buffer,
1336 create a new buffer-local binding for the variable.
1337 That means, give this buffer a new assoc for a local value
1338 and load that binding. */
1339 else
1341 /* local_if_set is only supported for buffer-local
1342 bindings, not for frame-local bindings. */
1343 eassert (!blv->frame_local);
1344 tem1 = Fcons (symbol, XCDR (blv->defcell));
1345 bset_local_var_alist
1346 (XBUFFER (where),
1347 Fcons (tem1, BVAR (XBUFFER (where), local_var_alist)));
1351 /* Record which binding is now loaded. */
1352 set_blv_valcell (blv, tem1);
1355 /* Store the new value in the cons cell. */
1356 set_blv_value (blv, newval);
1358 if (blv->fwd)
1360 if (voide)
1361 /* If storing void (making the symbol void), forward only through
1362 buffer-local indicator, not through Lisp_Objfwd, etc. */
1363 blv->fwd = NULL;
1364 else
1365 store_symval_forwarding (blv->fwd, newval,
1366 BUFFERP (where)
1367 ? XBUFFER (where) : current_buffer);
1369 break;
1371 case SYMBOL_FORWARDED:
1373 struct buffer *buf
1374 = BUFFERP (where) ? XBUFFER (where) : current_buffer;
1375 union Lisp_Fwd *innercontents = SYMBOL_FWD (sym);
1376 if (BUFFER_OBJFWDP (innercontents))
1378 int offset = XBUFFER_OBJFWD (innercontents)->offset;
1379 int idx = PER_BUFFER_IDX (offset);
1380 if (idx > 0
1381 && !bindflag
1382 && !let_shadows_buffer_binding_p (sym))
1383 SET_PER_BUFFER_VALUE_P (buf, idx, 1);
1386 if (voide)
1387 { /* If storing void (making the symbol void), forward only through
1388 buffer-local indicator, not through Lisp_Objfwd, etc. */
1389 sym->redirect = SYMBOL_PLAINVAL;
1390 SET_SYMBOL_VAL (sym, newval);
1392 else
1393 store_symval_forwarding (/* sym, */ innercontents, newval, buf);
1394 break;
1396 default: emacs_abort ();
1398 return;
1401 static void
1402 set_symbol_trapped_write (Lisp_Object symbol, enum symbol_trapped_write trap)
1404 struct Lisp_Symbol* sym = XSYMBOL (symbol);
1405 if (sym->trapped_write == SYMBOL_NOWRITE)
1406 xsignal1 (Qtrapping_constant, symbol);
1407 else if (sym->redirect == SYMBOL_LOCALIZED
1408 && SYMBOL_BLV (sym)->frame_local)
1409 xsignal1 (Qtrapping_frame_local, symbol);
1410 sym->trapped_write = trap;
1413 static void
1414 restore_symbol_trapped_write (Lisp_Object symbol)
1416 set_symbol_trapped_write (symbol, SYMBOL_TRAPPED_WRITE);
1419 static void
1420 harmonize_variable_watchers (Lisp_Object alias, Lisp_Object base_variable)
1422 if (!EQ (base_variable, alias)
1423 && EQ (base_variable, Findirect_variable (alias)))
1424 set_symbol_trapped_write
1425 (alias, XSYMBOL (base_variable)->trapped_write);
1428 DEFUN ("add-variable-watcher", Fadd_variable_watcher, Sadd_variable_watcher,
1429 2, 2, 0,
1430 doc: /* Cause WATCH-FUNCTION to be called when SYMBOL is set.
1432 It will be called with 4 arguments: (SYMBOL NEWVAL OPERATION WHERE).
1433 SYMBOL is the variable being changed.
1434 NEWVAL is the value it will be changed to.
1435 OPERATION is a symbol representing the kind of change, one of: `set',
1436 `let', `unlet', `makunbound', and `defvaralias'.
1437 WHERE is a buffer if the buffer-local value of the variable being
1438 changed, nil otherwise.
1440 All writes to aliases of SYMBOL will call WATCH-FUNCTION too. */)
1441 (Lisp_Object symbol, Lisp_Object watch_function)
1443 symbol = Findirect_variable (symbol);
1444 set_symbol_trapped_write (symbol, SYMBOL_TRAPPED_WRITE);
1445 map_obarray (Vobarray, harmonize_variable_watchers, symbol);
1447 Lisp_Object watchers = Fget (symbol, Qwatchers);
1448 Lisp_Object member = Fmember (watch_function, watchers);
1449 if (NILP (member))
1450 Fput (symbol, Qwatchers, Fcons (watch_function, watchers));
1451 return Qnil;
1454 DEFUN ("remove-variable-watcher", Fremove_variable_watcher, Sremove_variable_watcher,
1455 2, 2, 0,
1456 doc: /* Undo the effect of `add-variable-watcher'.
1457 Remove WATCH-FUNCTION from the list of functions to be called when
1458 SYMBOL (or its aliases) are set. */)
1459 (Lisp_Object symbol, Lisp_Object watch_function)
1461 symbol = Findirect_variable (symbol);
1462 Lisp_Object watchers = Fget (symbol, Qwatchers);
1463 watchers = Fdelete (watch_function, watchers);
1464 if (NILP (watchers))
1466 set_symbol_trapped_write (symbol, SYMBOL_UNTRAPPED_WRITE);
1467 map_obarray (Vobarray, harmonize_variable_watchers, symbol);
1469 Fput (symbol, Qwatchers, watchers);
1470 return Qnil;
1473 DEFUN ("get-variable-watchers", Fget_variable_watchers, Sget_variable_watchers,
1474 1, 1, 0,
1475 doc: /* Return a list of SYMBOL's active watchers. */)
1476 (Lisp_Object symbol)
1478 return (SYMBOL_TRAPPED_WRITE_P (symbol) == SYMBOL_TRAPPED_WRITE)
1479 ? Fget (Findirect_variable (symbol), Qwatchers)
1480 : Qnil;
1483 void
1484 notify_variable_watchers (Lisp_Object symbol,
1485 Lisp_Object newval,
1486 Lisp_Object operation,
1487 Lisp_Object where)
1489 symbol = Findirect_variable (symbol);
1491 ptrdiff_t count = SPECPDL_INDEX ();
1492 record_unwind_protect (restore_symbol_trapped_write, symbol);
1493 /* Avoid recursion. */
1494 set_symbol_trapped_write (symbol, SYMBOL_UNTRAPPED_WRITE);
1496 if (NILP (where)
1497 && !EQ (operation, Qset_default) && !EQ (operation, Qmakunbound)
1498 && !NILP (Flocal_variable_if_set_p (symbol, Fcurrent_buffer ())))
1500 XSETBUFFER (where, current_buffer);
1503 if (EQ (operation, Qset_default))
1504 operation = Qset;
1506 for (Lisp_Object watchers = Fget (symbol, Qwatchers);
1507 CONSP (watchers);
1508 watchers = XCDR (watchers))
1510 Lisp_Object watcher = XCAR (watchers);
1511 /* Call subr directly to avoid gc. */
1512 if (SUBRP (watcher))
1514 Lisp_Object args[] = { symbol, newval, operation, where };
1515 funcall_subr (XSUBR (watcher), ARRAYELTS (args), args);
1517 else
1518 CALLN (Ffuncall, watcher, symbol, newval, operation, where);
1521 unbind_to (count, Qnil);
1525 /* Access or set a buffer-local symbol's default value. */
1527 /* Return the default value of SYMBOL, but don't check for voidness.
1528 Return Qunbound if it is void. */
1530 static Lisp_Object
1531 default_value (Lisp_Object symbol)
1533 struct Lisp_Symbol *sym;
1535 CHECK_SYMBOL (symbol);
1536 sym = XSYMBOL (symbol);
1538 start:
1539 switch (sym->redirect)
1541 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1542 case SYMBOL_PLAINVAL: return SYMBOL_VAL (sym);
1543 case SYMBOL_LOCALIZED:
1545 /* If var is set up for a buffer that lacks a local value for it,
1546 the current value is nominally the default value.
1547 But the `realvalue' slot may be more up to date, since
1548 ordinary setq stores just that slot. So use that. */
1549 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1550 if (blv->fwd && EQ (blv->valcell, blv->defcell))
1551 return do_symval_forwarding (blv->fwd);
1552 else
1553 return XCDR (blv->defcell);
1555 case SYMBOL_FORWARDED:
1557 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
1559 /* For a built-in buffer-local variable, get the default value
1560 rather than letting do_symval_forwarding get the current value. */
1561 if (BUFFER_OBJFWDP (valcontents))
1563 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1564 if (PER_BUFFER_IDX (offset) != 0)
1565 return per_buffer_default (offset);
1568 /* For other variables, get the current value. */
1569 return do_symval_forwarding (valcontents);
1571 default: emacs_abort ();
1575 DEFUN ("default-boundp", Fdefault_boundp, Sdefault_boundp, 1, 1, 0,
1576 doc: /* Return t if SYMBOL has a non-void default value.
1577 This is the value that is seen in buffers that do not have their own values
1578 for this variable. */)
1579 (Lisp_Object symbol)
1581 register Lisp_Object value;
1583 value = default_value (symbol);
1584 return (EQ (value, Qunbound) ? Qnil : Qt);
1587 DEFUN ("default-value", Fdefault_value, Sdefault_value, 1, 1, 0,
1588 doc: /* Return SYMBOL's default value.
1589 This is the value that is seen in buffers that do not have their own values
1590 for this variable. The default value is meaningful for variables with
1591 local bindings in certain buffers. */)
1592 (Lisp_Object symbol)
1594 Lisp_Object value = default_value (symbol);
1595 if (!EQ (value, Qunbound))
1596 return value;
1598 xsignal1 (Qvoid_variable, symbol);
1601 DEFUN ("set-default", Fset_default, Sset_default, 2, 2, 0,
1602 doc: /* Set SYMBOL's default value to VALUE. SYMBOL and VALUE are evaluated.
1603 The default value is seen in buffers that do not have their own values
1604 for this variable. */)
1605 (Lisp_Object symbol, Lisp_Object value)
1607 struct Lisp_Symbol *sym;
1609 CHECK_SYMBOL (symbol);
1610 sym = XSYMBOL (symbol);
1611 switch (sym->trapped_write)
1613 case SYMBOL_NOWRITE:
1614 if (NILP (Fkeywordp (symbol))
1615 || !EQ (value, Fsymbol_value (symbol)))
1616 xsignal1 (Qsetting_constant, symbol);
1617 else
1618 /* Allow setting keywords to their own value. */
1619 return value;
1621 case SYMBOL_TRAPPED_WRITE:
1622 /* Don't notify here if we're going to call Fset anyway. */
1623 if (sym->redirect != SYMBOL_PLAINVAL)
1624 notify_variable_watchers (symbol, value, Qset_default, Qnil);
1625 /* FALLTHROUGH! */
1626 case SYMBOL_UNTRAPPED_WRITE:
1627 break;
1629 default: emacs_abort ();
1632 start:
1633 switch (sym->redirect)
1635 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1636 case SYMBOL_PLAINVAL: return Fset (symbol, value);
1637 case SYMBOL_LOCALIZED:
1639 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1641 /* Store new value into the DEFAULT-VALUE slot. */
1642 XSETCDR (blv->defcell, value);
1644 /* If the default binding is now loaded, set the REALVALUE slot too. */
1645 if (blv->fwd && EQ (blv->defcell, blv->valcell))
1646 store_symval_forwarding (blv->fwd, value, NULL);
1647 return value;
1649 case SYMBOL_FORWARDED:
1651 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
1653 /* Handle variables like case-fold-search that have special slots
1654 in the buffer.
1655 Make them work apparently like Lisp_Buffer_Local_Value variables. */
1656 if (BUFFER_OBJFWDP (valcontents))
1658 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1659 int idx = PER_BUFFER_IDX (offset);
1661 set_per_buffer_default (offset, value);
1663 /* If this variable is not always local in all buffers,
1664 set it in the buffers that don't nominally have a local value. */
1665 if (idx > 0)
1667 struct buffer *b;
1669 FOR_EACH_BUFFER (b)
1670 if (!PER_BUFFER_VALUE_P (b, idx))
1671 set_per_buffer_value (b, offset, value);
1673 return value;
1675 else
1676 return Fset (symbol, value);
1678 default: emacs_abort ();
1682 DEFUN ("setq-default", Fsetq_default, Ssetq_default, 0, UNEVALLED, 0,
1683 doc: /* Set the default value of variable VAR to VALUE.
1684 VAR, the variable name, is literal (not evaluated);
1685 VALUE is an expression: it is evaluated and its value returned.
1686 The default value of a variable is seen in buffers
1687 that do not have their own values for the variable.
1689 More generally, you can use multiple variables and values, as in
1690 (setq-default VAR VALUE VAR VALUE...)
1691 This sets each VAR's default value to the corresponding VALUE.
1692 The VALUE for the Nth VAR can refer to the new default values
1693 of previous VARs.
1694 usage: (setq-default [VAR VALUE]...) */)
1695 (Lisp_Object args)
1697 Lisp_Object args_left, symbol, val;
1699 args_left = val = args;
1701 while (CONSP (args_left))
1703 val = eval_sub (Fcar (XCDR (args_left)));
1704 symbol = XCAR (args_left);
1705 Fset_default (symbol, val);
1706 args_left = Fcdr (XCDR (args_left));
1709 return val;
1712 /* Lisp functions for creating and removing buffer-local variables. */
1714 union Lisp_Val_Fwd
1716 Lisp_Object value;
1717 union Lisp_Fwd *fwd;
1720 static struct Lisp_Buffer_Local_Value *
1721 make_blv (struct Lisp_Symbol *sym, bool forwarded,
1722 union Lisp_Val_Fwd valcontents)
1724 struct Lisp_Buffer_Local_Value *blv = xmalloc (sizeof *blv);
1725 Lisp_Object symbol;
1726 Lisp_Object tem;
1728 XSETSYMBOL (symbol, sym);
1729 tem = Fcons (symbol, (forwarded
1730 ? do_symval_forwarding (valcontents.fwd)
1731 : valcontents.value));
1733 /* Buffer_Local_Values cannot have as realval a buffer-local
1734 or keyboard-local forwarding. */
1735 eassert (!(forwarded && BUFFER_OBJFWDP (valcontents.fwd)));
1736 eassert (!(forwarded && KBOARD_OBJFWDP (valcontents.fwd)));
1737 blv->fwd = forwarded ? valcontents.fwd : NULL;
1738 set_blv_where (blv, Qnil);
1739 blv->frame_local = 0;
1740 blv->local_if_set = 0;
1741 set_blv_defcell (blv, tem);
1742 set_blv_valcell (blv, tem);
1743 set_blv_found (blv, 0);
1744 return blv;
1747 DEFUN ("make-variable-buffer-local", Fmake_variable_buffer_local,
1748 Smake_variable_buffer_local, 1, 1, "vMake Variable Buffer Local: ",
1749 doc: /* Make VARIABLE become buffer-local whenever it is set.
1750 At any time, the value for the current buffer is in effect,
1751 unless the variable has never been set in this buffer,
1752 in which case the default value is in effect.
1753 Note that binding the variable with `let', or setting it while
1754 a `let'-style binding made in this buffer is in effect,
1755 does not make the variable buffer-local. Return VARIABLE.
1757 This globally affects all uses of this variable, so it belongs together with
1758 the variable declaration, rather than with its uses (if you just want to make
1759 a variable local to the current buffer for one particular use, use
1760 `make-local-variable'). Buffer-local bindings are normally cleared
1761 while setting up a new major mode, unless they have a `permanent-local'
1762 property.
1764 The function `default-value' gets the default value and `set-default' sets it. */)
1765 (register Lisp_Object variable)
1767 struct Lisp_Symbol *sym;
1768 struct Lisp_Buffer_Local_Value *blv = NULL;
1769 union Lisp_Val_Fwd valcontents;
1770 bool forwarded;
1772 CHECK_SYMBOL (variable);
1773 sym = XSYMBOL (variable);
1775 start:
1776 switch (sym->redirect)
1778 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1779 case SYMBOL_PLAINVAL:
1780 forwarded = 0; valcontents.value = SYMBOL_VAL (sym);
1781 if (EQ (valcontents.value, Qunbound))
1782 valcontents.value = Qnil;
1783 break;
1784 case SYMBOL_LOCALIZED:
1785 blv = SYMBOL_BLV (sym);
1786 if (blv->frame_local)
1787 error ("Symbol %s may not be buffer-local",
1788 SDATA (SYMBOL_NAME (variable)));
1789 break;
1790 case SYMBOL_FORWARDED:
1791 forwarded = 1; valcontents.fwd = SYMBOL_FWD (sym);
1792 if (KBOARD_OBJFWDP (valcontents.fwd))
1793 error ("Symbol %s may not be buffer-local",
1794 SDATA (SYMBOL_NAME (variable)));
1795 else if (BUFFER_OBJFWDP (valcontents.fwd))
1796 return variable;
1797 break;
1798 default: emacs_abort ();
1801 if (SYMBOL_CONSTANT_P (variable))
1802 error ("Symbol %s may not be buffer-local", SDATA (SYMBOL_NAME (variable)));
1804 if (!blv)
1806 blv = make_blv (sym, forwarded, valcontents);
1807 sym->redirect = SYMBOL_LOCALIZED;
1808 SET_SYMBOL_BLV (sym, blv);
1810 Lisp_Object symbol;
1811 XSETSYMBOL (symbol, sym); /* In case `variable' is aliased. */
1812 if (let_shadows_global_binding_p (symbol))
1814 AUTO_STRING (format, "Making %s buffer-local while let-bound!");
1815 CALLN (Fmessage, format, SYMBOL_NAME (variable));
1820 blv->local_if_set = 1;
1821 return variable;
1824 DEFUN ("make-local-variable", Fmake_local_variable, Smake_local_variable,
1825 1, 1, "vMake Local Variable: ",
1826 doc: /* Make VARIABLE have a separate value in the current buffer.
1827 Other buffers will continue to share a common default value.
1828 \(The buffer-local value of VARIABLE starts out as the same value
1829 VARIABLE previously had. If VARIABLE was void, it remains void.)
1830 Return VARIABLE.
1832 If the variable is already arranged to become local when set,
1833 this function causes a local value to exist for this buffer,
1834 just as setting the variable would do.
1836 This function returns VARIABLE, and therefore
1837 (set (make-local-variable \\='VARIABLE) VALUE-EXP)
1838 works.
1840 See also `make-variable-buffer-local'.
1842 Do not use `make-local-variable' to make a hook variable buffer-local.
1843 Instead, use `add-hook' and specify t for the LOCAL argument. */)
1844 (Lisp_Object variable)
1846 Lisp_Object tem;
1847 bool forwarded;
1848 union Lisp_Val_Fwd valcontents;
1849 struct Lisp_Symbol *sym;
1850 struct Lisp_Buffer_Local_Value *blv = NULL;
1852 CHECK_SYMBOL (variable);
1853 sym = XSYMBOL (variable);
1855 start:
1856 switch (sym->redirect)
1858 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1859 case SYMBOL_PLAINVAL:
1860 forwarded = 0; valcontents.value = SYMBOL_VAL (sym); break;
1861 case SYMBOL_LOCALIZED:
1862 blv = SYMBOL_BLV (sym);
1863 if (blv->frame_local)
1864 error ("Symbol %s may not be buffer-local",
1865 SDATA (SYMBOL_NAME (variable)));
1866 break;
1867 case SYMBOL_FORWARDED:
1868 forwarded = 1; valcontents.fwd = SYMBOL_FWD (sym);
1869 if (KBOARD_OBJFWDP (valcontents.fwd))
1870 error ("Symbol %s may not be buffer-local",
1871 SDATA (SYMBOL_NAME (variable)));
1872 break;
1873 default: emacs_abort ();
1876 if (sym->trapped_write == SYMBOL_NOWRITE)
1877 error ("Symbol %s may not be buffer-local",
1878 SDATA (SYMBOL_NAME (variable)));
1880 if (blv ? blv->local_if_set
1881 : (forwarded && BUFFER_OBJFWDP (valcontents.fwd)))
1883 tem = Fboundp (variable);
1884 /* Make sure the symbol has a local value in this particular buffer,
1885 by setting it to the same value it already has. */
1886 Fset (variable, (EQ (tem, Qt) ? Fsymbol_value (variable) : Qunbound));
1887 return variable;
1889 if (!blv)
1891 blv = make_blv (sym, forwarded, valcontents);
1892 sym->redirect = SYMBOL_LOCALIZED;
1893 SET_SYMBOL_BLV (sym, blv);
1895 Lisp_Object symbol;
1896 XSETSYMBOL (symbol, sym); /* In case `variable' is aliased. */
1897 if (let_shadows_global_binding_p (symbol))
1899 AUTO_STRING (format, "Making %s local to %s while let-bound!");
1900 CALLN (Fmessage, format, SYMBOL_NAME (variable),
1901 BVAR (current_buffer, name));
1906 /* Make sure this buffer has its own value of symbol. */
1907 XSETSYMBOL (variable, sym); /* Update in case of aliasing. */
1908 tem = Fassq (variable, BVAR (current_buffer, local_var_alist));
1909 if (NILP (tem))
1911 if (let_shadows_buffer_binding_p (sym))
1913 AUTO_STRING (format,
1914 "Making %s buffer-local while locally let-bound!");
1915 CALLN (Fmessage, format, SYMBOL_NAME (variable));
1918 /* Swap out any local binding for some other buffer, and make
1919 sure the current value is permanently recorded, if it's the
1920 default value. */
1921 find_symbol_value (variable);
1923 bset_local_var_alist
1924 (current_buffer,
1925 Fcons (Fcons (variable, XCDR (blv->defcell)),
1926 BVAR (current_buffer, local_var_alist)));
1928 /* Make sure symbol does not think it is set up for this buffer;
1929 force it to look once again for this buffer's value. */
1930 if (current_buffer == XBUFFER (blv->where))
1931 set_blv_where (blv, Qnil);
1932 set_blv_found (blv, 0);
1935 /* If the symbol forwards into a C variable, then load the binding
1936 for this buffer now. If C code modifies the variable before we
1937 load the binding in, then that new value will clobber the default
1938 binding the next time we unload it. */
1939 if (blv->fwd)
1940 swap_in_symval_forwarding (sym, blv);
1942 return variable;
1945 DEFUN ("kill-local-variable", Fkill_local_variable, Skill_local_variable,
1946 1, 1, "vKill Local Variable: ",
1947 doc: /* Make VARIABLE no longer have a separate value in the current buffer.
1948 From now on the default value will apply in this buffer. Return VARIABLE. */)
1949 (register Lisp_Object variable)
1951 register Lisp_Object tem;
1952 struct Lisp_Buffer_Local_Value *blv;
1953 struct Lisp_Symbol *sym;
1955 CHECK_SYMBOL (variable);
1956 sym = XSYMBOL (variable);
1958 start:
1959 switch (sym->redirect)
1961 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1962 case SYMBOL_PLAINVAL: return variable;
1963 case SYMBOL_FORWARDED:
1965 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
1966 if (BUFFER_OBJFWDP (valcontents))
1968 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1969 int idx = PER_BUFFER_IDX (offset);
1971 if (idx > 0)
1973 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 0);
1974 set_per_buffer_value (current_buffer, offset,
1975 per_buffer_default (offset));
1978 return variable;
1980 case SYMBOL_LOCALIZED:
1981 blv = SYMBOL_BLV (sym);
1982 if (blv->frame_local)
1983 return variable;
1984 break;
1985 default: emacs_abort ();
1988 if (sym->trapped_write == SYMBOL_TRAPPED_WRITE)
1989 notify_variable_watchers (variable, Qnil, Qmakunbound, Fcurrent_buffer ());
1991 /* Get rid of this buffer's alist element, if any. */
1992 XSETSYMBOL (variable, sym); /* Propagate variable indirection. */
1993 tem = Fassq (variable, BVAR (current_buffer, local_var_alist));
1994 if (!NILP (tem))
1995 bset_local_var_alist
1996 (current_buffer,
1997 Fdelq (tem, BVAR (current_buffer, local_var_alist)));
1999 /* If the symbol is set up with the current buffer's binding
2000 loaded, recompute its value. We have to do it now, or else
2001 forwarded objects won't work right. */
2003 Lisp_Object buf; XSETBUFFER (buf, current_buffer);
2004 if (EQ (buf, blv->where))
2006 set_blv_where (blv, Qnil);
2007 blv->found = 0;
2008 find_symbol_value (variable);
2012 return variable;
2015 /* Lisp functions for creating and removing buffer-local variables. */
2017 /* Obsolete since 22.2. NB adjust doc of modify-frame-parameters
2018 when/if this is removed. */
2020 DEFUN ("make-variable-frame-local", Fmake_variable_frame_local, Smake_variable_frame_local,
2021 1, 1, "vMake Variable Frame Local: ",
2022 doc: /* Enable VARIABLE to have frame-local bindings.
2023 This does not create any frame-local bindings for VARIABLE,
2024 it just makes them possible.
2026 A frame-local binding is actually a frame parameter value.
2027 If a frame F has a value for the frame parameter named VARIABLE,
2028 that also acts as a frame-local binding for VARIABLE in F--
2029 provided this function has been called to enable VARIABLE
2030 to have frame-local bindings at all.
2032 The only way to create a frame-local binding for VARIABLE in a frame
2033 is to set the VARIABLE frame parameter of that frame. See
2034 `modify-frame-parameters' for how to set frame parameters.
2036 Note that since Emacs 23.1, variables cannot be both buffer-local and
2037 frame-local any more (buffer-local bindings used to take precedence over
2038 frame-local bindings). */)
2039 (Lisp_Object variable)
2041 bool forwarded;
2042 union Lisp_Val_Fwd valcontents;
2043 struct Lisp_Symbol *sym;
2044 struct Lisp_Buffer_Local_Value *blv = NULL;
2046 CHECK_SYMBOL (variable);
2047 sym = XSYMBOL (variable);
2049 start:
2050 switch (sym->redirect)
2052 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
2053 case SYMBOL_PLAINVAL:
2054 forwarded = 0; valcontents.value = SYMBOL_VAL (sym);
2055 if (EQ (valcontents.value, Qunbound))
2056 valcontents.value = Qnil;
2057 break;
2058 case SYMBOL_LOCALIZED:
2059 if (SYMBOL_BLV (sym)->frame_local)
2060 return variable;
2061 else
2062 error ("Symbol %s may not be frame-local",
2063 SDATA (SYMBOL_NAME (variable)));
2064 case SYMBOL_FORWARDED:
2065 forwarded = 1; valcontents.fwd = SYMBOL_FWD (sym);
2066 if (KBOARD_OBJFWDP (valcontents.fwd) || BUFFER_OBJFWDP (valcontents.fwd))
2067 error ("Symbol %s may not be frame-local",
2068 SDATA (SYMBOL_NAME (variable)));
2069 break;
2070 default: emacs_abort ();
2073 if (SYMBOL_TRAPPED_WRITE_P (variable))
2074 error ("Symbol %s may not be frame-local", SDATA (SYMBOL_NAME (variable)));
2076 blv = make_blv (sym, forwarded, valcontents);
2077 blv->frame_local = 1;
2078 sym->redirect = SYMBOL_LOCALIZED;
2079 SET_SYMBOL_BLV (sym, blv);
2081 Lisp_Object symbol;
2082 XSETSYMBOL (symbol, sym); /* In case `variable' is aliased. */
2083 if (let_shadows_global_binding_p (symbol))
2085 AUTO_STRING (format, "Making %s frame-local while let-bound!");
2086 CALLN (Fmessage, format, SYMBOL_NAME (variable));
2089 return variable;
2092 DEFUN ("local-variable-p", Flocal_variable_p, Slocal_variable_p,
2093 1, 2, 0,
2094 doc: /* Non-nil if VARIABLE has a local binding in buffer BUFFER.
2095 BUFFER defaults to the current buffer. */)
2096 (Lisp_Object variable, Lisp_Object buffer)
2098 struct buffer *buf = decode_buffer (buffer);
2099 struct Lisp_Symbol *sym;
2101 CHECK_SYMBOL (variable);
2102 sym = XSYMBOL (variable);
2104 start:
2105 switch (sym->redirect)
2107 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
2108 case SYMBOL_PLAINVAL: return Qnil;
2109 case SYMBOL_LOCALIZED:
2111 Lisp_Object tail, elt, tmp;
2112 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
2113 XSETBUFFER (tmp, buf);
2114 XSETSYMBOL (variable, sym); /* Update in case of aliasing. */
2116 if (EQ (blv->where, tmp)) /* The binding is already loaded. */
2117 return blv_found (blv) ? Qt : Qnil;
2118 else
2119 for (tail = BVAR (buf, local_var_alist); CONSP (tail); tail = XCDR (tail))
2121 elt = XCAR (tail);
2122 if (EQ (variable, XCAR (elt)))
2124 eassert (!blv->frame_local);
2125 return Qt;
2128 return Qnil;
2130 case SYMBOL_FORWARDED:
2132 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
2133 if (BUFFER_OBJFWDP (valcontents))
2135 int offset = XBUFFER_OBJFWD (valcontents)->offset;
2136 int idx = PER_BUFFER_IDX (offset);
2137 if (idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
2138 return Qt;
2140 return Qnil;
2142 default: emacs_abort ();
2146 DEFUN ("local-variable-if-set-p", Flocal_variable_if_set_p, Slocal_variable_if_set_p,
2147 1, 2, 0,
2148 doc: /* Non-nil if VARIABLE is local in buffer BUFFER when set there.
2149 BUFFER defaults to the current buffer.
2151 More precisely, return non-nil if either VARIABLE already has a local
2152 value in BUFFER, or if VARIABLE is automatically buffer-local (see
2153 `make-variable-buffer-local'). */)
2154 (register Lisp_Object variable, Lisp_Object buffer)
2156 struct Lisp_Symbol *sym;
2158 CHECK_SYMBOL (variable);
2159 sym = XSYMBOL (variable);
2161 start:
2162 switch (sym->redirect)
2164 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
2165 case SYMBOL_PLAINVAL: return Qnil;
2166 case SYMBOL_LOCALIZED:
2168 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
2169 if (blv->local_if_set)
2170 return Qt;
2171 XSETSYMBOL (variable, sym); /* Update in case of aliasing. */
2172 return Flocal_variable_p (variable, buffer);
2174 case SYMBOL_FORWARDED:
2175 /* All BUFFER_OBJFWD slots become local if they are set. */
2176 return (BUFFER_OBJFWDP (SYMBOL_FWD (sym)) ? Qt : Qnil);
2177 default: emacs_abort ();
2181 DEFUN ("variable-binding-locus", Fvariable_binding_locus, Svariable_binding_locus,
2182 1, 1, 0,
2183 doc: /* Return a value indicating where VARIABLE's current binding comes from.
2184 If the current binding is buffer-local, the value is the current buffer.
2185 If the current binding is frame-local, the value is the selected frame.
2186 If the current binding is global (the default), the value is nil. */)
2187 (register Lisp_Object variable)
2189 struct Lisp_Symbol *sym;
2191 CHECK_SYMBOL (variable);
2192 sym = XSYMBOL (variable);
2194 /* Make sure the current binding is actually swapped in. */
2195 find_symbol_value (variable);
2197 start:
2198 switch (sym->redirect)
2200 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
2201 case SYMBOL_PLAINVAL: return Qnil;
2202 case SYMBOL_FORWARDED:
2204 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
2205 if (KBOARD_OBJFWDP (valcontents))
2206 return Fframe_terminal (selected_frame);
2207 else if (!BUFFER_OBJFWDP (valcontents))
2208 return Qnil;
2210 /* FALLTHROUGH */
2211 case SYMBOL_LOCALIZED:
2212 /* For a local variable, record both the symbol and which
2213 buffer's or frame's value we are saving. */
2214 if (!NILP (Flocal_variable_p (variable, Qnil)))
2215 return Fcurrent_buffer ();
2216 else if (sym->redirect == SYMBOL_LOCALIZED
2217 && blv_found (SYMBOL_BLV (sym)))
2218 return SYMBOL_BLV (sym)->where;
2219 else
2220 return Qnil;
2221 default: emacs_abort ();
2225 /* This code is disabled now that we use the selected frame to return
2226 keyboard-local-values. */
2227 #if 0
2228 extern struct terminal *get_terminal (Lisp_Object display, int);
2230 DEFUN ("terminal-local-value", Fterminal_local_value,
2231 Sterminal_local_value, 2, 2, 0,
2232 doc: /* Return the terminal-local value of SYMBOL on TERMINAL.
2233 If SYMBOL is not a terminal-local variable, then return its normal
2234 value, like `symbol-value'.
2236 TERMINAL may be a terminal object, a frame, or nil (meaning the
2237 selected frame's terminal device). */)
2238 (Lisp_Object symbol, Lisp_Object terminal)
2240 Lisp_Object result;
2241 struct terminal *t = get_terminal (terminal, 1);
2242 push_kboard (t->kboard);
2243 result = Fsymbol_value (symbol);
2244 pop_kboard ();
2245 return result;
2248 DEFUN ("set-terminal-local-value", Fset_terminal_local_value,
2249 Sset_terminal_local_value, 3, 3, 0,
2250 doc: /* Set the terminal-local binding of SYMBOL on TERMINAL to VALUE.
2251 If VARIABLE is not a terminal-local variable, then set its normal
2252 binding, like `set'.
2254 TERMINAL may be a terminal object, a frame, or nil (meaning the
2255 selected frame's terminal device). */)
2256 (Lisp_Object symbol, Lisp_Object terminal, Lisp_Object value)
2258 Lisp_Object result;
2259 struct terminal *t = get_terminal (terminal, 1);
2260 push_kboard (d->kboard);
2261 result = Fset (symbol, value);
2262 pop_kboard ();
2263 return result;
2265 #endif
2267 /* Find the function at the end of a chain of symbol function indirections. */
2269 /* If OBJECT is a symbol, find the end of its function chain and
2270 return the value found there. If OBJECT is not a symbol, just
2271 return it. If there is a cycle in the function chain, signal a
2272 cyclic-function-indirection error.
2274 This is like Findirect_function, except that it doesn't signal an
2275 error if the chain ends up unbound. */
2276 Lisp_Object
2277 indirect_function (register Lisp_Object object)
2279 Lisp_Object tortoise, hare;
2281 hare = tortoise = object;
2283 for (;;)
2285 if (!SYMBOLP (hare) || NILP (hare))
2286 break;
2287 hare = XSYMBOL (hare)->function;
2288 if (!SYMBOLP (hare) || NILP (hare))
2289 break;
2290 hare = XSYMBOL (hare)->function;
2292 tortoise = XSYMBOL (tortoise)->function;
2294 if (EQ (hare, tortoise))
2295 xsignal1 (Qcyclic_function_indirection, object);
2298 return hare;
2301 DEFUN ("indirect-function", Findirect_function, Sindirect_function, 1, 2, 0,
2302 doc: /* Return the function at the end of OBJECT's function chain.
2303 If OBJECT is not a symbol, just return it. Otherwise, follow all
2304 function indirections to find the final function binding and return it.
2305 Signal a cyclic-function-indirection error if there is a loop in the
2306 function chain of symbols. */)
2307 (register Lisp_Object object, Lisp_Object noerror)
2309 Lisp_Object result;
2311 /* Optimize for no indirection. */
2312 result = object;
2313 if (SYMBOLP (result) && !NILP (result)
2314 && (result = XSYMBOL (result)->function, SYMBOLP (result)))
2315 result = indirect_function (result);
2316 if (!NILP (result))
2317 return result;
2319 return Qnil;
2322 /* Extract and set vector and string elements. */
2324 DEFUN ("aref", Faref, Saref, 2, 2, 0,
2325 doc: /* Return the element of ARRAY at index IDX.
2326 ARRAY may be a vector, a string, a char-table, a bool-vector,
2327 or a byte-code object. IDX starts at 0. */)
2328 (register Lisp_Object array, Lisp_Object idx)
2330 register EMACS_INT idxval;
2332 CHECK_NUMBER (idx);
2333 idxval = XINT (idx);
2334 if (STRINGP (array))
2336 int c;
2337 ptrdiff_t idxval_byte;
2339 if (idxval < 0 || idxval >= SCHARS (array))
2340 args_out_of_range (array, idx);
2341 if (! STRING_MULTIBYTE (array))
2342 return make_number ((unsigned char) SREF (array, idxval));
2343 idxval_byte = string_char_to_byte (array, idxval);
2345 c = STRING_CHAR (SDATA (array) + idxval_byte);
2346 return make_number (c);
2348 else if (BOOL_VECTOR_P (array))
2350 if (idxval < 0 || idxval >= bool_vector_size (array))
2351 args_out_of_range (array, idx);
2352 return bool_vector_ref (array, idxval);
2354 else if (CHAR_TABLE_P (array))
2356 CHECK_CHARACTER (idx);
2357 return CHAR_TABLE_REF (array, idxval);
2359 else
2361 ptrdiff_t size = 0;
2362 if (VECTORP (array))
2363 size = ASIZE (array);
2364 else if (COMPILEDP (array))
2365 size = ASIZE (array) & PSEUDOVECTOR_SIZE_MASK;
2366 else
2367 wrong_type_argument (Qarrayp, array);
2369 if (idxval < 0 || idxval >= size)
2370 args_out_of_range (array, idx);
2371 return AREF (array, idxval);
2375 DEFUN ("aset", Faset, Saset, 3, 3, 0,
2376 doc: /* Store into the element of ARRAY at index IDX the value NEWELT.
2377 Return NEWELT. ARRAY may be a vector, a string, a char-table or a
2378 bool-vector. IDX starts at 0. */)
2379 (register Lisp_Object array, Lisp_Object idx, Lisp_Object newelt)
2381 register EMACS_INT idxval;
2383 CHECK_NUMBER (idx);
2384 idxval = XINT (idx);
2385 CHECK_ARRAY (array, Qarrayp);
2387 if (VECTORP (array))
2389 CHECK_IMPURE (array, XVECTOR (array));
2390 if (idxval < 0 || idxval >= ASIZE (array))
2391 args_out_of_range (array, idx);
2392 ASET (array, idxval, newelt);
2394 else if (BOOL_VECTOR_P (array))
2396 if (idxval < 0 || idxval >= bool_vector_size (array))
2397 args_out_of_range (array, idx);
2398 bool_vector_set (array, idxval, !NILP (newelt));
2400 else if (CHAR_TABLE_P (array))
2402 CHECK_CHARACTER (idx);
2403 CHAR_TABLE_SET (array, idxval, newelt);
2405 else
2407 int c;
2409 CHECK_IMPURE (array, XSTRING (array));
2410 if (idxval < 0 || idxval >= SCHARS (array))
2411 args_out_of_range (array, idx);
2412 CHECK_CHARACTER (newelt);
2413 c = XFASTINT (newelt);
2415 if (STRING_MULTIBYTE (array))
2417 ptrdiff_t idxval_byte, nbytes;
2418 int prev_bytes, new_bytes;
2419 unsigned char workbuf[MAX_MULTIBYTE_LENGTH], *p0 = workbuf, *p1;
2421 nbytes = SBYTES (array);
2422 idxval_byte = string_char_to_byte (array, idxval);
2423 p1 = SDATA (array) + idxval_byte;
2424 prev_bytes = BYTES_BY_CHAR_HEAD (*p1);
2425 new_bytes = CHAR_STRING (c, p0);
2426 if (prev_bytes != new_bytes)
2428 /* We must relocate the string data. */
2429 ptrdiff_t nchars = SCHARS (array);
2430 USE_SAFE_ALLOCA;
2431 unsigned char *str = SAFE_ALLOCA (nbytes);
2433 memcpy (str, SDATA (array), nbytes);
2434 allocate_string_data (XSTRING (array), nchars,
2435 nbytes + new_bytes - prev_bytes);
2436 memcpy (SDATA (array), str, idxval_byte);
2437 p1 = SDATA (array) + idxval_byte;
2438 memcpy (p1 + new_bytes, str + idxval_byte + prev_bytes,
2439 nbytes - (idxval_byte + prev_bytes));
2440 SAFE_FREE ();
2441 clear_string_char_byte_cache ();
2443 while (new_bytes--)
2444 *p1++ = *p0++;
2446 else
2448 if (! SINGLE_BYTE_CHAR_P (c))
2450 ptrdiff_t i;
2452 for (i = SBYTES (array) - 1; i >= 0; i--)
2453 if (SREF (array, i) >= 0x80)
2454 args_out_of_range (array, newelt);
2455 /* ARRAY is an ASCII string. Convert it to a multibyte
2456 string, and try `aset' again. */
2457 STRING_SET_MULTIBYTE (array);
2458 return Faset (array, idx, newelt);
2460 SSET (array, idxval, c);
2464 return newelt;
2467 /* Arithmetic functions */
2469 Lisp_Object
2470 arithcompare (Lisp_Object num1, Lisp_Object num2, enum Arith_Comparison comparison)
2472 double f1 = 0, f2 = 0;
2473 bool floatp = 0;
2475 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num1);
2476 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num2);
2478 if (FLOATP (num1) || FLOATP (num2))
2480 floatp = 1;
2481 f1 = (FLOATP (num1)) ? XFLOAT_DATA (num1) : XINT (num1);
2482 f2 = (FLOATP (num2)) ? XFLOAT_DATA (num2) : XINT (num2);
2485 switch (comparison)
2487 case ARITH_EQUAL:
2488 if (floatp ? f1 == f2 : XINT (num1) == XINT (num2))
2489 return Qt;
2490 return Qnil;
2492 case ARITH_NOTEQUAL:
2493 if (floatp ? f1 != f2 : XINT (num1) != XINT (num2))
2494 return Qt;
2495 return Qnil;
2497 case ARITH_LESS:
2498 if (floatp ? f1 < f2 : XINT (num1) < XINT (num2))
2499 return Qt;
2500 return Qnil;
2502 case ARITH_LESS_OR_EQUAL:
2503 if (floatp ? f1 <= f2 : XINT (num1) <= XINT (num2))
2504 return Qt;
2505 return Qnil;
2507 case ARITH_GRTR:
2508 if (floatp ? f1 > f2 : XINT (num1) > XINT (num2))
2509 return Qt;
2510 return Qnil;
2512 case ARITH_GRTR_OR_EQUAL:
2513 if (floatp ? f1 >= f2 : XINT (num1) >= XINT (num2))
2514 return Qt;
2515 return Qnil;
2517 default:
2518 emacs_abort ();
2522 static Lisp_Object
2523 arithcompare_driver (ptrdiff_t nargs, Lisp_Object *args,
2524 enum Arith_Comparison comparison)
2526 ptrdiff_t argnum;
2527 for (argnum = 1; argnum < nargs; ++argnum)
2529 if (EQ (Qnil, arithcompare (args[argnum - 1], args[argnum], comparison)))
2530 return Qnil;
2532 return Qt;
2535 DEFUN ("=", Feqlsign, Seqlsign, 1, MANY, 0,
2536 doc: /* Return t if args, all numbers or markers, are equal.
2537 usage: (= NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2538 (ptrdiff_t nargs, Lisp_Object *args)
2540 return arithcompare_driver (nargs, args, ARITH_EQUAL);
2543 DEFUN ("<", Flss, Slss, 1, MANY, 0,
2544 doc: /* Return t if each arg (a number or marker), is less than the next arg.
2545 usage: (< NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2546 (ptrdiff_t nargs, Lisp_Object *args)
2548 return arithcompare_driver (nargs, args, ARITH_LESS);
2551 DEFUN (">", Fgtr, Sgtr, 1, MANY, 0,
2552 doc: /* Return t if each arg (a number or marker) is greater than the next arg.
2553 usage: (> NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2554 (ptrdiff_t nargs, Lisp_Object *args)
2556 return arithcompare_driver (nargs, args, ARITH_GRTR);
2559 DEFUN ("<=", Fleq, Sleq, 1, MANY, 0,
2560 doc: /* Return t if each arg (a number or marker) is less than or equal to the next.
2561 usage: (<= NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2562 (ptrdiff_t nargs, Lisp_Object *args)
2564 return arithcompare_driver (nargs, args, ARITH_LESS_OR_EQUAL);
2567 DEFUN (">=", Fgeq, Sgeq, 1, MANY, 0,
2568 doc: /* Return t if each arg (a number or marker) is greater than or equal to the next.
2569 usage: (>= NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2570 (ptrdiff_t nargs, Lisp_Object *args)
2572 return arithcompare_driver (nargs, args, ARITH_GRTR_OR_EQUAL);
2575 DEFUN ("/=", Fneq, Sneq, 2, 2, 0,
2576 doc: /* Return t if first arg is not equal to second arg. Both must be numbers or markers. */)
2577 (register Lisp_Object num1, Lisp_Object num2)
2579 return arithcompare (num1, num2, ARITH_NOTEQUAL);
2582 /* Convert the integer I to a cons-of-integers, where I is not in
2583 fixnum range. */
2585 #define INTBIG_TO_LISP(i, extremum) \
2586 (eassert (FIXNUM_OVERFLOW_P (i)), \
2587 (! (FIXNUM_OVERFLOW_P ((extremum) >> 16) \
2588 && FIXNUM_OVERFLOW_P ((i) >> 16)) \
2589 ? Fcons (make_number ((i) >> 16), make_number ((i) & 0xffff)) \
2590 : ! (FIXNUM_OVERFLOW_P ((extremum) >> 16 >> 24) \
2591 && FIXNUM_OVERFLOW_P ((i) >> 16 >> 24)) \
2592 ? Fcons (make_number ((i) >> 16 >> 24), \
2593 Fcons (make_number ((i) >> 16 & 0xffffff), \
2594 make_number ((i) & 0xffff))) \
2595 : make_float (i)))
2597 Lisp_Object
2598 intbig_to_lisp (intmax_t i)
2600 return INTBIG_TO_LISP (i, INTMAX_MIN);
2603 Lisp_Object
2604 uintbig_to_lisp (uintmax_t i)
2606 return INTBIG_TO_LISP (i, UINTMAX_MAX);
2609 /* Convert the cons-of-integers, integer, or float value C to an
2610 unsigned value with maximum value MAX. Signal an error if C does not
2611 have a valid format or is out of range. */
2612 uintmax_t
2613 cons_to_unsigned (Lisp_Object c, uintmax_t max)
2615 bool valid = 0;
2616 uintmax_t val;
2617 if (INTEGERP (c))
2619 valid = 0 <= XINT (c);
2620 val = XINT (c);
2622 else if (FLOATP (c))
2624 double d = XFLOAT_DATA (c);
2625 if (0 <= d
2626 && d < (max == UINTMAX_MAX ? (double) UINTMAX_MAX + 1 : max + 1))
2628 val = d;
2629 valid = 1;
2632 else if (CONSP (c) && NATNUMP (XCAR (c)))
2634 uintmax_t top = XFASTINT (XCAR (c));
2635 Lisp_Object rest = XCDR (c);
2636 if (top <= UINTMAX_MAX >> 24 >> 16
2637 && CONSP (rest)
2638 && NATNUMP (XCAR (rest)) && XFASTINT (XCAR (rest)) < 1 << 24
2639 && NATNUMP (XCDR (rest)) && XFASTINT (XCDR (rest)) < 1 << 16)
2641 uintmax_t mid = XFASTINT (XCAR (rest));
2642 val = top << 24 << 16 | mid << 16 | XFASTINT (XCDR (rest));
2643 valid = 1;
2645 else if (top <= UINTMAX_MAX >> 16)
2647 if (CONSP (rest))
2648 rest = XCAR (rest);
2649 if (NATNUMP (rest) && XFASTINT (rest) < 1 << 16)
2651 val = top << 16 | XFASTINT (rest);
2652 valid = 1;
2657 if (! (valid && val <= max))
2658 error ("Not an in-range integer, float, or cons of integers");
2659 return val;
2662 /* Convert the cons-of-integers, integer, or float value C to a signed
2663 value with extrema MIN and MAX. Signal an error if C does not have
2664 a valid format or is out of range. */
2665 intmax_t
2666 cons_to_signed (Lisp_Object c, intmax_t min, intmax_t max)
2668 bool valid = 0;
2669 intmax_t val;
2670 if (INTEGERP (c))
2672 val = XINT (c);
2673 valid = 1;
2675 else if (FLOATP (c))
2677 double d = XFLOAT_DATA (c);
2678 if (min <= d
2679 && d < (max == INTMAX_MAX ? (double) INTMAX_MAX + 1 : max + 1))
2681 val = d;
2682 valid = 1;
2685 else if (CONSP (c) && INTEGERP (XCAR (c)))
2687 intmax_t top = XINT (XCAR (c));
2688 Lisp_Object rest = XCDR (c);
2689 if (INTMAX_MIN >> 24 >> 16 <= top && top <= INTMAX_MAX >> 24 >> 16
2690 && CONSP (rest)
2691 && NATNUMP (XCAR (rest)) && XFASTINT (XCAR (rest)) < 1 << 24
2692 && NATNUMP (XCDR (rest)) && XFASTINT (XCDR (rest)) < 1 << 16)
2694 intmax_t mid = XFASTINT (XCAR (rest));
2695 val = top << 24 << 16 | mid << 16 | XFASTINT (XCDR (rest));
2696 valid = 1;
2698 else if (INTMAX_MIN >> 16 <= top && top <= INTMAX_MAX >> 16)
2700 if (CONSP (rest))
2701 rest = XCAR (rest);
2702 if (NATNUMP (rest) && XFASTINT (rest) < 1 << 16)
2704 val = top << 16 | XFASTINT (rest);
2705 valid = 1;
2710 if (! (valid && min <= val && val <= max))
2711 error ("Not an in-range integer, float, or cons of integers");
2712 return val;
2715 DEFUN ("number-to-string", Fnumber_to_string, Snumber_to_string, 1, 1, 0,
2716 doc: /* Return the decimal representation of NUMBER as a string.
2717 Uses a minus sign if negative.
2718 NUMBER may be an integer or a floating point number. */)
2719 (Lisp_Object number)
2721 char buffer[max (FLOAT_TO_STRING_BUFSIZE, INT_BUFSIZE_BOUND (EMACS_INT))];
2722 int len;
2724 CHECK_NUMBER_OR_FLOAT (number);
2726 if (FLOATP (number))
2727 len = float_to_string (buffer, XFLOAT_DATA (number));
2728 else
2729 len = sprintf (buffer, "%"pI"d", XINT (number));
2731 return make_unibyte_string (buffer, len);
2734 DEFUN ("string-to-number", Fstring_to_number, Sstring_to_number, 1, 2, 0,
2735 doc: /* Parse STRING as a decimal number and return the number.
2736 Ignore leading spaces and tabs, and all trailing chars. Return 0 if
2737 STRING cannot be parsed as an integer or floating point number.
2739 If BASE, interpret STRING as a number in that base. If BASE isn't
2740 present, base 10 is used. BASE must be between 2 and 16 (inclusive).
2741 If the base used is not 10, STRING is always parsed as an integer. */)
2742 (register Lisp_Object string, Lisp_Object base)
2744 register char *p;
2745 register int b;
2746 Lisp_Object val;
2748 CHECK_STRING (string);
2750 if (NILP (base))
2751 b = 10;
2752 else
2754 CHECK_NUMBER (base);
2755 if (! (2 <= XINT (base) && XINT (base) <= 16))
2756 xsignal1 (Qargs_out_of_range, base);
2757 b = XINT (base);
2760 p = SSDATA (string);
2761 while (*p == ' ' || *p == '\t')
2762 p++;
2764 val = string_to_number (p, b, 1);
2765 return NILP (val) ? make_number (0) : val;
2768 enum arithop
2770 Aadd,
2771 Asub,
2772 Amult,
2773 Adiv,
2774 Alogand,
2775 Alogior,
2776 Alogxor,
2777 Amax,
2778 Amin
2781 static Lisp_Object float_arith_driver (double, ptrdiff_t, enum arithop,
2782 ptrdiff_t, Lisp_Object *);
2783 static Lisp_Object
2784 arith_driver (enum arithop code, ptrdiff_t nargs, Lisp_Object *args)
2786 Lisp_Object val;
2787 ptrdiff_t argnum, ok_args;
2788 EMACS_INT accum = 0;
2789 EMACS_INT next, ok_accum;
2790 bool overflow = 0;
2792 switch (code)
2794 case Alogior:
2795 case Alogxor:
2796 case Aadd:
2797 case Asub:
2798 accum = 0;
2799 break;
2800 case Amult:
2801 case Adiv:
2802 accum = 1;
2803 break;
2804 case Alogand:
2805 accum = -1;
2806 break;
2807 default:
2808 break;
2811 for (argnum = 0; argnum < nargs; argnum++)
2813 if (! overflow)
2815 ok_args = argnum;
2816 ok_accum = accum;
2819 /* Using args[argnum] as argument to CHECK_NUMBER_... */
2820 val = args[argnum];
2821 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val);
2823 if (FLOATP (val))
2824 return float_arith_driver (ok_accum, ok_args, code,
2825 nargs, args);
2826 args[argnum] = val;
2827 next = XINT (args[argnum]);
2828 switch (code)
2830 case Aadd:
2831 overflow |= INT_ADD_WRAPV (accum, next, &accum);
2832 break;
2833 case Asub:
2834 if (! argnum)
2835 accum = nargs == 1 ? - next : next;
2836 else
2837 overflow |= INT_SUBTRACT_WRAPV (accum, next, &accum);
2838 break;
2839 case Amult:
2840 overflow |= INT_MULTIPLY_WRAPV (accum, next, &accum);
2841 break;
2842 case Adiv:
2843 if (! (argnum || nargs == 1))
2844 accum = next;
2845 else
2847 if (next == 0)
2848 xsignal0 (Qarith_error);
2849 if (INT_DIVIDE_OVERFLOW (accum, next))
2850 overflow = true;
2851 else
2852 accum /= next;
2854 break;
2855 case Alogand:
2856 accum &= next;
2857 break;
2858 case Alogior:
2859 accum |= next;
2860 break;
2861 case Alogxor:
2862 accum ^= next;
2863 break;
2864 case Amax:
2865 if (!argnum || next > accum)
2866 accum = next;
2867 break;
2868 case Amin:
2869 if (!argnum || next < accum)
2870 accum = next;
2871 break;
2875 XSETINT (val, accum);
2876 return val;
2879 #undef isnan
2880 #define isnan(x) ((x) != (x))
2882 static Lisp_Object
2883 float_arith_driver (double accum, ptrdiff_t argnum, enum arithop code,
2884 ptrdiff_t nargs, Lisp_Object *args)
2886 register Lisp_Object val;
2887 double next;
2889 for (; argnum < nargs; argnum++)
2891 val = args[argnum]; /* using args[argnum] as argument to CHECK_NUMBER_... */
2892 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val);
2894 if (FLOATP (val))
2896 next = XFLOAT_DATA (val);
2898 else
2900 args[argnum] = val; /* runs into a compiler bug. */
2901 next = XINT (args[argnum]);
2903 switch (code)
2905 case Aadd:
2906 accum += next;
2907 break;
2908 case Asub:
2909 accum = argnum ? accum - next : nargs == 1 ? - next : next;
2910 break;
2911 case Amult:
2912 accum *= next;
2913 break;
2914 case Adiv:
2915 if (! (argnum || nargs == 1))
2916 accum = next;
2917 else
2919 if (! IEEE_FLOATING_POINT && next == 0)
2920 xsignal0 (Qarith_error);
2921 accum /= next;
2923 break;
2924 case Alogand:
2925 case Alogior:
2926 case Alogxor:
2927 wrong_type_argument (Qinteger_or_marker_p, val);
2928 case Amax:
2929 if (!argnum || isnan (next) || next > accum)
2930 accum = next;
2931 break;
2932 case Amin:
2933 if (!argnum || isnan (next) || next < accum)
2934 accum = next;
2935 break;
2939 return make_float (accum);
2943 DEFUN ("+", Fplus, Splus, 0, MANY, 0,
2944 doc: /* Return sum of any number of arguments, which are numbers or markers.
2945 usage: (+ &rest NUMBERS-OR-MARKERS) */)
2946 (ptrdiff_t nargs, Lisp_Object *args)
2948 return arith_driver (Aadd, nargs, args);
2951 DEFUN ("-", Fminus, Sminus, 0, MANY, 0,
2952 doc: /* Negate number or subtract numbers or markers and return the result.
2953 With one arg, negates it. With more than one arg,
2954 subtracts all but the first from the first.
2955 usage: (- &optional NUMBER-OR-MARKER &rest MORE-NUMBERS-OR-MARKERS) */)
2956 (ptrdiff_t nargs, Lisp_Object *args)
2958 return arith_driver (Asub, nargs, args);
2961 DEFUN ("*", Ftimes, Stimes, 0, MANY, 0,
2962 doc: /* Return product of any number of arguments, which are numbers or markers.
2963 usage: (* &rest NUMBERS-OR-MARKERS) */)
2964 (ptrdiff_t nargs, Lisp_Object *args)
2966 return arith_driver (Amult, nargs, args);
2969 DEFUN ("/", Fquo, Squo, 1, MANY, 0,
2970 doc: /* Divide number by divisors and return the result.
2971 With two or more arguments, return first argument divided by the rest.
2972 With one argument, return 1 divided by the argument.
2973 The arguments must be numbers or markers.
2974 usage: (/ NUMBER &rest DIVISORS) */)
2975 (ptrdiff_t nargs, Lisp_Object *args)
2977 ptrdiff_t argnum;
2978 for (argnum = 2; argnum < nargs; argnum++)
2979 if (FLOATP (args[argnum]))
2980 return float_arith_driver (0, 0, Adiv, nargs, args);
2981 return arith_driver (Adiv, nargs, args);
2984 DEFUN ("%", Frem, Srem, 2, 2, 0,
2985 doc: /* Return remainder of X divided by Y.
2986 Both must be integers or markers. */)
2987 (register Lisp_Object x, Lisp_Object y)
2989 Lisp_Object val;
2991 CHECK_NUMBER_COERCE_MARKER (x);
2992 CHECK_NUMBER_COERCE_MARKER (y);
2994 if (XINT (y) == 0)
2995 xsignal0 (Qarith_error);
2997 XSETINT (val, XINT (x) % XINT (y));
2998 return val;
3001 DEFUN ("mod", Fmod, Smod, 2, 2, 0,
3002 doc: /* Return X modulo Y.
3003 The result falls between zero (inclusive) and Y (exclusive).
3004 Both X and Y must be numbers or markers. */)
3005 (register Lisp_Object x, Lisp_Object y)
3007 Lisp_Object val;
3008 EMACS_INT i1, i2;
3010 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (x);
3011 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (y);
3013 if (FLOATP (x) || FLOATP (y))
3014 return fmod_float (x, y);
3016 i1 = XINT (x);
3017 i2 = XINT (y);
3019 if (i2 == 0)
3020 xsignal0 (Qarith_error);
3022 i1 %= i2;
3024 /* If the "remainder" comes out with the wrong sign, fix it. */
3025 if (i2 < 0 ? i1 > 0 : i1 < 0)
3026 i1 += i2;
3028 XSETINT (val, i1);
3029 return val;
3032 DEFUN ("max", Fmax, Smax, 1, MANY, 0,
3033 doc: /* Return largest of all the arguments (which must be numbers or markers).
3034 The value is always a number; markers are converted to numbers.
3035 usage: (max NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
3036 (ptrdiff_t nargs, Lisp_Object *args)
3038 return arith_driver (Amax, nargs, args);
3041 DEFUN ("min", Fmin, Smin, 1, MANY, 0,
3042 doc: /* Return smallest of all the arguments (which must be numbers or markers).
3043 The value is always a number; markers are converted to numbers.
3044 usage: (min NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
3045 (ptrdiff_t nargs, Lisp_Object *args)
3047 return arith_driver (Amin, nargs, args);
3050 DEFUN ("logand", Flogand, Slogand, 0, MANY, 0,
3051 doc: /* Return bitwise-and of all the arguments.
3052 Arguments may be integers, or markers converted to integers.
3053 usage: (logand &rest INTS-OR-MARKERS) */)
3054 (ptrdiff_t nargs, Lisp_Object *args)
3056 return arith_driver (Alogand, nargs, args);
3059 DEFUN ("logior", Flogior, Slogior, 0, MANY, 0,
3060 doc: /* Return bitwise-or of all the arguments.
3061 Arguments may be integers, or markers converted to integers.
3062 usage: (logior &rest INTS-OR-MARKERS) */)
3063 (ptrdiff_t nargs, Lisp_Object *args)
3065 return arith_driver (Alogior, nargs, args);
3068 DEFUN ("logxor", Flogxor, Slogxor, 0, MANY, 0,
3069 doc: /* Return bitwise-exclusive-or of all the arguments.
3070 Arguments may be integers, or markers converted to integers.
3071 usage: (logxor &rest INTS-OR-MARKERS) */)
3072 (ptrdiff_t nargs, Lisp_Object *args)
3074 return arith_driver (Alogxor, nargs, args);
3077 static Lisp_Object
3078 ash_lsh_impl (register Lisp_Object value, Lisp_Object count, bool lsh)
3080 register Lisp_Object val;
3082 CHECK_NUMBER (value);
3083 CHECK_NUMBER (count);
3085 if (XINT (count) >= EMACS_INT_WIDTH)
3086 XSETINT (val, 0);
3087 else if (XINT (count) > 0)
3088 XSETINT (val, XUINT (value) << XFASTINT (count));
3089 else if (XINT (count) <= -EMACS_INT_WIDTH)
3090 XSETINT (val, lsh ? 0 : XINT (value) < 0 ? -1 : 0);
3091 else
3092 XSETINT (val, lsh ? XUINT (value) >> -XINT (count) : \
3093 XINT (value) >> -XINT (count));
3094 return val;
3097 DEFUN ("ash", Fash, Sash, 2, 2, 0,
3098 doc: /* Return VALUE with its bits shifted left by COUNT.
3099 If COUNT is negative, shifting is actually to the right.
3100 In this case, the sign bit is duplicated. */)
3101 (register Lisp_Object value, Lisp_Object count)
3103 return ash_lsh_impl (value, count, false);
3106 DEFUN ("lsh", Flsh, Slsh, 2, 2, 0,
3107 doc: /* Return VALUE with its bits shifted left by COUNT.
3108 If COUNT is negative, shifting is actually to the right.
3109 In this case, zeros are shifted in on the left. */)
3110 (register Lisp_Object value, Lisp_Object count)
3112 return ash_lsh_impl (value, count, true);
3115 DEFUN ("1+", Fadd1, Sadd1, 1, 1, 0,
3116 doc: /* Return NUMBER plus one. NUMBER may be a number or a marker.
3117 Markers are converted to integers. */)
3118 (register Lisp_Object number)
3120 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number);
3122 if (FLOATP (number))
3123 return (make_float (1.0 + XFLOAT_DATA (number)));
3125 XSETINT (number, XINT (number) + 1);
3126 return number;
3129 DEFUN ("1-", Fsub1, Ssub1, 1, 1, 0,
3130 doc: /* Return NUMBER minus one. NUMBER may be a number or a marker.
3131 Markers are converted to integers. */)
3132 (register Lisp_Object number)
3134 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number);
3136 if (FLOATP (number))
3137 return (make_float (-1.0 + XFLOAT_DATA (number)));
3139 XSETINT (number, XINT (number) - 1);
3140 return number;
3143 DEFUN ("lognot", Flognot, Slognot, 1, 1, 0,
3144 doc: /* Return the bitwise complement of NUMBER. NUMBER must be an integer. */)
3145 (register Lisp_Object number)
3147 CHECK_NUMBER (number);
3148 XSETINT (number, ~XINT (number));
3149 return number;
3152 DEFUN ("byteorder", Fbyteorder, Sbyteorder, 0, 0, 0,
3153 doc: /* Return the byteorder for the machine.
3154 Returns 66 (ASCII uppercase B) for big endian machines or 108 (ASCII
3155 lowercase l) for small endian machines. */
3156 attributes: const)
3157 (void)
3159 unsigned i = 0x04030201;
3160 int order = *(char *)&i == 1 ? 108 : 66;
3162 return make_number (order);
3165 /* Because we round up the bool vector allocate size to word_size
3166 units, we can safely read past the "end" of the vector in the
3167 operations below. These extra bits are always zero. */
3169 static bits_word
3170 bool_vector_spare_mask (EMACS_INT nr_bits)
3172 return (((bits_word) 1) << (nr_bits % BITS_PER_BITS_WORD)) - 1;
3175 /* Info about unsigned long long, falling back on unsigned long
3176 if unsigned long long is not available. */
3178 #if HAVE_UNSIGNED_LONG_LONG_INT && defined ULLONG_WIDTH
3179 enum { ULL_WIDTH = ULLONG_WIDTH };
3180 # define ULL_MAX ULLONG_MAX
3181 #else
3182 enum { ULL_WIDTH = ULONG_WIDTH };
3183 # define ULL_MAX ULONG_MAX
3184 # define count_one_bits_ll count_one_bits_l
3185 # define count_trailing_zeros_ll count_trailing_zeros_l
3186 #endif
3188 /* Shift VAL right by the width of an unsigned long long.
3189 ULL_WIDTH must be less than BITS_PER_BITS_WORD. */
3191 static bits_word
3192 shift_right_ull (bits_word w)
3194 /* Pacify bogus GCC warning about shift count exceeding type width. */
3195 int shift = ULL_WIDTH - BITS_PER_BITS_WORD < 0 ? ULL_WIDTH : 0;
3196 return w >> shift;
3199 /* Return the number of 1 bits in W. */
3201 static int
3202 count_one_bits_word (bits_word w)
3204 if (BITS_WORD_MAX <= UINT_MAX)
3205 return count_one_bits (w);
3206 else if (BITS_WORD_MAX <= ULONG_MAX)
3207 return count_one_bits_l (w);
3208 else
3210 int i = 0, count = 0;
3211 while (count += count_one_bits_ll (w),
3212 (i += ULL_WIDTH) < BITS_PER_BITS_WORD)
3213 w = shift_right_ull (w);
3214 return count;
3218 enum bool_vector_op { bool_vector_exclusive_or,
3219 bool_vector_union,
3220 bool_vector_intersection,
3221 bool_vector_set_difference,
3222 bool_vector_subsetp };
3224 static Lisp_Object
3225 bool_vector_binop_driver (Lisp_Object a,
3226 Lisp_Object b,
3227 Lisp_Object dest,
3228 enum bool_vector_op op)
3230 EMACS_INT nr_bits;
3231 bits_word *adata, *bdata, *destdata;
3232 ptrdiff_t i = 0;
3233 ptrdiff_t nr_words;
3235 CHECK_BOOL_VECTOR (a);
3236 CHECK_BOOL_VECTOR (b);
3238 nr_bits = bool_vector_size (a);
3239 if (bool_vector_size (b) != nr_bits)
3240 wrong_length_argument (a, b, dest);
3242 nr_words = bool_vector_words (nr_bits);
3243 adata = bool_vector_data (a);
3244 bdata = bool_vector_data (b);
3246 if (NILP (dest))
3248 dest = make_uninit_bool_vector (nr_bits);
3249 destdata = bool_vector_data (dest);
3251 else
3253 CHECK_BOOL_VECTOR (dest);
3254 destdata = bool_vector_data (dest);
3255 if (bool_vector_size (dest) != nr_bits)
3256 wrong_length_argument (a, b, dest);
3258 switch (op)
3260 case bool_vector_exclusive_or:
3261 for (; i < nr_words; i++)
3262 if (destdata[i] != (adata[i] ^ bdata[i]))
3263 goto set_dest;
3264 break;
3266 case bool_vector_subsetp:
3267 for (; i < nr_words; i++)
3268 if (adata[i] &~ bdata[i])
3269 return Qnil;
3270 return Qt;
3272 case bool_vector_union:
3273 for (; i < nr_words; i++)
3274 if (destdata[i] != (adata[i] | bdata[i]))
3275 goto set_dest;
3276 break;
3278 case bool_vector_intersection:
3279 for (; i < nr_words; i++)
3280 if (destdata[i] != (adata[i] & bdata[i]))
3281 goto set_dest;
3282 break;
3284 case bool_vector_set_difference:
3285 for (; i < nr_words; i++)
3286 if (destdata[i] != (adata[i] &~ bdata[i]))
3287 goto set_dest;
3288 break;
3291 return Qnil;
3294 set_dest:
3295 switch (op)
3297 case bool_vector_exclusive_or:
3298 for (; i < nr_words; i++)
3299 destdata[i] = adata[i] ^ bdata[i];
3300 break;
3302 case bool_vector_union:
3303 for (; i < nr_words; i++)
3304 destdata[i] = adata[i] | bdata[i];
3305 break;
3307 case bool_vector_intersection:
3308 for (; i < nr_words; i++)
3309 destdata[i] = adata[i] & bdata[i];
3310 break;
3312 case bool_vector_set_difference:
3313 for (; i < nr_words; i++)
3314 destdata[i] = adata[i] &~ bdata[i];
3315 break;
3317 default:
3318 eassume (0);
3321 return dest;
3324 /* PRECONDITION must be true. Return VALUE. This odd construction
3325 works around a bogus GCC diagnostic "shift count >= width of type". */
3327 static int
3328 pre_value (bool precondition, int value)
3330 eassume (precondition);
3331 return precondition ? value : 0;
3334 /* Compute the number of trailing zero bits in val. If val is zero,
3335 return the number of bits in val. */
3336 static int
3337 count_trailing_zero_bits (bits_word val)
3339 if (BITS_WORD_MAX == UINT_MAX)
3340 return count_trailing_zeros (val);
3341 if (BITS_WORD_MAX == ULONG_MAX)
3342 return count_trailing_zeros_l (val);
3343 if (BITS_WORD_MAX == ULL_MAX)
3344 return count_trailing_zeros_ll (val);
3346 /* The rest of this code is for the unlikely platform where bits_word differs
3347 in width from unsigned int, unsigned long, and unsigned long long. */
3348 val |= ~ BITS_WORD_MAX;
3349 if (BITS_WORD_MAX <= UINT_MAX)
3350 return count_trailing_zeros (val);
3351 if (BITS_WORD_MAX <= ULONG_MAX)
3352 return count_trailing_zeros_l (val);
3353 else
3355 int count;
3356 for (count = 0;
3357 count < BITS_PER_BITS_WORD - ULL_WIDTH;
3358 count += ULL_WIDTH)
3360 if (val & ULL_MAX)
3361 return count + count_trailing_zeros_ll (val);
3362 val = shift_right_ull (val);
3365 if (BITS_PER_BITS_WORD % ULL_WIDTH != 0
3366 && BITS_WORD_MAX == (bits_word) -1)
3367 val |= (bits_word) 1 << pre_value (ULONG_MAX < BITS_WORD_MAX,
3368 BITS_PER_BITS_WORD % ULL_WIDTH);
3369 return count + count_trailing_zeros_ll (val);
3373 static bits_word
3374 bits_word_to_host_endian (bits_word val)
3376 #ifndef WORDS_BIGENDIAN
3377 return val;
3378 #else
3379 if (BITS_WORD_MAX >> 31 == 1)
3380 return bswap_32 (val);
3381 # if HAVE_UNSIGNED_LONG_LONG
3382 if (BITS_WORD_MAX >> 31 >> 31 >> 1 == 1)
3383 return bswap_64 (val);
3384 # endif
3386 int i;
3387 bits_word r = 0;
3388 for (i = 0; i < sizeof val; i++)
3390 r = ((r << 1 << (CHAR_BIT - 1))
3391 | (val & ((1u << 1 << (CHAR_BIT - 1)) - 1)));
3392 val = val >> 1 >> (CHAR_BIT - 1);
3394 return r;
3396 #endif
3399 DEFUN ("bool-vector-exclusive-or", Fbool_vector_exclusive_or,
3400 Sbool_vector_exclusive_or, 2, 3, 0,
3401 doc: /* Return A ^ B, bitwise exclusive or.
3402 If optional third argument C is given, store result into C.
3403 A, B, and C must be bool vectors of the same length.
3404 Return the destination vector if it changed or nil otherwise. */)
3405 (Lisp_Object a, Lisp_Object b, Lisp_Object c)
3407 return bool_vector_binop_driver (a, b, c, bool_vector_exclusive_or);
3410 DEFUN ("bool-vector-union", Fbool_vector_union,
3411 Sbool_vector_union, 2, 3, 0,
3412 doc: /* Return A | B, bitwise or.
3413 If optional third argument C is given, store result into C.
3414 A, B, and C must be bool vectors of the same length.
3415 Return the destination vector if it changed or nil otherwise. */)
3416 (Lisp_Object a, Lisp_Object b, Lisp_Object c)
3418 return bool_vector_binop_driver (a, b, c, bool_vector_union);
3421 DEFUN ("bool-vector-intersection", Fbool_vector_intersection,
3422 Sbool_vector_intersection, 2, 3, 0,
3423 doc: /* Return A & B, bitwise and.
3424 If optional third argument C is given, store result into C.
3425 A, B, and C must be bool vectors of the same length.
3426 Return the destination vector if it changed or nil otherwise. */)
3427 (Lisp_Object a, Lisp_Object b, Lisp_Object c)
3429 return bool_vector_binop_driver (a, b, c, bool_vector_intersection);
3432 DEFUN ("bool-vector-set-difference", Fbool_vector_set_difference,
3433 Sbool_vector_set_difference, 2, 3, 0,
3434 doc: /* Return A &~ B, set difference.
3435 If optional third argument C is given, store result into C.
3436 A, B, and C must be bool vectors of the same length.
3437 Return the destination vector if it changed or nil otherwise. */)
3438 (Lisp_Object a, Lisp_Object b, Lisp_Object c)
3440 return bool_vector_binop_driver (a, b, c, bool_vector_set_difference);
3443 DEFUN ("bool-vector-subsetp", Fbool_vector_subsetp,
3444 Sbool_vector_subsetp, 2, 2, 0,
3445 doc: /* Return t if every t value in A is also t in B, nil otherwise.
3446 A and B must be bool vectors of the same length. */)
3447 (Lisp_Object a, Lisp_Object b)
3449 return bool_vector_binop_driver (a, b, b, bool_vector_subsetp);
3452 DEFUN ("bool-vector-not", Fbool_vector_not,
3453 Sbool_vector_not, 1, 2, 0,
3454 doc: /* Compute ~A, set complement.
3455 If optional second argument B is given, store result into B.
3456 A and B must be bool vectors of the same length.
3457 Return the destination vector. */)
3458 (Lisp_Object a, Lisp_Object b)
3460 EMACS_INT nr_bits;
3461 bits_word *bdata, *adata;
3462 ptrdiff_t i;
3464 CHECK_BOOL_VECTOR (a);
3465 nr_bits = bool_vector_size (a);
3467 if (NILP (b))
3468 b = make_uninit_bool_vector (nr_bits);
3469 else
3471 CHECK_BOOL_VECTOR (b);
3472 if (bool_vector_size (b) != nr_bits)
3473 wrong_length_argument (a, b, Qnil);
3476 bdata = bool_vector_data (b);
3477 adata = bool_vector_data (a);
3479 for (i = 0; i < nr_bits / BITS_PER_BITS_WORD; i++)
3480 bdata[i] = BITS_WORD_MAX & ~adata[i];
3482 if (nr_bits % BITS_PER_BITS_WORD)
3484 bits_word mword = bits_word_to_host_endian (adata[i]);
3485 mword = ~mword;
3486 mword &= bool_vector_spare_mask (nr_bits);
3487 bdata[i] = bits_word_to_host_endian (mword);
3490 return b;
3493 DEFUN ("bool-vector-count-population", Fbool_vector_count_population,
3494 Sbool_vector_count_population, 1, 1, 0,
3495 doc: /* Count how many elements in A are t.
3496 A is a bool vector. To count A's nil elements, subtract the return
3497 value from A's length. */)
3498 (Lisp_Object a)
3500 EMACS_INT count;
3501 EMACS_INT nr_bits;
3502 bits_word *adata;
3503 ptrdiff_t i, nwords;
3505 CHECK_BOOL_VECTOR (a);
3507 nr_bits = bool_vector_size (a);
3508 nwords = bool_vector_words (nr_bits);
3509 count = 0;
3510 adata = bool_vector_data (a);
3512 for (i = 0; i < nwords; i++)
3513 count += count_one_bits_word (adata[i]);
3515 return make_number (count);
3518 DEFUN ("bool-vector-count-consecutive", Fbool_vector_count_consecutive,
3519 Sbool_vector_count_consecutive, 3, 3, 0,
3520 doc: /* Count how many consecutive elements in A equal B starting at I.
3521 A is a bool vector, B is t or nil, and I is an index into A. */)
3522 (Lisp_Object a, Lisp_Object b, Lisp_Object i)
3524 EMACS_INT count;
3525 EMACS_INT nr_bits;
3526 int offset;
3527 bits_word *adata;
3528 bits_word twiddle;
3529 bits_word mword; /* Machine word. */
3530 ptrdiff_t pos, pos0;
3531 ptrdiff_t nr_words;
3533 CHECK_BOOL_VECTOR (a);
3534 CHECK_NATNUM (i);
3536 nr_bits = bool_vector_size (a);
3537 if (XFASTINT (i) > nr_bits) /* Allow one past the end for convenience */
3538 args_out_of_range (a, i);
3540 adata = bool_vector_data (a);
3541 nr_words = bool_vector_words (nr_bits);
3542 pos = XFASTINT (i) / BITS_PER_BITS_WORD;
3543 offset = XFASTINT (i) % BITS_PER_BITS_WORD;
3544 count = 0;
3546 /* By XORing with twiddle, we transform the problem of "count
3547 consecutive equal values" into "count the zero bits". The latter
3548 operation usually has hardware support. */
3549 twiddle = NILP (b) ? 0 : BITS_WORD_MAX;
3551 /* Scan the remainder of the mword at the current offset. */
3552 if (pos < nr_words && offset != 0)
3554 mword = bits_word_to_host_endian (adata[pos]);
3555 mword ^= twiddle;
3556 mword >>= offset;
3558 /* Do not count the pad bits. */
3559 mword |= (bits_word) 1 << (BITS_PER_BITS_WORD - offset);
3561 count = count_trailing_zero_bits (mword);
3562 pos++;
3563 if (count + offset < BITS_PER_BITS_WORD)
3564 return make_number (count);
3567 /* Scan whole words until we either reach the end of the vector or
3568 find an mword that doesn't completely match. twiddle is
3569 endian-independent. */
3570 pos0 = pos;
3571 while (pos < nr_words && adata[pos] == twiddle)
3572 pos++;
3573 count += (pos - pos0) * BITS_PER_BITS_WORD;
3575 if (pos < nr_words)
3577 /* If we stopped because of a mismatch, see how many bits match
3578 in the current mword. */
3579 mword = bits_word_to_host_endian (adata[pos]);
3580 mword ^= twiddle;
3581 count += count_trailing_zero_bits (mword);
3583 else if (nr_bits % BITS_PER_BITS_WORD != 0)
3585 /* If we hit the end, we might have overshot our count. Reduce
3586 the total by the number of spare bits at the end of the
3587 vector. */
3588 count -= BITS_PER_BITS_WORD - nr_bits % BITS_PER_BITS_WORD;
3591 return make_number (count);
3595 void
3596 syms_of_data (void)
3598 Lisp_Object error_tail, arith_tail;
3600 DEFSYM (Qquote, "quote");
3601 DEFSYM (Qlambda, "lambda");
3602 DEFSYM (Qsubr, "subr");
3603 DEFSYM (Qerror_conditions, "error-conditions");
3604 DEFSYM (Qerror_message, "error-message");
3605 DEFSYM (Qtop_level, "top-level");
3607 DEFSYM (Qerror, "error");
3608 DEFSYM (Quser_error, "user-error");
3609 DEFSYM (Qquit, "quit");
3610 DEFSYM (Qwrong_length_argument, "wrong-length-argument");
3611 DEFSYM (Qwrong_type_argument, "wrong-type-argument");
3612 DEFSYM (Qargs_out_of_range, "args-out-of-range");
3613 DEFSYM (Qvoid_function, "void-function");
3614 DEFSYM (Qcyclic_function_indirection, "cyclic-function-indirection");
3615 DEFSYM (Qcyclic_variable_indirection, "cyclic-variable-indirection");
3616 DEFSYM (Qvoid_variable, "void-variable");
3617 DEFSYM (Qsetting_constant, "setting-constant");
3618 DEFSYM (Qtrapping_constant, "trapping-constant");
3619 DEFSYM (Qtrapping_frame_local, "trapping-frame-local");
3620 DEFSYM (Qinvalid_read_syntax, "invalid-read-syntax");
3622 DEFSYM (Qinvalid_function, "invalid-function");
3623 DEFSYM (Qwrong_number_of_arguments, "wrong-number-of-arguments");
3624 DEFSYM (Qno_catch, "no-catch");
3625 DEFSYM (Qend_of_file, "end-of-file");
3626 DEFSYM (Qarith_error, "arith-error");
3627 DEFSYM (Qbeginning_of_buffer, "beginning-of-buffer");
3628 DEFSYM (Qend_of_buffer, "end-of-buffer");
3629 DEFSYM (Qbuffer_read_only, "buffer-read-only");
3630 DEFSYM (Qtext_read_only, "text-read-only");
3631 DEFSYM (Qmark_inactive, "mark-inactive");
3633 DEFSYM (Qlistp, "listp");
3634 DEFSYM (Qconsp, "consp");
3635 DEFSYM (Qsymbolp, "symbolp");
3636 DEFSYM (Qintegerp, "integerp");
3637 DEFSYM (Qnatnump, "natnump");
3638 DEFSYM (Qwholenump, "wholenump");
3639 DEFSYM (Qstringp, "stringp");
3640 DEFSYM (Qarrayp, "arrayp");
3641 DEFSYM (Qsequencep, "sequencep");
3642 DEFSYM (Qbufferp, "bufferp");
3643 DEFSYM (Qvectorp, "vectorp");
3644 DEFSYM (Qbool_vector_p, "bool-vector-p");
3645 DEFSYM (Qchar_or_string_p, "char-or-string-p");
3646 DEFSYM (Qmarkerp, "markerp");
3647 #ifdef HAVE_MODULES
3648 DEFSYM (Quser_ptrp, "user-ptrp");
3649 #endif
3650 DEFSYM (Qbuffer_or_string_p, "buffer-or-string-p");
3651 DEFSYM (Qinteger_or_marker_p, "integer-or-marker-p");
3652 DEFSYM (Qfboundp, "fboundp");
3654 DEFSYM (Qfloatp, "floatp");
3655 DEFSYM (Qnumberp, "numberp");
3656 DEFSYM (Qnumber_or_marker_p, "number-or-marker-p");
3658 DEFSYM (Qchar_table_p, "char-table-p");
3659 DEFSYM (Qvector_or_char_table_p, "vector-or-char-table-p");
3661 DEFSYM (Qsubrp, "subrp");
3662 DEFSYM (Qunevalled, "unevalled");
3663 DEFSYM (Qmany, "many");
3665 DEFSYM (Qcdr, "cdr");
3667 error_tail = pure_cons (Qerror, Qnil);
3669 /* ERROR is used as a signaler for random errors for which nothing else is
3670 right. */
3672 Fput (Qerror, Qerror_conditions,
3673 error_tail);
3674 Fput (Qerror, Qerror_message,
3675 build_pure_c_string ("error"));
3677 #define PUT_ERROR(sym, tail, msg) \
3678 Fput (sym, Qerror_conditions, pure_cons (sym, tail)); \
3679 Fput (sym, Qerror_message, build_pure_c_string (msg))
3681 PUT_ERROR (Qquit, Qnil, "Quit");
3683 PUT_ERROR (Quser_error, error_tail, "");
3684 PUT_ERROR (Qwrong_length_argument, error_tail, "Wrong length argument");
3685 PUT_ERROR (Qwrong_type_argument, error_tail, "Wrong type argument");
3686 PUT_ERROR (Qargs_out_of_range, error_tail, "Args out of range");
3687 PUT_ERROR (Qvoid_function, error_tail,
3688 "Symbol's function definition is void");
3689 PUT_ERROR (Qcyclic_function_indirection, error_tail,
3690 "Symbol's chain of function indirections contains a loop");
3691 PUT_ERROR (Qcyclic_variable_indirection, error_tail,
3692 "Symbol's chain of variable indirections contains a loop");
3693 DEFSYM (Qcircular_list, "circular-list");
3694 PUT_ERROR (Qcircular_list, error_tail, "List contains a loop");
3695 PUT_ERROR (Qvoid_variable, error_tail, "Symbol's value as variable is void");
3696 PUT_ERROR (Qsetting_constant, error_tail,
3697 "Attempt to set a constant symbol");
3698 PUT_ERROR (Qtrapping_constant, error_tail,
3699 "Attempt to trap writes to a constant symbol");
3700 PUT_ERROR (Qtrapping_frame_local, error_tail,
3701 "Attempt to trap writes to a frame local variable");
3702 PUT_ERROR (Qinvalid_read_syntax, error_tail, "Invalid read syntax");
3703 PUT_ERROR (Qinvalid_function, error_tail, "Invalid function");
3704 PUT_ERROR (Qwrong_number_of_arguments, error_tail,
3705 "Wrong number of arguments");
3706 PUT_ERROR (Qno_catch, error_tail, "No catch for tag");
3707 PUT_ERROR (Qend_of_file, error_tail, "End of file during parsing");
3709 arith_tail = pure_cons (Qarith_error, error_tail);
3710 Fput (Qarith_error, Qerror_conditions, arith_tail);
3711 Fput (Qarith_error, Qerror_message, build_pure_c_string ("Arithmetic error"));
3713 PUT_ERROR (Qbeginning_of_buffer, error_tail, "Beginning of buffer");
3714 PUT_ERROR (Qend_of_buffer, error_tail, "End of buffer");
3715 PUT_ERROR (Qbuffer_read_only, error_tail, "Buffer is read-only");
3716 PUT_ERROR (Qtext_read_only, pure_cons (Qbuffer_read_only, error_tail),
3717 "Text is read-only");
3719 DEFSYM (Qrange_error, "range-error");
3720 DEFSYM (Qdomain_error, "domain-error");
3721 DEFSYM (Qsingularity_error, "singularity-error");
3722 DEFSYM (Qoverflow_error, "overflow-error");
3723 DEFSYM (Qunderflow_error, "underflow-error");
3725 PUT_ERROR (Qdomain_error, arith_tail, "Arithmetic domain error");
3727 PUT_ERROR (Qrange_error, arith_tail, "Arithmetic range error");
3729 PUT_ERROR (Qsingularity_error, Fcons (Qdomain_error, arith_tail),
3730 "Arithmetic singularity error");
3732 PUT_ERROR (Qoverflow_error, Fcons (Qdomain_error, arith_tail),
3733 "Arithmetic overflow error");
3734 PUT_ERROR (Qunderflow_error, Fcons (Qdomain_error, arith_tail),
3735 "Arithmetic underflow error");
3737 /* Types that type-of returns. */
3738 DEFSYM (Qinteger, "integer");
3739 DEFSYM (Qsymbol, "symbol");
3740 DEFSYM (Qstring, "string");
3741 DEFSYM (Qcons, "cons");
3742 DEFSYM (Qmarker, "marker");
3743 DEFSYM (Qoverlay, "overlay");
3744 DEFSYM (Qfinalizer, "finalizer");
3745 #ifdef HAVE_MODULES
3746 DEFSYM (Quser_ptr, "user-ptr");
3747 #endif
3748 DEFSYM (Qfloat, "float");
3749 DEFSYM (Qwindow_configuration, "window-configuration");
3750 DEFSYM (Qprocess, "process");
3751 DEFSYM (Qwindow, "window");
3752 DEFSYM (Qcompiled_function, "compiled-function");
3753 DEFSYM (Qbuffer, "buffer");
3754 DEFSYM (Qframe, "frame");
3755 DEFSYM (Qvector, "vector");
3756 DEFSYM (Qchar_table, "char-table");
3757 DEFSYM (Qbool_vector, "bool-vector");
3758 DEFSYM (Qhash_table, "hash-table");
3760 DEFSYM (Qdefun, "defun");
3762 DEFSYM (Qfont_spec, "font-spec");
3763 DEFSYM (Qfont_entity, "font-entity");
3764 DEFSYM (Qfont_object, "font-object");
3766 DEFSYM (Qinteractive_form, "interactive-form");
3767 DEFSYM (Qdefalias_fset_function, "defalias-fset-function");
3769 defsubr (&Sindirect_variable);
3770 defsubr (&Sinteractive_form);
3771 defsubr (&Seq);
3772 defsubr (&Snull);
3773 defsubr (&Stype_of);
3774 defsubr (&Slistp);
3775 defsubr (&Snlistp);
3776 defsubr (&Sconsp);
3777 defsubr (&Satom);
3778 defsubr (&Sintegerp);
3779 defsubr (&Sinteger_or_marker_p);
3780 defsubr (&Snumberp);
3781 defsubr (&Snumber_or_marker_p);
3782 defsubr (&Sfloatp);
3783 defsubr (&Snatnump);
3784 defsubr (&Ssymbolp);
3785 defsubr (&Skeywordp);
3786 defsubr (&Sstringp);
3787 defsubr (&Smultibyte_string_p);
3788 defsubr (&Svectorp);
3789 defsubr (&Schar_table_p);
3790 defsubr (&Svector_or_char_table_p);
3791 defsubr (&Sbool_vector_p);
3792 defsubr (&Sarrayp);
3793 defsubr (&Ssequencep);
3794 defsubr (&Sbufferp);
3795 defsubr (&Smarkerp);
3796 defsubr (&Ssubrp);
3797 defsubr (&Sbyte_code_function_p);
3798 defsubr (&Schar_or_string_p);
3799 defsubr (&Scar);
3800 defsubr (&Scdr);
3801 defsubr (&Scar_safe);
3802 defsubr (&Scdr_safe);
3803 defsubr (&Ssetcar);
3804 defsubr (&Ssetcdr);
3805 defsubr (&Ssymbol_function);
3806 defsubr (&Sindirect_function);
3807 defsubr (&Ssymbol_plist);
3808 defsubr (&Ssymbol_name);
3809 defsubr (&Smakunbound);
3810 defsubr (&Sfmakunbound);
3811 defsubr (&Sboundp);
3812 defsubr (&Sfboundp);
3813 defsubr (&Sfset);
3814 defsubr (&Sdefalias);
3815 defsubr (&Ssetplist);
3816 defsubr (&Ssymbol_value);
3817 defsubr (&Sset);
3818 defsubr (&Sdefault_boundp);
3819 defsubr (&Sdefault_value);
3820 defsubr (&Sset_default);
3821 defsubr (&Ssetq_default);
3822 defsubr (&Smake_variable_buffer_local);
3823 defsubr (&Smake_local_variable);
3824 defsubr (&Skill_local_variable);
3825 defsubr (&Smake_variable_frame_local);
3826 defsubr (&Slocal_variable_p);
3827 defsubr (&Slocal_variable_if_set_p);
3828 defsubr (&Svariable_binding_locus);
3829 #if 0 /* XXX Remove this. --lorentey */
3830 defsubr (&Sterminal_local_value);
3831 defsubr (&Sset_terminal_local_value);
3832 #endif
3833 defsubr (&Saref);
3834 defsubr (&Saset);
3835 defsubr (&Snumber_to_string);
3836 defsubr (&Sstring_to_number);
3837 defsubr (&Seqlsign);
3838 defsubr (&Slss);
3839 defsubr (&Sgtr);
3840 defsubr (&Sleq);
3841 defsubr (&Sgeq);
3842 defsubr (&Sneq);
3843 defsubr (&Splus);
3844 defsubr (&Sminus);
3845 defsubr (&Stimes);
3846 defsubr (&Squo);
3847 defsubr (&Srem);
3848 defsubr (&Smod);
3849 defsubr (&Smax);
3850 defsubr (&Smin);
3851 defsubr (&Slogand);
3852 defsubr (&Slogior);
3853 defsubr (&Slogxor);
3854 defsubr (&Slsh);
3855 defsubr (&Sash);
3856 defsubr (&Sadd1);
3857 defsubr (&Ssub1);
3858 defsubr (&Slognot);
3859 defsubr (&Sbyteorder);
3860 defsubr (&Ssubr_arity);
3861 defsubr (&Ssubr_name);
3862 #ifdef HAVE_MODULES
3863 defsubr (&Suser_ptrp);
3864 #endif
3866 defsubr (&Sbool_vector_exclusive_or);
3867 defsubr (&Sbool_vector_union);
3868 defsubr (&Sbool_vector_intersection);
3869 defsubr (&Sbool_vector_set_difference);
3870 defsubr (&Sbool_vector_not);
3871 defsubr (&Sbool_vector_subsetp);
3872 defsubr (&Sbool_vector_count_consecutive);
3873 defsubr (&Sbool_vector_count_population);
3875 set_symbol_function (Qwholenump, XSYMBOL (Qnatnump)->function);
3877 DEFVAR_LISP ("most-positive-fixnum", Vmost_positive_fixnum,
3878 doc: /* The largest value that is representable in a Lisp integer. */);
3879 Vmost_positive_fixnum = make_number (MOST_POSITIVE_FIXNUM);
3880 make_symbol_constant (intern_c_string ("most-positive-fixnum"));
3882 DEFVAR_LISP ("most-negative-fixnum", Vmost_negative_fixnum,
3883 doc: /* The smallest value that is representable in a Lisp integer. */);
3884 Vmost_negative_fixnum = make_number (MOST_NEGATIVE_FIXNUM);
3885 make_symbol_constant (intern_c_string ("most-negative-fixnum"));
3887 DEFSYM (Qwatchers, "watchers");
3888 DEFSYM (Qmakunbound, "makunbound");
3889 DEFSYM (Qunlet, "unlet");
3890 DEFSYM (Qset, "set");
3891 DEFSYM (Qset_default, "set-default");
3892 defsubr (&Sadd_variable_watcher);
3893 defsubr (&Sremove_variable_watcher);
3894 defsubr (&Sget_variable_watchers);