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