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. */
56 /* NOTE: This symbol is also used in lread.c */
57 #define FEATUREP_SYNTAX
59 Lisp_Object Qstring_lessp;
60 Lisp_Object Qidentity;
62 static int internal_old_equal (Lisp_Object, Lisp_Object, int);
65 mark_bit_vector (Lisp_Object obj, void (*markobj) (Lisp_Object))
71 print_bit_vector (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
74 struct Lisp_Bit_Vector *v = XBIT_VECTOR (obj);
75 int len = bit_vector_length (v);
78 if (INTP (Vprint_length))
79 last = min (len, XINT (Vprint_length));
80 write_c_string ("#*", printcharfun);
81 for (i = 0; i < last; i++)
83 if (bit_vector_bit (v, i))
84 write_c_string ("1", printcharfun);
86 write_c_string ("0", printcharfun);
90 write_c_string ("...", printcharfun);
94 bit_vector_equal (Lisp_Object o1, Lisp_Object o2, int depth)
96 struct Lisp_Bit_Vector *v1 = XBIT_VECTOR (o1);
97 struct Lisp_Bit_Vector *v2 = XBIT_VECTOR (o2);
99 return ((bit_vector_length (v1) == bit_vector_length (v2)) &&
100 !memcmp (v1->bits, v2->bits,
101 BIT_VECTOR_LONG_STORAGE (bit_vector_length (v1)) *
106 bit_vector_hash (Lisp_Object obj, int depth)
108 struct Lisp_Bit_Vector *v = XBIT_VECTOR (obj);
109 return HASH2 (bit_vector_length (v),
110 memory_hash (v->bits,
111 BIT_VECTOR_LONG_STORAGE (bit_vector_length (v)) *
115 DEFINE_BASIC_LRECORD_IMPLEMENTATION ("bit-vector", bit_vector,
116 mark_bit_vector, print_bit_vector, 0,
117 bit_vector_equal, bit_vector_hash,
118 struct Lisp_Bit_Vector);
120 DEFUN ("identity", Fidentity, 1, 1, 0, /*
121 Return the argument unchanged.
128 extern long get_random (void);
129 extern void seed_random (long arg);
131 DEFUN ("random", Frandom, 0, 1, 0, /*
132 Return a pseudo-random number.
133 All integers representable in Lisp are equally likely.
134 On most systems, this is 28 bits' worth.
135 With positive integer argument N, return random number in interval [0,N).
136 With argument t, set the random number seed from the current time and pid.
141 unsigned long denominator;
144 seed_random (getpid () + time (NULL));
145 if (NATNUMP (limit) && !ZEROP (limit))
147 /* Try to take our random number from the higher bits of VAL,
148 not the lower, since (says Gentzel) the low bits of `random'
149 are less random than the higher ones. We do this by using the
150 quotient rather than the remainder. At the high end of the RNG
151 it's possible to get a quotient larger than limit; discarding
152 these values eliminates the bias that would otherwise appear
153 when using a large limit. */
154 denominator = ((unsigned long)1 << VALBITS) / XINT (limit);
156 val = get_random () / denominator;
157 while (val >= XINT (limit));
162 return make_int (val);
165 /* Random data-structure functions */
167 #ifdef LOSING_BYTECODE
169 /* #### Delete this shit */
171 /* Charcount is a misnomer here as we might be dealing with the
172 length of a vector or list, but emphasizes that we're not dealing
173 with Bytecounts in strings */
175 length_with_bytecode_hack (Lisp_Object seq)
177 if (!COMPILED_FUNCTIONP (seq))
178 return XINT (Flength (seq));
181 struct Lisp_Compiled_Function *b = XCOMPILED_FUNCTION (seq);
183 return (b->flags.interactivep ? COMPILED_INTERACTIVE :
184 b->flags.domainp ? COMPILED_DOMAIN :
190 #endif /* LOSING_BYTECODE */
193 check_losing_bytecode (CONST char *function, Lisp_Object seq)
195 if (COMPILED_FUNCTIONP (seq))
198 "As of 20.3, `%s' no longer works with compiled-function objects",
202 DEFUN ("length", Flength, 1, 1, 0, /*
203 Return the length of vector, bit vector, list or string SEQUENCE.
208 if (STRINGP (sequence))
209 return make_int (XSTRING_CHAR_LENGTH (sequence));
210 else if (CONSP (sequence))
215 EXTERNAL_LIST_LOOP (tail, sequence)
223 else if (VECTORP (sequence))
224 return make_int (XVECTOR_LENGTH (sequence));
225 else if (NILP (sequence))
227 else if (BIT_VECTORP (sequence))
228 return make_int (bit_vector_length (XBIT_VECTOR (sequence)));
231 check_losing_bytecode ("length", sequence);
232 sequence = wrong_type_argument (Qsequencep, sequence);
237 /* This does not check for quits. That is safe
238 since it must terminate. */
240 DEFUN ("safe-length", Fsafe_length, 1, 1, 0, /*
241 Return the length of a list, but avoid error or infinite loop.
242 This function never gets an error. If LIST is not really a list,
243 it returns 0. If LIST is circular, it returns a finite value
244 which is at least the number of distinct elements.
248 Lisp_Object halftail = list; /* Used to detect circular lists. */
252 for (tail = list; CONSP (tail); tail = XCDR (tail))
254 if (EQ (tail, halftail) && len != 0)
258 halftail = XCDR (halftail);
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 struct Lisp_String *p1, *p2;
280 p1 = XSYMBOL (s1)->name;
288 p2 = XSYMBOL (s2)->name;
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 struct Lisp_String *p1, *p2;
329 p1 = XSYMBOL (s1)->name;
337 p2 = XSYMBOL (s2)->name;
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
355 Bytecount bcend = charcount_to_bytecount (string_data (p1), end);
356 /* Compare strings using collation order of locale. */
357 /* Need to be tricky to handle embedded nulls. */
359 for (i = 0; i < bcend; i += strlen((char *) string_data (p1) + i) + 1)
361 int val = strcoll ((char *) string_data (p1) + i,
362 (char *) string_data (p2) + i);
368 #else /* not I18N2, or MULE */
369 /* #### It is not really necessary to do this: We could compare
370 byte-by-byte and still get a reasonable comparison, since this
371 would compare characters with a charset in the same way.
372 With a little rearrangement of the leading bytes, we could
373 make most inter-charset comparisons work out the same, too;
374 even if some don't, this is not a big deal because inter-charset
375 comparisons aren't really well-defined anyway. */
376 for (i = 0; i < end; i++)
378 if (string_char (p1, i) != string_char (p2, i))
379 return string_char (p1, i) < string_char (p2, i) ? Qt : Qnil;
381 #endif /* not I18N2, or MULE */
382 /* Can't do i < len2 because then comparison between "foo" and "foo^@"
383 won't work right in I18N2 case */
384 return end < len2 ? Qt : Qnil;
387 DEFUN ("string-modified-tick", Fstring_modified_tick, 1, 1, 0, /*
388 Return STRING's tick counter, incremented for each change to the string.
389 Each string has a tick counter which is incremented each time the contents
390 of the string are changed (e.g. with `aset'). It wraps around occasionally.
394 struct Lisp_String *s;
396 CHECK_STRING (string);
397 s = XSTRING (string);
398 if (CONSP (s->plist) && INTP (XCAR (s->plist)))
399 return XCAR (s->plist);
405 bump_string_modiff (Lisp_Object str)
407 struct Lisp_String *s = XSTRING (str);
408 Lisp_Object *ptr = &s->plist;
411 /* #### remove the `string-translatable' property from the string,
414 /* skip over extent info if it's there */
415 if (CONSP (*ptr) && EXTENT_INFOP (XCAR (*ptr)))
417 if (CONSP (*ptr) && INTP (XCAR (*ptr)))
418 XSETINT (XCAR (*ptr), 1+XINT (XCAR (*ptr)));
420 *ptr = Fcons (make_int (1), *ptr);
424 enum concat_target_type { c_cons, c_string, c_vector, c_bit_vector };
425 static Lisp_Object concat (int nargs, Lisp_Object *args,
426 enum concat_target_type target_type,
430 concat2 (Lisp_Object s1, Lisp_Object s2)
435 return concat (2, args, c_string, 0);
439 concat3 (Lisp_Object s1, Lisp_Object s2, Lisp_Object s3)
445 return concat (3, args, c_string, 0);
449 vconcat2 (Lisp_Object s1, Lisp_Object s2)
454 return concat (2, args, c_vector, 0);
458 vconcat3 (Lisp_Object s1, Lisp_Object s2, Lisp_Object s3)
464 return concat (3, args, c_vector, 0);
467 DEFUN ("append", Fappend, 0, MANY, 0, /*
468 Concatenate all the arguments and make the result a list.
469 The result is a list whose elements are the elements of all the arguments.
470 Each argument may be a list, vector, bit vector, or string.
471 The last argument is not copied, just used as the tail of the new list.
474 (int nargs, Lisp_Object *args))
476 return concat (nargs, args, c_cons, 1);
479 DEFUN ("concat", Fconcat, 0, MANY, 0, /*
480 Concatenate all the arguments and make the result a string.
481 The result is a string whose elements are the elements of all the arguments.
482 Each argument may be a string or a list or vector of characters.
484 As of XEmacs 21.0, this function does NOT accept individual integers
485 as arguments. Old code that relies on, for example, (concat "foo" 50)
486 returning "foo50" will fail. To fix such code, either apply
487 `int-to-string' to the integer argument, or use `format'.
489 (int nargs, Lisp_Object *args))
491 return concat (nargs, args, c_string, 0);
494 DEFUN ("vconcat", Fvconcat, 0, MANY, 0, /*
495 Concatenate all the arguments and make the result a vector.
496 The result is a vector whose elements are the elements of all the arguments.
497 Each argument may be a list, vector, bit vector, or string.
499 (int nargs, Lisp_Object *args))
501 return concat (nargs, args, c_vector, 0);
504 DEFUN ("bvconcat", Fbvconcat, 0, MANY, 0, /*
505 Concatenate all the arguments and make the result a bit vector.
506 The result is a bit vector whose elements are the elements of all the
507 arguments. Each argument may be a list, vector, bit vector, or string.
509 (int nargs, Lisp_Object *args))
511 return concat (nargs, args, c_bit_vector, 0);
514 DEFUN ("copy-sequence", Fcopy_sequence, 1, 1, 0, /*
515 Return a copy of a list, vector, bit vector or string.
516 The elements of a list or vector are not copied; they are shared
522 if (NILP (arg)) return arg;
523 /* We handle conses separately because concat() is big and hairy and
524 doesn't handle (copy-sequence '(a b . c)) and it's easier to redo this
525 than to fix concat() without worrying about breaking other things.
529 Lisp_Object head = Fcons (XCAR (arg), XCDR (arg));
530 Lisp_Object tail = head;
532 for (arg = XCDR (arg); CONSP (arg); arg = XCDR (arg))
534 XCDR (tail) = Fcons (XCAR (arg), XCDR (arg));
540 if (STRINGP (arg)) return concat (1, &arg, c_string, 0);
541 if (VECTORP (arg)) return concat (1, &arg, c_vector, 0);
542 if (BIT_VECTORP (arg)) return concat (1, &arg, c_bit_vector, 0);
544 check_losing_bytecode ("copy-sequence", arg);
545 arg = wrong_type_argument (Qsequencep, arg);
549 struct merge_string_extents_struct
552 Bytecount entry_offset;
553 Bytecount entry_length;
557 concat (int nargs, Lisp_Object *args,
558 enum concat_target_type target_type,
562 Lisp_Object tail = Qnil;
565 Lisp_Object last_tail;
567 struct merge_string_extents_struct *args_mse = 0;
568 Bufbyte *string_result = 0;
569 Bufbyte *string_result_ptr = 0;
572 /* The modus operandi in Emacs is "caller gc-protects args".
573 However, concat is called many times in Emacs on freshly
574 created stuff. So we help those callers out by protecting
575 the args ourselves to save them a lot of temporary-variable
579 gcpro1.nvars = nargs;
582 /* #### if the result is a string and any of the strings have a string
583 for the `string-translatable' property, then concat should also
584 concat the args but use the `string-translatable' strings, and store
585 the result in the returned string's `string-translatable' property. */
587 if (target_type == c_string)
588 args_mse = alloca_array (struct merge_string_extents_struct, nargs);
590 /* In append, the last arg isn't treated like the others */
591 if (last_special && nargs > 0)
594 last_tail = args[nargs];
599 /* Check and coerce the arguments. */
600 for (argnum = 0; argnum < nargs; argnum++)
602 Lisp_Object seq = args[argnum];
605 else if (VECTORP (seq) || STRINGP (seq) || BIT_VECTORP (seq))
607 #ifdef LOSING_BYTECODE
608 else if (COMPILED_FUNCTIONP (seq))
609 /* Urk! We allow this, for "compatibility"... */
612 #if 0 /* removed for XEmacs 21 */
614 /* This is too revolting to think about but maintains
615 compatibility with FSF (and lots and lots of old code). */
616 args[argnum] = Fnumber_to_string (seq);
620 check_losing_bytecode ("concat", seq);
621 args[argnum] = wrong_type_argument (Qsequencep, seq);
627 args_mse[argnum].string = seq;
629 args_mse[argnum].string = Qnil;
634 /* Charcount is a misnomer here as we might be dealing with the
635 length of a vector or list, but emphasizes that we're not dealing
636 with Bytecounts in strings */
637 Charcount total_length;
639 for (argnum = 0, total_length = 0; argnum < nargs; argnum++)
641 #ifdef LOSING_BYTECODE
642 Charcount thislen = length_with_bytecode_hack (args[argnum]);
644 Charcount thislen = XINT (Flength (args[argnum]));
646 total_length += thislen;
652 if (total_length == 0)
653 /* In append, if all but last arg are nil, return last arg */
654 RETURN_UNGCPRO (last_tail);
655 val = Fmake_list (make_int (total_length), Qnil);
658 val = make_vector (total_length, Qnil);
661 val = make_bit_vector (total_length, Qzero);
664 /* We don't make the string yet because we don't know the
665 actual number of bytes. This loop was formerly written
666 to call Fmake_string() here and then call set_string_char()
667 for each char. This seems logical enough but is waaaaaaaay
668 slow -- set_string_char() has to scan the whole string up
669 to the place where the substitution is called for in order
670 to find the place to change, and may have to do some
671 realloc()ing in order to make the char fit properly.
674 string_result = (Bufbyte *) alloca (total_length * MAX_EMCHAR_LEN);
675 string_result_ptr = string_result;
684 tail = val, toindex = -1; /* -1 in toindex is flag we are
691 for (argnum = 0; argnum < nargs; argnum++)
693 Charcount thisleni = 0;
694 Charcount thisindex = 0;
695 Lisp_Object seq = args[argnum];
696 Bufbyte *string_source_ptr = 0;
697 Bufbyte *string_prev_result_ptr = string_result_ptr;
701 #ifdef LOSING_BYTECODE
702 thisleni = length_with_bytecode_hack (seq);
704 thisleni = XINT (Flength (seq));
708 string_source_ptr = XSTRING_DATA (seq);
714 /* We've come to the end of this arg, so exit. */
718 /* Fetch next element of `seq' arg into `elt' */
726 if (thisindex >= thisleni)
731 elt = make_char (charptr_emchar (string_source_ptr));
732 INC_CHARPTR (string_source_ptr);
734 else if (VECTORP (seq))
735 elt = XVECTOR_DATA (seq)[thisindex];
736 else if (BIT_VECTORP (seq))
737 elt = make_int (bit_vector_bit (XBIT_VECTOR (seq),
740 elt = Felt (seq, make_int (thisindex));
744 /* Store into result */
747 /* toindex negative means we are making a list */
752 else if (VECTORP (val))
753 XVECTOR_DATA (val)[toindex++] = elt;
754 else if (BIT_VECTORP (val))
757 set_bit_vector_bit (XBIT_VECTOR (val), toindex++, XINT (elt));
761 CHECK_CHAR_COERCE_INT (elt);
762 string_result_ptr += set_charptr_emchar (string_result_ptr,
768 args_mse[argnum].entry_offset =
769 string_prev_result_ptr - string_result;
770 args_mse[argnum].entry_length =
771 string_result_ptr - string_prev_result_ptr;
775 /* Now we finally make the string. */
776 if (target_type == c_string)
778 val = make_string (string_result, string_result_ptr - string_result);
779 for (argnum = 0; argnum < nargs; argnum++)
781 if (STRINGP (args_mse[argnum].string))
782 copy_string_extents (val, args_mse[argnum].string,
783 args_mse[argnum].entry_offset, 0,
784 args_mse[argnum].entry_length);
789 XCDR (prev) = last_tail;
791 RETURN_UNGCPRO (val);
794 DEFUN ("copy-alist", Fcopy_alist, 1, 1, 0, /*
795 Return a copy of ALIST.
796 This is an alist which represents the same mapping from objects to objects,
797 but does not share the alist structure with ALIST.
798 The objects mapped (cars and cdrs of elements of the alist)
800 Elements of ALIST that are not conses are also shared.
810 alist = concat (1, &alist, c_cons, 0);
811 for (tail = alist; CONSP (tail); tail = XCDR (tail))
813 Lisp_Object car = XCAR (tail);
816 XCAR (tail) = Fcons (XCAR (car), XCDR (car));
821 DEFUN ("copy-tree", Fcopy_tree, 1, 2, 0, /*
822 Return a copy of a list and substructures.
823 The argument is copied, and any lists contained within it are copied
824 recursively. Circularities and shared substructures are not preserved.
825 Second arg VECP causes vectors to be copied, too. Strings and bit vectors
833 rest = arg = Fcopy_sequence (arg);
836 Lisp_Object elt = XCAR (rest);
838 if (CONSP (elt) || VECTORP (elt))
839 XCAR (rest) = Fcopy_tree (elt, vecp);
840 if (VECTORP (XCDR (rest))) /* hack for (a b . [c d]) */
841 XCDR (rest) = Fcopy_tree (XCDR (rest), vecp);
845 else if (VECTORP (arg) && ! NILP (vecp))
847 int i = XVECTOR_LENGTH (arg);
849 arg = Fcopy_sequence (arg);
850 for (j = 0; j < i; j++)
852 Lisp_Object elt = XVECTOR_DATA (arg) [j];
854 if (CONSP (elt) || VECTORP (elt))
855 XVECTOR_DATA (arg) [j] = Fcopy_tree (elt, vecp);
861 DEFUN ("substring", Fsubstring, 2, 3, 0, /*
862 Return a substring of STRING, starting at index FROM and ending before TO.
863 TO may be nil or omitted; then the substring runs to the end of STRING.
864 If FROM or TO is negative, it counts from the end.
865 Relevant parts of the string-extent-data are copied in the new string.
869 Charcount ccfr, ccto;
873 CHECK_STRING (string);
874 /* Historically, FROM could not be omitted. Whatever ... */
876 get_string_range_char (string, from, to, &ccfr, &ccto,
877 GB_HISTORICAL_STRING_BEHAVIOR);
878 bfr = charcount_to_bytecount (XSTRING_DATA (string), ccfr);
879 bto = charcount_to_bytecount (XSTRING_DATA (string), ccto);
880 val = make_string (XSTRING_DATA (string) + bfr, bto - bfr);
881 /* Copy any applicable extent information into the new string: */
882 copy_string_extents (val, string, 0, bfr, bto - bfr);
886 DEFUN ("subseq", Fsubseq, 2, 3, 0, /*
887 Return a subsequence of SEQ, starting at index FROM and ending before TO.
888 TO may be nil or omitted; then the subsequence runs to the end of SEQ.
889 If FROM or TO is negative, it counts from the end.
890 The resulting subsequence is always the same type as the original
892 If SEQ is a string, relevant parts of the string-extent-data are copied
900 return Fsubstring (seq, from, to);
902 if (!LISTP (seq) && !VECTORP (seq) && !BIT_VECTORP (seq))
904 check_losing_bytecode ("subseq", seq);
905 seq = wrong_type_argument (Qsequencep, seq);
908 len = XINT (Flength (seq));
925 if (!(0 <= f && f <= t && t <= len))
926 args_out_of_range_3 (seq, make_int (f), make_int (t));
930 Lisp_Object result = make_vector (t - f, Qnil);
932 Lisp_Object *in_elts = XVECTOR_DATA (seq);
933 Lisp_Object *out_elts = XVECTOR_DATA (result);
935 for (i = f; i < t; i++)
936 out_elts[i - f] = in_elts[i];
942 Lisp_Object result = Qnil;
945 seq = Fnthcdr (make_int (f), seq);
947 for (i = f; i < t; i++)
949 result = Fcons (Fcar (seq), result);
953 return Fnreverse (result);
958 Lisp_Object result = make_bit_vector (t - f, Qzero);
961 for (i = f; i < t; i++)
962 set_bit_vector_bit (XBIT_VECTOR (result), i - f,
963 bit_vector_bit (XBIT_VECTOR (seq), i));
969 DEFUN ("nthcdr", Fnthcdr, 2, 2, 0, /*
970 Take cdr N times on LIST, and return the result.
975 REGISTER Lisp_Object tail = list;
977 for (i = XINT (n); i; i--)
981 else if (NILP (tail))
985 tail = wrong_type_argument (Qlistp, tail);
992 DEFUN ("nth", Fnth, 2, 2, 0, /*
993 Return the Nth element of LIST.
994 N counts from zero. If LIST is not that long, nil is returned.
998 return Fcar (Fnthcdr (n, list));
1001 DEFUN ("elt", Felt, 2, 2, 0, /*
1002 Return element of SEQUENCE at index N.
1007 CHECK_INT_COERCE_CHAR (n); /* yuck! */
1008 if (LISTP (sequence))
1010 Lisp_Object tem = Fnthcdr (n, sequence);
1011 /* #### Utterly, completely, fucking disgusting.
1012 * #### The whole point of "elt" is that it operates on
1013 * #### sequences, and does error- (bounds-) checking.
1019 /* This is The Way It Has Always Been. */
1022 /* This is The Way Mly and Cltl2 say It Should Be. */
1023 args_out_of_range (sequence, n);
1026 else if (STRINGP (sequence)
1027 || VECTORP (sequence)
1028 || BIT_VECTORP (sequence))
1029 return Faref (sequence, n);
1030 #ifdef LOSING_BYTECODE
1031 else if (COMPILED_FUNCTIONP (sequence))
1037 args_out_of_range (sequence, n);
1039 /* Utter perversity */
1041 struct Lisp_Compiled_Function *b = XCOMPILED_FUNCTION (sequence);
1044 case COMPILED_ARGLIST:
1046 case COMPILED_BYTECODE:
1047 return b->bytecodes;
1048 case COMPILED_CONSTANTS:
1049 return b->constants;
1050 case COMPILED_STACK_DEPTH:
1051 return make_int (b->maxdepth);
1052 case COMPILED_DOC_STRING:
1053 return compiled_function_documentation (b);
1054 case COMPILED_DOMAIN:
1055 return compiled_function_domain (b);
1056 case COMPILED_INTERACTIVE:
1057 if (b->flags.interactivep)
1058 return compiled_function_interactive (b);
1059 /* if we return nil, can't tell interactive with no args
1060 from noninteractive. */
1067 #endif /* LOSING_BYTECODE */
1070 check_losing_bytecode ("elt", sequence);
1071 sequence = wrong_type_argument (Qsequencep, sequence);
1076 DEFUN ("member", Fmember, 2, 2, 0, /*
1077 Return non-nil if ELT is an element of LIST. Comparison done with `equal'.
1078 The value is actually the tail of LIST whose car is ELT.
1082 REGISTER Lisp_Object tail;
1083 LIST_LOOP (tail, list)
1085 CONCHECK_CONS (tail);
1086 if (internal_equal (elt, XCAR (tail), 0))
1093 DEFUN ("old-member", Fold_member, 2, 2, 0, /*
1094 Return non-nil if ELT is an element of LIST. Comparison done with `old-equal'.
1095 The value is actually the tail of LIST whose car is ELT.
1096 This function is provided only for byte-code compatibility with v19.
1101 REGISTER Lisp_Object tail;
1102 LIST_LOOP (tail, list)
1104 CONCHECK_CONS (tail);
1105 if (internal_old_equal (elt, XCAR (tail), 0))
1112 DEFUN ("memq", Fmemq, 2, 2, 0, /*
1113 Return non-nil if ELT is an element of LIST. Comparison done with `eq'.
1114 The value is actually the tail of LIST whose car is ELT.
1118 REGISTER Lisp_Object tail;
1119 LIST_LOOP (tail, list)
1121 REGISTER Lisp_Object tem;
1122 CONCHECK_CONS (tail);
1123 if (tem = XCAR (tail), EQ_WITH_EBOLA_NOTICE (elt, tem))
1130 DEFUN ("old-memq", Fold_memq, 2, 2, 0, /*
1131 Return non-nil if ELT is an element of LIST. Comparison done with `old-eq'.
1132 The value is actually the tail of LIST whose car is ELT.
1133 This function is provided only for byte-code compatibility with v19.
1138 REGISTER Lisp_Object tail;
1139 LIST_LOOP (tail, list)
1141 REGISTER Lisp_Object tem;
1142 CONCHECK_CONS (tail);
1143 if (tem = XCAR (tail), HACKEQ_UNSAFE (elt, tem))
1151 memq_no_quit (Lisp_Object elt, Lisp_Object list)
1153 REGISTER Lisp_Object tail;
1154 for (tail = list; CONSP (tail); tail = XCDR (tail))
1156 REGISTER Lisp_Object tem;
1157 if (tem = XCAR (tail), EQ_WITH_EBOLA_NOTICE (elt, tem))
1163 DEFUN ("assoc", Fassoc, 2, 2, 0, /*
1164 Return non-nil if KEY is `equal' to the car of an element of LIST.
1165 The value is actually the element of LIST whose car equals KEY.
1169 /* This function can GC. */
1170 REGISTER Lisp_Object tail;
1171 LIST_LOOP (tail, list)
1173 REGISTER Lisp_Object elt;
1174 CONCHECK_CONS (tail);
1176 if (CONSP (elt) && internal_equal (XCAR (elt), key, 0))
1183 DEFUN ("old-assoc", Fold_assoc, 2, 2, 0, /*
1184 Return non-nil if KEY is `old-equal' to the car of an element of LIST.
1185 The value is actually the element of LIST whose car equals KEY.
1189 /* This function can GC. */
1190 REGISTER Lisp_Object tail;
1191 LIST_LOOP (tail, list)
1193 REGISTER Lisp_Object elt;
1194 CONCHECK_CONS (tail);
1196 if (CONSP (elt) && internal_old_equal (XCAR (elt), key, 0))
1204 assoc_no_quit (Lisp_Object key, Lisp_Object list)
1206 int speccount = specpdl_depth ();
1207 specbind (Qinhibit_quit, Qt);
1208 return unbind_to (speccount, Fassoc (key, list));
1211 DEFUN ("assq", Fassq, 2, 2, 0, /*
1212 Return non-nil if KEY is `eq' to the car of an element of LIST.
1213 The value is actually the element of LIST whose car is KEY.
1214 Elements of LIST that are not conses are ignored.
1218 REGISTER Lisp_Object tail;
1219 LIST_LOOP (tail, list)
1221 REGISTER Lisp_Object elt, tem;
1222 CONCHECK_CONS (tail);
1224 if (CONSP (elt) && (tem = XCAR (elt), EQ_WITH_EBOLA_NOTICE (key, tem)))
1231 DEFUN ("old-assq", Fold_assq, 2, 2, 0, /*
1232 Return non-nil if KEY is `old-eq' to the car of an element of LIST.
1233 The value is actually the element of LIST whose car is KEY.
1234 Elements of LIST that are not conses are ignored.
1235 This function is provided only for byte-code compatibility with v19.
1240 REGISTER Lisp_Object tail;
1241 LIST_LOOP (tail, list)
1243 REGISTER Lisp_Object elt, tem;
1244 CONCHECK_CONS (tail);
1246 if (CONSP (elt) && (tem = XCAR (elt), HACKEQ_UNSAFE (key, tem)))
1253 /* Like Fassq but never report an error and do not allow quits.
1254 Use only on lists known never to be circular. */
1257 assq_no_quit (Lisp_Object key, Lisp_Object list)
1259 /* This cannot GC. */
1260 REGISTER Lisp_Object tail;
1261 for (tail = list; CONSP (tail); tail = XCDR (tail))
1263 REGISTER Lisp_Object tem, elt;
1265 if (CONSP (elt) && (tem = XCAR (elt), EQ_WITH_EBOLA_NOTICE (key, tem)))
1271 DEFUN ("rassoc", Frassoc, 2, 2, 0, /*
1272 Return non-nil if KEY is `equal' to the cdr of an element of LIST.
1273 The value is actually the element of LIST whose cdr equals KEY.
1277 REGISTER Lisp_Object tail;
1278 LIST_LOOP (tail, list)
1280 REGISTER Lisp_Object elt;
1281 CONCHECK_CONS (tail);
1283 if (CONSP (elt) && internal_equal (XCDR (elt), key, 0))
1290 DEFUN ("old-rassoc", Fold_rassoc, 2, 2, 0, /*
1291 Return non-nil if KEY is `old-equal' to the cdr of an element of LIST.
1292 The value is actually the element of LIST whose cdr equals KEY.
1296 REGISTER Lisp_Object tail;
1297 LIST_LOOP (tail, list)
1299 REGISTER Lisp_Object elt;
1300 CONCHECK_CONS (tail);
1302 if (CONSP (elt) && internal_old_equal (XCDR (elt), key, 0))
1309 DEFUN ("rassq", Frassq, 2, 2, 0, /*
1310 Return non-nil if KEY is `eq' to the cdr of an element of LIST.
1311 The value is actually the element of LIST whose cdr is KEY.
1315 REGISTER Lisp_Object tail;
1316 LIST_LOOP (tail, list)
1318 REGISTER Lisp_Object elt, tem;
1319 CONCHECK_CONS (tail);
1321 if (CONSP (elt) && (tem = XCDR (elt), EQ_WITH_EBOLA_NOTICE (key, tem)))
1328 DEFUN ("old-rassq", Fold_rassq, 2, 2, 0, /*
1329 Return non-nil if KEY is `old-eq' to the cdr of an element of LIST.
1330 The value is actually the element of LIST whose cdr is KEY.
1334 REGISTER Lisp_Object tail;
1335 LIST_LOOP (tail, list)
1337 REGISTER Lisp_Object elt, tem;
1338 CONCHECK_CONS (tail);
1340 if (CONSP (elt) && (tem = XCDR (elt), HACKEQ_UNSAFE (key, tem)))
1348 rassq_no_quit (Lisp_Object key, Lisp_Object list)
1350 REGISTER Lisp_Object tail;
1351 for (tail = list; CONSP (tail); tail = XCDR (tail))
1353 REGISTER Lisp_Object elt, tem;
1355 if (CONSP (elt) && (tem = XCDR (elt), EQ_WITH_EBOLA_NOTICE (key, tem)))
1362 DEFUN ("delete", Fdelete, 2, 2, 0, /*
1363 Delete by side effect any occurrences of ELT as a member of LIST.
1364 The modified LIST is returned. Comparison is done with `equal'.
1365 If the first member of LIST is ELT, there is no way to remove it by side
1366 effect; therefore, write `(setq foo (delete element foo))' to be sure
1367 of changing the value of `foo'.
1372 REGISTER Lisp_Object tail = list;
1373 REGISTER Lisp_Object prev = Qnil;
1375 while (!NILP (tail))
1377 CONCHECK_CONS (tail);
1378 if (internal_equal (elt, XCAR (tail), 0))
1383 XCDR (prev) = XCDR (tail);
1393 DEFUN ("old-delete", Fold_delete, 2, 2, 0, /*
1394 Delete by side effect any occurrences of ELT as a member of LIST.
1395 The modified LIST is returned. Comparison is done with `old-equal'.
1396 If the first member of LIST is ELT, there is no way to remove it by side
1397 effect; therefore, write `(setq foo (old-delete element foo))' to be sure
1398 of changing the value of `foo'.
1402 REGISTER Lisp_Object tail = list;
1403 REGISTER Lisp_Object prev = Qnil;
1405 while (!NILP (tail))
1407 CONCHECK_CONS (tail);
1408 if (internal_old_equal (elt, XCAR (tail), 0))
1413 XCDR (prev) = XCDR (tail);
1423 DEFUN ("delq", Fdelq, 2, 2, 0, /*
1424 Delete by side effect any occurrences of ELT as a member of LIST.
1425 The modified LIST is returned. Comparison is done with `eq'.
1426 If the first member of LIST is ELT, there is no way to remove it by side
1427 effect; therefore, write `(setq foo (delq element foo))' to be sure of
1428 changing the value of `foo'.
1432 REGISTER Lisp_Object tail = list;
1433 REGISTER Lisp_Object prev = Qnil;
1435 while (!NILP (tail))
1437 REGISTER Lisp_Object tem;
1438 CONCHECK_CONS (tail);
1439 if (tem = XCAR (tail), EQ_WITH_EBOLA_NOTICE (elt, tem))
1444 XCDR (prev) = XCDR (tail);
1454 DEFUN ("old-delq", Fold_delq, 2, 2, 0, /*
1455 Delete by side effect any occurrences of ELT as a member of LIST.
1456 The modified LIST is returned. Comparison is done with `old-eq'.
1457 If the first member of LIST is ELT, there is no way to remove it by side
1458 effect; therefore, write `(setq foo (old-delq element foo))' to be sure of
1459 changing the value of `foo'.
1463 REGISTER Lisp_Object tail = list;
1464 REGISTER Lisp_Object prev = Qnil;
1466 while (!NILP (tail))
1468 REGISTER Lisp_Object tem;
1469 CONCHECK_CONS (tail);
1470 if (tem = XCAR (tail), HACKEQ_UNSAFE (elt, tem))
1475 XCDR (prev) = XCDR (tail);
1485 /* no quit, no errors; be careful */
1488 delq_no_quit (Lisp_Object elt, Lisp_Object list)
1490 REGISTER Lisp_Object tail = list;
1491 REGISTER Lisp_Object prev = Qnil;
1493 while (CONSP (tail))
1495 REGISTER Lisp_Object tem;
1496 if (tem = XCAR (tail), EQ_WITH_EBOLA_NOTICE (elt, tem))
1501 XCDR (prev) = XCDR (tail);
1510 /* Be VERY careful with this. This is like delq_no_quit() but
1511 also calls free_cons() on the removed conses. You must be SURE
1512 that no pointers to the freed conses remain around (e.g.
1513 someone else is pointing to part of the list). This function
1514 is useful on internal lists that are used frequently and where
1515 the actual list doesn't escape beyond known code bounds. */
1518 delq_no_quit_and_free_cons (Lisp_Object elt, Lisp_Object list)
1520 REGISTER Lisp_Object tail = list;
1521 REGISTER Lisp_Object prev = Qnil;
1522 struct Lisp_Cons *cons_to_free = NULL;
1524 while (CONSP (tail))
1526 REGISTER Lisp_Object tem;
1527 if (tem = XCAR (tail), EQ_WITH_EBOLA_NOTICE (elt, tem))
1532 XCDR (prev) = XCDR (tail);
1533 cons_to_free = XCONS (tail);
1540 free_cons (cons_to_free);
1541 cons_to_free = NULL;
1547 DEFUN ("remassoc", Fremassoc, 2, 2, 0, /*
1548 Delete by side effect any elements of LIST whose car is `equal' to KEY.
1549 The modified LIST is returned. If the first member of LIST has a car
1550 that is `equal' to KEY, there is no way to remove it by side effect;
1551 therefore, write `(setq foo (remassoc key foo))' to be sure of changing
1556 REGISTER Lisp_Object tail = list;
1557 REGISTER Lisp_Object prev = Qnil;
1559 while (!NILP (tail))
1561 REGISTER Lisp_Object elt;
1562 CONCHECK_CONS (tail);
1564 if (CONSP (elt) && internal_equal (key, XCAR (elt), 0))
1569 XCDR (prev) = XCDR (tail);
1580 remassoc_no_quit (Lisp_Object key, Lisp_Object list)
1582 int speccount = specpdl_depth ();
1583 specbind (Qinhibit_quit, Qt);
1584 return unbind_to (speccount, Fremassoc (key, list));
1587 DEFUN ("remassq", Fremassq, 2, 2, 0, /*
1588 Delete by side effect any elements of LIST whose car is `eq' to KEY.
1589 The modified LIST is returned. If the first member of LIST has a car
1590 that is `eq' to KEY, there is no way to remove it by side effect;
1591 therefore, write `(setq foo (remassq key foo))' to be sure of changing
1596 REGISTER Lisp_Object tail = list;
1597 REGISTER Lisp_Object prev = Qnil;
1599 while (!NILP (tail))
1601 REGISTER Lisp_Object elt, tem;
1602 CONCHECK_CONS (tail);
1604 if (CONSP (elt) && (tem = XCAR (elt), EQ_WITH_EBOLA_NOTICE (key, tem)))
1609 XCDR (prev) = XCDR (tail);
1619 /* no quit, no errors; be careful */
1622 remassq_no_quit (Lisp_Object key, Lisp_Object list)
1624 REGISTER Lisp_Object tail = list;
1625 REGISTER Lisp_Object prev = Qnil;
1627 while (CONSP (tail))
1629 REGISTER Lisp_Object elt, tem;
1631 if (CONSP (elt) && (tem = XCAR (elt), EQ_WITH_EBOLA_NOTICE (key, tem)))
1636 XCDR (prev) = XCDR (tail);
1645 DEFUN ("remrassoc", Fremrassoc, 2, 2, 0, /*
1646 Delete by side effect any elements of LIST whose cdr is `equal' to VALUE.
1647 The modified LIST is returned. If the first member of LIST has a car
1648 that is `equal' to VALUE, there is no way to remove it by side effect;
1649 therefore, write `(setq foo (remrassoc value foo))' to be sure of changing
1654 REGISTER Lisp_Object tail = list;
1655 REGISTER Lisp_Object prev = Qnil;
1657 while (!NILP (tail))
1659 REGISTER Lisp_Object elt;
1660 CONCHECK_CONS (tail);
1662 if (CONSP (elt) && internal_equal (value, XCDR (elt), 0))
1667 XCDR (prev) = XCDR (tail);
1677 DEFUN ("remrassq", Fremrassq, 2, 2, 0, /*
1678 Delete by side effect any elements of LIST whose cdr is `eq' to VALUE.
1679 The modified LIST is returned. If the first member of LIST has a car
1680 that is `eq' to VALUE, there is no way to remove it by side effect;
1681 therefore, write `(setq foo (remrassq value foo))' to be sure of changing
1686 REGISTER Lisp_Object tail = list;
1687 REGISTER Lisp_Object prev = Qnil;
1689 while (!NILP (tail))
1691 REGISTER Lisp_Object elt, tem;
1692 CONCHECK_CONS (tail);
1694 if (CONSP (elt) && (tem = XCDR (elt), EQ_WITH_EBOLA_NOTICE (value, tem)))
1699 XCDR (prev) = XCDR (tail);
1709 /* no quit, no errors; be careful */
1712 remrassq_no_quit (Lisp_Object value, Lisp_Object list)
1714 REGISTER Lisp_Object tail = list;
1715 REGISTER Lisp_Object prev = Qnil;
1717 while (CONSP (tail))
1719 REGISTER Lisp_Object elt, tem;
1721 if (CONSP (elt) && (tem = XCDR (elt), EQ_WITH_EBOLA_NOTICE (value, tem)))
1726 XCDR (prev) = XCDR (tail);
1735 DEFUN ("nreverse", Fnreverse, 1, 1, 0, /*
1736 Reverse LIST by destructively modifying cdr pointers.
1737 Return the beginning of the reversed list.
1738 Also see: `reverse'.
1742 struct gcpro gcpro1, gcpro2;
1743 REGISTER Lisp_Object prev = Qnil;
1744 REGISTER Lisp_Object tail = list;
1746 /* We gcpro our args; see `nconc' */
1747 GCPRO2 (prev, tail);
1748 while (!NILP (tail))
1750 REGISTER Lisp_Object next;
1752 CONCHECK_CONS (tail);
1762 DEFUN ("reverse", Freverse, 1, 1, 0, /*
1763 Reverse LIST, copying. Return the beginning of the reversed list.
1764 See also the function `nreverse', which is used more often.
1768 REGISTER Lisp_Object tail;
1769 Lisp_Object new = Qnil;
1771 for (tail = list; CONSP (tail); tail = XCDR (tail))
1773 new = Fcons (XCAR (tail), new);
1777 dead_wrong_type_argument (Qlistp, tail);
1781 static Lisp_Object list_merge (Lisp_Object org_l1, Lisp_Object org_l2,
1782 Lisp_Object lisp_arg,
1783 int (*pred_fn) (Lisp_Object, Lisp_Object,
1784 Lisp_Object lisp_arg));
1787 list_sort (Lisp_Object list,
1788 Lisp_Object lisp_arg,
1789 int (*pred_fn) (Lisp_Object, Lisp_Object,
1790 Lisp_Object lisp_arg))
1792 struct gcpro gcpro1, gcpro2, gcpro3;
1793 Lisp_Object back, tem;
1794 Lisp_Object front = list;
1795 Lisp_Object len = Flength (list);
1796 int length = XINT (len);
1801 XSETINT (len, (length / 2) - 1);
1802 tem = Fnthcdr (len, list);
1804 Fsetcdr (tem, Qnil);
1806 GCPRO3 (front, back, lisp_arg);
1807 front = list_sort (front, lisp_arg, pred_fn);
1808 back = list_sort (back, lisp_arg, pred_fn);
1810 return list_merge (front, back, lisp_arg, pred_fn);
1815 merge_pred_function (Lisp_Object obj1, Lisp_Object obj2,
1820 /* prevents the GC from happening in call2 */
1821 int speccount = specpdl_depth ();
1822 /* Emacs' GC doesn't actually relocate pointers, so this probably
1823 isn't strictly necessary */
1824 record_unwind_protect (restore_gc_inhibit,
1825 make_int (gc_currently_forbidden));
1826 gc_currently_forbidden = 1;
1827 tmp = call2 (pred, obj1, obj2);
1828 unbind_to (speccount, Qnil);
1836 DEFUN ("sort", Fsort, 2, 2, 0, /*
1837 Sort LIST, stably, comparing elements using PREDICATE.
1838 Returns the sorted list. LIST is modified by side effects.
1839 PREDICATE is called with two elements of LIST, and should return T
1840 if the first element is "less" than the second.
1844 return list_sort (list, pred, merge_pred_function);
1848 merge (Lisp_Object org_l1, Lisp_Object org_l2,
1851 return list_merge (org_l1, org_l2, pred, merge_pred_function);
1856 list_merge (Lisp_Object org_l1, Lisp_Object org_l2,
1857 Lisp_Object lisp_arg,
1858 int (*pred_fn) (Lisp_Object, Lisp_Object, Lisp_Object lisp_arg))
1864 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1871 /* It is sufficient to protect org_l1 and org_l2.
1872 When l1 and l2 are updated, we copy the new values
1873 back into the org_ vars. */
1875 GCPRO4 (org_l1, org_l2, lisp_arg, value);
1896 if (((*pred_fn) (Fcar (l2), Fcar (l1), lisp_arg)) < 0)
1911 Fsetcdr (tail, tem);
1917 /************************************************************************/
1918 /* property-list functions */
1919 /************************************************************************/
1921 /* For properties of text, we need to do order-insensitive comparison of
1922 plists. That is, we need to compare two plists such that they are the
1923 same if they have the same set of keys, and equivalent values.
1924 So (a 1 b 2) would be equal to (b 2 a 1).
1926 NIL_MEANS_NOT_PRESENT is as in `plists-eq' etc.
1927 LAXP means use `equal' for comparisons.
1930 plists_differ (Lisp_Object a, Lisp_Object b, int nil_means_not_present,
1931 int laxp, int depth)
1933 int eqp = (depth == -1); /* -1 as depth means us eq, not equal. */
1934 int la, lb, m, i, fill;
1935 Lisp_Object *keys, *vals;
1939 if (NILP (a) && NILP (b))
1942 Fcheck_valid_plist (a);
1943 Fcheck_valid_plist (b);
1945 la = XINT (Flength (a));
1946 lb = XINT (Flength (b));
1947 m = (la > lb ? la : lb);
1949 keys = alloca_array (Lisp_Object, m);
1950 vals = alloca_array (Lisp_Object, m);
1951 flags = alloca_array (char, m);
1953 /* First extract the pairs from A. */
1954 for (rest = a; !NILP (rest); rest = XCDR (XCDR (rest)))
1956 Lisp_Object k = XCAR (rest);
1957 Lisp_Object v = XCAR (XCDR (rest));
1958 /* Maybe be Ebolified. */
1959 if (nil_means_not_present && NILP (v)) continue;
1965 /* Now iterate over B, and stop if we find something that's not in A,
1966 or that doesn't match. As we match, mark them. */
1967 for (rest = b; !NILP (rest); rest = XCDR (XCDR (rest)))
1969 Lisp_Object k = XCAR (rest);
1970 Lisp_Object v = XCAR (XCDR (rest));
1971 /* Maybe be Ebolified. */
1972 if (nil_means_not_present && NILP (v)) continue;
1973 for (i = 0; i < fill; i++)
1975 if (!laxp ? EQ (k, keys [i]) : internal_equal (k, keys [i], depth))
1978 /* We narrowly escaped being Ebolified here. */
1979 ? !EQ_WITH_EBOLA_NOTICE (v, vals [i])
1980 : !internal_equal (v, vals [i], depth)))
1981 /* a property in B has a different value than in A */
1988 /* there are some properties in B that are not in A */
1991 /* Now check to see that all the properties in A were also in B */
1992 for (i = 0; i < fill; i++)
2003 DEFUN ("plists-eq", Fplists_eq, 2, 3, 0, /*
2004 Return non-nil if property lists A and B are `eq'.
2005 A property list is an alternating list of keywords and values.
2006 This function does order-insensitive comparisons of the property lists:
2007 For example, the property lists '(a 1 b 2) and '(b 2 a 1) are equal.
2008 Comparison between values is done using `eq'. See also `plists-equal'.
2009 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
2010 a nil value is ignored. This feature is a virus that has infected
2011 old Lisp implementations, but should not be used except for backward
2014 (a, b, nil_means_not_present))
2016 return (plists_differ (a, b, !NILP (nil_means_not_present), 0, -1)
2020 DEFUN ("plists-equal", Fplists_equal, 2, 3, 0, /*
2021 Return non-nil if property lists A and B are `equal'.
2022 A property list is an alternating list of keywords and values. This
2023 function does order-insensitive comparisons of the property lists: For
2024 example, the property lists '(a 1 b 2) and '(b 2 a 1) are equal.
2025 Comparison between values is done using `equal'. See also `plists-eq'.
2026 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
2027 a nil value is ignored. This feature is a virus that has infected
2028 old Lisp implementations, but should not be used except for backward
2031 (a, b, nil_means_not_present))
2033 return (plists_differ (a, b, !NILP (nil_means_not_present), 0, 1)
2038 DEFUN ("lax-plists-eq", Flax_plists_eq, 2, 3, 0, /*
2039 Return non-nil if lax property lists A and B are `eq'.
2040 A property list is an alternating list of keywords and values.
2041 This function does order-insensitive comparisons of the property lists:
2042 For example, the property lists '(a 1 b 2) and '(b 2 a 1) are equal.
2043 Comparison between values is done using `eq'. See also `plists-equal'.
2044 A lax property list is like a regular one except that comparisons between
2045 keywords is done using `equal' instead of `eq'.
2046 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
2047 a nil value is ignored. This feature is a virus that has infected
2048 old Lisp implementations, but should not be used except for backward
2051 (a, b, nil_means_not_present))
2053 return (plists_differ (a, b, !NILP (nil_means_not_present), 1, -1)
2057 DEFUN ("lax-plists-equal", Flax_plists_equal, 2, 3, 0, /*
2058 Return non-nil if lax property lists A and B are `equal'.
2059 A property list is an alternating list of keywords and values. This
2060 function does order-insensitive comparisons of the property lists: For
2061 example, the property lists '(a 1 b 2) and '(b 2 a 1) are equal.
2062 Comparison between values is done using `equal'. See also `plists-eq'.
2063 A lax property list is like a regular one except that comparisons between
2064 keywords is done using `equal' instead of `eq'.
2065 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
2066 a nil value is ignored. This feature is a virus that has infected
2067 old Lisp implementations, but should not be used except for backward
2070 (a, b, nil_means_not_present))
2072 return (plists_differ (a, b, !NILP (nil_means_not_present), 1, 1)
2076 /* Return the value associated with key PROPERTY in property list PLIST.
2077 Return nil if key not found. This function is used for internal
2078 property lists that cannot be directly manipulated by the user.
2082 internal_plist_get (Lisp_Object plist, Lisp_Object property)
2084 Lisp_Object tail = plist;
2086 for (; !NILP (tail); tail = XCDR (XCDR (tail)))
2088 struct Lisp_Cons *c = XCONS (tail);
2089 if (EQ (c->car, property))
2090 return XCAR (c->cdr);
2096 /* Set PLIST's value for PROPERTY to VALUE. Analogous to
2097 internal_plist_get(). */
2100 internal_plist_put (Lisp_Object *plist, Lisp_Object property,
2105 for (tail = *plist; !NILP (tail); tail = XCDR (XCDR (tail)))
2107 if (EQ (XCAR (tail), property))
2109 XCAR (XCDR (tail)) = value;
2114 *plist = Fcons (property, Fcons (value, *plist));
2118 internal_remprop (Lisp_Object *plist, Lisp_Object property)
2120 Lisp_Object tail = *plist;
2125 if (EQ (XCAR (tail), property))
2127 *plist = XCDR (XCDR (tail));
2131 for (tail = XCDR (tail); !NILP (XCDR (tail));
2132 tail = XCDR (XCDR (tail)))
2134 struct Lisp_Cons *c = XCONS (tail);
2135 if (EQ (XCAR (c->cdr), property))
2137 c->cdr = XCDR (XCDR (c->cdr));
2145 /* Called on a malformed property list. BADPLACE should be some
2146 place where truncating will form a good list -- i.e. we shouldn't
2147 result in a list with an odd length. */
2150 bad_bad_bunny (Lisp_Object *plist, Lisp_Object *badplace, Error_behavior errb)
2152 if (ERRB_EQ (errb, ERROR_ME))
2153 return Fsignal (Qmalformed_property_list, list2 (*plist, *badplace));
2156 if (ERRB_EQ (errb, ERROR_ME_WARN))
2158 warn_when_safe_lispobj
2161 ("Malformed property list -- list has been truncated"),
2169 /* Called on a circular property list. BADPLACE should be some place
2170 where truncating will result in an even-length list, as above.
2171 If doesn't particularly matter where we truncate -- anywhere we
2172 truncate along the entire list will break the circularity, because
2173 it will create a terminus and the list currently doesn't have one.
2177 bad_bad_turtle (Lisp_Object *plist, Lisp_Object *badplace, Error_behavior errb)
2179 if (ERRB_EQ (errb, ERROR_ME))
2180 /* #### Eek, this will probably result in another error
2181 when PLIST is printed out */
2182 return Fsignal (Qcircular_property_list, list1 (*plist));
2185 if (ERRB_EQ (errb, ERROR_ME_WARN))
2187 warn_when_safe_lispobj
2190 ("Circular property list -- list has been truncated"),
2198 /* Advance the tortoise pointer by two (one iteration of a property-list
2199 loop) and the hare pointer by four and verify that no malformations
2200 or circularities exist. If so, return zero and store a value into
2201 RETVAL that should be returned by the calling function. Otherwise,
2202 return 1. See external_plist_get().
2206 advance_plist_pointers (Lisp_Object *plist,
2207 Lisp_Object **tortoise, Lisp_Object **hare,
2208 Error_behavior errb, Lisp_Object *retval)
2211 Lisp_Object *tortsave = *tortoise;
2213 /* Note that our "fixing" may be more brutal than necessary,
2214 but it's the user's own problem, not ours. if they went in and
2215 manually fucked up a plist. */
2217 for (i = 0; i < 2; i++)
2219 /* This is a standard iteration of a defensive-loop-checking
2220 loop. We just do it twice because we want to advance past
2221 both the property and its value.
2223 If the pointer indirection is confusing you, remember that
2224 one level of indirection on the hare and tortoise pointers
2225 is only due to pass-by-reference for this function. The other
2226 level is so that the plist can be fixed in place. */
2228 /* When we reach the end of a well-formed plist, **HARE is
2229 nil. In that case, we don't do anything at all except
2230 advance TORTOISE by one. Otherwise, we advance HARE
2231 by two (making sure it's OK to do so), then advance
2232 TORTOISE by one (it will always be OK to do so because
2233 the HARE is always ahead of the TORTOISE and will have
2234 already verified the path), then make sure TORTOISE and
2235 HARE don't contain the same non-nil object -- if the
2236 TORTOISE and the HARE ever meet, then obviously we're
2237 in a circularity, and if we're in a circularity, then
2238 the TORTOISE and the HARE can't cross paths without
2239 meeting, since the HARE only gains one step over the
2240 TORTOISE per iteration. */
2244 Lisp_Object *haresave = *hare;
2245 if (!CONSP (**hare))
2247 *retval = bad_bad_bunny (plist, haresave, errb);
2250 *hare = &XCDR (**hare);
2251 /* In a non-plist, we'd check here for a nil value for
2252 **HARE, which is OK (it just means the list has an
2253 odd number of elements). In a plist, it's not OK
2254 for the list to have an odd number of elements. */
2255 if (!CONSP (**hare))
2257 *retval = bad_bad_bunny (plist, haresave, errb);
2260 *hare = &XCDR (**hare);
2263 *tortoise = &XCDR (**tortoise);
2264 if (!NILP (**hare) && EQ (**tortoise, **hare))
2266 *retval = bad_bad_turtle (plist, tortsave, errb);
2274 /* Return the value of PROPERTY from PLIST, or Qunbound if
2275 property is not on the list.
2277 PLIST is a Lisp-accessible property list, meaning that it
2278 has to be checked for malformations and circularities.
2280 If ERRB is ERROR_ME, an error will be signalled. Otherwise, the
2281 function will never signal an error; and if ERRB is ERROR_ME_WARN,
2282 on finding a malformation or a circularity, it issues a warning and
2283 attempts to silently fix the problem.
2285 A pointer to PLIST is passed in so that PLIST can be successfully
2286 "fixed" even if the error is at the beginning of the plist. */
2289 external_plist_get (Lisp_Object *plist, Lisp_Object property,
2290 int laxp, Error_behavior errb)
2292 Lisp_Object *tortoise = plist;
2293 Lisp_Object *hare = plist;
2295 while (!NILP (*tortoise))
2297 Lisp_Object *tortsave = tortoise;
2300 /* We do the standard tortoise/hare march. We isolate the
2301 grungy stuff to do this in advance_plist_pointers(), though.
2302 To us, all this function does is advance the tortoise
2303 pointer by two and the hare pointer by four and make sure
2304 everything's OK. We first advance the pointers and then
2305 check if a property matched; this ensures that our
2306 check for a matching property is safe. */
2308 if (!advance_plist_pointers (plist, &tortoise, &hare, errb, &retval))
2311 if (!laxp ? EQ (XCAR (*tortsave), property)
2312 : internal_equal (XCAR (*tortsave), property, 0))
2313 return XCAR (XCDR (*tortsave));
2319 /* Set PLIST's value for PROPERTY to VALUE, given a possibly
2320 malformed or circular plist. Analogous to external_plist_get(). */
2323 external_plist_put (Lisp_Object *plist, Lisp_Object property,
2324 Lisp_Object value, int laxp, Error_behavior errb)
2326 Lisp_Object *tortoise = plist;
2327 Lisp_Object *hare = plist;
2329 while (!NILP (*tortoise))
2331 Lisp_Object *tortsave = tortoise;
2335 if (!advance_plist_pointers (plist, &tortoise, &hare, errb, &retval))
2338 if (!laxp ? EQ (XCAR (*tortsave), property)
2339 : internal_equal (XCAR (*tortsave), property, 0))
2341 XCAR (XCDR (*tortsave)) = value;
2346 *plist = Fcons (property, Fcons (value, *plist));
2350 external_remprop (Lisp_Object *plist, Lisp_Object property,
2351 int laxp, Error_behavior errb)
2353 Lisp_Object *tortoise = plist;
2354 Lisp_Object *hare = plist;
2356 while (!NILP (*tortoise))
2358 Lisp_Object *tortsave = tortoise;
2362 if (!advance_plist_pointers (plist, &tortoise, &hare, errb, &retval))
2365 if (!laxp ? EQ (XCAR (*tortsave), property)
2366 : internal_equal (XCAR (*tortsave), property, 0))
2368 /* Now you see why it's so convenient to have that level
2370 *tortsave = XCDR (XCDR (*tortsave));
2378 DEFUN ("plist-get", Fplist_get, 2, 3, 0, /*
2379 Extract a value from a property list.
2380 PLIST is a property list, which is a list of the form
2381 \(PROP1 VALUE1 PROP2 VALUE2...). This function returns the value
2382 corresponding to the given PROP, or DEFAULT if PROP is not
2383 one of the properties on the list.
2385 (plist, prop, default_))
2387 Lisp_Object val = external_plist_get (&plist, prop, 0, ERROR_ME);
2393 DEFUN ("plist-put", Fplist_put, 3, 3, 0, /*
2394 Change value in PLIST of PROP to VAL.
2395 PLIST is a property list, which is a list of the form \(PROP1 VALUE1
2396 PROP2 VALUE2 ...). PROP is usually a symbol and VAL is any object.
2397 If PROP is already a property on the list, its value is set to VAL,
2398 otherwise the new PROP VAL pair is added. The new plist is returned;
2399 use `(setq x (plist-put x prop val))' to be sure to use the new value.
2400 The PLIST is modified by side effects.
2404 external_plist_put (&plist, prop, val, 0, ERROR_ME);
2408 DEFUN ("plist-remprop", Fplist_remprop, 2, 2, 0, /*
2409 Remove from PLIST the property PROP and its value.
2410 PLIST is a property list, which is a list of the form \(PROP1 VALUE1
2411 PROP2 VALUE2 ...). PROP is usually a symbol. The new plist is
2412 returned; use `(setq x (plist-remprop x prop val))' to be sure to use
2413 the new value. The PLIST is modified by side effects.
2417 external_remprop (&plist, prop, 0, ERROR_ME);
2421 DEFUN ("plist-member", Fplist_member, 2, 2, 0, /*
2422 Return t if PROP has a value specified in PLIST.
2426 return UNBOUNDP (Fplist_get (plist, prop, Qunbound)) ? Qnil : Qt;
2429 DEFUN ("check-valid-plist", Fcheck_valid_plist, 1, 1, 0, /*
2430 Given a plist, signal an error if there is anything wrong with it.
2431 This means that it's a malformed or circular plist.
2435 Lisp_Object *tortoise;
2441 while (!NILP (*tortoise))
2446 if (!advance_plist_pointers (&plist, &tortoise, &hare, ERROR_ME,
2454 DEFUN ("valid-plist-p", Fvalid_plist_p, 1, 1, 0, /*
2455 Given a plist, return non-nil if its format is correct.
2456 If it returns nil, `check-valid-plist' will signal an error when given
2457 the plist; that means it's a malformed or circular plist or has non-symbols
2462 Lisp_Object *tortoise;
2467 while (!NILP (*tortoise))
2472 if (!advance_plist_pointers (&plist, &tortoise, &hare, ERROR_ME_NOT,
2480 DEFUN ("canonicalize-plist", Fcanonicalize_plist, 1, 2, 0, /*
2481 Destructively remove any duplicate entries from a 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 (plist, nil_means_not_present))
2495 Lisp_Object head = plist;
2497 Fcheck_valid_plist (plist);
2499 while (!NILP (plist))
2501 Lisp_Object prop = Fcar (plist);
2502 Lisp_Object next = Fcdr (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, plist))
2509 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, 0, ERROR_ME));
2516 plist = Fcdr (next);
2522 DEFUN ("lax-plist-get", Flax_plist_get, 2, 3, 0, /*
2523 Extract a value from a lax property list.
2525 LAX-PLIST is a lax property list, which is a list of the form \(PROP1
2526 VALUE1 PROP2 VALUE2...), where comparions between properties is done
2527 using `equal' instead of `eq'. This function returns the value
2528 corresponding to the given PROP, or DEFAULT if PROP is not one of the
2529 properties on the list.
2531 (lax_plist, prop, default_))
2533 Lisp_Object val = external_plist_get (&lax_plist, prop, 1, ERROR_ME);
2539 DEFUN ("lax-plist-put", Flax_plist_put, 3, 3, 0, /*
2540 Change value in LAX-PLIST of PROP to VAL.
2541 LAX-PLIST is a lax property list, which is a list of the form \(PROP1
2542 VALUE1 PROP2 VALUE2...), where comparions between properties is done
2543 using `equal' instead of `eq'. PROP is usually a symbol and VAL is
2544 any object. If PROP is already a property on the list, its value is
2545 set to VAL, otherwise the new PROP VAL pair is added. The new plist
2546 is returned; use `(setq x (lax-plist-put x prop val))' to be sure to
2547 use the new value. The LAX-PLIST is modified by side effects.
2549 (lax_plist, prop, val))
2551 external_plist_put (&lax_plist, prop, val, 1, ERROR_ME);
2555 DEFUN ("lax-plist-remprop", Flax_plist_remprop, 2, 2, 0, /*
2556 Remove from LAX-PLIST the property PROP and its value.
2557 LAX-PLIST is a lax property list, which is a list of the form \(PROP1
2558 VALUE1 PROP2 VALUE2...), where comparions between properties is done
2559 using `equal' instead of `eq'. PROP is usually a symbol. The new
2560 plist is returned; use `(setq x (lax-plist-remprop x prop val))' to be
2561 sure to use the new value. The LAX-PLIST is modified by side effects.
2565 external_remprop (&lax_plist, prop, 1, ERROR_ME);
2569 DEFUN ("lax-plist-member", Flax_plist_member, 2, 2, 0, /*
2570 Return t if PROP has a value specified in LAX-PLIST.
2571 LAX-PLIST is a lax property list, which is a list of the form \(PROP1
2572 VALUE1 PROP2 VALUE2...), where comparions between properties is done
2573 using `equal' instead of `eq'.
2577 return UNBOUNDP (Flax_plist_get (lax_plist, prop, Qunbound)) ? Qnil : Qt;
2580 DEFUN ("canonicalize-lax-plist", Fcanonicalize_lax_plist, 1, 2, 0, /*
2581 Destructively remove any duplicate entries from a lax plist.
2582 In such cases, the first entry applies.
2584 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
2585 a nil value is removed. This feature is a virus that has infected
2586 old Lisp implementations, but should not be used except for backward
2589 The new plist is returned. If NIL-MEANS-NOT-PRESENT is given, the
2590 return value may not be EQ to the passed-in value, so make sure to
2591 `setq' the value back into where it came from.
2593 (lax_plist, nil_means_not_present))
2595 Lisp_Object head = lax_plist;
2597 Fcheck_valid_plist (lax_plist);
2599 while (!NILP (lax_plist))
2601 Lisp_Object prop = Fcar (lax_plist);
2602 Lisp_Object next = Fcdr (lax_plist);
2604 CHECK_CONS (next); /* just make doubly sure we catch any errors */
2605 if (!NILP (nil_means_not_present) && NILP (Fcar (next)))
2607 if (EQ (head, lax_plist))
2609 lax_plist = Fcdr (next);
2612 /* external_remprop returns 1 if it removed any property.
2613 We have to loop till it didn't remove anything, in case
2614 the property occurs many times. */
2615 while (external_remprop (&XCDR (next), prop, 1, ERROR_ME));
2616 lax_plist = Fcdr (next);
2622 /* In C because the frame props stuff uses it */
2624 DEFUN ("destructive-alist-to-plist", Fdestructive_alist_to_plist, 1, 1, 0, /*
2625 Convert association list ALIST into the equivalent property-list form.
2626 The plist is returned. This converts from
2628 \((a . 1) (b . 2) (c . 3))
2634 The original alist is destroyed in the process of constructing the plist.
2635 See also `alist-to-plist'.
2639 Lisp_Object head = alist;
2640 while (!NILP (alist))
2642 /* remember the alist element. */
2643 Lisp_Object el = Fcar (alist);
2645 Fsetcar (alist, Fcar (el));
2646 Fsetcar (el, Fcdr (el));
2647 Fsetcdr (el, Fcdr (alist));
2648 Fsetcdr (alist, el);
2649 alist = Fcdr (Fcdr (alist));
2655 /* Symbol plists are directly accessible, so we need to protect against
2656 invalid property list structure */
2659 symbol_getprop (Lisp_Object sym, Lisp_Object propname, Lisp_Object default_)
2661 Lisp_Object val = external_plist_get (&XSYMBOL (sym)->plist, propname,
2663 return UNBOUNDP (val) ? default_ : val;
2667 symbol_putprop (Lisp_Object sym, Lisp_Object propname, Lisp_Object value)
2669 external_plist_put (&XSYMBOL (sym)->plist, propname, value, 0, ERROR_ME);
2673 symbol_remprop (Lisp_Object symbol, Lisp_Object propname)
2675 return external_remprop (&XSYMBOL (symbol)->plist, propname, 0, ERROR_ME);
2678 /* We store the string's extent info as the first element of the string's
2679 property list; and the string's MODIFF as the first or second element
2680 of the string's property list (depending on whether the extent info
2681 is present), but only if the string has been modified. This is ugly
2682 but it reduces the memory allocated for the string in the vast
2683 majority of cases, where the string is never modified and has no
2687 static Lisp_Object *
2688 string_plist_ptr (struct Lisp_String *s)
2690 Lisp_Object *ptr = &s->plist;
2692 if (CONSP (*ptr) && EXTENT_INFOP (XCAR (*ptr)))
2694 if (CONSP (*ptr) && INTP (XCAR (*ptr)))
2700 string_getprop (struct Lisp_String *s, Lisp_Object property,
2701 Lisp_Object default_)
2703 Lisp_Object val = external_plist_get (string_plist_ptr (s), property, 0,
2705 return UNBOUNDP (val) ? default_ : val;
2709 string_putprop (struct Lisp_String *s, Lisp_Object property,
2712 external_plist_put (string_plist_ptr (s), property, value, 0, ERROR_ME);
2716 string_remprop (struct Lisp_String *s, Lisp_Object property)
2718 return external_remprop (string_plist_ptr (s), property, 0, ERROR_ME);
2722 string_plist (struct Lisp_String *s)
2724 return *string_plist_ptr (s);
2727 DEFUN ("get", Fget, 2, 3, 0, /*
2728 Return the value of OBJECT's PROPNAME property.
2729 This is the last VALUE stored with `(put OBJECT PROPNAME VALUE)'.
2730 If there is no such property, return optional third arg DEFAULT
2731 \(which defaults to `nil'). OBJECT can be a symbol, face, extent,
2732 or string. See also `put', `remprop', and `object-plist'.
2734 (object, propname, default_))
2738 /* Various places in emacs call Fget() and expect it not to quit,
2741 /* It's easiest to treat symbols specially because they may not
2743 if (SYMBOLP (object))
2744 val = symbol_getprop (object, propname, default_);
2745 else if (STRINGP (object))
2746 val = string_getprop (XSTRING (object), propname, default_);
2747 else if (LRECORDP (object))
2749 CONST struct lrecord_implementation
2750 *imp = XRECORD_LHEADER_IMPLEMENTATION (object);
2753 val = (imp->getprop) (object, propname);
2763 signal_simple_error ("Object type has no properties", object);
2769 DEFUN ("put", Fput, 3, 3, 0, /*
2770 Store OBJECT's PROPNAME property with value VALUE.
2771 It can be retrieved with `(get OBJECT PROPNAME)'. OBJECT can be a
2772 symbol, face, extent, or string.
2774 For a string, no properties currently have predefined meanings.
2775 For the predefined properties for extents, see `set-extent-property'.
2776 For the predefined properties for faces, see `set-face-property'.
2778 See also `get', `remprop', and `object-plist'.
2780 (object, propname, value))
2782 CHECK_SYMBOL (propname);
2783 CHECK_IMPURE (object);
2785 if (SYMBOLP (object))
2786 symbol_putprop (object, propname, value);
2787 else if (STRINGP (object))
2788 string_putprop (XSTRING (object), propname, value);
2789 else if (LRECORDP (object))
2791 CONST struct lrecord_implementation
2792 *imp = XRECORD_LHEADER_IMPLEMENTATION (object);
2795 if (! (imp->putprop) (object, propname, value))
2796 signal_simple_error ("Can't set property on object", propname);
2804 signal_simple_error ("Object type has no settable properties", object);
2811 pure_put (Lisp_Object sym, Lisp_Object prop, Lisp_Object val)
2813 Fput (sym, prop, Fpurecopy (val));
2816 DEFUN ("remprop", Fremprop, 2, 2, 0, /*
2817 Remove from OBJECT's property list the property PROPNAME and its
2818 value. OBJECT can be a symbol, face, extent, or string. Returns
2819 non-nil if the property list was actually changed (i.e. if PROPNAME
2820 was present in the property list). See also `get', `put', and
2827 CHECK_SYMBOL (propname);
2828 CHECK_IMPURE (object);
2830 if (SYMBOLP (object))
2831 retval = symbol_remprop (object, propname);
2832 else if (STRINGP (object))
2833 retval = string_remprop (XSTRING (object), propname);
2834 else if (LRECORDP (object))
2836 CONST struct lrecord_implementation
2837 *imp = XRECORD_LHEADER_IMPLEMENTATION (object);
2840 retval = (imp->remprop) (object, propname);
2842 signal_simple_error ("Can't remove property from object",
2851 signal_simple_error ("Object type has no removable properties", object);
2854 return retval ? Qt : Qnil;
2857 DEFUN ("object-plist", Fobject_plist, 1, 1, 0, /*
2858 Return a property list of OBJECT's props.
2859 For a symbol this is equivalent to `symbol-plist'.
2860 Do not modify the property list directly; this may or may not have
2861 the desired effects. (In particular, for a property with a special
2862 interpretation, this will probably have no effect at all.)
2866 if (SYMBOLP (object))
2867 return Fsymbol_plist (object);
2868 else if (STRINGP (object))
2869 return string_plist (XSTRING (object));
2870 else if (LRECORDP (object))
2872 CONST struct lrecord_implementation
2873 *imp = XRECORD_LHEADER_IMPLEMENTATION (object);
2875 return (imp->plist) (object);
2877 signal_simple_error ("Object type has no properties", object);
2880 signal_simple_error ("Object type has no properties", object);
2887 internal_equal (Lisp_Object o1, Lisp_Object o2, int depth)
2890 error ("Stack overflow in equal");
2891 #ifndef LRECORD_CONS
2895 if (EQ_WITH_EBOLA_NOTICE (o1, o2))
2897 /* Note that (equal 20 20.0) should be nil */
2898 else if (XTYPE (o1) != XTYPE (o2))
2900 #ifndef LRECORD_CONS
2901 else if (CONSP (o1))
2903 if (!internal_equal (XCAR (o1), XCAR (o2), depth + 1))
2910 #ifndef LRECORD_VECTOR
2911 else if (VECTORP (o1))
2913 Lisp_Object *v1 = XVECTOR_DATA (o1);
2914 Lisp_Object *v2 = XVECTOR_DATA (o2);
2915 int len = XVECTOR_LENGTH (o1);
2916 if (len != XVECTOR_LENGTH (o2))
2919 if (!internal_equal (*v1++, *v2++, depth + 1))
2924 #ifndef LRECORD_STRING
2925 else if (STRINGP (o1))
2928 return (((len = XSTRING_LENGTH (o1)) == XSTRING_LENGTH (o2)) &&
2929 !memcmp (XSTRING_DATA (o1), XSTRING_DATA (o2), len));
2932 else if (LRECORDP (o1))
2934 CONST struct lrecord_implementation
2935 *imp1 = XRECORD_LHEADER_IMPLEMENTATION (o1),
2936 *imp2 = XRECORD_LHEADER_IMPLEMENTATION (o2);
2939 else if (imp1->equal == 0)
2940 /* EQ-ness of the objects was noticed above */
2943 return (imp1->equal) (o1, o2, depth);
2949 /* Note that we may be calling sub-objects that will use
2950 internal_equal() (instead of internal_old_equal()). Oh well.
2951 We will get an Ebola note if there's any possibility of confusion,
2952 but that seems unlikely. */
2955 internal_old_equal (Lisp_Object o1, Lisp_Object o2, int depth)
2958 error ("Stack overflow in equal");
2959 #ifndef LRECORD_CONS
2963 if (HACKEQ_UNSAFE (o1, o2))
2965 /* Note that (equal 20 20.0) should be nil */
2966 else if (XTYPE (o1) != XTYPE (o2))
2968 #ifndef LRECORD_CONS
2969 else if (CONSP (o1))
2971 if (!internal_old_equal (XCAR (o1), XCAR (o2), depth + 1))
2978 #ifndef LRECORD_VECTOR
2979 else if (VECTORP (o1))
2982 int len = XVECTOR_LENGTH (o1);
2983 if (len != XVECTOR_LENGTH (o2))
2985 for (indice = 0; indice < len; indice++)
2987 if (!internal_old_equal (XVECTOR_DATA (o1) [indice],
2988 XVECTOR_DATA (o2) [indice],
2995 #ifndef LRECORD_STRING
2996 else if (STRINGP (o1))
2998 Bytecount len = XSTRING_LENGTH (o1);
2999 if (len != XSTRING_LENGTH (o2))
3001 if (memcmp (XSTRING_DATA (o1), XSTRING_DATA (o2), len))
3006 else if (LRECORDP (o1))
3008 CONST struct lrecord_implementation
3009 *imp1 = XRECORD_LHEADER_IMPLEMENTATION (o1),
3010 *imp2 = XRECORD_LHEADER_IMPLEMENTATION (o2);
3013 else if (imp1->equal == 0)
3014 /* EQ-ness of the objects was noticed above */
3017 return (imp1->equal) (o1, o2, depth);
3023 DEFUN ("equal", Fequal, 2, 2, 0, /*
3024 Return t if two Lisp objects have similar structure and contents.
3025 They must have the same data type.
3026 Conses are compared by comparing the cars and the cdrs.
3027 Vectors and strings are compared element by element.
3028 Numbers are compared by value. Symbols must match exactly.
3032 return internal_equal (o1, o2, 0) ? Qt : Qnil;
3035 DEFUN ("old-equal", Fold_equal, 2, 2, 0, /*
3036 Return t if two Lisp objects have similar structure and contents.
3037 They must have the same data type.
3038 \(Note, however, that an exception is made for characters and integers;
3039 this is known as the "char-int confoundance disease." See `eq' and
3041 This function is provided only for byte-code compatibility with v19.
3046 return internal_old_equal (o1, o2, 0) ? Qt : Qnil;
3050 DEFUN ("fillarray", Ffillarray, 2, 2, 0, /*
3051 Store each element of ARRAY with ITEM.
3052 ARRAY is a vector, bit vector, or string.
3057 if (STRINGP (array))
3060 struct Lisp_String *s = XSTRING (array);
3061 Charcount len = string_char_length (s);
3063 CHECK_CHAR_COERCE_INT (item);
3064 CHECK_IMPURE (array);
3065 charval = XCHAR (item);
3066 for (i = 0; i < len; i++)
3067 set_string_char (s, i, charval);
3068 bump_string_modiff (array);
3070 else if (VECTORP (array))
3072 Lisp_Object *p = XVECTOR_DATA (array);
3073 int len = XVECTOR_LENGTH (array);
3074 CHECK_IMPURE (array);
3078 else if (BIT_VECTORP (array))
3080 struct Lisp_Bit_Vector *v = XBIT_VECTOR (array);
3081 int len = bit_vector_length (v);
3084 CHECK_IMPURE (array);
3087 set_bit_vector_bit (v, len, bit);
3091 array = wrong_type_argument (Qarrayp, array);
3098 nconc2 (Lisp_Object s1, Lisp_Object s2)
3100 Lisp_Object args[2];
3103 return Fnconc (2, args);
3106 DEFUN ("nconc", Fnconc, 0, MANY, 0, /*
3107 Concatenate any number of lists by altering them.
3108 Only the last argument is not altered, and need not be a list.
3110 If the first argument is nil, there is no way to modify it by side
3111 effect; therefore, write `(setq foo (nconc foo list))' to be sure of
3112 changing the value of `foo'.
3114 (int nargs, Lisp_Object *args))
3117 struct gcpro gcpro1;
3119 /* The modus operandi in Emacs is "caller gc-protects args".
3120 However, nconc (particularly nconc2 ()) is called many times
3121 in Emacs on freshly created stuff (e.g. you see the idiom
3122 nconc2 (Fcopy_sequence (foo), bar) a lot). So we help those
3123 callers out by protecting the args ourselves to save them
3124 a lot of temporary-variable grief. */
3127 gcpro1.nvars = nargs;
3129 while (argnum < nargs)
3131 Lisp_Object val = args[argnum];
3134 /* Found the first cons, which will be our return value. */
3135 Lisp_Object last = val;
3137 for (argnum++; argnum < nargs; argnum++)
3139 Lisp_Object next = args[argnum];
3141 if (CONSP (next) || argnum == nargs -1)
3143 /* (setcdr (last val) next) */
3144 while (CONSP (XCDR (last)))
3151 else if (NILP (next))
3157 next = wrong_type_argument (next, Qlistp);
3161 RETURN_UNGCPRO (val);
3163 else if (NILP (val))
3165 else if (argnum == nargs - 1) /* last arg? */
3166 RETURN_UNGCPRO (val);
3168 args[argnum] = wrong_type_argument (val, Qlistp);
3170 RETURN_UNGCPRO (Qnil); /* No non-nil args provided. */
3174 /* This is the guts of all mapping functions.
3175 Apply fn to each element of seq, one by one,
3176 storing the results into elements of vals, a C vector of Lisp_Objects.
3177 leni is the length of vals, which should also be the length of seq.
3179 If VALS is a null pointer, do not accumulate the results. */
3182 mapcar1 (int leni, Lisp_Object *vals, Lisp_Object fn, Lisp_Object seq)
3185 Lisp_Object dummy = Qnil;
3187 struct gcpro gcpro1, gcpro2, gcpro3;
3190 GCPRO3 (dummy, fn, seq);
3194 /* Don't let vals contain any garbage when GC happens. */
3195 for (i = 0; i < leni; i++)
3198 gcpro1.nvars = leni;
3201 /* We need not explicitly protect `tail' because it is used only on
3202 lists, and 1) lists are not relocated and 2) the list is marked
3203 via `seq' so will not be freed */
3207 for (i = 0; i < leni; i++)
3209 dummy = XVECTOR_DATA (seq)[i];
3210 result = call1 (fn, dummy);
3215 else if (BIT_VECTORP (seq))
3217 struct Lisp_Bit_Vector *v = XBIT_VECTOR (seq);
3218 for (i = 0; i < leni; i++)
3220 XSETINT (dummy, bit_vector_bit (v, i));
3221 result = call1 (fn, dummy);
3226 else if (STRINGP (seq))
3228 for (i = 0; i < leni; i++)
3230 result = call1 (fn, make_char (string_char (XSTRING (seq), i)));
3235 else /* Must be a list, since Flength did not get an error */
3238 for (i = 0; i < leni; i++)
3240 result = call1 (fn, Fcar (tail));
3250 DEFUN ("mapconcat", Fmapconcat, 3, 3, 0, /*
3251 Apply FN to each element of SEQ, and concat the results as strings.
3252 In between each pair of results, stick in SEP.
3253 Thus, " " as SEP results in spaces between the values returned by FN.
3257 int len = XINT (Flength (seq));
3260 struct gcpro gcpro1;
3261 int nargs = len + len - 1;
3263 if (nargs < 0) return build_string ("");
3265 args = alloca_array (Lisp_Object, nargs);
3268 mapcar1 (len, args, fn, seq);
3271 for (i = len - 1; i >= 0; i--)
3272 args[i + i] = args[i];
3274 for (i = 1; i < nargs; i += 2)
3277 return Fconcat (nargs, args);
3280 DEFUN ("mapcar", Fmapcar, 2, 2, 0, /*
3281 Apply FUNCTION to each element of SEQUENCE, and make a list of the results.
3282 The result is a list just as long as SEQUENCE.
3283 SEQUENCE may be a list, a vector, a bit vector, or a string.
3287 int len = XINT (Flength (seq));
3288 Lisp_Object *args = alloca_array (Lisp_Object, len);
3290 mapcar1 (len, args, fn, seq);
3292 return Flist (len, args);
3295 DEFUN ("mapvector", Fmapvector, 2, 2, 0, /*
3296 Apply FUNCTION to each element of SEQUENCE, making a vector of the results.
3297 The result is a vector of the same length as SEQUENCE.
3298 SEQUENCE may be a list, a vector or a string.
3302 int len = XINT (Flength (seq));
3303 /* Ideally, this should call make_vector_internal, because we don't
3304 need initialization. */
3305 Lisp_Object result = make_vector (len, Qnil);
3306 struct gcpro gcpro1;
3309 mapcar1 (len, XVECTOR_DATA (result), fn, seq);
3315 DEFUN ("mapc", Fmapc, 2, 2, 0, /*
3316 Apply FUNCTION to each element of SEQUENCE.
3317 SEQUENCE may be a list, a vector, a bit vector, or a string.
3318 This function is like `mapcar' but does not accumulate the results,
3319 which is more efficient if you do not use the results.
3323 mapcar1 (XINT (Flength (seq)), 0, fn, seq);
3329 /* #### this function doesn't belong in this file! */
3331 DEFUN ("load-average", Fload_average, 0, 1, 0, /*
3332 Return list of 1 minute, 5 minute and 15 minute load averages.
3333 Each of the three load averages is multiplied by 100,
3334 then converted to integer.
3336 When USE-FLOATS is non-nil, floats will be used instead of integers.
3337 These floats are not multiplied by 100.
3339 If the 5-minute or 15-minute load averages are not available, return a
3340 shortened list, containing only those averages which are available.
3342 On some systems, this won't work due to permissions on /dev/kmem,
3343 in which case you can't use this.
3348 int loads = getloadavg (load_ave, countof (load_ave));
3349 Lisp_Object ret = Qnil;
3352 error ("load-average not implemented for this operating system");
3354 signal_simple_error ("Could not get load-average",
3355 lisp_strerror (errno));
3359 Lisp_Object load = (NILP (use_floats) ?
3360 make_int ((int) (100.0 * load_ave[loads]))
3361 : make_float (load_ave[loads]));
3362 ret = Fcons (load, ret);
3368 Lisp_Object Vfeatures;
3370 DEFUN ("featurep", Ffeaturep, 1, 1, 0, /*
3371 Return non-nil if feature FEXP is present in this Emacs.
3372 Use this to conditionalize execution of lisp code based on the
3373 presence or absence of emacs or environment extensions.
3374 FEXP can be a symbol, a number, or a list.
3375 If it is a symbol, that symbol is looked up in the `features' variable,
3376 and non-nil will be returned if found.
3377 If it is a number, the function will return non-nil if this Emacs
3378 has an equal or greater version number than FEXP.
3379 If it is a list whose car is the symbol `and', it will return
3380 non-nil if all the features in its cdr are non-nil.
3381 If it is a list whose car is the symbol `or', it will return non-nil
3382 if any of the features in its cdr are non-nil.
3383 If it is a list whose car is the symbol `not', it will return
3384 non-nil if the feature is not present.
3389 => ; Non-nil on XEmacs.
3391 (featurep '(and xemacs gnus))
3392 => ; Non-nil on XEmacs with Gnus loaded.
3394 (featurep '(or tty-frames (and emacs 19.30)))
3395 => ; Non-nil if this Emacs supports TTY frames.
3397 (featurep '(or (and xemacs 19.15) (and emacs 19.34)))
3398 => ; Non-nil on XEmacs 19.15 and later, or FSF Emacs 19.34 and later.
3400 NOTE: The advanced arguments of this function (anything other than a
3401 symbol) are not yet supported by FSF Emacs. If you feel they are useful
3402 for supporting multiple Emacs variants, lobby Richard Stallman at
3403 <bug-gnu-emacs@prep.ai.mit.edu>.
3407 #ifndef FEATUREP_SYNTAX
3408 CHECK_SYMBOL (fexp);
3409 return NILP (Fmemq (fexp, Vfeatures)) ? Qnil : Qt;
3410 #else /* FEATUREP_SYNTAX */
3411 static double featurep_emacs_version;
3413 /* Brute force translation from Erik Naggum's lisp function. */
3416 /* Original definition */
3417 return NILP (Fmemq (fexp, Vfeatures)) ? Qnil : Qt;
3419 else if (INTP (fexp) || FLOATP (fexp))
3421 double d = extract_float (fexp);
3423 if (featurep_emacs_version == 0.0)
3425 featurep_emacs_version = XINT (Vemacs_major_version) +
3426 (XINT (Vemacs_minor_version) / 100.0);
3428 return featurep_emacs_version >= d ? Qt : Qnil;
3430 else if (CONSP (fexp))
3432 Lisp_Object tem = XCAR (fexp);
3438 negate = Fcar (tem);
3440 return NILP (call1 (Qfeaturep, negate)) ? Qt : Qnil;
3442 return Fsignal (Qinvalid_read_syntax, list1 (tem));
3444 else if (EQ (tem, Qand))
3447 /* Use Fcar/Fcdr for error-checking. */
3448 while (!NILP (tem) && !NILP (call1 (Qfeaturep, Fcar (tem))))
3452 return NILP (tem) ? Qt : Qnil;
3454 else if (EQ (tem, Qor))
3457 /* Use Fcar/Fcdr for error-checking. */
3458 while (!NILP (tem) && NILP (call1 (Qfeaturep, Fcar (tem))))
3462 return NILP (tem) ? Qnil : Qt;
3466 return Fsignal (Qinvalid_read_syntax, list1 (XCDR (fexp)));
3471 return Fsignal (Qinvalid_read_syntax, list1 (fexp));
3474 #endif /* FEATUREP_SYNTAX */
3476 DEFUN ("provide", Fprovide, 1, 1, 0, /*
3477 Announce that FEATURE is a feature of the current Emacs.
3478 This function updates the value of the variable `features'.
3483 CHECK_SYMBOL (feature);
3484 if (!NILP (Vautoload_queue))
3485 Vautoload_queue = Fcons (Fcons (Vfeatures, Qnil), Vautoload_queue);
3486 tem = Fmemq (feature, Vfeatures);
3488 Vfeatures = Fcons (feature, Vfeatures);
3489 LOADHIST_ATTACH (Fcons (Qprovide, feature));
3493 DEFUN ("require", Frequire, 1, 2, 0, /*
3494 If feature FEATURE is not loaded, load it from FILENAME.
3495 If FEATURE is not a member of the list `features', then the feature
3496 is not loaded; so load the file FILENAME.
3497 If FILENAME is omitted, the printname of FEATURE is used as the file name.
3499 (feature, file_name))
3502 CHECK_SYMBOL (feature);
3503 tem = Fmemq (feature, Vfeatures);
3504 LOADHIST_ATTACH (Fcons (Qrequire, feature));
3509 int speccount = specpdl_depth ();
3511 /* Value saved here is to be restored into Vautoload_queue */
3512 record_unwind_protect (un_autoload, Vautoload_queue);
3513 Vautoload_queue = Qt;
3515 call4 (Qload, NILP (file_name) ? Fsymbol_name (feature) : file_name,
3518 tem = Fmemq (feature, Vfeatures);
3520 error ("Required feature %s was not provided",
3521 string_data (XSYMBOL (feature)->name));
3523 /* Once loading finishes, don't undo it. */
3524 Vautoload_queue = Qt;
3525 return unbind_to (speccount, feature);
3529 /* base64 encode/decode functions.
3530 Based on code from GNU recode. */
3532 #define MIME_LINE_LENGTH 76
3534 #define IS_ASCII(Character) \
3536 #define IS_BASE64(Character) \
3537 (IS_ASCII (Character) && base64_char_to_value[Character] >= 0)
3539 /* Table of characters coding the 64 values. */
3540 static char base64_value_to_char[64] =
3542 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 0- 9 */
3543 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 10-19 */
3544 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', /* 20-29 */
3545 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', /* 30-39 */
3546 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', /* 40-49 */
3547 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', /* 50-59 */
3548 '8', '9', '+', '/' /* 60-63 */
3551 /* Table of base64 values for first 128 characters. */
3552 static short base64_char_to_value[128] =
3554 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0- 9 */
3555 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10- 19 */
3556 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20- 29 */
3557 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 30- 39 */
3558 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, /* 40- 49 */
3559 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, /* 50- 59 */
3560 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, /* 60- 69 */
3561 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 70- 79 */
3562 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, /* 80- 89 */
3563 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, /* 90- 99 */
3564 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, /* 100-109 */
3565 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, /* 110-119 */
3566 49, 50, 51, -1, -1, -1, -1, -1 /* 120-127 */
3569 /* The following diagram shows the logical steps by which three octets
3570 get transformed into four base64 characters.
3572 .--------. .--------. .--------.
3573 |aaaaaabb| |bbbbcccc| |ccdddddd|
3574 `--------' `--------' `--------'
3576 .--------+--------+--------+--------.
3577 |00aaaaaa|00bbbbbb|00cccccc|00dddddd|
3578 `--------+--------+--------+--------'
3580 .--------+--------+--------+--------.
3581 |AAAAAAAA|BBBBBBBB|CCCCCCCC|DDDDDDDD|
3582 `--------+--------+--------+--------'
3584 The octets are divided into 6 bit chunks, which are then encoded into
3585 base64 characters. */
3587 #define ADVANCE_INPUT(c, stream) \
3588 (ec = Lstream_get_emchar (stream), \
3591 (error ("Non-ascii character detected in base64 input"), 0) \
3592 : (c = (Bufbyte)ec, 1)))
3595 base64_encode_1 (Lstream *istream, Bufbyte *to, int line_break)
3597 EMACS_INT counter = 0;
3605 if (!ADVANCE_INPUT (c, istream))
3608 /* Wrap line every 76 characters. */
3611 if (counter < MIME_LINE_LENGTH / 4)
3620 /* Process first byte of a triplet. */
3621 *e++ = base64_value_to_char[0x3f & c >> 2];
3622 value = (0x03 & c) << 4;
3624 /* Process second byte of a triplet. */
3625 if (!ADVANCE_INPUT (c, istream))
3627 *e++ = base64_value_to_char[value];
3633 *e++ = base64_value_to_char[value | (0x0f & c >> 4)];
3634 value = (0x0f & c) << 2;
3636 /* Process third byte of a triplet. */
3637 if (!ADVANCE_INPUT (c, istream))
3639 *e++ = base64_value_to_char[value];
3644 *e++ = base64_value_to_char[value | (0x03 & c >> 6)];
3645 *e++ = base64_value_to_char[0x3f & c];
3648 /* Complete last partial line. */
3655 #undef ADVANCE_INPUT
3657 #define ADVANCE_INPUT(c, stream) \
3658 (ec = Lstream_get_emchar (stream), \
3659 ec == -1 ? 0 : (c = (Bufbyte)ec, 1))
3661 #define INPUT_EOF_P(stream) \
3662 (ADVANCE_INPUT (c2, stream) \
3663 ? (Lstream_unget_emchar (stream, (Emchar)c2), 0) \
3666 #define STORE_BYTE(pos, val) do { \
3667 pos += set_charptr_emchar (pos, (Emchar)((unsigned char)(val))); \
3672 base64_decode_1 (Lstream *istream, Bufbyte *to, Charcount *ccptr)
3674 EMACS_INT counter = 0;
3677 unsigned long value;
3684 if (!ADVANCE_INPUT (c, istream))
3687 /* Accept wrapping lines, reversibly if at each 76 characters. */
3690 if (!ADVANCE_INPUT (c, istream))
3692 if (INPUT_EOF_P (istream))
3694 /* FSF Emacs has this check, apparently inherited from
3695 recode. However, I see no reason to be this picky about
3696 line length -- why reject base64 with say 72-byte lines?
3697 (yes, there are programs that generate them.) */
3698 /*if (counter != MIME_LINE_LENGTH / 4) return -1;*/
3704 /* Process first byte of a quadruplet. */
3707 value = base64_char_to_value[c] << 18;
3709 /* Process second byte of a quadruplet. */
3710 if (!ADVANCE_INPUT (c, istream))
3715 value |= base64_char_to_value[c] << 12;
3717 STORE_BYTE (e, value >> 16);
3719 /* Process third byte of a quadruplet. */
3720 if (!ADVANCE_INPUT (c, istream))
3725 if (!ADVANCE_INPUT (c, istream))
3734 value |= base64_char_to_value[c] << 6;
3736 STORE_BYTE (e, 0xff & value >> 8);
3738 /* Process fourth byte of a quadruplet. */
3739 if (!ADVANCE_INPUT (c, istream))
3747 value |= base64_char_to_value[c];
3749 STORE_BYTE (e, 0xff & value);
3754 #undef ADVANCE_INPUT
3758 free_malloced_ptr (Lisp_Object unwind_obj)
3760 void *ptr = (void *)get_opaque_ptr (unwind_obj);
3762 free_opaque_ptr (unwind_obj);
3766 /* Don't use alloca for regions larger than this, lest we overflow
3768 #define MAX_ALLOCA 65536
3770 /* We need to setup proper unwinding, because there is a number of
3771 ways these functions can blow up, and we don't want to have memory
3772 leaks in those cases. */
3773 #define XMALLOC_OR_ALLOCA(ptr, len, type) do { \
3774 if ((len) > MAX_ALLOCA) \
3776 ptr = (type *)xmalloc ((len) * sizeof (type)); \
3777 speccount = specpdl_depth (); \
3778 record_unwind_protect (free_malloced_ptr, \
3779 make_opaque_ptr ((void *)ptr)); \
3782 ptr = alloca_array (type, len); \
3785 #define XMALLOC_UNBIND(ptr, len) do { \
3786 if ((len) > MAX_ALLOCA) \
3787 unbind_to (speccount, Qnil); \
3790 DEFUN ("base64-encode-region", Fbase64_encode_region, 2, 3, "r", /*
3791 Base64-encode the region between BEG and END.
3792 Return the length of the encoded text.
3793 Optional third argument NO-LINE-BREAK means do not break long lines
3796 (beg, end, no_line_break))
3799 Bytind encoded_length;
3800 Charcount allength, length;
3801 struct buffer *buf = current_buffer;
3802 Bufpos begv, zv, old_pt = BUF_PT (buf);
3806 get_buffer_range_char (buf, beg, end, &begv, &zv, 0);
3808 /* We need to allocate enough room for encoding the text.
3809 We need 33 1/3% more space, plus a newline every 76
3810 characters, and then we round up. */
3812 allength = length + length/3 + 1;
3813 allength += allength / MIME_LINE_LENGTH + 1 + 6;
3815 input = make_lisp_buffer_input_stream (buf, begv, zv, 0);
3816 /* We needn't multiply allength with MAX_EMCHAR_LEN because all the
3817 base64 characters will be single-byte. */
3818 XMALLOC_OR_ALLOCA (encoded, allength, Bufbyte);
3819 encoded_length = base64_encode_1 (XLSTREAM (input), encoded,
3820 NILP (no_line_break));
3821 if (encoded_length > allength)
3823 Lstream_delete (XLSTREAM (input));
3825 /* Now we have encoded the region, so we insert the new contents
3826 and delete the old. (Insert first in order to preserve markers.) */
3827 buffer_insert_raw_string_1 (buf, begv, encoded, encoded_length, 0);
3828 XMALLOC_UNBIND (encoded, allength);
3829 buffer_delete_range (buf, begv + encoded_length, zv + encoded_length, 0);
3831 /* Simulate FSF Emacs: if point was in the region, place it at the
3833 if (old_pt >= begv && old_pt < zv)
3834 BUF_SET_PT (buf, begv);
3836 /* We return the length of the encoded text. */
3837 return make_int (encoded_length);
3840 DEFUN ("base64-encode-string", Fbase64_encode_string, 1, 1, 0, /*
3841 Base64 encode STRING and return the result.
3845 Charcount allength, length;
3846 Bytind encoded_length;
3848 Lisp_Object input, result;
3851 CHECK_STRING (string);
3853 length = XSTRING_CHAR_LENGTH (string);
3854 allength = length + length/3 + 1 + 6;
3856 input = make_lisp_string_input_stream (string, 0, -1);
3857 XMALLOC_OR_ALLOCA (encoded, allength, Bufbyte);
3858 encoded_length = base64_encode_1 (XLSTREAM (input), encoded, 0);
3859 if (encoded_length > allength)
3861 Lstream_delete (XLSTREAM (input));
3862 result = make_string (encoded, encoded_length);
3863 XMALLOC_UNBIND (encoded, allength);
3867 DEFUN ("base64-decode-region", Fbase64_decode_region, 2, 2, "r", /*
3868 Base64-decode the region between BEG and END.
3869 Return the length of the decoded text.
3870 If the region can't be decoded, return nil and don't modify the buffer.
3874 struct buffer *buf = current_buffer;
3875 Bufpos begv, zv, old_pt = BUF_PT (buf);
3877 Bytind decoded_length;
3878 Charcount length, cc_decoded_length;
3882 get_buffer_range_char (buf, beg, end, &begv, &zv, 0);
3885 input = make_lisp_buffer_input_stream (buf, begv, zv, 0);
3886 /* We need to allocate enough room for decoding the text. */
3887 XMALLOC_OR_ALLOCA (decoded, length * MAX_EMCHAR_LEN, Bufbyte);
3888 decoded_length = base64_decode_1 (XLSTREAM (input), decoded, &cc_decoded_length);
3889 if (decoded_length > length * MAX_EMCHAR_LEN)
3891 Lstream_delete (XLSTREAM (input));
3893 if (decoded_length < 0)
3895 /* The decoding wasn't possible. */
3896 XMALLOC_UNBIND (decoded, length * MAX_EMCHAR_LEN);
3900 /* Now we have decoded the region, so we insert the new contents
3901 and delete the old. (Insert first in order to preserve markers.) */
3902 BUF_SET_PT (buf, begv);
3903 buffer_insert_raw_string_1 (buf, begv, decoded, decoded_length, 0);
3904 XMALLOC_UNBIND (decoded, length * MAX_EMCHAR_LEN);
3905 buffer_delete_range (buf, begv + cc_decoded_length,
3906 zv + cc_decoded_length, 0);
3908 /* Simulate FSF Emacs: if point was in the region, place it at the
3910 if (old_pt >= begv && old_pt < zv)
3911 BUF_SET_PT (buf, begv);
3913 return make_int (cc_decoded_length);
3916 DEFUN ("base64-decode-string", Fbase64_decode_string, 1, 1, 0, /*
3917 Base64-decode STRING and return the result.
3922 Bytind decoded_length;
3923 Charcount length, cc_decoded_length;
3924 Lisp_Object input, result;
3927 CHECK_STRING (string);
3929 length = XSTRING_CHAR_LENGTH (string);
3930 /* We need to allocate enough room for decoding the text. */
3931 XMALLOC_OR_ALLOCA (decoded, length * MAX_EMCHAR_LEN, Bufbyte);
3933 input = make_lisp_string_input_stream (string, 0, -1);
3934 decoded_length = base64_decode_1 (XLSTREAM (input), decoded,
3935 &cc_decoded_length);
3936 if (decoded_length > length * MAX_EMCHAR_LEN)
3938 Lstream_delete (XLSTREAM (input));
3940 if (decoded_length < 0)
3943 XMALLOC_UNBIND (decoded, length * MAX_EMCHAR_LEN);
3946 result = make_string (decoded, decoded_length);
3947 XMALLOC_UNBIND (decoded, length * MAX_EMCHAR_LEN);
3951 Lisp_Object Qyes_or_no_p;
3956 defsymbol (&Qstring_lessp, "string-lessp");
3957 defsymbol (&Qidentity, "identity");
3958 defsymbol (&Qyes_or_no_p, "yes-or-no-p");
3960 DEFSUBR (Fidentity);
3963 DEFSUBR (Fsafe_length);
3964 DEFSUBR (Fstring_equal);
3965 DEFSUBR (Fstring_lessp);
3966 DEFSUBR (Fstring_modified_tick);
3970 DEFSUBR (Fbvconcat);
3971 DEFSUBR (Fcopy_sequence);
3972 DEFSUBR (Fcopy_alist);
3973 DEFSUBR (Fcopy_tree);
3974 DEFSUBR (Fsubstring);
3980 DEFSUBR (Fold_member);
3982 DEFSUBR (Fold_memq);
3984 DEFSUBR (Fold_assoc);
3986 DEFSUBR (Fold_assq);
3988 DEFSUBR (Fold_rassoc);
3990 DEFSUBR (Fold_rassq);
3992 DEFSUBR (Fold_delete);
3994 DEFSUBR (Fold_delq);
3995 DEFSUBR (Fremassoc);
3997 DEFSUBR (Fremrassoc);
3998 DEFSUBR (Fremrassq);
3999 DEFSUBR (Fnreverse);
4002 DEFSUBR (Fplists_eq);
4003 DEFSUBR (Fplists_equal);
4004 DEFSUBR (Flax_plists_eq);
4005 DEFSUBR (Flax_plists_equal);
4006 DEFSUBR (Fplist_get);
4007 DEFSUBR (Fplist_put);
4008 DEFSUBR (Fplist_remprop);
4009 DEFSUBR (Fplist_member);
4010 DEFSUBR (Fcheck_valid_plist);
4011 DEFSUBR (Fvalid_plist_p);
4012 DEFSUBR (Fcanonicalize_plist);
4013 DEFSUBR (Flax_plist_get);
4014 DEFSUBR (Flax_plist_put);
4015 DEFSUBR (Flax_plist_remprop);
4016 DEFSUBR (Flax_plist_member);
4017 DEFSUBR (Fcanonicalize_lax_plist);
4018 DEFSUBR (Fdestructive_alist_to_plist);
4022 DEFSUBR (Fobject_plist);
4024 DEFSUBR (Fold_equal);
4025 DEFSUBR (Ffillarray);
4028 DEFSUBR (Fmapvector);
4030 DEFSUBR (Fmapconcat);
4031 DEFSUBR (Fload_average);
4032 DEFSUBR (Ffeaturep);
4035 DEFSUBR (Fbase64_encode_region);
4036 DEFSUBR (Fbase64_encode_string);
4037 DEFSUBR (Fbase64_decode_region);
4038 DEFSUBR (Fbase64_decode_string);
4042 init_provide_once (void)
4044 DEFVAR_LISP ("features", &Vfeatures /*
4045 A list of symbols which are the features of the executing emacs.
4046 Used by `featurep' and `require', and altered by `provide'.