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