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