data.c 116 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-2019 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)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
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)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
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'.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
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 234 235
	case PVEC_BIGNUM: return Qinteger;
	case PVEC_MARKER: return Qmarker;
	case PVEC_OVERLAY: return Qoverlay;
	case PVEC_FINALIZER: return Qfinalizer;
#ifdef HAVE_MODULES
	case PVEC_USER_PTR: return Quser_ptr;
#endif
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
        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;
259 260 261
        case PVEC_RECORD:
          {
            Lisp_Object t = AREF (object, 0);
262
            if (RECORDP (t) && 1 < PVSIZE (t))
263 264 265 266 267
              /* Return the type name field of the class!  */
              return AREF (t, 1);
            else
              return t;
          }
268 269
        case PVEC_MODULE_FUNCTION:
          return Qmodule_function;
270
        case PVEC_XWIDGET:
271
          return Qxwidget;
272
        case PVEC_XWIDGET_VIEW:
273
          return Qxwidget_view;
274
        /* "Impossible" cases.  */
275
	case PVEC_MISC_PTR:
276 277 278 279 280
        case PVEC_OTHER:
        case PVEC_SUB_CHAR_TABLE:
        case PVEC_FREE: ;
        }
      emacs_abort ();
281 282 283 284 285

    case Lisp_Float:
      return Qfloat;

    default:
286
      emacs_abort ();
287 288 289
    }
}

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

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

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

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

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

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

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

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

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

DEFUN ("char-table-p", Fchar_table_p, Schar_table_p, 1, 1, 0,
394
       doc: /* Return t if OBJECT is a char-table.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
395
  (Lisp_Object object)
396 397 398 399 400 401
{
  if (CHAR_TABLE_P (object))
    return Qt;
  return Qnil;
}

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

412 413
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
414
  (Lisp_Object object)
415 416 417 418 419 420
{
  if (BOOL_VECTOR_P (object))
    return Qt;
  return Qnil;
}

421 422
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
423
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
424
{
425
  if (ARRAYP (object))
Jim Blandy's avatar
Jim Blandy committed
426 427 428 429 430
    return Qt;
  return Qnil;
}

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

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

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

457 458 459 460 461 462 463 464 465 466 467
#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

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

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

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

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

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

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

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

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

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

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

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

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

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

Luc Teirlinck's avatar
Luc Teirlinck committed
599 600
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
601
  (register Lisp_Object list)
Jim Blandy's avatar
Jim Blandy committed
602
{
603
  return CAR (list);
Jim Blandy's avatar
Jim Blandy committed
604 605
}

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

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

Luc Teirlinck's avatar
Luc Teirlinck committed
617 618
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
619
  (register Lisp_Object list)
Jim Blandy's avatar
Jim Blandy committed
620
{
621
  return CDR (list);
Jim Blandy's avatar
Jim Blandy committed
622 623
}

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

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

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

Paul Eggert's avatar
Paul Eggert committed
653
DEFUN ("boundp", Fboundp, Sboundp, 1, 1, 0,
654 655 656
       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.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
657
  (register Lisp_Object symbol)
Jim Blandy's avatar
Jim Blandy committed
658 659
{
  Lisp_Object valcontents;
660
  struct Lisp_Symbol *sym;
661
  CHECK_SYMBOL (symbol);
662
  sym = XSYMBOL (symbol);
Jim Blandy's avatar
Jim Blandy committed
663

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

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

693 694 695 696
/* 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
697
DEFUN ("fboundp", Ffboundp, Sfboundp, 1, 1, 0,
698
       doc: /* Return t if SYMBOL's function definition is not void.  */)
699
  (Lisp_Object symbol)
Jim Blandy's avatar
Jim Blandy committed
700
{
701
  CHECK_SYMBOL (symbol);
702
  return NILP (XSYMBOL (symbol)->u.s.function) ? Qnil : Qt;
Jim Blandy's avatar
Jim Blandy committed
703 704
}

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

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

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

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

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

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

Paul Eggert's avatar
Paul Eggert committed
756
DEFUN ("fset", Ffset, Sfset, 2, 2, 0,
757
       doc: /* Set SYMBOL's function definition to DEFINITION, and return DEFINITION.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
758
  (register Lisp_Object symbol, Lisp_Object definition)
759
{
760
  register Lisp_Object function;
761
  CHECK_SYMBOL (symbol);
Glenn Morris's avatar
Glenn Morris committed
762
  /* Perhaps not quite the right error signal, but seems good enough.  */
763 764 765
  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
766
    xsignal1 (Qsetting_constant, symbol);
767

768
  function = XSYMBOL (symbol)->u.s.function;
769

770
  if (!NILP (Vautoload_queue) && !NILP (function))
771 772
    Vautoload_queue = Fcons (Fcons (symbol, function), Vautoload_queue);

773
  if (AUTOLOADP (function))
774 775
    Fput (symbol, Qautoload, XCDR (function));

776 777
  /* Convert to eassert or remove after GC bug is found.  In the
     meantime, check unconditionally, at a slight perf hit.  */
Paul Eggert's avatar
Paul Eggert committed
778
  if (! valid_lisp_object_p (definition))
779 780
    emacs_abort ();

781
  set_symbol_function (symbol, definition);
782

783
  return definition;
Jim Blandy's avatar
Jim Blandy committed
784 785
}

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

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

796
The return value is undefined.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
797
  (register Lisp_Object symbol, Lisp_Object definition, Lisp_Object docstring)
798
{
799
  CHECK_SYMBOL (symbol);
800 801 802 803
  if (!NILP (Vpurify_flag)
      /* If `definition' is a keymap, immutable (and copying) is wrong.  */
      && !KEYMAPP (definition))
    definition = Fpurecopy (definition);
804 805 806

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

811
	if (AUTOLOADP (XSYMBOL (symbol)->u.s.function))
812 813 814 815 816 817 818 819 820 821 822 823 824 825
	  /* 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);
  }

826 827
  if (!NILP (docstring))
    Fput (symbol, Qfunction_documentation, docstring);
828 829 830 831
  /* 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;
832 833
}

Jim Blandy's avatar
Jim Blandy committed
834
DEFUN ("setplist", Fsetplist, Ssetplist, 2, 2, 0,
Kenichi Handa's avatar
Kenichi Handa committed
835
       doc: /* Set SYMBOL's property list to NEWPLIST, and return NEWPLIST.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
836
  (register Lisp_Object symbol, Lisp_Object newplist)
Jim Blandy's avatar
Jim Blandy committed
837
{
838
  CHECK_SYMBOL (symbol);
839
  set_symbol_plist (symbol, newplist);
Jim Blandy's avatar
Jim Blandy committed
840 841
  return newplist;
}
Jim Blandy's avatar
Jim Blandy committed
842

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

Stefan Monnier's avatar
Stefan Monnier committed
861 862 863
DEFUN ("subr-name", Fsubr_name, Ssubr_name, 1, 1, 0,
       doc: /* Return name of subroutine SUBR.
SUBR must be a built-in function.  */)