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