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