eval.c 105 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Evaluator for GNU Emacs Lisp interpreter.
2
   Copyright (C) 1985-1987, 1993-1995, 1999-2012  Free Software Foundation, Inc.
Jim Blandy's avatar
Jim Blandy committed
3 4 5

This file is part of GNU Emacs.

6
GNU Emacs is free software: you can redistribute it and/or modify
Jim Blandy's avatar
Jim Blandy committed
7
it under the terms of the GNU General Public License as published by
8 9
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Jim Blandy's avatar
Jim Blandy committed
10 11 12 13 14 15 16

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
17
along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
Jim Blandy's avatar
Jim Blandy committed
18 19


20
#include <config.h>
21
#include <limits.h>
22
#include <setjmp.h>
23
#include <stdio.h>
Jim Blandy's avatar
Jim Blandy committed
24
#include "lisp.h"
25
#include "blockinput.h"
Jim Blandy's avatar
Jim Blandy committed
26
#include "commands.h"
Jim Blandy's avatar
Jim Blandy committed
27
#include "keyboard.h"
28
#include "dispextern.h"
29
#include "frame.h"		/* For XFRAME.  */
Jim Blandy's avatar
Jim Blandy committed
30

31 32 33 34
#if HAVE_X_WINDOWS
#include "xterm.h"
#endif

Jim Blandy's avatar
Jim Blandy committed
35
struct backtrace
36 37 38
{
  struct backtrace *next;
  Lisp_Object *function;
39
  Lisp_Object *args;	/* Points to vector of args.  */
40
  ptrdiff_t nargs;	/* Length of vector.  */
41
  /* Nonzero means call value of debugger when done with this operation.  */
42
  unsigned int debug_on_exit : 1;
43
};
Jim Blandy's avatar
Jim Blandy committed
44

45
static struct backtrace *backtrace_list;
46 47 48 49

#if !BYTE_MARK_STACK
static
#endif
Jim Blandy's avatar
Jim Blandy committed
50 51
struct catchtag *catchlist;

52 53 54 55 56 57 58 59 60 61 62
/* Chain of condition handlers currently in effect.
   The elements of this chain are contained in the stack frames
   of Fcondition_case and internal_condition_case.
   When an error is signaled (by calling Fsignal, below),
   this chain is searched for an element that applies.  */

#if !BYTE_MARK_STACK
static
#endif
struct handler *handlerlist;

63 64 65 66 67
#ifdef DEBUG_GCPRO
/* Count levels of GCPRO to detect failure to UNGCPRO.  */
int gcpro_level;
#endif

68
Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp;
69
Lisp_Object Qinhibit_quit;
70 71 72 73
Lisp_Object Qand_rest;
static Lisp_Object Qand_optional;
static Lisp_Object Qdebug_on_error;
static Lisp_Object Qdeclare;
74 75
Lisp_Object Qinternal_interpreter_environment, Qclosure;

Paul Eggert's avatar
Paul Eggert committed
76
static Lisp_Object Qdebug;
Jim Blandy's avatar
Jim Blandy committed
77

78 79 80
/* This holds either the symbol `run-hooks' or nil.
   It is nil at an early stage of startup, and when Emacs
   is shutting down.  */
81

Jim Blandy's avatar
Jim Blandy committed
82 83 84 85 86
Lisp_Object Vrun_hooks;

/* Non-nil means record all fset's and provide's, to be undone
   if the file being autoloaded is not fully loaded.
   They are recorded by being consed onto the front of Vautoload_queue:
87
   (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide.  */
Jim Blandy's avatar
Jim Blandy committed
88 89 90 91

Lisp_Object Vautoload_queue;

/* Current number of specbindings allocated in specpdl.  */
92

93
ptrdiff_t specpdl_size;
Jim Blandy's avatar
Jim Blandy committed
94 95

/* Pointer to beginning of specpdl.  */
96

Jim Blandy's avatar
Jim Blandy committed
97 98 99
struct specbinding *specpdl;

/* Pointer to first unused element in specpdl.  */
100

101
struct specbinding *specpdl_ptr;
Jim Blandy's avatar
Jim Blandy committed
102 103

/* Depth in Lisp evaluations and function calls.  */
104

105
static EMACS_INT lisp_eval_depth;
Jim Blandy's avatar
Jim Blandy committed
106

107
/* The value of num_nonmacro_input_events as of the last time we
108
   started to enter the debugger.  If we decide to enter the debugger
109
   again when this is still equal to num_nonmacro_input_events, then we
110 111 112
   know that the debugger itself has an error, and we should just
   signal the error instead of entering an infinite loop of debugger
   invocations.  */
113

114
static EMACS_INT when_entered_debugger;
Jim Blandy's avatar
Jim Blandy committed
115

116 117 118 119 120
/* The function from which the last `signal' was called.  Set in
   Fsignal.  */

Lisp_Object Vsignaling_function;

121 122 123 124 125 126
/* Set to non-zero while processing X events.  Checked in Feval to
   make sure the Lisp interpreter isn't called from a signal handler,
   which is unsafe because the interpreter isn't reentrant.  */

int handling_signal;

127 128 129 130 131 132
/* If non-nil, Lisp code must not be run since some part of Emacs is
   in an inconsistent state.  Currently, x-create-frame uses this to
   avoid triggering window-configuration-change-hook while the new
   frame is half-initialized.  */
Lisp_Object inhibit_lisp_code;

133
static Lisp_Object funcall_lambda (Lisp_Object, ptrdiff_t, Lisp_Object *);
Andreas Schwab's avatar
Andreas Schwab committed
134
static int interactive_p (int);
135
static Lisp_Object apply_lambda (Lisp_Object fun, Lisp_Object args);
136

Andreas Schwab's avatar
Andreas Schwab committed
137
void
138
init_eval_once (void)
Jim Blandy's avatar
Jim Blandy committed
139
{
140
  enum { size = 50 };
141
  specpdl = xmalloc (size * sizeof *specpdl);
142
  specpdl_size = size;
143
  specpdl_ptr = specpdl;
144
  /* Don't forget to update docs (lispref node "Local Variables").  */
Stefan Monnier's avatar
Stefan Monnier committed
145
  max_specpdl_size = 1300; /* 1000 is not enough for CEDET's c-by.el.  */
146
  max_lisp_eval_depth = 600;
147 148

  Vrun_hooks = Qnil;
Jim Blandy's avatar
Jim Blandy committed
149 150
}

Andreas Schwab's avatar
Andreas Schwab committed
151
void
152
init_eval (void)
Jim Blandy's avatar
Jim Blandy committed
153 154 155 156 157 158 159 160
{
  specpdl_ptr = specpdl;
  catchlist = 0;
  handlerlist = 0;
  backtrace_list = 0;
  Vquit_flag = Qnil;
  debug_on_next_call = 0;
  lisp_eval_depth = 0;
161
#ifdef DEBUG_GCPRO
162
  gcpro_level = 0;
163
#endif
164
  /* This is less than the initial value of num_nonmacro_input_events.  */
165
  when_entered_debugger = -1;
Jim Blandy's avatar
Jim Blandy committed
166 167
}

168
/* Unwind-protect function used by call_debugger.  */
169 170

static Lisp_Object
171
restore_stack_limits (Lisp_Object data)
172 173 174
{
  max_specpdl_size = XINT (XCAR (data));
  max_lisp_eval_depth = XINT (XCDR (data));
175
  return Qnil;
176 177 178 179
}

/* Call the Lisp debugger, giving it argument ARG.  */

180
static Lisp_Object
181
call_debugger (Lisp_Object arg)
Jim Blandy's avatar
Jim Blandy committed
182
{
183
  int debug_while_redisplaying;
184
  ptrdiff_t count = SPECPDL_INDEX ();
185
  Lisp_Object val;
186
  EMACS_INT old_max = max_specpdl_size;
187

188 189
  /* Temporarily bump up the stack limits,
     so the debugger won't run out of stack.  */
190

191 192 193 194 195 196 197 198 199
  max_specpdl_size += 1;
  record_unwind_protect (restore_stack_limits,
			 Fcons (make_number (old_max),
				make_number (max_lisp_eval_depth)));
  max_specpdl_size = old_max;

  if (lisp_eval_depth + 40 > max_lisp_eval_depth)
    max_lisp_eval_depth = lisp_eval_depth + 40;

200
  if (max_specpdl_size - 100 < SPECPDL_INDEX ())
201
    max_specpdl_size = SPECPDL_INDEX () + 100;
202

203
#ifdef HAVE_WINDOW_SYSTEM
204 205
  if (display_hourglass_p)
    cancel_hourglass ();
206 207
#endif

Jim Blandy's avatar
Jim Blandy committed
208
  debug_on_next_call = 0;
209
  when_entered_debugger = num_nonmacro_input_events;
210 211 212 213 214

  /* Resetting redisplaying_p to 0 makes sure that debug output is
     displayed if the debugger is invoked during redisplay.  */
  debug_while_redisplaying = redisplaying_p;
  redisplaying_p = 0;
215 216
  specbind (intern ("debugger-may-continue"),
	    debug_while_redisplaying ? Qnil : Qt);
217
  specbind (Qinhibit_redisplay, Qnil);
218
  specbind (Qdebug_on_error, Qnil);
219 220 221

#if 0 /* Binding this prevents execution of Lisp code during
	 redisplay, which necessarily leads to display problems.  */
222
  specbind (Qinhibit_eval_during_redisplay, Qt);
223
#endif
224

225 226 227 228
  val = apply1 (Vdebugger, arg);

  /* Interrupting redisplay and resuming it later is not safe under
     all circumstances.  So, when the debugger returns, abort the
229
     interrupted redisplay by going back to the top-level.  */
230 231 232
  if (debug_while_redisplaying)
    Ftop_level ();

233
  return unbind_to (count, val);
Jim Blandy's avatar
Jim Blandy committed
234 235
}

236
static void
237
do_debug_on_call (Lisp_Object code)
Jim Blandy's avatar
Jim Blandy committed
238 239 240 241 242 243 244 245 246 247 248
{
  debug_on_next_call = 0;
  backtrace_list->debug_on_exit = 1;
  call_debugger (Fcons (code, Qnil));
}

/* NOTE!!! Every function that can call EVAL must protect its args
   and temporaries from garbage collection while it needs them.
   The definition of `For' shows what you have to do.  */

DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
249 250 251
       doc: /* Eval args until one of them yields non-nil, then return that value.
The remaining args are not evalled at all.
If all args return nil, return nil.
Juanma Barranquero's avatar
Juanma Barranquero committed
252
usage: (or CONDITIONS...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
253
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
254
{
255
  register Lisp_Object val = Qnil;
Jim Blandy's avatar
Jim Blandy committed
256 257
  struct gcpro gcpro1;

258
  GCPRO1 (args);
Jim Blandy's avatar
Jim Blandy committed
259

260
  while (CONSP (args))
Jim Blandy's avatar
Jim Blandy committed
261
    {
262
      val = eval_sub (XCAR (args));
Jim Blandy's avatar
Jim Blandy committed
263
      if (!NILP (val))
Jim Blandy's avatar
Jim Blandy committed
264
	break;
265
      args = XCDR (args);
Jim Blandy's avatar
Jim Blandy committed
266 267 268 269 270 271 272
    }

  UNGCPRO;
  return val;
}

DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
273
       doc: /* Eval args until one of them yields nil, then return nil.
Pavel Janík's avatar
Pavel Janík committed
274 275
The remaining args are not evalled at all.
If no arg yields nil, return the last arg's value.
Juanma Barranquero's avatar
Juanma Barranquero committed
276
usage: (and CONDITIONS...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
277
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
278
{
279
  register Lisp_Object val = Qt;
Jim Blandy's avatar
Jim Blandy committed
280 281
  struct gcpro gcpro1;

282
  GCPRO1 (args);
Jim Blandy's avatar
Jim Blandy committed
283

284
  while (CONSP (args))
Jim Blandy's avatar
Jim Blandy committed
285
    {
286
      val = eval_sub (XCAR (args));
Jim Blandy's avatar
Jim Blandy committed
287
      if (NILP (val))
Jim Blandy's avatar
Jim Blandy committed
288
	break;
289
      args = XCDR (args);
Jim Blandy's avatar
Jim Blandy committed
290 291 292 293 294 295 296
    }

  UNGCPRO;
  return val;
}

DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
297
       doc: /* If COND yields non-nil, do THEN, else do ELSE...
Pavel Janík's avatar
Pavel Janík committed
298 299 300
Returns the value of THEN or the value of the last of the ELSE's.
THEN must be one expression, but ELSE... can be zero or more expressions.
If COND yields nil, and there are no ELSE's, the value is nil.
301
usage: (if COND THEN ELSE...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
302
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
303 304 305 306 307
{
  register Lisp_Object cond;
  struct gcpro gcpro1;

  GCPRO1 (args);
308
  cond = eval_sub (Fcar (args));
Jim Blandy's avatar
Jim Blandy committed
309 310
  UNGCPRO;

Jim Blandy's avatar
Jim Blandy committed
311
  if (!NILP (cond))
312
    return eval_sub (Fcar (Fcdr (args)));
Jim Blandy's avatar
Jim Blandy committed
313 314 315 316
  return Fprogn (Fcdr (Fcdr (args)));
}

DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
317 318 319 320 321 322 323 324
       doc: /* Try each clause until one succeeds.
Each clause looks like (CONDITION BODY...).  CONDITION is evaluated
and, if the value is non-nil, this clause succeeds:
then the expressions in BODY are evaluated and the last one's
value is the value of the cond-form.
If no clause succeeds, cond returns nil.
If a clause has one element, as in (CONDITION),
CONDITION's value if non-nil is returned from the cond-form.
325
usage: (cond CLAUSES...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
326
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
327 328 329 330 331 332
{
  register Lisp_Object clause, val;
  struct gcpro gcpro1;

  val = Qnil;
  GCPRO1 (args);
Jim Blandy's avatar
Jim Blandy committed
333
  while (!NILP (args))
Jim Blandy's avatar
Jim Blandy committed
334 335
    {
      clause = Fcar (args);
336
      val = eval_sub (Fcar (clause));
Jim Blandy's avatar
Jim Blandy committed
337
      if (!NILP (val))
Jim Blandy's avatar
Jim Blandy committed
338
	{
339 340
	  if (!EQ (XCDR (clause), Qnil))
	    val = Fprogn (XCDR (clause));
Jim Blandy's avatar
Jim Blandy committed
341 342
	  break;
	}
343
      args = XCDR (args);
Jim Blandy's avatar
Jim Blandy committed
344 345 346 347 348 349
    }
  UNGCPRO;

  return val;
}

Paul Eggert's avatar
Paul Eggert committed
350
DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
351
       doc: /* Eval BODY forms sequentially and return value of last one.
Juanma Barranquero's avatar
Juanma Barranquero committed
352
usage: (progn BODY...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
353
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
354
{
355
  register Lisp_Object val = Qnil;
Jim Blandy's avatar
Jim Blandy committed
356 357
  struct gcpro gcpro1;

358
  GCPRO1 (args);
Jim Blandy's avatar
Jim Blandy committed
359

360
  while (CONSP (args))
Jim Blandy's avatar
Jim Blandy committed
361
    {
362
      val = eval_sub (XCAR (args));
363
      args = XCDR (args);
Jim Blandy's avatar
Jim Blandy committed
364 365 366 367 368 369 370
    }

  UNGCPRO;
  return val;
}

DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
371
       doc: /* Eval FIRST and BODY sequentially; return value from FIRST.
Pavel Janík's avatar
Pavel Janík committed
372 373
The value of FIRST is saved during the evaluation of the remaining args,
whose values are discarded.
374
usage: (prog1 FIRST BODY...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
375
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
376 377 378 379 380 381 382 383 384
{
  Lisp_Object val;
  register Lisp_Object args_left;
  struct gcpro gcpro1, gcpro2;

  args_left = args;
  val = Qnil;
  GCPRO2 (args, val);

385 386 387
  val = eval_sub (XCAR (args_left));
  while (CONSP (args_left = XCDR (args_left)))
    eval_sub (XCAR (args_left));
Jim Blandy's avatar
Jim Blandy committed
388 389 390 391 392 393

  UNGCPRO;
  return val;
}

DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
394
       doc: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
395 396 397
The value of FORM2 is saved during the evaluation of the
remaining args, whose values are discarded.
usage: (prog2 FORM1 FORM2 BODY...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
398
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
399
{
400
  struct gcpro gcpro1;
Jim Blandy's avatar
Jim Blandy committed
401

402 403
  GCPRO1 (args);
  eval_sub (XCAR (args));
404 405
  UNGCPRO;
  return Fprog1 (XCDR (args));
Jim Blandy's avatar
Jim Blandy committed
406 407 408
}

DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
409 410 411 412 413 414 415
       doc: /* Set each SYM to the value of its VAL.
The symbols SYM are variables; they are literal (not evaluated).
The values VAL are expressions; they are evaluated.
Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
The second VAL is not computed until after the first SYM is set, and so on;
each VAL can use the new value of variables set earlier in the `setq'.
The return value of the `setq' form is the value of the last VAL.
Juanma Barranquero's avatar
Juanma Barranquero committed
416
usage: (setq [SYM VAL]...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
417
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
418 419
{
  register Lisp_Object args_left;
420
  register Lisp_Object val, sym, lex_binding;
Jim Blandy's avatar
Jim Blandy committed
421 422
  struct gcpro gcpro1;

423
  if (NILP (args))
Jim Blandy's avatar
Jim Blandy committed
424 425 426 427 428 429 430
    return Qnil;

  args_left = args;
  GCPRO1 (args);

  do
    {
431
      val = eval_sub (Fcar (Fcdr (args_left)));
Jim Blandy's avatar
Jim Blandy committed
432
      sym = Fcar (args_left);
433

434
      /* Like for eval_sub, we do not check declared_special here since
435 436
	 it's been done when let-binding.  */
      if (!NILP (Vinternal_interpreter_environment) /* Mere optimization!  */
437
	  && SYMBOLP (sym)
438 439
	  && !NILP (lex_binding
		    = Fassq (sym, Vinternal_interpreter_environment)))
440 441 442 443
	XSETCDR (lex_binding, val); /* SYM is lexically bound.  */
      else
	Fset (sym, val);	/* SYM is dynamically bound.  */

Jim Blandy's avatar
Jim Blandy committed
444 445
      args_left = Fcdr (Fcdr (args_left));
    }
Juanma Barranquero's avatar
Juanma Barranquero committed
446
  while (!NILP (args_left));
Jim Blandy's avatar
Jim Blandy committed
447 448 449 450

  UNGCPRO;
  return val;
}
451

Jim Blandy's avatar
Jim Blandy committed
452
DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
453
       doc: /* Return the argument, without evaluating it.  `(quote x)' yields `x'.
454 455 456 457 458 459 460 461
Warning: `quote' does not construct its return value, but just returns
the value that was pre-constructed by the Lisp reader (see info node
`(elisp)Printed Representation').
This means that '(a . b) is not identical to (cons 'a 'b): the former
does not cons.  Quoting should be reserved for constants that will
never be modified by side-effects, unless you like self-modifying code.
See the common pitfall in info node `(elisp)Rearrangement' for an example
of unexpected results when a quoted object is modified.
Pavel Janík's avatar
Pavel Janík committed
462
usage: (quote ARG)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
463
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
464
{
465 466
  if (!NILP (Fcdr (args)))
    xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args));
Jim Blandy's avatar
Jim Blandy committed
467 468
  return Fcar (args);
}
469

Jim Blandy's avatar
Jim Blandy committed
470
DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
471 472 473 474
       doc: /* Like `quote', but preferred for objects which are functions.
In byte compilation, `function' causes its argument to be compiled.
`quote' cannot do that.
usage: (function ARG)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
475
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
476
{
477 478
  Lisp_Object quoted = XCAR (args);

479 480
  if (!NILP (Fcdr (args)))
    xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
481 482 483 484 485 486

  if (!NILP (Vinternal_interpreter_environment)
      && CONSP (quoted)
      && EQ (XCAR (quoted), Qlambda))
    /* This is a lambda expression within a lexical environment;
       return an interpreted closure instead of a simple lambda.  */
487 488
    return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment,
				   XCDR (quoted)));
489 490 491
  else
    /* Simply quote the argument.  */
    return quoted;
Jim Blandy's avatar
Jim Blandy committed
492 493
}

494

Paul Eggert's avatar
Paul Eggert committed
495
DEFUN ("interactive-p", Finteractive_p, Sinteractive_p, 0, 0, 0,
496
       doc: /* Return t if the containing function was run directly by user input.
497 498
This means that the function was called with `call-interactively'
\(which includes being called as the binding of a key)
499
and input is currently coming from the keyboard (not a keyboard macro),
500 501 502 503 504 505 506 507
and Emacs is not running in batch mode (`noninteractive' is nil).

The only known proper use of `interactive-p' is in deciding whether to
display a helpful message, or how to display it.  If you're thinking
of using it for any other purpose, it is quite likely that you're
making a mistake.  Think: what do you want to do when the command is
called from a keyboard macro?

508 509 510 511
To test whether your function was called with `call-interactively',
either (i) add an extra optional argument and give it an `interactive'
spec that specifies non-nil unconditionally (such as \"p\"); or (ii)
use `called-interactively-p'.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
512
  (void)
Jim Blandy's avatar
Jim Blandy committed
513
{
514
  return interactive_p (1) ? Qt : Qnil;
515 516 517
}


518
DEFUN ("called-interactively-p", Fcalled_interactively_p, Scalled_interactively_p, 0, 1, 0,
519
       doc: /* Return t if the containing function was called by `call-interactively'.
520 521 522 523 524 525 526 527 528 529 530 531
If KIND is `interactive', then only return t if the call was made
interactively by the user, i.e. not in `noninteractive' mode nor
when `executing-kbd-macro'.
If KIND is `any', on the other hand, it will return t for any kind of
interactive call, including being called as the binding of a key, or
from a keyboard macro, or in `noninteractive' mode.

The only known proper use of `interactive' for KIND is in deciding
whether to display a helpful message, or how to display it.  If you're
thinking of using it for any other purpose, it is quite likely that
you're making a mistake.  Think: what do you want to do when the
command is called from a keyboard macro?
532 533 534 535 536

This function is meant for implementing advice and other
function-modifying features.  Instead of using this, it is sometimes
cleaner to give your function an extra optional argument whose
`interactive' spec specifies non-nil unconditionally (\"p\" is a good
537
way to do this), or via (not (or executing-kbd-macro noninteractive)).  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
538
  (Lisp_Object kind)
539
{
540 541
  return ((INTERACTIVE || !EQ (kind, intern ("interactive")))
	  && interactive_p (1)) ? Qt : Qnil;
542 543 544 545 546
}


/*  Return 1 if function in which this appears was called using
    call-interactively.
547 548 549 550

    EXCLUDE_SUBRS_P non-zero means always return 0 if the function
    called is a built-in.  */

Andreas Schwab's avatar
Andreas Schwab committed
551
static int
552
interactive_p (int exclude_subrs_p)
553 554 555
{
  struct backtrace *btp;
  Lisp_Object fun;
Jim Blandy's avatar
Jim Blandy committed
556 557

  btp = backtrace_list;
Jim Blandy's avatar
Jim Blandy committed
558 559

  /* If this isn't a byte-compiled function, there may be a frame at
560
     the top for Finteractive_p.  If so, skip it.  */
561
  fun = Findirect_function (*btp->function, Qnil);
562 563
  if (SUBRP (fun) && (XSUBR (fun) == &Sinteractive_p
		      || XSUBR (fun) == &Scalled_interactively_p))
Jim Blandy's avatar
Jim Blandy committed
564
    btp = btp->next;
Jim Blandy's avatar
Jim Blandy committed
565 566

  /* If we're running an Emacs 18-style byte-compiled function, there
567 568 569
     may be a frame for Fbytecode at the top level.  In any version of
     Emacs there can be Fbytecode frames for subexpressions evaluated
     inside catch and condition-case.  Skip past them.
Jim Blandy's avatar
Jim Blandy committed
570

571
     If this isn't a byte-compiled function, then we may now be
Jim Blandy's avatar
Jim Blandy committed
572
     looking at several frames for special forms.  Skip past them.  */
573 574
  while (btp
	 && (EQ (*btp->function, Qbytecode)
575
	     || btp->nargs == UNEVALLED))
Jim Blandy's avatar
Jim Blandy committed
576 577
    btp = btp->next;

578
  /* `btp' now points at the frame of the innermost function that isn't
Jim Blandy's avatar
Jim Blandy committed
579 580 581
     a special form, ignoring frames for Finteractive_p and/or
     Fbytecode at the top.  If this frame is for a built-in function
     (such as load or eval-region) return nil.  */
582
  fun = Findirect_function (*btp->function, Qnil);
583 584
  if (exclude_subrs_p && SUBRP (fun))
    return 0;
585

586
  /* `btp' points to the frame of a Lisp function that called interactive-p.
Jim Blandy's avatar
Jim Blandy committed
587 588
     Return t if that function was called interactively.  */
  if (btp && btp->next && EQ (*btp->next->function, Qcall_interactively))
589 590
    return 1;
  return 0;
Jim Blandy's avatar
Jim Blandy committed
591 592
}

593

594
DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
595
       doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
Richard M. Stallman's avatar
Richard M. Stallman committed
596
Aliased variables always have the same value; setting one sets the other.
597
Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS.  If it is
598 599 600 601
omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
itself an alias.  If NEW-ALIAS is bound, and BASE-VARIABLE is not,
then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
602
The return value is BASE-VARIABLE.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
603
  (Lisp_Object new_alias, Lisp_Object base_variable, Lisp_Object docstring)
Gerd Moellmann's avatar
Gerd Moellmann committed
604 605
{
  struct Lisp_Symbol *sym;
606

607 608
  CHECK_SYMBOL (new_alias);
  CHECK_SYMBOL (base_variable);
Gerd Moellmann's avatar
Gerd Moellmann committed
609

610
  sym = XSYMBOL (new_alias);
611 612

  if (sym->constant)
Stefan Monnier's avatar
Stefan Monnier committed
613 614
    /* Not sure why, but why not?  */
    error ("Cannot make a constant an alias");
615 616 617 618 619 620 621 622 623

  switch (sym->redirect)
    {
    case SYMBOL_FORWARDED:
      error ("Cannot make an internal variable an alias");
    case SYMBOL_LOCALIZED:
      error ("Don't know how to make a localized variable an alias");
    }

624
  /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
625 626 627 628
     If n_a is bound, but b_v is not, set the value of b_v to n_a,
     so that old-code that affects n_a before the aliasing is setup
     still works.  */
  if (NILP (Fboundp (base_variable)))
629
    set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1);
630 631 632 633

  {
    struct specbinding *p;

Paul Eggert's avatar
Paul Eggert committed
634
    for (p = specpdl_ptr; p > specpdl; )
635
      if ((--p)->func == NULL
636 637 638 639 640
	  && (EQ (new_alias,
		  CONSP (p->symbol) ? XCAR (p->symbol) : p->symbol)))
	error ("Don't know how to make a let-bound variable an alias");
  }

641
  sym->declared_special = 1;
642
  XSYMBOL (base_variable)->declared_special = 1;
643 644
  sym->redirect = SYMBOL_VARALIAS;
  SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
645 646
  sym->constant = SYMBOL_CONSTANT_P (base_variable);
  LOADHIST_ATTACH (new_alias);
647 648
  /* Even if docstring is nil: remove old docstring.  */
  Fput (new_alias, Qvariable_documentation, docstring);
649

650
  return base_variable;
Gerd Moellmann's avatar
Gerd Moellmann committed
651 652 653
}


Jim Blandy's avatar
Jim Blandy committed
654
DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
Juanma Barranquero's avatar
Juanma Barranquero committed
655
       doc: /* Define SYMBOL as a variable, and return SYMBOL.
656 657 658 659 660 661 662 663 664
You are not required to define a variable in order to use it, but
defining it lets you supply an initial value and documentation, which
can be referred to by the Emacs help facilities and other programming
tools.  The `defvar' form also declares the variable as \"special\",
so that it is always dynamically bound even if `lexical-binding' is t.

The optional argument INITVALUE is evaluated, and used to set SYMBOL,
only if SYMBOL's value is void.  If SYMBOL is buffer-local, its
default value is what is set; buffer-local values are not affected.
Pavel Janík's avatar
Pavel Janík committed
665
If INITVALUE is missing, SYMBOL's value is not set.
666 667 668 669 670 671 672

If SYMBOL has a local binding, then this form affects the local
binding.  This is usually not what you want.  Thus, if you need to
load a file defining variables, with this form or with `defconst' or
`defcustom', you should always load that file _outside_ any bindings
for these variables.  \(`defconst' and `defcustom' behave similarly in
this respect.)
673 674 675 676 677

The optional argument DOCSTRING is a documentation string for the
variable.

To define a user option, use `defcustom' instead of `defvar'.
678
usage: (defvar SYMBOL &optional INITVALUE DOCSTRING)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
679
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
680
{
681
  register Lisp_Object sym, tem, tail;
Jim Blandy's avatar
Jim Blandy committed
682 683

  sym = Fcar (args);
684 685
  tail = Fcdr (args);
  if (!NILP (Fcdr (Fcdr (tail))))
686
    error ("Too many arguments");
687

688
  tem = Fdefault_boundp (sym);
689
  if (!NILP (tail))
Jim Blandy's avatar
Jim Blandy committed
690
    {
691 692
      /* Do it before evaluating the initial value, for self-references.  */
      XSYMBOL (sym)->declared_special = 1;
693

Jim Blandy's avatar
Jim Blandy committed
694
      if (NILP (tem))
695
	Fset_default (sym, eval_sub (Fcar (tail)));
696 697 698
      else
	{ /* Check if there is really a global binding rather than just a let
	     binding that shadows the global unboundness of the var.  */
699
	  volatile struct specbinding *pdl = specpdl_ptr;
700
	  while (pdl > specpdl)
701
	    {
702
	      if (EQ ((--pdl)->symbol, sym) && !pdl->func
703 704 705 706 707 708 709 710
		  && EQ (pdl->old_value, Qunbound))
		{
		  message_with_string ("Warning: defvar ignored because %s is let-bound",
				       SYMBOL_NAME (sym), 1);
		  break;
		}
	    }
	}
711
      tail = Fcdr (tail);
712 713
      tem = Fcar (tail);
      if (!NILP (tem))
714 715 716 717 718
	{
	  if (!NILP (Vpurify_flag))
	    tem = Fpurecopy (tem);
	  Fput (sym, Qvariable_documentation, tem);
	}
719
      LOADHIST_ATTACH (sym);
Jim Blandy's avatar
Jim Blandy committed
720
    }
721 722 723 724 725 726 727
  else if (!NILP (Vinternal_interpreter_environment)
	   && !XSYMBOL (sym)->declared_special)
    /* A simple (defvar foo) with lexical scoping does "nothing" except
       declare that var to be dynamically scoped *locally* (i.e. within
       the current file or let-block).  */
    Vinternal_interpreter_environment =
      Fcons (sym, Vinternal_interpreter_environment);
728
  else
729 730 731 732 733
    {
      /* Simple (defvar <var>) should not count as a definition at all.
	 It could get in the way of other definitions, and unloading this
	 package could try to make the variable unbound.  */
    }
Andreas Schwab's avatar
Andreas Schwab committed
734

Jim Blandy's avatar
Jim Blandy committed
735 736 737 738
  return sym;
}

DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
739
       doc: /* Define SYMBOL as a constant variable.
740 741 742 743 744 745 746 747 748 749 750 751 752
This declares that neither programs nor users should ever change the
value.  This constancy is not actually enforced by Emacs Lisp, but
SYMBOL is marked as a special variable so that it is never lexically
bound.

The `defconst' form always sets the value of SYMBOL to the result of
evalling INITVALUE.  If SYMBOL is buffer-local, its default value is
what is set; buffer-local values are not affected.  If SYMBOL has a
local binding, then this form sets the local binding's value.
However, you should normally not make local bindings for variables
defined with this form.

The optional DOCSTRING specifies the variable's documentation string.
753
usage: (defconst SYMBOL INITVALUE [DOCSTRING])  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
754
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
755 756 757 758
{
  register Lisp_Object sym, tem;

  sym = Fcar (args);
759
  if (!NILP (Fcdr (Fcdr (Fcdr (args)))))
760
    error ("Too many arguments");
761

762
  tem = eval_sub (Fcar (Fcdr (args)));
763 764 765
  if (!NILP (Vpurify_flag))
    tem = Fpurecopy (tem);
  Fset_default (sym, tem);
766
  XSYMBOL (sym)->declared_special = 1;
Jim Blandy's avatar
Jim Blandy committed
767
  tem = Fcar (Fcdr (Fcdr (args)));
Jim Blandy's avatar
Jim Blandy committed
768
  if (!NILP (tem))
Jim Blandy's avatar
Jim Blandy committed
769
    {
Jim Blandy's avatar
Jim Blandy committed
770
      if (!NILP (Vpurify_flag))
Jim Blandy's avatar
Jim Blandy committed
771 772 773
	tem = Fpurecopy (tem);
      Fput (sym, Qvariable_documentation, tem);
    }
774
  Fput (sym, Qrisky_local_variable, Qt);
775
  LOADHIST_ATTACH (sym);
Jim Blandy's avatar
Jim Blandy committed
776 777 778
  return sym;
}

779 780 781 782 783 784 785 786 787 788 789
/* Make SYMBOL lexically scoped.  */
DEFUN ("internal-make-var-non-special", Fmake_var_non_special,
       Smake_var_non_special, 1, 1, 0,
       doc: /* Internal function.  */)
     (Lisp_Object symbol)
{
  CHECK_SYMBOL (symbol);
  XSYMBOL (symbol)->declared_special = 0;
  return Qnil;
}

Jim Blandy's avatar
Jim Blandy committed
790 791

DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
792 793 794 795 796
       doc: /* Bind variables according to VARLIST then eval BODY.
The value of the last form in BODY is returned.
Each element of VARLIST is a symbol (which is bound to nil)
or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
Each VALUEFORM can refer to the symbols already bound by this VARLIST.
797
usage: (let* VARLIST BODY...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
798
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
799
{
800
  Lisp_Object varlist, var, val, elt, lexenv;
801
  ptrdiff_t count = SPECPDL_INDEX ();
Jim Blandy's avatar
Jim Blandy committed
802 803 804 805
  struct gcpro gcpro1, gcpro2, gcpro3;

  GCPRO3 (args, elt, varlist);

806 807
  lexenv = Vinternal_interpreter_environment;

Jim Blandy's avatar
Jim Blandy committed
808
  varlist = Fcar (args);
809
  while (CONSP (varlist))
Jim Blandy's avatar
Jim Blandy committed
810 811
    {
      QUIT;
812 813

      elt = XCAR (varlist);
814
      if (SYMBOLP (elt))
815 816 817 818
	{
	  var = elt;
	  val = Qnil;
	}
Jim Blandy's avatar
Jim Blandy committed
819
      else if (! NILP (Fcdr (Fcdr (elt))))
820
	signal_error ("`let' bindings can have only one value-form", elt);
Jim Blandy's avatar
Jim Blandy committed
821 822
      else
	{
823
	  var = Fcar (elt);
824
	  val = eval_sub (Fcar (Fcdr (elt)));
Jim Blandy's avatar
Jim Blandy committed
825
	}
826

827 828 829
      if (!NILP (lexenv) && SYMBOLP (var)
	  && !XSYMBOL (var)->declared_special
	  && NILP (Fmemq (var, Vinternal_interpreter_environment)))
830 831 832
	/* Lexically bind VAR by adding it to the interpreter's binding
	   alist.  */
	{
833 834 835 836 837 838 839 840 841
	  Lisp_Object newenv
	    = Fcons (Fcons (var, val), Vinternal_interpreter_environment);
	  if (EQ (Vinternal_interpreter_environment, lexenv))
	    /* Save the old lexical environment on the specpdl stack,
	       but only for the first lexical binding, since we'll never
	       need to revert to one of the intermediate ones.  */
	    specbind (Qinternal_interpreter_environment, newenv);
	  else
	    Vinternal_interpreter_environment = newenv;
Jim Blandy's avatar
Jim Blandy committed
842
	}
843 844 845 846
      else
	specbind (var, val);

      varlist = XCDR (varlist);
Jim Blandy's avatar
Jim Blandy committed
847 848 849 850 851 852 853
    }
  UNGCPRO;
  val = Fprogn (Fcdr (args));
  return unbind_to (count, val);
}

DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,