* info.el (Info-try-follow-nearest-node): Move search for footnote
[emacs.git] / src / data.c
blob955c39727bbbd5329fb1dddb274d5d4a26c5ba73
1 /* Primitive operations on Lisp data types for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985-1986, 1988, 1993-1995, 1997-2013 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 <intprops.h>
26 #include "lisp.h"
27 #include "puresize.h"
28 #include "character.h"
29 #include "buffer.h"
30 #include "keyboard.h"
31 #include "frame.h"
32 #include "syssignal.h"
33 #include "termhooks.h" /* For FRAME_KBOARD reference in y-or-n-p. */
34 #include "font.h"
35 #include "keymap.h"
37 Lisp_Object Qnil, Qt, Qquote, Qlambda, Qunbound;
38 static Lisp_Object Qsubr;
39 Lisp_Object Qerror_conditions, Qerror_message, Qtop_level;
40 Lisp_Object Qerror, Quser_error, Qquit, Qargs_out_of_range;
41 static Lisp_Object Qwrong_type_argument;
42 Lisp_Object Qvoid_variable, Qvoid_function;
43 static Lisp_Object Qcyclic_function_indirection;
44 static Lisp_Object Qcyclic_variable_indirection;
45 Lisp_Object Qcircular_list;
46 static Lisp_Object Qsetting_constant;
47 Lisp_Object Qinvalid_read_syntax;
48 Lisp_Object Qinvalid_function, Qwrong_number_of_arguments, Qno_catch;
49 Lisp_Object Qend_of_file, Qarith_error, Qmark_inactive;
50 Lisp_Object Qbeginning_of_buffer, Qend_of_buffer, Qbuffer_read_only;
51 Lisp_Object Qtext_read_only;
53 Lisp_Object Qintegerp, Qwholenump, Qsymbolp, Qlistp, Qconsp;
54 static Lisp_Object Qnatnump;
55 Lisp_Object Qstringp, Qarrayp, Qsequencep, Qbufferp;
56 Lisp_Object Qchar_or_string_p, Qmarkerp, Qinteger_or_marker_p, Qvectorp;
57 Lisp_Object Qbuffer_or_string_p;
58 static Lisp_Object Qkeywordp, Qboundp;
59 Lisp_Object Qfboundp;
60 Lisp_Object Qchar_table_p, Qvector_or_char_table_p;
62 Lisp_Object Qcdr;
63 static Lisp_Object Qad_advice_info, Qad_activate_internal;
65 static Lisp_Object Qdomain_error, Qsingularity_error, Qunderflow_error;
66 Lisp_Object Qrange_error, Qoverflow_error;
68 Lisp_Object Qfloatp;
69 Lisp_Object Qnumberp, Qnumber_or_marker_p;
71 Lisp_Object Qinteger, Qsymbol;
72 static Lisp_Object Qcons, Qfloat, Qmisc, Qstring, Qvector;
73 Lisp_Object Qwindow;
74 static Lisp_Object Qoverlay, Qwindow_configuration;
75 static Lisp_Object Qprocess, Qmarker;
76 static Lisp_Object Qcompiled_function, Qframe;
77 Lisp_Object Qbuffer;
78 static Lisp_Object Qchar_table, Qbool_vector, Qhash_table;
79 static Lisp_Object Qsubrp;
80 static Lisp_Object Qmany, Qunevalled;
81 Lisp_Object Qfont_spec, Qfont_entity, Qfont_object;
82 static Lisp_Object Qdefun;
84 Lisp_Object Qinteractive_form;
85 static Lisp_Object Qdefalias_fset_function;
87 static void swap_in_symval_forwarding (struct Lisp_Symbol *, struct Lisp_Buffer_Local_Value *);
89 static bool
90 BOOLFWDP (union Lisp_Fwd *a)
92 return XFWDTYPE (a) == Lisp_Fwd_Bool;
94 static bool
95 INTFWDP (union Lisp_Fwd *a)
97 return XFWDTYPE (a) == Lisp_Fwd_Int;
99 static bool
100 KBOARD_OBJFWDP (union Lisp_Fwd *a)
102 return XFWDTYPE (a) == Lisp_Fwd_Kboard_Obj;
104 static bool
105 OBJFWDP (union Lisp_Fwd *a)
107 return XFWDTYPE (a) == Lisp_Fwd_Obj;
110 static struct Lisp_Boolfwd *
111 XBOOLFWD (union Lisp_Fwd *a)
113 eassert (BOOLFWDP (a));
114 return &a->u_boolfwd;
116 static struct Lisp_Kboard_Objfwd *
117 XKBOARD_OBJFWD (union Lisp_Fwd *a)
119 eassert (KBOARD_OBJFWDP (a));
120 return &a->u_kboard_objfwd;
122 static struct Lisp_Intfwd *
123 XINTFWD (union Lisp_Fwd *a)
125 eassert (INTFWDP (a));
126 return &a->u_intfwd;
128 static struct Lisp_Objfwd *
129 XOBJFWD (union Lisp_Fwd *a)
131 eassert (OBJFWDP (a));
132 return &a->u_objfwd;
135 static void
136 CHECK_SUBR (Lisp_Object x)
138 CHECK_TYPE (SUBRP (x), Qsubrp, x);
141 static void
142 set_blv_found (struct Lisp_Buffer_Local_Value *blv, int found)
144 eassert (found == !EQ (blv->defcell, blv->valcell));
145 blv->found = found;
148 static Lisp_Object
149 blv_value (struct Lisp_Buffer_Local_Value *blv)
151 return XCDR (blv->valcell);
154 static void
155 set_blv_value (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
157 XSETCDR (blv->valcell, val);
160 static void
161 set_blv_where (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
163 blv->where = val;
166 static void
167 set_blv_defcell (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
169 blv->defcell = val;
172 static void
173 set_blv_valcell (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
175 blv->valcell = val;
178 Lisp_Object
179 wrong_type_argument (register Lisp_Object predicate, register Lisp_Object value)
181 /* If VALUE is not even a valid Lisp object, we'd want to abort here
182 where we can get a backtrace showing where it came from. We used
183 to try and do that by checking the tagbits, but nowadays all
184 tagbits are potentially valid. */
185 /* if ((unsigned int) XTYPE (value) >= Lisp_Type_Limit)
186 * emacs_abort (); */
188 xsignal2 (Qwrong_type_argument, predicate, value);
191 void
192 pure_write_error (Lisp_Object obj)
194 xsignal2 (Qerror, build_string ("Attempt to modify read-only object"), obj);
197 void
198 args_out_of_range (Lisp_Object a1, Lisp_Object a2)
200 xsignal2 (Qargs_out_of_range, a1, a2);
203 void
204 args_out_of_range_3 (Lisp_Object a1, Lisp_Object a2, Lisp_Object a3)
206 xsignal3 (Qargs_out_of_range, a1, a2, a3);
210 /* Data type predicates. */
212 DEFUN ("eq", Feq, Seq, 2, 2, 0,
213 doc: /* Return t if the two args are the same Lisp object. */)
214 (Lisp_Object obj1, Lisp_Object obj2)
216 if (EQ (obj1, obj2))
217 return Qt;
218 return Qnil;
221 DEFUN ("null", Fnull, Snull, 1, 1, 0,
222 doc: /* Return t if OBJECT is nil. */)
223 (Lisp_Object object)
225 if (NILP (object))
226 return Qt;
227 return Qnil;
230 DEFUN ("type-of", Ftype_of, Stype_of, 1, 1, 0,
231 doc: /* Return a symbol representing the type of OBJECT.
232 The symbol returned names the object's basic type;
233 for example, (type-of 1) returns `integer'. */)
234 (Lisp_Object object)
236 switch (XTYPE (object))
238 case_Lisp_Int:
239 return Qinteger;
241 case Lisp_Symbol:
242 return Qsymbol;
244 case Lisp_String:
245 return Qstring;
247 case Lisp_Cons:
248 return Qcons;
250 case Lisp_Misc:
251 switch (XMISCTYPE (object))
253 case Lisp_Misc_Marker:
254 return Qmarker;
255 case Lisp_Misc_Overlay:
256 return Qoverlay;
257 case Lisp_Misc_Float:
258 return Qfloat;
260 emacs_abort ();
262 case Lisp_Vectorlike:
263 if (WINDOW_CONFIGURATIONP (object))
264 return Qwindow_configuration;
265 if (PROCESSP (object))
266 return Qprocess;
267 if (WINDOWP (object))
268 return Qwindow;
269 if (SUBRP (object))
270 return Qsubr;
271 if (COMPILEDP (object))
272 return Qcompiled_function;
273 if (BUFFERP (object))
274 return Qbuffer;
275 if (CHAR_TABLE_P (object))
276 return Qchar_table;
277 if (BOOL_VECTOR_P (object))
278 return Qbool_vector;
279 if (FRAMEP (object))
280 return Qframe;
281 if (HASH_TABLE_P (object))
282 return Qhash_table;
283 if (FONT_SPEC_P (object))
284 return Qfont_spec;
285 if (FONT_ENTITY_P (object))
286 return Qfont_entity;
287 if (FONT_OBJECT_P (object))
288 return Qfont_object;
289 return Qvector;
291 case Lisp_Float:
292 return Qfloat;
294 default:
295 emacs_abort ();
299 DEFUN ("consp", Fconsp, Sconsp, 1, 1, 0,
300 doc: /* Return t if OBJECT is a cons cell. */)
301 (Lisp_Object object)
303 if (CONSP (object))
304 return Qt;
305 return Qnil;
308 DEFUN ("atom", Fatom, Satom, 1, 1, 0,
309 doc: /* Return t if OBJECT is not a cons cell. This includes nil. */)
310 (Lisp_Object object)
312 if (CONSP (object))
313 return Qnil;
314 return Qt;
317 DEFUN ("listp", Flistp, Slistp, 1, 1, 0,
318 doc: /* Return t if OBJECT is a list, that is, a cons cell or nil.
319 Otherwise, return nil. */)
320 (Lisp_Object object)
322 if (CONSP (object) || NILP (object))
323 return Qt;
324 return Qnil;
327 DEFUN ("nlistp", Fnlistp, Snlistp, 1, 1, 0,
328 doc: /* Return t if OBJECT is not a list. Lists include nil. */)
329 (Lisp_Object object)
331 if (CONSP (object) || NILP (object))
332 return Qnil;
333 return Qt;
336 DEFUN ("symbolp", Fsymbolp, Ssymbolp, 1, 1, 0,
337 doc: /* Return t if OBJECT is a symbol. */)
338 (Lisp_Object object)
340 if (SYMBOLP (object))
341 return Qt;
342 return Qnil;
345 /* Define this in C to avoid unnecessarily consing up the symbol
346 name. */
347 DEFUN ("keywordp", Fkeywordp, Skeywordp, 1, 1, 0,
348 doc: /* Return t if OBJECT is a keyword.
349 This means that it is a symbol with a print name beginning with `:'
350 interned in the initial obarray. */)
351 (Lisp_Object object)
353 if (SYMBOLP (object)
354 && SREF (SYMBOL_NAME (object), 0) == ':'
355 && SYMBOL_INTERNED_IN_INITIAL_OBARRAY_P (object))
356 return Qt;
357 return Qnil;
360 DEFUN ("vectorp", Fvectorp, Svectorp, 1, 1, 0,
361 doc: /* Return t if OBJECT is a vector. */)
362 (Lisp_Object object)
364 if (VECTORP (object))
365 return Qt;
366 return Qnil;
369 DEFUN ("stringp", Fstringp, Sstringp, 1, 1, 0,
370 doc: /* Return t if OBJECT is a string. */)
371 (Lisp_Object object)
373 if (STRINGP (object))
374 return Qt;
375 return Qnil;
378 DEFUN ("multibyte-string-p", Fmultibyte_string_p, Smultibyte_string_p,
379 1, 1, 0,
380 doc: /* Return t if OBJECT is a multibyte string. */)
381 (Lisp_Object object)
383 if (STRINGP (object) && STRING_MULTIBYTE (object))
384 return Qt;
385 return Qnil;
388 DEFUN ("char-table-p", Fchar_table_p, Schar_table_p, 1, 1, 0,
389 doc: /* Return t if OBJECT is a char-table. */)
390 (Lisp_Object object)
392 if (CHAR_TABLE_P (object))
393 return Qt;
394 return Qnil;
397 DEFUN ("vector-or-char-table-p", Fvector_or_char_table_p,
398 Svector_or_char_table_p, 1, 1, 0,
399 doc: /* Return t if OBJECT is a char-table or vector. */)
400 (Lisp_Object object)
402 if (VECTORP (object) || CHAR_TABLE_P (object))
403 return Qt;
404 return Qnil;
407 DEFUN ("bool-vector-p", Fbool_vector_p, Sbool_vector_p, 1, 1, 0,
408 doc: /* Return t if OBJECT is a bool-vector. */)
409 (Lisp_Object object)
411 if (BOOL_VECTOR_P (object))
412 return Qt;
413 return Qnil;
416 DEFUN ("arrayp", Farrayp, Sarrayp, 1, 1, 0,
417 doc: /* Return t if OBJECT is an array (string or vector). */)
418 (Lisp_Object object)
420 if (ARRAYP (object))
421 return Qt;
422 return Qnil;
425 DEFUN ("sequencep", Fsequencep, Ssequencep, 1, 1, 0,
426 doc: /* Return t if OBJECT is a sequence (list or array). */)
427 (register Lisp_Object object)
429 if (CONSP (object) || NILP (object) || ARRAYP (object))
430 return Qt;
431 return Qnil;
434 DEFUN ("bufferp", Fbufferp, Sbufferp, 1, 1, 0,
435 doc: /* Return t if OBJECT is an editor buffer. */)
436 (Lisp_Object object)
438 if (BUFFERP (object))
439 return Qt;
440 return Qnil;
443 DEFUN ("markerp", Fmarkerp, Smarkerp, 1, 1, 0,
444 doc: /* Return t if OBJECT is a marker (editor pointer). */)
445 (Lisp_Object object)
447 if (MARKERP (object))
448 return Qt;
449 return Qnil;
452 DEFUN ("subrp", Fsubrp, Ssubrp, 1, 1, 0,
453 doc: /* Return t if OBJECT is a built-in function. */)
454 (Lisp_Object object)
456 if (SUBRP (object))
457 return Qt;
458 return Qnil;
461 DEFUN ("byte-code-function-p", Fbyte_code_function_p, Sbyte_code_function_p,
462 1, 1, 0,
463 doc: /* Return t if OBJECT is a byte-compiled function object. */)
464 (Lisp_Object object)
466 if (COMPILEDP (object))
467 return Qt;
468 return Qnil;
471 DEFUN ("char-or-string-p", Fchar_or_string_p, Schar_or_string_p, 1, 1, 0,
472 doc: /* Return t if OBJECT is a character or a string. */)
473 (register Lisp_Object object)
475 if (CHARACTERP (object) || STRINGP (object))
476 return Qt;
477 return Qnil;
480 DEFUN ("integerp", Fintegerp, Sintegerp, 1, 1, 0,
481 doc: /* Return t if OBJECT is an integer. */)
482 (Lisp_Object object)
484 if (INTEGERP (object))
485 return Qt;
486 return Qnil;
489 DEFUN ("integer-or-marker-p", Finteger_or_marker_p, Sinteger_or_marker_p, 1, 1, 0,
490 doc: /* Return t if OBJECT is an integer or a marker (editor pointer). */)
491 (register Lisp_Object object)
493 if (MARKERP (object) || INTEGERP (object))
494 return Qt;
495 return Qnil;
498 DEFUN ("natnump", Fnatnump, Snatnump, 1, 1, 0,
499 doc: /* Return t if OBJECT is a nonnegative integer. */)
500 (Lisp_Object object)
502 if (NATNUMP (object))
503 return Qt;
504 return Qnil;
507 DEFUN ("numberp", Fnumberp, Snumberp, 1, 1, 0,
508 doc: /* Return t if OBJECT is a number (floating point or integer). */)
509 (Lisp_Object object)
511 if (NUMBERP (object))
512 return Qt;
513 else
514 return Qnil;
517 DEFUN ("number-or-marker-p", Fnumber_or_marker_p,
518 Snumber_or_marker_p, 1, 1, 0,
519 doc: /* Return t if OBJECT is a number or a marker. */)
520 (Lisp_Object object)
522 if (NUMBERP (object) || MARKERP (object))
523 return Qt;
524 return Qnil;
527 DEFUN ("floatp", Ffloatp, Sfloatp, 1, 1, 0,
528 doc: /* Return t if OBJECT is a floating point number. */)
529 (Lisp_Object object)
531 if (FLOATP (object))
532 return Qt;
533 return Qnil;
537 /* Extract and set components of lists. */
539 DEFUN ("car", Fcar, Scar, 1, 1, 0,
540 doc: /* Return the car of LIST. If arg is nil, return nil.
541 Error if arg is not nil and not a cons cell. See also `car-safe'.
543 See Info node `(elisp)Cons Cells' for a discussion of related basic
544 Lisp concepts such as car, cdr, cons cell and list. */)
545 (register Lisp_Object list)
547 return CAR (list);
550 DEFUN ("car-safe", Fcar_safe, Scar_safe, 1, 1, 0,
551 doc: /* Return the car of OBJECT if it is a cons cell, or else nil. */)
552 (Lisp_Object object)
554 return CAR_SAFE (object);
557 DEFUN ("cdr", Fcdr, Scdr, 1, 1, 0,
558 doc: /* Return the cdr of LIST. If arg is nil, return nil.
559 Error if arg is not nil and not a cons cell. See also `cdr-safe'.
561 See Info node `(elisp)Cons Cells' for a discussion of related basic
562 Lisp concepts such as cdr, car, cons cell and list. */)
563 (register Lisp_Object list)
565 return CDR (list);
568 DEFUN ("cdr-safe", Fcdr_safe, Scdr_safe, 1, 1, 0,
569 doc: /* Return the cdr of OBJECT if it is a cons cell, or else nil. */)
570 (Lisp_Object object)
572 return CDR_SAFE (object);
575 DEFUN ("setcar", Fsetcar, Ssetcar, 2, 2, 0,
576 doc: /* Set the car of CELL to be NEWCAR. Returns NEWCAR. */)
577 (register Lisp_Object cell, Lisp_Object newcar)
579 CHECK_CONS (cell);
580 CHECK_IMPURE (cell);
581 XSETCAR (cell, newcar);
582 return newcar;
585 DEFUN ("setcdr", Fsetcdr, Ssetcdr, 2, 2, 0,
586 doc: /* Set the cdr of CELL to be NEWCDR. Returns NEWCDR. */)
587 (register Lisp_Object cell, Lisp_Object newcdr)
589 CHECK_CONS (cell);
590 CHECK_IMPURE (cell);
591 XSETCDR (cell, newcdr);
592 return newcdr;
595 /* Extract and set components of symbols. */
597 DEFUN ("boundp", Fboundp, Sboundp, 1, 1, 0,
598 doc: /* Return t if SYMBOL's value is not void.
599 Note that if `lexical-binding' is in effect, this refers to the
600 global value outside of any lexical scope. */)
601 (register Lisp_Object symbol)
603 Lisp_Object valcontents;
604 struct Lisp_Symbol *sym;
605 CHECK_SYMBOL (symbol);
606 sym = XSYMBOL (symbol);
608 start:
609 switch (sym->redirect)
611 case SYMBOL_PLAINVAL: valcontents = SYMBOL_VAL (sym); break;
612 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
613 case SYMBOL_LOCALIZED:
615 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
616 if (blv->fwd)
617 /* In set_internal, we un-forward vars when their value is
618 set to Qunbound. */
619 return Qt;
620 else
622 swap_in_symval_forwarding (sym, blv);
623 valcontents = blv_value (blv);
625 break;
627 case SYMBOL_FORWARDED:
628 /* In set_internal, we un-forward vars when their value is
629 set to Qunbound. */
630 return Qt;
631 default: emacs_abort ();
634 return (EQ (valcontents, Qunbound) ? Qnil : Qt);
637 /* FIXME: Make it an alias for function-symbol! */
638 DEFUN ("fboundp", Ffboundp, Sfboundp, 1, 1, 0,
639 doc: /* Return t if SYMBOL's function definition is not void. */)
640 (register Lisp_Object symbol)
642 CHECK_SYMBOL (symbol);
643 return NILP (XSYMBOL (symbol)->function) ? Qnil : Qt;
646 DEFUN ("makunbound", Fmakunbound, Smakunbound, 1, 1, 0,
647 doc: /* Make SYMBOL's value be void.
648 Return SYMBOL. */)
649 (register Lisp_Object symbol)
651 CHECK_SYMBOL (symbol);
652 if (SYMBOL_CONSTANT_P (symbol))
653 xsignal1 (Qsetting_constant, symbol);
654 Fset (symbol, Qunbound);
655 return symbol;
658 DEFUN ("fmakunbound", Ffmakunbound, Sfmakunbound, 1, 1, 0,
659 doc: /* Make SYMBOL's function definition be nil.
660 Return SYMBOL. */)
661 (register Lisp_Object symbol)
663 CHECK_SYMBOL (symbol);
664 if (NILP (symbol) || EQ (symbol, Qt))
665 xsignal1 (Qsetting_constant, symbol);
666 set_symbol_function (symbol, Qnil);
667 return symbol;
670 DEFUN ("symbol-function", Fsymbol_function, Ssymbol_function, 1, 1, 0,
671 doc: /* Return SYMBOL's function definition. Error if that is void. */)
672 (register Lisp_Object symbol)
674 CHECK_SYMBOL (symbol);
675 return XSYMBOL (symbol)->function;
678 DEFUN ("symbol-plist", Fsymbol_plist, Ssymbol_plist, 1, 1, 0,
679 doc: /* Return SYMBOL's property list. */)
680 (register Lisp_Object symbol)
682 CHECK_SYMBOL (symbol);
683 return XSYMBOL (symbol)->plist;
686 DEFUN ("symbol-name", Fsymbol_name, Ssymbol_name, 1, 1, 0,
687 doc: /* Return SYMBOL's name, a string. */)
688 (register Lisp_Object symbol)
690 register Lisp_Object name;
692 CHECK_SYMBOL (symbol);
693 name = SYMBOL_NAME (symbol);
694 return name;
697 DEFUN ("fset", Ffset, Sfset, 2, 2, 0,
698 doc: /* Set SYMBOL's function definition to DEFINITION, and return DEFINITION. */)
699 (register Lisp_Object symbol, Lisp_Object definition)
701 register Lisp_Object function;
702 CHECK_SYMBOL (symbol);
704 function = XSYMBOL (symbol)->function;
706 if (!NILP (Vautoload_queue) && !NILP (function))
707 Vautoload_queue = Fcons (Fcons (symbol, function), Vautoload_queue);
709 if (AUTOLOADP (function))
710 Fput (symbol, Qautoload, XCDR (function));
712 set_symbol_function (symbol, definition);
714 return definition;
717 DEFUN ("defalias", Fdefalias, Sdefalias, 2, 3, 0,
718 doc: /* Set SYMBOL's function definition to DEFINITION.
719 Associates the function with the current load file, if any.
720 The optional third argument DOCSTRING specifies the documentation string
721 for SYMBOL; if it is omitted or nil, SYMBOL uses the documentation string
722 determined by DEFINITION.
723 The return value is undefined. */)
724 (register Lisp_Object symbol, Lisp_Object definition, Lisp_Object docstring)
726 CHECK_SYMBOL (symbol);
727 if (!NILP (Vpurify_flag)
728 /* If `definition' is a keymap, immutable (and copying) is wrong. */
729 && !KEYMAPP (definition))
730 definition = Fpurecopy (definition);
733 bool autoload = AUTOLOADP (definition);
734 if (NILP (Vpurify_flag) || !autoload)
735 { /* Only add autoload entries after dumping, because the ones before are
736 not useful and else we get loads of them from the loaddefs.el. */
738 if (AUTOLOADP (XSYMBOL (symbol)->function))
739 /* Remember that the function was already an autoload. */
740 LOADHIST_ATTACH (Fcons (Qt, symbol));
741 LOADHIST_ATTACH (Fcons (autoload ? Qautoload : Qdefun, symbol));
745 { /* Handle automatic advice activation. */
746 Lisp_Object hook = Fget (symbol, Qdefalias_fset_function);
747 if (!NILP (hook))
748 call2 (hook, symbol, definition);
749 else
750 Ffset (symbol, definition);
753 if (!NILP (docstring))
754 Fput (symbol, Qfunction_documentation, docstring);
755 /* We used to return `definition', but now that `defun' and `defmacro' expand
756 to a call to `defalias', we return `symbol' for backward compatibility
757 (bug#11686). */
758 return symbol;
761 DEFUN ("setplist", Fsetplist, Ssetplist, 2, 2, 0,
762 doc: /* Set SYMBOL's property list to NEWPLIST, and return NEWPLIST. */)
763 (register Lisp_Object symbol, Lisp_Object newplist)
765 CHECK_SYMBOL (symbol);
766 set_symbol_plist (symbol, newplist);
767 return newplist;
770 DEFUN ("subr-arity", Fsubr_arity, Ssubr_arity, 1, 1, 0,
771 doc: /* Return minimum and maximum number of args allowed for SUBR.
772 SUBR must be a built-in function.
773 The returned value is a pair (MIN . MAX). MIN is the minimum number
774 of args. MAX is the maximum number or the symbol `many', for a
775 function with `&rest' args, or `unevalled' for a special form. */)
776 (Lisp_Object subr)
778 short minargs, maxargs;
779 CHECK_SUBR (subr);
780 minargs = XSUBR (subr)->min_args;
781 maxargs = XSUBR (subr)->max_args;
782 return Fcons (make_number (minargs),
783 maxargs == MANY ? Qmany
784 : maxargs == UNEVALLED ? Qunevalled
785 : make_number (maxargs));
788 DEFUN ("subr-name", Fsubr_name, Ssubr_name, 1, 1, 0,
789 doc: /* Return name of subroutine SUBR.
790 SUBR must be a built-in function. */)
791 (Lisp_Object subr)
793 const char *name;
794 CHECK_SUBR (subr);
795 name = XSUBR (subr)->symbol_name;
796 return build_string (name);
799 DEFUN ("interactive-form", Finteractive_form, Sinteractive_form, 1, 1, 0,
800 doc: /* Return the interactive form of CMD or nil if none.
801 If CMD is not a command, the return value is nil.
802 Value, if non-nil, is a list \(interactive SPEC). */)
803 (Lisp_Object cmd)
805 Lisp_Object fun = indirect_function (cmd); /* Check cycles. */
807 if (NILP (fun))
808 return Qnil;
810 /* Use an `interactive-form' property if present, analogous to the
811 function-documentation property. */
812 fun = cmd;
813 while (SYMBOLP (fun))
815 Lisp_Object tmp = Fget (fun, Qinteractive_form);
816 if (!NILP (tmp))
817 return tmp;
818 else
819 fun = Fsymbol_function (fun);
822 if (SUBRP (fun))
824 const char *spec = XSUBR (fun)->intspec;
825 if (spec)
826 return list2 (Qinteractive,
827 (*spec != '(') ? build_string (spec) :
828 Fcar (Fread_from_string (build_string (spec), Qnil, Qnil)));
830 else if (COMPILEDP (fun))
832 if ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE)
833 return list2 (Qinteractive, AREF (fun, COMPILED_INTERACTIVE));
835 else if (AUTOLOADP (fun))
836 return Finteractive_form (Fautoload_do_load (fun, cmd, Qnil));
837 else if (CONSP (fun))
839 Lisp_Object funcar = XCAR (fun);
840 if (EQ (funcar, Qclosure))
841 return Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun))));
842 else if (EQ (funcar, Qlambda))
843 return Fassq (Qinteractive, Fcdr (XCDR (fun)));
845 return Qnil;
849 /***********************************************************************
850 Getting and Setting Values of Symbols
851 ***********************************************************************/
853 /* Return the symbol holding SYMBOL's value. Signal
854 `cyclic-variable-indirection' if SYMBOL's chain of variable
855 indirections contains a loop. */
857 struct Lisp_Symbol *
858 indirect_variable (struct Lisp_Symbol *symbol)
860 struct Lisp_Symbol *tortoise, *hare;
862 hare = tortoise = symbol;
864 while (hare->redirect == SYMBOL_VARALIAS)
866 hare = SYMBOL_ALIAS (hare);
867 if (hare->redirect != SYMBOL_VARALIAS)
868 break;
870 hare = SYMBOL_ALIAS (hare);
871 tortoise = SYMBOL_ALIAS (tortoise);
873 if (hare == tortoise)
875 Lisp_Object tem;
876 XSETSYMBOL (tem, symbol);
877 xsignal1 (Qcyclic_variable_indirection, tem);
881 return hare;
885 DEFUN ("indirect-variable", Findirect_variable, Sindirect_variable, 1, 1, 0,
886 doc: /* Return the variable at the end of OBJECT's variable chain.
887 If OBJECT is a symbol, follow its variable indirections (if any), and
888 return the variable at the end of the chain of aliases. See Info node
889 `(elisp)Variable Aliases'.
891 If OBJECT is not a symbol, just return it. If there is a loop in the
892 chain of aliases, signal a `cyclic-variable-indirection' error. */)
893 (Lisp_Object object)
895 if (SYMBOLP (object))
897 struct Lisp_Symbol *sym = indirect_variable (XSYMBOL (object));
898 XSETSYMBOL (object, sym);
900 return object;
904 /* Given the raw contents of a symbol value cell,
905 return the Lisp value of the symbol.
906 This does not handle buffer-local variables; use
907 swap_in_symval_forwarding for that. */
909 Lisp_Object
910 do_symval_forwarding (register union Lisp_Fwd *valcontents)
912 register Lisp_Object val;
913 switch (XFWDTYPE (valcontents))
915 case Lisp_Fwd_Int:
916 XSETINT (val, *XINTFWD (valcontents)->intvar);
917 return val;
919 case Lisp_Fwd_Bool:
920 return (*XBOOLFWD (valcontents)->boolvar ? Qt : Qnil);
922 case Lisp_Fwd_Obj:
923 return *XOBJFWD (valcontents)->objvar;
925 case Lisp_Fwd_Buffer_Obj:
926 return per_buffer_value (current_buffer,
927 XBUFFER_OBJFWD (valcontents)->offset);
929 case Lisp_Fwd_Kboard_Obj:
930 /* We used to simply use current_kboard here, but from Lisp
931 code, its value is often unexpected. It seems nicer to
932 allow constructions like this to work as intuitively expected:
934 (with-selected-frame frame
935 (define-key local-function-map "\eOP" [f1]))
937 On the other hand, this affects the semantics of
938 last-command and real-last-command, and people may rely on
939 that. I took a quick look at the Lisp codebase, and I
940 don't think anything will break. --lorentey */
941 return *(Lisp_Object *)(XKBOARD_OBJFWD (valcontents)->offset
942 + (char *)FRAME_KBOARD (SELECTED_FRAME ()));
943 default: emacs_abort ();
947 /* Store NEWVAL into SYMBOL, where VALCONTENTS is found in the value cell
948 of SYMBOL. If SYMBOL is buffer-local, VALCONTENTS should be the
949 buffer-independent contents of the value cell: forwarded just one
950 step past the buffer-localness.
952 BUF non-zero means set the value in buffer BUF instead of the
953 current buffer. This only plays a role for per-buffer variables. */
955 static void
956 store_symval_forwarding (union Lisp_Fwd *valcontents, register Lisp_Object newval, struct buffer *buf)
958 switch (XFWDTYPE (valcontents))
960 case Lisp_Fwd_Int:
961 CHECK_NUMBER (newval);
962 *XINTFWD (valcontents)->intvar = XINT (newval);
963 break;
965 case Lisp_Fwd_Bool:
966 *XBOOLFWD (valcontents)->boolvar = !NILP (newval);
967 break;
969 case Lisp_Fwd_Obj:
970 *XOBJFWD (valcontents)->objvar = newval;
972 /* If this variable is a default for something stored
973 in the buffer itself, such as default-fill-column,
974 find the buffers that don't have local values for it
975 and update them. */
976 if (XOBJFWD (valcontents)->objvar > (Lisp_Object *) &buffer_defaults
977 && XOBJFWD (valcontents)->objvar < (Lisp_Object *) (&buffer_defaults + 1))
979 int offset = ((char *) XOBJFWD (valcontents)->objvar
980 - (char *) &buffer_defaults);
981 int idx = PER_BUFFER_IDX (offset);
983 Lisp_Object tail;
985 if (idx <= 0)
986 break;
988 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
990 Lisp_Object lbuf;
991 struct buffer *b;
993 lbuf = Fcdr (XCAR (tail));
994 if (!BUFFERP (lbuf)) continue;
995 b = XBUFFER (lbuf);
997 if (! PER_BUFFER_VALUE_P (b, idx))
998 set_per_buffer_value (b, offset, newval);
1001 break;
1003 case Lisp_Fwd_Buffer_Obj:
1005 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1006 Lisp_Object predicate = XBUFFER_OBJFWD (valcontents)->predicate;
1008 if (!NILP (predicate) && !NILP (newval)
1009 && NILP (call1 (predicate, newval)))
1010 wrong_type_argument (predicate, newval);
1012 if (buf == NULL)
1013 buf = current_buffer;
1014 set_per_buffer_value (buf, offset, newval);
1016 break;
1018 case Lisp_Fwd_Kboard_Obj:
1020 char *base = (char *) FRAME_KBOARD (SELECTED_FRAME ());
1021 char *p = base + XKBOARD_OBJFWD (valcontents)->offset;
1022 *(Lisp_Object *) p = newval;
1024 break;
1026 default:
1027 emacs_abort (); /* goto def; */
1031 /* Set up SYMBOL to refer to its global binding. This makes it safe
1032 to alter the status of other bindings. BEWARE: this may be called
1033 during the mark phase of GC, where we assume that Lisp_Object slots
1034 of BLV are marked after this function has changed them. */
1036 void
1037 swap_in_global_binding (struct Lisp_Symbol *symbol)
1039 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (symbol);
1041 /* Unload the previously loaded binding. */
1042 if (blv->fwd)
1043 set_blv_value (blv, do_symval_forwarding (blv->fwd));
1045 /* Select the global binding in the symbol. */
1046 set_blv_valcell (blv, blv->defcell);
1047 if (blv->fwd)
1048 store_symval_forwarding (blv->fwd, XCDR (blv->defcell), NULL);
1050 /* Indicate that the global binding is set up now. */
1051 set_blv_where (blv, Qnil);
1052 set_blv_found (blv, 0);
1055 /* Set up the buffer-local symbol SYMBOL for validity in the current buffer.
1056 VALCONTENTS is the contents of its value cell,
1057 which points to a struct Lisp_Buffer_Local_Value.
1059 Return the value forwarded one step past the buffer-local stage.
1060 This could be another forwarding pointer. */
1062 static void
1063 swap_in_symval_forwarding (struct Lisp_Symbol *symbol, struct Lisp_Buffer_Local_Value *blv)
1065 register Lisp_Object tem1;
1067 eassert (blv == SYMBOL_BLV (symbol));
1069 tem1 = blv->where;
1071 if (NILP (tem1)
1072 || (blv->frame_local
1073 ? !EQ (selected_frame, tem1)
1074 : current_buffer != XBUFFER (tem1)))
1077 /* Unload the previously loaded binding. */
1078 tem1 = blv->valcell;
1079 if (blv->fwd)
1080 set_blv_value (blv, do_symval_forwarding (blv->fwd));
1081 /* Choose the new binding. */
1083 Lisp_Object var;
1084 XSETSYMBOL (var, symbol);
1085 if (blv->frame_local)
1087 tem1 = assq_no_quit (var, XFRAME (selected_frame)->param_alist);
1088 set_blv_where (blv, selected_frame);
1090 else
1092 tem1 = assq_no_quit (var, BVAR (current_buffer, local_var_alist));
1093 set_blv_where (blv, Fcurrent_buffer ());
1096 if (!(blv->found = !NILP (tem1)))
1097 tem1 = blv->defcell;
1099 /* Load the new binding. */
1100 set_blv_valcell (blv, tem1);
1101 if (blv->fwd)
1102 store_symval_forwarding (blv->fwd, blv_value (blv), NULL);
1106 /* Find the value of a symbol, returning Qunbound if it's not bound.
1107 This is helpful for code which just wants to get a variable's value
1108 if it has one, without signaling an error.
1109 Note that it must not be possible to quit
1110 within this function. Great care is required for this. */
1112 Lisp_Object
1113 find_symbol_value (Lisp_Object symbol)
1115 struct Lisp_Symbol *sym;
1117 CHECK_SYMBOL (symbol);
1118 sym = XSYMBOL (symbol);
1120 start:
1121 switch (sym->redirect)
1123 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1124 case SYMBOL_PLAINVAL: return SYMBOL_VAL (sym);
1125 case SYMBOL_LOCALIZED:
1127 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1128 swap_in_symval_forwarding (sym, blv);
1129 return blv->fwd ? do_symval_forwarding (blv->fwd) : blv_value (blv);
1131 /* FALLTHROUGH */
1132 case SYMBOL_FORWARDED:
1133 return do_symval_forwarding (SYMBOL_FWD (sym));
1134 default: emacs_abort ();
1138 DEFUN ("symbol-value", Fsymbol_value, Ssymbol_value, 1, 1, 0,
1139 doc: /* Return SYMBOL's value. Error if that is void.
1140 Note that if `lexical-binding' is in effect, this returns the
1141 global value outside of any lexical scope. */)
1142 (Lisp_Object symbol)
1144 Lisp_Object val;
1146 val = find_symbol_value (symbol);
1147 if (!EQ (val, Qunbound))
1148 return val;
1150 xsignal1 (Qvoid_variable, symbol);
1153 DEFUN ("set", Fset, Sset, 2, 2, 0,
1154 doc: /* Set SYMBOL's value to NEWVAL, and return NEWVAL. */)
1155 (register Lisp_Object symbol, Lisp_Object newval)
1157 set_internal (symbol, newval, Qnil, 0);
1158 return newval;
1161 /* Store the value NEWVAL into SYMBOL.
1162 If buffer/frame-locality is an issue, WHERE specifies which context to use.
1163 (nil stands for the current buffer/frame).
1165 If BINDFLAG is false, then if this symbol is supposed to become
1166 local in every buffer where it is set, then we make it local.
1167 If BINDFLAG is true, we don't do that. */
1169 void
1170 set_internal (Lisp_Object symbol, Lisp_Object newval, Lisp_Object where,
1171 bool bindflag)
1173 bool voide = EQ (newval, Qunbound);
1174 struct Lisp_Symbol *sym;
1175 Lisp_Object tem1;
1177 /* If restoring in a dead buffer, do nothing. */
1178 /* if (BUFFERP (where) && NILP (XBUFFER (where)->name))
1179 return; */
1181 CHECK_SYMBOL (symbol);
1182 if (SYMBOL_CONSTANT_P (symbol))
1184 if (NILP (Fkeywordp (symbol))
1185 || !EQ (newval, Fsymbol_value (symbol)))
1186 xsignal1 (Qsetting_constant, symbol);
1187 else
1188 /* Allow setting keywords to their own value. */
1189 return;
1192 sym = XSYMBOL (symbol);
1194 start:
1195 switch (sym->redirect)
1197 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1198 case SYMBOL_PLAINVAL: SET_SYMBOL_VAL (sym , newval); return;
1199 case SYMBOL_LOCALIZED:
1201 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1202 if (NILP (where))
1204 if (blv->frame_local)
1205 where = selected_frame;
1206 else
1207 XSETBUFFER (where, current_buffer);
1209 /* If the current buffer is not the buffer whose binding is
1210 loaded, or if there may be frame-local bindings and the frame
1211 isn't the right one, or if it's a Lisp_Buffer_Local_Value and
1212 the default binding is loaded, the loaded binding may be the
1213 wrong one. */
1214 if (!EQ (blv->where, where)
1215 /* Also unload a global binding (if the var is local_if_set). */
1216 || (EQ (blv->valcell, blv->defcell)))
1218 /* The currently loaded binding is not necessarily valid.
1219 We need to unload it, and choose a new binding. */
1221 /* Write out `realvalue' to the old loaded binding. */
1222 if (blv->fwd)
1223 set_blv_value (blv, do_symval_forwarding (blv->fwd));
1225 /* Find the new binding. */
1226 XSETSYMBOL (symbol, sym); /* May have changed via aliasing. */
1227 tem1 = Fassq (symbol,
1228 (blv->frame_local
1229 ? XFRAME (where)->param_alist
1230 : BVAR (XBUFFER (where), local_var_alist)));
1231 set_blv_where (blv, where);
1232 blv->found = 1;
1234 if (NILP (tem1))
1236 /* This buffer still sees the default value. */
1238 /* If the variable is a Lisp_Some_Buffer_Local_Value,
1239 or if this is `let' rather than `set',
1240 make CURRENT-ALIST-ELEMENT point to itself,
1241 indicating that we're seeing the default value.
1242 Likewise if the variable has been let-bound
1243 in the current buffer. */
1244 if (bindflag || !blv->local_if_set
1245 || let_shadows_buffer_binding_p (sym))
1247 blv->found = 0;
1248 tem1 = blv->defcell;
1250 /* If it's a local_if_set, being set not bound,
1251 and we're not within a let that was made for this buffer,
1252 create a new buffer-local binding for the variable.
1253 That means, give this buffer a new assoc for a local value
1254 and load that binding. */
1255 else
1257 /* local_if_set is only supported for buffer-local
1258 bindings, not for frame-local bindings. */
1259 eassert (!blv->frame_local);
1260 tem1 = Fcons (symbol, XCDR (blv->defcell));
1261 bset_local_var_alist
1262 (XBUFFER (where),
1263 Fcons (tem1, BVAR (XBUFFER (where), local_var_alist)));
1267 /* Record which binding is now loaded. */
1268 set_blv_valcell (blv, tem1);
1271 /* Store the new value in the cons cell. */
1272 set_blv_value (blv, newval);
1274 if (blv->fwd)
1276 if (voide)
1277 /* If storing void (making the symbol void), forward only through
1278 buffer-local indicator, not through Lisp_Objfwd, etc. */
1279 blv->fwd = NULL;
1280 else
1281 store_symval_forwarding (blv->fwd, newval,
1282 BUFFERP (where)
1283 ? XBUFFER (where) : current_buffer);
1285 break;
1287 case SYMBOL_FORWARDED:
1289 struct buffer *buf
1290 = BUFFERP (where) ? XBUFFER (where) : current_buffer;
1291 union Lisp_Fwd *innercontents = SYMBOL_FWD (sym);
1292 if (BUFFER_OBJFWDP (innercontents))
1294 int offset = XBUFFER_OBJFWD (innercontents)->offset;
1295 int idx = PER_BUFFER_IDX (offset);
1296 if (idx > 0
1297 && !bindflag
1298 && !let_shadows_buffer_binding_p (sym))
1299 SET_PER_BUFFER_VALUE_P (buf, idx, 1);
1302 if (voide)
1303 { /* If storing void (making the symbol void), forward only through
1304 buffer-local indicator, not through Lisp_Objfwd, etc. */
1305 sym->redirect = SYMBOL_PLAINVAL;
1306 SET_SYMBOL_VAL (sym, newval);
1308 else
1309 store_symval_forwarding (/* sym, */ innercontents, newval, buf);
1310 break;
1312 default: emacs_abort ();
1314 return;
1317 /* Access or set a buffer-local symbol's default value. */
1319 /* Return the default value of SYMBOL, but don't check for voidness.
1320 Return Qunbound if it is void. */
1322 static Lisp_Object
1323 default_value (Lisp_Object symbol)
1325 struct Lisp_Symbol *sym;
1327 CHECK_SYMBOL (symbol);
1328 sym = XSYMBOL (symbol);
1330 start:
1331 switch (sym->redirect)
1333 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1334 case SYMBOL_PLAINVAL: return SYMBOL_VAL (sym);
1335 case SYMBOL_LOCALIZED:
1337 /* If var is set up for a buffer that lacks a local value for it,
1338 the current value is nominally the default value.
1339 But the `realvalue' slot may be more up to date, since
1340 ordinary setq stores just that slot. So use that. */
1341 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1342 if (blv->fwd && EQ (blv->valcell, blv->defcell))
1343 return do_symval_forwarding (blv->fwd);
1344 else
1345 return XCDR (blv->defcell);
1347 case SYMBOL_FORWARDED:
1349 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
1351 /* For a built-in buffer-local variable, get the default value
1352 rather than letting do_symval_forwarding get the current value. */
1353 if (BUFFER_OBJFWDP (valcontents))
1355 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1356 if (PER_BUFFER_IDX (offset) != 0)
1357 return per_buffer_default (offset);
1360 /* For other variables, get the current value. */
1361 return do_symval_forwarding (valcontents);
1363 default: emacs_abort ();
1367 DEFUN ("default-boundp", Fdefault_boundp, Sdefault_boundp, 1, 1, 0,
1368 doc: /* Return t if SYMBOL has a non-void default value.
1369 This is the value that is seen in buffers that do not have their own values
1370 for this variable. */)
1371 (Lisp_Object symbol)
1373 register Lisp_Object value;
1375 value = default_value (symbol);
1376 return (EQ (value, Qunbound) ? Qnil : Qt);
1379 DEFUN ("default-value", Fdefault_value, Sdefault_value, 1, 1, 0,
1380 doc: /* Return SYMBOL's default value.
1381 This is the value that is seen in buffers that do not have their own values
1382 for this variable. The default value is meaningful for variables with
1383 local bindings in certain buffers. */)
1384 (Lisp_Object symbol)
1386 register Lisp_Object value;
1388 value = default_value (symbol);
1389 if (!EQ (value, Qunbound))
1390 return value;
1392 xsignal1 (Qvoid_variable, symbol);
1395 DEFUN ("set-default", Fset_default, Sset_default, 2, 2, 0,
1396 doc: /* Set SYMBOL's default value to VALUE. SYMBOL and VALUE are evaluated.
1397 The default value is seen in buffers that do not have their own values
1398 for this variable. */)
1399 (Lisp_Object symbol, Lisp_Object value)
1401 struct Lisp_Symbol *sym;
1403 CHECK_SYMBOL (symbol);
1404 if (SYMBOL_CONSTANT_P (symbol))
1406 if (NILP (Fkeywordp (symbol))
1407 || !EQ (value, Fdefault_value (symbol)))
1408 xsignal1 (Qsetting_constant, symbol);
1409 else
1410 /* Allow setting keywords to their own value. */
1411 return value;
1413 sym = XSYMBOL (symbol);
1415 start:
1416 switch (sym->redirect)
1418 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1419 case SYMBOL_PLAINVAL: return Fset (symbol, value);
1420 case SYMBOL_LOCALIZED:
1422 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1424 /* Store new value into the DEFAULT-VALUE slot. */
1425 XSETCDR (blv->defcell, value);
1427 /* If the default binding is now loaded, set the REALVALUE slot too. */
1428 if (blv->fwd && EQ (blv->defcell, blv->valcell))
1429 store_symval_forwarding (blv->fwd, value, NULL);
1430 return value;
1432 case SYMBOL_FORWARDED:
1434 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
1436 /* Handle variables like case-fold-search that have special slots
1437 in the buffer.
1438 Make them work apparently like Lisp_Buffer_Local_Value variables. */
1439 if (BUFFER_OBJFWDP (valcontents))
1441 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1442 int idx = PER_BUFFER_IDX (offset);
1444 set_per_buffer_default (offset, value);
1446 /* If this variable is not always local in all buffers,
1447 set it in the buffers that don't nominally have a local value. */
1448 if (idx > 0)
1450 struct buffer *b;
1452 FOR_EACH_BUFFER (b)
1453 if (!PER_BUFFER_VALUE_P (b, idx))
1454 set_per_buffer_value (b, offset, value);
1456 return value;
1458 else
1459 return Fset (symbol, value);
1461 default: emacs_abort ();
1465 DEFUN ("setq-default", Fsetq_default, Ssetq_default, 0, UNEVALLED, 0,
1466 doc: /* Set the default value of variable VAR to VALUE.
1467 VAR, the variable name, is literal (not evaluated);
1468 VALUE is an expression: it is evaluated and its value returned.
1469 The default value of a variable is seen in buffers
1470 that do not have their own values for the variable.
1472 More generally, you can use multiple variables and values, as in
1473 (setq-default VAR VALUE VAR VALUE...)
1474 This sets each VAR's default value to the corresponding VALUE.
1475 The VALUE for the Nth VAR can refer to the new default values
1476 of previous VARs.
1477 usage: (setq-default [VAR VALUE]...) */)
1478 (Lisp_Object args)
1480 register Lisp_Object args_left;
1481 register Lisp_Object val, symbol;
1482 struct gcpro gcpro1;
1484 if (NILP (args))
1485 return Qnil;
1487 args_left = args;
1488 GCPRO1 (args);
1492 val = eval_sub (Fcar (Fcdr (args_left)));
1493 symbol = XCAR (args_left);
1494 Fset_default (symbol, val);
1495 args_left = Fcdr (XCDR (args_left));
1497 while (!NILP (args_left));
1499 UNGCPRO;
1500 return val;
1503 /* Lisp functions for creating and removing buffer-local variables. */
1505 union Lisp_Val_Fwd
1507 Lisp_Object value;
1508 union Lisp_Fwd *fwd;
1511 static struct Lisp_Buffer_Local_Value *
1512 make_blv (struct Lisp_Symbol *sym, bool forwarded,
1513 union Lisp_Val_Fwd valcontents)
1515 struct Lisp_Buffer_Local_Value *blv = xmalloc (sizeof *blv);
1516 Lisp_Object symbol;
1517 Lisp_Object tem;
1519 XSETSYMBOL (symbol, sym);
1520 tem = Fcons (symbol, (forwarded
1521 ? do_symval_forwarding (valcontents.fwd)
1522 : valcontents.value));
1524 /* Buffer_Local_Values cannot have as realval a buffer-local
1525 or keyboard-local forwarding. */
1526 eassert (!(forwarded && BUFFER_OBJFWDP (valcontents.fwd)));
1527 eassert (!(forwarded && KBOARD_OBJFWDP (valcontents.fwd)));
1528 blv->fwd = forwarded ? valcontents.fwd : NULL;
1529 set_blv_where (blv, Qnil);
1530 blv->frame_local = 0;
1531 blv->local_if_set = 0;
1532 set_blv_defcell (blv, tem);
1533 set_blv_valcell (blv, tem);
1534 set_blv_found (blv, 0);
1535 return blv;
1538 DEFUN ("make-variable-buffer-local", Fmake_variable_buffer_local,
1539 Smake_variable_buffer_local, 1, 1, "vMake Variable Buffer Local: ",
1540 doc: /* Make VARIABLE become buffer-local whenever it is set.
1541 At any time, the value for the current buffer is in effect,
1542 unless the variable has never been set in this buffer,
1543 in which case the default value is in effect.
1544 Note that binding the variable with `let', or setting it while
1545 a `let'-style binding made in this buffer is in effect,
1546 does not make the variable buffer-local. Return VARIABLE.
1548 In most cases it is better to use `make-local-variable',
1549 which makes a variable local in just one buffer.
1551 The function `default-value' gets the default value and `set-default' sets it. */)
1552 (register Lisp_Object variable)
1554 struct Lisp_Symbol *sym;
1555 struct Lisp_Buffer_Local_Value *blv = NULL;
1556 union Lisp_Val_Fwd valcontents IF_LINT (= {LISP_INITIALLY_ZERO});
1557 bool forwarded IF_LINT (= 0);
1559 CHECK_SYMBOL (variable);
1560 sym = XSYMBOL (variable);
1562 start:
1563 switch (sym->redirect)
1565 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1566 case SYMBOL_PLAINVAL:
1567 forwarded = 0; valcontents.value = SYMBOL_VAL (sym);
1568 if (EQ (valcontents.value, Qunbound))
1569 valcontents.value = Qnil;
1570 break;
1571 case SYMBOL_LOCALIZED:
1572 blv = SYMBOL_BLV (sym);
1573 if (blv->frame_local)
1574 error ("Symbol %s may not be buffer-local",
1575 SDATA (SYMBOL_NAME (variable)));
1576 break;
1577 case SYMBOL_FORWARDED:
1578 forwarded = 1; valcontents.fwd = SYMBOL_FWD (sym);
1579 if (KBOARD_OBJFWDP (valcontents.fwd))
1580 error ("Symbol %s may not be buffer-local",
1581 SDATA (SYMBOL_NAME (variable)));
1582 else if (BUFFER_OBJFWDP (valcontents.fwd))
1583 return variable;
1584 break;
1585 default: emacs_abort ();
1588 if (sym->constant)
1589 error ("Symbol %s may not be buffer-local", SDATA (SYMBOL_NAME (variable)));
1591 if (!blv)
1593 blv = make_blv (sym, forwarded, valcontents);
1594 sym->redirect = SYMBOL_LOCALIZED;
1595 SET_SYMBOL_BLV (sym, blv);
1597 Lisp_Object symbol;
1598 XSETSYMBOL (symbol, sym); /* In case `variable' is aliased. */
1599 if (let_shadows_global_binding_p (symbol))
1600 message ("Making %s buffer-local while let-bound!",
1601 SDATA (SYMBOL_NAME (variable)));
1605 blv->local_if_set = 1;
1606 return variable;
1609 DEFUN ("make-local-variable", Fmake_local_variable, Smake_local_variable,
1610 1, 1, "vMake Local Variable: ",
1611 doc: /* Make VARIABLE have a separate value in the current buffer.
1612 Other buffers will continue to share a common default value.
1613 \(The buffer-local value of VARIABLE starts out as the same value
1614 VARIABLE previously had. If VARIABLE was void, it remains void.\)
1615 Return VARIABLE.
1617 If the variable is already arranged to become local when set,
1618 this function causes a local value to exist for this buffer,
1619 just as setting the variable would do.
1621 This function returns VARIABLE, and therefore
1622 (set (make-local-variable 'VARIABLE) VALUE-EXP)
1623 works.
1625 See also `make-variable-buffer-local'.
1627 Do not use `make-local-variable' to make a hook variable buffer-local.
1628 Instead, use `add-hook' and specify t for the LOCAL argument. */)
1629 (Lisp_Object variable)
1631 Lisp_Object tem;
1632 bool forwarded IF_LINT (= 0);
1633 union Lisp_Val_Fwd valcontents IF_LINT (= {LISP_INITIALLY_ZERO});
1634 struct Lisp_Symbol *sym;
1635 struct Lisp_Buffer_Local_Value *blv = NULL;
1637 CHECK_SYMBOL (variable);
1638 sym = XSYMBOL (variable);
1640 start:
1641 switch (sym->redirect)
1643 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1644 case SYMBOL_PLAINVAL:
1645 forwarded = 0; valcontents.value = SYMBOL_VAL (sym); break;
1646 case SYMBOL_LOCALIZED:
1647 blv = SYMBOL_BLV (sym);
1648 if (blv->frame_local)
1649 error ("Symbol %s may not be buffer-local",
1650 SDATA (SYMBOL_NAME (variable)));
1651 break;
1652 case SYMBOL_FORWARDED:
1653 forwarded = 1; valcontents.fwd = SYMBOL_FWD (sym);
1654 if (KBOARD_OBJFWDP (valcontents.fwd))
1655 error ("Symbol %s may not be buffer-local",
1656 SDATA (SYMBOL_NAME (variable)));
1657 break;
1658 default: emacs_abort ();
1661 if (sym->constant)
1662 error ("Symbol %s may not be buffer-local",
1663 SDATA (SYMBOL_NAME (variable)));
1665 if (blv ? blv->local_if_set
1666 : (forwarded && BUFFER_OBJFWDP (valcontents.fwd)))
1668 tem = Fboundp (variable);
1669 /* Make sure the symbol has a local value in this particular buffer,
1670 by setting it to the same value it already has. */
1671 Fset (variable, (EQ (tem, Qt) ? Fsymbol_value (variable) : Qunbound));
1672 return variable;
1674 if (!blv)
1676 blv = make_blv (sym, forwarded, valcontents);
1677 sym->redirect = SYMBOL_LOCALIZED;
1678 SET_SYMBOL_BLV (sym, blv);
1680 Lisp_Object symbol;
1681 XSETSYMBOL (symbol, sym); /* In case `variable' is aliased. */
1682 if (let_shadows_global_binding_p (symbol))
1683 message ("Making %s local to %s while let-bound!",
1684 SDATA (SYMBOL_NAME (variable)),
1685 SDATA (BVAR (current_buffer, name)));
1689 /* Make sure this buffer has its own value of symbol. */
1690 XSETSYMBOL (variable, sym); /* Update in case of aliasing. */
1691 tem = Fassq (variable, BVAR (current_buffer, local_var_alist));
1692 if (NILP (tem))
1694 if (let_shadows_buffer_binding_p (sym))
1695 message ("Making %s buffer-local while locally let-bound!",
1696 SDATA (SYMBOL_NAME (variable)));
1698 /* Swap out any local binding for some other buffer, and make
1699 sure the current value is permanently recorded, if it's the
1700 default value. */
1701 find_symbol_value (variable);
1703 bset_local_var_alist
1704 (current_buffer,
1705 Fcons (Fcons (variable, XCDR (blv->defcell)),
1706 BVAR (current_buffer, local_var_alist)));
1708 /* Make sure symbol does not think it is set up for this buffer;
1709 force it to look once again for this buffer's value. */
1710 if (current_buffer == XBUFFER (blv->where))
1711 set_blv_where (blv, Qnil);
1712 set_blv_found (blv, 0);
1715 /* If the symbol forwards into a C variable, then load the binding
1716 for this buffer now. If C code modifies the variable before we
1717 load the binding in, then that new value will clobber the default
1718 binding the next time we unload it. */
1719 if (blv->fwd)
1720 swap_in_symval_forwarding (sym, blv);
1722 return variable;
1725 DEFUN ("kill-local-variable", Fkill_local_variable, Skill_local_variable,
1726 1, 1, "vKill Local Variable: ",
1727 doc: /* Make VARIABLE no longer have a separate value in the current buffer.
1728 From now on the default value will apply in this buffer. Return VARIABLE. */)
1729 (register Lisp_Object variable)
1731 register Lisp_Object tem;
1732 struct Lisp_Buffer_Local_Value *blv;
1733 struct Lisp_Symbol *sym;
1735 CHECK_SYMBOL (variable);
1736 sym = XSYMBOL (variable);
1738 start:
1739 switch (sym->redirect)
1741 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1742 case SYMBOL_PLAINVAL: return variable;
1743 case SYMBOL_FORWARDED:
1745 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
1746 if (BUFFER_OBJFWDP (valcontents))
1748 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1749 int idx = PER_BUFFER_IDX (offset);
1751 if (idx > 0)
1753 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 0);
1754 set_per_buffer_value (current_buffer, offset,
1755 per_buffer_default (offset));
1758 return variable;
1760 case SYMBOL_LOCALIZED:
1761 blv = SYMBOL_BLV (sym);
1762 if (blv->frame_local)
1763 return variable;
1764 break;
1765 default: emacs_abort ();
1768 /* Get rid of this buffer's alist element, if any. */
1769 XSETSYMBOL (variable, sym); /* Propagate variable indirection. */
1770 tem = Fassq (variable, BVAR (current_buffer, local_var_alist));
1771 if (!NILP (tem))
1772 bset_local_var_alist
1773 (current_buffer,
1774 Fdelq (tem, BVAR (current_buffer, local_var_alist)));
1776 /* If the symbol is set up with the current buffer's binding
1777 loaded, recompute its value. We have to do it now, or else
1778 forwarded objects won't work right. */
1780 Lisp_Object buf; XSETBUFFER (buf, current_buffer);
1781 if (EQ (buf, blv->where))
1783 set_blv_where (blv, Qnil);
1784 blv->found = 0;
1785 find_symbol_value (variable);
1789 return variable;
1792 /* Lisp functions for creating and removing buffer-local variables. */
1794 /* Obsolete since 22.2. NB adjust doc of modify-frame-parameters
1795 when/if this is removed. */
1797 DEFUN ("make-variable-frame-local", Fmake_variable_frame_local, Smake_variable_frame_local,
1798 1, 1, "vMake Variable Frame Local: ",
1799 doc: /* Enable VARIABLE to have frame-local bindings.
1800 This does not create any frame-local bindings for VARIABLE,
1801 it just makes them possible.
1803 A frame-local binding is actually a frame parameter value.
1804 If a frame F has a value for the frame parameter named VARIABLE,
1805 that also acts as a frame-local binding for VARIABLE in F--
1806 provided this function has been called to enable VARIABLE
1807 to have frame-local bindings at all.
1809 The only way to create a frame-local binding for VARIABLE in a frame
1810 is to set the VARIABLE frame parameter of that frame. See
1811 `modify-frame-parameters' for how to set frame parameters.
1813 Note that since Emacs 23.1, variables cannot be both buffer-local and
1814 frame-local any more (buffer-local bindings used to take precedence over
1815 frame-local bindings). */)
1816 (Lisp_Object variable)
1818 bool forwarded;
1819 union Lisp_Val_Fwd valcontents;
1820 struct Lisp_Symbol *sym;
1821 struct Lisp_Buffer_Local_Value *blv = NULL;
1823 CHECK_SYMBOL (variable);
1824 sym = XSYMBOL (variable);
1826 start:
1827 switch (sym->redirect)
1829 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1830 case SYMBOL_PLAINVAL:
1831 forwarded = 0; valcontents.value = SYMBOL_VAL (sym);
1832 if (EQ (valcontents.value, Qunbound))
1833 valcontents.value = Qnil;
1834 break;
1835 case SYMBOL_LOCALIZED:
1836 if (SYMBOL_BLV (sym)->frame_local)
1837 return variable;
1838 else
1839 error ("Symbol %s may not be frame-local",
1840 SDATA (SYMBOL_NAME (variable)));
1841 case SYMBOL_FORWARDED:
1842 forwarded = 1; valcontents.fwd = SYMBOL_FWD (sym);
1843 if (KBOARD_OBJFWDP (valcontents.fwd) || BUFFER_OBJFWDP (valcontents.fwd))
1844 error ("Symbol %s may not be frame-local",
1845 SDATA (SYMBOL_NAME (variable)));
1846 break;
1847 default: emacs_abort ();
1850 if (sym->constant)
1851 error ("Symbol %s may not be frame-local", SDATA (SYMBOL_NAME (variable)));
1853 blv = make_blv (sym, forwarded, valcontents);
1854 blv->frame_local = 1;
1855 sym->redirect = SYMBOL_LOCALIZED;
1856 SET_SYMBOL_BLV (sym, blv);
1858 Lisp_Object symbol;
1859 XSETSYMBOL (symbol, sym); /* In case `variable' is aliased. */
1860 if (let_shadows_global_binding_p (symbol))
1861 message ("Making %s frame-local while let-bound!",
1862 SDATA (SYMBOL_NAME (variable)));
1864 return variable;
1867 DEFUN ("local-variable-p", Flocal_variable_p, Slocal_variable_p,
1868 1, 2, 0,
1869 doc: /* Non-nil if VARIABLE has a local binding in buffer BUFFER.
1870 BUFFER defaults to the current buffer. */)
1871 (register Lisp_Object variable, Lisp_Object buffer)
1873 register struct buffer *buf;
1874 struct Lisp_Symbol *sym;
1876 if (NILP (buffer))
1877 buf = current_buffer;
1878 else
1880 CHECK_BUFFER (buffer);
1881 buf = XBUFFER (buffer);
1884 CHECK_SYMBOL (variable);
1885 sym = XSYMBOL (variable);
1887 start:
1888 switch (sym->redirect)
1890 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1891 case SYMBOL_PLAINVAL: return Qnil;
1892 case SYMBOL_LOCALIZED:
1894 Lisp_Object tail, elt, tmp;
1895 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1896 XSETBUFFER (tmp, buf);
1897 XSETSYMBOL (variable, sym); /* Update in case of aliasing. */
1899 if (EQ (blv->where, tmp)) /* The binding is already loaded. */
1900 return blv_found (blv) ? Qt : Qnil;
1901 else
1902 for (tail = BVAR (buf, local_var_alist); CONSP (tail); tail = XCDR (tail))
1904 elt = XCAR (tail);
1905 if (EQ (variable, XCAR (elt)))
1907 eassert (!blv->frame_local);
1908 return Qt;
1911 return Qnil;
1913 case SYMBOL_FORWARDED:
1915 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
1916 if (BUFFER_OBJFWDP (valcontents))
1918 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1919 int idx = PER_BUFFER_IDX (offset);
1920 if (idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
1921 return Qt;
1923 return Qnil;
1925 default: emacs_abort ();
1929 DEFUN ("local-variable-if-set-p", Flocal_variable_if_set_p, Slocal_variable_if_set_p,
1930 1, 2, 0,
1931 doc: /* Non-nil if VARIABLE is local in buffer BUFFER when set there.
1932 BUFFER defaults to the current buffer.
1934 More precisely, return non-nil if either VARIABLE already has a local
1935 value in BUFFER, or if VARIABLE is automatically buffer-local (see
1936 `make-variable-buffer-local'). */)
1937 (register Lisp_Object variable, Lisp_Object buffer)
1939 struct Lisp_Symbol *sym;
1941 CHECK_SYMBOL (variable);
1942 sym = XSYMBOL (variable);
1944 start:
1945 switch (sym->redirect)
1947 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1948 case SYMBOL_PLAINVAL: return Qnil;
1949 case SYMBOL_LOCALIZED:
1951 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1952 if (blv->local_if_set)
1953 return Qt;
1954 XSETSYMBOL (variable, sym); /* Update in case of aliasing. */
1955 return Flocal_variable_p (variable, buffer);
1957 case SYMBOL_FORWARDED:
1958 /* All BUFFER_OBJFWD slots become local if they are set. */
1959 return (BUFFER_OBJFWDP (SYMBOL_FWD (sym)) ? Qt : Qnil);
1960 default: emacs_abort ();
1964 DEFUN ("variable-binding-locus", Fvariable_binding_locus, Svariable_binding_locus,
1965 1, 1, 0,
1966 doc: /* Return a value indicating where VARIABLE's current binding comes from.
1967 If the current binding is buffer-local, the value is the current buffer.
1968 If the current binding is frame-local, the value is the selected frame.
1969 If the current binding is global (the default), the value is nil. */)
1970 (register Lisp_Object variable)
1972 struct Lisp_Symbol *sym;
1974 CHECK_SYMBOL (variable);
1975 sym = XSYMBOL (variable);
1977 /* Make sure the current binding is actually swapped in. */
1978 find_symbol_value (variable);
1980 start:
1981 switch (sym->redirect)
1983 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1984 case SYMBOL_PLAINVAL: return Qnil;
1985 case SYMBOL_FORWARDED:
1987 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
1988 if (KBOARD_OBJFWDP (valcontents))
1989 return Fframe_terminal (Fselected_frame ());
1990 else if (!BUFFER_OBJFWDP (valcontents))
1991 return Qnil;
1993 /* FALLTHROUGH */
1994 case SYMBOL_LOCALIZED:
1995 /* For a local variable, record both the symbol and which
1996 buffer's or frame's value we are saving. */
1997 if (!NILP (Flocal_variable_p (variable, Qnil)))
1998 return Fcurrent_buffer ();
1999 else if (sym->redirect == SYMBOL_LOCALIZED
2000 && blv_found (SYMBOL_BLV (sym)))
2001 return SYMBOL_BLV (sym)->where;
2002 else
2003 return Qnil;
2004 default: emacs_abort ();
2008 /* This code is disabled now that we use the selected frame to return
2009 keyboard-local-values. */
2010 #if 0
2011 extern struct terminal *get_terminal (Lisp_Object display, int);
2013 DEFUN ("terminal-local-value", Fterminal_local_value,
2014 Sterminal_local_value, 2, 2, 0,
2015 doc: /* Return the terminal-local value of SYMBOL on TERMINAL.
2016 If SYMBOL is not a terminal-local variable, then return its normal
2017 value, like `symbol-value'.
2019 TERMINAL may be a terminal object, a frame, or nil (meaning the
2020 selected frame's terminal device). */)
2021 (Lisp_Object symbol, Lisp_Object terminal)
2023 Lisp_Object result;
2024 struct terminal *t = get_terminal (terminal, 1);
2025 push_kboard (t->kboard);
2026 result = Fsymbol_value (symbol);
2027 pop_kboard ();
2028 return result;
2031 DEFUN ("set-terminal-local-value", Fset_terminal_local_value,
2032 Sset_terminal_local_value, 3, 3, 0,
2033 doc: /* Set the terminal-local binding of SYMBOL on TERMINAL to VALUE.
2034 If VARIABLE is not a terminal-local variable, then set its normal
2035 binding, like `set'.
2037 TERMINAL may be a terminal object, a frame, or nil (meaning the
2038 selected frame's terminal device). */)
2039 (Lisp_Object symbol, Lisp_Object terminal, Lisp_Object value)
2041 Lisp_Object result;
2042 struct terminal *t = get_terminal (terminal, 1);
2043 push_kboard (d->kboard);
2044 result = Fset (symbol, value);
2045 pop_kboard ();
2046 return result;
2048 #endif
2050 /* Find the function at the end of a chain of symbol function indirections. */
2052 /* If OBJECT is a symbol, find the end of its function chain and
2053 return the value found there. If OBJECT is not a symbol, just
2054 return it. If there is a cycle in the function chain, signal a
2055 cyclic-function-indirection error.
2057 This is like Findirect_function, except that it doesn't signal an
2058 error if the chain ends up unbound. */
2059 Lisp_Object
2060 indirect_function (register Lisp_Object object)
2062 Lisp_Object tortoise, hare;
2064 hare = tortoise = object;
2066 for (;;)
2068 if (!SYMBOLP (hare) || NILP (hare))
2069 break;
2070 hare = XSYMBOL (hare)->function;
2071 if (!SYMBOLP (hare) || NILP (hare))
2072 break;
2073 hare = XSYMBOL (hare)->function;
2075 tortoise = XSYMBOL (tortoise)->function;
2077 if (EQ (hare, tortoise))
2078 xsignal1 (Qcyclic_function_indirection, object);
2081 return hare;
2084 DEFUN ("indirect-function", Findirect_function, Sindirect_function, 1, 2, 0,
2085 doc: /* Return the function at the end of OBJECT's function chain.
2086 If OBJECT is not a symbol, just return it. Otherwise, follow all
2087 function indirections to find the final function binding and return it.
2088 If the final symbol in the chain is unbound, signal a void-function error.
2089 Optional arg NOERROR non-nil means to return nil instead of signaling.
2090 Signal a cyclic-function-indirection error if there is a loop in the
2091 function chain of symbols. */)
2092 (register Lisp_Object object, Lisp_Object noerror)
2094 Lisp_Object result;
2096 /* Optimize for no indirection. */
2097 result = object;
2098 if (SYMBOLP (result) && !NILP (result)
2099 && (result = XSYMBOL (result)->function, SYMBOLP (result)))
2100 result = indirect_function (result);
2101 if (!NILP (result))
2102 return result;
2104 if (NILP (noerror))
2105 xsignal1 (Qvoid_function, object);
2107 return Qnil;
2110 /* Extract and set vector and string elements. */
2112 DEFUN ("aref", Faref, Saref, 2, 2, 0,
2113 doc: /* Return the element of ARRAY at index IDX.
2114 ARRAY may be a vector, a string, a char-table, a bool-vector,
2115 or a byte-code object. IDX starts at 0. */)
2116 (register Lisp_Object array, Lisp_Object idx)
2118 register EMACS_INT idxval;
2120 CHECK_NUMBER (idx);
2121 idxval = XINT (idx);
2122 if (STRINGP (array))
2124 int c;
2125 ptrdiff_t idxval_byte;
2127 if (idxval < 0 || idxval >= SCHARS (array))
2128 args_out_of_range (array, idx);
2129 if (! STRING_MULTIBYTE (array))
2130 return make_number ((unsigned char) SREF (array, idxval));
2131 idxval_byte = string_char_to_byte (array, idxval);
2133 c = STRING_CHAR (SDATA (array) + idxval_byte);
2134 return make_number (c);
2136 else if (BOOL_VECTOR_P (array))
2138 int val;
2140 if (idxval < 0 || idxval >= XBOOL_VECTOR (array)->size)
2141 args_out_of_range (array, idx);
2143 val = (unsigned char) XBOOL_VECTOR (array)->data[idxval / BOOL_VECTOR_BITS_PER_CHAR];
2144 return (val & (1 << (idxval % BOOL_VECTOR_BITS_PER_CHAR)) ? Qt : Qnil);
2146 else if (CHAR_TABLE_P (array))
2148 CHECK_CHARACTER (idx);
2149 return CHAR_TABLE_REF (array, idxval);
2151 else
2153 ptrdiff_t size = 0;
2154 if (VECTORP (array))
2155 size = ASIZE (array);
2156 else if (COMPILEDP (array))
2157 size = ASIZE (array) & PSEUDOVECTOR_SIZE_MASK;
2158 else
2159 wrong_type_argument (Qarrayp, array);
2161 if (idxval < 0 || idxval >= size)
2162 args_out_of_range (array, idx);
2163 return AREF (array, idxval);
2167 DEFUN ("aset", Faset, Saset, 3, 3, 0,
2168 doc: /* Store into the element of ARRAY at index IDX the value NEWELT.
2169 Return NEWELT. ARRAY may be a vector, a string, a char-table or a
2170 bool-vector. IDX starts at 0. */)
2171 (register Lisp_Object array, Lisp_Object idx, Lisp_Object newelt)
2173 register EMACS_INT idxval;
2175 CHECK_NUMBER (idx);
2176 idxval = XINT (idx);
2177 CHECK_ARRAY (array, Qarrayp);
2178 CHECK_IMPURE (array);
2180 if (VECTORP (array))
2182 if (idxval < 0 || idxval >= ASIZE (array))
2183 args_out_of_range (array, idx);
2184 ASET (array, idxval, newelt);
2186 else if (BOOL_VECTOR_P (array))
2188 int val;
2190 if (idxval < 0 || idxval >= XBOOL_VECTOR (array)->size)
2191 args_out_of_range (array, idx);
2193 val = (unsigned char) XBOOL_VECTOR (array)->data[idxval / BOOL_VECTOR_BITS_PER_CHAR];
2195 if (! NILP (newelt))
2196 val |= 1 << (idxval % BOOL_VECTOR_BITS_PER_CHAR);
2197 else
2198 val &= ~(1 << (idxval % BOOL_VECTOR_BITS_PER_CHAR));
2199 XBOOL_VECTOR (array)->data[idxval / BOOL_VECTOR_BITS_PER_CHAR] = val;
2201 else if (CHAR_TABLE_P (array))
2203 CHECK_CHARACTER (idx);
2204 CHAR_TABLE_SET (array, idxval, newelt);
2206 else
2208 int c;
2210 if (idxval < 0 || idxval >= SCHARS (array))
2211 args_out_of_range (array, idx);
2212 CHECK_CHARACTER (newelt);
2213 c = XFASTINT (newelt);
2215 if (STRING_MULTIBYTE (array))
2217 ptrdiff_t idxval_byte, nbytes;
2218 int prev_bytes, new_bytes;
2219 unsigned char workbuf[MAX_MULTIBYTE_LENGTH], *p0 = workbuf, *p1;
2221 nbytes = SBYTES (array);
2222 idxval_byte = string_char_to_byte (array, idxval);
2223 p1 = SDATA (array) + idxval_byte;
2224 prev_bytes = BYTES_BY_CHAR_HEAD (*p1);
2225 new_bytes = CHAR_STRING (c, p0);
2226 if (prev_bytes != new_bytes)
2228 /* We must relocate the string data. */
2229 ptrdiff_t nchars = SCHARS (array);
2230 USE_SAFE_ALLOCA;
2231 unsigned char *str = SAFE_ALLOCA (nbytes);
2233 memcpy (str, SDATA (array), nbytes);
2234 allocate_string_data (XSTRING (array), nchars,
2235 nbytes + new_bytes - prev_bytes);
2236 memcpy (SDATA (array), str, idxval_byte);
2237 p1 = SDATA (array) + idxval_byte;
2238 memcpy (p1 + new_bytes, str + idxval_byte + prev_bytes,
2239 nbytes - (idxval_byte + prev_bytes));
2240 SAFE_FREE ();
2241 clear_string_char_byte_cache ();
2243 while (new_bytes--)
2244 *p1++ = *p0++;
2246 else
2248 if (! SINGLE_BYTE_CHAR_P (c))
2250 int i;
2252 for (i = SBYTES (array) - 1; i >= 0; i--)
2253 if (SREF (array, i) >= 0x80)
2254 args_out_of_range (array, newelt);
2255 /* ARRAY is an ASCII string. Convert it to a multibyte
2256 string, and try `aset' again. */
2257 STRING_SET_MULTIBYTE (array);
2258 return Faset (array, idx, newelt);
2260 SSET (array, idxval, c);
2264 return newelt;
2267 /* Arithmetic functions */
2269 enum comparison { equal, notequal, less, grtr, less_or_equal, grtr_or_equal };
2271 static Lisp_Object
2272 arithcompare (Lisp_Object num1, Lisp_Object num2, enum comparison comparison)
2274 double f1 = 0, f2 = 0;
2275 bool floatp = 0;
2277 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num1);
2278 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num2);
2280 if (FLOATP (num1) || FLOATP (num2))
2282 floatp = 1;
2283 f1 = (FLOATP (num1)) ? XFLOAT_DATA (num1) : XINT (num1);
2284 f2 = (FLOATP (num2)) ? XFLOAT_DATA (num2) : XINT (num2);
2287 switch (comparison)
2289 case equal:
2290 if (floatp ? f1 == f2 : XINT (num1) == XINT (num2))
2291 return Qt;
2292 return Qnil;
2294 case notequal:
2295 if (floatp ? f1 != f2 : XINT (num1) != XINT (num2))
2296 return Qt;
2297 return Qnil;
2299 case less:
2300 if (floatp ? f1 < f2 : XINT (num1) < XINT (num2))
2301 return Qt;
2302 return Qnil;
2304 case less_or_equal:
2305 if (floatp ? f1 <= f2 : XINT (num1) <= XINT (num2))
2306 return Qt;
2307 return Qnil;
2309 case grtr:
2310 if (floatp ? f1 > f2 : XINT (num1) > XINT (num2))
2311 return Qt;
2312 return Qnil;
2314 case grtr_or_equal:
2315 if (floatp ? f1 >= f2 : XINT (num1) >= XINT (num2))
2316 return Qt;
2317 return Qnil;
2319 default:
2320 emacs_abort ();
2324 DEFUN ("=", Feqlsign, Seqlsign, 2, 2, 0,
2325 doc: /* Return t if two args, both numbers or markers, are equal. */)
2326 (register Lisp_Object num1, Lisp_Object num2)
2328 return arithcompare (num1, num2, equal);
2331 DEFUN ("<", Flss, Slss, 2, 2, 0,
2332 doc: /* Return t if first arg is less than second arg. Both must be numbers or markers. */)
2333 (register Lisp_Object num1, Lisp_Object num2)
2335 return arithcompare (num1, num2, less);
2338 DEFUN (">", Fgtr, Sgtr, 2, 2, 0,
2339 doc: /* Return t if first arg is greater than second arg. Both must be numbers or markers. */)
2340 (register Lisp_Object num1, Lisp_Object num2)
2342 return arithcompare (num1, num2, grtr);
2345 DEFUN ("<=", Fleq, Sleq, 2, 2, 0,
2346 doc: /* Return t if first arg is less than or equal to second arg.
2347 Both must be numbers or markers. */)
2348 (register Lisp_Object num1, Lisp_Object num2)
2350 return arithcompare (num1, num2, less_or_equal);
2353 DEFUN (">=", Fgeq, Sgeq, 2, 2, 0,
2354 doc: /* Return t if first arg is greater than or equal to second arg.
2355 Both must be numbers or markers. */)
2356 (register Lisp_Object num1, Lisp_Object num2)
2358 return arithcompare (num1, num2, grtr_or_equal);
2361 DEFUN ("/=", Fneq, Sneq, 2, 2, 0,
2362 doc: /* Return t if first arg is not equal to second arg. Both must be numbers or markers. */)
2363 (register Lisp_Object num1, Lisp_Object num2)
2365 return arithcompare (num1, num2, notequal);
2368 DEFUN ("zerop", Fzerop, Szerop, 1, 1, 0,
2369 doc: /* Return t if NUMBER is zero. */)
2370 (register Lisp_Object number)
2372 CHECK_NUMBER_OR_FLOAT (number);
2374 if (FLOATP (number))
2376 if (XFLOAT_DATA (number) == 0.0)
2377 return Qt;
2378 return Qnil;
2381 if (!XINT (number))
2382 return Qt;
2383 return Qnil;
2386 /* Convert the cons-of-integers, integer, or float value C to an
2387 unsigned value with maximum value MAX. Signal an error if C does not
2388 have a valid format or is out of range. */
2389 uintmax_t
2390 cons_to_unsigned (Lisp_Object c, uintmax_t max)
2392 bool valid = 0;
2393 uintmax_t val IF_LINT (= 0);
2394 if (INTEGERP (c))
2396 valid = 0 <= XINT (c);
2397 val = XINT (c);
2399 else if (FLOATP (c))
2401 double d = XFLOAT_DATA (c);
2402 if (0 <= d
2403 && d < (max == UINTMAX_MAX ? (double) UINTMAX_MAX + 1 : max + 1))
2405 val = d;
2406 valid = 1;
2409 else if (CONSP (c) && NATNUMP (XCAR (c)))
2411 uintmax_t top = XFASTINT (XCAR (c));
2412 Lisp_Object rest = XCDR (c);
2413 if (top <= UINTMAX_MAX >> 24 >> 16
2414 && CONSP (rest)
2415 && NATNUMP (XCAR (rest)) && XFASTINT (XCAR (rest)) < 1 << 24
2416 && NATNUMP (XCDR (rest)) && XFASTINT (XCDR (rest)) < 1 << 16)
2418 uintmax_t mid = XFASTINT (XCAR (rest));
2419 val = top << 24 << 16 | mid << 16 | XFASTINT (XCDR (rest));
2420 valid = 1;
2422 else if (top <= UINTMAX_MAX >> 16)
2424 if (CONSP (rest))
2425 rest = XCAR (rest);
2426 if (NATNUMP (rest) && XFASTINT (rest) < 1 << 16)
2428 val = top << 16 | XFASTINT (rest);
2429 valid = 1;
2434 if (! (valid && val <= max))
2435 error ("Not an in-range integer, float, or cons of integers");
2436 return val;
2439 /* Convert the cons-of-integers, integer, or float value C to a signed
2440 value with extrema MIN and MAX. Signal an error if C does not have
2441 a valid format or is out of range. */
2442 intmax_t
2443 cons_to_signed (Lisp_Object c, intmax_t min, intmax_t max)
2445 bool valid = 0;
2446 intmax_t val IF_LINT (= 0);
2447 if (INTEGERP (c))
2449 val = XINT (c);
2450 valid = 1;
2452 else if (FLOATP (c))
2454 double d = XFLOAT_DATA (c);
2455 if (min <= d
2456 && d < (max == INTMAX_MAX ? (double) INTMAX_MAX + 1 : max + 1))
2458 val = d;
2459 valid = 1;
2462 else if (CONSP (c) && INTEGERP (XCAR (c)))
2464 intmax_t top = XINT (XCAR (c));
2465 Lisp_Object rest = XCDR (c);
2466 if (INTMAX_MIN >> 24 >> 16 <= top && top <= INTMAX_MAX >> 24 >> 16
2467 && CONSP (rest)
2468 && NATNUMP (XCAR (rest)) && XFASTINT (XCAR (rest)) < 1 << 24
2469 && NATNUMP (XCDR (rest)) && XFASTINT (XCDR (rest)) < 1 << 16)
2471 intmax_t mid = XFASTINT (XCAR (rest));
2472 val = top << 24 << 16 | mid << 16 | XFASTINT (XCDR (rest));
2473 valid = 1;
2475 else if (INTMAX_MIN >> 16 <= top && top <= INTMAX_MAX >> 16)
2477 if (CONSP (rest))
2478 rest = XCAR (rest);
2479 if (NATNUMP (rest) && XFASTINT (rest) < 1 << 16)
2481 val = top << 16 | XFASTINT (rest);
2482 valid = 1;
2487 if (! (valid && min <= val && val <= max))
2488 error ("Not an in-range integer, float, or cons of integers");
2489 return val;
2492 DEFUN ("number-to-string", Fnumber_to_string, Snumber_to_string, 1, 1, 0,
2493 doc: /* Return the decimal representation of NUMBER as a string.
2494 Uses a minus sign if negative.
2495 NUMBER may be an integer or a floating point number. */)
2496 (Lisp_Object number)
2498 char buffer[max (FLOAT_TO_STRING_BUFSIZE, INT_BUFSIZE_BOUND (EMACS_INT))];
2499 int len;
2501 CHECK_NUMBER_OR_FLOAT (number);
2503 if (FLOATP (number))
2504 len = float_to_string (buffer, XFLOAT_DATA (number));
2505 else
2506 len = sprintf (buffer, "%"pI"d", XINT (number));
2508 return make_unibyte_string (buffer, len);
2511 DEFUN ("string-to-number", Fstring_to_number, Sstring_to_number, 1, 2, 0,
2512 doc: /* Parse STRING as a decimal number and return the number.
2513 This parses both integers and floating point numbers.
2514 It ignores leading spaces and tabs, and all trailing chars.
2516 If BASE, interpret STRING as a number in that base. If BASE isn't
2517 present, base 10 is used. BASE must be between 2 and 16 (inclusive).
2518 If the base used is not 10, STRING is always parsed as integer. */)
2519 (register Lisp_Object string, Lisp_Object base)
2521 register char *p;
2522 register int b;
2523 Lisp_Object val;
2525 CHECK_STRING (string);
2527 if (NILP (base))
2528 b = 10;
2529 else
2531 CHECK_NUMBER (base);
2532 if (! (2 <= XINT (base) && XINT (base) <= 16))
2533 xsignal1 (Qargs_out_of_range, base);
2534 b = XINT (base);
2537 p = SSDATA (string);
2538 while (*p == ' ' || *p == '\t')
2539 p++;
2541 val = string_to_number (p, b, 1);
2542 return NILP (val) ? make_number (0) : val;
2545 enum arithop
2547 Aadd,
2548 Asub,
2549 Amult,
2550 Adiv,
2551 Alogand,
2552 Alogior,
2553 Alogxor,
2554 Amax,
2555 Amin
2558 static Lisp_Object float_arith_driver (double, ptrdiff_t, enum arithop,
2559 ptrdiff_t, Lisp_Object *);
2560 static Lisp_Object
2561 arith_driver (enum arithop code, ptrdiff_t nargs, Lisp_Object *args)
2563 Lisp_Object val;
2564 ptrdiff_t argnum, ok_args;
2565 EMACS_INT accum = 0;
2566 EMACS_INT next, ok_accum;
2567 bool overflow = 0;
2569 switch (code)
2571 case Alogior:
2572 case Alogxor:
2573 case Aadd:
2574 case Asub:
2575 accum = 0;
2576 break;
2577 case Amult:
2578 accum = 1;
2579 break;
2580 case Alogand:
2581 accum = -1;
2582 break;
2583 default:
2584 break;
2587 for (argnum = 0; argnum < nargs; argnum++)
2589 if (! overflow)
2591 ok_args = argnum;
2592 ok_accum = accum;
2595 /* Using args[argnum] as argument to CHECK_NUMBER_... */
2596 val = args[argnum];
2597 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val);
2599 if (FLOATP (val))
2600 return float_arith_driver (ok_accum, ok_args, code,
2601 nargs, args);
2602 args[argnum] = val;
2603 next = XINT (args[argnum]);
2604 switch (code)
2606 case Aadd:
2607 if (INT_ADD_OVERFLOW (accum, next))
2609 overflow = 1;
2610 accum &= INTMASK;
2612 accum += next;
2613 break;
2614 case Asub:
2615 if (INT_SUBTRACT_OVERFLOW (accum, next))
2617 overflow = 1;
2618 accum &= INTMASK;
2620 accum = argnum ? accum - next : nargs == 1 ? - next : next;
2621 break;
2622 case Amult:
2623 if (INT_MULTIPLY_OVERFLOW (accum, next))
2625 EMACS_UINT a = accum, b = next, ab = a * b;
2626 overflow = 1;
2627 accum = ab & INTMASK;
2629 else
2630 accum *= next;
2631 break;
2632 case Adiv:
2633 if (!argnum)
2634 accum = next;
2635 else
2637 if (next == 0)
2638 xsignal0 (Qarith_error);
2639 accum /= next;
2641 break;
2642 case Alogand:
2643 accum &= next;
2644 break;
2645 case Alogior:
2646 accum |= next;
2647 break;
2648 case Alogxor:
2649 accum ^= next;
2650 break;
2651 case Amax:
2652 if (!argnum || next > accum)
2653 accum = next;
2654 break;
2655 case Amin:
2656 if (!argnum || next < accum)
2657 accum = next;
2658 break;
2662 XSETINT (val, accum);
2663 return val;
2666 #undef isnan
2667 #define isnan(x) ((x) != (x))
2669 static Lisp_Object
2670 float_arith_driver (double accum, ptrdiff_t argnum, enum arithop code,
2671 ptrdiff_t nargs, Lisp_Object *args)
2673 register Lisp_Object val;
2674 double next;
2676 for (; argnum < nargs; argnum++)
2678 val = args[argnum]; /* using args[argnum] as argument to CHECK_NUMBER_... */
2679 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val);
2681 if (FLOATP (val))
2683 next = XFLOAT_DATA (val);
2685 else
2687 args[argnum] = val; /* runs into a compiler bug. */
2688 next = XINT (args[argnum]);
2690 switch (code)
2692 case Aadd:
2693 accum += next;
2694 break;
2695 case Asub:
2696 accum = argnum ? accum - next : nargs == 1 ? - next : next;
2697 break;
2698 case Amult:
2699 accum *= next;
2700 break;
2701 case Adiv:
2702 if (!argnum)
2703 accum = next;
2704 else
2706 if (! IEEE_FLOATING_POINT && next == 0)
2707 xsignal0 (Qarith_error);
2708 accum /= next;
2710 break;
2711 case Alogand:
2712 case Alogior:
2713 case Alogxor:
2714 return wrong_type_argument (Qinteger_or_marker_p, val);
2715 case Amax:
2716 if (!argnum || isnan (next) || next > accum)
2717 accum = next;
2718 break;
2719 case Amin:
2720 if (!argnum || isnan (next) || next < accum)
2721 accum = next;
2722 break;
2726 return make_float (accum);
2730 DEFUN ("+", Fplus, Splus, 0, MANY, 0,
2731 doc: /* Return sum of any number of arguments, which are numbers or markers.
2732 usage: (+ &rest NUMBERS-OR-MARKERS) */)
2733 (ptrdiff_t nargs, Lisp_Object *args)
2735 return arith_driver (Aadd, nargs, args);
2738 DEFUN ("-", Fminus, Sminus, 0, MANY, 0,
2739 doc: /* Negate number or subtract numbers or markers and return the result.
2740 With one arg, negates it. With more than one arg,
2741 subtracts all but the first from the first.
2742 usage: (- &optional NUMBER-OR-MARKER &rest MORE-NUMBERS-OR-MARKERS) */)
2743 (ptrdiff_t nargs, Lisp_Object *args)
2745 return arith_driver (Asub, nargs, args);
2748 DEFUN ("*", Ftimes, Stimes, 0, MANY, 0,
2749 doc: /* Return product of any number of arguments, which are numbers or markers.
2750 usage: (* &rest NUMBERS-OR-MARKERS) */)
2751 (ptrdiff_t nargs, Lisp_Object *args)
2753 return arith_driver (Amult, nargs, args);
2756 DEFUN ("/", Fquo, Squo, 1, MANY, 0,
2757 doc: /* Return first argument divided by all the remaining arguments.
2758 The arguments must be numbers or markers.
2759 usage: (/ DIVIDEND &rest DIVISORS) */)
2760 (ptrdiff_t nargs, Lisp_Object *args)
2762 ptrdiff_t argnum;
2763 for (argnum = 2; argnum < nargs; argnum++)
2764 if (FLOATP (args[argnum]))
2765 return float_arith_driver (0, 0, Adiv, nargs, args);
2766 return arith_driver (Adiv, nargs, args);
2769 DEFUN ("%", Frem, Srem, 2, 2, 0,
2770 doc: /* Return remainder of X divided by Y.
2771 Both must be integers or markers. */)
2772 (register Lisp_Object x, Lisp_Object y)
2774 Lisp_Object val;
2776 CHECK_NUMBER_COERCE_MARKER (x);
2777 CHECK_NUMBER_COERCE_MARKER (y);
2779 if (XINT (y) == 0)
2780 xsignal0 (Qarith_error);
2782 XSETINT (val, XINT (x) % XINT (y));
2783 return val;
2786 DEFUN ("mod", Fmod, Smod, 2, 2, 0,
2787 doc: /* Return X modulo Y.
2788 The result falls between zero (inclusive) and Y (exclusive).
2789 Both X and Y must be numbers or markers. */)
2790 (register Lisp_Object x, Lisp_Object y)
2792 Lisp_Object val;
2793 EMACS_INT i1, i2;
2795 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (x);
2796 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (y);
2798 if (FLOATP (x) || FLOATP (y))
2799 return fmod_float (x, y);
2801 i1 = XINT (x);
2802 i2 = XINT (y);
2804 if (i2 == 0)
2805 xsignal0 (Qarith_error);
2807 i1 %= i2;
2809 /* If the "remainder" comes out with the wrong sign, fix it. */
2810 if (i2 < 0 ? i1 > 0 : i1 < 0)
2811 i1 += i2;
2813 XSETINT (val, i1);
2814 return val;
2817 DEFUN ("max", Fmax, Smax, 1, MANY, 0,
2818 doc: /* Return largest of all the arguments (which must be numbers or markers).
2819 The value is always a number; markers are converted to numbers.
2820 usage: (max NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2821 (ptrdiff_t nargs, Lisp_Object *args)
2823 return arith_driver (Amax, nargs, args);
2826 DEFUN ("min", Fmin, Smin, 1, MANY, 0,
2827 doc: /* Return smallest of all the arguments (which must be numbers or markers).
2828 The value is always a number; markers are converted to numbers.
2829 usage: (min NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2830 (ptrdiff_t nargs, Lisp_Object *args)
2832 return arith_driver (Amin, nargs, args);
2835 DEFUN ("logand", Flogand, Slogand, 0, MANY, 0,
2836 doc: /* Return bitwise-and of all the arguments.
2837 Arguments may be integers, or markers converted to integers.
2838 usage: (logand &rest INTS-OR-MARKERS) */)
2839 (ptrdiff_t nargs, Lisp_Object *args)
2841 return arith_driver (Alogand, nargs, args);
2844 DEFUN ("logior", Flogior, Slogior, 0, MANY, 0,
2845 doc: /* Return bitwise-or of all the arguments.
2846 Arguments may be integers, or markers converted to integers.
2847 usage: (logior &rest INTS-OR-MARKERS) */)
2848 (ptrdiff_t nargs, Lisp_Object *args)
2850 return arith_driver (Alogior, nargs, args);
2853 DEFUN ("logxor", Flogxor, Slogxor, 0, MANY, 0,
2854 doc: /* Return bitwise-exclusive-or of all the arguments.
2855 Arguments may be integers, or markers converted to integers.
2856 usage: (logxor &rest INTS-OR-MARKERS) */)
2857 (ptrdiff_t nargs, Lisp_Object *args)
2859 return arith_driver (Alogxor, nargs, args);
2862 DEFUN ("ash", Fash, Sash, 2, 2, 0,
2863 doc: /* Return VALUE with its bits shifted left by COUNT.
2864 If COUNT is negative, shifting is actually to the right.
2865 In this case, the sign bit is duplicated. */)
2866 (register Lisp_Object value, Lisp_Object count)
2868 register Lisp_Object val;
2870 CHECK_NUMBER (value);
2871 CHECK_NUMBER (count);
2873 if (XINT (count) >= BITS_PER_EMACS_INT)
2874 XSETINT (val, 0);
2875 else if (XINT (count) > 0)
2876 XSETINT (val, XINT (value) << XFASTINT (count));
2877 else if (XINT (count) <= -BITS_PER_EMACS_INT)
2878 XSETINT (val, XINT (value) < 0 ? -1 : 0);
2879 else
2880 XSETINT (val, XINT (value) >> -XINT (count));
2881 return val;
2884 DEFUN ("lsh", Flsh, Slsh, 2, 2, 0,
2885 doc: /* Return VALUE with its bits shifted left by COUNT.
2886 If COUNT is negative, shifting is actually to the right.
2887 In this case, zeros are shifted in on the left. */)
2888 (register Lisp_Object value, Lisp_Object count)
2890 register Lisp_Object val;
2892 CHECK_NUMBER (value);
2893 CHECK_NUMBER (count);
2895 if (XINT (count) >= BITS_PER_EMACS_INT)
2896 XSETINT (val, 0);
2897 else if (XINT (count) > 0)
2898 XSETINT (val, XUINT (value) << XFASTINT (count));
2899 else if (XINT (count) <= -BITS_PER_EMACS_INT)
2900 XSETINT (val, 0);
2901 else
2902 XSETINT (val, XUINT (value) >> -XINT (count));
2903 return val;
2906 DEFUN ("1+", Fadd1, Sadd1, 1, 1, 0,
2907 doc: /* Return NUMBER plus one. NUMBER may be a number or a marker.
2908 Markers are converted to integers. */)
2909 (register Lisp_Object number)
2911 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number);
2913 if (FLOATP (number))
2914 return (make_float (1.0 + XFLOAT_DATA (number)));
2916 XSETINT (number, XINT (number) + 1);
2917 return number;
2920 DEFUN ("1-", Fsub1, Ssub1, 1, 1, 0,
2921 doc: /* Return NUMBER minus one. NUMBER may be a number or a marker.
2922 Markers are converted to integers. */)
2923 (register Lisp_Object number)
2925 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number);
2927 if (FLOATP (number))
2928 return (make_float (-1.0 + XFLOAT_DATA (number)));
2930 XSETINT (number, XINT (number) - 1);
2931 return number;
2934 DEFUN ("lognot", Flognot, Slognot, 1, 1, 0,
2935 doc: /* Return the bitwise complement of NUMBER. NUMBER must be an integer. */)
2936 (register Lisp_Object number)
2938 CHECK_NUMBER (number);
2939 XSETINT (number, ~XINT (number));
2940 return number;
2943 DEFUN ("byteorder", Fbyteorder, Sbyteorder, 0, 0, 0,
2944 doc: /* Return the byteorder for the machine.
2945 Returns 66 (ASCII uppercase B) for big endian machines or 108 (ASCII
2946 lowercase l) for small endian machines. */)
2947 (void)
2949 unsigned i = 0x04030201;
2950 int order = *(char *)&i == 1 ? 108 : 66;
2952 return make_number (order);
2957 void
2958 syms_of_data (void)
2960 Lisp_Object error_tail, arith_tail;
2962 DEFSYM (Qquote, "quote");
2963 DEFSYM (Qlambda, "lambda");
2964 DEFSYM (Qsubr, "subr");
2965 DEFSYM (Qerror_conditions, "error-conditions");
2966 DEFSYM (Qerror_message, "error-message");
2967 DEFSYM (Qtop_level, "top-level");
2969 DEFSYM (Qerror, "error");
2970 DEFSYM (Quser_error, "user-error");
2971 DEFSYM (Qquit, "quit");
2972 DEFSYM (Qwrong_type_argument, "wrong-type-argument");
2973 DEFSYM (Qargs_out_of_range, "args-out-of-range");
2974 DEFSYM (Qvoid_function, "void-function");
2975 DEFSYM (Qcyclic_function_indirection, "cyclic-function-indirection");
2976 DEFSYM (Qcyclic_variable_indirection, "cyclic-variable-indirection");
2977 DEFSYM (Qvoid_variable, "void-variable");
2978 DEFSYM (Qsetting_constant, "setting-constant");
2979 DEFSYM (Qinvalid_read_syntax, "invalid-read-syntax");
2981 DEFSYM (Qinvalid_function, "invalid-function");
2982 DEFSYM (Qwrong_number_of_arguments, "wrong-number-of-arguments");
2983 DEFSYM (Qno_catch, "no-catch");
2984 DEFSYM (Qend_of_file, "end-of-file");
2985 DEFSYM (Qarith_error, "arith-error");
2986 DEFSYM (Qbeginning_of_buffer, "beginning-of-buffer");
2987 DEFSYM (Qend_of_buffer, "end-of-buffer");
2988 DEFSYM (Qbuffer_read_only, "buffer-read-only");
2989 DEFSYM (Qtext_read_only, "text-read-only");
2990 DEFSYM (Qmark_inactive, "mark-inactive");
2992 DEFSYM (Qlistp, "listp");
2993 DEFSYM (Qconsp, "consp");
2994 DEFSYM (Qsymbolp, "symbolp");
2995 DEFSYM (Qkeywordp, "keywordp");
2996 DEFSYM (Qintegerp, "integerp");
2997 DEFSYM (Qnatnump, "natnump");
2998 DEFSYM (Qwholenump, "wholenump");
2999 DEFSYM (Qstringp, "stringp");
3000 DEFSYM (Qarrayp, "arrayp");
3001 DEFSYM (Qsequencep, "sequencep");
3002 DEFSYM (Qbufferp, "bufferp");
3003 DEFSYM (Qvectorp, "vectorp");
3004 DEFSYM (Qchar_or_string_p, "char-or-string-p");
3005 DEFSYM (Qmarkerp, "markerp");
3006 DEFSYM (Qbuffer_or_string_p, "buffer-or-string-p");
3007 DEFSYM (Qinteger_or_marker_p, "integer-or-marker-p");
3008 DEFSYM (Qboundp, "boundp");
3009 DEFSYM (Qfboundp, "fboundp");
3011 DEFSYM (Qfloatp, "floatp");
3012 DEFSYM (Qnumberp, "numberp");
3013 DEFSYM (Qnumber_or_marker_p, "number-or-marker-p");
3015 DEFSYM (Qchar_table_p, "char-table-p");
3016 DEFSYM (Qvector_or_char_table_p, "vector-or-char-table-p");
3018 DEFSYM (Qsubrp, "subrp");
3019 DEFSYM (Qunevalled, "unevalled");
3020 DEFSYM (Qmany, "many");
3022 DEFSYM (Qcdr, "cdr");
3024 /* Handle automatic advice activation. */
3025 DEFSYM (Qad_advice_info, "ad-advice-info");
3026 DEFSYM (Qad_activate_internal, "ad-activate-internal");
3028 error_tail = pure_cons (Qerror, Qnil);
3030 /* ERROR is used as a signaler for random errors for which nothing else is
3031 right. */
3033 Fput (Qerror, Qerror_conditions,
3034 error_tail);
3035 Fput (Qerror, Qerror_message,
3036 build_pure_c_string ("error"));
3038 #define PUT_ERROR(sym, tail, msg) \
3039 Fput (sym, Qerror_conditions, pure_cons (sym, tail)); \
3040 Fput (sym, Qerror_message, build_pure_c_string (msg))
3042 PUT_ERROR (Qquit, Qnil, "Quit");
3044 PUT_ERROR (Quser_error, error_tail, "");
3045 PUT_ERROR (Qwrong_type_argument, error_tail, "Wrong type argument");
3046 PUT_ERROR (Qargs_out_of_range, error_tail, "Args out of range");
3047 PUT_ERROR (Qvoid_function, error_tail,
3048 "Symbol's function definition is void");
3049 PUT_ERROR (Qcyclic_function_indirection, error_tail,
3050 "Symbol's chain of function indirections contains a loop");
3051 PUT_ERROR (Qcyclic_variable_indirection, error_tail,
3052 "Symbol's chain of variable indirections contains a loop");
3053 DEFSYM (Qcircular_list, "circular-list");
3054 PUT_ERROR (Qcircular_list, error_tail, "List contains a loop");
3055 PUT_ERROR (Qvoid_variable, error_tail, "Symbol's value as variable is void");
3056 PUT_ERROR (Qsetting_constant, error_tail,
3057 "Attempt to set a constant symbol");
3058 PUT_ERROR (Qinvalid_read_syntax, error_tail, "Invalid read syntax");
3059 PUT_ERROR (Qinvalid_function, error_tail, "Invalid function");
3060 PUT_ERROR (Qwrong_number_of_arguments, error_tail,
3061 "Wrong number of arguments");
3062 PUT_ERROR (Qno_catch, error_tail, "No catch for tag");
3063 PUT_ERROR (Qend_of_file, error_tail, "End of file during parsing");
3065 arith_tail = pure_cons (Qarith_error, error_tail);
3066 Fput (Qarith_error, Qerror_conditions, arith_tail);
3067 Fput (Qarith_error, Qerror_message, build_pure_c_string ("Arithmetic error"));
3069 PUT_ERROR (Qbeginning_of_buffer, error_tail, "Beginning of buffer");
3070 PUT_ERROR (Qend_of_buffer, error_tail, "End of buffer");
3071 PUT_ERROR (Qbuffer_read_only, error_tail, "Buffer is read-only");
3072 PUT_ERROR (Qtext_read_only, pure_cons (Qbuffer_read_only, error_tail),
3073 "Text is read-only");
3075 DEFSYM (Qrange_error, "range-error");
3076 DEFSYM (Qdomain_error, "domain-error");
3077 DEFSYM (Qsingularity_error, "singularity-error");
3078 DEFSYM (Qoverflow_error, "overflow-error");
3079 DEFSYM (Qunderflow_error, "underflow-error");
3081 PUT_ERROR (Qdomain_error, arith_tail, "Arithmetic domain error");
3083 PUT_ERROR (Qrange_error, arith_tail, "Arithmetic range error");
3085 PUT_ERROR (Qsingularity_error, Fcons (Qdomain_error, arith_tail),
3086 "Arithmetic singularity error");
3088 PUT_ERROR (Qoverflow_error, Fcons (Qdomain_error, arith_tail),
3089 "Arithmetic overflow error");
3090 PUT_ERROR (Qunderflow_error, Fcons (Qdomain_error, arith_tail),
3091 "Arithmetic underflow error");
3093 staticpro (&Qnil);
3094 staticpro (&Qt);
3095 staticpro (&Qunbound);
3097 /* Types that type-of returns. */
3098 DEFSYM (Qinteger, "integer");
3099 DEFSYM (Qsymbol, "symbol");
3100 DEFSYM (Qstring, "string");
3101 DEFSYM (Qcons, "cons");
3102 DEFSYM (Qmarker, "marker");
3103 DEFSYM (Qoverlay, "overlay");
3104 DEFSYM (Qfloat, "float");
3105 DEFSYM (Qwindow_configuration, "window-configuration");
3106 DEFSYM (Qprocess, "process");
3107 DEFSYM (Qwindow, "window");
3108 DEFSYM (Qcompiled_function, "compiled-function");
3109 DEFSYM (Qbuffer, "buffer");
3110 DEFSYM (Qframe, "frame");
3111 DEFSYM (Qvector, "vector");
3112 DEFSYM (Qchar_table, "char-table");
3113 DEFSYM (Qbool_vector, "bool-vector");
3114 DEFSYM (Qhash_table, "hash-table");
3115 DEFSYM (Qmisc, "misc");
3117 DEFSYM (Qdefun, "defun");
3119 DEFSYM (Qfont_spec, "font-spec");
3120 DEFSYM (Qfont_entity, "font-entity");
3121 DEFSYM (Qfont_object, "font-object");
3123 DEFSYM (Qinteractive_form, "interactive-form");
3124 DEFSYM (Qdefalias_fset_function, "defalias-fset-function");
3126 defsubr (&Sindirect_variable);
3127 defsubr (&Sinteractive_form);
3128 defsubr (&Seq);
3129 defsubr (&Snull);
3130 defsubr (&Stype_of);
3131 defsubr (&Slistp);
3132 defsubr (&Snlistp);
3133 defsubr (&Sconsp);
3134 defsubr (&Satom);
3135 defsubr (&Sintegerp);
3136 defsubr (&Sinteger_or_marker_p);
3137 defsubr (&Snumberp);
3138 defsubr (&Snumber_or_marker_p);
3139 defsubr (&Sfloatp);
3140 defsubr (&Snatnump);
3141 defsubr (&Ssymbolp);
3142 defsubr (&Skeywordp);
3143 defsubr (&Sstringp);
3144 defsubr (&Smultibyte_string_p);
3145 defsubr (&Svectorp);
3146 defsubr (&Schar_table_p);
3147 defsubr (&Svector_or_char_table_p);
3148 defsubr (&Sbool_vector_p);
3149 defsubr (&Sarrayp);
3150 defsubr (&Ssequencep);
3151 defsubr (&Sbufferp);
3152 defsubr (&Smarkerp);
3153 defsubr (&Ssubrp);
3154 defsubr (&Sbyte_code_function_p);
3155 defsubr (&Schar_or_string_p);
3156 defsubr (&Scar);
3157 defsubr (&Scdr);
3158 defsubr (&Scar_safe);
3159 defsubr (&Scdr_safe);
3160 defsubr (&Ssetcar);
3161 defsubr (&Ssetcdr);
3162 defsubr (&Ssymbol_function);
3163 defsubr (&Sindirect_function);
3164 defsubr (&Ssymbol_plist);
3165 defsubr (&Ssymbol_name);
3166 defsubr (&Smakunbound);
3167 defsubr (&Sfmakunbound);
3168 defsubr (&Sboundp);
3169 defsubr (&Sfboundp);
3170 defsubr (&Sfset);
3171 defsubr (&Sdefalias);
3172 defsubr (&Ssetplist);
3173 defsubr (&Ssymbol_value);
3174 defsubr (&Sset);
3175 defsubr (&Sdefault_boundp);
3176 defsubr (&Sdefault_value);
3177 defsubr (&Sset_default);
3178 defsubr (&Ssetq_default);
3179 defsubr (&Smake_variable_buffer_local);
3180 defsubr (&Smake_local_variable);
3181 defsubr (&Skill_local_variable);
3182 defsubr (&Smake_variable_frame_local);
3183 defsubr (&Slocal_variable_p);
3184 defsubr (&Slocal_variable_if_set_p);
3185 defsubr (&Svariable_binding_locus);
3186 #if 0 /* XXX Remove this. --lorentey */
3187 defsubr (&Sterminal_local_value);
3188 defsubr (&Sset_terminal_local_value);
3189 #endif
3190 defsubr (&Saref);
3191 defsubr (&Saset);
3192 defsubr (&Snumber_to_string);
3193 defsubr (&Sstring_to_number);
3194 defsubr (&Seqlsign);
3195 defsubr (&Slss);
3196 defsubr (&Sgtr);
3197 defsubr (&Sleq);
3198 defsubr (&Sgeq);
3199 defsubr (&Sneq);
3200 defsubr (&Szerop);
3201 defsubr (&Splus);
3202 defsubr (&Sminus);
3203 defsubr (&Stimes);
3204 defsubr (&Squo);
3205 defsubr (&Srem);
3206 defsubr (&Smod);
3207 defsubr (&Smax);
3208 defsubr (&Smin);
3209 defsubr (&Slogand);
3210 defsubr (&Slogior);
3211 defsubr (&Slogxor);
3212 defsubr (&Slsh);
3213 defsubr (&Sash);
3214 defsubr (&Sadd1);
3215 defsubr (&Ssub1);
3216 defsubr (&Slognot);
3217 defsubr (&Sbyteorder);
3218 defsubr (&Ssubr_arity);
3219 defsubr (&Ssubr_name);
3221 set_symbol_function (Qwholenump, XSYMBOL (Qnatnump)->function);
3223 DEFVAR_LISP ("most-positive-fixnum", Vmost_positive_fixnum,
3224 doc: /* The largest value that is representable in a Lisp integer. */);
3225 Vmost_positive_fixnum = make_number (MOST_POSITIVE_FIXNUM);
3226 XSYMBOL (intern_c_string ("most-positive-fixnum"))->constant = 1;
3228 DEFVAR_LISP ("most-negative-fixnum", Vmost_negative_fixnum,
3229 doc: /* The smallest value that is representable in a Lisp integer. */);
3230 Vmost_negative_fixnum = make_number (MOST_NEGATIVE_FIXNUM);
3231 XSYMBOL (intern_c_string ("most-negative-fixnum"))->constant = 1;