eval.c 109 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
/* Chain of condition and catch handlers currently in effect.  */
36 37 38

struct handler *handlerlist;

39 40 41 42 43
#ifdef DEBUG_GCPRO
/* Count levels of GCPRO to detect failure to UNGCPRO.  */
int gcpro_level;
#endif

44
Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp;
45
Lisp_Object Qinhibit_quit;
46 47
Lisp_Object Qand_rest;
static Lisp_Object Qand_optional;
48
static Lisp_Object Qinhibit_debugger;
49
static Lisp_Object Qdeclare;
50 51
Lisp_Object Qinternal_interpreter_environment, Qclosure;

Paul Eggert's avatar
Paul Eggert committed
52
static Lisp_Object Qdebug;
Jim Blandy's avatar
Jim Blandy committed
53

54 55 56
/* This holds either the symbol `run-hooks' or nil.
   It is nil at an early stage of startup, and when Emacs
   is shutting down.  */
57

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

Lisp_Object Vautoload_queue;

67 68
/* Current number of specbindings allocated in specpdl, not counting
   the dummy entry specpdl[-1].  */
69

70
ptrdiff_t specpdl_size;
Jim Blandy's avatar
Jim Blandy committed
71

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

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

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

79
union specbinding *specpdl_ptr;
Jim Blandy's avatar
Jim Blandy committed
80 81

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

83
EMACS_INT lisp_eval_depth;
Jim Blandy's avatar
Jim Blandy committed
84

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

92
static EMACS_INT when_entered_debugger;
Jim Blandy's avatar
Jim Blandy committed
93

94 95
/* The function from which the last `signal' was called.  Set in
   Fsignal.  */
96
/* FIXME: We should probably get rid of this!  */
97 98
Lisp_Object Vsignaling_function;

99 100 101 102 103 104
/* 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;

105 106 107 108 109 110 111
/* 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;

112
static Lisp_Object funcall_lambda (Lisp_Object, ptrdiff_t, Lisp_Object *);
113
static Lisp_Object apply_lambda (Lisp_Object fun, Lisp_Object args);
114

115
static Lisp_Object
116
specpdl_symbol (union specbinding *pdl)
117 118
{
  eassert (pdl->kind >= SPECPDL_LET);
119
  return pdl->let.symbol;
120 121 122
}

static Lisp_Object
123
specpdl_old_value (union specbinding *pdl)
124 125
{
  eassert (pdl->kind >= SPECPDL_LET);
126
  return pdl->let.old_value;
127 128
}

129 130 131 132 133 134 135
static void
set_specpdl_old_value (union specbinding *pdl, Lisp_Object val)
{
  eassert (pdl->kind >= SPECPDL_LET);
  pdl->let.old_value = val;
}

136
static Lisp_Object
137
specpdl_where (union specbinding *pdl)
138 139
{
  eassert (pdl->kind > SPECPDL_LET);
140
  return pdl->let.where;
141 142 143
}

static Lisp_Object
144
specpdl_arg (union specbinding *pdl)
145 146
{
  eassert (pdl->kind == SPECPDL_UNWIND);
147
  return pdl->unwind.arg;
148 149
}

150
Lisp_Object
151
backtrace_function (union specbinding *pdl)
152 153
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
154
  return pdl->bt.function;
155 156 157
}

static ptrdiff_t
158
backtrace_nargs (union specbinding *pdl)
159 160
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
161
  return pdl->bt.nargs;
162 163
}

164
Lisp_Object *
165
backtrace_args (union specbinding *pdl)
166 167
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
168
  return pdl->bt.args;
169 170 171
}

static bool
172
backtrace_debug_on_exit (union specbinding *pdl)
173 174
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
175
  return pdl->bt.debug_on_exit;
176 177
}

178
/* Functions to modify slots of backtrace records.  */
179

180
static void
181 182 183 184 185
set_backtrace_args (union specbinding *pdl, Lisp_Object *args)
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
  pdl->bt.args = args;
}
186

187
static void
188 189 190 191 192
set_backtrace_nargs (union specbinding *pdl, ptrdiff_t n)
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
  pdl->bt.nargs = n;
}
193

194
static void
195 196 197 198 199
set_backtrace_debug_on_exit (union specbinding *pdl, bool doe)
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
  pdl->bt.debug_on_exit = doe;
}
200 201 202

/* Helper functions to scan the backtrace.  */

203 204
bool
backtrace_p (union specbinding *pdl)
205
{ return pdl >= specpdl; }
206

207
union specbinding *
208
backtrace_top (void)
209
{
210
  union specbinding *pdl = specpdl_ptr - 1;
211
  while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
212 213
    pdl--;
  return pdl;
214
}
215

216 217
union specbinding *
backtrace_next (union specbinding *pdl)
218
{
219 220 221 222
  pdl--;
  while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
    pdl--;
  return pdl;
223 224
}

225

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

  Vrun_hooks = Qnil;
Jim Blandy's avatar
Jim Blandy committed
238 239
}

Andreas Schwab's avatar
Andreas Schwab committed
240
void
241
init_eval (void)
Jim Blandy's avatar
Jim Blandy committed
242 243
{
  specpdl_ptr = specpdl;
244
  handlerlist = NULL;
Jim Blandy's avatar
Jim Blandy committed
245 246 247
  Vquit_flag = Qnil;
  debug_on_next_call = 0;
  lisp_eval_depth = 0;
248
#ifdef DEBUG_GCPRO
249
  gcpro_level = 0;
250
#endif
251
  /* This is less than the initial value of num_nonmacro_input_events.  */
252
  when_entered_debugger = -1;
Jim Blandy's avatar
Jim Blandy committed
253 254
}

255
/* Unwind-protect function used by call_debugger.  */
256

257
static void
258
restore_stack_limits (Lisp_Object data)
259 260 261 262 263 264 265
{
  max_specpdl_size = XINT (XCAR (data));
  max_lisp_eval_depth = XINT (XCDR (data));
}

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

266
Lisp_Object
267
call_debugger (Lisp_Object arg)
Jim Blandy's avatar
Jim Blandy committed
268
{
269
  bool debug_while_redisplaying;
270
  ptrdiff_t count = SPECPDL_INDEX ();
271
  Lisp_Object val;
272
  EMACS_INT old_max = max_specpdl_size;
273

274 275
  /* Temporarily bump up the stack limits,
     so the debugger won't run out of stack.  */
276

277 278 279 280 281 282 283 284 285
  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;

286
  if (max_specpdl_size - 100 < SPECPDL_INDEX ())
287
    max_specpdl_size = SPECPDL_INDEX () + 100;
288

289
#ifdef HAVE_WINDOW_SYSTEM
290 291
  if (display_hourglass_p)
    cancel_hourglass ();
292 293
#endif

Jim Blandy's avatar
Jim Blandy committed
294
  debug_on_next_call = 0;
295
  when_entered_debugger = num_nonmacro_input_events;
296 297 298 299 300

  /* 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;
301 302
  specbind (intern ("debugger-may-continue"),
	    debug_while_redisplaying ? Qnil : Qt);
303
  specbind (Qinhibit_redisplay, Qnil);
304
  specbind (Qinhibit_debugger, Qt);
305 306 307

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

311 312 313 314
  val = apply1 (Vdebugger, arg);

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

319
  return unbind_to (count, val);
Jim Blandy's avatar
Jim Blandy committed
320 321
}

322
static void
323
do_debug_on_call (Lisp_Object code)
Jim Blandy's avatar
Jim Blandy committed
324 325
{
  debug_on_next_call = 0;
326
  set_backtrace_debug_on_exit (specpdl_ptr - 1, true);
327
  call_debugger (list1 (code));
Jim Blandy's avatar
Jim Blandy committed
328 329 330 331 332 333 334
}

/* 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
335 336 337
       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
338
usage: (or CONDITIONS...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
339
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
340
{
341
  register Lisp_Object val = Qnil;
Jim Blandy's avatar
Jim Blandy committed
342 343
  struct gcpro gcpro1;

344
  GCPRO1 (args);
Jim Blandy's avatar
Jim Blandy committed
345

346
  while (CONSP (args))
Jim Blandy's avatar
Jim Blandy committed
347
    {
348
      val = eval_sub (XCAR (args));
Jim Blandy's avatar
Jim Blandy committed
349
      if (!NILP (val))
Jim Blandy's avatar
Jim Blandy committed
350
	break;
351
      args = XCDR (args);
Jim Blandy's avatar
Jim Blandy committed
352 353 354 355 356 357 358
    }

  UNGCPRO;
  return val;
}

DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
359
       doc: /* Eval args until one of them yields nil, then return nil.
Pavel Janík's avatar
Pavel Janík committed
360 361
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
362
usage: (and CONDITIONS...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
363
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
364
{
365
  register Lisp_Object val = Qt;
Jim Blandy's avatar
Jim Blandy committed
366 367
  struct gcpro gcpro1;

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

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

  UNGCPRO;
  return val;
}

DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
383
       doc: /* If COND yields non-nil, do THEN, else do ELSE...
Pavel Janík's avatar
Pavel Janík committed
384 385 386
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.
387
usage: (if COND THEN ELSE...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
388
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
389
{
390
  Lisp_Object cond;
Jim Blandy's avatar
Jim Blandy committed
391 392 393
  struct gcpro gcpro1;

  GCPRO1 (args);
394
  cond = eval_sub (XCAR (args));
Jim Blandy's avatar
Jim Blandy committed
395 396
  UNGCPRO;

Jim Blandy's avatar
Jim Blandy committed
397
  if (!NILP (cond))
398 399
    return eval_sub (Fcar (XCDR (args)));
  return Fprogn (XCDR (XCDR (args)));
Jim Blandy's avatar
Jim Blandy committed
400 401 402
}

DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
403 404 405 406 407 408 409 410
       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.
411
usage: (cond CLAUSES...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
412
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
413
{
414
  Lisp_Object val = args;
Jim Blandy's avatar
Jim Blandy committed
415 416 417
  struct gcpro gcpro1;

  GCPRO1 (args);
418
  while (CONSP (args))
Jim Blandy's avatar
Jim Blandy committed
419
    {
420
      Lisp_Object clause = XCAR (args);
421
      val = eval_sub (Fcar (clause));
Jim Blandy's avatar
Jim Blandy committed
422
      if (!NILP (val))
Jim Blandy's avatar
Jim Blandy committed
423
	{
424
	  if (!NILP (XCDR (clause)))
425
	    val = Fprogn (XCDR (clause));
Jim Blandy's avatar
Jim Blandy committed
426 427
	  break;
	}
428
      args = XCDR (args);
Jim Blandy's avatar
Jim Blandy committed
429 430 431 432 433 434
    }
  UNGCPRO;

  return val;
}

Paul Eggert's avatar
Paul Eggert committed
435
DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
436
       doc: /* Eval BODY forms sequentially and return value of last one.
Juanma Barranquero's avatar
Juanma Barranquero committed
437
usage: (progn BODY...)  */)
438
  (Lisp_Object body)
Jim Blandy's avatar
Jim Blandy committed
439
{
440
  Lisp_Object val = Qnil;
Jim Blandy's avatar
Jim Blandy committed
441 442
  struct gcpro gcpro1;

443
  GCPRO1 (body);
Jim Blandy's avatar
Jim Blandy committed
444

445
  while (CONSP (body))
Jim Blandy's avatar
Jim Blandy committed
446
    {
447 448
      val = eval_sub (XCAR (body));
      body = XCDR (body);
Jim Blandy's avatar
Jim Blandy committed
449 450 451 452 453 454
    }

  UNGCPRO;
  return val;
}

Paul Eggert's avatar
Paul Eggert committed
455
/* Evaluate BODY sequentially, discarding its value.  Suitable for
456 457 458 459 460 461 462 463
   record_unwind_protect.  */

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

Jim Blandy's avatar
Jim Blandy committed
464
DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
465
       doc: /* Eval FIRST and BODY sequentially; return value from FIRST.
Pavel Janík's avatar
Pavel Janík committed
466 467
The value of FIRST is saved during the evaluation of the remaining args,
whose values are discarded.
468
usage: (prog1 FIRST BODY...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
469
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
470 471
{
  Lisp_Object val;
472
  Lisp_Object args_left;
Jim Blandy's avatar
Jim Blandy committed
473 474 475
  struct gcpro gcpro1, gcpro2;

  args_left = args;
476
  val = args;
Jim Blandy's avatar
Jim Blandy committed
477 478
  GCPRO2 (args, val);

479 480 481
  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
482 483 484 485 486 487

  UNGCPRO;
  return val;
}

DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
488
       doc: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
489 490 491
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
492
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
493
{
494
  struct gcpro gcpro1;
Jim Blandy's avatar
Jim Blandy committed
495

496 497
  GCPRO1 (args);
  eval_sub (XCAR (args));
498 499
  UNGCPRO;
  return Fprog1 (XCDR (args));
Jim Blandy's avatar
Jim Blandy committed
500 501 502
}

DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
503 504 505 506 507 508 509
       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
510
usage: (setq [SYM VAL]...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
511
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
512
{
513
  Lisp_Object val, sym, lex_binding;
Jim Blandy's avatar
Jim Blandy committed
514

515 516 517 518 519 520
  val = args;
  if (CONSP (args))
    {
      Lisp_Object args_left = args;
      struct gcpro gcpro1;
      GCPRO1 (args);
Jim Blandy's avatar
Jim Blandy committed
521

522 523 524 525 526 527 528 529 530 531 532 533 534 535
      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
536

537 538 539
	  args_left = Fcdr (XCDR (args_left));
	}
      while (CONSP (args_left));
540

541
      UNGCPRO;
Jim Blandy's avatar
Jim Blandy committed
542 543 544 545
    }

  return val;
}
546

Jim Blandy's avatar
Jim Blandy committed
547
DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
548
       doc: /* Return the argument, without evaluating it.  `(quote x)' yields `x'.
549 550 551 552 553 554 555 556
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
557
usage: (quote ARG)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
558
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
559
{
560
  if (CONSP (XCDR (args)))
561
    xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args));
562
  return XCAR (args);
Jim Blandy's avatar
Jim Blandy committed
563
}
564

Jim Blandy's avatar
Jim Blandy committed
565
DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
566 567 568 569
       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
570
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
571
{
572 573
  Lisp_Object quoted = XCAR (args);

574
  if (CONSP (XCDR (args)))
575
    xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
576 577 578 579 580 581

  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.  */
582 583
    return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment,
				   XCDR (quoted)));
584 585 586
  else
    /* Simply quote the argument.  */
    return quoted;
Jim Blandy's avatar
Jim Blandy committed
587 588
}

589

590
DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
591
       doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
Richard M. Stallman's avatar
Richard M. Stallman committed
592
Aliased variables always have the same value; setting one sets the other.
593
Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS.  If it is
594 595 596 597
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.
598
The return value is BASE-VARIABLE.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
599
  (Lisp_Object new_alias, Lisp_Object base_variable, Lisp_Object docstring)
Gerd Moellmann's avatar
Gerd Moellmann committed
600 601
{
  struct Lisp_Symbol *sym;
602

603 604
  CHECK_SYMBOL (new_alias);
  CHECK_SYMBOL (base_variable);
Gerd Moellmann's avatar
Gerd Moellmann committed
605

606
  sym = XSYMBOL (new_alias);
607 608

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

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

620
  /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
621 622 623 624
     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)))
625
    set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1);
626 627

  {
628
    union specbinding *p;
629

Paul Eggert's avatar
Paul Eggert committed
630
    for (p = specpdl_ptr; p > specpdl; )
631 632
      if ((--p)->kind >= SPECPDL_LET
	  && (EQ (new_alias, specpdl_symbol (p))))
633 634 635
	error ("Don't know how to make a let-bound variable an alias");
  }

636
  sym->declared_special = 1;
637
  XSYMBOL (base_variable)->declared_special = 1;
638 639
  sym->redirect = SYMBOL_VARALIAS;
  SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
640 641
  sym->constant = SYMBOL_CONSTANT_P (base_variable);
  LOADHIST_ATTACH (new_alias);
642 643
  /* Even if docstring is nil: remove old docstring.  */
  Fput (new_alias, Qvariable_documentation, docstring);
644

645
  return base_variable;
Gerd Moellmann's avatar
Gerd Moellmann committed
646 647
}

648 649 650 651 652 653 654 655 656 657 658 659 660 661 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
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
693

Jim Blandy's avatar
Jim Blandy committed
694
DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
Juanma Barranquero's avatar
Juanma Barranquero committed
695
       doc: /* Define SYMBOL as a variable, and return SYMBOL.
696 697 698 699 700 701 702 703 704
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
705
If INITVALUE is missing, SYMBOL's value is not set.
706 707 708 709 710 711 712

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.)
713 714 715 716 717

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

To define a user option, use `defcustom' instead of `defvar'.
718
usage: (defvar SYMBOL &optional INITVALUE DOCSTRING)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
719
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
720
{
721
  Lisp_Object sym, tem, tail;
Jim Blandy's avatar
Jim Blandy committed
722

723 724
  sym = XCAR (args);
  tail = XCDR (args);
725

726
  if (CONSP (tail))
Jim Blandy's avatar
Jim Blandy committed
727
    {
728 729 730 731 732
      if (CONSP (XCDR (tail)) && CONSP (XCDR (XCDR (tail))))
	error ("Too many arguments");

      tem = Fdefault_boundp (sym);

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

Jim Blandy's avatar
Jim Blandy committed
736
      if (NILP (tem))
737
	Fset_default (sym, eval_sub (XCAR (tail)));
738 739 740
      else
	{ /* Check if there is really a global binding rather than just a let
	     binding that shadows the global unboundness of the var.  */
741 742
	  union specbinding *binding = default_toplevel_binding (sym);
	  if (binding && EQ (specpdl_old_value (binding), Qunbound))
743
	    {
744
	      set_specpdl_old_value (binding, eval_sub (XCAR (tail)));
745 746
	    }
	}
747
      tail = XCDR (tail);
748 749
      tem = Fcar (tail);
      if (!NILP (tem))
750 751 752 753 754
	{
	  if (!NILP (Vpurify_flag))
	    tem = Fpurecopy (tem);
	  Fput (sym, Qvariable_documentation, tem);
	}
755
      LOADHIST_ATTACH (sym);
Jim Blandy's avatar
Jim Blandy committed
756
    }
757 758 759 760 761
  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).  */
762 763
    Vinternal_interpreter_environment
      = Fcons (sym, Vinternal_interpreter_environment);
764
  else
765 766 767 768 769
    {
      /* 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
770

Jim Blandy's avatar
Jim Blandy committed
771 772 773 774
  return sym;
}

DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
775
       doc: /* Define SYMBOL as a constant variable.
776 777 778 779 780 781 782 783 784 785 786 787 788
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.
789
usage: (defconst SYMBOL INITVALUE [DOCSTRING])  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
790
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
791
{
792
  Lisp_Object sym, tem;
Jim Blandy's avatar
Jim Blandy committed
793

794 795
  sym = XCAR (args);
  if (CONSP (Fcdr (XCDR (XCDR (args)))))
796
    error ("Too many arguments");
797

798
  tem = eval_sub (Fcar (XCDR (args)));
799 800 801
  if (!NILP (Vpurify_flag))
    tem = Fpurecopy (tem);
  Fset_default (sym, tem);
802
  XSYMBOL (sym)->declared_special = 1;
803
  tem = Fcar (XCDR (XCDR (args)));
Jim Blandy's avatar
Jim Blandy committed
804
  if (!NILP (tem))
Jim Blandy's avatar
Jim Blandy committed
805
    {
Jim Blandy's avatar
Jim Blandy committed
806
      if (!NILP (Vpurify_flag))
Jim Blandy's avatar
Jim Blandy committed
807 808 809
	tem = Fpurecopy (tem);
      Fput (sym, Qvariable_documentation, tem);
    }
810
  Fput (sym, Qrisky_local_variable, Qt);
811
  LOADHIST_ATTACH (sym);
Jim Blandy's avatar
Jim Blandy committed
812 813 814
  return sym;
}

815 816 817 818 819 820 821 822 823 824 825
/* 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
826 827

DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
828 829 830 831 832
       doc: /* Bind variables according to VARLIST then eval BODY.
The value of the last form in BODY is returned.