Get rid of funvec.
[bpt/emacs.git] / src / fns.c
1 /* Random utility Lisp functions.
2 Copyright (C) 1985-1987, 1993-1995, 1997-2011
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include <config.h>
21
22 #include <unistd.h>
23 #include <time.h>
24 #include <setjmp.h>
25
26 /* Note on some machines this defines `vector' as a typedef,
27 so make sure we don't use that name in this file. */
28 #undef vector
29 #define vector *****
30
31 #include "lisp.h"
32 #include "commands.h"
33 #include "character.h"
34 #include "coding.h"
35 #include "buffer.h"
36 #include "keyboard.h"
37 #include "keymap.h"
38 #include "intervals.h"
39 #include "frame.h"
40 #include "window.h"
41 #include "blockinput.h"
42 #ifdef HAVE_MENUS
43 #if defined (HAVE_X_WINDOWS)
44 #include "xterm.h"
45 #endif
46 #endif /* HAVE_MENUS */
47
48 #ifndef NULL
49 #define NULL ((POINTER_TYPE *)0)
50 #endif
51
52 Lisp_Object Qstring_lessp, Qprovide, Qrequire;
53 Lisp_Object Qyes_or_no_p_history;
54 Lisp_Object Qcursor_in_echo_area;
55 Lisp_Object Qwidget_type;
56 Lisp_Object Qcodeset, Qdays, Qmonths, Qpaper;
57
58 static int internal_equal (Lisp_Object , Lisp_Object, int, int);
59
60 extern long get_random (void);
61 extern void seed_random (long);
62
63 #ifndef HAVE_UNISTD_H
64 extern long time ();
65 #endif
66 \f
67 DEFUN ("identity", Fidentity, Sidentity, 1, 1, 0,
68 doc: /* Return the argument unchanged. */)
69 (Lisp_Object arg)
70 {
71 return arg;
72 }
73
74 DEFUN ("random", Frandom, Srandom, 0, 1, 0,
75 doc: /* Return a pseudo-random number.
76 All integers representable in Lisp are equally likely.
77 On most systems, this is 29 bits' worth.
78 With positive integer LIMIT, return random number in interval [0,LIMIT).
79 With argument t, set the random number seed from the current time and pid.
80 Other values of LIMIT are ignored. */)
81 (Lisp_Object limit)
82 {
83 EMACS_INT val;
84 Lisp_Object lispy_val;
85 unsigned long denominator;
86
87 if (EQ (limit, Qt))
88 seed_random (getpid () + time (NULL));
89 if (NATNUMP (limit) && XFASTINT (limit) != 0)
90 {
91 /* Try to take our random number from the higher bits of VAL,
92 not the lower, since (says Gentzel) the low bits of `random'
93 are less random than the higher ones. We do this by using the
94 quotient rather than the remainder. At the high end of the RNG
95 it's possible to get a quotient larger than n; discarding
96 these values eliminates the bias that would otherwise appear
97 when using a large n. */
98 denominator = ((unsigned long)1 << VALBITS) / XFASTINT (limit);
99 do
100 val = get_random () / denominator;
101 while (val >= XFASTINT (limit));
102 }
103 else
104 val = get_random ();
105 XSETINT (lispy_val, val);
106 return lispy_val;
107 }
108 \f
109 /* Random data-structure functions */
110
111 DEFUN ("length", Flength, Slength, 1, 1, 0,
112 doc: /* Return the length of vector, list or string SEQUENCE.
113 A byte-code function object is also allowed.
114 If the string contains multibyte characters, this is not necessarily
115 the number of bytes in the string; it is the number of characters.
116 To get the number of bytes, use `string-bytes'. */)
117 (register Lisp_Object sequence)
118 {
119 register Lisp_Object val;
120 register int i;
121
122 if (STRINGP (sequence))
123 XSETFASTINT (val, SCHARS (sequence));
124 else if (VECTORP (sequence))
125 XSETFASTINT (val, ASIZE (sequence));
126 else if (CHAR_TABLE_P (sequence))
127 XSETFASTINT (val, MAX_CHAR);
128 else if (BOOL_VECTOR_P (sequence))
129 XSETFASTINT (val, XBOOL_VECTOR (sequence)->size);
130 else if (COMPILEDP (sequence))
131 XSETFASTINT (val, ASIZE (sequence) & PSEUDOVECTOR_SIZE_MASK);
132 else if (CONSP (sequence))
133 {
134 i = 0;
135 while (CONSP (sequence))
136 {
137 sequence = XCDR (sequence);
138 ++i;
139
140 if (!CONSP (sequence))
141 break;
142
143 sequence = XCDR (sequence);
144 ++i;
145 QUIT;
146 }
147
148 CHECK_LIST_END (sequence, sequence);
149
150 val = make_number (i);
151 }
152 else if (NILP (sequence))
153 XSETFASTINT (val, 0);
154 else
155 wrong_type_argument (Qsequencep, sequence);
156
157 return val;
158 }
159
160 /* This does not check for quits. That is safe since it must terminate. */
161
162 DEFUN ("safe-length", Fsafe_length, Ssafe_length, 1, 1, 0,
163 doc: /* Return the length of a list, but avoid error or infinite loop.
164 This function never gets an error. If LIST is not really a list,
165 it returns 0. If LIST is circular, it returns a finite value
166 which is at least the number of distinct elements. */)
167 (Lisp_Object list)
168 {
169 Lisp_Object tail, halftail, length;
170 int len = 0;
171
172 /* halftail is used to detect circular lists. */
173 halftail = list;
174 for (tail = list; CONSP (tail); tail = XCDR (tail))
175 {
176 if (EQ (tail, halftail) && len != 0)
177 break;
178 len++;
179 if ((len & 1) == 0)
180 halftail = XCDR (halftail);
181 }
182
183 XSETINT (length, len);
184 return length;
185 }
186
187 DEFUN ("string-bytes", Fstring_bytes, Sstring_bytes, 1, 1, 0,
188 doc: /* Return the number of bytes in STRING.
189 If STRING is multibyte, this may be greater than the length of STRING. */)
190 (Lisp_Object string)
191 {
192 CHECK_STRING (string);
193 return make_number (SBYTES (string));
194 }
195
196 DEFUN ("string-equal", Fstring_equal, Sstring_equal, 2, 2, 0,
197 doc: /* Return t if two strings have identical contents.
198 Case is significant, but text properties are ignored.
199 Symbols are also allowed; their print names are used instead. */)
200 (register Lisp_Object s1, Lisp_Object s2)
201 {
202 if (SYMBOLP (s1))
203 s1 = SYMBOL_NAME (s1);
204 if (SYMBOLP (s2))
205 s2 = SYMBOL_NAME (s2);
206 CHECK_STRING (s1);
207 CHECK_STRING (s2);
208
209 if (SCHARS (s1) != SCHARS (s2)
210 || SBYTES (s1) != SBYTES (s2)
211 || memcmp (SDATA (s1), SDATA (s2), SBYTES (s1)))
212 return Qnil;
213 return Qt;
214 }
215
216 DEFUN ("compare-strings", Fcompare_strings, Scompare_strings, 6, 7, 0,
217 doc: /* Compare the contents of two strings, converting to multibyte if needed.
218 In string STR1, skip the first START1 characters and stop at END1.
219 In string STR2, skip the first START2 characters and stop at END2.
220 END1 and END2 default to the full lengths of the respective strings.
221
222 Case is significant in this comparison if IGNORE-CASE is nil.
223 Unibyte strings are converted to multibyte for comparison.
224
225 The value is t if the strings (or specified portions) match.
226 If string STR1 is less, the value is a negative number N;
227 - 1 - N is the number of characters that match at the beginning.
228 If string STR1 is greater, the value is a positive number N;
229 N - 1 is the number of characters that match at the beginning. */)
230 (Lisp_Object str1, Lisp_Object start1, Lisp_Object end1, Lisp_Object str2, Lisp_Object start2, Lisp_Object end2, Lisp_Object ignore_case)
231 {
232 register EMACS_INT end1_char, end2_char;
233 register EMACS_INT i1, i1_byte, i2, i2_byte;
234
235 CHECK_STRING (str1);
236 CHECK_STRING (str2);
237 if (NILP (start1))
238 start1 = make_number (0);
239 if (NILP (start2))
240 start2 = make_number (0);
241 CHECK_NATNUM (start1);
242 CHECK_NATNUM (start2);
243 if (! NILP (end1))
244 CHECK_NATNUM (end1);
245 if (! NILP (end2))
246 CHECK_NATNUM (end2);
247
248 i1 = XINT (start1);
249 i2 = XINT (start2);
250
251 i1_byte = string_char_to_byte (str1, i1);
252 i2_byte = string_char_to_byte (str2, i2);
253
254 end1_char = SCHARS (str1);
255 if (! NILP (end1) && end1_char > XINT (end1))
256 end1_char = XINT (end1);
257
258 end2_char = SCHARS (str2);
259 if (! NILP (end2) && end2_char > XINT (end2))
260 end2_char = XINT (end2);
261
262 while (i1 < end1_char && i2 < end2_char)
263 {
264 /* When we find a mismatch, we must compare the
265 characters, not just the bytes. */
266 int c1, c2;
267
268 if (STRING_MULTIBYTE (str1))
269 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c1, str1, i1, i1_byte);
270 else
271 {
272 c1 = SREF (str1, i1++);
273 MAKE_CHAR_MULTIBYTE (c1);
274 }
275
276 if (STRING_MULTIBYTE (str2))
277 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c2, str2, i2, i2_byte);
278 else
279 {
280 c2 = SREF (str2, i2++);
281 MAKE_CHAR_MULTIBYTE (c2);
282 }
283
284 if (c1 == c2)
285 continue;
286
287 if (! NILP (ignore_case))
288 {
289 Lisp_Object tem;
290
291 tem = Fupcase (make_number (c1));
292 c1 = XINT (tem);
293 tem = Fupcase (make_number (c2));
294 c2 = XINT (tem);
295 }
296
297 if (c1 == c2)
298 continue;
299
300 /* Note that I1 has already been incremented
301 past the character that we are comparing;
302 hence we don't add or subtract 1 here. */
303 if (c1 < c2)
304 return make_number (- i1 + XINT (start1));
305 else
306 return make_number (i1 - XINT (start1));
307 }
308
309 if (i1 < end1_char)
310 return make_number (i1 - XINT (start1) + 1);
311 if (i2 < end2_char)
312 return make_number (- i1 + XINT (start1) - 1);
313
314 return Qt;
315 }
316
317 DEFUN ("string-lessp", Fstring_lessp, Sstring_lessp, 2, 2, 0,
318 doc: /* Return t if first arg string is less than second in lexicographic order.
319 Case is significant.
320 Symbols are also allowed; their print names are used instead. */)
321 (register Lisp_Object s1, Lisp_Object s2)
322 {
323 register EMACS_INT end;
324 register EMACS_INT i1, i1_byte, i2, i2_byte;
325
326 if (SYMBOLP (s1))
327 s1 = SYMBOL_NAME (s1);
328 if (SYMBOLP (s2))
329 s2 = SYMBOL_NAME (s2);
330 CHECK_STRING (s1);
331 CHECK_STRING (s2);
332
333 i1 = i1_byte = i2 = i2_byte = 0;
334
335 end = SCHARS (s1);
336 if (end > SCHARS (s2))
337 end = SCHARS (s2);
338
339 while (i1 < end)
340 {
341 /* When we find a mismatch, we must compare the
342 characters, not just the bytes. */
343 int c1, c2;
344
345 FETCH_STRING_CHAR_ADVANCE (c1, s1, i1, i1_byte);
346 FETCH_STRING_CHAR_ADVANCE (c2, s2, i2, i2_byte);
347
348 if (c1 != c2)
349 return c1 < c2 ? Qt : Qnil;
350 }
351 return i1 < SCHARS (s2) ? Qt : Qnil;
352 }
353 \f
354 static Lisp_Object concat (int nargs, Lisp_Object *args,
355 enum Lisp_Type target_type, int last_special);
356
357 /* ARGSUSED */
358 Lisp_Object
359 concat2 (Lisp_Object s1, Lisp_Object s2)
360 {
361 Lisp_Object args[2];
362 args[0] = s1;
363 args[1] = s2;
364 return concat (2, args, Lisp_String, 0);
365 }
366
367 /* ARGSUSED */
368 Lisp_Object
369 concat3 (Lisp_Object s1, Lisp_Object s2, Lisp_Object s3)
370 {
371 Lisp_Object args[3];
372 args[0] = s1;
373 args[1] = s2;
374 args[2] = s3;
375 return concat (3, args, Lisp_String, 0);
376 }
377
378 DEFUN ("append", Fappend, Sappend, 0, MANY, 0,
379 doc: /* Concatenate all the arguments and make the result a list.
380 The result is a list whose elements are the elements of all the arguments.
381 Each argument may be a list, vector or string.
382 The last argument is not copied, just used as the tail of the new list.
383 usage: (append &rest SEQUENCES) */)
384 (int nargs, Lisp_Object *args)
385 {
386 return concat (nargs, args, Lisp_Cons, 1);
387 }
388
389 DEFUN ("concat", Fconcat, Sconcat, 0, MANY, 0,
390 doc: /* Concatenate all the arguments and make the result a string.
391 The result is a string whose elements are the elements of all the arguments.
392 Each argument may be a string or a list or vector of characters (integers).
393 usage: (concat &rest SEQUENCES) */)
394 (int nargs, Lisp_Object *args)
395 {
396 return concat (nargs, args, Lisp_String, 0);
397 }
398
399 DEFUN ("vconcat", Fvconcat, Svconcat, 0, MANY, 0,
400 doc: /* Concatenate all the arguments and make the result a vector.
401 The result is a vector whose elements are the elements of all the arguments.
402 Each argument may be a list, vector or string.
403 usage: (vconcat &rest SEQUENCES) */)
404 (int nargs, Lisp_Object *args)
405 {
406 return concat (nargs, args, Lisp_Vectorlike, 0);
407 }
408
409
410 DEFUN ("copy-sequence", Fcopy_sequence, Scopy_sequence, 1, 1, 0,
411 doc: /* Return a copy of a list, vector, string or char-table.
412 The elements of a list or vector are not copied; they are shared
413 with the original. */)
414 (Lisp_Object arg)
415 {
416 if (NILP (arg)) return arg;
417
418 if (CHAR_TABLE_P (arg))
419 {
420 return copy_char_table (arg);
421 }
422
423 if (BOOL_VECTOR_P (arg))
424 {
425 Lisp_Object val;
426 int size_in_chars
427 = ((XBOOL_VECTOR (arg)->size + BOOL_VECTOR_BITS_PER_CHAR - 1)
428 / BOOL_VECTOR_BITS_PER_CHAR);
429
430 val = Fmake_bool_vector (Flength (arg), Qnil);
431 memcpy (XBOOL_VECTOR (val)->data, XBOOL_VECTOR (arg)->data,
432 size_in_chars);
433 return val;
434 }
435
436 if (!CONSP (arg) && !VECTORP (arg) && !STRINGP (arg))
437 wrong_type_argument (Qsequencep, arg);
438
439 return concat (1, &arg, CONSP (arg) ? Lisp_Cons : XTYPE (arg), 0);
440 }
441
442 /* This structure holds information of an argument of `concat' that is
443 a string and has text properties to be copied. */
444 struct textprop_rec
445 {
446 int argnum; /* refer to ARGS (arguments of `concat') */
447 EMACS_INT from; /* refer to ARGS[argnum] (argument string) */
448 EMACS_INT to; /* refer to VAL (the target string) */
449 };
450
451 static Lisp_Object
452 concat (int nargs, Lisp_Object *args, enum Lisp_Type target_type, int last_special)
453 {
454 Lisp_Object val;
455 register Lisp_Object tail;
456 register Lisp_Object this;
457 EMACS_INT toindex;
458 EMACS_INT toindex_byte = 0;
459 register EMACS_INT result_len;
460 register EMACS_INT result_len_byte;
461 register int argnum;
462 Lisp_Object last_tail;
463 Lisp_Object prev;
464 int some_multibyte;
465 /* When we make a multibyte string, we can't copy text properties
466 while concatinating each string because the length of resulting
467 string can't be decided until we finish the whole concatination.
468 So, we record strings that have text properties to be copied
469 here, and copy the text properties after the concatination. */
470 struct textprop_rec *textprops = NULL;
471 /* Number of elements in textprops. */
472 int num_textprops = 0;
473 USE_SAFE_ALLOCA;
474
475 tail = Qnil;
476
477 /* In append, the last arg isn't treated like the others */
478 if (last_special && nargs > 0)
479 {
480 nargs--;
481 last_tail = args[nargs];
482 }
483 else
484 last_tail = Qnil;
485
486 /* Check each argument. */
487 for (argnum = 0; argnum < nargs; argnum++)
488 {
489 this = args[argnum];
490 if (!(CONSP (this) || NILP (this) || VECTORP (this) || STRINGP (this)
491 || COMPILEDP (this) || BOOL_VECTOR_P (this)))
492 wrong_type_argument (Qsequencep, this);
493 }
494
495 /* Compute total length in chars of arguments in RESULT_LEN.
496 If desired output is a string, also compute length in bytes
497 in RESULT_LEN_BYTE, and determine in SOME_MULTIBYTE
498 whether the result should be a multibyte string. */
499 result_len_byte = 0;
500 result_len = 0;
501 some_multibyte = 0;
502 for (argnum = 0; argnum < nargs; argnum++)
503 {
504 EMACS_INT len;
505 this = args[argnum];
506 len = XFASTINT (Flength (this));
507 if (target_type == Lisp_String)
508 {
509 /* We must count the number of bytes needed in the string
510 as well as the number of characters. */
511 EMACS_INT i;
512 Lisp_Object ch;
513 EMACS_INT this_len_byte;
514
515 if (VECTORP (this) || COMPILEDP (this))
516 for (i = 0; i < len; i++)
517 {
518 ch = AREF (this, i);
519 CHECK_CHARACTER (ch);
520 this_len_byte = CHAR_BYTES (XINT (ch));
521 result_len_byte += this_len_byte;
522 if (! ASCII_CHAR_P (XINT (ch)) && ! CHAR_BYTE8_P (XINT (ch)))
523 some_multibyte = 1;
524 }
525 else if (BOOL_VECTOR_P (this) && XBOOL_VECTOR (this)->size > 0)
526 wrong_type_argument (Qintegerp, Faref (this, make_number (0)));
527 else if (CONSP (this))
528 for (; CONSP (this); this = XCDR (this))
529 {
530 ch = XCAR (this);
531 CHECK_CHARACTER (ch);
532 this_len_byte = CHAR_BYTES (XINT (ch));
533 result_len_byte += this_len_byte;
534 if (! ASCII_CHAR_P (XINT (ch)) && ! CHAR_BYTE8_P (XINT (ch)))
535 some_multibyte = 1;
536 }
537 else if (STRINGP (this))
538 {
539 if (STRING_MULTIBYTE (this))
540 {
541 some_multibyte = 1;
542 result_len_byte += SBYTES (this);
543 }
544 else
545 result_len_byte += count_size_as_multibyte (SDATA (this),
546 SCHARS (this));
547 }
548 }
549
550 result_len += len;
551 if (result_len < 0)
552 error ("String overflow");
553 }
554
555 if (! some_multibyte)
556 result_len_byte = result_len;
557
558 /* Create the output object. */
559 if (target_type == Lisp_Cons)
560 val = Fmake_list (make_number (result_len), Qnil);
561 else if (target_type == Lisp_Vectorlike)
562 val = Fmake_vector (make_number (result_len), Qnil);
563 else if (some_multibyte)
564 val = make_uninit_multibyte_string (result_len, result_len_byte);
565 else
566 val = make_uninit_string (result_len);
567
568 /* In `append', if all but last arg are nil, return last arg. */
569 if (target_type == Lisp_Cons && EQ (val, Qnil))
570 return last_tail;
571
572 /* Copy the contents of the args into the result. */
573 if (CONSP (val))
574 tail = val, toindex = -1; /* -1 in toindex is flag we are making a list */
575 else
576 toindex = 0, toindex_byte = 0;
577
578 prev = Qnil;
579 if (STRINGP (val))
580 SAFE_ALLOCA (textprops, struct textprop_rec *, sizeof (struct textprop_rec) * nargs);
581
582 for (argnum = 0; argnum < nargs; argnum++)
583 {
584 Lisp_Object thislen;
585 EMACS_INT thisleni = 0;
586 register EMACS_INT thisindex = 0;
587 register EMACS_INT thisindex_byte = 0;
588
589 this = args[argnum];
590 if (!CONSP (this))
591 thislen = Flength (this), thisleni = XINT (thislen);
592
593 /* Between strings of the same kind, copy fast. */
594 if (STRINGP (this) && STRINGP (val)
595 && STRING_MULTIBYTE (this) == some_multibyte)
596 {
597 EMACS_INT thislen_byte = SBYTES (this);
598
599 memcpy (SDATA (val) + toindex_byte, SDATA (this), SBYTES (this));
600 if (! NULL_INTERVAL_P (STRING_INTERVALS (this)))
601 {
602 textprops[num_textprops].argnum = argnum;
603 textprops[num_textprops].from = 0;
604 textprops[num_textprops++].to = toindex;
605 }
606 toindex_byte += thislen_byte;
607 toindex += thisleni;
608 }
609 /* Copy a single-byte string to a multibyte string. */
610 else if (STRINGP (this) && STRINGP (val))
611 {
612 if (! NULL_INTERVAL_P (STRING_INTERVALS (this)))
613 {
614 textprops[num_textprops].argnum = argnum;
615 textprops[num_textprops].from = 0;
616 textprops[num_textprops++].to = toindex;
617 }
618 toindex_byte += copy_text (SDATA (this),
619 SDATA (val) + toindex_byte,
620 SCHARS (this), 0, 1);
621 toindex += thisleni;
622 }
623 else
624 /* Copy element by element. */
625 while (1)
626 {
627 register Lisp_Object elt;
628
629 /* Fetch next element of `this' arg into `elt', or break if
630 `this' is exhausted. */
631 if (NILP (this)) break;
632 if (CONSP (this))
633 elt = XCAR (this), this = XCDR (this);
634 else if (thisindex >= thisleni)
635 break;
636 else if (STRINGP (this))
637 {
638 int c;
639 if (STRING_MULTIBYTE (this))
640 {
641 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, this,
642 thisindex,
643 thisindex_byte);
644 XSETFASTINT (elt, c);
645 }
646 else
647 {
648 XSETFASTINT (elt, SREF (this, thisindex)); thisindex++;
649 if (some_multibyte
650 && !ASCII_CHAR_P (XINT (elt))
651 && XINT (elt) < 0400)
652 {
653 c = BYTE8_TO_CHAR (XINT (elt));
654 XSETINT (elt, c);
655 }
656 }
657 }
658 else if (BOOL_VECTOR_P (this))
659 {
660 int byte;
661 byte = XBOOL_VECTOR (this)->data[thisindex / BOOL_VECTOR_BITS_PER_CHAR];
662 if (byte & (1 << (thisindex % BOOL_VECTOR_BITS_PER_CHAR)))
663 elt = Qt;
664 else
665 elt = Qnil;
666 thisindex++;
667 }
668 else
669 {
670 elt = AREF (this, thisindex);
671 thisindex++;
672 }
673
674 /* Store this element into the result. */
675 if (toindex < 0)
676 {
677 XSETCAR (tail, elt);
678 prev = tail;
679 tail = XCDR (tail);
680 }
681 else if (VECTORP (val))
682 {
683 ASET (val, toindex, elt);
684 toindex++;
685 }
686 else
687 {
688 CHECK_NUMBER (elt);
689 if (some_multibyte)
690 toindex_byte += CHAR_STRING (XINT (elt),
691 SDATA (val) + toindex_byte);
692 else
693 SSET (val, toindex_byte++, XINT (elt));
694 toindex++;
695 }
696 }
697 }
698 if (!NILP (prev))
699 XSETCDR (prev, last_tail);
700
701 if (num_textprops > 0)
702 {
703 Lisp_Object props;
704 EMACS_INT last_to_end = -1;
705
706 for (argnum = 0; argnum < num_textprops; argnum++)
707 {
708 this = args[textprops[argnum].argnum];
709 props = text_property_list (this,
710 make_number (0),
711 make_number (SCHARS (this)),
712 Qnil);
713 /* If successive arguments have properites, be sure that the
714 value of `composition' property be the copy. */
715 if (last_to_end == textprops[argnum].to)
716 make_composition_value_copy (props);
717 add_text_properties_from_list (val, props,
718 make_number (textprops[argnum].to));
719 last_to_end = textprops[argnum].to + SCHARS (this);
720 }
721 }
722
723 SAFE_FREE ();
724 return val;
725 }
726 \f
727 static Lisp_Object string_char_byte_cache_string;
728 static EMACS_INT string_char_byte_cache_charpos;
729 static EMACS_INT string_char_byte_cache_bytepos;
730
731 void
732 clear_string_char_byte_cache (void)
733 {
734 string_char_byte_cache_string = Qnil;
735 }
736
737 /* Return the byte index corresponding to CHAR_INDEX in STRING. */
738
739 EMACS_INT
740 string_char_to_byte (Lisp_Object string, EMACS_INT char_index)
741 {
742 EMACS_INT i_byte;
743 EMACS_INT best_below, best_below_byte;
744 EMACS_INT best_above, best_above_byte;
745
746 best_below = best_below_byte = 0;
747 best_above = SCHARS (string);
748 best_above_byte = SBYTES (string);
749 if (best_above == best_above_byte)
750 return char_index;
751
752 if (EQ (string, string_char_byte_cache_string))
753 {
754 if (string_char_byte_cache_charpos < char_index)
755 {
756 best_below = string_char_byte_cache_charpos;
757 best_below_byte = string_char_byte_cache_bytepos;
758 }
759 else
760 {
761 best_above = string_char_byte_cache_charpos;
762 best_above_byte = string_char_byte_cache_bytepos;
763 }
764 }
765
766 if (char_index - best_below < best_above - char_index)
767 {
768 unsigned char *p = SDATA (string) + best_below_byte;
769
770 while (best_below < char_index)
771 {
772 p += BYTES_BY_CHAR_HEAD (*p);
773 best_below++;
774 }
775 i_byte = p - SDATA (string);
776 }
777 else
778 {
779 unsigned char *p = SDATA (string) + best_above_byte;
780
781 while (best_above > char_index)
782 {
783 p--;
784 while (!CHAR_HEAD_P (*p)) p--;
785 best_above--;
786 }
787 i_byte = p - SDATA (string);
788 }
789
790 string_char_byte_cache_bytepos = i_byte;
791 string_char_byte_cache_charpos = char_index;
792 string_char_byte_cache_string = string;
793
794 return i_byte;
795 }
796 \f
797 /* Return the character index corresponding to BYTE_INDEX in STRING. */
798
799 EMACS_INT
800 string_byte_to_char (Lisp_Object string, EMACS_INT byte_index)
801 {
802 EMACS_INT i, i_byte;
803 EMACS_INT best_below, best_below_byte;
804 EMACS_INT best_above, best_above_byte;
805
806 best_below = best_below_byte = 0;
807 best_above = SCHARS (string);
808 best_above_byte = SBYTES (string);
809 if (best_above == best_above_byte)
810 return byte_index;
811
812 if (EQ (string, string_char_byte_cache_string))
813 {
814 if (string_char_byte_cache_bytepos < byte_index)
815 {
816 best_below = string_char_byte_cache_charpos;
817 best_below_byte = string_char_byte_cache_bytepos;
818 }
819 else
820 {
821 best_above = string_char_byte_cache_charpos;
822 best_above_byte = string_char_byte_cache_bytepos;
823 }
824 }
825
826 if (byte_index - best_below_byte < best_above_byte - byte_index)
827 {
828 unsigned char *p = SDATA (string) + best_below_byte;
829 unsigned char *pend = SDATA (string) + byte_index;
830
831 while (p < pend)
832 {
833 p += BYTES_BY_CHAR_HEAD (*p);
834 best_below++;
835 }
836 i = best_below;
837 i_byte = p - SDATA (string);
838 }
839 else
840 {
841 unsigned char *p = SDATA (string) + best_above_byte;
842 unsigned char *pbeg = SDATA (string) + byte_index;
843
844 while (p > pbeg)
845 {
846 p--;
847 while (!CHAR_HEAD_P (*p)) p--;
848 best_above--;
849 }
850 i = best_above;
851 i_byte = p - SDATA (string);
852 }
853
854 string_char_byte_cache_bytepos = i_byte;
855 string_char_byte_cache_charpos = i;
856 string_char_byte_cache_string = string;
857
858 return i;
859 }
860 \f
861 /* Convert STRING to a multibyte string. */
862
863 static Lisp_Object
864 string_make_multibyte (Lisp_Object string)
865 {
866 unsigned char *buf;
867 EMACS_INT nbytes;
868 Lisp_Object ret;
869 USE_SAFE_ALLOCA;
870
871 if (STRING_MULTIBYTE (string))
872 return string;
873
874 nbytes = count_size_as_multibyte (SDATA (string),
875 SCHARS (string));
876 /* If all the chars are ASCII, they won't need any more bytes
877 once converted. In that case, we can return STRING itself. */
878 if (nbytes == SBYTES (string))
879 return string;
880
881 SAFE_ALLOCA (buf, unsigned char *, nbytes);
882 copy_text (SDATA (string), buf, SBYTES (string),
883 0, 1);
884
885 ret = make_multibyte_string ((char *) buf, SCHARS (string), nbytes);
886 SAFE_FREE ();
887
888 return ret;
889 }
890
891
892 /* Convert STRING (if unibyte) to a multibyte string without changing
893 the number of characters. Characters 0200 trough 0237 are
894 converted to eight-bit characters. */
895
896 Lisp_Object
897 string_to_multibyte (Lisp_Object string)
898 {
899 unsigned char *buf;
900 EMACS_INT nbytes;
901 Lisp_Object ret;
902 USE_SAFE_ALLOCA;
903
904 if (STRING_MULTIBYTE (string))
905 return string;
906
907 nbytes = parse_str_to_multibyte (SDATA (string), SBYTES (string));
908 /* If all the chars are ASCII, they won't need any more bytes once
909 converted. */
910 if (nbytes == SBYTES (string))
911 return make_multibyte_string (SSDATA (string), nbytes, nbytes);
912
913 SAFE_ALLOCA (buf, unsigned char *, nbytes);
914 memcpy (buf, SDATA (string), SBYTES (string));
915 str_to_multibyte (buf, nbytes, SBYTES (string));
916
917 ret = make_multibyte_string ((char *) buf, SCHARS (string), nbytes);
918 SAFE_FREE ();
919
920 return ret;
921 }
922
923
924 /* Convert STRING to a single-byte string. */
925
926 Lisp_Object
927 string_make_unibyte (Lisp_Object string)
928 {
929 EMACS_INT nchars;
930 unsigned char *buf;
931 Lisp_Object ret;
932 USE_SAFE_ALLOCA;
933
934 if (! STRING_MULTIBYTE (string))
935 return string;
936
937 nchars = SCHARS (string);
938
939 SAFE_ALLOCA (buf, unsigned char *, nchars);
940 copy_text (SDATA (string), buf, SBYTES (string),
941 1, 0);
942
943 ret = make_unibyte_string ((char *) buf, nchars);
944 SAFE_FREE ();
945
946 return ret;
947 }
948
949 DEFUN ("string-make-multibyte", Fstring_make_multibyte, Sstring_make_multibyte,
950 1, 1, 0,
951 doc: /* Return the multibyte equivalent of STRING.
952 If STRING is unibyte and contains non-ASCII characters, the function
953 `unibyte-char-to-multibyte' is used to convert each unibyte character
954 to a multibyte character. In this case, the returned string is a
955 newly created string with no text properties. If STRING is multibyte
956 or entirely ASCII, it is returned unchanged. In particular, when
957 STRING is unibyte and entirely ASCII, the returned string is unibyte.
958 \(When the characters are all ASCII, Emacs primitives will treat the
959 string the same way whether it is unibyte or multibyte.) */)
960 (Lisp_Object string)
961 {
962 CHECK_STRING (string);
963
964 return string_make_multibyte (string);
965 }
966
967 DEFUN ("string-make-unibyte", Fstring_make_unibyte, Sstring_make_unibyte,
968 1, 1, 0,
969 doc: /* Return the unibyte equivalent of STRING.
970 Multibyte character codes are converted to unibyte according to
971 `nonascii-translation-table' or, if that is nil, `nonascii-insert-offset'.
972 If the lookup in the translation table fails, this function takes just
973 the low 8 bits of each character. */)
974 (Lisp_Object string)
975 {
976 CHECK_STRING (string);
977
978 return string_make_unibyte (string);
979 }
980
981 DEFUN ("string-as-unibyte", Fstring_as_unibyte, Sstring_as_unibyte,
982 1, 1, 0,
983 doc: /* Return a unibyte string with the same individual bytes as STRING.
984 If STRING is unibyte, the result is STRING itself.
985 Otherwise it is a newly created string, with no text properties.
986 If STRING is multibyte and contains a character of charset
987 `eight-bit', it is converted to the corresponding single byte. */)
988 (Lisp_Object string)
989 {
990 CHECK_STRING (string);
991
992 if (STRING_MULTIBYTE (string))
993 {
994 EMACS_INT bytes = SBYTES (string);
995 unsigned char *str = (unsigned char *) xmalloc (bytes);
996
997 memcpy (str, SDATA (string), bytes);
998 bytes = str_as_unibyte (str, bytes);
999 string = make_unibyte_string ((char *) str, bytes);
1000 xfree (str);
1001 }
1002 return string;
1003 }
1004
1005 DEFUN ("string-as-multibyte", Fstring_as_multibyte, Sstring_as_multibyte,
1006 1, 1, 0,
1007 doc: /* Return a multibyte string with the same individual bytes as STRING.
1008 If STRING is multibyte, the result is STRING itself.
1009 Otherwise it is a newly created string, with no text properties.
1010
1011 If STRING is unibyte and contains an individual 8-bit byte (i.e. not
1012 part of a correct utf-8 sequence), it is converted to the corresponding
1013 multibyte character of charset `eight-bit'.
1014 See also `string-to-multibyte'.
1015
1016 Beware, this often doesn't really do what you think it does.
1017 It is similar to (decode-coding-string STRING 'utf-8-emacs).
1018 If you're not sure, whether to use `string-as-multibyte' or
1019 `string-to-multibyte', use `string-to-multibyte'. */)
1020 (Lisp_Object string)
1021 {
1022 CHECK_STRING (string);
1023
1024 if (! STRING_MULTIBYTE (string))
1025 {
1026 Lisp_Object new_string;
1027 EMACS_INT nchars, nbytes;
1028
1029 parse_str_as_multibyte (SDATA (string),
1030 SBYTES (string),
1031 &nchars, &nbytes);
1032 new_string = make_uninit_multibyte_string (nchars, nbytes);
1033 memcpy (SDATA (new_string), SDATA (string), SBYTES (string));
1034 if (nbytes != SBYTES (string))
1035 str_as_multibyte (SDATA (new_string), nbytes,
1036 SBYTES (string), NULL);
1037 string = new_string;
1038 STRING_SET_INTERVALS (string, NULL_INTERVAL);
1039 }
1040 return string;
1041 }
1042
1043 DEFUN ("string-to-multibyte", Fstring_to_multibyte, Sstring_to_multibyte,
1044 1, 1, 0,
1045 doc: /* Return a multibyte string with the same individual chars as STRING.
1046 If STRING is multibyte, the result is STRING itself.
1047 Otherwise it is a newly created string, with no text properties.
1048
1049 If STRING is unibyte and contains an 8-bit byte, it is converted to
1050 the corresponding multibyte character of charset `eight-bit'.
1051
1052 This differs from `string-as-multibyte' by converting each byte of a correct
1053 utf-8 sequence to an eight-bit character, not just bytes that don't form a
1054 correct sequence. */)
1055 (Lisp_Object string)
1056 {
1057 CHECK_STRING (string);
1058
1059 return string_to_multibyte (string);
1060 }
1061
1062 DEFUN ("string-to-unibyte", Fstring_to_unibyte, Sstring_to_unibyte,
1063 1, 1, 0,
1064 doc: /* Return a unibyte string with the same individual chars as STRING.
1065 If STRING is unibyte, the result is STRING itself.
1066 Otherwise it is a newly created string, with no text properties,
1067 where each `eight-bit' character is converted to the corresponding byte.
1068 If STRING contains a non-ASCII, non-`eight-bit' character,
1069 an error is signaled. */)
1070 (Lisp_Object string)
1071 {
1072 CHECK_STRING (string);
1073
1074 if (STRING_MULTIBYTE (string))
1075 {
1076 EMACS_INT chars = SCHARS (string);
1077 unsigned char *str = (unsigned char *) xmalloc (chars);
1078 EMACS_INT converted = str_to_unibyte (SDATA (string), str, chars, 0);
1079
1080 if (converted < chars)
1081 error ("Can't convert the %dth character to unibyte", converted);
1082 string = make_unibyte_string ((char *) str, chars);
1083 xfree (str);
1084 }
1085 return string;
1086 }
1087
1088 \f
1089 DEFUN ("copy-alist", Fcopy_alist, Scopy_alist, 1, 1, 0,
1090 doc: /* Return a copy of ALIST.
1091 This is an alist which represents the same mapping from objects to objects,
1092 but does not share the alist structure with ALIST.
1093 The objects mapped (cars and cdrs of elements of the alist)
1094 are shared, however.
1095 Elements of ALIST that are not conses are also shared. */)
1096 (Lisp_Object alist)
1097 {
1098 register Lisp_Object tem;
1099
1100 CHECK_LIST (alist);
1101 if (NILP (alist))
1102 return alist;
1103 alist = concat (1, &alist, Lisp_Cons, 0);
1104 for (tem = alist; CONSP (tem); tem = XCDR (tem))
1105 {
1106 register Lisp_Object car;
1107 car = XCAR (tem);
1108
1109 if (CONSP (car))
1110 XSETCAR (tem, Fcons (XCAR (car), XCDR (car)));
1111 }
1112 return alist;
1113 }
1114
1115 DEFUN ("substring", Fsubstring, Ssubstring, 2, 3, 0,
1116 doc: /* Return a new string whose contents are a substring of STRING.
1117 The returned string consists of the characters between index FROM
1118 \(inclusive) and index TO (exclusive) of STRING. FROM and TO are
1119 zero-indexed: 0 means the first character of STRING. Negative values
1120 are counted from the end of STRING. If TO is nil, the substring runs
1121 to the end of STRING.
1122
1123 The STRING argument may also be a vector. In that case, the return
1124 value is a new vector that contains the elements between index FROM
1125 \(inclusive) and index TO (exclusive) of that vector argument. */)
1126 (Lisp_Object string, register Lisp_Object from, Lisp_Object to)
1127 {
1128 Lisp_Object res;
1129 EMACS_INT size;
1130 EMACS_INT size_byte = 0;
1131 EMACS_INT from_char, to_char;
1132 EMACS_INT from_byte = 0, to_byte = 0;
1133
1134 CHECK_VECTOR_OR_STRING (string);
1135 CHECK_NUMBER (from);
1136
1137 if (STRINGP (string))
1138 {
1139 size = SCHARS (string);
1140 size_byte = SBYTES (string);
1141 }
1142 else
1143 size = ASIZE (string);
1144
1145 if (NILP (to))
1146 {
1147 to_char = size;
1148 to_byte = size_byte;
1149 }
1150 else
1151 {
1152 CHECK_NUMBER (to);
1153
1154 to_char = XINT (to);
1155 if (to_char < 0)
1156 to_char += size;
1157
1158 if (STRINGP (string))
1159 to_byte = string_char_to_byte (string, to_char);
1160 }
1161
1162 from_char = XINT (from);
1163 if (from_char < 0)
1164 from_char += size;
1165 if (STRINGP (string))
1166 from_byte = string_char_to_byte (string, from_char);
1167
1168 if (!(0 <= from_char && from_char <= to_char && to_char <= size))
1169 args_out_of_range_3 (string, make_number (from_char),
1170 make_number (to_char));
1171
1172 if (STRINGP (string))
1173 {
1174 res = make_specified_string (SSDATA (string) + from_byte,
1175 to_char - from_char, to_byte - from_byte,
1176 STRING_MULTIBYTE (string));
1177 copy_text_properties (make_number (from_char), make_number (to_char),
1178 string, make_number (0), res, Qnil);
1179 }
1180 else
1181 res = Fvector (to_char - from_char, &AREF (string, from_char));
1182
1183 return res;
1184 }
1185
1186
1187 DEFUN ("substring-no-properties", Fsubstring_no_properties, Ssubstring_no_properties, 1, 3, 0,
1188 doc: /* Return a substring of STRING, without text properties.
1189 It starts at index FROM and ends before TO.
1190 TO may be nil or omitted; then the substring runs to the end of STRING.
1191 If FROM is nil or omitted, the substring starts at the beginning of STRING.
1192 If FROM or TO is negative, it counts from the end.
1193
1194 With one argument, just copy STRING without its properties. */)
1195 (Lisp_Object string, register Lisp_Object from, Lisp_Object to)
1196 {
1197 EMACS_INT size, size_byte;
1198 EMACS_INT from_char, to_char;
1199 EMACS_INT from_byte, to_byte;
1200
1201 CHECK_STRING (string);
1202
1203 size = SCHARS (string);
1204 size_byte = SBYTES (string);
1205
1206 if (NILP (from))
1207 from_char = from_byte = 0;
1208 else
1209 {
1210 CHECK_NUMBER (from);
1211 from_char = XINT (from);
1212 if (from_char < 0)
1213 from_char += size;
1214
1215 from_byte = string_char_to_byte (string, from_char);
1216 }
1217
1218 if (NILP (to))
1219 {
1220 to_char = size;
1221 to_byte = size_byte;
1222 }
1223 else
1224 {
1225 CHECK_NUMBER (to);
1226
1227 to_char = XINT (to);
1228 if (to_char < 0)
1229 to_char += size;
1230
1231 to_byte = string_char_to_byte (string, to_char);
1232 }
1233
1234 if (!(0 <= from_char && from_char <= to_char && to_char <= size))
1235 args_out_of_range_3 (string, make_number (from_char),
1236 make_number (to_char));
1237
1238 return make_specified_string (SSDATA (string) + from_byte,
1239 to_char - from_char, to_byte - from_byte,
1240 STRING_MULTIBYTE (string));
1241 }
1242
1243 /* Extract a substring of STRING, giving start and end positions
1244 both in characters and in bytes. */
1245
1246 Lisp_Object
1247 substring_both (Lisp_Object string, EMACS_INT from, EMACS_INT from_byte,
1248 EMACS_INT to, EMACS_INT to_byte)
1249 {
1250 Lisp_Object res;
1251 EMACS_INT size;
1252 EMACS_INT size_byte;
1253
1254 CHECK_VECTOR_OR_STRING (string);
1255
1256 if (STRINGP (string))
1257 {
1258 size = SCHARS (string);
1259 size_byte = SBYTES (string);
1260 }
1261 else
1262 size = ASIZE (string);
1263
1264 if (!(0 <= from && from <= to && to <= size))
1265 args_out_of_range_3 (string, make_number (from), make_number (to));
1266
1267 if (STRINGP (string))
1268 {
1269 res = make_specified_string (SSDATA (string) + from_byte,
1270 to - from, to_byte - from_byte,
1271 STRING_MULTIBYTE (string));
1272 copy_text_properties (make_number (from), make_number (to),
1273 string, make_number (0), res, Qnil);
1274 }
1275 else
1276 res = Fvector (to - from, &AREF (string, from));
1277
1278 return res;
1279 }
1280 \f
1281 DEFUN ("nthcdr", Fnthcdr, Snthcdr, 2, 2, 0,
1282 doc: /* Take cdr N times on LIST, return the result. */)
1283 (Lisp_Object n, Lisp_Object list)
1284 {
1285 register int i, num;
1286 CHECK_NUMBER (n);
1287 num = XINT (n);
1288 for (i = 0; i < num && !NILP (list); i++)
1289 {
1290 QUIT;
1291 CHECK_LIST_CONS (list, list);
1292 list = XCDR (list);
1293 }
1294 return list;
1295 }
1296
1297 DEFUN ("nth", Fnth, Snth, 2, 2, 0,
1298 doc: /* Return the Nth element of LIST.
1299 N counts from zero. If LIST is not that long, nil is returned. */)
1300 (Lisp_Object n, Lisp_Object list)
1301 {
1302 return Fcar (Fnthcdr (n, list));
1303 }
1304
1305 DEFUN ("elt", Felt, Selt, 2, 2, 0,
1306 doc: /* Return element of SEQUENCE at index N. */)
1307 (register Lisp_Object sequence, Lisp_Object n)
1308 {
1309 CHECK_NUMBER (n);
1310 if (CONSP (sequence) || NILP (sequence))
1311 return Fcar (Fnthcdr (n, sequence));
1312
1313 /* Faref signals a "not array" error, so check here. */
1314 CHECK_ARRAY (sequence, Qsequencep);
1315 return Faref (sequence, n);
1316 }
1317
1318 DEFUN ("member", Fmember, Smember, 2, 2, 0,
1319 doc: /* Return non-nil if ELT is an element of LIST. Comparison done with `equal'.
1320 The value is actually the tail of LIST whose car is ELT. */)
1321 (register Lisp_Object elt, Lisp_Object list)
1322 {
1323 register Lisp_Object tail;
1324 for (tail = list; CONSP (tail); tail = XCDR (tail))
1325 {
1326 register Lisp_Object tem;
1327 CHECK_LIST_CONS (tail, list);
1328 tem = XCAR (tail);
1329 if (! NILP (Fequal (elt, tem)))
1330 return tail;
1331 QUIT;
1332 }
1333 return Qnil;
1334 }
1335
1336 DEFUN ("memq", Fmemq, Smemq, 2, 2, 0,
1337 doc: /* Return non-nil if ELT is an element of LIST. Comparison done with `eq'.
1338 The value is actually the tail of LIST whose car is ELT. */)
1339 (register Lisp_Object elt, Lisp_Object list)
1340 {
1341 while (1)
1342 {
1343 if (!CONSP (list) || EQ (XCAR (list), elt))
1344 break;
1345
1346 list = XCDR (list);
1347 if (!CONSP (list) || EQ (XCAR (list), elt))
1348 break;
1349
1350 list = XCDR (list);
1351 if (!CONSP (list) || EQ (XCAR (list), elt))
1352 break;
1353
1354 list = XCDR (list);
1355 QUIT;
1356 }
1357
1358 CHECK_LIST (list);
1359 return list;
1360 }
1361
1362 DEFUN ("memql", Fmemql, Smemql, 2, 2, 0,
1363 doc: /* Return non-nil if ELT is an element of LIST. Comparison done with `eql'.
1364 The value is actually the tail of LIST whose car is ELT. */)
1365 (register Lisp_Object elt, Lisp_Object list)
1366 {
1367 register Lisp_Object tail;
1368
1369 if (!FLOATP (elt))
1370 return Fmemq (elt, list);
1371
1372 for (tail = list; CONSP (tail); tail = XCDR (tail))
1373 {
1374 register Lisp_Object tem;
1375 CHECK_LIST_CONS (tail, list);
1376 tem = XCAR (tail);
1377 if (FLOATP (tem) && internal_equal (elt, tem, 0, 0))
1378 return tail;
1379 QUIT;
1380 }
1381 return Qnil;
1382 }
1383
1384 DEFUN ("assq", Fassq, Sassq, 2, 2, 0,
1385 doc: /* Return non-nil if KEY is `eq' to the car of an element of LIST.
1386 The value is actually the first element of LIST whose car is KEY.
1387 Elements of LIST that are not conses are ignored. */)
1388 (Lisp_Object key, Lisp_Object list)
1389 {
1390 while (1)
1391 {
1392 if (!CONSP (list)
1393 || (CONSP (XCAR (list))
1394 && EQ (XCAR (XCAR (list)), key)))
1395 break;
1396
1397 list = XCDR (list);
1398 if (!CONSP (list)
1399 || (CONSP (XCAR (list))
1400 && EQ (XCAR (XCAR (list)), key)))
1401 break;
1402
1403 list = XCDR (list);
1404 if (!CONSP (list)
1405 || (CONSP (XCAR (list))
1406 && EQ (XCAR (XCAR (list)), key)))
1407 break;
1408
1409 list = XCDR (list);
1410 QUIT;
1411 }
1412
1413 return CAR (list);
1414 }
1415
1416 /* Like Fassq but never report an error and do not allow quits.
1417 Use only on lists known never to be circular. */
1418
1419 Lisp_Object
1420 assq_no_quit (Lisp_Object key, Lisp_Object list)
1421 {
1422 while (CONSP (list)
1423 && (!CONSP (XCAR (list))
1424 || !EQ (XCAR (XCAR (list)), key)))
1425 list = XCDR (list);
1426
1427 return CAR_SAFE (list);
1428 }
1429
1430 DEFUN ("assoc", Fassoc, Sassoc, 2, 2, 0,
1431 doc: /* Return non-nil if KEY is `equal' to the car of an element of LIST.
1432 The value is actually the first element of LIST whose car equals KEY. */)
1433 (Lisp_Object key, Lisp_Object list)
1434 {
1435 Lisp_Object car;
1436
1437 while (1)
1438 {
1439 if (!CONSP (list)
1440 || (CONSP (XCAR (list))
1441 && (car = XCAR (XCAR (list)),
1442 EQ (car, key) || !NILP (Fequal (car, key)))))
1443 break;
1444
1445 list = XCDR (list);
1446 if (!CONSP (list)
1447 || (CONSP (XCAR (list))
1448 && (car = XCAR (XCAR (list)),
1449 EQ (car, key) || !NILP (Fequal (car, key)))))
1450 break;
1451
1452 list = XCDR (list);
1453 if (!CONSP (list)
1454 || (CONSP (XCAR (list))
1455 && (car = XCAR (XCAR (list)),
1456 EQ (car, key) || !NILP (Fequal (car, key)))))
1457 break;
1458
1459 list = XCDR (list);
1460 QUIT;
1461 }
1462
1463 return CAR (list);
1464 }
1465
1466 /* Like Fassoc but never report an error and do not allow quits.
1467 Use only on lists known never to be circular. */
1468
1469 Lisp_Object
1470 assoc_no_quit (Lisp_Object key, Lisp_Object list)
1471 {
1472 while (CONSP (list)
1473 && (!CONSP (XCAR (list))
1474 || (!EQ (XCAR (XCAR (list)), key)
1475 && NILP (Fequal (XCAR (XCAR (list)), key)))))
1476 list = XCDR (list);
1477
1478 return CONSP (list) ? XCAR (list) : Qnil;
1479 }
1480
1481 DEFUN ("rassq", Frassq, Srassq, 2, 2, 0,
1482 doc: /* Return non-nil if KEY is `eq' to the cdr of an element of LIST.
1483 The value is actually the first element of LIST whose cdr is KEY. */)
1484 (register Lisp_Object key, Lisp_Object list)
1485 {
1486 while (1)
1487 {
1488 if (!CONSP (list)
1489 || (CONSP (XCAR (list))
1490 && EQ (XCDR (XCAR (list)), key)))
1491 break;
1492
1493 list = XCDR (list);
1494 if (!CONSP (list)
1495 || (CONSP (XCAR (list))
1496 && EQ (XCDR (XCAR (list)), key)))
1497 break;
1498
1499 list = XCDR (list);
1500 if (!CONSP (list)
1501 || (CONSP (XCAR (list))
1502 && EQ (XCDR (XCAR (list)), key)))
1503 break;
1504
1505 list = XCDR (list);
1506 QUIT;
1507 }
1508
1509 return CAR (list);
1510 }
1511
1512 DEFUN ("rassoc", Frassoc, Srassoc, 2, 2, 0,
1513 doc: /* Return non-nil if KEY is `equal' to the cdr of an element of LIST.
1514 The value is actually the first element of LIST whose cdr equals KEY. */)
1515 (Lisp_Object key, Lisp_Object list)
1516 {
1517 Lisp_Object cdr;
1518
1519 while (1)
1520 {
1521 if (!CONSP (list)
1522 || (CONSP (XCAR (list))
1523 && (cdr = XCDR (XCAR (list)),
1524 EQ (cdr, key) || !NILP (Fequal (cdr, key)))))
1525 break;
1526
1527 list = XCDR (list);
1528 if (!CONSP (list)
1529 || (CONSP (XCAR (list))
1530 && (cdr = XCDR (XCAR (list)),
1531 EQ (cdr, key) || !NILP (Fequal (cdr, key)))))
1532 break;
1533
1534 list = XCDR (list);
1535 if (!CONSP (list)
1536 || (CONSP (XCAR (list))
1537 && (cdr = XCDR (XCAR (list)),
1538 EQ (cdr, key) || !NILP (Fequal (cdr, key)))))
1539 break;
1540
1541 list = XCDR (list);
1542 QUIT;
1543 }
1544
1545 return CAR (list);
1546 }
1547 \f
1548 DEFUN ("delq", Fdelq, Sdelq, 2, 2, 0,
1549 doc: /* Delete by side effect any occurrences of ELT as a member of LIST.
1550 The modified LIST is returned. Comparison is done with `eq'.
1551 If the first member of LIST is ELT, there is no way to remove it by side effect;
1552 therefore, write `(setq foo (delq element foo))'
1553 to be sure of changing the value of `foo'. */)
1554 (register Lisp_Object elt, Lisp_Object list)
1555 {
1556 register Lisp_Object tail, prev;
1557 register Lisp_Object tem;
1558
1559 tail = list;
1560 prev = Qnil;
1561 while (!NILP (tail))
1562 {
1563 CHECK_LIST_CONS (tail, list);
1564 tem = XCAR (tail);
1565 if (EQ (elt, tem))
1566 {
1567 if (NILP (prev))
1568 list = XCDR (tail);
1569 else
1570 Fsetcdr (prev, XCDR (tail));
1571 }
1572 else
1573 prev = tail;
1574 tail = XCDR (tail);
1575 QUIT;
1576 }
1577 return list;
1578 }
1579
1580 DEFUN ("delete", Fdelete, Sdelete, 2, 2, 0,
1581 doc: /* Delete by side effect any occurrences of ELT as a member of SEQ.
1582 SEQ must be a list, a vector, or a string.
1583 The modified SEQ is returned. Comparison is done with `equal'.
1584 If SEQ is not a list, or the first member of SEQ is ELT, deleting it
1585 is not a side effect; it is simply using a different sequence.
1586 Therefore, write `(setq foo (delete element foo))'
1587 to be sure of changing the value of `foo'. */)
1588 (Lisp_Object elt, Lisp_Object seq)
1589 {
1590 if (VECTORP (seq))
1591 {
1592 EMACS_INT i, n;
1593
1594 for (i = n = 0; i < ASIZE (seq); ++i)
1595 if (NILP (Fequal (AREF (seq, i), elt)))
1596 ++n;
1597
1598 if (n != ASIZE (seq))
1599 {
1600 struct Lisp_Vector *p = allocate_vector (n);
1601
1602 for (i = n = 0; i < ASIZE (seq); ++i)
1603 if (NILP (Fequal (AREF (seq, i), elt)))
1604 p->contents[n++] = AREF (seq, i);
1605
1606 XSETVECTOR (seq, p);
1607 }
1608 }
1609 else if (STRINGP (seq))
1610 {
1611 EMACS_INT i, ibyte, nchars, nbytes, cbytes;
1612 int c;
1613
1614 for (i = nchars = nbytes = ibyte = 0;
1615 i < SCHARS (seq);
1616 ++i, ibyte += cbytes)
1617 {
1618 if (STRING_MULTIBYTE (seq))
1619 {
1620 c = STRING_CHAR (SDATA (seq) + ibyte);
1621 cbytes = CHAR_BYTES (c);
1622 }
1623 else
1624 {
1625 c = SREF (seq, i);
1626 cbytes = 1;
1627 }
1628
1629 if (!INTEGERP (elt) || c != XINT (elt))
1630 {
1631 ++nchars;
1632 nbytes += cbytes;
1633 }
1634 }
1635
1636 if (nchars != SCHARS (seq))
1637 {
1638 Lisp_Object tem;
1639
1640 tem = make_uninit_multibyte_string (nchars, nbytes);
1641 if (!STRING_MULTIBYTE (seq))
1642 STRING_SET_UNIBYTE (tem);
1643
1644 for (i = nchars = nbytes = ibyte = 0;
1645 i < SCHARS (seq);
1646 ++i, ibyte += cbytes)
1647 {
1648 if (STRING_MULTIBYTE (seq))
1649 {
1650 c = STRING_CHAR (SDATA (seq) + ibyte);
1651 cbytes = CHAR_BYTES (c);
1652 }
1653 else
1654 {
1655 c = SREF (seq, i);
1656 cbytes = 1;
1657 }
1658
1659 if (!INTEGERP (elt) || c != XINT (elt))
1660 {
1661 unsigned char *from = SDATA (seq) + ibyte;
1662 unsigned char *to = SDATA (tem) + nbytes;
1663 EMACS_INT n;
1664
1665 ++nchars;
1666 nbytes += cbytes;
1667
1668 for (n = cbytes; n--; )
1669 *to++ = *from++;
1670 }
1671 }
1672
1673 seq = tem;
1674 }
1675 }
1676 else
1677 {
1678 Lisp_Object tail, prev;
1679
1680 for (tail = seq, prev = Qnil; CONSP (tail); tail = XCDR (tail))
1681 {
1682 CHECK_LIST_CONS (tail, seq);
1683
1684 if (!NILP (Fequal (elt, XCAR (tail))))
1685 {
1686 if (NILP (prev))
1687 seq = XCDR (tail);
1688 else
1689 Fsetcdr (prev, XCDR (tail));
1690 }
1691 else
1692 prev = tail;
1693 QUIT;
1694 }
1695 }
1696
1697 return seq;
1698 }
1699
1700 DEFUN ("nreverse", Fnreverse, Snreverse, 1, 1, 0,
1701 doc: /* Reverse LIST by modifying cdr pointers.
1702 Return the reversed list. */)
1703 (Lisp_Object list)
1704 {
1705 register Lisp_Object prev, tail, next;
1706
1707 if (NILP (list)) return list;
1708 prev = Qnil;
1709 tail = list;
1710 while (!NILP (tail))
1711 {
1712 QUIT;
1713 CHECK_LIST_CONS (tail, list);
1714 next = XCDR (tail);
1715 Fsetcdr (tail, prev);
1716 prev = tail;
1717 tail = next;
1718 }
1719 return prev;
1720 }
1721
1722 DEFUN ("reverse", Freverse, Sreverse, 1, 1, 0,
1723 doc: /* Reverse LIST, copying. Return the reversed list.
1724 See also the function `nreverse', which is used more often. */)
1725 (Lisp_Object list)
1726 {
1727 Lisp_Object new;
1728
1729 for (new = Qnil; CONSP (list); list = XCDR (list))
1730 {
1731 QUIT;
1732 new = Fcons (XCAR (list), new);
1733 }
1734 CHECK_LIST_END (list, list);
1735 return new;
1736 }
1737 \f
1738 Lisp_Object merge (Lisp_Object org_l1, Lisp_Object org_l2, Lisp_Object pred);
1739
1740 DEFUN ("sort", Fsort, Ssort, 2, 2, 0,
1741 doc: /* Sort LIST, stably, comparing elements using PREDICATE.
1742 Returns the sorted list. LIST is modified by side effects.
1743 PREDICATE is called with two elements of LIST, and should return non-nil
1744 if the first element should sort before the second. */)
1745 (Lisp_Object list, Lisp_Object predicate)
1746 {
1747 Lisp_Object front, back;
1748 register Lisp_Object len, tem;
1749 struct gcpro gcpro1, gcpro2;
1750 register int length;
1751
1752 front = list;
1753 len = Flength (list);
1754 length = XINT (len);
1755 if (length < 2)
1756 return list;
1757
1758 XSETINT (len, (length / 2) - 1);
1759 tem = Fnthcdr (len, list);
1760 back = Fcdr (tem);
1761 Fsetcdr (tem, Qnil);
1762
1763 GCPRO2 (front, back);
1764 front = Fsort (front, predicate);
1765 back = Fsort (back, predicate);
1766 UNGCPRO;
1767 return merge (front, back, predicate);
1768 }
1769
1770 Lisp_Object
1771 merge (Lisp_Object org_l1, Lisp_Object org_l2, Lisp_Object pred)
1772 {
1773 Lisp_Object value;
1774 register Lisp_Object tail;
1775 Lisp_Object tem;
1776 register Lisp_Object l1, l2;
1777 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1778
1779 l1 = org_l1;
1780 l2 = org_l2;
1781 tail = Qnil;
1782 value = Qnil;
1783
1784 /* It is sufficient to protect org_l1 and org_l2.
1785 When l1 and l2 are updated, we copy the new values
1786 back into the org_ vars. */
1787 GCPRO4 (org_l1, org_l2, pred, value);
1788
1789 while (1)
1790 {
1791 if (NILP (l1))
1792 {
1793 UNGCPRO;
1794 if (NILP (tail))
1795 return l2;
1796 Fsetcdr (tail, l2);
1797 return value;
1798 }
1799 if (NILP (l2))
1800 {
1801 UNGCPRO;
1802 if (NILP (tail))
1803 return l1;
1804 Fsetcdr (tail, l1);
1805 return value;
1806 }
1807 tem = call2 (pred, Fcar (l2), Fcar (l1));
1808 if (NILP (tem))
1809 {
1810 tem = l1;
1811 l1 = Fcdr (l1);
1812 org_l1 = l1;
1813 }
1814 else
1815 {
1816 tem = l2;
1817 l2 = Fcdr (l2);
1818 org_l2 = l2;
1819 }
1820 if (NILP (tail))
1821 value = tem;
1822 else
1823 Fsetcdr (tail, tem);
1824 tail = tem;
1825 }
1826 }
1827
1828 \f
1829 /* This does not check for quits. That is safe since it must terminate. */
1830
1831 DEFUN ("plist-get", Fplist_get, Splist_get, 2, 2, 0,
1832 doc: /* Extract a value from a property list.
1833 PLIST is a property list, which is a list of the form
1834 \(PROP1 VALUE1 PROP2 VALUE2...). This function returns the value
1835 corresponding to the given PROP, or nil if PROP is not one of the
1836 properties on the list. This function never signals an error. */)
1837 (Lisp_Object plist, Lisp_Object prop)
1838 {
1839 Lisp_Object tail, halftail;
1840
1841 /* halftail is used to detect circular lists. */
1842 tail = halftail = plist;
1843 while (CONSP (tail) && CONSP (XCDR (tail)))
1844 {
1845 if (EQ (prop, XCAR (tail)))
1846 return XCAR (XCDR (tail));
1847
1848 tail = XCDR (XCDR (tail));
1849 halftail = XCDR (halftail);
1850 if (EQ (tail, halftail))
1851 break;
1852
1853 #if 0 /* Unsafe version. */
1854 /* This function can be called asynchronously
1855 (setup_coding_system). Don't QUIT in that case. */
1856 if (!interrupt_input_blocked)
1857 QUIT;
1858 #endif
1859 }
1860
1861 return Qnil;
1862 }
1863
1864 DEFUN ("get", Fget, Sget, 2, 2, 0,
1865 doc: /* Return the value of SYMBOL's PROPNAME property.
1866 This is the last value stored with `(put SYMBOL PROPNAME VALUE)'. */)
1867 (Lisp_Object symbol, Lisp_Object propname)
1868 {
1869 CHECK_SYMBOL (symbol);
1870 return Fplist_get (XSYMBOL (symbol)->plist, propname);
1871 }
1872
1873 DEFUN ("plist-put", Fplist_put, Splist_put, 3, 3, 0,
1874 doc: /* Change value in PLIST of PROP to VAL.
1875 PLIST is a property list, which is a list of the form
1876 \(PROP1 VALUE1 PROP2 VALUE2 ...). PROP is a symbol and VAL is any object.
1877 If PROP is already a property on the list, its value is set to VAL,
1878 otherwise the new PROP VAL pair is added. The new plist is returned;
1879 use `(setq x (plist-put x prop val))' to be sure to use the new value.
1880 The PLIST is modified by side effects. */)
1881 (Lisp_Object plist, register Lisp_Object prop, Lisp_Object val)
1882 {
1883 register Lisp_Object tail, prev;
1884 Lisp_Object newcell;
1885 prev = Qnil;
1886 for (tail = plist; CONSP (tail) && CONSP (XCDR (tail));
1887 tail = XCDR (XCDR (tail)))
1888 {
1889 if (EQ (prop, XCAR (tail)))
1890 {
1891 Fsetcar (XCDR (tail), val);
1892 return plist;
1893 }
1894
1895 prev = tail;
1896 QUIT;
1897 }
1898 newcell = Fcons (prop, Fcons (val, NILP (prev) ? plist : XCDR (XCDR (prev))));
1899 if (NILP (prev))
1900 return newcell;
1901 else
1902 Fsetcdr (XCDR (prev), newcell);
1903 return plist;
1904 }
1905
1906 DEFUN ("put", Fput, Sput, 3, 3, 0,
1907 doc: /* Store SYMBOL's PROPNAME property with value VALUE.
1908 It can be retrieved with `(get SYMBOL PROPNAME)'. */)
1909 (Lisp_Object symbol, Lisp_Object propname, Lisp_Object value)
1910 {
1911 CHECK_SYMBOL (symbol);
1912 XSYMBOL (symbol)->plist
1913 = Fplist_put (XSYMBOL (symbol)->plist, propname, value);
1914 return value;
1915 }
1916 \f
1917 DEFUN ("lax-plist-get", Flax_plist_get, Slax_plist_get, 2, 2, 0,
1918 doc: /* Extract a value from a property list, comparing with `equal'.
1919 PLIST is a property list, which is a list of the form
1920 \(PROP1 VALUE1 PROP2 VALUE2...). This function returns the value
1921 corresponding to the given PROP, or nil if PROP is not
1922 one of the properties on the list. */)
1923 (Lisp_Object plist, Lisp_Object prop)
1924 {
1925 Lisp_Object tail;
1926
1927 for (tail = plist;
1928 CONSP (tail) && CONSP (XCDR (tail));
1929 tail = XCDR (XCDR (tail)))
1930 {
1931 if (! NILP (Fequal (prop, XCAR (tail))))
1932 return XCAR (XCDR (tail));
1933
1934 QUIT;
1935 }
1936
1937 CHECK_LIST_END (tail, prop);
1938
1939 return Qnil;
1940 }
1941
1942 DEFUN ("lax-plist-put", Flax_plist_put, Slax_plist_put, 3, 3, 0,
1943 doc: /* Change value in PLIST of PROP to VAL, comparing with `equal'.
1944 PLIST is a property list, which is a list of the form
1945 \(PROP1 VALUE1 PROP2 VALUE2 ...). PROP and VAL are any objects.
1946 If PROP is already a property on the list, its value is set to VAL,
1947 otherwise the new PROP VAL pair is added. The new plist is returned;
1948 use `(setq x (lax-plist-put x prop val))' to be sure to use the new value.
1949 The PLIST is modified by side effects. */)
1950 (Lisp_Object plist, register Lisp_Object prop, Lisp_Object val)
1951 {
1952 register Lisp_Object tail, prev;
1953 Lisp_Object newcell;
1954 prev = Qnil;
1955 for (tail = plist; CONSP (tail) && CONSP (XCDR (tail));
1956 tail = XCDR (XCDR (tail)))
1957 {
1958 if (! NILP (Fequal (prop, XCAR (tail))))
1959 {
1960 Fsetcar (XCDR (tail), val);
1961 return plist;
1962 }
1963
1964 prev = tail;
1965 QUIT;
1966 }
1967 newcell = Fcons (prop, Fcons (val, Qnil));
1968 if (NILP (prev))
1969 return newcell;
1970 else
1971 Fsetcdr (XCDR (prev), newcell);
1972 return plist;
1973 }
1974 \f
1975 DEFUN ("eql", Feql, Seql, 2, 2, 0,
1976 doc: /* Return t if the two args are the same Lisp object.
1977 Floating-point numbers of equal value are `eql', but they may not be `eq'. */)
1978 (Lisp_Object obj1, Lisp_Object obj2)
1979 {
1980 if (FLOATP (obj1))
1981 return internal_equal (obj1, obj2, 0, 0) ? Qt : Qnil;
1982 else
1983 return EQ (obj1, obj2) ? Qt : Qnil;
1984 }
1985
1986 DEFUN ("equal", Fequal, Sequal, 2, 2, 0,
1987 doc: /* Return t if two Lisp objects have similar structure and contents.
1988 They must have the same data type.
1989 Conses are compared by comparing the cars and the cdrs.
1990 Vectors and strings are compared element by element.
1991 Numbers are compared by value, but integers cannot equal floats.
1992 (Use `=' if you want integers and floats to be able to be equal.)
1993 Symbols must match exactly. */)
1994 (register Lisp_Object o1, Lisp_Object o2)
1995 {
1996 return internal_equal (o1, o2, 0, 0) ? Qt : Qnil;
1997 }
1998
1999 DEFUN ("equal-including-properties", Fequal_including_properties, Sequal_including_properties, 2, 2, 0,
2000 doc: /* Return t if two Lisp objects have similar structure and contents.
2001 This is like `equal' except that it compares the text properties
2002 of strings. (`equal' ignores text properties.) */)
2003 (register Lisp_Object o1, Lisp_Object o2)
2004 {
2005 return internal_equal (o1, o2, 0, 1) ? Qt : Qnil;
2006 }
2007
2008 /* DEPTH is current depth of recursion. Signal an error if it
2009 gets too deep.
2010 PROPS, if non-nil, means compare string text properties too. */
2011
2012 static int
2013 internal_equal (register Lisp_Object o1, register Lisp_Object o2, int depth, int props)
2014 {
2015 if (depth > 200)
2016 error ("Stack overflow in equal");
2017
2018 tail_recurse:
2019 QUIT;
2020 if (EQ (o1, o2))
2021 return 1;
2022 if (XTYPE (o1) != XTYPE (o2))
2023 return 0;
2024
2025 switch (XTYPE (o1))
2026 {
2027 case Lisp_Float:
2028 {
2029 double d1, d2;
2030
2031 d1 = extract_float (o1);
2032 d2 = extract_float (o2);
2033 /* If d is a NaN, then d != d. Two NaNs should be `equal' even
2034 though they are not =. */
2035 return d1 == d2 || (d1 != d1 && d2 != d2);
2036 }
2037
2038 case Lisp_Cons:
2039 if (!internal_equal (XCAR (o1), XCAR (o2), depth + 1, props))
2040 return 0;
2041 o1 = XCDR (o1);
2042 o2 = XCDR (o2);
2043 goto tail_recurse;
2044
2045 case Lisp_Misc:
2046 if (XMISCTYPE (o1) != XMISCTYPE (o2))
2047 return 0;
2048 if (OVERLAYP (o1))
2049 {
2050 if (!internal_equal (OVERLAY_START (o1), OVERLAY_START (o2),
2051 depth + 1, props)
2052 || !internal_equal (OVERLAY_END (o1), OVERLAY_END (o2),
2053 depth + 1, props))
2054 return 0;
2055 o1 = XOVERLAY (o1)->plist;
2056 o2 = XOVERLAY (o2)->plist;
2057 goto tail_recurse;
2058 }
2059 if (MARKERP (o1))
2060 {
2061 return (XMARKER (o1)->buffer == XMARKER (o2)->buffer
2062 && (XMARKER (o1)->buffer == 0
2063 || XMARKER (o1)->bytepos == XMARKER (o2)->bytepos));
2064 }
2065 break;
2066
2067 case Lisp_Vectorlike:
2068 {
2069 register int i;
2070 EMACS_INT size = ASIZE (o1);
2071 /* Pseudovectors have the type encoded in the size field, so this test
2072 actually checks that the objects have the same type as well as the
2073 same size. */
2074 if (ASIZE (o2) != size)
2075 return 0;
2076 /* Boolvectors are compared much like strings. */
2077 if (BOOL_VECTOR_P (o1))
2078 {
2079 int size_in_chars
2080 = ((XBOOL_VECTOR (o1)->size + BOOL_VECTOR_BITS_PER_CHAR - 1)
2081 / BOOL_VECTOR_BITS_PER_CHAR);
2082
2083 if (XBOOL_VECTOR (o1)->size != XBOOL_VECTOR (o2)->size)
2084 return 0;
2085 if (memcmp (XBOOL_VECTOR (o1)->data, XBOOL_VECTOR (o2)->data,
2086 size_in_chars))
2087 return 0;
2088 return 1;
2089 }
2090 if (WINDOW_CONFIGURATIONP (o1))
2091 return compare_window_configurations (o1, o2, 0);
2092
2093 /* Aside from them, only true vectors, char-tables, compiled
2094 functions, and fonts (font-spec, font-entity, font-ojbect)
2095 are sensible to compare, so eliminate the others now. */
2096 if (size & PSEUDOVECTOR_FLAG)
2097 {
2098 if (!(size & (PVEC_COMPILED
2099 | PVEC_CHAR_TABLE | PVEC_SUB_CHAR_TABLE | PVEC_FONT)))
2100 return 0;
2101 size &= PSEUDOVECTOR_SIZE_MASK;
2102 }
2103 for (i = 0; i < size; i++)
2104 {
2105 Lisp_Object v1, v2;
2106 v1 = AREF (o1, i);
2107 v2 = AREF (o2, i);
2108 if (!internal_equal (v1, v2, depth + 1, props))
2109 return 0;
2110 }
2111 return 1;
2112 }
2113 break;
2114
2115 case Lisp_String:
2116 if (SCHARS (o1) != SCHARS (o2))
2117 return 0;
2118 if (SBYTES (o1) != SBYTES (o2))
2119 return 0;
2120 if (memcmp (SDATA (o1), SDATA (o2), SBYTES (o1)))
2121 return 0;
2122 if (props && !compare_string_intervals (o1, o2))
2123 return 0;
2124 return 1;
2125
2126 default:
2127 break;
2128 }
2129
2130 return 0;
2131 }
2132 \f
2133
2134 DEFUN ("fillarray", Ffillarray, Sfillarray, 2, 2, 0,
2135 doc: /* Store each element of ARRAY with ITEM.
2136 ARRAY is a vector, string, char-table, or bool-vector. */)
2137 (Lisp_Object array, Lisp_Object item)
2138 {
2139 register EMACS_INT size, index;
2140 int charval;
2141
2142 if (VECTORP (array))
2143 {
2144 register Lisp_Object *p = XVECTOR (array)->contents;
2145 size = ASIZE (array);
2146 for (index = 0; index < size; index++)
2147 p[index] = item;
2148 }
2149 else if (CHAR_TABLE_P (array))
2150 {
2151 int i;
2152
2153 for (i = 0; i < (1 << CHARTAB_SIZE_BITS_0); i++)
2154 XCHAR_TABLE (array)->contents[i] = item;
2155 XCHAR_TABLE (array)->defalt = item;
2156 }
2157 else if (STRINGP (array))
2158 {
2159 register unsigned char *p = SDATA (array);
2160 CHECK_NUMBER (item);
2161 charval = XINT (item);
2162 size = SCHARS (array);
2163 if (STRING_MULTIBYTE (array))
2164 {
2165 unsigned char str[MAX_MULTIBYTE_LENGTH];
2166 int len = CHAR_STRING (charval, str);
2167 EMACS_INT size_byte = SBYTES (array);
2168 unsigned char *p1 = p, *endp = p + size_byte;
2169 int i;
2170
2171 if (size != size_byte)
2172 while (p1 < endp)
2173 {
2174 int this_len = BYTES_BY_CHAR_HEAD (*p1);
2175 if (len != this_len)
2176 error ("Attempt to change byte length of a string");
2177 p1 += this_len;
2178 }
2179 for (i = 0; i < size_byte; i++)
2180 *p++ = str[i % len];
2181 }
2182 else
2183 for (index = 0; index < size; index++)
2184 p[index] = charval;
2185 }
2186 else if (BOOL_VECTOR_P (array))
2187 {
2188 register unsigned char *p = XBOOL_VECTOR (array)->data;
2189 int size_in_chars
2190 = ((XBOOL_VECTOR (array)->size + BOOL_VECTOR_BITS_PER_CHAR - 1)
2191 / BOOL_VECTOR_BITS_PER_CHAR);
2192
2193 charval = (! NILP (item) ? -1 : 0);
2194 for (index = 0; index < size_in_chars - 1; index++)
2195 p[index] = charval;
2196 if (index < size_in_chars)
2197 {
2198 /* Mask out bits beyond the vector size. */
2199 if (XBOOL_VECTOR (array)->size % BOOL_VECTOR_BITS_PER_CHAR)
2200 charval &= (1 << (XBOOL_VECTOR (array)->size % BOOL_VECTOR_BITS_PER_CHAR)) - 1;
2201 p[index] = charval;
2202 }
2203 }
2204 else
2205 wrong_type_argument (Qarrayp, array);
2206 return array;
2207 }
2208
2209 DEFUN ("clear-string", Fclear_string, Sclear_string,
2210 1, 1, 0,
2211 doc: /* Clear the contents of STRING.
2212 This makes STRING unibyte and may change its length. */)
2213 (Lisp_Object string)
2214 {
2215 EMACS_INT len;
2216 CHECK_STRING (string);
2217 len = SBYTES (string);
2218 memset (SDATA (string), 0, len);
2219 STRING_SET_CHARS (string, len);
2220 STRING_SET_UNIBYTE (string);
2221 return Qnil;
2222 }
2223 \f
2224 /* ARGSUSED */
2225 Lisp_Object
2226 nconc2 (Lisp_Object s1, Lisp_Object s2)
2227 {
2228 Lisp_Object args[2];
2229 args[0] = s1;
2230 args[1] = s2;
2231 return Fnconc (2, args);
2232 }
2233
2234 DEFUN ("nconc", Fnconc, Snconc, 0, MANY, 0,
2235 doc: /* Concatenate any number of lists by altering them.
2236 Only the last argument is not altered, and need not be a list.
2237 usage: (nconc &rest LISTS) */)
2238 (int nargs, Lisp_Object *args)
2239 {
2240 register int argnum;
2241 register Lisp_Object tail, tem, val;
2242
2243 val = tail = Qnil;
2244
2245 for (argnum = 0; argnum < nargs; argnum++)
2246 {
2247 tem = args[argnum];
2248 if (NILP (tem)) continue;
2249
2250 if (NILP (val))
2251 val = tem;
2252
2253 if (argnum + 1 == nargs) break;
2254
2255 CHECK_LIST_CONS (tem, tem);
2256
2257 while (CONSP (tem))
2258 {
2259 tail = tem;
2260 tem = XCDR (tail);
2261 QUIT;
2262 }
2263
2264 tem = args[argnum + 1];
2265 Fsetcdr (tail, tem);
2266 if (NILP (tem))
2267 args[argnum + 1] = tail;
2268 }
2269
2270 return val;
2271 }
2272 \f
2273 /* This is the guts of all mapping functions.
2274 Apply FN to each element of SEQ, one by one,
2275 storing the results into elements of VALS, a C vector of Lisp_Objects.
2276 LENI is the length of VALS, which should also be the length of SEQ. */
2277
2278 static void
2279 mapcar1 (EMACS_INT leni, Lisp_Object *vals, Lisp_Object fn, Lisp_Object seq)
2280 {
2281 register Lisp_Object tail;
2282 Lisp_Object dummy;
2283 register EMACS_INT i;
2284 struct gcpro gcpro1, gcpro2, gcpro3;
2285
2286 if (vals)
2287 {
2288 /* Don't let vals contain any garbage when GC happens. */
2289 for (i = 0; i < leni; i++)
2290 vals[i] = Qnil;
2291
2292 GCPRO3 (dummy, fn, seq);
2293 gcpro1.var = vals;
2294 gcpro1.nvars = leni;
2295 }
2296 else
2297 GCPRO2 (fn, seq);
2298 /* We need not explicitly protect `tail' because it is used only on lists, and
2299 1) lists are not relocated and 2) the list is marked via `seq' so will not
2300 be freed */
2301
2302 if (VECTORP (seq) || COMPILEDP (seq))
2303 {
2304 for (i = 0; i < leni; i++)
2305 {
2306 dummy = call1 (fn, AREF (seq, i));
2307 if (vals)
2308 vals[i] = dummy;
2309 }
2310 }
2311 else if (BOOL_VECTOR_P (seq))
2312 {
2313 for (i = 0; i < leni; i++)
2314 {
2315 int byte;
2316 byte = XBOOL_VECTOR (seq)->data[i / BOOL_VECTOR_BITS_PER_CHAR];
2317 dummy = (byte & (1 << (i % BOOL_VECTOR_BITS_PER_CHAR))) ? Qt : Qnil;
2318 dummy = call1 (fn, dummy);
2319 if (vals)
2320 vals[i] = dummy;
2321 }
2322 }
2323 else if (STRINGP (seq))
2324 {
2325 EMACS_INT i_byte;
2326
2327 for (i = 0, i_byte = 0; i < leni;)
2328 {
2329 int c;
2330 EMACS_INT i_before = i;
2331
2332 FETCH_STRING_CHAR_ADVANCE (c, seq, i, i_byte);
2333 XSETFASTINT (dummy, c);
2334 dummy = call1 (fn, dummy);
2335 if (vals)
2336 vals[i_before] = dummy;
2337 }
2338 }
2339 else /* Must be a list, since Flength did not get an error */
2340 {
2341 tail = seq;
2342 for (i = 0; i < leni && CONSP (tail); i++)
2343 {
2344 dummy = call1 (fn, XCAR (tail));
2345 if (vals)
2346 vals[i] = dummy;
2347 tail = XCDR (tail);
2348 }
2349 }
2350
2351 UNGCPRO;
2352 }
2353
2354 DEFUN ("mapconcat", Fmapconcat, Smapconcat, 3, 3, 0,
2355 doc: /* Apply FUNCTION to each element of SEQUENCE, and concat the results as strings.
2356 In between each pair of results, stick in SEPARATOR. Thus, " " as
2357 SEPARATOR results in spaces between the values returned by FUNCTION.
2358 SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
2359 (Lisp_Object function, Lisp_Object sequence, Lisp_Object separator)
2360 {
2361 Lisp_Object len;
2362 register EMACS_INT leni;
2363 int nargs;
2364 register Lisp_Object *args;
2365 register EMACS_INT i;
2366 struct gcpro gcpro1;
2367 Lisp_Object ret;
2368 USE_SAFE_ALLOCA;
2369
2370 len = Flength (sequence);
2371 if (CHAR_TABLE_P (sequence))
2372 wrong_type_argument (Qlistp, sequence);
2373 leni = XINT (len);
2374 nargs = leni + leni - 1;
2375 if (nargs < 0) return empty_unibyte_string;
2376
2377 SAFE_ALLOCA_LISP (args, nargs);
2378
2379 GCPRO1 (separator);
2380 mapcar1 (leni, args, function, sequence);
2381 UNGCPRO;
2382
2383 for (i = leni - 1; i > 0; i--)
2384 args[i + i] = args[i];
2385
2386 for (i = 1; i < nargs; i += 2)
2387 args[i] = separator;
2388
2389 ret = Fconcat (nargs, args);
2390 SAFE_FREE ();
2391
2392 return ret;
2393 }
2394
2395 DEFUN ("mapcar", Fmapcar, Smapcar, 2, 2, 0,
2396 doc: /* Apply FUNCTION to each element of SEQUENCE, and make a list of the results.
2397 The result is a list just as long as SEQUENCE.
2398 SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
2399 (Lisp_Object function, Lisp_Object sequence)
2400 {
2401 register Lisp_Object len;
2402 register EMACS_INT leni;
2403 register Lisp_Object *args;
2404 Lisp_Object ret;
2405 USE_SAFE_ALLOCA;
2406
2407 len = Flength (sequence);
2408 if (CHAR_TABLE_P (sequence))
2409 wrong_type_argument (Qlistp, sequence);
2410 leni = XFASTINT (len);
2411
2412 SAFE_ALLOCA_LISP (args, leni);
2413
2414 mapcar1 (leni, args, function, sequence);
2415
2416 ret = Flist (leni, args);
2417 SAFE_FREE ();
2418
2419 return ret;
2420 }
2421
2422 DEFUN ("mapc", Fmapc, Smapc, 2, 2, 0,
2423 doc: /* Apply FUNCTION to each element of SEQUENCE for side effects only.
2424 Unlike `mapcar', don't accumulate the results. Return SEQUENCE.
2425 SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
2426 (Lisp_Object function, Lisp_Object sequence)
2427 {
2428 register EMACS_INT leni;
2429
2430 leni = XFASTINT (Flength (sequence));
2431 if (CHAR_TABLE_P (sequence))
2432 wrong_type_argument (Qlistp, sequence);
2433 mapcar1 (leni, 0, function, sequence);
2434
2435 return sequence;
2436 }
2437 \f
2438 /* This is how C code calls `yes-or-no-p' and allows the user
2439 to redefined it.
2440
2441 Anything that calls this function must protect from GC! */
2442
2443 Lisp_Object
2444 do_yes_or_no_p (Lisp_Object prompt)
2445 {
2446 return call1 (intern ("yes-or-no-p"), prompt);
2447 }
2448
2449 /* Anything that calls this function must protect from GC! */
2450
2451 DEFUN ("yes-or-no-p", Fyes_or_no_p, Syes_or_no_p, 1, 1, 0,
2452 doc: /* Ask user a yes-or-no question. Return t if answer is yes.
2453 PROMPT is the string to display to ask the question. It should end in
2454 a space; `yes-or-no-p' adds \"(yes or no) \" to it.
2455
2456 The user must confirm the answer with RET, and can edit it until it
2457 has been confirmed.
2458
2459 Under a windowing system a dialog box will be used if `last-nonmenu-event'
2460 is nil, and `use-dialog-box' is non-nil. */)
2461 (Lisp_Object prompt)
2462 {
2463 register Lisp_Object ans;
2464 Lisp_Object args[2];
2465 struct gcpro gcpro1;
2466
2467 CHECK_STRING (prompt);
2468
2469 #ifdef HAVE_MENUS
2470 if (FRAME_WINDOW_P (SELECTED_FRAME ())
2471 && (NILP (last_nonmenu_event) || CONSP (last_nonmenu_event))
2472 && use_dialog_box
2473 && have_menus_p ())
2474 {
2475 Lisp_Object pane, menu, obj;
2476 redisplay_preserve_echo_area (4);
2477 pane = Fcons (Fcons (build_string ("Yes"), Qt),
2478 Fcons (Fcons (build_string ("No"), Qnil),
2479 Qnil));
2480 GCPRO1 (pane);
2481 menu = Fcons (prompt, pane);
2482 obj = Fx_popup_dialog (Qt, menu, Qnil);
2483 UNGCPRO;
2484 return obj;
2485 }
2486 #endif /* HAVE_MENUS */
2487
2488 args[0] = prompt;
2489 args[1] = build_string ("(yes or no) ");
2490 prompt = Fconcat (2, args);
2491
2492 GCPRO1 (prompt);
2493
2494 while (1)
2495 {
2496 ans = Fdowncase (Fread_from_minibuffer (prompt, Qnil, Qnil, Qnil,
2497 Qyes_or_no_p_history, Qnil,
2498 Qnil));
2499 if (SCHARS (ans) == 3 && !strcmp (SSDATA (ans), "yes"))
2500 {
2501 UNGCPRO;
2502 return Qt;
2503 }
2504 if (SCHARS (ans) == 2 && !strcmp (SSDATA (ans), "no"))
2505 {
2506 UNGCPRO;
2507 return Qnil;
2508 }
2509
2510 Fding (Qnil);
2511 Fdiscard_input ();
2512 message ("Please answer yes or no.");
2513 Fsleep_for (make_number (2), Qnil);
2514 }
2515 }
2516 \f
2517 DEFUN ("load-average", Fload_average, Sload_average, 0, 1, 0,
2518 doc: /* Return list of 1 minute, 5 minute and 15 minute load averages.
2519
2520 Each of the three load averages is multiplied by 100, then converted
2521 to integer.
2522
2523 When USE-FLOATS is non-nil, floats will be used instead of integers.
2524 These floats are not multiplied by 100.
2525
2526 If the 5-minute or 15-minute load averages are not available, return a
2527 shortened list, containing only those averages which are available.
2528
2529 An error is thrown if the load average can't be obtained. In some
2530 cases making it work would require Emacs being installed setuid or
2531 setgid so that it can read kernel information, and that usually isn't
2532 advisable. */)
2533 (Lisp_Object use_floats)
2534 {
2535 double load_ave[3];
2536 int loads = getloadavg (load_ave, 3);
2537 Lisp_Object ret = Qnil;
2538
2539 if (loads < 0)
2540 error ("load-average not implemented for this operating system");
2541
2542 while (loads-- > 0)
2543 {
2544 Lisp_Object load = (NILP (use_floats) ?
2545 make_number ((int) (100.0 * load_ave[loads]))
2546 : make_float (load_ave[loads]));
2547 ret = Fcons (load, ret);
2548 }
2549
2550 return ret;
2551 }
2552 \f
2553 Lisp_Object Qsubfeatures;
2554
2555 DEFUN ("featurep", Ffeaturep, Sfeaturep, 1, 2, 0,
2556 doc: /* Return t if FEATURE is present in this Emacs.
2557
2558 Use this to conditionalize execution of lisp code based on the
2559 presence or absence of Emacs or environment extensions.
2560 Use `provide' to declare that a feature is available. This function
2561 looks at the value of the variable `features'. The optional argument
2562 SUBFEATURE can be used to check a specific subfeature of FEATURE. */)
2563 (Lisp_Object feature, Lisp_Object subfeature)
2564 {
2565 register Lisp_Object tem;
2566 CHECK_SYMBOL (feature);
2567 tem = Fmemq (feature, Vfeatures);
2568 if (!NILP (tem) && !NILP (subfeature))
2569 tem = Fmember (subfeature, Fget (feature, Qsubfeatures));
2570 return (NILP (tem)) ? Qnil : Qt;
2571 }
2572
2573 DEFUN ("provide", Fprovide, Sprovide, 1, 2, 0,
2574 doc: /* Announce that FEATURE is a feature of the current Emacs.
2575 The optional argument SUBFEATURES should be a list of symbols listing
2576 particular subfeatures supported in this version of FEATURE. */)
2577 (Lisp_Object feature, Lisp_Object subfeatures)
2578 {
2579 register Lisp_Object tem;
2580 CHECK_SYMBOL (feature);
2581 CHECK_LIST (subfeatures);
2582 if (!NILP (Vautoload_queue))
2583 Vautoload_queue = Fcons (Fcons (make_number (0), Vfeatures),
2584 Vautoload_queue);
2585 tem = Fmemq (feature, Vfeatures);
2586 if (NILP (tem))
2587 Vfeatures = Fcons (feature, Vfeatures);
2588 if (!NILP (subfeatures))
2589 Fput (feature, Qsubfeatures, subfeatures);
2590 LOADHIST_ATTACH (Fcons (Qprovide, feature));
2591
2592 /* Run any load-hooks for this file. */
2593 tem = Fassq (feature, Vafter_load_alist);
2594 if (CONSP (tem))
2595 Fprogn (XCDR (tem));
2596
2597 return feature;
2598 }
2599 \f
2600 /* `require' and its subroutines. */
2601
2602 /* List of features currently being require'd, innermost first. */
2603
2604 Lisp_Object require_nesting_list;
2605
2606 Lisp_Object
2607 require_unwind (Lisp_Object old_value)
2608 {
2609 return require_nesting_list = old_value;
2610 }
2611
2612 DEFUN ("require", Frequire, Srequire, 1, 3, 0,
2613 doc: /* If feature FEATURE is not loaded, load it from FILENAME.
2614 If FEATURE is not a member of the list `features', then the feature
2615 is not loaded; so load the file FILENAME.
2616 If FILENAME is omitted, the printname of FEATURE is used as the file name,
2617 and `load' will try to load this name appended with the suffix `.elc' or
2618 `.el', in that order. The name without appended suffix will not be used.
2619 If the optional third argument NOERROR is non-nil,
2620 then return nil if the file is not found instead of signaling an error.
2621 Normally the return value is FEATURE.
2622 The normal messages at start and end of loading FILENAME are suppressed. */)
2623 (Lisp_Object feature, Lisp_Object filename, Lisp_Object noerror)
2624 {
2625 register Lisp_Object tem;
2626 struct gcpro gcpro1, gcpro2;
2627 int from_file = load_in_progress;
2628
2629 CHECK_SYMBOL (feature);
2630
2631 /* Record the presence of `require' in this file
2632 even if the feature specified is already loaded.
2633 But not more than once in any file,
2634 and not when we aren't loading or reading from a file. */
2635 if (!from_file)
2636 for (tem = Vcurrent_load_list; CONSP (tem); tem = XCDR (tem))
2637 if (NILP (XCDR (tem)) && STRINGP (XCAR (tem)))
2638 from_file = 1;
2639
2640 if (from_file)
2641 {
2642 tem = Fcons (Qrequire, feature);
2643 if (NILP (Fmember (tem, Vcurrent_load_list)))
2644 LOADHIST_ATTACH (tem);
2645 }
2646 tem = Fmemq (feature, Vfeatures);
2647
2648 if (NILP (tem))
2649 {
2650 int count = SPECPDL_INDEX ();
2651 int nesting = 0;
2652
2653 /* This is to make sure that loadup.el gives a clear picture
2654 of what files are preloaded and when. */
2655 if (! NILP (Vpurify_flag))
2656 error ("(require %s) while preparing to dump",
2657 SDATA (SYMBOL_NAME (feature)));
2658
2659 /* A certain amount of recursive `require' is legitimate,
2660 but if we require the same feature recursively 3 times,
2661 signal an error. */
2662 tem = require_nesting_list;
2663 while (! NILP (tem))
2664 {
2665 if (! NILP (Fequal (feature, XCAR (tem))))
2666 nesting++;
2667 tem = XCDR (tem);
2668 }
2669 if (nesting > 3)
2670 error ("Recursive `require' for feature `%s'",
2671 SDATA (SYMBOL_NAME (feature)));
2672
2673 /* Update the list for any nested `require's that occur. */
2674 record_unwind_protect (require_unwind, require_nesting_list);
2675 require_nesting_list = Fcons (feature, require_nesting_list);
2676
2677 /* Value saved here is to be restored into Vautoload_queue */
2678 record_unwind_protect (un_autoload, Vautoload_queue);
2679 Vautoload_queue = Qt;
2680
2681 /* Load the file. */
2682 GCPRO2 (feature, filename);
2683 tem = Fload (NILP (filename) ? Fsymbol_name (feature) : filename,
2684 noerror, Qt, Qnil, (NILP (filename) ? Qt : Qnil));
2685 UNGCPRO;
2686
2687 /* If load failed entirely, return nil. */
2688 if (NILP (tem))
2689 return unbind_to (count, Qnil);
2690
2691 tem = Fmemq (feature, Vfeatures);
2692 if (NILP (tem))
2693 error ("Required feature `%s' was not provided",
2694 SDATA (SYMBOL_NAME (feature)));
2695
2696 /* Once loading finishes, don't undo it. */
2697 Vautoload_queue = Qt;
2698 feature = unbind_to (count, feature);
2699 }
2700
2701 return feature;
2702 }
2703 \f
2704 /* Primitives for work of the "widget" library.
2705 In an ideal world, this section would not have been necessary.
2706 However, lisp function calls being as slow as they are, it turns
2707 out that some functions in the widget library (wid-edit.el) are the
2708 bottleneck of Widget operation. Here is their translation to C,
2709 for the sole reason of efficiency. */
2710
2711 DEFUN ("plist-member", Fplist_member, Splist_member, 2, 2, 0,
2712 doc: /* Return non-nil if PLIST has the property PROP.
2713 PLIST is a property list, which is a list of the form
2714 \(PROP1 VALUE1 PROP2 VALUE2 ...\). PROP is a symbol.
2715 Unlike `plist-get', this allows you to distinguish between a missing
2716 property and a property with the value nil.
2717 The value is actually the tail of PLIST whose car is PROP. */)
2718 (Lisp_Object plist, Lisp_Object prop)
2719 {
2720 while (CONSP (plist) && !EQ (XCAR (plist), prop))
2721 {
2722 QUIT;
2723 plist = XCDR (plist);
2724 plist = CDR (plist);
2725 }
2726 return plist;
2727 }
2728
2729 DEFUN ("widget-put", Fwidget_put, Swidget_put, 3, 3, 0,
2730 doc: /* In WIDGET, set PROPERTY to VALUE.
2731 The value can later be retrieved with `widget-get'. */)
2732 (Lisp_Object widget, Lisp_Object property, Lisp_Object value)
2733 {
2734 CHECK_CONS (widget);
2735 XSETCDR (widget, Fplist_put (XCDR (widget), property, value));
2736 return value;
2737 }
2738
2739 DEFUN ("widget-get", Fwidget_get, Swidget_get, 2, 2, 0,
2740 doc: /* In WIDGET, get the value of PROPERTY.
2741 The value could either be specified when the widget was created, or
2742 later with `widget-put'. */)
2743 (Lisp_Object widget, Lisp_Object property)
2744 {
2745 Lisp_Object tmp;
2746
2747 while (1)
2748 {
2749 if (NILP (widget))
2750 return Qnil;
2751 CHECK_CONS (widget);
2752 tmp = Fplist_member (XCDR (widget), property);
2753 if (CONSP (tmp))
2754 {
2755 tmp = XCDR (tmp);
2756 return CAR (tmp);
2757 }
2758 tmp = XCAR (widget);
2759 if (NILP (tmp))
2760 return Qnil;
2761 widget = Fget (tmp, Qwidget_type);
2762 }
2763 }
2764
2765 DEFUN ("widget-apply", Fwidget_apply, Swidget_apply, 2, MANY, 0,
2766 doc: /* Apply the value of WIDGET's PROPERTY to the widget itself.
2767 ARGS are passed as extra arguments to the function.
2768 usage: (widget-apply WIDGET PROPERTY &rest ARGS) */)
2769 (int nargs, Lisp_Object *args)
2770 {
2771 /* This function can GC. */
2772 Lisp_Object newargs[3];
2773 struct gcpro gcpro1, gcpro2;
2774 Lisp_Object result;
2775
2776 newargs[0] = Fwidget_get (args[0], args[1]);
2777 newargs[1] = args[0];
2778 newargs[2] = Flist (nargs - 2, args + 2);
2779 GCPRO2 (newargs[0], newargs[2]);
2780 result = Fapply (3, newargs);
2781 UNGCPRO;
2782 return result;
2783 }
2784
2785 #ifdef HAVE_LANGINFO_CODESET
2786 #include <langinfo.h>
2787 #endif
2788
2789 DEFUN ("locale-info", Flocale_info, Slocale_info, 1, 1, 0,
2790 doc: /* Access locale data ITEM for the current C locale, if available.
2791 ITEM should be one of the following:
2792
2793 `codeset', returning the character set as a string (locale item CODESET);
2794
2795 `days', returning a 7-element vector of day names (locale items DAY_n);
2796
2797 `months', returning a 12-element vector of month names (locale items MON_n);
2798
2799 `paper', returning a list (WIDTH HEIGHT) for the default paper size,
2800 both measured in milimeters (locale items PAPER_WIDTH, PAPER_HEIGHT).
2801
2802 If the system can't provide such information through a call to
2803 `nl_langinfo', or if ITEM isn't from the list above, return nil.
2804
2805 See also Info node `(libc)Locales'.
2806
2807 The data read from the system are decoded using `locale-coding-system'. */)
2808 (Lisp_Object item)
2809 {
2810 char *str = NULL;
2811 #ifdef HAVE_LANGINFO_CODESET
2812 Lisp_Object val;
2813 if (EQ (item, Qcodeset))
2814 {
2815 str = nl_langinfo (CODESET);
2816 return build_string (str);
2817 }
2818 #ifdef DAY_1
2819 else if (EQ (item, Qdays)) /* e.g. for calendar-day-name-array */
2820 {
2821 Lisp_Object v = Fmake_vector (make_number (7), Qnil);
2822 const int days[7] = {DAY_1, DAY_2, DAY_3, DAY_4, DAY_5, DAY_6, DAY_7};
2823 int i;
2824 struct gcpro gcpro1;
2825 GCPRO1 (v);
2826 synchronize_system_time_locale ();
2827 for (i = 0; i < 7; i++)
2828 {
2829 str = nl_langinfo (days[i]);
2830 val = make_unibyte_string (str, strlen (str));
2831 /* Fixme: Is this coding system necessarily right, even if
2832 it is consistent with CODESET? If not, what to do? */
2833 Faset (v, make_number (i),
2834 code_convert_string_norecord (val, Vlocale_coding_system,
2835 0));
2836 }
2837 UNGCPRO;
2838 return v;
2839 }
2840 #endif /* DAY_1 */
2841 #ifdef MON_1
2842 else if (EQ (item, Qmonths)) /* e.g. for calendar-month-name-array */
2843 {
2844 Lisp_Object v = Fmake_vector (make_number (12), Qnil);
2845 const int months[12] = {MON_1, MON_2, MON_3, MON_4, MON_5, MON_6, MON_7,
2846 MON_8, MON_9, MON_10, MON_11, MON_12};
2847 int i;
2848 struct gcpro gcpro1;
2849 GCPRO1 (v);
2850 synchronize_system_time_locale ();
2851 for (i = 0; i < 12; i++)
2852 {
2853 str = nl_langinfo (months[i]);
2854 val = make_unibyte_string (str, strlen (str));
2855 Faset (v, make_number (i),
2856 code_convert_string_norecord (val, Vlocale_coding_system, 0));
2857 }
2858 UNGCPRO;
2859 return v;
2860 }
2861 #endif /* MON_1 */
2862 /* LC_PAPER stuff isn't defined as accessible in glibc as of 2.3.1,
2863 but is in the locale files. This could be used by ps-print. */
2864 #ifdef PAPER_WIDTH
2865 else if (EQ (item, Qpaper))
2866 {
2867 return list2 (make_number (nl_langinfo (PAPER_WIDTH)),
2868 make_number (nl_langinfo (PAPER_HEIGHT)));
2869 }
2870 #endif /* PAPER_WIDTH */
2871 #endif /* HAVE_LANGINFO_CODESET*/
2872 return Qnil;
2873 }
2874 \f
2875 /* base64 encode/decode functions (RFC 2045).
2876 Based on code from GNU recode. */
2877
2878 #define MIME_LINE_LENGTH 76
2879
2880 #define IS_ASCII(Character) \
2881 ((Character) < 128)
2882 #define IS_BASE64(Character) \
2883 (IS_ASCII (Character) && base64_char_to_value[Character] >= 0)
2884 #define IS_BASE64_IGNORABLE(Character) \
2885 ((Character) == ' ' || (Character) == '\t' || (Character) == '\n' \
2886 || (Character) == '\f' || (Character) == '\r')
2887
2888 /* Used by base64_decode_1 to retrieve a non-base64-ignorable
2889 character or return retval if there are no characters left to
2890 process. */
2891 #define READ_QUADRUPLET_BYTE(retval) \
2892 do \
2893 { \
2894 if (i == length) \
2895 { \
2896 if (nchars_return) \
2897 *nchars_return = nchars; \
2898 return (retval); \
2899 } \
2900 c = from[i++]; \
2901 } \
2902 while (IS_BASE64_IGNORABLE (c))
2903
2904 /* Table of characters coding the 64 values. */
2905 static const char base64_value_to_char[64] =
2906 {
2907 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 0- 9 */
2908 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 10-19 */
2909 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', /* 20-29 */
2910 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', /* 30-39 */
2911 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', /* 40-49 */
2912 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', /* 50-59 */
2913 '8', '9', '+', '/' /* 60-63 */
2914 };
2915
2916 /* Table of base64 values for first 128 characters. */
2917 static const short base64_char_to_value[128] =
2918 {
2919 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0- 9 */
2920 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10- 19 */
2921 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20- 29 */
2922 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 30- 39 */
2923 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, /* 40- 49 */
2924 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, /* 50- 59 */
2925 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, /* 60- 69 */
2926 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 70- 79 */
2927 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, /* 80- 89 */
2928 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, /* 90- 99 */
2929 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, /* 100-109 */
2930 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, /* 110-119 */
2931 49, 50, 51, -1, -1, -1, -1, -1 /* 120-127 */
2932 };
2933
2934 /* The following diagram shows the logical steps by which three octets
2935 get transformed into four base64 characters.
2936
2937 .--------. .--------. .--------.
2938 |aaaaaabb| |bbbbcccc| |ccdddddd|
2939 `--------' `--------' `--------'
2940 6 2 4 4 2 6
2941 .--------+--------+--------+--------.
2942 |00aaaaaa|00bbbbbb|00cccccc|00dddddd|
2943 `--------+--------+--------+--------'
2944
2945 .--------+--------+--------+--------.
2946 |AAAAAAAA|BBBBBBBB|CCCCCCCC|DDDDDDDD|
2947 `--------+--------+--------+--------'
2948
2949 The octets are divided into 6 bit chunks, which are then encoded into
2950 base64 characters. */
2951
2952
2953 static EMACS_INT base64_encode_1 (const char *, char *, EMACS_INT, int, int);
2954 static EMACS_INT base64_decode_1 (const char *, char *, EMACS_INT, int,
2955 EMACS_INT *);
2956
2957 DEFUN ("base64-encode-region", Fbase64_encode_region, Sbase64_encode_region,
2958 2, 3, "r",
2959 doc: /* Base64-encode the region between BEG and END.
2960 Return the length of the encoded text.
2961 Optional third argument NO-LINE-BREAK means do not break long lines
2962 into shorter lines. */)
2963 (Lisp_Object beg, Lisp_Object end, Lisp_Object no_line_break)
2964 {
2965 char *encoded;
2966 EMACS_INT allength, length;
2967 EMACS_INT ibeg, iend, encoded_length;
2968 EMACS_INT old_pos = PT;
2969 USE_SAFE_ALLOCA;
2970
2971 validate_region (&beg, &end);
2972
2973 ibeg = CHAR_TO_BYTE (XFASTINT (beg));
2974 iend = CHAR_TO_BYTE (XFASTINT (end));
2975 move_gap_both (XFASTINT (beg), ibeg);
2976
2977 /* We need to allocate enough room for encoding the text.
2978 We need 33 1/3% more space, plus a newline every 76
2979 characters, and then we round up. */
2980 length = iend - ibeg;
2981 allength = length + length/3 + 1;
2982 allength += allength / MIME_LINE_LENGTH + 1 + 6;
2983
2984 SAFE_ALLOCA (encoded, char *, allength);
2985 encoded_length = base64_encode_1 ((char *) BYTE_POS_ADDR (ibeg),
2986 encoded, length, NILP (no_line_break),
2987 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
2988 if (encoded_length > allength)
2989 abort ();
2990
2991 if (encoded_length < 0)
2992 {
2993 /* The encoding wasn't possible. */
2994 SAFE_FREE ();
2995 error ("Multibyte character in data for base64 encoding");
2996 }
2997
2998 /* Now we have encoded the region, so we insert the new contents
2999 and delete the old. (Insert first in order to preserve markers.) */
3000 SET_PT_BOTH (XFASTINT (beg), ibeg);
3001 insert (encoded, encoded_length);
3002 SAFE_FREE ();
3003 del_range_byte (ibeg + encoded_length, iend + encoded_length, 1);
3004
3005 /* If point was outside of the region, restore it exactly; else just
3006 move to the beginning of the region. */
3007 if (old_pos >= XFASTINT (end))
3008 old_pos += encoded_length - (XFASTINT (end) - XFASTINT (beg));
3009 else if (old_pos > XFASTINT (beg))
3010 old_pos = XFASTINT (beg);
3011 SET_PT (old_pos);
3012
3013 /* We return the length of the encoded text. */
3014 return make_number (encoded_length);
3015 }
3016
3017 DEFUN ("base64-encode-string", Fbase64_encode_string, Sbase64_encode_string,
3018 1, 2, 0,
3019 doc: /* Base64-encode STRING and return the result.
3020 Optional second argument NO-LINE-BREAK means do not break long lines
3021 into shorter lines. */)
3022 (Lisp_Object string, Lisp_Object no_line_break)
3023 {
3024 EMACS_INT allength, length, encoded_length;
3025 char *encoded;
3026 Lisp_Object encoded_string;
3027 USE_SAFE_ALLOCA;
3028
3029 CHECK_STRING (string);
3030
3031 /* We need to allocate enough room for encoding the text.
3032 We need 33 1/3% more space, plus a newline every 76
3033 characters, and then we round up. */
3034 length = SBYTES (string);
3035 allength = length + length/3 + 1;
3036 allength += allength / MIME_LINE_LENGTH + 1 + 6;
3037
3038 /* We need to allocate enough room for decoding the text. */
3039 SAFE_ALLOCA (encoded, char *, allength);
3040
3041 encoded_length = base64_encode_1 (SSDATA (string),
3042 encoded, length, NILP (no_line_break),
3043 STRING_MULTIBYTE (string));
3044 if (encoded_length > allength)
3045 abort ();
3046
3047 if (encoded_length < 0)
3048 {
3049 /* The encoding wasn't possible. */
3050 SAFE_FREE ();
3051 error ("Multibyte character in data for base64 encoding");
3052 }
3053
3054 encoded_string = make_unibyte_string (encoded, encoded_length);
3055 SAFE_FREE ();
3056
3057 return encoded_string;
3058 }
3059
3060 static EMACS_INT
3061 base64_encode_1 (const char *from, char *to, EMACS_INT length,
3062 int line_break, int multibyte)
3063 {
3064 int counter = 0;
3065 EMACS_INT i = 0;
3066 char *e = to;
3067 int c;
3068 unsigned int value;
3069 int bytes;
3070
3071 while (i < length)
3072 {
3073 if (multibyte)
3074 {
3075 c = STRING_CHAR_AND_LENGTH ((unsigned char *) from + i, bytes);
3076 if (CHAR_BYTE8_P (c))
3077 c = CHAR_TO_BYTE8 (c);
3078 else if (c >= 256)
3079 return -1;
3080 i += bytes;
3081 }
3082 else
3083 c = from[i++];
3084
3085 /* Wrap line every 76 characters. */
3086
3087 if (line_break)
3088 {
3089 if (counter < MIME_LINE_LENGTH / 4)
3090 counter++;
3091 else
3092 {
3093 *e++ = '\n';
3094 counter = 1;
3095 }
3096 }
3097
3098 /* Process first byte of a triplet. */
3099
3100 *e++ = base64_value_to_char[0x3f & c >> 2];
3101 value = (0x03 & c) << 4;
3102
3103 /* Process second byte of a triplet. */
3104
3105 if (i == length)
3106 {
3107 *e++ = base64_value_to_char[value];
3108 *e++ = '=';
3109 *e++ = '=';
3110 break;
3111 }
3112
3113 if (multibyte)
3114 {
3115 c = STRING_CHAR_AND_LENGTH ((unsigned char *) from + i, bytes);
3116 if (CHAR_BYTE8_P (c))
3117 c = CHAR_TO_BYTE8 (c);
3118 else if (c >= 256)
3119 return -1;
3120 i += bytes;
3121 }
3122 else
3123 c = from[i++];
3124
3125 *e++ = base64_value_to_char[value | (0x0f & c >> 4)];
3126 value = (0x0f & c) << 2;
3127
3128 /* Process third byte of a triplet. */
3129
3130 if (i == length)
3131 {
3132 *e++ = base64_value_to_char[value];
3133 *e++ = '=';
3134 break;
3135 }
3136
3137 if (multibyte)
3138 {
3139 c = STRING_CHAR_AND_LENGTH ((unsigned char *) from + i, bytes);
3140 if (CHAR_BYTE8_P (c))
3141 c = CHAR_TO_BYTE8 (c);
3142 else if (c >= 256)
3143 return -1;
3144 i += bytes;
3145 }
3146 else
3147 c = from[i++];
3148
3149 *e++ = base64_value_to_char[value | (0x03 & c >> 6)];
3150 *e++ = base64_value_to_char[0x3f & c];
3151 }
3152
3153 return e - to;
3154 }
3155
3156
3157 DEFUN ("base64-decode-region", Fbase64_decode_region, Sbase64_decode_region,
3158 2, 2, "r",
3159 doc: /* Base64-decode the region between BEG and END.
3160 Return the length of the decoded text.
3161 If the region can't be decoded, signal an error and don't modify the buffer. */)
3162 (Lisp_Object beg, Lisp_Object end)
3163 {
3164 EMACS_INT ibeg, iend, length, allength;
3165 char *decoded;
3166 EMACS_INT old_pos = PT;
3167 EMACS_INT decoded_length;
3168 EMACS_INT inserted_chars;
3169 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
3170 USE_SAFE_ALLOCA;
3171
3172 validate_region (&beg, &end);
3173
3174 ibeg = CHAR_TO_BYTE (XFASTINT (beg));
3175 iend = CHAR_TO_BYTE (XFASTINT (end));
3176
3177 length = iend - ibeg;
3178
3179 /* We need to allocate enough room for decoding the text. If we are
3180 working on a multibyte buffer, each decoded code may occupy at
3181 most two bytes. */
3182 allength = multibyte ? length * 2 : length;
3183 SAFE_ALLOCA (decoded, char *, allength);
3184
3185 move_gap_both (XFASTINT (beg), ibeg);
3186 decoded_length = base64_decode_1 ((char *) BYTE_POS_ADDR (ibeg),
3187 decoded, length,
3188 multibyte, &inserted_chars);
3189 if (decoded_length > allength)
3190 abort ();
3191
3192 if (decoded_length < 0)
3193 {
3194 /* The decoding wasn't possible. */
3195 SAFE_FREE ();
3196 error ("Invalid base64 data");
3197 }
3198
3199 /* Now we have decoded the region, so we insert the new contents
3200 and delete the old. (Insert first in order to preserve markers.) */
3201 TEMP_SET_PT_BOTH (XFASTINT (beg), ibeg);
3202 insert_1_both (decoded, inserted_chars, decoded_length, 0, 1, 0);
3203 SAFE_FREE ();
3204
3205 /* Delete the original text. */
3206 del_range_both (PT, PT_BYTE, XFASTINT (end) + inserted_chars,
3207 iend + decoded_length, 1);
3208
3209 /* If point was outside of the region, restore it exactly; else just
3210 move to the beginning of the region. */
3211 if (old_pos >= XFASTINT (end))
3212 old_pos += inserted_chars - (XFASTINT (end) - XFASTINT (beg));
3213 else if (old_pos > XFASTINT (beg))
3214 old_pos = XFASTINT (beg);
3215 SET_PT (old_pos > ZV ? ZV : old_pos);
3216
3217 return make_number (inserted_chars);
3218 }
3219
3220 DEFUN ("base64-decode-string", Fbase64_decode_string, Sbase64_decode_string,
3221 1, 1, 0,
3222 doc: /* Base64-decode STRING and return the result. */)
3223 (Lisp_Object string)
3224 {
3225 char *decoded;
3226 EMACS_INT length, decoded_length;
3227 Lisp_Object decoded_string;
3228 USE_SAFE_ALLOCA;
3229
3230 CHECK_STRING (string);
3231
3232 length = SBYTES (string);
3233 /* We need to allocate enough room for decoding the text. */
3234 SAFE_ALLOCA (decoded, char *, length);
3235
3236 /* The decoded result should be unibyte. */
3237 decoded_length = base64_decode_1 (SSDATA (string), decoded, length,
3238 0, NULL);
3239 if (decoded_length > length)
3240 abort ();
3241 else if (decoded_length >= 0)
3242 decoded_string = make_unibyte_string (decoded, decoded_length);
3243 else
3244 decoded_string = Qnil;
3245
3246 SAFE_FREE ();
3247 if (!STRINGP (decoded_string))
3248 error ("Invalid base64 data");
3249
3250 return decoded_string;
3251 }
3252
3253 /* Base64-decode the data at FROM of LENGHT bytes into TO. If
3254 MULTIBYTE is nonzero, the decoded result should be in multibyte
3255 form. If NCHARS_RETRUN is not NULL, store the number of produced
3256 characters in *NCHARS_RETURN. */
3257
3258 static EMACS_INT
3259 base64_decode_1 (const char *from, char *to, EMACS_INT length,
3260 int multibyte, EMACS_INT *nchars_return)
3261 {
3262 EMACS_INT i = 0; /* Used inside READ_QUADRUPLET_BYTE */
3263 char *e = to;
3264 unsigned char c;
3265 unsigned long value;
3266 EMACS_INT nchars = 0;
3267
3268 while (1)
3269 {
3270 /* Process first byte of a quadruplet. */
3271
3272 READ_QUADRUPLET_BYTE (e-to);
3273
3274 if (!IS_BASE64 (c))
3275 return -1;
3276 value = base64_char_to_value[c] << 18;
3277
3278 /* Process second byte of a quadruplet. */
3279
3280 READ_QUADRUPLET_BYTE (-1);
3281
3282 if (!IS_BASE64 (c))
3283 return -1;
3284 value |= base64_char_to_value[c] << 12;
3285
3286 c = (unsigned char) (value >> 16);
3287 if (multibyte && c >= 128)
3288 e += BYTE8_STRING (c, e);
3289 else
3290 *e++ = c;
3291 nchars++;
3292
3293 /* Process third byte of a quadruplet. */
3294
3295 READ_QUADRUPLET_BYTE (-1);
3296
3297 if (c == '=')
3298 {
3299 READ_QUADRUPLET_BYTE (-1);
3300
3301 if (c != '=')
3302 return -1;
3303 continue;
3304 }
3305
3306 if (!IS_BASE64 (c))
3307 return -1;
3308 value |= base64_char_to_value[c] << 6;
3309
3310 c = (unsigned char) (0xff & value >> 8);
3311 if (multibyte && c >= 128)
3312 e += BYTE8_STRING (c, e);
3313 else
3314 *e++ = c;
3315 nchars++;
3316
3317 /* Process fourth byte of a quadruplet. */
3318
3319 READ_QUADRUPLET_BYTE (-1);
3320
3321 if (c == '=')
3322 continue;
3323
3324 if (!IS_BASE64 (c))
3325 return -1;
3326 value |= base64_char_to_value[c];
3327
3328 c = (unsigned char) (0xff & value);
3329 if (multibyte && c >= 128)
3330 e += BYTE8_STRING (c, e);
3331 else
3332 *e++ = c;
3333 nchars++;
3334 }
3335 }
3336
3337
3338 \f
3339 /***********************************************************************
3340 ***** *****
3341 ***** Hash Tables *****
3342 ***** *****
3343 ***********************************************************************/
3344
3345 /* Implemented by gerd@gnu.org. This hash table implementation was
3346 inspired by CMUCL hash tables. */
3347
3348 /* Ideas:
3349
3350 1. For small tables, association lists are probably faster than
3351 hash tables because they have lower overhead.
3352
3353 For uses of hash tables where the O(1) behavior of table
3354 operations is not a requirement, it might therefore be a good idea
3355 not to hash. Instead, we could just do a linear search in the
3356 key_and_value vector of the hash table. This could be done
3357 if a `:linear-search t' argument is given to make-hash-table. */
3358
3359
3360 /* The list of all weak hash tables. Don't staticpro this one. */
3361
3362 struct Lisp_Hash_Table *weak_hash_tables;
3363
3364 /* Various symbols. */
3365
3366 Lisp_Object Qhash_table_p, Qeq, Qeql, Qequal, Qkey, Qvalue;
3367 Lisp_Object QCtest, QCsize, QCrehash_size, QCrehash_threshold, QCweakness;
3368 Lisp_Object Qhash_table_test, Qkey_or_value, Qkey_and_value;
3369
3370 /* Function prototypes. */
3371
3372 static struct Lisp_Hash_Table *check_hash_table (Lisp_Object);
3373 static int get_key_arg (Lisp_Object, int, Lisp_Object *, char *);
3374 static void maybe_resize_hash_table (struct Lisp_Hash_Table *);
3375 static int cmpfn_eql (struct Lisp_Hash_Table *, Lisp_Object, unsigned,
3376 Lisp_Object, unsigned);
3377 static int cmpfn_equal (struct Lisp_Hash_Table *, Lisp_Object, unsigned,
3378 Lisp_Object, unsigned);
3379 static int cmpfn_user_defined (struct Lisp_Hash_Table *, Lisp_Object,
3380 unsigned, Lisp_Object, unsigned);
3381 static unsigned hashfn_eq (struct Lisp_Hash_Table *, Lisp_Object);
3382 static unsigned hashfn_eql (struct Lisp_Hash_Table *, Lisp_Object);
3383 static unsigned hashfn_equal (struct Lisp_Hash_Table *, Lisp_Object);
3384 static unsigned hashfn_user_defined (struct Lisp_Hash_Table *,
3385 Lisp_Object);
3386 static unsigned sxhash_string (unsigned char *, int);
3387 static unsigned sxhash_list (Lisp_Object, int);
3388 static unsigned sxhash_vector (Lisp_Object, int);
3389 static unsigned sxhash_bool_vector (Lisp_Object);
3390 static int sweep_weak_table (struct Lisp_Hash_Table *, int);
3391
3392
3393 \f
3394 /***********************************************************************
3395 Utilities
3396 ***********************************************************************/
3397
3398 /* If OBJ is a Lisp hash table, return a pointer to its struct
3399 Lisp_Hash_Table. Otherwise, signal an error. */
3400
3401 static struct Lisp_Hash_Table *
3402 check_hash_table (Lisp_Object obj)
3403 {
3404 CHECK_HASH_TABLE (obj);
3405 return XHASH_TABLE (obj);
3406 }
3407
3408
3409 /* Value is the next integer I >= N, N >= 0 which is "almost" a prime
3410 number. */
3411
3412 int
3413 next_almost_prime (int n)
3414 {
3415 if (n % 2 == 0)
3416 n += 1;
3417 if (n % 3 == 0)
3418 n += 2;
3419 if (n % 7 == 0)
3420 n += 4;
3421 return n;
3422 }
3423
3424
3425 /* Find KEY in ARGS which has size NARGS. Don't consider indices for
3426 which USED[I] is non-zero. If found at index I in ARGS, set
3427 USED[I] and USED[I + 1] to 1, and return I + 1. Otherwise return
3428 -1. This function is used to extract a keyword/argument pair from
3429 a DEFUN parameter list. */
3430
3431 static int
3432 get_key_arg (Lisp_Object key, int nargs, Lisp_Object *args, char *used)
3433 {
3434 int i;
3435
3436 for (i = 0; i < nargs - 1; ++i)
3437 if (!used[i] && EQ (args[i], key))
3438 break;
3439
3440 if (i >= nargs - 1)
3441 i = -1;
3442 else
3443 {
3444 used[i++] = 1;
3445 used[i] = 1;
3446 }
3447
3448 return i;
3449 }
3450
3451
3452 /* Return a Lisp vector which has the same contents as VEC but has
3453 size NEW_SIZE, NEW_SIZE >= VEC->size. Entries in the resulting
3454 vector that are not copied from VEC are set to INIT. */
3455
3456 Lisp_Object
3457 larger_vector (Lisp_Object vec, int new_size, Lisp_Object init)
3458 {
3459 struct Lisp_Vector *v;
3460 int i, old_size;
3461
3462 xassert (VECTORP (vec));
3463 old_size = ASIZE (vec);
3464 xassert (new_size >= old_size);
3465
3466 v = allocate_vector (new_size);
3467 memcpy (v->contents, XVECTOR (vec)->contents, old_size * sizeof *v->contents);
3468 for (i = old_size; i < new_size; ++i)
3469 v->contents[i] = init;
3470 XSETVECTOR (vec, v);
3471 return vec;
3472 }
3473
3474
3475 /***********************************************************************
3476 Low-level Functions
3477 ***********************************************************************/
3478
3479 /* Compare KEY1 which has hash code HASH1 and KEY2 with hash code
3480 HASH2 in hash table H using `eql'. Value is non-zero if KEY1 and
3481 KEY2 are the same. */
3482
3483 static int
3484 cmpfn_eql (struct Lisp_Hash_Table *h, Lisp_Object key1, unsigned int hash1, Lisp_Object key2, unsigned int hash2)
3485 {
3486 return (FLOATP (key1)
3487 && FLOATP (key2)
3488 && XFLOAT_DATA (key1) == XFLOAT_DATA (key2));
3489 }
3490
3491
3492 /* Compare KEY1 which has hash code HASH1 and KEY2 with hash code
3493 HASH2 in hash table H using `equal'. Value is non-zero if KEY1 and
3494 KEY2 are the same. */
3495
3496 static int
3497 cmpfn_equal (struct Lisp_Hash_Table *h, Lisp_Object key1, unsigned int hash1, Lisp_Object key2, unsigned int hash2)
3498 {
3499 return hash1 == hash2 && !NILP (Fequal (key1, key2));
3500 }
3501
3502
3503 /* Compare KEY1 which has hash code HASH1, and KEY2 with hash code
3504 HASH2 in hash table H using H->user_cmp_function. Value is non-zero
3505 if KEY1 and KEY2 are the same. */
3506
3507 static int
3508 cmpfn_user_defined (struct Lisp_Hash_Table *h, Lisp_Object key1, unsigned int hash1, Lisp_Object key2, unsigned int hash2)
3509 {
3510 if (hash1 == hash2)
3511 {
3512 Lisp_Object args[3];
3513
3514 args[0] = h->user_cmp_function;
3515 args[1] = key1;
3516 args[2] = key2;
3517 return !NILP (Ffuncall (3, args));
3518 }
3519 else
3520 return 0;
3521 }
3522
3523
3524 /* Value is a hash code for KEY for use in hash table H which uses
3525 `eq' to compare keys. The hash code returned is guaranteed to fit
3526 in a Lisp integer. */
3527
3528 static unsigned
3529 hashfn_eq (struct Lisp_Hash_Table *h, Lisp_Object key)
3530 {
3531 unsigned hash = XUINT (key) ^ XTYPE (key);
3532 xassert ((hash & ~INTMASK) == 0);
3533 return hash;
3534 }
3535
3536
3537 /* Value is a hash code for KEY for use in hash table H which uses
3538 `eql' to compare keys. The hash code returned is guaranteed to fit
3539 in a Lisp integer. */
3540
3541 static unsigned
3542 hashfn_eql (struct Lisp_Hash_Table *h, Lisp_Object key)
3543 {
3544 unsigned hash;
3545 if (FLOATP (key))
3546 hash = sxhash (key, 0);
3547 else
3548 hash = XUINT (key) ^ XTYPE (key);
3549 xassert ((hash & ~INTMASK) == 0);
3550 return hash;
3551 }
3552
3553
3554 /* Value is a hash code for KEY for use in hash table H which uses
3555 `equal' to compare keys. The hash code returned is guaranteed to fit
3556 in a Lisp integer. */
3557
3558 static unsigned
3559 hashfn_equal (struct Lisp_Hash_Table *h, Lisp_Object key)
3560 {
3561 unsigned hash = sxhash (key, 0);
3562 xassert ((hash & ~INTMASK) == 0);
3563 return hash;
3564 }
3565
3566
3567 /* Value is a hash code for KEY for use in hash table H which uses as
3568 user-defined function to compare keys. The hash code returned is
3569 guaranteed to fit in a Lisp integer. */
3570
3571 static unsigned
3572 hashfn_user_defined (struct Lisp_Hash_Table *h, Lisp_Object key)
3573 {
3574 Lisp_Object args[2], hash;
3575
3576 args[0] = h->user_hash_function;
3577 args[1] = key;
3578 hash = Ffuncall (2, args);
3579 if (!INTEGERP (hash))
3580 signal_error ("Invalid hash code returned from user-supplied hash function", hash);
3581 return XUINT (hash);
3582 }
3583
3584
3585 /* Create and initialize a new hash table.
3586
3587 TEST specifies the test the hash table will use to compare keys.
3588 It must be either one of the predefined tests `eq', `eql' or
3589 `equal' or a symbol denoting a user-defined test named TEST with
3590 test and hash functions USER_TEST and USER_HASH.
3591
3592 Give the table initial capacity SIZE, SIZE >= 0, an integer.
3593
3594 If REHASH_SIZE is an integer, it must be > 0, and this hash table's
3595 new size when it becomes full is computed by adding REHASH_SIZE to
3596 its old size. If REHASH_SIZE is a float, it must be > 1.0, and the
3597 table's new size is computed by multiplying its old size with
3598 REHASH_SIZE.
3599
3600 REHASH_THRESHOLD must be a float <= 1.0, and > 0. The table will
3601 be resized when the ratio of (number of entries in the table) /
3602 (table size) is >= REHASH_THRESHOLD.
3603
3604 WEAK specifies the weakness of the table. If non-nil, it must be
3605 one of the symbols `key', `value', `key-or-value', or `key-and-value'. */
3606
3607 Lisp_Object
3608 make_hash_table (Lisp_Object test, Lisp_Object size, Lisp_Object rehash_size,
3609 Lisp_Object rehash_threshold, Lisp_Object weak,
3610 Lisp_Object user_test, Lisp_Object user_hash)
3611 {
3612 struct Lisp_Hash_Table *h;
3613 Lisp_Object table;
3614 int index_size, i, sz;
3615
3616 /* Preconditions. */
3617 xassert (SYMBOLP (test));
3618 xassert (INTEGERP (size) && XINT (size) >= 0);
3619 xassert ((INTEGERP (rehash_size) && XINT (rehash_size) > 0)
3620 || (FLOATP (rehash_size) && XFLOATINT (rehash_size) > 1.0));
3621 xassert (FLOATP (rehash_threshold)
3622 && XFLOATINT (rehash_threshold) > 0
3623 && XFLOATINT (rehash_threshold) <= 1.0);
3624
3625 if (XFASTINT (size) == 0)
3626 size = make_number (1);
3627
3628 /* Allocate a table and initialize it. */
3629 h = allocate_hash_table ();
3630
3631 /* Initialize hash table slots. */
3632 sz = XFASTINT (size);
3633
3634 h->test = test;
3635 if (EQ (test, Qeql))
3636 {
3637 h->cmpfn = cmpfn_eql;
3638 h->hashfn = hashfn_eql;
3639 }
3640 else if (EQ (test, Qeq))
3641 {
3642 h->cmpfn = NULL;
3643 h->hashfn = hashfn_eq;
3644 }
3645 else if (EQ (test, Qequal))
3646 {
3647 h->cmpfn = cmpfn_equal;
3648 h->hashfn = hashfn_equal;
3649 }
3650 else
3651 {
3652 h->user_cmp_function = user_test;
3653 h->user_hash_function = user_hash;
3654 h->cmpfn = cmpfn_user_defined;
3655 h->hashfn = hashfn_user_defined;
3656 }
3657
3658 h->weak = weak;
3659 h->rehash_threshold = rehash_threshold;
3660 h->rehash_size = rehash_size;
3661 h->count = 0;
3662 h->key_and_value = Fmake_vector (make_number (2 * sz), Qnil);
3663 h->hash = Fmake_vector (size, Qnil);
3664 h->next = Fmake_vector (size, Qnil);
3665 /* Cast to int here avoids losing with gcc 2.95 on Tru64/Alpha... */
3666 index_size = next_almost_prime ((int) (sz / XFLOATINT (rehash_threshold)));
3667 h->index = Fmake_vector (make_number (index_size), Qnil);
3668
3669 /* Set up the free list. */
3670 for (i = 0; i < sz - 1; ++i)
3671 HASH_NEXT (h, i) = make_number (i + 1);
3672 h->next_free = make_number (0);
3673
3674 XSET_HASH_TABLE (table, h);
3675 xassert (HASH_TABLE_P (table));
3676 xassert (XHASH_TABLE (table) == h);
3677
3678 /* Maybe add this hash table to the list of all weak hash tables. */
3679 if (NILP (h->weak))
3680 h->next_weak = NULL;
3681 else
3682 {
3683 h->next_weak = weak_hash_tables;
3684 weak_hash_tables = h;
3685 }
3686
3687 return table;
3688 }
3689
3690
3691 /* Return a copy of hash table H1. Keys and values are not copied,
3692 only the table itself is. */
3693
3694 static Lisp_Object
3695 copy_hash_table (struct Lisp_Hash_Table *h1)
3696 {
3697 Lisp_Object table;
3698 struct Lisp_Hash_Table *h2;
3699 struct Lisp_Vector *next;
3700
3701 h2 = allocate_hash_table ();
3702 next = h2->vec_next;
3703 memcpy (h2, h1, sizeof *h2);
3704 h2->vec_next = next;
3705 h2->key_and_value = Fcopy_sequence (h1->key_and_value);
3706 h2->hash = Fcopy_sequence (h1->hash);
3707 h2->next = Fcopy_sequence (h1->next);
3708 h2->index = Fcopy_sequence (h1->index);
3709 XSET_HASH_TABLE (table, h2);
3710
3711 /* Maybe add this hash table to the list of all weak hash tables. */
3712 if (!NILP (h2->weak))
3713 {
3714 h2->next_weak = weak_hash_tables;
3715 weak_hash_tables = h2;
3716 }
3717
3718 return table;
3719 }
3720
3721
3722 /* Resize hash table H if it's too full. If H cannot be resized
3723 because it's already too large, throw an error. */
3724
3725 static INLINE void
3726 maybe_resize_hash_table (struct Lisp_Hash_Table *h)
3727 {
3728 if (NILP (h->next_free))
3729 {
3730 int old_size = HASH_TABLE_SIZE (h);
3731 int i, new_size, index_size;
3732 EMACS_INT nsize;
3733
3734 if (INTEGERP (h->rehash_size))
3735 new_size = old_size + XFASTINT (h->rehash_size);
3736 else
3737 new_size = old_size * XFLOATINT (h->rehash_size);
3738 new_size = max (old_size + 1, new_size);
3739 index_size = next_almost_prime ((int)
3740 (new_size
3741 / XFLOATINT (h->rehash_threshold)));
3742 /* Assignment to EMACS_INT stops GCC whining about limited range
3743 of data type. */
3744 nsize = max (index_size, 2 * new_size);
3745 if (nsize > MOST_POSITIVE_FIXNUM)
3746 error ("Hash table too large to resize");
3747
3748 h->key_and_value = larger_vector (h->key_and_value, 2 * new_size, Qnil);
3749 h->next = larger_vector (h->next, new_size, Qnil);
3750 h->hash = larger_vector (h->hash, new_size, Qnil);
3751 h->index = Fmake_vector (make_number (index_size), Qnil);
3752
3753 /* Update the free list. Do it so that new entries are added at
3754 the end of the free list. This makes some operations like
3755 maphash faster. */
3756 for (i = old_size; i < new_size - 1; ++i)
3757 HASH_NEXT (h, i) = make_number (i + 1);
3758
3759 if (!NILP (h->next_free))
3760 {
3761 Lisp_Object last, next;
3762
3763 last = h->next_free;
3764 while (next = HASH_NEXT (h, XFASTINT (last)),
3765 !NILP (next))
3766 last = next;
3767
3768 HASH_NEXT (h, XFASTINT (last)) = make_number (old_size);
3769 }
3770 else
3771 XSETFASTINT (h->next_free, old_size);
3772
3773 /* Rehash. */
3774 for (i = 0; i < old_size; ++i)
3775 if (!NILP (HASH_HASH (h, i)))
3776 {
3777 unsigned hash_code = XUINT (HASH_HASH (h, i));
3778 int start_of_bucket = hash_code % ASIZE (h->index);
3779 HASH_NEXT (h, i) = HASH_INDEX (h, start_of_bucket);
3780 HASH_INDEX (h, start_of_bucket) = make_number (i);
3781 }
3782 }
3783 }
3784
3785
3786 /* Lookup KEY in hash table H. If HASH is non-null, return in *HASH
3787 the hash code of KEY. Value is the index of the entry in H
3788 matching KEY, or -1 if not found. */
3789
3790 int
3791 hash_lookup (struct Lisp_Hash_Table *h, Lisp_Object key, unsigned int *hash)
3792 {
3793 unsigned hash_code;
3794 int start_of_bucket;
3795 Lisp_Object idx;
3796
3797 hash_code = h->hashfn (h, key);
3798 if (hash)
3799 *hash = hash_code;
3800
3801 start_of_bucket = hash_code % ASIZE (h->index);
3802 idx = HASH_INDEX (h, start_of_bucket);
3803
3804 /* We need not gcpro idx since it's either an integer or nil. */
3805 while (!NILP (idx))
3806 {
3807 int i = XFASTINT (idx);
3808 if (EQ (key, HASH_KEY (h, i))
3809 || (h->cmpfn
3810 && h->cmpfn (h, key, hash_code,
3811 HASH_KEY (h, i), XUINT (HASH_HASH (h, i)))))
3812 break;
3813 idx = HASH_NEXT (h, i);
3814 }
3815
3816 return NILP (idx) ? -1 : XFASTINT (idx);
3817 }
3818
3819
3820 /* Put an entry into hash table H that associates KEY with VALUE.
3821 HASH is a previously computed hash code of KEY.
3822 Value is the index of the entry in H matching KEY. */
3823
3824 int
3825 hash_put (struct Lisp_Hash_Table *h, Lisp_Object key, Lisp_Object value, unsigned int hash)
3826 {
3827 int start_of_bucket, i;
3828
3829 xassert ((hash & ~INTMASK) == 0);
3830
3831 /* Increment count after resizing because resizing may fail. */
3832 maybe_resize_hash_table (h);
3833 h->count++;
3834
3835 /* Store key/value in the key_and_value vector. */
3836 i = XFASTINT (h->next_free);
3837 h->next_free = HASH_NEXT (h, i);
3838 HASH_KEY (h, i) = key;
3839 HASH_VALUE (h, i) = value;
3840
3841 /* Remember its hash code. */
3842 HASH_HASH (h, i) = make_number (hash);
3843
3844 /* Add new entry to its collision chain. */
3845 start_of_bucket = hash % ASIZE (h->index);
3846 HASH_NEXT (h, i) = HASH_INDEX (h, start_of_bucket);
3847 HASH_INDEX (h, start_of_bucket) = make_number (i);
3848 return i;
3849 }
3850
3851
3852 /* Remove the entry matching KEY from hash table H, if there is one. */
3853
3854 static void
3855 hash_remove_from_table (struct Lisp_Hash_Table *h, Lisp_Object key)
3856 {
3857 unsigned hash_code;
3858 int start_of_bucket;
3859 Lisp_Object idx, prev;
3860
3861 hash_code = h->hashfn (h, key);
3862 start_of_bucket = hash_code % ASIZE (h->index);
3863 idx = HASH_INDEX (h, start_of_bucket);
3864 prev = Qnil;
3865
3866 /* We need not gcpro idx, prev since they're either integers or nil. */
3867 while (!NILP (idx))
3868 {
3869 int i = XFASTINT (idx);
3870
3871 if (EQ (key, HASH_KEY (h, i))
3872 || (h->cmpfn
3873 && h->cmpfn (h, key, hash_code,
3874 HASH_KEY (h, i), XUINT (HASH_HASH (h, i)))))
3875 {
3876 /* Take entry out of collision chain. */
3877 if (NILP (prev))
3878 HASH_INDEX (h, start_of_bucket) = HASH_NEXT (h, i);
3879 else
3880 HASH_NEXT (h, XFASTINT (prev)) = HASH_NEXT (h, i);
3881
3882 /* Clear slots in key_and_value and add the slots to
3883 the free list. */
3884 HASH_KEY (h, i) = HASH_VALUE (h, i) = HASH_HASH (h, i) = Qnil;
3885 HASH_NEXT (h, i) = h->next_free;
3886 h->next_free = make_number (i);
3887 h->count--;
3888 xassert (h->count >= 0);
3889 break;
3890 }
3891 else
3892 {
3893 prev = idx;
3894 idx = HASH_NEXT (h, i);
3895 }
3896 }
3897 }
3898
3899
3900 /* Clear hash table H. */
3901
3902 static void
3903 hash_clear (struct Lisp_Hash_Table *h)
3904 {
3905 if (h->count > 0)
3906 {
3907 int i, size = HASH_TABLE_SIZE (h);
3908
3909 for (i = 0; i < size; ++i)
3910 {
3911 HASH_NEXT (h, i) = i < size - 1 ? make_number (i + 1) : Qnil;
3912 HASH_KEY (h, i) = Qnil;
3913 HASH_VALUE (h, i) = Qnil;
3914 HASH_HASH (h, i) = Qnil;
3915 }
3916
3917 for (i = 0; i < ASIZE (h->index); ++i)
3918 ASET (h->index, i, Qnil);
3919
3920 h->next_free = make_number (0);
3921 h->count = 0;
3922 }
3923 }
3924
3925
3926 \f
3927 /************************************************************************
3928 Weak Hash Tables
3929 ************************************************************************/
3930
3931 void
3932 init_weak_hash_tables (void)
3933 {
3934 weak_hash_tables = NULL;
3935 }
3936
3937 /* Sweep weak hash table H. REMOVE_ENTRIES_P non-zero means remove
3938 entries from the table that don't survive the current GC.
3939 REMOVE_ENTRIES_P zero means mark entries that are in use. Value is
3940 non-zero if anything was marked. */
3941
3942 static int
3943 sweep_weak_table (struct Lisp_Hash_Table *h, int remove_entries_p)
3944 {
3945 int bucket, n, marked;
3946
3947 n = ASIZE (h->index) & ~ARRAY_MARK_FLAG;
3948 marked = 0;
3949
3950 for (bucket = 0; bucket < n; ++bucket)
3951 {
3952 Lisp_Object idx, next, prev;
3953
3954 /* Follow collision chain, removing entries that
3955 don't survive this garbage collection. */
3956 prev = Qnil;
3957 for (idx = HASH_INDEX (h, bucket); !NILP (idx); idx = next)
3958 {
3959 int i = XFASTINT (idx);
3960 int key_known_to_survive_p = survives_gc_p (HASH_KEY (h, i));
3961 int value_known_to_survive_p = survives_gc_p (HASH_VALUE (h, i));
3962 int remove_p;
3963
3964 if (EQ (h->weak, Qkey))
3965 remove_p = !key_known_to_survive_p;
3966 else if (EQ (h->weak, Qvalue))
3967 remove_p = !value_known_to_survive_p;
3968 else if (EQ (h->weak, Qkey_or_value))
3969 remove_p = !(key_known_to_survive_p || value_known_to_survive_p);
3970 else if (EQ (h->weak, Qkey_and_value))
3971 remove_p = !(key_known_to_survive_p && value_known_to_survive_p);
3972 else
3973 abort ();
3974
3975 next = HASH_NEXT (h, i);
3976
3977 if (remove_entries_p)
3978 {
3979 if (remove_p)
3980 {
3981 /* Take out of collision chain. */
3982 if (NILP (prev))
3983 HASH_INDEX (h, bucket) = next;
3984 else
3985 HASH_NEXT (h, XFASTINT (prev)) = next;
3986
3987 /* Add to free list. */
3988 HASH_NEXT (h, i) = h->next_free;
3989 h->next_free = idx;
3990
3991 /* Clear key, value, and hash. */
3992 HASH_KEY (h, i) = HASH_VALUE (h, i) = Qnil;
3993 HASH_HASH (h, i) = Qnil;
3994
3995 h->count--;
3996 }
3997 else
3998 {
3999 prev = idx;
4000 }
4001 }
4002 else
4003 {
4004 if (!remove_p)
4005 {
4006 /* Make sure key and value survive. */
4007 if (!key_known_to_survive_p)
4008 {
4009 mark_object (HASH_KEY (h, i));
4010 marked = 1;
4011 }
4012
4013 if (!value_known_to_survive_p)
4014 {
4015 mark_object (HASH_VALUE (h, i));
4016 marked = 1;
4017 }
4018 }
4019 }
4020 }
4021 }
4022
4023 return marked;
4024 }
4025
4026 /* Remove elements from weak hash tables that don't survive the
4027 current garbage collection. Remove weak tables that don't survive
4028 from Vweak_hash_tables. Called from gc_sweep. */
4029
4030 void
4031 sweep_weak_hash_tables (void)
4032 {
4033 struct Lisp_Hash_Table *h, *used, *next;
4034 int marked;
4035
4036 /* Mark all keys and values that are in use. Keep on marking until
4037 there is no more change. This is necessary for cases like
4038 value-weak table A containing an entry X -> Y, where Y is used in a
4039 key-weak table B, Z -> Y. If B comes after A in the list of weak
4040 tables, X -> Y might be removed from A, although when looking at B
4041 one finds that it shouldn't. */
4042 do
4043 {
4044 marked = 0;
4045 for (h = weak_hash_tables; h; h = h->next_weak)
4046 {
4047 if (h->size & ARRAY_MARK_FLAG)
4048 marked |= sweep_weak_table (h, 0);
4049 }
4050 }
4051 while (marked);
4052
4053 /* Remove tables and entries that aren't used. */
4054 for (h = weak_hash_tables, used = NULL; h; h = next)
4055 {
4056 next = h->next_weak;
4057
4058 if (h->size & ARRAY_MARK_FLAG)
4059 {
4060 /* TABLE is marked as used. Sweep its contents. */
4061 if (h->count > 0)
4062 sweep_weak_table (h, 1);
4063
4064 /* Add table to the list of used weak hash tables. */
4065 h->next_weak = used;
4066 used = h;
4067 }
4068 }
4069
4070 weak_hash_tables = used;
4071 }
4072
4073
4074 \f
4075 /***********************************************************************
4076 Hash Code Computation
4077 ***********************************************************************/
4078
4079 /* Maximum depth up to which to dive into Lisp structures. */
4080
4081 #define SXHASH_MAX_DEPTH 3
4082
4083 /* Maximum length up to which to take list and vector elements into
4084 account. */
4085
4086 #define SXHASH_MAX_LEN 7
4087
4088 /* Combine two integers X and Y for hashing. */
4089
4090 #define SXHASH_COMBINE(X, Y) \
4091 ((((unsigned)(X) << 4) + (((unsigned)(X) >> 24) & 0x0fffffff)) \
4092 + (unsigned)(Y))
4093
4094
4095 /* Return a hash for string PTR which has length LEN. The hash
4096 code returned is guaranteed to fit in a Lisp integer. */
4097
4098 static unsigned
4099 sxhash_string (unsigned char *ptr, int len)
4100 {
4101 unsigned char *p = ptr;
4102 unsigned char *end = p + len;
4103 unsigned char c;
4104 unsigned hash = 0;
4105
4106 while (p != end)
4107 {
4108 c = *p++;
4109 if (c >= 0140)
4110 c -= 40;
4111 hash = ((hash << 4) + (hash >> 28) + c);
4112 }
4113
4114 return hash & INTMASK;
4115 }
4116
4117
4118 /* Return a hash for list LIST. DEPTH is the current depth in the
4119 list. We don't recurse deeper than SXHASH_MAX_DEPTH in it. */
4120
4121 static unsigned
4122 sxhash_list (Lisp_Object list, int depth)
4123 {
4124 unsigned hash = 0;
4125 int i;
4126
4127 if (depth < SXHASH_MAX_DEPTH)
4128 for (i = 0;
4129 CONSP (list) && i < SXHASH_MAX_LEN;
4130 list = XCDR (list), ++i)
4131 {
4132 unsigned hash2 = sxhash (XCAR (list), depth + 1);
4133 hash = SXHASH_COMBINE (hash, hash2);
4134 }
4135
4136 if (!NILP (list))
4137 {
4138 unsigned hash2 = sxhash (list, depth + 1);
4139 hash = SXHASH_COMBINE (hash, hash2);
4140 }
4141
4142 return hash;
4143 }
4144
4145
4146 /* Return a hash for vector VECTOR. DEPTH is the current depth in
4147 the Lisp structure. */
4148
4149 static unsigned
4150 sxhash_vector (Lisp_Object vec, int depth)
4151 {
4152 unsigned hash = ASIZE (vec);
4153 int i, n;
4154
4155 n = min (SXHASH_MAX_LEN, ASIZE (vec));
4156 for (i = 0; i < n; ++i)
4157 {
4158 unsigned hash2 = sxhash (AREF (vec, i), depth + 1);
4159 hash = SXHASH_COMBINE (hash, hash2);
4160 }
4161
4162 return hash;
4163 }
4164
4165
4166 /* Return a hash for bool-vector VECTOR. */
4167
4168 static unsigned
4169 sxhash_bool_vector (Lisp_Object vec)
4170 {
4171 unsigned hash = XBOOL_VECTOR (vec)->size;
4172 int i, n;
4173
4174 n = min (SXHASH_MAX_LEN, XBOOL_VECTOR (vec)->vector_size);
4175 for (i = 0; i < n; ++i)
4176 hash = SXHASH_COMBINE (hash, XBOOL_VECTOR (vec)->data[i]);
4177
4178 return hash;
4179 }
4180
4181
4182 /* Return a hash code for OBJ. DEPTH is the current depth in the Lisp
4183 structure. Value is an unsigned integer clipped to INTMASK. */
4184
4185 unsigned
4186 sxhash (Lisp_Object obj, int depth)
4187 {
4188 unsigned hash;
4189
4190 if (depth > SXHASH_MAX_DEPTH)
4191 return 0;
4192
4193 switch (XTYPE (obj))
4194 {
4195 case_Lisp_Int:
4196 hash = XUINT (obj);
4197 break;
4198
4199 case Lisp_Misc:
4200 hash = XUINT (obj);
4201 break;
4202
4203 case Lisp_Symbol:
4204 obj = SYMBOL_NAME (obj);
4205 /* Fall through. */
4206
4207 case Lisp_String:
4208 hash = sxhash_string (SDATA (obj), SCHARS (obj));
4209 break;
4210
4211 /* This can be everything from a vector to an overlay. */
4212 case Lisp_Vectorlike:
4213 if (VECTORP (obj))
4214 /* According to the CL HyperSpec, two arrays are equal only if
4215 they are `eq', except for strings and bit-vectors. In
4216 Emacs, this works differently. We have to compare element
4217 by element. */
4218 hash = sxhash_vector (obj, depth);
4219 else if (BOOL_VECTOR_P (obj))
4220 hash = sxhash_bool_vector (obj);
4221 else
4222 /* Others are `equal' if they are `eq', so let's take their
4223 address as hash. */
4224 hash = XUINT (obj);
4225 break;
4226
4227 case Lisp_Cons:
4228 hash = sxhash_list (obj, depth);
4229 break;
4230
4231 case Lisp_Float:
4232 {
4233 double val = XFLOAT_DATA (obj);
4234 unsigned char *p = (unsigned char *) &val;
4235 unsigned char *e = p + sizeof val;
4236 for (hash = 0; p < e; ++p)
4237 hash = SXHASH_COMBINE (hash, *p);
4238 break;
4239 }
4240
4241 default:
4242 abort ();
4243 }
4244
4245 return hash & INTMASK;
4246 }
4247
4248
4249 \f
4250 /***********************************************************************
4251 Lisp Interface
4252 ***********************************************************************/
4253
4254
4255 DEFUN ("sxhash", Fsxhash, Ssxhash, 1, 1, 0,
4256 doc: /* Compute a hash code for OBJ and return it as integer. */)
4257 (Lisp_Object obj)
4258 {
4259 unsigned hash = sxhash (obj, 0);
4260 return make_number (hash);
4261 }
4262
4263
4264 DEFUN ("make-hash-table", Fmake_hash_table, Smake_hash_table, 0, MANY, 0,
4265 doc: /* Create and return a new hash table.
4266
4267 Arguments are specified as keyword/argument pairs. The following
4268 arguments are defined:
4269
4270 :test TEST -- TEST must be a symbol that specifies how to compare
4271 keys. Default is `eql'. Predefined are the tests `eq', `eql', and
4272 `equal'. User-supplied test and hash functions can be specified via
4273 `define-hash-table-test'.
4274
4275 :size SIZE -- A hint as to how many elements will be put in the table.
4276 Default is 65.
4277
4278 :rehash-size REHASH-SIZE - Indicates how to expand the table when it
4279 fills up. If REHASH-SIZE is an integer, increase the size by that
4280 amount. If it is a float, it must be > 1.0, and the new size is the
4281 old size multiplied by that factor. Default is 1.5.
4282
4283 :rehash-threshold THRESHOLD -- THRESHOLD must a float > 0, and <= 1.0.
4284 Resize the hash table when the ratio (number of entries / table size)
4285 is greater than or equal to THRESHOLD. Default is 0.8.
4286
4287 :weakness WEAK -- WEAK must be one of nil, t, `key', `value',
4288 `key-or-value', or `key-and-value'. If WEAK is not nil, the table
4289 returned is a weak table. Key/value pairs are removed from a weak
4290 hash table when there are no non-weak references pointing to their
4291 key, value, one of key or value, or both key and value, depending on
4292 WEAK. WEAK t is equivalent to `key-and-value'. Default value of WEAK
4293 is nil.
4294
4295 usage: (make-hash-table &rest KEYWORD-ARGS) */)
4296 (int nargs, Lisp_Object *args)
4297 {
4298 Lisp_Object test, size, rehash_size, rehash_threshold, weak;
4299 Lisp_Object user_test, user_hash;
4300 char *used;
4301 int i;
4302
4303 /* The vector `used' is used to keep track of arguments that
4304 have been consumed. */
4305 used = (char *) alloca (nargs * sizeof *used);
4306 memset (used, 0, nargs * sizeof *used);
4307
4308 /* See if there's a `:test TEST' among the arguments. */
4309 i = get_key_arg (QCtest, nargs, args, used);
4310 test = i < 0 ? Qeql : args[i];
4311 if (!EQ (test, Qeq) && !EQ (test, Qeql) && !EQ (test, Qequal))
4312 {
4313 /* See if it is a user-defined test. */
4314 Lisp_Object prop;
4315
4316 prop = Fget (test, Qhash_table_test);
4317 if (!CONSP (prop) || !CONSP (XCDR (prop)))
4318 signal_error ("Invalid hash table test", test);
4319 user_test = XCAR (prop);
4320 user_hash = XCAR (XCDR (prop));
4321 }
4322 else
4323 user_test = user_hash = Qnil;
4324
4325 /* See if there's a `:size SIZE' argument. */
4326 i = get_key_arg (QCsize, nargs, args, used);
4327 size = i < 0 ? Qnil : args[i];
4328 if (NILP (size))
4329 size = make_number (DEFAULT_HASH_SIZE);
4330 else if (!INTEGERP (size) || XINT (size) < 0)
4331 signal_error ("Invalid hash table size", size);
4332
4333 /* Look for `:rehash-size SIZE'. */
4334 i = get_key_arg (QCrehash_size, nargs, args, used);
4335 rehash_size = i < 0 ? make_float (DEFAULT_REHASH_SIZE) : args[i];
4336 if (!NUMBERP (rehash_size)
4337 || (INTEGERP (rehash_size) && XINT (rehash_size) <= 0)
4338 || XFLOATINT (rehash_size) <= 1.0)
4339 signal_error ("Invalid hash table rehash size", rehash_size);
4340
4341 /* Look for `:rehash-threshold THRESHOLD'. */
4342 i = get_key_arg (QCrehash_threshold, nargs, args, used);
4343 rehash_threshold = i < 0 ? make_float (DEFAULT_REHASH_THRESHOLD) : args[i];
4344 if (!FLOATP (rehash_threshold)
4345 || XFLOATINT (rehash_threshold) <= 0.0
4346 || XFLOATINT (rehash_threshold) > 1.0)
4347 signal_error ("Invalid hash table rehash threshold", rehash_threshold);
4348
4349 /* Look for `:weakness WEAK'. */
4350 i = get_key_arg (QCweakness, nargs, args, used);
4351 weak = i < 0 ? Qnil : args[i];
4352 if (EQ (weak, Qt))
4353 weak = Qkey_and_value;
4354 if (!NILP (weak)
4355 && !EQ (weak, Qkey)
4356 && !EQ (weak, Qvalue)
4357 && !EQ (weak, Qkey_or_value)
4358 && !EQ (weak, Qkey_and_value))
4359 signal_error ("Invalid hash table weakness", weak);
4360
4361 /* Now, all args should have been used up, or there's a problem. */
4362 for (i = 0; i < nargs; ++i)
4363 if (!used[i])
4364 signal_error ("Invalid argument list", args[i]);
4365
4366 return make_hash_table (test, size, rehash_size, rehash_threshold, weak,
4367 user_test, user_hash);
4368 }
4369
4370
4371 DEFUN ("copy-hash-table", Fcopy_hash_table, Scopy_hash_table, 1, 1, 0,
4372 doc: /* Return a copy of hash table TABLE. */)
4373 (Lisp_Object table)
4374 {
4375 return copy_hash_table (check_hash_table (table));
4376 }
4377
4378
4379 DEFUN ("hash-table-count", Fhash_table_count, Shash_table_count, 1, 1, 0,
4380 doc: /* Return the number of elements in TABLE. */)
4381 (Lisp_Object table)
4382 {
4383 return make_number (check_hash_table (table)->count);
4384 }
4385
4386
4387 DEFUN ("hash-table-rehash-size", Fhash_table_rehash_size,
4388 Shash_table_rehash_size, 1, 1, 0,
4389 doc: /* Return the current rehash size of TABLE. */)
4390 (Lisp_Object table)
4391 {
4392 return check_hash_table (table)->rehash_size;
4393 }
4394
4395
4396 DEFUN ("hash-table-rehash-threshold", Fhash_table_rehash_threshold,
4397 Shash_table_rehash_threshold, 1, 1, 0,
4398 doc: /* Return the current rehash threshold of TABLE. */)
4399 (Lisp_Object table)
4400 {
4401 return check_hash_table (table)->rehash_threshold;
4402 }
4403
4404
4405 DEFUN ("hash-table-size", Fhash_table_size, Shash_table_size, 1, 1, 0,
4406 doc: /* Return the size of TABLE.
4407 The size can be used as an argument to `make-hash-table' to create
4408 a hash table than can hold as many elements as TABLE holds
4409 without need for resizing. */)
4410 (Lisp_Object table)
4411 {
4412 struct Lisp_Hash_Table *h = check_hash_table (table);
4413 return make_number (HASH_TABLE_SIZE (h));
4414 }
4415
4416
4417 DEFUN ("hash-table-test", Fhash_table_test, Shash_table_test, 1, 1, 0,
4418 doc: /* Return the test TABLE uses. */)
4419 (Lisp_Object table)
4420 {
4421 return check_hash_table (table)->test;
4422 }
4423
4424
4425 DEFUN ("hash-table-weakness", Fhash_table_weakness, Shash_table_weakness,
4426 1, 1, 0,
4427 doc: /* Return the weakness of TABLE. */)
4428 (Lisp_Object table)
4429 {
4430 return check_hash_table (table)->weak;
4431 }
4432
4433
4434 DEFUN ("hash-table-p", Fhash_table_p, Shash_table_p, 1, 1, 0,
4435 doc: /* Return t if OBJ is a Lisp hash table object. */)
4436 (Lisp_Object obj)
4437 {
4438 return HASH_TABLE_P (obj) ? Qt : Qnil;
4439 }
4440
4441
4442 DEFUN ("clrhash", Fclrhash, Sclrhash, 1, 1, 0,
4443 doc: /* Clear hash table TABLE and return it. */)
4444 (Lisp_Object table)
4445 {
4446 hash_clear (check_hash_table (table));
4447 /* Be compatible with XEmacs. */
4448 return table;
4449 }
4450
4451
4452 DEFUN ("gethash", Fgethash, Sgethash, 2, 3, 0,
4453 doc: /* Look up KEY in TABLE and return its associated value.
4454 If KEY is not found, return DFLT which defaults to nil. */)
4455 (Lisp_Object key, Lisp_Object table, Lisp_Object dflt)
4456 {
4457 struct Lisp_Hash_Table *h = check_hash_table (table);
4458 int i = hash_lookup (h, key, NULL);
4459 return i >= 0 ? HASH_VALUE (h, i) : dflt;
4460 }
4461
4462
4463 DEFUN ("puthash", Fputhash, Sputhash, 3, 3, 0,
4464 doc: /* Associate KEY with VALUE in hash table TABLE.
4465 If KEY is already present in table, replace its current value with
4466 VALUE. */)
4467 (Lisp_Object key, Lisp_Object value, Lisp_Object table)
4468 {
4469 struct Lisp_Hash_Table *h = check_hash_table (table);
4470 int i;
4471 unsigned hash;
4472
4473 i = hash_lookup (h, key, &hash);
4474 if (i >= 0)
4475 HASH_VALUE (h, i) = value;
4476 else
4477 hash_put (h, key, value, hash);
4478
4479 return value;
4480 }
4481
4482
4483 DEFUN ("remhash", Fremhash, Sremhash, 2, 2, 0,
4484 doc: /* Remove KEY from TABLE. */)
4485 (Lisp_Object key, Lisp_Object table)
4486 {
4487 struct Lisp_Hash_Table *h = check_hash_table (table);
4488 hash_remove_from_table (h, key);
4489 return Qnil;
4490 }
4491
4492
4493 DEFUN ("maphash", Fmaphash, Smaphash, 2, 2, 0,
4494 doc: /* Call FUNCTION for all entries in hash table TABLE.
4495 FUNCTION is called with two arguments, KEY and VALUE. */)
4496 (Lisp_Object function, Lisp_Object table)
4497 {
4498 struct Lisp_Hash_Table *h = check_hash_table (table);
4499 Lisp_Object args[3];
4500 int i;
4501
4502 for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
4503 if (!NILP (HASH_HASH (h, i)))
4504 {
4505 args[0] = function;
4506 args[1] = HASH_KEY (h, i);
4507 args[2] = HASH_VALUE (h, i);
4508 Ffuncall (3, args);
4509 }
4510
4511 return Qnil;
4512 }
4513
4514
4515 DEFUN ("define-hash-table-test", Fdefine_hash_table_test,
4516 Sdefine_hash_table_test, 3, 3, 0,
4517 doc: /* Define a new hash table test with name NAME, a symbol.
4518
4519 In hash tables created with NAME specified as test, use TEST to
4520 compare keys, and HASH for computing hash codes of keys.
4521
4522 TEST must be a function taking two arguments and returning non-nil if
4523 both arguments are the same. HASH must be a function taking one
4524 argument and return an integer that is the hash code of the argument.
4525 Hash code computation should use the whole value range of integers,
4526 including negative integers. */)
4527 (Lisp_Object name, Lisp_Object test, Lisp_Object hash)
4528 {
4529 return Fput (name, Qhash_table_test, list2 (test, hash));
4530 }
4531
4532
4533 \f
4534 /************************************************************************
4535 MD5
4536 ************************************************************************/
4537
4538 #include "md5.h"
4539
4540 DEFUN ("md5", Fmd5, Smd5, 1, 5, 0,
4541 doc: /* Return MD5 message digest of OBJECT, a buffer or string.
4542
4543 A message digest is a cryptographic checksum of a document, and the
4544 algorithm to calculate it is defined in RFC 1321.
4545
4546 The two optional arguments START and END are character positions
4547 specifying for which part of OBJECT the message digest should be
4548 computed. If nil or omitted, the digest is computed for the whole
4549 OBJECT.
4550
4551 The MD5 message digest is computed from the result of encoding the
4552 text in a coding system, not directly from the internal Emacs form of
4553 the text. The optional fourth argument CODING-SYSTEM specifies which
4554 coding system to encode the text with. It should be the same coding
4555 system that you used or will use when actually writing the text into a
4556 file.
4557
4558 If CODING-SYSTEM is nil or omitted, the default depends on OBJECT. If
4559 OBJECT is a buffer, the default for CODING-SYSTEM is whatever coding
4560 system would be chosen by default for writing this text into a file.
4561
4562 If OBJECT is a string, the most preferred coding system (see the
4563 command `prefer-coding-system') is used.
4564
4565 If NOERROR is non-nil, silently assume the `raw-text' coding if the
4566 guesswork fails. Normally, an error is signaled in such case. */)
4567 (Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object noerror)
4568 {
4569 unsigned char digest[16];
4570 char value[33];
4571 int i;
4572 EMACS_INT size;
4573 EMACS_INT size_byte = 0;
4574 EMACS_INT start_char = 0, end_char = 0;
4575 EMACS_INT start_byte = 0, end_byte = 0;
4576 register EMACS_INT b, e;
4577 register struct buffer *bp;
4578 EMACS_INT temp;
4579
4580 if (STRINGP (object))
4581 {
4582 if (NILP (coding_system))
4583 {
4584 /* Decide the coding-system to encode the data with. */
4585
4586 if (STRING_MULTIBYTE (object))
4587 /* use default, we can't guess correct value */
4588 coding_system = preferred_coding_system ();
4589 else
4590 coding_system = Qraw_text;
4591 }
4592
4593 if (NILP (Fcoding_system_p (coding_system)))
4594 {
4595 /* Invalid coding system. */
4596
4597 if (!NILP (noerror))
4598 coding_system = Qraw_text;
4599 else
4600 xsignal1 (Qcoding_system_error, coding_system);
4601 }
4602
4603 if (STRING_MULTIBYTE (object))
4604 object = code_convert_string (object, coding_system, Qnil, 1, 0, 1);
4605
4606 size = SCHARS (object);
4607 size_byte = SBYTES (object);
4608
4609 if (!NILP (start))
4610 {
4611 CHECK_NUMBER (start);
4612
4613 start_char = XINT (start);
4614
4615 if (start_char < 0)
4616 start_char += size;
4617
4618 start_byte = string_char_to_byte (object, start_char);
4619 }
4620
4621 if (NILP (end))
4622 {
4623 end_char = size;
4624 end_byte = size_byte;
4625 }
4626 else
4627 {
4628 CHECK_NUMBER (end);
4629
4630 end_char = XINT (end);
4631
4632 if (end_char < 0)
4633 end_char += size;
4634
4635 end_byte = string_char_to_byte (object, end_char);
4636 }
4637
4638 if (!(0 <= start_char && start_char <= end_char && end_char <= size))
4639 args_out_of_range_3 (object, make_number (start_char),
4640 make_number (end_char));
4641 }
4642 else
4643 {
4644 struct buffer *prev = current_buffer;
4645
4646 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
4647
4648 CHECK_BUFFER (object);
4649
4650 bp = XBUFFER (object);
4651 if (bp != current_buffer)
4652 set_buffer_internal (bp);
4653
4654 if (NILP (start))
4655 b = BEGV;
4656 else
4657 {
4658 CHECK_NUMBER_COERCE_MARKER (start);
4659 b = XINT (start);
4660 }
4661
4662 if (NILP (end))
4663 e = ZV;
4664 else
4665 {
4666 CHECK_NUMBER_COERCE_MARKER (end);
4667 e = XINT (end);
4668 }
4669
4670 if (b > e)
4671 temp = b, b = e, e = temp;
4672
4673 if (!(BEGV <= b && e <= ZV))
4674 args_out_of_range (start, end);
4675
4676 if (NILP (coding_system))
4677 {
4678 /* Decide the coding-system to encode the data with.
4679 See fileio.c:Fwrite-region */
4680
4681 if (!NILP (Vcoding_system_for_write))
4682 coding_system = Vcoding_system_for_write;
4683 else
4684 {
4685 int force_raw_text = 0;
4686
4687 coding_system = BVAR (XBUFFER (object), buffer_file_coding_system);
4688 if (NILP (coding_system)
4689 || NILP (Flocal_variable_p (Qbuffer_file_coding_system, Qnil)))
4690 {
4691 coding_system = Qnil;
4692 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
4693 force_raw_text = 1;
4694 }
4695
4696 if (NILP (coding_system) && !NILP (Fbuffer_file_name(object)))
4697 {
4698 /* Check file-coding-system-alist. */
4699 Lisp_Object args[4], val;
4700
4701 args[0] = Qwrite_region; args[1] = start; args[2] = end;
4702 args[3] = Fbuffer_file_name(object);
4703 val = Ffind_operation_coding_system (4, args);
4704 if (CONSP (val) && !NILP (XCDR (val)))
4705 coding_system = XCDR (val);
4706 }
4707
4708 if (NILP (coding_system)
4709 && !NILP (BVAR (XBUFFER (object), buffer_file_coding_system)))
4710 {
4711 /* If we still have not decided a coding system, use the
4712 default value of buffer-file-coding-system. */
4713 coding_system = BVAR (XBUFFER (object), buffer_file_coding_system);
4714 }
4715
4716 if (!force_raw_text
4717 && !NILP (Ffboundp (Vselect_safe_coding_system_function)))
4718 /* Confirm that VAL can surely encode the current region. */
4719 coding_system = call4 (Vselect_safe_coding_system_function,
4720 make_number (b), make_number (e),
4721 coding_system, Qnil);
4722
4723 if (force_raw_text)
4724 coding_system = Qraw_text;
4725 }
4726
4727 if (NILP (Fcoding_system_p (coding_system)))
4728 {
4729 /* Invalid coding system. */
4730
4731 if (!NILP (noerror))
4732 coding_system = Qraw_text;
4733 else
4734 xsignal1 (Qcoding_system_error, coding_system);
4735 }
4736 }
4737
4738 object = make_buffer_string (b, e, 0);
4739 if (prev != current_buffer)
4740 set_buffer_internal (prev);
4741 /* Discard the unwind protect for recovering the current
4742 buffer. */
4743 specpdl_ptr--;
4744
4745 if (STRING_MULTIBYTE (object))
4746 object = code_convert_string (object, coding_system, Qnil, 1, 0, 0);
4747 }
4748
4749 md5_buffer (SSDATA (object) + start_byte,
4750 SBYTES (object) - (size_byte - end_byte),
4751 digest);
4752
4753 for (i = 0; i < 16; i++)
4754 sprintf (&value[2 * i], "%02x", digest[i]);
4755 value[32] = '\0';
4756
4757 return make_string (value, 32);
4758 }
4759
4760 \f
4761 void
4762 syms_of_fns (void)
4763 {
4764 /* Hash table stuff. */
4765 Qhash_table_p = intern_c_string ("hash-table-p");
4766 staticpro (&Qhash_table_p);
4767 Qeq = intern_c_string ("eq");
4768 staticpro (&Qeq);
4769 Qeql = intern_c_string ("eql");
4770 staticpro (&Qeql);
4771 Qequal = intern_c_string ("equal");
4772 staticpro (&Qequal);
4773 QCtest = intern_c_string (":test");
4774 staticpro (&QCtest);
4775 QCsize = intern_c_string (":size");
4776 staticpro (&QCsize);
4777 QCrehash_size = intern_c_string (":rehash-size");
4778 staticpro (&QCrehash_size);
4779 QCrehash_threshold = intern_c_string (":rehash-threshold");
4780 staticpro (&QCrehash_threshold);
4781 QCweakness = intern_c_string (":weakness");
4782 staticpro (&QCweakness);
4783 Qkey = intern_c_string ("key");
4784 staticpro (&Qkey);
4785 Qvalue = intern_c_string ("value");
4786 staticpro (&Qvalue);
4787 Qhash_table_test = intern_c_string ("hash-table-test");
4788 staticpro (&Qhash_table_test);
4789 Qkey_or_value = intern_c_string ("key-or-value");
4790 staticpro (&Qkey_or_value);
4791 Qkey_and_value = intern_c_string ("key-and-value");
4792 staticpro (&Qkey_and_value);
4793
4794 defsubr (&Ssxhash);
4795 defsubr (&Smake_hash_table);
4796 defsubr (&Scopy_hash_table);
4797 defsubr (&Shash_table_count);
4798 defsubr (&Shash_table_rehash_size);
4799 defsubr (&Shash_table_rehash_threshold);
4800 defsubr (&Shash_table_size);
4801 defsubr (&Shash_table_test);
4802 defsubr (&Shash_table_weakness);
4803 defsubr (&Shash_table_p);
4804 defsubr (&Sclrhash);
4805 defsubr (&Sgethash);
4806 defsubr (&Sputhash);
4807 defsubr (&Sremhash);
4808 defsubr (&Smaphash);
4809 defsubr (&Sdefine_hash_table_test);
4810
4811 Qstring_lessp = intern_c_string ("string-lessp");
4812 staticpro (&Qstring_lessp);
4813 Qprovide = intern_c_string ("provide");
4814 staticpro (&Qprovide);
4815 Qrequire = intern_c_string ("require");
4816 staticpro (&Qrequire);
4817 Qyes_or_no_p_history = intern_c_string ("yes-or-no-p-history");
4818 staticpro (&Qyes_or_no_p_history);
4819 Qcursor_in_echo_area = intern_c_string ("cursor-in-echo-area");
4820 staticpro (&Qcursor_in_echo_area);
4821 Qwidget_type = intern_c_string ("widget-type");
4822 staticpro (&Qwidget_type);
4823
4824 staticpro (&string_char_byte_cache_string);
4825 string_char_byte_cache_string = Qnil;
4826
4827 require_nesting_list = Qnil;
4828 staticpro (&require_nesting_list);
4829
4830 Fset (Qyes_or_no_p_history, Qnil);
4831
4832 DEFVAR_LISP ("features", Vfeatures,
4833 doc: /* A list of symbols which are the features of the executing Emacs.
4834 Used by `featurep' and `require', and altered by `provide'. */);
4835 Vfeatures = Fcons (intern_c_string ("emacs"), Qnil);
4836 Qsubfeatures = intern_c_string ("subfeatures");
4837 staticpro (&Qsubfeatures);
4838
4839 #ifdef HAVE_LANGINFO_CODESET
4840 Qcodeset = intern_c_string ("codeset");
4841 staticpro (&Qcodeset);
4842 Qdays = intern_c_string ("days");
4843 staticpro (&Qdays);
4844 Qmonths = intern_c_string ("months");
4845 staticpro (&Qmonths);
4846 Qpaper = intern_c_string ("paper");
4847 staticpro (&Qpaper);
4848 #endif /* HAVE_LANGINFO_CODESET */
4849
4850 DEFVAR_BOOL ("use-dialog-box", use_dialog_box,
4851 doc: /* *Non-nil means mouse commands use dialog boxes to ask questions.
4852 This applies to `y-or-n-p' and `yes-or-no-p' questions asked by commands
4853 invoked by mouse clicks and mouse menu items.
4854
4855 On some platforms, file selection dialogs are also enabled if this is
4856 non-nil. */);
4857 use_dialog_box = 1;
4858
4859 DEFVAR_BOOL ("use-file-dialog", use_file_dialog,
4860 doc: /* *Non-nil means mouse commands use a file dialog to ask for files.
4861 This applies to commands from menus and tool bar buttons even when
4862 they are initiated from the keyboard. If `use-dialog-box' is nil,
4863 that disables the use of a file dialog, regardless of the value of
4864 this variable. */);
4865 use_file_dialog = 1;
4866
4867 defsubr (&Sidentity);
4868 defsubr (&Srandom);
4869 defsubr (&Slength);
4870 defsubr (&Ssafe_length);
4871 defsubr (&Sstring_bytes);
4872 defsubr (&Sstring_equal);
4873 defsubr (&Scompare_strings);
4874 defsubr (&Sstring_lessp);
4875 defsubr (&Sappend);
4876 defsubr (&Sconcat);
4877 defsubr (&Svconcat);
4878 defsubr (&Scopy_sequence);
4879 defsubr (&Sstring_make_multibyte);
4880 defsubr (&Sstring_make_unibyte);
4881 defsubr (&Sstring_as_multibyte);
4882 defsubr (&Sstring_as_unibyte);
4883 defsubr (&Sstring_to_multibyte);
4884 defsubr (&Sstring_to_unibyte);
4885 defsubr (&Scopy_alist);
4886 defsubr (&Ssubstring);
4887 defsubr (&Ssubstring_no_properties);
4888 defsubr (&Snthcdr);
4889 defsubr (&Snth);
4890 defsubr (&Selt);
4891 defsubr (&Smember);
4892 defsubr (&Smemq);
4893 defsubr (&Smemql);
4894 defsubr (&Sassq);
4895 defsubr (&Sassoc);
4896 defsubr (&Srassq);
4897 defsubr (&Srassoc);
4898 defsubr (&Sdelq);
4899 defsubr (&Sdelete);
4900 defsubr (&Snreverse);
4901 defsubr (&Sreverse);
4902 defsubr (&Ssort);
4903 defsubr (&Splist_get);
4904 defsubr (&Sget);
4905 defsubr (&Splist_put);
4906 defsubr (&Sput);
4907 defsubr (&Slax_plist_get);
4908 defsubr (&Slax_plist_put);
4909 defsubr (&Seql);
4910 defsubr (&Sequal);
4911 defsubr (&Sequal_including_properties);
4912 defsubr (&Sfillarray);
4913 defsubr (&Sclear_string);
4914 defsubr (&Snconc);
4915 defsubr (&Smapcar);
4916 defsubr (&Smapc);
4917 defsubr (&Smapconcat);
4918 defsubr (&Syes_or_no_p);
4919 defsubr (&Sload_average);
4920 defsubr (&Sfeaturep);
4921 defsubr (&Srequire);
4922 defsubr (&Sprovide);
4923 defsubr (&Splist_member);
4924 defsubr (&Swidget_put);
4925 defsubr (&Swidget_get);
4926 defsubr (&Swidget_apply);
4927 defsubr (&Sbase64_encode_region);
4928 defsubr (&Sbase64_decode_region);
4929 defsubr (&Sbase64_encode_string);
4930 defsubr (&Sbase64_decode_string);
4931 defsubr (&Smd5);
4932 defsubr (&Slocale_info);
4933 }
4934
4935
4936 void
4937 init_fns (void)
4938 {
4939 }