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