]> code.delx.au - gnu-emacs/blob - src/data.c
(store_symval_forwarding): Add parameter BUF. If BUF is
[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 \f
735 /* Getting and setting values of symbols */
736
737 /* Given the raw contents of a symbol value cell,
738 return the Lisp value of the symbol.
739 This does not handle buffer-local variables; use
740 swap_in_symval_forwarding for that. */
741
742 Lisp_Object
743 do_symval_forwarding (valcontents)
744 register Lisp_Object valcontents;
745 {
746 register Lisp_Object val;
747 int offset;
748 if (MISCP (valcontents))
749 switch (XMISCTYPE (valcontents))
750 {
751 case Lisp_Misc_Intfwd:
752 XSETINT (val, *XINTFWD (valcontents)->intvar);
753 return val;
754
755 case Lisp_Misc_Boolfwd:
756 return (*XBOOLFWD (valcontents)->boolvar ? Qt : Qnil);
757
758 case Lisp_Misc_Objfwd:
759 return *XOBJFWD (valcontents)->objvar;
760
761 case Lisp_Misc_Buffer_Objfwd:
762 offset = XBUFFER_OBJFWD (valcontents)->offset;
763 return PER_BUFFER_VALUE (current_buffer, offset);
764
765 case Lisp_Misc_Kboard_Objfwd:
766 offset = XKBOARD_OBJFWD (valcontents)->offset;
767 return *(Lisp_Object *)(offset + (char *)current_kboard);
768 }
769 return valcontents;
770 }
771
772 /* Store NEWVAL into SYMBOL, where VALCONTENTS is found in the value cell
773 of SYMBOL. If SYMBOL is buffer-local, VALCONTENTS should be the
774 buffer-independent contents of the value cell: forwarded just one
775 step past the buffer-localness.
776
777 BUF non-zero means set the value in buffer BUF instead of the
778 current buffer. This only plays a role for per-buffer variables. */
779
780 void
781 store_symval_forwarding (symbol, valcontents, newval, buf)
782 Lisp_Object symbol;
783 register Lisp_Object valcontents, newval;
784 struct buffer *buf;
785 {
786 switch (SWITCH_ENUM_CAST (XTYPE (valcontents)))
787 {
788 case Lisp_Misc:
789 switch (XMISCTYPE (valcontents))
790 {
791 case Lisp_Misc_Intfwd:
792 CHECK_NUMBER (newval, 1);
793 *XINTFWD (valcontents)->intvar = XINT (newval);
794 if (*XINTFWD (valcontents)->intvar != XINT (newval))
795 error ("Value out of range for variable `%s'",
796 XSYMBOL (symbol)->name->data);
797 break;
798
799 case Lisp_Misc_Boolfwd:
800 *XBOOLFWD (valcontents)->boolvar = NILP (newval) ? 0 : 1;
801 break;
802
803 case Lisp_Misc_Objfwd:
804 *XOBJFWD (valcontents)->objvar = newval;
805 break;
806
807 case Lisp_Misc_Buffer_Objfwd:
808 {
809 int offset = XBUFFER_OBJFWD (valcontents)->offset;
810 Lisp_Object type;
811
812 type = PER_BUFFER_TYPE (offset);
813 if (XINT (type) == -1)
814 error ("Variable %s is read-only", XSYMBOL (symbol)->name->data);
815
816 if (! NILP (type) && ! NILP (newval)
817 && XTYPE (newval) != XINT (type))
818 buffer_slot_type_mismatch (offset);
819
820 if (buf == NULL)
821 buf = current_buffer;
822 PER_BUFFER_VALUE (buf, offset) = newval;
823 }
824 break;
825
826 case Lisp_Misc_Kboard_Objfwd:
827 {
828 char *base = (char *) current_kboard;
829 char *p = base + XKBOARD_OBJFWD (valcontents)->offset;
830 *(Lisp_Object *) p = newval;
831 }
832 break;
833
834 default:
835 goto def;
836 }
837 break;
838
839 default:
840 def:
841 valcontents = XSYMBOL (symbol)->value;
842 if (BUFFER_LOCAL_VALUEP (valcontents)
843 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
844 XBUFFER_LOCAL_VALUE (valcontents)->realvalue = newval;
845 else
846 XSYMBOL (symbol)->value = newval;
847 }
848 }
849
850 /* Set up SYMBOL to refer to its global binding.
851 This makes it safe to alter the status of other bindings. */
852
853 void
854 swap_in_global_binding (symbol)
855 Lisp_Object symbol;
856 {
857 Lisp_Object valcontents, cdr;
858
859 valcontents = XSYMBOL (symbol)->value;
860 if (!BUFFER_LOCAL_VALUEP (valcontents)
861 && !SOME_BUFFER_LOCAL_VALUEP (valcontents))
862 abort ();
863 cdr = XBUFFER_LOCAL_VALUE (valcontents)->cdr;
864
865 /* Unload the previously loaded binding. */
866 Fsetcdr (XCAR (cdr),
867 do_symval_forwarding (XBUFFER_LOCAL_VALUE (valcontents)->realvalue));
868
869 /* Select the global binding in the symbol. */
870 XCAR (cdr) = cdr;
871 store_symval_forwarding (symbol, valcontents, XCDR (cdr), NULL);
872
873 /* Indicate that the global binding is set up now. */
874 XBUFFER_LOCAL_VALUE (valcontents)->frame = Qnil;
875 XBUFFER_LOCAL_VALUE (valcontents)->buffer = Qnil;
876 XBUFFER_LOCAL_VALUE (valcontents)->found_for_frame = 0;
877 XBUFFER_LOCAL_VALUE (valcontents)->found_for_buffer = 0;
878 }
879
880 /* Set up the buffer-local symbol SYMBOL for validity in the current buffer.
881 VALCONTENTS is the contents of its value cell,
882 which points to a struct Lisp_Buffer_Local_Value.
883
884 Return the value forwarded one step past the buffer-local stage.
885 This could be another forwarding pointer. */
886
887 static Lisp_Object
888 swap_in_symval_forwarding (symbol, valcontents)
889 Lisp_Object symbol, valcontents;
890 {
891 register Lisp_Object tem1;
892 tem1 = XBUFFER_LOCAL_VALUE (valcontents)->buffer;
893
894 if (NILP (tem1)
895 || current_buffer != XBUFFER (tem1)
896 || (XBUFFER_LOCAL_VALUE (valcontents)->check_frame
897 && ! EQ (selected_frame, XBUFFER_LOCAL_VALUE (valcontents)->frame)))
898 {
899 /* Unload the previously loaded binding. */
900 tem1 = XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
901 Fsetcdr (tem1,
902 do_symval_forwarding (XBUFFER_LOCAL_VALUE (valcontents)->realvalue));
903 /* Choose the new binding. */
904 tem1 = assq_no_quit (symbol, current_buffer->local_var_alist);
905 XBUFFER_LOCAL_VALUE (valcontents)->found_for_frame = 0;
906 XBUFFER_LOCAL_VALUE (valcontents)->found_for_buffer = 0;
907 if (NILP (tem1))
908 {
909 if (XBUFFER_LOCAL_VALUE (valcontents)->check_frame)
910 tem1 = assq_no_quit (symbol, XFRAME (selected_frame)->param_alist);
911 if (! NILP (tem1))
912 XBUFFER_LOCAL_VALUE (valcontents)->found_for_frame = 1;
913 else
914 tem1 = XBUFFER_LOCAL_VALUE (valcontents)->cdr;
915 }
916 else
917 XBUFFER_LOCAL_VALUE (valcontents)->found_for_buffer = 1;
918
919 /* Load the new binding. */
920 XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr) = tem1;
921 XSETBUFFER (XBUFFER_LOCAL_VALUE (valcontents)->buffer, current_buffer);
922 XBUFFER_LOCAL_VALUE (valcontents)->frame = selected_frame;
923 store_symval_forwarding (symbol,
924 XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
925 Fcdr (tem1), NULL);
926 }
927 return XBUFFER_LOCAL_VALUE (valcontents)->realvalue;
928 }
929 \f
930 /* Find the value of a symbol, returning Qunbound if it's not bound.
931 This is helpful for code which just wants to get a variable's value
932 if it has one, without signaling an error.
933 Note that it must not be possible to quit
934 within this function. Great care is required for this. */
935
936 Lisp_Object
937 find_symbol_value (symbol)
938 Lisp_Object symbol;
939 {
940 register Lisp_Object valcontents;
941 register Lisp_Object val;
942 CHECK_SYMBOL (symbol, 0);
943 valcontents = XSYMBOL (symbol)->value;
944
945 if (BUFFER_LOCAL_VALUEP (valcontents)
946 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
947 valcontents = swap_in_symval_forwarding (symbol, valcontents);
948
949 if (MISCP (valcontents))
950 {
951 switch (XMISCTYPE (valcontents))
952 {
953 case Lisp_Misc_Intfwd:
954 XSETINT (val, *XINTFWD (valcontents)->intvar);
955 return val;
956
957 case Lisp_Misc_Boolfwd:
958 return (*XBOOLFWD (valcontents)->boolvar ? Qt : Qnil);
959
960 case Lisp_Misc_Objfwd:
961 return *XOBJFWD (valcontents)->objvar;
962
963 case Lisp_Misc_Buffer_Objfwd:
964 return PER_BUFFER_VALUE (current_buffer,
965 XBUFFER_OBJFWD (valcontents)->offset);
966
967 case Lisp_Misc_Kboard_Objfwd:
968 return *(Lisp_Object *)(XKBOARD_OBJFWD (valcontents)->offset
969 + (char *)current_kboard);
970 }
971 }
972
973 return valcontents;
974 }
975
976 DEFUN ("symbol-value", Fsymbol_value, Ssymbol_value, 1, 1, 0,
977 "Return SYMBOL's value. Error if that is void.")
978 (symbol)
979 Lisp_Object symbol;
980 {
981 Lisp_Object val;
982
983 val = find_symbol_value (symbol);
984 if (EQ (val, Qunbound))
985 return Fsignal (Qvoid_variable, Fcons (symbol, Qnil));
986 else
987 return val;
988 }
989
990 DEFUN ("set", Fset, Sset, 2, 2, 0,
991 "Set SYMBOL's value to NEWVAL, and return NEWVAL.")
992 (symbol, newval)
993 register Lisp_Object symbol, newval;
994 {
995 return set_internal (symbol, newval, current_buffer, 0);
996 }
997
998 /* Return 1 if SYMBOL currently has a let-binding
999 which was made in the buffer that is now current. */
1000
1001 static int
1002 let_shadows_buffer_binding_p (symbol)
1003 Lisp_Object symbol;
1004 {
1005 struct specbinding *p;
1006
1007 for (p = specpdl_ptr - 1; p >= specpdl; p--)
1008 if (p->func == 0
1009 && CONSP (p->symbol)
1010 && EQ (symbol, XCAR (p->symbol))
1011 && XBUFFER (XCDR (XCDR (p->symbol))) == current_buffer)
1012 return 1;
1013
1014 return 0;
1015 }
1016
1017 /* Store the value NEWVAL into SYMBOL.
1018 If buffer-locality is an issue, BUF specifies which buffer to use.
1019 (0 stands for the current buffer.)
1020
1021 If BINDFLAG is zero, then if this symbol is supposed to become
1022 local in every buffer where it is set, then we make it local.
1023 If BINDFLAG is nonzero, we don't do that. */
1024
1025 Lisp_Object
1026 set_internal (symbol, newval, buf, bindflag)
1027 register Lisp_Object symbol, newval;
1028 struct buffer *buf;
1029 int bindflag;
1030 {
1031 int voide = EQ (newval, Qunbound);
1032
1033 register Lisp_Object valcontents, innercontents, tem1, current_alist_element;
1034
1035 if (buf == 0)
1036 buf = current_buffer;
1037
1038 /* If restoring in a dead buffer, do nothing. */
1039 if (NILP (buf->name))
1040 return newval;
1041
1042 CHECK_SYMBOL (symbol, 0);
1043 if (NILP (symbol) || EQ (symbol, Qt)
1044 || (XSYMBOL (symbol)->name->data[0] == ':'
1045 && EQ (XSYMBOL (symbol)->obarray, initial_obarray)
1046 && !EQ (newval, symbol)))
1047 return Fsignal (Qsetting_constant, Fcons (symbol, Qnil));
1048
1049 innercontents = valcontents = XSYMBOL (symbol)->value;
1050
1051 if (BUFFER_OBJFWDP (valcontents))
1052 {
1053 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1054 int idx = PER_BUFFER_IDX (offset);
1055 if (idx > 0
1056 && !bindflag
1057 && !let_shadows_buffer_binding_p (symbol))
1058 SET_PER_BUFFER_VALUE_P (buf, idx, 1);
1059 }
1060
1061 else if (BUFFER_LOCAL_VALUEP (valcontents)
1062 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
1063 {
1064 /* valcontents is a struct Lisp_Buffer_Local_Value. */
1065
1066 /* What binding is loaded right now? */
1067 current_alist_element
1068 = XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
1069
1070 /* If the current buffer is not the buffer whose binding is
1071 loaded, or if there may be frame-local bindings and the frame
1072 isn't the right one, or if it's a Lisp_Buffer_Local_Value and
1073 the default binding is loaded, the loaded binding may be the
1074 wrong one. */
1075 if (!BUFFERP (XBUFFER_LOCAL_VALUE (valcontents)->buffer)
1076 || buf != XBUFFER (XBUFFER_LOCAL_VALUE (valcontents)->buffer)
1077 || (XBUFFER_LOCAL_VALUE (valcontents)->check_frame
1078 && !EQ (selected_frame, XBUFFER_LOCAL_VALUE (valcontents)->frame))
1079 || (BUFFER_LOCAL_VALUEP (valcontents)
1080 && EQ (XCAR (current_alist_element),
1081 current_alist_element)))
1082 {
1083 /* The currently loaded binding is not necessarily valid.
1084 We need to unload it, and choose a new binding. */
1085
1086 /* Write out `realvalue' to the old loaded binding. */
1087 Fsetcdr (current_alist_element,
1088 do_symval_forwarding (XBUFFER_LOCAL_VALUE (valcontents)->realvalue));
1089
1090 /* Find the new binding. */
1091 tem1 = Fassq (symbol, buf->local_var_alist);
1092 XBUFFER_LOCAL_VALUE (valcontents)->found_for_buffer = 1;
1093 XBUFFER_LOCAL_VALUE (valcontents)->found_for_frame = 0;
1094
1095 if (NILP (tem1))
1096 {
1097 /* This buffer still sees the default value. */
1098
1099 /* If the variable is a Lisp_Some_Buffer_Local_Value,
1100 or if this is `let' rather than `set',
1101 make CURRENT-ALIST-ELEMENT point to itself,
1102 indicating that we're seeing the default value.
1103 Likewise if the variable has been let-bound
1104 in the current buffer. */
1105 if (bindflag || SOME_BUFFER_LOCAL_VALUEP (valcontents)
1106 || let_shadows_buffer_binding_p (symbol))
1107 {
1108 XBUFFER_LOCAL_VALUE (valcontents)->found_for_buffer = 0;
1109
1110 if (XBUFFER_LOCAL_VALUE (valcontents)->check_frame)
1111 tem1 = Fassq (symbol,
1112 XFRAME (selected_frame)->param_alist);
1113
1114 if (! NILP (tem1))
1115 XBUFFER_LOCAL_VALUE (valcontents)->found_for_frame = 1;
1116 else
1117 tem1 = XBUFFER_LOCAL_VALUE (valcontents)->cdr;
1118 }
1119 /* If it's a Lisp_Buffer_Local_Value, being set not bound,
1120 and we're not within a let that was made for this buffer,
1121 create a new buffer-local binding for the variable.
1122 That means, give this buffer a new assoc for a local value
1123 and load that binding. */
1124 else
1125 {
1126 tem1 = Fcons (symbol, Fcdr (current_alist_element));
1127 buf->local_var_alist
1128 = Fcons (tem1, buf->local_var_alist);
1129 }
1130 }
1131
1132 /* Record which binding is now loaded. */
1133 XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr)
1134 = tem1;
1135
1136 /* Set `buffer' and `frame' slots for thebinding now loaded. */
1137 XSETBUFFER (XBUFFER_LOCAL_VALUE (valcontents)->buffer, buf);
1138 XBUFFER_LOCAL_VALUE (valcontents)->frame = selected_frame;
1139 }
1140 innercontents = XBUFFER_LOCAL_VALUE (valcontents)->realvalue;
1141 }
1142
1143 /* If storing void (making the symbol void), forward only through
1144 buffer-local indicator, not through Lisp_Objfwd, etc. */
1145 if (voide)
1146 store_symval_forwarding (symbol, Qnil, newval, buf);
1147 else
1148 store_symval_forwarding (symbol, innercontents, newval, buf);
1149
1150 /* If we just set a variable whose current binding is frame-local,
1151 store the new value in the frame parameter too. */
1152
1153 if (BUFFER_LOCAL_VALUEP (valcontents)
1154 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
1155 {
1156 /* What binding is loaded right now? */
1157 current_alist_element
1158 = XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
1159
1160 /* If the current buffer is not the buffer whose binding is
1161 loaded, or if there may be frame-local bindings and the frame
1162 isn't the right one, or if it's a Lisp_Buffer_Local_Value and
1163 the default binding is loaded, the loaded binding may be the
1164 wrong one. */
1165 if (XBUFFER_LOCAL_VALUE (valcontents)->found_for_frame)
1166 XCDR (current_alist_element) = newval;
1167 }
1168
1169 return newval;
1170 }
1171 \f
1172 /* Access or set a buffer-local symbol's default value. */
1173
1174 /* Return the default value of SYMBOL, but don't check for voidness.
1175 Return Qunbound if it is void. */
1176
1177 Lisp_Object
1178 default_value (symbol)
1179 Lisp_Object symbol;
1180 {
1181 register Lisp_Object valcontents;
1182
1183 CHECK_SYMBOL (symbol, 0);
1184 valcontents = XSYMBOL (symbol)->value;
1185
1186 /* For a built-in buffer-local variable, get the default value
1187 rather than letting do_symval_forwarding get the current value. */
1188 if (BUFFER_OBJFWDP (valcontents))
1189 {
1190 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1191 if (PER_BUFFER_IDX (offset) != 0)
1192 return PER_BUFFER_DEFAULT (offset);
1193 }
1194
1195 /* Handle user-created local variables. */
1196 if (BUFFER_LOCAL_VALUEP (valcontents)
1197 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
1198 {
1199 /* If var is set up for a buffer that lacks a local value for it,
1200 the current value is nominally the default value.
1201 But the `realvalue' slot may be more up to date, since
1202 ordinary setq stores just that slot. So use that. */
1203 Lisp_Object current_alist_element, alist_element_car;
1204 current_alist_element
1205 = XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
1206 alist_element_car = XCAR (current_alist_element);
1207 if (EQ (alist_element_car, current_alist_element))
1208 return do_symval_forwarding (XBUFFER_LOCAL_VALUE (valcontents)->realvalue);
1209 else
1210 return XCDR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
1211 }
1212 /* For other variables, get the current value. */
1213 return do_symval_forwarding (valcontents);
1214 }
1215
1216 DEFUN ("default-boundp", Fdefault_boundp, Sdefault_boundp, 1, 1, 0,
1217 "Return t if SYMBOL has a non-void default value.\n\
1218 This is the value that is seen in buffers that do not have their own values\n\
1219 for this variable.")
1220 (symbol)
1221 Lisp_Object symbol;
1222 {
1223 register Lisp_Object value;
1224
1225 value = default_value (symbol);
1226 return (EQ (value, Qunbound) ? Qnil : Qt);
1227 }
1228
1229 DEFUN ("default-value", Fdefault_value, Sdefault_value, 1, 1, 0,
1230 "Return SYMBOL's default value.\n\
1231 This is the value that is seen in buffers that do not have their own values\n\
1232 for this variable. The default value is meaningful for variables with\n\
1233 local bindings in certain buffers.")
1234 (symbol)
1235 Lisp_Object symbol;
1236 {
1237 register Lisp_Object value;
1238
1239 value = default_value (symbol);
1240 if (EQ (value, Qunbound))
1241 return Fsignal (Qvoid_variable, Fcons (symbol, Qnil));
1242 return value;
1243 }
1244
1245 DEFUN ("set-default", Fset_default, Sset_default, 2, 2, 0,
1246 "Set SYMBOL's default value to VAL. SYMBOL and VAL are evaluated.\n\
1247 The default value is seen in buffers that do not have their own values\n\
1248 for this variable.")
1249 (symbol, value)
1250 Lisp_Object symbol, value;
1251 {
1252 register Lisp_Object valcontents, current_alist_element, alist_element_buffer;
1253
1254 CHECK_SYMBOL (symbol, 0);
1255 valcontents = XSYMBOL (symbol)->value;
1256
1257 /* Handle variables like case-fold-search that have special slots
1258 in the buffer. Make them work apparently like Lisp_Buffer_Local_Value
1259 variables. */
1260 if (BUFFER_OBJFWDP (valcontents))
1261 {
1262 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1263 int idx = PER_BUFFER_IDX (offset);
1264
1265 PER_BUFFER_DEFAULT (offset) = value;
1266
1267 /* If this variable is not always local in all buffers,
1268 set it in the buffers that don't nominally have a local value. */
1269 if (idx > 0)
1270 {
1271 struct buffer *b;
1272
1273 for (b = all_buffers; b; b = b->next)
1274 if (!PER_BUFFER_VALUE_P (b, idx))
1275 PER_BUFFER_VALUE (b, offset) = value;
1276 }
1277 return value;
1278 }
1279
1280 if (!BUFFER_LOCAL_VALUEP (valcontents)
1281 && !SOME_BUFFER_LOCAL_VALUEP (valcontents))
1282 return Fset (symbol, value);
1283
1284 /* Store new value into the DEFAULT-VALUE slot. */
1285 XCDR (XBUFFER_LOCAL_VALUE (valcontents)->cdr) = value;
1286
1287 /* If the default binding is now loaded, set the REALVALUE slot too. */
1288 current_alist_element
1289 = XCAR (XBUFFER_LOCAL_VALUE (valcontents)->cdr);
1290 alist_element_buffer = Fcar (current_alist_element);
1291 if (EQ (alist_element_buffer, current_alist_element))
1292 store_symval_forwarding (symbol,
1293 XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
1294 value, NULL);
1295
1296 return value;
1297 }
1298
1299 DEFUN ("setq-default", Fsetq_default, Ssetq_default, 2, UNEVALLED, 0,
1300 "Set the default value of variable VAR to VALUE.\n\
1301 VAR, the variable name, is literal (not evaluated);\n\
1302 VALUE is an expression and it is evaluated.\n\
1303 The default value of a variable is seen in buffers\n\
1304 that do not have their own values for the variable.\n\
1305 \n\
1306 More generally, you can use multiple variables and values, as in\n\
1307 (setq-default SYMBOL VALUE SYMBOL VALUE...)\n\
1308 This sets each SYMBOL's default value to the corresponding VALUE.\n\
1309 The VALUE for the Nth SYMBOL can refer to the new default values\n\
1310 of previous SYMs.")
1311 (args)
1312 Lisp_Object args;
1313 {
1314 register Lisp_Object args_left;
1315 register Lisp_Object val, symbol;
1316 struct gcpro gcpro1;
1317
1318 if (NILP (args))
1319 return Qnil;
1320
1321 args_left = args;
1322 GCPRO1 (args);
1323
1324 do
1325 {
1326 val = Feval (Fcar (Fcdr (args_left)));
1327 symbol = Fcar (args_left);
1328 Fset_default (symbol, val);
1329 args_left = Fcdr (Fcdr (args_left));
1330 }
1331 while (!NILP (args_left));
1332
1333 UNGCPRO;
1334 return val;
1335 }
1336 \f
1337 /* Lisp functions for creating and removing buffer-local variables. */
1338
1339 DEFUN ("make-variable-buffer-local", Fmake_variable_buffer_local, Smake_variable_buffer_local,
1340 1, 1, "vMake Variable Buffer Local: ",
1341 "Make VARIABLE become buffer-local whenever it is set.\n\
1342 At any time, the value for the current buffer is in effect,\n\
1343 unless the variable has never been set in this buffer,\n\
1344 in which case the default value is in effect.\n\
1345 Note that binding the variable with `let', or setting it while\n\
1346 a `let'-style binding made in this buffer is in effect,\n\
1347 does not make the variable buffer-local.\n\
1348 \n\
1349 The function `default-value' gets the default value and `set-default' sets it.")
1350 (variable)
1351 register Lisp_Object variable;
1352 {
1353 register Lisp_Object tem, valcontents, newval;
1354
1355 CHECK_SYMBOL (variable, 0);
1356
1357 valcontents = XSYMBOL (variable)->value;
1358 if (EQ (variable, Qnil) || EQ (variable, Qt) || KBOARD_OBJFWDP (valcontents))
1359 error ("Symbol %s may not be buffer-local", XSYMBOL (variable)->name->data);
1360
1361 if (BUFFER_LOCAL_VALUEP (valcontents) || BUFFER_OBJFWDP (valcontents))
1362 return variable;
1363 if (SOME_BUFFER_LOCAL_VALUEP (valcontents))
1364 {
1365 XMISCTYPE (XSYMBOL (variable)->value) = Lisp_Misc_Buffer_Local_Value;
1366 return variable;
1367 }
1368 if (EQ (valcontents, Qunbound))
1369 XSYMBOL (variable)->value = Qnil;
1370 tem = Fcons (Qnil, Fsymbol_value (variable));
1371 XCAR (tem) = tem;
1372 newval = allocate_misc ();
1373 XMISCTYPE (newval) = Lisp_Misc_Buffer_Local_Value;
1374 XBUFFER_LOCAL_VALUE (newval)->realvalue = XSYMBOL (variable)->value;
1375 XBUFFER_LOCAL_VALUE (newval)->buffer = Fcurrent_buffer ();
1376 XBUFFER_LOCAL_VALUE (newval)->frame = Qnil;
1377 XBUFFER_LOCAL_VALUE (newval)->found_for_buffer = 0;
1378 XBUFFER_LOCAL_VALUE (newval)->found_for_frame = 0;
1379 XBUFFER_LOCAL_VALUE (newval)->check_frame = 0;
1380 XBUFFER_LOCAL_VALUE (newval)->cdr = tem;
1381 XSYMBOL (variable)->value = newval;
1382 return variable;
1383 }
1384
1385 DEFUN ("make-local-variable", Fmake_local_variable, Smake_local_variable,
1386 1, 1, "vMake Local Variable: ",
1387 "Make VARIABLE have a separate value in the current buffer.\n\
1388 Other buffers will continue to share a common default value.\n\
1389 \(The buffer-local value of VARIABLE starts out as the same value\n\
1390 VARIABLE previously had. If VARIABLE was void, it remains void.\)\n\
1391 See also `make-variable-buffer-local'.\n\
1392 \n\
1393 If the variable is already arranged to become local when set,\n\
1394 this function causes a local value to exist for this buffer,\n\
1395 just as setting the variable would do.\n\
1396 \n\
1397 This function returns VARIABLE, and therefore\n\
1398 (set (make-local-variable 'VARIABLE) VALUE-EXP)\n\
1399 works.\n\
1400 \n\
1401 Do not use `make-local-variable' to make a hook variable buffer-local.\n\
1402 Use `make-local-hook' instead.")
1403 (variable)
1404 register Lisp_Object variable;
1405 {
1406 register Lisp_Object tem, valcontents;
1407
1408 CHECK_SYMBOL (variable, 0);
1409
1410 valcontents = XSYMBOL (variable)->value;
1411 if (EQ (variable, Qnil) || EQ (variable, Qt) || KBOARD_OBJFWDP (valcontents))
1412 error ("Symbol %s may not be buffer-local", XSYMBOL (variable)->name->data);
1413
1414 if (BUFFER_LOCAL_VALUEP (valcontents) || BUFFER_OBJFWDP (valcontents))
1415 {
1416 tem = Fboundp (variable);
1417
1418 /* Make sure the symbol has a local value in this particular buffer,
1419 by setting it to the same value it already has. */
1420 Fset (variable, (EQ (tem, Qt) ? Fsymbol_value (variable) : Qunbound));
1421 return variable;
1422 }
1423 /* Make sure symbol is set up to hold per-buffer values. */
1424 if (!SOME_BUFFER_LOCAL_VALUEP (valcontents))
1425 {
1426 Lisp_Object newval;
1427 tem = Fcons (Qnil, do_symval_forwarding (valcontents));
1428 XCAR (tem) = tem;
1429 newval = allocate_misc ();
1430 XMISCTYPE (newval) = Lisp_Misc_Some_Buffer_Local_Value;
1431 XBUFFER_LOCAL_VALUE (newval)->realvalue = XSYMBOL (variable)->value;
1432 XBUFFER_LOCAL_VALUE (newval)->buffer = Qnil;
1433 XBUFFER_LOCAL_VALUE (newval)->frame = Qnil;
1434 XBUFFER_LOCAL_VALUE (newval)->found_for_buffer = 0;
1435 XBUFFER_LOCAL_VALUE (newval)->found_for_frame = 0;
1436 XBUFFER_LOCAL_VALUE (newval)->check_frame = 0;
1437 XBUFFER_LOCAL_VALUE (newval)->cdr = tem;
1438 XSYMBOL (variable)->value = newval;
1439 }
1440 /* Make sure this buffer has its own value of symbol. */
1441 tem = Fassq (variable, current_buffer->local_var_alist);
1442 if (NILP (tem))
1443 {
1444 /* Swap out any local binding for some other buffer, and make
1445 sure the current value is permanently recorded, if it's the
1446 default value. */
1447 find_symbol_value (variable);
1448
1449 current_buffer->local_var_alist
1450 = Fcons (Fcons (variable, XCDR (XBUFFER_LOCAL_VALUE (XSYMBOL (variable)->value)->cdr)),
1451 current_buffer->local_var_alist);
1452
1453 /* Make sure symbol does not think it is set up for this buffer;
1454 force it to look once again for this buffer's value. */
1455 {
1456 Lisp_Object *pvalbuf;
1457
1458 valcontents = XSYMBOL (variable)->value;
1459
1460 pvalbuf = &XBUFFER_LOCAL_VALUE (valcontents)->buffer;
1461 if (current_buffer == XBUFFER (*pvalbuf))
1462 *pvalbuf = Qnil;
1463 XBUFFER_LOCAL_VALUE (valcontents)->found_for_buffer = 0;
1464 }
1465 }
1466
1467 /* If the symbol forwards into a C variable, then load the binding
1468 for this buffer now. If C code modifies the variable before we
1469 load the binding in, then that new value will clobber the default
1470 binding the next time we unload it. */
1471 valcontents = XBUFFER_LOCAL_VALUE (XSYMBOL (variable)->value)->realvalue;
1472 if (INTFWDP (valcontents) || BOOLFWDP (valcontents) || OBJFWDP (valcontents))
1473 swap_in_symval_forwarding (variable, XSYMBOL (variable)->value);
1474
1475 return variable;
1476 }
1477
1478 DEFUN ("kill-local-variable", Fkill_local_variable, Skill_local_variable,
1479 1, 1, "vKill Local Variable: ",
1480 "Make VARIABLE no longer have a separate value in the current buffer.\n\
1481 From now on the default value will apply in this buffer.")
1482 (variable)
1483 register Lisp_Object variable;
1484 {
1485 register Lisp_Object tem, valcontents;
1486
1487 CHECK_SYMBOL (variable, 0);
1488
1489 valcontents = XSYMBOL (variable)->value;
1490
1491 if (BUFFER_OBJFWDP (valcontents))
1492 {
1493 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1494 int idx = PER_BUFFER_IDX (offset);
1495
1496 if (idx > 0)
1497 {
1498 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 0);
1499 PER_BUFFER_VALUE (current_buffer, offset)
1500 = PER_BUFFER_DEFAULT (offset);
1501 }
1502 return variable;
1503 }
1504
1505 if (!BUFFER_LOCAL_VALUEP (valcontents)
1506 && !SOME_BUFFER_LOCAL_VALUEP (valcontents))
1507 return variable;
1508
1509 /* Get rid of this buffer's alist element, if any. */
1510
1511 tem = Fassq (variable, current_buffer->local_var_alist);
1512 if (!NILP (tem))
1513 current_buffer->local_var_alist
1514 = Fdelq (tem, current_buffer->local_var_alist);
1515
1516 /* If the symbol is set up with the current buffer's binding
1517 loaded, recompute its value. We have to do it now, or else
1518 forwarded objects won't work right. */
1519 {
1520 Lisp_Object *pvalbuf;
1521 valcontents = XSYMBOL (variable)->value;
1522 pvalbuf = &XBUFFER_LOCAL_VALUE (valcontents)->buffer;
1523 if (current_buffer == XBUFFER (*pvalbuf))
1524 {
1525 *pvalbuf = Qnil;
1526 XBUFFER_LOCAL_VALUE (valcontents)->found_for_buffer = 0;
1527 find_symbol_value (variable);
1528 }
1529 }
1530
1531 return variable;
1532 }
1533
1534 /* Lisp functions for creating and removing buffer-local variables. */
1535
1536 DEFUN ("make-variable-frame-local", Fmake_variable_frame_local, Smake_variable_frame_local,
1537 1, 1, "vMake Variable Frame Local: ",
1538 "Enable VARIABLE to have frame-local bindings.\n\
1539 When a frame-local binding exists in the current frame,\n\
1540 it is in effect whenever the current buffer has no buffer-local binding.\n\
1541 A frame-local binding is actual a frame parameter value;\n\
1542 thus, any given frame has a local binding for VARIABLE\n\
1543 if it has a value for the frame parameter named VARIABLE.\n\
1544 See `modify-frame-parameters'.")
1545 (variable)
1546 register Lisp_Object variable;
1547 {
1548 register Lisp_Object tem, valcontents, newval;
1549
1550 CHECK_SYMBOL (variable, 0);
1551
1552 valcontents = XSYMBOL (variable)->value;
1553 if (EQ (variable, Qnil) || EQ (variable, Qt) || KBOARD_OBJFWDP (valcontents)
1554 || BUFFER_OBJFWDP (valcontents))
1555 error ("Symbol %s may not be frame-local", XSYMBOL (variable)->name->data);
1556
1557 if (BUFFER_LOCAL_VALUEP (valcontents)
1558 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
1559 {
1560 XBUFFER_LOCAL_VALUE (valcontents)->check_frame = 1;
1561 return variable;
1562 }
1563
1564 if (EQ (valcontents, Qunbound))
1565 XSYMBOL (variable)->value = Qnil;
1566 tem = Fcons (Qnil, Fsymbol_value (variable));
1567 XCAR (tem) = tem;
1568 newval = allocate_misc ();
1569 XMISCTYPE (newval) = Lisp_Misc_Some_Buffer_Local_Value;
1570 XBUFFER_LOCAL_VALUE (newval)->realvalue = XSYMBOL (variable)->value;
1571 XBUFFER_LOCAL_VALUE (newval)->buffer = Qnil;
1572 XBUFFER_LOCAL_VALUE (newval)->frame = Qnil;
1573 XBUFFER_LOCAL_VALUE (newval)->found_for_buffer = 0;
1574 XBUFFER_LOCAL_VALUE (newval)->found_for_frame = 0;
1575 XBUFFER_LOCAL_VALUE (newval)->check_frame = 1;
1576 XBUFFER_LOCAL_VALUE (newval)->cdr = tem;
1577 XSYMBOL (variable)->value = newval;
1578 return variable;
1579 }
1580
1581 DEFUN ("local-variable-p", Flocal_variable_p, Slocal_variable_p,
1582 1, 2, 0,
1583 "Non-nil if VARIABLE has a local binding in buffer BUFFER.\n\
1584 BUFFER defaults to the current buffer.")
1585 (variable, buffer)
1586 register Lisp_Object variable, buffer;
1587 {
1588 Lisp_Object valcontents;
1589 register struct buffer *buf;
1590
1591 if (NILP (buffer))
1592 buf = current_buffer;
1593 else
1594 {
1595 CHECK_BUFFER (buffer, 0);
1596 buf = XBUFFER (buffer);
1597 }
1598
1599 CHECK_SYMBOL (variable, 0);
1600
1601 valcontents = XSYMBOL (variable)->value;
1602 if (BUFFER_LOCAL_VALUEP (valcontents)
1603 || SOME_BUFFER_LOCAL_VALUEP (valcontents))
1604 {
1605 Lisp_Object tail, elt;
1606 for (tail = buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
1607 {
1608 elt = XCAR (tail);
1609 if (EQ (variable, XCAR (elt)))
1610 return Qt;
1611 }
1612 }
1613 if (BUFFER_OBJFWDP (valcontents))
1614 {
1615 int offset = XBUFFER_OBJFWD (valcontents)->offset;
1616 int idx = PER_BUFFER_IDX (offset);
1617 if (idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
1618 return Qt;
1619 }
1620 return Qnil;
1621 }
1622
1623 DEFUN ("local-variable-if-set-p", Flocal_variable_if_set_p, Slocal_variable_if_set_p,
1624 1, 2, 0,
1625 "Non-nil if VARIABLE will be local in buffer BUFFER if it is set there.\n\
1626 BUFFER defaults to the current buffer.")
1627 (variable, buffer)
1628 register Lisp_Object variable, buffer;
1629 {
1630 Lisp_Object valcontents;
1631 register struct buffer *buf;
1632
1633 if (NILP (buffer))
1634 buf = current_buffer;
1635 else
1636 {
1637 CHECK_BUFFER (buffer, 0);
1638 buf = XBUFFER (buffer);
1639 }
1640
1641 CHECK_SYMBOL (variable, 0);
1642
1643 valcontents = XSYMBOL (variable)->value;
1644
1645 /* This means that make-variable-buffer-local was done. */
1646 if (BUFFER_LOCAL_VALUEP (valcontents))
1647 return Qt;
1648 /* All these slots become local if they are set. */
1649 if (BUFFER_OBJFWDP (valcontents))
1650 return Qt;
1651 if (SOME_BUFFER_LOCAL_VALUEP (valcontents))
1652 {
1653 Lisp_Object tail, elt;
1654 for (tail = buf->local_var_alist; CONSP (tail); tail = XCDR (tail))
1655 {
1656 elt = XCAR (tail);
1657 if (EQ (variable, XCAR (elt)))
1658 return Qt;
1659 }
1660 }
1661 return Qnil;
1662 }
1663 \f
1664 /* Find the function at the end of a chain of symbol function indirections. */
1665
1666 /* If OBJECT is a symbol, find the end of its function chain and
1667 return the value found there. If OBJECT is not a symbol, just
1668 return it. If there is a cycle in the function chain, signal a
1669 cyclic-function-indirection error.
1670
1671 This is like Findirect_function, except that it doesn't signal an
1672 error if the chain ends up unbound. */
1673 Lisp_Object
1674 indirect_function (object)
1675 register Lisp_Object object;
1676 {
1677 Lisp_Object tortoise, hare;
1678
1679 hare = tortoise = object;
1680
1681 for (;;)
1682 {
1683 if (!SYMBOLP (hare) || EQ (hare, Qunbound))
1684 break;
1685 hare = XSYMBOL (hare)->function;
1686 if (!SYMBOLP (hare) || EQ (hare, Qunbound))
1687 break;
1688 hare = XSYMBOL (hare)->function;
1689
1690 tortoise = XSYMBOL (tortoise)->function;
1691
1692 if (EQ (hare, tortoise))
1693 Fsignal (Qcyclic_function_indirection, Fcons (object, Qnil));
1694 }
1695
1696 return hare;
1697 }
1698
1699 DEFUN ("indirect-function", Findirect_function, Sindirect_function, 1, 1, 0,
1700 "Return the function at the end of OBJECT's function chain.\n\
1701 If OBJECT is a symbol, follow all function indirections and return the final\n\
1702 function binding.\n\
1703 If OBJECT is not a symbol, just return it.\n\
1704 Signal a void-function error if the final symbol is unbound.\n\
1705 Signal a cyclic-function-indirection error if there is a loop in the\n\
1706 function chain of symbols.")
1707 (object)
1708 register Lisp_Object object;
1709 {
1710 Lisp_Object result;
1711
1712 result = indirect_function (object);
1713
1714 if (EQ (result, Qunbound))
1715 return Fsignal (Qvoid_function, Fcons (object, Qnil));
1716 return result;
1717 }
1718 \f
1719 /* Extract and set vector and string elements */
1720
1721 DEFUN ("aref", Faref, Saref, 2, 2, 0,
1722 "Return the element of ARRAY at index IDX.\n\
1723 ARRAY may be a vector, a string, a char-table, a bool-vector,\n\
1724 or a byte-code object. IDX starts at 0.")
1725 (array, idx)
1726 register Lisp_Object array;
1727 Lisp_Object idx;
1728 {
1729 register int idxval;
1730
1731 CHECK_NUMBER (idx, 1);
1732 idxval = XINT (idx);
1733 if (STRINGP (array))
1734 {
1735 int c, idxval_byte;
1736
1737 if (idxval < 0 || idxval >= XSTRING (array)->size)
1738 args_out_of_range (array, idx);
1739 if (! STRING_MULTIBYTE (array))
1740 return make_number ((unsigned char) XSTRING (array)->data[idxval]);
1741 idxval_byte = string_char_to_byte (array, idxval);
1742
1743 c = STRING_CHAR (&XSTRING (array)->data[idxval_byte],
1744 STRING_BYTES (XSTRING (array)) - idxval_byte);
1745 return make_number (c);
1746 }
1747 else if (BOOL_VECTOR_P (array))
1748 {
1749 int val;
1750
1751 if (idxval < 0 || idxval >= XBOOL_VECTOR (array)->size)
1752 args_out_of_range (array, idx);
1753
1754 val = (unsigned char) XBOOL_VECTOR (array)->data[idxval / BITS_PER_CHAR];
1755 return (val & (1 << (idxval % BITS_PER_CHAR)) ? Qt : Qnil);
1756 }
1757 else if (CHAR_TABLE_P (array))
1758 {
1759 Lisp_Object val;
1760
1761 val = Qnil;
1762
1763 if (idxval < 0)
1764 args_out_of_range (array, idx);
1765 if (idxval < CHAR_TABLE_ORDINARY_SLOTS)
1766 {
1767 /* For ASCII and 8-bit European characters, the element is
1768 stored in the top table. */
1769 val = XCHAR_TABLE (array)->contents[idxval];
1770 if (NILP (val))
1771 val = XCHAR_TABLE (array)->defalt;
1772 while (NILP (val)) /* Follow parents until we find some value. */
1773 {
1774 array = XCHAR_TABLE (array)->parent;
1775 if (NILP (array))
1776 return Qnil;
1777 val = XCHAR_TABLE (array)->contents[idxval];
1778 if (NILP (val))
1779 val = XCHAR_TABLE (array)->defalt;
1780 }
1781 return val;
1782 }
1783 else
1784 {
1785 int code[4], i;
1786 Lisp_Object sub_table;
1787
1788 SPLIT_CHAR (idxval, code[0], code[1], code[2]);
1789 if (code[1] < 32) code[1] = -1;
1790 else if (code[2] < 32) code[2] = -1;
1791
1792 /* Here, the possible range of CODE[0] (== charset ID) is
1793 128..MAX_CHARSET. Since the top level char table contains
1794 data for multibyte characters after 256th element, we must
1795 increment CODE[0] by 128 to get a correct index. */
1796 code[0] += 128;
1797 code[3] = -1; /* anchor */
1798
1799 try_parent_char_table:
1800 sub_table = array;
1801 for (i = 0; code[i] >= 0; i++)
1802 {
1803 val = XCHAR_TABLE (sub_table)->contents[code[i]];
1804 if (SUB_CHAR_TABLE_P (val))
1805 sub_table = val;
1806 else
1807 {
1808 if (NILP (val))
1809 val = XCHAR_TABLE (sub_table)->defalt;
1810 if (NILP (val))
1811 {
1812 array = XCHAR_TABLE (array)->parent;
1813 if (!NILP (array))
1814 goto try_parent_char_table;
1815 }
1816 return val;
1817 }
1818 }
1819 /* Here, VAL is a sub char table. We try the default value
1820 and parent. */
1821 val = XCHAR_TABLE (val)->defalt;
1822 if (NILP (val))
1823 {
1824 array = XCHAR_TABLE (array)->parent;
1825 if (!NILP (array))
1826 goto try_parent_char_table;
1827 }
1828 return val;
1829 }
1830 }
1831 else
1832 {
1833 int size = 0;
1834 if (VECTORP (array))
1835 size = XVECTOR (array)->size;
1836 else if (COMPILEDP (array))
1837 size = XVECTOR (array)->size & PSEUDOVECTOR_SIZE_MASK;
1838 else
1839 wrong_type_argument (Qarrayp, array);
1840
1841 if (idxval < 0 || idxval >= size)
1842 args_out_of_range (array, idx);
1843 return XVECTOR (array)->contents[idxval];
1844 }
1845 }
1846
1847 /* Don't use alloca for relocating string data larger than this, lest
1848 we overflow their stack. The value is the same as what used in
1849 fns.c for base64 handling. */
1850 #define MAX_ALLOCA 16*1024
1851
1852 DEFUN ("aset", Faset, Saset, 3, 3, 0,
1853 "Store into the element of ARRAY at index IDX the value NEWELT.\n\
1854 ARRAY may be a vector, a string, a char-table or a bool-vector.\n\
1855 IDX starts at 0.")
1856 (array, idx, newelt)
1857 register Lisp_Object array;
1858 Lisp_Object idx, newelt;
1859 {
1860 register int idxval;
1861
1862 CHECK_NUMBER (idx, 1);
1863 idxval = XINT (idx);
1864 if (!VECTORP (array) && !STRINGP (array) && !BOOL_VECTOR_P (array)
1865 && ! CHAR_TABLE_P (array))
1866 array = wrong_type_argument (Qarrayp, array);
1867 CHECK_IMPURE (array);
1868
1869 if (VECTORP (array))
1870 {
1871 if (idxval < 0 || idxval >= XVECTOR (array)->size)
1872 args_out_of_range (array, idx);
1873 XVECTOR (array)->contents[idxval] = newelt;
1874 }
1875 else if (BOOL_VECTOR_P (array))
1876 {
1877 int val;
1878
1879 if (idxval < 0 || idxval >= XBOOL_VECTOR (array)->size)
1880 args_out_of_range (array, idx);
1881
1882 val = (unsigned char) XBOOL_VECTOR (array)->data[idxval / BITS_PER_CHAR];
1883
1884 if (! NILP (newelt))
1885 val |= 1 << (idxval % BITS_PER_CHAR);
1886 else
1887 val &= ~(1 << (idxval % BITS_PER_CHAR));
1888 XBOOL_VECTOR (array)->data[idxval / BITS_PER_CHAR] = val;
1889 }
1890 else if (CHAR_TABLE_P (array))
1891 {
1892 if (idxval < 0)
1893 args_out_of_range (array, idx);
1894 if (idxval < CHAR_TABLE_ORDINARY_SLOTS)
1895 XCHAR_TABLE (array)->contents[idxval] = newelt;
1896 else
1897 {
1898 int code[4], i;
1899 Lisp_Object val;
1900
1901 SPLIT_CHAR (idxval, code[0], code[1], code[2]);
1902 if (code[1] < 32) code[1] = -1;
1903 else if (code[2] < 32) code[2] = -1;
1904
1905 /* See the comment of the corresponding part in Faref. */
1906 code[0] += 128;
1907 code[3] = -1; /* anchor */
1908 for (i = 0; code[i + 1] >= 0; i++)
1909 {
1910 val = XCHAR_TABLE (array)->contents[code[i]];
1911 if (SUB_CHAR_TABLE_P (val))
1912 array = val;
1913 else
1914 {
1915 Lisp_Object temp;
1916
1917 /* VAL is a leaf. Create a sub char table with the
1918 default value VAL or XCHAR_TABLE (array)->defalt
1919 and look into it. */
1920
1921 temp = make_sub_char_table (NILP (val)
1922 ? XCHAR_TABLE (array)->defalt
1923 : val);
1924 XCHAR_TABLE (array)->contents[code[i]] = temp;
1925 array = temp;
1926 }
1927 }
1928 XCHAR_TABLE (array)->contents[code[i]] = newelt;
1929 }
1930 }
1931 else if (STRING_MULTIBYTE (array))
1932 {
1933 int idxval_byte, prev_bytes, new_bytes;
1934 unsigned char workbuf[MAX_MULTIBYTE_LENGTH], *p0 = workbuf, *p1;
1935
1936 if (idxval < 0 || idxval >= XSTRING (array)->size)
1937 args_out_of_range (array, idx);
1938 CHECK_NUMBER (newelt, 2);
1939
1940 idxval_byte = string_char_to_byte (array, idxval);
1941 p1 = &XSTRING (array)->data[idxval_byte];
1942 PARSE_MULTIBYTE_SEQ (p1, nbytes - idxval_byte, prev_bytes);
1943 new_bytes = CHAR_STRING (XINT (newelt), p0);
1944 if (prev_bytes != new_bytes)
1945 {
1946 /* We must relocate the string data. */
1947 int nchars = XSTRING (array)->size;
1948 int nbytes = STRING_BYTES (XSTRING (array));
1949 unsigned char *str;
1950
1951 str = (nbytes <= MAX_ALLOCA
1952 ? (unsigned char *) alloca (nbytes)
1953 : (unsigned char *) xmalloc (nbytes));
1954 bcopy (XSTRING (array)->data, str, nbytes);
1955 allocate_string_data (XSTRING (array), nchars,
1956 nbytes + new_bytes - prev_bytes);
1957 bcopy (str, XSTRING (array)->data, idxval_byte);
1958 p1 = XSTRING (array)->data + idxval_byte;
1959 bcopy (str + idxval_byte + prev_bytes, p1 + new_bytes,
1960 nbytes - (idxval_byte + prev_bytes));
1961 if (nbytes > MAX_ALLOCA)
1962 xfree (str);
1963 clear_string_char_byte_cache ();
1964 }
1965 while (new_bytes--)
1966 *p1++ = *p0++;
1967 }
1968 else
1969 {
1970 if (idxval < 0 || idxval >= XSTRING (array)->size)
1971 args_out_of_range (array, idx);
1972 CHECK_NUMBER (newelt, 2);
1973
1974 if (XINT (newelt) < 0 || SINGLE_BYTE_CHAR_P (XINT (newelt)))
1975 XSTRING (array)->data[idxval] = XINT (newelt);
1976 else
1977 {
1978 /* We must relocate the string data while converting it to
1979 multibyte. */
1980 int idxval_byte, prev_bytes, new_bytes;
1981 unsigned char workbuf[MAX_MULTIBYTE_LENGTH], *p0 = workbuf, *p1;
1982 unsigned char *origstr = XSTRING (array)->data, *str;
1983 int nchars, nbytes;
1984
1985 nchars = XSTRING (array)->size;
1986 nbytes = idxval_byte = count_size_as_multibyte (origstr, idxval);
1987 nbytes += count_size_as_multibyte (origstr + idxval,
1988 nchars - idxval);
1989 str = (nbytes <= MAX_ALLOCA
1990 ? (unsigned char *) alloca (nbytes)
1991 : (unsigned char *) xmalloc (nbytes));
1992 copy_text (XSTRING (array)->data, str, nchars, 0, 1);
1993 PARSE_MULTIBYTE_SEQ (str + idxval_byte, nbytes - idxval_byte,
1994 prev_bytes);
1995 new_bytes = CHAR_STRING (XINT (newelt), p0);
1996 allocate_string_data (XSTRING (array), nchars,
1997 nbytes + new_bytes - prev_bytes);
1998 bcopy (str, XSTRING (array)->data, idxval_byte);
1999 p1 = XSTRING (array)->data + idxval_byte;
2000 while (new_bytes--)
2001 *p1++ = *p0++;
2002 bcopy (str + idxval_byte + prev_bytes, p1,
2003 nbytes - (idxval_byte + prev_bytes));
2004 if (nbytes > MAX_ALLOCA)
2005 xfree (str);
2006 clear_string_char_byte_cache ();
2007 }
2008 }
2009
2010 return newelt;
2011 }
2012 \f
2013 /* Arithmetic functions */
2014
2015 enum comparison { equal, notequal, less, grtr, less_or_equal, grtr_or_equal };
2016
2017 Lisp_Object
2018 arithcompare (num1, num2, comparison)
2019 Lisp_Object num1, num2;
2020 enum comparison comparison;
2021 {
2022 double f1 = 0, f2 = 0;
2023 int floatp = 0;
2024
2025 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num1, 0);
2026 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num2, 0);
2027
2028 if (FLOATP (num1) || FLOATP (num2))
2029 {
2030 floatp = 1;
2031 f1 = (FLOATP (num1)) ? XFLOAT_DATA (num1) : XINT (num1);
2032 f2 = (FLOATP (num2)) ? XFLOAT_DATA (num2) : XINT (num2);
2033 }
2034
2035 switch (comparison)
2036 {
2037 case equal:
2038 if (floatp ? f1 == f2 : XINT (num1) == XINT (num2))
2039 return Qt;
2040 return Qnil;
2041
2042 case notequal:
2043 if (floatp ? f1 != f2 : XINT (num1) != XINT (num2))
2044 return Qt;
2045 return Qnil;
2046
2047 case less:
2048 if (floatp ? f1 < f2 : XINT (num1) < XINT (num2))
2049 return Qt;
2050 return Qnil;
2051
2052 case less_or_equal:
2053 if (floatp ? f1 <= f2 : XINT (num1) <= XINT (num2))
2054 return Qt;
2055 return Qnil;
2056
2057 case grtr:
2058 if (floatp ? f1 > f2 : XINT (num1) > XINT (num2))
2059 return Qt;
2060 return Qnil;
2061
2062 case grtr_or_equal:
2063 if (floatp ? f1 >= f2 : XINT (num1) >= XINT (num2))
2064 return Qt;
2065 return Qnil;
2066
2067 default:
2068 abort ();
2069 }
2070 }
2071
2072 DEFUN ("=", Feqlsign, Seqlsign, 2, 2, 0,
2073 "Return t if two args, both numbers or markers, are equal.")
2074 (num1, num2)
2075 register Lisp_Object num1, num2;
2076 {
2077 return arithcompare (num1, num2, equal);
2078 }
2079
2080 DEFUN ("<", Flss, Slss, 2, 2, 0,
2081 "Return t if first arg is less than second arg. Both must be numbers or markers.")
2082 (num1, num2)
2083 register Lisp_Object num1, num2;
2084 {
2085 return arithcompare (num1, num2, less);
2086 }
2087
2088 DEFUN (">", Fgtr, Sgtr, 2, 2, 0,
2089 "Return t if first arg is greater than second arg. Both must be numbers or markers.")
2090 (num1, num2)
2091 register Lisp_Object num1, num2;
2092 {
2093 return arithcompare (num1, num2, grtr);
2094 }
2095
2096 DEFUN ("<=", Fleq, Sleq, 2, 2, 0,
2097 "Return t if first arg is less than or equal to second arg.\n\
2098 Both must be numbers or markers.")
2099 (num1, num2)
2100 register Lisp_Object num1, num2;
2101 {
2102 return arithcompare (num1, num2, less_or_equal);
2103 }
2104
2105 DEFUN (">=", Fgeq, Sgeq, 2, 2, 0,
2106 "Return t if first arg is greater than or equal to second arg.\n\
2107 Both must be numbers or markers.")
2108 (num1, num2)
2109 register Lisp_Object num1, num2;
2110 {
2111 return arithcompare (num1, num2, grtr_or_equal);
2112 }
2113
2114 DEFUN ("/=", Fneq, Sneq, 2, 2, 0,
2115 "Return t if first arg is not equal to second arg. Both must be numbers or markers.")
2116 (num1, num2)
2117 register Lisp_Object num1, num2;
2118 {
2119 return arithcompare (num1, num2, notequal);
2120 }
2121
2122 DEFUN ("zerop", Fzerop, Szerop, 1, 1, 0, "Return t if NUMBER is zero.")
2123 (number)
2124 register Lisp_Object number;
2125 {
2126 CHECK_NUMBER_OR_FLOAT (number, 0);
2127
2128 if (FLOATP (number))
2129 {
2130 if (XFLOAT_DATA (number) == 0.0)
2131 return Qt;
2132 return Qnil;
2133 }
2134
2135 if (!XINT (number))
2136 return Qt;
2137 return Qnil;
2138 }
2139 \f
2140 /* Convert between long values and pairs of Lisp integers. */
2141
2142 Lisp_Object
2143 long_to_cons (i)
2144 unsigned long i;
2145 {
2146 unsigned int top = i >> 16;
2147 unsigned int bot = i & 0xFFFF;
2148 if (top == 0)
2149 return make_number (bot);
2150 if (top == (unsigned long)-1 >> 16)
2151 return Fcons (make_number (-1), make_number (bot));
2152 return Fcons (make_number (top), make_number (bot));
2153 }
2154
2155 unsigned long
2156 cons_to_long (c)
2157 Lisp_Object c;
2158 {
2159 Lisp_Object top, bot;
2160 if (INTEGERP (c))
2161 return XINT (c);
2162 top = XCAR (c);
2163 bot = XCDR (c);
2164 if (CONSP (bot))
2165 bot = XCAR (bot);
2166 return ((XINT (top) << 16) | XINT (bot));
2167 }
2168 \f
2169 DEFUN ("number-to-string", Fnumber_to_string, Snumber_to_string, 1, 1, 0,
2170 "Convert NUMBER to a string by printing it in decimal.\n\
2171 Uses a minus sign if negative.\n\
2172 NUMBER may be an integer or a floating point number.")
2173 (number)
2174 Lisp_Object number;
2175 {
2176 char buffer[VALBITS];
2177
2178 CHECK_NUMBER_OR_FLOAT (number, 0);
2179
2180 if (FLOATP (number))
2181 {
2182 char pigbuf[350]; /* see comments in float_to_string */
2183
2184 float_to_string (pigbuf, XFLOAT_DATA (number));
2185 return build_string (pigbuf);
2186 }
2187
2188 if (sizeof (int) == sizeof (EMACS_INT))
2189 sprintf (buffer, "%d", XINT (number));
2190 else if (sizeof (long) == sizeof (EMACS_INT))
2191 sprintf (buffer, "%ld", (long) XINT (number));
2192 else
2193 abort ();
2194 return build_string (buffer);
2195 }
2196
2197 INLINE static int
2198 digit_to_number (character, base)
2199 int character, base;
2200 {
2201 int digit;
2202
2203 if (character >= '0' && character <= '9')
2204 digit = character - '0';
2205 else if (character >= 'a' && character <= 'z')
2206 digit = character - 'a' + 10;
2207 else if (character >= 'A' && character <= 'Z')
2208 digit = character - 'A' + 10;
2209 else
2210 return -1;
2211
2212 if (digit >= base)
2213 return -1;
2214 else
2215 return digit;
2216 }
2217
2218 DEFUN ("string-to-number", Fstring_to_number, Sstring_to_number, 1, 2, 0,
2219 "Convert STRING to a number by parsing it as a decimal number.\n\
2220 This parses both integers and floating point numbers.\n\
2221 It ignores leading spaces and tabs.\n\
2222 \n\
2223 If BASE, interpret STRING as a number in that base. If BASE isn't\n\
2224 present, base 10 is used. BASE must be between 2 and 16 (inclusive).\n\
2225 If the base used is not 10, floating point is not recognized.")
2226 (string, base)
2227 register Lisp_Object string, base;
2228 {
2229 register unsigned char *p;
2230 register int b;
2231 int sign = 1;
2232 Lisp_Object val;
2233
2234 CHECK_STRING (string, 0);
2235
2236 if (NILP (base))
2237 b = 10;
2238 else
2239 {
2240 CHECK_NUMBER (base, 1);
2241 b = XINT (base);
2242 if (b < 2 || b > 16)
2243 Fsignal (Qargs_out_of_range, Fcons (base, Qnil));
2244 }
2245
2246 /* Skip any whitespace at the front of the number. Some versions of
2247 atoi do this anyway, so we might as well make Emacs lisp consistent. */
2248 p = XSTRING (string)->data;
2249 while (*p == ' ' || *p == '\t')
2250 p++;
2251
2252 if (*p == '-')
2253 {
2254 sign = -1;
2255 p++;
2256 }
2257 else if (*p == '+')
2258 p++;
2259
2260 if (isfloat_string (p) && b == 10)
2261 val = make_float (sign * atof (p));
2262 else
2263 {
2264 double v = 0;
2265
2266 while (1)
2267 {
2268 int digit = digit_to_number (*p++, b);
2269 if (digit < 0)
2270 break;
2271 v = v * b + digit;
2272 }
2273
2274 if (v > (EMACS_UINT) (VALMASK >> 1))
2275 val = make_float (sign * v);
2276 else
2277 val = make_number (sign * (int) v);
2278 }
2279
2280 return val;
2281 }
2282
2283 \f
2284 enum arithop
2285 {
2286 Aadd,
2287 Asub,
2288 Amult,
2289 Adiv,
2290 Alogand,
2291 Alogior,
2292 Alogxor,
2293 Amax,
2294 Amin
2295 };
2296
2297 static Lisp_Object float_arith_driver P_ ((double, int, enum arithop,
2298 int, Lisp_Object *));
2299 extern Lisp_Object fmod_float ();
2300
2301 Lisp_Object
2302 arith_driver (code, nargs, args)
2303 enum arithop code;
2304 int nargs;
2305 register Lisp_Object *args;
2306 {
2307 register Lisp_Object val;
2308 register int argnum;
2309 register EMACS_INT accum = 0;
2310 register EMACS_INT next;
2311
2312 switch (SWITCH_ENUM_CAST (code))
2313 {
2314 case Alogior:
2315 case Alogxor:
2316 case Aadd:
2317 case Asub:
2318 accum = 0;
2319 break;
2320 case Amult:
2321 accum = 1;
2322 break;
2323 case Alogand:
2324 accum = -1;
2325 break;
2326 default:
2327 break;
2328 }
2329
2330 for (argnum = 0; argnum < nargs; argnum++)
2331 {
2332 /* Using args[argnum] as argument to CHECK_NUMBER_... */
2333 val = args[argnum];
2334 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val, argnum);
2335
2336 if (FLOATP (val))
2337 return float_arith_driver ((double) accum, argnum, code,
2338 nargs, args);
2339 args[argnum] = val;
2340 next = XINT (args[argnum]);
2341 switch (SWITCH_ENUM_CAST (code))
2342 {
2343 case Aadd:
2344 accum += next;
2345 break;
2346 case Asub:
2347 accum = argnum ? accum - next : nargs == 1 ? - next : next;
2348 break;
2349 case Amult:
2350 accum *= next;
2351 break;
2352 case Adiv:
2353 if (!argnum)
2354 accum = next;
2355 else
2356 {
2357 if (next == 0)
2358 Fsignal (Qarith_error, Qnil);
2359 accum /= next;
2360 }
2361 break;
2362 case Alogand:
2363 accum &= next;
2364 break;
2365 case Alogior:
2366 accum |= next;
2367 break;
2368 case Alogxor:
2369 accum ^= next;
2370 break;
2371 case Amax:
2372 if (!argnum || next > accum)
2373 accum = next;
2374 break;
2375 case Amin:
2376 if (!argnum || next < accum)
2377 accum = next;
2378 break;
2379 }
2380 }
2381
2382 XSETINT (val, accum);
2383 return val;
2384 }
2385
2386 #undef isnan
2387 #define isnan(x) ((x) != (x))
2388
2389 static Lisp_Object
2390 float_arith_driver (accum, argnum, code, nargs, args)
2391 double accum;
2392 register int argnum;
2393 enum arithop code;
2394 int nargs;
2395 register Lisp_Object *args;
2396 {
2397 register Lisp_Object val;
2398 double next;
2399
2400 for (; argnum < nargs; argnum++)
2401 {
2402 val = args[argnum]; /* using args[argnum] as argument to CHECK_NUMBER_... */
2403 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val, argnum);
2404
2405 if (FLOATP (val))
2406 {
2407 next = XFLOAT_DATA (val);
2408 }
2409 else
2410 {
2411 args[argnum] = val; /* runs into a compiler bug. */
2412 next = XINT (args[argnum]);
2413 }
2414 switch (SWITCH_ENUM_CAST (code))
2415 {
2416 case Aadd:
2417 accum += next;
2418 break;
2419 case Asub:
2420 accum = argnum ? accum - next : nargs == 1 ? - next : next;
2421 break;
2422 case Amult:
2423 accum *= next;
2424 break;
2425 case Adiv:
2426 if (!argnum)
2427 accum = next;
2428 else
2429 {
2430 if (! IEEE_FLOATING_POINT && next == 0)
2431 Fsignal (Qarith_error, Qnil);
2432 accum /= next;
2433 }
2434 break;
2435 case Alogand:
2436 case Alogior:
2437 case Alogxor:
2438 return wrong_type_argument (Qinteger_or_marker_p, val);
2439 case Amax:
2440 if (!argnum || isnan (next) || next > accum)
2441 accum = next;
2442 break;
2443 case Amin:
2444 if (!argnum || isnan (next) || next < accum)
2445 accum = next;
2446 break;
2447 }
2448 }
2449
2450 return make_float (accum);
2451 }
2452
2453
2454 DEFUN ("+", Fplus, Splus, 0, MANY, 0,
2455 "Return sum of any number of arguments, which are numbers or markers.")
2456 (nargs, args)
2457 int nargs;
2458 Lisp_Object *args;
2459 {
2460 return arith_driver (Aadd, nargs, args);
2461 }
2462
2463 DEFUN ("-", Fminus, Sminus, 0, MANY, 0,
2464 "Negate number or subtract numbers or markers.\n\
2465 With one arg, negates it. With more than one arg,\n\
2466 subtracts all but the first from the first.")
2467 (nargs, args)
2468 int nargs;
2469 Lisp_Object *args;
2470 {
2471 return arith_driver (Asub, nargs, args);
2472 }
2473
2474 DEFUN ("*", Ftimes, Stimes, 0, MANY, 0,
2475 "Returns product of any number of arguments, which are numbers or markers.")
2476 (nargs, args)
2477 int nargs;
2478 Lisp_Object *args;
2479 {
2480 return arith_driver (Amult, nargs, args);
2481 }
2482
2483 DEFUN ("/", Fquo, Squo, 2, MANY, 0,
2484 "Returns first argument divided by all the remaining arguments.\n\
2485 The arguments must be numbers or markers.")
2486 (nargs, args)
2487 int nargs;
2488 Lisp_Object *args;
2489 {
2490 return arith_driver (Adiv, nargs, args);
2491 }
2492
2493 DEFUN ("%", Frem, Srem, 2, 2, 0,
2494 "Returns remainder of X divided by Y.\n\
2495 Both must be integers or markers.")
2496 (x, y)
2497 register Lisp_Object x, y;
2498 {
2499 Lisp_Object val;
2500
2501 CHECK_NUMBER_COERCE_MARKER (x, 0);
2502 CHECK_NUMBER_COERCE_MARKER (y, 1);
2503
2504 if (XFASTINT (y) == 0)
2505 Fsignal (Qarith_error, Qnil);
2506
2507 XSETINT (val, XINT (x) % XINT (y));
2508 return val;
2509 }
2510
2511 #ifndef HAVE_FMOD
2512 double
2513 fmod (f1, f2)
2514 double f1, f2;
2515 {
2516 double r = f1;
2517
2518 if (f2 < 0.0)
2519 f2 = -f2;
2520
2521 /* If the magnitude of the result exceeds that of the divisor, or
2522 the sign of the result does not agree with that of the dividend,
2523 iterate with the reduced value. This does not yield a
2524 particularly accurate result, but at least it will be in the
2525 range promised by fmod. */
2526 do
2527 r -= f2 * floor (r / f2);
2528 while (f2 <= (r < 0 ? -r : r) || ((r < 0) != (f1 < 0) && ! isnan (r)));
2529
2530 return r;
2531 }
2532 #endif /* ! HAVE_FMOD */
2533
2534 DEFUN ("mod", Fmod, Smod, 2, 2, 0,
2535 "Returns X modulo Y.\n\
2536 The result falls between zero (inclusive) and Y (exclusive).\n\
2537 Both X and Y must be numbers or markers.")
2538 (x, y)
2539 register Lisp_Object x, y;
2540 {
2541 Lisp_Object val;
2542 EMACS_INT i1, i2;
2543
2544 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (x, 0);
2545 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (y, 1);
2546
2547 if (FLOATP (x) || FLOATP (y))
2548 return fmod_float (x, y);
2549
2550 i1 = XINT (x);
2551 i2 = XINT (y);
2552
2553 if (i2 == 0)
2554 Fsignal (Qarith_error, Qnil);
2555
2556 i1 %= i2;
2557
2558 /* If the "remainder" comes out with the wrong sign, fix it. */
2559 if (i2 < 0 ? i1 > 0 : i1 < 0)
2560 i1 += i2;
2561
2562 XSETINT (val, i1);
2563 return val;
2564 }
2565
2566 DEFUN ("max", Fmax, Smax, 1, MANY, 0,
2567 "Return largest of all the arguments (which must be numbers or markers).\n\
2568 The value is always a number; markers are converted to numbers.")
2569 (nargs, args)
2570 int nargs;
2571 Lisp_Object *args;
2572 {
2573 return arith_driver (Amax, nargs, args);
2574 }
2575
2576 DEFUN ("min", Fmin, Smin, 1, MANY, 0,
2577 "Return smallest of all the arguments (which must be numbers or markers).\n\
2578 The value is always a number; markers are converted to numbers.")
2579 (nargs, args)
2580 int nargs;
2581 Lisp_Object *args;
2582 {
2583 return arith_driver (Amin, nargs, args);
2584 }
2585
2586 DEFUN ("logand", Flogand, Slogand, 0, MANY, 0,
2587 "Return bitwise-and of all the arguments.\n\
2588 Arguments may be integers, or markers converted to integers.")
2589 (nargs, args)
2590 int nargs;
2591 Lisp_Object *args;
2592 {
2593 return arith_driver (Alogand, nargs, args);
2594 }
2595
2596 DEFUN ("logior", Flogior, Slogior, 0, MANY, 0,
2597 "Return bitwise-or of all the arguments.\n\
2598 Arguments may be integers, or markers converted to integers.")
2599 (nargs, args)
2600 int nargs;
2601 Lisp_Object *args;
2602 {
2603 return arith_driver (Alogior, nargs, args);
2604 }
2605
2606 DEFUN ("logxor", Flogxor, Slogxor, 0, MANY, 0,
2607 "Return bitwise-exclusive-or of all the arguments.\n\
2608 Arguments may be integers, or markers converted to integers.")
2609 (nargs, args)
2610 int nargs;
2611 Lisp_Object *args;
2612 {
2613 return arith_driver (Alogxor, nargs, args);
2614 }
2615
2616 DEFUN ("ash", Fash, Sash, 2, 2, 0,
2617 "Return VALUE with its bits shifted left by COUNT.\n\
2618 If COUNT is negative, shifting is actually to the right.\n\
2619 In this case, the sign bit is duplicated.")
2620 (value, count)
2621 register Lisp_Object value, count;
2622 {
2623 register Lisp_Object val;
2624
2625 CHECK_NUMBER (value, 0);
2626 CHECK_NUMBER (count, 1);
2627
2628 if (XINT (count) >= BITS_PER_EMACS_INT)
2629 XSETINT (val, 0);
2630 else if (XINT (count) > 0)
2631 XSETINT (val, XINT (value) << XFASTINT (count));
2632 else if (XINT (count) <= -BITS_PER_EMACS_INT)
2633 XSETINT (val, XINT (value) < 0 ? -1 : 0);
2634 else
2635 XSETINT (val, XINT (value) >> -XINT (count));
2636 return val;
2637 }
2638
2639 DEFUN ("lsh", Flsh, Slsh, 2, 2, 0,
2640 "Return VALUE with its bits shifted left by COUNT.\n\
2641 If COUNT is negative, shifting is actually to the right.\n\
2642 In this case, zeros are shifted in on the left.")
2643 (value, count)
2644 register Lisp_Object value, count;
2645 {
2646 register Lisp_Object val;
2647
2648 CHECK_NUMBER (value, 0);
2649 CHECK_NUMBER (count, 1);
2650
2651 if (XINT (count) >= BITS_PER_EMACS_INT)
2652 XSETINT (val, 0);
2653 else if (XINT (count) > 0)
2654 XSETINT (val, (EMACS_UINT) XUINT (value) << XFASTINT (count));
2655 else if (XINT (count) <= -BITS_PER_EMACS_INT)
2656 XSETINT (val, 0);
2657 else
2658 XSETINT (val, (EMACS_UINT) XUINT (value) >> -XINT (count));
2659 return val;
2660 }
2661
2662 DEFUN ("1+", Fadd1, Sadd1, 1, 1, 0,
2663 "Return NUMBER plus one. NUMBER may be a number or a marker.\n\
2664 Markers are converted to integers.")
2665 (number)
2666 register Lisp_Object number;
2667 {
2668 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number, 0);
2669
2670 if (FLOATP (number))
2671 return (make_float (1.0 + XFLOAT_DATA (number)));
2672
2673 XSETINT (number, XINT (number) + 1);
2674 return number;
2675 }
2676
2677 DEFUN ("1-", Fsub1, Ssub1, 1, 1, 0,
2678 "Return NUMBER minus one. NUMBER may be a number or a marker.\n\
2679 Markers are converted to integers.")
2680 (number)
2681 register Lisp_Object number;
2682 {
2683 CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number, 0);
2684
2685 if (FLOATP (number))
2686 return (make_float (-1.0 + XFLOAT_DATA (number)));
2687
2688 XSETINT (number, XINT (number) - 1);
2689 return number;
2690 }
2691
2692 DEFUN ("lognot", Flognot, Slognot, 1, 1, 0,
2693 "Return the bitwise complement of NUMBER. NUMBER must be an integer.")
2694 (number)
2695 register Lisp_Object number;
2696 {
2697 CHECK_NUMBER (number, 0);
2698 XSETINT (number, ~XINT (number));
2699 return number;
2700 }
2701 \f
2702 void
2703 syms_of_data ()
2704 {
2705 Lisp_Object error_tail, arith_tail;
2706
2707 Qquote = intern ("quote");
2708 Qlambda = intern ("lambda");
2709 Qsubr = intern ("subr");
2710 Qerror_conditions = intern ("error-conditions");
2711 Qerror_message = intern ("error-message");
2712 Qtop_level = intern ("top-level");
2713
2714 Qerror = intern ("error");
2715 Qquit = intern ("quit");
2716 Qwrong_type_argument = intern ("wrong-type-argument");
2717 Qargs_out_of_range = intern ("args-out-of-range");
2718 Qvoid_function = intern ("void-function");
2719 Qcyclic_function_indirection = intern ("cyclic-function-indirection");
2720 Qvoid_variable = intern ("void-variable");
2721 Qsetting_constant = intern ("setting-constant");
2722 Qinvalid_read_syntax = intern ("invalid-read-syntax");
2723
2724 Qinvalid_function = intern ("invalid-function");
2725 Qwrong_number_of_arguments = intern ("wrong-number-of-arguments");
2726 Qno_catch = intern ("no-catch");
2727 Qend_of_file = intern ("end-of-file");
2728 Qarith_error = intern ("arith-error");
2729 Qbeginning_of_buffer = intern ("beginning-of-buffer");
2730 Qend_of_buffer = intern ("end-of-buffer");
2731 Qbuffer_read_only = intern ("buffer-read-only");
2732 Qtext_read_only = intern ("text-read-only");
2733 Qmark_inactive = intern ("mark-inactive");
2734
2735 Qlistp = intern ("listp");
2736 Qconsp = intern ("consp");
2737 Qsymbolp = intern ("symbolp");
2738 Qkeywordp = intern ("keywordp");
2739 Qintegerp = intern ("integerp");
2740 Qnatnump = intern ("natnump");
2741 Qwholenump = intern ("wholenump");
2742 Qstringp = intern ("stringp");
2743 Qarrayp = intern ("arrayp");
2744 Qsequencep = intern ("sequencep");
2745 Qbufferp = intern ("bufferp");
2746 Qvectorp = intern ("vectorp");
2747 Qchar_or_string_p = intern ("char-or-string-p");
2748 Qmarkerp = intern ("markerp");
2749 Qbuffer_or_string_p = intern ("buffer-or-string-p");
2750 Qinteger_or_marker_p = intern ("integer-or-marker-p");
2751 Qboundp = intern ("boundp");
2752 Qfboundp = intern ("fboundp");
2753
2754 Qfloatp = intern ("floatp");
2755 Qnumberp = intern ("numberp");
2756 Qnumber_or_marker_p = intern ("number-or-marker-p");
2757
2758 Qchar_table_p = intern ("char-table-p");
2759 Qvector_or_char_table_p = intern ("vector-or-char-table-p");
2760
2761 Qsubrp = intern ("subrp");
2762 Qunevalled = intern ("unevalled");
2763 Qmany = intern ("many");
2764
2765 Qcdr = intern ("cdr");
2766
2767 /* Handle automatic advice activation */
2768 Qad_advice_info = intern ("ad-advice-info");
2769 Qad_activate_internal = intern ("ad-activate-internal");
2770
2771 error_tail = Fcons (Qerror, Qnil);
2772
2773 /* ERROR is used as a signaler for random errors for which nothing else is right */
2774
2775 Fput (Qerror, Qerror_conditions,
2776 error_tail);
2777 Fput (Qerror, Qerror_message,
2778 build_string ("error"));
2779
2780 Fput (Qquit, Qerror_conditions,
2781 Fcons (Qquit, Qnil));
2782 Fput (Qquit, Qerror_message,
2783 build_string ("Quit"));
2784
2785 Fput (Qwrong_type_argument, Qerror_conditions,
2786 Fcons (Qwrong_type_argument, error_tail));
2787 Fput (Qwrong_type_argument, Qerror_message,
2788 build_string ("Wrong type argument"));
2789
2790 Fput (Qargs_out_of_range, Qerror_conditions,
2791 Fcons (Qargs_out_of_range, error_tail));
2792 Fput (Qargs_out_of_range, Qerror_message,
2793 build_string ("Args out of range"));
2794
2795 Fput (Qvoid_function, Qerror_conditions,
2796 Fcons (Qvoid_function, error_tail));
2797 Fput (Qvoid_function, Qerror_message,
2798 build_string ("Symbol's function definition is void"));
2799
2800 Fput (Qcyclic_function_indirection, Qerror_conditions,
2801 Fcons (Qcyclic_function_indirection, error_tail));
2802 Fput (Qcyclic_function_indirection, Qerror_message,
2803 build_string ("Symbol's chain of function indirections contains a loop"));
2804
2805 Fput (Qvoid_variable, Qerror_conditions,
2806 Fcons (Qvoid_variable, error_tail));
2807 Fput (Qvoid_variable, Qerror_message,
2808 build_string ("Symbol's value as variable is void"));
2809
2810 Fput (Qsetting_constant, Qerror_conditions,
2811 Fcons (Qsetting_constant, error_tail));
2812 Fput (Qsetting_constant, Qerror_message,
2813 build_string ("Attempt to set a constant symbol"));
2814
2815 Fput (Qinvalid_read_syntax, Qerror_conditions,
2816 Fcons (Qinvalid_read_syntax, error_tail));
2817 Fput (Qinvalid_read_syntax, Qerror_message,
2818 build_string ("Invalid read syntax"));
2819
2820 Fput (Qinvalid_function, Qerror_conditions,
2821 Fcons (Qinvalid_function, error_tail));
2822 Fput (Qinvalid_function, Qerror_message,
2823 build_string ("Invalid function"));
2824
2825 Fput (Qwrong_number_of_arguments, Qerror_conditions,
2826 Fcons (Qwrong_number_of_arguments, error_tail));
2827 Fput (Qwrong_number_of_arguments, Qerror_message,
2828 build_string ("Wrong number of arguments"));
2829
2830 Fput (Qno_catch, Qerror_conditions,
2831 Fcons (Qno_catch, error_tail));
2832 Fput (Qno_catch, Qerror_message,
2833 build_string ("No catch for tag"));
2834
2835 Fput (Qend_of_file, Qerror_conditions,
2836 Fcons (Qend_of_file, error_tail));
2837 Fput (Qend_of_file, Qerror_message,
2838 build_string ("End of file during parsing"));
2839
2840 arith_tail = Fcons (Qarith_error, error_tail);
2841 Fput (Qarith_error, Qerror_conditions,
2842 arith_tail);
2843 Fput (Qarith_error, Qerror_message,
2844 build_string ("Arithmetic error"));
2845
2846 Fput (Qbeginning_of_buffer, Qerror_conditions,
2847 Fcons (Qbeginning_of_buffer, error_tail));
2848 Fput (Qbeginning_of_buffer, Qerror_message,
2849 build_string ("Beginning of buffer"));
2850
2851 Fput (Qend_of_buffer, Qerror_conditions,
2852 Fcons (Qend_of_buffer, error_tail));
2853 Fput (Qend_of_buffer, Qerror_message,
2854 build_string ("End of buffer"));
2855
2856 Fput (Qbuffer_read_only, Qerror_conditions,
2857 Fcons (Qbuffer_read_only, error_tail));
2858 Fput (Qbuffer_read_only, Qerror_message,
2859 build_string ("Buffer is read-only"));
2860
2861 Fput (Qtext_read_only, Qerror_conditions,
2862 Fcons (Qtext_read_only, error_tail));
2863 Fput (Qtext_read_only, Qerror_message,
2864 build_string ("Text is read-only"));
2865
2866 Qrange_error = intern ("range-error");
2867 Qdomain_error = intern ("domain-error");
2868 Qsingularity_error = intern ("singularity-error");
2869 Qoverflow_error = intern ("overflow-error");
2870 Qunderflow_error = intern ("underflow-error");
2871
2872 Fput (Qdomain_error, Qerror_conditions,
2873 Fcons (Qdomain_error, arith_tail));
2874 Fput (Qdomain_error, Qerror_message,
2875 build_string ("Arithmetic domain error"));
2876
2877 Fput (Qrange_error, Qerror_conditions,
2878 Fcons (Qrange_error, arith_tail));
2879 Fput (Qrange_error, Qerror_message,
2880 build_string ("Arithmetic range error"));
2881
2882 Fput (Qsingularity_error, Qerror_conditions,
2883 Fcons (Qsingularity_error, Fcons (Qdomain_error, arith_tail)));
2884 Fput (Qsingularity_error, Qerror_message,
2885 build_string ("Arithmetic singularity error"));
2886
2887 Fput (Qoverflow_error, Qerror_conditions,
2888 Fcons (Qoverflow_error, Fcons (Qdomain_error, arith_tail)));
2889 Fput (Qoverflow_error, Qerror_message,
2890 build_string ("Arithmetic overflow error"));
2891
2892 Fput (Qunderflow_error, Qerror_conditions,
2893 Fcons (Qunderflow_error, Fcons (Qdomain_error, arith_tail)));
2894 Fput (Qunderflow_error, Qerror_message,
2895 build_string ("Arithmetic underflow error"));
2896
2897 staticpro (&Qrange_error);
2898 staticpro (&Qdomain_error);
2899 staticpro (&Qsingularity_error);
2900 staticpro (&Qoverflow_error);
2901 staticpro (&Qunderflow_error);
2902
2903 staticpro (&Qnil);
2904 staticpro (&Qt);
2905 staticpro (&Qquote);
2906 staticpro (&Qlambda);
2907 staticpro (&Qsubr);
2908 staticpro (&Qunbound);
2909 staticpro (&Qerror_conditions);
2910 staticpro (&Qerror_message);
2911 staticpro (&Qtop_level);
2912
2913 staticpro (&Qerror);
2914 staticpro (&Qquit);
2915 staticpro (&Qwrong_type_argument);
2916 staticpro (&Qargs_out_of_range);
2917 staticpro (&Qvoid_function);
2918 staticpro (&Qcyclic_function_indirection);
2919 staticpro (&Qvoid_variable);
2920 staticpro (&Qsetting_constant);
2921 staticpro (&Qinvalid_read_syntax);
2922 staticpro (&Qwrong_number_of_arguments);
2923 staticpro (&Qinvalid_function);
2924 staticpro (&Qno_catch);
2925 staticpro (&Qend_of_file);
2926 staticpro (&Qarith_error);
2927 staticpro (&Qbeginning_of_buffer);
2928 staticpro (&Qend_of_buffer);
2929 staticpro (&Qbuffer_read_only);
2930 staticpro (&Qtext_read_only);
2931 staticpro (&Qmark_inactive);
2932
2933 staticpro (&Qlistp);
2934 staticpro (&Qconsp);
2935 staticpro (&Qsymbolp);
2936 staticpro (&Qkeywordp);
2937 staticpro (&Qintegerp);
2938 staticpro (&Qnatnump);
2939 staticpro (&Qwholenump);
2940 staticpro (&Qstringp);
2941 staticpro (&Qarrayp);
2942 staticpro (&Qsequencep);
2943 staticpro (&Qbufferp);
2944 staticpro (&Qvectorp);
2945 staticpro (&Qchar_or_string_p);
2946 staticpro (&Qmarkerp);
2947 staticpro (&Qbuffer_or_string_p);
2948 staticpro (&Qinteger_or_marker_p);
2949 staticpro (&Qfloatp);
2950 staticpro (&Qnumberp);
2951 staticpro (&Qnumber_or_marker_p);
2952 staticpro (&Qchar_table_p);
2953 staticpro (&Qvector_or_char_table_p);
2954 staticpro (&Qsubrp);
2955 staticpro (&Qmany);
2956 staticpro (&Qunevalled);
2957
2958 staticpro (&Qboundp);
2959 staticpro (&Qfboundp);
2960 staticpro (&Qcdr);
2961 staticpro (&Qad_advice_info);
2962 staticpro (&Qad_activate_internal);
2963
2964 /* Types that type-of returns. */
2965 Qinteger = intern ("integer");
2966 Qsymbol = intern ("symbol");
2967 Qstring = intern ("string");
2968 Qcons = intern ("cons");
2969 Qmarker = intern ("marker");
2970 Qoverlay = intern ("overlay");
2971 Qfloat = intern ("float");
2972 Qwindow_configuration = intern ("window-configuration");
2973 Qprocess = intern ("process");
2974 Qwindow = intern ("window");
2975 /* Qsubr = intern ("subr"); */
2976 Qcompiled_function = intern ("compiled-function");
2977 Qbuffer = intern ("buffer");
2978 Qframe = intern ("frame");
2979 Qvector = intern ("vector");
2980 Qchar_table = intern ("char-table");
2981 Qbool_vector = intern ("bool-vector");
2982 Qhash_table = intern ("hash-table");
2983
2984 staticpro (&Qinteger);
2985 staticpro (&Qsymbol);
2986 staticpro (&Qstring);
2987 staticpro (&Qcons);
2988 staticpro (&Qmarker);
2989 staticpro (&Qoverlay);
2990 staticpro (&Qfloat);
2991 staticpro (&Qwindow_configuration);
2992 staticpro (&Qprocess);
2993 staticpro (&Qwindow);
2994 /* staticpro (&Qsubr); */
2995 staticpro (&Qcompiled_function);
2996 staticpro (&Qbuffer);
2997 staticpro (&Qframe);
2998 staticpro (&Qvector);
2999 staticpro (&Qchar_table);
3000 staticpro (&Qbool_vector);
3001 staticpro (&Qhash_table);
3002
3003 defsubr (&Seq);
3004 defsubr (&Snull);
3005 defsubr (&Stype_of);
3006 defsubr (&Slistp);
3007 defsubr (&Snlistp);
3008 defsubr (&Sconsp);
3009 defsubr (&Satom);
3010 defsubr (&Sintegerp);
3011 defsubr (&Sinteger_or_marker_p);
3012 defsubr (&Snumberp);
3013 defsubr (&Snumber_or_marker_p);
3014 defsubr (&Sfloatp);
3015 defsubr (&Snatnump);
3016 defsubr (&Ssymbolp);
3017 defsubr (&Skeywordp);
3018 defsubr (&Sstringp);
3019 defsubr (&Smultibyte_string_p);
3020 defsubr (&Svectorp);
3021 defsubr (&Schar_table_p);
3022 defsubr (&Svector_or_char_table_p);
3023 defsubr (&Sbool_vector_p);
3024 defsubr (&Sarrayp);
3025 defsubr (&Ssequencep);
3026 defsubr (&Sbufferp);
3027 defsubr (&Smarkerp);
3028 defsubr (&Ssubrp);
3029 defsubr (&Sbyte_code_function_p);
3030 defsubr (&Schar_or_string_p);
3031 defsubr (&Scar);
3032 defsubr (&Scdr);
3033 defsubr (&Scar_safe);
3034 defsubr (&Scdr_safe);
3035 defsubr (&Ssetcar);
3036 defsubr (&Ssetcdr);
3037 defsubr (&Ssymbol_function);
3038 defsubr (&Sindirect_function);
3039 defsubr (&Ssymbol_plist);
3040 defsubr (&Ssymbol_name);
3041 defsubr (&Smakunbound);
3042 defsubr (&Sfmakunbound);
3043 defsubr (&Sboundp);
3044 defsubr (&Sfboundp);
3045 defsubr (&Sfset);
3046 defsubr (&Sdefalias);
3047 defsubr (&Ssetplist);
3048 defsubr (&Ssymbol_value);
3049 defsubr (&Sset);
3050 defsubr (&Sdefault_boundp);
3051 defsubr (&Sdefault_value);
3052 defsubr (&Sset_default);
3053 defsubr (&Ssetq_default);
3054 defsubr (&Smake_variable_buffer_local);
3055 defsubr (&Smake_local_variable);
3056 defsubr (&Skill_local_variable);
3057 defsubr (&Smake_variable_frame_local);
3058 defsubr (&Slocal_variable_p);
3059 defsubr (&Slocal_variable_if_set_p);
3060 defsubr (&Saref);
3061 defsubr (&Saset);
3062 defsubr (&Snumber_to_string);
3063 defsubr (&Sstring_to_number);
3064 defsubr (&Seqlsign);
3065 defsubr (&Slss);
3066 defsubr (&Sgtr);
3067 defsubr (&Sleq);
3068 defsubr (&Sgeq);
3069 defsubr (&Sneq);
3070 defsubr (&Szerop);
3071 defsubr (&Splus);
3072 defsubr (&Sminus);
3073 defsubr (&Stimes);
3074 defsubr (&Squo);
3075 defsubr (&Srem);
3076 defsubr (&Smod);
3077 defsubr (&Smax);
3078 defsubr (&Smin);
3079 defsubr (&Slogand);
3080 defsubr (&Slogior);
3081 defsubr (&Slogxor);
3082 defsubr (&Slsh);
3083 defsubr (&Sash);
3084 defsubr (&Sadd1);
3085 defsubr (&Ssub1);
3086 defsubr (&Slognot);
3087 defsubr (&Ssubr_arity);
3088
3089 XSYMBOL (Qwholenump)->function = XSYMBOL (Qnatnump)->function;
3090 }
3091
3092 SIGTYPE
3093 arith_error (signo)
3094 int signo;
3095 {
3096 #if defined(USG) && !defined(POSIX_SIGNALS)
3097 /* USG systems forget handlers when they are used;
3098 must reestablish each time */
3099 signal (signo, arith_error);
3100 #endif /* USG */
3101 #ifdef VMS
3102 /* VMS systems are like USG. */
3103 signal (signo, arith_error);
3104 #endif /* VMS */
3105 #ifdef BSD4_1
3106 sigrelse (SIGFPE);
3107 #else /* not BSD4_1 */
3108 sigsetmask (SIGEMPTYMASK);
3109 #endif /* not BSD4_1 */
3110
3111 Fsignal (Qarith_error, Qnil);
3112 }
3113
3114 void
3115 init_data ()
3116 {
3117 /* Don't do this if just dumping out.
3118 We don't want to call `signal' in this case
3119 so that we don't have trouble with dumping
3120 signal-delivering routines in an inconsistent state. */
3121 #ifndef CANNOT_DUMP
3122 if (!initialized)
3123 return;
3124 #endif /* CANNOT_DUMP */
3125 signal (SIGFPE, arith_error);
3126
3127 #ifdef uts
3128 signal (SIGEMT, arith_error);
3129 #endif /* uts */
3130 }