Fix crash with invisible text and overlays (Bug#7016).
[emacs.git] / src / data.c
blobe405ca6af9eb2870d80ab9838c3aaad970284eb9
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 ();
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 Lisp_Object swap_in_symval_forwarding P_ ((Lisp_Object, Lisp_Object));
96 Lisp_Object Vmost_positive_fixnum, Vmost_negative_fixnum;
99 void
100 circular_list_error (list)
101 Lisp_Object list;
103 xsignal (Qcircular_list, list);
107 Lisp_Object
108 wrong_type_argument (predicate, value)
109 register Lisp_Object predicate, value;
111 /* If VALUE is not even a valid Lisp object, we'd want to abort here
112 where we can get a backtrace showing where it came from. We used
113 to try and do that by checking the tagbits, but nowadays all
114 tagbits are potentially valid. */
115 /* if ((unsigned int) XTYPE (value) >= Lisp_Type_Limit)
116 * abort (); */
118 xsignal2 (Qwrong_type_argument, predicate, value);
121 void
122 pure_write_error ()
124 error ("Attempt to modify read-only object");
127 void
128 args_out_of_range (a1, a2)
129 Lisp_Object a1, a2;
131 xsignal2 (Qargs_out_of_range, a1, a2);
134 void
135 args_out_of_range_3 (a1, a2, a3)
136 Lisp_Object a1, a2, a3;
138 xsignal3 (Qargs_out_of_range, a1, a2, a3);
141 /* On some machines, XINT needs a temporary location.
142 Here it is, in case it is needed. */
144 int sign_extend_temp;
146 /* On a few machines, XINT can only be done by calling this. */
149 sign_extend_lisp_int (num)
150 EMACS_INT num;
152 if (num & (((EMACS_INT) 1) << (VALBITS - 1)))
153 return num | (((EMACS_INT) (-1)) << VALBITS);
154 else
155 return num & ((((EMACS_INT) 1) << VALBITS) - 1);
158 /* Data type predicates */
160 DEFUN ("eq", Feq, Seq, 2, 2, 0,
161 doc: /* Return t if the two args are the same Lisp object. */)
162 (obj1, obj2)
163 Lisp_Object obj1, obj2;
165 if (EQ (obj1, obj2))
166 return Qt;
167 return Qnil;
170 DEFUN ("null", Fnull, Snull, 1, 1, 0,
171 doc: /* Return t if OBJECT is nil. */)
172 (object)
173 Lisp_Object object;
175 if (NILP (object))
176 return Qt;
177 return Qnil;
180 DEFUN ("type-of", Ftype_of, Stype_of, 1, 1, 0,
181 doc: /* Return a symbol representing the type of OBJECT.
182 The symbol returned names the object's basic type;
183 for example, (type-of 1) returns `integer'. */)
184 (object)
185 Lisp_Object object;
187 switch (XTYPE (object))
189 case_Lisp_Int:
190 return Qinteger;
192 case Lisp_Symbol:
193 return Qsymbol;
195 case Lisp_String:
196 return Qstring;
198 case Lisp_Cons:
199 return Qcons;
201 case Lisp_Misc:
202 switch (XMISCTYPE (object))
204 case Lisp_Misc_Marker:
205 return Qmarker;
206 case Lisp_Misc_Overlay:
207 return Qoverlay;
208 case Lisp_Misc_Float:
209 return Qfloat;
211 abort ();
213 case Lisp_Vectorlike:
214 if (WINDOW_CONFIGURATIONP (object))
215 return Qwindow_configuration;
216 if (PROCESSP (object))
217 return Qprocess;
218 if (WINDOWP (object))
219 return Qwindow;
220 if (SUBRP (object))
221 return Qsubr;
222 if (COMPILEDP (object))
223 return Qcompiled_function;
224 if (BUFFERP (object))
225 return Qbuffer;
226 if (CHAR_TABLE_P (object))
227 return Qchar_table;
228 if (BOOL_VECTOR_P (object))
229 return Qbool_vector;
230 if (FRAMEP (object))
231 return Qframe;
232 if (HASH_TABLE_P (object))
233 return Qhash_table;
234 if (FONT_SPEC_P (object))
235 return Qfont_spec;
236 if (FONT_ENTITY_P (object))
237 return Qfont_entity;
238 if (FONT_OBJECT_P (object))
239 return Qfont_object;
240 return Qvector;
242 case Lisp_Float:
243 return Qfloat;
245 default:
246 abort ();
250 DEFUN ("consp", Fconsp, Sconsp, 1, 1, 0,
251 doc: /* Return t if OBJECT is a cons cell. */)
252 (object)
253 Lisp_Object object;
255 if (CONSP (object))
256 return Qt;
257 return Qnil;
260 DEFUN ("atom", Fatom, Satom, 1, 1, 0,
261 doc: /* Return t if OBJECT is not a cons cell. This includes nil. */)
262 (object)
263 Lisp_Object object;
265 if (CONSP (object))
266 return Qnil;
267 return Qt;
270 DEFUN ("listp", Flistp, Slistp, 1, 1, 0,
271 doc: /* Return t if OBJECT is a list, that is, a cons cell or nil.
272 Otherwise, return nil. */)
273 (object)
274 Lisp_Object object;
276 if (CONSP (object) || NILP (object))
277 return Qt;
278 return Qnil;
281 DEFUN ("nlistp", Fnlistp, Snlistp, 1, 1, 0,
282 doc: /* Return t if OBJECT is not a list. Lists include nil. */)
283 (object)
284 Lisp_Object object;
286 if (CONSP (object) || NILP (object))
287 return Qnil;
288 return Qt;
291 DEFUN ("symbolp", Fsymbolp, Ssymbolp, 1, 1, 0,
292 doc: /* Return t if OBJECT is a symbol. */)
293 (object)
294 Lisp_Object object;
296 if (SYMBOLP (object))
297 return Qt;
298 return Qnil;
301 /* Define this in C to avoid unnecessarily consing up the symbol
302 name. */
303 DEFUN ("keywordp", Fkeywordp, Skeywordp, 1, 1, 0,
304 doc: /* Return t if OBJECT is a keyword.
305 This means that it is a symbol with a print name beginning with `:'
306 interned in the initial obarray. */)
307 (object)
308 Lisp_Object object;
310 if (SYMBOLP (object)
311 && SREF (SYMBOL_NAME (object), 0) == ':'
312 && SYMBOL_INTERNED_IN_INITIAL_OBARRAY_P (object))
313 return Qt;
314 return Qnil;
317 DEFUN ("vectorp", Fvectorp, Svectorp, 1, 1, 0,
318 doc: /* Return t if OBJECT is a vector. */)
319 (object)
320 Lisp_Object object;
322 if (VECTORP (object))
323 return Qt;
324 return Qnil;
327 DEFUN ("stringp", Fstringp, Sstringp, 1, 1, 0,
328 doc: /* Return t if OBJECT is a string. */)
329 (object)
330 Lisp_Object object;
332 if (STRINGP (object))
333 return Qt;
334 return Qnil;
337 DEFUN ("multibyte-string-p", Fmultibyte_string_p, Smultibyte_string_p,
338 1, 1, 0,
339 doc: /* Return t if OBJECT is a multibyte string. */)
340 (object)
341 Lisp_Object object;
343 if (STRINGP (object) && STRING_MULTIBYTE (object))
344 return Qt;
345 return Qnil;
348 DEFUN ("char-table-p", Fchar_table_p, Schar_table_p, 1, 1, 0,
349 doc: /* Return t if OBJECT is a char-table. */)
350 (object)
351 Lisp_Object object;
353 if (CHAR_TABLE_P (object))
354 return Qt;
355 return Qnil;
358 DEFUN ("vector-or-char-table-p", Fvector_or_char_table_p,
359 Svector_or_char_table_p, 1, 1, 0,
360 doc: /* Return t if OBJECT is a char-table or vector. */)
361 (object)
362 Lisp_Object object;
364 if (VECTORP (object) || CHAR_TABLE_P (object))
365 return Qt;
366 return Qnil;
369 DEFUN ("bool-vector-p", Fbool_vector_p, Sbool_vector_p, 1, 1, 0,
370 doc: /* Return t if OBJECT is a bool-vector. */)
371 (object)
372 Lisp_Object object;
374 if (BOOL_VECTOR_P (object))
375 return Qt;
376 return Qnil;
379 DEFUN ("arrayp", Farrayp, Sarrayp, 1, 1, 0,
380 doc: /* Return t if OBJECT is an array (string or vector). */)
381 (object)
382 Lisp_Object object;
384 if (ARRAYP (object))
385 return Qt;
386 return Qnil;
389 DEFUN ("sequencep", Fsequencep, Ssequencep, 1, 1, 0,
390 doc: /* Return t if OBJECT is a sequence (list or array). */)
391 (object)
392 register Lisp_Object object;
394 if (CONSP (object) || NILP (object) || ARRAYP (object))
395 return Qt;
396 return Qnil;
399 DEFUN ("bufferp", Fbufferp, Sbufferp, 1, 1, 0,
400 doc: /* Return t if OBJECT is an editor buffer. */)
401 (object)
402 Lisp_Object object;
404 if (BUFFERP (object))
405 return Qt;
406 return Qnil;
409 DEFUN ("markerp", Fmarkerp, Smarkerp, 1, 1, 0,
410 doc: /* Return t if OBJECT is a marker (editor pointer). */)
411 (object)
412 Lisp_Object object;
414 if (MARKERP (object))
415 return Qt;
416 return Qnil;
419 DEFUN ("subrp", Fsubrp, Ssubrp, 1, 1, 0,
420 doc: /* Return t if OBJECT is a built-in function. */)
421 (object)
422 Lisp_Object object;
424 if (SUBRP (object))
425 return Qt;
426 return Qnil;
429 DEFUN ("byte-code-function-p", Fbyte_code_function_p, Sbyte_code_function_p,
430 1, 1, 0,
431 doc: /* Return t if OBJECT is a byte-compiled function object. */)
432 (object)
433 Lisp_Object object;
435 if (COMPILEDP (object))
436 return Qt;
437 return Qnil;
440 DEFUN ("char-or-string-p", Fchar_or_string_p, Schar_or_string_p, 1, 1, 0,
441 doc: /* Return t if OBJECT is a character or a string. */)
442 (object)
443 register Lisp_Object object;
445 if (CHARACTERP (object) || STRINGP (object))
446 return Qt;
447 return Qnil;
450 DEFUN ("integerp", Fintegerp, Sintegerp, 1, 1, 0,
451 doc: /* Return t if OBJECT is an integer. */)
452 (object)
453 Lisp_Object object;
455 if (INTEGERP (object))
456 return Qt;
457 return Qnil;
460 DEFUN ("integer-or-marker-p", Finteger_or_marker_p, Sinteger_or_marker_p, 1, 1, 0,
461 doc: /* Return t if OBJECT is an integer or a marker (editor pointer). */)
462 (object)
463 register Lisp_Object object;
465 if (MARKERP (object) || INTEGERP (object))
466 return Qt;
467 return Qnil;
470 DEFUN ("natnump", Fnatnump, Snatnump, 1, 1, 0,
471 doc: /* Return t if OBJECT is a nonnegative integer. */)
472 (object)
473 Lisp_Object object;
475 if (NATNUMP (object))
476 return Qt;
477 return Qnil;
480 DEFUN ("numberp", Fnumberp, Snumberp, 1, 1, 0,
481 doc: /* Return t if OBJECT is a number (floating point or integer). */)
482 (object)
483 Lisp_Object object;
485 if (NUMBERP (object))
486 return Qt;
487 else
488 return Qnil;
491 DEFUN ("number-or-marker-p", Fnumber_or_marker_p,
492 Snumber_or_marker_p, 1, 1, 0,
493 doc: /* Return t if OBJECT is a number or a marker. */)
494 (object)
495 Lisp_Object object;
497 if (NUMBERP (object) || MARKERP (object))
498 return Qt;
499 return Qnil;
502 DEFUN ("floatp", Ffloatp, Sfloatp, 1, 1, 0,
503 doc: /* Return t if OBJECT is a floating point number. */)
504 (object)
505 Lisp_Object object;
507 if (FLOATP (object))
508 return Qt;
509 return Qnil;
513 /* Extract and set components of lists */
515 DEFUN ("car", Fcar, Scar, 1, 1, 0,
516 doc: /* Return the car of LIST. If arg is nil, return nil.
517 Error if arg is not nil and not a cons cell. See also `car-safe'.
519 See Info node `(elisp)Cons Cells' for a discussion of related basic
520 Lisp concepts such as car, cdr, cons cell and list. */)
521 (list)
522 register Lisp_Object list;
524 return CAR (list);
527 DEFUN ("car-safe", Fcar_safe, Scar_safe, 1, 1, 0,
528 doc: /* Return the car of OBJECT if it is a cons cell, or else nil. */)
529 (object)
530 Lisp_Object object;
532 return CAR_SAFE (object);
535 DEFUN ("cdr", Fcdr, Scdr, 1, 1, 0,
536 doc: /* Return the cdr of LIST. If arg is nil, return nil.
537 Error if arg is not nil and not a cons cell. See also `cdr-safe'.
539 See Info node `(elisp)Cons Cells' for a discussion of related basic
540 Lisp concepts such as cdr, car, cons cell and list. */)
541 (list)
542 register Lisp_Object list;
544 return CDR (list);
547 DEFUN ("cdr-safe", Fcdr_safe, Scdr_safe, 1, 1, 0,
548 doc: /* Return the cdr of OBJECT if it is a cons cell, or else nil. */)
549 (object)
550 Lisp_Object object;
552 return CDR_SAFE (object);
555 DEFUN ("setcar", Fsetcar, Ssetcar, 2, 2, 0,
556 doc: /* Set the car of CELL to be NEWCAR. Returns NEWCAR. */)
557 (cell, newcar)
558 register Lisp_Object cell, newcar;
560 CHECK_CONS (cell);
561 CHECK_IMPURE (cell);
562 XSETCAR (cell, newcar);
563 return newcar;
566 DEFUN ("setcdr", Fsetcdr, Ssetcdr, 2, 2, 0,
567 doc: /* Set the cdr of CELL to be NEWCDR. Returns NEWCDR. */)
568 (cell, newcdr)
569 register Lisp_Object cell, newcdr;
571 CHECK_CONS (cell);
572 CHECK_IMPURE (cell);
573 XSETCDR (cell, newcdr);
574 return newcdr;
577 /* Extract and set components of symbols */
579 DEFUN ("boundp", Fboundp, Sboundp, 1, 1, 0,
580 doc: /* Return t if SYMBOL's value is not void. */)
581 (symbol)
582 register Lisp_Object symbol;
584 Lisp_Object valcontents;
585 CHECK_SYMBOL (symbol);
587 valcontents = SYMBOL_VALUE (symbol);
589 if (BUFFER_LOCAL_VALUEP (valcontents))
590 valcontents = swap_in_symval_forwarding (symbol, valcontents);
592 return (EQ (valcontents, Qunbound) ? Qnil : Qt);
595 DEFUN ("fboundp", Ffboundp, Sfboundp, 1, 1, 0,
596 doc: /* Return t if SYMBOL's function definition is not void. */)
597 (symbol)
598 register Lisp_Object symbol;
600 CHECK_SYMBOL (symbol);
601 return (EQ (XSYMBOL (symbol)->function, Qunbound) ? Qnil : Qt);
604 DEFUN ("makunbound", Fmakunbound, Smakunbound, 1, 1, 0,
605 doc: /* Make SYMBOL's value be void.
606 Return SYMBOL. */)
607 (symbol)
608 register Lisp_Object symbol;
610 CHECK_SYMBOL (symbol);
611 if (SYMBOL_CONSTANT_P (symbol))
612 xsignal1 (Qsetting_constant, symbol);
613 Fset (symbol, Qunbound);
614 return symbol;
617 DEFUN ("fmakunbound", Ffmakunbound, Sfmakunbound, 1, 1, 0,
618 doc: /* Make SYMBOL's function definition be void.
619 Return SYMBOL. */)
620 (symbol)
621 register Lisp_Object symbol;
623 CHECK_SYMBOL (symbol);
624 if (NILP (symbol) || EQ (symbol, Qt))
625 xsignal1 (Qsetting_constant, symbol);
626 XSYMBOL (symbol)->function = Qunbound;
627 return symbol;
630 DEFUN ("symbol-function", Fsymbol_function, Ssymbol_function, 1, 1, 0,
631 doc: /* Return SYMBOL's function definition. Error if that is void. */)
632 (symbol)
633 register Lisp_Object symbol;
635 CHECK_SYMBOL (symbol);
636 if (!EQ (XSYMBOL (symbol)->function, Qunbound))
637 return XSYMBOL (symbol)->function;
638 xsignal1 (Qvoid_function, symbol);
641 DEFUN ("symbol-plist", Fsymbol_plist, Ssymbol_plist, 1, 1, 0,
642 doc: /* Return SYMBOL's property list. */)
643 (symbol)
644 register Lisp_Object symbol;
646 CHECK_SYMBOL (symbol);
647 return XSYMBOL (symbol)->plist;
650 DEFUN ("symbol-name", Fsymbol_name, Ssymbol_name, 1, 1, 0,
651 doc: /* Return SYMBOL's name, a string. */)
652 (symbol)
653 register Lisp_Object symbol;
655 register Lisp_Object name;
657 CHECK_SYMBOL (symbol);
658 name = SYMBOL_NAME (symbol);
659 return name;
662 DEFUN ("fset", Ffset, Sfset, 2, 2, 0,
663 doc: /* Set SYMBOL's function definition to DEFINITION, and return DEFINITION. */)
664 (symbol, definition)
665 register Lisp_Object symbol, definition;
667 register Lisp_Object function;
669 CHECK_SYMBOL (symbol);
670 if (NILP (symbol) || EQ (symbol, Qt))
671 xsignal1 (Qsetting_constant, symbol);
673 function = XSYMBOL (symbol)->function;
675 if (!NILP (Vautoload_queue) && !EQ (function, Qunbound))
676 Vautoload_queue = Fcons (Fcons (symbol, function), Vautoload_queue);
678 if (CONSP (function) && EQ (XCAR (function), Qautoload))
679 Fput (symbol, Qautoload, XCDR (function));
681 XSYMBOL (symbol)->function = definition;
682 /* Handle automatic advice activation */
683 if (CONSP (XSYMBOL (symbol)->plist) && !NILP (Fget (symbol, Qad_advice_info)))
685 call2 (Qad_activate_internal, symbol, Qnil);
686 definition = XSYMBOL (symbol)->function;
688 return definition;
691 extern Lisp_Object Qfunction_documentation;
693 DEFUN ("defalias", Fdefalias, Sdefalias, 2, 3, 0,
694 doc: /* Set SYMBOL's function definition to DEFINITION, and return DEFINITION.
695 Associates the function with the current load file, if any.
696 The optional third argument DOCSTRING specifies the documentation string
697 for SYMBOL; if it is omitted or nil, SYMBOL uses the documentation string
698 determined by DEFINITION. */)
699 (symbol, definition, docstring)
700 register Lisp_Object symbol, definition, docstring;
702 CHECK_SYMBOL (symbol);
703 if (CONSP (XSYMBOL (symbol)->function)
704 && EQ (XCAR (XSYMBOL (symbol)->function), Qautoload))
705 LOADHIST_ATTACH (Fcons (Qt, symbol));
706 definition = Ffset (symbol, definition);
707 LOADHIST_ATTACH (Fcons (Qdefun, symbol));
708 if (!NILP (docstring))
709 Fput (symbol, Qfunction_documentation, docstring);
710 return definition;
713 DEFUN ("setplist", Fsetplist, Ssetplist, 2, 2, 0,
714 doc: /* Set SYMBOL's property list to NEWPLIST, and return NEWPLIST. */)
715 (symbol, newplist)
716 register Lisp_Object symbol, newplist;
718 CHECK_SYMBOL (symbol);
719 XSYMBOL (symbol)->plist = newplist;
720 return newplist;
723 DEFUN ("subr-arity", Fsubr_arity, Ssubr_arity, 1, 1, 0,
724 doc: /* Return minimum and maximum number of args allowed for SUBR.
725 SUBR must be a built-in function.
726 The returned value is a pair (MIN . MAX). MIN is the minimum number
727 of args. MAX is the maximum number or the symbol `many', for a
728 function with `&rest' args, or `unevalled' for a special form. */)
729 (subr)
730 Lisp_Object subr;
732 short minargs, maxargs;
733 CHECK_SUBR (subr);
734 minargs = XSUBR (subr)->min_args;
735 maxargs = XSUBR (subr)->max_args;
736 if (maxargs == MANY)
737 return Fcons (make_number (minargs), Qmany);
738 else if (maxargs == UNEVALLED)
739 return Fcons (make_number (minargs), Qunevalled);
740 else
741 return Fcons (make_number (minargs), make_number (maxargs));
744 DEFUN ("subr-name", Fsubr_name, Ssubr_name, 1, 1, 0,
745 doc: /* Return name of subroutine SUBR.
746 SUBR must be a built-in function. */)
747 (subr)
748 Lisp_Object subr;
750 const char *name;
751 CHECK_SUBR (subr);
752 name = XSUBR (subr)->symbol_name;
753 return make_string (name, strlen (name));
756 DEFUN ("interactive-form", Finteractive_form, Sinteractive_form, 1, 1, 0,
757 doc: /* Return the interactive form of CMD or nil if none.
758 If CMD is not a command, the return value is nil.
759 Value, if non-nil, is a list \(interactive SPEC). */)
760 (cmd)
761 Lisp_Object cmd;
763 Lisp_Object fun = indirect_function (cmd); /* Check cycles. */
765 if (NILP (fun) || EQ (fun, Qunbound))
766 return Qnil;
768 /* Use an `interactive-form' property if present, analogous to the
769 function-documentation property. */
770 fun = cmd;
771 while (SYMBOLP (fun))
773 Lisp_Object tmp = Fget (fun, Qinteractive_form);
774 if (!NILP (tmp))
775 return tmp;
776 else
777 fun = Fsymbol_function (fun);
780 if (SUBRP (fun))
782 char *spec = XSUBR (fun)->intspec;
783 if (spec)
784 return list2 (Qinteractive,
785 (*spec != '(') ? build_string (spec) :
786 Fcar (Fread_from_string (build_string (spec), Qnil, Qnil)));
788 else if (COMPILEDP (fun))
790 if ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE)
791 return list2 (Qinteractive, AREF (fun, COMPILED_INTERACTIVE));
793 else if (CONSP (fun))
795 Lisp_Object funcar = XCAR (fun);
796 if (EQ (funcar, Qlambda))
797 return Fassq (Qinteractive, Fcdr (XCDR (fun)));
798 else if (EQ (funcar, Qautoload))
800 struct gcpro gcpro1;
801 GCPRO1 (cmd);
802 do_autoload (fun, cmd);
803 UNGCPRO;
804 return Finteractive_form (cmd);
807 return Qnil;
811 /***********************************************************************
812 Getting and Setting Values of Symbols
813 ***********************************************************************/
815 /* Return the symbol holding SYMBOL's value. Signal
816 `cyclic-variable-indirection' if SYMBOL's chain of variable
817 indirections contains a loop. */
819 struct Lisp_Symbol *
820 indirect_variable (symbol)
821 struct Lisp_Symbol *symbol;
823 struct Lisp_Symbol *tortoise, *hare;
825 hare = tortoise = symbol;
827 while (hare->indirect_variable)
829 hare = XSYMBOL (hare->value);
830 if (!hare->indirect_variable)
831 break;
833 hare = XSYMBOL (hare->value);
834 tortoise = XSYMBOL (tortoise->value);
836 if (hare == tortoise)
838 Lisp_Object tem;
839 XSETSYMBOL (tem, symbol);
840 xsignal1 (Qcyclic_variable_indirection, tem);
844 return hare;
848 DEFUN ("indirect-variable", Findirect_variable, Sindirect_variable, 1, 1, 0,
849 doc: /* Return the variable at the end of OBJECT's variable chain.
850 If OBJECT is a symbol, follow all variable indirections and return the final
851 variable. If OBJECT is not a symbol, just return it.
852 Signal a cyclic-variable-indirection error if there is a loop in the
853 variable chain of symbols. */)
854 (object)
855 Lisp_Object object;
857 if (SYMBOLP (object))
858 XSETSYMBOL (object, indirect_variable (XSYMBOL (object)));
859 return object;
863 /* Given the raw contents of a symbol value cell,
864 return the Lisp value of the symbol.
865 This does not handle buffer-local variables; use
866 swap_in_symval_forwarding for that. */
868 Lisp_Object
869 do_symval_forwarding (valcontents)
870 register Lisp_Object valcontents;
872 register Lisp_Object val;
873 if (MISCP (valcontents))
874 switch (XMISCTYPE (valcontents))
876 case Lisp_Misc_Intfwd:
877 XSETINT (val, *XINTFWD (valcontents)->intvar);
878 return val;
880 case Lisp_Misc_Boolfwd:
881 return (*XBOOLFWD (valcontents)->boolvar ? Qt : Qnil);
883 case Lisp_Misc_Objfwd:
884 return *XOBJFWD (valcontents)->objvar;
886 case Lisp_Misc_Buffer_Objfwd:
887 return PER_BUFFER_VALUE (current_buffer,
888 XBUFFER_OBJFWD (valcontents)->offset);
890 case Lisp_Misc_Kboard_Objfwd:
891 /* We used to simply use current_kboard here, but from Lisp
892 code, it's value is often unexpected. It seems nicer to
893 allow constructions like this to work as intuitively expected:
895 (with-selected-frame frame
896 (define-key local-function-map "\eOP" [f1]))
898 On the other hand, this affects the semantics of
899 last-command and real-last-command, and people may rely on
900 that. I took a quick look at the Lisp codebase, and I
901 don't think anything will break. --lorentey */
902 return *(Lisp_Object *)(XKBOARD_OBJFWD (valcontents)->offset
903 + (char *)FRAME_KBOARD (SELECTED_FRAME ()));
905 return valcontents;
908 /* Store NEWVAL into SYMBOL, where VALCONTENTS is found in the value cell
909 of SYMBOL. If SYMBOL is buffer-local, VALCONTENTS should be the
910 buffer-independent contents of the value cell: forwarded just one
911 step past the buffer-localness.
913 BUF non-zero means set the value in buffer BUF instead of the
914 current buffer. This only plays a role for per-buffer variables. */
916 void
917 store_symval_forwarding (symbol, valcontents, newval, buf)
918 Lisp_Object symbol;
919 register Lisp_Object valcontents, newval;
920 struct buffer *buf;
922 switch (SWITCH_ENUM_CAST (XTYPE (valcontents)))
924 case Lisp_Misc:
925 switch (XMISCTYPE (valcontents))
927 case Lisp_Misc_Intfwd:
928 CHECK_NUMBER (newval);
929 *XINTFWD (valcontents)->intvar = XINT (newval);
930 /* This can never happen since intvar points to an EMACS_INT
931 which is at least large enough to hold a Lisp_Object.
932 if (*XINTFWD (valcontents)->intvar != XINT (newval))
933 error ("Value out of range for variable `%s'",
934 SDATA (SYMBOL_NAME (symbol))); */
935 break;
937 case Lisp_Misc_Boolfwd:
938 *XBOOLFWD (valcontents)->boolvar = !NILP (newval);
939 break;
941 case Lisp_Misc_Objfwd:
942 *XOBJFWD (valcontents)->objvar = newval;
944 /* If this variable is a default for something stored
945 in the buffer itself, such as default-fill-column,
946 find the buffers that don't have local values for it
947 and update them. */
948 if (XOBJFWD (valcontents)->objvar > (Lisp_Object *) &buffer_defaults
949 && XOBJFWD (valcontents)->objvar < (Lisp_Object *) (&buffer_defaults + 1))
951 int offset = ((char *) XOBJFWD (valcontents)->objvar
952 - (char *) &buffer_defaults);
953 int idx = PER_BUFFER_IDX (offset);
955 Lisp_Object tail;
957 if (idx <= 0)
958 break;
960 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
962 Lisp_Object buf;
963 struct buffer *b;
965 buf = Fcdr (XCAR (tail));
966 if (!BUFFERP (buf)) continue;
967 b = XBUFFER (buf);
969 if (! PER_BUFFER_VALUE_P (b, idx))
970 PER_BUFFER_VALUE (b, offset) = newval;
973 break;
975 case Lisp_Misc_Buffer_Objfwd:
977 int offset = XBUFFER_OBJFWD (valcontents)->offset;
978 Lisp_Object type = XBUFFER_OBJFWD (valcontents)->slottype;
980 if (!(NILP (type) || NILP (newval)
981 || (XINT (type) == LISP_INT_TAG
982 ? INTEGERP (newval)
983 : XTYPE (newval) == XINT (type))))
984 buffer_slot_type_mismatch (newval, XINT (type));
986 if (buf == NULL)
987 buf = current_buffer;
988 PER_BUFFER_VALUE (buf, offset) = newval;
990 break;
992 case Lisp_Misc_Kboard_Objfwd:
994 char *base = (char *) FRAME_KBOARD (SELECTED_FRAME ());
995 char *p = base + XKBOARD_OBJFWD (valcontents)->offset;
996 *(Lisp_Object *) p = newval;
998 break;
1000 default:
1001 goto def;
1003 break;
1005 default:
1006 def:
1007 valcontents = SYMBOL_VALUE (symbol);
1008 if (BUFFER_LOCAL_VALUEP (valcontents))
1009 XBUFFER_LOCAL_VALUE (valcontents)->realvalue = newval;
1010 else
1011 SET_SYMBOL_VALUE (symbol, newval);
1015 /* Set up SYMBOL to refer to its global binding.
1016 This makes it safe to alter the status of other bindings. */
1018 void
1019 swap_in_global_binding (symbol)
1020 Lisp_Object symbol;
1022 Lisp_Object valcontents = SYMBOL_VALUE (symbol);
1023 struct Lisp_Buffer_Local_Value *blv = XBUFFER_LOCAL_VALUE (valcontents);
1024 Lisp_Object cdr = blv->cdr;
1026 /* Unload the previously loaded binding. */
1027 Fsetcdr (XCAR (cdr),
1028 do_symval_forwarding (blv->realvalue));
1030 /* Select the global binding in the symbol. */
1031 XSETCAR (cdr, cdr);
1032 store_symval_forwarding (symbol, blv->realvalue, XCDR (cdr), NULL);
1034 /* Indicate that the global binding is set up now. */
1035 blv->frame = Qnil;
1036 blv->buffer = Qnil;
1037 blv->found_for_frame = 0;
1038 blv->found_for_buffer = 0;
1041 /* Set up the buffer-local symbol SYMBOL for validity in the current buffer.
1042 VALCONTENTS is the contents of its value cell,
1043 which points to a struct Lisp_Buffer_Local_Value.
1045 Return the value forwarded one step past the buffer-local stage.
1046 This could be another forwarding pointer. */
1048 static Lisp_Object
1049 swap_in_symval_forwarding (symbol, valcontents)
1050 Lisp_Object symbol, valcontents;
1052 register Lisp_Object tem1;
1054 tem1 = XBUFFER_LOCAL_VALUE (valcontents)->buffer;
1056 if (NILP (tem1)
1057 || current_buffer != XBUFFER (tem1)
1058 || (XBUFFER_LOCAL_VALUE (valcontents)->check_frame
1059 && ! EQ (selected_frame, XBUFFER_LOCAL_VALUE (valcontents)->frame)))
1061 struct Lisp_Symbol *sym = XSYMBOL (symbol);
1062 if (sym->indirect_variable)
1064 sym = indirect_variable (sym);
1065 XSETSYMBOL (symbol, sym);
1068 /* Unload the previously loaded binding. */
1069 tem1 = XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
1070 Fsetcdr (tem1,
1071 do_symval_forwarding (XBUFFER_LOCAL_VALUE (valcontents)->realvalue));
1072 /* Choose the new binding. */
1073 tem1 = assq_no_quit (symbol, current_buffer->local_var_alist);
1074 XBUFFER_LOCAL_VALUE (valcontents)->found_for_frame = 0;
1075 XBUFFER_LOCAL_VALUE (valcontents)->found_for_buffer = 0;
1076 if (NILP (tem1))
1078 if (XBUFFER_LOCAL_VALUE (valcontents)->check_frame)
1079 tem1 = assq_no_quit (symbol, XFRAME (selected_frame)->param_alist);
1080 if (! NILP (tem1))
1081 XBUFFER_LOCAL_VALUE (valcontents)->found_for_frame = 1;
1082 else
1083 tem1 = XBUFFER_LOCAL_VALUE (valcontents)->cdr;
1085 else
1086 XBUFFER_LOCAL_VALUE (valcontents)->found_for_buffer = 1;
1088 /* Load the new binding. */
1089 XSETCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr, tem1);
1090 XSETBUFFER (XBUFFER_LOCAL_VALUE (valcontents)->buffer, current_buffer);
1091 XBUFFER_LOCAL_VALUE (valcontents)->frame = selected_frame;
1092 store_symval_forwarding (symbol,
1093 XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1094 Fcdr (tem1), NULL);
1096 return XBUFFER_LOCAL_VALUE (valcontents)->realvalue;
1099 /* Find the value of a symbol, returning Qunbound if it's not bound.
1100 This is helpful for code which just wants to get a variable's value
1101 if it has one, without signaling an error.
1102 Note that it must not be possible to quit
1103 within this function. Great care is required for this. */
1105 Lisp_Object
1106 find_symbol_value (symbol)
1107 Lisp_Object symbol;
1109 register Lisp_Object valcontents;
1110 register Lisp_Object val;
1112 CHECK_SYMBOL (symbol);
1113 valcontents = SYMBOL_VALUE (symbol);
1115 if (BUFFER_LOCAL_VALUEP (valcontents))
1116 valcontents = swap_in_symval_forwarding (symbol, valcontents);
1118 return do_symval_forwarding (valcontents);
1121 DEFUN ("symbol-value", Fsymbol_value, Ssymbol_value, 1, 1, 0,
1122 doc: /* Return SYMBOL's value. Error if that is void. */)
1123 (symbol)
1124 Lisp_Object symbol;
1126 Lisp_Object val;
1128 val = find_symbol_value (symbol);
1129 if (!EQ (val, Qunbound))
1130 return val;
1132 xsignal1 (Qvoid_variable, symbol);
1135 DEFUN ("set", Fset, Sset, 2, 2, 0,
1136 doc: /* Set SYMBOL's value to NEWVAL, and return NEWVAL. */)
1137 (symbol, newval)
1138 register Lisp_Object symbol, newval;
1140 return set_internal (symbol, newval, current_buffer, 0);
1143 /* Return 1 if SYMBOL currently has a let-binding
1144 which was made in the buffer that is now current. */
1146 static int
1147 let_shadows_buffer_binding_p (symbol)
1148 struct Lisp_Symbol *symbol;
1150 volatile struct specbinding *p;
1152 for (p = specpdl_ptr - 1; p >= specpdl; p--)
1153 if (p->func == NULL
1154 && CONSP (p->symbol))
1156 struct Lisp_Symbol *let_bound_symbol = XSYMBOL (XCAR (p->symbol));
1157 if ((symbol == let_bound_symbol
1158 || (let_bound_symbol->indirect_variable
1159 && symbol == indirect_variable (let_bound_symbol)))
1160 && XBUFFER (XCDR (XCDR (p->symbol))) == current_buffer)
1161 break;
1164 return p >= specpdl;
1167 /* Store the value NEWVAL into SYMBOL.
1168 If buffer-locality is an issue, BUF specifies which buffer to use.
1169 (0 stands for the current buffer.)
1171 If BINDFLAG is zero, then if this symbol is supposed to become
1172 local in every buffer where it is set, then we make it local.
1173 If BINDFLAG is nonzero, we don't do that. */
1175 Lisp_Object
1176 set_internal (symbol, newval, buf, bindflag)
1177 register Lisp_Object symbol, newval;
1178 struct buffer *buf;
1179 int bindflag;
1181 int voide = EQ (newval, Qunbound);
1183 register Lisp_Object valcontents, innercontents, tem1, current_alist_element;
1185 if (buf == 0)
1186 buf = current_buffer;
1188 /* If restoring in a dead buffer, do nothing. */
1189 if (NILP (buf->name))
1190 return newval;
1192 CHECK_SYMBOL (symbol);
1193 if (SYMBOL_CONSTANT_P (symbol)
1194 && (NILP (Fkeywordp (symbol))
1195 || !EQ (newval, SYMBOL_VALUE (symbol))))
1196 xsignal1 (Qsetting_constant, symbol);
1198 innercontents = valcontents = SYMBOL_VALUE (symbol);
1200 if (BUFFER_OBJFWDP (valcontents))
1202 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1203 int idx = PER_BUFFER_IDX (offset);
1204 if (idx > 0
1205 && !bindflag
1206 && !let_shadows_buffer_binding_p (XSYMBOL (symbol)))
1207 SET_PER_BUFFER_VALUE_P (buf, idx, 1);
1209 else if (BUFFER_LOCAL_VALUEP (valcontents))
1211 /* valcontents is a struct Lisp_Buffer_Local_Value. */
1212 if (XSYMBOL (symbol)->indirect_variable)
1213 XSETSYMBOL (symbol, indirect_variable (XSYMBOL (symbol)));
1215 /* What binding is loaded right now? */
1216 current_alist_element
1217 = XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
1219 /* If the current buffer is not the buffer whose binding is
1220 loaded, or if there may be frame-local bindings and the frame
1221 isn't the right one, or if it's a Lisp_Buffer_Local_Value and
1222 the default binding is loaded, the loaded binding may be the
1223 wrong one. */
1224 if (!BUFFERP (XBUFFER_LOCAL_VALUE (valcontents)->buffer)
1225 || buf != XBUFFER (XBUFFER_LOCAL_VALUE (valcontents)->buffer)
1226 || (XBUFFER_LOCAL_VALUE (valcontents)->check_frame
1227 && !EQ (selected_frame, XBUFFER_LOCAL_VALUE (valcontents)->frame))
1228 /* Also unload a global binding (if the var is local_if_set). */
1229 || (EQ (XCAR (current_alist_element),
1230 current_alist_element)))
1232 /* The currently loaded binding is not necessarily valid.
1233 We need to unload it, and choose a new binding. */
1235 /* Write out `realvalue' to the old loaded binding. */
1236 Fsetcdr (current_alist_element,
1237 do_symval_forwarding (XBUFFER_LOCAL_VALUE (valcontents)->realvalue));
1239 /* Find the new binding. */
1240 tem1 = Fassq (symbol, buf->local_var_alist);
1241 XBUFFER_LOCAL_VALUE (valcontents)->found_for_buffer = 1;
1242 XBUFFER_LOCAL_VALUE (valcontents)->found_for_frame = 0;
1244 if (NILP (tem1))
1246 /* This buffer still sees the default value. */
1248 /* If the variable is not local_if_set,
1249 or if this is `let' rather than `set',
1250 make CURRENT-ALIST-ELEMENT point to itself,
1251 indicating that we're seeing the default value.
1252 Likewise if the variable has been let-bound
1253 in the current buffer. */
1254 if (bindflag || !XBUFFER_LOCAL_VALUE (valcontents)->local_if_set
1255 || let_shadows_buffer_binding_p (XSYMBOL (symbol)))
1257 XBUFFER_LOCAL_VALUE (valcontents)->found_for_buffer = 0;
1259 if (XBUFFER_LOCAL_VALUE (valcontents)->check_frame)
1260 tem1 = Fassq (symbol,
1261 XFRAME (selected_frame)->param_alist);
1263 if (! NILP (tem1))
1264 XBUFFER_LOCAL_VALUE (valcontents)->found_for_frame = 1;
1265 else
1266 tem1 = XBUFFER_LOCAL_VALUE (valcontents)->cdr;
1268 /* If it's a Lisp_Buffer_Local_Value, being set not bound,
1269 and we're not within a let that was made for this buffer,
1270 create a new buffer-local binding for the variable.
1271 That means, give this buffer a new assoc for a local value
1272 and load that binding. */
1273 else
1275 tem1 = Fcons (symbol, XCDR (current_alist_element));
1276 buf->local_var_alist
1277 = Fcons (tem1, buf->local_var_alist);
1281 /* Record which binding is now loaded. */
1282 XSETCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr, tem1);
1284 /* Set `buffer' and `frame' slots for the binding now loaded. */
1285 XSETBUFFER (XBUFFER_LOCAL_VALUE (valcontents)->buffer, buf);
1286 XBUFFER_LOCAL_VALUE (valcontents)->frame = selected_frame;
1288 innercontents = XBUFFER_LOCAL_VALUE (valcontents)->realvalue;
1290 /* Store the new value in the cons-cell. */
1291 XSETCDR (XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr), newval);
1294 /* If storing void (making the symbol void), forward only through
1295 buffer-local indicator, not through Lisp_Objfwd, etc. */
1296 if (voide)
1297 store_symval_forwarding (symbol, Qnil, newval, buf);
1298 else
1299 store_symval_forwarding (symbol, innercontents, newval, buf);
1301 return newval;
1304 /* Access or set a buffer-local symbol's default value. */
1306 /* Return the default value of SYMBOL, but don't check for voidness.
1307 Return Qunbound if it is void. */
1309 Lisp_Object
1310 default_value (symbol)
1311 Lisp_Object symbol;
1313 register Lisp_Object valcontents;
1315 CHECK_SYMBOL (symbol);
1316 valcontents = SYMBOL_VALUE (symbol);
1318 /* For a built-in buffer-local variable, get the default value
1319 rather than letting do_symval_forwarding get the current value. */
1320 if (BUFFER_OBJFWDP (valcontents))
1322 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1323 if (PER_BUFFER_IDX (offset) != 0)
1324 return PER_BUFFER_DEFAULT (offset);
1327 /* Handle user-created local variables. */
1328 if (BUFFER_LOCAL_VALUEP (valcontents))
1330 /* If var is set up for a buffer that lacks a local value for it,
1331 the current value is nominally the default value.
1332 But the `realvalue' slot may be more up to date, since
1333 ordinary setq stores just that slot. So use that. */
1334 Lisp_Object current_alist_element, alist_element_car;
1335 current_alist_element
1336 = XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
1337 alist_element_car = XCAR (current_alist_element);
1338 if (EQ (alist_element_car, current_alist_element))
1339 return do_symval_forwarding (XBUFFER_LOCAL_VALUE (valcontents)->realvalue);
1340 else
1341 return XCDR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
1343 /* For other variables, get the current value. */
1344 return do_symval_forwarding (valcontents);
1347 DEFUN ("default-boundp", Fdefault_boundp, Sdefault_boundp, 1, 1, 0,
1348 doc: /* Return t if SYMBOL has a non-void default value.
1349 This is the value that is seen in buffers that do not have their own values
1350 for this variable. */)
1351 (symbol)
1352 Lisp_Object symbol;
1354 register Lisp_Object value;
1356 value = default_value (symbol);
1357 return (EQ (value, Qunbound) ? Qnil : Qt);
1360 DEFUN ("default-value", Fdefault_value, Sdefault_value, 1, 1, 0,
1361 doc: /* Return SYMBOL's default value.
1362 This is the value that is seen in buffers that do not have their own values
1363 for this variable. The default value is meaningful for variables with
1364 local bindings in certain buffers. */)
1365 (symbol)
1366 Lisp_Object symbol;
1368 register Lisp_Object value;
1370 value = default_value (symbol);
1371 if (!EQ (value, Qunbound))
1372 return value;
1374 xsignal1 (Qvoid_variable, symbol);
1377 DEFUN ("set-default", Fset_default, Sset_default, 2, 2, 0,
1378 doc: /* Set SYMBOL's default value to VALUE. SYMBOL and VALUE are evaluated.
1379 The default value is seen in buffers that do not have their own values
1380 for this variable. */)
1381 (symbol, value)
1382 Lisp_Object symbol, value;
1384 register Lisp_Object valcontents, current_alist_element, alist_element_buffer;
1386 CHECK_SYMBOL (symbol);
1387 valcontents = SYMBOL_VALUE (symbol);
1389 /* Handle variables like case-fold-search that have special slots
1390 in the buffer. Make them work apparently like Lisp_Buffer_Local_Value
1391 variables. */
1392 if (BUFFER_OBJFWDP (valcontents))
1394 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1395 int idx = PER_BUFFER_IDX (offset);
1397 PER_BUFFER_DEFAULT (offset) = value;
1399 /* If this variable is not always local in all buffers,
1400 set it in the buffers that don't nominally have a local value. */
1401 if (idx > 0)
1403 struct buffer *b;
1405 for (b = all_buffers; b; b = b->next)
1406 if (!PER_BUFFER_VALUE_P (b, idx))
1407 PER_BUFFER_VALUE (b, offset) = value;
1409 return value;
1412 if (!BUFFER_LOCAL_VALUEP (valcontents))
1413 return Fset (symbol, value);
1415 /* Store new value into the DEFAULT-VALUE slot. */
1416 XSETCDR (XBUFFER_LOCAL_VALUE (valcontents)->cdr, value);
1418 /* If the default binding is now loaded, set the REALVALUE slot too. */
1419 current_alist_element
1420 = XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
1421 alist_element_buffer = Fcar (current_alist_element);
1422 if (EQ (alist_element_buffer, current_alist_element))
1423 store_symval_forwarding (symbol,
1424 XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1425 value, NULL);
1427 return value;
1430 DEFUN ("setq-default", Fsetq_default, Ssetq_default, 0, UNEVALLED, 0,
1431 doc: /* Set the default value of variable VAR to VALUE.
1432 VAR, the variable name, is literal (not evaluated);
1433 VALUE is an expression: it is evaluated and its value returned.
1434 The default value of a variable is seen in buffers
1435 that do not have their own values for the variable.
1437 More generally, you can use multiple variables and values, as in
1438 (setq-default VAR VALUE VAR VALUE...)
1439 This sets each VAR's default value to the corresponding VALUE.
1440 The VALUE for the Nth VAR can refer to the new default values
1441 of previous VARs.
1442 usage: (setq-default [VAR VALUE]...) */)
1443 (args)
1444 Lisp_Object args;
1446 register Lisp_Object args_left;
1447 register Lisp_Object val, symbol;
1448 struct gcpro gcpro1;
1450 if (NILP (args))
1451 return Qnil;
1453 args_left = args;
1454 GCPRO1 (args);
1458 val = Feval (Fcar (Fcdr (args_left)));
1459 symbol = XCAR (args_left);
1460 Fset_default (symbol, val);
1461 args_left = Fcdr (XCDR (args_left));
1463 while (!NILP (args_left));
1465 UNGCPRO;
1466 return val;
1469 /* Lisp functions for creating and removing buffer-local variables. */
1471 DEFUN ("make-variable-buffer-local", Fmake_variable_buffer_local, Smake_variable_buffer_local,
1472 1, 1, "vMake Variable Buffer Local: ",
1473 doc: /* Make VARIABLE become buffer-local whenever it is set.
1474 At any time, the value for the current buffer is in effect,
1475 unless the variable has never been set in this buffer,
1476 in which case the default value is in effect.
1477 Note that binding the variable with `let', or setting it while
1478 a `let'-style binding made in this buffer is in effect,
1479 does not make the variable buffer-local. Return VARIABLE.
1481 In most cases it is better to use `make-local-variable',
1482 which makes a variable local in just one buffer.
1484 The function `default-value' gets the default value and `set-default' sets it. */)
1485 (variable)
1486 register Lisp_Object variable;
1488 register Lisp_Object tem, valcontents, newval;
1489 struct Lisp_Symbol *sym;
1491 CHECK_SYMBOL (variable);
1492 sym = indirect_variable (XSYMBOL (variable));
1494 valcontents = sym->value;
1495 if (sym->constant || KBOARD_OBJFWDP (valcontents))
1496 error ("Symbol %s may not be buffer-local", SDATA (sym->xname));
1498 if (BUFFER_OBJFWDP (valcontents))
1499 return variable;
1500 else if (BUFFER_LOCAL_VALUEP (valcontents))
1502 if (XBUFFER_LOCAL_VALUE (valcontents)->check_frame)
1503 error ("Symbol %s may not be buffer-local", SDATA (sym->xname));
1504 newval = valcontents;
1506 else
1508 if (EQ (valcontents, Qunbound))
1509 sym->value = Qnil;
1510 tem = Fcons (Qnil, Fsymbol_value (variable));
1511 XSETCAR (tem, tem);
1512 newval = allocate_misc ();
1513 XMISCTYPE (newval) = Lisp_Misc_Buffer_Local_Value;
1514 XBUFFER_LOCAL_VALUE (newval)->realvalue = sym->value;
1515 XBUFFER_LOCAL_VALUE (newval)->buffer = Fcurrent_buffer ();
1516 XBUFFER_LOCAL_VALUE (newval)->frame = Qnil;
1517 XBUFFER_LOCAL_VALUE (newval)->found_for_buffer = 0;
1518 XBUFFER_LOCAL_VALUE (newval)->found_for_frame = 0;
1519 XBUFFER_LOCAL_VALUE (newval)->check_frame = 0;
1520 XBUFFER_LOCAL_VALUE (newval)->cdr = tem;
1521 sym->value = newval;
1523 XBUFFER_LOCAL_VALUE (newval)->local_if_set = 1;
1524 return variable;
1527 DEFUN ("make-local-variable", Fmake_local_variable, Smake_local_variable,
1528 1, 1, "vMake Local Variable: ",
1529 doc: /* Make VARIABLE have a separate value in the current buffer.
1530 Other buffers will continue to share a common default value.
1531 \(The buffer-local value of VARIABLE starts out as the same value
1532 VARIABLE previously had. If VARIABLE was void, it remains void.\)
1533 Return VARIABLE.
1535 If the variable is already arranged to become local when set,
1536 this function causes a local value to exist for this buffer,
1537 just as setting the variable would do.
1539 This function returns VARIABLE, and therefore
1540 (set (make-local-variable 'VARIABLE) VALUE-EXP)
1541 works.
1543 See also `make-variable-buffer-local'.
1545 Do not use `make-local-variable' to make a hook variable buffer-local.
1546 Instead, use `add-hook' and specify t for the LOCAL argument. */)
1547 (variable)
1548 register Lisp_Object variable;
1550 register Lisp_Object tem, valcontents;
1551 struct Lisp_Symbol *sym;
1553 CHECK_SYMBOL (variable);
1554 sym = indirect_variable (XSYMBOL (variable));
1556 valcontents = sym->value;
1557 if (sym->constant || KBOARD_OBJFWDP (valcontents)
1558 || (BUFFER_LOCAL_VALUEP (valcontents)
1559 && (XBUFFER_LOCAL_VALUE (valcontents)->check_frame)))
1560 error ("Symbol %s may not be buffer-local", SDATA (sym->xname));
1562 if ((BUFFER_LOCAL_VALUEP (valcontents)
1563 && XBUFFER_LOCAL_VALUE (valcontents)->local_if_set)
1564 || BUFFER_OBJFWDP (valcontents))
1566 tem = Fboundp (variable);
1568 /* Make sure the symbol has a local value in this particular buffer,
1569 by setting it to the same value it already has. */
1570 Fset (variable, (EQ (tem, Qt) ? Fsymbol_value (variable) : Qunbound));
1571 return variable;
1573 /* Make sure symbol is set up to hold per-buffer values. */
1574 if (!BUFFER_LOCAL_VALUEP (valcontents))
1576 Lisp_Object newval;
1577 tem = Fcons (Qnil, do_symval_forwarding (valcontents));
1578 XSETCAR (tem, tem);
1579 newval = allocate_misc ();
1580 XMISCTYPE (newval) = Lisp_Misc_Buffer_Local_Value;
1581 XBUFFER_LOCAL_VALUE (newval)->realvalue = sym->value;
1582 XBUFFER_LOCAL_VALUE (newval)->buffer = Qnil;
1583 XBUFFER_LOCAL_VALUE (newval)->frame = Qnil;
1584 XBUFFER_LOCAL_VALUE (newval)->local_if_set = 0;
1585 XBUFFER_LOCAL_VALUE (newval)->found_for_buffer = 0;
1586 XBUFFER_LOCAL_VALUE (newval)->found_for_frame = 0;
1587 XBUFFER_LOCAL_VALUE (newval)->check_frame = 0;
1588 XBUFFER_LOCAL_VALUE (newval)->cdr = tem;
1589 sym->value = newval;
1591 /* Make sure this buffer has its own value of symbol. */
1592 XSETSYMBOL (variable, sym); /* Propagate variable indirections. */
1593 tem = Fassq (variable, current_buffer->local_var_alist);
1594 if (NILP (tem))
1596 /* Swap out any local binding for some other buffer, and make
1597 sure the current value is permanently recorded, if it's the
1598 default value. */
1599 find_symbol_value (variable);
1601 current_buffer->local_var_alist
1602 = Fcons (Fcons (variable, XCDR (XBUFFER_LOCAL_VALUE (sym->value)->cdr)),
1603 current_buffer->local_var_alist);
1605 /* Make sure symbol does not think it is set up for this buffer;
1606 force it to look once again for this buffer's value. */
1608 Lisp_Object *pvalbuf;
1610 valcontents = sym->value;
1612 pvalbuf = &XBUFFER_LOCAL_VALUE (valcontents)->buffer;
1613 if (current_buffer == XBUFFER (*pvalbuf))
1614 *pvalbuf = Qnil;
1615 XBUFFER_LOCAL_VALUE (valcontents)->found_for_buffer = 0;
1619 /* If the symbol forwards into a C variable, then load the binding
1620 for this buffer now. If C code modifies the variable before we
1621 load the binding in, then that new value will clobber the default
1622 binding the next time we unload it. */
1623 valcontents = XBUFFER_LOCAL_VALUE (sym->value)->realvalue;
1624 if (INTFWDP (valcontents) || BOOLFWDP (valcontents) || OBJFWDP (valcontents))
1625 swap_in_symval_forwarding (variable, sym->value);
1627 return variable;
1630 DEFUN ("kill-local-variable", Fkill_local_variable, Skill_local_variable,
1631 1, 1, "vKill Local Variable: ",
1632 doc: /* Make VARIABLE no longer have a separate value in the current buffer.
1633 From now on the default value will apply in this buffer. Return VARIABLE. */)
1634 (variable)
1635 register Lisp_Object variable;
1637 register Lisp_Object tem, valcontents;
1638 struct Lisp_Symbol *sym;
1640 CHECK_SYMBOL (variable);
1641 sym = indirect_variable (XSYMBOL (variable));
1643 valcontents = sym->value;
1645 if (BUFFER_OBJFWDP (valcontents))
1647 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1648 int idx = PER_BUFFER_IDX (offset);
1650 if (idx > 0)
1652 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 0);
1653 PER_BUFFER_VALUE (current_buffer, offset)
1654 = PER_BUFFER_DEFAULT (offset);
1656 return variable;
1659 if (!BUFFER_LOCAL_VALUEP (valcontents))
1660 return variable;
1662 /* Get rid of this buffer's alist element, if any. */
1663 XSETSYMBOL (variable, sym); /* Propagate variable indirection. */
1664 tem = Fassq (variable, current_buffer->local_var_alist);
1665 if (!NILP (tem))
1666 current_buffer->local_var_alist
1667 = Fdelq (tem, current_buffer->local_var_alist);
1669 /* If the symbol is set up with the current buffer's binding
1670 loaded, recompute its value. We have to do it now, or else
1671 forwarded objects won't work right. */
1673 Lisp_Object *pvalbuf, buf;
1674 valcontents = sym->value;
1675 pvalbuf = &XBUFFER_LOCAL_VALUE (valcontents)->buffer;
1676 XSETBUFFER (buf, current_buffer);
1677 if (EQ (buf, *pvalbuf))
1679 *pvalbuf = Qnil;
1680 XBUFFER_LOCAL_VALUE (valcontents)->found_for_buffer = 0;
1681 find_symbol_value (variable);
1685 return variable;
1688 /* Lisp functions for creating and removing buffer-local variables. */
1690 /* Obsolete since 22.2. NB adjust doc of modify-frame-parameters
1691 when/if this is removed. */
1693 DEFUN ("make-variable-frame-local", Fmake_variable_frame_local, Smake_variable_frame_local,
1694 1, 1, "vMake Variable Frame Local: ",
1695 doc: /* Enable VARIABLE to have frame-local bindings.
1696 This does not create any frame-local bindings for VARIABLE,
1697 it just makes them possible.
1699 A frame-local binding is actually a frame parameter value.
1700 If a frame F has a value for the frame parameter named VARIABLE,
1701 that also acts as a frame-local binding for VARIABLE in F--
1702 provided this function has been called to enable VARIABLE
1703 to have frame-local bindings at all.
1705 The only way to create a frame-local binding for VARIABLE in a frame
1706 is to set the VARIABLE frame parameter of that frame. See
1707 `modify-frame-parameters' for how to set frame parameters.
1709 Note that since Emacs 23.1, variables cannot be both buffer-local and
1710 frame-local any more (buffer-local bindings used to take precedence over
1711 frame-local bindings). */)
1712 (variable)
1713 register Lisp_Object variable;
1715 register Lisp_Object tem, valcontents, newval;
1716 struct Lisp_Symbol *sym;
1718 CHECK_SYMBOL (variable);
1719 sym = indirect_variable (XSYMBOL (variable));
1721 valcontents = sym->value;
1722 if (sym->constant || KBOARD_OBJFWDP (valcontents)
1723 || BUFFER_OBJFWDP (valcontents))
1724 error ("Symbol %s may not be frame-local", SDATA (sym->xname));
1726 if (BUFFER_LOCAL_VALUEP (valcontents))
1728 if (!XBUFFER_LOCAL_VALUE (valcontents)->check_frame)
1729 error ("Symbol %s may not be frame-local", SDATA (sym->xname));
1730 return variable;
1733 if (EQ (valcontents, Qunbound))
1734 sym->value = Qnil;
1735 tem = Fcons (Qnil, Fsymbol_value (variable));
1736 XSETCAR (tem, tem);
1737 newval = allocate_misc ();
1738 XMISCTYPE (newval) = Lisp_Misc_Buffer_Local_Value;
1739 XBUFFER_LOCAL_VALUE (newval)->realvalue = sym->value;
1740 XBUFFER_LOCAL_VALUE (newval)->buffer = Qnil;
1741 XBUFFER_LOCAL_VALUE (newval)->frame = Qnil;
1742 XBUFFER_LOCAL_VALUE (newval)->local_if_set = 0;
1743 XBUFFER_LOCAL_VALUE (newval)->found_for_buffer = 0;
1744 XBUFFER_LOCAL_VALUE (newval)->found_for_frame = 0;
1745 XBUFFER_LOCAL_VALUE (newval)->check_frame = 1;
1746 XBUFFER_LOCAL_VALUE (newval)->cdr = tem;
1747 sym->value = newval;
1748 return variable;
1751 DEFUN ("local-variable-p", Flocal_variable_p, Slocal_variable_p,
1752 1, 2, 0,
1753 doc: /* Non-nil if VARIABLE has a local binding in buffer BUFFER.
1754 BUFFER defaults to the current buffer. */)
1755 (variable, buffer)
1756 register Lisp_Object variable, buffer;
1758 Lisp_Object valcontents;
1759 register struct buffer *buf;
1760 struct Lisp_Symbol *sym;
1762 if (NILP (buffer))
1763 buf = current_buffer;
1764 else
1766 CHECK_BUFFER (buffer);
1767 buf = XBUFFER (buffer);
1770 CHECK_SYMBOL (variable);
1771 sym = indirect_variable (XSYMBOL (variable));
1772 XSETSYMBOL (variable, sym);
1774 valcontents = sym->value;
1775 if (BUFFER_LOCAL_VALUEP (valcontents))
1777 Lisp_Object tail, elt;
1779 for (tail = buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
1781 elt = XCAR (tail);
1782 if (EQ (variable, XCAR (elt)))
1783 return Qt;
1786 if (BUFFER_OBJFWDP (valcontents))
1788 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1789 int idx = PER_BUFFER_IDX (offset);
1790 if (idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
1791 return Qt;
1793 return Qnil;
1796 DEFUN ("local-variable-if-set-p", Flocal_variable_if_set_p, Slocal_variable_if_set_p,
1797 1, 2, 0,
1798 doc: /* Non-nil if VARIABLE will be local in buffer BUFFER when set there.
1799 More precisely, this means that setting the variable \(with `set' or`setq'),
1800 while it does not have a `let'-style binding that was made in BUFFER,
1801 will produce a buffer local binding. See Info node
1802 `(elisp)Creating Buffer-Local'.
1803 BUFFER defaults to the current buffer. */)
1804 (variable, buffer)
1805 register Lisp_Object variable, buffer;
1807 Lisp_Object valcontents;
1808 register struct buffer *buf;
1809 struct Lisp_Symbol *sym;
1811 if (NILP (buffer))
1812 buf = current_buffer;
1813 else
1815 CHECK_BUFFER (buffer);
1816 buf = XBUFFER (buffer);
1819 CHECK_SYMBOL (variable);
1820 sym = indirect_variable (XSYMBOL (variable));
1821 XSETSYMBOL (variable, sym);
1823 valcontents = sym->value;
1825 if (BUFFER_OBJFWDP (valcontents))
1826 /* All these slots become local if they are set. */
1827 return Qt;
1828 else if (BUFFER_LOCAL_VALUEP (valcontents))
1830 Lisp_Object tail, elt;
1831 if (XBUFFER_LOCAL_VALUE (valcontents)->local_if_set)
1832 return Qt;
1833 for (tail = buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
1835 elt = XCAR (tail);
1836 if (EQ (variable, XCAR (elt)))
1837 return Qt;
1840 return Qnil;
1843 DEFUN ("variable-binding-locus", Fvariable_binding_locus, Svariable_binding_locus,
1844 1, 1, 0,
1845 doc: /* Return a value indicating where VARIABLE's current binding comes from.
1846 If the current binding is buffer-local, the value is the current buffer.
1847 If the current binding is frame-local, the value is the selected frame.
1848 If the current binding is global (the default), the value is nil. */)
1849 (variable)
1850 register Lisp_Object variable;
1852 Lisp_Object valcontents;
1853 struct Lisp_Symbol *sym;
1855 CHECK_SYMBOL (variable);
1856 sym = indirect_variable (XSYMBOL (variable));
1858 /* Make sure the current binding is actually swapped in. */
1859 find_symbol_value (variable);
1861 valcontents = sym->value;
1863 if (BUFFER_LOCAL_VALUEP (valcontents)
1864 || BUFFER_OBJFWDP (valcontents))
1866 /* For a local variable, record both the symbol and which
1867 buffer's or frame's value we are saving. */
1868 if (!NILP (Flocal_variable_p (variable, Qnil)))
1869 return Fcurrent_buffer ();
1870 else if (BUFFER_LOCAL_VALUEP (valcontents)
1871 && XBUFFER_LOCAL_VALUE (valcontents)->found_for_frame)
1872 return XBUFFER_LOCAL_VALUE (valcontents)->frame;
1875 return Qnil;
1878 /* This code is disabled now that we use the selected frame to return
1879 keyboard-local-values. */
1880 #if 0
1881 extern struct terminal *get_terminal P_ ((Lisp_Object display, int));
1883 DEFUN ("terminal-local-value", Fterminal_local_value, Sterminal_local_value, 2, 2, 0,
1884 doc: /* Return the terminal-local value of SYMBOL on TERMINAL.
1885 If SYMBOL is not a terminal-local variable, then return its normal
1886 value, like `symbol-value'.
1888 TERMINAL may be a terminal object, a frame, or nil (meaning the
1889 selected frame's terminal device). */)
1890 (symbol, terminal)
1891 Lisp_Object symbol;
1892 Lisp_Object terminal;
1894 Lisp_Object result;
1895 struct terminal *t = get_terminal (terminal, 1);
1896 push_kboard (t->kboard);
1897 result = Fsymbol_value (symbol);
1898 pop_kboard ();
1899 return result;
1902 DEFUN ("set-terminal-local-value", Fset_terminal_local_value, Sset_terminal_local_value, 3, 3, 0,
1903 doc: /* Set the terminal-local binding of SYMBOL on TERMINAL to VALUE.
1904 If VARIABLE is not a terminal-local variable, then set its normal
1905 binding, like `set'.
1907 TERMINAL may be a terminal object, a frame, or nil (meaning the
1908 selected frame's terminal device). */)
1909 (symbol, terminal, value)
1910 Lisp_Object symbol;
1911 Lisp_Object terminal;
1912 Lisp_Object value;
1914 Lisp_Object result;
1915 struct terminal *t = get_terminal (terminal, 1);
1916 push_kboard (d->kboard);
1917 result = Fset (symbol, value);
1918 pop_kboard ();
1919 return result;
1921 #endif
1923 /* Find the function at the end of a chain of symbol function indirections. */
1925 /* If OBJECT is a symbol, find the end of its function chain and
1926 return the value found there. If OBJECT is not a symbol, just
1927 return it. If there is a cycle in the function chain, signal a
1928 cyclic-function-indirection error.
1930 This is like Findirect_function, except that it doesn't signal an
1931 error if the chain ends up unbound. */
1932 Lisp_Object
1933 indirect_function (object)
1934 register Lisp_Object object;
1936 Lisp_Object tortoise, hare;
1938 hare = tortoise = object;
1940 for (;;)
1942 if (!SYMBOLP (hare) || EQ (hare, Qunbound))
1943 break;
1944 hare = XSYMBOL (hare)->function;
1945 if (!SYMBOLP (hare) || EQ (hare, Qunbound))
1946 break;
1947 hare = XSYMBOL (hare)->function;
1949 tortoise = XSYMBOL (tortoise)->function;
1951 if (EQ (hare, tortoise))
1952 xsignal1 (Qcyclic_function_indirection, object);
1955 return hare;
1958 DEFUN ("indirect-function", Findirect_function, Sindirect_function, 1, 2, 0,
1959 doc: /* Return the function at the end of OBJECT's function chain.
1960 If OBJECT is not a symbol, just return it. Otherwise, follow all
1961 function indirections to find the final function binding and return it.
1962 If the final symbol in the chain is unbound, signal a void-function error.
1963 Optional arg NOERROR non-nil means to return nil instead of signalling.
1964 Signal a cyclic-function-indirection error if there is a loop in the
1965 function chain of symbols. */)
1966 (object, noerror)
1967 register Lisp_Object object;
1968 Lisp_Object noerror;
1970 Lisp_Object result;
1972 /* Optimize for no indirection. */
1973 result = object;
1974 if (SYMBOLP (result) && !EQ (result, Qunbound)
1975 && (result = XSYMBOL (result)->function, SYMBOLP (result)))
1976 result = indirect_function (result);
1977 if (!EQ (result, Qunbound))
1978 return result;
1980 if (NILP (noerror))
1981 xsignal1 (Qvoid_function, object);
1983 return Qnil;
1986 /* Extract and set vector and string elements */
1988 DEFUN ("aref", Faref, Saref, 2, 2, 0,
1989 doc: /* Return the element of ARRAY at index IDX.
1990 ARRAY may be a vector, a string, a char-table, a bool-vector,
1991 or a byte-code object. IDX starts at 0. */)
1992 (array, idx)
1993 register Lisp_Object array;
1994 Lisp_Object idx;
1996 register int idxval;
1998 CHECK_NUMBER (idx);
1999 idxval = XINT (idx);
2000 if (STRINGP (array))
2002 int c, idxval_byte;
2004 if (idxval < 0 || idxval >= SCHARS (array))
2005 args_out_of_range (array, idx);
2006 if (! STRING_MULTIBYTE (array))
2007 return make_number ((unsigned char) SREF (array, idxval));
2008 idxval_byte = string_char_to_byte (array, idxval);
2010 c = STRING_CHAR (SDATA (array) + idxval_byte);
2011 return make_number (c);
2013 else if (BOOL_VECTOR_P (array))
2015 int val;
2017 if (idxval < 0 || idxval >= XBOOL_VECTOR (array)->size)
2018 args_out_of_range (array, idx);
2020 val = (unsigned char) XBOOL_VECTOR (array)->data[idxval / BOOL_VECTOR_BITS_PER_CHAR];
2021 return (val & (1 << (idxval % BOOL_VECTOR_BITS_PER_CHAR)) ? Qt : Qnil);
2023 else if (CHAR_TABLE_P (array))
2025 CHECK_CHARACTER (idx);
2026 return CHAR_TABLE_REF (array, idxval);
2028 else
2030 int size = 0;
2031 if (VECTORP (array))
2032 size = XVECTOR (array)->size;
2033 else if (COMPILEDP (array))
2034 size = XVECTOR (array)->size & PSEUDOVECTOR_SIZE_MASK;
2035 else
2036 wrong_type_argument (Qarrayp, array);
2038 if (idxval < 0 || idxval >= size)
2039 args_out_of_range (array, idx);
2040 return XVECTOR (array)->contents[idxval];
2044 DEFUN ("aset", Faset, Saset, 3, 3, 0,
2045 doc: /* Store into the element of ARRAY at index IDX the value NEWELT.
2046 Return NEWELT. ARRAY may be a vector, a string, a char-table or a
2047 bool-vector. IDX starts at 0. */)
2048 (array, idx, newelt)
2049 register Lisp_Object array;
2050 Lisp_Object idx, newelt;
2052 register int idxval;
2054 CHECK_NUMBER (idx);
2055 idxval = XINT (idx);
2056 CHECK_ARRAY (array, Qarrayp);
2057 CHECK_IMPURE (array);
2059 if (VECTORP (array))
2061 if (idxval < 0 || idxval >= XVECTOR (array)->size)
2062 args_out_of_range (array, idx);
2063 XVECTOR (array)->contents[idxval] = newelt;
2065 else if (BOOL_VECTOR_P (array))
2067 int val;
2069 if (idxval < 0 || idxval >= XBOOL_VECTOR (array)->size)
2070 args_out_of_range (array, idx);
2072 val = (unsigned char) XBOOL_VECTOR (array)->data[idxval / BOOL_VECTOR_BITS_PER_CHAR];
2074 if (! NILP (newelt))
2075 val |= 1 << (idxval % BOOL_VECTOR_BITS_PER_CHAR);
2076 else
2077 val &= ~(1 << (idxval % BOOL_VECTOR_BITS_PER_CHAR));
2078 XBOOL_VECTOR (array)->data[idxval / BOOL_VECTOR_BITS_PER_CHAR] = val;
2080 else if (CHAR_TABLE_P (array))
2082 CHECK_CHARACTER (idx);
2083 CHAR_TABLE_SET (array, idxval, newelt);
2085 else if (STRING_MULTIBYTE (array))
2087 int idxval_byte, prev_bytes, new_bytes, nbytes;
2088 unsigned char workbuf[MAX_MULTIBYTE_LENGTH], *p0 = workbuf, *p1;
2090 if (idxval < 0 || idxval >= SCHARS (array))
2091 args_out_of_range (array, idx);
2092 CHECK_CHARACTER (newelt);
2094 nbytes = SBYTES (array);
2096 idxval_byte = string_char_to_byte (array, idxval);
2097 p1 = SDATA (array) + idxval_byte;
2098 PARSE_MULTIBYTE_SEQ (p1, nbytes - idxval_byte, prev_bytes);
2099 new_bytes = CHAR_STRING (XINT (newelt), p0);
2100 if (prev_bytes != new_bytes)
2102 /* We must relocate the string data. */
2103 int nchars = SCHARS (array);
2104 unsigned char *str;
2105 USE_SAFE_ALLOCA;
2107 SAFE_ALLOCA (str, unsigned char *, nbytes);
2108 bcopy (SDATA (array), str, nbytes);
2109 allocate_string_data (XSTRING (array), nchars,
2110 nbytes + new_bytes - prev_bytes);
2111 bcopy (str, SDATA (array), idxval_byte);
2112 p1 = SDATA (array) + idxval_byte;
2113 bcopy (str + idxval_byte + prev_bytes, p1 + new_bytes,
2114 nbytes - (idxval_byte + prev_bytes));
2115 SAFE_FREE ();
2116 clear_string_char_byte_cache ();
2118 while (new_bytes--)
2119 *p1++ = *p0++;
2121 else
2123 if (idxval < 0 || idxval >= SCHARS (array))
2124 args_out_of_range (array, idx);
2125 CHECK_NUMBER (newelt);
2127 if (XINT (newelt) >= 0 && ! SINGLE_BYTE_CHAR_P (XINT (newelt)))
2129 int i;
2131 for (i = SBYTES (array) - 1; i >= 0; i--)
2132 if (SREF (array, i) >= 0x80)
2133 args_out_of_range (array, newelt);
2134 /* ARRAY is an ASCII string. Convert it to a multibyte
2135 string, and try `aset' again. */
2136 STRING_SET_MULTIBYTE (array);
2137 return Faset (array, idx, newelt);
2139 SSET (array, idxval, XINT (newelt));
2142 return newelt;
2145 /* Arithmetic functions */
2147 enum comparison { equal, notequal, less, grtr, less_or_equal, grtr_or_equal };
2149 Lisp_Object
2150 arithcompare (num1, num2, comparison)
2151 Lisp_Object num1, num2;
2152 enum comparison comparison;
2154 double f1 = 0, f2 = 0;
2155 int floatp = 0;
2157 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num1);
2158 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num2);
2160 if (FLOATP (num1) || FLOATP (num2))
2162 floatp = 1;
2163 f1 = (FLOATP (num1)) ? XFLOAT_DATA (num1) : XINT (num1);
2164 f2 = (FLOATP (num2)) ? XFLOAT_DATA (num2) : XINT (num2);
2167 switch (comparison)
2169 case equal:
2170 if (floatp ? f1 == f2 : XINT (num1) == XINT (num2))
2171 return Qt;
2172 return Qnil;
2174 case notequal:
2175 if (floatp ? f1 != f2 : XINT (num1) != XINT (num2))
2176 return Qt;
2177 return Qnil;
2179 case less:
2180 if (floatp ? f1 < f2 : XINT (num1) < XINT (num2))
2181 return Qt;
2182 return Qnil;
2184 case less_or_equal:
2185 if (floatp ? f1 <= f2 : XINT (num1) <= XINT (num2))
2186 return Qt;
2187 return Qnil;
2189 case grtr:
2190 if (floatp ? f1 > f2 : XINT (num1) > XINT (num2))
2191 return Qt;
2192 return Qnil;
2194 case grtr_or_equal:
2195 if (floatp ? f1 >= f2 : XINT (num1) >= XINT (num2))
2196 return Qt;
2197 return Qnil;
2199 default:
2200 abort ();
2204 DEFUN ("=", Feqlsign, Seqlsign, 2, 2, 0,
2205 doc: /* Return t if two args, both numbers or markers, are equal. */)
2206 (num1, num2)
2207 register Lisp_Object num1, num2;
2209 return arithcompare (num1, num2, equal);
2212 DEFUN ("<", Flss, Slss, 2, 2, 0,
2213 doc: /* Return t if first arg is less than second arg. Both must be numbers or markers. */)
2214 (num1, num2)
2215 register Lisp_Object num1, num2;
2217 return arithcompare (num1, num2, less);
2220 DEFUN (">", Fgtr, Sgtr, 2, 2, 0,
2221 doc: /* Return t if first arg is greater than second arg. Both must be numbers or markers. */)
2222 (num1, num2)
2223 register Lisp_Object num1, num2;
2225 return arithcompare (num1, num2, grtr);
2228 DEFUN ("<=", Fleq, Sleq, 2, 2, 0,
2229 doc: /* Return t if first arg is less than or equal to second arg.
2230 Both must be numbers or markers. */)
2231 (num1, num2)
2232 register Lisp_Object num1, num2;
2234 return arithcompare (num1, num2, less_or_equal);
2237 DEFUN (">=", Fgeq, Sgeq, 2, 2, 0,
2238 doc: /* Return t if first arg is greater than or equal to second arg.
2239 Both must be numbers or markers. */)
2240 (num1, num2)
2241 register Lisp_Object num1, num2;
2243 return arithcompare (num1, num2, grtr_or_equal);
2246 DEFUN ("/=", Fneq, Sneq, 2, 2, 0,
2247 doc: /* Return t if first arg is not equal to second arg. Both must be numbers or markers. */)
2248 (num1, num2)
2249 register Lisp_Object num1, num2;
2251 return arithcompare (num1, num2, notequal);
2254 DEFUN ("zerop", Fzerop, Szerop, 1, 1, 0,
2255 doc: /* Return t if NUMBER is zero. */)
2256 (number)
2257 register Lisp_Object number;
2259 CHECK_NUMBER_OR_FLOAT (number);
2261 if (FLOATP (number))
2263 if (XFLOAT_DATA (number) == 0.0)
2264 return Qt;
2265 return Qnil;
2268 if (!XINT (number))
2269 return Qt;
2270 return Qnil;
2273 /* Convert between long values and pairs of Lisp integers.
2274 Note that long_to_cons returns a single Lisp integer
2275 when the value fits in one. */
2277 Lisp_Object
2278 long_to_cons (i)
2279 unsigned long i;
2281 unsigned long top = i >> 16;
2282 unsigned int bot = i & 0xFFFF;
2283 if (top == 0)
2284 return make_number (bot);
2285 if (top == (unsigned long)-1 >> 16)
2286 return Fcons (make_number (-1), make_number (bot));
2287 return Fcons (make_number (top), make_number (bot));
2290 unsigned long
2291 cons_to_long (c)
2292 Lisp_Object c;
2294 Lisp_Object top, bot;
2295 if (INTEGERP (c))
2296 return XINT (c);
2297 top = XCAR (c);
2298 bot = XCDR (c);
2299 if (CONSP (bot))
2300 bot = XCAR (bot);
2301 return ((XINT (top) << 16) | XINT (bot));
2304 DEFUN ("number-to-string", Fnumber_to_string, Snumber_to_string, 1, 1, 0,
2305 doc: /* Return the decimal representation of NUMBER as a string.
2306 Uses a minus sign if negative.
2307 NUMBER may be an integer or a floating point number. */)
2308 (number)
2309 Lisp_Object number;
2311 char buffer[VALBITS];
2313 CHECK_NUMBER_OR_FLOAT (number);
2315 if (FLOATP (number))
2317 char pigbuf[350]; /* see comments in float_to_string */
2319 float_to_string (pigbuf, XFLOAT_DATA (number));
2320 return build_string (pigbuf);
2323 if (sizeof (int) == sizeof (EMACS_INT))
2324 sprintf (buffer, "%d", (int) XINT (number));
2325 else if (sizeof (long) == sizeof (EMACS_INT))
2326 sprintf (buffer, "%ld", (long) XINT (number));
2327 else
2328 abort ();
2329 return build_string (buffer);
2332 INLINE static int
2333 digit_to_number (character, base)
2334 int character, base;
2336 int digit;
2338 if (character >= '0' && character <= '9')
2339 digit = character - '0';
2340 else if (character >= 'a' && character <= 'z')
2341 digit = character - 'a' + 10;
2342 else if (character >= 'A' && character <= 'Z')
2343 digit = character - 'A' + 10;
2344 else
2345 return -1;
2347 if (digit >= base)
2348 return -1;
2349 else
2350 return digit;
2353 DEFUN ("string-to-number", Fstring_to_number, Sstring_to_number, 1, 2, 0,
2354 doc: /* Parse STRING as a decimal number and return the number.
2355 This parses both integers and floating point numbers.
2356 It ignores leading spaces and tabs, and all trailing chars.
2358 If BASE, interpret STRING as a number in that base. If BASE isn't
2359 present, base 10 is used. BASE must be between 2 and 16 (inclusive).
2360 If the base used is not 10, STRING is always parsed as integer. */)
2361 (string, base)
2362 register Lisp_Object string, base;
2364 register unsigned char *p;
2365 register int b;
2366 int sign = 1;
2367 Lisp_Object val;
2369 CHECK_STRING (string);
2371 if (NILP (base))
2372 b = 10;
2373 else
2375 CHECK_NUMBER (base);
2376 b = XINT (base);
2377 if (b < 2 || b > 16)
2378 xsignal1 (Qargs_out_of_range, base);
2381 /* Skip any whitespace at the front of the number. Some versions of
2382 atoi do this anyway, so we might as well make Emacs lisp consistent. */
2383 p = SDATA (string);
2384 while (*p == ' ' || *p == '\t')
2385 p++;
2387 if (*p == '-')
2389 sign = -1;
2390 p++;
2392 else if (*p == '+')
2393 p++;
2395 if (isfloat_string (p, 1) && b == 10)
2396 val = make_float (sign * atof (p));
2397 else
2399 double v = 0;
2401 while (1)
2403 int digit = digit_to_number (*p++, b);
2404 if (digit < 0)
2405 break;
2406 v = v * b + digit;
2409 val = make_fixnum_or_float (sign * v);
2412 return val;
2416 enum arithop
2418 Aadd,
2419 Asub,
2420 Amult,
2421 Adiv,
2422 Alogand,
2423 Alogior,
2424 Alogxor,
2425 Amax,
2426 Amin
2429 static Lisp_Object float_arith_driver P_ ((double, int, enum arithop,
2430 int, Lisp_Object *));
2431 extern Lisp_Object fmod_float ();
2433 Lisp_Object
2434 arith_driver (code, nargs, args)
2435 enum arithop code;
2436 int nargs;
2437 register Lisp_Object *args;
2439 register Lisp_Object val;
2440 register int argnum;
2441 register EMACS_INT accum = 0;
2442 register EMACS_INT next;
2444 switch (SWITCH_ENUM_CAST (code))
2446 case Alogior:
2447 case Alogxor:
2448 case Aadd:
2449 case Asub:
2450 accum = 0;
2451 break;
2452 case Amult:
2453 accum = 1;
2454 break;
2455 case Alogand:
2456 accum = -1;
2457 break;
2458 default:
2459 break;
2462 for (argnum = 0; argnum < nargs; argnum++)
2464 /* Using args[argnum] as argument to CHECK_NUMBER_... */
2465 val = args[argnum];
2466 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val);
2468 if (FLOATP (val))
2469 return float_arith_driver ((double) accum, argnum, code,
2470 nargs, args);
2471 args[argnum] = val;
2472 next = XINT (args[argnum]);
2473 switch (SWITCH_ENUM_CAST (code))
2475 case Aadd:
2476 accum += next;
2477 break;
2478 case Asub:
2479 accum = argnum ? accum - next : nargs == 1 ? - next : next;
2480 break;
2481 case Amult:
2482 accum *= next;
2483 break;
2484 case Adiv:
2485 if (!argnum)
2486 accum = next;
2487 else
2489 if (next == 0)
2490 xsignal0 (Qarith_error);
2491 accum /= next;
2493 break;
2494 case Alogand:
2495 accum &= next;
2496 break;
2497 case Alogior:
2498 accum |= next;
2499 break;
2500 case Alogxor:
2501 accum ^= next;
2502 break;
2503 case Amax:
2504 if (!argnum || next > accum)
2505 accum = next;
2506 break;
2507 case Amin:
2508 if (!argnum || next < accum)
2509 accum = next;
2510 break;
2514 XSETINT (val, accum);
2515 return val;
2518 #undef isnan
2519 #define isnan(x) ((x) != (x))
2521 static Lisp_Object
2522 float_arith_driver (accum, argnum, code, nargs, args)
2523 double accum;
2524 register int argnum;
2525 enum arithop code;
2526 int nargs;
2527 register Lisp_Object *args;
2529 register Lisp_Object val;
2530 double next;
2532 for (; argnum < nargs; argnum++)
2534 val = args[argnum]; /* using args[argnum] as argument to CHECK_NUMBER_... */
2535 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val);
2537 if (FLOATP (val))
2539 next = XFLOAT_DATA (val);
2541 else
2543 args[argnum] = val; /* runs into a compiler bug. */
2544 next = XINT (args[argnum]);
2546 switch (SWITCH_ENUM_CAST (code))
2548 case Aadd:
2549 accum += next;
2550 break;
2551 case Asub:
2552 accum = argnum ? accum - next : nargs == 1 ? - next : next;
2553 break;
2554 case Amult:
2555 accum *= next;
2556 break;
2557 case Adiv:
2558 if (!argnum)
2559 accum = next;
2560 else
2562 if (! IEEE_FLOATING_POINT && next == 0)
2563 xsignal0 (Qarith_error);
2564 accum /= next;
2566 break;
2567 case Alogand:
2568 case Alogior:
2569 case Alogxor:
2570 return wrong_type_argument (Qinteger_or_marker_p, val);
2571 case Amax:
2572 if (!argnum || isnan (next) || next > accum)
2573 accum = next;
2574 break;
2575 case Amin:
2576 if (!argnum || isnan (next) || next < accum)
2577 accum = next;
2578 break;
2582 return make_float (accum);
2586 DEFUN ("+", Fplus, Splus, 0, MANY, 0,
2587 doc: /* Return sum of any number of arguments, which are numbers or markers.
2588 usage: (+ &rest NUMBERS-OR-MARKERS) */)
2589 (nargs, args)
2590 int nargs;
2591 Lisp_Object *args;
2593 return arith_driver (Aadd, nargs, args);
2596 DEFUN ("-", Fminus, Sminus, 0, MANY, 0,
2597 doc: /* Negate number or subtract numbers or markers and return the result.
2598 With one arg, negates it. With more than one arg,
2599 subtracts all but the first from the first.
2600 usage: (- &optional NUMBER-OR-MARKER &rest MORE-NUMBERS-OR-MARKERS) */)
2601 (nargs, args)
2602 int nargs;
2603 Lisp_Object *args;
2605 return arith_driver (Asub, nargs, args);
2608 DEFUN ("*", Ftimes, Stimes, 0, MANY, 0,
2609 doc: /* Return product of any number of arguments, which are numbers or markers.
2610 usage: (* &rest NUMBERS-OR-MARKERS) */)
2611 (nargs, args)
2612 int nargs;
2613 Lisp_Object *args;
2615 return arith_driver (Amult, nargs, args);
2618 DEFUN ("/", Fquo, Squo, 2, MANY, 0,
2619 doc: /* Return first argument divided by all the remaining arguments.
2620 The arguments must be numbers or markers.
2621 usage: (/ DIVIDEND DIVISOR &rest DIVISORS) */)
2622 (nargs, args)
2623 int nargs;
2624 Lisp_Object *args;
2626 int argnum;
2627 for (argnum = 2; argnum < nargs; argnum++)
2628 if (FLOATP (args[argnum]))
2629 return float_arith_driver (0, 0, Adiv, nargs, args);
2630 return arith_driver (Adiv, nargs, args);
2633 DEFUN ("%", Frem, Srem, 2, 2, 0,
2634 doc: /* Return remainder of X divided by Y.
2635 Both must be integers or markers. */)
2636 (x, y)
2637 register Lisp_Object x, y;
2639 Lisp_Object val;
2641 CHECK_NUMBER_COERCE_MARKER (x);
2642 CHECK_NUMBER_COERCE_MARKER (y);
2644 if (XFASTINT (y) == 0)
2645 xsignal0 (Qarith_error);
2647 XSETINT (val, XINT (x) % XINT (y));
2648 return val;
2651 #ifndef HAVE_FMOD
2652 double
2653 fmod (f1, f2)
2654 double f1, f2;
2656 double r = f1;
2658 if (f2 < 0.0)
2659 f2 = -f2;
2661 /* If the magnitude of the result exceeds that of the divisor, or
2662 the sign of the result does not agree with that of the dividend,
2663 iterate with the reduced value. This does not yield a
2664 particularly accurate result, but at least it will be in the
2665 range promised by fmod. */
2667 r -= f2 * floor (r / f2);
2668 while (f2 <= (r < 0 ? -r : r) || ((r < 0) != (f1 < 0) && ! isnan (r)));
2670 return r;
2672 #endif /* ! HAVE_FMOD */
2674 DEFUN ("mod", Fmod, Smod, 2, 2, 0,
2675 doc: /* Return X modulo Y.
2676 The result falls between zero (inclusive) and Y (exclusive).
2677 Both X and Y must be numbers or markers. */)
2678 (x, y)
2679 register Lisp_Object x, y;
2681 Lisp_Object val;
2682 EMACS_INT i1, i2;
2684 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (x);
2685 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (y);
2687 if (FLOATP (x) || FLOATP (y))
2688 return fmod_float (x, y);
2690 i1 = XINT (x);
2691 i2 = XINT (y);
2693 if (i2 == 0)
2694 xsignal0 (Qarith_error);
2696 i1 %= i2;
2698 /* If the "remainder" comes out with the wrong sign, fix it. */
2699 if (i2 < 0 ? i1 > 0 : i1 < 0)
2700 i1 += i2;
2702 XSETINT (val, i1);
2703 return val;
2706 DEFUN ("max", Fmax, Smax, 1, MANY, 0,
2707 doc: /* Return largest of all the arguments (which must be numbers or markers).
2708 The value is always a number; markers are converted to numbers.
2709 usage: (max NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2710 (nargs, args)
2711 int nargs;
2712 Lisp_Object *args;
2714 return arith_driver (Amax, nargs, args);
2717 DEFUN ("min", Fmin, Smin, 1, MANY, 0,
2718 doc: /* Return smallest of all the arguments (which must be numbers or markers).
2719 The value is always a number; markers are converted to numbers.
2720 usage: (min NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2721 (nargs, args)
2722 int nargs;
2723 Lisp_Object *args;
2725 return arith_driver (Amin, nargs, args);
2728 DEFUN ("logand", Flogand, Slogand, 0, MANY, 0,
2729 doc: /* Return bitwise-and of all the arguments.
2730 Arguments may be integers, or markers converted to integers.
2731 usage: (logand &rest INTS-OR-MARKERS) */)
2732 (nargs, args)
2733 int nargs;
2734 Lisp_Object *args;
2736 return arith_driver (Alogand, nargs, args);
2739 DEFUN ("logior", Flogior, Slogior, 0, MANY, 0,
2740 doc: /* Return bitwise-or of all the arguments.
2741 Arguments may be integers, or markers converted to integers.
2742 usage: (logior &rest INTS-OR-MARKERS) */)
2743 (nargs, args)
2744 int nargs;
2745 Lisp_Object *args;
2747 return arith_driver (Alogior, nargs, args);
2750 DEFUN ("logxor", Flogxor, Slogxor, 0, MANY, 0,
2751 doc: /* Return bitwise-exclusive-or of all the arguments.
2752 Arguments may be integers, or markers converted to integers.
2753 usage: (logxor &rest INTS-OR-MARKERS) */)
2754 (nargs, args)
2755 int nargs;
2756 Lisp_Object *args;
2758 return arith_driver (Alogxor, nargs, args);
2761 DEFUN ("ash", Fash, Sash, 2, 2, 0,
2762 doc: /* Return VALUE with its bits shifted left by COUNT.
2763 If COUNT is negative, shifting is actually to the right.
2764 In this case, the sign bit is duplicated. */)
2765 (value, count)
2766 register Lisp_Object value, count;
2768 register Lisp_Object val;
2770 CHECK_NUMBER (value);
2771 CHECK_NUMBER (count);
2773 if (XINT (count) >= BITS_PER_EMACS_INT)
2774 XSETINT (val, 0);
2775 else if (XINT (count) > 0)
2776 XSETINT (val, XINT (value) << XFASTINT (count));
2777 else if (XINT (count) <= -BITS_PER_EMACS_INT)
2778 XSETINT (val, XINT (value) < 0 ? -1 : 0);
2779 else
2780 XSETINT (val, XINT (value) >> -XINT (count));
2781 return val;
2784 DEFUN ("lsh", Flsh, Slsh, 2, 2, 0,
2785 doc: /* Return VALUE with its bits shifted left by COUNT.
2786 If COUNT is negative, shifting is actually to the right.
2787 In this case, zeros are shifted in on the left. */)
2788 (value, count)
2789 register Lisp_Object value, count;
2791 register Lisp_Object val;
2793 CHECK_NUMBER (value);
2794 CHECK_NUMBER (count);
2796 if (XINT (count) >= BITS_PER_EMACS_INT)
2797 XSETINT (val, 0);
2798 else if (XINT (count) > 0)
2799 XSETINT (val, (EMACS_UINT) XUINT (value) << XFASTINT (count));
2800 else if (XINT (count) <= -BITS_PER_EMACS_INT)
2801 XSETINT (val, 0);
2802 else
2803 XSETINT (val, (EMACS_UINT) XUINT (value) >> -XINT (count));
2804 return val;
2807 DEFUN ("1+", Fadd1, Sadd1, 1, 1, 0,
2808 doc: /* Return NUMBER plus one. NUMBER may be a number or a marker.
2809 Markers are converted to integers. */)
2810 (number)
2811 register Lisp_Object number;
2813 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number);
2815 if (FLOATP (number))
2816 return (make_float (1.0 + XFLOAT_DATA (number)));
2818 XSETINT (number, XINT (number) + 1);
2819 return number;
2822 DEFUN ("1-", Fsub1, Ssub1, 1, 1, 0,
2823 doc: /* Return NUMBER minus one. NUMBER may be a number or a marker.
2824 Markers are converted to integers. */)
2825 (number)
2826 register Lisp_Object number;
2828 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number);
2830 if (FLOATP (number))
2831 return (make_float (-1.0 + XFLOAT_DATA (number)));
2833 XSETINT (number, XINT (number) - 1);
2834 return number;
2837 DEFUN ("lognot", Flognot, Slognot, 1, 1, 0,
2838 doc: /* Return the bitwise complement of NUMBER. NUMBER must be an integer. */)
2839 (number)
2840 register Lisp_Object number;
2842 CHECK_NUMBER (number);
2843 XSETINT (number, ~XINT (number));
2844 return number;
2847 DEFUN ("byteorder", Fbyteorder, Sbyteorder, 0, 0, 0,
2848 doc: /* Return the byteorder for the machine.
2849 Returns 66 (ASCII uppercase B) for big endian machines or 108 (ASCII
2850 lowercase l) for small endian machines. */)
2853 unsigned i = 0x04030201;
2854 int order = *(char *)&i == 1 ? 108 : 66;
2856 return make_number (order);
2861 void
2862 syms_of_data ()
2864 Lisp_Object error_tail, arith_tail;
2866 Qquote = intern_c_string ("quote");
2867 Qlambda = intern_c_string ("lambda");
2868 Qsubr = intern_c_string ("subr");
2869 Qerror_conditions = intern_c_string ("error-conditions");
2870 Qerror_message = intern_c_string ("error-message");
2871 Qtop_level = intern_c_string ("top-level");
2873 Qerror = intern_c_string ("error");
2874 Qquit = intern_c_string ("quit");
2875 Qwrong_type_argument = intern_c_string ("wrong-type-argument");
2876 Qargs_out_of_range = intern_c_string ("args-out-of-range");
2877 Qvoid_function = intern_c_string ("void-function");
2878 Qcyclic_function_indirection = intern_c_string ("cyclic-function-indirection");
2879 Qcyclic_variable_indirection = intern_c_string ("cyclic-variable-indirection");
2880 Qvoid_variable = intern_c_string ("void-variable");
2881 Qsetting_constant = intern_c_string ("setting-constant");
2882 Qinvalid_read_syntax = intern_c_string ("invalid-read-syntax");
2884 Qinvalid_function = intern_c_string ("invalid-function");
2885 Qwrong_number_of_arguments = intern_c_string ("wrong-number-of-arguments");
2886 Qno_catch = intern_c_string ("no-catch");
2887 Qend_of_file = intern_c_string ("end-of-file");
2888 Qarith_error = intern_c_string ("arith-error");
2889 Qbeginning_of_buffer = intern_c_string ("beginning-of-buffer");
2890 Qend_of_buffer = intern_c_string ("end-of-buffer");
2891 Qbuffer_read_only = intern_c_string ("buffer-read-only");
2892 Qtext_read_only = intern_c_string ("text-read-only");
2893 Qmark_inactive = intern_c_string ("mark-inactive");
2895 Qlistp = intern_c_string ("listp");
2896 Qconsp = intern_c_string ("consp");
2897 Qsymbolp = intern_c_string ("symbolp");
2898 Qkeywordp = intern_c_string ("keywordp");
2899 Qintegerp = intern_c_string ("integerp");
2900 Qnatnump = intern_c_string ("natnump");
2901 Qwholenump = intern_c_string ("wholenump");
2902 Qstringp = intern_c_string ("stringp");
2903 Qarrayp = intern_c_string ("arrayp");
2904 Qsequencep = intern_c_string ("sequencep");
2905 Qbufferp = intern_c_string ("bufferp");
2906 Qvectorp = intern_c_string ("vectorp");
2907 Qchar_or_string_p = intern_c_string ("char-or-string-p");
2908 Qmarkerp = intern_c_string ("markerp");
2909 Qbuffer_or_string_p = intern_c_string ("buffer-or-string-p");
2910 Qinteger_or_marker_p = intern_c_string ("integer-or-marker-p");
2911 Qboundp = intern_c_string ("boundp");
2912 Qfboundp = intern_c_string ("fboundp");
2914 Qfloatp = intern_c_string ("floatp");
2915 Qnumberp = intern_c_string ("numberp");
2916 Qnumber_or_marker_p = intern_c_string ("number-or-marker-p");
2918 Qchar_table_p = intern_c_string ("char-table-p");
2919 Qvector_or_char_table_p = intern_c_string ("vector-or-char-table-p");
2921 Qsubrp = intern_c_string ("subrp");
2922 Qunevalled = intern_c_string ("unevalled");
2923 Qmany = intern_c_string ("many");
2925 Qcdr = intern_c_string ("cdr");
2927 /* Handle automatic advice activation */
2928 Qad_advice_info = intern_c_string ("ad-advice-info");
2929 Qad_activate_internal = intern_c_string ("ad-activate-internal");
2931 error_tail = pure_cons (Qerror, Qnil);
2933 /* ERROR is used as a signaler for random errors for which nothing else is right */
2935 Fput (Qerror, Qerror_conditions,
2936 error_tail);
2937 Fput (Qerror, Qerror_message,
2938 make_pure_c_string ("error"));
2940 Fput (Qquit, Qerror_conditions,
2941 pure_cons (Qquit, Qnil));
2942 Fput (Qquit, Qerror_message,
2943 make_pure_c_string ("Quit"));
2945 Fput (Qwrong_type_argument, Qerror_conditions,
2946 pure_cons (Qwrong_type_argument, error_tail));
2947 Fput (Qwrong_type_argument, Qerror_message,
2948 make_pure_c_string ("Wrong type argument"));
2950 Fput (Qargs_out_of_range, Qerror_conditions,
2951 pure_cons (Qargs_out_of_range, error_tail));
2952 Fput (Qargs_out_of_range, Qerror_message,
2953 make_pure_c_string ("Args out of range"));
2955 Fput (Qvoid_function, Qerror_conditions,
2956 pure_cons (Qvoid_function, error_tail));
2957 Fput (Qvoid_function, Qerror_message,
2958 make_pure_c_string ("Symbol's function definition is void"));
2960 Fput (Qcyclic_function_indirection, Qerror_conditions,
2961 pure_cons (Qcyclic_function_indirection, error_tail));
2962 Fput (Qcyclic_function_indirection, Qerror_message,
2963 make_pure_c_string ("Symbol's chain of function indirections contains a loop"));
2965 Fput (Qcyclic_variable_indirection, Qerror_conditions,
2966 pure_cons (Qcyclic_variable_indirection, error_tail));
2967 Fput (Qcyclic_variable_indirection, Qerror_message,
2968 make_pure_c_string ("Symbol's chain of variable indirections contains a loop"));
2970 Qcircular_list = intern_c_string ("circular-list");
2971 staticpro (&Qcircular_list);
2972 Fput (Qcircular_list, Qerror_conditions,
2973 pure_cons (Qcircular_list, error_tail));
2974 Fput (Qcircular_list, Qerror_message,
2975 make_pure_c_string ("List contains a loop"));
2977 Fput (Qvoid_variable, Qerror_conditions,
2978 pure_cons (Qvoid_variable, error_tail));
2979 Fput (Qvoid_variable, Qerror_message,
2980 make_pure_c_string ("Symbol's value as variable is void"));
2982 Fput (Qsetting_constant, Qerror_conditions,
2983 pure_cons (Qsetting_constant, error_tail));
2984 Fput (Qsetting_constant, Qerror_message,
2985 make_pure_c_string ("Attempt to set a constant symbol"));
2987 Fput (Qinvalid_read_syntax, Qerror_conditions,
2988 pure_cons (Qinvalid_read_syntax, error_tail));
2989 Fput (Qinvalid_read_syntax, Qerror_message,
2990 make_pure_c_string ("Invalid read syntax"));
2992 Fput (Qinvalid_function, Qerror_conditions,
2993 pure_cons (Qinvalid_function, error_tail));
2994 Fput (Qinvalid_function, Qerror_message,
2995 make_pure_c_string ("Invalid function"));
2997 Fput (Qwrong_number_of_arguments, Qerror_conditions,
2998 pure_cons (Qwrong_number_of_arguments, error_tail));
2999 Fput (Qwrong_number_of_arguments, Qerror_message,
3000 make_pure_c_string ("Wrong number of arguments"));
3002 Fput (Qno_catch, Qerror_conditions,
3003 pure_cons (Qno_catch, error_tail));
3004 Fput (Qno_catch, Qerror_message,
3005 make_pure_c_string ("No catch for tag"));
3007 Fput (Qend_of_file, Qerror_conditions,
3008 pure_cons (Qend_of_file, error_tail));
3009 Fput (Qend_of_file, Qerror_message,
3010 make_pure_c_string ("End of file during parsing"));
3012 arith_tail = pure_cons (Qarith_error, error_tail);
3013 Fput (Qarith_error, Qerror_conditions,
3014 arith_tail);
3015 Fput (Qarith_error, Qerror_message,
3016 make_pure_c_string ("Arithmetic error"));
3018 Fput (Qbeginning_of_buffer, Qerror_conditions,
3019 pure_cons (Qbeginning_of_buffer, error_tail));
3020 Fput (Qbeginning_of_buffer, Qerror_message,
3021 make_pure_c_string ("Beginning of buffer"));
3023 Fput (Qend_of_buffer, Qerror_conditions,
3024 pure_cons (Qend_of_buffer, error_tail));
3025 Fput (Qend_of_buffer, Qerror_message,
3026 make_pure_c_string ("End of buffer"));
3028 Fput (Qbuffer_read_only, Qerror_conditions,
3029 pure_cons (Qbuffer_read_only, error_tail));
3030 Fput (Qbuffer_read_only, Qerror_message,
3031 make_pure_c_string ("Buffer is read-only"));
3033 Fput (Qtext_read_only, Qerror_conditions,
3034 pure_cons (Qtext_read_only, error_tail));
3035 Fput (Qtext_read_only, Qerror_message,
3036 make_pure_c_string ("Text is read-only"));
3038 Qrange_error = intern_c_string ("range-error");
3039 Qdomain_error = intern_c_string ("domain-error");
3040 Qsingularity_error = intern_c_string ("singularity-error");
3041 Qoverflow_error = intern_c_string ("overflow-error");
3042 Qunderflow_error = intern_c_string ("underflow-error");
3044 Fput (Qdomain_error, Qerror_conditions,
3045 pure_cons (Qdomain_error, arith_tail));
3046 Fput (Qdomain_error, Qerror_message,
3047 make_pure_c_string ("Arithmetic domain error"));
3049 Fput (Qrange_error, Qerror_conditions,
3050 pure_cons (Qrange_error, arith_tail));
3051 Fput (Qrange_error, Qerror_message,
3052 make_pure_c_string ("Arithmetic range error"));
3054 Fput (Qsingularity_error, Qerror_conditions,
3055 pure_cons (Qsingularity_error, Fcons (Qdomain_error, arith_tail)));
3056 Fput (Qsingularity_error, Qerror_message,
3057 make_pure_c_string ("Arithmetic singularity error"));
3059 Fput (Qoverflow_error, Qerror_conditions,
3060 pure_cons (Qoverflow_error, Fcons (Qdomain_error, arith_tail)));
3061 Fput (Qoverflow_error, Qerror_message,
3062 make_pure_c_string ("Arithmetic overflow error"));
3064 Fput (Qunderflow_error, Qerror_conditions,
3065 pure_cons (Qunderflow_error, Fcons (Qdomain_error, arith_tail)));
3066 Fput (Qunderflow_error, Qerror_message,
3067 make_pure_c_string ("Arithmetic underflow error"));
3069 staticpro (&Qrange_error);
3070 staticpro (&Qdomain_error);
3071 staticpro (&Qsingularity_error);
3072 staticpro (&Qoverflow_error);
3073 staticpro (&Qunderflow_error);
3075 staticpro (&Qnil);
3076 staticpro (&Qt);
3077 staticpro (&Qquote);
3078 staticpro (&Qlambda);
3079 staticpro (&Qsubr);
3080 staticpro (&Qunbound);
3081 staticpro (&Qerror_conditions);
3082 staticpro (&Qerror_message);
3083 staticpro (&Qtop_level);
3085 staticpro (&Qerror);
3086 staticpro (&Qquit);
3087 staticpro (&Qwrong_type_argument);
3088 staticpro (&Qargs_out_of_range);
3089 staticpro (&Qvoid_function);
3090 staticpro (&Qcyclic_function_indirection);
3091 staticpro (&Qcyclic_variable_indirection);
3092 staticpro (&Qvoid_variable);
3093 staticpro (&Qsetting_constant);
3094 staticpro (&Qinvalid_read_syntax);
3095 staticpro (&Qwrong_number_of_arguments);
3096 staticpro (&Qinvalid_function);
3097 staticpro (&Qno_catch);
3098 staticpro (&Qend_of_file);
3099 staticpro (&Qarith_error);
3100 staticpro (&Qbeginning_of_buffer);
3101 staticpro (&Qend_of_buffer);
3102 staticpro (&Qbuffer_read_only);
3103 staticpro (&Qtext_read_only);
3104 staticpro (&Qmark_inactive);
3106 staticpro (&Qlistp);
3107 staticpro (&Qconsp);
3108 staticpro (&Qsymbolp);
3109 staticpro (&Qkeywordp);
3110 staticpro (&Qintegerp);
3111 staticpro (&Qnatnump);
3112 staticpro (&Qwholenump);
3113 staticpro (&Qstringp);
3114 staticpro (&Qarrayp);
3115 staticpro (&Qsequencep);
3116 staticpro (&Qbufferp);
3117 staticpro (&Qvectorp);
3118 staticpro (&Qchar_or_string_p);
3119 staticpro (&Qmarkerp);
3120 staticpro (&Qbuffer_or_string_p);
3121 staticpro (&Qinteger_or_marker_p);
3122 staticpro (&Qfloatp);
3123 staticpro (&Qnumberp);
3124 staticpro (&Qnumber_or_marker_p);
3125 staticpro (&Qchar_table_p);
3126 staticpro (&Qvector_or_char_table_p);
3127 staticpro (&Qsubrp);
3128 staticpro (&Qmany);
3129 staticpro (&Qunevalled);
3131 staticpro (&Qboundp);
3132 staticpro (&Qfboundp);
3133 staticpro (&Qcdr);
3134 staticpro (&Qad_advice_info);
3135 staticpro (&Qad_activate_internal);
3137 /* Types that type-of returns. */
3138 Qinteger = intern_c_string ("integer");
3139 Qsymbol = intern_c_string ("symbol");
3140 Qstring = intern_c_string ("string");
3141 Qcons = intern_c_string ("cons");
3142 Qmarker = intern_c_string ("marker");
3143 Qoverlay = intern_c_string ("overlay");
3144 Qfloat = intern_c_string ("float");
3145 Qwindow_configuration = intern_c_string ("window-configuration");
3146 Qprocess = intern_c_string ("process");
3147 Qwindow = intern_c_string ("window");
3148 /* Qsubr = intern_c_string ("subr"); */
3149 Qcompiled_function = intern_c_string ("compiled-function");
3150 Qbuffer = intern_c_string ("buffer");
3151 Qframe = intern_c_string ("frame");
3152 Qvector = intern_c_string ("vector");
3153 Qchar_table = intern_c_string ("char-table");
3154 Qbool_vector = intern_c_string ("bool-vector");
3155 Qhash_table = intern_c_string ("hash-table");
3157 DEFSYM (Qfont_spec, "font-spec");
3158 DEFSYM (Qfont_entity, "font-entity");
3159 DEFSYM (Qfont_object, "font-object");
3161 DEFSYM (Qinteractive_form, "interactive-form");
3163 staticpro (&Qinteger);
3164 staticpro (&Qsymbol);
3165 staticpro (&Qstring);
3166 staticpro (&Qcons);
3167 staticpro (&Qmarker);
3168 staticpro (&Qoverlay);
3169 staticpro (&Qfloat);
3170 staticpro (&Qwindow_configuration);
3171 staticpro (&Qprocess);
3172 staticpro (&Qwindow);
3173 /* staticpro (&Qsubr); */
3174 staticpro (&Qcompiled_function);
3175 staticpro (&Qbuffer);
3176 staticpro (&Qframe);
3177 staticpro (&Qvector);
3178 staticpro (&Qchar_table);
3179 staticpro (&Qbool_vector);
3180 staticpro (&Qhash_table);
3182 defsubr (&Sindirect_variable);
3183 defsubr (&Sinteractive_form);
3184 defsubr (&Seq);
3185 defsubr (&Snull);
3186 defsubr (&Stype_of);
3187 defsubr (&Slistp);
3188 defsubr (&Snlistp);
3189 defsubr (&Sconsp);
3190 defsubr (&Satom);
3191 defsubr (&Sintegerp);
3192 defsubr (&Sinteger_or_marker_p);
3193 defsubr (&Snumberp);
3194 defsubr (&Snumber_or_marker_p);
3195 defsubr (&Sfloatp);
3196 defsubr (&Snatnump);
3197 defsubr (&Ssymbolp);
3198 defsubr (&Skeywordp);
3199 defsubr (&Sstringp);
3200 defsubr (&Smultibyte_string_p);
3201 defsubr (&Svectorp);
3202 defsubr (&Schar_table_p);
3203 defsubr (&Svector_or_char_table_p);
3204 defsubr (&Sbool_vector_p);
3205 defsubr (&Sarrayp);
3206 defsubr (&Ssequencep);
3207 defsubr (&Sbufferp);
3208 defsubr (&Smarkerp);
3209 defsubr (&Ssubrp);
3210 defsubr (&Sbyte_code_function_p);
3211 defsubr (&Schar_or_string_p);
3212 defsubr (&Scar);
3213 defsubr (&Scdr);
3214 defsubr (&Scar_safe);
3215 defsubr (&Scdr_safe);
3216 defsubr (&Ssetcar);
3217 defsubr (&Ssetcdr);
3218 defsubr (&Ssymbol_function);
3219 defsubr (&Sindirect_function);
3220 defsubr (&Ssymbol_plist);
3221 defsubr (&Ssymbol_name);
3222 defsubr (&Smakunbound);
3223 defsubr (&Sfmakunbound);
3224 defsubr (&Sboundp);
3225 defsubr (&Sfboundp);
3226 defsubr (&Sfset);
3227 defsubr (&Sdefalias);
3228 defsubr (&Ssetplist);
3229 defsubr (&Ssymbol_value);
3230 defsubr (&Sset);
3231 defsubr (&Sdefault_boundp);
3232 defsubr (&Sdefault_value);
3233 defsubr (&Sset_default);
3234 defsubr (&Ssetq_default);
3235 defsubr (&Smake_variable_buffer_local);
3236 defsubr (&Smake_local_variable);
3237 defsubr (&Skill_local_variable);
3238 defsubr (&Smake_variable_frame_local);
3239 defsubr (&Slocal_variable_p);
3240 defsubr (&Slocal_variable_if_set_p);
3241 defsubr (&Svariable_binding_locus);
3242 #if 0 /* XXX Remove this. --lorentey */
3243 defsubr (&Sterminal_local_value);
3244 defsubr (&Sset_terminal_local_value);
3245 #endif
3246 defsubr (&Saref);
3247 defsubr (&Saset);
3248 defsubr (&Snumber_to_string);
3249 defsubr (&Sstring_to_number);
3250 defsubr (&Seqlsign);
3251 defsubr (&Slss);
3252 defsubr (&Sgtr);
3253 defsubr (&Sleq);
3254 defsubr (&Sgeq);
3255 defsubr (&Sneq);
3256 defsubr (&Szerop);
3257 defsubr (&Splus);
3258 defsubr (&Sminus);
3259 defsubr (&Stimes);
3260 defsubr (&Squo);
3261 defsubr (&Srem);
3262 defsubr (&Smod);
3263 defsubr (&Smax);
3264 defsubr (&Smin);
3265 defsubr (&Slogand);
3266 defsubr (&Slogior);
3267 defsubr (&Slogxor);
3268 defsubr (&Slsh);
3269 defsubr (&Sash);
3270 defsubr (&Sadd1);
3271 defsubr (&Ssub1);
3272 defsubr (&Slognot);
3273 defsubr (&Sbyteorder);
3274 defsubr (&Ssubr_arity);
3275 defsubr (&Ssubr_name);
3277 XSYMBOL (Qwholenump)->function = XSYMBOL (Qnatnump)->function;
3279 DEFVAR_LISP ("most-positive-fixnum", &Vmost_positive_fixnum,
3280 doc: /* The largest value that is representable in a Lisp integer. */);
3281 Vmost_positive_fixnum = make_number (MOST_POSITIVE_FIXNUM);
3282 XSYMBOL (intern_c_string ("most-positive-fixnum"))->constant = 1;
3284 DEFVAR_LISP ("most-negative-fixnum", &Vmost_negative_fixnum,
3285 doc: /* The smallest value that is representable in a Lisp integer. */);
3286 Vmost_negative_fixnum = make_number (MOST_NEGATIVE_FIXNUM);
3287 XSYMBOL (intern_c_string ("most-negative-fixnum"))->constant = 1;
3290 SIGTYPE
3291 arith_error (signo)
3292 int signo;
3294 #if defined(USG) && !defined(POSIX_SIGNALS)
3295 /* USG systems forget handlers when they are used;
3296 must reestablish each time */
3297 signal (signo, arith_error);
3298 #endif /* USG */
3299 sigsetmask (SIGEMPTYMASK);
3301 SIGNAL_THREAD_CHECK (signo);
3302 xsignal0 (Qarith_error);
3305 void
3306 init_data ()
3308 /* Don't do this if just dumping out.
3309 We don't want to call `signal' in this case
3310 so that we don't have trouble with dumping
3311 signal-delivering routines in an inconsistent state. */
3312 #ifndef CANNOT_DUMP
3313 if (!initialized)
3314 return;
3315 #endif /* CANNOT_DUMP */
3316 signal (SIGFPE, arith_error);
3318 #ifdef uts
3319 signal (SIGEMT, arith_error);
3320 #endif /* uts */
3323 /* arch-tag: 25879798-b84d-479a-9c89-7d148e2109f7
3324 (do not change this comment) */