Merge from gnus--devo--0
[emacs.git] / src / fns.c
blob7511eacb03a99fd0a4e24a6465ff68fa5d7e7309
1 /* Random utility Lisp functions.
2 Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
23 #ifdef HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26 #include <time.h>
28 #ifndef MAC_OS
29 /* On Mac OS, defining this conflicts with precompiled headers. */
31 /* Note on some machines this defines `vector' as a typedef,
32 so make sure we don't use that name in this file. */
33 #undef vector
34 #define vector *****
36 #endif /* ! MAC_OSX */
38 #include "lisp.h"
39 #include "commands.h"
40 #include "character.h"
41 #include "coding.h"
42 #include "buffer.h"
43 #include "keyboard.h"
44 #include "keymap.h"
45 #include "intervals.h"
46 #include "frame.h"
47 #include "window.h"
48 #include "blockinput.h"
49 #ifdef HAVE_MENUS
50 #if defined (HAVE_X_WINDOWS)
51 #include "xterm.h"
52 #elif defined (MAC_OS)
53 #include "macterm.h"
54 #endif
55 #endif
57 #ifndef NULL
58 #define NULL ((POINTER_TYPE *)0)
59 #endif
61 /* Nonzero enables use of dialog boxes for questions
62 asked by mouse commands. */
63 int use_dialog_box;
65 /* Nonzero enables use of a file dialog for file name
66 questions asked by mouse commands. */
67 int use_file_dialog;
69 extern int minibuffer_auto_raise;
70 extern Lisp_Object minibuf_window;
71 extern Lisp_Object Vlocale_coding_system;
72 extern int load_in_progress;
74 Lisp_Object Qstring_lessp, Qprovide, Qrequire;
75 Lisp_Object Qyes_or_no_p_history;
76 Lisp_Object Qcursor_in_echo_area;
77 Lisp_Object Qwidget_type;
78 Lisp_Object Qcodeset, Qdays, Qmonths, Qpaper;
80 extern Lisp_Object Qinput_method_function;
82 static int internal_equal P_ ((Lisp_Object , Lisp_Object, int, int));
84 extern long get_random ();
85 extern void seed_random P_ ((long));
87 #ifndef HAVE_UNISTD_H
88 extern long time ();
89 #endif
91 DEFUN ("identity", Fidentity, Sidentity, 1, 1, 0,
92 doc: /* Return the argument unchanged. */)
93 (arg)
94 Lisp_Object arg;
96 return arg;
99 DEFUN ("random", Frandom, Srandom, 0, 1, 0,
100 doc: /* Return a pseudo-random number.
101 All integers representable in Lisp are equally likely.
102 On most systems, this is 29 bits' worth.
103 With positive integer argument N, return random number in interval [0,N).
104 With argument t, set the random number seed from the current time and pid. */)
106 Lisp_Object n;
108 EMACS_INT val;
109 Lisp_Object lispy_val;
110 unsigned long denominator;
112 if (EQ (n, Qt))
113 seed_random (getpid () + time (NULL));
114 if (NATNUMP (n) && XFASTINT (n) != 0)
116 /* Try to take our random number from the higher bits of VAL,
117 not the lower, since (says Gentzel) the low bits of `random'
118 are less random than the higher ones. We do this by using the
119 quotient rather than the remainder. At the high end of the RNG
120 it's possible to get a quotient larger than n; discarding
121 these values eliminates the bias that would otherwise appear
122 when using a large n. */
123 denominator = ((unsigned long)1 << VALBITS) / XFASTINT (n);
125 val = get_random () / denominator;
126 while (val >= XFASTINT (n));
128 else
129 val = get_random ();
130 XSETINT (lispy_val, val);
131 return lispy_val;
134 /* Random data-structure functions */
136 DEFUN ("length", Flength, Slength, 1, 1, 0,
137 doc: /* Return the length of vector, list or string SEQUENCE.
138 A byte-code function object is also allowed.
139 If the string contains multibyte characters, this is not necessarily
140 the number of bytes in the string; it is the number of characters.
141 To get the number of bytes, use `string-bytes'. */)
142 (sequence)
143 register Lisp_Object sequence;
145 register Lisp_Object val;
146 register int i;
148 if (STRINGP (sequence))
149 XSETFASTINT (val, SCHARS (sequence));
150 else if (VECTORP (sequence))
151 XSETFASTINT (val, ASIZE (sequence));
152 else if (CHAR_TABLE_P (sequence))
153 XSETFASTINT (val, MAX_CHAR);
154 else if (BOOL_VECTOR_P (sequence))
155 XSETFASTINT (val, XBOOL_VECTOR (sequence)->size);
156 else if (COMPILEDP (sequence))
157 XSETFASTINT (val, ASIZE (sequence) & PSEUDOVECTOR_SIZE_MASK);
158 else if (CONSP (sequence))
160 i = 0;
161 while (CONSP (sequence))
163 sequence = XCDR (sequence);
164 ++i;
166 if (!CONSP (sequence))
167 break;
169 sequence = XCDR (sequence);
170 ++i;
171 QUIT;
174 CHECK_LIST_END (sequence, sequence);
176 val = make_number (i);
178 else if (NILP (sequence))
179 XSETFASTINT (val, 0);
180 else
181 wrong_type_argument (Qsequencep, sequence);
183 return val;
186 /* This does not check for quits. That is safe since it must terminate. */
188 DEFUN ("safe-length", Fsafe_length, Ssafe_length, 1, 1, 0,
189 doc: /* Return the length of a list, but avoid error or infinite loop.
190 This function never gets an error. If LIST is not really a list,
191 it returns 0. If LIST is circular, it returns a finite value
192 which is at least the number of distinct elements. */)
193 (list)
194 Lisp_Object list;
196 Lisp_Object tail, halftail, length;
197 int len = 0;
199 /* halftail is used to detect circular lists. */
200 halftail = list;
201 for (tail = list; CONSP (tail); tail = XCDR (tail))
203 if (EQ (tail, halftail) && len != 0)
204 break;
205 len++;
206 if ((len & 1) == 0)
207 halftail = XCDR (halftail);
210 XSETINT (length, len);
211 return length;
214 DEFUN ("string-bytes", Fstring_bytes, Sstring_bytes, 1, 1, 0,
215 doc: /* Return the number of bytes in STRING.
216 If STRING is multibyte, this may be greater than the length of STRING. */)
217 (string)
218 Lisp_Object string;
220 CHECK_STRING (string);
221 return make_number (SBYTES (string));
224 DEFUN ("string-equal", Fstring_equal, Sstring_equal, 2, 2, 0,
225 doc: /* Return t if two strings have identical contents.
226 Case is significant, but text properties are ignored.
227 Symbols are also allowed; their print names are used instead. */)
228 (s1, s2)
229 register Lisp_Object s1, s2;
231 if (SYMBOLP (s1))
232 s1 = SYMBOL_NAME (s1);
233 if (SYMBOLP (s2))
234 s2 = SYMBOL_NAME (s2);
235 CHECK_STRING (s1);
236 CHECK_STRING (s2);
238 if (SCHARS (s1) != SCHARS (s2)
239 || SBYTES (s1) != SBYTES (s2)
240 || bcmp (SDATA (s1), SDATA (s2), SBYTES (s1)))
241 return Qnil;
242 return Qt;
245 DEFUN ("compare-strings", Fcompare_strings,
246 Scompare_strings, 6, 7, 0,
247 doc: /* Compare the contents of two strings, converting to multibyte if needed.
248 In string STR1, skip the first START1 characters and stop at END1.
249 In string STR2, skip the first START2 characters and stop at END2.
250 END1 and END2 default to the full lengths of the respective strings.
252 Case is significant in this comparison if IGNORE-CASE is nil.
253 Unibyte strings are converted to multibyte for comparison.
255 The value is t if the strings (or specified portions) match.
256 If string STR1 is less, the value is a negative number N;
257 - 1 - N is the number of characters that match at the beginning.
258 If string STR1 is greater, the value is a positive number N;
259 N - 1 is the number of characters that match at the beginning. */)
260 (str1, start1, end1, str2, start2, end2, ignore_case)
261 Lisp_Object str1, start1, end1, start2, str2, end2, ignore_case;
263 register int end1_char, end2_char;
264 register int i1, i1_byte, i2, i2_byte;
266 CHECK_STRING (str1);
267 CHECK_STRING (str2);
268 if (NILP (start1))
269 start1 = make_number (0);
270 if (NILP (start2))
271 start2 = make_number (0);
272 CHECK_NATNUM (start1);
273 CHECK_NATNUM (start2);
274 if (! NILP (end1))
275 CHECK_NATNUM (end1);
276 if (! NILP (end2))
277 CHECK_NATNUM (end2);
279 i1 = XINT (start1);
280 i2 = XINT (start2);
282 i1_byte = string_char_to_byte (str1, i1);
283 i2_byte = string_char_to_byte (str2, i2);
285 end1_char = SCHARS (str1);
286 if (! NILP (end1) && end1_char > XINT (end1))
287 end1_char = XINT (end1);
289 end2_char = SCHARS (str2);
290 if (! NILP (end2) && end2_char > XINT (end2))
291 end2_char = XINT (end2);
293 while (i1 < end1_char && i2 < end2_char)
295 /* When we find a mismatch, we must compare the
296 characters, not just the bytes. */
297 int c1, c2;
299 if (STRING_MULTIBYTE (str1))
300 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c1, str1, i1, i1_byte);
301 else
303 c1 = SREF (str1, i1++);
304 c1 = unibyte_char_to_multibyte (c1);
307 if (STRING_MULTIBYTE (str2))
308 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c2, str2, i2, i2_byte);
309 else
311 c2 = SREF (str2, i2++);
312 c2 = unibyte_char_to_multibyte (c2);
315 if (c1 == c2)
316 continue;
318 if (! NILP (ignore_case))
320 Lisp_Object tem;
322 tem = Fupcase (make_number (c1));
323 c1 = XINT (tem);
324 tem = Fupcase (make_number (c2));
325 c2 = XINT (tem);
328 if (c1 == c2)
329 continue;
331 /* Note that I1 has already been incremented
332 past the character that we are comparing;
333 hence we don't add or subtract 1 here. */
334 if (c1 < c2)
335 return make_number (- i1 + XINT (start1));
336 else
337 return make_number (i1 - XINT (start1));
340 if (i1 < end1_char)
341 return make_number (i1 - XINT (start1) + 1);
342 if (i2 < end2_char)
343 return make_number (- i1 + XINT (start1) - 1);
345 return Qt;
348 DEFUN ("string-lessp", Fstring_lessp, Sstring_lessp, 2, 2, 0,
349 doc: /* Return t if first arg string is less than second in lexicographic order.
350 Case is significant.
351 Symbols are also allowed; their print names are used instead. */)
352 (s1, s2)
353 register Lisp_Object s1, s2;
355 register int end;
356 register int i1, i1_byte, i2, i2_byte;
358 if (SYMBOLP (s1))
359 s1 = SYMBOL_NAME (s1);
360 if (SYMBOLP (s2))
361 s2 = SYMBOL_NAME (s2);
362 CHECK_STRING (s1);
363 CHECK_STRING (s2);
365 i1 = i1_byte = i2 = i2_byte = 0;
367 end = SCHARS (s1);
368 if (end > SCHARS (s2))
369 end = SCHARS (s2);
371 while (i1 < end)
373 /* When we find a mismatch, we must compare the
374 characters, not just the bytes. */
375 int c1, c2;
377 FETCH_STRING_CHAR_ADVANCE (c1, s1, i1, i1_byte);
378 FETCH_STRING_CHAR_ADVANCE (c2, s2, i2, i2_byte);
380 if (c1 != c2)
381 return c1 < c2 ? Qt : Qnil;
383 return i1 < SCHARS (s2) ? Qt : Qnil;
386 #if __GNUC__
387 /* "gcc -O3" enables automatic function inlining, which optimizes out
388 the arguments for the invocations of this function, whereas it
389 expects these values on the stack. */
390 static Lisp_Object concat P_ ((int nargs, Lisp_Object *args, enum Lisp_Type target_type, int last_special)) __attribute__((noinline));
391 #else /* !__GNUC__ */
392 static Lisp_Object concat P_ ((int nargs, Lisp_Object *args, enum Lisp_Type target_type, int last_special));
393 #endif
395 /* ARGSUSED */
396 Lisp_Object
397 concat2 (s1, s2)
398 Lisp_Object s1, s2;
400 #ifdef NO_ARG_ARRAY
401 Lisp_Object args[2];
402 args[0] = s1;
403 args[1] = s2;
404 return concat (2, args, Lisp_String, 0);
405 #else
406 return concat (2, &s1, Lisp_String, 0);
407 #endif /* NO_ARG_ARRAY */
410 /* ARGSUSED */
411 Lisp_Object
412 concat3 (s1, s2, s3)
413 Lisp_Object s1, s2, s3;
415 #ifdef NO_ARG_ARRAY
416 Lisp_Object args[3];
417 args[0] = s1;
418 args[1] = s2;
419 args[2] = s3;
420 return concat (3, args, Lisp_String, 0);
421 #else
422 return concat (3, &s1, Lisp_String, 0);
423 #endif /* NO_ARG_ARRAY */
426 DEFUN ("append", Fappend, Sappend, 0, MANY, 0,
427 doc: /* Concatenate all the arguments and make the result a list.
428 The result is a list whose elements are the elements of all the arguments.
429 Each argument may be a list, vector or string.
430 The last argument is not copied, just used as the tail of the new list.
431 usage: (append &rest SEQUENCES) */)
432 (nargs, args)
433 int nargs;
434 Lisp_Object *args;
436 return concat (nargs, args, Lisp_Cons, 1);
439 DEFUN ("concat", Fconcat, Sconcat, 0, MANY, 0,
440 doc: /* Concatenate all the arguments and make the result a string.
441 The result is a string whose elements are the elements of all the arguments.
442 Each argument may be a string or a list or vector of characters (integers).
443 usage: (concat &rest SEQUENCES) */)
444 (nargs, args)
445 int nargs;
446 Lisp_Object *args;
448 return concat (nargs, args, Lisp_String, 0);
451 DEFUN ("vconcat", Fvconcat, Svconcat, 0, MANY, 0,
452 doc: /* Concatenate all the arguments and make the result a vector.
453 The result is a vector whose elements are the elements of all the arguments.
454 Each argument may be a list, vector or string.
455 usage: (vconcat &rest SEQUENCES) */)
456 (nargs, args)
457 int nargs;
458 Lisp_Object *args;
460 return concat (nargs, args, Lisp_Vectorlike, 0);
464 DEFUN ("copy-sequence", Fcopy_sequence, Scopy_sequence, 1, 1, 0,
465 doc: /* Return a copy of a list, vector, string or char-table.
466 The elements of a list or vector are not copied; they are shared
467 with the original. */)
468 (arg)
469 Lisp_Object arg;
471 if (NILP (arg)) return arg;
473 if (CHAR_TABLE_P (arg))
475 return copy_char_table (arg);
478 if (BOOL_VECTOR_P (arg))
480 Lisp_Object val;
481 int size_in_chars
482 = ((XBOOL_VECTOR (arg)->size + BOOL_VECTOR_BITS_PER_CHAR - 1)
483 / BOOL_VECTOR_BITS_PER_CHAR);
485 val = Fmake_bool_vector (Flength (arg), Qnil);
486 bcopy (XBOOL_VECTOR (arg)->data, XBOOL_VECTOR (val)->data,
487 size_in_chars);
488 return val;
491 if (!CONSP (arg) && !VECTORP (arg) && !STRINGP (arg))
492 wrong_type_argument (Qsequencep, arg);
494 return concat (1, &arg, CONSP (arg) ? Lisp_Cons : XTYPE (arg), 0);
497 /* This structure holds information of an argument of `concat' that is
498 a string and has text properties to be copied. */
499 struct textprop_rec
501 int argnum; /* refer to ARGS (arguments of `concat') */
502 int from; /* refer to ARGS[argnum] (argument string) */
503 int to; /* refer to VAL (the target string) */
506 static Lisp_Object
507 concat (nargs, args, target_type, last_special)
508 int nargs;
509 Lisp_Object *args;
510 enum Lisp_Type target_type;
511 int last_special;
513 Lisp_Object val;
514 register Lisp_Object tail;
515 register Lisp_Object this;
516 int toindex;
517 int toindex_byte = 0;
518 register int result_len;
519 register int result_len_byte;
520 register int argnum;
521 Lisp_Object last_tail;
522 Lisp_Object prev;
523 int some_multibyte;
524 /* When we make a multibyte string, we can't copy text properties
525 while concatinating each string because the length of resulting
526 string can't be decided until we finish the whole concatination.
527 So, we record strings that have text properties to be copied
528 here, and copy the text properties after the concatination. */
529 struct textprop_rec *textprops = NULL;
530 /* Number of elments in textprops. */
531 int num_textprops = 0;
532 USE_SAFE_ALLOCA;
534 tail = Qnil;
536 /* In append, the last arg isn't treated like the others */
537 if (last_special && nargs > 0)
539 nargs--;
540 last_tail = args[nargs];
542 else
543 last_tail = Qnil;
545 /* Check each argument. */
546 for (argnum = 0; argnum < nargs; argnum++)
548 this = args[argnum];
549 if (!(CONSP (this) || NILP (this) || VECTORP (this) || STRINGP (this)
550 || COMPILEDP (this) || BOOL_VECTOR_P (this)))
551 wrong_type_argument (Qsequencep, this);
554 /* Compute total length in chars of arguments in RESULT_LEN.
555 If desired output is a string, also compute length in bytes
556 in RESULT_LEN_BYTE, and determine in SOME_MULTIBYTE
557 whether the result should be a multibyte string. */
558 result_len_byte = 0;
559 result_len = 0;
560 some_multibyte = 0;
561 for (argnum = 0; argnum < nargs; argnum++)
563 int len;
564 this = args[argnum];
565 len = XFASTINT (Flength (this));
566 if (target_type == Lisp_String)
568 /* We must count the number of bytes needed in the string
569 as well as the number of characters. */
570 int i;
571 Lisp_Object ch;
572 int this_len_byte;
574 if (VECTORP (this))
575 for (i = 0; i < len; i++)
577 ch = AREF (this, i);
578 CHECK_CHARACTER (ch);
579 this_len_byte = CHAR_BYTES (XINT (ch));
580 result_len_byte += this_len_byte;
581 if (! ASCII_CHAR_P (XINT (ch)) && ! CHAR_BYTE8_P (XINT (ch)))
582 some_multibyte = 1;
584 else if (BOOL_VECTOR_P (this) && XBOOL_VECTOR (this)->size > 0)
585 wrong_type_argument (Qintegerp, Faref (this, make_number (0)));
586 else if (CONSP (this))
587 for (; CONSP (this); this = XCDR (this))
589 ch = XCAR (this);
590 CHECK_CHARACTER (ch);
591 this_len_byte = CHAR_BYTES (XINT (ch));
592 result_len_byte += this_len_byte;
593 if (! ASCII_CHAR_P (XINT (ch)) && ! CHAR_BYTE8_P (XINT (ch)))
594 some_multibyte = 1;
596 else if (STRINGP (this))
598 if (STRING_MULTIBYTE (this))
600 some_multibyte = 1;
601 result_len_byte += SBYTES (this);
603 else
604 result_len_byte += count_size_as_multibyte (SDATA (this),
605 SCHARS (this));
609 result_len += len;
612 if (! some_multibyte)
613 result_len_byte = result_len;
615 /* Create the output object. */
616 if (target_type == Lisp_Cons)
617 val = Fmake_list (make_number (result_len), Qnil);
618 else if (target_type == Lisp_Vectorlike)
619 val = Fmake_vector (make_number (result_len), Qnil);
620 else if (some_multibyte)
621 val = make_uninit_multibyte_string (result_len, result_len_byte);
622 else
623 val = make_uninit_string (result_len);
625 /* In `append', if all but last arg are nil, return last arg. */
626 if (target_type == Lisp_Cons && EQ (val, Qnil))
627 return last_tail;
629 /* Copy the contents of the args into the result. */
630 if (CONSP (val))
631 tail = val, toindex = -1; /* -1 in toindex is flag we are making a list */
632 else
633 toindex = 0, toindex_byte = 0;
635 prev = Qnil;
636 if (STRINGP (val))
637 SAFE_ALLOCA (textprops, struct textprop_rec *, sizeof (struct textprop_rec) * nargs);
639 for (argnum = 0; argnum < nargs; argnum++)
641 Lisp_Object thislen;
642 int thisleni = 0;
643 register unsigned int thisindex = 0;
644 register unsigned int thisindex_byte = 0;
646 this = args[argnum];
647 if (!CONSP (this))
648 thislen = Flength (this), thisleni = XINT (thislen);
650 /* Between strings of the same kind, copy fast. */
651 if (STRINGP (this) && STRINGP (val)
652 && STRING_MULTIBYTE (this) == some_multibyte)
654 int thislen_byte = SBYTES (this);
656 bcopy (SDATA (this), SDATA (val) + toindex_byte,
657 SBYTES (this));
658 if (! NULL_INTERVAL_P (STRING_INTERVALS (this)))
660 textprops[num_textprops].argnum = argnum;
661 textprops[num_textprops].from = 0;
662 textprops[num_textprops++].to = toindex;
664 toindex_byte += thislen_byte;
665 toindex += thisleni;
666 STRING_SET_CHARS (val, SCHARS (val));
668 /* Copy a single-byte string to a multibyte string. */
669 else if (STRINGP (this) && STRINGP (val))
671 if (! NULL_INTERVAL_P (STRING_INTERVALS (this)))
673 textprops[num_textprops].argnum = argnum;
674 textprops[num_textprops].from = 0;
675 textprops[num_textprops++].to = toindex;
677 toindex_byte += copy_text (SDATA (this),
678 SDATA (val) + toindex_byte,
679 SCHARS (this), 0, 1);
680 toindex += thisleni;
682 else
683 /* Copy element by element. */
684 while (1)
686 register Lisp_Object elt;
688 /* Fetch next element of `this' arg into `elt', or break if
689 `this' is exhausted. */
690 if (NILP (this)) break;
691 if (CONSP (this))
692 elt = XCAR (this), this = XCDR (this);
693 else if (thisindex >= thisleni)
694 break;
695 else if (STRINGP (this))
697 int c;
698 if (STRING_MULTIBYTE (this))
700 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, this,
701 thisindex,
702 thisindex_byte);
703 XSETFASTINT (elt, c);
705 else
707 XSETFASTINT (elt, SREF (this, thisindex)); thisindex++;
708 if (some_multibyte
709 && XINT (elt) >= 0200
710 && XINT (elt) < 0400)
712 c = unibyte_char_to_multibyte (XINT (elt));
713 XSETINT (elt, c);
717 else if (BOOL_VECTOR_P (this))
719 int byte;
720 byte = XBOOL_VECTOR (this)->data[thisindex / BOOL_VECTOR_BITS_PER_CHAR];
721 if (byte & (1 << (thisindex % BOOL_VECTOR_BITS_PER_CHAR)))
722 elt = Qt;
723 else
724 elt = Qnil;
725 thisindex++;
727 else
729 elt = AREF (this, thisindex);
730 thisindex++;
733 /* Store this element into the result. */
734 if (toindex < 0)
736 XSETCAR (tail, elt);
737 prev = tail;
738 tail = XCDR (tail);
740 else if (VECTORP (val))
742 ASET (val, toindex, elt);
743 toindex++;
745 else
747 CHECK_NUMBER (elt);
748 if (some_multibyte)
749 toindex_byte += CHAR_STRING (XINT (elt),
750 SDATA (val) + toindex_byte);
751 else
752 SSET (val, toindex_byte++, XINT (elt));
753 toindex++;
757 if (!NILP (prev))
758 XSETCDR (prev, last_tail);
760 if (num_textprops > 0)
762 Lisp_Object props;
763 int last_to_end = -1;
765 for (argnum = 0; argnum < num_textprops; argnum++)
767 this = args[textprops[argnum].argnum];
768 props = text_property_list (this,
769 make_number (0),
770 make_number (SCHARS (this)),
771 Qnil);
772 /* If successive arguments have properites, be sure that the
773 value of `composition' property be the copy. */
774 if (last_to_end == textprops[argnum].to)
775 make_composition_value_copy (props);
776 add_text_properties_from_list (val, props,
777 make_number (textprops[argnum].to));
778 last_to_end = textprops[argnum].to + SCHARS (this);
782 SAFE_FREE ();
783 return val;
786 static Lisp_Object string_char_byte_cache_string;
787 static EMACS_INT string_char_byte_cache_charpos;
788 static EMACS_INT string_char_byte_cache_bytepos;
790 void
791 clear_string_char_byte_cache ()
793 string_char_byte_cache_string = Qnil;
796 /* Return the byte index corresponding to CHAR_INDEX in STRING. */
798 EMACS_INT
799 string_char_to_byte (string, char_index)
800 Lisp_Object string;
801 EMACS_INT char_index;
803 EMACS_INT i_byte;
804 EMACS_INT best_below, best_below_byte;
805 EMACS_INT best_above, best_above_byte;
807 best_below = best_below_byte = 0;
808 best_above = SCHARS (string);
809 best_above_byte = SBYTES (string);
810 if (best_above == best_above_byte)
811 return char_index;
813 if (EQ (string, string_char_byte_cache_string))
815 if (string_char_byte_cache_charpos < char_index)
817 best_below = string_char_byte_cache_charpos;
818 best_below_byte = string_char_byte_cache_bytepos;
820 else
822 best_above = string_char_byte_cache_charpos;
823 best_above_byte = string_char_byte_cache_bytepos;
827 if (char_index - best_below < best_above - char_index)
829 unsigned char *p = SDATA (string) + best_below_byte;
831 while (best_below < char_index)
833 p += BYTES_BY_CHAR_HEAD (*p);
834 best_below++;
836 i_byte = p - SDATA (string);
838 else
840 unsigned char *p = SDATA (string) + best_above_byte;
842 while (best_above > char_index)
844 p--;
845 while (!CHAR_HEAD_P (*p)) p--;
846 best_above--;
848 i_byte = p - SDATA (string);
851 string_char_byte_cache_bytepos = i_byte;
852 string_char_byte_cache_charpos = char_index;
853 string_char_byte_cache_string = string;
855 return i_byte;
858 /* Return the character index corresponding to BYTE_INDEX in STRING. */
860 EMACS_INT
861 string_byte_to_char (string, byte_index)
862 Lisp_Object string;
863 EMACS_INT byte_index;
865 EMACS_INT i, i_byte;
866 EMACS_INT best_below, best_below_byte;
867 EMACS_INT best_above, best_above_byte;
869 best_below = best_below_byte = 0;
870 best_above = SCHARS (string);
871 best_above_byte = SBYTES (string);
872 if (best_above == best_above_byte)
873 return byte_index;
875 if (EQ (string, string_char_byte_cache_string))
877 if (string_char_byte_cache_bytepos < byte_index)
879 best_below = string_char_byte_cache_charpos;
880 best_below_byte = string_char_byte_cache_bytepos;
882 else
884 best_above = string_char_byte_cache_charpos;
885 best_above_byte = string_char_byte_cache_bytepos;
889 if (byte_index - best_below_byte < best_above_byte - byte_index)
891 unsigned char *p = SDATA (string) + best_below_byte;
892 unsigned char *pend = SDATA (string) + byte_index;
894 while (p < pend)
896 p += BYTES_BY_CHAR_HEAD (*p);
897 best_below++;
899 i = best_below;
900 i_byte = p - SDATA (string);
902 else
904 unsigned char *p = SDATA (string) + best_above_byte;
905 unsigned char *pbeg = SDATA (string) + byte_index;
907 while (p > pbeg)
909 p--;
910 while (!CHAR_HEAD_P (*p)) p--;
911 best_above--;
913 i = best_above;
914 i_byte = p - SDATA (string);
917 string_char_byte_cache_bytepos = i_byte;
918 string_char_byte_cache_charpos = i;
919 string_char_byte_cache_string = string;
921 return i;
924 /* Convert STRING to a multibyte string. */
926 Lisp_Object
927 string_make_multibyte (string)
928 Lisp_Object string;
930 unsigned char *buf;
931 EMACS_INT nbytes;
932 Lisp_Object ret;
933 USE_SAFE_ALLOCA;
935 if (STRING_MULTIBYTE (string))
936 return string;
938 nbytes = count_size_as_multibyte (SDATA (string),
939 SCHARS (string));
940 /* If all the chars are ASCII, they won't need any more bytes
941 once converted. In that case, we can return STRING itself. */
942 if (nbytes == SBYTES (string))
943 return string;
945 SAFE_ALLOCA (buf, unsigned char *, nbytes);
946 copy_text (SDATA (string), buf, SBYTES (string),
947 0, 1);
949 ret = make_multibyte_string (buf, SCHARS (string), nbytes);
950 SAFE_FREE ();
952 return ret;
956 /* Convert STRING (if unibyte) to a multibyte string without changing
957 the number of characters. Characters 0200 trough 0237 are
958 converted to eight-bit characters. */
960 Lisp_Object
961 string_to_multibyte (string)
962 Lisp_Object string;
964 unsigned char *buf;
965 EMACS_INT nbytes;
966 Lisp_Object ret;
967 USE_SAFE_ALLOCA;
969 if (STRING_MULTIBYTE (string))
970 return string;
972 nbytes = parse_str_to_multibyte (SDATA (string), SBYTES (string));
973 /* If all the chars are ASCII, they won't need any more bytes once
974 converted. */
975 if (nbytes == SBYTES (string))
976 return make_multibyte_string (SDATA (string), nbytes, nbytes);
978 SAFE_ALLOCA (buf, unsigned char *, nbytes);
979 bcopy (SDATA (string), buf, SBYTES (string));
980 str_to_multibyte (buf, nbytes, SBYTES (string));
982 ret = make_multibyte_string (buf, SCHARS (string), nbytes);
983 SAFE_FREE ();
985 return ret;
989 /* Convert STRING to a single-byte string. */
991 Lisp_Object
992 string_make_unibyte (string)
993 Lisp_Object string;
995 int nchars;
996 unsigned char *buf;
997 Lisp_Object ret;
998 USE_SAFE_ALLOCA;
1000 if (! STRING_MULTIBYTE (string))
1001 return string;
1003 nchars = SCHARS (string);
1005 SAFE_ALLOCA (buf, unsigned char *, nchars);
1006 copy_text (SDATA (string), buf, SBYTES (string),
1007 1, 0);
1009 ret = make_unibyte_string (buf, nchars);
1010 SAFE_FREE ();
1012 return ret;
1015 DEFUN ("string-make-multibyte", Fstring_make_multibyte, Sstring_make_multibyte,
1016 1, 1, 0,
1017 doc: /* Return the multibyte equivalent of STRING.
1018 If STRING is unibyte and contains non-ASCII characters, the function
1019 `unibyte-char-to-multibyte' is used to convert each unibyte character
1020 to a multibyte character. In this case, the returned string is a
1021 newly created string with no text properties. If STRING is multibyte
1022 or entirely ASCII, it is returned unchanged. In particular, when
1023 STRING is unibyte and entirely ASCII, the returned string is unibyte.
1024 \(When the characters are all ASCII, Emacs primitives will treat the
1025 string the same way whether it is unibyte or multibyte.) */)
1026 (string)
1027 Lisp_Object string;
1029 CHECK_STRING (string);
1031 return string_make_multibyte (string);
1034 DEFUN ("string-make-unibyte", Fstring_make_unibyte, Sstring_make_unibyte,
1035 1, 1, 0,
1036 doc: /* Return the unibyte equivalent of STRING.
1037 Multibyte character codes are converted to unibyte according to
1038 `nonascii-translation-table' or, if that is nil, `nonascii-insert-offset'.
1039 If the lookup in the translation table fails, this function takes just
1040 the low 8 bits of each character. */)
1041 (string)
1042 Lisp_Object string;
1044 CHECK_STRING (string);
1046 return string_make_unibyte (string);
1049 DEFUN ("string-as-unibyte", Fstring_as_unibyte, Sstring_as_unibyte,
1050 1, 1, 0,
1051 doc: /* Return a unibyte string with the same individual bytes as STRING.
1052 If STRING is unibyte, the result is STRING itself.
1053 Otherwise it is a newly created string, with no text properties.
1054 If STRING is multibyte and contains a character of charset
1055 `eight-bit', it is converted to the corresponding single byte. */)
1056 (string)
1057 Lisp_Object string;
1059 CHECK_STRING (string);
1061 if (STRING_MULTIBYTE (string))
1063 int bytes = SBYTES (string);
1064 unsigned char *str = (unsigned char *) xmalloc (bytes);
1066 bcopy (SDATA (string), str, bytes);
1067 bytes = str_as_unibyte (str, bytes);
1068 string = make_unibyte_string (str, bytes);
1069 xfree (str);
1071 return string;
1074 DEFUN ("string-as-multibyte", Fstring_as_multibyte, Sstring_as_multibyte,
1075 1, 1, 0,
1076 doc: /* Return a multibyte string with the same individual bytes as STRING.
1077 If STRING is multibyte, the result is STRING itself.
1078 Otherwise it is a newly created string, with no text properties.
1080 If STRING is unibyte and contains an individual 8-bit byte (i.e. not
1081 part of a correct utf-8 sequence), it is converted to the corresponding
1082 multibyte character of charset `eight-bit'.
1083 See also `string-to-multibyte'.
1085 Beware, this often doesn't really do what you think it does.
1086 It is similar to (decode-coding-string STRING 'utf-8-emacs).
1087 If you're not sure, whether to use `string-as-multibyte' or
1088 `string-to-multibyte', use `string-to-multibyte'. */)
1089 (string)
1090 Lisp_Object string;
1092 CHECK_STRING (string);
1094 if (! STRING_MULTIBYTE (string))
1096 Lisp_Object new_string;
1097 int nchars, nbytes;
1099 parse_str_as_multibyte (SDATA (string),
1100 SBYTES (string),
1101 &nchars, &nbytes);
1102 new_string = make_uninit_multibyte_string (nchars, nbytes);
1103 bcopy (SDATA (string), SDATA (new_string),
1104 SBYTES (string));
1105 if (nbytes != SBYTES (string))
1106 str_as_multibyte (SDATA (new_string), nbytes,
1107 SBYTES (string), NULL);
1108 string = new_string;
1109 STRING_SET_INTERVALS (string, NULL_INTERVAL);
1111 return string;
1114 DEFUN ("string-to-multibyte", Fstring_to_multibyte, Sstring_to_multibyte,
1115 1, 1, 0,
1116 doc: /* Return a multibyte string with the same individual chars as STRING.
1117 If STRING is multibyte, the result is STRING itself.
1118 Otherwise it is a newly created string, with no text properties.
1120 If STRING is unibyte and contains an 8-bit byte, it is converted to
1121 the corresponding multibyte character of charset `eight-bit'.
1123 This differs from `string-as-multibyte' by converting each byte of a correct
1124 utf-8 sequence to an eight-bit character, not just bytes that don't form a
1125 correct sequence. */)
1126 (string)
1127 Lisp_Object string;
1129 CHECK_STRING (string);
1131 return string_to_multibyte (string);
1134 DEFUN ("string-to-unibyte", Fstring_to_unibyte, Sstring_to_unibyte,
1135 1, 2, 0,
1136 doc: /* Return a unibyte string with the same individual chars as STRING.
1137 If STRING is unibyte, the result is STRING itself.
1138 Otherwise it is a newly created string, with no text properties,
1139 where each `eight-bit' character is converted to the corresponding byte.
1140 If STRING contains a non-ASCII, non-`eight-bit' character,
1141 an error is signaled.
1142 If the optional 2nd arg ACCEPT-LATIN-1 is non-nil, a Latin-1 character
1143 doesn't cause an error, but is converted to a byte of same code. */)
1144 (string, accept_latin_1)
1145 Lisp_Object string, accept_latin_1;
1147 CHECK_STRING (string);
1149 if (STRING_MULTIBYTE (string))
1151 EMACS_INT chars = SCHARS (string);
1152 unsigned char *str = (unsigned char *) xmalloc (chars);
1153 EMACS_INT converted = str_to_unibyte (SDATA (string), str, chars,
1154 ! NILP (accept_latin_1));
1155 if (converted < chars)
1156 error ("Can't convert the %dth character to unibyte", converted);
1157 string = make_unibyte_string (str, chars);
1158 xfree (str);
1160 return string;
1164 DEFUN ("copy-alist", Fcopy_alist, Scopy_alist, 1, 1, 0,
1165 doc: /* Return a copy of ALIST.
1166 This is an alist which represents the same mapping from objects to objects,
1167 but does not share the alist structure with ALIST.
1168 The objects mapped (cars and cdrs of elements of the alist)
1169 are shared, however.
1170 Elements of ALIST that are not conses are also shared. */)
1171 (alist)
1172 Lisp_Object alist;
1174 register Lisp_Object tem;
1176 CHECK_LIST (alist);
1177 if (NILP (alist))
1178 return alist;
1179 alist = concat (1, &alist, Lisp_Cons, 0);
1180 for (tem = alist; CONSP (tem); tem = XCDR (tem))
1182 register Lisp_Object car;
1183 car = XCAR (tem);
1185 if (CONSP (car))
1186 XSETCAR (tem, Fcons (XCAR (car), XCDR (car)));
1188 return alist;
1191 DEFUN ("substring", Fsubstring, Ssubstring, 2, 3, 0,
1192 doc: /* Return a substring of STRING, starting at index FROM and ending before TO.
1193 TO may be nil or omitted; then the substring runs to the end of STRING.
1194 FROM and TO start at 0. If either is negative, it counts from the end.
1196 This function allows vectors as well as strings. */)
1197 (string, from, to)
1198 Lisp_Object string;
1199 register Lisp_Object from, to;
1201 Lisp_Object res;
1202 int size;
1203 int size_byte = 0;
1204 int from_char, to_char;
1205 int from_byte = 0, to_byte = 0;
1207 CHECK_VECTOR_OR_STRING (string);
1208 CHECK_NUMBER (from);
1210 if (STRINGP (string))
1212 size = SCHARS (string);
1213 size_byte = SBYTES (string);
1215 else
1216 size = ASIZE (string);
1218 if (NILP (to))
1220 to_char = size;
1221 to_byte = size_byte;
1223 else
1225 CHECK_NUMBER (to);
1227 to_char = XINT (to);
1228 if (to_char < 0)
1229 to_char += size;
1231 if (STRINGP (string))
1232 to_byte = string_char_to_byte (string, to_char);
1235 from_char = XINT (from);
1236 if (from_char < 0)
1237 from_char += size;
1238 if (STRINGP (string))
1239 from_byte = string_char_to_byte (string, from_char);
1241 if (!(0 <= from_char && from_char <= to_char && to_char <= size))
1242 args_out_of_range_3 (string, make_number (from_char),
1243 make_number (to_char));
1245 if (STRINGP (string))
1247 res = make_specified_string (SDATA (string) + from_byte,
1248 to_char - from_char, to_byte - from_byte,
1249 STRING_MULTIBYTE (string));
1250 copy_text_properties (make_number (from_char), make_number (to_char),
1251 string, make_number (0), res, Qnil);
1253 else
1254 res = Fvector (to_char - from_char, &AREF (string, from_char));
1256 return res;
1260 DEFUN ("substring-no-properties", Fsubstring_no_properties, Ssubstring_no_properties, 1, 3, 0,
1261 doc: /* Return a substring of STRING, without text properties.
1262 It starts at index FROM and ending before TO.
1263 TO may be nil or omitted; then the substring runs to the end of STRING.
1264 If FROM is nil or omitted, the substring starts at the beginning of STRING.
1265 If FROM or TO is negative, it counts from the end.
1267 With one argument, just copy STRING without its properties. */)
1268 (string, from, to)
1269 Lisp_Object string;
1270 register Lisp_Object from, to;
1272 int size, size_byte;
1273 int from_char, to_char;
1274 int from_byte, to_byte;
1276 CHECK_STRING (string);
1278 size = SCHARS (string);
1279 size_byte = SBYTES (string);
1281 if (NILP (from))
1282 from_char = from_byte = 0;
1283 else
1285 CHECK_NUMBER (from);
1286 from_char = XINT (from);
1287 if (from_char < 0)
1288 from_char += size;
1290 from_byte = string_char_to_byte (string, from_char);
1293 if (NILP (to))
1295 to_char = size;
1296 to_byte = size_byte;
1298 else
1300 CHECK_NUMBER (to);
1302 to_char = XINT (to);
1303 if (to_char < 0)
1304 to_char += size;
1306 to_byte = string_char_to_byte (string, to_char);
1309 if (!(0 <= from_char && from_char <= to_char && to_char <= size))
1310 args_out_of_range_3 (string, make_number (from_char),
1311 make_number (to_char));
1313 return make_specified_string (SDATA (string) + from_byte,
1314 to_char - from_char, to_byte - from_byte,
1315 STRING_MULTIBYTE (string));
1318 /* Extract a substring of STRING, giving start and end positions
1319 both in characters and in bytes. */
1321 Lisp_Object
1322 substring_both (string, from, from_byte, to, to_byte)
1323 Lisp_Object string;
1324 int from, from_byte, to, to_byte;
1326 Lisp_Object res;
1327 int size;
1328 int size_byte;
1330 CHECK_VECTOR_OR_STRING (string);
1332 if (STRINGP (string))
1334 size = SCHARS (string);
1335 size_byte = SBYTES (string);
1337 else
1338 size = ASIZE (string);
1340 if (!(0 <= from && from <= to && to <= size))
1341 args_out_of_range_3 (string, make_number (from), make_number (to));
1343 if (STRINGP (string))
1345 res = make_specified_string (SDATA (string) + from_byte,
1346 to - from, to_byte - from_byte,
1347 STRING_MULTIBYTE (string));
1348 copy_text_properties (make_number (from), make_number (to),
1349 string, make_number (0), res, Qnil);
1351 else
1352 res = Fvector (to - from, &AREF (string, from));
1354 return res;
1357 DEFUN ("nthcdr", Fnthcdr, Snthcdr, 2, 2, 0,
1358 doc: /* Take cdr N times on LIST, returns the result. */)
1359 (n, list)
1360 Lisp_Object n;
1361 register Lisp_Object list;
1363 register int i, num;
1364 CHECK_NUMBER (n);
1365 num = XINT (n);
1366 for (i = 0; i < num && !NILP (list); i++)
1368 QUIT;
1369 CHECK_LIST_CONS (list, list);
1370 list = XCDR (list);
1372 return list;
1375 DEFUN ("nth", Fnth, Snth, 2, 2, 0,
1376 doc: /* Return the Nth element of LIST.
1377 N counts from zero. If LIST is not that long, nil is returned. */)
1378 (n, list)
1379 Lisp_Object n, list;
1381 return Fcar (Fnthcdr (n, list));
1384 DEFUN ("elt", Felt, Selt, 2, 2, 0,
1385 doc: /* Return element of SEQUENCE at index N. */)
1386 (sequence, n)
1387 register Lisp_Object sequence, n;
1389 CHECK_NUMBER (n);
1390 if (CONSP (sequence) || NILP (sequence))
1391 return Fcar (Fnthcdr (n, sequence));
1393 /* Faref signals a "not array" error, so check here. */
1394 CHECK_ARRAY (sequence, Qsequencep);
1395 return Faref (sequence, n);
1398 DEFUN ("member", Fmember, Smember, 2, 2, 0,
1399 doc: /* Return non-nil if ELT is an element of LIST. Comparison done with `equal'.
1400 The value is actually the tail of LIST whose car is ELT. */)
1401 (elt, list)
1402 register Lisp_Object elt;
1403 Lisp_Object list;
1405 register Lisp_Object tail;
1406 for (tail = list; CONSP (tail); tail = XCDR (tail))
1408 register Lisp_Object tem;
1409 CHECK_LIST_CONS (tail, list);
1410 tem = XCAR (tail);
1411 if (! NILP (Fequal (elt, tem)))
1412 return tail;
1413 QUIT;
1415 return Qnil;
1418 DEFUN ("memq", Fmemq, Smemq, 2, 2, 0,
1419 doc: /* Return non-nil if ELT is an element of LIST. Comparison done with `eq'.
1420 The value is actually the tail of LIST whose car is ELT. */)
1421 (elt, list)
1422 register Lisp_Object elt, list;
1424 while (1)
1426 if (!CONSP (list) || EQ (XCAR (list), elt))
1427 break;
1429 list = XCDR (list);
1430 if (!CONSP (list) || EQ (XCAR (list), elt))
1431 break;
1433 list = XCDR (list);
1434 if (!CONSP (list) || EQ (XCAR (list), elt))
1435 break;
1437 list = XCDR (list);
1438 QUIT;
1441 CHECK_LIST (list);
1442 return list;
1445 DEFUN ("memql", Fmemql, Smemql, 2, 2, 0,
1446 doc: /* Return non-nil if ELT is an element of LIST. Comparison done with `eql'.
1447 The value is actually the tail of LIST whose car is ELT. */)
1448 (elt, list)
1449 register Lisp_Object elt;
1450 Lisp_Object list;
1452 register Lisp_Object tail;
1454 if (!FLOATP (elt))
1455 return Fmemq (elt, list);
1457 for (tail = list; CONSP (tail); tail = XCDR (tail))
1459 register Lisp_Object tem;
1460 CHECK_LIST_CONS (tail, list);
1461 tem = XCAR (tail);
1462 if (FLOATP (tem) && internal_equal (elt, tem, 0, 0))
1463 return tail;
1464 QUIT;
1466 return Qnil;
1469 DEFUN ("assq", Fassq, Sassq, 2, 2, 0,
1470 doc: /* Return non-nil if KEY is `eq' to the car of an element of LIST.
1471 The value is actually the first element of LIST whose car is KEY.
1472 Elements of LIST that are not conses are ignored. */)
1473 (key, list)
1474 Lisp_Object key, list;
1476 while (1)
1478 if (!CONSP (list)
1479 || (CONSP (XCAR (list))
1480 && EQ (XCAR (XCAR (list)), key)))
1481 break;
1483 list = XCDR (list);
1484 if (!CONSP (list)
1485 || (CONSP (XCAR (list))
1486 && EQ (XCAR (XCAR (list)), key)))
1487 break;
1489 list = XCDR (list);
1490 if (!CONSP (list)
1491 || (CONSP (XCAR (list))
1492 && EQ (XCAR (XCAR (list)), key)))
1493 break;
1495 list = XCDR (list);
1496 QUIT;
1499 return CAR (list);
1502 /* Like Fassq but never report an error and do not allow quits.
1503 Use only on lists known never to be circular. */
1505 Lisp_Object
1506 assq_no_quit (key, list)
1507 Lisp_Object key, list;
1509 while (CONSP (list)
1510 && (!CONSP (XCAR (list))
1511 || !EQ (XCAR (XCAR (list)), key)))
1512 list = XCDR (list);
1514 return CAR_SAFE (list);
1517 DEFUN ("assoc", Fassoc, Sassoc, 2, 2, 0,
1518 doc: /* Return non-nil if KEY is `equal' to the car of an element of LIST.
1519 The value is actually the first element of LIST whose car equals KEY. */)
1520 (key, list)
1521 Lisp_Object key, list;
1523 Lisp_Object car;
1525 while (1)
1527 if (!CONSP (list)
1528 || (CONSP (XCAR (list))
1529 && (car = XCAR (XCAR (list)),
1530 EQ (car, key) || !NILP (Fequal (car, key)))))
1531 break;
1533 list = XCDR (list);
1534 if (!CONSP (list)
1535 || (CONSP (XCAR (list))
1536 && (car = XCAR (XCAR (list)),
1537 EQ (car, key) || !NILP (Fequal (car, key)))))
1538 break;
1540 list = XCDR (list);
1541 if (!CONSP (list)
1542 || (CONSP (XCAR (list))
1543 && (car = XCAR (XCAR (list)),
1544 EQ (car, key) || !NILP (Fequal (car, key)))))
1545 break;
1547 list = XCDR (list);
1548 QUIT;
1551 return CAR (list);
1554 /* Like Fassoc but never report an error and do not allow quits.
1555 Use only on lists known never to be circular. */
1557 Lisp_Object
1558 assoc_no_quit (key, list)
1559 Lisp_Object key, list;
1561 while (CONSP (list)
1562 && (!CONSP (XCAR (list))
1563 || (!EQ (XCAR (XCAR (list)), key)
1564 && NILP (Fequal (XCAR (XCAR (list)), key)))))
1565 list = XCDR (list);
1567 return CONSP (list) ? XCAR (list) : Qnil;
1570 DEFUN ("rassq", Frassq, Srassq, 2, 2, 0,
1571 doc: /* Return non-nil if KEY is `eq' to the cdr of an element of LIST.
1572 The value is actually the first element of LIST whose cdr is KEY. */)
1573 (key, list)
1574 register Lisp_Object key;
1575 Lisp_Object list;
1577 while (1)
1579 if (!CONSP (list)
1580 || (CONSP (XCAR (list))
1581 && EQ (XCDR (XCAR (list)), key)))
1582 break;
1584 list = XCDR (list);
1585 if (!CONSP (list)
1586 || (CONSP (XCAR (list))
1587 && EQ (XCDR (XCAR (list)), key)))
1588 break;
1590 list = XCDR (list);
1591 if (!CONSP (list)
1592 || (CONSP (XCAR (list))
1593 && EQ (XCDR (XCAR (list)), key)))
1594 break;
1596 list = XCDR (list);
1597 QUIT;
1600 return CAR (list);
1603 DEFUN ("rassoc", Frassoc, Srassoc, 2, 2, 0,
1604 doc: /* Return non-nil if KEY is `equal' to the cdr of an element of LIST.
1605 The value is actually the first element of LIST whose cdr equals KEY. */)
1606 (key, list)
1607 Lisp_Object key, list;
1609 Lisp_Object cdr;
1611 while (1)
1613 if (!CONSP (list)
1614 || (CONSP (XCAR (list))
1615 && (cdr = XCDR (XCAR (list)),
1616 EQ (cdr, key) || !NILP (Fequal (cdr, key)))))
1617 break;
1619 list = XCDR (list);
1620 if (!CONSP (list)
1621 || (CONSP (XCAR (list))
1622 && (cdr = XCDR (XCAR (list)),
1623 EQ (cdr, key) || !NILP (Fequal (cdr, key)))))
1624 break;
1626 list = XCDR (list);
1627 if (!CONSP (list)
1628 || (CONSP (XCAR (list))
1629 && (cdr = XCDR (XCAR (list)),
1630 EQ (cdr, key) || !NILP (Fequal (cdr, key)))))
1631 break;
1633 list = XCDR (list);
1634 QUIT;
1637 return CAR (list);
1640 DEFUN ("delq", Fdelq, Sdelq, 2, 2, 0,
1641 doc: /* Delete by side effect any occurrences of ELT as a member of LIST.
1642 The modified LIST is returned. Comparison is done with `eq'.
1643 If the first member of LIST is ELT, there is no way to remove it by side effect;
1644 therefore, write `(setq foo (delq element foo))'
1645 to be sure of changing the value of `foo'. */)
1646 (elt, list)
1647 register Lisp_Object elt;
1648 Lisp_Object list;
1650 register Lisp_Object tail, prev;
1651 register Lisp_Object tem;
1653 tail = list;
1654 prev = Qnil;
1655 while (!NILP (tail))
1657 CHECK_LIST_CONS (tail, list);
1658 tem = XCAR (tail);
1659 if (EQ (elt, tem))
1661 if (NILP (prev))
1662 list = XCDR (tail);
1663 else
1664 Fsetcdr (prev, XCDR (tail));
1666 else
1667 prev = tail;
1668 tail = XCDR (tail);
1669 QUIT;
1671 return list;
1674 DEFUN ("delete", Fdelete, Sdelete, 2, 2, 0,
1675 doc: /* Delete by side effect any occurrences of ELT as a member of SEQ.
1676 SEQ must be a list, a vector, or a string.
1677 The modified SEQ is returned. Comparison is done with `equal'.
1678 If SEQ is not a list, or the first member of SEQ is ELT, deleting it
1679 is not a side effect; it is simply using a different sequence.
1680 Therefore, write `(setq foo (delete element foo))'
1681 to be sure of changing the value of `foo'. */)
1682 (elt, seq)
1683 Lisp_Object elt, seq;
1685 if (VECTORP (seq))
1687 EMACS_INT i, n;
1689 for (i = n = 0; i < ASIZE (seq); ++i)
1690 if (NILP (Fequal (AREF (seq, i), elt)))
1691 ++n;
1693 if (n != ASIZE (seq))
1695 struct Lisp_Vector *p = allocate_vector (n);
1697 for (i = n = 0; i < ASIZE (seq); ++i)
1698 if (NILP (Fequal (AREF (seq, i), elt)))
1699 p->contents[n++] = AREF (seq, i);
1701 XSETVECTOR (seq, p);
1704 else if (STRINGP (seq))
1706 EMACS_INT i, ibyte, nchars, nbytes, cbytes;
1707 int c;
1709 for (i = nchars = nbytes = ibyte = 0;
1710 i < SCHARS (seq);
1711 ++i, ibyte += cbytes)
1713 if (STRING_MULTIBYTE (seq))
1715 c = STRING_CHAR (SDATA (seq) + ibyte,
1716 SBYTES (seq) - ibyte);
1717 cbytes = CHAR_BYTES (c);
1719 else
1721 c = SREF (seq, i);
1722 cbytes = 1;
1725 if (!INTEGERP (elt) || c != XINT (elt))
1727 ++nchars;
1728 nbytes += cbytes;
1732 if (nchars != SCHARS (seq))
1734 Lisp_Object tem;
1736 tem = make_uninit_multibyte_string (nchars, nbytes);
1737 if (!STRING_MULTIBYTE (seq))
1738 STRING_SET_UNIBYTE (tem);
1740 for (i = nchars = nbytes = ibyte = 0;
1741 i < SCHARS (seq);
1742 ++i, ibyte += cbytes)
1744 if (STRING_MULTIBYTE (seq))
1746 c = STRING_CHAR (SDATA (seq) + ibyte,
1747 SBYTES (seq) - ibyte);
1748 cbytes = CHAR_BYTES (c);
1750 else
1752 c = SREF (seq, i);
1753 cbytes = 1;
1756 if (!INTEGERP (elt) || c != XINT (elt))
1758 unsigned char *from = SDATA (seq) + ibyte;
1759 unsigned char *to = SDATA (tem) + nbytes;
1760 EMACS_INT n;
1762 ++nchars;
1763 nbytes += cbytes;
1765 for (n = cbytes; n--; )
1766 *to++ = *from++;
1770 seq = tem;
1773 else
1775 Lisp_Object tail, prev;
1777 for (tail = seq, prev = Qnil; CONSP (tail); tail = XCDR (tail))
1779 CHECK_LIST_CONS (tail, seq);
1781 if (!NILP (Fequal (elt, XCAR (tail))))
1783 if (NILP (prev))
1784 seq = XCDR (tail);
1785 else
1786 Fsetcdr (prev, XCDR (tail));
1788 else
1789 prev = tail;
1790 QUIT;
1794 return seq;
1797 DEFUN ("nreverse", Fnreverse, Snreverse, 1, 1, 0,
1798 doc: /* Reverse LIST by modifying cdr pointers.
1799 Return the reversed list. */)
1800 (list)
1801 Lisp_Object list;
1803 register Lisp_Object prev, tail, next;
1805 if (NILP (list)) return list;
1806 prev = Qnil;
1807 tail = list;
1808 while (!NILP (tail))
1810 QUIT;
1811 CHECK_LIST_CONS (tail, list);
1812 next = XCDR (tail);
1813 Fsetcdr (tail, prev);
1814 prev = tail;
1815 tail = next;
1817 return prev;
1820 DEFUN ("reverse", Freverse, Sreverse, 1, 1, 0,
1821 doc: /* Reverse LIST, copying. Return the reversed list.
1822 See also the function `nreverse', which is used more often. */)
1823 (list)
1824 Lisp_Object list;
1826 Lisp_Object new;
1828 for (new = Qnil; CONSP (list); list = XCDR (list))
1830 QUIT;
1831 new = Fcons (XCAR (list), new);
1833 CHECK_LIST_END (list, list);
1834 return new;
1837 Lisp_Object merge ();
1839 DEFUN ("sort", Fsort, Ssort, 2, 2, 0,
1840 doc: /* Sort LIST, stably, comparing elements using PREDICATE.
1841 Returns the sorted list. LIST is modified by side effects.
1842 PREDICATE is called with two elements of LIST, and should return non-nil
1843 if the first element should sort before the second. */)
1844 (list, predicate)
1845 Lisp_Object list, predicate;
1847 Lisp_Object front, back;
1848 register Lisp_Object len, tem;
1849 struct gcpro gcpro1, gcpro2;
1850 register int length;
1852 front = list;
1853 len = Flength (list);
1854 length = XINT (len);
1855 if (length < 2)
1856 return list;
1858 XSETINT (len, (length / 2) - 1);
1859 tem = Fnthcdr (len, list);
1860 back = Fcdr (tem);
1861 Fsetcdr (tem, Qnil);
1863 GCPRO2 (front, back);
1864 front = Fsort (front, predicate);
1865 back = Fsort (back, predicate);
1866 UNGCPRO;
1867 return merge (front, back, predicate);
1870 Lisp_Object
1871 merge (org_l1, org_l2, pred)
1872 Lisp_Object org_l1, org_l2;
1873 Lisp_Object pred;
1875 Lisp_Object value;
1876 register Lisp_Object tail;
1877 Lisp_Object tem;
1878 register Lisp_Object l1, l2;
1879 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1881 l1 = org_l1;
1882 l2 = org_l2;
1883 tail = Qnil;
1884 value = Qnil;
1886 /* It is sufficient to protect org_l1 and org_l2.
1887 When l1 and l2 are updated, we copy the new values
1888 back into the org_ vars. */
1889 GCPRO4 (org_l1, org_l2, pred, value);
1891 while (1)
1893 if (NILP (l1))
1895 UNGCPRO;
1896 if (NILP (tail))
1897 return l2;
1898 Fsetcdr (tail, l2);
1899 return value;
1901 if (NILP (l2))
1903 UNGCPRO;
1904 if (NILP (tail))
1905 return l1;
1906 Fsetcdr (tail, l1);
1907 return value;
1909 tem = call2 (pred, Fcar (l2), Fcar (l1));
1910 if (NILP (tem))
1912 tem = l1;
1913 l1 = Fcdr (l1);
1914 org_l1 = l1;
1916 else
1918 tem = l2;
1919 l2 = Fcdr (l2);
1920 org_l2 = l2;
1922 if (NILP (tail))
1923 value = tem;
1924 else
1925 Fsetcdr (tail, tem);
1926 tail = tem;
1931 #if 0 /* Unsafe version. */
1932 DEFUN ("plist-get", Fplist_get, Splist_get, 2, 2, 0,
1933 doc: /* Extract a value from a property list.
1934 PLIST is a property list, which is a list of the form
1935 \(PROP1 VALUE1 PROP2 VALUE2...). This function returns the value
1936 corresponding to the given PROP, or nil if PROP is not
1937 one of the properties on the list. */)
1938 (plist, prop)
1939 Lisp_Object plist;
1940 Lisp_Object prop;
1942 Lisp_Object tail;
1944 for (tail = plist;
1945 CONSP (tail) && CONSP (XCDR (tail));
1946 tail = XCDR (XCDR (tail)))
1948 if (EQ (prop, XCAR (tail)))
1949 return XCAR (XCDR (tail));
1951 /* This function can be called asynchronously
1952 (setup_coding_system). Don't QUIT in that case. */
1953 if (!interrupt_input_blocked)
1954 QUIT;
1957 CHECK_LIST_END (tail, prop);
1959 return Qnil;
1961 #endif
1963 /* This does not check for quits. That is safe since it must terminate. */
1965 DEFUN ("plist-get", Fplist_get, Splist_get, 2, 2, 0,
1966 doc: /* Extract a value from a property list.
1967 PLIST is a property list, which is a list of the form
1968 \(PROP1 VALUE1 PROP2 VALUE2...). This function returns the value
1969 corresponding to the given PROP, or nil if PROP is not one of the
1970 properties on the list. This function never signals an error. */)
1971 (plist, prop)
1972 Lisp_Object plist;
1973 Lisp_Object prop;
1975 Lisp_Object tail, halftail;
1977 /* halftail is used to detect circular lists. */
1978 tail = halftail = plist;
1979 while (CONSP (tail) && CONSP (XCDR (tail)))
1981 if (EQ (prop, XCAR (tail)))
1982 return XCAR (XCDR (tail));
1984 tail = XCDR (XCDR (tail));
1985 halftail = XCDR (halftail);
1986 if (EQ (tail, halftail))
1987 break;
1990 return Qnil;
1993 DEFUN ("get", Fget, Sget, 2, 2, 0,
1994 doc: /* Return the value of SYMBOL's PROPNAME property.
1995 This is the last value stored with `(put SYMBOL PROPNAME VALUE)'. */)
1996 (symbol, propname)
1997 Lisp_Object symbol, propname;
1999 CHECK_SYMBOL (symbol);
2000 return Fplist_get (XSYMBOL (symbol)->plist, propname);
2003 DEFUN ("plist-put", Fplist_put, Splist_put, 3, 3, 0,
2004 doc: /* Change value in PLIST of PROP to VAL.
2005 PLIST is a property list, which is a list of the form
2006 \(PROP1 VALUE1 PROP2 VALUE2 ...). PROP is a symbol and VAL is any object.
2007 If PROP is already a property on the list, its value is set to VAL,
2008 otherwise the new PROP VAL pair is added. The new plist is returned;
2009 use `(setq x (plist-put x prop val))' to be sure to use the new value.
2010 The PLIST is modified by side effects. */)
2011 (plist, prop, val)
2012 Lisp_Object plist;
2013 register Lisp_Object prop;
2014 Lisp_Object val;
2016 register Lisp_Object tail, prev;
2017 Lisp_Object newcell;
2018 prev = Qnil;
2019 for (tail = plist; CONSP (tail) && CONSP (XCDR (tail));
2020 tail = XCDR (XCDR (tail)))
2022 if (EQ (prop, XCAR (tail)))
2024 Fsetcar (XCDR (tail), val);
2025 return plist;
2028 prev = tail;
2029 QUIT;
2031 newcell = Fcons (prop, Fcons (val, NILP (prev) ? plist : XCDR (XCDR (prev))));
2032 if (NILP (prev))
2033 return newcell;
2034 else
2035 Fsetcdr (XCDR (prev), newcell);
2036 return plist;
2039 DEFUN ("put", Fput, Sput, 3, 3, 0,
2040 doc: /* Store SYMBOL's PROPNAME property with value VALUE.
2041 It can be retrieved with `(get SYMBOL PROPNAME)'. */)
2042 (symbol, propname, value)
2043 Lisp_Object symbol, propname, value;
2045 CHECK_SYMBOL (symbol);
2046 XSYMBOL (symbol)->plist
2047 = Fplist_put (XSYMBOL (symbol)->plist, propname, value);
2048 return value;
2051 DEFUN ("lax-plist-get", Flax_plist_get, Slax_plist_get, 2, 2, 0,
2052 doc: /* Extract a value from a property list, comparing with `equal'.
2053 PLIST is a property list, which is a list of the form
2054 \(PROP1 VALUE1 PROP2 VALUE2...). This function returns the value
2055 corresponding to the given PROP, or nil if PROP is not
2056 one of the properties on the list. */)
2057 (plist, prop)
2058 Lisp_Object plist;
2059 Lisp_Object prop;
2061 Lisp_Object tail;
2063 for (tail = plist;
2064 CONSP (tail) && CONSP (XCDR (tail));
2065 tail = XCDR (XCDR (tail)))
2067 if (! NILP (Fequal (prop, XCAR (tail))))
2068 return XCAR (XCDR (tail));
2070 QUIT;
2073 CHECK_LIST_END (tail, prop);
2075 return Qnil;
2078 DEFUN ("lax-plist-put", Flax_plist_put, Slax_plist_put, 3, 3, 0,
2079 doc: /* Change value in PLIST of PROP to VAL, comparing with `equal'.
2080 PLIST is a property list, which is a list of the form
2081 \(PROP1 VALUE1 PROP2 VALUE2 ...). PROP and VAL are any objects.
2082 If PROP is already a property on the list, its value is set to VAL,
2083 otherwise the new PROP VAL pair is added. The new plist is returned;
2084 use `(setq x (lax-plist-put x prop val))' to be sure to use the new value.
2085 The PLIST is modified by side effects. */)
2086 (plist, prop, val)
2087 Lisp_Object plist;
2088 register Lisp_Object prop;
2089 Lisp_Object val;
2091 register Lisp_Object tail, prev;
2092 Lisp_Object newcell;
2093 prev = Qnil;
2094 for (tail = plist; CONSP (tail) && CONSP (XCDR (tail));
2095 tail = XCDR (XCDR (tail)))
2097 if (! NILP (Fequal (prop, XCAR (tail))))
2099 Fsetcar (XCDR (tail), val);
2100 return plist;
2103 prev = tail;
2104 QUIT;
2106 newcell = Fcons (prop, Fcons (val, Qnil));
2107 if (NILP (prev))
2108 return newcell;
2109 else
2110 Fsetcdr (XCDR (prev), newcell);
2111 return plist;
2114 DEFUN ("eql", Feql, Seql, 2, 2, 0,
2115 doc: /* Return t if the two args are the same Lisp object.
2116 Floating-point numbers of equal value are `eql', but they may not be `eq'. */)
2117 (obj1, obj2)
2118 Lisp_Object obj1, obj2;
2120 if (FLOATP (obj1))
2121 return internal_equal (obj1, obj2, 0, 0) ? Qt : Qnil;
2122 else
2123 return EQ (obj1, obj2) ? Qt : Qnil;
2126 DEFUN ("equal", Fequal, Sequal, 2, 2, 0,
2127 doc: /* Return t if two Lisp objects have similar structure and contents.
2128 They must have the same data type.
2129 Conses are compared by comparing the cars and the cdrs.
2130 Vectors and strings are compared element by element.
2131 Numbers are compared by value, but integers cannot equal floats.
2132 (Use `=' if you want integers and floats to be able to be equal.)
2133 Symbols must match exactly. */)
2134 (o1, o2)
2135 register Lisp_Object o1, o2;
2137 return internal_equal (o1, o2, 0, 0) ? Qt : Qnil;
2140 DEFUN ("equal-including-properties", Fequal_including_properties, Sequal_including_properties, 2, 2, 0,
2141 doc: /* Return t if two Lisp objects have similar structure and contents.
2142 This is like `equal' except that it compares the text properties
2143 of strings. (`equal' ignores text properties.) */)
2144 (o1, o2)
2145 register Lisp_Object o1, o2;
2147 return internal_equal (o1, o2, 0, 1) ? Qt : Qnil;
2150 /* DEPTH is current depth of recursion. Signal an error if it
2151 gets too deep.
2152 PROPS, if non-nil, means compare string text properties too. */
2154 static int
2155 internal_equal (o1, o2, depth, props)
2156 register Lisp_Object o1, o2;
2157 int depth, props;
2159 if (depth > 200)
2160 error ("Stack overflow in equal");
2162 tail_recurse:
2163 QUIT;
2164 if (EQ (o1, o2))
2165 return 1;
2166 if (XTYPE (o1) != XTYPE (o2))
2167 return 0;
2169 switch (XTYPE (o1))
2171 case Lisp_Float:
2173 double d1, d2;
2175 d1 = extract_float (o1);
2176 d2 = extract_float (o2);
2177 /* If d is a NaN, then d != d. Two NaNs should be `equal' even
2178 though they are not =. */
2179 return d1 == d2 || (d1 != d1 && d2 != d2);
2182 case Lisp_Cons:
2183 if (!internal_equal (XCAR (o1), XCAR (o2), depth + 1, props))
2184 return 0;
2185 o1 = XCDR (o1);
2186 o2 = XCDR (o2);
2187 goto tail_recurse;
2189 case Lisp_Misc:
2190 if (XMISCTYPE (o1) != XMISCTYPE (o2))
2191 return 0;
2192 if (OVERLAYP (o1))
2194 if (!internal_equal (OVERLAY_START (o1), OVERLAY_START (o2),
2195 depth + 1, props)
2196 || !internal_equal (OVERLAY_END (o1), OVERLAY_END (o2),
2197 depth + 1, props))
2198 return 0;
2199 o1 = XOVERLAY (o1)->plist;
2200 o2 = XOVERLAY (o2)->plist;
2201 goto tail_recurse;
2203 if (MARKERP (o1))
2205 return (XMARKER (o1)->buffer == XMARKER (o2)->buffer
2206 && (XMARKER (o1)->buffer == 0
2207 || XMARKER (o1)->bytepos == XMARKER (o2)->bytepos));
2209 break;
2211 case Lisp_Vectorlike:
2213 register int i;
2214 EMACS_INT size = ASIZE (o1);
2215 /* Pseudovectors have the type encoded in the size field, so this test
2216 actually checks that the objects have the same type as well as the
2217 same size. */
2218 if (ASIZE (o2) != size)
2219 return 0;
2220 /* Boolvectors are compared much like strings. */
2221 if (BOOL_VECTOR_P (o1))
2223 int size_in_chars
2224 = ((XBOOL_VECTOR (o1)->size + BOOL_VECTOR_BITS_PER_CHAR - 1)
2225 / BOOL_VECTOR_BITS_PER_CHAR);
2227 if (XBOOL_VECTOR (o1)->size != XBOOL_VECTOR (o2)->size)
2228 return 0;
2229 if (bcmp (XBOOL_VECTOR (o1)->data, XBOOL_VECTOR (o2)->data,
2230 size_in_chars))
2231 return 0;
2232 return 1;
2234 if (WINDOW_CONFIGURATIONP (o1))
2235 return compare_window_configurations (o1, o2, 0);
2237 /* Aside from them, only true vectors, char-tables, compiled
2238 functions, and fonts (font-spec, font-entity, font-ojbect)
2239 are sensible to compare, so eliminate the others now. */
2240 if (size & PSEUDOVECTOR_FLAG)
2242 if (!(size & (PVEC_COMPILED
2243 | PVEC_CHAR_TABLE | PVEC_SUB_CHAR_TABLE | PVEC_FONT)))
2244 return 0;
2245 size &= PSEUDOVECTOR_SIZE_MASK;
2247 for (i = 0; i < size; i++)
2249 Lisp_Object v1, v2;
2250 v1 = AREF (o1, i);
2251 v2 = AREF (o2, i);
2252 if (!internal_equal (v1, v2, depth + 1, props))
2253 return 0;
2255 return 1;
2257 break;
2259 case Lisp_String:
2260 if (SCHARS (o1) != SCHARS (o2))
2261 return 0;
2262 if (SBYTES (o1) != SBYTES (o2))
2263 return 0;
2264 if (bcmp (SDATA (o1), SDATA (o2),
2265 SBYTES (o1)))
2266 return 0;
2267 if (props && !compare_string_intervals (o1, o2))
2268 return 0;
2269 return 1;
2271 case Lisp_Int:
2272 case Lisp_Symbol:
2273 case Lisp_Type_Limit:
2274 break;
2277 return 0;
2280 extern Lisp_Object Fmake_char_internal ();
2282 DEFUN ("fillarray", Ffillarray, Sfillarray, 2, 2, 0,
2283 doc: /* Store each element of ARRAY with ITEM.
2284 ARRAY is a vector, string, char-table, or bool-vector. */)
2285 (array, item)
2286 Lisp_Object array, item;
2288 register int size, index, charval;
2289 if (VECTORP (array))
2291 register Lisp_Object *p = XVECTOR (array)->contents;
2292 size = ASIZE (array);
2293 for (index = 0; index < size; index++)
2294 p[index] = item;
2296 else if (CHAR_TABLE_P (array))
2298 int i;
2300 for (i = 0; i < (1 << CHARTAB_SIZE_BITS_0); i++)
2301 XCHAR_TABLE (array)->contents[i] = item;
2302 XCHAR_TABLE (array)->defalt = item;
2304 else if (STRINGP (array))
2306 register unsigned char *p = SDATA (array);
2307 CHECK_NUMBER (item);
2308 charval = XINT (item);
2309 size = SCHARS (array);
2310 if (STRING_MULTIBYTE (array))
2312 unsigned char str[MAX_MULTIBYTE_LENGTH];
2313 int len = CHAR_STRING (charval, str);
2314 int size_byte = SBYTES (array);
2315 unsigned char *p1 = p, *endp = p + size_byte;
2316 int i;
2318 if (size != size_byte)
2319 while (p1 < endp)
2321 int this_len = MULTIBYTE_FORM_LENGTH (p1, endp - p1);
2322 if (len != this_len)
2323 error ("Attempt to change byte length of a string");
2324 p1 += this_len;
2326 for (i = 0; i < size_byte; i++)
2327 *p++ = str[i % len];
2329 else
2330 for (index = 0; index < size; index++)
2331 p[index] = charval;
2333 else if (BOOL_VECTOR_P (array))
2335 register unsigned char *p = XBOOL_VECTOR (array)->data;
2336 int size_in_chars
2337 = ((XBOOL_VECTOR (array)->size + BOOL_VECTOR_BITS_PER_CHAR - 1)
2338 / BOOL_VECTOR_BITS_PER_CHAR);
2340 charval = (! NILP (item) ? -1 : 0);
2341 for (index = 0; index < size_in_chars - 1; index++)
2342 p[index] = charval;
2343 if (index < size_in_chars)
2345 /* Mask out bits beyond the vector size. */
2346 if (XBOOL_VECTOR (array)->size % BOOL_VECTOR_BITS_PER_CHAR)
2347 charval &= (1 << (XBOOL_VECTOR (array)->size % BOOL_VECTOR_BITS_PER_CHAR)) - 1;
2348 p[index] = charval;
2351 else
2352 wrong_type_argument (Qarrayp, array);
2353 return array;
2356 DEFUN ("clear-string", Fclear_string, Sclear_string,
2357 1, 1, 0,
2358 doc: /* Clear the contents of STRING.
2359 This makes STRING unibyte and may change its length. */)
2360 (string)
2361 Lisp_Object string;
2363 int len;
2364 CHECK_STRING (string);
2365 len = SBYTES (string);
2366 bzero (SDATA (string), len);
2367 STRING_SET_CHARS (string, len);
2368 STRING_SET_UNIBYTE (string);
2369 return Qnil;
2372 /* ARGSUSED */
2373 Lisp_Object
2374 nconc2 (s1, s2)
2375 Lisp_Object s1, s2;
2377 #ifdef NO_ARG_ARRAY
2378 Lisp_Object args[2];
2379 args[0] = s1;
2380 args[1] = s2;
2381 return Fnconc (2, args);
2382 #else
2383 return Fnconc (2, &s1);
2384 #endif /* NO_ARG_ARRAY */
2387 DEFUN ("nconc", Fnconc, Snconc, 0, MANY, 0,
2388 doc: /* Concatenate any number of lists by altering them.
2389 Only the last argument is not altered, and need not be a list.
2390 usage: (nconc &rest LISTS) */)
2391 (nargs, args)
2392 int nargs;
2393 Lisp_Object *args;
2395 register int argnum;
2396 register Lisp_Object tail, tem, val;
2398 val = tail = Qnil;
2400 for (argnum = 0; argnum < nargs; argnum++)
2402 tem = args[argnum];
2403 if (NILP (tem)) continue;
2405 if (NILP (val))
2406 val = tem;
2408 if (argnum + 1 == nargs) break;
2410 CHECK_LIST_CONS (tem, tem);
2412 while (CONSP (tem))
2414 tail = tem;
2415 tem = XCDR (tail);
2416 QUIT;
2419 tem = args[argnum + 1];
2420 Fsetcdr (tail, tem);
2421 if (NILP (tem))
2422 args[argnum + 1] = tail;
2425 return val;
2428 /* This is the guts of all mapping functions.
2429 Apply FN to each element of SEQ, one by one,
2430 storing the results into elements of VALS, a C vector of Lisp_Objects.
2431 LENI is the length of VALS, which should also be the length of SEQ. */
2433 static void
2434 mapcar1 (leni, vals, fn, seq)
2435 int leni;
2436 Lisp_Object *vals;
2437 Lisp_Object fn, seq;
2439 register Lisp_Object tail;
2440 Lisp_Object dummy;
2441 register int i;
2442 struct gcpro gcpro1, gcpro2, gcpro3;
2444 if (vals)
2446 /* Don't let vals contain any garbage when GC happens. */
2447 for (i = 0; i < leni; i++)
2448 vals[i] = Qnil;
2450 GCPRO3 (dummy, fn, seq);
2451 gcpro1.var = vals;
2452 gcpro1.nvars = leni;
2454 else
2455 GCPRO2 (fn, seq);
2456 /* We need not explicitly protect `tail' because it is used only on lists, and
2457 1) lists are not relocated and 2) the list is marked via `seq' so will not
2458 be freed */
2460 if (VECTORP (seq))
2462 for (i = 0; i < leni; i++)
2464 dummy = call1 (fn, AREF (seq, i));
2465 if (vals)
2466 vals[i] = dummy;
2469 else if (BOOL_VECTOR_P (seq))
2471 for (i = 0; i < leni; i++)
2473 int byte;
2474 byte = XBOOL_VECTOR (seq)->data[i / BOOL_VECTOR_BITS_PER_CHAR];
2475 dummy = (byte & (1 << (i % BOOL_VECTOR_BITS_PER_CHAR))) ? Qt : Qnil;
2476 dummy = call1 (fn, dummy);
2477 if (vals)
2478 vals[i] = dummy;
2481 else if (STRINGP (seq))
2483 int i_byte;
2485 for (i = 0, i_byte = 0; i < leni;)
2487 int c;
2488 int i_before = i;
2490 FETCH_STRING_CHAR_ADVANCE (c, seq, i, i_byte);
2491 XSETFASTINT (dummy, c);
2492 dummy = call1 (fn, dummy);
2493 if (vals)
2494 vals[i_before] = dummy;
2497 else /* Must be a list, since Flength did not get an error */
2499 tail = seq;
2500 for (i = 0; i < leni && CONSP (tail); i++)
2502 dummy = call1 (fn, XCAR (tail));
2503 if (vals)
2504 vals[i] = dummy;
2505 tail = XCDR (tail);
2509 UNGCPRO;
2512 DEFUN ("mapconcat", Fmapconcat, Smapconcat, 3, 3, 0,
2513 doc: /* Apply FUNCTION to each element of SEQUENCE, and concat the results as strings.
2514 In between each pair of results, stick in SEPARATOR. Thus, " " as
2515 SEPARATOR results in spaces between the values returned by FUNCTION.
2516 SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
2517 (function, sequence, separator)
2518 Lisp_Object function, sequence, separator;
2520 Lisp_Object len;
2521 register int leni;
2522 int nargs;
2523 register Lisp_Object *args;
2524 register int i;
2525 struct gcpro gcpro1;
2526 Lisp_Object ret;
2527 USE_SAFE_ALLOCA;
2529 len = Flength (sequence);
2530 if (CHAR_TABLE_P (sequence))
2531 wrong_type_argument (Qlistp, sequence);
2532 leni = XINT (len);
2533 nargs = leni + leni - 1;
2534 if (nargs < 0) return empty_unibyte_string;
2536 SAFE_ALLOCA_LISP (args, nargs);
2538 GCPRO1 (separator);
2539 mapcar1 (leni, args, function, sequence);
2540 UNGCPRO;
2542 for (i = leni - 1; i > 0; i--)
2543 args[i + i] = args[i];
2545 for (i = 1; i < nargs; i += 2)
2546 args[i] = separator;
2548 ret = Fconcat (nargs, args);
2549 SAFE_FREE ();
2551 return ret;
2554 DEFUN ("mapcar", Fmapcar, Smapcar, 2, 2, 0,
2555 doc: /* Apply FUNCTION to each element of SEQUENCE, and make a list of the results.
2556 The result is a list just as long as SEQUENCE.
2557 SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
2558 (function, sequence)
2559 Lisp_Object function, sequence;
2561 register Lisp_Object len;
2562 register int leni;
2563 register Lisp_Object *args;
2564 Lisp_Object ret;
2565 USE_SAFE_ALLOCA;
2567 len = Flength (sequence);
2568 if (CHAR_TABLE_P (sequence))
2569 wrong_type_argument (Qlistp, sequence);
2570 leni = XFASTINT (len);
2572 SAFE_ALLOCA_LISP (args, leni);
2574 mapcar1 (leni, args, function, sequence);
2576 ret = Flist (leni, args);
2577 SAFE_FREE ();
2579 return ret;
2582 DEFUN ("mapc", Fmapc, Smapc, 2, 2, 0,
2583 doc: /* Apply FUNCTION to each element of SEQUENCE for side effects only.
2584 Unlike `mapcar', don't accumulate the results. Return SEQUENCE.
2585 SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
2586 (function, sequence)
2587 Lisp_Object function, sequence;
2589 register int leni;
2591 leni = XFASTINT (Flength (sequence));
2592 if (CHAR_TABLE_P (sequence))
2593 wrong_type_argument (Qlistp, sequence);
2594 mapcar1 (leni, 0, function, sequence);
2596 return sequence;
2599 /* Anything that calls this function must protect from GC! */
2601 DEFUN ("y-or-n-p", Fy_or_n_p, Sy_or_n_p, 1, 1, 0,
2602 doc: /* Ask user a "y or n" question. Return t if answer is "y".
2603 Takes one argument, which is the string to display to ask the question.
2604 It should end in a space; `y-or-n-p' adds `(y or n) ' to it.
2605 No confirmation of the answer is requested; a single character is enough.
2606 Also accepts Space to mean yes, or Delete to mean no. \(Actually, it uses
2607 the bindings in `query-replace-map'; see the documentation of that variable
2608 for more information. In this case, the useful bindings are `act', `skip',
2609 `recenter', and `quit'.\)
2611 Under a windowing system a dialog box will be used if `last-nonmenu-event'
2612 is nil and `use-dialog-box' is non-nil. */)
2613 (prompt)
2614 Lisp_Object prompt;
2616 register Lisp_Object obj, key, def, map;
2617 register int answer;
2618 Lisp_Object xprompt;
2619 Lisp_Object args[2];
2620 struct gcpro gcpro1, gcpro2;
2621 int count = SPECPDL_INDEX ();
2623 specbind (Qcursor_in_echo_area, Qt);
2625 map = Fsymbol_value (intern ("query-replace-map"));
2627 CHECK_STRING (prompt);
2628 xprompt = prompt;
2629 GCPRO2 (prompt, xprompt);
2631 #ifdef HAVE_WINDOW_SYSTEM
2632 if (display_hourglass_p)
2633 cancel_hourglass ();
2634 #endif
2636 while (1)
2639 #ifdef HAVE_MENUS
2640 if (FRAME_WINDOW_P (SELECTED_FRAME ())
2641 && (NILP (last_nonmenu_event) || CONSP (last_nonmenu_event))
2642 && use_dialog_box
2643 && have_menus_p ())
2645 Lisp_Object pane, menu;
2646 redisplay_preserve_echo_area (3);
2647 pane = Fcons (Fcons (build_string ("Yes"), Qt),
2648 Fcons (Fcons (build_string ("No"), Qnil),
2649 Qnil));
2650 menu = Fcons (prompt, pane);
2651 obj = Fx_popup_dialog (Qt, menu, Qnil);
2652 answer = !NILP (obj);
2653 break;
2655 #endif /* HAVE_MENUS */
2656 cursor_in_echo_area = 1;
2657 choose_minibuf_frame ();
2660 Lisp_Object pargs[3];
2662 /* Colorize prompt according to `minibuffer-prompt' face. */
2663 pargs[0] = build_string ("%s(y or n) ");
2664 pargs[1] = intern ("face");
2665 pargs[2] = intern ("minibuffer-prompt");
2666 args[0] = Fpropertize (3, pargs);
2667 args[1] = xprompt;
2668 Fmessage (2, args);
2671 if (minibuffer_auto_raise)
2673 Lisp_Object mini_frame;
2675 mini_frame = WINDOW_FRAME (XWINDOW (minibuf_window));
2677 Fraise_frame (mini_frame);
2680 temporarily_switch_to_single_kboard (SELECTED_FRAME ());
2681 obj = read_filtered_event (1, 0, 0, 0, Qnil);
2682 cursor_in_echo_area = 0;
2683 /* If we need to quit, quit with cursor_in_echo_area = 0. */
2684 QUIT;
2686 key = Fmake_vector (make_number (1), obj);
2687 def = Flookup_key (map, key, Qt);
2689 if (EQ (def, intern ("skip")))
2691 answer = 0;
2692 break;
2694 else if (EQ (def, intern ("act")))
2696 answer = 1;
2697 break;
2699 else if (EQ (def, intern ("recenter")))
2701 Frecenter (Qnil);
2702 xprompt = prompt;
2703 continue;
2705 else if (EQ (def, intern ("quit")))
2706 Vquit_flag = Qt;
2707 /* We want to exit this command for exit-prefix,
2708 and this is the only way to do it. */
2709 else if (EQ (def, intern ("exit-prefix")))
2710 Vquit_flag = Qt;
2712 QUIT;
2714 /* If we don't clear this, then the next call to read_char will
2715 return quit_char again, and we'll enter an infinite loop. */
2716 Vquit_flag = Qnil;
2718 Fding (Qnil);
2719 Fdiscard_input ();
2720 if (EQ (xprompt, prompt))
2722 args[0] = build_string ("Please answer y or n. ");
2723 args[1] = prompt;
2724 xprompt = Fconcat (2, args);
2727 UNGCPRO;
2729 if (! noninteractive)
2731 cursor_in_echo_area = -1;
2732 message_with_string (answer ? "%s(y or n) y" : "%s(y or n) n",
2733 xprompt, 0);
2736 unbind_to (count, Qnil);
2737 return answer ? Qt : Qnil;
2740 /* This is how C code calls `yes-or-no-p' and allows the user
2741 to redefined it.
2743 Anything that calls this function must protect from GC! */
2745 Lisp_Object
2746 do_yes_or_no_p (prompt)
2747 Lisp_Object prompt;
2749 return call1 (intern ("yes-or-no-p"), prompt);
2752 /* Anything that calls this function must protect from GC! */
2754 DEFUN ("yes-or-no-p", Fyes_or_no_p, Syes_or_no_p, 1, 1, 0,
2755 doc: /* Ask user a yes-or-no question. Return t if answer is yes.
2756 Takes one argument, which is the string to display to ask the question.
2757 It should end in a space; `yes-or-no-p' adds `(yes or no) ' to it.
2758 The user must confirm the answer with RET,
2759 and can edit it until it has been confirmed.
2761 Under a windowing system a dialog box will be used if `last-nonmenu-event'
2762 is nil, and `use-dialog-box' is non-nil. */)
2763 (prompt)
2764 Lisp_Object prompt;
2766 register Lisp_Object ans;
2767 Lisp_Object args[2];
2768 struct gcpro gcpro1;
2770 CHECK_STRING (prompt);
2772 #ifdef HAVE_MENUS
2773 if (FRAME_WINDOW_P (SELECTED_FRAME ())
2774 && (NILP (last_nonmenu_event) || CONSP (last_nonmenu_event))
2775 && use_dialog_box
2776 && have_menus_p ())
2778 Lisp_Object pane, menu, obj;
2779 redisplay_preserve_echo_area (4);
2780 pane = Fcons (Fcons (build_string ("Yes"), Qt),
2781 Fcons (Fcons (build_string ("No"), Qnil),
2782 Qnil));
2783 GCPRO1 (pane);
2784 menu = Fcons (prompt, pane);
2785 obj = Fx_popup_dialog (Qt, menu, Qnil);
2786 UNGCPRO;
2787 return obj;
2789 #endif /* HAVE_MENUS */
2791 args[0] = prompt;
2792 args[1] = build_string ("(yes or no) ");
2793 prompt = Fconcat (2, args);
2795 GCPRO1 (prompt);
2797 while (1)
2799 ans = Fdowncase (Fread_from_minibuffer (prompt, Qnil, Qnil, Qnil,
2800 Qyes_or_no_p_history, Qnil,
2801 Qnil));
2802 if (SCHARS (ans) == 3 && !strcmp (SDATA (ans), "yes"))
2804 UNGCPRO;
2805 return Qt;
2807 if (SCHARS (ans) == 2 && !strcmp (SDATA (ans), "no"))
2809 UNGCPRO;
2810 return Qnil;
2813 Fding (Qnil);
2814 Fdiscard_input ();
2815 message ("Please answer yes or no.");
2816 Fsleep_for (make_number (2), Qnil);
2820 DEFUN ("load-average", Fload_average, Sload_average, 0, 1, 0,
2821 doc: /* Return list of 1 minute, 5 minute and 15 minute load averages.
2823 Each of the three load averages is multiplied by 100, then converted
2824 to integer.
2826 When USE-FLOATS is non-nil, floats will be used instead of integers.
2827 These floats are not multiplied by 100.
2829 If the 5-minute or 15-minute load averages are not available, return a
2830 shortened list, containing only those averages which are available.
2832 An error is thrown if the load average can't be obtained. In some
2833 cases making it work would require Emacs being installed setuid or
2834 setgid so that it can read kernel information, and that usually isn't
2835 advisable. */)
2836 (use_floats)
2837 Lisp_Object use_floats;
2839 double load_ave[3];
2840 int loads = getloadavg (load_ave, 3);
2841 Lisp_Object ret = Qnil;
2843 if (loads < 0)
2844 error ("load-average not implemented for this operating system");
2846 while (loads-- > 0)
2848 Lisp_Object load = (NILP (use_floats) ?
2849 make_number ((int) (100.0 * load_ave[loads]))
2850 : make_float (load_ave[loads]));
2851 ret = Fcons (load, ret);
2854 return ret;
2857 Lisp_Object Vfeatures, Qsubfeatures;
2858 extern Lisp_Object Vafter_load_alist;
2860 DEFUN ("featurep", Ffeaturep, Sfeaturep, 1, 2, 0,
2861 doc: /* Returns t if FEATURE is present in this Emacs.
2863 Use this to conditionalize execution of lisp code based on the
2864 presence or absence of Emacs or environment extensions.
2865 Use `provide' to declare that a feature is available. This function
2866 looks at the value of the variable `features'. The optional argument
2867 SUBFEATURE can be used to check a specific subfeature of FEATURE. */)
2868 (feature, subfeature)
2869 Lisp_Object feature, subfeature;
2871 register Lisp_Object tem;
2872 CHECK_SYMBOL (feature);
2873 tem = Fmemq (feature, Vfeatures);
2874 if (!NILP (tem) && !NILP (subfeature))
2875 tem = Fmember (subfeature, Fget (feature, Qsubfeatures));
2876 return (NILP (tem)) ? Qnil : Qt;
2879 DEFUN ("provide", Fprovide, Sprovide, 1, 2, 0,
2880 doc: /* Announce that FEATURE is a feature of the current Emacs.
2881 The optional argument SUBFEATURES should be a list of symbols listing
2882 particular subfeatures supported in this version of FEATURE. */)
2883 (feature, subfeatures)
2884 Lisp_Object feature, subfeatures;
2886 register Lisp_Object tem;
2887 CHECK_SYMBOL (feature);
2888 CHECK_LIST (subfeatures);
2889 if (!NILP (Vautoload_queue))
2890 Vautoload_queue = Fcons (Fcons (make_number (0), Vfeatures),
2891 Vautoload_queue);
2892 tem = Fmemq (feature, Vfeatures);
2893 if (NILP (tem))
2894 Vfeatures = Fcons (feature, Vfeatures);
2895 if (!NILP (subfeatures))
2896 Fput (feature, Qsubfeatures, subfeatures);
2897 LOADHIST_ATTACH (Fcons (Qprovide, feature));
2899 /* Run any load-hooks for this file. */
2900 tem = Fassq (feature, Vafter_load_alist);
2901 if (CONSP (tem))
2902 Fprogn (XCDR (tem));
2904 return feature;
2907 /* `require' and its subroutines. */
2909 /* List of features currently being require'd, innermost first. */
2911 Lisp_Object require_nesting_list;
2913 Lisp_Object
2914 require_unwind (old_value)
2915 Lisp_Object old_value;
2917 return require_nesting_list = old_value;
2920 DEFUN ("require", Frequire, Srequire, 1, 3, 0,
2921 doc: /* If feature FEATURE is not loaded, load it from FILENAME.
2922 If FEATURE is not a member of the list `features', then the feature
2923 is not loaded; so load the file FILENAME.
2924 If FILENAME is omitted, the printname of FEATURE is used as the file name,
2925 and `load' will try to load this name appended with the suffix `.elc' or
2926 `.el', in that order. The name without appended suffix will not be used.
2927 If the optional third argument NOERROR is non-nil,
2928 then return nil if the file is not found instead of signaling an error.
2929 Normally the return value is FEATURE.
2930 The normal messages at start and end of loading FILENAME are suppressed. */)
2931 (feature, filename, noerror)
2932 Lisp_Object feature, filename, noerror;
2934 register Lisp_Object tem;
2935 struct gcpro gcpro1, gcpro2;
2936 int from_file = load_in_progress;
2938 CHECK_SYMBOL (feature);
2940 /* Record the presence of `require' in this file
2941 even if the feature specified is already loaded.
2942 But not more than once in any file,
2943 and not when we aren't loading or reading from a file. */
2944 if (!from_file)
2945 for (tem = Vcurrent_load_list; CONSP (tem); tem = XCDR (tem))
2946 if (NILP (XCDR (tem)) && STRINGP (XCAR (tem)))
2947 from_file = 1;
2949 if (from_file)
2951 tem = Fcons (Qrequire, feature);
2952 if (NILP (Fmember (tem, Vcurrent_load_list)))
2953 LOADHIST_ATTACH (tem);
2955 tem = Fmemq (feature, Vfeatures);
2957 if (NILP (tem))
2959 int count = SPECPDL_INDEX ();
2960 int nesting = 0;
2962 /* This is to make sure that loadup.el gives a clear picture
2963 of what files are preloaded and when. */
2964 if (! NILP (Vpurify_flag))
2965 error ("(require %s) while preparing to dump",
2966 SDATA (SYMBOL_NAME (feature)));
2968 /* A certain amount of recursive `require' is legitimate,
2969 but if we require the same feature recursively 3 times,
2970 signal an error. */
2971 tem = require_nesting_list;
2972 while (! NILP (tem))
2974 if (! NILP (Fequal (feature, XCAR (tem))))
2975 nesting++;
2976 tem = XCDR (tem);
2978 if (nesting > 3)
2979 error ("Recursive `require' for feature `%s'",
2980 SDATA (SYMBOL_NAME (feature)));
2982 /* Update the list for any nested `require's that occur. */
2983 record_unwind_protect (require_unwind, require_nesting_list);
2984 require_nesting_list = Fcons (feature, require_nesting_list);
2986 /* Value saved here is to be restored into Vautoload_queue */
2987 record_unwind_protect (un_autoload, Vautoload_queue);
2988 Vautoload_queue = Qt;
2990 /* Load the file. */
2991 GCPRO2 (feature, filename);
2992 tem = Fload (NILP (filename) ? Fsymbol_name (feature) : filename,
2993 noerror, Qt, Qnil, (NILP (filename) ? Qt : Qnil));
2994 UNGCPRO;
2996 /* If load failed entirely, return nil. */
2997 if (NILP (tem))
2998 return unbind_to (count, Qnil);
3000 tem = Fmemq (feature, Vfeatures);
3001 if (NILP (tem))
3002 error ("Required feature `%s' was not provided",
3003 SDATA (SYMBOL_NAME (feature)));
3005 /* Once loading finishes, don't undo it. */
3006 Vautoload_queue = Qt;
3007 feature = unbind_to (count, feature);
3010 return feature;
3013 /* Primitives for work of the "widget" library.
3014 In an ideal world, this section would not have been necessary.
3015 However, lisp function calls being as slow as they are, it turns
3016 out that some functions in the widget library (wid-edit.el) are the
3017 bottleneck of Widget operation. Here is their translation to C,
3018 for the sole reason of efficiency. */
3020 DEFUN ("plist-member", Fplist_member, Splist_member, 2, 2, 0,
3021 doc: /* Return non-nil if PLIST has the property PROP.
3022 PLIST is a property list, which is a list of the form
3023 \(PROP1 VALUE1 PROP2 VALUE2 ...\). PROP is a symbol.
3024 Unlike `plist-get', this allows you to distinguish between a missing
3025 property and a property with the value nil.
3026 The value is actually the tail of PLIST whose car is PROP. */)
3027 (plist, prop)
3028 Lisp_Object plist, prop;
3030 while (CONSP (plist) && !EQ (XCAR (plist), prop))
3032 QUIT;
3033 plist = XCDR (plist);
3034 plist = CDR (plist);
3036 return plist;
3039 DEFUN ("widget-put", Fwidget_put, Swidget_put, 3, 3, 0,
3040 doc: /* In WIDGET, set PROPERTY to VALUE.
3041 The value can later be retrieved with `widget-get'. */)
3042 (widget, property, value)
3043 Lisp_Object widget, property, value;
3045 CHECK_CONS (widget);
3046 XSETCDR (widget, Fplist_put (XCDR (widget), property, value));
3047 return value;
3050 DEFUN ("widget-get", Fwidget_get, Swidget_get, 2, 2, 0,
3051 doc: /* In WIDGET, get the value of PROPERTY.
3052 The value could either be specified when the widget was created, or
3053 later with `widget-put'. */)
3054 (widget, property)
3055 Lisp_Object widget, property;
3057 Lisp_Object tmp;
3059 while (1)
3061 if (NILP (widget))
3062 return Qnil;
3063 CHECK_CONS (widget);
3064 tmp = Fplist_member (XCDR (widget), property);
3065 if (CONSP (tmp))
3067 tmp = XCDR (tmp);
3068 return CAR (tmp);
3070 tmp = XCAR (widget);
3071 if (NILP (tmp))
3072 return Qnil;
3073 widget = Fget (tmp, Qwidget_type);
3077 DEFUN ("widget-apply", Fwidget_apply, Swidget_apply, 2, MANY, 0,
3078 doc: /* Apply the value of WIDGET's PROPERTY to the widget itself.
3079 ARGS are passed as extra arguments to the function.
3080 usage: (widget-apply WIDGET PROPERTY &rest ARGS) */)
3081 (nargs, args)
3082 int nargs;
3083 Lisp_Object *args;
3085 /* This function can GC. */
3086 Lisp_Object newargs[3];
3087 struct gcpro gcpro1, gcpro2;
3088 Lisp_Object result;
3090 newargs[0] = Fwidget_get (args[0], args[1]);
3091 newargs[1] = args[0];
3092 newargs[2] = Flist (nargs - 2, args + 2);
3093 GCPRO2 (newargs[0], newargs[2]);
3094 result = Fapply (3, newargs);
3095 UNGCPRO;
3096 return result;
3099 #ifdef HAVE_LANGINFO_CODESET
3100 #include <langinfo.h>
3101 #endif
3103 DEFUN ("locale-info", Flocale_info, Slocale_info, 1, 1, 0,
3104 doc: /* Access locale data ITEM for the current C locale, if available.
3105 ITEM should be one of the following:
3107 `codeset', returning the character set as a string (locale item CODESET);
3109 `days', returning a 7-element vector of day names (locale items DAY_n);
3111 `months', returning a 12-element vector of month names (locale items MON_n);
3113 `paper', returning a list (WIDTH HEIGHT) for the default paper size,
3114 both measured in milimeters (locale items PAPER_WIDTH, PAPER_HEIGHT).
3116 If the system can't provide such information through a call to
3117 `nl_langinfo', or if ITEM isn't from the list above, return nil.
3119 See also Info node `(libc)Locales'.
3121 The data read from the system are decoded using `locale-coding-system'. */)
3122 (item)
3123 Lisp_Object item;
3125 char *str = NULL;
3126 #ifdef HAVE_LANGINFO_CODESET
3127 Lisp_Object val;
3128 if (EQ (item, Qcodeset))
3130 str = nl_langinfo (CODESET);
3131 return build_string (str);
3133 #ifdef DAY_1
3134 else if (EQ (item, Qdays)) /* e.g. for calendar-day-name-array */
3136 Lisp_Object v = Fmake_vector (make_number (7), Qnil);
3137 int days[7] = {DAY_1, DAY_2, DAY_3, DAY_4, DAY_5, DAY_6, DAY_7};
3138 int i;
3139 synchronize_system_time_locale ();
3140 for (i = 0; i < 7; i++)
3142 str = nl_langinfo (days[i]);
3143 val = make_unibyte_string (str, strlen (str));
3144 /* Fixme: Is this coding system necessarily right, even if
3145 it is consistent with CODESET? If not, what to do? */
3146 Faset (v, make_number (i),
3147 code_convert_string_norecord (val, Vlocale_coding_system,
3148 0));
3150 return v;
3152 #endif /* DAY_1 */
3153 #ifdef MON_1
3154 else if (EQ (item, Qmonths)) /* e.g. for calendar-month-name-array */
3156 struct Lisp_Vector *p = allocate_vector (12);
3157 int months[12] = {MON_1, MON_2, MON_3, MON_4, MON_5, MON_6, MON_7,
3158 MON_8, MON_9, MON_10, MON_11, MON_12};
3159 int i;
3160 synchronize_system_time_locale ();
3161 for (i = 0; i < 12; i++)
3163 str = nl_langinfo (months[i]);
3164 val = make_unibyte_string (str, strlen (str));
3165 p->contents[i] =
3166 code_convert_string_norecord (val, Vlocale_coding_system, 0);
3168 XSETVECTOR (val, p);
3169 return val;
3171 #endif /* MON_1 */
3172 /* LC_PAPER stuff isn't defined as accessible in glibc as of 2.3.1,
3173 but is in the locale files. This could be used by ps-print. */
3174 #ifdef PAPER_WIDTH
3175 else if (EQ (item, Qpaper))
3177 return list2 (make_number (nl_langinfo (PAPER_WIDTH)),
3178 make_number (nl_langinfo (PAPER_HEIGHT)));
3180 #endif /* PAPER_WIDTH */
3181 #endif /* HAVE_LANGINFO_CODESET*/
3182 return Qnil;
3185 /* base64 encode/decode functions (RFC 2045).
3186 Based on code from GNU recode. */
3188 #define MIME_LINE_LENGTH 76
3190 #define IS_ASCII(Character) \
3191 ((Character) < 128)
3192 #define IS_BASE64(Character) \
3193 (IS_ASCII (Character) && base64_char_to_value[Character] >= 0)
3194 #define IS_BASE64_IGNORABLE(Character) \
3195 ((Character) == ' ' || (Character) == '\t' || (Character) == '\n' \
3196 || (Character) == '\f' || (Character) == '\r')
3198 /* Used by base64_decode_1 to retrieve a non-base64-ignorable
3199 character or return retval if there are no characters left to
3200 process. */
3201 #define READ_QUADRUPLET_BYTE(retval) \
3202 do \
3204 if (i == length) \
3206 if (nchars_return) \
3207 *nchars_return = nchars; \
3208 return (retval); \
3210 c = from[i++]; \
3212 while (IS_BASE64_IGNORABLE (c))
3214 /* Table of characters coding the 64 values. */
3215 static char base64_value_to_char[64] =
3217 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 0- 9 */
3218 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 10-19 */
3219 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', /* 20-29 */
3220 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', /* 30-39 */
3221 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', /* 40-49 */
3222 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', /* 50-59 */
3223 '8', '9', '+', '/' /* 60-63 */
3226 /* Table of base64 values for first 128 characters. */
3227 static short base64_char_to_value[128] =
3229 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0- 9 */
3230 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10- 19 */
3231 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20- 29 */
3232 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 30- 39 */
3233 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, /* 40- 49 */
3234 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, /* 50- 59 */
3235 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, /* 60- 69 */
3236 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 70- 79 */
3237 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, /* 80- 89 */
3238 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, /* 90- 99 */
3239 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, /* 100-109 */
3240 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, /* 110-119 */
3241 49, 50, 51, -1, -1, -1, -1, -1 /* 120-127 */
3244 /* The following diagram shows the logical steps by which three octets
3245 get transformed into four base64 characters.
3247 .--------. .--------. .--------.
3248 |aaaaaabb| |bbbbcccc| |ccdddddd|
3249 `--------' `--------' `--------'
3250 6 2 4 4 2 6
3251 .--------+--------+--------+--------.
3252 |00aaaaaa|00bbbbbb|00cccccc|00dddddd|
3253 `--------+--------+--------+--------'
3255 .--------+--------+--------+--------.
3256 |AAAAAAAA|BBBBBBBB|CCCCCCCC|DDDDDDDD|
3257 `--------+--------+--------+--------'
3259 The octets are divided into 6 bit chunks, which are then encoded into
3260 base64 characters. */
3263 static int base64_encode_1 P_ ((const char *, char *, int, int, int));
3264 static int base64_decode_1 P_ ((const char *, char *, int, int, int *));
3266 DEFUN ("base64-encode-region", Fbase64_encode_region, Sbase64_encode_region,
3267 2, 3, "r",
3268 doc: /* Base64-encode the region between BEG and END.
3269 Return the length of the encoded text.
3270 Optional third argument NO-LINE-BREAK means do not break long lines
3271 into shorter lines. */)
3272 (beg, end, no_line_break)
3273 Lisp_Object beg, end, no_line_break;
3275 char *encoded;
3276 int allength, length;
3277 int ibeg, iend, encoded_length;
3278 int old_pos = PT;
3279 USE_SAFE_ALLOCA;
3281 validate_region (&beg, &end);
3283 ibeg = CHAR_TO_BYTE (XFASTINT (beg));
3284 iend = CHAR_TO_BYTE (XFASTINT (end));
3285 move_gap_both (XFASTINT (beg), ibeg);
3287 /* We need to allocate enough room for encoding the text.
3288 We need 33 1/3% more space, plus a newline every 76
3289 characters, and then we round up. */
3290 length = iend - ibeg;
3291 allength = length + length/3 + 1;
3292 allength += allength / MIME_LINE_LENGTH + 1 + 6;
3294 SAFE_ALLOCA (encoded, char *, allength);
3295 encoded_length = base64_encode_1 (BYTE_POS_ADDR (ibeg), encoded, length,
3296 NILP (no_line_break),
3297 !NILP (current_buffer->enable_multibyte_characters));
3298 if (encoded_length > allength)
3299 abort ();
3301 if (encoded_length < 0)
3303 /* The encoding wasn't possible. */
3304 SAFE_FREE ();
3305 error ("Multibyte character in data for base64 encoding");
3308 /* Now we have encoded the region, so we insert the new contents
3309 and delete the old. (Insert first in order to preserve markers.) */
3310 SET_PT_BOTH (XFASTINT (beg), ibeg);
3311 insert (encoded, encoded_length);
3312 SAFE_FREE ();
3313 del_range_byte (ibeg + encoded_length, iend + encoded_length, 1);
3315 /* If point was outside of the region, restore it exactly; else just
3316 move to the beginning of the region. */
3317 if (old_pos >= XFASTINT (end))
3318 old_pos += encoded_length - (XFASTINT (end) - XFASTINT (beg));
3319 else if (old_pos > XFASTINT (beg))
3320 old_pos = XFASTINT (beg);
3321 SET_PT (old_pos);
3323 /* We return the length of the encoded text. */
3324 return make_number (encoded_length);
3327 DEFUN ("base64-encode-string", Fbase64_encode_string, Sbase64_encode_string,
3328 1, 2, 0,
3329 doc: /* Base64-encode STRING and return the result.
3330 Optional second argument NO-LINE-BREAK means do not break long lines
3331 into shorter lines. */)
3332 (string, no_line_break)
3333 Lisp_Object string, no_line_break;
3335 int allength, length, encoded_length;
3336 char *encoded;
3337 Lisp_Object encoded_string;
3338 USE_SAFE_ALLOCA;
3340 CHECK_STRING (string);
3342 /* We need to allocate enough room for encoding the text.
3343 We need 33 1/3% more space, plus a newline every 76
3344 characters, and then we round up. */
3345 length = SBYTES (string);
3346 allength = length + length/3 + 1;
3347 allength += allength / MIME_LINE_LENGTH + 1 + 6;
3349 /* We need to allocate enough room for decoding the text. */
3350 SAFE_ALLOCA (encoded, char *, allength);
3352 encoded_length = base64_encode_1 (SDATA (string),
3353 encoded, length, NILP (no_line_break),
3354 STRING_MULTIBYTE (string));
3355 if (encoded_length > allength)
3356 abort ();
3358 if (encoded_length < 0)
3360 /* The encoding wasn't possible. */
3361 SAFE_FREE ();
3362 error ("Multibyte character in data for base64 encoding");
3365 encoded_string = make_unibyte_string (encoded, encoded_length);
3366 SAFE_FREE ();
3368 return encoded_string;
3371 static int
3372 base64_encode_1 (from, to, length, line_break, multibyte)
3373 const char *from;
3374 char *to;
3375 int length;
3376 int line_break;
3377 int multibyte;
3379 int counter = 0, i = 0;
3380 char *e = to;
3381 int c;
3382 unsigned int value;
3383 int bytes;
3385 while (i < length)
3387 if (multibyte)
3389 c = STRING_CHAR_AND_LENGTH (from + i, length - i, bytes);
3390 if (CHAR_BYTE8_P (c))
3391 c = CHAR_TO_BYTE8 (c);
3392 else if (c >= 256)
3393 return -1;
3394 i += bytes;
3396 else
3397 c = from[i++];
3399 /* Wrap line every 76 characters. */
3401 if (line_break)
3403 if (counter < MIME_LINE_LENGTH / 4)
3404 counter++;
3405 else
3407 *e++ = '\n';
3408 counter = 1;
3412 /* Process first byte of a triplet. */
3414 *e++ = base64_value_to_char[0x3f & c >> 2];
3415 value = (0x03 & c) << 4;
3417 /* Process second byte of a triplet. */
3419 if (i == length)
3421 *e++ = base64_value_to_char[value];
3422 *e++ = '=';
3423 *e++ = '=';
3424 break;
3427 if (multibyte)
3429 c = STRING_CHAR_AND_LENGTH (from + i, length - i, bytes);
3430 if (CHAR_BYTE8_P (c))
3431 c = CHAR_TO_BYTE8 (c);
3432 else if (c >= 256)
3433 return -1;
3434 i += bytes;
3436 else
3437 c = from[i++];
3439 *e++ = base64_value_to_char[value | (0x0f & c >> 4)];
3440 value = (0x0f & c) << 2;
3442 /* Process third byte of a triplet. */
3444 if (i == length)
3446 *e++ = base64_value_to_char[value];
3447 *e++ = '=';
3448 break;
3451 if (multibyte)
3453 c = STRING_CHAR_AND_LENGTH (from + i, length - i, bytes);
3454 if (CHAR_BYTE8_P (c))
3455 c = CHAR_TO_BYTE8 (c);
3456 else if (c >= 256)
3457 return -1;
3458 i += bytes;
3460 else
3461 c = from[i++];
3463 *e++ = base64_value_to_char[value | (0x03 & c >> 6)];
3464 *e++ = base64_value_to_char[0x3f & c];
3467 return e - to;
3471 DEFUN ("base64-decode-region", Fbase64_decode_region, Sbase64_decode_region,
3472 2, 2, "r",
3473 doc: /* Base64-decode the region between BEG and END.
3474 Return the length of the decoded text.
3475 If the region can't be decoded, signal an error and don't modify the buffer. */)
3476 (beg, end)
3477 Lisp_Object beg, end;
3479 int ibeg, iend, length, allength;
3480 char *decoded;
3481 int old_pos = PT;
3482 int decoded_length;
3483 int inserted_chars;
3484 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
3485 USE_SAFE_ALLOCA;
3487 validate_region (&beg, &end);
3489 ibeg = CHAR_TO_BYTE (XFASTINT (beg));
3490 iend = CHAR_TO_BYTE (XFASTINT (end));
3492 length = iend - ibeg;
3494 /* We need to allocate enough room for decoding the text. If we are
3495 working on a multibyte buffer, each decoded code may occupy at
3496 most two bytes. */
3497 allength = multibyte ? length * 2 : length;
3498 SAFE_ALLOCA (decoded, char *, allength);
3500 move_gap_both (XFASTINT (beg), ibeg);
3501 decoded_length = base64_decode_1 (BYTE_POS_ADDR (ibeg), decoded, length,
3502 multibyte, &inserted_chars);
3503 if (decoded_length > allength)
3504 abort ();
3506 if (decoded_length < 0)
3508 /* The decoding wasn't possible. */
3509 SAFE_FREE ();
3510 error ("Invalid base64 data");
3513 /* Now we have decoded the region, so we insert the new contents
3514 and delete the old. (Insert first in order to preserve markers.) */
3515 TEMP_SET_PT_BOTH (XFASTINT (beg), ibeg);
3516 insert_1_both (decoded, inserted_chars, decoded_length, 0, 1, 0);
3517 SAFE_FREE ();
3519 /* Delete the original text. */
3520 del_range_both (PT, PT_BYTE, XFASTINT (end) + inserted_chars,
3521 iend + decoded_length, 1);
3523 /* If point was outside of the region, restore it exactly; else just
3524 move to the beginning of the region. */
3525 if (old_pos >= XFASTINT (end))
3526 old_pos += inserted_chars - (XFASTINT (end) - XFASTINT (beg));
3527 else if (old_pos > XFASTINT (beg))
3528 old_pos = XFASTINT (beg);
3529 SET_PT (old_pos > ZV ? ZV : old_pos);
3531 return make_number (inserted_chars);
3534 DEFUN ("base64-decode-string", Fbase64_decode_string, Sbase64_decode_string,
3535 1, 1, 0,
3536 doc: /* Base64-decode STRING and return the result. */)
3537 (string)
3538 Lisp_Object string;
3540 char *decoded;
3541 int length, decoded_length;
3542 Lisp_Object decoded_string;
3543 USE_SAFE_ALLOCA;
3545 CHECK_STRING (string);
3547 length = SBYTES (string);
3548 /* We need to allocate enough room for decoding the text. */
3549 SAFE_ALLOCA (decoded, char *, length);
3551 /* The decoded result should be unibyte. */
3552 decoded_length = base64_decode_1 (SDATA (string), decoded, length,
3553 0, NULL);
3554 if (decoded_length > length)
3555 abort ();
3556 else if (decoded_length >= 0)
3557 decoded_string = make_unibyte_string (decoded, decoded_length);
3558 else
3559 decoded_string = Qnil;
3561 SAFE_FREE ();
3562 if (!STRINGP (decoded_string))
3563 error ("Invalid base64 data");
3565 return decoded_string;
3568 /* Base64-decode the data at FROM of LENGHT bytes into TO. If
3569 MULTIBYTE is nonzero, the decoded result should be in multibyte
3570 form. If NCHARS_RETRUN is not NULL, store the number of produced
3571 characters in *NCHARS_RETURN. */
3573 static int
3574 base64_decode_1 (from, to, length, multibyte, nchars_return)
3575 const char *from;
3576 char *to;
3577 int length;
3578 int multibyte;
3579 int *nchars_return;
3581 int i = 0;
3582 char *e = to;
3583 unsigned char c;
3584 unsigned long value;
3585 int nchars = 0;
3587 while (1)
3589 /* Process first byte of a quadruplet. */
3591 READ_QUADRUPLET_BYTE (e-to);
3593 if (!IS_BASE64 (c))
3594 return -1;
3595 value = base64_char_to_value[c] << 18;
3597 /* Process second byte of a quadruplet. */
3599 READ_QUADRUPLET_BYTE (-1);
3601 if (!IS_BASE64 (c))
3602 return -1;
3603 value |= base64_char_to_value[c] << 12;
3605 c = (unsigned char) (value >> 16);
3606 if (multibyte && c >= 128)
3607 e += BYTE8_STRING (c, e);
3608 else
3609 *e++ = c;
3610 nchars++;
3612 /* Process third byte of a quadruplet. */
3614 READ_QUADRUPLET_BYTE (-1);
3616 if (c == '=')
3618 READ_QUADRUPLET_BYTE (-1);
3620 if (c != '=')
3621 return -1;
3622 continue;
3625 if (!IS_BASE64 (c))
3626 return -1;
3627 value |= base64_char_to_value[c] << 6;
3629 c = (unsigned char) (0xff & value >> 8);
3630 if (multibyte && c >= 128)
3631 e += BYTE8_STRING (c, e);
3632 else
3633 *e++ = c;
3634 nchars++;
3636 /* Process fourth byte of a quadruplet. */
3638 READ_QUADRUPLET_BYTE (-1);
3640 if (c == '=')
3641 continue;
3643 if (!IS_BASE64 (c))
3644 return -1;
3645 value |= base64_char_to_value[c];
3647 c = (unsigned char) (0xff & value);
3648 if (multibyte && c >= 128)
3649 e += BYTE8_STRING (c, e);
3650 else
3651 *e++ = c;
3652 nchars++;
3658 /***********************************************************************
3659 ***** *****
3660 ***** Hash Tables *****
3661 ***** *****
3662 ***********************************************************************/
3664 /* Implemented by gerd@gnu.org. This hash table implementation was
3665 inspired by CMUCL hash tables. */
3667 /* Ideas:
3669 1. For small tables, association lists are probably faster than
3670 hash tables because they have lower overhead.
3672 For uses of hash tables where the O(1) behavior of table
3673 operations is not a requirement, it might therefore be a good idea
3674 not to hash. Instead, we could just do a linear search in the
3675 key_and_value vector of the hash table. This could be done
3676 if a `:linear-search t' argument is given to make-hash-table. */
3679 /* The list of all weak hash tables. Don't staticpro this one. */
3681 struct Lisp_Hash_Table *weak_hash_tables;
3683 /* Various symbols. */
3685 Lisp_Object Qhash_table_p, Qeq, Qeql, Qequal, Qkey, Qvalue;
3686 Lisp_Object QCtest, QCsize, QCrehash_size, QCrehash_threshold, QCweakness;
3687 Lisp_Object Qhash_table_test, Qkey_or_value, Qkey_and_value;
3689 /* Function prototypes. */
3691 static struct Lisp_Hash_Table *check_hash_table P_ ((Lisp_Object));
3692 static int get_key_arg P_ ((Lisp_Object, int, Lisp_Object *, char *));
3693 static void maybe_resize_hash_table P_ ((struct Lisp_Hash_Table *));
3694 static int cmpfn_eql P_ ((struct Lisp_Hash_Table *, Lisp_Object, unsigned,
3695 Lisp_Object, unsigned));
3696 static int cmpfn_equal P_ ((struct Lisp_Hash_Table *, Lisp_Object, unsigned,
3697 Lisp_Object, unsigned));
3698 static int cmpfn_user_defined P_ ((struct Lisp_Hash_Table *, Lisp_Object,
3699 unsigned, Lisp_Object, unsigned));
3700 static unsigned hashfn_eq P_ ((struct Lisp_Hash_Table *, Lisp_Object));
3701 static unsigned hashfn_eql P_ ((struct Lisp_Hash_Table *, Lisp_Object));
3702 static unsigned hashfn_equal P_ ((struct Lisp_Hash_Table *, Lisp_Object));
3703 static unsigned hashfn_user_defined P_ ((struct Lisp_Hash_Table *,
3704 Lisp_Object));
3705 static unsigned sxhash_string P_ ((unsigned char *, int));
3706 static unsigned sxhash_list P_ ((Lisp_Object, int));
3707 static unsigned sxhash_vector P_ ((Lisp_Object, int));
3708 static unsigned sxhash_bool_vector P_ ((Lisp_Object));
3709 static int sweep_weak_table P_ ((struct Lisp_Hash_Table *, int));
3713 /***********************************************************************
3714 Utilities
3715 ***********************************************************************/
3717 /* If OBJ is a Lisp hash table, return a pointer to its struct
3718 Lisp_Hash_Table. Otherwise, signal an error. */
3720 static struct Lisp_Hash_Table *
3721 check_hash_table (obj)
3722 Lisp_Object obj;
3724 CHECK_HASH_TABLE (obj);
3725 return XHASH_TABLE (obj);
3729 /* Value is the next integer I >= N, N >= 0 which is "almost" a prime
3730 number. */
3733 next_almost_prime (n)
3734 int n;
3736 if (n % 2 == 0)
3737 n += 1;
3738 if (n % 3 == 0)
3739 n += 2;
3740 if (n % 7 == 0)
3741 n += 4;
3742 return n;
3746 /* Find KEY in ARGS which has size NARGS. Don't consider indices for
3747 which USED[I] is non-zero. If found at index I in ARGS, set
3748 USED[I] and USED[I + 1] to 1, and return I + 1. Otherwise return
3749 -1. This function is used to extract a keyword/argument pair from
3750 a DEFUN parameter list. */
3752 static int
3753 get_key_arg (key, nargs, args, used)
3754 Lisp_Object key;
3755 int nargs;
3756 Lisp_Object *args;
3757 char *used;
3759 int i;
3761 for (i = 0; i < nargs - 1; ++i)
3762 if (!used[i] && EQ (args[i], key))
3763 break;
3765 if (i >= nargs - 1)
3766 i = -1;
3767 else
3769 used[i++] = 1;
3770 used[i] = 1;
3773 return i;
3777 /* Return a Lisp vector which has the same contents as VEC but has
3778 size NEW_SIZE, NEW_SIZE >= VEC->size. Entries in the resulting
3779 vector that are not copied from VEC are set to INIT. */
3781 Lisp_Object
3782 larger_vector (vec, new_size, init)
3783 Lisp_Object vec;
3784 int new_size;
3785 Lisp_Object init;
3787 struct Lisp_Vector *v;
3788 int i, old_size;
3790 xassert (VECTORP (vec));
3791 old_size = ASIZE (vec);
3792 xassert (new_size >= old_size);
3794 v = allocate_vector (new_size);
3795 bcopy (XVECTOR (vec)->contents, v->contents,
3796 old_size * sizeof *v->contents);
3797 for (i = old_size; i < new_size; ++i)
3798 v->contents[i] = init;
3799 XSETVECTOR (vec, v);
3800 return vec;
3804 /***********************************************************************
3805 Low-level Functions
3806 ***********************************************************************/
3808 /* Compare KEY1 which has hash code HASH1 and KEY2 with hash code
3809 HASH2 in hash table H using `eql'. Value is non-zero if KEY1 and
3810 KEY2 are the same. */
3812 static int
3813 cmpfn_eql (h, key1, hash1, key2, hash2)
3814 struct Lisp_Hash_Table *h;
3815 Lisp_Object key1, key2;
3816 unsigned hash1, hash2;
3818 return (FLOATP (key1)
3819 && FLOATP (key2)
3820 && XFLOAT_DATA (key1) == XFLOAT_DATA (key2));
3824 /* Compare KEY1 which has hash code HASH1 and KEY2 with hash code
3825 HASH2 in hash table H using `equal'. Value is non-zero if KEY1 and
3826 KEY2 are the same. */
3828 static int
3829 cmpfn_equal (h, key1, hash1, key2, hash2)
3830 struct Lisp_Hash_Table *h;
3831 Lisp_Object key1, key2;
3832 unsigned hash1, hash2;
3834 return hash1 == hash2 && !NILP (Fequal (key1, key2));
3838 /* Compare KEY1 which has hash code HASH1, and KEY2 with hash code
3839 HASH2 in hash table H using H->user_cmp_function. Value is non-zero
3840 if KEY1 and KEY2 are the same. */
3842 static int
3843 cmpfn_user_defined (h, key1, hash1, key2, hash2)
3844 struct Lisp_Hash_Table *h;
3845 Lisp_Object key1, key2;
3846 unsigned hash1, hash2;
3848 if (hash1 == hash2)
3850 Lisp_Object args[3];
3852 args[0] = h->user_cmp_function;
3853 args[1] = key1;
3854 args[2] = key2;
3855 return !NILP (Ffuncall (3, args));
3857 else
3858 return 0;
3862 /* Value is a hash code for KEY for use in hash table H which uses
3863 `eq' to compare keys. The hash code returned is guaranteed to fit
3864 in a Lisp integer. */
3866 static unsigned
3867 hashfn_eq (h, key)
3868 struct Lisp_Hash_Table *h;
3869 Lisp_Object key;
3871 unsigned hash = XUINT (key) ^ XTYPE (key);
3872 xassert ((hash & ~INTMASK) == 0);
3873 return hash;
3877 /* Value is a hash code for KEY for use in hash table H which uses
3878 `eql' to compare keys. The hash code returned is guaranteed to fit
3879 in a Lisp integer. */
3881 static unsigned
3882 hashfn_eql (h, key)
3883 struct Lisp_Hash_Table *h;
3884 Lisp_Object key;
3886 unsigned hash;
3887 if (FLOATP (key))
3888 hash = sxhash (key, 0);
3889 else
3890 hash = XUINT (key) ^ XTYPE (key);
3891 xassert ((hash & ~INTMASK) == 0);
3892 return hash;
3896 /* Value is a hash code for KEY for use in hash table H which uses
3897 `equal' to compare keys. The hash code returned is guaranteed to fit
3898 in a Lisp integer. */
3900 static unsigned
3901 hashfn_equal (h, key)
3902 struct Lisp_Hash_Table *h;
3903 Lisp_Object key;
3905 unsigned hash = sxhash (key, 0);
3906 xassert ((hash & ~INTMASK) == 0);
3907 return hash;
3911 /* Value is a hash code for KEY for use in hash table H which uses as
3912 user-defined function to compare keys. The hash code returned is
3913 guaranteed to fit in a Lisp integer. */
3915 static unsigned
3916 hashfn_user_defined (h, key)
3917 struct Lisp_Hash_Table *h;
3918 Lisp_Object key;
3920 Lisp_Object args[2], hash;
3922 args[0] = h->user_hash_function;
3923 args[1] = key;
3924 hash = Ffuncall (2, args);
3925 if (!INTEGERP (hash))
3926 signal_error ("Invalid hash code returned from user-supplied hash function", hash);
3927 return XUINT (hash);
3931 /* Create and initialize a new hash table.
3933 TEST specifies the test the hash table will use to compare keys.
3934 It must be either one of the predefined tests `eq', `eql' or
3935 `equal' or a symbol denoting a user-defined test named TEST with
3936 test and hash functions USER_TEST and USER_HASH.
3938 Give the table initial capacity SIZE, SIZE >= 0, an integer.
3940 If REHASH_SIZE is an integer, it must be > 0, and this hash table's
3941 new size when it becomes full is computed by adding REHASH_SIZE to
3942 its old size. If REHASH_SIZE is a float, it must be > 1.0, and the
3943 table's new size is computed by multiplying its old size with
3944 REHASH_SIZE.
3946 REHASH_THRESHOLD must be a float <= 1.0, and > 0. The table will
3947 be resized when the ratio of (number of entries in the table) /
3948 (table size) is >= REHASH_THRESHOLD.
3950 WEAK specifies the weakness of the table. If non-nil, it must be
3951 one of the symbols `key', `value', `key-or-value', or `key-and-value'. */
3953 Lisp_Object
3954 make_hash_table (test, size, rehash_size, rehash_threshold, weak,
3955 user_test, user_hash)
3956 Lisp_Object test, size, rehash_size, rehash_threshold, weak;
3957 Lisp_Object user_test, user_hash;
3959 struct Lisp_Hash_Table *h;
3960 Lisp_Object table;
3961 int index_size, i, sz;
3963 /* Preconditions. */
3964 xassert (SYMBOLP (test));
3965 xassert (INTEGERP (size) && XINT (size) >= 0);
3966 xassert ((INTEGERP (rehash_size) && XINT (rehash_size) > 0)
3967 || (FLOATP (rehash_size) && XFLOATINT (rehash_size) > 1.0));
3968 xassert (FLOATP (rehash_threshold)
3969 && XFLOATINT (rehash_threshold) > 0
3970 && XFLOATINT (rehash_threshold) <= 1.0);
3972 if (XFASTINT (size) == 0)
3973 size = make_number (1);
3975 /* Allocate a table and initialize it. */
3976 h = allocate_hash_table ();
3978 /* Initialize hash table slots. */
3979 sz = XFASTINT (size);
3981 h->test = test;
3982 if (EQ (test, Qeql))
3984 h->cmpfn = cmpfn_eql;
3985 h->hashfn = hashfn_eql;
3987 else if (EQ (test, Qeq))
3989 h->cmpfn = NULL;
3990 h->hashfn = hashfn_eq;
3992 else if (EQ (test, Qequal))
3994 h->cmpfn = cmpfn_equal;
3995 h->hashfn = hashfn_equal;
3997 else
3999 h->user_cmp_function = user_test;
4000 h->user_hash_function = user_hash;
4001 h->cmpfn = cmpfn_user_defined;
4002 h->hashfn = hashfn_user_defined;
4005 h->weak = weak;
4006 h->rehash_threshold = rehash_threshold;
4007 h->rehash_size = rehash_size;
4008 h->count = 0;
4009 h->key_and_value = Fmake_vector (make_number (2 * sz), Qnil);
4010 h->hash = Fmake_vector (size, Qnil);
4011 h->next = Fmake_vector (size, Qnil);
4012 /* Cast to int here avoids losing with gcc 2.95 on Tru64/Alpha... */
4013 index_size = next_almost_prime ((int) (sz / XFLOATINT (rehash_threshold)));
4014 h->index = Fmake_vector (make_number (index_size), Qnil);
4016 /* Set up the free list. */
4017 for (i = 0; i < sz - 1; ++i)
4018 HASH_NEXT (h, i) = make_number (i + 1);
4019 h->next_free = make_number (0);
4021 XSET_HASH_TABLE (table, h);
4022 xassert (HASH_TABLE_P (table));
4023 xassert (XHASH_TABLE (table) == h);
4025 /* Maybe add this hash table to the list of all weak hash tables. */
4026 if (NILP (h->weak))
4027 h->next_weak = NULL;
4028 else
4030 h->next_weak = weak_hash_tables;
4031 weak_hash_tables = h;
4034 return table;
4038 /* Return a copy of hash table H1. Keys and values are not copied,
4039 only the table itself is. */
4041 Lisp_Object
4042 copy_hash_table (h1)
4043 struct Lisp_Hash_Table *h1;
4045 Lisp_Object table;
4046 struct Lisp_Hash_Table *h2;
4047 struct Lisp_Vector *next;
4049 h2 = allocate_hash_table ();
4050 next = h2->vec_next;
4051 bcopy (h1, h2, sizeof *h2);
4052 h2->vec_next = next;
4053 h2->key_and_value = Fcopy_sequence (h1->key_and_value);
4054 h2->hash = Fcopy_sequence (h1->hash);
4055 h2->next = Fcopy_sequence (h1->next);
4056 h2->index = Fcopy_sequence (h1->index);
4057 XSET_HASH_TABLE (table, h2);
4059 /* Maybe add this hash table to the list of all weak hash tables. */
4060 if (!NILP (h2->weak))
4062 h2->next_weak = weak_hash_tables;
4063 weak_hash_tables = h2;
4066 return table;
4070 /* Resize hash table H if it's too full. If H cannot be resized
4071 because it's already too large, throw an error. */
4073 static INLINE void
4074 maybe_resize_hash_table (h)
4075 struct Lisp_Hash_Table *h;
4077 if (NILP (h->next_free))
4079 int old_size = HASH_TABLE_SIZE (h);
4080 int i, new_size, index_size;
4081 EMACS_INT nsize;
4083 if (INTEGERP (h->rehash_size))
4084 new_size = old_size + XFASTINT (h->rehash_size);
4085 else
4086 new_size = old_size * XFLOATINT (h->rehash_size);
4087 new_size = max (old_size + 1, new_size);
4088 index_size = next_almost_prime ((int)
4089 (new_size
4090 / XFLOATINT (h->rehash_threshold)));
4091 /* Assignment to EMACS_INT stops GCC whining about limited range
4092 of data type. */
4093 nsize = max (index_size, 2 * new_size);
4094 if (nsize > MOST_POSITIVE_FIXNUM)
4095 error ("Hash table too large to resize");
4097 h->key_and_value = larger_vector (h->key_and_value, 2 * new_size, Qnil);
4098 h->next = larger_vector (h->next, new_size, Qnil);
4099 h->hash = larger_vector (h->hash, new_size, Qnil);
4100 h->index = Fmake_vector (make_number (index_size), Qnil);
4102 /* Update the free list. Do it so that new entries are added at
4103 the end of the free list. This makes some operations like
4104 maphash faster. */
4105 for (i = old_size; i < new_size - 1; ++i)
4106 HASH_NEXT (h, i) = make_number (i + 1);
4108 if (!NILP (h->next_free))
4110 Lisp_Object last, next;
4112 last = h->next_free;
4113 while (next = HASH_NEXT (h, XFASTINT (last)),
4114 !NILP (next))
4115 last = next;
4117 HASH_NEXT (h, XFASTINT (last)) = make_number (old_size);
4119 else
4120 XSETFASTINT (h->next_free, old_size);
4122 /* Rehash. */
4123 for (i = 0; i < old_size; ++i)
4124 if (!NILP (HASH_HASH (h, i)))
4126 unsigned hash_code = XUINT (HASH_HASH (h, i));
4127 int start_of_bucket = hash_code % ASIZE (h->index);
4128 HASH_NEXT (h, i) = HASH_INDEX (h, start_of_bucket);
4129 HASH_INDEX (h, start_of_bucket) = make_number (i);
4135 /* Lookup KEY in hash table H. If HASH is non-null, return in *HASH
4136 the hash code of KEY. Value is the index of the entry in H
4137 matching KEY, or -1 if not found. */
4140 hash_lookup (h, key, hash)
4141 struct Lisp_Hash_Table *h;
4142 Lisp_Object key;
4143 unsigned *hash;
4145 unsigned hash_code;
4146 int start_of_bucket;
4147 Lisp_Object idx;
4149 hash_code = h->hashfn (h, key);
4150 if (hash)
4151 *hash = hash_code;
4153 start_of_bucket = hash_code % ASIZE (h->index);
4154 idx = HASH_INDEX (h, start_of_bucket);
4156 /* We need not gcpro idx since it's either an integer or nil. */
4157 while (!NILP (idx))
4159 int i = XFASTINT (idx);
4160 if (EQ (key, HASH_KEY (h, i))
4161 || (h->cmpfn
4162 && h->cmpfn (h, key, hash_code,
4163 HASH_KEY (h, i), XUINT (HASH_HASH (h, i)))))
4164 break;
4165 idx = HASH_NEXT (h, i);
4168 return NILP (idx) ? -1 : XFASTINT (idx);
4172 /* Put an entry into hash table H that associates KEY with VALUE.
4173 HASH is a previously computed hash code of KEY.
4174 Value is the index of the entry in H matching KEY. */
4177 hash_put (h, key, value, hash)
4178 struct Lisp_Hash_Table *h;
4179 Lisp_Object key, value;
4180 unsigned hash;
4182 int start_of_bucket, i;
4184 xassert ((hash & ~INTMASK) == 0);
4186 /* Increment count after resizing because resizing may fail. */
4187 maybe_resize_hash_table (h);
4188 h->count++;
4190 /* Store key/value in the key_and_value vector. */
4191 i = XFASTINT (h->next_free);
4192 h->next_free = HASH_NEXT (h, i);
4193 HASH_KEY (h, i) = key;
4194 HASH_VALUE (h, i) = value;
4196 /* Remember its hash code. */
4197 HASH_HASH (h, i) = make_number (hash);
4199 /* Add new entry to its collision chain. */
4200 start_of_bucket = hash % ASIZE (h->index);
4201 HASH_NEXT (h, i) = HASH_INDEX (h, start_of_bucket);
4202 HASH_INDEX (h, start_of_bucket) = make_number (i);
4203 return i;
4207 /* Remove the entry matching KEY from hash table H, if there is one. */
4209 void
4210 hash_remove (h, key)
4211 struct Lisp_Hash_Table *h;
4212 Lisp_Object key;
4214 unsigned hash_code;
4215 int start_of_bucket;
4216 Lisp_Object idx, prev;
4218 hash_code = h->hashfn (h, key);
4219 start_of_bucket = hash_code % ASIZE (h->index);
4220 idx = HASH_INDEX (h, start_of_bucket);
4221 prev = Qnil;
4223 /* We need not gcpro idx, prev since they're either integers or nil. */
4224 while (!NILP (idx))
4226 int i = XFASTINT (idx);
4228 if (EQ (key, HASH_KEY (h, i))
4229 || (h->cmpfn
4230 && h->cmpfn (h, key, hash_code,
4231 HASH_KEY (h, i), XUINT (HASH_HASH (h, i)))))
4233 /* Take entry out of collision chain. */
4234 if (NILP (prev))
4235 HASH_INDEX (h, start_of_bucket) = HASH_NEXT (h, i);
4236 else
4237 HASH_NEXT (h, XFASTINT (prev)) = HASH_NEXT (h, i);
4239 /* Clear slots in key_and_value and add the slots to
4240 the free list. */
4241 HASH_KEY (h, i) = HASH_VALUE (h, i) = HASH_HASH (h, i) = Qnil;
4242 HASH_NEXT (h, i) = h->next_free;
4243 h->next_free = make_number (i);
4244 h->count--;
4245 xassert (h->count >= 0);
4246 break;
4248 else
4250 prev = idx;
4251 idx = HASH_NEXT (h, i);
4257 /* Clear hash table H. */
4259 void
4260 hash_clear (h)
4261 struct Lisp_Hash_Table *h;
4263 if (h->count > 0)
4265 int i, size = HASH_TABLE_SIZE (h);
4267 for (i = 0; i < size; ++i)
4269 HASH_NEXT (h, i) = i < size - 1 ? make_number (i + 1) : Qnil;
4270 HASH_KEY (h, i) = Qnil;
4271 HASH_VALUE (h, i) = Qnil;
4272 HASH_HASH (h, i) = Qnil;
4275 for (i = 0; i < ASIZE (h->index); ++i)
4276 ASET (h->index, i, Qnil);
4278 h->next_free = make_number (0);
4279 h->count = 0;
4285 /************************************************************************
4286 Weak Hash Tables
4287 ************************************************************************/
4289 void
4290 init_weak_hash_tables ()
4292 weak_hash_tables = NULL;
4295 /* Sweep weak hash table H. REMOVE_ENTRIES_P non-zero means remove
4296 entries from the table that don't survive the current GC.
4297 REMOVE_ENTRIES_P zero means mark entries that are in use. Value is
4298 non-zero if anything was marked. */
4300 static int
4301 sweep_weak_table (h, remove_entries_p)
4302 struct Lisp_Hash_Table *h;
4303 int remove_entries_p;
4305 int bucket, n, marked;
4307 n = ASIZE (h->index) & ~ARRAY_MARK_FLAG;
4308 marked = 0;
4310 for (bucket = 0; bucket < n; ++bucket)
4312 Lisp_Object idx, next, prev;
4314 /* Follow collision chain, removing entries that
4315 don't survive this garbage collection. */
4316 prev = Qnil;
4317 for (idx = HASH_INDEX (h, bucket); !NILP (idx); idx = next)
4319 int i = XFASTINT (idx);
4320 int key_known_to_survive_p = survives_gc_p (HASH_KEY (h, i));
4321 int value_known_to_survive_p = survives_gc_p (HASH_VALUE (h, i));
4322 int remove_p;
4324 if (EQ (h->weak, Qkey))
4325 remove_p = !key_known_to_survive_p;
4326 else if (EQ (h->weak, Qvalue))
4327 remove_p = !value_known_to_survive_p;
4328 else if (EQ (h->weak, Qkey_or_value))
4329 remove_p = !(key_known_to_survive_p || value_known_to_survive_p);
4330 else if (EQ (h->weak, Qkey_and_value))
4331 remove_p = !(key_known_to_survive_p && value_known_to_survive_p);
4332 else
4333 abort ();
4335 next = HASH_NEXT (h, i);
4337 if (remove_entries_p)
4339 if (remove_p)
4341 /* Take out of collision chain. */
4342 if (NILP (prev))
4343 HASH_INDEX (h, bucket) = next;
4344 else
4345 HASH_NEXT (h, XFASTINT (prev)) = next;
4347 /* Add to free list. */
4348 HASH_NEXT (h, i) = h->next_free;
4349 h->next_free = idx;
4351 /* Clear key, value, and hash. */
4352 HASH_KEY (h, i) = HASH_VALUE (h, i) = Qnil;
4353 HASH_HASH (h, i) = Qnil;
4355 h->count--;
4357 else
4359 prev = idx;
4362 else
4364 if (!remove_p)
4366 /* Make sure key and value survive. */
4367 if (!key_known_to_survive_p)
4369 mark_object (HASH_KEY (h, i));
4370 marked = 1;
4373 if (!value_known_to_survive_p)
4375 mark_object (HASH_VALUE (h, i));
4376 marked = 1;
4383 return marked;
4386 /* Remove elements from weak hash tables that don't survive the
4387 current garbage collection. Remove weak tables that don't survive
4388 from Vweak_hash_tables. Called from gc_sweep. */
4390 void
4391 sweep_weak_hash_tables ()
4393 struct Lisp_Hash_Table *h, *used, *next;
4394 int marked;
4396 /* Mark all keys and values that are in use. Keep on marking until
4397 there is no more change. This is necessary for cases like
4398 value-weak table A containing an entry X -> Y, where Y is used in a
4399 key-weak table B, Z -> Y. If B comes after A in the list of weak
4400 tables, X -> Y might be removed from A, although when looking at B
4401 one finds that it shouldn't. */
4404 marked = 0;
4405 for (h = weak_hash_tables; h; h = h->next_weak)
4407 if (h->size & ARRAY_MARK_FLAG)
4408 marked |= sweep_weak_table (h, 0);
4411 while (marked);
4413 /* Remove tables and entries that aren't used. */
4414 for (h = weak_hash_tables, used = NULL; h; h = next)
4416 next = h->next_weak;
4418 if (h->size & ARRAY_MARK_FLAG)
4420 /* TABLE is marked as used. Sweep its contents. */
4421 if (h->count > 0)
4422 sweep_weak_table (h, 1);
4424 /* Add table to the list of used weak hash tables. */
4425 h->next_weak = used;
4426 used = h;
4430 weak_hash_tables = used;
4435 /***********************************************************************
4436 Hash Code Computation
4437 ***********************************************************************/
4439 /* Maximum depth up to which to dive into Lisp structures. */
4441 #define SXHASH_MAX_DEPTH 3
4443 /* Maximum length up to which to take list and vector elements into
4444 account. */
4446 #define SXHASH_MAX_LEN 7
4448 /* Combine two integers X and Y for hashing. */
4450 #define SXHASH_COMBINE(X, Y) \
4451 ((((unsigned)(X) << 4) + (((unsigned)(X) >> 24) & 0x0fffffff)) \
4452 + (unsigned)(Y))
4455 /* Return a hash for string PTR which has length LEN. The hash
4456 code returned is guaranteed to fit in a Lisp integer. */
4458 static unsigned
4459 sxhash_string (ptr, len)
4460 unsigned char *ptr;
4461 int len;
4463 unsigned char *p = ptr;
4464 unsigned char *end = p + len;
4465 unsigned char c;
4466 unsigned hash = 0;
4468 while (p != end)
4470 c = *p++;
4471 if (c >= 0140)
4472 c -= 40;
4473 hash = ((hash << 4) + (hash >> 28) + c);
4476 return hash & INTMASK;
4480 /* Return a hash for list LIST. DEPTH is the current depth in the
4481 list. We don't recurse deeper than SXHASH_MAX_DEPTH in it. */
4483 static unsigned
4484 sxhash_list (list, depth)
4485 Lisp_Object list;
4486 int depth;
4488 unsigned hash = 0;
4489 int i;
4491 if (depth < SXHASH_MAX_DEPTH)
4492 for (i = 0;
4493 CONSP (list) && i < SXHASH_MAX_LEN;
4494 list = XCDR (list), ++i)
4496 unsigned hash2 = sxhash (XCAR (list), depth + 1);
4497 hash = SXHASH_COMBINE (hash, hash2);
4500 if (!NILP (list))
4502 unsigned hash2 = sxhash (list, depth + 1);
4503 hash = SXHASH_COMBINE (hash, hash2);
4506 return hash;
4510 /* Return a hash for vector VECTOR. DEPTH is the current depth in
4511 the Lisp structure. */
4513 static unsigned
4514 sxhash_vector (vec, depth)
4515 Lisp_Object vec;
4516 int depth;
4518 unsigned hash = ASIZE (vec);
4519 int i, n;
4521 n = min (SXHASH_MAX_LEN, ASIZE (vec));
4522 for (i = 0; i < n; ++i)
4524 unsigned hash2 = sxhash (AREF (vec, i), depth + 1);
4525 hash = SXHASH_COMBINE (hash, hash2);
4528 return hash;
4532 /* Return a hash for bool-vector VECTOR. */
4534 static unsigned
4535 sxhash_bool_vector (vec)
4536 Lisp_Object vec;
4538 unsigned hash = XBOOL_VECTOR (vec)->size;
4539 int i, n;
4541 n = min (SXHASH_MAX_LEN, XBOOL_VECTOR (vec)->vector_size);
4542 for (i = 0; i < n; ++i)
4543 hash = SXHASH_COMBINE (hash, XBOOL_VECTOR (vec)->data[i]);
4545 return hash;
4549 /* Return a hash code for OBJ. DEPTH is the current depth in the Lisp
4550 structure. Value is an unsigned integer clipped to INTMASK. */
4552 unsigned
4553 sxhash (obj, depth)
4554 Lisp_Object obj;
4555 int depth;
4557 unsigned hash;
4559 if (depth > SXHASH_MAX_DEPTH)
4560 return 0;
4562 switch (XTYPE (obj))
4564 case Lisp_Int:
4565 hash = XUINT (obj);
4566 break;
4568 case Lisp_Misc:
4569 hash = XUINT (obj);
4570 break;
4572 case Lisp_Symbol:
4573 obj = SYMBOL_NAME (obj);
4574 /* Fall through. */
4576 case Lisp_String:
4577 hash = sxhash_string (SDATA (obj), SCHARS (obj));
4578 break;
4580 /* This can be everything from a vector to an overlay. */
4581 case Lisp_Vectorlike:
4582 if (VECTORP (obj))
4583 /* According to the CL HyperSpec, two arrays are equal only if
4584 they are `eq', except for strings and bit-vectors. In
4585 Emacs, this works differently. We have to compare element
4586 by element. */
4587 hash = sxhash_vector (obj, depth);
4588 else if (BOOL_VECTOR_P (obj))
4589 hash = sxhash_bool_vector (obj);
4590 else
4591 /* Others are `equal' if they are `eq', so let's take their
4592 address as hash. */
4593 hash = XUINT (obj);
4594 break;
4596 case Lisp_Cons:
4597 hash = sxhash_list (obj, depth);
4598 break;
4600 case Lisp_Float:
4602 unsigned char *p = (unsigned char *) &XFLOAT_DATA (obj);
4603 unsigned char *e = p + sizeof XFLOAT_DATA (obj);
4604 for (hash = 0; p < e; ++p)
4605 hash = SXHASH_COMBINE (hash, *p);
4606 break;
4609 default:
4610 abort ();
4613 return hash & INTMASK;
4618 /***********************************************************************
4619 Lisp Interface
4620 ***********************************************************************/
4623 DEFUN ("sxhash", Fsxhash, Ssxhash, 1, 1, 0,
4624 doc: /* Compute a hash code for OBJ and return it as integer. */)
4625 (obj)
4626 Lisp_Object obj;
4628 unsigned hash = sxhash (obj, 0);
4629 return make_number (hash);
4633 DEFUN ("make-hash-table", Fmake_hash_table, Smake_hash_table, 0, MANY, 0,
4634 doc: /* Create and return a new hash table.
4636 Arguments are specified as keyword/argument pairs. The following
4637 arguments are defined:
4639 :test TEST -- TEST must be a symbol that specifies how to compare
4640 keys. Default is `eql'. Predefined are the tests `eq', `eql', and
4641 `equal'. User-supplied test and hash functions can be specified via
4642 `define-hash-table-test'.
4644 :size SIZE -- A hint as to how many elements will be put in the table.
4645 Default is 65.
4647 :rehash-size REHASH-SIZE - Indicates how to expand the table when it
4648 fills up. If REHASH-SIZE is an integer, add that many space. If it
4649 is a float, it must be > 1.0, and the new size is computed by
4650 multiplying the old size with that factor. Default is 1.5.
4652 :rehash-threshold THRESHOLD -- THRESHOLD must a float > 0, and <= 1.0.
4653 Resize the hash table when ratio of the number of entries in the
4654 table. Default is 0.8.
4656 :weakness WEAK -- WEAK must be one of nil, t, `key', `value',
4657 `key-or-value', or `key-and-value'. If WEAK is not nil, the table
4658 returned is a weak table. Key/value pairs are removed from a weak
4659 hash table when there are no non-weak references pointing to their
4660 key, value, one of key or value, or both key and value, depending on
4661 WEAK. WEAK t is equivalent to `key-and-value'. Default value of WEAK
4662 is nil.
4664 usage: (make-hash-table &rest KEYWORD-ARGS) */)
4665 (nargs, args)
4666 int nargs;
4667 Lisp_Object *args;
4669 Lisp_Object test, size, rehash_size, rehash_threshold, weak;
4670 Lisp_Object user_test, user_hash;
4671 char *used;
4672 int i;
4674 /* The vector `used' is used to keep track of arguments that
4675 have been consumed. */
4676 used = (char *) alloca (nargs * sizeof *used);
4677 bzero (used, nargs * sizeof *used);
4679 /* See if there's a `:test TEST' among the arguments. */
4680 i = get_key_arg (QCtest, nargs, args, used);
4681 test = i < 0 ? Qeql : args[i];
4682 if (!EQ (test, Qeq) && !EQ (test, Qeql) && !EQ (test, Qequal))
4684 /* See if it is a user-defined test. */
4685 Lisp_Object prop;
4687 prop = Fget (test, Qhash_table_test);
4688 if (!CONSP (prop) || !CONSP (XCDR (prop)))
4689 signal_error ("Invalid hash table test", test);
4690 user_test = XCAR (prop);
4691 user_hash = XCAR (XCDR (prop));
4693 else
4694 user_test = user_hash = Qnil;
4696 /* See if there's a `:size SIZE' argument. */
4697 i = get_key_arg (QCsize, nargs, args, used);
4698 size = i < 0 ? Qnil : args[i];
4699 if (NILP (size))
4700 size = make_number (DEFAULT_HASH_SIZE);
4701 else if (!INTEGERP (size) || XINT (size) < 0)
4702 signal_error ("Invalid hash table size", size);
4704 /* Look for `:rehash-size SIZE'. */
4705 i = get_key_arg (QCrehash_size, nargs, args, used);
4706 rehash_size = i < 0 ? make_float (DEFAULT_REHASH_SIZE) : args[i];
4707 if (!NUMBERP (rehash_size)
4708 || (INTEGERP (rehash_size) && XINT (rehash_size) <= 0)
4709 || XFLOATINT (rehash_size) <= 1.0)
4710 signal_error ("Invalid hash table rehash size", rehash_size);
4712 /* Look for `:rehash-threshold THRESHOLD'. */
4713 i = get_key_arg (QCrehash_threshold, nargs, args, used);
4714 rehash_threshold = i < 0 ? make_float (DEFAULT_REHASH_THRESHOLD) : args[i];
4715 if (!FLOATP (rehash_threshold)
4716 || XFLOATINT (rehash_threshold) <= 0.0
4717 || XFLOATINT (rehash_threshold) > 1.0)
4718 signal_error ("Invalid hash table rehash threshold", rehash_threshold);
4720 /* Look for `:weakness WEAK'. */
4721 i = get_key_arg (QCweakness, nargs, args, used);
4722 weak = i < 0 ? Qnil : args[i];
4723 if (EQ (weak, Qt))
4724 weak = Qkey_and_value;
4725 if (!NILP (weak)
4726 && !EQ (weak, Qkey)
4727 && !EQ (weak, Qvalue)
4728 && !EQ (weak, Qkey_or_value)
4729 && !EQ (weak, Qkey_and_value))
4730 signal_error ("Invalid hash table weakness", weak);
4732 /* Now, all args should have been used up, or there's a problem. */
4733 for (i = 0; i < nargs; ++i)
4734 if (!used[i])
4735 signal_error ("Invalid argument list", args[i]);
4737 return make_hash_table (test, size, rehash_size, rehash_threshold, weak,
4738 user_test, user_hash);
4742 DEFUN ("copy-hash-table", Fcopy_hash_table, Scopy_hash_table, 1, 1, 0,
4743 doc: /* Return a copy of hash table TABLE. */)
4744 (table)
4745 Lisp_Object table;
4747 return copy_hash_table (check_hash_table (table));
4751 DEFUN ("hash-table-count", Fhash_table_count, Shash_table_count, 1, 1, 0,
4752 doc: /* Return the number of elements in TABLE. */)
4753 (table)
4754 Lisp_Object table;
4756 return make_number (check_hash_table (table)->count);
4760 DEFUN ("hash-table-rehash-size", Fhash_table_rehash_size,
4761 Shash_table_rehash_size, 1, 1, 0,
4762 doc: /* Return the current rehash size of TABLE. */)
4763 (table)
4764 Lisp_Object table;
4766 return check_hash_table (table)->rehash_size;
4770 DEFUN ("hash-table-rehash-threshold", Fhash_table_rehash_threshold,
4771 Shash_table_rehash_threshold, 1, 1, 0,
4772 doc: /* Return the current rehash threshold of TABLE. */)
4773 (table)
4774 Lisp_Object table;
4776 return check_hash_table (table)->rehash_threshold;
4780 DEFUN ("hash-table-size", Fhash_table_size, Shash_table_size, 1, 1, 0,
4781 doc: /* Return the size of TABLE.
4782 The size can be used as an argument to `make-hash-table' to create
4783 a hash table than can hold as many elements of TABLE holds
4784 without need for resizing. */)
4785 (table)
4786 Lisp_Object table;
4788 struct Lisp_Hash_Table *h = check_hash_table (table);
4789 return make_number (HASH_TABLE_SIZE (h));
4793 DEFUN ("hash-table-test", Fhash_table_test, Shash_table_test, 1, 1, 0,
4794 doc: /* Return the test TABLE uses. */)
4795 (table)
4796 Lisp_Object table;
4798 return check_hash_table (table)->test;
4802 DEFUN ("hash-table-weakness", Fhash_table_weakness, Shash_table_weakness,
4803 1, 1, 0,
4804 doc: /* Return the weakness of TABLE. */)
4805 (table)
4806 Lisp_Object table;
4808 return check_hash_table (table)->weak;
4812 DEFUN ("hash-table-p", Fhash_table_p, Shash_table_p, 1, 1, 0,
4813 doc: /* Return t if OBJ is a Lisp hash table object. */)
4814 (obj)
4815 Lisp_Object obj;
4817 return HASH_TABLE_P (obj) ? Qt : Qnil;
4821 DEFUN ("clrhash", Fclrhash, Sclrhash, 1, 1, 0,
4822 doc: /* Clear hash table TABLE and return it. */)
4823 (table)
4824 Lisp_Object table;
4826 hash_clear (check_hash_table (table));
4827 /* Be compatible with XEmacs. */
4828 return table;
4832 DEFUN ("gethash", Fgethash, Sgethash, 2, 3, 0,
4833 doc: /* Look up KEY in TABLE and return its associated value.
4834 If KEY is not found, return DFLT which defaults to nil. */)
4835 (key, table, dflt)
4836 Lisp_Object key, table, dflt;
4838 struct Lisp_Hash_Table *h = check_hash_table (table);
4839 int i = hash_lookup (h, key, NULL);
4840 return i >= 0 ? HASH_VALUE (h, i) : dflt;
4844 DEFUN ("puthash", Fputhash, Sputhash, 3, 3, 0,
4845 doc: /* Associate KEY with VALUE in hash table TABLE.
4846 If KEY is already present in table, replace its current value with
4847 VALUE. */)
4848 (key, value, table)
4849 Lisp_Object key, value, table;
4851 struct Lisp_Hash_Table *h = check_hash_table (table);
4852 int i;
4853 unsigned hash;
4855 i = hash_lookup (h, key, &hash);
4856 if (i >= 0)
4857 HASH_VALUE (h, i) = value;
4858 else
4859 hash_put (h, key, value, hash);
4861 return value;
4865 DEFUN ("remhash", Fremhash, Sremhash, 2, 2, 0,
4866 doc: /* Remove KEY from TABLE. */)
4867 (key, table)
4868 Lisp_Object key, table;
4870 struct Lisp_Hash_Table *h = check_hash_table (table);
4871 hash_remove (h, key);
4872 return Qnil;
4876 DEFUN ("maphash", Fmaphash, Smaphash, 2, 2, 0,
4877 doc: /* Call FUNCTION for all entries in hash table TABLE.
4878 FUNCTION is called with two arguments, KEY and VALUE. */)
4879 (function, table)
4880 Lisp_Object function, table;
4882 struct Lisp_Hash_Table *h = check_hash_table (table);
4883 Lisp_Object args[3];
4884 int i;
4886 for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
4887 if (!NILP (HASH_HASH (h, i)))
4889 args[0] = function;
4890 args[1] = HASH_KEY (h, i);
4891 args[2] = HASH_VALUE (h, i);
4892 Ffuncall (3, args);
4895 return Qnil;
4899 DEFUN ("define-hash-table-test", Fdefine_hash_table_test,
4900 Sdefine_hash_table_test, 3, 3, 0,
4901 doc: /* Define a new hash table test with name NAME, a symbol.
4903 In hash tables created with NAME specified as test, use TEST to
4904 compare keys, and HASH for computing hash codes of keys.
4906 TEST must be a function taking two arguments and returning non-nil if
4907 both arguments are the same. HASH must be a function taking one
4908 argument and return an integer that is the hash code of the argument.
4909 Hash code computation should use the whole value range of integers,
4910 including negative integers. */)
4911 (name, test, hash)
4912 Lisp_Object name, test, hash;
4914 return Fput (name, Qhash_table_test, list2 (test, hash));
4919 /************************************************************************
4921 ************************************************************************/
4923 #include "md5.h"
4925 DEFUN ("md5", Fmd5, Smd5, 1, 5, 0,
4926 doc: /* Return MD5 message digest of OBJECT, a buffer or string.
4928 A message digest is a cryptographic checksum of a document, and the
4929 algorithm to calculate it is defined in RFC 1321.
4931 The two optional arguments START and END are character positions
4932 specifying for which part of OBJECT the message digest should be
4933 computed. If nil or omitted, the digest is computed for the whole
4934 OBJECT.
4936 The MD5 message digest is computed from the result of encoding the
4937 text in a coding system, not directly from the internal Emacs form of
4938 the text. The optional fourth argument CODING-SYSTEM specifies which
4939 coding system to encode the text with. It should be the same coding
4940 system that you used or will use when actually writing the text into a
4941 file.
4943 If CODING-SYSTEM is nil or omitted, the default depends on OBJECT. If
4944 OBJECT is a buffer, the default for CODING-SYSTEM is whatever coding
4945 system would be chosen by default for writing this text into a file.
4947 If OBJECT is a string, the most preferred coding system (see the
4948 command `prefer-coding-system') is used.
4950 If NOERROR is non-nil, silently assume the `raw-text' coding if the
4951 guesswork fails. Normally, an error is signaled in such case. */)
4952 (object, start, end, coding_system, noerror)
4953 Lisp_Object object, start, end, coding_system, noerror;
4955 unsigned char digest[16];
4956 unsigned char value[33];
4957 int i;
4958 int size;
4959 int size_byte = 0;
4960 int start_char = 0, end_char = 0;
4961 int start_byte = 0, end_byte = 0;
4962 register int b, e;
4963 register struct buffer *bp;
4964 int temp;
4966 if (STRINGP (object))
4968 if (NILP (coding_system))
4970 /* Decide the coding-system to encode the data with. */
4972 if (STRING_MULTIBYTE (object))
4973 /* use default, we can't guess correct value */
4974 coding_system = preferred_coding_system ();
4975 else
4976 coding_system = Qraw_text;
4979 if (NILP (Fcoding_system_p (coding_system)))
4981 /* Invalid coding system. */
4983 if (!NILP (noerror))
4984 coding_system = Qraw_text;
4985 else
4986 xsignal1 (Qcoding_system_error, coding_system);
4989 if (STRING_MULTIBYTE (object))
4990 object = code_convert_string (object, coding_system, Qnil, 1, 0, 1);
4992 size = SCHARS (object);
4993 size_byte = SBYTES (object);
4995 if (!NILP (start))
4997 CHECK_NUMBER (start);
4999 start_char = XINT (start);
5001 if (start_char < 0)
5002 start_char += size;
5004 start_byte = string_char_to_byte (object, start_char);
5007 if (NILP (end))
5009 end_char = size;
5010 end_byte = size_byte;
5012 else
5014 CHECK_NUMBER (end);
5016 end_char = XINT (end);
5018 if (end_char < 0)
5019 end_char += size;
5021 end_byte = string_char_to_byte (object, end_char);
5024 if (!(0 <= start_char && start_char <= end_char && end_char <= size))
5025 args_out_of_range_3 (object, make_number (start_char),
5026 make_number (end_char));
5028 else
5030 struct buffer *prev = current_buffer;
5032 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
5034 CHECK_BUFFER (object);
5036 bp = XBUFFER (object);
5037 if (bp != current_buffer)
5038 set_buffer_internal (bp);
5040 if (NILP (start))
5041 b = BEGV;
5042 else
5044 CHECK_NUMBER_COERCE_MARKER (start);
5045 b = XINT (start);
5048 if (NILP (end))
5049 e = ZV;
5050 else
5052 CHECK_NUMBER_COERCE_MARKER (end);
5053 e = XINT (end);
5056 if (b > e)
5057 temp = b, b = e, e = temp;
5059 if (!(BEGV <= b && e <= ZV))
5060 args_out_of_range (start, end);
5062 if (NILP (coding_system))
5064 /* Decide the coding-system to encode the data with.
5065 See fileio.c:Fwrite-region */
5067 if (!NILP (Vcoding_system_for_write))
5068 coding_system = Vcoding_system_for_write;
5069 else
5071 int force_raw_text = 0;
5073 coding_system = XBUFFER (object)->buffer_file_coding_system;
5074 if (NILP (coding_system)
5075 || NILP (Flocal_variable_p (Qbuffer_file_coding_system, Qnil)))
5077 coding_system = Qnil;
5078 if (NILP (current_buffer->enable_multibyte_characters))
5079 force_raw_text = 1;
5082 if (NILP (coding_system) && !NILP (Fbuffer_file_name(object)))
5084 /* Check file-coding-system-alist. */
5085 Lisp_Object args[4], val;
5087 args[0] = Qwrite_region; args[1] = start; args[2] = end;
5088 args[3] = Fbuffer_file_name(object);
5089 val = Ffind_operation_coding_system (4, args);
5090 if (CONSP (val) && !NILP (XCDR (val)))
5091 coding_system = XCDR (val);
5094 if (NILP (coding_system)
5095 && !NILP (XBUFFER (object)->buffer_file_coding_system))
5097 /* If we still have not decided a coding system, use the
5098 default value of buffer-file-coding-system. */
5099 coding_system = XBUFFER (object)->buffer_file_coding_system;
5102 if (!force_raw_text
5103 && !NILP (Ffboundp (Vselect_safe_coding_system_function)))
5104 /* Confirm that VAL can surely encode the current region. */
5105 coding_system = call4 (Vselect_safe_coding_system_function,
5106 make_number (b), make_number (e),
5107 coding_system, Qnil);
5109 if (force_raw_text)
5110 coding_system = Qraw_text;
5113 if (NILP (Fcoding_system_p (coding_system)))
5115 /* Invalid coding system. */
5117 if (!NILP (noerror))
5118 coding_system = Qraw_text;
5119 else
5120 xsignal1 (Qcoding_system_error, coding_system);
5124 object = make_buffer_string (b, e, 0);
5125 if (prev != current_buffer)
5126 set_buffer_internal (prev);
5127 /* Discard the unwind protect for recovering the current
5128 buffer. */
5129 specpdl_ptr--;
5131 if (STRING_MULTIBYTE (object))
5132 object = code_convert_string (object, coding_system, Qnil, 1, 0, 0);
5135 md5_buffer (SDATA (object) + start_byte,
5136 SBYTES (object) - (size_byte - end_byte),
5137 digest);
5139 for (i = 0; i < 16; i++)
5140 sprintf (&value[2 * i], "%02x", digest[i]);
5141 value[32] = '\0';
5143 return make_string (value, 32);
5147 void
5148 syms_of_fns ()
5150 /* Hash table stuff. */
5151 Qhash_table_p = intern ("hash-table-p");
5152 staticpro (&Qhash_table_p);
5153 Qeq = intern ("eq");
5154 staticpro (&Qeq);
5155 Qeql = intern ("eql");
5156 staticpro (&Qeql);
5157 Qequal = intern ("equal");
5158 staticpro (&Qequal);
5159 QCtest = intern (":test");
5160 staticpro (&QCtest);
5161 QCsize = intern (":size");
5162 staticpro (&QCsize);
5163 QCrehash_size = intern (":rehash-size");
5164 staticpro (&QCrehash_size);
5165 QCrehash_threshold = intern (":rehash-threshold");
5166 staticpro (&QCrehash_threshold);
5167 QCweakness = intern (":weakness");
5168 staticpro (&QCweakness);
5169 Qkey = intern ("key");
5170 staticpro (&Qkey);
5171 Qvalue = intern ("value");
5172 staticpro (&Qvalue);
5173 Qhash_table_test = intern ("hash-table-test");
5174 staticpro (&Qhash_table_test);
5175 Qkey_or_value = intern ("key-or-value");
5176 staticpro (&Qkey_or_value);
5177 Qkey_and_value = intern ("key-and-value");
5178 staticpro (&Qkey_and_value);
5180 defsubr (&Ssxhash);
5181 defsubr (&Smake_hash_table);
5182 defsubr (&Scopy_hash_table);
5183 defsubr (&Shash_table_count);
5184 defsubr (&Shash_table_rehash_size);
5185 defsubr (&Shash_table_rehash_threshold);
5186 defsubr (&Shash_table_size);
5187 defsubr (&Shash_table_test);
5188 defsubr (&Shash_table_weakness);
5189 defsubr (&Shash_table_p);
5190 defsubr (&Sclrhash);
5191 defsubr (&Sgethash);
5192 defsubr (&Sputhash);
5193 defsubr (&Sremhash);
5194 defsubr (&Smaphash);
5195 defsubr (&Sdefine_hash_table_test);
5197 Qstring_lessp = intern ("string-lessp");
5198 staticpro (&Qstring_lessp);
5199 Qprovide = intern ("provide");
5200 staticpro (&Qprovide);
5201 Qrequire = intern ("require");
5202 staticpro (&Qrequire);
5203 Qyes_or_no_p_history = intern ("yes-or-no-p-history");
5204 staticpro (&Qyes_or_no_p_history);
5205 Qcursor_in_echo_area = intern ("cursor-in-echo-area");
5206 staticpro (&Qcursor_in_echo_area);
5207 Qwidget_type = intern ("widget-type");
5208 staticpro (&Qwidget_type);
5210 staticpro (&string_char_byte_cache_string);
5211 string_char_byte_cache_string = Qnil;
5213 require_nesting_list = Qnil;
5214 staticpro (&require_nesting_list);
5216 Fset (Qyes_or_no_p_history, Qnil);
5218 DEFVAR_LISP ("features", &Vfeatures,
5219 doc: /* A list of symbols which are the features of the executing Emacs.
5220 Used by `featurep' and `require', and altered by `provide'. */);
5221 Vfeatures = Fcons (intern ("emacs"), Qnil);
5222 Qsubfeatures = intern ("subfeatures");
5223 staticpro (&Qsubfeatures);
5225 #ifdef HAVE_LANGINFO_CODESET
5226 Qcodeset = intern ("codeset");
5227 staticpro (&Qcodeset);
5228 Qdays = intern ("days");
5229 staticpro (&Qdays);
5230 Qmonths = intern ("months");
5231 staticpro (&Qmonths);
5232 Qpaper = intern ("paper");
5233 staticpro (&Qpaper);
5234 #endif /* HAVE_LANGINFO_CODESET */
5236 DEFVAR_BOOL ("use-dialog-box", &use_dialog_box,
5237 doc: /* *Non-nil means mouse commands use dialog boxes to ask questions.
5238 This applies to `y-or-n-p' and `yes-or-no-p' questions asked by commands
5239 invoked by mouse clicks and mouse menu items. */);
5240 use_dialog_box = 1;
5242 DEFVAR_BOOL ("use-file-dialog", &use_file_dialog,
5243 doc: /* *Non-nil means mouse commands use a file dialog to ask for files.
5244 This applies to commands from menus and tool bar buttons even when
5245 they are initiated from the keyboard. The value of `use-dialog-box'
5246 takes precedence over this variable, so a file dialog is only used if
5247 both `use-dialog-box' and this variable are non-nil. */);
5248 use_file_dialog = 1;
5250 defsubr (&Sidentity);
5251 defsubr (&Srandom);
5252 defsubr (&Slength);
5253 defsubr (&Ssafe_length);
5254 defsubr (&Sstring_bytes);
5255 defsubr (&Sstring_equal);
5256 defsubr (&Scompare_strings);
5257 defsubr (&Sstring_lessp);
5258 defsubr (&Sappend);
5259 defsubr (&Sconcat);
5260 defsubr (&Svconcat);
5261 defsubr (&Scopy_sequence);
5262 defsubr (&Sstring_make_multibyte);
5263 defsubr (&Sstring_make_unibyte);
5264 defsubr (&Sstring_as_multibyte);
5265 defsubr (&Sstring_as_unibyte);
5266 defsubr (&Sstring_to_multibyte);
5267 defsubr (&Sstring_to_unibyte);
5268 defsubr (&Scopy_alist);
5269 defsubr (&Ssubstring);
5270 defsubr (&Ssubstring_no_properties);
5271 defsubr (&Snthcdr);
5272 defsubr (&Snth);
5273 defsubr (&Selt);
5274 defsubr (&Smember);
5275 defsubr (&Smemq);
5276 defsubr (&Smemql);
5277 defsubr (&Sassq);
5278 defsubr (&Sassoc);
5279 defsubr (&Srassq);
5280 defsubr (&Srassoc);
5281 defsubr (&Sdelq);
5282 defsubr (&Sdelete);
5283 defsubr (&Snreverse);
5284 defsubr (&Sreverse);
5285 defsubr (&Ssort);
5286 defsubr (&Splist_get);
5287 defsubr (&Sget);
5288 defsubr (&Splist_put);
5289 defsubr (&Sput);
5290 defsubr (&Slax_plist_get);
5291 defsubr (&Slax_plist_put);
5292 defsubr (&Seql);
5293 defsubr (&Sequal);
5294 defsubr (&Sequal_including_properties);
5295 defsubr (&Sfillarray);
5296 defsubr (&Sclear_string);
5297 defsubr (&Snconc);
5298 defsubr (&Smapcar);
5299 defsubr (&Smapc);
5300 defsubr (&Smapconcat);
5301 defsubr (&Sy_or_n_p);
5302 defsubr (&Syes_or_no_p);
5303 defsubr (&Sload_average);
5304 defsubr (&Sfeaturep);
5305 defsubr (&Srequire);
5306 defsubr (&Sprovide);
5307 defsubr (&Splist_member);
5308 defsubr (&Swidget_put);
5309 defsubr (&Swidget_get);
5310 defsubr (&Swidget_apply);
5311 defsubr (&Sbase64_encode_region);
5312 defsubr (&Sbase64_decode_region);
5313 defsubr (&Sbase64_encode_string);
5314 defsubr (&Sbase64_decode_string);
5315 defsubr (&Smd5);
5316 defsubr (&Slocale_info);
5320 void
5321 init_fns ()
5325 /* arch-tag: 787f8219-5b74-46bd-8469-7e1cc475fa31
5326 (do not change this comment) */