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