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