Fix comments and whitespace.
[emacs.git] / src / data.c
blobf92bc7964807093bce511e6b395cd4f5a9eb35ac
1 /* Primitive operations on Lisp data types for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 1986, 1988, 1993, 1994, 1995, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 #include <config.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <setjmp.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"
36 #ifdef STDC_HEADERS
37 #include <float.h>
38 #endif
40 /* If IEEE_FLOATING_POINT isn't defined, default it from FLT_*. */
41 #ifndef IEEE_FLOATING_POINT
42 #if (FLT_RADIX == 2 && FLT_MANT_DIG == 24 \
43 && FLT_MIN_EXP == -125 && FLT_MAX_EXP == 128)
44 #define IEEE_FLOATING_POINT 1
45 #else
46 #define IEEE_FLOATING_POINT 0
47 #endif
48 #endif
50 #include <math.h>
52 #if !defined (atof)
53 extern double atof (const char *);
54 #endif /* !atof */
56 Lisp_Object Qnil, Qt, Qquote, Qlambda, Qsubr, Qunbound;
57 Lisp_Object Qerror_conditions, Qerror_message, Qtop_level;
58 Lisp_Object Qerror, Qquit, Qwrong_type_argument, Qargs_out_of_range;
59 Lisp_Object Qvoid_variable, Qvoid_function, Qcyclic_function_indirection;
60 Lisp_Object Qcyclic_variable_indirection, Qcircular_list;
61 Lisp_Object Qsetting_constant, Qinvalid_read_syntax;
62 Lisp_Object Qinvalid_function, Qwrong_number_of_arguments, Qno_catch;
63 Lisp_Object Qend_of_file, Qarith_error, Qmark_inactive;
64 Lisp_Object Qbeginning_of_buffer, Qend_of_buffer, Qbuffer_read_only;
65 Lisp_Object Qtext_read_only;
67 Lisp_Object Qintegerp, Qnatnump, Qwholenump, Qsymbolp, Qlistp, Qconsp;
68 Lisp_Object Qstringp, Qarrayp, Qsequencep, Qbufferp;
69 Lisp_Object Qchar_or_string_p, Qmarkerp, Qinteger_or_marker_p, Qvectorp;
70 Lisp_Object Qbuffer_or_string_p, Qkeywordp;
71 Lisp_Object Qboundp, Qfboundp;
72 Lisp_Object Qchar_table_p, Qvector_or_char_table_p;
74 Lisp_Object Qcdr;
75 Lisp_Object Qad_advice_info, Qad_activate_internal;
77 Lisp_Object Qrange_error, Qdomain_error, Qsingularity_error;
78 Lisp_Object Qoverflow_error, Qunderflow_error;
80 Lisp_Object Qfloatp;
81 Lisp_Object Qnumberp, Qnumber_or_marker_p;
83 Lisp_Object Qinteger;
84 static Lisp_Object Qsymbol, Qstring, Qcons, Qmarker, Qoverlay;
85 static Lisp_Object Qfloat, Qwindow_configuration, Qwindow;
86 Lisp_Object Qprocess;
87 static Lisp_Object Qcompiled_function, Qbuffer, Qframe, Qvector;
88 static Lisp_Object Qchar_table, Qbool_vector, Qhash_table;
89 static Lisp_Object Qsubrp, Qmany, Qunevalled;
90 Lisp_Object Qfont_spec, Qfont_entity, Qfont_object;
92 Lisp_Object Qinteractive_form;
94 static void swap_in_symval_forwarding (struct Lisp_Symbol *, struct Lisp_Buffer_Local_Value *);
96 Lisp_Object Vmost_positive_fixnum, Vmost_negative_fixnum;
99 void
100 circular_list_error (Lisp_Object list)
102 xsignal (Qcircular_list, list);
106 Lisp_Object
107 wrong_type_argument (register Lisp_Object predicate, register Lisp_Object value)
109 /* If VALUE is not even a valid Lisp object, we'd want to abort here
110 where we can get a backtrace showing where it came from. We used
111 to try and do that by checking the tagbits, but nowadays all
112 tagbits are potentially valid. */
113 /* if ((unsigned int) XTYPE (value) >= Lisp_Type_Limit)
114 * abort (); */
116 xsignal2 (Qwrong_type_argument, predicate, value);
119 void
120 pure_write_error (void)
122 error ("Attempt to modify read-only object");
125 void
126 args_out_of_range (Lisp_Object a1, Lisp_Object a2)
128 xsignal2 (Qargs_out_of_range, a1, a2);
131 void
132 args_out_of_range_3 (Lisp_Object a1, Lisp_Object a2, Lisp_Object a3)
134 xsignal3 (Qargs_out_of_range, a1, a2, a3);
137 /* On some machines, XINT needs a temporary location.
138 Here it is, in case it is needed. */
140 int sign_extend_temp;
142 /* On a few machines, XINT can only be done by calling this. */
145 sign_extend_lisp_int (EMACS_INT num)
147 if (num & (((EMACS_INT) 1) << (VALBITS - 1)))
148 return num | (((EMACS_INT) (-1)) << VALBITS);
149 else
150 return num & ((((EMACS_INT) 1) << VALBITS) - 1);
153 /* Data type predicates */
155 DEFUN ("eq", Feq, Seq, 2, 2, 0,
156 doc: /* Return t if the two args are the same Lisp object. */)
157 (obj1, obj2)
158 Lisp_Object obj1, obj2;
160 if (EQ (obj1, obj2))
161 return Qt;
162 return Qnil;
165 DEFUN ("null", Fnull, Snull, 1, 1, 0,
166 doc: /* Return t if OBJECT is nil. */)
167 (object)
168 Lisp_Object object;
170 if (NILP (object))
171 return Qt;
172 return Qnil;
175 DEFUN ("type-of", Ftype_of, Stype_of, 1, 1, 0,
176 doc: /* Return a symbol representing the type of OBJECT.
177 The symbol returned names the object's basic type;
178 for example, (type-of 1) returns `integer'. */)
179 (object)
180 Lisp_Object object;
182 switch (XTYPE (object))
184 case_Lisp_Int:
185 return Qinteger;
187 case Lisp_Symbol:
188 return Qsymbol;
190 case Lisp_String:
191 return Qstring;
193 case Lisp_Cons:
194 return Qcons;
196 case Lisp_Misc:
197 switch (XMISCTYPE (object))
199 case Lisp_Misc_Marker:
200 return Qmarker;
201 case Lisp_Misc_Overlay:
202 return Qoverlay;
203 case Lisp_Misc_Float:
204 return Qfloat;
206 abort ();
208 case Lisp_Vectorlike:
209 if (WINDOW_CONFIGURATIONP (object))
210 return Qwindow_configuration;
211 if (PROCESSP (object))
212 return Qprocess;
213 if (WINDOWP (object))
214 return Qwindow;
215 if (SUBRP (object))
216 return Qsubr;
217 if (COMPILEDP (object))
218 return Qcompiled_function;
219 if (BUFFERP (object))
220 return Qbuffer;
221 if (CHAR_TABLE_P (object))
222 return Qchar_table;
223 if (BOOL_VECTOR_P (object))
224 return Qbool_vector;
225 if (FRAMEP (object))
226 return Qframe;
227 if (HASH_TABLE_P (object))
228 return Qhash_table;
229 if (FONT_SPEC_P (object))
230 return Qfont_spec;
231 if (FONT_ENTITY_P (object))
232 return Qfont_entity;
233 if (FONT_OBJECT_P (object))
234 return Qfont_object;
235 return Qvector;
237 case Lisp_Float:
238 return Qfloat;
240 default:
241 abort ();
245 DEFUN ("consp", Fconsp, Sconsp, 1, 1, 0,
246 doc: /* Return t if OBJECT is a cons cell. */)
247 (object)
248 Lisp_Object object;
250 if (CONSP (object))
251 return Qt;
252 return Qnil;
255 DEFUN ("atom", Fatom, Satom, 1, 1, 0,
256 doc: /* Return t if OBJECT is not a cons cell. This includes nil. */)
257 (object)
258 Lisp_Object object;
260 if (CONSP (object))
261 return Qnil;
262 return Qt;
265 DEFUN ("listp", Flistp, Slistp, 1, 1, 0,
266 doc: /* Return t if OBJECT is a list, that is, a cons cell or nil.
267 Otherwise, return nil. */)
268 (object)
269 Lisp_Object object;
271 if (CONSP (object) || NILP (object))
272 return Qt;
273 return Qnil;
276 DEFUN ("nlistp", Fnlistp, Snlistp, 1, 1, 0,
277 doc: /* Return t if OBJECT is not a list. Lists include nil. */)
278 (object)
279 Lisp_Object object;
281 if (CONSP (object) || NILP (object))
282 return Qnil;
283 return Qt;
286 DEFUN ("symbolp", Fsymbolp, Ssymbolp, 1, 1, 0,
287 doc: /* Return t if OBJECT is a symbol. */)
288 (object)
289 Lisp_Object object;
291 if (SYMBOLP (object))
292 return Qt;
293 return Qnil;
296 /* Define this in C to avoid unnecessarily consing up the symbol
297 name. */
298 DEFUN ("keywordp", Fkeywordp, Skeywordp, 1, 1, 0,
299 doc: /* Return t if OBJECT is a keyword.
300 This means that it is a symbol with a print name beginning with `:'
301 interned in the initial obarray. */)
302 (object)
303 Lisp_Object object;
305 if (SYMBOLP (object)
306 && SREF (SYMBOL_NAME (object), 0) == ':'
307 && SYMBOL_INTERNED_IN_INITIAL_OBARRAY_P (object))
308 return Qt;
309 return Qnil;
312 DEFUN ("vectorp", Fvectorp, Svectorp, 1, 1, 0,
313 doc: /* Return t if OBJECT is a vector. */)
314 (object)
315 Lisp_Object object;
317 if (VECTORP (object))
318 return Qt;
319 return Qnil;
322 DEFUN ("stringp", Fstringp, Sstringp, 1, 1, 0,
323 doc: /* Return t if OBJECT is a string. */)
324 (object)
325 Lisp_Object object;
327 if (STRINGP (object))
328 return Qt;
329 return Qnil;
332 DEFUN ("multibyte-string-p", Fmultibyte_string_p, Smultibyte_string_p,
333 1, 1, 0,
334 doc: /* Return t if OBJECT is a multibyte string. */)
335 (object)
336 Lisp_Object object;
338 if (STRINGP (object) && STRING_MULTIBYTE (object))
339 return Qt;
340 return Qnil;
343 DEFUN ("char-table-p", Fchar_table_p, Schar_table_p, 1, 1, 0,
344 doc: /* Return t if OBJECT is a char-table. */)
345 (object)
346 Lisp_Object object;
348 if (CHAR_TABLE_P (object))
349 return Qt;
350 return Qnil;
353 DEFUN ("vector-or-char-table-p", Fvector_or_char_table_p,
354 Svector_or_char_table_p, 1, 1, 0,
355 doc: /* Return t if OBJECT is a char-table or vector. */)
356 (object)
357 Lisp_Object object;
359 if (VECTORP (object) || CHAR_TABLE_P (object))
360 return Qt;
361 return Qnil;
364 DEFUN ("bool-vector-p", Fbool_vector_p, Sbool_vector_p, 1, 1, 0,
365 doc: /* Return t if OBJECT is a bool-vector. */)
366 (object)
367 Lisp_Object object;
369 if (BOOL_VECTOR_P (object))
370 return Qt;
371 return Qnil;
374 DEFUN ("arrayp", Farrayp, Sarrayp, 1, 1, 0,
375 doc: /* Return t if OBJECT is an array (string or vector). */)
376 (object)
377 Lisp_Object object;
379 if (ARRAYP (object))
380 return Qt;
381 return Qnil;
384 DEFUN ("sequencep", Fsequencep, Ssequencep, 1, 1, 0,
385 doc: /* Return t if OBJECT is a sequence (list or array). */)
386 (object)
387 register Lisp_Object object;
389 if (CONSP (object) || NILP (object) || ARRAYP (object))
390 return Qt;
391 return Qnil;
394 DEFUN ("bufferp", Fbufferp, Sbufferp, 1, 1, 0,
395 doc: /* Return t if OBJECT is an editor buffer. */)
396 (object)
397 Lisp_Object object;
399 if (BUFFERP (object))
400 return Qt;
401 return Qnil;
404 DEFUN ("markerp", Fmarkerp, Smarkerp, 1, 1, 0,
405 doc: /* Return t if OBJECT is a marker (editor pointer). */)
406 (object)
407 Lisp_Object object;
409 if (MARKERP (object))
410 return Qt;
411 return Qnil;
414 DEFUN ("subrp", Fsubrp, Ssubrp, 1, 1, 0,
415 doc: /* Return t if OBJECT is a built-in function. */)
416 (object)
417 Lisp_Object object;
419 if (SUBRP (object))
420 return Qt;
421 return Qnil;
424 DEFUN ("byte-code-function-p", Fbyte_code_function_p, Sbyte_code_function_p,
425 1, 1, 0,
426 doc: /* Return t if OBJECT is a byte-compiled function object. */)
427 (object)
428 Lisp_Object object;
430 if (COMPILEDP (object))
431 return Qt;
432 return Qnil;
435 DEFUN ("char-or-string-p", Fchar_or_string_p, Schar_or_string_p, 1, 1, 0,
436 doc: /* Return t if OBJECT is a character or a string. */)
437 (object)
438 register Lisp_Object object;
440 if (CHARACTERP (object) || STRINGP (object))
441 return Qt;
442 return Qnil;
445 DEFUN ("integerp", Fintegerp, Sintegerp, 1, 1, 0,
446 doc: /* Return t if OBJECT is an integer. */)
447 (object)
448 Lisp_Object object;
450 if (INTEGERP (object))
451 return Qt;
452 return Qnil;
455 DEFUN ("integer-or-marker-p", Finteger_or_marker_p, Sinteger_or_marker_p, 1, 1, 0,
456 doc: /* Return t if OBJECT is an integer or a marker (editor pointer). */)
457 (object)
458 register Lisp_Object object;
460 if (MARKERP (object) || INTEGERP (object))
461 return Qt;
462 return Qnil;
465 DEFUN ("natnump", Fnatnump, Snatnump, 1, 1, 0,
466 doc: /* Return t if OBJECT is a nonnegative integer. */)
467 (object)
468 Lisp_Object object;
470 if (NATNUMP (object))
471 return Qt;
472 return Qnil;
475 DEFUN ("numberp", Fnumberp, Snumberp, 1, 1, 0,
476 doc: /* Return t if OBJECT is a number (floating point or integer). */)
477 (object)
478 Lisp_Object object;
480 if (NUMBERP (object))
481 return Qt;
482 else
483 return Qnil;
486 DEFUN ("number-or-marker-p", Fnumber_or_marker_p,
487 Snumber_or_marker_p, 1, 1, 0,
488 doc: /* Return t if OBJECT is a number or a marker. */)
489 (object)
490 Lisp_Object object;
492 if (NUMBERP (object) || MARKERP (object))
493 return Qt;
494 return Qnil;
497 DEFUN ("floatp", Ffloatp, Sfloatp, 1, 1, 0,
498 doc: /* Return t if OBJECT is a floating point number. */)
499 (object)
500 Lisp_Object object;
502 if (FLOATP (object))
503 return Qt;
504 return Qnil;
508 /* Extract and set components of lists */
510 DEFUN ("car", Fcar, Scar, 1, 1, 0,
511 doc: /* Return the car of LIST. If arg is nil, return nil.
512 Error if arg is not nil and not a cons cell. See also `car-safe'.
514 See Info node `(elisp)Cons Cells' for a discussion of related basic
515 Lisp concepts such as car, cdr, cons cell and list. */)
516 (list)
517 register Lisp_Object list;
519 return CAR (list);
522 DEFUN ("car-safe", Fcar_safe, Scar_safe, 1, 1, 0,
523 doc: /* Return the car of OBJECT if it is a cons cell, or else nil. */)
524 (object)
525 Lisp_Object object;
527 return CAR_SAFE (object);
530 DEFUN ("cdr", Fcdr, Scdr, 1, 1, 0,
531 doc: /* Return the cdr of LIST. If arg is nil, return nil.
532 Error if arg is not nil and not a cons cell. See also `cdr-safe'.
534 See Info node `(elisp)Cons Cells' for a discussion of related basic
535 Lisp concepts such as cdr, car, cons cell and list. */)
536 (list)
537 register Lisp_Object list;
539 return CDR (list);
542 DEFUN ("cdr-safe", Fcdr_safe, Scdr_safe, 1, 1, 0,
543 doc: /* Return the cdr of OBJECT if it is a cons cell, or else nil. */)
544 (object)
545 Lisp_Object object;
547 return CDR_SAFE (object);
550 DEFUN ("setcar", Fsetcar, Ssetcar, 2, 2, 0,
551 doc: /* Set the car of CELL to be NEWCAR. Returns NEWCAR. */)
552 (cell, newcar)
553 register Lisp_Object cell, newcar;
555 CHECK_CONS (cell);
556 CHECK_IMPURE (cell);
557 XSETCAR (cell, newcar);
558 return newcar;
561 DEFUN ("setcdr", Fsetcdr, Ssetcdr, 2, 2, 0,
562 doc: /* Set the cdr of CELL to be NEWCDR. Returns NEWCDR. */)
563 (cell, newcdr)
564 register Lisp_Object cell, newcdr;
566 CHECK_CONS (cell);
567 CHECK_IMPURE (cell);
568 XSETCDR (cell, newcdr);
569 return newcdr;
572 /* Extract and set components of symbols */
574 DEFUN ("boundp", Fboundp, Sboundp, 1, 1, 0,
575 doc: /* Return t if SYMBOL's value is not void. */)
576 (symbol)
577 register Lisp_Object symbol;
579 Lisp_Object valcontents;
580 struct Lisp_Symbol *sym;
581 CHECK_SYMBOL (symbol);
582 sym = XSYMBOL (symbol);
584 start:
585 switch (sym->redirect)
587 case SYMBOL_PLAINVAL: valcontents = SYMBOL_VAL (sym); break;
588 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
589 case SYMBOL_LOCALIZED:
591 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
592 if (blv->fwd)
593 /* In set_internal, we un-forward vars when their value is
594 set to Qunbound. */
595 return Qt;
596 else
598 swap_in_symval_forwarding (sym, blv);
599 valcontents = BLV_VALUE (blv);
601 break;
603 case SYMBOL_FORWARDED:
604 /* In set_internal, we un-forward vars when their value is
605 set to Qunbound. */
606 return Qt;
607 default: abort ();
610 return (EQ (valcontents, Qunbound) ? Qnil : Qt);
613 DEFUN ("fboundp", Ffboundp, Sfboundp, 1, 1, 0,
614 doc: /* Return t if SYMBOL's function definition is not void. */)
615 (symbol)
616 register Lisp_Object symbol;
618 CHECK_SYMBOL (symbol);
619 return (EQ (XSYMBOL (symbol)->function, Qunbound) ? Qnil : Qt);
622 DEFUN ("makunbound", Fmakunbound, Smakunbound, 1, 1, 0,
623 doc: /* Make SYMBOL's value be void.
624 Return SYMBOL. */)
625 (symbol)
626 register Lisp_Object symbol;
628 CHECK_SYMBOL (symbol);
629 if (SYMBOL_CONSTANT_P (symbol))
630 xsignal1 (Qsetting_constant, symbol);
631 Fset (symbol, Qunbound);
632 return symbol;
635 DEFUN ("fmakunbound", Ffmakunbound, Sfmakunbound, 1, 1, 0,
636 doc: /* Make SYMBOL's function definition be void.
637 Return SYMBOL. */)
638 (symbol)
639 register Lisp_Object symbol;
641 CHECK_SYMBOL (symbol);
642 if (NILP (symbol) || EQ (symbol, Qt))
643 xsignal1 (Qsetting_constant, symbol);
644 XSYMBOL (symbol)->function = Qunbound;
645 return symbol;
648 DEFUN ("symbol-function", Fsymbol_function, Ssymbol_function, 1, 1, 0,
649 doc: /* Return SYMBOL's function definition. Error if that is void. */)
650 (symbol)
651 register Lisp_Object symbol;
653 CHECK_SYMBOL (symbol);
654 if (!EQ (XSYMBOL (symbol)->function, Qunbound))
655 return XSYMBOL (symbol)->function;
656 xsignal1 (Qvoid_function, symbol);
659 DEFUN ("symbol-plist", Fsymbol_plist, Ssymbol_plist, 1, 1, 0,
660 doc: /* Return SYMBOL's property list. */)
661 (symbol)
662 register Lisp_Object symbol;
664 CHECK_SYMBOL (symbol);
665 return XSYMBOL (symbol)->plist;
668 DEFUN ("symbol-name", Fsymbol_name, Ssymbol_name, 1, 1, 0,
669 doc: /* Return SYMBOL's name, a string. */)
670 (symbol)
671 register Lisp_Object symbol;
673 register Lisp_Object name;
675 CHECK_SYMBOL (symbol);
676 name = SYMBOL_NAME (symbol);
677 return name;
680 DEFUN ("fset", Ffset, Sfset, 2, 2, 0,
681 doc: /* Set SYMBOL's function definition to DEFINITION, and return DEFINITION. */)
682 (symbol, definition)
683 register Lisp_Object symbol, definition;
685 register Lisp_Object function;
687 CHECK_SYMBOL (symbol);
688 if (NILP (symbol) || EQ (symbol, Qt))
689 xsignal1 (Qsetting_constant, symbol);
691 function = XSYMBOL (symbol)->function;
693 if (!NILP (Vautoload_queue) && !EQ (function, Qunbound))
694 Vautoload_queue = Fcons (Fcons (symbol, function), Vautoload_queue);
696 if (CONSP (function) && EQ (XCAR (function), Qautoload))
697 Fput (symbol, Qautoload, XCDR (function));
699 XSYMBOL (symbol)->function = definition;
700 /* Handle automatic advice activation */
701 if (CONSP (XSYMBOL (symbol)->plist) && !NILP (Fget (symbol, Qad_advice_info)))
703 call2 (Qad_activate_internal, symbol, Qnil);
704 definition = XSYMBOL (symbol)->function;
706 return definition;
709 extern Lisp_Object Qfunction_documentation;
711 DEFUN ("defalias", Fdefalias, Sdefalias, 2, 3, 0,
712 doc: /* Set SYMBOL's function definition to DEFINITION, and return DEFINITION.
713 Associates the function with the current load file, if any.
714 The optional third argument DOCSTRING specifies the documentation string
715 for SYMBOL; if it is omitted or nil, SYMBOL uses the documentation string
716 determined by DEFINITION. */)
717 (symbol, definition, docstring)
718 register Lisp_Object symbol, definition, docstring;
720 CHECK_SYMBOL (symbol);
721 if (CONSP (XSYMBOL (symbol)->function)
722 && EQ (XCAR (XSYMBOL (symbol)->function), Qautoload))
723 LOADHIST_ATTACH (Fcons (Qt, symbol));
724 definition = Ffset (symbol, definition);
725 LOADHIST_ATTACH (Fcons (Qdefun, symbol));
726 if (!NILP (docstring))
727 Fput (symbol, Qfunction_documentation, docstring);
728 return definition;
731 DEFUN ("setplist", Fsetplist, Ssetplist, 2, 2, 0,
732 doc: /* Set SYMBOL's property list to NEWPLIST, and return NEWPLIST. */)
733 (symbol, newplist)
734 register Lisp_Object symbol, newplist;
736 CHECK_SYMBOL (symbol);
737 XSYMBOL (symbol)->plist = newplist;
738 return newplist;
741 DEFUN ("subr-arity", Fsubr_arity, Ssubr_arity, 1, 1, 0,
742 doc: /* Return minimum and maximum number of args allowed for SUBR.
743 SUBR must be a built-in function.
744 The returned value is a pair (MIN . MAX). MIN is the minimum number
745 of args. MAX is the maximum number or the symbol `many', for a
746 function with `&rest' args, or `unevalled' for a special form. */)
747 (subr)
748 Lisp_Object subr;
750 short minargs, maxargs;
751 CHECK_SUBR (subr);
752 minargs = XSUBR (subr)->min_args;
753 maxargs = XSUBR (subr)->max_args;
754 if (maxargs == MANY)
755 return Fcons (make_number (minargs), Qmany);
756 else if (maxargs == UNEVALLED)
757 return Fcons (make_number (minargs), Qunevalled);
758 else
759 return Fcons (make_number (minargs), make_number (maxargs));
762 DEFUN ("subr-name", Fsubr_name, Ssubr_name, 1, 1, 0,
763 doc: /* Return name of subroutine SUBR.
764 SUBR must be a built-in function. */)
765 (subr)
766 Lisp_Object subr;
768 const char *name;
769 CHECK_SUBR (subr);
770 name = XSUBR (subr)->symbol_name;
771 return make_string (name, strlen (name));
774 DEFUN ("interactive-form", Finteractive_form, Sinteractive_form, 1, 1, 0,
775 doc: /* Return the interactive form of CMD or nil if none.
776 If CMD is not a command, the return value is nil.
777 Value, if non-nil, is a list \(interactive SPEC). */)
778 (cmd)
779 Lisp_Object cmd;
781 Lisp_Object fun = indirect_function (cmd); /* Check cycles. */
783 if (NILP (fun) || EQ (fun, Qunbound))
784 return Qnil;
786 /* Use an `interactive-form' property if present, analogous to the
787 function-documentation property. */
788 fun = cmd;
789 while (SYMBOLP (fun))
791 Lisp_Object tmp = Fget (fun, Qinteractive_form);
792 if (!NILP (tmp))
793 return tmp;
794 else
795 fun = Fsymbol_function (fun);
798 if (SUBRP (fun))
800 char *spec = XSUBR (fun)->intspec;
801 if (spec)
802 return list2 (Qinteractive,
803 (*spec != '(') ? build_string (spec) :
804 Fcar (Fread_from_string (build_string (spec), Qnil, Qnil)));
806 else if (COMPILEDP (fun))
808 if ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE)
809 return list2 (Qinteractive, AREF (fun, COMPILED_INTERACTIVE));
811 else if (CONSP (fun))
813 Lisp_Object funcar = XCAR (fun);
814 if (EQ (funcar, Qlambda))
815 return Fassq (Qinteractive, Fcdr (XCDR (fun)));
816 else if (EQ (funcar, Qautoload))
818 struct gcpro gcpro1;
819 GCPRO1 (cmd);
820 do_autoload (fun, cmd);
821 UNGCPRO;
822 return Finteractive_form (cmd);
825 return Qnil;
829 /***********************************************************************
830 Getting and Setting Values of Symbols
831 ***********************************************************************/
833 /* Return the symbol holding SYMBOL's value. Signal
834 `cyclic-variable-indirection' if SYMBOL's chain of variable
835 indirections contains a loop. */
837 struct Lisp_Symbol *
838 indirect_variable (struct Lisp_Symbol *symbol)
840 struct Lisp_Symbol *tortoise, *hare;
842 hare = tortoise = symbol;
844 while (hare->redirect == SYMBOL_VARALIAS)
846 hare = SYMBOL_ALIAS (hare);
847 if (hare->redirect != SYMBOL_VARALIAS)
848 break;
850 hare = SYMBOL_ALIAS (hare);
851 tortoise = SYMBOL_ALIAS (tortoise);
853 if (hare == tortoise)
855 Lisp_Object tem;
856 XSETSYMBOL (tem, symbol);
857 xsignal1 (Qcyclic_variable_indirection, tem);
861 return hare;
865 DEFUN ("indirect-variable", Findirect_variable, Sindirect_variable, 1, 1, 0,
866 doc: /* Return the variable at the end of OBJECT's variable chain.
867 If OBJECT is a symbol, follow all variable indirections and return the final
868 variable. If OBJECT is not a symbol, just return it.
869 Signal a cyclic-variable-indirection error if there is a loop in the
870 variable chain of symbols. */)
871 (object)
872 Lisp_Object object;
874 if (SYMBOLP (object))
875 XSETSYMBOL (object, indirect_variable (XSYMBOL (object)));
876 return object;
880 /* Given the raw contents of a symbol value cell,
881 return the Lisp value of the symbol.
882 This does not handle buffer-local variables; use
883 swap_in_symval_forwarding for that. */
885 #define do_blv_forwarding(blv) \
886 ((blv)->forwarded ? do_symval_forwarding (BLV_FWD (blv)) : BLV_VALUE (blv))
888 Lisp_Object
889 do_symval_forwarding (register union Lisp_Fwd *valcontents)
891 register Lisp_Object val;
892 switch (XFWDTYPE (valcontents))
894 case Lisp_Fwd_Int:
895 XSETINT (val, *XINTFWD (valcontents)->intvar);
896 return val;
898 case Lisp_Fwd_Bool:
899 return (*XBOOLFWD (valcontents)->boolvar ? Qt : Qnil);
901 case Lisp_Fwd_Obj:
902 return *XOBJFWD (valcontents)->objvar;
904 case Lisp_Fwd_Buffer_Obj:
905 return PER_BUFFER_VALUE (current_buffer,
906 XBUFFER_OBJFWD (valcontents)->offset);
908 case Lisp_Fwd_Kboard_Obj:
909 /* We used to simply use current_kboard here, but from Lisp
910 code, it's value is often unexpected. It seems nicer to
911 allow constructions like this to work as intuitively expected:
913 (with-selected-frame frame
914 (define-key local-function-map "\eOP" [f1]))
916 On the other hand, this affects the semantics of
917 last-command and real-last-command, and people may rely on
918 that. I took a quick look at the Lisp codebase, and I
919 don't think anything will break. --lorentey */
920 return *(Lisp_Object *)(XKBOARD_OBJFWD (valcontents)->offset
921 + (char *)FRAME_KBOARD (SELECTED_FRAME ()));
922 default: abort ();
926 /* Store NEWVAL into SYMBOL, where VALCONTENTS is found in the value cell
927 of SYMBOL. If SYMBOL is buffer-local, VALCONTENTS should be the
928 buffer-independent contents of the value cell: forwarded just one
929 step past the buffer-localness.
931 BUF non-zero means set the value in buffer BUF instead of the
932 current buffer. This only plays a role for per-buffer variables. */
934 #define store_blv_forwarding(blv, newval, buf) \
935 do { \
936 if ((blv)->forwarded) \
937 store_symval_forwarding (BLV_FWD (blv), (newval), (buf)); \
938 else \
939 SET_BLV_VALUE (blv, newval); \
940 } while (0)
942 static void
943 store_symval_forwarding (union Lisp_Fwd *valcontents, register Lisp_Object newval, struct buffer *buf)
945 switch (XFWDTYPE (valcontents))
947 case Lisp_Fwd_Int:
948 CHECK_NUMBER (newval);
949 *XINTFWD (valcontents)->intvar = XINT (newval);
950 break;
952 case Lisp_Fwd_Bool:
953 *XBOOLFWD (valcontents)->boolvar = !NILP (newval);
954 break;
956 case Lisp_Fwd_Obj:
957 *XOBJFWD (valcontents)->objvar = newval;
959 /* If this variable is a default for something stored
960 in the buffer itself, such as default-fill-column,
961 find the buffers that don't have local values for it
962 and update them. */
963 if (XOBJFWD (valcontents)->objvar > (Lisp_Object *) &buffer_defaults
964 && XOBJFWD (valcontents)->objvar < (Lisp_Object *) (&buffer_defaults + 1))
966 int offset = ((char *) XOBJFWD (valcontents)->objvar
967 - (char *) &buffer_defaults);
968 int idx = PER_BUFFER_IDX (offset);
970 Lisp_Object tail;
972 if (idx <= 0)
973 break;
975 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
977 Lisp_Object buf;
978 struct buffer *b;
980 buf = Fcdr (XCAR (tail));
981 if (!BUFFERP (buf)) continue;
982 b = XBUFFER (buf);
984 if (! PER_BUFFER_VALUE_P (b, idx))
985 PER_BUFFER_VALUE (b, offset) = newval;
988 break;
990 case Lisp_Fwd_Buffer_Obj:
992 int offset = XBUFFER_OBJFWD (valcontents)->offset;
993 Lisp_Object type = XBUFFER_OBJFWD (valcontents)->slottype;
995 if (!(NILP (type) || NILP (newval)
996 || (XINT (type) == LISP_INT_TAG
997 ? INTEGERP (newval)
998 : XTYPE (newval) == XINT (type))))
999 buffer_slot_type_mismatch (newval, XINT (type));
1001 if (buf == NULL)
1002 buf = current_buffer;
1003 PER_BUFFER_VALUE (buf, offset) = newval;
1005 break;
1007 case Lisp_Fwd_Kboard_Obj:
1009 char *base = (char *) FRAME_KBOARD (SELECTED_FRAME ());
1010 char *p = base + XKBOARD_OBJFWD (valcontents)->offset;
1011 *(Lisp_Object *) p = newval;
1013 break;
1015 default:
1016 abort (); /* goto def; */
1020 /* Set up SYMBOL to refer to its global binding.
1021 This makes it safe to alter the status of other bindings. */
1023 void
1024 swap_in_global_binding (struct Lisp_Symbol *symbol)
1026 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (symbol);
1028 /* Unload the previously loaded binding. */
1029 if (blv->fwd)
1030 SET_BLV_VALUE (blv, do_symval_forwarding (blv->fwd));
1032 /* Select the global binding in the symbol. */
1033 blv->valcell = blv->defcell;
1034 if (blv->fwd)
1035 store_symval_forwarding (blv->fwd, XCDR (blv->defcell), NULL);
1037 /* Indicate that the global binding is set up now. */
1038 blv->where = Qnil;
1039 SET_BLV_FOUND (blv, 0);
1042 /* Set up the buffer-local symbol SYMBOL for validity in the current buffer.
1043 VALCONTENTS is the contents of its value cell,
1044 which points to a struct Lisp_Buffer_Local_Value.
1046 Return the value forwarded one step past the buffer-local stage.
1047 This could be another forwarding pointer. */
1049 static void
1050 swap_in_symval_forwarding (struct Lisp_Symbol *symbol, struct Lisp_Buffer_Local_Value *blv)
1052 register Lisp_Object tem1;
1054 eassert (blv == SYMBOL_BLV (symbol));
1056 tem1 = blv->where;
1058 if (NILP (tem1)
1059 || (blv->frame_local
1060 ? !EQ (selected_frame, tem1)
1061 : current_buffer != XBUFFER (tem1)))
1064 /* Unload the previously loaded binding. */
1065 tem1 = blv->valcell;
1066 if (blv->fwd)
1067 SET_BLV_VALUE (blv, do_symval_forwarding (blv->fwd));
1068 /* Choose the new binding. */
1070 Lisp_Object var;
1071 XSETSYMBOL (var, symbol);
1072 if (blv->frame_local)
1074 tem1 = assq_no_quit (var, XFRAME (selected_frame)->param_alist);
1075 blv->where = selected_frame;
1077 else
1079 tem1 = assq_no_quit (var, current_buffer->local_var_alist);
1080 XSETBUFFER (blv->where, current_buffer);
1083 if (!(blv->found = !NILP (tem1)))
1084 tem1 = blv->defcell;
1086 /* Load the new binding. */
1087 blv->valcell = tem1;
1088 if (blv->fwd)
1089 store_symval_forwarding (blv->fwd, BLV_VALUE (blv), NULL);
1093 /* Find the value of a symbol, returning Qunbound if it's not bound.
1094 This is helpful for code which just wants to get a variable's value
1095 if it has one, without signaling an error.
1096 Note that it must not be possible to quit
1097 within this function. Great care is required for this. */
1099 Lisp_Object
1100 find_symbol_value (Lisp_Object symbol)
1102 struct Lisp_Symbol *sym;
1104 CHECK_SYMBOL (symbol);
1105 sym = XSYMBOL (symbol);
1107 start:
1108 switch (sym->redirect)
1110 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1111 case SYMBOL_PLAINVAL: return SYMBOL_VAL (sym);
1112 case SYMBOL_LOCALIZED:
1114 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1115 swap_in_symval_forwarding (sym, blv);
1116 return blv->fwd ? do_symval_forwarding (blv->fwd) : BLV_VALUE (blv);
1118 /* FALLTHROUGH */
1119 case SYMBOL_FORWARDED:
1120 return do_symval_forwarding (SYMBOL_FWD (sym));
1121 default: abort ();
1125 DEFUN ("symbol-value", Fsymbol_value, Ssymbol_value, 1, 1, 0,
1126 doc: /* Return SYMBOL's value. Error if that is void. */)
1127 (symbol)
1128 Lisp_Object symbol;
1130 Lisp_Object val;
1132 val = find_symbol_value (symbol);
1133 if (!EQ (val, Qunbound))
1134 return val;
1136 xsignal1 (Qvoid_variable, symbol);
1139 DEFUN ("set", Fset, Sset, 2, 2, 0,
1140 doc: /* Set SYMBOL's value to NEWVAL, and return NEWVAL. */)
1141 (symbol, newval)
1142 register Lisp_Object symbol, newval;
1144 set_internal (symbol, newval, Qnil, 0);
1145 return newval;
1148 /* Return 1 if SYMBOL currently has a let-binding
1149 which was made in the buffer that is now current. */
1151 static int
1152 let_shadows_buffer_binding_p (struct Lisp_Symbol *symbol)
1154 struct specbinding *p;
1156 for (p = specpdl_ptr - 1; p >= specpdl; p--)
1157 if (p->func == NULL
1158 && CONSP (p->symbol))
1160 struct Lisp_Symbol *let_bound_symbol = XSYMBOL (XCAR (p->symbol));
1161 eassert (let_bound_symbol->redirect != SYMBOL_VARALIAS);
1162 if (symbol == let_bound_symbol
1163 && XBUFFER (XCDR (XCDR (p->symbol))) == current_buffer)
1164 break;
1167 return p >= specpdl;
1170 static int
1171 let_shadows_global_binding_p (Lisp_Object symbol)
1173 struct specbinding *p;
1175 for (p = specpdl_ptr - 1; p >= specpdl; p--)
1176 if (p->func == NULL && EQ (p->symbol, symbol))
1177 break;
1179 return p >= specpdl;
1182 /* Store the value NEWVAL into SYMBOL.
1183 If buffer/frame-locality is an issue, WHERE specifies which context to use.
1184 (nil stands for the current buffer/frame).
1186 If BINDFLAG is zero, then if this symbol is supposed to become
1187 local in every buffer where it is set, then we make it local.
1188 If BINDFLAG is nonzero, we don't do that. */
1190 void
1191 set_internal (register Lisp_Object symbol, register Lisp_Object newval, register Lisp_Object where, int bindflag)
1193 int voide = EQ (newval, Qunbound);
1194 struct Lisp_Symbol *sym;
1195 Lisp_Object tem1;
1197 /* If restoring in a dead buffer, do nothing. */
1198 /* if (BUFFERP (where) && NILP (XBUFFER (where)->name))
1199 return; */
1201 CHECK_SYMBOL (symbol);
1202 if (SYMBOL_CONSTANT_P (symbol))
1204 if (NILP (Fkeywordp (symbol))
1205 || !EQ (newval, Fsymbol_value (symbol)))
1206 xsignal1 (Qsetting_constant, symbol);
1207 else
1208 /* Allow setting keywords to their own value. */
1209 return;
1212 sym = XSYMBOL (symbol);
1214 start:
1215 switch (sym->redirect)
1217 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1218 case SYMBOL_PLAINVAL: SET_SYMBOL_VAL (sym , newval); return;
1219 case SYMBOL_LOCALIZED:
1221 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1222 if (NILP (where))
1224 if (blv->frame_local)
1225 where = selected_frame;
1226 else
1227 XSETBUFFER (where, current_buffer);
1229 /* If the current buffer is not the buffer whose binding is
1230 loaded, or if there may be frame-local bindings and the frame
1231 isn't the right one, or if it's a Lisp_Buffer_Local_Value and
1232 the default binding is loaded, the loaded binding may be the
1233 wrong one. */
1234 if (!EQ (blv->where, where)
1235 /* Also unload a global binding (if the var is local_if_set). */
1236 || (EQ (blv->valcell, blv->defcell)))
1238 /* The currently loaded binding is not necessarily valid.
1239 We need to unload it, and choose a new binding. */
1241 /* Write out `realvalue' to the old loaded binding. */
1242 if (blv->fwd)
1243 SET_BLV_VALUE (blv, do_symval_forwarding (blv->fwd));
1245 /* Find the new binding. */
1246 XSETSYMBOL (symbol, sym); /* May have changed via aliasing. */
1247 tem1 = Fassq (symbol,
1248 (blv->frame_local
1249 ? XFRAME (where)->param_alist
1250 : XBUFFER (where)->local_var_alist));
1251 blv->where = where;
1252 blv->found = 1;
1254 if (NILP (tem1))
1256 /* This buffer still sees the default value. */
1258 /* If the variable is a Lisp_Some_Buffer_Local_Value,
1259 or if this is `let' rather than `set',
1260 make CURRENT-ALIST-ELEMENT point to itself,
1261 indicating that we're seeing the default value.
1262 Likewise if the variable has been let-bound
1263 in the current buffer. */
1264 if (bindflag || !blv->local_if_set
1265 || let_shadows_buffer_binding_p (sym))
1267 blv->found = 0;
1268 tem1 = blv->defcell;
1270 /* If it's a local_if_set, being set not bound,
1271 and we're not within a let that was made for this buffer,
1272 create a new buffer-local binding for the variable.
1273 That means, give this buffer a new assoc for a local value
1274 and load that binding. */
1275 else
1277 /* local_if_set is only supported for buffer-local
1278 bindings, not for frame-local bindings. */
1279 eassert (!blv->frame_local);
1280 tem1 = Fcons (symbol, XCDR (blv->defcell));
1281 XBUFFER (where)->local_var_alist
1282 = Fcons (tem1, XBUFFER (where)->local_var_alist);
1286 /* Record which binding is now loaded. */
1287 blv->valcell = tem1;
1290 /* Store the new value in the cons cell. */
1291 SET_BLV_VALUE (blv, newval);
1293 if (blv->fwd)
1295 if (voide)
1296 /* If storing void (making the symbol void), forward only through
1297 buffer-local indicator, not through Lisp_Objfwd, etc. */
1298 blv->fwd = NULL;
1299 else
1300 store_symval_forwarding (blv->fwd, newval,
1301 BUFFERP (where)
1302 ? XBUFFER (where) : current_buffer);
1304 break;
1306 case SYMBOL_FORWARDED:
1308 struct buffer *buf
1309 = BUFFERP (where) ? XBUFFER (where) : current_buffer;
1310 union Lisp_Fwd *innercontents = SYMBOL_FWD (sym);
1311 if (BUFFER_OBJFWDP (innercontents))
1313 int offset = XBUFFER_OBJFWD (innercontents)->offset;
1314 int idx = PER_BUFFER_IDX (offset);
1315 if (idx > 0
1316 && !bindflag
1317 && !let_shadows_buffer_binding_p (sym))
1318 SET_PER_BUFFER_VALUE_P (buf, idx, 1);
1321 if (voide)
1322 { /* If storing void (making the symbol void), forward only through
1323 buffer-local indicator, not through Lisp_Objfwd, etc. */
1324 sym->redirect = SYMBOL_PLAINVAL;
1325 SET_SYMBOL_VAL (sym, newval);
1327 else
1328 store_symval_forwarding (/* sym, */ innercontents, newval, buf);
1329 break;
1331 default: abort ();
1333 return;
1336 /* Access or set a buffer-local symbol's default value. */
1338 /* Return the default value of SYMBOL, but don't check for voidness.
1339 Return Qunbound if it is void. */
1341 Lisp_Object
1342 default_value (Lisp_Object symbol)
1344 struct Lisp_Symbol *sym;
1346 CHECK_SYMBOL (symbol);
1347 sym = XSYMBOL (symbol);
1349 start:
1350 switch (sym->redirect)
1352 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1353 case SYMBOL_PLAINVAL: return SYMBOL_VAL (sym);
1354 case SYMBOL_LOCALIZED:
1356 /* If var is set up for a buffer that lacks a local value for it,
1357 the current value is nominally the default value.
1358 But the `realvalue' slot may be more up to date, since
1359 ordinary setq stores just that slot. So use that. */
1360 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1361 if (blv->fwd && EQ (blv->valcell, blv->defcell))
1362 return do_symval_forwarding (blv->fwd);
1363 else
1364 return XCDR (blv->defcell);
1366 case SYMBOL_FORWARDED:
1368 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
1370 /* For a built-in buffer-local variable, get the default value
1371 rather than letting do_symval_forwarding get the current value. */
1372 if (BUFFER_OBJFWDP (valcontents))
1374 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1375 if (PER_BUFFER_IDX (offset) != 0)
1376 return PER_BUFFER_DEFAULT (offset);
1379 /* For other variables, get the current value. */
1380 return do_symval_forwarding (valcontents);
1382 default: abort ();
1386 DEFUN ("default-boundp", Fdefault_boundp, Sdefault_boundp, 1, 1, 0,
1387 doc: /* Return t if SYMBOL has a non-void default value.
1388 This is the value that is seen in buffers that do not have their own values
1389 for this variable. */)
1390 (symbol)
1391 Lisp_Object symbol;
1393 register Lisp_Object value;
1395 value = default_value (symbol);
1396 return (EQ (value, Qunbound) ? Qnil : Qt);
1399 DEFUN ("default-value", Fdefault_value, Sdefault_value, 1, 1, 0,
1400 doc: /* Return SYMBOL's default value.
1401 This is the value that is seen in buffers that do not have their own values
1402 for this variable. The default value is meaningful for variables with
1403 local bindings in certain buffers. */)
1404 (symbol)
1405 Lisp_Object symbol;
1407 register Lisp_Object value;
1409 value = default_value (symbol);
1410 if (!EQ (value, Qunbound))
1411 return value;
1413 xsignal1 (Qvoid_variable, symbol);
1416 DEFUN ("set-default", Fset_default, Sset_default, 2, 2, 0,
1417 doc: /* Set SYMBOL's default value to VALUE. SYMBOL and VALUE are evaluated.
1418 The default value is seen in buffers that do not have their own values
1419 for this variable. */)
1420 (symbol, value)
1421 Lisp_Object symbol, value;
1423 struct Lisp_Symbol *sym;
1425 CHECK_SYMBOL (symbol);
1426 if (SYMBOL_CONSTANT_P (symbol))
1428 if (NILP (Fkeywordp (symbol))
1429 || !EQ (value, Fdefault_value (symbol)))
1430 xsignal1 (Qsetting_constant, symbol);
1431 else
1432 /* Allow setting keywords to their own value. */
1433 return value;
1435 sym = XSYMBOL (symbol);
1437 start:
1438 switch (sym->redirect)
1440 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1441 case SYMBOL_PLAINVAL: return Fset (symbol, value);
1442 case SYMBOL_LOCALIZED:
1444 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1446 /* Store new value into the DEFAULT-VALUE slot. */
1447 XSETCDR (blv->defcell, value);
1449 /* If the default binding is now loaded, set the REALVALUE slot too. */
1450 if (blv->fwd && EQ (blv->defcell, blv->valcell))
1451 store_symval_forwarding (blv->fwd, value, NULL);
1452 return value;
1454 case SYMBOL_FORWARDED:
1456 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
1458 /* Handle variables like case-fold-search that have special slots
1459 in the buffer.
1460 Make them work apparently like Lisp_Buffer_Local_Value variables. */
1461 if (BUFFER_OBJFWDP (valcontents))
1463 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1464 int idx = PER_BUFFER_IDX (offset);
1466 PER_BUFFER_DEFAULT (offset) = value;
1468 /* If this variable is not always local in all buffers,
1469 set it in the buffers that don't nominally have a local value. */
1470 if (idx > 0)
1472 struct buffer *b;
1474 for (b = all_buffers; b; b = b->next)
1475 if (!PER_BUFFER_VALUE_P (b, idx))
1476 PER_BUFFER_VALUE (b, offset) = value;
1478 return value;
1480 else
1481 return Fset (symbol, value);
1483 default: abort ();
1487 DEFUN ("setq-default", Fsetq_default, Ssetq_default, 0, UNEVALLED, 0,
1488 doc: /* Set the default value of variable VAR to VALUE.
1489 VAR, the variable name, is literal (not evaluated);
1490 VALUE is an expression: it is evaluated and its value returned.
1491 The default value of a variable is seen in buffers
1492 that do not have their own values for the variable.
1494 More generally, you can use multiple variables and values, as in
1495 (setq-default VAR VALUE VAR VALUE...)
1496 This sets each VAR's default value to the corresponding VALUE.
1497 The VALUE for the Nth VAR can refer to the new default values
1498 of previous VARs.
1499 usage: (setq-default [VAR VALUE]...) */)
1500 (args)
1501 Lisp_Object args;
1503 register Lisp_Object args_left;
1504 register Lisp_Object val, symbol;
1505 struct gcpro gcpro1;
1507 if (NILP (args))
1508 return Qnil;
1510 args_left = args;
1511 GCPRO1 (args);
1515 val = Feval (Fcar (Fcdr (args_left)));
1516 symbol = XCAR (args_left);
1517 Fset_default (symbol, val);
1518 args_left = Fcdr (XCDR (args_left));
1520 while (!NILP (args_left));
1522 UNGCPRO;
1523 return val;
1526 /* Lisp functions for creating and removing buffer-local variables. */
1528 union Lisp_Val_Fwd
1530 Lisp_Object value;
1531 union Lisp_Fwd *fwd;
1534 static struct Lisp_Buffer_Local_Value *
1535 make_blv (struct Lisp_Symbol *sym, int forwarded, union Lisp_Val_Fwd valcontents)
1537 struct Lisp_Buffer_Local_Value *blv
1538 = xmalloc (sizeof (struct Lisp_Buffer_Local_Value));
1539 Lisp_Object symbol;
1540 Lisp_Object tem;
1542 XSETSYMBOL (symbol, sym);
1543 tem = Fcons (symbol, (forwarded
1544 ? do_symval_forwarding (valcontents.fwd)
1545 : valcontents.value));
1547 /* Buffer_Local_Values cannot have as realval a buffer-local
1548 or keyboard-local forwarding. */
1549 eassert (!(forwarded && BUFFER_OBJFWDP (valcontents.fwd)));
1550 eassert (!(forwarded && KBOARD_OBJFWDP (valcontents.fwd)));
1551 blv->fwd = forwarded ? valcontents.fwd : NULL;
1552 blv->where = Qnil;
1553 blv->frame_local = 0;
1554 blv->local_if_set = 0;
1555 blv->defcell = tem;
1556 blv->valcell = tem;
1557 SET_BLV_FOUND (blv, 0);
1558 return blv;
1561 DEFUN ("make-variable-buffer-local", Fmake_variable_buffer_local, Smake_variable_buffer_local,
1562 1, 1, "vMake Variable Buffer Local: ",
1563 doc: /* Make VARIABLE become buffer-local whenever it is set.
1564 At any time, the value for the current buffer is in effect,
1565 unless the variable has never been set in this buffer,
1566 in which case the default value is in effect.
1567 Note that binding the variable with `let', or setting it while
1568 a `let'-style binding made in this buffer is in effect,
1569 does not make the variable buffer-local. Return VARIABLE.
1571 In most cases it is better to use `make-local-variable',
1572 which makes a variable local in just one buffer.
1574 The function `default-value' gets the default value and `set-default' sets it. */)
1575 (variable)
1576 register Lisp_Object variable;
1578 struct Lisp_Symbol *sym;
1579 struct Lisp_Buffer_Local_Value *blv = NULL;
1580 union Lisp_Val_Fwd valcontents;
1581 int forwarded;
1583 CHECK_SYMBOL (variable);
1584 sym = XSYMBOL (variable);
1586 start:
1587 switch (sym->redirect)
1589 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1590 case SYMBOL_PLAINVAL:
1591 forwarded = 0; valcontents.value = SYMBOL_VAL (sym);
1592 if (EQ (valcontents.value, Qunbound))
1593 valcontents.value = Qnil;
1594 break;
1595 case SYMBOL_LOCALIZED:
1596 blv = SYMBOL_BLV (sym);
1597 if (blv->frame_local)
1598 error ("Symbol %s may not be buffer-local",
1599 SDATA (SYMBOL_NAME (variable)));
1600 break;
1601 case SYMBOL_FORWARDED:
1602 forwarded = 1; valcontents.fwd = SYMBOL_FWD (sym);
1603 if (KBOARD_OBJFWDP (valcontents.fwd))
1604 error ("Symbol %s may not be buffer-local",
1605 SDATA (SYMBOL_NAME (variable)));
1606 else if (BUFFER_OBJFWDP (valcontents.fwd))
1607 return variable;
1608 break;
1609 default: abort ();
1612 if (sym->constant)
1613 error ("Symbol %s may not be buffer-local", SDATA (SYMBOL_NAME (variable)));
1615 if (!blv)
1617 blv = make_blv (sym, forwarded, valcontents);
1618 sym->redirect = SYMBOL_LOCALIZED;
1619 SET_SYMBOL_BLV (sym, blv);
1621 Lisp_Object symbol;
1622 XSETSYMBOL (symbol, sym); /* In case `variable' is aliased. */
1623 if (let_shadows_global_binding_p (symbol))
1624 message ("Making %s buffer-local while let-bound!",
1625 SDATA (SYMBOL_NAME (variable)));
1629 blv->local_if_set = 1;
1630 return variable;
1633 DEFUN ("make-local-variable", Fmake_local_variable, Smake_local_variable,
1634 1, 1, "vMake Local Variable: ",
1635 doc: /* Make VARIABLE have a separate value in the current buffer.
1636 Other buffers will continue to share a common default value.
1637 \(The buffer-local value of VARIABLE starts out as the same value
1638 VARIABLE previously had. If VARIABLE was void, it remains void.\)
1639 Return VARIABLE.
1641 If the variable is already arranged to become local when set,
1642 this function causes a local value to exist for this buffer,
1643 just as setting the variable would do.
1645 This function returns VARIABLE, and therefore
1646 (set (make-local-variable 'VARIABLE) VALUE-EXP)
1647 works.
1649 See also `make-variable-buffer-local'.
1651 Do not use `make-local-variable' to make a hook variable buffer-local.
1652 Instead, use `add-hook' and specify t for the LOCAL argument. */)
1653 (variable)
1654 register Lisp_Object variable;
1656 register Lisp_Object tem;
1657 int forwarded;
1658 union Lisp_Val_Fwd valcontents;
1659 struct Lisp_Symbol *sym;
1660 struct Lisp_Buffer_Local_Value *blv = NULL;
1662 CHECK_SYMBOL (variable);
1663 sym = XSYMBOL (variable);
1665 start:
1666 switch (sym->redirect)
1668 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1669 case SYMBOL_PLAINVAL:
1670 forwarded = 0; valcontents.value = SYMBOL_VAL (sym); break;
1671 case SYMBOL_LOCALIZED:
1672 blv = SYMBOL_BLV (sym);
1673 if (blv->frame_local)
1674 error ("Symbol %s may not be buffer-local",
1675 SDATA (SYMBOL_NAME (variable)));
1676 break;
1677 case SYMBOL_FORWARDED:
1678 forwarded = 1; valcontents.fwd = SYMBOL_FWD (sym);
1679 if (KBOARD_OBJFWDP (valcontents.fwd))
1680 error ("Symbol %s may not be buffer-local",
1681 SDATA (SYMBOL_NAME (variable)));
1682 break;
1683 default: abort ();
1686 if (sym->constant)
1687 error ("Symbol %s may not be buffer-local",
1688 SDATA (SYMBOL_NAME (variable)));
1690 if (blv ? blv->local_if_set
1691 : (forwarded && BUFFER_OBJFWDP (valcontents.fwd)))
1693 tem = Fboundp (variable);
1694 /* Make sure the symbol has a local value in this particular buffer,
1695 by setting it to the same value it already has. */
1696 Fset (variable, (EQ (tem, Qt) ? Fsymbol_value (variable) : Qunbound));
1697 return variable;
1699 if (!blv)
1701 blv = make_blv (sym, forwarded, valcontents);
1702 sym->redirect = SYMBOL_LOCALIZED;
1703 SET_SYMBOL_BLV (sym, blv);
1705 Lisp_Object symbol;
1706 XSETSYMBOL (symbol, sym); /* In case `variable' is aliased. */
1707 if (let_shadows_global_binding_p (symbol))
1708 message ("Making %s local to %s while let-bound!",
1709 SDATA (SYMBOL_NAME (variable)),
1710 SDATA (current_buffer->name));
1714 /* Make sure this buffer has its own value of symbol. */
1715 XSETSYMBOL (variable, sym); /* Update in case of aliasing. */
1716 tem = Fassq (variable, current_buffer->local_var_alist);
1717 if (NILP (tem))
1719 if (let_shadows_buffer_binding_p (sym))
1720 message ("Making %s buffer-local while locally let-bound!",
1721 SDATA (SYMBOL_NAME (variable)));
1723 /* Swap out any local binding for some other buffer, and make
1724 sure the current value is permanently recorded, if it's the
1725 default value. */
1726 find_symbol_value (variable);
1728 current_buffer->local_var_alist
1729 = Fcons (Fcons (variable, XCDR (blv->defcell)),
1730 current_buffer->local_var_alist);
1732 /* Make sure symbol does not think it is set up for this buffer;
1733 force it to look once again for this buffer's value. */
1734 if (current_buffer == XBUFFER (blv->where))
1735 blv->where = Qnil;
1736 /* blv->valcell = blv->defcell;
1737 * SET_BLV_FOUND (blv, 0); */
1738 blv->found = 0;
1741 /* If the symbol forwards into a C variable, then load the binding
1742 for this buffer now. If C code modifies the variable before we
1743 load the binding in, then that new value will clobber the default
1744 binding the next time we unload it. */
1745 if (blv->fwd)
1746 swap_in_symval_forwarding (sym, blv);
1748 return variable;
1751 DEFUN ("kill-local-variable", Fkill_local_variable, Skill_local_variable,
1752 1, 1, "vKill Local Variable: ",
1753 doc: /* Make VARIABLE no longer have a separate value in the current buffer.
1754 From now on the default value will apply in this buffer. Return VARIABLE. */)
1755 (variable)
1756 register Lisp_Object variable;
1758 register Lisp_Object tem;
1759 struct Lisp_Buffer_Local_Value *blv;
1760 struct Lisp_Symbol *sym;
1762 CHECK_SYMBOL (variable);
1763 sym = XSYMBOL (variable);
1765 start:
1766 switch (sym->redirect)
1768 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1769 case SYMBOL_PLAINVAL: return variable;
1770 case SYMBOL_FORWARDED:
1772 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
1773 if (BUFFER_OBJFWDP (valcontents))
1775 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1776 int idx = PER_BUFFER_IDX (offset);
1778 if (idx > 0)
1780 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 0);
1781 PER_BUFFER_VALUE (current_buffer, offset)
1782 = PER_BUFFER_DEFAULT (offset);
1785 return variable;
1787 case SYMBOL_LOCALIZED:
1788 blv = SYMBOL_BLV (sym);
1789 if (blv->frame_local)
1790 return variable;
1791 break;
1792 default: abort ();
1795 /* Get rid of this buffer's alist element, if any. */
1796 XSETSYMBOL (variable, sym); /* Propagate variable indirection. */
1797 tem = Fassq (variable, current_buffer->local_var_alist);
1798 if (!NILP (tem))
1799 current_buffer->local_var_alist
1800 = Fdelq (tem, current_buffer->local_var_alist);
1802 /* If the symbol is set up with the current buffer's binding
1803 loaded, recompute its value. We have to do it now, or else
1804 forwarded objects won't work right. */
1806 Lisp_Object buf; XSETBUFFER (buf, current_buffer);
1807 if (EQ (buf, blv->where))
1809 blv->where = Qnil;
1810 /* blv->valcell = blv->defcell;
1811 * SET_BLV_FOUND (blv, 0); */
1812 blv->found = 0;
1813 find_symbol_value (variable);
1817 return variable;
1820 /* Lisp functions for creating and removing buffer-local variables. */
1822 /* Obsolete since 22.2. NB adjust doc of modify-frame-parameters
1823 when/if this is removed. */
1825 DEFUN ("make-variable-frame-local", Fmake_variable_frame_local, Smake_variable_frame_local,
1826 1, 1, "vMake Variable Frame Local: ",
1827 doc: /* Enable VARIABLE to have frame-local bindings.
1828 This does not create any frame-local bindings for VARIABLE,
1829 it just makes them possible.
1831 A frame-local binding is actually a frame parameter value.
1832 If a frame F has a value for the frame parameter named VARIABLE,
1833 that also acts as a frame-local binding for VARIABLE in F--
1834 provided this function has been called to enable VARIABLE
1835 to have frame-local bindings at all.
1837 The only way to create a frame-local binding for VARIABLE in a frame
1838 is to set the VARIABLE frame parameter of that frame. See
1839 `modify-frame-parameters' for how to set frame parameters.
1841 Note that since Emacs 23.1, variables cannot be both buffer-local and
1842 frame-local any more (buffer-local bindings used to take precedence over
1843 frame-local bindings). */)
1844 (variable)
1845 register Lisp_Object variable;
1847 int forwarded;
1848 union Lisp_Val_Fwd valcontents;
1849 struct Lisp_Symbol *sym;
1850 struct Lisp_Buffer_Local_Value *blv = NULL;
1852 CHECK_SYMBOL (variable);
1853 sym = XSYMBOL (variable);
1855 start:
1856 switch (sym->redirect)
1858 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1859 case SYMBOL_PLAINVAL:
1860 forwarded = 0; valcontents.value = SYMBOL_VAL (sym);
1861 if (EQ (valcontents.value, Qunbound))
1862 valcontents.value = Qnil;
1863 break;
1864 case SYMBOL_LOCALIZED:
1865 if (SYMBOL_BLV (sym)->frame_local)
1866 return variable;
1867 else
1868 error ("Symbol %s may not be frame-local",
1869 SDATA (SYMBOL_NAME (variable)));
1870 case SYMBOL_FORWARDED:
1871 forwarded = 1; valcontents.fwd = SYMBOL_FWD (sym);
1872 if (KBOARD_OBJFWDP (valcontents.fwd) || BUFFER_OBJFWDP (valcontents.fwd))
1873 error ("Symbol %s may not be frame-local",
1874 SDATA (SYMBOL_NAME (variable)));
1875 break;
1876 default: abort ();
1879 if (sym->constant)
1880 error ("Symbol %s may not be frame-local", SDATA (SYMBOL_NAME (variable)));
1882 blv = make_blv (sym, forwarded, valcontents);
1883 blv->frame_local = 1;
1884 sym->redirect = SYMBOL_LOCALIZED;
1885 SET_SYMBOL_BLV (sym, blv);
1887 Lisp_Object symbol;
1888 XSETSYMBOL (symbol, sym); /* In case `variable' is aliased. */
1889 if (let_shadows_global_binding_p (symbol))
1890 message ("Making %s frame-local while let-bound!",
1891 SDATA (SYMBOL_NAME (variable)));
1893 return variable;
1896 DEFUN ("local-variable-p", Flocal_variable_p, Slocal_variable_p,
1897 1, 2, 0,
1898 doc: /* Non-nil if VARIABLE has a local binding in buffer BUFFER.
1899 BUFFER defaults to the current buffer. */)
1900 (variable, buffer)
1901 register Lisp_Object variable, buffer;
1903 register struct buffer *buf;
1904 struct Lisp_Symbol *sym;
1906 if (NILP (buffer))
1907 buf = current_buffer;
1908 else
1910 CHECK_BUFFER (buffer);
1911 buf = XBUFFER (buffer);
1914 CHECK_SYMBOL (variable);
1915 sym = XSYMBOL (variable);
1917 start:
1918 switch (sym->redirect)
1920 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1921 case SYMBOL_PLAINVAL: return Qnil;
1922 case SYMBOL_LOCALIZED:
1924 Lisp_Object tail, elt, tmp;
1925 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1926 XSETBUFFER (tmp, buf);
1928 for (tail = buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
1930 elt = XCAR (tail);
1931 if (EQ (variable, XCAR (elt)))
1933 eassert (!blv->frame_local);
1934 eassert (BLV_FOUND (blv) || !EQ (blv->where, tmp));
1935 return Qt;
1938 eassert (!BLV_FOUND (blv) || !EQ (blv->where, tmp));
1939 return Qnil;
1941 case SYMBOL_FORWARDED:
1943 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
1944 if (BUFFER_OBJFWDP (valcontents))
1946 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1947 int idx = PER_BUFFER_IDX (offset);
1948 if (idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
1949 return Qt;
1951 return Qnil;
1953 default: abort ();
1957 DEFUN ("local-variable-if-set-p", Flocal_variable_if_set_p, Slocal_variable_if_set_p,
1958 1, 2, 0,
1959 doc: /* Non-nil if VARIABLE will be local in buffer BUFFER when set there.
1960 More precisely, this means that setting the variable \(with `set' or`setq'),
1961 while it does not have a `let'-style binding that was made in BUFFER,
1962 will produce a buffer local binding. See Info node
1963 `(elisp)Creating Buffer-Local'.
1964 BUFFER defaults to the current buffer. */)
1965 (variable, buffer)
1966 register Lisp_Object variable, buffer;
1968 struct Lisp_Symbol *sym;
1970 CHECK_SYMBOL (variable);
1971 sym = XSYMBOL (variable);
1973 start:
1974 switch (sym->redirect)
1976 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
1977 case SYMBOL_PLAINVAL: return Qnil;
1978 case SYMBOL_LOCALIZED:
1980 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
1981 if (blv->local_if_set)
1982 return Qt;
1983 XSETSYMBOL (variable, sym); /* Update in case of aliasing. */
1984 return Flocal_variable_p (variable, buffer);
1986 case SYMBOL_FORWARDED:
1987 /* All BUFFER_OBJFWD slots become local if they are set. */
1988 return (BUFFER_OBJFWDP (SYMBOL_FWD (sym)) ? Qt : Qnil);
1989 default: abort ();
1993 DEFUN ("variable-binding-locus", Fvariable_binding_locus, Svariable_binding_locus,
1994 1, 1, 0,
1995 doc: /* Return a value indicating where VARIABLE's current binding comes from.
1996 If the current binding is buffer-local, the value is the current buffer.
1997 If the current binding is frame-local, the value is the selected frame.
1998 If the current binding is global (the default), the value is nil. */)
1999 (variable)
2000 register Lisp_Object variable;
2002 struct Lisp_Symbol *sym;
2004 CHECK_SYMBOL (variable);
2005 sym = XSYMBOL (variable);
2007 /* Make sure the current binding is actually swapped in. */
2008 find_symbol_value (variable);
2010 start:
2011 switch (sym->redirect)
2013 case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
2014 case SYMBOL_PLAINVAL: return Qnil;
2015 case SYMBOL_FORWARDED:
2017 union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
2018 if (KBOARD_OBJFWDP (valcontents))
2019 return Fframe_terminal (Fselected_frame ());
2020 else if (!BUFFER_OBJFWDP (valcontents))
2021 return Qnil;
2023 /* FALLTHROUGH */
2024 case SYMBOL_LOCALIZED:
2025 /* For a local variable, record both the symbol and which
2026 buffer's or frame's value we are saving. */
2027 if (!NILP (Flocal_variable_p (variable, Qnil)))
2028 return Fcurrent_buffer ();
2029 else if (sym->redirect == SYMBOL_LOCALIZED
2030 && BLV_FOUND (SYMBOL_BLV (sym)))
2031 return SYMBOL_BLV (sym)->where;
2032 else
2033 return Qnil;
2034 default: abort ();
2038 /* This code is disabled now that we use the selected frame to return
2039 keyboard-local-values. */
2040 #if 0
2041 extern struct terminal *get_terminal (Lisp_Object display, int);
2043 DEFUN ("terminal-local-value", Fterminal_local_value, Sterminal_local_value, 2, 2, 0,
2044 doc: /* Return the terminal-local value of SYMBOL on TERMINAL.
2045 If SYMBOL is not a terminal-local variable, then return its normal
2046 value, like `symbol-value'.
2048 TERMINAL may be a terminal object, a frame, or nil (meaning the
2049 selected frame's terminal device). */)
2050 (symbol, terminal)
2051 Lisp_Object symbol;
2052 Lisp_Object terminal;
2054 Lisp_Object result;
2055 struct terminal *t = get_terminal (terminal, 1);
2056 push_kboard (t->kboard);
2057 result = Fsymbol_value (symbol);
2058 pop_kboard ();
2059 return result;
2062 DEFUN ("set-terminal-local-value", Fset_terminal_local_value, Sset_terminal_local_value, 3, 3, 0,
2063 doc: /* Set the terminal-local binding of SYMBOL on TERMINAL to VALUE.
2064 If VARIABLE is not a terminal-local variable, then set its normal
2065 binding, like `set'.
2067 TERMINAL may be a terminal object, a frame, or nil (meaning the
2068 selected frame's terminal device). */)
2069 (symbol, terminal, value)
2070 Lisp_Object symbol;
2071 Lisp_Object terminal;
2072 Lisp_Object value;
2074 Lisp_Object result;
2075 struct terminal *t = get_terminal (terminal, 1);
2076 push_kboard (d->kboard);
2077 result = Fset (symbol, value);
2078 pop_kboard ();
2079 return result;
2081 #endif
2083 /* Find the function at the end of a chain of symbol function indirections. */
2085 /* If OBJECT is a symbol, find the end of its function chain and
2086 return the value found there. If OBJECT is not a symbol, just
2087 return it. If there is a cycle in the function chain, signal a
2088 cyclic-function-indirection error.
2090 This is like Findirect_function, except that it doesn't signal an
2091 error if the chain ends up unbound. */
2092 Lisp_Object
2093 indirect_function (register Lisp_Object object)
2095 Lisp_Object tortoise, hare;
2097 hare = tortoise = object;
2099 for (;;)
2101 if (!SYMBOLP (hare) || EQ (hare, Qunbound))
2102 break;
2103 hare = XSYMBOL (hare)->function;
2104 if (!SYMBOLP (hare) || EQ (hare, Qunbound))
2105 break;
2106 hare = XSYMBOL (hare)->function;
2108 tortoise = XSYMBOL (tortoise)->function;
2110 if (EQ (hare, tortoise))
2111 xsignal1 (Qcyclic_function_indirection, object);
2114 return hare;
2117 DEFUN ("indirect-function", Findirect_function, Sindirect_function, 1, 2, 0,
2118 doc: /* Return the function at the end of OBJECT's function chain.
2119 If OBJECT is not a symbol, just return it. Otherwise, follow all
2120 function indirections to find the final function binding and return it.
2121 If the final symbol in the chain is unbound, signal a void-function error.
2122 Optional arg NOERROR non-nil means to return nil instead of signalling.
2123 Signal a cyclic-function-indirection error if there is a loop in the
2124 function chain of symbols. */)
2125 (object, noerror)
2126 register Lisp_Object object;
2127 Lisp_Object noerror;
2129 Lisp_Object result;
2131 /* Optimize for no indirection. */
2132 result = object;
2133 if (SYMBOLP (result) && !EQ (result, Qunbound)
2134 && (result = XSYMBOL (result)->function, SYMBOLP (result)))
2135 result = indirect_function (result);
2136 if (!EQ (result, Qunbound))
2137 return result;
2139 if (NILP (noerror))
2140 xsignal1 (Qvoid_function, object);
2142 return Qnil;
2145 /* Extract and set vector and string elements */
2147 DEFUN ("aref", Faref, Saref, 2, 2, 0,
2148 doc: /* Return the element of ARRAY at index IDX.
2149 ARRAY may be a vector, a string, a char-table, a bool-vector,
2150 or a byte-code object. IDX starts at 0. */)
2151 (array, idx)
2152 register Lisp_Object array;
2153 Lisp_Object idx;
2155 register int idxval;
2157 CHECK_NUMBER (idx);
2158 idxval = XINT (idx);
2159 if (STRINGP (array))
2161 int c, idxval_byte;
2163 if (idxval < 0 || idxval >= SCHARS (array))
2164 args_out_of_range (array, idx);
2165 if (! STRING_MULTIBYTE (array))
2166 return make_number ((unsigned char) SREF (array, idxval));
2167 idxval_byte = string_char_to_byte (array, idxval);
2169 c = STRING_CHAR (SDATA (array) + idxval_byte);
2170 return make_number (c);
2172 else if (BOOL_VECTOR_P (array))
2174 int val;
2176 if (idxval < 0 || idxval >= XBOOL_VECTOR (array)->size)
2177 args_out_of_range (array, idx);
2179 val = (unsigned char) XBOOL_VECTOR (array)->data[idxval / BOOL_VECTOR_BITS_PER_CHAR];
2180 return (val & (1 << (idxval % BOOL_VECTOR_BITS_PER_CHAR)) ? Qt : Qnil);
2182 else if (CHAR_TABLE_P (array))
2184 CHECK_CHARACTER (idx);
2185 return CHAR_TABLE_REF (array, idxval);
2187 else
2189 int size = 0;
2190 if (VECTORP (array))
2191 size = XVECTOR (array)->size;
2192 else if (COMPILEDP (array))
2193 size = XVECTOR (array)->size & PSEUDOVECTOR_SIZE_MASK;
2194 else
2195 wrong_type_argument (Qarrayp, array);
2197 if (idxval < 0 || idxval >= size)
2198 args_out_of_range (array, idx);
2199 return XVECTOR (array)->contents[idxval];
2203 DEFUN ("aset", Faset, Saset, 3, 3, 0,
2204 doc: /* Store into the element of ARRAY at index IDX the value NEWELT.
2205 Return NEWELT. ARRAY may be a vector, a string, a char-table or a
2206 bool-vector. IDX starts at 0. */)
2207 (array, idx, newelt)
2208 register Lisp_Object array;
2209 Lisp_Object idx, newelt;
2211 register int idxval;
2213 CHECK_NUMBER (idx);
2214 idxval = XINT (idx);
2215 CHECK_ARRAY (array, Qarrayp);
2216 CHECK_IMPURE (array);
2218 if (VECTORP (array))
2220 if (idxval < 0 || idxval >= XVECTOR (array)->size)
2221 args_out_of_range (array, idx);
2222 XVECTOR (array)->contents[idxval] = newelt;
2224 else if (BOOL_VECTOR_P (array))
2226 int val;
2228 if (idxval < 0 || idxval >= XBOOL_VECTOR (array)->size)
2229 args_out_of_range (array, idx);
2231 val = (unsigned char) XBOOL_VECTOR (array)->data[idxval / BOOL_VECTOR_BITS_PER_CHAR];
2233 if (! NILP (newelt))
2234 val |= 1 << (idxval % BOOL_VECTOR_BITS_PER_CHAR);
2235 else
2236 val &= ~(1 << (idxval % BOOL_VECTOR_BITS_PER_CHAR));
2237 XBOOL_VECTOR (array)->data[idxval / BOOL_VECTOR_BITS_PER_CHAR] = val;
2239 else if (CHAR_TABLE_P (array))
2241 CHECK_CHARACTER (idx);
2242 CHAR_TABLE_SET (array, idxval, newelt);
2244 else if (STRING_MULTIBYTE (array))
2246 int idxval_byte, prev_bytes, new_bytes, nbytes;
2247 unsigned char workbuf[MAX_MULTIBYTE_LENGTH], *p0 = workbuf, *p1;
2249 if (idxval < 0 || idxval >= SCHARS (array))
2250 args_out_of_range (array, idx);
2251 CHECK_CHARACTER (newelt);
2253 nbytes = SBYTES (array);
2255 idxval_byte = string_char_to_byte (array, idxval);
2256 p1 = SDATA (array) + idxval_byte;
2257 prev_bytes = BYTES_BY_CHAR_HEAD (*p1);
2258 new_bytes = CHAR_STRING (XINT (newelt), p0);
2259 if (prev_bytes != new_bytes)
2261 /* We must relocate the string data. */
2262 int nchars = SCHARS (array);
2263 unsigned char *str;
2264 USE_SAFE_ALLOCA;
2266 SAFE_ALLOCA (str, unsigned char *, nbytes);
2267 bcopy (SDATA (array), str, nbytes);
2268 allocate_string_data (XSTRING (array), nchars,
2269 nbytes + new_bytes - prev_bytes);
2270 bcopy (str, SDATA (array), idxval_byte);
2271 p1 = SDATA (array) + idxval_byte;
2272 bcopy (str + idxval_byte + prev_bytes, p1 + new_bytes,
2273 nbytes - (idxval_byte + prev_bytes));
2274 SAFE_FREE ();
2275 clear_string_char_byte_cache ();
2277 while (new_bytes--)
2278 *p1++ = *p0++;
2280 else
2282 if (idxval < 0 || idxval >= SCHARS (array))
2283 args_out_of_range (array, idx);
2284 CHECK_NUMBER (newelt);
2286 if (XINT (newelt) >= 0 && ! SINGLE_BYTE_CHAR_P (XINT (newelt)))
2288 int i;
2290 for (i = SBYTES (array) - 1; i >= 0; i--)
2291 if (SREF (array, i) >= 0x80)
2292 args_out_of_range (array, newelt);
2293 /* ARRAY is an ASCII string. Convert it to a multibyte
2294 string, and try `aset' again. */
2295 STRING_SET_MULTIBYTE (array);
2296 return Faset (array, idx, newelt);
2298 SSET (array, idxval, XINT (newelt));
2301 return newelt;
2304 /* Arithmetic functions */
2306 enum comparison { equal, notequal, less, grtr, less_or_equal, grtr_or_equal };
2308 Lisp_Object
2309 arithcompare (Lisp_Object num1, Lisp_Object num2, enum comparison comparison)
2311 double f1 = 0, f2 = 0;
2312 int floatp = 0;
2314 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num1);
2315 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num2);
2317 if (FLOATP (num1) || FLOATP (num2))
2319 floatp = 1;
2320 f1 = (FLOATP (num1)) ? XFLOAT_DATA (num1) : XINT (num1);
2321 f2 = (FLOATP (num2)) ? XFLOAT_DATA (num2) : XINT (num2);
2324 switch (comparison)
2326 case equal:
2327 if (floatp ? f1 == f2 : XINT (num1) == XINT (num2))
2328 return Qt;
2329 return Qnil;
2331 case notequal:
2332 if (floatp ? f1 != f2 : XINT (num1) != XINT (num2))
2333 return Qt;
2334 return Qnil;
2336 case less:
2337 if (floatp ? f1 < f2 : XINT (num1) < XINT (num2))
2338 return Qt;
2339 return Qnil;
2341 case less_or_equal:
2342 if (floatp ? f1 <= f2 : XINT (num1) <= XINT (num2))
2343 return Qt;
2344 return Qnil;
2346 case grtr:
2347 if (floatp ? f1 > f2 : XINT (num1) > XINT (num2))
2348 return Qt;
2349 return Qnil;
2351 case grtr_or_equal:
2352 if (floatp ? f1 >= f2 : XINT (num1) >= XINT (num2))
2353 return Qt;
2354 return Qnil;
2356 default:
2357 abort ();
2361 DEFUN ("=", Feqlsign, Seqlsign, 2, 2, 0,
2362 doc: /* Return t if two args, both numbers or markers, are equal. */)
2363 (num1, num2)
2364 register Lisp_Object num1, num2;
2366 return arithcompare (num1, num2, equal);
2369 DEFUN ("<", Flss, Slss, 2, 2, 0,
2370 doc: /* Return t if first arg is less than second arg. Both must be numbers or markers. */)
2371 (num1, num2)
2372 register Lisp_Object num1, num2;
2374 return arithcompare (num1, num2, less);
2377 DEFUN (">", Fgtr, Sgtr, 2, 2, 0,
2378 doc: /* Return t if first arg is greater than second arg. Both must be numbers or markers. */)
2379 (num1, num2)
2380 register Lisp_Object num1, num2;
2382 return arithcompare (num1, num2, grtr);
2385 DEFUN ("<=", Fleq, Sleq, 2, 2, 0,
2386 doc: /* Return t if first arg is less than or equal to second arg.
2387 Both must be numbers or markers. */)
2388 (num1, num2)
2389 register Lisp_Object num1, num2;
2391 return arithcompare (num1, num2, less_or_equal);
2394 DEFUN (">=", Fgeq, Sgeq, 2, 2, 0,
2395 doc: /* Return t if first arg is greater than or equal to second arg.
2396 Both must be numbers or markers. */)
2397 (num1, num2)
2398 register Lisp_Object num1, num2;
2400 return arithcompare (num1, num2, grtr_or_equal);
2403 DEFUN ("/=", Fneq, Sneq, 2, 2, 0,
2404 doc: /* Return t if first arg is not equal to second arg. Both must be numbers or markers. */)
2405 (num1, num2)
2406 register Lisp_Object num1, num2;
2408 return arithcompare (num1, num2, notequal);
2411 DEFUN ("zerop", Fzerop, Szerop, 1, 1, 0,
2412 doc: /* Return t if NUMBER is zero. */)
2413 (number)
2414 register Lisp_Object number;
2416 CHECK_NUMBER_OR_FLOAT (number);
2418 if (FLOATP (number))
2420 if (XFLOAT_DATA (number) == 0.0)
2421 return Qt;
2422 return Qnil;
2425 if (!XINT (number))
2426 return Qt;
2427 return Qnil;
2430 /* Convert between long values and pairs of Lisp integers.
2431 Note that long_to_cons returns a single Lisp integer
2432 when the value fits in one. */
2434 Lisp_Object
2435 long_to_cons (long unsigned int i)
2437 unsigned long top = i >> 16;
2438 unsigned int bot = i & 0xFFFF;
2439 if (top == 0)
2440 return make_number (bot);
2441 if (top == (unsigned long)-1 >> 16)
2442 return Fcons (make_number (-1), make_number (bot));
2443 return Fcons (make_number (top), make_number (bot));
2446 unsigned long
2447 cons_to_long (Lisp_Object c)
2449 Lisp_Object top, bot;
2450 if (INTEGERP (c))
2451 return XINT (c);
2452 top = XCAR (c);
2453 bot = XCDR (c);
2454 if (CONSP (bot))
2455 bot = XCAR (bot);
2456 return ((XINT (top) << 16) | XINT (bot));
2459 DEFUN ("number-to-string", Fnumber_to_string, Snumber_to_string, 1, 1, 0,
2460 doc: /* Return the decimal representation of NUMBER as a string.
2461 Uses a minus sign if negative.
2462 NUMBER may be an integer or a floating point number. */)
2463 (number)
2464 Lisp_Object number;
2466 char buffer[VALBITS];
2468 CHECK_NUMBER_OR_FLOAT (number);
2470 if (FLOATP (number))
2472 char pigbuf[350]; /* see comments in float_to_string */
2474 float_to_string (pigbuf, XFLOAT_DATA (number));
2475 return build_string (pigbuf);
2478 if (sizeof (int) == sizeof (EMACS_INT))
2479 sprintf (buffer, "%d", (int) XINT (number));
2480 else if (sizeof (long) == sizeof (EMACS_INT))
2481 sprintf (buffer, "%ld", (long) XINT (number));
2482 else
2483 abort ();
2484 return build_string (buffer);
2487 INLINE static int
2488 digit_to_number (int character, int base)
2490 int digit;
2492 if (character >= '0' && character <= '9')
2493 digit = character - '0';
2494 else if (character >= 'a' && character <= 'z')
2495 digit = character - 'a' + 10;
2496 else if (character >= 'A' && character <= 'Z')
2497 digit = character - 'A' + 10;
2498 else
2499 return -1;
2501 if (digit >= base)
2502 return -1;
2503 else
2504 return digit;
2507 DEFUN ("string-to-number", Fstring_to_number, Sstring_to_number, 1, 2, 0,
2508 doc: /* Parse STRING as a decimal number and return the number.
2509 This parses both integers and floating point numbers.
2510 It ignores leading spaces and tabs, and all trailing chars.
2512 If BASE, interpret STRING as a number in that base. If BASE isn't
2513 present, base 10 is used. BASE must be between 2 and 16 (inclusive).
2514 If the base used is not 10, STRING is always parsed as integer. */)
2515 (string, base)
2516 register Lisp_Object string, base;
2518 register unsigned char *p;
2519 register int b;
2520 int sign = 1;
2521 Lisp_Object val;
2523 CHECK_STRING (string);
2525 if (NILP (base))
2526 b = 10;
2527 else
2529 CHECK_NUMBER (base);
2530 b = XINT (base);
2531 if (b < 2 || b > 16)
2532 xsignal1 (Qargs_out_of_range, base);
2535 /* Skip any whitespace at the front of the number. Some versions of
2536 atoi do this anyway, so we might as well make Emacs lisp consistent. */
2537 p = SDATA (string);
2538 while (*p == ' ' || *p == '\t')
2539 p++;
2541 if (*p == '-')
2543 sign = -1;
2544 p++;
2546 else if (*p == '+')
2547 p++;
2549 if (isfloat_string (p, 1) && b == 10)
2550 val = make_float (sign * atof (p));
2551 else
2553 double v = 0;
2555 while (1)
2557 int digit = digit_to_number (*p++, b);
2558 if (digit < 0)
2559 break;
2560 v = v * b + digit;
2563 val = make_fixnum_or_float (sign * v);
2566 return val;
2570 enum arithop
2572 Aadd,
2573 Asub,
2574 Amult,
2575 Adiv,
2576 Alogand,
2577 Alogior,
2578 Alogxor,
2579 Amax,
2580 Amin
2583 static Lisp_Object float_arith_driver (double, int, enum arithop,
2584 int, Lisp_Object *);
2585 extern Lisp_Object fmod_float ();
2587 Lisp_Object
2588 arith_driver (enum arithop code, int nargs, register Lisp_Object *args)
2590 register Lisp_Object val;
2591 register int argnum;
2592 register EMACS_INT accum = 0;
2593 register EMACS_INT next;
2595 switch (SWITCH_ENUM_CAST (code))
2597 case Alogior:
2598 case Alogxor:
2599 case Aadd:
2600 case Asub:
2601 accum = 0;
2602 break;
2603 case Amult:
2604 accum = 1;
2605 break;
2606 case Alogand:
2607 accum = -1;
2608 break;
2609 default:
2610 break;
2613 for (argnum = 0; argnum < nargs; argnum++)
2615 /* Using args[argnum] as argument to CHECK_NUMBER_... */
2616 val = args[argnum];
2617 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val);
2619 if (FLOATP (val))
2620 return float_arith_driver ((double) accum, argnum, code,
2621 nargs, args);
2622 args[argnum] = val;
2623 next = XINT (args[argnum]);
2624 switch (SWITCH_ENUM_CAST (code))
2626 case Aadd:
2627 accum += next;
2628 break;
2629 case Asub:
2630 accum = argnum ? accum - next : nargs == 1 ? - next : next;
2631 break;
2632 case Amult:
2633 accum *= next;
2634 break;
2635 case Adiv:
2636 if (!argnum)
2637 accum = next;
2638 else
2640 if (next == 0)
2641 xsignal0 (Qarith_error);
2642 accum /= next;
2644 break;
2645 case Alogand:
2646 accum &= next;
2647 break;
2648 case Alogior:
2649 accum |= next;
2650 break;
2651 case Alogxor:
2652 accum ^= next;
2653 break;
2654 case Amax:
2655 if (!argnum || next > accum)
2656 accum = next;
2657 break;
2658 case Amin:
2659 if (!argnum || next < accum)
2660 accum = next;
2661 break;
2665 XSETINT (val, accum);
2666 return val;
2669 #undef isnan
2670 #define isnan(x) ((x) != (x))
2672 static Lisp_Object
2673 float_arith_driver (double accum, register int argnum, enum arithop code, int nargs, register Lisp_Object *args)
2675 register Lisp_Object val;
2676 double next;
2678 for (; argnum < nargs; argnum++)
2680 val = args[argnum]; /* using args[argnum] as argument to CHECK_NUMBER_... */
2681 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val);
2683 if (FLOATP (val))
2685 next = XFLOAT_DATA (val);
2687 else
2689 args[argnum] = val; /* runs into a compiler bug. */
2690 next = XINT (args[argnum]);
2692 switch (SWITCH_ENUM_CAST (code))
2694 case Aadd:
2695 accum += next;
2696 break;
2697 case Asub:
2698 accum = argnum ? accum - next : nargs == 1 ? - next : next;
2699 break;
2700 case Amult:
2701 accum *= next;
2702 break;
2703 case Adiv:
2704 if (!argnum)
2705 accum = next;
2706 else
2708 if (! IEEE_FLOATING_POINT && next == 0)
2709 xsignal0 (Qarith_error);
2710 accum /= next;
2712 break;
2713 case Alogand:
2714 case Alogior:
2715 case Alogxor:
2716 return wrong_type_argument (Qinteger_or_marker_p, val);
2717 case Amax:
2718 if (!argnum || isnan (next) || next > accum)
2719 accum = next;
2720 break;
2721 case Amin:
2722 if (!argnum || isnan (next) || next < accum)
2723 accum = next;
2724 break;
2728 return make_float (accum);
2732 DEFUN ("+", Fplus, Splus, 0, MANY, 0,
2733 doc: /* Return sum of any number of arguments, which are numbers or markers.
2734 usage: (+ &rest NUMBERS-OR-MARKERS) */)
2735 (nargs, args)
2736 int nargs;
2737 Lisp_Object *args;
2739 return arith_driver (Aadd, nargs, args);
2742 DEFUN ("-", Fminus, Sminus, 0, MANY, 0,
2743 doc: /* Negate number or subtract numbers or markers and return the result.
2744 With one arg, negates it. With more than one arg,
2745 subtracts all but the first from the first.
2746 usage: (- &optional NUMBER-OR-MARKER &rest MORE-NUMBERS-OR-MARKERS) */)
2747 (nargs, args)
2748 int nargs;
2749 Lisp_Object *args;
2751 return arith_driver (Asub, nargs, args);
2754 DEFUN ("*", Ftimes, Stimes, 0, MANY, 0,
2755 doc: /* Return product of any number of arguments, which are numbers or markers.
2756 usage: (* &rest NUMBERS-OR-MARKERS) */)
2757 (nargs, args)
2758 int nargs;
2759 Lisp_Object *args;
2761 return arith_driver (Amult, nargs, args);
2764 DEFUN ("/", Fquo, Squo, 2, MANY, 0,
2765 doc: /* Return first argument divided by all the remaining arguments.
2766 The arguments must be numbers or markers.
2767 usage: (/ DIVIDEND DIVISOR &rest DIVISORS) */)
2768 (nargs, args)
2769 int nargs;
2770 Lisp_Object *args;
2772 int argnum;
2773 for (argnum = 2; argnum < nargs; argnum++)
2774 if (FLOATP (args[argnum]))
2775 return float_arith_driver (0, 0, Adiv, nargs, args);
2776 return arith_driver (Adiv, nargs, args);
2779 DEFUN ("%", Frem, Srem, 2, 2, 0,
2780 doc: /* Return remainder of X divided by Y.
2781 Both must be integers or markers. */)
2782 (x, y)
2783 register Lisp_Object x, y;
2785 Lisp_Object val;
2787 CHECK_NUMBER_COERCE_MARKER (x);
2788 CHECK_NUMBER_COERCE_MARKER (y);
2790 if (XFASTINT (y) == 0)
2791 xsignal0 (Qarith_error);
2793 XSETINT (val, XINT (x) % XINT (y));
2794 return val;
2797 #ifndef HAVE_FMOD
2798 double
2799 fmod (f1, f2)
2800 double f1, f2;
2802 double r = f1;
2804 if (f2 < 0.0)
2805 f2 = -f2;
2807 /* If the magnitude of the result exceeds that of the divisor, or
2808 the sign of the result does not agree with that of the dividend,
2809 iterate with the reduced value. This does not yield a
2810 particularly accurate result, but at least it will be in the
2811 range promised by fmod. */
2813 r -= f2 * floor (r / f2);
2814 while (f2 <= (r < 0 ? -r : r) || ((r < 0) != (f1 < 0) && ! isnan (r)));
2816 return r;
2818 #endif /* ! HAVE_FMOD */
2820 DEFUN ("mod", Fmod, Smod, 2, 2, 0,
2821 doc: /* Return X modulo Y.
2822 The result falls between zero (inclusive) and Y (exclusive).
2823 Both X and Y must be numbers or markers. */)
2824 (x, y)
2825 register Lisp_Object x, y;
2827 Lisp_Object val;
2828 EMACS_INT i1, i2;
2830 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (x);
2831 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (y);
2833 if (FLOATP (x) || FLOATP (y))
2834 return fmod_float (x, y);
2836 i1 = XINT (x);
2837 i2 = XINT (y);
2839 if (i2 == 0)
2840 xsignal0 (Qarith_error);
2842 i1 %= i2;
2844 /* If the "remainder" comes out with the wrong sign, fix it. */
2845 if (i2 < 0 ? i1 > 0 : i1 < 0)
2846 i1 += i2;
2848 XSETINT (val, i1);
2849 return val;
2852 DEFUN ("max", Fmax, Smax, 1, MANY, 0,
2853 doc: /* Return largest of all the arguments (which must be numbers or markers).
2854 The value is always a number; markers are converted to numbers.
2855 usage: (max NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2856 (nargs, args)
2857 int nargs;
2858 Lisp_Object *args;
2860 return arith_driver (Amax, nargs, args);
2863 DEFUN ("min", Fmin, Smin, 1, MANY, 0,
2864 doc: /* Return smallest of all the arguments (which must be numbers or markers).
2865 The value is always a number; markers are converted to numbers.
2866 usage: (min NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2867 (nargs, args)
2868 int nargs;
2869 Lisp_Object *args;
2871 return arith_driver (Amin, nargs, args);
2874 DEFUN ("logand", Flogand, Slogand, 0, MANY, 0,
2875 doc: /* Return bitwise-and of all the arguments.
2876 Arguments may be integers, or markers converted to integers.
2877 usage: (logand &rest INTS-OR-MARKERS) */)
2878 (nargs, args)
2879 int nargs;
2880 Lisp_Object *args;
2882 return arith_driver (Alogand, nargs, args);
2885 DEFUN ("logior", Flogior, Slogior, 0, MANY, 0,
2886 doc: /* Return bitwise-or of all the arguments.
2887 Arguments may be integers, or markers converted to integers.
2888 usage: (logior &rest INTS-OR-MARKERS) */)
2889 (nargs, args)
2890 int nargs;
2891 Lisp_Object *args;
2893 return arith_driver (Alogior, nargs, args);
2896 DEFUN ("logxor", Flogxor, Slogxor, 0, MANY, 0,
2897 doc: /* Return bitwise-exclusive-or of all the arguments.
2898 Arguments may be integers, or markers converted to integers.
2899 usage: (logxor &rest INTS-OR-MARKERS) */)
2900 (nargs, args)
2901 int nargs;
2902 Lisp_Object *args;
2904 return arith_driver (Alogxor, nargs, args);
2907 DEFUN ("ash", Fash, Sash, 2, 2, 0,
2908 doc: /* Return VALUE with its bits shifted left by COUNT.
2909 If COUNT is negative, shifting is actually to the right.
2910 In this case, the sign bit is duplicated. */)
2911 (value, count)
2912 register Lisp_Object value, count;
2914 register Lisp_Object val;
2916 CHECK_NUMBER (value);
2917 CHECK_NUMBER (count);
2919 if (XINT (count) >= BITS_PER_EMACS_INT)
2920 XSETINT (val, 0);
2921 else if (XINT (count) > 0)
2922 XSETINT (val, XINT (value) << XFASTINT (count));
2923 else if (XINT (count) <= -BITS_PER_EMACS_INT)
2924 XSETINT (val, XINT (value) < 0 ? -1 : 0);
2925 else
2926 XSETINT (val, XINT (value) >> -XINT (count));
2927 return val;
2930 DEFUN ("lsh", Flsh, Slsh, 2, 2, 0,
2931 doc: /* Return VALUE with its bits shifted left by COUNT.
2932 If COUNT is negative, shifting is actually to the right.
2933 In this case, zeros are shifted in on the left. */)
2934 (value, count)
2935 register Lisp_Object value, count;
2937 register Lisp_Object val;
2939 CHECK_NUMBER (value);
2940 CHECK_NUMBER (count);
2942 if (XINT (count) >= BITS_PER_EMACS_INT)
2943 XSETINT (val, 0);
2944 else if (XINT (count) > 0)
2945 XSETINT (val, (EMACS_UINT) XUINT (value) << XFASTINT (count));
2946 else if (XINT (count) <= -BITS_PER_EMACS_INT)
2947 XSETINT (val, 0);
2948 else
2949 XSETINT (val, (EMACS_UINT) XUINT (value) >> -XINT (count));
2950 return val;
2953 DEFUN ("1+", Fadd1, Sadd1, 1, 1, 0,
2954 doc: /* Return NUMBER plus one. NUMBER may be a number or a marker.
2955 Markers are converted to integers. */)
2956 (number)
2957 register Lisp_Object number;
2959 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number);
2961 if (FLOATP (number))
2962 return (make_float (1.0 + XFLOAT_DATA (number)));
2964 XSETINT (number, XINT (number) + 1);
2965 return number;
2968 DEFUN ("1-", Fsub1, Ssub1, 1, 1, 0,
2969 doc: /* Return NUMBER minus one. NUMBER may be a number or a marker.
2970 Markers are converted to integers. */)
2971 (number)
2972 register Lisp_Object number;
2974 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number);
2976 if (FLOATP (number))
2977 return (make_float (-1.0 + XFLOAT_DATA (number)));
2979 XSETINT (number, XINT (number) - 1);
2980 return number;
2983 DEFUN ("lognot", Flognot, Slognot, 1, 1, 0,
2984 doc: /* Return the bitwise complement of NUMBER. NUMBER must be an integer. */)
2985 (number)
2986 register Lisp_Object number;
2988 CHECK_NUMBER (number);
2989 XSETINT (number, ~XINT (number));
2990 return number;
2993 DEFUN ("byteorder", Fbyteorder, Sbyteorder, 0, 0, 0,
2994 doc: /* Return the byteorder for the machine.
2995 Returns 66 (ASCII uppercase B) for big endian machines or 108 (ASCII
2996 lowercase l) for small endian machines. */)
2999 unsigned i = 0x04030201;
3000 int order = *(char *)&i == 1 ? 108 : 66;
3002 return make_number (order);
3007 void
3008 syms_of_data (void)
3010 Lisp_Object error_tail, arith_tail;
3012 Qquote = intern_c_string ("quote");
3013 Qlambda = intern_c_string ("lambda");
3014 Qsubr = intern_c_string ("subr");
3015 Qerror_conditions = intern_c_string ("error-conditions");
3016 Qerror_message = intern_c_string ("error-message");
3017 Qtop_level = intern_c_string ("top-level");
3019 Qerror = intern_c_string ("error");
3020 Qquit = intern_c_string ("quit");
3021 Qwrong_type_argument = intern_c_string ("wrong-type-argument");
3022 Qargs_out_of_range = intern_c_string ("args-out-of-range");
3023 Qvoid_function = intern_c_string ("void-function");
3024 Qcyclic_function_indirection = intern_c_string ("cyclic-function-indirection");
3025 Qcyclic_variable_indirection = intern_c_string ("cyclic-variable-indirection");
3026 Qvoid_variable = intern_c_string ("void-variable");
3027 Qsetting_constant = intern_c_string ("setting-constant");
3028 Qinvalid_read_syntax = intern_c_string ("invalid-read-syntax");
3030 Qinvalid_function = intern_c_string ("invalid-function");
3031 Qwrong_number_of_arguments = intern_c_string ("wrong-number-of-arguments");
3032 Qno_catch = intern_c_string ("no-catch");
3033 Qend_of_file = intern_c_string ("end-of-file");
3034 Qarith_error = intern_c_string ("arith-error");
3035 Qbeginning_of_buffer = intern_c_string ("beginning-of-buffer");
3036 Qend_of_buffer = intern_c_string ("end-of-buffer");
3037 Qbuffer_read_only = intern_c_string ("buffer-read-only");
3038 Qtext_read_only = intern_c_string ("text-read-only");
3039 Qmark_inactive = intern_c_string ("mark-inactive");
3041 Qlistp = intern_c_string ("listp");
3042 Qconsp = intern_c_string ("consp");
3043 Qsymbolp = intern_c_string ("symbolp");
3044 Qkeywordp = intern_c_string ("keywordp");
3045 Qintegerp = intern_c_string ("integerp");
3046 Qnatnump = intern_c_string ("natnump");
3047 Qwholenump = intern_c_string ("wholenump");
3048 Qstringp = intern_c_string ("stringp");
3049 Qarrayp = intern_c_string ("arrayp");
3050 Qsequencep = intern_c_string ("sequencep");
3051 Qbufferp = intern_c_string ("bufferp");
3052 Qvectorp = intern_c_string ("vectorp");
3053 Qchar_or_string_p = intern_c_string ("char-or-string-p");
3054 Qmarkerp = intern_c_string ("markerp");
3055 Qbuffer_or_string_p = intern_c_string ("buffer-or-string-p");
3056 Qinteger_or_marker_p = intern_c_string ("integer-or-marker-p");
3057 Qboundp = intern_c_string ("boundp");
3058 Qfboundp = intern_c_string ("fboundp");
3060 Qfloatp = intern_c_string ("floatp");
3061 Qnumberp = intern_c_string ("numberp");
3062 Qnumber_or_marker_p = intern_c_string ("number-or-marker-p");
3064 Qchar_table_p = intern_c_string ("char-table-p");
3065 Qvector_or_char_table_p = intern_c_string ("vector-or-char-table-p");
3067 Qsubrp = intern_c_string ("subrp");
3068 Qunevalled = intern_c_string ("unevalled");
3069 Qmany = intern_c_string ("many");
3071 Qcdr = intern_c_string ("cdr");
3073 /* Handle automatic advice activation */
3074 Qad_advice_info = intern_c_string ("ad-advice-info");
3075 Qad_activate_internal = intern_c_string ("ad-activate-internal");
3077 error_tail = pure_cons (Qerror, Qnil);
3079 /* ERROR is used as a signaler for random errors for which nothing else is right */
3081 Fput (Qerror, Qerror_conditions,
3082 error_tail);
3083 Fput (Qerror, Qerror_message,
3084 make_pure_c_string ("error"));
3086 Fput (Qquit, Qerror_conditions,
3087 pure_cons (Qquit, Qnil));
3088 Fput (Qquit, Qerror_message,
3089 make_pure_c_string ("Quit"));
3091 Fput (Qwrong_type_argument, Qerror_conditions,
3092 pure_cons (Qwrong_type_argument, error_tail));
3093 Fput (Qwrong_type_argument, Qerror_message,
3094 make_pure_c_string ("Wrong type argument"));
3096 Fput (Qargs_out_of_range, Qerror_conditions,
3097 pure_cons (Qargs_out_of_range, error_tail));
3098 Fput (Qargs_out_of_range, Qerror_message,
3099 make_pure_c_string ("Args out of range"));
3101 Fput (Qvoid_function, Qerror_conditions,
3102 pure_cons (Qvoid_function, error_tail));
3103 Fput (Qvoid_function, Qerror_message,
3104 make_pure_c_string ("Symbol's function definition is void"));
3106 Fput (Qcyclic_function_indirection, Qerror_conditions,
3107 pure_cons (Qcyclic_function_indirection, error_tail));
3108 Fput (Qcyclic_function_indirection, Qerror_message,
3109 make_pure_c_string ("Symbol's chain of function indirections contains a loop"));
3111 Fput (Qcyclic_variable_indirection, Qerror_conditions,
3112 pure_cons (Qcyclic_variable_indirection, error_tail));
3113 Fput (Qcyclic_variable_indirection, Qerror_message,
3114 make_pure_c_string ("Symbol's chain of variable indirections contains a loop"));
3116 Qcircular_list = intern_c_string ("circular-list");
3117 staticpro (&Qcircular_list);
3118 Fput (Qcircular_list, Qerror_conditions,
3119 pure_cons (Qcircular_list, error_tail));
3120 Fput (Qcircular_list, Qerror_message,
3121 make_pure_c_string ("List contains a loop"));
3123 Fput (Qvoid_variable, Qerror_conditions,
3124 pure_cons (Qvoid_variable, error_tail));
3125 Fput (Qvoid_variable, Qerror_message,
3126 make_pure_c_string ("Symbol's value as variable is void"));
3128 Fput (Qsetting_constant, Qerror_conditions,
3129 pure_cons (Qsetting_constant, error_tail));
3130 Fput (Qsetting_constant, Qerror_message,
3131 make_pure_c_string ("Attempt to set a constant symbol"));
3133 Fput (Qinvalid_read_syntax, Qerror_conditions,
3134 pure_cons (Qinvalid_read_syntax, error_tail));
3135 Fput (Qinvalid_read_syntax, Qerror_message,
3136 make_pure_c_string ("Invalid read syntax"));
3138 Fput (Qinvalid_function, Qerror_conditions,
3139 pure_cons (Qinvalid_function, error_tail));
3140 Fput (Qinvalid_function, Qerror_message,
3141 make_pure_c_string ("Invalid function"));
3143 Fput (Qwrong_number_of_arguments, Qerror_conditions,
3144 pure_cons (Qwrong_number_of_arguments, error_tail));
3145 Fput (Qwrong_number_of_arguments, Qerror_message,
3146 make_pure_c_string ("Wrong number of arguments"));
3148 Fput (Qno_catch, Qerror_conditions,
3149 pure_cons (Qno_catch, error_tail));
3150 Fput (Qno_catch, Qerror_message,
3151 make_pure_c_string ("No catch for tag"));
3153 Fput (Qend_of_file, Qerror_conditions,
3154 pure_cons (Qend_of_file, error_tail));
3155 Fput (Qend_of_file, Qerror_message,
3156 make_pure_c_string ("End of file during parsing"));
3158 arith_tail = pure_cons (Qarith_error, error_tail);
3159 Fput (Qarith_error, Qerror_conditions,
3160 arith_tail);
3161 Fput (Qarith_error, Qerror_message,
3162 make_pure_c_string ("Arithmetic error"));
3164 Fput (Qbeginning_of_buffer, Qerror_conditions,
3165 pure_cons (Qbeginning_of_buffer, error_tail));
3166 Fput (Qbeginning_of_buffer, Qerror_message,
3167 make_pure_c_string ("Beginning of buffer"));
3169 Fput (Qend_of_buffer, Qerror_conditions,
3170 pure_cons (Qend_of_buffer, error_tail));
3171 Fput (Qend_of_buffer, Qerror_message,
3172 make_pure_c_string ("End of buffer"));
3174 Fput (Qbuffer_read_only, Qerror_conditions,
3175 pure_cons (Qbuffer_read_only, error_tail));
3176 Fput (Qbuffer_read_only, Qerror_message,
3177 make_pure_c_string ("Buffer is read-only"));
3179 Fput (Qtext_read_only, Qerror_conditions,
3180 pure_cons (Qtext_read_only, error_tail));
3181 Fput (Qtext_read_only, Qerror_message,
3182 make_pure_c_string ("Text is read-only"));
3184 Qrange_error = intern_c_string ("range-error");
3185 Qdomain_error = intern_c_string ("domain-error");
3186 Qsingularity_error = intern_c_string ("singularity-error");
3187 Qoverflow_error = intern_c_string ("overflow-error");
3188 Qunderflow_error = intern_c_string ("underflow-error");
3190 Fput (Qdomain_error, Qerror_conditions,
3191 pure_cons (Qdomain_error, arith_tail));
3192 Fput (Qdomain_error, Qerror_message,
3193 make_pure_c_string ("Arithmetic domain error"));
3195 Fput (Qrange_error, Qerror_conditions,
3196 pure_cons (Qrange_error, arith_tail));
3197 Fput (Qrange_error, Qerror_message,
3198 make_pure_c_string ("Arithmetic range error"));
3200 Fput (Qsingularity_error, Qerror_conditions,
3201 pure_cons (Qsingularity_error, Fcons (Qdomain_error, arith_tail)));
3202 Fput (Qsingularity_error, Qerror_message,
3203 make_pure_c_string ("Arithmetic singularity error"));
3205 Fput (Qoverflow_error, Qerror_conditions,
3206 pure_cons (Qoverflow_error, Fcons (Qdomain_error, arith_tail)));
3207 Fput (Qoverflow_error, Qerror_message,
3208 make_pure_c_string ("Arithmetic overflow error"));
3210 Fput (Qunderflow_error, Qerror_conditions,
3211 pure_cons (Qunderflow_error, Fcons (Qdomain_error, arith_tail)));
3212 Fput (Qunderflow_error, Qerror_message,
3213 make_pure_c_string ("Arithmetic underflow error"));
3215 staticpro (&Qrange_error);
3216 staticpro (&Qdomain_error);
3217 staticpro (&Qsingularity_error);
3218 staticpro (&Qoverflow_error);
3219 staticpro (&Qunderflow_error);
3221 staticpro (&Qnil);
3222 staticpro (&Qt);
3223 staticpro (&Qquote);
3224 staticpro (&Qlambda);
3225 staticpro (&Qsubr);
3226 staticpro (&Qunbound);
3227 staticpro (&Qerror_conditions);
3228 staticpro (&Qerror_message);
3229 staticpro (&Qtop_level);
3231 staticpro (&Qerror);
3232 staticpro (&Qquit);
3233 staticpro (&Qwrong_type_argument);
3234 staticpro (&Qargs_out_of_range);
3235 staticpro (&Qvoid_function);
3236 staticpro (&Qcyclic_function_indirection);
3237 staticpro (&Qcyclic_variable_indirection);
3238 staticpro (&Qvoid_variable);
3239 staticpro (&Qsetting_constant);
3240 staticpro (&Qinvalid_read_syntax);
3241 staticpro (&Qwrong_number_of_arguments);
3242 staticpro (&Qinvalid_function);
3243 staticpro (&Qno_catch);
3244 staticpro (&Qend_of_file);
3245 staticpro (&Qarith_error);
3246 staticpro (&Qbeginning_of_buffer);
3247 staticpro (&Qend_of_buffer);
3248 staticpro (&Qbuffer_read_only);
3249 staticpro (&Qtext_read_only);
3250 staticpro (&Qmark_inactive);
3252 staticpro (&Qlistp);
3253 staticpro (&Qconsp);
3254 staticpro (&Qsymbolp);
3255 staticpro (&Qkeywordp);
3256 staticpro (&Qintegerp);
3257 staticpro (&Qnatnump);
3258 staticpro (&Qwholenump);
3259 staticpro (&Qstringp);
3260 staticpro (&Qarrayp);
3261 staticpro (&Qsequencep);
3262 staticpro (&Qbufferp);
3263 staticpro (&Qvectorp);
3264 staticpro (&Qchar_or_string_p);
3265 staticpro (&Qmarkerp);
3266 staticpro (&Qbuffer_or_string_p);
3267 staticpro (&Qinteger_or_marker_p);
3268 staticpro (&Qfloatp);
3269 staticpro (&Qnumberp);
3270 staticpro (&Qnumber_or_marker_p);
3271 staticpro (&Qchar_table_p);
3272 staticpro (&Qvector_or_char_table_p);
3273 staticpro (&Qsubrp);
3274 staticpro (&Qmany);
3275 staticpro (&Qunevalled);
3277 staticpro (&Qboundp);
3278 staticpro (&Qfboundp);
3279 staticpro (&Qcdr);
3280 staticpro (&Qad_advice_info);
3281 staticpro (&Qad_activate_internal);
3283 /* Types that type-of returns. */
3284 Qinteger = intern_c_string ("integer");
3285 Qsymbol = intern_c_string ("symbol");
3286 Qstring = intern_c_string ("string");
3287 Qcons = intern_c_string ("cons");
3288 Qmarker = intern_c_string ("marker");
3289 Qoverlay = intern_c_string ("overlay");
3290 Qfloat = intern_c_string ("float");
3291 Qwindow_configuration = intern_c_string ("window-configuration");
3292 Qprocess = intern_c_string ("process");
3293 Qwindow = intern_c_string ("window");
3294 /* Qsubr = intern_c_string ("subr"); */
3295 Qcompiled_function = intern_c_string ("compiled-function");
3296 Qbuffer = intern_c_string ("buffer");
3297 Qframe = intern_c_string ("frame");
3298 Qvector = intern_c_string ("vector");
3299 Qchar_table = intern_c_string ("char-table");
3300 Qbool_vector = intern_c_string ("bool-vector");
3301 Qhash_table = intern_c_string ("hash-table");
3303 DEFSYM (Qfont_spec, "font-spec");
3304 DEFSYM (Qfont_entity, "font-entity");
3305 DEFSYM (Qfont_object, "font-object");
3307 DEFSYM (Qinteractive_form, "interactive-form");
3309 staticpro (&Qinteger);
3310 staticpro (&Qsymbol);
3311 staticpro (&Qstring);
3312 staticpro (&Qcons);
3313 staticpro (&Qmarker);
3314 staticpro (&Qoverlay);
3315 staticpro (&Qfloat);
3316 staticpro (&Qwindow_configuration);
3317 staticpro (&Qprocess);
3318 staticpro (&Qwindow);
3319 /* staticpro (&Qsubr); */
3320 staticpro (&Qcompiled_function);
3321 staticpro (&Qbuffer);
3322 staticpro (&Qframe);
3323 staticpro (&Qvector);
3324 staticpro (&Qchar_table);
3325 staticpro (&Qbool_vector);
3326 staticpro (&Qhash_table);
3328 defsubr (&Sindirect_variable);
3329 defsubr (&Sinteractive_form);
3330 defsubr (&Seq);
3331 defsubr (&Snull);
3332 defsubr (&Stype_of);
3333 defsubr (&Slistp);
3334 defsubr (&Snlistp);
3335 defsubr (&Sconsp);
3336 defsubr (&Satom);
3337 defsubr (&Sintegerp);
3338 defsubr (&Sinteger_or_marker_p);
3339 defsubr (&Snumberp);
3340 defsubr (&Snumber_or_marker_p);
3341 defsubr (&Sfloatp);
3342 defsubr (&Snatnump);
3343 defsubr (&Ssymbolp);
3344 defsubr (&Skeywordp);
3345 defsubr (&Sstringp);
3346 defsubr (&Smultibyte_string_p);
3347 defsubr (&Svectorp);
3348 defsubr (&Schar_table_p);
3349 defsubr (&Svector_or_char_table_p);
3350 defsubr (&Sbool_vector_p);
3351 defsubr (&Sarrayp);
3352 defsubr (&Ssequencep);
3353 defsubr (&Sbufferp);
3354 defsubr (&Smarkerp);
3355 defsubr (&Ssubrp);
3356 defsubr (&Sbyte_code_function_p);
3357 defsubr (&Schar_or_string_p);
3358 defsubr (&Scar);
3359 defsubr (&Scdr);
3360 defsubr (&Scar_safe);
3361 defsubr (&Scdr_safe);
3362 defsubr (&Ssetcar);
3363 defsubr (&Ssetcdr);
3364 defsubr (&Ssymbol_function);
3365 defsubr (&Sindirect_function);
3366 defsubr (&Ssymbol_plist);
3367 defsubr (&Ssymbol_name);
3368 defsubr (&Smakunbound);
3369 defsubr (&Sfmakunbound);
3370 defsubr (&Sboundp);
3371 defsubr (&Sfboundp);
3372 defsubr (&Sfset);
3373 defsubr (&Sdefalias);
3374 defsubr (&Ssetplist);
3375 defsubr (&Ssymbol_value);
3376 defsubr (&Sset);
3377 defsubr (&Sdefault_boundp);
3378 defsubr (&Sdefault_value);
3379 defsubr (&Sset_default);
3380 defsubr (&Ssetq_default);
3381 defsubr (&Smake_variable_buffer_local);
3382 defsubr (&Smake_local_variable);
3383 defsubr (&Skill_local_variable);
3384 defsubr (&Smake_variable_frame_local);
3385 defsubr (&Slocal_variable_p);
3386 defsubr (&Slocal_variable_if_set_p);
3387 defsubr (&Svariable_binding_locus);
3388 #if 0 /* XXX Remove this. --lorentey */
3389 defsubr (&Sterminal_local_value);
3390 defsubr (&Sset_terminal_local_value);
3391 #endif
3392 defsubr (&Saref);
3393 defsubr (&Saset);
3394 defsubr (&Snumber_to_string);
3395 defsubr (&Sstring_to_number);
3396 defsubr (&Seqlsign);
3397 defsubr (&Slss);
3398 defsubr (&Sgtr);
3399 defsubr (&Sleq);
3400 defsubr (&Sgeq);
3401 defsubr (&Sneq);
3402 defsubr (&Szerop);
3403 defsubr (&Splus);
3404 defsubr (&Sminus);
3405 defsubr (&Stimes);
3406 defsubr (&Squo);
3407 defsubr (&Srem);
3408 defsubr (&Smod);
3409 defsubr (&Smax);
3410 defsubr (&Smin);
3411 defsubr (&Slogand);
3412 defsubr (&Slogior);
3413 defsubr (&Slogxor);
3414 defsubr (&Slsh);
3415 defsubr (&Sash);
3416 defsubr (&Sadd1);
3417 defsubr (&Ssub1);
3418 defsubr (&Slognot);
3419 defsubr (&Sbyteorder);
3420 defsubr (&Ssubr_arity);
3421 defsubr (&Ssubr_name);
3423 XSYMBOL (Qwholenump)->function = XSYMBOL (Qnatnump)->function;
3425 DEFVAR_LISP ("most-positive-fixnum", &Vmost_positive_fixnum,
3426 doc: /* The largest value that is representable in a Lisp integer. */);
3427 Vmost_positive_fixnum = make_number (MOST_POSITIVE_FIXNUM);
3428 XSYMBOL (intern_c_string ("most-positive-fixnum"))->constant = 1;
3430 DEFVAR_LISP ("most-negative-fixnum", &Vmost_negative_fixnum,
3431 doc: /* The smallest value that is representable in a Lisp integer. */);
3432 Vmost_negative_fixnum = make_number (MOST_NEGATIVE_FIXNUM);
3433 XSYMBOL (intern_c_string ("most-negative-fixnum"))->constant = 1;
3436 SIGTYPE
3437 arith_error (int signo)
3439 sigsetmask (SIGEMPTYMASK);
3441 SIGNAL_THREAD_CHECK (signo);
3442 xsignal0 (Qarith_error);
3445 void
3446 init_data (void)
3448 /* Don't do this if just dumping out.
3449 We don't want to call `signal' in this case
3450 so that we don't have trouble with dumping
3451 signal-delivering routines in an inconsistent state. */
3452 #ifndef CANNOT_DUMP
3453 if (!initialized)
3454 return;
3455 #endif /* CANNOT_DUMP */
3456 signal (SIGFPE, arith_error);
3458 #ifdef uts
3459 signal (SIGEMT, arith_error);
3460 #endif /* uts */
3463 /* arch-tag: 25879798-b84d-479a-9c89-7d148e2109f7
3464 (do not change this comment) */