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