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