data.c 63.2 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Primitive operations on Lisp data types for GNU Emacs Lisp interpreter.
Karl Heuer's avatar
Karl Heuer committed
2
   Copyright (C) 1985, 1986, 1988, 1993, 1994 Free Software Foundation, Inc.
Jim Blandy's avatar
Jim Blandy committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

This file is part of GNU Emacs.

GNU Emacs is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.

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
along with GNU Emacs; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */


#include <signal.h>

23
#include <config.h>
Jim Blandy's avatar
Jim Blandy committed
24
#include "lisp.h"
Jim Blandy's avatar
Jim Blandy committed
25
#include "puresize.h"
Jim Blandy's avatar
Jim Blandy committed
26 27 28 29 30

#ifndef standalone
#include "buffer.h"
#endif

Jim Blandy's avatar
Jim Blandy committed
31
#include "syssignal.h"
Jim Blandy's avatar
Jim Blandy committed
32

33
#ifdef MSDOS
Karl Heuer's avatar
Karl Heuer committed
34
/* These are redefined (correctly, but differently) in values.h.  */
35 36 37 38 39
#undef INTBITS
#undef LONGBITS
#undef SHORTBITS
#endif

Jim Blandy's avatar
Jim Blandy committed
40
#ifdef LISP_FLOAT_TYPE
41

42 43 44
#ifdef STDC_HEADERS
#include <stdlib.h>
#endif
45 46 47 48 49 50 51 52 53 54 55

/* Work around a problem that happens because math.h on hpux 7
   defines two static variables--which, in Emacs, are not really static,
   because `static' is defined as nothing.  The problem is that they are
   here, in floatfns.c, and in lread.c.
   These macros prevent the name conflict.  */
#if defined (HPUX) && !defined (HPUX8)
#define _MAXLDBL data_c_maxldbl
#define _NMAXLDBL data_c_nmaxldbl
#endif

Jim Blandy's avatar
Jim Blandy committed
56 57 58
#include <math.h>
#endif /* LISP_FLOAT_TYPE */

59 60 61 62
#if !defined (atof)
extern double atof ();
#endif /* !atof */

Jim Blandy's avatar
Jim Blandy committed
63 64 65
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
66
Lisp_Object Qvoid_variable, Qvoid_function, Qcyclic_function_indirection;
Jim Blandy's avatar
Jim Blandy committed
67 68
Lisp_Object Qsetting_constant, Qinvalid_read_syntax;
Lisp_Object Qinvalid_function, Qwrong_number_of_arguments, Qno_catch;
Roland McGrath's avatar
Roland McGrath committed
69
Lisp_Object Qend_of_file, Qarith_error, Qmark_inactive;
Jim Blandy's avatar
Jim Blandy committed
70
Lisp_Object Qbeginning_of_buffer, Qend_of_buffer, Qbuffer_read_only;
71
Lisp_Object Qintegerp, Qnatnump, Qwholenump, Qsymbolp, Qlistp, Qconsp;
Jim Blandy's avatar
Jim Blandy committed
72 73
Lisp_Object Qstringp, Qarrayp, Qsequencep, Qbufferp;
Lisp_Object Qchar_or_string_p, Qmarkerp, Qinteger_or_marker_p, Qvectorp;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
74
Lisp_Object Qbuffer_or_string_p;
Jim Blandy's avatar
Jim Blandy committed
75 76
Lisp_Object Qboundp, Qfboundp;
Lisp_Object Qcdr;
77
Lisp_Object Qad_advice_info, Qad_activate;
Jim Blandy's avatar
Jim Blandy committed
78

79 80 81
Lisp_Object Qrange_error, Qdomain_error, Qsingularity_error;
Lisp_Object Qoverflow_error, Qunderflow_error;

Jim Blandy's avatar
Jim Blandy committed
82
#ifdef LISP_FLOAT_TYPE
83
Lisp_Object Qfloatp;
Jim Blandy's avatar
Jim Blandy committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97
Lisp_Object Qnumberp, Qnumber_or_marker_p;
#endif

static Lisp_Object swap_in_symval_forwarding ();

Lisp_Object
wrong_type_argument (predicate, value)
     register Lisp_Object predicate, value;
{
  register Lisp_Object tem;
  do
    {
      if (!EQ (Vmocklisp_arguments, Qt))
	{
98
	 if (STRINGP (value) &&
Jim Blandy's avatar
Jim Blandy committed
99
	     (EQ (predicate, Qintegerp) || EQ (predicate, Qinteger_or_marker_p)))
100
	   return Fstring_to_number (value);
101
	 if (INTEGERP (value) && EQ (predicate, Qstringp))
102
	   return Fnumber_to_string (value);
Jim Blandy's avatar
Jim Blandy committed
103 104 105 106
	}
      value = Fsignal (Qwrong_type_argument, Fcons (predicate, Fcons (value, Qnil)));
      tem = call1 (predicate, value);
    }
Jim Blandy's avatar
Jim Blandy committed
107
  while (NILP (tem));
Jim Blandy's avatar
Jim Blandy committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
  return value;
}

pure_write_error ()
{
  error ("Attempt to modify read-only object");
}

void
args_out_of_range (a1, a2)
     Lisp_Object a1, a2;
{
  while (1)
    Fsignal (Qargs_out_of_range, Fcons (a1, Fcons (a2, Qnil)));
}

void
args_out_of_range_3 (a1, a2, a3)
     Lisp_Object a1, a2, a3;
{
  while (1)
    Fsignal (Qargs_out_of_range, Fcons (a1, Fcons (a2, Fcons (a3, Qnil))));
}

Lisp_Object
make_number (num)
     int num;
{
  register Lisp_Object val;
137
  XSETINT (val, num);
Jim Blandy's avatar
Jim Blandy committed
138 139 140 141 142 143 144 145 146 147 148 149
  return val;
}

/* On some machines, XINT needs a temporary location.
   Here it is, in case it is needed.  */

int sign_extend_temp;

/* On a few machines, XINT can only be done by calling this.  */

int
sign_extend_lisp_int (num)
150
     EMACS_INT num;
Jim Blandy's avatar
Jim Blandy committed
151
{
152 153
  if (num & (((EMACS_INT) 1) << (VALBITS - 1)))
    return num | (((EMACS_INT) (-1)) << VALBITS);
Jim Blandy's avatar
Jim Blandy committed
154
  else
155
    return num & ((((EMACS_INT) 1) << VALBITS) - 1);
Jim Blandy's avatar
Jim Blandy committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
}

/* Data type predicates */

DEFUN ("eq", Feq, Seq, 2, 2, 0,
  "T if the two args are the same Lisp object.")
  (obj1, obj2)
     Lisp_Object obj1, obj2;
{
  if (EQ (obj1, obj2))
    return Qt;
  return Qnil;
}

DEFUN ("null", Fnull, Snull, 1, 1, 0, "T if OBJECT is nil.")
  (obj)
     Lisp_Object obj;
{
Jim Blandy's avatar
Jim Blandy committed
174
  if (NILP (obj))
Jim Blandy's avatar
Jim Blandy committed
175 176 177 178 179 180 181 182
    return Qt;
  return Qnil;
}

DEFUN ("consp", Fconsp, Sconsp, 1, 1, 0, "T if OBJECT is a cons cell.")
  (obj)
     Lisp_Object obj;
{
183
  if (CONSP (obj))
Jim Blandy's avatar
Jim Blandy committed
184 185 186 187 188 189 190 191
    return Qt;
  return Qnil;
}

DEFUN ("atom", Fatom, Satom, 1, 1, 0, "T if OBJECT is not a cons cell.  This includes nil.")
  (obj)
     Lisp_Object obj;
{
192
  if (CONSP (obj))
Jim Blandy's avatar
Jim Blandy committed
193 194 195 196 197 198 199 200
    return Qnil;
  return Qt;
}

DEFUN ("listp", Flistp, Slistp, 1, 1, 0, "T if OBJECT is a list.  This includes nil.")
  (obj)
     Lisp_Object obj;
{
201
  if (CONSP (obj) || NILP (obj))
Jim Blandy's avatar
Jim Blandy committed
202 203 204 205 206 207 208 209
    return Qt;
  return Qnil;
}

DEFUN ("nlistp", Fnlistp, Snlistp, 1, 1, 0, "T if OBJECT is not a list.  Lists include nil.")
  (obj)
     Lisp_Object obj;
{
210
  if (CONSP (obj) || NILP (obj))
Jim Blandy's avatar
Jim Blandy committed
211 212 213 214 215 216 217 218
    return Qnil;
  return Qt;
}

DEFUN ("symbolp", Fsymbolp, Ssymbolp, 1, 1, 0, "T if OBJECT is a symbol.")
  (obj)
     Lisp_Object obj;
{
219
  if (SYMBOLP (obj))
Jim Blandy's avatar
Jim Blandy committed
220 221 222 223 224 225 226 227
    return Qt;
  return Qnil;
}

DEFUN ("vectorp", Fvectorp, Svectorp, 1, 1, 0, "T if OBJECT is a vector.")
  (obj)
     Lisp_Object obj;
{
228
  if (VECTORP (obj))
Jim Blandy's avatar
Jim Blandy committed
229 230 231 232 233 234 235 236
    return Qt;
  return Qnil;
}

DEFUN ("stringp", Fstringp, Sstringp, 1, 1, 0, "T if OBJECT is a string.")
  (obj)
     Lisp_Object obj;
{
237
  if (STRINGP (obj))
Jim Blandy's avatar
Jim Blandy committed
238 239 240 241 242 243 244 245
    return Qt;
  return Qnil;
}

DEFUN ("arrayp", Farrayp, Sarrayp, 1, 1, 0, "T if OBJECT is an array (string or vector).")
  (obj)
     Lisp_Object obj;
{
246
  if (VECTORP (obj) || STRINGP (obj))
Jim Blandy's avatar
Jim Blandy committed
247 248 249 250 251 252 253 254 255
    return Qt;
  return Qnil;
}

DEFUN ("sequencep", Fsequencep, Ssequencep, 1, 1, 0,
  "T if OBJECT is a sequence (list or array).")
  (obj)
     register Lisp_Object obj;
{
256
  if (CONSP (obj) || NILP (obj) || VECTORP (obj) || STRINGP (obj))
Jim Blandy's avatar
Jim Blandy committed
257 258 259 260 261 262 263 264
    return Qt;
  return Qnil;
}

DEFUN ("bufferp", Fbufferp, Sbufferp, 1, 1, 0, "T if OBJECT is an editor buffer.")
  (obj)
     Lisp_Object obj;
{
265
  if (BUFFERP (obj))
Jim Blandy's avatar
Jim Blandy committed
266 267 268 269 270 271 272 273
    return Qt;
  return Qnil;
}

DEFUN ("markerp", Fmarkerp, Smarkerp, 1, 1, 0, "T if OBJECT is a marker (editor pointer).")
  (obj)
     Lisp_Object obj;
{
274
  if (MARKERP (obj))
Jim Blandy's avatar
Jim Blandy committed
275 276 277 278 279 280 281 282
    return Qt;
  return Qnil;
}

DEFUN ("subrp", Fsubrp, Ssubrp, 1, 1, 0, "T if OBJECT is a built-in function.")
  (obj)
     Lisp_Object obj;
{
283
  if (SUBRP (obj))
Jim Blandy's avatar
Jim Blandy committed
284 285 286 287
    return Qt;
  return Qnil;
}

Jim Blandy's avatar
Jim Blandy committed
288 289
DEFUN ("byte-code-function-p", Fbyte_code_function_p, Sbyte_code_function_p,
       1, 1, 0, "T if OBJECT is a byte-compiled function object.")
Jim Blandy's avatar
Jim Blandy committed
290 291 292
  (obj)
     Lisp_Object obj;
{
293
  if (COMPILEDP (obj))
Jim Blandy's avatar
Jim Blandy committed
294 295 296 297
    return Qt;
  return Qnil;
}

298 299
DEFUN ("char-or-string-p", Fchar_or_string_p, Schar_or_string_p, 1, 1, 0,
  "T if OBJECT is a character (an integer) or a string.")
Jim Blandy's avatar
Jim Blandy committed
300 301 302
  (obj)
     register Lisp_Object obj;
{
303
  if (INTEGERP (obj) || STRINGP (obj))
Jim Blandy's avatar
Jim Blandy committed
304 305 306 307
    return Qt;
  return Qnil;
}

308
DEFUN ("integerp", Fintegerp, Sintegerp, 1, 1, 0, "T if OBJECT is an integer.")
Jim Blandy's avatar
Jim Blandy committed
309 310 311
  (obj)
     Lisp_Object obj;
{
312
  if (INTEGERP (obj))
Jim Blandy's avatar
Jim Blandy committed
313 314 315 316
    return Qt;
  return Qnil;
}

317 318
DEFUN ("integer-or-marker-p", Finteger_or_marker_p, Sinteger_or_marker_p, 1, 1, 0,
  "T if OBJECT is an integer or a marker (editor pointer).")
Jim Blandy's avatar
Jim Blandy committed
319
  (obj)
320
     register Lisp_Object obj;
Jim Blandy's avatar
Jim Blandy committed
321
{
322
  if (MARKERP (obj) || INTEGERP (obj))
Jim Blandy's avatar
Jim Blandy committed
323 324 325 326
    return Qt;
  return Qnil;
}

327 328
DEFUN ("natnump", Fnatnump, Snatnump, 1, 1, 0,
  "T if OBJECT is a nonnegative integer.")
Jim Blandy's avatar
Jim Blandy committed
329 330 331
  (obj)
     Lisp_Object obj;
{
332
  if (INTEGERP (obj) && XINT (obj) >= 0)
Jim Blandy's avatar
Jim Blandy committed
333 334 335 336 337 338 339 340 341
    return Qt;
  return Qnil;
}

DEFUN ("numberp", Fnumberp, Snumberp, 1, 1, 0,
       "T if OBJECT is a number (floating point or integer).")
  (obj)
     Lisp_Object obj;
{
Jim Blandy's avatar
Jim Blandy committed
342
  if (NUMBERP (obj))
Jim Blandy's avatar
Jim Blandy committed
343
    return Qt;
Jim Blandy's avatar
Jim Blandy committed
344 345
  else
    return Qnil;
Jim Blandy's avatar
Jim Blandy committed
346 347 348 349 350 351 352 353
}

DEFUN ("number-or-marker-p", Fnumber_or_marker_p,
       Snumber_or_marker_p, 1, 1, 0,
       "T if OBJECT is a number or a marker.")
  (obj)
     Lisp_Object obj;
{
354
  if (NUMBERP (obj) || MARKERP (obj))
Jim Blandy's avatar
Jim Blandy committed
355 356 357
    return Qt;
  return Qnil;
}
358 359 360 361 362 363 364

#ifdef LISP_FLOAT_TYPE
DEFUN ("floatp", Ffloatp, Sfloatp, 1, 1, 0,
       "T if OBJECT is a floating point number.")
  (obj)
     Lisp_Object obj;
{
365
  if (FLOATP (obj))
366 367 368
    return Qt;
  return Qnil;
}
Jim Blandy's avatar
Jim Blandy committed
369 370 371 372 373 374 375 376 377 378 379 380
#endif /* LISP_FLOAT_TYPE */

/* Extract and set components of lists */

DEFUN ("car", Fcar, Scar, 1, 1, 0,
  "Return the car of CONSCELL.  If arg is nil, return nil.\n\
Error if arg is not nil and not a cons cell.  See also `car-safe'.")
  (list)
     register Lisp_Object list;
{
  while (1)
    {
381
      if (CONSP (list))
Jim Blandy's avatar
Jim Blandy committed
382 383 384 385 386 387 388 389 390 391 392 393 394
	return XCONS (list)->car;
      else if (EQ (list, Qnil))
	return Qnil;
      else
	list = wrong_type_argument (Qlistp, list);
    }
}

DEFUN ("car-safe", Fcar_safe, Scar_safe, 1, 1, 0,
  "Return the car of OBJECT if it is a cons cell, or else nil.")
  (object)
     Lisp_Object object;
{
395
  if (CONSP (object))
Jim Blandy's avatar
Jim Blandy committed
396 397 398 399 400 401 402 403 404 405 406 407 408 409
    return XCONS (object)->car;
  else
    return Qnil;
}

DEFUN ("cdr", Fcdr, Scdr, 1, 1, 0,
  "Return the cdr of CONSCELL.  If arg is nil, return nil.\n\
Error if arg is not nil and not a cons cell.  See also `cdr-safe'.")

  (list)
     register Lisp_Object list;
{
  while (1)
    {
410
      if (CONSP (list))
Jim Blandy's avatar
Jim Blandy committed
411 412 413 414 415 416 417 418 419
	return XCONS (list)->cdr;
      else if (EQ (list, Qnil))
	return Qnil;
      else
	list = wrong_type_argument (Qlistp, list);
    }
}

DEFUN ("cdr-safe", Fcdr_safe, Scdr_safe, 1, 1, 0,
420
  "Return the cdr of OBJECT if it is a cons cell, or else nil.")
Jim Blandy's avatar
Jim Blandy committed
421 422 423
  (object)
     Lisp_Object object;
{
424
  if (CONSP (object))
Jim Blandy's avatar
Jim Blandy committed
425 426 427 428 429 430 431 432 433 434
    return XCONS (object)->cdr;
  else
    return Qnil;
}

DEFUN ("setcar", Fsetcar, Ssetcar, 2, 2, 0,
  "Set the car of CONSCELL to be NEWCAR.  Returns NEWCAR.")
  (cell, newcar)
     register Lisp_Object cell, newcar;
{
435
  if (!CONSP (cell))
Jim Blandy's avatar
Jim Blandy committed
436 437 438 439 440 441 442 443 444 445 446 447
    cell = wrong_type_argument (Qconsp, cell);

  CHECK_IMPURE (cell);
  XCONS (cell)->car = newcar;
  return newcar;
}

DEFUN ("setcdr", Fsetcdr, Ssetcdr, 2, 2, 0,
  "Set the cdr of CONSCELL to be NEWCDR.  Returns NEWCDR.")
  (cell, newcdr)
     register Lisp_Object cell, newcdr;
{
448
  if (!CONSP (cell))
Jim Blandy's avatar
Jim Blandy committed
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
    cell = wrong_type_argument (Qconsp, cell);

  CHECK_IMPURE (cell);
  XCONS (cell)->cdr = newcdr;
  return newcdr;
}

/* Extract and set components of symbols */

DEFUN ("boundp", Fboundp, Sboundp, 1, 1, 0, "T if SYMBOL's value is not void.")
  (sym)
     register Lisp_Object sym;
{
  Lisp_Object valcontents;
  CHECK_SYMBOL (sym, 0);

  valcontents = XSYMBOL (sym)->value;

#ifdef SWITCH_ENUM_BUG
  switch ((int) XTYPE (valcontents))
#else
  switch (XTYPE (valcontents))
#endif
    {
    case Lisp_Buffer_Local_Value:
    case Lisp_Some_Buffer_Local_Value:
      valcontents = swap_in_symval_forwarding (sym, valcontents);
    }

478
  return (EQ (valcontents, Qunbound) ? Qnil : Qt);
Jim Blandy's avatar
Jim Blandy committed
479 480 481 482 483 484 485
}

DEFUN ("fboundp", Ffboundp, Sfboundp, 1, 1, 0, "T if SYMBOL's function definition is not void.")
  (sym)
     register Lisp_Object sym;
{
  CHECK_SYMBOL (sym, 0);
486
  return (EQ (XSYMBOL (sym)->function, Qunbound) ? Qnil : Qt);
Jim Blandy's avatar
Jim Blandy committed
487 488 489 490 491 492 493
}

DEFUN ("makunbound", Fmakunbound, Smakunbound, 1, 1, 0, "Make SYMBOL's value be void.")
  (sym)
     register Lisp_Object sym;
{
  CHECK_SYMBOL (sym, 0);
Jim Blandy's avatar
Jim Blandy committed
494
  if (NILP (sym) || EQ (sym, Qt))
Jim Blandy's avatar
Jim Blandy committed
495 496 497 498 499 500 501 502 503 504
    return Fsignal (Qsetting_constant, Fcons (sym, Qnil));
  Fset (sym, Qunbound);
  return sym;
}

DEFUN ("fmakunbound", Ffmakunbound, Sfmakunbound, 1, 1, 0, "Make SYMBOL's function definition be void.")
  (sym)
     register Lisp_Object sym;
{
  CHECK_SYMBOL (sym, 0);
505 506
  if (NILP (sym) || EQ (sym, Qt))
    return Fsignal (Qsetting_constant, Fcons (sym, Qnil));
Jim Blandy's avatar
Jim Blandy committed
507 508 509 510 511 512
  XSYMBOL (sym)->function = Qunbound;
  return sym;
}

DEFUN ("symbol-function", Fsymbol_function, Ssymbol_function, 1, 1, 0,
  "Return SYMBOL's function definition.  Error if that is void.")
Jim Blandy's avatar
Jim Blandy committed
513 514
  (symbol)
     register Lisp_Object symbol;
Jim Blandy's avatar
Jim Blandy committed
515
{
Jim Blandy's avatar
Jim Blandy committed
516 517 518 519
  CHECK_SYMBOL (symbol, 0);
  if (EQ (XSYMBOL (symbol)->function, Qunbound))
    return Fsignal (Qvoid_function, Fcons (symbol, Qnil));
  return XSYMBOL (symbol)->function;
Jim Blandy's avatar
Jim Blandy committed
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
}

DEFUN ("symbol-plist", Fsymbol_plist, Ssymbol_plist, 1, 1, 0, "Return SYMBOL's property list.")
  (sym)
     register Lisp_Object sym;
{
  CHECK_SYMBOL (sym, 0);
  return XSYMBOL (sym)->plist;
}

DEFUN ("symbol-name", Fsymbol_name, Ssymbol_name, 1, 1, 0, "Return SYMBOL's name, a string.")
  (sym)
     register Lisp_Object sym;
{
  register Lisp_Object name;

  CHECK_SYMBOL (sym, 0);
537
  XSETSTRING (name, XSYMBOL (sym)->name);
Jim Blandy's avatar
Jim Blandy committed
538 539 540 541 542 543 544 545 546
  return name;
}

DEFUN ("fset", Ffset, Sfset, 2, 2, 0,
  "Set SYMBOL's function definition to NEWVAL, and return NEWVAL.")
  (sym, newdef)
     register Lisp_Object sym, newdef;
{
  CHECK_SYMBOL (sym, 0);
547 548
  if (NILP (sym) || EQ (sym, Qt))
    return Fsignal (Qsetting_constant, Fcons (sym, Qnil));
Jim Blandy's avatar
Jim Blandy committed
549
  if (!NILP (Vautoload_queue) && !EQ (XSYMBOL (sym)->function, Qunbound))
Jim Blandy's avatar
Jim Blandy committed
550 551 552
    Vautoload_queue = Fcons (Fcons (sym, XSYMBOL (sym)->function),
			     Vautoload_queue);
  XSYMBOL (sym)->function = newdef;
553
  /* Handle automatic advice activation */
554
  if (CONSP (XSYMBOL (sym)->plist) && !NILP (Fget (sym, Qad_advice_info)))
555
    {
556
      call2 (Qad_activate, sym, Qnil);
557 558
      newdef = XSYMBOL (sym)->function;
    }
Jim Blandy's avatar
Jim Blandy committed
559 560 561
  return newdef;
}

562 563
/* This name should be removed once it is eliminated from elsewhere.  */

564
DEFUN ("defalias", Fdefalias, Sdefalias, 2, 2, 0,
565 566 567 568 569 570 571 572 573 574
  "Set SYMBOL's function definition to NEWVAL, and return NEWVAL.\n\
Associates the function with the current load file, if any.")
  (sym, newdef)
     register Lisp_Object sym, newdef;
{
  CHECK_SYMBOL (sym, 0);
  if (!NILP (Vautoload_queue) && !EQ (XSYMBOL (sym)->function, Qunbound))
    Vautoload_queue = Fcons (Fcons (sym, XSYMBOL (sym)->function),
			     Vautoload_queue);
  XSYMBOL (sym)->function = newdef;
575 576 577 578 579 580
  /* Handle automatic advice activation */
  if (CONSP (XSYMBOL (sym)->plist) && !NILP (Fget (sym, Qad_advice_info)))
    {
      call2 (Qad_activate, sym, Qnil);
      newdef = XSYMBOL (sym)->function;
    }
581 582 583 584
  LOADHIST_ATTACH (sym);
  return newdef;
}

585 586 587 588 589 590 591 592 593 594 595
DEFUN ("define-function", Fdefine_function, Sdefine_function, 2, 2, 0,
  "Set SYMBOL's function definition to NEWVAL, and return NEWVAL.\n\
Associates the function with the current load file, if any.")
  (sym, newdef)
     register Lisp_Object sym, newdef;
{
  CHECK_SYMBOL (sym, 0);
  if (!NILP (Vautoload_queue) && !EQ (XSYMBOL (sym)->function, Qunbound))
    Vautoload_queue = Fcons (Fcons (sym, XSYMBOL (sym)->function),
			     Vautoload_queue);
  XSYMBOL (sym)->function = newdef;
596 597 598 599 600 601
  /* Handle automatic advice activation */
  if (CONSP (XSYMBOL (sym)->plist) && !NILP (Fget (sym, Qad_advice_info)))
    {
      call2 (Qad_activate, sym, Qnil);
      newdef = XSYMBOL (sym)->function;
    }
602 603 604 605
  LOADHIST_ATTACH (sym);
  return newdef;
}

Jim Blandy's avatar
Jim Blandy committed
606 607 608 609 610 611 612 613 614
DEFUN ("setplist", Fsetplist, Ssetplist, 2, 2, 0,
  "Set SYMBOL's property list to NEWVAL, and return NEWVAL.")
  (sym, newplist)
     register Lisp_Object sym, newplist;
{
  CHECK_SYMBOL (sym, 0);
  XSYMBOL (sym)->plist = newplist;
  return newplist;
}
Jim Blandy's avatar
Jim Blandy committed
615

Jim Blandy's avatar
Jim Blandy committed
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635

/* Getting and setting values of symbols */

/* 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
do_symval_forwarding (valcontents)
     register Lisp_Object valcontents;
{
  register Lisp_Object val;
#ifdef SWITCH_ENUM_BUG
  switch ((int) XTYPE (valcontents))
#else
  switch (XTYPE (valcontents))
#endif
    {
    case Lisp_Intfwd:
636
      XSETINT (val, *XINTPTR (valcontents));
Jim Blandy's avatar
Jim Blandy committed
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
      return val;

    case Lisp_Boolfwd:
      if (*XINTPTR (valcontents))
	return Qt;
      return Qnil;

    case Lisp_Objfwd:
      return *XOBJFWD (valcontents);

    case Lisp_Buffer_Objfwd:
      return *(Lisp_Object *)(XUINT (valcontents) + (char *)current_buffer);
    }
  return valcontents;
}

/* Store NEWVAL into SYM, where VALCONTENTS is found in the value cell
   of SYM.  If SYM is buffer-local, VALCONTENTS should be the
   buffer-independent contents of the value cell: forwarded just one
   step past the buffer-localness.  */

void
store_symval_forwarding (sym, valcontents, newval)
     Lisp_Object sym;
     register Lisp_Object valcontents, newval;
{
#ifdef SWITCH_ENUM_BUG
  switch ((int) XTYPE (valcontents))
#else
  switch (XTYPE (valcontents))
#endif
    {
    case Lisp_Intfwd:
      CHECK_NUMBER (newval, 1);
      *XINTPTR (valcontents) = XINT (newval);
      break;

    case Lisp_Boolfwd:
Jim Blandy's avatar
Jim Blandy committed
675
      *XINTPTR (valcontents) = NILP(newval) ? 0 : 1;
Jim Blandy's avatar
Jim Blandy committed
676 677 678 679 680 681 682
      break;

    case Lisp_Objfwd:
      *XOBJFWD (valcontents) = newval;
      break;

    case Lisp_Buffer_Objfwd:
683 684
      {
	unsigned int offset = XUINT (valcontents);
685
	Lisp_Object type;
686

687
	type = *(Lisp_Object *)(offset + (char *)&buffer_local_types);
688 689 690 691 692 693 694 695
	if (! NILP (type) && ! NILP (newval)
	    && XTYPE (newval) != XINT (type))
	  buffer_slot_type_mismatch (valcontents, newval);
	
	*(Lisp_Object *)(XUINT (valcontents) + (char *)current_buffer)
	  = newval;
	break;
      }
Jim Blandy's avatar
Jim Blandy committed
696 697 698

    default:
      valcontents = XSYMBOL (sym)->value;
699 700
      if (BUFFER_LOCAL_VALUEP (valcontents)
	  || SOME_BUFFER_LOCAL_VALUEP (valcontents))
Jim Blandy's avatar
Jim Blandy committed
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
	XCONS (XSYMBOL (sym)->value)->car = newval;
      else
	XSYMBOL (sym)->value = newval;
    }
}

/* Set up the buffer-local symbol SYM for validity in the current
   buffer.  VALCONTENTS is the contents of its value cell.
   Return the value forwarded one step past the buffer-local indicator.  */

static Lisp_Object
swap_in_symval_forwarding (sym, valcontents)
     Lisp_Object sym, valcontents;
{
  /* valcontents is a list
     (REALVALUE BUFFER CURRENT-ALIST-ELEMENT . DEFAULT-VALUE)).
     
     CURRENT-ALIST-ELEMENT is a pointer to an element of BUFFER's
719 720 721 722 723 724 725 726 727 728 729
     local_var_alist, that being the element whose car is this
     variable.  Or it can be a pointer to the
     (CURRENT-ALIST-ELEMENT . DEFAULT-VALUE), if BUFFER does not have
     an element in its alist for this variable.

     If the current buffer is not BUFFER, we store the current
     REALVALUE value into CURRENT-ALIST-ELEMENT, then find the
     appropriate alist element for the buffer now current and set up
     CURRENT-ALIST-ELEMENT.  Then we set REALVALUE out of that
     element, and store into BUFFER.

Jim Blandy's avatar
Jim Blandy committed
730 731 732 733 734
     Note that REALVALUE can be a forwarding pointer. */

  register Lisp_Object tem1;
  tem1 = XCONS (XCONS (valcontents)->cdr)->car;

Jim Blandy's avatar
Jim Blandy committed
735
  if (NILP (tem1) || current_buffer != XBUFFER (tem1))
Jim Blandy's avatar
Jim Blandy committed
736 737 738 739
    {
      tem1 = XCONS (XCONS (XCONS (valcontents)->cdr)->cdr)->car;
      Fsetcdr (tem1, do_symval_forwarding (XCONS (valcontents)->car));
      tem1 = assq_no_quit (sym, current_buffer->local_var_alist);
Jim Blandy's avatar
Jim Blandy committed
740
      if (NILP (tem1))
Jim Blandy's avatar
Jim Blandy committed
741 742
	tem1 = XCONS (XCONS (valcontents)->cdr)->cdr;
      XCONS (XCONS (XCONS (valcontents)->cdr)->cdr)->car = tem1;
743
      XSETBUFFER (XCONS (XCONS (valcontents)->cdr)->car, current_buffer);
Jim Blandy's avatar
Jim Blandy committed
744 745 746 747 748
      store_symval_forwarding (sym, XCONS (valcontents)->car, Fcdr (tem1));
    }
  return XCONS (valcontents)->car;
}

Jim Blandy's avatar
Jim Blandy committed
749 750 751 752 753
/* Find the value of a symbol, returning Qunbound if it's not bound.
   This is helpful for code which just wants to get a variable's value
   if it has one, without signalling an error.
   Note that it must not be possible to quit
   within this function.  Great care is required for this.  */
Jim Blandy's avatar
Jim Blandy committed
754

Jim Blandy's avatar
Jim Blandy committed
755 756
Lisp_Object
find_symbol_value (sym)
Jim Blandy's avatar
Jim Blandy committed
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
     Lisp_Object sym;
{
  register Lisp_Object valcontents, tem1;
  register Lisp_Object val;
  CHECK_SYMBOL (sym, 0);
  valcontents = XSYMBOL (sym)->value;

 retry:
#ifdef SWITCH_ENUM_BUG
  switch ((int) XTYPE (valcontents))
#else
  switch (XTYPE (valcontents))
#endif
    {
    case Lisp_Buffer_Local_Value:
    case Lisp_Some_Buffer_Local_Value:
      valcontents = swap_in_symval_forwarding (sym, valcontents);
      goto retry;

    case Lisp_Intfwd:
777
      XSETINT (val, *XINTPTR (valcontents));
Jim Blandy's avatar
Jim Blandy committed
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
      return val;

    case Lisp_Boolfwd:
      if (*XINTPTR (valcontents))
	return Qt;
      return Qnil;

    case Lisp_Objfwd:
      return *XOBJFWD (valcontents);

    case Lisp_Buffer_Objfwd:
      return *(Lisp_Object *)(XUINT (valcontents) + (char *)current_buffer);
    }

  return valcontents;
}

Jim Blandy's avatar
Jim Blandy committed
795 796 797 798 799
DEFUN ("symbol-value", Fsymbol_value, Ssymbol_value, 1, 1, 0,
  "Return SYMBOL's value.  Error if that is void.")
  (sym)
     Lisp_Object sym;
{
800
  Lisp_Object val;
Jim Blandy's avatar
Jim Blandy committed
801

802
  val = find_symbol_value (sym);
Jim Blandy's avatar
Jim Blandy committed
803 804 805 806 807 808
  if (EQ (val, Qunbound))
    return Fsignal (Qvoid_variable, Fcons (sym, Qnil));
  else
    return val;
}

Jim Blandy's avatar
Jim Blandy committed
809 810 811 812 813
DEFUN ("set", Fset, Sset, 2, 2, 0,
  "Set SYMBOL's value to NEWVAL, and return NEWVAL.")
  (sym, newval)
     register Lisp_Object sym, newval;
{
814
  int voide = EQ (newval, Qunbound);
Jim Blandy's avatar
Jim Blandy committed
815 816 817 818

  register Lisp_Object valcontents, tem1, current_alist_element;

  CHECK_SYMBOL (sym, 0);
Jim Blandy's avatar
Jim Blandy committed
819
  if (NILP (sym) || EQ (sym, Qt))
Jim Blandy's avatar
Jim Blandy committed
820 821 822
    return Fsignal (Qsetting_constant, Fcons (sym, Qnil));
  valcontents = XSYMBOL (sym)->value;

823
  if (BUFFER_OBJFWDP (valcontents))
Jim Blandy's avatar
Jim Blandy committed
824 825
    {
      register int idx = XUINT (valcontents);
826 827
      register int mask = XINT (*((Lisp_Object *)
				  (idx + (char *)&buffer_local_flags)));
Jim Blandy's avatar
Jim Blandy committed
828 829 830 831
      if (mask > 0)
	current_buffer->local_var_flags |= mask;
    }

832 833
  else if (BUFFER_LOCAL_VALUEP (valcontents)
	   || SOME_BUFFER_LOCAL_VALUEP (valcontents))
Jim Blandy's avatar
Jim Blandy committed
834
    {
Jim Blandy's avatar
Jim Blandy committed
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
      /* valcontents is actually a pointer to a cons heading something like:
	 (REALVALUE BUFFER CURRENT-ALIST-ELEMENT . DEFAULT-VALUE).

	 BUFFER is the last buffer for which this symbol's value was
	 made up to date.

	 CURRENT-ALIST-ELEMENT is a pointer to an element of BUFFER's
	 local_var_alist, that being the element whose car is this
	 variable.  Or it can be a pointer to the
	 (CURRENT-ALIST-ELEMENT . DEFAULT-VALUE), if BUFFER does not
	 have an element in its alist for this variable (that is, if
	 BUFFER sees the default value of this variable).

	 If we want to examine or set the value and BUFFER is current,
	 we just examine or set REALVALUE. If BUFFER is not current, we
	 store the current REALVALUE value into CURRENT-ALIST-ELEMENT,
	 then find the appropriate alist element for the buffer now
	 current and set up CURRENT-ALIST-ELEMENT.  Then we set
	 REALVALUE out of that element, and store into BUFFER.

	 If we are setting the variable and the current buffer does
	 not have an alist entry for this variable, an alist entry is
	 created.

	 Note that REALVALUE can be a forwarding pointer.  Each time
	 it is examined or set, forwarding must be done.  */

      /* What value are we caching right now?  */
      current_alist_element =
	XCONS (XCONS (XCONS (valcontents)->cdr)->cdr)->car;

      /* If the current buffer is not the buffer whose binding is
	 currently cached, or if it's a Lisp_Buffer_Local_Value and
	 we're looking at the default value, the cache is invalid; we
	 need to write it out, and find the new CURRENT-ALIST-ELEMENT.  */
      if ((current_buffer
	   != XBUFFER (XCONS (XCONS (valcontents)->cdr)->car))
872
	  || (BUFFER_LOCAL_VALUEP (valcontents)
873 874
	      && EQ (XCONS (current_alist_element)->car,
		     current_alist_element)))
Jim Blandy's avatar
Jim Blandy committed
875
	{
Jim Blandy's avatar
Jim Blandy committed
876 877 878 879 880
	  /* Write out the cached value for the old buffer; copy it
	     back to its alist element.  This works if the current
	     buffer only sees the default value, too.  */
          Fsetcdr (current_alist_element,
		   do_symval_forwarding (XCONS (valcontents)->car));
Jim Blandy's avatar
Jim Blandy committed
881

Jim Blandy's avatar
Jim Blandy committed
882
	  /* Find the new value for CURRENT-ALIST-ELEMENT.  */
Jim Blandy's avatar
Jim Blandy committed
883
	  tem1 = Fassq (sym, current_buffer->local_var_alist);
Jim Blandy's avatar
Jim Blandy committed
884
	  if (NILP (tem1))
Jim Blandy's avatar
Jim Blandy committed
885 886 887 888 889 890
	    {
	      /* This buffer still sees the default value.  */

	      /* If the variable is a Lisp_Some_Buffer_Local_Value,
		 make CURRENT-ALIST-ELEMENT point to itself,
		 indicating that we're seeing the default value.  */
891
	      if (SOME_BUFFER_LOCAL_VALUEP (valcontents))
Jim Blandy's avatar
Jim Blandy committed
892 893 894 895 896 897 898 899 900 901 902 903 904
		tem1 = XCONS (XCONS (valcontents)->cdr)->cdr;

	      /* If it's a Lisp_Buffer_Local_Value, give this buffer a
		 new assoc for a local value and set
		 CURRENT-ALIST-ELEMENT to point to that.  */
	      else
		{
		  tem1 = Fcons (sym, Fcdr (current_alist_element));
		  current_buffer->local_var_alist =
		    Fcons (tem1, current_buffer->local_var_alist);
		}
	    }
	  /* Cache the new buffer's assoc in CURRENT-ALIST-ELEMENT.  */
Jim Blandy's avatar
Jim Blandy committed
905
	  XCONS (XCONS (XCONS (valcontents)->cdr)->cdr)->car = tem1;
Jim Blandy's avatar
Jim Blandy committed
906 907

	  /* Set BUFFER, now that CURRENT-ALIST-ELEMENT is accurate.  */
908
	  XSETBUFFER (XCONS (XCONS (valcontents)->cdr)->car, current_buffer);
Jim Blandy's avatar
Jim Blandy committed
909 910 911
	}
      valcontents = XCONS (valcontents)->car;
    }
Jim Blandy's avatar
Jim Blandy committed
912

Jim Blandy's avatar
Jim Blandy committed
913 914 915 916 917 918
  /* If storing void (making the symbol void), forward only through
     buffer-local indicator, not through Lisp_Objfwd, etc.  */
  if (voide)
    store_symval_forwarding (sym, Qnil, newval);
  else
    store_symval_forwarding (sym, valcontents, newval);
Jim Blandy's avatar
Jim Blandy committed
919

Jim Blandy's avatar
Jim Blandy committed
920 921 922 923 924 925
  return newval;
}

/* Access or set a buffer-local symbol's default value.  */

/* Return the default value of SYM, but don't check for voidness.
926
   Return Qunbound if it is void.  */
Jim Blandy's avatar
Jim Blandy committed
927 928 929 930 931 932 933 934 935 936 937 938

Lisp_Object
default_value (sym)
     Lisp_Object sym;
{
  register Lisp_Object valcontents;

  CHECK_SYMBOL (sym, 0);
  valcontents = XSYMBOL (sym)->value;

  /* For a built-in buffer-local variable, get the default value
     rather than letting do_symval_forwarding get the current value.  */
939
  if (BUFFER_OBJFWDP (valcontents))
Jim Blandy's avatar
Jim Blandy committed
940 941 942
    {
      register int idx = XUINT (valcontents);

943
      if (XINT (*(Lisp_Object *) (idx + (char *) &buffer_local_flags)) != 0)
Jim Blandy's avatar
Jim Blandy committed
944 945 946 947
	return *(Lisp_Object *)(idx + (char *) &buffer_defaults);
    }

  /* Handle user-created local variables.  */
948 949
  if (BUFFER_LOCAL_VALUEP (valcontents)
      || SOME_BUFFER_LOCAL_VALUEP (valcontents))
Jim Blandy's avatar
Jim Blandy committed
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
    {
      /* If var is set up for a buffer that lacks a local value for it,
	 the current value is nominally the default value.
	 But the current value slot may be more up to date, since
	 ordinary setq stores just that slot.  So use that.  */
      Lisp_Object current_alist_element, alist_element_car;
      current_alist_element
	= XCONS (XCONS (XCONS (valcontents)->cdr)->cdr)->car;
      alist_element_car = XCONS (current_alist_element)->car;
      if (EQ (alist_element_car, current_alist_element))
	return do_symval_forwarding (XCONS (valcontents)->car);
      else
	return XCONS (XCONS (XCONS (valcontents)->cdr)->cdr)->cdr;
    }
  /* For other variables, get the current value.  */
  return do_symval_forwarding (valcontents);
}

DEFUN ("default-boundp", Fdefault_boundp, Sdefault_boundp, 1, 1, 0,
  "Return T if SYMBOL has a non-void default value.\n\
This is the value that is seen in buffers that do not have their own values\n\
for this variable.")
  (sym)
     Lisp_Object sym;
{
  register Lisp_Object value;

  value = default_value (sym);
978
  return (EQ (value, Qunbound) ? Qnil : Qt);
Jim Blandy's avatar
Jim Blandy committed
979 980 981 982 983 984 985 986 987 988 989 990 991
}

DEFUN ("default-value", Fdefault_value, Sdefault_value, 1, 1, 0,
  "Return SYMBOL's default value.\n\
This is the value that is seen in buffers that do not have their own values\n\
for this variable.  The default value is meaningful for variables with\n\
local bindings in certain buffers.")
  (sym)
     Lisp_Object sym;
{
  register Lisp_Object value;

  value = default_value (sym);
992
  if (EQ (value, Qunbound))
Jim Blandy's avatar
Jim Blandy committed
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
    return Fsignal (Qvoid_variable, Fcons (sym, Qnil));
  return value;
}

DEFUN ("set-default", Fset_default, Sset_default, 2, 2, 0,
  "Set SYMBOL's default value to VAL.  SYMBOL and VAL are evaluated.\n\
The default value is seen in buffers that do not have their own values\n\
for this variable.")
  (sym, value)
     Lisp_Object sym, value;
{
  register Lisp_Object valcontents, current_alist_element, alist_element_buffer;

  CHECK_SYMBOL (sym, 0);
  valcontents = XSYMBOL (sym)->value;

  /* Handle variables like case-fold-search that have special slots
     in the buffer.  Make them work apparently like Lisp_Buffer_Local_Value
     variables.  */
1012
  if (BUFFER_OBJFWDP (valcontents))
Jim Blandy's avatar
Jim Blandy committed
1013 1014 1015
    {
      register int idx = XUINT (valcontents);
      register struct buffer *b;
1016 1017
      register int mask = XINT (*((Lisp_Object *)
				  (idx + (char *)&buffer_local_flags)));
Jim Blandy's avatar
Jim Blandy committed
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028

      if (mask > 0)
	{
	  *(Lisp_Object *)(idx + (char *) &buffer_defaults) = value;
	  for (b = all_buffers; b; b = b->next)
	    if (!(b->local_var_flags & mask))
	      *(Lisp_Object *)(idx + (char *) b) = value;
	}
      return value;
    }

1029 1030
  if (!BUFFER_LOCAL_VALUEP (valcontents)
      && !SOME_BUFFER_LOCAL_VALUEP (valcontents))
Jim Blandy's avatar
Jim Blandy committed
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
    return Fset (sym, value);

  /* Store new value into the DEFAULT-VALUE slot */
  XCONS (XCONS (XCONS (valcontents)->cdr)->cdr)->cdr = value;

  /* If that slot is current, we must set the REALVALUE slot too */
  current_alist_element = XCONS (XCONS (XCONS (valcontents)->cdr)->cdr)->car;
  alist_element_buffer = Fcar (current_alist_element);
  if (EQ (alist_element_buffer, current_alist_element))
    store_symval_forwarding (sym, XCONS (valcontents)->car, value);

  return value;
}

DEFUN ("setq-default", Fsetq_default, Ssetq_default, 2, UNEVALLED, 0,
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
       "Set the default value of variable VAR to VALUE.\n\
VAR, the variable name, is literal (not evaluated);\n\
VALUE is an expression and it is evaluated.\n\
The default value of a variable is seen in buffers\n\
that do not have their own values for the variable.\n\
\n\
More generally, you can use multiple variables and values, as in\n\
  (setq-default SYM VALUE SYM VALUE...)\n\
This sets each SYM's default value to the corresponding VALUE.\n\
The VALUE for the Nth SYM can refer to the new default values\n\
of previous SYMs.")
Jim Blandy's avatar
Jim Blandy committed
1057 1058 1059 1060 1061 1062 1063
  (args)
     Lisp_Object args;
{
  register Lisp_Object args_left;
  register Lisp_Object val, sym;
  struct gcpro gcpro1;

Jim Blandy's avatar
Jim Blandy committed
1064
  if (NILP (args))
Jim Blandy's avatar
Jim Blandy committed
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
    return Qnil;

  args_left = args;
  GCPRO1 (args);

  do
    {
      val = Feval (Fcar (Fcdr (args_left)));
      sym = Fcar (args_left);
      Fset_default (sym, val);
      args_left = Fcdr (Fcdr (args_left));
    }
Jim Blandy's avatar
Jim Blandy committed
1077
  while (!NILP (args_left));
Jim Blandy's avatar
Jim Blandy committed
1078 1079 1080 1081 1082

  UNGCPRO;
  return val;
}

1083 1084
/* Lisp functions for creating and removing buffer-local variables.  */

Jim Blandy's avatar
Jim Blandy committed
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
DEFUN ("make-variable-buffer-local", Fmake_variable_buffer_local, Smake_variable_buffer_local,
  1, 1, "vMake Variable Buffer Local: ",
  "Make VARIABLE have a separate value for each buffer.\n\
At any time, the value for the current buffer is in effect.\n\
There is also a default value which is seen in any buffer which has not yet\n\
set its own value.\n\
Using `set' or `setq' to set the variable causes it to have a separate value\n\
for the current buffer if it was previously using the default value.\n\
The function `default-value' gets the default value and `set-default' sets it.")
  (sym)
     register Lisp_Object sym;
{
  register Lisp_Object tem, valcontents;

  CHECK_SYMBOL (sym, 0);

  if (EQ (sym, Qnil) || EQ (sym, Qt))
    error ("Symbol %s may not be buffer-local", XSYMBOL (sym)->name->data);

  valcontents = XSYMBOL (sym)->value;
1105
  if (BUFFER_LOCAL_VALUEP (valcontents) || BUFFER_OBJFWDP (valcontents))
Jim Blandy's avatar
Jim Blandy committed
1106
    return sym;
1107
  if (SOME_BUFFER_LOCAL_VALUEP (valcontents))
Jim Blandy's avatar
Jim Blandy committed
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
    {
      XSETTYPE (XSYMBOL (sym)->value, Lisp_Buffer_Local_Value);
      return sym;
    }
  if (EQ (valcontents, Qunbound))
    XSYMBOL (sym)->value = Qnil;
  tem = Fcons (Qnil, Fsymbol_value (sym));
  XCONS (tem)->car = tem;
  XSYMBOL (sym)->value = Fcons (XSYMBOL (sym)->value, Fcons (Fcurrent_buffer (), tem));
  XSETTYPE (XSYMBOL (sym)->value, Lisp_Buffer_Local_Value);
  return sym;
}

DEFUN ("make-local-variable", Fmake_local_variable, Smake_local_variable,
  1, 1, "vMake Local Variable: ",
  "Make VARIABLE have a separate value in the current buffer.\n\
Other buffers will continue to share a common default value.\n\
1125 1126
\(The buffer-local value of VARIABLE starts out as the same value\n\
VARIABLE previously had.  If VARIABLE was void, it remains void.\)\n\
Jim Blandy's avatar
Jim Blandy committed
1127 1128 1129
See also `make-variable-buffer-local'.\n\n\
If the variable is already arranged to become local when set,\n\
this function causes a local value to exist for this buffer,\n\
1130 1131 1132 1133
just as setting the variable would do.\n\
\n\
Do not use `make-local-variable' to make a hook variable buffer-local.\n\
Use `make-local-hook' instead.")
Jim Blandy's avatar
Jim Blandy committed
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
  (sym)
     register Lisp_Object sym;
{
  register Lisp_Object tem, valcontents;

  CHECK_SYMBOL (sym, 0);

  if (EQ (sym, Qnil) || EQ (sym, Qt))
    error ("Symbol %s may not be buffer-local", XSYMBOL (sym)->name->data);

  valcontents = XSYMBOL (sym)->value;
1145
  if (BUFFER_LOCAL_VALUEP (valcontents) || BUFFER_OBJFWDP (valcontents))
Jim Blandy's avatar
Jim Blandy committed
1146 1147 1148 1149 1150 1151 1152 1153 1154
    {
      tem = Fboundp (sym);
      
      /* Make sure the symbol has a local value in this particular buffer,
	 by setting it to the same value it already has.  */
      Fset (sym, (EQ (tem, Qt) ? Fsymbol_value (sym) : Qunbound));
      return sym;
    }
  /* Make sure sym is set up to hold per-buffer values */
1155
  if (!SOME_BUFFER_LOCAL_VALUEP (valcontents))
Jim Blandy's avatar
Jim Blandy committed
1156 1157 1158 1159 1160 1161 1162 1163
    {
      tem = Fcons (Qnil, do_symval_forwarding (valcontents));
      XCONS (tem)->car = tem;
      XSYMBOL (sym)->value = Fcons (XSYMBOL (sym)->value, Fcons (Qnil, tem));
      XSETTYPE (XSYMBOL (sym)->value, Lisp_Some_Buffer_Local_Value);
    }
  /* Make sure this buffer has its own value of sym */
  tem = Fassq (sym, current_buffer->local_var_alist);
Jim Blandy's avatar
Jim Blandy committed
1164
  if (NILP (tem))
Jim Blandy's avatar
Jim Blandy committed
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
    {
      current_buffer->local_var_alist
        = Fcons (Fcons (sym, XCONS (XCONS (XCONS (XSYMBOL (sym)->value)->cdr)->cdr)->cdr),
		 current_buffer->local_var_alist);

      /* Make sure symbol does not think it is set up for this buffer;
	 force it to look once again for this buffer's value */
      {
	/* This local variable avoids "expression too complex" on IBM RT.  */
	Lisp_Object xs;
    
	xs = XSYMBOL (sym)->value;
	if (current_buffer == XBUFFER (XCONS (XCONS (xs)->cdr)->car))
	  XCONS (XCONS (XSYMBOL (sym)->value)->cdr)->car = Qnil; 
      }
    }
1181 1182 1183 1184 1185 1186

  /* If the symbol forwards into a C variable, then swap in the
     variable for this buffer immediately.  If C code modifies the
     variable before we swap in, then that new value will clobber the
     default value the next time we swap.  */
  valcontents = XCONS (XSYMBOL (sym)->value)->car;
1187
  if (INTFWDP (valcontents) || BOOLFWDP (valcontents) || OBJFWDP (valcontents))
1188 1189
    swap_in_symval_forwarding (sym, XSYMBOL (sym)->value);

Jim Blandy's avatar
Jim Blandy committed
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
  return sym;
}

DEFUN ("kill-local-variable", Fkill_local_variable, Skill_local_variable,
  1, 1, "vKill Local Variable: ",
  "Make VARIABLE no longer have a separate value in the current buffer.\n\
From now on the default value will apply in this buffer.")
  (sym)
     register Lisp_Object sym;
{
  register Lisp_Object tem, valcontents;

  CHECK_SYMBOL (sym, 0);

  valcontents = XSYMBOL (sym)->value;

1206
  if (BUFFER_OBJFWDP (valcontents))
Jim Blandy's avatar
Jim Blandy committed
1207 1208
    {
      register int idx = XUINT (valcontents);
1209 1210
      register int mask = XINT (*((Lisp_Object*)
				  (idx + (char *)&buffer_local_flags)));
Jim Blandy's avatar
Jim Blandy committed
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220

      if (mask > 0)
	{
	  *(Lisp_Object *)(idx + (char *) current_buffer)
	    = *(Lisp_Object *)(idx + (char *) &buffer_defaults);
	  current_buffer->local_var_flags &= ~mask;
	}
      return sym;
    }

1221 1222
  if (!BUFFER_LOCAL_VALUEP (valcontents)
      && !SOME_BUFFER_LOCAL_VALUEP (valcontents))
Jim Blandy's avatar
Jim Blandy committed
1223 1224 1225 1226 1227
    return sym;

  /* Get rid of this buffer's alist element, if any */

  tem = Fassq (sym, current_buffer->local_var_alist);
Jim Blandy's avatar
Jim Blandy committed
1228
  if (!NILP (tem))
Jim Blandy's avatar
Jim Blandy committed
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
    current_buffer->local_var_alist = Fdelq (tem, current_buffer->local_var_alist);

  /* Make sure symbol does not think it is set up for this buffer;
     force it to look once again for this buffer's value */
  {
    Lisp_Object sv;
    sv = XSYMBOL (sym)->value;
    if (current_buffer == XBUFFER (XCONS (XCONS (sv)->cdr)->car))
      XCONS (XCONS (sv)->cdr)->car = Qnil;
  }

  return sym;
}
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258

DEFUN ("local-variable-p", Flocal_variable_p, Slocal_variable_p,
  1, 1, 0,
  "Non-nil if VARIABLE has a local binding in the current buffer.")
  (sym)
     register Lisp_Object sym;
{
  Lisp_Object valcontents;

  CHECK_SYMBOL (sym, 0);

  valcontents = XSYMBOL (sym)->value;
  return ((BUFFER_LOCAL_VALUEP (valcontents)
	   || SOME_BUFFER_LOCAL_VALUEP (valcontents)
	   || BUFFER_OBJFWDP (valcontents))
	  ? Qt : Qnil);
}
Jim Blandy's avatar
Jim Blandy committed
1259

Jim Blandy's avatar
Jim Blandy committed
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
/* Find the function at the end of a chain of symbol function indirections.  */

/* If OBJECT is a symbol, find the end of its function chain and
   return the value found there.  If OBJECT is not a symbol, just
   return it.  If there is a cycle in the function chain, signal a
   cyclic-function-indirection error.

   This is like Findirect_function, except that it doesn't signal an
   error if the chain ends up unbound.  */
Lisp_Object
1270
indirect_function (object)
1271
     register Lisp_Object object;
Jim Blandy's avatar
Jim Blandy committed
1272
{
1273
  Lisp_Object tortoise, hare;
Jim Blandy's avatar
Jim Blandy committed
1274

1275
  hare = tortoise = object;
Jim Blandy's avatar
Jim Blandy committed
1276 1277 1278

  for (;;)
    {
1279
      if (!SYMBOLP (hare) || EQ (hare, Qunbound))
Jim Blandy's avatar
Jim Blandy committed
1280 1281
	break;
      hare = XSYMBOL (hare)->function;
1282
      if (!SYMBOLP (hare) || EQ (hare, Qunbound))
Jim Blandy's avatar
Jim Blandy committed
1283 1284 1285
	break;
      hare = XSYMBOL (hare)->function;

1286
      tortoise = XSYMBOL (tortoise)->function;
Jim Blandy's avatar
Jim Blandy committed
1287

1288
      if (EQ (hare, tortoise))
Jim Blandy's avatar
Jim Blandy committed
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
	Fsignal (Qcyclic_function_indirection, Fcons (object, Qnil));
    }

  return hare;
}

DEFUN ("indirect-function", Findirect_function, Sindirect_function, 1, 1, 0,
  "Return the function at the end of OBJECT's function chain.\n\
If OBJECT is a symbol, follow all function indirections and return the final\n\
function binding.\n\
If OBJECT is not a symbol, just return it.\n\
Signal a void-function error if the final symbol is unbound.\n\
Signal a cyclic-function-indirection error if there is a loop in the\n\
function chain of symbols.")
  (object)
    register Lisp_Object object;
{
  Lisp_Object result;

  result = indirect_function (object);

  if (EQ (result, Qunbound))
    return Fsignal (Qvoid_function, Fcons (object, Qnil));
  return result;
}

Jim Blandy's avatar
Jim Blandy committed
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
/* Extract and set vector and string elements */

DEFUN ("aref", Faref, Saref, 2, 2, 0,
  "Return the element of ARRAY at index INDEX.\n\
ARRAY may be a vector or a string, or a byte-code object.  INDEX starts at 0.")
  (array, idx)
     register Lisp_Object array;
     Lisp_Object idx;
{
  register int idxval;

  CHECK_NUMBER (idx, 1);
  idxval = XINT (idx);
1328
  if (!VECTORP (array) && !STRINGP (array) && !COMPILEDP (array))
Jim Blandy's avatar
Jim Blandy committed
1329 1330 1331
    array = wrong_type_argument (Qarrayp, array);
  if (idxval < 0 || idxval >= XVECTOR (array)->size)
    args_out_of_range (array, idx);
1332
  if (STRINGP (array))
Jim Blandy's avatar
Jim Blandy committed
1333 1334
    {
      Lisp_Object val;
1335
      XSETFASTINT (val, (unsigned char) XSTRING (array)->data[idxval]);
Jim Blandy's avatar
Jim Blandy committed
1336 1337 1338 1339 1340 1341 1342
      return val;
    }
  else
    return XVECTOR (array)->contents[idxval];
}

DEFUN ("aset", Faset, Saset, 3, 3, 0,