eval.c 112 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Evaluator for GNU Emacs Lisp interpreter.
Glenn Morris's avatar
Glenn Morris committed
2

3 4
Copyright (C) 1985-1987, 1993-1995, 1999-2014 Free Software Foundation,
Inc.
Jim Blandy's avatar
Jim Blandy committed
5 6 7

This file is part of GNU Emacs.

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

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


22
#include <config.h>
23
#include <limits.h>
24
#include <stdio.h>
Jim Blandy's avatar
Jim Blandy committed
25
#include "lisp.h"
26
#include "blockinput.h"
Jim Blandy's avatar
Jim Blandy committed
27
#include "commands.h"
Jim Blandy's avatar
Jim Blandy committed
28
#include "keyboard.h"
29
#include "dispextern.h"
30

31
/* Chain of condition and catch handlers currently in effect.  */
32 33 34

struct handler *handlerlist;

35 36 37 38 39
#ifdef DEBUG_GCPRO
/* Count levels of GCPRO to detect failure to UNGCPRO.  */
int gcpro_level;
#endif

40
Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp;
41
Lisp_Object Qinhibit_quit;
42 43
Lisp_Object Qand_rest;
static Lisp_Object Qand_optional;
44
static Lisp_Object Qinhibit_debugger;
45
static Lisp_Object Qdeclare;
46 47
Lisp_Object Qinternal_interpreter_environment, Qclosure;

Paul Eggert's avatar
Paul Eggert committed
48
static Lisp_Object Qdebug;
Jim Blandy's avatar
Jim Blandy committed
49

50 51 52
/* This holds either the symbol `run-hooks' or nil.
   It is nil at an early stage of startup, and when Emacs
   is shutting down.  */
53

Jim Blandy's avatar
Jim Blandy committed
54 55 56 57 58
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:
59
   (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide.  */
Jim Blandy's avatar
Jim Blandy committed
60 61 62

Lisp_Object Vautoload_queue;

63 64
/* Current number of specbindings allocated in specpdl, not counting
   the dummy entry specpdl[-1].  */
65

66
ptrdiff_t specpdl_size;
Jim Blandy's avatar
Jim Blandy committed
67

68 69
/* Pointer to beginning of specpdl.  A dummy entry specpdl[-1] exists
   only so that its address can be taken.  */
70

71
union specbinding *specpdl;
Jim Blandy's avatar
Jim Blandy committed
72 73

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

75
union specbinding *specpdl_ptr;
Jim Blandy's avatar
Jim Blandy committed
76 77

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

79
EMACS_INT lisp_eval_depth;
Jim Blandy's avatar
Jim Blandy committed
80

81
/* The value of num_nonmacro_input_events as of the last time we
82
   started to enter the debugger.  If we decide to enter the debugger
83
   again when this is still equal to num_nonmacro_input_events, then we
84 85 86
   know that the debugger itself has an error, and we should just
   signal the error instead of entering an infinite loop of debugger
   invocations.  */
87

88
static EMACS_INT when_entered_debugger;
Jim Blandy's avatar
Jim Blandy committed
89

90 91
/* The function from which the last `signal' was called.  Set in
   Fsignal.  */
92
/* FIXME: We should probably get rid of this!  */
93 94
Lisp_Object Vsignaling_function;

95 96 97 98 99 100
/* 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;

101 102 103 104 105 106 107
/* These would ordinarily be static, but they need to be visible to GDB.  */
bool backtrace_p (union specbinding *) EXTERNALLY_VISIBLE;
Lisp_Object *backtrace_args (union specbinding *) EXTERNALLY_VISIBLE;
Lisp_Object backtrace_function (union specbinding *) EXTERNALLY_VISIBLE;
union specbinding *backtrace_next (union specbinding *) EXTERNALLY_VISIBLE;
union specbinding *backtrace_top (void) EXTERNALLY_VISIBLE;

108
static Lisp_Object funcall_lambda (Lisp_Object, ptrdiff_t, Lisp_Object *);
109
static Lisp_Object apply_lambda (Lisp_Object fun, Lisp_Object args);
110

111
static Lisp_Object
112
specpdl_symbol (union specbinding *pdl)
113 114
{
  eassert (pdl->kind >= SPECPDL_LET);
115
  return pdl->let.symbol;
116 117 118
}

static Lisp_Object
119
specpdl_old_value (union specbinding *pdl)
120 121
{
  eassert (pdl->kind >= SPECPDL_LET);
122
  return pdl->let.old_value;
123 124
}

125 126 127 128 129 130 131
static void
set_specpdl_old_value (union specbinding *pdl, Lisp_Object val)
{
  eassert (pdl->kind >= SPECPDL_LET);
  pdl->let.old_value = val;
}

132
static Lisp_Object
133
specpdl_where (union specbinding *pdl)
134 135
{
  eassert (pdl->kind > SPECPDL_LET);
136
  return pdl->let.where;
137 138 139
}

static Lisp_Object
140
specpdl_arg (union specbinding *pdl)
141 142
{
  eassert (pdl->kind == SPECPDL_UNWIND);
143
  return pdl->unwind.arg;
144 145
}

146
Lisp_Object
147
backtrace_function (union specbinding *pdl)
148 149
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
150
  return pdl->bt.function;
151 152 153
}

static ptrdiff_t
154
backtrace_nargs (union specbinding *pdl)
155 156
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
157
  return pdl->bt.nargs;
158 159
}

160
Lisp_Object *
161
backtrace_args (union specbinding *pdl)
162 163
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
164
  return pdl->bt.args;
165 166 167
}

static bool
168
backtrace_debug_on_exit (union specbinding *pdl)
169 170
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
171
  return pdl->bt.debug_on_exit;
172 173
}

174
/* Functions to modify slots of backtrace records.  */
175

176
static void
177 178 179 180 181
set_backtrace_args (union specbinding *pdl, Lisp_Object *args)
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
  pdl->bt.args = args;
}
182

183
static void
184 185 186 187 188
set_backtrace_nargs (union specbinding *pdl, ptrdiff_t n)
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
  pdl->bt.nargs = n;
}
189

190
static void
191 192 193 194 195
set_backtrace_debug_on_exit (union specbinding *pdl, bool doe)
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
  pdl->bt.debug_on_exit = doe;
}
196 197 198

/* Helper functions to scan the backtrace.  */

199 200
bool
backtrace_p (union specbinding *pdl)
201
{ return pdl >= specpdl; }
202

203
union specbinding *
204
backtrace_top (void)
205
{
206
  union specbinding *pdl = specpdl_ptr - 1;
207
  while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
208 209
    pdl--;
  return pdl;
210
}
211

212 213
union specbinding *
backtrace_next (union specbinding *pdl)
214
{
215 216 217 218
  pdl--;
  while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
    pdl--;
  return pdl;
219 220
}

221

Andreas Schwab's avatar
Andreas Schwab committed
222
void
223
init_eval_once (void)
Jim Blandy's avatar
Jim Blandy committed
224
{
225
  enum { size = 50 };
226
  union specbinding *pdlvec = xmalloc ((size + 1) * sizeof *specpdl);
227
  specpdl_size = size;
228
  specpdl = specpdl_ptr = pdlvec + 1;
229
  /* Don't forget to update docs (lispref node "Local Variables").  */
Stefan Monnier's avatar
Stefan Monnier committed
230
  max_specpdl_size = 1300; /* 1000 is not enough for CEDET's c-by.el.  */
231
  max_lisp_eval_depth = 600;
232 233

  Vrun_hooks = Qnil;
Jim Blandy's avatar
Jim Blandy committed
234 235
}

236 237
static struct handler handlerlist_sentinel;

Andreas Schwab's avatar
Andreas Schwab committed
238
void
239
init_eval (void)
Jim Blandy's avatar
Jim Blandy committed
240 241
{
  specpdl_ptr = specpdl;
242 243 244 245 246 247 248 249 250 251
  { /* Put a dummy catcher at top-level so that handlerlist is never NULL.
       This is important since handlerlist->nextfree holds the freelist
       which would otherwise leak every time we unwind back to top-level.   */
    struct handler *c;
    handlerlist = handlerlist_sentinel.nextfree = &handlerlist_sentinel;
    PUSH_HANDLER (c, Qunbound, CATCHER);
    eassert (c == &handlerlist_sentinel);
    handlerlist_sentinel.nextfree = NULL;
    handlerlist_sentinel.next = NULL;
  }
Jim Blandy's avatar
Jim Blandy committed
252 253 254
  Vquit_flag = Qnil;
  debug_on_next_call = 0;
  lisp_eval_depth = 0;
255
#ifdef DEBUG_GCPRO
256
  gcpro_level = 0;
257
#endif
258
  /* This is less than the initial value of num_nonmacro_input_events.  */
259
  when_entered_debugger = -1;
Jim Blandy's avatar
Jim Blandy committed
260 261
}

262
/* Unwind-protect function used by call_debugger.  */
263

264
static void
265
restore_stack_limits (Lisp_Object data)
266 267 268 269 270
{
  max_specpdl_size = XINT (XCAR (data));
  max_lisp_eval_depth = XINT (XCDR (data));
}

271 272
static void grow_specpdl (void);

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

275
Lisp_Object
276
call_debugger (Lisp_Object arg)
Jim Blandy's avatar
Jim Blandy committed
277
{
278
  bool debug_while_redisplaying;
279
  ptrdiff_t count = SPECPDL_INDEX ();
280
  Lisp_Object val;
281 282 283
  EMACS_INT old_depth = max_lisp_eval_depth;
  /* Do not allow max_specpdl_size less than actual depth (Bug#16603).  */
  EMACS_INT old_max = max (max_specpdl_size, count);
284 285 286 287

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

288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
  /* While debugging Bug#16603, previous value of 100 was found
     too small to avoid specpdl overflow in the debugger itself.  */
  if (max_specpdl_size - 200 < count)
    max_specpdl_size = count + 200;

  if (old_max == count)
    {
      /* We can enter the debugger due to specpdl overflow (Bug#16603).  */
      specpdl_ptr--;
      grow_specpdl ();
    }

  /* Restore limits after leaving the debugger.  */
  record_unwind_protect (restore_stack_limits,
			 Fcons (make_number (old_max),
				make_number (old_depth)));
304

305
#ifdef HAVE_WINDOW_SYSTEM
306 307
  if (display_hourglass_p)
    cancel_hourglass ();
308 309
#endif

Jim Blandy's avatar
Jim Blandy committed
310
  debug_on_next_call = 0;
311
  when_entered_debugger = num_nonmacro_input_events;
312 313 314 315 316

  /* 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;
317 318
  specbind (intern ("debugger-may-continue"),
	    debug_while_redisplaying ? Qnil : Qt);
319
  specbind (Qinhibit_redisplay, Qnil);
320
  specbind (Qinhibit_debugger, Qt);
321 322 323

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

327 328 329 330
  val = apply1 (Vdebugger, arg);

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

335
  return unbind_to (count, val);
Jim Blandy's avatar
Jim Blandy committed
336 337
}

338
static void
339
do_debug_on_call (Lisp_Object code)
Jim Blandy's avatar
Jim Blandy committed
340 341
{
  debug_on_next_call = 0;
342
  set_backtrace_debug_on_exit (specpdl_ptr - 1, true);
343
  call_debugger (list1 (code));
Jim Blandy's avatar
Jim Blandy committed
344 345 346 347 348 349 350
}

/* 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
351 352 353
       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
354
usage: (or CONDITIONS...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
355
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
356
{
357
  register Lisp_Object val = Qnil;
Jim Blandy's avatar
Jim Blandy committed
358 359
  struct gcpro gcpro1;

360
  GCPRO1 (args);
Jim Blandy's avatar
Jim Blandy committed
361

362
  while (CONSP (args))
Jim Blandy's avatar
Jim Blandy committed
363
    {
364
      val = eval_sub (XCAR (args));
Jim Blandy's avatar
Jim Blandy committed
365
      if (!NILP (val))
Jim Blandy's avatar
Jim Blandy committed
366
	break;
367
      args = XCDR (args);
Jim Blandy's avatar
Jim Blandy committed
368 369 370 371 372 373 374
    }

  UNGCPRO;
  return val;
}

DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
375
       doc: /* Eval args until one of them yields nil, then return nil.
Pavel Janík's avatar
Pavel Janík committed
376 377
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
378
usage: (and CONDITIONS...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
379
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
380
{
381
  register Lisp_Object val = Qt;
Jim Blandy's avatar
Jim Blandy committed
382 383
  struct gcpro gcpro1;

384
  GCPRO1 (args);
Jim Blandy's avatar
Jim Blandy committed
385

386
  while (CONSP (args))
Jim Blandy's avatar
Jim Blandy committed
387
    {
388
      val = eval_sub (XCAR (args));
Jim Blandy's avatar
Jim Blandy committed
389
      if (NILP (val))
Jim Blandy's avatar
Jim Blandy committed
390
	break;
391
      args = XCDR (args);
Jim Blandy's avatar
Jim Blandy committed
392 393 394 395 396 397 398
    }

  UNGCPRO;
  return val;
}

DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
399
       doc: /* If COND yields non-nil, do THEN, else do ELSE...
Pavel Janík's avatar
Pavel Janík committed
400 401 402
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.
403
usage: (if COND THEN ELSE...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
404
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
405
{
406
  Lisp_Object cond;
Jim Blandy's avatar
Jim Blandy committed
407 408 409
  struct gcpro gcpro1;

  GCPRO1 (args);
410
  cond = eval_sub (XCAR (args));
Jim Blandy's avatar
Jim Blandy committed
411 412
  UNGCPRO;

Jim Blandy's avatar
Jim Blandy committed
413
  if (!NILP (cond))
414 415
    return eval_sub (Fcar (XCDR (args)));
  return Fprogn (XCDR (XCDR (args)));
Jim Blandy's avatar
Jim Blandy committed
416 417 418
}

DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
419 420 421 422 423
       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.
Glenn Morris's avatar
Glenn Morris committed
424 425
If a clause has one element, as in (CONDITION), then the cond-form
returns CONDITION's value, if that is non-nil.
Pavel Janík's avatar
Pavel Janík committed
426
If no clause succeeds, cond returns nil.
427
usage: (cond CLAUSES...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
428
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
429
{
430
  Lisp_Object val = args;
Jim Blandy's avatar
Jim Blandy committed
431 432 433
  struct gcpro gcpro1;

  GCPRO1 (args);
434
  while (CONSP (args))
Jim Blandy's avatar
Jim Blandy committed
435
    {
436
      Lisp_Object clause = XCAR (args);
437
      val = eval_sub (Fcar (clause));
Jim Blandy's avatar
Jim Blandy committed
438
      if (!NILP (val))
Jim Blandy's avatar
Jim Blandy committed
439
	{
440
	  if (!NILP (XCDR (clause)))
441
	    val = Fprogn (XCDR (clause));
Jim Blandy's avatar
Jim Blandy committed
442 443
	  break;
	}
444
      args = XCDR (args);
Jim Blandy's avatar
Jim Blandy committed
445 446 447 448 449 450
    }
  UNGCPRO;

  return val;
}

Paul Eggert's avatar
Paul Eggert committed
451
DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
452
       doc: /* Eval BODY forms sequentially and return value of last one.
Juanma Barranquero's avatar
Juanma Barranquero committed
453
usage: (progn BODY...)  */)
454
  (Lisp_Object body)
Jim Blandy's avatar
Jim Blandy committed
455
{
456
  Lisp_Object val = Qnil;
Jim Blandy's avatar
Jim Blandy committed
457 458
  struct gcpro gcpro1;

459
  GCPRO1 (body);
Jim Blandy's avatar
Jim Blandy committed
460

461
  while (CONSP (body))
Jim Blandy's avatar
Jim Blandy committed
462
    {
463 464
      val = eval_sub (XCAR (body));
      body = XCDR (body);
Jim Blandy's avatar
Jim Blandy committed
465 466 467 468 469 470
    }

  UNGCPRO;
  return val;
}

Paul Eggert's avatar
Paul Eggert committed
471
/* Evaluate BODY sequentially, discarding its value.  Suitable for
472 473 474 475 476 477 478 479
   record_unwind_protect.  */

void
unwind_body (Lisp_Object body)
{
  Fprogn (body);
}

Jim Blandy's avatar
Jim Blandy committed
480
DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
481
       doc: /* Eval FIRST and BODY sequentially; return value from FIRST.
Pavel Janík's avatar
Pavel Janík committed
482 483
The value of FIRST is saved during the evaluation of the remaining args,
whose values are discarded.
484
usage: (prog1 FIRST BODY...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
485
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
486 487
{
  Lisp_Object val;
488
  Lisp_Object args_left;
Jim Blandy's avatar
Jim Blandy committed
489 490 491
  struct gcpro gcpro1, gcpro2;

  args_left = args;
492
  val = args;
Jim Blandy's avatar
Jim Blandy committed
493 494
  GCPRO2 (args, val);

495 496 497
  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
498 499 500 501 502 503

  UNGCPRO;
  return val;
}

DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
504
       doc: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
505 506 507
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
508
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
509
{
510
  struct gcpro gcpro1;
Jim Blandy's avatar
Jim Blandy committed
511

512 513
  GCPRO1 (args);
  eval_sub (XCAR (args));
514 515
  UNGCPRO;
  return Fprog1 (XCDR (args));
Jim Blandy's avatar
Jim Blandy committed
516 517 518
}

DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
519 520 521 522 523 524 525
       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
526
usage: (setq [SYM VAL]...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
527
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
528
{
529
  Lisp_Object val, sym, lex_binding;
Jim Blandy's avatar
Jim Blandy committed
530

531 532 533 534 535 536
  val = args;
  if (CONSP (args))
    {
      Lisp_Object args_left = args;
      struct gcpro gcpro1;
      GCPRO1 (args);
Jim Blandy's avatar
Jim Blandy committed
537

538 539 540 541 542 543 544 545 546 547 548 549 550 551
      do
	{
	  val = eval_sub (Fcar (XCDR (args_left)));
	  sym = XCAR (args_left);

	  /* Like for eval_sub, we do not check declared_special here since
	     it's been done when let-binding.  */
	  if (!NILP (Vinternal_interpreter_environment) /* Mere optimization!  */
	      && SYMBOLP (sym)
	      && !NILP (lex_binding
			= Fassq (sym, Vinternal_interpreter_environment)))
	    XSETCDR (lex_binding, val); /* SYM is lexically bound.  */
	  else
	    Fset (sym, val);	/* SYM is dynamically bound.  */
Jim Blandy's avatar
Jim Blandy committed
552

553 554 555
	  args_left = Fcdr (XCDR (args_left));
	}
      while (CONSP (args_left));
556

557
      UNGCPRO;
Jim Blandy's avatar
Jim Blandy committed
558 559 560 561
    }

  return val;
}
562

Jim Blandy's avatar
Jim Blandy committed
563
DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
564
       doc: /* Return the argument, without evaluating it.  `(quote x)' yields `x'.
565 566 567 568 569 570 571 572
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
573
usage: (quote ARG)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
574
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
575
{
576
  if (CONSP (XCDR (args)))
577
    xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args));
578
  return XCAR (args);
Jim Blandy's avatar
Jim Blandy committed
579
}
580

Jim Blandy's avatar
Jim Blandy committed
581
DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
582 583 584 585
       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
586
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
587
{
588 589
  Lisp_Object quoted = XCAR (args);

590
  if (CONSP (XCDR (args)))
591
    xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
592 593 594 595 596 597

  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.  */
598 599
    return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment,
				   XCDR (quoted)));
600 601 602
  else
    /* Simply quote the argument.  */
    return quoted;
Jim Blandy's avatar
Jim Blandy committed
603 604
}

605

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

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

622
  sym = XSYMBOL (new_alias);
623 624

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

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

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

  {
644
    union specbinding *p;
645

Paul Eggert's avatar
Paul Eggert committed
646
    for (p = specpdl_ptr; p > specpdl; )
647 648
      if ((--p)->kind >= SPECPDL_LET
	  && (EQ (new_alias, specpdl_symbol (p))))
649 650 651
	error ("Don't know how to make a let-bound variable an alias");
  }

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

661
  return base_variable;
Gerd Moellmann's avatar
Gerd Moellmann committed
662 663
}

664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
static union specbinding *
default_toplevel_binding (Lisp_Object symbol)
{
  union specbinding *binding = NULL;
  union specbinding *pdl = specpdl_ptr;
  while (pdl > specpdl)
    {
      switch ((--pdl)->kind)
	{
	case SPECPDL_LET_DEFAULT:
	case SPECPDL_LET:
	  if (EQ (specpdl_symbol (pdl), symbol))
	    binding = pdl;
	  break;
	}
    }
  return binding;
}

DEFUN ("default-toplevel-value", Fdefault_toplevel_value, Sdefault_toplevel_value, 1, 1, 0,
       doc: /* Return SYMBOL's toplevel default value.
"Toplevel" means outside of any let binding.  */)
  (Lisp_Object symbol)
{
  union specbinding *binding = default_toplevel_binding (symbol);
  Lisp_Object value
    = binding ? specpdl_old_value (binding) : Fdefault_value (symbol);
  if (!EQ (value, Qunbound))
    return value;
  xsignal1 (Qvoid_variable, symbol);
}

DEFUN ("set-default-toplevel-value", Fset_default_toplevel_value,
       Sset_default_toplevel_value, 2, 2, 0,
       doc: /* Set SYMBOL's toplevel default value to VALUE.
"Toplevel" means outside of any let binding.  */)
     (Lisp_Object symbol, Lisp_Object value)
{
  union specbinding *binding = default_toplevel_binding (symbol);
  if (binding)
    set_specpdl_old_value (binding, value);
  else
    Fset_default (symbol, value);
  return Qnil;
}
Gerd Moellmann's avatar
Gerd Moellmann committed
709

Jim Blandy's avatar
Jim Blandy committed
710
DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
Juanma Barranquero's avatar
Juanma Barranquero committed
711
       doc: /* Define SYMBOL as a variable, and return SYMBOL.
712 713 714 715 716 717 718 719 720
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
721
If INITVALUE is missing, SYMBOL's value is not set.
722 723 724 725 726 727 728

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.)
729 730 731 732 733

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

To define a user option, use `defcustom' instead of `defvar'.
734
usage: (defvar SYMBOL &optional INITVALUE DOCSTRING)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
735
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
736
{
737
  Lisp_Object sym, tem, tail;
Jim Blandy's avatar
Jim Blandy committed
738

739 740
  sym = XCAR (args);
  tail = XCDR (args);
741

742
  if (CONSP (tail))
Jim Blandy's avatar
Jim Blandy committed
743
    {
744 745 746 747 748
      if (CONSP (XCDR (tail)) && CONSP (XCDR (XCDR (tail))))
	error ("Too many arguments");

      tem = Fdefault_boundp (sym);

749 750
      /* Do it before evaluating the initial value, for self-references.  */
      XSYMBOL (sym)->declared_special = 1;
751

Jim Blandy's avatar
Jim Blandy committed
752
      if (NILP (tem))
753
	Fset_default (sym, eval_sub (XCAR (tail)));
754 755 756
      else
	{ /* Check if there is really a global binding rather than just a let
	     binding that shadows the global unboundness of the var.  */
757 758
	  union specbinding *binding = default_toplevel_binding (sym);
	  if (binding && EQ (specpdl_old_value (binding), Qunbound))
759
	    {
760
	      set_specpdl_old_value (binding, eval_sub (XCAR (tail)));
761 762
	    }
	}
763
      tail = XCDR (tail);
764 765
      tem = Fcar (tail);
      if (!NILP (tem))
766 767 768 769 770
	{
	  if (!NILP (Vpurify_flag))
	    tem = Fpurecopy (tem);
	  Fput (sym, Qvariable_documentation, tem);
	}
771
      LOADHIST_ATTACH (sym);
Jim Blandy's avatar
Jim Blandy committed
772
    }
773 774 775 776 777
  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).  */
778 779
    Vinternal_interpreter_environment
      = Fcons (sym, Vinternal_interpreter_environment);
780
  else
781 782 783 784 785
    {
      /* 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
786

Jim Blandy's avatar
Jim Blandy committed
787 788 789 790
  return sym;
}

DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
791
       doc: /* Define SYMBOL as a constant variable.
792 793 794 795 796 797 798 799 800 801 802 803 804
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.
805
usage: (defconst SYMBOL INITVALUE [DOCSTRING])  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
806
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
807
{
808
  Lisp_Object sym, tem;
Jim Blandy's avatar
Jim Blandy committed
809

810 811
  sym = XCAR (args);
  if (CONSP (Fcdr (XCDR (XCDR (args)))))
812
    error ("Too many arguments");
813

814
  tem = eval_sub (Fcar (XCDR (args)));
815 816 817
  if (!NILP (Vpurify_flag))
    tem = Fpurecopy (tem);
  Fset_default (sym, tem);
818
  XSYMBOL (sym)->declared_special = 1;
819
  tem = Fcar (XCDR (XCDR (args)));
Jim Blandy's avatar
Jim Blandy committed
820
  if (!NILP (tem))
Jim Blandy's avatar
Jim Blandy committed
821
    {
Jim Blandy's avatar
Jim Blandy committed
822
      if (!NILP (Vpurify_flag))
Jim Blandy's avatar
Jim Blandy committed
823 824 825
	tem = Fpurecopy (tem);
      Fput (sym, Qvariable_documentation, tem);
    }
826
  Fput (sym, Qrisky_local_variable, Qt);
827
  LOADHIST_ATTACH (sym);
Jim Blandy's avatar
Jim Blandy committed
828 829 830
  return sym;
}

831 832 833 834 835 836 837 838 839 840 841
/* 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
842 843

DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
844 845 846 847 848
       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.
849
usage: (let* VARLIST BODY...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
850
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
851
{
852
  Lisp_Object varlist, var, val, elt, lexenv;
853
  ptrdiff_t count = SPECPDL_INDEX ();
Jim Blandy's avatar
Jim Blandy committed
854 855 856 857
  struct gcpro gcpro1, gcpro2, gcpro3;

  GCPRO3 (args, elt, varlist);

858 859
  lexenv = Vinternal_interpreter_environment;

860
  varlist = XCAR (args);
861
  while (CONSP (varlist))
Jim Blandy's avatar
Jim Blandy committed
862 863
    {
      QUIT;
864 865

      elt = XCAR (varlist);
866
      if (SYMBOLP (elt))
867 868 869 870
	{
	  var = elt;
	  val = Qnil;
	}
Jim Blandy's avatar
Jim Blandy committed
871
      else if (! NILP (Fcdr (Fcdr (elt))))
872
	signal_error ("`let' bindings can have only one value-form", elt);
Jim Blandy's avatar
Jim Blandy committed
873 874
      else
	{
875
	  var = Fcar (elt);
876
	  val = eval_sub (Fcar (Fcdr (elt)));
Jim Blandy's avatar
Jim Blandy committed
877
	}
878

879 880 881
      if (!NILP (lexenv) && SYMBOLP (var)
	  && !XSYMBOL (var)->declared_special
	  && NILP (Fmemq (var, Vinternal_interpreter_environment)))
882 883 884
	/* Lexically bind VAR by adding it to the interpreter's binding
	   alist.  */
	{
885 886 887 888 889 890 891 892 893
	  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
894
	}
895 896 897 898
      else
	specbind (var, val);

      varlist = XCDR (varlist);
Jim Blandy's avatar