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