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.
5 This file is part of XEmacs.
7 XEmacs is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with XEmacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* Synched up with: Mule 2.0, FSF 19.30. */
24 /* This file has been Mule-ized. */
26 /* Note: FSF 19.30 has bool vectors. We have bit vectors. */
28 /* Hacked on for Mule by Ben Wing, December 1994, January 1995. */
32 /* Note on some machines this defines `vector' as a typedef,
33 so make sure we don't use that name in this file. */
52 /* NOTE: This symbol is also used in lread.c */
53 #define FEATUREP_SYNTAX
55 Lisp_Object Qstring_lessp;
56 Lisp_Object Qidentity;
58 static int internal_old_equal (Lisp_Object, Lisp_Object, int);
59 Lisp_Object safe_copy_tree (Lisp_Object arg, Lisp_Object vecp, int depth);
62 mark_bit_vector (Lisp_Object obj)
68 print_bit_vector (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
71 Lisp_Bit_Vector *v = XBIT_VECTOR (obj);
72 size_t len = bit_vector_length (v);
75 if (INTP (Vprint_length))
76 last = min (len, XINT (Vprint_length));
77 write_c_string ("#*", printcharfun);
78 for (i = 0; i < last; i++)
80 if (bit_vector_bit (v, i))
81 write_c_string ("1", printcharfun);
83 write_c_string ("0", printcharfun);
87 write_c_string ("...", printcharfun);
91 bit_vector_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
93 Lisp_Bit_Vector *v1 = XBIT_VECTOR (obj1);
94 Lisp_Bit_Vector *v2 = XBIT_VECTOR (obj2);
96 return ((bit_vector_length (v1) == bit_vector_length (v2)) &&
97 !memcmp (v1->bits, v2->bits,
98 BIT_VECTOR_LONG_STORAGE (bit_vector_length (v1)) *
103 bit_vector_hash (Lisp_Object obj, int depth)
105 Lisp_Bit_Vector *v = XBIT_VECTOR (obj);
106 return HASH2 (bit_vector_length (v),
107 memory_hash (v->bits,
108 BIT_VECTOR_LONG_STORAGE (bit_vector_length (v)) *
113 size_bit_vector (const void *lheader)
115 Lisp_Bit_Vector *v = (Lisp_Bit_Vector *) lheader;
116 return FLEXIBLE_ARRAY_STRUCT_SIZEOF (Lisp_Bit_Vector, unsigned long, bits,
117 BIT_VECTOR_LONG_STORAGE (bit_vector_length (v)));
120 static const struct lrecord_description bit_vector_description[] = {
121 { XD_LISP_OBJECT, offsetof (Lisp_Bit_Vector, next) },
126 DEFINE_BASIC_LRECORD_SEQUENCE_IMPLEMENTATION ("bit-vector", bit_vector,
127 mark_bit_vector, print_bit_vector, 0,
128 bit_vector_equal, bit_vector_hash,
129 bit_vector_description, size_bit_vector,
132 DEFUN ("identity", Fidentity, 1, 1, 0, /*
133 Return the argument unchanged.
140 extern long get_random (void);
141 extern void seed_random (long arg);
143 DEFUN ("random", Frandom, 0, 1, 0, /*
144 Return a pseudo-random number.
145 All integers representable in Lisp are equally likely.
146 On most systems, this is 28 bits' worth.
147 With positive integer argument N, return random number in interval [0,N).
148 With argument t, set the random number seed from the current time and pid.
153 unsigned long denominator;
156 seed_random (getpid () + time (NULL));
157 if (NATNUMP (limit) && !ZEROP (limit))
159 /* Try to take our random number from the higher bits of VAL,
160 not the lower, since (says Gentzel) the low bits of `random'
161 are less random than the higher ones. We do this by using the
162 quotient rather than the remainder. At the high end of the RNG
163 it's possible to get a quotient larger than limit; discarding
164 these values eliminates the bias that would otherwise appear
165 when using a large limit. */
166 denominator = ((unsigned long)1 << VALBITS) / XINT (limit);
168 val = get_random () / denominator;
169 while (val >= XINT (limit));
174 return make_int (val);
177 /* Random data-structure functions */
179 #ifdef LOSING_BYTECODE
181 /* #### Delete this shit */
183 /* Charcount is a misnomer here as we might be dealing with the
184 length of a vector or list, but emphasizes that we're not dealing
185 with Bytecounts in strings */
187 length_with_bytecode_hack (Lisp_Object seq)
189 if (!COMPILED_FUNCTIONP (seq))
190 return XINT (Flength (seq));
193 Lisp_Compiled_Function *f = XCOMPILED_FUNCTION (seq);
195 return (f->flags.interactivep ? COMPILED_INTERACTIVE :
196 f->flags.domainp ? COMPILED_DOMAIN :
202 #endif /* LOSING_BYTECODE */
205 check_losing_bytecode (const char *function, Lisp_Object seq)
207 if (COMPILED_FUNCTIONP (seq))
210 "As of 20.3, `%s' no longer works with compiled-function objects",
214 DEFUN ("length", Flength, 1, 1, 0, /*
215 Return the length of vector, bit vector, list or string SEQUENCE.
220 if (STRINGP (sequence))
221 return make_int (XSTRING_CHAR_LENGTH (sequence));
222 else if (CONSP (sequence))
225 GET_EXTERNAL_LIST_LENGTH (sequence, len);
226 return make_int (len);
228 else if (VECTORP (sequence))
229 return make_int (XVECTOR_LENGTH (sequence));
230 else if (NILP (sequence))
232 else if (BIT_VECTORP (sequence))
233 return make_int (bit_vector_length (XBIT_VECTOR (sequence)));
236 check_losing_bytecode ("length", sequence);
237 sequence = wrong_type_argument (Qsequencep, sequence);
242 DEFUN ("safe-length", Fsafe_length, 1, 1, 0, /*
243 Return the length of a list, but avoid error or infinite loop.
244 This function never gets an error. If LIST is not really a list,
245 it returns 0. If LIST is circular, it returns a finite value
246 which is at least the number of distinct elements.
250 Lisp_Object hare, tortoise;
253 for (hare = tortoise = list, len = 0;
254 CONSP (hare) && (! EQ (hare, tortoise) || len == 0);
255 hare = XCDR (hare), len++)
258 tortoise = XCDR (tortoise);
261 return make_int (len);
264 /*** string functions. ***/
266 DEFUN ("string-equal", Fstring_equal, 2, 2, 0, /*
267 Return t if two strings have identical contents.
268 Case is significant. Text properties are ignored.
269 \(Under XEmacs, `equal' also ignores text properties and extents in
270 strings, but this is not the case under FSF Emacs 19. In FSF Emacs 20
271 `equal' is the same as in XEmacs, in that respect.)
272 Symbols are also allowed; their print names are used instead.
277 Lisp_String *p1, *p2;
279 if (SYMBOLP (string1))
280 p1 = XSYMBOL (string1)->name;
283 CHECK_STRING (string1);
284 p1 = XSTRING (string1);
287 if (SYMBOLP (string2))
288 p2 = XSYMBOL (string2)->name;
291 CHECK_STRING (string2);
292 p2 = XSTRING (string2);
295 return (((len = string_length (p1)) == string_length (p2)) &&
296 !memcmp (string_data (p1), string_data (p2), len)) ? Qt : Qnil;
300 DEFUN ("string-lessp", Fstring_lessp, 2, 2, 0, /*
301 Return t if first arg string is less than second in lexicographic order.
302 If I18N2 support (but not Mule support) was compiled in, ordering is
303 determined by the locale. (Case is significant for the default C locale.)
304 In all other cases, comparison is simply done on a character-by-
305 character basis using the numeric value of a character. (Note that
306 this may not produce particularly meaningful results under Mule if
307 characters from different charsets are being compared.)
309 Symbols are also allowed; their print names are used instead.
311 The reason that the I18N2 locale-specific collation is not used under
312 Mule is that the locale model of internationalization does not handle
313 multiple charsets and thus has no hope of working properly under Mule.
314 What we really should do is create a collation table over all built-in
315 charsets. This is extremely difficult to do from scratch, however.
317 Unicode is a good first step towards solving this problem. In fact,
318 it is quite likely that a collation table exists (or will exist) for
319 Unicode. When Unicode support is added to XEmacs/Mule, this problem
324 Lisp_String *p1, *p2;
328 if (SYMBOLP (string1))
329 p1 = XSYMBOL (string1)->name;
332 CHECK_STRING (string1);
333 p1 = XSTRING (string1);
336 if (SYMBOLP (string2))
337 p2 = XSYMBOL (string2)->name;
340 CHECK_STRING (string2);
341 p2 = XSTRING (string2);
344 end = string_char_length (p1);
345 len2 = string_char_length (p2);
349 #if defined (I18N2) && !defined (MULE)
350 /* There is no hope of this working under Mule. Even if we converted
351 the data into an external format so that strcoll() processed it
352 properly, it would still not work because strcoll() does not
353 handle multiple locales. This is the fundamental flaw in the
356 Bytecount bcend = charcount_to_bytecount (string_data (p1), end);
357 /* Compare strings using collation order of locale. */
358 /* Need to be tricky to handle embedded nulls. */
360 for (i = 0; i < bcend; i += strlen((char *) string_data (p1) + i) + 1)
362 int val = strcoll ((char *) string_data (p1) + i,
363 (char *) string_data (p2) + i);
370 #else /* not I18N2, or MULE */
372 Bufbyte *ptr1 = string_data (p1);
373 Bufbyte *ptr2 = string_data (p2);
375 /* #### It is not really necessary to do this: We could compare
376 byte-by-byte and still get a reasonable comparison, since this
377 would compare characters with a charset in the same way. With
378 a little rearrangement of the leading bytes, we could make most
379 inter-charset comparisons work out the same, too; even if some
380 don't, this is not a big deal because inter-charset comparisons
381 aren't really well-defined anyway. */
382 for (i = 0; i < end; i++)
384 if (charptr_emchar (ptr1) != charptr_emchar (ptr2))
385 return charptr_emchar (ptr1) < charptr_emchar (ptr2) ? Qt : Qnil;
390 #endif /* not I18N2, or MULE */
391 /* Can't do i < len2 because then comparison between "foo" and "foo^@"
392 won't work right in I18N2 case */
393 return end < len2 ? Qt : Qnil;
396 DEFUN ("string-modified-tick", Fstring_modified_tick, 1, 1, 0, /*
397 Return STRING's tick counter, incremented for each change to the string.
398 Each string has a tick counter which is incremented each time the contents
399 of the string are changed (e.g. with `aset'). It wraps around occasionally.
405 CHECK_STRING (string);
406 s = XSTRING (string);
407 if (CONSP (s->plist) && INTP (XCAR (s->plist)))
408 return XCAR (s->plist);
414 bump_string_modiff (Lisp_Object str)
416 Lisp_String *s = XSTRING (str);
417 Lisp_Object *ptr = &s->plist;
420 /* #### remove the `string-translatable' property from the string,
423 /* skip over extent info if it's there */
424 if (CONSP (*ptr) && EXTENT_INFOP (XCAR (*ptr)))
426 if (CONSP (*ptr) && INTP (XCAR (*ptr)))
427 XSETINT (XCAR (*ptr), 1+XINT (XCAR (*ptr)));
429 *ptr = Fcons (make_int (1), *ptr);
433 enum concat_target_type { c_cons, c_string, c_vector, c_bit_vector };
434 static Lisp_Object concat (int nargs, Lisp_Object *args,
435 enum concat_target_type target_type,
439 concat2 (Lisp_Object string1, Lisp_Object string2)
444 return concat (2, args, c_string, 0);
448 concat3 (Lisp_Object string1, Lisp_Object string2, Lisp_Object string3)
454 return concat (3, args, c_string, 0);
458 vconcat2 (Lisp_Object vec1, Lisp_Object vec2)
463 return concat (2, args, c_vector, 0);
467 vconcat3 (Lisp_Object vec1, Lisp_Object vec2, Lisp_Object vec3)
473 return concat (3, args, c_vector, 0);
476 DEFUN ("append", Fappend, 0, MANY, 0, /*
477 Concatenate all the arguments and make the result a list.
478 The result is a list whose elements are the elements of all the arguments.
479 Each argument may be a list, vector, bit vector, or string.
480 The last argument is not copied, just used as the tail of the new list.
483 (int nargs, Lisp_Object *args))
485 return concat (nargs, args, c_cons, 1);
488 DEFUN ("concat", Fconcat, 0, MANY, 0, /*
489 Concatenate all the arguments and make the result a string.
490 The result is a string whose elements are the elements of all the arguments.
491 Each argument may be a string or a list or vector of characters.
493 As of XEmacs 21.0, this function does NOT accept individual integers
494 as arguments. Old code that relies on, for example, (concat "foo" 50)
495 returning "foo50" will fail. To fix such code, either apply
496 `int-to-string' to the integer argument, or use `format'.
498 (int nargs, Lisp_Object *args))
500 return concat (nargs, args, c_string, 0);
503 DEFUN ("vconcat", Fvconcat, 0, MANY, 0, /*
504 Concatenate all the arguments and make the result a vector.
505 The result is a vector whose elements are the elements of all the arguments.
506 Each argument may be a list, vector, bit vector, or string.
508 (int nargs, Lisp_Object *args))
510 return concat (nargs, args, c_vector, 0);
513 DEFUN ("bvconcat", Fbvconcat, 0, MANY, 0, /*
514 Concatenate all the arguments and make the result a bit vector.
515 The result is a bit vector whose elements are the elements of all the
516 arguments. Each argument may be a list, vector, bit vector, or string.
518 (int nargs, Lisp_Object *args))
520 return concat (nargs, args, c_bit_vector, 0);
523 /* Copy a (possibly dotted) list. LIST must be a cons.
524 Can't use concat (1, &alist, c_cons, 0) - doesn't handle dotted lists. */
526 copy_list (Lisp_Object list)
528 Lisp_Object list_copy = Fcons (XCAR (list), XCDR (list));
529 Lisp_Object last = list_copy;
530 Lisp_Object hare, tortoise;
533 for (tortoise = hare = XCDR (list), len = 1;
535 hare = XCDR (hare), len++)
537 XCDR (last) = Fcons (XCAR (hare), XCDR (hare));
540 if (len < CIRCULAR_LIST_SUSPICION_LENGTH)
543 tortoise = XCDR (tortoise);
544 if (EQ (tortoise, hare))
545 signal_circular_list_error (list);
551 DEFUN ("copy-list", Fcopy_list, 1, 1, 0, /*
552 Return a copy of list LIST, which may be a dotted list.
553 The elements of LIST are not copied; they are shared
559 if (NILP (list)) return list;
560 if (CONSP (list)) return copy_list (list);
562 list = wrong_type_argument (Qlistp, list);
566 DEFUN ("copy-sequence", Fcopy_sequence, 1, 1, 0, /*
567 Return a copy of list, vector, bit vector or string SEQUENCE.
568 The elements of a list or vector are not copied; they are shared
569 with the original. SEQUENCE may be a dotted list.
574 if (NILP (sequence)) return sequence;
575 if (CONSP (sequence)) return copy_list (sequence);
576 if (STRINGP (sequence)) return concat (1, &sequence, c_string, 0);
577 if (VECTORP (sequence)) return concat (1, &sequence, c_vector, 0);
578 if (BIT_VECTORP (sequence)) return concat (1, &sequence, c_bit_vector, 0);
580 check_losing_bytecode ("copy-sequence", sequence);
581 sequence = wrong_type_argument (Qsequencep, sequence);
585 struct merge_string_extents_struct
588 Bytecount entry_offset;
589 Bytecount entry_length;
593 concat (int nargs, Lisp_Object *args,
594 enum concat_target_type target_type,
598 Lisp_Object tail = Qnil;
601 Lisp_Object last_tail;
603 struct merge_string_extents_struct *args_mse = 0;
604 Bufbyte *string_result = 0;
605 Bufbyte *string_result_ptr = 0;
608 /* The modus operandi in Emacs is "caller gc-protects args".
609 However, concat is called many times in Emacs on freshly
610 created stuff. So we help those callers out by protecting
611 the args ourselves to save them a lot of temporary-variable
615 gcpro1.nvars = nargs;
618 /* #### if the result is a string and any of the strings have a string
619 for the `string-translatable' property, then concat should also
620 concat the args but use the `string-translatable' strings, and store
621 the result in the returned string's `string-translatable' property. */
623 if (target_type == c_string)
624 args_mse = alloca_array (struct merge_string_extents_struct, nargs);
626 /* In append, the last arg isn't treated like the others */
627 if (last_special && nargs > 0)
630 last_tail = args[nargs];
635 /* Check and coerce the arguments. */
636 for (argnum = 0; argnum < nargs; argnum++)
638 Lisp_Object seq = args[argnum];
641 else if (VECTORP (seq) || STRINGP (seq) || BIT_VECTORP (seq))
643 #ifdef LOSING_BYTECODE
644 else if (COMPILED_FUNCTIONP (seq))
645 /* Urk! We allow this, for "compatibility"... */
648 #if 0 /* removed for XEmacs 21 */
650 /* This is too revolting to think about but maintains
651 compatibility with FSF (and lots and lots of old code). */
652 args[argnum] = Fnumber_to_string (seq);
656 check_losing_bytecode ("concat", seq);
657 args[argnum] = wrong_type_argument (Qsequencep, seq);
663 args_mse[argnum].string = seq;
665 args_mse[argnum].string = Qnil;
670 /* Charcount is a misnomer here as we might be dealing with the
671 length of a vector or list, but emphasizes that we're not dealing
672 with Bytecounts in strings */
673 Charcount total_length;
675 for (argnum = 0, total_length = 0; argnum < nargs; argnum++)
677 #ifdef LOSING_BYTECODE
678 Charcount thislen = length_with_bytecode_hack (args[argnum]);
680 Charcount thislen = XINT (Flength (args[argnum]));
682 total_length += thislen;
688 if (total_length == 0)
689 /* In append, if all but last arg are nil, return last arg */
690 RETURN_UNGCPRO (last_tail);
691 val = Fmake_list (make_int (total_length), Qnil);
694 val = make_vector (total_length, Qnil);
697 val = make_bit_vector (total_length, Qzero);
700 /* We don't make the string yet because we don't know the
701 actual number of bytes. This loop was formerly written
702 to call Fmake_string() here and then call set_string_char()
703 for each char. This seems logical enough but is waaaaaaaay
704 slow -- set_string_char() has to scan the whole string up
705 to the place where the substitution is called for in order
706 to find the place to change, and may have to do some
707 realloc()ing in order to make the char fit properly.
710 string_result = (Bufbyte *) alloca (total_length * MAX_EMCHAR_LEN);
711 string_result_ptr = string_result;
721 tail = val, toindex = -1; /* -1 in toindex is flag we are
728 for (argnum = 0; argnum < nargs; argnum++)
730 Charcount thisleni = 0;
731 Charcount thisindex = 0;
732 Lisp_Object seq = args[argnum];
733 Bufbyte *string_source_ptr = 0;
734 Bufbyte *string_prev_result_ptr = string_result_ptr;
738 #ifdef LOSING_BYTECODE
739 thisleni = length_with_bytecode_hack (seq);
741 thisleni = XINT (Flength (seq));
745 string_source_ptr = XSTRING_DATA (seq);
751 /* We've come to the end of this arg, so exit. */
755 /* Fetch next element of `seq' arg into `elt' */
763 if (thisindex >= thisleni)
768 elt = make_char (charptr_emchar (string_source_ptr));
769 INC_CHARPTR (string_source_ptr);
771 else if (VECTORP (seq))
772 elt = XVECTOR_DATA (seq)[thisindex];
773 else if (BIT_VECTORP (seq))
774 elt = make_int (bit_vector_bit (XBIT_VECTOR (seq),
777 elt = Felt (seq, make_int (thisindex));
781 /* Store into result */
784 /* toindex negative means we are making a list */
789 else if (VECTORP (val))
790 XVECTOR_DATA (val)[toindex++] = elt;
791 else if (BIT_VECTORP (val))
794 set_bit_vector_bit (XBIT_VECTOR (val), toindex++, XINT (elt));
798 CHECK_CHAR_COERCE_INT (elt);
799 string_result_ptr += set_charptr_emchar (string_result_ptr,
805 args_mse[argnum].entry_offset =
806 string_prev_result_ptr - string_result;
807 args_mse[argnum].entry_length =
808 string_result_ptr - string_prev_result_ptr;
812 /* Now we finally make the string. */
813 if (target_type == c_string)
815 val = make_string (string_result, string_result_ptr - string_result);
816 for (argnum = 0; argnum < nargs; argnum++)
818 if (STRINGP (args_mse[argnum].string))
819 copy_string_extents (val, args_mse[argnum].string,
820 args_mse[argnum].entry_offset, 0,
821 args_mse[argnum].entry_length);
826 XCDR (prev) = last_tail;
828 RETURN_UNGCPRO (val);
831 DEFUN ("copy-alist", Fcopy_alist, 1, 1, 0, /*
832 Return a copy of ALIST.
833 This is an alist which represents the same mapping from objects to objects,
834 but does not share the alist structure with ALIST.
835 The objects mapped (cars and cdrs of elements of the alist)
837 Elements of ALIST that are not conses are also shared.
847 alist = concat (1, &alist, c_cons, 0);
848 for (tail = alist; CONSP (tail); tail = XCDR (tail))
850 Lisp_Object car = XCAR (tail);
853 XCAR (tail) = Fcons (XCAR (car), XCDR (car));
858 DEFUN ("copy-tree", Fcopy_tree, 1, 2, 0, /*
859 Return a copy of a list and substructures.
860 The argument is copied, and any lists contained within it are copied
861 recursively. Circularities and shared substructures are not preserved.
862 Second arg VECP causes vectors to be copied, too. Strings and bit vectors
867 return safe_copy_tree (arg, vecp, 0);
871 safe_copy_tree (Lisp_Object arg, Lisp_Object vecp, int depth)
874 signal_simple_error ("Stack overflow in copy-tree", arg);
879 rest = arg = Fcopy_sequence (arg);
882 Lisp_Object elt = XCAR (rest);
884 if (CONSP (elt) || VECTORP (elt))
885 XCAR (rest) = safe_copy_tree (elt, vecp, depth + 1);
886 if (VECTORP (XCDR (rest))) /* hack for (a b . [c d]) */
887 XCDR (rest) = safe_copy_tree (XCDR (rest), vecp, depth +1);
891 else if (VECTORP (arg) && ! NILP (vecp))
893 int i = XVECTOR_LENGTH (arg);
895 arg = Fcopy_sequence (arg);
896 for (j = 0; j < i; j++)
898 Lisp_Object elt = XVECTOR_DATA (arg) [j];
900 if (CONSP (elt) || VECTORP (elt))
901 XVECTOR_DATA (arg) [j] = safe_copy_tree (elt, vecp, depth + 1);
907 DEFUN ("substring", Fsubstring, 2, 3, 0, /*
908 Return the substring of STRING starting at START and ending before END.
909 END may be nil or omitted; then the substring runs to the end of STRING.
910 If START or END is negative, it counts from the end.
911 Relevant parts of the string-extent-data are copied to the new string.
913 (string, start, end))
915 Charcount ccstart, ccend;
916 Bytecount bstart, blen;
919 CHECK_STRING (string);
921 get_string_range_char (string, start, end, &ccstart, &ccend,
922 GB_HISTORICAL_STRING_BEHAVIOR);
923 bstart = charcount_to_bytecount (XSTRING_DATA (string), ccstart);
924 blen = charcount_to_bytecount (XSTRING_DATA (string) + bstart, ccend - ccstart);
925 val = make_string (XSTRING_DATA (string) + bstart, blen);
926 /* Copy any applicable extent information into the new string. */
927 copy_string_extents (val, string, 0, bstart, blen);
931 DEFUN ("subseq", Fsubseq, 2, 3, 0, /*
932 Return the subsequence of SEQUENCE starting at START and ending before END.
933 END may be omitted; then the subsequence runs to the end of SEQUENCE.
934 If START or END is negative, it counts from the end.
935 The returned subsequence is always of the same type as SEQUENCE.
936 If SEQUENCE is a string, relevant parts of the string-extent-data
937 are copied to the new string.
939 (sequence, start, end))
943 if (STRINGP (sequence))
944 return Fsubstring (sequence, start, end);
946 len = XINT (Flength (sequence));
963 if (!(0 <= s && s <= e && e <= len))
964 args_out_of_range_3 (sequence, make_int (s), make_int (e));
966 if (VECTORP (sequence))
968 Lisp_Object result = make_vector (e - s, Qnil);
970 Lisp_Object *in_elts = XVECTOR_DATA (sequence);
971 Lisp_Object *out_elts = XVECTOR_DATA (result);
973 for (i = s; i < e; i++)
974 out_elts[i - s] = in_elts[i];
977 else if (LISTP (sequence))
979 Lisp_Object result = Qnil;
982 sequence = Fnthcdr (make_int (s), sequence);
984 for (i = s; i < e; i++)
986 result = Fcons (Fcar (sequence), result);
987 sequence = Fcdr (sequence);
990 return Fnreverse (result);
992 else if (BIT_VECTORP (sequence))
994 Lisp_Object result = make_bit_vector (e - s, Qzero);
997 for (i = s; i < e; i++)
998 set_bit_vector_bit (XBIT_VECTOR (result), i - s,
999 bit_vector_bit (XBIT_VECTOR (sequence), i));
1004 abort (); /* unreachable, since Flength (sequence) did not get
1011 DEFUN ("nthcdr", Fnthcdr, 2, 2, 0, /*
1012 Take cdr N times on LIST, and return the result.
1017 REGISTER Lisp_Object tail = list;
1019 for (i = XINT (n); i; i--)
1023 else if (NILP (tail))
1027 tail = wrong_type_argument (Qlistp, tail);
1034 DEFUN ("nth", Fnth, 2, 2, 0, /*
1035 Return the Nth element of LIST.
1036 N counts from zero. If LIST is not that long, nil is returned.
1040 return Fcar (Fnthcdr (n, list));
1043 DEFUN ("elt", Felt, 2, 2, 0, /*
1044 Return element of SEQUENCE at index N.
1049 CHECK_INT_COERCE_CHAR (n); /* yuck! */
1050 if (LISTP (sequence))
1052 Lisp_Object tem = Fnthcdr (n, sequence);
1053 /* #### Utterly, completely, fucking disgusting.
1054 * #### The whole point of "elt" is that it operates on
1055 * #### sequences, and does error- (bounds-) checking.
1061 /* This is The Way It Has Always Been. */
1064 /* This is The Way Mly and Cltl2 say It Should Be. */
1065 args_out_of_range (sequence, n);
1068 else if (STRINGP (sequence) ||
1069 VECTORP (sequence) ||
1070 BIT_VECTORP (sequence))
1071 return Faref (sequence, n);
1072 #ifdef LOSING_BYTECODE
1073 else if (COMPILED_FUNCTIONP (sequence))
1075 EMACS_INT idx = XINT (n);
1079 args_out_of_range (sequence, n);
1081 /* Utter perversity */
1083 Lisp_Compiled_Function *f = XCOMPILED_FUNCTION (sequence);
1086 case COMPILED_ARGLIST:
1087 return compiled_function_arglist (f);
1088 case COMPILED_INSTRUCTIONS:
1089 return compiled_function_instructions (f);
1090 case COMPILED_CONSTANTS:
1091 return compiled_function_constants (f);
1092 case COMPILED_STACK_DEPTH:
1093 return compiled_function_stack_depth (f);
1094 case COMPILED_DOC_STRING:
1095 return compiled_function_documentation (f);
1096 case COMPILED_DOMAIN:
1097 return compiled_function_domain (f);
1098 case COMPILED_INTERACTIVE:
1099 if (f->flags.interactivep)
1100 return compiled_function_interactive (f);
1101 /* if we return nil, can't tell interactive with no args
1102 from noninteractive. */
1109 #endif /* LOSING_BYTECODE */
1112 check_losing_bytecode ("elt", sequence);
1113 sequence = wrong_type_argument (Qsequencep, sequence);
1118 DEFUN ("last", Flast, 1, 2, 0, /*
1119 Return the tail of list LIST, of length N (default 1).
1120 LIST may be a dotted list, but not a circular list.
1121 Optional argument N must be a non-negative integer.
1122 If N is zero, then the atom that terminates the list is returned.
1123 If N is greater than the length of LIST, then LIST itself is returned.
1127 EMACS_INT int_n, count;
1128 Lisp_Object retval, tortoise, hare;
1140 for (retval = tortoise = hare = list, count = 0;
1143 (int_n-- <= 0 ? ((void) (retval = XCDR (retval))) : (void)0),
1146 if (count < CIRCULAR_LIST_SUSPICION_LENGTH) continue;
1149 tortoise = XCDR (tortoise);
1150 if (EQ (hare, tortoise))
1151 signal_circular_list_error (list);
1157 DEFUN ("nbutlast", Fnbutlast, 1, 2, 0, /*
1158 Modify LIST to remove the last N (default 1) elements.
1159 If LIST has N or fewer elements, nil is returned and LIST is unmodified.
1176 Lisp_Object last_cons = list;
1178 EXTERNAL_LIST_LOOP_1 (list)
1181 last_cons = XCDR (last_cons);
1187 XCDR (last_cons) = Qnil;
1192 DEFUN ("butlast", Fbutlast, 1, 2, 0, /*
1193 Return a copy of LIST with the last N (default 1) elements removed.
1194 If LIST has N or fewer elements, nil is returned.
1211 Lisp_Object retval = Qnil;
1212 Lisp_Object tail = list;
1214 EXTERNAL_LIST_LOOP_1 (list)
1218 retval = Fcons (XCAR (tail), retval);
1223 return Fnreverse (retval);
1227 DEFUN ("member", Fmember, 2, 2, 0, /*
1228 Return non-nil if ELT is an element of LIST. Comparison done with `equal'.
1229 The value is actually the tail of LIST whose car is ELT.
1233 EXTERNAL_LIST_LOOP_3 (list_elt, list, tail)
1235 if (internal_equal (elt, list_elt, 0))
1241 DEFUN ("old-member", Fold_member, 2, 2, 0, /*
1242 Return non-nil if ELT is an element of LIST. Comparison done with `old-equal'.
1243 The value is actually the tail of LIST whose car is ELT.
1244 This function is provided only for byte-code compatibility with v19.
1249 EXTERNAL_LIST_LOOP_3 (list_elt, list, tail)
1251 if (internal_old_equal (elt, list_elt, 0))
1257 DEFUN ("memq", Fmemq, 2, 2, 0, /*
1258 Return non-nil if ELT is an element of LIST. Comparison done with `eq'.
1259 The value is actually the tail of LIST whose car is ELT.
1263 EXTERNAL_LIST_LOOP_3 (list_elt, list, tail)
1265 if (EQ_WITH_EBOLA_NOTICE (elt, list_elt))
1271 DEFUN ("old-memq", Fold_memq, 2, 2, 0, /*
1272 Return non-nil if ELT is an element of LIST. Comparison done with `old-eq'.
1273 The value is actually the tail of LIST whose car is ELT.
1274 This function is provided only for byte-code compatibility with v19.
1279 EXTERNAL_LIST_LOOP_3 (list_elt, list, tail)
1281 if (HACKEQ_UNSAFE (elt, list_elt))
1288 memq_no_quit (Lisp_Object elt, Lisp_Object list)
1290 LIST_LOOP_3 (list_elt, list, tail)
1292 if (EQ_WITH_EBOLA_NOTICE (elt, list_elt))
1298 DEFUN ("assoc", Fassoc, 2, 2, 0, /*
1299 Return non-nil if KEY is `equal' to the car of an element of ALIST.
1300 The value is actually the element of ALIST whose car equals KEY.
1304 /* This function can GC. */
1305 EXTERNAL_ALIST_LOOP_4 (elt, elt_car, elt_cdr, alist)
1307 if (internal_equal (key, elt_car, 0))
1313 DEFUN ("old-assoc", Fold_assoc, 2, 2, 0, /*
1314 Return non-nil if KEY is `old-equal' to the car of an element of ALIST.
1315 The value is actually the element of ALIST whose car equals KEY.
1319 /* This function can GC. */
1320 EXTERNAL_ALIST_LOOP_4 (elt, elt_car, elt_cdr, alist)
1322 if (internal_old_equal (key, elt_car, 0))
1329 assoc_no_quit (Lisp_Object key, Lisp_Object alist)
1331 int speccount = specpdl_depth ();
1332 specbind (Qinhibit_quit, Qt);
1333 return unbind_to (speccount, Fassoc (key, alist));
1336 DEFUN ("assq", Fassq, 2, 2, 0, /*
1337 Return non-nil if KEY is `eq' to the car of an element of ALIST.
1338 The value is actually the element of ALIST whose car is KEY.
1339 Elements of ALIST that are not conses are ignored.
1343 EXTERNAL_ALIST_LOOP_4 (elt, elt_car, elt_cdr, alist)
1345 if (EQ_WITH_EBOLA_NOTICE (key, elt_car))
1351 DEFUN ("old-assq", Fold_assq, 2, 2, 0, /*
1352 Return non-nil if KEY is `old-eq' to the car of an element of ALIST.
1353 The value is actually the element of ALIST whose car is KEY.
1354 Elements of ALIST that are not conses are ignored.
1355 This function is provided only for byte-code compatibility with v19.
1360 EXTERNAL_ALIST_LOOP_4 (elt, elt_car, elt_cdr, alist)
1362 if (HACKEQ_UNSAFE (key, elt_car))
1368 /* Like Fassq but never report an error and do not allow quits.
1369 Use only on lists known never to be circular. */
1372 assq_no_quit (Lisp_Object key, Lisp_Object alist)
1374 /* This cannot GC. */
1375 LIST_LOOP_2 (elt, alist)
1377 Lisp_Object elt_car = XCAR (elt);
1378 if (EQ_WITH_EBOLA_NOTICE (key, elt_car))
1384 DEFUN ("rassoc", Frassoc, 2, 2, 0, /*
1385 Return non-nil if VALUE is `equal' to the cdr of an element of ALIST.
1386 The value is actually the element of ALIST whose cdr equals VALUE.
1390 EXTERNAL_ALIST_LOOP_4 (elt, elt_car, elt_cdr, alist)
1392 if (internal_equal (value, elt_cdr, 0))
1398 DEFUN ("old-rassoc", Fold_rassoc, 2, 2, 0, /*
1399 Return non-nil if VALUE is `old-equal' to the cdr of an element of ALIST.
1400 The value is actually the element of ALIST whose cdr equals VALUE.
1404 EXTERNAL_ALIST_LOOP_4 (elt, elt_car, elt_cdr, alist)
1406 if (internal_old_equal (value, elt_cdr, 0))
1412 DEFUN ("rassq", Frassq, 2, 2, 0, /*
1413 Return non-nil if VALUE is `eq' to the cdr of an element of ALIST.
1414 The value is actually the element of ALIST whose cdr is VALUE.
1418 EXTERNAL_ALIST_LOOP_4 (elt, elt_car, elt_cdr, alist)
1420 if (EQ_WITH_EBOLA_NOTICE (value, elt_cdr))
1426 DEFUN ("old-rassq", Fold_rassq, 2, 2, 0, /*
1427 Return non-nil if VALUE is `old-eq' to the cdr of an element of ALIST.
1428 The value is actually the element of ALIST whose cdr is VALUE.
1432 EXTERNAL_ALIST_LOOP_4 (elt, elt_car, elt_cdr, alist)
1434 if (HACKEQ_UNSAFE (value, elt_cdr))
1440 /* Like Frassq, but caller must ensure that ALIST is properly
1441 nil-terminated and ebola-free. */
1443 rassq_no_quit (Lisp_Object value, Lisp_Object alist)
1445 LIST_LOOP_2 (elt, alist)
1447 Lisp_Object elt_cdr = XCDR (elt);
1448 if (EQ_WITH_EBOLA_NOTICE (value, elt_cdr))
1455 DEFUN ("delete", Fdelete, 2, 2, 0, /*
1456 Delete by side effect any occurrences of ELT as a member of LIST.
1457 The modified LIST is returned. Comparison is done with `equal'.
1458 If the first member of LIST is ELT, there is no way to remove it by side
1459 effect; therefore, write `(setq foo (delete element foo))' to be sure
1460 of changing the value of `foo'.
1465 EXTERNAL_LIST_LOOP_DELETE_IF (list_elt, list,
1466 (internal_equal (elt, list_elt, 0)));
1470 DEFUN ("old-delete", Fold_delete, 2, 2, 0, /*
1471 Delete by side effect any occurrences of ELT as a member of LIST.
1472 The modified LIST is returned. Comparison is done with `old-equal'.
1473 If the first member of LIST is ELT, there is no way to remove it by side
1474 effect; therefore, write `(setq foo (old-delete element foo))' to be sure
1475 of changing the value of `foo'.
1479 EXTERNAL_LIST_LOOP_DELETE_IF (list_elt, list,
1480 (internal_old_equal (elt, list_elt, 0)));
1484 DEFUN ("delq", Fdelq, 2, 2, 0, /*
1485 Delete by side effect any occurrences of ELT as a member of LIST.
1486 The modified LIST is returned. Comparison is done with `eq'.
1487 If the first member of LIST is ELT, there is no way to remove it by side
1488 effect; therefore, write `(setq foo (delq element foo))' to be sure of
1489 changing the value of `foo'.
1493 EXTERNAL_LIST_LOOP_DELETE_IF (list_elt, list,
1494 (EQ_WITH_EBOLA_NOTICE (elt, list_elt)));
1498 DEFUN ("old-delq", Fold_delq, 2, 2, 0, /*
1499 Delete by side effect any occurrences of ELT as a member of LIST.
1500 The modified LIST is returned. Comparison is done with `old-eq'.
1501 If the first member of LIST is ELT, there is no way to remove it by side
1502 effect; therefore, write `(setq foo (old-delq element foo))' to be sure of
1503 changing the value of `foo'.
1507 EXTERNAL_LIST_LOOP_DELETE_IF (list_elt, list,
1508 (HACKEQ_UNSAFE (elt, list_elt)));
1512 /* Like Fdelq, but caller must ensure that LIST is properly
1513 nil-terminated and ebola-free. */
1516 delq_no_quit (Lisp_Object elt, Lisp_Object list)
1518 LIST_LOOP_DELETE_IF (list_elt, list,
1519 (EQ_WITH_EBOLA_NOTICE (elt, list_elt)));
1523 /* Be VERY careful with this. This is like delq_no_quit() but
1524 also calls free_cons() on the removed conses. You must be SURE
1525 that no pointers to the freed conses remain around (e.g.
1526 someone else is pointing to part of the list). This function
1527 is useful on internal lists that are used frequently and where
1528 the actual list doesn't escape beyond known code bounds. */
1531 delq_no_quit_and_free_cons (Lisp_Object elt, Lisp_Object list)
1533 REGISTER Lisp_Object tail = list;
1534 REGISTER Lisp_Object prev = Qnil;
1536 while (!NILP (tail))
1538 REGISTER Lisp_Object tem = XCAR (tail);
1541 Lisp_Object cons_to_free = tail;
1545 XCDR (prev) = XCDR (tail);
1547 free_cons (XCONS (cons_to_free));
1558 DEFUN ("remassoc", Fremassoc, 2, 2, 0, /*
1559 Delete by side effect any elements of ALIST whose car is `equal' to KEY.
1560 The modified ALIST is returned. If the first member of ALIST has a car
1561 that is `equal' to KEY, there is no way to remove it by side effect;
1562 therefore, write `(setq foo (remassoc key foo))' to be sure of changing
1567 EXTERNAL_LIST_LOOP_DELETE_IF (elt, alist,
1569 internal_equal (key, XCAR (elt), 0)));
1574 remassoc_no_quit (Lisp_Object key, Lisp_Object alist)
1576 int speccount = specpdl_depth ();
1577 specbind (Qinhibit_quit, Qt);
1578 return unbind_to (speccount, Fremassoc (key, alist));
1581 DEFUN ("remassq", Fremassq, 2, 2, 0, /*
1582 Delete by side effect any elements of ALIST whose car is `eq' to KEY.
1583 The modified ALIST is returned. If the first member of ALIST has a car
1584 that is `eq' to KEY, there is no way to remove it by side effect;
1585 therefore, write `(setq foo (remassq key foo))' to be sure of changing
1590 EXTERNAL_LIST_LOOP_DELETE_IF (elt, alist,
1592 EQ_WITH_EBOLA_NOTICE (key, XCAR (elt))));
1596 /* no quit, no errors; be careful */
1599 remassq_no_quit (Lisp_Object key, Lisp_Object alist)
1601 LIST_LOOP_DELETE_IF (elt, alist,
1603 EQ_WITH_EBOLA_NOTICE (key, XCAR (elt))));
1607 DEFUN ("remrassoc", Fremrassoc, 2, 2, 0, /*
1608 Delete by side effect any elements of ALIST whose cdr is `equal' to VALUE.
1609 The modified ALIST is returned. If the first member of ALIST has a car
1610 that is `equal' to VALUE, there is no way to remove it by side effect;
1611 therefore, write `(setq foo (remrassoc value foo))' to be sure of changing
1616 EXTERNAL_LIST_LOOP_DELETE_IF (elt, alist,
1618 internal_equal (value, XCDR (elt), 0)));
1622 DEFUN ("remrassq", Fremrassq, 2, 2, 0, /*
1623 Delete by side effect any elements of ALIST whose cdr is `eq' to VALUE.
1624 The modified ALIST is returned. If the first member of ALIST has a car
1625 that is `eq' to VALUE, there is no way to remove it by side effect;
1626 therefore, write `(setq foo (remrassq value foo))' to be sure of changing
1631 EXTERNAL_LIST_LOOP_DELETE_IF (elt, alist,
1633 EQ_WITH_EBOLA_NOTICE (value, XCDR (elt))));
1637 /* Like Fremrassq, fast and unsafe; be careful */
1639 remrassq_no_quit (Lisp_Object value, Lisp_Object alist)
1641 LIST_LOOP_DELETE_IF (elt, alist,
1643 EQ_WITH_EBOLA_NOTICE (value, XCDR (elt))));
1647 DEFUN ("nreverse", Fnreverse, 1, 1, 0, /*
1648 Reverse LIST by destructively modifying cdr pointers.
1649 Return the beginning of the reversed list.
1650 Also see: `reverse'.
1654 struct gcpro gcpro1, gcpro2;
1655 REGISTER Lisp_Object prev = Qnil;
1656 REGISTER Lisp_Object tail = list;
1658 /* We gcpro our args; see `nconc' */
1659 GCPRO2 (prev, tail);
1660 while (!NILP (tail))
1662 REGISTER Lisp_Object next;
1663 CONCHECK_CONS (tail);
1673 DEFUN ("reverse", Freverse, 1, 1, 0, /*
1674 Reverse LIST, copying. Return the beginning of the reversed list.
1675 See also the function `nreverse', which is used more often.
1679 Lisp_Object reversed_list = Qnil;
1680 EXTERNAL_LIST_LOOP_2 (elt, list)
1682 reversed_list = Fcons (elt, reversed_list);
1684 return reversed_list;
1687 static Lisp_Object list_merge (Lisp_Object org_l1, Lisp_Object org_l2,
1688 Lisp_Object lisp_arg,
1689 int (*pred_fn) (Lisp_Object, Lisp_Object,
1690 Lisp_Object lisp_arg));
1693 list_sort (Lisp_Object list,
1694 Lisp_Object lisp_arg,
1695 int (*pred_fn) (Lisp_Object, Lisp_Object,
1696 Lisp_Object lisp_arg))
1698 struct gcpro gcpro1, gcpro2, gcpro3;
1699 Lisp_Object back, tem;
1700 Lisp_Object front = list;
1701 Lisp_Object len = Flength (list);
1706 len = make_int (XINT (len) / 2 - 1);
1707 tem = Fnthcdr (len, list);
1709 Fsetcdr (tem, Qnil);
1711 GCPRO3 (front, back, lisp_arg);
1712 front = list_sort (front, lisp_arg, pred_fn);
1713 back = list_sort (back, lisp_arg, pred_fn);
1715 return list_merge (front, back, lisp_arg, pred_fn);
1720 merge_pred_function (Lisp_Object obj1, Lisp_Object obj2,
1725 /* prevents the GC from happening in call2 */
1726 int speccount = specpdl_depth ();
1727 /* Emacs' GC doesn't actually relocate pointers, so this probably
1728 isn't strictly necessary */
1729 record_unwind_protect (restore_gc_inhibit,
1730 make_int (gc_currently_forbidden));
1731 gc_currently_forbidden = 1;
1732 tmp = call2 (pred, obj1, obj2);
1733 unbind_to (speccount, Qnil);
1741 DEFUN ("sort", Fsort, 2, 2, 0, /*
1742 Sort LIST, stably, comparing elements using PREDICATE.
1743 Returns the sorted list. LIST is modified by side effects.
1744 PREDICATE is called with two elements of LIST, and should return T
1745 if the first element is "less" than the second.
1749 return list_sort (list, predicate, merge_pred_function);
1753 merge (Lisp_Object org_l1, Lisp_Object org_l2,
1756 return list_merge (org_l1, org_l2, pred, merge_pred_function);
1761 list_merge (Lisp_Object org_l1, Lisp_Object org_l2,
1762 Lisp_Object lisp_arg,
1763 int (*pred_fn) (Lisp_Object, Lisp_Object, Lisp_Object lisp_arg))
1769 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1776 /* It is sufficient to protect org_l1 and org_l2.
1777 When l1 and l2 are updated, we copy the new values
1778 back into the org_ vars. */
1780 GCPRO4 (org_l1, org_l2, lisp_arg, value);
1801 if (((*pred_fn) (Fcar (l2), Fcar (l1), lisp_arg)) < 0)
1816 Fsetcdr (tail, tem);
1822 /************************************************************************/
1823 /* property-list functions */
1824 /************************************************************************/
1826 /* For properties of text, we need to do order-insensitive comparison of
1827 plists. That is, we need to compare two plists such that they are the
1828 same if they have the same set of keys, and equivalent values.
1829 So (a 1 b 2) would be equal to (b 2 a 1).
1831 NIL_MEANS_NOT_PRESENT is as in `plists-eq' etc.
1832 LAXP means use `equal' for comparisons.
1835 plists_differ (Lisp_Object a, Lisp_Object b, int nil_means_not_present,
1836 int laxp, int depth)
1838 int eqp = (depth == -1); /* -1 as depth means use eq, not equal. */
1839 int la, lb, m, i, fill;
1840 Lisp_Object *keys, *vals;
1844 if (NILP (a) && NILP (b))
1847 Fcheck_valid_plist (a);
1848 Fcheck_valid_plist (b);
1850 la = XINT (Flength (a));
1851 lb = XINT (Flength (b));
1852 m = (la > lb ? la : lb);
1854 keys = alloca_array (Lisp_Object, m);
1855 vals = alloca_array (Lisp_Object, m);
1856 flags = alloca_array (char, m);
1858 /* First extract the pairs from A. */
1859 for (rest = a; !NILP (rest); rest = XCDR (XCDR (rest)))
1861 Lisp_Object k = XCAR (rest);
1862 Lisp_Object v = XCAR (XCDR (rest));
1863 /* Maybe be Ebolified. */
1864 if (nil_means_not_present && NILP (v)) continue;
1870 /* Now iterate over B, and stop if we find something that's not in A,
1871 or that doesn't match. As we match, mark them. */
1872 for (rest = b; !NILP (rest); rest = XCDR (XCDR (rest)))
1874 Lisp_Object k = XCAR (rest);
1875 Lisp_Object v = XCAR (XCDR (rest));
1876 /* Maybe be Ebolified. */
1877 if (nil_means_not_present && NILP (v)) continue;
1878 for (i = 0; i < fill; i++)
1880 if (!laxp ? EQ (k, keys [i]) : internal_equal (k, keys [i], depth))
1883 /* We narrowly escaped being Ebolified here. */
1884 ? !EQ_WITH_EBOLA_NOTICE (v, vals [i])
1885 : !internal_equal (v, vals [i], depth))
1886 /* a property in B has a different value than in A */
1893 /* there are some properties in B that are not in A */
1896 /* Now check to see that all the properties in A were also in B */
1897 for (i = 0; i < fill; i++)
1908 DEFUN ("plists-eq", Fplists_eq, 2, 3, 0, /*
1909 Return non-nil if property lists A and B are `eq'.
1910 A property list is an alternating list of keywords and values.
1911 This function does order-insensitive comparisons of the property lists:
1912 For example, the property lists '(a 1 b 2) and '(b 2 a 1) are equal.
1913 Comparison between values is done using `eq'. See also `plists-equal'.
1914 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
1915 a nil value is ignored. This feature is a virus that has infected
1916 old Lisp implementations, but should not be used except for backward
1919 (a, b, nil_means_not_present))
1921 return (plists_differ (a, b, !NILP (nil_means_not_present), 0, -1)
1925 DEFUN ("plists-equal", Fplists_equal, 2, 3, 0, /*
1926 Return non-nil if property lists A and B are `equal'.
1927 A property list is an alternating list of keywords and values. This
1928 function does order-insensitive comparisons of the property lists: For
1929 example, the property lists '(a 1 b 2) and '(b 2 a 1) are equal.
1930 Comparison between values is done using `equal'. See also `plists-eq'.
1931 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
1932 a nil value is ignored. This feature is a virus that has infected
1933 old Lisp implementations, but should not be used except for backward
1936 (a, b, nil_means_not_present))
1938 return (plists_differ (a, b, !NILP (nil_means_not_present), 0, 1)
1943 DEFUN ("lax-plists-eq", Flax_plists_eq, 2, 3, 0, /*
1944 Return non-nil if lax property lists A and B are `eq'.
1945 A property list is an alternating list of keywords and values.
1946 This function does order-insensitive comparisons of the property lists:
1947 For example, the property lists '(a 1 b 2) and '(b 2 a 1) are equal.
1948 Comparison between values is done using `eq'. See also `plists-equal'.
1949 A lax property list is like a regular one except that comparisons between
1950 keywords is done using `equal' instead of `eq'.
1951 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
1952 a nil value is ignored. This feature is a virus that has infected
1953 old Lisp implementations, but should not be used except for backward
1956 (a, b, nil_means_not_present))
1958 return (plists_differ (a, b, !NILP (nil_means_not_present), 1, -1)
1962 DEFUN ("lax-plists-equal", Flax_plists_equal, 2, 3, 0, /*
1963 Return non-nil if lax property lists A and B are `equal'.
1964 A property list is an alternating list of keywords and values. This
1965 function does order-insensitive comparisons of the property lists: For
1966 example, the property lists '(a 1 b 2) and '(b 2 a 1) are equal.
1967 Comparison between values is done using `equal'. See also `plists-eq'.
1968 A lax property list is like a regular one except that comparisons between
1969 keywords is done using `equal' instead of `eq'.
1970 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
1971 a nil value is ignored. This feature is a virus that has infected
1972 old Lisp implementations, but should not be used except for backward
1975 (a, b, nil_means_not_present))
1977 return (plists_differ (a, b, !NILP (nil_means_not_present), 1, 1)
1981 /* Return the value associated with key PROPERTY in property list PLIST.
1982 Return nil if key not found. This function is used for internal
1983 property lists that cannot be directly manipulated by the user.
1987 internal_plist_get (Lisp_Object plist, Lisp_Object property)
1991 for (tail = plist; !NILP (tail); tail = XCDR (XCDR (tail)))
1993 if (EQ (XCAR (tail), property))
1994 return XCAR (XCDR (tail));
2000 /* Set PLIST's value for PROPERTY to VALUE. Analogous to
2001 internal_plist_get(). */
2004 internal_plist_put (Lisp_Object *plist, Lisp_Object property,
2009 for (tail = *plist; !NILP (tail); tail = XCDR (XCDR (tail)))
2011 if (EQ (XCAR (tail), property))
2013 XCAR (XCDR (tail)) = value;
2018 *plist = Fcons (property, Fcons (value, *plist));
2022 internal_remprop (Lisp_Object *plist, Lisp_Object property)
2024 Lisp_Object tail, prev;
2026 for (tail = *plist, prev = Qnil;
2028 tail = XCDR (XCDR (tail)))
2030 if (EQ (XCAR (tail), property))
2033 *plist = XCDR (XCDR (tail));
2035 XCDR (XCDR (prev)) = XCDR (XCDR (tail));
2045 /* Called on a malformed property list. BADPLACE should be some
2046 place where truncating will form a good list -- i.e. we shouldn't
2047 result in a list with an odd length. */
2050 bad_bad_bunny (Lisp_Object *plist, Lisp_Object *badplace, Error_behavior errb)
2052 if (ERRB_EQ (errb, ERROR_ME))
2053 return Fsignal (Qmalformed_property_list, list2 (*plist, *badplace));
2056 if (ERRB_EQ (errb, ERROR_ME_WARN))
2058 warn_when_safe_lispobj
2061 ("Malformed property list -- list has been truncated"),
2069 /* Called on a circular property list. BADPLACE should be some place
2070 where truncating will result in an even-length list, as above.
2071 If doesn't particularly matter where we truncate -- anywhere we
2072 truncate along the entire list will break the circularity, because
2073 it will create a terminus and the list currently doesn't have one.
2077 bad_bad_turtle (Lisp_Object *plist, Lisp_Object *badplace, Error_behavior errb)
2079 if (ERRB_EQ (errb, ERROR_ME))
2080 return Fsignal (Qcircular_property_list, list1 (*plist));
2083 if (ERRB_EQ (errb, ERROR_ME_WARN))
2085 warn_when_safe_lispobj
2088 ("Circular property list -- list has been truncated"),
2096 /* Advance the tortoise pointer by two (one iteration of a property-list
2097 loop) and the hare pointer by four and verify that no malformations
2098 or circularities exist. If so, return zero and store a value into
2099 RETVAL that should be returned by the calling function. Otherwise,
2100 return 1. See external_plist_get().
2104 advance_plist_pointers (Lisp_Object *plist,
2105 Lisp_Object **tortoise, Lisp_Object **hare,
2106 Error_behavior errb, Lisp_Object *retval)
2109 Lisp_Object *tortsave = *tortoise;
2111 /* Note that our "fixing" may be more brutal than necessary,
2112 but it's the user's own problem, not ours, if they went in and
2113 manually fucked up a plist. */
2115 for (i = 0; i < 2; i++)
2117 /* This is a standard iteration of a defensive-loop-checking
2118 loop. We just do it twice because we want to advance past
2119 both the property and its value.
2121 If the pointer indirection is confusing you, remember that
2122 one level of indirection on the hare and tortoise pointers
2123 is only due to pass-by-reference for this function. The other
2124 level is so that the plist can be fixed in place. */
2126 /* When we reach the end of a well-formed plist, **HARE is
2127 nil. In that case, we don't do anything at all except
2128 advance TORTOISE by one. Otherwise, we advance HARE
2129 by two (making sure it's OK to do so), then advance
2130 TORTOISE by one (it will always be OK to do so because
2131 the HARE is always ahead of the TORTOISE and will have
2132 already verified the path), then make sure TORTOISE and
2133 HARE don't contain the same non-nil object -- if the
2134 TORTOISE and the HARE ever meet, then obviously we're
2135 in a circularity, and if we're in a circularity, then
2136 the TORTOISE and the HARE can't cross paths without
2137 meeting, since the HARE only gains one step over the
2138 TORTOISE per iteration. */
2142 Lisp_Object *haresave = *hare;
2143 if (!CONSP (**hare))
2145 *retval = bad_bad_bunny (plist, haresave, errb);
2148 *hare = &XCDR (**hare);
2149 /* In a non-plist, we'd check here for a nil value for
2150 **HARE, which is OK (it just means the list has an
2151 odd number of elements). In a plist, it's not OK
2152 for the list to have an odd number of elements. */
2153 if (!CONSP (**hare))
2155 *retval = bad_bad_bunny (plist, haresave, errb);
2158 *hare = &XCDR (**hare);
2161 *tortoise = &XCDR (**tortoise);
2162 if (!NILP (**hare) && EQ (**tortoise, **hare))
2164 *retval = bad_bad_turtle (plist, tortsave, errb);
2172 /* Return the value of PROPERTY from PLIST, or Qunbound if
2173 property is not on the list.
2175 PLIST is a Lisp-accessible property list, meaning that it
2176 has to be checked for malformations and circularities.
2178 If ERRB is ERROR_ME, an error will be signalled. Otherwise, the
2179 function will never signal an error; and if ERRB is ERROR_ME_WARN,
2180 on finding a malformation or a circularity, it issues a warning and
2181 attempts to silently fix the problem.
2183 A pointer to PLIST is passed in so that PLIST can be successfully
2184 "fixed" even if the error is at the beginning of the plist. */
2187 external_plist_get (Lisp_Object *plist, Lisp_Object property,
2188 int laxp, Error_behavior errb)
2190 Lisp_Object *tortoise = plist;
2191 Lisp_Object *hare = plist;
2193 while (!NILP (*tortoise))
2195 Lisp_Object *tortsave = tortoise;
2198 /* We do the standard tortoise/hare march. We isolate the
2199 grungy stuff to do this in advance_plist_pointers(), though.
2200 To us, all this function does is advance the tortoise
2201 pointer by two and the hare pointer by four and make sure
2202 everything's OK. We first advance the pointers and then
2203 check if a property matched; this ensures that our
2204 check for a matching property is safe. */
2206 if (!advance_plist_pointers (plist, &tortoise, &hare, errb, &retval))
2209 if (!laxp ? EQ (XCAR (*tortsave), property)
2210 : internal_equal (XCAR (*tortsave), property, 0))
2211 return XCAR (XCDR (*tortsave));
2217 /* Set PLIST's value for PROPERTY to VALUE, given a possibly
2218 malformed or circular plist. Analogous to external_plist_get(). */
2221 external_plist_put (Lisp_Object *plist, Lisp_Object property,
2222 Lisp_Object value, int laxp, Error_behavior errb)
2224 Lisp_Object *tortoise = plist;
2225 Lisp_Object *hare = plist;
2227 while (!NILP (*tortoise))
2229 Lisp_Object *tortsave = tortoise;
2233 if (!advance_plist_pointers (plist, &tortoise, &hare, errb, &retval))
2236 if (!laxp ? EQ (XCAR (*tortsave), property)
2237 : internal_equal (XCAR (*tortsave), property, 0))
2239 XCAR (XCDR (*tortsave)) = value;
2244 *plist = Fcons (property, Fcons (value, *plist));
2248 external_remprop (Lisp_Object *plist, Lisp_Object property,
2249 int laxp, Error_behavior errb)
2251 Lisp_Object *tortoise = plist;
2252 Lisp_Object *hare = plist;
2254 while (!NILP (*tortoise))
2256 Lisp_Object *tortsave = tortoise;
2260 if (!advance_plist_pointers (plist, &tortoise, &hare, errb, &retval))
2263 if (!laxp ? EQ (XCAR (*tortsave), property)
2264 : internal_equal (XCAR (*tortsave), property, 0))
2266 /* Now you see why it's so convenient to have that level
2268 *tortsave = XCDR (XCDR (*tortsave));
2276 DEFUN ("plist-get", Fplist_get, 2, 3, 0, /*
2277 Extract a value from a property list.
2278 PLIST is a property list, which is a list of the form
2279 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2...).
2280 PROPERTY is usually a symbol.
2281 This function returns the value corresponding to the PROPERTY,
2282 or DEFAULT if PROPERTY is not one of the properties on the list.
2284 (plist, property, default_))
2286 Lisp_Object value = external_plist_get (&plist, property, 0, ERROR_ME);
2287 return UNBOUNDP (value) ? default_ : value;
2290 DEFUN ("plist-put", Fplist_put, 3, 3, 0, /*
2291 Change value in PLIST of PROPERTY to VALUE.
2292 PLIST is a property list, which is a list of the form
2293 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2 ...).
2294 PROPERTY is usually a symbol and VALUE is any object.
2295 If PROPERTY is already a property on the list, its value is set to VALUE,
2296 otherwise the new PROPERTY VALUE pair is added.
2297 The new plist is returned; use `(setq x (plist-put x property value))'
2298 to be sure to use the new value. PLIST is modified by side effect.
2300 (plist, property, value))
2302 external_plist_put (&plist, property, value, 0, ERROR_ME);
2306 DEFUN ("plist-remprop", Fplist_remprop, 2, 2, 0, /*
2307 Remove from PLIST the property PROPERTY and its value.
2308 PLIST is a property list, which is a list of the form
2309 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2 ...).
2310 PROPERTY is usually a symbol.
2311 The new plist is returned; use `(setq x (plist-remprop x property))'
2312 to be sure to use the new value. PLIST is modified by side effect.
2316 external_remprop (&plist, property, 0, ERROR_ME);
2320 DEFUN ("plist-member", Fplist_member, 2, 2, 0, /*
2321 Return t if PROPERTY has a value specified in PLIST.
2325 Lisp_Object value = Fplist_get (plist, property, Qunbound);
2326 return UNBOUNDP (value) ? Qnil : Qt;
2329 DEFUN ("check-valid-plist", Fcheck_valid_plist, 1, 1, 0, /*
2330 Given a plist, signal an error if there is anything wrong with it.
2331 This means that it's a malformed or circular plist.
2335 Lisp_Object *tortoise;
2341 while (!NILP (*tortoise))
2346 if (!advance_plist_pointers (&plist, &tortoise, &hare, ERROR_ME,
2354 DEFUN ("valid-plist-p", Fvalid_plist_p, 1, 1, 0, /*
2355 Given a plist, return non-nil if its format is correct.
2356 If it returns nil, `check-valid-plist' will signal an error when given
2357 the plist; that means it's a malformed or circular plist.
2361 Lisp_Object *tortoise;
2366 while (!NILP (*tortoise))
2371 if (!advance_plist_pointers (&plist, &tortoise, &hare, ERROR_ME_NOT,
2379 DEFUN ("canonicalize-plist", Fcanonicalize_plist, 1, 2, 0, /*
2380 Destructively remove any duplicate entries from a plist.
2381 In such cases, the first entry applies.
2383 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
2384 a nil value is removed. This feature is a virus that has infected
2385 old Lisp implementations, but should not be used except for backward
2388 The new plist is returned. If NIL-MEANS-NOT-PRESENT is given, the
2389 return value may not be EQ to the passed-in value, so make sure to
2390 `setq' the value back into where it came from.
2392 (plist, nil_means_not_present))
2394 Lisp_Object head = plist;
2396 Fcheck_valid_plist (plist);
2398 while (!NILP (plist))
2400 Lisp_Object prop = Fcar (plist);
2401 Lisp_Object next = Fcdr (plist);
2403 CHECK_CONS (next); /* just make doubly sure we catch any errors */
2404 if (!NILP (nil_means_not_present) && NILP (Fcar (next)))
2406 if (EQ (head, plist))
2408 plist = Fcdr (next);
2411 /* external_remprop returns 1 if it removed any property.
2412 We have to loop till it didn't remove anything, in case
2413 the property occurs many times. */
2414 while (external_remprop (&XCDR (next), prop, 0, ERROR_ME))
2416 plist = Fcdr (next);
2422 DEFUN ("lax-plist-get", Flax_plist_get, 2, 3, 0, /*
2423 Extract a value from a lax property list.
2424 LAX-PLIST is a lax property list, which is a list of the form
2425 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2...), where comparisons between
2426 properties is done using `equal' instead of `eq'.
2427 PROPERTY is usually a symbol.
2428 This function returns the value corresponding to PROPERTY,
2429 or DEFAULT if PROPERTY is not one of the properties on the list.
2431 (lax_plist, property, default_))
2433 Lisp_Object value = external_plist_get (&lax_plist, property, 1, ERROR_ME);
2434 return UNBOUNDP (value) ? default_ : value;
2437 DEFUN ("lax-plist-put", Flax_plist_put, 3, 3, 0, /*
2438 Change value in LAX-PLIST of PROPERTY to VALUE.
2439 LAX-PLIST is a lax property list, which is a list of the form
2440 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2...), where comparisons between
2441 properties is done using `equal' instead of `eq'.
2442 PROPERTY is usually a symbol and VALUE is any object.
2443 If PROPERTY is already a property on the list, its value is set to
2444 VALUE, otherwise the new PROPERTY VALUE pair is added.
2445 The new plist is returned; use `(setq x (lax-plist-put x property value))'
2446 to be sure to use the new value. LAX-PLIST is modified by side effect.
2448 (lax_plist, property, value))
2450 external_plist_put (&lax_plist, property, value, 1, ERROR_ME);
2454 DEFUN ("lax-plist-remprop", Flax_plist_remprop, 2, 2, 0, /*
2455 Remove from LAX-PLIST the property PROPERTY and its value.
2456 LAX-PLIST is a lax property list, which is a list of the form
2457 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2...), where comparisons between
2458 properties is done using `equal' instead of `eq'.
2459 PROPERTY is usually a symbol.
2460 The new plist is returned; use `(setq x (lax-plist-remprop x property))'
2461 to be sure to use the new value. LAX-PLIST is modified by side effect.
2463 (lax_plist, property))
2465 external_remprop (&lax_plist, property, 1, ERROR_ME);
2469 DEFUN ("lax-plist-member", Flax_plist_member, 2, 2, 0, /*
2470 Return t if PROPERTY has a value specified in LAX-PLIST.
2471 LAX-PLIST is a lax property list, which is a list of the form
2472 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2...), where comparisons between
2473 properties is done using `equal' instead of `eq'.
2475 (lax_plist, property))
2477 return UNBOUNDP (Flax_plist_get (lax_plist, property, Qunbound)) ? Qnil : Qt;
2480 DEFUN ("canonicalize-lax-plist", Fcanonicalize_lax_plist, 1, 2, 0, /*
2481 Destructively remove any duplicate entries from a lax plist.
2482 In such cases, the first entry applies.
2484 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
2485 a nil value is removed. This feature is a virus that has infected
2486 old Lisp implementations, but should not be used except for backward
2489 The new plist is returned. If NIL-MEANS-NOT-PRESENT is given, the
2490 return value may not be EQ to the passed-in value, so make sure to
2491 `setq' the value back into where it came from.
2493 (lax_plist, nil_means_not_present))
2495 Lisp_Object head = lax_plist;
2497 Fcheck_valid_plist (lax_plist);
2499 while (!NILP (lax_plist))
2501 Lisp_Object prop = Fcar (lax_plist);
2502 Lisp_Object next = Fcdr (lax_plist);
2504 CHECK_CONS (next); /* just make doubly sure we catch any errors */
2505 if (!NILP (nil_means_not_present) && NILP (Fcar (next)))
2507 if (EQ (head, lax_plist))
2509 lax_plist = Fcdr (next);
2512 /* external_remprop returns 1 if it removed any property.
2513 We have to loop till it didn't remove anything, in case
2514 the property occurs many times. */
2515 while (external_remprop (&XCDR (next), prop, 1, ERROR_ME))
2517 lax_plist = Fcdr (next);
2523 /* In C because the frame props stuff uses it */
2525 DEFUN ("destructive-alist-to-plist", Fdestructive_alist_to_plist, 1, 1, 0, /*
2526 Convert association list ALIST into the equivalent property-list form.
2527 The plist is returned. This converts from
2529 \((a . 1) (b . 2) (c . 3))
2535 The original alist is destroyed in the process of constructing the plist.
2536 See also `alist-to-plist'.
2540 Lisp_Object head = alist;
2541 while (!NILP (alist))
2543 /* remember the alist element. */
2544 Lisp_Object el = Fcar (alist);
2546 Fsetcar (alist, Fcar (el));
2547 Fsetcar (el, Fcdr (el));
2548 Fsetcdr (el, Fcdr (alist));
2549 Fsetcdr (alist, el);
2550 alist = Fcdr (Fcdr (alist));
2556 DEFUN ("get", Fget, 2, 3, 0, /*
2557 Return the value of OBJECT's PROPERTY property.
2558 This is the last VALUE stored with `(put OBJECT PROPERTY VALUE)'.
2559 If there is no such property, return optional third arg DEFAULT
2560 \(which defaults to `nil'). OBJECT can be a symbol, string, extent,
2561 face, or glyph. See also `put', `remprop', and `object-plist'.
2563 (object, property, default_))
2565 /* Various places in emacs call Fget() and expect it not to quit,
2569 if (LRECORDP (object) && XRECORD_LHEADER_IMPLEMENTATION (object)->getprop)
2570 val = XRECORD_LHEADER_IMPLEMENTATION (object)->getprop (object, property);
2572 signal_simple_error ("Object type has no properties", object);
2574 return UNBOUNDP (val) ? default_ : val;
2577 DEFUN ("put", Fput, 3, 3, 0, /*
2578 Set OBJECT's PROPERTY to VALUE.
2579 It can be subsequently retrieved with `(get OBJECT PROPERTY)'.
2580 OBJECT can be a symbol, face, extent, or string.
2581 For a string, no properties currently have predefined meanings.
2582 For the predefined properties for extents, see `set-extent-property'.
2583 For the predefined properties for faces, see `set-face-property'.
2584 See also `get', `remprop', and `object-plist'.
2586 (object, property, value))
2588 CHECK_LISP_WRITEABLE (object);
2590 if (LRECORDP (object) && XRECORD_LHEADER_IMPLEMENTATION (object)->putprop)
2592 if (! XRECORD_LHEADER_IMPLEMENTATION (object)->putprop
2593 (object, property, value))
2594 signal_simple_error ("Can't set property on object", property);
2597 signal_simple_error ("Object type has no settable properties", object);
2602 DEFUN ("remprop", Fremprop, 2, 2, 0, /*
2603 Remove, from OBJECT's property list, PROPERTY and its corresponding value.
2604 OBJECT can be a symbol, string, extent, face, or glyph. Return non-nil
2605 if the property list was actually modified (i.e. if PROPERTY was present
2606 in the property list). See also `get', `put', and `object-plist'.
2612 CHECK_LISP_WRITEABLE (object);
2614 if (LRECORDP (object) && XRECORD_LHEADER_IMPLEMENTATION (object)->remprop)
2616 ret = XRECORD_LHEADER_IMPLEMENTATION (object)->remprop (object, property);
2618 signal_simple_error ("Can't remove property from object", property);
2621 signal_simple_error ("Object type has no removable properties", object);
2623 return ret ? Qt : Qnil;
2626 DEFUN ("object-plist", Fobject_plist, 1, 1, 0, /*
2627 Return a property list of OBJECT's properties.
2628 For a symbol, this is equivalent to `symbol-plist'.
2629 OBJECT can be a symbol, string, extent, face, or glyph.
2630 Do not modify the returned property list directly;
2631 this may or may not have the desired effects. Use `put' instead.
2635 if (LRECORDP (object) && XRECORD_LHEADER_IMPLEMENTATION (object)->plist)
2636 return XRECORD_LHEADER_IMPLEMENTATION (object)->plist (object);
2638 signal_simple_error ("Object type has no properties", object);
2645 internal_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
2648 error ("Stack overflow in equal");
2650 if (EQ_WITH_EBOLA_NOTICE (obj1, obj2))
2652 /* Note that (equal 20 20.0) should be nil */
2653 if (XTYPE (obj1) != XTYPE (obj2))
2655 if (LRECORDP (obj1))
2657 const struct lrecord_implementation
2658 *imp1 = XRECORD_LHEADER_IMPLEMENTATION (obj1),
2659 *imp2 = XRECORD_LHEADER_IMPLEMENTATION (obj2);
2661 return (imp1 == imp2) &&
2662 /* EQ-ness of the objects was noticed above */
2663 (imp1->equal && (imp1->equal) (obj1, obj2, depth));
2669 /* Note that we may be calling sub-objects that will use
2670 internal_equal() (instead of internal_old_equal()). Oh well.
2671 We will get an Ebola note if there's any possibility of confusion,
2672 but that seems unlikely. */
2675 internal_old_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
2678 error ("Stack overflow in equal");
2680 if (HACKEQ_UNSAFE (obj1, obj2))
2682 /* Note that (equal 20 20.0) should be nil */
2683 if (XTYPE (obj1) != XTYPE (obj2))
2686 return internal_equal (obj1, obj2, depth);
2689 DEFUN ("equal", Fequal, 2, 2, 0, /*
2690 Return t if two Lisp objects have similar structure and contents.
2691 They must have the same data type.
2692 Conses are compared by comparing the cars and the cdrs.
2693 Vectors and strings are compared element by element.
2694 Numbers are compared by value. Symbols must match exactly.
2698 return internal_equal (object1, object2, 0) ? Qt : Qnil;
2701 DEFUN ("old-equal", Fold_equal, 2, 2, 0, /*
2702 Return t if two Lisp objects have similar structure and contents.
2703 They must have the same data type.
2704 \(Note, however, that an exception is made for characters and integers;
2705 this is known as the "char-int confoundance disease." See `eq' and
2707 This function is provided only for byte-code compatibility with v19.
2712 return internal_old_equal (object1, object2, 0) ? Qt : Qnil;
2716 DEFUN ("fillarray", Ffillarray, 2, 2, 0, /*
2717 Destructively modify ARRAY by replacing each element with ITEM.
2718 ARRAY is a vector, bit vector, or string.
2723 if (STRINGP (array))
2725 Lisp_String *s = XSTRING (array);
2726 Bytecount old_bytecount = string_length (s);
2727 Bytecount new_bytecount;
2728 Bytecount item_bytecount;
2729 Bufbyte item_buf[MAX_EMCHAR_LEN];
2733 CHECK_CHAR_COERCE_INT (item);
2734 CHECK_LISP_WRITEABLE (array);
2736 item_bytecount = set_charptr_emchar (item_buf, XCHAR (item));
2737 new_bytecount = item_bytecount * string_char_length (s);
2739 resize_string (s, -1, new_bytecount - old_bytecount);
2741 for (p = string_data (s), end = p + new_bytecount;
2743 p += item_bytecount)
2744 memcpy (p, item_buf, item_bytecount);
2747 bump_string_modiff (array);
2749 else if (VECTORP (array))
2751 Lisp_Object *p = XVECTOR_DATA (array);
2752 size_t len = XVECTOR_LENGTH (array);
2753 CHECK_LISP_WRITEABLE (array);
2757 else if (BIT_VECTORP (array))
2759 Lisp_Bit_Vector *v = XBIT_VECTOR (array);
2760 size_t len = bit_vector_length (v);
2764 CHECK_LISP_WRITEABLE (array);
2766 set_bit_vector_bit (v, len, bit);
2770 array = wrong_type_argument (Qarrayp, array);
2777 nconc2 (Lisp_Object arg1, Lisp_Object arg2)
2779 Lisp_Object args[2];
2780 struct gcpro gcpro1;
2787 RETURN_UNGCPRO (bytecode_nconc2 (args));
2791 bytecode_nconc2 (Lisp_Object *args)
2795 if (CONSP (args[0]))
2797 /* (setcdr (last args[0]) args[1]) */
2798 Lisp_Object tortoise, hare;
2801 for (hare = tortoise = args[0], count = 0;
2802 CONSP (XCDR (hare));
2803 hare = XCDR (hare), count++)
2805 if (count < CIRCULAR_LIST_SUSPICION_LENGTH) continue;
2808 tortoise = XCDR (tortoise);
2809 if (EQ (hare, tortoise))
2810 signal_circular_list_error (args[0]);
2812 XCDR (hare) = args[1];
2815 else if (NILP (args[0]))
2821 args[0] = wrong_type_argument (args[0], Qlistp);
2826 DEFUN ("nconc", Fnconc, 0, MANY, 0, /*
2827 Concatenate any number of lists by altering them.
2828 Only the last argument is not altered, and need not be a list.
2830 If the first argument is nil, there is no way to modify it by side
2831 effect; therefore, write `(setq foo (nconc foo list))' to be sure of
2832 changing the value of `foo'.
2834 (int nargs, Lisp_Object *args))
2837 struct gcpro gcpro1;
2839 /* The modus operandi in Emacs is "caller gc-protects args".
2840 However, nconc (particularly nconc2 ()) is called many times
2841 in Emacs on freshly created stuff (e.g. you see the idiom
2842 nconc2 (Fcopy_sequence (foo), bar) a lot). So we help those
2843 callers out by protecting the args ourselves to save them
2844 a lot of temporary-variable grief. */
2847 gcpro1.nvars = nargs;
2849 while (argnum < nargs)
2856 /* `val' is the first cons, which will be our return value. */
2857 /* `last_cons' will be the cons cell to mutate. */
2858 Lisp_Object last_cons = val;
2859 Lisp_Object tortoise = val;
2861 for (argnum++; argnum < nargs; argnum++)
2863 Lisp_Object next = args[argnum];
2865 if (CONSP (next) || argnum == nargs -1)
2867 /* (setcdr (last val) next) */
2871 CONSP (XCDR (last_cons));
2872 last_cons = XCDR (last_cons), count++)
2874 if (count < CIRCULAR_LIST_SUSPICION_LENGTH) continue;
2877 tortoise = XCDR (tortoise);
2878 if (EQ (last_cons, tortoise))
2879 signal_circular_list_error (args[argnum-1]);
2881 XCDR (last_cons) = next;
2883 else if (NILP (next))
2889 next = wrong_type_argument (Qlistp, next);
2893 RETURN_UNGCPRO (val);
2895 else if (NILP (val))
2897 else if (argnum == nargs - 1) /* last arg? */
2898 RETURN_UNGCPRO (val);
2901 args[argnum] = wrong_type_argument (Qlistp, val);
2905 RETURN_UNGCPRO (Qnil); /* No non-nil args provided. */
2909 /* This is the guts of several mapping functions.
2910 Apply FUNCTION to each element of SEQUENCE, one by one,
2911 storing the results into elements of VALS, a C vector of Lisp_Objects.
2912 LENI is the length of VALS, which should also be the length of SEQUENCE.
2914 If VALS is a null pointer, do not accumulate the results. */
2917 mapcar1 (size_t leni, Lisp_Object *vals,
2918 Lisp_Object function, Lisp_Object sequence)
2921 Lisp_Object args[2];
2922 struct gcpro gcpro1;
2932 if (LISTP (sequence))
2934 /* A devious `function' could either:
2935 - insert garbage into the list in front of us, causing XCDR to crash
2936 - amputate the list behind us using (setcdr), causing the remaining
2937 elts to lose their GCPRO status.
2939 if (vals != 0) we avoid this by copying the elts into the
2940 `vals' array. By a stroke of luck, `vals' is exactly large
2941 enough to hold the elts left to be traversed as well as the
2942 results computed so far.
2944 if (vals == 0) we don't have any free space available and
2945 don't want to eat up any more stack with alloca().
2946 So we use EXTERNAL_LIST_LOOP_3_NO_DECLARE and GCPRO the tail. */
2950 Lisp_Object *val = vals;
2953 LIST_LOOP_2 (elt, sequence)
2956 gcpro1.nvars = leni;
2958 for (i = 0; i < leni; i++)
2961 vals[i] = Ffuncall (2, args);
2966 Lisp_Object elt, tail;
2967 EMACS_INT len_unused;
2968 struct gcpro ngcpro1;
2973 EXTERNAL_LIST_LOOP_4_NO_DECLARE (elt, sequence, tail, len_unused)
2983 else if (VECTORP (sequence))
2985 Lisp_Object *objs = XVECTOR_DATA (sequence);
2987 for (i = 0; i < leni; i++)
2990 result = Ffuncall (2, args);
2991 if (vals) vals[gcpro1.nvars++] = result;
2994 else if (STRINGP (sequence))
2996 /* The string data of `sequence' might be relocated during GC. */
2997 Bytecount slen = XSTRING_LENGTH (sequence);
2998 Bufbyte *p = alloca_array (Bufbyte, slen);
2999 Bufbyte *end = p + slen;
3001 memcpy (p, XSTRING_DATA (sequence), slen);
3005 args[1] = make_char (charptr_emchar (p));
3007 result = Ffuncall (2, args);
3008 if (vals) vals[gcpro1.nvars++] = result;
3011 else if (BIT_VECTORP (sequence))
3013 Lisp_Bit_Vector *v = XBIT_VECTOR (sequence);
3015 for (i = 0; i < leni; i++)
3017 args[1] = make_int (bit_vector_bit (v, i));
3018 result = Ffuncall (2, args);
3019 if (vals) vals[gcpro1.nvars++] = result;
3023 abort (); /* unreachable, since Flength (sequence) did not get an error */
3029 DEFUN ("mapconcat", Fmapconcat, 3, 3, 0, /*
3030 Apply FUNCTION to each element of SEQUENCE, and concat the results to a string.
3031 Between each pair of results, insert SEPARATOR.
3033 Each result, and SEPARATOR, should be strings. Thus, using " " as SEPARATOR
3034 results in spaces between the values returned by FUNCTION. SEQUENCE itself
3035 may be a list, a vector, a bit vector, or a string.
3037 (function, sequence, separator))
3039 EMACS_INT len = XINT (Flength (sequence));
3042 EMACS_INT nargs = len + len - 1;
3044 if (len == 0) return build_string ("");
3046 args = alloca_array (Lisp_Object, nargs);
3048 mapcar1 (len, args, function, sequence);
3050 for (i = len - 1; i >= 0; i--)
3051 args[i + i] = args[i];
3053 for (i = 1; i < nargs; i += 2)
3054 args[i] = separator;
3056 return Fconcat (nargs, args);
3059 DEFUN ("mapcar", Fmapcar, 2, 2, 0, /*
3060 Apply FUNCTION to each element of SEQUENCE; return a list of the results.
3061 The result is a list of the same length as SEQUENCE.
3062 SEQUENCE may be a list, a vector, a bit vector, or a string.
3064 (function, sequence))
3066 size_t len = XINT (Flength (sequence));
3067 Lisp_Object *args = alloca_array (Lisp_Object, len);
3069 mapcar1 (len, args, function, sequence);
3071 return Flist (len, args);
3074 DEFUN ("mapvector", Fmapvector, 2, 2, 0, /*
3075 Apply FUNCTION to each element of SEQUENCE; return a vector of the results.
3076 The result is a vector of the same length as SEQUENCE.
3077 SEQUENCE may be a list, a vector, a bit vector, or a string.
3079 (function, sequence))
3081 size_t len = XINT (Flength (sequence));
3082 Lisp_Object result = make_vector (len, Qnil);
3083 struct gcpro gcpro1;
3086 mapcar1 (len, XVECTOR_DATA (result), function, sequence);
3092 DEFUN ("mapc-internal", Fmapc_internal, 2, 2, 0, /*
3093 Apply FUNCTION to each element of SEQUENCE.
3094 SEQUENCE may be a list, a vector, a bit vector, or a string.
3095 This function is like `mapcar' but does not accumulate the results,
3096 which is more efficient if you do not use the results.
3098 The difference between this and `mapc' is that `mapc' supports all
3099 the spiffy Common Lisp arguments. You should normally use `mapc'.
3101 (function, sequence))
3103 mapcar1 (XINT (Flength (sequence)), 0, function, sequence);
3111 DEFUN ("replace-list", Freplace_list, 2, 2, 0, /*
3112 Destructively replace the list OLD with NEW.
3113 This is like (copy-sequence NEW) except that it reuses the
3114 conses in OLD as much as possible. If OLD and NEW are the same
3115 length, no consing will take place.
3119 Lisp_Object tail, oldtail = old, prevoldtail = Qnil;
3121 EXTERNAL_LIST_LOOP (tail, new)
3123 if (!NILP (oldtail))
3125 CHECK_CONS (oldtail);
3126 XCAR (oldtail) = XCAR (tail);
3128 else if (!NILP (prevoldtail))
3130 XCDR (prevoldtail) = Fcons (XCAR (tail), Qnil);
3131 prevoldtail = XCDR (prevoldtail);
3134 old = oldtail = Fcons (XCAR (tail), Qnil);
3136 if (!NILP (oldtail))
3138 prevoldtail = oldtail;
3139 oldtail = XCDR (oldtail);
3143 if (!NILP (prevoldtail))
3144 XCDR (prevoldtail) = Qnil;
3152 /* #### this function doesn't belong in this file! */
3154 #ifdef HAVE_GETLOADAVG
3155 #ifdef HAVE_SYS_LOADAVG_H
3156 #include <sys/loadavg.h>
3159 int getloadavg (double loadavg[], int nelem); /* Defined in getloadavg.c */
3162 DEFUN ("load-average", Fload_average, 0, 1, 0, /*
3163 Return list of 1 minute, 5 minute and 15 minute load averages.
3164 Each of the three load averages is multiplied by 100,
3165 then converted to integer.
3167 When USE-FLOATS is non-nil, floats will be used instead of integers.
3168 These floats are not multiplied by 100.
3170 If the 5-minute or 15-minute load averages are not available, return a
3171 shortened list, containing only those averages which are available.
3173 On some systems, this won't work due to permissions on /dev/kmem,
3174 in which case you can't use this.
3179 int loads = getloadavg (load_ave, countof (load_ave));
3180 Lisp_Object ret = Qnil;
3183 error ("load-average not implemented for this operating system");
3185 signal_simple_error ("Could not get load-average",
3186 lisp_strerror (errno));
3190 Lisp_Object load = (NILP (use_floats) ?
3191 make_int ((int) (100.0 * load_ave[loads]))
3192 : make_float (load_ave[loads]));
3193 ret = Fcons (load, ret);
3199 Lisp_Object Vfeatures;
3201 DEFUN ("featurep", Ffeaturep, 1, 1, 0, /*
3202 Return non-nil if feature FEXP is present in this Emacs.
3203 Use this to conditionalize execution of lisp code based on the
3204 presence or absence of emacs or environment extensions.
3205 FEXP can be a symbol, a number, or a list.
3206 If it is a symbol, that symbol is looked up in the `features' variable,
3207 and non-nil will be returned if found.
3208 If it is a number, the function will return non-nil if this Emacs
3209 has an equal or greater version number than FEXP.
3210 If it is a list whose car is the symbol `and', it will return
3211 non-nil if all the features in its cdr are non-nil.
3212 If it is a list whose car is the symbol `or', it will return non-nil
3213 if any of the features in its cdr are non-nil.
3214 If it is a list whose car is the symbol `not', it will return
3215 non-nil if the feature is not present.
3220 => ; Non-nil on XEmacs.
3222 (featurep '(and xemacs gnus))
3223 => ; Non-nil on XEmacs with Gnus loaded.
3225 (featurep '(or tty-frames (and emacs 19.30)))
3226 => ; Non-nil if this Emacs supports TTY frames.
3228 (featurep '(or (and xemacs 19.15) (and emacs 19.34)))
3229 => ; Non-nil on XEmacs 19.15 and later, or FSF Emacs 19.34 and later.
3231 (featurep '(and xemacs 21.02))
3232 => ; Non-nil on XEmacs 21.2 and later.
3234 NOTE: The advanced arguments of this function (anything other than a
3235 symbol) are not yet supported by FSF Emacs. If you feel they are useful
3236 for supporting multiple Emacs variants, lobby Richard Stallman at
3237 <bug-gnu-emacs@gnu.org>.
3241 #ifndef FEATUREP_SYNTAX
3242 CHECK_SYMBOL (fexp);
3243 return NILP (Fmemq (fexp, Vfeatures)) ? Qnil : Qt;
3244 #else /* FEATUREP_SYNTAX */
3245 static double featurep_emacs_version;
3247 /* Brute force translation from Erik Naggum's lisp function. */
3250 /* Original definition */
3251 return NILP (Fmemq (fexp, Vfeatures)) ? Qnil : Qt;
3253 else if (INTP (fexp) || FLOATP (fexp))
3255 double d = extract_float (fexp);
3257 if (featurep_emacs_version == 0.0)
3259 featurep_emacs_version = XINT (Vemacs_major_version) +
3260 (XINT (Vemacs_minor_version) / 100.0);
3262 return featurep_emacs_version >= d ? Qt : Qnil;
3264 else if (CONSP (fexp))
3266 Lisp_Object tem = XCAR (fexp);
3272 negate = Fcar (tem);
3274 return NILP (call1 (Qfeaturep, negate)) ? Qt : Qnil;
3276 return Fsignal (Qinvalid_read_syntax, list1 (tem));
3278 else if (EQ (tem, Qand))
3281 /* Use Fcar/Fcdr for error-checking. */
3282 while (!NILP (tem) && !NILP (call1 (Qfeaturep, Fcar (tem))))
3286 return NILP (tem) ? Qt : Qnil;
3288 else if (EQ (tem, Qor))
3291 /* Use Fcar/Fcdr for error-checking. */
3292 while (!NILP (tem) && NILP (call1 (Qfeaturep, Fcar (tem))))
3296 return NILP (tem) ? Qnil : Qt;
3300 return Fsignal (Qinvalid_read_syntax, list1 (XCDR (fexp)));
3305 return Fsignal (Qinvalid_read_syntax, list1 (fexp));
3308 #endif /* FEATUREP_SYNTAX */
3310 DEFUN ("provide", Fprovide, 1, 1, 0, /*
3311 Announce that FEATURE is a feature of the current Emacs.
3312 This function updates the value of the variable `features'.
3317 CHECK_SYMBOL (feature);
3318 if (!NILP (Vautoload_queue))
3319 Vautoload_queue = Fcons (Fcons (Vfeatures, Qnil), Vautoload_queue);
3320 tem = Fmemq (feature, Vfeatures);
3322 Vfeatures = Fcons (feature, Vfeatures);
3323 LOADHIST_ATTACH (Fcons (Qprovide, feature));
3327 DEFUN ("require", Frequire, 1, 2, 0, /*
3328 If feature FEATURE is not loaded, load it from FILENAME.
3329 If FEATURE is not a member of the list `features', then the feature
3330 is not loaded; so load the file FILENAME.
3331 If FILENAME is omitted, the printname of FEATURE is used as the file name.
3333 (feature, filename))
3336 CHECK_SYMBOL (feature);
3337 tem = Fmemq (feature, Vfeatures);
3338 LOADHIST_ATTACH (Fcons (Qrequire, feature));
3343 int speccount = specpdl_depth ();
3345 /* Value saved here is to be restored into Vautoload_queue */
3346 record_unwind_protect (un_autoload, Vautoload_queue);
3347 Vautoload_queue = Qt;
3349 call4 (Qload, NILP (filename) ? Fsymbol_name (feature) : filename,
3352 tem = Fmemq (feature, Vfeatures);
3354 error ("Required feature %s was not provided",
3355 string_data (XSYMBOL (feature)->name));
3357 /* Once loading finishes, don't undo it. */
3358 Vautoload_queue = Qt;
3359 return unbind_to (speccount, feature);
3363 /* base64 encode/decode functions.
3365 Originally based on code from GNU recode. Ported to FSF Emacs by
3366 Lars Magne Ingebrigtsen and Karl Heuer. Ported to XEmacs and
3367 subsequently heavily hacked by Hrvoje Niksic. */
3369 #define MIME_LINE_LENGTH 72
3371 #define IS_ASCII(Character) \
3373 #define IS_BASE64(Character) \
3374 (IS_ASCII (Character) && base64_char_to_value[Character] >= 0)
3376 /* Table of characters coding the 64 values. */
3377 static char base64_value_to_char[64] =
3379 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 0- 9 */
3380 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 10-19 */
3381 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', /* 20-29 */
3382 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', /* 30-39 */
3383 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', /* 40-49 */
3384 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', /* 50-59 */
3385 '8', '9', '+', '/' /* 60-63 */
3388 /* Table of base64 values for first 128 characters. */
3389 static short base64_char_to_value[128] =
3391 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0- 9 */
3392 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10- 19 */
3393 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20- 29 */
3394 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 30- 39 */
3395 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, /* 40- 49 */
3396 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, /* 50- 59 */
3397 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, /* 60- 69 */
3398 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 70- 79 */
3399 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, /* 80- 89 */
3400 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, /* 90- 99 */
3401 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, /* 100-109 */
3402 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, /* 110-119 */
3403 49, 50, 51, -1, -1, -1, -1, -1 /* 120-127 */
3406 /* The following diagram shows the logical steps by which three octets
3407 get transformed into four base64 characters.
3409 .--------. .--------. .--------.
3410 |aaaaaabb| |bbbbcccc| |ccdddddd|
3411 `--------' `--------' `--------'
3413 .--------+--------+--------+--------.
3414 |00aaaaaa|00bbbbbb|00cccccc|00dddddd|
3415 `--------+--------+--------+--------'
3417 .--------+--------+--------+--------.
3418 |AAAAAAAA|BBBBBBBB|CCCCCCCC|DDDDDDDD|
3419 `--------+--------+--------+--------'
3421 The octets are divided into 6 bit chunks, which are then encoded into
3422 base64 characters. */
3424 #define ADVANCE_INPUT(c, stream) \
3425 ((ec = Lstream_get_emchar (stream)) == -1 ? 0 : \
3427 (signal_simple_error ("Non-ascii character in base64 input", \
3428 make_char (ec)), 0) \
3429 : (c = (Bufbyte)ec), 1))
3432 base64_encode_1 (Lstream *istream, Bufbyte *to, int line_break)
3434 EMACS_INT counter = 0;
3442 if (!ADVANCE_INPUT (c, istream))
3445 /* Wrap line every 76 characters. */
3448 if (counter < MIME_LINE_LENGTH / 4)
3457 /* Process first byte of a triplet. */
3458 *e++ = base64_value_to_char[0x3f & c >> 2];
3459 value = (0x03 & c) << 4;
3461 /* Process second byte of a triplet. */
3462 if (!ADVANCE_INPUT (c, istream))
3464 *e++ = base64_value_to_char[value];
3470 *e++ = base64_value_to_char[value | (0x0f & c >> 4)];
3471 value = (0x0f & c) << 2;
3473 /* Process third byte of a triplet. */
3474 if (!ADVANCE_INPUT (c, istream))
3476 *e++ = base64_value_to_char[value];
3481 *e++ = base64_value_to_char[value | (0x03 & c >> 6)];
3482 *e++ = base64_value_to_char[0x3f & c];
3487 #undef ADVANCE_INPUT
3489 /* Get next character from the stream, except that non-base64
3490 characters are ignored. This is in accordance with rfc2045. EC
3491 should be an Emchar, so that it can hold -1 as the value for EOF. */
3492 #define ADVANCE_INPUT_IGNORE_NONBASE64(ec, stream, streampos) do { \
3493 ec = Lstream_get_emchar (stream); \
3495 /* IS_BASE64 may not be called with negative arguments so check for \
3497 if (ec < 0 || IS_BASE64 (ec) || ec == '=') \
3501 #define STORE_BYTE(pos, val, ccnt) do { \
3502 pos += set_charptr_emchar (pos, (Emchar)((unsigned char)(val))); \
3507 base64_decode_1 (Lstream *istream, Bufbyte *to, Charcount *ccptr)
3511 EMACS_INT streampos = 0;
3516 unsigned long value;
3518 /* Process first byte of a quadruplet. */
3519 ADVANCE_INPUT_IGNORE_NONBASE64 (ec, istream, streampos);
3523 signal_simple_error ("Illegal `=' character while decoding base64",
3524 make_int (streampos));
3525 value = base64_char_to_value[ec] << 18;
3527 /* Process second byte of a quadruplet. */
3528 ADVANCE_INPUT_IGNORE_NONBASE64 (ec, istream, streampos);
3530 error ("Premature EOF while decoding base64");
3532 signal_simple_error ("Illegal `=' character while decoding base64",
3533 make_int (streampos));
3534 value |= base64_char_to_value[ec] << 12;
3535 STORE_BYTE (e, value >> 16, ccnt);
3537 /* Process third byte of a quadruplet. */
3538 ADVANCE_INPUT_IGNORE_NONBASE64 (ec, istream, streampos);
3540 error ("Premature EOF while decoding base64");
3544 ADVANCE_INPUT_IGNORE_NONBASE64 (ec, istream, streampos);
3546 error ("Premature EOF while decoding base64");
3548 signal_simple_error ("Padding `=' expected but not found while decoding base64",
3549 make_int (streampos));
3553 value |= base64_char_to_value[ec] << 6;
3554 STORE_BYTE (e, 0xff & value >> 8, ccnt);
3556 /* Process fourth byte of a quadruplet. */
3557 ADVANCE_INPUT_IGNORE_NONBASE64 (ec, istream, streampos);
3559 error ("Premature EOF while decoding base64");
3563 value |= base64_char_to_value[ec];
3564 STORE_BYTE (e, 0xff & value, ccnt);
3570 #undef ADVANCE_INPUT
3571 #undef ADVANCE_INPUT_IGNORE_NONBASE64
3575 free_malloced_ptr (Lisp_Object unwind_obj)
3577 void *ptr = (void *)get_opaque_ptr (unwind_obj);
3579 free_opaque_ptr (unwind_obj);
3583 /* Don't use alloca for regions larger than this, lest we overflow
3585 #define MAX_ALLOCA 65536
3587 /* We need to setup proper unwinding, because there is a number of
3588 ways these functions can blow up, and we don't want to have memory
3589 leaks in those cases. */
3590 #define XMALLOC_OR_ALLOCA(ptr, len, type) do { \
3591 size_t XOA_len = (len); \
3592 if (XOA_len > MAX_ALLOCA) \
3594 ptr = xnew_array (type, XOA_len); \
3595 record_unwind_protect (free_malloced_ptr, \
3596 make_opaque_ptr ((void *)ptr)); \
3599 ptr = alloca_array (type, XOA_len); \
3602 #define XMALLOC_UNBIND(ptr, len, speccount) do { \
3603 if ((len) > MAX_ALLOCA) \
3604 unbind_to (speccount, Qnil); \
3607 DEFUN ("base64-encode-region", Fbase64_encode_region, 2, 3, "r", /*
3608 Base64-encode the region between START and END.
3609 Return the length of the encoded text.
3610 Optional third argument NO-LINE-BREAK means do not break long lines
3613 (start, end, no_line_break))
3616 Bytind encoded_length;
3617 Charcount allength, length;
3618 struct buffer *buf = current_buffer;
3619 Bufpos begv, zv, old_pt = BUF_PT (buf);
3621 int speccount = specpdl_depth();
3623 get_buffer_range_char (buf, start, end, &begv, &zv, 0);
3624 barf_if_buffer_read_only (buf, begv, zv);
3626 /* We need to allocate enough room for encoding the text.
3627 We need 33 1/3% more space, plus a newline every 76
3628 characters, and then we round up. */
3630 allength = length + length/3 + 1;
3631 allength += allength / MIME_LINE_LENGTH + 1 + 6;
3633 input = make_lisp_buffer_input_stream (buf, begv, zv, 0);
3634 /* We needn't multiply allength with MAX_EMCHAR_LEN because all the
3635 base64 characters will be single-byte. */
3636 XMALLOC_OR_ALLOCA (encoded, allength, Bufbyte);
3637 encoded_length = base64_encode_1 (XLSTREAM (input), encoded,
3638 NILP (no_line_break));
3639 if (encoded_length > allength)
3641 Lstream_delete (XLSTREAM (input));
3643 /* Now we have encoded the region, so we insert the new contents
3644 and delete the old. (Insert first in order to preserve markers.) */
3645 buffer_insert_raw_string_1 (buf, begv, encoded, encoded_length, 0);
3646 XMALLOC_UNBIND (encoded, allength, speccount);
3647 buffer_delete_range (buf, begv + encoded_length, zv + encoded_length, 0);
3649 /* Simulate FSF Emacs implementation of this function: if point was
3650 in the region, place it at the beginning. */
3651 if (old_pt >= begv && old_pt < zv)
3652 BUF_SET_PT (buf, begv);
3654 /* We return the length of the encoded text. */
3655 return make_int (encoded_length);
3658 DEFUN ("base64-encode-string", Fbase64_encode_string, 1, 2, 0, /*
3659 Base64 encode STRING and return the result.
3660 Optional argument NO-LINE-BREAK means do not break long lines
3663 (string, no_line_break))
3665 Charcount allength, length;
3666 Bytind encoded_length;
3668 Lisp_Object input, result;
3669 int speccount = specpdl_depth();
3671 CHECK_STRING (string);
3673 length = XSTRING_CHAR_LENGTH (string);
3674 allength = length + length/3 + 1;
3675 allength += allength / MIME_LINE_LENGTH + 1 + 6;
3677 input = make_lisp_string_input_stream (string, 0, -1);
3678 XMALLOC_OR_ALLOCA (encoded, allength, Bufbyte);
3679 encoded_length = base64_encode_1 (XLSTREAM (input), encoded,
3680 NILP (no_line_break));
3681 if (encoded_length > allength)
3683 Lstream_delete (XLSTREAM (input));
3684 result = make_string (encoded, encoded_length);
3685 XMALLOC_UNBIND (encoded, allength, speccount);
3689 DEFUN ("base64-decode-region", Fbase64_decode_region, 2, 2, "r", /*
3690 Base64-decode the region between START and END.
3691 Return the length of the decoded text.
3692 If the region can't be decoded, return nil and don't modify the buffer.
3693 Characters out of the base64 alphabet are ignored.
3697 struct buffer *buf = current_buffer;
3698 Bufpos begv, zv, old_pt = BUF_PT (buf);
3700 Bytind decoded_length;
3701 Charcount length, cc_decoded_length;
3703 int speccount = specpdl_depth();
3705 get_buffer_range_char (buf, start, end, &begv, &zv, 0);
3706 barf_if_buffer_read_only (buf, begv, zv);
3710 input = make_lisp_buffer_input_stream (buf, begv, zv, 0);
3711 /* We need to allocate enough room for decoding the text. */
3712 XMALLOC_OR_ALLOCA (decoded, length * MAX_EMCHAR_LEN, Bufbyte);
3713 decoded_length = base64_decode_1 (XLSTREAM (input), decoded, &cc_decoded_length);
3714 if (decoded_length > length * MAX_EMCHAR_LEN)
3716 Lstream_delete (XLSTREAM (input));
3718 /* Now we have decoded the region, so we insert the new contents
3719 and delete the old. (Insert first in order to preserve markers.) */
3720 BUF_SET_PT (buf, begv);
3721 buffer_insert_raw_string_1 (buf, begv, decoded, decoded_length, 0);
3722 XMALLOC_UNBIND (decoded, length * MAX_EMCHAR_LEN, speccount);
3723 buffer_delete_range (buf, begv + cc_decoded_length,
3724 zv + cc_decoded_length, 0);
3726 /* Simulate FSF Emacs implementation of this function: if point was
3727 in the region, place it at the beginning. */
3728 if (old_pt >= begv && old_pt < zv)
3729 BUF_SET_PT (buf, begv);
3731 return make_int (cc_decoded_length);
3734 DEFUN ("base64-decode-string", Fbase64_decode_string, 1, 1, 0, /*
3735 Base64-decode STRING and return the result.
3736 Characters out of the base64 alphabet are ignored.
3741 Bytind decoded_length;
3742 Charcount length, cc_decoded_length;
3743 Lisp_Object input, result;
3744 int speccount = specpdl_depth();
3746 CHECK_STRING (string);
3748 length = XSTRING_CHAR_LENGTH (string);
3749 /* We need to allocate enough room for decoding the text. */
3750 XMALLOC_OR_ALLOCA (decoded, length * MAX_EMCHAR_LEN, Bufbyte);
3752 input = make_lisp_string_input_stream (string, 0, -1);
3753 decoded_length = base64_decode_1 (XLSTREAM (input), decoded,
3754 &cc_decoded_length);
3755 if (decoded_length > length * MAX_EMCHAR_LEN)
3757 Lstream_delete (XLSTREAM (input));
3759 result = make_string (decoded, decoded_length);
3760 XMALLOC_UNBIND (decoded, length * MAX_EMCHAR_LEN, speccount);
3764 Lisp_Object Qyes_or_no_p;
3769 INIT_LRECORD_IMPLEMENTATION (bit_vector);
3771 defsymbol (&Qstring_lessp, "string-lessp");
3772 defsymbol (&Qidentity, "identity");
3773 defsymbol (&Qyes_or_no_p, "yes-or-no-p");
3775 DEFSUBR (Fidentity);
3778 DEFSUBR (Fsafe_length);
3779 DEFSUBR (Fstring_equal);
3780 DEFSUBR (Fstring_lessp);
3781 DEFSUBR (Fstring_modified_tick);
3785 DEFSUBR (Fbvconcat);
3786 DEFSUBR (Fcopy_list);
3787 DEFSUBR (Fcopy_sequence);
3788 DEFSUBR (Fcopy_alist);
3789 DEFSUBR (Fcopy_tree);
3790 DEFSUBR (Fsubstring);
3797 DEFSUBR (Fnbutlast);
3799 DEFSUBR (Fold_member);
3801 DEFSUBR (Fold_memq);
3803 DEFSUBR (Fold_assoc);
3805 DEFSUBR (Fold_assq);
3807 DEFSUBR (Fold_rassoc);
3809 DEFSUBR (Fold_rassq);
3811 DEFSUBR (Fold_delete);
3813 DEFSUBR (Fold_delq);
3814 DEFSUBR (Fremassoc);
3816 DEFSUBR (Fremrassoc);
3817 DEFSUBR (Fremrassq);
3818 DEFSUBR (Fnreverse);
3821 DEFSUBR (Fplists_eq);
3822 DEFSUBR (Fplists_equal);
3823 DEFSUBR (Flax_plists_eq);
3824 DEFSUBR (Flax_plists_equal);
3825 DEFSUBR (Fplist_get);
3826 DEFSUBR (Fplist_put);
3827 DEFSUBR (Fplist_remprop);
3828 DEFSUBR (Fplist_member);
3829 DEFSUBR (Fcheck_valid_plist);
3830 DEFSUBR (Fvalid_plist_p);
3831 DEFSUBR (Fcanonicalize_plist);
3832 DEFSUBR (Flax_plist_get);
3833 DEFSUBR (Flax_plist_put);
3834 DEFSUBR (Flax_plist_remprop);
3835 DEFSUBR (Flax_plist_member);
3836 DEFSUBR (Fcanonicalize_lax_plist);
3837 DEFSUBR (Fdestructive_alist_to_plist);
3841 DEFSUBR (Fobject_plist);
3843 DEFSUBR (Fold_equal);
3844 DEFSUBR (Ffillarray);
3847 DEFSUBR (Fmapvector);
3848 DEFSUBR (Fmapc_internal);
3849 DEFSUBR (Fmapconcat);
3850 DEFSUBR (Freplace_list);
3851 DEFSUBR (Fload_average);
3852 DEFSUBR (Ffeaturep);
3855 DEFSUBR (Fbase64_encode_region);
3856 DEFSUBR (Fbase64_encode_string);
3857 DEFSUBR (Fbase64_decode_region);
3858 DEFSUBR (Fbase64_decode_string);
3862 init_provide_once (void)
3864 DEFVAR_LISP ("features", &Vfeatures /*
3865 A list of symbols which are the features of the executing emacs.
3866 Used by `featurep' and `require', and altered by `provide'.
3870 Fprovide (intern ("base64"));