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