eval.c 104 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 137 138 139 140 141 142 143 144 145 146 147 148 149 150

/* Functions to set Lisp_Object slots of struct specbinding.  */

static inline void
set_specpdl_symbol (Lisp_Object symbol)
{
  specpdl_ptr->symbol = symbol;
}

static inline void
set_specpdl_old_value (Lisp_Object oldval)
{
  specpdl_ptr->old_value = oldval;
}

Andreas Schwab's avatar
Andreas Schwab committed
151
void
152
init_eval_once (void)
Jim Blandy's avatar
Jim Blandy committed
153
{
154
  enum { size = 50 };
155
  specpdl = xmalloc (size * sizeof *specpdl);
156
  specpdl_size = size;
157
  specpdl_ptr = specpdl;
158
  /* Don't forget to update docs (lispref node "Local Variables").  */
Stefan Monnier's avatar
Stefan Monnier committed
159
  max_specpdl_size = 1300; /* 1000 is not enough for CEDET's c-by.el.  */
160
  max_lisp_eval_depth = 600;
161 162

  Vrun_hooks = Qnil;
Jim Blandy's avatar
Jim Blandy committed
163 164
}

Andreas Schwab's avatar
Andreas Schwab committed
165
void
166
init_eval (void)
Jim Blandy's avatar
Jim Blandy committed
167 168 169 170 171 172 173 174
{
  specpdl_ptr = specpdl;
  catchlist = 0;
  handlerlist = 0;
  backtrace_list = 0;
  Vquit_flag = Qnil;
  debug_on_next_call = 0;
  lisp_eval_depth = 0;
175
#ifdef DEBUG_GCPRO
176
  gcpro_level = 0;
177
#endif
178
  /* This is less than the initial value of num_nonmacro_input_events.  */
179
  when_entered_debugger = -1;
Jim Blandy's avatar
Jim Blandy committed
180 181
}

182
/* Unwind-protect function used by call_debugger.  */
183 184

static Lisp_Object
185
restore_stack_limits (Lisp_Object data)
186 187 188
{
  max_specpdl_size = XINT (XCAR (data));
  max_lisp_eval_depth = XINT (XCDR (data));
189
  return Qnil;
190 191 192 193
}

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

194
static Lisp_Object
195
call_debugger (Lisp_Object arg)
Jim Blandy's avatar
Jim Blandy committed
196
{
197
  int debug_while_redisplaying;
198
  ptrdiff_t count = SPECPDL_INDEX ();
199
  Lisp_Object val;
200
  EMACS_INT old_max = max_specpdl_size;
201

202 203
  /* Temporarily bump up the stack limits,
     so the debugger won't run out of stack.  */
204

205 206 207 208 209 210 211 212 213
  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;

214
  if (max_specpdl_size - 100 < SPECPDL_INDEX ())
215
    max_specpdl_size = SPECPDL_INDEX () + 100;
216

217
#ifdef HAVE_WINDOW_SYSTEM
218 219
  if (display_hourglass_p)
    cancel_hourglass ();
220 221
#endif

Jim Blandy's avatar
Jim Blandy committed
222
  debug_on_next_call = 0;
223
  when_entered_debugger = num_nonmacro_input_events;
224 225 226 227 228

  /* 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;
229 230
  specbind (intern ("debugger-may-continue"),
	    debug_while_redisplaying ? Qnil : Qt);
231
  specbind (Qinhibit_redisplay, Qnil);
232
  specbind (Qdebug_on_error, Qnil);
233 234 235

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

239 240 241 242
  val = apply1 (Vdebugger, arg);

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

247
  return unbind_to (count, val);
Jim Blandy's avatar
Jim Blandy committed
248 249
}

250
static void
251
do_debug_on_call (Lisp_Object code)
Jim Blandy's avatar
Jim Blandy committed
252 253 254 255 256 257 258 259 260 261 262
{
  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
263 264 265
       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
266
usage: (or CONDITIONS...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
267
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
268
{
269
  register Lisp_Object val = Qnil;
Jim Blandy's avatar
Jim Blandy committed
270 271
  struct gcpro gcpro1;

272
  GCPRO1 (args);
Jim Blandy's avatar
Jim Blandy committed
273

274
  while (CONSP (args))
Jim Blandy's avatar
Jim Blandy committed
275
    {
276
      val = eval_sub (XCAR (args));
Jim Blandy's avatar
Jim Blandy committed
277
      if (!NILP (val))
Jim Blandy's avatar
Jim Blandy committed
278
	break;
279
      args = XCDR (args);
Jim Blandy's avatar
Jim Blandy committed
280 281 282 283 284 285 286
    }

  UNGCPRO;
  return val;
}

DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
287
       doc: /* Eval args until one of them yields nil, then return nil.
Pavel Janík's avatar
Pavel Janík committed
288 289
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
290
usage: (and CONDITIONS...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
291
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
292
{
293
  register Lisp_Object val = Qt;
Jim Blandy's avatar
Jim Blandy committed
294 295
  struct gcpro gcpro1;

296
  GCPRO1 (args);
Jim Blandy's avatar
Jim Blandy committed
297

298
  while (CONSP (args))
Jim Blandy's avatar
Jim Blandy committed
299
    {
300
      val = eval_sub (XCAR (args));
Jim Blandy's avatar
Jim Blandy committed
301
      if (NILP (val))
Jim Blandy's avatar
Jim Blandy committed
302
	break;
303
      args = XCDR (args);
Jim Blandy's avatar
Jim Blandy committed
304 305 306 307 308 309 310
    }

  UNGCPRO;
  return val;
}

DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
311
       doc: /* If COND yields non-nil, do THEN, else do ELSE...
Pavel Janík's avatar
Pavel Janík committed
312 313 314
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.
315
usage: (if COND THEN ELSE...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
316
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
317 318 319 320 321
{
  register Lisp_Object cond;
  struct gcpro gcpro1;

  GCPRO1 (args);
322
  cond = eval_sub (Fcar (args));
Jim Blandy's avatar
Jim Blandy committed
323 324
  UNGCPRO;

Jim Blandy's avatar
Jim Blandy committed
325
  if (!NILP (cond))
326
    return eval_sub (Fcar (Fcdr (args)));
Jim Blandy's avatar
Jim Blandy committed
327 328 329 330
  return Fprogn (Fcdr (Fcdr (args)));
}

DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
331 332 333 334 335 336 337 338
       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.
339
usage: (cond CLAUSES...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
340
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
341 342 343 344 345 346
{
  register Lisp_Object clause, val;
  struct gcpro gcpro1;

  val = Qnil;
  GCPRO1 (args);
Jim Blandy's avatar
Jim Blandy committed
347
  while (!NILP (args))
Jim Blandy's avatar
Jim Blandy committed
348 349
    {
      clause = Fcar (args);
350
      val = eval_sub (Fcar (clause));
Jim Blandy's avatar
Jim Blandy committed
351
      if (!NILP (val))
Jim Blandy's avatar
Jim Blandy committed
352
	{
353 354
	  if (!EQ (XCDR (clause), Qnil))
	    val = Fprogn (XCDR (clause));
Jim Blandy's avatar
Jim Blandy committed
355 356
	  break;
	}
357
      args = XCDR (args);
Jim Blandy's avatar
Jim Blandy committed
358 359 360 361 362 363
    }
  UNGCPRO;

  return val;
}

Paul Eggert's avatar
Paul Eggert committed
364
DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
365
       doc: /* Eval BODY forms sequentially and return value of last one.
Juanma Barranquero's avatar
Juanma Barranquero committed
366
usage: (progn BODY...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
367
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
368
{
369
  register Lisp_Object val = Qnil;
Jim Blandy's avatar
Jim Blandy committed
370 371
  struct gcpro gcpro1;

372
  GCPRO1 (args);
Jim Blandy's avatar
Jim Blandy committed
373

374
  while (CONSP (args))
Jim Blandy's avatar
Jim Blandy committed
375
    {
376
      val = eval_sub (XCAR (args));
377
      args = XCDR (args);
Jim Blandy's avatar
Jim Blandy committed
378 379 380 381 382 383 384
    }

  UNGCPRO;
  return val;
}

DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
385
       doc: /* Eval FIRST and BODY sequentially; return value from FIRST.
Pavel Janík's avatar
Pavel Janík committed
386 387
The value of FIRST is saved during the evaluation of the remaining args,
whose values are discarded.
388
usage: (prog1 FIRST BODY...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
389
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
390 391 392 393 394 395 396 397 398
{
  Lisp_Object val;
  register Lisp_Object args_left;
  struct gcpro gcpro1, gcpro2;

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

399 400 401
  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
402 403 404 405 406 407

  UNGCPRO;
  return val;
}

DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
408
       doc: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
409 410 411
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
412
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
413
{
414
  struct gcpro gcpro1;
Jim Blandy's avatar
Jim Blandy committed
415

416 417
  GCPRO1 (args);
  eval_sub (XCAR (args));
418 419
  UNGCPRO;
  return Fprog1 (XCDR (args));
Jim Blandy's avatar
Jim Blandy committed
420 421 422
}

DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
423 424 425 426 427 428 429
       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
430
usage: (setq [SYM VAL]...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
431
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
432 433
{
  register Lisp_Object args_left;
434
  register Lisp_Object val, sym, lex_binding;
Jim Blandy's avatar
Jim Blandy committed
435 436
  struct gcpro gcpro1;

437
  if (NILP (args))
Jim Blandy's avatar
Jim Blandy committed
438 439 440 441 442 443 444
    return Qnil;

  args_left = args;
  GCPRO1 (args);

  do
    {
445
      val = eval_sub (Fcar (Fcdr (args_left)));
Jim Blandy's avatar
Jim Blandy committed
446
      sym = Fcar (args_left);
447

448
      /* Like for eval_sub, we do not check declared_special here since
449 450
	 it's been done when let-binding.  */
      if (!NILP (Vinternal_interpreter_environment) /* Mere optimization!  */
451
	  && SYMBOLP (sym)
452 453
	  && !NILP (lex_binding
		    = Fassq (sym, Vinternal_interpreter_environment)))
454 455 456 457
	XSETCDR (lex_binding, val); /* SYM is lexically bound.  */
      else
	Fset (sym, val);	/* SYM is dynamically bound.  */

Jim Blandy's avatar
Jim Blandy committed
458 459
      args_left = Fcdr (Fcdr (args_left));
    }
Juanma Barranquero's avatar
Juanma Barranquero committed
460
  while (!NILP (args_left));
Jim Blandy's avatar
Jim Blandy committed
461 462 463 464

  UNGCPRO;
  return val;
}
465

Jim Blandy's avatar
Jim Blandy committed
466
DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
467
       doc: /* Return the argument, without evaluating it.  `(quote x)' yields `x'.
468 469 470 471 472 473 474 475
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
476
usage: (quote ARG)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
477
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
478
{
479 480
  if (!NILP (Fcdr (args)))
    xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args));
Jim Blandy's avatar
Jim Blandy committed
481 482
  return Fcar (args);
}
483

Jim Blandy's avatar
Jim Blandy committed
484
DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
485 486 487 488
       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
489
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
490
{
491 492
  Lisp_Object quoted = XCAR (args);

493 494
  if (!NILP (Fcdr (args)))
    xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
495 496 497 498 499 500

  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.  */
501 502
    return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment,
				   XCDR (quoted)));
503 504 505
  else
    /* Simply quote the argument.  */
    return quoted;
Jim Blandy's avatar
Jim Blandy committed
506 507
}

508

Paul Eggert's avatar
Paul Eggert committed
509
DEFUN ("interactive-p", Finteractive_p, Sinteractive_p, 0, 0, 0,
510
       doc: /* Return t if the containing function was run directly by user input.
511 512
This means that the function was called with `call-interactively'
\(which includes being called as the binding of a key)
513
and input is currently coming from the keyboard (not a keyboard macro),
514 515 516 517 518 519 520 521
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?

522 523 524 525
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
526
  (void)
Jim Blandy's avatar
Jim Blandy committed
527
{
528
  return interactive_p (1) ? Qt : Qnil;
529 530 531
}


532
DEFUN ("called-interactively-p", Fcalled_interactively_p, Scalled_interactively_p, 0, 1, 0,
533
       doc: /* Return t if the containing function was called by `call-interactively'.
534 535 536 537 538 539 540 541 542 543 544 545
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?
546

547 548 549 550
Instead of using this function, it is sometimes cleaner to give your
function an extra optional argument whose `interactive' spec specifies
non-nil unconditionally (\"p\" is a good way to do this), or via
\(not (or executing-kbd-macro noninteractive)).  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
551
  (Lisp_Object kind)
552
{
553 554
  return ((INTERACTIVE || !EQ (kind, intern ("interactive")))
	  && interactive_p (1)) ? Qt : Qnil;
555 556 557 558 559
}


/*  Return 1 if function in which this appears was called using
    call-interactively.
560 561 562 563

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

Andreas Schwab's avatar
Andreas Schwab committed
564
static int
565
interactive_p (int exclude_subrs_p)
566 567 568
{
  struct backtrace *btp;
  Lisp_Object fun;
Jim Blandy's avatar
Jim Blandy committed
569 570

  btp = backtrace_list;
Jim Blandy's avatar
Jim Blandy committed
571 572

  /* If this isn't a byte-compiled function, there may be a frame at
573
     the top for Finteractive_p.  If so, skip it.  */
574
  fun = Findirect_function (*btp->function, Qnil);
575 576
  if (SUBRP (fun) && (XSUBR (fun) == &Sinteractive_p
		      || XSUBR (fun) == &Scalled_interactively_p))
Jim Blandy's avatar
Jim Blandy committed
577
    btp = btp->next;
Jim Blandy's avatar
Jim Blandy committed
578 579

  /* If we're running an Emacs 18-style byte-compiled function, there
580 581 582
     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
583

584
     If this isn't a byte-compiled function, then we may now be
Jim Blandy's avatar
Jim Blandy committed
585
     looking at several frames for special forms.  Skip past them.  */
586 587
  while (btp
	 && (EQ (*btp->function, Qbytecode)
588
	     || btp->nargs == UNEVALLED))
Jim Blandy's avatar
Jim Blandy committed
589 590
    btp = btp->next;

591
  /* `btp' now points at the frame of the innermost function that isn't
Jim Blandy's avatar
Jim Blandy committed
592 593 594
     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.  */
595
  fun = Findirect_function (*btp->function, Qnil);
596 597
  if (exclude_subrs_p && SUBRP (fun))
    return 0;
598

599
  /* `btp' points to the frame of a Lisp function that called interactive-p.
Jim Blandy's avatar
Jim Blandy committed
600 601
     Return t if that function was called interactively.  */
  if (btp && btp->next && EQ (*btp->next->function, Qcall_interactively))
602 603
    return 1;
  return 0;
Jim Blandy's avatar
Jim Blandy committed
604 605
}

606

607
DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
608
       doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
Richard M. Stallman's avatar
Richard M. Stallman committed
609
Aliased variables always have the same value; setting one sets the other.
610
Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS.  If it is
611 612 613 614
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.
615
The return value is BASE-VARIABLE.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
616
  (Lisp_Object new_alias, Lisp_Object base_variable, Lisp_Object docstring)
Gerd Moellmann's avatar
Gerd Moellmann committed
617 618
{
  struct Lisp_Symbol *sym;
619

620 621
  CHECK_SYMBOL (new_alias);
  CHECK_SYMBOL (base_variable);
Gerd Moellmann's avatar
Gerd Moellmann committed
622

623
  sym = XSYMBOL (new_alias);
624 625

  if (sym->constant)
Stefan Monnier's avatar
Stefan Monnier committed
626 627
    /* Not sure why, but why not?  */
    error ("Cannot make a constant an alias");
628 629 630 631 632 633 634 635 636

  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");
    }

637
  /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
638 639 640 641
     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)))
642
    set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1);
643 644 645 646

  {
    struct specbinding *p;

Paul Eggert's avatar
Paul Eggert committed
647
    for (p = specpdl_ptr; p > specpdl; )
648
      if ((--p)->func == NULL
649 650 651 652 653
	  && (EQ (new_alias,
		  CONSP (p->symbol) ? XCAR (p->symbol) : p->symbol)))
	error ("Don't know how to make a let-bound variable an alias");
  }

654
  sym->declared_special = 1;
655
  XSYMBOL (base_variable)->declared_special = 1;
656 657
  sym->redirect = SYMBOL_VARALIAS;
  SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
658 659
  sym->constant = SYMBOL_CONSTANT_P (base_variable);
  LOADHIST_ATTACH (new_alias);
660 661
  /* Even if docstring is nil: remove old docstring.  */
  Fput (new_alias, Qvariable_documentation, docstring);
662

663
  return base_variable;
Gerd Moellmann's avatar
Gerd Moellmann committed
664 665 666
}


Jim Blandy's avatar
Jim Blandy committed
667
DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
Juanma Barranquero's avatar
Juanma Barranquero committed
668
       doc: /* Define SYMBOL as a variable, and return SYMBOL.
669 670 671 672 673 674 675 676 677
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
678
If INITVALUE is missing, SYMBOL's value is not set.
679 680 681 682 683 684 685

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.)
686 687 688 689 690

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

To define a user option, use `defcustom' instead of `defvar'.
691
usage: (defvar SYMBOL &optional INITVALUE DOCSTRING)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
692
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
693
{
694
  register Lisp_Object sym, tem, tail;
Jim Blandy's avatar
Jim Blandy committed
695 696

  sym = Fcar (args);
697 698
  tail = Fcdr (args);
  if (!NILP (Fcdr (Fcdr (tail))))
699
    error ("Too many arguments");
700

701
  tem = Fdefault_boundp (sym);
702
  if (!NILP (tail))
Jim Blandy's avatar
Jim Blandy committed
703
    {
704 705
      /* Do it before evaluating the initial value, for self-references.  */
      XSYMBOL (sym)->declared_special = 1;
706

Jim Blandy's avatar
Jim Blandy committed
707
      if (NILP (tem))
708
	Fset_default (sym, eval_sub (Fcar (tail)));
709 710 711
      else
	{ /* Check if there is really a global binding rather than just a let
	     binding that shadows the global unboundness of the var.  */
712
	  volatile struct specbinding *pdl = specpdl_ptr;
713
	  while (pdl > specpdl)
714
	    {
715
	      if (EQ ((--pdl)->symbol, sym) && !pdl->func
716 717 718 719 720 721 722 723
		  && EQ (pdl->old_value, Qunbound))
		{
		  message_with_string ("Warning: defvar ignored because %s is let-bound",
				       SYMBOL_NAME (sym), 1);
		  break;
		}
	    }
	}
724
      tail = Fcdr (tail);
725 726
      tem = Fcar (tail);
      if (!NILP (tem))
727 728 729 730 731
	{
	  if (!NILP (Vpurify_flag))
	    tem = Fpurecopy (tem);
	  Fput (sym, Qvariable_documentation, tem);
	}
732
      LOADHIST_ATTACH (sym);
Jim Blandy's avatar
Jim Blandy committed
733
    }
734 735 736 737 738 739 740
  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);
741
  else
742 743 744 745 746
    {
      /* 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
747

Jim Blandy's avatar
Jim Blandy committed
748 749 750 751
  return sym;
}

DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
752
       doc: /* Define SYMBOL as a constant variable.
753 754 755 756 757 758 759 760 761 762 763 764 765
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.
766
usage: (defconst SYMBOL INITVALUE [DOCSTRING])  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
767
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
768 769 770 771
{
  register Lisp_Object sym, tem;

  sym = Fcar (args);
772
  if (!NILP (Fcdr (Fcdr (Fcdr (args)))))
773
    error ("Too many arguments");
774

775
  tem = eval_sub (Fcar (Fcdr (args)));
776 777 778
  if (!NILP (Vpurify_flag))
    tem = Fpurecopy (tem);
  Fset_default (sym, tem);
779
  XSYMBOL (sym)->declared_special = 1;
Jim Blandy's avatar
Jim Blandy committed
780
  tem = Fcar (Fcdr (Fcdr (args)));
Jim Blandy's avatar
Jim Blandy committed
781
  if (!NILP (tem))
Jim Blandy's avatar
Jim Blandy committed
782
    {
Jim Blandy's avatar
Jim Blandy committed
783
      if (!NILP (Vpurify_flag))
Jim Blandy's avatar
Jim Blandy committed
784 785 786
	tem = Fpurecopy (tem);
      Fput (sym, Qvariable_documentation, tem);
    }
787
  Fput (sym, Qrisky_local_variable, Qt);
788
  LOADHIST_ATTACH (sym);
Jim Blandy's avatar
Jim Blandy committed
789 790 791
  return sym;
}

792 793 794 795 796 797 798 799 800 801 802
/* 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
803 804

DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
805 806 807 808 809
       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.
810
usage: (let* VARLIST BODY...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
811
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
812
{
813
  Lisp_Object varlist, var, val, elt, lexenv;
814
  ptrdiff_t count = SPECPDL_INDEX ();
Jim Blandy's avatar
Jim Blandy committed
815 816 817 818
  struct gcpro gcpro1, gcpro2, gcpro3;

  GCPRO3 (args, elt, varlist);

819 820
  lexenv = Vinternal_interpreter_environment;

Jim Blandy's avatar
Jim Blandy committed
821
  varlist = Fcar (args);
822
  while (CONSP (varlist))
Jim Blandy's avatar
Jim Blandy committed
823 824
    {
      QUIT;
825 826

      elt = XCAR (varlist);
827
      if (SYMBOLP (elt))
828 829 830 831
	{
	  var = elt;
	  val = Qnil;
	}
Jim Blandy's avatar
Jim Blandy committed
832
      else if (! NILP (Fcdr (Fcdr (elt))))
833
	signal_error ("`let' bindings can have only one value-form", elt);
Jim Blandy's avatar
Jim Blandy committed
834 835
      else
	{
836
	  var = Fcar (elt);
837
	  val = eval_sub (Fcar (Fcdr (elt)));
Jim Blandy's avatar
Jim Blandy committed
838
	}
839

840 841 842
      if (!NILP (lexenv) && SYMBOLP (var)
	  && !XSYMBOL (var)->declared_special
	  && NILP (Fmemq (var, Vinternal_interpreter_environment)))
843 844 845
	/* Lexically bind VAR by adding it to the interpreter's binding
	   alist.  */
	{
846 847 848 849 850 851 852 853 854
	  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
855
	}
856 857 858 859
      else
	specbind (var, val);

      varlist = XCDR (varlist);