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