Fix problem with images referenced within svg files. (bug#19373)
[emacs.git] / src / data.c
blobb48dbbebabc3f934db46c3606d5883c1e9aeb166
1 /* Primitive operations on Lisp data types for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985-1986, 1988, 1993-1995, 1997-2014 Free Software
3 Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
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/>. */
21 #include <config.h>
22 #include <stdio.h>
24 #include <byteswap.h>
25 #include <count-one-bits.h>
26 #include <count-trailing-zeros.h>
27 #include <intprops.h>
29 #include "lisp.h"
30 #include "puresize.h"
31 #include "character.h"
32 #include "buffer.h"
33 #include "keyboard.h"
34 #include "frame.h"
35 #include "syssignal.h"
36 #include "termhooks.h" /* For FRAME_KBOARD reference in y-or-n-p. */
37 #include "font.h"
38 #include "keymap.h"
40 Lisp_Object Qnil, Qt, Qquote, Qlambda, Qunbound;
41 static Lisp_Object Qsubr;
42 Lisp_Object Qerror_conditions, Qerror_message, Qtop_level;
43 Lisp_Object Qerror, Quser_error, Qquit, Qargs_out_of_range;
44 static Lisp_Object Qwrong_length_argument;
45 static Lisp_Object Qwrong_type_argument;
46 Lisp_Object Qvoid_variable, Qvoid_function;
47 static Lisp_Object Qcyclic_function_indirection;
48 static Lisp_Object Qcyclic_variable_indirection;
49 Lisp_Object Qcircular_list;
50 static Lisp_Object Qsetting_constant;
51 Lisp_Object Qinvalid_read_syntax;
52 Lisp_Object Qinvalid_function, Qwrong_number_of_arguments, Qno_catch;
53 Lisp_Object Qend_of_file, Qarith_error, Qmark_inactive;
54 Lisp_Object Qbeginning_of_buffer, Qend_of_buffer, Qbuffer_read_only;
55 Lisp_Object Qtext_read_only;
57 Lisp_Object Qintegerp, Qwholenump, Qsymbolp, Qlistp, Qconsp;
58 static Lisp_Object Qnatnump;
59 Lisp_Object Qstringp, Qarrayp, Qsequencep, Qbufferp;
60 Lisp_Object Qchar_or_string_p, Qmarkerp, Qinteger_or_marker_p, Qvectorp;
61 Lisp_Object Qbool_vector_p;
62 Lisp_Object Qbuffer_or_string_p;
63 static Lisp_Object Qkeywordp, Qboundp;
64 Lisp_Object Qfboundp;
65 Lisp_Object Qchar_table_p, Qvector_or_char_table_p;
67 Lisp_Object Qcdr;
68 static Lisp_Object Qad_advice_info, Qad_activate_internal;
70 static Lisp_Object Qdomain_error, Qsingularity_error, Qunderflow_error;
71 Lisp_Object Qrange_error, Qoverflow_error;
73 Lisp_Object Qfloatp;
74 Lisp_Object Qnumberp, Qnumber_or_marker_p;
76 Lisp_Object Qinteger, Qsymbol;
77 static Lisp_Object Qcons, Qfloat, Qmisc, Qstring, Qvector;
78 Lisp_Object Qwindow;
79 static Lisp_Object Qoverlay, Qwindow_configuration;
80 static Lisp_Object Qprocess, Qmarker;
81 static Lisp_Object Qcompiled_function, Qframe;
82 Lisp_Object Qbuffer;
83 static Lisp_Object Qchar_table, Qbool_vector, Qhash_table;
84 static Lisp_Object Qsubrp;
85 static Lisp_Object Qmany, Qunevalled;
86 Lisp_Object Qfont_spec, Qfont_entity, Qfont_object;
87 static Lisp_Object Qdefun;
89 Lisp_Object Qinteractive_form;
90 static Lisp_Object Qdefalias_fset_function;
92 static void swap_in_symval_forwarding (struct Lisp_Symbol *, struct Lisp_Buffer_Local_Value *);
94 static bool
95 BOOLFWDP (union Lisp_Fwd *a)
97 return XFWDTYPE (a) == Lisp_Fwd_Bool;
99 static bool
100 INTFWDP (union Lisp_Fwd *a)
102 return XFWDTYPE (a) == Lisp_Fwd_Int;
104 static bool
105 KBOARD_OBJFWDP (union Lisp_Fwd *a)
107 return XFWDTYPE (a) == Lisp_Fwd_Kboard_Obj;
109 static bool
110 OBJFWDP (union Lisp_Fwd *a)
112 return XFWDTYPE (a) == Lisp_Fwd_Obj;
115 static struct Lisp_Boolfwd *
116 XBOOLFWD (union Lisp_Fwd *a)
118 eassert (BOOLFWDP (a));
119 return &a->u_boolfwd;
121 static struct Lisp_Kboard_Objfwd *
122 XKBOARD_OBJFWD (union Lisp_Fwd *a)
124 eassert (KBOARD_OBJFWDP (a));
125 return &a->u_kboard_objfwd;
127 static struct Lisp_Intfwd *
128 XINTFWD (union Lisp_Fwd *a)
130 eassert (INTFWDP (a));
131 return &a->u_intfwd;
133 static struct Lisp_Objfwd *
134 XOBJFWD (union Lisp_Fwd *a)
136 eassert (OBJFWDP (a));
137 return &a->u_objfwd;
140 static void
141 CHECK_SUBR (Lisp_Object x)
143 CHECK_TYPE (SUBRP (x), Qsubrp, x);
146 static void
147 set_blv_found (struct Lisp_Buffer_Local_Value *blv, int found)
149 eassert (found == !EQ (blv->defcell, blv->valcell));
150 blv->found = found;
153 static Lisp_Object
154 blv_value (struct Lisp_Buffer_Local_Value *blv)
156 return XCDR (blv->valcell);
159 static void
160 set_blv_value (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
162 XSETCDR (blv->valcell, val);
165 static void
166 set_blv_where (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
168 blv->where = val;
171 static void
172 set_blv_defcell (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
174 blv->defcell = val;
177 static void
178 set_blv_valcell (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
180 blv->valcell = val;
183 static _Noreturn void
184 wrong_length_argument (Lisp_Object a1, Lisp_Object a2, Lisp_Object a3)
186 Lisp_Object size1 = make_number (bool_vector_size (a1));
187 Lisp_Object size2 = make_number (bool_vector_size (a2));
188 if (NILP (a3))
189 xsignal2 (Qwrong_length_argument, size1, size2);
190 else
191 xsignal3 (Qwrong_length_argument, size1, size2,
192 make_number (bool_vector_size (a3)));
195 Lisp_Object
196 wrong_type_argument (register Lisp_Object predicate, register Lisp_Object value)
198 /* If VALUE is not even a valid Lisp object, we'd want to abort here
199 where we can get a backtrace showing where it came from. We used
200 to try and do that by checking the tagbits, but nowadays all
201 tagbits are potentially valid. */
202 /* if ((unsigned int) XTYPE (value) >= Lisp_Type_Limit)
203 * emacs_abort (); */
205 xsignal2 (Qwrong_type_argument, predicate, value);
208 void
209 pure_write_error (Lisp_Object obj)
211 xsignal2 (Qerror, build_string ("Attempt to modify read-only object"), obj);
214 void
215 args_out_of_range (Lisp_Object a1, Lisp_Object a2)
217 xsignal2 (Qargs_out_of_range, a1, a2);
220 void
221 args_out_of_range_3 (Lisp_Object a1, Lisp_Object a2, Lisp_Object a3)
223 xsignal3 (Qargs_out_of_range, a1, a2, a3);
227 /* Data type predicates. */
229 DEFUN ("eq", Feq, Seq, 2, 2, 0,
230 doc: /* Return t if the two args are the same Lisp object. */)
231 (Lisp_Object obj1, Lisp_Object obj2)
233 if (EQ (obj1, obj2))
234 return Qt;
235 return Qnil;
238 DEFUN ("null", Fnull, Snull, 1, 1, 0,
239 doc: /* Return t if OBJECT is nil. */)
240 (Lisp_Object object)
242 if (NILP (object))
243 return Qt;
244 return Qnil;
247 DEFUN ("type-of", Ftype_of, Stype_of, 1, 1, 0,
248 doc: /* Return a symbol representing the type of OBJECT.
249 The symbol returned names the object's basic type;
250 for example, (type-of 1) returns `integer'. */)
251 (Lisp_Object object)
253 switch (XTYPE (object))
255 case_Lisp_Int:
256 return Qinteger;
258 case Lisp_Symbol:
259 return Qsymbol;
261 case Lisp_String:
262 return Qstring;
264 case Lisp_Cons:
265 return Qcons;
267 case Lisp_Misc:
268 switch (XMISCTYPE (object))
270 case Lisp_Misc_Marker:
271 return Qmarker;
272 case Lisp_Misc_Overlay:
273 return Qoverlay;
274 case Lisp_Misc_Float:
275 return Qfloat;
277 emacs_abort ();
279 case Lisp_Vectorlike:
280 if (WINDOW_CONFIGURATIONP (object))
281 return Qwindow_configuration;
282 if (PROCESSP (object))
283 return Qprocess;
284 if (WINDOWP (object))
285 return Qwindow;
286 if (SUBRP (object))
287 return Qsubr;
288 if (COMPILEDP (object))
289 return Qcompiled_function;
290 if (BUFFERP (object))
291 return Qbuffer;
292 if (CHAR_TABLE_P (object))
293 return Qchar_table;
294 if (BOOL_VECTOR_P (object))
295 return Qbool_vector;
296 if (FRAMEP (object))
297 return Qframe;
298 if (HASH_TABLE_P (object))
299 return Qhash_table;
300 if (FONT_SPEC_P (object))
301 return Qfont_spec;
302 if (FONT_ENTITY_P (object))
303 return Qfont_entity;
304 if (FONT_OBJECT_P (object))
305 return Qfont_object;
306 return Qvector;
308 case Lisp_Float:
309 return Qfloat;
311 default:
312 emacs_abort ();
316 DEFUN ("consp", Fconsp, Sconsp, 1, 1, 0,
317 doc: /* Return t if OBJECT is a cons cell. */)
318 (Lisp_Object object)
320 if (CONSP (object))
321 return Qt;
322 return Qnil;
325 DEFUN ("atom", Fatom, Satom, 1, 1, 0,
326 doc: /* Return t if OBJECT is not a cons cell. This includes nil. */)
327 (Lisp_Object object)
329 if (CONSP (object))
330 return Qnil;
331 return Qt;
334 DEFUN ("listp", Flistp, Slistp, 1, 1, 0,
335 doc: /* Return t if OBJECT is a list, that is, a cons cell or nil.
336 Otherwise, return nil. */)
337 (Lisp_Object object)
339 if (CONSP (object) || NILP (object))
340 return Qt;
341 return Qnil;
344 DEFUN ("nlistp", Fnlistp, Snlistp, 1, 1, 0,
345 doc: /* Return t if OBJECT is not a list. Lists include nil. */)
346 (Lisp_Object object)
348 if (CONSP (object) || NILP (object))
349 return Qnil;
350 return Qt;
353 DEFUN ("symbolp", Fsymbolp, Ssymbolp, 1, 1, 0,
354 doc: /* Return t if OBJECT is a symbol. */)
355 (Lisp_Object object)
357 if (SYMBOLP (object))
358 return Qt;
359 return Qnil;
362 /* Define this in C to avoid unnecessarily consing up the symbol
363 name. */
364 DEFUN ("keywordp", Fkeywordp, Skeywordp, 1, 1, 0,
365 doc: /* Return t if OBJECT is a keyword.
366 This means that it is a symbol with a print name beginning with `:'
367 interned in the initial obarray. */)
368 (Lisp_Object object)
370 if (SYMBOLP (object)
371 && SREF (SYMBOL_NAME (object), 0) == ':'
372 && SYMBOL_INTERNED_IN_INITIAL_OBARRAY_P (object))
373 return Qt;
374 return Qnil;
377 DEFUN ("vectorp", Fvectorp, Svectorp, 1, 1, 0,
378 doc: /* Return t if OBJECT is a vector. */)
379 (Lisp_Object object)
381 if (VECTORP (object))
382 return Qt;
383 return Qnil;
386 DEFUN ("stringp", Fstringp, Sstringp, 1, 1, 0,
387 doc: /* Return t if OBJECT is a string. */)
388 (Lisp_Object object)
390 if (STRINGP (object))
391 return Qt;
392 return Qnil;
395 DEFUN ("multibyte-string-p", Fmultibyte_string_p, Smultibyte_string_p,
396 1, 1, 0,
397 doc: /* Return t if OBJECT is a multibyte string.
398 Return nil if OBJECT is either a unibyte string, or not a string. */)
399 (Lisp_Object object)
401 if (STRINGP (object) && STRING_MULTIBYTE (object))
402 return Qt;
403 return Qnil;
406 DEFUN ("char-table-p", Fchar_table_p, Schar_table_p, 1, 1, 0,
407 doc: /* Return t if OBJECT is a char-table. */)
408 (Lisp_Object object)
410 if (CHAR_TABLE_P (object))
411 return Qt;
412 return Qnil;
415 DEFUN ("vector-or-char-table-p", Fvector_or_char_table_p,
416 Svector_or_char_table_p, 1, 1, 0,
417 doc: /* Return t if OBJECT is a char-table or vector. */)
418 (Lisp_Object object)
420 if (VECTORP (object) || CHAR_TABLE_P (object))
421 return Qt;
422 return Qnil;
425 DEFUN ("bool-vector-p", Fbool_vector_p, Sbool_vector_p, 1, 1, 0,
426 doc: /* Return t if OBJECT is a bool-vector. */)
427 (Lisp_Object object)
429 if (BOOL_VECTOR_P (object))
430 return Qt;
431 return Qnil;
434 DEFUN ("arrayp", Farrayp, Sarrayp, 1, 1, 0,
435 doc: /* Return t if OBJECT is an array (string or vector). */)
436 (Lisp_Object object)
438 if (ARRAYP (object))
439 return Qt;
440 return Qnil;
443 DEFUN ("sequencep", Fsequencep, Ssequencep, 1, 1, 0,
444 doc: /* Return t if OBJECT is a sequence (list or array). */)
445 (register Lisp_Object object)
447 if (CONSP (object) || NILP (object) || ARRAYP (object))
448 return Qt;
449 return Qnil;
452 DEFUN ("bufferp", Fbufferp, Sbufferp, 1, 1, 0,
453 doc: /* Return t if OBJECT is an editor buffer. */)
454 (Lisp_Object object)
456 if (BUFFERP (object))
457 return Qt;
458 return Qnil;
461 DEFUN ("markerp", Fmarkerp, Smarkerp, 1, 1, 0,
462 doc: /* Return t if OBJECT is a marker (editor pointer). */)
463 (Lisp_Object object)
465 if (MARKERP (object))
466 return Qt;
467 return Qnil;
470 DEFUN ("subrp", Fsubrp, Ssubrp, 1, 1, 0,
471 doc: /* Return t if OBJECT is a built-in function. */)
472 (Lisp_Object object)
474 if (SUBRP (object))
475 return Qt;
476 return Qnil;
479 DEFUN ("byte-code-function-p", Fbyte_code_function_p, Sbyte_code_function_p,
480 1, 1, 0,
481 doc: /* Return t if OBJECT is a byte-compiled function object. */)
482 (Lisp_Object object)
484 if (COMPILEDP (object))
485 return Qt;
486 return Qnil;
489 DEFUN ("char-or-string-p", Fchar_or_string_p, Schar_or_string_p, 1, 1, 0,
490 doc: /* Return t if OBJECT is a character or a string. */)
491 (register Lisp_Object object)
493 if (CHARACTERP (object) || STRINGP (object))
494 return Qt;
495 return Qnil;
498 DEFUN ("integerp", Fintegerp, Sintegerp, 1, 1, 0,
499 doc: /* Return t if OBJECT is an integer. */)
500 (Lisp_Object object)
502 if (INTEGERP (object))
503 return Qt;
504 return Qnil;
507 DEFUN ("integer-or-marker-p", Finteger_or_marker_p, Sinteger_or_marker_p, 1, 1, 0,
508 doc: /* Return t if OBJECT is an integer or a marker (editor pointer). */)
509 (register Lisp_Object object)
511 if (MARKERP (object) || INTEGERP (object))
512 return Qt;
513 return Qnil;
516 DEFUN ("natnump", Fnatnump, Snatnump, 1, 1, 0,
517 doc: /* Return t if OBJECT is a nonnegative integer. */)
518 (Lisp_Object object)
520 if (NATNUMP (object))
521 return Qt;
522 return Qnil;
525 DEFUN ("numberp", Fnumberp, Snumberp, 1, 1, 0,
526 doc: /* Return t if OBJECT is a number (floating point or integer). */)
527 (Lisp_Object object)
529 if (NUMBERP (object))
530 return Qt;
531 else
532 return Qnil;
535 DEFUN ("number-or-marker-p", Fnumber_or_marker_p,
536 Snumber_or_marker_p, 1, 1, 0,
537 doc: /* Return t if OBJECT is a number or a marker. */)
538 (Lisp_Object object)
540 if (NUMBERP (object) || MARKERP (object))
541 return Qt;
542 return Qnil;
545 DEFUN ("floatp", Ffloatp, Sfloatp, 1, 1, 0,
546 doc: /* Return t if OBJECT is a floating point number. */)
547 (Lisp_Object object)
549 if (FLOATP (object))
550 return Qt;
551 return Qnil;
555 /* Extract and set components of lists. */
557 DEFUN ("car", Fcar, Scar, 1, 1, 0,
558 doc: /* Return the car of LIST. If arg is nil, return nil.
559 Error if arg is not nil and not a cons cell. See also `car-safe'.
561 See Info node `(elisp)Cons Cells' for a discussion of related basic
562 Lisp concepts such as car, cdr, cons cell and list. */)
563 (register Lisp_Object list)
565 return CAR (list);
568 DEFUN ("car-safe", Fcar_safe, Scar_safe, 1, 1, 0,
569 doc: /* Return the car of OBJECT if it is a cons cell, or else nil. */)
570 (Lisp_Object object)
572 return CAR_SAFE (object);
575 DEFUN ("cdr", Fcdr, Scdr, 1, 1, 0,
576 doc: /* Return the cdr of LIST. If arg is nil, return nil.
577 Error if arg is not nil and not a cons cell. See also `cdr-safe'.
579 See Info node `(elisp)Cons Cells' for a discussion of related basic
580 Lisp concepts such as cdr, car, cons cell and list. */)
581 (register Lisp_Object list)
583 return CDR (list);
586 DEFUN ("cdr-safe", Fcdr_safe, Scdr_safe, 1, 1, 0,
587 doc: /* Return the cdr of OBJECT if it is a cons cell, or else nil. */)
588 (Lisp_Object object)
590 return CDR_SAFE (object);
593 DEFUN ("setcar", Fsetcar, Ssetcar, 2, 2, 0,
594 doc: /* Set the car of CELL to be NEWCAR. Returns NEWCAR. */)
595 (register Lisp_Object cell, Lisp_Object newcar)
597 CHECK_CONS (cell);
598 CHECK_IMPURE (cell);
599 XSETCAR (cell, newcar);
600 return newcar;
603 DEFUN ("setcdr", Fsetcdr, Ssetcdr, 2, 2, 0,
604 doc: /* Set the cdr of CELL to be NEWCDR. Returns NEWCDR. */)
605 (register Lisp_Object cell, Lisp_Object newcdr)
607 CHECK_CONS (cell);
608 CHECK_IMPURE (cell);
609 XSETCDR (cell, newcdr);
610 return newcdr;
613 /* Extract and set components of symbols. */
615 DEFUN ("boundp", Fboundp, Sboundp, 1, 1, 0,
616 doc: /* Return t if SYMBOL's value is not void.
617 Note that if `lexical-binding' is in effect, this refers to the
618 global value outside of any lexical scope. */)
619 (register Lisp_Object symbol)
621 Lisp_Object valcontents;
622 struct Lisp_Symbol *sym;
623 CHECK_SYMBOL (symbol);
624 sym = XSYMBOL (symbol);
626 start:
627 switch (sym->redirect)
629 case SYMBOL_PLAINVAL: valcontents = SYMBOL_VAL (sym); break;
630 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
631 case SYMBOL_LOCALIZED:
633 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
634 if (blv->fwd)
635 /* In set_internal, we un-forward vars when their value is
636 set to Qunbound. */
637 return Qt;
638 else
640 swap_in_symval_forwarding (sym, blv);
641 valcontents = blv_value (blv);
643 break;
645 case SYMBOL_FORWARDED:
646 /* In set_internal, we un-forward vars when their value is
647 set to Qunbound. */
648 return Qt;
649 default: emacs_abort ();
652 return (EQ (valcontents, Qunbound) ? Qnil : Qt);
655 /* FIXME: Make it an alias for function-symbol! */
656 DEFUN ("fboundp", Ffboundp, Sfboundp, 1, 1, 0,
657 doc: /* Return t if SYMBOL's function definition is not void. */)
658 (register Lisp_Object symbol)
660 CHECK_SYMBOL (symbol);
661 return NILP (XSYMBOL (symbol)->function) ? Qnil : Qt;
664 DEFUN ("makunbound", Fmakunbound, Smakunbound, 1, 1, 0,
665 doc: /* Make SYMBOL's value be void.
666 Return SYMBOL. */)
667 (register Lisp_Object symbol)
669 CHECK_SYMBOL (symbol);
670 if (SYMBOL_CONSTANT_P (symbol))
671 xsignal1 (Qsetting_constant, symbol);
672 Fset (symbol, Qunbound);
673 return symbol;
676 DEFUN ("fmakunbound", Ffmakunbound, Sfmakunbound, 1, 1, 0,
677 doc: /* Make SYMBOL's function definition be nil.
678 Return SYMBOL. */)
679 (register Lisp_Object symbol)
681 CHECK_SYMBOL (symbol);
682 if (NILP (symbol) || EQ (symbol, Qt))
683 xsignal1 (Qsetting_constant, symbol);
684 set_symbol_function (symbol, Qnil);
685 return symbol;
688 DEFUN ("symbol-function", Fsymbol_function, Ssymbol_function, 1, 1, 0,
689 doc: /* Return SYMBOL's function definition, or nil if that is void. */)
690 (register Lisp_Object symbol)
692 CHECK_SYMBOL (symbol);
693 return XSYMBOL (symbol)->function;
696 DEFUN ("symbol-plist", Fsymbol_plist, Ssymbol_plist, 1, 1, 0,
697 doc: /* Return SYMBOL's property list. */)
698 (register Lisp_Object symbol)
700 CHECK_SYMBOL (symbol);
701 return XSYMBOL (symbol)->plist;
704 DEFUN ("symbol-name", Fsymbol_name, Ssymbol_name, 1, 1, 0,
705 doc: /* Return SYMBOL's name, a string. */)
706 (register Lisp_Object symbol)
708 register Lisp_Object name;
710 CHECK_SYMBOL (symbol);
711 name = SYMBOL_NAME (symbol);
712 return name;
715 DEFUN ("fset", Ffset, Sfset, 2, 2, 0,
716 doc: /* Set SYMBOL's function definition to DEFINITION, and return DEFINITION. */)
717 (register Lisp_Object symbol, Lisp_Object definition)
719 register Lisp_Object function;
720 CHECK_SYMBOL (symbol);
722 function = XSYMBOL (symbol)->function;
724 if (!NILP (Vautoload_queue) && !NILP (function))
725 Vautoload_queue = Fcons (Fcons (symbol, function), Vautoload_queue);
727 if (AUTOLOADP (function))
728 Fput (symbol, Qautoload, XCDR (function));
730 /* Convert to eassert or remove after GC bug is found. In the
731 meantime, check unconditionally, at a slight perf hit. */
732 if (! valid_lisp_object_p (definition))
733 emacs_abort ();
735 set_symbol_function (symbol, definition);
737 return definition;
740 DEFUN ("defalias", Fdefalias, Sdefalias, 2, 3, 0,
741 doc: /* Set SYMBOL's function definition to DEFINITION.
742 Associates the function with the current load file, if any.
743 The optional third argument DOCSTRING specifies the documentation string
744 for SYMBOL; if it is omitted or nil, SYMBOL uses the documentation string
745 determined by DEFINITION.
747 Internally, this normally uses `fset', but if SYMBOL has a
748 `defalias-fset-function' property, the associated value is used instead.
750 The return value is undefined. */)
751 (register Lisp_Object symbol, Lisp_Object definition, Lisp_Object docstring)
753 CHECK_SYMBOL (symbol);
754 if (!NILP (Vpurify_flag)
755 /* If `definition' is a keymap, immutable (and copying) is wrong. */
756 && !KEYMAPP (definition))
757 definition = Fpurecopy (definition);
760 bool autoload = AUTOLOADP (definition);
761 if (NILP (Vpurify_flag) || !autoload)
762 { /* Only add autoload entries after dumping, because the ones before are
763 not useful and else we get loads of them from the loaddefs.el. */
765 if (AUTOLOADP (XSYMBOL (symbol)->function))
766 /* Remember that the function was already an autoload. */
767 LOADHIST_ATTACH (Fcons (Qt, symbol));
768 LOADHIST_ATTACH (Fcons (autoload ? Qautoload : Qdefun, symbol));
772 { /* Handle automatic advice activation. */
773 Lisp_Object hook = Fget (symbol, Qdefalias_fset_function);
774 if (!NILP (hook))
775 call2 (hook, symbol, definition);
776 else
777 Ffset (symbol, definition);
780 if (!NILP (docstring))
781 Fput (symbol, Qfunction_documentation, docstring);
782 /* We used to return `definition', but now that `defun' and `defmacro' expand
783 to a call to `defalias', we return `symbol' for backward compatibility
784 (bug#11686). */
785 return symbol;
788 DEFUN ("setplist", Fsetplist, Ssetplist, 2, 2, 0,
789 doc: /* Set SYMBOL's property list to NEWPLIST, and return NEWPLIST. */)
790 (register Lisp_Object symbol, Lisp_Object newplist)
792 CHECK_SYMBOL (symbol);
793 set_symbol_plist (symbol, newplist);
794 return newplist;
797 DEFUN ("subr-arity", Fsubr_arity, Ssubr_arity, 1, 1, 0,
798 doc: /* Return minimum and maximum number of args allowed for SUBR.
799 SUBR must be a built-in function.
800 The returned value is a pair (MIN . MAX). MIN is the minimum number
801 of args. MAX is the maximum number or the symbol `many', for a
802 function with `&rest' args, or `unevalled' for a special form. */)
803 (Lisp_Object subr)
805 short minargs, maxargs;
806 CHECK_SUBR (subr);
807 minargs = XSUBR (subr)->min_args;
808 maxargs = XSUBR (subr)->max_args;
809 return Fcons (make_number (minargs),
810 maxargs == MANY ? Qmany
811 : maxargs == UNEVALLED ? Qunevalled
812 : make_number (maxargs));
815 DEFUN ("subr-name", Fsubr_name, Ssubr_name, 1, 1, 0,
816 doc: /* Return name of subroutine SUBR.
817 SUBR must be a built-in function. */)
818 (Lisp_Object subr)
820 const char *name;
821 CHECK_SUBR (subr);
822 name = XSUBR (subr)->symbol_name;
823 return build_string (name);
826 DEFUN ("interactive-form", Finteractive_form, Sinteractive_form, 1, 1, 0,
827 doc: /* Return the interactive form of CMD or nil if none.
828 If CMD is not a command, the return value is nil.
829 Value, if non-nil, is a list \(interactive SPEC). */)
830 (Lisp_Object cmd)
832 Lisp_Object fun = indirect_function (cmd); /* Check cycles. */
834 if (NILP (fun))
835 return Qnil;
837 /* Use an `interactive-form' property if present, analogous to the
838 function-documentation property. */
839 fun = cmd;
840 while (SYMBOLP (fun))
842 Lisp_Object tmp = Fget (fun, Qinteractive_form);
843 if (!NILP (tmp))
844 return tmp;
845 else
846 fun = Fsymbol_function (fun);
849 if (SUBRP (fun))
851 const char *spec = XSUBR (fun)->intspec;
852 if (spec)
853 return list2 (Qinteractive,
854 (*spec != '(') ? build_string (spec) :
855 Fcar (Fread_from_string (build_string (spec), Qnil, Qnil)));
857 else if (COMPILEDP (fun))
859 if ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE)
860 return list2 (Qinteractive, AREF (fun, COMPILED_INTERACTIVE));
862 else if (AUTOLOADP (fun))
863 return Finteractive_form (Fautoload_do_load (fun, cmd, Qnil));
864 else if (CONSP (fun))
866 Lisp_Object funcar = XCAR (fun);
867 if (EQ (funcar, Qclosure))
868 return Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun))));
869 else if (EQ (funcar, Qlambda))
870 return Fassq (Qinteractive, Fcdr (XCDR (fun)));
872 return Qnil;
876 /***********************************************************************
877 Getting and Setting Values of Symbols
878 ***********************************************************************/
880 /* Return the symbol holding SYMBOL's value. Signal
881 `cyclic-variable-indirection' if SYMBOL's chain of variable
882 indirections contains a loop. */
884 struct Lisp_Symbol *
885 indirect_variable (struct Lisp_Symbol *symbol)
887 struct Lisp_Symbol *tortoise, *hare;
889 hare = tortoise = symbol;
891 while (hare->redirect == SYMBOL_VARALIAS)
893 hare = SYMBOL_ALIAS (hare);
894 if (hare->redirect != SYMBOL_VARALIAS)
895 break;
897 hare = SYMBOL_ALIAS (hare);
898 tortoise = SYMBOL_ALIAS (tortoise);
900 if (hare == tortoise)
902 Lisp_Object tem;
903 XSETSYMBOL (tem, symbol);
904 xsignal1 (Qcyclic_variable_indirection, tem);
908 return hare;
912 DEFUN ("indirect-variable", Findirect_variable, Sindirect_variable, 1, 1, 0,
913 doc: /* Return the variable at the end of OBJECT's variable chain.
914 If OBJECT is a symbol, follow its variable indirections (if any), and
915 return the variable at the end of the chain of aliases. See Info node
916 `(elisp)Variable Aliases'.
918 If OBJECT is not a symbol, just return it. If there is a loop in the
919 chain of aliases, signal a `cyclic-variable-indirection' error. */)
920 (Lisp_Object object)
922 if (SYMBOLP (object))
924 struct Lisp_Symbol *sym = indirect_variable (XSYMBOL (object));
925 XSETSYMBOL (object, sym);
927 return object;
931 /* Given the raw contents of a symbol value cell,
932 return the Lisp value of the symbol.
933 This does not handle buffer-local variables; use
934 swap_in_symval_forwarding for that. */
936 Lisp_Object
937 do_symval_forwarding (register union Lisp_Fwd *valcontents)
939 register Lisp_Object val;
940 switch (XFWDTYPE (valcontents))
942 case Lisp_Fwd_Int:
943 XSETINT (val, *XINTFWD (valcontents)->intvar);
944 return val;
946 case Lisp_Fwd_Bool:
947 return (*XBOOLFWD (valcontents)->boolvar ? Qt : Qnil);
949 case Lisp_Fwd_Obj:
950 return *XOBJFWD (valcontents)->objvar;
952 case Lisp_Fwd_Buffer_Obj:
953 return per_buffer_value (current_buffer,
954 XBUFFER_OBJFWD (valcontents)->offset);
956 case Lisp_Fwd_Kboard_Obj:
957 /* We used to simply use current_kboard here, but from Lisp
958 code, its value is often unexpected. It seems nicer to
959 allow constructions like this to work as intuitively expected:
961 (with-selected-frame frame
962 (define-key local-function-map "\eOP" [f1]))
964 On the other hand, this affects the semantics of
965 last-command and real-last-command, and people may rely on
966 that. I took a quick look at the Lisp codebase, and I
967 don't think anything will break. --lorentey */
968 return *(Lisp_Object *)(XKBOARD_OBJFWD (valcontents)->offset
969 + (char *)FRAME_KBOARD (SELECTED_FRAME ()));
970 default: emacs_abort ();
974 /* Used to signal a user-friendly error when symbol WRONG is
975 not a member of CHOICE, which should be a list of symbols. */
977 void
978 wrong_choice (Lisp_Object choice, Lisp_Object wrong)
980 ptrdiff_t i = 0, len = XINT (Flength (choice));
981 Lisp_Object obj, *args;
982 AUTO_STRING (one_of, "One of ");
983 AUTO_STRING (comma, ", ");
984 AUTO_STRING (or, " or ");
985 AUTO_STRING (should_be_specified, " should be specified");
987 USE_SAFE_ALLOCA;
988 SAFE_ALLOCA_LISP (args, len * 2 + 1);
990 args[i++] = one_of;
992 for (obj = choice; !NILP (obj); obj = XCDR (obj))
994 args[i++] = SYMBOL_NAME (XCAR (obj));
995 args[i++] = (NILP (XCDR (obj)) ? should_be_specified
996 : NILP (XCDR (XCDR (obj))) ? or : comma);
999 obj = Fconcat (i, args);
1000 SAFE_FREE ();
1001 xsignal2 (Qerror, obj, wrong);
1004 /* Used to signal a user-friendly error if WRONG is not a number or
1005 integer/floating-point number outsize of inclusive MIN..MAX range. */
1007 static void
1008 wrong_range (Lisp_Object min, Lisp_Object max, Lisp_Object wrong)
1010 AUTO_STRING (value_should_be_from, "Value should be from ");
1011 AUTO_STRING (to, " to ");
1012 xsignal2 (Qerror,
1013 Fconcat (4, ((Lisp_Object [])
1014 {value_should_be_from, Fnumber_to_string (min),
1015 to, Fnumber_to_string (max)})),
1016 wrong);
1019 /* Store NEWVAL into SYMBOL, where VALCONTENTS is found in the value cell
1020 of SYMBOL. If SYMBOL is buffer-local, VALCONTENTS should be the
1021 buffer-independent contents of the value cell: forwarded just one
1022 step past the buffer-localness.
1024 BUF non-zero means set the value in buffer BUF instead of the
1025 current buffer. This only plays a role for per-buffer variables. */
1027 static void
1028 store_symval_forwarding (union Lisp_Fwd *valcontents, register Lisp_Object newval, struct buffer *buf)
1030 switch (XFWDTYPE (valcontents))
1032 case Lisp_Fwd_Int:
1033 CHECK_NUMBER (newval);
1034 *XINTFWD (valcontents)->intvar = XINT (newval);
1035 break;
1037 case Lisp_Fwd_Bool:
1038 *XBOOLFWD (valcontents)->boolvar = !NILP (newval);
1039 break;
1041 case Lisp_Fwd_Obj:
1042 *XOBJFWD (valcontents)->objvar = newval;
1044 /* If this variable is a default for something stored
1045 in the buffer itself, such as default-fill-column,
1046 find the buffers that don't have local values for it
1047 and update them. */
1048 if (XOBJFWD (valcontents)->objvar > (Lisp_Object *) &buffer_defaults
1049 && XOBJFWD (valcontents)->objvar < (Lisp_Object *) (&buffer_defaults + 1))
1051 int offset = ((char *) XOBJFWD (valcontents)->objvar
1052 - (char *) &buffer_defaults);
1053 int idx = PER_BUFFER_IDX (offset);
1055 Lisp_Object tail, buf;
1057 if (idx <= 0)
1058 break;
1060 FOR_EACH_LIVE_BUFFER (tail, buf)
1062 struct buffer *b = XBUFFER (buf);
1064 if (! PER_BUFFER_VALUE_P (b, idx))
1065 set_per_buffer_value (b, offset, newval);
1068 break;
1070 case Lisp_Fwd_Buffer_Obj:
1072 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1073 Lisp_Object predicate = XBUFFER_OBJFWD (valcontents)->predicate;
1075 if (!NILP (newval))
1077 if (SYMBOLP (predicate))
1079 Lisp_Object prop;
1081 if ((prop = Fget (predicate, Qchoice), !NILP (prop)))
1083 if (NILP (Fmemq (newval, prop)))
1084 wrong_choice (prop, newval);
1086 else if ((prop = Fget (predicate, Qrange), !NILP (prop)))
1088 Lisp_Object min = XCAR (prop), max = XCDR (prop);
1090 if (!NUMBERP (newval)
1091 || !NILP (arithcompare (newval, min, ARITH_LESS))
1092 || !NILP (arithcompare (newval, max, ARITH_GRTR)))
1093 wrong_range (min, max, newval);
1095 else if (FUNCTIONP (predicate))
1097 if (NILP (call1 (predicate, newval)))
1098 wrong_type_argument (predicate, newval);
1102 if (buf == NULL)
1103 buf = current_buffer;
1104 set_per_buffer_value (buf, offset, newval);
1106 break;
1108 case Lisp_Fwd_Kboard_Obj:
1110 char *base = (char *) FRAME_KBOARD (SELECTED_FRAME ());
1111 char *p = base + XKBOARD_OBJFWD (valcontents)->offset;
1112 *(Lisp_Object *) p = newval;
1114 break;
1116 default:
1117 emacs_abort (); /* goto def; */
1121 /* Set up SYMBOL to refer to its global binding. This makes it safe
1122 to alter the status of other bindings. BEWARE: this may be called
1123 during the mark phase of GC, where we assume that Lisp_Object slots
1124 of BLV are marked after this function has changed them. */
1126 void
1127 swap_in_global_binding (struct Lisp_Symbol *symbol)
1129 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (symbol);
1131 /* Unload the previously loaded binding. */
1132 if (blv->fwd)
1133 set_blv_value (blv, do_symval_forwarding (blv->fwd));
1135 /* Select the global binding in the symbol. */
1136 set_blv_valcell (blv, blv->defcell);
1137 if (blv->fwd)
1138 store_symval_forwarding (blv->fwd, XCDR (blv->defcell), NULL);
1140 /* Indicate that the global binding is set up now. */
1141 set_blv_where (blv, Qnil);
1142 set_blv_found (blv, 0);
1145 /* Set up the buffer-local symbol SYMBOL for validity in the current buffer.
1146 VALCONTENTS is the contents of its value cell,
1147 which points to a struct Lisp_Buffer_Local_Value.
1149 Return the value forwarded one step past the buffer-local stage.
1150 This could be another forwarding pointer. */
1152 static void
1153 swap_in_symval_forwarding (struct Lisp_Symbol *symbol, struct Lisp_Buffer_Local_Value *blv)
1155 register Lisp_Object tem1;
1157 eassert (blv == SYMBOL_BLV (symbol));
1159 tem1 = blv->where;
1161 if (NILP (tem1)
1162 || (blv->frame_local
1163 ? !EQ (selected_frame, tem1)
1164 : current_buffer != XBUFFER (tem1)))
1167 /* Unload the previously loaded binding. */
1168 tem1 = blv->valcell;
1169 if (blv->fwd)
1170 set_blv_value (blv, do_symval_forwarding (blv->fwd));
1171 /* Choose the new binding. */
1173 Lisp_Object var;
1174 XSETSYMBOL (var, symbol);
1175 if (blv->frame_local)
1177 tem1 = assq_no_quit (var, XFRAME (selected_frame)->param_alist);
1178 set_blv_where (blv, selected_frame);
1180 else
1182 tem1 = assq_no_quit (var, BVAR (current_buffer, local_var_alist));
1183 set_blv_where (blv, Fcurrent_buffer ());
1186 if (!(blv->found = !NILP (tem1)))
1187 tem1 = blv->defcell;
1189 /* Load the new binding. */
1190 set_blv_valcell (blv, tem1);
1191 if (blv->fwd)
1192 store_symval_forwarding (blv->fwd, blv_value (blv), NULL);
1196 /* Find the value of a symbol, returning Qunbound if it's not bound.
1197 This is helpful for code which just wants to get a variable's value
1198 if it has one, without signaling an error.
1199 Note that it must not be possible to quit
1200 within this function. Great care is required for this. */
1202 Lisp_Object
1203 find_symbol_value (Lisp_Object symbol)
1205 struct Lisp_Symbol *sym;
1207 CHECK_SYMBOL (symbol);
1208 sym = XSYMBOL (symbol);
1210 start:
1211 switch (sym->redirect)
1213 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1214 case SYMBOL_PLAINVAL: return SYMBOL_VAL (sym);
1215 case SYMBOL_LOCALIZED:
1217 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1218 swap_in_symval_forwarding (sym, blv);
1219 return blv->fwd ? do_symval_forwarding (blv->fwd) : blv_value (blv);
1221 /* FALLTHROUGH */
1222 case SYMBOL_FORWARDED:
1223 return do_symval_forwarding (SYMBOL_FWD (sym));
1224 default: emacs_abort ();
1228 DEFUN ("symbol-value", Fsymbol_value, Ssymbol_value, 1, 1, 0,
1229 doc: /* Return SYMBOL's value. Error if that is void.
1230 Note that if `lexical-binding' is in effect, this returns the
1231 global value outside of any lexical scope. */)
1232 (Lisp_Object symbol)
1234 Lisp_Object val;
1236 val = find_symbol_value (symbol);
1237 if (!EQ (val, Qunbound))
1238 return val;
1240 xsignal1 (Qvoid_variable, symbol);
1243 DEFUN ("set", Fset, Sset, 2, 2, 0,
1244 doc: /* Set SYMBOL's value to NEWVAL, and return NEWVAL. */)
1245 (register Lisp_Object symbol, Lisp_Object newval)
1247 set_internal (symbol, newval, Qnil, 0);
1248 return newval;
1251 /* Store the value NEWVAL into SYMBOL.
1252 If buffer/frame-locality is an issue, WHERE specifies which context to use.
1253 (nil stands for the current buffer/frame).
1255 If BINDFLAG is false, then if this symbol is supposed to become
1256 local in every buffer where it is set, then we make it local.
1257 If BINDFLAG is true, we don't do that. */
1259 void
1260 set_internal (Lisp_Object symbol, Lisp_Object newval, Lisp_Object where,
1261 bool bindflag)
1263 bool voide = EQ (newval, Qunbound);
1264 struct Lisp_Symbol *sym;
1265 Lisp_Object tem1;
1267 /* If restoring in a dead buffer, do nothing. */
1268 /* if (BUFFERP (where) && NILP (XBUFFER (where)->name))
1269 return; */
1271 CHECK_SYMBOL (symbol);
1272 if (SYMBOL_CONSTANT_P (symbol))
1274 if (NILP (Fkeywordp (symbol))
1275 || !EQ (newval, Fsymbol_value (symbol)))
1276 xsignal1 (Qsetting_constant, symbol);
1277 else
1278 /* Allow setting keywords to their own value. */
1279 return;
1282 sym = XSYMBOL (symbol);
1284 start:
1285 switch (sym->redirect)
1287 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1288 case SYMBOL_PLAINVAL: SET_SYMBOL_VAL (sym , newval); return;
1289 case SYMBOL_LOCALIZED:
1291 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1292 if (NILP (where))
1294 if (blv->frame_local)
1295 where = selected_frame;
1296 else
1297 XSETBUFFER (where, current_buffer);
1299 /* If the current buffer is not the buffer whose binding is
1300 loaded, or if there may be frame-local bindings and the frame
1301 isn't the right one, or if it's a Lisp_Buffer_Local_Value and
1302 the default binding is loaded, the loaded binding may be the
1303 wrong one. */
1304 if (!EQ (blv->where, where)
1305 /* Also unload a global binding (if the var is local_if_set). */
1306 || (EQ (blv->valcell, blv->defcell)))
1308 /* The currently loaded binding is not necessarily valid.
1309 We need to unload it, and choose a new binding. */
1311 /* Write out `realvalue' to the old loaded binding. */
1312 if (blv->fwd)
1313 set_blv_value (blv, do_symval_forwarding (blv->fwd));
1315 /* Find the new binding. */
1316 XSETSYMBOL (symbol, sym); /* May have changed via aliasing. */
1317 tem1 = assq_no_quit (symbol,
1318 (blv->frame_local
1319 ? XFRAME (where)->param_alist
1320 : BVAR (XBUFFER (where), local_var_alist)));
1321 set_blv_where (blv, where);
1322 blv->found = 1;
1324 if (NILP (tem1))
1326 /* This buffer still sees the default value. */
1328 /* If the variable is a Lisp_Some_Buffer_Local_Value,
1329 or if this is `let' rather than `set',
1330 make CURRENT-ALIST-ELEMENT point to itself,
1331 indicating that we're seeing the default value.
1332 Likewise if the variable has been let-bound
1333 in the current buffer. */
1334 if (bindflag || !blv->local_if_set
1335 || let_shadows_buffer_binding_p (sym))
1337 blv->found = 0;
1338 tem1 = blv->defcell;
1340 /* If it's a local_if_set, being set not bound,
1341 and we're not within a let that was made for this buffer,
1342 create a new buffer-local binding for the variable.
1343 That means, give this buffer a new assoc for a local value
1344 and load that binding. */
1345 else
1347 /* local_if_set is only supported for buffer-local
1348 bindings, not for frame-local bindings. */
1349 eassert (!blv->frame_local);
1350 tem1 = Fcons (symbol, XCDR (blv->defcell));
1351 bset_local_var_alist
1352 (XBUFFER (where),
1353 Fcons (tem1, BVAR (XBUFFER (where), local_var_alist)));
1357 /* Record which binding is now loaded. */
1358 set_blv_valcell (blv, tem1);
1361 /* Store the new value in the cons cell. */
1362 set_blv_value (blv, newval);
1364 if (blv->fwd)
1366 if (voide)
1367 /* If storing void (making the symbol void), forward only through
1368 buffer-local indicator, not through Lisp_Objfwd, etc. */
1369 blv->fwd = NULL;
1370 else
1371 store_symval_forwarding (blv->fwd, newval,
1372 BUFFERP (where)
1373 ? XBUFFER (where) : current_buffer);
1375 break;
1377 case SYMBOL_FORWARDED:
1379 struct buffer *buf
1380 = BUFFERP (where) ? XBUFFER (where) : current_buffer;
1381 union Lisp_Fwd *innercontents = SYMBOL_FWD (sym);
1382 if (BUFFER_OBJFWDP (innercontents))
1384 int offset = XBUFFER_OBJFWD (innercontents)->offset;
1385 int idx = PER_BUFFER_IDX (offset);
1386 if (idx > 0
1387 && !bindflag
1388 && !let_shadows_buffer_binding_p (sym))
1389 SET_PER_BUFFER_VALUE_P (buf, idx, 1);
1392 if (voide)
1393 { /* If storing void (making the symbol void), forward only through
1394 buffer-local indicator, not through Lisp_Objfwd, etc. */
1395 sym->redirect = SYMBOL_PLAINVAL;
1396 SET_SYMBOL_VAL (sym, newval);
1398 else
1399 store_symval_forwarding (/* sym, */ innercontents, newval, buf);
1400 break;
1402 default: emacs_abort ();
1404 return;
1407 /* Access or set a buffer-local symbol's default value. */
1409 /* Return the default value of SYMBOL, but don't check for voidness.
1410 Return Qunbound if it is void. */
1412 static Lisp_Object
1413 default_value (Lisp_Object symbol)
1415 struct Lisp_Symbol *sym;
1417 CHECK_SYMBOL (symbol);
1418 sym = XSYMBOL (symbol);
1420 start:
1421 switch (sym->redirect)
1423 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1424 case SYMBOL_PLAINVAL: return SYMBOL_VAL (sym);
1425 case SYMBOL_LOCALIZED:
1427 /* If var is set up for a buffer that lacks a local value for it,
1428 the current value is nominally the default value.
1429 But the `realvalue' slot may be more up to date, since
1430 ordinary setq stores just that slot. So use that. */
1431 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1432 if (blv->fwd && EQ (blv->valcell, blv->defcell))
1433 return do_symval_forwarding (blv->fwd);
1434 else
1435 return XCDR (blv->defcell);
1437 case SYMBOL_FORWARDED:
1439 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
1441 /* For a built-in buffer-local variable, get the default value
1442 rather than letting do_symval_forwarding get the current value. */
1443 if (BUFFER_OBJFWDP (valcontents))
1445 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1446 if (PER_BUFFER_IDX (offset) != 0)
1447 return per_buffer_default (offset);
1450 /* For other variables, get the current value. */
1451 return do_symval_forwarding (valcontents);
1453 default: emacs_abort ();
1457 DEFUN ("default-boundp", Fdefault_boundp, Sdefault_boundp, 1, 1, 0,
1458 doc: /* Return t if SYMBOL has a non-void default value.
1459 This is the value that is seen in buffers that do not have their own values
1460 for this variable. */)
1461 (Lisp_Object symbol)
1463 register Lisp_Object value;
1465 value = default_value (symbol);
1466 return (EQ (value, Qunbound) ? Qnil : Qt);
1469 DEFUN ("default-value", Fdefault_value, Sdefault_value, 1, 1, 0,
1470 doc: /* Return SYMBOL's default value.
1471 This is the value that is seen in buffers that do not have their own values
1472 for this variable. The default value is meaningful for variables with
1473 local bindings in certain buffers. */)
1474 (Lisp_Object symbol)
1476 Lisp_Object value = default_value (symbol);
1477 if (!EQ (value, Qunbound))
1478 return value;
1480 xsignal1 (Qvoid_variable, symbol);
1483 DEFUN ("set-default", Fset_default, Sset_default, 2, 2, 0,
1484 doc: /* Set SYMBOL's default value to VALUE. SYMBOL and VALUE are evaluated.
1485 The default value is seen in buffers that do not have their own values
1486 for this variable. */)
1487 (Lisp_Object symbol, Lisp_Object value)
1489 struct Lisp_Symbol *sym;
1491 CHECK_SYMBOL (symbol);
1492 if (SYMBOL_CONSTANT_P (symbol))
1494 if (NILP (Fkeywordp (symbol))
1495 || !EQ (value, Fdefault_value (symbol)))
1496 xsignal1 (Qsetting_constant, symbol);
1497 else
1498 /* Allow setting keywords to their own value. */
1499 return value;
1501 sym = XSYMBOL (symbol);
1503 start:
1504 switch (sym->redirect)
1506 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1507 case SYMBOL_PLAINVAL: return Fset (symbol, value);
1508 case SYMBOL_LOCALIZED:
1510 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1512 /* Store new value into the DEFAULT-VALUE slot. */
1513 XSETCDR (blv->defcell, value);
1515 /* If the default binding is now loaded, set the REALVALUE slot too. */
1516 if (blv->fwd && EQ (blv->defcell, blv->valcell))
1517 store_symval_forwarding (blv->fwd, value, NULL);
1518 return value;
1520 case SYMBOL_FORWARDED:
1522 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
1524 /* Handle variables like case-fold-search that have special slots
1525 in the buffer.
1526 Make them work apparently like Lisp_Buffer_Local_Value variables. */
1527 if (BUFFER_OBJFWDP (valcontents))
1529 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1530 int idx = PER_BUFFER_IDX (offset);
1532 set_per_buffer_default (offset, value);
1534 /* If this variable is not always local in all buffers,
1535 set it in the buffers that don't nominally have a local value. */
1536 if (idx > 0)
1538 struct buffer *b;
1540 FOR_EACH_BUFFER (b)
1541 if (!PER_BUFFER_VALUE_P (b, idx))
1542 set_per_buffer_value (b, offset, value);
1544 return value;
1546 else
1547 return Fset (symbol, value);
1549 default: emacs_abort ();
1553 DEFUN ("setq-default", Fsetq_default, Ssetq_default, 0, UNEVALLED, 0,
1554 doc: /* Set the default value of variable VAR to VALUE.
1555 VAR, the variable name, is literal (not evaluated);
1556 VALUE is an expression: it is evaluated and its value returned.
1557 The default value of a variable is seen in buffers
1558 that do not have their own values for the variable.
1560 More generally, you can use multiple variables and values, as in
1561 (setq-default VAR VALUE VAR VALUE...)
1562 This sets each VAR's default value to the corresponding VALUE.
1563 The VALUE for the Nth VAR can refer to the new default values
1564 of previous VARs.
1565 usage: (setq-default [VAR VALUE]...) */)
1566 (Lisp_Object args)
1568 Lisp_Object args_left, symbol, val;
1569 struct gcpro gcpro1;
1571 args_left = val = args;
1572 GCPRO1 (args);
1574 while (CONSP (args_left))
1576 val = eval_sub (Fcar (XCDR (args_left)));
1577 symbol = XCAR (args_left);
1578 Fset_default (symbol, val);
1579 args_left = Fcdr (XCDR (args_left));
1582 UNGCPRO;
1583 return val;
1586 /* Lisp functions for creating and removing buffer-local variables. */
1588 union Lisp_Val_Fwd
1590 Lisp_Object value;
1591 union Lisp_Fwd *fwd;
1594 static struct Lisp_Buffer_Local_Value *
1595 make_blv (struct Lisp_Symbol *sym, bool forwarded,
1596 union Lisp_Val_Fwd valcontents)
1598 struct Lisp_Buffer_Local_Value *blv = xmalloc (sizeof *blv);
1599 Lisp_Object symbol;
1600 Lisp_Object tem;
1602 XSETSYMBOL (symbol, sym);
1603 tem = Fcons (symbol, (forwarded
1604 ? do_symval_forwarding (valcontents.fwd)
1605 : valcontents.value));
1607 /* Buffer_Local_Values cannot have as realval a buffer-local
1608 or keyboard-local forwarding. */
1609 eassert (!(forwarded && BUFFER_OBJFWDP (valcontents.fwd)));
1610 eassert (!(forwarded && KBOARD_OBJFWDP (valcontents.fwd)));
1611 blv->fwd = forwarded ? valcontents.fwd : NULL;
1612 set_blv_where (blv, Qnil);
1613 blv->frame_local = 0;
1614 blv->local_if_set = 0;
1615 set_blv_defcell (blv, tem);
1616 set_blv_valcell (blv, tem);
1617 set_blv_found (blv, 0);
1618 return blv;
1621 DEFUN ("make-variable-buffer-local", Fmake_variable_buffer_local,
1622 Smake_variable_buffer_local, 1, 1, "vMake Variable Buffer Local: ",
1623 doc: /* Make VARIABLE become buffer-local whenever it is set.
1624 At any time, the value for the current buffer is in effect,
1625 unless the variable has never been set in this buffer,
1626 in which case the default value is in effect.
1627 Note that binding the variable with `let', or setting it while
1628 a `let'-style binding made in this buffer is in effect,
1629 does not make the variable buffer-local. Return VARIABLE.
1631 This globally affects all uses of this variable, so it belongs together with
1632 the variable declaration, rather than with its uses (if you just want to make
1633 a variable local to the current buffer for one particular use, use
1634 `make-local-variable'). Buffer-local bindings are normally cleared
1635 while setting up a new major mode, unless they have a `permanent-local'
1636 property.
1638 The function `default-value' gets the default value and `set-default' sets it. */)
1639 (register Lisp_Object variable)
1641 struct Lisp_Symbol *sym;
1642 struct Lisp_Buffer_Local_Value *blv = NULL;
1643 union Lisp_Val_Fwd valcontents IF_LINT (= {LISP_INITIALLY_ZERO});
1644 bool forwarded IF_LINT (= 0);
1646 CHECK_SYMBOL (variable);
1647 sym = XSYMBOL (variable);
1649 start:
1650 switch (sym->redirect)
1652 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1653 case SYMBOL_PLAINVAL:
1654 forwarded = 0; valcontents.value = SYMBOL_VAL (sym);
1655 if (EQ (valcontents.value, Qunbound))
1656 valcontents.value = Qnil;
1657 break;
1658 case SYMBOL_LOCALIZED:
1659 blv = SYMBOL_BLV (sym);
1660 if (blv->frame_local)
1661 error ("Symbol %s may not be buffer-local",
1662 SDATA (SYMBOL_NAME (variable)));
1663 break;
1664 case SYMBOL_FORWARDED:
1665 forwarded = 1; valcontents.fwd = SYMBOL_FWD (sym);
1666 if (KBOARD_OBJFWDP (valcontents.fwd))
1667 error ("Symbol %s may not be buffer-local",
1668 SDATA (SYMBOL_NAME (variable)));
1669 else if (BUFFER_OBJFWDP (valcontents.fwd))
1670 return variable;
1671 break;
1672 default: emacs_abort ();
1675 if (sym->constant)
1676 error ("Symbol %s may not be buffer-local", SDATA (SYMBOL_NAME (variable)));
1678 if (!blv)
1680 blv = make_blv (sym, forwarded, valcontents);
1681 sym->redirect = SYMBOL_LOCALIZED;
1682 SET_SYMBOL_BLV (sym, blv);
1684 Lisp_Object symbol;
1685 XSETSYMBOL (symbol, sym); /* In case `variable' is aliased. */
1686 if (let_shadows_global_binding_p (symbol))
1687 message ("Making %s buffer-local while let-bound!",
1688 SDATA (SYMBOL_NAME (variable)));
1692 blv->local_if_set = 1;
1693 return variable;
1696 DEFUN ("make-local-variable", Fmake_local_variable, Smake_local_variable,
1697 1, 1, "vMake Local Variable: ",
1698 doc: /* Make VARIABLE have a separate value in the current buffer.
1699 Other buffers will continue to share a common default value.
1700 \(The buffer-local value of VARIABLE starts out as the same value
1701 VARIABLE previously had. If VARIABLE was void, it remains void.\)
1702 Return VARIABLE.
1704 If the variable is already arranged to become local when set,
1705 this function causes a local value to exist for this buffer,
1706 just as setting the variable would do.
1708 This function returns VARIABLE, and therefore
1709 (set (make-local-variable 'VARIABLE) VALUE-EXP)
1710 works.
1712 See also `make-variable-buffer-local'.
1714 Do not use `make-local-variable' to make a hook variable buffer-local.
1715 Instead, use `add-hook' and specify t for the LOCAL argument. */)
1716 (Lisp_Object variable)
1718 Lisp_Object tem;
1719 bool forwarded IF_LINT (= 0);
1720 union Lisp_Val_Fwd valcontents IF_LINT (= {LISP_INITIALLY_ZERO});
1721 struct Lisp_Symbol *sym;
1722 struct Lisp_Buffer_Local_Value *blv = NULL;
1724 CHECK_SYMBOL (variable);
1725 sym = XSYMBOL (variable);
1727 start:
1728 switch (sym->redirect)
1730 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1731 case SYMBOL_PLAINVAL:
1732 forwarded = 0; valcontents.value = SYMBOL_VAL (sym); break;
1733 case SYMBOL_LOCALIZED:
1734 blv = SYMBOL_BLV (sym);
1735 if (blv->frame_local)
1736 error ("Symbol %s may not be buffer-local",
1737 SDATA (SYMBOL_NAME (variable)));
1738 break;
1739 case SYMBOL_FORWARDED:
1740 forwarded = 1; valcontents.fwd = SYMBOL_FWD (sym);
1741 if (KBOARD_OBJFWDP (valcontents.fwd))
1742 error ("Symbol %s may not be buffer-local",
1743 SDATA (SYMBOL_NAME (variable)));
1744 break;
1745 default: emacs_abort ();
1748 if (sym->constant)
1749 error ("Symbol %s may not be buffer-local",
1750 SDATA (SYMBOL_NAME (variable)));
1752 if (blv ? blv->local_if_set
1753 : (forwarded && BUFFER_OBJFWDP (valcontents.fwd)))
1755 tem = Fboundp (variable);
1756 /* Make sure the symbol has a local value in this particular buffer,
1757 by setting it to the same value it already has. */
1758 Fset (variable, (EQ (tem, Qt) ? Fsymbol_value (variable) : Qunbound));
1759 return variable;
1761 if (!blv)
1763 blv = make_blv (sym, forwarded, valcontents);
1764 sym->redirect = SYMBOL_LOCALIZED;
1765 SET_SYMBOL_BLV (sym, blv);
1767 Lisp_Object symbol;
1768 XSETSYMBOL (symbol, sym); /* In case `variable' is aliased. */
1769 if (let_shadows_global_binding_p (symbol))
1770 message ("Making %s local to %s while let-bound!",
1771 SDATA (SYMBOL_NAME (variable)),
1772 SDATA (BVAR (current_buffer, name)));
1776 /* Make sure this buffer has its own value of symbol. */
1777 XSETSYMBOL (variable, sym); /* Update in case of aliasing. */
1778 tem = Fassq (variable, BVAR (current_buffer, local_var_alist));
1779 if (NILP (tem))
1781 if (let_shadows_buffer_binding_p (sym))
1782 message ("Making %s buffer-local while locally let-bound!",
1783 SDATA (SYMBOL_NAME (variable)));
1785 /* Swap out any local binding for some other buffer, and make
1786 sure the current value is permanently recorded, if it's the
1787 default value. */
1788 find_symbol_value (variable);
1790 bset_local_var_alist
1791 (current_buffer,
1792 Fcons (Fcons (variable, XCDR (blv->defcell)),
1793 BVAR (current_buffer, local_var_alist)));
1795 /* Make sure symbol does not think it is set up for this buffer;
1796 force it to look once again for this buffer's value. */
1797 if (current_buffer == XBUFFER (blv->where))
1798 set_blv_where (blv, Qnil);
1799 set_blv_found (blv, 0);
1802 /* If the symbol forwards into a C variable, then load the binding
1803 for this buffer now. If C code modifies the variable before we
1804 load the binding in, then that new value will clobber the default
1805 binding the next time we unload it. */
1806 if (blv->fwd)
1807 swap_in_symval_forwarding (sym, blv);
1809 return variable;
1812 DEFUN ("kill-local-variable", Fkill_local_variable, Skill_local_variable,
1813 1, 1, "vKill Local Variable: ",
1814 doc: /* Make VARIABLE no longer have a separate value in the current buffer.
1815 From now on the default value will apply in this buffer. Return VARIABLE. */)
1816 (register Lisp_Object variable)
1818 register Lisp_Object tem;
1819 struct Lisp_Buffer_Local_Value *blv;
1820 struct Lisp_Symbol *sym;
1822 CHECK_SYMBOL (variable);
1823 sym = XSYMBOL (variable);
1825 start:
1826 switch (sym->redirect)
1828 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1829 case SYMBOL_PLAINVAL: return variable;
1830 case SYMBOL_FORWARDED:
1832 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
1833 if (BUFFER_OBJFWDP (valcontents))
1835 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1836 int idx = PER_BUFFER_IDX (offset);
1838 if (idx > 0)
1840 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 0);
1841 set_per_buffer_value (current_buffer, offset,
1842 per_buffer_default (offset));
1845 return variable;
1847 case SYMBOL_LOCALIZED:
1848 blv = SYMBOL_BLV (sym);
1849 if (blv->frame_local)
1850 return variable;
1851 break;
1852 default: emacs_abort ();
1855 /* Get rid of this buffer's alist element, if any. */
1856 XSETSYMBOL (variable, sym); /* Propagate variable indirection. */
1857 tem = Fassq (variable, BVAR (current_buffer, local_var_alist));
1858 if (!NILP (tem))
1859 bset_local_var_alist
1860 (current_buffer,
1861 Fdelq (tem, BVAR (current_buffer, local_var_alist)));
1863 /* If the symbol is set up with the current buffer's binding
1864 loaded, recompute its value. We have to do it now, or else
1865 forwarded objects won't work right. */
1867 Lisp_Object buf; XSETBUFFER (buf, current_buffer);
1868 if (EQ (buf, blv->where))
1870 set_blv_where (blv, Qnil);
1871 blv->found = 0;
1872 find_symbol_value (variable);
1876 return variable;
1879 /* Lisp functions for creating and removing buffer-local variables. */
1881 /* Obsolete since 22.2. NB adjust doc of modify-frame-parameters
1882 when/if this is removed. */
1884 DEFUN ("make-variable-frame-local", Fmake_variable_frame_local, Smake_variable_frame_local,
1885 1, 1, "vMake Variable Frame Local: ",
1886 doc: /* Enable VARIABLE to have frame-local bindings.
1887 This does not create any frame-local bindings for VARIABLE,
1888 it just makes them possible.
1890 A frame-local binding is actually a frame parameter value.
1891 If a frame F has a value for the frame parameter named VARIABLE,
1892 that also acts as a frame-local binding for VARIABLE in F--
1893 provided this function has been called to enable VARIABLE
1894 to have frame-local bindings at all.
1896 The only way to create a frame-local binding for VARIABLE in a frame
1897 is to set the VARIABLE frame parameter of that frame. See
1898 `modify-frame-parameters' for how to set frame parameters.
1900 Note that since Emacs 23.1, variables cannot be both buffer-local and
1901 frame-local any more (buffer-local bindings used to take precedence over
1902 frame-local bindings). */)
1903 (Lisp_Object variable)
1905 bool forwarded;
1906 union Lisp_Val_Fwd valcontents;
1907 struct Lisp_Symbol *sym;
1908 struct Lisp_Buffer_Local_Value *blv = NULL;
1910 CHECK_SYMBOL (variable);
1911 sym = XSYMBOL (variable);
1913 start:
1914 switch (sym->redirect)
1916 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1917 case SYMBOL_PLAINVAL:
1918 forwarded = 0; valcontents.value = SYMBOL_VAL (sym);
1919 if (EQ (valcontents.value, Qunbound))
1920 valcontents.value = Qnil;
1921 break;
1922 case SYMBOL_LOCALIZED:
1923 if (SYMBOL_BLV (sym)->frame_local)
1924 return variable;
1925 else
1926 error ("Symbol %s may not be frame-local",
1927 SDATA (SYMBOL_NAME (variable)));
1928 case SYMBOL_FORWARDED:
1929 forwarded = 1; valcontents.fwd = SYMBOL_FWD (sym);
1930 if (KBOARD_OBJFWDP (valcontents.fwd) || BUFFER_OBJFWDP (valcontents.fwd))
1931 error ("Symbol %s may not be frame-local",
1932 SDATA (SYMBOL_NAME (variable)));
1933 break;
1934 default: emacs_abort ();
1937 if (sym->constant)
1938 error ("Symbol %s may not be frame-local", SDATA (SYMBOL_NAME (variable)));
1940 blv = make_blv (sym, forwarded, valcontents);
1941 blv->frame_local = 1;
1942 sym->redirect = SYMBOL_LOCALIZED;
1943 SET_SYMBOL_BLV (sym, blv);
1945 Lisp_Object symbol;
1946 XSETSYMBOL (symbol, sym); /* In case `variable' is aliased. */
1947 if (let_shadows_global_binding_p (symbol))
1948 message ("Making %s frame-local while let-bound!",
1949 SDATA (SYMBOL_NAME (variable)));
1951 return variable;
1954 DEFUN ("local-variable-p", Flocal_variable_p, Slocal_variable_p,
1955 1, 2, 0,
1956 doc: /* Non-nil if VARIABLE has a local binding in buffer BUFFER.
1957 BUFFER defaults to the current buffer. */)
1958 (Lisp_Object variable, Lisp_Object buffer)
1960 struct buffer *buf = decode_buffer (buffer);
1961 struct Lisp_Symbol *sym;
1963 CHECK_SYMBOL (variable);
1964 sym = XSYMBOL (variable);
1966 start:
1967 switch (sym->redirect)
1969 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1970 case SYMBOL_PLAINVAL: return Qnil;
1971 case SYMBOL_LOCALIZED:
1973 Lisp_Object tail, elt, tmp;
1974 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1975 XSETBUFFER (tmp, buf);
1976 XSETSYMBOL (variable, sym); /* Update in case of aliasing. */
1978 if (EQ (blv->where, tmp)) /* The binding is already loaded. */
1979 return blv_found (blv) ? Qt : Qnil;
1980 else
1981 for (tail = BVAR (buf, local_var_alist); CONSP (tail); tail = XCDR (tail))
1983 elt = XCAR (tail);
1984 if (EQ (variable, XCAR (elt)))
1986 eassert (!blv->frame_local);
1987 return Qt;
1990 return Qnil;
1992 case SYMBOL_FORWARDED:
1994 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
1995 if (BUFFER_OBJFWDP (valcontents))
1997 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1998 int idx = PER_BUFFER_IDX (offset);
1999 if (idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
2000 return Qt;
2002 return Qnil;
2004 default: emacs_abort ();
2008 DEFUN ("local-variable-if-set-p", Flocal_variable_if_set_p, Slocal_variable_if_set_p,
2009 1, 2, 0,
2010 doc: /* Non-nil if VARIABLE is local in buffer BUFFER when set there.
2011 BUFFER defaults to the current buffer.
2013 More precisely, return non-nil if either VARIABLE already has a local
2014 value in BUFFER, or if VARIABLE is automatically buffer-local (see
2015 `make-variable-buffer-local'). */)
2016 (register Lisp_Object variable, Lisp_Object buffer)
2018 struct Lisp_Symbol *sym;
2020 CHECK_SYMBOL (variable);
2021 sym = XSYMBOL (variable);
2023 start:
2024 switch (sym->redirect)
2026 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
2027 case SYMBOL_PLAINVAL: return Qnil;
2028 case SYMBOL_LOCALIZED:
2030 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
2031 if (blv->local_if_set)
2032 return Qt;
2033 XSETSYMBOL (variable, sym); /* Update in case of aliasing. */
2034 return Flocal_variable_p (variable, buffer);
2036 case SYMBOL_FORWARDED:
2037 /* All BUFFER_OBJFWD slots become local if they are set. */
2038 return (BUFFER_OBJFWDP (SYMBOL_FWD (sym)) ? Qt : Qnil);
2039 default: emacs_abort ();
2043 DEFUN ("variable-binding-locus", Fvariable_binding_locus, Svariable_binding_locus,
2044 1, 1, 0,
2045 doc: /* Return a value indicating where VARIABLE's current binding comes from.
2046 If the current binding is buffer-local, the value is the current buffer.
2047 If the current binding is frame-local, the value is the selected frame.
2048 If the current binding is global (the default), the value is nil. */)
2049 (register Lisp_Object variable)
2051 struct Lisp_Symbol *sym;
2053 CHECK_SYMBOL (variable);
2054 sym = XSYMBOL (variable);
2056 /* Make sure the current binding is actually swapped in. */
2057 find_symbol_value (variable);
2059 start:
2060 switch (sym->redirect)
2062 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
2063 case SYMBOL_PLAINVAL: return Qnil;
2064 case SYMBOL_FORWARDED:
2066 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
2067 if (KBOARD_OBJFWDP (valcontents))
2068 return Fframe_terminal (selected_frame);
2069 else if (!BUFFER_OBJFWDP (valcontents))
2070 return Qnil;
2072 /* FALLTHROUGH */
2073 case SYMBOL_LOCALIZED:
2074 /* For a local variable, record both the symbol and which
2075 buffer's or frame's value we are saving. */
2076 if (!NILP (Flocal_variable_p (variable, Qnil)))
2077 return Fcurrent_buffer ();
2078 else if (sym->redirect == SYMBOL_LOCALIZED
2079 && blv_found (SYMBOL_BLV (sym)))
2080 return SYMBOL_BLV (sym)->where;
2081 else
2082 return Qnil;
2083 default: emacs_abort ();
2087 /* This code is disabled now that we use the selected frame to return
2088 keyboard-local-values. */
2089 #if 0
2090 extern struct terminal *get_terminal (Lisp_Object display, int);
2092 DEFUN ("terminal-local-value", Fterminal_local_value,
2093 Sterminal_local_value, 2, 2, 0,
2094 doc: /* Return the terminal-local value of SYMBOL on TERMINAL.
2095 If SYMBOL is not a terminal-local variable, then return its normal
2096 value, like `symbol-value'.
2098 TERMINAL may be a terminal object, a frame, or nil (meaning the
2099 selected frame's terminal device). */)
2100 (Lisp_Object symbol, Lisp_Object terminal)
2102 Lisp_Object result;
2103 struct terminal *t = get_terminal (terminal, 1);
2104 push_kboard (t->kboard);
2105 result = Fsymbol_value (symbol);
2106 pop_kboard ();
2107 return result;
2110 DEFUN ("set-terminal-local-value", Fset_terminal_local_value,
2111 Sset_terminal_local_value, 3, 3, 0,
2112 doc: /* Set the terminal-local binding of SYMBOL on TERMINAL to VALUE.
2113 If VARIABLE is not a terminal-local variable, then set its normal
2114 binding, like `set'.
2116 TERMINAL may be a terminal object, a frame, or nil (meaning the
2117 selected frame's terminal device). */)
2118 (Lisp_Object symbol, Lisp_Object terminal, Lisp_Object value)
2120 Lisp_Object result;
2121 struct terminal *t = get_terminal (terminal, 1);
2122 push_kboard (d->kboard);
2123 result = Fset (symbol, value);
2124 pop_kboard ();
2125 return result;
2127 #endif
2129 /* Find the function at the end of a chain of symbol function indirections. */
2131 /* If OBJECT is a symbol, find the end of its function chain and
2132 return the value found there. If OBJECT is not a symbol, just
2133 return it. If there is a cycle in the function chain, signal a
2134 cyclic-function-indirection error.
2136 This is like Findirect_function, except that it doesn't signal an
2137 error if the chain ends up unbound. */
2138 Lisp_Object
2139 indirect_function (register Lisp_Object object)
2141 Lisp_Object tortoise, hare;
2143 hare = tortoise = object;
2145 for (;;)
2147 if (!SYMBOLP (hare) || NILP (hare))
2148 break;
2149 hare = XSYMBOL (hare)->function;
2150 if (!SYMBOLP (hare) || NILP (hare))
2151 break;
2152 hare = XSYMBOL (hare)->function;
2154 tortoise = XSYMBOL (tortoise)->function;
2156 if (EQ (hare, tortoise))
2157 xsignal1 (Qcyclic_function_indirection, object);
2160 return hare;
2163 DEFUN ("indirect-function", Findirect_function, Sindirect_function, 1, 2, 0,
2164 doc: /* Return the function at the end of OBJECT's function chain.
2165 If OBJECT is not a symbol, just return it. Otherwise, follow all
2166 function indirections to find the final function binding and return it.
2167 If the final symbol in the chain is unbound, signal a void-function error.
2168 Optional arg NOERROR non-nil means to return nil instead of signaling.
2169 Signal a cyclic-function-indirection error if there is a loop in the
2170 function chain of symbols. */)
2171 (register Lisp_Object object, Lisp_Object noerror)
2173 Lisp_Object result;
2175 /* Optimize for no indirection. */
2176 result = object;
2177 if (SYMBOLP (result) && !NILP (result)
2178 && (result = XSYMBOL (result)->function, SYMBOLP (result)))
2179 result = indirect_function (result);
2180 if (!NILP (result))
2181 return result;
2183 if (NILP (noerror))
2184 xsignal1 (Qvoid_function, object);
2186 return Qnil;
2189 /* Extract and set vector and string elements. */
2191 DEFUN ("aref", Faref, Saref, 2, 2, 0,
2192 doc: /* Return the element of ARRAY at index IDX.
2193 ARRAY may be a vector, a string, a char-table, a bool-vector,
2194 or a byte-code object. IDX starts at 0. */)
2195 (register Lisp_Object array, Lisp_Object idx)
2197 register EMACS_INT idxval;
2199 CHECK_NUMBER (idx);
2200 idxval = XINT (idx);
2201 if (STRINGP (array))
2203 int c;
2204 ptrdiff_t idxval_byte;
2206 if (idxval < 0 || idxval >= SCHARS (array))
2207 args_out_of_range (array, idx);
2208 if (! STRING_MULTIBYTE (array))
2209 return make_number ((unsigned char) SREF (array, idxval));
2210 idxval_byte = string_char_to_byte (array, idxval);
2212 c = STRING_CHAR (SDATA (array) + idxval_byte);
2213 return make_number (c);
2215 else if (BOOL_VECTOR_P (array))
2217 if (idxval < 0 || idxval >= bool_vector_size (array))
2218 args_out_of_range (array, idx);
2219 return bool_vector_ref (array, idxval);
2221 else if (CHAR_TABLE_P (array))
2223 CHECK_CHARACTER (idx);
2224 return CHAR_TABLE_REF (array, idxval);
2226 else
2228 ptrdiff_t size = 0;
2229 if (VECTORP (array))
2230 size = ASIZE (array);
2231 else if (COMPILEDP (array))
2232 size = ASIZE (array) & PSEUDOVECTOR_SIZE_MASK;
2233 else
2234 wrong_type_argument (Qarrayp, array);
2236 if (idxval < 0 || idxval >= size)
2237 args_out_of_range (array, idx);
2238 return AREF (array, idxval);
2242 DEFUN ("aset", Faset, Saset, 3, 3, 0,
2243 doc: /* Store into the element of ARRAY at index IDX the value NEWELT.
2244 Return NEWELT. ARRAY may be a vector, a string, a char-table or a
2245 bool-vector. IDX starts at 0. */)
2246 (register Lisp_Object array, Lisp_Object idx, Lisp_Object newelt)
2248 register EMACS_INT idxval;
2250 CHECK_NUMBER (idx);
2251 idxval = XINT (idx);
2252 CHECK_ARRAY (array, Qarrayp);
2253 CHECK_IMPURE (array);
2255 if (VECTORP (array))
2257 if (idxval < 0 || idxval >= ASIZE (array))
2258 args_out_of_range (array, idx);
2259 ASET (array, idxval, newelt);
2261 else if (BOOL_VECTOR_P (array))
2263 if (idxval < 0 || idxval >= bool_vector_size (array))
2264 args_out_of_range (array, idx);
2265 bool_vector_set (array, idxval, !NILP (newelt));
2267 else if (CHAR_TABLE_P (array))
2269 CHECK_CHARACTER (idx);
2270 CHAR_TABLE_SET (array, idxval, newelt);
2272 else
2274 int c;
2276 if (idxval < 0 || idxval >= SCHARS (array))
2277 args_out_of_range (array, idx);
2278 CHECK_CHARACTER (newelt);
2279 c = XFASTINT (newelt);
2281 if (STRING_MULTIBYTE (array))
2283 ptrdiff_t idxval_byte, nbytes;
2284 int prev_bytes, new_bytes;
2285 unsigned char workbuf[MAX_MULTIBYTE_LENGTH], *p0 = workbuf, *p1;
2287 nbytes = SBYTES (array);
2288 idxval_byte = string_char_to_byte (array, idxval);
2289 p1 = SDATA (array) + idxval_byte;
2290 prev_bytes = BYTES_BY_CHAR_HEAD (*p1);
2291 new_bytes = CHAR_STRING (c, p0);
2292 if (prev_bytes != new_bytes)
2294 /* We must relocate the string data. */
2295 ptrdiff_t nchars = SCHARS (array);
2296 USE_SAFE_ALLOCA;
2297 unsigned char *str = SAFE_ALLOCA (nbytes);
2299 memcpy (str, SDATA (array), nbytes);
2300 allocate_string_data (XSTRING (array), nchars,
2301 nbytes + new_bytes - prev_bytes);
2302 memcpy (SDATA (array), str, idxval_byte);
2303 p1 = SDATA (array) + idxval_byte;
2304 memcpy (p1 + new_bytes, str + idxval_byte + prev_bytes,
2305 nbytes - (idxval_byte + prev_bytes));
2306 SAFE_FREE ();
2307 clear_string_char_byte_cache ();
2309 while (new_bytes--)
2310 *p1++ = *p0++;
2312 else
2314 if (! SINGLE_BYTE_CHAR_P (c))
2316 ptrdiff_t i;
2318 for (i = SBYTES (array) - 1; i >= 0; i--)
2319 if (SREF (array, i) >= 0x80)
2320 args_out_of_range (array, newelt);
2321 /* ARRAY is an ASCII string. Convert it to a multibyte
2322 string, and try `aset' again. */
2323 STRING_SET_MULTIBYTE (array);
2324 return Faset (array, idx, newelt);
2326 SSET (array, idxval, c);
2330 return newelt;
2333 /* Arithmetic functions */
2335 Lisp_Object
2336 arithcompare (Lisp_Object num1, Lisp_Object num2, enum Arith_Comparison comparison)
2338 double f1 = 0, f2 = 0;
2339 bool floatp = 0;
2341 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num1);
2342 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num2);
2344 if (FLOATP (num1) || FLOATP (num2))
2346 floatp = 1;
2347 f1 = (FLOATP (num1)) ? XFLOAT_DATA (num1) : XINT (num1);
2348 f2 = (FLOATP (num2)) ? XFLOAT_DATA (num2) : XINT (num2);
2351 switch (comparison)
2353 case ARITH_EQUAL:
2354 if (floatp ? f1 == f2 : XINT (num1) == XINT (num2))
2355 return Qt;
2356 return Qnil;
2358 case ARITH_NOTEQUAL:
2359 if (floatp ? f1 != f2 : XINT (num1) != XINT (num2))
2360 return Qt;
2361 return Qnil;
2363 case ARITH_LESS:
2364 if (floatp ? f1 < f2 : XINT (num1) < XINT (num2))
2365 return Qt;
2366 return Qnil;
2368 case ARITH_LESS_OR_EQUAL:
2369 if (floatp ? f1 <= f2 : XINT (num1) <= XINT (num2))
2370 return Qt;
2371 return Qnil;
2373 case ARITH_GRTR:
2374 if (floatp ? f1 > f2 : XINT (num1) > XINT (num2))
2375 return Qt;
2376 return Qnil;
2378 case ARITH_GRTR_OR_EQUAL:
2379 if (floatp ? f1 >= f2 : XINT (num1) >= XINT (num2))
2380 return Qt;
2381 return Qnil;
2383 default:
2384 emacs_abort ();
2388 static Lisp_Object
2389 arithcompare_driver (ptrdiff_t nargs, Lisp_Object *args,
2390 enum Arith_Comparison comparison)
2392 ptrdiff_t argnum;
2393 for (argnum = 1; argnum < nargs; ++argnum)
2395 if (EQ (Qnil, arithcompare (args[argnum - 1], args[argnum], comparison)))
2396 return Qnil;
2398 return Qt;
2401 DEFUN ("=", Feqlsign, Seqlsign, 1, MANY, 0,
2402 doc: /* Return t if args, all numbers or markers, are equal.
2403 usage: (= NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2404 (ptrdiff_t nargs, Lisp_Object *args)
2406 return arithcompare_driver (nargs, args, ARITH_EQUAL);
2409 DEFUN ("<", Flss, Slss, 1, MANY, 0,
2410 doc: /* Return t if each arg (a number or marker), is less than the next arg.
2411 usage: (< NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2412 (ptrdiff_t nargs, Lisp_Object *args)
2414 return arithcompare_driver (nargs, args, ARITH_LESS);
2417 DEFUN (">", Fgtr, Sgtr, 1, MANY, 0,
2418 doc: /* Return t if each arg (a number or marker) is greater than the next arg.
2419 usage: (> NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2420 (ptrdiff_t nargs, Lisp_Object *args)
2422 return arithcompare_driver (nargs, args, ARITH_GRTR);
2425 DEFUN ("<=", Fleq, Sleq, 1, MANY, 0,
2426 doc: /* Return t if each arg (a number or marker) is less than or equal to the next.
2427 usage: (<= NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2428 (ptrdiff_t nargs, Lisp_Object *args)
2430 return arithcompare_driver (nargs, args, ARITH_LESS_OR_EQUAL);
2433 DEFUN (">=", Fgeq, Sgeq, 1, MANY, 0,
2434 doc: /* Return t if each arg (a number or marker) is greater than or equal to the next.
2435 usage: (>= NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2436 (ptrdiff_t nargs, Lisp_Object *args)
2438 return arithcompare_driver (nargs, args, ARITH_GRTR_OR_EQUAL);
2441 DEFUN ("/=", Fneq, Sneq, 2, 2, 0,
2442 doc: /* Return t if first arg is not equal to second arg. Both must be numbers or markers. */)
2443 (register Lisp_Object num1, Lisp_Object num2)
2445 return arithcompare (num1, num2, ARITH_NOTEQUAL);
2448 /* Convert the cons-of-integers, integer, or float value C to an
2449 unsigned value with maximum value MAX. Signal an error if C does not
2450 have a valid format or is out of range. */
2451 uintmax_t
2452 cons_to_unsigned (Lisp_Object c, uintmax_t max)
2454 bool valid = 0;
2455 uintmax_t val IF_LINT (= 0);
2456 if (INTEGERP (c))
2458 valid = 0 <= XINT (c);
2459 val = XINT (c);
2461 else if (FLOATP (c))
2463 double d = XFLOAT_DATA (c);
2464 if (0 <= d
2465 && d < (max == UINTMAX_MAX ? (double) UINTMAX_MAX + 1 : max + 1))
2467 val = d;
2468 valid = 1;
2471 else if (CONSP (c) && NATNUMP (XCAR (c)))
2473 uintmax_t top = XFASTINT (XCAR (c));
2474 Lisp_Object rest = XCDR (c);
2475 if (top <= UINTMAX_MAX >> 24 >> 16
2476 && CONSP (rest)
2477 && NATNUMP (XCAR (rest)) && XFASTINT (XCAR (rest)) < 1 << 24
2478 && NATNUMP (XCDR (rest)) && XFASTINT (XCDR (rest)) < 1 << 16)
2480 uintmax_t mid = XFASTINT (XCAR (rest));
2481 val = top << 24 << 16 | mid << 16 | XFASTINT (XCDR (rest));
2482 valid = 1;
2484 else if (top <= UINTMAX_MAX >> 16)
2486 if (CONSP (rest))
2487 rest = XCAR (rest);
2488 if (NATNUMP (rest) && XFASTINT (rest) < 1 << 16)
2490 val = top << 16 | XFASTINT (rest);
2491 valid = 1;
2496 if (! (valid && val <= max))
2497 error ("Not an in-range integer, float, or cons of integers");
2498 return val;
2501 /* Convert the cons-of-integers, integer, or float value C to a signed
2502 value with extrema MIN and MAX. Signal an error if C does not have
2503 a valid format or is out of range. */
2504 intmax_t
2505 cons_to_signed (Lisp_Object c, intmax_t min, intmax_t max)
2507 bool valid = 0;
2508 intmax_t val IF_LINT (= 0);
2509 if (INTEGERP (c))
2511 val = XINT (c);
2512 valid = 1;
2514 else if (FLOATP (c))
2516 double d = XFLOAT_DATA (c);
2517 if (min <= d
2518 && d < (max == INTMAX_MAX ? (double) INTMAX_MAX + 1 : max + 1))
2520 val = d;
2521 valid = 1;
2524 else if (CONSP (c) && INTEGERP (XCAR (c)))
2526 intmax_t top = XINT (XCAR (c));
2527 Lisp_Object rest = XCDR (c);
2528 if (INTMAX_MIN >> 24 >> 16 <= top && top <= INTMAX_MAX >> 24 >> 16
2529 && CONSP (rest)
2530 && NATNUMP (XCAR (rest)) && XFASTINT (XCAR (rest)) < 1 << 24
2531 && NATNUMP (XCDR (rest)) && XFASTINT (XCDR (rest)) < 1 << 16)
2533 intmax_t mid = XFASTINT (XCAR (rest));
2534 val = top << 24 << 16 | mid << 16 | XFASTINT (XCDR (rest));
2535 valid = 1;
2537 else if (INTMAX_MIN >> 16 <= top && top <= INTMAX_MAX >> 16)
2539 if (CONSP (rest))
2540 rest = XCAR (rest);
2541 if (NATNUMP (rest) && XFASTINT (rest) < 1 << 16)
2543 val = top << 16 | XFASTINT (rest);
2544 valid = 1;
2549 if (! (valid && min <= val && val <= max))
2550 error ("Not an in-range integer, float, or cons of integers");
2551 return val;
2554 DEFUN ("number-to-string", Fnumber_to_string, Snumber_to_string, 1, 1, 0,
2555 doc: /* Return the decimal representation of NUMBER as a string.
2556 Uses a minus sign if negative.
2557 NUMBER may be an integer or a floating point number. */)
2558 (Lisp_Object number)
2560 char buffer[max (FLOAT_TO_STRING_BUFSIZE, INT_BUFSIZE_BOUND (EMACS_INT))];
2561 int len;
2563 CHECK_NUMBER_OR_FLOAT (number);
2565 if (FLOATP (number))
2566 len = float_to_string (buffer, XFLOAT_DATA (number));
2567 else
2568 len = sprintf (buffer, "%"pI"d", XINT (number));
2570 return make_unibyte_string (buffer, len);
2573 DEFUN ("string-to-number", Fstring_to_number, Sstring_to_number, 1, 2, 0,
2574 doc: /* Parse STRING as a decimal number and return the number.
2575 Ignore leading spaces and tabs, and all trailing chars. Return 0 if
2576 STRING cannot be parsed as an integer or floating point number.
2578 If BASE, interpret STRING as a number in that base. If BASE isn't
2579 present, base 10 is used. BASE must be between 2 and 16 (inclusive).
2580 If the base used is not 10, STRING is always parsed as an integer. */)
2581 (register Lisp_Object string, Lisp_Object base)
2583 register char *p;
2584 register int b;
2585 Lisp_Object val;
2587 CHECK_STRING (string);
2589 if (NILP (base))
2590 b = 10;
2591 else
2593 CHECK_NUMBER (base);
2594 if (! (2 <= XINT (base) && XINT (base) <= 16))
2595 xsignal1 (Qargs_out_of_range, base);
2596 b = XINT (base);
2599 p = SSDATA (string);
2600 while (*p == ' ' || *p == '\t')
2601 p++;
2603 val = string_to_number (p, b, 1);
2604 return NILP (val) ? make_number (0) : val;
2607 enum arithop
2609 Aadd,
2610 Asub,
2611 Amult,
2612 Adiv,
2613 Alogand,
2614 Alogior,
2615 Alogxor,
2616 Amax,
2617 Amin
2620 static Lisp_Object float_arith_driver (double, ptrdiff_t, enum arithop,
2621 ptrdiff_t, Lisp_Object *);
2622 static Lisp_Object
2623 arith_driver (enum arithop code, ptrdiff_t nargs, Lisp_Object *args)
2625 Lisp_Object val;
2626 ptrdiff_t argnum, ok_args;
2627 EMACS_INT accum = 0;
2628 EMACS_INT next, ok_accum;
2629 bool overflow = 0;
2631 switch (code)
2633 case Alogior:
2634 case Alogxor:
2635 case Aadd:
2636 case Asub:
2637 accum = 0;
2638 break;
2639 case Amult:
2640 accum = 1;
2641 break;
2642 case Alogand:
2643 accum = -1;
2644 break;
2645 default:
2646 break;
2649 for (argnum = 0; argnum < nargs; argnum++)
2651 if (! overflow)
2653 ok_args = argnum;
2654 ok_accum = accum;
2657 /* Using args[argnum] as argument to CHECK_NUMBER_... */
2658 val = args[argnum];
2659 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val);
2661 if (FLOATP (val))
2662 return float_arith_driver (ok_accum, ok_args, code,
2663 nargs, args);
2664 args[argnum] = val;
2665 next = XINT (args[argnum]);
2666 switch (code)
2668 case Aadd:
2669 if (INT_ADD_OVERFLOW (accum, next))
2671 overflow = 1;
2672 accum &= INTMASK;
2674 accum += next;
2675 break;
2676 case Asub:
2677 if (INT_SUBTRACT_OVERFLOW (accum, next))
2679 overflow = 1;
2680 accum &= INTMASK;
2682 accum = argnum ? accum - next : nargs == 1 ? - next : next;
2683 break;
2684 case Amult:
2685 if (INT_MULTIPLY_OVERFLOW (accum, next))
2687 EMACS_UINT a = accum, b = next, ab = a * b;
2688 overflow = 1;
2689 accum = ab & INTMASK;
2691 else
2692 accum *= next;
2693 break;
2694 case Adiv:
2695 if (!argnum)
2696 accum = next;
2697 else
2699 if (next == 0)
2700 xsignal0 (Qarith_error);
2701 accum /= next;
2703 break;
2704 case Alogand:
2705 accum &= next;
2706 break;
2707 case Alogior:
2708 accum |= next;
2709 break;
2710 case Alogxor:
2711 accum ^= next;
2712 break;
2713 case Amax:
2714 if (!argnum || next > accum)
2715 accum = next;
2716 break;
2717 case Amin:
2718 if (!argnum || next < accum)
2719 accum = next;
2720 break;
2724 XSETINT (val, accum);
2725 return val;
2728 #undef isnan
2729 #define isnan(x) ((x) != (x))
2731 static Lisp_Object
2732 float_arith_driver (double accum, ptrdiff_t argnum, enum arithop code,
2733 ptrdiff_t nargs, Lisp_Object *args)
2735 register Lisp_Object val;
2736 double next;
2738 for (; argnum < nargs; argnum++)
2740 val = args[argnum]; /* using args[argnum] as argument to CHECK_NUMBER_... */
2741 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val);
2743 if (FLOATP (val))
2745 next = XFLOAT_DATA (val);
2747 else
2749 args[argnum] = val; /* runs into a compiler bug. */
2750 next = XINT (args[argnum]);
2752 switch (code)
2754 case Aadd:
2755 accum += next;
2756 break;
2757 case Asub:
2758 accum = argnum ? accum - next : nargs == 1 ? - next : next;
2759 break;
2760 case Amult:
2761 accum *= next;
2762 break;
2763 case Adiv:
2764 if (!argnum)
2765 accum = next;
2766 else
2768 if (! IEEE_FLOATING_POINT && next == 0)
2769 xsignal0 (Qarith_error);
2770 accum /= next;
2772 break;
2773 case Alogand:
2774 case Alogior:
2775 case Alogxor:
2776 return wrong_type_argument (Qinteger_or_marker_p, val);
2777 case Amax:
2778 if (!argnum || isnan (next) || next > accum)
2779 accum = next;
2780 break;
2781 case Amin:
2782 if (!argnum || isnan (next) || next < accum)
2783 accum = next;
2784 break;
2788 return make_float (accum);
2792 DEFUN ("+", Fplus, Splus, 0, MANY, 0,
2793 doc: /* Return sum of any number of arguments, which are numbers or markers.
2794 usage: (+ &rest NUMBERS-OR-MARKERS) */)
2795 (ptrdiff_t nargs, Lisp_Object *args)
2797 return arith_driver (Aadd, nargs, args);
2800 DEFUN ("-", Fminus, Sminus, 0, MANY, 0,
2801 doc: /* Negate number or subtract numbers or markers and return the result.
2802 With one arg, negates it. With more than one arg,
2803 subtracts all but the first from the first.
2804 usage: (- &optional NUMBER-OR-MARKER &rest MORE-NUMBERS-OR-MARKERS) */)
2805 (ptrdiff_t nargs, Lisp_Object *args)
2807 return arith_driver (Asub, nargs, args);
2810 DEFUN ("*", Ftimes, Stimes, 0, MANY, 0,
2811 doc: /* Return product of any number of arguments, which are numbers or markers.
2812 usage: (* &rest NUMBERS-OR-MARKERS) */)
2813 (ptrdiff_t nargs, Lisp_Object *args)
2815 return arith_driver (Amult, nargs, args);
2818 DEFUN ("/", Fquo, Squo, 1, MANY, 0,
2819 doc: /* Return first argument divided by all the remaining arguments.
2820 The arguments must be numbers or markers.
2821 usage: (/ DIVIDEND &rest DIVISORS) */)
2822 (ptrdiff_t nargs, Lisp_Object *args)
2824 ptrdiff_t argnum;
2825 for (argnum = 2; argnum < nargs; argnum++)
2826 if (FLOATP (args[argnum]))
2827 return float_arith_driver (0, 0, Adiv, nargs, args);
2828 return arith_driver (Adiv, nargs, args);
2831 DEFUN ("%", Frem, Srem, 2, 2, 0,
2832 doc: /* Return remainder of X divided by Y.
2833 Both must be integers or markers. */)
2834 (register Lisp_Object x, Lisp_Object y)
2836 Lisp_Object val;
2838 CHECK_NUMBER_COERCE_MARKER (x);
2839 CHECK_NUMBER_COERCE_MARKER (y);
2841 if (XINT (y) == 0)
2842 xsignal0 (Qarith_error);
2844 XSETINT (val, XINT (x) % XINT (y));
2845 return val;
2848 DEFUN ("mod", Fmod, Smod, 2, 2, 0,
2849 doc: /* Return X modulo Y.
2850 The result falls between zero (inclusive) and Y (exclusive).
2851 Both X and Y must be numbers or markers. */)
2852 (register Lisp_Object x, Lisp_Object y)
2854 Lisp_Object val;
2855 EMACS_INT i1, i2;
2857 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (x);
2858 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (y);
2860 if (FLOATP (x) || FLOATP (y))
2861 return fmod_float (x, y);
2863 i1 = XINT (x);
2864 i2 = XINT (y);
2866 if (i2 == 0)
2867 xsignal0 (Qarith_error);
2869 i1 %= i2;
2871 /* If the "remainder" comes out with the wrong sign, fix it. */
2872 if (i2 < 0 ? i1 > 0 : i1 < 0)
2873 i1 += i2;
2875 XSETINT (val, i1);
2876 return val;
2879 DEFUN ("max", Fmax, Smax, 1, MANY, 0,
2880 doc: /* Return largest of all the arguments (which must be numbers or markers).
2881 The value is always a number; markers are converted to numbers.
2882 usage: (max NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2883 (ptrdiff_t nargs, Lisp_Object *args)
2885 return arith_driver (Amax, nargs, args);
2888 DEFUN ("min", Fmin, Smin, 1, MANY, 0,
2889 doc: /* Return smallest of all the arguments (which must be numbers or markers).
2890 The value is always a number; markers are converted to numbers.
2891 usage: (min NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2892 (ptrdiff_t nargs, Lisp_Object *args)
2894 return arith_driver (Amin, nargs, args);
2897 DEFUN ("logand", Flogand, Slogand, 0, MANY, 0,
2898 doc: /* Return bitwise-and of all the arguments.
2899 Arguments may be integers, or markers converted to integers.
2900 usage: (logand &rest INTS-OR-MARKERS) */)
2901 (ptrdiff_t nargs, Lisp_Object *args)
2903 return arith_driver (Alogand, nargs, args);
2906 DEFUN ("logior", Flogior, Slogior, 0, MANY, 0,
2907 doc: /* Return bitwise-or of all the arguments.
2908 Arguments may be integers, or markers converted to integers.
2909 usage: (logior &rest INTS-OR-MARKERS) */)
2910 (ptrdiff_t nargs, Lisp_Object *args)
2912 return arith_driver (Alogior, nargs, args);
2915 DEFUN ("logxor", Flogxor, Slogxor, 0, MANY, 0,
2916 doc: /* Return bitwise-exclusive-or of all the arguments.
2917 Arguments may be integers, or markers converted to integers.
2918 usage: (logxor &rest INTS-OR-MARKERS) */)
2919 (ptrdiff_t nargs, Lisp_Object *args)
2921 return arith_driver (Alogxor, nargs, args);
2924 DEFUN ("ash", Fash, Sash, 2, 2, 0,
2925 doc: /* Return VALUE with its bits shifted left by COUNT.
2926 If COUNT is negative, shifting is actually to the right.
2927 In this case, the sign bit is duplicated. */)
2928 (register Lisp_Object value, Lisp_Object count)
2930 register Lisp_Object val;
2932 CHECK_NUMBER (value);
2933 CHECK_NUMBER (count);
2935 if (XINT (count) >= BITS_PER_EMACS_INT)
2936 XSETINT (val, 0);
2937 else if (XINT (count) > 0)
2938 XSETINT (val, XUINT (value) << XFASTINT (count));
2939 else if (XINT (count) <= -BITS_PER_EMACS_INT)
2940 XSETINT (val, XINT (value) < 0 ? -1 : 0);
2941 else
2942 XSETINT (val, XINT (value) >> -XINT (count));
2943 return val;
2946 DEFUN ("lsh", Flsh, Slsh, 2, 2, 0,
2947 doc: /* Return VALUE with its bits shifted left by COUNT.
2948 If COUNT is negative, shifting is actually to the right.
2949 In this case, zeros are shifted in on the left. */)
2950 (register Lisp_Object value, Lisp_Object count)
2952 register Lisp_Object val;
2954 CHECK_NUMBER (value);
2955 CHECK_NUMBER (count);
2957 if (XINT (count) >= BITS_PER_EMACS_INT)
2958 XSETINT (val, 0);
2959 else if (XINT (count) > 0)
2960 XSETINT (val, XUINT (value) << XFASTINT (count));
2961 else if (XINT (count) <= -BITS_PER_EMACS_INT)
2962 XSETINT (val, 0);
2963 else
2964 XSETINT (val, XUINT (value) >> -XINT (count));
2965 return val;
2968 DEFUN ("1+", Fadd1, Sadd1, 1, 1, 0,
2969 doc: /* Return NUMBER plus one. NUMBER may be a number or a marker.
2970 Markers are converted to integers. */)
2971 (register Lisp_Object number)
2973 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number);
2975 if (FLOATP (number))
2976 return (make_float (1.0 + XFLOAT_DATA (number)));
2978 XSETINT (number, XINT (number) + 1);
2979 return number;
2982 DEFUN ("1-", Fsub1, Ssub1, 1, 1, 0,
2983 doc: /* Return NUMBER minus one. NUMBER may be a number or a marker.
2984 Markers are converted to integers. */)
2985 (register Lisp_Object number)
2987 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number);
2989 if (FLOATP (number))
2990 return (make_float (-1.0 + XFLOAT_DATA (number)));
2992 XSETINT (number, XINT (number) - 1);
2993 return number;
2996 DEFUN ("lognot", Flognot, Slognot, 1, 1, 0,
2997 doc: /* Return the bitwise complement of NUMBER. NUMBER must be an integer. */)
2998 (register Lisp_Object number)
3000 CHECK_NUMBER (number);
3001 XSETINT (number, ~XINT (number));
3002 return number;
3005 DEFUN ("byteorder", Fbyteorder, Sbyteorder, 0, 0, 0,
3006 doc: /* Return the byteorder for the machine.
3007 Returns 66 (ASCII uppercase B) for big endian machines or 108 (ASCII
3008 lowercase l) for small endian machines. */)
3009 (void)
3011 unsigned i = 0x04030201;
3012 int order = *(char *)&i == 1 ? 108 : 66;
3014 return make_number (order);
3017 /* Because we round up the bool vector allocate size to word_size
3018 units, we can safely read past the "end" of the vector in the
3019 operations below. These extra bits are always zero. */
3021 static bits_word
3022 bool_vector_spare_mask (EMACS_INT nr_bits)
3024 return (((bits_word) 1) << (nr_bits % BITS_PER_BITS_WORD)) - 1;
3027 /* Info about unsigned long long, falling back on unsigned long
3028 if unsigned long long is not available. */
3030 #if HAVE_UNSIGNED_LONG_LONG_INT && defined ULLONG_MAX
3031 enum { BITS_PER_ULL = CHAR_BIT * sizeof (unsigned long long) };
3032 # define ULL_MAX ULLONG_MAX
3033 #else
3034 enum { BITS_PER_ULL = CHAR_BIT * sizeof (unsigned long) };
3035 # define ULL_MAX ULONG_MAX
3036 # define count_one_bits_ll count_one_bits_l
3037 # define count_trailing_zeros_ll count_trailing_zeros_l
3038 #endif
3040 /* Shift VAL right by the width of an unsigned long long.
3041 BITS_PER_ULL must be less than BITS_PER_BITS_WORD. */
3043 static bits_word
3044 shift_right_ull (bits_word w)
3046 /* Pacify bogus GCC warning about shift count exceeding type width. */
3047 int shift = BITS_PER_ULL - BITS_PER_BITS_WORD < 0 ? BITS_PER_ULL : 0;
3048 return w >> shift;
3051 /* Return the number of 1 bits in W. */
3053 static int
3054 count_one_bits_word (bits_word w)
3056 if (BITS_WORD_MAX <= UINT_MAX)
3057 return count_one_bits (w);
3058 else if (BITS_WORD_MAX <= ULONG_MAX)
3059 return count_one_bits_l (w);
3060 else
3062 int i = 0, count = 0;
3063 while (count += count_one_bits_ll (w),
3064 (i += BITS_PER_ULL) < BITS_PER_BITS_WORD)
3065 w = shift_right_ull (w);
3066 return count;
3070 enum bool_vector_op { bool_vector_exclusive_or,
3071 bool_vector_union,
3072 bool_vector_intersection,
3073 bool_vector_set_difference,
3074 bool_vector_subsetp };
3076 static Lisp_Object
3077 bool_vector_binop_driver (Lisp_Object a,
3078 Lisp_Object b,
3079 Lisp_Object dest,
3080 enum bool_vector_op op)
3082 EMACS_INT nr_bits;
3083 bits_word *adata, *bdata, *destdata;
3084 ptrdiff_t i = 0;
3085 ptrdiff_t nr_words;
3087 CHECK_BOOL_VECTOR (a);
3088 CHECK_BOOL_VECTOR (b);
3090 nr_bits = bool_vector_size (a);
3091 if (bool_vector_size (b) != nr_bits)
3092 wrong_length_argument (a, b, dest);
3094 nr_words = bool_vector_words (nr_bits);
3095 adata = bool_vector_data (a);
3096 bdata = bool_vector_data (b);
3098 if (NILP (dest))
3100 dest = make_uninit_bool_vector (nr_bits);
3101 destdata = bool_vector_data (dest);
3103 else
3105 CHECK_BOOL_VECTOR (dest);
3106 destdata = bool_vector_data (dest);
3107 if (bool_vector_size (dest) != nr_bits)
3108 wrong_length_argument (a, b, dest);
3110 switch (op)
3112 case bool_vector_exclusive_or:
3113 for (; i < nr_words; i++)
3114 if (destdata[i] != (adata[i] ^ bdata[i]))
3115 goto set_dest;
3116 break;
3118 case bool_vector_subsetp:
3119 for (; i < nr_words; i++)
3120 if (adata[i] &~ bdata[i])
3121 return Qnil;
3122 return Qt;
3124 case bool_vector_union:
3125 for (; i < nr_words; i++)
3126 if (destdata[i] != (adata[i] | bdata[i]))
3127 goto set_dest;
3128 break;
3130 case bool_vector_intersection:
3131 for (; i < nr_words; i++)
3132 if (destdata[i] != (adata[i] & bdata[i]))
3133 goto set_dest;
3134 break;
3136 case bool_vector_set_difference:
3137 for (; i < nr_words; i++)
3138 if (destdata[i] != (adata[i] &~ bdata[i]))
3139 goto set_dest;
3140 break;
3143 return Qnil;
3146 set_dest:
3147 switch (op)
3149 case bool_vector_exclusive_or:
3150 for (; i < nr_words; i++)
3151 destdata[i] = adata[i] ^ bdata[i];
3152 break;
3154 case bool_vector_union:
3155 for (; i < nr_words; i++)
3156 destdata[i] = adata[i] | bdata[i];
3157 break;
3159 case bool_vector_intersection:
3160 for (; i < nr_words; i++)
3161 destdata[i] = adata[i] & bdata[i];
3162 break;
3164 case bool_vector_set_difference:
3165 for (; i < nr_words; i++)
3166 destdata[i] = adata[i] &~ bdata[i];
3167 break;
3169 default:
3170 eassume (0);
3173 return dest;
3176 /* PRECONDITION must be true. Return VALUE. This odd construction
3177 works around a bogus GCC diagnostic "shift count >= width of type". */
3179 static int
3180 pre_value (bool precondition, int value)
3182 eassume (precondition);
3183 return precondition ? value : 0;
3186 /* Compute the number of trailing zero bits in val. If val is zero,
3187 return the number of bits in val. */
3188 static int
3189 count_trailing_zero_bits (bits_word val)
3191 if (BITS_WORD_MAX == UINT_MAX)
3192 return count_trailing_zeros (val);
3193 if (BITS_WORD_MAX == ULONG_MAX)
3194 return count_trailing_zeros_l (val);
3195 if (BITS_WORD_MAX == ULL_MAX)
3196 return count_trailing_zeros_ll (val);
3198 /* The rest of this code is for the unlikely platform where bits_word differs
3199 in width from unsigned int, unsigned long, and unsigned long long. */
3200 val |= ~ BITS_WORD_MAX;
3201 if (BITS_WORD_MAX <= UINT_MAX)
3202 return count_trailing_zeros (val);
3203 if (BITS_WORD_MAX <= ULONG_MAX)
3204 return count_trailing_zeros_l (val);
3205 else
3207 int count;
3208 for (count = 0;
3209 count < BITS_PER_BITS_WORD - BITS_PER_ULL;
3210 count += BITS_PER_ULL)
3212 if (val & ULL_MAX)
3213 return count + count_trailing_zeros_ll (val);
3214 val = shift_right_ull (val);
3217 if (BITS_PER_BITS_WORD % BITS_PER_ULL != 0
3218 && BITS_WORD_MAX == (bits_word) -1)
3219 val |= (bits_word) 1 << pre_value (ULONG_MAX < BITS_WORD_MAX,
3220 BITS_PER_BITS_WORD % BITS_PER_ULL);
3221 return count + count_trailing_zeros_ll (val);
3225 static bits_word
3226 bits_word_to_host_endian (bits_word val)
3228 #ifndef WORDS_BIGENDIAN
3229 return val;
3230 #else
3231 if (BITS_WORD_MAX >> 31 == 1)
3232 return bswap_32 (val);
3233 # if HAVE_UNSIGNED_LONG_LONG
3234 if (BITS_WORD_MAX >> 31 >> 31 >> 1 == 1)
3235 return bswap_64 (val);
3236 # endif
3238 int i;
3239 bits_word r = 0;
3240 for (i = 0; i < sizeof val; i++)
3242 r = ((r << 1 << (CHAR_BIT - 1))
3243 | (val & ((1u << 1 << (CHAR_BIT - 1)) - 1)));
3244 val = val >> 1 >> (CHAR_BIT - 1);
3246 return r;
3248 #endif
3251 DEFUN ("bool-vector-exclusive-or", Fbool_vector_exclusive_or,
3252 Sbool_vector_exclusive_or, 2, 3, 0,
3253 doc: /* Return A ^ B, bitwise exclusive or.
3254 If optional third argument C is given, store result into C.
3255 A, B, and C must be bool vectors of the same length.
3256 Return the destination vector if it changed or nil otherwise. */)
3257 (Lisp_Object a, Lisp_Object b, Lisp_Object c)
3259 return bool_vector_binop_driver (a, b, c, bool_vector_exclusive_or);
3262 DEFUN ("bool-vector-union", Fbool_vector_union,
3263 Sbool_vector_union, 2, 3, 0,
3264 doc: /* Return A | B, bitwise or.
3265 If optional third argument C is given, store result into C.
3266 A, B, and C must be bool vectors of the same length.
3267 Return the destination vector if it changed or nil otherwise. */)
3268 (Lisp_Object a, Lisp_Object b, Lisp_Object c)
3270 return bool_vector_binop_driver (a, b, c, bool_vector_union);
3273 DEFUN ("bool-vector-intersection", Fbool_vector_intersection,
3274 Sbool_vector_intersection, 2, 3, 0,
3275 doc: /* Return A & B, bitwise and.
3276 If optional third argument C is given, store result into C.
3277 A, B, and C must be bool vectors of the same length.
3278 Return the destination vector if it changed or nil otherwise. */)
3279 (Lisp_Object a, Lisp_Object b, Lisp_Object c)
3281 return bool_vector_binop_driver (a, b, c, bool_vector_intersection);
3284 DEFUN ("bool-vector-set-difference", Fbool_vector_set_difference,
3285 Sbool_vector_set_difference, 2, 3, 0,
3286 doc: /* Return A &~ B, set difference.
3287 If optional third argument C is given, store result into C.
3288 A, B, and C must be bool vectors of the same length.
3289 Return the destination vector if it changed or nil otherwise. */)
3290 (Lisp_Object a, Lisp_Object b, Lisp_Object c)
3292 return bool_vector_binop_driver (a, b, c, bool_vector_set_difference);
3295 DEFUN ("bool-vector-subsetp", Fbool_vector_subsetp,
3296 Sbool_vector_subsetp, 2, 2, 0,
3297 doc: /* Return t if every t value in A is also t in B, nil otherwise.
3298 A and B must be bool vectors of the same length. */)
3299 (Lisp_Object a, Lisp_Object b)
3301 return bool_vector_binop_driver (a, b, b, bool_vector_subsetp);
3304 DEFUN ("bool-vector-not", Fbool_vector_not,
3305 Sbool_vector_not, 1, 2, 0,
3306 doc: /* Compute ~A, set complement.
3307 If optional second argument B is given, store result into B.
3308 A and B must be bool vectors of the same length.
3309 Return the destination vector. */)
3310 (Lisp_Object a, Lisp_Object b)
3312 EMACS_INT nr_bits;
3313 bits_word *bdata, *adata;
3314 ptrdiff_t i;
3316 CHECK_BOOL_VECTOR (a);
3317 nr_bits = bool_vector_size (a);
3319 if (NILP (b))
3320 b = make_uninit_bool_vector (nr_bits);
3321 else
3323 CHECK_BOOL_VECTOR (b);
3324 if (bool_vector_size (b) != nr_bits)
3325 wrong_length_argument (a, b, Qnil);
3328 bdata = bool_vector_data (b);
3329 adata = bool_vector_data (a);
3331 for (i = 0; i < nr_bits / BITS_PER_BITS_WORD; i++)
3332 bdata[i] = BITS_WORD_MAX & ~adata[i];
3334 if (nr_bits % BITS_PER_BITS_WORD)
3336 bits_word mword = bits_word_to_host_endian (adata[i]);
3337 mword = ~mword;
3338 mword &= bool_vector_spare_mask (nr_bits);
3339 bdata[i] = bits_word_to_host_endian (mword);
3342 return b;
3345 DEFUN ("bool-vector-count-population", Fbool_vector_count_population,
3346 Sbool_vector_count_population, 1, 1, 0,
3347 doc: /* Count how many elements in A are t.
3348 A is a bool vector. To count A's nil elements, subtract the return
3349 value from A's length. */)
3350 (Lisp_Object a)
3352 EMACS_INT count;
3353 EMACS_INT nr_bits;
3354 bits_word *adata;
3355 ptrdiff_t i, nwords;
3357 CHECK_BOOL_VECTOR (a);
3359 nr_bits = bool_vector_size (a);
3360 nwords = bool_vector_words (nr_bits);
3361 count = 0;
3362 adata = bool_vector_data (a);
3364 for (i = 0; i < nwords; i++)
3365 count += count_one_bits_word (adata[i]);
3367 return make_number (count);
3370 DEFUN ("bool-vector-count-consecutive", Fbool_vector_count_consecutive,
3371 Sbool_vector_count_consecutive, 3, 3, 0,
3372 doc: /* Count how many consecutive elements in A equal B starting at I.
3373 A is a bool vector, B is t or nil, and I is an index into A. */)
3374 (Lisp_Object a, Lisp_Object b, Lisp_Object i)
3376 EMACS_INT count;
3377 EMACS_INT nr_bits;
3378 int offset;
3379 bits_word *adata;
3380 bits_word twiddle;
3381 bits_word mword; /* Machine word. */
3382 ptrdiff_t pos, pos0;
3383 ptrdiff_t nr_words;
3385 CHECK_BOOL_VECTOR (a);
3386 CHECK_NATNUM (i);
3388 nr_bits = bool_vector_size (a);
3389 if (XFASTINT (i) > nr_bits) /* Allow one past the end for convenience */
3390 args_out_of_range (a, i);
3392 adata = bool_vector_data (a);
3393 nr_words = bool_vector_words (nr_bits);
3394 pos = XFASTINT (i) / BITS_PER_BITS_WORD;
3395 offset = XFASTINT (i) % BITS_PER_BITS_WORD;
3396 count = 0;
3398 /* By XORing with twiddle, we transform the problem of "count
3399 consecutive equal values" into "count the zero bits". The latter
3400 operation usually has hardware support. */
3401 twiddle = NILP (b) ? 0 : BITS_WORD_MAX;
3403 /* Scan the remainder of the mword at the current offset. */
3404 if (pos < nr_words && offset != 0)
3406 mword = bits_word_to_host_endian (adata[pos]);
3407 mword ^= twiddle;
3408 mword >>= offset;
3410 /* Do not count the pad bits. */
3411 mword |= (bits_word) 1 << (BITS_PER_BITS_WORD - offset);
3413 count = count_trailing_zero_bits (mword);
3414 pos++;
3415 if (count + offset < BITS_PER_BITS_WORD)
3416 return make_number (count);
3419 /* Scan whole words until we either reach the end of the vector or
3420 find an mword that doesn't completely match. twiddle is
3421 endian-independent. */
3422 pos0 = pos;
3423 while (pos < nr_words && adata[pos] == twiddle)
3424 pos++;
3425 count += (pos - pos0) * BITS_PER_BITS_WORD;
3427 if (pos < nr_words)
3429 /* If we stopped because of a mismatch, see how many bits match
3430 in the current mword. */
3431 mword = bits_word_to_host_endian (adata[pos]);
3432 mword ^= twiddle;
3433 count += count_trailing_zero_bits (mword);
3435 else if (nr_bits % BITS_PER_BITS_WORD != 0)
3437 /* If we hit the end, we might have overshot our count. Reduce
3438 the total by the number of spare bits at the end of the
3439 vector. */
3440 count -= BITS_PER_BITS_WORD - nr_bits % BITS_PER_BITS_WORD;
3443 return make_number (count);
3447 void
3448 syms_of_data (void)
3450 Lisp_Object error_tail, arith_tail;
3452 DEFSYM (Qquote, "quote");
3453 DEFSYM (Qlambda, "lambda");
3454 DEFSYM (Qsubr, "subr");
3455 DEFSYM (Qerror_conditions, "error-conditions");
3456 DEFSYM (Qerror_message, "error-message");
3457 DEFSYM (Qtop_level, "top-level");
3459 DEFSYM (Qerror, "error");
3460 DEFSYM (Quser_error, "user-error");
3461 DEFSYM (Qquit, "quit");
3462 DEFSYM (Qwrong_length_argument, "wrong-length-argument");
3463 DEFSYM (Qwrong_type_argument, "wrong-type-argument");
3464 DEFSYM (Qargs_out_of_range, "args-out-of-range");
3465 DEFSYM (Qvoid_function, "void-function");
3466 DEFSYM (Qcyclic_function_indirection, "cyclic-function-indirection");
3467 DEFSYM (Qcyclic_variable_indirection, "cyclic-variable-indirection");
3468 DEFSYM (Qvoid_variable, "void-variable");
3469 DEFSYM (Qsetting_constant, "setting-constant");
3470 DEFSYM (Qinvalid_read_syntax, "invalid-read-syntax");
3472 DEFSYM (Qinvalid_function, "invalid-function");
3473 DEFSYM (Qwrong_number_of_arguments, "wrong-number-of-arguments");
3474 DEFSYM (Qno_catch, "no-catch");
3475 DEFSYM (Qend_of_file, "end-of-file");
3476 DEFSYM (Qarith_error, "arith-error");
3477 DEFSYM (Qbeginning_of_buffer, "beginning-of-buffer");
3478 DEFSYM (Qend_of_buffer, "end-of-buffer");
3479 DEFSYM (Qbuffer_read_only, "buffer-read-only");
3480 DEFSYM (Qtext_read_only, "text-read-only");
3481 DEFSYM (Qmark_inactive, "mark-inactive");
3483 DEFSYM (Qlistp, "listp");
3484 DEFSYM (Qconsp, "consp");
3485 DEFSYM (Qsymbolp, "symbolp");
3486 DEFSYM (Qkeywordp, "keywordp");
3487 DEFSYM (Qintegerp, "integerp");
3488 DEFSYM (Qnatnump, "natnump");
3489 DEFSYM (Qwholenump, "wholenump");
3490 DEFSYM (Qstringp, "stringp");
3491 DEFSYM (Qarrayp, "arrayp");
3492 DEFSYM (Qsequencep, "sequencep");
3493 DEFSYM (Qbufferp, "bufferp");
3494 DEFSYM (Qvectorp, "vectorp");
3495 DEFSYM (Qbool_vector_p, "bool-vector-p");
3496 DEFSYM (Qchar_or_string_p, "char-or-string-p");
3497 DEFSYM (Qmarkerp, "markerp");
3498 DEFSYM (Qbuffer_or_string_p, "buffer-or-string-p");
3499 DEFSYM (Qinteger_or_marker_p, "integer-or-marker-p");
3500 DEFSYM (Qboundp, "boundp");
3501 DEFSYM (Qfboundp, "fboundp");
3503 DEFSYM (Qfloatp, "floatp");
3504 DEFSYM (Qnumberp, "numberp");
3505 DEFSYM (Qnumber_or_marker_p, "number-or-marker-p");
3507 DEFSYM (Qchar_table_p, "char-table-p");
3508 DEFSYM (Qvector_or_char_table_p, "vector-or-char-table-p");
3510 DEFSYM (Qsubrp, "subrp");
3511 DEFSYM (Qunevalled, "unevalled");
3512 DEFSYM (Qmany, "many");
3514 DEFSYM (Qcdr, "cdr");
3516 /* Handle automatic advice activation. */
3517 DEFSYM (Qad_advice_info, "ad-advice-info");
3518 DEFSYM (Qad_activate_internal, "ad-activate-internal");
3520 error_tail = pure_cons (Qerror, Qnil);
3522 /* ERROR is used as a signaler for random errors for which nothing else is
3523 right. */
3525 Fput (Qerror, Qerror_conditions,
3526 error_tail);
3527 Fput (Qerror, Qerror_message,
3528 build_pure_c_string ("error"));
3530 #define PUT_ERROR(sym, tail, msg) \
3531 Fput (sym, Qerror_conditions, pure_cons (sym, tail)); \
3532 Fput (sym, Qerror_message, build_pure_c_string (msg))
3534 PUT_ERROR (Qquit, Qnil, "Quit");
3536 PUT_ERROR (Quser_error, error_tail, "");
3537 PUT_ERROR (Qwrong_length_argument, error_tail, "Wrong length argument");
3538 PUT_ERROR (Qwrong_type_argument, error_tail, "Wrong type argument");
3539 PUT_ERROR (Qargs_out_of_range, error_tail, "Args out of range");
3540 PUT_ERROR (Qvoid_function, error_tail,
3541 "Symbol's function definition is void");
3542 PUT_ERROR (Qcyclic_function_indirection, error_tail,
3543 "Symbol's chain of function indirections contains a loop");
3544 PUT_ERROR (Qcyclic_variable_indirection, error_tail,
3545 "Symbol's chain of variable indirections contains a loop");
3546 DEFSYM (Qcircular_list, "circular-list");
3547 PUT_ERROR (Qcircular_list, error_tail, "List contains a loop");
3548 PUT_ERROR (Qvoid_variable, error_tail, "Symbol's value as variable is void");
3549 PUT_ERROR (Qsetting_constant, error_tail,
3550 "Attempt to set a constant symbol");
3551 PUT_ERROR (Qinvalid_read_syntax, error_tail, "Invalid read syntax");
3552 PUT_ERROR (Qinvalid_function, error_tail, "Invalid function");
3553 PUT_ERROR (Qwrong_number_of_arguments, error_tail,
3554 "Wrong number of arguments");
3555 PUT_ERROR (Qno_catch, error_tail, "No catch for tag");
3556 PUT_ERROR (Qend_of_file, error_tail, "End of file during parsing");
3558 arith_tail = pure_cons (Qarith_error, error_tail);
3559 Fput (Qarith_error, Qerror_conditions, arith_tail);
3560 Fput (Qarith_error, Qerror_message, build_pure_c_string ("Arithmetic error"));
3562 PUT_ERROR (Qbeginning_of_buffer, error_tail, "Beginning of buffer");
3563 PUT_ERROR (Qend_of_buffer, error_tail, "End of buffer");
3564 PUT_ERROR (Qbuffer_read_only, error_tail, "Buffer is read-only");
3565 PUT_ERROR (Qtext_read_only, pure_cons (Qbuffer_read_only, error_tail),
3566 "Text is read-only");
3568 DEFSYM (Qrange_error, "range-error");
3569 DEFSYM (Qdomain_error, "domain-error");
3570 DEFSYM (Qsingularity_error, "singularity-error");
3571 DEFSYM (Qoverflow_error, "overflow-error");
3572 DEFSYM (Qunderflow_error, "underflow-error");
3574 PUT_ERROR (Qdomain_error, arith_tail, "Arithmetic domain error");
3576 PUT_ERROR (Qrange_error, arith_tail, "Arithmetic range error");
3578 PUT_ERROR (Qsingularity_error, Fcons (Qdomain_error, arith_tail),
3579 "Arithmetic singularity error");
3581 PUT_ERROR (Qoverflow_error, Fcons (Qdomain_error, arith_tail),
3582 "Arithmetic overflow error");
3583 PUT_ERROR (Qunderflow_error, Fcons (Qdomain_error, arith_tail),
3584 "Arithmetic underflow error");
3586 staticpro (&Qnil);
3587 staticpro (&Qt);
3588 staticpro (&Qunbound);
3590 /* Types that type-of returns. */
3591 DEFSYM (Qinteger, "integer");
3592 DEFSYM (Qsymbol, "symbol");
3593 DEFSYM (Qstring, "string");
3594 DEFSYM (Qcons, "cons");
3595 DEFSYM (Qmarker, "marker");
3596 DEFSYM (Qoverlay, "overlay");
3597 DEFSYM (Qfloat, "float");
3598 DEFSYM (Qwindow_configuration, "window-configuration");
3599 DEFSYM (Qprocess, "process");
3600 DEFSYM (Qwindow, "window");
3601 DEFSYM (Qcompiled_function, "compiled-function");
3602 DEFSYM (Qbuffer, "buffer");
3603 DEFSYM (Qframe, "frame");
3604 DEFSYM (Qvector, "vector");
3605 DEFSYM (Qchar_table, "char-table");
3606 DEFSYM (Qbool_vector, "bool-vector");
3607 DEFSYM (Qhash_table, "hash-table");
3608 DEFSYM (Qmisc, "misc");
3610 DEFSYM (Qdefun, "defun");
3612 DEFSYM (Qfont_spec, "font-spec");
3613 DEFSYM (Qfont_entity, "font-entity");
3614 DEFSYM (Qfont_object, "font-object");
3616 DEFSYM (Qinteractive_form, "interactive-form");
3617 DEFSYM (Qdefalias_fset_function, "defalias-fset-function");
3619 defsubr (&Sindirect_variable);
3620 defsubr (&Sinteractive_form);
3621 defsubr (&Seq);
3622 defsubr (&Snull);
3623 defsubr (&Stype_of);
3624 defsubr (&Slistp);
3625 defsubr (&Snlistp);
3626 defsubr (&Sconsp);
3627 defsubr (&Satom);
3628 defsubr (&Sintegerp);
3629 defsubr (&Sinteger_or_marker_p);
3630 defsubr (&Snumberp);
3631 defsubr (&Snumber_or_marker_p);
3632 defsubr (&Sfloatp);
3633 defsubr (&Snatnump);
3634 defsubr (&Ssymbolp);
3635 defsubr (&Skeywordp);
3636 defsubr (&Sstringp);
3637 defsubr (&Smultibyte_string_p);
3638 defsubr (&Svectorp);
3639 defsubr (&Schar_table_p);
3640 defsubr (&Svector_or_char_table_p);
3641 defsubr (&Sbool_vector_p);
3642 defsubr (&Sarrayp);
3643 defsubr (&Ssequencep);
3644 defsubr (&Sbufferp);
3645 defsubr (&Smarkerp);
3646 defsubr (&Ssubrp);
3647 defsubr (&Sbyte_code_function_p);
3648 defsubr (&Schar_or_string_p);
3649 defsubr (&Scar);
3650 defsubr (&Scdr);
3651 defsubr (&Scar_safe);
3652 defsubr (&Scdr_safe);
3653 defsubr (&Ssetcar);
3654 defsubr (&Ssetcdr);
3655 defsubr (&Ssymbol_function);
3656 defsubr (&Sindirect_function);
3657 defsubr (&Ssymbol_plist);
3658 defsubr (&Ssymbol_name);
3659 defsubr (&Smakunbound);
3660 defsubr (&Sfmakunbound);
3661 defsubr (&Sboundp);
3662 defsubr (&Sfboundp);
3663 defsubr (&Sfset);
3664 defsubr (&Sdefalias);
3665 defsubr (&Ssetplist);
3666 defsubr (&Ssymbol_value);
3667 defsubr (&Sset);
3668 defsubr (&Sdefault_boundp);
3669 defsubr (&Sdefault_value);
3670 defsubr (&Sset_default);
3671 defsubr (&Ssetq_default);
3672 defsubr (&Smake_variable_buffer_local);
3673 defsubr (&Smake_local_variable);
3674 defsubr (&Skill_local_variable);
3675 defsubr (&Smake_variable_frame_local);
3676 defsubr (&Slocal_variable_p);
3677 defsubr (&Slocal_variable_if_set_p);
3678 defsubr (&Svariable_binding_locus);
3679 #if 0 /* XXX Remove this. --lorentey */
3680 defsubr (&Sterminal_local_value);
3681 defsubr (&Sset_terminal_local_value);
3682 #endif
3683 defsubr (&Saref);
3684 defsubr (&Saset);
3685 defsubr (&Snumber_to_string);
3686 defsubr (&Sstring_to_number);
3687 defsubr (&Seqlsign);
3688 defsubr (&Slss);
3689 defsubr (&Sgtr);
3690 defsubr (&Sleq);
3691 defsubr (&Sgeq);
3692 defsubr (&Sneq);
3693 defsubr (&Splus);
3694 defsubr (&Sminus);
3695 defsubr (&Stimes);
3696 defsubr (&Squo);
3697 defsubr (&Srem);
3698 defsubr (&Smod);
3699 defsubr (&Smax);
3700 defsubr (&Smin);
3701 defsubr (&Slogand);
3702 defsubr (&Slogior);
3703 defsubr (&Slogxor);
3704 defsubr (&Slsh);
3705 defsubr (&Sash);
3706 defsubr (&Sadd1);
3707 defsubr (&Ssub1);
3708 defsubr (&Slognot);
3709 defsubr (&Sbyteorder);
3710 defsubr (&Ssubr_arity);
3711 defsubr (&Ssubr_name);
3713 defsubr (&Sbool_vector_exclusive_or);
3714 defsubr (&Sbool_vector_union);
3715 defsubr (&Sbool_vector_intersection);
3716 defsubr (&Sbool_vector_set_difference);
3717 defsubr (&Sbool_vector_not);
3718 defsubr (&Sbool_vector_subsetp);
3719 defsubr (&Sbool_vector_count_consecutive);
3720 defsubr (&Sbool_vector_count_population);
3722 set_symbol_function (Qwholenump, XSYMBOL (Qnatnump)->function);
3724 DEFVAR_LISP ("most-positive-fixnum", Vmost_positive_fixnum,
3725 doc: /* The largest value that is representable in a Lisp integer. */);
3726 Vmost_positive_fixnum = make_number (MOST_POSITIVE_FIXNUM);
3727 XSYMBOL (intern_c_string ("most-positive-fixnum"))->constant = 1;
3729 DEFVAR_LISP ("most-negative-fixnum", Vmost_negative_fixnum,
3730 doc: /* The smallest value that is representable in a Lisp integer. */);
3731 Vmost_negative_fixnum = make_number (MOST_NEGATIVE_FIXNUM);
3732 XSYMBOL (intern_c_string ("most-negative-fixnum"))->constant = 1;