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