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