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

This file is part of GNU Emacs.

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

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
18
along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
Jim Blandy's avatar
Jim Blandy committed
19 20


21
#include <config.h>
22
#include <limits.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

35 36 37
#if !BYTE_MARK_STACK
static
#endif
Jim Blandy's avatar
Jim Blandy committed
38 39
struct catchtag *catchlist;

40 41 42 43 44 45 46 47 48 49 50
/* 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;

51 52 53 54 55
#ifdef DEBUG_GCPRO
/* Count levels of GCPRO to detect failure to UNGCPRO.  */
int gcpro_level;
#endif

56
Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp;
57
Lisp_Object Qinhibit_quit;
58 59
Lisp_Object Qand_rest;
static Lisp_Object Qand_optional;
60
static Lisp_Object Qinhibit_debugger;
61
static Lisp_Object Qdeclare;
62 63
Lisp_Object Qinternal_interpreter_environment, Qclosure;

Paul Eggert's avatar
Paul Eggert committed
64
static Lisp_Object Qdebug;
Jim Blandy's avatar
Jim Blandy committed
65

66 67 68
/* This holds either the symbol `run-hooks' or nil.
   It is nil at an early stage of startup, and when Emacs
   is shutting down.  */
69

Jim Blandy's avatar
Jim Blandy committed
70 71 72 73 74
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:
75
   (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide.  */
Jim Blandy's avatar
Jim Blandy committed
76 77 78 79

Lisp_Object Vautoload_queue;

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

81
ptrdiff_t specpdl_size;
Jim Blandy's avatar
Jim Blandy committed
82 83

/* Pointer to beginning of specpdl.  */
84

Jim Blandy's avatar
Jim Blandy committed
85 86 87
struct specbinding *specpdl;

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

89
struct specbinding *specpdl_ptr;
Jim Blandy's avatar
Jim Blandy committed
90 91

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

93
static EMACS_INT lisp_eval_depth;
Jim Blandy's avatar
Jim Blandy committed
94

95
/* The value of num_nonmacro_input_events as of the last time we
96
   started to enter the debugger.  If we decide to enter the debugger
97
   again when this is still equal to num_nonmacro_input_events, then we
98 99 100
   know that the debugger itself has an error, and we should just
   signal the error instead of entering an infinite loop of debugger
   invocations.  */
101

102
static EMACS_INT when_entered_debugger;
Jim Blandy's avatar
Jim Blandy committed
103

104 105
/* The function from which the last `signal' was called.  Set in
   Fsignal.  */
106
/* FIXME: We should probably get rid of this!  */
107 108
Lisp_Object Vsignaling_function;

109 110 111 112 113 114
/* 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;

115
static Lisp_Object funcall_lambda (Lisp_Object, ptrdiff_t, Lisp_Object *);
116
static Lisp_Object apply_lambda (Lisp_Object fun, Lisp_Object args);
117

118
/* Functions to modify slots of backtrace records.  */
119

120 121 122 123 124 125 126 127 128 129 130 131 132 133
static void set_backtrace_args (struct specbinding *pdl, Lisp_Object *args)
{ eassert (pdl->kind == SPECPDL_BACKTRACE); pdl->v.bt.args = args; }

static void set_backtrace_nargs (struct specbinding *pdl, ptrdiff_t n)
{ eassert (pdl->kind == SPECPDL_BACKTRACE); pdl->v.bt.nargs = n; }

void set_backtrace_debug_on_exit (struct specbinding *pdl, bool doe)
{ eassert (pdl->kind == SPECPDL_BACKTRACE); pdl->v.bt.debug_on_exit = doe; }

/* Helper functions to scan the backtrace.  */

LISP_INLINE bool backtrace_p (struct specbinding *pdl)
{ return pdl >= specpdl; }
LISP_INLINE struct specbinding *backtrace_top (void)
134
{
135 136 137 138
  struct specbinding *pdl = specpdl_ptr - 1;
  while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)     \
    pdl--;
  return pdl;
139
}
140
LISP_INLINE struct specbinding *backtrace_next (struct specbinding *pdl)
141
{
142 143 144 145
  pdl--;
  while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
    pdl--;
  return pdl;
146 147
}

148

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

  Vrun_hooks = Qnil;
Jim Blandy's avatar
Jim Blandy committed
161 162
}

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

179
/* Unwind-protect function used by call_debugger.  */
180 181

static Lisp_Object
182
restore_stack_limits (Lisp_Object data)
183 184 185
{
  max_specpdl_size = XINT (XCAR (data));
  max_lisp_eval_depth = XINT (XCDR (data));
186
  return Qnil;
187 188 189 190
}

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

191
Lisp_Object
192
call_debugger (Lisp_Object arg)
Jim Blandy's avatar
Jim Blandy committed
193
{
194
  bool debug_while_redisplaying;
195
  ptrdiff_t count = SPECPDL_INDEX ();
196
  Lisp_Object val;
197
  EMACS_INT old_max = max_specpdl_size;
198

199 200
  /* Temporarily bump up the stack limits,
     so the debugger won't run out of stack.  */
201

202 203 204 205 206 207 208 209 210
  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;

211
  if (max_specpdl_size - 100 < SPECPDL_INDEX ())
212
    max_specpdl_size = SPECPDL_INDEX () + 100;
213

214
#ifdef HAVE_WINDOW_SYSTEM
215 216
  if (display_hourglass_p)
    cancel_hourglass ();
217 218
#endif

Jim Blandy's avatar
Jim Blandy committed
219
  debug_on_next_call = 0;
220
  when_entered_debugger = num_nonmacro_input_events;
221 222 223 224 225

  /* 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;
226 227
  specbind (intern ("debugger-may-continue"),
	    debug_while_redisplaying ? Qnil : Qt);
228
  specbind (Qinhibit_redisplay, Qnil);
229
  specbind (Qinhibit_debugger, Qt);
230 231 232

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

236 237 238 239
  val = apply1 (Vdebugger, arg);

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

244
  return unbind_to (count, val);
Jim Blandy's avatar
Jim Blandy committed
245 246
}

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

269
  GCPRO1 (args);
Jim Blandy's avatar
Jim Blandy committed
270

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

  UNGCPRO;
  return val;
}

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

293
  GCPRO1 (args);
Jim Blandy's avatar
Jim Blandy committed
294

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

  UNGCPRO;
  return val;
}

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

  GCPRO1 (args);
319
  cond = eval_sub (Fcar (args));
Jim Blandy's avatar
Jim Blandy committed
320 321
  UNGCPRO;

Jim Blandy's avatar
Jim Blandy committed
322
  if (!NILP (cond))
323
    return eval_sub (Fcar (Fcdr (args)));
Jim Blandy's avatar
Jim Blandy committed
324 325 326 327
  return Fprogn (Fcdr (Fcdr (args)));
}

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

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

  return val;
}

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

369
  GCPRO1 (args);
Jim Blandy's avatar
Jim Blandy committed
370

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

  UNGCPRO;
  return val;
}

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

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

396 397 398
  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
399 400 401 402 403 404

  UNGCPRO;
  return val;
}

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

413 414
  GCPRO1 (args);
  eval_sub (XCAR (args));
415 416
  UNGCPRO;
  return Fprog1 (XCDR (args));
Jim Blandy's avatar
Jim Blandy committed
417 418 419
}

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

434
  if (NILP (args))
Jim Blandy's avatar
Jim Blandy committed
435 436 437 438 439 440 441
    return Qnil;

  args_left = args;
  GCPRO1 (args);

  do
    {
442
      val = eval_sub (Fcar (Fcdr (args_left)));
Jim Blandy's avatar
Jim Blandy committed
443
      sym = Fcar (args_left);
444

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

Jim Blandy's avatar
Jim Blandy committed
455 456
      args_left = Fcdr (Fcdr (args_left));
    }
Juanma Barranquero's avatar
Juanma Barranquero committed
457
  while (!NILP (args_left));
Jim Blandy's avatar
Jim Blandy committed
458 459 460 461

  UNGCPRO;
  return val;
}
462

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

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

490 491
  if (!NILP (Fcdr (args)))
    xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
492 493 494 495 496 497

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

505

506
DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
507
       doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
Richard M. Stallman's avatar
Richard M. Stallman committed
508
Aliased variables always have the same value; setting one sets the other.
509
Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS.  If it is
510 511 512 513
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.
514
The return value is BASE-VARIABLE.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
515
  (Lisp_Object new_alias, Lisp_Object base_variable, Lisp_Object docstring)
Gerd Moellmann's avatar
Gerd Moellmann committed
516 517
{
  struct Lisp_Symbol *sym;
518

519 520
  CHECK_SYMBOL (new_alias);
  CHECK_SYMBOL (base_variable);
Gerd Moellmann's avatar
Gerd Moellmann committed
521

522
  sym = XSYMBOL (new_alias);
523 524

  if (sym->constant)
Stefan Monnier's avatar
Stefan Monnier committed
525 526
    /* Not sure why, but why not?  */
    error ("Cannot make a constant an alias");
527 528 529 530 531 532 533 534 535

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

536
  /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
537 538 539 540
     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)))
541
    set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1);
542 543 544 545

  {
    struct specbinding *p;

Paul Eggert's avatar
Paul Eggert committed
546
    for (p = specpdl_ptr; p > specpdl; )
547 548
      if ((--p)->kind >= SPECPDL_LET
	  && (EQ (new_alias, specpdl_symbol (p))))
549 550 551
	error ("Don't know how to make a let-bound variable an alias");
  }

552
  sym->declared_special = 1;
553
  XSYMBOL (base_variable)->declared_special = 1;
554 555
  sym->redirect = SYMBOL_VARALIAS;
  SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
556 557
  sym->constant = SYMBOL_CONSTANT_P (base_variable);
  LOADHIST_ATTACH (new_alias);
558 559
  /* Even if docstring is nil: remove old docstring.  */
  Fput (new_alias, Qvariable_documentation, docstring);
560

561
  return base_variable;
Gerd Moellmann's avatar
Gerd Moellmann committed
562 563 564
}


Jim Blandy's avatar
Jim Blandy committed
565
DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
Juanma Barranquero's avatar
Juanma Barranquero committed
566
       doc: /* Define SYMBOL as a variable, and return SYMBOL.
567 568 569 570 571 572 573 574 575
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
576
If INITVALUE is missing, SYMBOL's value is not set.
577 578 579 580 581 582 583

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.)
584 585 586 587 588

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

To define a user option, use `defcustom' instead of `defvar'.
589
usage: (defvar SYMBOL &optional INITVALUE DOCSTRING)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
590
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
591
{
592
  register Lisp_Object sym, tem, tail;
Jim Blandy's avatar
Jim Blandy committed
593 594

  sym = Fcar (args);
595 596
  tail = Fcdr (args);
  if (!NILP (Fcdr (Fcdr (tail))))
597
    error ("Too many arguments");
598

599
  tem = Fdefault_boundp (sym);
600
  if (!NILP (tail))
Jim Blandy's avatar
Jim Blandy committed
601
    {
602 603
      /* Do it before evaluating the initial value, for self-references.  */
      XSYMBOL (sym)->declared_special = 1;
604

Jim Blandy's avatar
Jim Blandy committed
605
      if (NILP (tem))
606
	Fset_default (sym, eval_sub (Fcar (tail)));
607 608 609
      else
	{ /* Check if there is really a global binding rather than just a let
	     binding that shadows the global unboundness of the var.  */
610
	  struct specbinding *pdl = specpdl_ptr;
611
	  while (pdl > specpdl)
612
	    {
613 614 615
	      if ((--pdl)->kind >= SPECPDL_LET
		  && EQ (specpdl_symbol (pdl), sym)
		  && EQ (specpdl_old_value (pdl), Qunbound))
616
		{
617 618 619
		  message_with_string
		    ("Warning: defvar ignored because %s is let-bound",
		     SYMBOL_NAME (sym), 1);
620 621 622 623
		  break;
		}
	    }
	}
624
      tail = Fcdr (tail);
625 626
      tem = Fcar (tail);
      if (!NILP (tem))
627 628 629 630 631
	{
	  if (!NILP (Vpurify_flag))
	    tem = Fpurecopy (tem);
	  Fput (sym, Qvariable_documentation, tem);
	}
632
      LOADHIST_ATTACH (sym);
Jim Blandy's avatar
Jim Blandy committed
633
    }
634 635 636 637 638
  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).  */
639 640
    Vinternal_interpreter_environment
      = Fcons (sym, Vinternal_interpreter_environment);
641
  else
642 643 644 645 646
    {
      /* 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
647

Jim Blandy's avatar
Jim Blandy committed
648 649 650 651
  return sym;
}

DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
652
       doc: /* Define SYMBOL as a constant variable.
653 654 655 656 657 658 659 660 661 662 663 664 665
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.
666
usage: (defconst SYMBOL INITVALUE [DOCSTRING])  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
667
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
668 669 670 671
{
  register Lisp_Object sym, tem;

  sym = Fcar (args);
672
  if (!NILP (Fcdr (Fcdr (Fcdr (args)))))
673
    error ("Too many arguments");
674

675
  tem = eval_sub (Fcar (Fcdr (args)));
676 677 678
  if (!NILP (Vpurify_flag))
    tem = Fpurecopy (tem);
  Fset_default (sym, tem);
679
  XSYMBOL (sym)->declared_special = 1;
Jim Blandy's avatar
Jim Blandy committed
680
  tem = Fcar (Fcdr (Fcdr (args)));
Jim Blandy's avatar
Jim Blandy committed
681
  if (!NILP (tem))
Jim Blandy's avatar
Jim Blandy committed
682
    {
Jim Blandy's avatar
Jim Blandy committed
683
      if (!NILP (Vpurify_flag))
Jim Blandy's avatar
Jim Blandy committed
684 685 686
	tem = Fpurecopy (tem);
      Fput (sym, Qvariable_documentation, tem);
    }
687
  Fput (sym, Qrisky_local_variable, Qt);
688
  LOADHIST_ATTACH (sym);
Jim Blandy's avatar
Jim Blandy committed
689 690 691
  return sym;
}

692 693 694 695 696 697 698 699 700 701 702
/* 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
703 704

DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
705 706 707 708 709
       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.
710
usage: (let* VARLIST BODY...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
711
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
712
{
713
  Lisp_Object varlist, var, val, elt, lexenv;
714
  ptrdiff_t count = SPECPDL_INDEX ();
Jim Blandy's avatar
Jim Blandy committed
715 716 717 718
  struct gcpro gcpro1, gcpro2, gcpro3;

  GCPRO3 (args, elt, varlist);

719 720
  lexenv = Vinternal_interpreter_environment;

Jim Blandy's avatar
Jim Blandy committed
721
  varlist = Fcar (args);
722
  while (CONSP (varlist))
Jim Blandy's avatar
Jim Blandy committed
723 724
    {
      QUIT;
725 726

      elt = XCAR (varlist);
727
      if (SYMBOLP (elt))
728 729 730 731
	{
	  var = elt;
	  val = Qnil;
	}
Jim Blandy's avatar
Jim Blandy committed
732
      else if (! NILP (Fcdr (Fcdr (elt))))
733
	signal_error ("`let' bindings can have only one value-form", elt);
Jim Blandy's avatar
Jim Blandy committed
734 735
      else
	{
736
	  var = Fcar (elt);
737
	  val = eval_sub (Fcar (Fcdr (elt)));
Jim Blandy's avatar
Jim Blandy committed
738
	}
739

740 741 742
      if (!NILP (lexenv) && SYMBOLP (var)
	  && !XSYMBOL (var)->declared_special
	  && NILP (Fmemq (var, Vinternal_interpreter_environment)))
743 744 745
	/* Lexically bind VAR by adding it to the interpreter's binding
	   alist.  */
	{
746 747 748 749 750 751 752 753 754
	  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
755
	}
756 757 758 759
      else
	specbind (var, val);

      varlist = XCDR (varlist);
Jim Blandy's avatar
Jim Blandy committed
760 761 762 763 764 765 766
    }
  UNGCPRO;
  val = Fprogn (Fcdr (args));
  return unbind_to (count, val);
}

DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
767 768 769 770 771
       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).
All the VALUEFORMs are evalled before any symbols are bound.
772
usage: (let VARLIST BODY...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
773
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
774
{
775
  Lisp_Object *temps, tem, lexenv;
Jim Blandy's avatar
Jim Blandy committed
776
  register Lisp_Object elt, varlist;
777
  ptrdiff_t count = SPECPDL_INDEX ();
778
  ptrdiff_t argnum;
Jim Blandy's avatar
Jim Blandy committed
779
  struct gcpro gcpro1, gcpro2;
780
  USE_SAFE_ALLOCA;
Jim Blandy's avatar
Jim Blandy committed
781 782 783

  varlist = Fcar (args);

784
  /* Make space to hold the values to give the bound variables.  */
Jim Blandy's avatar
Jim Blandy committed
785
  elt = Flength (varlist);
786
  SAFE_ALLOCA_LISP (temps, XFASTINT (elt));
Jim Blandy's avatar
Jim Blandy committed
787

788
  /* Compute the values and store them in `temps'.  */
Jim Blandy's avatar
Jim Blandy committed
789 790 791 792

  GCPRO2 (args, *temps);
  gcpro2.nvars = 0;

793
  for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
Jim Blandy's avatar
Jim Blandy committed
794 795
    {
      QUIT;
796
      elt = XCAR (varlist);
797
      if (SYMBOLP (elt))
Jim Blandy's avatar
Jim Blandy committed
798
	temps [argnum++] = Qnil;
Jim Blandy's avatar
Jim Blandy committed
799
      else if (! NILP (Fcdr (Fcdr (elt))))
800
	signal_error ("`let' bindings can have only one value-form", elt);
Jim Blandy's avatar
Jim Blandy committed
801
      else
802
	temps [argnum++] = eval_sub (Fcar (Fcdr (elt)));
Jim Blandy's avatar
Jim Blandy committed
803 804 805 806
      gcpro2.nvars = argnum;
    }
  UNGCPRO;

807 808
  lexenv = Vinternal_interpreter_environment;

Jim Blandy's avatar
Jim Blandy committed
809
  varlist = Fcar (args);
810
  for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
Jim Blandy's avatar
Jim Blandy committed
811
    {
812 813
      Lisp_Object var;

814
      elt = XCAR (varlist);
815
      var = SYMBOLP (elt) ? elt : Fcar (elt);
Jim Blandy's avatar
Jim Blandy committed
816
      tem = temps[argnum++];
817

818 819 820
      if (!NILP (lexenv) && SYMBOLP (var)
	  && !XSYMBOL (var)->declared_special
	  && NILP (Fmemq (var, Vinternal_interpreter_environment)))
821 822
	/* Lexically bind VAR by adding it to the lexenv alist.  */
	lexenv = Fcons (Fcons (var, tem), lexenv);
Jim Blandy's avatar
Jim Blandy committed
823
      else
824 825
	/* Dynamically bind VAR.  */
	specbind (var, tem);
Jim Blandy's avatar
Jim Blandy committed
826 827
    }

828 829 830 831
  if (!EQ (lexenv, Vinternal_interpreter_environment))
    /* Instantiate a new lexical environment.  */
    specbind (Qinternal_interpreter_environment, lexenv);

Jim Blandy's avatar
Jim Blandy committed
832