eval.c 122 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-2018 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 <https://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 34 35 36 37 38 39 40 41
/* CACHEABLE is ordinarily nothing, except it is 'volatile' if
   necessary to cajole GCC into not warning incorrectly that a
   variable should be volatile.  */
#if defined GCC_LINT || defined lint
# define CACHEABLE volatile
#else
# define CACHEABLE /* empty */
#endif

42
/* Chain of condition and catch handlers currently in effect.  */
43

44
/* struct handler *handlerlist; */
45

Jim Blandy's avatar
Jim Blandy committed
46 47 48
/* 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:
49
   (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide.  */
Jim Blandy's avatar
Jim Blandy committed
50 51 52

Lisp_Object Vautoload_queue;

53 54 55 56 57
/* 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;

Eli Zaretskii's avatar
Eli Zaretskii committed
58 59
/* The commented-out variables below are macros defined in thread.h.  */

60 61
/* Current number of specbindings allocated in specpdl, not counting
   the dummy entry specpdl[-1].  */
62

63
/* ptrdiff_t specpdl_size; */
Jim Blandy's avatar
Jim Blandy committed
64

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

Tom Tromey's avatar
Tom Tromey committed
68
/* union specbinding *specpdl; */
Jim Blandy's avatar
Jim Blandy committed
69 70

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

Tom Tromey's avatar
Tom Tromey committed
72
/* union specbinding *specpdl_ptr; */
Jim Blandy's avatar
Jim Blandy committed
73 74

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

Eli Zaretskii's avatar
Eli Zaretskii committed
76
/* static EMACS_INT lisp_eval_depth; */
Jim Blandy's avatar
Jim Blandy committed
77

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

85
static EMACS_INT when_entered_debugger;
Jim Blandy's avatar
Jim Blandy committed
86

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

92 93
/* If non-nil, Lisp code must not be run since some part of Emacs is in
   an inconsistent state.  Currently unused.  */
94 95
Lisp_Object inhibit_lisp_code;

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

103
static Lisp_Object funcall_lambda (Lisp_Object, ptrdiff_t, Lisp_Object *);
104
static Lisp_Object apply_lambda (Lisp_Object, Lisp_Object, ptrdiff_t);
Paul Pogonyshev's avatar
Paul Pogonyshev committed
105
static Lisp_Object lambda_arity (Lisp_Object);
106

107
static Lisp_Object
108
specpdl_symbol (union specbinding *pdl)
109 110
{
  eassert (pdl->kind >= SPECPDL_LET);
111
  return pdl->let.symbol;
112 113
}

114 115 116 117 118 119 120
static enum specbind_tag
specpdl_kind (union specbinding *pdl)
{
  eassert (pdl->kind >= SPECPDL_LET);
  return pdl->let.kind;
}

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

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

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

Tom Tromey's avatar
Tom Tromey committed
142 143 144 145 146 147 148
static Lisp_Object
specpdl_saved_value (union specbinding *pdl)
{
  eassert (pdl->kind >= SPECPDL_LET);
  return pdl->let.saved_value;
}

149
static Lisp_Object
150
specpdl_arg (union specbinding *pdl)
151 152
{
  eassert (pdl->kind == SPECPDL_UNWIND);
153
  return pdl->unwind.arg;
154 155
}

156
Lisp_Object
157
backtrace_function (union specbinding *pdl)
158 159
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
160
  return pdl->bt.function;
161 162 163
}

static ptrdiff_t
164
backtrace_nargs (union specbinding *pdl)
165 166
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
167
  return pdl->bt.nargs;
168 169
}

170
Lisp_Object *
171
backtrace_args (union specbinding *pdl)
172 173
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
174
  return pdl->bt.args;
175 176 177
}

static bool
178
backtrace_debug_on_exit (union specbinding *pdl)
179 180
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
181
  return pdl->bt.debug_on_exit;
182 183
}

184
/* Functions to modify slots of backtrace records.  */
185

186
static void
187
set_backtrace_args (union specbinding *pdl, Lisp_Object *args, ptrdiff_t nargs)
188 189 190
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
  pdl->bt.args = args;
191
  pdl->bt.nargs = nargs;
192
}
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
}

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

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

Ken Raeburn's avatar
Ken Raeburn committed
239
/* static struct handler handlerlist_sentinel; */
240

Andreas Schwab's avatar
Andreas Schwab committed
241
void
242
init_eval (void)
Jim Blandy's avatar
Jim Blandy committed
243 244
{
  specpdl_ptr = specpdl;
245 246 247
  { /* 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.   */
Ken Raeburn's avatar
Ken Raeburn committed
248 249
    handlerlist_sentinel = xzalloc (sizeof (struct handler));
    handlerlist = handlerlist_sentinel->nextfree = handlerlist_sentinel;
250
    struct handler *c = push_handler (Qunbound, CATCHER);
Ken Raeburn's avatar
Ken Raeburn committed
251 252 253
    eassert (c == handlerlist_sentinel);
    handlerlist_sentinel->nextfree = NULL;
    handlerlist_sentinel->next = NULL;
254
  }
Jim Blandy's avatar
Jim Blandy committed
255 256 257
  Vquit_flag = Qnil;
  debug_on_next_call = 0;
  lisp_eval_depth = 0;
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 288 289 290
  /* The previous value of 40 is too small now that the debugger
     prints using cl-prin1 instead of prin1.  Printing lists nested 8
     deep (which is the value of print-level used in the debugger)
     currently requires 77 additional frames.  See bug#31919.  */
  if (lisp_eval_depth + 100 > max_lisp_eval_depth)
    max_lisp_eval_depth = lisp_eval_depth + 100;
291

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
  /* 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)));
308

309
#ifdef HAVE_WINDOW_SYSTEM
310 311
  if (display_hourglass_p)
    cancel_hourglass ();
312 313
#endif

Jim Blandy's avatar
Jim Blandy committed
314
  debug_on_next_call = 0;
315
  when_entered_debugger = num_nonmacro_input_events;
316 317 318 319 320

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

326 327 328 329 330
  /* 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);

331 332
#if 0 /* Binding this prevents execution of Lisp code during
	 redisplay, which necessarily leads to display problems.  */
333
  specbind (Qinhibit_eval_during_redisplay, Qt);
334
#endif
335

336 337 338 339
  val = apply1 (Vdebugger, arg);

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

344
  return unbind_to (count, val);
Jim Blandy's avatar
Jim Blandy committed
345 346
}

347
static void
348
do_debug_on_call (Lisp_Object code, ptrdiff_t count)
Jim Blandy's avatar
Jim Blandy committed
349 350
{
  debug_on_next_call = 0;
351
  set_backtrace_debug_on_exit (specpdl + count, true);
352
  call_debugger (list1 (code));
Jim Blandy's avatar
Jim Blandy committed
353 354 355 356 357 358 359
}

/* 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
360 361 362
       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
363
usage: (or CONDITIONS...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
364
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
365
{
366
  Lisp_Object val = Qnil;
Jim Blandy's avatar
Jim Blandy committed
367

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

  return val;
}

DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
381
       doc: /* Eval args until one of them yields nil, then return nil.
Pavel Janík's avatar
Pavel Janík committed
382 383
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
384
usage: (and CONDITIONS...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
385
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
386
{
387
  Lisp_Object val = Qt;
Jim Blandy's avatar
Jim Blandy committed
388

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

  return val;
}

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

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

Jim Blandy's avatar
Jim Blandy committed
413
  if (!NILP (cond))
414
    return eval_sub (Fcar (XCDR (args)));
415
  return Fprogn (Fcdr (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
  while (CONSP (args))
Jim Blandy's avatar
Jim Blandy committed
433
    {
434
      Lisp_Object clause = XCAR (args);
435
      val = eval_sub (Fcar (clause));
Jim Blandy's avatar
Jim Blandy committed
436
      if (!NILP (val))
Jim Blandy's avatar
Jim Blandy committed
437
	{
438
	  if (!NILP (XCDR (clause)))
439
	    val = Fprogn (XCDR (clause));
Jim Blandy's avatar
Jim Blandy committed
440 441
	  break;
	}
442
      args = XCDR (args);
Jim Blandy's avatar
Jim Blandy committed
443 444 445 446 447
    }

  return val;
}

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

455
  while (CONSP (body))
Jim Blandy's avatar
Jim Blandy committed
456
    {
457
      Lisp_Object form = XCAR (body);
458
      body = XCDR (body);
459
      val = eval_sub (form);
Jim Blandy's avatar
Jim Blandy committed
460 461 462 463 464
    }

  return val;
}

Paul Eggert's avatar
Paul Eggert committed
465
/* Evaluate BODY sequentially, discarding its value.  */
466 467

void
Paul Eggert's avatar
Paul Eggert committed
468
prog_ignore (Lisp_Object body)
469 470 471 472
{
  Fprogn (body);
}

Jim Blandy's avatar
Jim Blandy committed
473
DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
474
       doc: /* Eval FIRST and BODY sequentially; return value from FIRST.
Pavel Janík's avatar
Pavel Janík committed
475 476
The value of FIRST is saved during the evaluation of the remaining args,
whose values are discarded.
477
usage: (prog1 FIRST BODY...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
478
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
479
{
Paul Eggert's avatar
Paul Eggert committed
480 481
  Lisp_Object val = eval_sub (XCAR (args));
  prog_ignore (XCDR (args));
Jim Blandy's avatar
Jim Blandy committed
482 483 484 485
  return val;
}

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

DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
497 498 499 500 501 502 503
       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
504
usage: (setq [SYM VAL]...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
505
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
506
{
507
  Lisp_Object val = args, tail = args;
Jim Blandy's avatar
Jim Blandy committed
508

509
  for (EMACS_INT nargs = 0; CONSP (tail); nargs += 2)
510
    {
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
      Lisp_Object sym = XCAR (tail), lex_binding;
      tail = XCDR (tail);
      if (!CONSP (tail))
	xsignal2 (Qwrong_number_of_arguments, Qsetq, make_number (nargs + 1));
      Lisp_Object arg = XCAR (tail);
      tail = XCDR (tail);
      val = eval_sub (arg);
      /* 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
527 528 529 530
    }

  return val;
}
531

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

Jim Blandy's avatar
Jim Blandy committed
550
DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
551 552 553 554
       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
555
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
556
{
557 558
  Lisp_Object quoted = XCAR (args);

559
  if (!NILP (XCDR (args)))
560
    xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
561 562 563 564

  if (!NILP (Vinternal_interpreter_environment)
      && CONSP (quoted)
      && EQ (XCAR (quoted), Qlambda))
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
    { /* 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));
    }
582 583 584
  else
    /* Simply quote the argument.  */
    return quoted;
Jim Blandy's avatar
Jim Blandy committed
585 586
}

587

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

601 602
  CHECK_SYMBOL (new_alias);
  CHECK_SYMBOL (base_variable);
Gerd Moellmann's avatar
Gerd Moellmann committed
603

Noam Postavsky's avatar
Noam Postavsky committed
604 605
  if (SYMBOL_CONSTANT_P (new_alias))
    /* Making it an alias effectively changes its value.  */
Stefan Monnier's avatar
Stefan Monnier committed
606
    error ("Cannot make a constant an alias");
607

Noam Postavsky's avatar
Noam Postavsky committed
608 609
  sym = XSYMBOL (new_alias);

610
  switch (sym->u.s.redirect)
611 612 613 614 615
    {
    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");
616 617 618 619 620
    case SYMBOL_PLAINVAL:
    case SYMBOL_VARALIAS:
      break;
    default:
      emacs_abort ();
621 622
    }

623
  /* https://lists.gnu.org/r/emacs-devel/2008-04/msg00834.html
624 625 626 627
     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
628 629
    set_internal (base_variable, find_symbol_value (new_alias),
                  Qnil, SET_INTERNAL_BIND);
630
  {
631
    union specbinding *p;
632

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

639
  if (sym->u.s.trapped_write == SYMBOL_TRAPPED_WRITE)
Noam Postavsky's avatar
Noam Postavsky committed
640 641
    notify_variable_watchers (new_alias, base_variable, Qdefvaralias, Qnil);

642 643 644
  sym->u.s.declared_special = true;
  XSYMBOL (base_variable)->u.s.declared_special = true;
  sym->u.s.redirect = SYMBOL_VARALIAS;
645
  SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
646
  sym->u.s.trapped_write = XSYMBOL (base_variable)->u.s.trapped_write;
647
  LOADHIST_ATTACH (new_alias);
648 649
  /* Even if docstring is nil: remove old docstring.  */
  Fput (new_alias, Qvariable_documentation, docstring);
650

651
  return base_variable;
Gerd Moellmann's avatar
Gerd Moellmann committed
652 653
}

654 655 656 657 658 659 660 661 662 663 664 665 666 667
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;
668 669 670 671 672 673 674 675 676 677 678

	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 ();
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 709
	}
    }
  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
710

Jim Blandy's avatar
Jim Blandy committed
711
DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
Juanma Barranquero's avatar
Juanma Barranquero committed
712
       doc: /* Define SYMBOL as a variable, and return SYMBOL.
713 714 715 716 717 718
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.

719 720 721 722 723
If SYMBOL's value is void and the optional argument INITVALUE is
provided, INITVALUE is evaluated and the result used to set SYMBOL's
value.  If SYMBOL is buffer-local, its default value is what is set;
buffer-local values are not affected.  If INITVALUE is missing,
SYMBOL's value is not set.
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
729
for these variables.  (`defconst' and `defcustom' behave similarly in
730
this respect.)
731 732 733 734 735

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

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

741 742
  sym = XCAR (args);
  tail = XCDR (args);
743

744
  if (!NILP (tail))
Jim Blandy's avatar
Jim Blandy committed
745
    {
746
      if (!NILP (XCDR (tail)) && !NILP (XCDR (XCDR (tail))))
747 748 749 750
	error ("Too many arguments");

      tem = Fdefault_boundp (sym);

751
      /* Do it before evaluating the initial value, for self-references.  */
752
      XSYMBOL (sym)->u.s.declared_special = true;
753

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

Jim Blandy's avatar
Jim Blandy committed
789 790 791 792
  return sym;
}

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

812
  sym = XCAR (args);
813 814 815 816 817 818 819
  Lisp_Object docstring = Qnil;
  if (!NILP (XCDR (XCDR (args))))
    {
      if (!NILP (XCDR (XCDR (XCDR (args)))))
	error ("Too many arguments");
      docstring = XCAR (XCDR (XCDR (args)));
    }
820

821
  tem = eval_sub (XCAR (XCDR (args)));
822 823 824
  if (!NILP (Vpurify_flag))
    tem = Fpurecopy (tem);
  Fset_default (sym, tem);
825
  XSYMBOL (sym)->u.s.declared_special = true;
826
  if (!NILP (docstring))
Jim Blandy's avatar
Jim Blandy committed
827