Mercurial > hg > xemacs-beta
annotate src/eval.c @ 4642:48b45a606961
Support #'function-arglist with built-in special forms.
Also fix a couple of bugs in lisp/help.el.
lisp/ChangeLog addition:
2009-06-14 Aidan Kehoe <kehoea@parhasard.net>
* help.el (describe-function-1):
Check macro-p, not macrop, when describing whether a symbol has an
associated macro or an associated function. Relevant with
autoloaded macros.
(function-arglist):
Accept multi-line arglists in built-in functions, as found in
#'write-region-internal. #'dontusethis-set-symbol-value-handler
is still broken for other reasons.
src/ChangeLog addition:
2009-06-14 Aidan Kehoe <kehoea@parhasard.net>
* eval.c (For):
* eval.c (Fand):
* eval.c (Fif):
* eval.c (Fwhen):
* eval.c (Fcond):
* eval.c (Fprogn):
* eval.c (Fprog1):
* eval.c (Fprog2):
* eval.c (FletX):
* eval.c (Flet):
* eval.c (Fwhile):
* eval.c (Fdefvar):
* eval.c (Fdefconst):
* eval.c (Frun_hooks):
* eval.c (Frun_hooks_with_args):
* eval.c (Frun_hooks_with_args_until_success):
* eval.c (Frun_hooks_with_args_until_failure):
Add arguments information, understood by #'function-arglist, to
all these special forms, functions and macros. Remove the
argument information that was already there that was not
understood by #'function-arglist.
author | Aidan Kehoe <kehoea@parhasard.net> |
---|---|
date | Sun, 14 Jun 2009 15:07:13 +0100 |
parents | 9dd42cb187ed |
children | f8d7d8202635 |
rev | line source |
---|---|
428 | 1 /* Evaluator for XEmacs Lisp interpreter. |
2 Copyright (C) 1985-1987, 1992-1994 Free Software Foundation, Inc. | |
3 Copyright (C) 1995 Sun Microsystems, Inc. | |
2421 | 4 Copyright (C) 2000, 2001, 2002, 2003, 2004 Ben Wing. |
428 | 5 |
6 This file is part of XEmacs. | |
7 | |
8 XEmacs is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
10 Free Software Foundation; either version 2, or (at your option) any | |
11 later version. | |
12 | |
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with XEmacs; see the file COPYING. If not, write to | |
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
21 Boston, MA 02111-1307, USA. */ | |
22 | |
23 /* Synched up with: FSF 19.30 (except for Fsignal), Mule 2.0. */ | |
24 | |
853 | 25 /* Authorship: |
26 | |
27 Based on code from pre-release FSF 19, c. 1991. | |
28 Some work by Richard Mlynarik long ago (c. 1993?) -- | |
29 added call-with-condition-handler; synch. up to released FSF 19.7 | |
30 for lemacs 19.8. some signal changes. | |
31 Various work by Ben Wing, 1995-1996: | |
32 added all stuff dealing with trapping errors, suspended-errors, etc. | |
33 added most Fsignal front ends. | |
34 added warning code. | |
35 reworked the Fsignal code and synched the rest up to FSF 19.30. | |
36 Some changes by Martin Buchholz c. 1999? | |
37 e.g. PRIMITIVE_FUNCALL macros. | |
38 New call_trapping_problems code and large comments below | |
39 by Ben Wing, Mar-Apr 2000. | |
40 */ | |
41 | |
42 /* This file has been Mule-ized. */ | |
43 | |
44 /* What is in this file? | |
45 | |
46 This file contains the engine for the ELisp interpreter in XEmacs. | |
47 The engine does the actual work of implementing function calls, | |
48 form evaluation, non-local exits (catch, throw, signal, | |
49 condition-case, call-with-condition-handler), unwind-protects, | |
50 dynamic bindings, let constructs, backtraces, etc. You might say | |
51 that this module is the very heart of XEmacs, and everything else | |
52 in XEmacs is merely an auxiliary module implementing some specific | |
53 functionality that may be called from the heart at an appropriate | |
54 time. | |
55 | |
56 The only exception is the alloc.c module, which implements the | |
57 framework upon which this module (eval.c) works. alloc.c works | |
58 with creating the actual Lisp objects themselves and garbage | |
1960 | 59 collecting them as necessary, presenting a nice, high-level |
853 | 60 interface for object creation, deletion, access, and modification. |
61 | |
62 The only other exception that could be cited is the event-handling | |
63 module in event-stream.c. From its perspective, it is also the | |
64 heart of XEmacs, and controls exactly what gets done at what time. | |
65 From its perspective, eval.c is merely one of the auxiliary modules | |
66 out there that can be invoked by event-stream.c. | |
67 | |
68 Although the event-stream-centric view is a convenient fiction that | |
69 makes sense particularly from the user's perspective and from the | |
70 perspective of time, the engine-centric view is actually closest to | |
71 the truth, because anywhere within the event-stream module, you are | |
72 still somewhere in a Lisp backtrace, and event-loops are begun by | |
73 functions such as `command-loop-1', a Lisp function. | |
74 | |
75 As the Lisp engine is doing its thing, it maintains the state of | |
1960 | 76 the engine primarily in five list-like items, which are: |
853 | 77 |
78 -- the backtrace list | |
79 -- the catchtag list | |
80 -- the condition-handler list | |
81 -- the specbind list | |
82 -- the GCPRO list. | |
83 | |
84 These are described in detail in the next comment. | |
85 | |
86 --ben | |
87 */ | |
88 | |
89 /* Note that there are five separate lists used to maintain state in | |
90 the evaluator. All of them conceptually are stacks (last-in, | |
91 first-out). All non-local exits happen ultimately through the | |
92 catch/throw mechanism, which uses one of the five lists (the | |
93 catchtag list) and records the current state of the others in each | |
94 frame of the list (some other information is recorded and restored | |
95 as well, such as the current eval depth), so that all the state of | |
96 the evaluator is restored properly when a non-local exit occurs. | |
97 (Note that the current state of the condition-handler list is not | |
98 recorded in the catchtag list. Instead, when a condition-case or | |
99 call-with-condition-handler is set up, it installs an | |
100 unwind-protect on the specbind list to restore the appropriate | |
101 setting for the condition-handler list. During the course of | |
102 handling the non-local exit, all entries on the specbind list that | |
103 are past the location stored in the catch frame are "unwound" | |
104 (i.e. variable bindings are restored and unwind-protects are | |
105 executed), so the condition-handler list gets reset properly. | |
106 | |
107 The five lists are | |
108 | |
109 1. The backtrace list, which is chained through `struct backtrace's | |
110 declared in the stack frames of various primitives, and keeps | |
111 track of all Lisp function call entries and exits. | |
112 2. The catchtag list, which is chained through `struct catchtag's | |
113 declared in the stack frames of internal_catch and condition_case_1, | |
114 and keeps track of information needed to reset the internal state | |
115 of the evaluator to the state that was current when the catch or | |
116 condition-case were established, in the event of a non-local exit. | |
117 3. The condition-handler list, which is a simple Lisp list with new | |
118 entries consed onto the front of the list. It records condition-cases | |
119 and call-with-condition-handlers established either from C or from | |
120 Lisp. Unlike with the other lists (but similar to everything else | |
121 of a similar nature in the rest of the C and Lisp code), it takes care | |
122 of restoring itself appropriately in the event of a non-local exit | |
123 through the use of the unwind-protect mechanism. | |
124 4. The specbind list, which is a contiguous array of `struct specbinding's, | |
125 expanded as necessary using realloc(). It holds dynamic variable | |
126 bindings (the only kind we currently have in ELisp) and unwind-protects. | |
127 5. The GCPRO list, which is chained through `struct gcpro's declared in | |
128 the stack frames of any functions that need to GC-protect Lisp_Objects | |
129 declared on the stack. This is one of the most fragile areas of the | |
130 entire scheme -- you must not forget to UNGCPRO at the end of your | |
131 function, you must make sure you GCPRO in many circumstances you don't | |
132 think you have to, etc. See the internals manual for more information | |
133 about this. | |
134 | |
135 --ben | |
136 */ | |
137 | |
428 | 138 #include <config.h> |
139 #include "lisp.h" | |
140 | |
141 #include "commands.h" | |
142 #include "backtrace.h" | |
143 #include "bytecode.h" | |
144 #include "buffer.h" | |
872 | 145 #include "console-impl.h" |
853 | 146 #include "device.h" |
147 #include "frame.h" | |
148 #include "lstream.h" | |
428 | 149 #include "opaque.h" |
1292 | 150 #include "profile.h" |
853 | 151 #include "window.h" |
428 | 152 |
153 struct backtrace *backtrace_list; | |
154 | |
155 /* Macros for calling subrs with an argument list whose length is only | |
156 known at runtime. See EXFUN and DEFUN for similar hackery. */ | |
157 | |
158 #define AV_0(av) | |
159 #define AV_1(av) av[0] | |
160 #define AV_2(av) AV_1(av), av[1] | |
161 #define AV_3(av) AV_2(av), av[2] | |
162 #define AV_4(av) AV_3(av), av[3] | |
163 #define AV_5(av) AV_4(av), av[4] | |
164 #define AV_6(av) AV_5(av), av[5] | |
165 #define AV_7(av) AV_6(av), av[6] | |
166 #define AV_8(av) AV_7(av), av[7] | |
167 | |
168 #define PRIMITIVE_FUNCALL_1(fn, av, ac) \ | |
444 | 169 (((Lisp_Object (*)(EXFUN_##ac)) (fn)) (AV_##ac (av))) |
428 | 170 |
171 /* If subrs take more than 8 arguments, more cases need to be added | |
172 to this switch. (But wait - don't do it - if you really need | |
173 a SUBR with more than 8 arguments, use max_args == MANY. | |
853 | 174 Or better, considering using a property list as one of your args. |
428 | 175 See the DEFUN macro in lisp.h) */ |
176 #define PRIMITIVE_FUNCALL(rv, fn, av, ac) do { \ | |
177 void (*PF_fn)(void) = (void (*)(void)) fn; \ | |
178 Lisp_Object *PF_av = (av); \ | |
179 switch (ac) \ | |
180 { \ | |
436 | 181 default:rv = PRIMITIVE_FUNCALL_1(PF_fn, PF_av, 0); break; \ |
428 | 182 case 1: rv = PRIMITIVE_FUNCALL_1(PF_fn, PF_av, 1); break; \ |
183 case 2: rv = PRIMITIVE_FUNCALL_1(PF_fn, PF_av, 2); break; \ | |
184 case 3: rv = PRIMITIVE_FUNCALL_1(PF_fn, PF_av, 3); break; \ | |
185 case 4: rv = PRIMITIVE_FUNCALL_1(PF_fn, PF_av, 4); break; \ | |
186 case 5: rv = PRIMITIVE_FUNCALL_1(PF_fn, PF_av, 5); break; \ | |
187 case 6: rv = PRIMITIVE_FUNCALL_1(PF_fn, PF_av, 6); break; \ | |
188 case 7: rv = PRIMITIVE_FUNCALL_1(PF_fn, PF_av, 7); break; \ | |
189 case 8: rv = PRIMITIVE_FUNCALL_1(PF_fn, PF_av, 8); break; \ | |
190 } \ | |
191 } while (0) | |
192 | |
193 #define FUNCALL_SUBR(rv, subr, av, ac) \ | |
194 PRIMITIVE_FUNCALL (rv, subr_function (subr), av, ac); | |
195 | |
196 | |
197 /* This is the list of current catches (and also condition-cases). | |
853 | 198 This is a stack: the most recent catch is at the head of the list. |
199 The list is threaded through the stack frames of the C functions | |
200 that set up the catches; this is similar to the way the GCPRO list | |
201 is handled, but different from the condition-handler list (which is | |
202 a simple Lisp list) and the specbind stack, which is a contiguous | |
203 array of `struct specbinding's, grown (using realloc()) as | |
204 necessary. (Note that all four of these lists behave as a stacks.) | |
205 | |
3025 | 206 Catches are created by declaring a `struct catchtag' locally, |
853 | 207 filling the .TAG field in with the tag, and doing a setjmp() on |
208 .JMP. Fthrow() will store the value passed to it in .VAL and | |
209 longjmp() back to .JMP, back to the function that established the | |
210 catch. This will always be either internal_catch() (catches | |
211 established internally or through `catch') or condition_case_1 | |
212 (condition-cases established internally or through | |
213 `condition-case'). | |
428 | 214 |
215 The catchtag also records the current position in the | |
216 call stack (stored in BACKTRACE_LIST), the current position | |
217 in the specpdl stack (used for variable bindings and | |
218 unwind-protects), the value of LISP_EVAL_DEPTH, and the | |
219 current position in the GCPRO stack. All of these are | |
220 restored by Fthrow(). | |
853 | 221 */ |
428 | 222 |
223 struct catchtag *catchlist; | |
224 | |
853 | 225 /* A special tag that can be used internally from C code to catch |
226 every attempt to throw past this level. */ | |
227 Lisp_Object Vcatch_everything_tag; | |
228 | |
428 | 229 Lisp_Object Qautoload, Qmacro, Qexit; |
230 Lisp_Object Qinteractive, Qcommandp, Qdefun, Qprogn, Qvalues; | |
231 Lisp_Object Vquit_flag, Vinhibit_quit; | |
232 Lisp_Object Qand_rest, Qand_optional; | |
233 Lisp_Object Qdebug_on_error, Qstack_trace_on_error; | |
234 Lisp_Object Qdebug_on_signal, Qstack_trace_on_signal; | |
235 Lisp_Object Qdebugger; | |
236 Lisp_Object Qinhibit_quit; | |
887 | 237 Lisp_Object Qfinalize_list; |
428 | 238 Lisp_Object Qrun_hooks; |
239 Lisp_Object Qsetq; | |
240 Lisp_Object Qdisplay_warning; | |
241 Lisp_Object Vpending_warnings, Vpending_warnings_tail; | |
242 Lisp_Object Qif; | |
243 | |
853 | 244 /* Flags specifying which operations are currently inhibited. */ |
245 int inhibit_flags; | |
246 | |
247 /* Buffers, frames, windows, devices, and consoles created since most | |
248 recent active | |
249 call_trapping_problems (INHIBIT_EXISTING_PERMANENT_DISPLAY_OBJECT_DELETION). | |
250 */ | |
251 Lisp_Object Vdeletable_permanent_display_objects; | |
252 | |
253 /* Buffers created since most recent active | |
254 call_trapping_problems (INHIBIT_EXISTING_BUFFER_TEXT_MODIFICATION). */ | |
255 Lisp_Object Vmodifiable_buffers; | |
793 | 256 |
257 /* Minimum level at which warnings are logged. Below this, they're ignored | |
258 entirely -- not even generated. */ | |
259 Lisp_Object Vlog_warning_minimum_level; | |
260 | |
428 | 261 /* Non-nil means record all fset's and provide's, to be undone |
262 if the file being autoloaded is not fully loaded. | |
263 They are recorded by being consed onto the front of Vautoload_queue: | |
264 (FUN . ODEF) for a defun, (OFEATURES . nil) for a provide. */ | |
265 Lisp_Object Vautoload_queue; | |
266 | |
267 /* Current number of specbindings allocated in specpdl. */ | |
268 int specpdl_size; | |
269 | |
270 /* Pointer to beginning of specpdl. */ | |
271 struct specbinding *specpdl; | |
272 | |
273 /* Pointer to first unused element in specpdl. */ | |
274 struct specbinding *specpdl_ptr; | |
275 | |
276 /* specpdl_ptr - specpdl */ | |
277 int specpdl_depth_counter; | |
278 | |
279 /* Maximum size allowed for specpdl allocation */ | |
458 | 280 Fixnum max_specpdl_size; |
428 | 281 |
282 /* Depth in Lisp evaluations and function calls. */ | |
1292 | 283 int lisp_eval_depth; |
428 | 284 |
285 /* Maximum allowed depth in Lisp evaluations and function calls. */ | |
458 | 286 Fixnum max_lisp_eval_depth; |
428 | 287 |
288 /* Nonzero means enter debugger before next function call */ | |
289 static int debug_on_next_call; | |
290 | |
1292 | 291 int backtrace_with_internal_sections; |
292 | |
428 | 293 /* List of conditions (non-nil atom means all) which cause a backtrace |
294 if an error is handled by the command loop's error handler. */ | |
295 Lisp_Object Vstack_trace_on_error; | |
296 | |
297 /* List of conditions (non-nil atom means all) which enter the debugger | |
298 if an error is handled by the command loop's error handler. */ | |
299 Lisp_Object Vdebug_on_error; | |
300 | |
301 /* List of conditions and regexps specifying error messages which | |
302 do not enter the debugger even if Vdebug_on_error says they should. */ | |
303 Lisp_Object Vdebug_ignored_errors; | |
304 | |
305 /* List of conditions (non-nil atom means all) which cause a backtrace | |
306 if any error is signalled. */ | |
307 Lisp_Object Vstack_trace_on_signal; | |
308 | |
309 /* List of conditions (non-nil atom means all) which enter the debugger | |
310 if any error is signalled. */ | |
311 Lisp_Object Vdebug_on_signal; | |
312 | |
313 /* Nonzero means enter debugger if a quit signal | |
314 is handled by the command loop's error handler. | |
315 | |
316 From lisp, this is a boolean variable and may have the values 0 and 1. | |
317 But, eval.c temporarily uses the second bit of this variable to indicate | |
318 that a critical_quit is in progress. The second bit is reset immediately | |
319 after it is processed in signal_call_debugger(). */ | |
320 int debug_on_quit; | |
321 | |
322 #if 0 /* FSFmacs */ | |
323 /* entering_debugger is basically equivalent */ | |
324 /* The value of num_nonmacro_input_chars as of the last time we | |
325 started to enter the debugger. If we decide to enter the debugger | |
326 again when this is still equal to num_nonmacro_input_chars, then we | |
327 know that the debugger itself has an error, and we should just | |
328 signal the error instead of entering an infinite loop of debugger | |
329 invocations. */ | |
330 int when_entered_debugger; | |
331 #endif | |
332 | |
333 /* Nonzero means we are trying to enter the debugger. | |
334 This is to prevent recursive attempts. | |
335 Cleared by the debugger calling Fbacktrace */ | |
336 static int entering_debugger; | |
337 | |
338 /* Function to call to invoke the debugger */ | |
339 Lisp_Object Vdebugger; | |
340 | |
853 | 341 /* List of condition handlers currently in effect. |
342 The elements of this lists were at one point in the past | |
343 threaded through the stack frames of Fcondition_case and | |
344 related functions, but now are stored separately in a normal | |
345 stack. When an error is signaled (by calling Fsignal, below), | |
346 this list is searched for an element that applies. | |
428 | 347 |
348 Each element of this list is one of the following: | |
349 | |
853 | 350 -- A list of a handler function and possibly args to pass to the |
351 function. This is a handler established with the Lisp primitive | |
352 `call-with-condition-handler' or related C function | |
353 call_with_condition_handler(): | |
354 | |
355 If the handler function is an opaque ptr object, it is a handler | |
356 that was established in C using call_with_condition_handler(), | |
357 and the contents of the object are a function pointer which takes | |
358 three arguments, the signal name and signal data (same arguments | |
359 passed to `signal') and a third Lisp_Object argument, specified | |
360 in the call to call_with_condition_handler() and stored as the | |
361 second element of the list containing the handler functionl. | |
362 | |
363 If the handler function is a regular Lisp_Object, it is a handler | |
364 that was established using `call-with-condition-handler'. | |
365 Currently there are no more arguments in the list containing the | |
366 handler function, and only one argument is passed to the handler | |
367 function: a cons of the signal name and signal data arguments | |
368 passed to `signal'. | |
369 | |
370 -- A list whose car is Qunbound and whose cdr is Qt. This is a | |
371 special condition-case handler established by C code with | |
372 condition_case_1(). All errors are trapped; the debugger is not | |
373 invoked even if `debug-on-error' was set. | |
374 | |
375 -- A list whose car is Qunbound and whose cdr is Qerror. This is a | |
376 special condition-case handler established by C code with | |
377 condition_case_1(). It is like Qt except that the debugger is | |
378 invoked normally if it is called for. | |
379 | |
380 -- A list whose car is Qunbound and whose cdr is a list of lists | |
381 (CONDITION-NAME BODY ...) exactly as in `condition-case'. This is | |
382 a normal `condition-case' handler. | |
383 | |
384 Note that in all cases *except* the first, there is a corresponding | |
385 catch, whose TAG is the value of Vcondition_handlers just after the | |
386 handler data just described is pushed onto it. The reason is that | |
387 `condition-case' handlers need to throw back to the place where the | |
388 handler was installed before invoking it, while | |
389 `call-with-condition-handler' handlers are invoked in the | |
390 environment that `signal' was invoked in. */ | |
391 | |
392 | |
428 | 393 static Lisp_Object Vcondition_handlers; |
394 | |
853 | 395 /* I think we should keep this enabled all the time, not just when |
396 error checking is enabled, because if one of these puppies pops up, | |
397 it will trash the stack if not caught, making it that much harder to | |
398 debug. It doesn't cause speed loss. */ | |
442 | 399 #define DEFEND_AGAINST_THROW_RECURSION |
400 | |
401 #ifdef DEFEND_AGAINST_THROW_RECURSION | |
428 | 402 /* Used for error catching purposes by throw_or_bomb_out */ |
403 static int throw_level; | |
442 | 404 #endif |
405 | |
1123 | 406 static int warning_will_be_discarded (Lisp_Object level); |
2532 | 407 static Lisp_Object maybe_get_trapping_problems_backtrace (void); |
1123 | 408 |
428 | 409 |
410 /************************************************************************/ | |
411 /* The subr object type */ | |
412 /************************************************************************/ | |
413 | |
414 static void | |
2286 | 415 print_subr (Lisp_Object obj, Lisp_Object printcharfun, int UNUSED (escapeflag)) |
428 | 416 { |
417 Lisp_Subr *subr = XSUBR (obj); | |
867 | 418 const CIbyte *header = |
428 | 419 (subr->max_args == UNEVALLED) ? "#<special-form " : "#<subr "; |
867 | 420 const CIbyte *name = subr_name (subr); |
421 const CIbyte *trailer = subr->prompt ? " (interactive)>" : ">"; | |
428 | 422 |
423 if (print_readably) | |
563 | 424 printing_unreadable_object ("%s%s%s", header, name, trailer); |
428 | 425 |
826 | 426 write_c_string (printcharfun, header); |
427 write_c_string (printcharfun, name); | |
428 write_c_string (printcharfun, trailer); | |
428 | 429 } |
430 | |
1204 | 431 static const struct memory_description subr_description[] = { |
2551 | 432 { XD_DOC_STRING, offsetof (Lisp_Subr, doc), 0, { 0 }, XD_FLAG_NO_KKCC }, |
428 | 433 { XD_END } |
434 }; | |
435 | |
938 | 436 DEFINE_BASIC_LRECORD_IMPLEMENTATION ("subr", subr, |
437 1, /*dumpable-flag*/ | |
438 0, print_subr, 0, 0, 0, | |
439 subr_description, | |
440 Lisp_Subr); | |
428 | 441 |
442 /************************************************************************/ | |
443 /* Entering the debugger */ | |
444 /************************************************************************/ | |
445 | |
853 | 446 static Lisp_Object |
447 current_warning_level (void) | |
448 { | |
449 if (inhibit_flags & ISSUE_WARNINGS_AT_DEBUG_LEVEL) | |
450 return Qdebug; | |
451 else | |
452 return Qwarning; | |
453 } | |
454 | |
428 | 455 /* Actually call the debugger. ARG is a list of args that will be |
456 passed to the debugger function, as follows; | |
457 | |
458 If due to frame exit, args are `exit' and the value being returned; | |
459 this function's value will be returned instead of that. | |
460 If due to error, args are `error' and a list of the args to `signal'. | |
461 If due to `apply' or `funcall' entry, one arg, `lambda'. | |
462 If due to `eval' entry, one arg, t. | |
463 | |
464 */ | |
465 | |
466 static Lisp_Object | |
467 call_debugger_259 (Lisp_Object arg) | |
468 { | |
469 return apply1 (Vdebugger, arg); | |
470 } | |
471 | |
472 /* Call the debugger, doing some encapsulation. We make sure we have | |
473 some room on the eval and specpdl stacks, and bind entering_debugger | |
474 to 1 during this call. This is used to trap errors that may occur | |
475 when entering the debugger (e.g. the value of `debugger' is invalid), | |
476 so that the debugger will not be recursively entered if debug-on-error | |
477 is set. (Otherwise, XEmacs would infinitely recurse, attempting to | |
478 enter the debugger.) entering_debugger gets reset to 0 as soon | |
479 as a backtrace is displayed, so that further errors can indeed be | |
480 handled normally. | |
481 | |
3025 | 482 We also establish a catch for `debugger'. If the debugger function |
428 | 483 throws to this instead of returning a value, it means that the user |
484 pressed 'c' (pretend like the debugger was never entered). The | |
485 function then returns Qunbound. (If the user pressed 'r', for | |
486 return a value, then the debugger function returns normally with | |
487 this value.) | |
488 | |
489 The difference between 'c' and 'r' is as follows: | |
490 | |
491 debug-on-call: | |
492 No difference. The call proceeds as normal. | |
493 debug-on-exit: | |
494 With 'r', the specified value is returned as the function's | |
495 return value. With 'c', the value that would normally be | |
496 returned is returned. | |
497 signal: | |
498 With 'r', the specified value is returned as the return | |
499 value of `signal'. (This is the only time that `signal' | |
500 can return, instead of making a non-local exit.) With `c', | |
501 `signal' will continue looking for handlers as if the | |
502 debugger was never entered, and will probably end up | |
503 throwing to a handler or to top-level. | |
504 */ | |
505 | |
506 static Lisp_Object | |
507 call_debugger (Lisp_Object arg) | |
508 { | |
509 int threw; | |
510 Lisp_Object val; | |
511 int speccount; | |
512 | |
853 | 513 debug_on_next_call = 0; |
514 | |
515 if (inhibit_flags & INHIBIT_ENTERING_DEBUGGER) | |
516 { | |
517 if (!(inhibit_flags & INHIBIT_WARNING_ISSUE)) | |
518 warn_when_safe | |
519 (Qdebugger, current_warning_level (), | |
520 "Unable to enter debugger within critical section"); | |
521 return Qunbound; | |
522 } | |
523 | |
428 | 524 if (lisp_eval_depth + 20 > max_lisp_eval_depth) |
525 max_lisp_eval_depth = lisp_eval_depth + 20; | |
526 if (specpdl_size + 40 > max_specpdl_size) | |
527 max_specpdl_size = specpdl_size + 40; | |
853 | 528 |
529 speccount = internal_bind_int (&entering_debugger, 1); | |
2532 | 530 val = internal_catch (Qdebugger, call_debugger_259, arg, &threw, 0, 0); |
428 | 531 |
771 | 532 return unbind_to_1 (speccount, ((threw) |
428 | 533 ? Qunbound /* Not returning a value */ |
534 : val)); | |
535 } | |
536 | |
537 /* Called when debug-on-exit behavior is called for. Enter the debugger | |
538 with the appropriate args for this. VAL is the exit value that is | |
539 about to be returned. */ | |
540 | |
541 static Lisp_Object | |
542 do_debug_on_exit (Lisp_Object val) | |
543 { | |
544 /* This is falsified by call_debugger */ | |
545 Lisp_Object v = call_debugger (list2 (Qexit, val)); | |
546 | |
547 return !UNBOUNDP (v) ? v : val; | |
548 } | |
549 | |
550 /* Called when debug-on-call behavior is called for. Enter the debugger | |
551 with the appropriate args for this. VAL is either t for a call | |
3025 | 552 through `eval' or `lambda' for a call through `funcall'. |
428 | 553 |
554 #### The differentiation here between EVAL and FUNCALL is bogus. | |
555 FUNCALL can be defined as | |
556 | |
557 (defmacro func (fun &rest args) | |
558 (cons (eval fun) args)) | |
559 | |
560 and should be treated as such. | |
561 */ | |
562 | |
563 static void | |
564 do_debug_on_call (Lisp_Object code) | |
565 { | |
566 debug_on_next_call = 0; | |
567 backtrace_list->debug_on_exit = 1; | |
568 call_debugger (list1 (code)); | |
569 } | |
570 | |
571 /* LIST is the value of one of the variables `debug-on-error', | |
572 `debug-on-signal', `stack-trace-on-error', or `stack-trace-on-signal', | |
573 and CONDITIONS is the list of error conditions associated with | |
574 the error being signalled. This returns non-nil if LIST | |
575 matches CONDITIONS. (A nil value for LIST does not match | |
576 CONDITIONS. A non-list value for LIST does match CONDITIONS. | |
577 A list matches CONDITIONS when one of the symbols in LIST is the | |
578 same as one of the symbols in CONDITIONS.) */ | |
579 | |
580 static int | |
581 wants_debugger (Lisp_Object list, Lisp_Object conditions) | |
582 { | |
583 if (NILP (list)) | |
584 return 0; | |
585 if (! CONSP (list)) | |
586 return 1; | |
587 | |
588 while (CONSP (conditions)) | |
589 { | |
2552 | 590 Lisp_Object curr, tail; |
591 curr = XCAR (conditions); | |
428 | 592 for (tail = list; CONSP (tail); tail = XCDR (tail)) |
2552 | 593 if (EQ (XCAR (tail), curr)) |
428 | 594 return 1; |
595 conditions = XCDR (conditions); | |
596 } | |
597 return 0; | |
598 } | |
599 | |
600 | |
601 /* Return 1 if an error with condition-symbols CONDITIONS, | |
602 and described by SIGNAL-DATA, should skip the debugger | |
4624
9dd42cb187ed
Fix typo in comment on skip_debugger.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4535
diff
changeset
|
603 according to debug-ignored-errors. */ |
428 | 604 |
605 static int | |
606 skip_debugger (Lisp_Object conditions, Lisp_Object data) | |
607 { | |
608 /* This function can GC */ | |
609 Lisp_Object tail; | |
610 int first_string = 1; | |
611 Lisp_Object error_message = Qnil; | |
612 | |
613 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail)) | |
614 { | |
615 if (STRINGP (XCAR (tail))) | |
616 { | |
617 if (first_string) | |
618 { | |
619 error_message = Ferror_message_string (data); | |
620 first_string = 0; | |
621 } | |
622 if (fast_lisp_string_match (XCAR (tail), error_message) >= 0) | |
623 return 1; | |
624 } | |
625 else | |
626 { | |
627 Lisp_Object contail; | |
628 | |
629 for (contail = conditions; CONSP (contail); contail = XCDR (contail)) | |
630 if (EQ (XCAR (tail), XCAR (contail))) | |
631 return 1; | |
632 } | |
633 } | |
634 | |
635 return 0; | |
636 } | |
637 | |
638 /* Actually generate a backtrace on STREAM. */ | |
639 | |
640 static Lisp_Object | |
641 backtrace_259 (Lisp_Object stream) | |
642 { | |
643 return Fbacktrace (stream, Qt); | |
644 } | |
645 | |
1130 | 646 #ifdef DEBUG_XEMACS |
647 | |
648 static void | |
649 trace_out_and_die (Lisp_Object err) | |
650 { | |
651 Fdisplay_error (err, Qt); | |
652 backtrace_259 (Qnil); | |
653 stderr_out ("XEmacs exiting to debugger.\n"); | |
654 Fforce_debugging_signal (Qt); | |
655 /* Unlikely to be reached */ | |
656 } | |
657 | |
658 #endif | |
659 | |
428 | 660 /* An error was signaled. Maybe call the debugger, if the `debug-on-error' |
661 etc. variables call for this. CONDITIONS is the list of conditions | |
662 associated with the error being signalled. SIG is the actual error | |
663 being signalled, and DATA is the associated data (these are exactly | |
664 the same as the arguments to `signal'). ACTIVE_HANDLERS is the | |
665 list of error handlers that are to be put in place while the debugger | |
666 is called. This is generally the remaining handlers that are | |
667 outside of the innermost handler trapping this error. This way, | |
668 if the same error occurs inside of the debugger, you usually don't get | |
669 the debugger entered recursively. | |
670 | |
671 This function returns Qunbound if it didn't call the debugger or if | |
672 the user asked (through 'c') that XEmacs should pretend like the | |
673 debugger was never entered. Otherwise, it returns the value | |
674 that the user specified with `r'. (Note that much of the time, | |
675 the user will abort with C-], and we will never have a chance to | |
676 return anything at all.) | |
677 | |
678 SIGNAL_VARS_ONLY means we should only look at debug-on-signal | |
679 and stack-trace-on-signal to control whether we do anything. | |
680 This is so that debug-on-error doesn't make handled errors | |
681 cause the debugger to get invoked. | |
682 | |
683 STACK_TRACE_DISPLAYED and DEBUGGER_ENTERED are used so that | |
684 those functions aren't done more than once in a single `signal' | |
685 session. */ | |
686 | |
687 static Lisp_Object | |
688 signal_call_debugger (Lisp_Object conditions, | |
689 Lisp_Object sig, Lisp_Object data, | |
690 Lisp_Object active_handlers, | |
691 int signal_vars_only, | |
692 int *stack_trace_displayed, | |
693 int *debugger_entered) | |
694 { | |
853 | 695 #ifdef PIGS_FLY_AND_ALL_C_CODE_CAN_HANDLE_GC_OCCURRING_ALMOST_ANYWHERE |
428 | 696 /* This function can GC */ |
853 | 697 #else /* reality check */ |
698 /* This function cannot GC because it inhibits GC during its operation */ | |
699 #endif | |
700 | |
428 | 701 Lisp_Object val = Qunbound; |
702 Lisp_Object all_handlers = Vcondition_handlers; | |
703 Lisp_Object temp_data = Qnil; | |
853 | 704 int outer_speccount = specpdl_depth(); |
705 int speccount; | |
706 | |
707 #ifdef PIGS_FLY_AND_ALL_C_CODE_CAN_HANDLE_GC_OCCURRING_ALMOST_ANYWHERE | |
428 | 708 struct gcpro gcpro1, gcpro2; |
709 GCPRO2 (all_handlers, temp_data); | |
853 | 710 #else |
711 begin_gc_forbidden (); | |
712 #endif | |
713 | |
714 speccount = specpdl_depth(); | |
428 | 715 |
716 Vcondition_handlers = active_handlers; | |
717 | |
718 temp_data = Fcons (sig, data); /* needed for skip_debugger */ | |
719 | |
720 if (!entering_debugger && !*stack_trace_displayed && !signal_vars_only | |
721 && wants_debugger (Vstack_trace_on_error, conditions) | |
722 && !skip_debugger (conditions, temp_data)) | |
723 { | |
724 specbind (Qdebug_on_error, Qnil); | |
725 specbind (Qstack_trace_on_error, Qnil); | |
726 specbind (Qdebug_on_signal, Qnil); | |
727 specbind (Qstack_trace_on_signal, Qnil); | |
728 | |
442 | 729 if (!noninteractive) |
730 internal_with_output_to_temp_buffer (build_string ("*Backtrace*"), | |
731 backtrace_259, | |
732 Qnil, | |
733 Qnil); | |
734 else /* in batch mode, we want this going to stderr. */ | |
735 backtrace_259 (Qnil); | |
771 | 736 unbind_to (speccount); |
428 | 737 *stack_trace_displayed = 1; |
738 } | |
739 | |
740 if (!entering_debugger && !*debugger_entered && !signal_vars_only | |
741 && (EQ (sig, Qquit) | |
742 ? debug_on_quit | |
743 : wants_debugger (Vdebug_on_error, conditions)) | |
744 && !skip_debugger (conditions, temp_data)) | |
745 { | |
746 debug_on_quit &= ~2; /* reset critical bit */ | |
1123 | 747 |
428 | 748 specbind (Qdebug_on_error, Qnil); |
749 specbind (Qstack_trace_on_error, Qnil); | |
750 specbind (Qdebug_on_signal, Qnil); | |
751 specbind (Qstack_trace_on_signal, Qnil); | |
752 | |
1130 | 753 #ifdef DEBUG_XEMACS |
754 if (noninteractive) | |
755 trace_out_and_die (Fcons (sig, data)); | |
756 #endif | |
757 | |
428 | 758 val = call_debugger (list2 (Qerror, (Fcons (sig, data)))); |
853 | 759 unbind_to (speccount); |
428 | 760 *debugger_entered = 1; |
761 } | |
762 | |
763 if (!entering_debugger && !*stack_trace_displayed | |
764 && wants_debugger (Vstack_trace_on_signal, conditions)) | |
765 { | |
766 specbind (Qdebug_on_error, Qnil); | |
767 specbind (Qstack_trace_on_error, Qnil); | |
768 specbind (Qdebug_on_signal, Qnil); | |
769 specbind (Qstack_trace_on_signal, Qnil); | |
770 | |
442 | 771 if (!noninteractive) |
772 internal_with_output_to_temp_buffer (build_string ("*Backtrace*"), | |
773 backtrace_259, | |
774 Qnil, | |
775 Qnil); | |
776 else /* in batch mode, we want this going to stderr. */ | |
777 backtrace_259 (Qnil); | |
771 | 778 unbind_to (speccount); |
428 | 779 *stack_trace_displayed = 1; |
780 } | |
781 | |
782 if (!entering_debugger && !*debugger_entered | |
783 && (EQ (sig, Qquit) | |
784 ? debug_on_quit | |
785 : wants_debugger (Vdebug_on_signal, conditions))) | |
786 { | |
787 debug_on_quit &= ~2; /* reset critical bit */ | |
1123 | 788 |
428 | 789 specbind (Qdebug_on_error, Qnil); |
790 specbind (Qstack_trace_on_error, Qnil); | |
791 specbind (Qdebug_on_signal, Qnil); | |
792 specbind (Qstack_trace_on_signal, Qnil); | |
793 | |
1130 | 794 #ifdef DEBUG_XEMACS |
795 if (noninteractive) | |
796 trace_out_and_die (Fcons (sig, data)); | |
797 #endif | |
798 | |
428 | 799 val = call_debugger (list2 (Qerror, (Fcons (sig, data)))); |
800 *debugger_entered = 1; | |
801 } | |
802 | |
853 | 803 #ifdef PIGS_FLY_AND_ALL_C_CODE_CAN_HANDLE_GC_OCCURRING_ALMOST_ANYWHERE |
428 | 804 UNGCPRO; |
853 | 805 #endif |
428 | 806 Vcondition_handlers = all_handlers; |
853 | 807 return unbind_to_1 (outer_speccount, val); |
428 | 808 } |
809 | |
810 | |
811 /************************************************************************/ | |
812 /* The basic special forms */ | |
813 /************************************************************************/ | |
814 | |
815 /* Except for Fprogn(), the basic special forms below are only called | |
816 from interpreted code. The byte compiler turns them into bytecodes. */ | |
817 | |
818 DEFUN ("or", For, 0, UNEVALLED, 0, /* | |
4642
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
819 Eval ARGS until one of them yields non-nil, then return that value. |
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
820 The remaining ARGS are not evalled at all. |
428 | 821 If all args return nil, return nil. |
4642
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
822 |
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
823 arguments: (&rest ARGS) |
428 | 824 */ |
825 (args)) | |
826 { | |
827 /* This function can GC */ | |
442 | 828 REGISTER Lisp_Object val; |
428 | 829 |
830 LIST_LOOP_2 (arg, args) | |
831 { | |
832 if (!NILP (val = Feval (arg))) | |
833 return val; | |
834 } | |
835 | |
836 return Qnil; | |
837 } | |
838 | |
839 DEFUN ("and", Fand, 0, UNEVALLED, 0, /* | |
4642
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
840 Eval ARGS until one of them yields nil, then return nil. |
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
841 The remaining ARGS are not evalled at all. |
428 | 842 If no arg yields nil, return the last arg's value. |
4642
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
843 |
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
844 arguments: (&rest ARGS) |
428 | 845 */ |
846 (args)) | |
847 { | |
848 /* This function can GC */ | |
442 | 849 REGISTER Lisp_Object val = Qt; |
428 | 850 |
851 LIST_LOOP_2 (arg, args) | |
852 { | |
853 if (NILP (val = Feval (arg))) | |
854 return val; | |
855 } | |
856 | |
857 return val; | |
858 } | |
859 | |
860 DEFUN ("if", Fif, 2, UNEVALLED, 0, /* | |
4642
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
861 If COND yields non-nil, do THEN, else do ELSE. |
428 | 862 Returns the value of THEN or the value of the last of the ELSE's. |
863 THEN must be one expression, but ELSE... can be zero or more expressions. | |
864 If COND yields nil, and there are no ELSE's, the value is nil. | |
4642
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
865 |
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
866 arguments: (COND THEN &rest ELSE) |
428 | 867 */ |
868 (args)) | |
869 { | |
870 /* This function can GC */ | |
871 Lisp_Object condition = XCAR (args); | |
872 Lisp_Object then_form = XCAR (XCDR (args)); | |
873 Lisp_Object else_forms = XCDR (XCDR (args)); | |
874 | |
875 if (!NILP (Feval (condition))) | |
876 return Feval (then_form); | |
877 else | |
878 return Fprogn (else_forms); | |
879 } | |
880 | |
881 /* Macros `when' and `unless' are trivially defined in Lisp, | |
882 but it helps for bootstrapping to have them ALWAYS defined. */ | |
883 | |
884 DEFUN ("when", Fwhen, 1, MANY, 0, /* | |
4642
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
885 If COND yields non-nil, do BODY, else return nil. |
428 | 886 BODY can be zero or more expressions. If BODY is nil, return nil. |
4642
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
887 |
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
888 arguments: (COND &rest BODY) |
428 | 889 */ |
890 (int nargs, Lisp_Object *args)) | |
891 { | |
892 Lisp_Object cond = args[0]; | |
893 Lisp_Object body; | |
853 | 894 |
428 | 895 switch (nargs) |
896 { | |
897 case 1: body = Qnil; break; | |
898 case 2: body = args[1]; break; | |
899 default: body = Fcons (Qprogn, Flist (nargs-1, args+1)); break; | |
900 } | |
901 | |
902 return list3 (Qif, cond, body); | |
903 } | |
904 | |
905 DEFUN ("unless", Funless, 1, MANY, 0, /* | |
4642
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
906 If COND yields nil, do BODY, else return nil. |
428 | 907 BODY can be zero or more expressions. If BODY is nil, return nil. |
4642
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
908 |
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
909 arguments: (COND &rest BODY) |
428 | 910 */ |
911 (int nargs, Lisp_Object *args)) | |
912 { | |
913 Lisp_Object cond = args[0]; | |
914 Lisp_Object body = Flist (nargs-1, args+1); | |
915 return Fcons (Qif, Fcons (cond, Fcons (Qnil, body))); | |
916 } | |
917 | |
918 DEFUN ("cond", Fcond, 0, UNEVALLED, 0, /* | |
4642
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
919 Try each clause until one succeeds. |
428 | 920 Each clause looks like (CONDITION BODY...). CONDITION is evaluated |
921 and, if the value is non-nil, this clause succeeds: | |
922 then the expressions in BODY are evaluated and the last one's | |
923 value is the value of the cond-form. | |
924 If no clause succeeds, cond returns nil. | |
925 If a clause has one element, as in (CONDITION), | |
926 CONDITION's value if non-nil is returned from the cond-form. | |
4642
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
927 |
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
928 arguments: (&rest CLAUSES) |
428 | 929 */ |
930 (args)) | |
931 { | |
932 /* This function can GC */ | |
442 | 933 REGISTER Lisp_Object val; |
428 | 934 |
935 LIST_LOOP_2 (clause, args) | |
936 { | |
937 CHECK_CONS (clause); | |
938 if (!NILP (val = Feval (XCAR (clause)))) | |
939 { | |
940 if (!NILP (clause = XCDR (clause))) | |
941 { | |
942 CHECK_TRUE_LIST (clause); | |
943 val = Fprogn (clause); | |
944 } | |
945 return val; | |
946 } | |
947 } | |
948 | |
949 return Qnil; | |
950 } | |
951 | |
952 DEFUN ("progn", Fprogn, 0, UNEVALLED, 0, /* | |
4642
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
953 Eval BODY forms sequentially and return value of last one. |
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
954 |
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
955 arguments: (&rest BODY) |
428 | 956 */ |
957 (args)) | |
958 { | |
959 /* This function can GC */ | |
960 /* Caller must provide a true list in ARGS */ | |
442 | 961 REGISTER Lisp_Object val = Qnil; |
428 | 962 struct gcpro gcpro1; |
963 | |
964 GCPRO1 (args); | |
965 | |
966 { | |
967 LIST_LOOP_2 (form, args) | |
968 val = Feval (form); | |
969 } | |
970 | |
971 UNGCPRO; | |
972 return val; | |
973 } | |
974 | |
975 /* Fprog1() is the canonical example of a function that must GCPRO a | |
976 Lisp_Object across calls to Feval(). */ | |
977 | |
978 DEFUN ("prog1", Fprog1, 1, UNEVALLED, 0, /* | |
979 Similar to `progn', but the value of the first form is returned. | |
4642
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
980 |
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
981 All the arguments are evaluated sequentially. The value of FIRST is saved |
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
982 during evaluation of the remaining args, whose values are discarded. |
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
983 |
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
984 arguments: (FIRST &rest BODY) |
428 | 985 */ |
986 (args)) | |
987 { | |
1849 | 988 Lisp_Object val; |
428 | 989 struct gcpro gcpro1; |
990 | |
991 val = Feval (XCAR (args)); | |
992 | |
993 GCPRO1 (val); | |
994 | |
995 { | |
996 LIST_LOOP_2 (form, XCDR (args)) | |
997 Feval (form); | |
998 } | |
999 | |
1000 UNGCPRO; | |
1001 return val; | |
1002 } | |
1003 | |
1004 DEFUN ("prog2", Fprog2, 2, UNEVALLED, 0, /* | |
1005 Similar to `progn', but the value of the second form is returned. | |
4642
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
1006 |
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
1007 All the arguments are evaluated sequentially. The value of SECOND is saved |
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
1008 during evaluation of the remaining args, whose values are discarded. |
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
1009 |
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
1010 arguments: (FIRST SECOND &rest BODY) |
428 | 1011 */ |
1012 (args)) | |
1013 { | |
1014 /* This function can GC */ | |
1849 | 1015 Lisp_Object val; |
428 | 1016 struct gcpro gcpro1; |
1017 | |
1018 Feval (XCAR (args)); | |
1019 args = XCDR (args); | |
1020 val = Feval (XCAR (args)); | |
1021 args = XCDR (args); | |
1022 | |
1023 GCPRO1 (val); | |
1024 | |
442 | 1025 { |
1026 LIST_LOOP_2 (form, args) | |
1027 Feval (form); | |
1028 } | |
428 | 1029 |
1030 UNGCPRO; | |
1031 return val; | |
1032 } | |
1033 | |
1034 DEFUN ("let*", FletX, 1, UNEVALLED, 0, /* | |
4642
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
1035 Bind variables according to VARLIST then eval BODY. |
428 | 1036 The value of the last form in BODY is returned. |
1037 Each element of VARLIST is a symbol (which is bound to nil) | |
1038 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM). | |
1039 Each VALUEFORM can refer to the symbols already bound by this VARLIST. | |
4642
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
1040 |
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
1041 arguments: (VARLIST &rest BODY) |
428 | 1042 */ |
1043 (args)) | |
1044 { | |
1045 /* This function can GC */ | |
1046 Lisp_Object varlist = XCAR (args); | |
1047 Lisp_Object body = XCDR (args); | |
1048 int speccount = specpdl_depth(); | |
1049 | |
1050 EXTERNAL_LIST_LOOP_3 (var, varlist, tail) | |
1051 { | |
1052 Lisp_Object symbol, value, tem; | |
1053 if (SYMBOLP (var)) | |
1054 symbol = var, value = Qnil; | |
1055 else | |
1056 { | |
1057 CHECK_CONS (var); | |
1058 symbol = XCAR (var); | |
1059 tem = XCDR (var); | |
1060 if (NILP (tem)) | |
1061 value = Qnil; | |
1062 else | |
1063 { | |
1064 CHECK_CONS (tem); | |
1065 value = Feval (XCAR (tem)); | |
1066 if (!NILP (XCDR (tem))) | |
563 | 1067 sferror |
428 | 1068 ("`let' bindings can have only one value-form", var); |
1069 } | |
1070 } | |
1071 specbind (symbol, value); | |
1072 } | |
771 | 1073 return unbind_to_1 (speccount, Fprogn (body)); |
428 | 1074 } |
1075 | |
1076 DEFUN ("let", Flet, 1, UNEVALLED, 0, /* | |
4642
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
1077 Bind variables according to VARLIST then eval BODY. |
428 | 1078 The value of the last form in BODY is returned. |
1079 Each element of VARLIST is a symbol (which is bound to nil) | |
1080 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM). | |
1081 All the VALUEFORMs are evalled before any symbols are bound. | |
4642
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
1082 |
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
1083 arguments: (VARLIST &rest BODY) |
428 | 1084 */ |
1085 (args)) | |
1086 { | |
1087 /* This function can GC */ | |
1088 Lisp_Object varlist = XCAR (args); | |
1089 Lisp_Object body = XCDR (args); | |
1090 int speccount = specpdl_depth(); | |
1091 Lisp_Object *temps; | |
1092 int idx; | |
1093 struct gcpro gcpro1; | |
1094 | |
1095 /* Make space to hold the values to give the bound variables. */ | |
1096 { | |
1097 int varcount; | |
1098 GET_EXTERNAL_LIST_LENGTH (varlist, varcount); | |
1099 temps = alloca_array (Lisp_Object, varcount); | |
1100 } | |
1101 | |
1102 /* Compute the values and store them in `temps' */ | |
1103 GCPRO1 (*temps); | |
1104 gcpro1.nvars = 0; | |
1105 | |
1106 idx = 0; | |
442 | 1107 { |
1108 LIST_LOOP_2 (var, varlist) | |
1109 { | |
1110 Lisp_Object *value = &temps[idx++]; | |
1111 if (SYMBOLP (var)) | |
1112 *value = Qnil; | |
1113 else | |
1114 { | |
1115 Lisp_Object tem; | |
1116 CHECK_CONS (var); | |
1117 tem = XCDR (var); | |
1118 if (NILP (tem)) | |
1119 *value = Qnil; | |
1120 else | |
1121 { | |
1122 CHECK_CONS (tem); | |
1123 *value = Feval (XCAR (tem)); | |
1124 gcpro1.nvars = idx; | |
1125 | |
1126 if (!NILP (XCDR (tem))) | |
563 | 1127 sferror |
442 | 1128 ("`let' bindings can have only one value-form", var); |
1129 } | |
1130 } | |
1131 } | |
1132 } | |
428 | 1133 |
1134 idx = 0; | |
442 | 1135 { |
1136 LIST_LOOP_2 (var, varlist) | |
1137 { | |
1138 specbind (SYMBOLP (var) ? var : XCAR (var), temps[idx++]); | |
1139 } | |
1140 } | |
428 | 1141 |
1142 UNGCPRO; | |
1143 | |
771 | 1144 return unbind_to_1 (speccount, Fprogn (body)); |
428 | 1145 } |
1146 | |
1147 DEFUN ("while", Fwhile, 1, UNEVALLED, 0, /* | |
4642
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
1148 If TEST yields non-nil, eval BODY... and repeat. |
428 | 1149 The order of execution is thus TEST, BODY, TEST, BODY and so on |
1150 until TEST returns nil. | |
4642
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
1151 |
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
1152 arguments: (TEST &rest BODY) |
428 | 1153 */ |
1154 (args)) | |
1155 { | |
1156 /* This function can GC */ | |
1157 Lisp_Object test = XCAR (args); | |
1158 Lisp_Object body = XCDR (args); | |
1159 | |
1160 while (!NILP (Feval (test))) | |
1161 { | |
1162 QUIT; | |
1163 Fprogn (body); | |
1164 } | |
1165 | |
1166 return Qnil; | |
1167 } | |
1168 | |
1169 DEFUN ("setq", Fsetq, 0, UNEVALLED, 0, /* | |
1170 \(setq SYM VAL SYM VAL ...): set each SYM to the value of its VAL. | |
1171 The symbols SYM are variables; they are literal (not evaluated). | |
1172 The values VAL are expressions; they are evaluated. | |
1173 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'. | |
1174 The second VAL is not computed until after the first SYM is set, and so on; | |
1175 each VAL can use the new value of variables set earlier in the `setq'. | |
1176 The return value of the `setq' form is the value of the last VAL. | |
1177 */ | |
1178 (args)) | |
1179 { | |
1180 /* This function can GC */ | |
1181 int nargs; | |
2421 | 1182 Lisp_Object retval = Qnil; |
428 | 1183 |
1184 GET_LIST_LENGTH (args, nargs); | |
1185 | |
1186 if (nargs & 1) /* Odd number of arguments? */ | |
1187 Fsignal (Qwrong_number_of_arguments, list2 (Qsetq, make_int (nargs))); | |
1188 | |
2421 | 1189 GC_PROPERTY_LIST_LOOP_3 (symbol, val, args) |
428 | 1190 { |
1191 val = Feval (val); | |
1192 Fset (symbol, val); | |
2421 | 1193 retval = val; |
428 | 1194 } |
1195 | |
2421 | 1196 END_GC_PROPERTY_LIST_LOOP (symbol); |
1197 | |
1198 return retval; | |
428 | 1199 } |
1200 | |
1201 DEFUN ("quote", Fquote, 1, UNEVALLED, 0, /* | |
1202 Return the argument, without evaluating it. `(quote x)' yields `x'. | |
3794 | 1203 |
3842 | 1204 `quote' differs from `function' in that it is a hint that an expression is |
1205 data, not a function. In particular, under some circumstances the byte | |
1206 compiler will compile an expression quoted with `function', but it will | |
1207 never do so for an expression quoted with `quote'. These issues are most | |
1208 important for lambda expressions (see `lambda'). | |
1209 | |
1210 There is an alternative, more readable, reader syntax for `quote': a Lisp | |
1211 object preceded by `''. Thus, `'x' is equivalent to `(quote x)', in all | |
1212 contexts. A print function may use either. Internally the expression is | |
1213 represented as `(quote x)'). | |
428 | 1214 */ |
1215 (args)) | |
1216 { | |
1217 return XCAR (args); | |
1218 } | |
1219 | |
1220 DEFUN ("function", Ffunction, 1, UNEVALLED, 0, /* | |
3842 | 1221 Return the argument, without evaluating it. `(function x)' yields `x'. |
1222 | |
1223 `function' differs from `quote' in that it is a hint that an expression is | |
1224 a function, not data. In particular, under some circumstances the byte | |
1225 compiler will compile an expression quoted with `function', but it will | |
1226 never do so for an expression quoted with `quote'. However, the byte | |
1227 compiler will not compile an expression buried in a data structure such as | |
1228 a vector or a list which is not syntactically a function. These issues are | |
1229 most important for lambda expressions (see `lambda'). | |
1230 | |
1231 There is an alternative, more readable, reader syntax for `function': a Lisp | |
1232 object preceded by `#''. Thus, #'x is equivalent to (function x), in all | |
1233 contexts. A print function may use either. Internally the expression is | |
1234 represented as `(function x)'). | |
428 | 1235 */ |
1236 (args)) | |
1237 { | |
1238 return XCAR (args); | |
1239 } | |
1240 | |
1241 | |
1242 /************************************************************************/ | |
1243 /* Defining functions/variables */ | |
1244 /************************************************************************/ | |
1245 static Lisp_Object | |
1246 define_function (Lisp_Object name, Lisp_Object defn) | |
1247 { | |
1248 Ffset (name, defn); | |
4535
69a1eda3da06
Distinguish vars and functions in #'symbol-file, #'describe-{function,variable}
Aidan Kehoe <kehoea@parhasard.net>
parents:
4502
diff
changeset
|
1249 LOADHIST_ATTACH (Fcons (Qdefun, name)); |
428 | 1250 return name; |
1251 } | |
1252 | |
1253 DEFUN ("defun", Fdefun, 2, UNEVALLED, 0, /* | |
1254 \(defun NAME ARGLIST [DOCSTRING] BODY...): define NAME as a function. | |
1255 The definition is (lambda ARGLIST [DOCSTRING] BODY...). | |
1256 See also the function `interactive'. | |
1257 */ | |
1258 (args)) | |
1259 { | |
1260 /* This function can GC */ | |
1261 return define_function (XCAR (args), | |
1262 Fcons (Qlambda, XCDR (args))); | |
1263 } | |
1264 | |
1265 DEFUN ("defmacro", Fdefmacro, 2, UNEVALLED, 0, /* | |
1266 \(defmacro NAME ARGLIST [DOCSTRING] BODY...): define NAME as a macro. | |
1267 The definition is (macro lambda ARGLIST [DOCSTRING] BODY...). | |
1268 When the macro is called, as in (NAME ARGS...), | |
1269 the function (lambda ARGLIST BODY...) is applied to | |
1270 the list ARGS... as it appears in the expression, | |
1271 and the result should be a form to be evaluated instead of the original. | |
1272 */ | |
1273 (args)) | |
1274 { | |
1275 /* This function can GC */ | |
1276 return define_function (XCAR (args), | |
1277 Fcons (Qmacro, Fcons (Qlambda, XCDR (args)))); | |
1278 } | |
1279 | |
1280 DEFUN ("defvar", Fdefvar, 1, UNEVALLED, 0, /* | |
4642
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
1281 Define SYMBOL as a variable. |
428 | 1282 You are not required to define a variable in order to use it, |
1283 but the definition can supply documentation and an initial value | |
1284 in a way that tags can recognize. | |
1285 | |
1286 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is | |
1287 void. (However, when you evaluate a defvar interactively, it acts like a | |
1288 defconst: SYMBOL's value is always set regardless of whether it's currently | |
1289 void.) | |
1290 If SYMBOL is buffer-local, its default value is what is set; | |
1291 buffer-local values are not affected. | |
1292 INITVALUE and DOCSTRING are optional. | |
1293 If DOCSTRING starts with *, this variable is identified as a user option. | |
442 | 1294 This means that M-x set-variable recognizes it. |
428 | 1295 If INITVALUE is missing, SYMBOL's value is not set. |
1296 | |
1297 In lisp-interaction-mode defvar is treated as defconst. | |
4642
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
1298 |
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
1299 arguments: (SYMBOL &optional INITVALUE DOCSTRING) |
428 | 1300 */ |
1301 (args)) | |
1302 { | |
1303 /* This function can GC */ | |
1304 Lisp_Object sym = XCAR (args); | |
1305 | |
1306 if (!NILP (args = XCDR (args))) | |
1307 { | |
1308 Lisp_Object val = XCAR (args); | |
1309 | |
1310 if (NILP (Fdefault_boundp (sym))) | |
1311 { | |
1312 struct gcpro gcpro1; | |
1313 GCPRO1 (val); | |
1314 val = Feval (val); | |
1315 Fset_default (sym, val); | |
1316 UNGCPRO; | |
1317 } | |
1318 | |
1319 if (!NILP (args = XCDR (args))) | |
1320 { | |
1321 Lisp_Object doc = XCAR (args); | |
1322 Fput (sym, Qvariable_documentation, doc); | |
1323 if (!NILP (args = XCDR (args))) | |
563 | 1324 signal_error (Qwrong_number_of_arguments, "too many arguments", Qunbound); |
428 | 1325 } |
1326 } | |
1327 | |
1328 #ifdef I18N3 | |
1329 if (!NILP (Vfile_domain)) | |
1330 Fput (sym, Qvariable_domain, Vfile_domain); | |
1331 #endif | |
1332 | |
1333 LOADHIST_ATTACH (sym); | |
1334 return sym; | |
1335 } | |
1336 | |
1337 DEFUN ("defconst", Fdefconst, 2, UNEVALLED, 0, /* | |
4642
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
1338 Define SYMBOL as a constant variable. |
428 | 1339 The intent is that programs do not change this value, but users may. |
1340 Always sets the value of SYMBOL to the result of evalling INITVALUE. | |
1341 If SYMBOL is buffer-local, its default value is what is set; | |
1342 buffer-local values are not affected. | |
1343 DOCSTRING is optional. | |
1344 If DOCSTRING starts with *, this variable is identified as a user option. | |
442 | 1345 This means that M-x set-variable recognizes it. |
428 | 1346 |
1347 Note: do not use `defconst' for user options in libraries that are not | |
1348 normally loaded, since it is useful for users to be able to specify | |
1349 their own values for such variables before loading the library. | |
1350 Since `defconst' unconditionally assigns the variable, | |
1351 it would override the user's choice. | |
4642
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
1352 |
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
1353 arguments: (SYMBOL &optional INITVALUE DOCSTRING) |
428 | 1354 */ |
1355 (args)) | |
1356 { | |
1357 /* This function can GC */ | |
1358 Lisp_Object sym = XCAR (args); | |
1359 Lisp_Object val = Feval (XCAR (args = XCDR (args))); | |
1360 struct gcpro gcpro1; | |
1361 | |
1362 GCPRO1 (val); | |
1363 | |
1364 Fset_default (sym, val); | |
1365 | |
1366 UNGCPRO; | |
1367 | |
1368 if (!NILP (args = XCDR (args))) | |
1369 { | |
1370 Lisp_Object doc = XCAR (args); | |
1371 Fput (sym, Qvariable_documentation, doc); | |
1372 if (!NILP (args = XCDR (args))) | |
563 | 1373 signal_error (Qwrong_number_of_arguments, "too many arguments", Qunbound); |
428 | 1374 } |
1375 | |
1376 #ifdef I18N3 | |
1377 if (!NILP (Vfile_domain)) | |
1378 Fput (sym, Qvariable_domain, Vfile_domain); | |
1379 #endif | |
1380 | |
1381 LOADHIST_ATTACH (sym); | |
1382 return sym; | |
1383 } | |
1384 | |
4502
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4162
diff
changeset
|
1385 /* XEmacs: user-variable-p is in symbols.c, since it needs to mess around |
8748a3f7ceb4
Handle varalias chains, custom variables in #'user-variable-p.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4162
diff
changeset
|
1386 with the symbol variable aliases. */ |
428 | 1387 |
1388 DEFUN ("macroexpand-internal", Fmacroexpand_internal, 1, 2, 0, /* | |
1389 Return result of expanding macros at top level of FORM. | |
1390 If FORM is not a macro call, it is returned unchanged. | |
1391 Otherwise, the macro is expanded and the expansion is considered | |
1392 in place of FORM. When a non-macro-call results, it is returned. | |
1393 | |
442 | 1394 The second optional arg ENVIRONMENT specifies an environment of macro |
428 | 1395 definitions to shadow the loaded ones for use in file byte-compilation. |
1396 */ | |
442 | 1397 (form, environment)) |
428 | 1398 { |
1399 /* This function can GC */ | |
1400 /* With cleanups from Hallvard Furuseth. */ | |
1401 REGISTER Lisp_Object expander, sym, def, tem; | |
1402 | |
1403 while (1) | |
1404 { | |
1405 /* Come back here each time we expand a macro call, | |
1406 in case it expands into another macro call. */ | |
1407 if (!CONSP (form)) | |
1408 break; | |
1409 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */ | |
1410 def = sym = XCAR (form); | |
1411 tem = Qnil; | |
1412 /* Trace symbols aliases to other symbols | |
1413 until we get a symbol that is not an alias. */ | |
1414 while (SYMBOLP (def)) | |
1415 { | |
1416 QUIT; | |
1417 sym = def; | |
442 | 1418 tem = Fassq (sym, environment); |
428 | 1419 if (NILP (tem)) |
1420 { | |
1421 def = XSYMBOL (sym)->function; | |
1422 if (!UNBOUNDP (def)) | |
1423 continue; | |
1424 } | |
1425 break; | |
1426 } | |
442 | 1427 /* Right now TEM is the result from SYM in ENVIRONMENT, |
428 | 1428 and if TEM is nil then DEF is SYM's function definition. */ |
1429 if (NILP (tem)) | |
1430 { | |
442 | 1431 /* SYM is not mentioned in ENVIRONMENT. |
428 | 1432 Look at its function definition. */ |
1433 if (UNBOUNDP (def) | |
1434 || !CONSP (def)) | |
1435 /* Not defined or definition not suitable */ | |
1436 break; | |
1437 if (EQ (XCAR (def), Qautoload)) | |
1438 { | |
1439 /* Autoloading function: will it be a macro when loaded? */ | |
1440 tem = Felt (def, make_int (4)); | |
1441 if (EQ (tem, Qt) || EQ (tem, Qmacro)) | |
1442 { | |
1443 /* Yes, load it and try again. */ | |
970 | 1444 /* do_autoload GCPROs both arguments */ |
428 | 1445 do_autoload (def, sym); |
1446 continue; | |
1447 } | |
1448 else | |
1449 break; | |
1450 } | |
1451 else if (!EQ (XCAR (def), Qmacro)) | |
1452 break; | |
1453 else expander = XCDR (def); | |
1454 } | |
1455 else | |
1456 { | |
1457 expander = XCDR (tem); | |
1458 if (NILP (expander)) | |
1459 break; | |
1460 } | |
1461 form = apply1 (expander, XCDR (form)); | |
1462 } | |
1463 return form; | |
1464 } | |
1465 | |
1466 | |
1467 /************************************************************************/ | |
1468 /* Non-local exits */ | |
1469 /************************************************************************/ | |
1470 | |
1318 | 1471 #ifdef ERROR_CHECK_TRAPPING_PROBLEMS |
1472 | |
1473 int | |
1474 proper_redisplay_wrapping_in_place (void) | |
1475 { | |
1476 return !in_display | |
1477 || ((get_inhibit_flags () & INTERNAL_INHIBIT_ERRORS) | |
1478 && (get_inhibit_flags () & INTERNAL_INHIBIT_THROWS)); | |
1479 } | |
1480 | |
1481 static void | |
1482 check_proper_critical_section_nonlocal_exit_protection (void) | |
1483 { | |
1484 assert_with_message | |
1485 (proper_redisplay_wrapping_in_place (), | |
1486 "Attempted non-local exit from within redisplay without being properly wrapped"); | |
1487 } | |
1488 | |
1489 static void | |
1490 check_proper_critical_section_lisp_protection (void) | |
1491 { | |
1492 assert_with_message | |
1493 (proper_redisplay_wrapping_in_place (), | |
1494 "Attempt to call Lisp code from within redisplay without being properly wrapped"); | |
1495 } | |
1496 | |
1497 #endif /* ERROR_CHECK_TRAPPING_PROBLEMS */ | |
1498 | |
428 | 1499 DEFUN ("catch", Fcatch, 1, UNEVALLED, 0, /* |
1500 \(catch TAG BODY...): eval BODY allowing nonlocal exits using `throw'. | |
1501 TAG is evalled to get the tag to use. Then the BODY is executed. | |
3577 | 1502 Within BODY, (throw TAG VAL) with same (`eq') tag exits BODY and this `catch'. |
428 | 1503 If no throw happens, `catch' returns the value of the last BODY form. |
1504 If a throw happens, it specifies the value to return from `catch'. | |
1505 */ | |
1506 (args)) | |
1507 { | |
1508 /* This function can GC */ | |
1509 Lisp_Object tag = Feval (XCAR (args)); | |
1510 Lisp_Object body = XCDR (args); | |
2532 | 1511 return internal_catch (tag, Fprogn, body, 0, 0, 0); |
428 | 1512 } |
1513 | |
1514 /* Set up a catch, then call C function FUNC on argument ARG. | |
1515 FUNC should return a Lisp_Object. | |
1516 This is how catches are done from within C code. */ | |
1517 | |
1518 Lisp_Object | |
1519 internal_catch (Lisp_Object tag, | |
1520 Lisp_Object (*func) (Lisp_Object arg), | |
1521 Lisp_Object arg, | |
853 | 1522 int * volatile threw, |
2532 | 1523 Lisp_Object * volatile thrown_tag, |
1524 Lisp_Object * volatile backtrace_before_throw) | |
428 | 1525 { |
1526 /* This structure is made part of the chain `catchlist'. */ | |
1527 struct catchtag c; | |
1528 | |
1529 /* Fill in the components of c, and put it on the list. */ | |
1530 c.next = catchlist; | |
1531 c.tag = tag; | |
853 | 1532 c.actual_tag = Qnil; |
2532 | 1533 c.backtrace = Qnil; |
428 | 1534 c.val = Qnil; |
1535 c.backlist = backtrace_list; | |
1536 #if 0 /* FSFmacs */ | |
1537 /* #### */ | |
1538 c.handlerlist = handlerlist; | |
1539 #endif | |
1540 c.lisp_eval_depth = lisp_eval_depth; | |
1541 c.pdlcount = specpdl_depth(); | |
1542 #if 0 /* FSFmacs */ | |
1543 c.poll_suppress_count = async_timer_suppress_count; | |
1544 #endif | |
1545 c.gcpro = gcprolist; | |
1546 catchlist = &c; | |
1547 | |
1548 /* Call FUNC. */ | |
1549 if (SETJMP (c.jmp)) | |
1550 { | |
1551 /* Throw works by a longjmp that comes right here. */ | |
1552 if (threw) *threw = 1; | |
853 | 1553 if (thrown_tag) *thrown_tag = c.actual_tag; |
2532 | 1554 if (backtrace_before_throw) *backtrace_before_throw = c.backtrace; |
428 | 1555 return c.val; |
1556 } | |
1557 c.val = (*func) (arg); | |
1558 if (threw) *threw = 0; | |
853 | 1559 if (thrown_tag) *thrown_tag = Qnil; |
428 | 1560 catchlist = c.next; |
853 | 1561 check_catchlist_sanity (); |
428 | 1562 return c.val; |
1563 } | |
1564 | |
1565 | |
1566 /* Unwind the specbind, catch, and handler stacks back to CATCH, and | |
1567 jump to that CATCH, returning VALUE as the value of that catch. | |
1568 | |
2297 | 1569 This is the guts of Fthrow and Fsignal; they differ only in the |
1570 way they choose the catch tag to throw to. A catch tag for a | |
428 | 1571 condition-case form has a TAG of Qnil. |
1572 | |
1573 Before each catch is discarded, unbind all special bindings and | |
1574 execute all unwind-protect clauses made above that catch. Unwind | |
1575 the handler stack as we go, so that the proper handlers are in | |
1576 effect for each unwind-protect clause we run. At the end, restore | |
1577 some static info saved in CATCH, and longjmp to the location | |
1578 specified in the | |
1579 | |
1580 This is used for correct unwinding in Fthrow and Fsignal. */ | |
1581 | |
2268 | 1582 static DECLARE_DOESNT_RETURN (unwind_to_catch (struct catchtag *, Lisp_Object, |
1583 Lisp_Object)); | |
1584 | |
1585 static DOESNT_RETURN | |
853 | 1586 unwind_to_catch (struct catchtag *c, Lisp_Object val, Lisp_Object tag) |
428 | 1587 { |
1588 REGISTER int last_time; | |
1589 | |
1590 /* Unwind the specbind, catch, and handler stacks back to CATCH | |
1591 Before each catch is discarded, unbind all special bindings | |
1592 and execute all unwind-protect clauses made above that catch. | |
1593 At the end, restore some static info saved in CATCH, | |
1594 and longjmp to the location specified. | |
1595 */ | |
1596 | |
1597 /* Save the value somewhere it will be GC'ed. | |
1598 (Can't overwrite tag slot because an unwind-protect may | |
1599 want to throw to this same tag, which isn't yet invalid.) */ | |
1600 c->val = val; | |
853 | 1601 c->actual_tag = tag; |
428 | 1602 |
1603 #if 0 /* FSFmacs */ | |
1604 /* Restore the polling-suppression count. */ | |
1605 set_poll_suppress_count (catch->poll_suppress_count); | |
1606 #endif | |
1607 | |
617 | 1608 #if 1 |
428 | 1609 do |
1610 { | |
1611 last_time = catchlist == c; | |
1612 | |
1613 /* Unwind the specpdl stack, and then restore the proper set of | |
1614 handlers. */ | |
771 | 1615 unbind_to (catchlist->pdlcount); |
428 | 1616 catchlist = catchlist->next; |
853 | 1617 check_catchlist_sanity (); |
428 | 1618 } |
1619 while (! last_time); | |
617 | 1620 #else |
1621 /* Former XEmacs code. This is definitely not as correct because | |
1622 there may be a number of catches we're unwinding, and a number | |
1623 of unwind-protects in the process. By not undoing the catches till | |
1624 the end, there may be invalid catches still current. (This would | |
1625 be a particular problem with code like this: | |
1626 | |
1627 (catch 'foo | |
1628 (call-some-code-which-does... | |
1629 (catch 'bar | |
1630 (unwind-protect | |
1631 (call-some-code-which-does... | |
1632 (catch 'bar | |
1633 (call-some-code-which-does... | |
1634 (throw 'foo nil)))) | |
1635 (throw 'bar nil))))) | |
1636 | |
1637 This would try to throw to the inner (catch 'bar)! | |
1638 | |
1639 --ben | |
1640 */ | |
428 | 1641 /* Unwind the specpdl stack */ |
771 | 1642 unbind_to (c->pdlcount); |
428 | 1643 catchlist = c->next; |
853 | 1644 check_catchlist_sanity (); |
617 | 1645 #endif /* Former code */ |
428 | 1646 |
1204 | 1647 UNWIND_GCPRO_TO (c->gcpro); |
1292 | 1648 if (profiling_active) |
1649 { | |
1650 while (backtrace_list != c->backlist) | |
1651 { | |
1652 profile_record_unwind (backtrace_list); | |
1653 backtrace_list = backtrace_list->next; | |
1654 } | |
1655 } | |
1656 else | |
1657 backtrace_list = c->backlist; | |
428 | 1658 lisp_eval_depth = c->lisp_eval_depth; |
1659 | |
442 | 1660 #ifdef DEFEND_AGAINST_THROW_RECURSION |
428 | 1661 throw_level = 0; |
1662 #endif | |
1663 LONGJMP (c->jmp, 1); | |
1664 } | |
1665 | |
2268 | 1666 static DECLARE_DOESNT_RETURN (throw_or_bomb_out (Lisp_Object, Lisp_Object, int, |
1667 Lisp_Object, Lisp_Object)); | |
1668 | |
428 | 1669 static DOESNT_RETURN |
1670 throw_or_bomb_out (Lisp_Object tag, Lisp_Object val, int bomb_out_p, | |
1671 Lisp_Object sig, Lisp_Object data) | |
1672 { | |
442 | 1673 #ifdef DEFEND_AGAINST_THROW_RECURSION |
428 | 1674 /* die if we recurse more than is reasonable */ |
1675 if (++throw_level > 20) | |
2500 | 1676 ABORT (); |
428 | 1677 #endif |
1678 | |
1318 | 1679 #ifdef ERROR_CHECK_TRAPPING_PROBLEMS |
1123 | 1680 check_proper_critical_section_nonlocal_exit_protection (); |
1318 | 1681 #endif |
1123 | 1682 |
428 | 1683 /* If bomb_out_p is t, this is being called from Fsignal as a |
1684 "last resort" when there is no handler for this error and | |
1685 the debugger couldn't be invoked, so we are throwing to | |
3025 | 1686 `top-level'. If this tag doesn't exist (happens during the |
428 | 1687 initialization stages) we would get in an infinite recursive |
1688 Fsignal/Fthrow loop, so instead we bomb out to the | |
1689 really-early-error-handler. | |
1690 | |
1691 Note that in fact the only time that the "last resort" | |
3025 | 1692 occurs is when there's no catch for `top-level' -- the |
1693 `top-level' catch and the catch-all error handler are | |
428 | 1694 established at the same time, in initial_command_loop/ |
1695 top_level_1. | |
1696 | |
853 | 1697 [[#### Fix this horrifitude!]] |
1698 | |
1699 I don't think this is horrifitude, just defensive programming. --ben | |
428 | 1700 */ |
1701 | |
1702 while (1) | |
1703 { | |
1704 REGISTER struct catchtag *c; | |
1705 | |
1706 #if 0 /* FSFmacs */ | |
1707 if (!NILP (tag)) /* #### */ | |
1708 #endif | |
1709 for (c = catchlist; c; c = c->next) | |
1710 { | |
2532 | 1711 if (EQ (c->tag, Vcatch_everything_tag)) |
1712 c->backtrace = maybe_get_trapping_problems_backtrace (); | |
853 | 1713 if (EQ (c->tag, tag) || EQ (c->tag, Vcatch_everything_tag)) |
1714 unwind_to_catch (c, val, tag); | |
428 | 1715 } |
1716 if (!bomb_out_p) | |
1717 tag = Fsignal (Qno_catch, list2 (tag, val)); | |
1718 else | |
1719 call1 (Qreally_early_error_handler, Fcons (sig, data)); | |
1720 } | |
1721 } | |
1722 | |
1723 /* See above, where CATCHLIST is defined, for a description of how | |
1724 Fthrow() works. | |
1725 | |
1726 Fthrow() is also called by Fsignal(), to do a non-local jump | |
1727 back to the appropriate condition-case handler after (maybe) | |
1728 the debugger is entered. In that case, TAG is the value | |
1729 of Vcondition_handlers that was in place just after the | |
1730 condition-case handler was set up. The car of this will be | |
1731 some data referring to the handler: Its car will be Qunbound | |
1732 (thus, this tag can never be generated by Lisp code), and | |
1733 its CDR will be the HANDLERS argument to condition_case_1() | |
1734 (either Qerror, Qt, or a list of handlers as in `condition-case'). | |
1735 This works fine because Fthrow() does not care what TAG was | |
1736 passed to it: it just looks up the catch list for something | |
1737 that is EQ() to TAG. When it finds it, it will longjmp() | |
1738 back to the place that established the catch (in this case, | |
1739 condition_case_1). See below for more info. | |
1740 */ | |
1741 | |
2268 | 1742 DEFUN_NORETURN ("throw", Fthrow, 2, 2, 0, /* |
444 | 1743 Throw to the catch for TAG and return VALUE from it. |
2297 | 1744 Both TAG and VALUE are evalled. Tags are the same iff they are `eq'. |
428 | 1745 */ |
444 | 1746 (tag, value)) |
1747 { | |
1748 throw_or_bomb_out (tag, value, 0, Qnil, Qnil); /* Doesn't return */ | |
2268 | 1749 RETURN_NOT_REACHED (Qnil); |
428 | 1750 } |
1751 | |
1752 DEFUN ("unwind-protect", Funwind_protect, 1, UNEVALLED, 0, /* | |
1753 Do BODYFORM, protecting with UNWINDFORMS. | |
1754 Usage looks like (unwind-protect BODYFORM UNWINDFORMS...). | |
1755 If BODYFORM completes normally, its value is returned | |
1756 after executing the UNWINDFORMS. | |
1757 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway. | |
1758 */ | |
1759 (args)) | |
1760 { | |
1761 /* This function can GC */ | |
1762 int speccount = specpdl_depth(); | |
1763 | |
1764 record_unwind_protect (Fprogn, XCDR (args)); | |
771 | 1765 return unbind_to_1 (speccount, Feval (XCAR (args))); |
428 | 1766 } |
1767 | |
1768 | |
1769 /************************************************************************/ | |
1292 | 1770 /* Trapping errors */ |
428 | 1771 /************************************************************************/ |
1772 | |
1773 static Lisp_Object | |
1774 condition_bind_unwind (Lisp_Object loser) | |
1775 { | |
617 | 1776 /* There is no problem freeing stuff here like there is in |
1777 condition_case_unwind(), because there are no outside pointers | |
1778 (like the tag below in the catchlist) pointing to the objects. */ | |
853 | 1779 |
428 | 1780 /* ((handler-fun . handler-args) ... other handlers) */ |
1781 Lisp_Object tem = XCAR (loser); | |
853 | 1782 int first = 1; |
428 | 1783 |
1784 while (CONSP (tem)) | |
1785 { | |
853 | 1786 Lisp_Object victim = tem; |
1787 if (first && OPAQUE_PTRP (XCAR (victim))) | |
1788 free_opaque_ptr (XCAR (victim)); | |
1789 first = 0; | |
1790 tem = XCDR (victim); | |
428 | 1791 free_cons (victim); |
1792 } | |
1793 | |
1794 if (EQ (loser, Vcondition_handlers)) /* may have been rebound to some tail */ | |
853 | 1795 Vcondition_handlers = XCDR (loser); |
1796 | |
1797 free_cons (loser); | |
428 | 1798 return Qnil; |
1799 } | |
1800 | |
1801 static Lisp_Object | |
1802 condition_case_unwind (Lisp_Object loser) | |
1803 { | |
1804 /* ((<unbound> . clauses) ... other handlers */ | |
617 | 1805 /* NO! Doing this now leaves the tag deleted in a still-active |
1806 catch. With the recent changes to unwind_to_catch(), the | |
1807 evil situation might not happen any more; it certainly could | |
1808 happen before because it did. But it's very precarious to rely | |
1809 on something like this. #### Instead we should rewrite, adopting | |
1810 the FSF's mechanism with a struct handler instead of | |
1811 Vcondition_handlers; then we have NO Lisp-object structures used | |
1812 to hold all of the values, and there's no possibility either of | |
1813 crashes from freeing objects too quickly, or objects not getting | |
1814 freed and hanging around till the next GC. | |
1815 | |
1816 In practice, the extra consing here should not matter because | |
1817 it only happens when we throw past the condition-case, which almost | |
1818 always is the result of an error. Most of the time, there will be | |
1819 no error, and we will free the objects below in the main function. | |
1820 | |
1821 --ben | |
1822 | |
1823 DO NOT DO: free_cons (XCAR (loser)); | |
1824 */ | |
1825 | |
428 | 1826 if (EQ (loser, Vcondition_handlers)) /* may have been rebound to some tail */ |
617 | 1827 Vcondition_handlers = XCDR (loser); |
1828 | |
1829 /* DO NOT DO: free_cons (loser); */ | |
428 | 1830 return Qnil; |
1831 } | |
1832 | |
1833 /* Split out from condition_case_3 so that primitive C callers | |
1834 don't have to cons up a lisp handler form to be evaluated. */ | |
1835 | |
1836 /* Call a function BFUN of one argument BARG, trapping errors as | |
1837 specified by HANDLERS. If no error occurs that is indicated by | |
1838 HANDLERS as something to be caught, the return value of this | |
1839 function is the return value from BFUN. If such an error does | |
1840 occur, HFUN is called, and its return value becomes the | |
1841 return value of condition_case_1(). The second argument passed | |
1842 to HFUN will always be HARG. The first argument depends on | |
1843 HANDLERS: | |
1844 | |
1845 If HANDLERS is Qt, all errors (this includes QUIT, but not | |
1846 non-local exits with `throw') cause HFUN to be invoked, and VAL | |
1847 (the first argument to HFUN) is a cons (SIG . DATA) of the | |
1848 arguments passed to `signal'. The debugger is not invoked even if | |
1849 `debug-on-error' was set. | |
1850 | |
1851 A HANDLERS value of Qerror is the same as Qt except that the | |
1852 debugger is invoked if `debug-on-error' was set. | |
1853 | |
1854 Otherwise, HANDLERS should be a list of lists (CONDITION-NAME BODY ...) | |
1855 exactly as in `condition-case', and errors will be trapped | |
1856 as indicated in HANDLERS. VAL (the first argument to HFUN) will | |
1857 be a cons whose car is the cons (SIG . DATA) and whose CDR is the | |
1858 list (BODY ...) from the appropriate slot in HANDLERS. | |
1859 | |
1860 This function pushes HANDLERS onto the front of Vcondition_handlers | |
1861 (actually with a Qunbound marker as well -- see Fthrow() above | |
1862 for why), establishes a catch whose tag is this new value of | |
1863 Vcondition_handlers, and calls BFUN. When Fsignal() is called, | |
1864 it calls Fthrow(), setting TAG to this same new value of | |
1865 Vcondition_handlers and setting VAL to the same thing that will | |
1866 be passed to HFUN, as above. Fthrow() longjmp()s back to the | |
1867 jump point we just established, and we in turn just call the | |
1868 HFUN and return its value. | |
1869 | |
1870 For a real condition-case, HFUN will always be | |
1871 run_condition_case_handlers() and HARG is the argument VAR | |
1872 to condition-case. That function just binds VAR to the cons | |
1873 (SIG . DATA) that is the CAR of VAL, and calls the handler | |
1874 (BODY ...) that is the CDR of VAL. Note that before calling | |
1875 Fthrow(), Fsignal() restored Vcondition_handlers to the value | |
1876 it had *before* condition_case_1() was called. This maintains | |
1877 consistency (so that the state of things at exit of | |
1878 condition_case_1() is the same as at entry), and implies | |
1879 that the handler can signal the same error again (possibly | |
1880 after processing of its own), without getting in an infinite | |
1881 loop. */ | |
1882 | |
1883 Lisp_Object | |
1884 condition_case_1 (Lisp_Object handlers, | |
1885 Lisp_Object (*bfun) (Lisp_Object barg), | |
1886 Lisp_Object barg, | |
1887 Lisp_Object (*hfun) (Lisp_Object val, Lisp_Object harg), | |
1888 Lisp_Object harg) | |
1889 { | |
1890 int speccount = specpdl_depth(); | |
1891 struct catchtag c; | |
617 | 1892 struct gcpro gcpro1, gcpro2, gcpro3; |
428 | 1893 |
1894 #if 0 /* FSFmacs */ | |
1895 c.tag = Qnil; | |
1896 #else | |
1897 /* Do consing now so out-of-memory error happens up front */ | |
1898 /* (unbound . stuff) is a special condition-case kludge marker | |
1899 which is known specially by Fsignal. | |
617 | 1900 [[ This is an abomination, but to fix it would require either |
428 | 1901 making condition_case cons (a union of the conditions of the clauses) |
617 | 1902 or changing the byte-compiler output (no thanks).]] |
1903 | |
1904 The above comment is clearly wrong. FSF does not do it this way | |
1905 and did not change the byte-compiler output. Instead they use a | |
1906 `struct handler' to hold the various values (in place of our | |
1907 Vcondition_handlers) and chain them together, with pointers from | |
1908 the `struct catchtag' to the `struct handler'. We should perhaps | |
1909 consider moving to something similar, but not before I merge my | |
1910 stderr-proc workspace, which contains changes to these | |
1911 functions. --ben */ | |
428 | 1912 c.tag = noseeum_cons (noseeum_cons (Qunbound, handlers), |
1913 Vcondition_handlers); | |
1914 #endif | |
1915 c.val = Qnil; | |
853 | 1916 c.actual_tag = Qnil; |
2532 | 1917 c.backtrace = Qnil; |
428 | 1918 c.backlist = backtrace_list; |
1919 #if 0 /* FSFmacs */ | |
1920 /* #### */ | |
1921 c.handlerlist = handlerlist; | |
1922 #endif | |
1923 c.lisp_eval_depth = lisp_eval_depth; | |
1924 c.pdlcount = specpdl_depth(); | |
1925 #if 0 /* FSFmacs */ | |
1926 c.poll_suppress_count = async_timer_suppress_count; | |
1927 #endif | |
1928 c.gcpro = gcprolist; | |
1929 /* #### FSFmacs does the following statement *after* the setjmp(). */ | |
1930 c.next = catchlist; | |
1931 | |
1932 if (SETJMP (c.jmp)) | |
1933 { | |
1934 /* throw does ungcpro, etc */ | |
1935 return (*hfun) (c.val, harg); | |
1936 } | |
1937 | |
1938 record_unwind_protect (condition_case_unwind, c.tag); | |
1939 | |
1940 catchlist = &c; | |
1941 #if 0 /* FSFmacs */ | |
1942 h.handler = handlers; | |
1943 h.var = Qnil; | |
1944 h.next = handlerlist; | |
1945 h.tag = &c; | |
1946 handlerlist = &h; | |
1947 #else | |
1948 Vcondition_handlers = c.tag; | |
1949 #endif | |
1950 GCPRO1 (harg); /* Somebody has to gc-protect */ | |
1951 c.val = ((*bfun) (barg)); | |
1952 UNGCPRO; | |
617 | 1953 |
1954 /* Once we change `catchlist' below, the stuff in c will not be GCPRO'd. */ | |
1955 GCPRO3 (harg, c.val, c.tag); | |
1956 | |
428 | 1957 catchlist = c.next; |
853 | 1958 check_catchlist_sanity (); |
617 | 1959 /* Note: The unbind also resets Vcondition_handlers. Maybe we should |
1960 delete this here. */ | |
428 | 1961 Vcondition_handlers = XCDR (c.tag); |
771 | 1962 unbind_to (speccount); |
617 | 1963 |
1964 UNGCPRO; | |
1965 /* free the conses *after* the unbind, because the unbind will run | |
1966 condition_case_unwind above. */ | |
853 | 1967 free_cons (XCAR (c.tag)); |
1968 free_cons (c.tag); | |
617 | 1969 return c.val; |
428 | 1970 } |
1971 | |
1972 static Lisp_Object | |
1973 run_condition_case_handlers (Lisp_Object val, Lisp_Object var) | |
1974 { | |
1975 /* This function can GC */ | |
1976 #if 0 /* FSFmacs */ | |
1977 if (!NILP (h.var)) | |
1978 specbind (h.var, c.val); | |
1979 val = Fprogn (Fcdr (h.chosen_clause)); | |
1980 | |
1981 /* Note that this just undoes the binding of h.var; whoever | |
1982 longjmp()ed to us unwound the stack to c.pdlcount before | |
1983 throwing. */ | |
771 | 1984 unbind_to (c.pdlcount); |
428 | 1985 return val; |
1986 #else | |
1987 int speccount; | |
1988 | |
1989 CHECK_TRUE_LIST (val); | |
1990 if (NILP (var)) | |
1991 return Fprogn (Fcdr (val)); /* tail call */ | |
1992 | |
1993 speccount = specpdl_depth(); | |
1994 specbind (var, Fcar (val)); | |
1995 val = Fprogn (Fcdr (val)); | |
771 | 1996 return unbind_to_1 (speccount, val); |
428 | 1997 #endif |
1998 } | |
1999 | |
2000 /* Here for bytecode to call non-consfully. This is exactly like | |
2001 condition-case except that it takes three arguments rather | |
2002 than a single list of arguments. */ | |
2003 Lisp_Object | |
2004 condition_case_3 (Lisp_Object bodyform, Lisp_Object var, Lisp_Object handlers) | |
2005 { | |
2006 /* This function can GC */ | |
2007 EXTERNAL_LIST_LOOP_2 (handler, handlers) | |
2008 { | |
2009 if (NILP (handler)) | |
2010 ; | |
2011 else if (CONSP (handler)) | |
2012 { | |
2013 Lisp_Object conditions = XCAR (handler); | |
2014 /* CONDITIONS must a condition name or a list of condition names */ | |
2015 if (SYMBOLP (conditions)) | |
2016 ; | |
2017 else | |
2018 { | |
2019 EXTERNAL_LIST_LOOP_2 (condition, conditions) | |
2020 if (!SYMBOLP (condition)) | |
2021 goto invalid_condition_handler; | |
2022 } | |
2023 } | |
2024 else | |
2025 { | |
2026 invalid_condition_handler: | |
563 | 2027 sferror ("Invalid condition handler", handler); |
428 | 2028 } |
2029 } | |
2030 | |
2031 CHECK_SYMBOL (var); | |
2032 | |
2033 return condition_case_1 (handlers, | |
2034 Feval, bodyform, | |
2035 run_condition_case_handlers, | |
2036 var); | |
2037 } | |
2038 | |
2039 DEFUN ("condition-case", Fcondition_case, 2, UNEVALLED, 0, /* | |
2040 Regain control when an error is signalled. | |
2041 Usage looks like (condition-case VAR BODYFORM HANDLERS...). | |
2042 Executes BODYFORM and returns its value if no error happens. | |
2043 Each element of HANDLERS looks like (CONDITION-NAME BODY...) | |
2044 where the BODY is made of Lisp expressions. | |
2045 | |
771 | 2046 A typical usage of `condition-case' looks like this: |
2047 | |
2048 (condition-case nil | |
2049 ;; you need a progn here if you want more than one statement ... | |
2050 (progn | |
2051 (do-something) | |
2052 (do-something-else)) | |
2053 (error | |
2054 (issue-warning-or) | |
2055 ;; but strangely, you don't need one here. | |
2056 (return-a-value-etc) | |
2057 )) | |
2058 | |
428 | 2059 A handler is applicable to an error if CONDITION-NAME is one of the |
2060 error's condition names. If an error happens, the first applicable | |
2061 handler is run. As a special case, a CONDITION-NAME of t matches | |
2062 all errors, even those without the `error' condition name on them | |
2063 \(e.g. `quit'). | |
2064 | |
2065 The car of a handler may be a list of condition names | |
2066 instead of a single condition name. | |
2067 | |
2068 When a handler handles an error, | |
2069 control returns to the condition-case and the handler BODY... is executed | |
2070 with VAR bound to (SIGNALED-CONDITIONS . SIGNAL-DATA). | |
2071 VAR may be nil; then you do not get access to the signal information. | |
2072 | |
2073 The value of the last BODY form is returned from the condition-case. | |
2074 See also the function `signal' for more info. | |
2075 | |
2076 Note that at the time the condition handler is invoked, the Lisp stack | |
2077 and the current catches, condition-cases, and bindings have all been | |
2078 popped back to the state they were in just before the call to | |
2079 `condition-case'. This means that resignalling the error from | |
2080 within the handler will not result in an infinite loop. | |
2081 | |
2082 If you want to establish an error handler that is called with the | |
2083 Lisp stack, bindings, etc. as they were when `signal' was called, | |
2084 rather than when the handler was set, use `call-with-condition-handler'. | |
2085 */ | |
2086 (args)) | |
2087 { | |
2088 /* This function can GC */ | |
2089 Lisp_Object var = XCAR (args); | |
2090 Lisp_Object bodyform = XCAR (XCDR (args)); | |
2091 Lisp_Object handlers = XCDR (XCDR (args)); | |
2092 return condition_case_3 (bodyform, var, handlers); | |
2093 } | |
2094 | |
2095 DEFUN ("call-with-condition-handler", Fcall_with_condition_handler, 2, MANY, 0, /* | |
2096 Regain control when an error is signalled, without popping the stack. | |
2097 Usage looks like (call-with-condition-handler HANDLER FUNCTION &rest ARGS). | |
2098 This function is similar to `condition-case', but the handler is invoked | |
2099 with the same environment (Lisp stack, bindings, catches, condition-cases) | |
2100 that was current when `signal' was called, rather than when the handler | |
2101 was established. | |
2102 | |
2103 HANDLER should be a function of one argument, which is a cons of the args | |
2104 \(SIG . DATA) that were passed to `signal'. It is invoked whenever | |
2105 `signal' is called (this differs from `condition-case', which allows | |
2106 you to specify which errors are trapped). If the handler function | |
2107 returns, `signal' continues as if the handler were never invoked. | |
2108 \(It continues to look for handlers established earlier than this one, | |
2109 and invokes the standard error-handler if none is found.) | |
2110 */ | |
2111 (int nargs, Lisp_Object *args)) /* Note! Args side-effected! */ | |
2112 { | |
2113 /* This function can GC */ | |
2114 int speccount = specpdl_depth(); | |
2115 Lisp_Object tem; | |
2116 | |
853 | 2117 tem = Ffunction_max_args (args[0]); |
2118 if (! (XINT (Ffunction_min_args (args[0])) <= 1 | |
2119 && (NILP (tem) || 1 <= XINT (tem)))) | |
2120 invalid_argument ("Must be function of one argument", args[0]); | |
2121 | |
2122 /* (handler-fun . handler-args) but currently there are no handler-args */ | |
428 | 2123 tem = noseeum_cons (list1 (args[0]), Vcondition_handlers); |
2124 record_unwind_protect (condition_bind_unwind, tem); | |
2125 Vcondition_handlers = tem; | |
2126 | |
2127 /* Caller should have GC-protected args */ | |
771 | 2128 return unbind_to_1 (speccount, Ffuncall (nargs - 1, args + 1)); |
428 | 2129 } |
2130 | |
853 | 2131 /* This is the C version of the above function. It calls FUN, passing it |
2132 ARG, first setting up HANDLER to catch signals in the environment in | |
2133 which they were signalled. (HANDLER is only invoked if there was no | |
2134 handler (either from condition-case or call-with-condition-handler) set | |
2135 later on that handled the signal; therefore, this is a real error. | |
2136 | |
2137 HANDLER is invoked with three arguments: the ERROR-SYMBOL and DATA as | |
2138 passed to `signal', and HANDLER_ARG. Originally I made HANDLER_ARG and | |
2139 ARG be void * to facilitate passing structures, but I changed to | |
2140 Lisp_Objects because all the other C interfaces to catch/condition-case/etc. | |
2141 take Lisp_Objects, and it is easy enough to use make_opaque_ptr() et al. | |
2142 to convert between Lisp_Objects and structure pointers. */ | |
2143 | |
2144 Lisp_Object | |
2145 call_with_condition_handler (Lisp_Object (*handler) (Lisp_Object, Lisp_Object, | |
2146 Lisp_Object), | |
2147 Lisp_Object handler_arg, | |
2148 Lisp_Object (*fun) (Lisp_Object), | |
2149 Lisp_Object arg) | |
2150 { | |
2151 /* This function can GC */ | |
1111 | 2152 int speccount = specpdl_depth (); |
853 | 2153 Lisp_Object tem; |
2154 | |
2155 /* ((handler-fun . (handler-arg . nil)) ... ) */ | |
1111 | 2156 tem = noseeum_cons (noseeum_cons (make_opaque_ptr ((void *) handler), |
853 | 2157 noseeum_cons (handler_arg, Qnil)), |
2158 Vcondition_handlers); | |
2159 record_unwind_protect (condition_bind_unwind, tem); | |
2160 Vcondition_handlers = tem; | |
2161 | |
2162 return unbind_to_1 (speccount, (*fun) (arg)); | |
2163 } | |
2164 | |
428 | 2165 static int |
2166 condition_type_p (Lisp_Object type, Lisp_Object conditions) | |
2167 { | |
2168 if (EQ (type, Qt)) | |
2169 /* (condition-case c # (t c)) catches -all- signals | |
2170 * Use with caution! */ | |
2171 return 1; | |
2172 | |
2173 if (SYMBOLP (type)) | |
2174 return !NILP (Fmemq (type, conditions)); | |
2175 | |
2176 for (; CONSP (type); type = XCDR (type)) | |
2177 if (!NILP (Fmemq (XCAR (type), conditions))) | |
2178 return 1; | |
2179 | |
2180 return 0; | |
2181 } | |
2182 | |
2183 static Lisp_Object | |
2184 return_from_signal (Lisp_Object value) | |
2185 { | |
2186 #if 1 | |
2187 /* Most callers are not prepared to handle gc if this | |
2188 returns. So, since this feature is not very useful, | |
2189 take it out. */ | |
2190 /* Have called debugger; return value to signaller */ | |
2191 return value; | |
2192 #else /* But the reality is that that stinks, because: */ | |
2193 /* GACK!!! Really want some way for debug-on-quit errors | |
2194 to be continuable!! */ | |
563 | 2195 signal_error (Qunimplemented, |
2196 "Returning a value from an error is no longer supported", | |
2197 Qunbound); | |
428 | 2198 #endif |
2199 } | |
2200 | |
2201 | |
2202 /************************************************************************/ | |
2203 /* the workhorse error-signaling function */ | |
2204 /************************************************************************/ | |
2205 | |
853 | 2206 /* This exists only for debugging purposes, as a place to put a breakpoint |
2207 that won't get signalled for errors occurring when | |
2208 call_with_suspended_errors() was invoked. */ | |
2209 | |
872 | 2210 /* Don't make static or it might be compiled away */ |
2211 void signal_1 (void); | |
2212 | |
2213 void | |
853 | 2214 signal_1 (void) |
2215 { | |
2216 } | |
2217 | |
428 | 2218 /* #### This function has not been synched with FSF. It diverges |
2219 significantly. */ | |
2220 | |
853 | 2221 /* The simplest external error function: it would be called |
2222 signal_continuable_error() in the terminology below, but it's | |
2223 Lisp-callable. */ | |
2224 | |
2225 DEFUN ("signal", Fsignal, 2, 2, 0, /* | |
2226 Signal a continuable error. Args are ERROR-SYMBOL, and associated DATA. | |
2227 An error symbol is a symbol defined using `define-error'. | |
2228 DATA should be a list. Its elements are printed as part of the error message. | |
2229 If the signal is handled, DATA is made available to the handler. | |
2230 See also the function `signal-error', and the functions to handle errors: | |
2231 `condition-case' and `call-with-condition-handler'. | |
2232 | |
2233 Note that this function can return, if the debugger is invoked and the | |
2234 user invokes the "return from signal" option. | |
2235 */ | |
2236 (error_symbol, data)) | |
428 | 2237 { |
2238 /* This function can GC */ | |
853 | 2239 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; |
2240 Lisp_Object conditions = Qnil; | |
2241 Lisp_Object handlers = Qnil; | |
428 | 2242 /* signal_call_debugger() could get called more than once |
2243 (once when a call-with-condition-handler is about to | |
2244 be dealt with, and another when a condition-case handler | |
2245 is about to be invoked). So make sure the debugger and/or | |
2246 stack trace aren't done more than once. */ | |
2247 int stack_trace_displayed = 0; | |
2248 int debugger_entered = 0; | |
853 | 2249 |
2250 /* Fsignal() is one of these functions that's called all the time | |
2251 with newly-created Lisp objects. We allow this; but we must GC- | |
2252 protect the objects because all sorts of weird stuff could | |
2253 happen. */ | |
2254 | |
2255 GCPRO4 (conditions, handlers, error_symbol, data); | |
2256 | |
2257 if (!(inhibit_flags & CALL_WITH_SUSPENDED_ERRORS)) | |
2258 signal_1 (); | |
428 | 2259 |
2260 if (!initialized) | |
2261 { | |
2262 /* who knows how much has been initialized? Safest bet is | |
2263 just to bomb out immediately. */ | |
771 | 2264 stderr_out ("Error before initialization is complete!\n"); |
2500 | 2265 ABORT (); |
428 | 2266 } |
2267 | |
3092 | 2268 #ifndef NEW_GC |
1123 | 2269 assert (!gc_in_progress); |
3092 | 2270 #endif /* not NEW_GC */ |
1123 | 2271 |
2272 /* We abort if in_display and we are not protected, as garbage | |
2273 collections and non-local exits will invariably be fatal, but in | |
2274 messy, difficult-to-debug ways. See enter_redisplay_critical_section(). | |
2275 */ | |
2276 | |
1318 | 2277 #ifdef ERROR_CHECK_TRAPPING_PROBLEMS |
1123 | 2278 check_proper_critical_section_nonlocal_exit_protection (); |
1318 | 2279 #endif |
428 | 2280 |
853 | 2281 conditions = Fget (error_symbol, Qerror_conditions, Qnil); |
428 | 2282 |
2283 for (handlers = Vcondition_handlers; | |
2284 CONSP (handlers); | |
2285 handlers = XCDR (handlers)) | |
2286 { | |
2287 Lisp_Object handler_fun = XCAR (XCAR (handlers)); | |
2288 Lisp_Object handler_data = XCDR (XCAR (handlers)); | |
2289 Lisp_Object outer_handlers = XCDR (handlers); | |
2290 | |
2291 if (!UNBOUNDP (handler_fun)) | |
2292 { | |
2293 /* call-with-condition-handler */ | |
2294 Lisp_Object tem; | |
2295 Lisp_Object all_handlers = Vcondition_handlers; | |
2296 struct gcpro ngcpro1; | |
2297 NGCPRO1 (all_handlers); | |
2298 Vcondition_handlers = outer_handlers; | |
2299 | |
853 | 2300 tem = signal_call_debugger (conditions, error_symbol, data, |
428 | 2301 outer_handlers, 1, |
2302 &stack_trace_displayed, | |
2303 &debugger_entered); | |
2304 if (!UNBOUNDP (tem)) | |
2305 RETURN_NUNGCPRO (return_from_signal (tem)); | |
2306 | |
853 | 2307 if (OPAQUE_PTRP (handler_fun)) |
2308 { | |
2309 if (NILP (handler_data)) | |
2310 { | |
2311 Lisp_Object (*hfun) (Lisp_Object, Lisp_Object) = | |
2312 (Lisp_Object (*) (Lisp_Object, Lisp_Object)) | |
2313 (get_opaque_ptr (handler_fun)); | |
2314 | |
2315 tem = (*hfun) (error_symbol, data); | |
2316 } | |
2317 else | |
2318 { | |
2319 Lisp_Object (*hfun) (Lisp_Object, Lisp_Object, Lisp_Object) = | |
2320 (Lisp_Object (*) (Lisp_Object, Lisp_Object, Lisp_Object)) | |
2321 (get_opaque_ptr (handler_fun)); | |
2322 | |
2323 assert (NILP (XCDR (handler_data))); | |
2324 tem = (*hfun) (error_symbol, data, XCAR (handler_data)); | |
2325 } | |
2326 } | |
2327 else | |
2328 { | |
2329 tem = Fcons (error_symbol, data); | |
2330 if (NILP (handler_data)) | |
2331 tem = call1 (handler_fun, tem); | |
2332 else | |
2333 { | |
2334 /* (This code won't be used (for now?).) */ | |
2335 struct gcpro nngcpro1; | |
2336 Lisp_Object args[3]; | |
2337 NNGCPRO1 (args[0]); | |
2338 nngcpro1.nvars = 3; | |
2339 args[0] = handler_fun; | |
2340 args[1] = tem; | |
2341 args[2] = handler_data; | |
2342 nngcpro1.var = args; | |
2343 tem = Fapply (3, args); | |
2344 NNUNGCPRO; | |
2345 } | |
2346 } | |
428 | 2347 NUNGCPRO; |
2348 #if 0 | |
2349 if (!EQ (tem, Qsignal)) | |
2350 return return_from_signal (tem); | |
2351 #endif | |
2352 /* If handler didn't throw, try another handler */ | |
2353 Vcondition_handlers = all_handlers; | |
2354 } | |
2355 | |
2356 /* It's a condition-case handler */ | |
2357 | |
2358 /* t is used by handlers for all conditions, set up by C code. | |
2359 * debugger is not called even if debug_on_error */ | |
2360 else if (EQ (handler_data, Qt)) | |
2361 { | |
2362 UNGCPRO; | |
853 | 2363 return Fthrow (handlers, Fcons (error_symbol, data)); |
428 | 2364 } |
2365 /* `error' is used similarly to the way `t' is used, but in | |
2366 addition it invokes the debugger if debug_on_error. | |
2367 This is normally used for the outer command-loop error | |
2368 handler. */ | |
2369 else if (EQ (handler_data, Qerror)) | |
2370 { | |
853 | 2371 Lisp_Object tem = signal_call_debugger (conditions, error_symbol, |
2372 data, | |
428 | 2373 outer_handlers, 0, |
2374 &stack_trace_displayed, | |
2375 &debugger_entered); | |
2376 | |
2377 UNGCPRO; | |
2378 if (!UNBOUNDP (tem)) | |
2379 return return_from_signal (tem); | |
2380 | |
853 | 2381 tem = Fcons (error_symbol, data); |
428 | 2382 return Fthrow (handlers, tem); |
2383 } | |
2384 else | |
2385 { | |
2386 /* handler established by real (Lisp) condition-case */ | |
2387 Lisp_Object h; | |
2388 | |
2389 for (h = handler_data; CONSP (h); h = Fcdr (h)) | |
2390 { | |
2391 Lisp_Object clause = Fcar (h); | |
2392 Lisp_Object tem = Fcar (clause); | |
2393 | |
2394 if (condition_type_p (tem, conditions)) | |
2395 { | |
853 | 2396 tem = signal_call_debugger (conditions, error_symbol, data, |
428 | 2397 outer_handlers, 1, |
2398 &stack_trace_displayed, | |
2399 &debugger_entered); | |
2400 UNGCPRO; | |
2401 if (!UNBOUNDP (tem)) | |
2402 return return_from_signal (tem); | |
2403 | |
2404 /* Doesn't return */ | |
853 | 2405 tem = Fcons (Fcons (error_symbol, data), Fcdr (clause)); |
428 | 2406 return Fthrow (handlers, tem); |
2407 } | |
2408 } | |
2409 } | |
2410 } | |
2411 | |
2412 /* If no handler is present now, try to run the debugger, | |
2413 and if that fails, throw to top level. | |
2414 | |
2415 #### The only time that no handler is present is during | |
2416 temacs or perhaps very early in XEmacs. In both cases, | |
3025 | 2417 there is no `top-level' catch. (That's why the |
428 | 2418 "bomb-out" hack was added.) |
2419 | |
853 | 2420 [[#### Fix this horrifitude!]] |
2421 | |
2422 I don't think this is horrifitude, but just defensive coding. --ben */ | |
2423 | |
2424 signal_call_debugger (conditions, error_symbol, data, Qnil, 0, | |
428 | 2425 &stack_trace_displayed, |
2426 &debugger_entered); | |
2427 UNGCPRO; | |
853 | 2428 throw_or_bomb_out (Qtop_level, Qt, 1, error_symbol, |
2429 data); /* Doesn't return */ | |
2268 | 2430 RETURN_NOT_REACHED (Qnil); |
428 | 2431 } |
2432 | |
2433 /****************** Error functions class 1 ******************/ | |
2434 | |
2435 /* Class 1: General functions that signal an error. | |
2436 These functions take an error type and a list of associated error | |
2437 data. */ | |
2438 | |
853 | 2439 /* No signal_continuable_error_1(); it's called Fsignal(). */ |
428 | 2440 |
2441 /* Signal a non-continuable error. */ | |
2442 | |
2443 DOESNT_RETURN | |
563 | 2444 signal_error_1 (Lisp_Object sig, Lisp_Object data) |
428 | 2445 { |
2446 for (;;) | |
2447 Fsignal (sig, data); | |
2448 } | |
853 | 2449 |
2450 #ifdef ERROR_CHECK_CATCH | |
2451 | |
2452 void | |
2453 check_catchlist_sanity (void) | |
2454 { | |
2455 #if 0 | |
2456 /* vou me tomar no cu! i just masked andy's missing-unbind | |
2457 bug! */ | |
442 | 2458 struct catchtag *c; |
2459 int found_error_tag = 0; | |
2460 | |
2461 for (c = catchlist; c; c = c->next) | |
2462 { | |
2463 if (EQ (c->tag, Qunbound_suspended_errors_tag)) | |
2464 { | |
2465 found_error_tag = 1; | |
2466 break; | |
2467 } | |
2468 } | |
2469 | |
2470 assert (found_error_tag || NILP (Vcurrent_error_state)); | |
853 | 2471 #endif /* vou me tomar no cul */ |
2472 } | |
2473 | |
2474 void | |
2475 check_specbind_stack_sanity (void) | |
2476 { | |
2477 } | |
2478 | |
2479 #endif /* ERROR_CHECK_CATCH */ | |
428 | 2480 |
2481 /* Signal a non-continuable error or display a warning or do nothing, | |
2482 according to ERRB. CLASS is the class of warning and should | |
2483 refer to what sort of operation is being done (e.g. Qtoolbar, | |
2484 Qresource, etc.). */ | |
2485 | |
2486 void | |
1204 | 2487 maybe_signal_error_1 (Lisp_Object sig, Lisp_Object data, Lisp_Object class_, |
578 | 2488 Error_Behavior errb) |
428 | 2489 { |
2490 if (ERRB_EQ (errb, ERROR_ME_NOT)) | |
2491 return; | |
793 | 2492 else if (ERRB_EQ (errb, ERROR_ME_DEBUG_WARN)) |
1204 | 2493 warn_when_safe_lispobj (class_, Qdebug, Fcons (sig, data)); |
428 | 2494 else if (ERRB_EQ (errb, ERROR_ME_WARN)) |
1204 | 2495 warn_when_safe_lispobj (class_, Qwarning, Fcons (sig, data)); |
428 | 2496 else |
2497 for (;;) | |
2498 Fsignal (sig, data); | |
2499 } | |
2500 | |
2501 /* Signal a continuable error or display a warning or do nothing, | |
2502 according to ERRB. */ | |
2503 | |
2504 Lisp_Object | |
563 | 2505 maybe_signal_continuable_error_1 (Lisp_Object sig, Lisp_Object data, |
1204 | 2506 Lisp_Object class_, Error_Behavior errb) |
428 | 2507 { |
2508 if (ERRB_EQ (errb, ERROR_ME_NOT)) | |
2509 return Qnil; | |
793 | 2510 else if (ERRB_EQ (errb, ERROR_ME_DEBUG_WARN)) |
2511 { | |
1204 | 2512 warn_when_safe_lispobj (class_, Qdebug, Fcons (sig, data)); |
793 | 2513 return Qnil; |
2514 } | |
428 | 2515 else if (ERRB_EQ (errb, ERROR_ME_WARN)) |
2516 { | |
1204 | 2517 warn_when_safe_lispobj (class_, Qwarning, Fcons (sig, data)); |
428 | 2518 return Qnil; |
2519 } | |
2520 else | |
2521 return Fsignal (sig, data); | |
2522 } | |
2523 | |
2524 | |
2525 /****************** Error functions class 2 ******************/ | |
2526 | |
563 | 2527 /* Class 2: Signal an error with a string and an associated object. |
2528 Normally these functions are used to attach one associated object, | |
2529 but to attach no objects, specify Qunbound for FROB, and for more | |
2530 than one object, make a list of the objects with Qunbound as the | |
2531 first element. (If you have specifically two objects to attach, | |
2532 consider using the function in class 3 below.) These functions | |
2533 signal an error of a specified type, whose data is one or more | |
2534 objects (usually two), a string the related Lisp object(s) | |
2535 specified as FROB. */ | |
2536 | |
2537 /* Out of REASON and FROB, return a list of elements suitable for passing | |
2538 to signal_error_1(). */ | |
2539 | |
2540 Lisp_Object | |
867 | 2541 build_error_data (const CIbyte *reason, Lisp_Object frob) |
563 | 2542 { |
2543 if (EQ (frob, Qunbound)) | |
2544 frob = Qnil; | |
2545 else if (CONSP (frob) && EQ (XCAR (frob), Qunbound)) | |
2546 frob = XCDR (frob); | |
2547 else | |
2548 frob = list1 (frob); | |
2549 if (!reason) | |
2550 return frob; | |
2551 else | |
771 | 2552 return Fcons (build_msg_string (reason), frob); |
563 | 2553 } |
2554 | |
2555 DOESNT_RETURN | |
867 | 2556 signal_error (Lisp_Object type, const CIbyte *reason, Lisp_Object frob) |
563 | 2557 { |
2558 signal_error_1 (type, build_error_data (reason, frob)); | |
2559 } | |
2560 | |
2561 void | |
867 | 2562 maybe_signal_error (Lisp_Object type, const CIbyte *reason, |
1204 | 2563 Lisp_Object frob, Lisp_Object class_, |
578 | 2564 Error_Behavior errb) |
563 | 2565 { |
2566 /* Optimization: */ | |
2567 if (ERRB_EQ (errb, ERROR_ME_NOT)) | |
2568 return; | |
1204 | 2569 maybe_signal_error_1 (type, build_error_data (reason, frob), class_, errb); |
563 | 2570 } |
2571 | |
2572 Lisp_Object | |
867 | 2573 signal_continuable_error (Lisp_Object type, const CIbyte *reason, |
563 | 2574 Lisp_Object frob) |
2575 { | |
2576 return Fsignal (type, build_error_data (reason, frob)); | |
2577 } | |
2578 | |
2579 Lisp_Object | |
867 | 2580 maybe_signal_continuable_error (Lisp_Object type, const CIbyte *reason, |
1204 | 2581 Lisp_Object frob, Lisp_Object class_, |
578 | 2582 Error_Behavior errb) |
563 | 2583 { |
2584 /* Optimization: */ | |
2585 if (ERRB_EQ (errb, ERROR_ME_NOT)) | |
2586 return Qnil; | |
2587 return maybe_signal_continuable_error_1 (type, | |
2588 build_error_data (reason, frob), | |
1204 | 2589 class_, errb); |
563 | 2590 } |
2591 | |
2592 | |
2593 /****************** Error functions class 3 ******************/ | |
2594 | |
2595 /* Class 3: Signal an error with a string and two associated objects. | |
2596 These functions signal an error of a specified type, whose data | |
2597 is three objects, a string and two related Lisp objects. | |
2598 (The equivalent could be accomplished using the class 2 functions, | |
2599 but these are more convenient in this particular case.) */ | |
2600 | |
2601 DOESNT_RETURN | |
867 | 2602 signal_error_2 (Lisp_Object type, const CIbyte *reason, |
563 | 2603 Lisp_Object frob0, Lisp_Object frob1) |
2604 { | |
771 | 2605 signal_error_1 (type, list3 (build_msg_string (reason), frob0, |
563 | 2606 frob1)); |
2607 } | |
2608 | |
2609 void | |
867 | 2610 maybe_signal_error_2 (Lisp_Object type, const CIbyte *reason, |
563 | 2611 Lisp_Object frob0, Lisp_Object frob1, |
1204 | 2612 Lisp_Object class_, Error_Behavior errb) |
563 | 2613 { |
2614 /* Optimization: */ | |
2615 if (ERRB_EQ (errb, ERROR_ME_NOT)) | |
2616 return; | |
771 | 2617 maybe_signal_error_1 (type, list3 (build_msg_string (reason), frob0, |
1204 | 2618 frob1), class_, errb); |
563 | 2619 } |
2620 | |
2621 Lisp_Object | |
867 | 2622 signal_continuable_error_2 (Lisp_Object type, const CIbyte *reason, |
563 | 2623 Lisp_Object frob0, Lisp_Object frob1) |
2624 { | |
771 | 2625 return Fsignal (type, list3 (build_msg_string (reason), frob0, |
563 | 2626 frob1)); |
2627 } | |
2628 | |
2629 Lisp_Object | |
867 | 2630 maybe_signal_continuable_error_2 (Lisp_Object type, const CIbyte *reason, |
563 | 2631 Lisp_Object frob0, Lisp_Object frob1, |
1204 | 2632 Lisp_Object class_, Error_Behavior errb) |
563 | 2633 { |
2634 /* Optimization: */ | |
2635 if (ERRB_EQ (errb, ERROR_ME_NOT)) | |
2636 return Qnil; | |
2637 return maybe_signal_continuable_error_1 | |
771 | 2638 (type, list3 (build_msg_string (reason), frob0, frob1), |
1204 | 2639 class_, errb); |
563 | 2640 } |
2641 | |
2642 | |
2643 /****************** Error functions class 4 ******************/ | |
2644 | |
2645 /* Class 4: Printf-like functions that signal an error. | |
442 | 2646 These functions signal an error of a specified type, whose data |
428 | 2647 is a single string, created using the arguments. */ |
2648 | |
2649 DOESNT_RETURN | |
867 | 2650 signal_ferror (Lisp_Object type, const CIbyte *fmt, ...) |
442 | 2651 { |
2652 Lisp_Object obj; | |
2653 va_list args; | |
2654 | |
2655 va_start (args, fmt); | |
771 | 2656 obj = emacs_vsprintf_string (CGETTEXT (fmt), args); |
442 | 2657 va_end (args); |
2658 | |
2659 /* Fsignal GC-protects its args */ | |
563 | 2660 signal_error (type, 0, obj); |
442 | 2661 } |
2662 | |
2663 void | |
1204 | 2664 maybe_signal_ferror (Lisp_Object type, Lisp_Object class_, Error_Behavior errb, |
867 | 2665 const CIbyte *fmt, ...) |
442 | 2666 { |
2667 Lisp_Object obj; | |
2668 va_list args; | |
2669 | |
2670 /* Optimization: */ | |
2671 if (ERRB_EQ (errb, ERROR_ME_NOT)) | |
2672 return; | |
2673 | |
2674 va_start (args, fmt); | |
771 | 2675 obj = emacs_vsprintf_string (CGETTEXT (fmt), args); |
442 | 2676 va_end (args); |
2677 | |
2678 /* Fsignal GC-protects its args */ | |
1204 | 2679 maybe_signal_error (type, 0, obj, class_, errb); |
442 | 2680 } |
2681 | |
2682 Lisp_Object | |
867 | 2683 signal_continuable_ferror (Lisp_Object type, const CIbyte *fmt, ...) |
428 | 2684 { |
2685 Lisp_Object obj; | |
2686 va_list args; | |
2687 | |
2688 va_start (args, fmt); | |
771 | 2689 obj = emacs_vsprintf_string (CGETTEXT (fmt), args); |
442 | 2690 va_end (args); |
2691 | |
2692 /* Fsignal GC-protects its args */ | |
2693 return Fsignal (type, list1 (obj)); | |
2694 } | |
2695 | |
2696 Lisp_Object | |
1204 | 2697 maybe_signal_continuable_ferror (Lisp_Object type, Lisp_Object class_, |
867 | 2698 Error_Behavior errb, const CIbyte *fmt, ...) |
442 | 2699 { |
2700 Lisp_Object obj; | |
2701 va_list args; | |
2702 | |
2703 /* Optimization: */ | |
2704 if (ERRB_EQ (errb, ERROR_ME_NOT)) | |
2705 return Qnil; | |
2706 | |
2707 va_start (args, fmt); | |
771 | 2708 obj = emacs_vsprintf_string (CGETTEXT (fmt), args); |
442 | 2709 va_end (args); |
2710 | |
2711 /* Fsignal GC-protects its args */ | |
1204 | 2712 return maybe_signal_continuable_error (type, 0, obj, class_, errb); |
442 | 2713 } |
2714 | |
2715 | |
2716 /****************** Error functions class 5 ******************/ | |
2717 | |
563 | 2718 /* Class 5: Printf-like functions that signal an error. |
442 | 2719 These functions signal an error of a specified type, whose data |
563 | 2720 is a one or more objects, a string (created using the arguments) |
2721 and additional Lisp objects specified in FROB. (The syntax of FROB | |
2722 is the same as for class 2.) | |
2723 | |
2724 There is no need for a class 6 because you can always attach 2 | |
2725 objects using class 5 (for FROB, specify a list with three | |
2726 elements, the first of which is Qunbound), and these functions are | |
2727 not commonly used. | |
2728 */ | |
442 | 2729 |
2730 DOESNT_RETURN | |
867 | 2731 signal_ferror_with_frob (Lisp_Object type, Lisp_Object frob, const CIbyte *fmt, |
563 | 2732 ...) |
442 | 2733 { |
2734 Lisp_Object obj; | |
2735 va_list args; | |
2736 | |
2737 va_start (args, fmt); | |
771 | 2738 obj = emacs_vsprintf_string (CGETTEXT (fmt), args); |
442 | 2739 va_end (args); |
2740 | |
2741 /* Fsignal GC-protects its args */ | |
563 | 2742 signal_error_1 (type, Fcons (obj, build_error_data (0, frob))); |
442 | 2743 } |
2744 | |
2745 void | |
563 | 2746 maybe_signal_ferror_with_frob (Lisp_Object type, Lisp_Object frob, |
1204 | 2747 Lisp_Object class_, Error_Behavior errb, |
867 | 2748 const CIbyte *fmt, ...) |
442 | 2749 { |
2750 Lisp_Object obj; | |
2751 va_list args; | |
2752 | |
2753 /* Optimization: */ | |
2754 if (ERRB_EQ (errb, ERROR_ME_NOT)) | |
2755 return; | |
2756 | |
2757 va_start (args, fmt); | |
771 | 2758 obj = emacs_vsprintf_string (CGETTEXT (fmt), args); |
428 | 2759 va_end (args); |
2760 | |
2761 /* Fsignal GC-protects its args */ | |
1204 | 2762 maybe_signal_error_1 (type, Fcons (obj, build_error_data (0, frob)), class_, |
563 | 2763 errb); |
428 | 2764 } |
2765 | |
2766 Lisp_Object | |
563 | 2767 signal_continuable_ferror_with_frob (Lisp_Object type, Lisp_Object frob, |
867 | 2768 const CIbyte *fmt, ...) |
428 | 2769 { |
2770 Lisp_Object obj; | |
2771 va_list args; | |
2772 | |
2773 va_start (args, fmt); | |
771 | 2774 obj = emacs_vsprintf_string (CGETTEXT (fmt), args); |
428 | 2775 va_end (args); |
2776 | |
2777 /* Fsignal GC-protects its args */ | |
563 | 2778 return Fsignal (type, Fcons (obj, build_error_data (0, frob))); |
428 | 2779 } |
2780 | |
2781 Lisp_Object | |
563 | 2782 maybe_signal_continuable_ferror_with_frob (Lisp_Object type, Lisp_Object frob, |
1204 | 2783 Lisp_Object class_, |
578 | 2784 Error_Behavior errb, |
867 | 2785 const CIbyte *fmt, ...) |
428 | 2786 { |
2787 Lisp_Object obj; | |
2788 va_list args; | |
2789 | |
2790 /* Optimization: */ | |
2791 if (ERRB_EQ (errb, ERROR_ME_NOT)) | |
2792 return Qnil; | |
2793 | |
2794 va_start (args, fmt); | |
771 | 2795 obj = emacs_vsprintf_string (CGETTEXT (fmt), args); |
428 | 2796 va_end (args); |
2797 | |
2798 /* Fsignal GC-protects its args */ | |
563 | 2799 return maybe_signal_continuable_error_1 (type, |
2800 Fcons (obj, | |
2801 build_error_data (0, frob)), | |
1204 | 2802 class_, errb); |
428 | 2803 } |
2804 | |
2805 | |
2806 /* This is what the QUIT macro calls to signal a quit */ | |
2807 void | |
2808 signal_quit (void) | |
2809 { | |
853 | 2810 /* This function cannot GC. GC is prohibited because most callers do |
2811 not expect GC occurring in QUIT. Remove this if/when that gets fixed. | |
2812 --ben */ | |
2813 | |
2814 int count; | |
2815 | |
428 | 2816 if (EQ (Vquit_flag, Qcritical)) |
2817 debug_on_quit |= 2; /* set critical bit. */ | |
2818 Vquit_flag = Qnil; | |
853 | 2819 count = begin_gc_forbidden (); |
428 | 2820 /* note that this is continuable. */ |
2821 Fsignal (Qquit, Qnil); | |
853 | 2822 unbind_to (count); |
428 | 2823 } |
2824 | |
2825 | |
563 | 2826 /************************ convenience error functions ***********************/ |
2827 | |
436 | 2828 Lisp_Object |
428 | 2829 signal_void_function_error (Lisp_Object function) |
2830 { | |
436 | 2831 return Fsignal (Qvoid_function, list1 (function)); |
428 | 2832 } |
2833 | |
436 | 2834 Lisp_Object |
428 | 2835 signal_invalid_function_error (Lisp_Object function) |
2836 { | |
436 | 2837 return Fsignal (Qinvalid_function, list1 (function)); |
428 | 2838 } |
2839 | |
436 | 2840 Lisp_Object |
428 | 2841 signal_wrong_number_of_arguments_error (Lisp_Object function, int nargs) |
2842 { | |
436 | 2843 return Fsignal (Qwrong_number_of_arguments, |
2844 list2 (function, make_int (nargs))); | |
428 | 2845 } |
2846 | |
2847 /* Used in list traversal macros for efficiency. */ | |
436 | 2848 DOESNT_RETURN |
428 | 2849 signal_malformed_list_error (Lisp_Object list) |
2850 { | |
563 | 2851 signal_error (Qmalformed_list, 0, list); |
428 | 2852 } |
2853 | |
436 | 2854 DOESNT_RETURN |
428 | 2855 signal_malformed_property_list_error (Lisp_Object list) |
2856 { | |
563 | 2857 signal_error (Qmalformed_property_list, 0, list); |
428 | 2858 } |
2859 | |
436 | 2860 DOESNT_RETURN |
428 | 2861 signal_circular_list_error (Lisp_Object list) |
2862 { | |
563 | 2863 signal_error (Qcircular_list, 0, list); |
428 | 2864 } |
2865 | |
436 | 2866 DOESNT_RETURN |
428 | 2867 signal_circular_property_list_error (Lisp_Object list) |
2868 { | |
563 | 2869 signal_error (Qcircular_property_list, 0, list); |
428 | 2870 } |
442 | 2871 |
2267 | 2872 /* Called from within emacs_doprnt_1, so REASON is not formatted. */ |
442 | 2873 DOESNT_RETURN |
867 | 2874 syntax_error (const CIbyte *reason, Lisp_Object frob) |
442 | 2875 { |
563 | 2876 signal_error (Qsyntax_error, reason, frob); |
442 | 2877 } |
2878 | |
2879 DOESNT_RETURN | |
867 | 2880 syntax_error_2 (const CIbyte *reason, Lisp_Object frob1, Lisp_Object frob2) |
442 | 2881 { |
563 | 2882 signal_error_2 (Qsyntax_error, reason, frob1, frob2); |
2883 } | |
2884 | |
2885 void | |
867 | 2886 maybe_syntax_error (const CIbyte *reason, Lisp_Object frob, |
1204 | 2887 Lisp_Object class_, Error_Behavior errb) |
2888 { | |
2889 maybe_signal_error (Qsyntax_error, reason, frob, class_, errb); | |
563 | 2890 } |
2891 | |
2892 DOESNT_RETURN | |
867 | 2893 sferror (const CIbyte *reason, Lisp_Object frob) |
563 | 2894 { |
2895 signal_error (Qstructure_formation_error, reason, frob); | |
2896 } | |
2897 | |
2898 DOESNT_RETURN | |
867 | 2899 sferror_2 (const CIbyte *reason, Lisp_Object frob1, Lisp_Object frob2) |
563 | 2900 { |
2901 signal_error_2 (Qstructure_formation_error, reason, frob1, frob2); | |
2902 } | |
2903 | |
2904 void | |
867 | 2905 maybe_sferror (const CIbyte *reason, Lisp_Object frob, |
1204 | 2906 Lisp_Object class_, Error_Behavior errb) |
2907 { | |
2908 maybe_signal_error (Qstructure_formation_error, reason, frob, class_, errb); | |
442 | 2909 } |
2910 | |
2911 DOESNT_RETURN | |
867 | 2912 invalid_argument (const CIbyte *reason, Lisp_Object frob) |
442 | 2913 { |
563 | 2914 signal_error (Qinvalid_argument, reason, frob); |
442 | 2915 } |
2916 | |
2917 DOESNT_RETURN | |
867 | 2918 invalid_argument_2 (const CIbyte *reason, Lisp_Object frob1, |
609 | 2919 Lisp_Object frob2) |
442 | 2920 { |
563 | 2921 signal_error_2 (Qinvalid_argument, reason, frob1, frob2); |
2922 } | |
2923 | |
2924 void | |
867 | 2925 maybe_invalid_argument (const CIbyte *reason, Lisp_Object frob, |
1204 | 2926 Lisp_Object class_, Error_Behavior errb) |
2927 { | |
2928 maybe_signal_error (Qinvalid_argument, reason, frob, class_, errb); | |
563 | 2929 } |
2930 | |
2931 DOESNT_RETURN | |
867 | 2932 invalid_constant (const CIbyte *reason, Lisp_Object frob) |
563 | 2933 { |
2934 signal_error (Qinvalid_constant, reason, frob); | |
2935 } | |
2936 | |
2937 DOESNT_RETURN | |
867 | 2938 invalid_constant_2 (const CIbyte *reason, Lisp_Object frob1, |
609 | 2939 Lisp_Object frob2) |
563 | 2940 { |
2941 signal_error_2 (Qinvalid_constant, reason, frob1, frob2); | |
2942 } | |
2943 | |
2944 void | |
867 | 2945 maybe_invalid_constant (const CIbyte *reason, Lisp_Object frob, |
1204 | 2946 Lisp_Object class_, Error_Behavior errb) |
2947 { | |
2948 maybe_signal_error (Qinvalid_constant, reason, frob, class_, errb); | |
442 | 2949 } |
2950 | |
2951 DOESNT_RETURN | |
867 | 2952 invalid_operation (const CIbyte *reason, Lisp_Object frob) |
442 | 2953 { |
563 | 2954 signal_error (Qinvalid_operation, reason, frob); |
442 | 2955 } |
2956 | |
2957 DOESNT_RETURN | |
867 | 2958 invalid_operation_2 (const CIbyte *reason, Lisp_Object frob1, |
609 | 2959 Lisp_Object frob2) |
442 | 2960 { |
563 | 2961 signal_error_2 (Qinvalid_operation, reason, frob1, frob2); |
2962 } | |
2963 | |
2964 void | |
867 | 2965 maybe_invalid_operation (const CIbyte *reason, Lisp_Object frob, |
1204 | 2966 Lisp_Object class_, Error_Behavior errb) |
2967 { | |
2968 maybe_signal_error (Qinvalid_operation, reason, frob, class_, errb); | |
442 | 2969 } |
2970 | |
2971 DOESNT_RETURN | |
867 | 2972 invalid_change (const CIbyte *reason, Lisp_Object frob) |
442 | 2973 { |
563 | 2974 signal_error (Qinvalid_change, reason, frob); |
442 | 2975 } |
2976 | |
2977 DOESNT_RETURN | |
867 | 2978 invalid_change_2 (const CIbyte *reason, Lisp_Object frob1, Lisp_Object frob2) |
442 | 2979 { |
563 | 2980 signal_error_2 (Qinvalid_change, reason, frob1, frob2); |
2981 } | |
2982 | |
2983 void | |
867 | 2984 maybe_invalid_change (const CIbyte *reason, Lisp_Object frob, |
1204 | 2985 Lisp_Object class_, Error_Behavior errb) |
2986 { | |
2987 maybe_signal_error (Qinvalid_change, reason, frob, class_, errb); | |
563 | 2988 } |
2989 | |
2990 DOESNT_RETURN | |
867 | 2991 invalid_state (const CIbyte *reason, Lisp_Object frob) |
563 | 2992 { |
2993 signal_error (Qinvalid_state, reason, frob); | |
2994 } | |
2995 | |
2996 DOESNT_RETURN | |
867 | 2997 invalid_state_2 (const CIbyte *reason, Lisp_Object frob1, Lisp_Object frob2) |
563 | 2998 { |
2999 signal_error_2 (Qinvalid_state, reason, frob1, frob2); | |
3000 } | |
3001 | |
3002 void | |
867 | 3003 maybe_invalid_state (const CIbyte *reason, Lisp_Object frob, |
1204 | 3004 Lisp_Object class_, Error_Behavior errb) |
3005 { | |
3006 maybe_signal_error (Qinvalid_state, reason, frob, class_, errb); | |
563 | 3007 } |
3008 | |
3009 DOESNT_RETURN | |
867 | 3010 wtaerror (const CIbyte *reason, Lisp_Object frob) |
563 | 3011 { |
3012 signal_error (Qwrong_type_argument, reason, frob); | |
3013 } | |
3014 | |
3015 DOESNT_RETURN | |
867 | 3016 stack_overflow (const CIbyte *reason, Lisp_Object frob) |
563 | 3017 { |
3018 signal_error (Qstack_overflow, reason, frob); | |
3019 } | |
3020 | |
3021 DOESNT_RETURN | |
867 | 3022 out_of_memory (const CIbyte *reason, Lisp_Object frob) |
563 | 3023 { |
3024 signal_error (Qout_of_memory, reason, frob); | |
3025 } | |
3026 | |
3027 DOESNT_RETURN | |
867 | 3028 printing_unreadable_object (const CIbyte *fmt, ...) |
563 | 3029 { |
3030 Lisp_Object obj; | |
3031 va_list args; | |
3032 | |
3033 va_start (args, fmt); | |
771 | 3034 obj = emacs_vsprintf_string (CGETTEXT (fmt), args); |
563 | 3035 va_end (args); |
3036 | |
3037 /* Fsignal GC-protects its args */ | |
3038 signal_error (Qprinting_unreadable_object, 0, obj); | |
442 | 3039 } |
3040 | |
428 | 3041 |
3042 /************************************************************************/ | |
3043 /* User commands */ | |
3044 /************************************************************************/ | |
3045 | |
3046 DEFUN ("commandp", Fcommandp, 1, 1, 0, /* | |
3047 Return t if FUNCTION makes provisions for interactive calling. | |
3048 This means it contains a description for how to read arguments to give it. | |
3049 The value is nil for an invalid function or a symbol with no function | |
3050 definition. | |
3051 | |
3052 Interactively callable functions include | |
3053 | |
3054 -- strings and vectors (treated as keyboard macros) | |
3055 -- lambda-expressions that contain a top-level call to `interactive' | |
3056 -- autoload definitions made by `autoload' with non-nil fourth argument | |
3057 (i.e. the interactive flag) | |
3058 -- compiled-function objects with a non-nil `compiled-function-interactive' | |
3059 value | |
3060 -- subrs (built-in functions) that are interactively callable | |
3061 | |
3062 Also, a symbol satisfies `commandp' if its function definition does so. | |
3063 */ | |
3064 (function)) | |
3065 { | |
3066 Lisp_Object fun = indirect_function (function, 0); | |
3067 | |
3068 if (COMPILED_FUNCTIONP (fun)) | |
3069 return XCOMPILED_FUNCTION (fun)->flags.interactivep ? Qt : Qnil; | |
3070 | |
3071 /* Lists may represent commands. */ | |
3072 if (CONSP (fun)) | |
3073 { | |
3074 Lisp_Object funcar = XCAR (fun); | |
3075 if (EQ (funcar, Qlambda)) | |
3076 return Fassq (Qinteractive, Fcdr (Fcdr (fun))); | |
3077 if (EQ (funcar, Qautoload)) | |
3078 return Fcar (Fcdr (Fcdr (Fcdr (fun)))); | |
3079 else | |
3080 return Qnil; | |
3081 } | |
3082 | |
3083 /* Emacs primitives are interactive if their DEFUN specifies an | |
3084 interactive spec. */ | |
3085 if (SUBRP (fun)) | |
3086 return XSUBR (fun)->prompt ? Qt : Qnil; | |
3087 | |
3088 /* Strings and vectors are keyboard macros. */ | |
3089 if (VECTORP (fun) || STRINGP (fun)) | |
3090 return Qt; | |
3091 | |
3092 /* Everything else (including Qunbound) is not a command. */ | |
3093 return Qnil; | |
3094 } | |
3095 | |
3096 DEFUN ("command-execute", Fcommand_execute, 1, 3, 0, /* | |
3097 Execute CMD as an editor command. | |
3098 CMD must be an object that satisfies the `commandp' predicate. | |
3099 Optional second arg RECORD-FLAG is as in `call-interactively'. | |
3100 The argument KEYS specifies the value to use instead of (this-command-keys) | |
3101 when reading the arguments. | |
3102 */ | |
444 | 3103 (cmd, record_flag, keys)) |
428 | 3104 { |
3105 /* This function can GC */ | |
3106 Lisp_Object prefixarg; | |
3107 Lisp_Object final = cmd; | |
4162 | 3108 PROFILE_DECLARE(); |
428 | 3109 struct console *con = XCONSOLE (Vselected_console); |
3110 | |
3111 prefixarg = con->prefix_arg; | |
3112 con->prefix_arg = Qnil; | |
3113 Vcurrent_prefix_arg = prefixarg; | |
3114 debug_on_next_call = 0; /* #### from FSFmacs; correct? */ | |
3115 | |
3116 if (SYMBOLP (cmd) && !NILP (Fget (cmd, Qdisabled, Qnil))) | |
733 | 3117 return run_hook (Qdisabled_command_hook); |
428 | 3118 |
3119 for (;;) | |
3120 { | |
3121 final = indirect_function (cmd, 1); | |
3122 if (CONSP (final) && EQ (Fcar (final), Qautoload)) | |
970 | 3123 { |
3124 /* do_autoload GCPROs both arguments */ | |
3125 do_autoload (final, cmd); | |
3126 } | |
428 | 3127 else |
3128 break; | |
3129 } | |
3130 | |
3131 if (CONSP (final) || SUBRP (final) || COMPILED_FUNCTIONP (final)) | |
3132 { | |
3133 backtrace.function = &Qcall_interactively; | |
3134 backtrace.args = &cmd; | |
3135 backtrace.nargs = 1; | |
3136 backtrace.evalargs = 0; | |
1292 | 3137 backtrace.pdlcount = specpdl_depth (); |
428 | 3138 backtrace.debug_on_exit = 0; |
1292 | 3139 backtrace.function_being_called = 0; |
428 | 3140 PUSH_BACKTRACE (backtrace); |
3141 | |
1292 | 3142 PROFILE_ENTER_FUNCTION (); |
444 | 3143 final = Fcall_interactively (cmd, record_flag, keys); |
1292 | 3144 PROFILE_EXIT_FUNCTION (); |
428 | 3145 |
3146 POP_BACKTRACE (backtrace); | |
3147 return final; | |
3148 } | |
3149 else if (STRINGP (final) || VECTORP (final)) | |
3150 { | |
3151 return Fexecute_kbd_macro (final, prefixarg); | |
3152 } | |
3153 else | |
3154 { | |
3155 Fsignal (Qwrong_type_argument, | |
3156 Fcons (Qcommandp, | |
3157 (EQ (cmd, final) | |
3158 ? list1 (cmd) | |
3159 : list2 (cmd, final)))); | |
3160 return Qnil; | |
3161 } | |
3162 } | |
3163 | |
3164 DEFUN ("interactive-p", Finteractive_p, 0, 0, 0, /* | |
3165 Return t if function in which this appears was called interactively. | |
3166 This means that the function was called with call-interactively (which | |
3167 includes being called as the binding of a key) | |
3168 and input is currently coming from the keyboard (not in keyboard macro). | |
3169 */ | |
3170 ()) | |
3171 { | |
3172 REGISTER struct backtrace *btp; | |
3173 REGISTER Lisp_Object fun; | |
3174 | |
3175 if (!INTERACTIVE) | |
3176 return Qnil; | |
3177 | |
3178 /* Unless the object was compiled, skip the frame of interactive-p itself | |
3179 (if interpreted) or the frame of byte-code (if called from a compiled | |
3180 function). Note that *btp->function may be a symbol pointing at a | |
3181 compiled function. */ | |
3182 btp = backtrace_list; | |
3183 | |
3184 #if 0 /* FSFmacs */ | |
3185 | |
3186 /* #### FSFmacs does the following instead. I can't figure | |
3187 out which one is more correct. */ | |
3188 /* If this isn't a byte-compiled function, there may be a frame at | |
3189 the top for Finteractive_p itself. If so, skip it. */ | |
3190 fun = Findirect_function (*btp->function); | |
3191 if (SUBRP (fun) && XSUBR (fun) == &Sinteractive_p) | |
3192 btp = btp->next; | |
3193 | |
3194 /* If we're running an Emacs 18-style byte-compiled function, there | |
3195 may be a frame for Fbyte_code. Now, given the strictest | |
3196 definition, this function isn't really being called | |
3197 interactively, but because that's the way Emacs 18 always builds | |
3198 byte-compiled functions, we'll accept it for now. */ | |
3199 if (EQ (*btp->function, Qbyte_code)) | |
3200 btp = btp->next; | |
3201 | |
3202 /* If this isn't a byte-compiled function, then we may now be | |
3203 looking at several frames for special forms. Skip past them. */ | |
3204 while (btp && | |
3205 btp->nargs == UNEVALLED) | |
3206 btp = btp->next; | |
3207 | |
3208 #else | |
3209 | |
3210 if (! (COMPILED_FUNCTIONP (Findirect_function (*btp->function)))) | |
3211 btp = btp->next; | |
3212 for (; | |
3213 btp && (btp->nargs == UNEVALLED | |
3214 || EQ (*btp->function, Qbyte_code)); | |
3215 btp = btp->next) | |
3216 {} | |
3217 /* btp now points at the frame of the innermost function | |
3218 that DOES eval its args. | |
3219 If it is a built-in function (such as load or eval-region) | |
3220 return nil. */ | |
3221 /* Beats me why this is necessary, but it is */ | |
3222 if (btp && EQ (*btp->function, Qcall_interactively)) | |
3223 return Qt; | |
3224 | |
3225 #endif | |
3226 | |
3227 fun = Findirect_function (*btp->function); | |
3228 if (SUBRP (fun)) | |
3229 return Qnil; | |
3230 /* btp points to the frame of a Lisp function that called interactive-p. | |
3231 Return t if that function was called interactively. */ | |
3232 if (btp && btp->next && EQ (*btp->next->function, Qcall_interactively)) | |
3233 return Qt; | |
3234 return Qnil; | |
3235 } | |
3236 | |
3237 | |
3238 /************************************************************************/ | |
3239 /* Autoloading */ | |
3240 /************************************************************************/ | |
3241 | |
3242 DEFUN ("autoload", Fautoload, 2, 5, 0, /* | |
444 | 3243 Define FUNCTION to autoload from FILENAME. |
3244 FUNCTION is a symbol; FILENAME is a file name string to pass to `load'. | |
3245 The remaining optional arguments provide additional info about the | |
3246 real definition. | |
3247 DOCSTRING is documentation for FUNCTION. | |
3248 INTERACTIVE, if non-nil, says FUNCTION can be called interactively. | |
3249 TYPE indicates the type of the object: | |
428 | 3250 nil or omitted says FUNCTION is a function, |
3251 `keymap' says FUNCTION is really a keymap, and | |
3252 `macro' or t says FUNCTION is really a macro. | |
444 | 3253 If FUNCTION already has a non-void function definition that is not an |
3254 autoload object, this function does nothing and returns nil. | |
428 | 3255 */ |
444 | 3256 (function, filename, docstring, interactive, type)) |
428 | 3257 { |
3258 /* This function can GC */ | |
3259 CHECK_SYMBOL (function); | |
444 | 3260 CHECK_STRING (filename); |
428 | 3261 |
3262 /* If function is defined and not as an autoload, don't override */ | |
3263 { | |
3264 Lisp_Object f = XSYMBOL (function)->function; | |
3265 if (!UNBOUNDP (f) && !(CONSP (f) && EQ (XCAR (f), Qautoload))) | |
3266 return Qnil; | |
3267 } | |
3268 | |
3269 if (purify_flag) | |
3270 { | |
3271 /* Attempt to avoid consing identical (string=) pure strings. */ | |
444 | 3272 filename = Fsymbol_name (Fintern (filename, Qnil)); |
428 | 3273 } |
440 | 3274 |
444 | 3275 return Ffset (function, Fcons (Qautoload, list4 (filename, |
428 | 3276 docstring, |
3277 interactive, | |
3278 type))); | |
3279 } | |
3280 | |
3281 Lisp_Object | |
3282 un_autoload (Lisp_Object oldqueue) | |
3283 { | |
3284 /* This function can GC */ | |
3285 REGISTER Lisp_Object queue, first, second; | |
3286 | |
3287 /* Queue to unwind is current value of Vautoload_queue. | |
3288 oldqueue is the shadowed value to leave in Vautoload_queue. */ | |
3289 queue = Vautoload_queue; | |
3290 Vautoload_queue = oldqueue; | |
3291 while (CONSP (queue)) | |
3292 { | |
3293 first = XCAR (queue); | |
3294 second = Fcdr (first); | |
3295 first = Fcar (first); | |
3296 if (NILP (second)) | |
3297 Vfeatures = first; | |
3298 else | |
3299 Ffset (first, second); | |
3300 queue = Fcdr (queue); | |
3301 } | |
3302 return Qnil; | |
3303 } | |
3304 | |
970 | 3305 /* do_autoload GCPROs both arguments */ |
428 | 3306 void |
3307 do_autoload (Lisp_Object fundef, | |
3308 Lisp_Object funname) | |
3309 { | |
3310 /* This function can GC */ | |
3311 int speccount = specpdl_depth(); | |
3312 Lisp_Object fun = funname; | |
970 | 3313 struct gcpro gcpro1, gcpro2, gcpro3; |
428 | 3314 |
3315 CHECK_SYMBOL (funname); | |
970 | 3316 GCPRO3 (fundef, funname, fun); |
428 | 3317 |
3318 /* Value saved here is to be restored into Vautoload_queue */ | |
3319 record_unwind_protect (un_autoload, Vautoload_queue); | |
3320 Vautoload_queue = Qt; | |
3321 call4 (Qload, Fcar (Fcdr (fundef)), Qnil, noninteractive ? Qt : Qnil, Qnil); | |
3322 | |
3323 { | |
3324 Lisp_Object queue; | |
3325 | |
3326 /* Save the old autoloads, in case we ever do an unload. */ | |
3327 for (queue = Vautoload_queue; CONSP (queue); queue = XCDR (queue)) | |
3328 { | |
3329 Lisp_Object first = XCAR (queue); | |
3330 Lisp_Object second = Fcdr (first); | |
3331 | |
3332 first = Fcar (first); | |
3333 | |
3334 /* Note: This test is subtle. The cdr of an autoload-queue entry | |
3335 may be an atom if the autoload entry was generated by a defalias | |
3336 or fset. */ | |
3337 if (CONSP (second)) | |
3338 Fput (first, Qautoload, (XCDR (second))); | |
3339 } | |
3340 } | |
3341 | |
3342 /* Once loading finishes, don't undo it. */ | |
3343 Vautoload_queue = Qt; | |
771 | 3344 unbind_to (speccount); |
428 | 3345 |
3346 fun = indirect_function (fun, 0); | |
3347 | |
3348 #if 0 /* FSFmacs */ | |
3349 if (!NILP (Fequal (fun, fundef))) | |
3350 #else | |
3351 if (UNBOUNDP (fun) | |
3352 || (CONSP (fun) | |
3353 && EQ (XCAR (fun), Qautoload))) | |
3354 #endif | |
563 | 3355 invalid_state ("Autoloading failed to define function", funname); |
428 | 3356 UNGCPRO; |
3357 } | |
3358 | |
3359 | |
3360 /************************************************************************/ | |
3361 /* eval, funcall, apply */ | |
3362 /************************************************************************/ | |
3363 | |
814 | 3364 /* NOTE: If you are hearing the endless complaint that function calls in |
3365 elisp are extremely slow, it just isn't true any more! The stuff below | |
3366 -- in particular, the calling of subrs and compiled functions, the most | |
3367 common cases -- has been highly optimized. There isn't a whole lot left | |
3368 to do to squeeze more speed out except by switching to lexical | |
3369 variables, which would eliminate the specbind loop. (But the real gain | |
3370 from lexical variables would come from better optimization -- with | |
3371 dynamic binding, you have the constant problem that any function call | |
3372 that you haven't explicitly proven to be side-effect-free might | |
3373 potentially side effect your local variables, which makes optimization | |
3374 extremely difficult when there are function calls anywhere in a chunk of | |
3375 code to be optimized. Even worse, you don't know that *your* local | |
3376 variables aren't side-effecting an outer function's local variables, so | |
3377 it's impossible to optimize away almost *any* variable assignment.) */ | |
3378 | |
428 | 3379 static Lisp_Object funcall_lambda (Lisp_Object fun, |
442 | 3380 int nargs, Lisp_Object args[]); |
428 | 3381 static int in_warnings; |
3382 | |
3383 | |
814 | 3384 void handle_compiled_function_with_and_rest (Lisp_Compiled_Function *f, |
3385 int nargs, | |
3386 Lisp_Object args[]); | |
3387 | |
3388 /* The theory behind making this a separate function is to shrink | |
3389 funcall_compiled_function() so as to increase the likelihood of a cache | |
3390 hit in the L1 cache -- &rest processing is not going to be fast anyway. | |
3391 The idea is the same as with execute_rare_opcode() in bytecode.c. We | |
3392 make this non-static to ensure the compiler doesn't inline it. */ | |
3393 | |
3394 void | |
3395 handle_compiled_function_with_and_rest (Lisp_Compiled_Function *f, int nargs, | |
3396 Lisp_Object args[]) | |
3397 { | |
3398 REGISTER int i = 0; | |
3399 int max_non_rest_args = f->args_in_array - 1; | |
3400 int bindargs = min (nargs, max_non_rest_args); | |
3401 | |
3402 for (i = 0; i < bindargs; i++) | |
3092 | 3403 #ifdef NEW_GC |
3404 SPECBIND_FAST_UNSAFE (XCOMPILED_FUNCTION_ARGS_DATA (f->arguments)[i], | |
3405 args[i]); | |
3406 #else /* not NEW_GC */ | |
814 | 3407 SPECBIND_FAST_UNSAFE (f->args[i], args[i]); |
3092 | 3408 #endif /* not NEW_GC */ |
814 | 3409 for (i = bindargs; i < max_non_rest_args; i++) |
3092 | 3410 #ifdef NEW_GC |
3411 SPECBIND_FAST_UNSAFE (XCOMPILED_FUNCTION_ARGS_DATA (f->arguments)[i], | |
3412 Qnil); | |
3413 #else /* not NEW_GC */ | |
814 | 3414 SPECBIND_FAST_UNSAFE (f->args[i], Qnil); |
3092 | 3415 #endif /* not NEW_GC */ |
3416 #ifdef NEW_GC | |
3417 SPECBIND_FAST_UNSAFE | |
3418 (XCOMPILED_FUNCTION_ARGS_DATA (f->arguments)[max_non_rest_args], | |
3419 nargs > max_non_rest_args ? | |
3420 Flist (nargs - max_non_rest_args, &args[max_non_rest_args]) : | |
3421 Qnil); | |
3422 #else /* not NEW_GC */ | |
814 | 3423 SPECBIND_FAST_UNSAFE |
3424 (f->args[max_non_rest_args], | |
3425 nargs > max_non_rest_args ? | |
3426 Flist (nargs - max_non_rest_args, &args[max_non_rest_args]) : | |
3427 Qnil); | |
3092 | 3428 #endif /* not NEW_GC */ |
814 | 3429 } |
3430 | |
3431 /* Apply compiled-function object FUN to the NARGS evaluated arguments | |
3432 in ARGS, and return the result of evaluation. */ | |
3433 inline static Lisp_Object | |
3434 funcall_compiled_function (Lisp_Object fun, int nargs, Lisp_Object args[]) | |
3435 { | |
3436 /* This function can GC */ | |
3437 int speccount = specpdl_depth(); | |
3438 REGISTER int i = 0; | |
3439 Lisp_Compiled_Function *f = XCOMPILED_FUNCTION (fun); | |
3440 | |
3441 if (!OPAQUEP (f->instructions)) | |
3442 /* Lazily munge the instructions into a more efficient form */ | |
3443 optimize_compiled_function (fun); | |
3444 | |
3445 /* optimize_compiled_function() guaranteed that f->specpdl_depth is | |
3446 the required space on the specbinding stack for binding the args | |
3447 and local variables of fun. So just reserve it once. */ | |
3448 SPECPDL_RESERVE (f->specpdl_depth); | |
3449 | |
3450 if (nargs == f->max_args) /* Optimize for the common case -- no unspecified | |
3451 optional arguments. */ | |
3452 { | |
3453 #if 1 | |
3454 for (i = 0; i < nargs; i++) | |
3092 | 3455 #ifdef NEW_GC |
3456 SPECBIND_FAST_UNSAFE (XCOMPILED_FUNCTION_ARGS_DATA (f->arguments)[i], | |
3457 args[i]); | |
3458 #else /* not NEW_GC */ | |
814 | 3459 SPECBIND_FAST_UNSAFE (f->args[i], args[i]); |
3092 | 3460 #endif /* not NEW_GC */ |
814 | 3461 #else |
3462 /* Here's an alternate way to write the loop that tries to further | |
3463 optimize funcalls for functions with few arguments by partially | |
3464 unrolling the loop. It's not clear whether this is a win since it | |
3465 increases the size of the function and the possibility of L1 cache | |
3466 misses. (Microsoft VC++ 6 with /O2 /G5 generates 0x90 == 144 bytes | |
3467 per SPECBIND_FAST_UNSAFE().) Tests under VC++ 6, running the byte | |
3468 compiler repeatedly and looking at the total time, show very | |
3469 little difference between the simple loop above, the unrolled code | |
3470 below, and a "partly unrolled" solution with only cases 0-2 below | |
3471 instead of 0-4. Therefore, I'm keeping it at the simple loop | |
3472 because it's smaller. */ | |
3473 switch (nargs) | |
3474 { | |
3475 default: | |
3476 for (i = nargs - 1; i >= 4; i--) | |
3477 SPECBIND_FAST_UNSAFE (f->args[i], args[i]); | |
3478 case 4: SPECBIND_FAST_UNSAFE (f->args[3], args[3]); | |
3479 case 3: SPECBIND_FAST_UNSAFE (f->args[2], args[2]); | |
3480 case 2: SPECBIND_FAST_UNSAFE (f->args[1], args[1]); | |
3481 case 1: SPECBIND_FAST_UNSAFE (f->args[0], args[0]); | |
3482 case 0: break; | |
3483 } | |
3484 #endif | |
3485 } | |
3486 else if (nargs < f->min_args) | |
3487 goto wrong_number_of_arguments; | |
3488 else if (nargs < f->max_args) | |
3489 { | |
3490 for (i = 0; i < nargs; i++) | |
3092 | 3491 #ifdef NEW_GC |
3492 SPECBIND_FAST_UNSAFE (XCOMPILED_FUNCTION_ARGS_DATA (f->arguments)[i], | |
3493 args[i]); | |
3494 #else /* not NEW_GC */ | |
814 | 3495 SPECBIND_FAST_UNSAFE (f->args[i], args[i]); |
3092 | 3496 #endif /* not NEW_GC */ |
814 | 3497 for (i = nargs; i < f->max_args; i++) |
3092 | 3498 #ifdef NEW_GC |
3499 SPECBIND_FAST_UNSAFE (XCOMPILED_FUNCTION_ARGS_DATA (f->arguments)[i], | |
3500 Qnil); | |
3501 #else /* not NEW_GC */ | |
814 | 3502 SPECBIND_FAST_UNSAFE (f->args[i], Qnil); |
3092 | 3503 #endif /* not NEW_GC */ |
814 | 3504 } |
3505 else if (f->max_args == MANY) | |
3506 handle_compiled_function_with_and_rest (f, nargs, args); | |
3507 else | |
3508 { | |
3509 wrong_number_of_arguments: | |
3510 /* The actual printed compiled_function object is incomprehensible. | |
3511 Check the backtrace to see if we can get a more meaningful symbol. */ | |
3512 if (EQ (fun, indirect_function (*backtrace_list->function, 0))) | |
3513 fun = *backtrace_list->function; | |
3514 return Fsignal (Qwrong_number_of_arguments, | |
3515 list2 (fun, make_int (nargs))); | |
3516 } | |
3517 | |
3518 { | |
3519 Lisp_Object value = | |
3520 execute_optimized_program ((Opbyte *) XOPAQUE_DATA (f->instructions), | |
3521 f->stack_depth, | |
3522 XVECTOR_DATA (f->constants)); | |
3523 | |
3524 /* The attempt to optimize this by only unbinding variables failed | |
3525 because using buffer-local variables as function parameters | |
3526 leads to specpdl_ptr->func != 0 */ | |
3527 /* UNBIND_TO_GCPRO_VARIABLES_ONLY (speccount, value); */ | |
3528 UNBIND_TO_GCPRO (speccount, value); | |
3529 return value; | |
3530 } | |
3531 } | |
3532 | |
428 | 3533 DEFUN ("eval", Feval, 1, 1, 0, /* |
3534 Evaluate FORM and return its value. | |
3535 */ | |
3536 (form)) | |
3537 { | |
3538 /* This function can GC */ | |
3539 Lisp_Object fun, val, original_fun, original_args; | |
3540 int nargs; | |
4162 | 3541 PROFILE_DECLARE(); |
428 | 3542 |
1318 | 3543 #ifdef ERROR_CHECK_TRAPPING_PROBLEMS |
3544 check_proper_critical_section_lisp_protection (); | |
3545 #endif | |
3546 | |
3989 | 3547 if (!CONSP (form)) |
3548 { | |
3549 if (SYMBOLP (form)) | |
3550 { | |
3551 return Fsymbol_value (form); | |
3552 } | |
3553 | |
3554 return form; | |
3555 } | |
3556 | |
428 | 3557 /* I think this is a pretty safe place to call Lisp code, don't you? */ |
853 | 3558 while (!in_warnings && !NILP (Vpending_warnings) |
3559 /* well, perhaps not so safe after all! */ | |
3560 && !(inhibit_flags & INHIBIT_ANY_CHANGE_AFFECTING_REDISPLAY)) | |
428 | 3561 { |
3562 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; | |
1204 | 3563 Lisp_Object this_warning_cons, this_warning, class_, level, messij; |
853 | 3564 int speccount = internal_bind_int (&in_warnings, 1); |
3565 | |
428 | 3566 this_warning_cons = Vpending_warnings; |
3567 this_warning = XCAR (this_warning_cons); | |
3568 /* in case an error occurs in the warn function, at least | |
3569 it won't happen infinitely */ | |
3570 Vpending_warnings = XCDR (Vpending_warnings); | |
853 | 3571 free_cons (this_warning_cons); |
1204 | 3572 class_ = XCAR (this_warning); |
428 | 3573 level = XCAR (XCDR (this_warning)); |
3574 messij = XCAR (XCDR (XCDR (this_warning))); | |
3575 free_list (this_warning); | |
3576 | |
3577 if (NILP (Vpending_warnings)) | |
3578 Vpending_warnings_tail = Qnil; /* perhaps not strictly necessary, | |
3579 but safer */ | |
3580 | |
1204 | 3581 GCPRO4 (form, class_, level, messij); |
428 | 3582 if (!STRINGP (messij)) |
3583 messij = Fprin1_to_string (messij, Qnil); | |
1204 | 3584 call3 (Qdisplay_warning, class_, messij, level); |
428 | 3585 UNGCPRO; |
771 | 3586 unbind_to (speccount); |
428 | 3587 } |
3588 | |
3589 QUIT; | |
814 | 3590 if (need_to_garbage_collect) |
428 | 3591 { |
3592 struct gcpro gcpro1; | |
3593 GCPRO1 (form); | |
3092 | 3594 #ifdef NEW_GC |
3595 gc_incremental (); | |
3596 #else /* not NEW_GC */ | |
428 | 3597 garbage_collect_1 (); |
3092 | 3598 #endif /* not NEW_GC */ |
428 | 3599 UNGCPRO; |
3600 } | |
3601 | |
3602 if (++lisp_eval_depth > max_lisp_eval_depth) | |
3603 { | |
3604 if (max_lisp_eval_depth < 100) | |
3605 max_lisp_eval_depth = 100; | |
3606 if (lisp_eval_depth > max_lisp_eval_depth) | |
563 | 3607 stack_overflow ("Lisp nesting exceeds `max-lisp-eval-depth'", |
3608 Qunbound); | |
428 | 3609 } |
3610 | |
3611 /* We guaranteed CONSP (form) above */ | |
3612 original_fun = XCAR (form); | |
3613 original_args = XCDR (form); | |
3614 | |
3615 GET_EXTERNAL_LIST_LENGTH (original_args, nargs); | |
3616 | |
3617 backtrace.pdlcount = specpdl_depth(); | |
3618 backtrace.function = &original_fun; /* This also protects them from gc */ | |
3619 backtrace.args = &original_args; | |
3620 backtrace.nargs = UNEVALLED; | |
3621 backtrace.evalargs = 1; | |
3622 backtrace.debug_on_exit = 0; | |
1292 | 3623 backtrace.function_being_called = 0; |
428 | 3624 PUSH_BACKTRACE (backtrace); |
3625 | |
3626 if (debug_on_next_call) | |
3627 do_debug_on_call (Qt); | |
3628 | |
3629 /* At this point, only original_fun and original_args | |
3630 have values that will be used below. */ | |
3631 retry: | |
3989 | 3632 /* Optimise for no indirection. */ |
3633 fun = original_fun; | |
3634 if (SYMBOLP (fun) && !EQ (fun, Qunbound) | |
3635 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun))) | |
3636 { | |
3637 fun = indirect_function(original_fun, 1); | |
3638 } | |
428 | 3639 |
3640 if (SUBRP (fun)) | |
3641 { | |
3642 Lisp_Subr *subr = XSUBR (fun); | |
3643 int max_args = subr->max_args; | |
3644 | |
3645 if (nargs < subr->min_args) | |
3646 goto wrong_number_of_arguments; | |
3647 | |
3648 if (max_args == UNEVALLED) /* Optimize for the common case */ | |
3649 { | |
3650 backtrace.evalargs = 0; | |
1292 | 3651 PROFILE_ENTER_FUNCTION (); |
428 | 3652 val = (((Lisp_Object (*) (Lisp_Object)) subr_function (subr)) |
3653 (original_args)); | |
1292 | 3654 PROFILE_EXIT_FUNCTION (); |
428 | 3655 } |
3656 else if (nargs <= max_args) | |
3657 { | |
3658 struct gcpro gcpro1; | |
3659 Lisp_Object args[SUBR_MAX_ARGS]; | |
3660 REGISTER Lisp_Object *p = args; | |
3661 | |
3662 GCPRO1 (args[0]); | |
3663 gcpro1.nvars = 0; | |
3664 | |
3665 { | |
3666 LIST_LOOP_2 (arg, original_args) | |
3667 { | |
3668 *p++ = Feval (arg); | |
3669 gcpro1.nvars++; | |
3670 } | |
3671 } | |
3672 | |
3673 /* &optional args default to nil. */ | |
3674 while (p - args < max_args) | |
3675 *p++ = Qnil; | |
3676 | |
3677 backtrace.args = args; | |
3678 backtrace.nargs = nargs; | |
3679 | |
1292 | 3680 PROFILE_ENTER_FUNCTION (); |
428 | 3681 FUNCALL_SUBR (val, subr, args, max_args); |
1292 | 3682 PROFILE_EXIT_FUNCTION (); |
428 | 3683 |
3684 UNGCPRO; | |
3685 } | |
3686 else if (max_args == MANY) | |
3687 { | |
3688 /* Pass a vector of evaluated arguments */ | |
3689 struct gcpro gcpro1; | |
3690 Lisp_Object *args = alloca_array (Lisp_Object, nargs); | |
3691 REGISTER Lisp_Object *p = args; | |
3692 | |
3693 GCPRO1 (args[0]); | |
3694 gcpro1.nvars = 0; | |
3695 | |
3696 { | |
3697 LIST_LOOP_2 (arg, original_args) | |
3698 { | |
3699 *p++ = Feval (arg); | |
3700 gcpro1.nvars++; | |
3701 } | |
3702 } | |
3703 | |
3704 backtrace.args = args; | |
3705 backtrace.nargs = nargs; | |
3706 | |
1292 | 3707 PROFILE_ENTER_FUNCTION (); |
428 | 3708 val = (((Lisp_Object (*) (int, Lisp_Object *)) subr_function (subr)) |
3709 (nargs, args)); | |
1292 | 3710 PROFILE_EXIT_FUNCTION (); |
428 | 3711 |
3712 UNGCPRO; | |
3713 } | |
3714 else | |
3715 { | |
3716 wrong_number_of_arguments: | |
440 | 3717 val = signal_wrong_number_of_arguments_error (original_fun, nargs); |
428 | 3718 } |
3719 } | |
3720 else if (COMPILED_FUNCTIONP (fun)) | |
3721 { | |
3722 struct gcpro gcpro1; | |
3723 Lisp_Object *args = alloca_array (Lisp_Object, nargs); | |
3724 REGISTER Lisp_Object *p = args; | |
3725 | |
3726 GCPRO1 (args[0]); | |
3727 gcpro1.nvars = 0; | |
3728 | |
3729 { | |
3730 LIST_LOOP_2 (arg, original_args) | |
3731 { | |
3732 *p++ = Feval (arg); | |
3733 gcpro1.nvars++; | |
3734 } | |
3735 } | |
3736 | |
3737 backtrace.args = args; | |
3738 backtrace.nargs = nargs; | |
3739 backtrace.evalargs = 0; | |
3740 | |
1292 | 3741 PROFILE_ENTER_FUNCTION (); |
428 | 3742 val = funcall_compiled_function (fun, nargs, args); |
1292 | 3743 PROFILE_EXIT_FUNCTION (); |
428 | 3744 |
3745 /* Do the debug-on-exit now, while args is still GCPROed. */ | |
3746 if (backtrace.debug_on_exit) | |
3747 val = do_debug_on_exit (val); | |
3748 /* Don't do it again when we return to eval. */ | |
3749 backtrace.debug_on_exit = 0; | |
3750 | |
3751 UNGCPRO; | |
3752 } | |
3753 else if (CONSP (fun)) | |
3754 { | |
3755 Lisp_Object funcar = XCAR (fun); | |
3756 | |
3757 if (EQ (funcar, Qautoload)) | |
3758 { | |
970 | 3759 /* do_autoload GCPROs both arguments */ |
428 | 3760 do_autoload (fun, original_fun); |
3761 goto retry; | |
3762 } | |
3763 else if (EQ (funcar, Qmacro)) | |
3764 { | |
1292 | 3765 PROFILE_ENTER_FUNCTION (); |
428 | 3766 val = Feval (apply1 (XCDR (fun), original_args)); |
1292 | 3767 PROFILE_EXIT_FUNCTION (); |
428 | 3768 } |
3769 else if (EQ (funcar, Qlambda)) | |
3770 { | |
3771 struct gcpro gcpro1; | |
3772 Lisp_Object *args = alloca_array (Lisp_Object, nargs); | |
3773 REGISTER Lisp_Object *p = args; | |
3774 | |
3775 GCPRO1 (args[0]); | |
3776 gcpro1.nvars = 0; | |
3777 | |
3778 { | |
3779 LIST_LOOP_2 (arg, original_args) | |
3780 { | |
3781 *p++ = Feval (arg); | |
3782 gcpro1.nvars++; | |
3783 } | |
3784 } | |
3785 | |
3786 UNGCPRO; | |
3787 | |
3788 backtrace.args = args; /* this also GCPROs `args' */ | |
3789 backtrace.nargs = nargs; | |
3790 backtrace.evalargs = 0; | |
3791 | |
1292 | 3792 PROFILE_ENTER_FUNCTION (); |
428 | 3793 val = funcall_lambda (fun, nargs, args); |
1292 | 3794 PROFILE_EXIT_FUNCTION (); |
428 | 3795 |
3796 /* Do the debug-on-exit now, while args is still GCPROed. */ | |
3797 if (backtrace.debug_on_exit) | |
3798 val = do_debug_on_exit (val); | |
3799 /* Don't do it again when we return to eval. */ | |
3800 backtrace.debug_on_exit = 0; | |
3801 } | |
3802 else | |
3803 { | |
3804 goto invalid_function; | |
3805 } | |
3806 } | |
4104 | 3807 else if (UNBOUNDP (fun)) |
3808 { | |
3809 val = signal_void_function_error (original_fun); | |
3810 } | |
3811 else /* ! (SUBRP (fun) || COMPILED_FUNCTIONP (fun) || CONSP (fun) | |
3812 UNBOUNDP (fun)) */ | |
428 | 3813 { |
3814 invalid_function: | |
436 | 3815 val = signal_invalid_function_error (fun); |
428 | 3816 } |
3817 | |
3818 lisp_eval_depth--; | |
3819 if (backtrace.debug_on_exit) | |
3820 val = do_debug_on_exit (val); | |
3821 POP_BACKTRACE (backtrace); | |
3822 return val; | |
3823 } | |
3824 | |
3825 | |
1111 | 3826 |
3827 static void | |
3828 run_post_gc_hook (void) | |
3829 { | |
3830 Lisp_Object args[2]; | |
3831 | |
3832 args[0] = Qpost_gc_hook; | |
3833 args[1] = Fcons (Fcons (Qfinalize_list, zap_finalize_list ()), Qnil); | |
3834 | |
3835 run_hook_with_args_trapping_problems | |
1333 | 3836 (Qgarbage_collecting, 2, args, RUN_HOOKS_TO_COMPLETION, |
1111 | 3837 INHIBIT_QUIT | NO_INHIBIT_ERRORS); |
3838 } | |
3839 | |
428 | 3840 DEFUN ("funcall", Ffuncall, 1, MANY, 0, /* |
3841 Call first argument as a function, passing the remaining arguments to it. | |
3842 Thus, (funcall 'cons 'x 'y) returns (x . y). | |
3843 */ | |
3844 (int nargs, Lisp_Object *args)) | |
3845 { | |
3846 /* This function can GC */ | |
3847 Lisp_Object fun; | |
3848 Lisp_Object val; | |
4162 | 3849 PROFILE_DECLARE(); |
428 | 3850 int fun_nargs = nargs - 1; |
3851 Lisp_Object *fun_args = args + 1; | |
3852 | |
1318 | 3853 /* QUIT will check for proper redisplay wrapping */ |
3854 | |
428 | 3855 QUIT; |
851 | 3856 |
3857 if (funcall_allocation_flag) | |
3858 { | |
3859 if (need_to_garbage_collect) | |
3860 /* Callers should gcpro lexpr args */ | |
3092 | 3861 #ifdef NEW_GC |
3862 gc_incremental (); | |
3863 #else /* not NEW_GC */ | |
851 | 3864 garbage_collect_1 (); |
3092 | 3865 #endif /* not NEW_GC */ |
851 | 3866 if (need_to_check_c_alloca) |
3867 { | |
3868 if (++funcall_alloca_count >= MAX_FUNCALLS_BETWEEN_ALLOCA_CLEANUP) | |
3869 { | |
3870 xemacs_c_alloca (0); | |
3871 funcall_alloca_count = 0; | |
3872 } | |
3873 } | |
887 | 3874 if (need_to_signal_post_gc) |
3875 { | |
3876 need_to_signal_post_gc = 0; | |
1111 | 3877 recompute_funcall_allocation_flag (); |
3263 | 3878 #ifdef NEW_GC |
3879 run_finalizers (); | |
3880 #endif /* NEW_GC */ | |
1111 | 3881 run_post_gc_hook (); |
887 | 3882 } |
851 | 3883 } |
428 | 3884 |
3885 if (++lisp_eval_depth > max_lisp_eval_depth) | |
3886 { | |
3887 if (max_lisp_eval_depth < 100) | |
3888 max_lisp_eval_depth = 100; | |
3889 if (lisp_eval_depth > max_lisp_eval_depth) | |
563 | 3890 stack_overflow ("Lisp nesting exceeds `max-lisp-eval-depth'", |
3891 Qunbound); | |
428 | 3892 } |
3893 | |
1292 | 3894 backtrace.pdlcount = specpdl_depth (); |
428 | 3895 backtrace.function = &args[0]; |
3896 backtrace.args = fun_args; | |
3897 backtrace.nargs = fun_nargs; | |
3898 backtrace.evalargs = 0; | |
3899 backtrace.debug_on_exit = 0; | |
1292 | 3900 backtrace.function_being_called = 0; |
428 | 3901 PUSH_BACKTRACE (backtrace); |
3902 | |
3903 if (debug_on_next_call) | |
3904 do_debug_on_call (Qlambda); | |
3905 | |
3906 retry: | |
3907 | |
3908 fun = args[0]; | |
3909 | |
3910 /* We could call indirect_function directly, but profiling shows | |
3911 this is worth optimizing by partially unrolling the loop. */ | |
3912 if (SYMBOLP (fun)) | |
3913 { | |
3914 fun = XSYMBOL (fun)->function; | |
3915 if (SYMBOLP (fun)) | |
3916 { | |
3917 fun = XSYMBOL (fun)->function; | |
3918 if (SYMBOLP (fun)) | |
3919 fun = indirect_function (fun, 1); | |
3920 } | |
3921 } | |
3922 | |
3923 if (SUBRP (fun)) | |
3924 { | |
3925 Lisp_Subr *subr = XSUBR (fun); | |
3926 int max_args = subr->max_args; | |
3927 Lisp_Object spacious_args[SUBR_MAX_ARGS]; | |
3928 | |
3929 if (fun_nargs == max_args) /* Optimize for the common case */ | |
3930 { | |
3931 funcall_subr: | |
1292 | 3932 PROFILE_ENTER_FUNCTION (); |
428 | 3933 FUNCALL_SUBR (val, subr, fun_args, max_args); |
1292 | 3934 PROFILE_EXIT_FUNCTION (); |
428 | 3935 } |
436 | 3936 else if (fun_nargs < subr->min_args) |
3937 { | |
3938 goto wrong_number_of_arguments; | |
3939 } | |
428 | 3940 else if (fun_nargs < max_args) |
3941 { | |
3942 Lisp_Object *p = spacious_args; | |
3943 | |
3944 /* Default optionals to nil */ | |
3945 while (fun_nargs--) | |
3946 *p++ = *fun_args++; | |
3947 while (p - spacious_args < max_args) | |
3948 *p++ = Qnil; | |
3949 | |
3950 fun_args = spacious_args; | |
3951 goto funcall_subr; | |
3952 } | |
3953 else if (max_args == MANY) | |
3954 { | |
1292 | 3955 PROFILE_ENTER_FUNCTION (); |
436 | 3956 val = SUBR_FUNCTION (subr, MANY) (fun_nargs, fun_args); |
1292 | 3957 PROFILE_EXIT_FUNCTION (); |
428 | 3958 } |
3959 else if (max_args == UNEVALLED) /* Can't funcall a special form */ | |
3960 { | |
3961 goto invalid_function; | |
3962 } | |
3963 else | |
3964 { | |
3965 wrong_number_of_arguments: | |
436 | 3966 val = signal_wrong_number_of_arguments_error (fun, fun_nargs); |
428 | 3967 } |
3968 } | |
3969 else if (COMPILED_FUNCTIONP (fun)) | |
3970 { | |
1292 | 3971 PROFILE_ENTER_FUNCTION (); |
428 | 3972 val = funcall_compiled_function (fun, fun_nargs, fun_args); |
1292 | 3973 PROFILE_EXIT_FUNCTION (); |
428 | 3974 } |
3975 else if (CONSP (fun)) | |
3976 { | |
3977 Lisp_Object funcar = XCAR (fun); | |
3978 | |
3979 if (EQ (funcar, Qlambda)) | |
3980 { | |
1292 | 3981 PROFILE_ENTER_FUNCTION (); |
428 | 3982 val = funcall_lambda (fun, fun_nargs, fun_args); |
1292 | 3983 PROFILE_EXIT_FUNCTION (); |
428 | 3984 } |
3985 else if (EQ (funcar, Qautoload)) | |
3986 { | |
970 | 3987 /* do_autoload GCPROs both arguments */ |
428 | 3988 do_autoload (fun, args[0]); |
3989 goto retry; | |
3990 } | |
3991 else /* Can't funcall a macro */ | |
3992 { | |
3993 goto invalid_function; | |
3994 } | |
3995 } | |
3996 else if (UNBOUNDP (fun)) | |
3997 { | |
436 | 3998 val = signal_void_function_error (args[0]); |
428 | 3999 } |
4000 else | |
4001 { | |
4002 invalid_function: | |
436 | 4003 val = signal_invalid_function_error (fun); |
428 | 4004 } |
4005 | |
4006 lisp_eval_depth--; | |
4007 if (backtrace.debug_on_exit) | |
4008 val = do_debug_on_exit (val); | |
4009 POP_BACKTRACE (backtrace); | |
4010 return val; | |
4011 } | |
4012 | |
4013 DEFUN ("functionp", Ffunctionp, 1, 1, 0, /* | |
4014 Return t if OBJECT can be called as a function, else nil. | |
4015 A function is an object that can be applied to arguments, | |
4016 using for example `funcall' or `apply'. | |
4017 */ | |
4018 (object)) | |
4019 { | |
4020 if (SYMBOLP (object)) | |
4021 object = indirect_function (object, 0); | |
4022 | |
919 | 4023 if (COMPILED_FUNCTIONP (object) || SUBRP (object)) |
4024 return Qt; | |
4025 if (CONSP (object)) | |
4026 { | |
4027 Lisp_Object car = XCAR (object); | |
4028 if (EQ (car, Qlambda)) | |
4029 return Qt; | |
4030 if (EQ (car, Qautoload) | |
4031 && NILP (Fcar_safe (Fcdr_safe (Fcdr_safe (Fcdr_safe (XCDR (object))))))) | |
4032 return Qt; | |
4033 } | |
4034 return Qnil; | |
428 | 4035 } |
4036 | |
4037 static Lisp_Object | |
4038 function_argcount (Lisp_Object function, int function_min_args_p) | |
4039 { | |
4040 Lisp_Object orig_function = function; | |
4041 Lisp_Object arglist; | |
4042 | |
4043 retry: | |
4044 | |
4045 if (SYMBOLP (function)) | |
4046 function = indirect_function (function, 1); | |
4047 | |
4048 if (SUBRP (function)) | |
4049 { | |
442 | 4050 /* Using return with the ?: operator tickles a DEC CC compiler bug. */ |
4051 if (function_min_args_p) | |
4052 return Fsubr_min_args (function); | |
4053 else | |
4054 return Fsubr_max_args (function); | |
428 | 4055 } |
4056 else if (COMPILED_FUNCTIONP (function)) | |
4057 { | |
814 | 4058 Lisp_Compiled_Function *f = XCOMPILED_FUNCTION (function); |
4059 | |
1737 | 4060 if (!OPAQUEP (f->instructions)) |
4061 /* Lazily munge the instructions into a more efficient form */ | |
4062 /* Needed to set max_args */ | |
4063 optimize_compiled_function (function); | |
4064 | |
814 | 4065 if (function_min_args_p) |
4066 return make_int (f->min_args); | |
4067 else if (f->max_args == MANY) | |
4068 return Qnil; | |
4069 else | |
4070 return make_int (f->max_args); | |
428 | 4071 } |
4072 else if (CONSP (function)) | |
4073 { | |
4074 Lisp_Object funcar = XCAR (function); | |
4075 | |
4076 if (EQ (funcar, Qmacro)) | |
4077 { | |
4078 function = XCDR (function); | |
4079 goto retry; | |
4080 } | |
4081 else if (EQ (funcar, Qautoload)) | |
4082 { | |
970 | 4083 /* do_autoload GCPROs both arguments */ |
428 | 4084 do_autoload (function, orig_function); |
442 | 4085 function = orig_function; |
428 | 4086 goto retry; |
4087 } | |
4088 else if (EQ (funcar, Qlambda)) | |
4089 { | |
4090 arglist = Fcar (XCDR (function)); | |
4091 } | |
4092 else | |
4093 { | |
4094 goto invalid_function; | |
4095 } | |
4096 } | |
4097 else | |
4098 { | |
4099 invalid_function: | |
442 | 4100 return signal_invalid_function_error (orig_function); |
428 | 4101 } |
4102 | |
4103 { | |
4104 int argcount = 0; | |
4105 | |
4106 EXTERNAL_LIST_LOOP_2 (arg, arglist) | |
4107 { | |
4108 if (EQ (arg, Qand_optional)) | |
4109 { | |
4110 if (function_min_args_p) | |
4111 break; | |
4112 } | |
4113 else if (EQ (arg, Qand_rest)) | |
4114 { | |
4115 if (function_min_args_p) | |
4116 break; | |
4117 else | |
4118 return Qnil; | |
4119 } | |
4120 else | |
4121 { | |
4122 argcount++; | |
4123 } | |
4124 } | |
4125 | |
4126 return make_int (argcount); | |
4127 } | |
4128 } | |
4129 | |
4130 DEFUN ("function-min-args", Ffunction_min_args, 1, 1, 0, /* | |
617 | 4131 Return the minimum number of arguments a function may be called with. |
428 | 4132 The function may be any form that can be passed to `funcall', |
4133 any special form, or any macro. | |
853 | 4134 |
4135 To check if a function can be called with a specified number of | |
4136 arguments, use `function-allows-args'. | |
428 | 4137 */ |
4138 (function)) | |
4139 { | |
4140 return function_argcount (function, 1); | |
4141 } | |
4142 | |
4143 DEFUN ("function-max-args", Ffunction_max_args, 1, 1, 0, /* | |
617 | 4144 Return the maximum number of arguments a function may be called with. |
428 | 4145 The function may be any form that can be passed to `funcall', |
4146 any special form, or any macro. | |
4147 If the function takes an arbitrary number of arguments or is | |
4148 a built-in special form, nil is returned. | |
853 | 4149 |
4150 To check if a function can be called with a specified number of | |
4151 arguments, use `function-allows-args'. | |
428 | 4152 */ |
4153 (function)) | |
4154 { | |
4155 return function_argcount (function, 0); | |
4156 } | |
4157 | |
4158 | |
4159 DEFUN ("apply", Fapply, 2, MANY, 0, /* | |
4160 Call FUNCTION with the remaining args, using the last arg as a list of args. | |
4161 Thus, (apply '+ 1 2 '(3 4)) returns 10. | |
4162 */ | |
4163 (int nargs, Lisp_Object *args)) | |
4164 { | |
4165 /* This function can GC */ | |
4166 Lisp_Object fun = args[0]; | |
4167 Lisp_Object spread_arg = args [nargs - 1]; | |
4168 int numargs; | |
4169 int funcall_nargs; | |
4170 | |
4171 GET_EXTERNAL_LIST_LENGTH (spread_arg, numargs); | |
4172 | |
4173 if (numargs == 0) | |
4174 /* (apply foo 0 1 '()) */ | |
4175 return Ffuncall (nargs - 1, args); | |
4176 else if (numargs == 1) | |
4177 { | |
4178 /* (apply foo 0 1 '(2)) */ | |
4179 args [nargs - 1] = XCAR (spread_arg); | |
4180 return Ffuncall (nargs, args); | |
4181 } | |
4182 | |
4183 /* -1 for function, -1 for spread arg */ | |
4184 numargs = nargs - 2 + numargs; | |
4185 /* +1 for function */ | |
4186 funcall_nargs = 1 + numargs; | |
4187 | |
4188 if (SYMBOLP (fun)) | |
4189 fun = indirect_function (fun, 0); | |
4190 | |
4191 if (SUBRP (fun)) | |
4192 { | |
4193 Lisp_Subr *subr = XSUBR (fun); | |
4194 int max_args = subr->max_args; | |
4195 | |
4196 if (numargs < subr->min_args | |
4197 || (max_args >= 0 && max_args < numargs)) | |
4198 { | |
4199 /* Let funcall get the error */ | |
4200 } | |
4201 else if (max_args > numargs) | |
4202 { | |
4203 /* Avoid having funcall cons up yet another new vector of arguments | |
4204 by explicitly supplying nil's for optional values */ | |
4205 funcall_nargs += (max_args - numargs); | |
4206 } | |
4207 } | |
4208 else if (UNBOUNDP (fun)) | |
4209 { | |
4210 /* Let funcall get the error */ | |
4211 fun = args[0]; | |
4212 } | |
4213 | |
4214 { | |
4215 REGISTER int i; | |
4216 Lisp_Object *funcall_args = alloca_array (Lisp_Object, funcall_nargs); | |
4217 struct gcpro gcpro1; | |
4218 | |
4219 GCPRO1 (*funcall_args); | |
4220 gcpro1.nvars = funcall_nargs; | |
4221 | |
4222 /* Copy in the unspread args */ | |
4223 memcpy (funcall_args, args, (nargs - 1) * sizeof (Lisp_Object)); | |
4224 /* Spread the last arg we got. Its first element goes in | |
4225 the slot that it used to occupy, hence this value of I. */ | |
4226 for (i = nargs - 1; | |
4227 !NILP (spread_arg); /* i < 1 + numargs */ | |
4228 i++, spread_arg = XCDR (spread_arg)) | |
4229 { | |
4230 funcall_args [i] = XCAR (spread_arg); | |
4231 } | |
4232 /* Supply nil for optional args (to subrs) */ | |
4233 for (; i < funcall_nargs; i++) | |
4234 funcall_args[i] = Qnil; | |
4235 | |
4236 | |
4237 RETURN_UNGCPRO (Ffuncall (funcall_nargs, funcall_args)); | |
4238 } | |
4239 } | |
4240 | |
4241 | |
4242 /* Apply lambda list FUN to the NARGS evaluated arguments in ARGS and | |
4243 return the result of evaluation. */ | |
4244 | |
4245 static Lisp_Object | |
4246 funcall_lambda (Lisp_Object fun, int nargs, Lisp_Object args[]) | |
4247 { | |
4248 /* This function can GC */ | |
442 | 4249 Lisp_Object arglist, body, tail; |
428 | 4250 int speccount = specpdl_depth(); |
4251 REGISTER int i = 0; | |
4252 | |
4253 tail = XCDR (fun); | |
4254 | |
4255 if (!CONSP (tail)) | |
4256 goto invalid_function; | |
4257 | |
4258 arglist = XCAR (tail); | |
4259 body = XCDR (tail); | |
4260 | |
4261 { | |
4262 int optional = 0, rest = 0; | |
4263 | |
442 | 4264 EXTERNAL_LIST_LOOP_2 (symbol, arglist) |
428 | 4265 { |
4266 if (!SYMBOLP (symbol)) | |
4267 goto invalid_function; | |
4268 if (EQ (symbol, Qand_rest)) | |
4269 rest = 1; | |
4270 else if (EQ (symbol, Qand_optional)) | |
4271 optional = 1; | |
4272 else if (rest) | |
4273 { | |
4274 specbind (symbol, Flist (nargs - i, &args[i])); | |
4275 i = nargs; | |
4276 } | |
4277 else if (i < nargs) | |
4278 specbind (symbol, args[i++]); | |
4279 else if (!optional) | |
4280 goto wrong_number_of_arguments; | |
4281 else | |
4282 specbind (symbol, Qnil); | |
4283 } | |
4284 } | |
4285 | |
4286 if (i < nargs) | |
4287 goto wrong_number_of_arguments; | |
4288 | |
771 | 4289 return unbind_to_1 (speccount, Fprogn (body)); |
428 | 4290 |
4291 wrong_number_of_arguments: | |
436 | 4292 return signal_wrong_number_of_arguments_error (fun, nargs); |
428 | 4293 |
4294 invalid_function: | |
436 | 4295 return signal_invalid_function_error (fun); |
428 | 4296 } |
4297 | |
4298 | |
4299 /************************************************************************/ | |
4300 /* Run hook variables in various ways. */ | |
4301 /************************************************************************/ | |
4302 | |
4303 DEFUN ("run-hooks", Frun_hooks, 1, MANY, 0, /* | |
4304 Run each hook in HOOKS. Major mode functions use this. | |
4305 Each argument should be a symbol, a hook variable. | |
4306 These symbols are processed in the order specified. | |
4307 If a hook symbol has a non-nil value, that value may be a function | |
4308 or a list of functions to be called to run the hook. | |
4309 If the value is a function, it is called with no arguments. | |
4310 If it is a list, the elements are called, in order, with no arguments. | |
4311 | |
4312 To make a hook variable buffer-local, use `make-local-hook', | |
4313 not `make-local-variable'. | |
4642
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
4314 |
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
4315 arguments: (&rest HOOKS) |
428 | 4316 */ |
4317 (int nargs, Lisp_Object *args)) | |
4318 { | |
4319 REGISTER int i; | |
4320 | |
4321 for (i = 0; i < nargs; i++) | |
4322 run_hook_with_args (1, args + i, RUN_HOOKS_TO_COMPLETION); | |
4323 | |
4324 return Qnil; | |
4325 } | |
4326 | |
4327 DEFUN ("run-hook-with-args", Frun_hook_with_args, 1, MANY, 0, /* | |
4328 Run HOOK with the specified arguments ARGS. | |
4329 HOOK should be a symbol, a hook variable. If HOOK has a non-nil | |
4330 value, that value may be a function or a list of functions to be | |
4331 called to run the hook. If the value is a function, it is called with | |
4332 the given arguments and its return value is returned. If it is a list | |
4333 of functions, those functions are called, in order, | |
4334 with the given arguments ARGS. | |
444 | 4335 It is best not to depend on the value returned by `run-hook-with-args', |
428 | 4336 as that may change. |
4337 | |
4338 To make a hook variable buffer-local, use `make-local-hook', | |
4339 not `make-local-variable'. | |
4642
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
4340 |
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
4341 arguments: (HOOK &rest ARGS) |
428 | 4342 */ |
4343 (int nargs, Lisp_Object *args)) | |
4344 { | |
4345 return run_hook_with_args (nargs, args, RUN_HOOKS_TO_COMPLETION); | |
4346 } | |
4347 | |
4348 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success, 1, MANY, 0, /* | |
4349 Run HOOK with the specified arguments ARGS. | |
4350 HOOK should be a symbol, a hook variable. Its value should | |
4351 be a list of functions. We call those functions, one by one, | |
4352 passing arguments ARGS to each of them, until one of them | |
4353 returns a non-nil value. Then we return that value. | |
4354 If all the functions return nil, we return nil. | |
4355 | |
4356 To make a hook variable buffer-local, use `make-local-hook', | |
4357 not `make-local-variable'. | |
4642
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
4358 |
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
4359 arguments: (HOOK &rest ARGS) |
428 | 4360 */ |
4361 (int nargs, Lisp_Object *args)) | |
4362 { | |
4363 return run_hook_with_args (nargs, args, RUN_HOOKS_UNTIL_SUCCESS); | |
4364 } | |
4365 | |
4366 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure, 1, MANY, 0, /* | |
4367 Run HOOK with the specified arguments ARGS. | |
4368 HOOK should be a symbol, a hook variable. Its value should | |
4369 be a list of functions. We call those functions, one by one, | |
4370 passing arguments ARGS to each of them, until one of them | |
4371 returns nil. Then we return nil. | |
4372 If all the functions return non-nil, we return non-nil. | |
4373 | |
4374 To make a hook variable buffer-local, use `make-local-hook', | |
4375 not `make-local-variable'. | |
4642
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
4376 |
48b45a606961
Support #'function-arglist with built-in special forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4624
diff
changeset
|
4377 arguments: (HOOK &rest ARGS) |
428 | 4378 */ |
4379 (int nargs, Lisp_Object *args)) | |
4380 { | |
4381 return run_hook_with_args (nargs, args, RUN_HOOKS_UNTIL_FAILURE); | |
4382 } | |
4383 | |
4384 /* ARGS[0] should be a hook symbol. | |
4385 Call each of the functions in the hook value, passing each of them | |
4386 as arguments all the rest of ARGS (all NARGS - 1 elements). | |
4387 COND specifies a condition to test after each call | |
4388 to decide whether to stop. | |
4389 The caller (or its caller, etc) must gcpro all of ARGS, | |
4390 except that it isn't necessary to gcpro ARGS[0]. */ | |
4391 | |
4392 Lisp_Object | |
4393 run_hook_with_args_in_buffer (struct buffer *buf, int nargs, Lisp_Object *args, | |
4394 enum run_hooks_condition cond) | |
4395 { | |
4396 Lisp_Object sym, val, ret; | |
4397 | |
4398 if (!initialized || preparing_for_armageddon) | |
4399 /* We need to bail out of here pronto. */ | |
4400 return Qnil; | |
4401 | |
3092 | 4402 #ifndef NEW_GC |
428 | 4403 /* Whenever gc_in_progress is true, preparing_for_armageddon |
4404 will also be true unless something is really hosed. */ | |
4405 assert (!gc_in_progress); | |
3092 | 4406 #endif /* not NEW_GC */ |
428 | 4407 |
4408 sym = args[0]; | |
771 | 4409 val = symbol_value_in_buffer (sym, wrap_buffer (buf)); |
428 | 4410 ret = (cond == RUN_HOOKS_UNTIL_FAILURE ? Qt : Qnil); |
4411 | |
4412 if (UNBOUNDP (val) || NILP (val)) | |
4413 return ret; | |
4414 else if (!CONSP (val) || EQ (XCAR (val), Qlambda)) | |
4415 { | |
4416 args[0] = val; | |
4417 return Ffuncall (nargs, args); | |
4418 } | |
4419 else | |
4420 { | |
4421 struct gcpro gcpro1, gcpro2, gcpro3; | |
4422 Lisp_Object globals = Qnil; | |
4423 GCPRO3 (sym, val, globals); | |
4424 | |
4425 for (; | |
4426 CONSP (val) && ((cond == RUN_HOOKS_TO_COMPLETION) | |
4427 || (cond == RUN_HOOKS_UNTIL_SUCCESS ? NILP (ret) | |
4428 : !NILP (ret))); | |
4429 val = XCDR (val)) | |
4430 { | |
4431 if (EQ (XCAR (val), Qt)) | |
4432 { | |
4433 /* t indicates this hook has a local binding; | |
4434 it means to run the global binding too. */ | |
4435 globals = Fdefault_value (sym); | |
4436 | |
4437 if ((! CONSP (globals) || EQ (XCAR (globals), Qlambda)) && | |
4438 ! NILP (globals)) | |
4439 { | |
4440 args[0] = globals; | |
4441 ret = Ffuncall (nargs, args); | |
4442 } | |
4443 else | |
4444 { | |
4445 for (; | |
4446 CONSP (globals) && ((cond == RUN_HOOKS_TO_COMPLETION) | |
4447 || (cond == RUN_HOOKS_UNTIL_SUCCESS | |
4448 ? NILP (ret) | |
4449 : !NILP (ret))); | |
4450 globals = XCDR (globals)) | |
4451 { | |
4452 args[0] = XCAR (globals); | |
4453 /* In a global value, t should not occur. If it does, we | |
4454 must ignore it to avoid an endless loop. */ | |
4455 if (!EQ (args[0], Qt)) | |
4456 ret = Ffuncall (nargs, args); | |
4457 } | |
4458 } | |
4459 } | |
4460 else | |
4461 { | |
4462 args[0] = XCAR (val); | |
4463 ret = Ffuncall (nargs, args); | |
4464 } | |
4465 } | |
4466 | |
4467 UNGCPRO; | |
4468 return ret; | |
4469 } | |
4470 } | |
4471 | |
4472 Lisp_Object | |
4473 run_hook_with_args (int nargs, Lisp_Object *args, | |
4474 enum run_hooks_condition cond) | |
4475 { | |
4476 return run_hook_with_args_in_buffer (current_buffer, nargs, args, cond); | |
4477 } | |
4478 | |
4479 #if 0 | |
4480 | |
853 | 4481 /* From FSF 19.30, not currently used; seems like a big kludge. */ |
428 | 4482 |
4483 /* Run a hook symbol ARGS[0], but use FUNLIST instead of the actual | |
4484 present value of that symbol. | |
4485 Call each element of FUNLIST, | |
4486 passing each of them the rest of ARGS. | |
4487 The caller (or its caller, etc) must gcpro all of ARGS, | |
4488 except that it isn't necessary to gcpro ARGS[0]. */ | |
4489 | |
4490 Lisp_Object | |
4491 run_hook_list_with_args (Lisp_Object funlist, int nargs, Lisp_Object *args) | |
4492 { | |
853 | 4493 omitted; |
428 | 4494 } |
4495 | |
4496 #endif /* 0 */ | |
4497 | |
4498 void | |
4499 va_run_hook_with_args (Lisp_Object hook_var, int nargs, ...) | |
4500 { | |
4501 /* This function can GC */ | |
4502 struct gcpro gcpro1; | |
4503 int i; | |
4504 va_list vargs; | |
4505 Lisp_Object *funcall_args = alloca_array (Lisp_Object, 1 + nargs); | |
4506 | |
4507 va_start (vargs, nargs); | |
4508 funcall_args[0] = hook_var; | |
4509 for (i = 0; i < nargs; i++) | |
4510 funcall_args[i + 1] = va_arg (vargs, Lisp_Object); | |
4511 va_end (vargs); | |
4512 | |
4513 GCPRO1 (*funcall_args); | |
4514 gcpro1.nvars = nargs + 1; | |
4515 run_hook_with_args (nargs + 1, funcall_args, RUN_HOOKS_TO_COMPLETION); | |
4516 UNGCPRO; | |
4517 } | |
4518 | |
4519 void | |
4520 va_run_hook_with_args_in_buffer (struct buffer *buf, Lisp_Object hook_var, | |
4521 int nargs, ...) | |
4522 { | |
4523 /* This function can GC */ | |
4524 struct gcpro gcpro1; | |
4525 int i; | |
4526 va_list vargs; | |
4527 Lisp_Object *funcall_args = alloca_array (Lisp_Object, 1 + nargs); | |
4528 | |
4529 va_start (vargs, nargs); | |
4530 funcall_args[0] = hook_var; | |
4531 for (i = 0; i < nargs; i++) | |
4532 funcall_args[i + 1] = va_arg (vargs, Lisp_Object); | |
4533 va_end (vargs); | |
4534 | |
4535 GCPRO1 (*funcall_args); | |
4536 gcpro1.nvars = nargs + 1; | |
4537 run_hook_with_args_in_buffer (buf, nargs + 1, funcall_args, | |
4538 RUN_HOOKS_TO_COMPLETION); | |
4539 UNGCPRO; | |
4540 } | |
4541 | |
4542 Lisp_Object | |
4543 run_hook (Lisp_Object hook) | |
4544 { | |
853 | 4545 return run_hook_with_args (1, &hook, RUN_HOOKS_TO_COMPLETION); |
428 | 4546 } |
4547 | |
4548 | |
4549 /************************************************************************/ | |
4550 /* Front-ends to eval, funcall, apply */ | |
4551 /************************************************************************/ | |
4552 | |
4553 /* Apply fn to arg */ | |
4554 Lisp_Object | |
4555 apply1 (Lisp_Object fn, Lisp_Object arg) | |
4556 { | |
4557 /* This function can GC */ | |
4558 struct gcpro gcpro1; | |
4559 Lisp_Object args[2]; | |
4560 | |
4561 if (NILP (arg)) | |
4562 return Ffuncall (1, &fn); | |
4563 GCPRO1 (args[0]); | |
4564 gcpro1.nvars = 2; | |
4565 args[0] = fn; | |
4566 args[1] = arg; | |
4567 RETURN_UNGCPRO (Fapply (2, args)); | |
4568 } | |
4569 | |
4570 /* Call function fn on no arguments */ | |
4571 Lisp_Object | |
4572 call0 (Lisp_Object fn) | |
4573 { | |
4574 /* This function can GC */ | |
4575 struct gcpro gcpro1; | |
4576 | |
4577 GCPRO1 (fn); | |
4578 RETURN_UNGCPRO (Ffuncall (1, &fn)); | |
4579 } | |
4580 | |
4581 /* Call function fn with argument arg0 */ | |
4582 Lisp_Object | |
4583 call1 (Lisp_Object fn, | |
4584 Lisp_Object arg0) | |
4585 { | |
4586 /* This function can GC */ | |
4587 struct gcpro gcpro1; | |
4588 Lisp_Object args[2]; | |
4589 args[0] = fn; | |
4590 args[1] = arg0; | |
4591 GCPRO1 (args[0]); | |
4592 gcpro1.nvars = 2; | |
4593 RETURN_UNGCPRO (Ffuncall (2, args)); | |
4594 } | |
4595 | |
4596 /* Call function fn with arguments arg0, arg1 */ | |
4597 Lisp_Object | |
4598 call2 (Lisp_Object fn, | |
4599 Lisp_Object arg0, Lisp_Object arg1) | |
4600 { | |
4601 /* This function can GC */ | |
4602 struct gcpro gcpro1; | |
4603 Lisp_Object args[3]; | |
4604 args[0] = fn; | |
4605 args[1] = arg0; | |
4606 args[2] = arg1; | |
4607 GCPRO1 (args[0]); | |
4608 gcpro1.nvars = 3; | |
4609 RETURN_UNGCPRO (Ffuncall (3, args)); | |
4610 } | |
4611 | |
4612 /* Call function fn with arguments arg0, arg1, arg2 */ | |
4613 Lisp_Object | |
4614 call3 (Lisp_Object fn, | |
4615 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2) | |
4616 { | |
4617 /* This function can GC */ | |
4618 struct gcpro gcpro1; | |
4619 Lisp_Object args[4]; | |
4620 args[0] = fn; | |
4621 args[1] = arg0; | |
4622 args[2] = arg1; | |
4623 args[3] = arg2; | |
4624 GCPRO1 (args[0]); | |
4625 gcpro1.nvars = 4; | |
4626 RETURN_UNGCPRO (Ffuncall (4, args)); | |
4627 } | |
4628 | |
4629 /* Call function fn with arguments arg0, arg1, arg2, arg3 */ | |
4630 Lisp_Object | |
4631 call4 (Lisp_Object fn, | |
4632 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2, | |
4633 Lisp_Object arg3) | |
4634 { | |
4635 /* This function can GC */ | |
4636 struct gcpro gcpro1; | |
4637 Lisp_Object args[5]; | |
4638 args[0] = fn; | |
4639 args[1] = arg0; | |
4640 args[2] = arg1; | |
4641 args[3] = arg2; | |
4642 args[4] = arg3; | |
4643 GCPRO1 (args[0]); | |
4644 gcpro1.nvars = 5; | |
4645 RETURN_UNGCPRO (Ffuncall (5, args)); | |
4646 } | |
4647 | |
4648 /* Call function fn with arguments arg0, arg1, arg2, arg3, arg4 */ | |
4649 Lisp_Object | |
4650 call5 (Lisp_Object fn, | |
4651 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2, | |
4652 Lisp_Object arg3, Lisp_Object arg4) | |
4653 { | |
4654 /* This function can GC */ | |
4655 struct gcpro gcpro1; | |
4656 Lisp_Object args[6]; | |
4657 args[0] = fn; | |
4658 args[1] = arg0; | |
4659 args[2] = arg1; | |
4660 args[3] = arg2; | |
4661 args[4] = arg3; | |
4662 args[5] = arg4; | |
4663 GCPRO1 (args[0]); | |
4664 gcpro1.nvars = 6; | |
4665 RETURN_UNGCPRO (Ffuncall (6, args)); | |
4666 } | |
4667 | |
4668 Lisp_Object | |
4669 call6 (Lisp_Object fn, | |
4670 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2, | |
4671 Lisp_Object arg3, Lisp_Object arg4, Lisp_Object arg5) | |
4672 { | |
4673 /* This function can GC */ | |
4674 struct gcpro gcpro1; | |
4675 Lisp_Object args[7]; | |
4676 args[0] = fn; | |
4677 args[1] = arg0; | |
4678 args[2] = arg1; | |
4679 args[3] = arg2; | |
4680 args[4] = arg3; | |
4681 args[5] = arg4; | |
4682 args[6] = arg5; | |
4683 GCPRO1 (args[0]); | |
4684 gcpro1.nvars = 7; | |
4685 RETURN_UNGCPRO (Ffuncall (7, args)); | |
4686 } | |
4687 | |
4688 Lisp_Object | |
4689 call7 (Lisp_Object fn, | |
4690 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2, | |
4691 Lisp_Object arg3, Lisp_Object arg4, Lisp_Object arg5, | |
4692 Lisp_Object arg6) | |
4693 { | |
4694 /* This function can GC */ | |
4695 struct gcpro gcpro1; | |
4696 Lisp_Object args[8]; | |
4697 args[0] = fn; | |
4698 args[1] = arg0; | |
4699 args[2] = arg1; | |
4700 args[3] = arg2; | |
4701 args[4] = arg3; | |
4702 args[5] = arg4; | |
4703 args[6] = arg5; | |
4704 args[7] = arg6; | |
4705 GCPRO1 (args[0]); | |
4706 gcpro1.nvars = 8; | |
4707 RETURN_UNGCPRO (Ffuncall (8, args)); | |
4708 } | |
4709 | |
4710 Lisp_Object | |
4711 call8 (Lisp_Object fn, | |
4712 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2, | |
4713 Lisp_Object arg3, Lisp_Object arg4, Lisp_Object arg5, | |
4714 Lisp_Object arg6, Lisp_Object arg7) | |
4715 { | |
4716 /* This function can GC */ | |
4717 struct gcpro gcpro1; | |
4718 Lisp_Object args[9]; | |
4719 args[0] = fn; | |
4720 args[1] = arg0; | |
4721 args[2] = arg1; | |
4722 args[3] = arg2; | |
4723 args[4] = arg3; | |
4724 args[5] = arg4; | |
4725 args[6] = arg5; | |
4726 args[7] = arg6; | |
4727 args[8] = arg7; | |
4728 GCPRO1 (args[0]); | |
4729 gcpro1.nvars = 9; | |
4730 RETURN_UNGCPRO (Ffuncall (9, args)); | |
4731 } | |
4732 | |
4733 Lisp_Object | |
4734 call0_in_buffer (struct buffer *buf, Lisp_Object fn) | |
4735 { | |
4736 if (current_buffer == buf) | |
4737 return call0 (fn); | |
4738 else | |
4739 { | |
4740 Lisp_Object val; | |
4741 int speccount = specpdl_depth(); | |
4742 record_unwind_protect (Fset_buffer, Fcurrent_buffer ()); | |
4743 set_buffer_internal (buf); | |
4744 val = call0 (fn); | |
771 | 4745 unbind_to (speccount); |
428 | 4746 return val; |
4747 } | |
4748 } | |
4749 | |
4750 Lisp_Object | |
4751 call1_in_buffer (struct buffer *buf, Lisp_Object fn, | |
4752 Lisp_Object arg0) | |
4753 { | |
4754 if (current_buffer == buf) | |
4755 return call1 (fn, arg0); | |
4756 else | |
4757 { | |
4758 Lisp_Object val; | |
4759 int speccount = specpdl_depth(); | |
4760 record_unwind_protect (Fset_buffer, Fcurrent_buffer ()); | |
4761 set_buffer_internal (buf); | |
4762 val = call1 (fn, arg0); | |
771 | 4763 unbind_to (speccount); |
428 | 4764 return val; |
4765 } | |
4766 } | |
4767 | |
4768 Lisp_Object | |
4769 call2_in_buffer (struct buffer *buf, Lisp_Object fn, | |
4770 Lisp_Object arg0, Lisp_Object arg1) | |
4771 { | |
4772 if (current_buffer == buf) | |
4773 return call2 (fn, arg0, arg1); | |
4774 else | |
4775 { | |
4776 Lisp_Object val; | |
4777 int speccount = specpdl_depth(); | |
4778 record_unwind_protect (Fset_buffer, Fcurrent_buffer ()); | |
4779 set_buffer_internal (buf); | |
4780 val = call2 (fn, arg0, arg1); | |
771 | 4781 unbind_to (speccount); |
428 | 4782 return val; |
4783 } | |
4784 } | |
4785 | |
4786 Lisp_Object | |
4787 call3_in_buffer (struct buffer *buf, Lisp_Object fn, | |
4788 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2) | |
4789 { | |
4790 if (current_buffer == buf) | |
4791 return call3 (fn, arg0, arg1, arg2); | |
4792 else | |
4793 { | |
4794 Lisp_Object val; | |
4795 int speccount = specpdl_depth(); | |
4796 record_unwind_protect (Fset_buffer, Fcurrent_buffer ()); | |
4797 set_buffer_internal (buf); | |
4798 val = call3 (fn, arg0, arg1, arg2); | |
771 | 4799 unbind_to (speccount); |
428 | 4800 return val; |
4801 } | |
4802 } | |
4803 | |
4804 Lisp_Object | |
4805 call4_in_buffer (struct buffer *buf, Lisp_Object fn, | |
4806 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2, | |
4807 Lisp_Object arg3) | |
4808 { | |
4809 if (current_buffer == buf) | |
4810 return call4 (fn, arg0, arg1, arg2, arg3); | |
4811 else | |
4812 { | |
4813 Lisp_Object val; | |
4814 int speccount = specpdl_depth(); | |
4815 record_unwind_protect (Fset_buffer, Fcurrent_buffer ()); | |
4816 set_buffer_internal (buf); | |
4817 val = call4 (fn, arg0, arg1, arg2, arg3); | |
771 | 4818 unbind_to (speccount); |
428 | 4819 return val; |
4820 } | |
4821 } | |
4822 | |
4823 Lisp_Object | |
4824 eval_in_buffer (struct buffer *buf, Lisp_Object form) | |
4825 { | |
4826 if (current_buffer == buf) | |
4827 return Feval (form); | |
4828 else | |
4829 { | |
4830 Lisp_Object val; | |
4831 int speccount = specpdl_depth(); | |
4832 record_unwind_protect (Fset_buffer, Fcurrent_buffer ()); | |
4833 set_buffer_internal (buf); | |
4834 val = Feval (form); | |
771 | 4835 unbind_to (speccount); |
428 | 4836 return val; |
4837 } | |
4838 } | |
4839 | |
4840 | |
4841 /************************************************************************/ | |
4842 /* Error-catching front-ends to eval, funcall, apply */ | |
4843 /************************************************************************/ | |
4844 | |
853 | 4845 int |
4846 get_inhibit_flags (void) | |
4847 { | |
4848 return inhibit_flags; | |
4849 } | |
4850 | |
4851 void | |
2286 | 4852 check_allowed_operation (int what, Lisp_Object obj, Lisp_Object UNUSED (prop)) |
853 | 4853 { |
4854 if (inhibit_flags & INHIBIT_EXISTING_BUFFER_TEXT_MODIFICATION) | |
4855 { | |
4856 if (what == OPERATION_MODIFY_BUFFER_TEXT && BUFFERP (obj) | |
4857 && NILP (memq_no_quit (obj, Vmodifiable_buffers))) | |
4858 invalid_change | |
4859 ("Modification of this buffer not currently permitted", obj); | |
4860 } | |
4861 if (inhibit_flags & INHIBIT_EXISTING_PERMANENT_DISPLAY_OBJECT_DELETION) | |
4862 { | |
4863 if (what == OPERATION_DELETE_OBJECT | |
4864 && (BUFFERP (obj) || WINDOWP (obj) || FRAMEP (obj) || DEVICEP (obj) | |
4865 || CONSOLEP (obj)) | |
4866 && NILP (memq_no_quit (obj, Vdeletable_permanent_display_objects))) | |
4867 invalid_change | |
4868 ("Deletion of this object not currently permitted", obj); | |
4869 } | |
4870 } | |
4871 | |
4872 void | |
4873 note_object_created (Lisp_Object obj) | |
4874 { | |
4875 if (inhibit_flags & INHIBIT_EXISTING_BUFFER_TEXT_MODIFICATION) | |
4876 { | |
4877 if (BUFFERP (obj)) | |
4878 Vmodifiable_buffers = Fcons (obj, Vmodifiable_buffers); | |
4879 } | |
4880 if (inhibit_flags & INHIBIT_EXISTING_PERMANENT_DISPLAY_OBJECT_DELETION) | |
4881 { | |
4882 if (BUFFERP (obj) || WINDOWP (obj) || FRAMEP (obj) || DEVICEP (obj) | |
4883 || CONSOLEP (obj)) | |
4884 Vdeletable_permanent_display_objects = | |
4885 Fcons (obj, Vdeletable_permanent_display_objects); | |
4886 } | |
4887 } | |
4888 | |
4889 void | |
4890 note_object_deleted (Lisp_Object obj) | |
4891 { | |
4892 if (inhibit_flags & INHIBIT_EXISTING_BUFFER_TEXT_MODIFICATION) | |
4893 { | |
4894 if (BUFFERP (obj)) | |
4895 Vmodifiable_buffers = delq_no_quit (obj, Vmodifiable_buffers); | |
4896 } | |
4897 if (inhibit_flags & INHIBIT_EXISTING_PERMANENT_DISPLAY_OBJECT_DELETION) | |
4898 { | |
4899 if (BUFFERP (obj) || WINDOWP (obj) || FRAMEP (obj) || DEVICEP (obj) | |
4900 || CONSOLEP (obj)) | |
4901 Vdeletable_permanent_display_objects = | |
4902 delq_no_quit (obj, Vdeletable_permanent_display_objects); | |
4903 } | |
4904 } | |
4905 | |
4906 struct call_trapping_problems | |
4907 { | |
4908 Lisp_Object catchtag; | |
4909 Lisp_Object error_conditions; | |
4910 Lisp_Object data; | |
4911 Lisp_Object backtrace; | |
4912 Lisp_Object warning_class; | |
4913 | |
867 | 4914 const CIbyte *warning_string; |
853 | 4915 Lisp_Object (*fun) (void *); |
4916 void *arg; | |
4917 }; | |
428 | 4918 |
2532 | 4919 static Lisp_Object |
4920 maybe_get_trapping_problems_backtrace (void) | |
4921 { | |
4922 Lisp_Object backtrace; | |
853 | 4923 |
1123 | 4924 if (!(inhibit_flags & INHIBIT_WARNING_ISSUE) |
2532 | 4925 && !warning_will_be_discarded (current_warning_level ())) |
428 | 4926 { |
1333 | 4927 struct gcpro gcpro1; |
4928 Lisp_Object lstream = Qnil; | |
4929 int speccount = specpdl_depth (); | |
4930 | |
853 | 4931 /* We're no longer protected against errors or quit here, so at |
4932 least let's temporarily inhibit quit. We definitely do not | |
4933 want to inhibit quit during the calling of the function | |
4934 itself!!!!!!!!!!! */ | |
4935 | |
4936 specbind (Qinhibit_quit, Qt); | |
4937 | |
4938 GCPRO1 (lstream); | |
4939 lstream = make_resizing_buffer_output_stream (); | |
4940 Fbacktrace (lstream, Qt); | |
4941 Lstream_flush (XLSTREAM (lstream)); | |
2532 | 4942 backtrace = resizing_buffer_to_lisp_string (XLSTREAM (lstream)); |
853 | 4943 Lstream_delete (XLSTREAM (lstream)); |
4944 UNGCPRO; | |
4945 | |
4946 unbind_to (speccount); | |
428 | 4947 } |
853 | 4948 else |
2532 | 4949 backtrace = Qnil; |
4950 | |
4951 return backtrace; | |
4952 } | |
4953 | |
4954 static DECLARE_DOESNT_RETURN_TYPE | |
4955 (Lisp_Object, flagged_a_squirmer (Lisp_Object, Lisp_Object, Lisp_Object)); | |
4956 | |
4957 static DOESNT_RETURN_TYPE (Lisp_Object) | |
4958 flagged_a_squirmer (Lisp_Object error_conditions, Lisp_Object data, | |
4959 Lisp_Object opaque) | |
4960 { | |
4961 struct call_trapping_problems *p = | |
4962 (struct call_trapping_problems *) get_opaque_ptr (opaque); | |
4963 | |
4964 if (!EQ (error_conditions, Qquit)) | |
4965 p->backtrace = maybe_get_trapping_problems_backtrace (); | |
4966 else | |
853 | 4967 p->backtrace = Qnil; |
4968 p->error_conditions = error_conditions; | |
4969 p->data = data; | |
4970 | |
4971 Fthrow (p->catchtag, Qnil); | |
2268 | 4972 RETURN_NOT_REACHED (Qnil); |
853 | 4973 } |
4974 | |
4975 static Lisp_Object | |
4976 call_trapping_problems_2 (Lisp_Object opaque) | |
4977 { | |
4978 struct call_trapping_problems *p = | |
4979 (struct call_trapping_problems *) get_opaque_ptr (opaque); | |
4980 | |
4981 return (p->fun) (p->arg); | |
428 | 4982 } |
4983 | |
4984 static Lisp_Object | |
853 | 4985 call_trapping_problems_1 (Lisp_Object opaque) |
4986 { | |
4987 return call_with_condition_handler (flagged_a_squirmer, opaque, | |
4988 call_trapping_problems_2, opaque); | |
4989 } | |
4990 | |
1333 | 4991 static void |
4992 issue_call_trapping_problems_warning (Lisp_Object warning_class, | |
4993 const CIbyte *warning_string, | |
4994 struct call_trapping_problems_result *p) | |
4995 { | |
4996 if (!warning_will_be_discarded (current_warning_level ())) | |
4997 { | |
4998 int depth = specpdl_depth (); | |
4999 | |
5000 /* We're no longer protected against errors or quit here, so at | |
5001 least let's temporarily inhibit quit. */ | |
5002 specbind (Qinhibit_quit, Qt); | |
5003 | |
5004 if (p->caught_throw) | |
5005 { | |
5006 Lisp_Object errstr = | |
5007 emacs_sprintf_string_lisp | |
2532 | 5008 ("%s: Attempt to throw outside of function:" |
5009 "To catch `%s' with value `%s'\n\nBacktrace follows:\n\n%s", | |
2725 | 5010 Qnil, 4, |
1333 | 5011 build_msg_string (warning_string ? warning_string : "error"), |
2532 | 5012 p->thrown_tag, p->thrown_value, p->backtrace); |
1333 | 5013 warn_when_safe_lispobj (Qerror, current_warning_level (), errstr); |
5014 } | |
2421 | 5015 else if (p->caught_error && !EQ (p->error_conditions, Qquit)) |
1333 | 5016 { |
5017 Lisp_Object errstr; | |
5018 /* #### This should call | |
5019 (with-output-to-string (display-error (cons error_conditions | |
5020 data)) | |
5021 but that stuff is all in Lisp currently. */ | |
5022 errstr = | |
5023 emacs_sprintf_string_lisp | |
5024 ("%s: (%s %s)\n\nBacktrace follows:\n\n%s", | |
5025 Qnil, 4, | |
5026 build_msg_string (warning_string ? warning_string : "error"), | |
5027 p->error_conditions, p->data, p->backtrace); | |
5028 | |
5029 warn_when_safe_lispobj (warning_class, current_warning_level (), | |
5030 errstr); | |
5031 } | |
5032 | |
5033 unbind_to (depth); | |
5034 } | |
5035 } | |
5036 | |
1318 | 5037 /* Turn on the trapping flags in FLAGS -- see call_trapping_problems(). |
5038 This cannot handle INTERNAL_INHIBIT_THROWS() or INTERNAL_INHIBIT_ERRORS | |
5039 (because they ultimately boil down to a setjmp()!) -- you must directly | |
5040 use call_trapping_problems() for that. Turn the flags off with | |
5041 unbind_to(). Returns the "canonicalized" flags (particularly in the | |
5042 case of INHIBIT_ANY_CHANGE_AFFECTING_REDISPLAY, which is shorthand for | |
5043 various other flags). */ | |
5044 | |
5045 int | |
5046 set_trapping_problems_flags (int flags) | |
5047 { | |
5048 int new_inhibit_flags; | |
5049 | |
5050 if (flags & INHIBIT_ANY_CHANGE_AFFECTING_REDISPLAY) | |
5051 flags |= INHIBIT_EXISTING_PERMANENT_DISPLAY_OBJECT_DELETION | |
5052 | INHIBIT_EXISTING_BUFFER_TEXT_MODIFICATION | |
5053 | INHIBIT_ENTERING_DEBUGGER | |
5054 | INHIBIT_WARNING_ISSUE | |
5055 | INHIBIT_GC; | |
5056 | |
5057 new_inhibit_flags = inhibit_flags | flags; | |
5058 if (new_inhibit_flags != inhibit_flags) | |
5059 internal_bind_int (&inhibit_flags, new_inhibit_flags); | |
5060 | |
5061 if (flags & INHIBIT_QUIT) | |
5062 specbind (Qinhibit_quit, Qt); | |
5063 | |
5064 if (flags & UNINHIBIT_QUIT) | |
5065 begin_do_check_for_quit (); | |
5066 | |
5067 if (flags & INHIBIT_GC) | |
5068 begin_gc_forbidden (); | |
5069 | |
5070 /* #### If we have nested calls to call_trapping_problems(), and the | |
5071 inner one creates some buffers/etc., should the outer one be able | |
5072 to delete them? I think so, but it means we need to combine rather | |
5073 than just reset the value. */ | |
5074 if (flags & INHIBIT_EXISTING_PERMANENT_DISPLAY_OBJECT_DELETION) | |
5075 internal_bind_lisp_object (&Vdeletable_permanent_display_objects, Qnil); | |
5076 | |
5077 if (flags & INHIBIT_EXISTING_BUFFER_TEXT_MODIFICATION) | |
5078 internal_bind_lisp_object (&Vmodifiable_buffers, Qnil); | |
5079 | |
5080 return flags; | |
5081 } | |
5082 | |
853 | 5083 /* This is equivalent to (*fun) (arg), except that various conditions |
5084 can be trapped or inhibited, according to FLAGS. | |
5085 | |
5086 If FLAGS does not contain NO_INHIBIT_ERRORS, when an error occurs, | |
5087 the error is caught and a warning is issued, specifying the | |
5088 specific error that occurred and a backtrace. In that case, | |
5089 WARNING_STRING should be given, and will be printed at the | |
5090 beginning of the error to indicate where the error occurred. | |
5091 | |
5092 If FLAGS does not contain NO_INHIBIT_THROWS, all attempts to | |
5093 `throw' out of the function being called are trapped, and a warning | |
5094 issued. (Again, WARNING_STRING should be given.) | |
5095 | |
2367 | 5096 If FLAGS contains INHIBIT_WARNING_ISSUE, no warnings are issued; |
853 | 5097 this applies to recursive invocations of call_trapping_problems, too. |
5098 | |
1333 | 5099 If FLAGS contains POSTPONE_WARNING_ISSUE, no warnings are issued; |
5100 but values useful for generating a warning are still computed (in | |
5101 particular, the backtrace), so that the calling function can issue | |
5102 a warning. | |
5103 | |
853 | 5104 If FLAGS contains ISSUE_WARNINGS_AT_DEBUG_LEVEL, warnings will be |
5105 issued, but at level `debug', which normally is below the minimum | |
5106 specified by `log-warning-minimum-level', meaning such warnings will | |
5107 be ignored entirely. The user can change this variable, however, | |
5108 to see the warnings.) | |
5109 | |
5110 Note: If neither of NO_INHIBIT_THROWS or NO_INHIBIT_ERRORS is | |
5111 given, you are *guaranteed* that there will be no non-local exits | |
5112 out of this function. | |
5113 | |
5114 If FLAGS contains INHIBIT_QUIT, QUIT using C-g is inhibited. (This | |
5115 is *rarely* a good idea. Unless you use NO_INHIBIT_ERRORS, QUIT is | |
5116 automatically caught as well, and treated as an error; you can | |
5117 check for this using EQ (problems->error_conditions, Qquit). | |
5118 | |
5119 If FLAGS contains UNINHIBIT_QUIT, QUIT checking will be explicitly | |
5120 turned on. (It will abort the code being called, but will still be | |
5121 trapped and reported as an error, unless NO_INHIBIT_ERRORS is | |
5122 given.) This is useful when QUIT checking has been turned off by a | |
5123 higher-level caller. | |
5124 | |
5125 If FLAGS contains INHIBIT_GC, garbage collection is inhibited. | |
1123 | 5126 This is useful for Lisp called within redisplay, for example. |
853 | 5127 |
5128 If FLAGS contains INHIBIT_EXISTING_PERMANENT_DISPLAY_OBJECT_DELETION, | |
5129 Lisp code is not allowed to delete any window, buffers, frames, devices, | |
5130 or consoles that were already in existence at the time this function | |
5131 was called. (However, it's perfectly legal for code to create a new | |
5132 buffer and then delete it.) | |
5133 | |
5134 #### It might be useful to have a flag that inhibits deletion of a | |
5135 specific permanent display object and everything it's attached to | |
5136 (e.g. a window, and the buffer, frame, device, and console it's | |
5137 attached to. | |
5138 | |
5139 If FLAGS contains INHIBIT_EXISTING_BUFFER_TEXT_MODIFICATION, Lisp | |
5140 code is not allowed to modify the text of any buffers that were | |
5141 already in existence at the time this function was called. | |
5142 (However, it's perfectly legal for code to create a new buffer and | |
5143 then modify its text.) | |
5144 | |
5145 [These last two flags are implemented using global variables | |
5146 Vdeletable_permanent_display_objects and Vmodifiable_buffers, | |
5147 which keep track of a list of all buffers or permanent display | |
5148 objects created since the last time one of these flags was set. | |
5149 The code that deletes buffers, etc. and modifies buffers checks | |
5150 | |
5151 (1) if the corresponding flag is set (through the global variable | |
5152 inhibit_flags or its accessor function get_inhibit_flags()), and | |
5153 | |
5154 (2) if the object to be modified or deleted is not in the | |
5155 appropriate list. | |
5156 | |
5157 If so, it signals an error. | |
5158 | |
5159 Recursive calls to call_trapping_problems() are allowed. In | |
5160 the case of the two flags mentioned above, the current values | |
5161 of the global variables are stored in an unwind-protect, and | |
5162 they're reset to nil.] | |
5163 | |
5164 If FLAGS contains INHIBIT_ENTERING_DEBUGGER, the debugger will not | |
5165 be entered if an error occurs inside the Lisp code being called, | |
5166 even when the user has requested an error. In such case, a warning | |
5167 is issued stating that access to the debugger is denied, unless | |
5168 INHIBIT_WARNING_ISSUE has also been supplied. This is useful when | |
5169 calling Lisp code inside redisplay, in menu callbacks, etc. because | |
5170 in such cases either the display is in an inconsistent state or | |
5171 doing window operations is explicitly forbidden by the OS, and the | |
5172 debugger would causes visual changes on the screen and might create | |
5173 another frame. | |
5174 | |
5175 If FLAGS contains INHIBIT_ANY_CHANGE_AFFECTING_REDISPLAY, no | |
5176 changes of any sort to extents, faces, glyphs, buffer text, | |
5177 specifiers relating to display, other variables relating to | |
5178 display, splitting, deleting, or resizing windows or frames, | |
5179 deleting buffers, windows, frames, devices, or consoles, etc. is | |
5180 allowed. This is for things called absolutely in the middle of | |
5181 redisplay, which expects things to be *exactly* the same after the | |
5182 call as before. This isn't completely implemented and needs to be | |
5183 thought out some more to determine exactly what its semantics are. | |
5184 For the moment, turning on this flag also turns on | |
5185 | |
5186 INHIBIT_EXISTING_PERMANENT_DISPLAY_OBJECT_DELETION | |
5187 INHIBIT_EXISTING_BUFFER_TEXT_MODIFICATION | |
5188 INHIBIT_ENTERING_DEBUGGER | |
5189 INHIBIT_WARNING_ISSUE | |
5190 INHIBIT_GC | |
5191 | |
5192 #### The following five flags are defined, but unimplemented: | |
5193 | |
5194 #define INHIBIT_EXISTING_CODING_SYSTEM_DELETION (1<<6) | |
5195 #define INHIBIT_EXISTING_CHARSET_DELETION (1<<7) | |
5196 #define INHIBIT_PERMANENT_DISPLAY_OBJECT_CREATION (1<<8) | |
5197 #define INHIBIT_CODING_SYSTEM_CREATION (1<<9) | |
5198 #define INHIBIT_CHARSET_CREATION (1<<10) | |
5199 | |
5200 FLAGS containing CALL_WITH_SUSPENDED_ERRORS is a sign that | |
5201 call_with_suspended_errors() was invoked. This exists only for | |
5202 debugging purposes -- often we want to break when a signal happens, | |
5203 but ignore signals from call_with_suspended_errors(), because they | |
5204 occur often and for legitimate reasons. | |
5205 | |
5206 If PROBLEM is non-zero, it should be a pointer to a structure into | |
5207 which exact information about any occurring problems (either an | |
5208 error or an attempted throw past this boundary). | |
5209 | |
5210 If a problem occurred and aborted operation (error, quit, or | |
5211 invalid throw), Qunbound is returned. Otherwise the return value | |
5212 from the call to (*fun) (arg) is returned. */ | |
5213 | |
5214 Lisp_Object | |
5215 call_trapping_problems (Lisp_Object warning_class, | |
867 | 5216 const CIbyte *warning_string, |
853 | 5217 int flags, |
5218 struct call_trapping_problems_result *problem, | |
5219 Lisp_Object (*fun) (void *), | |
5220 void *arg) | |
5221 { | |
1318 | 5222 int speccount = specpdl_depth (); |
853 | 5223 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5; |
5224 struct call_trapping_problems package; | |
1333 | 5225 struct call_trapping_problems_result real_problem; |
2532 | 5226 Lisp_Object opaque, thrown_tag, tem, thrown_backtrace; |
853 | 5227 int thrown = 0; |
5228 | |
5229 assert (SYMBOLP (warning_class)); /* sanity-check */ | |
5230 assert (!NILP (warning_class)); | |
5231 | |
5232 flags ^= INTERNAL_INHIBIT_ERRORS | INTERNAL_INHIBIT_THROWS; | |
5233 | |
5234 package.warning_class = warning_class; | |
5235 package.warning_string = warning_string; | |
5236 package.fun = fun; | |
5237 package.arg = arg; | |
5238 package.catchtag = | |
5239 flags & INTERNAL_INHIBIT_THROWS ? Vcatch_everything_tag : | |
5240 flags & INTERNAL_INHIBIT_ERRORS ? make_opaque_ptr (0) : | |
5241 Qnil; | |
5242 package.error_conditions = Qnil; | |
5243 package.data = Qnil; | |
5244 package.backtrace = Qnil; | |
5245 | |
1318 | 5246 flags = set_trapping_problems_flags (flags); |
853 | 5247 |
5248 if (flags & (INTERNAL_INHIBIT_THROWS | INTERNAL_INHIBIT_ERRORS)) | |
5249 opaque = make_opaque_ptr (&package); | |
5250 else | |
5251 opaque = Qnil; | |
5252 | |
5253 GCPRO5 (package.catchtag, package.error_conditions, package.data, | |
5254 package.backtrace, opaque); | |
5255 | |
5256 if (flags & INTERNAL_INHIBIT_ERRORS) | |
5257 /* We need a catch so that our condition-handler can throw back here | |
5258 after printing the warning. (We print the warning in the stack | |
5259 context of the error, so we can get a backtrace.) */ | |
5260 tem = internal_catch (package.catchtag, call_trapping_problems_1, opaque, | |
2532 | 5261 &thrown, &thrown_tag, &thrown_backtrace); |
853 | 5262 else if (flags & INTERNAL_INHIBIT_THROWS) |
5263 /* We skip over the first wrapper, which traps errors. */ | |
5264 tem = internal_catch (package.catchtag, call_trapping_problems_2, opaque, | |
2532 | 5265 &thrown, &thrown_tag, &thrown_backtrace); |
853 | 5266 else |
5267 /* Nothing special. */ | |
5268 tem = (fun) (arg); | |
5269 | |
1333 | 5270 if (!problem) |
5271 problem = &real_problem; | |
5272 | |
5273 if (!thrown) | |
853 | 5274 { |
1333 | 5275 problem->caught_error = 0; |
5276 problem->caught_throw = 0; | |
5277 problem->error_conditions = Qnil; | |
5278 problem->data = Qnil; | |
5279 problem->backtrace = Qnil; | |
5280 problem->thrown_tag = Qnil; | |
5281 problem->thrown_value = Qnil; | |
853 | 5282 } |
1333 | 5283 else if (EQ (thrown_tag, package.catchtag)) |
853 | 5284 { |
1333 | 5285 problem->caught_error = 1; |
5286 problem->caught_throw = 0; | |
5287 problem->error_conditions = package.error_conditions; | |
5288 problem->data = package.data; | |
5289 problem->backtrace = package.backtrace; | |
5290 problem->thrown_tag = Qnil; | |
5291 problem->thrown_value = Qnil; | |
853 | 5292 } |
1333 | 5293 else |
5294 { | |
5295 problem->caught_error = 0; | |
5296 problem->caught_throw = 1; | |
5297 problem->error_conditions = Qnil; | |
5298 problem->data = Qnil; | |
2532 | 5299 problem->backtrace = thrown_backtrace; |
1333 | 5300 problem->thrown_tag = thrown_tag; |
5301 problem->thrown_value = tem; | |
5302 } | |
5303 | |
5304 if (!(flags & INHIBIT_WARNING_ISSUE) && !(flags & POSTPONE_WARNING_ISSUE)) | |
5305 issue_call_trapping_problems_warning (warning_class, warning_string, | |
5306 problem); | |
853 | 5307 |
5308 if (!NILP (package.catchtag) && | |
5309 !EQ (package.catchtag, Vcatch_everything_tag)) | |
5310 free_opaque_ptr (package.catchtag); | |
5311 | |
5312 if (!NILP (opaque)) | |
5313 free_opaque_ptr (opaque); | |
5314 | |
5315 unbind_to (speccount); | |
5316 RETURN_UNGCPRO (thrown ? Qunbound : tem); | |
5317 } | |
5318 | |
5319 struct va_call_trapping_problems | |
5320 { | |
5321 lisp_fn_t fun; | |
5322 int nargs; | |
5323 Lisp_Object *args; | |
5324 }; | |
5325 | |
5326 static Lisp_Object | |
5327 va_call_trapping_problems_1 (void *ai_mi_madre) | |
5328 { | |
5329 struct va_call_trapping_problems *ai_no_corrida = | |
5330 (struct va_call_trapping_problems *) ai_mi_madre; | |
5331 Lisp_Object pegar_no_bumbum; | |
5332 | |
5333 PRIMITIVE_FUNCALL (pegar_no_bumbum, ai_no_corrida->fun, | |
5334 ai_no_corrida->args, ai_no_corrida->nargs); | |
5335 return pegar_no_bumbum; | |
5336 } | |
5337 | |
5338 /* #### document me. */ | |
5339 | |
5340 Lisp_Object | |
5341 va_call_trapping_problems (Lisp_Object warning_class, | |
867 | 5342 const CIbyte *warning_string, |
853 | 5343 int flags, |
5344 struct call_trapping_problems_result *problem, | |
5345 lisp_fn_t fun, int nargs, ...) | |
5346 { | |
5347 va_list vargs; | |
5348 Lisp_Object args[20]; | |
5349 int i; | |
5350 struct va_call_trapping_problems fazer_invocacao_atrapalhando_problemas; | |
5351 struct gcpro gcpro1; | |
5352 | |
5353 assert (nargs >= 0 && nargs < 20); | |
5354 | |
5355 va_start (vargs, nargs); | |
5356 for (i = 0; i < nargs; i++) | |
5357 args[i] = va_arg (vargs, Lisp_Object); | |
5358 va_end (vargs); | |
5359 | |
5360 fazer_invocacao_atrapalhando_problemas.fun = fun; | |
5361 fazer_invocacao_atrapalhando_problemas.nargs = nargs; | |
5362 fazer_invocacao_atrapalhando_problemas.args = args; | |
5363 | |
5364 GCPRO1_ARRAY (args, nargs); | |
5365 RETURN_UNGCPRO | |
5366 (call_trapping_problems | |
5367 (warning_class, warning_string, flags, problem, | |
5368 va_call_trapping_problems_1, &fazer_invocacao_atrapalhando_problemas)); | |
5369 } | |
5370 | |
5371 /* this is an older interface, barely different from | |
5372 va_call_trapping_problems. | |
5373 | |
5374 #### eliminate this or at least merge the ERROR_BEHAVIOR stuff into | |
5375 va_call_trapping_problems(). */ | |
5376 | |
5377 Lisp_Object | |
5378 call_with_suspended_errors (lisp_fn_t fun, Lisp_Object retval, | |
1204 | 5379 Lisp_Object class_, Error_Behavior errb, |
853 | 5380 int nargs, ...) |
5381 { | |
5382 va_list vargs; | |
5383 Lisp_Object args[20]; | |
5384 int i; | |
5385 struct va_call_trapping_problems fazer_invocacao_atrapalhando_problemas; | |
5386 int flags; | |
5387 struct gcpro gcpro1; | |
5388 | |
1204 | 5389 assert (SYMBOLP (class_)); /* sanity-check */ |
5390 assert (!NILP (class_)); | |
853 | 5391 assert (nargs >= 0 && nargs < 20); |
5392 | |
5393 va_start (vargs, nargs); | |
5394 for (i = 0; i < nargs; i++) | |
5395 args[i] = va_arg (vargs, Lisp_Object); | |
5396 va_end (vargs); | |
5397 | |
5398 /* If error-checking is not disabled, just call the function. */ | |
5399 | |
5400 if (ERRB_EQ (errb, ERROR_ME)) | |
5401 { | |
5402 Lisp_Object val; | |
5403 PRIMITIVE_FUNCALL (val, fun, args, nargs); | |
5404 return val; | |
5405 } | |
5406 | |
5407 if (ERRB_EQ (errb, ERROR_ME_NOT)) /* person wants no warnings */ | |
5408 flags = INHIBIT_WARNING_ISSUE | INHIBIT_ENTERING_DEBUGGER; | |
5409 else if (ERRB_EQ (errb, ERROR_ME_DEBUG_WARN)) | |
5410 flags = ISSUE_WARNINGS_AT_DEBUG_LEVEL | INHIBIT_ENTERING_DEBUGGER; | |
5411 else | |
5412 { | |
5413 assert (ERRB_EQ (errb, ERROR_ME_WARN)); | |
5414 flags = INHIBIT_ENTERING_DEBUGGER; | |
5415 } | |
5416 | |
5417 flags |= CALL_WITH_SUSPENDED_ERRORS; | |
5418 | |
5419 fazer_invocacao_atrapalhando_problemas.fun = fun; | |
5420 fazer_invocacao_atrapalhando_problemas.nargs = nargs; | |
5421 fazer_invocacao_atrapalhando_problemas.args = args; | |
5422 | |
5423 GCPRO1_ARRAY (args, nargs); | |
5424 { | |
5425 Lisp_Object its_way_too_goddamn_late = | |
5426 call_trapping_problems | |
1204 | 5427 (class_, 0, flags, 0, va_call_trapping_problems_1, |
853 | 5428 &fazer_invocacao_atrapalhando_problemas); |
5429 UNGCPRO; | |
5430 if (UNBOUNDP (its_way_too_goddamn_late)) | |
5431 return retval; | |
5432 else | |
5433 return its_way_too_goddamn_late; | |
5434 } | |
5435 } | |
5436 | |
5437 struct calln_trapping_problems | |
5438 { | |
5439 int nargs; | |
5440 Lisp_Object *args; | |
5441 }; | |
5442 | |
5443 static Lisp_Object | |
5444 calln_trapping_problems_1 (void *puta) | |
5445 { | |
5446 struct calln_trapping_problems *p = (struct calln_trapping_problems *) puta; | |
5447 | |
5448 return Ffuncall (p->nargs, p->args); | |
428 | 5449 } |
5450 | |
5451 static Lisp_Object | |
853 | 5452 calln_trapping_problems (Lisp_Object warning_class, |
867 | 5453 const CIbyte *warning_string, int flags, |
853 | 5454 struct call_trapping_problems_result *problem, |
5455 int nargs, Lisp_Object *args) | |
5456 { | |
5457 struct calln_trapping_problems foo; | |
5458 struct gcpro gcpro1; | |
5459 | |
5460 if (SYMBOLP (args[0])) | |
5461 { | |
5462 Lisp_Object tem = XSYMBOL (args[0])->function; | |
5463 if (NILP (tem) || UNBOUNDP (tem)) | |
5464 { | |
5465 if (problem) | |
5466 { | |
5467 problem->caught_error = 0; | |
5468 problem->caught_throw = 0; | |
5469 problem->error_conditions = Qnil; | |
5470 problem->data = Qnil; | |
5471 problem->backtrace = Qnil; | |
5472 problem->thrown_tag = Qnil; | |
5473 problem->thrown_value = Qnil; | |
5474 } | |
5475 return Qnil; | |
5476 } | |
5477 } | |
5478 | |
5479 foo.nargs = nargs; | |
5480 foo.args = args; | |
5481 | |
5482 GCPRO1_ARRAY (args, nargs); | |
5483 RETURN_UNGCPRO (call_trapping_problems (warning_class, warning_string, | |
5484 flags, problem, | |
5485 calln_trapping_problems_1, | |
5486 &foo)); | |
5487 } | |
5488 | |
5489 /* #### fix these functions to follow the calling convention of | |
5490 call_trapping_problems! */ | |
5491 | |
5492 Lisp_Object | |
867 | 5493 call0_trapping_problems (const CIbyte *warning_string, Lisp_Object function, |
853 | 5494 int flags) |
5495 { | |
5496 return calln_trapping_problems (Qerror, warning_string, flags, 0, 1, | |
5497 &function); | |
428 | 5498 } |
5499 | |
5500 Lisp_Object | |
867 | 5501 call1_trapping_problems (const CIbyte *warning_string, Lisp_Object function, |
853 | 5502 Lisp_Object object, int flags) |
5503 { | |
5504 Lisp_Object args[2]; | |
5505 | |
5506 args[0] = function; | |
5507 args[1] = object; | |
5508 | |
5509 return calln_trapping_problems (Qerror, warning_string, flags, 0, 2, | |
5510 args); | |
5511 } | |
5512 | |
5513 Lisp_Object | |
867 | 5514 call2_trapping_problems (const CIbyte *warning_string, Lisp_Object function, |
853 | 5515 Lisp_Object object1, Lisp_Object object2, |
5516 int flags) | |
5517 { | |
5518 Lisp_Object args[3]; | |
5519 | |
5520 args[0] = function; | |
5521 args[1] = object1; | |
5522 args[2] = object2; | |
5523 | |
5524 return calln_trapping_problems (Qerror, warning_string, flags, 0, 3, | |
5525 args); | |
5526 } | |
5527 | |
5528 Lisp_Object | |
867 | 5529 call3_trapping_problems (const CIbyte *warning_string, Lisp_Object function, |
853 | 5530 Lisp_Object object1, Lisp_Object object2, |
5531 Lisp_Object object3, int flags) | |
5532 { | |
5533 Lisp_Object args[4]; | |
5534 | |
5535 args[0] = function; | |
5536 args[1] = object1; | |
5537 args[2] = object2; | |
5538 args[3] = object3; | |
5539 | |
5540 return calln_trapping_problems (Qerror, warning_string, flags, 0, 4, | |
5541 args); | |
5542 } | |
5543 | |
5544 Lisp_Object | |
867 | 5545 call4_trapping_problems (const CIbyte *warning_string, Lisp_Object function, |
853 | 5546 Lisp_Object object1, Lisp_Object object2, |
5547 Lisp_Object object3, Lisp_Object object4, | |
5548 int flags) | |
5549 { | |
5550 Lisp_Object args[5]; | |
5551 | |
5552 args[0] = function; | |
5553 args[1] = object1; | |
5554 args[2] = object2; | |
5555 args[3] = object3; | |
5556 args[4] = object4; | |
5557 | |
5558 return calln_trapping_problems (Qerror, warning_string, flags, 0, 5, | |
5559 args); | |
5560 } | |
5561 | |
5562 Lisp_Object | |
867 | 5563 call5_trapping_problems (const CIbyte *warning_string, Lisp_Object function, |
853 | 5564 Lisp_Object object1, Lisp_Object object2, |
5565 Lisp_Object object3, Lisp_Object object4, | |
5566 Lisp_Object object5, int flags) | |
5567 { | |
5568 Lisp_Object args[6]; | |
5569 | |
5570 args[0] = function; | |
5571 args[1] = object1; | |
5572 args[2] = object2; | |
5573 args[3] = object3; | |
5574 args[4] = object4; | |
5575 args[5] = object5; | |
5576 | |
5577 return calln_trapping_problems (Qerror, warning_string, flags, 0, 6, | |
5578 args); | |
5579 } | |
5580 | |
5581 struct eval_in_buffer_trapping_problems | |
5582 { | |
5583 struct buffer *buf; | |
5584 Lisp_Object form; | |
5585 }; | |
5586 | |
5587 static Lisp_Object | |
5588 eval_in_buffer_trapping_problems_1 (void *arg) | |
5589 { | |
5590 struct eval_in_buffer_trapping_problems *p = | |
5591 (struct eval_in_buffer_trapping_problems *) arg; | |
5592 | |
5593 return eval_in_buffer (p->buf, p->form); | |
5594 } | |
5595 | |
5596 /* #### fix these functions to follow the calling convention of | |
5597 call_trapping_problems! */ | |
5598 | |
5599 Lisp_Object | |
867 | 5600 eval_in_buffer_trapping_problems (const CIbyte *warning_string, |
853 | 5601 struct buffer *buf, Lisp_Object form, |
5602 int flags) | |
5603 { | |
5604 struct eval_in_buffer_trapping_problems p; | |
5605 Lisp_Object buffer = wrap_buffer (buf); | |
428 | 5606 struct gcpro gcpro1, gcpro2; |
5607 | |
853 | 5608 GCPRO2 (buffer, form); |
5609 p.buf = buf; | |
5610 p.form = form; | |
5611 RETURN_UNGCPRO (call_trapping_problems (Qerror, warning_string, flags, 0, | |
5612 eval_in_buffer_trapping_problems_1, | |
5613 &p)); | |
5614 } | |
5615 | |
5616 Lisp_Object | |
1333 | 5617 run_hook_trapping_problems (Lisp_Object warning_class, |
853 | 5618 Lisp_Object hook_symbol, |
5619 int flags) | |
5620 { | |
1333 | 5621 return run_hook_with_args_trapping_problems (warning_class, 1, &hook_symbol, |
853 | 5622 RUN_HOOKS_TO_COMPLETION, |
5623 flags); | |
428 | 5624 } |
5625 | |
5626 static Lisp_Object | |
853 | 5627 safe_run_hook_trapping_problems_1 (void *puta) |
5628 { | |
5629 Lisp_Object hook = VOID_TO_LISP (puta); | |
5630 | |
5631 run_hook (hook); | |
428 | 5632 return Qnil; |
5633 } | |
5634 | |
853 | 5635 /* Same as run_hook_trapping_problems() but also set the hook to nil |
5636 if an error occurs (but not a quit). */ | |
5637 | |
428 | 5638 Lisp_Object |
1333 | 5639 safe_run_hook_trapping_problems (Lisp_Object warning_class, |
5640 Lisp_Object hook_symbol, int flags) | |
853 | 5641 { |
428 | 5642 Lisp_Object tem; |
853 | 5643 struct gcpro gcpro1, gcpro2; |
5644 struct call_trapping_problems_result prob; | |
428 | 5645 |
5646 if (!initialized || preparing_for_armageddon) | |
5647 return Qnil; | |
5648 tem = find_symbol_value (hook_symbol); | |
5649 if (NILP (tem) || UNBOUNDP (tem)) | |
5650 return Qnil; | |
5651 | |
853 | 5652 GCPRO2 (hook_symbol, tem); |
1333 | 5653 tem = call_trapping_problems (Qerror, NULL, |
5654 flags | POSTPONE_WARNING_ISSUE, | |
853 | 5655 &prob, |
5656 safe_run_hook_trapping_problems_1, | |
5657 LISP_TO_VOID (hook_symbol)); | |
1333 | 5658 { |
5659 Lisp_Object hook_name = XSYMBOL_NAME (hook_symbol); | |
5660 Ibyte *hook_str = XSTRING_DATA (hook_name); | |
5661 Ibyte *err = alloca_ibytes (XSTRING_LENGTH (hook_name) + 100); | |
5662 | |
5663 if (prob.caught_throw || (prob.caught_error && !EQ (prob.error_conditions, | |
5664 Qquit))) | |
5665 { | |
5666 Fset (hook_symbol, Qnil); | |
5667 qxesprintf (err, "Error in `%s' (resetting to nil)", hook_str); | |
5668 } | |
5669 else | |
5670 qxesprintf (err, "Quit in `%s'", hook_str); | |
5671 | |
5672 | |
5673 issue_call_trapping_problems_warning (warning_class, (CIbyte *) err, | |
5674 &prob); | |
5675 } | |
5676 | |
5677 UNGCPRO; | |
5678 return tem; | |
853 | 5679 } |
5680 | |
5681 struct run_hook_with_args_in_buffer_trapping_problems | |
5682 { | |
5683 struct buffer *buf; | |
5684 int nargs; | |
5685 Lisp_Object *args; | |
5686 enum run_hooks_condition cond; | |
5687 }; | |
5688 | |
5689 static Lisp_Object | |
5690 run_hook_with_args_in_buffer_trapping_problems_1 (void *puta) | |
5691 { | |
5692 struct run_hook_with_args_in_buffer_trapping_problems *porra = | |
5693 (struct run_hook_with_args_in_buffer_trapping_problems *) puta; | |
5694 | |
5695 return run_hook_with_args_in_buffer (porra->buf, porra->nargs, porra->args, | |
5696 porra->cond); | |
5697 } | |
5698 | |
5699 /* #### fix these functions to follow the calling convention of | |
5700 call_trapping_problems! */ | |
428 | 5701 |
5702 Lisp_Object | |
1333 | 5703 run_hook_with_args_in_buffer_trapping_problems (Lisp_Object warning_class, |
853 | 5704 struct buffer *buf, int nargs, |
5705 Lisp_Object *args, | |
5706 enum run_hooks_condition cond, | |
5707 int flags) | |
5708 { | |
5709 Lisp_Object sym, val, ret; | |
5710 struct run_hook_with_args_in_buffer_trapping_problems diversity_and_distrust; | |
428 | 5711 struct gcpro gcpro1; |
1333 | 5712 Lisp_Object hook_name; |
5713 Ibyte *hook_str; | |
5714 Ibyte *err; | |
428 | 5715 |
5716 if (!initialized || preparing_for_armageddon) | |
853 | 5717 /* We need to bail out of here pronto. */ |
428 | 5718 return Qnil; |
5719 | |
853 | 5720 GCPRO1_ARRAY (args, nargs); |
5721 | |
5722 sym = args[0]; | |
5723 val = symbol_value_in_buffer (sym, wrap_buffer (buf)); | |
5724 ret = (cond == RUN_HOOKS_UNTIL_FAILURE ? Qt : Qnil); | |
5725 | |
5726 if (UNBOUNDP (val) || NILP (val)) | |
5727 RETURN_UNGCPRO (ret); | |
5728 | |
5729 diversity_and_distrust.buf = buf; | |
5730 diversity_and_distrust.nargs = nargs; | |
5731 diversity_and_distrust.args = args; | |
5732 diversity_and_distrust.cond = cond; | |
5733 | |
1333 | 5734 hook_name = XSYMBOL_NAME (args[0]); |
5735 hook_str = XSTRING_DATA (hook_name); | |
5736 err = alloca_ibytes (XSTRING_LENGTH (hook_name) + 100); | |
5737 qxesprintf (err, "Error in `%s'", hook_str); | |
853 | 5738 RETURN_UNGCPRO |
5739 (call_trapping_problems | |
1333 | 5740 (warning_class, (CIbyte *) err, flags, 0, |
853 | 5741 run_hook_with_args_in_buffer_trapping_problems_1, |
5742 &diversity_and_distrust)); | |
428 | 5743 } |
5744 | |
5745 Lisp_Object | |
1333 | 5746 run_hook_with_args_trapping_problems (Lisp_Object warning_class, |
853 | 5747 int nargs, |
5748 Lisp_Object *args, | |
5749 enum run_hooks_condition cond, | |
5750 int flags) | |
5751 { | |
5752 return run_hook_with_args_in_buffer_trapping_problems | |
1333 | 5753 (warning_class, current_buffer, nargs, args, cond, flags); |
428 | 5754 } |
5755 | |
5756 Lisp_Object | |
1333 | 5757 va_run_hook_with_args_trapping_problems (Lisp_Object warning_class, |
853 | 5758 Lisp_Object hook_var, |
5759 int nargs, ...) | |
5760 { | |
5761 /* This function can GC */ | |
5762 struct gcpro gcpro1; | |
5763 int i; | |
5764 va_list vargs; | |
5765 Lisp_Object *funcall_args = alloca_array (Lisp_Object, 1 + nargs); | |
5766 int flags; | |
5767 | |
5768 va_start (vargs, nargs); | |
5769 funcall_args[0] = hook_var; | |
5770 for (i = 0; i < nargs; i++) | |
5771 funcall_args[i + 1] = va_arg (vargs, Lisp_Object); | |
5772 flags = va_arg (vargs, int); | |
5773 va_end (vargs); | |
5774 | |
5775 GCPRO1_ARRAY (funcall_args, nargs + 1); | |
5776 RETURN_UNGCPRO (run_hook_with_args_in_buffer_trapping_problems | |
1333 | 5777 (warning_class, current_buffer, nargs + 1, funcall_args, |
853 | 5778 RUN_HOOKS_TO_COMPLETION, flags)); |
428 | 5779 } |
5780 | |
5781 Lisp_Object | |
1333 | 5782 va_run_hook_with_args_in_buffer_trapping_problems (Lisp_Object warning_class, |
853 | 5783 struct buffer *buf, |
5784 Lisp_Object hook_var, | |
5785 int nargs, ...) | |
5786 { | |
5787 /* This function can GC */ | |
5788 struct gcpro gcpro1; | |
5789 int i; | |
5790 va_list vargs; | |
5791 Lisp_Object *funcall_args = alloca_array (Lisp_Object, 1 + nargs); | |
5792 int flags; | |
5793 | |
5794 va_start (vargs, nargs); | |
5795 funcall_args[0] = hook_var; | |
5796 for (i = 0; i < nargs; i++) | |
5797 funcall_args[i + 1] = va_arg (vargs, Lisp_Object); | |
5798 flags = va_arg (vargs, int); | |
5799 va_end (vargs); | |
5800 | |
5801 GCPRO1_ARRAY (funcall_args, nargs + 1); | |
5802 RETURN_UNGCPRO (run_hook_with_args_in_buffer_trapping_problems | |
1333 | 5803 (warning_class, buf, nargs + 1, funcall_args, |
853 | 5804 RUN_HOOKS_TO_COMPLETION, flags)); |
428 | 5805 } |
5806 | |
5807 | |
5808 /************************************************************************/ | |
5809 /* The special binding stack */ | |
771 | 5810 /* Most C code should simply use specbind() and unbind_to_1(). */ |
428 | 5811 /* When performance is critical, use the macros in backtrace.h. */ |
5812 /************************************************************************/ | |
5813 | |
5814 #define min_max_specpdl_size 400 | |
5815 | |
5816 void | |
647 | 5817 grow_specpdl (EMACS_INT reserved) |
5818 { | |
5819 EMACS_INT size_needed = specpdl_depth() + reserved; | |
428 | 5820 if (size_needed >= max_specpdl_size) |
5821 { | |
5822 if (max_specpdl_size < min_max_specpdl_size) | |
5823 max_specpdl_size = min_max_specpdl_size; | |
5824 if (size_needed >= max_specpdl_size) | |
5825 { | |
1951 | 5826 /* Leave room for some specpdl in the debugger. */ |
5827 max_specpdl_size = size_needed + 100; | |
5828 if (max_specpdl_size > specpdl_size) | |
5829 { | |
5830 specpdl_size = max_specpdl_size; | |
5831 XREALLOC_ARRAY (specpdl, struct specbinding, specpdl_size); | |
5832 specpdl_ptr = specpdl + specpdl_depth(); | |
5833 } | |
563 | 5834 signal_continuable_error |
5835 (Qstack_overflow, | |
5836 "Variable binding depth exceeds max-specpdl-size", Qunbound); | |
428 | 5837 } |
5838 } | |
5839 while (specpdl_size < size_needed) | |
5840 { | |
5841 specpdl_size *= 2; | |
5842 if (specpdl_size > max_specpdl_size) | |
5843 specpdl_size = max_specpdl_size; | |
5844 } | |
5845 XREALLOC_ARRAY (specpdl, struct specbinding, specpdl_size); | |
5846 specpdl_ptr = specpdl + specpdl_depth(); | |
853 | 5847 check_specbind_stack_sanity (); |
428 | 5848 } |
5849 | |
5850 | |
5851 /* Handle unbinding buffer-local variables */ | |
5852 static Lisp_Object | |
5853 specbind_unwind_local (Lisp_Object ovalue) | |
5854 { | |
5855 Lisp_Object current = Fcurrent_buffer (); | |
5856 Lisp_Object symbol = specpdl_ptr->symbol; | |
853 | 5857 Lisp_Object victim = ovalue; |
5858 Lisp_Object buf = get_buffer (XCAR (victim), 0); | |
5859 ovalue = XCDR (victim); | |
428 | 5860 |
5861 free_cons (victim); | |
5862 | |
5863 if (NILP (buf)) | |
5864 { | |
5865 /* Deleted buffer -- do nothing */ | |
5866 } | |
5867 else if (symbol_value_buffer_local_info (symbol, XBUFFER (buf)) == 0) | |
5868 { | |
5869 /* Was buffer-local when binding was made, now no longer is. | |
5870 * (kill-local-variable can do this.) | |
5871 * Do nothing in this case. | |
5872 */ | |
5873 } | |
5874 else if (EQ (buf, current)) | |
5875 Fset (symbol, ovalue); | |
5876 else | |
5877 { | |
5878 /* Urk! Somebody switched buffers */ | |
5879 struct gcpro gcpro1; | |
5880 GCPRO1 (current); | |
5881 Fset_buffer (buf); | |
5882 Fset (symbol, ovalue); | |
5883 Fset_buffer (current); | |
5884 UNGCPRO; | |
5885 } | |
5886 return symbol; | |
5887 } | |
5888 | |
5889 static Lisp_Object | |
5890 specbind_unwind_wasnt_local (Lisp_Object buffer) | |
5891 { | |
5892 Lisp_Object current = Fcurrent_buffer (); | |
5893 Lisp_Object symbol = specpdl_ptr->symbol; | |
5894 | |
5895 buffer = get_buffer (buffer, 0); | |
5896 if (NILP (buffer)) | |
5897 { | |
5898 /* Deleted buffer -- do nothing */ | |
5899 } | |
5900 else if (symbol_value_buffer_local_info (symbol, XBUFFER (buffer)) == 0) | |
5901 { | |
5902 /* Was buffer-local when binding was made, now no longer is. | |
5903 * (kill-local-variable can do this.) | |
5904 * Do nothing in this case. | |
5905 */ | |
5906 } | |
5907 else if (EQ (buffer, current)) | |
5908 Fkill_local_variable (symbol); | |
5909 else | |
5910 { | |
5911 /* Urk! Somebody switched buffers */ | |
5912 struct gcpro gcpro1; | |
5913 GCPRO1 (current); | |
5914 Fset_buffer (buffer); | |
5915 Fkill_local_variable (symbol); | |
5916 Fset_buffer (current); | |
5917 UNGCPRO; | |
5918 } | |
5919 return symbol; | |
5920 } | |
5921 | |
5922 | |
5923 void | |
5924 specbind (Lisp_Object symbol, Lisp_Object value) | |
5925 { | |
5926 SPECBIND (symbol, value); | |
853 | 5927 |
5928 check_specbind_stack_sanity (); | |
428 | 5929 } |
5930 | |
5931 void | |
5932 specbind_magic (Lisp_Object symbol, Lisp_Object value) | |
5933 { | |
5934 int buffer_local = | |
5935 symbol_value_buffer_local_info (symbol, current_buffer); | |
5936 | |
5937 if (buffer_local == 0) | |
5938 { | |
5939 specpdl_ptr->old_value = find_symbol_value (symbol); | |
771 | 5940 specpdl_ptr->func = 0; /* Handled specially by unbind_to_1 */ |
428 | 5941 } |
5942 else if (buffer_local > 0) | |
5943 { | |
5944 /* Already buffer-local */ | |
5945 specpdl_ptr->old_value = noseeum_cons (Fcurrent_buffer (), | |
5946 find_symbol_value (symbol)); | |
5947 specpdl_ptr->func = specbind_unwind_local; | |
5948 } | |
5949 else | |
5950 { | |
5951 /* About to become buffer-local */ | |
5952 specpdl_ptr->old_value = Fcurrent_buffer (); | |
5953 specpdl_ptr->func = specbind_unwind_wasnt_local; | |
5954 } | |
5955 | |
5956 specpdl_ptr->symbol = symbol; | |
5957 specpdl_ptr++; | |
5958 specpdl_depth_counter++; | |
5959 | |
5960 Fset (symbol, value); | |
853 | 5961 |
5962 check_specbind_stack_sanity (); | |
428 | 5963 } |
5964 | |
771 | 5965 /* Record an unwind-protect -- FUNCTION will be called with ARG no matter |
5966 whether a normal or non-local exit occurs. (You need to call unbind_to_1() | |
5967 before your function returns normally, passing in the integer returned | |
5968 by this function.) Note: As long as the unwind-protect exists, ARG is | |
5969 automatically GCPRO'd. The return value from FUNCTION is completely | |
5970 ignored. #### We should eliminate it entirely. */ | |
5971 | |
5972 int | |
428 | 5973 record_unwind_protect (Lisp_Object (*function) (Lisp_Object arg), |
5974 Lisp_Object arg) | |
5975 { | |
5976 SPECPDL_RESERVE (1); | |
5977 specpdl_ptr->func = function; | |
5978 specpdl_ptr->symbol = Qnil; | |
5979 specpdl_ptr->old_value = arg; | |
5980 specpdl_ptr++; | |
5981 specpdl_depth_counter++; | |
853 | 5982 check_specbind_stack_sanity (); |
771 | 5983 return specpdl_depth_counter - 1; |
5984 } | |
5985 | |
5986 static Lisp_Object | |
802 | 5987 restore_lisp_object (Lisp_Object cons) |
5988 { | |
5989 Lisp_Object opaque = XCAR (cons); | |
5990 Lisp_Object *addr = (Lisp_Object *) get_opaque_ptr (opaque); | |
5991 *addr = XCDR (cons); | |
5992 free_opaque_ptr (opaque); | |
853 | 5993 free_cons (cons); |
802 | 5994 return Qnil; |
5995 } | |
5996 | |
5997 /* Establish an unwind-protect which will restore the Lisp_Object pointed to | |
5998 by ADDR with the value VAL. */ | |
814 | 5999 static int |
802 | 6000 record_unwind_protect_restoring_lisp_object (Lisp_Object *addr, |
6001 Lisp_Object val) | |
6002 { | |
6003 Lisp_Object opaque = make_opaque_ptr (addr); | |
6004 return record_unwind_protect (restore_lisp_object, | |
6005 noseeum_cons (opaque, val)); | |
6006 } | |
6007 | |
6008 /* Similar to specbind() but for any C variable whose value is a | |
6009 Lisp_Object. Sets up an unwind-protect to restore the variable | |
6010 pointed to by ADDR to its existing value, and then changes its | |
6011 value to NEWVAL. Returns the previous value of specpdl_depth(); | |
6012 pass this to unbind_to() after you are done. */ | |
6013 int | |
6014 internal_bind_lisp_object (Lisp_Object *addr, Lisp_Object newval) | |
6015 { | |
6016 int count = specpdl_depth (); | |
6017 record_unwind_protect_restoring_lisp_object (addr, *addr); | |
6018 *addr = newval; | |
6019 return count; | |
6020 } | |
6021 | |
6022 static Lisp_Object | |
6023 restore_int (Lisp_Object cons) | |
6024 { | |
6025 Lisp_Object opaque = XCAR (cons); | |
6026 Lisp_Object lval = XCDR (cons); | |
6027 int *addr = (int *) get_opaque_ptr (opaque); | |
6028 int val; | |
6029 | |
4025 | 6030 /* In the event that a C integer will always fit in an Emacs int, we |
6031 haven't ever stored a C integer as an opaque pointer. This #ifdef | |
6032 eliminates a warning on AMD 64, where EMACS_INT has 63 value bits and C | |
6033 integers have 32 value bits. */ | |
6034 #if INT_VALBITS < INTBITS | |
802 | 6035 if (INTP (lval)) |
4025 | 6036 { |
6037 val = XINT (lval); | |
6038 } | |
802 | 6039 else |
6040 { | |
6041 val = (int) get_opaque_ptr (lval); | |
6042 free_opaque_ptr (lval); | |
6043 } | |
4025 | 6044 #else /* !(INT_VALBITS < INTBITS) */ |
6045 val = XINT(lval); | |
6046 #endif /* INT_VALBITS < INTBITS */ | |
802 | 6047 |
6048 *addr = val; | |
6049 free_opaque_ptr (opaque); | |
853 | 6050 free_cons (cons); |
802 | 6051 return Qnil; |
6052 } | |
6053 | |
6054 /* Establish an unwind-protect which will restore the int pointed to | |
6055 by ADDR with the value VAL. This function works correctly with | |
6056 all ints, even those that don't fit into a Lisp integer. */ | |
1333 | 6057 int |
802 | 6058 record_unwind_protect_restoring_int (int *addr, int val) |
6059 { | |
6060 Lisp_Object opaque = make_opaque_ptr (addr); | |
6061 Lisp_Object lval; | |
6062 | |
4025 | 6063 /* In the event that a C integer will always fit in an Emacs int, we don't |
6064 ever want to store a C integer as an opaque pointer. This #ifdef | |
6065 eliminates a warning on AMD 64, where EMACS_INT has 63 value bits and C | |
6066 integers have 32 value bits. */ | |
6067 #if INT_VALBITS <= INTBITS | |
802 | 6068 if (NUMBER_FITS_IN_AN_EMACS_INT (val)) |
6069 lval = make_int (val); | |
6070 else | |
6071 lval = make_opaque_ptr ((void *) val); | |
4025 | 6072 #else /* !(INT_VALBITS < INTBITS) */ |
6073 lval = make_int (val); | |
6074 #endif /* INT_VALBITS <= INTBITS */ | |
6075 | |
802 | 6076 return record_unwind_protect (restore_int, noseeum_cons (opaque, lval)); |
6077 } | |
6078 | |
6079 /* Similar to specbind() but for any C variable whose value is an int. | |
6080 Sets up an unwind-protect to restore the variable pointed to by | |
6081 ADDR to its existing value, and then changes its value to NEWVAL. | |
6082 Returns the previous value of specpdl_depth(); pass this to | |
6083 unbind_to() after you are done. This function works correctly with | |
6084 all ints, even those that don't fit into a Lisp integer. */ | |
6085 int | |
6086 internal_bind_int (int *addr, int newval) | |
6087 { | |
6088 int count = specpdl_depth (); | |
6089 record_unwind_protect_restoring_int (addr, *addr); | |
6090 *addr = newval; | |
6091 return count; | |
6092 } | |
6093 | |
6094 static Lisp_Object | |
771 | 6095 free_pointer (Lisp_Object opaque) |
6096 { | |
1726 | 6097 xfree (get_opaque_ptr (opaque), void *); |
771 | 6098 free_opaque_ptr (opaque); |
6099 return Qnil; | |
6100 } | |
6101 | |
6102 /* Establish an unwind-protect which will free the specified block. | |
6103 */ | |
6104 int | |
6105 record_unwind_protect_freeing (void *ptr) | |
6106 { | |
6107 Lisp_Object opaque = make_opaque_ptr (ptr); | |
6108 return record_unwind_protect (free_pointer, opaque); | |
6109 } | |
6110 | |
6111 static Lisp_Object | |
6112 free_dynarr (Lisp_Object opaque) | |
6113 { | |
6114 Dynarr_free (get_opaque_ptr (opaque)); | |
6115 free_opaque_ptr (opaque); | |
6116 return Qnil; | |
6117 } | |
6118 | |
6119 int | |
6120 record_unwind_protect_freeing_dynarr (void *ptr) | |
6121 { | |
6122 Lisp_Object opaque = make_opaque_ptr (ptr); | |
6123 return record_unwind_protect (free_dynarr, opaque); | |
6124 } | |
428 | 6125 |
6126 /* Unwind the stack till specpdl_depth() == COUNT. | |
6127 VALUE is not used, except that, purely as a convenience to the | |
771 | 6128 caller, it is protected from garbage-protection and returned. */ |
428 | 6129 Lisp_Object |
771 | 6130 unbind_to_1 (int count, Lisp_Object value) |
428 | 6131 { |
6132 UNBIND_TO_GCPRO (count, value); | |
853 | 6133 check_specbind_stack_sanity (); |
428 | 6134 return value; |
6135 } | |
6136 | |
6137 /* Don't call this directly. | |
6138 Only for use by UNBIND_TO* macros in backtrace.h */ | |
6139 void | |
6140 unbind_to_hairy (int count) | |
6141 { | |
442 | 6142 ++specpdl_ptr; |
6143 ++specpdl_depth_counter; | |
6144 | |
428 | 6145 while (specpdl_depth_counter != count) |
6146 { | |
1313 | 6147 Lisp_Object oquit = Qunbound; |
6148 | |
6149 /* Do this check BEFORE decrementing the values below, because once | |
6150 they're decremented, GC protection is lost on | |
6151 specpdl_ptr->old_value. */ | |
1322 | 6152 if (specpdl_ptr[-1].func == Fprogn) |
1313 | 6153 { |
6154 /* Allow QUIT within unwind-protect routines, but defer any | |
6155 existing QUIT until afterwards. Only do this, however, for | |
6156 unwind-protects established by Lisp code, not by C code | |
6157 (e.g. free_opaque_ptr() or something), because the act of | |
6158 checking for QUIT can cause all sorts of weird things to | |
6159 happen, since it churns the event loop -- redisplay, running | |
6160 Lisp, etc. Code should not have to worry about this just | |
6161 because of establishing an unwind-protect. */ | |
6162 check_quit (); /* make Vquit_flag accurate */ | |
6163 oquit = Vquit_flag; | |
6164 Vquit_flag = Qnil; | |
6165 } | |
6166 | |
428 | 6167 --specpdl_ptr; |
6168 --specpdl_depth_counter; | |
6169 | |
1313 | 6170 /* #### At this point, there is no GC protection on old_value. This |
6171 could be a real problem, depending on what unwind-protect function | |
6172 is called. It looks like it just so happens that the ones | |
6173 actually called don't have a problem with this, e.g. Fprogn. But | |
6174 we should look into fixing this. (Many unwind-protect functions | |
6175 free values. Is it a problem if freed values are | |
6176 GC-protected?) */ | |
428 | 6177 if (specpdl_ptr->func != 0) |
1313 | 6178 { |
6179 /* An unwind-protect */ | |
6180 (*specpdl_ptr->func) (specpdl_ptr->old_value); | |
6181 } | |
6182 | |
428 | 6183 else |
6184 { | |
6185 /* We checked symbol for validity when we specbound it, | |
6186 so only need to call Fset if symbol has magic value. */ | |
440 | 6187 Lisp_Symbol *sym = XSYMBOL (specpdl_ptr->symbol); |
428 | 6188 if (!SYMBOL_VALUE_MAGIC_P (sym->value)) |
6189 sym->value = specpdl_ptr->old_value; | |
6190 else | |
6191 Fset (specpdl_ptr->symbol, specpdl_ptr->old_value); | |
6192 } | |
6193 | |
6194 #if 0 /* martin */ | |
6195 #ifndef EXCEEDINGLY_QUESTIONABLE_CODE | |
6196 /* There should never be anything here for us to remove. | |
6197 If so, it indicates a logic error in Emacs. Catches | |
6198 should get removed when a throw or signal occurs, or | |
6199 when a catch or condition-case exits normally. But | |
6200 it's too dangerous to just remove this code. --ben */ | |
6201 | |
6202 /* Furthermore, this code is not in FSFmacs!!! | |
6203 Braino on mly's part? */ | |
6204 /* If we're unwound past the pdlcount of a catch frame, | |
6205 that catch can't possibly still be valid. */ | |
6206 while (catchlist && catchlist->pdlcount > specpdl_depth_counter) | |
6207 { | |
6208 catchlist = catchlist->next; | |
6209 /* Don't mess with gcprolist, backtrace_list here */ | |
6210 } | |
6211 #endif | |
6212 #endif | |
1313 | 6213 |
6214 if (!UNBOUNDP (oquit)) | |
6215 Vquit_flag = oquit; | |
428 | 6216 } |
853 | 6217 check_specbind_stack_sanity (); |
428 | 6218 } |
6219 | |
6220 | |
6221 | |
6222 /* Get the value of symbol's global binding, even if that binding is | |
6223 not now dynamically visible. May return Qunbound or magic values. */ | |
6224 | |
6225 Lisp_Object | |
6226 top_level_value (Lisp_Object symbol) | |
6227 { | |
6228 REGISTER struct specbinding *ptr = specpdl; | |
6229 | |
6230 CHECK_SYMBOL (symbol); | |
6231 for (; ptr != specpdl_ptr; ptr++) | |
6232 { | |
6233 if (EQ (ptr->symbol, symbol)) | |
6234 return ptr->old_value; | |
6235 } | |
6236 return XSYMBOL (symbol)->value; | |
6237 } | |
6238 | |
6239 #if 0 | |
6240 | |
6241 Lisp_Object | |
6242 top_level_set (Lisp_Object symbol, Lisp_Object newval) | |
6243 { | |
6244 REGISTER struct specbinding *ptr = specpdl; | |
6245 | |
6246 CHECK_SYMBOL (symbol); | |
6247 for (; ptr != specpdl_ptr; ptr++) | |
6248 { | |
6249 if (EQ (ptr->symbol, symbol)) | |
6250 { | |
6251 ptr->old_value = newval; | |
6252 return newval; | |
6253 } | |
6254 } | |
6255 return Fset (symbol, newval); | |
6256 } | |
6257 | |
6258 #endif /* 0 */ | |
6259 | |
6260 | |
6261 /************************************************************************/ | |
6262 /* Backtraces */ | |
6263 /************************************************************************/ | |
6264 | |
6265 DEFUN ("backtrace-debug", Fbacktrace_debug, 2, 2, 0, /* | |
6266 Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG. | |
6267 The debugger is entered when that frame exits, if the flag is non-nil. | |
6268 */ | |
6269 (level, flag)) | |
6270 { | |
6271 REGISTER struct backtrace *backlist = backtrace_list; | |
6272 REGISTER int i; | |
6273 | |
6274 CHECK_INT (level); | |
6275 | |
6276 for (i = 0; backlist && i < XINT (level); i++) | |
6277 { | |
6278 backlist = backlist->next; | |
6279 } | |
6280 | |
6281 if (backlist) | |
6282 backlist->debug_on_exit = !NILP (flag); | |
6283 | |
6284 return flag; | |
6285 } | |
6286 | |
6287 static void | |
6288 backtrace_specials (int speccount, int speclimit, Lisp_Object stream) | |
6289 { | |
6290 int printing_bindings = 0; | |
6291 | |
6292 for (; speccount > speclimit; speccount--) | |
6293 { | |
6294 if (specpdl[speccount - 1].func == 0 | |
6295 || specpdl[speccount - 1].func == specbind_unwind_local | |
6296 || specpdl[speccount - 1].func == specbind_unwind_wasnt_local) | |
6297 { | |
826 | 6298 write_c_string (stream, !printing_bindings ? " # bind (" : " "); |
428 | 6299 Fprin1 (specpdl[speccount - 1].symbol, stream); |
6300 printing_bindings = 1; | |
6301 } | |
6302 else | |
6303 { | |
826 | 6304 if (printing_bindings) write_c_string (stream, ")\n"); |
6305 write_c_string (stream, " # (unwind-protect ...)\n"); | |
428 | 6306 printing_bindings = 0; |
6307 } | |
6308 } | |
826 | 6309 if (printing_bindings) write_c_string (stream, ")\n"); |
428 | 6310 } |
6311 | |
1292 | 6312 static Lisp_Object |
6313 backtrace_unevalled_args (Lisp_Object *args) | |
6314 { | |
6315 if (args) | |
6316 return *args; | |
6317 else | |
6318 return list1 (build_string ("[internal]")); | |
6319 } | |
6320 | |
428 | 6321 DEFUN ("backtrace", Fbacktrace, 0, 2, "", /* |
6322 Print a trace of Lisp function calls currently active. | |
438 | 6323 Optional arg STREAM specifies the output stream to send the backtrace to, |
444 | 6324 and defaults to the value of `standard-output'. |
6325 Optional second arg DETAILED non-nil means show places where currently | |
6326 active variable bindings, catches, condition-cases, and | |
6327 unwind-protects, as well as function calls, were made. | |
428 | 6328 */ |
6329 (stream, detailed)) | |
6330 { | |
6331 /* This function can GC */ | |
6332 struct backtrace *backlist = backtrace_list; | |
6333 struct catchtag *catches = catchlist; | |
6334 int speccount = specpdl_depth(); | |
6335 | |
6336 int old_nl = print_escape_newlines; | |
6337 int old_pr = print_readably; | |
6338 Lisp_Object old_level = Vprint_level; | |
6339 Lisp_Object oiq = Vinhibit_quit; | |
6340 struct gcpro gcpro1, gcpro2; | |
6341 | |
6342 /* We can't allow quits in here because that could cause the values | |
6343 of print_readably and print_escape_newlines to get screwed up. | |
6344 Normally we would use a record_unwind_protect but that would | |
6345 screw up the functioning of this function. */ | |
6346 Vinhibit_quit = Qt; | |
6347 | |
6348 entering_debugger = 0; | |
6349 | |
872 | 6350 if (!NILP (detailed)) |
6351 Vprint_level = make_int (50); | |
6352 else | |
6353 Vprint_level = make_int (3); | |
428 | 6354 print_readably = 0; |
6355 print_escape_newlines = 1; | |
6356 | |
6357 GCPRO2 (stream, old_level); | |
6358 | |
1261 | 6359 stream = canonicalize_printcharfun (stream); |
428 | 6360 |
6361 for (;;) | |
6362 { | |
6363 if (!NILP (detailed) && catches && catches->backlist == backlist) | |
6364 { | |
6365 int catchpdl = catches->pdlcount; | |
438 | 6366 if (speccount > catchpdl |
6367 && specpdl[catchpdl].func == condition_case_unwind) | |
428 | 6368 /* This is a condition-case catchpoint */ |
6369 catchpdl = catchpdl + 1; | |
6370 | |
6371 backtrace_specials (speccount, catchpdl, stream); | |
6372 | |
6373 speccount = catches->pdlcount; | |
6374 if (catchpdl == speccount) | |
6375 { | |
826 | 6376 write_c_string (stream, " # (catch "); |
428 | 6377 Fprin1 (catches->tag, stream); |
826 | 6378 write_c_string (stream, " ...)\n"); |
428 | 6379 } |
6380 else | |
6381 { | |
826 | 6382 write_c_string (stream, " # (condition-case ... . "); |
428 | 6383 Fprin1 (Fcdr (Fcar (catches->tag)), stream); |
826 | 6384 write_c_string (stream, ")\n"); |
428 | 6385 } |
6386 catches = catches->next; | |
6387 } | |
6388 else if (!backlist) | |
6389 break; | |
6390 else | |
6391 { | |
6392 if (!NILP (detailed) && backlist->pdlcount < speccount) | |
6393 { | |
6394 backtrace_specials (speccount, backlist->pdlcount, stream); | |
6395 speccount = backlist->pdlcount; | |
6396 } | |
826 | 6397 write_c_string (stream, backlist->debug_on_exit ? "* " : " "); |
428 | 6398 if (backlist->nargs == UNEVALLED) |
6399 { | |
1292 | 6400 Fprin1 (Fcons (*backlist->function, |
6401 backtrace_unevalled_args (backlist->args)), | |
6402 stream); | |
826 | 6403 write_c_string (stream, "\n"); /* from FSFmacs 19.30 */ |
428 | 6404 } |
6405 else | |
6406 { | |
6407 Lisp_Object tem = *backlist->function; | |
6408 Fprin1 (tem, stream); /* This can QUIT */ | |
826 | 6409 write_c_string (stream, "("); |
428 | 6410 if (backlist->nargs == MANY) |
6411 { | |
6412 int i; | |
6413 Lisp_Object tail = Qnil; | |
6414 struct gcpro ngcpro1; | |
6415 | |
6416 NGCPRO1 (tail); | |
6417 for (tail = *backlist->args, i = 0; | |
6418 !NILP (tail); | |
6419 tail = Fcdr (tail), i++) | |
6420 { | |
826 | 6421 if (i != 0) write_c_string (stream, " "); |
428 | 6422 Fprin1 (Fcar (tail), stream); |
6423 } | |
6424 NUNGCPRO; | |
6425 } | |
6426 else | |
6427 { | |
6428 int i; | |
6429 for (i = 0; i < backlist->nargs; i++) | |
6430 { | |
826 | 6431 if (!i && EQ (tem, Qbyte_code)) |
6432 { | |
6433 write_c_string (stream, "\"...\""); | |
6434 continue; | |
6435 } | |
6436 if (i != 0) write_c_string (stream, " "); | |
428 | 6437 Fprin1 (backlist->args[i], stream); |
6438 } | |
6439 } | |
826 | 6440 write_c_string (stream, ")\n"); |
428 | 6441 } |
6442 backlist = backlist->next; | |
6443 } | |
6444 } | |
6445 Vprint_level = old_level; | |
6446 print_readably = old_pr; | |
6447 print_escape_newlines = old_nl; | |
6448 UNGCPRO; | |
6449 Vinhibit_quit = oiq; | |
6450 return Qnil; | |
6451 } | |
6452 | |
6453 | |
444 | 6454 DEFUN ("backtrace-frame", Fbacktrace_frame, 1, 1, 0, /* |
6455 Return the function and arguments NFRAMES up from current execution point. | |
428 | 6456 If that frame has not evaluated the arguments yet (or is a special form), |
6457 the value is (nil FUNCTION ARG-FORMS...). | |
6458 If that frame has evaluated its arguments and called its function already, | |
6459 the value is (t FUNCTION ARG-VALUES...). | |
6460 A &rest arg is represented as the tail of the list ARG-VALUES. | |
6461 FUNCTION is whatever was supplied as car of evaluated list, | |
6462 or a lambda expression for macro calls. | |
444 | 6463 If NFRAMES is more than the number of frames, the value is nil. |
428 | 6464 */ |
6465 (nframes)) | |
6466 { | |
6467 REGISTER struct backtrace *backlist = backtrace_list; | |
6468 REGISTER int i; | |
6469 Lisp_Object tem; | |
6470 | |
6471 CHECK_NATNUM (nframes); | |
6472 | |
6473 /* Find the frame requested. */ | |
6474 for (i = XINT (nframes); backlist && (i-- > 0);) | |
6475 backlist = backlist->next; | |
6476 | |
6477 if (!backlist) | |
6478 return Qnil; | |
6479 if (backlist->nargs == UNEVALLED) | |
1292 | 6480 return Fcons (Qnil, Fcons (*backlist->function, |
6481 backtrace_unevalled_args (backlist->args))); | |
428 | 6482 else |
6483 { | |
6484 if (backlist->nargs == MANY) | |
6485 tem = *backlist->args; | |
6486 else | |
6487 tem = Flist (backlist->nargs, backlist->args); | |
6488 | |
6489 return Fcons (Qt, Fcons (*backlist->function, tem)); | |
6490 } | |
6491 } | |
6492 | |
6493 | |
6494 /************************************************************************/ | |
6495 /* Warnings */ | |
6496 /************************************************************************/ | |
6497 | |
1123 | 6498 static int |
6499 warning_will_be_discarded (Lisp_Object level) | |
6500 { | |
6501 /* Don't even generate debug warnings if they're going to be discarded, | |
6502 to avoid excessive consing. */ | |
6503 return (EQ (level, Qdebug) && !NILP (Vlog_warning_minimum_level) && | |
6504 !EQ (Vlog_warning_minimum_level, Qdebug)); | |
6505 } | |
6506 | |
428 | 6507 void |
1204 | 6508 warn_when_safe_lispobj (Lisp_Object class_, Lisp_Object level, |
428 | 6509 Lisp_Object obj) |
6510 { | |
1123 | 6511 if (warning_will_be_discarded (level)) |
793 | 6512 return; |
1123 | 6513 |
1204 | 6514 obj = list1 (list3 (class_, level, obj)); |
428 | 6515 if (NILP (Vpending_warnings)) |
6516 Vpending_warnings = Vpending_warnings_tail = obj; | |
6517 else | |
6518 { | |
6519 Fsetcdr (Vpending_warnings_tail, obj); | |
6520 Vpending_warnings_tail = obj; | |
6521 } | |
6522 } | |
6523 | |
6524 /* #### This should probably accept Lisp objects; but then we have | |
6525 to make sure that Feval() isn't called, since it might not be safe. | |
6526 | |
6527 An alternative approach is to just pass some non-string type of | |
6528 Lisp_Object to warn_when_safe_lispobj(); `prin1-to-string' will | |
6529 automatically be called when it is safe to do so. */ | |
6530 | |
6531 void | |
1204 | 6532 warn_when_safe (Lisp_Object class_, Lisp_Object level, const CIbyte *fmt, ...) |
428 | 6533 { |
6534 Lisp_Object obj; | |
6535 va_list args; | |
6536 | |
1123 | 6537 if (warning_will_be_discarded (level)) |
793 | 6538 return; |
1123 | 6539 |
428 | 6540 va_start (args, fmt); |
771 | 6541 obj = emacs_vsprintf_string (CGETTEXT (fmt), args); |
428 | 6542 va_end (args); |
6543 | |
1204 | 6544 warn_when_safe_lispobj (class_, level, obj); |
428 | 6545 } |
6546 | |
6547 | |
6548 | |
6549 | |
6550 /************************************************************************/ | |
6551 /* Initialization */ | |
6552 /************************************************************************/ | |
6553 | |
6554 void | |
6555 syms_of_eval (void) | |
6556 { | |
442 | 6557 INIT_LRECORD_IMPLEMENTATION (subr); |
6558 | |
563 | 6559 DEFSYMBOL (Qinhibit_quit); |
6560 DEFSYMBOL (Qautoload); | |
6561 DEFSYMBOL (Qdebug_on_error); | |
6562 DEFSYMBOL (Qstack_trace_on_error); | |
6563 DEFSYMBOL (Qdebug_on_signal); | |
6564 DEFSYMBOL (Qstack_trace_on_signal); | |
6565 DEFSYMBOL (Qdebugger); | |
6566 DEFSYMBOL (Qmacro); | |
428 | 6567 defsymbol (&Qand_rest, "&rest"); |
6568 defsymbol (&Qand_optional, "&optional"); | |
6569 /* Note that the process code also uses Qexit */ | |
563 | 6570 DEFSYMBOL (Qexit); |
6571 DEFSYMBOL (Qsetq); | |
6572 DEFSYMBOL (Qinteractive); | |
6573 DEFSYMBOL (Qcommandp); | |
6574 DEFSYMBOL (Qdefun); | |
6575 DEFSYMBOL (Qprogn); | |
6576 DEFSYMBOL (Qvalues); | |
6577 DEFSYMBOL (Qdisplay_warning); | |
6578 DEFSYMBOL (Qrun_hooks); | |
887 | 6579 DEFSYMBOL (Qfinalize_list); |
563 | 6580 DEFSYMBOL (Qif); |
428 | 6581 |
6582 DEFSUBR (For); | |
6583 DEFSUBR (Fand); | |
6584 DEFSUBR (Fif); | |
6585 DEFSUBR_MACRO (Fwhen); | |
6586 DEFSUBR_MACRO (Funless); | |
6587 DEFSUBR (Fcond); | |
6588 DEFSUBR (Fprogn); | |
6589 DEFSUBR (Fprog1); | |
6590 DEFSUBR (Fprog2); | |
6591 DEFSUBR (Fsetq); | |
6592 DEFSUBR (Fquote); | |
6593 DEFSUBR (Ffunction); | |
6594 DEFSUBR (Fdefun); | |
6595 DEFSUBR (Fdefmacro); | |
6596 DEFSUBR (Fdefvar); | |
6597 DEFSUBR (Fdefconst); | |
6598 DEFSUBR (Flet); | |
6599 DEFSUBR (FletX); | |
6600 DEFSUBR (Fwhile); | |
6601 DEFSUBR (Fmacroexpand_internal); | |
6602 DEFSUBR (Fcatch); | |
6603 DEFSUBR (Fthrow); | |
6604 DEFSUBR (Funwind_protect); | |
6605 DEFSUBR (Fcondition_case); | |
6606 DEFSUBR (Fcall_with_condition_handler); | |
6607 DEFSUBR (Fsignal); | |
6608 DEFSUBR (Finteractive_p); | |
6609 DEFSUBR (Fcommandp); | |
6610 DEFSUBR (Fcommand_execute); | |
6611 DEFSUBR (Fautoload); | |
6612 DEFSUBR (Feval); | |
6613 DEFSUBR (Fapply); | |
6614 DEFSUBR (Ffuncall); | |
6615 DEFSUBR (Ffunctionp); | |
6616 DEFSUBR (Ffunction_min_args); | |
6617 DEFSUBR (Ffunction_max_args); | |
6618 DEFSUBR (Frun_hooks); | |
6619 DEFSUBR (Frun_hook_with_args); | |
6620 DEFSUBR (Frun_hook_with_args_until_success); | |
6621 DEFSUBR (Frun_hook_with_args_until_failure); | |
6622 DEFSUBR (Fbacktrace_debug); | |
6623 DEFSUBR (Fbacktrace); | |
6624 DEFSUBR (Fbacktrace_frame); | |
6625 } | |
6626 | |
6627 void | |
814 | 6628 init_eval_semi_early (void) |
428 | 6629 { |
6630 specpdl_ptr = specpdl; | |
6631 specpdl_depth_counter = 0; | |
6632 catchlist = 0; | |
6633 Vcondition_handlers = Qnil; | |
6634 backtrace_list = 0; | |
6635 Vquit_flag = Qnil; | |
6636 debug_on_next_call = 0; | |
6637 lisp_eval_depth = 0; | |
6638 entering_debugger = 0; | |
6639 } | |
6640 | |
6641 void | |
6642 reinit_vars_of_eval (void) | |
6643 { | |
6644 preparing_for_armageddon = 0; | |
6645 in_warnings = 0; | |
6646 specpdl_size = 50; | |
6647 specpdl = xnew_array (struct specbinding, specpdl_size); | |
6648 /* XEmacs change: increase these values. */ | |
6649 max_specpdl_size = 3000; | |
442 | 6650 max_lisp_eval_depth = 1000; |
6651 #ifdef DEFEND_AGAINST_THROW_RECURSION | |
428 | 6652 throw_level = 0; |
6653 #endif | |
2367 | 6654 init_eval_semi_early (); |
428 | 6655 } |
6656 | |
6657 void | |
6658 vars_of_eval (void) | |
6659 { | |
6660 DEFVAR_INT ("max-specpdl-size", &max_specpdl_size /* | |
6661 Limit on number of Lisp variable bindings & unwind-protects before error. | |
6662 */ ); | |
6663 | |
6664 DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth /* | |
6665 Limit on depth in `eval', `apply' and `funcall' before error. | |
6666 This limit is to catch infinite recursions for you before they cause | |
6667 actual stack overflow in C, which would be fatal for Emacs. | |
6668 You can safely make it considerably larger than its default value, | |
6669 if that proves inconveniently small. | |
6670 */ ); | |
6671 | |
6672 DEFVAR_LISP ("quit-flag", &Vquit_flag /* | |
853 | 6673 t causes running Lisp code to abort, unless `inhibit-quit' is non-nil. |
6674 `critical' causes running Lisp code to abort regardless of `inhibit-quit'. | |
6675 Normally, you do not need to set this value yourself. It is set to | |
6676 t each time a Control-G is detected, and to `critical' each time a | |
6677 Shift-Control-G is detected. The XEmacs core C code is littered with | |
6678 calls to the QUIT; macro, which check the values of `quit-flag' and | |
2500 | 6679 `inhibit-quit' and ABORT (or more accurately, call (signal 'quit)) if |
853 | 6680 it's correct to do so. |
428 | 6681 */ ); |
6682 Vquit_flag = Qnil; | |
6683 | |
6684 DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit /* | |
6685 Non-nil inhibits C-g quitting from happening immediately. | |
6686 Note that `quit-flag' will still be set by typing C-g, | |
6687 so a quit will be signalled as soon as `inhibit-quit' is nil. | |
6688 To prevent this happening, set `quit-flag' to nil | |
853 | 6689 before making `inhibit-quit' nil. |
6690 | |
6691 The value of `inhibit-quit' is ignored if a critical quit is | |
6692 requested by typing control-shift-G in a window-system frame; | |
6693 this is explained in more detail in `quit-flag'. | |
428 | 6694 */ ); |
6695 Vinhibit_quit = Qnil; | |
6696 | |
6697 DEFVAR_LISP ("stack-trace-on-error", &Vstack_trace_on_error /* | |
6698 *Non-nil means automatically display a backtrace buffer | |
6699 after any error that is not handled by a `condition-case'. | |
6700 If the value is a list, an error only means to display a backtrace | |
6701 if one of its condition symbols appears in the list. | |
6702 See also variable `stack-trace-on-signal'. | |
6703 */ ); | |
6704 Vstack_trace_on_error = Qnil; | |
6705 | |
6706 DEFVAR_LISP ("stack-trace-on-signal", &Vstack_trace_on_signal /* | |
6707 *Non-nil means automatically display a backtrace buffer | |
6708 after any error that is signalled, whether or not it is handled by | |
6709 a `condition-case'. | |
6710 If the value is a list, an error only means to display a backtrace | |
6711 if one of its condition symbols appears in the list. | |
6712 See also variable `stack-trace-on-error'. | |
6713 */ ); | |
6714 Vstack_trace_on_signal = Qnil; | |
6715 | |
6716 DEFVAR_LISP ("debug-ignored-errors", &Vdebug_ignored_errors /* | |
6717 *List of errors for which the debugger should not be called. | |
6718 Each element may be a condition-name or a regexp that matches error messages. | |
6719 If any element applies to a given error, that error skips the debugger | |
6720 and just returns to top level. | |
6721 This overrides the variable `debug-on-error'. | |
6722 It does not apply to errors handled by `condition-case'. | |
6723 */ ); | |
6724 Vdebug_ignored_errors = Qnil; | |
6725 | |
6726 DEFVAR_LISP ("debug-on-error", &Vdebug_on_error /* | |
6727 *Non-nil means enter debugger if an unhandled error is signalled. | |
6728 The debugger will not be entered if the error is handled by | |
6729 a `condition-case'. | |
6730 If the value is a list, an error only means to enter the debugger | |
6731 if one of its condition symbols appears in the list. | |
6732 This variable is overridden by `debug-ignored-errors'. | |
6733 See also variables `debug-on-quit' and `debug-on-signal'. | |
1123 | 6734 |
6735 If this variable is set while XEmacs is running noninteractively (using | |
6736 `-batch'), and XEmacs was configured with `--debug' (#define XEMACS_DEBUG | |
6737 in the C code), instead of trying to invoke the Lisp debugger (which | |
6738 obviously won't work), XEmacs will break out to a C debugger using | |
6739 \(force-debugging-signal t). This is useful because debugging | |
6740 noninteractive runs of XEmacs is often very difficult, since they typically | |
6741 happen as part of sometimes large and complex make suites (e.g. rebuilding | |
2500 | 6742 the XEmacs packages). NOTE: This runs ABORT()!!! (As well as and after |
1123 | 6743 executing INT 3 under MS Windows, which should invoke a debugger if it's |
6744 active.) This is guaranteed to kill XEmacs! (But in this situation, XEmacs | |
6745 is about to die anyway, and if no debugger is present, this will usefully | |
6746 dump core.) The most useful way to set this flag when debugging | |
6747 noninteractive runs, especially in makefiles, is using the environment | |
6748 variable XEMACSDEBUG, like this: | |
771 | 6749 |
6750 \(using csh) setenv XEMACSDEBUG '(setq debug-on-error t)' | |
6751 \(using bash) export XEMACSDEBUG='(setq debug-on-error t)' | |
428 | 6752 */ ); |
6753 Vdebug_on_error = Qnil; | |
6754 | |
6755 DEFVAR_LISP ("debug-on-signal", &Vdebug_on_signal /* | |
6756 *Non-nil means enter debugger if an error is signalled. | |
6757 The debugger will be entered whether or not the error is handled by | |
6758 a `condition-case'. | |
6759 If the value is a list, an error only means to enter the debugger | |
6760 if one of its condition symbols appears in the list. | |
6761 See also variable `debug-on-quit'. | |
1123 | 6762 |
6763 This will attempt to enter a C debugger when XEmacs is run noninteractively | |
6764 and under the same conditions as described in `debug-on-error'. | |
428 | 6765 */ ); |
6766 Vdebug_on_signal = Qnil; | |
6767 | |
6768 DEFVAR_BOOL ("debug-on-quit", &debug_on_quit /* | |
6769 *Non-nil means enter debugger if quit is signalled (C-G, for example). | |
6770 Does not apply if quit is handled by a `condition-case'. Entering the | |
6771 debugger can also be achieved at any time (for X11 console) by typing | |
6772 control-shift-G to signal a critical quit. | |
6773 */ ); | |
6774 debug_on_quit = 0; | |
6775 | |
6776 DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call /* | |
6777 Non-nil means enter debugger before next `eval', `apply' or `funcall'. | |
6778 */ ); | |
6779 | |
1292 | 6780 DEFVAR_BOOL ("backtrace-with-interal-sections", |
6781 &backtrace_with_internal_sections /* | |
6782 Non-nil means backtraces will contain additional information indicating | |
6783 when particular sections of the C code have been entered, e.g. redisplay(), | |
6784 byte-char conversion, internal-external conversion, etc. This can be | |
6785 particularly useful when XEmacs crashes, in helping to pinpoint the problem. | |
6786 */ ); | |
6787 #ifdef ERROR_CHECK_STRUCTURES | |
6788 backtrace_with_internal_sections = 1; | |
6789 #else | |
6790 backtrace_with_internal_sections = 0; | |
6791 #endif | |
6792 | |
428 | 6793 DEFVAR_LISP ("debugger", &Vdebugger /* |
6794 Function to call to invoke debugger. | |
6795 If due to frame exit, args are `exit' and the value being returned; | |
6796 this function's value will be returned instead of that. | |
6797 If due to error, args are `error' and a list of the args to `signal'. | |
6798 If due to `apply' or `funcall' entry, one arg, `lambda'. | |
6799 If due to `eval' entry, one arg, t. | |
6800 */ ); | |
6801 Vdebugger = Qnil; | |
6802 | |
853 | 6803 staticpro (&Vcatch_everything_tag); |
6804 Vcatch_everything_tag = make_opaque (OPAQUE_CLEAR, 0); | |
6805 | |
428 | 6806 staticpro (&Vpending_warnings); |
6807 Vpending_warnings = Qnil; | |
1204 | 6808 dump_add_root_lisp_object (&Vpending_warnings_tail); |
428 | 6809 Vpending_warnings_tail = Qnil; |
6810 | |
793 | 6811 DEFVAR_LISP ("log-warning-minimum-level", &Vlog_warning_minimum_level); |
6812 Vlog_warning_minimum_level = Qinfo; | |
6813 | |
428 | 6814 staticpro (&Vautoload_queue); |
6815 Vautoload_queue = Qnil; | |
6816 | |
6817 staticpro (&Vcondition_handlers); | |
6818 | |
853 | 6819 staticpro (&Vdeletable_permanent_display_objects); |
6820 Vdeletable_permanent_display_objects = Qnil; | |
6821 | |
6822 staticpro (&Vmodifiable_buffers); | |
6823 Vmodifiable_buffers = Qnil; | |
6824 | |
6825 inhibit_flags = 0; | |
6826 } |