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