data.c 113 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Primitive operations on Lisp data types for GNU Emacs Lisp interpreter.
Paul Eggert's avatar
Paul Eggert committed
2
   Copyright (C) 1985-1986, 1988, 1993-1995, 1997-2020 Free Software
3
   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 <https://www.gnu.org/licenses/>.  */
Jim Blandy's avatar
Jim Blandy committed
19 20


21
#include <config.h>
22 23

#include <math.h>
24
#include <stdio.h>
25

26
#include <byteswap.h>
27 28
#include <count-one-bits.h>
#include <count-trailing-zeros.h>
29 30
#include <intprops.h>

Jim Blandy's avatar
Jim Blandy committed
31
#include "lisp.h"
Paul Eggert's avatar
Paul Eggert committed
32
#include "bignum.h"
Jim Blandy's avatar
Jim Blandy committed
33
#include "puresize.h"
34
#include "character.h"
Jim Blandy's avatar
Jim Blandy committed
35
#include "buffer.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
36
#include "keyboard.h"
37
#include "process.h"
38
#include "frame.h"
39
#include "keymap.h"
Jim Blandy's avatar
Jim Blandy committed
40

41 42
static void swap_in_symval_forwarding (struct Lisp_Symbol *,
				       struct Lisp_Buffer_Local_Value *);
43

44
static bool
45
BOOLFWDP (lispfwd a)
46 47 48 49
{
  return XFWDTYPE (a) == Lisp_Fwd_Bool;
}
static bool
50
INTFWDP (lispfwd a)
51 52 53 54
{
  return XFWDTYPE (a) == Lisp_Fwd_Int;
}
static bool
55
KBOARD_OBJFWDP (lispfwd a)
56 57 58 59
{
  return XFWDTYPE (a) == Lisp_Fwd_Kboard_Obj;
}
static bool
60
OBJFWDP (lispfwd a)
61 62 63 64
{
  return XFWDTYPE (a) == Lisp_Fwd_Obj;
}

65
static struct Lisp_Boolfwd const *
66
XBOOLFWD (lispfwd a)
67 68
{
  eassert (BOOLFWDP (a));
69
  return a.fwdptr;
70
}
71
static struct Lisp_Kboard_Objfwd const *
72
XKBOARD_OBJFWD (lispfwd a)
73 74
{
  eassert (KBOARD_OBJFWDP (a));
75
  return a.fwdptr;
76
}
77
static struct Lisp_Intfwd const *
78
XFIXNUMFWD (lispfwd a)
79 80
{
  eassert (INTFWDP (a));
81
  return a.fwdptr;
82
}
83
static struct Lisp_Objfwd const *
84
XOBJFWD (lispfwd a)
85 86
{
  eassert (OBJFWDP (a));
87
  return a.fwdptr;
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
}

static void
CHECK_SUBR (Lisp_Object x)
{
  CHECK_TYPE (SUBRP (x), Qsubrp, x);
}

static void
set_blv_found (struct Lisp_Buffer_Local_Value *blv, int found)
{
  eassert (found == !EQ (blv->defcell, blv->valcell));
  blv->found = found;
}

static Lisp_Object
blv_value (struct Lisp_Buffer_Local_Value *blv)
{
  return XCDR (blv->valcell);
}

static void
set_blv_value (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
{
  XSETCDR (blv->valcell, val);
}

static void
set_blv_where (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
{
  blv->where = val;
}

static void
set_blv_defcell (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
{
  blv->defcell = val;
}

static void
set_blv_valcell (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
{
  blv->valcell = val;
}
132

133
static AVOID
134 135
wrong_length_argument (Lisp_Object a1, Lisp_Object a2, Lisp_Object a3)
{
136 137
  Lisp_Object size1 = make_fixnum (bool_vector_size (a1));
  Lisp_Object size2 = make_fixnum (bool_vector_size (a2));
138 139 140 141
  if (NILP (a3))
    xsignal2 (Qwrong_length_argument, size1, size2);
  else
    xsignal3 (Qwrong_length_argument, size1, size2,
142
	      make_fixnum (bool_vector_size (a3)));
143 144
}

145
AVOID
146
wrong_type_argument (register Lisp_Object predicate, register Lisp_Object value)
Jim Blandy's avatar
Jim Blandy committed
147
{
148 149 150 151 152
  /* 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)
153
   *   emacs_abort (); */
154

155
  xsignal2 (Qwrong_type_argument, predicate, value);
Jim Blandy's avatar
Jim Blandy committed
156 157
}

Andreas Schwab's avatar
Andreas Schwab committed
158
void
159
pure_write_error (Lisp_Object obj)
Jim Blandy's avatar
Jim Blandy committed
160
{
161
  xsignal2 (Qerror, build_string ("Attempt to modify read-only object"), obj);
Jim Blandy's avatar
Jim Blandy committed
162 163 164
}

void
165
args_out_of_range (Lisp_Object a1, Lisp_Object a2)
Jim Blandy's avatar
Jim Blandy committed
166
{
167
  xsignal2 (Qargs_out_of_range, a1, a2);
Jim Blandy's avatar
Jim Blandy committed
168 169 170
}

void
171
args_out_of_range_3 (Lisp_Object a1, Lisp_Object a2, Lisp_Object a3)
Jim Blandy's avatar
Jim Blandy committed
172
{
173
  xsignal3 (Qargs_out_of_range, a1, a2, a3);
Jim Blandy's avatar
Jim Blandy committed
174 175
}

Paul Eggert's avatar
Paul Eggert committed
176 177 178 179 180 181
void
circular_list (Lisp_Object list)
{
  xsignal1 (Qcircular_list, list);
}

Jim Blandy's avatar
Jim Blandy committed
182

183
/* Data type predicates.  */
Jim Blandy's avatar
Jim Blandy committed
184 185

DEFUN ("eq", Feq, Seq, 2, 2, 0,
186 187
       doc: /* Return t if the two args are the same Lisp object.  */
       attributes: const)
188
  (Lisp_Object obj1, Lisp_Object obj2)
Jim Blandy's avatar
Jim Blandy committed
189 190 191 192 193 194
{
  if (EQ (obj1, obj2))
    return Qt;
  return Qnil;
}

195
DEFUN ("null", Fnull, Snull, 1, 1, 0,
196
       doc: /* Return t if OBJECT is nil, and return nil otherwise.  */
197
       attributes: const)
198
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
199
{
200
  if (NILP (object))
Jim Blandy's avatar
Jim Blandy committed
201 202 203 204
    return Qt;
  return Qnil;
}

205
DEFUN ("type-of", Ftype_of, Stype_of, 1, 1, 0,
206 207 208
       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'.  */)
209
  (Lisp_Object object)
210
{
211
  switch (XTYPE (object))
212
    {
213
    case_Lisp_Int:
214 215 216 217 218 219 220 221 222 223 224 225
      return Qinteger;

    case Lisp_Symbol:
      return Qsymbol;

    case Lisp_String:
      return Qstring;

    case Lisp_Cons:
      return Qcons;

    case Lisp_Vectorlike:
226 227 228
      switch (PSEUDOVECTOR_TYPE (XVECTOR (object)))
        {
        case PVEC_NORMAL_VECTOR: return Qvector;
229 230 231 232 233
	case PVEC_BIGNUM: return Qinteger;
	case PVEC_MARKER: return Qmarker;
	case PVEC_OVERLAY: return Qoverlay;
	case PVEC_FINALIZER: return Qfinalizer;
	case PVEC_USER_PTR: return Quser_ptr;
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
        case PVEC_WINDOW_CONFIGURATION: return Qwindow_configuration;
        case PVEC_PROCESS: return Qprocess;
        case PVEC_WINDOW: return Qwindow;
        case PVEC_SUBR: return Qsubr;
        case PVEC_COMPILED: return Qcompiled_function;
        case PVEC_BUFFER: return Qbuffer;
        case PVEC_CHAR_TABLE: return Qchar_table;
        case PVEC_BOOL_VECTOR: return Qbool_vector;
        case PVEC_FRAME: return Qframe;
        case PVEC_HASH_TABLE: return Qhash_table;
        case PVEC_FONT:
          if (FONT_SPEC_P (object))
	    return Qfont_spec;
          if (FONT_ENTITY_P (object))
	    return Qfont_entity;
          if (FONT_OBJECT_P (object))
	    return Qfont_object;
          else
            emacs_abort (); /* return Qfont?  */
        case PVEC_THREAD: return Qthread;
        case PVEC_MUTEX: return Qmutex;
        case PVEC_CONDVAR: return Qcondition_variable;
        case PVEC_TERMINAL: return Qterminal;
257 258 259
        case PVEC_RECORD:
          {
            Lisp_Object t = AREF (object, 0);
260
            if (RECORDP (t) && 1 < PVSIZE (t))
261 262 263 264 265
              /* Return the type name field of the class!  */
              return AREF (t, 1);
            else
              return t;
          }
266 267
        case PVEC_MODULE_FUNCTION:
          return Qmodule_function;
268
        case PVEC_XWIDGET:
269
          return Qxwidget;
270
        case PVEC_XWIDGET_VIEW:
271
          return Qxwidget_view;
272
        /* "Impossible" cases.  */
273
	case PVEC_MISC_PTR:
274 275 276 277 278
        case PVEC_OTHER:
        case PVEC_SUB_CHAR_TABLE:
        case PVEC_FREE: ;
        }
      emacs_abort ();
279 280 281 282 283

    case Lisp_Float:
      return Qfloat;

    default:
284
      emacs_abort ();
285 286 287
    }
}

288
DEFUN ("consp", Fconsp, Sconsp, 1, 1, 0,
289 290
       doc: /* Return t if OBJECT is a cons cell.  */
       attributes: const)
291
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
292
{
293
  if (CONSP (object))
Jim Blandy's avatar
Jim Blandy committed
294 295 296 297
    return Qt;
  return Qnil;
}

298
DEFUN ("atom", Fatom, Satom, 1, 1, 0,
299 300
       doc: /* Return t if OBJECT is not a cons cell.  This includes nil.  */
       attributes: const)
301
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
302
{
303
  if (CONSP (object))
Jim Blandy's avatar
Jim Blandy committed
304 305 306 307
    return Qnil;
  return Qt;
}

308
DEFUN ("listp", Flistp, Slistp, 1, 1, 0,
Luc Teirlinck's avatar
Luc Teirlinck committed
309
       doc: /* Return t if OBJECT is a list, that is, a cons cell or nil.
310 311
Otherwise, return nil.  */
       attributes: const)
312
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
313
{
314
  if (CONSP (object) || NILP (object))
Jim Blandy's avatar
Jim Blandy committed
315 316 317 318
    return Qt;
  return Qnil;
}

319
DEFUN ("nlistp", Fnlistp, Snlistp, 1, 1, 0,
320 321
       doc: /* Return t if OBJECT is not a list.  Lists include nil.  */
       attributes: const)
322
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
323
{
324
  if (CONSP (object) || NILP (object))
Jim Blandy's avatar
Jim Blandy committed
325 326 327 328
    return Qnil;
  return Qt;
}

329
DEFUN ("symbolp", Fsymbolp, Ssymbolp, 1, 1, 0,
330 331
       doc: /* Return t if OBJECT is a symbol.  */
       attributes: const)
332
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
333
{
334
  if (SYMBOLP (object))
Jim Blandy's avatar
Jim Blandy committed
335 336 337 338
    return Qt;
  return Qnil;
}

Dave Love's avatar
Dave Love committed
339
DEFUN ("keywordp", Fkeywordp, Skeywordp, 1, 1, 0,
340 341 342
       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.  */)
343
  (Lisp_Object object)
Dave Love's avatar
Dave Love committed
344 345
{
  if (SYMBOLP (object)
346
      && SREF (SYMBOL_NAME (object), 0) == ':'
347
      && SYMBOL_INTERNED_IN_INITIAL_OBARRAY_P (object))
Dave Love's avatar
Dave Love committed
348 349 350 351
    return Qt;
  return Qnil;
}

352
DEFUN ("vectorp", Fvectorp, Svectorp, 1, 1, 0,
353
       doc: /* Return t if OBJECT is a vector.  */)
354
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
355
{
356
  if (VECTORP (object))
Jim Blandy's avatar
Jim Blandy committed
357 358 359 360
    return Qt;
  return Qnil;
}

361
DEFUN ("recordp", Frecordp, Srecordp, 1, 1, 0,
362 363 364 365 366 367 368 369
       doc: /* Return t if OBJECT is a record.  */)
  (Lisp_Object object)
{
  if (RECORDP (object))
    return Qt;
  return Qnil;
}

370
DEFUN ("stringp", Fstringp, Sstringp, 1, 1, 0,
371 372
       doc: /* Return t if OBJECT is a string.  */
       attributes: const)
373
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
374
{
375
  if (STRINGP (object))
Jim Blandy's avatar
Jim Blandy committed
376 377 378 379
    return Qt;
  return Qnil;
}

380
DEFUN ("multibyte-string-p", Fmultibyte_string_p, Smultibyte_string_p,
381
       1, 1, 0,
382 383
       doc: /* Return t if OBJECT is a multibyte string.
Return nil if OBJECT is either a unibyte string, or not a string.  */)
384
  (Lisp_Object object)
385 386 387 388 389 390 391
{
  if (STRINGP (object) && STRING_MULTIBYTE (object))
    return Qt;
  return Qnil;
}

DEFUN ("char-table-p", Fchar_table_p, Schar_table_p, 1, 1, 0,
392
       doc: /* Return t if OBJECT is a char-table.  */)
393
  (Lisp_Object object)
394 395 396 397 398 399
{
  if (CHAR_TABLE_P (object))
    return Qt;
  return Qnil;
}

400 401
DEFUN ("vector-or-char-table-p", Fvector_or_char_table_p,
       Svector_or_char_table_p, 1, 1, 0,
402
       doc: /* Return t if OBJECT is a char-table or vector.  */)
403
  (Lisp_Object object)
404 405 406 407 408 409
{
  if (VECTORP (object) || CHAR_TABLE_P (object))
    return Qt;
  return Qnil;
}

410 411
DEFUN ("bool-vector-p", Fbool_vector_p, Sbool_vector_p, 1, 1, 0,
       doc: /* Return t if OBJECT is a bool-vector.  */)
412
  (Lisp_Object object)
413 414 415 416 417 418
{
  if (BOOL_VECTOR_P (object))
    return Qt;
  return Qnil;
}

419 420
DEFUN ("arrayp", Farrayp, Sarrayp, 1, 1, 0,
       doc: /* Return t if OBJECT is an array (string or vector).  */)
421
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
422
{
423
  if (ARRAYP (object))
Jim Blandy's avatar
Jim Blandy committed
424 425 426 427 428
    return Qt;
  return Qnil;
}

DEFUN ("sequencep", Fsequencep, Ssequencep, 1, 1, 0,
429
       doc: /* Return t if OBJECT is a sequence (list or array).  */)
430
  (register Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
431
{
432
  if (CONSP (object) || NILP (object) || ARRAYP (object))
Jim Blandy's avatar
Jim Blandy committed
433 434 435 436
    return Qt;
  return Qnil;
}

437 438
DEFUN ("bufferp", Fbufferp, Sbufferp, 1, 1, 0,
       doc: /* Return t if OBJECT is an editor buffer.  */)
439
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
440
{
441
  if (BUFFERP (object))
Jim Blandy's avatar
Jim Blandy committed
442 443 444 445
    return Qt;
  return Qnil;
}

446 447
DEFUN ("markerp", Fmarkerp, Smarkerp, 1, 1, 0,
       doc: /* Return t if OBJECT is a marker (editor pointer).  */)
448
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
449
{
450
  if (MARKERP (object))
Jim Blandy's avatar
Jim Blandy committed
451 452 453 454
    return Qt;
  return Qnil;
}

455 456 457 458 459 460 461 462 463 464 465
#ifdef HAVE_MODULES
DEFUN ("user-ptrp", Fuser_ptrp, Suser_ptrp, 1, 1, 0,
       doc: /* Return t if OBJECT is a module user pointer.  */)
     (Lisp_Object object)
{
  if (USER_PTRP (object))
    return Qt;
  return Qnil;
}
#endif

466 467
DEFUN ("subrp", Fsubrp, Ssubrp, 1, 1, 0,
       doc: /* Return t if OBJECT is a built-in function.  */)
468
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
469
{
470
  if (SUBRP (object))
Jim Blandy's avatar
Jim Blandy committed
471 472 473 474
    return Qt;
  return Qnil;
}

475
DEFUN ("byte-code-function-p", Fbyte_code_function_p, Sbyte_code_function_p,
476 477
       1, 1, 0,
       doc: /* Return t if OBJECT is a byte-compiled function object.  */)
478
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
479
{
480
  if (COMPILEDP (object))
Jim Blandy's avatar
Jim Blandy committed
481 482 483 484
    return Qt;
  return Qnil;
}

485 486 487 488 489 490 491 492
DEFUN ("module-function-p", Fmodule_function_p, Smodule_function_p, 1, 1, NULL,
       doc: /* Return t if OBJECT is a function loaded from a dynamic module.  */
       attributes: const)
  (Lisp_Object object)
{
  return MODULE_FUNCTIONP (object) ? Qt : Qnil;
}

493
DEFUN ("char-or-string-p", Fchar_or_string_p, Schar_or_string_p, 1, 1, 0,
494 495
       doc: /* Return t if OBJECT is a character or a string.  */
       attributes: const)
496
  (register Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
497
{
498
  if (CHARACTERP (object) || STRINGP (object))
Jim Blandy's avatar
Jim Blandy committed
499 500 501 502
    return Qt;
  return Qnil;
}

503
DEFUN ("integerp", Fintegerp, Sintegerp, 1, 1, 0,
504 505
       doc: /* Return t if OBJECT is an integer.  */
       attributes: const)
506
  (Lisp_Object object)
Tom Tromey's avatar
Tom Tromey committed
507 508 509 510 511 512
{
  if (INTEGERP (object))
    return Qt;
  return Qnil;
}

513
DEFUN ("integer-or-marker-p", Finteger_or_marker_p, Sinteger_or_marker_p, 1, 1, 0,
514
       doc: /* Return t if OBJECT is an integer or a marker (editor pointer).  */)
515
  (register Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
516
{
Tom Tromey's avatar
Tom Tromey committed
517
  if (MARKERP (object) || INTEGERP (object))
Jim Blandy's avatar
Jim Blandy committed
518 519 520 521
    return Qt;
  return Qnil;
}

522
DEFUN ("natnump", Fnatnump, Snatnump, 1, 1, 0,
523 524
       doc: /* Return t if OBJECT is a nonnegative integer.  */
       attributes: const)
525
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
526
{
Paul Eggert's avatar
Paul Eggert committed
527
  return ((FIXNUMP (object) ? 0 <= XFIXNUM (object)
528
	   : BIGNUMP (object) && 0 <= mpz_sgn (*xbignum_val (object)))
Paul Eggert's avatar
Paul Eggert committed
529
	  ? Qt : Qnil);
Jim Blandy's avatar
Jim Blandy committed
530 531 532
}

DEFUN ("numberp", Fnumberp, Snumberp, 1, 1, 0,
533 534
       doc: /* Return t if OBJECT is a number (floating point or integer).  */
       attributes: const)
535
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
536
{
Tom Tromey's avatar
Tom Tromey committed
537
  if (NUMBERP (object))
Jim Blandy's avatar
Jim Blandy committed
538
    return Qt;
539 540
  else
    return Qnil;
Jim Blandy's avatar
Jim Blandy committed
541 542 543 544
}

DEFUN ("number-or-marker-p", Fnumber_or_marker_p,
       Snumber_or_marker_p, 1, 1, 0,
545
       doc: /* Return t if OBJECT is a number or a marker.  */)
546
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
547
{
Tom Tromey's avatar
Tom Tromey committed
548
  if (NUMBERP (object) || MARKERP (object))
Jim Blandy's avatar
Jim Blandy committed
549 550 551
    return Qt;
  return Qnil;
}
552 553

DEFUN ("floatp", Ffloatp, Sfloatp, 1, 1, 0,
554 555
       doc: /* Return t if OBJECT is a floating point number.  */
       attributes: const)
556
  (Lisp_Object object)
557
{
558
  if (FLOATP (object))
559 560 561
    return Qt;
  return Qnil;
}
562

563 564 565 566 567 568
DEFUN ("threadp", Fthreadp, Sthreadp, 1, 1, 0,
       doc: /* Return t if OBJECT is a thread.  */)
  (Lisp_Object object)
{
  if (THREADP (object))
    return Qt;
569
  return Qnil;
570 571
}

572 573 574 575 576 577
DEFUN ("mutexp", Fmutexp, Smutexp, 1, 1, 0,
       doc: /* Return t if OBJECT is a mutex.  */)
  (Lisp_Object object)
{
  if (MUTEXP (object))
    return Qt;
578
  return Qnil;
579
}
Tom Tromey's avatar
Tom Tromey committed
580

581
DEFUN ("condition-variable-p", Fcondition_variable_p, Scondition_variable_p,
Tom Tromey's avatar
Tom Tromey committed
582 583 584 585 586 587
       1, 1, 0,
       doc: /* Return t if OBJECT is a condition variable.  */)
  (Lisp_Object object)
{
  if (CONDVARP (object))
    return Qt;
588
  return Qnil;
Tom Tromey's avatar
Tom Tromey committed
589
}
Jim Blandy's avatar
Jim Blandy committed
590

591
/* Extract and set components of lists.  */
Jim Blandy's avatar
Jim Blandy committed
592

Paul Eggert's avatar
Paul Eggert committed
593
DEFUN ("car", Fcar, Scar, 1, 1, 0,
594
       doc: /* Return the car of LIST.  If arg is nil, return nil.
595 596
Error if arg is not nil and not a cons cell.  See also `car-safe'.

Luc Teirlinck's avatar
Luc Teirlinck committed
597 598
See Info node `(elisp)Cons Cells' for a discussion of related basic
Lisp concepts such as car, cdr, cons cell and list.  */)
599
  (register Lisp_Object list)
Jim Blandy's avatar
Jim Blandy committed
600
{
601
  return CAR (list);
Jim Blandy's avatar
Jim Blandy committed
602 603
}

Paul Eggert's avatar
Paul Eggert committed
604
DEFUN ("car-safe", Fcar_safe, Scar_safe, 1, 1, 0,
605
       doc: /* Return the car of OBJECT if it is a cons cell, or else nil.  */)
606
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
607
{
608
  return CAR_SAFE (object);
Jim Blandy's avatar
Jim Blandy committed
609 610
}

Paul Eggert's avatar
Paul Eggert committed
611
DEFUN ("cdr", Fcdr, Scdr, 1, 1, 0,
612
       doc: /* Return the cdr of LIST.  If arg is nil, return nil.
613 614
Error if arg is not nil and not a cons cell.  See also `cdr-safe'.

Luc Teirlinck's avatar
Luc Teirlinck committed
615 616
See Info node `(elisp)Cons Cells' for a discussion of related basic
Lisp concepts such as cdr, car, cons cell and list.  */)
617
  (register Lisp_Object list)
Jim Blandy's avatar
Jim Blandy committed
618
{
619
  return CDR (list);
Jim Blandy's avatar
Jim Blandy committed
620 621
}

Paul Eggert's avatar
Paul Eggert committed
622
DEFUN ("cdr-safe", Fcdr_safe, Scdr_safe, 1, 1, 0,
623
       doc: /* Return the cdr of OBJECT if it is a cons cell, or else nil.  */)
624
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
625
{
626
  return CDR_SAFE (object);
Jim Blandy's avatar
Jim Blandy committed
627 628
}

Paul Eggert's avatar
Paul Eggert committed
629
DEFUN ("setcar", Fsetcar, Ssetcar, 2, 2, 0,
630
       doc: /* Set the car of CELL to be NEWCAR.  Returns NEWCAR.  */)
631
  (register Lisp_Object cell, Lisp_Object newcar)
Jim Blandy's avatar
Jim Blandy committed
632
{
633
  CHECK_CONS (cell);
634
  CHECK_IMPURE (cell, XCONS (cell));
635
  XSETCAR (cell, newcar);
Jim Blandy's avatar
Jim Blandy committed
636 637 638
  return newcar;
}

Paul Eggert's avatar
Paul Eggert committed
639
DEFUN ("setcdr", Fsetcdr, Ssetcdr, 2, 2, 0,
640
       doc: /* Set the cdr of CELL to be NEWCDR.  Returns NEWCDR.  */)
641
  (register Lisp_Object cell, Lisp_Object newcdr)
Jim Blandy's avatar
Jim Blandy committed
642
{
643
  CHECK_CONS (cell);
644
  CHECK_IMPURE (cell, XCONS (cell));
645
  XSETCDR (cell, newcdr);
Jim Blandy's avatar
Jim Blandy committed
646 647 648
  return newcdr;
}

649
/* Extract and set components of symbols.  */
Jim Blandy's avatar
Jim Blandy committed
650

Paul Eggert's avatar
Paul Eggert committed
651
DEFUN ("boundp", Fboundp, Sboundp, 1, 1, 0,
652 653 654
       doc: /* Return t if SYMBOL's value is not void.
Note that if `lexical-binding' is in effect, this refers to the
global value outside of any lexical scope.  */)
655
  (register Lisp_Object symbol)
Jim Blandy's avatar
Jim Blandy committed
656 657
{
  Lisp_Object valcontents;
658
  struct Lisp_Symbol *sym;
659
  CHECK_SYMBOL (symbol);
660
  sym = XSYMBOL (symbol);
Jim Blandy's avatar
Jim Blandy committed
661

662
 start:
663
  switch (sym->u.s.redirect)
664 665 666 667 668 669
    {
    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);
670
	if (blv->fwd.fwdptr)
671
	  /* In set_internal, we un-forward vars when their value is
672
	     set to Qunbound.  */
673 674 675 676
    	  return Qt;
	else
	  {
	    swap_in_symval_forwarding (sym, blv);
677
	    valcontents = blv_value (blv);
678 679 680 681 682
	  }
	break;
      }
    case SYMBOL_FORWARDED:
      /* In set_internal, we un-forward vars when their value is
683
	 set to Qunbound.  */
684
      return Qt;
685
    default: emacs_abort ();
686
    }
Jim Blandy's avatar
Jim Blandy committed
687

688
  return (EQ (valcontents, Qunbound) ? Qnil : Qt);
Jim Blandy's avatar
Jim Blandy committed
689 690
}

691 692 693 694
/* It has been previously suggested to make this function an alias for
   symbol-function, but upon discussion at Bug#23957, there is a risk
   breaking backward compatibility, as some users of fboundp may
   expect `t' in particular, rather than any true value.  */
Paul Eggert's avatar
Paul Eggert committed
695
DEFUN ("fboundp", Ffboundp, Sfboundp, 1, 1, 0,
696
       doc: /* Return t if SYMBOL's function definition is not void.  */)
697
  (Lisp_Object symbol)
Jim Blandy's avatar
Jim Blandy committed
698
{
699
  CHECK_SYMBOL (symbol);
700
  return NILP (XSYMBOL (symbol)->u.s.function) ? Qnil : Qt;
Jim Blandy's avatar
Jim Blandy committed
701 702
}

703
DEFUN ("makunbound", Fmakunbound, Smakunbound, 1, 1, 0,
704 705
       doc: /* Make SYMBOL's value be void.
Return SYMBOL.  */)
706
  (register Lisp_Object symbol)
Jim Blandy's avatar
Jim Blandy committed
707
{
708
  CHECK_SYMBOL (symbol);
709
  if (SYMBOL_CONSTANT_P (symbol))
710
    xsignal1 (Qsetting_constant, symbol);
711 712
  Fset (symbol, Qunbound);
  return symbol;
Jim Blandy's avatar
Jim Blandy committed
713 714
}

715
DEFUN ("fmakunbound", Ffmakunbound, Sfmakunbound, 1, 1, 0,
716
       doc: /* Make SYMBOL's function definition be nil.
717
Return SYMBOL.  */)
718
  (register Lisp_Object symbol)
Jim Blandy's avatar
Jim Blandy committed
719
{
720
  CHECK_SYMBOL (symbol);
721
  if (NILP (symbol) || EQ (symbol, Qt))
722
    xsignal1 (Qsetting_constant, symbol);
723
  set_symbol_function (symbol, Qnil);
724
  return symbol;
Jim Blandy's avatar
Jim Blandy committed
725 726
}

Paul Eggert's avatar
Paul Eggert committed
727
DEFUN ("symbol-function", Fsymbol_function, Ssymbol_function, 1, 1, 0,
Chong Yidong's avatar
Chong Yidong committed
728
       doc: /* Return SYMBOL's function definition, or nil if that is void.  */)
729
  (Lisp_Object symbol)
Jim Blandy's avatar
Jim Blandy committed
730
{
731
  CHECK_SYMBOL (symbol);
732
  return XSYMBOL (symbol)->u.s.function;
Jim Blandy's avatar
Jim Blandy committed
733 734
}

735 736
DEFUN ("symbol-plist", Fsymbol_plist, Ssymbol_plist, 1, 1, 0,
       doc: /* Return SYMBOL's property list.  */)
737
  (Lisp_Object symbol)
Jim Blandy's avatar
Jim Blandy committed
738
{
739
  CHECK_SYMBOL (symbol);
740
  return XSYMBOL (symbol)->u.s.plist;
Jim Blandy's avatar
Jim Blandy committed
741 742
}

Paul Eggert's avatar
Paul Eggert committed
743
DEFUN ("symbol-name", Fsymbol_name, Ssymbol_name, 1, 1, 0,
744
       doc: /* Return SYMBOL's name, a string.  */)
745
  (register Lisp_Object symbol)
Jim Blandy's avatar
Jim Blandy committed
746 747 748
{
  register Lisp_Object name;

749
  CHECK_SYMBOL (symbol);
750
  name = SYMBOL_NAME (symbol);
Jim Blandy's avatar
Jim Blandy committed
751 752 753
  return name;
}

Paul Eggert's avatar
Paul Eggert committed
754
DEFUN ("fset", Ffset, Sfset, 2, 2, 0,
755
       doc: /* Set SYMBOL's function definition to DEFINITION, and return DEFINITION.  */)
756
  (register Lisp_Object symbol, Lisp_Object definition)
757
{
758
  register Lisp_Object function;
759
  CHECK_SYMBOL (symbol);
Glenn Morris's avatar
Glenn Morris committed
760
  /* Perhaps not quite the right error signal, but seems good enough.  */
761 762 763
  if (NILP (symbol) && !NILP (definition))
    /* There are so many other ways to shoot oneself in the foot, I don't
       think this one little sanity check is worth its cost, but anyway.  */
Glenn Morris's avatar
Glenn Morris committed
764
    xsignal1 (Qsetting_constant, symbol);
765

766
  function = XSYMBOL (symbol)->u.s.function;
767

768
  if (!NILP (Vautoload_queue) && !NILP (function))
769 770
    Vautoload_queue = Fcons (Fcons (symbol, function), Vautoload_queue);

771
  if (AUTOLOADP (function))
772 773
    Fput (symbol, Qautoload, XCDR (function));

Paul Eggert's avatar
Paul Eggert committed
774
  eassert (valid_lisp_object_p (definition));
775

776
  set_symbol_function (symbol, definition);
777

778
  return definition;
Jim Blandy's avatar
Jim Blandy committed
779 780
}

781
DEFUN ("defalias", Fdefalias, Sdefalias, 2, 3, 0,
782
       doc: /* Set SYMBOL's function definition to DEFINITION.
Richard M. Stallman's avatar
Richard M. Stallman committed
783 784 785
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
786
determined by DEFINITION.
787 788 789 790

Internally, this normally uses `fset', but if SYMBOL has a
`defalias-fset-function' property, the associated value is used instead.

791
The return value is undefined.  */)
792
  (register Lisp_Object symbol, Lisp_Object definition, Lisp_Object docstring)
793
{
794
  CHECK_SYMBOL (symbol);
795 796 797 798
  if (!NILP (Vpurify_flag)
      /* If `definition' is a keymap, immutable (and copying) is wrong.  */
      && !KEYMAPP (definition))
    definition = Fpurecopy (definition);
799 800 801

  {
    bool autoload = AUTOLOADP (definition);
Daniel Colascione's avatar
Daniel Colascione committed
802
    if (!will_dump_p () || !autoload)
803 804 805
      { /* Only add autoload entries after dumping, because the ones before are
	   not useful and else we get loads of them from the loaddefs.el.  */

806
	if (AUTOLOADP (XSYMBOL (symbol)->u.s.function))
807 808 809 810 811 812 813 814 815 816 817 818 819 820
	  /* Remember that the function was already an autoload.  */
	  LOADHIST_ATTACH (Fcons (Qt, symbol));
	LOADHIST_ATTACH (Fcons (autoload ? Qautoload : Qdefun, symbol));
      }
  }

  { /* Handle automatic advice activation.  */
    Lisp_Object hook = Fget (symbol, Qdefalias_fset_function);
    if (!NILP (hook))
      call2 (hook, symbol, definition);
    else
      Ffset (symbol, definition);
  }

821 822
  if (!NILP (docstring))
    Fput (symbol, Qfunction_documentation, docstring);
823 824 825 826
  /* We used to return `definition', but now that `defun' and `defmacro' expand
     to a call to `defalias', we return `symbol' for backward compatibility
     (bug#11686).  */
  return symbol;
827 828
}

Jim Blandy's avatar
Jim Blandy committed
829
DEFUN ("setplist", Fsetplist, Ssetplist, 2, 2, 0,
Kenichi Handa's avatar
Kenichi Handa committed
830
       doc: /* Set SYMBOL's property list to NEWPLIST, and return NEWPLIST.  */)
831
  (register Lisp_Object symbol, Lisp_Object newplist)
Jim Blandy's avatar
Jim Blandy committed
832
{
833
  CHECK_SYMBOL (symbol);
834
  set_symbol_plist (symbol, newplist);
Jim Blandy's avatar
Jim Blandy committed
835 836
  return newplist;
}
Jim Blandy's avatar
Jim Blandy committed
837

838
DEFUN ("subr-arity", Fsubr_arity, Ssubr_arity, 1, 1, 0,
839 840 841 842 843
       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.  */)
844
  (Lisp_Object subr)
845 846
{
  short minargs, maxargs;
847
  CHECK_SUBR (subr);
848 849
  minargs = XSUBR (subr)->min_args;
  maxargs = XSUBR (subr)->max_args;
850
  return Fcons (make_fixnum (minargs),
851 852
		maxargs == MANY ?        Qmany
		: maxargs == UNEVALLED ? Qunevalled
853
		:                        make_fixnum (maxargs));
854 855
}

Stefan Monnier's avatar
Stefan Monnier committed
856 857 858
DEFUN ("subr-name", Fsubr_name, Ssubr_name, 1, 1, 0,
       doc: /* Return name of subroutine SUBR.
SUBR must be a built-in function.  */)
859
  (Lisp_Object subr)
Stefan Monnier's avatar
Stefan Monnier committed
860 861
{
  const char *name;
862
  CHECK_SUBR (subr);
Stefan Monnier's avatar
Stefan Monnier committed
863
  name = XSUBR (subr)->symbol_name;
864
  return build_string (name);
Stefan Monnier's avatar
Stefan Monnier committed
865 866
}

Paul Eggert's avatar
Paul Eggert committed
867
DEFUN ("interactive-form", Finteractive_form, Sinteractive_form, 1, 1, 0,
Kenichi Handa's avatar
Kenichi Handa committed
868
       doc: /* Return the interactive form of CMD or nil if none.
869
If CMD is not a command, the return value is nil.
870
Value, if non-nil, is a list (interactive SPEC).  */)
871
  (Lisp_Object cmd)
872
{
873
  Lisp_Object fun = indirect_function (cmd); /* Check cycles.  */
874

875
  if (NILP (fun))
876 877 878
    return Qnil;

  /* Use an `interactive-form' property if present, analogous to the
879
     function-documentation property.  */
880 881 882
  fun = cmd;
  while (SYMBOLP (fun))
    {
883
      Lisp_Object tmp = Fget (fun, Qinteractive_form);
884 885 886 887 888
      if (!NILP (tmp))
	return tmp;
      else
	fun = Fsymbol_function (fun);
    }
Kenichi Handa's avatar
Kenichi Handa committed
889 890 891

  if (SUBRP (fun))
    {
892
      const char *spec = XSUBR (fun)->intspec;
893 894 895 896
      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
897 898 899
    }
  else if (COMPILEDP (fun))
    {
900
      if (PVSIZE (fun) > COMPILED_INTERACTIVE)
Kenichi Handa's avatar
Kenichi Handa committed
901 902
	return list2 (Qinteractive, AREF (fun, COMPILED_INTERACTIVE));
    }
903 904
  else if (AUTOLOADP (fun))
    return Finteractive_form (Fautoload_do_load (fun, cmd, Qnil));
Kenichi Handa's avatar
Kenichi Handa committed
905 906 907
  else if (CONSP (fun))
    {
      Lisp_Object funcar = XCAR (fun);
908
      if (EQ (funcar, Qclosure))
909 910
	return Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun))));
      else if (EQ (funcar, Qlambda))
Kenichi Handa's avatar
Kenichi Handa committed
911 912
	return Fassq (Qinteractive, Fcdr (XCDR (fun)));
    }
913 914 915
  return Qnil;
}

Jim Blandy's avatar
Jim Blandy committed
916

917 918 919 920 921 922 923 924
/***********************************************************************
		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.  */

925
struct Lisp_Symbol *
926
indirect_variable (struct Lisp_Symbol *symbol)
927
{
928
  struct Lisp_Symbol *tortoise, *hare;
929 930 931

  hare = tortoise = symbol;

932
  while (hare->u.s.redirect == SYMBOL_VARALIAS)
933
    {
934
      hare = SYMBOL_ALIAS (hare);
935
      if (hare->u.s.redirect != SYMBOL_VARALIAS)
936
	break;
937

938 939
      hare = SYMBOL_ALIAS (hare);
      tortoise = SYMBOL_ALIAS (tortoise);
940

941 942 943 944 945 946
      if (hare == tortoise)
	{
	  Lisp_Object tem;
	  XSETSYMBOL (tem, symbol);
	  xsignal1 (Qcyclic_variable_indirection, tem);
	}
947 948 949 950 951 952 953
    }

  return hare;
}


DEFUN ("indirect-variable", Findirect_variable, Sindirect_variable, 1, 1, 0,
954
       doc: /* Return the variable at the end of OBJECT's variable chain.
Chong Yidong's avatar
Chong Yidong committed
955 956 957 958 959 960
If OBJECT is a symbol, follow its variable indirections (if any), and
return the variable at the end of the chain of aliases.  See Info node
`(elisp)Variable Aliases'.

If OBJECT is not a symbol, just return it.  If there is a loop in the
chain of aliases, signal a `cyclic-variable-indirection' error.  */)
961
  (Lisp_Object object)