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