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