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