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