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