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