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