428
+ − 1 /* Random utility Lisp functions.
+ − 2 Copyright (C) 1985, 86, 87, 93, 94, 95 Free Software Foundation, Inc.
1261
+ − 3 Copyright (C) 1995, 1996, 2000, 2001, 2002, 2003 Ben Wing.
428
+ − 4
+ − 5 This file is part of XEmacs.
+ − 6
+ − 7 XEmacs is free software; you can redistribute it and/or modify it
+ − 8 under the terms of the GNU General Public License as published by the
+ − 9 Free Software Foundation; either version 2, or (at your option) any
+ − 10 later version.
+ − 11
+ − 12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
+ − 13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ − 14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ − 15 for more details.
+ − 16
+ − 17 You should have received a copy of the GNU General Public License
+ − 18 along with XEmacs; see the file COPYING. If not, write to
+ − 19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ − 20 Boston, MA 02111-1307, USA. */
+ − 21
+ − 22 /* Synched up with: Mule 2.0, FSF 19.30. */
+ − 23
+ − 24 /* This file has been Mule-ized. */
+ − 25
+ − 26 /* Note: FSF 19.30 has bool vectors. We have bit vectors. */
+ − 27
+ − 28 /* Hacked on for Mule by Ben Wing, December 1994, January 1995. */
+ − 29
+ − 30 #include <config.h>
+ − 31
+ − 32 /* Note on some machines this defines `vector' as a typedef,
+ − 33 so make sure we don't use that name in this file. */
+ − 34 #undef vector
+ − 35 #define vector *****
+ − 36
+ − 37 #include "lisp.h"
+ − 38
442
+ − 39 #include "sysfile.h"
771
+ − 40 #include "sysproc.h" /* for qxe_getpid() */
428
+ − 41
+ − 42 #include "buffer.h"
+ − 43 #include "bytecode.h"
+ − 44 #include "device.h"
+ − 45 #include "events.h"
+ − 46 #include "extents.h"
+ − 47 #include "frame.h"
872
+ − 48 #include "process.h"
428
+ − 49 #include "systime.h"
+ − 50 #include "insdel.h"
+ − 51 #include "lstream.h"
+ − 52 #include "opaque.h"
+ − 53
+ − 54 /* NOTE: This symbol is also used in lread.c */
+ − 55 #define FEATUREP_SYNTAX
+ − 56
+ − 57 Lisp_Object Qstring_lessp;
+ − 58 Lisp_Object Qidentity;
+ − 59
563
+ − 60 Lisp_Object Qbase64_conversion_error;
+ − 61
771
+ − 62 Lisp_Object Vpath_separator;
+ − 63
428
+ − 64 static int internal_old_equal (Lisp_Object, Lisp_Object, int);
454
+ − 65 Lisp_Object safe_copy_tree (Lisp_Object arg, Lisp_Object vecp, int depth);
428
+ − 66
+ − 67 static Lisp_Object
2286
+ − 68 mark_bit_vector (Lisp_Object UNUSED (obj))
428
+ − 69 {
+ − 70 return Qnil;
+ − 71 }
+ − 72
+ − 73 static void
2286
+ − 74 print_bit_vector (Lisp_Object obj, Lisp_Object printcharfun,
+ − 75 int UNUSED (escapeflag))
428
+ − 76 {
665
+ − 77 Elemcount i;
440
+ − 78 Lisp_Bit_Vector *v = XBIT_VECTOR (obj);
665
+ − 79 Elemcount len = bit_vector_length (v);
+ − 80 Elemcount last = len;
428
+ − 81
+ − 82 if (INTP (Vprint_length))
+ − 83 last = min (len, XINT (Vprint_length));
826
+ − 84 write_c_string (printcharfun, "#*");
428
+ − 85 for (i = 0; i < last; i++)
+ − 86 {
+ − 87 if (bit_vector_bit (v, i))
826
+ − 88 write_c_string (printcharfun, "1");
428
+ − 89 else
826
+ − 90 write_c_string (printcharfun, "0");
428
+ − 91 }
+ − 92
+ − 93 if (last != len)
826
+ − 94 write_c_string (printcharfun, "...");
428
+ − 95 }
+ − 96
+ − 97 static int
2286
+ − 98 bit_vector_equal (Lisp_Object obj1, Lisp_Object obj2, int UNUSED (depth))
428
+ − 99 {
440
+ − 100 Lisp_Bit_Vector *v1 = XBIT_VECTOR (obj1);
+ − 101 Lisp_Bit_Vector *v2 = XBIT_VECTOR (obj2);
428
+ − 102
+ − 103 return ((bit_vector_length (v1) == bit_vector_length (v2)) &&
+ − 104 !memcmp (v1->bits, v2->bits,
+ − 105 BIT_VECTOR_LONG_STORAGE (bit_vector_length (v1)) *
+ − 106 sizeof (long)));
+ − 107 }
+ − 108
665
+ − 109 static Hashcode
2286
+ − 110 bit_vector_hash (Lisp_Object obj, int UNUSED (depth))
428
+ − 111 {
440
+ − 112 Lisp_Bit_Vector *v = XBIT_VECTOR (obj);
428
+ − 113 return HASH2 (bit_vector_length (v),
+ − 114 memory_hash (v->bits,
+ − 115 BIT_VECTOR_LONG_STORAGE (bit_vector_length (v)) *
+ − 116 sizeof (long)));
+ − 117 }
+ − 118
665
+ − 119 static Bytecount
442
+ − 120 size_bit_vector (const void *lheader)
+ − 121 {
+ − 122 Lisp_Bit_Vector *v = (Lisp_Bit_Vector *) lheader;
456
+ − 123 return FLEXIBLE_ARRAY_STRUCT_SIZEOF (Lisp_Bit_Vector, unsigned long, bits,
442
+ − 124 BIT_VECTOR_LONG_STORAGE (bit_vector_length (v)));
+ − 125 }
+ − 126
1204
+ − 127 static const struct memory_description bit_vector_description[] = {
428
+ − 128 { XD_END }
+ − 129 };
+ − 130
+ − 131
1204
+ − 132 DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION ("bit-vector", bit_vector,
+ − 133 1, /*dumpable-flag*/
+ − 134 mark_bit_vector,
+ − 135 print_bit_vector, 0,
+ − 136 bit_vector_equal,
+ − 137 bit_vector_hash,
+ − 138 bit_vector_description,
+ − 139 size_bit_vector,
+ − 140 Lisp_Bit_Vector);
934
+ − 141
428
+ − 142
+ − 143 DEFUN ("identity", Fidentity, 1, 1, 0, /*
+ − 144 Return the argument unchanged.
+ − 145 */
+ − 146 (arg))
+ − 147 {
+ − 148 return arg;
+ − 149 }
+ − 150
+ − 151 DEFUN ("random", Frandom, 0, 1, 0, /*
+ − 152 Return a pseudo-random number.
1983
+ − 153 All fixnums are equally likely. On most systems, this is 31 bits' worth.
428
+ − 154 With positive integer argument N, return random number in interval [0,N).
1983
+ − 155 N can be a bignum, in which case the range of possible values is extended.
428
+ − 156 With argument t, set the random number seed from the current time and pid.
+ − 157 */
+ − 158 (limit))
+ − 159 {
+ − 160 EMACS_INT val;
+ − 161 unsigned long denominator;
+ − 162
+ − 163 if (EQ (limit, Qt))
771
+ − 164 seed_random (qxe_getpid () + time (NULL));
428
+ − 165 if (NATNUMP (limit) && !ZEROP (limit))
+ − 166 {
+ − 167 /* Try to take our random number from the higher bits of VAL,
+ − 168 not the lower, since (says Gentzel) the low bits of `random'
+ − 169 are less random than the higher ones. We do this by using the
+ − 170 quotient rather than the remainder. At the high end of the RNG
+ − 171 it's possible to get a quotient larger than limit; discarding
+ − 172 these values eliminates the bias that would otherwise appear
+ − 173 when using a large limit. */
2039
+ − 174 denominator = ((unsigned long)1 << INT_VALBITS) / XINT (limit);
428
+ − 175 do
+ − 176 val = get_random () / denominator;
+ − 177 while (val >= XINT (limit));
+ − 178 }
1983
+ − 179 #ifdef HAVE_BIGNUM
+ − 180 else if (BIGNUMP (limit))
+ − 181 {
+ − 182 bignum_random (scratch_bignum, XBIGNUM_DATA (limit));
+ − 183 return Fcanonicalize_number (make_bignum_bg (scratch_bignum));
+ − 184 }
+ − 185 #endif
428
+ − 186 else
+ − 187 val = get_random ();
+ − 188
+ − 189 return make_int (val);
+ − 190 }
+ − 191
+ − 192 /* Random data-structure functions */
+ − 193
+ − 194 #ifdef LOSING_BYTECODE
+ − 195
+ − 196 /* #### Delete this shit */
+ − 197
+ − 198 /* Charcount is a misnomer here as we might be dealing with the
+ − 199 length of a vector or list, but emphasizes that we're not dealing
+ − 200 with Bytecounts in strings */
+ − 201 static Charcount
+ − 202 length_with_bytecode_hack (Lisp_Object seq)
+ − 203 {
+ − 204 if (!COMPILED_FUNCTIONP (seq))
+ − 205 return XINT (Flength (seq));
+ − 206 else
+ − 207 {
440
+ − 208 Lisp_Compiled_Function *f = XCOMPILED_FUNCTION (seq);
428
+ − 209
+ − 210 return (f->flags.interactivep ? COMPILED_INTERACTIVE :
+ − 211 f->flags.domainp ? COMPILED_DOMAIN :
+ − 212 COMPILED_DOC_STRING)
+ − 213 + 1;
+ − 214 }
+ − 215 }
+ − 216
+ − 217 #endif /* LOSING_BYTECODE */
+ − 218
+ − 219 void
442
+ − 220 check_losing_bytecode (const char *function, Lisp_Object seq)
428
+ − 221 {
+ − 222 if (COMPILED_FUNCTIONP (seq))
563
+ − 223 signal_ferror_with_frob
+ − 224 (Qinvalid_argument, seq,
428
+ − 225 "As of 20.3, `%s' no longer works with compiled-function objects",
+ − 226 function);
+ − 227 }
+ − 228
+ − 229 DEFUN ("length", Flength, 1, 1, 0, /*
+ − 230 Return the length of vector, bit vector, list or string SEQUENCE.
+ − 231 */
+ − 232 (sequence))
+ − 233 {
+ − 234 retry:
+ − 235 if (STRINGP (sequence))
826
+ − 236 return make_int (string_char_length (sequence));
428
+ − 237 else if (CONSP (sequence))
+ − 238 {
665
+ − 239 Elemcount len;
428
+ − 240 GET_EXTERNAL_LIST_LENGTH (sequence, len);
+ − 241 return make_int (len);
+ − 242 }
+ − 243 else if (VECTORP (sequence))
+ − 244 return make_int (XVECTOR_LENGTH (sequence));
+ − 245 else if (NILP (sequence))
+ − 246 return Qzero;
+ − 247 else if (BIT_VECTORP (sequence))
+ − 248 return make_int (bit_vector_length (XBIT_VECTOR (sequence)));
+ − 249 else
+ − 250 {
+ − 251 check_losing_bytecode ("length", sequence);
+ − 252 sequence = wrong_type_argument (Qsequencep, sequence);
+ − 253 goto retry;
+ − 254 }
+ − 255 }
+ − 256
+ − 257 DEFUN ("safe-length", Fsafe_length, 1, 1, 0, /*
+ − 258 Return the length of a list, but avoid error or infinite loop.
+ − 259 This function never gets an error. If LIST is not really a list,
+ − 260 it returns 0. If LIST is circular, it returns a finite value
+ − 261 which is at least the number of distinct elements.
+ − 262 */
+ − 263 (list))
+ − 264 {
+ − 265 Lisp_Object hare, tortoise;
665
+ − 266 Elemcount len;
428
+ − 267
+ − 268 for (hare = tortoise = list, len = 0;
+ − 269 CONSP (hare) && (! EQ (hare, tortoise) || len == 0);
+ − 270 hare = XCDR (hare), len++)
+ − 271 {
+ − 272 if (len & 1)
+ − 273 tortoise = XCDR (tortoise);
+ − 274 }
+ − 275
+ − 276 return make_int (len);
+ − 277 }
+ − 278
+ − 279 /*** string functions. ***/
+ − 280
+ − 281 DEFUN ("string-equal", Fstring_equal, 2, 2, 0, /*
+ − 282 Return t if two strings have identical contents.
+ − 283 Case is significant. Text properties are ignored.
+ − 284 \(Under XEmacs, `equal' also ignores text properties and extents in
+ − 285 strings, but this is not the case under FSF Emacs 19. In FSF Emacs 20
+ − 286 `equal' is the same as in XEmacs, in that respect.)
+ − 287 Symbols are also allowed; their print names are used instead.
+ − 288 */
444
+ − 289 (string1, string2))
428
+ − 290 {
+ − 291 Bytecount len;
793
+ − 292 Lisp_Object p1, p2;
428
+ − 293
444
+ − 294 if (SYMBOLP (string1))
+ − 295 p1 = XSYMBOL (string1)->name;
428
+ − 296 else
+ − 297 {
444
+ − 298 CHECK_STRING (string1);
793
+ − 299 p1 = string1;
428
+ − 300 }
+ − 301
444
+ − 302 if (SYMBOLP (string2))
+ − 303 p2 = XSYMBOL (string2)->name;
428
+ − 304 else
+ − 305 {
444
+ − 306 CHECK_STRING (string2);
793
+ − 307 p2 = string2;
428
+ − 308 }
+ − 309
793
+ − 310 return (((len = XSTRING_LENGTH (p1)) == XSTRING_LENGTH (p2)) &&
+ − 311 !memcmp (XSTRING_DATA (p1), XSTRING_DATA (p2), len)) ? Qt : Qnil;
428
+ − 312 }
+ − 313
801
+ − 314 DEFUN ("compare-strings", Fcompare_strings, 6, 7, 0, /*
+ − 315 Compare the contents of two strings, maybe ignoring case.
+ − 316 In string STR1, skip the first START1 characters and stop at END1.
+ − 317 In string STR2, skip the first START2 characters and stop at END2.
+ − 318 END1 and END2 default to the full lengths of the respective strings.
+ − 319
+ − 320 Case is significant in this comparison if IGNORE-CASE is nil.
+ − 321
+ − 322 The value is t if the strings (or specified portions) match.
+ − 323 If string STR1 is less, the value is a negative number N;
+ − 324 - 1 - N is the number of characters that match at the beginning.
+ − 325 If string STR1 is greater, the value is a positive number N;
+ − 326 N - 1 is the number of characters that match at the beginning.
+ − 327 */
+ − 328 (str1, start1, end1, str2, start2, end2, ignore_case))
+ − 329 {
+ − 330 Charcount ccstart1, ccend1, ccstart2, ccend2;
+ − 331 Bytecount bstart1, blen1, bstart2, blen2;
+ − 332 Charcount matching;
+ − 333 int res;
+ − 334
+ − 335 CHECK_STRING (str1);
+ − 336 CHECK_STRING (str2);
+ − 337 get_string_range_char (str1, start1, end1, &ccstart1, &ccend1,
+ − 338 GB_HISTORICAL_STRING_BEHAVIOR);
+ − 339 get_string_range_char (str2, start2, end2, &ccstart2, &ccend2,
+ − 340 GB_HISTORICAL_STRING_BEHAVIOR);
+ − 341
+ − 342 bstart1 = string_index_char_to_byte (str1, ccstart1);
+ − 343 blen1 = string_offset_char_to_byte_len (str1, bstart1, ccend1 - ccstart1);
+ − 344 bstart2 = string_index_char_to_byte (str2, ccstart2);
+ − 345 blen2 = string_offset_char_to_byte_len (str2, bstart2, ccend2 - ccstart2);
+ − 346
+ − 347 res = ((NILP (ignore_case) ? qxetextcmp_matching : qxetextcasecmp_matching)
+ − 348 (XSTRING_DATA (str1) + bstart1, blen1,
+ − 349 XSTRING_DATA (str2) + bstart2, blen2,
+ − 350 &matching));
+ − 351
+ − 352 if (!res)
+ − 353 return Qt;
+ − 354 else if (res > 0)
+ − 355 return make_int (1 + matching);
+ − 356 else
+ − 357 return make_int (-1 - matching);
+ − 358 }
+ − 359
428
+ − 360 DEFUN ("string-lessp", Fstring_lessp, 2, 2, 0, /*
+ − 361 Return t if first arg string is less than second in lexicographic order.
771
+ − 362 Comparison is simply done on a character-by-character basis using the
+ − 363 numeric value of a character. (Note that this may not produce
+ − 364 particularly meaningful results under Mule if characters from
+ − 365 different charsets are being compared.)
428
+ − 366
+ − 367 Symbols are also allowed; their print names are used instead.
+ − 368
771
+ − 369 Currently we don't do proper language-specific collation or handle
+ − 370 multiple character sets. This may be changed when Unicode support
+ − 371 is implemented.
428
+ − 372 */
444
+ − 373 (string1, string2))
428
+ − 374 {
793
+ − 375 Lisp_Object p1, p2;
428
+ − 376 Charcount end, len2;
+ − 377 int i;
+ − 378
444
+ − 379 if (SYMBOLP (string1))
+ − 380 p1 = XSYMBOL (string1)->name;
793
+ − 381 else
+ − 382 {
444
+ − 383 CHECK_STRING (string1);
793
+ − 384 p1 = string1;
428
+ − 385 }
+ − 386
444
+ − 387 if (SYMBOLP (string2))
+ − 388 p2 = XSYMBOL (string2)->name;
428
+ − 389 else
+ − 390 {
444
+ − 391 CHECK_STRING (string2);
793
+ − 392 p2 = string2;
428
+ − 393 }
+ − 394
826
+ − 395 end = string_char_length (p1);
+ − 396 len2 = string_char_length (p2);
428
+ − 397 if (end > len2)
+ − 398 end = len2;
+ − 399
+ − 400 {
867
+ − 401 Ibyte *ptr1 = XSTRING_DATA (p1);
+ − 402 Ibyte *ptr2 = XSTRING_DATA (p2);
428
+ − 403
+ − 404 /* #### It is not really necessary to do this: We could compare
+ − 405 byte-by-byte and still get a reasonable comparison, since this
+ − 406 would compare characters with a charset in the same way. With
+ − 407 a little rearrangement of the leading bytes, we could make most
+ − 408 inter-charset comparisons work out the same, too; even if some
+ − 409 don't, this is not a big deal because inter-charset comparisons
+ − 410 aren't really well-defined anyway. */
+ − 411 for (i = 0; i < end; i++)
+ − 412 {
867
+ − 413 if (itext_ichar (ptr1) != itext_ichar (ptr2))
+ − 414 return itext_ichar (ptr1) < itext_ichar (ptr2) ? Qt : Qnil;
+ − 415 INC_IBYTEPTR (ptr1);
+ − 416 INC_IBYTEPTR (ptr2);
428
+ − 417 }
+ − 418 }
+ − 419 /* Can't do i < len2 because then comparison between "foo" and "foo^@"
+ − 420 won't work right in I18N2 case */
+ − 421 return end < len2 ? Qt : Qnil;
+ − 422 }
+ − 423
+ − 424 DEFUN ("string-modified-tick", Fstring_modified_tick, 1, 1, 0, /*
+ − 425 Return STRING's tick counter, incremented for each change to the string.
+ − 426 Each string has a tick counter which is incremented each time the contents
+ − 427 of the string are changed (e.g. with `aset'). It wraps around occasionally.
+ − 428 */
+ − 429 (string))
+ − 430 {
+ − 431 CHECK_STRING (string);
793
+ − 432 if (CONSP (XSTRING_PLIST (string)) && INTP (XCAR (XSTRING_PLIST (string))))
+ − 433 return XCAR (XSTRING_PLIST (string));
428
+ − 434 else
+ − 435 return Qzero;
+ − 436 }
+ − 437
+ − 438 void
+ − 439 bump_string_modiff (Lisp_Object str)
+ − 440 {
793
+ − 441 Lisp_Object *ptr = &XSTRING_PLIST (str);
428
+ − 442
+ − 443 #ifdef I18N3
+ − 444 /* #### remove the `string-translatable' property from the string,
+ − 445 if there is one. */
+ − 446 #endif
+ − 447 /* skip over extent info if it's there */
+ − 448 if (CONSP (*ptr) && EXTENT_INFOP (XCAR (*ptr)))
+ − 449 ptr = &XCDR (*ptr);
+ − 450 if (CONSP (*ptr) && INTP (XCAR (*ptr)))
793
+ − 451 XCAR (*ptr) = make_int (1+XINT (XCAR (*ptr)));
428
+ − 452 else
+ − 453 *ptr = Fcons (make_int (1), *ptr);
+ − 454 }
+ − 455
+ − 456
+ − 457 enum concat_target_type { c_cons, c_string, c_vector, c_bit_vector };
+ − 458 static Lisp_Object concat (int nargs, Lisp_Object *args,
+ − 459 enum concat_target_type target_type,
+ − 460 int last_special);
+ − 461
+ − 462 Lisp_Object
444
+ − 463 concat2 (Lisp_Object string1, Lisp_Object string2)
428
+ − 464 {
+ − 465 Lisp_Object args[2];
444
+ − 466 args[0] = string1;
+ − 467 args[1] = string2;
428
+ − 468 return concat (2, args, c_string, 0);
+ − 469 }
+ − 470
+ − 471 Lisp_Object
444
+ − 472 concat3 (Lisp_Object string1, Lisp_Object string2, Lisp_Object string3)
428
+ − 473 {
+ − 474 Lisp_Object args[3];
444
+ − 475 args[0] = string1;
+ − 476 args[1] = string2;
+ − 477 args[2] = string3;
428
+ − 478 return concat (3, args, c_string, 0);
+ − 479 }
+ − 480
+ − 481 Lisp_Object
444
+ − 482 vconcat2 (Lisp_Object vec1, Lisp_Object vec2)
428
+ − 483 {
+ − 484 Lisp_Object args[2];
444
+ − 485 args[0] = vec1;
+ − 486 args[1] = vec2;
428
+ − 487 return concat (2, args, c_vector, 0);
+ − 488 }
+ − 489
+ − 490 Lisp_Object
444
+ − 491 vconcat3 (Lisp_Object vec1, Lisp_Object vec2, Lisp_Object vec3)
428
+ − 492 {
+ − 493 Lisp_Object args[3];
444
+ − 494 args[0] = vec1;
+ − 495 args[1] = vec2;
+ − 496 args[2] = vec3;
428
+ − 497 return concat (3, args, c_vector, 0);
+ − 498 }
+ − 499
+ − 500 DEFUN ("append", Fappend, 0, MANY, 0, /*
+ − 501 Concatenate all the arguments and make the result a list.
+ − 502 The result is a list whose elements are the elements of all the arguments.
+ − 503 Each argument may be a list, vector, bit vector, or string.
+ − 504 The last argument is not copied, just used as the tail of the new list.
+ − 505 Also see: `nconc'.
+ − 506 */
+ − 507 (int nargs, Lisp_Object *args))
+ − 508 {
+ − 509 return concat (nargs, args, c_cons, 1);
+ − 510 }
+ − 511
+ − 512 DEFUN ("concat", Fconcat, 0, MANY, 0, /*
+ − 513 Concatenate all the arguments and make the result a string.
+ − 514 The result is a string whose elements are the elements of all the arguments.
+ − 515 Each argument may be a string or a list or vector of characters.
+ − 516
+ − 517 As of XEmacs 21.0, this function does NOT accept individual integers
+ − 518 as arguments. Old code that relies on, for example, (concat "foo" 50)
+ − 519 returning "foo50" will fail. To fix such code, either apply
+ − 520 `int-to-string' to the integer argument, or use `format'.
+ − 521 */
+ − 522 (int nargs, Lisp_Object *args))
+ − 523 {
+ − 524 return concat (nargs, args, c_string, 0);
+ − 525 }
+ − 526
+ − 527 DEFUN ("vconcat", Fvconcat, 0, MANY, 0, /*
+ − 528 Concatenate all the arguments and make the result a vector.
+ − 529 The result is a vector whose elements are the elements of all the arguments.
+ − 530 Each argument may be a list, vector, bit vector, or string.
+ − 531 */
+ − 532 (int nargs, Lisp_Object *args))
+ − 533 {
+ − 534 return concat (nargs, args, c_vector, 0);
+ − 535 }
+ − 536
+ − 537 DEFUN ("bvconcat", Fbvconcat, 0, MANY, 0, /*
+ − 538 Concatenate all the arguments and make the result a bit vector.
+ − 539 The result is a bit vector whose elements are the elements of all the
+ − 540 arguments. Each argument may be a list, vector, bit vector, or string.
+ − 541 */
+ − 542 (int nargs, Lisp_Object *args))
+ − 543 {
+ − 544 return concat (nargs, args, c_bit_vector, 0);
+ − 545 }
+ − 546
+ − 547 /* Copy a (possibly dotted) list. LIST must be a cons.
+ − 548 Can't use concat (1, &alist, c_cons, 0) - doesn't handle dotted lists. */
+ − 549 static Lisp_Object
+ − 550 copy_list (Lisp_Object list)
+ − 551 {
+ − 552 Lisp_Object list_copy = Fcons (XCAR (list), XCDR (list));
+ − 553 Lisp_Object last = list_copy;
+ − 554 Lisp_Object hare, tortoise;
665
+ − 555 Elemcount len;
428
+ − 556
+ − 557 for (tortoise = hare = XCDR (list), len = 1;
+ − 558 CONSP (hare);
+ − 559 hare = XCDR (hare), len++)
+ − 560 {
+ − 561 XCDR (last) = Fcons (XCAR (hare), XCDR (hare));
+ − 562 last = XCDR (last);
+ − 563
+ − 564 if (len < CIRCULAR_LIST_SUSPICION_LENGTH)
+ − 565 continue;
+ − 566 if (len & 1)
+ − 567 tortoise = XCDR (tortoise);
+ − 568 if (EQ (tortoise, hare))
+ − 569 signal_circular_list_error (list);
+ − 570 }
+ − 571
+ − 572 return list_copy;
+ − 573 }
+ − 574
+ − 575 DEFUN ("copy-list", Fcopy_list, 1, 1, 0, /*
+ − 576 Return a copy of list LIST, which may be a dotted list.
+ − 577 The elements of LIST are not copied; they are shared
+ − 578 with the original.
+ − 579 */
+ − 580 (list))
+ − 581 {
+ − 582 again:
+ − 583 if (NILP (list)) return list;
+ − 584 if (CONSP (list)) return copy_list (list);
+ − 585
+ − 586 list = wrong_type_argument (Qlistp, list);
+ − 587 goto again;
+ − 588 }
+ − 589
+ − 590 DEFUN ("copy-sequence", Fcopy_sequence, 1, 1, 0, /*
+ − 591 Return a copy of list, vector, bit vector or string SEQUENCE.
+ − 592 The elements of a list or vector are not copied; they are shared
+ − 593 with the original. SEQUENCE may be a dotted list.
+ − 594 */
+ − 595 (sequence))
+ − 596 {
+ − 597 again:
+ − 598 if (NILP (sequence)) return sequence;
+ − 599 if (CONSP (sequence)) return copy_list (sequence);
+ − 600 if (STRINGP (sequence)) return concat (1, &sequence, c_string, 0);
+ − 601 if (VECTORP (sequence)) return concat (1, &sequence, c_vector, 0);
+ − 602 if (BIT_VECTORP (sequence)) return concat (1, &sequence, c_bit_vector, 0);
+ − 603
+ − 604 check_losing_bytecode ("copy-sequence", sequence);
+ − 605 sequence = wrong_type_argument (Qsequencep, sequence);
+ − 606 goto again;
+ − 607 }
+ − 608
+ − 609 struct merge_string_extents_struct
+ − 610 {
+ − 611 Lisp_Object string;
+ − 612 Bytecount entry_offset;
+ − 613 Bytecount entry_length;
+ − 614 };
+ − 615
+ − 616 static Lisp_Object
+ − 617 concat (int nargs, Lisp_Object *args,
+ − 618 enum concat_target_type target_type,
+ − 619 int last_special)
+ − 620 {
+ − 621 Lisp_Object val;
+ − 622 Lisp_Object tail = Qnil;
+ − 623 int toindex;
+ − 624 int argnum;
+ − 625 Lisp_Object last_tail;
+ − 626 Lisp_Object prev;
+ − 627 struct merge_string_extents_struct *args_mse = 0;
867
+ − 628 Ibyte *string_result = 0;
+ − 629 Ibyte *string_result_ptr = 0;
428
+ − 630 struct gcpro gcpro1;
851
+ − 631 int sdep = specpdl_depth ();
428
+ − 632
+ − 633 /* The modus operandi in Emacs is "caller gc-protects args".
+ − 634 However, concat is called many times in Emacs on freshly
+ − 635 created stuff. So we help those callers out by protecting
+ − 636 the args ourselves to save them a lot of temporary-variable
+ − 637 grief. */
+ − 638
+ − 639 GCPRO1 (args[0]);
+ − 640 gcpro1.nvars = nargs;
+ − 641
+ − 642 #ifdef I18N3
+ − 643 /* #### if the result is a string and any of the strings have a string
+ − 644 for the `string-translatable' property, then concat should also
+ − 645 concat the args but use the `string-translatable' strings, and store
+ − 646 the result in the returned string's `string-translatable' property. */
+ − 647 #endif
+ − 648 if (target_type == c_string)
+ − 649 args_mse = alloca_array (struct merge_string_extents_struct, nargs);
+ − 650
+ − 651 /* In append, the last arg isn't treated like the others */
+ − 652 if (last_special && nargs > 0)
+ − 653 {
+ − 654 nargs--;
+ − 655 last_tail = args[nargs];
+ − 656 }
+ − 657 else
+ − 658 last_tail = Qnil;
+ − 659
+ − 660 /* Check and coerce the arguments. */
+ − 661 for (argnum = 0; argnum < nargs; argnum++)
+ − 662 {
+ − 663 Lisp_Object seq = args[argnum];
+ − 664 if (LISTP (seq))
+ − 665 ;
+ − 666 else if (VECTORP (seq) || STRINGP (seq) || BIT_VECTORP (seq))
+ − 667 ;
+ − 668 #ifdef LOSING_BYTECODE
+ − 669 else if (COMPILED_FUNCTIONP (seq))
+ − 670 /* Urk! We allow this, for "compatibility"... */
+ − 671 ;
+ − 672 #endif
+ − 673 #if 0 /* removed for XEmacs 21 */
+ − 674 else if (INTP (seq))
+ − 675 /* This is too revolting to think about but maintains
+ − 676 compatibility with FSF (and lots and lots of old code). */
+ − 677 args[argnum] = Fnumber_to_string (seq);
+ − 678 #endif
+ − 679 else
+ − 680 {
+ − 681 check_losing_bytecode ("concat", seq);
+ − 682 args[argnum] = wrong_type_argument (Qsequencep, seq);
+ − 683 }
+ − 684
+ − 685 if (args_mse)
+ − 686 {
+ − 687 if (STRINGP (seq))
+ − 688 args_mse[argnum].string = seq;
+ − 689 else
+ − 690 args_mse[argnum].string = Qnil;
+ − 691 }
+ − 692 }
+ − 693
+ − 694 {
+ − 695 /* Charcount is a misnomer here as we might be dealing with the
+ − 696 length of a vector or list, but emphasizes that we're not dealing
+ − 697 with Bytecounts in strings */
+ − 698 Charcount total_length;
+ − 699
+ − 700 for (argnum = 0, total_length = 0; argnum < nargs; argnum++)
+ − 701 {
+ − 702 #ifdef LOSING_BYTECODE
+ − 703 Charcount thislen = length_with_bytecode_hack (args[argnum]);
+ − 704 #else
+ − 705 Charcount thislen = XINT (Flength (args[argnum]));
+ − 706 #endif
+ − 707 total_length += thislen;
+ − 708 }
+ − 709
+ − 710 switch (target_type)
+ − 711 {
+ − 712 case c_cons:
+ − 713 if (total_length == 0)
851
+ − 714 {
+ − 715 unbind_to (sdep);
+ − 716 /* In append, if all but last arg are nil, return last arg */
+ − 717 RETURN_UNGCPRO (last_tail);
+ − 718 }
428
+ − 719 val = Fmake_list (make_int (total_length), Qnil);
+ − 720 break;
+ − 721 case c_vector:
+ − 722 val = make_vector (total_length, Qnil);
+ − 723 break;
+ − 724 case c_bit_vector:
+ − 725 val = make_bit_vector (total_length, Qzero);
+ − 726 break;
+ − 727 case c_string:
+ − 728 /* We don't make the string yet because we don't know the
+ − 729 actual number of bytes. This loop was formerly written
+ − 730 to call Fmake_string() here and then call set_string_char()
+ − 731 for each char. This seems logical enough but is waaaaaaaay
+ − 732 slow -- set_string_char() has to scan the whole string up
+ − 733 to the place where the substitution is called for in order
+ − 734 to find the place to change, and may have to do some
+ − 735 realloc()ing in order to make the char fit properly.
+ − 736 O(N^2) yuckage. */
+ − 737 val = Qnil;
851
+ − 738 string_result =
867
+ − 739 (Ibyte *) MALLOC_OR_ALLOCA (total_length * MAX_ICHAR_LEN);
428
+ − 740 string_result_ptr = string_result;
+ − 741 break;
+ − 742 default:
442
+ − 743 val = Qnil;
2500
+ − 744 ABORT ();
428
+ − 745 }
+ − 746 }
+ − 747
+ − 748
+ − 749 if (CONSP (val))
+ − 750 tail = val, toindex = -1; /* -1 in toindex is flag we are
+ − 751 making a list */
+ − 752 else
+ − 753 toindex = 0;
+ − 754
+ − 755 prev = Qnil;
+ − 756
+ − 757 for (argnum = 0; argnum < nargs; argnum++)
+ − 758 {
+ − 759 Charcount thisleni = 0;
+ − 760 Charcount thisindex = 0;
+ − 761 Lisp_Object seq = args[argnum];
867
+ − 762 Ibyte *string_source_ptr = 0;
+ − 763 Ibyte *string_prev_result_ptr = string_result_ptr;
428
+ − 764
+ − 765 if (!CONSP (seq))
+ − 766 {
+ − 767 #ifdef LOSING_BYTECODE
+ − 768 thisleni = length_with_bytecode_hack (seq);
+ − 769 #else
+ − 770 thisleni = XINT (Flength (seq));
+ − 771 #endif
+ − 772 }
+ − 773 if (STRINGP (seq))
+ − 774 string_source_ptr = XSTRING_DATA (seq);
+ − 775
+ − 776 while (1)
+ − 777 {
+ − 778 Lisp_Object elt;
+ − 779
+ − 780 /* We've come to the end of this arg, so exit. */
+ − 781 if (NILP (seq))
+ − 782 break;
+ − 783
+ − 784 /* Fetch next element of `seq' arg into `elt' */
+ − 785 if (CONSP (seq))
+ − 786 {
+ − 787 elt = XCAR (seq);
+ − 788 seq = XCDR (seq);
+ − 789 }
+ − 790 else
+ − 791 {
+ − 792 if (thisindex >= thisleni)
+ − 793 break;
+ − 794
+ − 795 if (STRINGP (seq))
+ − 796 {
867
+ − 797 elt = make_char (itext_ichar (string_source_ptr));
+ − 798 INC_IBYTEPTR (string_source_ptr);
428
+ − 799 }
+ − 800 else if (VECTORP (seq))
+ − 801 elt = XVECTOR_DATA (seq)[thisindex];
+ − 802 else if (BIT_VECTORP (seq))
+ − 803 elt = make_int (bit_vector_bit (XBIT_VECTOR (seq),
+ − 804 thisindex));
+ − 805 else
+ − 806 elt = Felt (seq, make_int (thisindex));
+ − 807 thisindex++;
+ − 808 }
+ − 809
+ − 810 /* Store into result */
+ − 811 if (toindex < 0)
+ − 812 {
+ − 813 /* toindex negative means we are making a list */
+ − 814 XCAR (tail) = elt;
+ − 815 prev = tail;
+ − 816 tail = XCDR (tail);
+ − 817 }
+ − 818 else if (VECTORP (val))
+ − 819 XVECTOR_DATA (val)[toindex++] = elt;
+ − 820 else if (BIT_VECTORP (val))
+ − 821 {
+ − 822 CHECK_BIT (elt);
+ − 823 set_bit_vector_bit (XBIT_VECTOR (val), toindex++, XINT (elt));
+ − 824 }
+ − 825 else
+ − 826 {
+ − 827 CHECK_CHAR_COERCE_INT (elt);
867
+ − 828 string_result_ptr += set_itext_ichar (string_result_ptr,
428
+ − 829 XCHAR (elt));
+ − 830 }
+ − 831 }
+ − 832 if (args_mse)
+ − 833 {
+ − 834 args_mse[argnum].entry_offset =
+ − 835 string_prev_result_ptr - string_result;
+ − 836 args_mse[argnum].entry_length =
+ − 837 string_result_ptr - string_prev_result_ptr;
+ − 838 }
+ − 839 }
+ − 840
+ − 841 /* Now we finally make the string. */
+ − 842 if (target_type == c_string)
+ − 843 {
+ − 844 val = make_string (string_result, string_result_ptr - string_result);
+ − 845 for (argnum = 0; argnum < nargs; argnum++)
+ − 846 {
+ − 847 if (STRINGP (args_mse[argnum].string))
+ − 848 copy_string_extents (val, args_mse[argnum].string,
+ − 849 args_mse[argnum].entry_offset, 0,
+ − 850 args_mse[argnum].entry_length);
+ − 851 }
+ − 852 }
+ − 853
+ − 854 if (!NILP (prev))
+ − 855 XCDR (prev) = last_tail;
+ − 856
851
+ − 857 unbind_to (sdep);
428
+ − 858 RETURN_UNGCPRO (val);
+ − 859 }
+ − 860
+ − 861 DEFUN ("copy-alist", Fcopy_alist, 1, 1, 0, /*
+ − 862 Return a copy of ALIST.
+ − 863 This is an alist which represents the same mapping from objects to objects,
+ − 864 but does not share the alist structure with ALIST.
+ − 865 The objects mapped (cars and cdrs of elements of the alist)
+ − 866 are shared, however.
+ − 867 Elements of ALIST that are not conses are also shared.
+ − 868 */
+ − 869 (alist))
+ − 870 {
+ − 871 Lisp_Object tail;
+ − 872
+ − 873 if (NILP (alist))
+ − 874 return alist;
+ − 875 CHECK_CONS (alist);
+ − 876
+ − 877 alist = concat (1, &alist, c_cons, 0);
+ − 878 for (tail = alist; CONSP (tail); tail = XCDR (tail))
+ − 879 {
+ − 880 Lisp_Object car = XCAR (tail);
+ − 881
+ − 882 if (CONSP (car))
+ − 883 XCAR (tail) = Fcons (XCAR (car), XCDR (car));
+ − 884 }
+ − 885 return alist;
+ − 886 }
+ − 887
+ − 888 DEFUN ("copy-tree", Fcopy_tree, 1, 2, 0, /*
+ − 889 Return a copy of a list and substructures.
+ − 890 The argument is copied, and any lists contained within it are copied
+ − 891 recursively. Circularities and shared substructures are not preserved.
+ − 892 Second arg VECP causes vectors to be copied, too. Strings and bit vectors
+ − 893 are not copied.
+ − 894 */
+ − 895 (arg, vecp))
+ − 896 {
454
+ − 897 return safe_copy_tree (arg, vecp, 0);
+ − 898 }
+ − 899
+ − 900 Lisp_Object
+ − 901 safe_copy_tree (Lisp_Object arg, Lisp_Object vecp, int depth)
+ − 902 {
+ − 903 if (depth > 200)
563
+ − 904 stack_overflow ("Stack overflow in copy-tree", arg);
454
+ − 905
428
+ − 906 if (CONSP (arg))
+ − 907 {
+ − 908 Lisp_Object rest;
+ − 909 rest = arg = Fcopy_sequence (arg);
+ − 910 while (CONSP (rest))
+ − 911 {
+ − 912 Lisp_Object elt = XCAR (rest);
+ − 913 QUIT;
+ − 914 if (CONSP (elt) || VECTORP (elt))
454
+ − 915 XCAR (rest) = safe_copy_tree (elt, vecp, depth + 1);
428
+ − 916 if (VECTORP (XCDR (rest))) /* hack for (a b . [c d]) */
454
+ − 917 XCDR (rest) = safe_copy_tree (XCDR (rest), vecp, depth +1);
428
+ − 918 rest = XCDR (rest);
+ − 919 }
+ − 920 }
+ − 921 else if (VECTORP (arg) && ! NILP (vecp))
+ − 922 {
+ − 923 int i = XVECTOR_LENGTH (arg);
+ − 924 int j;
+ − 925 arg = Fcopy_sequence (arg);
+ − 926 for (j = 0; j < i; j++)
+ − 927 {
+ − 928 Lisp_Object elt = XVECTOR_DATA (arg) [j];
+ − 929 QUIT;
+ − 930 if (CONSP (elt) || VECTORP (elt))
454
+ − 931 XVECTOR_DATA (arg) [j] = safe_copy_tree (elt, vecp, depth + 1);
428
+ − 932 }
+ − 933 }
+ − 934 return arg;
+ − 935 }
+ − 936
+ − 937 DEFUN ("substring", Fsubstring, 2, 3, 0, /*
444
+ − 938 Return the substring of STRING starting at START and ending before END.
+ − 939 END may be nil or omitted; then the substring runs to the end of STRING.
+ − 940 If START or END is negative, it counts from the end.
+ − 941 Relevant parts of the string-extent-data are copied to the new string.
428
+ − 942 */
444
+ − 943 (string, start, end))
428
+ − 944 {
444
+ − 945 Charcount ccstart, ccend;
+ − 946 Bytecount bstart, blen;
428
+ − 947 Lisp_Object val;
+ − 948
+ − 949 CHECK_STRING (string);
444
+ − 950 CHECK_INT (start);
+ − 951 get_string_range_char (string, start, end, &ccstart, &ccend,
428
+ − 952 GB_HISTORICAL_STRING_BEHAVIOR);
793
+ − 953 bstart = string_index_char_to_byte (string, ccstart);
+ − 954 blen = string_offset_char_to_byte_len (string, bstart, ccend - ccstart);
444
+ − 955 val = make_string (XSTRING_DATA (string) + bstart, blen);
+ − 956 /* Copy any applicable extent information into the new string. */
+ − 957 copy_string_extents (val, string, 0, bstart, blen);
428
+ − 958 return val;
+ − 959 }
+ − 960
+ − 961 DEFUN ("subseq", Fsubseq, 2, 3, 0, /*
442
+ − 962 Return the subsequence of SEQUENCE starting at START and ending before END.
+ − 963 END may be omitted; then the subsequence runs to the end of SEQUENCE.
+ − 964 If START or END is negative, it counts from the end.
+ − 965 The returned subsequence is always of the same type as SEQUENCE.
+ − 966 If SEQUENCE is a string, relevant parts of the string-extent-data
+ − 967 are copied to the new string.
428
+ − 968 */
442
+ − 969 (sequence, start, end))
428
+ − 970 {
442
+ − 971 EMACS_INT len, s, e;
+ − 972
+ − 973 if (STRINGP (sequence))
+ − 974 return Fsubstring (sequence, start, end);
+ − 975
+ − 976 len = XINT (Flength (sequence));
+ − 977
+ − 978 CHECK_INT (start);
+ − 979 s = XINT (start);
+ − 980 if (s < 0)
+ − 981 s = len + s;
+ − 982
+ − 983 if (NILP (end))
+ − 984 e = len;
428
+ − 985 else
+ − 986 {
442
+ − 987 CHECK_INT (end);
+ − 988 e = XINT (end);
+ − 989 if (e < 0)
+ − 990 e = len + e;
428
+ − 991 }
+ − 992
442
+ − 993 if (!(0 <= s && s <= e && e <= len))
+ − 994 args_out_of_range_3 (sequence, make_int (s), make_int (e));
+ − 995
+ − 996 if (VECTORP (sequence))
428
+ − 997 {
442
+ − 998 Lisp_Object result = make_vector (e - s, Qnil);
428
+ − 999 EMACS_INT i;
442
+ − 1000 Lisp_Object *in_elts = XVECTOR_DATA (sequence);
428
+ − 1001 Lisp_Object *out_elts = XVECTOR_DATA (result);
+ − 1002
442
+ − 1003 for (i = s; i < e; i++)
+ − 1004 out_elts[i - s] = in_elts[i];
428
+ − 1005 return result;
+ − 1006 }
442
+ − 1007 else if (LISTP (sequence))
428
+ − 1008 {
+ − 1009 Lisp_Object result = Qnil;
+ − 1010 EMACS_INT i;
+ − 1011
442
+ − 1012 sequence = Fnthcdr (make_int (s), sequence);
+ − 1013
+ − 1014 for (i = s; i < e; i++)
428
+ − 1015 {
442
+ − 1016 result = Fcons (Fcar (sequence), result);
+ − 1017 sequence = Fcdr (sequence);
428
+ − 1018 }
+ − 1019
+ − 1020 return Fnreverse (result);
+ − 1021 }
442
+ − 1022 else if (BIT_VECTORP (sequence))
+ − 1023 {
+ − 1024 Lisp_Object result = make_bit_vector (e - s, Qzero);
+ − 1025 EMACS_INT i;
+ − 1026
+ − 1027 for (i = s; i < e; i++)
+ − 1028 set_bit_vector_bit (XBIT_VECTOR (result), i - s,
+ − 1029 bit_vector_bit (XBIT_VECTOR (sequence), i));
+ − 1030 return result;
+ − 1031 }
+ − 1032 else
+ − 1033 {
2500
+ − 1034 ABORT (); /* unreachable, since Flength (sequence) did not get
442
+ − 1035 an error */
+ − 1036 return Qnil;
+ − 1037 }
428
+ − 1038 }
+ − 1039
771
+ − 1040 /* Split STRING into a list of substrings. The substrings are the
+ − 1041 parts of original STRING separated by SEPCHAR. */
+ − 1042 static Lisp_Object
867
+ − 1043 split_string_by_ichar_1 (const Ibyte *string, Bytecount size,
+ − 1044 Ichar sepchar)
771
+ − 1045 {
+ − 1046 Lisp_Object result = Qnil;
867
+ − 1047 const Ibyte *end = string + size;
771
+ − 1048
+ − 1049 while (1)
+ − 1050 {
867
+ − 1051 const Ibyte *p = string;
771
+ − 1052 while (p < end)
+ − 1053 {
867
+ − 1054 if (itext_ichar (p) == sepchar)
771
+ − 1055 break;
867
+ − 1056 INC_IBYTEPTR (p);
771
+ − 1057 }
+ − 1058 result = Fcons (make_string (string, p - string), result);
+ − 1059 if (p < end)
+ − 1060 {
+ − 1061 string = p;
867
+ − 1062 INC_IBYTEPTR (string); /* skip sepchar */
771
+ − 1063 }
+ − 1064 else
+ − 1065 break;
+ − 1066 }
+ − 1067 return Fnreverse (result);
+ − 1068 }
+ − 1069
+ − 1070 /* The same as the above, except PATH is an external C string (it is
+ − 1071 converted using Qfile_name), and sepchar is hardcoded to SEPCHAR
+ − 1072 (':' or whatever). */
+ − 1073 Lisp_Object
+ − 1074 split_external_path (const Extbyte *path)
+ − 1075 {
+ − 1076 Bytecount newlen;
867
+ − 1077 Ibyte *newpath;
771
+ − 1078 if (!path)
+ − 1079 return Qnil;
+ − 1080
+ − 1081 TO_INTERNAL_FORMAT (C_STRING, path, ALLOCA, (newpath, newlen), Qfile_name);
+ − 1082
+ − 1083 /* #### Does this make sense? It certainly does for
+ − 1084 split_env_path(), but it looks dubious here. Does any code
+ − 1085 depend on split_external_path("") returning nil instead of an empty
+ − 1086 string? */
+ − 1087 if (!newlen)
+ − 1088 return Qnil;
+ − 1089
867
+ − 1090 return split_string_by_ichar_1 (newpath, newlen, SEPCHAR);
771
+ − 1091 }
+ − 1092
+ − 1093 Lisp_Object
867
+ − 1094 split_env_path (const CIbyte *evarname, const Ibyte *default_)
771
+ − 1095 {
867
+ − 1096 const Ibyte *path = 0;
771
+ − 1097 if (evarname)
+ − 1098 path = egetenv (evarname);
+ − 1099 if (!path)
+ − 1100 path = default_;
+ − 1101 if (!path)
+ − 1102 return Qnil;
867
+ − 1103 return split_string_by_ichar_1 (path, qxestrlen (path), SEPCHAR);
771
+ − 1104 }
+ − 1105
+ − 1106 /* Ben thinks this function should not exist or be exported to Lisp.
+ − 1107 We use it to define split-path-string in subr.el (not!). */
+ − 1108
949
+ − 1109 DEFUN ("split-string-by-char", Fsplit_string_by_char, 2, 2, 0, /*
771
+ − 1110 Split STRING into a list of substrings originally separated by SEPCHAR.
+ − 1111 */
+ − 1112 (string, sepchar))
+ − 1113 {
+ − 1114 CHECK_STRING (string);
+ − 1115 CHECK_CHAR (sepchar);
867
+ − 1116 return split_string_by_ichar_1 (XSTRING_DATA (string),
771
+ − 1117 XSTRING_LENGTH (string),
+ − 1118 XCHAR (sepchar));
+ − 1119 }
+ − 1120
+ − 1121 /* #### This was supposed to be in subr.el, but is used VERY early in
+ − 1122 the bootstrap process, so it goes here. Damn. */
+ − 1123
+ − 1124 DEFUN ("split-path", Fsplit_path, 1, 1, 0, /*
+ − 1125 Explode a search path into a list of strings.
+ − 1126 The path components are separated with the characters specified
+ − 1127 with `path-separator'.
+ − 1128 */
+ − 1129 (path))
+ − 1130 {
+ − 1131 CHECK_STRING (path);
+ − 1132
+ − 1133 while (!STRINGP (Vpath_separator)
826
+ − 1134 || (string_char_length (Vpath_separator) != 1))
771
+ − 1135 Vpath_separator = signal_continuable_error
+ − 1136 (Qinvalid_state,
+ − 1137 "`path-separator' should be set to a single-character string",
+ − 1138 Vpath_separator);
+ − 1139
867
+ − 1140 return (split_string_by_ichar_1
771
+ − 1141 (XSTRING_DATA (path), XSTRING_LENGTH (path),
867
+ − 1142 itext_ichar (XSTRING_DATA (Vpath_separator))));
771
+ − 1143 }
+ − 1144
428
+ − 1145
+ − 1146 DEFUN ("nthcdr", Fnthcdr, 2, 2, 0, /*
+ − 1147 Take cdr N times on LIST, and return the result.
+ − 1148 */
+ − 1149 (n, list))
+ − 1150 {
1920
+ − 1151 /* This function can GC */
647
+ − 1152 REGISTER EMACS_INT i;
428
+ − 1153 REGISTER Lisp_Object tail = list;
+ − 1154 CHECK_NATNUM (n);
+ − 1155 for (i = XINT (n); i; i--)
+ − 1156 {
+ − 1157 if (CONSP (tail))
+ − 1158 tail = XCDR (tail);
+ − 1159 else if (NILP (tail))
+ − 1160 return Qnil;
+ − 1161 else
+ − 1162 {
+ − 1163 tail = wrong_type_argument (Qlistp, tail);
+ − 1164 i++;
+ − 1165 }
+ − 1166 }
+ − 1167 return tail;
+ − 1168 }
+ − 1169
+ − 1170 DEFUN ("nth", Fnth, 2, 2, 0, /*
+ − 1171 Return the Nth element of LIST.
+ − 1172 N counts from zero. If LIST is not that long, nil is returned.
+ − 1173 */
+ − 1174 (n, list))
+ − 1175 {
1920
+ − 1176 /* This function can GC */
428
+ − 1177 return Fcar (Fnthcdr (n, list));
+ − 1178 }
+ − 1179
+ − 1180 DEFUN ("elt", Felt, 2, 2, 0, /*
+ − 1181 Return element of SEQUENCE at index N.
+ − 1182 */
+ − 1183 (sequence, n))
+ − 1184 {
1920
+ − 1185 /* This function can GC */
428
+ − 1186 retry:
+ − 1187 CHECK_INT_COERCE_CHAR (n); /* yuck! */
+ − 1188 if (LISTP (sequence))
+ − 1189 {
+ − 1190 Lisp_Object tem = Fnthcdr (n, sequence);
+ − 1191 /* #### Utterly, completely, fucking disgusting.
+ − 1192 * #### The whole point of "elt" is that it operates on
+ − 1193 * #### sequences, and does error- (bounds-) checking.
+ − 1194 */
+ − 1195 if (CONSP (tem))
+ − 1196 return XCAR (tem);
+ − 1197 else
+ − 1198 #if 1
+ − 1199 /* This is The Way It Has Always Been. */
+ − 1200 return Qnil;
+ − 1201 #else
+ − 1202 /* This is The Way Mly and Cltl2 say It Should Be. */
+ − 1203 args_out_of_range (sequence, n);
+ − 1204 #endif
+ − 1205 }
+ − 1206 else if (STRINGP (sequence) ||
+ − 1207 VECTORP (sequence) ||
+ − 1208 BIT_VECTORP (sequence))
+ − 1209 return Faref (sequence, n);
+ − 1210 #ifdef LOSING_BYTECODE
+ − 1211 else if (COMPILED_FUNCTIONP (sequence))
+ − 1212 {
+ − 1213 EMACS_INT idx = XINT (n);
+ − 1214 if (idx < 0)
+ − 1215 {
+ − 1216 lose:
+ − 1217 args_out_of_range (sequence, n);
+ − 1218 }
+ − 1219 /* Utter perversity */
+ − 1220 {
+ − 1221 Lisp_Compiled_Function *f = XCOMPILED_FUNCTION (sequence);
+ − 1222 switch (idx)
+ − 1223 {
+ − 1224 case COMPILED_ARGLIST:
+ − 1225 return compiled_function_arglist (f);
+ − 1226 case COMPILED_INSTRUCTIONS:
+ − 1227 return compiled_function_instructions (f);
+ − 1228 case COMPILED_CONSTANTS:
+ − 1229 return compiled_function_constants (f);
+ − 1230 case COMPILED_STACK_DEPTH:
+ − 1231 return compiled_function_stack_depth (f);
+ − 1232 case COMPILED_DOC_STRING:
+ − 1233 return compiled_function_documentation (f);
+ − 1234 case COMPILED_DOMAIN:
+ − 1235 return compiled_function_domain (f);
+ − 1236 case COMPILED_INTERACTIVE:
+ − 1237 if (f->flags.interactivep)
+ − 1238 return compiled_function_interactive (f);
+ − 1239 /* if we return nil, can't tell interactive with no args
+ − 1240 from noninteractive. */
+ − 1241 goto lose;
+ − 1242 default:
+ − 1243 goto lose;
+ − 1244 }
+ − 1245 }
+ − 1246 }
+ − 1247 #endif /* LOSING_BYTECODE */
+ − 1248 else
+ − 1249 {
+ − 1250 check_losing_bytecode ("elt", sequence);
+ − 1251 sequence = wrong_type_argument (Qsequencep, sequence);
+ − 1252 goto retry;
+ − 1253 }
+ − 1254 }
+ − 1255
+ − 1256 DEFUN ("last", Flast, 1, 2, 0, /*
+ − 1257 Return the tail of list LIST, of length N (default 1).
+ − 1258 LIST may be a dotted list, but not a circular list.
+ − 1259 Optional argument N must be a non-negative integer.
+ − 1260 If N is zero, then the atom that terminates the list is returned.
+ − 1261 If N is greater than the length of LIST, then LIST itself is returned.
+ − 1262 */
+ − 1263 (list, n))
+ − 1264 {
+ − 1265 EMACS_INT int_n, count;
+ − 1266 Lisp_Object retval, tortoise, hare;
+ − 1267
+ − 1268 CHECK_LIST (list);
+ − 1269
+ − 1270 if (NILP (n))
+ − 1271 int_n = 1;
+ − 1272 else
+ − 1273 {
+ − 1274 CHECK_NATNUM (n);
+ − 1275 int_n = XINT (n);
+ − 1276 }
+ − 1277
+ − 1278 for (retval = tortoise = hare = list, count = 0;
+ − 1279 CONSP (hare);
+ − 1280 hare = XCDR (hare),
+ − 1281 (int_n-- <= 0 ? ((void) (retval = XCDR (retval))) : (void)0),
+ − 1282 count++)
+ − 1283 {
+ − 1284 if (count < CIRCULAR_LIST_SUSPICION_LENGTH) continue;
+ − 1285
+ − 1286 if (count & 1)
+ − 1287 tortoise = XCDR (tortoise);
+ − 1288 if (EQ (hare, tortoise))
+ − 1289 signal_circular_list_error (list);
+ − 1290 }
+ − 1291
+ − 1292 return retval;
+ − 1293 }
+ − 1294
+ − 1295 DEFUN ("nbutlast", Fnbutlast, 1, 2, 0, /*
+ − 1296 Modify LIST to remove the last N (default 1) elements.
+ − 1297 If LIST has N or fewer elements, nil is returned and LIST is unmodified.
+ − 1298 */
+ − 1299 (list, n))
+ − 1300 {
+ − 1301 EMACS_INT int_n;
+ − 1302
+ − 1303 CHECK_LIST (list);
+ − 1304
+ − 1305 if (NILP (n))
+ − 1306 int_n = 1;
+ − 1307 else
+ − 1308 {
+ − 1309 CHECK_NATNUM (n);
+ − 1310 int_n = XINT (n);
+ − 1311 }
+ − 1312
+ − 1313 {
+ − 1314 Lisp_Object last_cons = list;
+ − 1315
+ − 1316 EXTERNAL_LIST_LOOP_1 (list)
+ − 1317 {
+ − 1318 if (int_n-- < 0)
+ − 1319 last_cons = XCDR (last_cons);
+ − 1320 }
+ − 1321
+ − 1322 if (int_n >= 0)
+ − 1323 return Qnil;
+ − 1324
+ − 1325 XCDR (last_cons) = Qnil;
+ − 1326 return list;
+ − 1327 }
+ − 1328 }
+ − 1329
+ − 1330 DEFUN ("butlast", Fbutlast, 1, 2, 0, /*
+ − 1331 Return a copy of LIST with the last N (default 1) elements removed.
+ − 1332 If LIST has N or fewer elements, nil is returned.
+ − 1333 */
+ − 1334 (list, n))
+ − 1335 {
444
+ − 1336 EMACS_INT int_n;
428
+ − 1337
+ − 1338 CHECK_LIST (list);
+ − 1339
+ − 1340 if (NILP (n))
+ − 1341 int_n = 1;
+ − 1342 else
+ − 1343 {
+ − 1344 CHECK_NATNUM (n);
+ − 1345 int_n = XINT (n);
+ − 1346 }
+ − 1347
+ − 1348 {
+ − 1349 Lisp_Object retval = Qnil;
+ − 1350 Lisp_Object tail = list;
+ − 1351
+ − 1352 EXTERNAL_LIST_LOOP_1 (list)
+ − 1353 {
+ − 1354 if (--int_n < 0)
+ − 1355 {
+ − 1356 retval = Fcons (XCAR (tail), retval);
+ − 1357 tail = XCDR (tail);
+ − 1358 }
+ − 1359 }
+ − 1360
+ − 1361 return Fnreverse (retval);
+ − 1362 }
+ − 1363 }
+ − 1364
+ − 1365 DEFUN ("member", Fmember, 2, 2, 0, /*
+ − 1366 Return non-nil if ELT is an element of LIST. Comparison done with `equal'.
+ − 1367 The value is actually the tail of LIST whose car is ELT.
+ − 1368 */
+ − 1369 (elt, list))
+ − 1370 {
+ − 1371 EXTERNAL_LIST_LOOP_3 (list_elt, list, tail)
+ − 1372 {
+ − 1373 if (internal_equal (elt, list_elt, 0))
+ − 1374 return tail;
+ − 1375 }
+ − 1376 return Qnil;
+ − 1377 }
+ − 1378
+ − 1379 DEFUN ("old-member", Fold_member, 2, 2, 0, /*
+ − 1380 Return non-nil if ELT is an element of LIST. Comparison done with `old-equal'.
+ − 1381 The value is actually the tail of LIST whose car is ELT.
+ − 1382 This function is provided only for byte-code compatibility with v19.
+ − 1383 Do not use it.
+ − 1384 */
+ − 1385 (elt, list))
+ − 1386 {
+ − 1387 EXTERNAL_LIST_LOOP_3 (list_elt, list, tail)
+ − 1388 {
+ − 1389 if (internal_old_equal (elt, list_elt, 0))
+ − 1390 return tail;
+ − 1391 }
+ − 1392 return Qnil;
+ − 1393 }
+ − 1394
+ − 1395 DEFUN ("memq", Fmemq, 2, 2, 0, /*
+ − 1396 Return non-nil if ELT is an element of LIST. Comparison done with `eq'.
+ − 1397 The value is actually the tail of LIST whose car is ELT.
+ − 1398 */
+ − 1399 (elt, list))
+ − 1400 {
+ − 1401 EXTERNAL_LIST_LOOP_3 (list_elt, list, tail)
+ − 1402 {
+ − 1403 if (EQ_WITH_EBOLA_NOTICE (elt, list_elt))
+ − 1404 return tail;
+ − 1405 }
+ − 1406 return Qnil;
+ − 1407 }
+ − 1408
+ − 1409 DEFUN ("old-memq", Fold_memq, 2, 2, 0, /*
+ − 1410 Return non-nil if ELT is an element of LIST. Comparison done with `old-eq'.
+ − 1411 The value is actually the tail of LIST whose car is ELT.
+ − 1412 This function is provided only for byte-code compatibility with v19.
+ − 1413 Do not use it.
+ − 1414 */
+ − 1415 (elt, list))
+ − 1416 {
+ − 1417 EXTERNAL_LIST_LOOP_3 (list_elt, list, tail)
+ − 1418 {
+ − 1419 if (HACKEQ_UNSAFE (elt, list_elt))
+ − 1420 return tail;
+ − 1421 }
+ − 1422 return Qnil;
+ − 1423 }
+ − 1424
+ − 1425 Lisp_Object
+ − 1426 memq_no_quit (Lisp_Object elt, Lisp_Object list)
+ − 1427 {
+ − 1428 LIST_LOOP_3 (list_elt, list, tail)
+ − 1429 {
+ − 1430 if (EQ_WITH_EBOLA_NOTICE (elt, list_elt))
+ − 1431 return tail;
+ − 1432 }
+ − 1433 return Qnil;
+ − 1434 }
+ − 1435
+ − 1436 DEFUN ("assoc", Fassoc, 2, 2, 0, /*
444
+ − 1437 Return non-nil if KEY is `equal' to the car of an element of ALIST.
+ − 1438 The value is actually the element of ALIST whose car equals KEY.
428
+ − 1439 */
444
+ − 1440 (key, alist))
428
+ − 1441 {
+ − 1442 /* This function can GC. */
444
+ − 1443 EXTERNAL_ALIST_LOOP_4 (elt, elt_car, elt_cdr, alist)
428
+ − 1444 {
+ − 1445 if (internal_equal (key, elt_car, 0))
+ − 1446 return elt;
+ − 1447 }
+ − 1448 return Qnil;
+ − 1449 }
+ − 1450
+ − 1451 DEFUN ("old-assoc", Fold_assoc, 2, 2, 0, /*
444
+ − 1452 Return non-nil if KEY is `old-equal' to the car of an element of ALIST.
+ − 1453 The value is actually the element of ALIST whose car equals KEY.
428
+ − 1454 */
444
+ − 1455 (key, alist))
428
+ − 1456 {
+ − 1457 /* This function can GC. */
444
+ − 1458 EXTERNAL_ALIST_LOOP_4 (elt, elt_car, elt_cdr, alist)
428
+ − 1459 {
+ − 1460 if (internal_old_equal (key, elt_car, 0))
+ − 1461 return elt;
+ − 1462 }
+ − 1463 return Qnil;
+ − 1464 }
+ − 1465
+ − 1466 Lisp_Object
444
+ − 1467 assoc_no_quit (Lisp_Object key, Lisp_Object alist)
428
+ − 1468 {
+ − 1469 int speccount = specpdl_depth ();
+ − 1470 specbind (Qinhibit_quit, Qt);
771
+ − 1471 return unbind_to_1 (speccount, Fassoc (key, alist));
428
+ − 1472 }
+ − 1473
+ − 1474 DEFUN ("assq", Fassq, 2, 2, 0, /*
444
+ − 1475 Return non-nil if KEY is `eq' to the car of an element of ALIST.
+ − 1476 The value is actually the element of ALIST whose car is KEY.
+ − 1477 Elements of ALIST that are not conses are ignored.
428
+ − 1478 */
444
+ − 1479 (key, alist))
428
+ − 1480 {
444
+ − 1481 EXTERNAL_ALIST_LOOP_4 (elt, elt_car, elt_cdr, alist)
428
+ − 1482 {
+ − 1483 if (EQ_WITH_EBOLA_NOTICE (key, elt_car))
+ − 1484 return elt;
+ − 1485 }
+ − 1486 return Qnil;
+ − 1487 }
+ − 1488
+ − 1489 DEFUN ("old-assq", Fold_assq, 2, 2, 0, /*
444
+ − 1490 Return non-nil if KEY is `old-eq' to the car of an element of ALIST.
+ − 1491 The value is actually the element of ALIST whose car is KEY.
+ − 1492 Elements of ALIST that are not conses are ignored.
428
+ − 1493 This function is provided only for byte-code compatibility with v19.
+ − 1494 Do not use it.
+ − 1495 */
444
+ − 1496 (key, alist))
428
+ − 1497 {
444
+ − 1498 EXTERNAL_ALIST_LOOP_4 (elt, elt_car, elt_cdr, alist)
428
+ − 1499 {
+ − 1500 if (HACKEQ_UNSAFE (key, elt_car))
+ − 1501 return elt;
+ − 1502 }
+ − 1503 return Qnil;
+ − 1504 }
+ − 1505
+ − 1506 /* Like Fassq but never report an error and do not allow quits.
+ − 1507 Use only on lists known never to be circular. */
+ − 1508
+ − 1509 Lisp_Object
444
+ − 1510 assq_no_quit (Lisp_Object key, Lisp_Object alist)
428
+ − 1511 {
+ − 1512 /* This cannot GC. */
444
+ − 1513 LIST_LOOP_2 (elt, alist)
428
+ − 1514 {
+ − 1515 Lisp_Object elt_car = XCAR (elt);
+ − 1516 if (EQ_WITH_EBOLA_NOTICE (key, elt_car))
+ − 1517 return elt;
+ − 1518 }
+ − 1519 return Qnil;
+ − 1520 }
+ − 1521
+ − 1522 DEFUN ("rassoc", Frassoc, 2, 2, 0, /*
444
+ − 1523 Return non-nil if VALUE is `equal' to the cdr of an element of ALIST.
+ − 1524 The value is actually the element of ALIST whose cdr equals VALUE.
428
+ − 1525 */
444
+ − 1526 (value, alist))
428
+ − 1527 {
444
+ − 1528 EXTERNAL_ALIST_LOOP_4 (elt, elt_car, elt_cdr, alist)
428
+ − 1529 {
444
+ − 1530 if (internal_equal (value, elt_cdr, 0))
428
+ − 1531 return elt;
+ − 1532 }
+ − 1533 return Qnil;
+ − 1534 }
+ − 1535
+ − 1536 DEFUN ("old-rassoc", Fold_rassoc, 2, 2, 0, /*
444
+ − 1537 Return non-nil if VALUE is `old-equal' to the cdr of an element of ALIST.
+ − 1538 The value is actually the element of ALIST whose cdr equals VALUE.
428
+ − 1539 */
444
+ − 1540 (value, alist))
428
+ − 1541 {
444
+ − 1542 EXTERNAL_ALIST_LOOP_4 (elt, elt_car, elt_cdr, alist)
428
+ − 1543 {
444
+ − 1544 if (internal_old_equal (value, elt_cdr, 0))
428
+ − 1545 return elt;
+ − 1546 }
+ − 1547 return Qnil;
+ − 1548 }
+ − 1549
+ − 1550 DEFUN ("rassq", Frassq, 2, 2, 0, /*
444
+ − 1551 Return non-nil if VALUE is `eq' to the cdr of an element of ALIST.
+ − 1552 The value is actually the element of ALIST whose cdr is VALUE.
428
+ − 1553 */
444
+ − 1554 (value, alist))
428
+ − 1555 {
444
+ − 1556 EXTERNAL_ALIST_LOOP_4 (elt, elt_car, elt_cdr, alist)
428
+ − 1557 {
444
+ − 1558 if (EQ_WITH_EBOLA_NOTICE (value, elt_cdr))
428
+ − 1559 return elt;
+ − 1560 }
+ − 1561 return Qnil;
+ − 1562 }
+ − 1563
+ − 1564 DEFUN ("old-rassq", Fold_rassq, 2, 2, 0, /*
444
+ − 1565 Return non-nil if VALUE is `old-eq' to the cdr of an element of ALIST.
+ − 1566 The value is actually the element of ALIST whose cdr is VALUE.
428
+ − 1567 */
444
+ − 1568 (value, alist))
428
+ − 1569 {
444
+ − 1570 EXTERNAL_ALIST_LOOP_4 (elt, elt_car, elt_cdr, alist)
428
+ − 1571 {
444
+ − 1572 if (HACKEQ_UNSAFE (value, elt_cdr))
428
+ − 1573 return elt;
+ − 1574 }
+ − 1575 return Qnil;
+ − 1576 }
+ − 1577
444
+ − 1578 /* Like Frassq, but caller must ensure that ALIST is properly
428
+ − 1579 nil-terminated and ebola-free. */
+ − 1580 Lisp_Object
444
+ − 1581 rassq_no_quit (Lisp_Object value, Lisp_Object alist)
428
+ − 1582 {
444
+ − 1583 LIST_LOOP_2 (elt, alist)
428
+ − 1584 {
+ − 1585 Lisp_Object elt_cdr = XCDR (elt);
444
+ − 1586 if (EQ_WITH_EBOLA_NOTICE (value, elt_cdr))
428
+ − 1587 return elt;
+ − 1588 }
+ − 1589 return Qnil;
+ − 1590 }
+ − 1591
+ − 1592
+ − 1593 DEFUN ("delete", Fdelete, 2, 2, 0, /*
+ − 1594 Delete by side effect any occurrences of ELT as a member of LIST.
+ − 1595 The modified LIST is returned. Comparison is done with `equal'.
+ − 1596 If the first member of LIST is ELT, there is no way to remove it by side
+ − 1597 effect; therefore, write `(setq foo (delete element foo))' to be sure
+ − 1598 of changing the value of `foo'.
+ − 1599 Also see: `remove'.
+ − 1600 */
+ − 1601 (elt, list))
+ − 1602 {
+ − 1603 EXTERNAL_LIST_LOOP_DELETE_IF (list_elt, list,
+ − 1604 (internal_equal (elt, list_elt, 0)));
+ − 1605 return list;
+ − 1606 }
+ − 1607
+ − 1608 DEFUN ("old-delete", Fold_delete, 2, 2, 0, /*
+ − 1609 Delete by side effect any occurrences of ELT as a member of LIST.
+ − 1610 The modified LIST is returned. Comparison is done with `old-equal'.
+ − 1611 If the first member of LIST is ELT, there is no way to remove it by side
+ − 1612 effect; therefore, write `(setq foo (old-delete element foo))' to be sure
+ − 1613 of changing the value of `foo'.
+ − 1614 */
+ − 1615 (elt, list))
+ − 1616 {
+ − 1617 EXTERNAL_LIST_LOOP_DELETE_IF (list_elt, list,
+ − 1618 (internal_old_equal (elt, list_elt, 0)));
+ − 1619 return list;
+ − 1620 }
+ − 1621
+ − 1622 DEFUN ("delq", Fdelq, 2, 2, 0, /*
+ − 1623 Delete by side effect any occurrences of ELT as a member of LIST.
+ − 1624 The modified LIST is returned. Comparison is done with `eq'.
+ − 1625 If the first member of LIST is ELT, there is no way to remove it by side
+ − 1626 effect; therefore, write `(setq foo (delq element foo))' to be sure of
+ − 1627 changing the value of `foo'.
+ − 1628 */
+ − 1629 (elt, list))
+ − 1630 {
+ − 1631 EXTERNAL_LIST_LOOP_DELETE_IF (list_elt, list,
+ − 1632 (EQ_WITH_EBOLA_NOTICE (elt, list_elt)));
+ − 1633 return list;
+ − 1634 }
+ − 1635
+ − 1636 DEFUN ("old-delq", Fold_delq, 2, 2, 0, /*
+ − 1637 Delete by side effect any occurrences of ELT as a member of LIST.
+ − 1638 The modified LIST is returned. Comparison is done with `old-eq'.
+ − 1639 If the first member of LIST is ELT, there is no way to remove it by side
+ − 1640 effect; therefore, write `(setq foo (old-delq element foo))' to be sure of
+ − 1641 changing the value of `foo'.
+ − 1642 */
+ − 1643 (elt, list))
+ − 1644 {
+ − 1645 EXTERNAL_LIST_LOOP_DELETE_IF (list_elt, list,
+ − 1646 (HACKEQ_UNSAFE (elt, list_elt)));
+ − 1647 return list;
+ − 1648 }
+ − 1649
+ − 1650 /* Like Fdelq, but caller must ensure that LIST is properly
+ − 1651 nil-terminated and ebola-free. */
+ − 1652
+ − 1653 Lisp_Object
+ − 1654 delq_no_quit (Lisp_Object elt, Lisp_Object list)
+ − 1655 {
+ − 1656 LIST_LOOP_DELETE_IF (list_elt, list,
+ − 1657 (EQ_WITH_EBOLA_NOTICE (elt, list_elt)));
+ − 1658 return list;
+ − 1659 }
+ − 1660
+ − 1661 /* Be VERY careful with this. This is like delq_no_quit() but
+ − 1662 also calls free_cons() on the removed conses. You must be SURE
+ − 1663 that no pointers to the freed conses remain around (e.g.
+ − 1664 someone else is pointing to part of the list). This function
+ − 1665 is useful on internal lists that are used frequently and where
+ − 1666 the actual list doesn't escape beyond known code bounds. */
+ − 1667
+ − 1668 Lisp_Object
+ − 1669 delq_no_quit_and_free_cons (Lisp_Object elt, Lisp_Object list)
+ − 1670 {
+ − 1671 REGISTER Lisp_Object tail = list;
+ − 1672 REGISTER Lisp_Object prev = Qnil;
+ − 1673
+ − 1674 while (!NILP (tail))
+ − 1675 {
+ − 1676 REGISTER Lisp_Object tem = XCAR (tail);
+ − 1677 if (EQ (elt, tem))
+ − 1678 {
+ − 1679 Lisp_Object cons_to_free = tail;
+ − 1680 if (NILP (prev))
+ − 1681 list = XCDR (tail);
+ − 1682 else
+ − 1683 XCDR (prev) = XCDR (tail);
+ − 1684 tail = XCDR (tail);
853
+ − 1685 free_cons (cons_to_free);
428
+ − 1686 }
+ − 1687 else
+ − 1688 {
+ − 1689 prev = tail;
+ − 1690 tail = XCDR (tail);
+ − 1691 }
+ − 1692 }
+ − 1693 return list;
+ − 1694 }
+ − 1695
+ − 1696 DEFUN ("remassoc", Fremassoc, 2, 2, 0, /*
444
+ − 1697 Delete by side effect any elements of ALIST whose car is `equal' to KEY.
+ − 1698 The modified ALIST is returned. If the first member of ALIST has a car
428
+ − 1699 that is `equal' to KEY, there is no way to remove it by side effect;
+ − 1700 therefore, write `(setq foo (remassoc key foo))' to be sure of changing
+ − 1701 the value of `foo'.
+ − 1702 */
444
+ − 1703 (key, alist))
428
+ − 1704 {
444
+ − 1705 EXTERNAL_LIST_LOOP_DELETE_IF (elt, alist,
428
+ − 1706 (CONSP (elt) &&
+ − 1707 internal_equal (key, XCAR (elt), 0)));
444
+ − 1708 return alist;
428
+ − 1709 }
+ − 1710
+ − 1711 Lisp_Object
444
+ − 1712 remassoc_no_quit (Lisp_Object key, Lisp_Object alist)
428
+ − 1713 {
+ − 1714 int speccount = specpdl_depth ();
+ − 1715 specbind (Qinhibit_quit, Qt);
771
+ − 1716 return unbind_to_1 (speccount, Fremassoc (key, alist));
428
+ − 1717 }
+ − 1718
+ − 1719 DEFUN ("remassq", Fremassq, 2, 2, 0, /*
444
+ − 1720 Delete by side effect any elements of ALIST whose car is `eq' to KEY.
+ − 1721 The modified ALIST is returned. If the first member of ALIST has a car
428
+ − 1722 that is `eq' to KEY, there is no way to remove it by side effect;
+ − 1723 therefore, write `(setq foo (remassq key foo))' to be sure of changing
+ − 1724 the value of `foo'.
+ − 1725 */
444
+ − 1726 (key, alist))
428
+ − 1727 {
444
+ − 1728 EXTERNAL_LIST_LOOP_DELETE_IF (elt, alist,
428
+ − 1729 (CONSP (elt) &&
+ − 1730 EQ_WITH_EBOLA_NOTICE (key, XCAR (elt))));
444
+ − 1731 return alist;
428
+ − 1732 }
+ − 1733
+ − 1734 /* no quit, no errors; be careful */
+ − 1735
+ − 1736 Lisp_Object
444
+ − 1737 remassq_no_quit (Lisp_Object key, Lisp_Object alist)
428
+ − 1738 {
444
+ − 1739 LIST_LOOP_DELETE_IF (elt, alist,
428
+ − 1740 (CONSP (elt) &&
+ − 1741 EQ_WITH_EBOLA_NOTICE (key, XCAR (elt))));
444
+ − 1742 return alist;
428
+ − 1743 }
+ − 1744
+ − 1745 DEFUN ("remrassoc", Fremrassoc, 2, 2, 0, /*
444
+ − 1746 Delete by side effect any elements of ALIST whose cdr is `equal' to VALUE.
+ − 1747 The modified ALIST is returned. If the first member of ALIST has a car
428
+ − 1748 that is `equal' to VALUE, there is no way to remove it by side effect;
+ − 1749 therefore, write `(setq foo (remrassoc value foo))' to be sure of changing
+ − 1750 the value of `foo'.
+ − 1751 */
444
+ − 1752 (value, alist))
428
+ − 1753 {
444
+ − 1754 EXTERNAL_LIST_LOOP_DELETE_IF (elt, alist,
428
+ − 1755 (CONSP (elt) &&
+ − 1756 internal_equal (value, XCDR (elt), 0)));
444
+ − 1757 return alist;
428
+ − 1758 }
+ − 1759
+ − 1760 DEFUN ("remrassq", Fremrassq, 2, 2, 0, /*
444
+ − 1761 Delete by side effect any elements of ALIST whose cdr is `eq' to VALUE.
+ − 1762 The modified ALIST is returned. If the first member of ALIST has a car
428
+ − 1763 that is `eq' to VALUE, there is no way to remove it by side effect;
+ − 1764 therefore, write `(setq foo (remrassq value foo))' to be sure of changing
+ − 1765 the value of `foo'.
+ − 1766 */
444
+ − 1767 (value, alist))
428
+ − 1768 {
444
+ − 1769 EXTERNAL_LIST_LOOP_DELETE_IF (elt, alist,
428
+ − 1770 (CONSP (elt) &&
+ − 1771 EQ_WITH_EBOLA_NOTICE (value, XCDR (elt))));
444
+ − 1772 return alist;
428
+ − 1773 }
+ − 1774
+ − 1775 /* Like Fremrassq, fast and unsafe; be careful */
+ − 1776 Lisp_Object
444
+ − 1777 remrassq_no_quit (Lisp_Object value, Lisp_Object alist)
428
+ − 1778 {
444
+ − 1779 LIST_LOOP_DELETE_IF (elt, alist,
428
+ − 1780 (CONSP (elt) &&
+ − 1781 EQ_WITH_EBOLA_NOTICE (value, XCDR (elt))));
444
+ − 1782 return alist;
428
+ − 1783 }
+ − 1784
+ − 1785 DEFUN ("nreverse", Fnreverse, 1, 1, 0, /*
+ − 1786 Reverse LIST by destructively modifying cdr pointers.
+ − 1787 Return the beginning of the reversed list.
+ − 1788 Also see: `reverse'.
+ − 1789 */
+ − 1790 (list))
+ − 1791 {
+ − 1792 struct gcpro gcpro1, gcpro2;
1849
+ − 1793 Lisp_Object prev = Qnil;
+ − 1794 Lisp_Object tail = list;
428
+ − 1795
+ − 1796 /* We gcpro our args; see `nconc' */
+ − 1797 GCPRO2 (prev, tail);
+ − 1798 while (!NILP (tail))
+ − 1799 {
+ − 1800 REGISTER Lisp_Object next;
+ − 1801 CONCHECK_CONS (tail);
+ − 1802 next = XCDR (tail);
+ − 1803 XCDR (tail) = prev;
+ − 1804 prev = tail;
+ − 1805 tail = next;
+ − 1806 }
+ − 1807 UNGCPRO;
+ − 1808 return prev;
+ − 1809 }
+ − 1810
+ − 1811 DEFUN ("reverse", Freverse, 1, 1, 0, /*
+ − 1812 Reverse LIST, copying. Return the beginning of the reversed list.
+ − 1813 See also the function `nreverse', which is used more often.
+ − 1814 */
+ − 1815 (list))
+ − 1816 {
+ − 1817 Lisp_Object reversed_list = Qnil;
+ − 1818 EXTERNAL_LIST_LOOP_2 (elt, list)
+ − 1819 {
+ − 1820 reversed_list = Fcons (elt, reversed_list);
+ − 1821 }
+ − 1822 return reversed_list;
+ − 1823 }
+ − 1824
+ − 1825 static Lisp_Object list_merge (Lisp_Object org_l1, Lisp_Object org_l2,
+ − 1826 Lisp_Object lisp_arg,
+ − 1827 int (*pred_fn) (Lisp_Object, Lisp_Object,
+ − 1828 Lisp_Object lisp_arg));
+ − 1829
872
+ − 1830 /* The sort function should return > 0 if OBJ1 < OBJ2, < 0 otherwise.
+ − 1831 NOTE: This is backwards from the way qsort() works. */
+ − 1832
428
+ − 1833 Lisp_Object
+ − 1834 list_sort (Lisp_Object list,
+ − 1835 Lisp_Object lisp_arg,
872
+ − 1836 int (*pred_fn) (Lisp_Object obj1, Lisp_Object obj2,
428
+ − 1837 Lisp_Object lisp_arg))
+ − 1838 {
+ − 1839 struct gcpro gcpro1, gcpro2, gcpro3;
+ − 1840 Lisp_Object back, tem;
+ − 1841 Lisp_Object front = list;
+ − 1842 Lisp_Object len = Flength (list);
444
+ − 1843
+ − 1844 if (XINT (len) < 2)
428
+ − 1845 return list;
+ − 1846
444
+ − 1847 len = make_int (XINT (len) / 2 - 1);
428
+ − 1848 tem = Fnthcdr (len, list);
+ − 1849 back = Fcdr (tem);
+ − 1850 Fsetcdr (tem, Qnil);
+ − 1851
+ − 1852 GCPRO3 (front, back, lisp_arg);
+ − 1853 front = list_sort (front, lisp_arg, pred_fn);
+ − 1854 back = list_sort (back, lisp_arg, pred_fn);
+ − 1855 UNGCPRO;
+ − 1856 return list_merge (front, back, lisp_arg, pred_fn);
+ − 1857 }
+ − 1858
+ − 1859
+ − 1860 static int
+ − 1861 merge_pred_function (Lisp_Object obj1, Lisp_Object obj2,
+ − 1862 Lisp_Object pred)
+ − 1863 {
+ − 1864 Lisp_Object tmp;
+ − 1865
+ − 1866 /* prevents the GC from happening in call2 */
853
+ − 1867 /* Emacs' GC doesn't actually relocate pointers, so this probably
+ − 1868 isn't strictly necessary */
771
+ − 1869 int speccount = begin_gc_forbidden ();
428
+ − 1870 tmp = call2 (pred, obj1, obj2);
771
+ − 1871 unbind_to (speccount);
428
+ − 1872
+ − 1873 if (NILP (tmp))
+ − 1874 return -1;
+ − 1875 else
+ − 1876 return 1;
+ − 1877 }
+ − 1878
+ − 1879 DEFUN ("sort", Fsort, 2, 2, 0, /*
+ − 1880 Sort LIST, stably, comparing elements using PREDICATE.
+ − 1881 Returns the sorted list. LIST is modified by side effects.
+ − 1882 PREDICATE is called with two elements of LIST, and should return T
+ − 1883 if the first element is "less" than the second.
+ − 1884 */
444
+ − 1885 (list, predicate))
428
+ − 1886 {
444
+ − 1887 return list_sort (list, predicate, merge_pred_function);
428
+ − 1888 }
+ − 1889
+ − 1890 Lisp_Object
+ − 1891 merge (Lisp_Object org_l1, Lisp_Object org_l2,
+ − 1892 Lisp_Object pred)
+ − 1893 {
+ − 1894 return list_merge (org_l1, org_l2, pred, merge_pred_function);
+ − 1895 }
+ − 1896
+ − 1897
+ − 1898 static Lisp_Object
+ − 1899 list_merge (Lisp_Object org_l1, Lisp_Object org_l2,
+ − 1900 Lisp_Object lisp_arg,
+ − 1901 int (*pred_fn) (Lisp_Object, Lisp_Object, Lisp_Object lisp_arg))
+ − 1902 {
+ − 1903 Lisp_Object value;
+ − 1904 Lisp_Object tail;
+ − 1905 Lisp_Object tem;
+ − 1906 Lisp_Object l1, l2;
+ − 1907 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
+ − 1908
+ − 1909 l1 = org_l1;
+ − 1910 l2 = org_l2;
+ − 1911 tail = Qnil;
+ − 1912 value = Qnil;
+ − 1913
+ − 1914 /* It is sufficient to protect org_l1 and org_l2.
+ − 1915 When l1 and l2 are updated, we copy the new values
+ − 1916 back into the org_ vars. */
+ − 1917
+ − 1918 GCPRO4 (org_l1, org_l2, lisp_arg, value);
+ − 1919
+ − 1920 while (1)
+ − 1921 {
+ − 1922 if (NILP (l1))
+ − 1923 {
+ − 1924 UNGCPRO;
+ − 1925 if (NILP (tail))
+ − 1926 return l2;
+ − 1927 Fsetcdr (tail, l2);
+ − 1928 return value;
+ − 1929 }
+ − 1930 if (NILP (l2))
+ − 1931 {
+ − 1932 UNGCPRO;
+ − 1933 if (NILP (tail))
+ − 1934 return l1;
+ − 1935 Fsetcdr (tail, l1);
+ − 1936 return value;
+ − 1937 }
+ − 1938
+ − 1939 if (((*pred_fn) (Fcar (l2), Fcar (l1), lisp_arg)) < 0)
+ − 1940 {
+ − 1941 tem = l1;
+ − 1942 l1 = Fcdr (l1);
+ − 1943 org_l1 = l1;
+ − 1944 }
+ − 1945 else
+ − 1946 {
+ − 1947 tem = l2;
+ − 1948 l2 = Fcdr (l2);
+ − 1949 org_l2 = l2;
+ − 1950 }
+ − 1951 if (NILP (tail))
+ − 1952 value = tem;
+ − 1953 else
+ − 1954 Fsetcdr (tail, tem);
+ − 1955 tail = tem;
+ − 1956 }
+ − 1957 }
+ − 1958
+ − 1959
+ − 1960 /************************************************************************/
+ − 1961 /* property-list functions */
+ − 1962 /************************************************************************/
+ − 1963
+ − 1964 /* For properties of text, we need to do order-insensitive comparison of
+ − 1965 plists. That is, we need to compare two plists such that they are the
+ − 1966 same if they have the same set of keys, and equivalent values.
+ − 1967 So (a 1 b 2) would be equal to (b 2 a 1).
+ − 1968
+ − 1969 NIL_MEANS_NOT_PRESENT is as in `plists-eq' etc.
+ − 1970 LAXP means use `equal' for comparisons.
+ − 1971 */
+ − 1972 int
+ − 1973 plists_differ (Lisp_Object a, Lisp_Object b, int nil_means_not_present,
+ − 1974 int laxp, int depth)
+ − 1975 {
438
+ − 1976 int eqp = (depth == -1); /* -1 as depth means use eq, not equal. */
428
+ − 1977 int la, lb, m, i, fill;
+ − 1978 Lisp_Object *keys, *vals;
+ − 1979 char *flags;
+ − 1980 Lisp_Object rest;
+ − 1981
+ − 1982 if (NILP (a) && NILP (b))
+ − 1983 return 0;
+ − 1984
+ − 1985 Fcheck_valid_plist (a);
+ − 1986 Fcheck_valid_plist (b);
+ − 1987
+ − 1988 la = XINT (Flength (a));
+ − 1989 lb = XINT (Flength (b));
+ − 1990 m = (la > lb ? la : lb);
+ − 1991 fill = 0;
+ − 1992 keys = alloca_array (Lisp_Object, m);
+ − 1993 vals = alloca_array (Lisp_Object, m);
+ − 1994 flags = alloca_array (char, m);
+ − 1995
+ − 1996 /* First extract the pairs from A. */
+ − 1997 for (rest = a; !NILP (rest); rest = XCDR (XCDR (rest)))
+ − 1998 {
+ − 1999 Lisp_Object k = XCAR (rest);
+ − 2000 Lisp_Object v = XCAR (XCDR (rest));
+ − 2001 /* Maybe be Ebolified. */
+ − 2002 if (nil_means_not_present && NILP (v)) continue;
+ − 2003 keys [fill] = k;
+ − 2004 vals [fill] = v;
+ − 2005 flags[fill] = 0;
+ − 2006 fill++;
+ − 2007 }
+ − 2008 /* Now iterate over B, and stop if we find something that's not in A,
+ − 2009 or that doesn't match. As we match, mark them. */
+ − 2010 for (rest = b; !NILP (rest); rest = XCDR (XCDR (rest)))
+ − 2011 {
+ − 2012 Lisp_Object k = XCAR (rest);
+ − 2013 Lisp_Object v = XCAR (XCDR (rest));
+ − 2014 /* Maybe be Ebolified. */
+ − 2015 if (nil_means_not_present && NILP (v)) continue;
+ − 2016 for (i = 0; i < fill; i++)
+ − 2017 {
+ − 2018 if (!laxp ? EQ (k, keys [i]) : internal_equal (k, keys [i], depth))
+ − 2019 {
434
+ − 2020 if (eqp
+ − 2021 /* We narrowly escaped being Ebolified here. */
+ − 2022 ? !EQ_WITH_EBOLA_NOTICE (v, vals [i])
+ − 2023 : !internal_equal (v, vals [i], depth))
428
+ − 2024 /* a property in B has a different value than in A */
+ − 2025 goto MISMATCH;
+ − 2026 flags [i] = 1;
+ − 2027 break;
+ − 2028 }
+ − 2029 }
+ − 2030 if (i == fill)
+ − 2031 /* there are some properties in B that are not in A */
+ − 2032 goto MISMATCH;
+ − 2033 }
+ − 2034 /* Now check to see that all the properties in A were also in B */
+ − 2035 for (i = 0; i < fill; i++)
+ − 2036 if (flags [i] == 0)
+ − 2037 goto MISMATCH;
+ − 2038
+ − 2039 /* Ok. */
+ − 2040 return 0;
+ − 2041
+ − 2042 MISMATCH:
+ − 2043 return 1;
+ − 2044 }
+ − 2045
+ − 2046 DEFUN ("plists-eq", Fplists_eq, 2, 3, 0, /*
+ − 2047 Return non-nil if property lists A and B are `eq'.
+ − 2048 A property list is an alternating list of keywords and values.
+ − 2049 This function does order-insensitive comparisons of the property lists:
+ − 2050 For example, the property lists '(a 1 b 2) and '(b 2 a 1) are equal.
+ − 2051 Comparison between values is done using `eq'. See also `plists-equal'.
+ − 2052 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
+ − 2053 a nil value is ignored. This feature is a virus that has infected
+ − 2054 old Lisp implementations, but should not be used except for backward
+ − 2055 compatibility.
+ − 2056 */
+ − 2057 (a, b, nil_means_not_present))
+ − 2058 {
+ − 2059 return (plists_differ (a, b, !NILP (nil_means_not_present), 0, -1)
+ − 2060 ? Qnil : Qt);
+ − 2061 }
+ − 2062
+ − 2063 DEFUN ("plists-equal", Fplists_equal, 2, 3, 0, /*
+ − 2064 Return non-nil if property lists A and B are `equal'.
+ − 2065 A property list is an alternating list of keywords and values. This
+ − 2066 function does order-insensitive comparisons of the property lists: For
+ − 2067 example, the property lists '(a 1 b 2) and '(b 2 a 1) are equal.
+ − 2068 Comparison between values is done using `equal'. See also `plists-eq'.
+ − 2069 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
+ − 2070 a nil value is ignored. This feature is a virus that has infected
+ − 2071 old Lisp implementations, but should not be used except for backward
+ − 2072 compatibility.
+ − 2073 */
+ − 2074 (a, b, nil_means_not_present))
+ − 2075 {
+ − 2076 return (plists_differ (a, b, !NILP (nil_means_not_present), 0, 1)
+ − 2077 ? Qnil : Qt);
+ − 2078 }
+ − 2079
+ − 2080
+ − 2081 DEFUN ("lax-plists-eq", Flax_plists_eq, 2, 3, 0, /*
+ − 2082 Return non-nil if lax property lists A and B are `eq'.
+ − 2083 A property list is an alternating list of keywords and values.
+ − 2084 This function does order-insensitive comparisons of the property lists:
+ − 2085 For example, the property lists '(a 1 b 2) and '(b 2 a 1) are equal.
+ − 2086 Comparison between values is done using `eq'. See also `plists-equal'.
+ − 2087 A lax property list is like a regular one except that comparisons between
+ − 2088 keywords is done using `equal' instead of `eq'.
+ − 2089 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
+ − 2090 a nil value is ignored. This feature is a virus that has infected
+ − 2091 old Lisp implementations, but should not be used except for backward
+ − 2092 compatibility.
+ − 2093 */
+ − 2094 (a, b, nil_means_not_present))
+ − 2095 {
+ − 2096 return (plists_differ (a, b, !NILP (nil_means_not_present), 1, -1)
+ − 2097 ? Qnil : Qt);
+ − 2098 }
+ − 2099
+ − 2100 DEFUN ("lax-plists-equal", Flax_plists_equal, 2, 3, 0, /*
+ − 2101 Return non-nil if lax property lists A and B are `equal'.
+ − 2102 A property list is an alternating list of keywords and values. This
+ − 2103 function does order-insensitive comparisons of the property lists: For
+ − 2104 example, the property lists '(a 1 b 2) and '(b 2 a 1) are equal.
+ − 2105 Comparison between values is done using `equal'. See also `plists-eq'.
+ − 2106 A lax property list is like a regular one except that comparisons between
+ − 2107 keywords is done using `equal' instead of `eq'.
+ − 2108 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
+ − 2109 a nil value is ignored. This feature is a virus that has infected
+ − 2110 old Lisp implementations, but should not be used except for backward
+ − 2111 compatibility.
+ − 2112 */
+ − 2113 (a, b, nil_means_not_present))
+ − 2114 {
+ − 2115 return (plists_differ (a, b, !NILP (nil_means_not_present), 1, 1)
+ − 2116 ? Qnil : Qt);
+ − 2117 }
+ − 2118
+ − 2119 /* Return the value associated with key PROPERTY in property list PLIST.
+ − 2120 Return nil if key not found. This function is used for internal
+ − 2121 property lists that cannot be directly manipulated by the user.
+ − 2122 */
+ − 2123
+ − 2124 Lisp_Object
+ − 2125 internal_plist_get (Lisp_Object plist, Lisp_Object property)
+ − 2126 {
+ − 2127 Lisp_Object tail;
+ − 2128
+ − 2129 for (tail = plist; !NILP (tail); tail = XCDR (XCDR (tail)))
+ − 2130 {
+ − 2131 if (EQ (XCAR (tail), property))
+ − 2132 return XCAR (XCDR (tail));
+ − 2133 }
+ − 2134
+ − 2135 return Qunbound;
+ − 2136 }
+ − 2137
+ − 2138 /* Set PLIST's value for PROPERTY to VALUE. Analogous to
+ − 2139 internal_plist_get(). */
+ − 2140
+ − 2141 void
+ − 2142 internal_plist_put (Lisp_Object *plist, Lisp_Object property,
+ − 2143 Lisp_Object value)
+ − 2144 {
+ − 2145 Lisp_Object tail;
+ − 2146
+ − 2147 for (tail = *plist; !NILP (tail); tail = XCDR (XCDR (tail)))
+ − 2148 {
+ − 2149 if (EQ (XCAR (tail), property))
+ − 2150 {
+ − 2151 XCAR (XCDR (tail)) = value;
+ − 2152 return;
+ − 2153 }
+ − 2154 }
+ − 2155
+ − 2156 *plist = Fcons (property, Fcons (value, *plist));
+ − 2157 }
+ − 2158
+ − 2159 int
+ − 2160 internal_remprop (Lisp_Object *plist, Lisp_Object property)
+ − 2161 {
+ − 2162 Lisp_Object tail, prev;
+ − 2163
+ − 2164 for (tail = *plist, prev = Qnil;
+ − 2165 !NILP (tail);
+ − 2166 tail = XCDR (XCDR (tail)))
+ − 2167 {
+ − 2168 if (EQ (XCAR (tail), property))
+ − 2169 {
+ − 2170 if (NILP (prev))
+ − 2171 *plist = XCDR (XCDR (tail));
+ − 2172 else
+ − 2173 XCDR (XCDR (prev)) = XCDR (XCDR (tail));
+ − 2174 return 1;
+ − 2175 }
+ − 2176 else
+ − 2177 prev = tail;
+ − 2178 }
+ − 2179
+ − 2180 return 0;
+ − 2181 }
+ − 2182
+ − 2183 /* Called on a malformed property list. BADPLACE should be some
+ − 2184 place where truncating will form a good list -- i.e. we shouldn't
+ − 2185 result in a list with an odd length. */
+ − 2186
+ − 2187 static Lisp_Object
578
+ − 2188 bad_bad_bunny (Lisp_Object *plist, Lisp_Object *badplace, Error_Behavior errb)
428
+ − 2189 {
+ − 2190 if (ERRB_EQ (errb, ERROR_ME))
+ − 2191 return Fsignal (Qmalformed_property_list, list2 (*plist, *badplace));
+ − 2192 else
+ − 2193 {
+ − 2194 if (ERRB_EQ (errb, ERROR_ME_WARN))
+ − 2195 {
+ − 2196 warn_when_safe_lispobj
+ − 2197 (Qlist, Qwarning,
771
+ − 2198 list2 (build_msg_string
428
+ − 2199 ("Malformed property list -- list has been truncated"),
+ − 2200 *plist));
793
+ − 2201 /* #### WARNING: This is more dangerous than it seems; perhaps
+ − 2202 not a good idea. It also violates the principle of least
+ − 2203 surprise -- passing in ERROR_ME_WARN causes truncation, but
+ − 2204 ERROR_ME and ERROR_ME_NOT don't. */
428
+ − 2205 *badplace = Qnil;
+ − 2206 }
+ − 2207 return Qunbound;
+ − 2208 }
+ − 2209 }
+ − 2210
+ − 2211 /* Called on a circular property list. BADPLACE should be some place
+ − 2212 where truncating will result in an even-length list, as above.
+ − 2213 If doesn't particularly matter where we truncate -- anywhere we
+ − 2214 truncate along the entire list will break the circularity, because
+ − 2215 it will create a terminus and the list currently doesn't have one.
+ − 2216 */
+ − 2217
+ − 2218 static Lisp_Object
578
+ − 2219 bad_bad_turtle (Lisp_Object *plist, Lisp_Object *badplace, Error_Behavior errb)
428
+ − 2220 {
+ − 2221 if (ERRB_EQ (errb, ERROR_ME))
+ − 2222 return Fsignal (Qcircular_property_list, list1 (*plist));
+ − 2223 else
+ − 2224 {
+ − 2225 if (ERRB_EQ (errb, ERROR_ME_WARN))
+ − 2226 {
+ − 2227 warn_when_safe_lispobj
+ − 2228 (Qlist, Qwarning,
771
+ − 2229 list2 (build_msg_string
428
+ − 2230 ("Circular property list -- list has been truncated"),
+ − 2231 *plist));
793
+ − 2232 /* #### WARNING: This is more dangerous than it seems; perhaps
+ − 2233 not a good idea. It also violates the principle of least
+ − 2234 surprise -- passing in ERROR_ME_WARN causes truncation, but
+ − 2235 ERROR_ME and ERROR_ME_NOT don't. */
428
+ − 2236 *badplace = Qnil;
+ − 2237 }
+ − 2238 return Qunbound;
+ − 2239 }
+ − 2240 }
+ − 2241
+ − 2242 /* Advance the tortoise pointer by two (one iteration of a property-list
+ − 2243 loop) and the hare pointer by four and verify that no malformations
+ − 2244 or circularities exist. If so, return zero and store a value into
+ − 2245 RETVAL that should be returned by the calling function. Otherwise,
+ − 2246 return 1. See external_plist_get().
+ − 2247 */
+ − 2248
+ − 2249 static int
+ − 2250 advance_plist_pointers (Lisp_Object *plist,
+ − 2251 Lisp_Object **tortoise, Lisp_Object **hare,
578
+ − 2252 Error_Behavior errb, Lisp_Object *retval)
428
+ − 2253 {
+ − 2254 int i;
+ − 2255 Lisp_Object *tortsave = *tortoise;
+ − 2256
+ − 2257 /* Note that our "fixing" may be more brutal than necessary,
+ − 2258 but it's the user's own problem, not ours, if they went in and
+ − 2259 manually fucked up a plist. */
+ − 2260
+ − 2261 for (i = 0; i < 2; i++)
+ − 2262 {
+ − 2263 /* This is a standard iteration of a defensive-loop-checking
+ − 2264 loop. We just do it twice because we want to advance past
+ − 2265 both the property and its value.
+ − 2266
+ − 2267 If the pointer indirection is confusing you, remember that
+ − 2268 one level of indirection on the hare and tortoise pointers
+ − 2269 is only due to pass-by-reference for this function. The other
+ − 2270 level is so that the plist can be fixed in place. */
+ − 2271
+ − 2272 /* When we reach the end of a well-formed plist, **HARE is
+ − 2273 nil. In that case, we don't do anything at all except
+ − 2274 advance TORTOISE by one. Otherwise, we advance HARE
+ − 2275 by two (making sure it's OK to do so), then advance
+ − 2276 TORTOISE by one (it will always be OK to do so because
+ − 2277 the HARE is always ahead of the TORTOISE and will have
+ − 2278 already verified the path), then make sure TORTOISE and
+ − 2279 HARE don't contain the same non-nil object -- if the
+ − 2280 TORTOISE and the HARE ever meet, then obviously we're
+ − 2281 in a circularity, and if we're in a circularity, then
+ − 2282 the TORTOISE and the HARE can't cross paths without
+ − 2283 meeting, since the HARE only gains one step over the
+ − 2284 TORTOISE per iteration. */
+ − 2285
+ − 2286 if (!NILP (**hare))
+ − 2287 {
+ − 2288 Lisp_Object *haresave = *hare;
+ − 2289 if (!CONSP (**hare))
+ − 2290 {
+ − 2291 *retval = bad_bad_bunny (plist, haresave, errb);
+ − 2292 return 0;
+ − 2293 }
+ − 2294 *hare = &XCDR (**hare);
+ − 2295 /* In a non-plist, we'd check here for a nil value for
+ − 2296 **HARE, which is OK (it just means the list has an
+ − 2297 odd number of elements). In a plist, it's not OK
+ − 2298 for the list to have an odd number of elements. */
+ − 2299 if (!CONSP (**hare))
+ − 2300 {
+ − 2301 *retval = bad_bad_bunny (plist, haresave, errb);
+ − 2302 return 0;
+ − 2303 }
+ − 2304 *hare = &XCDR (**hare);
+ − 2305 }
+ − 2306
+ − 2307 *tortoise = &XCDR (**tortoise);
+ − 2308 if (!NILP (**hare) && EQ (**tortoise, **hare))
+ − 2309 {
+ − 2310 *retval = bad_bad_turtle (plist, tortsave, errb);
+ − 2311 return 0;
+ − 2312 }
+ − 2313 }
+ − 2314
+ − 2315 return 1;
+ − 2316 }
+ − 2317
+ − 2318 /* Return the value of PROPERTY from PLIST, or Qunbound if
+ − 2319 property is not on the list.
+ − 2320
+ − 2321 PLIST is a Lisp-accessible property list, meaning that it
+ − 2322 has to be checked for malformations and circularities.
+ − 2323
+ − 2324 If ERRB is ERROR_ME, an error will be signalled. Otherwise, the
+ − 2325 function will never signal an error; and if ERRB is ERROR_ME_WARN,
+ − 2326 on finding a malformation or a circularity, it issues a warning and
+ − 2327 attempts to silently fix the problem.
+ − 2328
+ − 2329 A pointer to PLIST is passed in so that PLIST can be successfully
+ − 2330 "fixed" even if the error is at the beginning of the plist. */
+ − 2331
+ − 2332 Lisp_Object
+ − 2333 external_plist_get (Lisp_Object *plist, Lisp_Object property,
578
+ − 2334 int laxp, Error_Behavior errb)
428
+ − 2335 {
+ − 2336 Lisp_Object *tortoise = plist;
+ − 2337 Lisp_Object *hare = plist;
+ − 2338
+ − 2339 while (!NILP (*tortoise))
+ − 2340 {
+ − 2341 Lisp_Object *tortsave = tortoise;
+ − 2342 Lisp_Object retval;
+ − 2343
+ − 2344 /* We do the standard tortoise/hare march. We isolate the
+ − 2345 grungy stuff to do this in advance_plist_pointers(), though.
+ − 2346 To us, all this function does is advance the tortoise
+ − 2347 pointer by two and the hare pointer by four and make sure
+ − 2348 everything's OK. We first advance the pointers and then
+ − 2349 check if a property matched; this ensures that our
+ − 2350 check for a matching property is safe. */
+ − 2351
+ − 2352 if (!advance_plist_pointers (plist, &tortoise, &hare, errb, &retval))
+ − 2353 return retval;
+ − 2354
+ − 2355 if (!laxp ? EQ (XCAR (*tortsave), property)
+ − 2356 : internal_equal (XCAR (*tortsave), property, 0))
+ − 2357 return XCAR (XCDR (*tortsave));
+ − 2358 }
+ − 2359
+ − 2360 return Qunbound;
+ − 2361 }
+ − 2362
+ − 2363 /* Set PLIST's value for PROPERTY to VALUE, given a possibly
+ − 2364 malformed or circular plist. Analogous to external_plist_get(). */
+ − 2365
+ − 2366 void
+ − 2367 external_plist_put (Lisp_Object *plist, Lisp_Object property,
578
+ − 2368 Lisp_Object value, int laxp, Error_Behavior errb)
428
+ − 2369 {
+ − 2370 Lisp_Object *tortoise = plist;
+ − 2371 Lisp_Object *hare = plist;
+ − 2372
+ − 2373 while (!NILP (*tortoise))
+ − 2374 {
+ − 2375 Lisp_Object *tortsave = tortoise;
+ − 2376 Lisp_Object retval;
+ − 2377
+ − 2378 /* See above */
+ − 2379 if (!advance_plist_pointers (plist, &tortoise, &hare, errb, &retval))
+ − 2380 return;
+ − 2381
+ − 2382 if (!laxp ? EQ (XCAR (*tortsave), property)
+ − 2383 : internal_equal (XCAR (*tortsave), property, 0))
+ − 2384 {
+ − 2385 XCAR (XCDR (*tortsave)) = value;
+ − 2386 return;
+ − 2387 }
+ − 2388 }
+ − 2389
+ − 2390 *plist = Fcons (property, Fcons (value, *plist));
+ − 2391 }
+ − 2392
+ − 2393 int
+ − 2394 external_remprop (Lisp_Object *plist, Lisp_Object property,
578
+ − 2395 int laxp, Error_Behavior errb)
428
+ − 2396 {
+ − 2397 Lisp_Object *tortoise = plist;
+ − 2398 Lisp_Object *hare = plist;
+ − 2399
+ − 2400 while (!NILP (*tortoise))
+ − 2401 {
+ − 2402 Lisp_Object *tortsave = tortoise;
+ − 2403 Lisp_Object retval;
+ − 2404
+ − 2405 /* See above */
+ − 2406 if (!advance_plist_pointers (plist, &tortoise, &hare, errb, &retval))
+ − 2407 return 0;
+ − 2408
+ − 2409 if (!laxp ? EQ (XCAR (*tortsave), property)
+ − 2410 : internal_equal (XCAR (*tortsave), property, 0))
+ − 2411 {
+ − 2412 /* Now you see why it's so convenient to have that level
+ − 2413 of indirection. */
+ − 2414 *tortsave = XCDR (XCDR (*tortsave));
+ − 2415 return 1;
+ − 2416 }
+ − 2417 }
+ − 2418
+ − 2419 return 0;
+ − 2420 }
+ − 2421
+ − 2422 DEFUN ("plist-get", Fplist_get, 2, 3, 0, /*
+ − 2423 Extract a value from a property list.
+ − 2424 PLIST is a property list, which is a list of the form
444
+ − 2425 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2...).
+ − 2426 PROPERTY is usually a symbol.
+ − 2427 This function returns the value corresponding to the PROPERTY,
+ − 2428 or DEFAULT if PROPERTY is not one of the properties on the list.
428
+ − 2429 */
444
+ − 2430 (plist, property, default_))
428
+ − 2431 {
444
+ − 2432 Lisp_Object value = external_plist_get (&plist, property, 0, ERROR_ME);
+ − 2433 return UNBOUNDP (value) ? default_ : value;
428
+ − 2434 }
+ − 2435
+ − 2436 DEFUN ("plist-put", Fplist_put, 3, 3, 0, /*
444
+ − 2437 Change value in PLIST of PROPERTY to VALUE.
+ − 2438 PLIST is a property list, which is a list of the form
+ − 2439 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2 ...).
+ − 2440 PROPERTY is usually a symbol and VALUE is any object.
+ − 2441 If PROPERTY is already a property on the list, its value is set to VALUE,
+ − 2442 otherwise the new PROPERTY VALUE pair is added.
+ − 2443 The new plist is returned; use `(setq x (plist-put x property value))'
+ − 2444 to be sure to use the new value. PLIST is modified by side effect.
428
+ − 2445 */
444
+ − 2446 (plist, property, value))
428
+ − 2447 {
444
+ − 2448 external_plist_put (&plist, property, value, 0, ERROR_ME);
428
+ − 2449 return plist;
+ − 2450 }
+ − 2451
+ − 2452 DEFUN ("plist-remprop", Fplist_remprop, 2, 2, 0, /*
444
+ − 2453 Remove from PLIST the property PROPERTY and its value.
+ − 2454 PLIST is a property list, which is a list of the form
+ − 2455 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2 ...).
+ − 2456 PROPERTY is usually a symbol.
+ − 2457 The new plist is returned; use `(setq x (plist-remprop x property))'
+ − 2458 to be sure to use the new value. PLIST is modified by side effect.
428
+ − 2459 */
444
+ − 2460 (plist, property))
428
+ − 2461 {
444
+ − 2462 external_remprop (&plist, property, 0, ERROR_ME);
428
+ − 2463 return plist;
+ − 2464 }
+ − 2465
+ − 2466 DEFUN ("plist-member", Fplist_member, 2, 2, 0, /*
444
+ − 2467 Return t if PROPERTY has a value specified in PLIST.
428
+ − 2468 */
444
+ − 2469 (plist, property))
428
+ − 2470 {
444
+ − 2471 Lisp_Object value = Fplist_get (plist, property, Qunbound);
+ − 2472 return UNBOUNDP (value) ? Qnil : Qt;
428
+ − 2473 }
+ − 2474
+ − 2475 DEFUN ("check-valid-plist", Fcheck_valid_plist, 1, 1, 0, /*
+ − 2476 Given a plist, signal an error if there is anything wrong with it.
+ − 2477 This means that it's a malformed or circular plist.
+ − 2478 */
+ − 2479 (plist))
+ − 2480 {
+ − 2481 Lisp_Object *tortoise;
+ − 2482 Lisp_Object *hare;
+ − 2483
+ − 2484 start_over:
+ − 2485 tortoise = &plist;
+ − 2486 hare = &plist;
+ − 2487 while (!NILP (*tortoise))
+ − 2488 {
+ − 2489 Lisp_Object retval;
+ − 2490
+ − 2491 /* See above */
+ − 2492 if (!advance_plist_pointers (&plist, &tortoise, &hare, ERROR_ME,
+ − 2493 &retval))
+ − 2494 goto start_over;
+ − 2495 }
+ − 2496
+ − 2497 return Qnil;
+ − 2498 }
+ − 2499
+ − 2500 DEFUN ("valid-plist-p", Fvalid_plist_p, 1, 1, 0, /*
+ − 2501 Given a plist, return non-nil if its format is correct.
+ − 2502 If it returns nil, `check-valid-plist' will signal an error when given
442
+ − 2503 the plist; that means it's a malformed or circular plist.
428
+ − 2504 */
+ − 2505 (plist))
+ − 2506 {
+ − 2507 Lisp_Object *tortoise;
+ − 2508 Lisp_Object *hare;
+ − 2509
+ − 2510 tortoise = &plist;
+ − 2511 hare = &plist;
+ − 2512 while (!NILP (*tortoise))
+ − 2513 {
+ − 2514 Lisp_Object retval;
+ − 2515
+ − 2516 /* See above */
+ − 2517 if (!advance_plist_pointers (&plist, &tortoise, &hare, ERROR_ME_NOT,
+ − 2518 &retval))
+ − 2519 return Qnil;
+ − 2520 }
+ − 2521
+ − 2522 return Qt;
+ − 2523 }
+ − 2524
+ − 2525 DEFUN ("canonicalize-plist", Fcanonicalize_plist, 1, 2, 0, /*
+ − 2526 Destructively remove any duplicate entries from a plist.
+ − 2527 In such cases, the first entry applies.
+ − 2528
+ − 2529 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
+ − 2530 a nil value is removed. This feature is a virus that has infected
+ − 2531 old Lisp implementations, but should not be used except for backward
+ − 2532 compatibility.
+ − 2533
+ − 2534 The new plist is returned. If NIL-MEANS-NOT-PRESENT is given, the
+ − 2535 return value may not be EQ to the passed-in value, so make sure to
+ − 2536 `setq' the value back into where it came from.
+ − 2537 */
+ − 2538 (plist, nil_means_not_present))
+ − 2539 {
+ − 2540 Lisp_Object head = plist;
+ − 2541
+ − 2542 Fcheck_valid_plist (plist);
+ − 2543
+ − 2544 while (!NILP (plist))
+ − 2545 {
+ − 2546 Lisp_Object prop = Fcar (plist);
+ − 2547 Lisp_Object next = Fcdr (plist);
+ − 2548
+ − 2549 CHECK_CONS (next); /* just make doubly sure we catch any errors */
+ − 2550 if (!NILP (nil_means_not_present) && NILP (Fcar (next)))
+ − 2551 {
+ − 2552 if (EQ (head, plist))
+ − 2553 head = Fcdr (next);
+ − 2554 plist = Fcdr (next);
+ − 2555 continue;
+ − 2556 }
+ − 2557 /* external_remprop returns 1 if it removed any property.
+ − 2558 We have to loop till it didn't remove anything, in case
+ − 2559 the property occurs many times. */
+ − 2560 while (external_remprop (&XCDR (next), prop, 0, ERROR_ME))
+ − 2561 DO_NOTHING;
+ − 2562 plist = Fcdr (next);
+ − 2563 }
+ − 2564
+ − 2565 return head;
+ − 2566 }
+ − 2567
+ − 2568 DEFUN ("lax-plist-get", Flax_plist_get, 2, 3, 0, /*
+ − 2569 Extract a value from a lax property list.
444
+ − 2570 LAX-PLIST is a lax property list, which is a list of the form
+ − 2571 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2...), where comparisons between
+ − 2572 properties is done using `equal' instead of `eq'.
+ − 2573 PROPERTY is usually a symbol.
+ − 2574 This function returns the value corresponding to PROPERTY,
+ − 2575 or DEFAULT if PROPERTY is not one of the properties on the list.
428
+ − 2576 */
444
+ − 2577 (lax_plist, property, default_))
428
+ − 2578 {
444
+ − 2579 Lisp_Object value = external_plist_get (&lax_plist, property, 1, ERROR_ME);
+ − 2580 return UNBOUNDP (value) ? default_ : value;
428
+ − 2581 }
+ − 2582
+ − 2583 DEFUN ("lax-plist-put", Flax_plist_put, 3, 3, 0, /*
444
+ − 2584 Change value in LAX-PLIST of PROPERTY to VALUE.
+ − 2585 LAX-PLIST is a lax property list, which is a list of the form
+ − 2586 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2...), where comparisons between
+ − 2587 properties is done using `equal' instead of `eq'.
+ − 2588 PROPERTY is usually a symbol and VALUE is any object.
+ − 2589 If PROPERTY is already a property on the list, its value is set to
+ − 2590 VALUE, otherwise the new PROPERTY VALUE pair is added.
+ − 2591 The new plist is returned; use `(setq x (lax-plist-put x property value))'
+ − 2592 to be sure to use the new value. LAX-PLIST is modified by side effect.
428
+ − 2593 */
444
+ − 2594 (lax_plist, property, value))
428
+ − 2595 {
444
+ − 2596 external_plist_put (&lax_plist, property, value, 1, ERROR_ME);
428
+ − 2597 return lax_plist;
+ − 2598 }
+ − 2599
+ − 2600 DEFUN ("lax-plist-remprop", Flax_plist_remprop, 2, 2, 0, /*
444
+ − 2601 Remove from LAX-PLIST the property PROPERTY and its value.
+ − 2602 LAX-PLIST is a lax property list, which is a list of the form
+ − 2603 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2...), where comparisons between
+ − 2604 properties is done using `equal' instead of `eq'.
+ − 2605 PROPERTY is usually a symbol.
+ − 2606 The new plist is returned; use `(setq x (lax-plist-remprop x property))'
+ − 2607 to be sure to use the new value. LAX-PLIST is modified by side effect.
428
+ − 2608 */
444
+ − 2609 (lax_plist, property))
428
+ − 2610 {
444
+ − 2611 external_remprop (&lax_plist, property, 1, ERROR_ME);
428
+ − 2612 return lax_plist;
+ − 2613 }
+ − 2614
+ − 2615 DEFUN ("lax-plist-member", Flax_plist_member, 2, 2, 0, /*
444
+ − 2616 Return t if PROPERTY has a value specified in LAX-PLIST.
+ − 2617 LAX-PLIST is a lax property list, which is a list of the form
+ − 2618 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2...), where comparisons between
+ − 2619 properties is done using `equal' instead of `eq'.
428
+ − 2620 */
444
+ − 2621 (lax_plist, property))
428
+ − 2622 {
444
+ − 2623 return UNBOUNDP (Flax_plist_get (lax_plist, property, Qunbound)) ? Qnil : Qt;
428
+ − 2624 }
+ − 2625
+ − 2626 DEFUN ("canonicalize-lax-plist", Fcanonicalize_lax_plist, 1, 2, 0, /*
+ − 2627 Destructively remove any duplicate entries from a lax plist.
+ − 2628 In such cases, the first entry applies.
+ − 2629
+ − 2630 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
+ − 2631 a nil value is removed. This feature is a virus that has infected
+ − 2632 old Lisp implementations, but should not be used except for backward
+ − 2633 compatibility.
+ − 2634
+ − 2635 The new plist is returned. If NIL-MEANS-NOT-PRESENT is given, the
+ − 2636 return value may not be EQ to the passed-in value, so make sure to
+ − 2637 `setq' the value back into where it came from.
+ − 2638 */
+ − 2639 (lax_plist, nil_means_not_present))
+ − 2640 {
+ − 2641 Lisp_Object head = lax_plist;
+ − 2642
+ − 2643 Fcheck_valid_plist (lax_plist);
+ − 2644
+ − 2645 while (!NILP (lax_plist))
+ − 2646 {
+ − 2647 Lisp_Object prop = Fcar (lax_plist);
+ − 2648 Lisp_Object next = Fcdr (lax_plist);
+ − 2649
+ − 2650 CHECK_CONS (next); /* just make doubly sure we catch any errors */
+ − 2651 if (!NILP (nil_means_not_present) && NILP (Fcar (next)))
+ − 2652 {
+ − 2653 if (EQ (head, lax_plist))
+ − 2654 head = Fcdr (next);
+ − 2655 lax_plist = Fcdr (next);
+ − 2656 continue;
+ − 2657 }
+ − 2658 /* external_remprop returns 1 if it removed any property.
+ − 2659 We have to loop till it didn't remove anything, in case
+ − 2660 the property occurs many times. */
+ − 2661 while (external_remprop (&XCDR (next), prop, 1, ERROR_ME))
+ − 2662 DO_NOTHING;
+ − 2663 lax_plist = Fcdr (next);
+ − 2664 }
+ − 2665
+ − 2666 return head;
+ − 2667 }
+ − 2668
+ − 2669 /* In C because the frame props stuff uses it */
+ − 2670
+ − 2671 DEFUN ("destructive-alist-to-plist", Fdestructive_alist_to_plist, 1, 1, 0, /*
+ − 2672 Convert association list ALIST into the equivalent property-list form.
+ − 2673 The plist is returned. This converts from
+ − 2674
+ − 2675 \((a . 1) (b . 2) (c . 3))
+ − 2676
+ − 2677 into
+ − 2678
+ − 2679 \(a 1 b 2 c 3)
+ − 2680
+ − 2681 The original alist is destroyed in the process of constructing the plist.
+ − 2682 See also `alist-to-plist'.
+ − 2683 */
+ − 2684 (alist))
+ − 2685 {
+ − 2686 Lisp_Object head = alist;
+ − 2687 while (!NILP (alist))
+ − 2688 {
+ − 2689 /* remember the alist element. */
+ − 2690 Lisp_Object el = Fcar (alist);
+ − 2691
+ − 2692 Fsetcar (alist, Fcar (el));
+ − 2693 Fsetcar (el, Fcdr (el));
+ − 2694 Fsetcdr (el, Fcdr (alist));
+ − 2695 Fsetcdr (alist, el);
+ − 2696 alist = Fcdr (Fcdr (alist));
+ − 2697 }
+ − 2698
+ − 2699 return head;
+ − 2700 }
+ − 2701
+ − 2702 DEFUN ("get", Fget, 2, 3, 0, /*
442
+ − 2703 Return the value of OBJECT's PROPERTY property.
+ − 2704 This is the last VALUE stored with `(put OBJECT PROPERTY VALUE)'.
428
+ − 2705 If there is no such property, return optional third arg DEFAULT
442
+ − 2706 \(which defaults to `nil'). OBJECT can be a symbol, string, extent,
+ − 2707 face, or glyph. See also `put', `remprop', and `object-plist'.
428
+ − 2708 */
442
+ − 2709 (object, property, default_))
428
+ − 2710 {
+ − 2711 /* Various places in emacs call Fget() and expect it not to quit,
+ − 2712 so don't quit. */
442
+ − 2713 Lisp_Object val;
+ − 2714
+ − 2715 if (LRECORDP (object) && XRECORD_LHEADER_IMPLEMENTATION (object)->getprop)
+ − 2716 val = XRECORD_LHEADER_IMPLEMENTATION (object)->getprop (object, property);
428
+ − 2717 else
563
+ − 2718 invalid_operation ("Object type has no properties", object);
442
+ − 2719
+ − 2720 return UNBOUNDP (val) ? default_ : val;
428
+ − 2721 }
+ − 2722
+ − 2723 DEFUN ("put", Fput, 3, 3, 0, /*
442
+ − 2724 Set OBJECT's PROPERTY to VALUE.
+ − 2725 It can be subsequently retrieved with `(get OBJECT PROPERTY)'.
+ − 2726 OBJECT can be a symbol, face, extent, or string.
428
+ − 2727 For a string, no properties currently have predefined meanings.
+ − 2728 For the predefined properties for extents, see `set-extent-property'.
+ − 2729 For the predefined properties for faces, see `set-face-property'.
+ − 2730 See also `get', `remprop', and `object-plist'.
+ − 2731 */
442
+ − 2732 (object, property, value))
428
+ − 2733 {
1920
+ − 2734 /* This function cannot GC */
428
+ − 2735 CHECK_LISP_WRITEABLE (object);
+ − 2736
442
+ − 2737 if (LRECORDP (object) && XRECORD_LHEADER_IMPLEMENTATION (object)->putprop)
428
+ − 2738 {
442
+ − 2739 if (! XRECORD_LHEADER_IMPLEMENTATION (object)->putprop
+ − 2740 (object, property, value))
563
+ − 2741 invalid_change ("Can't set property on object", property);
428
+ − 2742 }
+ − 2743 else
563
+ − 2744 invalid_change ("Object type has no settable properties", object);
428
+ − 2745
+ − 2746 return value;
+ − 2747 }
+ − 2748
+ − 2749 DEFUN ("remprop", Fremprop, 2, 2, 0, /*
442
+ − 2750 Remove, from OBJECT's property list, PROPERTY and its corresponding value.
+ − 2751 OBJECT can be a symbol, string, extent, face, or glyph. Return non-nil
+ − 2752 if the property list was actually modified (i.e. if PROPERTY was present
+ − 2753 in the property list). See also `get', `put', and `object-plist'.
428
+ − 2754 */
442
+ − 2755 (object, property))
428
+ − 2756 {
442
+ − 2757 int ret = 0;
+ − 2758
428
+ − 2759 CHECK_LISP_WRITEABLE (object);
+ − 2760
442
+ − 2761 if (LRECORDP (object) && XRECORD_LHEADER_IMPLEMENTATION (object)->remprop)
428
+ − 2762 {
442
+ − 2763 ret = XRECORD_LHEADER_IMPLEMENTATION (object)->remprop (object, property);
+ − 2764 if (ret == -1)
563
+ − 2765 invalid_change ("Can't remove property from object", property);
428
+ − 2766 }
+ − 2767 else
563
+ − 2768 invalid_change ("Object type has no removable properties", object);
442
+ − 2769
+ − 2770 return ret ? Qt : Qnil;
428
+ − 2771 }
+ − 2772
+ − 2773 DEFUN ("object-plist", Fobject_plist, 1, 1, 0, /*
442
+ − 2774 Return a property list of OBJECT's properties.
+ − 2775 For a symbol, this is equivalent to `symbol-plist'.
+ − 2776 OBJECT can be a symbol, string, extent, face, or glyph.
+ − 2777 Do not modify the returned property list directly;
+ − 2778 this may or may not have the desired effects. Use `put' instead.
428
+ − 2779 */
+ − 2780 (object))
+ − 2781 {
442
+ − 2782 if (LRECORDP (object) && XRECORD_LHEADER_IMPLEMENTATION (object)->plist)
+ − 2783 return XRECORD_LHEADER_IMPLEMENTATION (object)->plist (object);
428
+ − 2784 else
563
+ − 2785 invalid_operation ("Object type has no properties", object);
428
+ − 2786
+ − 2787 return Qnil;
+ − 2788 }
+ − 2789
+ − 2790
853
+ − 2791 static Lisp_Object
+ − 2792 tweaked_internal_equal (Lisp_Object obj1, Lisp_Object obj2,
+ − 2793 Lisp_Object depth)
+ − 2794 {
+ − 2795 return make_int (internal_equal (obj1, obj2, XINT (depth)));
+ − 2796 }
+ − 2797
+ − 2798 int
+ − 2799 internal_equal_trapping_problems (Lisp_Object warning_class,
+ − 2800 const char *warning_string,
+ − 2801 int flags,
+ − 2802 struct call_trapping_problems_result *p,
+ − 2803 int retval,
+ − 2804 Lisp_Object obj1, Lisp_Object obj2,
+ − 2805 int depth)
+ − 2806 {
+ − 2807 Lisp_Object glorp =
+ − 2808 va_call_trapping_problems (warning_class, warning_string,
+ − 2809 flags, p,
+ − 2810 (lisp_fn_t) tweaked_internal_equal,
+ − 2811 3, obj1, obj2, make_int (depth));
+ − 2812 if (UNBOUNDP (glorp))
+ − 2813 return retval;
+ − 2814 else
+ − 2815 return XINT (glorp);
+ − 2816 }
+ − 2817
428
+ − 2818 int
+ − 2819 internal_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
+ − 2820 {
+ − 2821 if (depth > 200)
563
+ − 2822 stack_overflow ("Stack overflow in equal", Qunbound);
428
+ − 2823 QUIT;
+ − 2824 if (EQ_WITH_EBOLA_NOTICE (obj1, obj2))
+ − 2825 return 1;
+ − 2826 /* Note that (equal 20 20.0) should be nil */
+ − 2827 if (XTYPE (obj1) != XTYPE (obj2))
+ − 2828 return 0;
+ − 2829 if (LRECORDP (obj1))
+ − 2830 {
442
+ − 2831 const struct lrecord_implementation
428
+ − 2832 *imp1 = XRECORD_LHEADER_IMPLEMENTATION (obj1),
+ − 2833 *imp2 = XRECORD_LHEADER_IMPLEMENTATION (obj2);
+ − 2834
+ − 2835 return (imp1 == imp2) &&
+ − 2836 /* EQ-ness of the objects was noticed above */
+ − 2837 (imp1->equal && (imp1->equal) (obj1, obj2, depth));
+ − 2838 }
+ − 2839
+ − 2840 return 0;
+ − 2841 }
+ − 2842
801
+ − 2843 int
+ − 2844 internal_equalp (Lisp_Object obj1, Lisp_Object obj2, int depth)
+ − 2845 {
+ − 2846 if (depth > 200)
+ − 2847 stack_overflow ("Stack overflow in equalp", Qunbound);
+ − 2848 QUIT;
+ − 2849 if (EQ_WITH_EBOLA_NOTICE (obj1, obj2))
+ − 2850 return 1;
1983
+ − 2851 #ifdef WITH_NUMBER_TYPES
+ − 2852 if (NUMBERP (obj1) && NUMBERP (obj2))
+ − 2853 {
+ − 2854 switch (promote_args (&obj1, &obj2))
+ − 2855 {
+ − 2856 case FIXNUM_T:
+ − 2857 return XREALINT (obj1) == XREALINT (obj2);
+ − 2858 #ifdef HAVE_BIGNUM
+ − 2859 case BIGNUM_T:
+ − 2860 return bignum_eql (XBIGNUM_DATA (obj1), XBIGNUM_DATA (obj2));
+ − 2861 #endif
+ − 2862 #ifdef HAVE_RATIO
+ − 2863 case RATIO_T:
+ − 2864 return ratio_eql (XRATIO_DATA (obj1), XRATIO_DATA (obj2));
+ − 2865 #endif
+ − 2866 case FLOAT_T:
+ − 2867 return XFLOAT_DATA (obj1) == XFLOAT_DATA (obj2);
+ − 2868 #ifdef HAVE_BIGFLOAT
+ − 2869 case BIGFLOAT_T:
+ − 2870 return bigfloat_eql (XBIGFLOAT_DATA (obj1), XBIGFLOAT_DATA (obj2));
+ − 2871 #endif
+ − 2872 }
+ − 2873 }
+ − 2874 #else
801
+ − 2875 if ((INTP (obj1) && FLOATP (obj2)) || (FLOATP (obj1) && INTP (obj2)))
+ − 2876 return extract_float (obj1) == extract_float (obj2);
1983
+ − 2877 #endif
801
+ − 2878 if (CHARP (obj1) && CHARP (obj2))
+ − 2879 return DOWNCASE (0, XCHAR (obj1)) == DOWNCASE (0, XCHAR (obj2));
+ − 2880 if (XTYPE (obj1) != XTYPE (obj2))
+ − 2881 return 0;
+ − 2882 if (LRECORDP (obj1))
+ − 2883 {
+ − 2884 const struct lrecord_implementation
+ − 2885 *imp1 = XRECORD_LHEADER_IMPLEMENTATION (obj1),
+ − 2886 *imp2 = XRECORD_LHEADER_IMPLEMENTATION (obj2);
+ − 2887
+ − 2888 /* #### not yet implemented properly, needs another flag to specify
+ − 2889 equalp-ness */
+ − 2890 return (imp1 == imp2) &&
+ − 2891 /* EQ-ness of the objects was noticed above */
+ − 2892 (imp1->equal && (imp1->equal) (obj1, obj2, depth));
+ − 2893 }
+ − 2894
+ − 2895 return 0;
+ − 2896 }
+ − 2897
428
+ − 2898 /* Note that we may be calling sub-objects that will use
+ − 2899 internal_equal() (instead of internal_old_equal()). Oh well.
+ − 2900 We will get an Ebola note if there's any possibility of confusion,
+ − 2901 but that seems unlikely. */
+ − 2902
+ − 2903 static int
+ − 2904 internal_old_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
+ − 2905 {
+ − 2906 if (depth > 200)
563
+ − 2907 stack_overflow ("Stack overflow in equal", Qunbound);
428
+ − 2908 QUIT;
+ − 2909 if (HACKEQ_UNSAFE (obj1, obj2))
+ − 2910 return 1;
+ − 2911 /* Note that (equal 20 20.0) should be nil */
+ − 2912 if (XTYPE (obj1) != XTYPE (obj2))
+ − 2913 return 0;
+ − 2914
+ − 2915 return internal_equal (obj1, obj2, depth);
+ − 2916 }
+ − 2917
+ − 2918 DEFUN ("equal", Fequal, 2, 2, 0, /*
+ − 2919 Return t if two Lisp objects have similar structure and contents.
+ − 2920 They must have the same data type.
+ − 2921 Conses are compared by comparing the cars and the cdrs.
+ − 2922 Vectors and strings are compared element by element.
+ − 2923 Numbers are compared by value. Symbols must match exactly.
+ − 2924 */
444
+ − 2925 (object1, object2))
428
+ − 2926 {
444
+ − 2927 return internal_equal (object1, object2, 0) ? Qt : Qnil;
428
+ − 2928 }
+ − 2929
+ − 2930 DEFUN ("old-equal", Fold_equal, 2, 2, 0, /*
+ − 2931 Return t if two Lisp objects have similar structure and contents.
+ − 2932 They must have the same data type.
+ − 2933 \(Note, however, that an exception is made for characters and integers;
+ − 2934 this is known as the "char-int confoundance disease." See `eq' and
+ − 2935 `old-eq'.)
+ − 2936 This function is provided only for byte-code compatibility with v19.
+ − 2937 Do not use it.
+ − 2938 */
444
+ − 2939 (object1, object2))
428
+ − 2940 {
444
+ − 2941 return internal_old_equal (object1, object2, 0) ? Qt : Qnil;
428
+ − 2942 }
+ − 2943
+ − 2944
+ − 2945 DEFUN ("fillarray", Ffillarray, 2, 2, 0, /*
434
+ − 2946 Destructively modify ARRAY by replacing each element with ITEM.
428
+ − 2947 ARRAY is a vector, bit vector, or string.
+ − 2948 */
+ − 2949 (array, item))
+ − 2950 {
+ − 2951 retry:
+ − 2952 if (STRINGP (array))
+ − 2953 {
793
+ − 2954 Bytecount old_bytecount = XSTRING_LENGTH (array);
434
+ − 2955 Bytecount new_bytecount;
+ − 2956 Bytecount item_bytecount;
867
+ − 2957 Ibyte item_buf[MAX_ICHAR_LEN];
+ − 2958 Ibyte *p;
+ − 2959 Ibyte *end;
434
+ − 2960
428
+ − 2961 CHECK_CHAR_COERCE_INT (item);
+ − 2962 CHECK_LISP_WRITEABLE (array);
434
+ − 2963
771
+ − 2964 sledgehammer_check_ascii_begin (array);
867
+ − 2965 item_bytecount = set_itext_ichar (item_buf, XCHAR (item));
826
+ − 2966 new_bytecount = item_bytecount * (Bytecount) string_char_length (array);
793
+ − 2967
+ − 2968 resize_string (array, -1, new_bytecount - old_bytecount);
+ − 2969
+ − 2970 for (p = XSTRING_DATA (array), end = p + new_bytecount;
434
+ − 2971 p < end;
+ − 2972 p += item_bytecount)
+ − 2973 memcpy (p, item_buf, item_bytecount);
+ − 2974 *p = '\0';
+ − 2975
793
+ − 2976 XSET_STRING_ASCII_BEGIN (array,
+ − 2977 item_bytecount == 1 ?
+ − 2978 min (new_bytecount, MAX_STRING_ASCII_BEGIN) :
+ − 2979 0);
428
+ − 2980 bump_string_modiff (array);
771
+ − 2981 sledgehammer_check_ascii_begin (array);
428
+ − 2982 }
+ − 2983 else if (VECTORP (array))
+ − 2984 {
+ − 2985 Lisp_Object *p = XVECTOR_DATA (array);
665
+ − 2986 Elemcount len = XVECTOR_LENGTH (array);
428
+ − 2987 CHECK_LISP_WRITEABLE (array);
+ − 2988 while (len--)
+ − 2989 *p++ = item;
+ − 2990 }
+ − 2991 else if (BIT_VECTORP (array))
+ − 2992 {
440
+ − 2993 Lisp_Bit_Vector *v = XBIT_VECTOR (array);
665
+ − 2994 Elemcount len = bit_vector_length (v);
428
+ − 2995 int bit;
+ − 2996 CHECK_BIT (item);
444
+ − 2997 bit = XINT (item);
428
+ − 2998 CHECK_LISP_WRITEABLE (array);
+ − 2999 while (len--)
+ − 3000 set_bit_vector_bit (v, len, bit);
+ − 3001 }
+ − 3002 else
+ − 3003 {
+ − 3004 array = wrong_type_argument (Qarrayp, array);
+ − 3005 goto retry;
+ − 3006 }
+ − 3007 return array;
+ − 3008 }
+ − 3009
+ − 3010 Lisp_Object
+ − 3011 nconc2 (Lisp_Object arg1, Lisp_Object arg2)
+ − 3012 {
+ − 3013 Lisp_Object args[2];
+ − 3014 struct gcpro gcpro1;
+ − 3015 args[0] = arg1;
+ − 3016 args[1] = arg2;
+ − 3017
+ − 3018 GCPRO1 (args[0]);
+ − 3019 gcpro1.nvars = 2;
+ − 3020
+ − 3021 RETURN_UNGCPRO (bytecode_nconc2 (args));
+ − 3022 }
+ − 3023
+ − 3024 Lisp_Object
+ − 3025 bytecode_nconc2 (Lisp_Object *args)
+ − 3026 {
+ − 3027 retry:
+ − 3028
+ − 3029 if (CONSP (args[0]))
+ − 3030 {
+ − 3031 /* (setcdr (last args[0]) args[1]) */
+ − 3032 Lisp_Object tortoise, hare;
665
+ − 3033 Elemcount count;
428
+ − 3034
+ − 3035 for (hare = tortoise = args[0], count = 0;
+ − 3036 CONSP (XCDR (hare));
+ − 3037 hare = XCDR (hare), count++)
+ − 3038 {
+ − 3039 if (count < CIRCULAR_LIST_SUSPICION_LENGTH) continue;
+ − 3040
+ − 3041 if (count & 1)
+ − 3042 tortoise = XCDR (tortoise);
+ − 3043 if (EQ (hare, tortoise))
+ − 3044 signal_circular_list_error (args[0]);
+ − 3045 }
+ − 3046 XCDR (hare) = args[1];
+ − 3047 return args[0];
+ − 3048 }
+ − 3049 else if (NILP (args[0]))
+ − 3050 {
+ − 3051 return args[1];
+ − 3052 }
+ − 3053 else
+ − 3054 {
+ − 3055 args[0] = wrong_type_argument (args[0], Qlistp);
+ − 3056 goto retry;
+ − 3057 }
+ − 3058 }
+ − 3059
+ − 3060 DEFUN ("nconc", Fnconc, 0, MANY, 0, /*
+ − 3061 Concatenate any number of lists by altering them.
+ − 3062 Only the last argument is not altered, and need not be a list.
+ − 3063 Also see: `append'.
+ − 3064 If the first argument is nil, there is no way to modify it by side
+ − 3065 effect; therefore, write `(setq foo (nconc foo list))' to be sure of
+ − 3066 changing the value of `foo'.
+ − 3067 */
+ − 3068 (int nargs, Lisp_Object *args))
+ − 3069 {
+ − 3070 int argnum = 0;
+ − 3071 struct gcpro gcpro1;
+ − 3072
+ − 3073 /* The modus operandi in Emacs is "caller gc-protects args".
+ − 3074 However, nconc (particularly nconc2 ()) is called many times
+ − 3075 in Emacs on freshly created stuff (e.g. you see the idiom
+ − 3076 nconc2 (Fcopy_sequence (foo), bar) a lot). So we help those
+ − 3077 callers out by protecting the args ourselves to save them
+ − 3078 a lot of temporary-variable grief. */
+ − 3079
+ − 3080 GCPRO1 (args[0]);
+ − 3081 gcpro1.nvars = nargs;
+ − 3082
+ − 3083 while (argnum < nargs)
+ − 3084 {
+ − 3085 Lisp_Object val;
+ − 3086 retry:
+ − 3087 val = args[argnum];
+ − 3088 if (CONSP (val))
+ − 3089 {
+ − 3090 /* `val' is the first cons, which will be our return value. */
+ − 3091 /* `last_cons' will be the cons cell to mutate. */
+ − 3092 Lisp_Object last_cons = val;
+ − 3093 Lisp_Object tortoise = val;
+ − 3094
+ − 3095 for (argnum++; argnum < nargs; argnum++)
+ − 3096 {
+ − 3097 Lisp_Object next = args[argnum];
+ − 3098 retry_next:
+ − 3099 if (CONSP (next) || argnum == nargs -1)
+ − 3100 {
+ − 3101 /* (setcdr (last val) next) */
665
+ − 3102 Elemcount count;
428
+ − 3103
+ − 3104 for (count = 0;
+ − 3105 CONSP (XCDR (last_cons));
+ − 3106 last_cons = XCDR (last_cons), count++)
+ − 3107 {
+ − 3108 if (count < CIRCULAR_LIST_SUSPICION_LENGTH) continue;
+ − 3109
+ − 3110 if (count & 1)
+ − 3111 tortoise = XCDR (tortoise);
+ − 3112 if (EQ (last_cons, tortoise))
+ − 3113 signal_circular_list_error (args[argnum-1]);
+ − 3114 }
+ − 3115 XCDR (last_cons) = next;
+ − 3116 }
+ − 3117 else if (NILP (next))
+ − 3118 {
+ − 3119 continue;
+ − 3120 }
+ − 3121 else
+ − 3122 {
+ − 3123 next = wrong_type_argument (Qlistp, next);
+ − 3124 goto retry_next;
+ − 3125 }
+ − 3126 }
+ − 3127 RETURN_UNGCPRO (val);
+ − 3128 }
+ − 3129 else if (NILP (val))
+ − 3130 argnum++;
+ − 3131 else if (argnum == nargs - 1) /* last arg? */
+ − 3132 RETURN_UNGCPRO (val);
+ − 3133 else
+ − 3134 {
+ − 3135 args[argnum] = wrong_type_argument (Qlistp, val);
+ − 3136 goto retry;
+ − 3137 }
+ − 3138 }
+ − 3139 RETURN_UNGCPRO (Qnil); /* No non-nil args provided. */
+ − 3140 }
+ − 3141
+ − 3142
434
+ − 3143 /* This is the guts of several mapping functions.
+ − 3144 Apply FUNCTION to each element of SEQUENCE, one by one,
+ − 3145 storing the results into elements of VALS, a C vector of Lisp_Objects.
+ − 3146 LENI is the length of VALS, which should also be the length of SEQUENCE.
428
+ − 3147
+ − 3148 If VALS is a null pointer, do not accumulate the results. */
+ − 3149
+ − 3150 static void
665
+ − 3151 mapcar1 (Elemcount leni, Lisp_Object *vals,
434
+ − 3152 Lisp_Object function, Lisp_Object sequence)
428
+ − 3153 {
+ − 3154 Lisp_Object result;
+ − 3155 Lisp_Object args[2];
+ − 3156 struct gcpro gcpro1;
+ − 3157
+ − 3158 if (vals)
+ − 3159 {
+ − 3160 GCPRO1 (vals[0]);
+ − 3161 gcpro1.nvars = 0;
+ − 3162 }
+ − 3163
434
+ − 3164 args[0] = function;
+ − 3165
+ − 3166 if (LISTP (sequence))
428
+ − 3167 {
434
+ − 3168 /* A devious `function' could either:
+ − 3169 - insert garbage into the list in front of us, causing XCDR to crash
+ − 3170 - amputate the list behind us using (setcdr), causing the remaining
+ − 3171 elts to lose their GCPRO status.
+ − 3172
+ − 3173 if (vals != 0) we avoid this by copying the elts into the
+ − 3174 `vals' array. By a stroke of luck, `vals' is exactly large
+ − 3175 enough to hold the elts left to be traversed as well as the
+ − 3176 results computed so far.
+ − 3177
+ − 3178 if (vals == 0) we don't have any free space available and
851
+ − 3179 don't want to eat up any more stack with ALLOCA ().
442
+ − 3180 So we use EXTERNAL_LIST_LOOP_3_NO_DECLARE and GCPRO the tail. */
434
+ − 3181
+ − 3182 if (vals)
428
+ − 3183 {
434
+ − 3184 Lisp_Object *val = vals;
665
+ − 3185 Elemcount i;
434
+ − 3186
+ − 3187 LIST_LOOP_2 (elt, sequence)
+ − 3188 *val++ = elt;
+ − 3189
+ − 3190 gcpro1.nvars = leni;
+ − 3191
+ − 3192 for (i = 0; i < leni; i++)
+ − 3193 {
+ − 3194 args[1] = vals[i];
+ − 3195 vals[i] = Ffuncall (2, args);
+ − 3196 }
+ − 3197 }
+ − 3198 else
+ − 3199 {
+ − 3200 Lisp_Object elt, tail;
442
+ − 3201 EMACS_INT len_unused;
434
+ − 3202 struct gcpro ngcpro1;
+ − 3203
+ − 3204 NGCPRO1 (tail);
+ − 3205
+ − 3206 {
442
+ − 3207 EXTERNAL_LIST_LOOP_4_NO_DECLARE (elt, sequence, tail, len_unused)
434
+ − 3208 {
+ − 3209 args[1] = elt;
+ − 3210 Ffuncall (2, args);
+ − 3211 }
+ − 3212 }
+ − 3213
+ − 3214 NUNGCPRO;
428
+ − 3215 }
+ − 3216 }
434
+ − 3217 else if (VECTORP (sequence))
428
+ − 3218 {
434
+ − 3219 Lisp_Object *objs = XVECTOR_DATA (sequence);
665
+ − 3220 Elemcount i;
428
+ − 3221 for (i = 0; i < leni; i++)
+ − 3222 {
+ − 3223 args[1] = *objs++;
+ − 3224 result = Ffuncall (2, args);
+ − 3225 if (vals) vals[gcpro1.nvars++] = result;
+ − 3226 }
+ − 3227 }
434
+ − 3228 else if (STRINGP (sequence))
428
+ − 3229 {
434
+ − 3230 /* The string data of `sequence' might be relocated during GC. */
+ − 3231 Bytecount slen = XSTRING_LENGTH (sequence);
2367
+ − 3232 Ibyte *p = alloca_ibytes (slen);
867
+ − 3233 Ibyte *end = p + slen;
434
+ − 3234
+ − 3235 memcpy (p, XSTRING_DATA (sequence), slen);
+ − 3236
+ − 3237 while (p < end)
428
+ − 3238 {
867
+ − 3239 args[1] = make_char (itext_ichar (p));
+ − 3240 INC_IBYTEPTR (p);
428
+ − 3241 result = Ffuncall (2, args);
+ − 3242 if (vals) vals[gcpro1.nvars++] = result;
+ − 3243 }
+ − 3244 }
434
+ − 3245 else if (BIT_VECTORP (sequence))
428
+ − 3246 {
440
+ − 3247 Lisp_Bit_Vector *v = XBIT_VECTOR (sequence);
665
+ − 3248 Elemcount i;
428
+ − 3249 for (i = 0; i < leni; i++)
+ − 3250 {
+ − 3251 args[1] = make_int (bit_vector_bit (v, i));
+ − 3252 result = Ffuncall (2, args);
+ − 3253 if (vals) vals[gcpro1.nvars++] = result;
+ − 3254 }
+ − 3255 }
+ − 3256 else
2500
+ − 3257 ABORT (); /* unreachable, since Flength (sequence) did not get an error */
428
+ − 3258
+ − 3259 if (vals)
+ − 3260 UNGCPRO;
+ − 3261 }
+ − 3262
+ − 3263 DEFUN ("mapconcat", Fmapconcat, 3, 3, 0, /*
751
+ − 3264 Apply FUNCTION to each element of SEQUENCE, and concat the results to a string.
+ − 3265 Between each pair of results, insert SEPARATOR.
+ − 3266
+ − 3267 Each result, and SEPARATOR, should be strings. Thus, using " " as SEPARATOR
+ − 3268 results in spaces between the values returned by FUNCTION. SEQUENCE itself
+ − 3269 may be a list, a vector, a bit vector, or a string.
428
+ − 3270 */
434
+ − 3271 (function, sequence, separator))
428
+ − 3272 {
444
+ − 3273 EMACS_INT len = XINT (Flength (sequence));
428
+ − 3274 Lisp_Object *args;
444
+ − 3275 EMACS_INT i;
+ − 3276 EMACS_INT nargs = len + len - 1;
428
+ − 3277
442
+ − 3278 if (len == 0) return build_string ("");
428
+ − 3279
+ − 3280 args = alloca_array (Lisp_Object, nargs);
+ − 3281
434
+ − 3282 mapcar1 (len, args, function, sequence);
428
+ − 3283
+ − 3284 for (i = len - 1; i >= 0; i--)
+ − 3285 args[i + i] = args[i];
+ − 3286
+ − 3287 for (i = 1; i < nargs; i += 2)
434
+ − 3288 args[i] = separator;
428
+ − 3289
+ − 3290 return Fconcat (nargs, args);
+ − 3291 }
+ − 3292
+ − 3293 DEFUN ("mapcar", Fmapcar, 2, 2, 0, /*
434
+ − 3294 Apply FUNCTION to each element of SEQUENCE; return a list of the results.
+ − 3295 The result is a list of the same length as SEQUENCE.
428
+ − 3296 SEQUENCE may be a list, a vector, a bit vector, or a string.
+ − 3297 */
434
+ − 3298 (function, sequence))
428
+ − 3299 {
665
+ − 3300 Elemcount len = XINT (Flength (sequence));
428
+ − 3301 Lisp_Object *args = alloca_array (Lisp_Object, len);
+ − 3302
434
+ − 3303 mapcar1 (len, args, function, sequence);
428
+ − 3304
647
+ − 3305 return Flist ((int) len, args);
428
+ − 3306 }
+ − 3307
+ − 3308 DEFUN ("mapvector", Fmapvector, 2, 2, 0, /*
434
+ − 3309 Apply FUNCTION to each element of SEQUENCE; return a vector of the results.
428
+ − 3310 The result is a vector of the same length as SEQUENCE.
434
+ − 3311 SEQUENCE may be a list, a vector, a bit vector, or a string.
428
+ − 3312 */
434
+ − 3313 (function, sequence))
428
+ − 3314 {
665
+ − 3315 Elemcount len = XINT (Flength (sequence));
428
+ − 3316 Lisp_Object result = make_vector (len, Qnil);
+ − 3317 struct gcpro gcpro1;
+ − 3318
+ − 3319 GCPRO1 (result);
434
+ − 3320 mapcar1 (len, XVECTOR_DATA (result), function, sequence);
428
+ − 3321 UNGCPRO;
+ − 3322
+ − 3323 return result;
+ − 3324 }
+ − 3325
+ − 3326 DEFUN ("mapc-internal", Fmapc_internal, 2, 2, 0, /*
+ − 3327 Apply FUNCTION to each element of SEQUENCE.
+ − 3328 SEQUENCE may be a list, a vector, a bit vector, or a string.
+ − 3329 This function is like `mapcar' but does not accumulate the results,
+ − 3330 which is more efficient if you do not use the results.
+ − 3331
+ − 3332 The difference between this and `mapc' is that `mapc' supports all
+ − 3333 the spiffy Common Lisp arguments. You should normally use `mapc'.
+ − 3334 */
434
+ − 3335 (function, sequence))
428
+ − 3336 {
434
+ − 3337 mapcar1 (XINT (Flength (sequence)), 0, function, sequence);
+ − 3338
+ − 3339 return sequence;
428
+ − 3340 }
+ − 3341
+ − 3342
771
+ − 3343 /* Extra random functions */
442
+ − 3344
+ − 3345 DEFUN ("replace-list", Freplace_list, 2, 2, 0, /*
+ − 3346 Destructively replace the list OLD with NEW.
+ − 3347 This is like (copy-sequence NEW) except that it reuses the
+ − 3348 conses in OLD as much as possible. If OLD and NEW are the same
+ − 3349 length, no consing will take place.
+ − 3350 */
+ − 3351 (old, new))
+ − 3352 {
2367
+ − 3353 Lisp_Object oldtail = old, prevoldtail = Qnil;
+ − 3354
+ − 3355 EXTERNAL_LIST_LOOP_2 (elt, new)
442
+ − 3356 {
+ − 3357 if (!NILP (oldtail))
+ − 3358 {
+ − 3359 CHECK_CONS (oldtail);
2367
+ − 3360 XCAR (oldtail) = elt;
442
+ − 3361 }
+ − 3362 else if (!NILP (prevoldtail))
+ − 3363 {
2367
+ − 3364 XCDR (prevoldtail) = Fcons (elt, Qnil);
442
+ − 3365 prevoldtail = XCDR (prevoldtail);
+ − 3366 }
+ − 3367 else
2367
+ − 3368 old = oldtail = Fcons (elt, Qnil);
442
+ − 3369
+ − 3370 if (!NILP (oldtail))
+ − 3371 {
+ − 3372 prevoldtail = oldtail;
+ − 3373 oldtail = XCDR (oldtail);
+ − 3374 }
+ − 3375 }
+ − 3376
+ − 3377 if (!NILP (prevoldtail))
+ − 3378 XCDR (prevoldtail) = Qnil;
+ − 3379 else
+ − 3380 old = Qnil;
+ − 3381
+ − 3382 return old;
+ − 3383 }
+ − 3384
771
+ − 3385 Lisp_Object
2367
+ − 3386 add_suffix_to_symbol (Lisp_Object symbol, const Ascbyte *ascii_string)
771
+ − 3387 {
+ − 3388 return Fintern (concat2 (Fsymbol_name (symbol),
+ − 3389 build_string (ascii_string)),
+ − 3390 Qnil);
+ − 3391 }
+ − 3392
+ − 3393 Lisp_Object
2367
+ − 3394 add_prefix_to_symbol (const Ascbyte *ascii_string, Lisp_Object symbol)
771
+ − 3395 {
+ − 3396 return Fintern (concat2 (build_string (ascii_string),
+ − 3397 Fsymbol_name (symbol)),
+ − 3398 Qnil);
+ − 3399 }
+ − 3400
442
+ − 3401
428
+ − 3402 /* #### this function doesn't belong in this file! */
+ − 3403
442
+ − 3404 #ifdef HAVE_GETLOADAVG
+ − 3405 #ifdef HAVE_SYS_LOADAVG_H
+ − 3406 #include <sys/loadavg.h>
+ − 3407 #endif
+ − 3408 #else
+ − 3409 int getloadavg (double loadavg[], int nelem); /* Defined in getloadavg.c */
+ − 3410 #endif
+ − 3411
428
+ − 3412 DEFUN ("load-average", Fload_average, 0, 1, 0, /*
+ − 3413 Return list of 1 minute, 5 minute and 15 minute load averages.
+ − 3414 Each of the three load averages is multiplied by 100,
+ − 3415 then converted to integer.
+ − 3416
+ − 3417 When USE-FLOATS is non-nil, floats will be used instead of integers.
+ − 3418 These floats are not multiplied by 100.
+ − 3419
+ − 3420 If the 5-minute or 15-minute load averages are not available, return a
+ − 3421 shortened list, containing only those averages which are available.
+ − 3422
+ − 3423 On some systems, this won't work due to permissions on /dev/kmem,
+ − 3424 in which case you can't use this.
+ − 3425 */
+ − 3426 (use_floats))
+ − 3427 {
+ − 3428 double load_ave[3];
+ − 3429 int loads = getloadavg (load_ave, countof (load_ave));
+ − 3430 Lisp_Object ret = Qnil;
+ − 3431
+ − 3432 if (loads == -2)
563
+ − 3433 signal_error (Qunimplemented,
+ − 3434 "load-average not implemented for this operating system",
+ − 3435 Qunbound);
428
+ − 3436 else if (loads < 0)
563
+ − 3437 invalid_operation ("Could not get load-average", lisp_strerror (errno));
428
+ − 3438
+ − 3439 while (loads-- > 0)
+ − 3440 {
+ − 3441 Lisp_Object load = (NILP (use_floats) ?
+ − 3442 make_int ((int) (100.0 * load_ave[loads]))
+ − 3443 : make_float (load_ave[loads]));
+ − 3444 ret = Fcons (load, ret);
+ − 3445 }
+ − 3446 return ret;
+ − 3447 }
+ − 3448
+ − 3449
+ − 3450 Lisp_Object Vfeatures;
+ − 3451
+ − 3452 DEFUN ("featurep", Ffeaturep, 1, 1, 0, /*
+ − 3453 Return non-nil if feature FEXP is present in this Emacs.
+ − 3454 Use this to conditionalize execution of lisp code based on the
+ − 3455 presence or absence of emacs or environment extensions.
+ − 3456 FEXP can be a symbol, a number, or a list.
+ − 3457 If it is a symbol, that symbol is looked up in the `features' variable,
+ − 3458 and non-nil will be returned if found.
+ − 3459 If it is a number, the function will return non-nil if this Emacs
+ − 3460 has an equal or greater version number than FEXP.
+ − 3461 If it is a list whose car is the symbol `and', it will return
+ − 3462 non-nil if all the features in its cdr are non-nil.
+ − 3463 If it is a list whose car is the symbol `or', it will return non-nil
+ − 3464 if any of the features in its cdr are non-nil.
+ − 3465 If it is a list whose car is the symbol `not', it will return
+ − 3466 non-nil if the feature is not present.
+ − 3467
+ − 3468 Examples:
+ − 3469
+ − 3470 (featurep 'xemacs)
+ − 3471 => ; Non-nil on XEmacs.
+ − 3472
+ − 3473 (featurep '(and xemacs gnus))
+ − 3474 => ; Non-nil on XEmacs with Gnus loaded.
+ − 3475
+ − 3476 (featurep '(or tty-frames (and emacs 19.30)))
+ − 3477 => ; Non-nil if this Emacs supports TTY frames.
+ − 3478
+ − 3479 (featurep '(or (and xemacs 19.15) (and emacs 19.34)))
+ − 3480 => ; Non-nil on XEmacs 19.15 and later, or FSF Emacs 19.34 and later.
+ − 3481
442
+ − 3482 (featurep '(and xemacs 21.02))
+ − 3483 => ; Non-nil on XEmacs 21.2 and later.
+ − 3484
428
+ − 3485 NOTE: The advanced arguments of this function (anything other than a
+ − 3486 symbol) are not yet supported by FSF Emacs. If you feel they are useful
+ − 3487 for supporting multiple Emacs variants, lobby Richard Stallman at
442
+ − 3488 <bug-gnu-emacs@gnu.org>.
428
+ − 3489 */
+ − 3490 (fexp))
+ − 3491 {
+ − 3492 #ifndef FEATUREP_SYNTAX
+ − 3493 CHECK_SYMBOL (fexp);
+ − 3494 return NILP (Fmemq (fexp, Vfeatures)) ? Qnil : Qt;
+ − 3495 #else /* FEATUREP_SYNTAX */
+ − 3496 static double featurep_emacs_version;
+ − 3497
+ − 3498 /* Brute force translation from Erik Naggum's lisp function. */
+ − 3499 if (SYMBOLP (fexp))
+ − 3500 {
+ − 3501 /* Original definition */
+ − 3502 return NILP (Fmemq (fexp, Vfeatures)) ? Qnil : Qt;
+ − 3503 }
+ − 3504 else if (INTP (fexp) || FLOATP (fexp))
+ − 3505 {
+ − 3506 double d = extract_float (fexp);
+ − 3507
+ − 3508 if (featurep_emacs_version == 0.0)
+ − 3509 {
+ − 3510 featurep_emacs_version = XINT (Vemacs_major_version) +
+ − 3511 (XINT (Vemacs_minor_version) / 100.0);
+ − 3512 }
+ − 3513 return featurep_emacs_version >= d ? Qt : Qnil;
+ − 3514 }
+ − 3515 else if (CONSP (fexp))
+ − 3516 {
+ − 3517 Lisp_Object tem = XCAR (fexp);
+ − 3518 if (EQ (tem, Qnot))
+ − 3519 {
+ − 3520 Lisp_Object negate;
+ − 3521
+ − 3522 tem = XCDR (fexp);
+ − 3523 negate = Fcar (tem);
+ − 3524 if (!NILP (tem))
+ − 3525 return NILP (call1 (Qfeaturep, negate)) ? Qt : Qnil;
+ − 3526 else
+ − 3527 return Fsignal (Qinvalid_read_syntax, list1 (tem));
+ − 3528 }
+ − 3529 else if (EQ (tem, Qand))
+ − 3530 {
+ − 3531 tem = XCDR (fexp);
+ − 3532 /* Use Fcar/Fcdr for error-checking. */
+ − 3533 while (!NILP (tem) && !NILP (call1 (Qfeaturep, Fcar (tem))))
+ − 3534 {
+ − 3535 tem = Fcdr (tem);
+ − 3536 }
+ − 3537 return NILP (tem) ? Qt : Qnil;
+ − 3538 }
+ − 3539 else if (EQ (tem, Qor))
+ − 3540 {
+ − 3541 tem = XCDR (fexp);
+ − 3542 /* Use Fcar/Fcdr for error-checking. */
+ − 3543 while (!NILP (tem) && NILP (call1 (Qfeaturep, Fcar (tem))))
+ − 3544 {
+ − 3545 tem = Fcdr (tem);
+ − 3546 }
+ − 3547 return NILP (tem) ? Qnil : Qt;
+ − 3548 }
+ − 3549 else
+ − 3550 {
+ − 3551 return Fsignal (Qinvalid_read_syntax, list1 (XCDR (fexp)));
+ − 3552 }
+ − 3553 }
+ − 3554 else
+ − 3555 {
+ − 3556 return Fsignal (Qinvalid_read_syntax, list1 (fexp));
+ − 3557 }
+ − 3558 }
+ − 3559 #endif /* FEATUREP_SYNTAX */
+ − 3560
+ − 3561 DEFUN ("provide", Fprovide, 1, 1, 0, /*
+ − 3562 Announce that FEATURE is a feature of the current Emacs.
+ − 3563 This function updates the value of the variable `features'.
+ − 3564 */
+ − 3565 (feature))
+ − 3566 {
+ − 3567 Lisp_Object tem;
+ − 3568 CHECK_SYMBOL (feature);
+ − 3569 if (!NILP (Vautoload_queue))
+ − 3570 Vautoload_queue = Fcons (Fcons (Vfeatures, Qnil), Vautoload_queue);
+ − 3571 tem = Fmemq (feature, Vfeatures);
+ − 3572 if (NILP (tem))
+ − 3573 Vfeatures = Fcons (feature, Vfeatures);
+ − 3574 LOADHIST_ATTACH (Fcons (Qprovide, feature));
+ − 3575 return feature;
+ − 3576 }
+ − 3577
1067
+ − 3578 DEFUN ("require", Frequire, 1, 3, 0, /*
428
+ − 3579 If feature FEATURE is not loaded, load it from FILENAME.
+ − 3580 If FEATURE is not a member of the list `features', then the feature
+ − 3581 is not loaded; so load the file FILENAME.
+ − 3582 If FILENAME is omitted, the printname of FEATURE is used as the file name.
1067
+ − 3583 If optional third argument NOERROR is non-nil, then return nil if the file
+ − 3584 is not found instead of signaling an error.
428
+ − 3585 */
1067
+ − 3586 (feature, filename, noerror))
428
+ − 3587 {
+ − 3588 Lisp_Object tem;
+ − 3589 CHECK_SYMBOL (feature);
+ − 3590 tem = Fmemq (feature, Vfeatures);
+ − 3591 LOADHIST_ATTACH (Fcons (Qrequire, feature));
+ − 3592 if (!NILP (tem))
+ − 3593 return feature;
+ − 3594 else
+ − 3595 {
+ − 3596 int speccount = specpdl_depth ();
+ − 3597
+ − 3598 /* Value saved here is to be restored into Vautoload_queue */
+ − 3599 record_unwind_protect (un_autoload, Vautoload_queue);
+ − 3600 Vautoload_queue = Qt;
+ − 3601
1067
+ − 3602 tem = call4 (Qload, NILP (filename) ? Fsymbol_name (feature) : filename,
1261
+ − 3603 noerror, Qrequire, Qnil);
1067
+ − 3604 /* If load failed entirely, return nil. */
+ − 3605 if (NILP (tem))
+ − 3606 return unbind_to_1 (speccount, Qnil);
428
+ − 3607
+ − 3608 tem = Fmemq (feature, Vfeatures);
+ − 3609 if (NILP (tem))
563
+ − 3610 invalid_state ("Required feature was not provided", feature);
428
+ − 3611
+ − 3612 /* Once loading finishes, don't undo it. */
+ − 3613 Vautoload_queue = Qt;
771
+ − 3614 return unbind_to_1 (speccount, feature);
428
+ − 3615 }
+ − 3616 }
+ − 3617
+ − 3618 /* base64 encode/decode functions.
+ − 3619
+ − 3620 Originally based on code from GNU recode. Ported to FSF Emacs by
+ − 3621 Lars Magne Ingebrigtsen and Karl Heuer. Ported to XEmacs and
+ − 3622 subsequently heavily hacked by Hrvoje Niksic. */
+ − 3623
+ − 3624 #define MIME_LINE_LENGTH 72
+ − 3625
+ − 3626 #define IS_ASCII(Character) \
+ − 3627 ((Character) < 128)
+ − 3628 #define IS_BASE64(Character) \
+ − 3629 (IS_ASCII (Character) && base64_char_to_value[Character] >= 0)
+ − 3630
+ − 3631 /* Table of characters coding the 64 values. */
+ − 3632 static char base64_value_to_char[64] =
+ − 3633 {
+ − 3634 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 0- 9 */
+ − 3635 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 10-19 */
+ − 3636 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', /* 20-29 */
+ − 3637 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', /* 30-39 */
+ − 3638 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', /* 40-49 */
+ − 3639 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', /* 50-59 */
+ − 3640 '8', '9', '+', '/' /* 60-63 */
+ − 3641 };
+ − 3642
+ − 3643 /* Table of base64 values for first 128 characters. */
+ − 3644 static short base64_char_to_value[128] =
+ − 3645 {
+ − 3646 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0- 9 */
+ − 3647 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10- 19 */
+ − 3648 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20- 29 */
+ − 3649 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 30- 39 */
+ − 3650 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, /* 40- 49 */
+ − 3651 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, /* 50- 59 */
+ − 3652 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, /* 60- 69 */
+ − 3653 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 70- 79 */
+ − 3654 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, /* 80- 89 */
+ − 3655 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, /* 90- 99 */
+ − 3656 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, /* 100-109 */
+ − 3657 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, /* 110-119 */
+ − 3658 49, 50, 51, -1, -1, -1, -1, -1 /* 120-127 */
+ − 3659 };
+ − 3660
+ − 3661 /* The following diagram shows the logical steps by which three octets
+ − 3662 get transformed into four base64 characters.
+ − 3663
+ − 3664 .--------. .--------. .--------.
+ − 3665 |aaaaaabb| |bbbbcccc| |ccdddddd|
+ − 3666 `--------' `--------' `--------'
+ − 3667 6 2 4 4 2 6
+ − 3668 .--------+--------+--------+--------.
+ − 3669 |00aaaaaa|00bbbbbb|00cccccc|00dddddd|
+ − 3670 `--------+--------+--------+--------'
+ − 3671
+ − 3672 .--------+--------+--------+--------.
+ − 3673 |AAAAAAAA|BBBBBBBB|CCCCCCCC|DDDDDDDD|
+ − 3674 `--------+--------+--------+--------'
+ − 3675
+ − 3676 The octets are divided into 6 bit chunks, which are then encoded into
+ − 3677 base64 characters. */
+ − 3678
2268
+ − 3679 static DECLARE_DOESNT_RETURN (base64_conversion_error (const char *,
+ − 3680 Lisp_Object));
+ − 3681
575
+ − 3682 static DOESNT_RETURN
563
+ − 3683 base64_conversion_error (const char *reason, Lisp_Object frob)
+ − 3684 {
+ − 3685 signal_error (Qbase64_conversion_error, reason, frob);
+ − 3686 }
+ − 3687
+ − 3688 #define ADVANCE_INPUT(c, stream) \
867
+ − 3689 ((ec = Lstream_get_ichar (stream)) == -1 ? 0 : \
563
+ − 3690 ((ec > 255) ? \
+ − 3691 (base64_conversion_error ("Non-ascii character in base64 input", \
+ − 3692 make_char (ec)), 0) \
867
+ − 3693 : (c = (Ibyte)ec), 1))
665
+ − 3694
+ − 3695 static Bytebpos
867
+ − 3696 base64_encode_1 (Lstream *istream, Ibyte *to, int line_break)
428
+ − 3697 {
+ − 3698 EMACS_INT counter = 0;
867
+ − 3699 Ibyte *e = to;
+ − 3700 Ichar ec;
428
+ − 3701 unsigned int value;
+ − 3702
+ − 3703 while (1)
+ − 3704 {
1204
+ − 3705 Ibyte c = 0;
428
+ − 3706 if (!ADVANCE_INPUT (c, istream))
+ − 3707 break;
+ − 3708
+ − 3709 /* Wrap line every 76 characters. */
+ − 3710 if (line_break)
+ − 3711 {
+ − 3712 if (counter < MIME_LINE_LENGTH / 4)
+ − 3713 counter++;
+ − 3714 else
+ − 3715 {
+ − 3716 *e++ = '\n';
+ − 3717 counter = 1;
+ − 3718 }
+ − 3719 }
+ − 3720
+ − 3721 /* Process first byte of a triplet. */
+ − 3722 *e++ = base64_value_to_char[0x3f & c >> 2];
+ − 3723 value = (0x03 & c) << 4;
+ − 3724
+ − 3725 /* Process second byte of a triplet. */
+ − 3726 if (!ADVANCE_INPUT (c, istream))
+ − 3727 {
+ − 3728 *e++ = base64_value_to_char[value];
+ − 3729 *e++ = '=';
+ − 3730 *e++ = '=';
+ − 3731 break;
+ − 3732 }
+ − 3733
+ − 3734 *e++ = base64_value_to_char[value | (0x0f & c >> 4)];
+ − 3735 value = (0x0f & c) << 2;
+ − 3736
+ − 3737 /* Process third byte of a triplet. */
+ − 3738 if (!ADVANCE_INPUT (c, istream))
+ − 3739 {
+ − 3740 *e++ = base64_value_to_char[value];
+ − 3741 *e++ = '=';
+ − 3742 break;
+ − 3743 }
+ − 3744
+ − 3745 *e++ = base64_value_to_char[value | (0x03 & c >> 6)];
+ − 3746 *e++ = base64_value_to_char[0x3f & c];
+ − 3747 }
+ − 3748
+ − 3749 return e - to;
+ − 3750 }
+ − 3751 #undef ADVANCE_INPUT
+ − 3752
+ − 3753 /* Get next character from the stream, except that non-base64
+ − 3754 characters are ignored. This is in accordance with rfc2045. EC
867
+ − 3755 should be an Ichar, so that it can hold -1 as the value for EOF. */
428
+ − 3756 #define ADVANCE_INPUT_IGNORE_NONBASE64(ec, stream, streampos) do { \
867
+ − 3757 ec = Lstream_get_ichar (stream); \
428
+ − 3758 ++streampos; \
+ − 3759 /* IS_BASE64 may not be called with negative arguments so check for \
+ − 3760 EOF first. */ \
+ − 3761 if (ec < 0 || IS_BASE64 (ec) || ec == '=') \
+ − 3762 break; \
+ − 3763 } while (1)
+ − 3764
+ − 3765 #define STORE_BYTE(pos, val, ccnt) do { \
867
+ − 3766 pos += set_itext_ichar (pos, (Ichar)((unsigned char)(val))); \
428
+ − 3767 ++ccnt; \
+ − 3768 } while (0)
+ − 3769
665
+ − 3770 static Bytebpos
867
+ − 3771 base64_decode_1 (Lstream *istream, Ibyte *to, Charcount *ccptr)
428
+ − 3772 {
+ − 3773 Charcount ccnt = 0;
867
+ − 3774 Ibyte *e = to;
428
+ − 3775 EMACS_INT streampos = 0;
+ − 3776
+ − 3777 while (1)
+ − 3778 {
867
+ − 3779 Ichar ec;
428
+ − 3780 unsigned long value;
+ − 3781
+ − 3782 /* Process first byte of a quadruplet. */
+ − 3783 ADVANCE_INPUT_IGNORE_NONBASE64 (ec, istream, streampos);
+ − 3784 if (ec < 0)
+ − 3785 break;
+ − 3786 if (ec == '=')
563
+ − 3787 base64_conversion_error ("Illegal `=' character while decoding base64",
+ − 3788 make_int (streampos));
428
+ − 3789 value = base64_char_to_value[ec] << 18;
+ − 3790
+ − 3791 /* Process second byte of a quadruplet. */
+ − 3792 ADVANCE_INPUT_IGNORE_NONBASE64 (ec, istream, streampos);
+ − 3793 if (ec < 0)
563
+ − 3794 base64_conversion_error ("Premature EOF while decoding base64",
+ − 3795 Qunbound);
428
+ − 3796 if (ec == '=')
563
+ − 3797 base64_conversion_error ("Illegal `=' character while decoding base64",
+ − 3798 make_int (streampos));
428
+ − 3799 value |= base64_char_to_value[ec] << 12;
+ − 3800 STORE_BYTE (e, value >> 16, ccnt);
+ − 3801
+ − 3802 /* Process third byte of a quadruplet. */
+ − 3803 ADVANCE_INPUT_IGNORE_NONBASE64 (ec, istream, streampos);
+ − 3804 if (ec < 0)
563
+ − 3805 base64_conversion_error ("Premature EOF while decoding base64",
+ − 3806 Qunbound);
428
+ − 3807
+ − 3808 if (ec == '=')
+ − 3809 {
+ − 3810 ADVANCE_INPUT_IGNORE_NONBASE64 (ec, istream, streampos);
+ − 3811 if (ec < 0)
563
+ − 3812 base64_conversion_error ("Premature EOF while decoding base64",
+ − 3813 Qunbound);
428
+ − 3814 if (ec != '=')
563
+ − 3815 base64_conversion_error
+ − 3816 ("Padding `=' expected but not found while decoding base64",
+ − 3817 make_int (streampos));
428
+ − 3818 continue;
+ − 3819 }
+ − 3820
+ − 3821 value |= base64_char_to_value[ec] << 6;
+ − 3822 STORE_BYTE (e, 0xff & value >> 8, ccnt);
+ − 3823
+ − 3824 /* Process fourth byte of a quadruplet. */
+ − 3825 ADVANCE_INPUT_IGNORE_NONBASE64 (ec, istream, streampos);
+ − 3826 if (ec < 0)
563
+ − 3827 base64_conversion_error ("Premature EOF while decoding base64",
+ − 3828 Qunbound);
428
+ − 3829 if (ec == '=')
+ − 3830 continue;
+ − 3831
+ − 3832 value |= base64_char_to_value[ec];
+ − 3833 STORE_BYTE (e, 0xff & value, ccnt);
+ − 3834 }
+ − 3835
+ − 3836 *ccptr = ccnt;
+ − 3837 return e - to;
+ − 3838 }
+ − 3839 #undef ADVANCE_INPUT
+ − 3840 #undef ADVANCE_INPUT_IGNORE_NONBASE64
+ − 3841 #undef STORE_BYTE
+ − 3842
+ − 3843 DEFUN ("base64-encode-region", Fbase64_encode_region, 2, 3, "r", /*
444
+ − 3844 Base64-encode the region between START and END.
428
+ − 3845 Return the length of the encoded text.
+ − 3846 Optional third argument NO-LINE-BREAK means do not break long lines
+ − 3847 into shorter lines.
+ − 3848 */
444
+ − 3849 (start, end, no_line_break))
428
+ − 3850 {
867
+ − 3851 Ibyte *encoded;
665
+ − 3852 Bytebpos encoded_length;
428
+ − 3853 Charcount allength, length;
+ − 3854 struct buffer *buf = current_buffer;
665
+ − 3855 Charbpos begv, zv, old_pt = BUF_PT (buf);
428
+ − 3856 Lisp_Object input;
851
+ − 3857 int speccount = specpdl_depth ();
428
+ − 3858
444
+ − 3859 get_buffer_range_char (buf, start, end, &begv, &zv, 0);
428
+ − 3860 barf_if_buffer_read_only (buf, begv, zv);
+ − 3861
+ − 3862 /* We need to allocate enough room for encoding the text.
+ − 3863 We need 33 1/3% more space, plus a newline every 76
+ − 3864 characters, and then we round up. */
+ − 3865 length = zv - begv;
+ − 3866 allength = length + length/3 + 1;
+ − 3867 allength += allength / MIME_LINE_LENGTH + 1 + 6;
+ − 3868
+ − 3869 input = make_lisp_buffer_input_stream (buf, begv, zv, 0);
867
+ − 3870 /* We needn't multiply allength with MAX_ICHAR_LEN because all the
428
+ − 3871 base64 characters will be single-byte. */
867
+ − 3872 encoded = (Ibyte *) MALLOC_OR_ALLOCA (allength);
428
+ − 3873 encoded_length = base64_encode_1 (XLSTREAM (input), encoded,
+ − 3874 NILP (no_line_break));
+ − 3875 if (encoded_length > allength)
2500
+ − 3876 ABORT ();
428
+ − 3877 Lstream_delete (XLSTREAM (input));
+ − 3878
+ − 3879 /* Now we have encoded the region, so we insert the new contents
+ − 3880 and delete the old. (Insert first in order to preserve markers.) */
+ − 3881 buffer_insert_raw_string_1 (buf, begv, encoded, encoded_length, 0);
851
+ − 3882 unbind_to (speccount);
428
+ − 3883 buffer_delete_range (buf, begv + encoded_length, zv + encoded_length, 0);
+ − 3884
+ − 3885 /* Simulate FSF Emacs implementation of this function: if point was
+ − 3886 in the region, place it at the beginning. */
+ − 3887 if (old_pt >= begv && old_pt < zv)
+ − 3888 BUF_SET_PT (buf, begv);
+ − 3889
+ − 3890 /* We return the length of the encoded text. */
+ − 3891 return make_int (encoded_length);
+ − 3892 }
+ − 3893
+ − 3894 DEFUN ("base64-encode-string", Fbase64_encode_string, 1, 2, 0, /*
+ − 3895 Base64 encode STRING and return the result.
444
+ − 3896 Optional argument NO-LINE-BREAK means do not break long lines
+ − 3897 into shorter lines.
428
+ − 3898 */
+ − 3899 (string, no_line_break))
+ − 3900 {
+ − 3901 Charcount allength, length;
665
+ − 3902 Bytebpos encoded_length;
867
+ − 3903 Ibyte *encoded;
428
+ − 3904 Lisp_Object input, result;
+ − 3905 int speccount = specpdl_depth();
+ − 3906
+ − 3907 CHECK_STRING (string);
+ − 3908
826
+ − 3909 length = string_char_length (string);
428
+ − 3910 allength = length + length/3 + 1;
+ − 3911 allength += allength / MIME_LINE_LENGTH + 1 + 6;
+ − 3912
+ − 3913 input = make_lisp_string_input_stream (string, 0, -1);
867
+ − 3914 encoded = (Ibyte *) MALLOC_OR_ALLOCA (allength);
428
+ − 3915 encoded_length = base64_encode_1 (XLSTREAM (input), encoded,
+ − 3916 NILP (no_line_break));
+ − 3917 if (encoded_length > allength)
2500
+ − 3918 ABORT ();
428
+ − 3919 Lstream_delete (XLSTREAM (input));
+ − 3920 result = make_string (encoded, encoded_length);
851
+ − 3921 unbind_to (speccount);
428
+ − 3922 return result;
+ − 3923 }
+ − 3924
+ − 3925 DEFUN ("base64-decode-region", Fbase64_decode_region, 2, 2, "r", /*
444
+ − 3926 Base64-decode the region between START and END.
428
+ − 3927 Return the length of the decoded text.
+ − 3928 If the region can't be decoded, return nil and don't modify the buffer.
+ − 3929 Characters out of the base64 alphabet are ignored.
+ − 3930 */
444
+ − 3931 (start, end))
428
+ − 3932 {
+ − 3933 struct buffer *buf = current_buffer;
665
+ − 3934 Charbpos begv, zv, old_pt = BUF_PT (buf);
867
+ − 3935 Ibyte *decoded;
665
+ − 3936 Bytebpos decoded_length;
428
+ − 3937 Charcount length, cc_decoded_length;
+ − 3938 Lisp_Object input;
+ − 3939 int speccount = specpdl_depth();
+ − 3940
444
+ − 3941 get_buffer_range_char (buf, start, end, &begv, &zv, 0);
428
+ − 3942 barf_if_buffer_read_only (buf, begv, zv);
+ − 3943
+ − 3944 length = zv - begv;
+ − 3945
+ − 3946 input = make_lisp_buffer_input_stream (buf, begv, zv, 0);
+ − 3947 /* We need to allocate enough room for decoding the text. */
867
+ − 3948 decoded = (Ibyte *) MALLOC_OR_ALLOCA (length * MAX_ICHAR_LEN);
428
+ − 3949 decoded_length = base64_decode_1 (XLSTREAM (input), decoded, &cc_decoded_length);
867
+ − 3950 if (decoded_length > length * MAX_ICHAR_LEN)
2500
+ − 3951 ABORT ();
428
+ − 3952 Lstream_delete (XLSTREAM (input));
+ − 3953
+ − 3954 /* Now we have decoded the region, so we insert the new contents
+ − 3955 and delete the old. (Insert first in order to preserve markers.) */
+ − 3956 BUF_SET_PT (buf, begv);
+ − 3957 buffer_insert_raw_string_1 (buf, begv, decoded, decoded_length, 0);
851
+ − 3958 unbind_to (speccount);
428
+ − 3959 buffer_delete_range (buf, begv + cc_decoded_length,
+ − 3960 zv + cc_decoded_length, 0);
+ − 3961
+ − 3962 /* Simulate FSF Emacs implementation of this function: if point was
+ − 3963 in the region, place it at the beginning. */
+ − 3964 if (old_pt >= begv && old_pt < zv)
+ − 3965 BUF_SET_PT (buf, begv);
+ − 3966
+ − 3967 return make_int (cc_decoded_length);
+ − 3968 }
+ − 3969
+ − 3970 DEFUN ("base64-decode-string", Fbase64_decode_string, 1, 1, 0, /*
+ − 3971 Base64-decode STRING and return the result.
+ − 3972 Characters out of the base64 alphabet are ignored.
+ − 3973 */
+ − 3974 (string))
+ − 3975 {
867
+ − 3976 Ibyte *decoded;
665
+ − 3977 Bytebpos decoded_length;
428
+ − 3978 Charcount length, cc_decoded_length;
+ − 3979 Lisp_Object input, result;
+ − 3980 int speccount = specpdl_depth();
+ − 3981
+ − 3982 CHECK_STRING (string);
+ − 3983
826
+ − 3984 length = string_char_length (string);
428
+ − 3985 /* We need to allocate enough room for decoding the text. */
867
+ − 3986 decoded = (Ibyte *) MALLOC_OR_ALLOCA (length * MAX_ICHAR_LEN);
428
+ − 3987
+ − 3988 input = make_lisp_string_input_stream (string, 0, -1);
+ − 3989 decoded_length = base64_decode_1 (XLSTREAM (input), decoded,
+ − 3990 &cc_decoded_length);
867
+ − 3991 if (decoded_length > length * MAX_ICHAR_LEN)
2500
+ − 3992 ABORT ();
428
+ − 3993 Lstream_delete (XLSTREAM (input));
+ − 3994
+ − 3995 result = make_string (decoded, decoded_length);
851
+ − 3996 unbind_to (speccount);
428
+ − 3997 return result;
+ − 3998 }
+ − 3999
+ − 4000 Lisp_Object Qyes_or_no_p;
+ − 4001
+ − 4002 void
+ − 4003 syms_of_fns (void)
+ − 4004 {
442
+ − 4005 INIT_LRECORD_IMPLEMENTATION (bit_vector);
+ − 4006
563
+ − 4007 DEFSYMBOL (Qstring_lessp);
+ − 4008 DEFSYMBOL (Qidentity);
+ − 4009 DEFSYMBOL (Qyes_or_no_p);
+ − 4010
+ − 4011 DEFERROR_STANDARD (Qbase64_conversion_error, Qconversion_error);
428
+ − 4012
+ − 4013 DEFSUBR (Fidentity);
+ − 4014 DEFSUBR (Frandom);
+ − 4015 DEFSUBR (Flength);
+ − 4016 DEFSUBR (Fsafe_length);
+ − 4017 DEFSUBR (Fstring_equal);
801
+ − 4018 DEFSUBR (Fcompare_strings);
428
+ − 4019 DEFSUBR (Fstring_lessp);
+ − 4020 DEFSUBR (Fstring_modified_tick);
+ − 4021 DEFSUBR (Fappend);
+ − 4022 DEFSUBR (Fconcat);
+ − 4023 DEFSUBR (Fvconcat);
+ − 4024 DEFSUBR (Fbvconcat);
+ − 4025 DEFSUBR (Fcopy_list);
+ − 4026 DEFSUBR (Fcopy_sequence);
+ − 4027 DEFSUBR (Fcopy_alist);
+ − 4028 DEFSUBR (Fcopy_tree);
+ − 4029 DEFSUBR (Fsubstring);
+ − 4030 DEFSUBR (Fsubseq);
+ − 4031 DEFSUBR (Fnthcdr);
+ − 4032 DEFSUBR (Fnth);
+ − 4033 DEFSUBR (Felt);
+ − 4034 DEFSUBR (Flast);
+ − 4035 DEFSUBR (Fbutlast);
+ − 4036 DEFSUBR (Fnbutlast);
+ − 4037 DEFSUBR (Fmember);
+ − 4038 DEFSUBR (Fold_member);
+ − 4039 DEFSUBR (Fmemq);
+ − 4040 DEFSUBR (Fold_memq);
+ − 4041 DEFSUBR (Fassoc);
+ − 4042 DEFSUBR (Fold_assoc);
+ − 4043 DEFSUBR (Fassq);
+ − 4044 DEFSUBR (Fold_assq);
+ − 4045 DEFSUBR (Frassoc);
+ − 4046 DEFSUBR (Fold_rassoc);
+ − 4047 DEFSUBR (Frassq);
+ − 4048 DEFSUBR (Fold_rassq);
+ − 4049 DEFSUBR (Fdelete);
+ − 4050 DEFSUBR (Fold_delete);
+ − 4051 DEFSUBR (Fdelq);
+ − 4052 DEFSUBR (Fold_delq);
+ − 4053 DEFSUBR (Fremassoc);
+ − 4054 DEFSUBR (Fremassq);
+ − 4055 DEFSUBR (Fremrassoc);
+ − 4056 DEFSUBR (Fremrassq);
+ − 4057 DEFSUBR (Fnreverse);
+ − 4058 DEFSUBR (Freverse);
+ − 4059 DEFSUBR (Fsort);
+ − 4060 DEFSUBR (Fplists_eq);
+ − 4061 DEFSUBR (Fplists_equal);
+ − 4062 DEFSUBR (Flax_plists_eq);
+ − 4063 DEFSUBR (Flax_plists_equal);
+ − 4064 DEFSUBR (Fplist_get);
+ − 4065 DEFSUBR (Fplist_put);
+ − 4066 DEFSUBR (Fplist_remprop);
+ − 4067 DEFSUBR (Fplist_member);
+ − 4068 DEFSUBR (Fcheck_valid_plist);
+ − 4069 DEFSUBR (Fvalid_plist_p);
+ − 4070 DEFSUBR (Fcanonicalize_plist);
+ − 4071 DEFSUBR (Flax_plist_get);
+ − 4072 DEFSUBR (Flax_plist_put);
+ − 4073 DEFSUBR (Flax_plist_remprop);
+ − 4074 DEFSUBR (Flax_plist_member);
+ − 4075 DEFSUBR (Fcanonicalize_lax_plist);
+ − 4076 DEFSUBR (Fdestructive_alist_to_plist);
+ − 4077 DEFSUBR (Fget);
+ − 4078 DEFSUBR (Fput);
+ − 4079 DEFSUBR (Fremprop);
+ − 4080 DEFSUBR (Fobject_plist);
+ − 4081 DEFSUBR (Fequal);
+ − 4082 DEFSUBR (Fold_equal);
+ − 4083 DEFSUBR (Ffillarray);
+ − 4084 DEFSUBR (Fnconc);
+ − 4085 DEFSUBR (Fmapcar);
+ − 4086 DEFSUBR (Fmapvector);
+ − 4087 DEFSUBR (Fmapc_internal);
+ − 4088 DEFSUBR (Fmapconcat);
442
+ − 4089 DEFSUBR (Freplace_list);
428
+ − 4090 DEFSUBR (Fload_average);
+ − 4091 DEFSUBR (Ffeaturep);
+ − 4092 DEFSUBR (Frequire);
+ − 4093 DEFSUBR (Fprovide);
+ − 4094 DEFSUBR (Fbase64_encode_region);
+ − 4095 DEFSUBR (Fbase64_encode_string);
+ − 4096 DEFSUBR (Fbase64_decode_region);
+ − 4097 DEFSUBR (Fbase64_decode_string);
771
+ − 4098
+ − 4099 DEFSUBR (Fsplit_string_by_char);
+ − 4100 DEFSUBR (Fsplit_path); /* #### */
+ − 4101 }
+ − 4102
+ − 4103 void
+ − 4104 vars_of_fns (void)
+ − 4105 {
+ − 4106 DEFVAR_LISP ("path-separator", &Vpath_separator /*
+ − 4107 The directory separator in search paths, as a string.
+ − 4108 */ );
+ − 4109 {
+ − 4110 char c = SEPCHAR;
867
+ − 4111 Vpath_separator = make_string ((Ibyte *) &c, 1);
771
+ − 4112 }
428
+ − 4113 }
+ − 4114
+ − 4115 void
+ − 4116 init_provide_once (void)
+ − 4117 {
+ − 4118 DEFVAR_LISP ("features", &Vfeatures /*
+ − 4119 A list of symbols which are the features of the executing emacs.
+ − 4120 Used by `featurep' and `require', and altered by `provide'.
+ − 4121 */ );
+ − 4122 Vfeatures = Qnil;
+ − 4123
+ − 4124 Fprovide (intern ("base64"));
+ − 4125 }