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