Switch license to GPLv3 or later.
[bpt/emacs.git] / src / data.c
CommitLineData
7921925c 1/* Primitive operations on Lisp data types for GNU Emacs Lisp interpreter.
0b5538bd 2 Copyright (C) 1985, 1986, 1988, 1993, 1994, 1995, 1997, 1998, 1999, 2000,
4e6835db 3 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
7921925c
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
684d6f5b 9the Free Software Foundation; either version 3, or (at your option)
7921925c
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
4fc5845f
LK
19the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20Boston, MA 02110-1301, USA. */
7921925c
JB
21
22
18160b98 23#include <config.h>
68c45bf0 24#include <signal.h>
dd8daec5 25#include <stdio.h>
7921925c 26#include "lisp.h"
29eab336 27#include "puresize.h"
8313c4e7 28#include "charset.h"
7921925c 29#include "buffer.h"
077d751f 30#include "keyboard.h"
b0c2d1c6 31#include "frame.h"
a44804c2 32#include "syssignal.h"
fb8e9847 33
93b91208 34#ifdef STDC_HEADERS
2f261542 35#include <float.h>
93b91208 36#endif
defa77b5 37
ad8d56b9
PE
38/* If IEEE_FLOATING_POINT isn't defined, default it from FLT_*. */
39#ifndef IEEE_FLOATING_POINT
40#if (FLT_RADIX == 2 && FLT_MANT_DIG == 24 \
41 && FLT_MIN_EXP == -125 && FLT_MAX_EXP == 128)
42#define IEEE_FLOATING_POINT 1
43#else
44#define IEEE_FLOATING_POINT 0
45#endif
46#endif
47
defa77b5
RS
48/* Work around a problem that happens because math.h on hpux 7
49 defines two static variables--which, in Emacs, are not really static,
50 because `static' is defined as nothing. The problem is that they are
51 here, in floatfns.c, and in lread.c.
52 These macros prevent the name conflict. */
53#if defined (HPUX) && !defined (HPUX8)
54#define _MAXLDBL data_c_maxldbl
55#define _NMAXLDBL data_c_nmaxldbl
56#endif
57
7921925c 58#include <math.h>
7921925c 59
024ec58f
BF
60#if !defined (atof)
61extern double atof ();
62#endif /* !atof */
63
7921925c
JB
64Lisp_Object Qnil, Qt, Qquote, Qlambda, Qsubr, Qunbound;
65Lisp_Object Qerror_conditions, Qerror_message, Qtop_level;
66Lisp_Object Qerror, Qquit, Qwrong_type_argument, Qargs_out_of_range;
ffd56f97 67Lisp_Object Qvoid_variable, Qvoid_function, Qcyclic_function_indirection;
13d95cc0 68Lisp_Object Qcyclic_variable_indirection, Qcircular_list;
7921925c
JB
69Lisp_Object Qsetting_constant, Qinvalid_read_syntax;
70Lisp_Object Qinvalid_function, Qwrong_number_of_arguments, Qno_catch;
3b8819d6 71Lisp_Object Qend_of_file, Qarith_error, Qmark_inactive;
7921925c 72Lisp_Object Qbeginning_of_buffer, Qend_of_buffer, Qbuffer_read_only;
8f9f49d7 73Lisp_Object Qtext_read_only;
aae13d36 74
8e86942b 75Lisp_Object Qintegerp, Qnatnump, Qwholenump, Qsymbolp, Qlistp, Qconsp;
7921925c
JB
76Lisp_Object Qstringp, Qarrayp, Qsequencep, Qbufferp;
77Lisp_Object Qchar_or_string_p, Qmarkerp, Qinteger_or_marker_p, Qvectorp;
cda9b832 78Lisp_Object Qbuffer_or_string_p, Qkeywordp;
7921925c 79Lisp_Object Qboundp, Qfboundp;
7f0edce7 80Lisp_Object Qchar_table_p, Qvector_or_char_table_p;
39bcc759 81
7921925c 82Lisp_Object Qcdr;
c1307a23 83Lisp_Object Qad_advice_info, Qad_activate_internal;
7921925c 84
6315e761
RS
85Lisp_Object Qrange_error, Qdomain_error, Qsingularity_error;
86Lisp_Object Qoverflow_error, Qunderflow_error;
87
464f8898 88Lisp_Object Qfloatp;
7921925c 89Lisp_Object Qnumberp, Qnumber_or_marker_p;
7921925c 90
aae13d36
LH
91Lisp_Object Qinteger;
92static Lisp_Object Qsymbol, Qstring, Qcons, Qmarker, Qoverlay;
8313c4e7
KH
93static Lisp_Object Qfloat, Qwindow_configuration, Qwindow;
94Lisp_Object Qprocess;
39bcc759 95static Lisp_Object Qcompiled_function, Qbuffer, Qframe, Qvector;
81dc5de5 96static Lisp_Object Qchar_table, Qbool_vector, Qhash_table;
6f0e897f 97static Lisp_Object Qsubrp, Qmany, Qunevalled;
39bcc759 98
7a283f36 99static Lisp_Object swap_in_symval_forwarding P_ ((Lisp_Object, Lisp_Object));
d02eeab3 100
9d113d9d 101Lisp_Object Vmost_positive_fixnum, Vmost_negative_fixnum;
e6190b11 102
13d95cc0
GM
103
104void
105circular_list_error (list)
106 Lisp_Object list;
107{
740ef0b5 108 xsignal (Qcircular_list, list);
13d95cc0
GM
109}
110
111
7921925c
JB
112Lisp_Object
113wrong_type_argument (predicate, value)
114 register Lisp_Object predicate, value;
115{
0c64a8cd
KS
116 /* If VALUE is not even a valid Lisp object, abort here
117 where we can get a backtrace showing where it came from. */
118 if ((unsigned int) XGCTYPE (value) >= Lisp_Type_Limit)
119 abort ();
120
740ef0b5 121 xsignal2 (Qwrong_type_argument, predicate, value);
7921925c
JB
122}
123
dfcf069d 124void
7921925c
JB
125pure_write_error ()
126{
127 error ("Attempt to modify read-only object");
128}
129
130void
131args_out_of_range (a1, a2)
132 Lisp_Object a1, a2;
133{
740ef0b5 134 xsignal2 (Qargs_out_of_range, a1, a2);
7921925c
JB
135}
136
137void
138args_out_of_range_3 (a1, a2, a3)
139 Lisp_Object a1, a2, a3;
140{
740ef0b5 141 xsignal3 (Qargs_out_of_range, a1, a2, a3);
7921925c
JB
142}
143
7921925c
JB
144/* On some machines, XINT needs a temporary location.
145 Here it is, in case it is needed. */
146
147int sign_extend_temp;
148
149/* On a few machines, XINT can only be done by calling this. */
150
151int
152sign_extend_lisp_int (num)
a0ed95ea 153 EMACS_INT num;
7921925c 154{
a0ed95ea
RS
155 if (num & (((EMACS_INT) 1) << (VALBITS - 1)))
156 return num | (((EMACS_INT) (-1)) << VALBITS);
7921925c 157 else
a0ed95ea 158 return num & ((((EMACS_INT) 1) << VALBITS) - 1);
7921925c
JB
159}
160\f
161/* Data type predicates */
162
163DEFUN ("eq", Feq, Seq, 2, 2, 0,
8c1a1077
PJ
164 doc: /* Return t if the two args are the same Lisp object. */)
165 (obj1, obj2)
7921925c
JB
166 Lisp_Object obj1, obj2;
167{
168 if (EQ (obj1, obj2))
169 return Qt;
170 return Qnil;
171}
172
8c1a1077
PJ
173DEFUN ("null", Fnull, Snull, 1, 1, 0,
174 doc: /* Return t if OBJECT is nil. */)
175 (object)
39bcc759 176 Lisp_Object object;
7921925c 177{
39bcc759 178 if (NILP (object))
7921925c
JB
179 return Qt;
180 return Qnil;
181}
182
39bcc759 183DEFUN ("type-of", Ftype_of, Stype_of, 1, 1, 0,
8c1a1077
PJ
184 doc: /* Return a symbol representing the type of OBJECT.
185The symbol returned names the object's basic type;
186for example, (type-of 1) returns `integer'. */)
187 (object)
39bcc759
RS
188 Lisp_Object object;
189{
190 switch (XGCTYPE (object))
191 {
192 case Lisp_Int:
193 return Qinteger;
194
195 case Lisp_Symbol:
196 return Qsymbol;
197
198 case Lisp_String:
199 return Qstring;
200
201 case Lisp_Cons:
202 return Qcons;
203
204 case Lisp_Misc:
324a6eef 205 switch (XMISCTYPE (object))
39bcc759
RS
206 {
207 case Lisp_Misc_Marker:
208 return Qmarker;
209 case Lisp_Misc_Overlay:
210 return Qoverlay;
211 case Lisp_Misc_Float:
212 return Qfloat;
213 }
214 abort ();
215
216 case Lisp_Vectorlike:
217 if (GC_WINDOW_CONFIGURATIONP (object))
218 return Qwindow_configuration;
219 if (GC_PROCESSP (object))
220 return Qprocess;
221 if (GC_WINDOWP (object))
222 return Qwindow;
223 if (GC_SUBRP (object))
224 return Qsubr;
225 if (GC_COMPILEDP (object))
226 return Qcompiled_function;
227 if (GC_BUFFERP (object))
228 return Qbuffer;
fc67d5be
KH
229 if (GC_CHAR_TABLE_P (object))
230 return Qchar_table;
231 if (GC_BOOL_VECTOR_P (object))
232 return Qbool_vector;
39bcc759
RS
233 if (GC_FRAMEP (object))
234 return Qframe;
81dc5de5
GM
235 if (GC_HASH_TABLE_P (object))
236 return Qhash_table;
39bcc759
RS
237 return Qvector;
238
39bcc759
RS
239 case Lisp_Float:
240 return Qfloat;
39bcc759
RS
241
242 default:
243 abort ();
244 }
245}
246
8c1a1077
PJ
247DEFUN ("consp", Fconsp, Sconsp, 1, 1, 0,
248 doc: /* Return t if OBJECT is a cons cell. */)
249 (object)
39bcc759 250 Lisp_Object object;
7921925c 251{
39bcc759 252 if (CONSP (object))
7921925c
JB
253 return Qt;
254 return Qnil;
255}
256
25638b07 257DEFUN ("atom", Fatom, Satom, 1, 1, 0,
8c1a1077
PJ
258 doc: /* Return t if OBJECT is not a cons cell. This includes nil. */)
259 (object)
39bcc759 260 Lisp_Object object;
7921925c 261{
39bcc759 262 if (CONSP (object))
7921925c
JB
263 return Qnil;
264 return Qt;
265}
266
25638b07 267DEFUN ("listp", Flistp, Slistp, 1, 1, 0,
4cdcdcc9
LT
268 doc: /* Return t if OBJECT is a list, that is, a cons cell or nil.
269Otherwise, return nil. */)
8c1a1077 270 (object)
39bcc759 271 Lisp_Object object;
7921925c 272{
39bcc759 273 if (CONSP (object) || NILP (object))
7921925c
JB
274 return Qt;
275 return Qnil;
276}
277
25638b07 278DEFUN ("nlistp", Fnlistp, Snlistp, 1, 1, 0,
8c1a1077
PJ
279 doc: /* Return t if OBJECT is not a list. Lists include nil. */)
280 (object)
39bcc759 281 Lisp_Object object;
7921925c 282{
39bcc759 283 if (CONSP (object) || NILP (object))
7921925c
JB
284 return Qnil;
285 return Qt;
286}
287\f
25638b07 288DEFUN ("symbolp", Fsymbolp, Ssymbolp, 1, 1, 0,
8c1a1077
PJ
289 doc: /* Return t if OBJECT is a symbol. */)
290 (object)
39bcc759 291 Lisp_Object object;
7921925c 292{
39bcc759 293 if (SYMBOLP (object))
7921925c
JB
294 return Qt;
295 return Qnil;
296}
297
cda9b832
DL
298/* Define this in C to avoid unnecessarily consing up the symbol
299 name. */
300DEFUN ("keywordp", Fkeywordp, Skeywordp, 1, 1, 0,
8c1a1077
PJ
301 doc: /* Return t if OBJECT is a keyword.
302This means that it is a symbol with a print name beginning with `:'
303interned in the initial obarray. */)
304 (object)
cda9b832
DL
305 Lisp_Object object;
306{
307 if (SYMBOLP (object)
d5db4077 308 && SREF (SYMBOL_NAME (object), 0) == ':'
f35d5bad 309 && SYMBOL_INTERNED_IN_INITIAL_OBARRAY_P (object))
cda9b832
DL
310 return Qt;
311 return Qnil;
312}
313
25638b07 314DEFUN ("vectorp", Fvectorp, Svectorp, 1, 1, 0,
8c1a1077
PJ
315 doc: /* Return t if OBJECT is a vector. */)
316 (object)
39bcc759 317 Lisp_Object object;
7921925c 318{
39bcc759 319 if (VECTORP (object))
7921925c
JB
320 return Qt;
321 return Qnil;
322}
323
25638b07 324DEFUN ("stringp", Fstringp, Sstringp, 1, 1, 0,
8c1a1077
PJ
325 doc: /* Return t if OBJECT is a string. */)
326 (object)
39bcc759 327 Lisp_Object object;
7921925c 328{
39bcc759 329 if (STRINGP (object))
7921925c
JB
330 return Qt;
331 return Qnil;
332}
333
25638b07 334DEFUN ("multibyte-string-p", Fmultibyte_string_p, Smultibyte_string_p,
8c1a1077
PJ
335 1, 1, 0,
336 doc: /* Return t if OBJECT is a multibyte string. */)
337 (object)
25638b07
RS
338 Lisp_Object object;
339{
340 if (STRINGP (object) && STRING_MULTIBYTE (object))
341 return Qt;
342 return Qnil;
343}
344
345DEFUN ("char-table-p", Fchar_table_p, Schar_table_p, 1, 1, 0,
8c1a1077
PJ
346 doc: /* Return t if OBJECT is a char-table. */)
347 (object)
4d276982
RS
348 Lisp_Object object;
349{
350 if (CHAR_TABLE_P (object))
351 return Qt;
352 return Qnil;
353}
354
7f0edce7
RS
355DEFUN ("vector-or-char-table-p", Fvector_or_char_table_p,
356 Svector_or_char_table_p, 1, 1, 0,
8c1a1077
PJ
357 doc: /* Return t if OBJECT is a char-table or vector. */)
358 (object)
7f0edce7
RS
359 Lisp_Object object;
360{
361 if (VECTORP (object) || CHAR_TABLE_P (object))
362 return Qt;
363 return Qnil;
364}
365
8c1a1077
PJ
366DEFUN ("bool-vector-p", Fbool_vector_p, Sbool_vector_p, 1, 1, 0,
367 doc: /* Return t if OBJECT is a bool-vector. */)
368 (object)
4d276982
RS
369 Lisp_Object object;
370{
371 if (BOOL_VECTOR_P (object))
372 return Qt;
373 return Qnil;
374}
375
8c1a1077
PJ
376DEFUN ("arrayp", Farrayp, Sarrayp, 1, 1, 0,
377 doc: /* Return t if OBJECT is an array (string or vector). */)
378 (object)
39bcc759 379 Lisp_Object object;
7921925c 380{
0c64a8cd 381 if (ARRAYP (object))
7921925c
JB
382 return Qt;
383 return Qnil;
384}
385
386DEFUN ("sequencep", Fsequencep, Ssequencep, 1, 1, 0,
8c1a1077
PJ
387 doc: /* Return t if OBJECT is a sequence (list or array). */)
388 (object)
39bcc759 389 register Lisp_Object object;
7921925c 390{
0c64a8cd 391 if (CONSP (object) || NILP (object) || ARRAYP (object))
7921925c
JB
392 return Qt;
393 return Qnil;
394}
395
8c1a1077
PJ
396DEFUN ("bufferp", Fbufferp, Sbufferp, 1, 1, 0,
397 doc: /* Return t if OBJECT is an editor buffer. */)
398 (object)
39bcc759 399 Lisp_Object object;
7921925c 400{
39bcc759 401 if (BUFFERP (object))
7921925c
JB
402 return Qt;
403 return Qnil;
404}
405
8c1a1077
PJ
406DEFUN ("markerp", Fmarkerp, Smarkerp, 1, 1, 0,
407 doc: /* Return t if OBJECT is a marker (editor pointer). */)
408 (object)
39bcc759 409 Lisp_Object object;
7921925c 410{
39bcc759 411 if (MARKERP (object))
7921925c
JB
412 return Qt;
413 return Qnil;
414}
415
8c1a1077
PJ
416DEFUN ("subrp", Fsubrp, Ssubrp, 1, 1, 0,
417 doc: /* Return t if OBJECT is a built-in function. */)
418 (object)
39bcc759 419 Lisp_Object object;
7921925c 420{
39bcc759 421 if (SUBRP (object))
7921925c
JB
422 return Qt;
423 return Qnil;
424}
425
dbc4e1c1 426DEFUN ("byte-code-function-p", Fbyte_code_function_p, Sbyte_code_function_p,
8c1a1077
PJ
427 1, 1, 0,
428 doc: /* Return t if OBJECT is a byte-compiled function object. */)
429 (object)
39bcc759 430 Lisp_Object object;
7921925c 431{
39bcc759 432 if (COMPILEDP (object))
7921925c
JB
433 return Qt;
434 return Qnil;
435}
436
0321d75c 437DEFUN ("char-or-string-p", Fchar_or_string_p, Schar_or_string_p, 1, 1, 0,
8c1a1077
PJ
438 doc: /* Return t if OBJECT is a character (an integer) or a string. */)
439 (object)
39bcc759 440 register Lisp_Object object;
7921925c 441{
39bcc759 442 if (INTEGERP (object) || STRINGP (object))
7921925c
JB
443 return Qt;
444 return Qnil;
445}
446\f
8c1a1077
PJ
447DEFUN ("integerp", Fintegerp, Sintegerp, 1, 1, 0,
448 doc: /* Return t if OBJECT is an integer. */)
449 (object)
39bcc759 450 Lisp_Object object;
7921925c 451{
39bcc759 452 if (INTEGERP (object))
7921925c
JB
453 return Qt;
454 return Qnil;
455}
456
464f8898 457DEFUN ("integer-or-marker-p", Finteger_or_marker_p, Sinteger_or_marker_p, 1, 1, 0,
8c1a1077
PJ
458 doc: /* Return t if OBJECT is an integer or a marker (editor pointer). */)
459 (object)
39bcc759 460 register Lisp_Object object;
7921925c 461{
39bcc759 462 if (MARKERP (object) || INTEGERP (object))
7921925c
JB
463 return Qt;
464 return Qnil;
465}
466
0321d75c 467DEFUN ("natnump", Fnatnump, Snatnump, 1, 1, 0,
8c1a1077
PJ
468 doc: /* Return t if OBJECT is a nonnegative integer. */)
469 (object)
39bcc759 470 Lisp_Object object;
7921925c 471{
39bcc759 472 if (NATNUMP (object))
7921925c
JB
473 return Qt;
474 return Qnil;
475}
476
477DEFUN ("numberp", Fnumberp, Snumberp, 1, 1, 0,
8c1a1077
PJ
478 doc: /* Return t if OBJECT is a number (floating point or integer). */)
479 (object)
39bcc759 480 Lisp_Object object;
7921925c 481{
39bcc759 482 if (NUMBERP (object))
7921925c 483 return Qt;
dbc4e1c1
JB
484 else
485 return Qnil;
7921925c
JB
486}
487
488DEFUN ("number-or-marker-p", Fnumber_or_marker_p,
489 Snumber_or_marker_p, 1, 1, 0,
8c1a1077
PJ
490 doc: /* Return t if OBJECT is a number or a marker. */)
491 (object)
39bcc759 492 Lisp_Object object;
7921925c 493{
39bcc759 494 if (NUMBERP (object) || MARKERP (object))
7921925c
JB
495 return Qt;
496 return Qnil;
497}
464f8898 498
464f8898 499DEFUN ("floatp", Ffloatp, Sfloatp, 1, 1, 0,
8c1a1077
PJ
500 doc: /* Return t if OBJECT is a floating point number. */)
501 (object)
39bcc759 502 Lisp_Object object;
464f8898 503{
39bcc759 504 if (FLOATP (object))
464f8898
RS
505 return Qt;
506 return Qnil;
507}
cc94f3b2 508
7921925c
JB
509\f
510/* Extract and set components of lists */
511
512DEFUN ("car", Fcar, Scar, 1, 1, 0,
8c1a1077 513 doc: /* Return the car of LIST. If arg is nil, return nil.
9701c742
LT
514Error if arg is not nil and not a cons cell. See also `car-safe'.
515
da46c5be
LT
516See Info node `(elisp)Cons Cells' for a discussion of related basic
517Lisp concepts such as car, cdr, cons cell and list. */)
8c1a1077 518 (list)
7921925c
JB
519 register Lisp_Object list;
520{
0c64a8cd 521 return CAR (list);
7921925c
JB
522}
523
524DEFUN ("car-safe", Fcar_safe, Scar_safe, 1, 1, 0,
8c1a1077
PJ
525 doc: /* Return the car of OBJECT if it is a cons cell, or else nil. */)
526 (object)
7921925c
JB
527 Lisp_Object object;
528{
0c64a8cd 529 return CAR_SAFE (object);
7921925c
JB
530}
531
532DEFUN ("cdr", Fcdr, Scdr, 1, 1, 0,
8c1a1077 533 doc: /* Return the cdr of LIST. If arg is nil, return nil.
9701c742
LT
534Error if arg is not nil and not a cons cell. See also `cdr-safe'.
535
da46c5be
LT
536See Info node `(elisp)Cons Cells' for a discussion of related basic
537Lisp concepts such as cdr, car, cons cell and list. */)
8c1a1077 538 (list)
7921925c
JB
539 register Lisp_Object list;
540{
0c64a8cd 541 return CDR (list);
7921925c
JB
542}
543
544DEFUN ("cdr-safe", Fcdr_safe, Scdr_safe, 1, 1, 0,
8c1a1077
PJ
545 doc: /* Return the cdr of OBJECT if it is a cons cell, or else nil. */)
546 (object)
7921925c
JB
547 Lisp_Object object;
548{
0c64a8cd 549 return CDR_SAFE (object);
7921925c
JB
550}
551
552DEFUN ("setcar", Fsetcar, Ssetcar, 2, 2, 0,
8c1a1077
PJ
553 doc: /* Set the car of CELL to be NEWCAR. Returns NEWCAR. */)
554 (cell, newcar)
7921925c
JB
555 register Lisp_Object cell, newcar;
556{
0c64a8cd 557 CHECK_CONS (cell);
7921925c 558 CHECK_IMPURE (cell);
f3fbd155 559 XSETCAR (cell, newcar);
7921925c
JB
560 return newcar;
561}
562
563DEFUN ("setcdr", Fsetcdr, Ssetcdr, 2, 2, 0,
8c1a1077
PJ
564 doc: /* Set the cdr of CELL to be NEWCDR. Returns NEWCDR. */)
565 (cell, newcdr)
7921925c
JB
566 register Lisp_Object cell, newcdr;
567{
0c64a8cd 568 CHECK_CONS (cell);
7921925c 569 CHECK_IMPURE (cell);
f3fbd155 570 XSETCDR (cell, newcdr);
7921925c
JB
571 return newcdr;
572}
573\f
574/* Extract and set components of symbols */
575
8c1a1077
PJ
576DEFUN ("boundp", Fboundp, Sboundp, 1, 1, 0,
577 doc: /* Return t if SYMBOL's value is not void. */)
578 (symbol)
d9c2a0f2 579 register Lisp_Object symbol;
7921925c
JB
580{
581 Lisp_Object valcontents;
b7826503 582 CHECK_SYMBOL (symbol);
7921925c 583
f35d5bad 584 valcontents = SYMBOL_VALUE (symbol);
7921925c 585
aabf6bec
KH
586 if (BUFFER_LOCAL_VALUEP (valcontents)
587 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
d9c2a0f2 588 valcontents = swap_in_symval_forwarding (symbol, valcontents);
7921925c 589
1bfcade3 590 return (EQ (valcontents, Qunbound) ? Qnil : Qt);
7921925c
JB
591}
592
8c1a1077
PJ
593DEFUN ("fboundp", Ffboundp, Sfboundp, 1, 1, 0,
594 doc: /* Return t if SYMBOL's function definition is not void. */)
595 (symbol)
d9c2a0f2 596 register Lisp_Object symbol;
7921925c 597{
b7826503 598 CHECK_SYMBOL (symbol);
d9c2a0f2 599 return (EQ (XSYMBOL (symbol)->function, Qunbound) ? Qnil : Qt);
7921925c
JB
600}
601
8c1a1077 602DEFUN ("makunbound", Fmakunbound, Smakunbound, 1, 1, 0,
bfb96cb7
FP
603 doc: /* Make SYMBOL's value be void.
604Return SYMBOL. */)
8c1a1077 605 (symbol)
d9c2a0f2 606 register Lisp_Object symbol;
7921925c 607{
b7826503 608 CHECK_SYMBOL (symbol);
64ec26d6 609 if (SYMBOL_CONSTANT_P (symbol))
740ef0b5 610 xsignal1 (Qsetting_constant, symbol);
d9c2a0f2
EN
611 Fset (symbol, Qunbound);
612 return symbol;
7921925c
JB
613}
614
8c1a1077 615DEFUN ("fmakunbound", Ffmakunbound, Sfmakunbound, 1, 1, 0,
bfb96cb7
FP
616 doc: /* Make SYMBOL's function definition be void.
617Return SYMBOL. */)
8c1a1077 618 (symbol)
d9c2a0f2 619 register Lisp_Object symbol;
7921925c 620{
b7826503 621 CHECK_SYMBOL (symbol);
d9c2a0f2 622 if (NILP (symbol) || EQ (symbol, Qt))
740ef0b5 623 xsignal1 (Qsetting_constant, symbol);
d9c2a0f2
EN
624 XSYMBOL (symbol)->function = Qunbound;
625 return symbol;
7921925c
JB
626}
627
628DEFUN ("symbol-function", Fsymbol_function, Ssymbol_function, 1, 1, 0,
8c1a1077
PJ
629 doc: /* Return SYMBOL's function definition. Error if that is void. */)
630 (symbol)
ffd56f97 631 register Lisp_Object symbol;
7921925c 632{
b7826503 633 CHECK_SYMBOL (symbol);
740ef0b5
KS
634 if (!EQ (XSYMBOL (symbol)->function, Qunbound))
635 return XSYMBOL (symbol)->function;
636 xsignal1 (Qvoid_function, symbol);
7921925c
JB
637}
638
8c1a1077
PJ
639DEFUN ("symbol-plist", Fsymbol_plist, Ssymbol_plist, 1, 1, 0,
640 doc: /* Return SYMBOL's property list. */)
641 (symbol)
d9c2a0f2 642 register Lisp_Object symbol;
7921925c 643{
b7826503 644 CHECK_SYMBOL (symbol);
d9c2a0f2 645 return XSYMBOL (symbol)->plist;
7921925c
JB
646}
647
8c1a1077
PJ
648DEFUN ("symbol-name", Fsymbol_name, Ssymbol_name, 1, 1, 0,
649 doc: /* Return SYMBOL's name, a string. */)
650 (symbol)
d9c2a0f2 651 register Lisp_Object symbol;
7921925c
JB
652{
653 register Lisp_Object name;
654
b7826503 655 CHECK_SYMBOL (symbol);
84023177 656 name = SYMBOL_NAME (symbol);
7921925c
JB
657 return name;
658}
659
660DEFUN ("fset", Ffset, Sfset, 2, 2, 0,
8c1a1077
PJ
661 doc: /* Set SYMBOL's function definition to DEFINITION, and return DEFINITION. */)
662 (symbol, definition)
8c0b5540 663 register Lisp_Object symbol, definition;
d9c2a0f2 664{
b7826503 665 CHECK_SYMBOL (symbol);
d9c2a0f2 666 if (NILP (symbol) || EQ (symbol, Qt))
740ef0b5 667 xsignal1 (Qsetting_constant, symbol);
d9c2a0f2
EN
668 if (!NILP (Vautoload_queue) && !EQ (XSYMBOL (symbol)->function, Qunbound))
669 Vautoload_queue = Fcons (Fcons (symbol, XSYMBOL (symbol)->function),
7921925c 670 Vautoload_queue);
8c0b5540 671 XSYMBOL (symbol)->function = definition;
f845f2c9 672 /* Handle automatic advice activation */
d9c2a0f2 673 if (CONSP (XSYMBOL (symbol)->plist) && !NILP (Fget (symbol, Qad_advice_info)))
f845f2c9 674 {
c1307a23 675 call2 (Qad_activate_internal, symbol, Qnil);
8c0b5540 676 definition = XSYMBOL (symbol)->function;
f845f2c9 677 }
8c0b5540 678 return definition;
7921925c
JB
679}
680
d2fde41d
SM
681extern Lisp_Object Qfunction_documentation;
682
683DEFUN ("defalias", Fdefalias, Sdefalias, 2, 3, 0,
8c1a1077 684 doc: /* Set SYMBOL's function definition to DEFINITION, and return DEFINITION.
96143227
RS
685Associates the function with the current load file, if any.
686The optional third argument DOCSTRING specifies the documentation string
687for SYMBOL; if it is omitted or nil, SYMBOL uses the documentation string
688determined by DEFINITION. */)
d2fde41d
SM
689 (symbol, definition, docstring)
690 register Lisp_Object symbol, definition, docstring;
fc08c367 691{
8a658a52 692 CHECK_SYMBOL (symbol);
894f6538
RS
693 if (CONSP (XSYMBOL (symbol)->function)
694 && EQ (XCAR (XSYMBOL (symbol)->function), Qautoload))
695 LOADHIST_ATTACH (Fcons (Qt, symbol));
32f532b7 696 definition = Ffset (symbol, definition);
62a29071 697 LOADHIST_ATTACH (Fcons (Qdefun, symbol));
d2fde41d
SM
698 if (!NILP (docstring))
699 Fput (symbol, Qfunction_documentation, docstring);
73e0d965 700 return definition;
fc08c367
RS
701}
702
7921925c 703DEFUN ("setplist", Fsetplist, Ssetplist, 2, 2, 0,
29802b85 704 doc: /* Set SYMBOL's property list to NEWPLIST, and return NEWPLIST. */)
8c1a1077 705 (symbol, newplist)
d9c2a0f2 706 register Lisp_Object symbol, newplist;
7921925c 707{
b7826503 708 CHECK_SYMBOL (symbol);
d9c2a0f2 709 XSYMBOL (symbol)->plist = newplist;
7921925c
JB
710 return newplist;
711}
ffd56f97 712
6f0e897f 713DEFUN ("subr-arity", Fsubr_arity, Ssubr_arity, 1, 1, 0,
8c1a1077
PJ
714 doc: /* Return minimum and maximum number of args allowed for SUBR.
715SUBR must be a built-in function.
716The returned value is a pair (MIN . MAX). MIN is the minimum number
717of args. MAX is the maximum number or the symbol `many', for a
718function with `&rest' args, or `unevalled' for a special form. */)
719 (subr)
6f0e897f
DL
720 Lisp_Object subr;
721{
722 short minargs, maxargs;
0c64a8cd 723 CHECK_SUBR (subr);
6f0e897f
DL
724 minargs = XSUBR (subr)->min_args;
725 maxargs = XSUBR (subr)->max_args;
726 if (maxargs == MANY)
727 return Fcons (make_number (minargs), Qmany);
728 else if (maxargs == UNEVALLED)
729 return Fcons (make_number (minargs), Qunevalled);
730 else
731 return Fcons (make_number (minargs), make_number (maxargs));
732}
733
0fddae66
SM
734DEFUN ("subr-name", Fsubr_name, Ssubr_name, 1, 1, 0,
735 doc: /* Return name of subroutine SUBR.
736SUBR must be a built-in function. */)
737 (subr)
738 Lisp_Object subr;
739{
740 const char *name;
0c64a8cd 741 CHECK_SUBR (subr);
0fddae66
SM
742 name = XSUBR (subr)->symbol_name;
743 return make_string (name, strlen (name));
744}
745
f52a3ca3
SM
746DEFUN ("interactive-form", Finteractive_form, Sinteractive_form, 1, 1, 0,
747 doc: /* Return the interactive form of CMD or nil if none.
df133612
LT
748If CMD is not a command, the return value is nil.
749Value, if non-nil, is a list \(interactive SPEC). */)
f52a3ca3
SM
750 (cmd)
751 Lisp_Object cmd;
cc515226 752{
f52a3ca3
SM
753 Lisp_Object fun = indirect_function (cmd);
754
755 if (SUBRP (fun))
756 {
757 if (XSUBR (fun)->prompt)
758 return list2 (Qinteractive, build_string (XSUBR (fun)->prompt));
759 }
760 else if (COMPILEDP (fun))
761 {
762 if ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE)
763 return list2 (Qinteractive, AREF (fun, COMPILED_INTERACTIVE));
764 }
765 else if (CONSP (fun))
766 {
767 Lisp_Object funcar = XCAR (fun);
768 if (EQ (funcar, Qlambda))
769 return Fassq (Qinteractive, Fcdr (XCDR (fun)));
770 else if (EQ (funcar, Qautoload))
771 {
772 struct gcpro gcpro1;
773 GCPRO1 (cmd);
774 do_autoload (fun, cmd);
775 UNGCPRO;
776 return Finteractive_form (cmd);
777 }
778 }
cc515226
GM
779 return Qnil;
780}
781
7921925c 782\f
f35d5bad
GM
783/***********************************************************************
784 Getting and Setting Values of Symbols
785 ***********************************************************************/
786
787/* Return the symbol holding SYMBOL's value. Signal
788 `cyclic-variable-indirection' if SYMBOL's chain of variable
789 indirections contains a loop. */
790
791Lisp_Object
792indirect_variable (symbol)
793 Lisp_Object symbol;
794{
795 Lisp_Object tortoise, hare;
796
797 hare = tortoise = symbol;
798
799 while (XSYMBOL (hare)->indirect_variable)
800 {
801 hare = XSYMBOL (hare)->value;
802 if (!XSYMBOL (hare)->indirect_variable)
803 break;
bfb96cb7 804
f35d5bad
GM
805 hare = XSYMBOL (hare)->value;
806 tortoise = XSYMBOL (tortoise)->value;
807
808 if (EQ (hare, tortoise))
740ef0b5 809 xsignal1 (Qcyclic_variable_indirection, symbol);
f35d5bad
GM
810 }
811
812 return hare;
813}
814
815
816DEFUN ("indirect-variable", Findirect_variable, Sindirect_variable, 1, 1, 0,
8c1a1077
PJ
817 doc: /* Return the variable at the end of OBJECT's variable chain.
818If OBJECT is a symbol, follow all variable indirections and return the final
819variable. If OBJECT is not a symbol, just return it.
820Signal a cyclic-variable-indirection error if there is a loop in the
821variable chain of symbols. */)
822 (object)
f35d5bad
GM
823 Lisp_Object object;
824{
825 if (SYMBOLP (object))
826 object = indirect_variable (object);
827 return object;
828}
829
7921925c
JB
830
831/* Given the raw contents of a symbol value cell,
832 return the Lisp value of the symbol.
833 This does not handle buffer-local variables; use
834 swap_in_symval_forwarding for that. */
835
836Lisp_Object
837do_symval_forwarding (valcontents)
838 register Lisp_Object valcontents;
839{
840 register Lisp_Object val;
46b2ac21
KH
841 int offset;
842 if (MISCP (valcontents))
324a6eef 843 switch (XMISCTYPE (valcontents))
46b2ac21
KH
844 {
845 case Lisp_Misc_Intfwd:
846 XSETINT (val, *XINTFWD (valcontents)->intvar);
847 return val;
7921925c 848
46b2ac21
KH
849 case Lisp_Misc_Boolfwd:
850 return (*XBOOLFWD (valcontents)->boolvar ? Qt : Qnil);
7921925c 851
46b2ac21
KH
852 case Lisp_Misc_Objfwd:
853 return *XOBJFWD (valcontents)->objvar;
7921925c 854
46b2ac21
KH
855 case Lisp_Misc_Buffer_Objfwd:
856 offset = XBUFFER_OBJFWD (valcontents)->offset;
f6cd0527 857 return PER_BUFFER_VALUE (current_buffer, offset);
7403b5c8 858
e5f8af9e
KH
859 case Lisp_Misc_Kboard_Objfwd:
860 offset = XKBOARD_OBJFWD (valcontents)->offset;
861 return *(Lisp_Object *)(offset + (char *)current_kboard);
46b2ac21 862 }
7921925c
JB
863 return valcontents;
864}
865
d9c2a0f2
EN
866/* Store NEWVAL into SYMBOL, where VALCONTENTS is found in the value cell
867 of SYMBOL. If SYMBOL is buffer-local, VALCONTENTS should be the
7921925c 868 buffer-independent contents of the value cell: forwarded just one
7a283f36
GM
869 step past the buffer-localness.
870
871 BUF non-zero means set the value in buffer BUF instead of the
872 current buffer. This only plays a role for per-buffer variables. */
7921925c
JB
873
874void
7a283f36 875store_symval_forwarding (symbol, valcontents, newval, buf)
d9c2a0f2 876 Lisp_Object symbol;
7921925c 877 register Lisp_Object valcontents, newval;
7a283f36 878 struct buffer *buf;
7921925c 879{
0220c518 880 switch (SWITCH_ENUM_CAST (XTYPE (valcontents)))
7921925c 881 {
46b2ac21 882 case Lisp_Misc:
324a6eef 883 switch (XMISCTYPE (valcontents))
46b2ac21
KH
884 {
885 case Lisp_Misc_Intfwd:
b7826503 886 CHECK_NUMBER (newval);
46b2ac21 887 *XINTFWD (valcontents)->intvar = XINT (newval);
e6c82a8d
RS
888 if (*XINTFWD (valcontents)->intvar != XINT (newval))
889 error ("Value out of range for variable `%s'",
d5db4077 890 SDATA (SYMBOL_NAME (symbol)));
46b2ac21
KH
891 break;
892
893 case Lisp_Misc_Boolfwd:
894 *XBOOLFWD (valcontents)->boolvar = NILP (newval) ? 0 : 1;
895 break;
896
897 case Lisp_Misc_Objfwd:
898 *XOBJFWD (valcontents)->objvar = newval;
7abd90ea
RS
899
900 /* If this variable is a default for something stored
901 in the buffer itself, such as default-fill-column,
902 find the buffers that don't have local values for it
903 and update them. */
904 if (XOBJFWD (valcontents)->objvar > (Lisp_Object *) &buffer_defaults
905 && XOBJFWD (valcontents)->objvar < (Lisp_Object *) (&buffer_defaults + 1))
906 {
907 int offset = ((char *) XOBJFWD (valcontents)->objvar
908 - (char *) &buffer_defaults);
909 int idx = PER_BUFFER_IDX (offset);
910
43e50e40 911 Lisp_Object tail;
7abd90ea
RS
912
913 if (idx <= 0)
914 break;
915
916 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
917 {
918 Lisp_Object buf;
919 struct buffer *b;
920
921 buf = Fcdr (XCAR (tail));
922 if (!BUFFERP (buf)) continue;
923 b = XBUFFER (buf);
924
925 if (! PER_BUFFER_VALUE_P (b, idx))
926 PER_BUFFER_VALUE (b, offset) = newval;
927 }
928 }
46b2ac21
KH
929 break;
930
931 case Lisp_Misc_Buffer_Objfwd:
932 {
933 int offset = XBUFFER_OBJFWD (valcontents)->offset;
934 Lisp_Object type;
935
d9b36d19 936 type = PER_BUFFER_TYPE (offset);
46b2ac21
KH
937 if (! NILP (type) && ! NILP (newval)
938 && XTYPE (newval) != XINT (type))
939 buffer_slot_type_mismatch (offset);
940
7a283f36
GM
941 if (buf == NULL)
942 buf = current_buffer;
943 PER_BUFFER_VALUE (buf, offset) = newval;
46b2ac21 944 }
7403b5c8
KH
945 break;
946
e5f8af9e 947 case Lisp_Misc_Kboard_Objfwd:
7a283f36
GM
948 {
949 char *base = (char *) current_kboard;
950 char *p = base + XKBOARD_OBJFWD (valcontents)->offset;
951 *(Lisp_Object *) p = newval;
952 }
7403b5c8
KH
953 break;
954
46b2ac21
KH
955 default:
956 goto def;
957 }
7921925c
JB
958 break;
959
7921925c 960 default:
46b2ac21 961 def:
f35d5bad 962 valcontents = SYMBOL_VALUE (symbol);
e9ebc175
KH
963 if (BUFFER_LOCAL_VALUEP (valcontents)
964 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
b0c2d1c6 965 XBUFFER_LOCAL_VALUE (valcontents)->realvalue = newval;
7921925c 966 else
f35d5bad 967 SET_SYMBOL_VALUE (symbol, newval);
7921925c
JB
968 }
969}
970
b0d53add
GM
971/* Set up SYMBOL to refer to its global binding.
972 This makes it safe to alter the status of other bindings. */
973
974void
975swap_in_global_binding (symbol)
976 Lisp_Object symbol;
977{
978 Lisp_Object valcontents, cdr;
bfb96cb7 979
f35d5bad 980 valcontents = SYMBOL_VALUE (symbol);
b0d53add
GM
981 if (!BUFFER_LOCAL_VALUEP (valcontents)
982 && !SOME_BUFFER_LOCAL_VALUEP (valcontents))
983 abort ();
984 cdr = XBUFFER_LOCAL_VALUE (valcontents)->cdr;
985
986 /* Unload the previously loaded binding. */
987 Fsetcdr (XCAR (cdr),
988 do_symval_forwarding (XBUFFER_LOCAL_VALUE (valcontents)->realvalue));
bfb96cb7 989
b0d53add 990 /* Select the global binding in the symbol. */
f3fbd155 991 XSETCAR (cdr, cdr);
7a283f36 992 store_symval_forwarding (symbol, valcontents, XCDR (cdr), NULL);
b0d53add
GM
993
994 /* Indicate that the global binding is set up now. */
995 XBUFFER_LOCAL_VALUE (valcontents)->frame = Qnil;
996 XBUFFER_LOCAL_VALUE (valcontents)->buffer = Qnil;
997 XBUFFER_LOCAL_VALUE (valcontents)->found_for_frame = 0;
998 XBUFFER_LOCAL_VALUE (valcontents)->found_for_buffer = 0;
999}
1000
2829d05f 1001/* Set up the buffer-local symbol SYMBOL for validity in the current buffer.
42e975f0
RS
1002 VALCONTENTS is the contents of its value cell,
1003 which points to a struct Lisp_Buffer_Local_Value.
1004
1005 Return the value forwarded one step past the buffer-local stage.
1006 This could be another forwarding pointer. */
7921925c
JB
1007
1008static Lisp_Object
d9c2a0f2
EN
1009swap_in_symval_forwarding (symbol, valcontents)
1010 Lisp_Object symbol, valcontents;
7921925c 1011{
7921925c 1012 register Lisp_Object tem1;
bfb96cb7 1013
b0c2d1c6 1014 tem1 = XBUFFER_LOCAL_VALUE (valcontents)->buffer;
7921925c 1015
42e975f0
RS
1016 if (NILP (tem1)
1017 || current_buffer != XBUFFER (tem1)
1018 || (XBUFFER_LOCAL_VALUE (valcontents)->check_frame
1019 && ! EQ (selected_frame, XBUFFER_LOCAL_VALUE (valcontents)->frame)))
7921925c 1020 {
f35d5bad
GM
1021 if (XSYMBOL (symbol)->indirect_variable)
1022 symbol = indirect_variable (symbol);
bfb96cb7 1023
42e975f0 1024 /* Unload the previously loaded binding. */
7539e11f 1025 tem1 = XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
8d4afcac 1026 Fsetcdr (tem1,
b0c2d1c6 1027 do_symval_forwarding (XBUFFER_LOCAL_VALUE (valcontents)->realvalue));
42e975f0 1028 /* Choose the new binding. */
d9c2a0f2 1029 tem1 = assq_no_quit (symbol, current_buffer->local_var_alist);
b0c2d1c6
RS
1030 XBUFFER_LOCAL_VALUE (valcontents)->found_for_frame = 0;
1031 XBUFFER_LOCAL_VALUE (valcontents)->found_for_buffer = 0;
a33ef3ab 1032 if (NILP (tem1))
b0c2d1c6
RS
1033 {
1034 if (XBUFFER_LOCAL_VALUE (valcontents)->check_frame)
1cc04aed 1035 tem1 = assq_no_quit (symbol, XFRAME (selected_frame)->param_alist);
b0c2d1c6
RS
1036 if (! NILP (tem1))
1037 XBUFFER_LOCAL_VALUE (valcontents)->found_for_frame = 1;
1038 else
1039 tem1 = XBUFFER_LOCAL_VALUE (valcontents)->cdr;
1040 }
1041 else
1042 XBUFFER_LOCAL_VALUE (valcontents)->found_for_buffer = 1;
1043
42e975f0 1044 /* Load the new binding. */
f3fbd155 1045 XSETCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr, tem1);
b0c2d1c6 1046 XSETBUFFER (XBUFFER_LOCAL_VALUE (valcontents)->buffer, current_buffer);
1cc04aed 1047 XBUFFER_LOCAL_VALUE (valcontents)->frame = selected_frame;
b0c2d1c6
RS
1048 store_symval_forwarding (symbol,
1049 XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
7a283f36 1050 Fcdr (tem1), NULL);
7921925c 1051 }
b0c2d1c6 1052 return XBUFFER_LOCAL_VALUE (valcontents)->realvalue;
7921925c
JB
1053}
1054\f
14e76af9
JB
1055/* Find the value of a symbol, returning Qunbound if it's not bound.
1056 This is helpful for code which just wants to get a variable's value
8e6208c5 1057 if it has one, without signaling an error.
14e76af9
JB
1058 Note that it must not be possible to quit
1059 within this function. Great care is required for this. */
7921925c 1060
14e76af9 1061Lisp_Object
d9c2a0f2
EN
1062find_symbol_value (symbol)
1063 Lisp_Object symbol;
7921925c 1064{
dd8daec5 1065 register Lisp_Object valcontents;
7921925c 1066 register Lisp_Object val;
bfb96cb7 1067
b7826503 1068 CHECK_SYMBOL (symbol);
f35d5bad 1069 valcontents = SYMBOL_VALUE (symbol);
7921925c 1070
aabf6bec
KH
1071 if (BUFFER_LOCAL_VALUEP (valcontents)
1072 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
2a359158 1073 valcontents = swap_in_symval_forwarding (symbol, valcontents);
7921925c 1074
536b772a
KH
1075 if (MISCP (valcontents))
1076 {
324a6eef 1077 switch (XMISCTYPE (valcontents))
46b2ac21
KH
1078 {
1079 case Lisp_Misc_Intfwd:
1080 XSETINT (val, *XINTFWD (valcontents)->intvar);
1081 return val;
7921925c 1082
46b2ac21
KH
1083 case Lisp_Misc_Boolfwd:
1084 return (*XBOOLFWD (valcontents)->boolvar ? Qt : Qnil);
7921925c 1085
46b2ac21
KH
1086 case Lisp_Misc_Objfwd:
1087 return *XOBJFWD (valcontents)->objvar;
7921925c 1088
46b2ac21 1089 case Lisp_Misc_Buffer_Objfwd:
f6cd0527 1090 return PER_BUFFER_VALUE (current_buffer,
999b32fd 1091 XBUFFER_OBJFWD (valcontents)->offset);
7403b5c8 1092
e5f8af9e
KH
1093 case Lisp_Misc_Kboard_Objfwd:
1094 return *(Lisp_Object *)(XKBOARD_OBJFWD (valcontents)->offset
1095 + (char *)current_kboard);
46b2ac21 1096 }
7921925c
JB
1097 }
1098
1099 return valcontents;
1100}
1101
14e76af9 1102DEFUN ("symbol-value", Fsymbol_value, Ssymbol_value, 1, 1, 0,
8c1a1077
PJ
1103 doc: /* Return SYMBOL's value. Error if that is void. */)
1104 (symbol)
d9c2a0f2 1105 Lisp_Object symbol;
14e76af9 1106{
0671d7c0 1107 Lisp_Object val;
14e76af9 1108
d9c2a0f2 1109 val = find_symbol_value (symbol);
740ef0b5 1110 if (!EQ (val, Qunbound))
14e76af9 1111 return val;
740ef0b5
KS
1112
1113 xsignal1 (Qvoid_variable, symbol);
14e76af9
JB
1114}
1115
7921925c 1116DEFUN ("set", Fset, Sset, 2, 2, 0,
8c1a1077
PJ
1117 doc: /* Set SYMBOL's value to NEWVAL, and return NEWVAL. */)
1118 (symbol, newval)
d9c2a0f2 1119 register Lisp_Object symbol, newval;
05ef7169 1120{
2829d05f 1121 return set_internal (symbol, newval, current_buffer, 0);
05ef7169
RS
1122}
1123
1f35ce36
RS
1124/* Return 1 if SYMBOL currently has a let-binding
1125 which was made in the buffer that is now current. */
1126
1127static int
1128let_shadows_buffer_binding_p (symbol)
1129 Lisp_Object symbol;
1130{
92426155 1131 volatile struct specbinding *p;
1f35ce36
RS
1132
1133 for (p = specpdl_ptr - 1; p >= specpdl; p--)
f35d5bad
GM
1134 if (p->func == NULL
1135 && CONSP (p->symbol))
1136 {
1137 Lisp_Object let_bound_symbol = XCAR (p->symbol);
1138 if ((EQ (symbol, let_bound_symbol)
1139 || (XSYMBOL (let_bound_symbol)->indirect_variable
1140 && EQ (symbol, indirect_variable (let_bound_symbol))))
1141 && XBUFFER (XCDR (XCDR (p->symbol))) == current_buffer)
1142 break;
1143 }
1f35ce36 1144
f35d5bad 1145 return p >= specpdl;
1f35ce36
RS
1146}
1147
25638b07 1148/* Store the value NEWVAL into SYMBOL.
2829d05f
RS
1149 If buffer-locality is an issue, BUF specifies which buffer to use.
1150 (0 stands for the current buffer.)
1151
05ef7169
RS
1152 If BINDFLAG is zero, then if this symbol is supposed to become
1153 local in every buffer where it is set, then we make it local.
1154 If BINDFLAG is nonzero, we don't do that. */
1155
1156Lisp_Object
2829d05f 1157set_internal (symbol, newval, buf, bindflag)
05ef7169 1158 register Lisp_Object symbol, newval;
2829d05f 1159 struct buffer *buf;
05ef7169 1160 int bindflag;
7921925c 1161{
1bfcade3 1162 int voide = EQ (newval, Qunbound);
7921925c 1163
4c47c973 1164 register Lisp_Object valcontents, innercontents, tem1, current_alist_element;
7921925c 1165
2829d05f
RS
1166 if (buf == 0)
1167 buf = current_buffer;
1168
1169 /* If restoring in a dead buffer, do nothing. */
1170 if (NILP (buf->name))
1171 return newval;
1172
b7826503 1173 CHECK_SYMBOL (symbol);
f35d5bad
GM
1174 if (SYMBOL_CONSTANT_P (symbol)
1175 && (NILP (Fkeywordp (symbol))
1176 || !EQ (newval, SYMBOL_VALUE (symbol))))
740ef0b5 1177 xsignal1 (Qsetting_constant, symbol);
4c47c973 1178
f35d5bad 1179 innercontents = valcontents = SYMBOL_VALUE (symbol);
bfb96cb7 1180
e9ebc175 1181 if (BUFFER_OBJFWDP (valcontents))
7921925c 1182 {
999b32fd 1183 int offset = XBUFFER_OBJFWD (valcontents)->offset;
f6cd0527 1184 int idx = PER_BUFFER_IDX (offset);
999b32fd
GM
1185 if (idx > 0
1186 && !bindflag
1187 && !let_shadows_buffer_binding_p (symbol))
f6cd0527 1188 SET_PER_BUFFER_VALUE_P (buf, idx, 1);
7921925c 1189 }
e9ebc175
KH
1190 else if (BUFFER_LOCAL_VALUEP (valcontents)
1191 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
7921925c 1192 {
42e975f0 1193 /* valcontents is a struct Lisp_Buffer_Local_Value. */
f35d5bad
GM
1194 if (XSYMBOL (symbol)->indirect_variable)
1195 symbol = indirect_variable (symbol);
42e975f0
RS
1196
1197 /* What binding is loaded right now? */
b0c2d1c6 1198 current_alist_element
7539e11f 1199 = XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
d8cafeb5
JB
1200
1201 /* If the current buffer is not the buffer whose binding is
42e975f0
RS
1202 loaded, or if there may be frame-local bindings and the frame
1203 isn't the right one, or if it's a Lisp_Buffer_Local_Value and
1204 the default binding is loaded, the loaded binding may be the
1205 wrong one. */
8801a864
KR
1206 if (!BUFFERP (XBUFFER_LOCAL_VALUE (valcontents)->buffer)
1207 || buf != XBUFFER (XBUFFER_LOCAL_VALUE (valcontents)->buffer)
42e975f0
RS
1208 || (XBUFFER_LOCAL_VALUE (valcontents)->check_frame
1209 && !EQ (selected_frame, XBUFFER_LOCAL_VALUE (valcontents)->frame))
1210 || (BUFFER_LOCAL_VALUEP (valcontents)
1211 && EQ (XCAR (current_alist_element),
1212 current_alist_element)))
7921925c 1213 {
42e975f0
RS
1214 /* The currently loaded binding is not necessarily valid.
1215 We need to unload it, and choose a new binding. */
1216
1217 /* Write out `realvalue' to the old loaded binding. */
d8cafeb5 1218 Fsetcdr (current_alist_element,
b0c2d1c6 1219 do_symval_forwarding (XBUFFER_LOCAL_VALUE (valcontents)->realvalue));
7921925c 1220
42e975f0 1221 /* Find the new binding. */
2829d05f 1222 tem1 = Fassq (symbol, buf->local_var_alist);
b0c2d1c6
RS
1223 XBUFFER_LOCAL_VALUE (valcontents)->found_for_buffer = 1;
1224 XBUFFER_LOCAL_VALUE (valcontents)->found_for_frame = 0;
1225
a33ef3ab 1226 if (NILP (tem1))
d8cafeb5
JB
1227 {
1228 /* This buffer still sees the default value. */
1229
1230 /* If the variable is a Lisp_Some_Buffer_Local_Value,
05ef7169 1231 or if this is `let' rather than `set',
d8cafeb5 1232 make CURRENT-ALIST-ELEMENT point to itself,
1f35ce36
RS
1233 indicating that we're seeing the default value.
1234 Likewise if the variable has been let-bound
1235 in the current buffer. */
1236 if (bindflag || SOME_BUFFER_LOCAL_VALUEP (valcontents)
1237 || let_shadows_buffer_binding_p (symbol))
b0c2d1c6
RS
1238 {
1239 XBUFFER_LOCAL_VALUE (valcontents)->found_for_buffer = 0;
1240
1241 if (XBUFFER_LOCAL_VALUE (valcontents)->check_frame)
1cc04aed
GM
1242 tem1 = Fassq (symbol,
1243 XFRAME (selected_frame)->param_alist);
d8cafeb5 1244
b0c2d1c6
RS
1245 if (! NILP (tem1))
1246 XBUFFER_LOCAL_VALUE (valcontents)->found_for_frame = 1;
1247 else
1248 tem1 = XBUFFER_LOCAL_VALUE (valcontents)->cdr;
1249 }
05ef7169 1250 /* If it's a Lisp_Buffer_Local_Value, being set not bound,
1f35ce36
RS
1251 and we're not within a let that was made for this buffer,
1252 create a new buffer-local binding for the variable.
1253 That means, give this buffer a new assoc for a local value
42e975f0 1254 and load that binding. */
d8cafeb5
JB
1255 else
1256 {
d2fde41d 1257 tem1 = Fcons (symbol, XCDR (current_alist_element));
2829d05f
RS
1258 buf->local_var_alist
1259 = Fcons (tem1, buf->local_var_alist);
d8cafeb5
JB
1260 }
1261 }
b0c2d1c6 1262
42e975f0 1263 /* Record which binding is now loaded. */
f3fbd155
KR
1264 XSETCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr,
1265 tem1);
d8cafeb5 1266
8013296c 1267 /* Set `buffer' and `frame' slots for the binding now loaded. */
2829d05f 1268 XSETBUFFER (XBUFFER_LOCAL_VALUE (valcontents)->buffer, buf);
1cc04aed 1269 XBUFFER_LOCAL_VALUE (valcontents)->frame = selected_frame;
7921925c 1270 }
4c47c973 1271 innercontents = XBUFFER_LOCAL_VALUE (valcontents)->realvalue;
7921925c 1272 }
d8cafeb5 1273
7921925c
JB
1274 /* If storing void (making the symbol void), forward only through
1275 buffer-local indicator, not through Lisp_Objfwd, etc. */
1276 if (voide)
7a283f36 1277 store_symval_forwarding (symbol, Qnil, newval, buf);
7921925c 1278 else
7a283f36 1279 store_symval_forwarding (symbol, innercontents, newval, buf);
4c47c973
GM
1280
1281 /* If we just set a variable whose current binding is frame-local,
1282 store the new value in the frame parameter too. */
1283
1284 if (BUFFER_LOCAL_VALUEP (valcontents)
1285 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
1286 {
1287 /* What binding is loaded right now? */
1288 current_alist_element
1289 = XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
1290
1291 /* If the current buffer is not the buffer whose binding is
1292 loaded, or if there may be frame-local bindings and the frame
1293 isn't the right one, or if it's a Lisp_Buffer_Local_Value and
1294 the default binding is loaded, the loaded binding may be the
1295 wrong one. */
1296 if (XBUFFER_LOCAL_VALUE (valcontents)->found_for_frame)
f3fbd155 1297 XSETCDR (current_alist_element, newval);
4c47c973 1298 }
d8cafeb5 1299
7921925c
JB
1300 return newval;
1301}
1302\f
1303/* Access or set a buffer-local symbol's default value. */
1304
d9c2a0f2 1305/* Return the default value of SYMBOL, but don't check for voidness.
1bfcade3 1306 Return Qunbound if it is void. */
7921925c
JB
1307
1308Lisp_Object
d9c2a0f2
EN
1309default_value (symbol)
1310 Lisp_Object symbol;
7921925c
JB
1311{
1312 register Lisp_Object valcontents;
1313
b7826503 1314 CHECK_SYMBOL (symbol);
f35d5bad 1315 valcontents = SYMBOL_VALUE (symbol);
7921925c
JB
1316
1317 /* For a built-in buffer-local variable, get the default value
1318 rather than letting do_symval_forwarding get the current value. */
e9ebc175 1319 if (BUFFER_OBJFWDP (valcontents))
7921925c 1320 {
999b32fd 1321 int offset = XBUFFER_OBJFWD (valcontents)->offset;
f6cd0527
GM
1322 if (PER_BUFFER_IDX (offset) != 0)
1323 return PER_BUFFER_DEFAULT (offset);
7921925c
JB
1324 }
1325
1326 /* Handle user-created local variables. */
e9ebc175
KH
1327 if (BUFFER_LOCAL_VALUEP (valcontents)
1328 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
7921925c
JB
1329 {
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.
42e975f0 1332 But the `realvalue' slot may be more up to date, since
7921925c
JB
1333 ordinary setq stores just that slot. So use that. */
1334 Lisp_Object current_alist_element, alist_element_car;
1335 current_alist_element
7539e11f
KR
1336 = XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
1337 alist_element_car = XCAR (current_alist_element);
7921925c 1338 if (EQ (alist_element_car, current_alist_element))
b0c2d1c6 1339 return do_symval_forwarding (XBUFFER_LOCAL_VALUE (valcontents)->realvalue);
7921925c 1340 else
7539e11f 1341 return XCDR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
7921925c
JB
1342 }
1343 /* For other variables, get the current value. */
1344 return do_symval_forwarding (valcontents);
1345}
1346
1347DEFUN ("default-boundp", Fdefault_boundp, Sdefault_boundp, 1, 1, 0,
8c1a1077
PJ
1348 doc: /* Return t if SYMBOL has a non-void default value.
1349This is the value that is seen in buffers that do not have their own values
1350for this variable. */)
1351 (symbol)
d9c2a0f2 1352 Lisp_Object symbol;
7921925c
JB
1353{
1354 register Lisp_Object value;
1355
d9c2a0f2 1356 value = default_value (symbol);
1bfcade3 1357 return (EQ (value, Qunbound) ? Qnil : Qt);
7921925c
JB
1358}
1359
1360DEFUN ("default-value", Fdefault_value, Sdefault_value, 1, 1, 0,
8c1a1077
PJ
1361 doc: /* Return SYMBOL's default value.
1362This is the value that is seen in buffers that do not have their own values
1363for this variable. The default value is meaningful for variables with
1364local bindings in certain buffers. */)
1365 (symbol)
d9c2a0f2 1366 Lisp_Object symbol;
7921925c
JB
1367{
1368 register Lisp_Object value;
1369
d9c2a0f2 1370 value = default_value (symbol);
740ef0b5
KS
1371 if (!EQ (value, Qunbound))
1372 return value;
1373
1374 xsignal1 (Qvoid_variable, symbol);
7921925c
JB
1375}
1376
1377DEFUN ("set-default", Fset_default, Sset_default, 2, 2, 0,
6e86a75d 1378 doc: /* Set SYMBOL's default value to VALUE. SYMBOL and VALUE are evaluated.
8c1a1077
PJ
1379The default value is seen in buffers that do not have their own values
1380for this variable. */)
1381 (symbol, value)
d9c2a0f2 1382 Lisp_Object symbol, value;
7921925c
JB
1383{
1384 register Lisp_Object valcontents, current_alist_element, alist_element_buffer;
1385
b7826503 1386 CHECK_SYMBOL (symbol);
f35d5bad 1387 valcontents = SYMBOL_VALUE (symbol);
7921925c
JB
1388
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. */
e9ebc175 1392 if (BUFFER_OBJFWDP (valcontents))
7921925c 1393 {
999b32fd 1394 int offset = XBUFFER_OBJFWD (valcontents)->offset;
f6cd0527 1395 int idx = PER_BUFFER_IDX (offset);
7921925c 1396
f6cd0527 1397 PER_BUFFER_DEFAULT (offset) = value;
984ef137
KH
1398
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. */
999b32fd 1401 if (idx > 0)
7921925c 1402 {
999b32fd 1403 struct buffer *b;
bfb96cb7 1404
7921925c 1405 for (b = all_buffers; b; b = b->next)
f6cd0527
GM
1406 if (!PER_BUFFER_VALUE_P (b, idx))
1407 PER_BUFFER_VALUE (b, offset) = value;
7921925c
JB
1408 }
1409 return value;
1410 }
1411
e9ebc175
KH
1412 if (!BUFFER_LOCAL_VALUEP (valcontents)
1413 && !SOME_BUFFER_LOCAL_VALUEP (valcontents))
d9c2a0f2 1414 return Fset (symbol, value);
7921925c 1415
42e975f0 1416 /* Store new value into the DEFAULT-VALUE slot. */
f3fbd155 1417 XSETCDR (XBUFFER_LOCAL_VALUE (valcontents)->cdr, value);
7921925c 1418
42e975f0 1419 /* If the default binding is now loaded, set the REALVALUE slot too. */
8d4afcac 1420 current_alist_element
7539e11f 1421 = XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
7921925c
JB
1422 alist_element_buffer = Fcar (current_alist_element);
1423 if (EQ (alist_element_buffer, current_alist_element))
7a283f36
GM
1424 store_symval_forwarding (symbol,
1425 XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1426 value, NULL);
7921925c
JB
1427
1428 return value;
1429}
1430
7a7df7ac 1431DEFUN ("setq-default", Fsetq_default, Ssetq_default, 0, UNEVALLED, 0,
8c1a1077
PJ
1432 doc: /* Set the default value of variable VAR to VALUE.
1433VAR, the variable name, is literal (not evaluated);
bfb96cb7 1434VALUE is an expression: it is evaluated and its value returned.
8c1a1077
PJ
1435The default value of a variable is seen in buffers
1436that do not have their own values for the variable.
1437
1438More generally, you can use multiple variables and values, as in
948d2995
JB
1439 (setq-default VAR VALUE VAR VALUE...)
1440This sets each VAR's default value to the corresponding VALUE.
1441The VALUE for the Nth VAR can refer to the new default values
1442of previous VARs.
70e9f399 1443usage: (setq-default [VAR VALUE]...) */)
8c1a1077 1444 (args)
7921925c
JB
1445 Lisp_Object args;
1446{
1447 register Lisp_Object args_left;
d9c2a0f2 1448 register Lisp_Object val, symbol;
7921925c
JB
1449 struct gcpro gcpro1;
1450
a33ef3ab 1451 if (NILP (args))
7921925c
JB
1452 return Qnil;
1453
1454 args_left = args;
1455 GCPRO1 (args);
1456
1457 do
1458 {
1459 val = Feval (Fcar (Fcdr (args_left)));
d2fde41d 1460 symbol = XCAR (args_left);
d9c2a0f2 1461 Fset_default (symbol, val);
d2fde41d 1462 args_left = Fcdr (XCDR (args_left));
7921925c 1463 }
a33ef3ab 1464 while (!NILP (args_left));
7921925c
JB
1465
1466 UNGCPRO;
1467 return val;
1468}
1469\f
a5ca2b75
JB
1470/* Lisp functions for creating and removing buffer-local variables. */
1471
7921925c 1472DEFUN ("make-variable-buffer-local", Fmake_variable_buffer_local, Smake_variable_buffer_local,
8c1a1077
PJ
1473 1, 1, "vMake Variable Buffer Local: ",
1474 doc: /* Make VARIABLE become buffer-local whenever it is set.
1475At any time, the value for the current buffer is in effect,
1476unless the variable has never been set in this buffer,
1477in which case the default value is in effect.
1478Note that binding the variable with `let', or setting it while
1479a `let'-style binding made in this buffer is in effect,
bfb96cb7 1480does not make the variable buffer-local. Return VARIABLE.
8c1a1077 1481
a9908653
RS
1482In most cases it is better to use `make-local-variable',
1483which makes a variable local in just one buffer.
1484
8c1a1077
PJ
1485The function `default-value' gets the default value and `set-default' sets it. */)
1486 (variable)
d9c2a0f2 1487 register Lisp_Object variable;
7921925c 1488{
8d4afcac 1489 register Lisp_Object tem, valcontents, newval;
7921925c 1490
b7826503 1491 CHECK_SYMBOL (variable);
14970b45 1492 variable = indirect_variable (variable);
7921925c 1493
f35d5bad 1494 valcontents = SYMBOL_VALUE (variable);
d9c2a0f2 1495 if (EQ (variable, Qnil) || EQ (variable, Qt) || KBOARD_OBJFWDP (valcontents))
d5db4077 1496 error ("Symbol %s may not be buffer-local", SDATA (SYMBOL_NAME (variable)));
7921925c 1497
e9ebc175 1498 if (BUFFER_LOCAL_VALUEP (valcontents) || BUFFER_OBJFWDP (valcontents))
d9c2a0f2 1499 return variable;
e9ebc175 1500 if (SOME_BUFFER_LOCAL_VALUEP (valcontents))
7921925c 1501 {
f35d5bad 1502 XMISCTYPE (SYMBOL_VALUE (variable)) = Lisp_Misc_Buffer_Local_Value;
d9c2a0f2 1503 return variable;
7921925c
JB
1504 }
1505 if (EQ (valcontents, Qunbound))
f35d5bad 1506 SET_SYMBOL_VALUE (variable, Qnil);
d9c2a0f2 1507 tem = Fcons (Qnil, Fsymbol_value (variable));
f3fbd155 1508 XSETCAR (tem, tem);
8d4afcac 1509 newval = allocate_misc ();
324a6eef 1510 XMISCTYPE (newval) = Lisp_Misc_Buffer_Local_Value;
f35d5bad 1511 XBUFFER_LOCAL_VALUE (newval)->realvalue = SYMBOL_VALUE (variable);
b0c2d1c6
RS
1512 XBUFFER_LOCAL_VALUE (newval)->buffer = Fcurrent_buffer ();
1513 XBUFFER_LOCAL_VALUE (newval)->frame = Qnil;
42e975f0 1514 XBUFFER_LOCAL_VALUE (newval)->found_for_buffer = 0;
b0c2d1c6
RS
1515 XBUFFER_LOCAL_VALUE (newval)->found_for_frame = 0;
1516 XBUFFER_LOCAL_VALUE (newval)->check_frame = 0;
1517 XBUFFER_LOCAL_VALUE (newval)->cdr = tem;
f35d5bad 1518 SET_SYMBOL_VALUE (variable, newval);
d9c2a0f2 1519 return variable;
7921925c
JB
1520}
1521
1522DEFUN ("make-local-variable", Fmake_local_variable, Smake_local_variable,
8c1a1077
PJ
1523 1, 1, "vMake Local Variable: ",
1524 doc: /* Make VARIABLE have a separate value in the current buffer.
1525Other buffers will continue to share a common default value.
1526\(The buffer-local value of VARIABLE starts out as the same value
1527VARIABLE previously had. If VARIABLE was void, it remains void.\)
a9908653 1528Return VARIABLE.
8c1a1077
PJ
1529
1530If the variable is already arranged to become local when set,
1531this function causes a local value to exist for this buffer,
1532just as setting the variable would do.
1533
1534This function returns VARIABLE, and therefore
1535 (set (make-local-variable 'VARIABLE) VALUE-EXP)
1536works.
1537
a9908653
RS
1538See also `make-variable-buffer-local'.
1539
8c1a1077 1540Do not use `make-local-variable' to make a hook variable buffer-local.
515f3f25 1541Instead, use `add-hook' and specify t for the LOCAL argument. */)
8c1a1077 1542 (variable)
d9c2a0f2 1543 register Lisp_Object variable;
7921925c
JB
1544{
1545 register Lisp_Object tem, valcontents;
1546
b7826503 1547 CHECK_SYMBOL (variable);
14970b45 1548 variable = indirect_variable (variable);
7921925c 1549
f35d5bad 1550 valcontents = SYMBOL_VALUE (variable);
d9c2a0f2 1551 if (EQ (variable, Qnil) || EQ (variable, Qt) || KBOARD_OBJFWDP (valcontents))
d5db4077 1552 error ("Symbol %s may not be buffer-local", SDATA (SYMBOL_NAME (variable)));
7921925c 1553
e9ebc175 1554 if (BUFFER_LOCAL_VALUEP (valcontents) || BUFFER_OBJFWDP (valcontents))
7921925c 1555 {
d9c2a0f2 1556 tem = Fboundp (variable);
7403b5c8 1557
7921925c
JB
1558 /* Make sure the symbol has a local value in this particular buffer,
1559 by setting it to the same value it already has. */
d9c2a0f2
EN
1560 Fset (variable, (EQ (tem, Qt) ? Fsymbol_value (variable) : Qunbound));
1561 return variable;
7921925c 1562 }
42e975f0 1563 /* Make sure symbol is set up to hold per-buffer values. */
e9ebc175 1564 if (!SOME_BUFFER_LOCAL_VALUEP (valcontents))
7921925c 1565 {
8d4afcac 1566 Lisp_Object newval;
7921925c 1567 tem = Fcons (Qnil, do_symval_forwarding (valcontents));
f3fbd155 1568 XSETCAR (tem, tem);
8d4afcac 1569 newval = allocate_misc ();
324a6eef 1570 XMISCTYPE (newval) = Lisp_Misc_Some_Buffer_Local_Value;
f35d5bad 1571 XBUFFER_LOCAL_VALUE (newval)->realvalue = SYMBOL_VALUE (variable);
b0c2d1c6
RS
1572 XBUFFER_LOCAL_VALUE (newval)->buffer = Qnil;
1573 XBUFFER_LOCAL_VALUE (newval)->frame = Qnil;
1574 XBUFFER_LOCAL_VALUE (newval)->found_for_buffer = 0;
1575 XBUFFER_LOCAL_VALUE (newval)->found_for_frame = 0;
1576 XBUFFER_LOCAL_VALUE (newval)->check_frame = 0;
1577 XBUFFER_LOCAL_VALUE (newval)->cdr = tem;
9dff2aaa 1578 SET_SYMBOL_VALUE (variable, newval);
7921925c 1579 }
42e975f0 1580 /* Make sure this buffer has its own value of symbol. */
d9c2a0f2 1581 tem = Fassq (variable, current_buffer->local_var_alist);
a33ef3ab 1582 if (NILP (tem))
7921925c 1583 {
a5d004a1
RS
1584 /* Swap out any local binding for some other buffer, and make
1585 sure the current value is permanently recorded, if it's the
1586 default value. */
d9c2a0f2 1587 find_symbol_value (variable);
a5d004a1 1588
7921925c 1589 current_buffer->local_var_alist
f35d5bad 1590 = Fcons (Fcons (variable, XCDR (XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (variable))->cdr)),
7921925c
JB
1591 current_buffer->local_var_alist);
1592
1593 /* Make sure symbol does not think it is set up for this buffer;
42e975f0 1594 force it to look once again for this buffer's value. */
7921925c 1595 {
8d4afcac 1596 Lisp_Object *pvalbuf;
a5d004a1 1597
f35d5bad 1598 valcontents = SYMBOL_VALUE (variable);
a5d004a1 1599
b0c2d1c6 1600 pvalbuf = &XBUFFER_LOCAL_VALUE (valcontents)->buffer;
8d4afcac
KH
1601 if (current_buffer == XBUFFER (*pvalbuf))
1602 *pvalbuf = Qnil;
b0c2d1c6 1603 XBUFFER_LOCAL_VALUE (valcontents)->found_for_buffer = 0;
7921925c 1604 }
7921925c 1605 }
a5ca2b75 1606
42e975f0
RS
1607 /* If the symbol forwards into a C variable, then load the binding
1608 for this buffer now. If C code modifies the variable before we
1609 load the binding in, then that new value will clobber the default
1610 binding the next time we unload it. */
f35d5bad 1611 valcontents = XBUFFER_LOCAL_VALUE (SYMBOL_VALUE (variable))->realvalue;
e9ebc175 1612 if (INTFWDP (valcontents) || BOOLFWDP (valcontents) || OBJFWDP (valcontents))
f35d5bad 1613 swap_in_symval_forwarding (variable, SYMBOL_VALUE (variable));
a5ca2b75 1614
d9c2a0f2 1615 return variable;
7921925c
JB
1616}
1617
1618DEFUN ("kill-local-variable", Fkill_local_variable, Skill_local_variable,
8c1a1077
PJ
1619 1, 1, "vKill Local Variable: ",
1620 doc: /* Make VARIABLE no longer have a separate value in the current buffer.
bfb96cb7 1621From now on the default value will apply in this buffer. Return VARIABLE. */)
8c1a1077 1622 (variable)
d9c2a0f2 1623 register Lisp_Object variable;
7921925c
JB
1624{
1625 register Lisp_Object tem, valcontents;
1626
b7826503 1627 CHECK_SYMBOL (variable);
14970b45 1628 variable = indirect_variable (variable);
7921925c 1629
f35d5bad 1630 valcontents = SYMBOL_VALUE (variable);
7921925c 1631
e9ebc175 1632 if (BUFFER_OBJFWDP (valcontents))
7921925c 1633 {
999b32fd 1634 int offset = XBUFFER_OBJFWD (valcontents)->offset;
f6cd0527 1635 int idx = PER_BUFFER_IDX (offset);
7921925c 1636
999b32fd 1637 if (idx > 0)
7921925c 1638 {
f6cd0527
GM
1639 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 0);
1640 PER_BUFFER_VALUE (current_buffer, offset)
1641 = PER_BUFFER_DEFAULT (offset);
7921925c 1642 }
d9c2a0f2 1643 return variable;
7921925c
JB
1644 }
1645
e9ebc175
KH
1646 if (!BUFFER_LOCAL_VALUEP (valcontents)
1647 && !SOME_BUFFER_LOCAL_VALUEP (valcontents))
d9c2a0f2 1648 return variable;
7921925c 1649
42e975f0 1650 /* Get rid of this buffer's alist element, if any. */
7921925c 1651
d9c2a0f2 1652 tem = Fassq (variable, current_buffer->local_var_alist);
a33ef3ab 1653 if (!NILP (tem))
8d4afcac
KH
1654 current_buffer->local_var_alist
1655 = Fdelq (tem, current_buffer->local_var_alist);
7921925c 1656
42e975f0
RS
1657 /* If the symbol is set up with the current buffer's binding
1658 loaded, recompute its value. We have to do it now, or else
1659 forwarded objects won't work right. */
7921925c 1660 {
999e6484 1661 Lisp_Object *pvalbuf, buf;
f35d5bad 1662 valcontents = SYMBOL_VALUE (variable);
b0c2d1c6 1663 pvalbuf = &XBUFFER_LOCAL_VALUE (valcontents)->buffer;
999e6484
SM
1664 XSETBUFFER (buf, current_buffer);
1665 if (EQ (buf, *pvalbuf))
79c83e03
KH
1666 {
1667 *pvalbuf = Qnil;
b0c2d1c6 1668 XBUFFER_LOCAL_VALUE (valcontents)->found_for_buffer = 0;
978dd578 1669 find_symbol_value (variable);
79c83e03 1670 }
7921925c
JB
1671 }
1672
d9c2a0f2 1673 return variable;
7921925c 1674}
62476adc 1675
b0c2d1c6
RS
1676/* Lisp functions for creating and removing buffer-local variables. */
1677
1678DEFUN ("make-variable-frame-local", Fmake_variable_frame_local, Smake_variable_frame_local,
8c1a1077
PJ
1679 1, 1, "vMake Variable Frame Local: ",
1680 doc: /* Enable VARIABLE to have frame-local bindings.
4d4d36b1
RS
1681This does not create any frame-local bindings for VARIABLE,
1682it just makes them possible.
1683
1684A frame-local binding is actually a frame parameter value.
1685If a frame F has a value for the frame parameter named VARIABLE,
1686that also acts as a frame-local binding for VARIABLE in F--
1687provided this function has been called to enable VARIABLE
1688to have frame-local bindings at all.
1689
1690The only way to create a frame-local binding for VARIABLE in a frame
1691is to set the VARIABLE frame parameter of that frame. See
1692`modify-frame-parameters' for how to set frame parameters.
1693
1694Buffer-local bindings take precedence over frame-local bindings. */)
8c1a1077 1695 (variable)
b0c2d1c6
RS
1696 register Lisp_Object variable;
1697{
1698 register Lisp_Object tem, valcontents, newval;
1699
b7826503 1700 CHECK_SYMBOL (variable);
14970b45 1701 variable = indirect_variable (variable);
b0c2d1c6 1702
f35d5bad 1703 valcontents = SYMBOL_VALUE (variable);
b0c2d1c6
RS
1704 if (EQ (variable, Qnil) || EQ (variable, Qt) || KBOARD_OBJFWDP (valcontents)
1705 || BUFFER_OBJFWDP (valcontents))
d5db4077 1706 error ("Symbol %s may not be frame-local", SDATA (SYMBOL_NAME (variable)));
b0c2d1c6
RS
1707
1708 if (BUFFER_LOCAL_VALUEP (valcontents)
1709 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
42e975f0
RS
1710 {
1711 XBUFFER_LOCAL_VALUE (valcontents)->check_frame = 1;
1712 return variable;
1713 }
b0c2d1c6
RS
1714
1715 if (EQ (valcontents, Qunbound))
f35d5bad 1716 SET_SYMBOL_VALUE (variable, Qnil);
b0c2d1c6 1717 tem = Fcons (Qnil, Fsymbol_value (variable));
f3fbd155 1718 XSETCAR (tem, tem);
b0c2d1c6
RS
1719 newval = allocate_misc ();
1720 XMISCTYPE (newval) = Lisp_Misc_Some_Buffer_Local_Value;
f35d5bad 1721 XBUFFER_LOCAL_VALUE (newval)->realvalue = SYMBOL_VALUE (variable);
b0c2d1c6
RS
1722 XBUFFER_LOCAL_VALUE (newval)->buffer = Qnil;
1723 XBUFFER_LOCAL_VALUE (newval)->frame = Qnil;
1724 XBUFFER_LOCAL_VALUE (newval)->found_for_buffer = 0;
1725 XBUFFER_LOCAL_VALUE (newval)->found_for_frame = 0;
1726 XBUFFER_LOCAL_VALUE (newval)->check_frame = 1;
1727 XBUFFER_LOCAL_VALUE (newval)->cdr = tem;
f35d5bad 1728 SET_SYMBOL_VALUE (variable, newval);
b0c2d1c6
RS
1729 return variable;
1730}
1731
62476adc 1732DEFUN ("local-variable-p", Flocal_variable_p, Slocal_variable_p,
8c1a1077
PJ
1733 1, 2, 0,
1734 doc: /* Non-nil if VARIABLE has a local binding in buffer BUFFER.
1735BUFFER defaults to the current buffer. */)
1736 (variable, buffer)
d9c2a0f2 1737 register Lisp_Object variable, buffer;
62476adc
RS
1738{
1739 Lisp_Object valcontents;
c48ead86
KH
1740 register struct buffer *buf;
1741
1742 if (NILP (buffer))
1743 buf = current_buffer;
1744 else
1745 {
b7826503 1746 CHECK_BUFFER (buffer);
c48ead86
KH
1747 buf = XBUFFER (buffer);
1748 }
62476adc 1749
b7826503 1750 CHECK_SYMBOL (variable);
14970b45 1751 variable = indirect_variable (variable);
62476adc 1752
f35d5bad 1753 valcontents = SYMBOL_VALUE (variable);
c48ead86 1754 if (BUFFER_LOCAL_VALUEP (valcontents)
1e5f16fa 1755 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
c48ead86
KH
1756 {
1757 Lisp_Object tail, elt;
f35d5bad 1758
7539e11f 1759 for (tail = buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
c48ead86 1760 {
7539e11f
KR
1761 elt = XCAR (tail);
1762 if (EQ (variable, XCAR (elt)))
c48ead86
KH
1763 return Qt;
1764 }
1765 }
1766 if (BUFFER_OBJFWDP (valcontents))
1767 {
1768 int offset = XBUFFER_OBJFWD (valcontents)->offset;
f6cd0527
GM
1769 int idx = PER_BUFFER_IDX (offset);
1770 if (idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
c48ead86
KH
1771 return Qt;
1772 }
1773 return Qnil;
62476adc 1774}
f4f04cee
RS
1775
1776DEFUN ("local-variable-if-set-p", Flocal_variable_if_set_p, Slocal_variable_if_set_p,
8c1a1077 1777 1, 2, 0,
1dc5ba01
LT
1778 doc: /* Non-nil if VARIABLE will be local in buffer BUFFER when set there.
1779More precisely, this means that setting the variable \(with `set' or`setq'),
1780while it does not have a `let'-style binding that was made in BUFFER,
1781will produce a buffer local binding. See Info node
1782`(elisp)Creating Buffer-Local'.
8c1a1077
PJ
1783BUFFER defaults to the current buffer. */)
1784 (variable, buffer)
d9c2a0f2 1785 register Lisp_Object variable, buffer;
f4f04cee
RS
1786{
1787 Lisp_Object valcontents;
1788 register struct buffer *buf;
1789
1790 if (NILP (buffer))
1791 buf = current_buffer;
1792 else
1793 {
b7826503 1794 CHECK_BUFFER (buffer);
f4f04cee
RS
1795 buf = XBUFFER (buffer);
1796 }
1797
b7826503 1798 CHECK_SYMBOL (variable);
14970b45 1799 variable = indirect_variable (variable);
f4f04cee 1800
f35d5bad 1801 valcontents = SYMBOL_VALUE (variable);
f4f04cee
RS
1802
1803 /* This means that make-variable-buffer-local was done. */
1804 if (BUFFER_LOCAL_VALUEP (valcontents))
1805 return Qt;
1806 /* All these slots become local if they are set. */
1807 if (BUFFER_OBJFWDP (valcontents))
1808 return Qt;
1809 if (SOME_BUFFER_LOCAL_VALUEP (valcontents))
1810 {
1811 Lisp_Object tail, elt;
7539e11f 1812 for (tail = buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
f4f04cee 1813 {
7539e11f
KR
1814 elt = XCAR (tail);
1815 if (EQ (variable, XCAR (elt)))
f4f04cee
RS
1816 return Qt;
1817 }
1818 }
1819 return Qnil;
1820}
0a2546d4
RS
1821
1822DEFUN ("variable-binding-locus", Fvariable_binding_locus, Svariable_binding_locus,
1823 1, 1, 0,
1824 doc: /* Return a value indicating where VARIABLE's current binding comes from.
1825If the current binding is buffer-local, the value is the current buffer.
1826If the current binding is frame-local, the value is the selected frame.
1827If the current binding is global (the default), the value is nil. */)
1828 (variable)
1829 register Lisp_Object variable;
1830{
1831 Lisp_Object valcontents;
1832
1833 CHECK_SYMBOL (variable);
1834 variable = indirect_variable (variable);
1835
1836 /* Make sure the current binding is actually swapped in. */
1837 find_symbol_value (variable);
1838
1839 valcontents = XSYMBOL (variable)->value;
1840
1841 if (BUFFER_LOCAL_VALUEP (valcontents)
1842 || SOME_BUFFER_LOCAL_VALUEP (valcontents)
1843 || BUFFER_OBJFWDP (valcontents))
1844 {
1845 /* For a local variable, record both the symbol and which
1846 buffer's or frame's value we are saving. */
1847 if (!NILP (Flocal_variable_p (variable, Qnil)))
1848 return Fcurrent_buffer ();
1849 else if (!BUFFER_OBJFWDP (valcontents)
1850 && XBUFFER_LOCAL_VALUE (valcontents)->found_for_frame)
1851 return XBUFFER_LOCAL_VALUE (valcontents)->frame;
1852 }
1853
1854 return Qnil;
1855}
7921925c 1856\f
ffd56f97
JB
1857/* Find the function at the end of a chain of symbol function indirections. */
1858
1859/* If OBJECT is a symbol, find the end of its function chain and
1860 return the value found there. If OBJECT is not a symbol, just
1861 return it. If there is a cycle in the function chain, signal a
1862 cyclic-function-indirection error.
1863
1864 This is like Findirect_function, except that it doesn't signal an
1865 error if the chain ends up unbound. */
1866Lisp_Object
a2932990 1867indirect_function (object)
62476adc 1868 register Lisp_Object object;
ffd56f97 1869{
eb8c3be9 1870 Lisp_Object tortoise, hare;
ffd56f97 1871
eb8c3be9 1872 hare = tortoise = object;
ffd56f97
JB
1873
1874 for (;;)
1875 {
e9ebc175 1876 if (!SYMBOLP (hare) || EQ (hare, Qunbound))
ffd56f97
JB
1877 break;
1878 hare = XSYMBOL (hare)->function;
e9ebc175 1879 if (!SYMBOLP (hare) || EQ (hare, Qunbound))
ffd56f97
JB
1880 break;
1881 hare = XSYMBOL (hare)->function;
1882
eb8c3be9 1883 tortoise = XSYMBOL (tortoise)->function;
ffd56f97 1884
eb8c3be9 1885 if (EQ (hare, tortoise))
740ef0b5 1886 xsignal1 (Qcyclic_function_indirection, object);
ffd56f97
JB
1887 }
1888
1889 return hare;
1890}
1891
a7f96a35 1892DEFUN ("indirect-function", Findirect_function, Sindirect_function, 1, 2, 0,
8c1a1077 1893 doc: /* Return the function at the end of OBJECT's function chain.
0ddb0ae8
TTN
1894If OBJECT is not a symbol, just return it. Otherwise, follow all
1895function indirections to find the final function binding and return it.
1896If the final symbol in the chain is unbound, signal a void-function error.
1897Optional arg NOERROR non-nil means to return nil instead of signalling.
8c1a1077
PJ
1898Signal a cyclic-function-indirection error if there is a loop in the
1899function chain of symbols. */)
0ddb0ae8 1900 (object, noerror)
8c1a1077 1901 register Lisp_Object object;
0ddb0ae8 1902 Lisp_Object noerror;
ffd56f97
JB
1903{
1904 Lisp_Object result;
1905
64de53d8
KS
1906 /* Optimize for no indirection. */
1907 result = object;
1908 if (SYMBOLP (result) && !EQ (result, Qunbound)
1909 && (result = XSYMBOL (result)->function, SYMBOLP (result)))
1910 result = indirect_function (result);
1911 if (!EQ (result, Qunbound))
1912 return result;
ffd56f97 1913
64de53d8 1914 if (NILP (noerror))
740ef0b5 1915 xsignal1 (Qvoid_function, object);
64de53d8
KS
1916
1917 return Qnil;
ffd56f97
JB
1918}
1919\f
7921925c
JB
1920/* Extract and set vector and string elements */
1921
1922DEFUN ("aref", Faref, Saref, 2, 2, 0,
8c1a1077
PJ
1923 doc: /* Return the element of ARRAY at index IDX.
1924ARRAY may be a vector, a string, a char-table, a bool-vector,
1925or a byte-code object. IDX starts at 0. */)
1926 (array, idx)
7921925c
JB
1927 register Lisp_Object array;
1928 Lisp_Object idx;
1929{
1930 register int idxval;
1931
b7826503 1932 CHECK_NUMBER (idx);
7921925c 1933 idxval = XINT (idx);
e9ebc175 1934 if (STRINGP (array))
7921925c 1935 {
25638b07
RS
1936 int c, idxval_byte;
1937
d5db4077 1938 if (idxval < 0 || idxval >= SCHARS (array))
c24e4efe 1939 args_out_of_range (array, idx);
25638b07 1940 if (! STRING_MULTIBYTE (array))
d5db4077 1941 return make_number ((unsigned char) SREF (array, idxval));
25638b07
RS
1942 idxval_byte = string_char_to_byte (array, idxval);
1943
29f44a37 1944 c = STRING_CHAR (SDATA (array) + idxval_byte,
d5db4077 1945 SBYTES (array) - idxval_byte);
25638b07 1946 return make_number (c);
7921925c 1947 }
4d276982
RS
1948 else if (BOOL_VECTOR_P (array))
1949 {
1950 int val;
4d276982
RS
1951
1952 if (idxval < 0 || idxval >= XBOOL_VECTOR (array)->size)
1953 args_out_of_range (array, idx);
1954
b9ed2177
AS
1955 val = (unsigned char) XBOOL_VECTOR (array)->data[idxval / BOOL_VECTOR_BITS_PER_CHAR];
1956 return (val & (1 << (idxval % BOOL_VECTOR_BITS_PER_CHAR)) ? Qt : Qnil);
4d276982
RS
1957 }
1958 else if (CHAR_TABLE_P (array))
1959 {
1960 Lisp_Object val;
1961
6bbd7a29
GM
1962 val = Qnil;
1963
4d276982
RS
1964 if (idxval < 0)
1965 args_out_of_range (array, idx);
ab5c3f93 1966 if (idxval < CHAR_TABLE_ORDINARY_SLOTS)
8313c4e7 1967 {
256d566c
KH
1968 if (! SINGLE_BYTE_CHAR_P (idxval))
1969 args_out_of_range (array, idx);
39e16e51 1970 /* For ASCII and 8-bit European characters, the element is
3a6cf6bd 1971 stored in the top table. */
8313c4e7 1972 val = XCHAR_TABLE (array)->contents[idxval];
256d566c
KH
1973 if (NILP (val))
1974 {
1975 int default_slot
1976 = (idxval < 0x80 ? CHAR_TABLE_DEFAULT_SLOT_ASCII
1977 : idxval < 0xA0 ? CHAR_TABLE_DEFAULT_SLOT_8_BIT_CONTROL
1978 : CHAR_TABLE_DEFAULT_SLOT_8_BIT_GRAPHIC);
1979 val = XCHAR_TABLE (array)->contents[default_slot];
1980 }
8313c4e7
KH
1981 if (NILP (val))
1982 val = XCHAR_TABLE (array)->defalt;
1983 while (NILP (val)) /* Follow parents until we find some value. */
1984 {
1985 array = XCHAR_TABLE (array)->parent;
1986 if (NILP (array))
1987 return Qnil;
1988 val = XCHAR_TABLE (array)->contents[idxval];
1989 if (NILP (val))
1990 val = XCHAR_TABLE (array)->defalt;
1991 }
1992 return val;
1993 }
4d276982
RS
1994 else
1995 {
39e16e51
KH
1996 int code[4], i;
1997 Lisp_Object sub_table;
256d566c 1998 Lisp_Object current_default;
8313c4e7 1999
37309c9d 2000 SPLIT_CHAR (idxval, code[0], code[1], code[2]);
e19c1eb4
KH
2001 if (code[1] < 32) code[1] = -1;
2002 else if (code[2] < 32) code[2] = -1;
2003
39e16e51
KH
2004 /* Here, the possible range of CODE[0] (== charset ID) is
2005 128..MAX_CHARSET. Since the top level char table contains
2006 data for multibyte characters after 256th element, we must
2007 increment CODE[0] by 128 to get a correct index. */
2008 code[0] += 128;
2009 code[3] = -1; /* anchor */
4d276982
RS
2010
2011 try_parent_char_table:
256d566c 2012 current_default = XCHAR_TABLE (array)->defalt;
39e16e51
KH
2013 sub_table = array;
2014 for (i = 0; code[i] >= 0; i++)
4d276982 2015 {
39e16e51
KH
2016 val = XCHAR_TABLE (sub_table)->contents[code[i]];
2017 if (SUB_CHAR_TABLE_P (val))
256d566c
KH
2018 {
2019 sub_table = val;
2020 if (! NILP (XCHAR_TABLE (sub_table)->defalt))
2021 current_default = XCHAR_TABLE (sub_table)->defalt;
2022 }
39e16e51 2023 else
8313c4e7 2024 {
39e16e51 2025 if (NILP (val))
256d566c 2026 val = current_default;
39e16e51
KH
2027 if (NILP (val))
2028 {
2029 array = XCHAR_TABLE (array)->parent;
2030 if (!NILP (array))
2031 goto try_parent_char_table;
2032 }
2033 return val;
8313c4e7 2034 }
4d276982 2035 }
256d566c
KH
2036 /* Reaching here means IDXVAL is a generic character in
2037 which each character or a group has independent value.
2038 Essentially it's nonsense to get a value for such a
2039 generic character, but for backward compatibility, we try
2040 the default value and parent. */
2041 val = current_default;
8313c4e7 2042 if (NILP (val))
4d276982
RS
2043 {
2044 array = XCHAR_TABLE (array)->parent;
39e16e51
KH
2045 if (!NILP (array))
2046 goto try_parent_char_table;
4d276982 2047 }
4d276982
RS
2048 return val;
2049 }
4d276982 2050 }
7921925c 2051 else
c24e4efe 2052 {
6bbd7a29 2053 int size = 0;
7f358972
RS
2054 if (VECTORP (array))
2055 size = XVECTOR (array)->size;
2056 else if (COMPILEDP (array))
2057 size = XVECTOR (array)->size & PSEUDOVECTOR_SIZE_MASK;
2058 else
2059 wrong_type_argument (Qarrayp, array);
2060
2061 if (idxval < 0 || idxval >= size)
c24e4efe
KH
2062 args_out_of_range (array, idx);
2063 return XVECTOR (array)->contents[idxval];
2064 }
7921925c
JB
2065}
2066
2067DEFUN ("aset", Faset, Saset, 3, 3, 0,
8c1a1077 2068 doc: /* Store into the element of ARRAY at index IDX the value NEWELT.
bfb96cb7
FP
2069Return NEWELT. ARRAY may be a vector, a string, a char-table or a
2070bool-vector. IDX starts at 0. */)
8c1a1077 2071 (array, idx, newelt)
7921925c
JB
2072 register Lisp_Object array;
2073 Lisp_Object idx, newelt;
2074{
2075 register int idxval;
2076
b7826503 2077 CHECK_NUMBER (idx);
7921925c 2078 idxval = XINT (idx);
0c64a8cd 2079 CHECK_ARRAY (array, Qarrayp);
7921925c
JB
2080 CHECK_IMPURE (array);
2081
e9ebc175 2082 if (VECTORP (array))
c24e4efe
KH
2083 {
2084 if (idxval < 0 || idxval >= XVECTOR (array)->size)
2085 args_out_of_range (array, idx);
2086 XVECTOR (array)->contents[idxval] = newelt;
2087 }
4d276982
RS
2088 else if (BOOL_VECTOR_P (array))
2089 {
2090 int val;
4d276982
RS
2091
2092 if (idxval < 0 || idxval >= XBOOL_VECTOR (array)->size)
2093 args_out_of_range (array, idx);
2094
b9ed2177 2095 val = (unsigned char) XBOOL_VECTOR (array)->data[idxval / BOOL_VECTOR_BITS_PER_CHAR];
4d276982
RS
2096
2097 if (! NILP (newelt))
b9ed2177 2098 val |= 1 << (idxval % BOOL_VECTOR_BITS_PER_CHAR);
4d276982 2099 else
b9ed2177
AS
2100 val &= ~(1 << (idxval % BOOL_VECTOR_BITS_PER_CHAR));
2101 XBOOL_VECTOR (array)->data[idxval / BOOL_VECTOR_BITS_PER_CHAR] = val;
4d276982
RS
2102 }
2103 else if (CHAR_TABLE_P (array))
2104 {
4d276982
RS
2105 if (idxval < 0)
2106 args_out_of_range (array, idx);
ab5c3f93 2107 if (idxval < CHAR_TABLE_ORDINARY_SLOTS)
256d566c
KH
2108 {
2109 if (! SINGLE_BYTE_CHAR_P (idxval))
2110 args_out_of_range (array, idx);
2111 XCHAR_TABLE (array)->contents[idxval] = newelt;
2112 }
4d276982
RS
2113 else
2114 {
39e16e51 2115 int code[4], i;
8313c4e7 2116 Lisp_Object val;
4d276982 2117
37309c9d 2118 SPLIT_CHAR (idxval, code[0], code[1], code[2]);
e19c1eb4
KH
2119 if (code[1] < 32) code[1] = -1;
2120 else if (code[2] < 32) code[2] = -1;
2121
39e16e51
KH
2122 /* See the comment of the corresponding part in Faref. */
2123 code[0] += 128;
2124 code[3] = -1; /* anchor */
2125 for (i = 0; code[i + 1] >= 0; i++)
8313c4e7 2126 {
39e16e51
KH
2127 val = XCHAR_TABLE (array)->contents[code[i]];
2128 if (SUB_CHAR_TABLE_P (val))
8313c4e7
KH
2129 array = val;
2130 else
3c8fccc3
RS
2131 {
2132 Lisp_Object temp;
2133
2134 /* VAL is a leaf. Create a sub char table with the
256d566c 2135 initial value VAL and look into it. */
3c8fccc3 2136
256d566c 2137 temp = make_sub_char_table (val);
3c8fccc3
RS
2138 XCHAR_TABLE (array)->contents[code[i]] = temp;
2139 array = temp;
2140 }
8313c4e7 2141 }
39e16e51 2142 XCHAR_TABLE (array)->contents[code[i]] = newelt;
4d276982 2143 }
4d276982 2144 }
25638b07
RS
2145 else if (STRING_MULTIBYTE (array))
2146 {
c6464167 2147 int idxval_byte, prev_bytes, new_bytes, nbytes;
3c9de1af 2148 unsigned char workbuf[MAX_MULTIBYTE_LENGTH], *p0 = workbuf, *p1;
25638b07 2149
d5db4077 2150 if (idxval < 0 || idxval >= SCHARS (array))
25638b07 2151 args_out_of_range (array, idx);
b7826503 2152 CHECK_NUMBER (newelt);
25638b07 2153
c6464167
MB
2154 nbytes = SBYTES (array);
2155
25638b07 2156 idxval_byte = string_char_to_byte (array, idxval);
29f44a37 2157 p1 = SDATA (array) + idxval_byte;
3c9de1af
KH
2158 PARSE_MULTIBYTE_SEQ (p1, nbytes - idxval_byte, prev_bytes);
2159 new_bytes = CHAR_STRING (XINT (newelt), p0);
2160 if (prev_bytes != new_bytes)
2161 {
2162 /* We must relocate the string data. */
d5db4077 2163 int nchars = SCHARS (array);
3c9de1af 2164 unsigned char *str;
f1a87317 2165 USE_SAFE_ALLOCA;
3c9de1af 2166
f1a87317 2167 SAFE_ALLOCA (str, unsigned char *, nbytes);
d5db4077 2168 bcopy (SDATA (array), str, nbytes);
3c9de1af
KH
2169 allocate_string_data (XSTRING (array), nchars,
2170 nbytes + new_bytes - prev_bytes);
d5db4077
KR
2171 bcopy (str, SDATA (array), idxval_byte);
2172 p1 = SDATA (array) + idxval_byte;
3c9de1af
KH
2173 bcopy (str + idxval_byte + prev_bytes, p1 + new_bytes,
2174 nbytes - (idxval_byte + prev_bytes));
233f3db6 2175 SAFE_FREE ();
3c9de1af
KH
2176 clear_string_char_byte_cache ();
2177 }
2178 while (new_bytes--)
2179 *p1++ = *p0++;
25638b07 2180 }
7921925c
JB
2181 else
2182 {
d5db4077 2183 if (idxval < 0 || idxval >= SCHARS (array))
c24e4efe 2184 args_out_of_range (array, idx);
b7826503 2185 CHECK_NUMBER (newelt);
3c9de1af
KH
2186
2187 if (XINT (newelt) < 0 || SINGLE_BYTE_CHAR_P (XINT (newelt)))
29f44a37 2188 SSET (array, idxval, XINT (newelt));
3c9de1af
KH
2189 else
2190 {
2191 /* We must relocate the string data while converting it to
2192 multibyte. */
2193 int idxval_byte, prev_bytes, new_bytes;
2194 unsigned char workbuf[MAX_MULTIBYTE_LENGTH], *p0 = workbuf, *p1;
d5db4077 2195 unsigned char *origstr = SDATA (array), *str;
3c9de1af 2196 int nchars, nbytes;
f1a87317 2197 USE_SAFE_ALLOCA;
3c9de1af 2198
d5db4077 2199 nchars = SCHARS (array);
3c9de1af
KH
2200 nbytes = idxval_byte = count_size_as_multibyte (origstr, idxval);
2201 nbytes += count_size_as_multibyte (origstr + idxval,
2202 nchars - idxval);
f1a87317 2203 SAFE_ALLOCA (str, unsigned char *, nbytes);
d5db4077 2204 copy_text (SDATA (array), str, nchars, 0, 1);
3c9de1af
KH
2205 PARSE_MULTIBYTE_SEQ (str + idxval_byte, nbytes - idxval_byte,
2206 prev_bytes);
2207 new_bytes = CHAR_STRING (XINT (newelt), p0);
2208 allocate_string_data (XSTRING (array), nchars,
2209 nbytes + new_bytes - prev_bytes);
d5db4077
KR
2210 bcopy (str, SDATA (array), idxval_byte);
2211 p1 = SDATA (array) + idxval_byte;
3c9de1af
KH
2212 while (new_bytes--)
2213 *p1++ = *p0++;
2214 bcopy (str + idxval_byte + prev_bytes, p1,
2215 nbytes - (idxval_byte + prev_bytes));
233f3db6 2216 SAFE_FREE ();
3c9de1af
KH
2217 clear_string_char_byte_cache ();
2218 }
7921925c
JB
2219 }
2220
2221 return newelt;
2222}
7921925c
JB
2223\f
2224/* Arithmetic functions */
2225
2226enum comparison { equal, notequal, less, grtr, less_or_equal, grtr_or_equal };
2227
2228Lisp_Object
2229arithcompare (num1, num2, comparison)
2230 Lisp_Object num1, num2;
2231 enum comparison comparison;
2232{
6bbd7a29 2233 double f1 = 0, f2 = 0;
7921925c
JB
2234 int floatp = 0;
2235
b7826503
PJ
2236 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num1);
2237 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num2);
7921925c 2238
e9ebc175 2239 if (FLOATP (num1) || FLOATP (num2))
7921925c
JB
2240 {
2241 floatp = 1;
7539e11f
KR
2242 f1 = (FLOATP (num1)) ? XFLOAT_DATA (num1) : XINT (num1);
2243 f2 = (FLOATP (num2)) ? XFLOAT_DATA (num2) : XINT (num2);
7921925c 2244 }
7921925c
JB
2245
2246 switch (comparison)
2247 {
2248 case equal:
2249 if (floatp ? f1 == f2 : XINT (num1) == XINT (num2))
2250 return Qt;
2251 return Qnil;
2252
2253 case notequal:
2254 if (floatp ? f1 != f2 : XINT (num1) != XINT (num2))
2255 return Qt;
2256 return Qnil;
2257
2258 case less:
2259 if (floatp ? f1 < f2 : XINT (num1) < XINT (num2))
2260 return Qt;
2261 return Qnil;
2262
2263 case less_or_equal:
2264 if (floatp ? f1 <= f2 : XINT (num1) <= XINT (num2))
2265 return Qt;
2266 return Qnil;
2267
2268 case grtr:
2269 if (floatp ? f1 > f2 : XINT (num1) > XINT (num2))
2270 return Qt;
2271 return Qnil;
2272
2273 case grtr_or_equal:
2274 if (floatp ? f1 >= f2 : XINT (num1) >= XINT (num2))
2275 return Qt;
2276 return Qnil;
25e40a4b
JB
2277
2278 default:
2279 abort ();
7921925c
JB
2280 }
2281}
2282
2283DEFUN ("=", Feqlsign, Seqlsign, 2, 2, 0,
8c1a1077
PJ
2284 doc: /* Return t if two args, both numbers or markers, are equal. */)
2285 (num1, num2)
7921925c
JB
2286 register Lisp_Object num1, num2;
2287{
2288 return arithcompare (num1, num2, equal);
2289}
2290
2291DEFUN ("<", Flss, Slss, 2, 2, 0,
8c1a1077
PJ
2292 doc: /* Return t if first arg is less than second arg. Both must be numbers or markers. */)
2293 (num1, num2)
7921925c
JB
2294 register Lisp_Object num1, num2;
2295{
2296 return arithcompare (num1, num2, less);
2297}
2298
2299DEFUN (">", Fgtr, Sgtr, 2, 2, 0,
8c1a1077
PJ
2300 doc: /* Return t if first arg is greater than second arg. Both must be numbers or markers. */)
2301 (num1, num2)
7921925c
JB
2302 register Lisp_Object num1, num2;
2303{
2304 return arithcompare (num1, num2, grtr);
2305}
2306
2307DEFUN ("<=", Fleq, Sleq, 2, 2, 0,
8c1a1077
PJ
2308 doc: /* Return t if first arg is less than or equal to second arg.
2309Both must be numbers or markers. */)
2310 (num1, num2)
7921925c
JB
2311 register Lisp_Object num1, num2;
2312{
2313 return arithcompare (num1, num2, less_or_equal);
2314}
2315
2316DEFUN (">=", Fgeq, Sgeq, 2, 2, 0,
8c1a1077
PJ
2317 doc: /* Return t if first arg is greater than or equal to second arg.
2318Both must be numbers or markers. */)
2319 (num1, num2)
7921925c
JB
2320 register Lisp_Object num1, num2;
2321{
2322 return arithcompare (num1, num2, grtr_or_equal);
2323}
2324
2325DEFUN ("/=", Fneq, Sneq, 2, 2, 0,
8c1a1077
PJ
2326 doc: /* Return t if first arg is not equal to second arg. Both must be numbers or markers. */)
2327 (num1, num2)
7921925c
JB
2328 register Lisp_Object num1, num2;
2329{
2330 return arithcompare (num1, num2, notequal);
2331}
2332
8c1a1077
PJ
2333DEFUN ("zerop", Fzerop, Szerop, 1, 1, 0,
2334 doc: /* Return t if NUMBER is zero. */)
2335 (number)
d9c2a0f2 2336 register Lisp_Object number;
7921925c 2337{
b7826503 2338 CHECK_NUMBER_OR_FLOAT (number);
7921925c 2339
d9c2a0f2 2340 if (FLOATP (number))
7921925c 2341 {
7539e11f 2342 if (XFLOAT_DATA (number) == 0.0)
7921925c
JB
2343 return Qt;
2344 return Qnil;
2345 }
7921925c 2346
d9c2a0f2 2347 if (!XINT (number))
7921925c
JB
2348 return Qt;
2349 return Qnil;
2350}
2351\f
70e9f399
RS
2352/* Convert between long values and pairs of Lisp integers.
2353 Note that long_to_cons returns a single Lisp integer
2354 when the value fits in one. */
51cf3e31
JB
2355
2356Lisp_Object
2357long_to_cons (i)
2358 unsigned long i;
2359{
9bc7166b 2360 unsigned long top = i >> 16;
51cf3e31
JB
2361 unsigned int bot = i & 0xFFFF;
2362 if (top == 0)
2363 return make_number (bot);
b42cfa11 2364 if (top == (unsigned long)-1 >> 16)
51cf3e31
JB
2365 return Fcons (make_number (-1), make_number (bot));
2366 return Fcons (make_number (top), make_number (bot));
2367}
2368
2369unsigned long
2370cons_to_long (c)
2371 Lisp_Object c;
2372{
878a80cc 2373 Lisp_Object top, bot;
51cf3e31
JB
2374 if (INTEGERP (c))
2375 return XINT (c);
7539e11f
KR
2376 top = XCAR (c);
2377 bot = XCDR (c);
51cf3e31 2378 if (CONSP (bot))
7539e11f 2379 bot = XCAR (bot);
51cf3e31
JB
2380 return ((XINT (top) << 16) | XINT (bot));
2381}
2382\f
f2980264 2383DEFUN ("number-to-string", Fnumber_to_string, Snumber_to_string, 1, 1, 0,
bfb96cb7 2384 doc: /* Return the decimal representation of NUMBER as a string.
8c1a1077
PJ
2385Uses a minus sign if negative.
2386NUMBER may be an integer or a floating point number. */)
2387 (number)
d9c2a0f2 2388 Lisp_Object number;
7921925c 2389{
6030ce64 2390 char buffer[VALBITS];
7921925c 2391
b7826503 2392 CHECK_NUMBER_OR_FLOAT (number);
7921925c 2393
d9c2a0f2 2394 if (FLOATP (number))
7921925c
JB
2395 {
2396 char pigbuf[350]; /* see comments in float_to_string */
2397
7539e11f 2398 float_to_string (pigbuf, XFLOAT_DATA (number));
7403b5c8 2399 return build_string (pigbuf);
7921925c 2400 }
7921925c 2401
e6c82a8d 2402 if (sizeof (int) == sizeof (EMACS_INT))
d9c2a0f2 2403 sprintf (buffer, "%d", XINT (number));
e6c82a8d 2404 else if (sizeof (long) == sizeof (EMACS_INT))
dd8daec5 2405 sprintf (buffer, "%ld", (long) XINT (number));
e6c82a8d
RS
2406 else
2407 abort ();
7921925c
JB
2408 return build_string (buffer);
2409}
2410
3883fbeb
RS
2411INLINE static int
2412digit_to_number (character, base)
2413 int character, base;
2414{
2415 int digit;
2416
2417 if (character >= '0' && character <= '9')
2418 digit = character - '0';
2419 else if (character >= 'a' && character <= 'z')
2420 digit = character - 'a' + 10;
2421 else if (character >= 'A' && character <= 'Z')
2422 digit = character - 'A' + 10;
2423 else
2424 return -1;
2425
2426 if (digit >= base)
2427 return -1;
2428 else
2429 return digit;
bfb96cb7 2430}
3883fbeb
RS
2431
2432DEFUN ("string-to-number", Fstring_to_number, Sstring_to_number, 1, 2, 0,
558ee900 2433 doc: /* Parse STRING as a decimal number and return the number.
8c1a1077
PJ
2434This parses both integers and floating point numbers.
2435It ignores leading spaces and tabs.
2436
2437If BASE, interpret STRING as a number in that base. If BASE isn't
2438present, base 10 is used. BASE must be between 2 and 16 (inclusive).
2439If the base used is not 10, floating point is not recognized. */)
2440 (string, base)
3883fbeb 2441 register Lisp_Object string, base;
7921925c 2442{
3883fbeb 2443 register unsigned char *p;
342858a5
GM
2444 register int b;
2445 int sign = 1;
2446 Lisp_Object val;
25e40a4b 2447
b7826503 2448 CHECK_STRING (string);
7921925c 2449
3883fbeb
RS
2450 if (NILP (base))
2451 b = 10;
2452 else
2453 {
b7826503 2454 CHECK_NUMBER (base);
3883fbeb
RS
2455 b = XINT (base);
2456 if (b < 2 || b > 16)
740ef0b5 2457 xsignal1 (Qargs_out_of_range, base);
3883fbeb
RS
2458 }
2459
25e40a4b
JB
2460 /* Skip any whitespace at the front of the number. Some versions of
2461 atoi do this anyway, so we might as well make Emacs lisp consistent. */
d5db4077 2462 p = SDATA (string);
0a3e4d65 2463 while (*p == ' ' || *p == '\t')
25e40a4b
JB
2464 p++;
2465
3883fbeb
RS
2466 if (*p == '-')
2467 {
342858a5 2468 sign = -1;
3883fbeb
RS
2469 p++;
2470 }
2471 else if (*p == '+')
2472 p++;
bfb96cb7 2473
8e36ae7f 2474 if (isfloat_string (p) && b == 10)
342858a5
GM
2475 val = make_float (sign * atof (p));
2476 else
3883fbeb 2477 {
342858a5
GM
2478 double v = 0;
2479
2480 while (1)
2481 {
2482 int digit = digit_to_number (*p++, b);
2483 if (digit < 0)
2484 break;
2485 v = v * b + digit;
2486 }
2487
cb938d46 2488 val = make_fixnum_or_float (sign * v);
3883fbeb 2489 }
342858a5
GM
2490
2491 return val;
7921925c 2492}
3883fbeb 2493
7403b5c8 2494\f
7921925c 2495enum arithop
7a283f36
GM
2496 {
2497 Aadd,
2498 Asub,
2499 Amult,
2500 Adiv,
2501 Alogand,
2502 Alogior,
2503 Alogxor,
2504 Amax,
2505 Amin
2506 };
2507
2508static Lisp_Object float_arith_driver P_ ((double, int, enum arithop,
2509 int, Lisp_Object *));
ad8d56b9 2510extern Lisp_Object fmod_float ();
b06faa91 2511
7921925c 2512Lisp_Object
87fbf902 2513arith_driver (code, nargs, args)
7921925c
JB
2514 enum arithop code;
2515 int nargs;
2516 register Lisp_Object *args;
2517{
2518 register Lisp_Object val;
2519 register int argnum;
7a283f36 2520 register EMACS_INT accum = 0;
5260234d 2521 register EMACS_INT next;
7921925c 2522
0220c518 2523 switch (SWITCH_ENUM_CAST (code))
7921925c
JB
2524 {
2525 case Alogior:
2526 case Alogxor:
2527 case Aadd:
2528 case Asub:
7a283f36
GM
2529 accum = 0;
2530 break;
7921925c 2531 case Amult:
7a283f36
GM
2532 accum = 1;
2533 break;
7921925c 2534 case Alogand:
7a283f36
GM
2535 accum = -1;
2536 break;
2537 default:
2538 break;
7921925c
JB
2539 }
2540
2541 for (argnum = 0; argnum < nargs; argnum++)
2542 {
7a283f36
GM
2543 /* Using args[argnum] as argument to CHECK_NUMBER_... */
2544 val = args[argnum];
b7826503 2545 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val);
7921925c 2546
7a283f36
GM
2547 if (FLOATP (val))
2548 return float_arith_driver ((double) accum, argnum, code,
2549 nargs, args);
2550 args[argnum] = val;
7921925c 2551 next = XINT (args[argnum]);
0220c518 2552 switch (SWITCH_ENUM_CAST (code))
7921925c 2553 {
7a283f36
GM
2554 case Aadd:
2555 accum += next;
2556 break;
7921925c 2557 case Asub:
e64981da 2558 accum = argnum ? accum - next : nargs == 1 ? - next : next;
7921925c 2559 break;
7a283f36
GM
2560 case Amult:
2561 accum *= next;
2562 break;
7921925c 2563 case Adiv:
7a283f36
GM
2564 if (!argnum)
2565 accum = next;
87fbf902
RS
2566 else
2567 {
2568 if (next == 0)
740ef0b5 2569 xsignal0 (Qarith_error);
87fbf902
RS
2570 accum /= next;
2571 }
7921925c 2572 break;
7a283f36
GM
2573 case Alogand:
2574 accum &= next;
2575 break;
2576 case Alogior:
2577 accum |= next;
2578 break;
2579 case Alogxor:
2580 accum ^= next;
2581 break;
2582 case Amax:
2583 if (!argnum || next > accum)
2584 accum = next;
2585 break;
2586 case Amin:
2587 if (!argnum || next < accum)
2588 accum = next;
2589 break;
7921925c
JB
2590 }
2591 }
2592
f187f1f7 2593 XSETINT (val, accum);
7921925c
JB
2594 return val;
2595}
2596
1a2f2d33
KH
2597#undef isnan
2598#define isnan(x) ((x) != (x))
2599
7a283f36 2600static Lisp_Object
7921925c
JB
2601float_arith_driver (accum, argnum, code, nargs, args)
2602 double accum;
2603 register int argnum;
2604 enum arithop code;
2605 int nargs;
2606 register Lisp_Object *args;
2607{
2608 register Lisp_Object val;
2609 double next;
7403b5c8 2610
7921925c
JB
2611 for (; argnum < nargs; argnum++)
2612 {
2613 val = args[argnum]; /* using args[argnum] as argument to CHECK_NUMBER_... */
b7826503 2614 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val);
7921925c 2615
e9ebc175 2616 if (FLOATP (val))
7921925c 2617 {
7539e11f 2618 next = XFLOAT_DATA (val);
7921925c
JB
2619 }
2620 else
2621 {
2622 args[argnum] = val; /* runs into a compiler bug. */
2623 next = XINT (args[argnum]);
2624 }
0220c518 2625 switch (SWITCH_ENUM_CAST (code))
7921925c
JB
2626 {
2627 case Aadd:
2628 accum += next;
2629 break;
2630 case Asub:
e64981da 2631 accum = argnum ? accum - next : nargs == 1 ? - next : next;
7921925c
JB
2632 break;
2633 case Amult:
2634 accum *= next;
2635 break;
2636 case Adiv:
2637 if (!argnum)
2638 accum = next;
2639 else
87fbf902 2640 {
ad8d56b9 2641 if (! IEEE_FLOATING_POINT && next == 0)
740ef0b5 2642 xsignal0 (Qarith_error);
87fbf902
RS
2643 accum /= next;
2644 }
7921925c
JB
2645 break;
2646 case Alogand:
2647 case Alogior:
2648 case Alogxor:
2649 return wrong_type_argument (Qinteger_or_marker_p, val);
2650 case Amax:
1a2f2d33 2651 if (!argnum || isnan (next) || next > accum)
7921925c
JB
2652 accum = next;
2653 break;
2654 case Amin:
1a2f2d33 2655 if (!argnum || isnan (next) || next < accum)
7921925c
JB
2656 accum = next;
2657 break;
2658 }
2659 }
2660
2661 return make_float (accum);
2662}
cc94f3b2 2663
7921925c
JB
2664
2665DEFUN ("+", Fplus, Splus, 0, MANY, 0,
8c1a1077
PJ
2666 doc: /* Return sum of any number of arguments, which are numbers or markers.
2667usage: (+ &rest NUMBERS-OR-MARKERS) */)
2668 (nargs, args)
7921925c
JB
2669 int nargs;
2670 Lisp_Object *args;
2671{
2672 return arith_driver (Aadd, nargs, args);
2673}
2674
2675DEFUN ("-", Fminus, Sminus, 0, MANY, 0,
558ee900 2676 doc: /* Negate number or subtract numbers or markers and return the result.
8c1a1077 2677With one arg, negates it. With more than one arg,
f44fba9e 2678subtracts all but the first from the first.
8c1a1077
PJ
2679usage: (- &optional NUMBER-OR-MARKER &rest MORE-NUMBERS-OR-MARKERS) */)
2680 (nargs, args)
7921925c
JB
2681 int nargs;
2682 Lisp_Object *args;
2683{
2684 return arith_driver (Asub, nargs, args);
2685}
2686
2687DEFUN ("*", Ftimes, Stimes, 0, MANY, 0,
be24eadf 2688 doc: /* Return product of any number of arguments, which are numbers or markers.
8c1a1077
PJ
2689usage: (* &rest NUMBERS-OR-MARKERS) */)
2690 (nargs, args)
7921925c
JB
2691 int nargs;
2692 Lisp_Object *args;
2693{
2694 return arith_driver (Amult, nargs, args);
2695}
2696
2697DEFUN ("/", Fquo, Squo, 2, MANY, 0,
be24eadf 2698 doc: /* Return first argument divided by all the remaining arguments.
f44fba9e 2699The arguments must be numbers or markers.
8c1a1077
PJ
2700usage: (/ DIVIDEND DIVISOR &rest DIVISORS) */)
2701 (nargs, args)
7921925c
JB
2702 int nargs;
2703 Lisp_Object *args;
2704{
28712a21 2705 int argnum;
7ef98053 2706 for (argnum = 2; argnum < nargs; argnum++)
28712a21
JB
2707 if (FLOATP (args[argnum]))
2708 return float_arith_driver (0, 0, Adiv, nargs, args);
7921925c
JB
2709 return arith_driver (Adiv, nargs, args);
2710}
2711
2712DEFUN ("%", Frem, Srem, 2, 2, 0,
be24eadf 2713 doc: /* Return remainder of X divided by Y.
8c1a1077
PJ
2714Both must be integers or markers. */)
2715 (x, y)
d9c2a0f2 2716 register Lisp_Object x, y;
7921925c
JB
2717{
2718 Lisp_Object val;
2719
b7826503
PJ
2720 CHECK_NUMBER_COERCE_MARKER (x);
2721 CHECK_NUMBER_COERCE_MARKER (y);
7921925c 2722
d9c2a0f2 2723 if (XFASTINT (y) == 0)
740ef0b5 2724 xsignal0 (Qarith_error);
87fbf902 2725
d9c2a0f2 2726 XSETINT (val, XINT (x) % XINT (y));
7921925c
JB
2727 return val;
2728}
2729
1d66a5fa
KH
2730#ifndef HAVE_FMOD
2731double
2732fmod (f1, f2)
2733 double f1, f2;
2734{
bc1c9d7e
PE
2735 double r = f1;
2736
fa43b1e8
KH
2737 if (f2 < 0.0)
2738 f2 = -f2;
bc1c9d7e
PE
2739
2740 /* If the magnitude of the result exceeds that of the divisor, or
2741 the sign of the result does not agree with that of the dividend,
2742 iterate with the reduced value. This does not yield a
2743 particularly accurate result, but at least it will be in the
2744 range promised by fmod. */
2745 do
2746 r -= f2 * floor (r / f2);
2747 while (f2 <= (r < 0 ? -r : r) || ((r < 0) != (f1 < 0) && ! isnan (r)));
2748
2749 return r;
1d66a5fa
KH
2750}
2751#endif /* ! HAVE_FMOD */
2752
44fa9da5 2753DEFUN ("mod", Fmod, Smod, 2, 2, 0,
be24eadf 2754 doc: /* Return X modulo Y.
8c1a1077
PJ
2755The result falls between zero (inclusive) and Y (exclusive).
2756Both X and Y must be numbers or markers. */)
2757 (x, y)
d9c2a0f2 2758 register Lisp_Object x, y;
44fa9da5
PE
2759{
2760 Lisp_Object val;
5260234d 2761 EMACS_INT i1, i2;
44fa9da5 2762
b7826503
PJ
2763 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (x);
2764 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (y);
44fa9da5 2765
d9c2a0f2 2766 if (FLOATP (x) || FLOATP (y))
ad8d56b9
PE
2767 return fmod_float (x, y);
2768
d9c2a0f2
EN
2769 i1 = XINT (x);
2770 i2 = XINT (y);
44fa9da5
PE
2771
2772 if (i2 == 0)
740ef0b5 2773 xsignal0 (Qarith_error);
7403b5c8 2774
44fa9da5
PE
2775 i1 %= i2;
2776
2777 /* If the "remainder" comes out with the wrong sign, fix it. */
04f7ec69 2778 if (i2 < 0 ? i1 > 0 : i1 < 0)
44fa9da5
PE
2779 i1 += i2;
2780
f187f1f7 2781 XSETINT (val, i1);
44fa9da5
PE
2782 return val;
2783}
2784
7921925c 2785DEFUN ("max", Fmax, Smax, 1, MANY, 0,
8c1a1077 2786 doc: /* Return largest of all the arguments (which must be numbers or markers).
f44fba9e 2787The value is always a number; markers are converted to numbers.
8c1a1077
PJ
2788usage: (max NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2789 (nargs, args)
7921925c
JB
2790 int nargs;
2791 Lisp_Object *args;
2792{
2793 return arith_driver (Amax, nargs, args);
2794}
2795
2796DEFUN ("min", Fmin, Smin, 1, MANY, 0,
8c1a1077 2797 doc: /* Return smallest of all the arguments (which must be numbers or markers).
f44fba9e 2798The value is always a number; markers are converted to numbers.
8c1a1077
PJ
2799usage: (min NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2800 (nargs, args)
7921925c
JB
2801 int nargs;
2802 Lisp_Object *args;
2803{
2804 return arith_driver (Amin, nargs, args);
2805}
2806
2807DEFUN ("logand", Flogand, Slogand, 0, MANY, 0,
8c1a1077 2808 doc: /* Return bitwise-and of all the arguments.
f44fba9e 2809Arguments may be integers, or markers converted to integers.
8c1a1077
PJ
2810usage: (logand &rest INTS-OR-MARKERS) */)
2811 (nargs, args)
7921925c
JB
2812 int nargs;
2813 Lisp_Object *args;
2814{
2815 return arith_driver (Alogand, nargs, args);
2816}
2817
2818DEFUN ("logior", Flogior, Slogior, 0, MANY, 0,
8c1a1077 2819 doc: /* Return bitwise-or of all the arguments.
f44fba9e 2820Arguments may be integers, or markers converted to integers.
8c1a1077
PJ
2821usage: (logior &rest INTS-OR-MARKERS) */)
2822 (nargs, args)
7921925c
JB
2823 int nargs;
2824 Lisp_Object *args;
2825{
2826 return arith_driver (Alogior, nargs, args);
2827}
2828
2829DEFUN ("logxor", Flogxor, Slogxor, 0, MANY, 0,
8c1a1077 2830 doc: /* Return bitwise-exclusive-or of all the arguments.
f44fba9e 2831Arguments may be integers, or markers converted to integers.
31fb1b2c 2832usage: (logxor &rest INTS-OR-MARKERS) */)
8c1a1077 2833 (nargs, args)
7921925c
JB
2834 int nargs;
2835 Lisp_Object *args;
2836{
2837 return arith_driver (Alogxor, nargs, args);
2838}
2839
2840DEFUN ("ash", Fash, Sash, 2, 2, 0,
8c1a1077
PJ
2841 doc: /* Return VALUE with its bits shifted left by COUNT.
2842If COUNT is negative, shifting is actually to the right.
2843In this case, the sign bit is duplicated. */)
2844 (value, count)
3b9f7964 2845 register Lisp_Object value, count;
7921925c
JB
2846{
2847 register Lisp_Object val;
2848
b7826503
PJ
2849 CHECK_NUMBER (value);
2850 CHECK_NUMBER (count);
7921925c 2851
81d70626
RS
2852 if (XINT (count) >= BITS_PER_EMACS_INT)
2853 XSETINT (val, 0);
2854 else if (XINT (count) > 0)
3d9652eb 2855 XSETINT (val, XINT (value) << XFASTINT (count));
81d70626
RS
2856 else if (XINT (count) <= -BITS_PER_EMACS_INT)
2857 XSETINT (val, XINT (value) < 0 ? -1 : 0);
7921925c 2858 else
3d9652eb 2859 XSETINT (val, XINT (value) >> -XINT (count));
7921925c
JB
2860 return val;
2861}
2862
2863DEFUN ("lsh", Flsh, Slsh, 2, 2, 0,
8c1a1077
PJ
2864 doc: /* Return VALUE with its bits shifted left by COUNT.
2865If COUNT is negative, shifting is actually to the right.
3a9b1297 2866In this case, zeros are shifted in on the left. */)
8c1a1077 2867 (value, count)
3d9652eb 2868 register Lisp_Object value, count;
7921925c
JB
2869{
2870 register Lisp_Object val;
2871
b7826503
PJ
2872 CHECK_NUMBER (value);
2873 CHECK_NUMBER (count);
7921925c 2874
81d70626
RS
2875 if (XINT (count) >= BITS_PER_EMACS_INT)
2876 XSETINT (val, 0);
2877 else if (XINT (count) > 0)
3d9652eb 2878 XSETINT (val, (EMACS_UINT) XUINT (value) << XFASTINT (count));
81d70626
RS
2879 else if (XINT (count) <= -BITS_PER_EMACS_INT)
2880 XSETINT (val, 0);
7921925c 2881 else
3d9652eb 2882 XSETINT (val, (EMACS_UINT) XUINT (value) >> -XINT (count));
7921925c
JB
2883 return val;
2884}
2885
2886DEFUN ("1+", Fadd1, Sadd1, 1, 1, 0,
8c1a1077
PJ
2887 doc: /* Return NUMBER plus one. NUMBER may be a number or a marker.
2888Markers are converted to integers. */)
2889 (number)
d9c2a0f2 2890 register Lisp_Object number;
7921925c 2891{
b7826503 2892 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number);
7921925c 2893
d9c2a0f2 2894 if (FLOATP (number))
7539e11f 2895 return (make_float (1.0 + XFLOAT_DATA (number)));
7921925c 2896
d9c2a0f2
EN
2897 XSETINT (number, XINT (number) + 1);
2898 return number;
7921925c
JB
2899}
2900
2901DEFUN ("1-", Fsub1, Ssub1, 1, 1, 0,
8c1a1077
PJ
2902 doc: /* Return NUMBER minus one. NUMBER may be a number or a marker.
2903Markers are converted to integers. */)
2904 (number)
d9c2a0f2 2905 register Lisp_Object number;
7921925c 2906{
b7826503 2907 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number);
7921925c 2908
d9c2a0f2 2909 if (FLOATP (number))
7539e11f 2910 return (make_float (-1.0 + XFLOAT_DATA (number)));
7921925c 2911
d9c2a0f2
EN
2912 XSETINT (number, XINT (number) - 1);
2913 return number;
7921925c
JB
2914}
2915
2916DEFUN ("lognot", Flognot, Slognot, 1, 1, 0,
8c1a1077
PJ
2917 doc: /* Return the bitwise complement of NUMBER. NUMBER must be an integer. */)
2918 (number)
d9c2a0f2 2919 register Lisp_Object number;
7921925c 2920{
b7826503 2921 CHECK_NUMBER (number);
53924017 2922 XSETINT (number, ~XINT (number));
d9c2a0f2 2923 return number;
7921925c 2924}
d4be0d31
JD
2925
2926DEFUN ("byteorder", Fbyteorder, Sbyteorder, 0, 0, 0,
2927 doc: /* Return the byteorder for the machine.
2928Returns 66 (ASCII uppercase B) for big endian machines or 108 (ASCII
2929lowercase l) for small endian machines. */)
2930 ()
2931{
2932 unsigned i = 0x04030201;
7a8560d7 2933 int order = *(char *)&i == 1 ? 108 : 66;
d4be0d31 2934
d2f14999 2935 return make_number (order);
d4be0d31
JD
2936}
2937
2938
7921925c
JB
2939\f
2940void
2941syms_of_data ()
2942{
6315e761
RS
2943 Lisp_Object error_tail, arith_tail;
2944
7921925c
JB
2945 Qquote = intern ("quote");
2946 Qlambda = intern ("lambda");
2947 Qsubr = intern ("subr");
2948 Qerror_conditions = intern ("error-conditions");
2949 Qerror_message = intern ("error-message");
2950 Qtop_level = intern ("top-level");
2951
2952 Qerror = intern ("error");
2953 Qquit = intern ("quit");
2954 Qwrong_type_argument = intern ("wrong-type-argument");
2955 Qargs_out_of_range = intern ("args-out-of-range");
2956 Qvoid_function = intern ("void-function");
ffd56f97 2957 Qcyclic_function_indirection = intern ("cyclic-function-indirection");
f35d5bad 2958 Qcyclic_variable_indirection = intern ("cyclic-variable-indirection");
7921925c
JB
2959 Qvoid_variable = intern ("void-variable");
2960 Qsetting_constant = intern ("setting-constant");
2961 Qinvalid_read_syntax = intern ("invalid-read-syntax");
2962
2963 Qinvalid_function = intern ("invalid-function");
2964 Qwrong_number_of_arguments = intern ("wrong-number-of-arguments");
2965 Qno_catch = intern ("no-catch");
2966 Qend_of_file = intern ("end-of-file");
2967 Qarith_error = intern ("arith-error");
2968 Qbeginning_of_buffer = intern ("beginning-of-buffer");
2969 Qend_of_buffer = intern ("end-of-buffer");
2970 Qbuffer_read_only = intern ("buffer-read-only");
8f9f49d7 2971 Qtext_read_only = intern ("text-read-only");
3b8819d6 2972 Qmark_inactive = intern ("mark-inactive");
7921925c
JB
2973
2974 Qlistp = intern ("listp");
2975 Qconsp = intern ("consp");
2976 Qsymbolp = intern ("symbolp");
cda9b832 2977 Qkeywordp = intern ("keywordp");
7921925c
JB
2978 Qintegerp = intern ("integerp");
2979 Qnatnump = intern ("natnump");
8e86942b 2980 Qwholenump = intern ("wholenump");
7921925c
JB
2981 Qstringp = intern ("stringp");
2982 Qarrayp = intern ("arrayp");
2983 Qsequencep = intern ("sequencep");
2984 Qbufferp = intern ("bufferp");
2985 Qvectorp = intern ("vectorp");
2986 Qchar_or_string_p = intern ("char-or-string-p");
2987 Qmarkerp = intern ("markerp");
07bd8472 2988 Qbuffer_or_string_p = intern ("buffer-or-string-p");
7921925c
JB
2989 Qinteger_or_marker_p = intern ("integer-or-marker-p");
2990 Qboundp = intern ("boundp");
2991 Qfboundp = intern ("fboundp");
2992
7921925c
JB
2993 Qfloatp = intern ("floatp");
2994 Qnumberp = intern ("numberp");
2995 Qnumber_or_marker_p = intern ("number-or-marker-p");
7921925c 2996
4d276982 2997 Qchar_table_p = intern ("char-table-p");
7f0edce7 2998 Qvector_or_char_table_p = intern ("vector-or-char-table-p");
4d276982 2999
6f0e897f
DL
3000 Qsubrp = intern ("subrp");
3001 Qunevalled = intern ("unevalled");
3002 Qmany = intern ("many");
3003
7921925c
JB
3004 Qcdr = intern ("cdr");
3005
f845f2c9 3006 /* Handle automatic advice activation */
ab297811 3007 Qad_advice_info = intern ("ad-advice-info");
c1307a23 3008 Qad_activate_internal = intern ("ad-activate-internal");
f845f2c9 3009
6315e761
RS
3010 error_tail = Fcons (Qerror, Qnil);
3011
7921925c
JB
3012 /* ERROR is used as a signaler for random errors for which nothing else is right */
3013
3014 Fput (Qerror, Qerror_conditions,
6315e761 3015 error_tail);
7921925c
JB
3016 Fput (Qerror, Qerror_message,
3017 build_string ("error"));
3018
3019 Fput (Qquit, Qerror_conditions,
3020 Fcons (Qquit, Qnil));
3021 Fput (Qquit, Qerror_message,
3022 build_string ("Quit"));
3023
3024 Fput (Qwrong_type_argument, Qerror_conditions,
6315e761 3025 Fcons (Qwrong_type_argument, error_tail));
7921925c
JB
3026 Fput (Qwrong_type_argument, Qerror_message,
3027 build_string ("Wrong type argument"));
3028
3029 Fput (Qargs_out_of_range, Qerror_conditions,
6315e761 3030 Fcons (Qargs_out_of_range, error_tail));
7921925c
JB
3031 Fput (Qargs_out_of_range, Qerror_message,
3032 build_string ("Args out of range"));
3033
3034 Fput (Qvoid_function, Qerror_conditions,
6315e761 3035 Fcons (Qvoid_function, error_tail));
7921925c
JB
3036 Fput (Qvoid_function, Qerror_message,
3037 build_string ("Symbol's function definition is void"));
3038
ffd56f97 3039 Fput (Qcyclic_function_indirection, Qerror_conditions,
6315e761 3040 Fcons (Qcyclic_function_indirection, error_tail));
ffd56f97
JB
3041 Fput (Qcyclic_function_indirection, Qerror_message,
3042 build_string ("Symbol's chain of function indirections contains a loop"));
3043
f35d5bad
GM
3044 Fput (Qcyclic_variable_indirection, Qerror_conditions,
3045 Fcons (Qcyclic_variable_indirection, error_tail));
3046 Fput (Qcyclic_variable_indirection, Qerror_message,
3047 build_string ("Symbol's chain of variable indirections contains a loop"));
3048
13d95cc0
GM
3049 Qcircular_list = intern ("circular-list");
3050 staticpro (&Qcircular_list);
3051 Fput (Qcircular_list, Qerror_conditions,
3052 Fcons (Qcircular_list, error_tail));
3053 Fput (Qcircular_list, Qerror_message,
3054 build_string ("List contains a loop"));
3055
7921925c 3056 Fput (Qvoid_variable, Qerror_conditions,
6315e761 3057 Fcons (Qvoid_variable, error_tail));
7921925c
JB
3058 Fput (Qvoid_variable, Qerror_message,
3059 build_string ("Symbol's value as variable is void"));
3060
3061 Fput (Qsetting_constant, Qerror_conditions,
6315e761 3062 Fcons (Qsetting_constant, error_tail));
7921925c
JB
3063 Fput (Qsetting_constant, Qerror_message,
3064 build_string ("Attempt to set a constant symbol"));
3065
3066 Fput (Qinvalid_read_syntax, Qerror_conditions,
6315e761 3067 Fcons (Qinvalid_read_syntax, error_tail));
7921925c
JB
3068 Fput (Qinvalid_read_syntax, Qerror_message,
3069 build_string ("Invalid read syntax"));
3070
3071 Fput (Qinvalid_function, Qerror_conditions,
6315e761 3072 Fcons (Qinvalid_function, error_tail));
7921925c
JB
3073 Fput (Qinvalid_function, Qerror_message,
3074 build_string ("Invalid function"));
3075
3076 Fput (Qwrong_number_of_arguments, Qerror_conditions,
6315e761 3077 Fcons (Qwrong_number_of_arguments, error_tail));
7921925c
JB
3078 Fput (Qwrong_number_of_arguments, Qerror_message,
3079 build_string ("Wrong number of arguments"));
3080
3081 Fput (Qno_catch, Qerror_conditions,
6315e761 3082 Fcons (Qno_catch, error_tail));
7921925c
JB
3083 Fput (Qno_catch, Qerror_message,
3084 build_string ("No catch for tag"));
3085
3086 Fput (Qend_of_file, Qerror_conditions,
6315e761 3087 Fcons (Qend_of_file, error_tail));
7921925c
JB
3088 Fput (Qend_of_file, Qerror_message,
3089 build_string ("End of file during parsing"));
3090
6315e761 3091 arith_tail = Fcons (Qarith_error, error_tail);
7921925c 3092 Fput (Qarith_error, Qerror_conditions,
6315e761 3093 arith_tail);
7921925c
JB
3094 Fput (Qarith_error, Qerror_message,
3095 build_string ("Arithmetic error"));
3096
3097 Fput (Qbeginning_of_buffer, Qerror_conditions,
6315e761 3098 Fcons (Qbeginning_of_buffer, error_tail));
7921925c
JB
3099 Fput (Qbeginning_of_buffer, Qerror_message,
3100 build_string ("Beginning of buffer"));
3101
3102 Fput (Qend_of_buffer, Qerror_conditions,
6315e761 3103 Fcons (Qend_of_buffer, error_tail));
7921925c
JB
3104 Fput (Qend_of_buffer, Qerror_message,
3105 build_string ("End of buffer"));
3106
3107 Fput (Qbuffer_read_only, Qerror_conditions,
6315e761 3108 Fcons (Qbuffer_read_only, error_tail));
7921925c
JB
3109 Fput (Qbuffer_read_only, Qerror_message,
3110 build_string ("Buffer is read-only"));
3111
8f9f49d7
GM
3112 Fput (Qtext_read_only, Qerror_conditions,
3113 Fcons (Qtext_read_only, error_tail));
3114 Fput (Qtext_read_only, Qerror_message,
3115 build_string ("Text is read-only"));
3116
6315e761
RS
3117 Qrange_error = intern ("range-error");
3118 Qdomain_error = intern ("domain-error");
3119 Qsingularity_error = intern ("singularity-error");
3120 Qoverflow_error = intern ("overflow-error");
3121 Qunderflow_error = intern ("underflow-error");
3122
3123 Fput (Qdomain_error, Qerror_conditions,
3124 Fcons (Qdomain_error, arith_tail));
3125 Fput (Qdomain_error, Qerror_message,
3126 build_string ("Arithmetic domain error"));
3127
3128 Fput (Qrange_error, Qerror_conditions,
3129 Fcons (Qrange_error, arith_tail));
3130 Fput (Qrange_error, Qerror_message,
3131 build_string ("Arithmetic range error"));
3132
3133 Fput (Qsingularity_error, Qerror_conditions,
3134 Fcons (Qsingularity_error, Fcons (Qdomain_error, arith_tail)));
3135 Fput (Qsingularity_error, Qerror_message,
3136 build_string ("Arithmetic singularity error"));
3137
3138 Fput (Qoverflow_error, Qerror_conditions,
3139 Fcons (Qoverflow_error, Fcons (Qdomain_error, arith_tail)));
3140 Fput (Qoverflow_error, Qerror_message,
3141 build_string ("Arithmetic overflow error"));
3142
3143 Fput (Qunderflow_error, Qerror_conditions,
3144 Fcons (Qunderflow_error, Fcons (Qdomain_error, arith_tail)));
3145 Fput (Qunderflow_error, Qerror_message,
3146 build_string ("Arithmetic underflow error"));
3147
3148 staticpro (&Qrange_error);
3149 staticpro (&Qdomain_error);
3150 staticpro (&Qsingularity_error);
3151 staticpro (&Qoverflow_error);
3152 staticpro (&Qunderflow_error);
6315e761 3153
7921925c
JB
3154 staticpro (&Qnil);
3155 staticpro (&Qt);
3156 staticpro (&Qquote);
3157 staticpro (&Qlambda);
3158 staticpro (&Qsubr);
3159 staticpro (&Qunbound);
3160 staticpro (&Qerror_conditions);
3161 staticpro (&Qerror_message);
3162 staticpro (&Qtop_level);
3163
3164 staticpro (&Qerror);
3165 staticpro (&Qquit);
3166 staticpro (&Qwrong_type_argument);
3167 staticpro (&Qargs_out_of_range);
3168 staticpro (&Qvoid_function);
ffd56f97 3169 staticpro (&Qcyclic_function_indirection);
dfad0b34 3170 staticpro (&Qcyclic_variable_indirection);
7921925c
JB
3171 staticpro (&Qvoid_variable);
3172 staticpro (&Qsetting_constant);
3173 staticpro (&Qinvalid_read_syntax);
3174 staticpro (&Qwrong_number_of_arguments);
3175 staticpro (&Qinvalid_function);
3176 staticpro (&Qno_catch);
3177 staticpro (&Qend_of_file);
3178 staticpro (&Qarith_error);
3179 staticpro (&Qbeginning_of_buffer);
3180 staticpro (&Qend_of_buffer);
3181 staticpro (&Qbuffer_read_only);
8f9f49d7 3182 staticpro (&Qtext_read_only);
638b77e6 3183 staticpro (&Qmark_inactive);
7921925c
JB
3184
3185 staticpro (&Qlistp);
3186 staticpro (&Qconsp);
3187 staticpro (&Qsymbolp);
cda9b832 3188 staticpro (&Qkeywordp);
7921925c
JB
3189 staticpro (&Qintegerp);
3190 staticpro (&Qnatnump);
8e86942b 3191 staticpro (&Qwholenump);
7921925c
JB
3192 staticpro (&Qstringp);
3193 staticpro (&Qarrayp);
3194 staticpro (&Qsequencep);
3195 staticpro (&Qbufferp);
3196 staticpro (&Qvectorp);
3197 staticpro (&Qchar_or_string_p);
3198 staticpro (&Qmarkerp);
07bd8472 3199 staticpro (&Qbuffer_or_string_p);
7921925c 3200 staticpro (&Qinteger_or_marker_p);
7921925c 3201 staticpro (&Qfloatp);
464f8898
RS
3202 staticpro (&Qnumberp);
3203 staticpro (&Qnumber_or_marker_p);
4d276982 3204 staticpro (&Qchar_table_p);
7f0edce7 3205 staticpro (&Qvector_or_char_table_p);
6f0e897f
DL
3206 staticpro (&Qsubrp);
3207 staticpro (&Qmany);
3208 staticpro (&Qunevalled);
7921925c
JB
3209
3210 staticpro (&Qboundp);
3211 staticpro (&Qfboundp);
3212 staticpro (&Qcdr);
ab297811 3213 staticpro (&Qad_advice_info);
c1307a23 3214 staticpro (&Qad_activate_internal);
7921925c 3215
39bcc759
RS
3216 /* Types that type-of returns. */
3217 Qinteger = intern ("integer");
3218 Qsymbol = intern ("symbol");
3219 Qstring = intern ("string");
3220 Qcons = intern ("cons");
3221 Qmarker = intern ("marker");
3222 Qoverlay = intern ("overlay");
3223 Qfloat = intern ("float");
3224 Qwindow_configuration = intern ("window-configuration");
3225 Qprocess = intern ("process");
3226 Qwindow = intern ("window");
3227 /* Qsubr = intern ("subr"); */
3228 Qcompiled_function = intern ("compiled-function");
3229 Qbuffer = intern ("buffer");
3230 Qframe = intern ("frame");
3231 Qvector = intern ("vector");
fc67d5be
KH
3232 Qchar_table = intern ("char-table");
3233 Qbool_vector = intern ("bool-vector");
81dc5de5 3234 Qhash_table = intern ("hash-table");
39bcc759
RS
3235
3236 staticpro (&Qinteger);
3237 staticpro (&Qsymbol);
3238 staticpro (&Qstring);
3239 staticpro (&Qcons);
3240 staticpro (&Qmarker);
3241 staticpro (&Qoverlay);
3242 staticpro (&Qfloat);
3243 staticpro (&Qwindow_configuration);
3244 staticpro (&Qprocess);
3245 staticpro (&Qwindow);
3246 /* staticpro (&Qsubr); */
3247 staticpro (&Qcompiled_function);
3248 staticpro (&Qbuffer);
3249 staticpro (&Qframe);
3250 staticpro (&Qvector);
fc67d5be
KH
3251 staticpro (&Qchar_table);
3252 staticpro (&Qbool_vector);
81dc5de5 3253 staticpro (&Qhash_table);
39bcc759 3254
f35d5bad 3255 defsubr (&Sindirect_variable);
f52a3ca3 3256 defsubr (&Sinteractive_form);
7921925c
JB
3257 defsubr (&Seq);
3258 defsubr (&Snull);
39bcc759 3259 defsubr (&Stype_of);
7921925c
JB
3260 defsubr (&Slistp);
3261 defsubr (&Snlistp);
3262 defsubr (&Sconsp);
3263 defsubr (&Satom);
3264 defsubr (&Sintegerp);
464f8898 3265 defsubr (&Sinteger_or_marker_p);
7921925c
JB
3266 defsubr (&Snumberp);
3267 defsubr (&Snumber_or_marker_p);
464f8898 3268 defsubr (&Sfloatp);
7921925c
JB
3269 defsubr (&Snatnump);
3270 defsubr (&Ssymbolp);
cda9b832 3271 defsubr (&Skeywordp);
7921925c 3272 defsubr (&Sstringp);
0f56470d 3273 defsubr (&Smultibyte_string_p);
7921925c 3274 defsubr (&Svectorp);
4d276982 3275 defsubr (&Schar_table_p);
7f0edce7 3276 defsubr (&Svector_or_char_table_p);
4d276982 3277 defsubr (&Sbool_vector_p);
7921925c
JB
3278 defsubr (&Sarrayp);
3279 defsubr (&Ssequencep);
3280 defsubr (&Sbufferp);
3281 defsubr (&Smarkerp);
7921925c 3282 defsubr (&Ssubrp);
dbc4e1c1 3283 defsubr (&Sbyte_code_function_p);
7921925c
JB
3284 defsubr (&Schar_or_string_p);
3285 defsubr (&Scar);
3286 defsubr (&Scdr);
3287 defsubr (&Scar_safe);
3288 defsubr (&Scdr_safe);
3289 defsubr (&Ssetcar);
3290 defsubr (&Ssetcdr);
3291 defsubr (&Ssymbol_function);
ffd56f97 3292 defsubr (&Sindirect_function);
7921925c
JB
3293 defsubr (&Ssymbol_plist);
3294 defsubr (&Ssymbol_name);
3295 defsubr (&Smakunbound);
3296 defsubr (&Sfmakunbound);
3297 defsubr (&Sboundp);
3298 defsubr (&Sfboundp);
3299 defsubr (&Sfset);
80df38a2 3300 defsubr (&Sdefalias);
7921925c
JB
3301 defsubr (&Ssetplist);
3302 defsubr (&Ssymbol_value);
3303 defsubr (&Sset);
3304 defsubr (&Sdefault_boundp);
3305 defsubr (&Sdefault_value);
3306 defsubr (&Sset_default);
3307 defsubr (&Ssetq_default);
3308 defsubr (&Smake_variable_buffer_local);
3309 defsubr (&Smake_local_variable);
3310 defsubr (&Skill_local_variable);
b0c2d1c6 3311 defsubr (&Smake_variable_frame_local);
62476adc 3312 defsubr (&Slocal_variable_p);
f4f04cee 3313 defsubr (&Slocal_variable_if_set_p);
0a2546d4 3314 defsubr (&Svariable_binding_locus);
7921925c
JB
3315 defsubr (&Saref);
3316 defsubr (&Saset);
f2980264 3317 defsubr (&Snumber_to_string);
25e40a4b 3318 defsubr (&Sstring_to_number);
7921925c
JB
3319 defsubr (&Seqlsign);
3320 defsubr (&Slss);
3321 defsubr (&Sgtr);
3322 defsubr (&Sleq);
3323 defsubr (&Sgeq);
3324 defsubr (&Sneq);
3325 defsubr (&Szerop);
3326 defsubr (&Splus);
3327 defsubr (&Sminus);
3328 defsubr (&Stimes);
3329 defsubr (&Squo);
3330 defsubr (&Srem);
44fa9da5 3331 defsubr (&Smod);
7921925c
JB
3332 defsubr (&Smax);
3333 defsubr (&Smin);
3334 defsubr (&Slogand);
3335 defsubr (&Slogior);
3336 defsubr (&Slogxor);
3337 defsubr (&Slsh);
3338 defsubr (&Sash);
3339 defsubr (&Sadd1);
3340 defsubr (&Ssub1);
3341 defsubr (&Slognot);
d4be0d31 3342 defsubr (&Sbyteorder);
6f0e897f 3343 defsubr (&Ssubr_arity);
0fddae66 3344 defsubr (&Ssubr_name);
8e86942b 3345
c80bd143 3346 XSYMBOL (Qwholenump)->function = XSYMBOL (Qnatnump)->function;
e6190b11 3347
9d113d9d
AS
3348 DEFVAR_LISP ("most-positive-fixnum", &Vmost_positive_fixnum,
3349 doc: /* The largest value that is representable in a Lisp integer. */);
3350 Vmost_positive_fixnum = make_number (MOST_POSITIVE_FIXNUM);
bfb96cb7 3351
9d113d9d
AS
3352 DEFVAR_LISP ("most-negative-fixnum", &Vmost_negative_fixnum,
3353 doc: /* The smallest value that is representable in a Lisp integer. */);
3354 Vmost_negative_fixnum = make_number (MOST_NEGATIVE_FIXNUM);
7921925c
JB
3355}
3356
a33ef3ab 3357SIGTYPE
7921925c
JB
3358arith_error (signo)
3359 int signo;
3360{
fe42a920 3361#if defined(USG) && !defined(POSIX_SIGNALS)
7921925c
JB
3362 /* USG systems forget handlers when they are used;
3363 must reestablish each time */
3364 signal (signo, arith_error);
3365#endif /* USG */
3366#ifdef VMS
3367 /* VMS systems are like USG. */
3368 signal (signo, arith_error);
3369#endif /* VMS */
3370#ifdef BSD4_1
3371 sigrelse (SIGFPE);
3372#else /* not BSD4_1 */
e065a56e 3373 sigsetmask (SIGEMPTYMASK);
7921925c
JB
3374#endif /* not BSD4_1 */
3375
333f1b6f 3376 SIGNAL_THREAD_CHECK (signo);
740ef0b5 3377 xsignal0 (Qarith_error);
7921925c
JB
3378}
3379
dfcf069d 3380void
7921925c
JB
3381init_data ()
3382{
3383 /* Don't do this if just dumping out.
3384 We don't want to call `signal' in this case
3385 so that we don't have trouble with dumping
3386 signal-delivering routines in an inconsistent state. */
3387#ifndef CANNOT_DUMP
3388 if (!initialized)
3389 return;
3390#endif /* CANNOT_DUMP */
3391 signal (SIGFPE, arith_error);
7403b5c8 3392
7921925c
JB
3393#ifdef uts
3394 signal (SIGEMT, arith_error);
3395#endif /* uts */
3396}
ab5796a9
MB
3397
3398/* arch-tag: 25879798-b84d-479a-9c89-7d148e2109f7
3399 (do not change this comment) */