eval.c 105 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Evaluator for GNU Emacs Lisp interpreter.
2 3
   Copyright (C) 1985-1987, 1993-1995, 1999-2013 Free Software
   Foundation, Inc.
Jim Blandy's avatar
Jim Blandy committed
4 5 6

This file is part of GNU Emacs.

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

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
18
along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
Jim Blandy's avatar
Jim Blandy committed
19 20


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

31 32 33 34
#if HAVE_X_WINDOWS
#include "xterm.h"
#endif

35 36 37
#if !BYTE_MARK_STACK
static
#endif
Jim Blandy's avatar
Jim Blandy committed
38 39
struct catchtag *catchlist;

40 41 42 43 44 45 46 47 48 49 50
/* Chain of condition handlers currently in effect.
   The elements of this chain are contained in the stack frames
   of Fcondition_case and internal_condition_case.
   When an error is signaled (by calling Fsignal, below),
   this chain is searched for an element that applies.  */

#if !BYTE_MARK_STACK
static
#endif
struct handler *handlerlist;

51 52 53 54 55
#ifdef DEBUG_GCPRO
/* Count levels of GCPRO to detect failure to UNGCPRO.  */
int gcpro_level;
#endif

56
Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp;
57
Lisp_Object Qinhibit_quit;
58 59
Lisp_Object Qand_rest;
static Lisp_Object Qand_optional;
60
static Lisp_Object Qinhibit_debugger;
61
static Lisp_Object Qdeclare;
62 63
Lisp_Object Qinternal_interpreter_environment, Qclosure;

Paul Eggert's avatar
Paul Eggert committed
64
static Lisp_Object Qdebug;
Jim Blandy's avatar
Jim Blandy committed
65

66 67 68
/* This holds either the symbol `run-hooks' or nil.
   It is nil at an early stage of startup, and when Emacs
   is shutting down.  */
69

Jim Blandy's avatar
Jim Blandy committed
70 71 72 73 74
Lisp_Object Vrun_hooks;

/* Non-nil means record all fset's and provide's, to be undone
   if the file being autoloaded is not fully loaded.
   They are recorded by being consed onto the front of Vautoload_queue:
75
   (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide.  */
Jim Blandy's avatar
Jim Blandy committed
76 77 78

Lisp_Object Vautoload_queue;

79 80
/* Current number of specbindings allocated in specpdl, not counting
   the dummy entry specpdl[-1].  */
81

82
ptrdiff_t specpdl_size;
Jim Blandy's avatar
Jim Blandy committed
83

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

87
union specbinding *specpdl;
Jim Blandy's avatar
Jim Blandy committed
88 89

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

91
union specbinding *specpdl_ptr;
Jim Blandy's avatar
Jim Blandy committed
92 93

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

95
static EMACS_INT lisp_eval_depth;
Jim Blandy's avatar
Jim Blandy committed
96

97
/* The value of num_nonmacro_input_events as of the last time we
98
   started to enter the debugger.  If we decide to enter the debugger
99
   again when this is still equal to num_nonmacro_input_events, then we
100 101 102
   know that the debugger itself has an error, and we should just
   signal the error instead of entering an infinite loop of debugger
   invocations.  */
103

104
static EMACS_INT when_entered_debugger;
Jim Blandy's avatar
Jim Blandy committed
105

106 107
/* The function from which the last `signal' was called.  Set in
   Fsignal.  */
108
/* FIXME: We should probably get rid of this!  */
109 110
Lisp_Object Vsignaling_function;

111 112 113 114 115 116
/* If non-nil, Lisp code must not be run since some part of Emacs is
   in an inconsistent state.  Currently, x-create-frame uses this to
   avoid triggering window-configuration-change-hook while the new
   frame is half-initialized.  */
Lisp_Object inhibit_lisp_code;

117 118 119 120 121 122 123
/* 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;

124
static Lisp_Object funcall_lambda (Lisp_Object, ptrdiff_t, Lisp_Object *);
125
static Lisp_Object apply_lambda (Lisp_Object fun, Lisp_Object args);
126

127
static Lisp_Object
128
specpdl_symbol (union specbinding *pdl)
129 130
{
  eassert (pdl->kind >= SPECPDL_LET);
131
  return pdl->let.symbol;
132 133 134
}

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

static Lisp_Object
142
specpdl_where (union specbinding *pdl)
143 144
{
  eassert (pdl->kind > SPECPDL_LET);
145
  return pdl->let.where;
146 147 148
}

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

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

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

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

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

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

185
static void
186 187 188 189 190
set_backtrace_args (union specbinding *pdl, Lisp_Object *args)
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
  pdl->bt.args = args;
}
191

192
static void
193 194 195 196 197
set_backtrace_nargs (union specbinding *pdl, ptrdiff_t n)
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
  pdl->bt.nargs = n;
}
198

199
static void
200 201 202 203 204
set_backtrace_debug_on_exit (union specbinding *pdl, bool doe)
{
  eassert (pdl->kind == SPECPDL_BACKTRACE);
  pdl->bt.debug_on_exit = doe;
}
205 206 207

/* Helper functions to scan the backtrace.  */

208 209
bool
backtrace_p (union specbinding *pdl)
210
{ return pdl >= specpdl; }
211

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

221 222
union specbinding *
backtrace_next (union specbinding *pdl)
223
{
224 225 226 227
  pdl--;
  while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
    pdl--;
  return pdl;
228 229
}

230

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

  Vrun_hooks = Qnil;
Jim Blandy's avatar
Jim Blandy committed
243 244
}

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

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

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

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

272
Lisp_Object
273
call_debugger (Lisp_Object arg)
Jim Blandy's avatar
Jim Blandy committed
274
{
275
  bool debug_while_redisplaying;
276
  ptrdiff_t count = SPECPDL_INDEX ();
277
  Lisp_Object val;
278
  EMACS_INT old_max = max_specpdl_size;
279

280 281
  /* Temporarily bump up the stack limits,
     so the debugger won't run out of stack.  */
282

283 284 285 286 287 288 289 290 291
  max_specpdl_size += 1;
  record_unwind_protect (restore_stack_limits,
			 Fcons (make_number (old_max),
				make_number (max_lisp_eval_depth)));
  max_specpdl_size = old_max;

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

292
  if (max_specpdl_size - 100 < SPECPDL_INDEX ())
293
    max_specpdl_size = SPECPDL_INDEX () + 100;
294

295
#ifdef HAVE_WINDOW_SYSTEM
296 297
  if (display_hourglass_p)
    cancel_hourglass ();
298 299
#endif

Jim Blandy's avatar
Jim Blandy committed
300
  debug_on_next_call = 0;
301
  when_entered_debugger = num_nonmacro_input_events;
302 303 304 305 306

  /* 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;
307 308
  specbind (intern ("debugger-may-continue"),
	    debug_while_redisplaying ? Qnil : Qt);
309
  specbind (Qinhibit_redisplay, Qnil);
310
  specbind (Qinhibit_debugger, Qt);
311 312 313

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

317 318 319 320
  val = apply1 (Vdebugger, arg);

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

325
  return unbind_to (count, val);
Jim Blandy's avatar
Jim Blandy committed
326 327
}

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

350
  GCPRO1 (args);
Jim Blandy's avatar
Jim Blandy committed
351

352
  while (CONSP (args))
Jim Blandy's avatar
Jim Blandy committed
353
    {
354
      val = eval_sub (XCAR (args));
Jim Blandy's avatar
Jim Blandy committed
355
      if (!NILP (val))
Jim Blandy's avatar
Jim Blandy committed
356
	break;
357
      args = XCDR (args);
Jim Blandy's avatar
Jim Blandy committed
358 359 360 361 362 363 364
    }

  UNGCPRO;
  return val;
}

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

374
  GCPRO1 (args);
Jim Blandy's avatar
Jim Blandy committed
375

376
  while (CONSP (args))
Jim Blandy's avatar
Jim Blandy committed
377
    {
378
      val = eval_sub (XCAR (args));
Jim Blandy's avatar
Jim Blandy committed
379
      if (NILP (val))
Jim Blandy's avatar
Jim Blandy committed
380
	break;
381
      args = XCDR (args);
Jim Blandy's avatar
Jim Blandy committed
382 383 384 385 386 387 388
    }

  UNGCPRO;
  return val;
}

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

  GCPRO1 (args);
400
  cond = eval_sub (Fcar (args));
Jim Blandy's avatar
Jim Blandy committed
401 402
  UNGCPRO;

Jim Blandy's avatar
Jim Blandy committed
403
  if (!NILP (cond))
404
    return eval_sub (Fcar (Fcdr (args)));
Jim Blandy's avatar
Jim Blandy committed
405 406 407 408
  return Fprogn (Fcdr (Fcdr (args)));
}

DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
409 410 411 412 413 414 415 416
       doc: /* Try each clause until one succeeds.
Each clause looks like (CONDITION BODY...).  CONDITION is evaluated
and, if the value is non-nil, this clause succeeds:
then the expressions in BODY are evaluated and the last one's
value is the value of the cond-form.
If no clause succeeds, cond returns nil.
If a clause has one element, as in (CONDITION),
CONDITION's value if non-nil is returned from the cond-form.
417
usage: (cond CLAUSES...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
418
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
419 420 421 422 423 424
{
  register Lisp_Object clause, val;
  struct gcpro gcpro1;

  val = Qnil;
  GCPRO1 (args);
Jim Blandy's avatar
Jim Blandy committed
425
  while (!NILP (args))
Jim Blandy's avatar
Jim Blandy committed
426 427
    {
      clause = Fcar (args);
428
      val = eval_sub (Fcar (clause));
Jim Blandy's avatar
Jim Blandy committed
429
      if (!NILP (val))
Jim Blandy's avatar
Jim Blandy committed
430
	{
431 432
	  if (!EQ (XCDR (clause), Qnil))
	    val = Fprogn (XCDR (clause));
Jim Blandy's avatar
Jim Blandy committed
433 434
	  break;
	}
435
      args = XCDR (args);
Jim Blandy's avatar
Jim Blandy committed
436 437 438 439 440 441
    }
  UNGCPRO;

  return val;
}

Paul Eggert's avatar
Paul Eggert committed
442
DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
443
       doc: /* Eval BODY forms sequentially and return value of last one.
Juanma Barranquero's avatar
Juanma Barranquero committed
444
usage: (progn BODY...)  */)
445
  (Lisp_Object body)
Jim Blandy's avatar
Jim Blandy committed
446
{
447
  Lisp_Object val = Qnil;
Jim Blandy's avatar
Jim Blandy committed
448 449
  struct gcpro gcpro1;

450
  GCPRO1 (body);
Jim Blandy's avatar
Jim Blandy committed
451

452
  while (CONSP (body))
Jim Blandy's avatar
Jim Blandy committed
453
    {
454 455
      val = eval_sub (XCAR (body));
      body = XCDR (body);
Jim Blandy's avatar
Jim Blandy committed
456 457 458 459 460 461
    }

  UNGCPRO;
  return val;
}

462 463 464 465 466 467 468 469 470
/* Evaluate BODY sequentually, discarding its value.  Suitable for
   record_unwind_protect.  */

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

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

  args_left = args;
  val = Qnil;
  GCPRO2 (args, val);

486 487 488
  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
489 490 491 492 493 494

  UNGCPRO;
  return val;
}

DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
495
       doc: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
496 497 498
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
499
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
500
{
501
  struct gcpro gcpro1;
Jim Blandy's avatar
Jim Blandy committed
502

503 504
  GCPRO1 (args);
  eval_sub (XCAR (args));
505 506
  UNGCPRO;
  return Fprog1 (XCDR (args));
Jim Blandy's avatar
Jim Blandy committed
507 508 509
}

DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
510 511 512 513 514 515 516
       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
517
usage: (setq [SYM VAL]...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
518
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
519 520
{
  register Lisp_Object args_left;
521
  register Lisp_Object val, sym, lex_binding;
Jim Blandy's avatar
Jim Blandy committed
522 523
  struct gcpro gcpro1;

524
  if (NILP (args))
Jim Blandy's avatar
Jim Blandy committed
525 526 527 528 529 530 531
    return Qnil;

  args_left = args;
  GCPRO1 (args);

  do
    {
532
      val = eval_sub (Fcar (Fcdr (args_left)));
Jim Blandy's avatar
Jim Blandy committed
533
      sym = Fcar (args_left);
534

535
      /* Like for eval_sub, we do not check declared_special here since
536 537
	 it's been done when let-binding.  */
      if (!NILP (Vinternal_interpreter_environment) /* Mere optimization!  */
538
	  && SYMBOLP (sym)
539 540
	  && !NILP (lex_binding
		    = Fassq (sym, Vinternal_interpreter_environment)))
541 542 543 544
	XSETCDR (lex_binding, val); /* SYM is lexically bound.  */
      else
	Fset (sym, val);	/* SYM is dynamically bound.  */

Jim Blandy's avatar
Jim Blandy committed
545 546
      args_left = Fcdr (Fcdr (args_left));
    }
Juanma Barranquero's avatar
Juanma Barranquero committed
547
  while (!NILP (args_left));
Jim Blandy's avatar
Jim Blandy committed
548 549 550 551

  UNGCPRO;
  return val;
}
552

Jim Blandy's avatar
Jim Blandy committed
553
DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
554
       doc: /* Return the argument, without evaluating it.  `(quote x)' yields `x'.
555 556 557 558 559 560 561 562
Warning: `quote' does not construct its return value, but just returns
the value that was pre-constructed by the Lisp reader (see info node
`(elisp)Printed Representation').
This means that '(a . b) is not identical to (cons 'a 'b): the former
does not cons.  Quoting should be reserved for constants that will
never be modified by side-effects, unless you like self-modifying code.
See the common pitfall in info node `(elisp)Rearrangement' for an example
of unexpected results when a quoted object is modified.
Pavel Janík's avatar
Pavel Janík committed
563
usage: (quote ARG)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
564
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
565
{
566 567
  if (!NILP (Fcdr (args)))
    xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args));
Jim Blandy's avatar
Jim Blandy committed
568 569
  return Fcar (args);
}
570

Jim Blandy's avatar
Jim Blandy committed
571
DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
572 573 574 575
       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
576
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
577
{
578 579
  Lisp_Object quoted = XCAR (args);

580 581
  if (!NILP (Fcdr (args)))
    xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
582 583 584 585 586 587

  if (!NILP (Vinternal_interpreter_environment)
      && CONSP (quoted)
      && EQ (XCAR (quoted), Qlambda))
    /* This is a lambda expression within a lexical environment;
       return an interpreted closure instead of a simple lambda.  */
588 589
    return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment,
				   XCDR (quoted)));
590 591 592
  else
    /* Simply quote the argument.  */
    return quoted;
Jim Blandy's avatar
Jim Blandy committed
593 594
}

595

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

609 610
  CHECK_SYMBOL (new_alias);
  CHECK_SYMBOL (base_variable);
Gerd Moellmann's avatar
Gerd Moellmann committed
611

612
  sym = XSYMBOL (new_alias);
613 614

  if (sym->constant)
Stefan Monnier's avatar
Stefan Monnier committed
615 616
    /* Not sure why, but why not?  */
    error ("Cannot make a constant an alias");
617 618 619 620 621 622 623 624 625

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

626
  /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
627 628 629 630
     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)))
631
    set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1);
632 633

  {
634
    union specbinding *p;
635

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

642
  sym->declared_special = 1;
643
  XSYMBOL (base_variable)->declared_special = 1;
644 645
  sym->redirect = SYMBOL_VARALIAS;
  SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
646 647
  sym->constant = SYMBOL_CONSTANT_P (base_variable);
  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
}


Jim Blandy's avatar
Jim Blandy committed
655
DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
Juanma Barranquero's avatar
Juanma Barranquero committed
656
       doc: /* Define SYMBOL as a variable, and return SYMBOL.
657 658 659 660 661 662 663 664 665
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
666
If INITVALUE is missing, SYMBOL's value is not set.
667 668 669 670 671 672 673

If SYMBOL has a local binding, then this form affects the local
binding.  This is usually not what you want.  Thus, if you need to
load a file defining variables, with this form or with `defconst' or
`defcustom', you should always load that file _outside_ any bindings
for these variables.  \(`defconst' and `defcustom' behave similarly in
this respect.)
674 675 676 677 678

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

To define a user option, use `defcustom' instead of `defvar'.
679
usage: (defvar SYMBOL &optional INITVALUE DOCSTRING)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
680
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
681
{
682
  register Lisp_Object sym, tem, tail;
Jim Blandy's avatar
Jim Blandy committed
683 684

  sym = Fcar (args);
685 686
  tail = Fcdr (args);
  if (!NILP (Fcdr (Fcdr (tail))))
687
    error ("Too many arguments");
688

689
  tem = Fdefault_boundp (sym);
690
  if (!NILP (tail))
Jim Blandy's avatar
Jim Blandy committed
691
    {
692 693
      /* Do it before evaluating the initial value, for self-references.  */
      XSYMBOL (sym)->declared_special = 1;
694

Jim Blandy's avatar
Jim Blandy committed
695
      if (NILP (tem))
696
	Fset_default (sym, eval_sub (Fcar (tail)));
697 698 699
      else
	{ /* Check if there is really a global binding rather than just a let
	     binding that shadows the global unboundness of the var.  */
700
	  union specbinding *pdl = specpdl_ptr;
701
	  while (pdl > specpdl)
702
	    {
703 704 705
	      if ((--pdl)->kind >= SPECPDL_LET
		  && EQ (specpdl_symbol (pdl), sym)
		  && EQ (specpdl_old_value (pdl), Qunbound))
706
		{
707 708 709
		  message_with_string
		    ("Warning: defvar ignored because %s is let-bound",
		     SYMBOL_NAME (sym), 1);
710 711 712 713
		  break;
		}
	    }
	}
714
      tail = Fcdr (tail);
715 716
      tem = Fcar (tail);
      if (!NILP (tem))
717 718 719 720 721
	{
	  if (!NILP (Vpurify_flag))
	    tem = Fpurecopy (tem);
	  Fput (sym, Qvariable_documentation, tem);
	}
722
      LOADHIST_ATTACH (sym);
Jim Blandy's avatar
Jim Blandy committed
723
    }
724 725 726 727 728
  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).  */
729 730
    Vinternal_interpreter_environment
      = Fcons (sym, Vinternal_interpreter_environment);
731
  else
732 733 734 735 736
    {
      /* 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
737

Jim Blandy's avatar
Jim Blandy committed
738 739 740 741
  return sym;
}

DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
742
       doc: /* Define SYMBOL as a constant variable.
743 744 745 746 747 748 749 750 751 752 753 754 755
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.
756
usage: (defconst SYMBOL INITVALUE [DOCSTRING])  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
757
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
758 759 760 761
{
  register Lisp_Object sym, tem;

  sym = Fcar (args);
762
  if (!NILP (Fcdr (Fcdr (Fcdr (args)))))
763
    error ("Too many arguments");
764

765
  tem = eval_sub (Fcar (Fcdr (args)));
766 767 768
  if (!NILP (Vpurify_flag))
    tem = Fpurecopy (tem);
  Fset_default (sym, tem);
769
  XSYMBOL (sym)->declared_special = 1;
Jim Blandy's avatar
Jim Blandy committed
770
  tem = Fcar (Fcdr (Fcdr (args)));
Jim Blandy's avatar
Jim Blandy committed
771
  if (!NILP (tem))
Jim Blandy's avatar
Jim Blandy committed
772
    {
Jim Blandy's avatar
Jim Blandy committed
773
      if (!NILP (Vpurify_flag))
Jim Blandy's avatar
Jim Blandy committed
774 775 776
	tem = Fpurecopy (tem);
      Fput (sym, Qvariable_documentation, tem);
    }
777
  Fput (sym, Qrisky_local_variable, Qt);
778
  LOADHIST_ATTACH (sym);
Jim Blandy's avatar
Jim Blandy committed
779 780 781
  return sym;
}

782 783 784 785 786 787 788 789 790 791 792
/* Make SYMBOL lexically scoped.  */
DEFUN ("internal-make-var-non-special", Fmake_var_non_special,
       Smake_var_non_special, 1, 1, 0,
       doc: /* Internal function.  */)
     (Lisp_Object symbol)
{
  CHECK_SYMBOL (symbol);
  XSYMBOL (symbol)->declared_special = 0;
  return Qnil;
}

Jim Blandy's avatar
Jim Blandy committed
793 794

DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
795 796 797 798 799
       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.
800
usage: (let* VARLIST BODY...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
801
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
802
{
803
  Lisp_Object varlist, var, val, elt, lexenv;
804
  ptrdiff_t count = SPECPDL_INDEX ();
Jim Blandy's avatar
Jim Blandy committed
805 806 807 808
  struct gcpro gcpro1, gcpro2, gcpro3;

  GCPRO3 (args, elt, varlist);

809 810
  lexenv = Vinternal_interpreter_environment;

Jim Blandy's avatar
Jim Blandy committed
811
  varlist = Fcar (args);
812
  while (CONSP (varlist))
Jim Blandy's avatar
Jim Blandy committed
813 814
    {
      QUIT;
815 816

      elt = XCAR (varlist);
817
      if (SYMBOLP (elt))
818 819 820 821
	{
	  var = elt;
	  val = Qnil;
	}
Jim Blandy's avatar
Jim Blandy committed
822
      else if (! NILP (Fcdr (Fcdr (elt))))
823
	signal_error ("`let' bindings can have only one value-form", elt);