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