eval.c 116 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

Paul Eggert's avatar
Paul Eggert committed
3
Copyright (C) 1985-1987, 1993-1995, 1999-2016 Free Software Foundation,
4
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>
Paul Eggert's avatar
Paul Eggert committed
25
#include <stdlib.h>
Jim Blandy's avatar
Jim Blandy committed
26
#include "lisp.h"
27
#include "blockinput.h"
Jim Blandy's avatar
Jim Blandy committed
28
#include "commands.h"
Jim Blandy's avatar
Jim Blandy committed
29
#include "keyboard.h"
30
#include "dispextern.h"
31
#include "buffer.h"
32

33
/* Chain of condition and catch handlers currently in effect.  */
34 35 36

struct handler *handlerlist;

Jim Blandy's avatar
Jim Blandy committed
37 38 39
/* 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:
40
   (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide.  */
Jim Blandy's avatar
Jim Blandy committed
41 42 43

Lisp_Object Vautoload_queue;

44 45 46 47 48
/* This holds either the symbol `run-hooks' or nil.
   It is nil at an early stage of startup, and when Emacs
   is shutting down.  */
Lisp_Object Vrun_hooks;

49 50
/* Current number of specbindings allocated in specpdl, not counting
   the dummy entry specpdl[-1].  */
51

52
ptrdiff_t specpdl_size;
Jim Blandy's avatar
Jim Blandy committed
53

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

57
union specbinding *specpdl;
Jim Blandy's avatar
Jim Blandy committed
58 59

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

61
union specbinding *specpdl_ptr;
Jim Blandy's avatar
Jim Blandy committed
62 63

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

65
static EMACS_INT lisp_eval_depth;
Jim Blandy's avatar
Jim Blandy committed
66

67
/* The value of num_nonmacro_input_events as of the last time we
68
   started to enter the debugger.  If we decide to enter the debugger
69
   again when this is still equal to num_nonmacro_input_events, then we
70 71 72
   know that the debugger itself has an error, and we should just
   signal the error instead of entering an infinite loop of debugger
   invocations.  */
73

74
static EMACS_INT when_entered_debugger;
Jim Blandy's avatar
Jim Blandy committed
75

76 77
/* The function from which the last `signal' was called.  Set in
   Fsignal.  */
78
/* FIXME: We should probably get rid of this!  */
79 80
Lisp_Object Vsignaling_function;

81 82
/* If non-nil, Lisp code must not be run since some part of Emacs is in
   an inconsistent state.  Currently unused.  */
83 84
Lisp_Object inhibit_lisp_code;

85 86 87 88 89 90 91
/* 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;

92
static Lisp_Object funcall_lambda (Lisp_Object, ptrdiff_t, Lisp_Object *);
93
static Lisp_Object apply_lambda (Lisp_Object, Lisp_Object, ptrdiff_t);
Paul Pogonyshev's avatar
Paul Pogonyshev committed
94
static Lisp_Object lambda_arity (Lisp_Object);
95

96
static Lisp_Object
97
specpdl_symbol (union specbinding *pdl)
98 99
{
  eassert (pdl->kind >= SPECPDL_LET);
100
  return pdl->let.symbol;
101 102 103
}

static Lisp_Object
104
specpdl_old_value (union specbinding *pdl)
105 106
{
  eassert (pdl->kind >= SPECPDL_LET);
107
  return pdl->let.old_value;
108 109
}

110 111 112 113 114 115 116
static void
set_specpdl_old_value (union specbinding *pdl, Lisp_Object val)
{
  eassert (pdl->kind >= SPECPDL_LET);
  pdl->let.old_value = val;
}

117
static Lisp_Object
118
specpdl_where (union specbinding *pdl)
119 120
{
  eassert (pdl->kind > SPECPDL_LET);
121
  return pdl->let.where;
122 123 124
}

static Lisp_Object
125
specpdl_arg (union specbinding *pdl)
126 127
{
  eassert (pdl->kind == SPECPDL_UNWIND);
128
  return pdl->unwind.arg;
129 130
}

131
Lisp_Object
132
backtrace_function (union specbinding *pdl)
133 134
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
135
  return pdl->bt.function;
136 137 138
}

static ptrdiff_t
139
backtrace_nargs (union specbinding *pdl)
140 141
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
142
  return pdl->bt.nargs;
143 144
}

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

static bool
153
backtrace_debug_on_exit (union specbinding *pdl)
154 155
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
156
  return pdl->bt.debug_on_exit;
157 158
}

159
/* Functions to modify slots of backtrace records.  */
160

161
static void
162
set_backtrace_args (union specbinding *pdl, Lisp_Object *args, ptrdiff_t nargs)
163 164 165
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
  pdl->bt.args = args;
166
  pdl->bt.nargs = nargs;
167
}
168

169
static void
170 171 172 173 174
set_backtrace_debug_on_exit (union specbinding *pdl, bool doe)
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
  pdl->bt.debug_on_exit = doe;
}
175 176 177

/* Helper functions to scan the backtrace.  */

178 179
bool
backtrace_p (union specbinding *pdl)
180
{ return pdl >= specpdl; }
181

182
union specbinding *
183
backtrace_top (void)
184
{
185
  union specbinding *pdl = specpdl_ptr - 1;
186
  while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
187 188
    pdl--;
  return pdl;
189
}
190

191 192
union specbinding *
backtrace_next (union specbinding *pdl)
193
{
194 195 196 197
  pdl--;
  while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
    pdl--;
  return pdl;
198 199
}

200 201 202 203 204 205
/* Return a pointer to somewhere near the top of the C stack.  */
void *
near_C_stack_top (void)
{
  return backtrace_args (backtrace_top ());
}
206

Andreas Schwab's avatar
Andreas Schwab committed
207
void
208
init_eval_once (void)
Jim Blandy's avatar
Jim Blandy committed
209
{
210
  enum { size = 50 };
211
  union specbinding *pdlvec = xmalloc ((size + 1) * sizeof *specpdl);
212
  specpdl_size = size;
213
  specpdl = specpdl_ptr = pdlvec + 1;
214
  /* Don't forget to update docs (lispref node "Local Variables").  */
Stefan Monnier's avatar
Stefan Monnier committed
215
  max_specpdl_size = 1300; /* 1000 is not enough for CEDET's c-by.el.  */
216
  max_lisp_eval_depth = 800;
217 218

  Vrun_hooks = Qnil;
Jim Blandy's avatar
Jim Blandy committed
219 220
}

221 222
static struct handler handlerlist_sentinel;

Andreas Schwab's avatar
Andreas Schwab committed
223
void
224
init_eval (void)
Jim Blandy's avatar
Jim Blandy committed
225 226
{
  specpdl_ptr = specpdl;
227 228 229 230
  { /* 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.   */
    handlerlist = handlerlist_sentinel.nextfree = &handlerlist_sentinel;
231
    struct handler *c = push_handler (Qunbound, CATCHER);
232 233 234 235
    eassert (c == &handlerlist_sentinel);
    handlerlist_sentinel.nextfree = NULL;
    handlerlist_sentinel.next = NULL;
  }
Jim Blandy's avatar
Jim Blandy committed
236 237 238
  Vquit_flag = Qnil;
  debug_on_next_call = 0;
  lisp_eval_depth = 0;
239
  /* This is less than the initial value of num_nonmacro_input_events.  */
240
  when_entered_debugger = -1;
Jim Blandy's avatar
Jim Blandy committed
241 242
}

243
/* Unwind-protect function used by call_debugger.  */
244

245
static void
246
restore_stack_limits (Lisp_Object data)
247 248 249 250 251
{
  max_specpdl_size = XINT (XCAR (data));
  max_lisp_eval_depth = XINT (XCDR (data));
}

252 253
static void grow_specpdl (void);

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

256
Lisp_Object
257
call_debugger (Lisp_Object arg)
Jim Blandy's avatar
Jim Blandy committed
258
{
259
  bool debug_while_redisplaying;
260
  ptrdiff_t count = SPECPDL_INDEX ();
261
  Lisp_Object val;
262 263 264
  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);
265 266 267 268

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

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
  /* 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)));
285

286
#ifdef HAVE_WINDOW_SYSTEM
287 288
  if (display_hourglass_p)
    cancel_hourglass ();
289 290
#endif

Jim Blandy's avatar
Jim Blandy committed
291
  debug_on_next_call = 0;
292
  when_entered_debugger = num_nonmacro_input_events;
293 294 295 296 297

  /* 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;
298 299
  specbind (intern ("debugger-may-continue"),
	    debug_while_redisplaying ? Qnil : Qt);
300
  specbind (Qinhibit_redisplay, Qnil);
301
  specbind (Qinhibit_debugger, Qt);
302

303 304 305 306 307
  /* If we are debugging an error while `inhibit-changing-match-data'
     is bound to non-nil (e.g., within a call to `string-match-p'),
     then make sure debugger code can still use match data.  */
  specbind (Qinhibit_changing_match_data, Qnil);

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

313 314 315 316
  val = apply1 (Vdebugger, arg);

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

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

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

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

  return val;
}

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

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

  return val;
}

DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
377
       doc: /* If COND yields non-nil, do THEN, else do ELSE...
Pavel Janík's avatar
Pavel Janík committed
378 379 380
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.
381
usage: (if COND THEN ELSE...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
382
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
383
{
384
  Lisp_Object cond;
Jim Blandy's avatar
Jim Blandy committed
385

386
  cond = eval_sub (XCAR (args));
Jim Blandy's avatar
Jim Blandy committed
387

Jim Blandy's avatar
Jim Blandy committed
388
  if (!NILP (cond))
389 390
    return eval_sub (Fcar (XCDR (args)));
  return Fprogn (XCDR (XCDR (args)));
Jim Blandy's avatar
Jim Blandy committed
391 392 393
}

DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
394 395 396 397 398
       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
399 400
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
401
If no clause succeeds, cond returns nil.
402
usage: (cond CLAUSES...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
403
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
404
{
405
  Lisp_Object val = args;
Jim Blandy's avatar
Jim Blandy committed
406

407
  while (CONSP (args))
Jim Blandy's avatar
Jim Blandy committed
408
    {
409
      Lisp_Object clause = XCAR (args);
410
      val = eval_sub (Fcar (clause));
Jim Blandy's avatar
Jim Blandy committed
411
      if (!NILP (val))
Jim Blandy's avatar
Jim Blandy committed
412
	{
413
	  if (!NILP (XCDR (clause)))
414
	    val = Fprogn (XCDR (clause));
Jim Blandy's avatar
Jim Blandy committed
415 416
	  break;
	}
417
      args = XCDR (args);
Jim Blandy's avatar
Jim Blandy committed
418 419 420 421 422
    }

  return val;
}

Paul Eggert's avatar
Paul Eggert committed
423
DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
424
       doc: /* Eval BODY forms sequentially and return value of last one.
Juanma Barranquero's avatar
Juanma Barranquero committed
425
usage: (progn BODY...)  */)
426
  (Lisp_Object body)
Jim Blandy's avatar
Jim Blandy committed
427
{
428
  Lisp_Object val = Qnil;
Jim Blandy's avatar
Jim Blandy committed
429

430
  while (CONSP (body))
Jim Blandy's avatar
Jim Blandy committed
431
    {
432 433
      val = eval_sub (XCAR (body));
      body = XCDR (body);
Jim Blandy's avatar
Jim Blandy committed
434 435 436 437 438
    }

  return val;
}

Paul Eggert's avatar
Paul Eggert committed
439
/* Evaluate BODY sequentially, discarding its value.  Suitable for
440 441 442 443 444 445 446 447
   record_unwind_protect.  */

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

Jim Blandy's avatar
Jim Blandy committed
448
DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
449
       doc: /* Eval FIRST and BODY sequentially; return value from FIRST.
Pavel Janík's avatar
Pavel Janík committed
450 451
The value of FIRST is saved during the evaluation of the remaining args,
whose values are discarded.
452
usage: (prog1 FIRST BODY...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
453
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
454 455
{
  Lisp_Object val;
456
  Lisp_Object args_left;
Jim Blandy's avatar
Jim Blandy committed
457 458

  args_left = args;
459
  val = args;
Jim Blandy's avatar
Jim Blandy committed
460

461 462 463
  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
464 465 466 467 468

  return val;
}

DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
469
       doc: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
470 471 472
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
473
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
474
{
475
  eval_sub (XCAR (args));
476
  return Fprog1 (XCDR (args));
Jim Blandy's avatar
Jim Blandy committed
477 478 479
}

DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
480 481 482 483 484 485 486
       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
487
usage: (setq [SYM VAL]...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
488
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
489
{
490
  Lisp_Object val, sym, lex_binding;
Jim Blandy's avatar
Jim Blandy committed
491

492 493 494 495
  val = args;
  if (CONSP (args))
    {
      Lisp_Object args_left = args;
496 497 498 499
      Lisp_Object numargs = Flength (args);

      if (XINT (numargs) & 1)
        xsignal2 (Qwrong_number_of_arguments, Qsetq, numargs);
Jim Blandy's avatar
Jim Blandy committed
500

501 502 503 504 505 506 507 508 509 510 511 512 513 514
      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
515

516 517 518
	  args_left = Fcdr (XCDR (args_left));
	}
      while (CONSP (args_left));
Jim Blandy's avatar
Jim Blandy committed
519 520 521 522
    }

  return val;
}
523

Jim Blandy's avatar
Jim Blandy committed
524
DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
525
       doc: /* Return the argument, without evaluating it.  `(quote x)' yields `x'.
526 527 528
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').
529
This means that \\='(a . b) is not identical to (cons \\='a \\='b): the former
530 531 532 533
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
534
usage: (quote ARG)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
535
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
536
{
537
  if (CONSP (XCDR (args)))
538
    xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args));
539
  return XCAR (args);
Jim Blandy's avatar
Jim Blandy committed
540
}
541

Jim Blandy's avatar
Jim Blandy committed
542
DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
543 544 545 546
       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
547
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
548
{
549 550
  Lisp_Object quoted = XCAR (args);

551
  if (CONSP (XCDR (args)))
552
    xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
553 554 555 556

  if (!NILP (Vinternal_interpreter_environment)
      && CONSP (quoted)
      && EQ (XCAR (quoted), Qlambda))
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
    { /* This is a lambda expression within a lexical environment;
	 return an interpreted closure instead of a simple lambda.  */
      Lisp_Object cdr = XCDR (quoted);
      Lisp_Object tmp = cdr;
      if (CONSP (tmp)
	  && (tmp = XCDR (tmp), CONSP (tmp))
	  && (tmp = XCAR (tmp), CONSP (tmp))
	  && (EQ (QCdocumentation, XCAR (tmp))))
	{ /* Handle the special (:documentation <form>) to build the docstring
	     dynamically.  */
	  Lisp_Object docstring = eval_sub (Fcar (XCDR (tmp)));
	  CHECK_STRING (docstring);
	  cdr = Fcons (XCAR (cdr), Fcons (docstring, XCDR (XCDR (cdr))));
	}
      return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment,
				     cdr));
    }
574 575 576
  else
    /* Simply quote the argument.  */
    return quoted;
Jim Blandy's avatar
Jim Blandy committed
577 578
}

579

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

593 594
  CHECK_SYMBOL (new_alias);
  CHECK_SYMBOL (base_variable);
Gerd Moellmann's avatar
Gerd Moellmann committed
595

Noam Postavsky's avatar
Noam Postavsky committed
596 597
  if (SYMBOL_CONSTANT_P (new_alias))
    /* Making it an alias effectively changes its value.  */
Stefan Monnier's avatar
Stefan Monnier committed
598
    error ("Cannot make a constant an alias");
599

Noam Postavsky's avatar
Noam Postavsky committed
600 601
  sym = XSYMBOL (new_alias);

602 603 604 605 606 607
  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");
608 609 610 611 612
    case SYMBOL_PLAINVAL:
    case SYMBOL_VARALIAS:
      break;
    default:
      emacs_abort ();
613 614
    }

615
  /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
616 617 618 619
     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)))
Noam Postavsky's avatar
Noam Postavsky committed
620 621
    set_internal (base_variable, find_symbol_value (new_alias),
                  Qnil, SET_INTERNAL_BIND);
622
  {
623
    union specbinding *p;
624

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

Noam Postavsky's avatar
Noam Postavsky committed
631 632 633
  if (sym->trapped_write == SYMBOL_TRAPPED_WRITE)
    notify_variable_watchers (new_alias, base_variable, Qdefvaralias, Qnil);

634
  sym->declared_special = 1;
635
  XSYMBOL (base_variable)->declared_special = 1;
636 637
  sym->redirect = SYMBOL_VARALIAS;
  SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
Noam Postavsky's avatar
Noam Postavsky committed
638
  sym->trapped_write = XSYMBOL (base_variable)->trapped_write;
639
  LOADHIST_ATTACH (new_alias);
640 641
  /* Even if docstring is nil: remove old docstring.  */
  Fput (new_alias, Qvariable_documentation, docstring);
642

643
  return base_variable;
Gerd Moellmann's avatar
Gerd Moellmann committed
644 645
}

646 647 648 649 650 651 652 653 654 655 656 657 658 659
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;
660 661 662 663 664 665 666 667 668 669 670

	case SPECPDL_UNWIND:
	case SPECPDL_UNWIND_PTR:
	case SPECPDL_UNWIND_INT:
	case SPECPDL_UNWIND_VOID:
	case SPECPDL_BACKTRACE:
	case SPECPDL_LET_LOCAL:
	  break;

	default:
	  emacs_abort ();
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
	}
    }
  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
702

Jim Blandy's avatar
Jim Blandy committed
703
DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
Juanma Barranquero's avatar
Juanma Barranquero committed
704
       doc: /* Define SYMBOL as a variable, and return SYMBOL.
705 706 707 708 709 710 711 712 713
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
714
If INITVALUE is missing, SYMBOL's value is not set.
715 716 717 718 719

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
720
for these variables.  (`defconst' and `defcustom' behave similarly in
721
this respect.)
722 723 724 725 726

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

To define a user option, use `defcustom' instead of `defvar'.
727
usage: (defvar SYMBOL &optional INITVALUE DOCSTRING)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
728
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
729
{
730
  Lisp_Object sym, tem, tail;
Jim Blandy's avatar
Jim Blandy committed
731

732 733
  sym = XCAR (args);
  tail = XCDR (args);
734

735
  if (CONSP (tail))
Jim Blandy's avatar
Jim Blandy committed
736
    {
737 738 739 740 741
      if (CONSP (XCDR (tail)) && CONSP (XCDR (XCDR (tail))))
	error ("Too many arguments");

      tem = Fdefault_boundp (sym);

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

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

Jim Blandy's avatar
Jim Blandy committed
780 781 782 783
  return sym;
}

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

803 804
  sym = XCAR (args);
  if (CONSP (Fcdr (XCDR (XCDR (args)))))
805
    error ("Too many arguments");
806

807
  tem = eval_sub (Fcar (XCDR (args)));
808 809 810
  if (!NILP (Vpurify_flag))
    tem = Fpurecopy (tem);
  Fset_default (sym, tem);
811
  XSYMBOL (sym)->declared_special = 1;
812
  tem = Fcar (XCDR (XCDR (args)));
Jim Blandy's avatar
Jim Blandy committed
813
  if (!NILP (tem))
Jim Blandy's avatar
Jim Blandy committed
814
    {
Jim Blandy's avatar
Jim Blandy committed
815
      if (!NILP (Vpurify_flag))
Jim Blandy's avatar
Jim Blandy committed
816 817 818
	tem = Fpurecopy (tem);
      Fput (sym, Qvariable_documentation, tem);
    }
819
  Fput (sym, Qrisky_local_variable, Qt);
820
  LOADHIST_ATTACH (sym);
Jim Blandy's avatar
Jim Blandy committed
821 822 823
  return sym;
}

824 825 826 827 828 829 830 831 832 833 834
/* 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;
}