data.c 92.8 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Primitive operations on Lisp data types for GNU Emacs Lisp interpreter.
2
   Copyright (C) 1985-1986, 1988, 1993-1995, 1997-2011
Glenn Morris's avatar
Glenn Morris committed
3
                 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 <signal.h>
23
#include <stdio.h>
24
#include <setjmp.h>
Jim Blandy's avatar
Jim Blandy committed
25
#include "lisp.h"
Jim Blandy's avatar
Jim Blandy committed
26
#include "puresize.h"
27
#include "character.h"
Jim Blandy's avatar
Jim Blandy committed
28
#include "buffer.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
29
#include "keyboard.h"
30
#include "frame.h"
Jim Blandy's avatar
Jim Blandy committed
31
#include "syssignal.h"
32
#include "termhooks.h"  /* For FRAME_KBOARD reference in y-or-n-p. */
33
#include "font.h"
Jim Blandy's avatar
Jim Blandy committed
34

35
#ifdef STDC_HEADERS
36
#include <float.h>
37
#endif
38

39 40 41 42 43 44 45 46 47 48
/* If IEEE_FLOATING_POINT isn't defined, default it from FLT_*. */
#ifndef IEEE_FLOATING_POINT
#if (FLT_RADIX == 2 && FLT_MANT_DIG == 24 \
     && FLT_MIN_EXP == -125 && FLT_MAX_EXP == 128)
#define IEEE_FLOATING_POINT 1
#else
#define IEEE_FLOATING_POINT 0
#endif
#endif

Jim Blandy's avatar
Jim Blandy committed
49 50
#include <math.h>

51
#if !defined (atof)
52
extern double atof (const char *);
53 54
#endif /* !atof */

Jim Blandy's avatar
Jim Blandy committed
55 56 57
Lisp_Object Qnil, Qt, Qquote, Qlambda, Qsubr, Qunbound;
Lisp_Object Qerror_conditions, Qerror_message, Qtop_level;
Lisp_Object Qerror, Qquit, Qwrong_type_argument, Qargs_out_of_range;
Jim Blandy's avatar
Jim Blandy committed
58
Lisp_Object Qvoid_variable, Qvoid_function, Qcyclic_function_indirection;
59
Lisp_Object Qcyclic_variable_indirection, Qcircular_list;
Jim Blandy's avatar
Jim Blandy committed
60 61
Lisp_Object Qsetting_constant, Qinvalid_read_syntax;
Lisp_Object Qinvalid_function, Qwrong_number_of_arguments, Qno_catch;
Roland McGrath's avatar
Roland McGrath committed
62
Lisp_Object Qend_of_file, Qarith_error, Qmark_inactive;
Jim Blandy's avatar
Jim Blandy committed
63
Lisp_Object Qbeginning_of_buffer, Qend_of_buffer, Qbuffer_read_only;
64
Lisp_Object Qtext_read_only;
Kenichi Handa's avatar
Kenichi Handa committed
65

66
Lisp_Object Qintegerp, Qnatnump, Qwholenump, Qsymbolp, Qlistp, Qconsp;
Jim Blandy's avatar
Jim Blandy committed
67 68
Lisp_Object Qstringp, Qarrayp, Qsequencep, Qbufferp;
Lisp_Object Qchar_or_string_p, Qmarkerp, Qinteger_or_marker_p, Qvectorp;
Dave Love's avatar
Dave Love committed
69
Lisp_Object Qbuffer_or_string_p, Qkeywordp;
Jim Blandy's avatar
Jim Blandy committed
70
Lisp_Object Qboundp, Qfboundp;
71
Lisp_Object Qchar_table_p, Qvector_or_char_table_p;
72

Jim Blandy's avatar
Jim Blandy committed
73
Lisp_Object Qcdr;
74
Lisp_Object Qad_advice_info, Qad_activate_internal;
Jim Blandy's avatar
Jim Blandy committed
75

76 77 78
Lisp_Object Qrange_error, Qdomain_error, Qsingularity_error;
Lisp_Object Qoverflow_error, Qunderflow_error;

79
Lisp_Object Qfloatp;
Jim Blandy's avatar
Jim Blandy committed
80 81
Lisp_Object Qnumberp, Qnumber_or_marker_p;

Kenichi Handa's avatar
Kenichi Handa committed
82 83
Lisp_Object Qinteger;
static Lisp_Object Qsymbol, Qstring, Qcons, Qmarker, Qoverlay;
84 85
Lisp_Object Qwindow;
static Lisp_Object Qfloat, Qwindow_configuration;
Karl Heuer's avatar
Karl Heuer committed
86
Lisp_Object Qprocess;
Stefan Monnier's avatar
Stefan Monnier committed
87
static Lisp_Object Qcompiled_function, Qbuffer, Qframe, Qvector;
Gerd Moellmann's avatar
Gerd Moellmann committed
88
static Lisp_Object Qchar_table, Qbool_vector, Qhash_table;
89
static Lisp_Object Qsubrp, Qmany, Qunevalled;
90
Lisp_Object Qfont_spec, Qfont_entity, Qfont_object;
91

92 93
Lisp_Object Qinteractive_form;

94
static void swap_in_symval_forwarding (struct Lisp_Symbol *, struct Lisp_Buffer_Local_Value *);
95

96 97

void
98
circular_list_error (Lisp_Object list)
99
{
100
  xsignal (Qcircular_list, list);
101 102 103
}


Jim Blandy's avatar
Jim Blandy committed
104
Lisp_Object
105
wrong_type_argument (register Lisp_Object predicate, register Lisp_Object value)
Jim Blandy's avatar
Jim Blandy committed
106
{
107 108 109 110 111 112
  /* If VALUE is not even a valid Lisp object, we'd want to abort here
     where we can get a backtrace showing where it came from.  We used
     to try and do that by checking the tagbits, but nowadays all
     tagbits are potentially valid.  */
  /* if ((unsigned int) XTYPE (value) >= Lisp_Type_Limit)
   *   abort (); */
113

114
  xsignal2 (Qwrong_type_argument, predicate, value);
Jim Blandy's avatar
Jim Blandy committed
115 116
}

Andreas Schwab's avatar
Andreas Schwab committed
117
void
118
pure_write_error (void)
Jim Blandy's avatar
Jim Blandy committed
119 120 121 122 123
{
  error ("Attempt to modify read-only object");
}

void
124
args_out_of_range (Lisp_Object a1, Lisp_Object a2)
Jim Blandy's avatar
Jim Blandy committed
125
{
126
  xsignal2 (Qargs_out_of_range, a1, a2);
Jim Blandy's avatar
Jim Blandy committed
127 128 129
}

void
130
args_out_of_range_3 (Lisp_Object a1, Lisp_Object a2, Lisp_Object a3)
Jim Blandy's avatar
Jim Blandy committed
131
{
132
  xsignal3 (Qargs_out_of_range, a1, a2, a3);
Jim Blandy's avatar
Jim Blandy committed
133 134 135 136 137 138
}


/* Data type predicates */

DEFUN ("eq", Feq, Seq, 2, 2, 0,
139
       doc: /* Return t if the two args are the same Lisp object.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
140
  (Lisp_Object obj1, Lisp_Object obj2)
Jim Blandy's avatar
Jim Blandy committed
141 142 143 144 145 146
{
  if (EQ (obj1, obj2))
    return Qt;
  return Qnil;
}

147 148
DEFUN ("null", Fnull, Snull, 1, 1, 0,
       doc: /* Return t if OBJECT is nil.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
149
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
150
{
151
  if (NILP (object))
Jim Blandy's avatar
Jim Blandy committed
152 153 154 155
    return Qt;
  return Qnil;
}

156
DEFUN ("type-of", Ftype_of, Stype_of, 1, 1, 0,
157 158 159
       doc: /* Return a symbol representing the type of OBJECT.
The symbol returned names the object's basic type;
for example, (type-of 1) returns `integer'.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
160
  (Lisp_Object object)
161
{
162
  switch (XTYPE (object))
163
    {
164
    case_Lisp_Int:
165 166 167 168 169 170 171 172 173 174 175 176
      return Qinteger;

    case Lisp_Symbol:
      return Qsymbol;

    case Lisp_String:
      return Qstring;

    case Lisp_Cons:
      return Qcons;

    case Lisp_Misc:
177
      switch (XMISCTYPE (object))
178 179 180 181 182 183 184 185 186 187 188
	{
	case Lisp_Misc_Marker:
	  return Qmarker;
	case Lisp_Misc_Overlay:
	  return Qoverlay;
	case Lisp_Misc_Float:
	  return Qfloat;
	}
      abort ();

    case Lisp_Vectorlike:
189
      if (WINDOW_CONFIGURATIONP (object))
190
	return Qwindow_configuration;
191
      if (PROCESSP (object))
192
	return Qprocess;
193
      if (WINDOWP (object))
194
	return Qwindow;
195
      if (SUBRP (object))
196
	return Qsubr;
Stefan Monnier's avatar
Stefan Monnier committed
197 198
      if (COMPILEDP (object))
	return Qcompiled_function;
199
      if (BUFFERP (object))
200
	return Qbuffer;
201
      if (CHAR_TABLE_P (object))
202
	return Qchar_table;
203
      if (BOOL_VECTOR_P (object))
204
	return Qbool_vector;
205
      if (FRAMEP (object))
206
	return Qframe;
207
      if (HASH_TABLE_P (object))
Gerd Moellmann's avatar
Gerd Moellmann committed
208
	return Qhash_table;
209 210 211 212 213 214
      if (FONT_SPEC_P (object))
	return Qfont_spec;
      if (FONT_ENTITY_P (object))
	return Qfont_entity;
      if (FONT_OBJECT_P (object))
	return Qfont_object;
215 216 217 218 219 220 221 222 223 224
      return Qvector;

    case Lisp_Float:
      return Qfloat;

    default:
      abort ();
    }
}

225 226
DEFUN ("consp", Fconsp, Sconsp, 1, 1, 0,
       doc: /* Return t if OBJECT is a cons cell.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
227
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
228
{
229
  if (CONSP (object))
Jim Blandy's avatar
Jim Blandy committed
230 231 232 233
    return Qt;
  return Qnil;
}

234
DEFUN ("atom", Fatom, Satom, 1, 1, 0,
235
       doc: /* Return t if OBJECT is not a cons cell.  This includes nil.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
236
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
237
{
238
  if (CONSP (object))
Jim Blandy's avatar
Jim Blandy committed
239 240 241 242
    return Qnil;
  return Qt;
}

243
DEFUN ("listp", Flistp, Slistp, 1, 1, 0,
Luc Teirlinck's avatar
Luc Teirlinck committed
244 245
       doc: /* Return t if OBJECT is a list, that is, a cons cell or nil.
Otherwise, return nil.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
246
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
247
{
248
  if (CONSP (object) || NILP (object))
Jim Blandy's avatar
Jim Blandy committed
249 250 251 252
    return Qt;
  return Qnil;
}

253
DEFUN ("nlistp", Fnlistp, Snlistp, 1, 1, 0,
254
       doc: /* Return t if OBJECT is not a list.  Lists include nil.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
255
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
256
{
257
  if (CONSP (object) || NILP (object))
Jim Blandy's avatar
Jim Blandy committed
258 259 260 261
    return Qnil;
  return Qt;
}

262
DEFUN ("symbolp", Fsymbolp, Ssymbolp, 1, 1, 0,
263
       doc: /* Return t if OBJECT is a symbol.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
264
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
265
{
266
  if (SYMBOLP (object))
Jim Blandy's avatar
Jim Blandy committed
267 268 269 270
    return Qt;
  return Qnil;
}

Dave Love's avatar
Dave Love committed
271 272 273
/* Define this in C to avoid unnecessarily consing up the symbol
   name.  */
DEFUN ("keywordp", Fkeywordp, Skeywordp, 1, 1, 0,
274 275 276
       doc: /* Return t if OBJECT is a keyword.
This means that it is a symbol with a print name beginning with `:'
interned in the initial obarray.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
277
  (Lisp_Object object)
Dave Love's avatar
Dave Love committed
278 279
{
  if (SYMBOLP (object)
280
      && SREF (SYMBOL_NAME (object), 0) == ':'
281
      && SYMBOL_INTERNED_IN_INITIAL_OBARRAY_P (object))
Dave Love's avatar
Dave Love committed
282 283 284 285
    return Qt;
  return Qnil;
}

286
DEFUN ("vectorp", Fvectorp, Svectorp, 1, 1, 0,
287
       doc: /* Return t if OBJECT is a vector.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
288
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
289
{
290
  if (VECTORP (object))
Jim Blandy's avatar
Jim Blandy committed
291 292 293 294
    return Qt;
  return Qnil;
}

295
DEFUN ("stringp", Fstringp, Sstringp, 1, 1, 0,
296
       doc: /* Return t if OBJECT is a string.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
297
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
298
{
299
  if (STRINGP (object))
Jim Blandy's avatar
Jim Blandy committed
300 301 302 303
    return Qt;
  return Qnil;
}

304
DEFUN ("multibyte-string-p", Fmultibyte_string_p, Smultibyte_string_p,
305 306
       1, 1, 0,
       doc: /* Return t if OBJECT is a multibyte string.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
307
  (Lisp_Object object)
308 309 310 311 312 313 314
{
  if (STRINGP (object) && STRING_MULTIBYTE (object))
    return Qt;
  return Qnil;
}

DEFUN ("char-table-p", Fchar_table_p, Schar_table_p, 1, 1, 0,
315
       doc: /* Return t if OBJECT is a char-table.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
316
  (Lisp_Object object)
317 318 319 320 321 322
{
  if (CHAR_TABLE_P (object))
    return Qt;
  return Qnil;
}

323 324
DEFUN ("vector-or-char-table-p", Fvector_or_char_table_p,
       Svector_or_char_table_p, 1, 1, 0,
325
       doc: /* Return t if OBJECT is a char-table or vector.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
326
  (Lisp_Object object)
327 328 329 330 331 332
{
  if (VECTORP (object) || CHAR_TABLE_P (object))
    return Qt;
  return Qnil;
}

333 334
DEFUN ("bool-vector-p", Fbool_vector_p, Sbool_vector_p, 1, 1, 0,
       doc: /* Return t if OBJECT is a bool-vector.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
335
  (Lisp_Object object)
336 337 338 339 340 341
{
  if (BOOL_VECTOR_P (object))
    return Qt;
  return Qnil;
}

342 343
DEFUN ("arrayp", Farrayp, Sarrayp, 1, 1, 0,
       doc: /* Return t if OBJECT is an array (string or vector).  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
344
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
345
{
346
  if (ARRAYP (object))
Jim Blandy's avatar
Jim Blandy committed
347 348 349 350 351
    return Qt;
  return Qnil;
}

DEFUN ("sequencep", Fsequencep, Ssequencep, 1, 1, 0,
352
       doc: /* Return t if OBJECT is a sequence (list or array).  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
353
  (register Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
354
{
355
  if (CONSP (object) || NILP (object) || ARRAYP (object))
Jim Blandy's avatar
Jim Blandy committed
356 357 358 359
    return Qt;
  return Qnil;
}

360 361
DEFUN ("bufferp", Fbufferp, Sbufferp, 1, 1, 0,
       doc: /* Return t if OBJECT is an editor buffer.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
362
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
363
{
364
  if (BUFFERP (object))
Jim Blandy's avatar
Jim Blandy committed
365 366 367 368
    return Qt;
  return Qnil;
}

369 370
DEFUN ("markerp", Fmarkerp, Smarkerp, 1, 1, 0,
       doc: /* Return t if OBJECT is a marker (editor pointer).  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
371
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
372
{
373
  if (MARKERP (object))
Jim Blandy's avatar
Jim Blandy committed
374 375 376 377
    return Qt;
  return Qnil;
}

378 379
DEFUN ("subrp", Fsubrp, Ssubrp, 1, 1, 0,
       doc: /* Return t if OBJECT is a built-in function.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
380
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
381
{
382
  if (SUBRP (object))
Jim Blandy's avatar
Jim Blandy committed
383 384 385 386
    return Qt;
  return Qnil;
}

Jim Blandy's avatar
Jim Blandy committed
387
DEFUN ("byte-code-function-p", Fbyte_code_function_p, Sbyte_code_function_p,
388 389
       1, 1, 0,
       doc: /* Return t if OBJECT is a byte-compiled function object.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
390
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
391
{
392
  if (COMPILEDP (object))
Jim Blandy's avatar
Jim Blandy committed
393 394 395 396
    return Qt;
  return Qnil;
}

397
DEFUN ("char-or-string-p", Fchar_or_string_p, Schar_or_string_p, 1, 1, 0,
398
       doc: /* Return t if OBJECT is a character or a string.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
399
  (register Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
400
{
401
  if (CHARACTERP (object) || STRINGP (object))
Jim Blandy's avatar
Jim Blandy committed
402 403 404 405
    return Qt;
  return Qnil;
}

406 407
DEFUN ("integerp", Fintegerp, Sintegerp, 1, 1, 0,
       doc: /* Return t if OBJECT is an integer.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
408
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
409
{
410
  if (INTEGERP (object))
Jim Blandy's avatar
Jim Blandy committed
411 412 413 414
    return Qt;
  return Qnil;
}

415
DEFUN ("integer-or-marker-p", Finteger_or_marker_p, Sinteger_or_marker_p, 1, 1, 0,
416
       doc: /* Return t if OBJECT is an integer or a marker (editor pointer).  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
417
  (register Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
418
{
419
  if (MARKERP (object) || INTEGERP (object))
Jim Blandy's avatar
Jim Blandy committed
420 421 422 423
    return Qt;
  return Qnil;
}

424
DEFUN ("natnump", Fnatnump, Snatnump, 1, 1, 0,
425
       doc: /* Return t if OBJECT is a nonnegative integer.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
426
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
427
{
428
  if (NATNUMP (object))
Jim Blandy's avatar
Jim Blandy committed
429 430 431 432 433
    return Qt;
  return Qnil;
}

DEFUN ("numberp", Fnumberp, Snumberp, 1, 1, 0,
434
       doc: /* Return t if OBJECT is a number (floating point or integer).  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
435
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
436
{
437
  if (NUMBERP (object))
Jim Blandy's avatar
Jim Blandy committed
438
    return Qt;
Jim Blandy's avatar
Jim Blandy committed
439 440
  else
    return Qnil;
Jim Blandy's avatar
Jim Blandy committed
441 442 443 444
}

DEFUN ("number-or-marker-p", Fnumber_or_marker_p,
       Snumber_or_marker_p, 1, 1, 0,
445
       doc: /* Return t if OBJECT is a number or a marker.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
446
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
447
{
448
  if (NUMBERP (object) || MARKERP (object))
Jim Blandy's avatar
Jim Blandy committed
449 450 451
    return Qt;
  return Qnil;
}
452 453

DEFUN ("floatp", Ffloatp, Sfloatp, 1, 1, 0,
454
       doc: /* Return t if OBJECT is a floating point number.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
455
  (Lisp_Object object)
456
{
457
  if (FLOATP (object))
458 459 460
    return Qt;
  return Qnil;
}
461

Jim Blandy's avatar
Jim Blandy committed
462 463 464

/* Extract and set components of lists */

465
DEFUE ("car", Fcar, Scar, 1, 1, 0,
466
       doc: /* Return the car of LIST.  If arg is nil, return nil.
467 468
Error if arg is not nil and not a cons cell.  See also `car-safe'.

Luc Teirlinck's avatar
Luc Teirlinck committed
469 470
See Info node `(elisp)Cons Cells' for a discussion of related basic
Lisp concepts such as car, cdr, cons cell and list.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
471
  (register Lisp_Object list)
Jim Blandy's avatar
Jim Blandy committed
472
{
473
  return CAR (list);
Jim Blandy's avatar
Jim Blandy committed
474 475
}

476
DEFUE ("car-safe", Fcar_safe, Scar_safe, 1, 1, 0,
477
       doc: /* Return the car of OBJECT if it is a cons cell, or else nil.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
478
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
479
{
480
  return CAR_SAFE (object);
Jim Blandy's avatar
Jim Blandy committed
481 482
}

483
DEFUE ("cdr", Fcdr, Scdr, 1, 1, 0,
484
       doc: /* Return the cdr of LIST.  If arg is nil, return nil.
485 486
Error if arg is not nil and not a cons cell.  See also `cdr-safe'.

Luc Teirlinck's avatar
Luc Teirlinck committed
487 488
See Info node `(elisp)Cons Cells' for a discussion of related basic
Lisp concepts such as cdr, car, cons cell and list.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
489
  (register Lisp_Object list)
Jim Blandy's avatar
Jim Blandy committed
490
{
491
  return CDR (list);
Jim Blandy's avatar
Jim Blandy committed
492 493
}

494
DEFUE ("cdr-safe", Fcdr_safe, Scdr_safe, 1, 1, 0,
495
       doc: /* Return the cdr of OBJECT if it is a cons cell, or else nil.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
496
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
497
{
498
  return CDR_SAFE (object);
Jim Blandy's avatar
Jim Blandy committed
499 500
}

501
DEFUE ("setcar", Fsetcar, Ssetcar, 2, 2, 0,
502
       doc: /* Set the car of CELL to be NEWCAR.  Returns NEWCAR.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
503
  (register Lisp_Object cell, Lisp_Object newcar)
Jim Blandy's avatar
Jim Blandy committed
504
{
505
  CHECK_CONS (cell);
Jim Blandy's avatar
Jim Blandy committed
506
  CHECK_IMPURE (cell);
507
  XSETCAR (cell, newcar);
Jim Blandy's avatar
Jim Blandy committed
508 509 510
  return newcar;
}

511
DEFUE ("setcdr", Fsetcdr, Ssetcdr, 2, 2, 0,
512
       doc: /* Set the cdr of CELL to be NEWCDR.  Returns NEWCDR.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
513
  (register Lisp_Object cell, Lisp_Object newcdr)
Jim Blandy's avatar
Jim Blandy committed
514
{
515
  CHECK_CONS (cell);
Jim Blandy's avatar
Jim Blandy committed
516
  CHECK_IMPURE (cell);
517
  XSETCDR (cell, newcdr);
Jim Blandy's avatar
Jim Blandy committed
518 519 520 521 522
  return newcdr;
}

/* Extract and set components of symbols */

523
DEFUE ("boundp", Fboundp, Sboundp, 1, 1, 0,
524
       doc: /* Return t if SYMBOL's value is not void.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
525
  (register Lisp_Object symbol)
Jim Blandy's avatar
Jim Blandy committed
526 527
{
  Lisp_Object valcontents;
528
  struct Lisp_Symbol *sym;
529
  CHECK_SYMBOL (symbol);
530
  sym = XSYMBOL (symbol);
Jim Blandy's avatar
Jim Blandy committed
531

532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
 start:
  switch (sym->redirect)
    {
    case SYMBOL_PLAINVAL: valcontents = SYMBOL_VAL (sym); break;
    case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
    case SYMBOL_LOCALIZED:
      {
	struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
	if (blv->fwd)
	  /* In set_internal, we un-forward vars when their value is
    	     set to Qunbound. */
    	  return Qt;
	else
	  {
	    swap_in_symval_forwarding (sym, blv);
	    valcontents = BLV_VALUE (blv);
	  }
	break;
      }
    case SYMBOL_FORWARDED:
      /* In set_internal, we un-forward vars when their value is
	 set to Qunbound. */
      return Qt;
    default: abort ();
    }
Jim Blandy's avatar
Jim Blandy committed
557

558
  return (EQ (valcontents, Qunbound) ? Qnil : Qt);
Jim Blandy's avatar
Jim Blandy committed
559 560
}

561
DEFUE ("fboundp", Ffboundp, Sfboundp, 1, 1, 0,
562
       doc: /* Return t if SYMBOL's function definition is not void.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
563
  (register Lisp_Object symbol)
Jim Blandy's avatar
Jim Blandy committed
564
{
565
  CHECK_SYMBOL (symbol);
566
  return (EQ (XSYMBOL (symbol)->function, Qunbound) ? Qnil : Qt);
Jim Blandy's avatar
Jim Blandy committed
567 568
}

569
DEFUN ("makunbound", Fmakunbound, Smakunbound, 1, 1, 0,
570 571
       doc: /* Make SYMBOL's value be void.
Return SYMBOL.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
572
  (register Lisp_Object symbol)
Jim Blandy's avatar
Jim Blandy committed
573
{
574
  CHECK_SYMBOL (symbol);
575
  if (SYMBOL_CONSTANT_P (symbol))
576
    xsignal1 (Qsetting_constant, symbol);
577 578
  Fset (symbol, Qunbound);
  return symbol;
Jim Blandy's avatar
Jim Blandy committed
579 580
}

581
DEFUN ("fmakunbound", Ffmakunbound, Sfmakunbound, 1, 1, 0,
582 583
       doc: /* Make SYMBOL's function definition be void.
Return SYMBOL.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
584
  (register Lisp_Object symbol)
Jim Blandy's avatar
Jim Blandy committed
585
{
586
  CHECK_SYMBOL (symbol);
587
  if (NILP (symbol) || EQ (symbol, Qt))
588
    xsignal1 (Qsetting_constant, symbol);
589 590
  XSYMBOL (symbol)->function = Qunbound;
  return symbol;
Jim Blandy's avatar
Jim Blandy committed
591 592
}

593
DEFUE ("symbol-function", Fsymbol_function, Ssymbol_function, 1, 1, 0,
594
       doc: /* Return SYMBOL's function definition.  Error if that is void.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
595
  (register Lisp_Object symbol)
Jim Blandy's avatar
Jim Blandy committed
596
{
597
  CHECK_SYMBOL (symbol);
598 599 600
  if (!EQ (XSYMBOL (symbol)->function, Qunbound))
    return XSYMBOL (symbol)->function;
  xsignal1 (Qvoid_function, symbol);
Jim Blandy's avatar
Jim Blandy committed
601 602
}

603 604
DEFUN ("symbol-plist", Fsymbol_plist, Ssymbol_plist, 1, 1, 0,
       doc: /* Return SYMBOL's property list.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
605
  (register Lisp_Object symbol)
Jim Blandy's avatar
Jim Blandy committed
606
{
607
  CHECK_SYMBOL (symbol);
608
  return XSYMBOL (symbol)->plist;
Jim Blandy's avatar
Jim Blandy committed
609 610
}

611
DEFUE ("symbol-name", Fsymbol_name, Ssymbol_name, 1, 1, 0,
612
       doc: /* Return SYMBOL's name, a string.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
613
  (register Lisp_Object symbol)
Jim Blandy's avatar
Jim Blandy committed
614 615 616
{
  register Lisp_Object name;

617
  CHECK_SYMBOL (symbol);
618
  name = SYMBOL_NAME (symbol);
Jim Blandy's avatar
Jim Blandy committed
619 620 621
  return name;
}

622
DEFUE ("fset", Ffset, Sfset, 2, 2, 0,
623
       doc: /* Set SYMBOL's function definition to DEFINITION, and return DEFINITION.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
624
  (register Lisp_Object symbol, Lisp_Object definition)
625
{
626 627
  register Lisp_Object function;

628
  CHECK_SYMBOL (symbol);
629
  if (NILP (symbol) || EQ (symbol, Qt))
630
    xsignal1 (Qsetting_constant, symbol);
631 632 633 634 635 636 637 638 639

  function = XSYMBOL (symbol)->function;

  if (!NILP (Vautoload_queue) && !EQ (function, Qunbound))
    Vautoload_queue = Fcons (Fcons (symbol, function), Vautoload_queue);

  if (CONSP (function) && EQ (XCAR (function), Qautoload))
    Fput (symbol, Qautoload, XCDR (function));

640
  XSYMBOL (symbol)->function = definition;
641
  /* Handle automatic advice activation */
642
  if (CONSP (XSYMBOL (symbol)->plist) && !NILP (Fget (symbol, Qad_advice_info)))
643
    {
644
      call2 (Qad_activate_internal, symbol, Qnil);
645
      definition = XSYMBOL (symbol)->function;
646
    }
647
  return definition;
Jim Blandy's avatar
Jim Blandy committed
648 649
}

650
DEFUN ("defalias", Fdefalias, Sdefalias, 2, 3, 0,
651
       doc: /* Set SYMBOL's function definition to DEFINITION, and return DEFINITION.
Richard M. Stallman's avatar
Richard M. Stallman committed
652 653 654 655
Associates the function with the current load file, if any.
The optional third argument DOCSTRING specifies the documentation string
for SYMBOL; if it is omitted or nil, SYMBOL uses the documentation string
determined by DEFINITION.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
656
  (register Lisp_Object symbol, Lisp_Object definition, Lisp_Object docstring)
657
{
658
  CHECK_SYMBOL (symbol);
659 660 661
  if (CONSP (XSYMBOL (symbol)->function)
      && EQ (XCAR (XSYMBOL (symbol)->function), Qautoload))
    LOADHIST_ATTACH (Fcons (Qt, symbol));
662
  definition = Ffset (symbol, definition);
663
  LOADHIST_ATTACH (Fcons (Qdefun, symbol));
664 665
  if (!NILP (docstring))
    Fput (symbol, Qfunction_documentation, docstring);
666
  return definition;
667 668
}

Jim Blandy's avatar
Jim Blandy committed
669
DEFUN ("setplist", Fsetplist, Ssetplist, 2, 2, 0,
Kenichi Handa's avatar
Kenichi Handa committed
670
       doc: /* Set SYMBOL's property list to NEWPLIST, and return NEWPLIST.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
671
  (register Lisp_Object symbol, Lisp_Object newplist)
Jim Blandy's avatar
Jim Blandy committed
672
{
673
  CHECK_SYMBOL (symbol);
674
  XSYMBOL (symbol)->plist = newplist;
Jim Blandy's avatar
Jim Blandy committed
675 676
  return newplist;
}
Jim Blandy's avatar
Jim Blandy committed
677

678
DEFUN ("subr-arity", Fsubr_arity, Ssubr_arity, 1, 1, 0,
679 680 681 682 683
       doc: /* Return minimum and maximum number of args allowed for SUBR.
SUBR must be a built-in function.
The returned value is a pair (MIN . MAX).  MIN is the minimum number
of args.  MAX is the maximum number or the symbol `many', for a
function with `&rest' args, or `unevalled' for a special form.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
684
  (Lisp_Object subr)
685 686
{
  short minargs, maxargs;
687
  CHECK_SUBR (subr);
688 689 690 691 692 693 694 695 696 697
  minargs = XSUBR (subr)->min_args;
  maxargs = XSUBR (subr)->max_args;
  if (maxargs == MANY)
    return Fcons (make_number (minargs), Qmany);
  else if (maxargs == UNEVALLED)
    return Fcons (make_number (minargs), Qunevalled);
  else
    return Fcons (make_number (minargs), make_number (maxargs));
}

Stefan Monnier's avatar
Stefan Monnier committed
698 699 700
DEFUN ("subr-name", Fsubr_name, Ssubr_name, 1, 1, 0,
       doc: /* Return name of subroutine SUBR.
SUBR must be a built-in function.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
701
  (Lisp_Object subr)
Stefan Monnier's avatar
Stefan Monnier committed
702 703
{
  const char *name;
704
  CHECK_SUBR (subr);
Stefan Monnier's avatar
Stefan Monnier committed
705 706 707 708
  name = XSUBR (subr)->symbol_name;
  return make_string (name, strlen (name));
}

709
DEFUE ("interactive-form", Finteractive_form, Sinteractive_form, 1, 1, 0,
Kenichi Handa's avatar
Kenichi Handa committed
710
       doc: /* Return the interactive form of CMD or nil if none.
Luc Teirlinck's avatar
Luc Teirlinck committed
711 712
If CMD is not a command, the return value is nil.
Value, if non-nil, is a list \(interactive SPEC).  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
713
  (Lisp_Object cmd)
714
{
715
  Lisp_Object fun = indirect_function (cmd); /* Check cycles.  */
716

717 718 719 720 721 722 723 724
  if (NILP (fun) || EQ (fun, Qunbound))
    return Qnil;

  /* Use an `interactive-form' property if present, analogous to the
     function-documentation property. */
  fun = cmd;
  while (SYMBOLP (fun))
    {
725
      Lisp_Object tmp = Fget (fun, Qinteractive_form);
726 727 728 729 730
      if (!NILP (tmp))
	return tmp;
      else
	fun = Fsymbol_function (fun);
    }
Kenichi Handa's avatar
Kenichi Handa committed
731 732 733

  if (SUBRP (fun))
    {
734
      const char *spec = XSUBR (fun)->intspec;
735 736 737 738
      if (spec)
	return list2 (Qinteractive,
		      (*spec != '(') ? build_string (spec) :
		      Fcar (Fread_from_string (build_string (spec), Qnil, Qnil)));
Kenichi Handa's avatar
Kenichi Handa committed
739 740 741 742 743 744 745 746 747
    }
  else if (COMPILEDP (fun))
    {
      if ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE)
	return list2 (Qinteractive, AREF (fun, COMPILED_INTERACTIVE));
    }
  else if (CONSP (fun))
    {
      Lisp_Object funcar = XCAR (fun);
748
      if (EQ (funcar, Qclosure))
749 750
	return Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun))));
      else if (EQ (funcar, Qlambda))
Kenichi Handa's avatar
Kenichi Handa committed
751 752 753 754 755 756 757 758 759 760
	return Fassq (Qinteractive, Fcdr (XCDR (fun)));
      else if (EQ (funcar, Qautoload))
	{
	  struct gcpro gcpro1;
	  GCPRO1 (cmd);
	  do_autoload (fun, cmd);
	  UNGCPRO;
	  return Finteractive_form (cmd);
	}
    }
761 762 763
  return Qnil;
}

Jim Blandy's avatar
Jim Blandy committed
764

765 766 767 768 769 770 771 772
/***********************************************************************
		Getting and Setting Values of Symbols
 ***********************************************************************/

/* Return the symbol holding SYMBOL's value.  Signal
   `cyclic-variable-indirection' if SYMBOL's chain of variable
   indirections contains a loop.  */

Stefan Monnier's avatar
Stefan Monnier committed
773
struct Lisp_Symbol *
774
indirect_variable (struct Lisp_Symbol *symbol)
775
{
Stefan Monnier's avatar
Stefan Monnier committed
776
  struct Lisp_Symbol *tortoise, *hare;
777 778 779

  hare = tortoise = symbol;

780
  while (hare->redirect == SYMBOL_VARALIAS)
781
    {
782 783
      hare = SYMBOL_ALIAS (hare);
      if (hare->redirect != SYMBOL_VARALIAS)
784
	break;
785

786 787
      hare = SYMBOL_ALIAS (hare);
      tortoise = SYMBOL_ALIAS (tortoise);
788

Stefan Monnier's avatar
Stefan Monnier committed
789 790 791 792 793 794
      if (hare == tortoise)
	{
	  Lisp_Object tem;
	  XSETSYMBOL (tem, symbol);
	  xsignal1 (Qcyclic_variable_indirection, tem);
	}
795 796 797 798 799 800 801
    }

  return hare;
}


DEFUN ("indirect-variable", Findirect_variable, Sindirect_variable, 1, 1, 0,
802 803 804 805 806
       doc: /* Return the variable at the end of OBJECT's variable chain.
If OBJECT is a symbol, follow all variable indirections and return the final
variable.  If OBJECT is not a symbol, just return it.
Signal a cyclic-variable-indirection error if there is a loop in the
variable chain of symbols.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
807
  (Lisp_Object object)
808 809
{
  if (SYMBOLP (object))
810 811 812 813
    {
      struct Lisp_Symbol *sym = indirect_variable (XSYMBOL (object));
      XSETSYMBOL (object, sym);
    }
814 815 816
  return object;
}

Jim Blandy's avatar
Jim Blandy committed
817 818 819 820 821 822 823

/* Given the raw contents of a symbol value cell,
   return the Lisp value of the symbol.
   This does not handle buffer-local variables; use
   swap_in_symval_forwarding for that.  */

Lisp_Object
824
do_symval_forwarding (register union Lisp_Fwd *valcontents)
Jim Blandy's avatar
Jim Blandy committed
825 826
{
  register Lisp_Object val;
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
  switch (XFWDTYPE (valcontents))
    {
    case Lisp_Fwd_Int:
      XSETINT (val, *XINTFWD (valcontents)->intvar);
      return val;

    case Lisp_Fwd_Bool:
      return (*XBOOLFWD (valcontents)->boolvar ? Qt : Qnil);

    case Lisp_Fwd_Obj:
      return *XOBJFWD (valcontents)->objvar;

    case Lisp_Fwd_Buffer_Obj:
      return PER_BUFFER_VALUE (current_buffer,
			       XBUFFER_OBJFWD (valcontents)->offset);

    case Lisp_Fwd_Kboard_Obj:
      /* We used to simply use current_kboard here, but from Lisp
	 code, it's value is often unexpected.  It seems nicer to
	 allow constructions like this to work as intuitively expected:

	 (with-selected-frame frame
	 (define-key local-function-map "\eOP" [f1]))

	 On the other hand, this affects the semantics of
	 last-command and real-last-command, and people may rely on
	 that.  I took a quick look at the Lisp codebase, and I
	 don't think anything will break.  --lorentey  */
      return *(Lisp_Object *)(XKBOARD_OBJFWD (valcontents)->offset
			      + (char *)FRAME_KBOARD (SELECTED_FRAME ()));
    default: abort ();
    }
Jim Blandy's avatar
Jim Blandy committed
859 860
}

861 862
/* Store NEWVAL into SYMBOL, where VALCONTENTS is found in the value cell
   of SYMBOL.  If SYMBOL is buffer-local, VALCONTENTS should be the
Jim Blandy's avatar
Jim Blandy committed
863
   buffer-independent contents of the value cell: forwarded just one
864 865 866 867
   step past the buffer-localness.

   BUF non-zero means set the value in buffer BUF instead of the
   current buffer.  This only plays a role for per-buffer variables.  */
Jim Blandy's avatar
Jim Blandy committed
868

Stefan Monnier's avatar