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