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