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

This file is part of GNU Emacs.

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

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
17
along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
Jim Blandy's avatar
Jim Blandy committed
18 19


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

29 30 31 32
#if HAVE_X_WINDOWS
#include "xterm.h"
#endif

Jim Blandy's avatar
Jim Blandy committed
33 34 35 36
/* This definition is duplicated in alloc.c and keyboard.c */
/* Putting it in lisp.h makes cc bomb out! */

struct backtrace
37 38 39 40 41
{
  struct backtrace *next;
  Lisp_Object *function;
  Lisp_Object *args;	/* Points to vector of args. */
  int nargs;		/* Length of vector.
Jim Blandy's avatar
Jim Blandy committed
42 43
			   If nargs is UNEVALLED, args points to slot holding
			   list of unevalled args */
44 45 46 47
  char evalargs;
  /* Nonzero means call value of debugger when done with this operation. */
  char debug_on_exit;
};
Jim Blandy's avatar
Jim Blandy committed
48 49 50 51 52

struct backtrace *backtrace_list;

struct catchtag *catchlist;

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

58
Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp, Qdefun;
59
Lisp_Object Qinhibit_quit;
Jim Blandy's avatar
Jim Blandy committed
60 61
Lisp_Object Qand_rest, Qand_optional;
Lisp_Object Qdebug_on_error;
62
Lisp_Object Qdeclare;
63
Lisp_Object Qdebug;
Jim Blandy's avatar
Jim Blandy committed
64

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

Jim Blandy's avatar
Jim Blandy committed
69 70 71 72 73
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:
74
   (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide.  */
Jim Blandy's avatar
Jim Blandy committed
75 76 77 78

Lisp_Object Vautoload_queue;

/* Current number of specbindings allocated in specpdl.  */
79

80
EMACS_INT specpdl_size;
Jim Blandy's avatar
Jim Blandy committed
81 82

/* Pointer to beginning of specpdl.  */
83

Jim Blandy's avatar
Jim Blandy committed
84 85 86
struct specbinding *specpdl;

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

88
struct specbinding *specpdl_ptr;
Jim Blandy's avatar
Jim Blandy committed
89 90

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

92
EMACS_INT lisp_eval_depth;
Jim Blandy's avatar
Jim Blandy committed
93

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

101
int when_entered_debugger;
Jim Blandy's avatar
Jim Blandy committed
102

103 104 105 106 107
/* The function from which the last `signal' was called.  Set in
   Fsignal.  */

Lisp_Object Vsignaling_function;

108 109 110 111 112 113
/* Set to non-zero while processing X events.  Checked in Feval to
   make sure the Lisp interpreter isn't called from a signal handler,
   which is unsafe because the interpreter isn't reentrant.  */

int handling_signal;

114 115
static Lisp_Object funcall_lambda (Lisp_Object, int, Lisp_Object*);
static void unwind_to_catch (struct catchtag *, Lisp_Object) NO_RETURN;
Andreas Schwab's avatar
Andreas Schwab committed
116 117
static int interactive_p (int);
static Lisp_Object apply_lambda (Lisp_Object, Lisp_Object, int);
118

Andreas Schwab's avatar
Andreas Schwab committed
119
void
120
init_eval_once (void)
Jim Blandy's avatar
Jim Blandy committed
121 122
{
  specpdl_size = 50;
123
  specpdl = (struct specbinding *) xmalloc (specpdl_size * sizeof (struct specbinding));
124
  specpdl_ptr = specpdl;
125
  /* Don't forget to update docs (lispref node "Local Variables").  */
126
  max_specpdl_size = 1000;
127
  max_lisp_eval_depth = 600;
128 129

  Vrun_hooks = Qnil;
Jim Blandy's avatar
Jim Blandy committed
130 131
}

Andreas Schwab's avatar
Andreas Schwab committed
132
void
133
init_eval (void)
Jim Blandy's avatar
Jim Blandy committed
134 135 136 137 138 139 140 141
{
  specpdl_ptr = specpdl;
  catchlist = 0;
  handlerlist = 0;
  backtrace_list = 0;
  Vquit_flag = Qnil;
  debug_on_next_call = 0;
  lisp_eval_depth = 0;
142
#ifdef DEBUG_GCPRO
143
  gcpro_level = 0;
144
#endif
145
  /* This is less than the initial value of num_nonmacro_input_events.  */
146
  when_entered_debugger = -1;
Jim Blandy's avatar
Jim Blandy committed
147 148
}

149 150 151
/* unwind-protect function used by call_debugger.  */

static Lisp_Object
152
restore_stack_limits (Lisp_Object data)
153 154 155
{
  max_specpdl_size = XINT (XCAR (data));
  max_lisp_eval_depth = XINT (XCDR (data));
156
  return Qnil;
157 158 159 160
}

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

161
static Lisp_Object
162
call_debugger (Lisp_Object arg)
Jim Blandy's avatar
Jim Blandy committed
163
{
164
  int debug_while_redisplaying;
Juanma Barranquero's avatar
Juanma Barranquero committed
165
  int count = SPECPDL_INDEX ();
166
  Lisp_Object val;
167
  EMACS_INT old_max = max_specpdl_size;
168

169 170
  /* Temporarily bump up the stack limits,
     so the debugger won't run out of stack.  */
171

172 173 174 175 176 177 178 179 180 181 182
  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;

  if (SPECPDL_INDEX () + 100 > max_specpdl_size)
    max_specpdl_size = SPECPDL_INDEX () + 100;
183

184
#ifdef HAVE_WINDOW_SYSTEM
185 186
  if (display_hourglass_p)
    cancel_hourglass ();
187 188
#endif

Jim Blandy's avatar
Jim Blandy committed
189
  debug_on_next_call = 0;
190
  when_entered_debugger = num_nonmacro_input_events;
191 192 193 194 195

  /* 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;
196 197
  specbind (intern ("debugger-may-continue"),
	    debug_while_redisplaying ? Qnil : Qt);
198
  specbind (Qinhibit_redisplay, Qnil);
199
  specbind (Qdebug_on_error, Qnil);
200 201 202

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

206 207 208 209
  val = apply1 (Vdebugger, arg);

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

214
  return unbind_to (count, val);
Jim Blandy's avatar
Jim Blandy committed
215 216
}

217
static void
218
do_debug_on_call (Lisp_Object code)
Jim Blandy's avatar
Jim Blandy committed
219 220 221 222 223 224 225 226 227 228 229
{
  debug_on_next_call = 0;
  backtrace_list->debug_on_exit = 1;
  call_debugger (Fcons (code, Qnil));
}

/* 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
230 231 232
       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
233
usage: (or CONDITIONS...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
234
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
235
{
236
  register Lisp_Object val = Qnil;
Jim Blandy's avatar
Jim Blandy committed
237 238
  struct gcpro gcpro1;

239
  GCPRO1 (args);
Jim Blandy's avatar
Jim Blandy committed
240

241
  while (CONSP (args))
Jim Blandy's avatar
Jim Blandy committed
242
    {
243
      val = Feval (XCAR (args));
Jim Blandy's avatar
Jim Blandy committed
244
      if (!NILP (val))
Jim Blandy's avatar
Jim Blandy committed
245
	break;
246
      args = XCDR (args);
Jim Blandy's avatar
Jim Blandy committed
247 248 249 250 251 252 253
    }

  UNGCPRO;
  return val;
}

DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
254
       doc: /* Eval args until one of them yields nil, then return nil.
Pavel Janík's avatar
Pavel Janík committed
255 256
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
257
usage: (and CONDITIONS...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
258
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
259
{
260
  register Lisp_Object val = Qt;
Jim Blandy's avatar
Jim Blandy committed
261 262
  struct gcpro gcpro1;

263
  GCPRO1 (args);
Jim Blandy's avatar
Jim Blandy committed
264

265
  while (CONSP (args))
Jim Blandy's avatar
Jim Blandy committed
266
    {
267
      val = Feval (XCAR (args));
Jim Blandy's avatar
Jim Blandy committed
268
      if (NILP (val))
Jim Blandy's avatar
Jim Blandy committed
269
	break;
270
      args = XCDR (args);
Jim Blandy's avatar
Jim Blandy committed
271 272 273 274 275 276 277
    }

  UNGCPRO;
  return val;
}

DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
278
       doc: /* If COND yields non-nil, do THEN, else do ELSE...
Pavel Janík's avatar
Pavel Janík committed
279 280 281
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.
282
usage: (if COND THEN ELSE...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
283
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
284 285 286 287 288 289 290 291
{
  register Lisp_Object cond;
  struct gcpro gcpro1;

  GCPRO1 (args);
  cond = Feval (Fcar (args));
  UNGCPRO;

Jim Blandy's avatar
Jim Blandy committed
292
  if (!NILP (cond))
Jim Blandy's avatar
Jim Blandy committed
293 294 295 296 297
    return Feval (Fcar (Fcdr (args)));
  return Fprogn (Fcdr (Fcdr (args)));
}

DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
298 299 300 301 302 303 304 305
       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.
306
usage: (cond CLAUSES...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
307
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
308 309 310 311 312 313
{
  register Lisp_Object clause, val;
  struct gcpro gcpro1;

  val = Qnil;
  GCPRO1 (args);
Jim Blandy's avatar
Jim Blandy committed
314
  while (!NILP (args))
Jim Blandy's avatar
Jim Blandy committed
315 316 317
    {
      clause = Fcar (args);
      val = Feval (Fcar (clause));
Jim Blandy's avatar
Jim Blandy committed
318
      if (!NILP (val))
Jim Blandy's avatar
Jim Blandy committed
319
	{
320 321
	  if (!EQ (XCDR (clause), Qnil))
	    val = Fprogn (XCDR (clause));
Jim Blandy's avatar
Jim Blandy committed
322 323
	  break;
	}
324
      args = XCDR (args);
Jim Blandy's avatar
Jim Blandy committed
325 326 327 328 329 330 331
    }
  UNGCPRO;

  return val;
}

DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
332
       doc: /* Eval BODY forms sequentially and return value of last one.
Juanma Barranquero's avatar
Juanma Barranquero committed
333
usage: (progn BODY...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
334
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
335
{
336
  register Lisp_Object val = Qnil;
Jim Blandy's avatar
Jim Blandy committed
337 338
  struct gcpro gcpro1;

339
  GCPRO1 (args);
Jim Blandy's avatar
Jim Blandy committed
340

341
  while (CONSP (args))
Jim Blandy's avatar
Jim Blandy committed
342
    {
343 344
      val = Feval (XCAR (args));
      args = XCDR (args);
Jim Blandy's avatar
Jim Blandy committed
345 346 347 348 349 350 351
    }

  UNGCPRO;
  return val;
}

DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
352
       doc: /* Eval FIRST and BODY sequentially; return value from FIRST.
Pavel Janík's avatar
Pavel Janík committed
353 354
The value of FIRST is saved during the evaluation of the remaining args,
whose values are discarded.
355
usage: (prog1 FIRST BODY...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
356
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
357 358 359 360 361 362
{
  Lisp_Object val;
  register Lisp_Object args_left;
  struct gcpro gcpro1, gcpro2;
  register int argnum = 0;

363
  if (NILP (args))
Jim Blandy's avatar
Jim Blandy committed
364 365 366 367 368 369 370 371 372
    return Qnil;

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

  do
    {
      if (!(argnum++))
Andreas Schwab's avatar
Andreas Schwab committed
373
	val = Feval (Fcar (args_left));
Jim Blandy's avatar
Jim Blandy committed
374 375 376 377
      else
	Feval (Fcar (args_left));
      args_left = Fcdr (args_left);
    }
Jim Blandy's avatar
Jim Blandy committed
378
  while (!NILP(args_left));
Jim Blandy's avatar
Jim Blandy committed
379 380 381 382 383 384

  UNGCPRO;
  return val;
}

DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
385
       doc: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
386 387 388
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
389
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
390 391 392 393 394 395 396 397
{
  Lisp_Object val;
  register Lisp_Object args_left;
  struct gcpro gcpro1, gcpro2;
  register int argnum = -1;

  val = Qnil;

Richard M. Stallman's avatar
Richard M. Stallman committed
398
  if (NILP (args))
Jim Blandy's avatar
Jim Blandy committed
399 400 401 402 403 404 405 406 407
    return Qnil;

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

  do
    {
      if (!(argnum++))
Andreas Schwab's avatar
Andreas Schwab committed
408
	val = Feval (Fcar (args_left));
Jim Blandy's avatar
Jim Blandy committed
409 410 411 412
      else
	Feval (Fcar (args_left));
      args_left = Fcdr (args_left);
    }
Richard M. Stallman's avatar
Richard M. Stallman committed
413
  while (!NILP (args_left));
Jim Blandy's avatar
Jim Blandy committed
414 415 416 417 418 419

  UNGCPRO;
  return val;
}

DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
420 421 422 423 424 425 426
       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
427
usage: (setq [SYM VAL]...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
428
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
429 430 431 432 433
{
  register Lisp_Object args_left;
  register Lisp_Object val, sym;
  struct gcpro gcpro1;

434
  if (NILP (args))
Jim Blandy's avatar
Jim Blandy committed
435 436 437 438 439 440 441 442 443 444 445 446
    return Qnil;

  args_left = args;
  GCPRO1 (args);

  do
    {
      val = Feval (Fcar (Fcdr (args_left)));
      sym = Fcar (args_left);
      Fset (sym, val);
      args_left = Fcdr (Fcdr (args_left));
    }
Jim Blandy's avatar
Jim Blandy committed
447
  while (!NILP(args_left));
Jim Blandy's avatar
Jim Blandy committed
448 449 450 451

  UNGCPRO;
  return val;
}
452

Jim Blandy's avatar
Jim Blandy committed
453
DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
454 455
       doc: /* Return the argument, without evaluating it.  `(quote x)' yields `x'.
usage: (quote ARG)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
456
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
457
{
458 459
  if (!NILP (Fcdr (args)))
    xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args));
Jim Blandy's avatar
Jim Blandy committed
460 461
  return Fcar (args);
}
462

Jim Blandy's avatar
Jim Blandy committed
463
DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
464 465 466 467
       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
468
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
469
{
470 471
  if (!NILP (Fcdr (args)))
    xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
Jim Blandy's avatar
Jim Blandy committed
472 473 474
  return Fcar (args);
}

475

Jim Blandy's avatar
Jim Blandy committed
476
DEFUN ("interactive-p", Finteractive_p, Sinteractive_p, 0, 0, 0,
477
       doc: /* Return t if the containing function was run directly by user input.
478 479
This means that the function was called with `call-interactively'
\(which includes being called as the binding of a key)
480
and input is currently coming from the keyboard (not a keyboard macro),
481 482 483 484 485 486 487 488
and Emacs is not running in batch mode (`noninteractive' is nil).

The only known proper use of `interactive-p' is in deciding whether to
display a helpful message, or how to display it.  If you're thinking
of using it for any other purpose, it is quite likely that you're
making a mistake.  Think: what do you want to do when the command is
called from a keyboard macro?

489 490 491 492
To test whether your function was called with `call-interactively',
either (i) add an extra optional argument and give it an `interactive'
spec that specifies non-nil unconditionally (such as \"p\"); or (ii)
use `called-interactively-p'.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
493
  (void)
Jim Blandy's avatar
Jim Blandy committed
494
{
495
  return (INTERACTIVE && interactive_p (1)) ? Qt : Qnil;
496 497 498
}


499
DEFUN ("called-interactively-p", Fcalled_interactively_p, Scalled_interactively_p, 0, 1, 0,
500
       doc: /* Return t if the containing function was called by `call-interactively'.
501 502 503 504 505 506 507 508 509 510 511 512
If KIND is `interactive', then only return t if the call was made
interactively by the user, i.e. not in `noninteractive' mode nor
when `executing-kbd-macro'.
If KIND is `any', on the other hand, it will return t for any kind of
interactive call, including being called as the binding of a key, or
from a keyboard macro, or in `noninteractive' mode.

The only known proper use of `interactive' for KIND is in deciding
whether to display a helpful message, or how to display it.  If you're
thinking of using it for any other purpose, it is quite likely that
you're making a mistake.  Think: what do you want to do when the
command is called from a keyboard macro?
513 514 515 516 517

This function is meant for implementing advice and other
function-modifying features.  Instead of using this, it is sometimes
cleaner to give your function an extra optional argument whose
`interactive' spec specifies non-nil unconditionally (\"p\" is a good
518
way to do this), or via (not (or executing-kbd-macro noninteractive)).  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
519
  (Lisp_Object kind)
520
{
521 522
  return ((INTERACTIVE || !EQ (kind, intern ("interactive")))
	  && interactive_p (1)) ? Qt : Qnil;
523 524 525 526 527
}


/*  Return 1 if function in which this appears was called using
    call-interactively.
528 529 530 531

    EXCLUDE_SUBRS_P non-zero means always return 0 if the function
    called is a built-in.  */

Andreas Schwab's avatar
Andreas Schwab committed
532
static int
533
interactive_p (int exclude_subrs_p)
534 535 536
{
  struct backtrace *btp;
  Lisp_Object fun;
Jim Blandy's avatar
Jim Blandy committed
537 538

  btp = backtrace_list;
Jim Blandy's avatar
Jim Blandy committed
539 540

  /* If this isn't a byte-compiled function, there may be a frame at
541
     the top for Finteractive_p.  If so, skip it.  */
542
  fun = Findirect_function (*btp->function, Qnil);
543 544
  if (SUBRP (fun) && (XSUBR (fun) == &Sinteractive_p
		      || XSUBR (fun) == &Scalled_interactively_p))
Jim Blandy's avatar
Jim Blandy committed
545
    btp = btp->next;
Jim Blandy's avatar
Jim Blandy committed
546 547

  /* If we're running an Emacs 18-style byte-compiled function, there
548 549 550
     may be a frame for Fbytecode at the top level.  In any version of
     Emacs there can be Fbytecode frames for subexpressions evaluated
     inside catch and condition-case.  Skip past them.
Jim Blandy's avatar
Jim Blandy committed
551

552
     If this isn't a byte-compiled function, then we may now be
Jim Blandy's avatar
Jim Blandy committed
553
     looking at several frames for special forms.  Skip past them.  */
554 555 556
  while (btp
	 && (EQ (*btp->function, Qbytecode)
	     || btp->nargs == UNEVALLED))
Jim Blandy's avatar
Jim Blandy committed
557 558
    btp = btp->next;

Jim Blandy's avatar
Jim Blandy committed
559 560 561 562
  /* btp now points at the frame of the innermost function that isn't
     a special form, ignoring frames for Finteractive_p and/or
     Fbytecode at the top.  If this frame is for a built-in function
     (such as load or eval-region) return nil.  */
563
  fun = Findirect_function (*btp->function, Qnil);
564 565
  if (exclude_subrs_p && SUBRP (fun))
    return 0;
566

Jim Blandy's avatar
Jim Blandy committed
567 568 569
  /* btp points to the frame of a Lisp function that called interactive-p.
     Return t if that function was called interactively.  */
  if (btp && btp->next && EQ (*btp->next->function, Qcall_interactively))
570 571
    return 1;
  return 0;
Jim Blandy's avatar
Jim Blandy committed
572 573
}

574

Jim Blandy's avatar
Jim Blandy committed
575
DEFUN ("defun", Fdefun, Sdefun, 2, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
576 577 578
       doc: /* Define NAME as a function.
The definition is (lambda ARGLIST [DOCSTRING] BODY...).
See also the function `interactive'.
579
usage: (defun NAME ARGLIST [DOCSTRING] BODY...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
580
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
581 582 583 584 585
{
  register Lisp_Object fn_name;
  register Lisp_Object defn;

  fn_name = Fcar (args);
586
  CHECK_SYMBOL (fn_name);
Jim Blandy's avatar
Jim Blandy committed
587
  defn = Fcons (Qlambda, Fcdr (args));
Jim Blandy's avatar
Jim Blandy committed
588
  if (!NILP (Vpurify_flag))
Jim Blandy's avatar
Jim Blandy committed
589
    defn = Fpurecopy (defn);
590 591 592
  if (CONSP (XSYMBOL (fn_name)->function)
      && EQ (XCAR (XSYMBOL (fn_name)->function), Qautoload))
    LOADHIST_ATTACH (Fcons (Qt, fn_name));
Jim Blandy's avatar
Jim Blandy committed
593
  Ffset (fn_name, defn);
594
  LOADHIST_ATTACH (Fcons (Qdefun, fn_name));
Jim Blandy's avatar
Jim Blandy committed
595 596 597 598
  return fn_name;
}

DEFUN ("defmacro", Fdefmacro, Sdefmacro, 2, UNEVALLED, 0,
Pavel Janík's avatar
Pavel Janík committed
599
       doc: /* Define NAME as a macro.
Richard M. Stallman's avatar
Richard M. Stallman committed
600 601
The actual definition looks like
 (macro lambda ARGLIST [DOCSTRING] [DECL] BODY...).
Pavel Janík's avatar
Pavel Janík committed
602 603 604 605
When the macro is called, as in (NAME ARGS...),
the function (lambda ARGLIST BODY...) is applied to
the list ARGS... as it appears in the expression,
and the result should be a form to be evaluated instead of the original.
Richard M. Stallman's avatar
Richard M. Stallman committed
606 607

DECL is a declaration, optional, which can specify how to indent
John Paul Wallington's avatar
John Paul Wallington committed
608 609
calls to this macro, how Edebug should handle it, and which argument
should be treated as documentation.  It looks like this:
Richard M. Stallman's avatar
Richard M. Stallman committed
610 611 612 613 614
  (declare SPECS...)
The elements can look like this:
  (indent INDENT)
	Set NAME's `lisp-indent-function' property to INDENT.

615
  (debug DEBUG)
Richard M. Stallman's avatar
Richard M. Stallman committed
616
	Set NAME's `edebug-form-spec' property to DEBUG.  (This is
Juanma Barranquero's avatar
Juanma Barranquero committed
617
	equivalent to writing a `def-edebug-spec' for the macro.)
John Paul Wallington's avatar
John Paul Wallington committed
618 619 620 621

  (doc-string ELT)
	Set NAME's `doc-string-elt' property to ELT.

Richard M. Stallman's avatar
Richard M. Stallman committed
622
usage: (defmacro NAME ARGLIST [DOCSTRING] [DECL] BODY...)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
623
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
624 625 626
{
  register Lisp_Object fn_name;
  register Lisp_Object defn;
627
  Lisp_Object lambda_list, doc, tail;
Jim Blandy's avatar
Jim Blandy committed
628 629

  fn_name = Fcar (args);
630
  CHECK_SYMBOL (fn_name);
631 632 633 634 635 636
  lambda_list = Fcar (Fcdr (args));
  tail = Fcdr (Fcdr (args));

  doc = Qnil;
  if (STRINGP (Fcar (tail)))
    {
637 638
      doc = XCAR (tail);
      tail = XCDR (tail);
639 640
    }

641 642
  if (CONSP (Fcar (tail))
      && EQ (Fcar (Fcar (tail)), Qdeclare))
643 644 645 646 647 648 649 650
    {
      if (!NILP (Vmacro_declaration_function))
	{
	  struct gcpro gcpro1;
	  GCPRO1 (args);
	  call2 (Vmacro_declaration_function, fn_name, Fcar (tail));
	  UNGCPRO;
	}
651

652 653 654 655 656 657 658 659
      tail = Fcdr (tail);
    }

  if (NILP (doc))
    tail = Fcons (lambda_list, tail);
  else
    tail = Fcons (lambda_list, Fcons (doc, tail));
  defn = Fcons (Qmacro, Fcons (Qlambda, tail));
660

Jim Blandy's avatar
Jim Blandy committed
661
  if (!NILP (Vpurify_flag))
Jim Blandy's avatar
Jim Blandy committed
662
    defn = Fpurecopy (defn);
663 664 665
  if (CONSP (XSYMBOL (fn_name)->function)
      && EQ (XCAR (XSYMBOL (fn_name)->function), Qautoload))
    LOADHIST_ATTACH (Fcons (Qt, fn_name));
Jim Blandy's avatar
Jim Blandy committed
666
  Ffset (fn_name, defn);
667
  LOADHIST_ATTACH (Fcons (Qdefun, fn_name));
Jim Blandy's avatar
Jim Blandy committed
668 669 670
  return fn_name;
}

Gerd Moellmann's avatar
Gerd Moellmann committed
671

672
DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
673
       doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
Richard M. Stallman's avatar
Richard M. Stallman committed
674
Aliased variables always have the same value; setting one sets the other.
675
Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS.  If it is
676 677 678 679
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.
680
The return value is BASE-VARIABLE.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
681
  (Lisp_Object new_alias, Lisp_Object base_variable, Lisp_Object docstring)
Gerd Moellmann's avatar
Gerd Moellmann committed
682 683
{
  struct Lisp_Symbol *sym;
684

685 686
  CHECK_SYMBOL (new_alias);
  CHECK_SYMBOL (base_variable);
Gerd Moellmann's avatar
Gerd Moellmann committed
687

688
  sym = XSYMBOL (new_alias);
689 690

  if (sym->constant)
Stefan Monnier's avatar
Stefan Monnier committed
691 692
    /* Not sure why, but why not?  */
    error ("Cannot make a constant an alias");
693 694 695 696 697 698 699 700 701

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

702
  /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
703 704 705 706
     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)))
707
    set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1);
708 709 710 711 712 713 714 715 716 717 718 719 720

  {
    struct specbinding *p;

    for (p = specpdl_ptr - 1; p >= specpdl; p--)
      if (p->func == NULL
	  && (EQ (new_alias,
		  CONSP (p->symbol) ? XCAR (p->symbol) : p->symbol)))
	error ("Don't know how to make a let-bound variable an alias");
  }

  sym->redirect = SYMBOL_VARALIAS;
  SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
721 722
  sym->constant = SYMBOL_CONSTANT_P (base_variable);
  LOADHIST_ATTACH (new_alias);
723 724
  /* Even if docstring is nil: remove old docstring.  */
  Fput (new_alias, Qvariable_documentation, docstring);
725

726
  return base_variable;
Gerd Moellmann's avatar
Gerd Moellmann committed
727 728 729
}


Jim Blandy's avatar
Jim Blandy committed
730
DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
Juanma Barranquero's avatar
Juanma Barranquero committed
731
       doc: /* Define SYMBOL as a variable, and return SYMBOL.
Pavel Janík's avatar
Pavel Janík committed
732 733 734 735 736 737 738 739 740 741 742 743
You are not required to define a variable in order to use it,
but the definition can supply documentation and an initial value
in a way that tags can recognize.

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.
INITVALUE and DOCSTRING are optional.
If DOCSTRING starts with *, this variable is identified as a user option.
 This means that M-x set-variable recognizes it.
 See also `user-variable-p'.
If INITVALUE is missing, SYMBOL's value is not set.
744 745 746 747 748 749 750

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.)
751
usage: (defvar SYMBOL &optional INITVALUE DOCSTRING)  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
752
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
753
{
754
  register Lisp_Object sym, tem, tail;
Jim Blandy's avatar
Jim Blandy committed
755 756

  sym = Fcar (args);
757 758
  tail = Fcdr (args);
  if (!NILP (Fcdr (Fcdr (tail))))
759
    error ("Too many arguments");
760

761
  tem = Fdefault_boundp (sym);
762
  if (!NILP (tail))
Jim Blandy's avatar
Jim Blandy committed
763
    {
764 765 766 767 768 769 770 771 772 773 774 775
      if (SYMBOL_CONSTANT_P (sym))
	{
	  /* For upward compatibility, allow (defvar :foo (quote :foo)).  */
	  Lisp_Object tem = Fcar (tail);
	  if (! (CONSP (tem)
		 && EQ (XCAR (tem), Qquote)
		 && CONSP (XCDR (tem))
		 && EQ (XCAR (XCDR (tem)), sym)))
	    error ("Constant symbol `%s' specified in defvar",
		   SDATA (SYMBOL_NAME (sym)));
	}

Jim Blandy's avatar
Jim Blandy committed
776
      if (NILP (tem))
777
	Fset_default (sym, Feval (Fcar (tail)));
778 779 780
      else
	{ /* Check if there is really a global binding rather than just a let
	     binding that shadows the global unboundness of the var.  */
781
	  volatile struct specbinding *pdl = specpdl_ptr;
782 783 784 785 786 787 788 789 790 791 792
	  while (--pdl >= specpdl)
	    {
	      if (EQ (pdl->symbol, sym) && !pdl->func
		  && EQ (pdl->old_value, Qunbound))
		{
		  message_with_string ("Warning: defvar ignored because %s is let-bound",
				       SYMBOL_NAME (sym), 1);
		  break;
		}
	    }
	}
793
      tail = Fcdr (tail);
794 795
      tem = Fcar (tail);
      if (!NILP (tem))
796 797 798 799 800
	{
	  if (!NILP (Vpurify_flag))
	    tem = Fpurecopy (tem);
	  Fput (sym, Qvariable_documentation, tem);
	}
801
      LOADHIST_ATTACH (sym);
Jim Blandy's avatar
Jim Blandy committed
802
    }
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 815 816 817 818 819
       doc: /* Define SYMBOL as a constant variable.
The intent is that neither programs nor users should ever change this value.
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.
DOCSTRING is optional.
820 821 822 823

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.
824
usage: (defconst SYMBOL INITVALUE [DOCSTRING])  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
825
  (Lisp_Object args)
Jim Blandy's avatar
Jim Blandy committed
826 827 828 829
{
  register Lisp_Object sym, tem;

  sym = Fcar (args);
830
  if (!NILP (Fcdr (Fcdr (Fcdr (args)))))
831
    error ("Too many arguments");
832

833 834 835 836
  tem = Feval (Fcar (Fcdr (args)));
  if (!NILP (Vpurify_flag))
    tem = Fpurecopy (tem);
  Fset_default (sym, tem);
Jim Blandy's avatar
Jim Blandy committed
837
  tem = Fcar (Fcdr (Fcdr (args)));
Jim Blandy's avatar
Jim Blandy committed
838
  if (!NILP (tem))
Jim Blandy's avatar
Jim Blandy committed
839
    {
Jim Blandy's avatar
Jim Blandy committed
840
      if (!NILP (Vpurify_flag))
Jim Blandy's avatar
Jim Blandy committed
841 842 843
	tem = Fpurecopy (tem);
      Fput (sym, Qvariable_documentation, tem);
    }
844
  Fput (sym, Qrisky_local_variable, Qt);
845
  LOADHIST_ATTACH (sym);
Jim Blandy's avatar
Jim Blandy committed
846 847 848
  return sym;
}

849 850
/* Error handler used in Fuser_variable_p.  */
static Lisp_Object
851
user_variable_p_eh (Lisp_Object ignore)
852 853 854 855
{
  return Qnil;
}

856 857 858
static Lisp_Object
lisp_indirect_variable (Lisp_Object sym)
{
859 860
  struct Lisp_Symbol *s = indirect_variable (XSYMBOL (sym));
  XSETSYMBOL (sym, s);
861 862 863
  return sym;
}

Jim Blandy's avatar
Jim Blandy committed
864
DEFUN ("user-variable-p", Fuser_variable_p, Suser_variable_p, 1, 1, 0,
865
       doc: /* Return t if VARIABLE is intended to be set and modified by users.
Pavel Janík's avatar
Pavel Janík committed
866
\(The alternative is a variable used internally in a Lisp program.)
867 868 869 870 871 872 873
A variable is a user variable if
\(1) the first character of its documentation is `*', or
\(2) it is customizable (its property list contains a non-nil value
    of `standard-value' or `custom-autoload'), or
\(3) it is an alias for another user variable.
Return nil if VARIABLE is an alias and there is a loop in the
chain of symbols.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
874
  (Lisp_Object variable)
Jim Blandy's avatar
Jim Blandy committed
875 876
{
  Lisp_Object documentation;
877

878 879 880
  if (!SYMBOLP (variable))
      return Qnil;

881
  /* If indirect and there's an alias loop, don't check anything else.  */
882
  if (XSYMBOL (variable)->redirect == SYMBOL_VARALIAS
883
      && NILP (internal_condition_case_1 (lisp_indirect_variable, variable,
Andreas Schwab's avatar
Andreas Schwab committed
884
					  Qt, user_variable_p_eh)))
885 886 887 888 889 890
    return Qnil;

  while (1)
    {
      documentation = Fget (variable, Qvariable_documentation);
      if (INTEGERP (documentation) && XINT (documentation) < 0)
Andreas Schwab's avatar
Andreas Schwab committed
891
	return Qt;
892
      if (STRINGP (documentation)
Andreas Schwab's avatar
Andreas Schwab committed
893 894
	  && ((unsigned char) SREF (documentation, 0) == '*'))
	return Qt;
895 896
      /* If it is (STRING . INTEGER), a negative integer means a user variable.  */
      if (CONSP (documentation)
Andreas Schwab's avatar
Andreas Schwab committed
897 898 899 900
	  && STRINGP (XCAR (documentation))
	  && INTEGERP (XCDR (documentation))
	  && XINT (XCDR (documentation)) < 0)
	return Qt;