1 /* Primitive operations on Lisp data types for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985-1986, 1988, 1993-1995, 1997-2015 Free Software
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
10 (at 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/>. */
25 #include <count-one-bits.h>
26 #include <count-trailing-zeros.h>
31 #include "character.h"
35 #include "syssignal.h"
36 #include "termhooks.h" /* For FRAME_KBOARD reference in y-or-n-p. */
40 static void swap_in_symval_forwarding (struct Lisp_Symbol
*,
41 struct Lisp_Buffer_Local_Value
*);
44 BOOLFWDP (union Lisp_Fwd
*a
)
46 return XFWDTYPE (a
) == Lisp_Fwd_Bool
;
49 INTFWDP (union Lisp_Fwd
*a
)
51 return XFWDTYPE (a
) == Lisp_Fwd_Int
;
54 KBOARD_OBJFWDP (union Lisp_Fwd
*a
)
56 return XFWDTYPE (a
) == Lisp_Fwd_Kboard_Obj
;
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
));
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
));
82 static struct Lisp_Objfwd
*
83 XOBJFWD (union Lisp_Fwd
*a
)
85 eassert (OBJFWDP (a
));
90 CHECK_SUBR (Lisp_Object x
)
92 CHECK_TYPE (SUBRP (x
), Qsubrp
, x
);
96 set_blv_found (struct Lisp_Buffer_Local_Value
*blv
, int found
)
98 eassert (found
== !EQ (blv
->defcell
, blv
->valcell
));
103 blv_value (struct Lisp_Buffer_Local_Value
*blv
)
105 return XCDR (blv
->valcell
);
109 set_blv_value (struct Lisp_Buffer_Local_Value
*blv
, Lisp_Object val
)
111 XSETCDR (blv
->valcell
, val
);
115 set_blv_where (struct Lisp_Buffer_Local_Value
*blv
, Lisp_Object val
)
121 set_blv_defcell (struct Lisp_Buffer_Local_Value
*blv
, Lisp_Object val
)
127 set_blv_valcell (struct Lisp_Buffer_Local_Value
*blv
, Lisp_Object 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
));
138 xsignal2 (Qwrong_length_argument
, size1
, size2
);
140 xsignal3 (Qwrong_length_argument
, size1
, size2
,
141 make_number (bool_vector_size (a3
)));
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)
154 xsignal2 (Qwrong_type_argument
, predicate
, value
);
158 pure_write_error (Lisp_Object obj
)
160 xsignal2 (Qerror
, build_string ("Attempt to modify read-only object"), obj
);
164 args_out_of_range (Lisp_Object a1
, Lisp_Object a2
)
166 xsignal2 (Qargs_out_of_range
, a1
, a2
);
170 args_out_of_range_3 (Lisp_Object a1
, Lisp_Object a2
, Lisp_Object a3
)
172 xsignal3 (Qargs_out_of_range
, a1
, a2
, a3
);
176 /* Data type predicates. */
178 DEFUN ("eq", Feq
, Seq
, 2, 2, 0,
179 doc
: /* Return t if the two args are the same Lisp object. */)
180 (Lisp_Object obj1
, Lisp_Object obj2
)
187 DEFUN ("null", Fnull
, Snull
, 1, 1, 0,
188 doc
: /* Return t if OBJECT is nil. */)
196 DEFUN ("type-of", Ftype_of
, Stype_of
, 1, 1, 0,
197 doc
: /* Return a symbol representing the type of OBJECT.
198 The symbol returned names the object's basic type;
199 for example, (type-of 1) returns `integer'. */)
202 switch (XTYPE (object
))
217 switch (XMISCTYPE (object
))
219 case Lisp_Misc_Marker
:
221 case Lisp_Misc_Overlay
:
223 case Lisp_Misc_Float
:
228 case Lisp_Vectorlike
:
229 if (WINDOW_CONFIGURATIONP (object
))
230 return Qwindow_configuration
;
231 if (PROCESSP (object
))
233 if (WINDOWP (object
))
237 if (COMPILEDP (object
))
238 return Qcompiled_function
;
239 if (BUFFERP (object
))
241 if (CHAR_TABLE_P (object
))
243 if (BOOL_VECTOR_P (object
))
247 if (HASH_TABLE_P (object
))
249 if (FONT_SPEC_P (object
))
251 if (FONT_ENTITY_P (object
))
253 if (FONT_OBJECT_P (object
))
265 DEFUN ("consp", Fconsp
, Sconsp
, 1, 1, 0,
266 doc
: /* Return t if OBJECT is a cons cell. */)
274 DEFUN ("atom", Fatom
, Satom
, 1, 1, 0,
275 doc
: /* Return t if OBJECT is not a cons cell. This includes nil. */)
283 DEFUN ("listp", Flistp
, Slistp
, 1, 1, 0,
284 doc
: /* Return t if OBJECT is a list, that is, a cons cell or nil.
285 Otherwise, return nil. */)
288 if (CONSP (object
) || NILP (object
))
293 DEFUN ("nlistp", Fnlistp
, Snlistp
, 1, 1, 0,
294 doc
: /* Return t if OBJECT is not a list. Lists include nil. */)
297 if (CONSP (object
) || NILP (object
))
302 DEFUN ("symbolp", Fsymbolp
, Ssymbolp
, 1, 1, 0,
303 doc
: /* Return t if OBJECT is a symbol. */)
306 if (SYMBOLP (object
))
311 /* Define this in C to avoid unnecessarily consing up the symbol
313 DEFUN ("keywordp", Fkeywordp
, Skeywordp
, 1, 1, 0,
314 doc
: /* Return t if OBJECT is a keyword.
315 This means that it is a symbol with a print name beginning with `:'
316 interned in the initial obarray. */)
320 && SREF (SYMBOL_NAME (object
), 0) == ':'
321 && SYMBOL_INTERNED_IN_INITIAL_OBARRAY_P (object
))
326 DEFUN ("vectorp", Fvectorp
, Svectorp
, 1, 1, 0,
327 doc
: /* Return t if OBJECT is a vector. */)
330 if (VECTORP (object
))
335 DEFUN ("stringp", Fstringp
, Sstringp
, 1, 1, 0,
336 doc
: /* Return t if OBJECT is a string. */)
339 if (STRINGP (object
))
344 DEFUN ("multibyte-string-p", Fmultibyte_string_p
, Smultibyte_string_p
,
346 doc
: /* Return t if OBJECT is a multibyte string.
347 Return nil if OBJECT is either a unibyte string, or not a string. */)
350 if (STRINGP (object
) && STRING_MULTIBYTE (object
))
355 DEFUN ("char-table-p", Fchar_table_p
, Schar_table_p
, 1, 1, 0,
356 doc
: /* Return t if OBJECT is a char-table. */)
359 if (CHAR_TABLE_P (object
))
364 DEFUN ("vector-or-char-table-p", Fvector_or_char_table_p
,
365 Svector_or_char_table_p
, 1, 1, 0,
366 doc
: /* Return t if OBJECT is a char-table or vector. */)
369 if (VECTORP (object
) || CHAR_TABLE_P (object
))
374 DEFUN ("bool-vector-p", Fbool_vector_p
, Sbool_vector_p
, 1, 1, 0,
375 doc
: /* Return t if OBJECT is a bool-vector. */)
378 if (BOOL_VECTOR_P (object
))
383 DEFUN ("arrayp", Farrayp
, Sarrayp
, 1, 1, 0,
384 doc
: /* Return t if OBJECT is an array (string or vector). */)
392 DEFUN ("sequencep", Fsequencep
, Ssequencep
, 1, 1, 0,
393 doc
: /* Return t if OBJECT is a sequence (list or array). */)
394 (register Lisp_Object object
)
396 if (CONSP (object
) || NILP (object
) || ARRAYP (object
))
401 DEFUN ("bufferp", Fbufferp
, Sbufferp
, 1, 1, 0,
402 doc
: /* Return t if OBJECT is an editor buffer. */)
405 if (BUFFERP (object
))
410 DEFUN ("markerp", Fmarkerp
, Smarkerp
, 1, 1, 0,
411 doc
: /* Return t if OBJECT is a marker (editor pointer). */)
414 if (MARKERP (object
))
419 DEFUN ("subrp", Fsubrp
, Ssubrp
, 1, 1, 0,
420 doc
: /* Return t if OBJECT is a built-in function. */)
428 DEFUN ("byte-code-function-p", Fbyte_code_function_p
, Sbyte_code_function_p
,
430 doc
: /* Return t if OBJECT is a byte-compiled function object. */)
433 if (COMPILEDP (object
))
438 DEFUN ("char-or-string-p", Fchar_or_string_p
, Schar_or_string_p
, 1, 1, 0,
439 doc
: /* Return t if OBJECT is a character or a string. */)
440 (register Lisp_Object object
)
442 if (CHARACTERP (object
) || STRINGP (object
))
447 DEFUN ("integerp", Fintegerp
, Sintegerp
, 1, 1, 0,
448 doc
: /* Return t if OBJECT is an integer. */)
451 if (INTEGERP (object
))
456 DEFUN ("integer-or-marker-p", Finteger_or_marker_p
, Sinteger_or_marker_p
, 1, 1, 0,
457 doc
: /* Return t if OBJECT is an integer or a marker (editor pointer). */)
458 (register Lisp_Object object
)
460 if (MARKERP (object
) || INTEGERP (object
))
465 DEFUN ("natnump", Fnatnump
, Snatnump
, 1, 1, 0,
466 doc
: /* Return t if OBJECT is a nonnegative integer. */)
469 if (NATNUMP (object
))
474 DEFUN ("numberp", Fnumberp
, Snumberp
, 1, 1, 0,
475 doc
: /* Return t if OBJECT is a number (floating point or integer). */)
478 if (NUMBERP (object
))
484 DEFUN ("number-or-marker-p", Fnumber_or_marker_p
,
485 Snumber_or_marker_p
, 1, 1, 0,
486 doc
: /* Return t if OBJECT is a number or a marker. */)
489 if (NUMBERP (object
) || MARKERP (object
))
494 DEFUN ("floatp", Ffloatp
, Sfloatp
, 1, 1, 0,
495 doc
: /* Return t if OBJECT is a floating point number. */)
504 /* Extract and set components of lists. */
506 DEFUN ("car", Fcar
, Scar
, 1, 1, 0,
507 doc
: /* Return the car of LIST. If arg is nil, return nil.
508 Error if arg is not nil and not a cons cell. See also `car-safe'.
510 See Info node `(elisp)Cons Cells' for a discussion of related basic
511 Lisp concepts such as car, cdr, cons cell and list. */)
512 (register Lisp_Object list
)
517 DEFUN ("car-safe", Fcar_safe
, Scar_safe
, 1, 1, 0,
518 doc
: /* Return the car of OBJECT if it is a cons cell, or else nil. */)
521 return CAR_SAFE (object
);
524 DEFUN ("cdr", Fcdr
, Scdr
, 1, 1, 0,
525 doc
: /* Return the cdr of LIST. If arg is nil, return nil.
526 Error if arg is not nil and not a cons cell. See also `cdr-safe'.
528 See Info node `(elisp)Cons Cells' for a discussion of related basic
529 Lisp concepts such as cdr, car, cons cell and list. */)
530 (register Lisp_Object list
)
535 DEFUN ("cdr-safe", Fcdr_safe
, Scdr_safe
, 1, 1, 0,
536 doc
: /* Return the cdr of OBJECT if it is a cons cell, or else nil. */)
539 return CDR_SAFE (object
);
542 DEFUN ("setcar", Fsetcar
, Ssetcar
, 2, 2, 0,
543 doc
: /* Set the car of CELL to be NEWCAR. Returns NEWCAR. */)
544 (register Lisp_Object cell
, Lisp_Object newcar
)
548 XSETCAR (cell
, newcar
);
552 DEFUN ("setcdr", Fsetcdr
, Ssetcdr
, 2, 2, 0,
553 doc
: /* Set the cdr of CELL to be NEWCDR. Returns NEWCDR. */)
554 (register Lisp_Object cell
, Lisp_Object newcdr
)
558 XSETCDR (cell
, newcdr
);
562 /* Extract and set components of symbols. */
564 DEFUN ("boundp", Fboundp
, Sboundp
, 1, 1, 0,
565 doc
: /* Return t if SYMBOL's value is not void.
566 Note that if `lexical-binding' is in effect, this refers to the
567 global value outside of any lexical scope. */)
568 (register Lisp_Object symbol
)
570 Lisp_Object valcontents
;
571 struct Lisp_Symbol
*sym
;
572 CHECK_SYMBOL (symbol
);
573 sym
= XSYMBOL (symbol
);
576 switch (sym
->redirect
)
578 case SYMBOL_PLAINVAL
: valcontents
= SYMBOL_VAL (sym
); break;
579 case SYMBOL_VARALIAS
: sym
= indirect_variable (sym
); goto start
;
580 case SYMBOL_LOCALIZED
:
582 struct Lisp_Buffer_Local_Value
*blv
= SYMBOL_BLV (sym
);
584 /* In set_internal, we un-forward vars when their value is
589 swap_in_symval_forwarding (sym
, blv
);
590 valcontents
= blv_value (blv
);
594 case SYMBOL_FORWARDED
:
595 /* In set_internal, we un-forward vars when their value is
598 default: emacs_abort ();
601 return (EQ (valcontents
, Qunbound
) ? Qnil
: Qt
);
604 /* FIXME: Make it an alias for function-symbol! */
605 DEFUN ("fboundp", Ffboundp
, Sfboundp
, 1, 1, 0,
606 doc
: /* Return t if SYMBOL's function definition is not void. */)
607 (register Lisp_Object symbol
)
609 CHECK_SYMBOL (symbol
);
610 return NILP (XSYMBOL (symbol
)->function
) ? Qnil
: Qt
;
613 DEFUN ("makunbound", Fmakunbound
, Smakunbound
, 1, 1, 0,
614 doc
: /* Make SYMBOL's value be void.
616 (register Lisp_Object symbol
)
618 CHECK_SYMBOL (symbol
);
619 if (SYMBOL_CONSTANT_P (symbol
))
620 xsignal1 (Qsetting_constant
, symbol
);
621 Fset (symbol
, Qunbound
);
625 DEFUN ("fmakunbound", Ffmakunbound
, Sfmakunbound
, 1, 1, 0,
626 doc
: /* Make SYMBOL's function definition be nil.
628 (register Lisp_Object symbol
)
630 CHECK_SYMBOL (symbol
);
631 if (NILP (symbol
) || EQ (symbol
, Qt
))
632 xsignal1 (Qsetting_constant
, symbol
);
633 set_symbol_function (symbol
, Qnil
);
637 DEFUN ("symbol-function", Fsymbol_function
, Ssymbol_function
, 1, 1, 0,
638 doc
: /* Return SYMBOL's function definition, or nil if that is void. */)
639 (register Lisp_Object symbol
)
641 CHECK_SYMBOL (symbol
);
642 return XSYMBOL (symbol
)->function
;
645 DEFUN ("symbol-plist", Fsymbol_plist
, Ssymbol_plist
, 1, 1, 0,
646 doc
: /* Return SYMBOL's property list. */)
647 (register Lisp_Object symbol
)
649 CHECK_SYMBOL (symbol
);
650 return XSYMBOL (symbol
)->plist
;
653 DEFUN ("symbol-name", Fsymbol_name
, Ssymbol_name
, 1, 1, 0,
654 doc
: /* Return SYMBOL's name, a string. */)
655 (register Lisp_Object symbol
)
657 register Lisp_Object name
;
659 CHECK_SYMBOL (symbol
);
660 name
= SYMBOL_NAME (symbol
);
664 DEFUN ("fset", Ffset
, Sfset
, 2, 2, 0,
665 doc
: /* Set SYMBOL's function definition to DEFINITION, and return DEFINITION. */)
666 (register Lisp_Object symbol
, Lisp_Object definition
)
668 register Lisp_Object function
;
669 CHECK_SYMBOL (symbol
);
671 function
= XSYMBOL (symbol
)->function
;
673 if (!NILP (Vautoload_queue
) && !NILP (function
))
674 Vautoload_queue
= Fcons (Fcons (symbol
, function
), Vautoload_queue
);
676 if (AUTOLOADP (function
))
677 Fput (symbol
, Qautoload
, XCDR (function
));
679 /* Convert to eassert or remove after GC bug is found. In the
680 meantime, check unconditionally, at a slight perf hit. */
681 if (! valid_lisp_object_p (definition
))
684 set_symbol_function (symbol
, definition
);
689 DEFUN ("defalias", Fdefalias
, Sdefalias
, 2, 3, 0,
690 doc
: /* Set SYMBOL's function definition to DEFINITION.
691 Associates the function with the current load file, if any.
692 The optional third argument DOCSTRING specifies the documentation string
693 for SYMBOL; if it is omitted or nil, SYMBOL uses the documentation string
694 determined by DEFINITION.
696 Internally, this normally uses `fset', but if SYMBOL has a
697 `defalias-fset-function' property, the associated value is used instead.
699 The return value is undefined. */)
700 (register Lisp_Object symbol
, Lisp_Object definition
, Lisp_Object docstring
)
702 CHECK_SYMBOL (symbol
);
703 if (!NILP (Vpurify_flag
)
704 /* If `definition' is a keymap, immutable (and copying) is wrong. */
705 && !KEYMAPP (definition
))
706 definition
= Fpurecopy (definition
);
709 bool autoload
= AUTOLOADP (definition
);
710 if (NILP (Vpurify_flag
) || !autoload
)
711 { /* Only add autoload entries after dumping, because the ones before are
712 not useful and else we get loads of them from the loaddefs.el. */
714 if (AUTOLOADP (XSYMBOL (symbol
)->function
))
715 /* Remember that the function was already an autoload. */
716 LOADHIST_ATTACH (Fcons (Qt
, symbol
));
717 LOADHIST_ATTACH (Fcons (autoload
? Qautoload
: Qdefun
, symbol
));
721 { /* Handle automatic advice activation. */
722 Lisp_Object hook
= Fget (symbol
, Qdefalias_fset_function
);
724 call2 (hook
, symbol
, definition
);
726 Ffset (symbol
, definition
);
729 if (!NILP (docstring
))
730 Fput (symbol
, Qfunction_documentation
, docstring
);
731 /* We used to return `definition', but now that `defun' and `defmacro' expand
732 to a call to `defalias', we return `symbol' for backward compatibility
737 DEFUN ("setplist", Fsetplist
, Ssetplist
, 2, 2, 0,
738 doc
: /* Set SYMBOL's property list to NEWPLIST, and return NEWPLIST. */)
739 (register Lisp_Object symbol
, Lisp_Object newplist
)
741 CHECK_SYMBOL (symbol
);
742 set_symbol_plist (symbol
, newplist
);
746 DEFUN ("subr-arity", Fsubr_arity
, Ssubr_arity
, 1, 1, 0,
747 doc
: /* Return minimum and maximum number of args allowed for SUBR.
748 SUBR must be a built-in function.
749 The returned value is a pair (MIN . MAX). MIN is the minimum number
750 of args. MAX is the maximum number or the symbol `many', for a
751 function with `&rest' args, or `unevalled' for a special form. */)
754 short minargs
, maxargs
;
756 minargs
= XSUBR (subr
)->min_args
;
757 maxargs
= XSUBR (subr
)->max_args
;
758 return Fcons (make_number (minargs
),
759 maxargs
== MANY
? Qmany
760 : maxargs
== UNEVALLED
? Qunevalled
761 : make_number (maxargs
));
764 DEFUN ("subr-name", Fsubr_name
, Ssubr_name
, 1, 1, 0,
765 doc
: /* Return name of subroutine SUBR.
766 SUBR must be a built-in function. */)
771 name
= XSUBR (subr
)->symbol_name
;
772 return build_string (name
);
775 DEFUN ("interactive-form", Finteractive_form
, Sinteractive_form
, 1, 1, 0,
776 doc
: /* Return the interactive form of CMD or nil if none.
777 If CMD is not a command, the return value is nil.
778 Value, if non-nil, is a list \(interactive SPEC). */)
781 Lisp_Object fun
= indirect_function (cmd
); /* Check cycles. */
786 /* Use an `interactive-form' property if present, analogous to the
787 function-documentation property. */
789 while (SYMBOLP (fun
))
791 Lisp_Object tmp
= Fget (fun
, Qinteractive_form
);
795 fun
= Fsymbol_function (fun
);
800 const char *spec
= XSUBR (fun
)->intspec
;
802 return list2 (Qinteractive
,
803 (*spec
!= '(') ? build_string (spec
) :
804 Fcar (Fread_from_string (build_string (spec
), Qnil
, Qnil
)));
806 else if (COMPILEDP (fun
))
808 if ((ASIZE (fun
) & PSEUDOVECTOR_SIZE_MASK
) > COMPILED_INTERACTIVE
)
809 return list2 (Qinteractive
, AREF (fun
, COMPILED_INTERACTIVE
));
811 else if (AUTOLOADP (fun
))
812 return Finteractive_form (Fautoload_do_load (fun
, cmd
, Qnil
));
813 else if (CONSP (fun
))
815 Lisp_Object funcar
= XCAR (fun
);
816 if (EQ (funcar
, Qclosure
))
817 return Fassq (Qinteractive
, Fcdr (Fcdr (XCDR (fun
))));
818 else if (EQ (funcar
, Qlambda
))
819 return Fassq (Qinteractive
, Fcdr (XCDR (fun
)));
825 /***********************************************************************
826 Getting and Setting Values of Symbols
827 ***********************************************************************/
829 /* Return the symbol holding SYMBOL's value. Signal
830 `cyclic-variable-indirection' if SYMBOL's chain of variable
831 indirections contains a loop. */
834 indirect_variable (struct Lisp_Symbol
*symbol
)
836 struct Lisp_Symbol
*tortoise
, *hare
;
838 hare
= tortoise
= symbol
;
840 while (hare
->redirect
== SYMBOL_VARALIAS
)
842 hare
= SYMBOL_ALIAS (hare
);
843 if (hare
->redirect
!= SYMBOL_VARALIAS
)
846 hare
= SYMBOL_ALIAS (hare
);
847 tortoise
= SYMBOL_ALIAS (tortoise
);
849 if (hare
== tortoise
)
852 XSETSYMBOL (tem
, symbol
);
853 xsignal1 (Qcyclic_variable_indirection
, tem
);
861 DEFUN ("indirect-variable", Findirect_variable
, Sindirect_variable
, 1, 1, 0,
862 doc
: /* Return the variable at the end of OBJECT's variable chain.
863 If OBJECT is a symbol, follow its variable indirections (if any), and
864 return the variable at the end of the chain of aliases. See Info node
865 `(elisp)Variable Aliases'.
867 If OBJECT is not a symbol, just return it. If there is a loop in the
868 chain of aliases, signal a `cyclic-variable-indirection' error. */)
871 if (SYMBOLP (object
))
873 struct Lisp_Symbol
*sym
= indirect_variable (XSYMBOL (object
));
874 XSETSYMBOL (object
, sym
);
880 /* Given the raw contents of a symbol value cell,
881 return the Lisp value of the symbol.
882 This does not handle buffer-local variables; use
883 swap_in_symval_forwarding for that. */
886 do_symval_forwarding (register union Lisp_Fwd
*valcontents
)
888 register Lisp_Object val
;
889 switch (XFWDTYPE (valcontents
))
892 XSETINT (val
, *XINTFWD (valcontents
)->intvar
);
896 return (*XBOOLFWD (valcontents
)->boolvar
? Qt
: Qnil
);
899 return *XOBJFWD (valcontents
)->objvar
;
901 case Lisp_Fwd_Buffer_Obj
:
902 return per_buffer_value (current_buffer
,
903 XBUFFER_OBJFWD (valcontents
)->offset
);
905 case Lisp_Fwd_Kboard_Obj
:
906 /* We used to simply use current_kboard here, but from Lisp
907 code, its value is often unexpected. It seems nicer to
908 allow constructions like this to work as intuitively expected:
910 (with-selected-frame frame
911 (define-key local-function-map "\eOP" [f1]))
913 On the other hand, this affects the semantics of
914 last-command and real-last-command, and people may rely on
915 that. I took a quick look at the Lisp codebase, and I
916 don't think anything will break. --lorentey */
917 return *(Lisp_Object
*)(XKBOARD_OBJFWD (valcontents
)->offset
918 + (char *)FRAME_KBOARD (SELECTED_FRAME ()));
919 default: emacs_abort ();
923 /* Used to signal a user-friendly error when symbol WRONG is
924 not a member of CHOICE, which should be a list of symbols. */
927 wrong_choice (Lisp_Object choice
, Lisp_Object wrong
)
929 ptrdiff_t i
= 0, len
= XINT (Flength (choice
));
930 Lisp_Object obj
, *args
;
931 AUTO_STRING (one_of
, "One of ");
932 AUTO_STRING (comma
, ", ");
933 AUTO_STRING (or, " or ");
934 AUTO_STRING (should_be_specified
, " should be specified");
937 SAFE_ALLOCA_LISP (args
, len
* 2 + 1);
941 for (obj
= choice
; !NILP (obj
); obj
= XCDR (obj
))
943 args
[i
++] = SYMBOL_NAME (XCAR (obj
));
944 args
[i
++] = (NILP (XCDR (obj
)) ? should_be_specified
945 : NILP (XCDR (XCDR (obj
))) ? or : comma
);
948 obj
= Fconcat (i
, args
);
950 xsignal2 (Qerror
, obj
, wrong
);
953 /* Used to signal a user-friendly error if WRONG is not a number or
954 integer/floating-point number outsize of inclusive MIN..MAX range. */
957 wrong_range (Lisp_Object min
, Lisp_Object max
, Lisp_Object wrong
)
959 AUTO_STRING (value_should_be_from
, "Value should be from ");
960 AUTO_STRING (to
, " to ");
962 Fconcat (4, ((Lisp_Object
[])
963 {value_should_be_from
, Fnumber_to_string (min
),
964 to
, Fnumber_to_string (max
)})),
968 /* Store NEWVAL into SYMBOL, where VALCONTENTS is found in the value cell
969 of SYMBOL. If SYMBOL is buffer-local, VALCONTENTS should be the
970 buffer-independent contents of the value cell: forwarded just one
971 step past the buffer-localness.
973 BUF non-zero means set the value in buffer BUF instead of the
974 current buffer. This only plays a role for per-buffer variables. */
977 store_symval_forwarding (union Lisp_Fwd
*valcontents
, register Lisp_Object newval
, struct buffer
*buf
)
979 switch (XFWDTYPE (valcontents
))
982 CHECK_NUMBER (newval
);
983 *XINTFWD (valcontents
)->intvar
= XINT (newval
);
987 *XBOOLFWD (valcontents
)->boolvar
= !NILP (newval
);
991 *XOBJFWD (valcontents
)->objvar
= newval
;
993 /* If this variable is a default for something stored
994 in the buffer itself, such as default-fill-column,
995 find the buffers that don't have local values for it
997 if (XOBJFWD (valcontents
)->objvar
> (Lisp_Object
*) &buffer_defaults
998 && XOBJFWD (valcontents
)->objvar
< (Lisp_Object
*) (&buffer_defaults
+ 1))
1000 int offset
= ((char *) XOBJFWD (valcontents
)->objvar
1001 - (char *) &buffer_defaults
);
1002 int idx
= PER_BUFFER_IDX (offset
);
1004 Lisp_Object tail
, buf
;
1009 FOR_EACH_LIVE_BUFFER (tail
, buf
)
1011 struct buffer
*b
= XBUFFER (buf
);
1013 if (! PER_BUFFER_VALUE_P (b
, idx
))
1014 set_per_buffer_value (b
, offset
, newval
);
1019 case Lisp_Fwd_Buffer_Obj
:
1021 int offset
= XBUFFER_OBJFWD (valcontents
)->offset
;
1022 Lisp_Object predicate
= XBUFFER_OBJFWD (valcontents
)->predicate
;
1026 if (SYMBOLP (predicate
))
1030 if ((prop
= Fget (predicate
, Qchoice
), !NILP (prop
)))
1032 if (NILP (Fmemq (newval
, prop
)))
1033 wrong_choice (prop
, newval
);
1035 else if ((prop
= Fget (predicate
, Qrange
), !NILP (prop
)))
1037 Lisp_Object min
= XCAR (prop
), max
= XCDR (prop
);
1039 if (!NUMBERP (newval
)
1040 || !NILP (arithcompare (newval
, min
, ARITH_LESS
))
1041 || !NILP (arithcompare (newval
, max
, ARITH_GRTR
)))
1042 wrong_range (min
, max
, newval
);
1044 else if (FUNCTIONP (predicate
))
1046 if (NILP (call1 (predicate
, newval
)))
1047 wrong_type_argument (predicate
, newval
);
1052 buf
= current_buffer
;
1053 set_per_buffer_value (buf
, offset
, newval
);
1057 case Lisp_Fwd_Kboard_Obj
:
1059 char *base
= (char *) FRAME_KBOARD (SELECTED_FRAME ());
1060 char *p
= base
+ XKBOARD_OBJFWD (valcontents
)->offset
;
1061 *(Lisp_Object
*) p
= newval
;
1066 emacs_abort (); /* goto def; */
1070 /* Set up SYMBOL to refer to its global binding. This makes it safe
1071 to alter the status of other bindings. BEWARE: this may be called
1072 during the mark phase of GC, where we assume that Lisp_Object slots
1073 of BLV are marked after this function has changed them. */
1076 swap_in_global_binding (struct Lisp_Symbol
*symbol
)
1078 struct Lisp_Buffer_Local_Value
*blv
= SYMBOL_BLV (symbol
);
1080 /* Unload the previously loaded binding. */
1082 set_blv_value (blv
, do_symval_forwarding (blv
->fwd
));
1084 /* Select the global binding in the symbol. */
1085 set_blv_valcell (blv
, blv
->defcell
);
1087 store_symval_forwarding (blv
->fwd
, XCDR (blv
->defcell
), NULL
);
1089 /* Indicate that the global binding is set up now. */
1090 set_blv_where (blv
, Qnil
);
1091 set_blv_found (blv
, 0);
1094 /* Set up the buffer-local symbol SYMBOL for validity in the current buffer.
1095 VALCONTENTS is the contents of its value cell,
1096 which points to a struct Lisp_Buffer_Local_Value.
1098 Return the value forwarded one step past the buffer-local stage.
1099 This could be another forwarding pointer. */
1102 swap_in_symval_forwarding (struct Lisp_Symbol
*symbol
, struct Lisp_Buffer_Local_Value
*blv
)
1104 register Lisp_Object tem1
;
1106 eassert (blv
== SYMBOL_BLV (symbol
));
1111 || (blv
->frame_local
1112 ? !EQ (selected_frame
, tem1
)
1113 : current_buffer
!= XBUFFER (tem1
)))
1116 /* Unload the previously loaded binding. */
1117 tem1
= blv
->valcell
;
1119 set_blv_value (blv
, do_symval_forwarding (blv
->fwd
));
1120 /* Choose the new binding. */
1123 XSETSYMBOL (var
, symbol
);
1124 if (blv
->frame_local
)
1126 tem1
= assq_no_quit (var
, XFRAME (selected_frame
)->param_alist
);
1127 set_blv_where (blv
, selected_frame
);
1131 tem1
= assq_no_quit (var
, BVAR (current_buffer
, local_var_alist
));
1132 set_blv_where (blv
, Fcurrent_buffer ());
1135 if (!(blv
->found
= !NILP (tem1
)))
1136 tem1
= blv
->defcell
;
1138 /* Load the new binding. */
1139 set_blv_valcell (blv
, tem1
);
1141 store_symval_forwarding (blv
->fwd
, blv_value (blv
), NULL
);
1145 /* Find the value of a symbol, returning Qunbound if it's not bound.
1146 This is helpful for code which just wants to get a variable's value
1147 if it has one, without signaling an error.
1148 Note that it must not be possible to quit
1149 within this function. Great care is required for this. */
1152 find_symbol_value (Lisp_Object symbol
)
1154 struct Lisp_Symbol
*sym
;
1156 CHECK_SYMBOL (symbol
);
1157 sym
= XSYMBOL (symbol
);
1160 switch (sym
->redirect
)
1162 case SYMBOL_VARALIAS
: sym
= indirect_variable (sym
); goto start
;
1163 case SYMBOL_PLAINVAL
: return SYMBOL_VAL (sym
);
1164 case SYMBOL_LOCALIZED
:
1166 struct Lisp_Buffer_Local_Value
*blv
= SYMBOL_BLV (sym
);
1167 swap_in_symval_forwarding (sym
, blv
);
1168 return blv
->fwd
? do_symval_forwarding (blv
->fwd
) : blv_value (blv
);
1171 case SYMBOL_FORWARDED
:
1172 return do_symval_forwarding (SYMBOL_FWD (sym
));
1173 default: emacs_abort ();
1177 DEFUN ("symbol-value", Fsymbol_value
, Ssymbol_value
, 1, 1, 0,
1178 doc
: /* Return SYMBOL's value. Error if that is void.
1179 Note that if `lexical-binding' is in effect, this returns the
1180 global value outside of any lexical scope. */)
1181 (Lisp_Object symbol
)
1185 val
= find_symbol_value (symbol
);
1186 if (!EQ (val
, Qunbound
))
1189 xsignal1 (Qvoid_variable
, symbol
);
1192 DEFUN ("set", Fset
, Sset
, 2, 2, 0,
1193 doc
: /* Set SYMBOL's value to NEWVAL, and return NEWVAL. */)
1194 (register Lisp_Object symbol
, Lisp_Object newval
)
1196 set_internal (symbol
, newval
, Qnil
, 0);
1200 /* Store the value NEWVAL into SYMBOL.
1201 If buffer/frame-locality is an issue, WHERE specifies which context to use.
1202 (nil stands for the current buffer/frame).
1204 If BINDFLAG is false, then if this symbol is supposed to become
1205 local in every buffer where it is set, then we make it local.
1206 If BINDFLAG is true, we don't do that. */
1209 set_internal (Lisp_Object symbol
, Lisp_Object newval
, Lisp_Object where
,
1212 bool voide
= EQ (newval
, Qunbound
);
1213 struct Lisp_Symbol
*sym
;
1216 /* If restoring in a dead buffer, do nothing. */
1217 /* if (BUFFERP (where) && NILP (XBUFFER (where)->name))
1220 CHECK_SYMBOL (symbol
);
1221 if (SYMBOL_CONSTANT_P (symbol
))
1223 if (NILP (Fkeywordp (symbol
))
1224 || !EQ (newval
, Fsymbol_value (symbol
)))
1225 xsignal1 (Qsetting_constant
, symbol
);
1227 /* Allow setting keywords to their own value. */
1231 sym
= XSYMBOL (symbol
);
1234 switch (sym
->redirect
)
1236 case SYMBOL_VARALIAS
: sym
= indirect_variable (sym
); goto start
;
1237 case SYMBOL_PLAINVAL
: SET_SYMBOL_VAL (sym
, newval
); return;
1238 case SYMBOL_LOCALIZED
:
1240 struct Lisp_Buffer_Local_Value
*blv
= SYMBOL_BLV (sym
);
1243 if (blv
->frame_local
)
1244 where
= selected_frame
;
1246 XSETBUFFER (where
, current_buffer
);
1248 /* If the current buffer is not the buffer whose binding is
1249 loaded, or if there may be frame-local bindings and the frame
1250 isn't the right one, or if it's a Lisp_Buffer_Local_Value and
1251 the default binding is loaded, the loaded binding may be the
1253 if (!EQ (blv
->where
, where
)
1254 /* Also unload a global binding (if the var is local_if_set). */
1255 || (EQ (blv
->valcell
, blv
->defcell
)))
1257 /* The currently loaded binding is not necessarily valid.
1258 We need to unload it, and choose a new binding. */
1260 /* Write out `realvalue' to the old loaded binding. */
1262 set_blv_value (blv
, do_symval_forwarding (blv
->fwd
));
1264 /* Find the new binding. */
1265 XSETSYMBOL (symbol
, sym
); /* May have changed via aliasing. */
1266 tem1
= assq_no_quit (symbol
,
1268 ? XFRAME (where
)->param_alist
1269 : BVAR (XBUFFER (where
), local_var_alist
)));
1270 set_blv_where (blv
, where
);
1275 /* This buffer still sees the default value. */
1277 /* If the variable is a Lisp_Some_Buffer_Local_Value,
1278 or if this is `let' rather than `set',
1279 make CURRENT-ALIST-ELEMENT point to itself,
1280 indicating that we're seeing the default value.
1281 Likewise if the variable has been let-bound
1282 in the current buffer. */
1283 if (bindflag
|| !blv
->local_if_set
1284 || let_shadows_buffer_binding_p (sym
))
1287 tem1
= blv
->defcell
;
1289 /* If it's a local_if_set, being set not bound,
1290 and we're not within a let that was made for this buffer,
1291 create a new buffer-local binding for the variable.
1292 That means, give this buffer a new assoc for a local value
1293 and load that binding. */
1296 /* local_if_set is only supported for buffer-local
1297 bindings, not for frame-local bindings. */
1298 eassert (!blv
->frame_local
);
1299 tem1
= Fcons (symbol
, XCDR (blv
->defcell
));
1300 bset_local_var_alist
1302 Fcons (tem1
, BVAR (XBUFFER (where
), local_var_alist
)));
1306 /* Record which binding is now loaded. */
1307 set_blv_valcell (blv
, tem1
);
1310 /* Store the new value in the cons cell. */
1311 set_blv_value (blv
, newval
);
1316 /* If storing void (making the symbol void), forward only through
1317 buffer-local indicator, not through Lisp_Objfwd, etc. */
1320 store_symval_forwarding (blv
->fwd
, newval
,
1322 ? XBUFFER (where
) : current_buffer
);
1326 case SYMBOL_FORWARDED
:
1329 = BUFFERP (where
) ? XBUFFER (where
) : current_buffer
;
1330 union Lisp_Fwd
*innercontents
= SYMBOL_FWD (sym
);
1331 if (BUFFER_OBJFWDP (innercontents
))
1333 int offset
= XBUFFER_OBJFWD (innercontents
)->offset
;
1334 int idx
= PER_BUFFER_IDX (offset
);
1337 && !let_shadows_buffer_binding_p (sym
))
1338 SET_PER_BUFFER_VALUE_P (buf
, idx
, 1);
1342 { /* If storing void (making the symbol void), forward only through
1343 buffer-local indicator, not through Lisp_Objfwd, etc. */
1344 sym
->redirect
= SYMBOL_PLAINVAL
;
1345 SET_SYMBOL_VAL (sym
, newval
);
1348 store_symval_forwarding (/* sym, */ innercontents
, newval
, buf
);
1351 default: emacs_abort ();
1356 /* Access or set a buffer-local symbol's default value. */
1358 /* Return the default value of SYMBOL, but don't check for voidness.
1359 Return Qunbound if it is void. */
1362 default_value (Lisp_Object symbol
)
1364 struct Lisp_Symbol
*sym
;
1366 CHECK_SYMBOL (symbol
);
1367 sym
= XSYMBOL (symbol
);
1370 switch (sym
->redirect
)
1372 case SYMBOL_VARALIAS
: sym
= indirect_variable (sym
); goto start
;
1373 case SYMBOL_PLAINVAL
: return SYMBOL_VAL (sym
);
1374 case SYMBOL_LOCALIZED
:
1376 /* If var is set up for a buffer that lacks a local value for it,
1377 the current value is nominally the default value.
1378 But the `realvalue' slot may be more up to date, since
1379 ordinary setq stores just that slot. So use that. */
1380 struct Lisp_Buffer_Local_Value
*blv
= SYMBOL_BLV (sym
);
1381 if (blv
->fwd
&& EQ (blv
->valcell
, blv
->defcell
))
1382 return do_symval_forwarding (blv
->fwd
);
1384 return XCDR (blv
->defcell
);
1386 case SYMBOL_FORWARDED
:
1388 union Lisp_Fwd
*valcontents
= SYMBOL_FWD (sym
);
1390 /* For a built-in buffer-local variable, get the default value
1391 rather than letting do_symval_forwarding get the current value. */
1392 if (BUFFER_OBJFWDP (valcontents
))
1394 int offset
= XBUFFER_OBJFWD (valcontents
)->offset
;
1395 if (PER_BUFFER_IDX (offset
) != 0)
1396 return per_buffer_default (offset
);
1399 /* For other variables, get the current value. */
1400 return do_symval_forwarding (valcontents
);
1402 default: emacs_abort ();
1406 DEFUN ("default-boundp", Fdefault_boundp
, Sdefault_boundp
, 1, 1, 0,
1407 doc
: /* Return t if SYMBOL has a non-void default value.
1408 This is the value that is seen in buffers that do not have their own values
1409 for this variable. */)
1410 (Lisp_Object symbol
)
1412 register Lisp_Object value
;
1414 value
= default_value (symbol
);
1415 return (EQ (value
, Qunbound
) ? Qnil
: Qt
);
1418 DEFUN ("default-value", Fdefault_value
, Sdefault_value
, 1, 1, 0,
1419 doc
: /* Return SYMBOL's default value.
1420 This is the value that is seen in buffers that do not have their own values
1421 for this variable. The default value is meaningful for variables with
1422 local bindings in certain buffers. */)
1423 (Lisp_Object symbol
)
1425 Lisp_Object value
= default_value (symbol
);
1426 if (!EQ (value
, Qunbound
))
1429 xsignal1 (Qvoid_variable
, symbol
);
1432 DEFUN ("set-default", Fset_default
, Sset_default
, 2, 2, 0,
1433 doc
: /* Set SYMBOL's default value to VALUE. SYMBOL and VALUE are evaluated.
1434 The default value is seen in buffers that do not have their own values
1435 for this variable. */)
1436 (Lisp_Object symbol
, Lisp_Object value
)
1438 struct Lisp_Symbol
*sym
;
1440 CHECK_SYMBOL (symbol
);
1441 if (SYMBOL_CONSTANT_P (symbol
))
1443 if (NILP (Fkeywordp (symbol
))
1444 || !EQ (value
, Fdefault_value (symbol
)))
1445 xsignal1 (Qsetting_constant
, symbol
);
1447 /* Allow setting keywords to their own value. */
1450 sym
= XSYMBOL (symbol
);
1453 switch (sym
->redirect
)
1455 case SYMBOL_VARALIAS
: sym
= indirect_variable (sym
); goto start
;
1456 case SYMBOL_PLAINVAL
: return Fset (symbol
, value
);
1457 case SYMBOL_LOCALIZED
:
1459 struct Lisp_Buffer_Local_Value
*blv
= SYMBOL_BLV (sym
);
1461 /* Store new value into the DEFAULT-VALUE slot. */
1462 XSETCDR (blv
->defcell
, value
);
1464 /* If the default binding is now loaded, set the REALVALUE slot too. */
1465 if (blv
->fwd
&& EQ (blv
->defcell
, blv
->valcell
))
1466 store_symval_forwarding (blv
->fwd
, value
, NULL
);
1469 case SYMBOL_FORWARDED
:
1471 union Lisp_Fwd
*valcontents
= SYMBOL_FWD (sym
);
1473 /* Handle variables like case-fold-search that have special slots
1475 Make them work apparently like Lisp_Buffer_Local_Value variables. */
1476 if (BUFFER_OBJFWDP (valcontents
))
1478 int offset
= XBUFFER_OBJFWD (valcontents
)->offset
;
1479 int idx
= PER_BUFFER_IDX (offset
);
1481 set_per_buffer_default (offset
, value
);
1483 /* If this variable is not always local in all buffers,
1484 set it in the buffers that don't nominally have a local value. */
1490 if (!PER_BUFFER_VALUE_P (b
, idx
))
1491 set_per_buffer_value (b
, offset
, value
);
1496 return Fset (symbol
, value
);
1498 default: emacs_abort ();
1502 DEFUN ("setq-default", Fsetq_default
, Ssetq_default
, 0, UNEVALLED
, 0,
1503 doc
: /* Set the default value of variable VAR to VALUE.
1504 VAR, the variable name, is literal (not evaluated);
1505 VALUE is an expression: it is evaluated and its value returned.
1506 The default value of a variable is seen in buffers
1507 that do not have their own values for the variable.
1509 More generally, you can use multiple variables and values, as in
1510 (setq-default VAR VALUE VAR VALUE...)
1511 This sets each VAR's default value to the corresponding VALUE.
1512 The VALUE for the Nth VAR can refer to the new default values
1514 usage: (setq-default [VAR VALUE]...) */)
1517 Lisp_Object args_left
, symbol
, val
;
1518 struct gcpro gcpro1
;
1520 args_left
= val
= args
;
1523 while (CONSP (args_left
))
1525 val
= eval_sub (Fcar (XCDR (args_left
)));
1526 symbol
= XCAR (args_left
);
1527 Fset_default (symbol
, val
);
1528 args_left
= Fcdr (XCDR (args_left
));
1535 /* Lisp functions for creating and removing buffer-local variables. */
1540 union Lisp_Fwd
*fwd
;
1543 static struct Lisp_Buffer_Local_Value
*
1544 make_blv (struct Lisp_Symbol
*sym
, bool forwarded
,
1545 union Lisp_Val_Fwd valcontents
)
1547 struct Lisp_Buffer_Local_Value
*blv
= xmalloc (sizeof *blv
);
1551 XSETSYMBOL (symbol
, sym
);
1552 tem
= Fcons (symbol
, (forwarded
1553 ? do_symval_forwarding (valcontents
.fwd
)
1554 : valcontents
.value
));
1556 /* Buffer_Local_Values cannot have as realval a buffer-local
1557 or keyboard-local forwarding. */
1558 eassert (!(forwarded
&& BUFFER_OBJFWDP (valcontents
.fwd
)));
1559 eassert (!(forwarded
&& KBOARD_OBJFWDP (valcontents
.fwd
)));
1560 blv
->fwd
= forwarded
? valcontents
.fwd
: NULL
;
1561 set_blv_where (blv
, Qnil
);
1562 blv
->frame_local
= 0;
1563 blv
->local_if_set
= 0;
1564 set_blv_defcell (blv
, tem
);
1565 set_blv_valcell (blv
, tem
);
1566 set_blv_found (blv
, 0);
1570 DEFUN ("make-variable-buffer-local", Fmake_variable_buffer_local
,
1571 Smake_variable_buffer_local
, 1, 1, "vMake Variable Buffer Local: ",
1572 doc
: /* Make VARIABLE become buffer-local whenever it is set.
1573 At any time, the value for the current buffer is in effect,
1574 unless the variable has never been set in this buffer,
1575 in which case the default value is in effect.
1576 Note that binding the variable with `let', or setting it while
1577 a `let'-style binding made in this buffer is in effect,
1578 does not make the variable buffer-local. Return VARIABLE.
1580 This globally affects all uses of this variable, so it belongs together with
1581 the variable declaration, rather than with its uses (if you just want to make
1582 a variable local to the current buffer for one particular use, use
1583 `make-local-variable'). Buffer-local bindings are normally cleared
1584 while setting up a new major mode, unless they have a `permanent-local'
1587 The function `default-value' gets the default value and `set-default' sets it. */)
1588 (register Lisp_Object variable
)
1590 struct Lisp_Symbol
*sym
;
1591 struct Lisp_Buffer_Local_Value
*blv
= NULL
;
1592 union Lisp_Val_Fwd valcontents
IF_LINT (= {LISP_INITIALLY_ZERO
});
1593 bool forwarded
IF_LINT (= 0);
1595 CHECK_SYMBOL (variable
);
1596 sym
= XSYMBOL (variable
);
1599 switch (sym
->redirect
)
1601 case SYMBOL_VARALIAS
: sym
= indirect_variable (sym
); goto start
;
1602 case SYMBOL_PLAINVAL
:
1603 forwarded
= 0; valcontents
.value
= SYMBOL_VAL (sym
);
1604 if (EQ (valcontents
.value
, Qunbound
))
1605 valcontents
.value
= Qnil
;
1607 case SYMBOL_LOCALIZED
:
1608 blv
= SYMBOL_BLV (sym
);
1609 if (blv
->frame_local
)
1610 error ("Symbol %s may not be buffer-local",
1611 SDATA (SYMBOL_NAME (variable
)));
1613 case SYMBOL_FORWARDED
:
1614 forwarded
= 1; valcontents
.fwd
= SYMBOL_FWD (sym
);
1615 if (KBOARD_OBJFWDP (valcontents
.fwd
))
1616 error ("Symbol %s may not be buffer-local",
1617 SDATA (SYMBOL_NAME (variable
)));
1618 else if (BUFFER_OBJFWDP (valcontents
.fwd
))
1621 default: emacs_abort ();
1625 error ("Symbol %s may not be buffer-local", SDATA (SYMBOL_NAME (variable
)));
1629 blv
= make_blv (sym
, forwarded
, valcontents
);
1630 sym
->redirect
= SYMBOL_LOCALIZED
;
1631 SET_SYMBOL_BLV (sym
, blv
);
1634 XSETSYMBOL (symbol
, sym
); /* In case `variable' is aliased. */
1635 if (let_shadows_global_binding_p (symbol
))
1636 message ("Making %s buffer-local while let-bound!",
1637 SDATA (SYMBOL_NAME (variable
)));
1641 blv
->local_if_set
= 1;
1645 DEFUN ("make-local-variable", Fmake_local_variable
, Smake_local_variable
,
1646 1, 1, "vMake Local Variable: ",
1647 doc
: /* Make VARIABLE have a separate value in the current buffer.
1648 Other buffers will continue to share a common default value.
1649 \(The buffer-local value of VARIABLE starts out as the same value
1650 VARIABLE previously had. If VARIABLE was void, it remains void.\)
1653 If the variable is already arranged to become local when set,
1654 this function causes a local value to exist for this buffer,
1655 just as setting the variable would do.
1657 This function returns VARIABLE, and therefore
1658 (set (make-local-variable 'VARIABLE) VALUE-EXP)
1661 See also `make-variable-buffer-local'.
1663 Do not use `make-local-variable' to make a hook variable buffer-local.
1664 Instead, use `add-hook' and specify t for the LOCAL argument. */)
1665 (Lisp_Object variable
)
1668 bool forwarded
IF_LINT (= 0);
1669 union Lisp_Val_Fwd valcontents
IF_LINT (= {LISP_INITIALLY_ZERO
});
1670 struct Lisp_Symbol
*sym
;
1671 struct Lisp_Buffer_Local_Value
*blv
= NULL
;
1673 CHECK_SYMBOL (variable
);
1674 sym
= XSYMBOL (variable
);
1677 switch (sym
->redirect
)
1679 case SYMBOL_VARALIAS
: sym
= indirect_variable (sym
); goto start
;
1680 case SYMBOL_PLAINVAL
:
1681 forwarded
= 0; valcontents
.value
= SYMBOL_VAL (sym
); break;
1682 case SYMBOL_LOCALIZED
:
1683 blv
= SYMBOL_BLV (sym
);
1684 if (blv
->frame_local
)
1685 error ("Symbol %s may not be buffer-local",
1686 SDATA (SYMBOL_NAME (variable
)));
1688 case SYMBOL_FORWARDED
:
1689 forwarded
= 1; valcontents
.fwd
= SYMBOL_FWD (sym
);
1690 if (KBOARD_OBJFWDP (valcontents
.fwd
))
1691 error ("Symbol %s may not be buffer-local",
1692 SDATA (SYMBOL_NAME (variable
)));
1694 default: emacs_abort ();
1698 error ("Symbol %s may not be buffer-local",
1699 SDATA (SYMBOL_NAME (variable
)));
1701 if (blv
? blv
->local_if_set
1702 : (forwarded
&& BUFFER_OBJFWDP (valcontents
.fwd
)))
1704 tem
= Fboundp (variable
);
1705 /* Make sure the symbol has a local value in this particular buffer,
1706 by setting it to the same value it already has. */
1707 Fset (variable
, (EQ (tem
, Qt
) ? Fsymbol_value (variable
) : Qunbound
));
1712 blv
= make_blv (sym
, forwarded
, valcontents
);
1713 sym
->redirect
= SYMBOL_LOCALIZED
;
1714 SET_SYMBOL_BLV (sym
, blv
);
1717 XSETSYMBOL (symbol
, sym
); /* In case `variable' is aliased. */
1718 if (let_shadows_global_binding_p (symbol
))
1719 message ("Making %s local to %s while let-bound!",
1720 SDATA (SYMBOL_NAME (variable
)),
1721 SDATA (BVAR (current_buffer
, name
)));
1725 /* Make sure this buffer has its own value of symbol. */
1726 XSETSYMBOL (variable
, sym
); /* Update in case of aliasing. */
1727 tem
= Fassq (variable
, BVAR (current_buffer
, local_var_alist
));
1730 if (let_shadows_buffer_binding_p (sym
))
1731 message ("Making %s buffer-local while locally let-bound!",
1732 SDATA (SYMBOL_NAME (variable
)));
1734 /* Swap out any local binding for some other buffer, and make
1735 sure the current value is permanently recorded, if it's the
1737 find_symbol_value (variable
);
1739 bset_local_var_alist
1741 Fcons (Fcons (variable
, XCDR (blv
->defcell
)),
1742 BVAR (current_buffer
, local_var_alist
)));
1744 /* Make sure symbol does not think it is set up for this buffer;
1745 force it to look once again for this buffer's value. */
1746 if (current_buffer
== XBUFFER (blv
->where
))
1747 set_blv_where (blv
, Qnil
);
1748 set_blv_found (blv
, 0);
1751 /* If the symbol forwards into a C variable, then load the binding
1752 for this buffer now. If C code modifies the variable before we
1753 load the binding in, then that new value will clobber the default
1754 binding the next time we unload it. */
1756 swap_in_symval_forwarding (sym
, blv
);
1761 DEFUN ("kill-local-variable", Fkill_local_variable
, Skill_local_variable
,
1762 1, 1, "vKill Local Variable: ",
1763 doc
: /* Make VARIABLE no longer have a separate value in the current buffer.
1764 From now on the default value will apply in this buffer. Return VARIABLE. */)
1765 (register Lisp_Object variable
)
1767 register Lisp_Object tem
;
1768 struct Lisp_Buffer_Local_Value
*blv
;
1769 struct Lisp_Symbol
*sym
;
1771 CHECK_SYMBOL (variable
);
1772 sym
= XSYMBOL (variable
);
1775 switch (sym
->redirect
)
1777 case SYMBOL_VARALIAS
: sym
= indirect_variable (sym
); goto start
;
1778 case SYMBOL_PLAINVAL
: return variable
;
1779 case SYMBOL_FORWARDED
:
1781 union Lisp_Fwd
*valcontents
= SYMBOL_FWD (sym
);
1782 if (BUFFER_OBJFWDP (valcontents
))
1784 int offset
= XBUFFER_OBJFWD (valcontents
)->offset
;
1785 int idx
= PER_BUFFER_IDX (offset
);
1789 SET_PER_BUFFER_VALUE_P (current_buffer
, idx
, 0);
1790 set_per_buffer_value (current_buffer
, offset
,
1791 per_buffer_default (offset
));
1796 case SYMBOL_LOCALIZED
:
1797 blv
= SYMBOL_BLV (sym
);
1798 if (blv
->frame_local
)
1801 default: emacs_abort ();
1804 /* Get rid of this buffer's alist element, if any. */
1805 XSETSYMBOL (variable
, sym
); /* Propagate variable indirection. */
1806 tem
= Fassq (variable
, BVAR (current_buffer
, local_var_alist
));
1808 bset_local_var_alist
1810 Fdelq (tem
, BVAR (current_buffer
, local_var_alist
)));
1812 /* If the symbol is set up with the current buffer's binding
1813 loaded, recompute its value. We have to do it now, or else
1814 forwarded objects won't work right. */
1816 Lisp_Object buf
; XSETBUFFER (buf
, current_buffer
);
1817 if (EQ (buf
, blv
->where
))
1819 set_blv_where (blv
, Qnil
);
1821 find_symbol_value (variable
);
1828 /* Lisp functions for creating and removing buffer-local variables. */
1830 /* Obsolete since 22.2. NB adjust doc of modify-frame-parameters
1831 when/if this is removed. */
1833 DEFUN ("make-variable-frame-local", Fmake_variable_frame_local
, Smake_variable_frame_local
,
1834 1, 1, "vMake Variable Frame Local: ",
1835 doc
: /* Enable VARIABLE to have frame-local bindings.
1836 This does not create any frame-local bindings for VARIABLE,
1837 it just makes them possible.
1839 A frame-local binding is actually a frame parameter value.
1840 If a frame F has a value for the frame parameter named VARIABLE,
1841 that also acts as a frame-local binding for VARIABLE in F--
1842 provided this function has been called to enable VARIABLE
1843 to have frame-local bindings at all.
1845 The only way to create a frame-local binding for VARIABLE in a frame
1846 is to set the VARIABLE frame parameter of that frame. See
1847 `modify-frame-parameters' for how to set frame parameters.
1849 Note that since Emacs 23.1, variables cannot be both buffer-local and
1850 frame-local any more (buffer-local bindings used to take precedence over
1851 frame-local bindings). */)
1852 (Lisp_Object variable
)
1855 union Lisp_Val_Fwd valcontents
;
1856 struct Lisp_Symbol
*sym
;
1857 struct Lisp_Buffer_Local_Value
*blv
= NULL
;
1859 CHECK_SYMBOL (variable
);
1860 sym
= XSYMBOL (variable
);
1863 switch (sym
->redirect
)
1865 case SYMBOL_VARALIAS
: sym
= indirect_variable (sym
); goto start
;
1866 case SYMBOL_PLAINVAL
:
1867 forwarded
= 0; valcontents
.value
= SYMBOL_VAL (sym
);
1868 if (EQ (valcontents
.value
, Qunbound
))
1869 valcontents
.value
= Qnil
;
1871 case SYMBOL_LOCALIZED
:
1872 if (SYMBOL_BLV (sym
)->frame_local
)
1875 error ("Symbol %s may not be frame-local",
1876 SDATA (SYMBOL_NAME (variable
)));
1877 case SYMBOL_FORWARDED
:
1878 forwarded
= 1; valcontents
.fwd
= SYMBOL_FWD (sym
);
1879 if (KBOARD_OBJFWDP (valcontents
.fwd
) || BUFFER_OBJFWDP (valcontents
.fwd
))
1880 error ("Symbol %s may not be frame-local",
1881 SDATA (SYMBOL_NAME (variable
)));
1883 default: emacs_abort ();
1887 error ("Symbol %s may not be frame-local", SDATA (SYMBOL_NAME (variable
)));
1889 blv
= make_blv (sym
, forwarded
, valcontents
);
1890 blv
->frame_local
= 1;
1891 sym
->redirect
= SYMBOL_LOCALIZED
;
1892 SET_SYMBOL_BLV (sym
, blv
);
1895 XSETSYMBOL (symbol
, sym
); /* In case `variable' is aliased. */
1896 if (let_shadows_global_binding_p (symbol
))
1897 message ("Making %s frame-local while let-bound!",
1898 SDATA (SYMBOL_NAME (variable
)));
1903 DEFUN ("local-variable-p", Flocal_variable_p
, Slocal_variable_p
,
1905 doc
: /* Non-nil if VARIABLE has a local binding in buffer BUFFER.
1906 BUFFER defaults to the current buffer. */)
1907 (Lisp_Object variable
, Lisp_Object buffer
)
1909 struct buffer
*buf
= decode_buffer (buffer
);
1910 struct Lisp_Symbol
*sym
;
1912 CHECK_SYMBOL (variable
);
1913 sym
= XSYMBOL (variable
);
1916 switch (sym
->redirect
)
1918 case SYMBOL_VARALIAS
: sym
= indirect_variable (sym
); goto start
;
1919 case SYMBOL_PLAINVAL
: return Qnil
;
1920 case SYMBOL_LOCALIZED
:
1922 Lisp_Object tail
, elt
, tmp
;
1923 struct Lisp_Buffer_Local_Value
*blv
= SYMBOL_BLV (sym
);
1924 XSETBUFFER (tmp
, buf
);
1925 XSETSYMBOL (variable
, sym
); /* Update in case of aliasing. */
1927 if (EQ (blv
->where
, tmp
)) /* The binding is already loaded. */
1928 return blv_found (blv
) ? Qt
: Qnil
;
1930 for (tail
= BVAR (buf
, local_var_alist
); CONSP (tail
); tail
= XCDR (tail
))
1933 if (EQ (variable
, XCAR (elt
)))
1935 eassert (!blv
->frame_local
);
1941 case SYMBOL_FORWARDED
:
1943 union Lisp_Fwd
*valcontents
= SYMBOL_FWD (sym
);
1944 if (BUFFER_OBJFWDP (valcontents
))
1946 int offset
= XBUFFER_OBJFWD (valcontents
)->offset
;
1947 int idx
= PER_BUFFER_IDX (offset
);
1948 if (idx
== -1 || PER_BUFFER_VALUE_P (buf
, idx
))
1953 default: emacs_abort ();
1957 DEFUN ("local-variable-if-set-p", Flocal_variable_if_set_p
, Slocal_variable_if_set_p
,
1959 doc
: /* Non-nil if VARIABLE is local in buffer BUFFER when set there.
1960 BUFFER defaults to the current buffer.
1962 More precisely, return non-nil if either VARIABLE already has a local
1963 value in BUFFER, or if VARIABLE is automatically buffer-local (see
1964 `make-variable-buffer-local'). */)
1965 (register Lisp_Object variable
, Lisp_Object buffer
)
1967 struct Lisp_Symbol
*sym
;
1969 CHECK_SYMBOL (variable
);
1970 sym
= XSYMBOL (variable
);
1973 switch (sym
->redirect
)
1975 case SYMBOL_VARALIAS
: sym
= indirect_variable (sym
); goto start
;
1976 case SYMBOL_PLAINVAL
: return Qnil
;
1977 case SYMBOL_LOCALIZED
:
1979 struct Lisp_Buffer_Local_Value
*blv
= SYMBOL_BLV (sym
);
1980 if (blv
->local_if_set
)
1982 XSETSYMBOL (variable
, sym
); /* Update in case of aliasing. */
1983 return Flocal_variable_p (variable
, buffer
);
1985 case SYMBOL_FORWARDED
:
1986 /* All BUFFER_OBJFWD slots become local if they are set. */
1987 return (BUFFER_OBJFWDP (SYMBOL_FWD (sym
)) ? Qt
: Qnil
);
1988 default: emacs_abort ();
1992 DEFUN ("variable-binding-locus", Fvariable_binding_locus
, Svariable_binding_locus
,
1994 doc
: /* Return a value indicating where VARIABLE's current binding comes from.
1995 If the current binding is buffer-local, the value is the current buffer.
1996 If the current binding is frame-local, the value is the selected frame.
1997 If the current binding is global (the default), the value is nil. */)
1998 (register Lisp_Object variable
)
2000 struct Lisp_Symbol
*sym
;
2002 CHECK_SYMBOL (variable
);
2003 sym
= XSYMBOL (variable
);
2005 /* Make sure the current binding is actually swapped in. */
2006 find_symbol_value (variable
);
2009 switch (sym
->redirect
)
2011 case SYMBOL_VARALIAS
: sym
= indirect_variable (sym
); goto start
;
2012 case SYMBOL_PLAINVAL
: return Qnil
;
2013 case SYMBOL_FORWARDED
:
2015 union Lisp_Fwd
*valcontents
= SYMBOL_FWD (sym
);
2016 if (KBOARD_OBJFWDP (valcontents
))
2017 return Fframe_terminal (selected_frame
);
2018 else if (!BUFFER_OBJFWDP (valcontents
))
2022 case SYMBOL_LOCALIZED
:
2023 /* For a local variable, record both the symbol and which
2024 buffer's or frame's value we are saving. */
2025 if (!NILP (Flocal_variable_p (variable
, Qnil
)))
2026 return Fcurrent_buffer ();
2027 else if (sym
->redirect
== SYMBOL_LOCALIZED
2028 && blv_found (SYMBOL_BLV (sym
)))
2029 return SYMBOL_BLV (sym
)->where
;
2032 default: emacs_abort ();
2036 /* This code is disabled now that we use the selected frame to return
2037 keyboard-local-values. */
2039 extern struct terminal
*get_terminal (Lisp_Object display
, int);
2041 DEFUN ("terminal-local-value", Fterminal_local_value
,
2042 Sterminal_local_value
, 2, 2, 0,
2043 doc
: /* Return the terminal-local value of SYMBOL on TERMINAL.
2044 If SYMBOL is not a terminal-local variable, then return its normal
2045 value, like `symbol-value'.
2047 TERMINAL may be a terminal object, a frame, or nil (meaning the
2048 selected frame's terminal device). */)
2049 (Lisp_Object symbol
, Lisp_Object terminal
)
2052 struct terminal
*t
= get_terminal (terminal
, 1);
2053 push_kboard (t
->kboard
);
2054 result
= Fsymbol_value (symbol
);
2059 DEFUN ("set-terminal-local-value", Fset_terminal_local_value
,
2060 Sset_terminal_local_value
, 3, 3, 0,
2061 doc
: /* Set the terminal-local binding of SYMBOL on TERMINAL to VALUE.
2062 If VARIABLE is not a terminal-local variable, then set its normal
2063 binding, like `set'.
2065 TERMINAL may be a terminal object, a frame, or nil (meaning the
2066 selected frame's terminal device). */)
2067 (Lisp_Object symbol
, Lisp_Object terminal
, Lisp_Object value
)
2070 struct terminal
*t
= get_terminal (terminal
, 1);
2071 push_kboard (d
->kboard
);
2072 result
= Fset (symbol
, value
);
2078 /* Find the function at the end of a chain of symbol function indirections. */
2080 /* If OBJECT is a symbol, find the end of its function chain and
2081 return the value found there. If OBJECT is not a symbol, just
2082 return it. If there is a cycle in the function chain, signal a
2083 cyclic-function-indirection error.
2085 This is like Findirect_function, except that it doesn't signal an
2086 error if the chain ends up unbound. */
2088 indirect_function (register Lisp_Object object
)
2090 Lisp_Object tortoise
, hare
;
2092 hare
= tortoise
= object
;
2096 if (!SYMBOLP (hare
) || NILP (hare
))
2098 hare
= XSYMBOL (hare
)->function
;
2099 if (!SYMBOLP (hare
) || NILP (hare
))
2101 hare
= XSYMBOL (hare
)->function
;
2103 tortoise
= XSYMBOL (tortoise
)->function
;
2105 if (EQ (hare
, tortoise
))
2106 xsignal1 (Qcyclic_function_indirection
, object
);
2112 DEFUN ("indirect-function", Findirect_function
, Sindirect_function
, 1, 2, 0,
2113 doc
: /* Return the function at the end of OBJECT's function chain.
2114 If OBJECT is not a symbol, just return it. Otherwise, follow all
2115 function indirections to find the final function binding and return it.
2116 If the final symbol in the chain is unbound, signal a void-function error.
2117 Optional arg NOERROR non-nil means to return nil instead of signaling.
2118 Signal a cyclic-function-indirection error if there is a loop in the
2119 function chain of symbols. */)
2120 (register Lisp_Object object
, Lisp_Object noerror
)
2124 /* Optimize for no indirection. */
2126 if (SYMBOLP (result
) && !NILP (result
)
2127 && (result
= XSYMBOL (result
)->function
, SYMBOLP (result
)))
2128 result
= indirect_function (result
);
2133 xsignal1 (Qvoid_function
, object
);
2138 /* Extract and set vector and string elements. */
2140 DEFUN ("aref", Faref
, Saref
, 2, 2, 0,
2141 doc
: /* Return the element of ARRAY at index IDX.
2142 ARRAY may be a vector, a string, a char-table, a bool-vector,
2143 or a byte-code object. IDX starts at 0. */)
2144 (register Lisp_Object array
, Lisp_Object idx
)
2146 register EMACS_INT idxval
;
2149 idxval
= XINT (idx
);
2150 if (STRINGP (array
))
2153 ptrdiff_t idxval_byte
;
2155 if (idxval
< 0 || idxval
>= SCHARS (array
))
2156 args_out_of_range (array
, idx
);
2157 if (! STRING_MULTIBYTE (array
))
2158 return make_number ((unsigned char) SREF (array
, idxval
));
2159 idxval_byte
= string_char_to_byte (array
, idxval
);
2161 c
= STRING_CHAR (SDATA (array
) + idxval_byte
);
2162 return make_number (c
);
2164 else if (BOOL_VECTOR_P (array
))
2166 if (idxval
< 0 || idxval
>= bool_vector_size (array
))
2167 args_out_of_range (array
, idx
);
2168 return bool_vector_ref (array
, idxval
);
2170 else if (CHAR_TABLE_P (array
))
2172 CHECK_CHARACTER (idx
);
2173 return CHAR_TABLE_REF (array
, idxval
);
2178 if (VECTORP (array
))
2179 size
= ASIZE (array
);
2180 else if (COMPILEDP (array
))
2181 size
= ASIZE (array
) & PSEUDOVECTOR_SIZE_MASK
;
2183 wrong_type_argument (Qarrayp
, array
);
2185 if (idxval
< 0 || idxval
>= size
)
2186 args_out_of_range (array
, idx
);
2187 return AREF (array
, idxval
);
2191 DEFUN ("aset", Faset
, Saset
, 3, 3, 0,
2192 doc
: /* Store into the element of ARRAY at index IDX the value NEWELT.
2193 Return NEWELT. ARRAY may be a vector, a string, a char-table or a
2194 bool-vector. IDX starts at 0. */)
2195 (register Lisp_Object array
, Lisp_Object idx
, Lisp_Object newelt
)
2197 register EMACS_INT idxval
;
2200 idxval
= XINT (idx
);
2201 CHECK_ARRAY (array
, Qarrayp
);
2202 CHECK_IMPURE (array
);
2204 if (VECTORP (array
))
2206 if (idxval
< 0 || idxval
>= ASIZE (array
))
2207 args_out_of_range (array
, idx
);
2208 ASET (array
, idxval
, newelt
);
2210 else if (BOOL_VECTOR_P (array
))
2212 if (idxval
< 0 || idxval
>= bool_vector_size (array
))
2213 args_out_of_range (array
, idx
);
2214 bool_vector_set (array
, idxval
, !NILP (newelt
));
2216 else if (CHAR_TABLE_P (array
))
2218 CHECK_CHARACTER (idx
);
2219 CHAR_TABLE_SET (array
, idxval
, newelt
);
2225 if (idxval
< 0 || idxval
>= SCHARS (array
))
2226 args_out_of_range (array
, idx
);
2227 CHECK_CHARACTER (newelt
);
2228 c
= XFASTINT (newelt
);
2230 if (STRING_MULTIBYTE (array
))
2232 ptrdiff_t idxval_byte
, nbytes
;
2233 int prev_bytes
, new_bytes
;
2234 unsigned char workbuf
[MAX_MULTIBYTE_LENGTH
], *p0
= workbuf
, *p1
;
2236 nbytes
= SBYTES (array
);
2237 idxval_byte
= string_char_to_byte (array
, idxval
);
2238 p1
= SDATA (array
) + idxval_byte
;
2239 prev_bytes
= BYTES_BY_CHAR_HEAD (*p1
);
2240 new_bytes
= CHAR_STRING (c
, p0
);
2241 if (prev_bytes
!= new_bytes
)
2243 /* We must relocate the string data. */
2244 ptrdiff_t nchars
= SCHARS (array
);
2246 unsigned char *str
= SAFE_ALLOCA (nbytes
);
2248 memcpy (str
, SDATA (array
), nbytes
);
2249 allocate_string_data (XSTRING (array
), nchars
,
2250 nbytes
+ new_bytes
- prev_bytes
);
2251 memcpy (SDATA (array
), str
, idxval_byte
);
2252 p1
= SDATA (array
) + idxval_byte
;
2253 memcpy (p1
+ new_bytes
, str
+ idxval_byte
+ prev_bytes
,
2254 nbytes
- (idxval_byte
+ prev_bytes
));
2256 clear_string_char_byte_cache ();
2263 if (! SINGLE_BYTE_CHAR_P (c
))
2267 for (i
= SBYTES (array
) - 1; i
>= 0; i
--)
2268 if (SREF (array
, i
) >= 0x80)
2269 args_out_of_range (array
, newelt
);
2270 /* ARRAY is an ASCII string. Convert it to a multibyte
2271 string, and try `aset' again. */
2272 STRING_SET_MULTIBYTE (array
);
2273 return Faset (array
, idx
, newelt
);
2275 SSET (array
, idxval
, c
);
2282 /* Arithmetic functions */
2285 arithcompare (Lisp_Object num1
, Lisp_Object num2
, enum Arith_Comparison comparison
)
2287 double f1
= 0, f2
= 0;
2290 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num1
);
2291 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num2
);
2293 if (FLOATP (num1
) || FLOATP (num2
))
2296 f1
= (FLOATP (num1
)) ? XFLOAT_DATA (num1
) : XINT (num1
);
2297 f2
= (FLOATP (num2
)) ? XFLOAT_DATA (num2
) : XINT (num2
);
2303 if (floatp
? f1
== f2
: XINT (num1
) == XINT (num2
))
2307 case ARITH_NOTEQUAL
:
2308 if (floatp
? f1
!= f2
: XINT (num1
) != XINT (num2
))
2313 if (floatp
? f1
< f2
: XINT (num1
) < XINT (num2
))
2317 case ARITH_LESS_OR_EQUAL
:
2318 if (floatp
? f1
<= f2
: XINT (num1
) <= XINT (num2
))
2323 if (floatp
? f1
> f2
: XINT (num1
) > XINT (num2
))
2327 case ARITH_GRTR_OR_EQUAL
:
2328 if (floatp
? f1
>= f2
: XINT (num1
) >= XINT (num2
))
2338 arithcompare_driver (ptrdiff_t nargs
, Lisp_Object
*args
,
2339 enum Arith_Comparison comparison
)
2342 for (argnum
= 1; argnum
< nargs
; ++argnum
)
2344 if (EQ (Qnil
, arithcompare (args
[argnum
- 1], args
[argnum
], comparison
)))
2350 DEFUN ("=", Feqlsign
, Seqlsign
, 1, MANY
, 0,
2351 doc
: /* Return t if args, all numbers or markers, are equal.
2352 usage: (= NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2353 (ptrdiff_t nargs
, Lisp_Object
*args
)
2355 return arithcompare_driver (nargs
, args
, ARITH_EQUAL
);
2358 DEFUN ("<", Flss
, Slss
, 1, MANY
, 0,
2359 doc
: /* Return t if each arg (a number or marker), is less than the next arg.
2360 usage: (< NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2361 (ptrdiff_t nargs
, Lisp_Object
*args
)
2363 return arithcompare_driver (nargs
, args
, ARITH_LESS
);
2366 DEFUN (">", Fgtr
, Sgtr
, 1, MANY
, 0,
2367 doc
: /* Return t if each arg (a number or marker) is greater than the next arg.
2368 usage: (> NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2369 (ptrdiff_t nargs
, Lisp_Object
*args
)
2371 return arithcompare_driver (nargs
, args
, ARITH_GRTR
);
2374 DEFUN ("<=", Fleq
, Sleq
, 1, MANY
, 0,
2375 doc
: /* Return t if each arg (a number or marker) is less than or equal to the next.
2376 usage: (<= NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2377 (ptrdiff_t nargs
, Lisp_Object
*args
)
2379 return arithcompare_driver (nargs
, args
, ARITH_LESS_OR_EQUAL
);
2382 DEFUN (">=", Fgeq
, Sgeq
, 1, MANY
, 0,
2383 doc
: /* Return t if each arg (a number or marker) is greater than or equal to the next.
2384 usage: (>= NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2385 (ptrdiff_t nargs
, Lisp_Object
*args
)
2387 return arithcompare_driver (nargs
, args
, ARITH_GRTR_OR_EQUAL
);
2390 DEFUN ("/=", Fneq
, Sneq
, 2, 2, 0,
2391 doc
: /* Return t if first arg is not equal to second arg. Both must be numbers or markers. */)
2392 (register Lisp_Object num1
, Lisp_Object num2
)
2394 return arithcompare (num1
, num2
, ARITH_NOTEQUAL
);
2397 /* Convert the cons-of-integers, integer, or float value C to an
2398 unsigned value with maximum value MAX. Signal an error if C does not
2399 have a valid format or is out of range. */
2401 cons_to_unsigned (Lisp_Object c
, uintmax_t max
)
2404 uintmax_t val
IF_LINT (= 0);
2407 valid
= 0 <= XINT (c
);
2410 else if (FLOATP (c
))
2412 double d
= XFLOAT_DATA (c
);
2414 && d
< (max
== UINTMAX_MAX
? (double) UINTMAX_MAX
+ 1 : max
+ 1))
2420 else if (CONSP (c
) && NATNUMP (XCAR (c
)))
2422 uintmax_t top
= XFASTINT (XCAR (c
));
2423 Lisp_Object rest
= XCDR (c
);
2424 if (top
<= UINTMAX_MAX
>> 24 >> 16
2426 && NATNUMP (XCAR (rest
)) && XFASTINT (XCAR (rest
)) < 1 << 24
2427 && NATNUMP (XCDR (rest
)) && XFASTINT (XCDR (rest
)) < 1 << 16)
2429 uintmax_t mid
= XFASTINT (XCAR (rest
));
2430 val
= top
<< 24 << 16 | mid
<< 16 | XFASTINT (XCDR (rest
));
2433 else if (top
<= UINTMAX_MAX
>> 16)
2437 if (NATNUMP (rest
) && XFASTINT (rest
) < 1 << 16)
2439 val
= top
<< 16 | XFASTINT (rest
);
2445 if (! (valid
&& val
<= max
))
2446 error ("Not an in-range integer, float, or cons of integers");
2450 /* Convert the cons-of-integers, integer, or float value C to a signed
2451 value with extrema MIN and MAX. Signal an error if C does not have
2452 a valid format or is out of range. */
2454 cons_to_signed (Lisp_Object c
, intmax_t min
, intmax_t max
)
2457 intmax_t val
IF_LINT (= 0);
2463 else if (FLOATP (c
))
2465 double d
= XFLOAT_DATA (c
);
2467 && d
< (max
== INTMAX_MAX
? (double) INTMAX_MAX
+ 1 : max
+ 1))
2473 else if (CONSP (c
) && INTEGERP (XCAR (c
)))
2475 intmax_t top
= XINT (XCAR (c
));
2476 Lisp_Object rest
= XCDR (c
);
2477 if (INTMAX_MIN
>> 24 >> 16 <= top
&& top
<= INTMAX_MAX
>> 24 >> 16
2479 && NATNUMP (XCAR (rest
)) && XFASTINT (XCAR (rest
)) < 1 << 24
2480 && NATNUMP (XCDR (rest
)) && XFASTINT (XCDR (rest
)) < 1 << 16)
2482 intmax_t mid
= XFASTINT (XCAR (rest
));
2483 val
= top
<< 24 << 16 | mid
<< 16 | XFASTINT (XCDR (rest
));
2486 else if (INTMAX_MIN
>> 16 <= top
&& top
<= INTMAX_MAX
>> 16)
2490 if (NATNUMP (rest
) && XFASTINT (rest
) < 1 << 16)
2492 val
= top
<< 16 | XFASTINT (rest
);
2498 if (! (valid
&& min
<= val
&& val
<= max
))
2499 error ("Not an in-range integer, float, or cons of integers");
2503 DEFUN ("number-to-string", Fnumber_to_string
, Snumber_to_string
, 1, 1, 0,
2504 doc
: /* Return the decimal representation of NUMBER as a string.
2505 Uses a minus sign if negative.
2506 NUMBER may be an integer or a floating point number. */)
2507 (Lisp_Object number
)
2509 char buffer
[max (FLOAT_TO_STRING_BUFSIZE
, INT_BUFSIZE_BOUND (EMACS_INT
))];
2512 CHECK_NUMBER_OR_FLOAT (number
);
2514 if (FLOATP (number
))
2515 len
= float_to_string (buffer
, XFLOAT_DATA (number
));
2517 len
= sprintf (buffer
, "%"pI
"d", XINT (number
));
2519 return make_unibyte_string (buffer
, len
);
2522 DEFUN ("string-to-number", Fstring_to_number
, Sstring_to_number
, 1, 2, 0,
2523 doc
: /* Parse STRING as a decimal number and return the number.
2524 Ignore leading spaces and tabs, and all trailing chars. Return 0 if
2525 STRING cannot be parsed as an integer or floating point number.
2527 If BASE, interpret STRING as a number in that base. If BASE isn't
2528 present, base 10 is used. BASE must be between 2 and 16 (inclusive).
2529 If the base used is not 10, STRING is always parsed as an integer. */)
2530 (register Lisp_Object string
, Lisp_Object base
)
2536 CHECK_STRING (string
);
2542 CHECK_NUMBER (base
);
2543 if (! (2 <= XINT (base
) && XINT (base
) <= 16))
2544 xsignal1 (Qargs_out_of_range
, base
);
2548 p
= SSDATA (string
);
2549 while (*p
== ' ' || *p
== '\t')
2552 val
= string_to_number (p
, b
, 1);
2553 return NILP (val
) ? make_number (0) : val
;
2569 static Lisp_Object
float_arith_driver (double, ptrdiff_t, enum arithop
,
2570 ptrdiff_t, Lisp_Object
*);
2572 arith_driver (enum arithop code
, ptrdiff_t nargs
, Lisp_Object
*args
)
2575 ptrdiff_t argnum
, ok_args
;
2576 EMACS_INT accum
= 0;
2577 EMACS_INT next
, ok_accum
;
2598 for (argnum
= 0; argnum
< nargs
; argnum
++)
2606 /* Using args[argnum] as argument to CHECK_NUMBER_... */
2608 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val
);
2611 return float_arith_driver (ok_accum
, ok_args
, code
,
2614 next
= XINT (args
[argnum
]);
2618 if (INT_ADD_OVERFLOW (accum
, next
))
2626 if (INT_SUBTRACT_OVERFLOW (accum
, next
))
2631 accum
= argnum
? accum
- next
: nargs
== 1 ? - next
: next
;
2634 if (INT_MULTIPLY_OVERFLOW (accum
, next
))
2636 EMACS_UINT a
= accum
, b
= next
, ab
= a
* b
;
2638 accum
= ab
& INTMASK
;
2649 xsignal0 (Qarith_error
);
2663 if (!argnum
|| next
> accum
)
2667 if (!argnum
|| next
< accum
)
2673 XSETINT (val
, accum
);
2678 #define isnan(x) ((x) != (x))
2681 float_arith_driver (double accum
, ptrdiff_t argnum
, enum arithop code
,
2682 ptrdiff_t nargs
, Lisp_Object
*args
)
2684 register Lisp_Object val
;
2687 for (; argnum
< nargs
; argnum
++)
2689 val
= args
[argnum
]; /* using args[argnum] as argument to CHECK_NUMBER_... */
2690 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val
);
2694 next
= XFLOAT_DATA (val
);
2698 args
[argnum
] = val
; /* runs into a compiler bug. */
2699 next
= XINT (args
[argnum
]);
2707 accum
= argnum
? accum
- next
: nargs
== 1 ? - next
: next
;
2717 if (! IEEE_FLOATING_POINT
&& next
== 0)
2718 xsignal0 (Qarith_error
);
2725 return wrong_type_argument (Qinteger_or_marker_p
, val
);
2727 if (!argnum
|| isnan (next
) || next
> accum
)
2731 if (!argnum
|| isnan (next
) || next
< accum
)
2737 return make_float (accum
);
2741 DEFUN ("+", Fplus
, Splus
, 0, MANY
, 0,
2742 doc
: /* Return sum of any number of arguments, which are numbers or markers.
2743 usage: (+ &rest NUMBERS-OR-MARKERS) */)
2744 (ptrdiff_t nargs
, Lisp_Object
*args
)
2746 return arith_driver (Aadd
, nargs
, args
);
2749 DEFUN ("-", Fminus
, Sminus
, 0, MANY
, 0,
2750 doc
: /* Negate number or subtract numbers or markers and return the result.
2751 With one arg, negates it. With more than one arg,
2752 subtracts all but the first from the first.
2753 usage: (- &optional NUMBER-OR-MARKER &rest MORE-NUMBERS-OR-MARKERS) */)
2754 (ptrdiff_t nargs
, Lisp_Object
*args
)
2756 return arith_driver (Asub
, nargs
, args
);
2759 DEFUN ("*", Ftimes
, Stimes
, 0, MANY
, 0,
2760 doc
: /* Return product of any number of arguments, which are numbers or markers.
2761 usage: (* &rest NUMBERS-OR-MARKERS) */)
2762 (ptrdiff_t nargs
, Lisp_Object
*args
)
2764 return arith_driver (Amult
, nargs
, args
);
2767 DEFUN ("/", Fquo
, Squo
, 1, MANY
, 0,
2768 doc
: /* Return first argument divided by all the remaining arguments.
2769 The arguments must be numbers or markers.
2770 usage: (/ DIVIDEND &rest DIVISORS) */)
2771 (ptrdiff_t nargs
, Lisp_Object
*args
)
2774 for (argnum
= 2; argnum
< nargs
; argnum
++)
2775 if (FLOATP (args
[argnum
]))
2776 return float_arith_driver (0, 0, Adiv
, nargs
, args
);
2777 return arith_driver (Adiv
, nargs
, args
);
2780 DEFUN ("%", Frem
, Srem
, 2, 2, 0,
2781 doc
: /* Return remainder of X divided by Y.
2782 Both must be integers or markers. */)
2783 (register Lisp_Object x
, Lisp_Object y
)
2787 CHECK_NUMBER_COERCE_MARKER (x
);
2788 CHECK_NUMBER_COERCE_MARKER (y
);
2791 xsignal0 (Qarith_error
);
2793 XSETINT (val
, XINT (x
) % XINT (y
));
2797 DEFUN ("mod", Fmod
, Smod
, 2, 2, 0,
2798 doc
: /* Return X modulo Y.
2799 The result falls between zero (inclusive) and Y (exclusive).
2800 Both X and Y must be numbers or markers. */)
2801 (register Lisp_Object x
, Lisp_Object y
)
2806 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (x
);
2807 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (y
);
2809 if (FLOATP (x
) || FLOATP (y
))
2810 return fmod_float (x
, y
);
2816 xsignal0 (Qarith_error
);
2820 /* If the "remainder" comes out with the wrong sign, fix it. */
2821 if (i2
< 0 ? i1
> 0 : i1
< 0)
2828 DEFUN ("max", Fmax
, Smax
, 1, MANY
, 0,
2829 doc
: /* Return largest of all the arguments (which must be numbers or markers).
2830 The value is always a number; markers are converted to numbers.
2831 usage: (max NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2832 (ptrdiff_t nargs
, Lisp_Object
*args
)
2834 return arith_driver (Amax
, nargs
, args
);
2837 DEFUN ("min", Fmin
, Smin
, 1, MANY
, 0,
2838 doc
: /* Return smallest of all the arguments (which must be numbers or markers).
2839 The value is always a number; markers are converted to numbers.
2840 usage: (min NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2841 (ptrdiff_t nargs
, Lisp_Object
*args
)
2843 return arith_driver (Amin
, nargs
, args
);
2846 DEFUN ("logand", Flogand
, Slogand
, 0, MANY
, 0,
2847 doc
: /* Return bitwise-and of all the arguments.
2848 Arguments may be integers, or markers converted to integers.
2849 usage: (logand &rest INTS-OR-MARKERS) */)
2850 (ptrdiff_t nargs
, Lisp_Object
*args
)
2852 return arith_driver (Alogand
, nargs
, args
);
2855 DEFUN ("logior", Flogior
, Slogior
, 0, MANY
, 0,
2856 doc
: /* Return bitwise-or of all the arguments.
2857 Arguments may be integers, or markers converted to integers.
2858 usage: (logior &rest INTS-OR-MARKERS) */)
2859 (ptrdiff_t nargs
, Lisp_Object
*args
)
2861 return arith_driver (Alogior
, nargs
, args
);
2864 DEFUN ("logxor", Flogxor
, Slogxor
, 0, MANY
, 0,
2865 doc
: /* Return bitwise-exclusive-or of all the arguments.
2866 Arguments may be integers, or markers converted to integers.
2867 usage: (logxor &rest INTS-OR-MARKERS) */)
2868 (ptrdiff_t nargs
, Lisp_Object
*args
)
2870 return arith_driver (Alogxor
, nargs
, args
);
2873 DEFUN ("ash", Fash
, Sash
, 2, 2, 0,
2874 doc
: /* Return VALUE with its bits shifted left by COUNT.
2875 If COUNT is negative, shifting is actually to the right.
2876 In this case, the sign bit is duplicated. */)
2877 (register Lisp_Object value
, Lisp_Object count
)
2879 register Lisp_Object val
;
2881 CHECK_NUMBER (value
);
2882 CHECK_NUMBER (count
);
2884 if (XINT (count
) >= BITS_PER_EMACS_INT
)
2886 else if (XINT (count
) > 0)
2887 XSETINT (val
, XUINT (value
) << XFASTINT (count
));
2888 else if (XINT (count
) <= -BITS_PER_EMACS_INT
)
2889 XSETINT (val
, XINT (value
) < 0 ? -1 : 0);
2891 XSETINT (val
, XINT (value
) >> -XINT (count
));
2895 DEFUN ("lsh", Flsh
, Slsh
, 2, 2, 0,
2896 doc
: /* Return VALUE with its bits shifted left by COUNT.
2897 If COUNT is negative, shifting is actually to the right.
2898 In this case, zeros are shifted in on the left. */)
2899 (register Lisp_Object value
, Lisp_Object count
)
2901 register Lisp_Object val
;
2903 CHECK_NUMBER (value
);
2904 CHECK_NUMBER (count
);
2906 if (XINT (count
) >= BITS_PER_EMACS_INT
)
2908 else if (XINT (count
) > 0)
2909 XSETINT (val
, XUINT (value
) << XFASTINT (count
));
2910 else if (XINT (count
) <= -BITS_PER_EMACS_INT
)
2913 XSETINT (val
, XUINT (value
) >> -XINT (count
));
2917 DEFUN ("1+", Fadd1
, Sadd1
, 1, 1, 0,
2918 doc
: /* Return NUMBER plus one. NUMBER may be a number or a marker.
2919 Markers are converted to integers. */)
2920 (register Lisp_Object number
)
2922 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number
);
2924 if (FLOATP (number
))
2925 return (make_float (1.0 + XFLOAT_DATA (number
)));
2927 XSETINT (number
, XINT (number
) + 1);
2931 DEFUN ("1-", Fsub1
, Ssub1
, 1, 1, 0,
2932 doc
: /* Return NUMBER minus one. NUMBER may be a number or a marker.
2933 Markers are converted to integers. */)
2934 (register Lisp_Object number
)
2936 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number
);
2938 if (FLOATP (number
))
2939 return (make_float (-1.0 + XFLOAT_DATA (number
)));
2941 XSETINT (number
, XINT (number
) - 1);
2945 DEFUN ("lognot", Flognot
, Slognot
, 1, 1, 0,
2946 doc
: /* Return the bitwise complement of NUMBER. NUMBER must be an integer. */)
2947 (register Lisp_Object number
)
2949 CHECK_NUMBER (number
);
2950 XSETINT (number
, ~XINT (number
));
2954 DEFUN ("byteorder", Fbyteorder
, Sbyteorder
, 0, 0, 0,
2955 doc
: /* Return the byteorder for the machine.
2956 Returns 66 (ASCII uppercase B) for big endian machines or 108 (ASCII
2957 lowercase l) for small endian machines. */)
2960 unsigned i
= 0x04030201;
2961 int order
= *(char *)&i
== 1 ? 108 : 66;
2963 return make_number (order
);
2966 /* Because we round up the bool vector allocate size to word_size
2967 units, we can safely read past the "end" of the vector in the
2968 operations below. These extra bits are always zero. */
2971 bool_vector_spare_mask (EMACS_INT nr_bits
)
2973 return (((bits_word
) 1) << (nr_bits
% BITS_PER_BITS_WORD
)) - 1;
2976 /* Info about unsigned long long, falling back on unsigned long
2977 if unsigned long long is not available. */
2979 #if HAVE_UNSIGNED_LONG_LONG_INT && defined ULLONG_MAX
2980 enum { BITS_PER_ULL
= CHAR_BIT
* sizeof (unsigned long long) };
2981 # define ULL_MAX ULLONG_MAX
2983 enum { BITS_PER_ULL
= CHAR_BIT
* sizeof (unsigned long) };
2984 # define ULL_MAX ULONG_MAX
2985 # define count_one_bits_ll count_one_bits_l
2986 # define count_trailing_zeros_ll count_trailing_zeros_l
2989 /* Shift VAL right by the width of an unsigned long long.
2990 BITS_PER_ULL must be less than BITS_PER_BITS_WORD. */
2993 shift_right_ull (bits_word w
)
2995 /* Pacify bogus GCC warning about shift count exceeding type width. */
2996 int shift
= BITS_PER_ULL
- BITS_PER_BITS_WORD
< 0 ? BITS_PER_ULL
: 0;
3000 /* Return the number of 1 bits in W. */
3003 count_one_bits_word (bits_word w
)
3005 if (BITS_WORD_MAX
<= UINT_MAX
)
3006 return count_one_bits (w
);
3007 else if (BITS_WORD_MAX
<= ULONG_MAX
)
3008 return count_one_bits_l (w
);
3011 int i
= 0, count
= 0;
3012 while (count
+= count_one_bits_ll (w
),
3013 (i
+= BITS_PER_ULL
) < BITS_PER_BITS_WORD
)
3014 w
= shift_right_ull (w
);
3019 enum bool_vector_op
{ bool_vector_exclusive_or
,
3021 bool_vector_intersection
,
3022 bool_vector_set_difference
,
3023 bool_vector_subsetp
};
3026 bool_vector_binop_driver (Lisp_Object a
,
3029 enum bool_vector_op op
)
3032 bits_word
*adata
, *bdata
, *destdata
;
3036 CHECK_BOOL_VECTOR (a
);
3037 CHECK_BOOL_VECTOR (b
);
3039 nr_bits
= bool_vector_size (a
);
3040 if (bool_vector_size (b
) != nr_bits
)
3041 wrong_length_argument (a
, b
, dest
);
3043 nr_words
= bool_vector_words (nr_bits
);
3044 adata
= bool_vector_data (a
);
3045 bdata
= bool_vector_data (b
);
3049 dest
= make_uninit_bool_vector (nr_bits
);
3050 destdata
= bool_vector_data (dest
);
3054 CHECK_BOOL_VECTOR (dest
);
3055 destdata
= bool_vector_data (dest
);
3056 if (bool_vector_size (dest
) != nr_bits
)
3057 wrong_length_argument (a
, b
, dest
);
3061 case bool_vector_exclusive_or
:
3062 for (; i
< nr_words
; i
++)
3063 if (destdata
[i
] != (adata
[i
] ^ bdata
[i
]))
3067 case bool_vector_subsetp
:
3068 for (; i
< nr_words
; i
++)
3069 if (adata
[i
] &~ bdata
[i
])
3073 case bool_vector_union
:
3074 for (; i
< nr_words
; i
++)
3075 if (destdata
[i
] != (adata
[i
] | bdata
[i
]))
3079 case bool_vector_intersection
:
3080 for (; i
< nr_words
; i
++)
3081 if (destdata
[i
] != (adata
[i
] & bdata
[i
]))
3085 case bool_vector_set_difference
:
3086 for (; i
< nr_words
; i
++)
3087 if (destdata
[i
] != (adata
[i
] &~ bdata
[i
]))
3098 case bool_vector_exclusive_or
:
3099 for (; i
< nr_words
; i
++)
3100 destdata
[i
] = adata
[i
] ^ bdata
[i
];
3103 case bool_vector_union
:
3104 for (; i
< nr_words
; i
++)
3105 destdata
[i
] = adata
[i
] | bdata
[i
];
3108 case bool_vector_intersection
:
3109 for (; i
< nr_words
; i
++)
3110 destdata
[i
] = adata
[i
] & bdata
[i
];
3113 case bool_vector_set_difference
:
3114 for (; i
< nr_words
; i
++)
3115 destdata
[i
] = adata
[i
] &~ bdata
[i
];
3125 /* PRECONDITION must be true. Return VALUE. This odd construction
3126 works around a bogus GCC diagnostic "shift count >= width of type". */
3129 pre_value (bool precondition
, int value
)
3131 eassume (precondition
);
3132 return precondition
? value
: 0;
3135 /* Compute the number of trailing zero bits in val. If val is zero,
3136 return the number of bits in val. */
3138 count_trailing_zero_bits (bits_word val
)
3140 if (BITS_WORD_MAX
== UINT_MAX
)
3141 return count_trailing_zeros (val
);
3142 if (BITS_WORD_MAX
== ULONG_MAX
)
3143 return count_trailing_zeros_l (val
);
3144 if (BITS_WORD_MAX
== ULL_MAX
)
3145 return count_trailing_zeros_ll (val
);
3147 /* The rest of this code is for the unlikely platform where bits_word differs
3148 in width from unsigned int, unsigned long, and unsigned long long. */
3149 val
|= ~ BITS_WORD_MAX
;
3150 if (BITS_WORD_MAX
<= UINT_MAX
)
3151 return count_trailing_zeros (val
);
3152 if (BITS_WORD_MAX
<= ULONG_MAX
)
3153 return count_trailing_zeros_l (val
);
3158 count
< BITS_PER_BITS_WORD
- BITS_PER_ULL
;
3159 count
+= BITS_PER_ULL
)
3162 return count
+ count_trailing_zeros_ll (val
);
3163 val
= shift_right_ull (val
);
3166 if (BITS_PER_BITS_WORD
% BITS_PER_ULL
!= 0
3167 && BITS_WORD_MAX
== (bits_word
) -1)
3168 val
|= (bits_word
) 1 << pre_value (ULONG_MAX
< BITS_WORD_MAX
,
3169 BITS_PER_BITS_WORD
% BITS_PER_ULL
);
3170 return count
+ count_trailing_zeros_ll (val
);
3175 bits_word_to_host_endian (bits_word val
)
3177 #ifndef WORDS_BIGENDIAN
3180 if (BITS_WORD_MAX
>> 31 == 1)
3181 return bswap_32 (val
);
3182 # if HAVE_UNSIGNED_LONG_LONG
3183 if (BITS_WORD_MAX
>> 31 >> 31 >> 1 == 1)
3184 return bswap_64 (val
);
3189 for (i
= 0; i
< sizeof val
; i
++)
3191 r
= ((r
<< 1 << (CHAR_BIT
- 1))
3192 | (val
& ((1u << 1 << (CHAR_BIT
- 1)) - 1)));
3193 val
= val
>> 1 >> (CHAR_BIT
- 1);
3200 DEFUN ("bool-vector-exclusive-or", Fbool_vector_exclusive_or
,
3201 Sbool_vector_exclusive_or
, 2, 3, 0,
3202 doc
: /* Return A ^ B, bitwise exclusive or.
3203 If optional third argument C is given, store result into C.
3204 A, B, and C must be bool vectors of the same length.
3205 Return the destination vector if it changed or nil otherwise. */)
3206 (Lisp_Object a
, Lisp_Object b
, Lisp_Object c
)
3208 return bool_vector_binop_driver (a
, b
, c
, bool_vector_exclusive_or
);
3211 DEFUN ("bool-vector-union", Fbool_vector_union
,
3212 Sbool_vector_union
, 2, 3, 0,
3213 doc
: /* Return A | B, bitwise or.
3214 If optional third argument C is given, store result into C.
3215 A, B, and C must be bool vectors of the same length.
3216 Return the destination vector if it changed or nil otherwise. */)
3217 (Lisp_Object a
, Lisp_Object b
, Lisp_Object c
)
3219 return bool_vector_binop_driver (a
, b
, c
, bool_vector_union
);
3222 DEFUN ("bool-vector-intersection", Fbool_vector_intersection
,
3223 Sbool_vector_intersection
, 2, 3, 0,
3224 doc
: /* Return A & B, bitwise and.
3225 If optional third argument C is given, store result into C.
3226 A, B, and C must be bool vectors of the same length.
3227 Return the destination vector if it changed or nil otherwise. */)
3228 (Lisp_Object a
, Lisp_Object b
, Lisp_Object c
)
3230 return bool_vector_binop_driver (a
, b
, c
, bool_vector_intersection
);
3233 DEFUN ("bool-vector-set-difference", Fbool_vector_set_difference
,
3234 Sbool_vector_set_difference
, 2, 3, 0,
3235 doc
: /* Return A &~ B, set difference.
3236 If optional third argument C is given, store result into C.
3237 A, B, and C must be bool vectors of the same length.
3238 Return the destination vector if it changed or nil otherwise. */)
3239 (Lisp_Object a
, Lisp_Object b
, Lisp_Object c
)
3241 return bool_vector_binop_driver (a
, b
, c
, bool_vector_set_difference
);
3244 DEFUN ("bool-vector-subsetp", Fbool_vector_subsetp
,
3245 Sbool_vector_subsetp
, 2, 2, 0,
3246 doc
: /* Return t if every t value in A is also t in B, nil otherwise.
3247 A and B must be bool vectors of the same length. */)
3248 (Lisp_Object a
, Lisp_Object b
)
3250 return bool_vector_binop_driver (a
, b
, b
, bool_vector_subsetp
);
3253 DEFUN ("bool-vector-not", Fbool_vector_not
,
3254 Sbool_vector_not
, 1, 2, 0,
3255 doc
: /* Compute ~A, set complement.
3256 If optional second argument B is given, store result into B.
3257 A and B must be bool vectors of the same length.
3258 Return the destination vector. */)
3259 (Lisp_Object a
, Lisp_Object b
)
3262 bits_word
*bdata
, *adata
;
3265 CHECK_BOOL_VECTOR (a
);
3266 nr_bits
= bool_vector_size (a
);
3269 b
= make_uninit_bool_vector (nr_bits
);
3272 CHECK_BOOL_VECTOR (b
);
3273 if (bool_vector_size (b
) != nr_bits
)
3274 wrong_length_argument (a
, b
, Qnil
);
3277 bdata
= bool_vector_data (b
);
3278 adata
= bool_vector_data (a
);
3280 for (i
= 0; i
< nr_bits
/ BITS_PER_BITS_WORD
; i
++)
3281 bdata
[i
] = BITS_WORD_MAX
& ~adata
[i
];
3283 if (nr_bits
% BITS_PER_BITS_WORD
)
3285 bits_word mword
= bits_word_to_host_endian (adata
[i
]);
3287 mword
&= bool_vector_spare_mask (nr_bits
);
3288 bdata
[i
] = bits_word_to_host_endian (mword
);
3294 DEFUN ("bool-vector-count-population", Fbool_vector_count_population
,
3295 Sbool_vector_count_population
, 1, 1, 0,
3296 doc
: /* Count how many elements in A are t.
3297 A is a bool vector. To count A's nil elements, subtract the return
3298 value from A's length. */)
3304 ptrdiff_t i
, nwords
;
3306 CHECK_BOOL_VECTOR (a
);
3308 nr_bits
= bool_vector_size (a
);
3309 nwords
= bool_vector_words (nr_bits
);
3311 adata
= bool_vector_data (a
);
3313 for (i
= 0; i
< nwords
; i
++)
3314 count
+= count_one_bits_word (adata
[i
]);
3316 return make_number (count
);
3319 DEFUN ("bool-vector-count-consecutive", Fbool_vector_count_consecutive
,
3320 Sbool_vector_count_consecutive
, 3, 3, 0,
3321 doc
: /* Count how many consecutive elements in A equal B starting at I.
3322 A is a bool vector, B is t or nil, and I is an index into A. */)
3323 (Lisp_Object a
, Lisp_Object b
, Lisp_Object i
)
3330 bits_word mword
; /* Machine word. */
3331 ptrdiff_t pos
, pos0
;
3334 CHECK_BOOL_VECTOR (a
);
3337 nr_bits
= bool_vector_size (a
);
3338 if (XFASTINT (i
) > nr_bits
) /* Allow one past the end for convenience */
3339 args_out_of_range (a
, i
);
3341 adata
= bool_vector_data (a
);
3342 nr_words
= bool_vector_words (nr_bits
);
3343 pos
= XFASTINT (i
) / BITS_PER_BITS_WORD
;
3344 offset
= XFASTINT (i
) % BITS_PER_BITS_WORD
;
3347 /* By XORing with twiddle, we transform the problem of "count
3348 consecutive equal values" into "count the zero bits". The latter
3349 operation usually has hardware support. */
3350 twiddle
= NILP (b
) ? 0 : BITS_WORD_MAX
;
3352 /* Scan the remainder of the mword at the current offset. */
3353 if (pos
< nr_words
&& offset
!= 0)
3355 mword
= bits_word_to_host_endian (adata
[pos
]);
3359 /* Do not count the pad bits. */
3360 mword
|= (bits_word
) 1 << (BITS_PER_BITS_WORD
- offset
);
3362 count
= count_trailing_zero_bits (mword
);
3364 if (count
+ offset
< BITS_PER_BITS_WORD
)
3365 return make_number (count
);
3368 /* Scan whole words until we either reach the end of the vector or
3369 find an mword that doesn't completely match. twiddle is
3370 endian-independent. */
3372 while (pos
< nr_words
&& adata
[pos
] == twiddle
)
3374 count
+= (pos
- pos0
) * BITS_PER_BITS_WORD
;
3378 /* If we stopped because of a mismatch, see how many bits match
3379 in the current mword. */
3380 mword
= bits_word_to_host_endian (adata
[pos
]);
3382 count
+= count_trailing_zero_bits (mword
);
3384 else if (nr_bits
% BITS_PER_BITS_WORD
!= 0)
3386 /* If we hit the end, we might have overshot our count. Reduce
3387 the total by the number of spare bits at the end of the
3389 count
-= BITS_PER_BITS_WORD
- nr_bits
% BITS_PER_BITS_WORD
;
3392 return make_number (count
);
3399 Lisp_Object error_tail
, arith_tail
;
3401 DEFSYM (Qquote
, "quote");
3402 DEFSYM (Qlambda
, "lambda");
3403 DEFSYM (Qsubr
, "subr");
3404 DEFSYM (Qerror_conditions
, "error-conditions");
3405 DEFSYM (Qerror_message
, "error-message");
3406 DEFSYM (Qtop_level
, "top-level");
3408 DEFSYM (Qerror
, "error");
3409 DEFSYM (Quser_error
, "user-error");
3410 DEFSYM (Qquit
, "quit");
3411 DEFSYM (Qwrong_length_argument
, "wrong-length-argument");
3412 DEFSYM (Qwrong_type_argument
, "wrong-type-argument");
3413 DEFSYM (Qargs_out_of_range
, "args-out-of-range");
3414 DEFSYM (Qvoid_function
, "void-function");
3415 DEFSYM (Qcyclic_function_indirection
, "cyclic-function-indirection");
3416 DEFSYM (Qcyclic_variable_indirection
, "cyclic-variable-indirection");
3417 DEFSYM (Qvoid_variable
, "void-variable");
3418 DEFSYM (Qsetting_constant
, "setting-constant");
3419 DEFSYM (Qinvalid_read_syntax
, "invalid-read-syntax");
3421 DEFSYM (Qinvalid_function
, "invalid-function");
3422 DEFSYM (Qwrong_number_of_arguments
, "wrong-number-of-arguments");
3423 DEFSYM (Qno_catch
, "no-catch");
3424 DEFSYM (Qend_of_file
, "end-of-file");
3425 DEFSYM (Qarith_error
, "arith-error");
3426 DEFSYM (Qbeginning_of_buffer
, "beginning-of-buffer");
3427 DEFSYM (Qend_of_buffer
, "end-of-buffer");
3428 DEFSYM (Qbuffer_read_only
, "buffer-read-only");
3429 DEFSYM (Qtext_read_only
, "text-read-only");
3430 DEFSYM (Qmark_inactive
, "mark-inactive");
3432 DEFSYM (Qlistp
, "listp");
3433 DEFSYM (Qconsp
, "consp");
3434 DEFSYM (Qsymbolp
, "symbolp");
3435 DEFSYM (Qkeywordp
, "keywordp");
3436 DEFSYM (Qintegerp
, "integerp");
3437 DEFSYM (Qnatnump
, "natnump");
3438 DEFSYM (Qwholenump
, "wholenump");
3439 DEFSYM (Qstringp
, "stringp");
3440 DEFSYM (Qarrayp
, "arrayp");
3441 DEFSYM (Qsequencep
, "sequencep");
3442 DEFSYM (Qbufferp
, "bufferp");
3443 DEFSYM (Qvectorp
, "vectorp");
3444 DEFSYM (Qbool_vector_p
, "bool-vector-p");
3445 DEFSYM (Qchar_or_string_p
, "char-or-string-p");
3446 DEFSYM (Qmarkerp
, "markerp");
3447 DEFSYM (Qbuffer_or_string_p
, "buffer-or-string-p");
3448 DEFSYM (Qinteger_or_marker_p
, "integer-or-marker-p");
3449 DEFSYM (Qboundp
, "boundp");
3450 DEFSYM (Qfboundp
, "fboundp");
3452 DEFSYM (Qfloatp
, "floatp");
3453 DEFSYM (Qnumberp
, "numberp");
3454 DEFSYM (Qnumber_or_marker_p
, "number-or-marker-p");
3456 DEFSYM (Qchar_table_p
, "char-table-p");
3457 DEFSYM (Qvector_or_char_table_p
, "vector-or-char-table-p");
3459 DEFSYM (Qsubrp
, "subrp");
3460 DEFSYM (Qunevalled
, "unevalled");
3461 DEFSYM (Qmany
, "many");
3463 DEFSYM (Qcdr
, "cdr");
3465 /* Handle automatic advice activation. */
3466 DEFSYM (Qad_advice_info
, "ad-advice-info");
3467 DEFSYM (Qad_activate_internal
, "ad-activate-internal");
3469 error_tail
= pure_cons (Qerror
, Qnil
);
3471 /* ERROR is used as a signaler for random errors for which nothing else is
3474 Fput (Qerror
, Qerror_conditions
,
3476 Fput (Qerror
, Qerror_message
,
3477 build_pure_c_string ("error"));
3479 #define PUT_ERROR(sym, tail, msg) \
3480 Fput (sym, Qerror_conditions, pure_cons (sym, tail)); \
3481 Fput (sym, Qerror_message, build_pure_c_string (msg))
3483 PUT_ERROR (Qquit
, Qnil
, "Quit");
3485 PUT_ERROR (Quser_error
, error_tail
, "");
3486 PUT_ERROR (Qwrong_length_argument
, error_tail
, "Wrong length argument");
3487 PUT_ERROR (Qwrong_type_argument
, error_tail
, "Wrong type argument");
3488 PUT_ERROR (Qargs_out_of_range
, error_tail
, "Args out of range");
3489 PUT_ERROR (Qvoid_function
, error_tail
,
3490 "Symbol's function definition is void");
3491 PUT_ERROR (Qcyclic_function_indirection
, error_tail
,
3492 "Symbol's chain of function indirections contains a loop");
3493 PUT_ERROR (Qcyclic_variable_indirection
, error_tail
,
3494 "Symbol's chain of variable indirections contains a loop");
3495 DEFSYM (Qcircular_list
, "circular-list");
3496 PUT_ERROR (Qcircular_list
, error_tail
, "List contains a loop");
3497 PUT_ERROR (Qvoid_variable
, error_tail
, "Symbol's value as variable is void");
3498 PUT_ERROR (Qsetting_constant
, error_tail
,
3499 "Attempt to set a constant symbol");
3500 PUT_ERROR (Qinvalid_read_syntax
, error_tail
, "Invalid read syntax");
3501 PUT_ERROR (Qinvalid_function
, error_tail
, "Invalid function");
3502 PUT_ERROR (Qwrong_number_of_arguments
, error_tail
,
3503 "Wrong number of arguments");
3504 PUT_ERROR (Qno_catch
, error_tail
, "No catch for tag");
3505 PUT_ERROR (Qend_of_file
, error_tail
, "End of file during parsing");
3507 arith_tail
= pure_cons (Qarith_error
, error_tail
);
3508 Fput (Qarith_error
, Qerror_conditions
, arith_tail
);
3509 Fput (Qarith_error
, Qerror_message
, build_pure_c_string ("Arithmetic error"));
3511 PUT_ERROR (Qbeginning_of_buffer
, error_tail
, "Beginning of buffer");
3512 PUT_ERROR (Qend_of_buffer
, error_tail
, "End of buffer");
3513 PUT_ERROR (Qbuffer_read_only
, error_tail
, "Buffer is read-only");
3514 PUT_ERROR (Qtext_read_only
, pure_cons (Qbuffer_read_only
, error_tail
),
3515 "Text is read-only");
3517 DEFSYM (Qrange_error
, "range-error");
3518 DEFSYM (Qdomain_error
, "domain-error");
3519 DEFSYM (Qsingularity_error
, "singularity-error");
3520 DEFSYM (Qoverflow_error
, "overflow-error");
3521 DEFSYM (Qunderflow_error
, "underflow-error");
3523 PUT_ERROR (Qdomain_error
, arith_tail
, "Arithmetic domain error");
3525 PUT_ERROR (Qrange_error
, arith_tail
, "Arithmetic range error");
3527 PUT_ERROR (Qsingularity_error
, Fcons (Qdomain_error
, arith_tail
),
3528 "Arithmetic singularity error");
3530 PUT_ERROR (Qoverflow_error
, Fcons (Qdomain_error
, arith_tail
),
3531 "Arithmetic overflow error");
3532 PUT_ERROR (Qunderflow_error
, Fcons (Qdomain_error
, arith_tail
),
3533 "Arithmetic underflow error");
3535 /* Types that type-of returns. */
3536 DEFSYM (Qinteger
, "integer");
3537 DEFSYM (Qsymbol
, "symbol");
3538 DEFSYM (Qstring
, "string");
3539 DEFSYM (Qcons
, "cons");
3540 DEFSYM (Qmarker
, "marker");
3541 DEFSYM (Qoverlay
, "overlay");
3542 DEFSYM (Qfloat
, "float");
3543 DEFSYM (Qwindow_configuration
, "window-configuration");
3544 DEFSYM (Qprocess
, "process");
3545 DEFSYM (Qwindow
, "window");
3546 DEFSYM (Qcompiled_function
, "compiled-function");
3547 DEFSYM (Qbuffer
, "buffer");
3548 DEFSYM (Qframe
, "frame");
3549 DEFSYM (Qvector
, "vector");
3550 DEFSYM (Qchar_table
, "char-table");
3551 DEFSYM (Qbool_vector
, "bool-vector");
3552 DEFSYM (Qhash_table
, "hash-table");
3553 DEFSYM (Qmisc
, "misc");
3555 DEFSYM (Qdefun
, "defun");
3557 DEFSYM (Qfont_spec
, "font-spec");
3558 DEFSYM (Qfont_entity
, "font-entity");
3559 DEFSYM (Qfont_object
, "font-object");
3561 DEFSYM (Qinteractive_form
, "interactive-form");
3562 DEFSYM (Qdefalias_fset_function
, "defalias-fset-function");
3564 defsubr (&Sindirect_variable
);
3565 defsubr (&Sinteractive_form
);
3568 defsubr (&Stype_of
);
3573 defsubr (&Sintegerp
);
3574 defsubr (&Sinteger_or_marker_p
);
3575 defsubr (&Snumberp
);
3576 defsubr (&Snumber_or_marker_p
);
3578 defsubr (&Snatnump
);
3579 defsubr (&Ssymbolp
);
3580 defsubr (&Skeywordp
);
3581 defsubr (&Sstringp
);
3582 defsubr (&Smultibyte_string_p
);
3583 defsubr (&Svectorp
);
3584 defsubr (&Schar_table_p
);
3585 defsubr (&Svector_or_char_table_p
);
3586 defsubr (&Sbool_vector_p
);
3588 defsubr (&Ssequencep
);
3589 defsubr (&Sbufferp
);
3590 defsubr (&Smarkerp
);
3592 defsubr (&Sbyte_code_function_p
);
3593 defsubr (&Schar_or_string_p
);
3596 defsubr (&Scar_safe
);
3597 defsubr (&Scdr_safe
);
3600 defsubr (&Ssymbol_function
);
3601 defsubr (&Sindirect_function
);
3602 defsubr (&Ssymbol_plist
);
3603 defsubr (&Ssymbol_name
);
3604 defsubr (&Smakunbound
);
3605 defsubr (&Sfmakunbound
);
3607 defsubr (&Sfboundp
);
3609 defsubr (&Sdefalias
);
3610 defsubr (&Ssetplist
);
3611 defsubr (&Ssymbol_value
);
3613 defsubr (&Sdefault_boundp
);
3614 defsubr (&Sdefault_value
);
3615 defsubr (&Sset_default
);
3616 defsubr (&Ssetq_default
);
3617 defsubr (&Smake_variable_buffer_local
);
3618 defsubr (&Smake_local_variable
);
3619 defsubr (&Skill_local_variable
);
3620 defsubr (&Smake_variable_frame_local
);
3621 defsubr (&Slocal_variable_p
);
3622 defsubr (&Slocal_variable_if_set_p
);
3623 defsubr (&Svariable_binding_locus
);
3624 #if 0 /* XXX Remove this. --lorentey */
3625 defsubr (&Sterminal_local_value
);
3626 defsubr (&Sset_terminal_local_value
);
3630 defsubr (&Snumber_to_string
);
3631 defsubr (&Sstring_to_number
);
3632 defsubr (&Seqlsign
);
3654 defsubr (&Sbyteorder
);
3655 defsubr (&Ssubr_arity
);
3656 defsubr (&Ssubr_name
);
3658 defsubr (&Sbool_vector_exclusive_or
);
3659 defsubr (&Sbool_vector_union
);
3660 defsubr (&Sbool_vector_intersection
);
3661 defsubr (&Sbool_vector_set_difference
);
3662 defsubr (&Sbool_vector_not
);
3663 defsubr (&Sbool_vector_subsetp
);
3664 defsubr (&Sbool_vector_count_consecutive
);
3665 defsubr (&Sbool_vector_count_population
);
3667 set_symbol_function (Qwholenump
, XSYMBOL (Qnatnump
)->function
);
3669 DEFVAR_LISP ("most-positive-fixnum", Vmost_positive_fixnum
,
3670 doc
: /* The largest value that is representable in a Lisp integer. */);
3671 Vmost_positive_fixnum
= make_number (MOST_POSITIVE_FIXNUM
);
3672 XSYMBOL (intern_c_string ("most-positive-fixnum"))->constant
= 1;
3674 DEFVAR_LISP ("most-negative-fixnum", Vmost_negative_fixnum
,
3675 doc
: /* The smallest value that is representable in a Lisp integer. */);
3676 Vmost_negative_fixnum
= make_number (MOST_NEGATIVE_FIXNUM
);
3677 XSYMBOL (intern_c_string ("most-negative-fixnum"))->constant
= 1;