eval.c 127 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-2019 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"
Daniel Colascione's avatar
Daniel Colascione committed
32
#include "pdumper.h"
33

34 35 36 37 38 39 40 41 42
/* 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

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

Lisp_Object Vautoload_queue;

50 51 52 53 54
/* 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;

55 56
/* The function from which the last `signal' was called.  Set in
   Fsignal.  */
57
/* FIXME: We should probably get rid of this!  */
58 59
Lisp_Object Vsignaling_function;

60 61 62 63 64 65 66
/* 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;

67
static Lisp_Object funcall_lambda (Lisp_Object, ptrdiff_t, Lisp_Object *);
68
static Lisp_Object apply_lambda (Lisp_Object, Lisp_Object, ptrdiff_t);
Paul Pogonyshev's avatar
Paul Pogonyshev committed
69
static Lisp_Object lambda_arity (Lisp_Object);
70

71
static Lisp_Object
72
specpdl_symbol (union specbinding *pdl)
73 74
{
  eassert (pdl->kind >= SPECPDL_LET);
75
  return pdl->let.symbol;
76 77
}

78 79 80 81 82 83 84
static enum specbind_tag
specpdl_kind (union specbinding *pdl)
{
  eassert (pdl->kind >= SPECPDL_LET);
  return pdl->let.kind;
}

85
static Lisp_Object
86
specpdl_old_value (union specbinding *pdl)
87 88
{
  eassert (pdl->kind >= SPECPDL_LET);
89
  return pdl->let.old_value;
90 91
}

92 93 94 95 96 97 98
static void
set_specpdl_old_value (union specbinding *pdl, Lisp_Object val)
{
  eassert (pdl->kind >= SPECPDL_LET);
  pdl->let.old_value = val;
}

99
static Lisp_Object
100
specpdl_where (union specbinding *pdl)
101 102
{
  eassert (pdl->kind > SPECPDL_LET);
103
  return pdl->let.where;
104 105
}

Tom Tromey's avatar
Tom Tromey committed
106 107 108 109 110 111 112
static Lisp_Object
specpdl_saved_value (union specbinding *pdl)
{
  eassert (pdl->kind >= SPECPDL_LET);
  return pdl->let.saved_value;
}

113
static Lisp_Object
114
specpdl_arg (union specbinding *pdl)
115 116
{
  eassert (pdl->kind == SPECPDL_UNWIND);
117
  return pdl->unwind.arg;
118 119
}

120
Lisp_Object
121
backtrace_function (union specbinding *pdl)
122 123
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
124
  return pdl->bt.function;
125 126 127
}

static ptrdiff_t
128
backtrace_nargs (union specbinding *pdl)
129 130
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
131
  return pdl->bt.nargs;
132 133
}

134
Lisp_Object *
135
backtrace_args (union specbinding *pdl)
136 137
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
138
  return pdl->bt.args;
139 140 141
}

static bool
142
backtrace_debug_on_exit (union specbinding *pdl)
143 144
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
145
  return pdl->bt.debug_on_exit;
146 147
}

148
/* Functions to modify slots of backtrace records.  */
149

150
static void
151
set_backtrace_args (union specbinding *pdl, Lisp_Object *args, ptrdiff_t nargs)
152 153 154
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
  pdl->bt.args = args;
155
  pdl->bt.nargs = nargs;
156
}
157

158
static void
159 160 161 162 163
set_backtrace_debug_on_exit (union specbinding *pdl, bool doe)
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
  pdl->bt.debug_on_exit = doe;
}
164 165 166

/* Helper functions to scan the backtrace.  */

167 168
bool
backtrace_p (union specbinding *pdl)
169
{ return specpdl ? pdl >= specpdl : false; }
170

171 172 173 174
static bool
backtrace_thread_p (struct thread_state *tstate, union specbinding *pdl)
{ return pdl >= tstate->m_specpdl; }

175
union specbinding *
176
backtrace_top (void)
177
{
178 179 180 181 182 183
  /* This is so "xbacktrace" doesn't crash in pdumped Emacs if they
     invoke the command before init_eval_once_for_pdumper initializes
     specpdl machinery.  See also backtrace_p above.  */
  if (!specpdl)
    return NULL;

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

190 191 192 193 194 195 196 197 198
static union specbinding *
backtrace_thread_top (struct thread_state *tstate)
{
  union specbinding *pdl = tstate->m_specpdl_ptr - 1;
  while (backtrace_thread_p (tstate, pdl) && pdl->kind != SPECPDL_BACKTRACE)
    pdl--;
  return pdl;
}

199 200
union specbinding *
backtrace_next (union specbinding *pdl)
201
{
202 203 204 205
  pdl--;
  while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
    pdl--;
  return pdl;
206 207
}

Daniel Colascione's avatar
Daniel Colascione committed
208 209
static void init_eval_once_for_pdumper (void);

210 211 212 213 214 215 216 217 218
static union specbinding *
backtrace_thread_next (struct thread_state *tstate, union specbinding *pdl)
{
  pdl--;
  while (backtrace_thread_p (tstate, pdl) && pdl->kind != SPECPDL_BACKTRACE)
    pdl--;
  return pdl;
}

Andreas Schwab's avatar
Andreas Schwab committed
219
void
220
init_eval_once (void)
Jim Blandy's avatar
Jim Blandy committed
221
{
222
  /* Don't forget to update docs (lispref node "Local Variables").  */
223
  max_specpdl_size = 1500; /* 1300 is not enough for cl-generic.el.  */
224
  max_lisp_eval_depth = 800;
225
  Vrun_hooks = Qnil;
Daniel Colascione's avatar
Daniel Colascione committed
226 227 228 229 230 231 232 233 234 235
  pdumper_do_now_and_after_load (init_eval_once_for_pdumper);
}

static void
init_eval_once_for_pdumper (void)
{
  enum { size = 50 };
  union specbinding *pdlvec = malloc ((size + 1) * sizeof *specpdl);
  specpdl_size = size;
  specpdl = specpdl_ptr = pdlvec + 1;
Jim Blandy's avatar
Jim Blandy committed
236 237
}

Andreas Schwab's avatar
Andreas Schwab committed
238
void
239
init_eval (void)
Jim Blandy's avatar
Jim Blandy committed
240 241
{
  specpdl_ptr = specpdl;
242 243 244
  { /* 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
245 246
    handlerlist_sentinel = xzalloc (sizeof (struct handler));
    handlerlist = handlerlist_sentinel->nextfree = handlerlist_sentinel;
247
    struct handler *c = push_handler (Qunbound, CATCHER);
Ken Raeburn's avatar
Ken Raeburn committed
248 249 250
    eassert (c == handlerlist_sentinel);
    handlerlist_sentinel->nextfree = NULL;
    handlerlist_sentinel->next = NULL;
251
  }
Jim Blandy's avatar
Jim Blandy committed
252 253 254
  Vquit_flag = Qnil;
  debug_on_next_call = 0;
  lisp_eval_depth = 0;
255
  /* This is less than the initial value of num_nonmacro_input_events.  */
256
  when_entered_debugger = -1;
Jim Blandy's avatar
Jim Blandy committed
257 258
}

259 260 261 262 263 264 265 266 267 268
/* Ensure that *M is at least A + B if possible, or is its maximum
   value otherwise.  */

static void
max_ensure_room (intmax_t *m, intmax_t a, intmax_t b)
{
  intmax_t sum = INT_ADD_WRAPV (a, b, &sum) ? INTMAX_MAX : sum;
  *m = max (*m, sum);
}

269
/* Unwind-protect function used by call_debugger.  */
270

271
static void
272
restore_stack_limits (Lisp_Object data)
273
{
274 275
  integer_to_intmax (XCAR (data), &max_specpdl_size);
  integer_to_intmax (XCDR (data), &max_lisp_eval_depth);
276 277
}

278 279
static void grow_specpdl (void);

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

282
Lisp_Object
283
call_debugger (Lisp_Object arg)
Jim Blandy's avatar
Jim Blandy committed
284
{
285
  bool debug_while_redisplaying;
286
  ptrdiff_t count = SPECPDL_INDEX ();
287
  Lisp_Object val;
288
  intmax_t old_depth = max_lisp_eval_depth;
289
  /* Do not allow max_specpdl_size less than actual depth (Bug#16603).  */
290
  intmax_t old_max = max (max_specpdl_size, count);
291

292 293 294 295
  /* 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.  */
296
  max_ensure_room (&max_lisp_eval_depth, lisp_eval_depth, 100);
297

298 299
  /* While debugging Bug#16603, previous value of 100 was found
     too small to avoid specpdl overflow in the debugger itself.  */
300
  max_ensure_room (&max_specpdl_size, count, 200);
301 302 303 304 305 306 307 308 309 310

  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,
311
			 Fcons (make_int (old_max), make_int (old_depth)));
312

313
#ifdef HAVE_WINDOW_SYSTEM
314 315
  if (display_hourglass_p)
    cancel_hourglass ();
316 317
#endif

Jim Blandy's avatar
Jim Blandy committed
318
  debug_on_next_call = 0;
319
  when_entered_debugger = num_nonmacro_input_events;
320 321 322 323 324

  /* 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;
325 326
  specbind (intern ("debugger-may-continue"),
	    debug_while_redisplaying ? Qnil : Qt);
327
  specbind (Qinhibit_redisplay, Qnil);
328
  specbind (Qinhibit_debugger, Qt);
329

330 331 332 333 334
  /* 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);

335 336
#if 0 /* Binding this prevents execution of Lisp code during
	 redisplay, which necessarily leads to display problems.  */
337
  specbind (Qinhibit_eval_during_redisplay, Qt);
338
#endif
339

340 341 342 343
  val = apply1 (Vdebugger, arg);

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

348
  return unbind_to (count, val);
Jim Blandy's avatar
Jim Blandy committed
349 350
}

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

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

  return val;
}

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

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

  return val;
}

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

415
  cond = eval_sub (XCAR (args));
Jim Blandy's avatar
Jim Blandy committed
416

Jim Blandy's avatar
Jim Blandy committed
417
  if (!NILP (cond))
418
    return eval_sub (Fcar (XCDR (args)));
419
  return Fprogn (Fcdr (XCDR (args)));
Jim Blandy's avatar
Jim Blandy committed
420 421 422
}

DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
423 424 425 426 427
       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
428 429
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
430
If no clause succeeds, cond returns nil.
431
usage: (cond CLAUSES...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
432
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
433
{
434
  Lisp_Object val = args;
Jim Blandy's avatar
Jim Blandy committed
435

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

  return val;
}

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

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

  return val;
}

Paul Eggert's avatar
Paul Eggert committed
469
/* Evaluate BODY sequentially, discarding its value.  */
470 471

void
Paul Eggert's avatar
Paul Eggert committed
472
prog_ignore (Lisp_Object body)
473 474 475 476
{
  Fprogn (body);
}

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

DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
490 491 492 493 494 495 496
       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
497
usage: (setq [SYM VAL]...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
498
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
499
{
500
  Lisp_Object val = args, tail = args;
Jim Blandy's avatar
Jim Blandy committed
501

502
  for (EMACS_INT nargs = 0; CONSP (tail); nargs += 2)
503
    {
504
      Lisp_Object sym = XCAR (tail);
505 506
      tail = XCDR (tail);
      if (!CONSP (tail))
507
	xsignal2 (Qwrong_number_of_arguments, Qsetq, make_fixnum (nargs + 1));
508 509 510 511 512
      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.  */
513 514 515
      Lisp_Object lex_binding
	= ((!NILP (Vinternal_interpreter_environment) /* Mere optimization!  */
	    && SYMBOLP (sym))
516
	   ? Fassq (sym, Vinternal_interpreter_environment)
517 518
	   : Qnil);
      if (!NILP (lex_binding))
519 520 521
	XSETCDR (lex_binding, val); /* SYM is lexically bound.  */
      else
	Fset (sym, val);	/* SYM is dynamically bound.  */
Jim Blandy's avatar
Jim Blandy committed
522 523 524 525
    }

  return val;
}
526

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

Jim Blandy's avatar
Jim Blandy committed
545
DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
546
       doc: /* Like `quote', but preferred for objects which are functions.
547 548
In byte compilation, `function' causes its argument to be handled by
the byte compiler.  `quote' cannot do that.
Pavel Janík's avatar
Pavel Janík committed
549
usage: (function ARG)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
550
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
551
{
552 553
  Lisp_Object quoted = XCAR (args);

554
  if (!NILP (XCDR (args)))
555
    xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
556 557 558 559

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

582

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

596 597
  CHECK_SYMBOL (new_alias);
  CHECK_SYMBOL (base_variable);
Gerd Moellmann's avatar
Gerd Moellmann committed
598

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

Noam Postavsky's avatar
Noam Postavsky committed
603 604
  sym = XSYMBOL (new_alias);

605
  switch (sym->u.s.redirect)
606 607 608 609 610
    {
    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");
611 612 613 614 615
    case SYMBOL_PLAINVAL:
    case SYMBOL_VARALIAS:
      break;
    default:
      emacs_abort ();
616 617
    }

618
  /* https://lists.gnu.org/r/emacs-devel/2008-04/msg00834.html
619 620 621 622
     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
623 624
    set_internal (base_variable, find_symbol_value (new_alias),
                  Qnil, SET_INTERNAL_BIND);
625 626 627 628 629 630 631 632 633 634
  else if (!NILP (Fboundp (new_alias))
           && !EQ (find_symbol_value (new_alias),
                   find_symbol_value (base_variable)))
    call2 (intern ("display-warning"),
           list3 (intern ("defvaralias"), intern ("losing-value"), new_alias),
           CALLN (Fformat_message,
                  build_string
                  ("Overwriting value of `%s' by aliasing to `%s'"),
                  new_alias, base_variable));

635
  {
636
    union specbinding *p;
637

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

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

647 648 649
  sym->u.s.declared_special = true;
  XSYMBOL (base_variable)->u.s.declared_special = true;
  sym->u.s.redirect = SYMBOL_VARALIAS;
650
  SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
651
  sym->u.s.trapped_write = XSYMBOL (base_variable)->u.s.trapped_write;
652
  LOADHIST_ATTACH (new_alias);
653 654
  /* Even if docstring is nil: remove old docstring.  */
  Fput (new_alias, Qvariable_documentation, docstring);
655

656
  return base_variable;
Gerd Moellmann's avatar
Gerd Moellmann committed
657 658
}

659 660 661 662 663 664 665 666 667 668 669 670 671 672
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;
673 674

	case SPECPDL_UNWIND:
675
	case SPECPDL_UNWIND_ARRAY:
676 677
	case SPECPDL_UNWIND_PTR:
	case SPECPDL_UNWIND_INT:
678
	case SPECPDL_UNWIND_EXCURSION:
679 680 681 682 683 684 685
	case SPECPDL_UNWIND_VOID:
	case SPECPDL_BACKTRACE:
	case SPECPDL_LET_LOCAL:
	  break;

	default:
	  emacs_abort ();
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
	}
    }
  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
717

718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
DEFUN ("internal--define-uninitialized-variable",
       Finternal__define_uninitialized_variable,
       Sinternal__define_uninitialized_variable, 1, 2, 0,
       doc: /* Define SYMBOL as a variable, with DOC as its docstring.
This is like `defvar' and `defconst' but without affecting the variable's
value.  */)
  (Lisp_Object symbol, Lisp_Object doc)
{
  XSYMBOL (symbol)->u.s.declared_special = true;
  if (!NILP (doc))
    {
      if (!NILP (Vpurify_flag))
	doc = Fpurecopy (doc);
      Fput (symbol, Qvariable_documentation, doc);
    }
  LOADHIST_ATTACH (symbol);
  return Qnil;
}

Jim Blandy's avatar
Jim Blandy committed
737
DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
Juanma Barranquero's avatar
Juanma Barranquero committed
738
       doc: /* Define SYMBOL as a variable, and return SYMBOL.
739 740 741 742 743 744
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.

745 746 747 748 749
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.
750 751 752 753 754

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
755
for these variables.  (`defconst' and `defcustom' behave similarly in
756
this respect.)
757 758 759 760 761

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

To define a user option, use `defcustom' instead of `defvar'.
762
usage: (defvar SYMBOL &optional INITVALUE DOCSTRING)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
763
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
764
{
765
  Lisp_Object sym, tem, tail;
Jim Blandy's avatar
Jim Blandy committed
766

767 768
  sym = XCAR (args);
  tail = XCDR (args);
769

770 771
  CHECK_SYMBOL (sym);

772
  if (!NILP (tail))
Jim Blandy's avatar
Jim Blandy committed
773
    {
774
      if (!NILP (XCDR (tail)) && !NILP (XCDR (XCDR (tail))))
775
	error ("Too many arguments");
776
      Lisp_Object exp = XCAR (tail);
777 778

      tem = Fdefault_boundp (sym);
779
      tail = XCDR (tail);
780

781
      /* Do it before evaluating the initial value, for self-references.  */
782
      Finternal__define_uninitialized_variable (sym, CAR (tail));
783

Jim Blandy's avatar
Jim Blandy committed
784
      if (NILP (tem))
785
	Fset_default (sym, eval_sub (exp));
786 787 788
      else
	{ /* Check if there is really a global binding rather than just a let
	     binding that shadows the global unboundness of the var.  */
789 790
	  union specbinding *binding = default_toplevel_binding (sym);
	  if (binding && EQ (specpdl_old_value (binding), Qunbound))
791
	    {
792
	      set_specpdl_old_value (binding, eval_sub (exp));
793 794
	    }
	}
Jim Blandy's avatar
Jim Blandy committed
795
    }
796
  else if (!NILP (Vinternal_interpreter_environment)
797
	   && (SYMBOLP (sym) && !XSYMBOL (sym)->u.s.declared_special))
798 799 800
    /* 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).  */
801 802
    Vinternal_interpreter_environment
      = Fcons (sym, Vinternal_interpreter_environment);
803
  else
804 805 806 807 808
    {
      /* 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
809

Jim Blandy's avatar
Jim Blandy committed
810 811 812 813
  return sym;
}

DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
814
       doc: /* Define SYMBOL as a constant variable.
815 816 817 818 819 820 821 822 823 824 825 826 827
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.
828
usage: (defconst SYMBOL INITVALUE [DOCSTRING])  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
829
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
830
{
831
  Lisp_Object sym, tem;
Jim Blandy's avatar
Jim Blandy committed
832

833
  sym = XCAR (args);
834 835 836 837 838 839 840
  Lisp_Object docstring = Qnil;
  if (!NILP (XCDR (XCDR (args))))
    {
      if (!NILP (XCDR (XCDR (XCDR (args)))))
	error ("Too many arguments");
      docstring = XCAR (XCDR (XCDR (args)));
    }
841

842
  Finternal__define_uninitialized_variable (sym, docstring);
843
  tem = eval_sub (XCAR (XCDR (args)));
844 845
  if (!NILP (Vpurify_flag))
    tem = Fpurecopy (tem);
846 847
  Fset_default (sym, tem);      /* FIXME: set-default-toplevel-value? */
  Fput (sym, Qrisky_local_variable, Qt); /* FIXME: Why?  */
Jim Blandy's avatar
Jim Blandy committed
848 849 850
  return sym;
}

851 852 853 854 855 856 857
/* 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);
858
  XSYMBOL (symbol)->u.s.declared_special = false;
859 860 861
  return Qnil;
}

Jim Blandy's avatar
Jim Blandy committed
862 863

DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
864 865 866 867 868
       doc: /* Bind variables according to VARLIST then eval BODY.
The value of the last form in BODY is returned.
Each element of VARLIST is a symbol (which is bound to nil)
or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
Each VALUEFORM can refer to the symbols already bound by this VARLIST.
869
usage: (let* VARLIST BODY...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
870
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
871
{
872
  Lisp_Object var, val, elt, lexenv;
873
  ptrdiff_t count = SPECPDL_INDEX ();
Jim Blandy's avatar
Jim Blandy committed
874

875 876
  lexenv = Vinternal_interpreter_environment;

877 878
  Lisp_Object varlist = XCAR (args);
  while (CONSP (varlist))
Jim Blandy's avatar
Jim Blandy committed
879
    {
Paul Eggert's avatar
Paul Eggert committed
880
      maybe_quit ();
881 882

      elt = XCAR (varlist);
883
      varlist = XCDR (varlist);
884
      if (SYMBOLP (elt))
885 886 887 888
	{
	  var = elt;
	  val = Qnil;
	}
Jim Blandy's avatar
Jim Blandy committed
889 890
      else
	{
891
	  var = Fcar (elt);
892 893 894
	  if (! NILP (Fcdr (XCDR (elt))))
	    signal_error ("`let' bindings can have only one value-form", elt);
	  val = eval_sub (Fcar (XCDR (elt)));
Jim Blandy's avatar
Jim Blandy committed
895
	}
896

897
      if (!NILP (lexenv) && SYMBOLP (var)
898
	  && !XSYMBOL (var)->u.s.declared_special
899
	  && NILP (Fmemq (var, Vinternal_interpreter_environment)))
900 901 902
	/* Lexically bind VAR by adding it to the interpreter's binding
	   alist.  */
	{
903 904 905 906 907 908 909 910 911
	  Lisp_Object newenv
	    = Fcons (Fcons (var, val), Vinternal_interpreter_environment);
	  if (EQ (Vinternal_interpreter_environment, lexenv))
	    /* Save the old lexical environment on the specpdl stack,
	       but only for the first lexical binding, since we'll never
	       need to revert to one of the intermediate ones.  */
	    specbind (Qinternal_interpreter_environment, newenv);
	  else
	    Vinternal_interpreter_environment = newenv;
Jim Blandy's avatar
Jim Blandy committed