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