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