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