1 /* Random utility Lisp functions.
2 Copyright (C) 1985, 86, 87, 93, 94, 95 Free Software Foundation, Inc.
3 Copyright (C) 1995, 1996 Ben Wing.
4 Copyright (C) 2002, 2003, 2004 MORIOKA Tomohiko
6 This file is part of XEmacs.
8 XEmacs is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with XEmacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* Synched up with: Mule 2.0, FSF 19.30. */
25 /* This file has been Mule-ized. */
27 /* Note: FSF 19.30 has bool vectors. We have bit vectors. */
29 /* Hacked on for Mule by Ben Wing, December 1994, January 1995. */
33 /* Note on some machines this defines `vector' as a typedef,
34 so make sure we don't use that name in this file. */
53 /* NOTE: This symbol is also used in lread.c */
54 #define FEATUREP_SYNTAX
56 Lisp_Object Qstring_lessp;
57 Lisp_Object Qidentity;
59 static int internal_old_equal (Lisp_Object, Lisp_Object, int);
60 Lisp_Object safe_copy_tree (Lisp_Object arg, Lisp_Object vecp, int depth);
63 mark_bit_vector (Lisp_Object obj)
69 print_bit_vector (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
72 Lisp_Bit_Vector *v = XBIT_VECTOR (obj);
73 size_t len = bit_vector_length (v);
76 if (INTP (Vprint_length))
77 last = min ((EMACS_INT) len, XINT (Vprint_length));
78 write_c_string ("#*", printcharfun);
79 for (i = 0; i < last; i++)
81 if (bit_vector_bit (v, i))
82 write_c_string ("1", printcharfun);
84 write_c_string ("0", printcharfun);
88 write_c_string ("...", printcharfun);
92 bit_vector_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
94 Lisp_Bit_Vector *v1 = XBIT_VECTOR (obj1);
95 Lisp_Bit_Vector *v2 = XBIT_VECTOR (obj2);
97 return ((bit_vector_length (v1) == bit_vector_length (v2)) &&
98 !memcmp (v1->bits, v2->bits,
99 BIT_VECTOR_LONG_STORAGE (bit_vector_length (v1)) *
104 bit_vector_hash (Lisp_Object obj, int depth)
106 Lisp_Bit_Vector *v = XBIT_VECTOR (obj);
107 return HASH2 (bit_vector_length (v),
108 memory_hash (v->bits,
109 BIT_VECTOR_LONG_STORAGE (bit_vector_length (v)) *
114 size_bit_vector (const void *lheader)
116 Lisp_Bit_Vector *v = (Lisp_Bit_Vector *) lheader;
117 return FLEXIBLE_ARRAY_STRUCT_SIZEOF (Lisp_Bit_Vector, unsigned long, bits,
118 BIT_VECTOR_LONG_STORAGE (bit_vector_length (v)));
121 static const struct lrecord_description bit_vector_description[] = {
122 { XD_LISP_OBJECT, offsetof (Lisp_Bit_Vector, next) },
127 DEFINE_BASIC_LRECORD_SEQUENCE_IMPLEMENTATION ("bit-vector", bit_vector,
128 mark_bit_vector, print_bit_vector, 0,
129 bit_vector_equal, bit_vector_hash,
130 bit_vector_description, size_bit_vector,
133 DEFUN ("identity", Fidentity, 1, 1, 0, /*
134 Return the argument unchanged.
141 extern long get_random (void);
142 extern void seed_random (long arg);
144 DEFUN ("random", Frandom, 0, 1, 0, /*
145 Return a pseudo-random number.
146 All integers representable in Lisp are equally likely.
147 On most systems, this is 28 bits' worth.
148 With positive integer argument N, return random number in interval [0,N).
149 With argument t, set the random number seed from the current time and pid.
154 unsigned long denominator;
157 seed_random (getpid () + time (NULL));
158 if (NATNUMP (limit) && !ZEROP (limit))
160 /* Try to take our random number from the higher bits of VAL,
161 not the lower, since (says Gentzel) the low bits of `random'
162 are less random than the higher ones. We do this by using the
163 quotient rather than the remainder. At the high end of the RNG
164 it's possible to get a quotient larger than limit; discarding
165 these values eliminates the bias that would otherwise appear
166 when using a large limit. */
167 denominator = ((unsigned long)1 << VALBITS) / XINT (limit);
169 val = get_random () / denominator;
170 while (val >= XINT (limit));
175 return make_int (val);
178 /* Random data-structure functions */
180 #ifdef LOSING_BYTECODE
182 /* #### Delete this shit */
184 /* Charcount is a misnomer here as we might be dealing with the
185 length of a vector or list, but emphasizes that we're not dealing
186 with Bytecounts in strings */
188 length_with_bytecode_hack (Lisp_Object seq)
190 if (!COMPILED_FUNCTIONP (seq))
191 return XINT (Flength (seq));
194 Lisp_Compiled_Function *f = XCOMPILED_FUNCTION (seq);
196 return (f->flags.interactivep ? COMPILED_INTERACTIVE :
197 f->flags.domainp ? COMPILED_DOMAIN :
203 #endif /* LOSING_BYTECODE */
206 check_losing_bytecode (const char *function, Lisp_Object seq)
208 if (COMPILED_FUNCTIONP (seq))
211 "As of 20.3, `%s' no longer works with compiled-function objects",
215 DEFUN ("length", Flength, 1, 1, 0, /*
216 Return the length of vector, bit vector, list or string SEQUENCE.
221 if (STRINGP (sequence))
222 return make_int (XSTRING_CHAR_LENGTH (sequence));
223 else if (CONSP (sequence))
226 GET_EXTERNAL_LIST_LENGTH (sequence, len);
227 return make_int (len);
229 else if (VECTORP (sequence))
230 return make_int (XVECTOR_LENGTH (sequence));
231 else if (NILP (sequence))
233 else if (BIT_VECTORP (sequence))
234 return make_int (bit_vector_length (XBIT_VECTOR (sequence)));
237 check_losing_bytecode ("length", sequence);
238 sequence = wrong_type_argument (Qsequencep, sequence);
243 DEFUN ("safe-length", Fsafe_length, 1, 1, 0, /*
244 Return the length of a list, but avoid error or infinite loop.
245 This function never gets an error. If LIST is not really a list,
246 it returns 0. If LIST is circular, it returns a finite value
247 which is at least the number of distinct elements.
251 Lisp_Object hare, tortoise;
254 for (hare = tortoise = list, len = 0;
255 CONSP (hare) && (! EQ (hare, tortoise) || len == 0);
256 hare = XCDR (hare), len++)
259 tortoise = XCDR (tortoise);
262 return make_int (len);
265 /*** string functions. ***/
267 DEFUN ("string-equal", Fstring_equal, 2, 2, 0, /*
268 Return t if two strings have identical contents.
269 Case is significant. Text properties are ignored.
270 \(Under XEmacs, `equal' also ignores text properties and extents in
271 strings, but this is not the case under FSF Emacs 19. In FSF Emacs 20
272 `equal' is the same as in XEmacs, in that respect.)
273 Symbols are also allowed; their print names are used instead.
278 Lisp_String *p1, *p2;
280 if (SYMBOLP (string1))
281 p1 = XSYMBOL (string1)->name;
284 CHECK_STRING (string1);
285 p1 = XSTRING (string1);
288 if (SYMBOLP (string2))
289 p2 = XSYMBOL (string2)->name;
292 CHECK_STRING (string2);
293 p2 = XSTRING (string2);
296 return (((len = string_length (p1)) == string_length (p2)) &&
297 !memcmp (string_data (p1), string_data (p2), len)) ? Qt : Qnil;
301 DEFUN ("string-lessp", Fstring_lessp, 2, 2, 0, /*
302 Return t if first arg string is less than second in lexicographic order.
303 If I18N2 support (but not Mule support) was compiled in, ordering is
304 determined by the locale. (Case is significant for the default C locale.)
305 In all other cases, comparison is simply done on a character-by-
306 character basis using the numeric value of a character. (Note that
307 this may not produce particularly meaningful results under Mule if
308 characters from different charsets are being compared.)
310 Symbols are also allowed; their print names are used instead.
312 The reason that the I18N2 locale-specific collation is not used under
313 Mule is that the locale model of internationalization does not handle
314 multiple charsets and thus has no hope of working properly under Mule.
315 What we really should do is create a collation table over all built-in
316 charsets. This is extremely difficult to do from scratch, however.
318 Unicode is a good first step towards solving this problem. In fact,
319 it is quite likely that a collation table exists (or will exist) for
320 Unicode. When Unicode support is added to XEmacs/Mule, this problem
325 Lisp_String *p1, *p2;
329 if (SYMBOLP (string1))
330 p1 = XSYMBOL (string1)->name;
333 CHECK_STRING (string1);
334 p1 = XSTRING (string1);
337 if (SYMBOLP (string2))
338 p2 = XSYMBOL (string2)->name;
341 CHECK_STRING (string2);
342 p2 = XSTRING (string2);
345 end = string_char_length (p1);
346 len2 = string_char_length (p2);
350 #if defined (I18N2) && !defined (MULE)
351 /* There is no hope of this working under Mule. Even if we converted
352 the data into an external format so that strcoll() processed it
353 properly, it would still not work because strcoll() does not
354 handle multiple locales. This is the fundamental flaw in the
357 Bytecount bcend = charcount_to_bytecount (string_data (p1), end);
358 /* Compare strings using collation order of locale. */
359 /* Need to be tricky to handle embedded nulls. */
361 for (i = 0; i < bcend; i += strlen((char *) string_data (p1) + i) + 1)
363 int val = strcoll ((char *) string_data (p1) + i,
364 (char *) string_data (p2) + i);
371 #else /* not I18N2, or MULE */
373 Bufbyte *ptr1 = string_data (p1);
374 Bufbyte *ptr2 = string_data (p2);
376 /* #### It is not really necessary to do this: We could compare
377 byte-by-byte and still get a reasonable comparison, since this
378 would compare characters with a charset in the same way. With
379 a little rearrangement of the leading bytes, we could make most
380 inter-charset comparisons work out the same, too; even if some
381 don't, this is not a big deal because inter-charset comparisons
382 aren't really well-defined anyway. */
383 for (i = 0; i < end; i++)
385 if (charptr_emchar (ptr1) != charptr_emchar (ptr2))
386 return charptr_emchar (ptr1) < charptr_emchar (ptr2) ? Qt : Qnil;
391 #endif /* not I18N2, or MULE */
392 /* Can't do i < len2 because then comparison between "foo" and "foo^@"
393 won't work right in I18N2 case */
394 return end < len2 ? Qt : Qnil;
397 DEFUN ("string-modified-tick", Fstring_modified_tick, 1, 1, 0, /*
398 Return STRING's tick counter, incremented for each change to the string.
399 Each string has a tick counter which is incremented each time the contents
400 of the string are changed (e.g. with `aset'). It wraps around occasionally.
406 CHECK_STRING (string);
407 s = XSTRING (string);
408 if (CONSP (s->plist) && INTP (XCAR (s->plist)))
409 return XCAR (s->plist);
415 bump_string_modiff (Lisp_Object str)
417 Lisp_String *s = XSTRING (str);
418 Lisp_Object *ptr = &s->plist;
421 /* #### remove the `string-translatable' property from the string,
424 /* skip over extent info if it's there */
425 if (CONSP (*ptr) && EXTENT_INFOP (XCAR (*ptr)))
427 if (CONSP (*ptr) && INTP (XCAR (*ptr)))
428 XSETINT (XCAR (*ptr), 1+XINT (XCAR (*ptr)));
430 *ptr = Fcons (make_int (1), *ptr);
434 enum concat_target_type { c_cons, c_string, c_vector, c_bit_vector };
435 static Lisp_Object concat (int nargs, Lisp_Object *args,
436 enum concat_target_type target_type,
440 concat2 (Lisp_Object string1, Lisp_Object string2)
445 return concat (2, args, c_string, 0);
449 concat3 (Lisp_Object string1, Lisp_Object string2, Lisp_Object string3)
455 return concat (3, args, c_string, 0);
459 vconcat2 (Lisp_Object vec1, Lisp_Object vec2)
464 return concat (2, args, c_vector, 0);
468 vconcat3 (Lisp_Object vec1, Lisp_Object vec2, Lisp_Object vec3)
474 return concat (3, args, c_vector, 0);
477 DEFUN ("append", Fappend, 0, MANY, 0, /*
478 Concatenate all the arguments and make the result a list.
479 The result is a list whose elements are the elements of all the arguments.
480 Each argument may be a list, vector, bit vector, or string.
481 The last argument is not copied, just used as the tail of the new list.
484 (int nargs, Lisp_Object *args))
486 return concat (nargs, args, c_cons, 1);
489 DEFUN ("concat", Fconcat, 0, MANY, 0, /*
490 Concatenate all the arguments and make the result a string.
491 The result is a string whose elements are the elements of all the arguments.
492 Each argument may be a string or a list or vector of characters.
494 As of XEmacs 21.0, this function does NOT accept individual integers
495 as arguments. Old code that relies on, for example, (concat "foo" 50)
496 returning "foo50" will fail. To fix such code, either apply
497 `int-to-string' to the integer argument, or use `format'.
499 (int nargs, Lisp_Object *args))
501 return concat (nargs, args, c_string, 0);
504 DEFUN ("vconcat", Fvconcat, 0, MANY, 0, /*
505 Concatenate all the arguments and make the result a vector.
506 The result is a vector whose elements are the elements of all the arguments.
507 Each argument may be a list, vector, bit vector, or string.
509 (int nargs, Lisp_Object *args))
511 return concat (nargs, args, c_vector, 0);
514 DEFUN ("bvconcat", Fbvconcat, 0, MANY, 0, /*
515 Concatenate all the arguments and make the result a bit vector.
516 The result is a bit vector whose elements are the elements of all the
517 arguments. Each argument may be a list, vector, bit vector, or string.
519 (int nargs, Lisp_Object *args))
521 return concat (nargs, args, c_bit_vector, 0);
524 /* Copy a (possibly dotted) list. LIST must be a cons.
525 Can't use concat (1, &alist, c_cons, 0) - doesn't handle dotted lists. */
527 copy_list (Lisp_Object list)
529 Lisp_Object list_copy = Fcons (XCAR (list), XCDR (list));
530 Lisp_Object last = list_copy;
531 Lisp_Object hare, tortoise;
534 for (tortoise = hare = XCDR (list), len = 1;
536 hare = XCDR (hare), len++)
538 XCDR (last) = Fcons (XCAR (hare), XCDR (hare));
541 if (len < CIRCULAR_LIST_SUSPICION_LENGTH)
544 tortoise = XCDR (tortoise);
545 if (EQ (tortoise, hare))
546 signal_circular_list_error (list);
552 DEFUN ("copy-list", Fcopy_list, 1, 1, 0, /*
553 Return a copy of list LIST, which may be a dotted list.
554 The elements of LIST are not copied; they are shared
560 if (NILP (list)) return list;
561 if (CONSP (list)) return copy_list (list);
563 list = wrong_type_argument (Qlistp, list);
567 DEFUN ("copy-sequence", Fcopy_sequence, 1, 1, 0, /*
568 Return a copy of list, vector, bit vector or string SEQUENCE.
569 The elements of a list or vector are not copied; they are shared
570 with the original. SEQUENCE may be a dotted list.
575 if (NILP (sequence)) return sequence;
576 if (CONSP (sequence)) return copy_list (sequence);
577 if (STRINGP (sequence)) return concat (1, &sequence, c_string, 0);
578 if (VECTORP (sequence)) return concat (1, &sequence, c_vector, 0);
579 if (BIT_VECTORP (sequence)) return concat (1, &sequence, c_bit_vector, 0);
581 check_losing_bytecode ("copy-sequence", sequence);
582 sequence = wrong_type_argument (Qsequencep, sequence);
586 struct merge_string_extents_struct
589 Bytecount entry_offset;
590 Bytecount entry_length;
594 concat (int nargs, Lisp_Object *args,
595 enum concat_target_type target_type,
599 Lisp_Object tail = Qnil;
602 Lisp_Object last_tail;
604 struct merge_string_extents_struct *args_mse = 0;
605 Bufbyte *string_result = 0;
606 Bufbyte *string_result_ptr = 0;
609 /* The modus operandi in Emacs is "caller gc-protects args".
610 However, concat is called many times in Emacs on freshly
611 created stuff. So we help those callers out by protecting
612 the args ourselves to save them a lot of temporary-variable
616 gcpro1.nvars = nargs;
619 /* #### if the result is a string and any of the strings have a string
620 for the `string-translatable' property, then concat should also
621 concat the args but use the `string-translatable' strings, and store
622 the result in the returned string's `string-translatable' property. */
624 if (target_type == c_string)
625 args_mse = alloca_array (struct merge_string_extents_struct, nargs);
627 /* In append, the last arg isn't treated like the others */
628 if (last_special && nargs > 0)
631 last_tail = args[nargs];
636 /* Check and coerce the arguments. */
637 for (argnum = 0; argnum < nargs; argnum++)
639 Lisp_Object seq = args[argnum];
642 else if (VECTORP (seq) || STRINGP (seq) || BIT_VECTORP (seq))
644 #ifdef LOSING_BYTECODE
645 else if (COMPILED_FUNCTIONP (seq))
646 /* Urk! We allow this, for "compatibility"... */
649 #if 0 /* removed for XEmacs 21 */
651 /* This is too revolting to think about but maintains
652 compatibility with FSF (and lots and lots of old code). */
653 args[argnum] = Fnumber_to_string (seq);
657 check_losing_bytecode ("concat", seq);
658 args[argnum] = wrong_type_argument (Qsequencep, seq);
664 args_mse[argnum].string = seq;
666 args_mse[argnum].string = Qnil;
671 /* Charcount is a misnomer here as we might be dealing with the
672 length of a vector or list, but emphasizes that we're not dealing
673 with Bytecounts in strings */
674 Charcount total_length;
676 for (argnum = 0, total_length = 0; argnum < nargs; argnum++)
678 #ifdef LOSING_BYTECODE
679 Charcount thislen = length_with_bytecode_hack (args[argnum]);
681 Charcount thislen = XINT (Flength (args[argnum]));
683 total_length += thislen;
689 if (total_length == 0)
690 /* In append, if all but last arg are nil, return last arg */
691 RETURN_UNGCPRO (last_tail);
692 val = Fmake_list (make_int (total_length), Qnil);
695 val = make_vector (total_length, Qnil);
698 val = make_bit_vector (total_length, Qzero);
701 /* We don't make the string yet because we don't know the
702 actual number of bytes. This loop was formerly written
703 to call Fmake_string() here and then call set_string_char()
704 for each char. This seems logical enough but is waaaaaaaay
705 slow -- set_string_char() has to scan the whole string up
706 to the place where the substitution is called for in order
707 to find the place to change, and may have to do some
708 realloc()ing in order to make the char fit properly.
711 string_result = (Bufbyte *) alloca (total_length * MAX_EMCHAR_LEN);
712 string_result_ptr = string_result;
722 tail = val, toindex = -1; /* -1 in toindex is flag we are
729 for (argnum = 0; argnum < nargs; argnum++)
731 Charcount thisleni = 0;
732 Charcount thisindex = 0;
733 Lisp_Object seq = args[argnum];
734 Bufbyte *string_source_ptr = 0;
735 Bufbyte *string_prev_result_ptr = string_result_ptr;
739 #ifdef LOSING_BYTECODE
740 thisleni = length_with_bytecode_hack (seq);
742 thisleni = XINT (Flength (seq));
746 string_source_ptr = XSTRING_DATA (seq);
752 /* We've come to the end of this arg, so exit. */
756 /* Fetch next element of `seq' arg into `elt' */
764 if (thisindex >= thisleni)
769 elt = make_char (charptr_emchar (string_source_ptr));
770 INC_CHARPTR (string_source_ptr);
772 else if (VECTORP (seq))
773 elt = XVECTOR_DATA (seq)[thisindex];
774 else if (BIT_VECTORP (seq))
775 elt = make_int (bit_vector_bit (XBIT_VECTOR (seq),
778 elt = Felt (seq, make_int (thisindex));
782 /* Store into result */
785 /* toindex negative means we are making a list */
790 else if (VECTORP (val))
791 XVECTOR_DATA (val)[toindex++] = elt;
792 else if (BIT_VECTORP (val))
795 set_bit_vector_bit (XBIT_VECTOR (val), toindex++, XINT (elt));
799 CHECK_CHAR_COERCE_INT (elt);
800 string_result_ptr += set_charptr_emchar (string_result_ptr,
806 args_mse[argnum].entry_offset =
807 string_prev_result_ptr - string_result;
808 args_mse[argnum].entry_length =
809 string_result_ptr - string_prev_result_ptr;
813 /* Now we finally make the string. */
814 if (target_type == c_string)
816 val = make_string (string_result, string_result_ptr - string_result);
817 for (argnum = 0; argnum < nargs; argnum++)
819 if (STRINGP (args_mse[argnum].string))
820 copy_string_extents (val, args_mse[argnum].string,
821 args_mse[argnum].entry_offset, 0,
822 args_mse[argnum].entry_length);
827 XCDR (prev) = last_tail;
829 RETURN_UNGCPRO (val);
832 DEFUN ("copy-alist", Fcopy_alist, 1, 1, 0, /*
833 Return a copy of ALIST.
834 This is an alist which represents the same mapping from objects to objects,
835 but does not share the alist structure with ALIST.
836 The objects mapped (cars and cdrs of elements of the alist)
838 Elements of ALIST that are not conses are also shared.
848 alist = concat (1, &alist, c_cons, 0);
849 for (tail = alist; CONSP (tail); tail = XCDR (tail))
851 Lisp_Object car = XCAR (tail);
854 XCAR (tail) = Fcons (XCAR (car), XCDR (car));
859 DEFUN ("copy-tree", Fcopy_tree, 1, 2, 0, /*
860 Return a copy of a list and substructures.
861 The argument is copied, and any lists contained within it are copied
862 recursively. Circularities and shared substructures are not preserved.
863 Second arg VECP causes vectors to be copied, too. Strings and bit vectors
868 return safe_copy_tree (arg, vecp, 0);
872 safe_copy_tree (Lisp_Object arg, Lisp_Object vecp, int depth)
875 signal_simple_error ("Stack overflow in copy-tree", arg);
880 rest = arg = Fcopy_sequence (arg);
883 Lisp_Object elt = XCAR (rest);
885 if (CONSP (elt) || VECTORP (elt))
886 XCAR (rest) = safe_copy_tree (elt, vecp, depth + 1);
887 if (VECTORP (XCDR (rest))) /* hack for (a b . [c d]) */
888 XCDR (rest) = safe_copy_tree (XCDR (rest), vecp, depth +1);
892 else if (VECTORP (arg) && ! NILP (vecp))
894 int i = XVECTOR_LENGTH (arg);
896 arg = Fcopy_sequence (arg);
897 for (j = 0; j < i; j++)
899 Lisp_Object elt = XVECTOR_DATA (arg) [j];
901 if (CONSP (elt) || VECTORP (elt))
902 XVECTOR_DATA (arg) [j] = safe_copy_tree (elt, vecp, depth + 1);
908 DEFUN ("substring", Fsubstring, 2, 3, 0, /*
909 Return the substring of STRING starting at START and ending before END.
910 END may be nil or omitted; then the substring runs to the end of STRING.
911 If START or END is negative, it counts from the end.
912 Relevant parts of the string-extent-data are copied to the new string.
914 (string, start, end))
916 Charcount ccstart, ccend;
917 Bytecount bstart, blen;
920 CHECK_STRING (string);
922 get_string_range_char (string, start, end, &ccstart, &ccend,
923 GB_HISTORICAL_STRING_BEHAVIOR);
924 bstart = charcount_to_bytecount (XSTRING_DATA (string), ccstart);
925 blen = charcount_to_bytecount (XSTRING_DATA (string) + bstart, ccend - ccstart);
926 val = make_string (XSTRING_DATA (string) + bstart, blen);
927 /* Copy any applicable extent information into the new string. */
928 copy_string_extents (val, string, 0, bstart, blen);
932 DEFUN ("subseq", Fsubseq, 2, 3, 0, /*
933 Return the subsequence of SEQUENCE starting at START and ending before END.
934 END may be omitted; then the subsequence runs to the end of SEQUENCE.
935 If START or END is negative, it counts from the end.
936 The returned subsequence is always of the same type as SEQUENCE.
937 If SEQUENCE is a string, relevant parts of the string-extent-data
938 are copied to the new string.
940 (sequence, start, end))
944 if (STRINGP (sequence))
945 return Fsubstring (sequence, start, end);
947 len = XINT (Flength (sequence));
964 if (!(0 <= s && s <= e && e <= len))
965 args_out_of_range_3 (sequence, make_int (s), make_int (e));
967 if (VECTORP (sequence))
969 Lisp_Object result = make_vector (e - s, Qnil);
971 Lisp_Object *in_elts = XVECTOR_DATA (sequence);
972 Lisp_Object *out_elts = XVECTOR_DATA (result);
974 for (i = s; i < e; i++)
975 out_elts[i - s] = in_elts[i];
978 else if (LISTP (sequence))
980 Lisp_Object result = Qnil;
983 sequence = Fnthcdr (make_int (s), sequence);
985 for (i = s; i < e; i++)
987 result = Fcons (Fcar (sequence), result);
988 sequence = Fcdr (sequence);
991 return Fnreverse (result);
993 else if (BIT_VECTORP (sequence))
995 Lisp_Object result = make_bit_vector (e - s, Qzero);
998 for (i = s; i < e; i++)
999 set_bit_vector_bit (XBIT_VECTOR (result), i - s,
1000 bit_vector_bit (XBIT_VECTOR (sequence), i));
1005 abort (); /* unreachable, since Flength (sequence) did not get
1012 DEFUN ("nthcdr", Fnthcdr, 2, 2, 0, /*
1013 Take cdr N times on LIST, and return the result.
1018 REGISTER Lisp_Object tail = list;
1020 for (i = XINT (n); i; i--)
1024 else if (NILP (tail))
1028 tail = wrong_type_argument (Qlistp, tail);
1035 DEFUN ("nth", Fnth, 2, 2, 0, /*
1036 Return the Nth element of LIST.
1037 N counts from zero. If LIST is not that long, nil is returned.
1041 return Fcar (Fnthcdr (n, list));
1044 DEFUN ("elt", Felt, 2, 2, 0, /*
1045 Return element of SEQUENCE at index N.
1050 CHECK_INT_COERCE_CHAR (n); /* yuck! */
1051 if (LISTP (sequence))
1053 Lisp_Object tem = Fnthcdr (n, sequence);
1054 /* #### Utterly, completely, fucking disgusting.
1055 * #### The whole point of "elt" is that it operates on
1056 * #### sequences, and does error- (bounds-) checking.
1062 /* This is The Way It Has Always Been. */
1065 /* This is The Way Mly and Cltl2 say It Should Be. */
1066 args_out_of_range (sequence, n);
1069 else if (STRINGP (sequence) ||
1070 VECTORP (sequence) ||
1071 BIT_VECTORP (sequence))
1072 return Faref (sequence, n);
1073 #ifdef LOSING_BYTECODE
1074 else if (COMPILED_FUNCTIONP (sequence))
1076 EMACS_INT idx = XINT (n);
1080 args_out_of_range (sequence, n);
1082 /* Utter perversity */
1084 Lisp_Compiled_Function *f = XCOMPILED_FUNCTION (sequence);
1087 case COMPILED_ARGLIST:
1088 return compiled_function_arglist (f);
1089 case COMPILED_INSTRUCTIONS:
1090 return compiled_function_instructions (f);
1091 case COMPILED_CONSTANTS:
1092 return compiled_function_constants (f);
1093 case COMPILED_STACK_DEPTH:
1094 return compiled_function_stack_depth (f);
1095 case COMPILED_DOC_STRING:
1096 return compiled_function_documentation (f);
1097 case COMPILED_DOMAIN:
1098 return compiled_function_domain (f);
1099 case COMPILED_INTERACTIVE:
1100 if (f->flags.interactivep)
1101 return compiled_function_interactive (f);
1102 /* if we return nil, can't tell interactive with no args
1103 from noninteractive. */
1110 #endif /* LOSING_BYTECODE */
1113 check_losing_bytecode ("elt", sequence);
1114 sequence = wrong_type_argument (Qsequencep, sequence);
1119 DEFUN ("last", Flast, 1, 2, 0, /*
1120 Return the tail of list LIST, of length N (default 1).
1121 LIST may be a dotted list, but not a circular list.
1122 Optional argument N must be a non-negative integer.
1123 If N is zero, then the atom that terminates the list is returned.
1124 If N is greater than the length of LIST, then LIST itself is returned.
1128 EMACS_INT int_n, count;
1129 Lisp_Object retval, tortoise, hare;
1141 for (retval = tortoise = hare = list, count = 0;
1144 (int_n-- <= 0 ? ((void) (retval = XCDR (retval))) : (void)0),
1147 if (count < CIRCULAR_LIST_SUSPICION_LENGTH) continue;
1150 tortoise = XCDR (tortoise);
1151 if (EQ (hare, tortoise))
1152 signal_circular_list_error (list);
1158 DEFUN ("nbutlast", Fnbutlast, 1, 2, 0, /*
1159 Modify LIST to remove the last N (default 1) elements.
1160 If LIST has N or fewer elements, nil is returned and LIST is unmodified.
1177 Lisp_Object last_cons = list;
1179 EXTERNAL_LIST_LOOP_1 (list)
1182 last_cons = XCDR (last_cons);
1188 XCDR (last_cons) = Qnil;
1193 DEFUN ("butlast", Fbutlast, 1, 2, 0, /*
1194 Return a copy of LIST with the last N (default 1) elements removed.
1195 If LIST has N or fewer elements, nil is returned.
1212 Lisp_Object retval = Qnil;
1213 Lisp_Object tail = list;
1215 EXTERNAL_LIST_LOOP_1 (list)
1219 retval = Fcons (XCAR (tail), retval);
1224 return Fnreverse (retval);
1228 DEFUN ("member", Fmember, 2, 2, 0, /*
1229 Return non-nil if ELT is an element of LIST. Comparison done with `equal'.
1230 The value is actually the tail of LIST whose car is ELT.
1234 EXTERNAL_LIST_LOOP_3 (list_elt, list, tail)
1236 if (internal_equal (elt, list_elt, 0))
1242 DEFUN ("old-member", Fold_member, 2, 2, 0, /*
1243 Return non-nil if ELT is an element of LIST. Comparison done with `old-equal'.
1244 The value is actually the tail of LIST whose car is ELT.
1245 This function is provided only for byte-code compatibility with v19.
1250 EXTERNAL_LIST_LOOP_3 (list_elt, list, tail)
1252 if (internal_old_equal (elt, list_elt, 0))
1258 DEFUN ("memq", Fmemq, 2, 2, 0, /*
1259 Return non-nil if ELT is an element of LIST. Comparison done with `eq'.
1260 The value is actually the tail of LIST whose car is ELT.
1264 EXTERNAL_LIST_LOOP_3 (list_elt, list, tail)
1266 if (EQ_WITH_EBOLA_NOTICE (elt, list_elt))
1272 DEFUN ("old-memq", Fold_memq, 2, 2, 0, /*
1273 Return non-nil if ELT is an element of LIST. Comparison done with `old-eq'.
1274 The value is actually the tail of LIST whose car is ELT.
1275 This function is provided only for byte-code compatibility with v19.
1280 EXTERNAL_LIST_LOOP_3 (list_elt, list, tail)
1282 if (HACKEQ_UNSAFE (elt, list_elt))
1289 memq_no_quit (Lisp_Object elt, Lisp_Object list)
1291 LIST_LOOP_3 (list_elt, list, tail)
1293 if (EQ_WITH_EBOLA_NOTICE (elt, list_elt))
1299 DEFUN ("assoc", Fassoc, 2, 2, 0, /*
1300 Return non-nil if KEY is `equal' to the car of an element of ALIST.
1301 The value is actually the element of ALIST whose car equals KEY.
1305 /* This function can GC. */
1306 EXTERNAL_ALIST_LOOP_4 (elt, elt_car, elt_cdr, alist)
1308 if (internal_equal (key, elt_car, 0))
1314 DEFUN ("old-assoc", Fold_assoc, 2, 2, 0, /*
1315 Return non-nil if KEY is `old-equal' to the car of an element of ALIST.
1316 The value is actually the element of ALIST whose car equals KEY.
1320 /* This function can GC. */
1321 EXTERNAL_ALIST_LOOP_4 (elt, elt_car, elt_cdr, alist)
1323 if (internal_old_equal (key, elt_car, 0))
1330 assoc_no_quit (Lisp_Object key, Lisp_Object alist)
1332 int speccount = specpdl_depth ();
1333 specbind (Qinhibit_quit, Qt);
1334 return unbind_to (speccount, Fassoc (key, alist));
1337 DEFUN ("assq", Fassq, 2, 2, 0, /*
1338 Return non-nil if KEY is `eq' to the car of an element of ALIST.
1339 The value is actually the element of ALIST whose car is KEY.
1340 Elements of ALIST that are not conses are ignored.
1344 EXTERNAL_ALIST_LOOP_4 (elt, elt_car, elt_cdr, alist)
1346 if (EQ_WITH_EBOLA_NOTICE (key, elt_car))
1352 DEFUN ("old-assq", Fold_assq, 2, 2, 0, /*
1353 Return non-nil if KEY is `old-eq' to the car of an element of ALIST.
1354 The value is actually the element of ALIST whose car is KEY.
1355 Elements of ALIST that are not conses are ignored.
1356 This function is provided only for byte-code compatibility with v19.
1361 EXTERNAL_ALIST_LOOP_4 (elt, elt_car, elt_cdr, alist)
1363 if (HACKEQ_UNSAFE (key, elt_car))
1369 /* Like Fassq but never report an error and do not allow quits.
1370 Use only on lists known never to be circular. */
1373 assq_no_quit (Lisp_Object key, Lisp_Object alist)
1375 /* This cannot GC. */
1376 LIST_LOOP_2 (elt, alist)
1378 Lisp_Object elt_car = XCAR (elt);
1379 if (EQ_WITH_EBOLA_NOTICE (key, elt_car))
1385 DEFUN ("rassoc", Frassoc, 2, 2, 0, /*
1386 Return non-nil if VALUE is `equal' to the cdr of an element of ALIST.
1387 The value is actually the element of ALIST whose cdr equals VALUE.
1391 EXTERNAL_ALIST_LOOP_4 (elt, elt_car, elt_cdr, alist)
1393 if (internal_equal (value, elt_cdr, 0))
1399 DEFUN ("old-rassoc", Fold_rassoc, 2, 2, 0, /*
1400 Return non-nil if VALUE is `old-equal' to the cdr of an element of ALIST.
1401 The value is actually the element of ALIST whose cdr equals VALUE.
1405 EXTERNAL_ALIST_LOOP_4 (elt, elt_car, elt_cdr, alist)
1407 if (internal_old_equal (value, elt_cdr, 0))
1413 DEFUN ("rassq", Frassq, 2, 2, 0, /*
1414 Return non-nil if VALUE is `eq' to the cdr of an element of ALIST.
1415 The value is actually the element of ALIST whose cdr is VALUE.
1419 EXTERNAL_ALIST_LOOP_4 (elt, elt_car, elt_cdr, alist)
1421 if (EQ_WITH_EBOLA_NOTICE (value, elt_cdr))
1427 DEFUN ("old-rassq", Fold_rassq, 2, 2, 0, /*
1428 Return non-nil if VALUE is `old-eq' to the cdr of an element of ALIST.
1429 The value is actually the element of ALIST whose cdr is VALUE.
1433 EXTERNAL_ALIST_LOOP_4 (elt, elt_car, elt_cdr, alist)
1435 if (HACKEQ_UNSAFE (value, elt_cdr))
1441 /* Like Frassq, but caller must ensure that ALIST is properly
1442 nil-terminated and ebola-free. */
1444 rassq_no_quit (Lisp_Object value, Lisp_Object alist)
1446 LIST_LOOP_2 (elt, alist)
1448 Lisp_Object elt_cdr = XCDR (elt);
1449 if (EQ_WITH_EBOLA_NOTICE (value, elt_cdr))
1456 DEFUN ("delete", Fdelete, 2, 2, 0, /*
1457 Delete by side effect any occurrences of ELT as a member of LIST.
1458 The modified LIST is returned. Comparison is done with `equal'.
1459 If the first member of LIST is ELT, there is no way to remove it by side
1460 effect; therefore, write `(setq foo (delete element foo))' to be sure
1461 of changing the value of `foo'.
1466 EXTERNAL_LIST_LOOP_DELETE_IF (list_elt, list,
1467 (internal_equal (elt, list_elt, 0)));
1471 DEFUN ("old-delete", Fold_delete, 2, 2, 0, /*
1472 Delete by side effect any occurrences of ELT as a member of LIST.
1473 The modified LIST is returned. Comparison is done with `old-equal'.
1474 If the first member of LIST is ELT, there is no way to remove it by side
1475 effect; therefore, write `(setq foo (old-delete element foo))' to be sure
1476 of changing the value of `foo'.
1480 EXTERNAL_LIST_LOOP_DELETE_IF (list_elt, list,
1481 (internal_old_equal (elt, list_elt, 0)));
1485 DEFUN ("delq", Fdelq, 2, 2, 0, /*
1486 Delete by side effect any occurrences of ELT as a member of LIST.
1487 The modified LIST is returned. Comparison is done with `eq'.
1488 If the first member of LIST is ELT, there is no way to remove it by side
1489 effect; therefore, write `(setq foo (delq element foo))' to be sure of
1490 changing the value of `foo'.
1494 EXTERNAL_LIST_LOOP_DELETE_IF (list_elt, list,
1495 (EQ_WITH_EBOLA_NOTICE (elt, list_elt)));
1499 DEFUN ("old-delq", Fold_delq, 2, 2, 0, /*
1500 Delete by side effect any occurrences of ELT as a member of LIST.
1501 The modified LIST is returned. Comparison is done with `old-eq'.
1502 If the first member of LIST is ELT, there is no way to remove it by side
1503 effect; therefore, write `(setq foo (old-delq element foo))' to be sure of
1504 changing the value of `foo'.
1508 EXTERNAL_LIST_LOOP_DELETE_IF (list_elt, list,
1509 (HACKEQ_UNSAFE (elt, list_elt)));
1513 /* Like Fdelq, but caller must ensure that LIST is properly
1514 nil-terminated and ebola-free. */
1517 delq_no_quit (Lisp_Object elt, Lisp_Object list)
1519 LIST_LOOP_DELETE_IF (list_elt, list,
1520 (EQ_WITH_EBOLA_NOTICE (elt, list_elt)));
1524 /* Be VERY careful with this. This is like delq_no_quit() but
1525 also calls free_cons() on the removed conses. You must be SURE
1526 that no pointers to the freed conses remain around (e.g.
1527 someone else is pointing to part of the list). This function
1528 is useful on internal lists that are used frequently and where
1529 the actual list doesn't escape beyond known code bounds. */
1532 delq_no_quit_and_free_cons (Lisp_Object elt, Lisp_Object list)
1534 REGISTER Lisp_Object tail = list;
1535 REGISTER Lisp_Object prev = Qnil;
1537 while (!NILP (tail))
1539 REGISTER Lisp_Object tem = XCAR (tail);
1542 Lisp_Object cons_to_free = tail;
1546 XCDR (prev) = XCDR (tail);
1548 free_cons (XCONS (cons_to_free));
1559 DEFUN ("remassoc", Fremassoc, 2, 2, 0, /*
1560 Delete by side effect any elements of ALIST whose car is `equal' to KEY.
1561 The modified ALIST is returned. If the first member of ALIST has a car
1562 that is `equal' to KEY, there is no way to remove it by side effect;
1563 therefore, write `(setq foo (remassoc key foo))' to be sure of changing
1568 EXTERNAL_LIST_LOOP_DELETE_IF (elt, alist,
1570 internal_equal (key, XCAR (elt), 0)));
1575 remassoc_no_quit (Lisp_Object key, Lisp_Object alist)
1577 int speccount = specpdl_depth ();
1578 specbind (Qinhibit_quit, Qt);
1579 return unbind_to (speccount, Fremassoc (key, alist));
1582 DEFUN ("remassq", Fremassq, 2, 2, 0, /*
1583 Delete by side effect any elements of ALIST whose car is `eq' to KEY.
1584 The modified ALIST is returned. If the first member of ALIST has a car
1585 that is `eq' to KEY, there is no way to remove it by side effect;
1586 therefore, write `(setq foo (remassq key foo))' to be sure of changing
1591 EXTERNAL_LIST_LOOP_DELETE_IF (elt, alist,
1593 EQ_WITH_EBOLA_NOTICE (key, XCAR (elt))));
1597 /* no quit, no errors; be careful */
1600 remassq_no_quit (Lisp_Object key, Lisp_Object alist)
1602 LIST_LOOP_DELETE_IF (elt, alist,
1604 EQ_WITH_EBOLA_NOTICE (key, XCAR (elt))));
1608 DEFUN ("remrassoc", Fremrassoc, 2, 2, 0, /*
1609 Delete by side effect any elements of ALIST whose cdr is `equal' to VALUE.
1610 The modified ALIST is returned. If the first member of ALIST has a car
1611 that is `equal' to VALUE, there is no way to remove it by side effect;
1612 therefore, write `(setq foo (remrassoc value foo))' to be sure of changing
1617 EXTERNAL_LIST_LOOP_DELETE_IF (elt, alist,
1619 internal_equal (value, XCDR (elt), 0)));
1623 DEFUN ("remrassq", Fremrassq, 2, 2, 0, /*
1624 Delete by side effect any elements of ALIST whose cdr is `eq' to VALUE.
1625 The modified ALIST is returned. If the first member of ALIST has a car
1626 that is `eq' to VALUE, there is no way to remove it by side effect;
1627 therefore, write `(setq foo (remrassq value foo))' to be sure of changing
1632 EXTERNAL_LIST_LOOP_DELETE_IF (elt, alist,
1634 EQ_WITH_EBOLA_NOTICE (value, XCDR (elt))));
1638 /* Like Fremrassq, fast and unsafe; be careful */
1640 remrassq_no_quit (Lisp_Object value, Lisp_Object alist)
1642 LIST_LOOP_DELETE_IF (elt, alist,
1644 EQ_WITH_EBOLA_NOTICE (value, XCDR (elt))));
1648 DEFUN ("nreverse", Fnreverse, 1, 1, 0, /*
1649 Reverse LIST by destructively modifying cdr pointers.
1650 Return the beginning of the reversed list.
1651 Also see: `reverse'.
1655 struct gcpro gcpro1, gcpro2;
1656 REGISTER Lisp_Object prev = Qnil;
1657 REGISTER Lisp_Object tail = list;
1659 /* We gcpro our args; see `nconc' */
1660 GCPRO2 (prev, tail);
1661 while (!NILP (tail))
1663 REGISTER Lisp_Object next;
1664 CONCHECK_CONS (tail);
1674 DEFUN ("reverse", Freverse, 1, 1, 0, /*
1675 Reverse LIST, copying. Return the beginning of the reversed list.
1676 See also the function `nreverse', which is used more often.
1680 Lisp_Object reversed_list = Qnil;
1681 EXTERNAL_LIST_LOOP_2 (elt, list)
1683 reversed_list = Fcons (elt, reversed_list);
1685 return reversed_list;
1688 static Lisp_Object list_merge (Lisp_Object org_l1, Lisp_Object org_l2,
1689 Lisp_Object lisp_arg,
1690 int (*pred_fn) (Lisp_Object, Lisp_Object,
1691 Lisp_Object lisp_arg));
1694 list_sort (Lisp_Object list,
1695 Lisp_Object lisp_arg,
1696 int (*pred_fn) (Lisp_Object, Lisp_Object,
1697 Lisp_Object lisp_arg))
1699 struct gcpro gcpro1, gcpro2, gcpro3;
1700 Lisp_Object back, tem;
1701 Lisp_Object front = list;
1702 Lisp_Object len = Flength (list);
1707 len = make_int (XINT (len) / 2 - 1);
1708 tem = Fnthcdr (len, list);
1710 Fsetcdr (tem, Qnil);
1712 GCPRO3 (front, back, lisp_arg);
1713 front = list_sort (front, lisp_arg, pred_fn);
1714 back = list_sort (back, lisp_arg, pred_fn);
1716 return list_merge (front, back, lisp_arg, pred_fn);
1721 merge_pred_function (Lisp_Object obj1, Lisp_Object obj2,
1726 /* prevents the GC from happening in call2 */
1727 int speccount = specpdl_depth ();
1728 /* Emacs' GC doesn't actually relocate pointers, so this probably
1729 isn't strictly necessary */
1730 record_unwind_protect (restore_gc_inhibit,
1731 make_int (gc_currently_forbidden));
1732 gc_currently_forbidden = 1;
1733 tmp = call2 (pred, obj1, obj2);
1734 unbind_to (speccount, Qnil);
1742 DEFUN ("sort", Fsort, 2, 2, 0, /*
1743 Sort LIST, stably, comparing elements using PREDICATE.
1744 Returns the sorted list. LIST is modified by side effects.
1745 PREDICATE is called with two elements of LIST, and should return T
1746 if the first element is "less" than the second.
1750 return list_sort (list, predicate, merge_pred_function);
1754 merge (Lisp_Object org_l1, Lisp_Object org_l2,
1757 return list_merge (org_l1, org_l2, pred, merge_pred_function);
1762 list_merge (Lisp_Object org_l1, Lisp_Object org_l2,
1763 Lisp_Object lisp_arg,
1764 int (*pred_fn) (Lisp_Object, Lisp_Object, Lisp_Object lisp_arg))
1770 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1777 /* It is sufficient to protect org_l1 and org_l2.
1778 When l1 and l2 are updated, we copy the new values
1779 back into the org_ vars. */
1781 GCPRO4 (org_l1, org_l2, lisp_arg, value);
1802 if (((*pred_fn) (Fcar (l2), Fcar (l1), lisp_arg)) < 0)
1817 Fsetcdr (tail, tem);
1823 /************************************************************************/
1824 /* property-list functions */
1825 /************************************************************************/
1827 /* For properties of text, we need to do order-insensitive comparison of
1828 plists. That is, we need to compare two plists such that they are the
1829 same if they have the same set of keys, and equivalent values.
1830 So (a 1 b 2) would be equal to (b 2 a 1).
1832 NIL_MEANS_NOT_PRESENT is as in `plists-eq' etc.
1833 LAXP means use `equal' for comparisons.
1836 plists_differ (Lisp_Object a, Lisp_Object b, int nil_means_not_present,
1837 int laxp, int depth)
1839 int eqp = (depth == -1); /* -1 as depth means use eq, not equal. */
1840 int la, lb, m, i, fill;
1841 Lisp_Object *keys, *vals;
1845 if (NILP (a) && NILP (b))
1848 Fcheck_valid_plist (a);
1849 Fcheck_valid_plist (b);
1851 la = XINT (Flength (a));
1852 lb = XINT (Flength (b));
1853 m = (la > lb ? la : lb);
1855 keys = alloca_array (Lisp_Object, m);
1856 vals = alloca_array (Lisp_Object, m);
1857 flags = alloca_array (char, m);
1859 /* First extract the pairs from A. */
1860 for (rest = a; !NILP (rest); rest = XCDR (XCDR (rest)))
1862 Lisp_Object k = XCAR (rest);
1863 Lisp_Object v = XCAR (XCDR (rest));
1864 /* Maybe be Ebolified. */
1865 if (nil_means_not_present && NILP (v)) continue;
1871 /* Now iterate over B, and stop if we find something that's not in A,
1872 or that doesn't match. As we match, mark them. */
1873 for (rest = b; !NILP (rest); rest = XCDR (XCDR (rest)))
1875 Lisp_Object k = XCAR (rest);
1876 Lisp_Object v = XCAR (XCDR (rest));
1877 /* Maybe be Ebolified. */
1878 if (nil_means_not_present && NILP (v)) continue;
1879 for (i = 0; i < fill; i++)
1881 if (!laxp ? EQ (k, keys [i]) : internal_equal (k, keys [i], depth))
1884 /* We narrowly escaped being Ebolified here. */
1885 ? !EQ_WITH_EBOLA_NOTICE (v, vals [i])
1886 : !internal_equal (v, vals [i], depth))
1887 /* a property in B has a different value than in A */
1894 /* there are some properties in B that are not in A */
1897 /* Now check to see that all the properties in A were also in B */
1898 for (i = 0; i < fill; i++)
1909 DEFUN ("plists-eq", Fplists_eq, 2, 3, 0, /*
1910 Return non-nil if property lists A and B are `eq'.
1911 A property list is an alternating list of keywords and values.
1912 This function does order-insensitive comparisons of the property lists:
1913 For example, the property lists '(a 1 b 2) and '(b 2 a 1) are equal.
1914 Comparison between values is done using `eq'. See also `plists-equal'.
1915 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
1916 a nil value is ignored. This feature is a virus that has infected
1917 old Lisp implementations, but should not be used except for backward
1920 (a, b, nil_means_not_present))
1922 return (plists_differ (a, b, !NILP (nil_means_not_present), 0, -1)
1926 DEFUN ("plists-equal", Fplists_equal, 2, 3, 0, /*
1927 Return non-nil if property lists A and B are `equal'.
1928 A property list is an alternating list of keywords and values. This
1929 function does order-insensitive comparisons of the property lists: For
1930 example, the property lists '(a 1 b 2) and '(b 2 a 1) are equal.
1931 Comparison between values is done using `equal'. See also `plists-eq'.
1932 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
1933 a nil value is ignored. This feature is a virus that has infected
1934 old Lisp implementations, but should not be used except for backward
1937 (a, b, nil_means_not_present))
1939 return (plists_differ (a, b, !NILP (nil_means_not_present), 0, 1)
1944 DEFUN ("lax-plists-eq", Flax_plists_eq, 2, 3, 0, /*
1945 Return non-nil if lax property lists A and B are `eq'.
1946 A property list is an alternating list of keywords and values.
1947 This function does order-insensitive comparisons of the property lists:
1948 For example, the property lists '(a 1 b 2) and '(b 2 a 1) are equal.
1949 Comparison between values is done using `eq'. See also `plists-equal'.
1950 A lax property list is like a regular one except that comparisons between
1951 keywords is done using `equal' instead of `eq'.
1952 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
1953 a nil value is ignored. This feature is a virus that has infected
1954 old Lisp implementations, but should not be used except for backward
1957 (a, b, nil_means_not_present))
1959 return (plists_differ (a, b, !NILP (nil_means_not_present), 1, -1)
1963 DEFUN ("lax-plists-equal", Flax_plists_equal, 2, 3, 0, /*
1964 Return non-nil if lax property lists A and B are `equal'.
1965 A property list is an alternating list of keywords and values. This
1966 function does order-insensitive comparisons of the property lists: For
1967 example, the property lists '(a 1 b 2) and '(b 2 a 1) are equal.
1968 Comparison between values is done using `equal'. See also `plists-eq'.
1969 A lax property list is like a regular one except that comparisons between
1970 keywords is done using `equal' instead of `eq'.
1971 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
1972 a nil value is ignored. This feature is a virus that has infected
1973 old Lisp implementations, but should not be used except for backward
1976 (a, b, nil_means_not_present))
1978 return (plists_differ (a, b, !NILP (nil_means_not_present), 1, 1)
1982 /* Return the value associated with key PROPERTY in property list PLIST.
1983 Return nil if key not found. This function is used for internal
1984 property lists that cannot be directly manipulated by the user.
1988 internal_plist_get (Lisp_Object plist, Lisp_Object property)
1992 for (tail = plist; !NILP (tail); tail = XCDR (XCDR (tail)))
1994 if (EQ (XCAR (tail), property))
1995 return XCAR (XCDR (tail));
2001 /* Set PLIST's value for PROPERTY to VALUE. Analogous to
2002 internal_plist_get(). */
2005 internal_plist_put (Lisp_Object *plist, Lisp_Object property,
2010 for (tail = *plist; !NILP (tail); tail = XCDR (XCDR (tail)))
2012 if (EQ (XCAR (tail), property))
2014 XCAR (XCDR (tail)) = value;
2019 *plist = Fcons (property, Fcons (value, *plist));
2023 internal_remprop (Lisp_Object *plist, Lisp_Object property)
2025 Lisp_Object tail, prev;
2027 for (tail = *plist, prev = Qnil;
2029 tail = XCDR (XCDR (tail)))
2031 if (EQ (XCAR (tail), property))
2034 *plist = XCDR (XCDR (tail));
2036 XCDR (XCDR (prev)) = XCDR (XCDR (tail));
2046 /* Called on a malformed property list. BADPLACE should be some
2047 place where truncating will form a good list -- i.e. we shouldn't
2048 result in a list with an odd length. */
2051 bad_bad_bunny (Lisp_Object *plist, Lisp_Object *badplace, Error_behavior errb)
2053 if (ERRB_EQ (errb, ERROR_ME))
2054 return Fsignal (Qmalformed_property_list, list2 (*plist, *badplace));
2057 if (ERRB_EQ (errb, ERROR_ME_WARN))
2059 warn_when_safe_lispobj
2062 ("Malformed property list -- list has been truncated"),
2070 /* Called on a circular property list. BADPLACE should be some place
2071 where truncating will result in an even-length list, as above.
2072 If doesn't particularly matter where we truncate -- anywhere we
2073 truncate along the entire list will break the circularity, because
2074 it will create a terminus and the list currently doesn't have one.
2078 bad_bad_turtle (Lisp_Object *plist, Lisp_Object *badplace, Error_behavior errb)
2080 if (ERRB_EQ (errb, ERROR_ME))
2081 return Fsignal (Qcircular_property_list, list1 (*plist));
2084 if (ERRB_EQ (errb, ERROR_ME_WARN))
2086 warn_when_safe_lispobj
2089 ("Circular property list -- list has been truncated"),
2097 /* Advance the tortoise pointer by two (one iteration of a property-list
2098 loop) and the hare pointer by four and verify that no malformations
2099 or circularities exist. If so, return zero and store a value into
2100 RETVAL that should be returned by the calling function. Otherwise,
2101 return 1. See external_plist_get().
2105 advance_plist_pointers (Lisp_Object *plist,
2106 Lisp_Object **tortoise, Lisp_Object **hare,
2107 Error_behavior errb, Lisp_Object *retval)
2110 Lisp_Object *tortsave = *tortoise;
2112 /* Note that our "fixing" may be more brutal than necessary,
2113 but it's the user's own problem, not ours, if they went in and
2114 manually fucked up a plist. */
2116 for (i = 0; i < 2; i++)
2118 /* This is a standard iteration of a defensive-loop-checking
2119 loop. We just do it twice because we want to advance past
2120 both the property and its value.
2122 If the pointer indirection is confusing you, remember that
2123 one level of indirection on the hare and tortoise pointers
2124 is only due to pass-by-reference for this function. The other
2125 level is so that the plist can be fixed in place. */
2127 /* When we reach the end of a well-formed plist, **HARE is
2128 nil. In that case, we don't do anything at all except
2129 advance TORTOISE by one. Otherwise, we advance HARE
2130 by two (making sure it's OK to do so), then advance
2131 TORTOISE by one (it will always be OK to do so because
2132 the HARE is always ahead of the TORTOISE and will have
2133 already verified the path), then make sure TORTOISE and
2134 HARE don't contain the same non-nil object -- if the
2135 TORTOISE and the HARE ever meet, then obviously we're
2136 in a circularity, and if we're in a circularity, then
2137 the TORTOISE and the HARE can't cross paths without
2138 meeting, since the HARE only gains one step over the
2139 TORTOISE per iteration. */
2143 Lisp_Object *haresave = *hare;
2144 if (!CONSP (**hare))
2146 *retval = bad_bad_bunny (plist, haresave, errb);
2149 *hare = &XCDR (**hare);
2150 /* In a non-plist, we'd check here for a nil value for
2151 **HARE, which is OK (it just means the list has an
2152 odd number of elements). In a plist, it's not OK
2153 for the list to have an odd number of elements. */
2154 if (!CONSP (**hare))
2156 *retval = bad_bad_bunny (plist, haresave, errb);
2159 *hare = &XCDR (**hare);
2162 *tortoise = &XCDR (**tortoise);
2163 if (!NILP (**hare) && EQ (**tortoise, **hare))
2165 *retval = bad_bad_turtle (plist, tortsave, errb);
2173 /* Return the value of PROPERTY from PLIST, or Qunbound if
2174 property is not on the list.
2176 PLIST is a Lisp-accessible property list, meaning that it
2177 has to be checked for malformations and circularities.
2179 If ERRB is ERROR_ME, an error will be signalled. Otherwise, the
2180 function will never signal an error; and if ERRB is ERROR_ME_WARN,
2181 on finding a malformation or a circularity, it issues a warning and
2182 attempts to silently fix the problem.
2184 A pointer to PLIST is passed in so that PLIST can be successfully
2185 "fixed" even if the error is at the beginning of the plist. */
2188 external_plist_get (Lisp_Object *plist, Lisp_Object property,
2189 int laxp, Error_behavior errb)
2191 Lisp_Object *tortoise = plist;
2192 Lisp_Object *hare = plist;
2194 while (!NILP (*tortoise))
2196 Lisp_Object *tortsave = tortoise;
2199 /* We do the standard tortoise/hare march. We isolate the
2200 grungy stuff to do this in advance_plist_pointers(), though.
2201 To us, all this function does is advance the tortoise
2202 pointer by two and the hare pointer by four and make sure
2203 everything's OK. We first advance the pointers and then
2204 check if a property matched; this ensures that our
2205 check for a matching property is safe. */
2207 if (!advance_plist_pointers (plist, &tortoise, &hare, errb, &retval))
2210 if (!laxp ? EQ (XCAR (*tortsave), property)
2211 : internal_equal (XCAR (*tortsave), property, 0))
2212 return XCAR (XCDR (*tortsave));
2218 /* Set PLIST's value for PROPERTY to VALUE, given a possibly
2219 malformed or circular plist. Analogous to external_plist_get(). */
2222 external_plist_put (Lisp_Object *plist, Lisp_Object property,
2223 Lisp_Object value, int laxp, Error_behavior errb)
2225 Lisp_Object *tortoise = plist;
2226 Lisp_Object *hare = plist;
2228 while (!NILP (*tortoise))
2230 Lisp_Object *tortsave = tortoise;
2234 if (!advance_plist_pointers (plist, &tortoise, &hare, errb, &retval))
2237 if (!laxp ? EQ (XCAR (*tortsave), property)
2238 : internal_equal (XCAR (*tortsave), property, 0))
2240 XCAR (XCDR (*tortsave)) = value;
2245 *plist = Fcons (property, Fcons (value, *plist));
2249 external_remprop (Lisp_Object *plist, Lisp_Object property,
2250 int laxp, Error_behavior errb)
2252 Lisp_Object *tortoise = plist;
2253 Lisp_Object *hare = plist;
2255 while (!NILP (*tortoise))
2257 Lisp_Object *tortsave = tortoise;
2261 if (!advance_plist_pointers (plist, &tortoise, &hare, errb, &retval))
2264 if (!laxp ? EQ (XCAR (*tortsave), property)
2265 : internal_equal (XCAR (*tortsave), property, 0))
2267 /* Now you see why it's so convenient to have that level
2269 *tortsave = XCDR (XCDR (*tortsave));
2277 DEFUN ("plist-get", Fplist_get, 2, 3, 0, /*
2278 Extract a value from a property list.
2279 PLIST is a property list, which is a list of the form
2280 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2...).
2281 PROPERTY is usually a symbol.
2282 This function returns the value corresponding to the PROPERTY,
2283 or DEFAULT if PROPERTY is not one of the properties on the list.
2285 (plist, property, default_))
2287 Lisp_Object value = external_plist_get (&plist, property, 0, ERROR_ME);
2288 return UNBOUNDP (value) ? default_ : value;
2291 DEFUN ("plist-put", Fplist_put, 3, 3, 0, /*
2292 Change value in PLIST of PROPERTY to VALUE.
2293 PLIST is a property list, which is a list of the form
2294 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2 ...).
2295 PROPERTY is usually a symbol and VALUE is any object.
2296 If PROPERTY is already a property on the list, its value is set to VALUE,
2297 otherwise the new PROPERTY VALUE pair is added.
2298 The new plist is returned; use `(setq x (plist-put x property value))'
2299 to be sure to use the new value. PLIST is modified by side effect.
2301 (plist, property, value))
2303 external_plist_put (&plist, property, value, 0, ERROR_ME);
2307 DEFUN ("plist-remprop", Fplist_remprop, 2, 2, 0, /*
2308 Remove from PLIST the property PROPERTY and its value.
2309 PLIST is a property list, which is a list of the form
2310 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2 ...).
2311 PROPERTY is usually a symbol.
2312 The new plist is returned; use `(setq x (plist-remprop x property))'
2313 to be sure to use the new value. PLIST is modified by side effect.
2317 external_remprop (&plist, property, 0, ERROR_ME);
2321 DEFUN ("plist-member", Fplist_member, 2, 2, 0, /*
2322 Return t if PROPERTY has a value specified in PLIST.
2326 Lisp_Object value = Fplist_get (plist, property, Qunbound);
2327 return UNBOUNDP (value) ? Qnil : Qt;
2330 DEFUN ("check-valid-plist", Fcheck_valid_plist, 1, 1, 0, /*
2331 Given a plist, signal an error if there is anything wrong with it.
2332 This means that it's a malformed or circular plist.
2336 Lisp_Object *tortoise;
2342 while (!NILP (*tortoise))
2347 if (!advance_plist_pointers (&plist, &tortoise, &hare, ERROR_ME,
2355 DEFUN ("valid-plist-p", Fvalid_plist_p, 1, 1, 0, /*
2356 Given a plist, return non-nil if its format is correct.
2357 If it returns nil, `check-valid-plist' will signal an error when given
2358 the plist; that means it's a malformed or circular plist.
2362 Lisp_Object *tortoise;
2367 while (!NILP (*tortoise))
2372 if (!advance_plist_pointers (&plist, &tortoise, &hare, ERROR_ME_NOT,
2380 DEFUN ("canonicalize-plist", Fcanonicalize_plist, 1, 2, 0, /*
2381 Destructively remove any duplicate entries from a plist.
2382 In such cases, the first entry applies.
2384 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
2385 a nil value is removed. This feature is a virus that has infected
2386 old Lisp implementations, but should not be used except for backward
2389 The new plist is returned. If NIL-MEANS-NOT-PRESENT is given, the
2390 return value may not be EQ to the passed-in value, so make sure to
2391 `setq' the value back into where it came from.
2393 (plist, nil_means_not_present))
2395 Lisp_Object head = plist;
2397 Fcheck_valid_plist (plist);
2399 while (!NILP (plist))
2401 Lisp_Object prop = Fcar (plist);
2402 Lisp_Object next = Fcdr (plist);
2404 CHECK_CONS (next); /* just make doubly sure we catch any errors */
2405 if (!NILP (nil_means_not_present) && NILP (Fcar (next)))
2407 if (EQ (head, plist))
2409 plist = Fcdr (next);
2412 /* external_remprop returns 1 if it removed any property.
2413 We have to loop till it didn't remove anything, in case
2414 the property occurs many times. */
2415 while (external_remprop (&XCDR (next), prop, 0, ERROR_ME))
2417 plist = Fcdr (next);
2423 DEFUN ("lax-plist-get", Flax_plist_get, 2, 3, 0, /*
2424 Extract a value from a lax property list.
2425 LAX-PLIST is a lax property list, which is a list of the form
2426 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2...), where comparisons between
2427 properties is done using `equal' instead of `eq'.
2428 PROPERTY is usually a symbol.
2429 This function returns the value corresponding to PROPERTY,
2430 or DEFAULT if PROPERTY is not one of the properties on the list.
2432 (lax_plist, property, default_))
2434 Lisp_Object value = external_plist_get (&lax_plist, property, 1, ERROR_ME);
2435 return UNBOUNDP (value) ? default_ : value;
2438 DEFUN ("lax-plist-put", Flax_plist_put, 3, 3, 0, /*
2439 Change value in LAX-PLIST of PROPERTY to VALUE.
2440 LAX-PLIST is a lax property list, which is a list of the form
2441 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2...), where comparisons between
2442 properties is done using `equal' instead of `eq'.
2443 PROPERTY is usually a symbol and VALUE is any object.
2444 If PROPERTY is already a property on the list, its value is set to
2445 VALUE, otherwise the new PROPERTY VALUE pair is added.
2446 The new plist is returned; use `(setq x (lax-plist-put x property value))'
2447 to be sure to use the new value. LAX-PLIST is modified by side effect.
2449 (lax_plist, property, value))
2451 external_plist_put (&lax_plist, property, value, 1, ERROR_ME);
2455 DEFUN ("lax-plist-remprop", Flax_plist_remprop, 2, 2, 0, /*
2456 Remove from LAX-PLIST the property PROPERTY and its value.
2457 LAX-PLIST is a lax property list, which is a list of the form
2458 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2...), where comparisons between
2459 properties is done using `equal' instead of `eq'.
2460 PROPERTY is usually a symbol.
2461 The new plist is returned; use `(setq x (lax-plist-remprop x property))'
2462 to be sure to use the new value. LAX-PLIST is modified by side effect.
2464 (lax_plist, property))
2466 external_remprop (&lax_plist, property, 1, ERROR_ME);
2470 DEFUN ("lax-plist-member", Flax_plist_member, 2, 2, 0, /*
2471 Return t if PROPERTY has a value specified in LAX-PLIST.
2472 LAX-PLIST is a lax property list, which is a list of the form
2473 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2...), where comparisons between
2474 properties is done using `equal' instead of `eq'.
2476 (lax_plist, property))
2478 return UNBOUNDP (Flax_plist_get (lax_plist, property, Qunbound)) ? Qnil : Qt;
2481 DEFUN ("canonicalize-lax-plist", Fcanonicalize_lax_plist, 1, 2, 0, /*
2482 Destructively remove any duplicate entries from a lax plist.
2483 In such cases, the first entry applies.
2485 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
2486 a nil value is removed. This feature is a virus that has infected
2487 old Lisp implementations, but should not be used except for backward
2490 The new plist is returned. If NIL-MEANS-NOT-PRESENT is given, the
2491 return value may not be EQ to the passed-in value, so make sure to
2492 `setq' the value back into where it came from.
2494 (lax_plist, nil_means_not_present))
2496 Lisp_Object head = lax_plist;
2498 Fcheck_valid_plist (lax_plist);
2500 while (!NILP (lax_plist))
2502 Lisp_Object prop = Fcar (lax_plist);
2503 Lisp_Object next = Fcdr (lax_plist);
2505 CHECK_CONS (next); /* just make doubly sure we catch any errors */
2506 if (!NILP (nil_means_not_present) && NILP (Fcar (next)))
2508 if (EQ (head, lax_plist))
2510 lax_plist = Fcdr (next);
2513 /* external_remprop returns 1 if it removed any property.
2514 We have to loop till it didn't remove anything, in case
2515 the property occurs many times. */
2516 while (external_remprop (&XCDR (next), prop, 1, ERROR_ME))
2518 lax_plist = Fcdr (next);
2524 /* In C because the frame props stuff uses it */
2526 DEFUN ("destructive-alist-to-plist", Fdestructive_alist_to_plist, 1, 1, 0, /*
2527 Convert association list ALIST into the equivalent property-list form.
2528 The plist is returned. This converts from
2530 \((a . 1) (b . 2) (c . 3))
2536 The original alist is destroyed in the process of constructing the plist.
2537 See also `alist-to-plist'.
2541 Lisp_Object head = alist;
2542 while (!NILP (alist))
2544 /* remember the alist element. */
2545 Lisp_Object el = Fcar (alist);
2547 Fsetcar (alist, Fcar (el));
2548 Fsetcar (el, Fcdr (el));
2549 Fsetcdr (el, Fcdr (alist));
2550 Fsetcdr (alist, el);
2551 alist = Fcdr (Fcdr (alist));
2557 DEFUN ("get", Fget, 2, 3, 0, /*
2558 Return the value of OBJECT's PROPERTY property.
2559 This is the last VALUE stored with `(put OBJECT PROPERTY VALUE)'.
2560 If there is no such property, return optional third arg DEFAULT
2561 \(which defaults to `nil'). OBJECT can be a symbol, string, extent,
2562 face, or glyph. See also `put', `remprop', and `object-plist'.
2564 (object, property, default_))
2566 /* Various places in emacs call Fget() and expect it not to quit,
2570 if (LRECORDP (object) && XRECORD_LHEADER_IMPLEMENTATION (object)->getprop)
2571 val = XRECORD_LHEADER_IMPLEMENTATION (object)->getprop (object, property);
2573 signal_simple_error ("Object type has no properties", object);
2575 return UNBOUNDP (val) ? default_ : val;
2578 DEFUN ("put", Fput, 3, 3, 0, /*
2579 Set OBJECT's PROPERTY to VALUE.
2580 It can be subsequently retrieved with `(get OBJECT PROPERTY)'.
2581 OBJECT can be a symbol, face, extent, or string.
2582 For a string, no properties currently have predefined meanings.
2583 For the predefined properties for extents, see `set-extent-property'.
2584 For the predefined properties for faces, see `set-face-property'.
2585 See also `get', `remprop', and `object-plist'.
2587 (object, property, value))
2589 CHECK_LISP_WRITEABLE (object);
2591 if (LRECORDP (object) && XRECORD_LHEADER_IMPLEMENTATION (object)->putprop)
2593 if (! XRECORD_LHEADER_IMPLEMENTATION (object)->putprop
2594 (object, property, value))
2595 signal_simple_error ("Can't set property on object", property);
2598 signal_simple_error ("Object type has no settable properties", object);
2603 DEFUN ("remprop", Fremprop, 2, 2, 0, /*
2604 Remove, from OBJECT's property list, PROPERTY and its corresponding value.
2605 OBJECT can be a symbol, string, extent, face, or glyph. Return non-nil
2606 if the property list was actually modified (i.e. if PROPERTY was present
2607 in the property list). See also `get', `put', and `object-plist'.
2613 CHECK_LISP_WRITEABLE (object);
2615 if (LRECORDP (object) && XRECORD_LHEADER_IMPLEMENTATION (object)->remprop)
2617 ret = XRECORD_LHEADER_IMPLEMENTATION (object)->remprop (object, property);
2619 signal_simple_error ("Can't remove property from object", property);
2622 signal_simple_error ("Object type has no removable properties", object);
2624 return ret ? Qt : Qnil;
2627 DEFUN ("object-plist", Fobject_plist, 1, 1, 0, /*
2628 Return a property list of OBJECT's properties.
2629 For a symbol, this is equivalent to `symbol-plist'.
2630 OBJECT can be a symbol, string, extent, face, or glyph.
2631 Do not modify the returned property list directly;
2632 this may or may not have the desired effects. Use `put' instead.
2636 if (LRECORDP (object) && XRECORD_LHEADER_IMPLEMENTATION (object)->plist)
2637 return XRECORD_LHEADER_IMPLEMENTATION (object)->plist (object);
2639 signal_simple_error ("Object type has no properties", object);
2646 internal_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
2649 error ("Stack overflow in equal");
2651 if (EQ_WITH_EBOLA_NOTICE (obj1, obj2))
2653 /* Note that (equal 20 20.0) should be nil */
2654 if (XTYPE (obj1) != XTYPE (obj2))
2656 if (LRECORDP (obj1))
2658 const struct lrecord_implementation
2659 *imp1 = XRECORD_LHEADER_IMPLEMENTATION (obj1),
2660 *imp2 = XRECORD_LHEADER_IMPLEMENTATION (obj2);
2662 return (imp1 == imp2) &&
2663 /* EQ-ness of the objects was noticed above */
2664 (imp1->equal && (imp1->equal) (obj1, obj2, depth));
2670 /* Note that we may be calling sub-objects that will use
2671 internal_equal() (instead of internal_old_equal()). Oh well.
2672 We will get an Ebola note if there's any possibility of confusion,
2673 but that seems unlikely. */
2676 internal_old_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
2679 error ("Stack overflow in equal");
2681 if (HACKEQ_UNSAFE (obj1, obj2))
2683 /* Note that (equal 20 20.0) should be nil */
2684 if (XTYPE (obj1) != XTYPE (obj2))
2687 return internal_equal (obj1, obj2, depth);
2690 DEFUN ("equal", Fequal, 2, 2, 0, /*
2691 Return t if two Lisp objects have similar structure and contents.
2692 They must have the same data type.
2693 Conses are compared by comparing the cars and the cdrs.
2694 Vectors and strings are compared element by element.
2695 Numbers are compared by value. Symbols must match exactly.
2699 return internal_equal (object1, object2, 0) ? Qt : Qnil;
2702 DEFUN ("old-equal", Fold_equal, 2, 2, 0, /*
2703 Return t if two Lisp objects have similar structure and contents.
2704 They must have the same data type.
2705 \(Note, however, that an exception is made for characters and integers;
2706 this is known as the "char-int confoundance disease." See `eq' and
2708 This function is provided only for byte-code compatibility with v19.
2713 return internal_old_equal (object1, object2, 0) ? Qt : Qnil;
2717 DEFUN ("fillarray", Ffillarray, 2, 2, 0, /*
2718 Destructively modify ARRAY by replacing each element with ITEM.
2719 ARRAY is a vector, bit vector, or string.
2724 if (STRINGP (array))
2726 Lisp_String *s = XSTRING (array);
2727 Bytecount old_bytecount = string_length (s);
2728 Bytecount new_bytecount;
2729 Bytecount item_bytecount;
2730 Bufbyte item_buf[MAX_EMCHAR_LEN];
2734 CHECK_CHAR_COERCE_INT (item);
2735 CHECK_LISP_WRITEABLE (array);
2737 item_bytecount = set_charptr_emchar (item_buf, XCHAR (item));
2738 new_bytecount = item_bytecount * string_char_length (s);
2740 resize_string (s, -1, new_bytecount - old_bytecount);
2742 for (p = string_data (s), end = p + new_bytecount;
2744 p += item_bytecount)
2745 memcpy (p, item_buf, item_bytecount);
2748 bump_string_modiff (array);
2750 else if (VECTORP (array))
2752 Lisp_Object *p = XVECTOR_DATA (array);
2753 size_t len = XVECTOR_LENGTH (array);
2754 CHECK_LISP_WRITEABLE (array);
2758 else if (BIT_VECTORP (array))
2760 Lisp_Bit_Vector *v = XBIT_VECTOR (array);
2761 size_t len = bit_vector_length (v);
2765 CHECK_LISP_WRITEABLE (array);
2767 set_bit_vector_bit (v, len, bit);
2771 array = wrong_type_argument (Qarrayp, array);
2778 nconc2 (Lisp_Object arg1, Lisp_Object arg2)
2780 Lisp_Object args[2];
2781 struct gcpro gcpro1;
2788 RETURN_UNGCPRO (bytecode_nconc2 (args));
2792 bytecode_nconc2 (Lisp_Object *args)
2796 if (CONSP (args[0]))
2798 /* (setcdr (last args[0]) args[1]) */
2799 Lisp_Object tortoise, hare;
2802 for (hare = tortoise = args[0], count = 0;
2803 CONSP (XCDR (hare));
2804 hare = XCDR (hare), count++)
2806 if (count < CIRCULAR_LIST_SUSPICION_LENGTH) continue;
2809 tortoise = XCDR (tortoise);
2810 if (EQ (hare, tortoise))
2811 signal_circular_list_error (args[0]);
2813 XCDR (hare) = args[1];
2816 else if (NILP (args[0]))
2822 args[0] = wrong_type_argument (args[0], Qlistp);
2827 DEFUN ("nconc", Fnconc, 0, MANY, 0, /*
2828 Concatenate any number of lists by altering them.
2829 Only the last argument is not altered, and need not be a list.
2831 If the first argument is nil, there is no way to modify it by side
2832 effect; therefore, write `(setq foo (nconc foo list))' to be sure of
2833 changing the value of `foo'.
2835 (int nargs, Lisp_Object *args))
2838 struct gcpro gcpro1;
2840 /* The modus operandi in Emacs is "caller gc-protects args".
2841 However, nconc (particularly nconc2 ()) is called many times
2842 in Emacs on freshly created stuff (e.g. you see the idiom
2843 nconc2 (Fcopy_sequence (foo), bar) a lot). So we help those
2844 callers out by protecting the args ourselves to save them
2845 a lot of temporary-variable grief. */
2848 gcpro1.nvars = nargs;
2850 while (argnum < nargs)
2857 /* `val' is the first cons, which will be our return value. */
2858 /* `last_cons' will be the cons cell to mutate. */
2859 Lisp_Object last_cons = val;
2860 Lisp_Object tortoise = val;
2862 for (argnum++; argnum < nargs; argnum++)
2864 Lisp_Object next = args[argnum];
2866 if (CONSP (next) || argnum == nargs -1)
2868 /* (setcdr (last val) next) */
2872 CONSP (XCDR (last_cons));
2873 last_cons = XCDR (last_cons), count++)
2875 if (count < CIRCULAR_LIST_SUSPICION_LENGTH) continue;
2878 tortoise = XCDR (tortoise);
2879 if (EQ (last_cons, tortoise))
2880 signal_circular_list_error (args[argnum-1]);
2882 XCDR (last_cons) = next;
2884 else if (NILP (next))
2890 next = wrong_type_argument (Qlistp, next);
2894 RETURN_UNGCPRO (val);
2896 else if (NILP (val))
2898 else if (argnum == nargs - 1) /* last arg? */
2899 RETURN_UNGCPRO (val);
2902 args[argnum] = wrong_type_argument (Qlistp, val);
2906 RETURN_UNGCPRO (Qnil); /* No non-nil args provided. */
2910 /* This is the guts of several mapping functions.
2911 Apply FUNCTION to each element of SEQUENCE, one by one,
2912 storing the results into elements of VALS, a C vector of Lisp_Objects.
2913 LENI is the length of VALS, which should also be the length of SEQUENCE.
2915 If VALS is a null pointer, do not accumulate the results. */
2918 mapcar1 (size_t leni, Lisp_Object *vals,
2919 Lisp_Object function, Lisp_Object sequence)
2922 Lisp_Object args[2];
2923 struct gcpro gcpro1;
2933 if (LISTP (sequence))
2935 /* A devious `function' could either:
2936 - insert garbage into the list in front of us, causing XCDR to crash
2937 - amputate the list behind us using (setcdr), causing the remaining
2938 elts to lose their GCPRO status.
2940 if (vals != 0) we avoid this by copying the elts into the
2941 `vals' array. By a stroke of luck, `vals' is exactly large
2942 enough to hold the elts left to be traversed as well as the
2943 results computed so far.
2945 if (vals == 0) we don't have any free space available and
2946 don't want to eat up any more stack with alloca().
2947 So we use EXTERNAL_LIST_LOOP_3_NO_DECLARE and GCPRO the tail. */
2951 Lisp_Object *val = vals;
2954 LIST_LOOP_2 (elt, sequence)
2957 gcpro1.nvars = leni;
2959 for (i = 0; i < leni; i++)
2962 vals[i] = Ffuncall (2, args);
2967 Lisp_Object elt, tail;
2968 EMACS_INT len_unused;
2969 struct gcpro ngcpro1;
2974 EXTERNAL_LIST_LOOP_4_NO_DECLARE (elt, sequence, tail, len_unused)
2984 else if (VECTORP (sequence))
2986 Lisp_Object *objs = XVECTOR_DATA (sequence);
2988 for (i = 0; i < leni; i++)
2991 result = Ffuncall (2, args);
2992 if (vals) vals[gcpro1.nvars++] = result;
2995 else if (STRINGP (sequence))
2997 /* The string data of `sequence' might be relocated during GC. */
2998 Bytecount slen = XSTRING_LENGTH (sequence);
2999 Bufbyte *p = alloca_array (Bufbyte, slen);
3000 Bufbyte *end = p + slen;
3002 memcpy (p, XSTRING_DATA (sequence), slen);
3006 args[1] = make_char (charptr_emchar (p));
3008 result = Ffuncall (2, args);
3009 if (vals) vals[gcpro1.nvars++] = result;
3012 else if (BIT_VECTORP (sequence))
3014 Lisp_Bit_Vector *v = XBIT_VECTOR (sequence);
3016 for (i = 0; i < leni; i++)
3018 args[1] = make_int (bit_vector_bit (v, i));
3019 result = Ffuncall (2, args);
3020 if (vals) vals[gcpro1.nvars++] = result;
3024 abort (); /* unreachable, since Flength (sequence) did not get an error */
3030 DEFUN ("mapconcat", Fmapconcat, 3, 3, 0, /*
3031 Apply FUNCTION to each element of SEQUENCE, and concat the results to a string.
3032 Between each pair of results, insert SEPARATOR.
3034 Each result, and SEPARATOR, should be strings. Thus, using " " as SEPARATOR
3035 results in spaces between the values returned by FUNCTION. SEQUENCE itself
3036 may be a list, a vector, a bit vector, or a string.
3038 (function, sequence, separator))
3040 EMACS_INT len = XINT (Flength (sequence));
3043 EMACS_INT nargs = len + len - 1;
3045 if (len == 0) return build_string ("");
3047 args = alloca_array (Lisp_Object, nargs);
3049 mapcar1 (len, args, function, sequence);
3051 for (i = len - 1; i >= 0; i--)
3052 args[i + i] = args[i];
3054 for (i = 1; i < nargs; i += 2)
3055 args[i] = separator;
3057 return Fconcat (nargs, args);
3060 DEFUN ("mapcar", Fmapcar, 2, 2, 0, /*
3061 Apply FUNCTION to each element of SEQUENCE; return a list of the results.
3062 The result is a list of the same length as SEQUENCE.
3063 SEQUENCE may be a list, a vector, a bit vector, or a string.
3065 (function, sequence))
3067 size_t len = XINT (Flength (sequence));
3068 Lisp_Object *args = alloca_array (Lisp_Object, len);
3070 mapcar1 (len, args, function, sequence);
3072 return Flist (len, args);
3075 DEFUN ("mapvector", Fmapvector, 2, 2, 0, /*
3076 Apply FUNCTION to each element of SEQUENCE; return a vector of the results.
3077 The result is a vector of the same length as SEQUENCE.
3078 SEQUENCE may be a list, a vector, a bit vector, or a string.
3080 (function, sequence))
3082 size_t len = XINT (Flength (sequence));
3083 Lisp_Object result = make_vector (len, Qnil);
3084 struct gcpro gcpro1;
3087 mapcar1 (len, XVECTOR_DATA (result), function, sequence);
3093 DEFUN ("mapc-internal", Fmapc_internal, 2, 2, 0, /*
3094 Apply FUNCTION to each element of SEQUENCE.
3095 SEQUENCE may be a list, a vector, a bit vector, or a string.
3096 This function is like `mapcar' but does not accumulate the results,
3097 which is more efficient if you do not use the results.
3099 The difference between this and `mapc' is that `mapc' supports all
3100 the spiffy Common Lisp arguments. You should normally use `mapc'.
3102 (function, sequence))
3104 mapcar1 (XINT (Flength (sequence)), 0, function, sequence);
3112 DEFUN ("replace-list", Freplace_list, 2, 2, 0, /*
3113 Destructively replace the list OLD with NEW.
3114 This is like (copy-sequence NEW) except that it reuses the
3115 conses in OLD as much as possible. If OLD and NEW are the same
3116 length, no consing will take place.
3120 Lisp_Object tail, oldtail = old, prevoldtail = Qnil;
3122 EXTERNAL_LIST_LOOP (tail, new)
3124 if (!NILP (oldtail))
3126 CHECK_CONS (oldtail);
3127 XCAR (oldtail) = XCAR (tail);
3129 else if (!NILP (prevoldtail))
3131 XCDR (prevoldtail) = Fcons (XCAR (tail), Qnil);
3132 prevoldtail = XCDR (prevoldtail);
3135 old = oldtail = Fcons (XCAR (tail), Qnil);
3137 if (!NILP (oldtail))
3139 prevoldtail = oldtail;
3140 oldtail = XCDR (oldtail);
3144 if (!NILP (prevoldtail))
3145 XCDR (prevoldtail) = Qnil;
3153 /* #### this function doesn't belong in this file! */
3155 #ifdef HAVE_GETLOADAVG
3156 #ifdef HAVE_SYS_LOADAVG_H
3157 #include <sys/loadavg.h>
3160 int getloadavg (double loadavg[], int nelem); /* Defined in getloadavg.c */
3163 DEFUN ("load-average", Fload_average, 0, 1, 0, /*
3164 Return list of 1 minute, 5 minute and 15 minute load averages.
3165 Each of the three load averages is multiplied by 100,
3166 then converted to integer.
3168 When USE-FLOATS is non-nil, floats will be used instead of integers.
3169 These floats are not multiplied by 100.
3171 If the 5-minute or 15-minute load averages are not available, return a
3172 shortened list, containing only those averages which are available.
3174 On some systems, this won't work due to permissions on /dev/kmem,
3175 in which case you can't use this.
3180 int loads = getloadavg (load_ave, countof (load_ave));
3181 Lisp_Object ret = Qnil;
3184 error ("load-average not implemented for this operating system");
3186 signal_simple_error ("Could not get load-average",
3187 lisp_strerror (errno));
3191 Lisp_Object load = (NILP (use_floats) ?
3192 make_int ((int) (100.0 * load_ave[loads]))
3193 : make_float (load_ave[loads]));
3194 ret = Fcons (load, ret);
3200 Lisp_Object Vfeatures;
3202 DEFUN ("featurep", Ffeaturep, 1, 1, 0, /*
3203 Return non-nil if feature FEXP is present in this Emacs.
3204 Use this to conditionalize execution of lisp code based on the
3205 presence or absence of emacs or environment extensions.
3206 FEXP can be a symbol, a number, or a list.
3207 If it is a symbol, that symbol is looked up in the `features' variable,
3208 and non-nil will be returned if found.
3209 If it is a number, the function will return non-nil if this Emacs
3210 has an equal or greater version number than FEXP.
3211 If it is a list whose car is the symbol `and', it will return
3212 non-nil if all the features in its cdr are non-nil.
3213 If it is a list whose car is the symbol `or', it will return non-nil
3214 if any of the features in its cdr are non-nil.
3215 If it is a list whose car is the symbol `not', it will return
3216 non-nil if the feature is not present.
3221 => ; Non-nil on XEmacs.
3223 (featurep '(and xemacs gnus))
3224 => ; Non-nil on XEmacs with Gnus loaded.
3226 (featurep '(or tty-frames (and emacs 19.30)))
3227 => ; Non-nil if this Emacs supports TTY frames.
3229 (featurep '(or (and xemacs 19.15) (and emacs 19.34)))
3230 => ; Non-nil on XEmacs 19.15 and later, or FSF Emacs 19.34 and later.
3232 (featurep '(and xemacs 21.02))
3233 => ; Non-nil on XEmacs 21.2 and later.
3235 NOTE: The advanced arguments of this function (anything other than a
3236 symbol) are not yet supported by FSF Emacs. If you feel they are useful
3237 for supporting multiple Emacs variants, lobby Richard Stallman at
3238 <bug-gnu-emacs@gnu.org>.
3242 #ifndef FEATUREP_SYNTAX
3243 CHECK_SYMBOL (fexp);
3244 return NILP (Fmemq (fexp, Vfeatures)) ? Qnil : Qt;
3245 #else /* FEATUREP_SYNTAX */
3246 static double featurep_emacs_version;
3248 /* Brute force translation from Erik Naggum's lisp function. */
3251 /* Original definition */
3252 return NILP (Fmemq (fexp, Vfeatures)) ? Qnil : Qt;
3254 else if (INTP (fexp) || FLOATP (fexp))
3256 double d = extract_float (fexp);
3258 if (featurep_emacs_version == 0.0)
3260 featurep_emacs_version = XINT (Vemacs_major_version) +
3261 (XINT (Vemacs_minor_version) / 100.0);
3263 return featurep_emacs_version >= d ? Qt : Qnil;
3265 else if (CONSP (fexp))
3267 Lisp_Object tem = XCAR (fexp);
3273 negate = Fcar (tem);
3275 return NILP (call1 (Qfeaturep, negate)) ? Qt : Qnil;
3277 return Fsignal (Qinvalid_read_syntax, list1 (tem));
3279 else if (EQ (tem, Qand))
3282 /* Use Fcar/Fcdr for error-checking. */
3283 while (!NILP (tem) && !NILP (call1 (Qfeaturep, Fcar (tem))))
3287 return NILP (tem) ? Qt : Qnil;
3289 else if (EQ (tem, Qor))
3292 /* Use Fcar/Fcdr for error-checking. */
3293 while (!NILP (tem) && NILP (call1 (Qfeaturep, Fcar (tem))))
3297 return NILP (tem) ? Qnil : Qt;
3301 return Fsignal (Qinvalid_read_syntax, list1 (XCDR (fexp)));
3306 return Fsignal (Qinvalid_read_syntax, list1 (fexp));
3309 #endif /* FEATUREP_SYNTAX */
3311 DEFUN ("provide", Fprovide, 1, 1, 0, /*
3312 Announce that FEATURE is a feature of the current Emacs.
3313 This function updates the value of the variable `features'.
3318 CHECK_SYMBOL (feature);
3319 if (!NILP (Vautoload_queue))
3320 Vautoload_queue = Fcons (Fcons (Vfeatures, Qnil), Vautoload_queue);
3321 tem = Fmemq (feature, Vfeatures);
3323 Vfeatures = Fcons (feature, Vfeatures);
3324 LOADHIST_ATTACH (Fcons (Qprovide, feature));
3328 DEFUN ("require", Frequire, 1, 2, 0, /*
3329 If feature FEATURE is not loaded, load it from FILENAME.
3330 If FEATURE is not a member of the list `features', then the feature
3331 is not loaded; so load the file FILENAME.
3332 If FILENAME is omitted, the printname of FEATURE is used as the file name.
3334 (feature, filename))
3337 CHECK_SYMBOL (feature);
3338 tem = Fmemq (feature, Vfeatures);
3339 LOADHIST_ATTACH (Fcons (Qrequire, feature));
3344 int speccount = specpdl_depth ();
3346 /* Value saved here is to be restored into Vautoload_queue */
3347 record_unwind_protect (un_autoload, Vautoload_queue);
3348 Vautoload_queue = Qt;
3350 call4 (Qload, NILP (filename) ? Fsymbol_name (feature) : filename,
3353 tem = Fmemq (feature, Vfeatures);
3355 error ("Required feature %s was not provided",
3356 string_data (XSYMBOL (feature)->name));
3358 /* Once loading finishes, don't undo it. */
3359 Vautoload_queue = Qt;
3360 return unbind_to (speccount, feature);
3364 /* base64 encode/decode functions.
3366 Originally based on code from GNU recode. Ported to FSF Emacs by
3367 Lars Magne Ingebrigtsen and Karl Heuer. Ported to XEmacs and
3368 subsequently heavily hacked by Hrvoje Niksic. */
3370 #define MIME_LINE_LENGTH 72
3372 #define IS_ASCII(Character) \
3374 #define IS_BASE64(Character) \
3375 (IS_ASCII (Character) && base64_char_to_value[Character] >= 0)
3377 /* Table of characters coding the 64 values. */
3378 static char base64_value_to_char[64] =
3380 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 0- 9 */
3381 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 10-19 */
3382 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', /* 20-29 */
3383 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', /* 30-39 */
3384 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', /* 40-49 */
3385 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', /* 50-59 */
3386 '8', '9', '+', '/' /* 60-63 */
3389 /* Table of base64 values for first 128 characters. */
3390 static short base64_char_to_value[128] =
3392 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0- 9 */
3393 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10- 19 */
3394 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20- 29 */
3395 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 30- 39 */
3396 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, /* 40- 49 */
3397 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, /* 50- 59 */
3398 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, /* 60- 69 */
3399 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 70- 79 */
3400 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, /* 80- 89 */
3401 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, /* 90- 99 */
3402 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, /* 100-109 */
3403 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, /* 110-119 */
3404 49, 50, 51, -1, -1, -1, -1, -1 /* 120-127 */
3407 /* The following diagram shows the logical steps by which three octets
3408 get transformed into four base64 characters.
3410 .--------. .--------. .--------.
3411 |aaaaaabb| |bbbbcccc| |ccdddddd|
3412 `--------' `--------' `--------'
3414 .--------+--------+--------+--------.
3415 |00aaaaaa|00bbbbbb|00cccccc|00dddddd|
3416 `--------+--------+--------+--------'
3418 .--------+--------+--------+--------.
3419 |AAAAAAAA|BBBBBBBB|CCCCCCCC|DDDDDDDD|
3420 `--------+--------+--------+--------'
3422 The octets are divided into 6 bit chunks, which are then encoded into
3423 base64 characters. */
3425 #define ADVANCE_INPUT(c, stream) \
3426 ((ec = Lstream_get_emchar (stream)) == -1 ? 0 : \
3428 (signal_simple_error ("Non-ascii character in base64 input", \
3429 make_char (ec)), 0) \
3430 : (c = (Bufbyte)ec), 1))
3433 base64_encode_1 (Lstream *istream, Bufbyte *to, int line_break)
3435 EMACS_INT counter = 0;
3443 if (!ADVANCE_INPUT (c, istream))
3446 /* Wrap line every 76 characters. */
3449 if (counter < MIME_LINE_LENGTH / 4)
3458 /* Process first byte of a triplet. */
3459 *e++ = base64_value_to_char[0x3f & c >> 2];
3460 value = (0x03 & c) << 4;
3462 /* Process second byte of a triplet. */
3463 if (!ADVANCE_INPUT (c, istream))
3465 *e++ = base64_value_to_char[value];
3471 *e++ = base64_value_to_char[value | (0x0f & c >> 4)];
3472 value = (0x0f & c) << 2;
3474 /* Process third byte of a triplet. */
3475 if (!ADVANCE_INPUT (c, istream))
3477 *e++ = base64_value_to_char[value];
3482 *e++ = base64_value_to_char[value | (0x03 & c >> 6)];
3483 *e++ = base64_value_to_char[0x3f & c];
3488 #undef ADVANCE_INPUT
3490 /* Get next character from the stream, except that non-base64
3491 characters are ignored. This is in accordance with rfc2045. EC
3492 should be an Emchar, so that it can hold -1 as the value for EOF. */
3493 #define ADVANCE_INPUT_IGNORE_NONBASE64(ec, stream, streampos) do { \
3494 ec = Lstream_get_emchar (stream); \
3496 /* IS_BASE64 may not be called with negative arguments so check for \
3498 if (ec < 0 || IS_BASE64 (ec) || ec == '=') \
3502 #define STORE_BYTE(pos, val, ccnt) do { \
3503 pos += set_charptr_emchar (pos, (Emchar)((unsigned char)(val))); \
3508 base64_decode_1 (Lstream *istream, Bufbyte *to, Charcount *ccptr)
3512 EMACS_INT streampos = 0;
3517 unsigned long value;
3519 /* Process first byte of a quadruplet. */
3520 ADVANCE_INPUT_IGNORE_NONBASE64 (ec, istream, streampos);
3524 signal_simple_error ("Illegal `=' character while decoding base64",
3525 make_int (streampos));
3526 value = base64_char_to_value[ec] << 18;
3528 /* Process second byte of a quadruplet. */
3529 ADVANCE_INPUT_IGNORE_NONBASE64 (ec, istream, streampos);
3531 error ("Premature EOF while decoding base64");
3533 signal_simple_error ("Illegal `=' character while decoding base64",
3534 make_int (streampos));
3535 value |= base64_char_to_value[ec] << 12;
3536 STORE_BYTE (e, value >> 16, ccnt);
3538 /* Process third byte of a quadruplet. */
3539 ADVANCE_INPUT_IGNORE_NONBASE64 (ec, istream, streampos);
3541 error ("Premature EOF while decoding base64");
3545 ADVANCE_INPUT_IGNORE_NONBASE64 (ec, istream, streampos);
3547 error ("Premature EOF while decoding base64");
3549 signal_simple_error ("Padding `=' expected but not found while decoding base64",
3550 make_int (streampos));
3554 value |= base64_char_to_value[ec] << 6;
3555 STORE_BYTE (e, 0xff & value >> 8, ccnt);
3557 /* Process fourth byte of a quadruplet. */
3558 ADVANCE_INPUT_IGNORE_NONBASE64 (ec, istream, streampos);
3560 error ("Premature EOF while decoding base64");
3564 value |= base64_char_to_value[ec];
3565 STORE_BYTE (e, 0xff & value, ccnt);
3571 #undef ADVANCE_INPUT
3572 #undef ADVANCE_INPUT_IGNORE_NONBASE64
3576 free_malloced_ptr (Lisp_Object unwind_obj)
3578 void *ptr = (void *)get_opaque_ptr (unwind_obj);
3580 free_opaque_ptr (unwind_obj);
3584 /* Don't use alloca for regions larger than this, lest we overflow
3586 #define MAX_ALLOCA 65536
3588 /* We need to setup proper unwinding, because there is a number of
3589 ways these functions can blow up, and we don't want to have memory
3590 leaks in those cases. */
3591 #define XMALLOC_OR_ALLOCA(ptr, len, type) do { \
3592 size_t XOA_len = (len); \
3593 if (XOA_len > MAX_ALLOCA) \
3595 ptr = xnew_array (type, XOA_len); \
3596 record_unwind_protect (free_malloced_ptr, \
3597 make_opaque_ptr ((void *)ptr)); \
3600 ptr = alloca_array (type, XOA_len); \
3603 #define XMALLOC_UNBIND(ptr, len, speccount) do { \
3604 if ((len) > MAX_ALLOCA) \
3605 unbind_to (speccount, Qnil); \
3608 DEFUN ("base64-encode-region", Fbase64_encode_region, 2, 3, "r", /*
3609 Base64-encode the region between START and END.
3610 Return the length of the encoded text.
3611 Optional third argument NO-LINE-BREAK means do not break long lines
3614 (start, end, no_line_break))
3617 Bytind encoded_length;
3618 Charcount allength, length;
3619 struct buffer *buf = current_buffer;
3620 Bufpos begv, zv, old_pt = BUF_PT (buf);
3622 int speccount = specpdl_depth();
3624 get_buffer_range_char (buf, start, end, &begv, &zv, 0);
3625 barf_if_buffer_read_only (buf, begv, zv);
3627 /* We need to allocate enough room for encoding the text.
3628 We need 33 1/3% more space, plus a newline every 76
3629 characters, and then we round up. */
3631 allength = length + length/3 + 1;
3632 allength += allength / MIME_LINE_LENGTH + 1 + 6;
3634 input = make_lisp_buffer_input_stream (buf, begv, zv, 0);
3635 /* We needn't multiply allength with MAX_EMCHAR_LEN because all the
3636 base64 characters will be single-byte. */
3637 XMALLOC_OR_ALLOCA (encoded, allength, Bufbyte);
3638 encoded_length = base64_encode_1 (XLSTREAM (input), encoded,
3639 NILP (no_line_break));
3640 if (encoded_length > allength)
3642 Lstream_delete (XLSTREAM (input));
3644 /* Now we have encoded the region, so we insert the new contents
3645 and delete the old. (Insert first in order to preserve markers.) */
3646 buffer_insert_raw_string_1 (buf, begv, encoded, encoded_length, 0);
3647 XMALLOC_UNBIND (encoded, allength, speccount);
3648 buffer_delete_range (buf, begv + encoded_length, zv + encoded_length, 0);
3650 /* Simulate FSF Emacs implementation of this function: if point was
3651 in the region, place it at the beginning. */
3652 if (old_pt >= begv && old_pt < zv)
3653 BUF_SET_PT (buf, begv);
3655 /* We return the length of the encoded text. */
3656 return make_int (encoded_length);
3659 DEFUN ("base64-encode-string", Fbase64_encode_string, 1, 2, 0, /*
3660 Base64 encode STRING and return the result.
3661 Optional argument NO-LINE-BREAK means do not break long lines
3664 (string, no_line_break))
3666 Charcount allength, length;
3667 Bytind encoded_length;
3669 Lisp_Object input, result;
3670 int speccount = specpdl_depth();
3672 CHECK_STRING (string);
3674 length = XSTRING_CHAR_LENGTH (string);
3675 allength = length + length/3 + 1;
3676 allength += allength / MIME_LINE_LENGTH + 1 + 6;
3678 input = make_lisp_string_input_stream (string, 0, -1);
3679 XMALLOC_OR_ALLOCA (encoded, allength, Bufbyte);
3680 encoded_length = base64_encode_1 (XLSTREAM (input), encoded,
3681 NILP (no_line_break));
3682 if (encoded_length > allength)
3684 Lstream_delete (XLSTREAM (input));
3685 result = make_string (encoded, encoded_length);
3686 XMALLOC_UNBIND (encoded, allength, speccount);
3690 DEFUN ("base64-decode-region", Fbase64_decode_region, 2, 2, "r", /*
3691 Base64-decode the region between START and END.
3692 Return the length of the decoded text.
3693 If the region can't be decoded, return nil and don't modify the buffer.
3694 Characters out of the base64 alphabet are ignored.
3698 struct buffer *buf = current_buffer;
3699 Bufpos begv, zv, old_pt = BUF_PT (buf);
3701 Bytind decoded_length;
3702 Charcount length, cc_decoded_length;
3704 int speccount = specpdl_depth();
3706 get_buffer_range_char (buf, start, end, &begv, &zv, 0);
3707 barf_if_buffer_read_only (buf, begv, zv);
3711 input = make_lisp_buffer_input_stream (buf, begv, zv, 0);
3712 /* We need to allocate enough room for decoding the text. */
3713 XMALLOC_OR_ALLOCA (decoded, length * MAX_EMCHAR_LEN, Bufbyte);
3714 decoded_length = base64_decode_1 (XLSTREAM (input), decoded, &cc_decoded_length);
3715 if (decoded_length > length * MAX_EMCHAR_LEN)
3717 Lstream_delete (XLSTREAM (input));
3719 /* Now we have decoded the region, so we insert the new contents
3720 and delete the old. (Insert first in order to preserve markers.) */
3721 BUF_SET_PT (buf, begv);
3722 buffer_insert_raw_string_1 (buf, begv, decoded, decoded_length, 0);
3723 XMALLOC_UNBIND (decoded, length * MAX_EMCHAR_LEN, speccount);
3724 buffer_delete_range (buf, begv + cc_decoded_length,
3725 zv + cc_decoded_length, 0);
3727 /* Simulate FSF Emacs implementation of this function: if point was
3728 in the region, place it at the beginning. */
3729 if (old_pt >= begv && old_pt < zv)
3730 BUF_SET_PT (buf, begv);
3732 return make_int (cc_decoded_length);
3735 DEFUN ("base64-decode-string", Fbase64_decode_string, 1, 1, 0, /*
3736 Base64-decode STRING and return the result.
3737 Characters out of the base64 alphabet are ignored.
3742 Bytind decoded_length;
3743 Charcount length, cc_decoded_length;
3744 Lisp_Object input, result;
3745 int speccount = specpdl_depth();
3747 CHECK_STRING (string);
3749 length = XSTRING_CHAR_LENGTH (string);
3750 /* We need to allocate enough room for decoding the text. */
3751 XMALLOC_OR_ALLOCA (decoded, length * MAX_EMCHAR_LEN, Bufbyte);
3753 input = make_lisp_string_input_stream (string, 0, -1);
3754 decoded_length = base64_decode_1 (XLSTREAM (input), decoded,
3755 &cc_decoded_length);
3756 if (decoded_length > length * MAX_EMCHAR_LEN)
3758 Lstream_delete (XLSTREAM (input));
3760 result = make_string (decoded, decoded_length);
3761 XMALLOC_UNBIND (decoded, length * MAX_EMCHAR_LEN, speccount);
3765 Lisp_Object Qideographic_structure;
3766 Lisp_Object Qkeyword_char;
3768 EXFUN (Fideographic_structure_to_ids, 1);
3770 Lisp_Object ids_format_unit (Lisp_Object ids_char);
3772 ids_format_unit (Lisp_Object ids_char)
3774 if (CHARP (ids_char))
3775 return Fchar_to_string (ids_char);
3776 else if (INTP (ids_char))
3777 return Fchar_to_string (Fdecode_char (Qmap_ucs, ids_char, Qnil, Qnil));
3780 Lisp_Object ret = Ffind_char (ids_char);
3783 return Fchar_to_string (ret);
3786 ret = Fassq (Qideographic_structure, ids_char);
3789 return Fideographic_structure_to_ids (XCDR (ret));
3795 DEFUN ("ideographic-structure-to-ids",
3796 Fideographic_structure_to_ids, 1, 1, 0, /*
3797 Format ideographic-structure IDS-LIST as an IDS-string.
3801 Lisp_Object dest = Qnil;
3803 while (CONSP (ids_list))
3805 Lisp_Object cell = XCAR (ids_list);
3807 if (!NILP (Fchar_ref_p (cell)))
3808 cell = Fplist_get (cell, Qkeyword_char, Qnil);
3809 dest = concat2 (dest, ids_format_unit (cell));
3810 ids_list = XCDR (ids_list);
3815 Lisp_Object simplify_char_spec (Lisp_Object char_spec);
3817 simplify_char_spec (Lisp_Object char_spec)
3819 if (CHARP (char_spec))
3822 int code_point = ENCODE_CHAR (XCHAR (char_spec), ccs);
3824 if (code_point >= 0)
3826 int cid = decode_defined_char (ccs, code_point, Qnil);
3829 return make_char (cid);
3833 else if (INTP (char_spec))
3834 return Fdecode_char (Qmap_ucs, char_spec, Qnil, Qnil);
3838 Lisp_Object ret = Ffind_char (char_spec);
3841 Lisp_Object rest = char_spec;
3844 while (CONSP (rest))
3846 Lisp_Object cell = Fcar (rest);
3851 signal_simple_error ("Invalid argument", char_spec);
3853 if (!NILP (ccs = Ffind_charset (Fcar (cell))))
3857 ret = Fmake_char (ccs, Fcar (cell), Fcar (Fcdr (cell)));
3859 ret = Fdecode_char (ccs, cell, Qt, Qt);
3867 ret = Fdefine_char (char_spec);
3879 Lisp_Object char_ref_simplify_spec (Lisp_Object char_ref);
3881 char_ref_simplify_spec (Lisp_Object char_ref)
3883 if (!NILP (Fchar_ref_p (char_ref)))
3885 Lisp_Object ret = Fplist_get (char_ref, Qkeyword_char, Qnil);
3890 return Fplist_put (Fcopy_sequence (char_ref), Qkeyword_char,
3891 simplify_char_spec (ret));
3894 return simplify_char_spec (char_ref);
3897 DEFUN ("char-refs-simplify-char-specs",
3898 Fchar_refs_simplify_char_specs, 1, 1, 0, /*
3899 Simplify char-specs in CHAR-REFS.
3903 Lisp_Object rest = char_refs;
3905 while (CONSP (rest))
3907 Fsetcar (rest, char_ref_simplify_spec (XCAR (rest)));
3913 Lisp_Object Qyes_or_no_p;
3918 INIT_LRECORD_IMPLEMENTATION (bit_vector);
3920 defsymbol (&Qstring_lessp, "string-lessp");
3921 defsymbol (&Qidentity, "identity");
3922 defsymbol (&Qideographic_structure, "ideographic-structure");
3923 defsymbol (&Qkeyword_char, ":char");
3924 defsymbol (&Qyes_or_no_p, "yes-or-no-p");
3926 DEFSUBR (Fidentity);
3929 DEFSUBR (Fsafe_length);
3930 DEFSUBR (Fstring_equal);
3931 DEFSUBR (Fstring_lessp);
3932 DEFSUBR (Fstring_modified_tick);
3936 DEFSUBR (Fbvconcat);
3937 DEFSUBR (Fcopy_list);
3938 DEFSUBR (Fcopy_sequence);
3939 DEFSUBR (Fcopy_alist);
3940 DEFSUBR (Fcopy_tree);
3941 DEFSUBR (Fsubstring);
3948 DEFSUBR (Fnbutlast);
3950 DEFSUBR (Fold_member);
3952 DEFSUBR (Fold_memq);
3954 DEFSUBR (Fold_assoc);
3956 DEFSUBR (Fold_assq);
3958 DEFSUBR (Fold_rassoc);
3960 DEFSUBR (Fold_rassq);
3962 DEFSUBR (Fold_delete);
3964 DEFSUBR (Fold_delq);
3965 DEFSUBR (Fremassoc);
3967 DEFSUBR (Fremrassoc);
3968 DEFSUBR (Fremrassq);
3969 DEFSUBR (Fnreverse);
3972 DEFSUBR (Fplists_eq);
3973 DEFSUBR (Fplists_equal);
3974 DEFSUBR (Flax_plists_eq);
3975 DEFSUBR (Flax_plists_equal);
3976 DEFSUBR (Fplist_get);
3977 DEFSUBR (Fplist_put);
3978 DEFSUBR (Fplist_remprop);
3979 DEFSUBR (Fplist_member);
3980 DEFSUBR (Fcheck_valid_plist);
3981 DEFSUBR (Fvalid_plist_p);
3982 DEFSUBR (Fcanonicalize_plist);
3983 DEFSUBR (Flax_plist_get);
3984 DEFSUBR (Flax_plist_put);
3985 DEFSUBR (Flax_plist_remprop);
3986 DEFSUBR (Flax_plist_member);
3987 DEFSUBR (Fcanonicalize_lax_plist);
3988 DEFSUBR (Fdestructive_alist_to_plist);
3992 DEFSUBR (Fobject_plist);
3994 DEFSUBR (Fold_equal);
3995 DEFSUBR (Ffillarray);
3998 DEFSUBR (Fmapvector);
3999 DEFSUBR (Fmapc_internal);
4000 DEFSUBR (Fmapconcat);
4001 DEFSUBR (Freplace_list);
4002 DEFSUBR (Fload_average);
4003 DEFSUBR (Ffeaturep);
4006 DEFSUBR (Fbase64_encode_region);
4007 DEFSUBR (Fbase64_encode_string);
4008 DEFSUBR (Fbase64_decode_region);
4009 DEFSUBR (Fbase64_decode_string);
4010 DEFSUBR (Fideographic_structure_to_ids);
4011 DEFSUBR (Fchar_refs_simplify_char_specs);
4015 init_provide_once (void)
4017 DEFVAR_LISP ("features", &Vfeatures /*
4018 A list of symbols which are the features of the executing emacs.
4019 Used by `featurep' and `require', and altered by `provide'.
4023 Fprovide (intern ("base64"));