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