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