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