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