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 SXEmacs
7 SXEmacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 SXEmacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 /* Synched up with: Mule 2.0, FSF 19.30. */
23 /* This file has been Mule-ized. */
25 /* Note: FSF 19.30 has bool vectors. We have bit vectors. */
27 /* Hacked on for Mule by Ben Wing, December 1994, January 1995. */
31 /* Note on some machines this defines `vector' as a typedef,
32 so make sure we don't use that name in this file. */
42 #include "ui/device.h"
43 #include "events/events.h"
47 #include "ui/insdel.h"
49 /* for the categorial views */
52 /* for all the map* funs */
56 /* NOTE: This symbol is also used in lread.c */
57 #define FEATUREP_SYNTAX
59 Lisp_Object Qstring_lessp, Qstring_greaterp;
60 Lisp_Object Qidentity;
62 static int internal_old_equal(Lisp_Object, Lisp_Object, int);
63 Lisp_Object safe_copy_tree(Lisp_Object arg, Lisp_Object vecp, int depth);
64 int internal_equalp(Lisp_Object, Lisp_Object, int);
66 static Lisp_Object mark_bit_vector(Lisp_Object obj)
72 print_bit_vector(Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
75 Lisp_Bit_Vector *v = XBIT_VECTOR(obj);
76 size_t len = bit_vector_length(v);
79 if (INTP(Vprint_length))
80 last = min((EMACS_INT) len, XINT(Vprint_length));
81 write_c_string("#*", printcharfun);
82 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);
93 static int bit_vector_equal(Lisp_Object obj1, Lisp_Object obj2, int depth)
95 Lisp_Bit_Vector *v1 = XBIT_VECTOR(obj1);
96 Lisp_Bit_Vector *v2 = XBIT_VECTOR(obj2);
98 return ((bit_vector_length(v1) == bit_vector_length(v2)) &&
99 !memcmp(v1->bits, v2->bits,
100 BIT_VECTOR_LONG_STORAGE(bit_vector_length(v1)) *
104 static unsigned long bit_vector_hash(Lisp_Object obj, int depth)
106 Lisp_Bit_Vector *v = XBIT_VECTOR(obj);
107 return HASH2(bit_vector_length(v),
109 BIT_VECTOR_LONG_STORAGE(bit_vector_length(v)) *
113 static size_t size_bit_vector(const void *lheader)
115 const Lisp_Bit_Vector *v = (const Lisp_Bit_Vector *) lheader;
116 return FLEXIBLE_ARRAY_STRUCT_SIZEOF(Lisp_Bit_Vector, unsigned long,
118 BIT_VECTOR_LONG_STORAGE
119 (bit_vector_length(v)));
122 static const struct lrecord_description bit_vector_description[] = {
123 {XD_LISP_OBJECT, offsetof(Lisp_Bit_Vector, next)},
127 DEFINE_BASIC_LRECORD_SEQUENCE_IMPLEMENTATION("bit-vector", bit_vector,
128 mark_bit_vector, print_bit_vector,
131 bit_vector_description,
132 size_bit_vector, Lisp_Bit_Vector);
134 DEFUN("identity", Fidentity, 1, 1, 0, /*
135 Return the argument unchanged.
142 extern long get_random(void);
143 extern void seed_random(long arg);
145 DEFUN("random", Frandom, 0, 1, 0, /*
146 Return a pseudo-random number.
147 All integers representable in Lisp are equally likely.
148 On most systems, this is 31 bits' worth.
150 With positive integer argument LIMIT, return random number
151 in interval [0,LIMIT). LIMIT can be a big integer, in which
152 case the range of possible values is extended.
154 With argument t, set the random number seed from the
155 current time and pid.
160 unsigned long denominator;
163 seed_random(getpid() + time(NULL));
164 if (NATNUMP(limit) && !ZEROP(limit)) {
165 /* Try to take our random number from the higher bits of VAL,
166 not the lower, since (says Gentzel) the low bits of `random'
167 are less random than the higher ones. We do this by using the
168 quotient rather than the remainder. At the high end of the RNG
169 it's possible to get a quotient larger than limit; discarding
170 these values eliminates the bias that would otherwise appear
171 when using a large limit. */
172 denominator = ((unsigned long)1 << INT_VALBITS) / XINT(limit);
174 val = get_random() / denominator;
175 while (val >= XINT(limit));
176 } else if (ZEROP(limit)) {
177 return wrong_type_argument(Qpositivep, limit);
178 #if defined HAVE_MPZ && defined WITH_GMP
179 } else if (BIGZP(limit)) {
183 if (bigz_sign(XBIGZ_DATA(limit)) <= 0)
184 return wrong_type_argument(Qpositivep, limit);
188 bigz_random(bz, XBIGZ_DATA(limit));
189 result = ent_mpz_downgrade_maybe(bz);
193 #endif /* HAVE_MPZ */
197 return make_int(val);
200 #if defined(WITH_GMP) && defined(HAVE_MPZ)
201 DEFUN("randomb", Frandomb, 1, 1, 0, /*
202 Return a uniform pseudo-random number in the range [0, 2^LIMIT).
210 CHECK_INTEGER(limit);
212 if (NILP(Fnonnegativep(limit)))
213 return wrong_type_argument(Qnonnegativep, limit);
214 else if (INTP(limit))
216 else if (BIGZP(limit) && bigz_fits_ulong_p(XBIGZ_DATA(limit)))
217 limui = bigz_to_ulong(XBIGZ_DATA(limit));
219 return wrong_type_argument(Qintegerp, limit);
223 mpz_urandomb(bz, random_state, limui);
224 result = make_bigz_bz(bz);
229 #endif /* HAVE_MPZ */
232 /* Random data-structure functions */
234 #ifdef LOSING_BYTECODE
236 /* #### Delete this shit */
238 /* Charcount is a misnomer here as we might be dealing with the
239 length of a vector or list, but emphasizes that we're not dealing
240 with Bytecounts in strings */
241 static Charcount length_with_bytecode_hack(Lisp_Object seq)
243 if (!COMPILED_FUNCTIONP(seq))
244 return XINT(Flength(seq));
246 Lisp_Compiled_Function *f = XCOMPILED_FUNCTION(seq);
248 return (f->flags.interactivep ? COMPILED_INTERACTIVE :
249 f->flags.domainp ? COMPILED_DOMAIN :
255 #endif /* LOSING_BYTECODE */
257 void check_losing_bytecode(const char *function, Lisp_Object seq)
259 if (COMPILED_FUNCTIONP(seq))
262 "As of 20.3, `%s' no longer works with compiled-function objects",
266 DEFUN("length", Flength, 1, 1, 0, /*
267 Return the length of vector, bit vector, list or string SEQUENCE.
272 /* that's whither we have to get */
273 if (LIKELY(!NILP(sequence))) {
274 return make_int(seq_length((seq_t)sequence));
280 if (LIKELY(STRINGP(sequence) ||
284 BIT_VECTORP(sequence))) {
285 return make_int(seq_length(sequence));
286 } else if (NILP(sequence)) {
289 check_losing_bytecode("length", sequence);
290 sequence = wrong_type_argument(Qsequencep, sequence);
295 if (STRINGP(sequence))
296 return make_int(XSTRING_CHAR_LENGTH(sequence));
297 else if (CONSP(sequence)) {
298 return make_int(seq_length(sequence));
299 } else if (VECTORP(sequence))
300 return make_int(seq_length(sequence));
301 else if (DLLISTP(sequence))
302 return make_int(XDLLIST_SIZE(sequence));
303 else if (NILP(sequence))
305 else if (BIT_VECTORP(sequence))
306 return make_int(bit_vector_length(XBIT_VECTOR(sequence)));
308 check_losing_bytecode("length", sequence);
309 sequence = wrong_type_argument(Qsequencep, sequence);
315 DEFUN("safe-length", Fsafe_length, 1, 1, 0, /*
316 Return the length of a list, but avoid error or infinite loop.
317 This function never gets an error. If LIST is not really a list,
318 it returns 0. If LIST is circular, it returns a finite value
319 which is at least the number of distinct elements.
323 Lisp_Object hare, tortoise;
326 for (hare = tortoise = list, len = 0;
327 CONSP(hare) && (!EQ(hare, tortoise) || len == 0);
328 hare = XCDR(hare), len++) {
330 tortoise = XCDR(tortoise);
333 return make_int(len);
336 /*** string functions. ***/
338 DEFUN("string-equal", Fstring_equal, 2, 2, 0, /*
339 Return t if two strings have identical contents.
340 Case is significant. Text properties are ignored.
341 \(Under SXEmacs, `equal' also ignores text properties and extents in
342 strings, but this is not the case under FSF Emacs 19. In FSF Emacs 20
343 `equal' is the same as in SXEmacs, in that respect.)
344 Symbols are also allowed; their print names are used instead.
349 Lisp_String *p1, *p2;
351 if (SYMBOLP(string1))
352 p1 = XSYMBOL(string1)->name;
354 CHECK_STRING(string1);
355 p1 = XSTRING(string1);
358 if (SYMBOLP(string2))
359 p2 = XSYMBOL(string2)->name;
361 CHECK_STRING(string2);
362 p2 = XSTRING(string2);
365 return (((len = string_length(p1)) == string_length(p2)) &&
366 !memcmp(string_data(p1), string_data(p2), len)) ? Qt : Qnil;
369 DEFUN("string-lessp", Fstring_lessp, 2, 2, 0, /*
370 Return t if first arg string is less than second in lexicographic order.
371 If I18N2 support (but not Mule support) was compiled in, ordering is
372 determined by the locale. (Case is significant for the default C locale.)
373 In all other cases, comparison is simply done on a character-by-
374 character basis using the numeric value of a character. (Note that
375 this may not produce particularly meaningful results under Mule if
376 characters from different charsets are being compared.)
378 Symbols are also allowed; their print names are used instead.
380 The reason that the I18N2 locale-specific collation is not used under
381 Mule is that the locale model of internationalization does not handle
382 multiple charsets and thus has no hope of working properly under Mule.
383 What we really should do is create a collation table over all built-in
384 charsets. This is extremely difficult to do from scratch, however.
386 Unicode is a good first step towards solving this problem. In fact,
387 it is quite likely that a collation table exists (or will exist) for
388 Unicode. When Unicode support is added to SXEmacs/Mule, this problem
393 Lisp_String *p1, *p2;
397 if (SYMBOLP(string1))
398 p1 = XSYMBOL(string1)->name;
400 CHECK_STRING(string1);
401 p1 = XSTRING(string1);
404 if (SYMBOLP(string2))
405 p2 = XSYMBOL(string2)->name;
407 CHECK_STRING(string2);
408 p2 = XSTRING(string2);
411 end = string_char_length(p1);
412 len2 = string_char_length(p2);
416 #if defined (I18N2) && !defined (MULE)
417 /* There is no hope of this working under Mule. Even if we converted
418 the data into an external format so that strcoll() processed it
419 properly, it would still not work because strcoll() does not
420 handle multiple locales. This is the fundamental flaw in the
423 Bytecount bcend = charcount_to_bytecount(string_data(p1), end);
424 /* Compare strings using collation order of locale. */
425 /* Need to be tricky to handle embedded nulls. */
427 for (i = 0; i < bcend;
428 i += strlen((char *)string_data(p1) + i) + 1) {
429 int val = strcoll((char *)string_data(p1) + i,
430 (char *)string_data(p2) + i);
437 #else /* not I18N2, or MULE */
439 Bufbyte *ptr1 = string_data(p1);
440 Bufbyte *ptr2 = string_data(p2);
442 /* #### It is not really necessary to do this: We could compare
443 byte-by-byte and still get a reasonable comparison, since this
444 would compare characters with a charset in the same way. With
445 a little rearrangement of the leading bytes, we could make most
446 inter-charset comparisons work out the same, too; even if some
447 don't, this is not a big deal because inter-charset comparisons
448 aren't really well-defined anyway. */
449 for (i = 0; i < end; i++) {
450 if (charptr_emchar(ptr1) != charptr_emchar(ptr2))
451 return charptr_emchar(ptr1) <
452 charptr_emchar(ptr2) ? Qt : Qnil;
457 #endif /* not I18N2, or MULE */
458 /* Can't do i < len2 because then comparison between "foo" and "foo^@"
459 won't work right in I18N2 case */
460 return end < len2 ? Qt : Qnil;
463 DEFUN("string-greaterp", Fstring_greaterp, 2, 2, 0, /*
464 Return t if first arg string is greater than second in lexicographic order.
465 If I18N2 support (but not Mule support) was compiled in, ordering is
466 determined by the locale. (Case is significant for the default C locale.)
467 In all other cases, comparison is simply done on a character-by-
468 character basis using the numeric value of a character. (Note that
469 this may not produce particularly meaningful results under Mule if
470 characters from different charsets are being compared.)
472 Symbols are also allowed; their print names are used instead.
474 The reason that the I18N2 locale-specific collation is not used under
475 Mule is that the locale model of internationalization does not handle
476 multiple charsets and thus has no hope of working properly under Mule.
477 What we really should do is create a collation table over all built-in
478 charsets. This is extremely difficult to do from scratch, however.
480 Unicode is a good first step towards solving this problem. In fact,
481 it is quite likely that a collation table exists (or will exist) for
482 Unicode. When Unicode support is added to SXEmacs/Mule, this problem
487 return Fstring_lessp(string2, string1);
490 DEFUN("string-modified-tick", Fstring_modified_tick, 1, 1, 0, /*
491 Return STRING's tick counter, incremented for each change to the string.
492 Each string has a tick counter which is incremented each time the contents
493 of the string are changed (e.g. with `aset'). It wraps around occasionally.
499 CHECK_STRING(string);
501 if (CONSP(s->plist) && INTP(XCAR(s->plist)))
502 return XCAR(s->plist);
507 void bump_string_modiff(Lisp_Object str)
509 Lisp_String *s = XSTRING(str);
510 Lisp_Object *ptr = &s->plist;
513 /* #### remove the `string-translatable' property from the string,
516 /* skip over extent info if it's there */
517 if (CONSP(*ptr) && EXTENT_INFOP(XCAR(*ptr)))
519 if (CONSP(*ptr) && INTP(XCAR(*ptr)))
520 XSETINT(XCAR(*ptr), 1 + XINT(XCAR(*ptr)));
522 *ptr = Fcons(make_int(1), *ptr);
525 enum concat_target_type { c_cons, c_string, c_vector, c_bit_vector, c_dllist };
526 static Lisp_Object concat(int nargs, Lisp_Object * args,
527 enum concat_target_type target_type,
530 Lisp_Object concat2(Lisp_Object string1, Lisp_Object string2)
535 return concat(2, args, c_string, 0);
539 concat3(Lisp_Object string1, Lisp_Object string2, Lisp_Object string3)
545 return concat(3, args, c_string, 0);
548 Lisp_Object vconcat2(Lisp_Object vec1, Lisp_Object vec2)
553 return concat(2, args, c_vector, 0);
556 Lisp_Object vconcat3(Lisp_Object vec1, Lisp_Object vec2, Lisp_Object vec3)
562 return concat(3, args, c_vector, 0);
565 DEFUN("append", Fappend, 0, MANY, 0, /*
566 Concatenate all the arguments and make the result a list.
567 The result is a list whose elements are the elements of all the arguments.
568 Each argument may be a list, vector, bit vector, or string.
569 The last argument is not copied, just used as the tail of the new list.
572 (int nargs, Lisp_Object * args))
574 return concat(nargs, args, c_cons, 1);
577 DEFUN("concat", Fconcat, 0, MANY, 0, /*
578 Concatenate all the arguments and make the result a string.
579 The result is a string whose elements are the elements of all the arguments.
580 Each argument may be a string or a list or vector of characters.
582 As of XEmacs 21.0, this function does NOT accept individual integers
583 as arguments. Old code that relies on, for example, (concat "foo" 50)
584 returning "foo50" will fail. To fix such code, either apply
585 `int-to-string' to the integer argument, or use `format'.
587 (int nargs, Lisp_Object * args))
589 return concat(nargs, args, c_string, 0);
592 DEFUN("vconcat", Fvconcat, 0, MANY, 0, /*
593 Concatenate all the arguments and make the result a vector.
594 The result is a vector whose elements are the elements of all the arguments.
595 Each argument may be a list, vector, bit vector, or string.
597 (int nargs, Lisp_Object * args))
599 return concat(nargs, args, c_vector, 0);
602 DEFUN("bvconcat", Fbvconcat, 0, MANY, 0, /*
603 Concatenate all the arguments and make the result a bit vector.
604 The result is a bit vector whose elements are the elements of all the
605 arguments. Each argument may be a list, vector, bit vector, or string.
607 (int nargs, Lisp_Object * args))
609 return concat(nargs, args, c_bit_vector, 0);
612 /* Copy a (possibly dotted) list. LIST must be a cons.
613 Can't use concat (1, &alist, c_cons, 0) - doesn't handle dotted lists. */
614 static Lisp_Object copy_list(Lisp_Object list)
616 Lisp_Object list_copy = Fcons(XCAR(list), XCDR(list));
617 Lisp_Object last = list_copy;
618 Lisp_Object hare, tortoise;
621 for (tortoise = hare = XCDR(list), len = 1;
622 CONSP(hare); hare = XCDR(hare), len++) {
623 XCDR(last) = Fcons(XCAR(hare), XCDR(hare));
626 if (len < CIRCULAR_LIST_SUSPICION_LENGTH)
629 tortoise = XCDR(tortoise);
630 if (EQ(tortoise, hare))
631 signal_circular_list_error(list);
637 DEFUN("copy-list", Fcopy_list, 1, 1, 0, /*
638 Return a copy of list LIST, which may be a dotted list.
639 The elements of LIST are not copied; they are shared
648 return copy_list(list);
650 list = wrong_type_argument(Qlistp, list);
654 DEFUN("copy-sequence", Fcopy_sequence, 1, 1, 0, /*
655 Return a copy of list, dllist, vector, bit vector or string SEQUENCE.
656 The elements of a list or vector are not copied; they are shared
657 with the original. SEQUENCE may be a dotted list.
665 return copy_list(sequence);
666 if (DLLISTP(sequence))
667 return Fcopy_dllist(sequence);
668 if (STRINGP(sequence))
669 return concat(1, &sequence, c_string, 0);
670 if (VECTORP(sequence))
671 return concat(1, &sequence, c_vector, 0);
672 if (BIT_VECTORP(sequence))
673 return concat(1, &sequence, c_bit_vector, 0);
675 check_losing_bytecode("copy-sequence", sequence);
676 sequence = wrong_type_argument(Qsequencep, sequence);
680 struct merge_string_extents_struct {
682 Bytecount entry_offset;
683 Bytecount entry_length;
687 concat(int nargs, Lisp_Object * args,
688 enum concat_target_type target_type, int last_special)
691 Lisp_Object tail = Qnil;
694 Lisp_Object last_tail;
696 struct merge_string_extents_struct *args_mse = 0;
697 Bufbyte *string_result = NULL;
698 Bufbyte *string_result_ptr = NULL;
700 int speccount = specpdl_depth();
701 Charcount total_length;
704 /* The modus operandi in Emacs is "caller gc-protects args".
705 However, concat is called many times in Emacs on freshly
706 created stuff. So we help those callers out by protecting
707 the args ourselves to save them a lot of temporary-variable
713 /* #### if the result is a string and any of the strings have a string
714 for the `string-translatable' property, then concat should also
715 concat the args but use the `string-translatable' strings, and store
716 the result in the returned string's `string-translatable' property. */
718 if (target_type == c_string)
719 XMALLOC_OR_ALLOCA(args_mse, nargs, struct merge_string_extents_struct);
721 /* In append, the last arg isn't treated like the others */
722 if (last_special && nargs > 0) {
724 last_tail = args[nargs];
728 /* Check and coerce the arguments. */
729 for (argnum = 0; argnum < nargs; argnum++) {
730 Lisp_Object seq = args[argnum];
731 if (LISTP(seq) || DLLISTP(seq)) ;
732 else if (VECTORP(seq) || STRINGP(seq) || BIT_VECTORP(seq)) ;
733 #ifdef LOSING_BYTECODE
734 else if (COMPILED_FUNCTIONP(seq))
735 /* Urk! We allow this, for "compatibility"... */
738 #if 0 /* removed for XEmacs 21 */
740 /* This is too revolting to think about but maintains
741 compatibility with FSF (and lots and lots of old code). */
742 args[argnum] = Fnumber_to_string(seq);
745 check_losing_bytecode("concat", seq);
746 args[argnum] = wrong_type_argument(Qsequencep, seq);
751 args_mse[argnum].string = seq;
753 args_mse[argnum].string = Qnil;
758 /* Charcount is a misnomer here as we might be dealing with the
759 length of a vector or list, but emphasizes that we're not dealing
760 with Bytecounts in strings */
761 /* Charcount total_length; */
763 for (argnum = 0, total_length = 0; argnum < nargs; argnum++) {
764 #ifdef LOSING_BYTECODE
766 length_with_bytecode_hack(args[argnum]);
768 Charcount thislen = XINT(Flength(args[argnum]));
770 total_length += thislen;
773 switch (target_type) {
775 if (total_length == 0) {
776 /* In append, if all but last arg are nil,
778 XMALLOC_UNBIND(args_mse, nargs, speccount);
779 RETURN_UNGCPRO(last_tail);
781 val = Fmake_list(make_int(total_length), Qnil);
784 if (total_length == 0) {
785 /* In append, if all but last arg are nil,
787 XMALLOC_UNBIND(args_mse, nargs, speccount);
788 RETURN_UNGCPRO(last_tail);
790 val = Fmake_list(make_int(total_length), Qnil);
793 val = make_vector(total_length, Qnil);
796 val = make_bit_vector(total_length, Qzero);
799 /* We don't make the string yet because we don't know
800 the actual number of bytes. This loop was formerly
801 written to call Fmake_string() here and then call
802 set_string_char() for each char. This seems logical
803 enough but is waaaaaaaay slow -- set_string_char()
804 has to scan the whole string up to the place where
805 the substitution is called for in order to find the
806 place to change, and may have to do some realloc()ing
807 in order to make the char fit properly. O(N^2)
810 XMALLOC_ATOMIC_OR_ALLOCA( string_result,
811 total_length * MAX_EMCHAR_LEN,
813 string_result_ptr = string_result;
822 tail = val, toindex = -1; /* -1 in toindex is flag we are
829 for (argnum = 0; argnum < nargs; argnum++) {
830 Charcount thisleni = 0;
831 Charcount thisindex = 0;
832 Lisp_Object seq = args[argnum];
833 Bufbyte *string_source_ptr = 0;
834 Bufbyte *string_prev_result_ptr = string_result_ptr;
837 #ifdef LOSING_BYTECODE
838 thisleni = length_with_bytecode_hack(seq);
840 thisleni = XINT(Flength(seq));
844 string_source_ptr = XSTRING_DATA(seq);
849 /* We've come to the end of this arg, so exit. */
853 /* Fetch next element of `seq' arg into `elt' */
858 if (thisindex >= thisleni)
863 make_char(charptr_emchar
864 (string_source_ptr));
865 INC_CHARPTR(string_source_ptr);
866 } else if (VECTORP(seq))
867 elt = XVECTOR_DATA(seq)[thisindex];
868 else if (BIT_VECTORP(seq))
870 make_int(bit_vector_bit
874 elt = Felt(seq, make_int(thisindex));
878 /* Store into result */
880 /* toindex negative means we are making a list */
884 } else if (VECTORP(val))
885 XVECTOR_DATA(val)[toindex++] = elt;
886 else if (BIT_VECTORP(val)) {
888 set_bit_vector_bit(XBIT_VECTOR(val), toindex++,
891 CHECK_CHAR_COERCE_INT(elt);
892 if(string_result_ptr != NULL) {
894 set_charptr_emchar(string_result_ptr,
902 args_mse[argnum].entry_offset =
903 string_prev_result_ptr - string_result;
904 args_mse[argnum].entry_length =
905 string_result_ptr - string_prev_result_ptr;
909 /* Now we finally make the string. */
910 if (target_type == c_string) {
912 make_string(string_result,
913 string_result_ptr - string_result);
914 for (argnum = 0; argnum < nargs; argnum++) {
915 if (STRINGP(args_mse[argnum].string))
916 copy_string_extents(val,
917 args_mse[argnum].string,
923 XMALLOC_UNBIND(string_result,
924 total_length * MAX_EMCHAR_LEN, speccount);
925 XMALLOC_UNBIND(args_mse, nargs, speccount);
929 XCDR(prev) = last_tail;
934 DEFUN("copy-alist", Fcopy_alist, 1, 1, 0, /*
935 Return a copy of ALIST.
936 This is an alist which represents the same mapping from objects to objects,
937 but does not share the alist structure with ALIST.
938 The objects mapped (cars and cdrs of elements of the alist)
940 Elements of ALIST that are not conses are also shared.
950 alist = concat(1, &alist, c_cons, 0);
951 for (tail = alist; CONSP(tail); tail = XCDR(tail)) {
952 Lisp_Object car = XCAR(tail);
955 XCAR(tail) = Fcons(XCAR(car), XCDR(car));
960 DEFUN("copy-tree", Fcopy_tree, 1, 2, 0, /*
961 Return a copy of a list and substructures.
962 The argument is copied, and any lists contained within it are copied
963 recursively. Circularities and shared substructures are not preserved.
964 Second arg VECP causes vectors to be copied, too. Strings and bit vectors
969 return safe_copy_tree(arg, vecp, 0);
972 Lisp_Object safe_copy_tree(Lisp_Object arg, Lisp_Object vecp, int depth)
975 signal_simple_error("Stack overflow in copy-tree", arg);
979 rest = arg = Fcopy_sequence(arg);
980 while (CONSP(rest)) {
981 Lisp_Object elt = XCAR(rest);
983 if (CONSP(elt) || VECTORP(elt))
985 safe_copy_tree(elt, vecp, depth + 1);
986 if (VECTORP(XCDR(rest))) /* hack for (a b . [c d]) */
988 safe_copy_tree(XCDR(rest), vecp, depth + 1);
991 } else if (VECTORP(arg) && !NILP(vecp)) {
992 int i = XVECTOR_LENGTH(arg);
994 arg = Fcopy_sequence(arg);
995 for (j = 0; j < i; j++) {
996 Lisp_Object elt = XVECTOR_DATA(arg)[j];
998 if (CONSP(elt) || VECTORP(elt))
999 XVECTOR_DATA(arg)[j] =
1000 safe_copy_tree(elt, vecp, depth + 1);
1006 DEFUN("substring", Fsubstring, 2, 3, 0, /*
1007 Return the substring of STRING starting at START and ending before END.
1008 END may be nil or omitted; then the substring runs to the end of STRING.
1009 If START or END is negative, it counts from the end.
1010 Relevant parts of the string-extent-data are copied to the new string.
1012 (string, start, end))
1014 Charcount ccstart, ccend;
1015 Bytecount bstart, blen;
1018 CHECK_STRING(string);
1020 get_string_range_char(string, start, end, &ccstart, &ccend,
1021 GB_HISTORICAL_STRING_BEHAVIOR);
1022 bstart = charcount_to_bytecount(XSTRING_DATA(string), ccstart);
1024 charcount_to_bytecount(XSTRING_DATA(string) + bstart,
1026 val = make_string(XSTRING_DATA(string) + bstart, blen);
1027 /* Copy any applicable extent information into the new string. */
1028 copy_string_extents(val, string, 0, bstart, blen);
1032 DEFUN("subseq", Fsubseq, 2, 3, 0, /*
1033 Return the subsequence of SEQUENCE starting at START and ending before END.
1034 END may be omitted; then the subsequence runs to the end of SEQUENCE.
1035 If START or END is negative, it counts from the end.
1036 The returned subsequence is always of the same type as SEQUENCE.
1037 If SEQUENCE is a string, relevant parts of the string-extent-data
1038 are copied to the new string.
1040 (sequence, start, end))
1042 EMACS_INT len, s, e;
1044 if (STRINGP(sequence))
1045 return Fsubstring(sequence, start, end);
1047 len = XINT(Flength(sequence));
1063 if (!(0 <= s && s <= e && e <= len))
1064 args_out_of_range_3(sequence, make_int(s), make_int(e));
1066 if (VECTORP(sequence)) {
1067 Lisp_Object result = make_vector(e - s, Qnil);
1069 Lisp_Object *in_elts = XVECTOR_DATA(sequence);
1070 Lisp_Object *out_elts = XVECTOR_DATA(result);
1072 for (i = s; i < e; i++)
1073 out_elts[i - s] = in_elts[i];
1075 } else if (LISTP(sequence)) {
1076 Lisp_Object result = Qnil;
1079 sequence = Fnthcdr(make_int(s), sequence);
1081 for (i = s; i < e; i++) {
1082 result = Fcons(Fcar(sequence), result);
1083 sequence = Fcdr(sequence);
1086 return Fnreverse(result);
1087 } else if (BIT_VECTORP(sequence)) {
1088 Lisp_Object result = make_bit_vector(e - s, Qzero);
1091 for (i = s; i < e; i++)
1092 set_bit_vector_bit(XBIT_VECTOR(result), i - s,
1093 bit_vector_bit(XBIT_VECTOR(sequence),
1097 abort(); /* unreachable, since Flength (sequence) did not get
1103 DEFUN("nthcdr", Fnthcdr, 2, 2, 0, /*
1104 Take cdr N times on LIST, and return the result.
1109 REGISTER Lisp_Object tail = list;
1111 for (i = XINT(n); i; i--) {
1114 else if (NILP(tail))
1117 tail = wrong_type_argument(Qlistp, tail);
1124 DEFUN("nth", Fnth, 2, 2, 0, /*
1125 Return the Nth element of LIST.
1126 N counts from zero. If LIST is not that long, nil is returned.
1130 return Fcar(Fnthcdr(n, list));
1133 DEFUN("elt", Felt, 2, 2, 0, /*
1134 Return element of SEQUENCE at index N.
1139 if (!(INTP(n) || CHARP(n))) {
1140 n = wrong_type_argument(Qinteger_or_char_p, n);
1144 if (LISTP(sequence)) {
1145 Lisp_Object tem = Fnthcdr(n, sequence);
1146 /* #### Utterly, completely, fucking disgusting.
1147 * #### The whole point of "elt" is that it operates on
1148 * #### sequences, and does error- (bounds-) checking.
1154 /* This is The Way It Has Always Been. */
1157 /* This is The Way Mly and Cltl2 say It Should Be. */
1158 args_out_of_range(sequence, n);
1160 } else if (DLLISTP(sequence)) {
1161 dllist_item_t elm = NULL;
1164 EMACS_INT rn = ent_int(n);
1167 args_out_of_range(sequence, n);
1171 if (rn * 2 < (EMACS_INT)XDLLIST_SIZE(sequence)) {
1172 /* start at the front */
1173 elm = XDLLIST_FIRST(sequence);
1176 /* start at the end */
1177 elm = XDLLIST_LAST(sequence);
1179 i = XDLLIST_SIZE(sequence) - rn - 1;
1182 for (; i > 0 && elm != NULL; i--)
1189 return (Lisp_Object)elm->item;
1193 } else if (STRINGP(sequence) ||
1194 VECTORP(sequence) || BIT_VECTORP(sequence))
1195 return Faref(sequence, n);
1196 #ifdef LOSING_BYTECODE
1197 else if (COMPILED_FUNCTIONP(sequence)) {
1198 EMACS_INT idx = ent_int(n);
1201 args_out_of_range(sequence, n);
1203 /* Utter perversity */
1205 Lisp_Compiled_Function *f =
1206 XCOMPILED_FUNCTION(sequence);
1208 case COMPILED_ARGLIST:
1209 return compiled_function_arglist(f);
1210 case COMPILED_INSTRUCTIONS:
1211 return compiled_function_instructions(f);
1212 case COMPILED_CONSTANTS:
1213 return compiled_function_constants(f);
1214 case COMPILED_STACK_DEPTH:
1215 return compiled_function_stack_depth(f);
1216 case COMPILED_DOC_STRING:
1217 return compiled_function_documentation(f);
1218 case COMPILED_DOMAIN:
1219 return compiled_function_domain(f);
1220 case COMPILED_INTERACTIVE:
1221 if (f->flags.interactivep)
1222 return compiled_function_interactive(f);
1223 /* if we return nil, can't tell interactive with no args
1224 from noninteractive. */
1231 #endif /* LOSING_BYTECODE */
1233 check_losing_bytecode("elt", sequence);
1234 sequence = wrong_type_argument(Qsequencep, sequence);
1239 DEFUN("last", Flast, 1, 2, 0, /*
1240 Return the tail of list LIST, of length N (default 1).
1241 LIST may be a dotted list, but not a circular list.
1242 Optional argument N must be a non-negative integer.
1243 If N is zero, then the atom that terminates the list is returned.
1244 If N is greater than the length of LIST, then LIST itself is returned.
1248 EMACS_INT int_n, count;
1249 Lisp_Object retval, tortoise, hare;
1252 return Fdllist_rac(list);
1263 for (retval = tortoise = hare = list, count = 0;
1266 (int_n-- <= 0 ? ((void)(retval = XCDR(retval))) : (void)0),
1268 if (count < CIRCULAR_LIST_SUSPICION_LENGTH)
1272 tortoise = XCDR(tortoise);
1273 if (EQ(hare, tortoise))
1274 signal_circular_list_error(list);
1280 DEFUN("nbutlast", Fnbutlast, 1, 2, 0, /*
1281 Modify LIST to remove the last N (default 1) elements.
1282 If LIST has N or fewer elements, nil is returned and LIST is unmodified.
1298 Lisp_Object last_cons = list;
1300 EXTERNAL_LIST_LOOP_1(list) {
1302 last_cons = XCDR(last_cons);
1308 XCDR(last_cons) = Qnil;
1313 DEFUN("butlast", Fbutlast, 1, 2, 0, /*
1314 Return a copy of LIST with the last N (default 1) elements removed.
1315 If LIST has N or fewer elements, nil is returned.
1331 Lisp_Object retval = Qnil;
1332 Lisp_Object tail = list;
1334 EXTERNAL_LIST_LOOP_1(list) {
1336 retval = Fcons(XCAR(tail), retval);
1341 return Fnreverse(retval);
1345 DEFUN("member", Fmember, 2, 2, 0, /*
1346 Return non-nil if ELT is an element of LIST. Comparison done with `equal'.
1347 The value is actually the tail of LIST whose car is ELT.
1351 EXTERNAL_LIST_LOOP_3(list_elt, list, tail) {
1352 if (internal_equal(elt, list_elt, 0))
1358 DEFUN("old-member", Fold_member, 2, 2, 0, /*
1359 Return non-nil if ELT is an element of LIST. Comparison done with `old-equal'.
1360 The value is actually the tail of LIST whose car is ELT.
1361 This function is provided only for byte-code compatibility with v19.
1366 EXTERNAL_LIST_LOOP_3(list_elt, list, tail) {
1367 if (internal_old_equal(elt, list_elt, 0))
1373 DEFUN("memq", Fmemq, 2, 2, 0, /*
1374 Return non-nil if ELT is an element of LIST. Comparison done with `eq'.
1375 The value is actually the tail of LIST whose car is ELT.
1379 EXTERNAL_LIST_LOOP_3(list_elt, list, tail) {
1380 if (EQ_WITH_EBOLA_NOTICE(elt, list_elt))
1386 DEFUN("old-memq", Fold_memq, 2, 2, 0, /*
1387 Return non-nil if ELT is an element of LIST. Comparison done with `old-eq'.
1388 The value is actually the tail of LIST whose car is ELT.
1389 This function is provided only for byte-code compatibility with v19.
1394 EXTERNAL_LIST_LOOP_3(list_elt, list, tail) {
1395 if (HACKEQ_UNSAFE(elt, list_elt))
1401 Lisp_Object memq_no_quit(Lisp_Object elt, Lisp_Object list)
1403 LIST_LOOP_3(list_elt, list, tail) {
1404 if (EQ_WITH_EBOLA_NOTICE(elt, list_elt))
1410 DEFUN("assoc", Fassoc, 2, 2, 0, /*
1411 Return non-nil if KEY is `equal' to the car of an element of ALIST.
1412 The value is actually the element of ALIST whose car equals KEY.
1416 /* This function can GC. */
1417 EXTERNAL_ALIST_LOOP_4(elt, elt_car, elt_cdr, alist) {
1418 if (internal_equal(key, elt_car, 0))
1424 DEFUN("old-assoc", Fold_assoc, 2, 2, 0, /*
1425 Return non-nil if KEY is `old-equal' to the car of an element of ALIST.
1426 The value is actually the element of ALIST whose car equals KEY.
1430 /* This function can GC. */
1431 EXTERNAL_ALIST_LOOP_4(elt, elt_car, elt_cdr, alist) {
1432 if (internal_old_equal(key, elt_car, 0))
1438 Lisp_Object assoc_no_quit(Lisp_Object key, Lisp_Object alist)
1440 int speccount = specpdl_depth();
1441 specbind(Qinhibit_quit, Qt);
1442 return unbind_to(speccount, Fassoc(key, alist));
1445 DEFUN("assq", Fassq, 2, 2, 0, /*
1446 Return non-nil if KEY is `eq' to the car of an element of ALIST.
1447 The value is actually the element of ALIST whose car is KEY.
1448 Elements of ALIST that are not conses are ignored.
1452 EXTERNAL_ALIST_LOOP_4(elt, elt_car, elt_cdr, alist) {
1453 if (EQ_WITH_EBOLA_NOTICE(key, elt_car))
1459 DEFUN("old-assq", Fold_assq, 2, 2, 0, /*
1460 Return non-nil if KEY is `old-eq' to the car of an element of ALIST.
1461 The value is actually the element of ALIST whose car is KEY.
1462 Elements of ALIST that are not conses are ignored.
1463 This function is provided only for byte-code compatibility with v19.
1468 EXTERNAL_ALIST_LOOP_4(elt, elt_car, elt_cdr, alist) {
1469 if (HACKEQ_UNSAFE(key, elt_car))
1475 /* Like Fassq but never report an error and do not allow quits.
1476 Use only on lists known never to be circular. */
1478 Lisp_Object assq_no_quit(Lisp_Object key, Lisp_Object alist)
1480 /* This cannot GC. */
1481 LIST_LOOP_2(elt, alist) {
1482 Lisp_Object elt_car = XCAR(elt);
1483 if (EQ_WITH_EBOLA_NOTICE(key, elt_car))
1489 DEFUN("rassoc", Frassoc, 2, 2, 0, /*
1490 Return non-nil if VALUE is `equal' to the cdr of an element of ALIST.
1491 The value is actually the element of ALIST whose cdr equals VALUE.
1495 EXTERNAL_ALIST_LOOP_4(elt, elt_car, elt_cdr, alist) {
1496 if (internal_equal(value, elt_cdr, 0))
1502 DEFUN("old-rassoc", Fold_rassoc, 2, 2, 0, /*
1503 Return non-nil if VALUE is `old-equal' to the cdr of an element of ALIST.
1504 The value is actually the element of ALIST whose cdr equals VALUE.
1508 EXTERNAL_ALIST_LOOP_4(elt, elt_car, elt_cdr, alist) {
1509 if (internal_old_equal(value, elt_cdr, 0))
1515 DEFUN("rassq", Frassq, 2, 2, 0, /*
1516 Return non-nil if VALUE is `eq' to the cdr of an element of ALIST.
1517 The value is actually the element of ALIST whose cdr is VALUE.
1521 EXTERNAL_ALIST_LOOP_4(elt, elt_car, elt_cdr, alist) {
1522 if (EQ_WITH_EBOLA_NOTICE(value, elt_cdr))
1528 DEFUN("old-rassq", Fold_rassq, 2, 2, 0, /*
1529 Return non-nil if VALUE is `old-eq' to the cdr of an element of ALIST.
1530 The value is actually the element of ALIST whose cdr is VALUE.
1534 EXTERNAL_ALIST_LOOP_4(elt, elt_car, elt_cdr, alist) {
1535 if (HACKEQ_UNSAFE(value, elt_cdr))
1541 /* Like Frassq, but caller must ensure that ALIST is properly
1542 nil-terminated and ebola-free. */
1543 Lisp_Object rassq_no_quit(Lisp_Object value, Lisp_Object alist)
1545 LIST_LOOP_2(elt, alist) {
1546 Lisp_Object elt_cdr = XCDR(elt);
1547 if (EQ_WITH_EBOLA_NOTICE(value, elt_cdr))
1553 DEFUN("delete", Fdelete, 2, 2, 0, /*
1554 Delete by side effect any occurrences of ELT as a member of LIST.
1555 The modified LIST is returned. Comparison is done with `equal'.
1556 If the first member of LIST is ELT, there is no way to remove it by side
1557 effect; therefore, write `(setq foo (delete element foo))' to be sure
1558 of changing the value of `foo'.
1563 EXTERNAL_LIST_LOOP_DELETE_IF(list_elt, list,
1564 (internal_equal(elt, list_elt, 0)));
1568 DEFUN("old-delete", Fold_delete, 2, 2, 0, /*
1569 Delete by side effect any occurrences of ELT as a member of LIST.
1570 The modified LIST is returned. Comparison is done with `old-equal'.
1571 If the first member of LIST is ELT, there is no way to remove it by side
1572 effect; therefore, write `(setq foo (old-delete element foo))' to be sure
1573 of changing the value of `foo'.
1577 EXTERNAL_LIST_LOOP_DELETE_IF(list_elt, list,
1578 (internal_old_equal(elt, list_elt, 0)));
1582 DEFUN("delq", Fdelq, 2, 2, 0, /*
1583 Delete by side effect any occurrences of ELT as a member of LIST.
1584 The modified LIST is returned. Comparison is done with `eq'.
1585 If the first member of LIST is ELT, there is no way to remove it by side
1586 effect; therefore, write `(setq foo (delq element foo))' to be sure of
1587 changing the value of `foo'.
1591 EXTERNAL_LIST_LOOP_DELETE_IF(list_elt, list,
1592 (EQ_WITH_EBOLA_NOTICE(elt, list_elt)));
1596 DEFUN("old-delq", Fold_delq, 2, 2, 0, /*
1597 Delete by side effect any occurrences of ELT as a member of LIST.
1598 The modified LIST is returned. Comparison is done with `old-eq'.
1599 If the first member of LIST is ELT, there is no way to remove it by side
1600 effect; therefore, write `(setq foo (old-delq element foo))' to be sure of
1601 changing the value of `foo'.
1605 EXTERNAL_LIST_LOOP_DELETE_IF(list_elt, list,
1606 (HACKEQ_UNSAFE(elt, list_elt)));
1610 /* Like Fdelq, but caller must ensure that LIST is properly
1611 nil-terminated and ebola-free. */
1613 Lisp_Object delq_no_quit(Lisp_Object elt, Lisp_Object list)
1615 LIST_LOOP_DELETE_IF(list_elt, list,
1616 (EQ_WITH_EBOLA_NOTICE(elt, list_elt)));
1620 /* Be VERY careful with this. This is like delq_no_quit() but
1621 also calls free_cons() on the removed conses. You must be SURE
1622 that no pointers to the freed conses remain around (e.g.
1623 someone else is pointing to part of the list). This function
1624 is useful on internal lists that are used frequently and where
1625 the actual list doesn't escape beyond known code bounds. */
1627 Lisp_Object delq_no_quit_and_free_cons(Lisp_Object elt, Lisp_Object list)
1629 REGISTER Lisp_Object tail = list;
1630 REGISTER Lisp_Object prev = Qnil;
1632 while (!NILP(tail)) {
1633 REGISTER Lisp_Object tem = XCAR(tail);
1635 Lisp_Object cons_to_free = tail;
1639 XCDR(prev) = XCDR(tail);
1641 free_cons(XCONS(cons_to_free));
1650 DEFUN("remassoc", Fremassoc, 2, 2, 0, /*
1651 Delete by side effect any elements of ALIST whose car is `equal' to KEY.
1652 The modified ALIST is returned. If the first member of ALIST has a car
1653 that is `equal' to KEY, there is no way to remove it by side effect;
1654 therefore, write `(setq foo (remassoc key foo))' to be sure of changing
1659 EXTERNAL_LIST_LOOP_DELETE_IF(elt, alist,
1661 internal_equal(key, XCAR(elt), 0)));
1665 Lisp_Object remassoc_no_quit(Lisp_Object key, Lisp_Object alist)
1667 int speccount = specpdl_depth();
1668 specbind(Qinhibit_quit, Qt);
1669 return unbind_to(speccount, Fremassoc(key, alist));
1672 DEFUN("remassq", Fremassq, 2, 2, 0, /*
1673 Delete by side effect any elements of ALIST whose car is `eq' to KEY.
1674 The modified ALIST is returned. If the first member of ALIST has a car
1675 that is `eq' to KEY, there is no way to remove it by side effect;
1676 therefore, write `(setq foo (remassq key foo))' to be sure of changing
1681 EXTERNAL_LIST_LOOP_DELETE_IF(elt, alist,
1683 EQ_WITH_EBOLA_NOTICE(key, XCAR(elt))));
1687 /* no quit, no errors; be careful */
1689 Lisp_Object remassq_no_quit(Lisp_Object key, Lisp_Object alist)
1691 LIST_LOOP_DELETE_IF(elt, alist,
1693 EQ_WITH_EBOLA_NOTICE(key, XCAR(elt))));
1697 DEFUN("remrassoc", Fremrassoc, 2, 2, 0, /*
1698 Delete by side effect any elements of ALIST whose cdr is `equal' to VALUE.
1699 The modified ALIST is returned. If the first member of ALIST has a car
1700 that is `equal' to VALUE, there is no way to remove it by side effect;
1701 therefore, write `(setq foo (remrassoc value foo))' to be sure of changing
1706 EXTERNAL_LIST_LOOP_DELETE_IF(elt, alist,
1708 internal_equal(value, XCDR(elt), 0)));
1712 DEFUN("remrassq", Fremrassq, 2, 2, 0, /*
1713 Delete by side effect any elements of ALIST whose cdr is `eq' to VALUE.
1714 The modified ALIST is returned. If the first member of ALIST has a car
1715 that is `eq' to VALUE, there is no way to remove it by side effect;
1716 therefore, write `(setq foo (remrassq value foo))' to be sure of changing
1721 EXTERNAL_LIST_LOOP_DELETE_IF(elt, alist,
1723 EQ_WITH_EBOLA_NOTICE(value, XCDR(elt))));
1727 /* Like Fremrassq, fast and unsafe; be careful */
1728 Lisp_Object remrassq_no_quit(Lisp_Object value, Lisp_Object alist)
1730 LIST_LOOP_DELETE_IF(elt, alist,
1732 EQ_WITH_EBOLA_NOTICE(value, XCDR(elt))));
1736 DEFUN("nreverse", Fnreverse, 1, 1, 0, /*
1737 Reverse LIST by destructively modifying cdr pointers.
1738 Return the beginning of the reversed list.
1739 Also see: `reverse'.
1743 struct gcpro gcpro1, gcpro2;
1744 REGISTER Lisp_Object prev = Qnil;
1745 REGISTER Lisp_Object tail = list;
1747 /* We gcpro our args; see `nconc' */
1749 while (!NILP(tail)) {
1750 REGISTER Lisp_Object next;
1751 CONCHECK_CONS(tail);
1761 DEFUN("reverse", Freverse, 1, 1, 0, /*
1762 Reverse LIST, copying. Return the beginning of the reversed list.
1763 See also the function `nreverse', which is used more often.
1767 Lisp_Object reversed_list = Qnil;
1768 EXTERNAL_LIST_LOOP_2(elt, list) {
1769 reversed_list = Fcons(elt, reversed_list);
1771 return reversed_list;
1774 static Lisp_Object list_merge(Lisp_Object org_l1, Lisp_Object org_l2,
1775 Lisp_Object lisp_arg,
1776 int (*pred_fn) (Lisp_Object, Lisp_Object,
1777 Lisp_Object lisp_arg));
1780 list_sort(Lisp_Object list,
1781 Lisp_Object lisp_arg,
1782 int (*pred_fn) (Lisp_Object, Lisp_Object, Lisp_Object lisp_arg))
1784 struct gcpro gcpro1, gcpro2, gcpro3;
1785 Lisp_Object back, tem;
1786 Lisp_Object front = list;
1787 Lisp_Object len = Flength(list);
1792 len = make_int(XINT(len) / 2 - 1);
1793 tem = Fnthcdr(len, list);
1797 GCPRO3(front, back, lisp_arg);
1798 front = list_sort(front, lisp_arg, pred_fn);
1799 back = list_sort(back, lisp_arg, pred_fn);
1801 return list_merge(front, back, lisp_arg, pred_fn);
1805 merge_pred_function(Lisp_Object obj1, Lisp_Object obj2, Lisp_Object pred)
1809 /* prevents the GC from happening in call2 */
1810 int speccount = specpdl_depth();
1811 /* Emacs' GC doesn't actually relocate pointers, so this probably
1812 isn't strictly necessary */
1813 record_unwind_protect(restore_gc_inhibit,
1814 make_int(gc_currently_forbidden));
1815 gc_currently_forbidden = 1;
1816 tmp = call2(pred, obj1, obj2);
1817 unbind_to(speccount, Qnil);
1825 DEFUN("sort", Fsort, 2, 2, 0, /*
1826 Sort LIST, stably, comparing elements using PREDICATE.
1827 Returns the sorted list. LIST is modified by side effects.
1828 PREDICATE is called with two elements of LIST, and should return T
1829 if the first element is "less" than the second.
1833 return list_sort(list, predicate, merge_pred_function);
1836 Lisp_Object merge(Lisp_Object org_l1, Lisp_Object org_l2, Lisp_Object pred)
1838 return list_merge(org_l1, org_l2, pred, merge_pred_function);
1842 list_merge(Lisp_Object org_l1, Lisp_Object org_l2,
1843 Lisp_Object lisp_arg,
1844 int (*pred_fn) (Lisp_Object, Lisp_Object, Lisp_Object lisp_arg))
1850 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1857 /* It is sufficient to protect org_l1 and org_l2.
1858 When l1 and l2 are updated, we copy the new values
1859 back into the org_ vars. */
1861 GCPRO4(org_l1, org_l2, lisp_arg, value);
1879 if (((*pred_fn) (Fcar(l2), Fcar(l1), lisp_arg)) < 0) {
1896 /************************************************************************/
1897 /* property-list functions */
1898 /************************************************************************/
1900 /* For properties of text, we need to do order-insensitive comparison of
1901 plists. That is, we need to compare two plists such that they are the
1902 same if they have the same set of keys, and equivalent values.
1903 So (a 1 b 2) would be equal to (b 2 a 1).
1905 NIL_MEANS_NOT_PRESENT is as in `plists-eq' etc.
1906 LAXP means use `equal' for comparisons.
1909 plists_differ(Lisp_Object a, Lisp_Object b, int nil_means_not_present,
1910 int laxp, int depth)
1912 int eqp = (depth == -1); /* -1 as depth means use eq, not equal. */
1913 int la, lb, m, i, fill;
1914 Lisp_Object *keys, *vals;
1917 int speccount = specpdl_depth();
1919 if (NILP(a) && NILP(b))
1922 Fcheck_valid_plist(a);
1923 Fcheck_valid_plist(b);
1925 la = XINT(Flength(a));
1926 lb = XINT(Flength(b));
1927 m = (la > lb ? la : lb);
1929 XMALLOC_OR_ALLOCA(keys, m, Lisp_Object);
1930 XMALLOC_OR_ALLOCA(vals, m, Lisp_Object);
1931 XMALLOC_ATOMIC_OR_ALLOCA(flags, m, char);
1933 /* First extract the pairs from A. */
1934 for (rest = a; !NILP(rest); rest = XCDR(XCDR(rest))) {
1935 Lisp_Object k = XCAR(rest);
1936 Lisp_Object v = XCAR(XCDR(rest));
1937 /* Maybe be Ebolified. */
1938 if (nil_means_not_present && NILP(v))
1945 /* Now iterate over B, and stop if we find something that's not in A,
1946 or that doesn't match. As we match, mark them. */
1947 for (rest = b; !NILP(rest); rest = XCDR(XCDR(rest))) {
1948 Lisp_Object k = XCAR(rest);
1949 Lisp_Object v = XCAR(XCDR(rest));
1950 /* Maybe be Ebolified. */
1951 if (nil_means_not_present && NILP(v))
1953 for (i = 0; i < fill; i++) {
1954 if (!laxp ? EQ(k, keys[i]) :
1955 internal_equal(k, keys[i], depth)) {
1957 /* We narrowly escaped being Ebolified
1959 ? !EQ_WITH_EBOLA_NOTICE(v, vals[i])
1960 : !internal_equal(v, vals[i], depth))
1961 /* a property in B has a different value
1969 /* there are some properties in B that are not in A */
1972 /* Now check to see that all the properties in A were also in B */
1973 for (i = 0; i < fill; i++)
1977 XMALLOC_UNBIND(flags, m, speccount);
1978 XMALLOC_UNBIND(vals, m, speccount);
1979 XMALLOC_UNBIND(keys, m, speccount);
1984 XMALLOC_UNBIND(flags, m, speccount);
1985 XMALLOC_UNBIND(vals, m, speccount);
1986 XMALLOC_UNBIND(keys, m, speccount);
1990 DEFUN("plists-eq", Fplists_eq, 2, 3, 0, /*
1991 Return non-nil if property lists A and B are `eq'.
1992 A property list is an alternating list of keywords and values.
1993 This function does order-insensitive comparisons of the property lists:
1994 For example, the property lists '(a 1 b 2) and '(b 2 a 1) are equal.
1995 Comparison between values is done using `eq'. See also `plists-equal'.
1996 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
1997 a nil value is ignored. This feature is a virus that has infected
1998 old Lisp implementations, but should not be used except for backward
2001 (a, b, nil_means_not_present))
2003 return (plists_differ(a, b, !NILP(nil_means_not_present), 0, -1)
2007 DEFUN("plists-equal", Fplists_equal, 2, 3, 0, /*
2008 Return non-nil if property lists A and B are `equal'.
2009 A property list is an alternating list of keywords and values. This
2010 function does order-insensitive comparisons of the property lists: For
2011 example, the property lists '(a 1 b 2) and '(b 2 a 1) are equal.
2012 Comparison between values is done using `equal'. See also `plists-eq'.
2013 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
2014 a nil value is ignored. This feature is a virus that has infected
2015 old Lisp implementations, but should not be used except for backward
2018 (a, b, nil_means_not_present))
2020 return (plists_differ(a, b, !NILP(nil_means_not_present), 0, 1)
2024 DEFUN("lax-plists-eq", Flax_plists_eq, 2, 3, 0, /*
2025 Return non-nil if lax property lists A and B are `eq'.
2026 A property list is an alternating list of keywords and values.
2027 This function does order-insensitive comparisons of the property lists:
2028 For example, the property lists '(a 1 b 2) and '(b 2 a 1) are equal.
2029 Comparison between values is done using `eq'. See also `plists-equal'.
2030 A lax property list is like a regular one except that comparisons between
2031 keywords is done using `equal' instead of `eq'.
2032 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
2033 a nil value is ignored. This feature is a virus that has infected
2034 old Lisp implementations, but should not be used except for backward
2037 (a, b, nil_means_not_present))
2039 return (plists_differ(a, b, !NILP(nil_means_not_present), 1, -1)
2043 DEFUN("lax-plists-equal", Flax_plists_equal, 2, 3, 0, /*
2044 Return non-nil if lax property lists A and B are `equal'.
2045 A property list is an alternating list of keywords and values. This
2046 function does order-insensitive comparisons of the property lists: For
2047 example, the property lists '(a 1 b 2) and '(b 2 a 1) are equal.
2048 Comparison between values is done using `equal'. See also `plists-eq'.
2049 A lax property list is like a regular one except that comparisons between
2050 keywords is done using `equal' instead of `eq'.
2051 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
2052 a nil value is ignored. This feature is a virus that has infected
2053 old Lisp implementations, but should not be used except for backward
2056 (a, b, nil_means_not_present))
2058 return (plists_differ(a, b, !NILP(nil_means_not_present), 1, 1)
2062 /* Return the value associated with key PROPERTY in property list PLIST.
2063 Return nil if key not found. This function is used for internal
2064 property lists that cannot be directly manipulated by the user.
2067 Lisp_Object internal_plist_get(Lisp_Object plist, Lisp_Object property)
2071 for (tail = plist; !NILP(tail); tail = XCDR(XCDR(tail))) {
2072 if (EQ(XCAR(tail), property))
2073 return XCAR(XCDR(tail));
2079 /* Set PLIST's value for PROPERTY to VALUE. Analogous to
2080 internal_plist_get(). */
2083 internal_plist_put(Lisp_Object * plist, Lisp_Object property, Lisp_Object value)
2087 for (tail = *plist; !NILP(tail); tail = XCDR(XCDR(tail))) {
2088 if (EQ(XCAR(tail), property)) {
2089 XCAR(XCDR(tail)) = value;
2094 *plist = Fcons(property, Fcons(value, *plist));
2097 int internal_remprop(Lisp_Object * plist, Lisp_Object property)
2099 Lisp_Object tail, prev;
2101 for (tail = *plist, prev = Qnil; !NILP(tail); tail = XCDR(XCDR(tail))) {
2102 if (EQ(XCAR(tail), property)) {
2104 *plist = XCDR(XCDR(tail));
2106 XCDR(XCDR(prev)) = XCDR(XCDR(tail));
2115 /* Called on a malformed property list. BADPLACE should be some
2116 place where truncating will form a good list -- i.e. we shouldn't
2117 result in a list with an odd length. */
2120 bad_bad_bunny(Lisp_Object * plist, Lisp_Object * badplace, Error_behavior errb)
2122 if (ERRB_EQ(errb, ERROR_ME))
2123 return Fsignal(Qmalformed_property_list,
2124 list2(*plist, *badplace));
2126 if (ERRB_EQ(errb, ERROR_ME_WARN)) {
2127 warn_when_safe_lispobj
2130 ("Malformed property list -- list has been truncated"),
2138 /* Called on a circular property list. BADPLACE should be some place
2139 where truncating will result in an even-length list, as above.
2140 If doesn't particularly matter where we truncate -- anywhere we
2141 truncate along the entire list will break the circularity, because
2142 it will create a terminus and the list currently doesn't have one.
2146 bad_bad_turtle(Lisp_Object * plist, Lisp_Object * badplace, Error_behavior errb)
2148 if (ERRB_EQ(errb, ERROR_ME))
2149 return Fsignal(Qcircular_property_list, list1(*plist));
2151 if (ERRB_EQ(errb, ERROR_ME_WARN)) {
2152 warn_when_safe_lispobj
2155 ("Circular property list -- list has been truncated"),
2163 /* Advance the tortoise pointer by two (one iteration of a property-list
2164 loop) and the hare pointer by four and verify that no malformations
2165 or circularities exist. If so, return zero and store a value into
2166 RETVAL that should be returned by the calling function. Otherwise,
2167 return 1. See external_plist_get().
2171 advance_plist_pointers(Lisp_Object * plist,
2172 Lisp_Object ** tortoise, Lisp_Object ** hare,
2173 Error_behavior errb, Lisp_Object * retval)
2176 Lisp_Object *tortsave = *tortoise;
2178 /* Note that our "fixing" may be more brutal than necessary,
2179 but it's the user's own problem, not ours, if they went in and
2180 manually fucked up a plist. */
2182 for (i = 0; i < 2; i++) {
2183 /* This is a standard iteration of a defensive-loop-checking
2184 loop. We just do it twice because we want to advance past
2185 both the property and its value.
2187 If the pointer indirection is confusing you, remember that
2188 one level of indirection on the hare and tortoise pointers
2189 is only due to pass-by-reference for this function. The other
2190 level is so that the plist can be fixed in place. */
2192 /* When we reach the end of a well-formed plist, **HARE is
2193 nil. In that case, we don't do anything at all except
2194 advance TORTOISE by one. Otherwise, we advance HARE
2195 by two (making sure it's OK to do so), then advance
2196 TORTOISE by one (it will always be OK to do so because
2197 the HARE is always ahead of the TORTOISE and will have
2198 already verified the path), then make sure TORTOISE and
2199 HARE don't contain the same non-nil object -- if the
2200 TORTOISE and the HARE ever meet, then obviously we're
2201 in a circularity, and if we're in a circularity, then
2202 the TORTOISE and the HARE can't cross paths without
2203 meeting, since the HARE only gains one step over the
2204 TORTOISE per iteration. */
2206 if (!NILP(**hare)) {
2207 Lisp_Object *haresave = *hare;
2208 if (!CONSP(**hare)) {
2209 *retval = bad_bad_bunny(plist, haresave, errb);
2212 *hare = &XCDR(**hare);
2213 /* In a non-plist, we'd check here for a nil value for
2214 **HARE, which is OK (it just means the list has an
2215 odd number of elements). In a plist, it's not OK
2216 for the list to have an odd number of elements. */
2217 if (!CONSP(**hare)) {
2218 *retval = bad_bad_bunny(plist, haresave, errb);
2221 *hare = &XCDR(**hare);
2224 *tortoise = &XCDR(**tortoise);
2225 if (!NILP(**hare) && EQ(**tortoise, **hare)) {
2226 *retval = bad_bad_turtle(plist, tortsave, errb);
2234 /* Return the value of PROPERTY from PLIST, or Qunbound if
2235 property is not on the list.
2237 PLIST is a Lisp-accessible property list, meaning that it
2238 has to be checked for malformations and circularities.
2240 If ERRB is ERROR_ME, an error will be signalled. Otherwise, the
2241 function will never signal an error; and if ERRB is ERROR_ME_WARN,
2242 on finding a malformation or a circularity, it issues a warning and
2243 attempts to silently fix the problem.
2245 A pointer to PLIST is passed in so that PLIST can be successfully
2246 "fixed" even if the error is at the beginning of the plist. */
2249 external_plist_get(Lisp_Object * plist, Lisp_Object property,
2250 int laxp, Error_behavior errb)
2252 Lisp_Object *tortoise = plist;
2253 Lisp_Object *hare = plist;
2255 while (!NILP(*tortoise)) {
2256 Lisp_Object *tortsave = tortoise;
2259 /* We do the standard tortoise/hare march. We isolate the
2260 grungy stuff to do this in advance_plist_pointers(), though.
2261 To us, all this function does is advance the tortoise
2262 pointer by two and the hare pointer by four and make sure
2263 everything's OK. We first advance the pointers and then
2264 check if a property matched; this ensures that our
2265 check for a matching property is safe. */
2267 if (!advance_plist_pointers
2268 (plist, &tortoise, &hare, errb, &retval))
2271 if (!laxp ? EQ(XCAR(*tortsave), property)
2272 : internal_equal(XCAR(*tortsave), property, 0))
2273 return XCAR(XCDR(*tortsave));
2279 /* Set PLIST's value for PROPERTY to VALUE, given a possibly
2280 malformed or circular plist. Analogous to external_plist_get(). */
2283 external_plist_put(Lisp_Object * plist, Lisp_Object property,
2284 Lisp_Object value, int laxp, Error_behavior errb)
2286 Lisp_Object *tortoise = plist;
2287 Lisp_Object *hare = plist;
2289 while (!NILP(*tortoise)) {
2290 Lisp_Object *tortsave = tortoise;
2294 if (!advance_plist_pointers
2295 (plist, &tortoise, &hare, errb, &retval))
2298 if (!laxp ? EQ(XCAR(*tortsave), property)
2299 : internal_equal(XCAR(*tortsave), property, 0)) {
2300 XCAR(XCDR(*tortsave)) = value;
2305 *plist = Fcons(property, Fcons(value, *plist));
2309 external_remprop(Lisp_Object * plist, Lisp_Object property,
2310 int laxp, Error_behavior errb)
2312 Lisp_Object *tortoise = plist;
2313 Lisp_Object *hare = plist;
2315 while (!NILP(*tortoise)) {
2316 Lisp_Object *tortsave = tortoise;
2320 if (!advance_plist_pointers
2321 (plist, &tortoise, &hare, errb, &retval))
2324 if (!laxp ? EQ(XCAR(*tortsave), property)
2325 : internal_equal(XCAR(*tortsave), property, 0)) {
2326 /* Now you see why it's so convenient to have that level
2328 *tortsave = XCDR(XCDR(*tortsave));
2336 DEFUN("plist-get", Fplist_get, 2, 3, 0, /*
2337 Extract a value from a property list.
2338 PLIST is a property list, which is a list of the form
2339 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2...).
2340 PROPERTY is usually a symbol.
2341 This function returns the value corresponding to the PROPERTY,
2342 or DEFAULT if PROPERTY is not one of the properties on the list.
2344 (plist, property, default_))
2346 Lisp_Object value = external_plist_get(&plist, property, 0, ERROR_ME);
2347 return UNBOUNDP(value) ? default_ : value;
2350 DEFUN("plist-put", Fplist_put, 3, 3, 0, /*
2351 Change value in PLIST of PROPERTY to VALUE.
2352 PLIST is a property list, which is a list of the form
2353 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2 ...).
2354 PROPERTY is usually a symbol and VALUE is any object.
2355 If PROPERTY is already a property on the list, its value is set to VALUE,
2356 otherwise the new PROPERTY VALUE pair is added.
2357 The new plist is returned; use `(setq x (plist-put x property value))'
2358 to be sure to use the new value. PLIST is modified by side effect.
2360 (plist, property, value))
2362 external_plist_put(&plist, property, value, 0, ERROR_ME);
2366 DEFUN("plist-remprop", Fplist_remprop, 2, 2, 0, /*
2367 Remove from PLIST the property PROPERTY and its value.
2368 PLIST is a property list, which is a list of the form
2369 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2 ...).
2370 PROPERTY is usually a symbol.
2371 The new plist is returned; use `(setq x (plist-remprop x property))'
2372 to be sure to use the new value. PLIST is modified by side effect.
2376 external_remprop(&plist, property, 0, ERROR_ME);
2380 DEFUN("plist-member", Fplist_member, 2, 2, 0, /*
2381 Return t if PROPERTY has a value specified in PLIST.
2385 Lisp_Object value = Fplist_get(plist, property, Qunbound);
2386 return UNBOUNDP(value) ? Qnil : Qt;
2389 DEFUN("check-valid-plist", Fcheck_valid_plist, 1, 1, 0, /*
2390 Given a plist, signal an error if there is anything wrong with it.
2391 This means that it's a malformed or circular plist.
2395 Lisp_Object *tortoise;
2401 while (!NILP(*tortoise)) {
2405 if (!advance_plist_pointers(&plist, &tortoise, &hare, ERROR_ME,
2413 DEFUN("valid-plist-p", Fvalid_plist_p, 1, 1, 0, /*
2414 Given a plist, return non-nil if its format is correct.
2415 If it returns nil, `check-valid-plist' will signal an error when given
2416 the plist; that means it's a malformed or circular plist.
2420 Lisp_Object *tortoise;
2425 while (!NILP(*tortoise)) {
2429 if (!advance_plist_pointers
2430 (&plist, &tortoise, &hare, ERROR_ME_NOT, &retval))
2437 DEFUN("canonicalize-plist", Fcanonicalize_plist, 1, 2, 0, /*
2438 Destructively remove any duplicate entries from a plist.
2439 In such cases, the first entry applies.
2441 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
2442 a nil value is removed. This feature is a virus that has infected
2443 old Lisp implementations, but should not be used except for backward
2446 The new plist is returned. If NIL-MEANS-NOT-PRESENT is given, the
2447 return value may not be EQ to the passed-in value, so make sure to
2448 `setq' the value back into where it came from.
2450 (plist, nil_means_not_present))
2452 Lisp_Object head = plist;
2454 Fcheck_valid_plist(plist);
2456 while (!NILP(plist)) {
2457 Lisp_Object prop = Fcar(plist);
2458 Lisp_Object next = Fcdr(plist);
2460 CHECK_CONS(next); /* just make doubly sure we catch any errors */
2461 if (!NILP(nil_means_not_present) && NILP(Fcar(next))) {
2462 if (EQ(head, plist))
2467 /* external_remprop returns 1 if it removed any property.
2468 We have to loop till it didn't remove anything, in case
2469 the property occurs many times. */
2470 while (external_remprop(&XCDR(next), prop, 0, ERROR_ME))
2478 DEFUN("lax-plist-get", Flax_plist_get, 2, 3, 0, /*
2479 Extract a value from a lax property list.
2480 LAX-PLIST is a lax property list, which is a list of the form
2481 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2...), where comparisons between
2482 properties is done using `equal' instead of `eq'.
2483 PROPERTY is usually a symbol.
2484 This function returns the value corresponding to PROPERTY,
2485 or DEFAULT if PROPERTY is not one of the properties on the list.
2487 (lax_plist, property, default_))
2490 external_plist_get(&lax_plist, property, 1, ERROR_ME);
2491 return UNBOUNDP(value) ? default_ : value;
2494 DEFUN("lax-plist-put", Flax_plist_put, 3, 3, 0, /*
2495 Change value in LAX-PLIST of PROPERTY to VALUE.
2496 LAX-PLIST is a lax property list, which is a list of the form
2497 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2...), where comparisons between
2498 properties is done using `equal' instead of `eq'.
2499 PROPERTY is usually a symbol and VALUE is any object.
2500 If PROPERTY is already a property on the list, its value is set to
2501 VALUE, otherwise the new PROPERTY VALUE pair is added.
2502 The new plist is returned; use `(setq x (lax-plist-put x property value))'
2503 to be sure to use the new value. LAX-PLIST is modified by side effect.
2505 (lax_plist, property, value))
2507 external_plist_put(&lax_plist, property, value, 1, ERROR_ME);
2511 DEFUN("lax-plist-remprop", Flax_plist_remprop, 2, 2, 0, /*
2512 Remove from LAX-PLIST the property PROPERTY and its value.
2513 LAX-PLIST is a lax property list, which is a list of the form
2514 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2...), where comparisons between
2515 properties is done using `equal' instead of `eq'.
2516 PROPERTY is usually a symbol.
2517 The new plist is returned; use `(setq x (lax-plist-remprop x property))'
2518 to be sure to use the new value. LAX-PLIST is modified by side effect.
2520 (lax_plist, property))
2522 external_remprop(&lax_plist, property, 1, ERROR_ME);
2526 DEFUN("lax-plist-member", Flax_plist_member, 2, 2, 0, /*
2527 Return t if PROPERTY has a value specified in LAX-PLIST.
2528 LAX-PLIST is a lax property list, which is a list of the form
2529 \(PROPERTY1 VALUE1 PROPERTY2 VALUE2...), where comparisons between
2530 properties is done using `equal' instead of `eq'.
2532 (lax_plist, property))
2534 return UNBOUNDP(Flax_plist_get(lax_plist, property, Qunbound)) ? Qnil :
2538 DEFUN("canonicalize-lax-plist", Fcanonicalize_lax_plist, 1, 2, 0, /*
2539 Destructively remove any duplicate entries from a lax plist.
2540 In such cases, the first entry applies.
2542 If optional arg NIL-MEANS-NOT-PRESENT is non-nil, then a property with
2543 a nil value is removed. This feature is a virus that has infected
2544 old Lisp implementations, but should not be used except for backward
2547 The new plist is returned. If NIL-MEANS-NOT-PRESENT is given, the
2548 return value may not be EQ to the passed-in value, so make sure to
2549 `setq' the value back into where it came from.
2551 (lax_plist, nil_means_not_present))
2553 Lisp_Object head = lax_plist;
2555 Fcheck_valid_plist(lax_plist);
2557 while (!NILP(lax_plist)) {
2558 Lisp_Object prop = Fcar(lax_plist);
2559 Lisp_Object next = Fcdr(lax_plist);
2561 CHECK_CONS(next); /* just make doubly sure we catch any errors */
2562 if (!NILP(nil_means_not_present) && NILP(Fcar(next))) {
2563 if (EQ(head, lax_plist))
2565 lax_plist = Fcdr(next);
2568 /* external_remprop returns 1 if it removed any property.
2569 We have to loop till it didn't remove anything, in case
2570 the property occurs many times. */
2571 while (external_remprop(&XCDR(next), prop, 1, ERROR_ME))
2573 lax_plist = Fcdr(next);
2579 /* In C because the frame props stuff uses it */
2581 DEFUN("destructive-alist-to-plist", Fdestructive_alist_to_plist, 1, 1, 0, /*
2582 Convert association list ALIST into the equivalent property-list form.
2583 The plist is returned. This converts from
2585 \((a . 1) (b . 2) (c . 3))
2591 The original alist is destroyed in the process of constructing the plist.
2592 See also `alist-to-plist'.
2596 Lisp_Object head = alist;
2597 while (!NILP(alist)) {
2598 /* remember the alist element. */
2599 Lisp_Object el = Fcar(alist);
2601 Fsetcar(alist, Fcar(el));
2602 Fsetcar(el, Fcdr(el));
2603 Fsetcdr(el, Fcdr(alist));
2605 alist = Fcdr(Fcdr(alist));
2611 DEFUN("get", Fget, 2, 3, 0, /*
2612 Return the value of OBJECT's PROPERTY property.
2613 This is the last VALUE stored with `(put OBJECT PROPERTY VALUE)'.
2614 If there is no such property, return optional third arg DEFAULT
2615 \(which defaults to `nil'). OBJECT can be a symbol, string, extent,
2616 face, or glyph. See also `put', `remprop', and `object-plist'.
2618 (object, property, default_))
2620 /* Various places in emacs call Fget() and expect it not to quit,
2624 if (LRECORDP(object) && XRECORD_LHEADER_IMPLEMENTATION(object)->getprop)
2626 XRECORD_LHEADER_IMPLEMENTATION(object)->getprop(object,
2629 signal_simple_error("Object type has no properties", object);
2631 return UNBOUNDP(val) ? default_ : val;
2634 DEFUN("put", Fput, 3, 3, 0, /*
2635 Set OBJECT's PROPERTY to VALUE.
2636 It can be subsequently retrieved with `(get OBJECT PROPERTY)'.
2637 OBJECT can be a symbol, face, extent, or string.
2638 For a string, no properties currently have predefined meanings.
2639 For the predefined properties for extents, see `set-extent-property'.
2640 For the predefined properties for faces, see `set-face-property'.
2641 See also `get', `remprop', and `object-plist'.
2643 (object, property, value))
2645 CHECK_LISP_WRITEABLE(object);
2647 if (LRECORDP(object) && XRECORD_LHEADER_IMPLEMENTATION(object)->putprop) {
2648 if (!XRECORD_LHEADER_IMPLEMENTATION(object)->putprop
2649 (object, property, value))
2650 signal_simple_error("Can't set property on object",
2653 signal_simple_error("Object type has no settable properties",
2659 DEFUN("remprop", Fremprop, 2, 2, 0, /*
2660 Remove, from OBJECT's property list, PROPERTY and its corresponding value.
2661 OBJECT can be a symbol, string, extent, face, or glyph. Return non-nil
2662 if the property list was actually modified (i.e. if PROPERTY was present
2663 in the property list). See also `get', `put', and `object-plist'.
2669 CHECK_LISP_WRITEABLE(object);
2671 if (LRECORDP(object) && XRECORD_LHEADER_IMPLEMENTATION(object)->remprop) {
2673 XRECORD_LHEADER_IMPLEMENTATION(object)->remprop(object,
2676 signal_simple_error("Can't remove property from object",
2679 signal_simple_error("Object type has no removable properties",
2682 return ret ? Qt : Qnil;
2685 DEFUN("object-plist", Fobject_plist, 1, 1, 0, /*
2686 Return a property list of OBJECT's properties.
2687 For a symbol, this is equivalent to `symbol-plist'.
2688 OBJECT can be a symbol, string, extent, face, or glyph.
2689 Do not modify the returned property list directly;
2690 this may or may not have the desired effects. Use `put' instead.
2694 if (LRECORDP(object) && XRECORD_LHEADER_IMPLEMENTATION(object)->plist)
2695 return XRECORD_LHEADER_IMPLEMENTATION(object)->plist(object);
2697 signal_simple_error("Object type has no properties", object);
2702 int internal_equal(Lisp_Object obj1, Lisp_Object obj2, int depth)
2705 error("Stack overflow in equal");
2707 if (EQ_WITH_EBOLA_NOTICE(obj1, obj2))
2709 /* Note that (equal 20 20.0) should be nil */
2710 if (XTYPE(obj1) != XTYPE(obj2))
2712 if (LRECORDP(obj1)) {
2713 const struct lrecord_implementation
2714 *imp1 = XRECORD_LHEADER_IMPLEMENTATION(obj1),
2715 *imp2 = XRECORD_LHEADER_IMPLEMENTATION(obj2);
2717 return (imp1 == imp2) &&
2718 /* EQ-ness of the objects was noticed above */
2719 (imp1->equal && (imp1->equal) (obj1, obj2, depth));
2726 internal_equalp (Lisp_Object obj1, Lisp_Object obj2, int depth)
2729 error ("Stack overflow in equalp");
2731 if (EQ_WITH_EBOLA_NOTICE (obj1, obj2))
2734 if (NUMBERP(obj1) && NUMBERP(obj2)) {
2735 return ent_binrel(ASE_BINARY_REL_EQUALP, obj1, obj2);
2738 if (CHARP(obj1) && CHARP(obj2))
2739 return XCHAR(obj1) == XCHAR(obj2);
2740 if (XTYPE(obj1) != XTYPE(obj2))
2742 if (LRECORDP(obj1)) {
2743 const struct lrecord_implementation
2744 *imp1 = XRECORD_LHEADER_IMPLEMENTATION (obj1),
2745 *imp2 = XRECORD_LHEADER_IMPLEMENTATION (obj2);
2747 /* #### not yet implemented properly, needs another flag to specify
2749 return (imp1 == imp2) &&
2750 /* EQ-ness of the objects was noticed above */
2751 (imp1->equal && (imp1->equal) (obj1, obj2, depth));
2758 /* Note that we may be calling sub-objects that will use
2759 internal_equal() (instead of internal_old_equal()). Oh well.
2760 We will get an Ebola note if there's any possibility of confusion,
2761 but that seems unlikely. */
2763 static int internal_old_equal(Lisp_Object obj1, Lisp_Object obj2, int depth)
2766 error("Stack overflow in equal");
2768 if (HACKEQ_UNSAFE(obj1, obj2))
2770 /* Note that (equal 20 20.0) should be nil */
2771 if (XTYPE(obj1) != XTYPE(obj2))
2774 return internal_equal(obj1, obj2, depth);
2777 DEFUN("equal", Fequal, 2, 2, 0, /*
2778 Return t if two Lisp objects have similar structure and contents.
2779 They must have the same data type.
2780 Conses are compared by comparing the cars and the cdrs.
2781 Vectors and strings are compared element by element.
2782 Numbers are compared by value. Symbols must match exactly.
2786 return internal_equal(object1, object2, 0) ? Qt : Qnil;
2789 DEFUN("old-equal", Fold_equal, 2, 2, 0, /*
2790 Return t if two Lisp objects have similar structure and contents.
2791 They must have the same data type.
2792 \(Note, however, that an exception is made for characters and integers;
2793 this is known as the "char-int confoundance disease." See `eq' and
2795 This function is provided only for byte-code compatibility with v19.
2800 return internal_old_equal(object1, object2, 0) ? Qt : Qnil;
2803 DEFUN("fillarray", Ffillarray, 2, 2, 0, /*
2804 Destructively modify ARRAY by replacing each element with ITEM.
2805 ARRAY is a vector, bit vector, or string.
2810 if (STRINGP(array)) {
2811 Lisp_String *s = XSTRING(array);
2812 Bytecount old_bytecount = string_length(s);
2813 Bytecount new_bytecount;
2814 Bytecount item_bytecount;
2815 Bufbyte item_buf[MAX_EMCHAR_LEN];
2819 CHECK_CHAR_COERCE_INT(item);
2820 CHECK_LISP_WRITEABLE(array);
2822 item_bytecount = set_charptr_emchar(item_buf, XCHAR(item));
2823 new_bytecount = item_bytecount * string_char_length(s);
2825 resize_string(s, -1, new_bytecount - old_bytecount);
2827 for (p = string_data(s), end = p + new_bytecount;
2828 p < end; p += item_bytecount)
2829 memcpy(p, item_buf, item_bytecount);
2832 bump_string_modiff(array);
2833 } else if (VECTORP(array)) {
2834 Lisp_Object *p = XVECTOR_DATA(array);
2835 size_t len = XVECTOR_LENGTH(array);
2836 CHECK_LISP_WRITEABLE(array);
2839 } else if (BIT_VECTORP(array)) {
2840 Lisp_Bit_Vector *v = XBIT_VECTOR(array);
2841 size_t len = bit_vector_length(v);
2845 CHECK_LISP_WRITEABLE(array);
2847 set_bit_vector_bit(v, len, bit);
2849 array = wrong_type_argument(Qarrayp, array);
2855 Lisp_Object nconc2(Lisp_Object arg1, Lisp_Object arg2)
2857 Lisp_Object args[2] = {arg1, arg2};
2858 struct gcpro gcpro1;
2860 GCPROn(args, countof(args));
2861 RETURN_UNGCPRO(bytecode_nconc2(args));
2864 Lisp_Object bytecode_nconc2(Lisp_Object * args)
2868 if (CONSP(args[0])) {
2869 /* (setcdr (last args[0]) args[1]) */
2870 Lisp_Object tortoise, hare;
2873 for (hare = tortoise = args[0], count = 0;
2874 CONSP(XCDR(hare)); hare = XCDR(hare), count++) {
2875 if (count < CIRCULAR_LIST_SUSPICION_LENGTH)
2879 tortoise = XCDR(tortoise);
2880 if (EQ(hare, tortoise))
2881 signal_circular_list_error(args[0]);
2883 XCDR(hare) = args[1];
2885 } else if (NILP(args[0])) {
2888 args[0] = wrong_type_argument(args[0], Qlistp);
2893 DEFUN("nconc", Fnconc, 0, MANY, 0, /*
2894 Concatenate any number of lists by altering them.
2895 Only the last argument is not altered, and need not be a list.
2897 If the first argument is nil, there is no way to modify it by side
2898 effect; therefore, write `(setq foo (nconc foo list))' to be sure of
2899 changing the value of `foo'.
2901 (int nargs, Lisp_Object * args))
2904 struct gcpro gcpro1;
2906 /* The modus operandi in Emacs is "caller gc-protects args".
2907 However, nconc (particularly nconc2 ()) is called many times
2908 in Emacs on freshly created stuff (e.g. you see the idiom
2909 nconc2 (Fcopy_sequence (foo), bar) a lot). So we help those
2910 callers out by protecting the args ourselves to save them
2911 a lot of temporary-variable grief. */
2913 GCPROn(args, nargs);
2915 while (argnum < nargs) {
2920 /* `val' is the first cons, which will be our return
2922 * `last_cons' will be the cons cell to mutate. */
2923 Lisp_Object last_cons = val;
2924 Lisp_Object tortoise = val;
2926 for (argnum++; argnum < nargs; argnum++) {
2927 Lisp_Object next = args[argnum];
2929 if (CONSP(next) || argnum == nargs - 1) {
2930 /* (setcdr (last val) next) */
2934 CONSP(XCDR(last_cons));
2936 XCDR(last_cons), count++) {
2938 CIRCULAR_LIST_SUSPICION_LENGTH)
2944 if (EQ(last_cons, tortoise))
2945 signal_circular_list_error
2948 XCDR(last_cons) = next;
2949 } else if (NILP(next)) {
2953 wrong_type_argument(Qlistp, next);
2957 RETURN_UNGCPRO(val);
2958 } else if (NILP(val))
2960 else if (argnum == nargs - 1) /* last arg? */
2961 RETURN_UNGCPRO(val);
2963 args[argnum] = wrong_type_argument(Qlistp, val);
2967 RETURN_UNGCPRO(Qnil); /* No non-nil args provided. */
2971 DEFUN("replace-list", Freplace_list, 2, 2, 0, /*
2972 Destructively replace the list OLD with NEW.
2973 This is like (copy-sequence NEW) except that it reuses the
2974 conses in OLD as much as possible. If OLD and NEW are the same
2975 length, no consing will take place.
2979 Lisp_Object tail, oldtail = old, prevoldtail = Qnil;
2981 EXTERNAL_LIST_LOOP(tail, new) {
2982 if (!NILP(oldtail)) {
2983 CHECK_CONS(oldtail);
2984 XCAR(oldtail) = XCAR(tail);
2985 } else if (!NILP(prevoldtail)) {
2986 XCDR(prevoldtail) = Fcons(XCAR(tail), Qnil);
2987 prevoldtail = XCDR(prevoldtail);
2989 old = oldtail = Fcons(XCAR(tail), Qnil);
2991 if (!NILP(oldtail)) {
2992 prevoldtail = oldtail;
2993 oldtail = XCDR(oldtail);
2997 if (!NILP(prevoldtail))
2998 XCDR(prevoldtail) = Qnil;
3005 /* #### this function doesn't belong in this file! */
3007 #ifdef HAVE_GETLOADAVG
3008 #ifdef HAVE_SYS_LOADAVG_H
3009 #include <sys/loadavg.h>
3012 int getloadavg(double loadavg[], int nelem); /* Defined in getloadavg.c */
3015 DEFUN("load-average", Fload_average, 0, 1, 0, /*
3016 Return list of 1 minute, 5 minute and 15 minute load averages.
3017 Each of the three load averages is multiplied by 100,
3018 then converted to integer.
3020 When USE-FLOATS is non-nil, floats will be used instead of integers.
3021 These floats are not multiplied by 100.
3023 If the 5-minute or 15-minute load averages are not available, return a
3024 shortened list, containing only those averages which are available.
3026 On some systems, this won't work due to permissions on /dev/kmem,
3027 in which case you can't use this.
3032 int loads = getloadavg(load_ave, countof(load_ave));
3033 Lisp_Object ret = Qnil;
3036 error("load-average not implemented for this operating system");
3038 signal_simple_error("Could not get load-average",
3039 lisp_strerror(errno));
3041 while (loads-- > 0) {
3042 Lisp_Object load = (NILP(use_floats) ?
3043 make_int((int)(100.0 * load_ave[loads]))
3044 : make_float(load_ave[loads]));
3045 ret = Fcons(load, ret);
3050 Lisp_Object Vfeatures;
3052 DEFUN("featurep", Ffeaturep, 1, 1, 0, /*
3053 Return non-nil if feature FEXP is present in this Emacs.
3054 Use this to conditionalize execution of lisp code based on the
3055 presence or absence of emacs or environment extensions.
3056 FEXP can be a symbol, a number, or a list.
3057 If it is a symbol, that symbol is looked up in the `features' variable,
3058 and non-nil will be returned if found.
3059 If it is a number, the function will return non-nil if this Emacs
3060 has an equal or greater version number than FEXP.
3061 If it is a list whose car is the symbol `and', it will return
3062 non-nil if all the features in its cdr are non-nil.
3063 If it is a list whose car is the symbol `or', it will return non-nil
3064 if any of the features in its cdr are non-nil.
3065 If it is a list whose car is the symbol `not', it will return
3066 non-nil if the feature is not present.
3071 => ; Non-nil on SXEmacs.
3073 (featurep '(and sxemacs gnus))
3074 => ; Non-nil on SXEmacs with Gnus loaded.
3076 (featurep '(or tty-frames (and emacs 19.30)))
3077 => ; Non-nil if this Emacs supports TTY frames.
3079 (featurep '(or (and xemacs 19.15) (and emacs 19.34)))
3080 => ; Non-nil on XEmacs 19.15 and later, or FSF Emacs 19.34 and later.
3082 (featurep '(and xemacs 21.02))
3083 => ; Non-nil on XEmacs 21.2 and later.
3085 NOTE: The advanced arguments of this function (anything other than a
3086 symbol) are not yet supported by FSF Emacs. If you feel they are useful
3087 for supporting multiple Emacs variants, lobby Richard Stallman at
3088 <bug-gnu-emacs@gnu.org>.
3092 #ifndef FEATUREP_SYNTAX
3094 return NILP(Fmemq(fexp, Vfeatures)) ? Qnil : Qt;
3095 #else /* FEATUREP_SYNTAX */
3096 static double featurep_emacs_version;
3098 /* Brute force translation from Erik Naggum's lisp function. */
3099 if (SYMBOLP(fexp)) {
3100 /* Original definition */
3101 return NILP(Fmemq(fexp, Vfeatures)) ? Qnil : Qt;
3102 } else if (INTP(fexp) || FLOATP(fexp)) {
3103 double d = extract_float(fexp);
3105 if (featurep_emacs_version == 0.0) {
3106 featurep_emacs_version = XINT(Vemacs_major_version) +
3107 (XINT(Vemacs_minor_version) / 100.0);
3109 return featurep_emacs_version >= d ? Qt : Qnil;
3110 } else if (CONSP(fexp)) {
3111 Lisp_Object tem = XCAR(fexp);
3112 if (EQ(tem, Qnot)) {
3118 return NILP(call1(Qfeaturep, negate)) ? Qt :
3121 return Fsignal(Qinvalid_read_syntax,
3123 } else if (EQ(tem, Qand)) {
3125 /* Use Fcar/Fcdr for error-checking. */
3126 while (!NILP(tem) && !NILP(call1(Qfeaturep, Fcar(tem)))) {
3129 return NILP(tem) ? Qt : Qnil;
3130 } else if (EQ(tem, Qor)) {
3132 /* Use Fcar/Fcdr for error-checking. */
3133 while (!NILP(tem) && NILP(call1(Qfeaturep, Fcar(tem)))) {
3136 return NILP(tem) ? Qnil : Qt;
3138 return Fsignal(Qinvalid_read_syntax, list1(XCDR(fexp)));
3141 return Fsignal(Qinvalid_read_syntax, list1(fexp));
3144 #endif /* FEATUREP_SYNTAX */
3146 DEFUN("provide", Fprovide, 1, 1, 0, /*
3147 Announce that FEATURE is a feature of the current Emacs.
3148 This function updates the value of the variable `features'.
3153 CHECK_SYMBOL(feature);
3154 if (!NILP(Vautoload_queue))
3156 Fcons(Fcons(Vfeatures, Qnil), Vautoload_queue);
3157 tem = Fmemq(feature, Vfeatures);
3159 Vfeatures = Fcons(feature, Vfeatures);
3160 LOADHIST_ATTACH(Fcons(Qprovide, feature));
3164 DEFUN("require", Frequire, 1, 2, 0, /*
3165 If feature FEATURE is not loaded, load it from FILENAME.
3166 If FEATURE is not a member of the list `features', then the feature
3167 is not loaded; so load the file FILENAME.
3168 If FILENAME is omitted, the printname of FEATURE is used as the file name.
3170 (feature, filename))
3174 CHECK_SYMBOL(feature);
3175 tem = Fmemq(feature, Vfeatures);
3176 LOADHIST_ATTACH(Fcons(Qrequire, feature));
3181 int speccount = specpdl_depth();
3183 /* Value saved here is to be restored into Vautoload_queue */
3184 record_unwind_protect(un_autoload, Vautoload_queue);
3185 Vautoload_queue = Qt;
3187 /* defined in code-files.el */
3188 call4(Qload, NILP(filename) ? Fsymbol_name(feature) : filename,
3191 tem = Fmemq(feature, Vfeatures);
3193 error("Required feature %s was not provided",
3194 string_data(XSYMBOL(feature)->name));
3196 /* Once loading finishes, don't undo it. */
3197 Vautoload_queue = Qt;
3198 return unbind_to(speccount, feature);
3202 DEFUN("revoke", Frevoke, 1, 1, 0, /*
3203 Announce that FEATURE is no longer a feature of the current Emacs.
3207 CHECK_SYMBOL(feature);
3208 if (!NILP(Vautoload_queue))
3210 Fcons(Fcons(Vfeatures, Qnil), Vautoload_queue);
3212 if (LIKELY(CONSP(Vfeatures) && EQ(XCAR(Vfeatures), feature))) {
3213 /* special case where feature is the head of 'features */
3214 Vfeatures = XCDR(Vfeatures);
3217 for (Lisp_Object tmp = Vfeatures;
3218 CONSP(tmp) && CONSP(XCDR(tmp));
3220 if (EQ(XCAR(XCDR(tmp)), feature)) {
3221 XCDR(tmp) = XCDR(XCDR(tmp));
3228 /* base64 encode/decode functions.
3230 Originally based on code from GNU recode. Ported to FSF Emacs by
3231 Lars Magne Ingebrigtsen and Karl Heuer. Ported to XEmacs and
3232 subsequently heavily hacked by Hrvoje Niksic. */
3234 #define MIME_LINE_LENGTH 72
3236 #define IS_ASCII(Character) \
3238 #define IS_BASE64(Character) \
3239 (IS_ASCII (Character) && base64_char_to_value[Character] >= 0)
3241 /* Table of characters coding the 64 values. */
3242 static char base64_value_to_char[64] = {
3243 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 0- 9 */
3244 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 10-19 */
3245 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', /* 20-29 */
3246 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', /* 30-39 */
3247 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', /* 40-49 */
3248 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', /* 50-59 */
3249 '8', '9', '+', '/' /* 60-63 */
3252 /* Table of base64 values for first 128 characters. */
3253 static short base64_char_to_value[128] = {
3254 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0- 9 */
3255 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10- 19 */
3256 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20- 29 */
3257 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 30- 39 */
3258 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, /* 40- 49 */
3259 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, /* 50- 59 */
3260 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, /* 60- 69 */
3261 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 70- 79 */
3262 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, /* 80- 89 */
3263 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, /* 90- 99 */
3264 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, /* 100-109 */
3265 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, /* 110-119 */
3266 49, 50, 51, -1, -1, -1, -1, -1 /* 120-127 */
3269 /* The following diagram shows the logical steps by which three octets
3270 get transformed into four base64 characters.
3272 .--------. .--------. .--------.
3273 |aaaaaabb| |bbbbcccc| |ccdddddd|
3274 `--------' `--------' `--------'
3276 .--------+--------+--------+--------.
3277 |00aaaaaa|00bbbbbb|00cccccc|00dddddd|
3278 `--------+--------+--------+--------'
3280 .--------+--------+--------+--------.
3281 |AAAAAAAA|BBBBBBBB|CCCCCCCC|DDDDDDDD|
3282 `--------+--------+--------+--------'
3284 The octets are divided into 6 bit chunks, which are then encoded into
3285 base64 characters. */
3287 #define ADVANCE_INPUT(c, stream) \
3288 ((ec = Lstream_get_emchar (stream)) == -1 ? 0 : \
3290 (signal_simple_error ("Non-ascii character in base64 input", \
3291 make_char (ec)), 0) \
3292 : (c = (Bufbyte)ec), 1))
3294 static Bytind base64_encode_1(Lstream * istream, Bufbyte * to, int line_break)
3296 EMACS_INT counter = 0;
3303 if (!ADVANCE_INPUT(c, istream))
3306 /* Wrap line every 76 characters. */
3308 if (counter < MIME_LINE_LENGTH / 4)
3316 /* Process first byte of a triplet. */
3317 *e++ = base64_value_to_char[0x3f & c >> 2];
3318 value = (0x03 & c) << 4;
3320 /* Process second byte of a triplet. */
3321 if (!ADVANCE_INPUT(c, istream)) {
3322 *e++ = base64_value_to_char[value];
3328 *e++ = base64_value_to_char[value | (0x0f & c >> 4)];
3329 value = (0x0f & c) << 2;
3331 /* Process third byte of a triplet. */
3332 if (!ADVANCE_INPUT(c, istream)) {
3333 *e++ = base64_value_to_char[value];
3338 *e++ = base64_value_to_char[value | (0x03 & c >> 6)];
3339 *e++ = base64_value_to_char[0x3f & c];
3345 #undef ADVANCE_INPUT
3347 /* Get next character from the stream, except that non-base64
3348 characters are ignored. This is in accordance with rfc2045. EC
3349 should be an Emchar, so that it can hold -1 as the value for EOF. */
3350 #define ADVANCE_INPUT_IGNORE_NONBASE64(ec, stream, streampos) do { \
3351 ec = Lstream_get_emchar (stream); \
3353 /* IS_BASE64 may not be called with negative arguments so check for \
3355 if (ec < 0 || IS_BASE64 (ec) || ec == '=') \
3359 #define STORE_BYTE(pos, val, ccnt) do { \
3360 pos += set_charptr_emchar (pos, (Emchar)((unsigned char)(val))); \
3365 base64_decode_1(Lstream * istream, Bufbyte * to, Charcount * ccptr)
3369 EMACS_INT streampos = 0;
3373 unsigned long value;
3375 /* Process first byte of a quadruplet. */
3376 ADVANCE_INPUT_IGNORE_NONBASE64(ec, istream, streampos);
3381 ("Illegal `=' character while decoding base64",
3382 make_int(streampos));
3383 value = base64_char_to_value[ec] << 18;
3385 /* Process second byte of a quadruplet. */
3386 ADVANCE_INPUT_IGNORE_NONBASE64(ec, istream, streampos);
3388 error("Premature EOF while decoding base64");
3391 ("Illegal `=' character while decoding base64",
3392 make_int(streampos));
3393 value |= base64_char_to_value[ec] << 12;
3394 STORE_BYTE(e, value >> 16, ccnt);
3396 /* Process third byte of a quadruplet. */
3397 ADVANCE_INPUT_IGNORE_NONBASE64(ec, istream, streampos);
3399 error("Premature EOF while decoding base64");
3402 ADVANCE_INPUT_IGNORE_NONBASE64(ec, istream, streampos);
3404 error("Premature EOF while decoding base64");
3407 ("Padding `=' expected but not found while decoding base64",
3408 make_int(streampos));
3412 value |= base64_char_to_value[ec] << 6;
3413 STORE_BYTE(e, 0xff & value >> 8, ccnt);
3415 /* Process fourth byte of a quadruplet. */
3416 ADVANCE_INPUT_IGNORE_NONBASE64(ec, istream, streampos);
3418 error("Premature EOF while decoding base64");
3422 value |= base64_char_to_value[ec];
3423 STORE_BYTE(e, 0xff & value, ccnt);
3430 #undef ADVANCE_INPUT
3431 #undef ADVANCE_INPUT_IGNORE_NONBASE64
3434 DEFUN("base64-encode-region", Fbase64_encode_region, 2, 3, "r", /*
3435 Base64-encode the region between START and END.
3436 Return the length of the encoded text.
3437 Optional third argument NO-LINE-BREAK means do not break long lines
3440 (start, end, no_line_break))
3443 Bytind encoded_length;
3444 Charcount allength, length;
3445 struct buffer *buf = current_buffer;
3446 Bufpos begv, zv, old_pt = BUF_PT(buf);
3448 int speccount = specpdl_depth();
3450 get_buffer_range_char(buf, start, end, &begv, &zv, 0);
3451 barf_if_buffer_read_only(buf, begv, zv);
3453 /* We need to allocate enough room for encoding the text.
3454 We need 33 1/3% more space, plus a newline every 76
3455 characters, and then we round up. */
3457 allength = length + length / 3 + 1;
3458 allength += allength / MIME_LINE_LENGTH + 1 + 6;
3460 input = make_lisp_buffer_input_stream(buf, begv, zv, 0);
3461 /* We needn't multiply allength with MAX_EMCHAR_LEN because all the
3462 base64 characters will be single-byte. */
3463 XMALLOC_ATOMIC_OR_ALLOCA(encoded, allength, Bufbyte);
3464 encoded_length = base64_encode_1(XLSTREAM(input), encoded,
3465 NILP(no_line_break));
3466 if (encoded_length > allength) {
3469 Lstream_delete(XLSTREAM(input));
3471 /* Now we have encoded the region, so we insert the new contents
3472 and delete the old. (Insert first in order to preserve markers.) */
3473 buffer_insert_raw_string_1(buf, begv, encoded, encoded_length, 0);
3474 XMALLOC_UNBIND(encoded, allength, speccount);
3475 buffer_delete_range(buf, begv + encoded_length, zv + encoded_length, 0);
3477 /* Simulate FSF Emacs implementation of this function: if point was
3478 in the region, place it at the beginning. */
3479 if (old_pt >= begv && old_pt < zv) {
3480 BUF_SET_PT(buf, begv);
3483 /* We return the length of the encoded text. */
3484 return make_int(encoded_length);
3487 DEFUN("base64-encode-string", Fbase64_encode_string, 1, 2, 0, /*
3488 Base64 encode STRING and return the result.
3489 Optional argument NO-LINE-BREAK means do not break long lines
3492 (string, no_line_break))
3494 Charcount allength, length;
3495 Bytind encoded_length;
3497 Lisp_Object input, result;
3498 int speccount = specpdl_depth();
3500 CHECK_STRING(string);
3502 length = XSTRING_CHAR_LENGTH(string);
3503 allength = length + length / 3 + 1;
3504 allength += allength / MIME_LINE_LENGTH + 1 + 6;
3506 input = make_lisp_string_input_stream(string, 0, -1);
3507 XMALLOC_ATOMIC_OR_ALLOCA(encoded, allength, Bufbyte);
3508 encoded_length = base64_encode_1(XLSTREAM(input), encoded,
3509 NILP(no_line_break));
3510 if (encoded_length > allength) {
3513 Lstream_delete(XLSTREAM(input));
3514 result = make_string(encoded, encoded_length);
3515 XMALLOC_UNBIND(encoded, allength, speccount);
3519 DEFUN("base64-decode-region", Fbase64_decode_region, 2, 2, "r", /*
3520 Base64-decode the region between START and END.
3521 Return the length of the decoded text.
3522 If the region can't be decoded, return nil and don't modify the buffer.
3523 Characters out of the base64 alphabet are ignored.
3527 struct buffer *buf = current_buffer;
3528 Bufpos begv, zv, old_pt = BUF_PT(buf);
3530 Bytind decoded_length;
3531 Charcount length, cc_decoded_length;
3533 int speccount = specpdl_depth();
3535 get_buffer_range_char(buf, start, end, &begv, &zv, 0);
3536 barf_if_buffer_read_only(buf, begv, zv);
3540 input = make_lisp_buffer_input_stream(buf, begv, zv, 0);
3541 /* We need to allocate enough room for decoding the text. */
3542 XMALLOC_ATOMIC_OR_ALLOCA(decoded, length * MAX_EMCHAR_LEN, Bufbyte);
3544 base64_decode_1(XLSTREAM(input), decoded, &cc_decoded_length);
3545 if (decoded_length > length * MAX_EMCHAR_LEN) {
3548 Lstream_delete(XLSTREAM(input));
3550 /* Now we have decoded the region, so we insert the new contents
3551 and delete the old. (Insert first in order to preserve markers.) */
3552 BUF_SET_PT(buf, begv);
3553 buffer_insert_raw_string_1(buf, begv, decoded, decoded_length, 0);
3554 XMALLOC_UNBIND(decoded, length * MAX_EMCHAR_LEN, speccount);
3555 buffer_delete_range(buf, begv + cc_decoded_length,
3556 zv + cc_decoded_length, 0);
3558 /* Simulate FSF Emacs implementation of this function: if point was
3559 in the region, place it at the beginning. */
3560 if (old_pt >= begv && old_pt < zv) {
3561 BUF_SET_PT(buf, begv);
3564 return make_int(cc_decoded_length);
3567 DEFUN("base64-decode-string", Fbase64_decode_string, 1, 1, 0, /*
3568 Base64-decode STRING and return the result.
3569 Characters out of the base64 alphabet are ignored.
3574 Bytind decoded_length;
3575 Charcount length, cc_decoded_length;
3576 Lisp_Object input, result;
3577 int speccount = specpdl_depth();
3579 CHECK_STRING(string);
3581 length = XSTRING_CHAR_LENGTH(string);
3582 /* We need to allocate enough room for decoding the text. */
3583 XMALLOC_ATOMIC_OR_ALLOCA(decoded, length * MAX_EMCHAR_LEN, Bufbyte);
3585 input = make_lisp_string_input_stream(string, 0, -1);
3586 decoded_length = base64_decode_1(XLSTREAM(input), decoded,
3587 &cc_decoded_length);
3588 if (decoded_length > length * MAX_EMCHAR_LEN) {
3591 Lstream_delete(XLSTREAM(input));
3593 result = make_string(decoded, decoded_length);
3594 XMALLOC_UNBIND(decoded, length * MAX_EMCHAR_LEN, speccount);
3598 /* base16 encode/decode functions. */
3600 base16_encode_1(Lstream * istream, int length, Bufbyte * to, int max)
3605 for (i=0; i < length; i++) {
3606 ec = Lstream_get_emchar (istream);
3607 sz = snprintf((char *)to+2*i, 3, "%02x", ec);
3608 assert( sz >= 0 && sz < 3);
3616 base16_decode_1(Lstream * istream, int length, Bufbyte * to)
3619 Emchar high = 0, low = 0;
3620 int high_set_p = 0, ignore_p = 0;
3623 /* high and low perform flip flop operation */
3625 ec = Lstream_get_emchar (istream);
3630 else if (isupper(ec))
3631 low = ec - 'A' + 10;
3632 else if (islower(ec))
3633 low = ec - 'a' + 10;
3637 if (low < 0 || low >= 16)
3645 to[i] = high*16+low;
3655 DEFUN("base16-encode-string", Fbase16_encode_string, 1, 1, 0, /*
3656 Base16 encode (i.e. hex dump) STRING and return the result.
3657 Optional argument NO-LINE-BREAK means do not break long lines
3664 Lisp_Object input, result;
3666 int speccount = specpdl_depth();
3668 CHECK_STRING(string);
3670 length = XSTRING_CHAR_LENGTH(string);
3672 input = make_lisp_string_input_stream(string, 0, -1);
3673 XMALLOC_ATOMIC_OR_ALLOCA(encoded, sz+1, Bufbyte);
3674 base16_encode_1(XLSTREAM(input), length, encoded, sz);
3675 Lstream_delete(XLSTREAM(input));
3676 result = make_string(encoded, sz);
3677 XMALLOC_UNBIND(encoded, sz+1, speccount);
3679 XSTRING(result)->plist = XSTRING(string)->plist;
3684 DEFUN("base16-decode-string", Fbase16_decode_string, 1, 1, 0, /*
3685 Base16-decode (i.e. read hex data from) STRING and return the result.
3686 Characters out of the base16 alphabet are ignored.
3691 Bytind decoded_length;
3693 Lisp_Object input, result;
3694 int speccount = specpdl_depth();
3696 CHECK_STRING(string);
3698 length = XSTRING_CHAR_LENGTH(string);
3699 /* We need to allocate enough room for decoding the text. */
3700 XMALLOC_ATOMIC_OR_ALLOCA(decoded, length, Bufbyte);
3702 input = make_lisp_string_input_stream(string, 0, -1);
3703 decoded_length = base16_decode_1(XLSTREAM(input), length, decoded);
3704 Lstream_delete(XLSTREAM(input));
3706 /* this result might be raw, we declare it binary */
3707 result = make_ext_string((char *)decoded, decoded_length, Qbinary);
3708 XMALLOC_UNBIND(decoded, length, speccount);
3710 XSTRING(result)->plist = XSTRING(string)->plist;
3715 Lisp_Object Qyes_or_no_p;
3717 DEFUN("foobar", Ffoobar, 2, 2, 0, /*
3721 return make_int(__nbits_right_of(XINT(n), XINT(b)));
3724 void syms_of_fns(void)
3726 INIT_LRECORD_IMPLEMENTATION(bit_vector);
3728 defsymbol(&Qstring_lessp, "string-lessp");
3729 defsymbol(&Qstring_greaterp, "string-greaterp");
3730 defsymbol(&Qidentity, "identity");
3731 defsymbol(&Qyes_or_no_p, "yes-or-no-p");
3737 #if defined(WITH_GMP) && defined(HAVE_MPZ)
3741 DEFSUBR(Fsafe_length);
3742 DEFSUBR(Fstring_equal);
3743 DEFSUBR(Fstring_lessp);
3744 DEFSUBR(Fstring_greaterp);
3745 DEFSUBR(Fstring_modified_tick);
3750 DEFSUBR(Fcopy_list);
3751 DEFSUBR(Fcopy_sequence);
3752 DEFSUBR(Fcopy_alist);
3753 DEFSUBR(Fcopy_tree);
3754 DEFSUBR(Fsubstring);
3763 DEFSUBR(Fold_member);
3767 DEFSUBR(Fold_assoc);
3771 DEFSUBR(Fold_rassoc);
3773 DEFSUBR(Fold_rassq);
3775 DEFSUBR(Fold_delete);
3780 DEFSUBR(Fremrassoc);
3785 DEFSUBR(Fplists_eq);
3786 DEFSUBR(Fplists_equal);
3787 DEFSUBR(Flax_plists_eq);
3788 DEFSUBR(Flax_plists_equal);
3789 DEFSUBR(Fplist_get);
3790 DEFSUBR(Fplist_put);
3791 DEFSUBR(Fplist_remprop);
3792 DEFSUBR(Fplist_member);
3793 DEFSUBR(Fcheck_valid_plist);
3794 DEFSUBR(Fvalid_plist_p);
3795 DEFSUBR(Fcanonicalize_plist);
3796 DEFSUBR(Flax_plist_get);
3797 DEFSUBR(Flax_plist_put);
3798 DEFSUBR(Flax_plist_remprop);
3799 DEFSUBR(Flax_plist_member);
3800 DEFSUBR(Fcanonicalize_lax_plist);
3801 DEFSUBR(Fdestructive_alist_to_plist);
3805 DEFSUBR(Fobject_plist);
3807 DEFSUBR(Fold_equal);
3808 DEFSUBR(Ffillarray);
3810 DEFSUBR(Freplace_list);
3811 DEFSUBR(Fload_average);
3816 DEFSUBR(Fbase64_encode_region);
3817 DEFSUBR(Fbase64_encode_string);
3818 DEFSUBR(Fbase64_decode_region);
3819 DEFSUBR(Fbase64_decode_string);
3820 DEFSUBR(Fbase16_encode_string);
3821 DEFSUBR(Fbase16_decode_string);
3828 void init_provide_once(void)
3830 DEFVAR_LISP("features", &Vfeatures /*
3831 A list of symbols which are the features of the executing emacs.
3832 Used by `featurep' and `require', and altered by `provide'.
3836 Fprovide(intern("base64"));
3837 Fprovide(intern("base16"));
3839 #if defined HAVE_BDWGC && defined EF_USE_BDWGC
3840 /* it's fuck ugly to define that here :( */
3841 Fprovide(intern("bdwgc"));