9f0b9a3e6ec2ff47627c54a14e452755d72a4ecb
[bpt/emacs.git] / src / data.c
1 /* Primitive operations on Lisp data types for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985-1986, 1988, 1993-1995, 1997-2014 Free Software
3 Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20
21 #include <config.h>
22 #include <stdio.h>
23
24 #include <byteswap.h>
25 #include <count-one-bits.h>
26 #include <count-trailing-zeros.h>
27 #include <intprops.h>
28
29 #include "lisp.h"
30 #include "puresize.h"
31 #include "character.h"
32 #include "buffer.h"
33 #include "keyboard.h"
34 #include "frame.h"
35 #include "syssignal.h"
36 #include "termhooks.h" /* For FRAME_KBOARD reference in y-or-n-p. */
37 #include "font.h"
38 #include "keymap.h"
39
40 Lisp_Object Qnil, Qt, Qquote, Qlambda, Qunbound;
41 static Lisp_Object Qsubr;
42 Lisp_Object Qerror_conditions, Qerror_message, Qtop_level;
43 Lisp_Object Qerror, Quser_error, Qquit, Qargs_out_of_range;
44 static Lisp_Object Qwrong_length_argument;
45 static Lisp_Object Qwrong_type_argument;
46 Lisp_Object Qvoid_variable, Qvoid_function;
47 static Lisp_Object Qcyclic_function_indirection;
48 static Lisp_Object Qcyclic_variable_indirection;
49 Lisp_Object Qcircular_list;
50 static Lisp_Object Qsetting_constant;
51 Lisp_Object Qinvalid_read_syntax;
52 Lisp_Object Qinvalid_function, Qwrong_number_of_arguments, Qno_catch;
53 Lisp_Object Qend_of_file, Qarith_error, Qmark_inactive;
54 Lisp_Object Qbeginning_of_buffer, Qend_of_buffer, Qbuffer_read_only;
55 Lisp_Object Qtext_read_only;
56
57 Lisp_Object Qintegerp, Qwholenump, Qsymbolp, Qlistp, Qconsp;
58 static Lisp_Object Qnatnump;
59 Lisp_Object Qstringp, Qarrayp, Qsequencep, Qbufferp;
60 Lisp_Object Qchar_or_string_p, Qmarkerp, Qinteger_or_marker_p, Qvectorp;
61 Lisp_Object Qbool_vector_p;
62 Lisp_Object Qbuffer_or_string_p;
63 static Lisp_Object Qkeywordp, Qboundp;
64 Lisp_Object Qfboundp;
65 Lisp_Object Qchar_table_p, Qvector_or_char_table_p;
66
67 Lisp_Object Qcdr;
68 static Lisp_Object Qad_advice_info, Qad_activate_internal;
69
70 static Lisp_Object Qdomain_error, Qsingularity_error, Qunderflow_error;
71 Lisp_Object Qrange_error, Qoverflow_error;
72
73 Lisp_Object Qfloatp;
74 Lisp_Object Qnumberp, Qnumber_or_marker_p;
75
76 Lisp_Object Qinteger, Qsymbol;
77 static Lisp_Object Qcons, Qfloat, Qmisc, Qstring, Qvector;
78 Lisp_Object Qwindow;
79 static Lisp_Object Qoverlay, Qwindow_configuration;
80 static Lisp_Object Qprocess, Qmarker;
81 static Lisp_Object Qcompiled_function, Qframe;
82 Lisp_Object Qbuffer;
83 static Lisp_Object Qchar_table, Qbool_vector, Qhash_table;
84 static Lisp_Object Qsubrp;
85 static Lisp_Object Qmany, Qunevalled;
86 Lisp_Object Qfont_spec, Qfont_entity, Qfont_object;
87 static Lisp_Object Qdefun;
88
89 Lisp_Object Qinteractive_form;
90 static Lisp_Object Qdefalias_fset_function;
91
92 static void swap_in_symval_forwarding (struct Lisp_Symbol *, struct Lisp_Buffer_Local_Value *);
93
94 static bool
95 BOOLFWDP (union Lisp_Fwd *a)
96 {
97 return XFWDTYPE (a) == Lisp_Fwd_Bool;
98 }
99 static bool
100 INTFWDP (union Lisp_Fwd *a)
101 {
102 return XFWDTYPE (a) == Lisp_Fwd_Int;
103 }
104 static bool
105 KBOARD_OBJFWDP (union Lisp_Fwd *a)
106 {
107 return XFWDTYPE (a) == Lisp_Fwd_Kboard_Obj;
108 }
109 static bool
110 OBJFWDP (union Lisp_Fwd *a)
111 {
112 return XFWDTYPE (a) == Lisp_Fwd_Obj;
113 }
114
115 static struct Lisp_Boolfwd *
116 XBOOLFWD (union Lisp_Fwd *a)
117 {
118 eassert (BOOLFWDP (a));
119 return &a->u_boolfwd;
120 }
121 static struct Lisp_Kboard_Objfwd *
122 XKBOARD_OBJFWD (union Lisp_Fwd *a)
123 {
124 eassert (KBOARD_OBJFWDP (a));
125 return &a->u_kboard_objfwd;
126 }
127 static struct Lisp_Intfwd *
128 XINTFWD (union Lisp_Fwd *a)
129 {
130 eassert (INTFWDP (a));
131 return &a->u_intfwd;
132 }
133 static struct Lisp_Objfwd *
134 XOBJFWD (union Lisp_Fwd *a)
135 {
136 eassert (OBJFWDP (a));
137 return &a->u_objfwd;
138 }
139
140 static void
141 CHECK_SUBR (Lisp_Object x)
142 {
143 CHECK_TYPE (SUBRP (x), Qsubrp, x);
144 }
145
146 static void
147 set_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
153 static Lisp_Object
154 blv_value (struct Lisp_Buffer_Local_Value *blv)
155 {
156 return XCDR (blv->valcell);
157 }
158
159 static void
160 set_blv_value (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
161 {
162 XSETCDR (blv->valcell, val);
163 }
164
165 static void
166 set_blv_where (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
167 {
168 blv->where = val;
169 }
170
171 static void
172 set_blv_defcell (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
173 {
174 blv->defcell = val;
175 }
176
177 static void
178 set_blv_valcell (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
179 {
180 blv->valcell = val;
181 }
182
183 static _Noreturn void
184 wrong_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
195 Lisp_Object
196 wrong_type_argument (register Lisp_Object predicate, register Lisp_Object value)
197 {
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)
203 * emacs_abort (); */
204
205 xsignal2 (Qwrong_type_argument, predicate, value);
206 }
207
208 void
209 args_out_of_range (Lisp_Object a1, Lisp_Object a2)
210 {
211 xsignal2 (Qargs_out_of_range, a1, a2);
212 }
213
214 void
215 args_out_of_range_3 (Lisp_Object a1, Lisp_Object a2, Lisp_Object a3)
216 {
217 xsignal3 (Qargs_out_of_range, a1, a2, a3);
218 }
219
220 \f
221 /* Data type predicates. */
222
223 DEFUN ("eq", Feq, Seq, 2, 2, 0,
224 doc: /* Return t if the two args are the same Lisp object. */)
225 (Lisp_Object obj1, Lisp_Object obj2)
226 {
227 if (EQ (obj1, obj2))
228 return Qt;
229 return Qnil;
230 }
231
232 DEFUN ("null", Fnull, Snull, 1, 1, 0,
233 doc: /* Return t if OBJECT is nil. */)
234 (Lisp_Object object)
235 {
236 if (NILP (object))
237 return Qt;
238 return Qnil;
239 }
240
241 DEFUN ("type-of", Ftype_of, Stype_of, 1, 1, 0,
242 doc: /* Return a symbol representing the type of OBJECT.
243 The symbol returned names the object's basic type;
244 for example, (type-of 1) returns `integer'. */)
245 (Lisp_Object object)
246 {
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))
256 {
257 switch (XMISCTYPE (object))
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 }
266 emacs_abort ();
267 }
268 else if (VECTORLIKEP (object))
269 {
270 if (WINDOW_CONFIGURATIONP (object))
271 return Qwindow_configuration;
272 if (PROCESSP (object))
273 return Qprocess;
274 if (WINDOWP (object))
275 return Qwindow;
276 if (SUBRP (object))
277 return Qsubr;
278 if (COMPILEDP (object))
279 return Qcompiled_function;
280 if (BUFFERP (object))
281 return Qbuffer;
282 if (CHAR_TABLE_P (object))
283 return Qchar_table;
284 if (BOOL_VECTOR_P (object))
285 return Qbool_vector;
286 if (FRAMEP (object))
287 return Qframe;
288 if (HASH_TABLE_P (object))
289 return Qhash_table;
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;
296 return Qvector;
297 }
298 else if (FLOATP (object))
299 return Qfloat;
300 else
301 return Qt;
302 }
303
304 DEFUN ("consp", Fconsp, Sconsp, 1, 1, 0,
305 doc: /* Return t if OBJECT is a cons cell. */)
306 (Lisp_Object object)
307 {
308 if (CONSP (object))
309 return Qt;
310 return Qnil;
311 }
312
313 DEFUN ("atom", Fatom, Satom, 1, 1, 0,
314 doc: /* Return t if OBJECT is not a cons cell. This includes nil. */)
315 (Lisp_Object object)
316 {
317 if (CONSP (object))
318 return Qnil;
319 return Qt;
320 }
321
322 DEFUN ("listp", Flistp, Slistp, 1, 1, 0,
323 doc: /* Return t if OBJECT is a list, that is, a cons cell or nil.
324 Otherwise, return nil. */)
325 (Lisp_Object object)
326 {
327 if (CONSP (object) || NILP (object))
328 return Qt;
329 return Qnil;
330 }
331
332 DEFUN ("nlistp", Fnlistp, Snlistp, 1, 1, 0,
333 doc: /* Return t if OBJECT is not a list. Lists include nil. */)
334 (Lisp_Object object)
335 {
336 if (CONSP (object) || NILP (object))
337 return Qnil;
338 return Qt;
339 }
340 \f
341 DEFUN ("symbolp", Fsymbolp, Ssymbolp, 1, 1, 0,
342 doc: /* Return t if OBJECT is a symbol. */)
343 (Lisp_Object object)
344 {
345 if (SYMBOLP (object))
346 return Qt;
347 return Qnil;
348 }
349
350 /* Define this in C to avoid unnecessarily consing up the symbol
351 name. */
352 DEFUN ("keywordp", Fkeywordp, Skeywordp, 1, 1, 0,
353 doc: /* Return t if OBJECT is a keyword.
354 This means that it is a symbol with a print name beginning with `:'
355 interned in the initial obarray. */)
356 (Lisp_Object object)
357 {
358 if (SYMBOLP (object)
359 && SREF (SYMBOL_NAME (object), 0) == ':'
360 && SYMBOL_INTERNED_IN_INITIAL_OBARRAY_P (object))
361 return Qt;
362 return Qnil;
363 }
364
365 DEFUN ("vectorp", Fvectorp, Svectorp, 1, 1, 0,
366 doc: /* Return t if OBJECT is a vector. */)
367 (Lisp_Object object)
368 {
369 if (VECTORP (object))
370 return Qt;
371 return Qnil;
372 }
373
374 DEFUN ("stringp", Fstringp, Sstringp, 1, 1, 0,
375 doc: /* Return t if OBJECT is a string. */)
376 (Lisp_Object object)
377 {
378 if (STRINGP (object))
379 return Qt;
380 return Qnil;
381 }
382
383 DEFUN ("multibyte-string-p", Fmultibyte_string_p, Smultibyte_string_p,
384 1, 1, 0,
385 doc: /* Return t if OBJECT is a multibyte string.
386 Return nil if OBJECT is either a unibyte string, or not a string. */)
387 (Lisp_Object object)
388 {
389 if (STRINGP (object) && STRING_MULTIBYTE (object))
390 return Qt;
391 return Qnil;
392 }
393
394 DEFUN ("char-table-p", Fchar_table_p, Schar_table_p, 1, 1, 0,
395 doc: /* Return t if OBJECT is a char-table. */)
396 (Lisp_Object object)
397 {
398 if (CHAR_TABLE_P (object))
399 return Qt;
400 return Qnil;
401 }
402
403 DEFUN ("vector-or-char-table-p", Fvector_or_char_table_p,
404 Svector_or_char_table_p, 1, 1, 0,
405 doc: /* Return t if OBJECT is a char-table or vector. */)
406 (Lisp_Object object)
407 {
408 if (VECTORP (object) || CHAR_TABLE_P (object))
409 return Qt;
410 return Qnil;
411 }
412
413 DEFUN ("bool-vector-p", Fbool_vector_p, Sbool_vector_p, 1, 1, 0,
414 doc: /* Return t if OBJECT is a bool-vector. */)
415 (Lisp_Object object)
416 {
417 if (BOOL_VECTOR_P (object))
418 return Qt;
419 return Qnil;
420 }
421
422 DEFUN ("arrayp", Farrayp, Sarrayp, 1, 1, 0,
423 doc: /* Return t if OBJECT is an array (string or vector). */)
424 (Lisp_Object object)
425 {
426 if (ARRAYP (object))
427 return Qt;
428 return Qnil;
429 }
430
431 DEFUN ("sequencep", Fsequencep, Ssequencep, 1, 1, 0,
432 doc: /* Return t if OBJECT is a sequence (list or array). */)
433 (register Lisp_Object object)
434 {
435 if (CONSP (object) || NILP (object) || ARRAYP (object))
436 return Qt;
437 return Qnil;
438 }
439
440 DEFUN ("bufferp", Fbufferp, Sbufferp, 1, 1, 0,
441 doc: /* Return t if OBJECT is an editor buffer. */)
442 (Lisp_Object object)
443 {
444 if (BUFFERP (object))
445 return Qt;
446 return Qnil;
447 }
448
449 DEFUN ("markerp", Fmarkerp, Smarkerp, 1, 1, 0,
450 doc: /* Return t if OBJECT is a marker (editor pointer). */)
451 (Lisp_Object object)
452 {
453 if (MARKERP (object))
454 return Qt;
455 return Qnil;
456 }
457
458 DEFUN ("subrp", Fsubrp, Ssubrp, 1, 1, 0,
459 doc: /* Return t if OBJECT is a built-in function. */)
460 (Lisp_Object object)
461 {
462 if (SUBRP (object))
463 return Qt;
464 return Qnil;
465 }
466
467 DEFUN ("byte-code-function-p", Fbyte_code_function_p, Sbyte_code_function_p,
468 1, 1, 0,
469 doc: /* Return t if OBJECT is a byte-compiled function object. */)
470 (Lisp_Object object)
471 {
472 if (COMPILEDP (object))
473 return Qt;
474 return Qnil;
475 }
476
477 DEFUN ("char-or-string-p", Fchar_or_string_p, Schar_or_string_p, 1, 1, 0,
478 doc: /* Return t if OBJECT is a character or a string. */)
479 (register Lisp_Object object)
480 {
481 if (CHARACTERP (object) || STRINGP (object))
482 return Qt;
483 return Qnil;
484 }
485 \f
486 DEFUN ("integerp", Fintegerp, Sintegerp, 1, 1, 0,
487 doc: /* Return t if OBJECT is an integer. */)
488 (Lisp_Object object)
489 {
490 if (INTEGERP (object))
491 return Qt;
492 return Qnil;
493 }
494
495 DEFUN ("integer-or-marker-p", Finteger_or_marker_p, Sinteger_or_marker_p, 1, 1, 0,
496 doc: /* Return t if OBJECT is an integer or a marker (editor pointer). */)
497 (register Lisp_Object object)
498 {
499 if (MARKERP (object) || INTEGERP (object))
500 return Qt;
501 return Qnil;
502 }
503
504 DEFUN ("natnump", Fnatnump, Snatnump, 1, 1, 0,
505 doc: /* Return t if OBJECT is a nonnegative integer. */)
506 (Lisp_Object object)
507 {
508 if (NATNUMP (object))
509 return Qt;
510 return Qnil;
511 }
512
513 DEFUN ("numberp", Fnumberp, Snumberp, 1, 1, 0,
514 doc: /* Return t if OBJECT is a number (floating point or integer). */)
515 (Lisp_Object object)
516 {
517 if (NUMBERP (object))
518 return Qt;
519 else
520 return Qnil;
521 }
522
523 DEFUN ("number-or-marker-p", Fnumber_or_marker_p,
524 Snumber_or_marker_p, 1, 1, 0,
525 doc: /* Return t if OBJECT is a number or a marker. */)
526 (Lisp_Object object)
527 {
528 if (NUMBERP (object) || MARKERP (object))
529 return Qt;
530 return Qnil;
531 }
532
533 DEFUN ("floatp", Ffloatp, Sfloatp, 1, 1, 0,
534 doc: /* Return t if OBJECT is a floating point number. */)
535 (Lisp_Object object)
536 {
537 if (FLOATP (object))
538 return Qt;
539 return Qnil;
540 }
541
542 \f
543 /* Extract and set components of lists. */
544
545 DEFUN ("car", Fcar, Scar, 1, 1, 0,
546 doc: /* Return the car of LIST. If arg is nil, return nil.
547 Error if arg is not nil and not a cons cell. See also `car-safe'.
548
549 See Info node `(elisp)Cons Cells' for a discussion of related basic
550 Lisp concepts such as car, cdr, cons cell and list. */)
551 (register Lisp_Object list)
552 {
553 return CAR (list);
554 }
555
556 DEFUN ("car-safe", Fcar_safe, Scar_safe, 1, 1, 0,
557 doc: /* Return the car of OBJECT if it is a cons cell, or else nil. */)
558 (Lisp_Object object)
559 {
560 return CAR_SAFE (object);
561 }
562
563 DEFUN ("cdr", Fcdr, Scdr, 1, 1, 0,
564 doc: /* Return the cdr of LIST. If arg is nil, return nil.
565 Error if arg is not nil and not a cons cell. See also `cdr-safe'.
566
567 See Info node `(elisp)Cons Cells' for a discussion of related basic
568 Lisp concepts such as cdr, car, cons cell and list. */)
569 (register Lisp_Object list)
570 {
571 return CDR (list);
572 }
573
574 DEFUN ("cdr-safe", Fcdr_safe, Scdr_safe, 1, 1, 0,
575 doc: /* Return the cdr of OBJECT if it is a cons cell, or else nil. */)
576 (Lisp_Object object)
577 {
578 return CDR_SAFE (object);
579 }
580
581 DEFUN ("setcar", Fsetcar, Ssetcar, 2, 2, 0,
582 doc: /* Set the car of CELL to be NEWCAR. Returns NEWCAR. */)
583 (register Lisp_Object cell, Lisp_Object newcar)
584 {
585 CHECK_CONS (cell);
586 CHECK_IMPURE (cell);
587 XSETCAR (cell, newcar);
588 return newcar;
589 }
590
591 DEFUN ("setcdr", Fsetcdr, Ssetcdr, 2, 2, 0,
592 doc: /* Set the cdr of CELL to be NEWCDR. Returns NEWCDR. */)
593 (register Lisp_Object cell, Lisp_Object newcdr)
594 {
595 CHECK_CONS (cell);
596 CHECK_IMPURE (cell);
597 XSETCDR (cell, newcdr);
598 return newcdr;
599 }
600 \f
601 /* Extract and set components of symbols. */
602
603 DEFUN ("boundp", Fboundp, Sboundp, 1, 1, 0,
604 doc: /* Return t if SYMBOL's value is not void.
605 Note that if `lexical-binding' is in effect, this refers to the
606 global value outside of any lexical scope. */)
607 (register Lisp_Object symbol)
608 {
609 Lisp_Object valcontents;
610 struct Lisp_Symbol *sym;
611 CHECK_SYMBOL (symbol);
612 sym = XSYMBOL (symbol);
613
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
624 set to Qunbound. */
625 return Qt;
626 else
627 {
628 swap_in_symval_forwarding (sym, blv);
629 valcontents = blv_value (blv);
630 }
631 break;
632 }
633 case SYMBOL_FORWARDED:
634 /* In set_internal, we un-forward vars when their value is
635 set to Qunbound. */
636 return Qt;
637 default: emacs_abort ();
638 }
639
640 return (EQ (valcontents, Qunbound) ? Qnil : Qt);
641 }
642
643 /* FIXME: Make it an alias for function-symbol! */
644 DEFUN ("fboundp", Ffboundp, Sfboundp, 1, 1, 0,
645 doc: /* Return t if SYMBOL's function definition is not void. */)
646 (register Lisp_Object symbol)
647 {
648 CHECK_SYMBOL (symbol);
649 return NILP (SYMBOL_FUNCTION (symbol)) ? Qnil : Qt;
650 }
651
652 DEFUN ("makunbound", Fmakunbound, Smakunbound, 1, 1, 0,
653 doc: /* Make SYMBOL's value be void.
654 Return SYMBOL. */)
655 (register Lisp_Object symbol)
656 {
657 CHECK_SYMBOL (symbol);
658 if (SYMBOL_CONSTANT_P (symbol))
659 xsignal1 (Qsetting_constant, symbol);
660 Fset (symbol, Qunbound);
661 return symbol;
662 }
663
664 DEFUN ("fmakunbound", Ffmakunbound, Sfmakunbound, 1, 1, 0,
665 doc: /* Make SYMBOL's function definition be nil.
666 Return SYMBOL. */)
667 (register Lisp_Object symbol)
668 {
669 CHECK_SYMBOL (symbol);
670 if (NILP (symbol) || EQ (symbol, Qt))
671 xsignal1 (Qsetting_constant, symbol);
672 set_symbol_function (symbol, Qnil);
673 return symbol;
674 }
675
676 DEFUN ("symbol-function", Fsymbol_function, Ssymbol_function, 1, 1, 0,
677 doc: /* Return SYMBOL's function definition, or nil if that is void. */)
678 (register Lisp_Object symbol)
679 {
680 CHECK_SYMBOL (symbol);
681 return SYMBOL_FUNCTION (symbol);
682 }
683
684 DEFUN ("symbol-plist", Fsymbol_plist, Ssymbol_plist, 1, 1, 0,
685 doc: /* Return SYMBOL's property list. */)
686 (register Lisp_Object symbol)
687 {
688 CHECK_SYMBOL (symbol);
689 return symbol_plist (symbol);
690 }
691
692 DEFUN ("symbol-name", Fsymbol_name, Ssymbol_name, 1, 1, 0,
693 doc: /* Return SYMBOL's name, a string. */)
694 (register Lisp_Object symbol)
695 {
696 register Lisp_Object name;
697
698 CHECK_SYMBOL (symbol);
699 name = SYMBOL_NAME (symbol);
700 return name;
701 }
702
703 DEFUN ("fset", Ffset, Sfset, 2, 2, 0,
704 doc: /* Set SYMBOL's function definition to DEFINITION, and return DEFINITION. */)
705 (register Lisp_Object symbol, Lisp_Object definition)
706 {
707 register Lisp_Object function;
708 CHECK_SYMBOL (symbol);
709
710 function = SYMBOL_FUNCTION (symbol);
711
712 if (!NILP (Vautoload_queue) && !NILP (function))
713 Vautoload_queue = Fcons (Fcons (symbol, function), Vautoload_queue);
714
715 if (AUTOLOADP (function))
716 Fput (symbol, Qautoload, XCDR (function));
717
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
723 set_symbol_function (symbol, definition);
724
725 return definition;
726 }
727
728 DEFUN ("defalias", Fdefalias, Sdefalias, 2, 3, 0,
729 doc: /* Set SYMBOL's function definition to DEFINITION.
730 Associates the function with the current load file, if any.
731 The optional third argument DOCSTRING specifies the documentation string
732 for SYMBOL; if it is omitted or nil, SYMBOL uses the documentation string
733 determined by DEFINITION.
734
735 Internally, this normally uses `fset', but if SYMBOL has a
736 `defalias-fset-function' property, the associated value is used instead.
737
738 The return value is undefined. */)
739 (register Lisp_Object symbol, Lisp_Object definition, Lisp_Object docstring)
740 {
741 CHECK_SYMBOL (symbol);
742 if (!NILP (Vpurify_flag)
743 /* If `definition' is a keymap, immutable (and copying) is wrong. */
744 && !KEYMAPP (definition))
745 definition = Fpurecopy (definition);
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 (SYMBOL_FUNCTION (symbol)))
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
768 if (!NILP (docstring))
769 Fput (symbol, Qfunction_documentation, docstring);
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;
774 }
775
776 DEFUN ("setplist", Fsetplist, Ssetplist, 2, 2, 0,
777 doc: /* Set SYMBOL's property list to NEWPLIST, and return NEWPLIST. */)
778 (register Lisp_Object symbol, Lisp_Object newplist)
779 {
780 CHECK_SYMBOL (symbol);
781 set_symbol_plist (symbol, newplist);
782 return newplist;
783 }
784
785 DEFUN ("subr-arity", Fsubr_arity, Ssubr_arity, 1, 1, 0,
786 doc: /* Return minimum and maximum number of args allowed for SUBR.
787 SUBR must be a built-in function.
788 The returned value is a pair (MIN . MAX). MIN is the minimum number
789 of args. MAX is the maximum number or the symbol `many', for a
790 function with `&rest' args, or `unevalled' for a special form. */)
791 (Lisp_Object subr)
792 {
793 short minargs, maxargs;
794 CHECK_SUBR (subr);
795 minargs = XSUBR (subr)->min_args;
796 maxargs = XSUBR (subr)->max_args;
797 return Fcons (make_number (minargs),
798 maxargs == MANY ? Qmany
799 : maxargs == UNEVALLED ? Qunevalled
800 : make_number (maxargs));
801 }
802
803 DEFUN ("subr-name", Fsubr_name, Ssubr_name, 1, 1, 0,
804 doc: /* Return name of subroutine SUBR.
805 SUBR must be a built-in function. */)
806 (Lisp_Object subr)
807 {
808 const char *name;
809 CHECK_SUBR (subr);
810 name = XSUBR (subr)->symbol_name;
811 return build_string (name);
812 }
813
814 DEFUN ("interactive-form", Finteractive_form, Sinteractive_form, 1, 1, 0,
815 doc: /* Return the interactive form of CMD or nil if none.
816 If CMD is not a command, the return value is nil.
817 Value, if non-nil, is a list \(interactive SPEC). */)
818 (Lisp_Object cmd)
819 {
820 Lisp_Object fun = indirect_function (cmd); /* Check cycles. */
821
822 if (NILP (fun))
823 return Qnil;
824
825 /* Use an `interactive-form' property if present, analogous to the
826 function-documentation property. */
827 fun = cmd;
828 while (SYMBOLP (fun))
829 {
830 Lisp_Object tmp = Fget (fun, Qinteractive_form);
831 if (!NILP (tmp))
832 return tmp;
833 else
834 fun = Fsymbol_function (fun);
835 }
836
837 if (SUBRP (fun))
838 {
839 const char *spec = XSUBR (fun)->intspec;
840 if (spec)
841 return list2 (Qinteractive,
842 (*spec != '(') ? build_string (spec) :
843 Fcar (Fread_from_string (build_string (spec), Qnil, Qnil)));
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 }
850 else if (AUTOLOADP (fun))
851 return Finteractive_form (Fautoload_do_load (fun, cmd, Qnil));
852 else if (CONSP (fun))
853 {
854 Lisp_Object funcar = XCAR (fun);
855 if (EQ (funcar, Qclosure))
856 return Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun))));
857 else if (EQ (funcar, Qlambda))
858 return Fassq (Qinteractive, Fcdr (XCDR (fun)));
859 }
860 return Qnil;
861 }
862
863 \f
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
872 struct Lisp_Symbol *
873 indirect_variable (struct Lisp_Symbol *symbol)
874 {
875 struct Lisp_Symbol *tortoise, *hare;
876
877 hare = tortoise = symbol;
878
879 while (hare->redirect == SYMBOL_VARALIAS)
880 {
881 hare = SYMBOL_ALIAS (hare);
882 if (hare->redirect != SYMBOL_VARALIAS)
883 break;
884
885 hare = SYMBOL_ALIAS (hare);
886 tortoise = SYMBOL_ALIAS (tortoise);
887
888 if (hare == tortoise)
889 {
890 Lisp_Object tem;
891 XSETSYMBOL (tem, symbol);
892 xsignal1 (Qcyclic_variable_indirection, tem);
893 }
894 }
895
896 return hare;
897 }
898
899
900 DEFUN ("indirect-variable", Findirect_variable, Sindirect_variable, 1, 1, 0,
901 doc: /* Return the variable at the end of OBJECT's variable chain.
902 If OBJECT is a symbol, follow its variable indirections (if any), and
903 return the variable at the end of the chain of aliases. See Info node
904 `(elisp)Variable Aliases'.
905
906 If OBJECT is not a symbol, just return it. If there is a loop in the
907 chain of aliases, signal a `cyclic-variable-indirection' error. */)
908 (Lisp_Object object)
909 {
910 if (SYMBOLP (object))
911 {
912 struct Lisp_Symbol *sym = indirect_variable (XSYMBOL (object));
913 XSETSYMBOL (object, sym);
914 }
915 return object;
916 }
917
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
924 Lisp_Object
925 do_symval_forwarding (register union Lisp_Fwd *valcontents)
926 {
927 register Lisp_Object val;
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:
941 return per_buffer_value (current_buffer,
942 XBUFFER_OBJFWD (valcontents)->offset);
943
944 case Lisp_Fwd_Kboard_Obj:
945 /* We used to simply use current_kboard here, but from Lisp
946 code, its value is often unexpected. It seems nicer to
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 ()));
958 default: emacs_abort ();
959 }
960 }
961
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
964 buffer-independent contents of the value cell: forwarded just one
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. */
969
970 static void
971 store_symval_forwarding (union Lisp_Fwd *valcontents, register Lisp_Object newval, struct buffer *buf)
972 {
973 switch (XFWDTYPE (valcontents))
974 {
975 case Lisp_Fwd_Int:
976 CHECK_NUMBER (newval);
977 *XINTFWD (valcontents)->intvar = XINT (newval);
978 break;
979
980 case Lisp_Fwd_Bool:
981 *XBOOLFWD (valcontents)->boolvar = !NILP (newval);
982 break;
983
984 case Lisp_Fwd_Obj:
985 *XOBJFWD (valcontents)->objvar = newval;
986
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))
993 {
994 int offset = ((char *) XOBJFWD (valcontents)->objvar
995 - (char *) &buffer_defaults);
996 int idx = PER_BUFFER_IDX (offset);
997
998 Lisp_Object tail, buf;
999
1000 if (idx <= 0)
1001 break;
1002
1003 FOR_EACH_LIVE_BUFFER (tail, buf)
1004 {
1005 struct buffer *b = XBUFFER (buf);
1006
1007 if (! PER_BUFFER_VALUE_P (b, idx))
1008 set_per_buffer_value (b, offset, newval);
1009 }
1010 }
1011 break;
1012
1013 case Lisp_Fwd_Buffer_Obj:
1014 {
1015 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1016 Lisp_Object predicate = XBUFFER_OBJFWD (valcontents)->predicate;
1017
1018 if (!NILP (predicate) && !NILP (newval)
1019 && NILP (call1 (predicate, newval)))
1020 wrong_type_argument (predicate, newval);
1021
1022 if (buf == NULL)
1023 buf = current_buffer;
1024 set_per_buffer_value (buf, offset, newval);
1025 }
1026 break;
1027
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 }
1034 break;
1035
1036 default:
1037 emacs_abort (); /* goto def; */
1038 }
1039 }
1040
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. */
1045
1046 void
1047 swap_in_global_binding (struct Lisp_Symbol *symbol)
1048 {
1049 struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (symbol);
1050
1051 /* Unload the previously loaded binding. */
1052 if (blv->fwd)
1053 set_blv_value (blv, do_symval_forwarding (blv->fwd));
1054
1055 /* Select the global binding in the symbol. */
1056 set_blv_valcell (blv, blv->defcell);
1057 if (blv->fwd)
1058 store_symval_forwarding (blv->fwd, XCDR (blv->defcell), NULL);
1059
1060 /* Indicate that the global binding is set up now. */
1061 set_blv_where (blv, Qnil);
1062 set_blv_found (blv, 0);
1063 }
1064
1065 /* Set up the buffer-local symbol SYMBOL for validity in the current buffer.
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. */
1071
1072 static void
1073 swap_in_symval_forwarding (struct Lisp_Symbol *symbol, struct Lisp_Buffer_Local_Value *blv)
1074 {
1075 register Lisp_Object tem1;
1076
1077 eassert (blv == SYMBOL_BLV (symbol));
1078
1079 tem1 = blv->where;
1080
1081 if (NILP (tem1)
1082 || (blv->frame_local
1083 ? !EQ (selected_frame, tem1)
1084 : current_buffer != XBUFFER (tem1)))
1085 {
1086
1087 /* Unload the previously loaded binding. */
1088 tem1 = blv->valcell;
1089 if (blv->fwd)
1090 set_blv_value (blv, do_symval_forwarding (blv->fwd));
1091 /* Choose the new binding. */
1092 {
1093 Lisp_Object var;
1094 XSETSYMBOL (var, symbol);
1095 if (blv->frame_local)
1096 {
1097 tem1 = assq_no_quit (var, XFRAME (selected_frame)->param_alist);
1098 set_blv_where (blv, selected_frame);
1099 }
1100 else
1101 {
1102 tem1 = assq_no_quit (var, BVAR (current_buffer, local_var_alist));
1103 set_blv_where (blv, Fcurrent_buffer ());
1104 }
1105 }
1106 if (!(blv->found = !NILP (tem1)))
1107 tem1 = blv->defcell;
1108
1109 /* Load the new binding. */
1110 set_blv_valcell (blv, tem1);
1111 if (blv->fwd)
1112 store_symval_forwarding (blv->fwd, blv_value (blv), NULL);
1113 }
1114 }
1115 \f
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
1118 if it has one, without signaling an error.
1119 Note that it must not be possible to quit
1120 within this function. Great care is required for this. */
1121
1122 Lisp_Object
1123 find_symbol_value (Lisp_Object symbol)
1124 {
1125 struct Lisp_Symbol *sym;
1126
1127 CHECK_SYMBOL (symbol);
1128 sym = XSYMBOL (symbol);
1129
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);
1139 return blv->fwd ? do_symval_forwarding (blv->fwd) : blv_value (blv);
1140 }
1141 /* FALLTHROUGH */
1142 case SYMBOL_FORWARDED:
1143 return do_symval_forwarding (SYMBOL_FWD (sym));
1144 default: emacs_abort ();
1145 }
1146 }
1147
1148 DEFUN ("symbol-value", Fsymbol_value, Ssymbol_value, 1, 1, 0,
1149 doc: /* Return SYMBOL's value. Error if that is void.
1150 Note that if `lexical-binding' is in effect, this returns the
1151 global value outside of any lexical scope. */)
1152 (Lisp_Object symbol)
1153 {
1154 Lisp_Object val;
1155
1156 val = find_symbol_value (symbol);
1157 if (!EQ (val, Qunbound))
1158 return val;
1159
1160 xsignal1 (Qvoid_variable, symbol);
1161 }
1162
1163 DEFUN ("set", Fset, Sset, 2, 2, 0,
1164 doc: /* Set SYMBOL's value to NEWVAL, and return NEWVAL. */)
1165 (register Lisp_Object symbol, Lisp_Object newval)
1166 {
1167 set_internal (symbol, newval, Qnil, 0);
1168 return newval;
1169 }
1170
1171 /* Store the value NEWVAL into SYMBOL.
1172 If buffer/frame-locality is an issue, WHERE specifies which context to use.
1173 (nil stands for the current buffer/frame).
1174
1175 If BINDFLAG is false, then if this symbol is supposed to become
1176 local in every buffer where it is set, then we make it local.
1177 If BINDFLAG is true, we don't do that. */
1178
1179 void
1180 set_internal (Lisp_Object symbol, Lisp_Object newval, Lisp_Object where,
1181 bool bindflag)
1182 {
1183 bool voide = EQ (newval, Qunbound);
1184 struct Lisp_Symbol *sym;
1185 Lisp_Object tem1;
1186
1187 /* If restoring in a dead buffer, do nothing. */
1188 /* if (BUFFERP (where) && NILP (XBUFFER (where)->name))
1189 return; */
1190
1191 CHECK_SYMBOL (symbol);
1192 if (SYMBOL_CONSTANT_P (symbol))
1193 {
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;
1200 }
1201
1202 sym = XSYMBOL (symbol);
1203
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);
1212 if (NILP (where))
1213 {
1214 if (blv->frame_local)
1215 where = selected_frame;
1216 else
1217 XSETBUFFER (where, current_buffer);
1218 }
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. */
1224 if (!EQ (blv->where, where)
1225 /* Also unload a global binding (if the var is local_if_set). */
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)
1233 set_blv_value (blv, do_symval_forwarding (blv->fwd));
1234
1235 /* Find the new binding. */
1236 XSETSYMBOL (symbol, sym); /* May have changed via aliasing. */
1237 tem1 = Fassq (symbol,
1238 (blv->frame_local
1239 ? XFRAME (where)->param_alist
1240 : BVAR (XBUFFER (where), local_var_alist)));
1241 set_blv_where (blv, where);
1242 blv->found = 1;
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 {
1257 blv->found = 0;
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));
1271 bset_local_var_alist
1272 (XBUFFER (where),
1273 Fcons (tem1, BVAR (XBUFFER (where), local_var_alist)));
1274 }
1275 }
1276
1277 /* Record which binding is now loaded. */
1278 set_blv_valcell (blv, tem1);
1279 }
1280
1281 /* Store the new value in the cons cell. */
1282 set_blv_value (blv, newval);
1283
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
1291 store_symval_forwarding (blv->fwd, newval,
1292 BUFFERP (where)
1293 ? XBUFFER (where) : current_buffer);
1294 }
1295 break;
1296 }
1297 case SYMBOL_FORWARDED:
1298 {
1299 struct buffer *buf
1300 = BUFFERP (where) ? XBUFFER (where) : current_buffer;
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 }
1311
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 }
1322 default: emacs_abort ();
1323 }
1324 return;
1325 }
1326 \f
1327 /* Access or set a buffer-local symbol's default value. */
1328
1329 /* Return the default value of SYMBOL, but don't check for voidness.
1330 Return Qunbound if it is void. */
1331
1332 static Lisp_Object
1333 default_value (Lisp_Object symbol)
1334 {
1335 struct Lisp_Symbol *sym;
1336
1337 CHECK_SYMBOL (symbol);
1338 sym = XSYMBOL (symbol);
1339
1340 start:
1341 switch (sym->redirect)
1342 {
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);
1360
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)
1367 return per_buffer_default (offset);
1368 }
1369
1370 /* For other variables, get the current value. */
1371 return do_symval_forwarding (valcontents);
1372 }
1373 default: emacs_abort ();
1374 }
1375 }
1376
1377 DEFUN ("default-boundp", Fdefault_boundp, Sdefault_boundp, 1, 1, 0,
1378 doc: /* Return t if SYMBOL has a non-void default value.
1379 This is the value that is seen in buffers that do not have their own values
1380 for this variable. */)
1381 (Lisp_Object symbol)
1382 {
1383 register Lisp_Object value;
1384
1385 value = default_value (symbol);
1386 return (EQ (value, Qunbound) ? Qnil : Qt);
1387 }
1388
1389 DEFUN ("default-value", Fdefault_value, Sdefault_value, 1, 1, 0,
1390 doc: /* Return SYMBOL's default value.
1391 This is the value that is seen in buffers that do not have their own values
1392 for this variable. The default value is meaningful for variables with
1393 local bindings in certain buffers. */)
1394 (Lisp_Object symbol)
1395 {
1396 Lisp_Object value = default_value (symbol);
1397 if (!EQ (value, Qunbound))
1398 return value;
1399
1400 xsignal1 (Qvoid_variable, symbol);
1401 }
1402
1403 DEFUN ("set-default", Fset_default, Sset_default, 2, 2, 0,
1404 doc: /* Set SYMBOL's default value to VALUE. SYMBOL and VALUE are evaluated.
1405 The default value is seen in buffers that do not have their own values
1406 for this variable. */)
1407 (Lisp_Object symbol, Lisp_Object value)
1408 {
1409 struct Lisp_Symbol *sym;
1410
1411 CHECK_SYMBOL (symbol);
1412 if (SYMBOL_CONSTANT_P (symbol))
1413 {
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);
1422
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);
1431
1432 /* Store new value into the DEFAULT-VALUE slot. */
1433 XSETCDR (blv->defcell, value);
1434
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);
1443
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);
1451
1452 set_per_buffer_default (offset, value);
1453
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;
1459
1460 FOR_EACH_BUFFER (b)
1461 if (!PER_BUFFER_VALUE_P (b, idx))
1462 set_per_buffer_value (b, offset, value);
1463 }
1464 return value;
1465 }
1466 else
1467 return Fset (symbol, value);
1468 }
1469 default: emacs_abort ();
1470 }
1471 }
1472
1473 DEFUN ("setq-default", Fsetq_default, Ssetq_default, 0, UNEVALLED, 0,
1474 doc: /* Set the default value of variable VAR to VALUE.
1475 VAR, the variable name, is literal (not evaluated);
1476 VALUE is an expression: it is evaluated and its value returned.
1477 The default value of a variable is seen in buffers
1478 that do not have their own values for the variable.
1479
1480 More generally, you can use multiple variables and values, as in
1481 (setq-default VAR VALUE VAR VALUE...)
1482 This sets each VAR's default value to the corresponding VALUE.
1483 The VALUE for the Nth VAR can refer to the new default values
1484 of previous VARs.
1485 usage: (setq-default [VAR VALUE]...) */)
1486 (Lisp_Object args)
1487 {
1488 Lisp_Object args_left, symbol, val;
1489 struct gcpro gcpro1;
1490
1491 args_left = val = args;
1492 GCPRO1 (args);
1493
1494 while (CONSP (args_left))
1495 {
1496 val = eval_sub (Fcar (XCDR (args_left)));
1497 symbol = XCAR (args_left);
1498 Fset_default (symbol, val);
1499 args_left = Fcdr (XCDR (args_left));
1500 }
1501
1502 UNGCPRO;
1503 return val;
1504 }
1505 \f
1506 /* Lisp functions for creating and removing buffer-local variables. */
1507
1508 union Lisp_Val_Fwd
1509 {
1510 Lisp_Object value;
1511 union Lisp_Fwd *fwd;
1512 };
1513
1514 static struct Lisp_Buffer_Local_Value *
1515 make_blv (struct Lisp_Symbol *sym, bool forwarded,
1516 union Lisp_Val_Fwd valcontents)
1517 {
1518 struct Lisp_Buffer_Local_Value *blv = xmalloc (sizeof *blv);
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
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;
1532 set_blv_where (blv, Qnil);
1533 blv->frame_local = 0;
1534 blv->local_if_set = 0;
1535 set_blv_defcell (blv, tem);
1536 set_blv_valcell (blv, tem);
1537 set_blv_found (blv, 0);
1538 return blv;
1539 }
1540
1541 DEFUN ("make-variable-buffer-local", Fmake_variable_buffer_local,
1542 Smake_variable_buffer_local, 1, 1, "vMake Variable Buffer Local: ",
1543 doc: /* Make VARIABLE become buffer-local whenever it is set.
1544 At any time, the value for the current buffer is in effect,
1545 unless the variable has never been set in this buffer,
1546 in which case the default value is in effect.
1547 Note that binding the variable with `let', or setting it while
1548 a `let'-style binding made in this buffer is in effect,
1549 does not make the variable buffer-local. Return VARIABLE.
1550
1551 This globally affects all uses of this variable, so it belongs together with
1552 the variable declaration, rather than with its uses (if you just want to make
1553 a variable local to the current buffer for one particular use, use
1554 `make-local-variable'). Buffer-local bindings are normally cleared
1555 while setting up a new major mode, unless they have a `permanent-local'
1556 property.
1557
1558 The function `default-value' gets the default value and `set-default' sets it. */)
1559 (register Lisp_Object variable)
1560 {
1561 struct Lisp_Symbol *sym;
1562 struct Lisp_Buffer_Local_Value *blv = NULL;
1563 union Lisp_Val_Fwd valcontents IF_LINT (= {LISP_INITIALLY_ZERO});
1564 bool forwarded IF_LINT (= 0);
1565
1566 CHECK_SYMBOL (variable);
1567 sym = XSYMBOL (variable);
1568
1569 start:
1570 switch (sym->redirect)
1571 {
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;
1592 default: emacs_abort ();
1593 }
1594
1595 if (sym->constant)
1596 error ("Symbol %s may not be buffer-local", SDATA (SYMBOL_NAME (variable)));
1597
1598 if (!blv)
1599 {
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))
1607 message ("Making %s buffer-local while let-bound!",
1608 SDATA (SYMBOL_NAME (variable)));
1609 }
1610 }
1611
1612 blv->local_if_set = 1;
1613 return variable;
1614 }
1615
1616 DEFUN ("make-local-variable", Fmake_local_variable, Smake_local_variable,
1617 1, 1, "vMake Local Variable: ",
1618 doc: /* Make VARIABLE have a separate value in the current buffer.
1619 Other buffers will continue to share a common default value.
1620 \(The buffer-local value of VARIABLE starts out as the same value
1621 VARIABLE previously had. If VARIABLE was void, it remains void.\)
1622 Return VARIABLE.
1623
1624 If the variable is already arranged to become local when set,
1625 this function causes a local value to exist for this buffer,
1626 just as setting the variable would do.
1627
1628 This function returns VARIABLE, and therefore
1629 (set (make-local-variable 'VARIABLE) VALUE-EXP)
1630 works.
1631
1632 See also `make-variable-buffer-local'.
1633
1634 Do not use `make-local-variable' to make a hook variable buffer-local.
1635 Instead, use `add-hook' and specify t for the LOCAL argument. */)
1636 (Lisp_Object variable)
1637 {
1638 Lisp_Object tem;
1639 bool forwarded IF_LINT (= 0);
1640 union Lisp_Val_Fwd valcontents IF_LINT (= {LISP_INITIALLY_ZERO});
1641 struct Lisp_Symbol *sym;
1642 struct Lisp_Buffer_Local_Value *blv = NULL;
1643
1644 CHECK_SYMBOL (variable);
1645 sym = XSYMBOL (variable);
1646
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;
1665 default: emacs_abort ();
1666 }
1667
1668 if (sym->constant)
1669 error ("Symbol %s may not be buffer-local",
1670 SDATA (SYMBOL_NAME (variable)));
1671
1672 if (blv ? blv->local_if_set
1673 : (forwarded && BUFFER_OBJFWDP (valcontents.fwd)))
1674 {
1675 tem = Fboundp (variable);
1676 /* Make sure the symbol has a local value in this particular buffer,
1677 by setting it to the same value it already has. */
1678 Fset (variable, (EQ (tem, Qt) ? Fsymbol_value (variable) : Qunbound));
1679 return variable;
1680 }
1681 if (!blv)
1682 {
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))
1690 message ("Making %s local to %s while let-bound!",
1691 SDATA (SYMBOL_NAME (variable)),
1692 SDATA (BVAR (current_buffer, name)));
1693 }
1694 }
1695
1696 /* Make sure this buffer has its own value of symbol. */
1697 XSETSYMBOL (variable, sym); /* Update in case of aliasing. */
1698 tem = Fassq (variable, BVAR (current_buffer, local_var_alist));
1699 if (NILP (tem))
1700 {
1701 if (let_shadows_buffer_binding_p (sym))
1702 message ("Making %s buffer-local while locally let-bound!",
1703 SDATA (SYMBOL_NAME (variable)));
1704
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. */
1708 find_symbol_value (variable);
1709
1710 bset_local_var_alist
1711 (current_buffer,
1712 Fcons (Fcons (variable, XCDR (blv->defcell)),
1713 BVAR (current_buffer, local_var_alist)));
1714
1715 /* Make sure symbol does not think it is set up for this buffer;
1716 force it to look once again for this buffer's value. */
1717 if (current_buffer == XBUFFER (blv->where))
1718 set_blv_where (blv, Qnil);
1719 set_blv_found (blv, 0);
1720 }
1721
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. */
1726 if (blv->fwd)
1727 swap_in_symval_forwarding (sym, blv);
1728
1729 return variable;
1730 }
1731
1732 DEFUN ("kill-local-variable", Fkill_local_variable, Skill_local_variable,
1733 1, 1, "vKill Local Variable: ",
1734 doc: /* Make VARIABLE no longer have a separate value in the current buffer.
1735 From now on the default value will apply in this buffer. Return VARIABLE. */)
1736 (register Lisp_Object variable)
1737 {
1738 register Lisp_Object tem;
1739 struct Lisp_Buffer_Local_Value *blv;
1740 struct Lisp_Symbol *sym;
1741
1742 CHECK_SYMBOL (variable);
1743 sym = XSYMBOL (variable);
1744
1745 start:
1746 switch (sym->redirect)
1747 {
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);
1761 set_per_buffer_value (current_buffer, offset,
1762 per_buffer_default (offset));
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;
1772 default: emacs_abort ();
1773 }
1774
1775 /* Get rid of this buffer's alist element, if any. */
1776 XSETSYMBOL (variable, sym); /* Propagate variable indirection. */
1777 tem = Fassq (variable, BVAR (current_buffer, local_var_alist));
1778 if (!NILP (tem))
1779 bset_local_var_alist
1780 (current_buffer,
1781 Fdelq (tem, BVAR (current_buffer, local_var_alist)));
1782
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. */
1786 {
1787 Lisp_Object buf; XSETBUFFER (buf, current_buffer);
1788 if (EQ (buf, blv->where))
1789 {
1790 set_blv_where (blv, Qnil);
1791 blv->found = 0;
1792 find_symbol_value (variable);
1793 }
1794 }
1795
1796 return variable;
1797 }
1798
1799 /* Lisp functions for creating and removing buffer-local variables. */
1800
1801 /* Obsolete since 22.2. NB adjust doc of modify-frame-parameters
1802 when/if this is removed. */
1803
1804 DEFUN ("make-variable-frame-local", Fmake_variable_frame_local, Smake_variable_frame_local,
1805 1, 1, "vMake Variable Frame Local: ",
1806 doc: /* Enable VARIABLE to have frame-local bindings.
1807 This does not create any frame-local bindings for VARIABLE,
1808 it just makes them possible.
1809
1810 A frame-local binding is actually a frame parameter value.
1811 If a frame F has a value for the frame parameter named VARIABLE,
1812 that also acts as a frame-local binding for VARIABLE in F--
1813 provided this function has been called to enable VARIABLE
1814 to have frame-local bindings at all.
1815
1816 The only way to create a frame-local binding for VARIABLE in a frame
1817 is to set the VARIABLE frame parameter of that frame. See
1818 `modify-frame-parameters' for how to set frame parameters.
1819
1820 Note that since Emacs 23.1, variables cannot be both buffer-local and
1821 frame-local any more (buffer-local bindings used to take precedence over
1822 frame-local bindings). */)
1823 (Lisp_Object variable)
1824 {
1825 bool forwarded;
1826 union Lisp_Val_Fwd valcontents;
1827 struct Lisp_Symbol *sym;
1828 struct Lisp_Buffer_Local_Value *blv = NULL;
1829
1830 CHECK_SYMBOL (variable);
1831 sym = XSYMBOL (variable);
1832
1833 start:
1834 switch (sym->redirect)
1835 {
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;
1854 default: emacs_abort ();
1855 }
1856
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);
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 }
1871 return variable;
1872 }
1873
1874 DEFUN ("local-variable-p", Flocal_variable_p, Slocal_variable_p,
1875 1, 2, 0,
1876 doc: /* Non-nil if VARIABLE has a local binding in buffer BUFFER.
1877 BUFFER defaults to the current buffer. */)
1878 (register Lisp_Object variable, Lisp_Object buffer)
1879 {
1880 register struct buffer *buf;
1881 struct Lisp_Symbol *sym;
1882
1883 if (NILP (buffer))
1884 buf = current_buffer;
1885 else
1886 {
1887 CHECK_BUFFER (buffer);
1888 buf = XBUFFER (buffer);
1889 }
1890
1891 CHECK_SYMBOL (variable);
1892 sym = XSYMBOL (variable);
1893
1894 start:
1895 switch (sym->redirect)
1896 {
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);
1904 XSETSYMBOL (variable, sym); /* Update in case of aliasing. */
1905
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 }
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 }
1932 default: emacs_abort ();
1933 }
1934 }
1935
1936 DEFUN ("local-variable-if-set-p", Flocal_variable_if_set_p, Slocal_variable_if_set_p,
1937 1, 2, 0,
1938 doc: /* Non-nil if VARIABLE is local in buffer BUFFER when set there.
1939 BUFFER defaults to the current buffer.
1940
1941 More precisely, return non-nil if either VARIABLE already has a local
1942 value in BUFFER, or if VARIABLE is automatically buffer-local (see
1943 `make-variable-buffer-local'). */)
1944 (register Lisp_Object variable, Lisp_Object buffer)
1945 {
1946 struct Lisp_Symbol *sym;
1947
1948 CHECK_SYMBOL (variable);
1949 sym = XSYMBOL (variable);
1950
1951 start:
1952 switch (sym->redirect)
1953 {
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);
1967 default: emacs_abort ();
1968 }
1969 }
1970
1971 DEFUN ("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.
1974 If the current binding is buffer-local, the value is the current buffer.
1975 If the current binding is frame-local, the value is the selected frame.
1976 If the current binding is global (the default), the value is nil. */)
1977 (register Lisp_Object variable)
1978 {
1979 struct Lisp_Symbol *sym;
1980
1981 CHECK_SYMBOL (variable);
1982 sym = XSYMBOL (variable);
1983
1984 /* Make sure the current binding is actually swapped in. */
1985 find_symbol_value (variable);
1986
1987 start:
1988 switch (sym->redirect)
1989 {
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))
1996 return Fframe_terminal (selected_frame);
1997 else if (!BUFFER_OBJFWDP (valcontents))
1998 return Qnil;
1999 }
2000 /* FALLTHROUGH */
2001 case SYMBOL_LOCALIZED:
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 ();
2006 else if (sym->redirect == SYMBOL_LOCALIZED
2007 && blv_found (SYMBOL_BLV (sym)))
2008 return SYMBOL_BLV (sym)->where;
2009 else
2010 return Qnil;
2011 default: emacs_abort ();
2012 }
2013 }
2014
2015 /* This code is disabled now that we use the selected frame to return
2016 keyboard-local-values. */
2017 #if 0
2018 extern struct terminal *get_terminal (Lisp_Object display, int);
2019
2020 DEFUN ("terminal-local-value", Fterminal_local_value,
2021 Sterminal_local_value, 2, 2, 0,
2022 doc: /* Return the terminal-local value of SYMBOL on TERMINAL.
2023 If SYMBOL is not a terminal-local variable, then return its normal
2024 value, like `symbol-value'.
2025
2026 TERMINAL may be a terminal object, a frame, or nil (meaning the
2027 selected frame's terminal device). */)
2028 (Lisp_Object symbol, Lisp_Object terminal)
2029 {
2030 Lisp_Object result;
2031 struct terminal *t = get_terminal (terminal, 1);
2032 push_kboard (t->kboard);
2033 result = Fsymbol_value (symbol);
2034 pop_kboard ();
2035 return result;
2036 }
2037
2038 DEFUN ("set-terminal-local-value", Fset_terminal_local_value,
2039 Sset_terminal_local_value, 3, 3, 0,
2040 doc: /* Set the terminal-local binding of SYMBOL on TERMINAL to VALUE.
2041 If VARIABLE is not a terminal-local variable, then set its normal
2042 binding, like `set'.
2043
2044 TERMINAL may be a terminal object, a frame, or nil (meaning the
2045 selected frame's terminal device). */)
2046 (Lisp_Object symbol, Lisp_Object terminal, Lisp_Object value)
2047 {
2048 Lisp_Object result;
2049 struct terminal *t = get_terminal (terminal, 1);
2050 push_kboard (d->kboard);
2051 result = Fset (symbol, value);
2052 pop_kboard ();
2053 return result;
2054 }
2055 #endif
2056 \f
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. */
2066 Lisp_Object
2067 indirect_function (register Lisp_Object object)
2068 {
2069 Lisp_Object tortoise, hare;
2070
2071 hare = tortoise = object;
2072
2073 for (;;)
2074 {
2075 if (!SYMBOLP (hare) || NILP (hare))
2076 break;
2077 hare = SYMBOL_FUNCTION (hare);
2078 if (!SYMBOLP (hare) || NILP (hare))
2079 break;
2080 hare = SYMBOL_FUNCTION (hare);
2081
2082 tortoise = SYMBOL_FUNCTION (tortoise);
2083
2084 if (EQ (hare, tortoise))
2085 xsignal1 (Qcyclic_function_indirection, object);
2086 }
2087
2088 return hare;
2089 }
2090
2091 DEFUN ("indirect-function", Findirect_function, Sindirect_function, 1, 2, 0,
2092 doc: /* Return the function at the end of OBJECT's function chain.
2093 If OBJECT is not a symbol, just return it. Otherwise, follow all
2094 function indirections to find the final function binding and return it.
2095 If the final symbol in the chain is unbound, signal a void-function error.
2096 Optional arg NOERROR non-nil means to return nil instead of signaling.
2097 Signal a cyclic-function-indirection error if there is a loop in the
2098 function chain of symbols. */)
2099 (register Lisp_Object object, Lisp_Object noerror)
2100 {
2101 Lisp_Object result;
2102
2103 /* Optimize for no indirection. */
2104 result = object;
2105 if (SYMBOLP (result) && !NILP (result)
2106 && (result = SYMBOL_FUNCTION (result), SYMBOLP (result)))
2107 result = indirect_function (result);
2108 if (!NILP (result))
2109 return result;
2110
2111 if (NILP (noerror))
2112 xsignal1 (Qvoid_function, object);
2113
2114 return Qnil;
2115 }
2116 \f
2117 /* Extract and set vector and string elements. */
2118
2119 DEFUN ("aref", Faref, Saref, 2, 2, 0,
2120 doc: /* Return the element of ARRAY at index IDX.
2121 ARRAY may be a vector, a string, a char-table, a bool-vector,
2122 or a byte-code object. IDX starts at 0. */)
2123 (register Lisp_Object array, Lisp_Object idx)
2124 {
2125 register EMACS_INT idxval;
2126
2127 CHECK_NUMBER (idx);
2128 idxval = XINT (idx);
2129 if (STRINGP (array))
2130 {
2131 int c;
2132 ptrdiff_t idxval_byte;
2133
2134 if (idxval < 0 || idxval >= SCHARS (array))
2135 args_out_of_range (array, idx);
2136 if (! STRING_MULTIBYTE (array))
2137 return make_number ((unsigned char) SREF (array, idxval));
2138 idxval_byte = string_char_to_byte (array, idxval);
2139
2140 c = STRING_CHAR (SDATA (array) + idxval_byte);
2141 return make_number (c);
2142 }
2143 else if (BOOL_VECTOR_P (array))
2144 {
2145 if (idxval < 0 || idxval >= bool_vector_size (array))
2146 args_out_of_range (array, idx);
2147 return bool_vector_ref (array, idxval);
2148 }
2149 else if (CHAR_TABLE_P (array))
2150 {
2151 CHECK_CHARACTER (idx);
2152 return CHAR_TABLE_REF (array, idxval);
2153 }
2154 else
2155 {
2156 ptrdiff_t size = 0;
2157 if (VECTORP (array))
2158 size = ASIZE (array);
2159 else if (COMPILEDP (array))
2160 size = ASIZE (array) & PSEUDOVECTOR_SIZE_MASK;
2161 else
2162 wrong_type_argument (Qarrayp, array);
2163
2164 if (idxval < 0 || idxval >= size)
2165 args_out_of_range (array, idx);
2166 return AREF (array, idxval);
2167 }
2168 }
2169
2170 DEFUN ("aset", Faset, Saset, 3, 3, 0,
2171 doc: /* Store into the element of ARRAY at index IDX the value NEWELT.
2172 Return NEWELT. ARRAY may be a vector, a string, a char-table or a
2173 bool-vector. IDX starts at 0. */)
2174 (register Lisp_Object array, Lisp_Object idx, Lisp_Object newelt)
2175 {
2176 register EMACS_INT idxval;
2177
2178 CHECK_NUMBER (idx);
2179 idxval = XINT (idx);
2180 CHECK_ARRAY (array, Qarrayp);
2181 CHECK_IMPURE (array);
2182
2183 if (VECTORP (array))
2184 {
2185 if (idxval < 0 || idxval >= ASIZE (array))
2186 args_out_of_range (array, idx);
2187 ASET (array, idxval, newelt);
2188 }
2189 else if (BOOL_VECTOR_P (array))
2190 {
2191 if (idxval < 0 || idxval >= bool_vector_size (array))
2192 args_out_of_range (array, idx);
2193 bool_vector_set (array, idxval, !NILP (newelt));
2194 }
2195 else if (CHAR_TABLE_P (array))
2196 {
2197 CHECK_CHARACTER (idx);
2198 CHAR_TABLE_SET (array, idxval, newelt);
2199 }
2200 else
2201 {
2202 int c;
2203
2204 if (idxval < 0 || idxval >= SCHARS (array))
2205 args_out_of_range (array, idx);
2206 CHECK_CHARACTER (newelt);
2207 c = XFASTINT (newelt);
2208
2209 if (STRING_MULTIBYTE (array))
2210 {
2211 ptrdiff_t idxval_byte, nbytes;
2212 int prev_bytes, new_bytes;
2213 unsigned char workbuf[MAX_MULTIBYTE_LENGTH], *p0 = workbuf, *p1;
2214
2215 nbytes = SBYTES (array);
2216 idxval_byte = string_char_to_byte (array, idxval);
2217 p1 = SDATA (array) + idxval_byte;
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. */
2223 ptrdiff_t nchars = SCHARS (array);
2224 USE_SAFE_ALLOCA;
2225 unsigned char *str = SAFE_ALLOCA (nbytes);
2226
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++;
2239 }
2240 else
2241 {
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);
2255 }
2256 }
2257
2258 return newelt;
2259 }
2260 \f
2261 /* Arithmetic functions */
2262
2263 Lisp_Object
2264 arithcompare (Lisp_Object num1, Lisp_Object num2, enum Arith_Comparison comparison)
2265 {
2266 double f1 = 0, f2 = 0;
2267 bool floatp = 0;
2268
2269 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num1);
2270 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num2);
2271
2272 if (FLOATP (num1) || FLOATP (num2))
2273 {
2274 floatp = 1;
2275 f1 = (FLOATP (num1)) ? XFLOAT_DATA (num1) : XINT (num1);
2276 f2 = (FLOATP (num2)) ? XFLOAT_DATA (num2) : XINT (num2);
2277 }
2278
2279 switch (comparison)
2280 {
2281 case ARITH_EQUAL:
2282 if (floatp ? f1 == f2 : XINT (num1) == XINT (num2))
2283 return Qt;
2284 return Qnil;
2285
2286 case ARITH_NOTEQUAL:
2287 if (floatp ? f1 != f2 : XINT (num1) != XINT (num2))
2288 return Qt;
2289 return Qnil;
2290
2291 case ARITH_LESS:
2292 if (floatp ? f1 < f2 : XINT (num1) < XINT (num2))
2293 return Qt;
2294 return Qnil;
2295
2296 case ARITH_LESS_OR_EQUAL:
2297 if (floatp ? f1 <= f2 : XINT (num1) <= XINT (num2))
2298 return Qt;
2299 return Qnil;
2300
2301 case ARITH_GRTR:
2302 if (floatp ? f1 > f2 : XINT (num1) > XINT (num2))
2303 return Qt;
2304 return Qnil;
2305
2306 case ARITH_GRTR_OR_EQUAL:
2307 if (floatp ? f1 >= f2 : XINT (num1) >= XINT (num2))
2308 return Qt;
2309 return Qnil;
2310
2311 default:
2312 emacs_abort ();
2313 }
2314 }
2315
2316 static Lisp_Object
2317 arithcompare_driver (ptrdiff_t nargs, Lisp_Object *args,
2318 enum Arith_Comparison comparison)
2319 {
2320 ptrdiff_t argnum;
2321 for (argnum = 1; argnum < nargs; ++argnum)
2322 {
2323 if (EQ (Qnil, arithcompare (args[argnum - 1], args[argnum], comparison)))
2324 return Qnil;
2325 }
2326 return Qt;
2327 }
2328
2329 DEFUN ("=", Feqlsign, Seqlsign, 1, MANY, 0,
2330 doc: /* Return t if args, all numbers or markers, are equal.
2331 usage: (= NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2332 (ptrdiff_t nargs, Lisp_Object *args)
2333 {
2334 return arithcompare_driver (nargs, args, ARITH_EQUAL);
2335 }
2336
2337 DEFUN ("<", Flss, Slss, 1, MANY, 0,
2338 doc: /* Return t if each arg (a number or marker), is less than the next arg.
2339 usage: (< NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2340 (ptrdiff_t nargs, Lisp_Object *args)
2341 {
2342 return arithcompare_driver (nargs, args, ARITH_LESS);
2343 }
2344
2345 DEFUN (">", Fgtr, Sgtr, 1, MANY, 0,
2346 doc: /* Return t if each arg (a number or marker) is greater than the next arg.
2347 usage: (> NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2348 (ptrdiff_t nargs, Lisp_Object *args)
2349 {
2350 return arithcompare_driver (nargs, args, ARITH_GRTR);
2351 }
2352
2353 DEFUN ("<=", Fleq, Sleq, 1, MANY, 0,
2354 doc: /* Return t if each arg (a number or marker) is less than or equal to the next.
2355 usage: (<= NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2356 (ptrdiff_t nargs, Lisp_Object *args)
2357 {
2358 return arithcompare_driver (nargs, args, ARITH_LESS_OR_EQUAL);
2359 }
2360
2361 DEFUN (">=", Fgeq, Sgeq, 1, MANY, 0,
2362 doc: /* Return t if each arg (a number or marker) is greater than or equal to the next.
2363 usage: (>= NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2364 (ptrdiff_t nargs, Lisp_Object *args)
2365 {
2366 return arithcompare_driver (nargs, args, ARITH_GRTR_OR_EQUAL);
2367 }
2368
2369 DEFUN ("/=", Fneq, Sneq, 2, 2, 0,
2370 doc: /* Return t if first arg is not equal to second arg. Both must be numbers or markers. */)
2371 (register Lisp_Object num1, Lisp_Object num2)
2372 {
2373 return arithcompare (num1, num2, ARITH_NOTEQUAL);
2374 }
2375 \f
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. */
2379 uintmax_t
2380 cons_to_unsigned (Lisp_Object c, uintmax_t max)
2381 {
2382 bool valid = 0;
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 }
2423
2424 if (! (valid && val <= max))
2425 error ("Not an in-range integer, float, or cons of integers");
2426 return val;
2427 }
2428
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. */
2432 intmax_t
2433 cons_to_signed (Lisp_Object c, intmax_t min, intmax_t max)
2434 {
2435 bool valid = 0;
2436 intmax_t val IF_LINT (= 0);
2437 if (INTEGERP (c))
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;
2480 }
2481 \f
2482 DEFUN ("number-to-string", Fnumber_to_string, Snumber_to_string, 1, 1, 0,
2483 doc: /* Return the decimal representation of NUMBER as a string.
2484 Uses a minus sign if negative.
2485 NUMBER may be an integer or a floating point number. */)
2486 (Lisp_Object number)
2487 {
2488 char buffer[max (FLOAT_TO_STRING_BUFSIZE, INT_BUFSIZE_BOUND (EMACS_INT))];
2489 int len;
2490
2491 CHECK_NUMBER_OR_FLOAT (number);
2492
2493 if (FLOATP (number))
2494 len = float_to_string (buffer, XFLOAT_DATA (number));
2495 else
2496 len = sprintf (buffer, "%"pI"d", XINT (number));
2497
2498 return make_unibyte_string (buffer, len);
2499 }
2500
2501 DEFUN ("string-to-number", Fstring_to_number, Sstring_to_number, 1, 2, 0,
2502 doc: /* Parse STRING as a decimal number and return the number.
2503 Ignore leading spaces and tabs, and all trailing chars. Return 0 if
2504 STRING cannot be parsed as an integer or floating point number.
2505
2506 If BASE, interpret STRING as a number in that base. If BASE isn't
2507 present, base 10 is used. BASE must be between 2 and 16 (inclusive).
2508 If the base used is not 10, STRING is always parsed as an integer. */)
2509 (register Lisp_Object string, Lisp_Object base)
2510 {
2511 register char *p;
2512 register int b;
2513 Lisp_Object val;
2514
2515 CHECK_STRING (string);
2516
2517 if (NILP (base))
2518 b = 10;
2519 else
2520 {
2521 CHECK_NUMBER (base);
2522 if (! (2 <= XINT (base) && XINT (base) <= 16))
2523 xsignal1 (Qargs_out_of_range, base);
2524 b = XINT (base);
2525 }
2526
2527 p = SSDATA (string);
2528 while (*p == ' ' || *p == '\t')
2529 p++;
2530
2531 val = string_to_number (p, b, 1);
2532 return NILP (val) ? make_number (0) : val;
2533 }
2534 \f
2535 enum arithop
2536 {
2537 Aadd,
2538 Asub,
2539 Amult,
2540 Adiv,
2541 Alogand,
2542 Alogior,
2543 Alogxor,
2544 Amax,
2545 Amin
2546 };
2547
2548 static Lisp_Object float_arith_driver (double, ptrdiff_t, enum arithop,
2549 ptrdiff_t, Lisp_Object *);
2550 static Lisp_Object
2551 arith_driver (enum arithop code, ptrdiff_t nargs, Lisp_Object *args)
2552 {
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;
2558
2559 switch (code)
2560 {
2561 case Alogior:
2562 case Alogxor:
2563 case Aadd:
2564 case Asub:
2565 accum = 0;
2566 break;
2567 case Amult:
2568 accum = 1;
2569 break;
2570 case Alogand:
2571 accum = -1;
2572 break;
2573 default:
2574 break;
2575 }
2576
2577 for (argnum = 0; argnum < nargs; argnum++)
2578 {
2579 if (! overflow)
2580 {
2581 ok_args = argnum;
2582 ok_accum = accum;
2583 }
2584
2585 /* Using args[argnum] as argument to CHECK_NUMBER_... */
2586 val = args[argnum];
2587 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val);
2588
2589 if (FLOATP (val))
2590 return float_arith_driver (ok_accum, ok_args, code,
2591 nargs, args);
2592 args[argnum] = val;
2593 next = XINT (args[argnum]);
2594 switch (code)
2595 {
2596 case Aadd:
2597 if (INT_ADD_OVERFLOW (accum, next))
2598 {
2599 overflow = 1;
2600 accum &= INTMASK;
2601 }
2602 accum += next;
2603 break;
2604 case Asub:
2605 if (INT_SUBTRACT_OVERFLOW (accum, next))
2606 {
2607 overflow = 1;
2608 accum &= INTMASK;
2609 }
2610 accum = argnum ? accum - next : nargs == 1 ? - next : next;
2611 break;
2612 case Amult:
2613 if (INT_MULTIPLY_OVERFLOW (accum, next))
2614 {
2615 EMACS_UINT a = accum, b = next, ab = a * b;
2616 overflow = 1;
2617 accum = ab & INTMASK;
2618 }
2619 else
2620 accum *= next;
2621 break;
2622 case Adiv:
2623 if (!argnum)
2624 accum = next;
2625 else
2626 {
2627 if (next == 0)
2628 xsignal0 (Qarith_error);
2629 accum /= next;
2630 }
2631 break;
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;
2649 }
2650 }
2651
2652 XSETINT (val, accum);
2653 return val;
2654 }
2655
2656 #undef isnan
2657 #define isnan(x) ((x) != (x))
2658
2659 static Lisp_Object
2660 float_arith_driver (double accum, ptrdiff_t argnum, enum arithop code,
2661 ptrdiff_t nargs, Lisp_Object *args)
2662 {
2663 register Lisp_Object val;
2664 double next;
2665
2666 for (; argnum < nargs; argnum++)
2667 {
2668 val = args[argnum]; /* using args[argnum] as argument to CHECK_NUMBER_... */
2669 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val);
2670
2671 if (FLOATP (val))
2672 {
2673 next = XFLOAT_DATA (val);
2674 }
2675 else
2676 {
2677 args[argnum] = val; /* runs into a compiler bug. */
2678 next = XINT (args[argnum]);
2679 }
2680 switch (code)
2681 {
2682 case Aadd:
2683 accum += next;
2684 break;
2685 case Asub:
2686 accum = argnum ? accum - next : nargs == 1 ? - next : next;
2687 break;
2688 case Amult:
2689 accum *= next;
2690 break;
2691 case Adiv:
2692 if (!argnum)
2693 accum = next;
2694 else
2695 {
2696 if (! IEEE_FLOATING_POINT && next == 0)
2697 xsignal0 (Qarith_error);
2698 accum /= next;
2699 }
2700 break;
2701 case Alogand:
2702 case Alogior:
2703 case Alogxor:
2704 return wrong_type_argument (Qinteger_or_marker_p, val);
2705 case Amax:
2706 if (!argnum || isnan (next) || next > accum)
2707 accum = next;
2708 break;
2709 case Amin:
2710 if (!argnum || isnan (next) || next < accum)
2711 accum = next;
2712 break;
2713 }
2714 }
2715
2716 return make_float (accum);
2717 }
2718
2719
2720 DEFUN ("+", Fplus, Splus, 0, MANY, 0,
2721 doc: /* Return sum of any number of arguments, which are numbers or markers.
2722 usage: (+ &rest NUMBERS-OR-MARKERS) */)
2723 (ptrdiff_t nargs, Lisp_Object *args)
2724 {
2725 return arith_driver (Aadd, nargs, args);
2726 }
2727
2728 DEFUN ("-", Fminus, Sminus, 0, MANY, 0,
2729 doc: /* Negate number or subtract numbers or markers and return the result.
2730 With one arg, negates it. With more than one arg,
2731 subtracts all but the first from the first.
2732 usage: (- &optional NUMBER-OR-MARKER &rest MORE-NUMBERS-OR-MARKERS) */)
2733 (ptrdiff_t nargs, Lisp_Object *args)
2734 {
2735 return arith_driver (Asub, nargs, args);
2736 }
2737
2738 DEFUN ("*", Ftimes, Stimes, 0, MANY, 0,
2739 doc: /* Return product of any number of arguments, which are numbers or markers.
2740 usage: (* &rest NUMBERS-OR-MARKERS) */)
2741 (ptrdiff_t nargs, Lisp_Object *args)
2742 {
2743 return arith_driver (Amult, nargs, args);
2744 }
2745
2746 DEFUN ("/", Fquo, Squo, 1, MANY, 0,
2747 doc: /* Return first argument divided by all the remaining arguments.
2748 The arguments must be numbers or markers.
2749 usage: (/ DIVIDEND &rest DIVISORS) */)
2750 (ptrdiff_t nargs, Lisp_Object *args)
2751 {
2752 ptrdiff_t argnum;
2753 for (argnum = 2; argnum < nargs; argnum++)
2754 if (FLOATP (args[argnum]))
2755 return float_arith_driver (0, 0, Adiv, nargs, args);
2756 return arith_driver (Adiv, nargs, args);
2757 }
2758
2759 DEFUN ("%", Frem, Srem, 2, 2, 0,
2760 doc: /* Return remainder of X divided by Y.
2761 Both must be integers or markers. */)
2762 (register Lisp_Object x, Lisp_Object y)
2763 {
2764 Lisp_Object val;
2765
2766 CHECK_NUMBER_COERCE_MARKER (x);
2767 CHECK_NUMBER_COERCE_MARKER (y);
2768
2769 if (XINT (y) == 0)
2770 xsignal0 (Qarith_error);
2771
2772 XSETINT (val, XINT (x) % XINT (y));
2773 return val;
2774 }
2775
2776 DEFUN ("mod", Fmod, Smod, 2, 2, 0,
2777 doc: /* Return X modulo Y.
2778 The result falls between zero (inclusive) and Y (exclusive).
2779 Both X and Y must be numbers or markers. */)
2780 (register Lisp_Object x, Lisp_Object y)
2781 {
2782 Lisp_Object val;
2783 EMACS_INT i1, i2;
2784
2785 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (x);
2786 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (y);
2787
2788 if (FLOATP (x) || FLOATP (y))
2789 return fmod_float (x, y);
2790
2791 i1 = XINT (x);
2792 i2 = XINT (y);
2793
2794 if (i2 == 0)
2795 xsignal0 (Qarith_error);
2796
2797 i1 %= i2;
2798
2799 /* If the "remainder" comes out with the wrong sign, fix it. */
2800 if (i2 < 0 ? i1 > 0 : i1 < 0)
2801 i1 += i2;
2802
2803 XSETINT (val, i1);
2804 return val;
2805 }
2806
2807 DEFUN ("max", Fmax, Smax, 1, MANY, 0,
2808 doc: /* Return largest of all the arguments (which must be numbers or markers).
2809 The value is always a number; markers are converted to numbers.
2810 usage: (max NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2811 (ptrdiff_t nargs, Lisp_Object *args)
2812 {
2813 return arith_driver (Amax, nargs, args);
2814 }
2815
2816 DEFUN ("min", Fmin, Smin, 1, MANY, 0,
2817 doc: /* Return smallest of all the arguments (which must be numbers or markers).
2818 The value is always a number; markers are converted to numbers.
2819 usage: (min NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
2820 (ptrdiff_t nargs, Lisp_Object *args)
2821 {
2822 return arith_driver (Amin, nargs, args);
2823 }
2824
2825 DEFUN ("logand", Flogand, Slogand, 0, MANY, 0,
2826 doc: /* Return bitwise-and of all the arguments.
2827 Arguments may be integers, or markers converted to integers.
2828 usage: (logand &rest INTS-OR-MARKERS) */)
2829 (ptrdiff_t nargs, Lisp_Object *args)
2830 {
2831 return arith_driver (Alogand, nargs, args);
2832 }
2833
2834 DEFUN ("logior", Flogior, Slogior, 0, MANY, 0,
2835 doc: /* Return bitwise-or of all the arguments.
2836 Arguments may be integers, or markers converted to integers.
2837 usage: (logior &rest INTS-OR-MARKERS) */)
2838 (ptrdiff_t nargs, Lisp_Object *args)
2839 {
2840 return arith_driver (Alogior, nargs, args);
2841 }
2842
2843 DEFUN ("logxor", Flogxor, Slogxor, 0, MANY, 0,
2844 doc: /* Return bitwise-exclusive-or of all the arguments.
2845 Arguments may be integers, or markers converted to integers.
2846 usage: (logxor &rest INTS-OR-MARKERS) */)
2847 (ptrdiff_t nargs, Lisp_Object *args)
2848 {
2849 return arith_driver (Alogxor, nargs, args);
2850 }
2851
2852 DEFUN ("ash", Fash, Sash, 2, 2, 0,
2853 doc: /* Return VALUE with its bits shifted left by COUNT.
2854 If COUNT is negative, shifting is actually to the right.
2855 In this case, the sign bit is duplicated. */)
2856 (register Lisp_Object value, Lisp_Object count)
2857 {
2858 register Lisp_Object val;
2859
2860 CHECK_NUMBER (value);
2861 CHECK_NUMBER (count);
2862
2863 if (XINT (count) >= BITS_PER_EMACS_INT)
2864 XSETINT (val, 0);
2865 else if (XINT (count) > 0)
2866 XSETINT (val, XUINT (value) << XFASTINT (count));
2867 else if (XINT (count) <= -BITS_PER_EMACS_INT)
2868 XSETINT (val, XINT (value) < 0 ? -1 : 0);
2869 else
2870 XSETINT (val, XINT (value) >> -XINT (count));
2871 return val;
2872 }
2873
2874 DEFUN ("lsh", Flsh, Slsh, 2, 2, 0,
2875 doc: /* Return VALUE with its bits shifted left by COUNT.
2876 If COUNT is negative, shifting is actually to the right.
2877 In this case, zeros are shifted in on the left. */)
2878 (register Lisp_Object value, Lisp_Object count)
2879 {
2880 register Lisp_Object val;
2881
2882 CHECK_NUMBER (value);
2883 CHECK_NUMBER (count);
2884
2885 if (XINT (count) >= BITS_PER_EMACS_INT)
2886 XSETINT (val, 0);
2887 else if (XINT (count) > 0)
2888 XSETINT (val, XUINT (value) << XFASTINT (count));
2889 else if (XINT (count) <= -BITS_PER_EMACS_INT)
2890 XSETINT (val, 0);
2891 else
2892 XSETINT (val, XUINT (value) >> -XINT (count));
2893 return val;
2894 }
2895
2896 DEFUN ("1+", Fadd1, Sadd1, 1, 1, 0,
2897 doc: /* Return NUMBER plus one. NUMBER may be a number or a marker.
2898 Markers are converted to integers. */)
2899 (register Lisp_Object number)
2900 {
2901 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number);
2902
2903 if (FLOATP (number))
2904 return (make_float (1.0 + XFLOAT_DATA (number)));
2905
2906 XSETINT (number, XINT (number) + 1);
2907 return number;
2908 }
2909
2910 DEFUN ("1-", Fsub1, Ssub1, 1, 1, 0,
2911 doc: /* Return NUMBER minus one. NUMBER may be a number or a marker.
2912 Markers are converted to integers. */)
2913 (register Lisp_Object number)
2914 {
2915 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number);
2916
2917 if (FLOATP (number))
2918 return (make_float (-1.0 + XFLOAT_DATA (number)));
2919
2920 XSETINT (number, XINT (number) - 1);
2921 return number;
2922 }
2923
2924 DEFUN ("lognot", Flognot, Slognot, 1, 1, 0,
2925 doc: /* Return the bitwise complement of NUMBER. NUMBER must be an integer. */)
2926 (register Lisp_Object number)
2927 {
2928 CHECK_NUMBER (number);
2929 XSETINT (number, ~XINT (number));
2930 return number;
2931 }
2932
2933 DEFUN ("byteorder", Fbyteorder, Sbyteorder, 0, 0, 0,
2934 doc: /* Return the byteorder for the machine.
2935 Returns 66 (ASCII uppercase B) for big endian machines or 108 (ASCII
2936 lowercase l) for small endian machines. */)
2937 (void)
2938 {
2939 unsigned i = 0x04030201;
2940 int order = *(char *)&i == 1 ? 108 : 66;
2941
2942 return make_number (order);
2943 }
2944
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
2947 operations below. These extra bits are always zero. */
2948
2949 static bits_word
2950 bool_vector_spare_mask (EMACS_INT nr_bits)
2951 {
2952 return (((bits_word) 1) << (nr_bits % BITS_PER_BITS_WORD)) - 1;
2953 }
2954
2955 /* Info about unsigned long long, falling back on unsigned long
2956 if unsigned long long is not available. */
2957
2958 #if HAVE_UNSIGNED_LONG_LONG_INT && defined ULLONG_MAX
2959 enum { BITS_PER_ULL = CHAR_BIT * sizeof (unsigned long long) };
2960 # define ULL_MAX ULLONG_MAX
2961 #else
2962 enum { BITS_PER_ULL = CHAR_BIT * sizeof (unsigned long) };
2963 # define ULL_MAX ULONG_MAX
2964 # define count_one_bits_ll count_one_bits_l
2965 # define count_trailing_zeros_ll count_trailing_zeros_l
2966 #endif
2967
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
2971 static bits_word
2972 shift_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
2981 static int
2982 count_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),
2992 (i += BITS_PER_ULL) < BITS_PER_BITS_WORD)
2993 w = shift_right_ull (w);
2994 return count;
2995 }
2996 }
2997
2998 enum 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
3004 static Lisp_Object
3005 bool_vector_binop_driver (Lisp_Object a,
3006 Lisp_Object b,
3007 Lisp_Object dest,
3008 enum bool_vector_op op)
3009 {
3010 EMACS_INT nr_bits;
3011 bits_word *adata, *bdata, *destdata;
3012 ptrdiff_t i = 0;
3013 ptrdiff_t nr_words;
3014
3015 CHECK_BOOL_VECTOR (a);
3016 CHECK_BOOL_VECTOR (b);
3017
3018 nr_bits = bool_vector_size (a);
3019 if (bool_vector_size (b) != nr_bits)
3020 wrong_length_argument (a, b, dest);
3021
3022 nr_words = bool_vector_words (nr_bits);
3023 adata = bool_vector_data (a);
3024 bdata = bool_vector_data (b);
3025
3026 if (NILP (dest))
3027 {
3028 dest = make_uninit_bool_vector (nr_bits);
3029 destdata = bool_vector_data (dest);
3030 }
3031 else
3032 {
3033 CHECK_BOOL_VECTOR (dest);
3034 destdata = bool_vector_data (dest);
3035 if (bool_vector_size (dest) != nr_bits)
3036 wrong_length_argument (a, b, dest);
3037
3038 switch (op)
3039 {
3040 case bool_vector_exclusive_or:
3041 for (; i < nr_words; i++)
3042 if (destdata[i] != (adata[i] ^ bdata[i]))
3043 goto set_dest;
3044 break;
3045
3046 case bool_vector_subsetp:
3047 for (; i < nr_words; i++)
3048 if (adata[i] &~ bdata[i])
3049 return Qnil;
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;
3056 break;
3057
3058 case bool_vector_intersection:
3059 for (; i < nr_words; i++)
3060 if (destdata[i] != (adata[i] & bdata[i]))
3061 goto set_dest;
3062 break;
3063
3064 case bool_vector_set_difference:
3065 for (; i < nr_words; i++)
3066 if (destdata[i] != (adata[i] &~ bdata[i]))
3067 goto set_dest;
3068 break;
3069 }
3070
3071 return Qnil;
3072 }
3073
3074 set_dest:
3075 switch (op)
3076 {
3077 case bool_vector_exclusive_or:
3078 for (; i < nr_words; i++)
3079 destdata[i] = adata[i] ^ bdata[i];
3080 break;
3081
3082 case bool_vector_union:
3083 for (; i < nr_words; i++)
3084 destdata[i] = adata[i] | bdata[i];
3085 break;
3086
3087 case bool_vector_intersection:
3088 for (; i < nr_words; i++)
3089 destdata[i] = adata[i] & bdata[i];
3090 break;
3091
3092 case bool_vector_set_difference:
3093 for (; i < nr_words; i++)
3094 destdata[i] = adata[i] &~ bdata[i];
3095 break;
3096
3097 default:
3098 eassume (0);
3099 }
3100
3101 return dest;
3102 }
3103
3104 /* PRECONDITION must be true. Return VALUE. This odd construction
3105 works around a bogus GCC diagnostic "shift count >= width of type". */
3106
3107 static int
3108 pre_value (bool precondition, int value)
3109 {
3110 eassume (precondition);
3111 return precondition ? value : 0;
3112 }
3113
3114 /* Compute the number of trailing zero bits in val. If val is zero,
3115 return the number of bits in val. */
3116 static int
3117 count_trailing_zero_bits (bits_word val)
3118 {
3119 if (BITS_WORD_MAX == UINT_MAX)
3120 return count_trailing_zeros (val);
3121 if (BITS_WORD_MAX == ULONG_MAX)
3122 return count_trailing_zeros_l (val);
3123 if (BITS_WORD_MAX == ULL_MAX)
3124 return count_trailing_zeros_ll (val);
3125
3126 /* The rest of this code is for the unlikely platform where bits_word differs
3127 in width from unsigned int, unsigned long, and unsigned long long. */
3128 val |= ~ BITS_WORD_MAX;
3129 if (BITS_WORD_MAX <= UINT_MAX)
3130 return count_trailing_zeros (val);
3131 if (BITS_WORD_MAX <= ULONG_MAX)
3132 return count_trailing_zeros_l (val);
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 {
3140 if (val & ULL_MAX)
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)
3147 val |= (bits_word) 1 << pre_value (ULONG_MAX < BITS_WORD_MAX,
3148 BITS_PER_BITS_WORD % BITS_PER_ULL);
3149 return count + count_trailing_zeros_ll (val);
3150 }
3151 }
3152
3153 static bits_word
3154 bits_word_to_host_endian (bits_word val)
3155 {
3156 #ifndef WORDS_BIGENDIAN
3157 return val;
3158 #else
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 }
3176 #endif
3177 }
3178
3179 DEFUN ("bool-vector-exclusive-or", Fbool_vector_exclusive_or,
3180 Sbool_vector_exclusive_or, 2, 3, 0,
3181 doc: /* Return A ^ B, bitwise exclusive or.
3182 If optional third argument C is given, store result into C.
3183 A, B, and C must be bool vectors of the same length.
3184 Return the destination vector if it changed or nil otherwise. */)
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
3190 DEFUN ("bool-vector-union", Fbool_vector_union,
3191 Sbool_vector_union, 2, 3, 0,
3192 doc: /* Return A | B, bitwise or.
3193 If optional third argument C is given, store result into C.
3194 A, B, and C must be bool vectors of the same length.
3195 Return the destination vector if it changed or nil otherwise. */)
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
3201 DEFUN ("bool-vector-intersection", Fbool_vector_intersection,
3202 Sbool_vector_intersection, 2, 3, 0,
3203 doc: /* Return A & B, bitwise and.
3204 If optional third argument C is given, store result into C.
3205 A, B, and C must be bool vectors of the same length.
3206 Return the destination vector if it changed or nil otherwise. */)
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
3212 DEFUN ("bool-vector-set-difference", Fbool_vector_set_difference,
3213 Sbool_vector_set_difference, 2, 3, 0,
3214 doc: /* Return A &~ B, set difference.
3215 If optional third argument C is given, store result into C.
3216 A, B, and C must be bool vectors of the same length.
3217 Return the destination vector if it changed or nil otherwise. */)
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
3223 DEFUN ("bool-vector-subsetp", Fbool_vector_subsetp,
3224 Sbool_vector_subsetp, 2, 2, 0,
3225 doc: /* Return t if every t value in A is also t in B, nil otherwise.
3226 A and B must be bool vectors of the same length. */)
3227 (Lisp_Object a, Lisp_Object b)
3228 {
3229 return bool_vector_binop_driver (a, b, b, bool_vector_subsetp);
3230 }
3231
3232 DEFUN ("bool-vector-not", Fbool_vector_not,
3233 Sbool_vector_not, 1, 2, 0,
3234 doc: /* Compute ~A, set complement.
3235 If optional second argument B is given, store result into B.
3236 A and B must be bool vectors of the same length.
3237 Return the destination vector. */)
3238 (Lisp_Object a, Lisp_Object b)
3239 {
3240 EMACS_INT nr_bits;
3241 bits_word *bdata, *adata;
3242 ptrdiff_t i;
3243
3244 CHECK_BOOL_VECTOR (a);
3245 nr_bits = bool_vector_size (a);
3246
3247 if (NILP (b))
3248 b = make_uninit_bool_vector (nr_bits);
3249 else
3250 {
3251 CHECK_BOOL_VECTOR (b);
3252 if (bool_vector_size (b) != nr_bits)
3253 wrong_length_argument (a, b, Qnil);
3254 }
3255
3256 bdata = bool_vector_data (b);
3257 adata = bool_vector_data (a);
3258
3259 for (i = 0; i < nr_bits / BITS_PER_BITS_WORD; i++)
3260 bdata[i] = BITS_WORD_MAX & ~adata[i];
3261
3262 if (nr_bits % BITS_PER_BITS_WORD)
3263 {
3264 bits_word mword = bits_word_to_host_endian (adata[i]);
3265 mword = ~mword;
3266 mword &= bool_vector_spare_mask (nr_bits);
3267 bdata[i] = bits_word_to_host_endian (mword);
3268 }
3269
3270 return b;
3271 }
3272
3273 DEFUN ("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.
3276 A is a bool vector. To count A's nil elements, subtract the return
3277 value from A's length. */)
3278 (Lisp_Object a)
3279 {
3280 EMACS_INT count;
3281 EMACS_INT nr_bits;
3282 bits_word *adata;
3283 ptrdiff_t i, nwords;
3284
3285 CHECK_BOOL_VECTOR (a);
3286
3287 nr_bits = bool_vector_size (a);
3288 nwords = bool_vector_words (nr_bits);
3289 count = 0;
3290 adata = bool_vector_data (a);
3291
3292 for (i = 0; i < nwords; i++)
3293 count += count_one_bits_word (adata[i]);
3294
3295 return make_number (count);
3296 }
3297
3298 DEFUN ("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.
3301 A is a bool vector, B is t or nil, and I is an index into A. */)
3302 (Lisp_Object a, Lisp_Object b, Lisp_Object i)
3303 {
3304 EMACS_INT count;
3305 EMACS_INT nr_bits;
3306 int offset;
3307 bits_word *adata;
3308 bits_word twiddle;
3309 bits_word mword; /* Machine word. */
3310 ptrdiff_t pos, pos0;
3311 ptrdiff_t nr_words;
3312
3313 CHECK_BOOL_VECTOR (a);
3314 CHECK_NATNUM (i);
3315
3316 nr_bits = bool_vector_size (a);
3317 if (XFASTINT (i) > nr_bits) /* Allow one past the end for convenience */
3318 args_out_of_range (a, i);
3319
3320 adata = bool_vector_data (a);
3321 nr_words = bool_vector_words (nr_bits);
3322 pos = XFASTINT (i) / BITS_PER_BITS_WORD;
3323 offset = XFASTINT (i) % BITS_PER_BITS_WORD;
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. */
3329 twiddle = NILP (b) ? 0 : BITS_WORD_MAX;
3330
3331 /* Scan the remainder of the mword at the current offset. */
3332 if (pos < nr_words && offset != 0)
3333 {
3334 mword = bits_word_to_host_endian (adata[pos]);
3335 mword ^= twiddle;
3336 mword >>= offset;
3337
3338 /* Do not count the pad bits. */
3339 mword |= (bits_word) 1 << (BITS_PER_BITS_WORD - offset);
3340
3341 count = count_trailing_zero_bits (mword);
3342 pos++;
3343 if (count + offset < BITS_PER_BITS_WORD)
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. */
3350 pos0 = pos;
3351 while (pos < nr_words && adata[pos] == twiddle)
3352 pos++;
3353 count += (pos - pos0) * BITS_PER_BITS_WORD;
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. */
3359 mword = bits_word_to_host_endian (adata[pos]);
3360 mword ^= twiddle;
3361 count += count_trailing_zero_bits (mword);
3362 }
3363 else if (nr_bits % BITS_PER_BITS_WORD != 0)
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. */
3368 count -= BITS_PER_BITS_WORD - nr_bits % BITS_PER_BITS_WORD;
3369 }
3370
3371 return make_number (count);
3372 }
3373
3374 \f
3375 void
3376 syms_of_data (void)
3377 {
3378 Lisp_Object error_tail, arith_tail;
3379
3380 #include "data.x"
3381
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");
3390 DEFSYM (Quser_error, "user-error");
3391 DEFSYM (Qquit, "quit");
3392 DEFSYM (Qwrong_length_argument, "wrong-length-argument");
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");
3425 DEFSYM (Qbool_vector_p, "bool-vector-p");
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");
3449
3450 error_tail = pure_cons (Qerror, Qnil);
3451
3452 /* ERROR is used as a signaler for random errors for which nothing else is
3453 right. */
3454
3455 Fput (Qerror, Qerror_conditions,
3456 error_tail);
3457 Fput (Qerror, Qerror_message,
3458 build_pure_c_string ("error"));
3459
3460 #define PUT_ERROR(sym, tail, msg) \
3461 Fput (sym, Qerror_conditions, pure_cons (sym, tail)); \
3462 Fput (sym, Qerror_message, build_pure_c_string (msg))
3463
3464 PUT_ERROR (Qquit, Qnil, "Quit");
3465
3466 PUT_ERROR (Quser_error, error_tail, "");
3467 PUT_ERROR (Qwrong_length_argument, error_tail, "Wrong length argument");
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");
3476 DEFSYM (Qcircular_list, "circular-list");
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");
3487
3488 arith_tail = pure_cons (Qarith_error, error_tail);
3489 Fput (Qarith_error, Qerror_conditions, arith_tail);
3490 Fput (Qarith_error, Qerror_message, build_pure_c_string ("Arithmetic error"));
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");
3497
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");
3503
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");
3515
3516 staticpro (&Qnil);
3517 staticpro (&Qt);
3518 staticpro (&Qunbound);
3519
3520 /* Types that type-of returns. */
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");
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");
3538 DEFSYM (Qmisc, "misc");
3539
3540 DEFSYM (Qdefun, "defun");
3541
3542 DEFSYM (Qfont_spec, "font-spec");
3543 DEFSYM (Qfont_entity, "font-entity");
3544 DEFSYM (Qfont_object, "font-object");
3545
3546 DEFSYM (Qinteractive_form, "interactive-form");
3547 DEFSYM (Qdefalias_fset_function, "defalias-fset-function");
3548
3549 set_symbol_function (Qwholenump, SYMBOL_FUNCTION (Qnatnump));
3550
3551 DEFVAR_LISP ("most-positive-fixnum", Vmost_positive_fixnum,
3552 doc: /* The largest value that is representable in a Lisp integer. */);
3553 Vmost_positive_fixnum = make_number (MOST_POSITIVE_FIXNUM);
3554 XSYMBOL (intern_c_string ("most-positive-fixnum"))->constant = 1;
3555
3556 DEFVAR_LISP ("most-negative-fixnum", Vmost_negative_fixnum,
3557 doc: /* The smallest value that is representable in a Lisp integer. */);
3558 Vmost_negative_fixnum = make_number (MOST_NEGATIVE_FIXNUM);
3559 XSYMBOL (intern_c_string ("most-negative-fixnum"))->constant = 1;
3560 }