| 1 | /* Primitive operations on Lisp data types for GNU Emacs Lisp interpreter. |
| 2 | Copyright (C) 1985-1986, 1988, 1993-1995, 1997-2012 |
| 3 | Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of GNU Emacs. |
| 6 | |
| 7 | GNU Emacs is free software: you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation, either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | GNU Emacs is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | |
| 21 | #include <config.h> |
| 22 | #include <stdio.h> |
| 23 | |
| 24 | #include <intprops.h> |
| 25 | |
| 26 | #include "lisp.h" |
| 27 | #include "puresize.h" |
| 28 | #include "character.h" |
| 29 | #include "buffer.h" |
| 30 | #include "keyboard.h" |
| 31 | #include "frame.h" |
| 32 | #include "syssignal.h" |
| 33 | #include "termhooks.h" /* For FRAME_KBOARD reference in y-or-n-p. */ |
| 34 | #include "font.h" |
| 35 | #include "keymap.h" |
| 36 | |
| 37 | #include <float.h> |
| 38 | #if (FLT_RADIX == 2 && FLT_MANT_DIG == 24 \ |
| 39 | && FLT_MIN_EXP == -125 && FLT_MAX_EXP == 128) |
| 40 | #define IEEE_FLOATING_POINT 1 |
| 41 | #else |
| 42 | #define IEEE_FLOATING_POINT 0 |
| 43 | #endif |
| 44 | |
| 45 | Lisp_Object Qnil, Qt, Qquote, Qlambda, Qunbound; |
| 46 | static Lisp_Object Qsubr; |
| 47 | Lisp_Object Qerror_conditions, Qerror_message, Qtop_level; |
| 48 | Lisp_Object Qerror, Quser_error, Qquit, Qargs_out_of_range; |
| 49 | static Lisp_Object Qwrong_type_argument; |
| 50 | Lisp_Object Qvoid_variable, Qvoid_function; |
| 51 | static Lisp_Object Qcyclic_function_indirection; |
| 52 | static Lisp_Object Qcyclic_variable_indirection; |
| 53 | Lisp_Object Qcircular_list; |
| 54 | static Lisp_Object Qsetting_constant; |
| 55 | Lisp_Object Qinvalid_read_syntax; |
| 56 | Lisp_Object Qinvalid_function, Qwrong_number_of_arguments, Qno_catch; |
| 57 | Lisp_Object Qend_of_file, Qarith_error, Qmark_inactive; |
| 58 | Lisp_Object Qbeginning_of_buffer, Qend_of_buffer, Qbuffer_read_only; |
| 59 | Lisp_Object Qtext_read_only; |
| 60 | |
| 61 | Lisp_Object Qintegerp, Qwholenump, Qsymbolp, Qlistp, Qconsp; |
| 62 | static Lisp_Object Qnatnump; |
| 63 | Lisp_Object Qstringp, Qarrayp, Qsequencep, Qbufferp; |
| 64 | Lisp_Object Qchar_or_string_p, Qmarkerp, Qinteger_or_marker_p, Qvectorp; |
| 65 | Lisp_Object Qbuffer_or_string_p; |
| 66 | static Lisp_Object Qkeywordp, Qboundp; |
| 67 | Lisp_Object Qfboundp; |
| 68 | Lisp_Object Qchar_table_p, Qvector_or_char_table_p; |
| 69 | |
| 70 | Lisp_Object Qcdr; |
| 71 | static Lisp_Object Qad_advice_info, Qad_activate_internal; |
| 72 | |
| 73 | static Lisp_Object Qdomain_error, Qsingularity_error, Qunderflow_error; |
| 74 | Lisp_Object Qrange_error, Qoverflow_error; |
| 75 | |
| 76 | Lisp_Object Qfloatp; |
| 77 | Lisp_Object Qnumberp, Qnumber_or_marker_p; |
| 78 | |
| 79 | Lisp_Object Qinteger, Qsymbol; |
| 80 | static Lisp_Object Qcons, Qfloat, Qmisc, Qstring, Qvector; |
| 81 | Lisp_Object Qwindow; |
| 82 | static Lisp_Object Qoverlay, Qwindow_configuration; |
| 83 | static Lisp_Object Qprocess, Qmarker; |
| 84 | static Lisp_Object Qcompiled_function, Qframe; |
| 85 | Lisp_Object Qbuffer; |
| 86 | static Lisp_Object Qchar_table, Qbool_vector, Qhash_table; |
| 87 | static Lisp_Object Qsubrp, Qmany, Qunevalled; |
| 88 | Lisp_Object Qfont_spec, Qfont_entity, Qfont_object; |
| 89 | static Lisp_Object Qdefun; |
| 90 | |
| 91 | Lisp_Object Qinteractive_form; |
| 92 | |
| 93 | static void swap_in_symval_forwarding (struct Lisp_Symbol *, struct Lisp_Buffer_Local_Value *); |
| 94 | |
| 95 | |
| 96 | Lisp_Object |
| 97 | wrong_type_argument (register Lisp_Object predicate, register Lisp_Object value) |
| 98 | { |
| 99 | /* If VALUE is not even a valid Lisp object, we'd want to abort here |
| 100 | where we can get a backtrace showing where it came from. We used |
| 101 | to try and do that by checking the tagbits, but nowadays all |
| 102 | tagbits are potentially valid. */ |
| 103 | /* if ((unsigned int) XTYPE (value) >= Lisp_Type_Limit) |
| 104 | * emacs_abort (); */ |
| 105 | |
| 106 | xsignal2 (Qwrong_type_argument, predicate, value); |
| 107 | } |
| 108 | |
| 109 | void |
| 110 | pure_write_error (void) |
| 111 | { |
| 112 | error ("Attempt to modify read-only object"); |
| 113 | } |
| 114 | |
| 115 | void |
| 116 | args_out_of_range (Lisp_Object a1, Lisp_Object a2) |
| 117 | { |
| 118 | xsignal2 (Qargs_out_of_range, a1, a2); |
| 119 | } |
| 120 | |
| 121 | void |
| 122 | args_out_of_range_3 (Lisp_Object a1, Lisp_Object a2, Lisp_Object a3) |
| 123 | { |
| 124 | xsignal3 (Qargs_out_of_range, a1, a2, a3); |
| 125 | } |
| 126 | |
| 127 | \f |
| 128 | /* Data type predicates. */ |
| 129 | |
| 130 | DEFUN ("eq", Feq, Seq, 2, 2, 0, |
| 131 | doc: /* Return t if the two args are the same Lisp object. */) |
| 132 | (Lisp_Object obj1, Lisp_Object obj2) |
| 133 | { |
| 134 | if (EQ (obj1, obj2)) |
| 135 | return Qt; |
| 136 | return Qnil; |
| 137 | } |
| 138 | |
| 139 | DEFUN ("null", Fnull, Snull, 1, 1, 0, |
| 140 | doc: /* Return t if OBJECT is nil. */) |
| 141 | (Lisp_Object object) |
| 142 | { |
| 143 | if (NILP (object)) |
| 144 | return Qt; |
| 145 | return Qnil; |
| 146 | } |
| 147 | |
| 148 | DEFUN ("type-of", Ftype_of, Stype_of, 1, 1, 0, |
| 149 | doc: /* Return a symbol representing the type of OBJECT. |
| 150 | The symbol returned names the object's basic type; |
| 151 | for example, (type-of 1) returns `integer'. */) |
| 152 | (Lisp_Object object) |
| 153 | { |
| 154 | switch (XTYPE (object)) |
| 155 | { |
| 156 | case_Lisp_Int: |
| 157 | return Qinteger; |
| 158 | |
| 159 | case Lisp_Symbol: |
| 160 | return Qsymbol; |
| 161 | |
| 162 | case Lisp_String: |
| 163 | return Qstring; |
| 164 | |
| 165 | case Lisp_Cons: |
| 166 | return Qcons; |
| 167 | |
| 168 | case Lisp_Misc: |
| 169 | switch (XMISCTYPE (object)) |
| 170 | { |
| 171 | case Lisp_Misc_Marker: |
| 172 | return Qmarker; |
| 173 | case Lisp_Misc_Overlay: |
| 174 | return Qoverlay; |
| 175 | case Lisp_Misc_Float: |
| 176 | return Qfloat; |
| 177 | } |
| 178 | emacs_abort (); |
| 179 | |
| 180 | case Lisp_Vectorlike: |
| 181 | if (WINDOW_CONFIGURATIONP (object)) |
| 182 | return Qwindow_configuration; |
| 183 | if (PROCESSP (object)) |
| 184 | return Qprocess; |
| 185 | if (WINDOWP (object)) |
| 186 | return Qwindow; |
| 187 | if (SUBRP (object)) |
| 188 | return Qsubr; |
| 189 | if (COMPILEDP (object)) |
| 190 | return Qcompiled_function; |
| 191 | if (BUFFERP (object)) |
| 192 | return Qbuffer; |
| 193 | if (CHAR_TABLE_P (object)) |
| 194 | return Qchar_table; |
| 195 | if (BOOL_VECTOR_P (object)) |
| 196 | return Qbool_vector; |
| 197 | if (FRAMEP (object)) |
| 198 | return Qframe; |
| 199 | if (HASH_TABLE_P (object)) |
| 200 | return Qhash_table; |
| 201 | if (FONT_SPEC_P (object)) |
| 202 | return Qfont_spec; |
| 203 | if (FONT_ENTITY_P (object)) |
| 204 | return Qfont_entity; |
| 205 | if (FONT_OBJECT_P (object)) |
| 206 | return Qfont_object; |
| 207 | return Qvector; |
| 208 | |
| 209 | case Lisp_Float: |
| 210 | return Qfloat; |
| 211 | |
| 212 | default: |
| 213 | emacs_abort (); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | DEFUN ("consp", Fconsp, Sconsp, 1, 1, 0, |
| 218 | doc: /* Return t if OBJECT is a cons cell. */) |
| 219 | (Lisp_Object object) |
| 220 | { |
| 221 | if (CONSP (object)) |
| 222 | return Qt; |
| 223 | return Qnil; |
| 224 | } |
| 225 | |
| 226 | DEFUN ("atom", Fatom, Satom, 1, 1, 0, |
| 227 | doc: /* Return t if OBJECT is not a cons cell. This includes nil. */) |
| 228 | (Lisp_Object object) |
| 229 | { |
| 230 | if (CONSP (object)) |
| 231 | return Qnil; |
| 232 | return Qt; |
| 233 | } |
| 234 | |
| 235 | DEFUN ("listp", Flistp, Slistp, 1, 1, 0, |
| 236 | doc: /* Return t if OBJECT is a list, that is, a cons cell or nil. |
| 237 | Otherwise, return nil. */) |
| 238 | (Lisp_Object object) |
| 239 | { |
| 240 | if (CONSP (object) || NILP (object)) |
| 241 | return Qt; |
| 242 | return Qnil; |
| 243 | } |
| 244 | |
| 245 | DEFUN ("nlistp", Fnlistp, Snlistp, 1, 1, 0, |
| 246 | doc: /* Return t if OBJECT is not a list. Lists include nil. */) |
| 247 | (Lisp_Object object) |
| 248 | { |
| 249 | if (CONSP (object) || NILP (object)) |
| 250 | return Qnil; |
| 251 | return Qt; |
| 252 | } |
| 253 | \f |
| 254 | DEFUN ("symbolp", Fsymbolp, Ssymbolp, 1, 1, 0, |
| 255 | doc: /* Return t if OBJECT is a symbol. */) |
| 256 | (Lisp_Object object) |
| 257 | { |
| 258 | if (SYMBOLP (object)) |
| 259 | return Qt; |
| 260 | return Qnil; |
| 261 | } |
| 262 | |
| 263 | /* Define this in C to avoid unnecessarily consing up the symbol |
| 264 | name. */ |
| 265 | DEFUN ("keywordp", Fkeywordp, Skeywordp, 1, 1, 0, |
| 266 | doc: /* Return t if OBJECT is a keyword. |
| 267 | This means that it is a symbol with a print name beginning with `:' |
| 268 | interned in the initial obarray. */) |
| 269 | (Lisp_Object object) |
| 270 | { |
| 271 | if (SYMBOLP (object) |
| 272 | && SREF (SYMBOL_NAME (object), 0) == ':' |
| 273 | && SYMBOL_INTERNED_IN_INITIAL_OBARRAY_P (object)) |
| 274 | return Qt; |
| 275 | return Qnil; |
| 276 | } |
| 277 | |
| 278 | DEFUN ("vectorp", Fvectorp, Svectorp, 1, 1, 0, |
| 279 | doc: /* Return t if OBJECT is a vector. */) |
| 280 | (Lisp_Object object) |
| 281 | { |
| 282 | if (VECTORP (object)) |
| 283 | return Qt; |
| 284 | return Qnil; |
| 285 | } |
| 286 | |
| 287 | DEFUN ("stringp", Fstringp, Sstringp, 1, 1, 0, |
| 288 | doc: /* Return t if OBJECT is a string. */) |
| 289 | (Lisp_Object object) |
| 290 | { |
| 291 | if (STRINGP (object)) |
| 292 | return Qt; |
| 293 | return Qnil; |
| 294 | } |
| 295 | |
| 296 | DEFUN ("multibyte-string-p", Fmultibyte_string_p, Smultibyte_string_p, |
| 297 | 1, 1, 0, |
| 298 | doc: /* Return t if OBJECT is a multibyte string. */) |
| 299 | (Lisp_Object object) |
| 300 | { |
| 301 | if (STRINGP (object) && STRING_MULTIBYTE (object)) |
| 302 | return Qt; |
| 303 | return Qnil; |
| 304 | } |
| 305 | |
| 306 | DEFUN ("char-table-p", Fchar_table_p, Schar_table_p, 1, 1, 0, |
| 307 | doc: /* Return t if OBJECT is a char-table. */) |
| 308 | (Lisp_Object object) |
| 309 | { |
| 310 | if (CHAR_TABLE_P (object)) |
| 311 | return Qt; |
| 312 | return Qnil; |
| 313 | } |
| 314 | |
| 315 | DEFUN ("vector-or-char-table-p", Fvector_or_char_table_p, |
| 316 | Svector_or_char_table_p, 1, 1, 0, |
| 317 | doc: /* Return t if OBJECT is a char-table or vector. */) |
| 318 | (Lisp_Object object) |
| 319 | { |
| 320 | if (VECTORP (object) || CHAR_TABLE_P (object)) |
| 321 | return Qt; |
| 322 | return Qnil; |
| 323 | } |
| 324 | |
| 325 | DEFUN ("bool-vector-p", Fbool_vector_p, Sbool_vector_p, 1, 1, 0, |
| 326 | doc: /* Return t if OBJECT is a bool-vector. */) |
| 327 | (Lisp_Object object) |
| 328 | { |
| 329 | if (BOOL_VECTOR_P (object)) |
| 330 | return Qt; |
| 331 | return Qnil; |
| 332 | } |
| 333 | |
| 334 | DEFUN ("arrayp", Farrayp, Sarrayp, 1, 1, 0, |
| 335 | doc: /* Return t if OBJECT is an array (string or vector). */) |
| 336 | (Lisp_Object object) |
| 337 | { |
| 338 | if (ARRAYP (object)) |
| 339 | return Qt; |
| 340 | return Qnil; |
| 341 | } |
| 342 | |
| 343 | DEFUN ("sequencep", Fsequencep, Ssequencep, 1, 1, 0, |
| 344 | doc: /* Return t if OBJECT is a sequence (list or array). */) |
| 345 | (register Lisp_Object object) |
| 346 | { |
| 347 | if (CONSP (object) || NILP (object) || ARRAYP (object)) |
| 348 | return Qt; |
| 349 | return Qnil; |
| 350 | } |
| 351 | |
| 352 | DEFUN ("bufferp", Fbufferp, Sbufferp, 1, 1, 0, |
| 353 | doc: /* Return t if OBJECT is an editor buffer. */) |
| 354 | (Lisp_Object object) |
| 355 | { |
| 356 | if (BUFFERP (object)) |
| 357 | return Qt; |
| 358 | return Qnil; |
| 359 | } |
| 360 | |
| 361 | DEFUN ("markerp", Fmarkerp, Smarkerp, 1, 1, 0, |
| 362 | doc: /* Return t if OBJECT is a marker (editor pointer). */) |
| 363 | (Lisp_Object object) |
| 364 | { |
| 365 | if (MARKERP (object)) |
| 366 | return Qt; |
| 367 | return Qnil; |
| 368 | } |
| 369 | |
| 370 | DEFUN ("subrp", Fsubrp, Ssubrp, 1, 1, 0, |
| 371 | doc: /* Return t if OBJECT is a built-in function. */) |
| 372 | (Lisp_Object object) |
| 373 | { |
| 374 | if (SUBRP (object)) |
| 375 | return Qt; |
| 376 | return Qnil; |
| 377 | } |
| 378 | |
| 379 | DEFUN ("byte-code-function-p", Fbyte_code_function_p, Sbyte_code_function_p, |
| 380 | 1, 1, 0, |
| 381 | doc: /* Return t if OBJECT is a byte-compiled function object. */) |
| 382 | (Lisp_Object object) |
| 383 | { |
| 384 | if (COMPILEDP (object)) |
| 385 | return Qt; |
| 386 | return Qnil; |
| 387 | } |
| 388 | |
| 389 | DEFUN ("char-or-string-p", Fchar_or_string_p, Schar_or_string_p, 1, 1, 0, |
| 390 | doc: /* Return t if OBJECT is a character or a string. */) |
| 391 | (register Lisp_Object object) |
| 392 | { |
| 393 | if (CHARACTERP (object) || STRINGP (object)) |
| 394 | return Qt; |
| 395 | return Qnil; |
| 396 | } |
| 397 | \f |
| 398 | DEFUN ("integerp", Fintegerp, Sintegerp, 1, 1, 0, |
| 399 | doc: /* Return t if OBJECT is an integer. */) |
| 400 | (Lisp_Object object) |
| 401 | { |
| 402 | if (INTEGERP (object)) |
| 403 | return Qt; |
| 404 | return Qnil; |
| 405 | } |
| 406 | |
| 407 | DEFUN ("integer-or-marker-p", Finteger_or_marker_p, Sinteger_or_marker_p, 1, 1, 0, |
| 408 | doc: /* Return t if OBJECT is an integer or a marker (editor pointer). */) |
| 409 | (register Lisp_Object object) |
| 410 | { |
| 411 | if (MARKERP (object) || INTEGERP (object)) |
| 412 | return Qt; |
| 413 | return Qnil; |
| 414 | } |
| 415 | |
| 416 | DEFUN ("natnump", Fnatnump, Snatnump, 1, 1, 0, |
| 417 | doc: /* Return t if OBJECT is a nonnegative integer. */) |
| 418 | (Lisp_Object object) |
| 419 | { |
| 420 | if (NATNUMP (object)) |
| 421 | return Qt; |
| 422 | return Qnil; |
| 423 | } |
| 424 | |
| 425 | DEFUN ("numberp", Fnumberp, Snumberp, 1, 1, 0, |
| 426 | doc: /* Return t if OBJECT is a number (floating point or integer). */) |
| 427 | (Lisp_Object object) |
| 428 | { |
| 429 | if (NUMBERP (object)) |
| 430 | return Qt; |
| 431 | else |
| 432 | return Qnil; |
| 433 | } |
| 434 | |
| 435 | DEFUN ("number-or-marker-p", Fnumber_or_marker_p, |
| 436 | Snumber_or_marker_p, 1, 1, 0, |
| 437 | doc: /* Return t if OBJECT is a number or a marker. */) |
| 438 | (Lisp_Object object) |
| 439 | { |
| 440 | if (NUMBERP (object) || MARKERP (object)) |
| 441 | return Qt; |
| 442 | return Qnil; |
| 443 | } |
| 444 | |
| 445 | DEFUN ("floatp", Ffloatp, Sfloatp, 1, 1, 0, |
| 446 | doc: /* Return t if OBJECT is a floating point number. */) |
| 447 | (Lisp_Object object) |
| 448 | { |
| 449 | if (FLOATP (object)) |
| 450 | return Qt; |
| 451 | return Qnil; |
| 452 | } |
| 453 | |
| 454 | \f |
| 455 | /* Extract and set components of lists */ |
| 456 | |
| 457 | DEFUN ("car", Fcar, Scar, 1, 1, 0, |
| 458 | doc: /* Return the car of LIST. If arg is nil, return nil. |
| 459 | Error if arg is not nil and not a cons cell. See also `car-safe'. |
| 460 | |
| 461 | See Info node `(elisp)Cons Cells' for a discussion of related basic |
| 462 | Lisp concepts such as car, cdr, cons cell and list. */) |
| 463 | (register Lisp_Object list) |
| 464 | { |
| 465 | return CAR (list); |
| 466 | } |
| 467 | |
| 468 | DEFUN ("car-safe", Fcar_safe, Scar_safe, 1, 1, 0, |
| 469 | doc: /* Return the car of OBJECT if it is a cons cell, or else nil. */) |
| 470 | (Lisp_Object object) |
| 471 | { |
| 472 | return CAR_SAFE (object); |
| 473 | } |
| 474 | |
| 475 | DEFUN ("cdr", Fcdr, Scdr, 1, 1, 0, |
| 476 | doc: /* Return the cdr of LIST. If arg is nil, return nil. |
| 477 | Error if arg is not nil and not a cons cell. See also `cdr-safe'. |
| 478 | |
| 479 | See Info node `(elisp)Cons Cells' for a discussion of related basic |
| 480 | Lisp concepts such as cdr, car, cons cell and list. */) |
| 481 | (register Lisp_Object list) |
| 482 | { |
| 483 | return CDR (list); |
| 484 | } |
| 485 | |
| 486 | DEFUN ("cdr-safe", Fcdr_safe, Scdr_safe, 1, 1, 0, |
| 487 | doc: /* Return the cdr of OBJECT if it is a cons cell, or else nil. */) |
| 488 | (Lisp_Object object) |
| 489 | { |
| 490 | return CDR_SAFE (object); |
| 491 | } |
| 492 | |
| 493 | DEFUN ("setcar", Fsetcar, Ssetcar, 2, 2, 0, |
| 494 | doc: /* Set the car of CELL to be NEWCAR. Returns NEWCAR. */) |
| 495 | (register Lisp_Object cell, Lisp_Object newcar) |
| 496 | { |
| 497 | CHECK_CONS (cell); |
| 498 | CHECK_IMPURE (cell); |
| 499 | XSETCAR (cell, newcar); |
| 500 | return newcar; |
| 501 | } |
| 502 | |
| 503 | DEFUN ("setcdr", Fsetcdr, Ssetcdr, 2, 2, 0, |
| 504 | doc: /* Set the cdr of CELL to be NEWCDR. Returns NEWCDR. */) |
| 505 | (register Lisp_Object cell, Lisp_Object newcdr) |
| 506 | { |
| 507 | CHECK_CONS (cell); |
| 508 | CHECK_IMPURE (cell); |
| 509 | XSETCDR (cell, newcdr); |
| 510 | return newcdr; |
| 511 | } |
| 512 | \f |
| 513 | /* Extract and set components of symbols. */ |
| 514 | |
| 515 | DEFUN ("boundp", Fboundp, Sboundp, 1, 1, 0, |
| 516 | doc: /* Return t if SYMBOL's value is not void. */) |
| 517 | (register Lisp_Object symbol) |
| 518 | { |
| 519 | Lisp_Object valcontents; |
| 520 | struct Lisp_Symbol *sym; |
| 521 | CHECK_SYMBOL (symbol); |
| 522 | sym = XSYMBOL (symbol); |
| 523 | |
| 524 | start: |
| 525 | switch (sym->redirect) |
| 526 | { |
| 527 | case SYMBOL_PLAINVAL: valcontents = SYMBOL_VAL (sym); break; |
| 528 | case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start; |
| 529 | case SYMBOL_LOCALIZED: |
| 530 | { |
| 531 | struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym); |
| 532 | if (blv->fwd) |
| 533 | /* In set_internal, we un-forward vars when their value is |
| 534 | set to Qunbound. */ |
| 535 | return Qt; |
| 536 | else |
| 537 | { |
| 538 | swap_in_symval_forwarding (sym, blv); |
| 539 | valcontents = blv_value (blv); |
| 540 | } |
| 541 | break; |
| 542 | } |
| 543 | case SYMBOL_FORWARDED: |
| 544 | /* In set_internal, we un-forward vars when their value is |
| 545 | set to Qunbound. */ |
| 546 | return Qt; |
| 547 | default: emacs_abort (); |
| 548 | } |
| 549 | |
| 550 | return (EQ (valcontents, Qunbound) ? Qnil : Qt); |
| 551 | } |
| 552 | |
| 553 | DEFUN ("fboundp", Ffboundp, Sfboundp, 1, 1, 0, |
| 554 | doc: /* Return t if SYMBOL's function definition is not void. */) |
| 555 | (register Lisp_Object symbol) |
| 556 | { |
| 557 | CHECK_SYMBOL (symbol); |
| 558 | return EQ (XSYMBOL (symbol)->function, Qunbound) ? Qnil : Qt; |
| 559 | } |
| 560 | |
| 561 | DEFUN ("makunbound", Fmakunbound, Smakunbound, 1, 1, 0, |
| 562 | doc: /* Make SYMBOL's value be void. |
| 563 | Return SYMBOL. */) |
| 564 | (register Lisp_Object symbol) |
| 565 | { |
| 566 | CHECK_SYMBOL (symbol); |
| 567 | if (SYMBOL_CONSTANT_P (symbol)) |
| 568 | xsignal1 (Qsetting_constant, symbol); |
| 569 | Fset (symbol, Qunbound); |
| 570 | return symbol; |
| 571 | } |
| 572 | |
| 573 | DEFUN ("fmakunbound", Ffmakunbound, Sfmakunbound, 1, 1, 0, |
| 574 | doc: /* Make SYMBOL's function definition be void. |
| 575 | Return SYMBOL. */) |
| 576 | (register Lisp_Object symbol) |
| 577 | { |
| 578 | CHECK_SYMBOL (symbol); |
| 579 | if (NILP (symbol) || EQ (symbol, Qt)) |
| 580 | xsignal1 (Qsetting_constant, symbol); |
| 581 | set_symbol_function (symbol, Qunbound); |
| 582 | return symbol; |
| 583 | } |
| 584 | |
| 585 | DEFUN ("symbol-function", Fsymbol_function, Ssymbol_function, 1, 1, 0, |
| 586 | doc: /* Return SYMBOL's function definition. Error if that is void. */) |
| 587 | (register Lisp_Object symbol) |
| 588 | { |
| 589 | CHECK_SYMBOL (symbol); |
| 590 | if (!EQ (XSYMBOL (symbol)->function, Qunbound)) |
| 591 | return XSYMBOL (symbol)->function; |
| 592 | xsignal1 (Qvoid_function, symbol); |
| 593 | } |
| 594 | |
| 595 | DEFUN ("symbol-plist", Fsymbol_plist, Ssymbol_plist, 1, 1, 0, |
| 596 | doc: /* Return SYMBOL's property list. */) |
| 597 | (register Lisp_Object symbol) |
| 598 | { |
| 599 | CHECK_SYMBOL (symbol); |
| 600 | return XSYMBOL (symbol)->plist; |
| 601 | } |
| 602 | |
| 603 | DEFUN ("symbol-name", Fsymbol_name, Ssymbol_name, 1, 1, 0, |
| 604 | doc: /* Return SYMBOL's name, a string. */) |
| 605 | (register Lisp_Object symbol) |
| 606 | { |
| 607 | register Lisp_Object name; |
| 608 | |
| 609 | CHECK_SYMBOL (symbol); |
| 610 | name = SYMBOL_NAME (symbol); |
| 611 | return name; |
| 612 | } |
| 613 | |
| 614 | DEFUN ("fset", Ffset, Sfset, 2, 2, 0, |
| 615 | doc: /* Set SYMBOL's function definition to DEFINITION, and return DEFINITION. */) |
| 616 | (register Lisp_Object symbol, Lisp_Object definition) |
| 617 | { |
| 618 | register Lisp_Object function; |
| 619 | |
| 620 | CHECK_SYMBOL (symbol); |
| 621 | if (NILP (symbol) || EQ (symbol, Qt)) |
| 622 | xsignal1 (Qsetting_constant, symbol); |
| 623 | |
| 624 | function = XSYMBOL (symbol)->function; |
| 625 | |
| 626 | if (!NILP (Vautoload_queue) && !EQ (function, Qunbound)) |
| 627 | Vautoload_queue = Fcons (Fcons (symbol, function), Vautoload_queue); |
| 628 | |
| 629 | if (CONSP (function) && EQ (XCAR (function), Qautoload)) |
| 630 | Fput (symbol, Qautoload, XCDR (function)); |
| 631 | |
| 632 | set_symbol_function (symbol, definition); |
| 633 | /* Handle automatic advice activation. */ |
| 634 | if (CONSP (XSYMBOL (symbol)->plist) |
| 635 | && !NILP (Fget (symbol, Qad_advice_info))) |
| 636 | { |
| 637 | call2 (Qad_activate_internal, symbol, Qnil); |
| 638 | definition = XSYMBOL (symbol)->function; |
| 639 | } |
| 640 | return definition; |
| 641 | } |
| 642 | |
| 643 | DEFUN ("defalias", Fdefalias, Sdefalias, 2, 3, 0, |
| 644 | doc: /* Set SYMBOL's function definition to DEFINITION. |
| 645 | Associates the function with the current load file, if any. |
| 646 | The optional third argument DOCSTRING specifies the documentation string |
| 647 | for SYMBOL; if it is omitted or nil, SYMBOL uses the documentation string |
| 648 | determined by DEFINITION. |
| 649 | The return value is undefined. */) |
| 650 | (register Lisp_Object symbol, Lisp_Object definition, Lisp_Object docstring) |
| 651 | { |
| 652 | CHECK_SYMBOL (symbol); |
| 653 | if (CONSP (XSYMBOL (symbol)->function) |
| 654 | && EQ (XCAR (XSYMBOL (symbol)->function), Qautoload)) |
| 655 | LOADHIST_ATTACH (Fcons (Qt, symbol)); |
| 656 | if (!NILP (Vpurify_flag) |
| 657 | /* If `definition' is a keymap, immutable (and copying) is wrong. */ |
| 658 | && !KEYMAPP (definition)) |
| 659 | definition = Fpurecopy (definition); |
| 660 | definition = Ffset (symbol, definition); |
| 661 | LOADHIST_ATTACH (Fcons (Qdefun, symbol)); |
| 662 | if (!NILP (docstring)) |
| 663 | Fput (symbol, Qfunction_documentation, docstring); |
| 664 | /* We used to return `definition', but now that `defun' and `defmacro' expand |
| 665 | to a call to `defalias', we return `symbol' for backward compatibility |
| 666 | (bug#11686). */ |
| 667 | return symbol; |
| 668 | } |
| 669 | |
| 670 | DEFUN ("setplist", Fsetplist, Ssetplist, 2, 2, 0, |
| 671 | doc: /* Set SYMBOL's property list to NEWPLIST, and return NEWPLIST. */) |
| 672 | (register Lisp_Object symbol, Lisp_Object newplist) |
| 673 | { |
| 674 | CHECK_SYMBOL (symbol); |
| 675 | set_symbol_plist (symbol, newplist); |
| 676 | return newplist; |
| 677 | } |
| 678 | |
| 679 | DEFUN ("subr-arity", Fsubr_arity, Ssubr_arity, 1, 1, 0, |
| 680 | doc: /* Return minimum and maximum number of args allowed for SUBR. |
| 681 | SUBR must be a built-in function. |
| 682 | The returned value is a pair (MIN . MAX). MIN is the minimum number |
| 683 | of args. MAX is the maximum number or the symbol `many', for a |
| 684 | function with `&rest' args, or `unevalled' for a special form. */) |
| 685 | (Lisp_Object subr) |
| 686 | { |
| 687 | short minargs, maxargs; |
| 688 | CHECK_SUBR (subr); |
| 689 | minargs = XSUBR (subr)->min_args; |
| 690 | maxargs = XSUBR (subr)->max_args; |
| 691 | if (maxargs == MANY) |
| 692 | return Fcons (make_number (minargs), Qmany); |
| 693 | else if (maxargs == UNEVALLED) |
| 694 | return Fcons (make_number (minargs), Qunevalled); |
| 695 | else |
| 696 | return Fcons (make_number (minargs), make_number (maxargs)); |
| 697 | } |
| 698 | |
| 699 | DEFUN ("subr-name", Fsubr_name, Ssubr_name, 1, 1, 0, |
| 700 | doc: /* Return name of subroutine SUBR. |
| 701 | SUBR must be a built-in function. */) |
| 702 | (Lisp_Object subr) |
| 703 | { |
| 704 | const char *name; |
| 705 | CHECK_SUBR (subr); |
| 706 | name = XSUBR (subr)->symbol_name; |
| 707 | return build_string (name); |
| 708 | } |
| 709 | |
| 710 | DEFUN ("interactive-form", Finteractive_form, Sinteractive_form, 1, 1, 0, |
| 711 | doc: /* Return the interactive form of CMD or nil if none. |
| 712 | If CMD is not a command, the return value is nil. |
| 713 | Value, if non-nil, is a list \(interactive SPEC). */) |
| 714 | (Lisp_Object cmd) |
| 715 | { |
| 716 | Lisp_Object fun = indirect_function (cmd); /* Check cycles. */ |
| 717 | |
| 718 | if (NILP (fun) || EQ (fun, Qunbound)) |
| 719 | return Qnil; |
| 720 | |
| 721 | /* Use an `interactive-form' property if present, analogous to the |
| 722 | function-documentation property. */ |
| 723 | fun = cmd; |
| 724 | while (SYMBOLP (fun)) |
| 725 | { |
| 726 | Lisp_Object tmp = Fget (fun, Qinteractive_form); |
| 727 | if (!NILP (tmp)) |
| 728 | return tmp; |
| 729 | else |
| 730 | fun = Fsymbol_function (fun); |
| 731 | } |
| 732 | |
| 733 | if (SUBRP (fun)) |
| 734 | { |
| 735 | const char *spec = XSUBR (fun)->intspec; |
| 736 | if (spec) |
| 737 | return list2 (Qinteractive, |
| 738 | (*spec != '(') ? build_string (spec) : |
| 739 | Fcar (Fread_from_string (build_string (spec), Qnil, Qnil))); |
| 740 | } |
| 741 | else if (COMPILEDP (fun)) |
| 742 | { |
| 743 | if ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE) |
| 744 | return list2 (Qinteractive, AREF (fun, COMPILED_INTERACTIVE)); |
| 745 | } |
| 746 | else if (CONSP (fun)) |
| 747 | { |
| 748 | Lisp_Object funcar = XCAR (fun); |
| 749 | if (EQ (funcar, Qclosure)) |
| 750 | return Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun)))); |
| 751 | else if (EQ (funcar, Qlambda)) |
| 752 | return Fassq (Qinteractive, Fcdr (XCDR (fun))); |
| 753 | else if (EQ (funcar, Qautoload)) |
| 754 | { |
| 755 | struct gcpro gcpro1; |
| 756 | GCPRO1 (cmd); |
| 757 | Fautoload_do_load (fun, cmd, Qnil); |
| 758 | UNGCPRO; |
| 759 | return Finteractive_form (cmd); |
| 760 | } |
| 761 | } |
| 762 | return Qnil; |
| 763 | } |
| 764 | |
| 765 | \f |
| 766 | /*********************************************************************** |
| 767 | Getting and Setting Values of Symbols |
| 768 | ***********************************************************************/ |
| 769 | |
| 770 | /* Return the symbol holding SYMBOL's value. Signal |
| 771 | `cyclic-variable-indirection' if SYMBOL's chain of variable |
| 772 | indirections contains a loop. */ |
| 773 | |
| 774 | struct Lisp_Symbol * |
| 775 | indirect_variable (struct Lisp_Symbol *symbol) |
| 776 | { |
| 777 | struct Lisp_Symbol *tortoise, *hare; |
| 778 | |
| 779 | hare = tortoise = symbol; |
| 780 | |
| 781 | while (hare->redirect == SYMBOL_VARALIAS) |
| 782 | { |
| 783 | hare = SYMBOL_ALIAS (hare); |
| 784 | if (hare->redirect != SYMBOL_VARALIAS) |
| 785 | break; |
| 786 | |
| 787 | hare = SYMBOL_ALIAS (hare); |
| 788 | tortoise = SYMBOL_ALIAS (tortoise); |
| 789 | |
| 790 | if (hare == tortoise) |
| 791 | { |
| 792 | Lisp_Object tem; |
| 793 | XSETSYMBOL (tem, symbol); |
| 794 | xsignal1 (Qcyclic_variable_indirection, tem); |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | return hare; |
| 799 | } |
| 800 | |
| 801 | |
| 802 | DEFUN ("indirect-variable", Findirect_variable, Sindirect_variable, 1, 1, 0, |
| 803 | doc: /* Return the variable at the end of OBJECT's variable chain. |
| 804 | If OBJECT is a symbol, follow its variable indirections (if any), and |
| 805 | return the variable at the end of the chain of aliases. See Info node |
| 806 | `(elisp)Variable Aliases'. |
| 807 | |
| 808 | If OBJECT is not a symbol, just return it. If there is a loop in the |
| 809 | chain of aliases, signal a `cyclic-variable-indirection' error. */) |
| 810 | (Lisp_Object object) |
| 811 | { |
| 812 | if (SYMBOLP (object)) |
| 813 | { |
| 814 | struct Lisp_Symbol *sym = indirect_variable (XSYMBOL (object)); |
| 815 | XSETSYMBOL (object, sym); |
| 816 | } |
| 817 | return object; |
| 818 | } |
| 819 | |
| 820 | |
| 821 | /* Given the raw contents of a symbol value cell, |
| 822 | return the Lisp value of the symbol. |
| 823 | This does not handle buffer-local variables; use |
| 824 | swap_in_symval_forwarding for that. */ |
| 825 | |
| 826 | Lisp_Object |
| 827 | do_symval_forwarding (register union Lisp_Fwd *valcontents) |
| 828 | { |
| 829 | register Lisp_Object val; |
| 830 | switch (XFWDTYPE (valcontents)) |
| 831 | { |
| 832 | case Lisp_Fwd_Int: |
| 833 | XSETINT (val, *XINTFWD (valcontents)->intvar); |
| 834 | return val; |
| 835 | |
| 836 | case Lisp_Fwd_Bool: |
| 837 | return (*XBOOLFWD (valcontents)->boolvar ? Qt : Qnil); |
| 838 | |
| 839 | case Lisp_Fwd_Obj: |
| 840 | return *XOBJFWD (valcontents)->objvar; |
| 841 | |
| 842 | case Lisp_Fwd_Buffer_Obj: |
| 843 | return per_buffer_value (current_buffer, |
| 844 | XBUFFER_OBJFWD (valcontents)->offset); |
| 845 | |
| 846 | case Lisp_Fwd_Kboard_Obj: |
| 847 | /* We used to simply use current_kboard here, but from Lisp |
| 848 | code, its value is often unexpected. It seems nicer to |
| 849 | allow constructions like this to work as intuitively expected: |
| 850 | |
| 851 | (with-selected-frame frame |
| 852 | (define-key local-function-map "\eOP" [f1])) |
| 853 | |
| 854 | On the other hand, this affects the semantics of |
| 855 | last-command and real-last-command, and people may rely on |
| 856 | that. I took a quick look at the Lisp codebase, and I |
| 857 | don't think anything will break. --lorentey */ |
| 858 | return *(Lisp_Object *)(XKBOARD_OBJFWD (valcontents)->offset |
| 859 | + (char *)FRAME_KBOARD (SELECTED_FRAME ())); |
| 860 | default: emacs_abort (); |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | /* Store NEWVAL into SYMBOL, where VALCONTENTS is found in the value cell |
| 865 | of SYMBOL. If SYMBOL is buffer-local, VALCONTENTS should be the |
| 866 | buffer-independent contents of the value cell: forwarded just one |
| 867 | step past the buffer-localness. |
| 868 | |
| 869 | BUF non-zero means set the value in buffer BUF instead of the |
| 870 | current buffer. This only plays a role for per-buffer variables. */ |
| 871 | |
| 872 | static void |
| 873 | store_symval_forwarding (union Lisp_Fwd *valcontents, register Lisp_Object newval, struct buffer *buf) |
| 874 | { |
| 875 | switch (XFWDTYPE (valcontents)) |
| 876 | { |
| 877 | case Lisp_Fwd_Int: |
| 878 | CHECK_NUMBER (newval); |
| 879 | *XINTFWD (valcontents)->intvar = XINT (newval); |
| 880 | break; |
| 881 | |
| 882 | case Lisp_Fwd_Bool: |
| 883 | *XBOOLFWD (valcontents)->boolvar = !NILP (newval); |
| 884 | break; |
| 885 | |
| 886 | case Lisp_Fwd_Obj: |
| 887 | *XOBJFWD (valcontents)->objvar = newval; |
| 888 | |
| 889 | /* If this variable is a default for something stored |
| 890 | in the buffer itself, such as default-fill-column, |
| 891 | find the buffers that don't have local values for it |
| 892 | and update them. */ |
| 893 | if (XOBJFWD (valcontents)->objvar > (Lisp_Object *) &buffer_defaults |
| 894 | && XOBJFWD (valcontents)->objvar < (Lisp_Object *) (&buffer_defaults + 1)) |
| 895 | { |
| 896 | int offset = ((char *) XOBJFWD (valcontents)->objvar |
| 897 | - (char *) &buffer_defaults); |
| 898 | int idx = PER_BUFFER_IDX (offset); |
| 899 | |
| 900 | Lisp_Object tail; |
| 901 | |
| 902 | if (idx <= 0) |
| 903 | break; |
| 904 | |
| 905 | for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail)) |
| 906 | { |
| 907 | Lisp_Object lbuf; |
| 908 | struct buffer *b; |
| 909 | |
| 910 | lbuf = Fcdr (XCAR (tail)); |
| 911 | if (!BUFFERP (lbuf)) continue; |
| 912 | b = XBUFFER (lbuf); |
| 913 | |
| 914 | if (! PER_BUFFER_VALUE_P (b, idx)) |
| 915 | set_per_buffer_value (b, offset, newval); |
| 916 | } |
| 917 | } |
| 918 | break; |
| 919 | |
| 920 | case Lisp_Fwd_Buffer_Obj: |
| 921 | { |
| 922 | int offset = XBUFFER_OBJFWD (valcontents)->offset; |
| 923 | Lisp_Object type = XBUFFER_OBJFWD (valcontents)->slottype; |
| 924 | |
| 925 | if (!(NILP (type) || NILP (newval) |
| 926 | || (XINT (type) == Lisp_Int0 |
| 927 | ? INTEGERP (newval) |
| 928 | : XTYPE (newval) == XINT (type)))) |
| 929 | buffer_slot_type_mismatch (newval, XINT (type)); |
| 930 | |
| 931 | if (buf == NULL) |
| 932 | buf = current_buffer; |
| 933 | set_per_buffer_value (buf, offset, newval); |
| 934 | } |
| 935 | break; |
| 936 | |
| 937 | case Lisp_Fwd_Kboard_Obj: |
| 938 | { |
| 939 | char *base = (char *) FRAME_KBOARD (SELECTED_FRAME ()); |
| 940 | char *p = base + XKBOARD_OBJFWD (valcontents)->offset; |
| 941 | *(Lisp_Object *) p = newval; |
| 942 | } |
| 943 | break; |
| 944 | |
| 945 | default: |
| 946 | emacs_abort (); /* goto def; */ |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | /* Set up SYMBOL to refer to its global binding. This makes it safe |
| 951 | to alter the status of other bindings. BEWARE: this may be called |
| 952 | during the mark phase of GC, where we assume that Lisp_Object slots |
| 953 | of BLV are marked after this function has changed them. */ |
| 954 | |
| 955 | void |
| 956 | swap_in_global_binding (struct Lisp_Symbol *symbol) |
| 957 | { |
| 958 | struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (symbol); |
| 959 | |
| 960 | /* Unload the previously loaded binding. */ |
| 961 | if (blv->fwd) |
| 962 | set_blv_value (blv, do_symval_forwarding (blv->fwd)); |
| 963 | |
| 964 | /* Select the global binding in the symbol. */ |
| 965 | set_blv_valcell (blv, blv->defcell); |
| 966 | if (blv->fwd) |
| 967 | store_symval_forwarding (blv->fwd, XCDR (blv->defcell), NULL); |
| 968 | |
| 969 | /* Indicate that the global binding is set up now. */ |
| 970 | set_blv_where (blv, Qnil); |
| 971 | set_blv_found (blv, 0); |
| 972 | } |
| 973 | |
| 974 | /* Set up the buffer-local symbol SYMBOL for validity in the current buffer. |
| 975 | VALCONTENTS is the contents of its value cell, |
| 976 | which points to a struct Lisp_Buffer_Local_Value. |
| 977 | |
| 978 | Return the value forwarded one step past the buffer-local stage. |
| 979 | This could be another forwarding pointer. */ |
| 980 | |
| 981 | static void |
| 982 | swap_in_symval_forwarding (struct Lisp_Symbol *symbol, struct Lisp_Buffer_Local_Value *blv) |
| 983 | { |
| 984 | register Lisp_Object tem1; |
| 985 | |
| 986 | eassert (blv == SYMBOL_BLV (symbol)); |
| 987 | |
| 988 | tem1 = blv->where; |
| 989 | |
| 990 | if (NILP (tem1) |
| 991 | || (blv->frame_local |
| 992 | ? !EQ (selected_frame, tem1) |
| 993 | : current_buffer != XBUFFER (tem1))) |
| 994 | { |
| 995 | |
| 996 | /* Unload the previously loaded binding. */ |
| 997 | tem1 = blv->valcell; |
| 998 | if (blv->fwd) |
| 999 | set_blv_value (blv, do_symval_forwarding (blv->fwd)); |
| 1000 | /* Choose the new binding. */ |
| 1001 | { |
| 1002 | Lisp_Object var; |
| 1003 | XSETSYMBOL (var, symbol); |
| 1004 | if (blv->frame_local) |
| 1005 | { |
| 1006 | tem1 = assq_no_quit (var, XFRAME (selected_frame)->param_alist); |
| 1007 | set_blv_where (blv, selected_frame); |
| 1008 | } |
| 1009 | else |
| 1010 | { |
| 1011 | tem1 = assq_no_quit (var, BVAR (current_buffer, local_var_alist)); |
| 1012 | set_blv_where (blv, Fcurrent_buffer ()); |
| 1013 | } |
| 1014 | } |
| 1015 | if (!(blv->found = !NILP (tem1))) |
| 1016 | tem1 = blv->defcell; |
| 1017 | |
| 1018 | /* Load the new binding. */ |
| 1019 | set_blv_valcell (blv, tem1); |
| 1020 | if (blv->fwd) |
| 1021 | store_symval_forwarding (blv->fwd, blv_value (blv), NULL); |
| 1022 | } |
| 1023 | } |
| 1024 | \f |
| 1025 | /* Find the value of a symbol, returning Qunbound if it's not bound. |
| 1026 | This is helpful for code which just wants to get a variable's value |
| 1027 | if it has one, without signaling an error. |
| 1028 | Note that it must not be possible to quit |
| 1029 | within this function. Great care is required for this. */ |
| 1030 | |
| 1031 | Lisp_Object |
| 1032 | find_symbol_value (Lisp_Object symbol) |
| 1033 | { |
| 1034 | struct Lisp_Symbol *sym; |
| 1035 | |
| 1036 | CHECK_SYMBOL (symbol); |
| 1037 | sym = XSYMBOL (symbol); |
| 1038 | |
| 1039 | start: |
| 1040 | switch (sym->redirect) |
| 1041 | { |
| 1042 | case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start; |
| 1043 | case SYMBOL_PLAINVAL: return SYMBOL_VAL (sym); |
| 1044 | case SYMBOL_LOCALIZED: |
| 1045 | { |
| 1046 | struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym); |
| 1047 | swap_in_symval_forwarding (sym, blv); |
| 1048 | return blv->fwd ? do_symval_forwarding (blv->fwd) : blv_value (blv); |
| 1049 | } |
| 1050 | /* FALLTHROUGH */ |
| 1051 | case SYMBOL_FORWARDED: |
| 1052 | return do_symval_forwarding (SYMBOL_FWD (sym)); |
| 1053 | default: emacs_abort (); |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | DEFUN ("symbol-value", Fsymbol_value, Ssymbol_value, 1, 1, 0, |
| 1058 | doc: /* Return SYMBOL's value. Error if that is void. */) |
| 1059 | (Lisp_Object symbol) |
| 1060 | { |
| 1061 | Lisp_Object val; |
| 1062 | |
| 1063 | val = find_symbol_value (symbol); |
| 1064 | if (!EQ (val, Qunbound)) |
| 1065 | return val; |
| 1066 | |
| 1067 | xsignal1 (Qvoid_variable, symbol); |
| 1068 | } |
| 1069 | |
| 1070 | DEFUN ("set", Fset, Sset, 2, 2, 0, |
| 1071 | doc: /* Set SYMBOL's value to NEWVAL, and return NEWVAL. */) |
| 1072 | (register Lisp_Object symbol, Lisp_Object newval) |
| 1073 | { |
| 1074 | set_internal (symbol, newval, Qnil, 0); |
| 1075 | return newval; |
| 1076 | } |
| 1077 | |
| 1078 | /* Return true if SYMBOL currently has a let-binding |
| 1079 | which was made in the buffer that is now current. */ |
| 1080 | |
| 1081 | static bool |
| 1082 | let_shadows_buffer_binding_p (struct Lisp_Symbol *symbol) |
| 1083 | { |
| 1084 | struct specbinding *p; |
| 1085 | |
| 1086 | for (p = specpdl_ptr; p > specpdl; ) |
| 1087 | if ((--p)->func == NULL |
| 1088 | && CONSP (p->symbol)) |
| 1089 | { |
| 1090 | struct Lisp_Symbol *let_bound_symbol = XSYMBOL (XCAR (p->symbol)); |
| 1091 | eassert (let_bound_symbol->redirect != SYMBOL_VARALIAS); |
| 1092 | if (symbol == let_bound_symbol |
| 1093 | && XBUFFER (XCDR (XCDR (p->symbol))) == current_buffer) |
| 1094 | return 1; |
| 1095 | } |
| 1096 | |
| 1097 | return 0; |
| 1098 | } |
| 1099 | |
| 1100 | static bool |
| 1101 | let_shadows_global_binding_p (Lisp_Object symbol) |
| 1102 | { |
| 1103 | struct specbinding *p; |
| 1104 | |
| 1105 | for (p = specpdl_ptr; p > specpdl; ) |
| 1106 | if ((--p)->func == NULL && EQ (p->symbol, symbol)) |
| 1107 | return 1; |
| 1108 | |
| 1109 | return 0; |
| 1110 | } |
| 1111 | |
| 1112 | /* Store the value NEWVAL into SYMBOL. |
| 1113 | If buffer/frame-locality is an issue, WHERE specifies which context to use. |
| 1114 | (nil stands for the current buffer/frame). |
| 1115 | |
| 1116 | If BINDFLAG is false, then if this symbol is supposed to become |
| 1117 | local in every buffer where it is set, then we make it local. |
| 1118 | If BINDFLAG is true, we don't do that. */ |
| 1119 | |
| 1120 | void |
| 1121 | set_internal (Lisp_Object symbol, Lisp_Object newval, Lisp_Object where, |
| 1122 | bool bindflag) |
| 1123 | { |
| 1124 | bool voide = EQ (newval, Qunbound); |
| 1125 | struct Lisp_Symbol *sym; |
| 1126 | Lisp_Object tem1; |
| 1127 | |
| 1128 | /* If restoring in a dead buffer, do nothing. */ |
| 1129 | /* if (BUFFERP (where) && NILP (XBUFFER (where)->name)) |
| 1130 | return; */ |
| 1131 | |
| 1132 | CHECK_SYMBOL (symbol); |
| 1133 | if (SYMBOL_CONSTANT_P (symbol)) |
| 1134 | { |
| 1135 | if (NILP (Fkeywordp (symbol)) |
| 1136 | || !EQ (newval, Fsymbol_value (symbol))) |
| 1137 | xsignal1 (Qsetting_constant, symbol); |
| 1138 | else |
| 1139 | /* Allow setting keywords to their own value. */ |
| 1140 | return; |
| 1141 | } |
| 1142 | |
| 1143 | sym = XSYMBOL (symbol); |
| 1144 | |
| 1145 | start: |
| 1146 | switch (sym->redirect) |
| 1147 | { |
| 1148 | case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start; |
| 1149 | case SYMBOL_PLAINVAL: SET_SYMBOL_VAL (sym , newval); return; |
| 1150 | case SYMBOL_LOCALIZED: |
| 1151 | { |
| 1152 | struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym); |
| 1153 | if (NILP (where)) |
| 1154 | { |
| 1155 | if (blv->frame_local) |
| 1156 | where = selected_frame; |
| 1157 | else |
| 1158 | XSETBUFFER (where, current_buffer); |
| 1159 | } |
| 1160 | /* If the current buffer is not the buffer whose binding is |
| 1161 | loaded, or if there may be frame-local bindings and the frame |
| 1162 | isn't the right one, or if it's a Lisp_Buffer_Local_Value and |
| 1163 | the default binding is loaded, the loaded binding may be the |
| 1164 | wrong one. */ |
| 1165 | if (!EQ (blv->where, where) |
| 1166 | /* Also unload a global binding (if the var is local_if_set). */ |
| 1167 | || (EQ (blv->valcell, blv->defcell))) |
| 1168 | { |
| 1169 | /* The currently loaded binding is not necessarily valid. |
| 1170 | We need to unload it, and choose a new binding. */ |
| 1171 | |
| 1172 | /* Write out `realvalue' to the old loaded binding. */ |
| 1173 | if (blv->fwd) |
| 1174 | set_blv_value (blv, do_symval_forwarding (blv->fwd)); |
| 1175 | |
| 1176 | /* Find the new binding. */ |
| 1177 | XSETSYMBOL (symbol, sym); /* May have changed via aliasing. */ |
| 1178 | tem1 = Fassq (symbol, |
| 1179 | (blv->frame_local |
| 1180 | ? XFRAME (where)->param_alist |
| 1181 | : BVAR (XBUFFER (where), local_var_alist))); |
| 1182 | set_blv_where (blv, where); |
| 1183 | blv->found = 1; |
| 1184 | |
| 1185 | if (NILP (tem1)) |
| 1186 | { |
| 1187 | /* This buffer still sees the default value. */ |
| 1188 | |
| 1189 | /* If the variable is a Lisp_Some_Buffer_Local_Value, |
| 1190 | or if this is `let' rather than `set', |
| 1191 | make CURRENT-ALIST-ELEMENT point to itself, |
| 1192 | indicating that we're seeing the default value. |
| 1193 | Likewise if the variable has been let-bound |
| 1194 | in the current buffer. */ |
| 1195 | if (bindflag || !blv->local_if_set |
| 1196 | || let_shadows_buffer_binding_p (sym)) |
| 1197 | { |
| 1198 | blv->found = 0; |
| 1199 | tem1 = blv->defcell; |
| 1200 | } |
| 1201 | /* If it's a local_if_set, being set not bound, |
| 1202 | and we're not within a let that was made for this buffer, |
| 1203 | create a new buffer-local binding for the variable. |
| 1204 | That means, give this buffer a new assoc for a local value |
| 1205 | and load that binding. */ |
| 1206 | else |
| 1207 | { |
| 1208 | /* local_if_set is only supported for buffer-local |
| 1209 | bindings, not for frame-local bindings. */ |
| 1210 | eassert (!blv->frame_local); |
| 1211 | tem1 = Fcons (symbol, XCDR (blv->defcell)); |
| 1212 | bset_local_var_alist |
| 1213 | (XBUFFER (where), |
| 1214 | Fcons (tem1, BVAR (XBUFFER (where), local_var_alist))); |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | /* Record which binding is now loaded. */ |
| 1219 | set_blv_valcell (blv, tem1); |
| 1220 | } |
| 1221 | |
| 1222 | /* Store the new value in the cons cell. */ |
| 1223 | set_blv_value (blv, newval); |
| 1224 | |
| 1225 | if (blv->fwd) |
| 1226 | { |
| 1227 | if (voide) |
| 1228 | /* If storing void (making the symbol void), forward only through |
| 1229 | buffer-local indicator, not through Lisp_Objfwd, etc. */ |
| 1230 | blv->fwd = NULL; |
| 1231 | else |
| 1232 | store_symval_forwarding (blv->fwd, newval, |
| 1233 | BUFFERP (where) |
| 1234 | ? XBUFFER (where) : current_buffer); |
| 1235 | } |
| 1236 | break; |
| 1237 | } |
| 1238 | case SYMBOL_FORWARDED: |
| 1239 | { |
| 1240 | struct buffer *buf |
| 1241 | = BUFFERP (where) ? XBUFFER (where) : current_buffer; |
| 1242 | union Lisp_Fwd *innercontents = SYMBOL_FWD (sym); |
| 1243 | if (BUFFER_OBJFWDP (innercontents)) |
| 1244 | { |
| 1245 | int offset = XBUFFER_OBJFWD (innercontents)->offset; |
| 1246 | int idx = PER_BUFFER_IDX (offset); |
| 1247 | if (idx > 0 |
| 1248 | && !bindflag |
| 1249 | && !let_shadows_buffer_binding_p (sym)) |
| 1250 | SET_PER_BUFFER_VALUE_P (buf, idx, 1); |
| 1251 | } |
| 1252 | |
| 1253 | if (voide) |
| 1254 | { /* If storing void (making the symbol void), forward only through |
| 1255 | buffer-local indicator, not through Lisp_Objfwd, etc. */ |
| 1256 | sym->redirect = SYMBOL_PLAINVAL; |
| 1257 | SET_SYMBOL_VAL (sym, newval); |
| 1258 | } |
| 1259 | else |
| 1260 | store_symval_forwarding (/* sym, */ innercontents, newval, buf); |
| 1261 | break; |
| 1262 | } |
| 1263 | default: emacs_abort (); |
| 1264 | } |
| 1265 | return; |
| 1266 | } |
| 1267 | \f |
| 1268 | /* Access or set a buffer-local symbol's default value. */ |
| 1269 | |
| 1270 | /* Return the default value of SYMBOL, but don't check for voidness. |
| 1271 | Return Qunbound if it is void. */ |
| 1272 | |
| 1273 | static Lisp_Object |
| 1274 | default_value (Lisp_Object symbol) |
| 1275 | { |
| 1276 | struct Lisp_Symbol *sym; |
| 1277 | |
| 1278 | CHECK_SYMBOL (symbol); |
| 1279 | sym = XSYMBOL (symbol); |
| 1280 | |
| 1281 | start: |
| 1282 | switch (sym->redirect) |
| 1283 | { |
| 1284 | case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start; |
| 1285 | case SYMBOL_PLAINVAL: return SYMBOL_VAL (sym); |
| 1286 | case SYMBOL_LOCALIZED: |
| 1287 | { |
| 1288 | /* If var is set up for a buffer that lacks a local value for it, |
| 1289 | the current value is nominally the default value. |
| 1290 | But the `realvalue' slot may be more up to date, since |
| 1291 | ordinary setq stores just that slot. So use that. */ |
| 1292 | struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym); |
| 1293 | if (blv->fwd && EQ (blv->valcell, blv->defcell)) |
| 1294 | return do_symval_forwarding (blv->fwd); |
| 1295 | else |
| 1296 | return XCDR (blv->defcell); |
| 1297 | } |
| 1298 | case SYMBOL_FORWARDED: |
| 1299 | { |
| 1300 | union Lisp_Fwd *valcontents = SYMBOL_FWD (sym); |
| 1301 | |
| 1302 | /* For a built-in buffer-local variable, get the default value |
| 1303 | rather than letting do_symval_forwarding get the current value. */ |
| 1304 | if (BUFFER_OBJFWDP (valcontents)) |
| 1305 | { |
| 1306 | int offset = XBUFFER_OBJFWD (valcontents)->offset; |
| 1307 | if (PER_BUFFER_IDX (offset) != 0) |
| 1308 | return per_buffer_default (offset); |
| 1309 | } |
| 1310 | |
| 1311 | /* For other variables, get the current value. */ |
| 1312 | return do_symval_forwarding (valcontents); |
| 1313 | } |
| 1314 | default: emacs_abort (); |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | DEFUN ("default-boundp", Fdefault_boundp, Sdefault_boundp, 1, 1, 0, |
| 1319 | doc: /* Return t if SYMBOL has a non-void default value. |
| 1320 | This is the value that is seen in buffers that do not have their own values |
| 1321 | for this variable. */) |
| 1322 | (Lisp_Object symbol) |
| 1323 | { |
| 1324 | register Lisp_Object value; |
| 1325 | |
| 1326 | value = default_value (symbol); |
| 1327 | return (EQ (value, Qunbound) ? Qnil : Qt); |
| 1328 | } |
| 1329 | |
| 1330 | DEFUN ("default-value", Fdefault_value, Sdefault_value, 1, 1, 0, |
| 1331 | doc: /* Return SYMBOL's default value. |
| 1332 | This is the value that is seen in buffers that do not have their own values |
| 1333 | for this variable. The default value is meaningful for variables with |
| 1334 | local bindings in certain buffers. */) |
| 1335 | (Lisp_Object symbol) |
| 1336 | { |
| 1337 | register Lisp_Object value; |
| 1338 | |
| 1339 | value = default_value (symbol); |
| 1340 | if (!EQ (value, Qunbound)) |
| 1341 | return value; |
| 1342 | |
| 1343 | xsignal1 (Qvoid_variable, symbol); |
| 1344 | } |
| 1345 | |
| 1346 | DEFUN ("set-default", Fset_default, Sset_default, 2, 2, 0, |
| 1347 | doc: /* Set SYMBOL's default value to VALUE. SYMBOL and VALUE are evaluated. |
| 1348 | The default value is seen in buffers that do not have their own values |
| 1349 | for this variable. */) |
| 1350 | (Lisp_Object symbol, Lisp_Object value) |
| 1351 | { |
| 1352 | struct Lisp_Symbol *sym; |
| 1353 | |
| 1354 | CHECK_SYMBOL (symbol); |
| 1355 | if (SYMBOL_CONSTANT_P (symbol)) |
| 1356 | { |
| 1357 | if (NILP (Fkeywordp (symbol)) |
| 1358 | || !EQ (value, Fdefault_value (symbol))) |
| 1359 | xsignal1 (Qsetting_constant, symbol); |
| 1360 | else |
| 1361 | /* Allow setting keywords to their own value. */ |
| 1362 | return value; |
| 1363 | } |
| 1364 | sym = XSYMBOL (symbol); |
| 1365 | |
| 1366 | start: |
| 1367 | switch (sym->redirect) |
| 1368 | { |
| 1369 | case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start; |
| 1370 | case SYMBOL_PLAINVAL: return Fset (symbol, value); |
| 1371 | case SYMBOL_LOCALIZED: |
| 1372 | { |
| 1373 | struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym); |
| 1374 | |
| 1375 | /* Store new value into the DEFAULT-VALUE slot. */ |
| 1376 | XSETCDR (blv->defcell, value); |
| 1377 | |
| 1378 | /* If the default binding is now loaded, set the REALVALUE slot too. */ |
| 1379 | if (blv->fwd && EQ (blv->defcell, blv->valcell)) |
| 1380 | store_symval_forwarding (blv->fwd, value, NULL); |
| 1381 | return value; |
| 1382 | } |
| 1383 | case SYMBOL_FORWARDED: |
| 1384 | { |
| 1385 | union Lisp_Fwd *valcontents = SYMBOL_FWD (sym); |
| 1386 | |
| 1387 | /* Handle variables like case-fold-search that have special slots |
| 1388 | in the buffer. |
| 1389 | Make them work apparently like Lisp_Buffer_Local_Value variables. */ |
| 1390 | if (BUFFER_OBJFWDP (valcontents)) |
| 1391 | { |
| 1392 | int offset = XBUFFER_OBJFWD (valcontents)->offset; |
| 1393 | int idx = PER_BUFFER_IDX (offset); |
| 1394 | |
| 1395 | set_per_buffer_default (offset, value); |
| 1396 | |
| 1397 | /* If this variable is not always local in all buffers, |
| 1398 | set it in the buffers that don't nominally have a local value. */ |
| 1399 | if (idx > 0) |
| 1400 | { |
| 1401 | struct buffer *b; |
| 1402 | |
| 1403 | FOR_EACH_BUFFER (b) |
| 1404 | if (!PER_BUFFER_VALUE_P (b, idx)) |
| 1405 | set_per_buffer_value (b, offset, value); |
| 1406 | } |
| 1407 | return value; |
| 1408 | } |
| 1409 | else |
| 1410 | return Fset (symbol, value); |
| 1411 | } |
| 1412 | default: emacs_abort (); |
| 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | DEFUN ("setq-default", Fsetq_default, Ssetq_default, 0, UNEVALLED, 0, |
| 1417 | doc: /* Set the default value of variable VAR to VALUE. |
| 1418 | VAR, the variable name, is literal (not evaluated); |
| 1419 | VALUE is an expression: it is evaluated and its value returned. |
| 1420 | The default value of a variable is seen in buffers |
| 1421 | that do not have their own values for the variable. |
| 1422 | |
| 1423 | More generally, you can use multiple variables and values, as in |
| 1424 | (setq-default VAR VALUE VAR VALUE...) |
| 1425 | This sets each VAR's default value to the corresponding VALUE. |
| 1426 | The VALUE for the Nth VAR can refer to the new default values |
| 1427 | of previous VARs. |
| 1428 | usage: (setq-default [VAR VALUE]...) */) |
| 1429 | (Lisp_Object args) |
| 1430 | { |
| 1431 | register Lisp_Object args_left; |
| 1432 | register Lisp_Object val, symbol; |
| 1433 | struct gcpro gcpro1; |
| 1434 | |
| 1435 | if (NILP (args)) |
| 1436 | return Qnil; |
| 1437 | |
| 1438 | args_left = args; |
| 1439 | GCPRO1 (args); |
| 1440 | |
| 1441 | do |
| 1442 | { |
| 1443 | val = eval_sub (Fcar (Fcdr (args_left))); |
| 1444 | symbol = XCAR (args_left); |
| 1445 | Fset_default (symbol, val); |
| 1446 | args_left = Fcdr (XCDR (args_left)); |
| 1447 | } |
| 1448 | while (!NILP (args_left)); |
| 1449 | |
| 1450 | UNGCPRO; |
| 1451 | return val; |
| 1452 | } |
| 1453 | \f |
| 1454 | /* Lisp functions for creating and removing buffer-local variables. */ |
| 1455 | |
| 1456 | union Lisp_Val_Fwd |
| 1457 | { |
| 1458 | Lisp_Object value; |
| 1459 | union Lisp_Fwd *fwd; |
| 1460 | }; |
| 1461 | |
| 1462 | static struct Lisp_Buffer_Local_Value * |
| 1463 | make_blv (struct Lisp_Symbol *sym, bool forwarded, |
| 1464 | union Lisp_Val_Fwd valcontents) |
| 1465 | { |
| 1466 | struct Lisp_Buffer_Local_Value *blv = xmalloc (sizeof *blv); |
| 1467 | Lisp_Object symbol; |
| 1468 | Lisp_Object tem; |
| 1469 | |
| 1470 | XSETSYMBOL (symbol, sym); |
| 1471 | tem = Fcons (symbol, (forwarded |
| 1472 | ? do_symval_forwarding (valcontents.fwd) |
| 1473 | : valcontents.value)); |
| 1474 | |
| 1475 | /* Buffer_Local_Values cannot have as realval a buffer-local |
| 1476 | or keyboard-local forwarding. */ |
| 1477 | eassert (!(forwarded && BUFFER_OBJFWDP (valcontents.fwd))); |
| 1478 | eassert (!(forwarded && KBOARD_OBJFWDP (valcontents.fwd))); |
| 1479 | blv->fwd = forwarded ? valcontents.fwd : NULL; |
| 1480 | set_blv_where (blv, Qnil); |
| 1481 | blv->frame_local = 0; |
| 1482 | blv->local_if_set = 0; |
| 1483 | set_blv_defcell (blv, tem); |
| 1484 | set_blv_valcell (blv, tem); |
| 1485 | set_blv_found (blv, 0); |
| 1486 | return blv; |
| 1487 | } |
| 1488 | |
| 1489 | DEFUN ("make-variable-buffer-local", Fmake_variable_buffer_local, |
| 1490 | Smake_variable_buffer_local, 1, 1, "vMake Variable Buffer Local: ", |
| 1491 | doc: /* Make VARIABLE become buffer-local whenever it is set. |
| 1492 | At any time, the value for the current buffer is in effect, |
| 1493 | unless the variable has never been set in this buffer, |
| 1494 | in which case the default value is in effect. |
| 1495 | Note that binding the variable with `let', or setting it while |
| 1496 | a `let'-style binding made in this buffer is in effect, |
| 1497 | does not make the variable buffer-local. Return VARIABLE. |
| 1498 | |
| 1499 | In most cases it is better to use `make-local-variable', |
| 1500 | which makes a variable local in just one buffer. |
| 1501 | |
| 1502 | The function `default-value' gets the default value and `set-default' sets it. */) |
| 1503 | (register Lisp_Object variable) |
| 1504 | { |
| 1505 | struct Lisp_Symbol *sym; |
| 1506 | struct Lisp_Buffer_Local_Value *blv = NULL; |
| 1507 | union Lisp_Val_Fwd valcontents IF_LINT (= {LISP_INITIALLY_ZERO}); |
| 1508 | bool forwarded IF_LINT (= 0); |
| 1509 | |
| 1510 | CHECK_SYMBOL (variable); |
| 1511 | sym = XSYMBOL (variable); |
| 1512 | |
| 1513 | start: |
| 1514 | switch (sym->redirect) |
| 1515 | { |
| 1516 | case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start; |
| 1517 | case SYMBOL_PLAINVAL: |
| 1518 | forwarded = 0; valcontents.value = SYMBOL_VAL (sym); |
| 1519 | if (EQ (valcontents.value, Qunbound)) |
| 1520 | valcontents.value = Qnil; |
| 1521 | break; |
| 1522 | case SYMBOL_LOCALIZED: |
| 1523 | blv = SYMBOL_BLV (sym); |
| 1524 | if (blv->frame_local) |
| 1525 | error ("Symbol %s may not be buffer-local", |
| 1526 | SDATA (SYMBOL_NAME (variable))); |
| 1527 | break; |
| 1528 | case SYMBOL_FORWARDED: |
| 1529 | forwarded = 1; valcontents.fwd = SYMBOL_FWD (sym); |
| 1530 | if (KBOARD_OBJFWDP (valcontents.fwd)) |
| 1531 | error ("Symbol %s may not be buffer-local", |
| 1532 | SDATA (SYMBOL_NAME (variable))); |
| 1533 | else if (BUFFER_OBJFWDP (valcontents.fwd)) |
| 1534 | return variable; |
| 1535 | break; |
| 1536 | default: emacs_abort (); |
| 1537 | } |
| 1538 | |
| 1539 | if (sym->constant) |
| 1540 | error ("Symbol %s may not be buffer-local", SDATA (SYMBOL_NAME (variable))); |
| 1541 | |
| 1542 | if (!blv) |
| 1543 | { |
| 1544 | blv = make_blv (sym, forwarded, valcontents); |
| 1545 | sym->redirect = SYMBOL_LOCALIZED; |
| 1546 | SET_SYMBOL_BLV (sym, blv); |
| 1547 | { |
| 1548 | Lisp_Object symbol; |
| 1549 | XSETSYMBOL (symbol, sym); /* In case `variable' is aliased. */ |
| 1550 | if (let_shadows_global_binding_p (symbol)) |
| 1551 | message ("Making %s buffer-local while let-bound!", |
| 1552 | SDATA (SYMBOL_NAME (variable))); |
| 1553 | } |
| 1554 | } |
| 1555 | |
| 1556 | blv->local_if_set = 1; |
| 1557 | return variable; |
| 1558 | } |
| 1559 | |
| 1560 | DEFUN ("make-local-variable", Fmake_local_variable, Smake_local_variable, |
| 1561 | 1, 1, "vMake Local Variable: ", |
| 1562 | doc: /* Make VARIABLE have a separate value in the current buffer. |
| 1563 | Other buffers will continue to share a common default value. |
| 1564 | \(The buffer-local value of VARIABLE starts out as the same value |
| 1565 | VARIABLE previously had. If VARIABLE was void, it remains void.\) |
| 1566 | Return VARIABLE. |
| 1567 | |
| 1568 | If the variable is already arranged to become local when set, |
| 1569 | this function causes a local value to exist for this buffer, |
| 1570 | just as setting the variable would do. |
| 1571 | |
| 1572 | This function returns VARIABLE, and therefore |
| 1573 | (set (make-local-variable 'VARIABLE) VALUE-EXP) |
| 1574 | works. |
| 1575 | |
| 1576 | See also `make-variable-buffer-local'. |
| 1577 | |
| 1578 | Do not use `make-local-variable' to make a hook variable buffer-local. |
| 1579 | Instead, use `add-hook' and specify t for the LOCAL argument. */) |
| 1580 | (Lisp_Object variable) |
| 1581 | { |
| 1582 | Lisp_Object tem; |
| 1583 | bool forwarded IF_LINT (= 0); |
| 1584 | union Lisp_Val_Fwd valcontents IF_LINT (= {LISP_INITIALLY_ZERO}); |
| 1585 | struct Lisp_Symbol *sym; |
| 1586 | struct Lisp_Buffer_Local_Value *blv = NULL; |
| 1587 | |
| 1588 | CHECK_SYMBOL (variable); |
| 1589 | sym = XSYMBOL (variable); |
| 1590 | |
| 1591 | start: |
| 1592 | switch (sym->redirect) |
| 1593 | { |
| 1594 | case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start; |
| 1595 | case SYMBOL_PLAINVAL: |
| 1596 | forwarded = 0; valcontents.value = SYMBOL_VAL (sym); break; |
| 1597 | case SYMBOL_LOCALIZED: |
| 1598 | blv = SYMBOL_BLV (sym); |
| 1599 | if (blv->frame_local) |
| 1600 | error ("Symbol %s may not be buffer-local", |
| 1601 | SDATA (SYMBOL_NAME (variable))); |
| 1602 | break; |
| 1603 | case SYMBOL_FORWARDED: |
| 1604 | forwarded = 1; valcontents.fwd = SYMBOL_FWD (sym); |
| 1605 | if (KBOARD_OBJFWDP (valcontents.fwd)) |
| 1606 | error ("Symbol %s may not be buffer-local", |
| 1607 | SDATA (SYMBOL_NAME (variable))); |
| 1608 | break; |
| 1609 | default: emacs_abort (); |
| 1610 | } |
| 1611 | |
| 1612 | if (sym->constant) |
| 1613 | error ("Symbol %s may not be buffer-local", |
| 1614 | SDATA (SYMBOL_NAME (variable))); |
| 1615 | |
| 1616 | if (blv ? blv->local_if_set |
| 1617 | : (forwarded && BUFFER_OBJFWDP (valcontents.fwd))) |
| 1618 | { |
| 1619 | tem = Fboundp (variable); |
| 1620 | /* Make sure the symbol has a local value in this particular buffer, |
| 1621 | by setting it to the same value it already has. */ |
| 1622 | Fset (variable, (EQ (tem, Qt) ? Fsymbol_value (variable) : Qunbound)); |
| 1623 | return variable; |
| 1624 | } |
| 1625 | if (!blv) |
| 1626 | { |
| 1627 | blv = make_blv (sym, forwarded, valcontents); |
| 1628 | sym->redirect = SYMBOL_LOCALIZED; |
| 1629 | SET_SYMBOL_BLV (sym, blv); |
| 1630 | { |
| 1631 | Lisp_Object symbol; |
| 1632 | XSETSYMBOL (symbol, sym); /* In case `variable' is aliased. */ |
| 1633 | if (let_shadows_global_binding_p (symbol)) |
| 1634 | message ("Making %s local to %s while let-bound!", |
| 1635 | SDATA (SYMBOL_NAME (variable)), |
| 1636 | SDATA (BVAR (current_buffer, name))); |
| 1637 | } |
| 1638 | } |
| 1639 | |
| 1640 | /* Make sure this buffer has its own value of symbol. */ |
| 1641 | XSETSYMBOL (variable, sym); /* Update in case of aliasing. */ |
| 1642 | tem = Fassq (variable, BVAR (current_buffer, local_var_alist)); |
| 1643 | if (NILP (tem)) |
| 1644 | { |
| 1645 | if (let_shadows_buffer_binding_p (sym)) |
| 1646 | message ("Making %s buffer-local while locally let-bound!", |
| 1647 | SDATA (SYMBOL_NAME (variable))); |
| 1648 | |
| 1649 | /* Swap out any local binding for some other buffer, and make |
| 1650 | sure the current value is permanently recorded, if it's the |
| 1651 | default value. */ |
| 1652 | find_symbol_value (variable); |
| 1653 | |
| 1654 | bset_local_var_alist |
| 1655 | (current_buffer, |
| 1656 | Fcons (Fcons (variable, XCDR (blv->defcell)), |
| 1657 | BVAR (current_buffer, local_var_alist))); |
| 1658 | |
| 1659 | /* Make sure symbol does not think it is set up for this buffer; |
| 1660 | force it to look once again for this buffer's value. */ |
| 1661 | if (current_buffer == XBUFFER (blv->where)) |
| 1662 | set_blv_where (blv, Qnil); |
| 1663 | set_blv_found (blv, 0); |
| 1664 | } |
| 1665 | |
| 1666 | /* If the symbol forwards into a C variable, then load the binding |
| 1667 | for this buffer now. If C code modifies the variable before we |
| 1668 | load the binding in, then that new value will clobber the default |
| 1669 | binding the next time we unload it. */ |
| 1670 | if (blv->fwd) |
| 1671 | swap_in_symval_forwarding (sym, blv); |
| 1672 | |
| 1673 | return variable; |
| 1674 | } |
| 1675 | |
| 1676 | DEFUN ("kill-local-variable", Fkill_local_variable, Skill_local_variable, |
| 1677 | 1, 1, "vKill Local Variable: ", |
| 1678 | doc: /* Make VARIABLE no longer have a separate value in the current buffer. |
| 1679 | From now on the default value will apply in this buffer. Return VARIABLE. */) |
| 1680 | (register Lisp_Object variable) |
| 1681 | { |
| 1682 | register Lisp_Object tem; |
| 1683 | struct Lisp_Buffer_Local_Value *blv; |
| 1684 | struct Lisp_Symbol *sym; |
| 1685 | |
| 1686 | CHECK_SYMBOL (variable); |
| 1687 | sym = XSYMBOL (variable); |
| 1688 | |
| 1689 | start: |
| 1690 | switch (sym->redirect) |
| 1691 | { |
| 1692 | case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start; |
| 1693 | case SYMBOL_PLAINVAL: return variable; |
| 1694 | case SYMBOL_FORWARDED: |
| 1695 | { |
| 1696 | union Lisp_Fwd *valcontents = SYMBOL_FWD (sym); |
| 1697 | if (BUFFER_OBJFWDP (valcontents)) |
| 1698 | { |
| 1699 | int offset = XBUFFER_OBJFWD (valcontents)->offset; |
| 1700 | int idx = PER_BUFFER_IDX (offset); |
| 1701 | |
| 1702 | if (idx > 0) |
| 1703 | { |
| 1704 | SET_PER_BUFFER_VALUE_P (current_buffer, idx, 0); |
| 1705 | set_per_buffer_value (current_buffer, offset, |
| 1706 | per_buffer_default (offset)); |
| 1707 | } |
| 1708 | } |
| 1709 | return variable; |
| 1710 | } |
| 1711 | case SYMBOL_LOCALIZED: |
| 1712 | blv = SYMBOL_BLV (sym); |
| 1713 | if (blv->frame_local) |
| 1714 | return variable; |
| 1715 | break; |
| 1716 | default: emacs_abort (); |
| 1717 | } |
| 1718 | |
| 1719 | /* Get rid of this buffer's alist element, if any. */ |
| 1720 | XSETSYMBOL (variable, sym); /* Propagate variable indirection. */ |
| 1721 | tem = Fassq (variable, BVAR (current_buffer, local_var_alist)); |
| 1722 | if (!NILP (tem)) |
| 1723 | bset_local_var_alist |
| 1724 | (current_buffer, |
| 1725 | Fdelq (tem, BVAR (current_buffer, local_var_alist))); |
| 1726 | |
| 1727 | /* If the symbol is set up with the current buffer's binding |
| 1728 | loaded, recompute its value. We have to do it now, or else |
| 1729 | forwarded objects won't work right. */ |
| 1730 | { |
| 1731 | Lisp_Object buf; XSETBUFFER (buf, current_buffer); |
| 1732 | if (EQ (buf, blv->where)) |
| 1733 | { |
| 1734 | set_blv_where (blv, Qnil); |
| 1735 | blv->found = 0; |
| 1736 | find_symbol_value (variable); |
| 1737 | } |
| 1738 | } |
| 1739 | |
| 1740 | return variable; |
| 1741 | } |
| 1742 | |
| 1743 | /* Lisp functions for creating and removing buffer-local variables. */ |
| 1744 | |
| 1745 | /* Obsolete since 22.2. NB adjust doc of modify-frame-parameters |
| 1746 | when/if this is removed. */ |
| 1747 | |
| 1748 | DEFUN ("make-variable-frame-local", Fmake_variable_frame_local, Smake_variable_frame_local, |
| 1749 | 1, 1, "vMake Variable Frame Local: ", |
| 1750 | doc: /* Enable VARIABLE to have frame-local bindings. |
| 1751 | This does not create any frame-local bindings for VARIABLE, |
| 1752 | it just makes them possible. |
| 1753 | |
| 1754 | A frame-local binding is actually a frame parameter value. |
| 1755 | If a frame F has a value for the frame parameter named VARIABLE, |
| 1756 | that also acts as a frame-local binding for VARIABLE in F-- |
| 1757 | provided this function has been called to enable VARIABLE |
| 1758 | to have frame-local bindings at all. |
| 1759 | |
| 1760 | The only way to create a frame-local binding for VARIABLE in a frame |
| 1761 | is to set the VARIABLE frame parameter of that frame. See |
| 1762 | `modify-frame-parameters' for how to set frame parameters. |
| 1763 | |
| 1764 | Note that since Emacs 23.1, variables cannot be both buffer-local and |
| 1765 | frame-local any more (buffer-local bindings used to take precedence over |
| 1766 | frame-local bindings). */) |
| 1767 | (Lisp_Object variable) |
| 1768 | { |
| 1769 | bool forwarded; |
| 1770 | union Lisp_Val_Fwd valcontents; |
| 1771 | struct Lisp_Symbol *sym; |
| 1772 | struct Lisp_Buffer_Local_Value *blv = NULL; |
| 1773 | |
| 1774 | CHECK_SYMBOL (variable); |
| 1775 | sym = XSYMBOL (variable); |
| 1776 | |
| 1777 | start: |
| 1778 | switch (sym->redirect) |
| 1779 | { |
| 1780 | case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start; |
| 1781 | case SYMBOL_PLAINVAL: |
| 1782 | forwarded = 0; valcontents.value = SYMBOL_VAL (sym); |
| 1783 | if (EQ (valcontents.value, Qunbound)) |
| 1784 | valcontents.value = Qnil; |
| 1785 | break; |
| 1786 | case SYMBOL_LOCALIZED: |
| 1787 | if (SYMBOL_BLV (sym)->frame_local) |
| 1788 | return variable; |
| 1789 | else |
| 1790 | error ("Symbol %s may not be frame-local", |
| 1791 | SDATA (SYMBOL_NAME (variable))); |
| 1792 | case SYMBOL_FORWARDED: |
| 1793 | forwarded = 1; valcontents.fwd = SYMBOL_FWD (sym); |
| 1794 | if (KBOARD_OBJFWDP (valcontents.fwd) || BUFFER_OBJFWDP (valcontents.fwd)) |
| 1795 | error ("Symbol %s may not be frame-local", |
| 1796 | SDATA (SYMBOL_NAME (variable))); |
| 1797 | break; |
| 1798 | default: emacs_abort (); |
| 1799 | } |
| 1800 | |
| 1801 | if (sym->constant) |
| 1802 | error ("Symbol %s may not be frame-local", SDATA (SYMBOL_NAME (variable))); |
| 1803 | |
| 1804 | blv = make_blv (sym, forwarded, valcontents); |
| 1805 | blv->frame_local = 1; |
| 1806 | sym->redirect = SYMBOL_LOCALIZED; |
| 1807 | SET_SYMBOL_BLV (sym, blv); |
| 1808 | { |
| 1809 | Lisp_Object symbol; |
| 1810 | XSETSYMBOL (symbol, sym); /* In case `variable' is aliased. */ |
| 1811 | if (let_shadows_global_binding_p (symbol)) |
| 1812 | message ("Making %s frame-local while let-bound!", |
| 1813 | SDATA (SYMBOL_NAME (variable))); |
| 1814 | } |
| 1815 | return variable; |
| 1816 | } |
| 1817 | |
| 1818 | DEFUN ("local-variable-p", Flocal_variable_p, Slocal_variable_p, |
| 1819 | 1, 2, 0, |
| 1820 | doc: /* Non-nil if VARIABLE has a local binding in buffer BUFFER. |
| 1821 | BUFFER defaults to the current buffer. */) |
| 1822 | (register Lisp_Object variable, Lisp_Object buffer) |
| 1823 | { |
| 1824 | register struct buffer *buf; |
| 1825 | struct Lisp_Symbol *sym; |
| 1826 | |
| 1827 | if (NILP (buffer)) |
| 1828 | buf = current_buffer; |
| 1829 | else |
| 1830 | { |
| 1831 | CHECK_BUFFER (buffer); |
| 1832 | buf = XBUFFER (buffer); |
| 1833 | } |
| 1834 | |
| 1835 | CHECK_SYMBOL (variable); |
| 1836 | sym = XSYMBOL (variable); |
| 1837 | |
| 1838 | start: |
| 1839 | switch (sym->redirect) |
| 1840 | { |
| 1841 | case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start; |
| 1842 | case SYMBOL_PLAINVAL: return Qnil; |
| 1843 | case SYMBOL_LOCALIZED: |
| 1844 | { |
| 1845 | Lisp_Object tail, elt, tmp; |
| 1846 | struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym); |
| 1847 | XSETBUFFER (tmp, buf); |
| 1848 | XSETSYMBOL (variable, sym); /* Update in case of aliasing. */ |
| 1849 | |
| 1850 | for (tail = BVAR (buf, local_var_alist); CONSP (tail); tail = XCDR (tail)) |
| 1851 | { |
| 1852 | elt = XCAR (tail); |
| 1853 | if (EQ (variable, XCAR (elt))) |
| 1854 | { |
| 1855 | eassert (!blv->frame_local); |
| 1856 | eassert (blv_found (blv) || !EQ (blv->where, tmp)); |
| 1857 | return Qt; |
| 1858 | } |
| 1859 | } |
| 1860 | eassert (!blv_found (blv) || !EQ (blv->where, tmp)); |
| 1861 | return Qnil; |
| 1862 | } |
| 1863 | case SYMBOL_FORWARDED: |
| 1864 | { |
| 1865 | union Lisp_Fwd *valcontents = SYMBOL_FWD (sym); |
| 1866 | if (BUFFER_OBJFWDP (valcontents)) |
| 1867 | { |
| 1868 | int offset = XBUFFER_OBJFWD (valcontents)->offset; |
| 1869 | int idx = PER_BUFFER_IDX (offset); |
| 1870 | if (idx == -1 || PER_BUFFER_VALUE_P (buf, idx)) |
| 1871 | return Qt; |
| 1872 | } |
| 1873 | return Qnil; |
| 1874 | } |
| 1875 | default: emacs_abort (); |
| 1876 | } |
| 1877 | } |
| 1878 | |
| 1879 | DEFUN ("local-variable-if-set-p", Flocal_variable_if_set_p, Slocal_variable_if_set_p, |
| 1880 | 1, 2, 0, |
| 1881 | doc: /* Non-nil if VARIABLE is local in buffer BUFFER when set there. |
| 1882 | BUFFER defaults to the current buffer. |
| 1883 | |
| 1884 | More precisely, return non-nil if either VARIABLE already has a local |
| 1885 | value in BUFFER, or if VARIABLE is automatically buffer-local (see |
| 1886 | `make-variable-buffer-local'). */) |
| 1887 | (register Lisp_Object variable, Lisp_Object buffer) |
| 1888 | { |
| 1889 | struct Lisp_Symbol *sym; |
| 1890 | |
| 1891 | CHECK_SYMBOL (variable); |
| 1892 | sym = XSYMBOL (variable); |
| 1893 | |
| 1894 | start: |
| 1895 | switch (sym->redirect) |
| 1896 | { |
| 1897 | case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start; |
| 1898 | case SYMBOL_PLAINVAL: return Qnil; |
| 1899 | case SYMBOL_LOCALIZED: |
| 1900 | { |
| 1901 | struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym); |
| 1902 | if (blv->local_if_set) |
| 1903 | return Qt; |
| 1904 | XSETSYMBOL (variable, sym); /* Update in case of aliasing. */ |
| 1905 | return Flocal_variable_p (variable, buffer); |
| 1906 | } |
| 1907 | case SYMBOL_FORWARDED: |
| 1908 | /* All BUFFER_OBJFWD slots become local if they are set. */ |
| 1909 | return (BUFFER_OBJFWDP (SYMBOL_FWD (sym)) ? Qt : Qnil); |
| 1910 | default: emacs_abort (); |
| 1911 | } |
| 1912 | } |
| 1913 | |
| 1914 | DEFUN ("variable-binding-locus", Fvariable_binding_locus, Svariable_binding_locus, |
| 1915 | 1, 1, 0, |
| 1916 | doc: /* Return a value indicating where VARIABLE's current binding comes from. |
| 1917 | If the current binding is buffer-local, the value is the current buffer. |
| 1918 | If the current binding is frame-local, the value is the selected frame. |
| 1919 | If the current binding is global (the default), the value is nil. */) |
| 1920 | (register Lisp_Object variable) |
| 1921 | { |
| 1922 | struct Lisp_Symbol *sym; |
| 1923 | |
| 1924 | CHECK_SYMBOL (variable); |
| 1925 | sym = XSYMBOL (variable); |
| 1926 | |
| 1927 | /* Make sure the current binding is actually swapped in. */ |
| 1928 | find_symbol_value (variable); |
| 1929 | |
| 1930 | start: |
| 1931 | switch (sym->redirect) |
| 1932 | { |
| 1933 | case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start; |
| 1934 | case SYMBOL_PLAINVAL: return Qnil; |
| 1935 | case SYMBOL_FORWARDED: |
| 1936 | { |
| 1937 | union Lisp_Fwd *valcontents = SYMBOL_FWD (sym); |
| 1938 | if (KBOARD_OBJFWDP (valcontents)) |
| 1939 | return Fframe_terminal (Fselected_frame ()); |
| 1940 | else if (!BUFFER_OBJFWDP (valcontents)) |
| 1941 | return Qnil; |
| 1942 | } |
| 1943 | /* FALLTHROUGH */ |
| 1944 | case SYMBOL_LOCALIZED: |
| 1945 | /* For a local variable, record both the symbol and which |
| 1946 | buffer's or frame's value we are saving. */ |
| 1947 | if (!NILP (Flocal_variable_p (variable, Qnil))) |
| 1948 | return Fcurrent_buffer (); |
| 1949 | else if (sym->redirect == SYMBOL_LOCALIZED |
| 1950 | && blv_found (SYMBOL_BLV (sym))) |
| 1951 | return SYMBOL_BLV (sym)->where; |
| 1952 | else |
| 1953 | return Qnil; |
| 1954 | default: emacs_abort (); |
| 1955 | } |
| 1956 | } |
| 1957 | |
| 1958 | /* This code is disabled now that we use the selected frame to return |
| 1959 | keyboard-local-values. */ |
| 1960 | #if 0 |
| 1961 | extern struct terminal *get_terminal (Lisp_Object display, int); |
| 1962 | |
| 1963 | DEFUN ("terminal-local-value", Fterminal_local_value, |
| 1964 | Sterminal_local_value, 2, 2, 0, |
| 1965 | doc: /* Return the terminal-local value of SYMBOL on TERMINAL. |
| 1966 | If SYMBOL is not a terminal-local variable, then return its normal |
| 1967 | value, like `symbol-value'. |
| 1968 | |
| 1969 | TERMINAL may be a terminal object, a frame, or nil (meaning the |
| 1970 | selected frame's terminal device). */) |
| 1971 | (Lisp_Object symbol, Lisp_Object terminal) |
| 1972 | { |
| 1973 | Lisp_Object result; |
| 1974 | struct terminal *t = get_terminal (terminal, 1); |
| 1975 | push_kboard (t->kboard); |
| 1976 | result = Fsymbol_value (symbol); |
| 1977 | pop_kboard (); |
| 1978 | return result; |
| 1979 | } |
| 1980 | |
| 1981 | DEFUN ("set-terminal-local-value", Fset_terminal_local_value, |
| 1982 | Sset_terminal_local_value, 3, 3, 0, |
| 1983 | doc: /* Set the terminal-local binding of SYMBOL on TERMINAL to VALUE. |
| 1984 | If VARIABLE is not a terminal-local variable, then set its normal |
| 1985 | binding, like `set'. |
| 1986 | |
| 1987 | TERMINAL may be a terminal object, a frame, or nil (meaning the |
| 1988 | selected frame's terminal device). */) |
| 1989 | (Lisp_Object symbol, Lisp_Object terminal, Lisp_Object value) |
| 1990 | { |
| 1991 | Lisp_Object result; |
| 1992 | struct terminal *t = get_terminal (terminal, 1); |
| 1993 | push_kboard (d->kboard); |
| 1994 | result = Fset (symbol, value); |
| 1995 | pop_kboard (); |
| 1996 | return result; |
| 1997 | } |
| 1998 | #endif |
| 1999 | \f |
| 2000 | /* Find the function at the end of a chain of symbol function indirections. */ |
| 2001 | |
| 2002 | /* If OBJECT is a symbol, find the end of its function chain and |
| 2003 | return the value found there. If OBJECT is not a symbol, just |
| 2004 | return it. If there is a cycle in the function chain, signal a |
| 2005 | cyclic-function-indirection error. |
| 2006 | |
| 2007 | This is like Findirect_function, except that it doesn't signal an |
| 2008 | error if the chain ends up unbound. */ |
| 2009 | Lisp_Object |
| 2010 | indirect_function (register Lisp_Object object) |
| 2011 | { |
| 2012 | Lisp_Object tortoise, hare; |
| 2013 | |
| 2014 | hare = tortoise = object; |
| 2015 | |
| 2016 | for (;;) |
| 2017 | { |
| 2018 | if (!SYMBOLP (hare) || EQ (hare, Qunbound)) |
| 2019 | break; |
| 2020 | hare = XSYMBOL (hare)->function; |
| 2021 | if (!SYMBOLP (hare) || EQ (hare, Qunbound)) |
| 2022 | break; |
| 2023 | hare = XSYMBOL (hare)->function; |
| 2024 | |
| 2025 | tortoise = XSYMBOL (tortoise)->function; |
| 2026 | |
| 2027 | if (EQ (hare, tortoise)) |
| 2028 | xsignal1 (Qcyclic_function_indirection, object); |
| 2029 | } |
| 2030 | |
| 2031 | return hare; |
| 2032 | } |
| 2033 | |
| 2034 | DEFUN ("indirect-function", Findirect_function, Sindirect_function, 1, 2, 0, |
| 2035 | doc: /* Return the function at the end of OBJECT's function chain. |
| 2036 | If OBJECT is not a symbol, just return it. Otherwise, follow all |
| 2037 | function indirections to find the final function binding and return it. |
| 2038 | If the final symbol in the chain is unbound, signal a void-function error. |
| 2039 | Optional arg NOERROR non-nil means to return nil instead of signaling. |
| 2040 | Signal a cyclic-function-indirection error if there is a loop in the |
| 2041 | function chain of symbols. */) |
| 2042 | (register Lisp_Object object, Lisp_Object noerror) |
| 2043 | { |
| 2044 | Lisp_Object result; |
| 2045 | |
| 2046 | /* Optimize for no indirection. */ |
| 2047 | result = object; |
| 2048 | if (SYMBOLP (result) && !EQ (result, Qunbound) |
| 2049 | && (result = XSYMBOL (result)->function, SYMBOLP (result))) |
| 2050 | result = indirect_function (result); |
| 2051 | if (!EQ (result, Qunbound)) |
| 2052 | return result; |
| 2053 | |
| 2054 | if (NILP (noerror)) |
| 2055 | xsignal1 (Qvoid_function, object); |
| 2056 | |
| 2057 | return Qnil; |
| 2058 | } |
| 2059 | \f |
| 2060 | /* Extract and set vector and string elements. */ |
| 2061 | |
| 2062 | DEFUN ("aref", Faref, Saref, 2, 2, 0, |
| 2063 | doc: /* Return the element of ARRAY at index IDX. |
| 2064 | ARRAY may be a vector, a string, a char-table, a bool-vector, |
| 2065 | or a byte-code object. IDX starts at 0. */) |
| 2066 | (register Lisp_Object array, Lisp_Object idx) |
| 2067 | { |
| 2068 | register EMACS_INT idxval; |
| 2069 | |
| 2070 | CHECK_NUMBER (idx); |
| 2071 | idxval = XINT (idx); |
| 2072 | if (STRINGP (array)) |
| 2073 | { |
| 2074 | int c; |
| 2075 | ptrdiff_t idxval_byte; |
| 2076 | |
| 2077 | if (idxval < 0 || idxval >= SCHARS (array)) |
| 2078 | args_out_of_range (array, idx); |
| 2079 | if (! STRING_MULTIBYTE (array)) |
| 2080 | return make_number ((unsigned char) SREF (array, idxval)); |
| 2081 | idxval_byte = string_char_to_byte (array, idxval); |
| 2082 | |
| 2083 | c = STRING_CHAR (SDATA (array) + idxval_byte); |
| 2084 | return make_number (c); |
| 2085 | } |
| 2086 | else if (BOOL_VECTOR_P (array)) |
| 2087 | { |
| 2088 | int val; |
| 2089 | |
| 2090 | if (idxval < 0 || idxval >= XBOOL_VECTOR (array)->size) |
| 2091 | args_out_of_range (array, idx); |
| 2092 | |
| 2093 | val = (unsigned char) XBOOL_VECTOR (array)->data[idxval / BOOL_VECTOR_BITS_PER_CHAR]; |
| 2094 | return (val & (1 << (idxval % BOOL_VECTOR_BITS_PER_CHAR)) ? Qt : Qnil); |
| 2095 | } |
| 2096 | else if (CHAR_TABLE_P (array)) |
| 2097 | { |
| 2098 | CHECK_CHARACTER (idx); |
| 2099 | return CHAR_TABLE_REF (array, idxval); |
| 2100 | } |
| 2101 | else |
| 2102 | { |
| 2103 | ptrdiff_t size = 0; |
| 2104 | if (VECTORP (array)) |
| 2105 | size = ASIZE (array); |
| 2106 | else if (COMPILEDP (array)) |
| 2107 | size = ASIZE (array) & PSEUDOVECTOR_SIZE_MASK; |
| 2108 | else |
| 2109 | wrong_type_argument (Qarrayp, array); |
| 2110 | |
| 2111 | if (idxval < 0 || idxval >= size) |
| 2112 | args_out_of_range (array, idx); |
| 2113 | return AREF (array, idxval); |
| 2114 | } |
| 2115 | } |
| 2116 | |
| 2117 | DEFUN ("aset", Faset, Saset, 3, 3, 0, |
| 2118 | doc: /* Store into the element of ARRAY at index IDX the value NEWELT. |
| 2119 | Return NEWELT. ARRAY may be a vector, a string, a char-table or a |
| 2120 | bool-vector. IDX starts at 0. */) |
| 2121 | (register Lisp_Object array, Lisp_Object idx, Lisp_Object newelt) |
| 2122 | { |
| 2123 | register EMACS_INT idxval; |
| 2124 | |
| 2125 | CHECK_NUMBER (idx); |
| 2126 | idxval = XINT (idx); |
| 2127 | CHECK_ARRAY (array, Qarrayp); |
| 2128 | CHECK_IMPURE (array); |
| 2129 | |
| 2130 | if (VECTORP (array)) |
| 2131 | { |
| 2132 | if (idxval < 0 || idxval >= ASIZE (array)) |
| 2133 | args_out_of_range (array, idx); |
| 2134 | ASET (array, idxval, newelt); |
| 2135 | } |
| 2136 | else if (BOOL_VECTOR_P (array)) |
| 2137 | { |
| 2138 | int val; |
| 2139 | |
| 2140 | if (idxval < 0 || idxval >= XBOOL_VECTOR (array)->size) |
| 2141 | args_out_of_range (array, idx); |
| 2142 | |
| 2143 | val = (unsigned char) XBOOL_VECTOR (array)->data[idxval / BOOL_VECTOR_BITS_PER_CHAR]; |
| 2144 | |
| 2145 | if (! NILP (newelt)) |
| 2146 | val |= 1 << (idxval % BOOL_VECTOR_BITS_PER_CHAR); |
| 2147 | else |
| 2148 | val &= ~(1 << (idxval % BOOL_VECTOR_BITS_PER_CHAR)); |
| 2149 | XBOOL_VECTOR (array)->data[idxval / BOOL_VECTOR_BITS_PER_CHAR] = val; |
| 2150 | } |
| 2151 | else if (CHAR_TABLE_P (array)) |
| 2152 | { |
| 2153 | CHECK_CHARACTER (idx); |
| 2154 | CHAR_TABLE_SET (array, idxval, newelt); |
| 2155 | } |
| 2156 | else |
| 2157 | { |
| 2158 | int c; |
| 2159 | |
| 2160 | if (idxval < 0 || idxval >= SCHARS (array)) |
| 2161 | args_out_of_range (array, idx); |
| 2162 | CHECK_CHARACTER (newelt); |
| 2163 | c = XFASTINT (newelt); |
| 2164 | |
| 2165 | if (STRING_MULTIBYTE (array)) |
| 2166 | { |
| 2167 | ptrdiff_t idxval_byte, nbytes; |
| 2168 | int prev_bytes, new_bytes; |
| 2169 | unsigned char workbuf[MAX_MULTIBYTE_LENGTH], *p0 = workbuf, *p1; |
| 2170 | |
| 2171 | nbytes = SBYTES (array); |
| 2172 | idxval_byte = string_char_to_byte (array, idxval); |
| 2173 | p1 = SDATA (array) + idxval_byte; |
| 2174 | prev_bytes = BYTES_BY_CHAR_HEAD (*p1); |
| 2175 | new_bytes = CHAR_STRING (c, p0); |
| 2176 | if (prev_bytes != new_bytes) |
| 2177 | { |
| 2178 | /* We must relocate the string data. */ |
| 2179 | ptrdiff_t nchars = SCHARS (array); |
| 2180 | USE_SAFE_ALLOCA; |
| 2181 | unsigned char *str = SAFE_ALLOCA (nbytes); |
| 2182 | |
| 2183 | memcpy (str, SDATA (array), nbytes); |
| 2184 | allocate_string_data (XSTRING (array), nchars, |
| 2185 | nbytes + new_bytes - prev_bytes); |
| 2186 | memcpy (SDATA (array), str, idxval_byte); |
| 2187 | p1 = SDATA (array) + idxval_byte; |
| 2188 | memcpy (p1 + new_bytes, str + idxval_byte + prev_bytes, |
| 2189 | nbytes - (idxval_byte + prev_bytes)); |
| 2190 | SAFE_FREE (); |
| 2191 | clear_string_char_byte_cache (); |
| 2192 | } |
| 2193 | while (new_bytes--) |
| 2194 | *p1++ = *p0++; |
| 2195 | } |
| 2196 | else |
| 2197 | { |
| 2198 | if (! SINGLE_BYTE_CHAR_P (c)) |
| 2199 | { |
| 2200 | int i; |
| 2201 | |
| 2202 | for (i = SBYTES (array) - 1; i >= 0; i--) |
| 2203 | if (SREF (array, i) >= 0x80) |
| 2204 | args_out_of_range (array, newelt); |
| 2205 | /* ARRAY is an ASCII string. Convert it to a multibyte |
| 2206 | string, and try `aset' again. */ |
| 2207 | STRING_SET_MULTIBYTE (array); |
| 2208 | return Faset (array, idx, newelt); |
| 2209 | } |
| 2210 | SSET (array, idxval, c); |
| 2211 | } |
| 2212 | } |
| 2213 | |
| 2214 | return newelt; |
| 2215 | } |
| 2216 | \f |
| 2217 | /* Arithmetic functions */ |
| 2218 | |
| 2219 | enum comparison { equal, notequal, less, grtr, less_or_equal, grtr_or_equal }; |
| 2220 | |
| 2221 | static Lisp_Object |
| 2222 | arithcompare (Lisp_Object num1, Lisp_Object num2, enum comparison comparison) |
| 2223 | { |
| 2224 | double f1 = 0, f2 = 0; |
| 2225 | bool floatp = 0; |
| 2226 | |
| 2227 | CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num1); |
| 2228 | CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (num2); |
| 2229 | |
| 2230 | if (FLOATP (num1) || FLOATP (num2)) |
| 2231 | { |
| 2232 | floatp = 1; |
| 2233 | f1 = (FLOATP (num1)) ? XFLOAT_DATA (num1) : XINT (num1); |
| 2234 | f2 = (FLOATP (num2)) ? XFLOAT_DATA (num2) : XINT (num2); |
| 2235 | } |
| 2236 | |
| 2237 | switch (comparison) |
| 2238 | { |
| 2239 | case equal: |
| 2240 | if (floatp ? f1 == f2 : XINT (num1) == XINT (num2)) |
| 2241 | return Qt; |
| 2242 | return Qnil; |
| 2243 | |
| 2244 | case notequal: |
| 2245 | if (floatp ? f1 != f2 : XINT (num1) != XINT (num2)) |
| 2246 | return Qt; |
| 2247 | return Qnil; |
| 2248 | |
| 2249 | case less: |
| 2250 | if (floatp ? f1 < f2 : XINT (num1) < XINT (num2)) |
| 2251 | return Qt; |
| 2252 | return Qnil; |
| 2253 | |
| 2254 | case less_or_equal: |
| 2255 | if (floatp ? f1 <= f2 : XINT (num1) <= XINT (num2)) |
| 2256 | return Qt; |
| 2257 | return Qnil; |
| 2258 | |
| 2259 | case grtr: |
| 2260 | if (floatp ? f1 > f2 : XINT (num1) > XINT (num2)) |
| 2261 | return Qt; |
| 2262 | return Qnil; |
| 2263 | |
| 2264 | case grtr_or_equal: |
| 2265 | if (floatp ? f1 >= f2 : XINT (num1) >= XINT (num2)) |
| 2266 | return Qt; |
| 2267 | return Qnil; |
| 2268 | |
| 2269 | default: |
| 2270 | emacs_abort (); |
| 2271 | } |
| 2272 | } |
| 2273 | |
| 2274 | DEFUN ("=", Feqlsign, Seqlsign, 2, 2, 0, |
| 2275 | doc: /* Return t if two args, both numbers or markers, are equal. */) |
| 2276 | (register Lisp_Object num1, Lisp_Object num2) |
| 2277 | { |
| 2278 | return arithcompare (num1, num2, equal); |
| 2279 | } |
| 2280 | |
| 2281 | DEFUN ("<", Flss, Slss, 2, 2, 0, |
| 2282 | doc: /* Return t if first arg is less than second arg. Both must be numbers or markers. */) |
| 2283 | (register Lisp_Object num1, Lisp_Object num2) |
| 2284 | { |
| 2285 | return arithcompare (num1, num2, less); |
| 2286 | } |
| 2287 | |
| 2288 | DEFUN (">", Fgtr, Sgtr, 2, 2, 0, |
| 2289 | doc: /* Return t if first arg is greater than second arg. Both must be numbers or markers. */) |
| 2290 | (register Lisp_Object num1, Lisp_Object num2) |
| 2291 | { |
| 2292 | return arithcompare (num1, num2, grtr); |
| 2293 | } |
| 2294 | |
| 2295 | DEFUN ("<=", Fleq, Sleq, 2, 2, 0, |
| 2296 | doc: /* Return t if first arg is less than or equal to second arg. |
| 2297 | Both must be numbers or markers. */) |
| 2298 | (register Lisp_Object num1, Lisp_Object num2) |
| 2299 | { |
| 2300 | return arithcompare (num1, num2, less_or_equal); |
| 2301 | } |
| 2302 | |
| 2303 | DEFUN (">=", Fgeq, Sgeq, 2, 2, 0, |
| 2304 | doc: /* Return t if first arg is greater than or equal to second arg. |
| 2305 | Both must be numbers or markers. */) |
| 2306 | (register Lisp_Object num1, Lisp_Object num2) |
| 2307 | { |
| 2308 | return arithcompare (num1, num2, grtr_or_equal); |
| 2309 | } |
| 2310 | |
| 2311 | DEFUN ("/=", Fneq, Sneq, 2, 2, 0, |
| 2312 | doc: /* Return t if first arg is not equal to second arg. Both must be numbers or markers. */) |
| 2313 | (register Lisp_Object num1, Lisp_Object num2) |
| 2314 | { |
| 2315 | return arithcompare (num1, num2, notequal); |
| 2316 | } |
| 2317 | |
| 2318 | DEFUN ("zerop", Fzerop, Szerop, 1, 1, 0, |
| 2319 | doc: /* Return t if NUMBER is zero. */) |
| 2320 | (register Lisp_Object number) |
| 2321 | { |
| 2322 | CHECK_NUMBER_OR_FLOAT (number); |
| 2323 | |
| 2324 | if (FLOATP (number)) |
| 2325 | { |
| 2326 | if (XFLOAT_DATA (number) == 0.0) |
| 2327 | return Qt; |
| 2328 | return Qnil; |
| 2329 | } |
| 2330 | |
| 2331 | if (!XINT (number)) |
| 2332 | return Qt; |
| 2333 | return Qnil; |
| 2334 | } |
| 2335 | \f |
| 2336 | /* Convert the cons-of-integers, integer, or float value C to an |
| 2337 | unsigned value with maximum value MAX. Signal an error if C does not |
| 2338 | have a valid format or is out of range. */ |
| 2339 | uintmax_t |
| 2340 | cons_to_unsigned (Lisp_Object c, uintmax_t max) |
| 2341 | { |
| 2342 | bool valid = 0; |
| 2343 | uintmax_t val IF_LINT (= 0); |
| 2344 | if (INTEGERP (c)) |
| 2345 | { |
| 2346 | valid = 0 <= XINT (c); |
| 2347 | val = XINT (c); |
| 2348 | } |
| 2349 | else if (FLOATP (c)) |
| 2350 | { |
| 2351 | double d = XFLOAT_DATA (c); |
| 2352 | if (0 <= d |
| 2353 | && d < (max == UINTMAX_MAX ? (double) UINTMAX_MAX + 1 : max + 1)) |
| 2354 | { |
| 2355 | val = d; |
| 2356 | valid = 1; |
| 2357 | } |
| 2358 | } |
| 2359 | else if (CONSP (c) && NATNUMP (XCAR (c))) |
| 2360 | { |
| 2361 | uintmax_t top = XFASTINT (XCAR (c)); |
| 2362 | Lisp_Object rest = XCDR (c); |
| 2363 | if (top <= UINTMAX_MAX >> 24 >> 16 |
| 2364 | && CONSP (rest) |
| 2365 | && NATNUMP (XCAR (rest)) && XFASTINT (XCAR (rest)) < 1 << 24 |
| 2366 | && NATNUMP (XCDR (rest)) && XFASTINT (XCDR (rest)) < 1 << 16) |
| 2367 | { |
| 2368 | uintmax_t mid = XFASTINT (XCAR (rest)); |
| 2369 | val = top << 24 << 16 | mid << 16 | XFASTINT (XCDR (rest)); |
| 2370 | valid = 1; |
| 2371 | } |
| 2372 | else if (top <= UINTMAX_MAX >> 16) |
| 2373 | { |
| 2374 | if (CONSP (rest)) |
| 2375 | rest = XCAR (rest); |
| 2376 | if (NATNUMP (rest) && XFASTINT (rest) < 1 << 16) |
| 2377 | { |
| 2378 | val = top << 16 | XFASTINT (rest); |
| 2379 | valid = 1; |
| 2380 | } |
| 2381 | } |
| 2382 | } |
| 2383 | |
| 2384 | if (! (valid && val <= max)) |
| 2385 | error ("Not an in-range integer, float, or cons of integers"); |
| 2386 | return val; |
| 2387 | } |
| 2388 | |
| 2389 | /* Convert the cons-of-integers, integer, or float value C to a signed |
| 2390 | value with extrema MIN and MAX. Signal an error if C does not have |
| 2391 | a valid format or is out of range. */ |
| 2392 | intmax_t |
| 2393 | cons_to_signed (Lisp_Object c, intmax_t min, intmax_t max) |
| 2394 | { |
| 2395 | bool valid = 0; |
| 2396 | intmax_t val IF_LINT (= 0); |
| 2397 | if (INTEGERP (c)) |
| 2398 | { |
| 2399 | val = XINT (c); |
| 2400 | valid = 1; |
| 2401 | } |
| 2402 | else if (FLOATP (c)) |
| 2403 | { |
| 2404 | double d = XFLOAT_DATA (c); |
| 2405 | if (min <= d |
| 2406 | && d < (max == INTMAX_MAX ? (double) INTMAX_MAX + 1 : max + 1)) |
| 2407 | { |
| 2408 | val = d; |
| 2409 | valid = 1; |
| 2410 | } |
| 2411 | } |
| 2412 | else if (CONSP (c) && INTEGERP (XCAR (c))) |
| 2413 | { |
| 2414 | intmax_t top = XINT (XCAR (c)); |
| 2415 | Lisp_Object rest = XCDR (c); |
| 2416 | if (INTMAX_MIN >> 24 >> 16 <= top && top <= INTMAX_MAX >> 24 >> 16 |
| 2417 | && CONSP (rest) |
| 2418 | && NATNUMP (XCAR (rest)) && XFASTINT (XCAR (rest)) < 1 << 24 |
| 2419 | && NATNUMP (XCDR (rest)) && XFASTINT (XCDR (rest)) < 1 << 16) |
| 2420 | { |
| 2421 | intmax_t mid = XFASTINT (XCAR (rest)); |
| 2422 | val = top << 24 << 16 | mid << 16 | XFASTINT (XCDR (rest)); |
| 2423 | valid = 1; |
| 2424 | } |
| 2425 | else if (INTMAX_MIN >> 16 <= top && top <= INTMAX_MAX >> 16) |
| 2426 | { |
| 2427 | if (CONSP (rest)) |
| 2428 | rest = XCAR (rest); |
| 2429 | if (NATNUMP (rest) && XFASTINT (rest) < 1 << 16) |
| 2430 | { |
| 2431 | val = top << 16 | XFASTINT (rest); |
| 2432 | valid = 1; |
| 2433 | } |
| 2434 | } |
| 2435 | } |
| 2436 | |
| 2437 | if (! (valid && min <= val && val <= max)) |
| 2438 | error ("Not an in-range integer, float, or cons of integers"); |
| 2439 | return val; |
| 2440 | } |
| 2441 | \f |
| 2442 | DEFUN ("number-to-string", Fnumber_to_string, Snumber_to_string, 1, 1, 0, |
| 2443 | doc: /* Return the decimal representation of NUMBER as a string. |
| 2444 | Uses a minus sign if negative. |
| 2445 | NUMBER may be an integer or a floating point number. */) |
| 2446 | (Lisp_Object number) |
| 2447 | { |
| 2448 | char buffer[max (FLOAT_TO_STRING_BUFSIZE, INT_BUFSIZE_BOUND (EMACS_INT))]; |
| 2449 | int len; |
| 2450 | |
| 2451 | CHECK_NUMBER_OR_FLOAT (number); |
| 2452 | |
| 2453 | if (FLOATP (number)) |
| 2454 | len = float_to_string (buffer, XFLOAT_DATA (number)); |
| 2455 | else |
| 2456 | len = sprintf (buffer, "%"pI"d", XINT (number)); |
| 2457 | |
| 2458 | return make_unibyte_string (buffer, len); |
| 2459 | } |
| 2460 | |
| 2461 | DEFUN ("string-to-number", Fstring_to_number, Sstring_to_number, 1, 2, 0, |
| 2462 | doc: /* Parse STRING as a decimal number and return the number. |
| 2463 | This parses both integers and floating point numbers. |
| 2464 | It ignores leading spaces and tabs, and all trailing chars. |
| 2465 | |
| 2466 | If BASE, interpret STRING as a number in that base. If BASE isn't |
| 2467 | present, base 10 is used. BASE must be between 2 and 16 (inclusive). |
| 2468 | If the base used is not 10, STRING is always parsed as integer. */) |
| 2469 | (register Lisp_Object string, Lisp_Object base) |
| 2470 | { |
| 2471 | register char *p; |
| 2472 | register int b; |
| 2473 | Lisp_Object val; |
| 2474 | |
| 2475 | CHECK_STRING (string); |
| 2476 | |
| 2477 | if (NILP (base)) |
| 2478 | b = 10; |
| 2479 | else |
| 2480 | { |
| 2481 | CHECK_NUMBER (base); |
| 2482 | if (! (2 <= XINT (base) && XINT (base) <= 16)) |
| 2483 | xsignal1 (Qargs_out_of_range, base); |
| 2484 | b = XINT (base); |
| 2485 | } |
| 2486 | |
| 2487 | p = SSDATA (string); |
| 2488 | while (*p == ' ' || *p == '\t') |
| 2489 | p++; |
| 2490 | |
| 2491 | val = string_to_number (p, b, 1); |
| 2492 | return NILP (val) ? make_number (0) : val; |
| 2493 | } |
| 2494 | \f |
| 2495 | enum arithop |
| 2496 | { |
| 2497 | Aadd, |
| 2498 | Asub, |
| 2499 | Amult, |
| 2500 | Adiv, |
| 2501 | Alogand, |
| 2502 | Alogior, |
| 2503 | Alogxor, |
| 2504 | Amax, |
| 2505 | Amin |
| 2506 | }; |
| 2507 | |
| 2508 | static Lisp_Object float_arith_driver (double, ptrdiff_t, enum arithop, |
| 2509 | ptrdiff_t, Lisp_Object *); |
| 2510 | static Lisp_Object |
| 2511 | arith_driver (enum arithop code, ptrdiff_t nargs, Lisp_Object *args) |
| 2512 | { |
| 2513 | Lisp_Object val; |
| 2514 | ptrdiff_t argnum, ok_args; |
| 2515 | EMACS_INT accum = 0; |
| 2516 | EMACS_INT next, ok_accum; |
| 2517 | bool overflow = 0; |
| 2518 | |
| 2519 | switch (code) |
| 2520 | { |
| 2521 | case Alogior: |
| 2522 | case Alogxor: |
| 2523 | case Aadd: |
| 2524 | case Asub: |
| 2525 | accum = 0; |
| 2526 | break; |
| 2527 | case Amult: |
| 2528 | accum = 1; |
| 2529 | break; |
| 2530 | case Alogand: |
| 2531 | accum = -1; |
| 2532 | break; |
| 2533 | default: |
| 2534 | break; |
| 2535 | } |
| 2536 | |
| 2537 | for (argnum = 0; argnum < nargs; argnum++) |
| 2538 | { |
| 2539 | if (! overflow) |
| 2540 | { |
| 2541 | ok_args = argnum; |
| 2542 | ok_accum = accum; |
| 2543 | } |
| 2544 | |
| 2545 | /* Using args[argnum] as argument to CHECK_NUMBER_... */ |
| 2546 | val = args[argnum]; |
| 2547 | CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val); |
| 2548 | |
| 2549 | if (FLOATP (val)) |
| 2550 | return float_arith_driver (ok_accum, ok_args, code, |
| 2551 | nargs, args); |
| 2552 | args[argnum] = val; |
| 2553 | next = XINT (args[argnum]); |
| 2554 | switch (code) |
| 2555 | { |
| 2556 | case Aadd: |
| 2557 | if (INT_ADD_OVERFLOW (accum, next)) |
| 2558 | { |
| 2559 | overflow = 1; |
| 2560 | accum &= INTMASK; |
| 2561 | } |
| 2562 | accum += next; |
| 2563 | break; |
| 2564 | case Asub: |
| 2565 | if (INT_SUBTRACT_OVERFLOW (accum, next)) |
| 2566 | { |
| 2567 | overflow = 1; |
| 2568 | accum &= INTMASK; |
| 2569 | } |
| 2570 | accum = argnum ? accum - next : nargs == 1 ? - next : next; |
| 2571 | break; |
| 2572 | case Amult: |
| 2573 | if (INT_MULTIPLY_OVERFLOW (accum, next)) |
| 2574 | { |
| 2575 | EMACS_UINT a = accum, b = next, ab = a * b; |
| 2576 | overflow = 1; |
| 2577 | accum = ab & INTMASK; |
| 2578 | } |
| 2579 | else |
| 2580 | accum *= next; |
| 2581 | break; |
| 2582 | case Adiv: |
| 2583 | if (!argnum) |
| 2584 | accum = next; |
| 2585 | else |
| 2586 | { |
| 2587 | if (next == 0) |
| 2588 | xsignal0 (Qarith_error); |
| 2589 | accum /= next; |
| 2590 | } |
| 2591 | break; |
| 2592 | case Alogand: |
| 2593 | accum &= next; |
| 2594 | break; |
| 2595 | case Alogior: |
| 2596 | accum |= next; |
| 2597 | break; |
| 2598 | case Alogxor: |
| 2599 | accum ^= next; |
| 2600 | break; |
| 2601 | case Amax: |
| 2602 | if (!argnum || next > accum) |
| 2603 | accum = next; |
| 2604 | break; |
| 2605 | case Amin: |
| 2606 | if (!argnum || next < accum) |
| 2607 | accum = next; |
| 2608 | break; |
| 2609 | } |
| 2610 | } |
| 2611 | |
| 2612 | XSETINT (val, accum); |
| 2613 | return val; |
| 2614 | } |
| 2615 | |
| 2616 | #undef isnan |
| 2617 | #define isnan(x) ((x) != (x)) |
| 2618 | |
| 2619 | static Lisp_Object |
| 2620 | float_arith_driver (double accum, ptrdiff_t argnum, enum arithop code, |
| 2621 | ptrdiff_t nargs, Lisp_Object *args) |
| 2622 | { |
| 2623 | register Lisp_Object val; |
| 2624 | double next; |
| 2625 | |
| 2626 | for (; argnum < nargs; argnum++) |
| 2627 | { |
| 2628 | val = args[argnum]; /* using args[argnum] as argument to CHECK_NUMBER_... */ |
| 2629 | CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val); |
| 2630 | |
| 2631 | if (FLOATP (val)) |
| 2632 | { |
| 2633 | next = XFLOAT_DATA (val); |
| 2634 | } |
| 2635 | else |
| 2636 | { |
| 2637 | args[argnum] = val; /* runs into a compiler bug. */ |
| 2638 | next = XINT (args[argnum]); |
| 2639 | } |
| 2640 | switch (code) |
| 2641 | { |
| 2642 | case Aadd: |
| 2643 | accum += next; |
| 2644 | break; |
| 2645 | case Asub: |
| 2646 | accum = argnum ? accum - next : nargs == 1 ? - next : next; |
| 2647 | break; |
| 2648 | case Amult: |
| 2649 | accum *= next; |
| 2650 | break; |
| 2651 | case Adiv: |
| 2652 | if (!argnum) |
| 2653 | accum = next; |
| 2654 | else |
| 2655 | { |
| 2656 | if (! IEEE_FLOATING_POINT && next == 0) |
| 2657 | xsignal0 (Qarith_error); |
| 2658 | accum /= next; |
| 2659 | } |
| 2660 | break; |
| 2661 | case Alogand: |
| 2662 | case Alogior: |
| 2663 | case Alogxor: |
| 2664 | return wrong_type_argument (Qinteger_or_marker_p, val); |
| 2665 | case Amax: |
| 2666 | if (!argnum || isnan (next) || next > accum) |
| 2667 | accum = next; |
| 2668 | break; |
| 2669 | case Amin: |
| 2670 | if (!argnum || isnan (next) || next < accum) |
| 2671 | accum = next; |
| 2672 | break; |
| 2673 | } |
| 2674 | } |
| 2675 | |
| 2676 | return make_float (accum); |
| 2677 | } |
| 2678 | |
| 2679 | |
| 2680 | DEFUN ("+", Fplus, Splus, 0, MANY, 0, |
| 2681 | doc: /* Return sum of any number of arguments, which are numbers or markers. |
| 2682 | usage: (+ &rest NUMBERS-OR-MARKERS) */) |
| 2683 | (ptrdiff_t nargs, Lisp_Object *args) |
| 2684 | { |
| 2685 | return arith_driver (Aadd, nargs, args); |
| 2686 | } |
| 2687 | |
| 2688 | DEFUN ("-", Fminus, Sminus, 0, MANY, 0, |
| 2689 | doc: /* Negate number or subtract numbers or markers and return the result. |
| 2690 | With one arg, negates it. With more than one arg, |
| 2691 | subtracts all but the first from the first. |
| 2692 | usage: (- &optional NUMBER-OR-MARKER &rest MORE-NUMBERS-OR-MARKERS) */) |
| 2693 | (ptrdiff_t nargs, Lisp_Object *args) |
| 2694 | { |
| 2695 | return arith_driver (Asub, nargs, args); |
| 2696 | } |
| 2697 | |
| 2698 | DEFUN ("*", Ftimes, Stimes, 0, MANY, 0, |
| 2699 | doc: /* Return product of any number of arguments, which are numbers or markers. |
| 2700 | usage: (* &rest NUMBERS-OR-MARKERS) */) |
| 2701 | (ptrdiff_t nargs, Lisp_Object *args) |
| 2702 | { |
| 2703 | return arith_driver (Amult, nargs, args); |
| 2704 | } |
| 2705 | |
| 2706 | DEFUN ("/", Fquo, Squo, 2, MANY, 0, |
| 2707 | doc: /* Return first argument divided by all the remaining arguments. |
| 2708 | The arguments must be numbers or markers. |
| 2709 | usage: (/ DIVIDEND DIVISOR &rest DIVISORS) */) |
| 2710 | (ptrdiff_t nargs, Lisp_Object *args) |
| 2711 | { |
| 2712 | ptrdiff_t argnum; |
| 2713 | for (argnum = 2; argnum < nargs; argnum++) |
| 2714 | if (FLOATP (args[argnum])) |
| 2715 | return float_arith_driver (0, 0, Adiv, nargs, args); |
| 2716 | return arith_driver (Adiv, nargs, args); |
| 2717 | } |
| 2718 | |
| 2719 | DEFUN ("%", Frem, Srem, 2, 2, 0, |
| 2720 | doc: /* Return remainder of X divided by Y. |
| 2721 | Both must be integers or markers. */) |
| 2722 | (register Lisp_Object x, Lisp_Object y) |
| 2723 | { |
| 2724 | Lisp_Object val; |
| 2725 | |
| 2726 | CHECK_NUMBER_COERCE_MARKER (x); |
| 2727 | CHECK_NUMBER_COERCE_MARKER (y); |
| 2728 | |
| 2729 | if (XINT (y) == 0) |
| 2730 | xsignal0 (Qarith_error); |
| 2731 | |
| 2732 | XSETINT (val, XINT (x) % XINT (y)); |
| 2733 | return val; |
| 2734 | } |
| 2735 | |
| 2736 | DEFUN ("mod", Fmod, Smod, 2, 2, 0, |
| 2737 | doc: /* Return X modulo Y. |
| 2738 | The result falls between zero (inclusive) and Y (exclusive). |
| 2739 | Both X and Y must be numbers or markers. */) |
| 2740 | (register Lisp_Object x, Lisp_Object y) |
| 2741 | { |
| 2742 | Lisp_Object val; |
| 2743 | EMACS_INT i1, i2; |
| 2744 | |
| 2745 | CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (x); |
| 2746 | CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (y); |
| 2747 | |
| 2748 | if (FLOATP (x) || FLOATP (y)) |
| 2749 | return fmod_float (x, y); |
| 2750 | |
| 2751 | i1 = XINT (x); |
| 2752 | i2 = XINT (y); |
| 2753 | |
| 2754 | if (i2 == 0) |
| 2755 | xsignal0 (Qarith_error); |
| 2756 | |
| 2757 | i1 %= i2; |
| 2758 | |
| 2759 | /* If the "remainder" comes out with the wrong sign, fix it. */ |
| 2760 | if (i2 < 0 ? i1 > 0 : i1 < 0) |
| 2761 | i1 += i2; |
| 2762 | |
| 2763 | XSETINT (val, i1); |
| 2764 | return val; |
| 2765 | } |
| 2766 | |
| 2767 | DEFUN ("max", Fmax, Smax, 1, MANY, 0, |
| 2768 | doc: /* Return largest of all the arguments (which must be numbers or markers). |
| 2769 | The value is always a number; markers are converted to numbers. |
| 2770 | usage: (max NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */) |
| 2771 | (ptrdiff_t nargs, Lisp_Object *args) |
| 2772 | { |
| 2773 | return arith_driver (Amax, nargs, args); |
| 2774 | } |
| 2775 | |
| 2776 | DEFUN ("min", Fmin, Smin, 1, MANY, 0, |
| 2777 | doc: /* Return smallest of all the arguments (which must be numbers or markers). |
| 2778 | The value is always a number; markers are converted to numbers. |
| 2779 | usage: (min NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */) |
| 2780 | (ptrdiff_t nargs, Lisp_Object *args) |
| 2781 | { |
| 2782 | return arith_driver (Amin, nargs, args); |
| 2783 | } |
| 2784 | |
| 2785 | DEFUN ("logand", Flogand, Slogand, 0, MANY, 0, |
| 2786 | doc: /* Return bitwise-and of all the arguments. |
| 2787 | Arguments may be integers, or markers converted to integers. |
| 2788 | usage: (logand &rest INTS-OR-MARKERS) */) |
| 2789 | (ptrdiff_t nargs, Lisp_Object *args) |
| 2790 | { |
| 2791 | return arith_driver (Alogand, nargs, args); |
| 2792 | } |
| 2793 | |
| 2794 | DEFUN ("logior", Flogior, Slogior, 0, MANY, 0, |
| 2795 | doc: /* Return bitwise-or of all the arguments. |
| 2796 | Arguments may be integers, or markers converted to integers. |
| 2797 | usage: (logior &rest INTS-OR-MARKERS) */) |
| 2798 | (ptrdiff_t nargs, Lisp_Object *args) |
| 2799 | { |
| 2800 | return arith_driver (Alogior, nargs, args); |
| 2801 | } |
| 2802 | |
| 2803 | DEFUN ("logxor", Flogxor, Slogxor, 0, MANY, 0, |
| 2804 | doc: /* Return bitwise-exclusive-or of all the arguments. |
| 2805 | Arguments may be integers, or markers converted to integers. |
| 2806 | usage: (logxor &rest INTS-OR-MARKERS) */) |
| 2807 | (ptrdiff_t nargs, Lisp_Object *args) |
| 2808 | { |
| 2809 | return arith_driver (Alogxor, nargs, args); |
| 2810 | } |
| 2811 | |
| 2812 | DEFUN ("ash", Fash, Sash, 2, 2, 0, |
| 2813 | doc: /* Return VALUE with its bits shifted left by COUNT. |
| 2814 | If COUNT is negative, shifting is actually to the right. |
| 2815 | In this case, the sign bit is duplicated. */) |
| 2816 | (register Lisp_Object value, Lisp_Object count) |
| 2817 | { |
| 2818 | register Lisp_Object val; |
| 2819 | |
| 2820 | CHECK_NUMBER (value); |
| 2821 | CHECK_NUMBER (count); |
| 2822 | |
| 2823 | if (XINT (count) >= BITS_PER_EMACS_INT) |
| 2824 | XSETINT (val, 0); |
| 2825 | else if (XINT (count) > 0) |
| 2826 | XSETINT (val, XINT (value) << XFASTINT (count)); |
| 2827 | else if (XINT (count) <= -BITS_PER_EMACS_INT) |
| 2828 | XSETINT (val, XINT (value) < 0 ? -1 : 0); |
| 2829 | else |
| 2830 | XSETINT (val, XINT (value) >> -XINT (count)); |
| 2831 | return val; |
| 2832 | } |
| 2833 | |
| 2834 | DEFUN ("lsh", Flsh, Slsh, 2, 2, 0, |
| 2835 | doc: /* Return VALUE with its bits shifted left by COUNT. |
| 2836 | If COUNT is negative, shifting is actually to the right. |
| 2837 | In this case, zeros are shifted in on the left. */) |
| 2838 | (register Lisp_Object value, Lisp_Object count) |
| 2839 | { |
| 2840 | register Lisp_Object val; |
| 2841 | |
| 2842 | CHECK_NUMBER (value); |
| 2843 | CHECK_NUMBER (count); |
| 2844 | |
| 2845 | if (XINT (count) >= BITS_PER_EMACS_INT) |
| 2846 | XSETINT (val, 0); |
| 2847 | else if (XINT (count) > 0) |
| 2848 | XSETINT (val, XUINT (value) << XFASTINT (count)); |
| 2849 | else if (XINT (count) <= -BITS_PER_EMACS_INT) |
| 2850 | XSETINT (val, 0); |
| 2851 | else |
| 2852 | XSETINT (val, XUINT (value) >> -XINT (count)); |
| 2853 | return val; |
| 2854 | } |
| 2855 | |
| 2856 | DEFUN ("1+", Fadd1, Sadd1, 1, 1, 0, |
| 2857 | doc: /* Return NUMBER plus one. NUMBER may be a number or a marker. |
| 2858 | Markers are converted to integers. */) |
| 2859 | (register Lisp_Object number) |
| 2860 | { |
| 2861 | CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number); |
| 2862 | |
| 2863 | if (FLOATP (number)) |
| 2864 | return (make_float (1.0 + XFLOAT_DATA (number))); |
| 2865 | |
| 2866 | XSETINT (number, XINT (number) + 1); |
| 2867 | return number; |
| 2868 | } |
| 2869 | |
| 2870 | DEFUN ("1-", Fsub1, Ssub1, 1, 1, 0, |
| 2871 | doc: /* Return NUMBER minus one. NUMBER may be a number or a marker. |
| 2872 | Markers are converted to integers. */) |
| 2873 | (register Lisp_Object number) |
| 2874 | { |
| 2875 | CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number); |
| 2876 | |
| 2877 | if (FLOATP (number)) |
| 2878 | return (make_float (-1.0 + XFLOAT_DATA (number))); |
| 2879 | |
| 2880 | XSETINT (number, XINT (number) - 1); |
| 2881 | return number; |
| 2882 | } |
| 2883 | |
| 2884 | DEFUN ("lognot", Flognot, Slognot, 1, 1, 0, |
| 2885 | doc: /* Return the bitwise complement of NUMBER. NUMBER must be an integer. */) |
| 2886 | (register Lisp_Object number) |
| 2887 | { |
| 2888 | CHECK_NUMBER (number); |
| 2889 | XSETINT (number, ~XINT (number)); |
| 2890 | return number; |
| 2891 | } |
| 2892 | |
| 2893 | DEFUN ("byteorder", Fbyteorder, Sbyteorder, 0, 0, 0, |
| 2894 | doc: /* Return the byteorder for the machine. |
| 2895 | Returns 66 (ASCII uppercase B) for big endian machines or 108 (ASCII |
| 2896 | lowercase l) for small endian machines. */) |
| 2897 | (void) |
| 2898 | { |
| 2899 | unsigned i = 0x04030201; |
| 2900 | int order = *(char *)&i == 1 ? 108 : 66; |
| 2901 | |
| 2902 | return make_number (order); |
| 2903 | } |
| 2904 | |
| 2905 | |
| 2906 | \f |
| 2907 | void |
| 2908 | syms_of_data (void) |
| 2909 | { |
| 2910 | Lisp_Object error_tail, arith_tail; |
| 2911 | |
| 2912 | DEFSYM (Qquote, "quote"); |
| 2913 | DEFSYM (Qlambda, "lambda"); |
| 2914 | DEFSYM (Qsubr, "subr"); |
| 2915 | DEFSYM (Qerror_conditions, "error-conditions"); |
| 2916 | DEFSYM (Qerror_message, "error-message"); |
| 2917 | DEFSYM (Qtop_level, "top-level"); |
| 2918 | |
| 2919 | DEFSYM (Qerror, "error"); |
| 2920 | DEFSYM (Quser_error, "user-error"); |
| 2921 | DEFSYM (Qquit, "quit"); |
| 2922 | DEFSYM (Qwrong_type_argument, "wrong-type-argument"); |
| 2923 | DEFSYM (Qargs_out_of_range, "args-out-of-range"); |
| 2924 | DEFSYM (Qvoid_function, "void-function"); |
| 2925 | DEFSYM (Qcyclic_function_indirection, "cyclic-function-indirection"); |
| 2926 | DEFSYM (Qcyclic_variable_indirection, "cyclic-variable-indirection"); |
| 2927 | DEFSYM (Qvoid_variable, "void-variable"); |
| 2928 | DEFSYM (Qsetting_constant, "setting-constant"); |
| 2929 | DEFSYM (Qinvalid_read_syntax, "invalid-read-syntax"); |
| 2930 | |
| 2931 | DEFSYM (Qinvalid_function, "invalid-function"); |
| 2932 | DEFSYM (Qwrong_number_of_arguments, "wrong-number-of-arguments"); |
| 2933 | DEFSYM (Qno_catch, "no-catch"); |
| 2934 | DEFSYM (Qend_of_file, "end-of-file"); |
| 2935 | DEFSYM (Qarith_error, "arith-error"); |
| 2936 | DEFSYM (Qbeginning_of_buffer, "beginning-of-buffer"); |
| 2937 | DEFSYM (Qend_of_buffer, "end-of-buffer"); |
| 2938 | DEFSYM (Qbuffer_read_only, "buffer-read-only"); |
| 2939 | DEFSYM (Qtext_read_only, "text-read-only"); |
| 2940 | DEFSYM (Qmark_inactive, "mark-inactive"); |
| 2941 | |
| 2942 | DEFSYM (Qlistp, "listp"); |
| 2943 | DEFSYM (Qconsp, "consp"); |
| 2944 | DEFSYM (Qsymbolp, "symbolp"); |
| 2945 | DEFSYM (Qkeywordp, "keywordp"); |
| 2946 | DEFSYM (Qintegerp, "integerp"); |
| 2947 | DEFSYM (Qnatnump, "natnump"); |
| 2948 | DEFSYM (Qwholenump, "wholenump"); |
| 2949 | DEFSYM (Qstringp, "stringp"); |
| 2950 | DEFSYM (Qarrayp, "arrayp"); |
| 2951 | DEFSYM (Qsequencep, "sequencep"); |
| 2952 | DEFSYM (Qbufferp, "bufferp"); |
| 2953 | DEFSYM (Qvectorp, "vectorp"); |
| 2954 | DEFSYM (Qchar_or_string_p, "char-or-string-p"); |
| 2955 | DEFSYM (Qmarkerp, "markerp"); |
| 2956 | DEFSYM (Qbuffer_or_string_p, "buffer-or-string-p"); |
| 2957 | DEFSYM (Qinteger_or_marker_p, "integer-or-marker-p"); |
| 2958 | DEFSYM (Qboundp, "boundp"); |
| 2959 | DEFSYM (Qfboundp, "fboundp"); |
| 2960 | |
| 2961 | DEFSYM (Qfloatp, "floatp"); |
| 2962 | DEFSYM (Qnumberp, "numberp"); |
| 2963 | DEFSYM (Qnumber_or_marker_p, "number-or-marker-p"); |
| 2964 | |
| 2965 | DEFSYM (Qchar_table_p, "char-table-p"); |
| 2966 | DEFSYM (Qvector_or_char_table_p, "vector-or-char-table-p"); |
| 2967 | |
| 2968 | DEFSYM (Qsubrp, "subrp"); |
| 2969 | DEFSYM (Qunevalled, "unevalled"); |
| 2970 | DEFSYM (Qmany, "many"); |
| 2971 | |
| 2972 | DEFSYM (Qcdr, "cdr"); |
| 2973 | |
| 2974 | /* Handle automatic advice activation. */ |
| 2975 | DEFSYM (Qad_advice_info, "ad-advice-info"); |
| 2976 | DEFSYM (Qad_activate_internal, "ad-activate-internal"); |
| 2977 | |
| 2978 | error_tail = pure_cons (Qerror, Qnil); |
| 2979 | |
| 2980 | /* ERROR is used as a signaler for random errors for which nothing else is |
| 2981 | right. */ |
| 2982 | |
| 2983 | Fput (Qerror, Qerror_conditions, |
| 2984 | error_tail); |
| 2985 | Fput (Qerror, Qerror_message, |
| 2986 | build_pure_c_string ("error")); |
| 2987 | |
| 2988 | #define PUT_ERROR(sym, tail, msg) \ |
| 2989 | Fput (sym, Qerror_conditions, pure_cons (sym, tail)); \ |
| 2990 | Fput (sym, Qerror_message, build_pure_c_string (msg)) |
| 2991 | |
| 2992 | PUT_ERROR (Qquit, Qnil, "Quit"); |
| 2993 | |
| 2994 | PUT_ERROR (Quser_error, error_tail, ""); |
| 2995 | PUT_ERROR (Qwrong_type_argument, error_tail, "Wrong type argument"); |
| 2996 | PUT_ERROR (Qargs_out_of_range, error_tail, "Args out of range"); |
| 2997 | PUT_ERROR (Qvoid_function, error_tail, |
| 2998 | "Symbol's function definition is void"); |
| 2999 | PUT_ERROR (Qcyclic_function_indirection, error_tail, |
| 3000 | "Symbol's chain of function indirections contains a loop"); |
| 3001 | PUT_ERROR (Qcyclic_variable_indirection, error_tail, |
| 3002 | "Symbol's chain of variable indirections contains a loop"); |
| 3003 | DEFSYM (Qcircular_list, "circular-list"); |
| 3004 | PUT_ERROR (Qcircular_list, error_tail, "List contains a loop"); |
| 3005 | PUT_ERROR (Qvoid_variable, error_tail, "Symbol's value as variable is void"); |
| 3006 | PUT_ERROR (Qsetting_constant, error_tail, |
| 3007 | "Attempt to set a constant symbol"); |
| 3008 | PUT_ERROR (Qinvalid_read_syntax, error_tail, "Invalid read syntax"); |
| 3009 | PUT_ERROR (Qinvalid_function, error_tail, "Invalid function"); |
| 3010 | PUT_ERROR (Qwrong_number_of_arguments, error_tail, |
| 3011 | "Wrong number of arguments"); |
| 3012 | PUT_ERROR (Qno_catch, error_tail, "No catch for tag"); |
| 3013 | PUT_ERROR (Qend_of_file, error_tail, "End of file during parsing"); |
| 3014 | |
| 3015 | arith_tail = pure_cons (Qarith_error, error_tail); |
| 3016 | Fput (Qarith_error, Qerror_conditions, arith_tail); |
| 3017 | Fput (Qarith_error, Qerror_message, build_pure_c_string ("Arithmetic error")); |
| 3018 | |
| 3019 | PUT_ERROR (Qbeginning_of_buffer, error_tail, "Beginning of buffer"); |
| 3020 | PUT_ERROR (Qend_of_buffer, error_tail, "End of buffer"); |
| 3021 | PUT_ERROR (Qbuffer_read_only, error_tail, "Buffer is read-only"); |
| 3022 | PUT_ERROR (Qtext_read_only, pure_cons (Qbuffer_read_only, error_tail), |
| 3023 | "Text is read-only"); |
| 3024 | |
| 3025 | DEFSYM (Qrange_error, "range-error"); |
| 3026 | DEFSYM (Qdomain_error, "domain-error"); |
| 3027 | DEFSYM (Qsingularity_error, "singularity-error"); |
| 3028 | DEFSYM (Qoverflow_error, "overflow-error"); |
| 3029 | DEFSYM (Qunderflow_error, "underflow-error"); |
| 3030 | |
| 3031 | PUT_ERROR (Qdomain_error, arith_tail, "Arithmetic domain error"); |
| 3032 | |
| 3033 | PUT_ERROR (Qrange_error, arith_tail, "Arithmetic range error"); |
| 3034 | |
| 3035 | PUT_ERROR (Qsingularity_error, Fcons (Qdomain_error, arith_tail), |
| 3036 | "Arithmetic singularity error"); |
| 3037 | |
| 3038 | PUT_ERROR (Qoverflow_error, Fcons (Qdomain_error, arith_tail), |
| 3039 | "Arithmetic overflow error"); |
| 3040 | PUT_ERROR (Qunderflow_error, Fcons (Qdomain_error, arith_tail), |
| 3041 | "Arithmetic underflow error"); |
| 3042 | |
| 3043 | staticpro (&Qnil); |
| 3044 | staticpro (&Qt); |
| 3045 | staticpro (&Qunbound); |
| 3046 | |
| 3047 | /* Types that type-of returns. */ |
| 3048 | DEFSYM (Qinteger, "integer"); |
| 3049 | DEFSYM (Qsymbol, "symbol"); |
| 3050 | DEFSYM (Qstring, "string"); |
| 3051 | DEFSYM (Qcons, "cons"); |
| 3052 | DEFSYM (Qmarker, "marker"); |
| 3053 | DEFSYM (Qoverlay, "overlay"); |
| 3054 | DEFSYM (Qfloat, "float"); |
| 3055 | DEFSYM (Qwindow_configuration, "window-configuration"); |
| 3056 | DEFSYM (Qprocess, "process"); |
| 3057 | DEFSYM (Qwindow, "window"); |
| 3058 | DEFSYM (Qcompiled_function, "compiled-function"); |
| 3059 | DEFSYM (Qbuffer, "buffer"); |
| 3060 | DEFSYM (Qframe, "frame"); |
| 3061 | DEFSYM (Qvector, "vector"); |
| 3062 | DEFSYM (Qchar_table, "char-table"); |
| 3063 | DEFSYM (Qbool_vector, "bool-vector"); |
| 3064 | DEFSYM (Qhash_table, "hash-table"); |
| 3065 | DEFSYM (Qmisc, "misc"); |
| 3066 | |
| 3067 | DEFSYM (Qdefun, "defun"); |
| 3068 | |
| 3069 | DEFSYM (Qfont_spec, "font-spec"); |
| 3070 | DEFSYM (Qfont_entity, "font-entity"); |
| 3071 | DEFSYM (Qfont_object, "font-object"); |
| 3072 | |
| 3073 | DEFSYM (Qinteractive_form, "interactive-form"); |
| 3074 | |
| 3075 | defsubr (&Sindirect_variable); |
| 3076 | defsubr (&Sinteractive_form); |
| 3077 | defsubr (&Seq); |
| 3078 | defsubr (&Snull); |
| 3079 | defsubr (&Stype_of); |
| 3080 | defsubr (&Slistp); |
| 3081 | defsubr (&Snlistp); |
| 3082 | defsubr (&Sconsp); |
| 3083 | defsubr (&Satom); |
| 3084 | defsubr (&Sintegerp); |
| 3085 | defsubr (&Sinteger_or_marker_p); |
| 3086 | defsubr (&Snumberp); |
| 3087 | defsubr (&Snumber_or_marker_p); |
| 3088 | defsubr (&Sfloatp); |
| 3089 | defsubr (&Snatnump); |
| 3090 | defsubr (&Ssymbolp); |
| 3091 | defsubr (&Skeywordp); |
| 3092 | defsubr (&Sstringp); |
| 3093 | defsubr (&Smultibyte_string_p); |
| 3094 | defsubr (&Svectorp); |
| 3095 | defsubr (&Schar_table_p); |
| 3096 | defsubr (&Svector_or_char_table_p); |
| 3097 | defsubr (&Sbool_vector_p); |
| 3098 | defsubr (&Sarrayp); |
| 3099 | defsubr (&Ssequencep); |
| 3100 | defsubr (&Sbufferp); |
| 3101 | defsubr (&Smarkerp); |
| 3102 | defsubr (&Ssubrp); |
| 3103 | defsubr (&Sbyte_code_function_p); |
| 3104 | defsubr (&Schar_or_string_p); |
| 3105 | defsubr (&Scar); |
| 3106 | defsubr (&Scdr); |
| 3107 | defsubr (&Scar_safe); |
| 3108 | defsubr (&Scdr_safe); |
| 3109 | defsubr (&Ssetcar); |
| 3110 | defsubr (&Ssetcdr); |
| 3111 | defsubr (&Ssymbol_function); |
| 3112 | defsubr (&Sindirect_function); |
| 3113 | defsubr (&Ssymbol_plist); |
| 3114 | defsubr (&Ssymbol_name); |
| 3115 | defsubr (&Smakunbound); |
| 3116 | defsubr (&Sfmakunbound); |
| 3117 | defsubr (&Sboundp); |
| 3118 | defsubr (&Sfboundp); |
| 3119 | defsubr (&Sfset); |
| 3120 | defsubr (&Sdefalias); |
| 3121 | defsubr (&Ssetplist); |
| 3122 | defsubr (&Ssymbol_value); |
| 3123 | defsubr (&Sset); |
| 3124 | defsubr (&Sdefault_boundp); |
| 3125 | defsubr (&Sdefault_value); |
| 3126 | defsubr (&Sset_default); |
| 3127 | defsubr (&Ssetq_default); |
| 3128 | defsubr (&Smake_variable_buffer_local); |
| 3129 | defsubr (&Smake_local_variable); |
| 3130 | defsubr (&Skill_local_variable); |
| 3131 | defsubr (&Smake_variable_frame_local); |
| 3132 | defsubr (&Slocal_variable_p); |
| 3133 | defsubr (&Slocal_variable_if_set_p); |
| 3134 | defsubr (&Svariable_binding_locus); |
| 3135 | #if 0 /* XXX Remove this. --lorentey */ |
| 3136 | defsubr (&Sterminal_local_value); |
| 3137 | defsubr (&Sset_terminal_local_value); |
| 3138 | #endif |
| 3139 | defsubr (&Saref); |
| 3140 | defsubr (&Saset); |
| 3141 | defsubr (&Snumber_to_string); |
| 3142 | defsubr (&Sstring_to_number); |
| 3143 | defsubr (&Seqlsign); |
| 3144 | defsubr (&Slss); |
| 3145 | defsubr (&Sgtr); |
| 3146 | defsubr (&Sleq); |
| 3147 | defsubr (&Sgeq); |
| 3148 | defsubr (&Sneq); |
| 3149 | defsubr (&Szerop); |
| 3150 | defsubr (&Splus); |
| 3151 | defsubr (&Sminus); |
| 3152 | defsubr (&Stimes); |
| 3153 | defsubr (&Squo); |
| 3154 | defsubr (&Srem); |
| 3155 | defsubr (&Smod); |
| 3156 | defsubr (&Smax); |
| 3157 | defsubr (&Smin); |
| 3158 | defsubr (&Slogand); |
| 3159 | defsubr (&Slogior); |
| 3160 | defsubr (&Slogxor); |
| 3161 | defsubr (&Slsh); |
| 3162 | defsubr (&Sash); |
| 3163 | defsubr (&Sadd1); |
| 3164 | defsubr (&Ssub1); |
| 3165 | defsubr (&Slognot); |
| 3166 | defsubr (&Sbyteorder); |
| 3167 | defsubr (&Ssubr_arity); |
| 3168 | defsubr (&Ssubr_name); |
| 3169 | |
| 3170 | set_symbol_function (Qwholenump, XSYMBOL (Qnatnump)->function); |
| 3171 | |
| 3172 | DEFVAR_LISP ("most-positive-fixnum", Vmost_positive_fixnum, |
| 3173 | doc: /* The largest value that is representable in a Lisp integer. */); |
| 3174 | Vmost_positive_fixnum = make_number (MOST_POSITIVE_FIXNUM); |
| 3175 | XSYMBOL (intern_c_string ("most-positive-fixnum"))->constant = 1; |
| 3176 | |
| 3177 | DEFVAR_LISP ("most-negative-fixnum", Vmost_negative_fixnum, |
| 3178 | doc: /* The smallest value that is representable in a Lisp integer. */); |
| 3179 | Vmost_negative_fixnum = make_number (MOST_NEGATIVE_FIXNUM); |
| 3180 | XSYMBOL (intern_c_string ("most-negative-fixnum"))->constant = 1; |
| 3181 | } |
| 3182 | |
| 3183 | static _Noreturn void |
| 3184 | handle_arith_signal (int sig) |
| 3185 | { |
| 3186 | pthread_sigmask (SIG_SETMASK, &empty_mask, 0); |
| 3187 | xsignal0 (Qarith_error); |
| 3188 | } |
| 3189 | |
| 3190 | static void |
| 3191 | deliver_arith_signal (int sig) |
| 3192 | { |
| 3193 | handle_on_main_thread (sig, handle_arith_signal); |
| 3194 | } |
| 3195 | |
| 3196 | void |
| 3197 | init_data (void) |
| 3198 | { |
| 3199 | struct sigaction action; |
| 3200 | /* Don't do this if just dumping out. |
| 3201 | We don't want to call `signal' in this case |
| 3202 | so that we don't have trouble with dumping |
| 3203 | signal-delivering routines in an inconsistent state. */ |
| 3204 | #ifndef CANNOT_DUMP |
| 3205 | if (!initialized) |
| 3206 | return; |
| 3207 | #endif /* CANNOT_DUMP */ |
| 3208 | emacs_sigaction_init (&action, deliver_arith_signal); |
| 3209 | sigaction (SIGFPE, &action, 0); |
| 3210 | } |