Commit | Line | Data |
---|---|---|
db9f0278 | 1 | /* Evaluator for GNU Emacs Lisp interpreter. |
ab422c4d PE |
2 | Copyright (C) 1985-1987, 1993-1995, 1999-2013 Free Software |
3 | 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. | |
408 | If no clause succeeds, cond returns nil. | |
409 | If a clause has one element, as in (CONDITION), | |
410 | CONDITION's value if non-nil is returned from the cond-form. | |
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 | ||
1075 | /* Set up a catch, then call C function FUNC on argument ARG. | |
1076 | FUNC should return a Lisp_Object. | |
2f592f95 | 1077 | This is how catches are done from within C code. */ |
db9f0278 JB |
1078 | |
1079 | Lisp_Object | |
d3da34e0 | 1080 | internal_catch (Lisp_Object tag, Lisp_Object (*func) (Lisp_Object), Lisp_Object arg) |
db9f0278 JB |
1081 | { |
1082 | /* This structure is made part of the chain `catchlist'. */ | |
adf2aa61 | 1083 | struct handler *c; |
db9f0278 JB |
1084 | |
1085 | /* Fill in the components of c, and put it on the list. */ | |
adf2aa61 | 1086 | PUSH_HANDLER (c, tag, CATCHER); |
db9f0278 JB |
1087 | |
1088 | /* Call FUNC. */ | |
adf2aa61 SM |
1089 | if (! sys_setjmp (c->jmp)) |
1090 | { | |
1091 | Lisp_Object val = (*func) (arg); | |
1092 | eassert (handlerlist == c); | |
1093 | handlerlist = c->next; | |
1094 | return val; | |
1095 | } | |
1096 | else | |
1097 | { /* Throw works by a longjmp that comes right here. */ | |
1098 | Lisp_Object val = handlerlist->val; | |
1099 | eassert (handlerlist == c); | |
1100 | handlerlist = handlerlist->next; | |
1101 | return val; | |
1102 | } | |
db9f0278 JB |
1103 | } |
1104 | ||
ba410f40 JB |
1105 | /* Unwind the specbind, catch, and handler stacks back to CATCH, and |
1106 | jump to that CATCH, returning VALUE as the value of that catch. | |
db9f0278 | 1107 | |
4d7e6e51 | 1108 | This is the guts of Fthrow and Fsignal; they differ only in the way |
ba410f40 JB |
1109 | they choose the catch tag to throw to. A catch tag for a |
1110 | condition-case form has a TAG of Qnil. | |
db9f0278 | 1111 | |
ba410f40 JB |
1112 | Before each catch is discarded, unbind all special bindings and |
1113 | execute all unwind-protect clauses made above that catch. Unwind | |
1114 | the handler stack as we go, so that the proper handlers are in | |
1115 | effect for each unwind-protect clause we run. At the end, restore | |
1116 | some static info saved in CATCH, and longjmp to the location | |
4d7e6e51 | 1117 | specified there. |
ba410f40 JB |
1118 | |
1119 | This is used for correct unwinding in Fthrow and Fsignal. */ | |
db9f0278 | 1120 | |
845ca893 | 1121 | static _Noreturn void |
adf2aa61 | 1122 | unwind_to_catch (struct handler *catch, Lisp_Object value) |
db9f0278 | 1123 | { |
1882aa38 | 1124 | bool last_time; |
db9f0278 | 1125 | |
ba410f40 JB |
1126 | /* Save the value in the tag. */ |
1127 | catch->val = value; | |
1128 | ||
0b31741c | 1129 | /* Restore certain special C variables. */ |
1cdc3155 | 1130 | set_poll_suppress_count (catch->poll_suppress_count); |
4d7e6e51 | 1131 | unblock_input_to (catch->interrupt_input_blocked); |
69bbd6bd | 1132 | immediate_quit = 0; |
82da7701 | 1133 | |
db9f0278 JB |
1134 | do |
1135 | { | |
82da7701 | 1136 | /* Unwind the specpdl stack, and then restore the proper set of |
bb8e180f | 1137 | handlers. */ |
adf2aa61 SM |
1138 | unbind_to (handlerlist->pdlcount, Qnil); |
1139 | last_time = handlerlist == catch; | |
1140 | if (! last_time) | |
1141 | handlerlist = handlerlist->next; | |
db9f0278 JB |
1142 | } |
1143 | while (! last_time); | |
1144 | ||
adf2aa61 SM |
1145 | eassert (handlerlist == catch); |
1146 | ||
bcf28080 | 1147 | byte_stack_list = catch->byte_stack; |
db9f0278 | 1148 | gcprolist = catch->gcpro; |
15934ffa | 1149 | #ifdef DEBUG_GCPRO |
d8e2b5ba | 1150 | gcpro_level = gcprolist ? gcprolist->level + 1 : 0; |
15934ffa | 1151 | #endif |
db9f0278 | 1152 | lisp_eval_depth = catch->lisp_eval_depth; |
177c0ea7 | 1153 | |
0328b6de | 1154 | sys_longjmp (catch->jmp, 1); |
db9f0278 JB |
1155 | } |
1156 | ||
a7ca3326 | 1157 | DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0, |
9dbc9081 PJ |
1158 | doc: /* Throw to the catch for TAG and return VALUE from it. |
1159 | Both TAG and VALUE are evalled. */) | |
5842a27b | 1160 | (register Lisp_Object tag, Lisp_Object value) |
db9f0278 | 1161 | { |
adf2aa61 | 1162 | struct handler *c; |
db9f0278 | 1163 | |
8788120f | 1164 | if (!NILP (tag)) |
adf2aa61 | 1165 | for (c = handlerlist; c; c = c->next) |
8788120f | 1166 | { |
adf2aa61 | 1167 | if (c->type == CATCHER && EQ (c->tag_or_ch, tag)) |
8788120f KS |
1168 | unwind_to_catch (c, value); |
1169 | } | |
734d55a2 | 1170 | xsignal2 (Qno_catch, tag, value); |
db9f0278 JB |
1171 | } |
1172 | ||
1173 | ||
1174 | DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0, | |
9dbc9081 PJ |
1175 | doc: /* Do BODYFORM, protecting with UNWINDFORMS. |
1176 | If BODYFORM completes normally, its value is returned | |
1177 | after executing the UNWINDFORMS. | |
1178 | If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway. | |
7a25dc6d | 1179 | usage: (unwind-protect BODYFORM UNWINDFORMS...) */) |
5842a27b | 1180 | (Lisp_Object args) |
db9f0278 JB |
1181 | { |
1182 | Lisp_Object val; | |
d311d28c | 1183 | ptrdiff_t count = SPECPDL_INDEX (); |
db9f0278 | 1184 | |
16b0520a PE |
1185 | record_unwind_protect (unwind_body, XCDR (args)); |
1186 | val = eval_sub (XCAR (args)); | |
177c0ea7 | 1187 | return unbind_to (count, val); |
db9f0278 JB |
1188 | } |
1189 | \f | |
db9f0278 | 1190 | DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0, |
9dbc9081 | 1191 | doc: /* Regain control when an error is signaled. |
1b1acc13 | 1192 | Executes BODYFORM and returns its value if no error happens. |
9dbc9081 PJ |
1193 | Each element of HANDLERS looks like (CONDITION-NAME BODY...) |
1194 | where the BODY is made of Lisp expressions. | |
1195 | ||
1196 | A handler is applicable to an error | |
1197 | if CONDITION-NAME is one of the error's condition names. | |
1198 | If an error happens, the first applicable handler is run. | |
1199 | ||
024a2d76 CY |
1200 | The car of a handler may be a list of condition names instead of a |
1201 | single condition name; then it handles all of them. If the special | |
1202 | condition name `debug' is present in this list, it allows another | |
1203 | condition in the list to run the debugger if `debug-on-error' and the | |
1204 | other usual mechanisms says it should (otherwise, `condition-case' | |
1205 | suppresses the debugger). | |
9dbc9081 | 1206 | |
c997bb25 RS |
1207 | When a handler handles an error, control returns to the `condition-case' |
1208 | and it executes the handler's BODY... | |
d0acbbaf | 1209 | with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error. |
bb8e180f | 1210 | \(If VAR is nil, the handler can't access that information.) |
c997bb25 RS |
1211 | Then the value of the last BODY form is returned from the `condition-case' |
1212 | expression. | |
9dbc9081 | 1213 | |
9dbc9081 | 1214 | See also the function `signal' for more info. |
2b47b74d | 1215 | usage: (condition-case VAR BODYFORM &rest HANDLERS) */) |
bb8e180f | 1216 | (Lisp_Object args) |
db9f0278 | 1217 | { |
16b0520a PE |
1218 | Lisp_Object var = XCAR (args); |
1219 | Lisp_Object bodyform = XCAR (XCDR (args)); | |
1220 | Lisp_Object handlers = XCDR (XCDR (args)); | |
ee830945 RS |
1221 | |
1222 | return internal_lisp_condition_case (var, bodyform, handlers); | |
1223 | } | |
1224 | ||
1225 | /* Like Fcondition_case, but the args are separate | |
1226 | rather than passed in a list. Used by Fbyte_code. */ | |
1227 | ||
1228 | Lisp_Object | |
d3da34e0 JB |
1229 | internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform, |
1230 | Lisp_Object handlers) | |
ee830945 RS |
1231 | { |
1232 | Lisp_Object val; | |
adf2aa61 SM |
1233 | struct handler *c; |
1234 | struct handler *oldhandlerlist = handlerlist; | |
1235 | int clausenb = 0; | |
ee830945 | 1236 | |
b7826503 | 1237 | CHECK_SYMBOL (var); |
82da7701 | 1238 | |
2b47b74d | 1239 | for (val = handlers; CONSP (val); val = XCDR (val)) |
82da7701 | 1240 | { |
adf2aa61 SM |
1241 | Lisp_Object tem = XCAR (val); |
1242 | clausenb++; | |
5f96776a RS |
1243 | if (! (NILP (tem) |
1244 | || (CONSP (tem) | |
03699b14 KR |
1245 | && (SYMBOLP (XCAR (tem)) |
1246 | || CONSP (XCAR (tem)))))) | |
e6c3da20 EZ |
1247 | error ("Invalid condition handler: %s", |
1248 | SDATA (Fprin1_to_string (tem, Qt))); | |
82da7701 | 1249 | } |
db9f0278 | 1250 | |
adf2aa61 SM |
1251 | { /* The first clause is the one that should be checked first, so it should |
1252 | be added to handlerlist last. So we build in `clauses' a table that | |
1253 | contains `handlers' but in reverse order. */ | |
1254 | Lisp_Object *clauses = alloca (clausenb * sizeof (Lisp_Object *)); | |
1255 | int i = clausenb; | |
1256 | for (val = handlers; CONSP (val); val = XCDR (val)) | |
1257 | clauses[--i] = XCAR (val); | |
1258 | for (i = 0; i < clausenb; i++) | |
1259 | { | |
1260 | Lisp_Object clause = clauses[i]; | |
1261 | Lisp_Object condition = XCAR (clause); | |
1262 | if (!CONSP (condition)) | |
1263 | condition = Fcons (condition, Qnil); | |
1264 | PUSH_HANDLER (c, condition, CONDITION_CASE); | |
1265 | if (sys_setjmp (c->jmp)) | |
1266 | { | |
1267 | ptrdiff_t count = SPECPDL_INDEX (); | |
1268 | Lisp_Object val = handlerlist->val; | |
1269 | Lisp_Object *chosen_clause = clauses; | |
1270 | for (c = handlerlist->next; c != oldhandlerlist; c = c->next) | |
1271 | chosen_clause++; | |
1272 | handlerlist = oldhandlerlist; | |
1273 | if (!NILP (var)) | |
1274 | { | |
1275 | if (!NILP (Vinternal_interpreter_environment)) | |
1276 | specbind (Qinternal_interpreter_environment, | |
1277 | Fcons (Fcons (var, val), | |
1278 | Vinternal_interpreter_environment)); | |
1279 | else | |
1280 | specbind (var, val); | |
1281 | } | |
1282 | val = Fprogn (XCDR (*chosen_clause)); | |
1283 | /* Note that this just undoes the binding of var; whoever | |
1284 | longjumped to us unwound the stack to c.pdlcount before | |
1285 | throwing. */ | |
1286 | if (!NILP (var)) | |
1287 | unbind_to (count, Qnil); | |
1288 | return val; | |
1289 | } | |
1290 | } | |
db9f0278 | 1291 | } |
db9f0278 | 1292 | |
defb1411 | 1293 | val = eval_sub (bodyform); |
adf2aa61 | 1294 | handlerlist = oldhandlerlist; |
db9f0278 JB |
1295 | return val; |
1296 | } | |
1297 | ||
f029ca5f RS |
1298 | /* Call the function BFUN with no arguments, catching errors within it |
1299 | according to HANDLERS. If there is an error, call HFUN with | |
1300 | one argument which is the data that describes the error: | |
1301 | (SIGNALNAME . DATA) | |
1302 | ||
1303 | HANDLERS can be a list of conditions to catch. | |
1304 | If HANDLERS is Qt, catch all errors. | |
1305 | If HANDLERS is Qerror, catch all errors | |
1306 | but allow the debugger to run if that is enabled. */ | |
1307 | ||
db9f0278 | 1308 | Lisp_Object |
d3da34e0 JB |
1309 | internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers, |
1310 | Lisp_Object (*hfun) (Lisp_Object)) | |
db9f0278 JB |
1311 | { |
1312 | Lisp_Object val; | |
adf2aa61 SM |
1313 | struct handler *c; |
1314 | ||
1315 | PUSH_HANDLER (c, handlers, CONDITION_CASE); | |
1316 | if (sys_setjmp (c->jmp)) | |
db9f0278 | 1317 | { |
adf2aa61 SM |
1318 | Lisp_Object val = handlerlist->val; |
1319 | eassert (handlerlist == c); | |
1320 | handlerlist = handlerlist->next; | |
1321 | return (*hfun) (val); | |
db9f0278 | 1322 | } |
db9f0278 JB |
1323 | |
1324 | val = (*bfun) (); | |
adf2aa61 SM |
1325 | eassert (handlerlist == c); |
1326 | handlerlist = c->next; | |
db9f0278 JB |
1327 | return val; |
1328 | } | |
1329 | ||
2659a09f | 1330 | /* Like internal_condition_case but call BFUN with ARG as its argument. */ |
f029ca5f | 1331 | |
d227775c | 1332 | Lisp_Object |
d3da34e0 JB |
1333 | internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg, |
1334 | Lisp_Object handlers, Lisp_Object (*hfun) (Lisp_Object)) | |
d227775c RS |
1335 | { |
1336 | Lisp_Object val; | |
adf2aa61 SM |
1337 | struct handler *c; |
1338 | ||
1339 | PUSH_HANDLER (c, handlers, CONDITION_CASE); | |
1340 | if (sys_setjmp (c->jmp)) | |
d227775c | 1341 | { |
adf2aa61 SM |
1342 | Lisp_Object val = handlerlist->val; |
1343 | eassert (handlerlist == c); | |
1344 | handlerlist = handlerlist->next; | |
1345 | return (*hfun) (val); | |
d227775c | 1346 | } |
d227775c RS |
1347 | |
1348 | val = (*bfun) (arg); | |
adf2aa61 SM |
1349 | eassert (handlerlist == c); |
1350 | handlerlist = c->next; | |
d227775c RS |
1351 | return val; |
1352 | } | |
10b29d41 | 1353 | |
53967e09 CY |
1354 | /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as |
1355 | its arguments. */ | |
1356 | ||
1357 | Lisp_Object | |
178f2507 SM |
1358 | internal_condition_case_2 (Lisp_Object (*bfun) (Lisp_Object, Lisp_Object), |
1359 | Lisp_Object arg1, | |
1360 | Lisp_Object arg2, | |
1361 | Lisp_Object handlers, | |
1362 | Lisp_Object (*hfun) (Lisp_Object)) | |
53967e09 CY |
1363 | { |
1364 | Lisp_Object val; | |
adf2aa61 SM |
1365 | struct handler *c; |
1366 | ||
1367 | PUSH_HANDLER (c, handlers, CONDITION_CASE); | |
1368 | if (sys_setjmp (c->jmp)) | |
53967e09 | 1369 | { |
adf2aa61 SM |
1370 | Lisp_Object val = handlerlist->val; |
1371 | eassert (handlerlist == c); | |
1372 | handlerlist = handlerlist->next; | |
1373 | return (*hfun) (val); | |
53967e09 | 1374 | } |
53967e09 CY |
1375 | |
1376 | val = (*bfun) (arg1, arg2); | |
adf2aa61 SM |
1377 | eassert (handlerlist == c); |
1378 | handlerlist = c->next; | |
53967e09 CY |
1379 | return val; |
1380 | } | |
10b29d41 | 1381 | |
2659a09f | 1382 | /* Like internal_condition_case but call BFUN with NARGS as first, |
10b29d41 GM |
1383 | and ARGS as second argument. */ |
1384 | ||
1385 | Lisp_Object | |
f66c7cf8 PE |
1386 | internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *), |
1387 | ptrdiff_t nargs, | |
178f2507 SM |
1388 | Lisp_Object *args, |
1389 | Lisp_Object handlers, | |
cc92c454 SM |
1390 | Lisp_Object (*hfun) (Lisp_Object err, |
1391 | ptrdiff_t nargs, | |
1392 | Lisp_Object *args)) | |
10b29d41 GM |
1393 | { |
1394 | Lisp_Object val; | |
adf2aa61 SM |
1395 | struct handler *c; |
1396 | ||
1397 | PUSH_HANDLER (c, handlers, CONDITION_CASE); | |
1398 | if (sys_setjmp (c->jmp)) | |
10b29d41 | 1399 | { |
adf2aa61 SM |
1400 | Lisp_Object val = handlerlist->val; |
1401 | eassert (handlerlist == c); | |
1402 | handlerlist = handlerlist->next; | |
1403 | return (*hfun) (val, nargs, args); | |
10b29d41 | 1404 | } |
10b29d41 GM |
1405 | |
1406 | val = (*bfun) (nargs, args); | |
adf2aa61 SM |
1407 | eassert (handlerlist == c); |
1408 | handlerlist = c->next; | |
10b29d41 GM |
1409 | return val; |
1410 | } | |
1411 | ||
d227775c | 1412 | \f |
7d47b580 | 1413 | static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object); |
1882aa38 PE |
1414 | static bool maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, |
1415 | Lisp_Object data); | |
db9f0278 | 1416 | |
6d5eb5b0 SM |
1417 | void |
1418 | process_quit_flag (void) | |
1419 | { | |
1420 | Lisp_Object flag = Vquit_flag; | |
1421 | Vquit_flag = Qnil; | |
1422 | if (EQ (flag, Qkill_emacs)) | |
1423 | Fkill_emacs (Qnil); | |
1424 | if (EQ (Vthrow_on_input, flag)) | |
1425 | Fthrow (Vthrow_on_input, Qt); | |
1426 | Fsignal (Qquit, Qnil); | |
1427 | } | |
1428 | ||
a7ca3326 | 1429 | DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0, |
9dbc9081 PJ |
1430 | doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA. |
1431 | This function does not return. | |
1432 | ||
1433 | An error symbol is a symbol with an `error-conditions' property | |
1434 | that is a list of condition names. | |
1435 | A handler for any of those names will get to handle this signal. | |
1436 | The symbol `error' should normally be one of them. | |
1437 | ||
1438 | DATA should be a list. Its elements are printed as part of the error message. | |
3297ec22 LT |
1439 | See Info anchor `(elisp)Definition of signal' for some details on how this |
1440 | error message is constructed. | |
9dbc9081 PJ |
1441 | If the signal is handled, DATA is made available to the handler. |
1442 | See also the function `condition-case'. */) | |
5842a27b | 1443 | (Lisp_Object error_symbol, Lisp_Object data) |
db9f0278 | 1444 | { |
bfa8ca43 | 1445 | /* When memory is full, ERROR-SYMBOL is nil, |
26631f2b RS |
1446 | and DATA is (REAL-ERROR-SYMBOL . REAL-DATA). |
1447 | That is a special case--don't do this in other situations. */ | |
db9f0278 | 1448 | Lisp_Object conditions; |
c11d3d17 | 1449 | Lisp_Object string; |
e7f7fbaa SM |
1450 | Lisp_Object real_error_symbol |
1451 | = (NILP (error_symbol) ? Fcar (data) : error_symbol); | |
1452 | register Lisp_Object clause = Qnil; | |
1453 | struct handler *h; | |
db9f0278 | 1454 | |
0caaedb1 | 1455 | immediate_quit = 0; |
d063129f | 1456 | abort_on_gc = 0; |
db9f0278 | 1457 | if (gc_in_progress || waiting_for_input) |
1088b922 | 1458 | emacs_abort (); |
db9f0278 | 1459 | |
26631f2b RS |
1460 | #if 0 /* rms: I don't know why this was here, |
1461 | but it is surely wrong for an error that is handled. */ | |
d148e14d | 1462 | #ifdef HAVE_WINDOW_SYSTEM |
df6c90d8 GM |
1463 | if (display_hourglass_p) |
1464 | cancel_hourglass (); | |
48f8dfa3 | 1465 | #endif |
177c0ea7 | 1466 | #endif |
48f8dfa3 | 1467 | |
61ede770 | 1468 | /* This hook is used by edebug. */ |
26631f2b RS |
1469 | if (! NILP (Vsignal_hook_function) |
1470 | && ! NILP (error_symbol)) | |
9f5903bb RS |
1471 | { |
1472 | /* Edebug takes care of restoring these variables when it exits. */ | |
1473 | if (lisp_eval_depth + 20 > max_lisp_eval_depth) | |
1474 | max_lisp_eval_depth = lisp_eval_depth + 20; | |
1475 | ||
1476 | if (SPECPDL_INDEX () + 40 > max_specpdl_size) | |
1477 | max_specpdl_size = SPECPDL_INDEX () + 40; | |
1478 | ||
1479 | call2 (Vsignal_hook_function, error_symbol, data); | |
1480 | } | |
61ede770 | 1481 | |
1ea9dec4 | 1482 | conditions = Fget (real_error_symbol, Qerror_conditions); |
db9f0278 | 1483 | |
a2ff3819 GM |
1484 | /* Remember from where signal was called. Skip over the frame for |
1485 | `signal' itself. If a frame for `error' follows, skip that, | |
26631f2b RS |
1486 | too. Don't do this when ERROR_SYMBOL is nil, because that |
1487 | is a memory-full error. */ | |
090a072f | 1488 | Vsignaling_function = Qnil; |
2f592f95 | 1489 | if (!NILP (error_symbol)) |
090a072f | 1490 | { |
9349e5f7 | 1491 | union specbinding *pdl = backtrace_next (backtrace_top ()); |
2f592f95 SM |
1492 | if (backtrace_p (pdl) && EQ (backtrace_function (pdl), Qerror)) |
1493 | pdl = backtrace_next (pdl); | |
1494 | if (backtrace_p (pdl)) | |
1495 | Vsignaling_function = backtrace_function (pdl); | |
090a072f | 1496 | } |
a2ff3819 | 1497 | |
e7f7fbaa | 1498 | for (h = handlerlist; h; h = h->next) |
db9f0278 | 1499 | { |
adf2aa61 SM |
1500 | if (h->type != CONDITION_CASE) |
1501 | continue; | |
1502 | clause = find_handler_clause (h->tag_or_ch, conditions); | |
265a9e55 | 1503 | if (!NILP (clause)) |
e7f7fbaa | 1504 | break; |
db9f0278 | 1505 | } |
475545b5 | 1506 | |
e7f7fbaa | 1507 | if (/* Don't run the debugger for a memory-full error. |
e7c1b6ef | 1508 | (There is no room in memory to do that!) */ |
e7f7fbaa SM |
1509 | !NILP (error_symbol) |
1510 | && (!NILP (Vdebug_on_signal) | |
1511 | /* If no handler is present now, try to run the debugger. */ | |
1512 | || NILP (clause) | |
bd1ba3e8 CY |
1513 | /* A `debug' symbol in the handler list disables the normal |
1514 | suppression of the debugger. */ | |
1515 | || (CONSP (clause) && CONSP (XCAR (clause)) | |
1516 | && !NILP (Fmemq (Qdebug, XCAR (clause)))) | |
e7f7fbaa SM |
1517 | /* Special handler that means "print a message and run debugger |
1518 | if requested". */ | |
adf2aa61 | 1519 | || EQ (h->tag_or_ch, Qerror))) |
e7f7fbaa | 1520 | { |
1882aa38 | 1521 | bool debugger_called |
e7f7fbaa SM |
1522 | = maybe_call_debugger (conditions, error_symbol, data); |
1523 | /* We can't return values to code which signaled an error, but we | |
1524 | can continue code which has signaled a quit. */ | |
1525 | if (debugger_called && EQ (real_error_symbol, Qquit)) | |
1526 | return Qnil; | |
475545b5 | 1527 | } |
db9f0278 | 1528 | |
e7f7fbaa SM |
1529 | if (!NILP (clause)) |
1530 | { | |
1531 | Lisp_Object unwind_data | |
1532 | = (NILP (error_symbol) ? data : Fcons (error_symbol, data)); | |
475545b5 | 1533 | |
adf2aa61 | 1534 | unwind_to_catch (h, unwind_data); |
e7f7fbaa SM |
1535 | } |
1536 | else | |
1537 | { | |
adf2aa61 | 1538 | if (handlerlist != 0) |
e7f7fbaa SM |
1539 | Fthrow (Qtop_level, Qt); |
1540 | } | |
c11d3d17 | 1541 | |
1ea9dec4 | 1542 | if (! NILP (error_symbol)) |
c11d3d17 | 1543 | data = Fcons (error_symbol, data); |
475545b5 | 1544 | |
c11d3d17 | 1545 | string = Ferror_message_string (data); |
583f48b9 | 1546 | fatal ("%s", SDATA (string)); |
db9f0278 JB |
1547 | } |
1548 | ||
734d55a2 KS |
1549 | /* Internal version of Fsignal that never returns. |
1550 | Used for anything but Qquit (which can return from Fsignal). */ | |
1551 | ||
1552 | void | |
d3da34e0 | 1553 | xsignal (Lisp_Object error_symbol, Lisp_Object data) |
734d55a2 KS |
1554 | { |
1555 | Fsignal (error_symbol, data); | |
1088b922 | 1556 | emacs_abort (); |
734d55a2 KS |
1557 | } |
1558 | ||
1559 | /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */ | |
1560 | ||
1561 | void | |
d3da34e0 | 1562 | xsignal0 (Lisp_Object error_symbol) |
734d55a2 KS |
1563 | { |
1564 | xsignal (error_symbol, Qnil); | |
1565 | } | |
1566 | ||
1567 | void | |
d3da34e0 | 1568 | xsignal1 (Lisp_Object error_symbol, Lisp_Object arg) |
734d55a2 KS |
1569 | { |
1570 | xsignal (error_symbol, list1 (arg)); | |
1571 | } | |
1572 | ||
1573 | void | |
d3da34e0 | 1574 | xsignal2 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2) |
734d55a2 KS |
1575 | { |
1576 | xsignal (error_symbol, list2 (arg1, arg2)); | |
1577 | } | |
1578 | ||
1579 | void | |
d3da34e0 | 1580 | xsignal3 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3) |
734d55a2 KS |
1581 | { |
1582 | xsignal (error_symbol, list3 (arg1, arg2, arg3)); | |
1583 | } | |
1584 | ||
1585 | /* Signal `error' with message S, and additional arg ARG. | |
1586 | If ARG is not a genuine list, make it a one-element list. */ | |
1587 | ||
1588 | void | |
a8fe7202 | 1589 | signal_error (const char *s, Lisp_Object arg) |
734d55a2 KS |
1590 | { |
1591 | Lisp_Object tortoise, hare; | |
1592 | ||
1593 | hare = tortoise = arg; | |
1594 | while (CONSP (hare)) | |
1595 | { | |
1596 | hare = XCDR (hare); | |
1597 | if (!CONSP (hare)) | |
1598 | break; | |
1599 | ||
1600 | hare = XCDR (hare); | |
1601 | tortoise = XCDR (tortoise); | |
1602 | ||
1603 | if (EQ (hare, tortoise)) | |
1604 | break; | |
1605 | } | |
1606 | ||
1607 | if (!NILP (hare)) | |
6c6f1994 | 1608 | arg = list1 (arg); |
734d55a2 KS |
1609 | |
1610 | xsignal (Qerror, Fcons (build_string (s), arg)); | |
1611 | } | |
1612 | ||
1613 | ||
1882aa38 | 1614 | /* Return true if LIST is a non-nil atom or |
128c0f66 RM |
1615 | a list containing one of CONDITIONS. */ |
1616 | ||
1882aa38 | 1617 | static bool |
d3da34e0 | 1618 | wants_debugger (Lisp_Object list, Lisp_Object conditions) |
128c0f66 | 1619 | { |
4de86b16 | 1620 | if (NILP (list)) |
128c0f66 RM |
1621 | return 0; |
1622 | if (! CONSP (list)) | |
1623 | return 1; | |
1624 | ||
ab67260b | 1625 | while (CONSP (conditions)) |
128c0f66 | 1626 | { |
ab67260b | 1627 | Lisp_Object this, tail; |
03699b14 KR |
1628 | this = XCAR (conditions); |
1629 | for (tail = list; CONSP (tail); tail = XCDR (tail)) | |
1630 | if (EQ (XCAR (tail), this)) | |
128c0f66 | 1631 | return 1; |
03699b14 | 1632 | conditions = XCDR (conditions); |
128c0f66 | 1633 | } |
ab67260b | 1634 | return 0; |
128c0f66 RM |
1635 | } |
1636 | ||
1882aa38 | 1637 | /* Return true if an error with condition-symbols CONDITIONS, |
fc950e09 | 1638 | and described by SIGNAL-DATA, should skip the debugger |
1b1acc13 | 1639 | according to debugger-ignored-errors. */ |
fc950e09 | 1640 | |
1882aa38 | 1641 | static bool |
d3da34e0 | 1642 | skip_debugger (Lisp_Object conditions, Lisp_Object data) |
fc950e09 KH |
1643 | { |
1644 | Lisp_Object tail; | |
1882aa38 | 1645 | bool first_string = 1; |
fc950e09 KH |
1646 | Lisp_Object error_message; |
1647 | ||
17401c97 GM |
1648 | error_message = Qnil; |
1649 | for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail)) | |
fc950e09 | 1650 | { |
03699b14 | 1651 | if (STRINGP (XCAR (tail))) |
fc950e09 KH |
1652 | { |
1653 | if (first_string) | |
1654 | { | |
1655 | error_message = Ferror_message_string (data); | |
1656 | first_string = 0; | |
1657 | } | |
177c0ea7 | 1658 | |
03699b14 | 1659 | if (fast_string_match (XCAR (tail), error_message) >= 0) |
fc950e09 KH |
1660 | return 1; |
1661 | } | |
1662 | else | |
1663 | { | |
1664 | Lisp_Object contail; | |
1665 | ||
17401c97 | 1666 | for (contail = conditions; CONSP (contail); contail = XCDR (contail)) |
03699b14 | 1667 | if (EQ (XCAR (tail), XCAR (contail))) |
fc950e09 KH |
1668 | return 1; |
1669 | } | |
1670 | } | |
1671 | ||
1672 | return 0; | |
1673 | } | |
1674 | ||
ddaa36e1 | 1675 | /* Call the debugger if calling it is currently enabled for CONDITIONS. |
7d47b580 JB |
1676 | SIG and DATA describe the signal. There are two ways to pass them: |
1677 | = SIG is the error symbol, and DATA is the rest of the data. | |
1678 | = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA). | |
1679 | This is for memory-full errors only. */ | |
1882aa38 | 1680 | static bool |
d3da34e0 | 1681 | maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data) |
ddaa36e1 AS |
1682 | { |
1683 | Lisp_Object combined_data; | |
1684 | ||
1685 | combined_data = Fcons (sig, data); | |
1686 | ||
1687 | if ( | |
1688 | /* Don't try to run the debugger with interrupts blocked. | |
1689 | The editing loop would return anyway. */ | |
4d7e6e51 | 1690 | ! input_blocked_p () |
45b82ad0 | 1691 | && NILP (Vinhibit_debugger) |
ddaa36e1 AS |
1692 | /* Does user want to enter debugger for this kind of error? */ |
1693 | && (EQ (sig, Qquit) | |
1694 | ? debug_on_quit | |
1695 | : wants_debugger (Vdebug_on_error, conditions)) | |
1696 | && ! skip_debugger (conditions, combined_data) | |
f6d62986 | 1697 | /* RMS: What's this for? */ |
ddaa36e1 AS |
1698 | && when_entered_debugger < num_nonmacro_input_events) |
1699 | { | |
6c6f1994 | 1700 | call_debugger (list2 (Qerror, combined_data)); |
ddaa36e1 AS |
1701 | return 1; |
1702 | } | |
1703 | ||
1704 | return 0; | |
1705 | } | |
1706 | ||
db9f0278 | 1707 | static Lisp_Object |
7d47b580 | 1708 | find_handler_clause (Lisp_Object handlers, Lisp_Object conditions) |
db9f0278 JB |
1709 | { |
1710 | register Lisp_Object h; | |
db9f0278 | 1711 | |
f01cbfdd RS |
1712 | /* t is used by handlers for all conditions, set up by C code. */ |
1713 | if (EQ (handlers, Qt)) | |
db9f0278 | 1714 | return Qt; |
f01cbfdd | 1715 | |
61ede770 RS |
1716 | /* error is used similarly, but means print an error message |
1717 | and run the debugger if that is enabled. */ | |
e7f7fbaa SM |
1718 | if (EQ (handlers, Qerror)) |
1719 | return Qt; | |
f01cbfdd | 1720 | |
e7f7fbaa | 1721 | for (h = handlers; CONSP (h); h = XCDR (h)) |
db9f0278 | 1722 | { |
e7f7fbaa | 1723 | Lisp_Object handler = XCAR (h); |
adf2aa61 SM |
1724 | if (!NILP (Fmemq (handler, conditions))) |
1725 | return handlers; | |
db9f0278 | 1726 | } |
f01cbfdd | 1727 | |
db9f0278 JB |
1728 | return Qnil; |
1729 | } | |
1730 | ||
db9f0278 | 1731 | |
f6d62986 | 1732 | /* Dump an error message; called like vprintf. */ |
db9f0278 | 1733 | void |
b3ffc17c | 1734 | verror (const char *m, va_list ap) |
db9f0278 | 1735 | { |
70476b54 | 1736 | char buf[4000]; |
c2d1e36d PE |
1737 | ptrdiff_t size = sizeof buf; |
1738 | ptrdiff_t size_max = STRING_BYTES_BOUND + 1; | |
9125da08 | 1739 | char *buffer = buf; |
c2d1e36d | 1740 | ptrdiff_t used; |
9125da08 RS |
1741 | Lisp_Object string; |
1742 | ||
d749b01b | 1743 | used = evxprintf (&buffer, &size, buf, size_max, m, ap); |
5fdb398c | 1744 | string = make_string (buffer, used); |
eb3f1cc8 | 1745 | if (buffer != buf) |
9ae6734f | 1746 | xfree (buffer); |
9125da08 | 1747 | |
734d55a2 | 1748 | xsignal1 (Qerror, string); |
db9f0278 | 1749 | } |
b3ffc17c DN |
1750 | |
1751 | ||
f6d62986 | 1752 | /* Dump an error message; called like printf. */ |
b3ffc17c DN |
1753 | |
1754 | /* VARARGS 1 */ | |
1755 | void | |
1756 | error (const char *m, ...) | |
1757 | { | |
1758 | va_list ap; | |
1759 | va_start (ap, m); | |
1760 | verror (m, ap); | |
b3ffc17c | 1761 | } |
db9f0278 | 1762 | \f |
a7ca3326 | 1763 | DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0, |
9dbc9081 PJ |
1764 | doc: /* Non-nil if FUNCTION makes provisions for interactive calling. |
1765 | This means it contains a description for how to read arguments to give it. | |
1766 | The value is nil for an invalid function or a symbol with no function | |
1767 | definition. | |
1768 | ||
1769 | Interactively callable functions include strings and vectors (treated | |
1770 | as keyboard macros), lambda-expressions that contain a top-level call | |
1771 | to `interactive', autoload definitions made by `autoload' with non-nil | |
1772 | fourth argument, and some of the built-in functions of Lisp. | |
1773 | ||
e72706be RS |
1774 | Also, a symbol satisfies `commandp' if its function definition does so. |
1775 | ||
1776 | If the optional argument FOR-CALL-INTERACTIVELY is non-nil, | |
769b4fb2 | 1777 | then strings and vectors are not accepted. */) |
5842a27b | 1778 | (Lisp_Object function, Lisp_Object for_call_interactively) |
db9f0278 JB |
1779 | { |
1780 | register Lisp_Object fun; | |
1781 | register Lisp_Object funcar; | |
52b71f49 | 1782 | Lisp_Object if_prop = Qnil; |
db9f0278 JB |
1783 | |
1784 | fun = function; | |
1785 | ||
eadf1faa SM |
1786 | fun = indirect_function (fun); /* Check cycles. */ |
1787 | if (NILP (fun)) | |
ffd56f97 | 1788 | return Qnil; |
db9f0278 | 1789 | |
52b71f49 | 1790 | /* Check an `interactive-form' property if present, analogous to the |
eadf1faa | 1791 | function-documentation property. */ |
52b71f49 SM |
1792 | fun = function; |
1793 | while (SYMBOLP (fun)) | |
1794 | { | |
2b9aa051 | 1795 | Lisp_Object tmp = Fget (fun, Qinteractive_form); |
52b71f49 SM |
1796 | if (!NILP (tmp)) |
1797 | if_prop = Qt; | |
1798 | fun = Fsymbol_function (fun); | |
1799 | } | |
1800 | ||
db9f0278 JB |
1801 | /* Emacs primitives are interactive if their DEFUN specifies an |
1802 | interactive spec. */ | |
90165123 | 1803 | if (SUBRP (fun)) |
04724b69 | 1804 | return XSUBR (fun)->intspec ? Qt : if_prop; |
db9f0278 JB |
1805 | |
1806 | /* Bytecode objects are interactive if they are long enough to | |
1807 | have an element whose index is COMPILED_INTERACTIVE, which is | |
1808 | where the interactive spec is stored. */ | |
90165123 | 1809 | else if (COMPILEDP (fun)) |
845975f5 | 1810 | return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE |
52b71f49 | 1811 | ? Qt : if_prop); |
db9f0278 JB |
1812 | |
1813 | /* Strings and vectors are keyboard macros. */ | |
52b71f49 | 1814 | if (STRINGP (fun) || VECTORP (fun)) |
6e33efc4 | 1815 | return (NILP (for_call_interactively) ? Qt : Qnil); |
db9f0278 JB |
1816 | |
1817 | /* Lists may represent commands. */ | |
1818 | if (!CONSP (fun)) | |
1819 | return Qnil; | |
ed16fb98 | 1820 | funcar = XCAR (fun); |
b38b1ec0 | 1821 | if (EQ (funcar, Qclosure)) |
7200d79c SM |
1822 | return (!NILP (Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun))))) |
1823 | ? Qt : if_prop); | |
23aba0ea | 1824 | else if (EQ (funcar, Qlambda)) |
52b71f49 | 1825 | return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop; |
b38b1ec0 | 1826 | else if (EQ (funcar, Qautoload)) |
52b71f49 | 1827 | return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop; |
db9f0278 JB |
1828 | else |
1829 | return Qnil; | |
1830 | } | |
1831 | ||
db9f0278 | 1832 | DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0, |
9dbc9081 PJ |
1833 | doc: /* Define FUNCTION to autoload from FILE. |
1834 | FUNCTION is a symbol; FILE is a file name string to pass to `load'. | |
1835 | Third arg DOCSTRING is documentation for the function. | |
1836 | Fourth arg INTERACTIVE if non-nil says function can be called interactively. | |
1837 | Fifth arg TYPE indicates the type of the object: | |
1838 | nil or omitted says FUNCTION is a function, | |
1839 | `keymap' says FUNCTION is really a keymap, and | |
1840 | `macro' or t says FUNCTION is really a macro. | |
1841 | Third through fifth args give info about the real definition. | |
1842 | They default to nil. | |
1843 | If FUNCTION is already defined other than as an autoload, | |
1844 | this does nothing and returns nil. */) | |
5842a27b | 1845 | (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object interactive, Lisp_Object type) |
db9f0278 | 1846 | { |
b7826503 PJ |
1847 | CHECK_SYMBOL (function); |
1848 | CHECK_STRING (file); | |
db9f0278 | 1849 | |
f6d62986 | 1850 | /* If function is defined and not as an autoload, don't override. */ |
eadf1faa | 1851 | if (!NILP (XSYMBOL (function)->function) |
32e5c58c | 1852 | && !AUTOLOADP (XSYMBOL (function)->function)) |
db9f0278 JB |
1853 | return Qnil; |
1854 | ||
32e5c58c | 1855 | if (!NILP (Vpurify_flag) && EQ (docstring, make_number (0))) |
61b108cc SM |
1856 | /* `read1' in lread.c has found the docstring starting with "\ |
1857 | and assumed the docstring will be provided by Snarf-documentation, so it | |
1858 | passed us 0 instead. But that leads to accidental sharing in purecopy's | |
1859 | hash-consing, so we use a (hopefully) unique integer instead. */ | |
32e5c58c SM |
1860 | docstring = make_number (XHASH (function)); |
1861 | return Fdefalias (function, | |
1862 | list5 (Qautoload, file, docstring, interactive, type), | |
1863 | Qnil); | |
db9f0278 JB |
1864 | } |
1865 | ||
27e498e6 | 1866 | void |
d3da34e0 | 1867 | un_autoload (Lisp_Object oldqueue) |
db9f0278 | 1868 | { |
27e498e6 | 1869 | Lisp_Object queue, first, second; |
db9f0278 JB |
1870 | |
1871 | /* Queue to unwind is current value of Vautoload_queue. | |
1872 | oldqueue is the shadowed value to leave in Vautoload_queue. */ | |
1873 | queue = Vautoload_queue; | |
1874 | Vautoload_queue = oldqueue; | |
1875 | while (CONSP (queue)) | |
1876 | { | |
e509f168 | 1877 | first = XCAR (queue); |
db9f0278 JB |
1878 | second = Fcdr (first); |
1879 | first = Fcar (first); | |
47b82df9 RS |
1880 | if (EQ (first, make_number (0))) |
1881 | Vfeatures = second; | |
db9f0278 JB |
1882 | else |
1883 | Ffset (first, second); | |
e509f168 | 1884 | queue = XCDR (queue); |
db9f0278 | 1885 | } |
db9f0278 JB |
1886 | } |
1887 | ||
ca20916b RS |
1888 | /* Load an autoloaded function. |
1889 | FUNNAME is the symbol which is the function's name. | |
1890 | FUNDEF is the autoload definition (a list). */ | |
1891 | ||
7abaf5cc SM |
1892 | DEFUN ("autoload-do-load", Fautoload_do_load, Sautoload_do_load, 1, 3, 0, |
1893 | doc: /* Load FUNDEF which should be an autoload. | |
1894 | If non-nil, FUNNAME should be the symbol whose function value is FUNDEF, | |
1895 | in which case the function returns the new autoloaded function value. | |
1896 | If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if | |
1897 | it is defines a macro. */) | |
1898 | (Lisp_Object fundef, Lisp_Object funname, Lisp_Object macro_only) | |
db9f0278 | 1899 | { |
d311d28c | 1900 | ptrdiff_t count = SPECPDL_INDEX (); |
ca20916b | 1901 | struct gcpro gcpro1, gcpro2, gcpro3; |
db9f0278 | 1902 | |
7abaf5cc SM |
1903 | if (!CONSP (fundef) || !EQ (Qautoload, XCAR (fundef))) |
1904 | return fundef; | |
1905 | ||
1906 | if (EQ (macro_only, Qmacro)) | |
1907 | { | |
1908 | Lisp_Object kind = Fnth (make_number (4), fundef); | |
1909 | if (! (EQ (kind, Qt) || EQ (kind, Qmacro))) | |
1910 | return fundef; | |
1911 | } | |
1912 | ||
aea6173f RS |
1913 | /* This is to make sure that loadup.el gives a clear picture |
1914 | of what files are preloaded and when. */ | |
ab4db096 RS |
1915 | if (! NILP (Vpurify_flag)) |
1916 | error ("Attempt to autoload %s while preparing to dump", | |
d5db4077 | 1917 | SDATA (SYMBOL_NAME (funname))); |
ab4db096 | 1918 | |
b7826503 | 1919 | CHECK_SYMBOL (funname); |
7abaf5cc | 1920 | GCPRO3 (funname, fundef, macro_only); |
db9f0278 | 1921 | |
f87740dc | 1922 | /* Preserve the match data. */ |
89f2614d | 1923 | record_unwind_save_match_data (); |
177c0ea7 | 1924 | |
a04ee161 RS |
1925 | /* If autoloading gets an error (which includes the error of failing |
1926 | to define the function being called), we use Vautoload_queue | |
1927 | to undo function definitions and `provide' calls made by | |
1928 | the function. We do this in the specific case of autoloading | |
1929 | because autoloading is not an explicit request "load this file", | |
1930 | but rather a request to "call this function". | |
d3da34e0 | 1931 | |
a04ee161 | 1932 | The value saved here is to be restored into Vautoload_queue. */ |
db9f0278 JB |
1933 | record_unwind_protect (un_autoload, Vautoload_queue); |
1934 | Vautoload_queue = Qt; | |
7abaf5cc SM |
1935 | /* If `macro_only', assume this autoload to be a "best-effort", |
1936 | so don't signal an error if autoloading fails. */ | |
1937 | Fload (Fcar (Fcdr (fundef)), macro_only, Qt, Qnil, Qt); | |
2a49b6e5 | 1938 | |
db9f0278 JB |
1939 | /* Once loading finishes, don't undo it. */ |
1940 | Vautoload_queue = Qt; | |
1941 | unbind_to (count, Qnil); | |
1942 | ||
ca20916b | 1943 | UNGCPRO; |
7abaf5cc SM |
1944 | |
1945 | if (NILP (funname)) | |
1946 | return Qnil; | |
1947 | else | |
1948 | { | |
1949 | Lisp_Object fun = Findirect_function (funname, Qnil); | |
1950 | ||
1951 | if (!NILP (Fequal (fun, fundef))) | |
1952 | error ("Autoloading failed to define function %s", | |
1953 | SDATA (SYMBOL_NAME (funname))); | |
1954 | else | |
1955 | return fun; | |
1956 | } | |
db9f0278 | 1957 | } |
4c576a83 | 1958 | |
db9f0278 | 1959 | \f |
a7ca3326 | 1960 | DEFUN ("eval", Feval, Seval, 1, 2, 0, |
a0ee6f27 | 1961 | doc: /* Evaluate FORM and return its value. |
8c27f5ff SM |
1962 | If LEXICAL is t, evaluate using lexical scoping. |
1963 | LEXICAL can also be an actual lexical environment, in the form of an | |
1964 | alist mapping symbols to their value. */) | |
a0ee6f27 | 1965 | (Lisp_Object form, Lisp_Object lexical) |
defb1411 | 1966 | { |
d311d28c | 1967 | ptrdiff_t count = SPECPDL_INDEX (); |
a0ee6f27 | 1968 | specbind (Qinternal_interpreter_environment, |
6c6f1994 | 1969 | CONSP (lexical) || NILP (lexical) ? lexical : list1 (Qt)); |
defb1411 SM |
1970 | return unbind_to (count, eval_sub (form)); |
1971 | } | |
1972 | ||
5e301d76 PE |
1973 | /* Grow the specpdl stack by one entry. |
1974 | The caller should have already initialized the entry. | |
1975 | Signal an error on stack overflow. | |
1976 | ||
1977 | Make sure that there is always one unused entry past the top of the | |
1978 | stack, so that the just-initialized entry is safely unwound if | |
1979 | memory exhausted and an error is signaled here. Also, allocate a | |
1980 | never-used entry just before the bottom of the stack; sometimes its | |
1981 | address is taken. */ | |
1982 | ||
2f592f95 SM |
1983 | static void |
1984 | grow_specpdl (void) | |
1985 | { | |
5e301d76 PE |
1986 | specpdl_ptr++; |
1987 | ||
1988 | if (specpdl_ptr == specpdl + specpdl_size) | |
2f592f95 | 1989 | { |
5e301d76 PE |
1990 | ptrdiff_t count = SPECPDL_INDEX (); |
1991 | ptrdiff_t max_size = min (max_specpdl_size, PTRDIFF_MAX - 1000); | |
1992 | union specbinding *pdlvec = specpdl - 1; | |
1993 | ptrdiff_t pdlvecsize = specpdl_size + 1; | |
2f592f95 | 1994 | if (max_size <= specpdl_size) |
5e301d76 PE |
1995 | { |
1996 | if (max_specpdl_size < 400) | |
1997 | max_size = max_specpdl_size = 400; | |
1998 | if (max_size <= specpdl_size) | |
1999 | signal_error ("Variable binding depth exceeds max-specpdl-size", | |
2000 | Qnil); | |
2001 | } | |
2002 | pdlvec = xpalloc (pdlvec, &pdlvecsize, 1, max_size + 1, sizeof *specpdl); | |
2003 | specpdl = pdlvec + 1; | |
2004 | specpdl_size = pdlvecsize - 1; | |
2005 | specpdl_ptr = specpdl + count; | |
2f592f95 | 2006 | } |
2f592f95 SM |
2007 | } |
2008 | ||
3d5ee10a | 2009 | void |
2f592f95 SM |
2010 | record_in_backtrace (Lisp_Object function, Lisp_Object *args, ptrdiff_t nargs) |
2011 | { | |
2012 | eassert (nargs >= UNEVALLED); | |
9349e5f7 PE |
2013 | specpdl_ptr->bt.kind = SPECPDL_BACKTRACE; |
2014 | specpdl_ptr->bt.debug_on_exit = false; | |
2015 | specpdl_ptr->bt.function = function; | |
2016 | specpdl_ptr->bt.args = args; | |
2017 | specpdl_ptr->bt.nargs = nargs; | |
5e301d76 | 2018 | grow_specpdl (); |
2f592f95 SM |
2019 | } |
2020 | ||
defb1411 SM |
2021 | /* Eval a sub-expression of the current expression (i.e. in the same |
2022 | lexical scope). */ | |
2023 | Lisp_Object | |
2024 | eval_sub (Lisp_Object form) | |
db9f0278 JB |
2025 | { |
2026 | Lisp_Object fun, val, original_fun, original_args; | |
2027 | Lisp_Object funcar; | |
db9f0278 JB |
2028 | struct gcpro gcpro1, gcpro2, gcpro3; |
2029 | ||
90165123 | 2030 | if (SYMBOLP (form)) |
b9598260 | 2031 | { |
f07a954e SM |
2032 | /* Look up its binding in the lexical environment. |
2033 | We do not pay attention to the declared_special flag here, since we | |
2034 | already did that when let-binding the variable. */ | |
2035 | Lisp_Object lex_binding | |
2036 | = !NILP (Vinternal_interpreter_environment) /* Mere optimization! */ | |
2037 | ? Fassq (form, Vinternal_interpreter_environment) | |
2038 | : Qnil; | |
2039 | if (CONSP (lex_binding)) | |
2040 | return XCDR (lex_binding); | |
2041 | else | |
2042 | return Fsymbol_value (form); | |
b9598260 SM |
2043 | } |
2044 | ||
db9f0278 JB |
2045 | if (!CONSP (form)) |
2046 | return form; | |
2047 | ||
2048 | QUIT; | |
9d5a1260 DA |
2049 | |
2050 | GCPRO1 (form); | |
765e61e3 | 2051 | maybe_gc (); |
9d5a1260 | 2052 | UNGCPRO; |
db9f0278 JB |
2053 | |
2054 | if (++lisp_eval_depth > max_lisp_eval_depth) | |
2055 | { | |
2056 | if (max_lisp_eval_depth < 100) | |
2057 | max_lisp_eval_depth = 100; | |
2058 | if (lisp_eval_depth > max_lisp_eval_depth) | |
921baa95 | 2059 | error ("Lisp nesting exceeds `max-lisp-eval-depth'"); |
db9f0278 JB |
2060 | } |
2061 | ||
7d7bbefd DA |
2062 | original_fun = XCAR (form); |
2063 | original_args = XCDR (form); | |
db9f0278 | 2064 | |
2f592f95 SM |
2065 | /* This also protects them from gc. */ |
2066 | record_in_backtrace (original_fun, &original_args, UNEVALLED); | |
db9f0278 JB |
2067 | |
2068 | if (debug_on_next_call) | |
2069 | do_debug_on_call (Qt); | |
2070 | ||
2071 | /* At this point, only original_fun and original_args | |
f6d62986 | 2072 | have values that will be used below. */ |
db9f0278 | 2073 | retry: |
8788120f KS |
2074 | |
2075 | /* Optimize for no indirection. */ | |
2076 | fun = original_fun; | |
306d67bd | 2077 | if (!SYMBOLP (fun)) |
3ec7babc | 2078 | fun = Ffunction (Fcons (fun, Qnil)); |
306d67bd SM |
2079 | else if (!NILP (fun) && (fun = XSYMBOL (fun)->function, SYMBOLP (fun))) |
2080 | fun = indirect_function (fun); | |
db9f0278 | 2081 | |
90165123 | 2082 | if (SUBRP (fun)) |
db9f0278 JB |
2083 | { |
2084 | Lisp_Object numargs; | |
166c822d | 2085 | Lisp_Object argvals[8]; |
db9f0278 JB |
2086 | Lisp_Object args_left; |
2087 | register int i, maxargs; | |
2088 | ||
2089 | args_left = original_args; | |
2090 | numargs = Flength (args_left); | |
2091 | ||
7e63e0c3 | 2092 | check_cons_list (); |
c1788fbc | 2093 | |
f6d62986 SM |
2094 | if (XINT (numargs) < XSUBR (fun)->min_args |
2095 | || (XSUBR (fun)->max_args >= 0 | |
2096 | && XSUBR (fun)->max_args < XINT (numargs))) | |
734d55a2 | 2097 | xsignal2 (Qwrong_number_of_arguments, original_fun, numargs); |
db9f0278 | 2098 | |
ef1b0ba7 | 2099 | else if (XSUBR (fun)->max_args == UNEVALLED) |
bbc6b304 | 2100 | val = (XSUBR (fun)->function.aUNEVALLED) (args_left); |
ef1b0ba7 | 2101 | else if (XSUBR (fun)->max_args == MANY) |
db9f0278 | 2102 | { |
f6d62986 | 2103 | /* Pass a vector of evaluated arguments. */ |
db9f0278 | 2104 | Lisp_Object *vals; |
f66c7cf8 | 2105 | ptrdiff_t argnum = 0; |
3a7a9129 | 2106 | USE_SAFE_ALLOCA; |
db9f0278 | 2107 | |
b72e0717 | 2108 | SAFE_ALLOCA_LISP (vals, XINT (numargs)); |
db9f0278 JB |
2109 | |
2110 | GCPRO3 (args_left, fun, fun); | |
2111 | gcpro3.var = vals; | |
2112 | gcpro3.nvars = 0; | |
2113 | ||
265a9e55 | 2114 | while (!NILP (args_left)) |
db9f0278 | 2115 | { |
defb1411 | 2116 | vals[argnum++] = eval_sub (Fcar (args_left)); |
db9f0278 JB |
2117 | args_left = Fcdr (args_left); |
2118 | gcpro3.nvars = argnum; | |
2119 | } | |
db9f0278 | 2120 | |
2f592f95 SM |
2121 | set_backtrace_args (specpdl_ptr - 1, vals); |
2122 | set_backtrace_nargs (specpdl_ptr - 1, XINT (numargs)); | |
db9f0278 | 2123 | |
d5273788 | 2124 | val = (XSUBR (fun)->function.aMANY) (XINT (numargs), vals); |
a6e3fa71 | 2125 | UNGCPRO; |
3a7a9129 | 2126 | SAFE_FREE (); |
db9f0278 | 2127 | } |
ef1b0ba7 | 2128 | else |
db9f0278 | 2129 | { |
ef1b0ba7 SM |
2130 | GCPRO3 (args_left, fun, fun); |
2131 | gcpro3.var = argvals; | |
2132 | gcpro3.nvars = 0; | |
db9f0278 | 2133 | |
ef1b0ba7 SM |
2134 | maxargs = XSUBR (fun)->max_args; |
2135 | for (i = 0; i < maxargs; args_left = Fcdr (args_left)) | |
2136 | { | |
a0ee6f27 | 2137 | argvals[i] = eval_sub (Fcar (args_left)); |
ef1b0ba7 SM |
2138 | gcpro3.nvars = ++i; |
2139 | } | |
db9f0278 | 2140 | |
ef1b0ba7 | 2141 | UNGCPRO; |
db9f0278 | 2142 | |
2f592f95 SM |
2143 | set_backtrace_args (specpdl_ptr - 1, argvals); |
2144 | set_backtrace_nargs (specpdl_ptr - 1, XINT (numargs)); | |
ef1b0ba7 SM |
2145 | |
2146 | switch (i) | |
2147 | { | |
2148 | case 0: | |
2149 | val = (XSUBR (fun)->function.a0 ()); | |
2150 | break; | |
2151 | case 1: | |
2152 | val = (XSUBR (fun)->function.a1 (argvals[0])); | |
2153 | break; | |
2154 | case 2: | |
2155 | val = (XSUBR (fun)->function.a2 (argvals[0], argvals[1])); | |
2156 | break; | |
2157 | case 3: | |
2158 | val = (XSUBR (fun)->function.a3 | |
2159 | (argvals[0], argvals[1], argvals[2])); | |
2160 | break; | |
2161 | case 4: | |
2162 | val = (XSUBR (fun)->function.a4 | |
2163 | (argvals[0], argvals[1], argvals[2], argvals[3])); | |
2164 | break; | |
2165 | case 5: | |
2166 | val = (XSUBR (fun)->function.a5 | |
2167 | (argvals[0], argvals[1], argvals[2], argvals[3], | |
2168 | argvals[4])); | |
2169 | break; | |
2170 | case 6: | |
2171 | val = (XSUBR (fun)->function.a6 | |
2172 | (argvals[0], argvals[1], argvals[2], argvals[3], | |
2173 | argvals[4], argvals[5])); | |
2174 | break; | |
2175 | case 7: | |
2176 | val = (XSUBR (fun)->function.a7 | |
2177 | (argvals[0], argvals[1], argvals[2], argvals[3], | |
2178 | argvals[4], argvals[5], argvals[6])); | |
2179 | break; | |
2180 | ||
2181 | case 8: | |
2182 | val = (XSUBR (fun)->function.a8 | |
2183 | (argvals[0], argvals[1], argvals[2], argvals[3], | |
2184 | argvals[4], argvals[5], argvals[6], argvals[7])); | |
2185 | break; | |
2186 | ||
2187 | default: | |
2188 | /* Someone has created a subr that takes more arguments than | |
2189 | is supported by this code. We need to either rewrite the | |
2190 | subr to use a different argument protocol, or add more | |
2191 | cases to this switch. */ | |
1088b922 | 2192 | emacs_abort (); |
ef1b0ba7 | 2193 | } |
db9f0278 JB |
2194 | } |
2195 | } | |
ef1b0ba7 | 2196 | else if (COMPILEDP (fun)) |
defb1411 | 2197 | val = apply_lambda (fun, original_args); |
db9f0278 JB |
2198 | else |
2199 | { | |
eadf1faa | 2200 | if (NILP (fun)) |
734d55a2 | 2201 | xsignal1 (Qvoid_function, original_fun); |
db9f0278 | 2202 | if (!CONSP (fun)) |
734d55a2 KS |
2203 | xsignal1 (Qinvalid_function, original_fun); |
2204 | funcar = XCAR (fun); | |
90165123 | 2205 | if (!SYMBOLP (funcar)) |
734d55a2 | 2206 | xsignal1 (Qinvalid_function, original_fun); |
db9f0278 JB |
2207 | if (EQ (funcar, Qautoload)) |
2208 | { | |
7abaf5cc | 2209 | Fautoload_do_load (fun, original_fun, Qnil); |
db9f0278 JB |
2210 | goto retry; |
2211 | } | |
2212 | if (EQ (funcar, Qmacro)) | |
8be3a09c SM |
2213 | { |
2214 | ptrdiff_t count = SPECPDL_INDEX (); | |
8be3a09c SM |
2215 | Lisp_Object exp; |
2216 | /* Bind lexical-binding during expansion of the macro, so the | |
2217 | macro can know reliably if the code it outputs will be | |
2218 | interpreted using lexical-binding or not. */ | |
2219 | specbind (Qlexical_binding, | |
2220 | NILP (Vinternal_interpreter_environment) ? Qnil : Qt); | |
2221 | exp = apply1 (Fcdr (fun), original_args); | |
2222 | unbind_to (count, Qnil); | |
2223 | val = eval_sub (exp); | |
2224 | } | |
defb1411 SM |
2225 | else if (EQ (funcar, Qlambda) |
2226 | || EQ (funcar, Qclosure)) | |
2227 | val = apply_lambda (fun, original_args); | |
db9f0278 | 2228 | else |
734d55a2 | 2229 | xsignal1 (Qinvalid_function, original_fun); |
db9f0278 | 2230 | } |
7e63e0c3 | 2231 | check_cons_list (); |
c1788fbc | 2232 | |
db9f0278 | 2233 | lisp_eval_depth--; |
2f592f95 | 2234 | if (backtrace_debug_on_exit (specpdl_ptr - 1)) |
6c6f1994 | 2235 | val = call_debugger (list2 (Qexit, val)); |
2f592f95 | 2236 | specpdl_ptr--; |
824eb35e | 2237 | |
db9f0278 JB |
2238 | return val; |
2239 | } | |
2240 | \f | |
8edd4a2b | 2241 | DEFUN ("apply", Fapply, Sapply, 1, MANY, 0, |
9dbc9081 PJ |
2242 | doc: /* Call FUNCTION with our remaining args, using our last arg as list of args. |
2243 | Then return the value FUNCTION returns. | |
2244 | Thus, (apply '+ 1 2 '(3 4)) returns 10. | |
2245 | usage: (apply FUNCTION &rest ARGUMENTS) */) | |
f66c7cf8 | 2246 | (ptrdiff_t nargs, Lisp_Object *args) |
db9f0278 | 2247 | { |
d311d28c PE |
2248 | ptrdiff_t i; |
2249 | EMACS_INT numargs; | |
db9f0278 JB |
2250 | register Lisp_Object spread_arg; |
2251 | register Lisp_Object *funcall_args; | |
3a7a9129 | 2252 | Lisp_Object fun, retval; |
96d44c64 | 2253 | struct gcpro gcpro1; |
3a7a9129 | 2254 | USE_SAFE_ALLOCA; |
db9f0278 JB |
2255 | |
2256 | fun = args [0]; | |
2257 | funcall_args = 0; | |
2258 | spread_arg = args [nargs - 1]; | |
b7826503 | 2259 | CHECK_LIST (spread_arg); |
177c0ea7 | 2260 | |
db9f0278 JB |
2261 | numargs = XINT (Flength (spread_arg)); |
2262 | ||
2263 | if (numargs == 0) | |
2264 | return Ffuncall (nargs - 1, args); | |
2265 | else if (numargs == 1) | |
2266 | { | |
03699b14 | 2267 | args [nargs - 1] = XCAR (spread_arg); |
db9f0278 JB |
2268 | return Ffuncall (nargs, args); |
2269 | } | |
2270 | ||
a6e3fa71 | 2271 | numargs += nargs - 2; |
db9f0278 | 2272 | |
8788120f | 2273 | /* Optimize for no indirection. */ |
eadf1faa | 2274 | if (SYMBOLP (fun) && !NILP (fun) |
c644523b | 2275 | && (fun = XSYMBOL (fun)->function, SYMBOLP (fun))) |
8788120f | 2276 | fun = indirect_function (fun); |
eadf1faa | 2277 | if (NILP (fun)) |
db9f0278 | 2278 | { |
f6d62986 | 2279 | /* Let funcall get the error. */ |
ffd56f97 JB |
2280 | fun = args[0]; |
2281 | goto funcall; | |
db9f0278 JB |
2282 | } |
2283 | ||
90165123 | 2284 | if (SUBRP (fun)) |
db9f0278 JB |
2285 | { |
2286 | if (numargs < XSUBR (fun)->min_args | |
2287 | || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs)) | |
f6d62986 | 2288 | goto funcall; /* Let funcall get the error. */ |
c5101a77 | 2289 | else if (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args > numargs) |
db9f0278 JB |
2290 | { |
2291 | /* Avoid making funcall cons up a yet another new vector of arguments | |
f6d62986 | 2292 | by explicitly supplying nil's for optional values. */ |
b72e0717 | 2293 | SAFE_ALLOCA_LISP (funcall_args, 1 + XSUBR (fun)->max_args); |
db9f0278 JB |
2294 | for (i = numargs; i < XSUBR (fun)->max_args;) |
2295 | funcall_args[++i] = Qnil; | |
96d44c64 SM |
2296 | GCPRO1 (*funcall_args); |
2297 | gcpro1.nvars = 1 + XSUBR (fun)->max_args; | |
db9f0278 JB |
2298 | } |
2299 | } | |
2300 | funcall: | |
2301 | /* We add 1 to numargs because funcall_args includes the | |
2302 | function itself as well as its arguments. */ | |
2303 | if (!funcall_args) | |
a6e3fa71 | 2304 | { |
b72e0717 | 2305 | SAFE_ALLOCA_LISP (funcall_args, 1 + numargs); |
96d44c64 SM |
2306 | GCPRO1 (*funcall_args); |
2307 | gcpro1.nvars = 1 + numargs; | |
a6e3fa71 JB |
2308 | } |
2309 | ||
663e2b3f | 2310 | memcpy (funcall_args, args, nargs * word_size); |
db9f0278 JB |
2311 | /* Spread the last arg we got. Its first element goes in |
2312 | the slot that it used to occupy, hence this value of I. */ | |
2313 | i = nargs - 1; | |
265a9e55 | 2314 | while (!NILP (spread_arg)) |
db9f0278 | 2315 | { |
03699b14 KR |
2316 | funcall_args [i++] = XCAR (spread_arg); |
2317 | spread_arg = XCDR (spread_arg); | |
db9f0278 | 2318 | } |
a6e3fa71 | 2319 | |
96d44c64 | 2320 | /* By convention, the caller needs to gcpro Ffuncall's args. */ |
3a7a9129 CY |
2321 | retval = Ffuncall (gcpro1.nvars, funcall_args); |
2322 | UNGCPRO; | |
2323 | SAFE_FREE (); | |
2324 | ||
2325 | return retval; | |
db9f0278 JB |
2326 | } |
2327 | \f | |
ff936e53 SM |
2328 | /* Run hook variables in various ways. */ |
2329 | ||
f6d62986 | 2330 | static Lisp_Object |
f66c7cf8 | 2331 | funcall_nil (ptrdiff_t nargs, Lisp_Object *args) |
f6d62986 SM |
2332 | { |
2333 | Ffuncall (nargs, args); | |
2334 | return Qnil; | |
2335 | } | |
ff936e53 | 2336 | |
a7ca3326 | 2337 | DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0, |
9f685258 | 2338 | doc: /* Run each hook in HOOKS. |
9dbc9081 PJ |
2339 | Each argument should be a symbol, a hook variable. |
2340 | These symbols are processed in the order specified. | |
2341 | If a hook symbol has a non-nil value, that value may be a function | |
2342 | or a list of functions to be called to run the hook. | |
2343 | If the value is a function, it is called with no arguments. | |
2344 | If it is a list, the elements are called, in order, with no arguments. | |
2345 | ||
9f685258 LK |
2346 | Major modes should not use this function directly to run their mode |
2347 | hook; they should use `run-mode-hooks' instead. | |
2348 | ||
72e85d5d RS |
2349 | Do not use `make-local-variable' to make a hook variable buffer-local. |
2350 | Instead, use `add-hook' and specify t for the LOCAL argument. | |
9dbc9081 | 2351 | usage: (run-hooks &rest HOOKS) */) |
f66c7cf8 | 2352 | (ptrdiff_t nargs, Lisp_Object *args) |
ff936e53 SM |
2353 | { |
2354 | Lisp_Object hook[1]; | |
f66c7cf8 | 2355 | ptrdiff_t i; |
ff936e53 SM |
2356 | |
2357 | for (i = 0; i < nargs; i++) | |
2358 | { | |
2359 | hook[0] = args[i]; | |
f6d62986 | 2360 | run_hook_with_args (1, hook, funcall_nil); |
ff936e53 SM |
2361 | } |
2362 | ||
2363 | return Qnil; | |
2364 | } | |
177c0ea7 | 2365 | |
a7ca3326 | 2366 | DEFUN ("run-hook-with-args", Frun_hook_with_args, |
9dbc9081 PJ |
2367 | Srun_hook_with_args, 1, MANY, 0, |
2368 | doc: /* Run HOOK with the specified arguments ARGS. | |
d393cefb GM |
2369 | HOOK should be a symbol, a hook variable. The value of HOOK |
2370 | may be nil, a function, or a list of functions. Call each | |
2371 | function in order with arguments ARGS. The final return value | |
2372 | is unspecified. | |
9dbc9081 | 2373 | |
72e85d5d RS |
2374 | Do not use `make-local-variable' to make a hook variable buffer-local. |
2375 | Instead, use `add-hook' and specify t for the LOCAL argument. | |
9dbc9081 | 2376 | usage: (run-hook-with-args HOOK &rest ARGS) */) |
f66c7cf8 | 2377 | (ptrdiff_t nargs, Lisp_Object *args) |
ff936e53 | 2378 | { |
f6d62986 | 2379 | return run_hook_with_args (nargs, args, funcall_nil); |
ff936e53 SM |
2380 | } |
2381 | ||
d393cefb GM |
2382 | /* NB this one still documents a specific non-nil return value. |
2383 | (As did run-hook-with-args and run-hook-with-args-until-failure | |
2384 | until they were changed in 24.1.) */ | |
a0d76c27 | 2385 | DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success, |
9dbc9081 PJ |
2386 | Srun_hook_with_args_until_success, 1, MANY, 0, |
2387 | doc: /* Run HOOK with the specified arguments ARGS. | |
d393cefb GM |
2388 | HOOK should be a symbol, a hook variable. The value of HOOK |
2389 | may be nil, a function, or a list of functions. Call each | |
2390 | function in order with arguments ARGS, stopping at the first | |
2391 | one that returns non-nil, and return that value. Otherwise (if | |
2392 | all functions return nil, or if there are no functions to call), | |
2393 | return nil. | |
9dbc9081 | 2394 | |
72e85d5d RS |
2395 | Do not use `make-local-variable' to make a hook variable buffer-local. |
2396 | Instead, use `add-hook' and specify t for the LOCAL argument. | |
9dbc9081 | 2397 | usage: (run-hook-with-args-until-success HOOK &rest ARGS) */) |
f66c7cf8 | 2398 | (ptrdiff_t nargs, Lisp_Object *args) |
b0b667cb | 2399 | { |
f6d62986 SM |
2400 | return run_hook_with_args (nargs, args, Ffuncall); |
2401 | } | |
2402 | ||
2403 | static Lisp_Object | |
f66c7cf8 | 2404 | funcall_not (ptrdiff_t nargs, Lisp_Object *args) |
f6d62986 SM |
2405 | { |
2406 | return NILP (Ffuncall (nargs, args)) ? Qt : Qnil; | |
ff936e53 SM |
2407 | } |
2408 | ||
a7ca3326 | 2409 | DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure, |
9dbc9081 PJ |
2410 | Srun_hook_with_args_until_failure, 1, MANY, 0, |
2411 | doc: /* Run HOOK with the specified arguments ARGS. | |
d393cefb GM |
2412 | HOOK should be a symbol, a hook variable. The value of HOOK |
2413 | may be nil, a function, or a list of functions. Call each | |
2414 | function in order with arguments ARGS, stopping at the first | |
2415 | one that returns nil, and return nil. Otherwise (if all functions | |
2416 | return non-nil, or if there are no functions to call), return non-nil | |
2417 | \(do not rely on the precise return value in this case). | |
9dbc9081 | 2418 | |
72e85d5d RS |
2419 | Do not use `make-local-variable' to make a hook variable buffer-local. |
2420 | Instead, use `add-hook' and specify t for the LOCAL argument. | |
9dbc9081 | 2421 | usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */) |
f66c7cf8 | 2422 | (ptrdiff_t nargs, Lisp_Object *args) |
ff936e53 | 2423 | { |
f6d62986 | 2424 | return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil; |
ff936e53 SM |
2425 | } |
2426 | ||
f6d62986 | 2427 | static Lisp_Object |
f66c7cf8 | 2428 | run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args) |
f6d62986 SM |
2429 | { |
2430 | Lisp_Object tmp = args[0], ret; | |
2431 | args[0] = args[1]; | |
2432 | args[1] = tmp; | |
2433 | ret = Ffuncall (nargs, args); | |
2434 | args[1] = args[0]; | |
2435 | args[0] = tmp; | |
2436 | return ret; | |
2437 | } | |
2438 | ||
2439 | DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0, | |
2440 | doc: /* Run HOOK, passing each function through WRAP-FUNCTION. | |
2441 | I.e. instead of calling each function FUN directly with arguments ARGS, | |
2442 | it calls WRAP-FUNCTION with arguments FUN and ARGS. | |
2443 | As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped' | |
2444 | aborts and returns that value. | |
2445 | usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */) | |
f66c7cf8 | 2446 | (ptrdiff_t nargs, Lisp_Object *args) |
f6d62986 SM |
2447 | { |
2448 | return run_hook_with_args (nargs, args, run_hook_wrapped_funcall); | |
2449 | } | |
ff936e53 | 2450 | |
c933ea05 RS |
2451 | /* ARGS[0] should be a hook symbol. |
2452 | Call each of the functions in the hook value, passing each of them | |
2453 | as arguments all the rest of ARGS (all NARGS - 1 elements). | |
f6d62986 | 2454 | FUNCALL specifies how to call each function on the hook. |
c933ea05 RS |
2455 | The caller (or its caller, etc) must gcpro all of ARGS, |
2456 | except that it isn't necessary to gcpro ARGS[0]. */ | |
2457 | ||
f6d62986 | 2458 | Lisp_Object |
f66c7cf8 PE |
2459 | run_hook_with_args (ptrdiff_t nargs, Lisp_Object *args, |
2460 | Lisp_Object (*funcall) (ptrdiff_t nargs, Lisp_Object *args)) | |
ff936e53 | 2461 | { |
f6d62986 | 2462 | Lisp_Object sym, val, ret = Qnil; |
fada05d6 | 2463 | struct gcpro gcpro1, gcpro2, gcpro3; |
b0b667cb | 2464 | |
f029ca5f RS |
2465 | /* If we are dying or still initializing, |
2466 | don't do anything--it would probably crash if we tried. */ | |
2467 | if (NILP (Vrun_hooks)) | |
caff32a7 | 2468 | return Qnil; |
f029ca5f | 2469 | |
b0b667cb | 2470 | sym = args[0]; |
aa681b51 | 2471 | val = find_symbol_value (sym); |
ff936e53 | 2472 | |
b0b667cb | 2473 | if (EQ (val, Qunbound) || NILP (val)) |
ff936e53 | 2474 | return ret; |
03699b14 | 2475 | else if (!CONSP (val) || EQ (XCAR (val), Qlambda)) |
b0b667cb KH |
2476 | { |
2477 | args[0] = val; | |
f6d62986 | 2478 | return funcall (nargs, args); |
b0b667cb KH |
2479 | } |
2480 | else | |
2481 | { | |
1faed8ae PE |
2482 | Lisp_Object global_vals = Qnil; |
2483 | GCPRO3 (sym, val, global_vals); | |
cb9d21f8 | 2484 | |
ff936e53 | 2485 | for (; |
f6d62986 | 2486 | CONSP (val) && NILP (ret); |
03699b14 | 2487 | val = XCDR (val)) |
b0b667cb | 2488 | { |
03699b14 | 2489 | if (EQ (XCAR (val), Qt)) |
b0b667cb KH |
2490 | { |
2491 | /* t indicates this hook has a local binding; | |
2492 | it means to run the global binding too. */ | |
1faed8ae PE |
2493 | global_vals = Fdefault_value (sym); |
2494 | if (NILP (global_vals)) continue; | |
b0b667cb | 2495 | |
1faed8ae | 2496 | if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda)) |
b0b667cb | 2497 | { |
1faed8ae | 2498 | args[0] = global_vals; |
f6d62986 | 2499 | ret = funcall (nargs, args); |
8932b1c2 CY |
2500 | } |
2501 | else | |
2502 | { | |
2503 | for (; | |
f6d62986 | 2504 | CONSP (global_vals) && NILP (ret); |
1faed8ae | 2505 | global_vals = XCDR (global_vals)) |
8932b1c2 | 2506 | { |
1faed8ae | 2507 | args[0] = XCAR (global_vals); |
8932b1c2 CY |
2508 | /* In a global value, t should not occur. If it does, we |
2509 | must ignore it to avoid an endless loop. */ | |
2510 | if (!EQ (args[0], Qt)) | |
f6d62986 | 2511 | ret = funcall (nargs, args); |
8932b1c2 | 2512 | } |
b0b667cb KH |
2513 | } |
2514 | } | |
2515 | else | |
2516 | { | |
03699b14 | 2517 | args[0] = XCAR (val); |
f6d62986 | 2518 | ret = funcall (nargs, args); |
b0b667cb KH |
2519 | } |
2520 | } | |
cb9d21f8 RS |
2521 | |
2522 | UNGCPRO; | |
ff936e53 | 2523 | return ret; |
b0b667cb KH |
2524 | } |
2525 | } | |
c933ea05 | 2526 | |
7d48558f RS |
2527 | /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */ |
2528 | ||
2529 | void | |
d3da34e0 | 2530 | run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2) |
7d48558f RS |
2531 | { |
2532 | Lisp_Object temp[3]; | |
2533 | temp[0] = hook; | |
2534 | temp[1] = arg1; | |
2535 | temp[2] = arg2; | |
2536 | ||
2537 | Frun_hook_with_args (3, temp); | |
2538 | } | |
ff936e53 | 2539 | \f |
f6d62986 | 2540 | /* Apply fn to arg. */ |
db9f0278 | 2541 | Lisp_Object |
d3da34e0 | 2542 | apply1 (Lisp_Object fn, Lisp_Object arg) |
db9f0278 | 2543 | { |
a6e3fa71 JB |
2544 | struct gcpro gcpro1; |
2545 | ||
2546 | GCPRO1 (fn); | |
265a9e55 | 2547 | if (NILP (arg)) |
a6e3fa71 JB |
2548 | RETURN_UNGCPRO (Ffuncall (1, &fn)); |
2549 | gcpro1.nvars = 2; | |
db9f0278 JB |
2550 | { |
2551 | Lisp_Object args[2]; | |
2552 | args[0] = fn; | |
2553 | args[1] = arg; | |
a6e3fa71 JB |
2554 | gcpro1.var = args; |
2555 | RETURN_UNGCPRO (Fapply (2, args)); | |
db9f0278 | 2556 | } |
db9f0278 JB |
2557 | } |
2558 | ||
f6d62986 | 2559 | /* Call function fn on no arguments. */ |
db9f0278 | 2560 | Lisp_Object |
d3da34e0 | 2561 | call0 (Lisp_Object fn) |
db9f0278 | 2562 | { |
a6e3fa71 JB |
2563 | struct gcpro gcpro1; |
2564 | ||
2565 | GCPRO1 (fn); | |
2566 | RETURN_UNGCPRO (Ffuncall (1, &fn)); | |
db9f0278 JB |
2567 | } |
2568 | ||
f6d62986 | 2569 | /* Call function fn with 1 argument arg1. */ |
db9f0278 JB |
2570 | /* ARGSUSED */ |
2571 | Lisp_Object | |
d3da34e0 | 2572 | call1 (Lisp_Object fn, Lisp_Object arg1) |
db9f0278 | 2573 | { |
a6e3fa71 | 2574 | struct gcpro gcpro1; |
177c0ea7 | 2575 | Lisp_Object args[2]; |
a6e3fa71 | 2576 | |
db9f0278 | 2577 | args[0] = fn; |
15285f9f | 2578 | args[1] = arg1; |
a6e3fa71 JB |
2579 | GCPRO1 (args[0]); |
2580 | gcpro1.nvars = 2; | |
2581 | RETURN_UNGCPRO (Ffuncall (2, args)); | |
db9f0278 JB |
2582 | } |
2583 | ||
f6d62986 | 2584 | /* Call function fn with 2 arguments arg1, arg2. */ |
db9f0278 JB |
2585 | /* ARGSUSED */ |
2586 | Lisp_Object | |
d3da34e0 | 2587 | call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2) |
db9f0278 | 2588 | { |
a6e3fa71 | 2589 | struct gcpro gcpro1; |
db9f0278 JB |
2590 | Lisp_Object args[3]; |
2591 | args[0] = fn; | |
15285f9f RS |
2592 | args[1] = arg1; |
2593 | args[2] = arg2; | |
a6e3fa71 JB |
2594 | GCPRO1 (args[0]); |
2595 | gcpro1.nvars = 3; | |
2596 | RETURN_UNGCPRO (Ffuncall (3, args)); | |
db9f0278 JB |
2597 | } |
2598 | ||
f6d62986 | 2599 | /* Call function fn with 3 arguments arg1, arg2, arg3. */ |
db9f0278 JB |
2600 | /* ARGSUSED */ |
2601 | Lisp_Object | |
d3da34e0 | 2602 | call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3) |
db9f0278 | 2603 | { |
a6e3fa71 | 2604 | struct gcpro gcpro1; |
db9f0278 JB |
2605 | Lisp_Object args[4]; |
2606 | args[0] = fn; | |
15285f9f RS |
2607 | args[1] = arg1; |
2608 | args[2] = arg2; | |
2609 | args[3] = arg3; | |
a6e3fa71 JB |
2610 | GCPRO1 (args[0]); |
2611 | gcpro1.nvars = 4; | |
2612 | RETURN_UNGCPRO (Ffuncall (4, args)); | |
db9f0278 JB |
2613 | } |
2614 | ||
f6d62986 | 2615 | /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */ |
a5a44b91 JB |
2616 | /* ARGSUSED */ |
2617 | Lisp_Object | |
d3da34e0 JB |
2618 | call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3, |
2619 | Lisp_Object arg4) | |
a5a44b91 JB |
2620 | { |
2621 | struct gcpro gcpro1; | |
a5a44b91 JB |
2622 | Lisp_Object args[5]; |
2623 | args[0] = fn; | |
15285f9f RS |
2624 | args[1] = arg1; |
2625 | args[2] = arg2; | |
2626 | args[3] = arg3; | |
2627 | args[4] = arg4; | |
a5a44b91 JB |
2628 | GCPRO1 (args[0]); |
2629 | gcpro1.nvars = 5; | |
2630 | RETURN_UNGCPRO (Ffuncall (5, args)); | |
a5a44b91 JB |
2631 | } |
2632 | ||
f6d62986 | 2633 | /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */ |
15285f9f RS |
2634 | /* ARGSUSED */ |
2635 | Lisp_Object | |
d3da34e0 JB |
2636 | call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3, |
2637 | Lisp_Object arg4, Lisp_Object arg5) | |
15285f9f RS |
2638 | { |
2639 | struct gcpro gcpro1; | |
15285f9f RS |
2640 | Lisp_Object args[6]; |
2641 | args[0] = fn; | |
2642 | args[1] = arg1; | |
2643 | args[2] = arg2; | |
2644 | args[3] = arg3; | |
2645 | args[4] = arg4; | |
2646 | args[5] = arg5; | |
2647 | GCPRO1 (args[0]); | |
2648 | gcpro1.nvars = 6; | |
2649 | RETURN_UNGCPRO (Ffuncall (6, args)); | |
15285f9f RS |
2650 | } |
2651 | ||
f6d62986 | 2652 | /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */ |
15285f9f RS |
2653 | /* ARGSUSED */ |
2654 | Lisp_Object | |
d3da34e0 JB |
2655 | call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3, |
2656 | Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6) | |
15285f9f RS |
2657 | { |
2658 | struct gcpro gcpro1; | |
15285f9f RS |
2659 | Lisp_Object args[7]; |
2660 | args[0] = fn; | |
2661 | args[1] = arg1; | |
2662 | args[2] = arg2; | |
2663 | args[3] = arg3; | |
2664 | args[4] = arg4; | |
2665 | args[5] = arg5; | |
2666 | args[6] = arg6; | |
2667 | GCPRO1 (args[0]); | |
2668 | gcpro1.nvars = 7; | |
2669 | RETURN_UNGCPRO (Ffuncall (7, args)); | |
15285f9f RS |
2670 | } |
2671 | ||
f6d62986 | 2672 | /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */ |
574c05e2 KK |
2673 | /* ARGSUSED */ |
2674 | Lisp_Object | |
d3da34e0 JB |
2675 | call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3, |
2676 | Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7) | |
574c05e2 KK |
2677 | { |
2678 | struct gcpro gcpro1; | |
574c05e2 KK |
2679 | Lisp_Object args[8]; |
2680 | args[0] = fn; | |
2681 | args[1] = arg1; | |
2682 | args[2] = arg2; | |
2683 | args[3] = arg3; | |
2684 | args[4] = arg4; | |
2685 | args[5] = arg5; | |
2686 | args[6] = arg6; | |
2687 | args[7] = arg7; | |
2688 | GCPRO1 (args[0]); | |
2689 | gcpro1.nvars = 8; | |
2690 | RETURN_UNGCPRO (Ffuncall (8, args)); | |
574c05e2 KK |
2691 | } |
2692 | ||
6c2ef893 RS |
2693 | /* The caller should GCPRO all the elements of ARGS. */ |
2694 | ||
a7ca3326 | 2695 | DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0, |
7200d79c | 2696 | doc: /* Non-nil if OBJECT is a function. */) |
c566235d | 2697 | (Lisp_Object object) |
b9598260 | 2698 | { |
e1f29348 | 2699 | if (FUNCTIONP (object)) |
b9598260 | 2700 | return Qt; |
e1f29348 | 2701 | return Qnil; |
b9598260 SM |
2702 | } |
2703 | ||
a7ca3326 | 2704 | DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0, |
9dbc9081 PJ |
2705 | doc: /* Call first argument as a function, passing remaining arguments to it. |
2706 | Return the value that function returns. | |
2707 | Thus, (funcall 'cons 'x 'y) returns (x . y). | |
2708 | usage: (funcall FUNCTION &rest ARGUMENTS) */) | |
f66c7cf8 | 2709 | (ptrdiff_t nargs, Lisp_Object *args) |
db9f0278 | 2710 | { |
8788120f | 2711 | Lisp_Object fun, original_fun; |
db9f0278 | 2712 | Lisp_Object funcar; |
f66c7cf8 | 2713 | ptrdiff_t numargs = nargs - 1; |
db9f0278 JB |
2714 | Lisp_Object lisp_numargs; |
2715 | Lisp_Object val; | |
db9f0278 | 2716 | register Lisp_Object *internal_args; |
f66c7cf8 | 2717 | ptrdiff_t i; |
db9f0278 JB |
2718 | |
2719 | QUIT; | |
db9f0278 JB |
2720 | |
2721 | if (++lisp_eval_depth > max_lisp_eval_depth) | |
2722 | { | |
2723 | if (max_lisp_eval_depth < 100) | |
2724 | max_lisp_eval_depth = 100; | |
2725 | if (lisp_eval_depth > max_lisp_eval_depth) | |
921baa95 | 2726 | error ("Lisp nesting exceeds `max-lisp-eval-depth'"); |
db9f0278 JB |
2727 | } |
2728 | ||
2f592f95 SM |
2729 | /* This also GCPROs them. */ |
2730 | record_in_backtrace (args[0], &args[1], nargs - 1); | |
db9f0278 | 2731 | |
7abaf5cc SM |
2732 | /* Call GC after setting up the backtrace, so the latter GCPROs the args. */ |
2733 | maybe_gc (); | |
2734 | ||
db9f0278 JB |
2735 | if (debug_on_next_call) |
2736 | do_debug_on_call (Qlambda); | |
2737 | ||
7e63e0c3 | 2738 | check_cons_list (); |
fff3ff9c | 2739 | |
8788120f KS |
2740 | original_fun = args[0]; |
2741 | ||
db9f0278 JB |
2742 | retry: |
2743 | ||
8788120f KS |
2744 | /* Optimize for no indirection. */ |
2745 | fun = original_fun; | |
eadf1faa | 2746 | if (SYMBOLP (fun) && !NILP (fun) |
c644523b | 2747 | && (fun = XSYMBOL (fun)->function, SYMBOLP (fun))) |
8788120f | 2748 | fun = indirect_function (fun); |
db9f0278 | 2749 | |
90165123 | 2750 | if (SUBRP (fun)) |
db9f0278 | 2751 | { |
ef1b0ba7 | 2752 | if (numargs < XSUBR (fun)->min_args |
db9f0278 JB |
2753 | || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs)) |
2754 | { | |
a631e24c | 2755 | XSETFASTINT (lisp_numargs, numargs); |
734d55a2 | 2756 | xsignal2 (Qwrong_number_of_arguments, original_fun, lisp_numargs); |
db9f0278 JB |
2757 | } |
2758 | ||
ef1b0ba7 | 2759 | else if (XSUBR (fun)->max_args == UNEVALLED) |
734d55a2 | 2760 | xsignal1 (Qinvalid_function, original_fun); |
db9f0278 | 2761 | |
ef1b0ba7 SM |
2762 | else if (XSUBR (fun)->max_args == MANY) |
2763 | val = (XSUBR (fun)->function.aMANY) (numargs, args + 1); | |
db9f0278 | 2764 | else |
db9f0278 | 2765 | { |
ef1b0ba7 SM |
2766 | if (XSUBR (fun)->max_args > numargs) |
2767 | { | |
38182d90 PE |
2768 | internal_args = alloca (XSUBR (fun)->max_args |
2769 | * sizeof *internal_args); | |
663e2b3f | 2770 | memcpy (internal_args, args + 1, numargs * word_size); |
ef1b0ba7 SM |
2771 | for (i = numargs; i < XSUBR (fun)->max_args; i++) |
2772 | internal_args[i] = Qnil; | |
2773 | } | |
2774 | else | |
2775 | internal_args = args + 1; | |
2776 | switch (XSUBR (fun)->max_args) | |
2777 | { | |
2778 | case 0: | |
2779 | val = (XSUBR (fun)->function.a0 ()); | |
2780 | break; | |
2781 | case 1: | |
2782 | val = (XSUBR (fun)->function.a1 (internal_args[0])); | |
2783 | break; | |
2784 | case 2: | |
2785 | val = (XSUBR (fun)->function.a2 | |
2786 | (internal_args[0], internal_args[1])); | |
2787 | break; | |
2788 | case 3: | |
2789 | val = (XSUBR (fun)->function.a3 | |
2790 | (internal_args[0], internal_args[1], internal_args[2])); | |
2791 | break; | |
2792 | case 4: | |
2793 | val = (XSUBR (fun)->function.a4 | |
2794 | (internal_args[0], internal_args[1], internal_args[2], | |
2795 | internal_args[3])); | |
2796 | break; | |
2797 | case 5: | |
2798 | val = (XSUBR (fun)->function.a5 | |
2799 | (internal_args[0], internal_args[1], internal_args[2], | |
2800 | internal_args[3], internal_args[4])); | |
2801 | break; | |
2802 | case 6: | |
2803 | val = (XSUBR (fun)->function.a6 | |
2804 | (internal_args[0], internal_args[1], internal_args[2], | |
2805 | internal_args[3], internal_args[4], internal_args[5])); | |
2806 | break; | |
2807 | case 7: | |
2808 | val = (XSUBR (fun)->function.a7 | |
2809 | (internal_args[0], internal_args[1], internal_args[2], | |
2810 | internal_args[3], internal_args[4], internal_args[5], | |
2811 | internal_args[6])); | |
2812 | break; | |
2813 | ||
2814 | case 8: | |
2815 | val = (XSUBR (fun)->function.a8 | |
2816 | (internal_args[0], internal_args[1], internal_args[2], | |
2817 | internal_args[3], internal_args[4], internal_args[5], | |
2818 | internal_args[6], internal_args[7])); | |
2819 | break; | |
2820 | ||
2821 | default: | |
2822 | ||
2823 | /* If a subr takes more than 8 arguments without using MANY | |
2824 | or UNEVALLED, we need to extend this function to support it. | |
2825 | Until this is done, there is no way to call the function. */ | |
1088b922 | 2826 | emacs_abort (); |
ef1b0ba7 | 2827 | } |
db9f0278 JB |
2828 | } |
2829 | } | |
ef1b0ba7 | 2830 | else if (COMPILEDP (fun)) |
db9f0278 JB |
2831 | val = funcall_lambda (fun, numargs, args + 1); |
2832 | else | |
2833 | { | |
eadf1faa | 2834 | if (NILP (fun)) |
734d55a2 | 2835 | xsignal1 (Qvoid_function, original_fun); |
db9f0278 | 2836 | if (!CONSP (fun)) |
734d55a2 KS |
2837 | xsignal1 (Qinvalid_function, original_fun); |
2838 | funcar = XCAR (fun); | |
90165123 | 2839 | if (!SYMBOLP (funcar)) |
734d55a2 | 2840 | xsignal1 (Qinvalid_function, original_fun); |
defb1411 SM |
2841 | if (EQ (funcar, Qlambda) |
2842 | || EQ (funcar, Qclosure)) | |
db9f0278 | 2843 | val = funcall_lambda (fun, numargs, args + 1); |
db9f0278 JB |
2844 | else if (EQ (funcar, Qautoload)) |
2845 | { | |
7abaf5cc | 2846 | Fautoload_do_load (fun, original_fun, Qnil); |
7e63e0c3 | 2847 | check_cons_list (); |
db9f0278 JB |
2848 | goto retry; |
2849 | } | |
2850 | else | |
734d55a2 | 2851 | xsignal1 (Qinvalid_function, original_fun); |
db9f0278 | 2852 | } |
7e63e0c3 | 2853 | check_cons_list (); |
db9f0278 | 2854 | lisp_eval_depth--; |
2f592f95 | 2855 | if (backtrace_debug_on_exit (specpdl_ptr - 1)) |
6c6f1994 | 2856 | val = call_debugger (list2 (Qexit, val)); |
2f592f95 | 2857 | specpdl_ptr--; |
db9f0278 JB |
2858 | return val; |
2859 | } | |
2860 | \f | |
2f7c71a1 | 2861 | static Lisp_Object |
defb1411 | 2862 | apply_lambda (Lisp_Object fun, Lisp_Object args) |
db9f0278 JB |
2863 | { |
2864 | Lisp_Object args_left; | |
d311d28c PE |
2865 | ptrdiff_t i; |
2866 | EMACS_INT numargs; | |
db9f0278 JB |
2867 | register Lisp_Object *arg_vector; |
2868 | struct gcpro gcpro1, gcpro2, gcpro3; | |
db9f0278 | 2869 | register Lisp_Object tem; |
3a7a9129 | 2870 | USE_SAFE_ALLOCA; |
db9f0278 | 2871 | |
f66c7cf8 | 2872 | numargs = XFASTINT (Flength (args)); |
c5101a77 | 2873 | SAFE_ALLOCA_LISP (arg_vector, numargs); |
db9f0278 JB |
2874 | args_left = args; |
2875 | ||
2876 | GCPRO3 (*arg_vector, args_left, fun); | |
2877 | gcpro1.nvars = 0; | |
2878 | ||
c5101a77 | 2879 | for (i = 0; i < numargs; ) |
db9f0278 JB |
2880 | { |
2881 | tem = Fcar (args_left), args_left = Fcdr (args_left); | |
defb1411 | 2882 | tem = eval_sub (tem); |
db9f0278 JB |
2883 | arg_vector[i++] = tem; |
2884 | gcpro1.nvars = i; | |
2885 | } | |
2886 | ||
2887 | UNGCPRO; | |
2888 | ||
2f592f95 SM |
2889 | set_backtrace_args (specpdl_ptr - 1, arg_vector); |
2890 | set_backtrace_nargs (specpdl_ptr - 1, i); | |
c5101a77 | 2891 | tem = funcall_lambda (fun, numargs, arg_vector); |
db9f0278 JB |
2892 | |
2893 | /* Do the debug-on-exit now, while arg_vector still exists. */ | |
2f592f95 SM |
2894 | if (backtrace_debug_on_exit (specpdl_ptr - 1)) |
2895 | { | |
2896 | /* Don't do it again when we return to eval. */ | |
2897 | set_backtrace_debug_on_exit (specpdl_ptr - 1, false); | |
6c6f1994 | 2898 | tem = call_debugger (list2 (Qexit, tem)); |
2f592f95 | 2899 | } |
3a7a9129 | 2900 | SAFE_FREE (); |
db9f0278 JB |
2901 | return tem; |
2902 | } | |
2903 | ||
2904 | /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR | |
2905 | and return the result of evaluation. | |
2906 | FUN must be either a lambda-expression or a compiled-code object. */ | |
2907 | ||
2901f1d1 | 2908 | static Lisp_Object |
f66c7cf8 | 2909 | funcall_lambda (Lisp_Object fun, ptrdiff_t nargs, |
c5101a77 | 2910 | register Lisp_Object *arg_vector) |
db9f0278 | 2911 | { |
defb1411 | 2912 | Lisp_Object val, syms_left, next, lexenv; |
d311d28c | 2913 | ptrdiff_t count = SPECPDL_INDEX (); |
f66c7cf8 | 2914 | ptrdiff_t i; |
1882aa38 | 2915 | bool optional, rest; |
db9f0278 | 2916 | |
90165123 | 2917 | if (CONSP (fun)) |
9ab90667 | 2918 | { |
defb1411 SM |
2919 | if (EQ (XCAR (fun), Qclosure)) |
2920 | { | |
2921 | fun = XCDR (fun); /* Drop `closure'. */ | |
2922 | lexenv = XCAR (fun); | |
23aba0ea | 2923 | CHECK_LIST_CONS (fun, fun); |
defb1411 SM |
2924 | } |
2925 | else | |
2926 | lexenv = Qnil; | |
9ab90667 GM |
2927 | syms_left = XCDR (fun); |
2928 | if (CONSP (syms_left)) | |
2929 | syms_left = XCAR (syms_left); | |
2930 | else | |
734d55a2 | 2931 | xsignal1 (Qinvalid_function, fun); |
9ab90667 | 2932 | } |
90165123 | 2933 | else if (COMPILEDP (fun)) |
defb1411 | 2934 | { |
798cb644 SM |
2935 | syms_left = AREF (fun, COMPILED_ARGLIST); |
2936 | if (INTEGERP (syms_left)) | |
876c194c SM |
2937 | /* A byte-code object with a non-nil `push args' slot means we |
2938 | shouldn't bind any arguments, instead just call the byte-code | |
2939 | interpreter directly; it will push arguments as necessary. | |
2940 | ||
9173deec | 2941 | Byte-code objects with either a non-existent, or a nil value for |
876c194c SM |
2942 | the `push args' slot (the default), have dynamically-bound |
2943 | arguments, and use the argument-binding code below instead (as do | |
2944 | all interpreted functions, even lexically bound ones). */ | |
2945 | { | |
2946 | /* If we have not actually read the bytecode string | |
2947 | and constants vector yet, fetch them from the file. */ | |
2948 | if (CONSP (AREF (fun, COMPILED_BYTECODE))) | |
2949 | Ffetch_bytecode (fun); | |
2950 | return exec_byte_code (AREF (fun, COMPILED_BYTECODE), | |
2951 | AREF (fun, COMPILED_CONSTANTS), | |
2952 | AREF (fun, COMPILED_STACK_DEPTH), | |
798cb644 | 2953 | syms_left, |
876c194c SM |
2954 | nargs, arg_vector); |
2955 | } | |
defb1411 SM |
2956 | lexenv = Qnil; |
2957 | } | |
9ab90667 | 2958 | else |
1088b922 | 2959 | emacs_abort (); |
db9f0278 | 2960 | |
9ab90667 GM |
2961 | i = optional = rest = 0; |
2962 | for (; CONSP (syms_left); syms_left = XCDR (syms_left)) | |
db9f0278 JB |
2963 | { |
2964 | QUIT; | |
177c0ea7 | 2965 | |
9ab90667 | 2966 | next = XCAR (syms_left); |
8788120f | 2967 | if (!SYMBOLP (next)) |
734d55a2 | 2968 | xsignal1 (Qinvalid_function, fun); |
177c0ea7 | 2969 | |
db9f0278 JB |
2970 | if (EQ (next, Qand_rest)) |
2971 | rest = 1; | |
2972 | else if (EQ (next, Qand_optional)) | |
2973 | optional = 1; | |
db9f0278 | 2974 | else |
db9f0278 | 2975 | { |
e610eaca | 2976 | Lisp_Object arg; |
defb1411 SM |
2977 | if (rest) |
2978 | { | |
e610eaca | 2979 | arg = Flist (nargs - i, &arg_vector[i]); |
defb1411 SM |
2980 | i = nargs; |
2981 | } | |
2982 | else if (i < nargs) | |
e610eaca | 2983 | arg = arg_vector[i++]; |
b9598260 SM |
2984 | else if (!optional) |
2985 | xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs)); | |
2986 | else | |
e610eaca | 2987 | arg = Qnil; |
7200d79c | 2988 | |
b9598260 | 2989 | /* Bind the argument. */ |
876c194c | 2990 | if (!NILP (lexenv) && SYMBOLP (next)) |
b9598260 | 2991 | /* Lexically bind NEXT by adding it to the lexenv alist. */ |
e610eaca | 2992 | lexenv = Fcons (Fcons (next, arg), lexenv); |
b9598260 SM |
2993 | else |
2994 | /* Dynamically bind NEXT. */ | |
e610eaca | 2995 | specbind (next, arg); |
db9f0278 | 2996 | } |
db9f0278 JB |
2997 | } |
2998 | ||
9ab90667 | 2999 | if (!NILP (syms_left)) |
734d55a2 | 3000 | xsignal1 (Qinvalid_function, fun); |
9ab90667 | 3001 | else if (i < nargs) |
734d55a2 | 3002 | xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs)); |
db9f0278 | 3003 | |
b9598260 SM |
3004 | if (!EQ (lexenv, Vinternal_interpreter_environment)) |
3005 | /* Instantiate a new lexical environment. */ | |
3006 | specbind (Qinternal_interpreter_environment, lexenv); | |
3007 | ||
90165123 | 3008 | if (CONSP (fun)) |
9ab90667 | 3009 | val = Fprogn (XCDR (XCDR (fun))); |
db9f0278 | 3010 | else |
ca248607 RS |
3011 | { |
3012 | /* If we have not actually read the bytecode string | |
3013 | and constants vector yet, fetch them from the file. */ | |
845975f5 | 3014 | if (CONSP (AREF (fun, COMPILED_BYTECODE))) |
661c7d6e | 3015 | Ffetch_bytecode (fun); |
b9598260 SM |
3016 | val = exec_byte_code (AREF (fun, COMPILED_BYTECODE), |
3017 | AREF (fun, COMPILED_CONSTANTS), | |
3018 | AREF (fun, COMPILED_STACK_DEPTH), | |
3019 | Qnil, 0, 0); | |
ca248607 | 3020 | } |
177c0ea7 | 3021 | |
db9f0278 JB |
3022 | return unbind_to (count, val); |
3023 | } | |
661c7d6e KH |
3024 | |
3025 | DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode, | |
9dbc9081 PJ |
3026 | 1, 1, 0, |
3027 | doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */) | |
5842a27b | 3028 | (Lisp_Object object) |
661c7d6e KH |
3029 | { |
3030 | Lisp_Object tem; | |
3031 | ||
845975f5 | 3032 | if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE))) |
661c7d6e | 3033 | { |
845975f5 | 3034 | tem = read_doc_string (AREF (object, COMPILED_BYTECODE)); |
5bbdb090 | 3035 | if (!CONSP (tem)) |
845975f5 SM |
3036 | { |
3037 | tem = AREF (object, COMPILED_BYTECODE); | |
3038 | if (CONSP (tem) && STRINGP (XCAR (tem))) | |
d5db4077 | 3039 | error ("Invalid byte code in %s", SDATA (XCAR (tem))); |
845975f5 SM |
3040 | else |
3041 | error ("Invalid byte code"); | |
3042 | } | |
3ae565b3 SM |
3043 | ASET (object, COMPILED_BYTECODE, XCAR (tem)); |
3044 | ASET (object, COMPILED_CONSTANTS, XCDR (tem)); | |
661c7d6e KH |
3045 | } |
3046 | return object; | |
3047 | } | |
db9f0278 | 3048 | \f |
2f592f95 SM |
3049 | /* Return true if SYMBOL currently has a let-binding |
3050 | which was made in the buffer that is now current. */ | |
3051 | ||
3052 | bool | |
3053 | let_shadows_buffer_binding_p (struct Lisp_Symbol *symbol) | |
db9f0278 | 3054 | { |
9349e5f7 | 3055 | union specbinding *p; |
2f592f95 SM |
3056 | Lisp_Object buf = Fcurrent_buffer (); |
3057 | ||
3058 | for (p = specpdl_ptr; p > specpdl; ) | |
3059 | if ((--p)->kind > SPECPDL_LET) | |
3060 | { | |
3061 | struct Lisp_Symbol *let_bound_symbol = XSYMBOL (specpdl_symbol (p)); | |
3062 | eassert (let_bound_symbol->redirect != SYMBOL_VARALIAS); | |
3063 | if (symbol == let_bound_symbol | |
3064 | && EQ (specpdl_where (p), buf)) | |
3065 | return 1; | |
3066 | } | |
3067 | ||
3068 | return 0; | |
3069 | } | |
3070 | ||
3071 | bool | |
3072 | let_shadows_global_binding_p (Lisp_Object symbol) | |
3073 | { | |
9349e5f7 | 3074 | union specbinding *p; |
2f592f95 SM |
3075 | |
3076 | for (p = specpdl_ptr; p > specpdl; ) | |
3077 | if ((--p)->kind >= SPECPDL_LET && EQ (specpdl_symbol (p), symbol)) | |
3078 | return 1; | |
3079 | ||
3080 | return 0; | |
db9f0278 JB |
3081 | } |
3082 | ||
3ec7babc | 3083 | /* `specpdl_ptr' describes which variable is |
4e2db1fe | 3084 | let-bound, so it can be properly undone when we unbind_to. |
3ec7babc SM |
3085 | It can be either a plain SPECPDL_LET or a SPECPDL_LET_LOCAL/DEFAULT. |
3086 | - SYMBOL is the variable being bound. Note that it should not be | |
4e2db1fe SM |
3087 | aliased (i.e. when let-binding V1 that's aliased to V2, we want |
3088 | to record V2 here). | |
3ec7babc SM |
3089 | - WHERE tells us in which buffer the binding took place. |
3090 | This is used for SPECPDL_LET_LOCAL bindings (i.e. bindings to a | |
3091 | buffer-local variable) as well as for SPECPDL_LET_DEFAULT bindings, | |
3092 | i.e. bindings to the default value of a variable which can be | |
3093 | buffer-local. */ | |
4e2db1fe | 3094 | |
db9f0278 | 3095 | void |
d3da34e0 | 3096 | specbind (Lisp_Object symbol, Lisp_Object value) |
db9f0278 | 3097 | { |
ce5b453a SM |
3098 | struct Lisp_Symbol *sym; |
3099 | ||
b7826503 | 3100 | CHECK_SYMBOL (symbol); |
ce5b453a | 3101 | sym = XSYMBOL (symbol); |
719177b3 | 3102 | |
ce5b453a SM |
3103 | start: |
3104 | switch (sym->redirect) | |
719177b3 | 3105 | { |
ce5b453a SM |
3106 | case SYMBOL_VARALIAS: |
3107 | sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start; | |
3108 | case SYMBOL_PLAINVAL: | |
bb8e180f AS |
3109 | /* The most common case is that of a non-constant symbol with a |
3110 | trivial value. Make that as fast as we can. */ | |
9349e5f7 PE |
3111 | specpdl_ptr->let.kind = SPECPDL_LET; |
3112 | specpdl_ptr->let.symbol = symbol; | |
3113 | specpdl_ptr->let.old_value = SYMBOL_VAL (sym); | |
5e301d76 | 3114 | grow_specpdl (); |
bb8e180f AS |
3115 | if (!sym->constant) |
3116 | SET_SYMBOL_VAL (sym, value); | |
3117 | else | |
3118 | set_internal (symbol, value, Qnil, 1); | |
3119 | break; | |
4e2db1fe SM |
3120 | case SYMBOL_LOCALIZED: |
3121 | if (SYMBOL_BLV (sym)->frame_local) | |
3122 | error ("Frame-local vars cannot be let-bound"); | |
3123 | case SYMBOL_FORWARDED: | |
ce5b453a SM |
3124 | { |
3125 | Lisp_Object ovalue = find_symbol_value (symbol); | |
9349e5f7 PE |
3126 | specpdl_ptr->let.kind = SPECPDL_LET_LOCAL; |
3127 | specpdl_ptr->let.symbol = symbol; | |
3128 | specpdl_ptr->let.old_value = ovalue; | |
3129 | specpdl_ptr->let.where = Fcurrent_buffer (); | |
ce5b453a SM |
3130 | |
3131 | eassert (sym->redirect != SYMBOL_LOCALIZED | |
2f592f95 | 3132 | || (EQ (SYMBOL_BLV (sym)->where, Fcurrent_buffer ()))); |
ce5b453a | 3133 | |
2f592f95 SM |
3134 | if (sym->redirect == SYMBOL_LOCALIZED) |
3135 | { | |
3136 | if (!blv_found (SYMBOL_BLV (sym))) | |
9349e5f7 | 3137 | specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT; |
2f592f95 SM |
3138 | } |
3139 | else if (BUFFER_OBJFWDP (SYMBOL_FWD (sym))) | |
ce5b453a | 3140 | { |
ce5b453a SM |
3141 | /* If SYMBOL is a per-buffer variable which doesn't have a |
3142 | buffer-local value here, make the `let' change the global | |
3143 | value by changing the value of SYMBOL in all buffers not | |
3144 | having their own value. This is consistent with what | |
3145 | happens with other buffer-local variables. */ | |
2f592f95 | 3146 | if (NILP (Flocal_variable_p (symbol, Qnil))) |
ce5b453a | 3147 | { |
9349e5f7 | 3148 | specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT; |
5e301d76 | 3149 | grow_specpdl (); |
ce5b453a SM |
3150 | Fset_default (symbol, value); |
3151 | return; | |
3152 | } | |
3153 | } | |
3154 | else | |
9349e5f7 | 3155 | specpdl_ptr->let.kind = SPECPDL_LET; |
ce5b453a | 3156 | |
5e301d76 | 3157 | grow_specpdl (); |
94b612ad | 3158 | set_internal (symbol, value, Qnil, 1); |
ce5b453a SM |
3159 | break; |
3160 | } | |
1088b922 | 3161 | default: emacs_abort (); |
9ab90667 | 3162 | } |
db9f0278 JB |
3163 | } |
3164 | ||
f4b1eb36 PE |
3165 | /* Push unwind-protect entries of various types. */ |
3166 | ||
db9f0278 | 3167 | void |
27e498e6 | 3168 | record_unwind_protect (void (*function) (Lisp_Object), Lisp_Object arg) |
db9f0278 | 3169 | { |
9349e5f7 PE |
3170 | specpdl_ptr->unwind.kind = SPECPDL_UNWIND; |
3171 | specpdl_ptr->unwind.func = function; | |
3172 | specpdl_ptr->unwind.arg = arg; | |
5e301d76 | 3173 | grow_specpdl (); |
db9f0278 JB |
3174 | } |
3175 | ||
27e498e6 PE |
3176 | void |
3177 | record_unwind_protect_ptr (void (*function) (void *), void *arg) | |
3178 | { | |
3179 | specpdl_ptr->unwind_ptr.kind = SPECPDL_UNWIND_PTR; | |
3180 | specpdl_ptr->unwind_ptr.func = function; | |
3181 | specpdl_ptr->unwind_ptr.arg = arg; | |
3182 | grow_specpdl (); | |
3183 | } | |
3184 | ||
3185 | void | |
3186 | record_unwind_protect_int (void (*function) (int), int arg) | |
3187 | { | |
3188 | specpdl_ptr->unwind_int.kind = SPECPDL_UNWIND_INT; | |
3189 | specpdl_ptr->unwind_int.func = function; | |
3190 | specpdl_ptr->unwind_int.arg = arg; | |
3191 | grow_specpdl (); | |
3192 | } | |
3193 | ||
3194 | void | |
3195 | record_unwind_protect_void (void (*function) (void)) | |
3196 | { | |
3197 | specpdl_ptr->unwind_void.kind = SPECPDL_UNWIND_VOID; | |
3198 | specpdl_ptr->unwind_void.func = function; | |
3199 | grow_specpdl (); | |
3200 | } | |
3201 | ||
a0931322 PE |
3202 | static void |
3203 | do_nothing (void) | |
3204 | {} | |
3205 | ||
f4b1eb36 PE |
3206 | /* Push an unwind-protect entry that does nothing, so that |
3207 | set_unwind_protect_ptr can overwrite it later. */ | |
3208 | ||
3209 | void | |
3210 | record_unwind_protect_nothing (void) | |
3211 | { | |
3212 | record_unwind_protect_void (do_nothing); | |
3213 | } | |
3214 | ||
3215 | /* Clear the unwind-protect entry COUNT, so that it does nothing. | |
3216 | It need not be at the top of the stack. */ | |
3217 | ||
a0931322 PE |
3218 | void |
3219 | clear_unwind_protect (ptrdiff_t count) | |
3220 | { | |
3221 | union specbinding *p = specpdl + count; | |
3222 | p->unwind_void.kind = SPECPDL_UNWIND_VOID; | |
3223 | p->unwind_void.func = do_nothing; | |
3224 | } | |
3225 | ||
f4b1eb36 PE |
3226 | /* Set the unwind-protect entry COUNT so that it invokes FUNC (ARG). |
3227 | It need not be at the top of the stack. Discard the entry's | |
3228 | previous value without invoking it. */ | |
3229 | ||
94fcd171 PE |
3230 | void |
3231 | set_unwind_protect (ptrdiff_t count, void (*func) (Lisp_Object), | |
3232 | Lisp_Object arg) | |
3233 | { | |
3234 | union specbinding *p = specpdl + count; | |
3235 | p->unwind.kind = SPECPDL_UNWIND; | |
3236 | p->unwind.func = func; | |
3237 | p->unwind.arg = arg; | |
3238 | } | |
3239 | ||
a0931322 PE |
3240 | void |
3241 | set_unwind_protect_ptr (ptrdiff_t count, void (*func) (void *), void *arg) | |
3242 | { | |
3243 | union specbinding *p = specpdl + count; | |
3244 | p->unwind_ptr.kind = SPECPDL_UNWIND_PTR; | |
3245 | p->unwind_ptr.func = func; | |
3246 | p->unwind_ptr.arg = arg; | |
3247 | } | |
3248 | ||
f4b1eb36 PE |
3249 | /* Pop and execute entries from the unwind-protect stack until the |
3250 | depth COUNT is reached. Return VALUE. */ | |
3251 | ||
db9f0278 | 3252 | Lisp_Object |
d311d28c | 3253 | unbind_to (ptrdiff_t count, Lisp_Object value) |
db9f0278 | 3254 | { |
5a073f50 KS |
3255 | Lisp_Object quitf = Vquit_flag; |
3256 | struct gcpro gcpro1, gcpro2; | |
db9f0278 | 3257 | |
5a073f50 | 3258 | GCPRO2 (value, quitf); |
db9f0278 JB |
3259 | Vquit_flag = Qnil; |
3260 | ||
3261 | while (specpdl_ptr != specpdl + count) | |
3262 | { | |
9349e5f7 PE |
3263 | /* Decrement specpdl_ptr before we do the work to unbind it, so |
3264 | that an error in unbinding won't try to unbind the same entry | |
3265 | again. Take care to copy any parts of the binding needed | |
3266 | before invoking any code that can make more bindings. */ | |
eb700b82 | 3267 | |
9349e5f7 | 3268 | specpdl_ptr--; |
611a8f8c | 3269 | |
9349e5f7 | 3270 | switch (specpdl_ptr->kind) |
719177b3 | 3271 | { |
2f592f95 | 3272 | case SPECPDL_UNWIND: |
27e498e6 PE |
3273 | specpdl_ptr->unwind.func (specpdl_ptr->unwind.arg); |
3274 | break; | |
3275 | case SPECPDL_UNWIND_PTR: | |
3276 | specpdl_ptr->unwind_ptr.func (specpdl_ptr->unwind_ptr.arg); | |
3277 | break; | |
3278 | case SPECPDL_UNWIND_INT: | |
3279 | specpdl_ptr->unwind_int.func (specpdl_ptr->unwind_int.arg); | |
3280 | break; | |
3281 | case SPECPDL_UNWIND_VOID: | |
3282 | specpdl_ptr->unwind_void.func (); | |
2f592f95 | 3283 | break; |
56ea7291 SM |
3284 | case SPECPDL_BACKTRACE: |
3285 | break; | |
2f592f95 | 3286 | case SPECPDL_LET: |
a104f656 SM |
3287 | { /* If variable has a trivial value (no forwarding), we can |
3288 | just set it. No need to check for constant symbols here, | |
3289 | since that was already done by specbind. */ | |
3290 | struct Lisp_Symbol *sym = XSYMBOL (specpdl_symbol (specpdl_ptr)); | |
3291 | if (sym->redirect == SYMBOL_PLAINVAL) | |
3292 | { | |
3293 | SET_SYMBOL_VAL (sym, specpdl_old_value (specpdl_ptr)); | |
3294 | break; | |
3295 | } | |
3296 | else | |
3297 | { /* FALLTHROUGH!! | |
3298 | NOTE: we only ever come here if make_local_foo was used for | |
3299 | the first time on this var within this let. */ | |
3300 | } | |
3301 | } | |
56ea7291 SM |
3302 | case SPECPDL_LET_DEFAULT: |
3303 | Fset_default (specpdl_symbol (specpdl_ptr), | |
3304 | specpdl_old_value (specpdl_ptr)); | |
2f592f95 SM |
3305 | break; |
3306 | case SPECPDL_LET_LOCAL: | |
56ea7291 | 3307 | { |
9349e5f7 PE |
3308 | Lisp_Object symbol = specpdl_symbol (specpdl_ptr); |
3309 | Lisp_Object where = specpdl_where (specpdl_ptr); | |
3310 | Lisp_Object old_value = specpdl_old_value (specpdl_ptr); | |
2f592f95 SM |
3311 | eassert (BUFFERP (where)); |
3312 | ||
2f592f95 SM |
3313 | /* If this was a local binding, reset the value in the appropriate |
3314 | buffer, but only if that buffer's binding still exists. */ | |
56ea7291 | 3315 | if (!NILP (Flocal_variable_p (symbol, where))) |
9349e5f7 | 3316 | set_internal (symbol, old_value, where, 1); |
2f592f95 SM |
3317 | } |
3318 | break; | |
719177b3 | 3319 | } |
db9f0278 | 3320 | } |
177c0ea7 | 3321 | |
5a073f50 KS |
3322 | if (NILP (Vquit_flag) && !NILP (quitf)) |
3323 | Vquit_flag = quitf; | |
db9f0278 JB |
3324 | |
3325 | UNGCPRO; | |
db9f0278 JB |
3326 | return value; |
3327 | } | |
b9598260 | 3328 | |
4a330052 | 3329 | DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0, |
b9598260 SM |
3330 | doc: /* Return non-nil if SYMBOL's global binding has been declared special. |
3331 | A special variable is one that will be bound dynamically, even in a | |
3332 | context where binding is lexical by default. */) | |
c566235d | 3333 | (Lisp_Object symbol) |
b9598260 SM |
3334 | { |
3335 | CHECK_SYMBOL (symbol); | |
3336 | return XSYMBOL (symbol)->declared_special ? Qt : Qnil; | |
3337 | } | |
3338 | ||
db9f0278 | 3339 | \f |
db9f0278 | 3340 | DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0, |
9dbc9081 PJ |
3341 | doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG. |
3342 | The debugger is entered when that frame exits, if the flag is non-nil. */) | |
5842a27b | 3343 | (Lisp_Object level, Lisp_Object flag) |
db9f0278 | 3344 | { |
9349e5f7 | 3345 | union specbinding *pdl = backtrace_top (); |
d311d28c | 3346 | register EMACS_INT i; |
db9f0278 | 3347 | |
b7826503 | 3348 | CHECK_NUMBER (level); |
db9f0278 | 3349 | |
2f592f95 SM |
3350 | for (i = 0; backtrace_p (pdl) && i < XINT (level); i++) |
3351 | pdl = backtrace_next (pdl); | |
db9f0278 | 3352 | |
2f592f95 SM |
3353 | if (backtrace_p (pdl)) |
3354 | set_backtrace_debug_on_exit (pdl, !NILP (flag)); | |
db9f0278 JB |
3355 | |
3356 | return flag; | |
3357 | } | |
3358 | ||
3359 | DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "", | |
9dbc9081 PJ |
3360 | doc: /* Print a trace of Lisp function calls currently active. |
3361 | Output stream used is value of `standard-output'. */) | |
5842a27b | 3362 | (void) |
db9f0278 | 3363 | { |
9349e5f7 | 3364 | union specbinding *pdl = backtrace_top (); |
db9f0278 | 3365 | Lisp_Object tem; |
d4b6d95d | 3366 | Lisp_Object old_print_level = Vprint_level; |
db9f0278 | 3367 | |
d4b6d95d LMI |
3368 | if (NILP (Vprint_level)) |
3369 | XSETFASTINT (Vprint_level, 8); | |
db9f0278 | 3370 | |
2f592f95 | 3371 | while (backtrace_p (pdl)) |
db9f0278 | 3372 | { |
2f592f95 SM |
3373 | write_string (backtrace_debug_on_exit (pdl) ? "* " : " ", 2); |
3374 | if (backtrace_nargs (pdl) == UNEVALLED) | |
db9f0278 | 3375 | { |
2f592f95 SM |
3376 | Fprin1 (Fcons (backtrace_function (pdl), *backtrace_args (pdl)), |
3377 | Qnil); | |
b6703b02 | 3378 | write_string ("\n", -1); |
db9f0278 JB |
3379 | } |
3380 | else | |
3381 | { | |
2f592f95 | 3382 | tem = backtrace_function (pdl); |
f6d62986 | 3383 | Fprin1 (tem, Qnil); /* This can QUIT. */ |
db9f0278 | 3384 | write_string ("(", -1); |
2f592f95 SM |
3385 | { |
3386 | ptrdiff_t i; | |
3387 | for (i = 0; i < backtrace_nargs (pdl); i++) | |
3388 | { | |
3389 | if (i) write_string (" ", -1); | |
3390 | Fprin1 (backtrace_args (pdl)[i], Qnil); | |
3391 | } | |
3392 | } | |
b6703b02 | 3393 | write_string (")\n", -1); |
db9f0278 | 3394 | } |
2f592f95 | 3395 | pdl = backtrace_next (pdl); |
db9f0278 JB |
3396 | } |
3397 | ||
d4b6d95d | 3398 | Vprint_level = old_print_level; |
db9f0278 JB |
3399 | return Qnil; |
3400 | } | |
3401 | ||
d5a7a9d9 | 3402 | static union specbinding * |
56ea7291 SM |
3403 | get_backtrace_frame (Lisp_Object nframes, Lisp_Object base) |
3404 | { | |
3405 | union specbinding *pdl = backtrace_top (); | |
3406 | register EMACS_INT i; | |
3407 | ||
3408 | CHECK_NATNUM (nframes); | |
3409 | ||
3410 | if (!NILP (base)) | |
3411 | { /* Skip up to `base'. */ | |
3412 | base = Findirect_function (base, Qt); | |
3413 | while (backtrace_p (pdl) | |
3414 | && !EQ (base, Findirect_function (backtrace_function (pdl), Qt))) | |
3415 | pdl = backtrace_next (pdl); | |
3416 | } | |
3417 | ||
3418 | /* Find the frame requested. */ | |
3419 | for (i = XFASTINT (nframes); i > 0 && backtrace_p (pdl); i--) | |
3420 | pdl = backtrace_next (pdl); | |
3421 | ||
3422 | return pdl; | |
3423 | } | |
3424 | ||
3425 | DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 2, NULL, | |
9dbc9081 PJ |
3426 | doc: /* Return the function and arguments NFRAMES up from current execution point. |
3427 | If that frame has not evaluated the arguments yet (or is a special form), | |
3428 | the value is (nil FUNCTION ARG-FORMS...). | |
3429 | If that frame has evaluated its arguments and called its function already, | |
3430 | the value is (t FUNCTION ARG-VALUES...). | |
3431 | A &rest arg is represented as the tail of the list ARG-VALUES. | |
3432 | FUNCTION is whatever was supplied as car of evaluated list, | |
3433 | or a lambda expression for macro calls. | |
56ea7291 SM |
3434 | If NFRAMES is more than the number of frames, the value is nil. |
3435 | If BASE is non-nil, it should be a function and NFRAMES counts from its | |
3436 | nearest activation frame. */) | |
3437 | (Lisp_Object nframes, Lisp_Object base) | |
db9f0278 | 3438 | { |
56ea7291 | 3439 | union specbinding *pdl = get_backtrace_frame (nframes, base); |
db9f0278 | 3440 | |
2f592f95 | 3441 | if (!backtrace_p (pdl)) |
db9f0278 | 3442 | return Qnil; |
2f592f95 SM |
3443 | if (backtrace_nargs (pdl) == UNEVALLED) |
3444 | return Fcons (Qnil, | |
3445 | Fcons (backtrace_function (pdl), *backtrace_args (pdl))); | |
db9f0278 JB |
3446 | else |
3447 | { | |
2f592f95 | 3448 | Lisp_Object tem = Flist (backtrace_nargs (pdl), backtrace_args (pdl)); |
db9f0278 | 3449 | |
2f592f95 | 3450 | return Fcons (Qt, Fcons (backtrace_function (pdl), tem)); |
db9f0278 JB |
3451 | } |
3452 | } | |
a2ff3819 | 3453 | |
56ea7291 SM |
3454 | /* For backtrace-eval, we want to temporarily unwind the last few elements of |
3455 | the specpdl stack, and then rewind them. We store the pre-unwind values | |
3456 | directly in the pre-existing specpdl elements (i.e. we swap the current | |
3457 | value and the old value stored in the specpdl), kind of like the inplace | |
3458 | pointer-reversal trick. As it turns out, the rewind does the same as the | |
94fea300 | 3459 | unwind, except it starts from the other end of the specpdl stack, so we use |
56ea7291 | 3460 | the same function for both unwind and rewind. */ |
d5a7a9d9 | 3461 | static void |
56ea7291 SM |
3462 | backtrace_eval_unrewind (int distance) |
3463 | { | |
3464 | union specbinding *tmp = specpdl_ptr; | |
3465 | int step = -1; | |
3466 | if (distance < 0) | |
3467 | { /* It's a rewind rather than unwind. */ | |
3468 | tmp += distance - 1; | |
3469 | step = 1; | |
3470 | distance = -distance; | |
3471 | } | |
3472 | ||
3473 | for (; distance > 0; distance--) | |
3474 | { | |
3475 | tmp += step; | |
3476 | /* */ | |
3477 | switch (tmp->kind) | |
3478 | { | |
3479 | /* FIXME: Ideally we'd like to "temporarily unwind" (some of) those | |
3480 | unwind_protect, but the problem is that we don't know how to | |
3481 | rewind them afterwards. */ | |
3482 | case SPECPDL_UNWIND: | |
3483 | case SPECPDL_UNWIND_PTR: | |
3484 | case SPECPDL_UNWIND_INT: | |
3485 | case SPECPDL_UNWIND_VOID: | |
3486 | case SPECPDL_BACKTRACE: | |
3487 | break; | |
3488 | case SPECPDL_LET: | |
a104f656 SM |
3489 | { /* If variable has a trivial value (no forwarding), we can |
3490 | just set it. No need to check for constant symbols here, | |
3491 | since that was already done by specbind. */ | |
3492 | struct Lisp_Symbol *sym = XSYMBOL (specpdl_symbol (tmp)); | |
3493 | if (sym->redirect == SYMBOL_PLAINVAL) | |
3494 | { | |
3495 | Lisp_Object old_value = specpdl_old_value (tmp); | |
3496 | set_specpdl_old_value (tmp, SYMBOL_VAL (sym)); | |
3497 | SET_SYMBOL_VAL (sym, old_value); | |
3498 | break; | |
3499 | } | |
3500 | else | |
3501 | { /* FALLTHROUGH!! | |
3502 | NOTE: we only ever come here if make_local_foo was used for | |
3503 | the first time on this var within this let. */ | |
3504 | } | |
3505 | } | |
56ea7291 SM |
3506 | case SPECPDL_LET_DEFAULT: |
3507 | { | |
3508 | Lisp_Object sym = specpdl_symbol (tmp); | |
3509 | Lisp_Object old_value = specpdl_old_value (tmp); | |
3510 | set_specpdl_old_value (tmp, Fdefault_value (sym)); | |
3511 | Fset_default (sym, old_value); | |
3512 | } | |
3513 | break; | |
3514 | case SPECPDL_LET_LOCAL: | |
3515 | { | |
3516 | Lisp_Object symbol = specpdl_symbol (tmp); | |
3517 | Lisp_Object where = specpdl_where (tmp); | |
3518 | Lisp_Object old_value = specpdl_old_value (tmp); | |
3519 | eassert (BUFFERP (where)); | |
3520 | ||
3521 | /* If this was a local binding, reset the value in the appropriate | |
3522 | buffer, but only if that buffer's binding still exists. */ | |
3523 | if (!NILP (Flocal_variable_p (symbol, where))) | |
3524 | { | |
3525 | set_specpdl_old_value | |
3526 | (tmp, Fbuffer_local_value (symbol, where)); | |
3527 | set_internal (symbol, old_value, where, 1); | |
3528 | } | |
3529 | } | |
3530 | break; | |
3531 | } | |
3532 | } | |
3533 | } | |
3534 | ||
3535 | DEFUN ("backtrace-eval", Fbacktrace_eval, Sbacktrace_eval, 2, 3, NULL, | |
3536 | doc: /* Evaluate EXP in the context of some activation frame. | |
3537 | NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */) | |
3538 | (Lisp_Object exp, Lisp_Object nframes, Lisp_Object base) | |
3539 | { | |
3540 | union specbinding *pdl = get_backtrace_frame (nframes, base); | |
3541 | ptrdiff_t count = SPECPDL_INDEX (); | |
3542 | ptrdiff_t distance = specpdl_ptr - pdl; | |
3543 | eassert (distance >= 0); | |
3544 | ||
3545 | if (!backtrace_p (pdl)) | |
3546 | error ("Activation frame not found!"); | |
3547 | ||
3548 | backtrace_eval_unrewind (distance); | |
3549 | record_unwind_protect_int (backtrace_eval_unrewind, -distance); | |
3550 | ||
3551 | /* Use eval_sub rather than Feval since the main motivation behind | |
3552 | backtrace-eval is to be able to get/set the value of lexical variables | |
3553 | from the debugger. */ | |
3554 | return unbind_to (count, eval_sub (exp)); | |
3555 | } | |
db9f0278 | 3556 | \f |
4ce0541e | 3557 | void |
2f592f95 | 3558 | mark_specpdl (void) |
4ce0541e | 3559 | { |
9349e5f7 | 3560 | union specbinding *pdl; |
2f592f95 | 3561 | for (pdl = specpdl; pdl != specpdl_ptr; pdl++) |
4ce0541e | 3562 | { |
2f592f95 SM |
3563 | switch (pdl->kind) |
3564 | { | |
3565 | case SPECPDL_UNWIND: | |
3566 | mark_object (specpdl_arg (pdl)); | |
3567 | break; | |
9349e5f7 | 3568 | |
2f592f95 SM |
3569 | case SPECPDL_BACKTRACE: |
3570 | { | |
3571 | ptrdiff_t nargs = backtrace_nargs (pdl); | |
3572 | mark_object (backtrace_function (pdl)); | |
3573 | if (nargs == UNEVALLED) | |
3574 | nargs = 1; | |
3575 | while (nargs--) | |
3576 | mark_object (backtrace_args (pdl)[nargs]); | |
3577 | } | |
3578 | break; | |
9349e5f7 | 3579 | |
2f592f95 SM |
3580 | case SPECPDL_LET_DEFAULT: |
3581 | case SPECPDL_LET_LOCAL: | |
3582 | mark_object (specpdl_where (pdl)); | |
9349e5f7 | 3583 | /* Fall through. */ |
2f592f95 SM |
3584 | case SPECPDL_LET: |
3585 | mark_object (specpdl_symbol (pdl)); | |
3586 | mark_object (specpdl_old_value (pdl)); | |
9349e5f7 | 3587 | break; |
2f592f95 SM |
3588 | } |
3589 | } | |
3590 | } | |
3591 | ||
3592 | void | |
3593 | get_backtrace (Lisp_Object array) | |
3594 | { | |
9349e5f7 | 3595 | union specbinding *pdl = backtrace_next (backtrace_top ()); |
2f592f95 | 3596 | ptrdiff_t i = 0, asize = ASIZE (array); |
4ce0541e | 3597 | |
2f592f95 SM |
3598 | /* Copy the backtrace contents into working memory. */ |
3599 | for (; i < asize; i++) | |
3600 | { | |
3601 | if (backtrace_p (pdl)) | |
3602 | { | |
3603 | ASET (array, i, backtrace_function (pdl)); | |
3604 | pdl = backtrace_next (pdl); | |
3605 | } | |
4ce0541e | 3606 | else |
2f592f95 | 3607 | ASET (array, i, Qnil); |
4ce0541e SM |
3608 | } |
3609 | } | |
2f592f95 SM |
3610 | |
3611 | Lisp_Object backtrace_top_function (void) | |
3612 | { | |
9349e5f7 | 3613 | union specbinding *pdl = backtrace_top (); |
2f592f95 SM |
3614 | return (backtrace_p (pdl) ? backtrace_function (pdl) : Qnil); |
3615 | } | |
4ce0541e | 3616 | |
dfcf069d | 3617 | void |
d3da34e0 | 3618 | syms_of_eval (void) |
db9f0278 | 3619 | { |
29208e82 | 3620 | DEFVAR_INT ("max-specpdl-size", max_specpdl_size, |
fb7ada5f | 3621 | doc: /* Limit on number of Lisp variable bindings and `unwind-protect's. |
9f5903bb | 3622 | If Lisp code tries to increase the total number past this amount, |
2520dc0c RS |
3623 | an error is signaled. |
3624 | You can safely use a value considerably larger than the default value, | |
3625 | if that proves inconveniently small. However, if you increase it too far, | |
3626 | Emacs could run out of memory trying to make the stack bigger. */); | |
db9f0278 | 3627 | |
29208e82 | 3628 | DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth, |
fb7ada5f | 3629 | doc: /* Limit on depth in `eval', `apply' and `funcall' before error. |
2520dc0c RS |
3630 | |
3631 | This limit serves to catch infinite recursions for you before they cause | |
9dbc9081 PJ |
3632 | actual stack overflow in C, which would be fatal for Emacs. |
3633 | You can safely make it considerably larger than its default value, | |
2520dc0c RS |
3634 | if that proves inconveniently small. However, if you increase it too far, |
3635 | Emacs could overflow the real C stack, and crash. */); | |
db9f0278 | 3636 | |
29208e82 | 3637 | DEFVAR_LISP ("quit-flag", Vquit_flag, |
9dbc9081 | 3638 | doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil. |
42ed718e RS |
3639 | If the value is t, that means do an ordinary quit. |
3640 | If the value equals `throw-on-input', that means quit by throwing | |
3641 | to the tag specified in `throw-on-input'; it's for handling `while-no-input'. | |
3642 | Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit', | |
3643 | but `inhibit-quit' non-nil prevents anything from taking notice of that. */); | |
db9f0278 JB |
3644 | Vquit_flag = Qnil; |
3645 | ||
29208e82 | 3646 | DEFVAR_LISP ("inhibit-quit", Vinhibit_quit, |
9dbc9081 PJ |
3647 | doc: /* Non-nil inhibits C-g quitting from happening immediately. |
3648 | Note that `quit-flag' will still be set by typing C-g, | |
3649 | so a quit will be signaled as soon as `inhibit-quit' is nil. | |
3650 | To prevent this happening, set `quit-flag' to nil | |
3651 | before making `inhibit-quit' nil. */); | |
db9f0278 JB |
3652 | Vinhibit_quit = Qnil; |
3653 | ||
cd3520a4 JB |
3654 | DEFSYM (Qinhibit_quit, "inhibit-quit"); |
3655 | DEFSYM (Qautoload, "autoload"); | |
45b82ad0 | 3656 | DEFSYM (Qinhibit_debugger, "inhibit-debugger"); |
cd3520a4 JB |
3657 | DEFSYM (Qmacro, "macro"); |
3658 | DEFSYM (Qdeclare, "declare"); | |
177c0ea7 | 3659 | |
db9f0278 JB |
3660 | /* Note that the process handling also uses Qexit, but we don't want |
3661 | to staticpro it twice, so we just do it here. */ | |
cd3520a4 | 3662 | DEFSYM (Qexit, "exit"); |
b9598260 | 3663 | |
cd3520a4 JB |
3664 | DEFSYM (Qinteractive, "interactive"); |
3665 | DEFSYM (Qcommandp, "commandp"); | |
cd3520a4 JB |
3666 | DEFSYM (Qand_rest, "&rest"); |
3667 | DEFSYM (Qand_optional, "&optional"); | |
3668 | DEFSYM (Qclosure, "closure"); | |
3669 | DEFSYM (Qdebug, "debug"); | |
f01cbfdd | 3670 | |
45b82ad0 SM |
3671 | DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger, |
3672 | doc: /* Non-nil means never enter the debugger. | |
3673 | Normally set while the debugger is already active, to avoid recursive | |
3674 | invocations. */); | |
3675 | Vinhibit_debugger = Qnil; | |
3676 | ||
29208e82 | 3677 | DEFVAR_LISP ("debug-on-error", Vdebug_on_error, |
fb7ada5f | 3678 | doc: /* Non-nil means enter debugger if an error is signaled. |
9dbc9081 PJ |
3679 | Does not apply to errors handled by `condition-case' or those |
3680 | matched by `debug-ignored-errors'. | |
3681 | If the value is a list, an error only means to enter the debugger | |
3682 | if one of its condition symbols appears in the list. | |
3683 | When you evaluate an expression interactively, this variable | |
3684 | is temporarily non-nil if `eval-expression-debug-on-error' is non-nil. | |
fbbdcf2f | 3685 | The command `toggle-debug-on-error' toggles this. |
45b82ad0 | 3686 | See also the variable `debug-on-quit' and `inhibit-debugger'. */); |
128c0f66 | 3687 | Vdebug_on_error = Qnil; |
db9f0278 | 3688 | |
29208e82 | 3689 | DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors, |
fb7ada5f | 3690 | doc: /* List of errors for which the debugger should not be called. |
9dbc9081 PJ |
3691 | Each element may be a condition-name or a regexp that matches error messages. |
3692 | If any element applies to a given error, that error skips the debugger | |
3693 | and just returns to top level. | |
3694 | This overrides the variable `debug-on-error'. | |
3695 | It does not apply to errors handled by `condition-case'. */); | |
fc950e09 KH |
3696 | Vdebug_ignored_errors = Qnil; |
3697 | ||
29208e82 | 3698 | DEFVAR_BOOL ("debug-on-quit", debug_on_quit, |
fb7ada5f | 3699 | doc: /* Non-nil means enter debugger if quit is signaled (C-g, for example). |
82fc29a1 | 3700 | Does not apply if quit is handled by a `condition-case'. */); |
db9f0278 JB |
3701 | debug_on_quit = 0; |
3702 | ||
29208e82 | 3703 | DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call, |
9dbc9081 | 3704 | doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */); |
db9f0278 | 3705 | |
29208e82 | 3706 | DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue, |
9dbc9081 PJ |
3707 | doc: /* Non-nil means debugger may continue execution. |
3708 | This is nil when the debugger is called under circumstances where it | |
3709 | might not be safe to continue. */); | |
dac204bc | 3710 | debugger_may_continue = 1; |
556d7314 | 3711 | |
29208e82 | 3712 | DEFVAR_LISP ("debugger", Vdebugger, |
9dbc9081 PJ |
3713 | doc: /* Function to call to invoke debugger. |
3714 | If due to frame exit, args are `exit' and the value being returned; | |
3715 | this function's value will be returned instead of that. | |
3716 | If due to error, args are `error' and a list of the args to `signal'. | |
3717 | If due to `apply' or `funcall' entry, one arg, `lambda'. | |
3718 | If due to `eval' entry, one arg, t. */); | |
db9f0278 JB |
3719 | Vdebugger = Qnil; |
3720 | ||
29208e82 | 3721 | DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function, |
9dbc9081 PJ |
3722 | doc: /* If non-nil, this is a function for `signal' to call. |
3723 | It receives the same arguments that `signal' was given. | |
3724 | The Edebug package uses this to regain control. */); | |
61ede770 RS |
3725 | Vsignal_hook_function = Qnil; |
3726 | ||
29208e82 | 3727 | DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal, |
fb7ada5f | 3728 | doc: /* Non-nil means call the debugger regardless of condition handlers. |
9dbc9081 PJ |
3729 | Note that `debug-on-error', `debug-on-quit' and friends |
3730 | still determine whether to handle the particular condition. */); | |
57a6e758 | 3731 | Vdebug_on_signal = Qnil; |
61ede770 | 3732 | |
b38b1ec0 | 3733 | /* When lexical binding is being used, |
61b108cc | 3734 | Vinternal_interpreter_environment is non-nil, and contains an alist |
b38b1ec0 SM |
3735 | of lexically-bound variable, or (t), indicating an empty |
3736 | environment. The lisp name of this variable would be | |
3737 | `internal-interpreter-environment' if it weren't hidden. | |
3738 | Every element of this list can be either a cons (VAR . VAL) | |
3739 | specifying a lexical binding, or a single symbol VAR indicating | |
3740 | that this variable should use dynamic scoping. */ | |
61b108cc SM |
3741 | DEFSYM (Qinternal_interpreter_environment, |
3742 | "internal-interpreter-environment"); | |
b38b1ec0 SM |
3743 | DEFVAR_LISP ("internal-interpreter-environment", |
3744 | Vinternal_interpreter_environment, | |
b9598260 SM |
3745 | doc: /* If non-nil, the current lexical environment of the lisp interpreter. |
3746 | When lexical binding is not being used, this variable is nil. | |
3747 | A value of `(t)' indicates an empty environment, otherwise it is an | |
3748 | alist of active lexical bindings. */); | |
3749 | Vinternal_interpreter_environment = Qnil; | |
c80e3b4a | 3750 | /* Don't export this variable to Elisp, so no one can mess with it |
b38b1ec0 SM |
3751 | (Just imagine if someone makes it buffer-local). */ |
3752 | Funintern (Qinternal_interpreter_environment, Qnil); | |
b9598260 | 3753 | |
cd3520a4 | 3754 | DEFSYM (Vrun_hooks, "run-hooks"); |
db9f0278 JB |
3755 | |
3756 | staticpro (&Vautoload_queue); | |
3757 | Vautoload_queue = Qnil; | |
a2ff3819 GM |
3758 | staticpro (&Vsignaling_function); |
3759 | Vsignaling_function = Qnil; | |
db9f0278 | 3760 | |
d1f55f16 CY |
3761 | inhibit_lisp_code = Qnil; |
3762 | ||
db9f0278 JB |
3763 | defsubr (&Sor); |
3764 | defsubr (&Sand); | |
3765 | defsubr (&Sif); | |
3766 | defsubr (&Scond); | |
3767 | defsubr (&Sprogn); | |
3768 | defsubr (&Sprog1); | |
3769 | defsubr (&Sprog2); | |
3770 | defsubr (&Ssetq); | |
3771 | defsubr (&Squote); | |
3772 | defsubr (&Sfunction); | |
a104f656 SM |
3773 | defsubr (&Sdefault_toplevel_value); |
3774 | defsubr (&Sset_default_toplevel_value); | |
db9f0278 | 3775 | defsubr (&Sdefvar); |
19cebf5a | 3776 | defsubr (&Sdefvaralias); |
db9f0278 | 3777 | defsubr (&Sdefconst); |
513749ee | 3778 | defsubr (&Smake_var_non_special); |
db9f0278 JB |
3779 | defsubr (&Slet); |
3780 | defsubr (&SletX); | |
3781 | defsubr (&Swhile); | |
3782 | defsubr (&Smacroexpand); | |
3783 | defsubr (&Scatch); | |
3784 | defsubr (&Sthrow); | |
3785 | defsubr (&Sunwind_protect); | |
3786 | defsubr (&Scondition_case); | |
3787 | defsubr (&Ssignal); | |
db9f0278 JB |
3788 | defsubr (&Scommandp); |
3789 | defsubr (&Sautoload); | |
7abaf5cc | 3790 | defsubr (&Sautoload_do_load); |
db9f0278 JB |
3791 | defsubr (&Seval); |
3792 | defsubr (&Sapply); | |
3793 | defsubr (&Sfuncall); | |
ff936e53 SM |
3794 | defsubr (&Srun_hooks); |
3795 | defsubr (&Srun_hook_with_args); | |
3796 | defsubr (&Srun_hook_with_args_until_success); | |
3797 | defsubr (&Srun_hook_with_args_until_failure); | |
f6d62986 | 3798 | defsubr (&Srun_hook_wrapped); |
661c7d6e | 3799 | defsubr (&Sfetch_bytecode); |
db9f0278 JB |
3800 | defsubr (&Sbacktrace_debug); |
3801 | defsubr (&Sbacktrace); | |
3802 | defsubr (&Sbacktrace_frame); | |
56ea7291 | 3803 | defsubr (&Sbacktrace_eval); |
4a330052 | 3804 | defsubr (&Sspecial_variable_p); |
b9598260 | 3805 | defsubr (&Sfunctionp); |
db9f0278 | 3806 | } |