Commit | Line | Data |
---|---|---|
db9f0278 | 1 | /* Evaluator for GNU Emacs Lisp interpreter. |
fa022909 | 2 | |
ba318903 PE |
3 | Copyright (C) 1985-1987, 1993-1995, 1999-2014 Free Software Foundation, |
4 | Inc. | |
db9f0278 JB |
5 | |
6 | This file is part of GNU Emacs. | |
7 | ||
9ec0b715 | 8 | GNU Emacs is free software: you can redistribute it and/or modify |
db9f0278 | 9 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
10 | the Free Software Foundation, either version 3 of the License, or |
11 | (at your option) any later version. | |
db9f0278 JB |
12 | |
13 | GNU Emacs is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
9ec0b715 | 19 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
db9f0278 JB |
20 | |
21 | ||
18160b98 | 22 | #include <config.h> |
eb3f1cc8 | 23 | #include <limits.h> |
4e2fe2e6 | 24 | #include <stdio.h> |
db9f0278 | 25 | #include "lisp.h" |
9ac0d9e0 | 26 | #include "blockinput.h" |
db9f0278 | 27 | #include "commands.h" |
1f98fa48 | 28 | #include "keyboard.h" |
3648c842 | 29 | #include "dispextern.h" |
b70e1a2b | 30 | |
adf2aa61 | 31 | /* Chain of condition and catch handlers currently in effect. */ |
244ed907 | 32 | |
244ed907 PE |
33 | struct handler *handlerlist; |
34 | ||
15934ffa RS |
35 | #ifdef DEBUG_GCPRO |
36 | /* Count levels of GCPRO to detect failure to UNGCPRO. */ | |
37 | int gcpro_level; | |
38 | #endif | |
39 | ||
61b108cc | 40 | Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp; |
29208e82 | 41 | Lisp_Object Qinhibit_quit; |
955cbe7b PE |
42 | Lisp_Object Qand_rest; |
43 | static Lisp_Object Qand_optional; | |
45b82ad0 | 44 | static Lisp_Object Qinhibit_debugger; |
955cbe7b | 45 | static Lisp_Object Qdeclare; |
b9598260 SM |
46 | Lisp_Object Qinternal_interpreter_environment, Qclosure; |
47 | ||
ed008a6d | 48 | static Lisp_Object Qdebug; |
db9f0278 | 49 | |
6e6e9f08 RS |
50 | /* This holds either the symbol `run-hooks' or nil. |
51 | It is nil at an early stage of startup, and when Emacs | |
52 | is shutting down. */ | |
4c576a83 | 53 | |
db9f0278 JB |
54 | Lisp_Object Vrun_hooks; |
55 | ||
56 | /* Non-nil means record all fset's and provide's, to be undone | |
57 | if the file being autoloaded is not fully loaded. | |
58 | They are recorded by being consed onto the front of Vautoload_queue: | |
47b82df9 | 59 | (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */ |
db9f0278 JB |
60 | |
61 | Lisp_Object Vautoload_queue; | |
62 | ||
9349e5f7 PE |
63 | /* Current number of specbindings allocated in specpdl, not counting |
64 | the dummy entry specpdl[-1]. */ | |
4c576a83 | 65 | |
d311d28c | 66 | ptrdiff_t specpdl_size; |
db9f0278 | 67 | |
9349e5f7 PE |
68 | /* Pointer to beginning of specpdl. A dummy entry specpdl[-1] exists |
69 | only so that its address can be taken. */ | |
4c576a83 | 70 | |
9349e5f7 | 71 | union specbinding *specpdl; |
db9f0278 JB |
72 | |
73 | /* Pointer to first unused element in specpdl. */ | |
4c576a83 | 74 | |
9349e5f7 | 75 | union specbinding *specpdl_ptr; |
db9f0278 | 76 | |
db9f0278 | 77 | /* Depth in Lisp evaluations and function calls. */ |
4c576a83 | 78 | |
adf2aa61 | 79 | EMACS_INT lisp_eval_depth; |
db9f0278 | 80 | |
be857679 | 81 | /* The value of num_nonmacro_input_events as of the last time we |
82da7701 | 82 | started to enter the debugger. If we decide to enter the debugger |
be857679 | 83 | again when this is still equal to num_nonmacro_input_events, then we |
82da7701 JB |
84 | know that the debugger itself has an error, and we should just |
85 | signal the error instead of entering an infinite loop of debugger | |
86 | invocations. */ | |
4c576a83 | 87 | |
d311d28c | 88 | static EMACS_INT when_entered_debugger; |
db9f0278 | 89 | |
a2ff3819 GM |
90 | /* The function from which the last `signal' was called. Set in |
91 | Fsignal. */ | |
2f592f95 | 92 | /* FIXME: We should probably get rid of this! */ |
a2ff3819 GM |
93 | Lisp_Object Vsignaling_function; |
94 | ||
d1f55f16 CY |
95 | /* If non-nil, Lisp code must not be run since some part of Emacs is |
96 | in an inconsistent state. Currently, x-create-frame uses this to | |
97 | avoid triggering window-configuration-change-hook while the new | |
98 | frame is half-initialized. */ | |
99 | Lisp_Object inhibit_lisp_code; | |
100 | ||
9c203066 PE |
101 | /* These would ordinarily be static, but they need to be visible to GDB. */ |
102 | bool backtrace_p (union specbinding *) EXTERNALLY_VISIBLE; | |
103 | Lisp_Object *backtrace_args (union specbinding *) EXTERNALLY_VISIBLE; | |
104 | Lisp_Object backtrace_function (union specbinding *) EXTERNALLY_VISIBLE; | |
105 | union specbinding *backtrace_next (union specbinding *) EXTERNALLY_VISIBLE; | |
106 | union specbinding *backtrace_top (void) EXTERNALLY_VISIBLE; | |
107 | ||
f66c7cf8 | 108 | static Lisp_Object funcall_lambda (Lisp_Object, ptrdiff_t, Lisp_Object *); |
7200d79c | 109 | static Lisp_Object apply_lambda (Lisp_Object fun, Lisp_Object args); |
e46f2325 | 110 | |
84575e67 | 111 | static Lisp_Object |
9349e5f7 | 112 | specpdl_symbol (union specbinding *pdl) |
84575e67 PE |
113 | { |
114 | eassert (pdl->kind >= SPECPDL_LET); | |
9349e5f7 | 115 | return pdl->let.symbol; |
84575e67 PE |
116 | } |
117 | ||
118 | static Lisp_Object | |
9349e5f7 | 119 | specpdl_old_value (union specbinding *pdl) |
84575e67 PE |
120 | { |
121 | eassert (pdl->kind >= SPECPDL_LET); | |
9349e5f7 | 122 | return pdl->let.old_value; |
84575e67 PE |
123 | } |
124 | ||
56ea7291 SM |
125 | static void |
126 | set_specpdl_old_value (union specbinding *pdl, Lisp_Object val) | |
127 | { | |
128 | eassert (pdl->kind >= SPECPDL_LET); | |
129 | pdl->let.old_value = val; | |
130 | } | |
131 | ||
84575e67 | 132 | static Lisp_Object |
9349e5f7 | 133 | specpdl_where (union specbinding *pdl) |
84575e67 PE |
134 | { |
135 | eassert (pdl->kind > SPECPDL_LET); | |
9349e5f7 | 136 | return pdl->let.where; |
84575e67 PE |
137 | } |
138 | ||
139 | static Lisp_Object | |
9349e5f7 | 140 | specpdl_arg (union specbinding *pdl) |
84575e67 PE |
141 | { |
142 | eassert (pdl->kind == SPECPDL_UNWIND); | |
9349e5f7 | 143 | return pdl->unwind.arg; |
84575e67 PE |
144 | } |
145 | ||
9c203066 | 146 | Lisp_Object |
9349e5f7 | 147 | backtrace_function (union specbinding *pdl) |
84575e67 PE |
148 | { |
149 | eassert (pdl->kind == SPECPDL_BACKTRACE); | |
9349e5f7 | 150 | return pdl->bt.function; |
84575e67 PE |
151 | } |
152 | ||
153 | static ptrdiff_t | |
9349e5f7 | 154 | backtrace_nargs (union specbinding *pdl) |
84575e67 PE |
155 | { |
156 | eassert (pdl->kind == SPECPDL_BACKTRACE); | |
9349e5f7 | 157 | return pdl->bt.nargs; |
84575e67 PE |
158 | } |
159 | ||
9c203066 | 160 | Lisp_Object * |
9349e5f7 | 161 | backtrace_args (union specbinding *pdl) |
84575e67 PE |
162 | { |
163 | eassert (pdl->kind == SPECPDL_BACKTRACE); | |
9349e5f7 | 164 | return pdl->bt.args; |
84575e67 PE |
165 | } |
166 | ||
167 | static bool | |
9349e5f7 | 168 | backtrace_debug_on_exit (union specbinding *pdl) |
84575e67 PE |
169 | { |
170 | eassert (pdl->kind == SPECPDL_BACKTRACE); | |
9349e5f7 | 171 | return pdl->bt.debug_on_exit; |
84575e67 PE |
172 | } |
173 | ||
2f592f95 | 174 | /* Functions to modify slots of backtrace records. */ |
e46f2325 | 175 | |
3d5ee10a | 176 | static void |
9349e5f7 PE |
177 | set_backtrace_args (union specbinding *pdl, Lisp_Object *args) |
178 | { | |
179 | eassert (pdl->kind == SPECPDL_BACKTRACE); | |
180 | pdl->bt.args = args; | |
181 | } | |
2f592f95 | 182 | |
3d5ee10a | 183 | static void |
9349e5f7 PE |
184 | set_backtrace_nargs (union specbinding *pdl, ptrdiff_t n) |
185 | { | |
186 | eassert (pdl->kind == SPECPDL_BACKTRACE); | |
187 | pdl->bt.nargs = n; | |
188 | } | |
2f592f95 | 189 | |
3d5ee10a | 190 | static void |
9349e5f7 PE |
191 | set_backtrace_debug_on_exit (union specbinding *pdl, bool doe) |
192 | { | |
193 | eassert (pdl->kind == SPECPDL_BACKTRACE); | |
194 | pdl->bt.debug_on_exit = doe; | |
195 | } | |
2f592f95 SM |
196 | |
197 | /* Helper functions to scan the backtrace. */ | |
198 | ||
9349e5f7 PE |
199 | bool |
200 | backtrace_p (union specbinding *pdl) | |
2f592f95 | 201 | { return pdl >= specpdl; } |
a8a7c5f6 | 202 | |
9349e5f7 | 203 | union specbinding * |
3d5ee10a | 204 | backtrace_top (void) |
e46f2325 | 205 | { |
9349e5f7 | 206 | union specbinding *pdl = specpdl_ptr - 1; |
a8a7c5f6 | 207 | while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE) |
2f592f95 SM |
208 | pdl--; |
209 | return pdl; | |
e46f2325 | 210 | } |
a8a7c5f6 | 211 | |
9349e5f7 PE |
212 | union specbinding * |
213 | backtrace_next (union specbinding *pdl) | |
e46f2325 | 214 | { |
2f592f95 SM |
215 | pdl--; |
216 | while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE) | |
217 | pdl--; | |
218 | return pdl; | |
e46f2325 DA |
219 | } |
220 | ||
2f592f95 | 221 | |
dfcf069d | 222 | void |
d3da34e0 | 223 | init_eval_once (void) |
db9f0278 | 224 | { |
98e8eae1 | 225 | enum { size = 50 }; |
9349e5f7 | 226 | union specbinding *pdlvec = xmalloc ((size + 1) * sizeof *specpdl); |
98e8eae1 | 227 | specpdl_size = size; |
9349e5f7 | 228 | specpdl = specpdl_ptr = pdlvec + 1; |
6588243d | 229 | /* Don't forget to update docs (lispref node "Local Variables"). */ |
c530e1c2 | 230 | max_specpdl_size = 1300; /* 1000 is not enough for CEDET's c-by.el. */ |
d46f6bbb | 231 | max_lisp_eval_depth = 600; |
34d470ba RS |
232 | |
233 | Vrun_hooks = Qnil; | |
db9f0278 JB |
234 | } |
235 | ||
70de5e86 SM |
236 | static struct handler handlerlist_sentinel; |
237 | ||
dfcf069d | 238 | void |
d3da34e0 | 239 | init_eval (void) |
db9f0278 JB |
240 | { |
241 | specpdl_ptr = specpdl; | |
70de5e86 SM |
242 | { /* Put a dummy catcher at top-level so that handlerlist is never NULL. |
243 | This is important since handlerlist->nextfree holds the freelist | |
244 | which would otherwise leak every time we unwind back to top-level. */ | |
245 | struct handler *c; | |
246 | handlerlist = handlerlist_sentinel.nextfree = &handlerlist_sentinel; | |
247 | PUSH_HANDLER (c, Qunbound, CATCHER); | |
248 | eassert (c == &handlerlist_sentinel); | |
249 | handlerlist_sentinel.nextfree = NULL; | |
250 | handlerlist_sentinel.next = NULL; | |
251 | } | |
db9f0278 JB |
252 | Vquit_flag = Qnil; |
253 | debug_on_next_call = 0; | |
254 | lisp_eval_depth = 0; | |
87e21fbd | 255 | #ifdef DEBUG_GCPRO |
15934ffa | 256 | gcpro_level = 0; |
87e21fbd | 257 | #endif |
be857679 | 258 | /* This is less than the initial value of num_nonmacro_input_events. */ |
b5b911f9 | 259 | when_entered_debugger = -1; |
db9f0278 JB |
260 | } |
261 | ||
f6d62986 | 262 | /* Unwind-protect function used by call_debugger. */ |
9f5903bb | 263 | |
27e498e6 | 264 | static void |
d3da34e0 | 265 | restore_stack_limits (Lisp_Object data) |
9f5903bb RS |
266 | { |
267 | max_specpdl_size = XINT (XCAR (data)); | |
268 | max_lisp_eval_depth = XINT (XCDR (data)); | |
269 | } | |
270 | ||
9cad4576 DA |
271 | static void grow_specpdl (void); |
272 | ||
9f5903bb RS |
273 | /* Call the Lisp debugger, giving it argument ARG. */ |
274 | ||
7f7e0167 | 275 | Lisp_Object |
d3da34e0 | 276 | call_debugger (Lisp_Object arg) |
db9f0278 | 277 | { |
1882aa38 | 278 | bool debug_while_redisplaying; |
d311d28c | 279 | ptrdiff_t count = SPECPDL_INDEX (); |
3648c842 | 280 | Lisp_Object val; |
575593db DA |
281 | EMACS_INT old_depth = max_lisp_eval_depth; |
282 | /* Do not allow max_specpdl_size less than actual depth (Bug#16603). */ | |
283 | EMACS_INT old_max = max (max_specpdl_size, count); | |
9f5903bb RS |
284 | |
285 | if (lisp_eval_depth + 40 > max_lisp_eval_depth) | |
286 | max_lisp_eval_depth = lisp_eval_depth + 40; | |
287 | ||
9cad4576 DA |
288 | /* While debugging Bug#16603, previous value of 100 was found |
289 | too small to avoid specpdl overflow in the debugger itself. */ | |
290 | if (max_specpdl_size - 200 < count) | |
291 | max_specpdl_size = count + 200; | |
292 | ||
293 | if (old_max == count) | |
294 | { | |
295 | /* We can enter the debugger due to specpdl overflow (Bug#16603). */ | |
296 | specpdl_ptr--; | |
297 | grow_specpdl (); | |
298 | } | |
299 | ||
300 | /* Restore limits after leaving the debugger. */ | |
301 | record_unwind_protect (restore_stack_limits, | |
302 | Fcons (make_number (old_max), | |
303 | make_number (old_depth))); | |
177c0ea7 | 304 | |
d148e14d | 305 | #ifdef HAVE_WINDOW_SYSTEM |
df6c90d8 GM |
306 | if (display_hourglass_p) |
307 | cancel_hourglass (); | |
237c23b0 GM |
308 | #endif |
309 | ||
db9f0278 | 310 | debug_on_next_call = 0; |
be857679 | 311 | when_entered_debugger = num_nonmacro_input_events; |
3648c842 GM |
312 | |
313 | /* Resetting redisplaying_p to 0 makes sure that debug output is | |
314 | displayed if the debugger is invoked during redisplay. */ | |
315 | debug_while_redisplaying = redisplaying_p; | |
316 | redisplaying_p = 0; | |
556d7314 GM |
317 | specbind (intern ("debugger-may-continue"), |
318 | debug_while_redisplaying ? Qnil : Qt); | |
8efb6cc7 | 319 | specbind (Qinhibit_redisplay, Qnil); |
45b82ad0 | 320 | specbind (Qinhibit_debugger, Qt); |
9db6f6b4 GM |
321 | |
322 | #if 0 /* Binding this prevents execution of Lisp code during | |
323 | redisplay, which necessarily leads to display problems. */ | |
8efb6cc7 | 324 | specbind (Qinhibit_eval_during_redisplay, Qt); |
9db6f6b4 | 325 | #endif |
177c0ea7 | 326 | |
3648c842 GM |
327 | val = apply1 (Vdebugger, arg); |
328 | ||
329 | /* Interrupting redisplay and resuming it later is not safe under | |
330 | all circumstances. So, when the debugger returns, abort the | |
1b1acc13 | 331 | interrupted redisplay by going back to the top-level. */ |
3648c842 GM |
332 | if (debug_while_redisplaying) |
333 | Ftop_level (); | |
334 | ||
556d7314 | 335 | return unbind_to (count, val); |
db9f0278 JB |
336 | } |
337 | ||
475545b5 | 338 | static void |
d3da34e0 | 339 | do_debug_on_call (Lisp_Object code) |
db9f0278 JB |
340 | { |
341 | debug_on_next_call = 0; | |
2f592f95 | 342 | set_backtrace_debug_on_exit (specpdl_ptr - 1, true); |
6c6f1994 | 343 | call_debugger (list1 (code)); |
db9f0278 JB |
344 | } |
345 | \f | |
346 | /* NOTE!!! Every function that can call EVAL must protect its args | |
347 | and temporaries from garbage collection while it needs them. | |
348 | The definition of `For' shows what you have to do. */ | |
349 | ||
350 | DEFUN ("or", For, Sor, 0, UNEVALLED, 0, | |
9dbc9081 PJ |
351 | doc: /* Eval args until one of them yields non-nil, then return that value. |
352 | The remaining args are not evalled at all. | |
353 | If all args return nil, return nil. | |
533eb34b | 354 | usage: (or CONDITIONS...) */) |
5842a27b | 355 | (Lisp_Object args) |
db9f0278 | 356 | { |
e509f168 | 357 | register Lisp_Object val = Qnil; |
db9f0278 JB |
358 | struct gcpro gcpro1; |
359 | ||
e509f168 | 360 | GCPRO1 (args); |
db9f0278 | 361 | |
e509f168 | 362 | while (CONSP (args)) |
db9f0278 | 363 | { |
defb1411 | 364 | val = eval_sub (XCAR (args)); |
265a9e55 | 365 | if (!NILP (val)) |
db9f0278 | 366 | break; |
e509f168 | 367 | args = XCDR (args); |
db9f0278 | 368 | } |
db9f0278 JB |
369 | |
370 | UNGCPRO; | |
371 | return val; | |
372 | } | |
373 | ||
374 | DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0, | |
b8de5714 | 375 | doc: /* Eval args until one of them yields nil, then return nil. |
9dbc9081 PJ |
376 | The remaining args are not evalled at all. |
377 | If no arg yields nil, return the last arg's value. | |
533eb34b | 378 | usage: (and CONDITIONS...) */) |
5842a27b | 379 | (Lisp_Object args) |
db9f0278 | 380 | { |
e509f168 | 381 | register Lisp_Object val = Qt; |
db9f0278 JB |
382 | struct gcpro gcpro1; |
383 | ||
e509f168 | 384 | GCPRO1 (args); |
db9f0278 | 385 | |
e509f168 | 386 | while (CONSP (args)) |
db9f0278 | 387 | { |
defb1411 | 388 | val = eval_sub (XCAR (args)); |
265a9e55 | 389 | if (NILP (val)) |
db9f0278 | 390 | break; |
e509f168 | 391 | args = XCDR (args); |
db9f0278 | 392 | } |
db9f0278 JB |
393 | |
394 | UNGCPRO; | |
395 | return val; | |
396 | } | |
397 | ||
398 | DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0, | |
b8de5714 | 399 | doc: /* If COND yields non-nil, do THEN, else do ELSE... |
9dbc9081 PJ |
400 | Returns the value of THEN or the value of the last of the ELSE's. |
401 | THEN must be one expression, but ELSE... can be zero or more expressions. | |
402 | If COND yields nil, and there are no ELSE's, the value is nil. | |
7a25dc6d | 403 | usage: (if COND THEN ELSE...) */) |
5842a27b | 404 | (Lisp_Object args) |
db9f0278 | 405 | { |
16b0520a | 406 | Lisp_Object cond; |
db9f0278 JB |
407 | struct gcpro gcpro1; |
408 | ||
409 | GCPRO1 (args); | |
16b0520a | 410 | cond = eval_sub (XCAR (args)); |
db9f0278 JB |
411 | UNGCPRO; |
412 | ||
265a9e55 | 413 | if (!NILP (cond)) |
16b0520a PE |
414 | return eval_sub (Fcar (XCDR (args))); |
415 | return Fprogn (XCDR (XCDR (args))); | |
db9f0278 JB |
416 | } |
417 | ||
418 | DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0, | |
9dbc9081 PJ |
419 | doc: /* Try each clause until one succeeds. |
420 | Each clause looks like (CONDITION BODY...). CONDITION is evaluated | |
421 | and, if the value is non-nil, this clause succeeds: | |
422 | then the expressions in BODY are evaluated and the last one's | |
423 | value is the value of the cond-form. | |
fa022909 GM |
424 | If a clause has one element, as in (CONDITION), then the cond-form |
425 | returns CONDITION's value, if that is non-nil. | |
9dbc9081 | 426 | If no clause succeeds, cond returns nil. |
7a25dc6d | 427 | usage: (cond CLAUSES...) */) |
5842a27b | 428 | (Lisp_Object args) |
db9f0278 | 429 | { |
16b0520a | 430 | Lisp_Object val = args; |
db9f0278 JB |
431 | struct gcpro gcpro1; |
432 | ||
db9f0278 | 433 | GCPRO1 (args); |
16b0520a | 434 | while (CONSP (args)) |
db9f0278 | 435 | { |
16b0520a | 436 | Lisp_Object clause = XCAR (args); |
defb1411 | 437 | val = eval_sub (Fcar (clause)); |
265a9e55 | 438 | if (!NILP (val)) |
db9f0278 | 439 | { |
16b0520a | 440 | if (!NILP (XCDR (clause))) |
03699b14 | 441 | val = Fprogn (XCDR (clause)); |
db9f0278 JB |
442 | break; |
443 | } | |
03699b14 | 444 | args = XCDR (args); |
db9f0278 JB |
445 | } |
446 | UNGCPRO; | |
447 | ||
448 | return val; | |
449 | } | |
450 | ||
a7ca3326 | 451 | DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0, |
9dbc9081 | 452 | doc: /* Eval BODY forms sequentially and return value of last one. |
5b4a1f50 | 453 | usage: (progn BODY...) */) |
27e498e6 | 454 | (Lisp_Object body) |
db9f0278 | 455 | { |
27e498e6 | 456 | Lisp_Object val = Qnil; |
db9f0278 JB |
457 | struct gcpro gcpro1; |
458 | ||
27e498e6 | 459 | GCPRO1 (body); |
db9f0278 | 460 | |
27e498e6 | 461 | while (CONSP (body)) |
db9f0278 | 462 | { |
27e498e6 PE |
463 | val = eval_sub (XCAR (body)); |
464 | body = XCDR (body); | |
db9f0278 | 465 | } |
db9f0278 JB |
466 | |
467 | UNGCPRO; | |
468 | return val; | |
469 | } | |
470 | ||
02c66599 | 471 | /* Evaluate BODY sequentially, discarding its value. Suitable for |
27e498e6 PE |
472 | record_unwind_protect. */ |
473 | ||
474 | void | |
475 | unwind_body (Lisp_Object body) | |
476 | { | |
477 | Fprogn (body); | |
478 | } | |
479 | ||
db9f0278 | 480 | DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0, |
bdee2ef3 | 481 | doc: /* Eval FIRST and BODY sequentially; return value from FIRST. |
9dbc9081 PJ |
482 | The value of FIRST is saved during the evaluation of the remaining args, |
483 | whose values are discarded. | |
7a25dc6d | 484 | usage: (prog1 FIRST BODY...) */) |
5842a27b | 485 | (Lisp_Object args) |
db9f0278 JB |
486 | { |
487 | Lisp_Object val; | |
16b0520a | 488 | Lisp_Object args_left; |
db9f0278 | 489 | struct gcpro gcpro1, gcpro2; |
db9f0278 JB |
490 | |
491 | args_left = args; | |
16b0520a | 492 | val = args; |
db9f0278 JB |
493 | GCPRO2 (args, val); |
494 | ||
856bbc81 PE |
495 | val = eval_sub (XCAR (args_left)); |
496 | while (CONSP (args_left = XCDR (args_left))) | |
497 | eval_sub (XCAR (args_left)); | |
db9f0278 JB |
498 | |
499 | UNGCPRO; | |
500 | return val; | |
501 | } | |
502 | ||
503 | DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0, | |
bdee2ef3 | 504 | doc: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2. |
82fc29a1 JB |
505 | The value of FORM2 is saved during the evaluation of the |
506 | remaining args, whose values are discarded. | |
507 | usage: (prog2 FORM1 FORM2 BODY...) */) | |
5842a27b | 508 | (Lisp_Object args) |
db9f0278 | 509 | { |
856bbc81 | 510 | struct gcpro gcpro1; |
db9f0278 | 511 | |
856bbc81 | 512 | GCPRO1 (args); |
856bbc81 | 513 | eval_sub (XCAR (args)); |
a63df926 PE |
514 | UNGCPRO; |
515 | return Fprog1 (XCDR (args)); | |
db9f0278 JB |
516 | } |
517 | ||
518 | DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0, | |
9dbc9081 PJ |
519 | doc: /* Set each SYM to the value of its VAL. |
520 | The symbols SYM are variables; they are literal (not evaluated). | |
521 | The values VAL are expressions; they are evaluated. | |
522 | Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'. | |
523 | The second VAL is not computed until after the first SYM is set, and so on; | |
524 | each VAL can use the new value of variables set earlier in the `setq'. | |
525 | The return value of the `setq' form is the value of the last VAL. | |
819586b2 | 526 | usage: (setq [SYM VAL]...) */) |
5842a27b | 527 | (Lisp_Object args) |
db9f0278 | 528 | { |
16b0520a | 529 | Lisp_Object val, sym, lex_binding; |
db9f0278 | 530 | |
16b0520a PE |
531 | val = args; |
532 | if (CONSP (args)) | |
533 | { | |
534 | Lisp_Object args_left = args; | |
535 | struct gcpro gcpro1; | |
536 | GCPRO1 (args); | |
db9f0278 | 537 | |
16b0520a PE |
538 | do |
539 | { | |
540 | val = eval_sub (Fcar (XCDR (args_left))); | |
541 | sym = XCAR (args_left); | |
542 | ||
543 | /* Like for eval_sub, we do not check declared_special here since | |
544 | it's been done when let-binding. */ | |
545 | if (!NILP (Vinternal_interpreter_environment) /* Mere optimization! */ | |
546 | && SYMBOLP (sym) | |
547 | && !NILP (lex_binding | |
548 | = Fassq (sym, Vinternal_interpreter_environment))) | |
549 | XSETCDR (lex_binding, val); /* SYM is lexically bound. */ | |
550 | else | |
551 | Fset (sym, val); /* SYM is dynamically bound. */ | |
db9f0278 | 552 | |
16b0520a PE |
553 | args_left = Fcdr (XCDR (args_left)); |
554 | } | |
555 | while (CONSP (args_left)); | |
b9598260 | 556 | |
16b0520a | 557 | UNGCPRO; |
db9f0278 | 558 | } |
db9f0278 | 559 | |
db9f0278 JB |
560 | return val; |
561 | } | |
177c0ea7 | 562 | |
db9f0278 | 563 | DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0, |
9dbc9081 | 564 | doc: /* Return the argument, without evaluating it. `(quote x)' yields `x'. |
91a15bc6 SM |
565 | Warning: `quote' does not construct its return value, but just returns |
566 | the value that was pre-constructed by the Lisp reader (see info node | |
567 | `(elisp)Printed Representation'). | |
568 | This means that '(a . b) is not identical to (cons 'a 'b): the former | |
569 | does not cons. Quoting should be reserved for constants that will | |
570 | never be modified by side-effects, unless you like self-modifying code. | |
571 | See the common pitfall in info node `(elisp)Rearrangement' for an example | |
572 | of unexpected results when a quoted object is modified. | |
9dbc9081 | 573 | usage: (quote ARG) */) |
5842a27b | 574 | (Lisp_Object args) |
db9f0278 | 575 | { |
16b0520a | 576 | if (CONSP (XCDR (args))) |
1283140e | 577 | xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args)); |
16b0520a | 578 | return XCAR (args); |
db9f0278 | 579 | } |
177c0ea7 | 580 | |
db9f0278 | 581 | DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0, |
9dbc9081 PJ |
582 | doc: /* Like `quote', but preferred for objects which are functions. |
583 | In byte compilation, `function' causes its argument to be compiled. | |
584 | `quote' cannot do that. | |
585 | usage: (function ARG) */) | |
5842a27b | 586 | (Lisp_Object args) |
db9f0278 | 587 | { |
b9598260 SM |
588 | Lisp_Object quoted = XCAR (args); |
589 | ||
16b0520a | 590 | if (CONSP (XCDR (args))) |
1283140e | 591 | xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args)); |
b9598260 SM |
592 | |
593 | if (!NILP (Vinternal_interpreter_environment) | |
594 | && CONSP (quoted) | |
595 | && EQ (XCAR (quoted), Qlambda)) | |
596 | /* This is a lambda expression within a lexical environment; | |
597 | return an interpreted closure instead of a simple lambda. */ | |
23aba0ea SM |
598 | return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment, |
599 | XCDR (quoted))); | |
b9598260 SM |
600 | else |
601 | /* Simply quote the argument. */ | |
602 | return quoted; | |
db9f0278 JB |
603 | } |
604 | ||
e0f331ab | 605 | |
1848d15d | 606 | DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0, |
4a9308b8 | 607 | doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE. |
e102f0d8 | 608 | Aliased variables always have the same value; setting one sets the other. |
4a9308b8 | 609 | Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is |
dd60787c GM |
610 | omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE, |
611 | or of the variable at the end of the chain of aliases, if BASE-VARIABLE is | |
612 | itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not, | |
613 | then the value of BASE-VARIABLE is set to that of NEW-ALIAS. | |
4a9308b8 | 614 | The return value is BASE-VARIABLE. */) |
5842a27b | 615 | (Lisp_Object new_alias, Lisp_Object base_variable, Lisp_Object docstring) |
19cebf5a GM |
616 | { |
617 | struct Lisp_Symbol *sym; | |
1848d15d | 618 | |
4a9308b8 JB |
619 | CHECK_SYMBOL (new_alias); |
620 | CHECK_SYMBOL (base_variable); | |
19cebf5a | 621 | |
4a9308b8 | 622 | sym = XSYMBOL (new_alias); |
ce5b453a SM |
623 | |
624 | if (sym->constant) | |
178f2507 SM |
625 | /* Not sure why, but why not? */ |
626 | error ("Cannot make a constant an alias"); | |
ce5b453a SM |
627 | |
628 | switch (sym->redirect) | |
629 | { | |
630 | case SYMBOL_FORWARDED: | |
631 | error ("Cannot make an internal variable an alias"); | |
632 | case SYMBOL_LOCALIZED: | |
633 | error ("Don't know how to make a localized variable an alias"); | |
634 | } | |
635 | ||
dd60787c | 636 | /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html |
ce5b453a SM |
637 | If n_a is bound, but b_v is not, set the value of b_v to n_a, |
638 | so that old-code that affects n_a before the aliasing is setup | |
639 | still works. */ | |
640 | if (NILP (Fboundp (base_variable))) | |
94b612ad | 641 | set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1); |
ce5b453a SM |
642 | |
643 | { | |
9349e5f7 | 644 | union specbinding *p; |
ce5b453a | 645 | |
bc985141 | 646 | for (p = specpdl_ptr; p > specpdl; ) |
2f592f95 SM |
647 | if ((--p)->kind >= SPECPDL_LET |
648 | && (EQ (new_alias, specpdl_symbol (p)))) | |
ce5b453a SM |
649 | error ("Don't know how to make a let-bound variable an alias"); |
650 | } | |
651 | ||
b9598260 | 652 | sym->declared_special = 1; |
0ac30604 | 653 | XSYMBOL (base_variable)->declared_special = 1; |
ce5b453a SM |
654 | sym->redirect = SYMBOL_VARALIAS; |
655 | SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable)); | |
4a9308b8 JB |
656 | sym->constant = SYMBOL_CONSTANT_P (base_variable); |
657 | LOADHIST_ATTACH (new_alias); | |
ce5b453a SM |
658 | /* Even if docstring is nil: remove old docstring. */ |
659 | Fput (new_alias, Qvariable_documentation, docstring); | |
1848d15d | 660 | |
4a9308b8 | 661 | return base_variable; |
19cebf5a GM |
662 | } |
663 | ||
a104f656 SM |
664 | static union specbinding * |
665 | default_toplevel_binding (Lisp_Object symbol) | |
666 | { | |
667 | union specbinding *binding = NULL; | |
668 | union specbinding *pdl = specpdl_ptr; | |
669 | while (pdl > specpdl) | |
670 | { | |
671 | switch ((--pdl)->kind) | |
672 | { | |
673 | case SPECPDL_LET_DEFAULT: | |
674 | case SPECPDL_LET: | |
675 | if (EQ (specpdl_symbol (pdl), symbol)) | |
676 | binding = pdl; | |
677 | break; | |
678 | } | |
679 | } | |
680 | return binding; | |
681 | } | |
682 | ||
683 | DEFUN ("default-toplevel-value", Fdefault_toplevel_value, Sdefault_toplevel_value, 1, 1, 0, | |
684 | doc: /* Return SYMBOL's toplevel default value. | |
685 | "Toplevel" means outside of any let binding. */) | |
686 | (Lisp_Object symbol) | |
687 | { | |
688 | union specbinding *binding = default_toplevel_binding (symbol); | |
689 | Lisp_Object value | |
690 | = binding ? specpdl_old_value (binding) : Fdefault_value (symbol); | |
691 | if (!EQ (value, Qunbound)) | |
692 | return value; | |
693 | xsignal1 (Qvoid_variable, symbol); | |
694 | } | |
695 | ||
696 | DEFUN ("set-default-toplevel-value", Fset_default_toplevel_value, | |
697 | Sset_default_toplevel_value, 2, 2, 0, | |
698 | doc: /* Set SYMBOL's toplevel default value to VALUE. | |
699 | "Toplevel" means outside of any let binding. */) | |
700 | (Lisp_Object symbol, Lisp_Object value) | |
701 | { | |
702 | union specbinding *binding = default_toplevel_binding (symbol); | |
703 | if (binding) | |
704 | set_specpdl_old_value (binding, value); | |
705 | else | |
706 | Fset_default (symbol, value); | |
707 | return Qnil; | |
708 | } | |
19cebf5a | 709 | |
db9f0278 | 710 | DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0, |
29357847 | 711 | doc: /* Define SYMBOL as a variable, and return SYMBOL. |
c3a70e2b CY |
712 | You are not required to define a variable in order to use it, but |
713 | defining it lets you supply an initial value and documentation, which | |
714 | can be referred to by the Emacs help facilities and other programming | |
715 | tools. The `defvar' form also declares the variable as \"special\", | |
716 | so that it is always dynamically bound even if `lexical-binding' is t. | |
717 | ||
718 | The optional argument INITVALUE is evaluated, and used to set SYMBOL, | |
719 | only if SYMBOL's value is void. If SYMBOL is buffer-local, its | |
720 | default value is what is set; buffer-local values are not affected. | |
9dbc9081 | 721 | If INITVALUE is missing, SYMBOL's value is not set. |
733f68b6 LT |
722 | |
723 | If SYMBOL has a local binding, then this form affects the local | |
724 | binding. This is usually not what you want. Thus, if you need to | |
725 | load a file defining variables, with this form or with `defconst' or | |
726 | `defcustom', you should always load that file _outside_ any bindings | |
727 | for these variables. \(`defconst' and `defcustom' behave similarly in | |
728 | this respect.) | |
c3a70e2b CY |
729 | |
730 | The optional argument DOCSTRING is a documentation string for the | |
731 | variable. | |
732 | ||
733 | To define a user option, use `defcustom' instead of `defvar'. | |
2df5238c | 734 | usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */) |
5842a27b | 735 | (Lisp_Object args) |
db9f0278 | 736 | { |
16b0520a | 737 | Lisp_Object sym, tem, tail; |
db9f0278 | 738 | |
16b0520a PE |
739 | sym = XCAR (args); |
740 | tail = XCDR (args); | |
a42ba017 | 741 | |
16b0520a | 742 | if (CONSP (tail)) |
db9f0278 | 743 | { |
16b0520a PE |
744 | if (CONSP (XCDR (tail)) && CONSP (XCDR (XCDR (tail)))) |
745 | error ("Too many arguments"); | |
746 | ||
747 | tem = Fdefault_boundp (sym); | |
748 | ||
ba83908c SM |
749 | /* Do it before evaluating the initial value, for self-references. */ |
750 | XSYMBOL (sym)->declared_special = 1; | |
590130fb | 751 | |
265a9e55 | 752 | if (NILP (tem)) |
16b0520a | 753 | Fset_default (sym, eval_sub (XCAR (tail))); |
d0bce91e SM |
754 | else |
755 | { /* Check if there is really a global binding rather than just a let | |
756 | binding that shadows the global unboundness of the var. */ | |
a104f656 SM |
757 | union specbinding *binding = default_toplevel_binding (sym); |
758 | if (binding && EQ (specpdl_old_value (binding), Qunbound)) | |
d0bce91e | 759 | { |
a104f656 | 760 | set_specpdl_old_value (binding, eval_sub (XCAR (tail))); |
d0bce91e SM |
761 | } |
762 | } | |
16b0520a | 763 | tail = XCDR (tail); |
e509f168 SM |
764 | tem = Fcar (tail); |
765 | if (!NILP (tem)) | |
33568849 | 766 | { |
33568849 SM |
767 | if (!NILP (Vpurify_flag)) |
768 | tem = Fpurecopy (tem); | |
769 | Fput (sym, Qvariable_documentation, tem); | |
770 | } | |
6fd797f5 | 771 | LOADHIST_ATTACH (sym); |
db9f0278 | 772 | } |
f07a954e SM |
773 | else if (!NILP (Vinternal_interpreter_environment) |
774 | && !XSYMBOL (sym)->declared_special) | |
775 | /* A simple (defvar foo) with lexical scoping does "nothing" except | |
776 | declare that var to be dynamically scoped *locally* (i.e. within | |
777 | the current file or let-block). */ | |
23ba2705 SM |
778 | Vinternal_interpreter_environment |
779 | = Fcons (sym, Vinternal_interpreter_environment); | |
33568849 | 780 | else |
d28a2170 PE |
781 | { |
782 | /* Simple (defvar <var>) should not count as a definition at all. | |
783 | It could get in the way of other definitions, and unloading this | |
784 | package could try to make the variable unbound. */ | |
785 | } | |
addf35fd | 786 | |
db9f0278 JB |
787 | return sym; |
788 | } | |
789 | ||
790 | DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0, | |
9dbc9081 | 791 | doc: /* Define SYMBOL as a constant variable. |
c3a70e2b CY |
792 | This declares that neither programs nor users should ever change the |
793 | value. This constancy is not actually enforced by Emacs Lisp, but | |
794 | SYMBOL is marked as a special variable so that it is never lexically | |
795 | bound. | |
796 | ||
797 | The `defconst' form always sets the value of SYMBOL to the result of | |
798 | evalling INITVALUE. If SYMBOL is buffer-local, its default value is | |
799 | what is set; buffer-local values are not affected. If SYMBOL has a | |
800 | local binding, then this form sets the local binding's value. | |
801 | However, you should normally not make local bindings for variables | |
802 | defined with this form. | |
803 | ||
804 | The optional DOCSTRING specifies the variable's documentation string. | |
7a25dc6d | 805 | usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */) |
5842a27b | 806 | (Lisp_Object args) |
db9f0278 | 807 | { |
16b0520a | 808 | Lisp_Object sym, tem; |
db9f0278 | 809 | |
16b0520a PE |
810 | sym = XCAR (args); |
811 | if (CONSP (Fcdr (XCDR (XCDR (args))))) | |
921baa95 | 812 | error ("Too many arguments"); |
a42ba017 | 813 | |
16b0520a | 814 | tem = eval_sub (Fcar (XCDR (args))); |
1182a7cb DL |
815 | if (!NILP (Vpurify_flag)) |
816 | tem = Fpurecopy (tem); | |
817 | Fset_default (sym, tem); | |
b9598260 | 818 | XSYMBOL (sym)->declared_special = 1; |
16b0520a | 819 | tem = Fcar (XCDR (XCDR (args))); |
265a9e55 | 820 | if (!NILP (tem)) |
db9f0278 | 821 | { |
265a9e55 | 822 | if (!NILP (Vpurify_flag)) |
db9f0278 JB |
823 | tem = Fpurecopy (tem); |
824 | Fput (sym, Qvariable_documentation, tem); | |
825 | } | |
873759d5 | 826 | Fput (sym, Qrisky_local_variable, Qt); |
6fd797f5 | 827 | LOADHIST_ATTACH (sym); |
db9f0278 JB |
828 | return sym; |
829 | } | |
830 | ||
513749ee SM |
831 | /* Make SYMBOL lexically scoped. */ |
832 | DEFUN ("internal-make-var-non-special", Fmake_var_non_special, | |
833 | Smake_var_non_special, 1, 1, 0, | |
834 | doc: /* Internal function. */) | |
835 | (Lisp_Object symbol) | |
836 | { | |
837 | CHECK_SYMBOL (symbol); | |
838 | XSYMBOL (symbol)->declared_special = 0; | |
839 | return Qnil; | |
840 | } | |
841 | ||
db9f0278 JB |
842 | \f |
843 | DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0, | |
9dbc9081 PJ |
844 | doc: /* Bind variables according to VARLIST then eval BODY. |
845 | The value of the last form in BODY is returned. | |
846 | Each element of VARLIST is a symbol (which is bound to nil) | |
847 | or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM). | |
848 | Each VALUEFORM can refer to the symbols already bound by this VARLIST. | |
7a25dc6d | 849 | usage: (let* VARLIST BODY...) */) |
5842a27b | 850 | (Lisp_Object args) |
db9f0278 | 851 | { |
b9598260 | 852 | Lisp_Object varlist, var, val, elt, lexenv; |
d311d28c | 853 | ptrdiff_t count = SPECPDL_INDEX (); |
db9f0278 JB |
854 | struct gcpro gcpro1, gcpro2, gcpro3; |
855 | ||
856 | GCPRO3 (args, elt, varlist); | |
857 | ||
b9598260 SM |
858 | lexenv = Vinternal_interpreter_environment; |
859 | ||
16b0520a | 860 | varlist = XCAR (args); |
b9598260 | 861 | while (CONSP (varlist)) |
db9f0278 JB |
862 | { |
863 | QUIT; | |
b9598260 SM |
864 | |
865 | elt = XCAR (varlist); | |
90165123 | 866 | if (SYMBOLP (elt)) |
b9598260 SM |
867 | { |
868 | var = elt; | |
869 | val = Qnil; | |
870 | } | |
08564963 | 871 | else if (! NILP (Fcdr (Fcdr (elt)))) |
734d55a2 | 872 | signal_error ("`let' bindings can have only one value-form", elt); |
db9f0278 JB |
873 | else |
874 | { | |
b9598260 | 875 | var = Fcar (elt); |
defb1411 | 876 | val = eval_sub (Fcar (Fcdr (elt))); |
db9f0278 | 877 | } |
b9598260 | 878 | |
f07a954e SM |
879 | if (!NILP (lexenv) && SYMBOLP (var) |
880 | && !XSYMBOL (var)->declared_special | |
881 | && NILP (Fmemq (var, Vinternal_interpreter_environment))) | |
b9598260 SM |
882 | /* Lexically bind VAR by adding it to the interpreter's binding |
883 | alist. */ | |
884 | { | |
f07a954e SM |
885 | Lisp_Object newenv |
886 | = Fcons (Fcons (var, val), Vinternal_interpreter_environment); | |
887 | if (EQ (Vinternal_interpreter_environment, lexenv)) | |
888 | /* Save the old lexical environment on the specpdl stack, | |
889 | but only for the first lexical binding, since we'll never | |
890 | need to revert to one of the intermediate ones. */ | |
891 | specbind (Qinternal_interpreter_environment, newenv); | |
892 | else | |
893 | Vinternal_interpreter_environment = newenv; | |
db9f0278 | 894 | } |
b9598260 SM |
895 | else |
896 | specbind (var, val); | |
897 | ||
898 | varlist = XCDR (varlist); | |
db9f0278 JB |
899 | } |
900 | UNGCPRO; | |
16b0520a | 901 | val = Fprogn (XCDR (args)); |
db9f0278 JB |
902 | return unbind_to (count, val); |
903 | } | |
904 | ||
905 | DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0, | |
9dbc9081 PJ |
906 | doc: /* Bind variables according to VARLIST then eval BODY. |
907 | The value of the last form in BODY is returned. | |
908 | Each element of VARLIST is a symbol (which is bound to nil) | |
909 | or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM). | |
910 | All the VALUEFORMs are evalled before any symbols are bound. | |
7a25dc6d | 911 | usage: (let VARLIST BODY...) */) |
5842a27b | 912 | (Lisp_Object args) |
db9f0278 | 913 | { |
b9598260 | 914 | Lisp_Object *temps, tem, lexenv; |
db9f0278 | 915 | register Lisp_Object elt, varlist; |
d311d28c | 916 | ptrdiff_t count = SPECPDL_INDEX (); |
f66c7cf8 | 917 | ptrdiff_t argnum; |
db9f0278 | 918 | struct gcpro gcpro1, gcpro2; |
3a7a9129 | 919 | USE_SAFE_ALLOCA; |
db9f0278 | 920 | |
16b0520a | 921 | varlist = XCAR (args); |
db9f0278 | 922 | |
f6d62986 | 923 | /* Make space to hold the values to give the bound variables. */ |
db9f0278 | 924 | elt = Flength (varlist); |
b72e0717 | 925 | SAFE_ALLOCA_LISP (temps, XFASTINT (elt)); |
db9f0278 | 926 | |
f6d62986 | 927 | /* Compute the values and store them in `temps'. */ |
db9f0278 JB |
928 | |
929 | GCPRO2 (args, *temps); | |
930 | gcpro2.nvars = 0; | |
931 | ||
67ee9f6e | 932 | for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist)) |
db9f0278 JB |
933 | { |
934 | QUIT; | |
67ee9f6e | 935 | elt = XCAR (varlist); |
90165123 | 936 | if (SYMBOLP (elt)) |
db9f0278 | 937 | temps [argnum++] = Qnil; |
08564963 | 938 | else if (! NILP (Fcdr (Fcdr (elt)))) |
734d55a2 | 939 | signal_error ("`let' bindings can have only one value-form", elt); |
db9f0278 | 940 | else |
defb1411 | 941 | temps [argnum++] = eval_sub (Fcar (Fcdr (elt))); |
db9f0278 JB |
942 | gcpro2.nvars = argnum; |
943 | } | |
944 | UNGCPRO; | |
945 | ||
b9598260 SM |
946 | lexenv = Vinternal_interpreter_environment; |
947 | ||
16b0520a | 948 | varlist = XCAR (args); |
67ee9f6e | 949 | for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist)) |
db9f0278 | 950 | { |
b9598260 SM |
951 | Lisp_Object var; |
952 | ||
67ee9f6e | 953 | elt = XCAR (varlist); |
b9598260 | 954 | var = SYMBOLP (elt) ? elt : Fcar (elt); |
db9f0278 | 955 | tem = temps[argnum++]; |
b9598260 | 956 | |
f07a954e SM |
957 | if (!NILP (lexenv) && SYMBOLP (var) |
958 | && !XSYMBOL (var)->declared_special | |
959 | && NILP (Fmemq (var, Vinternal_interpreter_environment))) | |
b9598260 SM |
960 | /* Lexically bind VAR by adding it to the lexenv alist. */ |
961 | lexenv = Fcons (Fcons (var, tem), lexenv); | |
db9f0278 | 962 | else |
b9598260 SM |
963 | /* Dynamically bind VAR. */ |
964 | specbind (var, tem); | |
db9f0278 JB |
965 | } |
966 | ||
b9598260 SM |
967 | if (!EQ (lexenv, Vinternal_interpreter_environment)) |
968 | /* Instantiate a new lexical environment. */ | |
969 | specbind (Qinternal_interpreter_environment, lexenv); | |
970 | ||
16b0520a | 971 | elt = Fprogn (XCDR (args)); |
3a7a9129 | 972 | SAFE_FREE (); |
db9f0278 JB |
973 | return unbind_to (count, elt); |
974 | } | |
975 | ||
976 | DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0, | |
9dbc9081 PJ |
977 | doc: /* If TEST yields non-nil, eval BODY... and repeat. |
978 | The order of execution is thus TEST, BODY, TEST, BODY and so on | |
979 | until TEST returns nil. | |
7a25dc6d | 980 | usage: (while TEST BODY...) */) |
5842a27b | 981 | (Lisp_Object args) |
db9f0278 | 982 | { |
2b9bde76 | 983 | Lisp_Object test, body; |
db9f0278 JB |
984 | struct gcpro gcpro1, gcpro2; |
985 | ||
986 | GCPRO2 (test, body); | |
987 | ||
16b0520a PE |
988 | test = XCAR (args); |
989 | body = XCDR (args); | |
defb1411 | 990 | while (!NILP (eval_sub (test))) |
db9f0278 JB |
991 | { |
992 | QUIT; | |
993 | Fprogn (body); | |
994 | } | |
995 | ||
996 | UNGCPRO; | |
997 | return Qnil; | |
998 | } | |
999 | ||
1000 | DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0, | |
9dbc9081 PJ |
1001 | doc: /* Return result of expanding macros at top level of FORM. |
1002 | If FORM is not a macro call, it is returned unchanged. | |
1003 | Otherwise, the macro is expanded and the expansion is considered | |
1004 | in place of FORM. When a non-macro-call results, it is returned. | |
1005 | ||
1006 | The second optional arg ENVIRONMENT specifies an environment of macro | |
1007 | definitions to shadow the loaded ones for use in file byte-compilation. */) | |
5842a27b | 1008 | (Lisp_Object form, Lisp_Object environment) |
db9f0278 | 1009 | { |
23d6b5a6 | 1010 | /* With cleanups from Hallvard Furuseth. */ |
db9f0278 JB |
1011 | register Lisp_Object expander, sym, def, tem; |
1012 | ||
1013 | while (1) | |
1014 | { | |
1015 | /* Come back here each time we expand a macro call, | |
1016 | in case it expands into another macro call. */ | |
90165123 | 1017 | if (!CONSP (form)) |
db9f0278 | 1018 | break; |
23d6b5a6 | 1019 | /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */ |
03699b14 | 1020 | def = sym = XCAR (form); |
23d6b5a6 | 1021 | tem = Qnil; |
db9f0278 JB |
1022 | /* Trace symbols aliases to other symbols |
1023 | until we get a symbol that is not an alias. */ | |
90165123 | 1024 | while (SYMBOLP (def)) |
db9f0278 JB |
1025 | { |
1026 | QUIT; | |
23d6b5a6 | 1027 | sym = def; |
79e8bfbf | 1028 | tem = Fassq (sym, environment); |
265a9e55 | 1029 | if (NILP (tem)) |
db9f0278 | 1030 | { |
c644523b | 1031 | def = XSYMBOL (sym)->function; |
eadf1faa | 1032 | if (!NILP (def)) |
23d6b5a6 | 1033 | continue; |
db9f0278 | 1034 | } |
23d6b5a6 | 1035 | break; |
db9f0278 | 1036 | } |
79e8bfbf | 1037 | /* Right now TEM is the result from SYM in ENVIRONMENT, |
db9f0278 | 1038 | and if TEM is nil then DEF is SYM's function definition. */ |
265a9e55 | 1039 | if (NILP (tem)) |
db9f0278 | 1040 | { |
79e8bfbf | 1041 | /* SYM is not mentioned in ENVIRONMENT. |
db9f0278 | 1042 | Look at its function definition. */ |
7abaf5cc SM |
1043 | struct gcpro gcpro1; |
1044 | GCPRO1 (form); | |
1045 | def = Fautoload_do_load (def, sym, Qmacro); | |
1046 | UNGCPRO; | |
eadf1faa | 1047 | if (!CONSP (def)) |
f6d62986 | 1048 | /* Not defined or definition not suitable. */ |
db9f0278 | 1049 | break; |
7abaf5cc | 1050 | if (!EQ (XCAR (def), Qmacro)) |
db9f0278 | 1051 | break; |
03699b14 | 1052 | else expander = XCDR (def); |
db9f0278 JB |
1053 | } |
1054 | else | |
1055 | { | |
03699b14 | 1056 | expander = XCDR (tem); |
265a9e55 | 1057 | if (NILP (expander)) |
db9f0278 JB |
1058 | break; |
1059 | } | |
4f18a4ed SM |
1060 | { |
1061 | Lisp_Object newform = apply1 (expander, XCDR (form)); | |
1062 | if (EQ (form, newform)) | |
1063 | break; | |
1064 | else | |
1065 | form = newform; | |
1066 | } | |
db9f0278 JB |
1067 | } |
1068 | return form; | |
1069 | } | |
1070 | \f | |
1071 | DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0, | |
9dbc9081 PJ |
1072 | doc: /* Eval BODY allowing nonlocal exits using `throw'. |
1073 | TAG is evalled to get the tag to use; it must not be nil. | |
1074 | ||
1075 | Then the BODY is executed. | |
1d632ccf | 1076 | Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'. |
9dbc9081 PJ |
1077 | If no throw happens, `catch' returns the value of the last BODY form. |
1078 | If a throw happens, it specifies the value to return from `catch'. | |
7a25dc6d | 1079 | usage: (catch TAG BODY...) */) |
5842a27b | 1080 | (Lisp_Object args) |
db9f0278 JB |
1081 | { |
1082 | register Lisp_Object tag; | |
1083 | struct gcpro gcpro1; | |
1084 | ||
1085 | GCPRO1 (args); | |
16b0520a | 1086 | tag = eval_sub (XCAR (args)); |
db9f0278 | 1087 | UNGCPRO; |
16b0520a | 1088 | return internal_catch (tag, Fprogn, XCDR (args)); |
db9f0278 JB |
1089 | } |
1090 | ||
b52f569d PE |
1091 | /* Assert that E is true, as a comment only. Use this instead of |
1092 | eassert (E) when E contains variables that might be clobbered by a | |
1093 | longjmp. */ | |
1094 | ||
1095 | #define clobbered_eassert(E) ((void) 0) | |
1096 | ||
db9f0278 JB |
1097 | /* Set up a catch, then call C function FUNC on argument ARG. |
1098 | FUNC should return a Lisp_Object. | |
2f592f95 | 1099 | This is how catches are done from within C code. */ |
db9f0278 JB |
1100 | |
1101 | Lisp_Object | |
d3da34e0 | 1102 | internal_catch (Lisp_Object tag, Lisp_Object (*func) (Lisp_Object), Lisp_Object arg) |
db9f0278 JB |
1103 | { |
1104 | /* This structure is made part of the chain `catchlist'. */ | |
adf2aa61 | 1105 | struct handler *c; |
db9f0278 JB |
1106 | |
1107 | /* Fill in the components of c, and put it on the list. */ | |
adf2aa61 | 1108 | PUSH_HANDLER (c, tag, CATCHER); |
db9f0278 JB |
1109 | |
1110 | /* Call FUNC. */ | |
adf2aa61 SM |
1111 | if (! sys_setjmp (c->jmp)) |
1112 | { | |
1113 | Lisp_Object val = (*func) (arg); | |
b52f569d PE |
1114 | clobbered_eassert (handlerlist == c); |
1115 | handlerlist = handlerlist->next; | |
adf2aa61 SM |
1116 | return val; |
1117 | } | |
1118 | else | |
1119 | { /* Throw works by a longjmp that comes right here. */ | |
1120 | Lisp_Object val = handlerlist->val; | |
b52f569d | 1121 | clobbered_eassert (handlerlist == c); |
adf2aa61 SM |
1122 | handlerlist = handlerlist->next; |
1123 | return val; | |
1124 | } | |
db9f0278 JB |
1125 | } |
1126 | ||
ba410f40 JB |
1127 | /* Unwind the specbind, catch, and handler stacks back to CATCH, and |
1128 | jump to that CATCH, returning VALUE as the value of that catch. | |
db9f0278 | 1129 | |
4d7e6e51 | 1130 | This is the guts of Fthrow and Fsignal; they differ only in the way |
ba410f40 JB |
1131 | they choose the catch tag to throw to. A catch tag for a |
1132 | condition-case form has a TAG of Qnil. | |
db9f0278 | 1133 | |
ba410f40 JB |
1134 | Before each catch is discarded, unbind all special bindings and |
1135 | execute all unwind-protect clauses made above that catch. Unwind | |
1136 | the handler stack as we go, so that the proper handlers are in | |
1137 | effect for each unwind-protect clause we run. At the end, restore | |
1138 | some static info saved in CATCH, and longjmp to the location | |
4d7e6e51 | 1139 | specified there. |
ba410f40 JB |
1140 | |
1141 | This is used for correct unwinding in Fthrow and Fsignal. */ | |
db9f0278 | 1142 | |
845ca893 | 1143 | static _Noreturn void |
adf2aa61 | 1144 | unwind_to_catch (struct handler *catch, Lisp_Object value) |
db9f0278 | 1145 | { |
1882aa38 | 1146 | bool last_time; |
db9f0278 | 1147 | |
70de5e86 SM |
1148 | eassert (catch->next); |
1149 | ||
ba410f40 JB |
1150 | /* Save the value in the tag. */ |
1151 | catch->val = value; | |
1152 | ||
0b31741c | 1153 | /* Restore certain special C variables. */ |
1cdc3155 | 1154 | set_poll_suppress_count (catch->poll_suppress_count); |
4d7e6e51 | 1155 | unblock_input_to (catch->interrupt_input_blocked); |
69bbd6bd | 1156 | immediate_quit = 0; |
82da7701 | 1157 | |
db9f0278 JB |
1158 | do |
1159 | { | |
82da7701 | 1160 | /* Unwind the specpdl stack, and then restore the proper set of |
bb8e180f | 1161 | handlers. */ |
adf2aa61 SM |
1162 | unbind_to (handlerlist->pdlcount, Qnil); |
1163 | last_time = handlerlist == catch; | |
1164 | if (! last_time) | |
1165 | handlerlist = handlerlist->next; | |
db9f0278 JB |
1166 | } |
1167 | while (! last_time); | |
1168 | ||
adf2aa61 SM |
1169 | eassert (handlerlist == catch); |
1170 | ||
bcf28080 | 1171 | byte_stack_list = catch->byte_stack; |
db9f0278 | 1172 | gcprolist = catch->gcpro; |
15934ffa | 1173 | #ifdef DEBUG_GCPRO |
d8e2b5ba | 1174 | gcpro_level = gcprolist ? gcprolist->level + 1 : 0; |
15934ffa | 1175 | #endif |
db9f0278 | 1176 | lisp_eval_depth = catch->lisp_eval_depth; |
177c0ea7 | 1177 | |
0328b6de | 1178 | sys_longjmp (catch->jmp, 1); |
db9f0278 JB |
1179 | } |
1180 | ||
a7ca3326 | 1181 | DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0, |
9dbc9081 PJ |
1182 | doc: /* Throw to the catch for TAG and return VALUE from it. |
1183 | Both TAG and VALUE are evalled. */) | |
5842a27b | 1184 | (register Lisp_Object tag, Lisp_Object value) |
db9f0278 | 1185 | { |
adf2aa61 | 1186 | struct handler *c; |
db9f0278 | 1187 | |
8788120f | 1188 | if (!NILP (tag)) |
adf2aa61 | 1189 | for (c = handlerlist; c; c = c->next) |
8788120f | 1190 | { |
adf2aa61 | 1191 | if (c->type == CATCHER && EQ (c->tag_or_ch, tag)) |
8788120f KS |
1192 | unwind_to_catch (c, value); |
1193 | } | |
734d55a2 | 1194 | xsignal2 (Qno_catch, tag, value); |
db9f0278 JB |
1195 | } |
1196 | ||
1197 | ||
1198 | DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0, | |
9dbc9081 PJ |
1199 | doc: /* Do BODYFORM, protecting with UNWINDFORMS. |
1200 | If BODYFORM completes normally, its value is returned | |
1201 | after executing the UNWINDFORMS. | |
1202 | If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway. | |
7a25dc6d | 1203 | usage: (unwind-protect BODYFORM UNWINDFORMS...) */) |
5842a27b | 1204 | (Lisp_Object args) |
db9f0278 JB |
1205 | { |
1206 | Lisp_Object val; | |
d311d28c | 1207 | ptrdiff_t count = SPECPDL_INDEX (); |
db9f0278 | 1208 | |
16b0520a PE |
1209 | record_unwind_protect (unwind_body, XCDR (args)); |
1210 | val = eval_sub (XCAR (args)); | |
177c0ea7 | 1211 | return unbind_to (count, val); |
db9f0278 JB |
1212 | } |
1213 | \f | |
db9f0278 | 1214 | DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0, |
9dbc9081 | 1215 | doc: /* Regain control when an error is signaled. |
1b1acc13 | 1216 | Executes BODYFORM and returns its value if no error happens. |
9dbc9081 PJ |
1217 | Each element of HANDLERS looks like (CONDITION-NAME BODY...) |
1218 | where the BODY is made of Lisp expressions. | |
1219 | ||
1220 | A handler is applicable to an error | |
1221 | if CONDITION-NAME is one of the error's condition names. | |
1222 | If an error happens, the first applicable handler is run. | |
1223 | ||
024a2d76 CY |
1224 | The car of a handler may be a list of condition names instead of a |
1225 | single condition name; then it handles all of them. If the special | |
1226 | condition name `debug' is present in this list, it allows another | |
1227 | condition in the list to run the debugger if `debug-on-error' and the | |
1228 | other usual mechanisms says it should (otherwise, `condition-case' | |
1229 | suppresses the debugger). | |
9dbc9081 | 1230 | |
c997bb25 RS |
1231 | When a handler handles an error, control returns to the `condition-case' |
1232 | and it executes the handler's BODY... | |
d0acbbaf | 1233 | with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error. |
bb8e180f | 1234 | \(If VAR is nil, the handler can't access that information.) |
c997bb25 RS |
1235 | Then the value of the last BODY form is returned from the `condition-case' |
1236 | expression. | |
9dbc9081 | 1237 | |
9dbc9081 | 1238 | See also the function `signal' for more info. |
2b47b74d | 1239 | usage: (condition-case VAR BODYFORM &rest HANDLERS) */) |
bb8e180f | 1240 | (Lisp_Object args) |
db9f0278 | 1241 | { |
16b0520a PE |
1242 | Lisp_Object var = XCAR (args); |
1243 | Lisp_Object bodyform = XCAR (XCDR (args)); | |
1244 | Lisp_Object handlers = XCDR (XCDR (args)); | |
ee830945 RS |
1245 | |
1246 | return internal_lisp_condition_case (var, bodyform, handlers); | |
1247 | } | |
1248 | ||
1249 | /* Like Fcondition_case, but the args are separate | |
1250 | rather than passed in a list. Used by Fbyte_code. */ | |
1251 | ||
1252 | Lisp_Object | |
d3da34e0 JB |
1253 | internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform, |
1254 | Lisp_Object handlers) | |
ee830945 RS |
1255 | { |
1256 | Lisp_Object val; | |
adf2aa61 SM |
1257 | struct handler *c; |
1258 | struct handler *oldhandlerlist = handlerlist; | |
1259 | int clausenb = 0; | |
ee830945 | 1260 | |
b7826503 | 1261 | CHECK_SYMBOL (var); |
82da7701 | 1262 | |
2b47b74d | 1263 | for (val = handlers; CONSP (val); val = XCDR (val)) |
82da7701 | 1264 | { |
adf2aa61 SM |
1265 | Lisp_Object tem = XCAR (val); |
1266 | clausenb++; | |
5f96776a RS |
1267 | if (! (NILP (tem) |
1268 | || (CONSP (tem) | |
03699b14 KR |
1269 | && (SYMBOLP (XCAR (tem)) |
1270 | || CONSP (XCAR (tem)))))) | |
e6c3da20 EZ |
1271 | error ("Invalid condition handler: %s", |
1272 | SDATA (Fprin1_to_string (tem, Qt))); | |
82da7701 | 1273 | } |
db9f0278 | 1274 | |
adf2aa61 SM |
1275 | { /* The first clause is the one that should be checked first, so it should |
1276 | be added to handlerlist last. So we build in `clauses' a table that | |
1277 | contains `handlers' but in reverse order. */ | |
1278 | Lisp_Object *clauses = alloca (clausenb * sizeof (Lisp_Object *)); | |
b52f569d | 1279 | Lisp_Object *volatile clauses_volatile = clauses; |
adf2aa61 SM |
1280 | int i = clausenb; |
1281 | for (val = handlers; CONSP (val); val = XCDR (val)) | |
1282 | clauses[--i] = XCAR (val); | |
1283 | for (i = 0; i < clausenb; i++) | |
1284 | { | |
1285 | Lisp_Object clause = clauses[i]; | |
1286 | Lisp_Object condition = XCAR (clause); | |
1287 | if (!CONSP (condition)) | |
1288 | condition = Fcons (condition, Qnil); | |
1289 | PUSH_HANDLER (c, condition, CONDITION_CASE); | |
1290 | if (sys_setjmp (c->jmp)) | |
1291 | { | |
1292 | ptrdiff_t count = SPECPDL_INDEX (); | |
1293 | Lisp_Object val = handlerlist->val; | |
b52f569d | 1294 | Lisp_Object *chosen_clause = clauses_volatile; |
adf2aa61 SM |
1295 | for (c = handlerlist->next; c != oldhandlerlist; c = c->next) |
1296 | chosen_clause++; | |
1297 | handlerlist = oldhandlerlist; | |
1298 | if (!NILP (var)) | |
1299 | { | |
1300 | if (!NILP (Vinternal_interpreter_environment)) | |
1301 | specbind (Qinternal_interpreter_environment, | |
1302 | Fcons (Fcons (var, val), | |
1303 | Vinternal_interpreter_environment)); | |
1304 | else | |
1305 | specbind (var, val); | |
1306 | } | |
1307 | val = Fprogn (XCDR (*chosen_clause)); | |
1308 | /* Note that this just undoes the binding of var; whoever | |
1309 | longjumped to us unwound the stack to c.pdlcount before | |
1310 | throwing. */ | |
1311 | if (!NILP (var)) | |
1312 | unbind_to (count, Qnil); | |
1313 | return val; | |
1314 | } | |
1315 | } | |
db9f0278 | 1316 | } |
db9f0278 | 1317 | |
defb1411 | 1318 | val = eval_sub (bodyform); |
adf2aa61 | 1319 | handlerlist = oldhandlerlist; |
db9f0278 JB |
1320 | return val; |
1321 | } | |
1322 | ||
f029ca5f RS |
1323 | /* Call the function BFUN with no arguments, catching errors within it |
1324 | according to HANDLERS. If there is an error, call HFUN with | |
1325 | one argument which is the data that describes the error: | |
1326 | (SIGNALNAME . DATA) | |
1327 | ||
1328 | HANDLERS can be a list of conditions to catch. | |
1329 | If HANDLERS is Qt, catch all errors. | |
1330 | If HANDLERS is Qerror, catch all errors | |
1331 | but allow the debugger to run if that is enabled. */ | |
1332 | ||
db9f0278 | 1333 | Lisp_Object |
d3da34e0 JB |
1334 | internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers, |
1335 | Lisp_Object (*hfun) (Lisp_Object)) | |
db9f0278 JB |
1336 | { |
1337 | Lisp_Object val; | |
adf2aa61 SM |
1338 | struct handler *c; |
1339 | ||
1340 | PUSH_HANDLER (c, handlers, CONDITION_CASE); | |
1341 | if (sys_setjmp (c->jmp)) | |
db9f0278 | 1342 | { |
adf2aa61 | 1343 | Lisp_Object val = handlerlist->val; |
b52f569d | 1344 | clobbered_eassert (handlerlist == c); |
adf2aa61 SM |
1345 | handlerlist = handlerlist->next; |
1346 | return (*hfun) (val); | |
db9f0278 | 1347 | } |
db9f0278 JB |
1348 | |
1349 | val = (*bfun) (); | |
b52f569d PE |
1350 | clobbered_eassert (handlerlist == c); |
1351 | handlerlist = handlerlist->next; | |
db9f0278 JB |
1352 | return val; |
1353 | } | |
1354 | ||
2659a09f | 1355 | /* Like internal_condition_case but call BFUN with ARG as its argument. */ |
f029ca5f | 1356 | |
d227775c | 1357 | Lisp_Object |
d3da34e0 JB |
1358 | internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg, |
1359 | Lisp_Object handlers, Lisp_Object (*hfun) (Lisp_Object)) | |
d227775c RS |
1360 | { |
1361 | Lisp_Object val; | |
adf2aa61 SM |
1362 | struct handler *c; |
1363 | ||
1364 | PUSH_HANDLER (c, handlers, CONDITION_CASE); | |
1365 | if (sys_setjmp (c->jmp)) | |
d227775c | 1366 | { |
adf2aa61 | 1367 | Lisp_Object val = handlerlist->val; |
b52f569d | 1368 | clobbered_eassert (handlerlist == c); |
adf2aa61 SM |
1369 | handlerlist = handlerlist->next; |
1370 | return (*hfun) (val); | |
d227775c | 1371 | } |
d227775c RS |
1372 | |
1373 | val = (*bfun) (arg); | |
b52f569d PE |
1374 | clobbered_eassert (handlerlist == c); |
1375 | handlerlist = handlerlist->next; | |
d227775c RS |
1376 | return val; |
1377 | } | |
10b29d41 | 1378 | |
53967e09 CY |
1379 | /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as |
1380 | its arguments. */ | |
1381 | ||
1382 | Lisp_Object | |
178f2507 SM |
1383 | internal_condition_case_2 (Lisp_Object (*bfun) (Lisp_Object, Lisp_Object), |
1384 | Lisp_Object arg1, | |
1385 | Lisp_Object arg2, | |
1386 | Lisp_Object handlers, | |
1387 | Lisp_Object (*hfun) (Lisp_Object)) | |
53967e09 CY |
1388 | { |
1389 | Lisp_Object val; | |
adf2aa61 SM |
1390 | struct handler *c; |
1391 | ||
1392 | PUSH_HANDLER (c, handlers, CONDITION_CASE); | |
1393 | if (sys_setjmp (c->jmp)) | |
53967e09 | 1394 | { |
adf2aa61 | 1395 | Lisp_Object val = handlerlist->val; |
b52f569d | 1396 | clobbered_eassert (handlerlist == c); |
adf2aa61 SM |
1397 | handlerlist = handlerlist->next; |
1398 | return (*hfun) (val); | |
53967e09 | 1399 | } |
53967e09 CY |
1400 | |
1401 | val = (*bfun) (arg1, arg2); | |
b52f569d PE |
1402 | clobbered_eassert (handlerlist == c); |
1403 | handlerlist = handlerlist->next; | |
53967e09 CY |
1404 | return val; |
1405 | } | |
10b29d41 | 1406 | |
2659a09f | 1407 | /* Like internal_condition_case but call BFUN with NARGS as first, |
10b29d41 GM |
1408 | and ARGS as second argument. */ |
1409 | ||
1410 | Lisp_Object | |
f66c7cf8 PE |
1411 | internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *), |
1412 | ptrdiff_t nargs, | |
178f2507 SM |
1413 | Lisp_Object *args, |
1414 | Lisp_Object handlers, | |
cc92c454 SM |
1415 | Lisp_Object (*hfun) (Lisp_Object err, |
1416 | ptrdiff_t nargs, | |
1417 | Lisp_Object *args)) | |
10b29d41 GM |
1418 | { |
1419 | Lisp_Object val; | |
adf2aa61 SM |
1420 | struct handler *c; |
1421 | ||
1422 | PUSH_HANDLER (c, handlers, CONDITION_CASE); | |
1423 | if (sys_setjmp (c->jmp)) | |
10b29d41 | 1424 | { |
adf2aa61 | 1425 | Lisp_Object val = handlerlist->val; |
b52f569d | 1426 | clobbered_eassert (handlerlist == c); |
adf2aa61 SM |
1427 | handlerlist = handlerlist->next; |
1428 | return (*hfun) (val, nargs, args); | |
10b29d41 | 1429 | } |
10b29d41 GM |
1430 | |
1431 | val = (*bfun) (nargs, args); | |
b52f569d PE |
1432 | clobbered_eassert (handlerlist == c); |
1433 | handlerlist = handlerlist->next; | |
10b29d41 GM |
1434 | return val; |
1435 | } | |
1436 | ||
d227775c | 1437 | \f |
7d47b580 | 1438 | static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object); |
1882aa38 PE |
1439 | static bool maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, |
1440 | Lisp_Object data); | |
db9f0278 | 1441 | |
6d5eb5b0 SM |
1442 | void |
1443 | process_quit_flag (void) | |
1444 | { | |
1445 | Lisp_Object flag = Vquit_flag; | |
1446 | Vquit_flag = Qnil; | |
1447 | if (EQ (flag, Qkill_emacs)) | |
1448 | Fkill_emacs (Qnil); | |
1449 | if (EQ (Vthrow_on_input, flag)) | |
1450 | Fthrow (Vthrow_on_input, Qt); | |
1451 | Fsignal (Qquit, Qnil); | |
1452 | } | |
1453 | ||
a7ca3326 | 1454 | DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0, |
9dbc9081 PJ |
1455 | doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA. |
1456 | This function does not return. | |
1457 | ||
1458 | An error symbol is a symbol with an `error-conditions' property | |
1459 | that is a list of condition names. | |
1460 | A handler for any of those names will get to handle this signal. | |
1461 | The symbol `error' should normally be one of them. | |
1462 | ||
1463 | DATA should be a list. Its elements are printed as part of the error message. | |
3297ec22 LT |
1464 | See Info anchor `(elisp)Definition of signal' for some details on how this |
1465 | error message is constructed. | |
9dbc9081 PJ |
1466 | If the signal is handled, DATA is made available to the handler. |
1467 | See also the function `condition-case'. */) | |
5842a27b | 1468 | (Lisp_Object error_symbol, Lisp_Object data) |
db9f0278 | 1469 | { |
bfa8ca43 | 1470 | /* When memory is full, ERROR-SYMBOL is nil, |
26631f2b RS |
1471 | and DATA is (REAL-ERROR-SYMBOL . REAL-DATA). |
1472 | That is a special case--don't do this in other situations. */ | |
db9f0278 | 1473 | Lisp_Object conditions; |
c11d3d17 | 1474 | Lisp_Object string; |
e7f7fbaa SM |
1475 | Lisp_Object real_error_symbol |
1476 | = (NILP (error_symbol) ? Fcar (data) : error_symbol); | |
1477 | register Lisp_Object clause = Qnil; | |
1478 | struct handler *h; | |
db9f0278 | 1479 | |
0caaedb1 | 1480 | immediate_quit = 0; |
d063129f | 1481 | abort_on_gc = 0; |
db9f0278 | 1482 | if (gc_in_progress || waiting_for_input) |
1088b922 | 1483 | emacs_abort (); |
db9f0278 | 1484 | |
26631f2b RS |
1485 | #if 0 /* rms: I don't know why this was here, |
1486 | but it is surely wrong for an error that is handled. */ | |
d148e14d | 1487 | #ifdef HAVE_WINDOW_SYSTEM |
df6c90d8 GM |
1488 | if (display_hourglass_p) |
1489 | cancel_hourglass (); | |
48f8dfa3 | 1490 | #endif |
177c0ea7 | 1491 | #endif |
48f8dfa3 | 1492 | |
61ede770 | 1493 | /* This hook is used by edebug. */ |
26631f2b RS |
1494 | if (! NILP (Vsignal_hook_function) |
1495 | && ! NILP (error_symbol)) | |
9f5903bb RS |
1496 | { |
1497 | /* Edebug takes care of restoring these variables when it exits. */ | |
1498 | if (lisp_eval_depth + 20 > max_lisp_eval_depth) | |
1499 | max_lisp_eval_depth = lisp_eval_depth + 20; | |
1500 | ||
1501 | if (SPECPDL_INDEX () + 40 > max_specpdl_size) | |
1502 | max_specpdl_size = SPECPDL_INDEX () + 40; | |
1503 | ||
1504 | call2 (Vsignal_hook_function, error_symbol, data); | |
1505 | } | |
61ede770 | 1506 | |
1ea9dec4 | 1507 | conditions = Fget (real_error_symbol, Qerror_conditions); |
db9f0278 | 1508 | |
a2ff3819 GM |
1509 | /* Remember from where signal was called. Skip over the frame for |
1510 | `signal' itself. If a frame for `error' follows, skip that, | |
26631f2b RS |
1511 | too. Don't do this when ERROR_SYMBOL is nil, because that |
1512 | is a memory-full error. */ | |
090a072f | 1513 | Vsignaling_function = Qnil; |
2f592f95 | 1514 | if (!NILP (error_symbol)) |
090a072f | 1515 | { |
9349e5f7 | 1516 | union specbinding *pdl = backtrace_next (backtrace_top ()); |
2f592f95 SM |
1517 | if (backtrace_p (pdl) && EQ (backtrace_function (pdl), Qerror)) |
1518 | pdl = backtrace_next (pdl); | |
1519 | if (backtrace_p (pdl)) | |
1520 | Vsignaling_function = backtrace_function (pdl); | |
090a072f | 1521 | } |
a2ff3819 | 1522 | |
e7f7fbaa | 1523 | for (h = handlerlist; h; h = h->next) |
db9f0278 | 1524 | { |
adf2aa61 SM |
1525 | if (h->type != CONDITION_CASE) |
1526 | continue; | |
1527 | clause = find_handler_clause (h->tag_or_ch, conditions); | |
265a9e55 | 1528 | if (!NILP (clause)) |
e7f7fbaa | 1529 | break; |
db9f0278 | 1530 | } |
475545b5 | 1531 | |
e7f7fbaa | 1532 | if (/* Don't run the debugger for a memory-full error. |
e7c1b6ef | 1533 | (There is no room in memory to do that!) */ |
e7f7fbaa SM |
1534 | !NILP (error_symbol) |
1535 | && (!NILP (Vdebug_on_signal) | |
1536 | /* If no handler is present now, try to run the debugger. */ | |
1537 | || NILP (clause) | |
bd1ba3e8 CY |
1538 | /* A `debug' symbol in the handler list disables the normal |
1539 | suppression of the debugger. */ | |
afd4479f SM |
1540 | || (CONSP (clause) && CONSP (clause) |
1541 | && !NILP (Fmemq (Qdebug, clause))) | |
e7f7fbaa SM |
1542 | /* Special handler that means "print a message and run debugger |
1543 | if requested". */ | |
adf2aa61 | 1544 | || EQ (h->tag_or_ch, Qerror))) |
e7f7fbaa | 1545 | { |
1882aa38 | 1546 | bool debugger_called |
e7f7fbaa SM |
1547 | = maybe_call_debugger (conditions, error_symbol, data); |
1548 | /* We can't return values to code which signaled an error, but we | |
1549 | can continue code which has signaled a quit. */ | |
1550 | if (debugger_called && EQ (real_error_symbol, Qquit)) | |
1551 | return Qnil; | |
475545b5 | 1552 | } |
db9f0278 | 1553 | |
e7f7fbaa SM |
1554 | if (!NILP (clause)) |
1555 | { | |
1556 | Lisp_Object unwind_data | |
1557 | = (NILP (error_symbol) ? data : Fcons (error_symbol, data)); | |
475545b5 | 1558 | |
adf2aa61 | 1559 | unwind_to_catch (h, unwind_data); |
e7f7fbaa SM |
1560 | } |
1561 | else | |
1562 | { | |
70de5e86 SM |
1563 | if (handlerlist != &handlerlist_sentinel) |
1564 | /* FIXME: This will come right back here if there's no `top-level' | |
1565 | catcher. A better solution would be to abort here, and instead | |
1566 | add a catch-all condition handler so we never come here. */ | |
e7f7fbaa SM |
1567 | Fthrow (Qtop_level, Qt); |
1568 | } | |
c11d3d17 | 1569 | |
1ea9dec4 | 1570 | if (! NILP (error_symbol)) |
c11d3d17 | 1571 | data = Fcons (error_symbol, data); |
475545b5 | 1572 | |
c11d3d17 | 1573 | string = Ferror_message_string (data); |
583f48b9 | 1574 | fatal ("%s", SDATA (string)); |
db9f0278 JB |
1575 | } |
1576 | ||
734d55a2 KS |
1577 | /* Internal version of Fsignal that never returns. |
1578 | Used for anything but Qquit (which can return from Fsignal). */ | |
1579 | ||
1580 | void | |
d3da34e0 | 1581 | xsignal (Lisp_Object error_symbol, Lisp_Object data) |
734d55a2 KS |
1582 | { |
1583 | Fsignal (error_symbol, data); | |
1088b922 | 1584 | emacs_abort (); |
734d55a2 KS |
1585 | } |
1586 | ||
1587 | /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */ | |
1588 | ||
1589 | void | |
d3da34e0 | 1590 | xsignal0 (Lisp_Object error_symbol) |
734d55a2 KS |
1591 | { |
1592 | xsignal (error_symbol, Qnil); | |
1593 | } | |
1594 | ||
1595 | void | |
d3da34e0 | 1596 | xsignal1 (Lisp_Object error_symbol, Lisp_Object arg) |
734d55a2 KS |
1597 | { |
1598 | xsignal (error_symbol, list1 (arg)); | |
1599 | } | |
1600 | ||
1601 | void | |
d3da34e0 | 1602 | xsignal2 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2) |
734d55a2 KS |
1603 | { |
1604 | xsignal (error_symbol, list2 (arg1, arg2)); | |
1605 | } | |
1606 | ||
1607 | void | |
d3da34e0 | 1608 | xsignal3 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3) |
734d55a2 KS |
1609 | { |
1610 | xsignal (error_symbol, list3 (arg1, arg2, arg3)); | |
1611 | } | |
1612 | ||
1613 | /* Signal `error' with message S, and additional arg ARG. | |
1614 | If ARG is not a genuine list, make it a one-element list. */ | |
1615 | ||
1616 | void | |
a8fe7202 | 1617 | signal_error (const char *s, Lisp_Object arg) |
734d55a2 KS |
1618 | { |
1619 | Lisp_Object tortoise, hare; | |
1620 | ||
1621 | hare = tortoise = arg; | |
1622 | while (CONSP (hare)) | |
1623 | { | |
1624 | hare = XCDR (hare); | |
1625 | if (!CONSP (hare)) | |
1626 | break; | |
1627 | ||
1628 | hare = XCDR (hare); | |
1629 | tortoise = XCDR (tortoise); | |
1630 | ||
1631 | if (EQ (hare, tortoise)) | |
1632 | break; | |
1633 | } | |
1634 | ||
1635 | if (!NILP (hare)) | |
6c6f1994 | 1636 | arg = list1 (arg); |
734d55a2 KS |
1637 | |
1638 | xsignal (Qerror, Fcons (build_string (s), arg)); | |
1639 | } | |
1640 | ||
1641 | ||
1882aa38 | 1642 | /* Return true if LIST is a non-nil atom or |
128c0f66 RM |
1643 | a list containing one of CONDITIONS. */ |
1644 | ||
1882aa38 | 1645 | static bool |
d3da34e0 | 1646 | wants_debugger (Lisp_Object list, Lisp_Object conditions) |
128c0f66 | 1647 | { |
4de86b16 | 1648 | if (NILP (list)) |
128c0f66 RM |
1649 | return 0; |
1650 | if (! CONSP (list)) | |
1651 | return 1; | |
1652 | ||
ab67260b | 1653 | while (CONSP (conditions)) |
128c0f66 | 1654 | { |
ab67260b | 1655 | Lisp_Object this, tail; |
03699b14 KR |
1656 | this = XCAR (conditions); |
1657 | for (tail = list; CONSP (tail); tail = XCDR (tail)) | |
1658 | if (EQ (XCAR (tail), this)) | |
128c0f66 | 1659 | return 1; |
03699b14 | 1660 | conditions = XCDR (conditions); |
128c0f66 | 1661 | } |
ab67260b | 1662 | return 0; |
128c0f66 RM |
1663 | } |
1664 | ||
1882aa38 | 1665 | /* Return true if an error with condition-symbols CONDITIONS, |
fc950e09 | 1666 | and described by SIGNAL-DATA, should skip the debugger |
1b1acc13 | 1667 | according to debugger-ignored-errors. */ |
fc950e09 | 1668 | |
1882aa38 | 1669 | static bool |
d3da34e0 | 1670 | skip_debugger (Lisp_Object conditions, Lisp_Object data) |
fc950e09 KH |
1671 | { |
1672 | Lisp_Object tail; | |
1882aa38 | 1673 | bool first_string = 1; |
fc950e09 KH |
1674 | Lisp_Object error_message; |
1675 | ||
17401c97 GM |
1676 | error_message = Qnil; |
1677 | for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail)) | |
fc950e09 | 1678 | { |
03699b14 | 1679 | if (STRINGP (XCAR (tail))) |
fc950e09 KH |
1680 | { |
1681 | if (first_string) | |
1682 | { | |
1683 | error_message = Ferror_message_string (data); | |
1684 | first_string = 0; | |
1685 | } | |
177c0ea7 | 1686 | |
03699b14 | 1687 | if (fast_string_match (XCAR (tail), error_message) >= 0) |
fc950e09 KH |
1688 | return 1; |
1689 | } | |
1690 | else | |
1691 | { | |
1692 | Lisp_Object contail; | |
1693 | ||
17401c97 | 1694 | for (contail = conditions; CONSP (contail); contail = XCDR (contail)) |
03699b14 | 1695 | if (EQ (XCAR (tail), XCAR (contail))) |
fc950e09 KH |
1696 | return 1; |
1697 | } | |
1698 | } | |
1699 | ||
1700 | return 0; | |
1701 | } | |
1702 | ||
ddaa36e1 | 1703 | /* Call the debugger if calling it is currently enabled for CONDITIONS. |
7d47b580 JB |
1704 | SIG and DATA describe the signal. There are two ways to pass them: |
1705 | = SIG is the error symbol, and DATA is the rest of the data. | |
1706 | = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA). | |
1707 | This is for memory-full errors only. */ | |
1882aa38 | 1708 | static bool |
d3da34e0 | 1709 | maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data) |
ddaa36e1 AS |
1710 | { |
1711 | Lisp_Object combined_data; | |
1712 | ||
1713 | combined_data = Fcons (sig, data); | |
1714 | ||
1715 | if ( | |
1716 | /* Don't try to run the debugger with interrupts blocked. | |
1717 | The editing loop would return anyway. */ | |
4d7e6e51 | 1718 | ! input_blocked_p () |
45b82ad0 | 1719 | && NILP (Vinhibit_debugger) |
ddaa36e1 AS |
1720 | /* Does user want to enter debugger for this kind of error? */ |
1721 | && (EQ (sig, Qquit) | |
1722 | ? debug_on_quit | |
1723 | : wants_debugger (Vdebug_on_error, conditions)) | |
1724 | && ! skip_debugger (conditions, combined_data) | |
f6d62986 | 1725 | /* RMS: What's this for? */ |
ddaa36e1 AS |
1726 | && when_entered_debugger < num_nonmacro_input_events) |
1727 | { | |
6c6f1994 | 1728 | call_debugger (list2 (Qerror, combined_data)); |
ddaa36e1 AS |
1729 | return 1; |
1730 | } | |
1731 | ||
1732 | return 0; | |
1733 | } | |
1734 | ||
db9f0278 | 1735 | static Lisp_Object |
7d47b580 | 1736 | find_handler_clause (Lisp_Object handlers, Lisp_Object conditions) |
db9f0278 JB |
1737 | { |
1738 | register Lisp_Object h; | |
db9f0278 | 1739 | |
f01cbfdd RS |
1740 | /* t is used by handlers for all conditions, set up by C code. */ |
1741 | if (EQ (handlers, Qt)) | |
db9f0278 | 1742 | return Qt; |
f01cbfdd | 1743 | |
61ede770 RS |
1744 | /* error is used similarly, but means print an error message |
1745 | and run the debugger if that is enabled. */ | |
e7f7fbaa SM |
1746 | if (EQ (handlers, Qerror)) |
1747 | return Qt; | |
f01cbfdd | 1748 | |
e7f7fbaa | 1749 | for (h = handlers; CONSP (h); h = XCDR (h)) |
db9f0278 | 1750 | { |
e7f7fbaa | 1751 | Lisp_Object handler = XCAR (h); |
adf2aa61 SM |
1752 | if (!NILP (Fmemq (handler, conditions))) |
1753 | return handlers; | |
db9f0278 | 1754 | } |
f01cbfdd | 1755 | |
db9f0278 JB |
1756 | return Qnil; |
1757 | } | |
1758 | ||
db9f0278 | 1759 | |
f6d62986 | 1760 | /* Dump an error message; called like vprintf. */ |
db9f0278 | 1761 | void |
b3ffc17c | 1762 | verror (const char *m, va_list ap) |
db9f0278 | 1763 | { |
70476b54 | 1764 | char buf[4000]; |
c2d1e36d PE |
1765 | ptrdiff_t size = sizeof buf; |
1766 | ptrdiff_t size_max = STRING_BYTES_BOUND + 1; | |
9125da08 | 1767 | char *buffer = buf; |
c2d1e36d | 1768 | ptrdiff_t used; |
9125da08 RS |
1769 | Lisp_Object string; |
1770 | ||
d749b01b | 1771 | used = evxprintf (&buffer, &size, buf, size_max, m, ap); |
5fdb398c | 1772 | string = make_string (buffer, used); |
eb3f1cc8 | 1773 | if (buffer != buf) |
9ae6734f | 1774 | xfree (buffer); |
9125da08 | 1775 | |
734d55a2 | 1776 | xsignal1 (Qerror, string); |
db9f0278 | 1777 | } |
b3ffc17c DN |
1778 | |
1779 | ||
f6d62986 | 1780 | /* Dump an error message; called like printf. */ |
b3ffc17c DN |
1781 | |
1782 | /* VARARGS 1 */ | |
1783 | void | |
1784 | error (const char *m, ...) | |
1785 | { | |
1786 | va_list ap; | |
1787 | va_start (ap, m); | |
1788 | verror (m, ap); | |
b3ffc17c | 1789 | } |
db9f0278 | 1790 | \f |
a7ca3326 | 1791 | DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0, |
9dbc9081 PJ |
1792 | doc: /* Non-nil if FUNCTION makes provisions for interactive calling. |
1793 | This means it contains a description for how to read arguments to give it. | |
1794 | The value is nil for an invalid function or a symbol with no function | |
1795 | definition. | |
1796 | ||
1797 | Interactively callable functions include strings and vectors (treated | |
1798 | as keyboard macros), lambda-expressions that contain a top-level call | |
1799 | to `interactive', autoload definitions made by `autoload' with non-nil | |
1800 | fourth argument, and some of the built-in functions of Lisp. | |
1801 | ||
e72706be RS |
1802 | Also, a symbol satisfies `commandp' if its function definition does so. |
1803 | ||
1804 | If the optional argument FOR-CALL-INTERACTIVELY is non-nil, | |
769b4fb2 | 1805 | then strings and vectors are not accepted. */) |
5842a27b | 1806 | (Lisp_Object function, Lisp_Object for_call_interactively) |
db9f0278 JB |
1807 | { |
1808 | register Lisp_Object fun; | |
1809 | register Lisp_Object funcar; | |
52b71f49 | 1810 | Lisp_Object if_prop = Qnil; |
db9f0278 JB |
1811 | |
1812 | fun = function; | |
1813 | ||
eadf1faa SM |
1814 | fun = indirect_function (fun); /* Check cycles. */ |
1815 | if (NILP (fun)) | |
ffd56f97 | 1816 | return Qnil; |
db9f0278 | 1817 | |
52b71f49 | 1818 | /* Check an `interactive-form' property if present, analogous to the |
eadf1faa | 1819 | function-documentation property. */ |
52b71f49 SM |
1820 | fun = function; |
1821 | while (SYMBOLP (fun)) | |
1822 | { | |
2b9aa051 | 1823 | Lisp_Object tmp = Fget (fun, Qinteractive_form); |
52b71f49 SM |
1824 | if (!NILP (tmp)) |
1825 | if_prop = Qt; | |
1826 | fun = Fsymbol_function (fun); | |
1827 | } | |
1828 | ||
db9f0278 JB |
1829 | /* Emacs primitives are interactive if their DEFUN specifies an |
1830 | interactive spec. */ | |
90165123 | 1831 | if (SUBRP (fun)) |
04724b69 | 1832 | return XSUBR (fun)->intspec ? Qt : if_prop; |
db9f0278 JB |
1833 | |
1834 | /* Bytecode objects are interactive if they are long enough to | |
1835 | have an element whose index is COMPILED_INTERACTIVE, which is | |
1836 | where the interactive spec is stored. */ | |
90165123 | 1837 | else if (COMPILEDP (fun)) |
845975f5 | 1838 | return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE |
52b71f49 | 1839 | ? Qt : if_prop); |
db9f0278 JB |
1840 | |
1841 | /* Strings and vectors are keyboard macros. */ | |
52b71f49 | 1842 | if (STRINGP (fun) || VECTORP (fun)) |
6e33efc4 | 1843 | return (NILP (for_call_interactively) ? Qt : Qnil); |
db9f0278 JB |
1844 | |
1845 | /* Lists may represent commands. */ | |
1846 | if (!CONSP (fun)) | |
1847 | return Qnil; | |
ed16fb98 | 1848 | funcar = XCAR (fun); |
b38b1ec0 | 1849 | if (EQ (funcar, Qclosure)) |
7200d79c SM |
1850 | return (!NILP (Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun))))) |
1851 | ? Qt : if_prop); | |
23aba0ea | 1852 | else if (EQ (funcar, Qlambda)) |
52b71f49 | 1853 | return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop; |
b38b1ec0 | 1854 | else if (EQ (funcar, Qautoload)) |
52b71f49 | 1855 | return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop; |
db9f0278 JB |
1856 | else |
1857 | return Qnil; | |
1858 | } | |
1859 | ||
db9f0278 | 1860 | DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0, |
9dbc9081 PJ |
1861 | doc: /* Define FUNCTION to autoload from FILE. |
1862 | FUNCTION is a symbol; FILE is a file name string to pass to `load'. | |
1863 | Third arg DOCSTRING is documentation for the function. | |
1864 | Fourth arg INTERACTIVE if non-nil says function can be called interactively. | |
1865 | Fifth arg TYPE indicates the type of the object: | |
1866 | nil or omitted says FUNCTION is a function, | |
1867 | `keymap' says FUNCTION is really a keymap, and | |
1868 | `macro' or t says FUNCTION is really a macro. | |
1869 | Third through fifth args give info about the real definition. | |
1870 | They default to nil. | |
1871 | If FUNCTION is already defined other than as an autoload, | |
1872 | this does nothing and returns nil. */) | |
5842a27b | 1873 | (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object interactive, Lisp_Object type) |
db9f0278 | 1874 | { |
b7826503 PJ |
1875 | CHECK_SYMBOL (function); |
1876 | CHECK_STRING (file); | |
db9f0278 | 1877 | |
f6d62986 | 1878 | /* If function is defined and not as an autoload, don't override. */ |
eadf1faa | 1879 | if (!NILP (XSYMBOL (function)->function) |
32e5c58c | 1880 | && !AUTOLOADP (XSYMBOL (function)->function)) |
db9f0278 JB |
1881 | return Qnil; |
1882 | ||
32e5c58c | 1883 | if (!NILP (Vpurify_flag) && EQ (docstring, make_number (0))) |
61b108cc SM |
1884 | /* `read1' in lread.c has found the docstring starting with "\ |
1885 | and assumed the docstring will be provided by Snarf-documentation, so it | |
1886 | passed us 0 instead. But that leads to accidental sharing in purecopy's | |
1887 | hash-consing, so we use a (hopefully) unique integer instead. */ | |
32e5c58c SM |
1888 | docstring = make_number (XHASH (function)); |
1889 | return Fdefalias (function, | |
1890 | list5 (Qautoload, file, docstring, interactive, type), | |
1891 | Qnil); | |
db9f0278 JB |
1892 | } |
1893 | ||
27e498e6 | 1894 | void |
d3da34e0 | 1895 | un_autoload (Lisp_Object oldqueue) |
db9f0278 | 1896 | { |
27e498e6 | 1897 | Lisp_Object queue, first, second; |
db9f0278 JB |
1898 | |
1899 | /* Queue to unwind is current value of Vautoload_queue. | |
1900 | oldqueue is the shadowed value to leave in Vautoload_queue. */ | |
1901 | queue = Vautoload_queue; | |
1902 | Vautoload_queue = oldqueue; | |
1903 | while (CONSP (queue)) | |
1904 | { | |
e509f168 | 1905 | first = XCAR (queue); |
db9f0278 JB |
1906 | second = Fcdr (first); |
1907 | first = Fcar (first); | |
47b82df9 RS |
1908 | if (EQ (first, make_number (0))) |
1909 | Vfeatures = second; | |
db9f0278 JB |
1910 | else |
1911 | Ffset (first, second); | |
e509f168 | 1912 | queue = XCDR (queue); |
db9f0278 | 1913 | } |
db9f0278 JB |
1914 | } |
1915 | ||
ca20916b RS |
1916 | /* Load an autoloaded function. |
1917 | FUNNAME is the symbol which is the function's name. | |
1918 | FUNDEF is the autoload definition (a list). */ | |
1919 | ||
7abaf5cc SM |
1920 | DEFUN ("autoload-do-load", Fautoload_do_load, Sautoload_do_load, 1, 3, 0, |
1921 | doc: /* Load FUNDEF which should be an autoload. | |
1922 | If non-nil, FUNNAME should be the symbol whose function value is FUNDEF, | |
1923 | in which case the function returns the new autoloaded function value. | |
1924 | If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if | |
1925 | it is defines a macro. */) | |
1926 | (Lisp_Object fundef, Lisp_Object funname, Lisp_Object macro_only) | |
db9f0278 | 1927 | { |
d311d28c | 1928 | ptrdiff_t count = SPECPDL_INDEX (); |
ca20916b | 1929 | struct gcpro gcpro1, gcpro2, gcpro3; |
db9f0278 | 1930 | |
7abaf5cc SM |
1931 | if (!CONSP (fundef) || !EQ (Qautoload, XCAR (fundef))) |
1932 | return fundef; | |
1933 | ||
1934 | if (EQ (macro_only, Qmacro)) | |
1935 | { | |
1936 | Lisp_Object kind = Fnth (make_number (4), fundef); | |
1937 | if (! (EQ (kind, Qt) || EQ (kind, Qmacro))) | |
1938 | return fundef; | |
1939 | } | |
1940 | ||
aea6173f RS |
1941 | /* This is to make sure that loadup.el gives a clear picture |
1942 | of what files are preloaded and when. */ | |
ab4db096 RS |
1943 | if (! NILP (Vpurify_flag)) |
1944 | error ("Attempt to autoload %s while preparing to dump", | |
d5db4077 | 1945 | SDATA (SYMBOL_NAME (funname))); |
ab4db096 | 1946 | |
b7826503 | 1947 | CHECK_SYMBOL (funname); |
7abaf5cc | 1948 | GCPRO3 (funname, fundef, macro_only); |
db9f0278 | 1949 | |
f87740dc | 1950 | /* Preserve the match data. */ |
89f2614d | 1951 | record_unwind_save_match_data (); |
177c0ea7 | 1952 | |
a04ee161 RS |
1953 | /* If autoloading gets an error (which includes the error of failing |
1954 | to define the function being called), we use Vautoload_queue | |
1955 | to undo function definitions and `provide' calls made by | |
1956 | the function. We do this in the specific case of autoloading | |
1957 | because autoloading is not an explicit request "load this file", | |
1958 | but rather a request to "call this function". | |
d3da34e0 | 1959 | |
a04ee161 | 1960 | The value saved here is to be restored into Vautoload_queue. */ |
db9f0278 JB |
1961 | record_unwind_protect (un_autoload, Vautoload_queue); |
1962 | Vautoload_queue = Qt; | |
7abaf5cc SM |
1963 | /* If `macro_only', assume this autoload to be a "best-effort", |
1964 | so don't signal an error if autoloading fails. */ | |
1965 | Fload (Fcar (Fcdr (fundef)), macro_only, Qt, Qnil, Qt); | |
2a49b6e5 | 1966 | |
db9f0278 JB |
1967 | /* Once loading finishes, don't undo it. */ |
1968 | Vautoload_queue = Qt; | |
1969 | unbind_to (count, Qnil); | |
1970 | ||
ca20916b | 1971 | UNGCPRO; |
7abaf5cc SM |
1972 | |
1973 | if (NILP (funname)) | |
1974 | return Qnil; | |
1975 | else | |
1976 | { | |
1977 | Lisp_Object fun = Findirect_function (funname, Qnil); | |
1978 | ||
1979 | if (!NILP (Fequal (fun, fundef))) | |
1980 | error ("Autoloading failed to define function %s", | |
1981 | SDATA (SYMBOL_NAME (funname))); | |
1982 | else | |
1983 | return fun; | |
1984 | } | |
db9f0278 | 1985 | } |
4c576a83 | 1986 | |
db9f0278 | 1987 | \f |
a7ca3326 | 1988 | DEFUN ("eval", Feval, Seval, 1, 2, 0, |
a0ee6f27 | 1989 | doc: /* Evaluate FORM and return its value. |
8c27f5ff SM |
1990 | If LEXICAL is t, evaluate using lexical scoping. |
1991 | LEXICAL can also be an actual lexical environment, in the form of an | |
1992 | alist mapping symbols to their value. */) | |
a0ee6f27 | 1993 | (Lisp_Object form, Lisp_Object lexical) |
defb1411 | 1994 | { |
d311d28c | 1995 | ptrdiff_t count = SPECPDL_INDEX (); |
a0ee6f27 | 1996 | specbind (Qinternal_interpreter_environment, |
6c6f1994 | 1997 | CONSP (lexical) || NILP (lexical) ? lexical : list1 (Qt)); |
defb1411 SM |
1998 | return unbind_to (count, eval_sub (form)); |
1999 | } | |
2000 | ||
5e301d76 PE |
2001 | /* Grow the specpdl stack by one entry. |
2002 | The caller should have already initialized the entry. | |
2003 | Signal an error on stack overflow. | |
2004 | ||
2005 | Make sure that there is always one unused entry past the top of the | |
2006 | stack, so that the just-initialized entry is safely unwound if | |
2007 | memory exhausted and an error is signaled here. Also, allocate a | |
2008 | never-used entry just before the bottom of the stack; sometimes its | |
2009 | address is taken. */ | |
2010 | ||
2f592f95 SM |
2011 | static void |
2012 | grow_specpdl (void) | |
2013 | { | |
5e301d76 PE |
2014 | specpdl_ptr++; |
2015 | ||
2016 | if (specpdl_ptr == specpdl + specpdl_size) | |
2f592f95 | 2017 | { |
5e301d76 PE |
2018 | ptrdiff_t count = SPECPDL_INDEX (); |
2019 | ptrdiff_t max_size = min (max_specpdl_size, PTRDIFF_MAX - 1000); | |
2020 | union specbinding *pdlvec = specpdl - 1; | |
2021 | ptrdiff_t pdlvecsize = specpdl_size + 1; | |
2f592f95 | 2022 | if (max_size <= specpdl_size) |
5e301d76 PE |
2023 | { |
2024 | if (max_specpdl_size < 400) | |
2025 | max_size = max_specpdl_size = 400; | |
2026 | if (max_size <= specpdl_size) | |
2027 | signal_error ("Variable binding depth exceeds max-specpdl-size", | |
2028 | Qnil); | |
2029 | } | |
2030 | pdlvec = xpalloc (pdlvec, &pdlvecsize, 1, max_size + 1, sizeof *specpdl); | |
2031 | specpdl = pdlvec + 1; | |
2032 | specpdl_size = pdlvecsize - 1; | |
2033 | specpdl_ptr = specpdl + count; | |
2f592f95 | 2034 | } |
2f592f95 SM |
2035 | } |
2036 | ||
3d5ee10a | 2037 | void |
2f592f95 SM |
2038 | record_in_backtrace (Lisp_Object function, Lisp_Object *args, ptrdiff_t nargs) |
2039 | { | |
2040 | eassert (nargs >= UNEVALLED); | |
9349e5f7 PE |
2041 | specpdl_ptr->bt.kind = SPECPDL_BACKTRACE; |
2042 | specpdl_ptr->bt.debug_on_exit = false; | |
2043 | specpdl_ptr->bt.function = function; | |
2044 | specpdl_ptr->bt.args = args; | |
2045 | specpdl_ptr->bt.nargs = nargs; | |
5e301d76 | 2046 | grow_specpdl (); |
2f592f95 SM |
2047 | } |
2048 | ||
defb1411 SM |
2049 | /* Eval a sub-expression of the current expression (i.e. in the same |
2050 | lexical scope). */ | |
2051 | Lisp_Object | |
2052 | eval_sub (Lisp_Object form) | |
db9f0278 JB |
2053 | { |
2054 | Lisp_Object fun, val, original_fun, original_args; | |
2055 | Lisp_Object funcar; | |
db9f0278 JB |
2056 | struct gcpro gcpro1, gcpro2, gcpro3; |
2057 | ||
90165123 | 2058 | if (SYMBOLP (form)) |
b9598260 | 2059 | { |
f07a954e SM |
2060 | /* Look up its binding in the lexical environment. |
2061 | We do not pay attention to the declared_special flag here, since we | |
2062 | already did that when let-binding the variable. */ | |
2063 | Lisp_Object lex_binding | |
2064 | = !NILP (Vinternal_interpreter_environment) /* Mere optimization! */ | |
2065 | ? Fassq (form, Vinternal_interpreter_environment) | |
2066 | : Qnil; | |
2067 | if (CONSP (lex_binding)) | |
2068 | return XCDR (lex_binding); | |
2069 | else | |
2070 | return Fsymbol_value (form); | |
b9598260 SM |
2071 | } |
2072 | ||
db9f0278 JB |
2073 | if (!CONSP (form)) |
2074 | return form; | |
2075 | ||
2076 | QUIT; | |
9d5a1260 DA |
2077 | |
2078 | GCPRO1 (form); | |
765e61e3 | 2079 | maybe_gc (); |
9d5a1260 | 2080 | UNGCPRO; |
db9f0278 JB |
2081 | |
2082 | if (++lisp_eval_depth > max_lisp_eval_depth) | |
2083 | { | |
2084 | if (max_lisp_eval_depth < 100) | |
2085 | max_lisp_eval_depth = 100; | |
2086 | if (lisp_eval_depth > max_lisp_eval_depth) | |
921baa95 | 2087 | error ("Lisp nesting exceeds `max-lisp-eval-depth'"); |
db9f0278 JB |
2088 | } |
2089 | ||
7d7bbefd DA |
2090 | original_fun = XCAR (form); |
2091 | original_args = XCDR (form); | |
db9f0278 | 2092 | |
2f592f95 SM |
2093 | /* This also protects them from gc. */ |
2094 | record_in_backtrace (original_fun, &original_args, UNEVALLED); | |
db9f0278 JB |
2095 | |
2096 | if (debug_on_next_call) | |
2097 | do_debug_on_call (Qt); | |
2098 | ||
2099 | /* At this point, only original_fun and original_args | |
f6d62986 | 2100 | have values that will be used below. */ |
db9f0278 | 2101 | retry: |
8788120f KS |
2102 | |
2103 | /* Optimize for no indirection. */ | |
2104 | fun = original_fun; | |
306d67bd | 2105 | if (!SYMBOLP (fun)) |
3ec7babc | 2106 | fun = Ffunction (Fcons (fun, Qnil)); |
306d67bd SM |
2107 | else if (!NILP (fun) && (fun = XSYMBOL (fun)->function, SYMBOLP (fun))) |
2108 | fun = indirect_function (fun); | |
db9f0278 | 2109 | |
90165123 | 2110 | if (SUBRP (fun)) |
db9f0278 JB |
2111 | { |
2112 | Lisp_Object numargs; | |
166c822d | 2113 | Lisp_Object argvals[8]; |
db9f0278 JB |
2114 | Lisp_Object args_left; |
2115 | register int i, maxargs; | |
2116 | ||
2117 | args_left = original_args; | |
2118 | numargs = Flength (args_left); | |
2119 | ||
7e63e0c3 | 2120 | check_cons_list (); |
c1788fbc | 2121 | |
f6d62986 SM |
2122 | if (XINT (numargs) < XSUBR (fun)->min_args |
2123 | || (XSUBR (fun)->max_args >= 0 | |
2124 | && XSUBR (fun)->max_args < XINT (numargs))) | |
734d55a2 | 2125 | xsignal2 (Qwrong_number_of_arguments, original_fun, numargs); |
db9f0278 | 2126 | |
ef1b0ba7 | 2127 | else if (XSUBR (fun)->max_args == UNEVALLED) |
bbc6b304 | 2128 | val = (XSUBR (fun)->function.aUNEVALLED) (args_left); |
ef1b0ba7 | 2129 | else if (XSUBR (fun)->max_args == MANY) |
db9f0278 | 2130 | { |
f6d62986 | 2131 | /* Pass a vector of evaluated arguments. */ |
db9f0278 | 2132 | Lisp_Object *vals; |
f66c7cf8 | 2133 | ptrdiff_t argnum = 0; |
3a7a9129 | 2134 | USE_SAFE_ALLOCA; |
db9f0278 | 2135 | |
b72e0717 | 2136 | SAFE_ALLOCA_LISP (vals, XINT (numargs)); |
db9f0278 JB |
2137 | |
2138 | GCPRO3 (args_left, fun, fun); | |
2139 | gcpro3.var = vals; | |
2140 | gcpro3.nvars = 0; | |
2141 | ||
265a9e55 | 2142 | while (!NILP (args_left)) |
db9f0278 | 2143 | { |
defb1411 | 2144 | vals[argnum++] = eval_sub (Fcar (args_left)); |
db9f0278 JB |
2145 | args_left = Fcdr (args_left); |
2146 | gcpro3.nvars = argnum; | |
2147 | } | |
db9f0278 | 2148 | |
2f592f95 SM |
2149 | set_backtrace_args (specpdl_ptr - 1, vals); |
2150 | set_backtrace_nargs (specpdl_ptr - 1, XINT (numargs)); | |
db9f0278 | 2151 | |
d5273788 | 2152 | val = (XSUBR (fun)->function.aMANY) (XINT (numargs), vals); |
a6e3fa71 | 2153 | UNGCPRO; |
3a7a9129 | 2154 | SAFE_FREE (); |
db9f0278 | 2155 | } |
ef1b0ba7 | 2156 | else |
db9f0278 | 2157 | { |
ef1b0ba7 SM |
2158 | GCPRO3 (args_left, fun, fun); |
2159 | gcpro3.var = argvals; | |
2160 | gcpro3.nvars = 0; | |
db9f0278 | 2161 | |
ef1b0ba7 SM |
2162 | maxargs = XSUBR (fun)->max_args; |
2163 | for (i = 0; i < maxargs; args_left = Fcdr (args_left)) | |
2164 | { | |
a0ee6f27 | 2165 | argvals[i] = eval_sub (Fcar (args_left)); |
ef1b0ba7 SM |
2166 | gcpro3.nvars = ++i; |
2167 | } | |
db9f0278 | 2168 | |
ef1b0ba7 | 2169 | UNGCPRO; |
db9f0278 | 2170 | |
2f592f95 SM |
2171 | set_backtrace_args (specpdl_ptr - 1, argvals); |
2172 | set_backtrace_nargs (specpdl_ptr - 1, XINT (numargs)); | |
ef1b0ba7 SM |
2173 | |
2174 | switch (i) | |
2175 | { | |
2176 | case 0: | |
2177 | val = (XSUBR (fun)->function.a0 ()); | |
2178 | break; | |
2179 | case 1: | |
2180 | val = (XSUBR (fun)->function.a1 (argvals[0])); | |
2181 | break; | |
2182 | case 2: | |
2183 | val = (XSUBR (fun)->function.a2 (argvals[0], argvals[1])); | |
2184 | break; | |
2185 | case 3: | |
2186 | val = (XSUBR (fun)->function.a3 | |
2187 | (argvals[0], argvals[1], argvals[2])); | |
2188 | break; | |
2189 | case 4: | |
2190 | val = (XSUBR (fun)->function.a4 | |
2191 | (argvals[0], argvals[1], argvals[2], argvals[3])); | |
2192 | break; | |
2193 | case 5: | |
2194 | val = (XSUBR (fun)->function.a5 | |
2195 | (argvals[0], argvals[1], argvals[2], argvals[3], | |
2196 | argvals[4])); | |
2197 | break; | |
2198 | case 6: | |
2199 | val = (XSUBR (fun)->function.a6 | |
2200 | (argvals[0], argvals[1], argvals[2], argvals[3], | |
2201 | argvals[4], argvals[5])); | |
2202 | break; | |
2203 | case 7: | |
2204 | val = (XSUBR (fun)->function.a7 | |
2205 | (argvals[0], argvals[1], argvals[2], argvals[3], | |
2206 | argvals[4], argvals[5], argvals[6])); | |
2207 | break; | |
2208 | ||
2209 | case 8: | |
2210 | val = (XSUBR (fun)->function.a8 | |
2211 | (argvals[0], argvals[1], argvals[2], argvals[3], | |
2212 | argvals[4], argvals[5], argvals[6], argvals[7])); | |
2213 | break; | |
2214 | ||
2215 | default: | |
2216 | /* Someone has created a subr that takes more arguments than | |
2217 | is supported by this code. We need to either rewrite the | |
2218 | subr to use a different argument protocol, or add more | |
2219 | cases to this switch. */ | |
1088b922 | 2220 | emacs_abort (); |
ef1b0ba7 | 2221 | } |
db9f0278 JB |
2222 | } |
2223 | } | |
ef1b0ba7 | 2224 | else if (COMPILEDP (fun)) |
defb1411 | 2225 | val = apply_lambda (fun, original_args); |
db9f0278 JB |
2226 | else |
2227 | { | |
eadf1faa | 2228 | if (NILP (fun)) |
734d55a2 | 2229 | xsignal1 (Qvoid_function, original_fun); |
db9f0278 | 2230 | if (!CONSP (fun)) |
734d55a2 KS |
2231 | xsignal1 (Qinvalid_function, original_fun); |
2232 | funcar = XCAR (fun); | |
90165123 | 2233 | if (!SYMBOLP (funcar)) |
734d55a2 | 2234 | xsignal1 (Qinvalid_function, original_fun); |
db9f0278 JB |
2235 | if (EQ (funcar, Qautoload)) |
2236 | { | |
7abaf5cc | 2237 | Fautoload_do_load (fun, original_fun, Qnil); |
db9f0278 JB |
2238 | goto retry; |
2239 | } | |
2240 | if (EQ (funcar, Qmacro)) | |
8be3a09c SM |
2241 | { |
2242 | ptrdiff_t count = SPECPDL_INDEX (); | |
8be3a09c SM |
2243 | Lisp_Object exp; |
2244 | /* Bind lexical-binding during expansion of the macro, so the | |
2245 | macro can know reliably if the code it outputs will be | |
2246 | interpreted using lexical-binding or not. */ | |
2247 | specbind (Qlexical_binding, | |
2248 | NILP (Vinternal_interpreter_environment) ? Qnil : Qt); | |
2249 | exp = apply1 (Fcdr (fun), original_args); | |
2250 | unbind_to (count, Qnil); | |
2251 | val = eval_sub (exp); | |
2252 | } | |
defb1411 SM |
2253 | else if (EQ (funcar, Qlambda) |
2254 | || EQ (funcar, Qclosure)) | |
2255 | val = apply_lambda (fun, original_args); | |
db9f0278 | 2256 | else |
734d55a2 | 2257 | xsignal1 (Qinvalid_function, original_fun); |
db9f0278 | 2258 | } |
7e63e0c3 | 2259 | check_cons_list (); |
c1788fbc | 2260 | |
db9f0278 | 2261 | lisp_eval_depth--; |
2f592f95 | 2262 | if (backtrace_debug_on_exit (specpdl_ptr - 1)) |
6c6f1994 | 2263 | val = call_debugger (list2 (Qexit, val)); |
2f592f95 | 2264 | specpdl_ptr--; |
824eb35e | 2265 | |
db9f0278 JB |
2266 | return val; |
2267 | } | |
2268 | \f | |
8edd4a2b | 2269 | DEFUN ("apply", Fapply, Sapply, 1, MANY, 0, |
9dbc9081 PJ |
2270 | doc: /* Call FUNCTION with our remaining args, using our last arg as list of args. |
2271 | Then return the value FUNCTION returns. | |
2272 | Thus, (apply '+ 1 2 '(3 4)) returns 10. | |
2273 | usage: (apply FUNCTION &rest ARGUMENTS) */) | |
f66c7cf8 | 2274 | (ptrdiff_t nargs, Lisp_Object *args) |
db9f0278 | 2275 | { |
d311d28c PE |
2276 | ptrdiff_t i; |
2277 | EMACS_INT numargs; | |
db9f0278 JB |
2278 | register Lisp_Object spread_arg; |
2279 | register Lisp_Object *funcall_args; | |
3a7a9129 | 2280 | Lisp_Object fun, retval; |
96d44c64 | 2281 | struct gcpro gcpro1; |
3a7a9129 | 2282 | USE_SAFE_ALLOCA; |
db9f0278 JB |
2283 | |
2284 | fun = args [0]; | |
2285 | funcall_args = 0; | |
2286 | spread_arg = args [nargs - 1]; | |
b7826503 | 2287 | CHECK_LIST (spread_arg); |
177c0ea7 | 2288 | |
db9f0278 JB |
2289 | numargs = XINT (Flength (spread_arg)); |
2290 | ||
2291 | if (numargs == 0) | |
2292 | return Ffuncall (nargs - 1, args); | |
2293 | else if (numargs == 1) | |
2294 | { | |
03699b14 | 2295 | args [nargs - 1] = XCAR (spread_arg); |
db9f0278 JB |
2296 | return Ffuncall (nargs, args); |
2297 | } | |
2298 | ||
a6e3fa71 | 2299 | numargs += nargs - 2; |
db9f0278 | 2300 | |
8788120f | 2301 | /* Optimize for no indirection. */ |
eadf1faa | 2302 | if (SYMBOLP (fun) && !NILP (fun) |
c644523b | 2303 | && (fun = XSYMBOL (fun)->function, SYMBOLP (fun))) |
8788120f | 2304 | fun = indirect_function (fun); |
eadf1faa | 2305 | if (NILP (fun)) |
db9f0278 | 2306 | { |
f6d62986 | 2307 | /* Let funcall get the error. */ |
ffd56f97 JB |
2308 | fun = args[0]; |
2309 | goto funcall; | |
db9f0278 JB |
2310 | } |
2311 | ||
90165123 | 2312 | if (SUBRP (fun)) |
db9f0278 JB |
2313 | { |
2314 | if (numargs < XSUBR (fun)->min_args | |
2315 | || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs)) | |
f6d62986 | 2316 | goto funcall; /* Let funcall get the error. */ |
c5101a77 | 2317 | else if (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args > numargs) |
db9f0278 JB |
2318 | { |
2319 | /* Avoid making funcall cons up a yet another new vector of arguments | |
f6d62986 | 2320 | by explicitly supplying nil's for optional values. */ |
b72e0717 | 2321 | SAFE_ALLOCA_LISP (funcall_args, 1 + XSUBR (fun)->max_args); |
db9f0278 JB |
2322 | for (i = numargs; i < XSUBR (fun)->max_args;) |
2323 | funcall_args[++i] = Qnil; | |
96d44c64 SM |
2324 | GCPRO1 (*funcall_args); |
2325 | gcpro1.nvars = 1 + XSUBR (fun)->max_args; | |
db9f0278 JB |
2326 | } |
2327 | } | |
2328 | funcall: | |
2329 | /* We add 1 to numargs because funcall_args includes the | |
2330 | function itself as well as its arguments. */ | |
2331 | if (!funcall_args) | |
a6e3fa71 | 2332 | { |
b72e0717 | 2333 | SAFE_ALLOCA_LISP (funcall_args, 1 + numargs); |
96d44c64 SM |
2334 | GCPRO1 (*funcall_args); |
2335 | gcpro1.nvars = 1 + numargs; | |
a6e3fa71 JB |
2336 | } |
2337 | ||
663e2b3f | 2338 | memcpy (funcall_args, args, nargs * word_size); |
db9f0278 JB |
2339 | /* Spread the last arg we got. Its first element goes in |
2340 | the slot that it used to occupy, hence this value of I. */ | |
2341 | i = nargs - 1; | |
265a9e55 | 2342 | while (!NILP (spread_arg)) |
db9f0278 | 2343 | { |
03699b14 KR |
2344 | funcall_args [i++] = XCAR (spread_arg); |
2345 | spread_arg = XCDR (spread_arg); | |
db9f0278 | 2346 | } |
a6e3fa71 | 2347 | |
96d44c64 | 2348 | /* By convention, the caller needs to gcpro Ffuncall's args. */ |
3a7a9129 CY |
2349 | retval = Ffuncall (gcpro1.nvars, funcall_args); |
2350 | UNGCPRO; | |
2351 | SAFE_FREE (); | |
2352 | ||
2353 | return retval; | |
db9f0278 JB |
2354 | } |
2355 | \f | |
ff936e53 SM |
2356 | /* Run hook variables in various ways. */ |
2357 | ||
f6d62986 | 2358 | static Lisp_Object |
f66c7cf8 | 2359 | funcall_nil (ptrdiff_t nargs, Lisp_Object *args) |
f6d62986 SM |
2360 | { |
2361 | Ffuncall (nargs, args); | |
2362 | return Qnil; | |
2363 | } | |
ff936e53 | 2364 | |
a7ca3326 | 2365 | DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0, |
9f685258 | 2366 | doc: /* Run each hook in HOOKS. |
9dbc9081 PJ |
2367 | Each argument should be a symbol, a hook variable. |
2368 | These symbols are processed in the order specified. | |
2369 | If a hook symbol has a non-nil value, that value may be a function | |
2370 | or a list of functions to be called to run the hook. | |
2371 | If the value is a function, it is called with no arguments. | |
2372 | If it is a list, the elements are called, in order, with no arguments. | |
2373 | ||
9f685258 LK |
2374 | Major modes should not use this function directly to run their mode |
2375 | hook; they should use `run-mode-hooks' instead. | |
2376 | ||
72e85d5d RS |
2377 | Do not use `make-local-variable' to make a hook variable buffer-local. |
2378 | Instead, use `add-hook' and specify t for the LOCAL argument. | |
9dbc9081 | 2379 | usage: (run-hooks &rest HOOKS) */) |
f66c7cf8 | 2380 | (ptrdiff_t nargs, Lisp_Object *args) |
ff936e53 SM |
2381 | { |
2382 | Lisp_Object hook[1]; | |
f66c7cf8 | 2383 | ptrdiff_t i; |
ff936e53 SM |
2384 | |
2385 | for (i = 0; i < nargs; i++) | |
2386 | { | |
2387 | hook[0] = args[i]; | |
f6d62986 | 2388 | run_hook_with_args (1, hook, funcall_nil); |
ff936e53 SM |
2389 | } |
2390 | ||
2391 | return Qnil; | |
2392 | } | |
177c0ea7 | 2393 | |
a7ca3326 | 2394 | DEFUN ("run-hook-with-args", Frun_hook_with_args, |
9dbc9081 PJ |
2395 | Srun_hook_with_args, 1, MANY, 0, |
2396 | doc: /* Run HOOK with the specified arguments ARGS. | |
d393cefb GM |
2397 | HOOK should be a symbol, a hook variable. The value of HOOK |
2398 | may be nil, a function, or a list of functions. Call each | |
2399 | function in order with arguments ARGS. The final return value | |
2400 | is unspecified. | |
9dbc9081 | 2401 | |
72e85d5d RS |
2402 | Do not use `make-local-variable' to make a hook variable buffer-local. |
2403 | Instead, use `add-hook' and specify t for the LOCAL argument. | |
9dbc9081 | 2404 | usage: (run-hook-with-args HOOK &rest ARGS) */) |
f66c7cf8 | 2405 | (ptrdiff_t nargs, Lisp_Object *args) |
ff936e53 | 2406 | { |
f6d62986 | 2407 | return run_hook_with_args (nargs, args, funcall_nil); |
ff936e53 SM |
2408 | } |
2409 | ||
d393cefb GM |
2410 | /* NB this one still documents a specific non-nil return value. |
2411 | (As did run-hook-with-args and run-hook-with-args-until-failure | |
2412 | until they were changed in 24.1.) */ | |
a0d76c27 | 2413 | DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success, |
9dbc9081 PJ |
2414 | Srun_hook_with_args_until_success, 1, MANY, 0, |
2415 | doc: /* Run HOOK with the specified arguments ARGS. | |
d393cefb GM |
2416 | HOOK should be a symbol, a hook variable. The value of HOOK |
2417 | may be nil, a function, or a list of functions. Call each | |
2418 | function in order with arguments ARGS, stopping at the first | |
2419 | one that returns non-nil, and return that value. Otherwise (if | |
2420 | all functions return nil, or if there are no functions to call), | |
2421 | return nil. | |
9dbc9081 | 2422 | |
72e85d5d RS |
2423 | Do not use `make-local-variable' to make a hook variable buffer-local. |
2424 | Instead, use `add-hook' and specify t for the LOCAL argument. | |
9dbc9081 | 2425 | usage: (run-hook-with-args-until-success HOOK &rest ARGS) */) |
f66c7cf8 | 2426 | (ptrdiff_t nargs, Lisp_Object *args) |
b0b667cb | 2427 | { |
f6d62986 SM |
2428 | return run_hook_with_args (nargs, args, Ffuncall); |
2429 | } | |
2430 | ||
2431 | static Lisp_Object | |
f66c7cf8 | 2432 | funcall_not (ptrdiff_t nargs, Lisp_Object *args) |
f6d62986 SM |
2433 | { |
2434 | return NILP (Ffuncall (nargs, args)) ? Qt : Qnil; | |
ff936e53 SM |
2435 | } |
2436 | ||
a7ca3326 | 2437 | DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure, |
9dbc9081 PJ |
2438 | Srun_hook_with_args_until_failure, 1, MANY, 0, |
2439 | doc: /* Run HOOK with the specified arguments ARGS. | |
d393cefb GM |
2440 | HOOK should be a symbol, a hook variable. The value of HOOK |
2441 | may be nil, a function, or a list of functions. Call each | |
2442 | function in order with arguments ARGS, stopping at the first | |
2443 | one that returns nil, and return nil. Otherwise (if all functions | |
2444 | return non-nil, or if there are no functions to call), return non-nil | |
2445 | \(do not rely on the precise return value in this case). | |
9dbc9081 | 2446 | |
72e85d5d RS |
2447 | Do not use `make-local-variable' to make a hook variable buffer-local. |
2448 | Instead, use `add-hook' and specify t for the LOCAL argument. | |
9dbc9081 | 2449 | usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */) |
f66c7cf8 | 2450 | (ptrdiff_t nargs, Lisp_Object *args) |
ff936e53 | 2451 | { |
f6d62986 | 2452 | return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil; |
ff936e53 SM |
2453 | } |
2454 | ||
f6d62986 | 2455 | static Lisp_Object |
f66c7cf8 | 2456 | run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args) |
f6d62986 SM |
2457 | { |
2458 | Lisp_Object tmp = args[0], ret; | |
2459 | args[0] = args[1]; | |
2460 | args[1] = tmp; | |
2461 | ret = Ffuncall (nargs, args); | |
2462 | args[1] = args[0]; | |
2463 | args[0] = tmp; | |
2464 | return ret; | |
2465 | } | |
2466 | ||
2467 | DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0, | |
2468 | doc: /* Run HOOK, passing each function through WRAP-FUNCTION. | |
2469 | I.e. instead of calling each function FUN directly with arguments ARGS, | |
2470 | it calls WRAP-FUNCTION with arguments FUN and ARGS. | |
2471 | As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped' | |
2472 | aborts and returns that value. | |
2473 | usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */) | |
f66c7cf8 | 2474 | (ptrdiff_t nargs, Lisp_Object *args) |
f6d62986 SM |
2475 | { |
2476 | return run_hook_with_args (nargs, args, run_hook_wrapped_funcall); | |
2477 | } | |
ff936e53 | 2478 | |
c933ea05 RS |
2479 | /* ARGS[0] should be a hook symbol. |
2480 | Call each of the functions in the hook value, passing each of them | |
2481 | as arguments all the rest of ARGS (all NARGS - 1 elements). | |
f6d62986 | 2482 | FUNCALL specifies how to call each function on the hook. |
c933ea05 RS |
2483 | The caller (or its caller, etc) must gcpro all of ARGS, |
2484 | except that it isn't necessary to gcpro ARGS[0]. */ | |
2485 | ||
f6d62986 | 2486 | Lisp_Object |
f66c7cf8 PE |
2487 | run_hook_with_args (ptrdiff_t nargs, Lisp_Object *args, |
2488 | Lisp_Object (*funcall) (ptrdiff_t nargs, Lisp_Object *args)) | |
ff936e53 | 2489 | { |
f6d62986 | 2490 | Lisp_Object sym, val, ret = Qnil; |
fada05d6 | 2491 | struct gcpro gcpro1, gcpro2, gcpro3; |
b0b667cb | 2492 | |
f029ca5f RS |
2493 | /* If we are dying or still initializing, |
2494 | don't do anything--it would probably crash if we tried. */ | |
2495 | if (NILP (Vrun_hooks)) | |
caff32a7 | 2496 | return Qnil; |
f029ca5f | 2497 | |
b0b667cb | 2498 | sym = args[0]; |
aa681b51 | 2499 | val = find_symbol_value (sym); |
ff936e53 | 2500 | |
b0b667cb | 2501 | if (EQ (val, Qunbound) || NILP (val)) |
ff936e53 | 2502 | return ret; |
dee4ba59 | 2503 | else if (!CONSP (val) || FUNCTIONP (val)) |
b0b667cb KH |
2504 | { |
2505 | args[0] = val; | |
f6d62986 | 2506 | return funcall (nargs, args); |
b0b667cb KH |
2507 | } |
2508 | else | |
2509 | { | |
1faed8ae PE |
2510 | Lisp_Object global_vals = Qnil; |
2511 | GCPRO3 (sym, val, global_vals); | |
cb9d21f8 | 2512 | |
ff936e53 | 2513 | for (; |
f6d62986 | 2514 | CONSP (val) && NILP (ret); |
03699b14 | 2515 | val = XCDR (val)) |
b0b667cb | 2516 | { |
03699b14 | 2517 | if (EQ (XCAR (val), Qt)) |
b0b667cb KH |
2518 | { |
2519 | /* t indicates this hook has a local binding; | |
2520 | it means to run the global binding too. */ | |
1faed8ae PE |
2521 | global_vals = Fdefault_value (sym); |
2522 | if (NILP (global_vals)) continue; | |
b0b667cb | 2523 | |
1faed8ae | 2524 | if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda)) |
b0b667cb | 2525 | { |
1faed8ae | 2526 | args[0] = global_vals; |
f6d62986 | 2527 | ret = funcall (nargs, args); |
8932b1c2 CY |
2528 | } |
2529 | else | |
2530 | { | |
2531 | for (; | |
f6d62986 | 2532 | CONSP (global_vals) && NILP (ret); |
1faed8ae | 2533 | global_vals = XCDR (global_vals)) |
8932b1c2 | 2534 | { |
1faed8ae | 2535 | args[0] = XCAR (global_vals); |
8932b1c2 CY |
2536 | /* In a global value, t should not occur. If it does, we |
2537 | must ignore it to avoid an endless loop. */ | |
2538 | if (!EQ (args[0], Qt)) | |
f6d62986 | 2539 | ret = funcall (nargs, args); |
8932b1c2 | 2540 | } |
b0b667cb KH |
2541 | } |
2542 | } | |
2543 | else | |
2544 | { | |
03699b14 | 2545 | args[0] = XCAR (val); |
f6d62986 | 2546 | ret = funcall (nargs, args); |
b0b667cb KH |
2547 | } |
2548 | } | |
cb9d21f8 RS |
2549 | |
2550 | UNGCPRO; | |
ff936e53 | 2551 | return ret; |
b0b667cb KH |
2552 | } |
2553 | } | |
c933ea05 | 2554 | |
7d48558f RS |
2555 | /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */ |
2556 | ||
2557 | void | |
d3da34e0 | 2558 | run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2) |
7d48558f RS |
2559 | { |
2560 | Lisp_Object temp[3]; | |
2561 | temp[0] = hook; | |
2562 | temp[1] = arg1; | |
2563 | temp[2] = arg2; | |
2564 | ||
2565 | Frun_hook_with_args (3, temp); | |
2566 | } | |
ff936e53 | 2567 | \f |
f6d62986 | 2568 | /* Apply fn to arg. */ |
db9f0278 | 2569 | Lisp_Object |
d3da34e0 | 2570 | apply1 (Lisp_Object fn, Lisp_Object arg) |
db9f0278 | 2571 | { |
a6e3fa71 JB |
2572 | struct gcpro gcpro1; |
2573 | ||
2574 | GCPRO1 (fn); | |
265a9e55 | 2575 | if (NILP (arg)) |
a6e3fa71 JB |
2576 | RETURN_UNGCPRO (Ffuncall (1, &fn)); |
2577 | gcpro1.nvars = 2; | |
db9f0278 JB |
2578 | { |
2579 | Lisp_Object args[2]; | |
2580 | args[0] = fn; | |
2581 | args[1] = arg; | |
a6e3fa71 JB |
2582 | gcpro1.var = args; |
2583 | RETURN_UNGCPRO (Fapply (2, args)); | |
db9f0278 | 2584 | } |
db9f0278 JB |
2585 | } |
2586 | ||
f6d62986 | 2587 | /* Call function fn on no arguments. */ |
db9f0278 | 2588 | Lisp_Object |
d3da34e0 | 2589 | call0 (Lisp_Object fn) |
db9f0278 | 2590 | { |
a6e3fa71 JB |
2591 | struct gcpro gcpro1; |
2592 | ||
2593 | GCPRO1 (fn); | |
2594 | RETURN_UNGCPRO (Ffuncall (1, &fn)); | |
db9f0278 JB |
2595 | } |
2596 | ||
f6d62986 | 2597 | /* Call function fn with 1 argument arg1. */ |
db9f0278 JB |
2598 | /* ARGSUSED */ |
2599 | Lisp_Object | |
d3da34e0 | 2600 | call1 (Lisp_Object fn, Lisp_Object arg1) |
db9f0278 | 2601 | { |
a6e3fa71 | 2602 | struct gcpro gcpro1; |
177c0ea7 | 2603 | Lisp_Object args[2]; |
a6e3fa71 | 2604 | |
db9f0278 | 2605 | args[0] = fn; |
15285f9f | 2606 | args[1] = arg1; |
a6e3fa71 JB |
2607 | GCPRO1 (args[0]); |
2608 | gcpro1.nvars = 2; | |
2609 | RETURN_UNGCPRO (Ffuncall (2, args)); | |
db9f0278 JB |
2610 | } |
2611 | ||
f6d62986 | 2612 | /* Call function fn with 2 arguments arg1, arg2. */ |
db9f0278 JB |
2613 | /* ARGSUSED */ |
2614 | Lisp_Object | |
d3da34e0 | 2615 | call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2) |
db9f0278 | 2616 | { |
a6e3fa71 | 2617 | struct gcpro gcpro1; |
db9f0278 JB |
2618 | Lisp_Object args[3]; |
2619 | args[0] = fn; | |
15285f9f RS |
2620 | args[1] = arg1; |
2621 | args[2] = arg2; | |
a6e3fa71 JB |
2622 | GCPRO1 (args[0]); |
2623 | gcpro1.nvars = 3; | |
2624 | RETURN_UNGCPRO (Ffuncall (3, args)); | |
db9f0278 JB |
2625 | } |
2626 | ||
f6d62986 | 2627 | /* Call function fn with 3 arguments arg1, arg2, arg3. */ |
db9f0278 JB |
2628 | /* ARGSUSED */ |
2629 | Lisp_Object | |
d3da34e0 | 2630 | call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3) |
db9f0278 | 2631 | { |
a6e3fa71 | 2632 | struct gcpro gcpro1; |
db9f0278 JB |
2633 | Lisp_Object args[4]; |
2634 | args[0] = fn; | |
15285f9f RS |
2635 | args[1] = arg1; |
2636 | args[2] = arg2; | |
2637 | args[3] = arg3; | |
a6e3fa71 JB |
2638 | GCPRO1 (args[0]); |
2639 | gcpro1.nvars = 4; | |
2640 | RETURN_UNGCPRO (Ffuncall (4, args)); | |
db9f0278 JB |
2641 | } |
2642 | ||
f6d62986 | 2643 | /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */ |
a5a44b91 JB |
2644 | /* ARGSUSED */ |
2645 | Lisp_Object | |
d3da34e0 JB |
2646 | call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3, |
2647 | Lisp_Object arg4) | |
a5a44b91 JB |
2648 | { |
2649 | struct gcpro gcpro1; | |
a5a44b91 JB |
2650 | Lisp_Object args[5]; |
2651 | args[0] = fn; | |
15285f9f RS |
2652 | args[1] = arg1; |
2653 | args[2] = arg2; | |
2654 | args[3] = arg3; | |
2655 | args[4] = arg4; | |
a5a44b91 JB |
2656 | GCPRO1 (args[0]); |
2657 | gcpro1.nvars = 5; | |
2658 | RETURN_UNGCPRO (Ffuncall (5, args)); | |
a5a44b91 JB |
2659 | } |
2660 | ||
f6d62986 | 2661 | /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */ |
15285f9f RS |
2662 | /* ARGSUSED */ |
2663 | Lisp_Object | |
d3da34e0 JB |
2664 | call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3, |
2665 | Lisp_Object arg4, Lisp_Object arg5) | |
15285f9f RS |
2666 | { |
2667 | struct gcpro gcpro1; | |
15285f9f RS |
2668 | Lisp_Object args[6]; |
2669 | args[0] = fn; | |
2670 | args[1] = arg1; | |
2671 | args[2] = arg2; | |
2672 | args[3] = arg3; | |
2673 | args[4] = arg4; | |
2674 | args[5] = arg5; | |
2675 | GCPRO1 (args[0]); | |
2676 | gcpro1.nvars = 6; | |
2677 | RETURN_UNGCPRO (Ffuncall (6, args)); | |
15285f9f RS |
2678 | } |
2679 | ||
f6d62986 | 2680 | /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */ |
15285f9f RS |
2681 | /* ARGSUSED */ |
2682 | Lisp_Object | |
d3da34e0 JB |
2683 | call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3, |
2684 | Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6) | |
15285f9f RS |
2685 | { |
2686 | struct gcpro gcpro1; | |
15285f9f RS |
2687 | Lisp_Object args[7]; |
2688 | args[0] = fn; | |
2689 | args[1] = arg1; | |
2690 | args[2] = arg2; | |
2691 | args[3] = arg3; | |
2692 | args[4] = arg4; | |
2693 | args[5] = arg5; | |
2694 | args[6] = arg6; | |
2695 | GCPRO1 (args[0]); | |
2696 | gcpro1.nvars = 7; | |
2697 | RETURN_UNGCPRO (Ffuncall (7, args)); | |
15285f9f RS |
2698 | } |
2699 | ||
f6d62986 | 2700 | /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */ |
574c05e2 KK |
2701 | /* ARGSUSED */ |
2702 | Lisp_Object | |
d3da34e0 JB |
2703 | call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3, |
2704 | Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7) | |
574c05e2 KK |
2705 | { |
2706 | struct gcpro gcpro1; | |
574c05e2 KK |
2707 | Lisp_Object args[8]; |
2708 | args[0] = fn; | |
2709 | args[1] = arg1; | |
2710 | args[2] = arg2; | |
2711 | args[3] = arg3; | |
2712 | args[4] = arg4; | |
2713 | args[5] = arg5; | |
2714 | args[6] = arg6; | |
2715 | args[7] = arg7; | |
2716 | GCPRO1 (args[0]); | |
2717 | gcpro1.nvars = 8; | |
2718 | RETURN_UNGCPRO (Ffuncall (8, args)); | |
574c05e2 KK |
2719 | } |
2720 | ||
6c2ef893 RS |
2721 | /* The caller should GCPRO all the elements of ARGS. */ |
2722 | ||
a7ca3326 | 2723 | DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0, |
7200d79c | 2724 | doc: /* Non-nil if OBJECT is a function. */) |
c566235d | 2725 | (Lisp_Object object) |
b9598260 | 2726 | { |
e1f29348 | 2727 | if (FUNCTIONP (object)) |
b9598260 | 2728 | return Qt; |
e1f29348 | 2729 | return Qnil; |
b9598260 SM |
2730 | } |
2731 | ||
a7ca3326 | 2732 | DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0, |
9dbc9081 PJ |
2733 | doc: /* Call first argument as a function, passing remaining arguments to it. |
2734 | Return the value that function returns. | |
2735 | Thus, (funcall 'cons 'x 'y) returns (x . y). | |
2736 | usage: (funcall FUNCTION &rest ARGUMENTS) */) | |
f66c7cf8 | 2737 | (ptrdiff_t nargs, Lisp_Object *args) |
db9f0278 | 2738 | { |
8788120f | 2739 | Lisp_Object fun, original_fun; |
db9f0278 | 2740 | Lisp_Object funcar; |
f66c7cf8 | 2741 | ptrdiff_t numargs = nargs - 1; |
db9f0278 JB |
2742 | Lisp_Object lisp_numargs; |
2743 | Lisp_Object val; | |
db9f0278 | 2744 | register Lisp_Object *internal_args; |
f66c7cf8 | 2745 | ptrdiff_t i; |
db9f0278 JB |
2746 | |
2747 | QUIT; | |
db9f0278 JB |
2748 | |
2749 | if (++lisp_eval_depth > max_lisp_eval_depth) | |
2750 | { | |
2751 | if (max_lisp_eval_depth < 100) | |
2752 | max_lisp_eval_depth = 100; | |
2753 | if (lisp_eval_depth > max_lisp_eval_depth) | |
921baa95 | 2754 | error ("Lisp nesting exceeds `max-lisp-eval-depth'"); |
db9f0278 JB |
2755 | } |
2756 | ||
2f592f95 SM |
2757 | /* This also GCPROs them. */ |
2758 | record_in_backtrace (args[0], &args[1], nargs - 1); | |
db9f0278 | 2759 | |
7abaf5cc SM |
2760 | /* Call GC after setting up the backtrace, so the latter GCPROs the args. */ |
2761 | maybe_gc (); | |
2762 | ||
db9f0278 JB |
2763 | if (debug_on_next_call) |
2764 | do_debug_on_call (Qlambda); | |
2765 | ||
7e63e0c3 | 2766 | check_cons_list (); |
fff3ff9c | 2767 | |
8788120f KS |
2768 | original_fun = args[0]; |
2769 | ||
db9f0278 JB |
2770 | retry: |
2771 | ||
8788120f KS |
2772 | /* Optimize for no indirection. */ |
2773 | fun = original_fun; | |
eadf1faa | 2774 | if (SYMBOLP (fun) && !NILP (fun) |
c644523b | 2775 | && (fun = XSYMBOL (fun)->function, SYMBOLP (fun))) |
8788120f | 2776 | fun = indirect_function (fun); |
db9f0278 | 2777 | |
90165123 | 2778 | if (SUBRP (fun)) |
db9f0278 | 2779 | { |
ef1b0ba7 | 2780 | if (numargs < XSUBR (fun)->min_args |
db9f0278 JB |
2781 | || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs)) |
2782 | { | |
a631e24c | 2783 | XSETFASTINT (lisp_numargs, numargs); |
734d55a2 | 2784 | xsignal2 (Qwrong_number_of_arguments, original_fun, lisp_numargs); |
db9f0278 JB |
2785 | } |
2786 | ||
ef1b0ba7 | 2787 | else if (XSUBR (fun)->max_args == UNEVALLED) |
734d55a2 | 2788 | xsignal1 (Qinvalid_function, original_fun); |
db9f0278 | 2789 | |
ef1b0ba7 SM |
2790 | else if (XSUBR (fun)->max_args == MANY) |
2791 | val = (XSUBR (fun)->function.aMANY) (numargs, args + 1); | |
db9f0278 | 2792 | else |
db9f0278 | 2793 | { |
ef1b0ba7 SM |
2794 | if (XSUBR (fun)->max_args > numargs) |
2795 | { | |
38182d90 PE |
2796 | internal_args = alloca (XSUBR (fun)->max_args |
2797 | * sizeof *internal_args); | |
663e2b3f | 2798 | memcpy (internal_args, args + 1, numargs * word_size); |
ef1b0ba7 SM |
2799 | for (i = numargs; i < XSUBR (fun)->max_args; i++) |
2800 | internal_args[i] = Qnil; | |
2801 | } | |
2802 | else | |
2803 | internal_args = args + 1; | |
2804 | switch (XSUBR (fun)->max_args) | |
2805 | { | |
2806 | case 0: | |
2807 | val = (XSUBR (fun)->function.a0 ()); | |
2808 | break; | |
2809 | case 1: | |
2810 | val = (XSUBR (fun)->function.a1 (internal_args[0])); | |
2811 | break; | |
2812 | case 2: | |
2813 | val = (XSUBR (fun)->function.a2 | |
2814 | (internal_args[0], internal_args[1])); | |
2815 | break; | |
2816 | case 3: | |
2817 | val = (XSUBR (fun)->function.a3 | |
2818 | (internal_args[0], internal_args[1], internal_args[2])); | |
2819 | break; | |
2820 | case 4: | |
2821 | val = (XSUBR (fun)->function.a4 | |
2822 | (internal_args[0], internal_args[1], internal_args[2], | |
2823 | internal_args[3])); | |
2824 | break; | |
2825 | case 5: | |
2826 | val = (XSUBR (fun)->function.a5 | |
2827 | (internal_args[0], internal_args[1], internal_args[2], | |
2828 | internal_args[3], internal_args[4])); | |
2829 | break; | |
2830 | case 6: | |
2831 | val = (XSUBR (fun)->function.a6 | |
2832 | (internal_args[0], internal_args[1], internal_args[2], | |
2833 | internal_args[3], internal_args[4], internal_args[5])); | |
2834 | break; | |
2835 | case 7: | |
2836 | val = (XSUBR (fun)->function.a7 | |
2837 | (internal_args[0], internal_args[1], internal_args[2], | |
2838 | internal_args[3], internal_args[4], internal_args[5], | |
2839 | internal_args[6])); | |
2840 | break; | |
2841 | ||
2842 | case 8: | |
2843 | val = (XSUBR (fun)->function.a8 | |
2844 | (internal_args[0], internal_args[1], internal_args[2], | |
2845 | internal_args[3], internal_args[4], internal_args[5], | |
2846 | internal_args[6], internal_args[7])); | |
2847 | break; | |
2848 | ||
2849 | default: | |
2850 | ||
2851 | /* If a subr takes more than 8 arguments without using MANY | |
2852 | or UNEVALLED, we need to extend this function to support it. | |
2853 | Until this is done, there is no way to call the function. */ | |
1088b922 | 2854 | emacs_abort (); |
ef1b0ba7 | 2855 | } |
db9f0278 JB |
2856 | } |
2857 | } | |
ef1b0ba7 | 2858 | else if (COMPILEDP (fun)) |
db9f0278 JB |
2859 | val = funcall_lambda (fun, numargs, args + 1); |
2860 | else | |
2861 | { | |
eadf1faa | 2862 | if (NILP (fun)) |
734d55a2 | 2863 | xsignal1 (Qvoid_function, original_fun); |
db9f0278 | 2864 | if (!CONSP (fun)) |
734d55a2 KS |
2865 | xsignal1 (Qinvalid_function, original_fun); |
2866 | funcar = XCAR (fun); | |
90165123 | 2867 | if (!SYMBOLP (funcar)) |
734d55a2 | 2868 | xsignal1 (Qinvalid_function, original_fun); |
defb1411 SM |
2869 | if (EQ (funcar, Qlambda) |
2870 | || EQ (funcar, Qclosure)) | |
db9f0278 | 2871 | val = funcall_lambda (fun, numargs, args + 1); |
db9f0278 JB |
2872 | else if (EQ (funcar, Qautoload)) |
2873 | { | |
7abaf5cc | 2874 | Fautoload_do_load (fun, original_fun, Qnil); |
7e63e0c3 | 2875 | check_cons_list (); |
db9f0278 JB |
2876 | goto retry; |
2877 | } | |
2878 | else | |
734d55a2 | 2879 | xsignal1 (Qinvalid_function, original_fun); |
db9f0278 | 2880 | } |
7e63e0c3 | 2881 | check_cons_list (); |
db9f0278 | 2882 | lisp_eval_depth--; |
2f592f95 | 2883 | if (backtrace_debug_on_exit (specpdl_ptr - 1)) |
6c6f1994 | 2884 | val = call_debugger (list2 (Qexit, val)); |
2f592f95 | 2885 | specpdl_ptr--; |
db9f0278 JB |
2886 | return val; |
2887 | } | |
2888 | \f | |
2f7c71a1 | 2889 | static Lisp_Object |
defb1411 | 2890 | apply_lambda (Lisp_Object fun, Lisp_Object args) |
db9f0278 JB |
2891 | { |
2892 | Lisp_Object args_left; | |
d311d28c PE |
2893 | ptrdiff_t i; |
2894 | EMACS_INT numargs; | |
db9f0278 JB |
2895 | register Lisp_Object *arg_vector; |
2896 | struct gcpro gcpro1, gcpro2, gcpro3; | |
db9f0278 | 2897 | register Lisp_Object tem; |
3a7a9129 | 2898 | USE_SAFE_ALLOCA; |
db9f0278 | 2899 | |
f66c7cf8 | 2900 | numargs = XFASTINT (Flength (args)); |
c5101a77 | 2901 | SAFE_ALLOCA_LISP (arg_vector, numargs); |
db9f0278 JB |
2902 | args_left = args; |
2903 | ||
2904 | GCPRO3 (*arg_vector, args_left, fun); | |
2905 | gcpro1.nvars = 0; | |
2906 | ||
c5101a77 | 2907 | for (i = 0; i < numargs; ) |
db9f0278 JB |
2908 | { |
2909 | tem = Fcar (args_left), args_left = Fcdr (args_left); | |
defb1411 | 2910 | tem = eval_sub (tem); |
db9f0278 JB |
2911 | arg_vector[i++] = tem; |
2912 | gcpro1.nvars = i; | |
2913 | } | |
2914 | ||
2915 | UNGCPRO; | |
2916 | ||
2f592f95 SM |
2917 | set_backtrace_args (specpdl_ptr - 1, arg_vector); |
2918 | set_backtrace_nargs (specpdl_ptr - 1, i); | |
c5101a77 | 2919 | tem = funcall_lambda (fun, numargs, arg_vector); |
db9f0278 JB |
2920 | |
2921 | /* Do the debug-on-exit now, while arg_vector still exists. */ | |
2f592f95 SM |
2922 | if (backtrace_debug_on_exit (specpdl_ptr - 1)) |
2923 | { | |
2924 | /* Don't do it again when we return to eval. */ | |
2925 | set_backtrace_debug_on_exit (specpdl_ptr - 1, false); | |
6c6f1994 | 2926 | tem = call_debugger (list2 (Qexit, tem)); |
2f592f95 | 2927 | } |
3a7a9129 | 2928 | SAFE_FREE (); |
db9f0278 JB |
2929 | return tem; |
2930 | } | |
2931 | ||
2932 | /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR | |
2933 | and return the result of evaluation. | |
2934 | FUN must be either a lambda-expression or a compiled-code object. */ | |
2935 | ||
2901f1d1 | 2936 | static Lisp_Object |
f66c7cf8 | 2937 | funcall_lambda (Lisp_Object fun, ptrdiff_t nargs, |
c5101a77 | 2938 | register Lisp_Object *arg_vector) |
db9f0278 | 2939 | { |
defb1411 | 2940 | Lisp_Object val, syms_left, next, lexenv; |
d311d28c | 2941 | ptrdiff_t count = SPECPDL_INDEX (); |
f66c7cf8 | 2942 | ptrdiff_t i; |
1882aa38 | 2943 | bool optional, rest; |
db9f0278 | 2944 | |
90165123 | 2945 | if (CONSP (fun)) |
9ab90667 | 2946 | { |
defb1411 SM |
2947 | if (EQ (XCAR (fun), Qclosure)) |
2948 | { | |
2949 | fun = XCDR (fun); /* Drop `closure'. */ | |
2950 | lexenv = XCAR (fun); | |
23aba0ea | 2951 | CHECK_LIST_CONS (fun, fun); |
defb1411 SM |
2952 | } |
2953 | else | |
2954 | lexenv = Qnil; | |
9ab90667 GM |
2955 | syms_left = XCDR (fun); |
2956 | if (CONSP (syms_left)) | |
2957 | syms_left = XCAR (syms_left); | |
2958 | else | |
734d55a2 | 2959 | xsignal1 (Qinvalid_function, fun); |
9ab90667 | 2960 | } |
90165123 | 2961 | else if (COMPILEDP (fun)) |
defb1411 | 2962 | { |
798cb644 SM |
2963 | syms_left = AREF (fun, COMPILED_ARGLIST); |
2964 | if (INTEGERP (syms_left)) | |
876c194c SM |
2965 | /* A byte-code object with a non-nil `push args' slot means we |
2966 | shouldn't bind any arguments, instead just call the byte-code | |
2967 | interpreter directly; it will push arguments as necessary. | |
2968 | ||
9173deec | 2969 | Byte-code objects with either a non-existent, or a nil value for |
876c194c SM |
2970 | the `push args' slot (the default), have dynamically-bound |
2971 | arguments, and use the argument-binding code below instead (as do | |
2972 | all interpreted functions, even lexically bound ones). */ | |
2973 | { | |
2974 | /* If we have not actually read the bytecode string | |
2975 | and constants vector yet, fetch them from the file. */ | |
2976 | if (CONSP (AREF (fun, COMPILED_BYTECODE))) | |
2977 | Ffetch_bytecode (fun); | |
2978 | return exec_byte_code (AREF (fun, COMPILED_BYTECODE), | |
2979 | AREF (fun, COMPILED_CONSTANTS), | |
2980 | AREF (fun, COMPILED_STACK_DEPTH), | |
798cb644 | 2981 | syms_left, |
876c194c SM |
2982 | nargs, arg_vector); |
2983 | } | |
defb1411 SM |
2984 | lexenv = Qnil; |
2985 | } | |
9ab90667 | 2986 | else |
1088b922 | 2987 | emacs_abort (); |
db9f0278 | 2988 | |
9ab90667 GM |
2989 | i = optional = rest = 0; |
2990 | for (; CONSP (syms_left); syms_left = XCDR (syms_left)) | |
db9f0278 JB |
2991 | { |
2992 | QUIT; | |
177c0ea7 | 2993 | |
9ab90667 | 2994 | next = XCAR (syms_left); |
8788120f | 2995 | if (!SYMBOLP (next)) |
734d55a2 | 2996 | xsignal1 (Qinvalid_function, fun); |
177c0ea7 | 2997 | |
db9f0278 JB |
2998 | if (EQ (next, Qand_rest)) |
2999 | rest = 1; | |
3000 | else if (EQ (next, Qand_optional)) | |
3001 | optional = 1; | |
db9f0278 | 3002 | else |
db9f0278 | 3003 | { |
e610eaca | 3004 | Lisp_Object arg; |
defb1411 SM |
3005 | if (rest) |
3006 | { | |
e610eaca | 3007 | arg = Flist (nargs - i, &arg_vector[i]); |
defb1411 SM |
3008 | i = nargs; |
3009 | } | |
3010 | else if (i < nargs) | |
e610eaca | 3011 | arg = arg_vector[i++]; |
b9598260 SM |
3012 | else if (!optional) |
3013 | xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs)); | |
3014 | else | |
e610eaca | 3015 | arg = Qnil; |
7200d79c | 3016 | |
b9598260 | 3017 | /* Bind the argument. */ |
876c194c | 3018 | if (!NILP (lexenv) && SYMBOLP (next)) |
b9598260 | 3019 | /* Lexically bind NEXT by adding it to the lexenv alist. */ |
e610eaca | 3020 | lexenv = Fcons (Fcons (next, arg), lexenv); |
b9598260 SM |
3021 | else |
3022 | /* Dynamically bind NEXT. */ | |
e610eaca | 3023 | specbind (next, arg); |
db9f0278 | 3024 | } |
db9f0278 JB |
3025 | } |
3026 | ||
9ab90667 | 3027 | if (!NILP (syms_left)) |
734d55a2 | 3028 | xsignal1 (Qinvalid_function, fun); |
9ab90667 | 3029 | else if (i < nargs) |
734d55a2 | 3030 | xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs)); |
db9f0278 | 3031 | |
b9598260 SM |
3032 | if (!EQ (lexenv, Vinternal_interpreter_environment)) |
3033 | /* Instantiate a new lexical environment. */ | |
3034 | specbind (Qinternal_interpreter_environment, lexenv); | |
3035 | ||
90165123 | 3036 | if (CONSP (fun)) |
9ab90667 | 3037 | val = Fprogn (XCDR (XCDR (fun))); |
db9f0278 | 3038 | else |
ca248607 RS |
3039 | { |
3040 | /* If we have not actually read the bytecode string | |
3041 | and constants vector yet, fetch them from the file. */ | |
845975f5 | 3042 | if (CONSP (AREF (fun, COMPILED_BYTECODE))) |
661c7d6e | 3043 | Ffetch_bytecode (fun); |
b9598260 SM |
3044 | val = exec_byte_code (AREF (fun, COMPILED_BYTECODE), |
3045 | AREF (fun, COMPILED_CONSTANTS), | |
3046 | AREF (fun, COMPILED_STACK_DEPTH), | |
3047 | Qnil, 0, 0); | |
ca248607 | 3048 | } |
177c0ea7 | 3049 | |
db9f0278 JB |
3050 | return unbind_to (count, val); |
3051 | } | |
661c7d6e KH |
3052 | |
3053 | DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode, | |
9dbc9081 PJ |
3054 | 1, 1, 0, |
3055 | doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */) | |
5842a27b | 3056 | (Lisp_Object object) |
661c7d6e KH |
3057 | { |
3058 | Lisp_Object tem; | |
3059 | ||
845975f5 | 3060 | if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE))) |
661c7d6e | 3061 | { |
845975f5 | 3062 | tem = read_doc_string (AREF (object, COMPILED_BYTECODE)); |
5bbdb090 | 3063 | if (!CONSP (tem)) |
845975f5 SM |
3064 | { |
3065 | tem = AREF (object, COMPILED_BYTECODE); | |
3066 | if (CONSP (tem) && STRINGP (XCAR (tem))) | |
d5db4077 | 3067 | error ("Invalid byte code in %s", SDATA (XCAR (tem))); |
845975f5 SM |
3068 | else |
3069 | error ("Invalid byte code"); | |
3070 | } | |
3ae565b3 SM |
3071 | ASET (object, COMPILED_BYTECODE, XCAR (tem)); |
3072 | ASET (object, COMPILED_CONSTANTS, XCDR (tem)); | |
661c7d6e KH |
3073 | } |
3074 | return object; | |
3075 | } | |
db9f0278 | 3076 | \f |
2f592f95 SM |
3077 | /* Return true if SYMBOL currently has a let-binding |
3078 | which was made in the buffer that is now current. */ | |
3079 | ||
3080 | bool | |
3081 | let_shadows_buffer_binding_p (struct Lisp_Symbol *symbol) | |
db9f0278 | 3082 | { |
9349e5f7 | 3083 | union specbinding *p; |
2f592f95 SM |
3084 | Lisp_Object buf = Fcurrent_buffer (); |
3085 | ||
3086 | for (p = specpdl_ptr; p > specpdl; ) | |
3087 | if ((--p)->kind > SPECPDL_LET) | |
3088 | { | |
3089 | struct Lisp_Symbol *let_bound_symbol = XSYMBOL (specpdl_symbol (p)); | |
3090 | eassert (let_bound_symbol->redirect != SYMBOL_VARALIAS); | |
3091 | if (symbol == let_bound_symbol | |
3092 | && EQ (specpdl_where (p), buf)) | |
3093 | return 1; | |
3094 | } | |
3095 | ||
3096 | return 0; | |
3097 | } | |
3098 | ||
3099 | bool | |
3100 | let_shadows_global_binding_p (Lisp_Object symbol) | |
3101 | { | |
9349e5f7 | 3102 | union specbinding *p; |
2f592f95 SM |
3103 | |
3104 | for (p = specpdl_ptr; p > specpdl; ) | |
3105 | if ((--p)->kind >= SPECPDL_LET && EQ (specpdl_symbol (p), symbol)) | |
3106 | return 1; | |
3107 | ||
3108 | return 0; | |
db9f0278 JB |
3109 | } |
3110 | ||
3ec7babc | 3111 | /* `specpdl_ptr' describes which variable is |
4e2db1fe | 3112 | let-bound, so it can be properly undone when we unbind_to. |
3ec7babc SM |
3113 | It can be either a plain SPECPDL_LET or a SPECPDL_LET_LOCAL/DEFAULT. |
3114 | - SYMBOL is the variable being bound. Note that it should not be | |
4e2db1fe SM |
3115 | aliased (i.e. when let-binding V1 that's aliased to V2, we want |
3116 | to record V2 here). | |
3ec7babc SM |
3117 | - WHERE tells us in which buffer the binding took place. |
3118 | This is used for SPECPDL_LET_LOCAL bindings (i.e. bindings to a | |
3119 | buffer-local variable) as well as for SPECPDL_LET_DEFAULT bindings, | |
3120 | i.e. bindings to the default value of a variable which can be | |
3121 | buffer-local. */ | |
4e2db1fe | 3122 | |
db9f0278 | 3123 | void |
d3da34e0 | 3124 | specbind (Lisp_Object symbol, Lisp_Object value) |
db9f0278 | 3125 | { |
ce5b453a SM |
3126 | struct Lisp_Symbol *sym; |
3127 | ||
b7826503 | 3128 | CHECK_SYMBOL (symbol); |
ce5b453a | 3129 | sym = XSYMBOL (symbol); |
719177b3 | 3130 | |
ce5b453a SM |
3131 | start: |
3132 | switch (sym->redirect) | |
719177b3 | 3133 | { |
ce5b453a SM |
3134 | case SYMBOL_VARALIAS: |
3135 | sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start; | |
3136 | case SYMBOL_PLAINVAL: | |
bb8e180f AS |
3137 | /* The most common case is that of a non-constant symbol with a |
3138 | trivial value. Make that as fast as we can. */ | |
9349e5f7 PE |
3139 | specpdl_ptr->let.kind = SPECPDL_LET; |
3140 | specpdl_ptr->let.symbol = symbol; | |
3141 | specpdl_ptr->let.old_value = SYMBOL_VAL (sym); | |
5e301d76 | 3142 | grow_specpdl (); |
bb8e180f AS |
3143 | if (!sym->constant) |
3144 | SET_SYMBOL_VAL (sym, value); | |
3145 | else | |
3146 | set_internal (symbol, value, Qnil, 1); | |
3147 | break; | |
4e2db1fe SM |
3148 | case SYMBOL_LOCALIZED: |
3149 | if (SYMBOL_BLV (sym)->frame_local) | |
3150 | error ("Frame-local vars cannot be let-bound"); | |
3151 | case SYMBOL_FORWARDED: | |
ce5b453a SM |
3152 | { |
3153 | Lisp_Object ovalue = find_symbol_value (symbol); | |
9349e5f7 PE |
3154 | specpdl_ptr->let.kind = SPECPDL_LET_LOCAL; |
3155 | specpdl_ptr->let.symbol = symbol; | |
3156 | specpdl_ptr->let.old_value = ovalue; | |
3157 | specpdl_ptr->let.where = Fcurrent_buffer (); | |
ce5b453a SM |
3158 | |
3159 | eassert (sym->redirect != SYMBOL_LOCALIZED | |
2f592f95 | 3160 | || (EQ (SYMBOL_BLV (sym)->where, Fcurrent_buffer ()))); |
ce5b453a | 3161 | |
2f592f95 SM |
3162 | if (sym->redirect == SYMBOL_LOCALIZED) |
3163 | { | |
3164 | if (!blv_found (SYMBOL_BLV (sym))) | |
9349e5f7 | 3165 | specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT; |
2f592f95 SM |
3166 | } |
3167 | else if (BUFFER_OBJFWDP (SYMBOL_FWD (sym))) | |
ce5b453a | 3168 | { |
ce5b453a SM |
3169 | /* If SYMBOL is a per-buffer variable which doesn't have a |
3170 | buffer-local value here, make the `let' change the global | |
3171 | value by changing the value of SYMBOL in all buffers not | |
3172 | having their own value. This is consistent with what | |
3173 | happens with other buffer-local variables. */ | |
2f592f95 | 3174 | if (NILP (Flocal_variable_p (symbol, Qnil))) |
ce5b453a | 3175 | { |
9349e5f7 | 3176 | specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT; |
5e301d76 | 3177 | grow_specpdl (); |
ce5b453a SM |
3178 | Fset_default (symbol, value); |
3179 | return; | |
3180 | } | |
3181 | } | |
3182 | else | |
9349e5f7 | 3183 | specpdl_ptr->let.kind = SPECPDL_LET; |
ce5b453a | 3184 | |
5e301d76 | 3185 | grow_specpdl (); |
94b612ad | 3186 | set_internal (symbol, value, Qnil, 1); |
ce5b453a SM |
3187 | break; |
3188 | } | |
1088b922 | 3189 | default: emacs_abort (); |
9ab90667 | 3190 | } |
db9f0278 JB |
3191 | } |
3192 | ||
f4b1eb36 PE |
3193 | /* Push unwind-protect entries of various types. */ |
3194 | ||
db9f0278 | 3195 | void |
27e498e6 | 3196 | record_unwind_protect (void (*function) (Lisp_Object), Lisp_Object arg) |
db9f0278 | 3197 | { |
9349e5f7 PE |
3198 | specpdl_ptr->unwind.kind = SPECPDL_UNWIND; |
3199 | specpdl_ptr->unwind.func = function; | |
3200 | specpdl_ptr->unwind.arg = arg; | |
5e301d76 | 3201 | grow_specpdl (); |
db9f0278 JB |
3202 | } |
3203 | ||
27e498e6 PE |
3204 | void |
3205 | record_unwind_protect_ptr (void (*function) (void *), void *arg) | |
3206 | { | |
3207 | specpdl_ptr->unwind_ptr.kind = SPECPDL_UNWIND_PTR; | |
3208 | specpdl_ptr->unwind_ptr.func = function; | |
3209 | specpdl_ptr->unwind_ptr.arg = arg; | |
3210 | grow_specpdl (); | |
3211 | } | |
3212 | ||
3213 | void | |
3214 | record_unwind_protect_int (void (*function) (int), int arg) | |
3215 | { | |
3216 | specpdl_ptr->unwind_int.kind = SPECPDL_UNWIND_INT; | |
3217 | specpdl_ptr->unwind_int.func = function; | |
3218 | specpdl_ptr->unwind_int.arg = arg; | |
3219 | grow_specpdl (); | |
3220 | } | |
3221 | ||
3222 | void | |
3223 | record_unwind_protect_void (void (*function) (void)) | |
3224 | { | |
3225 | specpdl_ptr->unwind_void.kind = SPECPDL_UNWIND_VOID; | |
3226 | specpdl_ptr->unwind_void.func = function; | |
3227 | grow_specpdl (); | |
3228 | } | |
3229 | ||
a0931322 PE |
3230 | static void |
3231 | do_nothing (void) | |
3232 | {} | |
3233 | ||
f4b1eb36 PE |
3234 | /* Push an unwind-protect entry that does nothing, so that |
3235 | set_unwind_protect_ptr can overwrite it later. */ | |
3236 | ||
3237 | void | |
3238 | record_unwind_protect_nothing (void) | |
3239 | { | |
3240 | record_unwind_protect_void (do_nothing); | |
3241 | } | |
3242 | ||
3243 | /* Clear the unwind-protect entry COUNT, so that it does nothing. | |
3244 | It need not be at the top of the stack. */ | |
3245 | ||
a0931322 PE |
3246 | void |
3247 | clear_unwind_protect (ptrdiff_t count) | |
3248 | { | |
3249 | union specbinding *p = specpdl + count; | |
3250 | p->unwind_void.kind = SPECPDL_UNWIND_VOID; | |
3251 | p->unwind_void.func = do_nothing; | |
3252 | } | |
3253 | ||
f4b1eb36 PE |
3254 | /* Set the unwind-protect entry COUNT so that it invokes FUNC (ARG). |
3255 | It need not be at the top of the stack. Discard the entry's | |
3256 | previous value without invoking it. */ | |
3257 | ||
94fcd171 PE |
3258 | void |
3259 | set_unwind_protect (ptrdiff_t count, void (*func) (Lisp_Object), | |
3260 | Lisp_Object arg) | |
3261 | { | |
3262 | union specbinding *p = specpdl + count; | |
3263 | p->unwind.kind = SPECPDL_UNWIND; | |
3264 | p->unwind.func = func; | |
3265 | p->unwind.arg = arg; | |
3266 | } | |
3267 | ||
a0931322 PE |
3268 | void |
3269 | set_unwind_protect_ptr (ptrdiff_t count, void (*func) (void *), void *arg) | |
3270 | { | |
3271 | union specbinding *p = specpdl + count; | |
3272 | p->unwind_ptr.kind = SPECPDL_UNWIND_PTR; | |
3273 | p->unwind_ptr.func = func; | |
3274 | p->unwind_ptr.arg = arg; | |
3275 | } | |
3276 | ||
f4b1eb36 PE |
3277 | /* Pop and execute entries from the unwind-protect stack until the |
3278 | depth COUNT is reached. Return VALUE. */ | |
3279 | ||
db9f0278 | 3280 | Lisp_Object |
d311d28c | 3281 | unbind_to (ptrdiff_t count, Lisp_Object value) |
db9f0278 | 3282 | { |
5a073f50 KS |
3283 | Lisp_Object quitf = Vquit_flag; |
3284 | struct gcpro gcpro1, gcpro2; | |
db9f0278 | 3285 | |
5a073f50 | 3286 | GCPRO2 (value, quitf); |
db9f0278 JB |
3287 | Vquit_flag = Qnil; |
3288 | ||
3289 | while (specpdl_ptr != specpdl + count) | |
3290 | { | |
9349e5f7 PE |
3291 | /* Decrement specpdl_ptr before we do the work to unbind it, so |
3292 | that an error in unbinding won't try to unbind the same entry | |
3293 | again. Take care to copy any parts of the binding needed | |
3294 | before invoking any code that can make more bindings. */ | |
eb700b82 | 3295 | |
9349e5f7 | 3296 | specpdl_ptr--; |
611a8f8c | 3297 | |
9349e5f7 | 3298 | switch (specpdl_ptr->kind) |
719177b3 | 3299 | { |
2f592f95 | 3300 | case SPECPDL_UNWIND: |
27e498e6 PE |
3301 | specpdl_ptr->unwind.func (specpdl_ptr->unwind.arg); |
3302 | break; | |
3303 | case SPECPDL_UNWIND_PTR: | |
3304 | specpdl_ptr->unwind_ptr.func (specpdl_ptr->unwind_ptr.arg); | |
3305 | break; | |
3306 | case SPECPDL_UNWIND_INT: | |
3307 | specpdl_ptr->unwind_int.func (specpdl_ptr->unwind_int.arg); | |
3308 | break; | |
3309 | case SPECPDL_UNWIND_VOID: | |
3310 | specpdl_ptr->unwind_void.func (); | |
2f592f95 | 3311 | break; |
56ea7291 SM |
3312 | case SPECPDL_BACKTRACE: |
3313 | break; | |
2f592f95 | 3314 | case SPECPDL_LET: |
a104f656 SM |
3315 | { /* If variable has a trivial value (no forwarding), we can |
3316 | just set it. No need to check for constant symbols here, | |
3317 | since that was already done by specbind. */ | |
3318 | struct Lisp_Symbol *sym = XSYMBOL (specpdl_symbol (specpdl_ptr)); | |
3319 | if (sym->redirect == SYMBOL_PLAINVAL) | |
3320 | { | |
3321 | SET_SYMBOL_VAL (sym, specpdl_old_value (specpdl_ptr)); | |
3322 | break; | |
3323 | } | |
3324 | else | |
3325 | { /* FALLTHROUGH!! | |
3326 | NOTE: we only ever come here if make_local_foo was used for | |
3327 | the first time on this var within this let. */ | |
3328 | } | |
3329 | } | |
56ea7291 SM |
3330 | case SPECPDL_LET_DEFAULT: |
3331 | Fset_default (specpdl_symbol (specpdl_ptr), | |
3332 | specpdl_old_value (specpdl_ptr)); | |
2f592f95 SM |
3333 | break; |
3334 | case SPECPDL_LET_LOCAL: | |
56ea7291 | 3335 | { |
9349e5f7 PE |
3336 | Lisp_Object symbol = specpdl_symbol (specpdl_ptr); |
3337 | Lisp_Object where = specpdl_where (specpdl_ptr); | |
3338 | Lisp_Object old_value = specpdl_old_value (specpdl_ptr); | |
2f592f95 SM |
3339 | eassert (BUFFERP (where)); |
3340 | ||
2f592f95 SM |
3341 | /* If this was a local binding, reset the value in the appropriate |
3342 | buffer, but only if that buffer's binding still exists. */ | |
56ea7291 | 3343 | if (!NILP (Flocal_variable_p (symbol, where))) |
9349e5f7 | 3344 | set_internal (symbol, old_value, where, 1); |
2f592f95 SM |
3345 | } |
3346 | break; | |
719177b3 | 3347 | } |
db9f0278 | 3348 | } |
177c0ea7 | 3349 | |
5a073f50 KS |
3350 | if (NILP (Vquit_flag) && !NILP (quitf)) |
3351 | Vquit_flag = quitf; | |
db9f0278 JB |
3352 | |
3353 | UNGCPRO; | |
db9f0278 JB |
3354 | return value; |
3355 | } | |
b9598260 | 3356 | |
4a330052 | 3357 | DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0, |
b9598260 SM |
3358 | doc: /* Return non-nil if SYMBOL's global binding has been declared special. |
3359 | A special variable is one that will be bound dynamically, even in a | |
3360 | context where binding is lexical by default. */) | |
c566235d | 3361 | (Lisp_Object symbol) |
b9598260 SM |
3362 | { |
3363 | CHECK_SYMBOL (symbol); | |
3364 | return XSYMBOL (symbol)->declared_special ? Qt : Qnil; | |
3365 | } | |
3366 | ||
db9f0278 | 3367 | \f |
db9f0278 | 3368 | DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0, |
9dbc9081 PJ |
3369 | doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG. |
3370 | The debugger is entered when that frame exits, if the flag is non-nil. */) | |
5842a27b | 3371 | (Lisp_Object level, Lisp_Object flag) |
db9f0278 | 3372 | { |
9349e5f7 | 3373 | union specbinding *pdl = backtrace_top (); |
d311d28c | 3374 | register EMACS_INT i; |
db9f0278 | 3375 | |
b7826503 | 3376 | CHECK_NUMBER (level); |
db9f0278 | 3377 | |
2f592f95 SM |
3378 | for (i = 0; backtrace_p (pdl) && i < XINT (level); i++) |
3379 | pdl = backtrace_next (pdl); | |
db9f0278 | 3380 | |
2f592f95 SM |
3381 | if (backtrace_p (pdl)) |
3382 | set_backtrace_debug_on_exit (pdl, !NILP (flag)); | |
db9f0278 JB |
3383 | |
3384 | return flag; | |
3385 | } | |
3386 | ||
3387 | DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "", | |
9dbc9081 PJ |
3388 | doc: /* Print a trace of Lisp function calls currently active. |
3389 | Output stream used is value of `standard-output'. */) | |
5842a27b | 3390 | (void) |
db9f0278 | 3391 | { |
9349e5f7 | 3392 | union specbinding *pdl = backtrace_top (); |
db9f0278 | 3393 | Lisp_Object tem; |
d4b6d95d | 3394 | Lisp_Object old_print_level = Vprint_level; |
db9f0278 | 3395 | |
d4b6d95d LMI |
3396 | if (NILP (Vprint_level)) |
3397 | XSETFASTINT (Vprint_level, 8); | |
db9f0278 | 3398 | |
2f592f95 | 3399 | while (backtrace_p (pdl)) |
db9f0278 | 3400 | { |
2f592f95 SM |
3401 | write_string (backtrace_debug_on_exit (pdl) ? "* " : " ", 2); |
3402 | if (backtrace_nargs (pdl) == UNEVALLED) | |
db9f0278 | 3403 | { |
2f592f95 SM |
3404 | Fprin1 (Fcons (backtrace_function (pdl), *backtrace_args (pdl)), |
3405 | Qnil); | |
b6703b02 | 3406 | write_string ("\n", -1); |
db9f0278 JB |
3407 | } |
3408 | else | |
3409 | { | |
2f592f95 | 3410 | tem = backtrace_function (pdl); |
f6d62986 | 3411 | Fprin1 (tem, Qnil); /* This can QUIT. */ |
db9f0278 | 3412 | write_string ("(", -1); |
2f592f95 SM |
3413 | { |
3414 | ptrdiff_t i; | |
3415 | for (i = 0; i < backtrace_nargs (pdl); i++) | |
3416 | { | |
3417 | if (i) write_string (" ", -1); | |
3418 | Fprin1 (backtrace_args (pdl)[i], Qnil); | |
3419 | } | |
3420 | } | |
b6703b02 | 3421 | write_string (")\n", -1); |
db9f0278 | 3422 | } |
2f592f95 | 3423 | pdl = backtrace_next (pdl); |
db9f0278 JB |
3424 | } |
3425 | ||
d4b6d95d | 3426 | Vprint_level = old_print_level; |
db9f0278 JB |
3427 | return Qnil; |
3428 | } | |
3429 | ||
d5a7a9d9 | 3430 | static union specbinding * |
56ea7291 SM |
3431 | get_backtrace_frame (Lisp_Object nframes, Lisp_Object base) |
3432 | { | |
3433 | union specbinding *pdl = backtrace_top (); | |
3434 | register EMACS_INT i; | |
3435 | ||
3436 | CHECK_NATNUM (nframes); | |
3437 | ||
3438 | if (!NILP (base)) | |
3439 | { /* Skip up to `base'. */ | |
3440 | base = Findirect_function (base, Qt); | |
3441 | while (backtrace_p (pdl) | |
3442 | && !EQ (base, Findirect_function (backtrace_function (pdl), Qt))) | |
3443 | pdl = backtrace_next (pdl); | |
3444 | } | |
3445 | ||
3446 | /* Find the frame requested. */ | |
3447 | for (i = XFASTINT (nframes); i > 0 && backtrace_p (pdl); i--) | |
3448 | pdl = backtrace_next (pdl); | |
3449 | ||
3450 | return pdl; | |
3451 | } | |
3452 | ||
3453 | DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 2, NULL, | |
9dbc9081 PJ |
3454 | doc: /* Return the function and arguments NFRAMES up from current execution point. |
3455 | If that frame has not evaluated the arguments yet (or is a special form), | |
3456 | the value is (nil FUNCTION ARG-FORMS...). | |
3457 | If that frame has evaluated its arguments and called its function already, | |
3458 | the value is (t FUNCTION ARG-VALUES...). | |
3459 | A &rest arg is represented as the tail of the list ARG-VALUES. | |
3460 | FUNCTION is whatever was supplied as car of evaluated list, | |
3461 | or a lambda expression for macro calls. | |
56ea7291 SM |
3462 | If NFRAMES is more than the number of frames, the value is nil. |
3463 | If BASE is non-nil, it should be a function and NFRAMES counts from its | |
3464 | nearest activation frame. */) | |
3465 | (Lisp_Object nframes, Lisp_Object base) | |
db9f0278 | 3466 | { |
56ea7291 | 3467 | union specbinding *pdl = get_backtrace_frame (nframes, base); |
db9f0278 | 3468 | |
2f592f95 | 3469 | if (!backtrace_p (pdl)) |
db9f0278 | 3470 | return Qnil; |
2f592f95 SM |
3471 | if (backtrace_nargs (pdl) == UNEVALLED) |
3472 | return Fcons (Qnil, | |
3473 | Fcons (backtrace_function (pdl), *backtrace_args (pdl))); | |
db9f0278 JB |
3474 | else |
3475 | { | |
2f592f95 | 3476 | Lisp_Object tem = Flist (backtrace_nargs (pdl), backtrace_args (pdl)); |
db9f0278 | 3477 | |
2f592f95 | 3478 | return Fcons (Qt, Fcons (backtrace_function (pdl), tem)); |
db9f0278 JB |
3479 | } |
3480 | } | |
a2ff3819 | 3481 | |
56ea7291 SM |
3482 | /* For backtrace-eval, we want to temporarily unwind the last few elements of |
3483 | the specpdl stack, and then rewind them. We store the pre-unwind values | |
3484 | directly in the pre-existing specpdl elements (i.e. we swap the current | |
3485 | value and the old value stored in the specpdl), kind of like the inplace | |
3486 | pointer-reversal trick. As it turns out, the rewind does the same as the | |
94fea300 | 3487 | unwind, except it starts from the other end of the specpdl stack, so we use |
56ea7291 | 3488 | the same function for both unwind and rewind. */ |
d5a7a9d9 | 3489 | static void |
56ea7291 SM |
3490 | backtrace_eval_unrewind (int distance) |
3491 | { | |
3492 | union specbinding *tmp = specpdl_ptr; | |
3493 | int step = -1; | |
3494 | if (distance < 0) | |
3495 | { /* It's a rewind rather than unwind. */ | |
3496 | tmp += distance - 1; | |
3497 | step = 1; | |
3498 | distance = -distance; | |
3499 | } | |
3500 | ||
3501 | for (; distance > 0; distance--) | |
3502 | { | |
3503 | tmp += step; | |
3504 | /* */ | |
3505 | switch (tmp->kind) | |
3506 | { | |
3507 | /* FIXME: Ideally we'd like to "temporarily unwind" (some of) those | |
3508 | unwind_protect, but the problem is that we don't know how to | |
3509 | rewind them afterwards. */ | |
3510 | case SPECPDL_UNWIND: | |
3511 | case SPECPDL_UNWIND_PTR: | |
3512 | case SPECPDL_UNWIND_INT: | |
3513 | case SPECPDL_UNWIND_VOID: | |
3514 | case SPECPDL_BACKTRACE: | |
3515 | break; | |
3516 | case SPECPDL_LET: | |
a104f656 SM |
3517 | { /* If variable has a trivial value (no forwarding), we can |
3518 | just set it. No need to check for constant symbols here, | |
3519 | since that was already done by specbind. */ | |
3520 | struct Lisp_Symbol *sym = XSYMBOL (specpdl_symbol (tmp)); | |
3521 | if (sym->redirect == SYMBOL_PLAINVAL) | |
3522 | { | |
3523 | Lisp_Object old_value = specpdl_old_value (tmp); | |
3524 | set_specpdl_old_value (tmp, SYMBOL_VAL (sym)); | |
3525 | SET_SYMBOL_VAL (sym, old_value); | |
3526 | break; | |
3527 | } | |
3528 | else | |
3529 | { /* FALLTHROUGH!! | |
3530 | NOTE: we only ever come here if make_local_foo was used for | |
3531 | the first time on this var within this let. */ | |
3532 | } | |
3533 | } | |
56ea7291 SM |
3534 | case SPECPDL_LET_DEFAULT: |
3535 | { | |
3536 | Lisp_Object sym = specpdl_symbol (tmp); | |
3537 | Lisp_Object old_value = specpdl_old_value (tmp); | |
3538 | set_specpdl_old_value (tmp, Fdefault_value (sym)); | |
3539 | Fset_default (sym, old_value); | |
3540 | } | |
3541 | break; | |
3542 | case SPECPDL_LET_LOCAL: | |
3543 | { | |
3544 | Lisp_Object symbol = specpdl_symbol (tmp); | |
3545 | Lisp_Object where = specpdl_where (tmp); | |
3546 | Lisp_Object old_value = specpdl_old_value (tmp); | |
3547 | eassert (BUFFERP (where)); | |
3548 | ||
3549 | /* If this was a local binding, reset the value in the appropriate | |
3550 | buffer, but only if that buffer's binding still exists. */ | |
3551 | if (!NILP (Flocal_variable_p (symbol, where))) | |
3552 | { | |
3553 | set_specpdl_old_value | |
3554 | (tmp, Fbuffer_local_value (symbol, where)); | |
3555 | set_internal (symbol, old_value, where, 1); | |
3556 | } | |
3557 | } | |
3558 | break; | |
3559 | } | |
3560 | } | |
3561 | } | |
3562 | ||
3563 | DEFUN ("backtrace-eval", Fbacktrace_eval, Sbacktrace_eval, 2, 3, NULL, | |
3564 | doc: /* Evaluate EXP in the context of some activation frame. | |
3565 | NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */) | |
3566 | (Lisp_Object exp, Lisp_Object nframes, Lisp_Object base) | |
3567 | { | |
3568 | union specbinding *pdl = get_backtrace_frame (nframes, base); | |
3569 | ptrdiff_t count = SPECPDL_INDEX (); | |
3570 | ptrdiff_t distance = specpdl_ptr - pdl; | |
3571 | eassert (distance >= 0); | |
3572 | ||
3573 | if (!backtrace_p (pdl)) | |
3574 | error ("Activation frame not found!"); | |
3575 | ||
3576 | backtrace_eval_unrewind (distance); | |
3577 | record_unwind_protect_int (backtrace_eval_unrewind, -distance); | |
3578 | ||
3579 | /* Use eval_sub rather than Feval since the main motivation behind | |
3580 | backtrace-eval is to be able to get/set the value of lexical variables | |
3581 | from the debugger. */ | |
3582 | return unbind_to (count, eval_sub (exp)); | |
3583 | } | |
f345395c HE |
3584 | |
3585 | DEFUN ("backtrace--locals", Fbacktrace__locals, Sbacktrace__locals, 1, 2, NULL, | |
3586 | doc: /* Return names and values of local variables of a stack frame. | |
3587 | NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */) | |
3588 | (Lisp_Object nframes, Lisp_Object base) | |
3589 | { | |
3590 | union specbinding *frame = get_backtrace_frame (nframes, base); | |
3591 | union specbinding *prevframe | |
3592 | = get_backtrace_frame (make_number (XFASTINT (nframes) - 1), base); | |
3593 | ptrdiff_t distance = specpdl_ptr - frame; | |
3594 | Lisp_Object result = Qnil; | |
3595 | eassert (distance >= 0); | |
3596 | ||
3597 | if (!backtrace_p (prevframe)) | |
3598 | error ("Activation frame not found!"); | |
3599 | if (!backtrace_p (frame)) | |
3600 | error ("Activation frame not found!"); | |
3601 | ||
3602 | /* The specpdl entries normally contain the symbol being bound along with its | |
3603 | `old_value', so it can be restored. The new value to which it is bound is | |
3604 | available in one of two places: either in the current value of the | |
09a7c0fe | 3605 | variable (if it hasn't been rebound yet) or in the `old_value' slot of the |
f345395c HE |
3606 | next specpdl entry for it. |
3607 | `backtrace_eval_unrewind' happens to swap the role of `old_value' | |
3608 | and "new value", so we abuse it here, to fetch the new value. | |
3609 | It's ugly (we'd rather not modify global data) and a bit inefficient, | |
3610 | but it does the job for now. */ | |
3611 | backtrace_eval_unrewind (distance); | |
3612 | ||
3613 | /* Grab values. */ | |
3614 | { | |
3615 | union specbinding *tmp = prevframe; | |
3616 | for (; tmp > frame; tmp--) | |
3617 | { | |
3618 | switch (tmp->kind) | |
3619 | { | |
3620 | case SPECPDL_LET: | |
3621 | case SPECPDL_LET_DEFAULT: | |
3622 | case SPECPDL_LET_LOCAL: | |
3623 | { | |
3624 | Lisp_Object sym = specpdl_symbol (tmp); | |
3625 | Lisp_Object val = specpdl_old_value (tmp); | |
3626 | if (EQ (sym, Qinternal_interpreter_environment)) | |
3627 | { | |
3628 | Lisp_Object env = val; | |
3629 | for (; CONSP (env); env = XCDR (env)) | |
3630 | { | |
3631 | Lisp_Object binding = XCAR (env); | |
3632 | if (CONSP (binding)) | |
3633 | result = Fcons (Fcons (XCAR (binding), | |
3634 | XCDR (binding)), | |
3635 | result); | |
3636 | } | |
3637 | } | |
3638 | else | |
3639 | result = Fcons (Fcons (sym, val), result); | |
3640 | } | |
3641 | } | |
3642 | } | |
3643 | } | |
3644 | ||
3645 | /* Restore values from specpdl to original place. */ | |
3646 | backtrace_eval_unrewind (-distance); | |
3647 | ||
3648 | return result; | |
3649 | } | |
3650 | ||
db9f0278 | 3651 | \f |
4ce0541e | 3652 | void |
2f592f95 | 3653 | mark_specpdl (void) |
4ce0541e | 3654 | { |
9349e5f7 | 3655 | union specbinding *pdl; |
2f592f95 | 3656 | for (pdl = specpdl; pdl != specpdl_ptr; pdl++) |
4ce0541e | 3657 | { |
2f592f95 SM |
3658 | switch (pdl->kind) |
3659 | { | |
3660 | case SPECPDL_UNWIND: | |
3661 | mark_object (specpdl_arg (pdl)); | |
3662 | break; | |
9349e5f7 | 3663 | |
2f592f95 SM |
3664 | case SPECPDL_BACKTRACE: |
3665 | { | |
3666 | ptrdiff_t nargs = backtrace_nargs (pdl); | |
3667 | mark_object (backtrace_function (pdl)); | |
3668 | if (nargs == UNEVALLED) | |
3669 | nargs = 1; | |
3670 | while (nargs--) | |
3671 | mark_object (backtrace_args (pdl)[nargs]); | |
3672 | } | |
3673 | break; | |
9349e5f7 | 3674 | |
2f592f95 SM |
3675 | case SPECPDL_LET_DEFAULT: |
3676 | case SPECPDL_LET_LOCAL: | |
3677 | mark_object (specpdl_where (pdl)); | |
9349e5f7 | 3678 | /* Fall through. */ |
2f592f95 SM |
3679 | case SPECPDL_LET: |
3680 | mark_object (specpdl_symbol (pdl)); | |
3681 | mark_object (specpdl_old_value (pdl)); | |
9349e5f7 | 3682 | break; |
2f592f95 SM |
3683 | } |
3684 | } | |
3685 | } | |
3686 | ||
3687 | void | |
3688 | get_backtrace (Lisp_Object array) | |
3689 | { | |
9349e5f7 | 3690 | union specbinding *pdl = backtrace_next (backtrace_top ()); |
2f592f95 | 3691 | ptrdiff_t i = 0, asize = ASIZE (array); |
4ce0541e | 3692 | |
2f592f95 SM |
3693 | /* Copy the backtrace contents into working memory. */ |
3694 | for (; i < asize; i++) | |
3695 | { | |
3696 | if (backtrace_p (pdl)) | |
3697 | { | |
3698 | ASET (array, i, backtrace_function (pdl)); | |
3699 | pdl = backtrace_next (pdl); | |
3700 | } | |
4ce0541e | 3701 | else |
2f592f95 | 3702 | ASET (array, i, Qnil); |
4ce0541e SM |
3703 | } |
3704 | } | |
2f592f95 SM |
3705 | |
3706 | Lisp_Object backtrace_top_function (void) | |
3707 | { | |
9349e5f7 | 3708 | union specbinding *pdl = backtrace_top (); |
2f592f95 SM |
3709 | return (backtrace_p (pdl) ? backtrace_function (pdl) : Qnil); |
3710 | } | |
4ce0541e | 3711 | |
dfcf069d | 3712 | void |
d3da34e0 | 3713 | syms_of_eval (void) |
db9f0278 | 3714 | { |
29208e82 | 3715 | DEFVAR_INT ("max-specpdl-size", max_specpdl_size, |
fb7ada5f | 3716 | doc: /* Limit on number of Lisp variable bindings and `unwind-protect's. |
9f5903bb | 3717 | If Lisp code tries to increase the total number past this amount, |
2520dc0c RS |
3718 | an error is signaled. |
3719 | You can safely use a value considerably larger than the default value, | |
3720 | if that proves inconveniently small. However, if you increase it too far, | |
575593db DA |
3721 | Emacs could run out of memory trying to make the stack bigger. |
3722 | Note that this limit may be silently increased by the debugger | |
3723 | if `debug-on-error' or `debug-on-quit' is set. */); | |
db9f0278 | 3724 | |
29208e82 | 3725 | DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth, |
fb7ada5f | 3726 | doc: /* Limit on depth in `eval', `apply' and `funcall' before error. |
2520dc0c RS |
3727 | |
3728 | This limit serves to catch infinite recursions for you before they cause | |
9dbc9081 PJ |
3729 | actual stack overflow in C, which would be fatal for Emacs. |
3730 | You can safely make it considerably larger than its default value, | |
2520dc0c RS |
3731 | if that proves inconveniently small. However, if you increase it too far, |
3732 | Emacs could overflow the real C stack, and crash. */); | |
db9f0278 | 3733 | |
29208e82 | 3734 | DEFVAR_LISP ("quit-flag", Vquit_flag, |
9dbc9081 | 3735 | doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil. |
42ed718e RS |
3736 | If the value is t, that means do an ordinary quit. |
3737 | If the value equals `throw-on-input', that means quit by throwing | |
3738 | to the tag specified in `throw-on-input'; it's for handling `while-no-input'. | |
3739 | Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit', | |
3740 | but `inhibit-quit' non-nil prevents anything from taking notice of that. */); | |
db9f0278 JB |
3741 | Vquit_flag = Qnil; |
3742 | ||
29208e82 | 3743 | DEFVAR_LISP ("inhibit-quit", Vinhibit_quit, |
9dbc9081 PJ |
3744 | doc: /* Non-nil inhibits C-g quitting from happening immediately. |
3745 | Note that `quit-flag' will still be set by typing C-g, | |
3746 | so a quit will be signaled as soon as `inhibit-quit' is nil. | |
3747 | To prevent this happening, set `quit-flag' to nil | |
3748 | before making `inhibit-quit' nil. */); | |
db9f0278 JB |
3749 | Vinhibit_quit = Qnil; |
3750 | ||
cd3520a4 JB |
3751 | DEFSYM (Qinhibit_quit, "inhibit-quit"); |
3752 | DEFSYM (Qautoload, "autoload"); | |
45b82ad0 | 3753 | DEFSYM (Qinhibit_debugger, "inhibit-debugger"); |
cd3520a4 JB |
3754 | DEFSYM (Qmacro, "macro"); |
3755 | DEFSYM (Qdeclare, "declare"); | |
177c0ea7 | 3756 | |
db9f0278 JB |
3757 | /* Note that the process handling also uses Qexit, but we don't want |
3758 | to staticpro it twice, so we just do it here. */ | |
cd3520a4 | 3759 | DEFSYM (Qexit, "exit"); |
b9598260 | 3760 | |
cd3520a4 JB |
3761 | DEFSYM (Qinteractive, "interactive"); |
3762 | DEFSYM (Qcommandp, "commandp"); | |
cd3520a4 JB |
3763 | DEFSYM (Qand_rest, "&rest"); |
3764 | DEFSYM (Qand_optional, "&optional"); | |
3765 | DEFSYM (Qclosure, "closure"); | |
3766 | DEFSYM (Qdebug, "debug"); | |
f01cbfdd | 3767 | |
45b82ad0 SM |
3768 | DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger, |
3769 | doc: /* Non-nil means never enter the debugger. | |
3770 | Normally set while the debugger is already active, to avoid recursive | |
3771 | invocations. */); | |
3772 | Vinhibit_debugger = Qnil; | |
3773 | ||
29208e82 | 3774 | DEFVAR_LISP ("debug-on-error", Vdebug_on_error, |
fb7ada5f | 3775 | doc: /* Non-nil means enter debugger if an error is signaled. |
9dbc9081 PJ |
3776 | Does not apply to errors handled by `condition-case' or those |
3777 | matched by `debug-ignored-errors'. | |
3778 | If the value is a list, an error only means to enter the debugger | |
3779 | if one of its condition symbols appears in the list. | |
3780 | When you evaluate an expression interactively, this variable | |
3781 | is temporarily non-nil if `eval-expression-debug-on-error' is non-nil. | |
fbbdcf2f | 3782 | The command `toggle-debug-on-error' toggles this. |
45b82ad0 | 3783 | See also the variable `debug-on-quit' and `inhibit-debugger'. */); |
128c0f66 | 3784 | Vdebug_on_error = Qnil; |
db9f0278 | 3785 | |
29208e82 | 3786 | DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors, |
fb7ada5f | 3787 | doc: /* List of errors for which the debugger should not be called. |
9dbc9081 PJ |
3788 | Each element may be a condition-name or a regexp that matches error messages. |
3789 | If any element applies to a given error, that error skips the debugger | |
3790 | and just returns to top level. | |
3791 | This overrides the variable `debug-on-error'. | |
3792 | It does not apply to errors handled by `condition-case'. */); | |
fc950e09 KH |
3793 | Vdebug_ignored_errors = Qnil; |
3794 | ||
29208e82 | 3795 | DEFVAR_BOOL ("debug-on-quit", debug_on_quit, |
fb7ada5f | 3796 | doc: /* Non-nil means enter debugger if quit is signaled (C-g, for example). |
82fc29a1 | 3797 | Does not apply if quit is handled by a `condition-case'. */); |
db9f0278 JB |
3798 | debug_on_quit = 0; |
3799 | ||
29208e82 | 3800 | DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call, |
9dbc9081 | 3801 | doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */); |
db9f0278 | 3802 | |
29208e82 | 3803 | DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue, |
9dbc9081 PJ |
3804 | doc: /* Non-nil means debugger may continue execution. |
3805 | This is nil when the debugger is called under circumstances where it | |
3806 | might not be safe to continue. */); | |
dac204bc | 3807 | debugger_may_continue = 1; |
556d7314 | 3808 | |
29208e82 | 3809 | DEFVAR_LISP ("debugger", Vdebugger, |
9dbc9081 PJ |
3810 | doc: /* Function to call to invoke debugger. |
3811 | If due to frame exit, args are `exit' and the value being returned; | |
3812 | this function's value will be returned instead of that. | |
3813 | If due to error, args are `error' and a list of the args to `signal'. | |
3814 | If due to `apply' or `funcall' entry, one arg, `lambda'. | |
3815 | If due to `eval' entry, one arg, t. */); | |
db9f0278 JB |
3816 | Vdebugger = Qnil; |
3817 | ||
29208e82 | 3818 | DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function, |
9dbc9081 PJ |
3819 | doc: /* If non-nil, this is a function for `signal' to call. |
3820 | It receives the same arguments that `signal' was given. | |
3821 | The Edebug package uses this to regain control. */); | |
61ede770 RS |
3822 | Vsignal_hook_function = Qnil; |
3823 | ||
29208e82 | 3824 | DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal, |
fb7ada5f | 3825 | doc: /* Non-nil means call the debugger regardless of condition handlers. |
9dbc9081 PJ |
3826 | Note that `debug-on-error', `debug-on-quit' and friends |
3827 | still determine whether to handle the particular condition. */); | |
57a6e758 | 3828 | Vdebug_on_signal = Qnil; |
61ede770 | 3829 | |
b38b1ec0 | 3830 | /* When lexical binding is being used, |
61b108cc | 3831 | Vinternal_interpreter_environment is non-nil, and contains an alist |
b38b1ec0 SM |
3832 | of lexically-bound variable, or (t), indicating an empty |
3833 | environment. The lisp name of this variable would be | |
3834 | `internal-interpreter-environment' if it weren't hidden. | |
3835 | Every element of this list can be either a cons (VAR . VAL) | |
3836 | specifying a lexical binding, or a single symbol VAR indicating | |
3837 | that this variable should use dynamic scoping. */ | |
61b108cc SM |
3838 | DEFSYM (Qinternal_interpreter_environment, |
3839 | "internal-interpreter-environment"); | |
b38b1ec0 SM |
3840 | DEFVAR_LISP ("internal-interpreter-environment", |
3841 | Vinternal_interpreter_environment, | |
b9598260 SM |
3842 | doc: /* If non-nil, the current lexical environment of the lisp interpreter. |
3843 | When lexical binding is not being used, this variable is nil. | |
3844 | A value of `(t)' indicates an empty environment, otherwise it is an | |
3845 | alist of active lexical bindings. */); | |
3846 | Vinternal_interpreter_environment = Qnil; | |
c80e3b4a | 3847 | /* Don't export this variable to Elisp, so no one can mess with it |
b38b1ec0 SM |
3848 | (Just imagine if someone makes it buffer-local). */ |
3849 | Funintern (Qinternal_interpreter_environment, Qnil); | |
b9598260 | 3850 | |
cd3520a4 | 3851 | DEFSYM (Vrun_hooks, "run-hooks"); |
db9f0278 JB |
3852 | |
3853 | staticpro (&Vautoload_queue); | |
3854 | Vautoload_queue = Qnil; | |
a2ff3819 GM |
3855 | staticpro (&Vsignaling_function); |
3856 | Vsignaling_function = Qnil; | |
db9f0278 | 3857 | |
d1f55f16 CY |
3858 | inhibit_lisp_code = Qnil; |
3859 | ||
db9f0278 JB |
3860 | defsubr (&Sor); |
3861 | defsubr (&Sand); | |
3862 | defsubr (&Sif); | |
3863 | defsubr (&Scond); | |
3864 | defsubr (&Sprogn); | |
3865 | defsubr (&Sprog1); | |
3866 | defsubr (&Sprog2); | |
3867 | defsubr (&Ssetq); | |
3868 | defsubr (&Squote); | |
3869 | defsubr (&Sfunction); | |
a104f656 SM |
3870 | defsubr (&Sdefault_toplevel_value); |
3871 | defsubr (&Sset_default_toplevel_value); | |
db9f0278 | 3872 | defsubr (&Sdefvar); |
19cebf5a | 3873 | defsubr (&Sdefvaralias); |
db9f0278 | 3874 | defsubr (&Sdefconst); |
513749ee | 3875 | defsubr (&Smake_var_non_special); |
db9f0278 JB |
3876 | defsubr (&Slet); |
3877 | defsubr (&SletX); | |
3878 | defsubr (&Swhile); | |
3879 | defsubr (&Smacroexpand); | |
3880 | defsubr (&Scatch); | |
3881 | defsubr (&Sthrow); | |
3882 | defsubr (&Sunwind_protect); | |
3883 | defsubr (&Scondition_case); | |
3884 | defsubr (&Ssignal); | |
db9f0278 JB |
3885 | defsubr (&Scommandp); |
3886 | defsubr (&Sautoload); | |
7abaf5cc | 3887 | defsubr (&Sautoload_do_load); |
db9f0278 JB |
3888 | defsubr (&Seval); |
3889 | defsubr (&Sapply); | |
3890 | defsubr (&Sfuncall); | |
ff936e53 SM |
3891 | defsubr (&Srun_hooks); |
3892 | defsubr (&Srun_hook_with_args); | |
3893 | defsubr (&Srun_hook_with_args_until_success); | |
3894 | defsubr (&Srun_hook_with_args_until_failure); | |
f6d62986 | 3895 | defsubr (&Srun_hook_wrapped); |
661c7d6e | 3896 | defsubr (&Sfetch_bytecode); |
db9f0278 JB |
3897 | defsubr (&Sbacktrace_debug); |
3898 | defsubr (&Sbacktrace); | |
3899 | defsubr (&Sbacktrace_frame); | |
56ea7291 | 3900 | defsubr (&Sbacktrace_eval); |
f345395c | 3901 | defsubr (&Sbacktrace__locals); |
4a330052 | 3902 | defsubr (&Sspecial_variable_p); |
b9598260 | 3903 | defsubr (&Sfunctionp); |
db9f0278 | 3904 | } |