Fix linum under text-scaling when leuven-theme is used
[emacs.git] / src / data.c
blob25859105ee04d10d1403a6c496755a25f4ea357b
1 /* Primitive operations on Lisp data types for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985-1986, 1988, 1993-1995, 1997-2017 Free Software
3 Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or (at
10 your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
23 #include <math.h>
24 #include <stdio.h>
26 #include <byteswap.h>
27 #include <count-one-bits.h>
28 #include <count-trailing-zeros.h>
29 #include <intprops.h>
31 #include "lisp.h"
32 #include "puresize.h"
33 #include "character.h"
34 #include "buffer.h"
35 #include "keyboard.h"
36 #include "process.h"
37 #include "frame.h"
38 #include "keymap.h"
40 static void swap_in_symval_forwarding (struct Lisp_Symbol *,
41 struct Lisp_Buffer_Local_Value *);
43 static bool
44 BOOLFWDP (union Lisp_Fwd *a)
46 return XFWDTYPE (a) == Lisp_Fwd_Bool;
48 static bool
49 INTFWDP (union Lisp_Fwd *a)
51 return XFWDTYPE (a) == Lisp_Fwd_Int;
53 static bool
54 KBOARD_OBJFWDP (union Lisp_Fwd *a)
56 return XFWDTYPE (a) == Lisp_Fwd_Kboard_Obj;
58 static bool
59 OBJFWDP (union Lisp_Fwd *a)
61 return XFWDTYPE (a) == Lisp_Fwd_Obj;
64 static struct Lisp_Boolfwd *
65 XBOOLFWD (union Lisp_Fwd *a)
67 eassert (BOOLFWDP (a));
68 return &a->u_boolfwd;
70 static struct Lisp_Kboard_Objfwd *
71 XKBOARD_OBJFWD (union Lisp_Fwd *a)
73 eassert (KBOARD_OBJFWDP (a));
74 return &a->u_kboard_objfwd;
76 static struct Lisp_Intfwd *
77 XINTFWD (union Lisp_Fwd *a)
79 eassert (INTFWDP (a));
80 return &a->u_intfwd;
82 static struct Lisp_Objfwd *
83 XOBJFWD (union Lisp_Fwd *a)
85 eassert (OBJFWDP (a));
86 return &a->u_objfwd;
89 static void
90 CHECK_SUBR (Lisp_Object x)
92 CHECK_TYPE (SUBRP (x), Qsubrp, x);
95 static void
96 set_blv_found (struct Lisp_Buffer_Local_Value *blv, int found)
98 eassert (found == !EQ (blv->defcell, blv->valcell));
99 blv->found = found;
102 static Lisp_Object
103 blv_value (struct Lisp_Buffer_Local_Value *blv)
105 return XCDR (blv->valcell);
108 static void
109 set_blv_value (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
111 XSETCDR (blv->valcell, val);
114 static void
115 set_blv_where (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
117 blv->where = val;
120 static void
121 set_blv_defcell (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
123 blv->defcell = val;
126 static void
127 set_blv_valcell (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
129 blv->valcell = val;
132 static _Noreturn void
133 wrong_length_argument (Lisp_Object a1, Lisp_Object a2, Lisp_Object a3)
135 Lisp_Object size1 = make_number (bool_vector_size (a1));
136 Lisp_Object size2 = make_number (bool_vector_size (a2));
137 if (NILP (a3))
138 xsignal2 (Qwrong_length_argument, size1, size2);
139 else
140 xsignal3 (Qwrong_length_argument, size1, size2,
141 make_number (bool_vector_size (a3)));
144 _Noreturn void
145 wrong_type_argument (register Lisp_Object predicate, register Lisp_Object value)
147 /* If VALUE is not even a valid Lisp object, we'd want to abort here
148 where we can get a backtrace showing where it came from. We used
149 to try and do that by checking the tagbits, but nowadays all
150 tagbits are potentially valid. */
151 /* if ((unsigned int) XTYPE (value) >= Lisp_Type_Limit)
152 * emacs_abort (); */
154 xsignal2 (Qwrong_type_argument, predicate, value);
157 void
158 pure_write_error (Lisp_Object obj)
160 xsignal2 (Qerror, build_string ("Attempt to modify read-only object"), obj);
163 void
164 args_out_of_range (Lisp_Object a1, Lisp_Object a2)
166 xsignal2 (Qargs_out_of_range, a1, a2);
169 void
170 args_out_of_range_3 (Lisp_Object a1, Lisp_Object a2, Lisp_Object a3)
172 xsignal3 (Qargs_out_of_range, a1, a2, a3);
175 void
176 circular_list (Lisp_Object list)
178 xsignal1 (Qcircular_list, list);
182 /* Data type predicates. */
184 DEFUN ("eq", Feq, Seq, 2, 2, 0,
185 doc: /* Return t if the two args are the same Lisp object. */
186 attributes: const)
187 (Lisp_Object obj1, Lisp_Object obj2)
189 if (EQ (obj1, obj2))
190 return Qt;
191 return Qnil;
194 DEFUN ("null", Fnull, Snull, 1, 1, 0,
195 doc: /* Return t if OBJECT is nil, and return nil otherwise. */
196 attributes: const)
197 (Lisp_Object object)
199 if (NILP (object))
200 return Qt;
201 return Qnil;
204 DEFUN ("type-of", Ftype_of, Stype_of, 1, 1, 0,
205 doc: /* Return a symbol representing the type of OBJECT.
206 The symbol returned names the object's basic type;
207 for example, (type-of 1) returns `integer'. */)
208 (Lisp_Object object)
210 switch (XTYPE (object))
212 case_Lisp_Int:
213 return Qinteger;
215 case Lisp_Symbol:
216 return Qsymbol;
218 case Lisp_String:
219 return Qstring;
221 case Lisp_Cons:
222 return Qcons;
224 case Lisp_Misc:
225 switch (XMISCTYPE (object))
227 case Lisp_Misc_Marker:
228 return Qmarker;
229 case Lisp_Misc_Overlay:
230 return Qoverlay;
231 case Lisp_Misc_Float:
232 return Qfloat;
233 case Lisp_Misc_Finalizer:
234 return Qfinalizer;
235 #ifdef HAVE_MODULES
236 case Lisp_Misc_User_Ptr:
237 return Quser_ptr;
238 #endif
239 default:
240 emacs_abort ();
243 case Lisp_Vectorlike:
244 switch (PSEUDOVECTOR_TYPE (XVECTOR (object)))
246 case PVEC_NORMAL_VECTOR: return Qvector;
247 case PVEC_WINDOW_CONFIGURATION: return Qwindow_configuration;
248 case PVEC_PROCESS: return Qprocess;
249 case PVEC_WINDOW: return Qwindow;
250 case PVEC_SUBR: return Qsubr;
251 case PVEC_COMPILED: return Qcompiled_function;
252 case PVEC_BUFFER: return Qbuffer;
253 case PVEC_CHAR_TABLE: return Qchar_table;
254 case PVEC_BOOL_VECTOR: return Qbool_vector;
255 case PVEC_FRAME: return Qframe;
256 case PVEC_HASH_TABLE: return Qhash_table;
257 case PVEC_FONT:
258 if (FONT_SPEC_P (object))
259 return Qfont_spec;
260 if (FONT_ENTITY_P (object))
261 return Qfont_entity;
262 if (FONT_OBJECT_P (object))
263 return Qfont_object;
264 else
265 emacs_abort (); /* return Qfont? */
266 case PVEC_THREAD: return Qthread;
267 case PVEC_MUTEX: return Qmutex;
268 case PVEC_CONDVAR: return Qcondition_variable;
269 case PVEC_TERMINAL: return Qterminal;
270 case PVEC_RECORD:
272 Lisp_Object t = AREF (object, 0);
273 if (RECORDP (t) && 1 < PVSIZE (t))
274 /* Return the type name field of the class! */
275 return AREF (t, 1);
276 else
277 return t;
279 case PVEC_MODULE_FUNCTION:
280 return Qmodule_function;
281 /* "Impossible" cases. */
282 case PVEC_XWIDGET:
283 case PVEC_OTHER:
284 case PVEC_XWIDGET_VIEW:
285 case PVEC_SUB_CHAR_TABLE:
286 case PVEC_FREE: ;
288 emacs_abort ();
290 case Lisp_Float:
291 return Qfloat;
293 default:
294 emacs_abort ();
298 DEFUN ("consp", Fconsp, Sconsp, 1, 1, 0,
299 doc: /* Return t if OBJECT is a cons cell. */
300 attributes: const)
301 (Lisp_Object object)
303 if (CONSP (object))
304 return Qt;
305 return Qnil;
308 DEFUN ("atom", Fatom, Satom, 1, 1, 0,
309 doc: /* Return t if OBJECT is not a cons cell. This includes nil. */
310 attributes: const)
311 (Lisp_Object object)
313 if (CONSP (object))
314 return Qnil;
315 return Qt;
318 DEFUN ("listp", Flistp, Slistp, 1, 1, 0,
319 doc: /* Return t if OBJECT is a list, that is, a cons cell or nil.
320 Otherwise, return nil. */
321 attributes: const)
322 (Lisp_Object object)
324 if (CONSP (object) || NILP (object))
325 return Qt;
326 return Qnil;
329 DEFUN ("nlistp", Fnlistp, Snlistp, 1, 1, 0,
330 doc: /* Return t if OBJECT is not a list. Lists include nil. */
331 attributes: const)
332 (Lisp_Object object)
334 if (CONSP (object) || NILP (object))
335 return Qnil;
336 return Qt;
339 DEFUN ("symbolp", Fsymbolp, Ssymbolp, 1, 1, 0,
340 doc: /* Return t if OBJECT is a symbol. */
341 attributes: const)
342 (Lisp_Object object)
344 if (SYMBOLP (object))
345 return Qt;
346 return Qnil;
349 /* Define this in C to avoid unnecessarily consing up the symbol
350 name. */
351 DEFUN ("keywordp", Fkeywordp, Skeywordp, 1, 1, 0,
352 doc: /* Return t if OBJECT is a keyword.
353 This means that it is a symbol with a print name beginning with `:'
354 interned in the initial obarray. */)
355 (Lisp_Object object)
357 if (SYMBOLP (object)
358 && SREF (SYMBOL_NAME (object), 0) == ':'
359 && SYMBOL_INTERNED_IN_INITIAL_OBARRAY_P (object))
360 return Qt;
361 return Qnil;
364 DEFUN ("vectorp", Fvectorp, Svectorp, 1, 1, 0,
365 doc: /* Return t if OBJECT is a vector. */)
366 (Lisp_Object object)
368 if (VECTORP (object))
369 return Qt;
370 return Qnil;
373 DEFUN ("recordp", Frecordp, Srecordp, 1, 1, 0,
374 doc: /* Return t if OBJECT is a record. */)
375 (Lisp_Object object)
377 if (RECORDP (object))
378 return Qt;
379 return Qnil;
382 DEFUN ("stringp", Fstringp, Sstringp, 1, 1, 0,
383 doc: /* Return t if OBJECT is a string. */
384 attributes: const)
385 (Lisp_Object object)
387 if (STRINGP (object))
388 return Qt;
389 return Qnil;
392 DEFUN ("multibyte-string-p", Fmultibyte_string_p, Smultibyte_string_p,
393 1, 1, 0,
394 doc: /* Return t if OBJECT is a multibyte string.
395 Return nil if OBJECT is either a unibyte string, or not a string. */)
396 (Lisp_Object object)
398 if (STRINGP (object) && STRING_MULTIBYTE (object))
399 return Qt;
400 return Qnil;
403 DEFUN ("char-table-p", Fchar_table_p, Schar_table_p, 1, 1, 0,
404 doc: /* Return t if OBJECT is a char-table. */)
405 (Lisp_Object object)
407 if (CHAR_TABLE_P (object))
408 return Qt;
409 return Qnil;
412 DEFUN ("vector-or-char-table-p", Fvector_or_char_table_p,
413 Svector_or_char_table_p, 1, 1, 0,
414 doc: /* Return t if OBJECT is a char-table or vector. */)
415 (Lisp_Object object)
417 if (VECTORP (object) || CHAR_TABLE_P (object))
418 return Qt;
419 return Qnil;
422 DEFUN ("bool-vector-p", Fbool_vector_p, Sbool_vector_p, 1, 1, 0,
423 doc: /* Return t if OBJECT is a bool-vector. */)
424 (Lisp_Object object)
426 if (BOOL_VECTOR_P (object))
427 return Qt;
428 return Qnil;
431 DEFUN ("arrayp", Farrayp, Sarrayp, 1, 1, 0,
432 doc: /* Return t if OBJECT is an array (string or vector). */)
433 (Lisp_Object object)
435 if (ARRAYP (object))
436 return Qt;
437 return Qnil;
440 DEFUN ("sequencep", Fsequencep, Ssequencep, 1, 1, 0,
441 doc: /* Return t if OBJECT is a sequence (list or array). */)
442 (register Lisp_Object object)
444 if (CONSP (object) || NILP (object) || ARRAYP (object))
445 return Qt;
446 return Qnil;
449 DEFUN ("bufferp", Fbufferp, Sbufferp, 1, 1, 0,
450 doc: /* Return t if OBJECT is an editor buffer. */)
451 (Lisp_Object object)
453 if (BUFFERP (object))
454 return Qt;
455 return Qnil;
458 DEFUN ("markerp", Fmarkerp, Smarkerp, 1, 1, 0,
459 doc: /* Return t if OBJECT is a marker (editor pointer). */)
460 (Lisp_Object object)
462 if (MARKERP (object))
463 return Qt;
464 return Qnil;
467 #ifdef HAVE_MODULES
468 DEFUN ("user-ptrp", Fuser_ptrp, Suser_ptrp, 1, 1, 0,
469 doc: /* Return t if OBJECT is a module user pointer. */)
470 (Lisp_Object object)
472 if (USER_PTRP (object))
473 return Qt;
474 return Qnil;
476 #endif
478 DEFUN ("subrp", Fsubrp, Ssubrp, 1, 1, 0,
479 doc: /* Return t if OBJECT is a built-in function. */)
480 (Lisp_Object object)
482 if (SUBRP (object))
483 return Qt;
484 return Qnil;
487 DEFUN ("byte-code-function-p", Fbyte_code_function_p, Sbyte_code_function_p,
488 1, 1, 0,
489 doc: /* Return t if OBJECT is a byte-compiled function object. */)
490 (Lisp_Object object)
492 if (COMPILEDP (object))
493 return Qt;
494 return Qnil;
497 DEFUN ("module-function-p", Fmodule_function_p, Smodule_function_p, 1, 1, NULL,
498 doc: /* Return t if OBJECT is a function loaded from a dynamic module. */
499 attributes: const)
500 (Lisp_Object object)
502 return MODULE_FUNCTIONP (object) ? Qt : Qnil;
505 DEFUN ("char-or-string-p", Fchar_or_string_p, Schar_or_string_p, 1, 1, 0,
506 doc: /* Return t if OBJECT is a character or a string. */
507 attributes: const)
508 (register Lisp_Object object)
510 if (CHARACTERP (object) || STRINGP (object))
511 return Qt;
512 return Qnil;
515 DEFUN ("integerp", Fintegerp, Sintegerp, 1, 1, 0,
516 doc: /* Return t if OBJECT is an integer. */
517 attributes: const)
518 (Lisp_Object object)
520 if (INTEGERP (object))
521 return Qt;
522 return Qnil;
525 DEFUN ("integer-or-marker-p", Finteger_or_marker_p, Sinteger_or_marker_p, 1, 1, 0,
526 doc: /* Return t if OBJECT is an integer or a marker (editor pointer). */)
527 (register Lisp_Object object)
529 if (MARKERP (object) || INTEGERP (object))
530 return Qt;
531 return Qnil;
534 DEFUN ("natnump", Fnatnump, Snatnump, 1, 1, 0,
535 doc: /* Return t if OBJECT is a nonnegative integer. */
536 attributes: const)
537 (Lisp_Object object)
539 if (NATNUMP (object))
540 return Qt;
541 return Qnil;
544 DEFUN ("numberp", Fnumberp, Snumberp, 1, 1, 0,
545 doc: /* Return t if OBJECT is a number (floating point or integer). */
546 attributes: const)
547 (Lisp_Object object)
549 if (NUMBERP (object))
550 return Qt;
551 else
552 return Qnil;
555 DEFUN ("number-or-marker-p", Fnumber_or_marker_p,
556 Snumber_or_marker_p, 1, 1, 0,
557 doc: /* Return t if OBJECT is a number or a marker. */)
558 (Lisp_Object object)
560 if (NUMBERP (object) || MARKERP (object))
561 return Qt;
562 return Qnil;
565 DEFUN ("floatp", Ffloatp, Sfloatp, 1, 1, 0,
566 doc: /* Return t if OBJECT is a floating point number. */
567 attributes: const)
568 (Lisp_Object object)
570 if (FLOATP (object))
571 return Qt;
572 return Qnil;
575 DEFUN ("threadp", Fthreadp, Sthreadp, 1, 1, 0,
576 doc: /* Return t if OBJECT is a thread. */)
577 (Lisp_Object object)
579 if (THREADP (object))
580 return Qt;
581 return Qnil;
584 DEFUN ("mutexp", Fmutexp, Smutexp, 1, 1, 0,
585 doc: /* Return t if OBJECT is a mutex. */)
586 (Lisp_Object object)
588 if (MUTEXP (object))
589 return Qt;
590 return Qnil;
593 DEFUN ("condition-variable-p", Fcondition_variable_p, Scondition_variable_p,
594 1, 1, 0,
595 doc: /* Return t if OBJECT is a condition variable. */)
596 (Lisp_Object object)
598 if (CONDVARP (object))
599 return Qt;
600 return Qnil;
603 /* Extract and set components of lists. */
605 DEFUN ("car", Fcar, Scar, 1, 1, 0,
606 doc: /* Return the car of LIST. If arg is nil, return nil.
607 Error if arg is not nil and not a cons cell. See also `car-safe'.
609 See Info node `(elisp)Cons Cells' for a discussion of related basic
610 Lisp concepts such as car, cdr, cons cell and list. */)
611 (register Lisp_Object list)
613 return CAR (list);
616 DEFUN ("car-safe", Fcar_safe, Scar_safe, 1, 1, 0,
617 doc: /* Return the car of OBJECT if it is a cons cell, or else nil. */)
618 (Lisp_Object object)
620 return CAR_SAFE (object);
623 DEFUN ("cdr", Fcdr, Scdr, 1, 1, 0,
624 doc: /* Return the cdr of LIST. If arg is nil, return nil.
625 Error if arg is not nil and not a cons cell. See also `cdr-safe'.
627 See Info node `(elisp)Cons Cells' for a discussion of related basic
628 Lisp concepts such as cdr, car, cons cell and list. */)
629 (register Lisp_Object list)
631 return CDR (list);
634 DEFUN ("cdr-safe", Fcdr_safe, Scdr_safe, 1, 1, 0,
635 doc: /* Return the cdr of OBJECT if it is a cons cell, or else nil. */)
636 (Lisp_Object object)
638 return CDR_SAFE (object);
641 DEFUN ("setcar", Fsetcar, Ssetcar, 2, 2, 0,
642 doc: /* Set the car of CELL to be NEWCAR. Returns NEWCAR. */)
643 (register Lisp_Object cell, Lisp_Object newcar)
645 CHECK_CONS (cell);
646 CHECK_IMPURE (cell, XCONS (cell));
647 XSETCAR (cell, newcar);
648 return newcar;
651 DEFUN ("setcdr", Fsetcdr, Ssetcdr, 2, 2, 0,
652 doc: /* Set the cdr of CELL to be NEWCDR. Returns NEWCDR. */)
653 (register Lisp_Object cell, Lisp_Object newcdr)
655 CHECK_CONS (cell);
656 CHECK_IMPURE (cell, XCONS (cell));
657 XSETCDR (cell, newcdr);
658 return newcdr;
661 /* Extract and set components of symbols. */
663 DEFUN ("boundp", Fboundp, Sboundp, 1, 1, 0,
664 doc: /* Return t if SYMBOL's value is not void.
665 Note that if `lexical-binding' is in effect, this refers to the
666 global value outside of any lexical scope. */)
667 (register Lisp_Object symbol)
669 Lisp_Object valcontents;
670 struct Lisp_Symbol *sym;
671 CHECK_SYMBOL (symbol);
672 sym = XSYMBOL (symbol);
674 start:
675 switch (sym->redirect)
677 case SYMBOL_PLAINVAL: valcontents = SYMBOL_VAL (sym); break;
678 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
679 case SYMBOL_LOCALIZED:
681 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
682 if (blv->fwd)
683 /* In set_internal, we un-forward vars when their value is
684 set to Qunbound. */
685 return Qt;
686 else
688 swap_in_symval_forwarding (sym, blv);
689 valcontents = blv_value (blv);
691 break;
693 case SYMBOL_FORWARDED:
694 /* In set_internal, we un-forward vars when their value is
695 set to Qunbound. */
696 return Qt;
697 default: emacs_abort ();
700 return (EQ (valcontents, Qunbound) ? Qnil : Qt);
703 /* FIXME: It has been previously suggested to make this function an
704 alias for symbol-function, but upon discussion at Bug#23957,
705 there is a risk breaking backward compatibility, as some users of
706 fboundp may expect `t' in particular, rather than any true
707 value. An alias is still welcome so long as the compatibility
708 issues are addressed. */
709 DEFUN ("fboundp", Ffboundp, Sfboundp, 1, 1, 0,
710 doc: /* Return t if SYMBOL's function definition is not void. */)
711 (register Lisp_Object symbol)
713 CHECK_SYMBOL (symbol);
714 return NILP (XSYMBOL (symbol)->function) ? Qnil : Qt;
717 DEFUN ("makunbound", Fmakunbound, Smakunbound, 1, 1, 0,
718 doc: /* Make SYMBOL's value be void.
719 Return SYMBOL. */)
720 (register Lisp_Object symbol)
722 CHECK_SYMBOL (symbol);
723 if (SYMBOL_CONSTANT_P (symbol))
724 xsignal1 (Qsetting_constant, symbol);
725 Fset (symbol, Qunbound);
726 return symbol;
729 DEFUN ("fmakunbound", Ffmakunbound, Sfmakunbound, 1, 1, 0,
730 doc: /* Make SYMBOL's function definition be nil.
731 Return SYMBOL. */)
732 (register Lisp_Object symbol)
734 CHECK_SYMBOL (symbol);
735 if (NILP (symbol) || EQ (symbol, Qt))
736 xsignal1 (Qsetting_constant, symbol);
737 set_symbol_function (symbol, Qnil);
738 return symbol;
741 DEFUN ("symbol-function", Fsymbol_function, Ssymbol_function, 1, 1, 0,
742 doc: /* Return SYMBOL's function definition, or nil if that is void. */)
743 (register Lisp_Object symbol)
745 CHECK_SYMBOL (symbol);
746 return XSYMBOL (symbol)->function;
749 DEFUN ("symbol-plist", Fsymbol_plist, Ssymbol_plist, 1, 1, 0,
750 doc: /* Return SYMBOL's property list. */)
751 (register Lisp_Object symbol)
753 CHECK_SYMBOL (symbol);
754 return XSYMBOL (symbol)->plist;
757 DEFUN ("symbol-name", Fsymbol_name, Ssymbol_name, 1, 1, 0,
758 doc: /* Return SYMBOL's name, a string. */)
759 (register Lisp_Object symbol)
761 register Lisp_Object name;
763 CHECK_SYMBOL (symbol);
764 name = SYMBOL_NAME (symbol);
765 return name;
768 DEFUN ("fset", Ffset, Sfset, 2, 2, 0,
769 doc: /* Set SYMBOL's function definition to DEFINITION, and return DEFINITION. */)
770 (register Lisp_Object symbol, Lisp_Object definition)
772 register Lisp_Object function;
773 CHECK_SYMBOL (symbol);
774 /* Perhaps not quite the right error signal, but seems good enough. */
775 if (NILP (symbol))
776 xsignal1 (Qsetting_constant, symbol);
778 function = XSYMBOL (symbol)->function;
780 if (!NILP (Vautoload_queue) && !NILP (function))
781 Vautoload_queue = Fcons (Fcons (symbol, function), Vautoload_queue);
783 if (AUTOLOADP (function))
784 Fput (symbol, Qautoload, XCDR (function));
786 /* Convert to eassert or remove after GC bug is found. In the
787 meantime, check unconditionally, at a slight perf hit. */
788 if (! valid_lisp_object_p (definition))
789 emacs_abort ();
791 set_symbol_function (symbol, definition);
793 return definition;
796 DEFUN ("defalias", Fdefalias, Sdefalias, 2, 3, 0,
797 doc: /* Set SYMBOL's function definition to DEFINITION.
798 Associates the function with the current load file, if any.
799 The optional third argument DOCSTRING specifies the documentation string
800 for SYMBOL; if it is omitted or nil, SYMBOL uses the documentation string
801 determined by DEFINITION.
803 Internally, this normally uses `fset', but if SYMBOL has a
804 `defalias-fset-function' property, the associated value is used instead.
806 The return value is undefined. */)
807 (register Lisp_Object symbol, Lisp_Object definition, Lisp_Object docstring)
809 CHECK_SYMBOL (symbol);
810 if (!NILP (Vpurify_flag)
811 /* If `definition' is a keymap, immutable (and copying) is wrong. */
812 && !KEYMAPP (definition))
813 definition = Fpurecopy (definition);
816 bool autoload = AUTOLOADP (definition);
817 if (NILP (Vpurify_flag) || !autoload)
818 { /* Only add autoload entries after dumping, because the ones before are
819 not useful and else we get loads of them from the loaddefs.el. */
821 if (AUTOLOADP (XSYMBOL (symbol)->function))
822 /* Remember that the function was already an autoload. */
823 LOADHIST_ATTACH (Fcons (Qt, symbol));
824 LOADHIST_ATTACH (Fcons (autoload ? Qautoload : Qdefun, symbol));
828 { /* Handle automatic advice activation. */
829 Lisp_Object hook = Fget (symbol, Qdefalias_fset_function);
830 if (!NILP (hook))
831 call2 (hook, symbol, definition);
832 else
833 Ffset (symbol, definition);
836 if (!NILP (docstring))
837 Fput (symbol, Qfunction_documentation, docstring);
838 /* We used to return `definition', but now that `defun' and `defmacro' expand
839 to a call to `defalias', we return `symbol' for backward compatibility
840 (bug#11686). */
841 return symbol;
844 DEFUN ("setplist", Fsetplist, Ssetplist, 2, 2, 0,
845 doc: /* Set SYMBOL's property list to NEWPLIST, and return NEWPLIST. */)
846 (register Lisp_Object symbol, Lisp_Object newplist)
848 CHECK_SYMBOL (symbol);
849 set_symbol_plist (symbol, newplist);
850 return newplist;
853 DEFUN ("subr-arity", Fsubr_arity, Ssubr_arity, 1, 1, 0,
854 doc: /* Return minimum and maximum number of args allowed for SUBR.
855 SUBR must be a built-in function.
856 The returned value is a pair (MIN . MAX). MIN is the minimum number
857 of args. MAX is the maximum number or the symbol `many', for a
858 function with `&rest' args, or `unevalled' for a special form. */)
859 (Lisp_Object subr)
861 short minargs, maxargs;
862 CHECK_SUBR (subr);
863 minargs = XSUBR (subr)->min_args;
864 maxargs = XSUBR (subr)->max_args;
865 return Fcons (make_number (minargs),
866 maxargs == MANY ? Qmany
867 : maxargs == UNEVALLED ? Qunevalled
868 : make_number (maxargs));
871 DEFUN ("subr-name", Fsubr_name, Ssubr_name, 1, 1, 0,
872 doc: /* Return name of subroutine SUBR.
873 SUBR must be a built-in function. */)
874 (Lisp_Object subr)
876 const char *name;
877 CHECK_SUBR (subr);
878 name = XSUBR (subr)->symbol_name;
879 return build_string (name);
882 DEFUN ("interactive-form", Finteractive_form, Sinteractive_form, 1, 1, 0,
883 doc: /* Return the interactive form of CMD or nil if none.
884 If CMD is not a command, the return value is nil.
885 Value, if non-nil, is a list (interactive SPEC). */)
886 (Lisp_Object cmd)
888 Lisp_Object fun = indirect_function (cmd); /* Check cycles. */
890 if (NILP (fun))
891 return Qnil;
893 /* Use an `interactive-form' property if present, analogous to the
894 function-documentation property. */
895 fun = cmd;
896 while (SYMBOLP (fun))
898 Lisp_Object tmp = Fget (fun, Qinteractive_form);
899 if (!NILP (tmp))
900 return tmp;
901 else
902 fun = Fsymbol_function (fun);
905 if (SUBRP (fun))
907 const char *spec = XSUBR (fun)->intspec;
908 if (spec)
909 return list2 (Qinteractive,
910 (*spec != '(') ? build_string (spec) :
911 Fcar (Fread_from_string (build_string (spec), Qnil, Qnil)));
913 else if (COMPILEDP (fun))
915 if (PVSIZE (fun) > COMPILED_INTERACTIVE)
916 return list2 (Qinteractive, AREF (fun, COMPILED_INTERACTIVE));
918 else if (AUTOLOADP (fun))
919 return Finteractive_form (Fautoload_do_load (fun, cmd, Qnil));
920 else if (CONSP (fun))
922 Lisp_Object funcar = XCAR (fun);
923 if (EQ (funcar, Qclosure))
924 return Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun))));
925 else if (EQ (funcar, Qlambda))
926 return Fassq (Qinteractive, Fcdr (XCDR (fun)));
928 return Qnil;
932 /***********************************************************************
933 Getting and Setting Values of Symbols
934 ***********************************************************************/
936 /* Return the symbol holding SYMBOL's value. Signal
937 `cyclic-variable-indirection' if SYMBOL's chain of variable
938 indirections contains a loop. */
940 struct Lisp_Symbol *
941 indirect_variable (struct Lisp_Symbol *symbol)
943 struct Lisp_Symbol *tortoise, *hare;
945 hare = tortoise = symbol;
947 while (hare->redirect == SYMBOL_VARALIAS)
949 hare = SYMBOL_ALIAS (hare);
950 if (hare->redirect != SYMBOL_VARALIAS)
951 break;
953 hare = SYMBOL_ALIAS (hare);
954 tortoise = SYMBOL_ALIAS (tortoise);
956 if (hare == tortoise)
958 Lisp_Object tem;
959 XSETSYMBOL (tem, symbol);
960 xsignal1 (Qcyclic_variable_indirection, tem);
964 return hare;
968 DEFUN ("indirect-variable", Findirect_variable, Sindirect_variable, 1, 1, 0,
969 doc: /* Return the variable at the end of OBJECT's variable chain.
970 If OBJECT is a symbol, follow its variable indirections (if any), and
971 return the variable at the end of the chain of aliases. See Info node
972 `(elisp)Variable Aliases'.
974 If OBJECT is not a symbol, just return it. If there is a loop in the
975 chain of aliases, signal a `cyclic-variable-indirection' error. */)
976 (Lisp_Object object)
978 if (SYMBOLP (object))
980 struct Lisp_Symbol *sym = indirect_variable (XSYMBOL (object));
981 XSETSYMBOL (object, sym);
983 return object;
987 /* Given the raw contents of a symbol value cell,
988 return the Lisp value of the symbol.
989 This does not handle buffer-local variables; use
990 swap_in_symval_forwarding for that. */
992 Lisp_Object
993 do_symval_forwarding (register union Lisp_Fwd *valcontents)
995 register Lisp_Object val;
996 switch (XFWDTYPE (valcontents))
998 case Lisp_Fwd_Int:
999 XSETINT (val, *XINTFWD (valcontents)->intvar);
1000 return val;
1002 case Lisp_Fwd_Bool:
1003 return (*XBOOLFWD (valcontents)->boolvar ? Qt : Qnil);
1005 case Lisp_Fwd_Obj:
1006 return *XOBJFWD (valcontents)->objvar;
1008 case Lisp_Fwd_Buffer_Obj:
1009 return per_buffer_value (current_buffer,
1010 XBUFFER_OBJFWD (valcontents)->offset);
1012 case Lisp_Fwd_Kboard_Obj:
1013 /* We used to simply use current_kboard here, but from Lisp
1014 code, its value is often unexpected. It seems nicer to
1015 allow constructions like this to work as intuitively expected:
1017 (with-selected-frame frame
1018 (define-key local-function-map "\eOP" [f1]))
1020 On the other hand, this affects the semantics of
1021 last-command and real-last-command, and people may rely on
1022 that. I took a quick look at the Lisp codebase, and I
1023 don't think anything will break. --lorentey */
1024 return *(Lisp_Object *)(XKBOARD_OBJFWD (valcontents)->offset
1025 + (char *)FRAME_KBOARD (SELECTED_FRAME ()));
1026 default: emacs_abort ();
1030 /* Used to signal a user-friendly error when symbol WRONG is
1031 not a member of CHOICE, which should be a list of symbols. */
1033 void
1034 wrong_choice (Lisp_Object choice, Lisp_Object wrong)
1036 ptrdiff_t i = 0, len = XINT (Flength (choice));
1037 Lisp_Object obj, *args;
1038 AUTO_STRING (one_of, "One of ");
1039 AUTO_STRING (comma, ", ");
1040 AUTO_STRING (or, " or ");
1041 AUTO_STRING (should_be_specified, " should be specified");
1043 USE_SAFE_ALLOCA;
1044 SAFE_ALLOCA_LISP (args, len * 2 + 1);
1046 args[i++] = one_of;
1048 for (obj = choice; !NILP (obj); obj = XCDR (obj))
1050 args[i++] = SYMBOL_NAME (XCAR (obj));
1051 args[i++] = (NILP (XCDR (obj)) ? should_be_specified
1052 : NILP (XCDR (XCDR (obj))) ? or : comma);
1055 obj = Fconcat (i, args);
1056 SAFE_FREE ();
1057 xsignal2 (Qerror, obj, wrong);
1060 /* Used to signal a user-friendly error if WRONG is not a number or
1061 integer/floating-point number outsize of inclusive MIN..MAX range. */
1063 static void
1064 wrong_range (Lisp_Object min, Lisp_Object max, Lisp_Object wrong)
1066 AUTO_STRING (value_should_be_from, "Value should be from ");
1067 AUTO_STRING (to, " to ");
1068 xsignal2 (Qerror,
1069 CALLN (Fconcat, value_should_be_from, Fnumber_to_string (min),
1070 to, Fnumber_to_string (max)),
1071 wrong);
1074 /* Store NEWVAL into SYMBOL, where VALCONTENTS is found in the value cell
1075 of SYMBOL. If SYMBOL is buffer-local, VALCONTENTS should be the
1076 buffer-independent contents of the value cell: forwarded just one
1077 step past the buffer-localness.
1079 BUF non-zero means set the value in buffer BUF instead of the
1080 current buffer. This only plays a role for per-buffer variables. */
1082 static void
1083 store_symval_forwarding (union Lisp_Fwd *valcontents, register Lisp_Object newval, struct buffer *buf)
1085 switch (XFWDTYPE (valcontents))
1087 case Lisp_Fwd_Int:
1088 CHECK_NUMBER (newval);
1089 *XINTFWD (valcontents)->intvar = XINT (newval);
1090 break;
1092 case Lisp_Fwd_Bool:
1093 *XBOOLFWD (valcontents)->boolvar = !NILP (newval);
1094 break;
1096 case Lisp_Fwd_Obj:
1097 *XOBJFWD (valcontents)->objvar = newval;
1099 /* If this variable is a default for something stored
1100 in the buffer itself, such as default-fill-column,
1101 find the buffers that don't have local values for it
1102 and update them. */
1103 if (XOBJFWD (valcontents)->objvar > (Lisp_Object *) &buffer_defaults
1104 && XOBJFWD (valcontents)->objvar < (Lisp_Object *) (&buffer_defaults + 1))
1106 int offset = ((char *) XOBJFWD (valcontents)->objvar
1107 - (char *) &buffer_defaults);
1108 int idx = PER_BUFFER_IDX (offset);
1110 Lisp_Object tail, buf;
1112 if (idx <= 0)
1113 break;
1115 FOR_EACH_LIVE_BUFFER (tail, buf)
1117 struct buffer *b = XBUFFER (buf);
1119 if (! PER_BUFFER_VALUE_P (b, idx))
1120 set_per_buffer_value (b, offset, newval);
1123 break;
1125 case Lisp_Fwd_Buffer_Obj:
1127 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1128 Lisp_Object predicate = XBUFFER_OBJFWD (valcontents)->predicate;
1130 if (!NILP (newval))
1132 if (SYMBOLP (predicate))
1134 Lisp_Object prop;
1136 if ((prop = Fget (predicate, Qchoice), !NILP (prop)))
1138 if (NILP (Fmemq (newval, prop)))
1139 wrong_choice (prop, newval);
1141 else if ((prop = Fget (predicate, Qrange), !NILP (prop)))
1143 Lisp_Object min = XCAR (prop), max = XCDR (prop);
1144 if (! NUMBERP (newval)
1145 || NILP (CALLN (Fleq, min, newval, max)))
1146 wrong_range (min, max, newval);
1148 else if (FUNCTIONP (predicate))
1150 if (NILP (call1 (predicate, newval)))
1151 wrong_type_argument (predicate, newval);
1155 if (buf == NULL)
1156 buf = current_buffer;
1157 set_per_buffer_value (buf, offset, newval);
1159 break;
1161 case Lisp_Fwd_Kboard_Obj:
1163 char *base = (char *) FRAME_KBOARD (SELECTED_FRAME ());
1164 char *p = base + XKBOARD_OBJFWD (valcontents)->offset;
1165 *(Lisp_Object *) p = newval;
1167 break;
1169 default:
1170 emacs_abort (); /* goto def; */
1174 /* Set up SYMBOL to refer to its global binding. This makes it safe
1175 to alter the status of other bindings. BEWARE: this may be called
1176 during the mark phase of GC, where we assume that Lisp_Object slots
1177 of BLV are marked after this function has changed them. */
1179 void
1180 swap_in_global_binding (struct Lisp_Symbol *symbol)
1182 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (symbol);
1184 /* Unload the previously loaded binding. */
1185 if (blv->fwd)
1186 set_blv_value (blv, do_symval_forwarding (blv->fwd));
1188 /* Select the global binding in the symbol. */
1189 set_blv_valcell (blv, blv->defcell);
1190 if (blv->fwd)
1191 store_symval_forwarding (blv->fwd, XCDR (blv->defcell), NULL);
1193 /* Indicate that the global binding is set up now. */
1194 set_blv_where (blv, Qnil);
1195 set_blv_found (blv, 0);
1198 /* Set up the buffer-local symbol SYMBOL for validity in the current buffer.
1199 VALCONTENTS is the contents of its value cell,
1200 which points to a struct Lisp_Buffer_Local_Value.
1202 Return the value forwarded one step past the buffer-local stage.
1203 This could be another forwarding pointer. */
1205 static void
1206 swap_in_symval_forwarding (struct Lisp_Symbol *symbol, struct Lisp_Buffer_Local_Value *blv)
1208 register Lisp_Object tem1;
1210 eassert (blv == SYMBOL_BLV (symbol));
1212 tem1 = blv->where;
1214 if (NILP (tem1)
1215 || current_buffer != XBUFFER (tem1))
1218 /* Unload the previously loaded binding. */
1219 tem1 = blv->valcell;
1220 if (blv->fwd)
1221 set_blv_value (blv, do_symval_forwarding (blv->fwd));
1222 /* Choose the new binding. */
1224 Lisp_Object var;
1225 XSETSYMBOL (var, symbol);
1226 tem1 = assq_no_quit (var, BVAR (current_buffer, local_var_alist));
1227 set_blv_where (blv, Fcurrent_buffer ());
1229 if (!(blv->found = !NILP (tem1)))
1230 tem1 = blv->defcell;
1232 /* Load the new binding. */
1233 set_blv_valcell (blv, tem1);
1234 if (blv->fwd)
1235 store_symval_forwarding (blv->fwd, blv_value (blv), NULL);
1239 /* Find the value of a symbol, returning Qunbound if it's not bound.
1240 This is helpful for code which just wants to get a variable's value
1241 if it has one, without signaling an error.
1242 Note that it must not be possible to quit
1243 within this function. Great care is required for this. */
1245 Lisp_Object
1246 find_symbol_value (Lisp_Object symbol)
1248 struct Lisp_Symbol *sym;
1250 CHECK_SYMBOL (symbol);
1251 sym = XSYMBOL (symbol);
1253 start:
1254 switch (sym->redirect)
1256 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1257 case SYMBOL_PLAINVAL: return SYMBOL_VAL (sym);
1258 case SYMBOL_LOCALIZED:
1260 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1261 swap_in_symval_forwarding (sym, blv);
1262 return blv->fwd ? do_symval_forwarding (blv->fwd) : blv_value (blv);
1264 /* FALLTHROUGH */
1265 case SYMBOL_FORWARDED:
1266 return do_symval_forwarding (SYMBOL_FWD (sym));
1267 default: emacs_abort ();
1271 DEFUN ("symbol-value", Fsymbol_value, Ssymbol_value, 1, 1, 0,
1272 doc: /* Return SYMBOL's value. Error if that is void.
1273 Note that if `lexical-binding' is in effect, this returns the
1274 global value outside of any lexical scope. */)
1275 (Lisp_Object symbol)
1277 Lisp_Object val;
1279 val = find_symbol_value (symbol);
1280 if (!EQ (val, Qunbound))
1281 return val;
1283 xsignal1 (Qvoid_variable, symbol);
1286 DEFUN ("set", Fset, Sset, 2, 2, 0,
1287 doc: /* Set SYMBOL's value to NEWVAL, and return NEWVAL. */)
1288 (register Lisp_Object symbol, Lisp_Object newval)
1290 set_internal (symbol, newval, Qnil, SET_INTERNAL_SET);
1291 return newval;
1294 /* Store the value NEWVAL into SYMBOL.
1295 If buffer-locality is an issue, WHERE specifies which context to use.
1296 (nil stands for the current buffer/frame).
1298 If BINDFLAG is SET_INTERNAL_SET, then if this symbol is supposed to
1299 become local in every buffer where it is set, then we make it
1300 local. If BINDFLAG is SET_INTERNAL_BIND or SET_INTERNAL_UNBIND, we
1301 don't do that. */
1303 void
1304 set_internal (Lisp_Object symbol, Lisp_Object newval, Lisp_Object where,
1305 enum Set_Internal_Bind bindflag)
1307 bool voide = EQ (newval, Qunbound);
1308 struct Lisp_Symbol *sym;
1309 Lisp_Object tem1;
1311 /* If restoring in a dead buffer, do nothing. */
1312 /* if (BUFFERP (where) && NILP (XBUFFER (where)->name))
1313 return; */
1315 CHECK_SYMBOL (symbol);
1316 sym = XSYMBOL (symbol);
1317 switch (sym->trapped_write)
1319 case SYMBOL_NOWRITE:
1320 if (NILP (Fkeywordp (symbol))
1321 || !EQ (newval, Fsymbol_value (symbol)))
1322 xsignal1 (Qsetting_constant, symbol);
1323 else
1324 /* Allow setting keywords to their own value. */
1325 return;
1327 case SYMBOL_TRAPPED_WRITE:
1328 /* Setting due to thread-switching doesn't count. */
1329 if (bindflag != SET_INTERNAL_THREAD_SWITCH)
1330 notify_variable_watchers (symbol, voide? Qnil : newval,
1331 (bindflag == SET_INTERNAL_BIND? Qlet :
1332 bindflag == SET_INTERNAL_UNBIND? Qunlet :
1333 voide? Qmakunbound : Qset),
1334 where);
1335 /* FALLTHROUGH! */
1336 case SYMBOL_UNTRAPPED_WRITE:
1337 break;
1339 default: emacs_abort ();
1342 start:
1343 switch (sym->redirect)
1345 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1346 case SYMBOL_PLAINVAL: SET_SYMBOL_VAL (sym , newval); return;
1347 case SYMBOL_LOCALIZED:
1349 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1350 if (NILP (where))
1351 XSETBUFFER (where, current_buffer);
1353 /* If the current buffer is not the buffer whose binding is
1354 loaded, or if it's a Lisp_Buffer_Local_Value and
1355 the default binding is loaded, the loaded binding may be the
1356 wrong one. */
1357 if (!EQ (blv->where, where)
1358 /* Also unload a global binding (if the var is local_if_set). */
1359 || (EQ (blv->valcell, blv->defcell)))
1361 /* The currently loaded binding is not necessarily valid.
1362 We need to unload it, and choose a new binding. */
1364 /* Write out `realvalue' to the old loaded binding. */
1365 if (blv->fwd)
1366 set_blv_value (blv, do_symval_forwarding (blv->fwd));
1368 /* Find the new binding. */
1369 XSETSYMBOL (symbol, sym); /* May have changed via aliasing. */
1370 tem1 = assq_no_quit (symbol,
1371 BVAR (XBUFFER (where), local_var_alist));
1372 set_blv_where (blv, where);
1373 blv->found = 1;
1375 if (NILP (tem1))
1377 /* This buffer still sees the default value. */
1379 /* If the variable is a Lisp_Some_Buffer_Local_Value,
1380 or if this is `let' rather than `set',
1381 make CURRENT-ALIST-ELEMENT point to itself,
1382 indicating that we're seeing the default value.
1383 Likewise if the variable has been let-bound
1384 in the current buffer. */
1385 if (bindflag || !blv->local_if_set
1386 || let_shadows_buffer_binding_p (sym))
1388 blv->found = 0;
1389 tem1 = blv->defcell;
1391 /* If it's a local_if_set, being set not bound,
1392 and we're not within a let that was made for this buffer,
1393 create a new buffer-local binding for the variable.
1394 That means, give this buffer a new assoc for a local value
1395 and load that binding. */
1396 else
1398 tem1 = Fcons (symbol, XCDR (blv->defcell));
1399 bset_local_var_alist
1400 (XBUFFER (where),
1401 Fcons (tem1, BVAR (XBUFFER (where), local_var_alist)));
1405 /* Record which binding is now loaded. */
1406 set_blv_valcell (blv, tem1);
1409 /* Store the new value in the cons cell. */
1410 set_blv_value (blv, newval);
1412 if (blv->fwd)
1414 if (voide)
1415 /* If storing void (making the symbol void), forward only through
1416 buffer-local indicator, not through Lisp_Objfwd, etc. */
1417 blv->fwd = NULL;
1418 else
1419 store_symval_forwarding (blv->fwd, newval,
1420 BUFFERP (where)
1421 ? XBUFFER (where) : current_buffer);
1423 break;
1425 case SYMBOL_FORWARDED:
1427 struct buffer *buf
1428 = BUFFERP (where) ? XBUFFER (where) : current_buffer;
1429 union Lisp_Fwd *innercontents = SYMBOL_FWD (sym);
1430 if (BUFFER_OBJFWDP (innercontents))
1432 int offset = XBUFFER_OBJFWD (innercontents)->offset;
1433 int idx = PER_BUFFER_IDX (offset);
1434 if (idx > 0
1435 && bindflag == SET_INTERNAL_SET
1436 && !let_shadows_buffer_binding_p (sym))
1437 SET_PER_BUFFER_VALUE_P (buf, idx, 1);
1440 if (voide)
1441 { /* If storing void (making the symbol void), forward only through
1442 buffer-local indicator, not through Lisp_Objfwd, etc. */
1443 sym->redirect = SYMBOL_PLAINVAL;
1444 SET_SYMBOL_VAL (sym, newval);
1446 else
1447 store_symval_forwarding (/* sym, */ innercontents, newval, buf);
1448 break;
1450 default: emacs_abort ();
1452 return;
1455 static void
1456 set_symbol_trapped_write (Lisp_Object symbol, enum symbol_trapped_write trap)
1458 struct Lisp_Symbol *sym = XSYMBOL (symbol);
1459 if (sym->trapped_write == SYMBOL_NOWRITE)
1460 xsignal1 (Qtrapping_constant, symbol);
1461 sym->trapped_write = trap;
1464 static void
1465 restore_symbol_trapped_write (Lisp_Object symbol)
1467 set_symbol_trapped_write (symbol, SYMBOL_TRAPPED_WRITE);
1470 static void
1471 harmonize_variable_watchers (Lisp_Object alias, Lisp_Object base_variable)
1473 if (!EQ (base_variable, alias)
1474 && EQ (base_variable, Findirect_variable (alias)))
1475 set_symbol_trapped_write
1476 (alias, XSYMBOL (base_variable)->trapped_write);
1479 DEFUN ("add-variable-watcher", Fadd_variable_watcher, Sadd_variable_watcher,
1480 2, 2, 0,
1481 doc: /* Cause WATCH-FUNCTION to be called when SYMBOL is set.
1483 It will be called with 4 arguments: (SYMBOL NEWVAL OPERATION WHERE).
1484 SYMBOL is the variable being changed.
1485 NEWVAL is the value it will be changed to.
1486 OPERATION is a symbol representing the kind of change, one of: `set',
1487 `let', `unlet', `makunbound', and `defvaralias'.
1488 WHERE is a buffer if the buffer-local value of the variable being
1489 changed, nil otherwise.
1491 All writes to aliases of SYMBOL will call WATCH-FUNCTION too. */)
1492 (Lisp_Object symbol, Lisp_Object watch_function)
1494 symbol = Findirect_variable (symbol);
1495 set_symbol_trapped_write (symbol, SYMBOL_TRAPPED_WRITE);
1496 map_obarray (Vobarray, harmonize_variable_watchers, symbol);
1498 Lisp_Object watchers = Fget (symbol, Qwatchers);
1499 Lisp_Object member = Fmember (watch_function, watchers);
1500 if (NILP (member))
1501 Fput (symbol, Qwatchers, Fcons (watch_function, watchers));
1502 return Qnil;
1505 DEFUN ("remove-variable-watcher", Fremove_variable_watcher, Sremove_variable_watcher,
1506 2, 2, 0,
1507 doc: /* Undo the effect of `add-variable-watcher'.
1508 Remove WATCH-FUNCTION from the list of functions to be called when
1509 SYMBOL (or its aliases) are set. */)
1510 (Lisp_Object symbol, Lisp_Object watch_function)
1512 symbol = Findirect_variable (symbol);
1513 Lisp_Object watchers = Fget (symbol, Qwatchers);
1514 watchers = Fdelete (watch_function, watchers);
1515 if (NILP (watchers))
1517 set_symbol_trapped_write (symbol, SYMBOL_UNTRAPPED_WRITE);
1518 map_obarray (Vobarray, harmonize_variable_watchers, symbol);
1520 Fput (symbol, Qwatchers, watchers);
1521 return Qnil;
1524 DEFUN ("get-variable-watchers", Fget_variable_watchers, Sget_variable_watchers,
1525 1, 1, 0,
1526 doc: /* Return a list of SYMBOL's active watchers. */)
1527 (Lisp_Object symbol)
1529 return (SYMBOL_TRAPPED_WRITE_P (symbol) == SYMBOL_TRAPPED_WRITE)
1530 ? Fget (Findirect_variable (symbol), Qwatchers)
1531 : Qnil;
1534 void
1535 notify_variable_watchers (Lisp_Object symbol,
1536 Lisp_Object newval,
1537 Lisp_Object operation,
1538 Lisp_Object where)
1540 symbol = Findirect_variable (symbol);
1542 ptrdiff_t count = SPECPDL_INDEX ();
1543 record_unwind_protect (restore_symbol_trapped_write, symbol);
1544 /* Avoid recursion. */
1545 set_symbol_trapped_write (symbol, SYMBOL_UNTRAPPED_WRITE);
1547 if (NILP (where)
1548 && !EQ (operation, Qset_default) && !EQ (operation, Qmakunbound)
1549 && !NILP (Flocal_variable_if_set_p (symbol, Fcurrent_buffer ())))
1551 XSETBUFFER (where, current_buffer);
1554 if (EQ (operation, Qset_default))
1555 operation = Qset;
1557 for (Lisp_Object watchers = Fget (symbol, Qwatchers);
1558 CONSP (watchers);
1559 watchers = XCDR (watchers))
1561 Lisp_Object watcher = XCAR (watchers);
1562 /* Call subr directly to avoid gc. */
1563 if (SUBRP (watcher))
1565 Lisp_Object args[] = { symbol, newval, operation, where };
1566 funcall_subr (XSUBR (watcher), ARRAYELTS (args), args);
1568 else
1569 CALLN (Ffuncall, watcher, symbol, newval, operation, where);
1572 unbind_to (count, Qnil);
1576 /* Access or set a buffer-local symbol's default value. */
1578 /* Return the default value of SYMBOL, but don't check for voidness.
1579 Return Qunbound if it is void. */
1581 static Lisp_Object
1582 default_value (Lisp_Object symbol)
1584 struct Lisp_Symbol *sym;
1586 CHECK_SYMBOL (symbol);
1587 sym = XSYMBOL (symbol);
1589 start:
1590 switch (sym->redirect)
1592 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1593 case SYMBOL_PLAINVAL: return SYMBOL_VAL (sym);
1594 case SYMBOL_LOCALIZED:
1596 /* If var is set up for a buffer that lacks a local value for it,
1597 the current value is nominally the default value.
1598 But the `realvalue' slot may be more up to date, since
1599 ordinary setq stores just that slot. So use that. */
1600 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1601 if (blv->fwd && EQ (blv->valcell, blv->defcell))
1602 return do_symval_forwarding (blv->fwd);
1603 else
1604 return XCDR (blv->defcell);
1606 case SYMBOL_FORWARDED:
1608 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
1610 /* For a built-in buffer-local variable, get the default value
1611 rather than letting do_symval_forwarding get the current value. */
1612 if (BUFFER_OBJFWDP (valcontents))
1614 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1615 if (PER_BUFFER_IDX (offset) != 0)
1616 return per_buffer_default (offset);
1619 /* For other variables, get the current value. */
1620 return do_symval_forwarding (valcontents);
1622 default: emacs_abort ();
1626 DEFUN ("default-boundp", Fdefault_boundp, Sdefault_boundp, 1, 1, 0,
1627 doc: /* Return t if SYMBOL has a non-void default value.
1628 This is the value that is seen in buffers that do not have their own values
1629 for this variable. */)
1630 (Lisp_Object symbol)
1632 register Lisp_Object value;
1634 value = default_value (symbol);
1635 return (EQ (value, Qunbound) ? Qnil : Qt);
1638 DEFUN ("default-value", Fdefault_value, Sdefault_value, 1, 1, 0,
1639 doc: /* Return SYMBOL's default value.
1640 This is the value that is seen in buffers that do not have their own values
1641 for this variable. The default value is meaningful for variables with
1642 local bindings in certain buffers. */)
1643 (Lisp_Object symbol)
1645 Lisp_Object value = default_value (symbol);
1646 if (!EQ (value, Qunbound))
1647 return value;
1649 xsignal1 (Qvoid_variable, symbol);
1652 void
1653 set_default_internal (Lisp_Object symbol, Lisp_Object value,
1654 enum Set_Internal_Bind bindflag)
1656 struct Lisp_Symbol *sym;
1658 CHECK_SYMBOL (symbol);
1659 sym = XSYMBOL (symbol);
1660 switch (sym->trapped_write)
1662 case SYMBOL_NOWRITE:
1663 if (NILP (Fkeywordp (symbol))
1664 || !EQ (value, Fsymbol_value (symbol)))
1665 xsignal1 (Qsetting_constant, symbol);
1666 else
1667 /* Allow setting keywords to their own value. */
1668 return;
1670 case SYMBOL_TRAPPED_WRITE:
1671 /* Don't notify here if we're going to call Fset anyway. */
1672 if (sym->redirect != SYMBOL_PLAINVAL
1673 /* Setting due to thread switching doesn't count. */
1674 && bindflag != SET_INTERNAL_THREAD_SWITCH)
1675 notify_variable_watchers (symbol, value, Qset_default, Qnil);
1676 /* FALLTHROUGH! */
1677 case SYMBOL_UNTRAPPED_WRITE:
1678 break;
1680 default: emacs_abort ();
1683 start:
1684 switch (sym->redirect)
1686 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1687 case SYMBOL_PLAINVAL: set_internal (symbol, value, Qnil, bindflag); return;
1688 case SYMBOL_LOCALIZED:
1690 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1692 /* Store new value into the DEFAULT-VALUE slot. */
1693 XSETCDR (blv->defcell, value);
1695 /* If the default binding is now loaded, set the REALVALUE slot too. */
1696 if (blv->fwd && EQ (blv->defcell, blv->valcell))
1697 store_symval_forwarding (blv->fwd, value, NULL);
1698 return;
1700 case SYMBOL_FORWARDED:
1702 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
1704 /* Handle variables like case-fold-search that have special slots
1705 in the buffer.
1706 Make them work apparently like Lisp_Buffer_Local_Value variables. */
1707 if (BUFFER_OBJFWDP (valcontents))
1709 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1710 int idx = PER_BUFFER_IDX (offset);
1712 set_per_buffer_default (offset, value);
1714 /* If this variable is not always local in all buffers,
1715 set it in the buffers that don't nominally have a local value. */
1716 if (idx > 0)
1718 struct buffer *b;
1720 FOR_EACH_BUFFER (b)
1721 if (!PER_BUFFER_VALUE_P (b, idx))
1722 set_per_buffer_value (b, offset, value);
1725 else
1726 set_internal (symbol, value, Qnil, bindflag);
1727 return;
1729 default: emacs_abort ();
1733 DEFUN ("set-default", Fset_default, Sset_default, 2, 2, 0,
1734 doc: /* Set SYMBOL's default value to VALUE. SYMBOL and VALUE are evaluated.
1735 The default value is seen in buffers that do not have their own values
1736 for this variable. */)
1737 (Lisp_Object symbol, Lisp_Object value)
1739 set_default_internal (symbol, value, SET_INTERNAL_SET);
1740 return value;
1743 DEFUN ("setq-default", Fsetq_default, Ssetq_default, 0, UNEVALLED, 0,
1744 doc: /* Set the default value of variable VAR to VALUE.
1745 VAR, the variable name, is literal (not evaluated);
1746 VALUE is an expression: it is evaluated and its value returned.
1747 The default value of a variable is seen in buffers
1748 that do not have their own values for the variable.
1750 More generally, you can use multiple variables and values, as in
1751 (setq-default VAR VALUE VAR VALUE...)
1752 This sets each VAR's default value to the corresponding VALUE.
1753 The VALUE for the Nth VAR can refer to the new default values
1754 of previous VARs.
1755 usage: (setq-default [VAR VALUE]...) */)
1756 (Lisp_Object args)
1758 Lisp_Object args_left, symbol, val;
1760 args_left = val = args;
1762 while (CONSP (args_left))
1764 val = eval_sub (Fcar (XCDR (args_left)));
1765 symbol = XCAR (args_left);
1766 Fset_default (symbol, val);
1767 args_left = Fcdr (XCDR (args_left));
1770 return val;
1773 /* Lisp functions for creating and removing buffer-local variables. */
1775 union Lisp_Val_Fwd
1777 Lisp_Object value;
1778 union Lisp_Fwd *fwd;
1781 static struct Lisp_Buffer_Local_Value *
1782 make_blv (struct Lisp_Symbol *sym, bool forwarded,
1783 union Lisp_Val_Fwd valcontents)
1785 struct Lisp_Buffer_Local_Value *blv = xmalloc (sizeof *blv);
1786 Lisp_Object symbol;
1787 Lisp_Object tem;
1789 XSETSYMBOL (symbol, sym);
1790 tem = Fcons (symbol, (forwarded
1791 ? do_symval_forwarding (valcontents.fwd)
1792 : valcontents.value));
1794 /* Buffer_Local_Values cannot have as realval a buffer-local
1795 or keyboard-local forwarding. */
1796 eassert (!(forwarded && BUFFER_OBJFWDP (valcontents.fwd)));
1797 eassert (!(forwarded && KBOARD_OBJFWDP (valcontents.fwd)));
1798 blv->fwd = forwarded ? valcontents.fwd : NULL;
1799 set_blv_where (blv, Qnil);
1800 blv->local_if_set = 0;
1801 set_blv_defcell (blv, tem);
1802 set_blv_valcell (blv, tem);
1803 set_blv_found (blv, 0);
1804 return blv;
1807 DEFUN ("make-variable-buffer-local", Fmake_variable_buffer_local,
1808 Smake_variable_buffer_local, 1, 1, "vMake Variable Buffer Local: ",
1809 doc: /* Make VARIABLE become buffer-local whenever it is set.
1810 At any time, the value for the current buffer is in effect,
1811 unless the variable has never been set in this buffer,
1812 in which case the default value is in effect.
1813 Note that binding the variable with `let', or setting it while
1814 a `let'-style binding made in this buffer is in effect,
1815 does not make the variable buffer-local. Return VARIABLE.
1817 This globally affects all uses of this variable, so it belongs together with
1818 the variable declaration, rather than with its uses (if you just want to make
1819 a variable local to the current buffer for one particular use, use
1820 `make-local-variable'). Buffer-local bindings are normally cleared
1821 while setting up a new major mode, unless they have a `permanent-local'
1822 property.
1824 The function `default-value' gets the default value and `set-default' sets it. */)
1825 (register Lisp_Object variable)
1827 struct Lisp_Symbol *sym;
1828 struct Lisp_Buffer_Local_Value *blv = NULL;
1829 union Lisp_Val_Fwd valcontents;
1830 bool forwarded;
1832 CHECK_SYMBOL (variable);
1833 sym = XSYMBOL (variable);
1835 start:
1836 switch (sym->redirect)
1838 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1839 case SYMBOL_PLAINVAL:
1840 forwarded = 0; valcontents.value = SYMBOL_VAL (sym);
1841 if (EQ (valcontents.value, Qunbound))
1842 valcontents.value = Qnil;
1843 break;
1844 case SYMBOL_LOCALIZED:
1845 blv = SYMBOL_BLV (sym);
1846 break;
1847 case SYMBOL_FORWARDED:
1848 forwarded = 1; valcontents.fwd = SYMBOL_FWD (sym);
1849 if (KBOARD_OBJFWDP (valcontents.fwd))
1850 error ("Symbol %s may not be buffer-local",
1851 SDATA (SYMBOL_NAME (variable)));
1852 else if (BUFFER_OBJFWDP (valcontents.fwd))
1853 return variable;
1854 break;
1855 default: emacs_abort ();
1858 if (SYMBOL_CONSTANT_P (variable))
1859 error ("Symbol %s may not be buffer-local", SDATA (SYMBOL_NAME (variable)));
1861 if (!blv)
1863 blv = make_blv (sym, forwarded, valcontents);
1864 sym->redirect = SYMBOL_LOCALIZED;
1865 SET_SYMBOL_BLV (sym, blv);
1868 blv->local_if_set = 1;
1869 return variable;
1872 DEFUN ("make-local-variable", Fmake_local_variable, Smake_local_variable,
1873 1, 1, "vMake Local Variable: ",
1874 doc: /* Make VARIABLE have a separate value in the current buffer.
1875 Other buffers will continue to share a common default value.
1876 \(The buffer-local value of VARIABLE starts out as the same value
1877 VARIABLE previously had. If VARIABLE was void, it remains void.)
1878 Return VARIABLE.
1880 If the variable is already arranged to become local when set,
1881 this function causes a local value to exist for this buffer,
1882 just as setting the variable would do.
1884 This function returns VARIABLE, and therefore
1885 (set (make-local-variable \\='VARIABLE) VALUE-EXP)
1886 works.
1888 See also `make-variable-buffer-local'.
1890 Do not use `make-local-variable' to make a hook variable buffer-local.
1891 Instead, use `add-hook' and specify t for the LOCAL argument. */)
1892 (Lisp_Object variable)
1894 Lisp_Object tem;
1895 bool forwarded;
1896 union Lisp_Val_Fwd valcontents;
1897 struct Lisp_Symbol *sym;
1898 struct Lisp_Buffer_Local_Value *blv = NULL;
1900 CHECK_SYMBOL (variable);
1901 sym = XSYMBOL (variable);
1903 start:
1904 switch (sym->redirect)
1906 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1907 case SYMBOL_PLAINVAL:
1908 forwarded = 0; valcontents.value = SYMBOL_VAL (sym); break;
1909 case SYMBOL_LOCALIZED:
1910 blv = SYMBOL_BLV (sym);
1911 break;
1912 case SYMBOL_FORWARDED:
1913 forwarded = 1; valcontents.fwd = SYMBOL_FWD (sym);
1914 if (KBOARD_OBJFWDP (valcontents.fwd))
1915 error ("Symbol %s may not be buffer-local",
1916 SDATA (SYMBOL_NAME (variable)));
1917 break;
1918 default: emacs_abort ();
1921 if (sym->trapped_write == SYMBOL_NOWRITE)
1922 error ("Symbol %s may not be buffer-local",
1923 SDATA (SYMBOL_NAME (variable)));
1925 if (blv ? blv->local_if_set
1926 : (forwarded && BUFFER_OBJFWDP (valcontents.fwd)))
1928 tem = Fboundp (variable);
1929 /* Make sure the symbol has a local value in this particular buffer,
1930 by setting it to the same value it already has. */
1931 Fset (variable, (EQ (tem, Qt) ? Fsymbol_value (variable) : Qunbound));
1932 return variable;
1934 if (!blv)
1936 blv = make_blv (sym, forwarded, valcontents);
1937 sym->redirect = SYMBOL_LOCALIZED;
1938 SET_SYMBOL_BLV (sym, blv);
1941 /* Make sure this buffer has its own value of symbol. */
1942 XSETSYMBOL (variable, sym); /* Update in case of aliasing. */
1943 tem = Fassq (variable, BVAR (current_buffer, local_var_alist));
1944 if (NILP (tem))
1946 if (let_shadows_buffer_binding_p (sym))
1948 AUTO_STRING (format,
1949 "Making %s buffer-local while locally let-bound!");
1950 CALLN (Fmessage, format, SYMBOL_NAME (variable));
1953 /* Swap out any local binding for some other buffer, and make
1954 sure the current value is permanently recorded, if it's the
1955 default value. */
1956 find_symbol_value (variable);
1958 bset_local_var_alist
1959 (current_buffer,
1960 Fcons (Fcons (variable, XCDR (blv->defcell)),
1961 BVAR (current_buffer, local_var_alist)));
1963 /* Make sure symbol does not think it is set up for this buffer;
1964 force it to look once again for this buffer's value. */
1965 if (current_buffer == XBUFFER (blv->where))
1966 set_blv_where (blv, Qnil);
1967 set_blv_found (blv, 0);
1970 /* If the symbol forwards into a C variable, then load the binding
1971 for this buffer now. If C code modifies the variable before we
1972 load the binding in, then that new value will clobber the default
1973 binding the next time we unload it. */
1974 if (blv->fwd)
1975 swap_in_symval_forwarding (sym, blv);
1977 return variable;
1980 DEFUN ("kill-local-variable", Fkill_local_variable, Skill_local_variable,
1981 1, 1, "vKill Local Variable: ",
1982 doc: /* Make VARIABLE no longer have a separate value in the current buffer.
1983 From now on the default value will apply in this buffer. Return VARIABLE. */)
1984 (register Lisp_Object variable)
1986 register Lisp_Object tem;
1987 struct Lisp_Buffer_Local_Value *blv;
1988 struct Lisp_Symbol *sym;
1990 CHECK_SYMBOL (variable);
1991 sym = XSYMBOL (variable);
1993 start:
1994 switch (sym->redirect)
1996 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1997 case SYMBOL_PLAINVAL: return variable;
1998 case SYMBOL_FORWARDED:
2000 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
2001 if (BUFFER_OBJFWDP (valcontents))
2003 int offset = XBUFFER_OBJFWD (valcontents)->offset;
2004 int idx = PER_BUFFER_IDX (offset);
2006 if (idx > 0)
2008 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 0);
2009 set_per_buffer_value (current_buffer, offset,
2010 per_buffer_default (offset));
2013 return variable;
2015 case SYMBOL_LOCALIZED:
2016 blv = SYMBOL_BLV (sym);
2017 break;
2018 default: emacs_abort ();
2021 if (sym->trapped_write == SYMBOL_TRAPPED_WRITE)
2022 notify_variable_watchers (variable, Qnil, Qmakunbound, Fcurrent_buffer ());
2024 /* Get rid of this buffer's alist element, if any. */
2025 XSETSYMBOL (variable, sym); /* Propagate variable indirection. */
2026 tem = Fassq (variable, BVAR (current_buffer, local_var_alist));
2027 if (!NILP (tem))
2028 bset_local_var_alist
2029 (current_buffer,
2030 Fdelq (tem, BVAR (current_buffer, local_var_alist)));
2032 /* If the symbol is set up with the current buffer's binding
2033 loaded, recompute its value. We have to do it now, or else
2034 forwarded objects won't work right. */
2036 Lisp_Object buf; XSETBUFFER (buf, current_buffer);
2037 if (EQ (buf, blv->where))
2039 set_blv_where (blv, Qnil);
2040 blv->found = 0;
2041 find_symbol_value (variable);
2045 return variable;
2048 /* Lisp functions for creating and removing buffer-local variables. */
2050 DEFUN ("local-variable-p", Flocal_variable_p, Slocal_variable_p,
2051 1, 2, 0,
2052 doc: /* Non-nil if VARIABLE has a local binding in buffer BUFFER.
2053 BUFFER defaults to the current buffer. */)
2054 (Lisp_Object variable, Lisp_Object buffer)
2056 struct buffer *buf = decode_buffer (buffer);
2057 struct Lisp_Symbol *sym;
2059 CHECK_SYMBOL (variable);
2060 sym = XSYMBOL (variable);
2062 start:
2063 switch (sym->redirect)
2065 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
2066 case SYMBOL_PLAINVAL: return Qnil;
2067 case SYMBOL_LOCALIZED:
2069 Lisp_Object tail, elt, tmp;
2070 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
2071 XSETBUFFER (tmp, buf);
2072 XSETSYMBOL (variable, sym); /* Update in case of aliasing. */
2074 if (EQ (blv->where, tmp)) /* The binding is already loaded. */
2075 return blv_found (blv) ? Qt : Qnil;
2076 else
2077 for (tail = BVAR (buf, local_var_alist); CONSP (tail); tail = XCDR (tail))
2079 elt = XCAR (tail);
2080 if (EQ (variable, XCAR (elt)))
2081 return Qt;
2083 return Qnil;
2085 case SYMBOL_FORWARDED:
2087 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
2088 if (BUFFER_OBJFWDP (valcontents))
2090 int offset = XBUFFER_OBJFWD (valcontents)->offset;
2091 int idx = PER_BUFFER_IDX (offset);
2092 if (idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
2093 return Qt;
2095 return Qnil;
2097 default: emacs_abort ();
2101 DEFUN ("local-variable-if-set-p", Flocal_variable_if_set_p, Slocal_variable_if_set_p,
2102 1, 2, 0,
2103 doc: /* Non-nil if VARIABLE is local in buffer BUFFER when set there.
2104 BUFFER defaults to the current buffer.
2106 More precisely, return non-nil if either VARIABLE already has a local
2107 value in BUFFER, or if VARIABLE is automatically buffer-local (see
2108 `make-variable-buffer-local'). */)
2109 (register Lisp_Object variable, Lisp_Object buffer)
2111 struct Lisp_Symbol *sym;
2113 CHECK_SYMBOL (variable);
2114 sym = XSYMBOL (variable);
2116 start:
2117 switch (sym->redirect)
2119 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
2120 case SYMBOL_PLAINVAL: return Qnil;
2121 case SYMBOL_LOCALIZED:
2123 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
2124 if (blv->local_if_set)
2125 return Qt;
2126 XSETSYMBOL (variable, sym); /* Update in case of aliasing. */
2127 return Flocal_variable_p (variable, buffer);
2129 case SYMBOL_FORWARDED:
2130 /* All BUFFER_OBJFWD slots become local if they are set. */
2131 return (BUFFER_OBJFWDP (SYMBOL_FWD (sym)) ? Qt : Qnil);
2132 default: emacs_abort ();
2136 DEFUN ("variable-binding-locus", Fvariable_binding_locus, Svariable_binding_locus,
2137 1, 1, 0,
2138 doc: /* Return a value indicating where VARIABLE's current binding comes from.
2139 If the current binding is buffer-local, the value is the current buffer.
2140 If the current binding is global (the default), the value is nil. */)
2141 (register Lisp_Object variable)
2143 struct Lisp_Symbol *sym;
2145 CHECK_SYMBOL (variable);
2146 sym = XSYMBOL (variable);
2148 /* Make sure the current binding is actually swapped in. */
2149 find_symbol_value (variable);
2151 start:
2152 switch (sym->redirect)
2154 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
2155 case SYMBOL_PLAINVAL: return Qnil;
2156 case SYMBOL_FORWARDED:
2158 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
2159 if (KBOARD_OBJFWDP (valcontents))
2160 return Fframe_terminal (selected_frame);
2161 else if (!BUFFER_OBJFWDP (valcontents))
2162 return Qnil;
2164 FALLTHROUGH;
2165 case SYMBOL_LOCALIZED:
2166 /* For a local variable, record both the symbol and which
2167 buffer's or frame's value we are saving. */
2168 if (!NILP (Flocal_variable_p (variable, Qnil)))
2169 return Fcurrent_buffer ();
2170 else if (sym->redirect == SYMBOL_LOCALIZED
2171 && blv_found (SYMBOL_BLV (sym)))
2172 return SYMBOL_BLV (sym)->where;
2173 else
2174 return Qnil;
2175 default: emacs_abort ();
2179 /* This code is disabled now that we use the selected frame to return
2180 keyboard-local-values. */
2181 #if 0
2182 extern struct terminal *get_terminal (Lisp_Object display, int);
2184 DEFUN ("terminal-local-value", Fterminal_local_value,
2185 Sterminal_local_value, 2, 2, 0,
2186 doc: /* Return the terminal-local value of SYMBOL on TERMINAL.
2187 If SYMBOL is not a terminal-local variable, then return its normal
2188 value, like `symbol-value'.
2190 TERMINAL may be a terminal object, a frame, or nil (meaning the
2191 selected frame's terminal device). */)
2192 (Lisp_Object symbol, Lisp_Object terminal)
2194 Lisp_Object result;
2195 struct terminal *t = get_terminal (terminal, 1);
2196 push_kboard (t->kboard);
2197 result = Fsymbol_value (symbol);
2198 pop_kboard ();
2199 return result;
2202 DEFUN ("set-terminal-local-value", Fset_terminal_local_value,
2203 Sset_terminal_local_value, 3, 3, 0,
2204 doc: /* Set the terminal-local binding of SYMBOL on TERMINAL to VALUE.
2205 If VARIABLE is not a terminal-local variable, then set its normal
2206 binding, like `set'.
2208 TERMINAL may be a terminal object, a frame, or nil (meaning the
2209 selected frame's terminal device). */)
2210 (Lisp_Object symbol, Lisp_Object terminal, Lisp_Object value)
2212 Lisp_Object result;
2213 struct terminal *t = get_terminal (terminal, 1);
2214 push_kboard (d->kboard);
2215 result = Fset (symbol, value);
2216 pop_kboard ();
2217 return result;
2219 #endif
2221 /* Find the function at the end of a chain of symbol function indirections. */
2223 /* If OBJECT is a symbol, find the end of its function chain and
2224 return the value found there. If OBJECT is not a symbol, just
2225 return it. If there is a cycle in the function chain, signal a
2226 cyclic-function-indirection error.
2228 This is like Findirect_function, except that it doesn't signal an
2229 error if the chain ends up unbound. */
2230 Lisp_Object
2231 indirect_function (register Lisp_Object object)
2233 Lisp_Object tortoise, hare;
2235 hare = tortoise = object;
2237 for (;;)
2239 if (!SYMBOLP (hare) || NILP (hare))
2240 break;
2241 hare = XSYMBOL (hare)->function;
2242 if (!SYMBOLP (hare) || NILP (hare))
2243 break;
2244 hare = XSYMBOL (hare)->function;
2246 tortoise = XSYMBOL (tortoise)->function;
2248 if (EQ (hare, tortoise))
2249 xsignal1 (Qcyclic_function_indirection, object);
2252 return hare;
2255 DEFUN ("indirect-function", Findirect_function, Sindirect_function, 1, 2, 0,
2256 doc: /* Return the function at the end of OBJECT's function chain.
2257 If OBJECT is not a symbol, just return it. Otherwise, follow all
2258 function indirections to find the final function binding and return it.
2259 Signal a cyclic-function-indirection error if there is a loop in the
2260 function chain of symbols. */)
2261 (register Lisp_Object object, Lisp_Object noerror)
2263 Lisp_Object result;
2265 /* Optimize for no indirection. */
2266 result = object;
2267 if (SYMBOLP (result) && !NILP (result)
2268 && (result = XSYMBOL (result)->function, SYMBOLP (result)))
2269 result = indirect_function (result);
2270 if (!NILP (result))
2271 return result;
2273 return Qnil;
2276 /* Extract and set vector and string elements. */
2278 DEFUN ("aref", Faref, Saref, 2, 2, 0,
2279 doc: /* Return the element of ARG at index IDX.
2280 ARG may be a vector, a string, a char-table, a bool-vector, a record,
2281 or a byte-code object. IDX starts at 0. */)
2282 (register Lisp_Object array, Lisp_Object idx)
2284 register EMACS_INT idxval;
2286 CHECK_NUMBER (idx);
2287 idxval = XINT (idx);
2288 if (STRINGP (array))
2290 int c;
2291 ptrdiff_t idxval_byte;
2293 if (idxval < 0 || idxval >= SCHARS (array))
2294 args_out_of_range (array, idx);
2295 if (! STRING_MULTIBYTE (array))
2296 return make_number ((unsigned char) SREF (array, idxval));
2297 idxval_byte = string_char_to_byte (array, idxval);
2299 c = STRING_CHAR (SDATA (array) + idxval_byte);
2300 return make_number (c);
2302 else if (BOOL_VECTOR_P (array))
2304 if (idxval < 0 || idxval >= bool_vector_size (array))
2305 args_out_of_range (array, idx);
2306 return bool_vector_ref (array, idxval);
2308 else if (CHAR_TABLE_P (array))
2310 CHECK_CHARACTER (idx);
2311 return CHAR_TABLE_REF (array, idxval);
2313 else
2315 ptrdiff_t size = 0;
2316 if (VECTORP (array))
2317 size = ASIZE (array);
2318 else if (COMPILEDP (array) || RECORDP (array))
2319 size = PVSIZE (array);
2320 else
2321 wrong_type_argument (Qarrayp, array);
2323 if (idxval < 0 || idxval >= size)
2324 args_out_of_range (array, idx);
2325 return AREF (array, idxval);
2329 DEFUN ("aset", Faset, Saset, 3, 3, 0,
2330 doc: /* Store into the element of ARRAY at index IDX the value NEWELT.
2331 Return NEWELT. ARRAY may be a vector, a string, a char-table or a
2332 bool-vector. IDX starts at 0. */)
2333 (register Lisp_Object array, Lisp_Object idx, Lisp_Object newelt)
2335 register EMACS_INT idxval;
2337 CHECK_NUMBER (idx);
2338 idxval = XINT (idx);
2339 if (! RECORDP (array))
2340 CHECK_ARRAY (array, Qarrayp);
2342 if (VECTORP (array))
2344 CHECK_IMPURE (array, XVECTOR (array));
2345 if (idxval < 0 || idxval >= ASIZE (array))
2346 args_out_of_range (array, idx);
2347 ASET (array, idxval, newelt);
2349 else if (BOOL_VECTOR_P (array))
2351 if (idxval < 0 || idxval >= bool_vector_size (array))
2352 args_out_of_range (array, idx);
2353 bool_vector_set (array, idxval, !NILP (newelt));
2355 else if (CHAR_TABLE_P (array))
2357 CHECK_CHARACTER (idx);
2358 CHAR_TABLE_SET (array, idxval, newelt);
2360 else if (RECORDP (array))
2362 if (idxval < 0 || idxval >= PVSIZE (array))
2363 args_out_of_range (array, idx);
2364 ASET (array, idxval, newelt);
2366 else /* STRINGP */
2368 int c;
2370 CHECK_IMPURE (array, XSTRING (array));
2371 if (idxval < 0 || idxval >= SCHARS (array))
2372 args_out_of_range (array, idx);
2373 CHECK_CHARACTER (newelt);
2374 c = XFASTINT (newelt);
2376 if (STRING_MULTIBYTE (array))
2378 ptrdiff_t idxval_byte, nbytes;
2379 int prev_bytes, new_bytes;
2380 unsigned char workbuf[MAX_MULTIBYTE_LENGTH], *p0 = workbuf, *p1;
2382 nbytes = SBYTES (array);
2383 idxval_byte = string_char_to_byte (array, idxval);
2384 p1 = SDATA (array) + idxval_byte;
2385 prev_bytes = BYTES_BY_CHAR_HEAD (*p1);
2386 new_bytes = CHAR_STRING (c, p0);
2387 if (prev_bytes != new_bytes)
2389 /* We must relocate the string data. */
2390 ptrdiff_t nchars = SCHARS (array);
2391 USE_SAFE_ALLOCA;
2392 unsigned char *str = SAFE_ALLOCA (nbytes);
2394 memcpy (str, SDATA (array), nbytes);
2395 allocate_string_data (XSTRING (array), nchars,
2396 nbytes + new_bytes - prev_bytes);
2397 memcpy (SDATA (array), str, idxval_byte);
2398 p1 = SDATA (array) + idxval_byte;
2399 memcpy (p1 + new_bytes, str + idxval_byte + prev_bytes,
2400 nbytes - (idxval_byte + prev_bytes));
2401 SAFE_FREE ();
2402 clear_string_char_byte_cache ();
2404 while (new_bytes--)
2405 *p1++ = *p0++;
2407 else
2409 if (! SINGLE_BYTE_CHAR_P (c))
2411 ptrdiff_t i;
2413 for (i = SBYTES (array) - 1; i >= 0; i--)
2414 if (SREF (array, i) >= 0x80)
2415 args_out_of_range (array, newelt);
2416 /* ARRAY is an ASCII string. Convert it to a multibyte
2417 string, and try `aset' again. */
2418 STRING_SET_MULTIBYTE (array);
2419 return Faset (array, idx, newelt);
2421 SSET (array, idxval, c);
2425 return newelt;
2428 /* Arithmetic functions */
2430 Lisp_Object
2431 arithcompare (Lisp_Object num1, Lisp_Object num2,
2432 enum Arith_Comparison comparison)
2434 double f1, f2;
2435 EMACS_INT i1, i2;
2436 bool fneq;
2437 bool test;
2439 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num1);
2440 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num2);
2442 /* If either arg is floating point, set F1 and F2 to the 'double'
2443 approximations of the two arguments, and set FNEQ if floating-point
2444 comparison reports that F1 is not equal to F2, possibly because F1
2445 or F2 is a NaN. Regardless, set I1 and I2 to integers that break
2446 ties if the floating-point comparison is either not done or reports
2447 equality. */
2449 if (FLOATP (num1))
2451 f1 = XFLOAT_DATA (num1);
2452 if (FLOATP (num2))
2454 i1 = i2 = 0;
2455 f2 = XFLOAT_DATA (num2);
2457 else
2459 /* Compare a float NUM1 to an integer NUM2 by converting the
2460 integer I2 (i.e., NUM2) to the double F2 (a conversion that
2461 can round on some platforms, if I2 is large enough), and then
2462 converting F2 back to the integer I1 (a conversion that is
2463 always exact), so that I1 exactly equals ((double) NUM2). If
2464 floating-point comparison reports a tie, NUM1 = F1 = F2 = I1
2465 (exactly) so I1 - I2 = NUM1 - NUM2 (exactly), so comparing I1
2466 to I2 will break the tie correctly. */
2467 i1 = f2 = i2 = XINT (num2);
2469 fneq = f1 != f2;
2471 else
2473 i1 = XINT (num1);
2474 if (FLOATP (num2))
2476 /* Compare an integer NUM1 to a float NUM2. This is the
2477 converse of comparing float to integer (see above). */
2478 i2 = f1 = i1;
2479 f2 = XFLOAT_DATA (num2);
2480 fneq = f1 != f2;
2482 else
2484 i2 = XINT (num2);
2485 fneq = false;
2489 switch (comparison)
2491 case ARITH_EQUAL:
2492 test = !fneq && i1 == i2;
2493 break;
2495 case ARITH_NOTEQUAL:
2496 test = fneq || i1 != i2;
2497 break;
2499 case ARITH_LESS:
2500 test = fneq ? f1 < f2 : i1 < i2;
2501 break;
2503 case ARITH_LESS_OR_EQUAL:
2504 test = fneq ? f1 <= f2 : i1 <= i2;
2505 break;
2507 case ARITH_GRTR:
2508 test = fneq ? f1 > f2 : i1 > i2;
2509 break;
2511 case ARITH_GRTR_OR_EQUAL:
2512 test = fneq ? f1 >= f2 : i1 >= i2;
2513 break;
2515 default:
2516 eassume (false);
2519 return test ? Qt : Qnil;
2522 static Lisp_Object
2523 arithcompare_driver (ptrdiff_t nargs, Lisp_Object *args,
2524 enum Arith_Comparison comparison)
2526 for (ptrdiff_t i = 1; i < nargs; i++)
2527 if (NILP (arithcompare (args[i - 1], args[i], comparison)))
2528 return Qnil;
2529 return Qt;
2532 DEFUN ("=", Feqlsign, Seqlsign, 1, MANY, 0,
2533 doc: /* Return t if args, all numbers or markers, are equal.
2534 usage: (= NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2535 (ptrdiff_t nargs, Lisp_Object *args)
2537 return arithcompare_driver (nargs, args, ARITH_EQUAL);
2540 DEFUN ("<", Flss, Slss, 1, MANY, 0,
2541 doc: /* Return t if each arg (a number or marker), is less than the next arg.
2542 usage: (< NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2543 (ptrdiff_t nargs, Lisp_Object *args)
2545 return arithcompare_driver (nargs, args, ARITH_LESS);
2548 DEFUN (">", Fgtr, Sgtr, 1, MANY, 0,
2549 doc: /* Return t if each arg (a number or marker) is greater than the next arg.
2550 usage: (> NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2551 (ptrdiff_t nargs, Lisp_Object *args)
2553 return arithcompare_driver (nargs, args, ARITH_GRTR);
2556 DEFUN ("<=", Fleq, Sleq, 1, MANY, 0,
2557 doc: /* Return t if each arg (a number or marker) is less than or equal to the next.
2558 usage: (<= NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2559 (ptrdiff_t nargs, Lisp_Object *args)
2561 return arithcompare_driver (nargs, args, ARITH_LESS_OR_EQUAL);
2564 DEFUN (">=", Fgeq, Sgeq, 1, MANY, 0,
2565 doc: /* Return t if each arg (a number or marker) is greater than or equal to the next.
2566 usage: (>= NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2567 (ptrdiff_t nargs, Lisp_Object *args)
2569 return arithcompare_driver (nargs, args, ARITH_GRTR_OR_EQUAL);
2572 DEFUN ("/=", Fneq, Sneq, 2, 2, 0,
2573 doc: /* Return t if first arg is not equal to second arg. Both must be numbers or markers. */)
2574 (register Lisp_Object num1, Lisp_Object num2)
2576 return arithcompare (num1, num2, ARITH_NOTEQUAL);
2579 /* Convert the integer I to a cons-of-integers, where I is not in
2580 fixnum range. */
2582 #define INTBIG_TO_LISP(i, extremum) \
2583 (eassert (FIXNUM_OVERFLOW_P (i)), \
2584 (! (FIXNUM_OVERFLOW_P ((extremum) >> 16) \
2585 && FIXNUM_OVERFLOW_P ((i) >> 16)) \
2586 ? Fcons (make_number ((i) >> 16), make_number ((i) & 0xffff)) \
2587 : ! (FIXNUM_OVERFLOW_P ((extremum) >> 16 >> 24) \
2588 && FIXNUM_OVERFLOW_P ((i) >> 16 >> 24)) \
2589 ? Fcons (make_number ((i) >> 16 >> 24), \
2590 Fcons (make_number ((i) >> 16 & 0xffffff), \
2591 make_number ((i) & 0xffff))) \
2592 : make_float (i)))
2594 Lisp_Object
2595 intbig_to_lisp (intmax_t i)
2597 return INTBIG_TO_LISP (i, INTMAX_MIN);
2600 Lisp_Object
2601 uintbig_to_lisp (uintmax_t i)
2603 return INTBIG_TO_LISP (i, UINTMAX_MAX);
2606 /* Convert the cons-of-integers, integer, or float value C to an
2607 unsigned value with maximum value MAX, where MAX is one less than a
2608 power of 2. Signal an error if C does not have a valid format or
2609 is out of range. */
2610 uintmax_t
2611 cons_to_unsigned (Lisp_Object c, uintmax_t max)
2613 bool valid = false;
2614 uintmax_t val;
2615 if (INTEGERP (c))
2617 valid = XINT (c) >= 0;
2618 val = XINT (c);
2620 else if (FLOATP (c))
2622 double d = XFLOAT_DATA (c);
2623 if (d >= 0 && d < 1.0 + max)
2625 val = d;
2626 valid = val == d;
2629 else if (CONSP (c) && NATNUMP (XCAR (c)))
2631 uintmax_t top = XFASTINT (XCAR (c));
2632 Lisp_Object rest = XCDR (c);
2633 if (top <= UINTMAX_MAX >> 24 >> 16
2634 && CONSP (rest)
2635 && NATNUMP (XCAR (rest)) && XFASTINT (XCAR (rest)) < 1 << 24
2636 && NATNUMP (XCDR (rest)) && XFASTINT (XCDR (rest)) < 1 << 16)
2638 uintmax_t mid = XFASTINT (XCAR (rest));
2639 val = top << 24 << 16 | mid << 16 | XFASTINT (XCDR (rest));
2640 valid = true;
2642 else if (top <= UINTMAX_MAX >> 16)
2644 if (CONSP (rest))
2645 rest = XCAR (rest);
2646 if (NATNUMP (rest) && XFASTINT (rest) < 1 << 16)
2648 val = top << 16 | XFASTINT (rest);
2649 valid = true;
2654 if (! (valid && val <= max))
2655 error ("Not an in-range integer, integral float, or cons of integers");
2656 return val;
2659 /* Convert the cons-of-integers, integer, or float value C to a signed
2660 value with extrema MIN and MAX. MAX should be one less than a
2661 power of 2, and MIN should be zero or the negative of a power of 2.
2662 Signal an error if C does not have a valid format or is out of
2663 range. */
2664 intmax_t
2665 cons_to_signed (Lisp_Object c, intmax_t min, intmax_t max)
2667 bool valid = false;
2668 intmax_t val;
2669 if (INTEGERP (c))
2671 val = XINT (c);
2672 valid = true;
2674 else if (FLOATP (c))
2676 double d = XFLOAT_DATA (c);
2677 if (d >= min && d < 1.0 + max)
2679 val = d;
2680 valid = val == d;
2683 else if (CONSP (c) && INTEGERP (XCAR (c)))
2685 intmax_t top = XINT (XCAR (c));
2686 Lisp_Object rest = XCDR (c);
2687 if (top >= INTMAX_MIN >> 24 >> 16 && top <= INTMAX_MAX >> 24 >> 16
2688 && CONSP (rest)
2689 && NATNUMP (XCAR (rest)) && XFASTINT (XCAR (rest)) < 1 << 24
2690 && NATNUMP (XCDR (rest)) && XFASTINT (XCDR (rest)) < 1 << 16)
2692 intmax_t mid = XFASTINT (XCAR (rest));
2693 val = top << 24 << 16 | mid << 16 | XFASTINT (XCDR (rest));
2694 valid = true;
2696 else if (top >= INTMAX_MIN >> 16 && top <= INTMAX_MAX >> 16)
2698 if (CONSP (rest))
2699 rest = XCAR (rest);
2700 if (NATNUMP (rest) && XFASTINT (rest) < 1 << 16)
2702 val = top << 16 | XFASTINT (rest);
2703 valid = true;
2708 if (! (valid && min <= val && val <= max))
2709 error ("Not an in-range integer, integral float, or cons of integers");
2710 return val;
2713 DEFUN ("number-to-string", Fnumber_to_string, Snumber_to_string, 1, 1, 0,
2714 doc: /* Return the decimal representation of NUMBER as a string.
2715 Uses a minus sign if negative.
2716 NUMBER may be an integer or a floating point number. */)
2717 (Lisp_Object number)
2719 char buffer[max (FLOAT_TO_STRING_BUFSIZE, INT_BUFSIZE_BOUND (EMACS_INT))];
2720 int len;
2722 CHECK_NUMBER_OR_FLOAT (number);
2724 if (FLOATP (number))
2725 len = float_to_string (buffer, XFLOAT_DATA (number));
2726 else
2727 len = sprintf (buffer, "%"pI"d", XINT (number));
2729 return make_unibyte_string (buffer, len);
2732 DEFUN ("string-to-number", Fstring_to_number, Sstring_to_number, 1, 2, 0,
2733 doc: /* Parse STRING as a decimal number and return the number.
2734 Ignore leading spaces and tabs, and all trailing chars. Return 0 if
2735 STRING cannot be parsed as an integer or floating point number.
2737 If BASE, interpret STRING as a number in that base. If BASE isn't
2738 present, base 10 is used. BASE must be between 2 and 16 (inclusive).
2739 If the base used is not 10, STRING is always parsed as an integer. */)
2740 (register Lisp_Object string, Lisp_Object base)
2742 register char *p;
2743 register int b;
2744 Lisp_Object val;
2746 CHECK_STRING (string);
2748 if (NILP (base))
2749 b = 10;
2750 else
2752 CHECK_NUMBER (base);
2753 if (! (XINT (base) >= 2 && XINT (base) <= 16))
2754 xsignal1 (Qargs_out_of_range, base);
2755 b = XINT (base);
2758 p = SSDATA (string);
2759 while (*p == ' ' || *p == '\t')
2760 p++;
2762 val = string_to_number (p, b, 1);
2763 return NILP (val) ? make_number (0) : val;
2766 enum arithop
2768 Aadd,
2769 Asub,
2770 Amult,
2771 Adiv,
2772 Alogand,
2773 Alogior,
2774 Alogxor
2777 static Lisp_Object float_arith_driver (double, ptrdiff_t, enum arithop,
2778 ptrdiff_t, Lisp_Object *);
2779 static Lisp_Object
2780 arith_driver (enum arithop code, ptrdiff_t nargs, Lisp_Object *args)
2782 Lisp_Object val;
2783 ptrdiff_t argnum, ok_args;
2784 EMACS_INT accum = 0;
2785 EMACS_INT next, ok_accum;
2786 bool overflow = 0;
2788 switch (code)
2790 case Alogior:
2791 case Alogxor:
2792 case Aadd:
2793 case Asub:
2794 accum = 0;
2795 break;
2796 case Amult:
2797 case Adiv:
2798 accum = 1;
2799 break;
2800 case Alogand:
2801 accum = -1;
2802 break;
2803 default:
2804 break;
2807 for (argnum = 0; argnum < nargs; argnum++)
2809 if (! overflow)
2811 ok_args = argnum;
2812 ok_accum = accum;
2815 /* Using args[argnum] as argument to CHECK_NUMBER_... */
2816 val = args[argnum];
2817 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val);
2819 if (FLOATP (val))
2820 return float_arith_driver (ok_accum, ok_args, code,
2821 nargs, args);
2822 args[argnum] = val;
2823 next = XINT (args[argnum]);
2824 switch (code)
2826 case Aadd:
2827 overflow |= INT_ADD_WRAPV (accum, next, &accum);
2828 break;
2829 case Asub:
2830 if (! argnum)
2831 accum = nargs == 1 ? - next : next;
2832 else
2833 overflow |= INT_SUBTRACT_WRAPV (accum, next, &accum);
2834 break;
2835 case Amult:
2836 overflow |= INT_MULTIPLY_WRAPV (accum, next, &accum);
2837 break;
2838 case Adiv:
2839 if (! (argnum || nargs == 1))
2840 accum = next;
2841 else
2843 if (next == 0)
2844 xsignal0 (Qarith_error);
2845 if (INT_DIVIDE_OVERFLOW (accum, next))
2846 overflow = true;
2847 else
2848 accum /= next;
2850 break;
2851 case Alogand:
2852 accum &= next;
2853 break;
2854 case Alogior:
2855 accum |= next;
2856 break;
2857 case Alogxor:
2858 accum ^= next;
2859 break;
2863 XSETINT (val, accum);
2864 return val;
2867 #ifndef isnan
2868 # define isnan(x) ((x) != (x))
2869 #endif
2871 static Lisp_Object
2872 float_arith_driver (double accum, ptrdiff_t argnum, enum arithop code,
2873 ptrdiff_t nargs, Lisp_Object *args)
2875 register Lisp_Object val;
2876 double next;
2878 for (; argnum < nargs; argnum++)
2880 val = args[argnum]; /* using args[argnum] as argument to CHECK_NUMBER_... */
2881 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val);
2883 if (FLOATP (val))
2885 next = XFLOAT_DATA (val);
2887 else
2889 args[argnum] = val; /* runs into a compiler bug. */
2890 next = XINT (args[argnum]);
2892 switch (code)
2894 case Aadd:
2895 accum += next;
2896 break;
2897 case Asub:
2898 accum = argnum ? accum - next : nargs == 1 ? - next : next;
2899 break;
2900 case Amult:
2901 accum *= next;
2902 break;
2903 case Adiv:
2904 if (! (argnum || nargs == 1))
2905 accum = next;
2906 else
2908 if (! IEEE_FLOATING_POINT && next == 0)
2909 xsignal0 (Qarith_error);
2910 accum /= next;
2912 break;
2913 case Alogand:
2914 case Alogior:
2915 case Alogxor:
2916 wrong_type_argument (Qinteger_or_marker_p, val);
2920 return make_float (accum);
2924 DEFUN ("+", Fplus, Splus, 0, MANY, 0,
2925 doc: /* Return sum of any number of arguments, which are numbers or markers.
2926 usage: (+ &rest NUMBERS-OR-MARKERS) */)
2927 (ptrdiff_t nargs, Lisp_Object *args)
2929 return arith_driver (Aadd, nargs, args);
2932 DEFUN ("-", Fminus, Sminus, 0, MANY, 0,
2933 doc: /* Negate number or subtract numbers or markers and return the result.
2934 With one arg, negates it. With more than one arg,
2935 subtracts all but the first from the first.
2936 usage: (- &optional NUMBER-OR-MARKER &rest MORE-NUMBERS-OR-MARKERS) */)
2937 (ptrdiff_t nargs, Lisp_Object *args)
2939 return arith_driver (Asub, nargs, args);
2942 DEFUN ("*", Ftimes, Stimes, 0, MANY, 0,
2943 doc: /* Return product of any number of arguments, which are numbers or markers.
2944 usage: (* &rest NUMBERS-OR-MARKERS) */)
2945 (ptrdiff_t nargs, Lisp_Object *args)
2947 return arith_driver (Amult, nargs, args);
2950 DEFUN ("/", Fquo, Squo, 1, MANY, 0,
2951 doc: /* Divide number by divisors and return the result.
2952 With two or more arguments, return first argument divided by the rest.
2953 With one argument, return 1 divided by the argument.
2954 The arguments must be numbers or markers.
2955 usage: (/ NUMBER &rest DIVISORS) */)
2956 (ptrdiff_t nargs, Lisp_Object *args)
2958 ptrdiff_t argnum;
2959 for (argnum = 2; argnum < nargs; argnum++)
2960 if (FLOATP (args[argnum]))
2961 return float_arith_driver (0, 0, Adiv, nargs, args);
2962 return arith_driver (Adiv, nargs, args);
2965 DEFUN ("%", Frem, Srem, 2, 2, 0,
2966 doc: /* Return remainder of X divided by Y.
2967 Both must be integers or markers. */)
2968 (register Lisp_Object x, Lisp_Object y)
2970 Lisp_Object val;
2972 CHECK_NUMBER_COERCE_MARKER (x);
2973 CHECK_NUMBER_COERCE_MARKER (y);
2975 if (XINT (y) == 0)
2976 xsignal0 (Qarith_error);
2978 XSETINT (val, XINT (x) % XINT (y));
2979 return val;
2982 DEFUN ("mod", Fmod, Smod, 2, 2, 0,
2983 doc: /* Return X modulo Y.
2984 The result falls between zero (inclusive) and Y (exclusive).
2985 Both X and Y must be numbers or markers. */)
2986 (register Lisp_Object x, Lisp_Object y)
2988 Lisp_Object val;
2989 EMACS_INT i1, i2;
2991 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (x);
2992 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (y);
2994 if (FLOATP (x) || FLOATP (y))
2995 return fmod_float (x, y);
2997 i1 = XINT (x);
2998 i2 = XINT (y);
3000 if (i2 == 0)
3001 xsignal0 (Qarith_error);
3003 i1 %= i2;
3005 /* If the "remainder" comes out with the wrong sign, fix it. */
3006 if (i2 < 0 ? i1 > 0 : i1 < 0)
3007 i1 += i2;
3009 XSETINT (val, i1);
3010 return val;
3013 static Lisp_Object
3014 minmax_driver (ptrdiff_t nargs, Lisp_Object *args,
3015 enum Arith_Comparison comparison)
3017 eassume (0 < nargs);
3018 Lisp_Object accum;
3019 for (ptrdiff_t argnum = 0; argnum < nargs; argnum++)
3021 Lisp_Object val = args[argnum];
3022 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val);
3023 if (argnum == 0 || !NILP (arithcompare (val, accum, comparison)))
3024 accum = val;
3025 else if (FLOATP (accum) && isnan (XFLOAT_DATA (accum)))
3026 return accum;
3028 return accum;
3031 DEFUN ("max", Fmax, Smax, 1, MANY, 0,
3032 doc: /* Return largest of all the arguments (which must be numbers or markers).
3033 The value is always a number; markers are converted to numbers.
3034 usage: (max NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
3035 (ptrdiff_t nargs, Lisp_Object *args)
3037 return minmax_driver (nargs, args, ARITH_GRTR);
3040 DEFUN ("min", Fmin, Smin, 1, MANY, 0,
3041 doc: /* Return smallest of all the arguments (which must be numbers or markers).
3042 The value is always a number; markers are converted to numbers.
3043 usage: (min NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
3044 (ptrdiff_t nargs, Lisp_Object *args)
3046 return minmax_driver (nargs, args, ARITH_LESS);
3049 DEFUN ("logand", Flogand, Slogand, 0, MANY, 0,
3050 doc: /* Return bitwise-and of all the arguments.
3051 Arguments may be integers, or markers converted to integers.
3052 usage: (logand &rest INTS-OR-MARKERS) */)
3053 (ptrdiff_t nargs, Lisp_Object *args)
3055 return arith_driver (Alogand, nargs, args);
3058 DEFUN ("logior", Flogior, Slogior, 0, MANY, 0,
3059 doc: /* Return bitwise-or of all the arguments.
3060 Arguments may be integers, or markers converted to integers.
3061 usage: (logior &rest INTS-OR-MARKERS) */)
3062 (ptrdiff_t nargs, Lisp_Object *args)
3064 return arith_driver (Alogior, nargs, args);
3067 DEFUN ("logxor", Flogxor, Slogxor, 0, MANY, 0,
3068 doc: /* Return bitwise-exclusive-or of all the arguments.
3069 Arguments may be integers, or markers converted to integers.
3070 usage: (logxor &rest INTS-OR-MARKERS) */)
3071 (ptrdiff_t nargs, Lisp_Object *args)
3073 return arith_driver (Alogxor, nargs, args);
3076 static Lisp_Object
3077 ash_lsh_impl (Lisp_Object value, Lisp_Object count, bool lsh)
3079 /* This code assumes that signed right shifts are arithmetic. */
3080 verify ((EMACS_INT) -1 >> 1 == -1);
3082 Lisp_Object val;
3084 CHECK_NUMBER (value);
3085 CHECK_NUMBER (count);
3087 if (XINT (count) >= EMACS_INT_WIDTH)
3088 XSETINT (val, 0);
3089 else if (XINT (count) > 0)
3090 XSETINT (val, XUINT (value) << XINT (count));
3091 else if (XINT (count) <= -EMACS_INT_WIDTH)
3092 XSETINT (val, lsh ? 0 : XINT (value) < 0 ? -1 : 0);
3093 else
3094 XSETINT (val, (lsh ? XUINT (value) >> -XINT (count)
3095 : XINT (value) >> -XINT (count)));
3096 return val;
3099 DEFUN ("ash", Fash, Sash, 2, 2, 0,
3100 doc: /* Return VALUE with its bits shifted left by COUNT.
3101 If COUNT is negative, shifting is actually to the right.
3102 In this case, the sign bit is duplicated. */)
3103 (register Lisp_Object value, Lisp_Object count)
3105 return ash_lsh_impl (value, count, false);
3108 DEFUN ("lsh", Flsh, Slsh, 2, 2, 0,
3109 doc: /* Return VALUE with its bits shifted left by COUNT.
3110 If COUNT is negative, shifting is actually to the right.
3111 In this case, zeros are shifted in on the left. */)
3112 (register Lisp_Object value, Lisp_Object count)
3114 return ash_lsh_impl (value, count, true);
3117 DEFUN ("1+", Fadd1, Sadd1, 1, 1, 0,
3118 doc: /* Return NUMBER plus one. NUMBER may be a number or a marker.
3119 Markers are converted to integers. */)
3120 (register Lisp_Object number)
3122 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number);
3124 if (FLOATP (number))
3125 return (make_float (1.0 + XFLOAT_DATA (number)));
3127 XSETINT (number, XINT (number) + 1);
3128 return number;
3131 DEFUN ("1-", Fsub1, Ssub1, 1, 1, 0,
3132 doc: /* Return NUMBER minus one. NUMBER may be a number or a marker.
3133 Markers are converted to integers. */)
3134 (register Lisp_Object number)
3136 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number);
3138 if (FLOATP (number))
3139 return (make_float (-1.0 + XFLOAT_DATA (number)));
3141 XSETINT (number, XINT (number) - 1);
3142 return number;
3145 DEFUN ("lognot", Flognot, Slognot, 1, 1, 0,
3146 doc: /* Return the bitwise complement of NUMBER. NUMBER must be an integer. */)
3147 (register Lisp_Object number)
3149 CHECK_NUMBER (number);
3150 XSETINT (number, ~XINT (number));
3151 return number;
3154 DEFUN ("byteorder", Fbyteorder, Sbyteorder, 0, 0, 0,
3155 doc: /* Return the byteorder for the machine.
3156 Returns 66 (ASCII uppercase B) for big endian machines or 108 (ASCII
3157 lowercase l) for small endian machines. */
3158 attributes: const)
3159 (void)
3161 unsigned i = 0x04030201;
3162 int order = *(char *)&i == 1 ? 108 : 66;
3164 return make_number (order);
3167 /* Because we round up the bool vector allocate size to word_size
3168 units, we can safely read past the "end" of the vector in the
3169 operations below. These extra bits are always zero. */
3171 static bits_word
3172 bool_vector_spare_mask (EMACS_INT nr_bits)
3174 return (((bits_word) 1) << (nr_bits % BITS_PER_BITS_WORD)) - 1;
3177 /* Info about unsigned long long, falling back on unsigned long
3178 if unsigned long long is not available. */
3180 #if HAVE_UNSIGNED_LONG_LONG_INT && defined ULLONG_WIDTH
3181 enum { ULL_WIDTH = ULLONG_WIDTH };
3182 # define ULL_MAX ULLONG_MAX
3183 #else
3184 enum { ULL_WIDTH = ULONG_WIDTH };
3185 # define ULL_MAX ULONG_MAX
3186 # define count_one_bits_ll count_one_bits_l
3187 # define count_trailing_zeros_ll count_trailing_zeros_l
3188 #endif
3190 /* Shift VAL right by the width of an unsigned long long.
3191 ULL_WIDTH must be less than BITS_PER_BITS_WORD. */
3193 static bits_word
3194 shift_right_ull (bits_word w)
3196 /* Pacify bogus GCC warning about shift count exceeding type width. */
3197 int shift = ULL_WIDTH - BITS_PER_BITS_WORD < 0 ? ULL_WIDTH : 0;
3198 return w >> shift;
3201 /* Return the number of 1 bits in W. */
3203 static int
3204 count_one_bits_word (bits_word w)
3206 if (BITS_WORD_MAX <= UINT_MAX)
3207 return count_one_bits (w);
3208 else if (BITS_WORD_MAX <= ULONG_MAX)
3209 return count_one_bits_l (w);
3210 else
3212 int i = 0, count = 0;
3213 while (count += count_one_bits_ll (w),
3214 (i += ULL_WIDTH) < BITS_PER_BITS_WORD)
3215 w = shift_right_ull (w);
3216 return count;
3220 enum bool_vector_op { bool_vector_exclusive_or,
3221 bool_vector_union,
3222 bool_vector_intersection,
3223 bool_vector_set_difference,
3224 bool_vector_subsetp };
3226 static Lisp_Object
3227 bool_vector_binop_driver (Lisp_Object a,
3228 Lisp_Object b,
3229 Lisp_Object dest,
3230 enum bool_vector_op op)
3232 EMACS_INT nr_bits;
3233 bits_word *adata, *bdata, *destdata;
3234 ptrdiff_t i = 0;
3235 ptrdiff_t nr_words;
3237 CHECK_BOOL_VECTOR (a);
3238 CHECK_BOOL_VECTOR (b);
3240 nr_bits = bool_vector_size (a);
3241 if (bool_vector_size (b) != nr_bits)
3242 wrong_length_argument (a, b, dest);
3244 nr_words = bool_vector_words (nr_bits);
3245 adata = bool_vector_data (a);
3246 bdata = bool_vector_data (b);
3248 if (NILP (dest))
3250 dest = make_uninit_bool_vector (nr_bits);
3251 destdata = bool_vector_data (dest);
3253 else
3255 CHECK_BOOL_VECTOR (dest);
3256 destdata = bool_vector_data (dest);
3257 if (bool_vector_size (dest) != nr_bits)
3258 wrong_length_argument (a, b, dest);
3260 switch (op)
3262 case bool_vector_exclusive_or:
3263 for (; i < nr_words; i++)
3264 if (destdata[i] != (adata[i] ^ bdata[i]))
3265 goto set_dest;
3266 break;
3268 case bool_vector_subsetp:
3269 for (; i < nr_words; i++)
3270 if (adata[i] &~ bdata[i])
3271 return Qnil;
3272 return Qt;
3274 case bool_vector_union:
3275 for (; i < nr_words; i++)
3276 if (destdata[i] != (adata[i] | bdata[i]))
3277 goto set_dest;
3278 break;
3280 case bool_vector_intersection:
3281 for (; i < nr_words; i++)
3282 if (destdata[i] != (adata[i] & bdata[i]))
3283 goto set_dest;
3284 break;
3286 case bool_vector_set_difference:
3287 for (; i < nr_words; i++)
3288 if (destdata[i] != (adata[i] &~ bdata[i]))
3289 goto set_dest;
3290 break;
3293 return Qnil;
3296 set_dest:
3297 switch (op)
3299 case bool_vector_exclusive_or:
3300 for (; i < nr_words; i++)
3301 destdata[i] = adata[i] ^ bdata[i];
3302 break;
3304 case bool_vector_union:
3305 for (; i < nr_words; i++)
3306 destdata[i] = adata[i] | bdata[i];
3307 break;
3309 case bool_vector_intersection:
3310 for (; i < nr_words; i++)
3311 destdata[i] = adata[i] & bdata[i];
3312 break;
3314 case bool_vector_set_difference:
3315 for (; i < nr_words; i++)
3316 destdata[i] = adata[i] &~ bdata[i];
3317 break;
3319 default:
3320 eassume (0);
3323 return dest;
3326 /* PRECONDITION must be true. Return VALUE. This odd construction
3327 works around a bogus GCC diagnostic "shift count >= width of type". */
3329 static int
3330 pre_value (bool precondition, int value)
3332 eassume (precondition);
3333 return precondition ? value : 0;
3336 /* Compute the number of trailing zero bits in val. If val is zero,
3337 return the number of bits in val. */
3338 static int
3339 count_trailing_zero_bits (bits_word val)
3341 if (BITS_WORD_MAX == UINT_MAX)
3342 return count_trailing_zeros (val);
3343 if (BITS_WORD_MAX == ULONG_MAX)
3344 return count_trailing_zeros_l (val);
3345 if (BITS_WORD_MAX == ULL_MAX)
3346 return count_trailing_zeros_ll (val);
3348 /* The rest of this code is for the unlikely platform where bits_word differs
3349 in width from unsigned int, unsigned long, and unsigned long long. */
3350 val |= ~ BITS_WORD_MAX;
3351 if (BITS_WORD_MAX <= UINT_MAX)
3352 return count_trailing_zeros (val);
3353 if (BITS_WORD_MAX <= ULONG_MAX)
3354 return count_trailing_zeros_l (val);
3355 else
3357 int count;
3358 for (count = 0;
3359 count < BITS_PER_BITS_WORD - ULL_WIDTH;
3360 count += ULL_WIDTH)
3362 if (val & ULL_MAX)
3363 return count + count_trailing_zeros_ll (val);
3364 val = shift_right_ull (val);
3367 if (BITS_PER_BITS_WORD % ULL_WIDTH != 0
3368 && BITS_WORD_MAX == (bits_word) -1)
3369 val |= (bits_word) 1 << pre_value (ULONG_MAX < BITS_WORD_MAX,
3370 BITS_PER_BITS_WORD % ULL_WIDTH);
3371 return count + count_trailing_zeros_ll (val);
3375 static bits_word
3376 bits_word_to_host_endian (bits_word val)
3378 #ifndef WORDS_BIGENDIAN
3379 return val;
3380 #else
3381 if (BITS_WORD_MAX >> 31 == 1)
3382 return bswap_32 (val);
3383 # if HAVE_UNSIGNED_LONG_LONG
3384 if (BITS_WORD_MAX >> 31 >> 31 >> 1 == 1)
3385 return bswap_64 (val);
3386 # endif
3388 int i;
3389 bits_word r = 0;
3390 for (i = 0; i < sizeof val; i++)
3392 r = ((r << 1 << (CHAR_BIT - 1))
3393 | (val & ((1u << 1 << (CHAR_BIT - 1)) - 1)));
3394 val = val >> 1 >> (CHAR_BIT - 1);
3396 return r;
3398 #endif
3401 DEFUN ("bool-vector-exclusive-or", Fbool_vector_exclusive_or,
3402 Sbool_vector_exclusive_or, 2, 3, 0,
3403 doc: /* Return A ^ B, bitwise exclusive or.
3404 If optional third argument C is given, store result into C.
3405 A, B, and C must be bool vectors of the same length.
3406 Return the destination vector if it changed or nil otherwise. */)
3407 (Lisp_Object a, Lisp_Object b, Lisp_Object c)
3409 return bool_vector_binop_driver (a, b, c, bool_vector_exclusive_or);
3412 DEFUN ("bool-vector-union", Fbool_vector_union,
3413 Sbool_vector_union, 2, 3, 0,
3414 doc: /* Return A | B, bitwise or.
3415 If optional third argument C is given, store result into C.
3416 A, B, and C must be bool vectors of the same length.
3417 Return the destination vector if it changed or nil otherwise. */)
3418 (Lisp_Object a, Lisp_Object b, Lisp_Object c)
3420 return bool_vector_binop_driver (a, b, c, bool_vector_union);
3423 DEFUN ("bool-vector-intersection", Fbool_vector_intersection,
3424 Sbool_vector_intersection, 2, 3, 0,
3425 doc: /* Return A & B, bitwise and.
3426 If optional third argument C is given, store result into C.
3427 A, B, and C must be bool vectors of the same length.
3428 Return the destination vector if it changed or nil otherwise. */)
3429 (Lisp_Object a, Lisp_Object b, Lisp_Object c)
3431 return bool_vector_binop_driver (a, b, c, bool_vector_intersection);
3434 DEFUN ("bool-vector-set-difference", Fbool_vector_set_difference,
3435 Sbool_vector_set_difference, 2, 3, 0,
3436 doc: /* Return A &~ B, set difference.
3437 If optional third argument C is given, store result into C.
3438 A, B, and C must be bool vectors of the same length.
3439 Return the destination vector if it changed or nil otherwise. */)
3440 (Lisp_Object a, Lisp_Object b, Lisp_Object c)
3442 return bool_vector_binop_driver (a, b, c, bool_vector_set_difference);
3445 DEFUN ("bool-vector-subsetp", Fbool_vector_subsetp,
3446 Sbool_vector_subsetp, 2, 2, 0,
3447 doc: /* Return t if every t value in A is also t in B, nil otherwise.
3448 A and B must be bool vectors of the same length. */)
3449 (Lisp_Object a, Lisp_Object b)
3451 return bool_vector_binop_driver (a, b, b, bool_vector_subsetp);
3454 DEFUN ("bool-vector-not", Fbool_vector_not,
3455 Sbool_vector_not, 1, 2, 0,
3456 doc: /* Compute ~A, set complement.
3457 If optional second argument B is given, store result into B.
3458 A and B must be bool vectors of the same length.
3459 Return the destination vector. */)
3460 (Lisp_Object a, Lisp_Object b)
3462 EMACS_INT nr_bits;
3463 bits_word *bdata, *adata;
3464 ptrdiff_t i;
3466 CHECK_BOOL_VECTOR (a);
3467 nr_bits = bool_vector_size (a);
3469 if (NILP (b))
3470 b = make_uninit_bool_vector (nr_bits);
3471 else
3473 CHECK_BOOL_VECTOR (b);
3474 if (bool_vector_size (b) != nr_bits)
3475 wrong_length_argument (a, b, Qnil);
3478 bdata = bool_vector_data (b);
3479 adata = bool_vector_data (a);
3481 for (i = 0; i < nr_bits / BITS_PER_BITS_WORD; i++)
3482 bdata[i] = BITS_WORD_MAX & ~adata[i];
3484 if (nr_bits % BITS_PER_BITS_WORD)
3486 bits_word mword = bits_word_to_host_endian (adata[i]);
3487 mword = ~mword;
3488 mword &= bool_vector_spare_mask (nr_bits);
3489 bdata[i] = bits_word_to_host_endian (mword);
3492 return b;
3495 DEFUN ("bool-vector-count-population", Fbool_vector_count_population,
3496 Sbool_vector_count_population, 1, 1, 0,
3497 doc: /* Count how many elements in A are t.
3498 A is a bool vector. To count A's nil elements, subtract the return
3499 value from A's length. */)
3500 (Lisp_Object a)
3502 EMACS_INT count;
3503 EMACS_INT nr_bits;
3504 bits_word *adata;
3505 ptrdiff_t i, nwords;
3507 CHECK_BOOL_VECTOR (a);
3509 nr_bits = bool_vector_size (a);
3510 nwords = bool_vector_words (nr_bits);
3511 count = 0;
3512 adata = bool_vector_data (a);
3514 for (i = 0; i < nwords; i++)
3515 count += count_one_bits_word (adata[i]);
3517 return make_number (count);
3520 DEFUN ("bool-vector-count-consecutive", Fbool_vector_count_consecutive,
3521 Sbool_vector_count_consecutive, 3, 3, 0,
3522 doc: /* Count how many consecutive elements in A equal B starting at I.
3523 A is a bool vector, B is t or nil, and I is an index into A. */)
3524 (Lisp_Object a, Lisp_Object b, Lisp_Object i)
3526 EMACS_INT count;
3527 EMACS_INT nr_bits;
3528 int offset;
3529 bits_word *adata;
3530 bits_word twiddle;
3531 bits_word mword; /* Machine word. */
3532 ptrdiff_t pos, pos0;
3533 ptrdiff_t nr_words;
3535 CHECK_BOOL_VECTOR (a);
3536 CHECK_NATNUM (i);
3538 nr_bits = bool_vector_size (a);
3539 if (XFASTINT (i) > nr_bits) /* Allow one past the end for convenience */
3540 args_out_of_range (a, i);
3542 adata = bool_vector_data (a);
3543 nr_words = bool_vector_words (nr_bits);
3544 pos = XFASTINT (i) / BITS_PER_BITS_WORD;
3545 offset = XFASTINT (i) % BITS_PER_BITS_WORD;
3546 count = 0;
3548 /* By XORing with twiddle, we transform the problem of "count
3549 consecutive equal values" into "count the zero bits". The latter
3550 operation usually has hardware support. */
3551 twiddle = NILP (b) ? 0 : BITS_WORD_MAX;
3553 /* Scan the remainder of the mword at the current offset. */
3554 if (pos < nr_words && offset != 0)
3556 mword = bits_word_to_host_endian (adata[pos]);
3557 mword ^= twiddle;
3558 mword >>= offset;
3560 /* Do not count the pad bits. */
3561 mword |= (bits_word) 1 << (BITS_PER_BITS_WORD - offset);
3563 count = count_trailing_zero_bits (mword);
3564 pos++;
3565 if (count + offset < BITS_PER_BITS_WORD)
3566 return make_number (count);
3569 /* Scan whole words until we either reach the end of the vector or
3570 find an mword that doesn't completely match. twiddle is
3571 endian-independent. */
3572 pos0 = pos;
3573 while (pos < nr_words && adata[pos] == twiddle)
3574 pos++;
3575 count += (pos - pos0) * BITS_PER_BITS_WORD;
3577 if (pos < nr_words)
3579 /* If we stopped because of a mismatch, see how many bits match
3580 in the current mword. */
3581 mword = bits_word_to_host_endian (adata[pos]);
3582 mword ^= twiddle;
3583 count += count_trailing_zero_bits (mword);
3585 else if (nr_bits % BITS_PER_BITS_WORD != 0)
3587 /* If we hit the end, we might have overshot our count. Reduce
3588 the total by the number of spare bits at the end of the
3589 vector. */
3590 count -= BITS_PER_BITS_WORD - nr_bits % BITS_PER_BITS_WORD;
3593 return make_number (count);
3597 void
3598 syms_of_data (void)
3600 Lisp_Object error_tail, arith_tail;
3602 DEFSYM (Qquote, "quote");
3603 DEFSYM (Qlambda, "lambda");
3604 DEFSYM (Qerror_conditions, "error-conditions");
3605 DEFSYM (Qerror_message, "error-message");
3606 DEFSYM (Qtop_level, "top-level");
3608 DEFSYM (Qerror, "error");
3609 DEFSYM (Quser_error, "user-error");
3610 DEFSYM (Qquit, "quit");
3611 DEFSYM (Qwrong_length_argument, "wrong-length-argument");
3612 DEFSYM (Qwrong_type_argument, "wrong-type-argument");
3613 DEFSYM (Qargs_out_of_range, "args-out-of-range");
3614 DEFSYM (Qvoid_function, "void-function");
3615 DEFSYM (Qcyclic_function_indirection, "cyclic-function-indirection");
3616 DEFSYM (Qcyclic_variable_indirection, "cyclic-variable-indirection");
3617 DEFSYM (Qvoid_variable, "void-variable");
3618 DEFSYM (Qsetting_constant, "setting-constant");
3619 DEFSYM (Qtrapping_constant, "trapping-constant");
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 (Qrecordp, "recordp");
3645 DEFSYM (Qbool_vector_p, "bool-vector-p");
3646 DEFSYM (Qchar_or_string_p, "char-or-string-p");
3647 DEFSYM (Qmarkerp, "markerp");
3648 #ifdef HAVE_MODULES
3649 DEFSYM (Quser_ptrp, "user-ptrp");
3650 #endif
3651 DEFSYM (Qbuffer_or_string_p, "buffer-or-string-p");
3652 DEFSYM (Qinteger_or_marker_p, "integer-or-marker-p");
3653 DEFSYM (Qfboundp, "fboundp");
3655 DEFSYM (Qfloatp, "floatp");
3656 DEFSYM (Qnumberp, "numberp");
3657 DEFSYM (Qnumber_or_marker_p, "number-or-marker-p");
3659 DEFSYM (Qchar_table_p, "char-table-p");
3660 DEFSYM (Qvector_or_char_table_p, "vector-or-char-table-p");
3662 DEFSYM (Qsubrp, "subrp");
3663 DEFSYM (Qunevalled, "unevalled");
3664 DEFSYM (Qmany, "many");
3666 DEFSYM (Qcdr, "cdr");
3668 error_tail = pure_cons (Qerror, Qnil);
3670 /* ERROR is used as a signaler for random errors for which nothing else is
3671 right. */
3673 Fput (Qerror, Qerror_conditions,
3674 error_tail);
3675 Fput (Qerror, Qerror_message,
3676 build_pure_c_string ("error"));
3678 #define PUT_ERROR(sym, tail, msg) \
3679 Fput (sym, Qerror_conditions, pure_cons (sym, tail)); \
3680 Fput (sym, Qerror_message, build_pure_c_string (msg))
3682 PUT_ERROR (Qquit, Qnil, "Quit");
3684 PUT_ERROR (Quser_error, error_tail, "");
3685 PUT_ERROR (Qwrong_length_argument, error_tail, "Wrong length argument");
3686 PUT_ERROR (Qwrong_type_argument, error_tail, "Wrong type argument");
3687 PUT_ERROR (Qargs_out_of_range, error_tail, "Args out of range");
3688 PUT_ERROR (Qvoid_function, error_tail,
3689 "Symbol's function definition is void");
3690 PUT_ERROR (Qcyclic_function_indirection, error_tail,
3691 "Symbol's chain of function indirections contains a loop");
3692 PUT_ERROR (Qcyclic_variable_indirection, error_tail,
3693 "Symbol's chain of variable indirections contains a loop");
3694 DEFSYM (Qcircular_list, "circular-list");
3695 PUT_ERROR (Qcircular_list, error_tail, "List contains a loop");
3696 PUT_ERROR (Qvoid_variable, error_tail, "Symbol's value as variable is void");
3697 PUT_ERROR (Qsetting_constant, error_tail,
3698 "Attempt to set a constant symbol");
3699 PUT_ERROR (Qtrapping_constant, error_tail,
3700 "Attempt to trap writes to a constant symbol");
3701 PUT_ERROR (Qinvalid_read_syntax, error_tail, "Invalid read syntax");
3702 PUT_ERROR (Qinvalid_function, error_tail, "Invalid function");
3703 PUT_ERROR (Qwrong_number_of_arguments, error_tail,
3704 "Wrong number of arguments");
3705 PUT_ERROR (Qno_catch, error_tail, "No catch for tag");
3706 PUT_ERROR (Qend_of_file, error_tail, "End of file during parsing");
3708 arith_tail = pure_cons (Qarith_error, error_tail);
3709 Fput (Qarith_error, Qerror_conditions, arith_tail);
3710 Fput (Qarith_error, Qerror_message, build_pure_c_string ("Arithmetic error"));
3712 PUT_ERROR (Qbeginning_of_buffer, error_tail, "Beginning of buffer");
3713 PUT_ERROR (Qend_of_buffer, error_tail, "End of buffer");
3714 PUT_ERROR (Qbuffer_read_only, error_tail, "Buffer is read-only");
3715 PUT_ERROR (Qtext_read_only, pure_cons (Qbuffer_read_only, error_tail),
3716 "Text is read-only");
3718 DEFSYM (Qrange_error, "range-error");
3719 DEFSYM (Qdomain_error, "domain-error");
3720 DEFSYM (Qsingularity_error, "singularity-error");
3721 DEFSYM (Qoverflow_error, "overflow-error");
3722 DEFSYM (Qunderflow_error, "underflow-error");
3724 PUT_ERROR (Qdomain_error, arith_tail, "Arithmetic domain error");
3726 PUT_ERROR (Qrange_error, arith_tail, "Arithmetic range error");
3728 PUT_ERROR (Qsingularity_error, Fcons (Qdomain_error, arith_tail),
3729 "Arithmetic singularity error");
3731 PUT_ERROR (Qoverflow_error, Fcons (Qdomain_error, arith_tail),
3732 "Arithmetic overflow error");
3733 PUT_ERROR (Qunderflow_error, Fcons (Qdomain_error, arith_tail),
3734 "Arithmetic underflow error");
3736 /* Types that type-of returns. */
3737 DEFSYM (Qinteger, "integer");
3738 DEFSYM (Qsymbol, "symbol");
3739 DEFSYM (Qstring, "string");
3740 DEFSYM (Qcons, "cons");
3741 DEFSYM (Qmarker, "marker");
3742 DEFSYM (Qoverlay, "overlay");
3743 DEFSYM (Qfinalizer, "finalizer");
3744 #ifdef HAVE_MODULES
3745 DEFSYM (Qmodule_function, "module-function");
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 (Qsubr, "subr");
3753 DEFSYM (Qcompiled_function, "compiled-function");
3754 DEFSYM (Qbuffer, "buffer");
3755 DEFSYM (Qframe, "frame");
3756 DEFSYM (Qvector, "vector");
3757 DEFSYM (Qrecord, "record");
3758 DEFSYM (Qchar_table, "char-table");
3759 DEFSYM (Qbool_vector, "bool-vector");
3760 DEFSYM (Qhash_table, "hash-table");
3761 DEFSYM (Qthread, "thread");
3762 DEFSYM (Qmutex, "mutex");
3763 DEFSYM (Qcondition_variable, "condition-variable");
3764 DEFSYM (Qfont_spec, "font-spec");
3765 DEFSYM (Qfont_entity, "font-entity");
3766 DEFSYM (Qfont_object, "font-object");
3767 DEFSYM (Qterminal, "terminal");
3769 DEFSYM (Qdefun, "defun");
3771 DEFSYM (Qinteractive_form, "interactive-form");
3772 DEFSYM (Qdefalias_fset_function, "defalias-fset-function");
3774 defsubr (&Sindirect_variable);
3775 defsubr (&Sinteractive_form);
3776 defsubr (&Seq);
3777 defsubr (&Snull);
3778 defsubr (&Stype_of);
3779 defsubr (&Slistp);
3780 defsubr (&Snlistp);
3781 defsubr (&Sconsp);
3782 defsubr (&Satom);
3783 defsubr (&Sintegerp);
3784 defsubr (&Sinteger_or_marker_p);
3785 defsubr (&Snumberp);
3786 defsubr (&Snumber_or_marker_p);
3787 defsubr (&Sfloatp);
3788 defsubr (&Snatnump);
3789 defsubr (&Ssymbolp);
3790 defsubr (&Skeywordp);
3791 defsubr (&Sstringp);
3792 defsubr (&Smultibyte_string_p);
3793 defsubr (&Svectorp);
3794 defsubr (&Srecordp);
3795 defsubr (&Schar_table_p);
3796 defsubr (&Svector_or_char_table_p);
3797 defsubr (&Sbool_vector_p);
3798 defsubr (&Sarrayp);
3799 defsubr (&Ssequencep);
3800 defsubr (&Sbufferp);
3801 defsubr (&Smarkerp);
3802 defsubr (&Ssubrp);
3803 defsubr (&Sbyte_code_function_p);
3804 defsubr (&Smodule_function_p);
3805 defsubr (&Schar_or_string_p);
3806 defsubr (&Sthreadp);
3807 defsubr (&Smutexp);
3808 defsubr (&Scondition_variable_p);
3809 defsubr (&Scar);
3810 defsubr (&Scdr);
3811 defsubr (&Scar_safe);
3812 defsubr (&Scdr_safe);
3813 defsubr (&Ssetcar);
3814 defsubr (&Ssetcdr);
3815 defsubr (&Ssymbol_function);
3816 defsubr (&Sindirect_function);
3817 defsubr (&Ssymbol_plist);
3818 defsubr (&Ssymbol_name);
3819 defsubr (&Smakunbound);
3820 defsubr (&Sfmakunbound);
3821 defsubr (&Sboundp);
3822 defsubr (&Sfboundp);
3823 defsubr (&Sfset);
3824 defsubr (&Sdefalias);
3825 defsubr (&Ssetplist);
3826 defsubr (&Ssymbol_value);
3827 defsubr (&Sset);
3828 defsubr (&Sdefault_boundp);
3829 defsubr (&Sdefault_value);
3830 defsubr (&Sset_default);
3831 defsubr (&Ssetq_default);
3832 defsubr (&Smake_variable_buffer_local);
3833 defsubr (&Smake_local_variable);
3834 defsubr (&Skill_local_variable);
3835 defsubr (&Slocal_variable_p);
3836 defsubr (&Slocal_variable_if_set_p);
3837 defsubr (&Svariable_binding_locus);
3838 #if 0 /* XXX Remove this. --lorentey */
3839 defsubr (&Sterminal_local_value);
3840 defsubr (&Sset_terminal_local_value);
3841 #endif
3842 defsubr (&Saref);
3843 defsubr (&Saset);
3844 defsubr (&Snumber_to_string);
3845 defsubr (&Sstring_to_number);
3846 defsubr (&Seqlsign);
3847 defsubr (&Slss);
3848 defsubr (&Sgtr);
3849 defsubr (&Sleq);
3850 defsubr (&Sgeq);
3851 defsubr (&Sneq);
3852 defsubr (&Splus);
3853 defsubr (&Sminus);
3854 defsubr (&Stimes);
3855 defsubr (&Squo);
3856 defsubr (&Srem);
3857 defsubr (&Smod);
3858 defsubr (&Smax);
3859 defsubr (&Smin);
3860 defsubr (&Slogand);
3861 defsubr (&Slogior);
3862 defsubr (&Slogxor);
3863 defsubr (&Slsh);
3864 defsubr (&Sash);
3865 defsubr (&Sadd1);
3866 defsubr (&Ssub1);
3867 defsubr (&Slognot);
3868 defsubr (&Sbyteorder);
3869 defsubr (&Ssubr_arity);
3870 defsubr (&Ssubr_name);
3871 #ifdef HAVE_MODULES
3872 defsubr (&Suser_ptrp);
3873 #endif
3875 defsubr (&Sbool_vector_exclusive_or);
3876 defsubr (&Sbool_vector_union);
3877 defsubr (&Sbool_vector_intersection);
3878 defsubr (&Sbool_vector_set_difference);
3879 defsubr (&Sbool_vector_not);
3880 defsubr (&Sbool_vector_subsetp);
3881 defsubr (&Sbool_vector_count_consecutive);
3882 defsubr (&Sbool_vector_count_population);
3884 set_symbol_function (Qwholenump, XSYMBOL (Qnatnump)->function);
3886 DEFVAR_LISP ("most-positive-fixnum", Vmost_positive_fixnum,
3887 doc: /* The largest value that is representable in a Lisp integer. */);
3888 Vmost_positive_fixnum = make_number (MOST_POSITIVE_FIXNUM);
3889 make_symbol_constant (intern_c_string ("most-positive-fixnum"));
3891 DEFVAR_LISP ("most-negative-fixnum", Vmost_negative_fixnum,
3892 doc: /* The smallest value that is representable in a Lisp integer. */);
3893 Vmost_negative_fixnum = make_number (MOST_NEGATIVE_FIXNUM);
3894 make_symbol_constant (intern_c_string ("most-negative-fixnum"));
3896 DEFSYM (Qwatchers, "watchers");
3897 DEFSYM (Qmakunbound, "makunbound");
3898 DEFSYM (Qunlet, "unlet");
3899 DEFSYM (Qset, "set");
3900 DEFSYM (Qset_default, "set-default");
3901 defsubr (&Sadd_variable_watcher);
3902 defsubr (&Sremove_variable_watcher);
3903 defsubr (&Sget_variable_watchers);