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