Add new VC methods: vc-log-incoming and vc-log-outgoing.
[emacs.git] / src / fns.c
blob0100ff228f28d847f7fcb7ab56b052f79d52cac6
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, 2009, 2010 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>
27 #include <setjmp.h>
29 /* Note on some machines this defines `vector' as a typedef,
30 so make sure we don't use that name in this file. */
31 #undef vector
32 #define vector *****
34 #include "lisp.h"
35 #include "commands.h"
36 #include "character.h"
37 #include "coding.h"
38 #include "buffer.h"
39 #include "keyboard.h"
40 #include "keymap.h"
41 #include "intervals.h"
42 #include "frame.h"
43 #include "window.h"
44 #include "blockinput.h"
45 #ifdef HAVE_MENUS
46 #if defined (HAVE_X_WINDOWS)
47 #include "xterm.h"
48 #endif
49 #endif /* HAVE_MENUS */
51 #ifndef NULL
52 #define NULL ((POINTER_TYPE *)0)
53 #endif
55 /* Nonzero enables use of dialog boxes for questions
56 asked by mouse commands. */
57 int use_dialog_box;
59 /* Nonzero enables use of a file dialog for file name
60 questions asked by mouse commands. */
61 int use_file_dialog;
63 extern int minibuffer_auto_raise;
64 extern Lisp_Object minibuf_window;
65 extern Lisp_Object Vlocale_coding_system;
66 extern int load_in_progress;
68 Lisp_Object Qstring_lessp, Qprovide, Qrequire;
69 Lisp_Object Qyes_or_no_p_history;
70 Lisp_Object Qcursor_in_echo_area;
71 Lisp_Object Qwidget_type;
72 Lisp_Object Qcodeset, Qdays, Qmonths, Qpaper;
74 extern Lisp_Object Qinput_method_function;
76 static int internal_equal P_ ((Lisp_Object , Lisp_Object, int, int));
78 extern long get_random ();
79 extern void seed_random P_ ((long));
81 #ifndef HAVE_UNISTD_H
82 extern long time ();
83 #endif
85 DEFUN ("identity", Fidentity, Sidentity, 1, 1, 0,
86 doc: /* Return the argument unchanged. */)
87 (arg)
88 Lisp_Object arg;
90 return arg;
93 DEFUN ("random", Frandom, Srandom, 0, 1, 0,
94 doc: /* Return a pseudo-random number.
95 All integers representable in Lisp are equally likely.
96 On most systems, this is 29 bits' worth.
97 With positive integer LIMIT, return random number in interval [0,LIMIT).
98 With argument t, set the random number seed from the current time and pid.
99 Other values of LIMIT are ignored. */)
100 (limit)
101 Lisp_Object limit;
103 EMACS_INT val;
104 Lisp_Object lispy_val;
105 unsigned long denominator;
107 if (EQ (limit, Qt))
108 seed_random (getpid () + time (NULL));
109 if (NATNUMP (limit) && XFASTINT (limit) != 0)
111 /* Try to take our random number from the higher bits of VAL,
112 not the lower, since (says Gentzel) the low bits of `random'
113 are less random than the higher ones. We do this by using the
114 quotient rather than the remainder. At the high end of the RNG
115 it's possible to get a quotient larger than n; discarding
116 these values eliminates the bias that would otherwise appear
117 when using a large n. */
118 denominator = ((unsigned long)1 << VALBITS) / XFASTINT (limit);
120 val = get_random () / denominator;
121 while (val >= XFASTINT (limit));
123 else
124 val = get_random ();
125 XSETINT (lispy_val, val);
126 return lispy_val;
129 /* Random data-structure functions */
131 DEFUN ("length", Flength, Slength, 1, 1, 0,
132 doc: /* Return the length of vector, list or string SEQUENCE.
133 A byte-code function object is also allowed.
134 If the string contains multibyte characters, this is not necessarily
135 the number of bytes in the string; it is the number of characters.
136 To get the number of bytes, use `string-bytes'. */)
137 (sequence)
138 register Lisp_Object sequence;
140 register Lisp_Object val;
141 register int i;
143 if (STRINGP (sequence))
144 XSETFASTINT (val, SCHARS (sequence));
145 else if (VECTORP (sequence))
146 XSETFASTINT (val, ASIZE (sequence));
147 else if (CHAR_TABLE_P (sequence))
148 XSETFASTINT (val, MAX_CHAR);
149 else if (BOOL_VECTOR_P (sequence))
150 XSETFASTINT (val, XBOOL_VECTOR (sequence)->size);
151 else if (COMPILEDP (sequence))
152 XSETFASTINT (val, ASIZE (sequence) & PSEUDOVECTOR_SIZE_MASK);
153 else if (CONSP (sequence))
155 i = 0;
156 while (CONSP (sequence))
158 sequence = XCDR (sequence);
159 ++i;
161 if (!CONSP (sequence))
162 break;
164 sequence = XCDR (sequence);
165 ++i;
166 QUIT;
169 CHECK_LIST_END (sequence, sequence);
171 val = make_number (i);
173 else if (NILP (sequence))
174 XSETFASTINT (val, 0);
175 else
176 wrong_type_argument (Qsequencep, sequence);
178 return val;
181 /* This does not check for quits. That is safe since it must terminate. */
183 DEFUN ("safe-length", Fsafe_length, Ssafe_length, 1, 1, 0,
184 doc: /* Return the length of a list, but avoid error or infinite loop.
185 This function never gets an error. If LIST is not really a list,
186 it returns 0. If LIST is circular, it returns a finite value
187 which is at least the number of distinct elements. */)
188 (list)
189 Lisp_Object list;
191 Lisp_Object tail, halftail, length;
192 int len = 0;
194 /* halftail is used to detect circular lists. */
195 halftail = list;
196 for (tail = list; CONSP (tail); tail = XCDR (tail))
198 if (EQ (tail, halftail) && len != 0)
199 break;
200 len++;
201 if ((len & 1) == 0)
202 halftail = XCDR (halftail);
205 XSETINT (length, len);
206 return length;
209 DEFUN ("string-bytes", Fstring_bytes, Sstring_bytes, 1, 1, 0,
210 doc: /* Return the number of bytes in STRING.
211 If STRING is multibyte, this may be greater than the length of STRING. */)
212 (string)
213 Lisp_Object string;
215 CHECK_STRING (string);
216 return make_number (SBYTES (string));
219 DEFUN ("string-equal", Fstring_equal, Sstring_equal, 2, 2, 0,
220 doc: /* Return t if two strings have identical contents.
221 Case is significant, but text properties are ignored.
222 Symbols are also allowed; their print names are used instead. */)
223 (s1, s2)
224 register Lisp_Object s1, s2;
226 if (SYMBOLP (s1))
227 s1 = SYMBOL_NAME (s1);
228 if (SYMBOLP (s2))
229 s2 = SYMBOL_NAME (s2);
230 CHECK_STRING (s1);
231 CHECK_STRING (s2);
233 if (SCHARS (s1) != SCHARS (s2)
234 || SBYTES (s1) != SBYTES (s2)
235 || bcmp (SDATA (s1), SDATA (s2), SBYTES (s1)))
236 return Qnil;
237 return Qt;
240 DEFUN ("compare-strings", Fcompare_strings,
241 Scompare_strings, 6, 7, 0,
242 doc: /* Compare the contents of two strings, converting to multibyte if needed.
243 In string STR1, skip the first START1 characters and stop at END1.
244 In string STR2, skip the first START2 characters and stop at END2.
245 END1 and END2 default to the full lengths of the respective strings.
247 Case is significant in this comparison if IGNORE-CASE is nil.
248 Unibyte strings are converted to multibyte for comparison.
250 The value is t if the strings (or specified portions) match.
251 If string STR1 is less, the value is a negative number N;
252 - 1 - N is the number of characters that match at the beginning.
253 If string STR1 is greater, the value is a positive number N;
254 N - 1 is the number of characters that match at the beginning. */)
255 (str1, start1, end1, str2, start2, end2, ignore_case)
256 Lisp_Object str1, start1, end1, start2, str2, end2, ignore_case;
258 register int end1_char, end2_char;
259 register int i1, i1_byte, i2, i2_byte;
261 CHECK_STRING (str1);
262 CHECK_STRING (str2);
263 if (NILP (start1))
264 start1 = make_number (0);
265 if (NILP (start2))
266 start2 = make_number (0);
267 CHECK_NATNUM (start1);
268 CHECK_NATNUM (start2);
269 if (! NILP (end1))
270 CHECK_NATNUM (end1);
271 if (! NILP (end2))
272 CHECK_NATNUM (end2);
274 i1 = XINT (start1);
275 i2 = XINT (start2);
277 i1_byte = string_char_to_byte (str1, i1);
278 i2_byte = string_char_to_byte (str2, i2);
280 end1_char = SCHARS (str1);
281 if (! NILP (end1) && end1_char > XINT (end1))
282 end1_char = XINT (end1);
284 end2_char = SCHARS (str2);
285 if (! NILP (end2) && end2_char > XINT (end2))
286 end2_char = XINT (end2);
288 while (i1 < end1_char && i2 < end2_char)
290 /* When we find a mismatch, we must compare the
291 characters, not just the bytes. */
292 int c1, c2;
294 if (STRING_MULTIBYTE (str1))
295 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c1, str1, i1, i1_byte);
296 else
298 c1 = SREF (str1, i1++);
299 MAKE_CHAR_MULTIBYTE (c1);
302 if (STRING_MULTIBYTE (str2))
303 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c2, str2, i2, i2_byte);
304 else
306 c2 = SREF (str2, i2++);
307 MAKE_CHAR_MULTIBYTE (c2);
310 if (c1 == c2)
311 continue;
313 if (! NILP (ignore_case))
315 Lisp_Object tem;
317 tem = Fupcase (make_number (c1));
318 c1 = XINT (tem);
319 tem = Fupcase (make_number (c2));
320 c2 = XINT (tem);
323 if (c1 == c2)
324 continue;
326 /* Note that I1 has already been incremented
327 past the character that we are comparing;
328 hence we don't add or subtract 1 here. */
329 if (c1 < c2)
330 return make_number (- i1 + XINT (start1));
331 else
332 return make_number (i1 - XINT (start1));
335 if (i1 < end1_char)
336 return make_number (i1 - XINT (start1) + 1);
337 if (i2 < end2_char)
338 return make_number (- i1 + XINT (start1) - 1);
340 return Qt;
343 DEFUN ("string-lessp", Fstring_lessp, Sstring_lessp, 2, 2, 0,
344 doc: /* Return t if first arg string is less than second in lexicographic order.
345 Case is significant.
346 Symbols are also allowed; their print names are used instead. */)
347 (s1, s2)
348 register Lisp_Object s1, s2;
350 register int end;
351 register int i1, i1_byte, i2, i2_byte;
353 if (SYMBOLP (s1))
354 s1 = SYMBOL_NAME (s1);
355 if (SYMBOLP (s2))
356 s2 = SYMBOL_NAME (s2);
357 CHECK_STRING (s1);
358 CHECK_STRING (s2);
360 i1 = i1_byte = i2 = i2_byte = 0;
362 end = SCHARS (s1);
363 if (end > SCHARS (s2))
364 end = SCHARS (s2);
366 while (i1 < end)
368 /* When we find a mismatch, we must compare the
369 characters, not just the bytes. */
370 int c1, c2;
372 FETCH_STRING_CHAR_ADVANCE (c1, s1, i1, i1_byte);
373 FETCH_STRING_CHAR_ADVANCE (c2, s2, i2, i2_byte);
375 if (c1 != c2)
376 return c1 < c2 ? Qt : Qnil;
378 return i1 < SCHARS (s2) ? Qt : Qnil;
381 #if __GNUC__
382 /* "gcc -O3" enables automatic function inlining, which optimizes out
383 the arguments for the invocations of this function, whereas it
384 expects these values on the stack. */
385 static Lisp_Object concat P_ ((int nargs, Lisp_Object *args, enum Lisp_Type target_type, int last_special)) __attribute__((noinline));
386 #else /* !__GNUC__ */
387 static Lisp_Object concat P_ ((int nargs, Lisp_Object *args, enum Lisp_Type target_type, int last_special));
388 #endif
390 /* ARGSUSED */
391 Lisp_Object
392 concat2 (s1, s2)
393 Lisp_Object s1, s2;
395 #ifdef NO_ARG_ARRAY
396 Lisp_Object args[2];
397 args[0] = s1;
398 args[1] = s2;
399 return concat (2, args, Lisp_String, 0);
400 #else
401 return concat (2, &s1, Lisp_String, 0);
402 #endif /* NO_ARG_ARRAY */
405 /* ARGSUSED */
406 Lisp_Object
407 concat3 (s1, s2, s3)
408 Lisp_Object s1, s2, s3;
410 #ifdef NO_ARG_ARRAY
411 Lisp_Object args[3];
412 args[0] = s1;
413 args[1] = s2;
414 args[2] = s3;
415 return concat (3, args, Lisp_String, 0);
416 #else
417 return concat (3, &s1, Lisp_String, 0);
418 #endif /* NO_ARG_ARRAY */
421 DEFUN ("append", Fappend, Sappend, 0, MANY, 0,
422 doc: /* Concatenate all the arguments and make the result a list.
423 The result is a list whose elements are the elements of all the arguments.
424 Each argument may be a list, vector or string.
425 The last argument is not copied, just used as the tail of the new list.
426 usage: (append &rest SEQUENCES) */)
427 (nargs, args)
428 int nargs;
429 Lisp_Object *args;
431 return concat (nargs, args, Lisp_Cons, 1);
434 DEFUN ("concat", Fconcat, Sconcat, 0, MANY, 0,
435 doc: /* Concatenate all the arguments and make the result a string.
436 The result is a string whose elements are the elements of all the arguments.
437 Each argument may be a string or a list or vector of characters (integers).
438 usage: (concat &rest SEQUENCES) */)
439 (nargs, args)
440 int nargs;
441 Lisp_Object *args;
443 return concat (nargs, args, Lisp_String, 0);
446 DEFUN ("vconcat", Fvconcat, Svconcat, 0, MANY, 0,
447 doc: /* Concatenate all the arguments and make the result a vector.
448 The result is a vector whose elements are the elements of all the arguments.
449 Each argument may be a list, vector or string.
450 usage: (vconcat &rest SEQUENCES) */)
451 (nargs, args)
452 int nargs;
453 Lisp_Object *args;
455 return concat (nargs, args, Lisp_Vectorlike, 0);
459 DEFUN ("copy-sequence", Fcopy_sequence, Scopy_sequence, 1, 1, 0,
460 doc: /* Return a copy of a list, vector, string or char-table.
461 The elements of a list or vector are not copied; they are shared
462 with the original. */)
463 (arg)
464 Lisp_Object arg;
466 if (NILP (arg)) return arg;
468 if (CHAR_TABLE_P (arg))
470 return copy_char_table (arg);
473 if (BOOL_VECTOR_P (arg))
475 Lisp_Object val;
476 int size_in_chars
477 = ((XBOOL_VECTOR (arg)->size + BOOL_VECTOR_BITS_PER_CHAR - 1)
478 / BOOL_VECTOR_BITS_PER_CHAR);
480 val = Fmake_bool_vector (Flength (arg), Qnil);
481 bcopy (XBOOL_VECTOR (arg)->data, XBOOL_VECTOR (val)->data,
482 size_in_chars);
483 return val;
486 if (!CONSP (arg) && !VECTORP (arg) && !STRINGP (arg))
487 wrong_type_argument (Qsequencep, arg);
489 return concat (1, &arg, CONSP (arg) ? Lisp_Cons : XTYPE (arg), 0);
492 /* This structure holds information of an argument of `concat' that is
493 a string and has text properties to be copied. */
494 struct textprop_rec
496 int argnum; /* refer to ARGS (arguments of `concat') */
497 int from; /* refer to ARGS[argnum] (argument string) */
498 int to; /* refer to VAL (the target string) */
501 static Lisp_Object
502 concat (nargs, args, target_type, last_special)
503 int nargs;
504 Lisp_Object *args;
505 enum Lisp_Type target_type;
506 int last_special;
508 Lisp_Object val;
509 register Lisp_Object tail;
510 register Lisp_Object this;
511 int toindex;
512 int toindex_byte = 0;
513 register int result_len;
514 register int result_len_byte;
515 register int argnum;
516 Lisp_Object last_tail;
517 Lisp_Object prev;
518 int some_multibyte;
519 /* When we make a multibyte string, we can't copy text properties
520 while concatinating each string because the length of resulting
521 string can't be decided until we finish the whole concatination.
522 So, we record strings that have text properties to be copied
523 here, and copy the text properties after the concatination. */
524 struct textprop_rec *textprops = NULL;
525 /* Number of elments in textprops. */
526 int num_textprops = 0;
527 USE_SAFE_ALLOCA;
529 tail = Qnil;
531 /* In append, the last arg isn't treated like the others */
532 if (last_special && nargs > 0)
534 nargs--;
535 last_tail = args[nargs];
537 else
538 last_tail = Qnil;
540 /* Check each argument. */
541 for (argnum = 0; argnum < nargs; argnum++)
543 this = args[argnum];
544 if (!(CONSP (this) || NILP (this) || VECTORP (this) || STRINGP (this)
545 || COMPILEDP (this) || BOOL_VECTOR_P (this)))
546 wrong_type_argument (Qsequencep, this);
549 /* Compute total length in chars of arguments in RESULT_LEN.
550 If desired output is a string, also compute length in bytes
551 in RESULT_LEN_BYTE, and determine in SOME_MULTIBYTE
552 whether the result should be a multibyte string. */
553 result_len_byte = 0;
554 result_len = 0;
555 some_multibyte = 0;
556 for (argnum = 0; argnum < nargs; argnum++)
558 int len;
559 this = args[argnum];
560 len = XFASTINT (Flength (this));
561 if (target_type == Lisp_String)
563 /* We must count the number of bytes needed in the string
564 as well as the number of characters. */
565 int i;
566 Lisp_Object ch;
567 int this_len_byte;
569 if (VECTORP (this))
570 for (i = 0; i < len; i++)
572 ch = AREF (this, i);
573 CHECK_CHARACTER (ch);
574 this_len_byte = CHAR_BYTES (XINT (ch));
575 result_len_byte += this_len_byte;
576 if (! ASCII_CHAR_P (XINT (ch)) && ! CHAR_BYTE8_P (XINT (ch)))
577 some_multibyte = 1;
579 else if (BOOL_VECTOR_P (this) && XBOOL_VECTOR (this)->size > 0)
580 wrong_type_argument (Qintegerp, Faref (this, make_number (0)));
581 else if (CONSP (this))
582 for (; CONSP (this); this = XCDR (this))
584 ch = XCAR (this);
585 CHECK_CHARACTER (ch);
586 this_len_byte = CHAR_BYTES (XINT (ch));
587 result_len_byte += this_len_byte;
588 if (! ASCII_CHAR_P (XINT (ch)) && ! CHAR_BYTE8_P (XINT (ch)))
589 some_multibyte = 1;
591 else if (STRINGP (this))
593 if (STRING_MULTIBYTE (this))
595 some_multibyte = 1;
596 result_len_byte += SBYTES (this);
598 else
599 result_len_byte += count_size_as_multibyte (SDATA (this),
600 SCHARS (this));
604 result_len += len;
605 if (result_len < 0)
606 error ("String overflow");
609 if (! some_multibyte)
610 result_len_byte = result_len;
612 /* Create the output object. */
613 if (target_type == Lisp_Cons)
614 val = Fmake_list (make_number (result_len), Qnil);
615 else if (target_type == Lisp_Vectorlike)
616 val = Fmake_vector (make_number (result_len), Qnil);
617 else if (some_multibyte)
618 val = make_uninit_multibyte_string (result_len, result_len_byte);
619 else
620 val = make_uninit_string (result_len);
622 /* In `append', if all but last arg are nil, return last arg. */
623 if (target_type == Lisp_Cons && EQ (val, Qnil))
624 return last_tail;
626 /* Copy the contents of the args into the result. */
627 if (CONSP (val))
628 tail = val, toindex = -1; /* -1 in toindex is flag we are making a list */
629 else
630 toindex = 0, toindex_byte = 0;
632 prev = Qnil;
633 if (STRINGP (val))
634 SAFE_ALLOCA (textprops, struct textprop_rec *, sizeof (struct textprop_rec) * nargs);
636 for (argnum = 0; argnum < nargs; argnum++)
638 Lisp_Object thislen;
639 int thisleni = 0;
640 register unsigned int thisindex = 0;
641 register unsigned int thisindex_byte = 0;
643 this = args[argnum];
644 if (!CONSP (this))
645 thislen = Flength (this), thisleni = XINT (thislen);
647 /* Between strings of the same kind, copy fast. */
648 if (STRINGP (this) && STRINGP (val)
649 && STRING_MULTIBYTE (this) == some_multibyte)
651 int thislen_byte = SBYTES (this);
653 bcopy (SDATA (this), SDATA (val) + toindex_byte,
654 SBYTES (this));
655 if (! NULL_INTERVAL_P (STRING_INTERVALS (this)))
657 textprops[num_textprops].argnum = argnum;
658 textprops[num_textprops].from = 0;
659 textprops[num_textprops++].to = toindex;
661 toindex_byte += thislen_byte;
662 toindex += thisleni;
664 /* Copy a single-byte string to a multibyte string. */
665 else if (STRINGP (this) && STRINGP (val))
667 if (! NULL_INTERVAL_P (STRING_INTERVALS (this)))
669 textprops[num_textprops].argnum = argnum;
670 textprops[num_textprops].from = 0;
671 textprops[num_textprops++].to = toindex;
673 toindex_byte += copy_text (SDATA (this),
674 SDATA (val) + toindex_byte,
675 SCHARS (this), 0, 1);
676 toindex += thisleni;
678 else
679 /* Copy element by element. */
680 while (1)
682 register Lisp_Object elt;
684 /* Fetch next element of `this' arg into `elt', or break if
685 `this' is exhausted. */
686 if (NILP (this)) break;
687 if (CONSP (this))
688 elt = XCAR (this), this = XCDR (this);
689 else if (thisindex >= thisleni)
690 break;
691 else if (STRINGP (this))
693 int c;
694 if (STRING_MULTIBYTE (this))
696 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, this,
697 thisindex,
698 thisindex_byte);
699 XSETFASTINT (elt, c);
701 else
703 XSETFASTINT (elt, SREF (this, thisindex)); thisindex++;
704 if (some_multibyte
705 && !ASCII_CHAR_P (XINT (elt))
706 && XINT (elt) < 0400)
708 c = BYTE8_TO_CHAR (XINT (elt));
709 XSETINT (elt, c);
713 else if (BOOL_VECTOR_P (this))
715 int byte;
716 byte = XBOOL_VECTOR (this)->data[thisindex / BOOL_VECTOR_BITS_PER_CHAR];
717 if (byte & (1 << (thisindex % BOOL_VECTOR_BITS_PER_CHAR)))
718 elt = Qt;
719 else
720 elt = Qnil;
721 thisindex++;
723 else
725 elt = AREF (this, thisindex);
726 thisindex++;
729 /* Store this element into the result. */
730 if (toindex < 0)
732 XSETCAR (tail, elt);
733 prev = tail;
734 tail = XCDR (tail);
736 else if (VECTORP (val))
738 ASET (val, toindex, elt);
739 toindex++;
741 else
743 CHECK_NUMBER (elt);
744 if (some_multibyte)
745 toindex_byte += CHAR_STRING (XINT (elt),
746 SDATA (val) + toindex_byte);
747 else
748 SSET (val, toindex_byte++, XINT (elt));
749 toindex++;
753 if (!NILP (prev))
754 XSETCDR (prev, last_tail);
756 if (num_textprops > 0)
758 Lisp_Object props;
759 int last_to_end = -1;
761 for (argnum = 0; argnum < num_textprops; argnum++)
763 this = args[textprops[argnum].argnum];
764 props = text_property_list (this,
765 make_number (0),
766 make_number (SCHARS (this)),
767 Qnil);
768 /* If successive arguments have properites, be sure that the
769 value of `composition' property be the copy. */
770 if (last_to_end == textprops[argnum].to)
771 make_composition_value_copy (props);
772 add_text_properties_from_list (val, props,
773 make_number (textprops[argnum].to));
774 last_to_end = textprops[argnum].to + SCHARS (this);
778 SAFE_FREE ();
779 return val;
782 static Lisp_Object string_char_byte_cache_string;
783 static EMACS_INT string_char_byte_cache_charpos;
784 static EMACS_INT string_char_byte_cache_bytepos;
786 void
787 clear_string_char_byte_cache ()
789 string_char_byte_cache_string = Qnil;
792 /* Return the byte index corresponding to CHAR_INDEX in STRING. */
794 EMACS_INT
795 string_char_to_byte (string, char_index)
796 Lisp_Object string;
797 EMACS_INT char_index;
799 EMACS_INT i_byte;
800 EMACS_INT best_below, best_below_byte;
801 EMACS_INT best_above, best_above_byte;
803 best_below = best_below_byte = 0;
804 best_above = SCHARS (string);
805 best_above_byte = SBYTES (string);
806 if (best_above == best_above_byte)
807 return char_index;
809 if (EQ (string, string_char_byte_cache_string))
811 if (string_char_byte_cache_charpos < char_index)
813 best_below = string_char_byte_cache_charpos;
814 best_below_byte = string_char_byte_cache_bytepos;
816 else
818 best_above = string_char_byte_cache_charpos;
819 best_above_byte = string_char_byte_cache_bytepos;
823 if (char_index - best_below < best_above - char_index)
825 unsigned char *p = SDATA (string) + best_below_byte;
827 while (best_below < char_index)
829 p += BYTES_BY_CHAR_HEAD (*p);
830 best_below++;
832 i_byte = p - SDATA (string);
834 else
836 unsigned char *p = SDATA (string) + best_above_byte;
838 while (best_above > char_index)
840 p--;
841 while (!CHAR_HEAD_P (*p)) p--;
842 best_above--;
844 i_byte = p - SDATA (string);
847 string_char_byte_cache_bytepos = i_byte;
848 string_char_byte_cache_charpos = char_index;
849 string_char_byte_cache_string = string;
851 return i_byte;
854 /* Return the character index corresponding to BYTE_INDEX in STRING. */
856 EMACS_INT
857 string_byte_to_char (string, byte_index)
858 Lisp_Object string;
859 EMACS_INT byte_index;
861 EMACS_INT i, i_byte;
862 EMACS_INT best_below, best_below_byte;
863 EMACS_INT best_above, best_above_byte;
865 best_below = best_below_byte = 0;
866 best_above = SCHARS (string);
867 best_above_byte = SBYTES (string);
868 if (best_above == best_above_byte)
869 return byte_index;
871 if (EQ (string, string_char_byte_cache_string))
873 if (string_char_byte_cache_bytepos < byte_index)
875 best_below = string_char_byte_cache_charpos;
876 best_below_byte = string_char_byte_cache_bytepos;
878 else
880 best_above = string_char_byte_cache_charpos;
881 best_above_byte = string_char_byte_cache_bytepos;
885 if (byte_index - best_below_byte < best_above_byte - byte_index)
887 unsigned char *p = SDATA (string) + best_below_byte;
888 unsigned char *pend = SDATA (string) + byte_index;
890 while (p < pend)
892 p += BYTES_BY_CHAR_HEAD (*p);
893 best_below++;
895 i = best_below;
896 i_byte = p - SDATA (string);
898 else
900 unsigned char *p = SDATA (string) + best_above_byte;
901 unsigned char *pbeg = SDATA (string) + byte_index;
903 while (p > pbeg)
905 p--;
906 while (!CHAR_HEAD_P (*p)) p--;
907 best_above--;
909 i = best_above;
910 i_byte = p - SDATA (string);
913 string_char_byte_cache_bytepos = i_byte;
914 string_char_byte_cache_charpos = i;
915 string_char_byte_cache_string = string;
917 return i;
920 /* Convert STRING to a multibyte string. */
922 Lisp_Object
923 string_make_multibyte (string)
924 Lisp_Object string;
926 unsigned char *buf;
927 EMACS_INT nbytes;
928 Lisp_Object ret;
929 USE_SAFE_ALLOCA;
931 if (STRING_MULTIBYTE (string))
932 return string;
934 nbytes = count_size_as_multibyte (SDATA (string),
935 SCHARS (string));
936 /* If all the chars are ASCII, they won't need any more bytes
937 once converted. In that case, we can return STRING itself. */
938 if (nbytes == SBYTES (string))
939 return string;
941 SAFE_ALLOCA (buf, unsigned char *, nbytes);
942 copy_text (SDATA (string), buf, SBYTES (string),
943 0, 1);
945 ret = make_multibyte_string (buf, SCHARS (string), nbytes);
946 SAFE_FREE ();
948 return ret;
952 /* Convert STRING (if unibyte) to a multibyte string without changing
953 the number of characters. Characters 0200 trough 0237 are
954 converted to eight-bit characters. */
956 Lisp_Object
957 string_to_multibyte (string)
958 Lisp_Object string;
960 unsigned char *buf;
961 EMACS_INT nbytes;
962 Lisp_Object ret;
963 USE_SAFE_ALLOCA;
965 if (STRING_MULTIBYTE (string))
966 return string;
968 nbytes = parse_str_to_multibyte (SDATA (string), SBYTES (string));
969 /* If all the chars are ASCII, they won't need any more bytes once
970 converted. */
971 if (nbytes == SBYTES (string))
972 return make_multibyte_string (SDATA (string), nbytes, nbytes);
974 SAFE_ALLOCA (buf, unsigned char *, nbytes);
975 bcopy (SDATA (string), buf, SBYTES (string));
976 str_to_multibyte (buf, nbytes, SBYTES (string));
978 ret = make_multibyte_string (buf, SCHARS (string), nbytes);
979 SAFE_FREE ();
981 return ret;
985 /* Convert STRING to a single-byte string. */
987 Lisp_Object
988 string_make_unibyte (string)
989 Lisp_Object string;
991 int nchars;
992 unsigned char *buf;
993 Lisp_Object ret;
994 USE_SAFE_ALLOCA;
996 if (! STRING_MULTIBYTE (string))
997 return string;
999 nchars = SCHARS (string);
1001 SAFE_ALLOCA (buf, unsigned char *, nchars);
1002 copy_text (SDATA (string), buf, SBYTES (string),
1003 1, 0);
1005 ret = make_unibyte_string (buf, nchars);
1006 SAFE_FREE ();
1008 return ret;
1011 DEFUN ("string-make-multibyte", Fstring_make_multibyte, Sstring_make_multibyte,
1012 1, 1, 0,
1013 doc: /* Return the multibyte equivalent of STRING.
1014 If STRING is unibyte and contains non-ASCII characters, the function
1015 `unibyte-char-to-multibyte' is used to convert each unibyte character
1016 to a multibyte character. In this case, the returned string is a
1017 newly created string with no text properties. If STRING is multibyte
1018 or entirely ASCII, it is returned unchanged. In particular, when
1019 STRING is unibyte and entirely ASCII, the returned string is unibyte.
1020 \(When the characters are all ASCII, Emacs primitives will treat the
1021 string the same way whether it is unibyte or multibyte.) */)
1022 (string)
1023 Lisp_Object string;
1025 CHECK_STRING (string);
1027 return string_make_multibyte (string);
1030 DEFUN ("string-make-unibyte", Fstring_make_unibyte, Sstring_make_unibyte,
1031 1, 1, 0,
1032 doc: /* Return the unibyte equivalent of STRING.
1033 Multibyte character codes are converted to unibyte according to
1034 `nonascii-translation-table' or, if that is nil, `nonascii-insert-offset'.
1035 If the lookup in the translation table fails, this function takes just
1036 the low 8 bits of each character. */)
1037 (string)
1038 Lisp_Object string;
1040 CHECK_STRING (string);
1042 return string_make_unibyte (string);
1045 DEFUN ("string-as-unibyte", Fstring_as_unibyte, Sstring_as_unibyte,
1046 1, 1, 0,
1047 doc: /* Return a unibyte string with the same individual bytes as STRING.
1048 If STRING is unibyte, the result is STRING itself.
1049 Otherwise it is a newly created string, with no text properties.
1050 If STRING is multibyte and contains a character of charset
1051 `eight-bit', it is converted to the corresponding single byte. */)
1052 (string)
1053 Lisp_Object string;
1055 CHECK_STRING (string);
1057 if (STRING_MULTIBYTE (string))
1059 int bytes = SBYTES (string);
1060 unsigned char *str = (unsigned char *) xmalloc (bytes);
1062 bcopy (SDATA (string), str, bytes);
1063 bytes = str_as_unibyte (str, bytes);
1064 string = make_unibyte_string (str, bytes);
1065 xfree (str);
1067 return string;
1070 DEFUN ("string-as-multibyte", Fstring_as_multibyte, Sstring_as_multibyte,
1071 1, 1, 0,
1072 doc: /* Return a multibyte string with the same individual bytes as STRING.
1073 If STRING is multibyte, the result is STRING itself.
1074 Otherwise it is a newly created string, with no text properties.
1076 If STRING is unibyte and contains an individual 8-bit byte (i.e. not
1077 part of a correct utf-8 sequence), it is converted to the corresponding
1078 multibyte character of charset `eight-bit'.
1079 See also `string-to-multibyte'.
1081 Beware, this often doesn't really do what you think it does.
1082 It is similar to (decode-coding-string STRING 'utf-8-emacs).
1083 If you're not sure, whether to use `string-as-multibyte' or
1084 `string-to-multibyte', use `string-to-multibyte'. */)
1085 (string)
1086 Lisp_Object string;
1088 CHECK_STRING (string);
1090 if (! STRING_MULTIBYTE (string))
1092 Lisp_Object new_string;
1093 int nchars, nbytes;
1095 parse_str_as_multibyte (SDATA (string),
1096 SBYTES (string),
1097 &nchars, &nbytes);
1098 new_string = make_uninit_multibyte_string (nchars, nbytes);
1099 bcopy (SDATA (string), SDATA (new_string),
1100 SBYTES (string));
1101 if (nbytes != SBYTES (string))
1102 str_as_multibyte (SDATA (new_string), nbytes,
1103 SBYTES (string), NULL);
1104 string = new_string;
1105 STRING_SET_INTERVALS (string, NULL_INTERVAL);
1107 return string;
1110 DEFUN ("string-to-multibyte", Fstring_to_multibyte, Sstring_to_multibyte,
1111 1, 1, 0,
1112 doc: /* Return a multibyte string with the same individual chars as STRING.
1113 If STRING is multibyte, the result is STRING itself.
1114 Otherwise it is a newly created string, with no text properties.
1116 If STRING is unibyte and contains an 8-bit byte, it is converted to
1117 the corresponding multibyte character of charset `eight-bit'.
1119 This differs from `string-as-multibyte' by converting each byte of a correct
1120 utf-8 sequence to an eight-bit character, not just bytes that don't form a
1121 correct sequence. */)
1122 (string)
1123 Lisp_Object string;
1125 CHECK_STRING (string);
1127 return string_to_multibyte (string);
1130 DEFUN ("string-to-unibyte", Fstring_to_unibyte, Sstring_to_unibyte,
1131 1, 1, 0,
1132 doc: /* Return a unibyte string with the same individual chars as STRING.
1133 If STRING is unibyte, the result is STRING itself.
1134 Otherwise it is a newly created string, with no text properties,
1135 where each `eight-bit' character is converted to the corresponding byte.
1136 If STRING contains a non-ASCII, non-`eight-bit' character,
1137 an error is signaled. */)
1138 (string)
1139 Lisp_Object string;
1141 CHECK_STRING (string);
1143 if (STRING_MULTIBYTE (string))
1145 EMACS_INT chars = SCHARS (string);
1146 unsigned char *str = (unsigned char *) xmalloc (chars);
1147 EMACS_INT converted = str_to_unibyte (SDATA (string), str, chars, 0);
1149 if (converted < chars)
1150 error ("Can't convert the %dth character to unibyte", converted);
1151 string = make_unibyte_string (str, chars);
1152 xfree (str);
1154 return string;
1158 DEFUN ("copy-alist", Fcopy_alist, Scopy_alist, 1, 1, 0,
1159 doc: /* Return a copy of ALIST.
1160 This is an alist which represents the same mapping from objects to objects,
1161 but does not share the alist structure with ALIST.
1162 The objects mapped (cars and cdrs of elements of the alist)
1163 are shared, however.
1164 Elements of ALIST that are not conses are also shared. */)
1165 (alist)
1166 Lisp_Object alist;
1168 register Lisp_Object tem;
1170 CHECK_LIST (alist);
1171 if (NILP (alist))
1172 return alist;
1173 alist = concat (1, &alist, Lisp_Cons, 0);
1174 for (tem = alist; CONSP (tem); tem = XCDR (tem))
1176 register Lisp_Object car;
1177 car = XCAR (tem);
1179 if (CONSP (car))
1180 XSETCAR (tem, Fcons (XCAR (car), XCDR (car)));
1182 return alist;
1185 DEFUN ("substring", Fsubstring, Ssubstring, 2, 3, 0,
1186 doc: /* Return a new string whose contents are a substring of STRING.
1187 The returned string consists of the characters between index FROM
1188 \(inclusive) and index TO (exclusive) of STRING. FROM and TO are
1189 zero-indexed: 0 means the first character of STRING. Negative values
1190 are counted from the end of STRING. If TO is nil, the substring runs
1191 to the end of STRING.
1193 The STRING argument may also be a vector. In that case, the return
1194 value is a new vector that contains the elements between index FROM
1195 \(inclusive) and index TO (exclusive) of that vector argument. */)
1196 (string, from, to)
1197 Lisp_Object string;
1198 register Lisp_Object from, to;
1200 Lisp_Object res;
1201 int size;
1202 int size_byte = 0;
1203 int from_char, to_char;
1204 int from_byte = 0, to_byte = 0;
1206 CHECK_VECTOR_OR_STRING (string);
1207 CHECK_NUMBER (from);
1209 if (STRINGP (string))
1211 size = SCHARS (string);
1212 size_byte = SBYTES (string);
1214 else
1215 size = ASIZE (string);
1217 if (NILP (to))
1219 to_char = size;
1220 to_byte = size_byte;
1222 else
1224 CHECK_NUMBER (to);
1226 to_char = XINT (to);
1227 if (to_char < 0)
1228 to_char += size;
1230 if (STRINGP (string))
1231 to_byte = string_char_to_byte (string, to_char);
1234 from_char = XINT (from);
1235 if (from_char < 0)
1236 from_char += size;
1237 if (STRINGP (string))
1238 from_byte = string_char_to_byte (string, from_char);
1240 if (!(0 <= from_char && from_char <= to_char && to_char <= size))
1241 args_out_of_range_3 (string, make_number (from_char),
1242 make_number (to_char));
1244 if (STRINGP (string))
1246 res = make_specified_string (SDATA (string) + from_byte,
1247 to_char - from_char, to_byte - from_byte,
1248 STRING_MULTIBYTE (string));
1249 copy_text_properties (make_number (from_char), make_number (to_char),
1250 string, make_number (0), res, Qnil);
1252 else
1253 res = Fvector (to_char - from_char, &AREF (string, from_char));
1255 return res;
1259 DEFUN ("substring-no-properties", Fsubstring_no_properties, Ssubstring_no_properties, 1, 3, 0,
1260 doc: /* Return a substring of STRING, without text properties.
1261 It starts at index FROM and ending before TO.
1262 TO may be nil or omitted; then the substring runs to the end of STRING.
1263 If FROM is nil or omitted, the substring starts at the beginning of STRING.
1264 If FROM or TO is negative, it counts from the end.
1266 With one argument, just copy STRING without its properties. */)
1267 (string, from, to)
1268 Lisp_Object string;
1269 register Lisp_Object from, to;
1271 int size, size_byte;
1272 int from_char, to_char;
1273 int from_byte, to_byte;
1275 CHECK_STRING (string);
1277 size = SCHARS (string);
1278 size_byte = SBYTES (string);
1280 if (NILP (from))
1281 from_char = from_byte = 0;
1282 else
1284 CHECK_NUMBER (from);
1285 from_char = XINT (from);
1286 if (from_char < 0)
1287 from_char += size;
1289 from_byte = string_char_to_byte (string, from_char);
1292 if (NILP (to))
1294 to_char = size;
1295 to_byte = size_byte;
1297 else
1299 CHECK_NUMBER (to);
1301 to_char = XINT (to);
1302 if (to_char < 0)
1303 to_char += size;
1305 to_byte = string_char_to_byte (string, to_char);
1308 if (!(0 <= from_char && from_char <= to_char && to_char <= size))
1309 args_out_of_range_3 (string, make_number (from_char),
1310 make_number (to_char));
1312 return make_specified_string (SDATA (string) + from_byte,
1313 to_char - from_char, to_byte - from_byte,
1314 STRING_MULTIBYTE (string));
1317 /* Extract a substring of STRING, giving start and end positions
1318 both in characters and in bytes. */
1320 Lisp_Object
1321 substring_both (string, from, from_byte, to, to_byte)
1322 Lisp_Object string;
1323 int from, from_byte, to, to_byte;
1325 Lisp_Object res;
1326 int size;
1327 int size_byte;
1329 CHECK_VECTOR_OR_STRING (string);
1331 if (STRINGP (string))
1333 size = SCHARS (string);
1334 size_byte = SBYTES (string);
1336 else
1337 size = ASIZE (string);
1339 if (!(0 <= from && from <= to && to <= size))
1340 args_out_of_range_3 (string, make_number (from), make_number (to));
1342 if (STRINGP (string))
1344 res = make_specified_string (SDATA (string) + from_byte,
1345 to - from, to_byte - from_byte,
1346 STRING_MULTIBYTE (string));
1347 copy_text_properties (make_number (from), make_number (to),
1348 string, make_number (0), res, Qnil);
1350 else
1351 res = Fvector (to - from, &AREF (string, from));
1353 return res;
1356 DEFUN ("nthcdr", Fnthcdr, Snthcdr, 2, 2, 0,
1357 doc: /* Take cdr N times on LIST, returns the result. */)
1358 (n, list)
1359 Lisp_Object n;
1360 register Lisp_Object list;
1362 register int i, num;
1363 CHECK_NUMBER (n);
1364 num = XINT (n);
1365 for (i = 0; i < num && !NILP (list); i++)
1367 QUIT;
1368 CHECK_LIST_CONS (list, list);
1369 list = XCDR (list);
1371 return list;
1374 DEFUN ("nth", Fnth, Snth, 2, 2, 0,
1375 doc: /* Return the Nth element of LIST.
1376 N counts from zero. If LIST is not that long, nil is returned. */)
1377 (n, list)
1378 Lisp_Object n, list;
1380 return Fcar (Fnthcdr (n, list));
1383 DEFUN ("elt", Felt, Selt, 2, 2, 0,
1384 doc: /* Return element of SEQUENCE at index N. */)
1385 (sequence, n)
1386 register Lisp_Object sequence, n;
1388 CHECK_NUMBER (n);
1389 if (CONSP (sequence) || NILP (sequence))
1390 return Fcar (Fnthcdr (n, sequence));
1392 /* Faref signals a "not array" error, so check here. */
1393 CHECK_ARRAY (sequence, Qsequencep);
1394 return Faref (sequence, n);
1397 DEFUN ("member", Fmember, Smember, 2, 2, 0,
1398 doc: /* Return non-nil if ELT is an element of LIST. Comparison done with `equal'.
1399 The value is actually the tail of LIST whose car is ELT. */)
1400 (elt, list)
1401 register Lisp_Object elt;
1402 Lisp_Object list;
1404 register Lisp_Object tail;
1405 for (tail = list; CONSP (tail); tail = XCDR (tail))
1407 register Lisp_Object tem;
1408 CHECK_LIST_CONS (tail, list);
1409 tem = XCAR (tail);
1410 if (! NILP (Fequal (elt, tem)))
1411 return tail;
1412 QUIT;
1414 return Qnil;
1417 DEFUN ("memq", Fmemq, Smemq, 2, 2, 0,
1418 doc: /* Return non-nil if ELT is an element of LIST. Comparison done with `eq'.
1419 The value is actually the tail of LIST whose car is ELT. */)
1420 (elt, list)
1421 register Lisp_Object elt, list;
1423 while (1)
1425 if (!CONSP (list) || EQ (XCAR (list), elt))
1426 break;
1428 list = XCDR (list);
1429 if (!CONSP (list) || EQ (XCAR (list), elt))
1430 break;
1432 list = XCDR (list);
1433 if (!CONSP (list) || EQ (XCAR (list), elt))
1434 break;
1436 list = XCDR (list);
1437 QUIT;
1440 CHECK_LIST (list);
1441 return list;
1444 DEFUN ("memql", Fmemql, Smemql, 2, 2, 0,
1445 doc: /* Return non-nil if ELT is an element of LIST. Comparison done with `eql'.
1446 The value is actually the tail of LIST whose car is ELT. */)
1447 (elt, list)
1448 register Lisp_Object elt;
1449 Lisp_Object list;
1451 register Lisp_Object tail;
1453 if (!FLOATP (elt))
1454 return Fmemq (elt, list);
1456 for (tail = list; CONSP (tail); tail = XCDR (tail))
1458 register Lisp_Object tem;
1459 CHECK_LIST_CONS (tail, list);
1460 tem = XCAR (tail);
1461 if (FLOATP (tem) && internal_equal (elt, tem, 0, 0))
1462 return tail;
1463 QUIT;
1465 return Qnil;
1468 DEFUN ("assq", Fassq, Sassq, 2, 2, 0,
1469 doc: /* Return non-nil if KEY is `eq' to the car of an element of LIST.
1470 The value is actually the first element of LIST whose car is KEY.
1471 Elements of LIST that are not conses are ignored. */)
1472 (key, list)
1473 Lisp_Object key, list;
1475 while (1)
1477 if (!CONSP (list)
1478 || (CONSP (XCAR (list))
1479 && EQ (XCAR (XCAR (list)), key)))
1480 break;
1482 list = XCDR (list);
1483 if (!CONSP (list)
1484 || (CONSP (XCAR (list))
1485 && EQ (XCAR (XCAR (list)), key)))
1486 break;
1488 list = XCDR (list);
1489 if (!CONSP (list)
1490 || (CONSP (XCAR (list))
1491 && EQ (XCAR (XCAR (list)), key)))
1492 break;
1494 list = XCDR (list);
1495 QUIT;
1498 return CAR (list);
1501 /* Like Fassq but never report an error and do not allow quits.
1502 Use only on lists known never to be circular. */
1504 Lisp_Object
1505 assq_no_quit (key, list)
1506 Lisp_Object key, list;
1508 while (CONSP (list)
1509 && (!CONSP (XCAR (list))
1510 || !EQ (XCAR (XCAR (list)), key)))
1511 list = XCDR (list);
1513 return CAR_SAFE (list);
1516 DEFUN ("assoc", Fassoc, Sassoc, 2, 2, 0,
1517 doc: /* Return non-nil if KEY is `equal' to the car of an element of LIST.
1518 The value is actually the first element of LIST whose car equals KEY. */)
1519 (key, list)
1520 Lisp_Object key, list;
1522 Lisp_Object car;
1524 while (1)
1526 if (!CONSP (list)
1527 || (CONSP (XCAR (list))
1528 && (car = XCAR (XCAR (list)),
1529 EQ (car, key) || !NILP (Fequal (car, key)))))
1530 break;
1532 list = XCDR (list);
1533 if (!CONSP (list)
1534 || (CONSP (XCAR (list))
1535 && (car = XCAR (XCAR (list)),
1536 EQ (car, key) || !NILP (Fequal (car, key)))))
1537 break;
1539 list = XCDR (list);
1540 if (!CONSP (list)
1541 || (CONSP (XCAR (list))
1542 && (car = XCAR (XCAR (list)),
1543 EQ (car, key) || !NILP (Fequal (car, key)))))
1544 break;
1546 list = XCDR (list);
1547 QUIT;
1550 return CAR (list);
1553 /* Like Fassoc but never report an error and do not allow quits.
1554 Use only on lists known never to be circular. */
1556 Lisp_Object
1557 assoc_no_quit (key, list)
1558 Lisp_Object key, list;
1560 while (CONSP (list)
1561 && (!CONSP (XCAR (list))
1562 || (!EQ (XCAR (XCAR (list)), key)
1563 && NILP (Fequal (XCAR (XCAR (list)), key)))))
1564 list = XCDR (list);
1566 return CONSP (list) ? XCAR (list) : Qnil;
1569 DEFUN ("rassq", Frassq, Srassq, 2, 2, 0,
1570 doc: /* Return non-nil if KEY is `eq' to the cdr of an element of LIST.
1571 The value is actually the first element of LIST whose cdr is KEY. */)
1572 (key, list)
1573 register Lisp_Object key;
1574 Lisp_Object list;
1576 while (1)
1578 if (!CONSP (list)
1579 || (CONSP (XCAR (list))
1580 && EQ (XCDR (XCAR (list)), key)))
1581 break;
1583 list = XCDR (list);
1584 if (!CONSP (list)
1585 || (CONSP (XCAR (list))
1586 && EQ (XCDR (XCAR (list)), key)))
1587 break;
1589 list = XCDR (list);
1590 if (!CONSP (list)
1591 || (CONSP (XCAR (list))
1592 && EQ (XCDR (XCAR (list)), key)))
1593 break;
1595 list = XCDR (list);
1596 QUIT;
1599 return CAR (list);
1602 DEFUN ("rassoc", Frassoc, Srassoc, 2, 2, 0,
1603 doc: /* Return non-nil if KEY is `equal' to the cdr of an element of LIST.
1604 The value is actually the first element of LIST whose cdr equals KEY. */)
1605 (key, list)
1606 Lisp_Object key, list;
1608 Lisp_Object cdr;
1610 while (1)
1612 if (!CONSP (list)
1613 || (CONSP (XCAR (list))
1614 && (cdr = XCDR (XCAR (list)),
1615 EQ (cdr, key) || !NILP (Fequal (cdr, key)))))
1616 break;
1618 list = XCDR (list);
1619 if (!CONSP (list)
1620 || (CONSP (XCAR (list))
1621 && (cdr = XCDR (XCAR (list)),
1622 EQ (cdr, key) || !NILP (Fequal (cdr, key)))))
1623 break;
1625 list = XCDR (list);
1626 if (!CONSP (list)
1627 || (CONSP (XCAR (list))
1628 && (cdr = XCDR (XCAR (list)),
1629 EQ (cdr, key) || !NILP (Fequal (cdr, key)))))
1630 break;
1632 list = XCDR (list);
1633 QUIT;
1636 return CAR (list);
1639 DEFUN ("delq", Fdelq, Sdelq, 2, 2, 0,
1640 doc: /* Delete by side effect any occurrences of ELT as a member of LIST.
1641 The modified LIST is returned. Comparison is done with `eq'.
1642 If the first member of LIST is ELT, there is no way to remove it by side effect;
1643 therefore, write `(setq foo (delq element foo))'
1644 to be sure of changing the value of `foo'. */)
1645 (elt, list)
1646 register Lisp_Object elt;
1647 Lisp_Object list;
1649 register Lisp_Object tail, prev;
1650 register Lisp_Object tem;
1652 tail = list;
1653 prev = Qnil;
1654 while (!NILP (tail))
1656 CHECK_LIST_CONS (tail, list);
1657 tem = XCAR (tail);
1658 if (EQ (elt, tem))
1660 if (NILP (prev))
1661 list = XCDR (tail);
1662 else
1663 Fsetcdr (prev, XCDR (tail));
1665 else
1666 prev = tail;
1667 tail = XCDR (tail);
1668 QUIT;
1670 return list;
1673 DEFUN ("delete", Fdelete, Sdelete, 2, 2, 0,
1674 doc: /* Delete by side effect any occurrences of ELT as a member of SEQ.
1675 SEQ must be a list, a vector, or a string.
1676 The modified SEQ is returned. Comparison is done with `equal'.
1677 If SEQ is not a list, or the first member of SEQ is ELT, deleting it
1678 is not a side effect; it is simply using a different sequence.
1679 Therefore, write `(setq foo (delete element foo))'
1680 to be sure of changing the value of `foo'. */)
1681 (elt, seq)
1682 Lisp_Object elt, seq;
1684 if (VECTORP (seq))
1686 EMACS_INT i, n;
1688 for (i = n = 0; i < ASIZE (seq); ++i)
1689 if (NILP (Fequal (AREF (seq, i), elt)))
1690 ++n;
1692 if (n != ASIZE (seq))
1694 struct Lisp_Vector *p = allocate_vector (n);
1696 for (i = n = 0; i < ASIZE (seq); ++i)
1697 if (NILP (Fequal (AREF (seq, i), elt)))
1698 p->contents[n++] = AREF (seq, i);
1700 XSETVECTOR (seq, p);
1703 else if (STRINGP (seq))
1705 EMACS_INT i, ibyte, nchars, nbytes, cbytes;
1706 int c;
1708 for (i = nchars = nbytes = ibyte = 0;
1709 i < SCHARS (seq);
1710 ++i, ibyte += cbytes)
1712 if (STRING_MULTIBYTE (seq))
1714 c = STRING_CHAR (SDATA (seq) + ibyte);
1715 cbytes = CHAR_BYTES (c);
1717 else
1719 c = SREF (seq, i);
1720 cbytes = 1;
1723 if (!INTEGERP (elt) || c != XINT (elt))
1725 ++nchars;
1726 nbytes += cbytes;
1730 if (nchars != SCHARS (seq))
1732 Lisp_Object tem;
1734 tem = make_uninit_multibyte_string (nchars, nbytes);
1735 if (!STRING_MULTIBYTE (seq))
1736 STRING_SET_UNIBYTE (tem);
1738 for (i = nchars = nbytes = ibyte = 0;
1739 i < SCHARS (seq);
1740 ++i, ibyte += cbytes)
1742 if (STRING_MULTIBYTE (seq))
1744 c = STRING_CHAR (SDATA (seq) + ibyte);
1745 cbytes = CHAR_BYTES (c);
1747 else
1749 c = SREF (seq, i);
1750 cbytes = 1;
1753 if (!INTEGERP (elt) || c != XINT (elt))
1755 unsigned char *from = SDATA (seq) + ibyte;
1756 unsigned char *to = SDATA (tem) + nbytes;
1757 EMACS_INT n;
1759 ++nchars;
1760 nbytes += cbytes;
1762 for (n = cbytes; n--; )
1763 *to++ = *from++;
1767 seq = tem;
1770 else
1772 Lisp_Object tail, prev;
1774 for (tail = seq, prev = Qnil; CONSP (tail); tail = XCDR (tail))
1776 CHECK_LIST_CONS (tail, seq);
1778 if (!NILP (Fequal (elt, XCAR (tail))))
1780 if (NILP (prev))
1781 seq = XCDR (tail);
1782 else
1783 Fsetcdr (prev, XCDR (tail));
1785 else
1786 prev = tail;
1787 QUIT;
1791 return seq;
1794 DEFUN ("nreverse", Fnreverse, Snreverse, 1, 1, 0,
1795 doc: /* Reverse LIST by modifying cdr pointers.
1796 Return the reversed list. */)
1797 (list)
1798 Lisp_Object list;
1800 register Lisp_Object prev, tail, next;
1802 if (NILP (list)) return list;
1803 prev = Qnil;
1804 tail = list;
1805 while (!NILP (tail))
1807 QUIT;
1808 CHECK_LIST_CONS (tail, list);
1809 next = XCDR (tail);
1810 Fsetcdr (tail, prev);
1811 prev = tail;
1812 tail = next;
1814 return prev;
1817 DEFUN ("reverse", Freverse, Sreverse, 1, 1, 0,
1818 doc: /* Reverse LIST, copying. Return the reversed list.
1819 See also the function `nreverse', which is used more often. */)
1820 (list)
1821 Lisp_Object list;
1823 Lisp_Object new;
1825 for (new = Qnil; CONSP (list); list = XCDR (list))
1827 QUIT;
1828 new = Fcons (XCAR (list), new);
1830 CHECK_LIST_END (list, list);
1831 return new;
1834 Lisp_Object merge ();
1836 DEFUN ("sort", Fsort, Ssort, 2, 2, 0,
1837 doc: /* Sort LIST, stably, comparing elements using PREDICATE.
1838 Returns the sorted list. LIST is modified by side effects.
1839 PREDICATE is called with two elements of LIST, and should return non-nil
1840 if the first element should sort before the second. */)
1841 (list, predicate)
1842 Lisp_Object list, predicate;
1844 Lisp_Object front, back;
1845 register Lisp_Object len, tem;
1846 struct gcpro gcpro1, gcpro2;
1847 register int length;
1849 front = list;
1850 len = Flength (list);
1851 length = XINT (len);
1852 if (length < 2)
1853 return list;
1855 XSETINT (len, (length / 2) - 1);
1856 tem = Fnthcdr (len, list);
1857 back = Fcdr (tem);
1858 Fsetcdr (tem, Qnil);
1860 GCPRO2 (front, back);
1861 front = Fsort (front, predicate);
1862 back = Fsort (back, predicate);
1863 UNGCPRO;
1864 return merge (front, back, predicate);
1867 Lisp_Object
1868 merge (org_l1, org_l2, pred)
1869 Lisp_Object org_l1, org_l2;
1870 Lisp_Object pred;
1872 Lisp_Object value;
1873 register Lisp_Object tail;
1874 Lisp_Object tem;
1875 register Lisp_Object l1, l2;
1876 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1878 l1 = org_l1;
1879 l2 = org_l2;
1880 tail = Qnil;
1881 value = Qnil;
1883 /* It is sufficient to protect org_l1 and org_l2.
1884 When l1 and l2 are updated, we copy the new values
1885 back into the org_ vars. */
1886 GCPRO4 (org_l1, org_l2, pred, value);
1888 while (1)
1890 if (NILP (l1))
1892 UNGCPRO;
1893 if (NILP (tail))
1894 return l2;
1895 Fsetcdr (tail, l2);
1896 return value;
1898 if (NILP (l2))
1900 UNGCPRO;
1901 if (NILP (tail))
1902 return l1;
1903 Fsetcdr (tail, l1);
1904 return value;
1906 tem = call2 (pred, Fcar (l2), Fcar (l1));
1907 if (NILP (tem))
1909 tem = l1;
1910 l1 = Fcdr (l1);
1911 org_l1 = l1;
1913 else
1915 tem = l2;
1916 l2 = Fcdr (l2);
1917 org_l2 = l2;
1919 if (NILP (tail))
1920 value = tem;
1921 else
1922 Fsetcdr (tail, tem);
1923 tail = tem;
1928 /* This does not check for quits. That is safe since it must terminate. */
1930 DEFUN ("plist-get", Fplist_get, Splist_get, 2, 2, 0,
1931 doc: /* Extract a value from a property list.
1932 PLIST is a property list, which is a list of the form
1933 \(PROP1 VALUE1 PROP2 VALUE2...). This function returns the value
1934 corresponding to the given PROP, or nil if PROP is not one of the
1935 properties on the list. This function never signals an error. */)
1936 (plist, prop)
1937 Lisp_Object plist;
1938 Lisp_Object prop;
1940 Lisp_Object tail, halftail;
1942 /* halftail is used to detect circular lists. */
1943 tail = halftail = plist;
1944 while (CONSP (tail) && CONSP (XCDR (tail)))
1946 if (EQ (prop, XCAR (tail)))
1947 return XCAR (XCDR (tail));
1949 tail = XCDR (XCDR (tail));
1950 halftail = XCDR (halftail);
1951 if (EQ (tail, halftail))
1952 break;
1954 #if 0 /* Unsafe version. */
1955 /* This function can be called asynchronously
1956 (setup_coding_system). Don't QUIT in that case. */
1957 if (!interrupt_input_blocked)
1958 QUIT;
1959 #endif
1962 return Qnil;
1965 DEFUN ("get", Fget, Sget, 2, 2, 0,
1966 doc: /* Return the value of SYMBOL's PROPNAME property.
1967 This is the last value stored with `(put SYMBOL PROPNAME VALUE)'. */)
1968 (symbol, propname)
1969 Lisp_Object symbol, propname;
1971 CHECK_SYMBOL (symbol);
1972 return Fplist_get (XSYMBOL (symbol)->plist, propname);
1975 DEFUN ("plist-put", Fplist_put, Splist_put, 3, 3, 0,
1976 doc: /* Change value in PLIST of PROP to VAL.
1977 PLIST is a property list, which is a list of the form
1978 \(PROP1 VALUE1 PROP2 VALUE2 ...). PROP is a symbol and VAL is any object.
1979 If PROP is already a property on the list, its value is set to VAL,
1980 otherwise the new PROP VAL pair is added. The new plist is returned;
1981 use `(setq x (plist-put x prop val))' to be sure to use the new value.
1982 The PLIST is modified by side effects. */)
1983 (plist, prop, val)
1984 Lisp_Object plist;
1985 register Lisp_Object prop;
1986 Lisp_Object val;
1988 register Lisp_Object tail, prev;
1989 Lisp_Object newcell;
1990 prev = Qnil;
1991 for (tail = plist; CONSP (tail) && CONSP (XCDR (tail));
1992 tail = XCDR (XCDR (tail)))
1994 if (EQ (prop, XCAR (tail)))
1996 Fsetcar (XCDR (tail), val);
1997 return plist;
2000 prev = tail;
2001 QUIT;
2003 newcell = Fcons (prop, Fcons (val, NILP (prev) ? plist : XCDR (XCDR (prev))));
2004 if (NILP (prev))
2005 return newcell;
2006 else
2007 Fsetcdr (XCDR (prev), newcell);
2008 return plist;
2011 DEFUN ("put", Fput, Sput, 3, 3, 0,
2012 doc: /* Store SYMBOL's PROPNAME property with value VALUE.
2013 It can be retrieved with `(get SYMBOL PROPNAME)'. */)
2014 (symbol, propname, value)
2015 Lisp_Object symbol, propname, value;
2017 CHECK_SYMBOL (symbol);
2018 XSYMBOL (symbol)->plist
2019 = Fplist_put (XSYMBOL (symbol)->plist, propname, value);
2020 return value;
2023 DEFUN ("lax-plist-get", Flax_plist_get, Slax_plist_get, 2, 2, 0,
2024 doc: /* Extract a value from a property list, comparing with `equal'.
2025 PLIST is a property list, which is a list of the form
2026 \(PROP1 VALUE1 PROP2 VALUE2...). This function returns the value
2027 corresponding to the given PROP, or nil if PROP is not
2028 one of the properties on the list. */)
2029 (plist, prop)
2030 Lisp_Object plist;
2031 Lisp_Object prop;
2033 Lisp_Object tail;
2035 for (tail = plist;
2036 CONSP (tail) && CONSP (XCDR (tail));
2037 tail = XCDR (XCDR (tail)))
2039 if (! NILP (Fequal (prop, XCAR (tail))))
2040 return XCAR (XCDR (tail));
2042 QUIT;
2045 CHECK_LIST_END (tail, prop);
2047 return Qnil;
2050 DEFUN ("lax-plist-put", Flax_plist_put, Slax_plist_put, 3, 3, 0,
2051 doc: /* Change value in PLIST of PROP to VAL, comparing with `equal'.
2052 PLIST is a property list, which is a list of the form
2053 \(PROP1 VALUE1 PROP2 VALUE2 ...). PROP and VAL are any objects.
2054 If PROP is already a property on the list, its value is set to VAL,
2055 otherwise the new PROP VAL pair is added. The new plist is returned;
2056 use `(setq x (lax-plist-put x prop val))' to be sure to use the new value.
2057 The PLIST is modified by side effects. */)
2058 (plist, prop, val)
2059 Lisp_Object plist;
2060 register Lisp_Object prop;
2061 Lisp_Object val;
2063 register Lisp_Object tail, prev;
2064 Lisp_Object newcell;
2065 prev = Qnil;
2066 for (tail = plist; CONSP (tail) && CONSP (XCDR (tail));
2067 tail = XCDR (XCDR (tail)))
2069 if (! NILP (Fequal (prop, XCAR (tail))))
2071 Fsetcar (XCDR (tail), val);
2072 return plist;
2075 prev = tail;
2076 QUIT;
2078 newcell = Fcons (prop, Fcons (val, Qnil));
2079 if (NILP (prev))
2080 return newcell;
2081 else
2082 Fsetcdr (XCDR (prev), newcell);
2083 return plist;
2086 DEFUN ("eql", Feql, Seql, 2, 2, 0,
2087 doc: /* Return t if the two args are the same Lisp object.
2088 Floating-point numbers of equal value are `eql', but they may not be `eq'. */)
2089 (obj1, obj2)
2090 Lisp_Object obj1, obj2;
2092 if (FLOATP (obj1))
2093 return internal_equal (obj1, obj2, 0, 0) ? Qt : Qnil;
2094 else
2095 return EQ (obj1, obj2) ? Qt : Qnil;
2098 DEFUN ("equal", Fequal, Sequal, 2, 2, 0,
2099 doc: /* Return t if two Lisp objects have similar structure and contents.
2100 They must have the same data type.
2101 Conses are compared by comparing the cars and the cdrs.
2102 Vectors and strings are compared element by element.
2103 Numbers are compared by value, but integers cannot equal floats.
2104 (Use `=' if you want integers and floats to be able to be equal.)
2105 Symbols must match exactly. */)
2106 (o1, o2)
2107 register Lisp_Object o1, o2;
2109 return internal_equal (o1, o2, 0, 0) ? Qt : Qnil;
2112 DEFUN ("equal-including-properties", Fequal_including_properties, Sequal_including_properties, 2, 2, 0,
2113 doc: /* Return t if two Lisp objects have similar structure and contents.
2114 This is like `equal' except that it compares the text properties
2115 of strings. (`equal' ignores text properties.) */)
2116 (o1, o2)
2117 register Lisp_Object o1, o2;
2119 return internal_equal (o1, o2, 0, 1) ? Qt : Qnil;
2122 /* DEPTH is current depth of recursion. Signal an error if it
2123 gets too deep.
2124 PROPS, if non-nil, means compare string text properties too. */
2126 static int
2127 internal_equal (o1, o2, depth, props)
2128 register Lisp_Object o1, o2;
2129 int depth, props;
2131 if (depth > 200)
2132 error ("Stack overflow in equal");
2134 tail_recurse:
2135 QUIT;
2136 if (EQ (o1, o2))
2137 return 1;
2138 if (XTYPE (o1) != XTYPE (o2))
2139 return 0;
2141 switch (XTYPE (o1))
2143 case Lisp_Float:
2145 double d1, d2;
2147 d1 = extract_float (o1);
2148 d2 = extract_float (o2);
2149 /* If d is a NaN, then d != d. Two NaNs should be `equal' even
2150 though they are not =. */
2151 return d1 == d2 || (d1 != d1 && d2 != d2);
2154 case Lisp_Cons:
2155 if (!internal_equal (XCAR (o1), XCAR (o2), depth + 1, props))
2156 return 0;
2157 o1 = XCDR (o1);
2158 o2 = XCDR (o2);
2159 goto tail_recurse;
2161 case Lisp_Misc:
2162 if (XMISCTYPE (o1) != XMISCTYPE (o2))
2163 return 0;
2164 if (OVERLAYP (o1))
2166 if (!internal_equal (OVERLAY_START (o1), OVERLAY_START (o2),
2167 depth + 1, props)
2168 || !internal_equal (OVERLAY_END (o1), OVERLAY_END (o2),
2169 depth + 1, props))
2170 return 0;
2171 o1 = XOVERLAY (o1)->plist;
2172 o2 = XOVERLAY (o2)->plist;
2173 goto tail_recurse;
2175 if (MARKERP (o1))
2177 return (XMARKER (o1)->buffer == XMARKER (o2)->buffer
2178 && (XMARKER (o1)->buffer == 0
2179 || XMARKER (o1)->bytepos == XMARKER (o2)->bytepos));
2181 break;
2183 case Lisp_Vectorlike:
2185 register int i;
2186 EMACS_INT size = ASIZE (o1);
2187 /* Pseudovectors have the type encoded in the size field, so this test
2188 actually checks that the objects have the same type as well as the
2189 same size. */
2190 if (ASIZE (o2) != size)
2191 return 0;
2192 /* Boolvectors are compared much like strings. */
2193 if (BOOL_VECTOR_P (o1))
2195 int size_in_chars
2196 = ((XBOOL_VECTOR (o1)->size + BOOL_VECTOR_BITS_PER_CHAR - 1)
2197 / BOOL_VECTOR_BITS_PER_CHAR);
2199 if (XBOOL_VECTOR (o1)->size != XBOOL_VECTOR (o2)->size)
2200 return 0;
2201 if (bcmp (XBOOL_VECTOR (o1)->data, XBOOL_VECTOR (o2)->data,
2202 size_in_chars))
2203 return 0;
2204 return 1;
2206 if (WINDOW_CONFIGURATIONP (o1))
2207 return compare_window_configurations (o1, o2, 0);
2209 /* Aside from them, only true vectors, char-tables, compiled
2210 functions, and fonts (font-spec, font-entity, font-ojbect)
2211 are sensible to compare, so eliminate the others now. */
2212 if (size & PSEUDOVECTOR_FLAG)
2214 if (!(size & (PVEC_COMPILED
2215 | PVEC_CHAR_TABLE | PVEC_SUB_CHAR_TABLE | PVEC_FONT)))
2216 return 0;
2217 size &= PSEUDOVECTOR_SIZE_MASK;
2219 for (i = 0; i < size; i++)
2221 Lisp_Object v1, v2;
2222 v1 = AREF (o1, i);
2223 v2 = AREF (o2, i);
2224 if (!internal_equal (v1, v2, depth + 1, props))
2225 return 0;
2227 return 1;
2229 break;
2231 case Lisp_String:
2232 if (SCHARS (o1) != SCHARS (o2))
2233 return 0;
2234 if (SBYTES (o1) != SBYTES (o2))
2235 return 0;
2236 if (bcmp (SDATA (o1), SDATA (o2),
2237 SBYTES (o1)))
2238 return 0;
2239 if (props && !compare_string_intervals (o1, o2))
2240 return 0;
2241 return 1;
2243 default:
2244 break;
2247 return 0;
2250 extern Lisp_Object Fmake_char_internal ();
2252 DEFUN ("fillarray", Ffillarray, Sfillarray, 2, 2, 0,
2253 doc: /* Store each element of ARRAY with ITEM.
2254 ARRAY is a vector, string, char-table, or bool-vector. */)
2255 (array, item)
2256 Lisp_Object array, item;
2258 register int size, index, charval;
2259 if (VECTORP (array))
2261 register Lisp_Object *p = XVECTOR (array)->contents;
2262 size = ASIZE (array);
2263 for (index = 0; index < size; index++)
2264 p[index] = item;
2266 else if (CHAR_TABLE_P (array))
2268 int i;
2270 for (i = 0; i < (1 << CHARTAB_SIZE_BITS_0); i++)
2271 XCHAR_TABLE (array)->contents[i] = item;
2272 XCHAR_TABLE (array)->defalt = item;
2274 else if (STRINGP (array))
2276 register unsigned char *p = SDATA (array);
2277 CHECK_NUMBER (item);
2278 charval = XINT (item);
2279 size = SCHARS (array);
2280 if (STRING_MULTIBYTE (array))
2282 unsigned char str[MAX_MULTIBYTE_LENGTH];
2283 int len = CHAR_STRING (charval, str);
2284 int size_byte = SBYTES (array);
2285 unsigned char *p1 = p, *endp = p + size_byte;
2286 int i;
2288 if (size != size_byte)
2289 while (p1 < endp)
2291 int this_len = MULTIBYTE_FORM_LENGTH (p1, endp - p1);
2292 if (len != this_len)
2293 error ("Attempt to change byte length of a string");
2294 p1 += this_len;
2296 for (i = 0; i < size_byte; i++)
2297 *p++ = str[i % len];
2299 else
2300 for (index = 0; index < size; index++)
2301 p[index] = charval;
2303 else if (BOOL_VECTOR_P (array))
2305 register unsigned char *p = XBOOL_VECTOR (array)->data;
2306 int size_in_chars
2307 = ((XBOOL_VECTOR (array)->size + BOOL_VECTOR_BITS_PER_CHAR - 1)
2308 / BOOL_VECTOR_BITS_PER_CHAR);
2310 charval = (! NILP (item) ? -1 : 0);
2311 for (index = 0; index < size_in_chars - 1; index++)
2312 p[index] = charval;
2313 if (index < size_in_chars)
2315 /* Mask out bits beyond the vector size. */
2316 if (XBOOL_VECTOR (array)->size % BOOL_VECTOR_BITS_PER_CHAR)
2317 charval &= (1 << (XBOOL_VECTOR (array)->size % BOOL_VECTOR_BITS_PER_CHAR)) - 1;
2318 p[index] = charval;
2321 else
2322 wrong_type_argument (Qarrayp, array);
2323 return array;
2326 DEFUN ("clear-string", Fclear_string, Sclear_string,
2327 1, 1, 0,
2328 doc: /* Clear the contents of STRING.
2329 This makes STRING unibyte and may change its length. */)
2330 (string)
2331 Lisp_Object string;
2333 int len;
2334 CHECK_STRING (string);
2335 len = SBYTES (string);
2336 bzero (SDATA (string), len);
2337 STRING_SET_CHARS (string, len);
2338 STRING_SET_UNIBYTE (string);
2339 return Qnil;
2342 /* ARGSUSED */
2343 Lisp_Object
2344 nconc2 (s1, s2)
2345 Lisp_Object s1, s2;
2347 #ifdef NO_ARG_ARRAY
2348 Lisp_Object args[2];
2349 args[0] = s1;
2350 args[1] = s2;
2351 return Fnconc (2, args);
2352 #else
2353 return Fnconc (2, &s1);
2354 #endif /* NO_ARG_ARRAY */
2357 DEFUN ("nconc", Fnconc, Snconc, 0, MANY, 0,
2358 doc: /* Concatenate any number of lists by altering them.
2359 Only the last argument is not altered, and need not be a list.
2360 usage: (nconc &rest LISTS) */)
2361 (nargs, args)
2362 int nargs;
2363 Lisp_Object *args;
2365 register int argnum;
2366 register Lisp_Object tail, tem, val;
2368 val = tail = Qnil;
2370 for (argnum = 0; argnum < nargs; argnum++)
2372 tem = args[argnum];
2373 if (NILP (tem)) continue;
2375 if (NILP (val))
2376 val = tem;
2378 if (argnum + 1 == nargs) break;
2380 CHECK_LIST_CONS (tem, tem);
2382 while (CONSP (tem))
2384 tail = tem;
2385 tem = XCDR (tail);
2386 QUIT;
2389 tem = args[argnum + 1];
2390 Fsetcdr (tail, tem);
2391 if (NILP (tem))
2392 args[argnum + 1] = tail;
2395 return val;
2398 /* This is the guts of all mapping functions.
2399 Apply FN to each element of SEQ, one by one,
2400 storing the results into elements of VALS, a C vector of Lisp_Objects.
2401 LENI is the length of VALS, which should also be the length of SEQ. */
2403 static void
2404 mapcar1 (leni, vals, fn, seq)
2405 int leni;
2406 Lisp_Object *vals;
2407 Lisp_Object fn, seq;
2409 register Lisp_Object tail;
2410 Lisp_Object dummy;
2411 register int i;
2412 struct gcpro gcpro1, gcpro2, gcpro3;
2414 if (vals)
2416 /* Don't let vals contain any garbage when GC happens. */
2417 for (i = 0; i < leni; i++)
2418 vals[i] = Qnil;
2420 GCPRO3 (dummy, fn, seq);
2421 gcpro1.var = vals;
2422 gcpro1.nvars = leni;
2424 else
2425 GCPRO2 (fn, seq);
2426 /* We need not explicitly protect `tail' because it is used only on lists, and
2427 1) lists are not relocated and 2) the list is marked via `seq' so will not
2428 be freed */
2430 if (VECTORP (seq))
2432 for (i = 0; i < leni; i++)
2434 dummy = call1 (fn, AREF (seq, i));
2435 if (vals)
2436 vals[i] = dummy;
2439 else if (BOOL_VECTOR_P (seq))
2441 for (i = 0; i < leni; i++)
2443 int byte;
2444 byte = XBOOL_VECTOR (seq)->data[i / BOOL_VECTOR_BITS_PER_CHAR];
2445 dummy = (byte & (1 << (i % BOOL_VECTOR_BITS_PER_CHAR))) ? Qt : Qnil;
2446 dummy = call1 (fn, dummy);
2447 if (vals)
2448 vals[i] = dummy;
2451 else if (STRINGP (seq))
2453 int i_byte;
2455 for (i = 0, i_byte = 0; i < leni;)
2457 int c;
2458 int i_before = i;
2460 FETCH_STRING_CHAR_ADVANCE (c, seq, i, i_byte);
2461 XSETFASTINT (dummy, c);
2462 dummy = call1 (fn, dummy);
2463 if (vals)
2464 vals[i_before] = dummy;
2467 else /* Must be a list, since Flength did not get an error */
2469 tail = seq;
2470 for (i = 0; i < leni && CONSP (tail); i++)
2472 dummy = call1 (fn, XCAR (tail));
2473 if (vals)
2474 vals[i] = dummy;
2475 tail = XCDR (tail);
2479 UNGCPRO;
2482 DEFUN ("mapconcat", Fmapconcat, Smapconcat, 3, 3, 0,
2483 doc: /* Apply FUNCTION to each element of SEQUENCE, and concat the results as strings.
2484 In between each pair of results, stick in SEPARATOR. Thus, " " as
2485 SEPARATOR results in spaces between the values returned by FUNCTION.
2486 SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
2487 (function, sequence, separator)
2488 Lisp_Object function, sequence, separator;
2490 Lisp_Object len;
2491 register int leni;
2492 int nargs;
2493 register Lisp_Object *args;
2494 register int i;
2495 struct gcpro gcpro1;
2496 Lisp_Object ret;
2497 USE_SAFE_ALLOCA;
2499 len = Flength (sequence);
2500 if (CHAR_TABLE_P (sequence))
2501 wrong_type_argument (Qlistp, sequence);
2502 leni = XINT (len);
2503 nargs = leni + leni - 1;
2504 if (nargs < 0) return empty_unibyte_string;
2506 SAFE_ALLOCA_LISP (args, nargs);
2508 GCPRO1 (separator);
2509 mapcar1 (leni, args, function, sequence);
2510 UNGCPRO;
2512 for (i = leni - 1; i > 0; i--)
2513 args[i + i] = args[i];
2515 for (i = 1; i < nargs; i += 2)
2516 args[i] = separator;
2518 ret = Fconcat (nargs, args);
2519 SAFE_FREE ();
2521 return ret;
2524 DEFUN ("mapcar", Fmapcar, Smapcar, 2, 2, 0,
2525 doc: /* Apply FUNCTION to each element of SEQUENCE, and make a list of the results.
2526 The result is a list just as long as SEQUENCE.
2527 SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
2528 (function, sequence)
2529 Lisp_Object function, sequence;
2531 register Lisp_Object len;
2532 register int leni;
2533 register Lisp_Object *args;
2534 Lisp_Object ret;
2535 USE_SAFE_ALLOCA;
2537 len = Flength (sequence);
2538 if (CHAR_TABLE_P (sequence))
2539 wrong_type_argument (Qlistp, sequence);
2540 leni = XFASTINT (len);
2542 SAFE_ALLOCA_LISP (args, leni);
2544 mapcar1 (leni, args, function, sequence);
2546 ret = Flist (leni, args);
2547 SAFE_FREE ();
2549 return ret;
2552 DEFUN ("mapc", Fmapc, Smapc, 2, 2, 0,
2553 doc: /* Apply FUNCTION to each element of SEQUENCE for side effects only.
2554 Unlike `mapcar', don't accumulate the results. Return SEQUENCE.
2555 SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
2556 (function, sequence)
2557 Lisp_Object function, sequence;
2559 register int leni;
2561 leni = XFASTINT (Flength (sequence));
2562 if (CHAR_TABLE_P (sequence))
2563 wrong_type_argument (Qlistp, sequence);
2564 mapcar1 (leni, 0, function, sequence);
2566 return sequence;
2569 /* Anything that calls this function must protect from GC! */
2571 DEFUN ("y-or-n-p", Fy_or_n_p, Sy_or_n_p, 1, 1, 0,
2572 doc: /* Ask user a "y or n" question. Return t if answer is "y".
2573 Takes one argument, which is the string to display to ask the question.
2574 It should end in a space; `y-or-n-p' adds `(y or n) ' to it.
2575 No confirmation of the answer is requested; a single character is enough.
2576 Also accepts Space to mean yes, or Delete to mean no. \(Actually, it uses
2577 the bindings in `query-replace-map'; see the documentation of that variable
2578 for more information. In this case, the useful bindings are `act', `skip',
2579 `recenter', and `quit'.\)
2581 Under a windowing system a dialog box will be used if `last-nonmenu-event'
2582 is nil and `use-dialog-box' is non-nil. */)
2583 (prompt)
2584 Lisp_Object prompt;
2586 register Lisp_Object obj, key, def, map;
2587 register int answer;
2588 Lisp_Object xprompt;
2589 Lisp_Object args[2];
2590 struct gcpro gcpro1, gcpro2;
2591 int count = SPECPDL_INDEX ();
2593 specbind (Qcursor_in_echo_area, Qt);
2595 map = Fsymbol_value (intern ("query-replace-map"));
2597 CHECK_STRING (prompt);
2598 xprompt = prompt;
2599 GCPRO2 (prompt, xprompt);
2601 #ifdef HAVE_WINDOW_SYSTEM
2602 if (display_hourglass_p)
2603 cancel_hourglass ();
2604 #endif
2606 while (1)
2609 #ifdef HAVE_MENUS
2610 if (FRAME_WINDOW_P (SELECTED_FRAME ())
2611 && (NILP (last_nonmenu_event) || CONSP (last_nonmenu_event))
2612 && use_dialog_box
2613 && have_menus_p ())
2615 Lisp_Object pane, menu;
2616 redisplay_preserve_echo_area (3);
2617 pane = Fcons (Fcons (build_string ("Yes"), Qt),
2618 Fcons (Fcons (build_string ("No"), Qnil),
2619 Qnil));
2620 menu = Fcons (prompt, pane);
2621 obj = Fx_popup_dialog (Qt, menu, Qnil);
2622 answer = !NILP (obj);
2623 break;
2625 #endif /* HAVE_MENUS */
2626 cursor_in_echo_area = 1;
2627 choose_minibuf_frame ();
2630 Lisp_Object pargs[3];
2632 /* Colorize prompt according to `minibuffer-prompt' face. */
2633 pargs[0] = build_string ("%s(y or n) ");
2634 pargs[1] = intern ("face");
2635 pargs[2] = intern ("minibuffer-prompt");
2636 args[0] = Fpropertize (3, pargs);
2637 args[1] = xprompt;
2638 Fmessage (2, args);
2641 if (minibuffer_auto_raise)
2643 Lisp_Object mini_frame;
2645 mini_frame = WINDOW_FRAME (XWINDOW (minibuf_window));
2647 Fraise_frame (mini_frame);
2650 temporarily_switch_to_single_kboard (SELECTED_FRAME ());
2651 obj = read_filtered_event (1, 0, 0, 0, Qnil);
2652 cursor_in_echo_area = 0;
2653 /* If we need to quit, quit with cursor_in_echo_area = 0. */
2654 QUIT;
2656 key = Fmake_vector (make_number (1), obj);
2657 def = Flookup_key (map, key, Qt);
2659 if (EQ (def, intern ("skip")))
2661 answer = 0;
2662 break;
2664 else if (EQ (def, intern ("act")))
2666 answer = 1;
2667 break;
2669 else if (EQ (def, intern ("recenter")))
2671 Frecenter (Qnil);
2672 xprompt = prompt;
2673 continue;
2675 else if (EQ (def, intern ("quit")))
2676 Vquit_flag = Qt;
2677 /* We want to exit this command for exit-prefix,
2678 and this is the only way to do it. */
2679 else if (EQ (def, intern ("exit-prefix")))
2680 Vquit_flag = Qt;
2682 QUIT;
2684 /* If we don't clear this, then the next call to read_char will
2685 return quit_char again, and we'll enter an infinite loop. */
2686 Vquit_flag = Qnil;
2688 Fding (Qnil);
2689 Fdiscard_input ();
2690 if (EQ (xprompt, prompt))
2692 args[0] = build_string ("Please answer y or n. ");
2693 args[1] = prompt;
2694 xprompt = Fconcat (2, args);
2697 UNGCPRO;
2699 if (! noninteractive)
2701 cursor_in_echo_area = -1;
2702 message_with_string (answer ? "%s(y or n) y" : "%s(y or n) n",
2703 xprompt, 0);
2706 unbind_to (count, Qnil);
2707 return answer ? Qt : Qnil;
2710 /* This is how C code calls `yes-or-no-p' and allows the user
2711 to redefined it.
2713 Anything that calls this function must protect from GC! */
2715 Lisp_Object
2716 do_yes_or_no_p (prompt)
2717 Lisp_Object prompt;
2719 return call1 (intern ("yes-or-no-p"), prompt);
2722 /* Anything that calls this function must protect from GC! */
2724 DEFUN ("yes-or-no-p", Fyes_or_no_p, Syes_or_no_p, 1, 1, 0,
2725 doc: /* Ask user a yes-or-no question. Return t if answer is yes.
2726 Takes one argument, which is the string to display to ask the question.
2727 It should end in a space; `yes-or-no-p' adds `(yes or no) ' to it.
2728 The user must confirm the answer with RET,
2729 and can edit it until it has been confirmed.
2731 Under a windowing system a dialog box will be used if `last-nonmenu-event'
2732 is nil, and `use-dialog-box' is non-nil. */)
2733 (prompt)
2734 Lisp_Object prompt;
2736 register Lisp_Object ans;
2737 Lisp_Object args[2];
2738 struct gcpro gcpro1;
2740 CHECK_STRING (prompt);
2742 #ifdef HAVE_MENUS
2743 if (FRAME_WINDOW_P (SELECTED_FRAME ())
2744 && (NILP (last_nonmenu_event) || CONSP (last_nonmenu_event))
2745 && use_dialog_box
2746 && have_menus_p ())
2748 Lisp_Object pane, menu, obj;
2749 redisplay_preserve_echo_area (4);
2750 pane = Fcons (Fcons (build_string ("Yes"), Qt),
2751 Fcons (Fcons (build_string ("No"), Qnil),
2752 Qnil));
2753 GCPRO1 (pane);
2754 menu = Fcons (prompt, pane);
2755 obj = Fx_popup_dialog (Qt, menu, Qnil);
2756 UNGCPRO;
2757 return obj;
2759 #endif /* HAVE_MENUS */
2761 args[0] = prompt;
2762 args[1] = build_string ("(yes or no) ");
2763 prompt = Fconcat (2, args);
2765 GCPRO1 (prompt);
2767 while (1)
2769 ans = Fdowncase (Fread_from_minibuffer (prompt, Qnil, Qnil, Qnil,
2770 Qyes_or_no_p_history, Qnil,
2771 Qnil));
2772 if (SCHARS (ans) == 3 && !strcmp (SDATA (ans), "yes"))
2774 UNGCPRO;
2775 return Qt;
2777 if (SCHARS (ans) == 2 && !strcmp (SDATA (ans), "no"))
2779 UNGCPRO;
2780 return Qnil;
2783 Fding (Qnil);
2784 Fdiscard_input ();
2785 message ("Please answer yes or no.");
2786 Fsleep_for (make_number (2), Qnil);
2790 DEFUN ("load-average", Fload_average, Sload_average, 0, 1, 0,
2791 doc: /* Return list of 1 minute, 5 minute and 15 minute load averages.
2793 Each of the three load averages is multiplied by 100, then converted
2794 to integer.
2796 When USE-FLOATS is non-nil, floats will be used instead of integers.
2797 These floats are not multiplied by 100.
2799 If the 5-minute or 15-minute load averages are not available, return a
2800 shortened list, containing only those averages which are available.
2802 An error is thrown if the load average can't be obtained. In some
2803 cases making it work would require Emacs being installed setuid or
2804 setgid so that it can read kernel information, and that usually isn't
2805 advisable. */)
2806 (use_floats)
2807 Lisp_Object use_floats;
2809 double load_ave[3];
2810 int loads = getloadavg (load_ave, 3);
2811 Lisp_Object ret = Qnil;
2813 if (loads < 0)
2814 error ("load-average not implemented for this operating system");
2816 while (loads-- > 0)
2818 Lisp_Object load = (NILP (use_floats) ?
2819 make_number ((int) (100.0 * load_ave[loads]))
2820 : make_float (load_ave[loads]));
2821 ret = Fcons (load, ret);
2824 return ret;
2827 Lisp_Object Vfeatures, Qsubfeatures;
2828 extern Lisp_Object Vafter_load_alist;
2830 DEFUN ("featurep", Ffeaturep, Sfeaturep, 1, 2, 0,
2831 doc: /* Returns t if FEATURE is present in this Emacs.
2833 Use this to conditionalize execution of lisp code based on the
2834 presence or absence of Emacs or environment extensions.
2835 Use `provide' to declare that a feature is available. This function
2836 looks at the value of the variable `features'. The optional argument
2837 SUBFEATURE can be used to check a specific subfeature of FEATURE. */)
2838 (feature, subfeature)
2839 Lisp_Object feature, subfeature;
2841 register Lisp_Object tem;
2842 CHECK_SYMBOL (feature);
2843 tem = Fmemq (feature, Vfeatures);
2844 if (!NILP (tem) && !NILP (subfeature))
2845 tem = Fmember (subfeature, Fget (feature, Qsubfeatures));
2846 return (NILP (tem)) ? Qnil : Qt;
2849 DEFUN ("provide", Fprovide, Sprovide, 1, 2, 0,
2850 doc: /* Announce that FEATURE is a feature of the current Emacs.
2851 The optional argument SUBFEATURES should be a list of symbols listing
2852 particular subfeatures supported in this version of FEATURE. */)
2853 (feature, subfeatures)
2854 Lisp_Object feature, subfeatures;
2856 register Lisp_Object tem;
2857 CHECK_SYMBOL (feature);
2858 CHECK_LIST (subfeatures);
2859 if (!NILP (Vautoload_queue))
2860 Vautoload_queue = Fcons (Fcons (make_number (0), Vfeatures),
2861 Vautoload_queue);
2862 tem = Fmemq (feature, Vfeatures);
2863 if (NILP (tem))
2864 Vfeatures = Fcons (feature, Vfeatures);
2865 if (!NILP (subfeatures))
2866 Fput (feature, Qsubfeatures, subfeatures);
2867 LOADHIST_ATTACH (Fcons (Qprovide, feature));
2869 /* Run any load-hooks for this file. */
2870 tem = Fassq (feature, Vafter_load_alist);
2871 if (CONSP (tem))
2872 Fprogn (XCDR (tem));
2874 return feature;
2877 /* `require' and its subroutines. */
2879 /* List of features currently being require'd, innermost first. */
2881 Lisp_Object require_nesting_list;
2883 Lisp_Object
2884 require_unwind (old_value)
2885 Lisp_Object old_value;
2887 return require_nesting_list = old_value;
2890 DEFUN ("require", Frequire, Srequire, 1, 3, 0,
2891 doc: /* If feature FEATURE is not loaded, load it from FILENAME.
2892 If FEATURE is not a member of the list `features', then the feature
2893 is not loaded; so load the file FILENAME.
2894 If FILENAME is omitted, the printname of FEATURE is used as the file name,
2895 and `load' will try to load this name appended with the suffix `.elc' or
2896 `.el', in that order. The name without appended suffix will not be used.
2897 If the optional third argument NOERROR is non-nil,
2898 then return nil if the file is not found instead of signaling an error.
2899 Normally the return value is FEATURE.
2900 The normal messages at start and end of loading FILENAME are suppressed. */)
2901 (feature, filename, noerror)
2902 Lisp_Object feature, filename, noerror;
2904 register Lisp_Object tem;
2905 struct gcpro gcpro1, gcpro2;
2906 int from_file = load_in_progress;
2908 CHECK_SYMBOL (feature);
2910 /* Record the presence of `require' in this file
2911 even if the feature specified is already loaded.
2912 But not more than once in any file,
2913 and not when we aren't loading or reading from a file. */
2914 if (!from_file)
2915 for (tem = Vcurrent_load_list; CONSP (tem); tem = XCDR (tem))
2916 if (NILP (XCDR (tem)) && STRINGP (XCAR (tem)))
2917 from_file = 1;
2919 if (from_file)
2921 tem = Fcons (Qrequire, feature);
2922 if (NILP (Fmember (tem, Vcurrent_load_list)))
2923 LOADHIST_ATTACH (tem);
2925 tem = Fmemq (feature, Vfeatures);
2927 if (NILP (tem))
2929 int count = SPECPDL_INDEX ();
2930 int nesting = 0;
2932 /* This is to make sure that loadup.el gives a clear picture
2933 of what files are preloaded and when. */
2934 if (! NILP (Vpurify_flag))
2935 error ("(require %s) while preparing to dump",
2936 SDATA (SYMBOL_NAME (feature)));
2938 /* A certain amount of recursive `require' is legitimate,
2939 but if we require the same feature recursively 3 times,
2940 signal an error. */
2941 tem = require_nesting_list;
2942 while (! NILP (tem))
2944 if (! NILP (Fequal (feature, XCAR (tem))))
2945 nesting++;
2946 tem = XCDR (tem);
2948 if (nesting > 3)
2949 error ("Recursive `require' for feature `%s'",
2950 SDATA (SYMBOL_NAME (feature)));
2952 /* Update the list for any nested `require's that occur. */
2953 record_unwind_protect (require_unwind, require_nesting_list);
2954 require_nesting_list = Fcons (feature, require_nesting_list);
2956 /* Value saved here is to be restored into Vautoload_queue */
2957 record_unwind_protect (un_autoload, Vautoload_queue);
2958 Vautoload_queue = Qt;
2960 /* Load the file. */
2961 GCPRO2 (feature, filename);
2962 tem = Fload (NILP (filename) ? Fsymbol_name (feature) : filename,
2963 noerror, Qt, Qnil, (NILP (filename) ? Qt : Qnil));
2964 UNGCPRO;
2966 /* If load failed entirely, return nil. */
2967 if (NILP (tem))
2968 return unbind_to (count, Qnil);
2970 tem = Fmemq (feature, Vfeatures);
2971 if (NILP (tem))
2972 error ("Required feature `%s' was not provided",
2973 SDATA (SYMBOL_NAME (feature)));
2975 /* Once loading finishes, don't undo it. */
2976 Vautoload_queue = Qt;
2977 feature = unbind_to (count, feature);
2980 return feature;
2983 /* Primitives for work of the "widget" library.
2984 In an ideal world, this section would not have been necessary.
2985 However, lisp function calls being as slow as they are, it turns
2986 out that some functions in the widget library (wid-edit.el) are the
2987 bottleneck of Widget operation. Here is their translation to C,
2988 for the sole reason of efficiency. */
2990 DEFUN ("plist-member", Fplist_member, Splist_member, 2, 2, 0,
2991 doc: /* Return non-nil if PLIST has the property PROP.
2992 PLIST is a property list, which is a list of the form
2993 \(PROP1 VALUE1 PROP2 VALUE2 ...\). PROP is a symbol.
2994 Unlike `plist-get', this allows you to distinguish between a missing
2995 property and a property with the value nil.
2996 The value is actually the tail of PLIST whose car is PROP. */)
2997 (plist, prop)
2998 Lisp_Object plist, prop;
3000 while (CONSP (plist) && !EQ (XCAR (plist), prop))
3002 QUIT;
3003 plist = XCDR (plist);
3004 plist = CDR (plist);
3006 return plist;
3009 DEFUN ("widget-put", Fwidget_put, Swidget_put, 3, 3, 0,
3010 doc: /* In WIDGET, set PROPERTY to VALUE.
3011 The value can later be retrieved with `widget-get'. */)
3012 (widget, property, value)
3013 Lisp_Object widget, property, value;
3015 CHECK_CONS (widget);
3016 XSETCDR (widget, Fplist_put (XCDR (widget), property, value));
3017 return value;
3020 DEFUN ("widget-get", Fwidget_get, Swidget_get, 2, 2, 0,
3021 doc: /* In WIDGET, get the value of PROPERTY.
3022 The value could either be specified when the widget was created, or
3023 later with `widget-put'. */)
3024 (widget, property)
3025 Lisp_Object widget, property;
3027 Lisp_Object tmp;
3029 while (1)
3031 if (NILP (widget))
3032 return Qnil;
3033 CHECK_CONS (widget);
3034 tmp = Fplist_member (XCDR (widget), property);
3035 if (CONSP (tmp))
3037 tmp = XCDR (tmp);
3038 return CAR (tmp);
3040 tmp = XCAR (widget);
3041 if (NILP (tmp))
3042 return Qnil;
3043 widget = Fget (tmp, Qwidget_type);
3047 DEFUN ("widget-apply", Fwidget_apply, Swidget_apply, 2, MANY, 0,
3048 doc: /* Apply the value of WIDGET's PROPERTY to the widget itself.
3049 ARGS are passed as extra arguments to the function.
3050 usage: (widget-apply WIDGET PROPERTY &rest ARGS) */)
3051 (nargs, args)
3052 int nargs;
3053 Lisp_Object *args;
3055 /* This function can GC. */
3056 Lisp_Object newargs[3];
3057 struct gcpro gcpro1, gcpro2;
3058 Lisp_Object result;
3060 newargs[0] = Fwidget_get (args[0], args[1]);
3061 newargs[1] = args[0];
3062 newargs[2] = Flist (nargs - 2, args + 2);
3063 GCPRO2 (newargs[0], newargs[2]);
3064 result = Fapply (3, newargs);
3065 UNGCPRO;
3066 return result;
3069 #ifdef HAVE_LANGINFO_CODESET
3070 #include <langinfo.h>
3071 #endif
3073 DEFUN ("locale-info", Flocale_info, Slocale_info, 1, 1, 0,
3074 doc: /* Access locale data ITEM for the current C locale, if available.
3075 ITEM should be one of the following:
3077 `codeset', returning the character set as a string (locale item CODESET);
3079 `days', returning a 7-element vector of day names (locale items DAY_n);
3081 `months', returning a 12-element vector of month names (locale items MON_n);
3083 `paper', returning a list (WIDTH HEIGHT) for the default paper size,
3084 both measured in milimeters (locale items PAPER_WIDTH, PAPER_HEIGHT).
3086 If the system can't provide such information through a call to
3087 `nl_langinfo', or if ITEM isn't from the list above, return nil.
3089 See also Info node `(libc)Locales'.
3091 The data read from the system are decoded using `locale-coding-system'. */)
3092 (item)
3093 Lisp_Object item;
3095 char *str = NULL;
3096 #ifdef HAVE_LANGINFO_CODESET
3097 Lisp_Object val;
3098 if (EQ (item, Qcodeset))
3100 str = nl_langinfo (CODESET);
3101 return build_string (str);
3103 #ifdef DAY_1
3104 else if (EQ (item, Qdays)) /* e.g. for calendar-day-name-array */
3106 Lisp_Object v = Fmake_vector (make_number (7), Qnil);
3107 const int days[7] = {DAY_1, DAY_2, DAY_3, DAY_4, DAY_5, DAY_6, DAY_7};
3108 int i;
3109 struct gcpro gcpro1;
3110 GCPRO1 (v);
3111 synchronize_system_time_locale ();
3112 for (i = 0; i < 7; i++)
3114 str = nl_langinfo (days[i]);
3115 val = make_unibyte_string (str, strlen (str));
3116 /* Fixme: Is this coding system necessarily right, even if
3117 it is consistent with CODESET? If not, what to do? */
3118 Faset (v, make_number (i),
3119 code_convert_string_norecord (val, Vlocale_coding_system,
3120 0));
3122 UNGCPRO;
3123 return v;
3125 #endif /* DAY_1 */
3126 #ifdef MON_1
3127 else if (EQ (item, Qmonths)) /* e.g. for calendar-month-name-array */
3129 Lisp_Object v = Fmake_vector (make_number (12), Qnil);
3130 const int months[12] = {MON_1, MON_2, MON_3, MON_4, MON_5, MON_6, MON_7,
3131 MON_8, MON_9, MON_10, MON_11, MON_12};
3132 int i;
3133 struct gcpro gcpro1;
3134 GCPRO1 (v);
3135 synchronize_system_time_locale ();
3136 for (i = 0; i < 12; i++)
3138 str = nl_langinfo (months[i]);
3139 val = make_unibyte_string (str, strlen (str));
3140 Faset (v, make_number (i),
3141 code_convert_string_norecord (val, Vlocale_coding_system, 0));
3143 UNGCPRO;
3144 return v;
3146 #endif /* MON_1 */
3147 /* LC_PAPER stuff isn't defined as accessible in glibc as of 2.3.1,
3148 but is in the locale files. This could be used by ps-print. */
3149 #ifdef PAPER_WIDTH
3150 else if (EQ (item, Qpaper))
3152 return list2 (make_number (nl_langinfo (PAPER_WIDTH)),
3153 make_number (nl_langinfo (PAPER_HEIGHT)));
3155 #endif /* PAPER_WIDTH */
3156 #endif /* HAVE_LANGINFO_CODESET*/
3157 return Qnil;
3160 /* base64 encode/decode functions (RFC 2045).
3161 Based on code from GNU recode. */
3163 #define MIME_LINE_LENGTH 76
3165 #define IS_ASCII(Character) \
3166 ((Character) < 128)
3167 #define IS_BASE64(Character) \
3168 (IS_ASCII (Character) && base64_char_to_value[Character] >= 0)
3169 #define IS_BASE64_IGNORABLE(Character) \
3170 ((Character) == ' ' || (Character) == '\t' || (Character) == '\n' \
3171 || (Character) == '\f' || (Character) == '\r')
3173 /* Used by base64_decode_1 to retrieve a non-base64-ignorable
3174 character or return retval if there are no characters left to
3175 process. */
3176 #define READ_QUADRUPLET_BYTE(retval) \
3177 do \
3179 if (i == length) \
3181 if (nchars_return) \
3182 *nchars_return = nchars; \
3183 return (retval); \
3185 c = from[i++]; \
3187 while (IS_BASE64_IGNORABLE (c))
3189 /* Table of characters coding the 64 values. */
3190 static const char base64_value_to_char[64] =
3192 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 0- 9 */
3193 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 10-19 */
3194 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', /* 20-29 */
3195 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', /* 30-39 */
3196 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', /* 40-49 */
3197 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', /* 50-59 */
3198 '8', '9', '+', '/' /* 60-63 */
3201 /* Table of base64 values for first 128 characters. */
3202 static const short base64_char_to_value[128] =
3204 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0- 9 */
3205 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10- 19 */
3206 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20- 29 */
3207 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 30- 39 */
3208 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, /* 40- 49 */
3209 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, /* 50- 59 */
3210 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, /* 60- 69 */
3211 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 70- 79 */
3212 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, /* 80- 89 */
3213 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, /* 90- 99 */
3214 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, /* 100-109 */
3215 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, /* 110-119 */
3216 49, 50, 51, -1, -1, -1, -1, -1 /* 120-127 */
3219 /* The following diagram shows the logical steps by which three octets
3220 get transformed into four base64 characters.
3222 .--------. .--------. .--------.
3223 |aaaaaabb| |bbbbcccc| |ccdddddd|
3224 `--------' `--------' `--------'
3225 6 2 4 4 2 6
3226 .--------+--------+--------+--------.
3227 |00aaaaaa|00bbbbbb|00cccccc|00dddddd|
3228 `--------+--------+--------+--------'
3230 .--------+--------+--------+--------.
3231 |AAAAAAAA|BBBBBBBB|CCCCCCCC|DDDDDDDD|
3232 `--------+--------+--------+--------'
3234 The octets are divided into 6 bit chunks, which are then encoded into
3235 base64 characters. */
3238 static int base64_encode_1 P_ ((const char *, char *, int, int, int));
3239 static int base64_decode_1 P_ ((const char *, char *, int, int, int *));
3241 DEFUN ("base64-encode-region", Fbase64_encode_region, Sbase64_encode_region,
3242 2, 3, "r",
3243 doc: /* Base64-encode the region between BEG and END.
3244 Return the length of the encoded text.
3245 Optional third argument NO-LINE-BREAK means do not break long lines
3246 into shorter lines. */)
3247 (beg, end, no_line_break)
3248 Lisp_Object beg, end, no_line_break;
3250 char *encoded;
3251 int allength, length;
3252 int ibeg, iend, encoded_length;
3253 int old_pos = PT;
3254 USE_SAFE_ALLOCA;
3256 validate_region (&beg, &end);
3258 ibeg = CHAR_TO_BYTE (XFASTINT (beg));
3259 iend = CHAR_TO_BYTE (XFASTINT (end));
3260 move_gap_both (XFASTINT (beg), ibeg);
3262 /* We need to allocate enough room for encoding the text.
3263 We need 33 1/3% more space, plus a newline every 76
3264 characters, and then we round up. */
3265 length = iend - ibeg;
3266 allength = length + length/3 + 1;
3267 allength += allength / MIME_LINE_LENGTH + 1 + 6;
3269 SAFE_ALLOCA (encoded, char *, allength);
3270 encoded_length = base64_encode_1 (BYTE_POS_ADDR (ibeg), encoded, length,
3271 NILP (no_line_break),
3272 !NILP (current_buffer->enable_multibyte_characters));
3273 if (encoded_length > allength)
3274 abort ();
3276 if (encoded_length < 0)
3278 /* The encoding wasn't possible. */
3279 SAFE_FREE ();
3280 error ("Multibyte character in data for base64 encoding");
3283 /* Now we have encoded the region, so we insert the new contents
3284 and delete the old. (Insert first in order to preserve markers.) */
3285 SET_PT_BOTH (XFASTINT (beg), ibeg);
3286 insert (encoded, encoded_length);
3287 SAFE_FREE ();
3288 del_range_byte (ibeg + encoded_length, iend + encoded_length, 1);
3290 /* If point was outside of the region, restore it exactly; else just
3291 move to the beginning of the region. */
3292 if (old_pos >= XFASTINT (end))
3293 old_pos += encoded_length - (XFASTINT (end) - XFASTINT (beg));
3294 else if (old_pos > XFASTINT (beg))
3295 old_pos = XFASTINT (beg);
3296 SET_PT (old_pos);
3298 /* We return the length of the encoded text. */
3299 return make_number (encoded_length);
3302 DEFUN ("base64-encode-string", Fbase64_encode_string, Sbase64_encode_string,
3303 1, 2, 0,
3304 doc: /* Base64-encode STRING and return the result.
3305 Optional second argument NO-LINE-BREAK means do not break long lines
3306 into shorter lines. */)
3307 (string, no_line_break)
3308 Lisp_Object string, no_line_break;
3310 int allength, length, encoded_length;
3311 char *encoded;
3312 Lisp_Object encoded_string;
3313 USE_SAFE_ALLOCA;
3315 CHECK_STRING (string);
3317 /* We need to allocate enough room for encoding the text.
3318 We need 33 1/3% more space, plus a newline every 76
3319 characters, and then we round up. */
3320 length = SBYTES (string);
3321 allength = length + length/3 + 1;
3322 allength += allength / MIME_LINE_LENGTH + 1 + 6;
3324 /* We need to allocate enough room for decoding the text. */
3325 SAFE_ALLOCA (encoded, char *, allength);
3327 encoded_length = base64_encode_1 (SDATA (string),
3328 encoded, length, NILP (no_line_break),
3329 STRING_MULTIBYTE (string));
3330 if (encoded_length > allength)
3331 abort ();
3333 if (encoded_length < 0)
3335 /* The encoding wasn't possible. */
3336 SAFE_FREE ();
3337 error ("Multibyte character in data for base64 encoding");
3340 encoded_string = make_unibyte_string (encoded, encoded_length);
3341 SAFE_FREE ();
3343 return encoded_string;
3346 static int
3347 base64_encode_1 (from, to, length, line_break, multibyte)
3348 const char *from;
3349 char *to;
3350 int length;
3351 int line_break;
3352 int multibyte;
3354 int counter = 0, i = 0;
3355 char *e = to;
3356 int c;
3357 unsigned int value;
3358 int bytes;
3360 while (i < length)
3362 if (multibyte)
3364 c = STRING_CHAR_AND_LENGTH (from + i, bytes);
3365 if (CHAR_BYTE8_P (c))
3366 c = CHAR_TO_BYTE8 (c);
3367 else if (c >= 256)
3368 return -1;
3369 i += bytes;
3371 else
3372 c = from[i++];
3374 /* Wrap line every 76 characters. */
3376 if (line_break)
3378 if (counter < MIME_LINE_LENGTH / 4)
3379 counter++;
3380 else
3382 *e++ = '\n';
3383 counter = 1;
3387 /* Process first byte of a triplet. */
3389 *e++ = base64_value_to_char[0x3f & c >> 2];
3390 value = (0x03 & c) << 4;
3392 /* Process second byte of a triplet. */
3394 if (i == length)
3396 *e++ = base64_value_to_char[value];
3397 *e++ = '=';
3398 *e++ = '=';
3399 break;
3402 if (multibyte)
3404 c = STRING_CHAR_AND_LENGTH (from + i, bytes);
3405 if (CHAR_BYTE8_P (c))
3406 c = CHAR_TO_BYTE8 (c);
3407 else if (c >= 256)
3408 return -1;
3409 i += bytes;
3411 else
3412 c = from[i++];
3414 *e++ = base64_value_to_char[value | (0x0f & c >> 4)];
3415 value = (0x0f & c) << 2;
3417 /* Process third byte of a triplet. */
3419 if (i == length)
3421 *e++ = base64_value_to_char[value];
3422 *e++ = '=';
3423 break;
3426 if (multibyte)
3428 c = STRING_CHAR_AND_LENGTH (from + i, bytes);
3429 if (CHAR_BYTE8_P (c))
3430 c = CHAR_TO_BYTE8 (c);
3431 else if (c >= 256)
3432 return -1;
3433 i += bytes;
3435 else
3436 c = from[i++];
3438 *e++ = base64_value_to_char[value | (0x03 & c >> 6)];
3439 *e++ = base64_value_to_char[0x3f & c];
3442 return e - to;
3446 DEFUN ("base64-decode-region", Fbase64_decode_region, Sbase64_decode_region,
3447 2, 2, "r",
3448 doc: /* Base64-decode the region between BEG and END.
3449 Return the length of the decoded text.
3450 If the region can't be decoded, signal an error and don't modify the buffer. */)
3451 (beg, end)
3452 Lisp_Object beg, end;
3454 int ibeg, iend, length, allength;
3455 char *decoded;
3456 int old_pos = PT;
3457 int decoded_length;
3458 int inserted_chars;
3459 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
3460 USE_SAFE_ALLOCA;
3462 validate_region (&beg, &end);
3464 ibeg = CHAR_TO_BYTE (XFASTINT (beg));
3465 iend = CHAR_TO_BYTE (XFASTINT (end));
3467 length = iend - ibeg;
3469 /* We need to allocate enough room for decoding the text. If we are
3470 working on a multibyte buffer, each decoded code may occupy at
3471 most two bytes. */
3472 allength = multibyte ? length * 2 : length;
3473 SAFE_ALLOCA (decoded, char *, allength);
3475 move_gap_both (XFASTINT (beg), ibeg);
3476 decoded_length = base64_decode_1 (BYTE_POS_ADDR (ibeg), decoded, length,
3477 multibyte, &inserted_chars);
3478 if (decoded_length > allength)
3479 abort ();
3481 if (decoded_length < 0)
3483 /* The decoding wasn't possible. */
3484 SAFE_FREE ();
3485 error ("Invalid base64 data");
3488 /* Now we have decoded the region, so we insert the new contents
3489 and delete the old. (Insert first in order to preserve markers.) */
3490 TEMP_SET_PT_BOTH (XFASTINT (beg), ibeg);
3491 insert_1_both (decoded, inserted_chars, decoded_length, 0, 1, 0);
3492 SAFE_FREE ();
3494 /* Delete the original text. */
3495 del_range_both (PT, PT_BYTE, XFASTINT (end) + inserted_chars,
3496 iend + decoded_length, 1);
3498 /* If point was outside of the region, restore it exactly; else just
3499 move to the beginning of the region. */
3500 if (old_pos >= XFASTINT (end))
3501 old_pos += inserted_chars - (XFASTINT (end) - XFASTINT (beg));
3502 else if (old_pos > XFASTINT (beg))
3503 old_pos = XFASTINT (beg);
3504 SET_PT (old_pos > ZV ? ZV : old_pos);
3506 return make_number (inserted_chars);
3509 DEFUN ("base64-decode-string", Fbase64_decode_string, Sbase64_decode_string,
3510 1, 1, 0,
3511 doc: /* Base64-decode STRING and return the result. */)
3512 (string)
3513 Lisp_Object string;
3515 char *decoded;
3516 int length, decoded_length;
3517 Lisp_Object decoded_string;
3518 USE_SAFE_ALLOCA;
3520 CHECK_STRING (string);
3522 length = SBYTES (string);
3523 /* We need to allocate enough room for decoding the text. */
3524 SAFE_ALLOCA (decoded, char *, length);
3526 /* The decoded result should be unibyte. */
3527 decoded_length = base64_decode_1 (SDATA (string), decoded, length,
3528 0, NULL);
3529 if (decoded_length > length)
3530 abort ();
3531 else if (decoded_length >= 0)
3532 decoded_string = make_unibyte_string (decoded, decoded_length);
3533 else
3534 decoded_string = Qnil;
3536 SAFE_FREE ();
3537 if (!STRINGP (decoded_string))
3538 error ("Invalid base64 data");
3540 return decoded_string;
3543 /* Base64-decode the data at FROM of LENGHT bytes into TO. If
3544 MULTIBYTE is nonzero, the decoded result should be in multibyte
3545 form. If NCHARS_RETRUN is not NULL, store the number of produced
3546 characters in *NCHARS_RETURN. */
3548 static int
3549 base64_decode_1 (from, to, length, multibyte, nchars_return)
3550 const char *from;
3551 char *to;
3552 int length;
3553 int multibyte;
3554 int *nchars_return;
3556 int i = 0;
3557 char *e = to;
3558 unsigned char c;
3559 unsigned long value;
3560 int nchars = 0;
3562 while (1)
3564 /* Process first byte of a quadruplet. */
3566 READ_QUADRUPLET_BYTE (e-to);
3568 if (!IS_BASE64 (c))
3569 return -1;
3570 value = base64_char_to_value[c] << 18;
3572 /* Process second byte of a quadruplet. */
3574 READ_QUADRUPLET_BYTE (-1);
3576 if (!IS_BASE64 (c))
3577 return -1;
3578 value |= base64_char_to_value[c] << 12;
3580 c = (unsigned char) (value >> 16);
3581 if (multibyte && c >= 128)
3582 e += BYTE8_STRING (c, e);
3583 else
3584 *e++ = c;
3585 nchars++;
3587 /* Process third byte of a quadruplet. */
3589 READ_QUADRUPLET_BYTE (-1);
3591 if (c == '=')
3593 READ_QUADRUPLET_BYTE (-1);
3595 if (c != '=')
3596 return -1;
3597 continue;
3600 if (!IS_BASE64 (c))
3601 return -1;
3602 value |= base64_char_to_value[c] << 6;
3604 c = (unsigned char) (0xff & value >> 8);
3605 if (multibyte && c >= 128)
3606 e += BYTE8_STRING (c, e);
3607 else
3608 *e++ = c;
3609 nchars++;
3611 /* Process fourth byte of a quadruplet. */
3613 READ_QUADRUPLET_BYTE (-1);
3615 if (c == '=')
3616 continue;
3618 if (!IS_BASE64 (c))
3619 return -1;
3620 value |= base64_char_to_value[c];
3622 c = (unsigned char) (0xff & value);
3623 if (multibyte && c >= 128)
3624 e += BYTE8_STRING (c, e);
3625 else
3626 *e++ = c;
3627 nchars++;
3633 /***********************************************************************
3634 ***** *****
3635 ***** Hash Tables *****
3636 ***** *****
3637 ***********************************************************************/
3639 /* Implemented by gerd@gnu.org. This hash table implementation was
3640 inspired by CMUCL hash tables. */
3642 /* Ideas:
3644 1. For small tables, association lists are probably faster than
3645 hash tables because they have lower overhead.
3647 For uses of hash tables where the O(1) behavior of table
3648 operations is not a requirement, it might therefore be a good idea
3649 not to hash. Instead, we could just do a linear search in the
3650 key_and_value vector of the hash table. This could be done
3651 if a `:linear-search t' argument is given to make-hash-table. */
3654 /* The list of all weak hash tables. Don't staticpro this one. */
3656 struct Lisp_Hash_Table *weak_hash_tables;
3658 /* Various symbols. */
3660 Lisp_Object Qhash_table_p, Qeq, Qeql, Qequal, Qkey, Qvalue;
3661 Lisp_Object QCtest, QCsize, QCrehash_size, QCrehash_threshold, QCweakness;
3662 Lisp_Object Qhash_table_test, Qkey_or_value, Qkey_and_value;
3664 /* Function prototypes. */
3666 static struct Lisp_Hash_Table *check_hash_table P_ ((Lisp_Object));
3667 static int get_key_arg P_ ((Lisp_Object, int, Lisp_Object *, char *));
3668 static void maybe_resize_hash_table P_ ((struct Lisp_Hash_Table *));
3669 static int cmpfn_eql P_ ((struct Lisp_Hash_Table *, Lisp_Object, unsigned,
3670 Lisp_Object, unsigned));
3671 static int cmpfn_equal P_ ((struct Lisp_Hash_Table *, Lisp_Object, unsigned,
3672 Lisp_Object, unsigned));
3673 static int cmpfn_user_defined P_ ((struct Lisp_Hash_Table *, Lisp_Object,
3674 unsigned, Lisp_Object, unsigned));
3675 static unsigned hashfn_eq P_ ((struct Lisp_Hash_Table *, Lisp_Object));
3676 static unsigned hashfn_eql P_ ((struct Lisp_Hash_Table *, Lisp_Object));
3677 static unsigned hashfn_equal P_ ((struct Lisp_Hash_Table *, Lisp_Object));
3678 static unsigned hashfn_user_defined P_ ((struct Lisp_Hash_Table *,
3679 Lisp_Object));
3680 static unsigned sxhash_string P_ ((unsigned char *, int));
3681 static unsigned sxhash_list P_ ((Lisp_Object, int));
3682 static unsigned sxhash_vector P_ ((Lisp_Object, int));
3683 static unsigned sxhash_bool_vector P_ ((Lisp_Object));
3684 static int sweep_weak_table P_ ((struct Lisp_Hash_Table *, int));
3688 /***********************************************************************
3689 Utilities
3690 ***********************************************************************/
3692 /* If OBJ is a Lisp hash table, return a pointer to its struct
3693 Lisp_Hash_Table. Otherwise, signal an error. */
3695 static struct Lisp_Hash_Table *
3696 check_hash_table (obj)
3697 Lisp_Object obj;
3699 CHECK_HASH_TABLE (obj);
3700 return XHASH_TABLE (obj);
3704 /* Value is the next integer I >= N, N >= 0 which is "almost" a prime
3705 number. */
3708 next_almost_prime (n)
3709 int n;
3711 if (n % 2 == 0)
3712 n += 1;
3713 if (n % 3 == 0)
3714 n += 2;
3715 if (n % 7 == 0)
3716 n += 4;
3717 return n;
3721 /* Find KEY in ARGS which has size NARGS. Don't consider indices for
3722 which USED[I] is non-zero. If found at index I in ARGS, set
3723 USED[I] and USED[I + 1] to 1, and return I + 1. Otherwise return
3724 -1. This function is used to extract a keyword/argument pair from
3725 a DEFUN parameter list. */
3727 static int
3728 get_key_arg (key, nargs, args, used)
3729 Lisp_Object key;
3730 int nargs;
3731 Lisp_Object *args;
3732 char *used;
3734 int i;
3736 for (i = 0; i < nargs - 1; ++i)
3737 if (!used[i] && EQ (args[i], key))
3738 break;
3740 if (i >= nargs - 1)
3741 i = -1;
3742 else
3744 used[i++] = 1;
3745 used[i] = 1;
3748 return i;
3752 /* Return a Lisp vector which has the same contents as VEC but has
3753 size NEW_SIZE, NEW_SIZE >= VEC->size. Entries in the resulting
3754 vector that are not copied from VEC are set to INIT. */
3756 Lisp_Object
3757 larger_vector (vec, new_size, init)
3758 Lisp_Object vec;
3759 int new_size;
3760 Lisp_Object init;
3762 struct Lisp_Vector *v;
3763 int i, old_size;
3765 xassert (VECTORP (vec));
3766 old_size = ASIZE (vec);
3767 xassert (new_size >= old_size);
3769 v = allocate_vector (new_size);
3770 bcopy (XVECTOR (vec)->contents, v->contents,
3771 old_size * sizeof *v->contents);
3772 for (i = old_size; i < new_size; ++i)
3773 v->contents[i] = init;
3774 XSETVECTOR (vec, v);
3775 return vec;
3779 /***********************************************************************
3780 Low-level Functions
3781 ***********************************************************************/
3783 /* Compare KEY1 which has hash code HASH1 and KEY2 with hash code
3784 HASH2 in hash table H using `eql'. Value is non-zero if KEY1 and
3785 KEY2 are the same. */
3787 static int
3788 cmpfn_eql (h, key1, hash1, key2, hash2)
3789 struct Lisp_Hash_Table *h;
3790 Lisp_Object key1, key2;
3791 unsigned hash1, hash2;
3793 return (FLOATP (key1)
3794 && FLOATP (key2)
3795 && XFLOAT_DATA (key1) == XFLOAT_DATA (key2));
3799 /* Compare KEY1 which has hash code HASH1 and KEY2 with hash code
3800 HASH2 in hash table H using `equal'. Value is non-zero if KEY1 and
3801 KEY2 are the same. */
3803 static int
3804 cmpfn_equal (h, key1, hash1, key2, hash2)
3805 struct Lisp_Hash_Table *h;
3806 Lisp_Object key1, key2;
3807 unsigned hash1, hash2;
3809 return hash1 == hash2 && !NILP (Fequal (key1, key2));
3813 /* Compare KEY1 which has hash code HASH1, and KEY2 with hash code
3814 HASH2 in hash table H using H->user_cmp_function. Value is non-zero
3815 if KEY1 and KEY2 are the same. */
3817 static int
3818 cmpfn_user_defined (h, key1, hash1, key2, hash2)
3819 struct Lisp_Hash_Table *h;
3820 Lisp_Object key1, key2;
3821 unsigned hash1, hash2;
3823 if (hash1 == hash2)
3825 Lisp_Object args[3];
3827 args[0] = h->user_cmp_function;
3828 args[1] = key1;
3829 args[2] = key2;
3830 return !NILP (Ffuncall (3, args));
3832 else
3833 return 0;
3837 /* Value is a hash code for KEY for use in hash table H which uses
3838 `eq' to compare keys. The hash code returned is guaranteed to fit
3839 in a Lisp integer. */
3841 static unsigned
3842 hashfn_eq (h, key)
3843 struct Lisp_Hash_Table *h;
3844 Lisp_Object key;
3846 unsigned hash = XUINT (key) ^ XTYPE (key);
3847 xassert ((hash & ~INTMASK) == 0);
3848 return hash;
3852 /* Value is a hash code for KEY for use in hash table H which uses
3853 `eql' to compare keys. The hash code returned is guaranteed to fit
3854 in a Lisp integer. */
3856 static unsigned
3857 hashfn_eql (h, key)
3858 struct Lisp_Hash_Table *h;
3859 Lisp_Object key;
3861 unsigned hash;
3862 if (FLOATP (key))
3863 hash = sxhash (key, 0);
3864 else
3865 hash = XUINT (key) ^ XTYPE (key);
3866 xassert ((hash & ~INTMASK) == 0);
3867 return hash;
3871 /* Value is a hash code for KEY for use in hash table H which uses
3872 `equal' to compare keys. The hash code returned is guaranteed to fit
3873 in a Lisp integer. */
3875 static unsigned
3876 hashfn_equal (h, key)
3877 struct Lisp_Hash_Table *h;
3878 Lisp_Object key;
3880 unsigned hash = sxhash (key, 0);
3881 xassert ((hash & ~INTMASK) == 0);
3882 return hash;
3886 /* Value is a hash code for KEY for use in hash table H which uses as
3887 user-defined function to compare keys. The hash code returned is
3888 guaranteed to fit in a Lisp integer. */
3890 static unsigned
3891 hashfn_user_defined (h, key)
3892 struct Lisp_Hash_Table *h;
3893 Lisp_Object key;
3895 Lisp_Object args[2], hash;
3897 args[0] = h->user_hash_function;
3898 args[1] = key;
3899 hash = Ffuncall (2, args);
3900 if (!INTEGERP (hash))
3901 signal_error ("Invalid hash code returned from user-supplied hash function", hash);
3902 return XUINT (hash);
3906 /* Create and initialize a new hash table.
3908 TEST specifies the test the hash table will use to compare keys.
3909 It must be either one of the predefined tests `eq', `eql' or
3910 `equal' or a symbol denoting a user-defined test named TEST with
3911 test and hash functions USER_TEST and USER_HASH.
3913 Give the table initial capacity SIZE, SIZE >= 0, an integer.
3915 If REHASH_SIZE is an integer, it must be > 0, and this hash table's
3916 new size when it becomes full is computed by adding REHASH_SIZE to
3917 its old size. If REHASH_SIZE is a float, it must be > 1.0, and the
3918 table's new size is computed by multiplying its old size with
3919 REHASH_SIZE.
3921 REHASH_THRESHOLD must be a float <= 1.0, and > 0. The table will
3922 be resized when the ratio of (number of entries in the table) /
3923 (table size) is >= REHASH_THRESHOLD.
3925 WEAK specifies the weakness of the table. If non-nil, it must be
3926 one of the symbols `key', `value', `key-or-value', or `key-and-value'. */
3928 Lisp_Object
3929 make_hash_table (test, size, rehash_size, rehash_threshold, weak,
3930 user_test, user_hash)
3931 Lisp_Object test, size, rehash_size, rehash_threshold, weak;
3932 Lisp_Object user_test, user_hash;
3934 struct Lisp_Hash_Table *h;
3935 Lisp_Object table;
3936 int index_size, i, sz;
3938 /* Preconditions. */
3939 xassert (SYMBOLP (test));
3940 xassert (INTEGERP (size) && XINT (size) >= 0);
3941 xassert ((INTEGERP (rehash_size) && XINT (rehash_size) > 0)
3942 || (FLOATP (rehash_size) && XFLOATINT (rehash_size) > 1.0));
3943 xassert (FLOATP (rehash_threshold)
3944 && XFLOATINT (rehash_threshold) > 0
3945 && XFLOATINT (rehash_threshold) <= 1.0);
3947 if (XFASTINT (size) == 0)
3948 size = make_number (1);
3950 /* Allocate a table and initialize it. */
3951 h = allocate_hash_table ();
3953 /* Initialize hash table slots. */
3954 sz = XFASTINT (size);
3956 h->test = test;
3957 if (EQ (test, Qeql))
3959 h->cmpfn = cmpfn_eql;
3960 h->hashfn = hashfn_eql;
3962 else if (EQ (test, Qeq))
3964 h->cmpfn = NULL;
3965 h->hashfn = hashfn_eq;
3967 else if (EQ (test, Qequal))
3969 h->cmpfn = cmpfn_equal;
3970 h->hashfn = hashfn_equal;
3972 else
3974 h->user_cmp_function = user_test;
3975 h->user_hash_function = user_hash;
3976 h->cmpfn = cmpfn_user_defined;
3977 h->hashfn = hashfn_user_defined;
3980 h->weak = weak;
3981 h->rehash_threshold = rehash_threshold;
3982 h->rehash_size = rehash_size;
3983 h->count = 0;
3984 h->key_and_value = Fmake_vector (make_number (2 * sz), Qnil);
3985 h->hash = Fmake_vector (size, Qnil);
3986 h->next = Fmake_vector (size, Qnil);
3987 /* Cast to int here avoids losing with gcc 2.95 on Tru64/Alpha... */
3988 index_size = next_almost_prime ((int) (sz / XFLOATINT (rehash_threshold)));
3989 h->index = Fmake_vector (make_number (index_size), Qnil);
3991 /* Set up the free list. */
3992 for (i = 0; i < sz - 1; ++i)
3993 HASH_NEXT (h, i) = make_number (i + 1);
3994 h->next_free = make_number (0);
3996 XSET_HASH_TABLE (table, h);
3997 xassert (HASH_TABLE_P (table));
3998 xassert (XHASH_TABLE (table) == h);
4000 /* Maybe add this hash table to the list of all weak hash tables. */
4001 if (NILP (h->weak))
4002 h->next_weak = NULL;
4003 else
4005 h->next_weak = weak_hash_tables;
4006 weak_hash_tables = h;
4009 return table;
4013 /* Return a copy of hash table H1. Keys and values are not copied,
4014 only the table itself is. */
4016 Lisp_Object
4017 copy_hash_table (h1)
4018 struct Lisp_Hash_Table *h1;
4020 Lisp_Object table;
4021 struct Lisp_Hash_Table *h2;
4022 struct Lisp_Vector *next;
4024 h2 = allocate_hash_table ();
4025 next = h2->vec_next;
4026 bcopy (h1, h2, sizeof *h2);
4027 h2->vec_next = next;
4028 h2->key_and_value = Fcopy_sequence (h1->key_and_value);
4029 h2->hash = Fcopy_sequence (h1->hash);
4030 h2->next = Fcopy_sequence (h1->next);
4031 h2->index = Fcopy_sequence (h1->index);
4032 XSET_HASH_TABLE (table, h2);
4034 /* Maybe add this hash table to the list of all weak hash tables. */
4035 if (!NILP (h2->weak))
4037 h2->next_weak = weak_hash_tables;
4038 weak_hash_tables = h2;
4041 return table;
4045 /* Resize hash table H if it's too full. If H cannot be resized
4046 because it's already too large, throw an error. */
4048 static INLINE void
4049 maybe_resize_hash_table (h)
4050 struct Lisp_Hash_Table *h;
4052 if (NILP (h->next_free))
4054 int old_size = HASH_TABLE_SIZE (h);
4055 int i, new_size, index_size;
4056 EMACS_INT nsize;
4058 if (INTEGERP (h->rehash_size))
4059 new_size = old_size + XFASTINT (h->rehash_size);
4060 else
4061 new_size = old_size * XFLOATINT (h->rehash_size);
4062 new_size = max (old_size + 1, new_size);
4063 index_size = next_almost_prime ((int)
4064 (new_size
4065 / XFLOATINT (h->rehash_threshold)));
4066 /* Assignment to EMACS_INT stops GCC whining about limited range
4067 of data type. */
4068 nsize = max (index_size, 2 * new_size);
4069 if (nsize > MOST_POSITIVE_FIXNUM)
4070 error ("Hash table too large to resize");
4072 h->key_and_value = larger_vector (h->key_and_value, 2 * new_size, Qnil);
4073 h->next = larger_vector (h->next, new_size, Qnil);
4074 h->hash = larger_vector (h->hash, new_size, Qnil);
4075 h->index = Fmake_vector (make_number (index_size), Qnil);
4077 /* Update the free list. Do it so that new entries are added at
4078 the end of the free list. This makes some operations like
4079 maphash faster. */
4080 for (i = old_size; i < new_size - 1; ++i)
4081 HASH_NEXT (h, i) = make_number (i + 1);
4083 if (!NILP (h->next_free))
4085 Lisp_Object last, next;
4087 last = h->next_free;
4088 while (next = HASH_NEXT (h, XFASTINT (last)),
4089 !NILP (next))
4090 last = next;
4092 HASH_NEXT (h, XFASTINT (last)) = make_number (old_size);
4094 else
4095 XSETFASTINT (h->next_free, old_size);
4097 /* Rehash. */
4098 for (i = 0; i < old_size; ++i)
4099 if (!NILP (HASH_HASH (h, i)))
4101 unsigned hash_code = XUINT (HASH_HASH (h, i));
4102 int start_of_bucket = hash_code % ASIZE (h->index);
4103 HASH_NEXT (h, i) = HASH_INDEX (h, start_of_bucket);
4104 HASH_INDEX (h, start_of_bucket) = make_number (i);
4110 /* Lookup KEY in hash table H. If HASH is non-null, return in *HASH
4111 the hash code of KEY. Value is the index of the entry in H
4112 matching KEY, or -1 if not found. */
4115 hash_lookup (h, key, hash)
4116 struct Lisp_Hash_Table *h;
4117 Lisp_Object key;
4118 unsigned *hash;
4120 unsigned hash_code;
4121 int start_of_bucket;
4122 Lisp_Object idx;
4124 hash_code = h->hashfn (h, key);
4125 if (hash)
4126 *hash = hash_code;
4128 start_of_bucket = hash_code % ASIZE (h->index);
4129 idx = HASH_INDEX (h, start_of_bucket);
4131 /* We need not gcpro idx since it's either an integer or nil. */
4132 while (!NILP (idx))
4134 int i = XFASTINT (idx);
4135 if (EQ (key, HASH_KEY (h, i))
4136 || (h->cmpfn
4137 && h->cmpfn (h, key, hash_code,
4138 HASH_KEY (h, i), XUINT (HASH_HASH (h, i)))))
4139 break;
4140 idx = HASH_NEXT (h, i);
4143 return NILP (idx) ? -1 : XFASTINT (idx);
4147 /* Put an entry into hash table H that associates KEY with VALUE.
4148 HASH is a previously computed hash code of KEY.
4149 Value is the index of the entry in H matching KEY. */
4152 hash_put (h, key, value, hash)
4153 struct Lisp_Hash_Table *h;
4154 Lisp_Object key, value;
4155 unsigned hash;
4157 int start_of_bucket, i;
4159 xassert ((hash & ~INTMASK) == 0);
4161 /* Increment count after resizing because resizing may fail. */
4162 maybe_resize_hash_table (h);
4163 h->count++;
4165 /* Store key/value in the key_and_value vector. */
4166 i = XFASTINT (h->next_free);
4167 h->next_free = HASH_NEXT (h, i);
4168 HASH_KEY (h, i) = key;
4169 HASH_VALUE (h, i) = value;
4171 /* Remember its hash code. */
4172 HASH_HASH (h, i) = make_number (hash);
4174 /* Add new entry to its collision chain. */
4175 start_of_bucket = hash % ASIZE (h->index);
4176 HASH_NEXT (h, i) = HASH_INDEX (h, start_of_bucket);
4177 HASH_INDEX (h, start_of_bucket) = make_number (i);
4178 return i;
4182 /* Remove the entry matching KEY from hash table H, if there is one. */
4184 static void
4185 hash_remove_from_table (h, key)
4186 struct Lisp_Hash_Table *h;
4187 Lisp_Object key;
4189 unsigned hash_code;
4190 int start_of_bucket;
4191 Lisp_Object idx, prev;
4193 hash_code = h->hashfn (h, key);
4194 start_of_bucket = hash_code % ASIZE (h->index);
4195 idx = HASH_INDEX (h, start_of_bucket);
4196 prev = Qnil;
4198 /* We need not gcpro idx, prev since they're either integers or nil. */
4199 while (!NILP (idx))
4201 int i = XFASTINT (idx);
4203 if (EQ (key, HASH_KEY (h, i))
4204 || (h->cmpfn
4205 && h->cmpfn (h, key, hash_code,
4206 HASH_KEY (h, i), XUINT (HASH_HASH (h, i)))))
4208 /* Take entry out of collision chain. */
4209 if (NILP (prev))
4210 HASH_INDEX (h, start_of_bucket) = HASH_NEXT (h, i);
4211 else
4212 HASH_NEXT (h, XFASTINT (prev)) = HASH_NEXT (h, i);
4214 /* Clear slots in key_and_value and add the slots to
4215 the free list. */
4216 HASH_KEY (h, i) = HASH_VALUE (h, i) = HASH_HASH (h, i) = Qnil;
4217 HASH_NEXT (h, i) = h->next_free;
4218 h->next_free = make_number (i);
4219 h->count--;
4220 xassert (h->count >= 0);
4221 break;
4223 else
4225 prev = idx;
4226 idx = HASH_NEXT (h, i);
4232 /* Clear hash table H. */
4234 void
4235 hash_clear (h)
4236 struct Lisp_Hash_Table *h;
4238 if (h->count > 0)
4240 int i, size = HASH_TABLE_SIZE (h);
4242 for (i = 0; i < size; ++i)
4244 HASH_NEXT (h, i) = i < size - 1 ? make_number (i + 1) : Qnil;
4245 HASH_KEY (h, i) = Qnil;
4246 HASH_VALUE (h, i) = Qnil;
4247 HASH_HASH (h, i) = Qnil;
4250 for (i = 0; i < ASIZE (h->index); ++i)
4251 ASET (h->index, i, Qnil);
4253 h->next_free = make_number (0);
4254 h->count = 0;
4260 /************************************************************************
4261 Weak Hash Tables
4262 ************************************************************************/
4264 void
4265 init_weak_hash_tables ()
4267 weak_hash_tables = NULL;
4270 /* Sweep weak hash table H. REMOVE_ENTRIES_P non-zero means remove
4271 entries from the table that don't survive the current GC.
4272 REMOVE_ENTRIES_P zero means mark entries that are in use. Value is
4273 non-zero if anything was marked. */
4275 static int
4276 sweep_weak_table (h, remove_entries_p)
4277 struct Lisp_Hash_Table *h;
4278 int remove_entries_p;
4280 int bucket, n, marked;
4282 n = ASIZE (h->index) & ~ARRAY_MARK_FLAG;
4283 marked = 0;
4285 for (bucket = 0; bucket < n; ++bucket)
4287 Lisp_Object idx, next, prev;
4289 /* Follow collision chain, removing entries that
4290 don't survive this garbage collection. */
4291 prev = Qnil;
4292 for (idx = HASH_INDEX (h, bucket); !NILP (idx); idx = next)
4294 int i = XFASTINT (idx);
4295 int key_known_to_survive_p = survives_gc_p (HASH_KEY (h, i));
4296 int value_known_to_survive_p = survives_gc_p (HASH_VALUE (h, i));
4297 int remove_p;
4299 if (EQ (h->weak, Qkey))
4300 remove_p = !key_known_to_survive_p;
4301 else if (EQ (h->weak, Qvalue))
4302 remove_p = !value_known_to_survive_p;
4303 else if (EQ (h->weak, Qkey_or_value))
4304 remove_p = !(key_known_to_survive_p || value_known_to_survive_p);
4305 else if (EQ (h->weak, Qkey_and_value))
4306 remove_p = !(key_known_to_survive_p && value_known_to_survive_p);
4307 else
4308 abort ();
4310 next = HASH_NEXT (h, i);
4312 if (remove_entries_p)
4314 if (remove_p)
4316 /* Take out of collision chain. */
4317 if (NILP (prev))
4318 HASH_INDEX (h, bucket) = next;
4319 else
4320 HASH_NEXT (h, XFASTINT (prev)) = next;
4322 /* Add to free list. */
4323 HASH_NEXT (h, i) = h->next_free;
4324 h->next_free = idx;
4326 /* Clear key, value, and hash. */
4327 HASH_KEY (h, i) = HASH_VALUE (h, i) = Qnil;
4328 HASH_HASH (h, i) = Qnil;
4330 h->count--;
4332 else
4334 prev = idx;
4337 else
4339 if (!remove_p)
4341 /* Make sure key and value survive. */
4342 if (!key_known_to_survive_p)
4344 mark_object (HASH_KEY (h, i));
4345 marked = 1;
4348 if (!value_known_to_survive_p)
4350 mark_object (HASH_VALUE (h, i));
4351 marked = 1;
4358 return marked;
4361 /* Remove elements from weak hash tables that don't survive the
4362 current garbage collection. Remove weak tables that don't survive
4363 from Vweak_hash_tables. Called from gc_sweep. */
4365 void
4366 sweep_weak_hash_tables ()
4368 struct Lisp_Hash_Table *h, *used, *next;
4369 int marked;
4371 /* Mark all keys and values that are in use. Keep on marking until
4372 there is no more change. This is necessary for cases like
4373 value-weak table A containing an entry X -> Y, where Y is used in a
4374 key-weak table B, Z -> Y. If B comes after A in the list of weak
4375 tables, X -> Y might be removed from A, although when looking at B
4376 one finds that it shouldn't. */
4379 marked = 0;
4380 for (h = weak_hash_tables; h; h = h->next_weak)
4382 if (h->size & ARRAY_MARK_FLAG)
4383 marked |= sweep_weak_table (h, 0);
4386 while (marked);
4388 /* Remove tables and entries that aren't used. */
4389 for (h = weak_hash_tables, used = NULL; h; h = next)
4391 next = h->next_weak;
4393 if (h->size & ARRAY_MARK_FLAG)
4395 /* TABLE is marked as used. Sweep its contents. */
4396 if (h->count > 0)
4397 sweep_weak_table (h, 1);
4399 /* Add table to the list of used weak hash tables. */
4400 h->next_weak = used;
4401 used = h;
4405 weak_hash_tables = used;
4410 /***********************************************************************
4411 Hash Code Computation
4412 ***********************************************************************/
4414 /* Maximum depth up to which to dive into Lisp structures. */
4416 #define SXHASH_MAX_DEPTH 3
4418 /* Maximum length up to which to take list and vector elements into
4419 account. */
4421 #define SXHASH_MAX_LEN 7
4423 /* Combine two integers X and Y for hashing. */
4425 #define SXHASH_COMBINE(X, Y) \
4426 ((((unsigned)(X) << 4) + (((unsigned)(X) >> 24) & 0x0fffffff)) \
4427 + (unsigned)(Y))
4430 /* Return a hash for string PTR which has length LEN. The hash
4431 code returned is guaranteed to fit in a Lisp integer. */
4433 static unsigned
4434 sxhash_string (ptr, len)
4435 unsigned char *ptr;
4436 int len;
4438 unsigned char *p = ptr;
4439 unsigned char *end = p + len;
4440 unsigned char c;
4441 unsigned hash = 0;
4443 while (p != end)
4445 c = *p++;
4446 if (c >= 0140)
4447 c -= 40;
4448 hash = ((hash << 4) + (hash >> 28) + c);
4451 return hash & INTMASK;
4455 /* Return a hash for list LIST. DEPTH is the current depth in the
4456 list. We don't recurse deeper than SXHASH_MAX_DEPTH in it. */
4458 static unsigned
4459 sxhash_list (list, depth)
4460 Lisp_Object list;
4461 int depth;
4463 unsigned hash = 0;
4464 int i;
4466 if (depth < SXHASH_MAX_DEPTH)
4467 for (i = 0;
4468 CONSP (list) && i < SXHASH_MAX_LEN;
4469 list = XCDR (list), ++i)
4471 unsigned hash2 = sxhash (XCAR (list), depth + 1);
4472 hash = SXHASH_COMBINE (hash, hash2);
4475 if (!NILP (list))
4477 unsigned hash2 = sxhash (list, depth + 1);
4478 hash = SXHASH_COMBINE (hash, hash2);
4481 return hash;
4485 /* Return a hash for vector VECTOR. DEPTH is the current depth in
4486 the Lisp structure. */
4488 static unsigned
4489 sxhash_vector (vec, depth)
4490 Lisp_Object vec;
4491 int depth;
4493 unsigned hash = ASIZE (vec);
4494 int i, n;
4496 n = min (SXHASH_MAX_LEN, ASIZE (vec));
4497 for (i = 0; i < n; ++i)
4499 unsigned hash2 = sxhash (AREF (vec, i), depth + 1);
4500 hash = SXHASH_COMBINE (hash, hash2);
4503 return hash;
4507 /* Return a hash for bool-vector VECTOR. */
4509 static unsigned
4510 sxhash_bool_vector (vec)
4511 Lisp_Object vec;
4513 unsigned hash = XBOOL_VECTOR (vec)->size;
4514 int i, n;
4516 n = min (SXHASH_MAX_LEN, XBOOL_VECTOR (vec)->vector_size);
4517 for (i = 0; i < n; ++i)
4518 hash = SXHASH_COMBINE (hash, XBOOL_VECTOR (vec)->data[i]);
4520 return hash;
4524 /* Return a hash code for OBJ. DEPTH is the current depth in the Lisp
4525 structure. Value is an unsigned integer clipped to INTMASK. */
4527 unsigned
4528 sxhash (obj, depth)
4529 Lisp_Object obj;
4530 int depth;
4532 unsigned hash;
4534 if (depth > SXHASH_MAX_DEPTH)
4535 return 0;
4537 switch (XTYPE (obj))
4539 case_Lisp_Int:
4540 hash = XUINT (obj);
4541 break;
4543 case Lisp_Misc:
4544 hash = XUINT (obj);
4545 break;
4547 case Lisp_Symbol:
4548 obj = SYMBOL_NAME (obj);
4549 /* Fall through. */
4551 case Lisp_String:
4552 hash = sxhash_string (SDATA (obj), SCHARS (obj));
4553 break;
4555 /* This can be everything from a vector to an overlay. */
4556 case Lisp_Vectorlike:
4557 if (VECTORP (obj))
4558 /* According to the CL HyperSpec, two arrays are equal only if
4559 they are `eq', except for strings and bit-vectors. In
4560 Emacs, this works differently. We have to compare element
4561 by element. */
4562 hash = sxhash_vector (obj, depth);
4563 else if (BOOL_VECTOR_P (obj))
4564 hash = sxhash_bool_vector (obj);
4565 else
4566 /* Others are `equal' if they are `eq', so let's take their
4567 address as hash. */
4568 hash = XUINT (obj);
4569 break;
4571 case Lisp_Cons:
4572 hash = sxhash_list (obj, depth);
4573 break;
4575 case Lisp_Float:
4577 double val = XFLOAT_DATA (obj);
4578 unsigned char *p = (unsigned char *) &val;
4579 unsigned char *e = p + sizeof val;
4580 for (hash = 0; p < e; ++p)
4581 hash = SXHASH_COMBINE (hash, *p);
4582 break;
4585 default:
4586 abort ();
4589 return hash & INTMASK;
4594 /***********************************************************************
4595 Lisp Interface
4596 ***********************************************************************/
4599 DEFUN ("sxhash", Fsxhash, Ssxhash, 1, 1, 0,
4600 doc: /* Compute a hash code for OBJ and return it as integer. */)
4601 (obj)
4602 Lisp_Object obj;
4604 unsigned hash = sxhash (obj, 0);
4605 return make_number (hash);
4609 DEFUN ("make-hash-table", Fmake_hash_table, Smake_hash_table, 0, MANY, 0,
4610 doc: /* Create and return a new hash table.
4612 Arguments are specified as keyword/argument pairs. The following
4613 arguments are defined:
4615 :test TEST -- TEST must be a symbol that specifies how to compare
4616 keys. Default is `eql'. Predefined are the tests `eq', `eql', and
4617 `equal'. User-supplied test and hash functions can be specified via
4618 `define-hash-table-test'.
4620 :size SIZE -- A hint as to how many elements will be put in the table.
4621 Default is 65.
4623 :rehash-size REHASH-SIZE - Indicates how to expand the table when it
4624 fills up. If REHASH-SIZE is an integer, add that many space. If it
4625 is a float, it must be > 1.0, and the new size is computed by
4626 multiplying the old size with that factor. Default is 1.5.
4628 :rehash-threshold THRESHOLD -- THRESHOLD must a float > 0, and <= 1.0.
4629 Resize the hash table when ratio of the number of entries in the
4630 table. Default is 0.8.
4632 :weakness WEAK -- WEAK must be one of nil, t, `key', `value',
4633 `key-or-value', or `key-and-value'. If WEAK is not nil, the table
4634 returned is a weak table. Key/value pairs are removed from a weak
4635 hash table when there are no non-weak references pointing to their
4636 key, value, one of key or value, or both key and value, depending on
4637 WEAK. WEAK t is equivalent to `key-and-value'. Default value of WEAK
4638 is nil.
4640 usage: (make-hash-table &rest KEYWORD-ARGS) */)
4641 (nargs, args)
4642 int nargs;
4643 Lisp_Object *args;
4645 Lisp_Object test, size, rehash_size, rehash_threshold, weak;
4646 Lisp_Object user_test, user_hash;
4647 char *used;
4648 int i;
4650 /* The vector `used' is used to keep track of arguments that
4651 have been consumed. */
4652 used = (char *) alloca (nargs * sizeof *used);
4653 bzero (used, nargs * sizeof *used);
4655 /* See if there's a `:test TEST' among the arguments. */
4656 i = get_key_arg (QCtest, nargs, args, used);
4657 test = i < 0 ? Qeql : args[i];
4658 if (!EQ (test, Qeq) && !EQ (test, Qeql) && !EQ (test, Qequal))
4660 /* See if it is a user-defined test. */
4661 Lisp_Object prop;
4663 prop = Fget (test, Qhash_table_test);
4664 if (!CONSP (prop) || !CONSP (XCDR (prop)))
4665 signal_error ("Invalid hash table test", test);
4666 user_test = XCAR (prop);
4667 user_hash = XCAR (XCDR (prop));
4669 else
4670 user_test = user_hash = Qnil;
4672 /* See if there's a `:size SIZE' argument. */
4673 i = get_key_arg (QCsize, nargs, args, used);
4674 size = i < 0 ? Qnil : args[i];
4675 if (NILP (size))
4676 size = make_number (DEFAULT_HASH_SIZE);
4677 else if (!INTEGERP (size) || XINT (size) < 0)
4678 signal_error ("Invalid hash table size", size);
4680 /* Look for `:rehash-size SIZE'. */
4681 i = get_key_arg (QCrehash_size, nargs, args, used);
4682 rehash_size = i < 0 ? make_float (DEFAULT_REHASH_SIZE) : args[i];
4683 if (!NUMBERP (rehash_size)
4684 || (INTEGERP (rehash_size) && XINT (rehash_size) <= 0)
4685 || XFLOATINT (rehash_size) <= 1.0)
4686 signal_error ("Invalid hash table rehash size", rehash_size);
4688 /* Look for `:rehash-threshold THRESHOLD'. */
4689 i = get_key_arg (QCrehash_threshold, nargs, args, used);
4690 rehash_threshold = i < 0 ? make_float (DEFAULT_REHASH_THRESHOLD) : args[i];
4691 if (!FLOATP (rehash_threshold)
4692 || XFLOATINT (rehash_threshold) <= 0.0
4693 || XFLOATINT (rehash_threshold) > 1.0)
4694 signal_error ("Invalid hash table rehash threshold", rehash_threshold);
4696 /* Look for `:weakness WEAK'. */
4697 i = get_key_arg (QCweakness, nargs, args, used);
4698 weak = i < 0 ? Qnil : args[i];
4699 if (EQ (weak, Qt))
4700 weak = Qkey_and_value;
4701 if (!NILP (weak)
4702 && !EQ (weak, Qkey)
4703 && !EQ (weak, Qvalue)
4704 && !EQ (weak, Qkey_or_value)
4705 && !EQ (weak, Qkey_and_value))
4706 signal_error ("Invalid hash table weakness", weak);
4708 /* Now, all args should have been used up, or there's a problem. */
4709 for (i = 0; i < nargs; ++i)
4710 if (!used[i])
4711 signal_error ("Invalid argument list", args[i]);
4713 return make_hash_table (test, size, rehash_size, rehash_threshold, weak,
4714 user_test, user_hash);
4718 DEFUN ("copy-hash-table", Fcopy_hash_table, Scopy_hash_table, 1, 1, 0,
4719 doc: /* Return a copy of hash table TABLE. */)
4720 (table)
4721 Lisp_Object table;
4723 return copy_hash_table (check_hash_table (table));
4727 DEFUN ("hash-table-count", Fhash_table_count, Shash_table_count, 1, 1, 0,
4728 doc: /* Return the number of elements in TABLE. */)
4729 (table)
4730 Lisp_Object table;
4732 return make_number (check_hash_table (table)->count);
4736 DEFUN ("hash-table-rehash-size", Fhash_table_rehash_size,
4737 Shash_table_rehash_size, 1, 1, 0,
4738 doc: /* Return the current rehash size of TABLE. */)
4739 (table)
4740 Lisp_Object table;
4742 return check_hash_table (table)->rehash_size;
4746 DEFUN ("hash-table-rehash-threshold", Fhash_table_rehash_threshold,
4747 Shash_table_rehash_threshold, 1, 1, 0,
4748 doc: /* Return the current rehash threshold of TABLE. */)
4749 (table)
4750 Lisp_Object table;
4752 return check_hash_table (table)->rehash_threshold;
4756 DEFUN ("hash-table-size", Fhash_table_size, Shash_table_size, 1, 1, 0,
4757 doc: /* Return the size of TABLE.
4758 The size can be used as an argument to `make-hash-table' to create
4759 a hash table than can hold as many elements of TABLE holds
4760 without need for resizing. */)
4761 (table)
4762 Lisp_Object table;
4764 struct Lisp_Hash_Table *h = check_hash_table (table);
4765 return make_number (HASH_TABLE_SIZE (h));
4769 DEFUN ("hash-table-test", Fhash_table_test, Shash_table_test, 1, 1, 0,
4770 doc: /* Return the test TABLE uses. */)
4771 (table)
4772 Lisp_Object table;
4774 return check_hash_table (table)->test;
4778 DEFUN ("hash-table-weakness", Fhash_table_weakness, Shash_table_weakness,
4779 1, 1, 0,
4780 doc: /* Return the weakness of TABLE. */)
4781 (table)
4782 Lisp_Object table;
4784 return check_hash_table (table)->weak;
4788 DEFUN ("hash-table-p", Fhash_table_p, Shash_table_p, 1, 1, 0,
4789 doc: /* Return t if OBJ is a Lisp hash table object. */)
4790 (obj)
4791 Lisp_Object obj;
4793 return HASH_TABLE_P (obj) ? Qt : Qnil;
4797 DEFUN ("clrhash", Fclrhash, Sclrhash, 1, 1, 0,
4798 doc: /* Clear hash table TABLE and return it. */)
4799 (table)
4800 Lisp_Object table;
4802 hash_clear (check_hash_table (table));
4803 /* Be compatible with XEmacs. */
4804 return table;
4808 DEFUN ("gethash", Fgethash, Sgethash, 2, 3, 0,
4809 doc: /* Look up KEY in TABLE and return its associated value.
4810 If KEY is not found, return DFLT which defaults to nil. */)
4811 (key, table, dflt)
4812 Lisp_Object key, table, dflt;
4814 struct Lisp_Hash_Table *h = check_hash_table (table);
4815 int i = hash_lookup (h, key, NULL);
4816 return i >= 0 ? HASH_VALUE (h, i) : dflt;
4820 DEFUN ("puthash", Fputhash, Sputhash, 3, 3, 0,
4821 doc: /* Associate KEY with VALUE in hash table TABLE.
4822 If KEY is already present in table, replace its current value with
4823 VALUE. */)
4824 (key, value, table)
4825 Lisp_Object key, value, table;
4827 struct Lisp_Hash_Table *h = check_hash_table (table);
4828 int i;
4829 unsigned hash;
4831 i = hash_lookup (h, key, &hash);
4832 if (i >= 0)
4833 HASH_VALUE (h, i) = value;
4834 else
4835 hash_put (h, key, value, hash);
4837 return value;
4841 DEFUN ("remhash", Fremhash, Sremhash, 2, 2, 0,
4842 doc: /* Remove KEY from TABLE. */)
4843 (key, table)
4844 Lisp_Object key, table;
4846 struct Lisp_Hash_Table *h = check_hash_table (table);
4847 hash_remove_from_table (h, key);
4848 return Qnil;
4852 DEFUN ("maphash", Fmaphash, Smaphash, 2, 2, 0,
4853 doc: /* Call FUNCTION for all entries in hash table TABLE.
4854 FUNCTION is called with two arguments, KEY and VALUE. */)
4855 (function, table)
4856 Lisp_Object function, table;
4858 struct Lisp_Hash_Table *h = check_hash_table (table);
4859 Lisp_Object args[3];
4860 int i;
4862 for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
4863 if (!NILP (HASH_HASH (h, i)))
4865 args[0] = function;
4866 args[1] = HASH_KEY (h, i);
4867 args[2] = HASH_VALUE (h, i);
4868 Ffuncall (3, args);
4871 return Qnil;
4875 DEFUN ("define-hash-table-test", Fdefine_hash_table_test,
4876 Sdefine_hash_table_test, 3, 3, 0,
4877 doc: /* Define a new hash table test with name NAME, a symbol.
4879 In hash tables created with NAME specified as test, use TEST to
4880 compare keys, and HASH for computing hash codes of keys.
4882 TEST must be a function taking two arguments and returning non-nil if
4883 both arguments are the same. HASH must be a function taking one
4884 argument and return an integer that is the hash code of the argument.
4885 Hash code computation should use the whole value range of integers,
4886 including negative integers. */)
4887 (name, test, hash)
4888 Lisp_Object name, test, hash;
4890 return Fput (name, Qhash_table_test, list2 (test, hash));
4895 /************************************************************************
4897 ************************************************************************/
4899 #include "md5.h"
4901 DEFUN ("md5", Fmd5, Smd5, 1, 5, 0,
4902 doc: /* Return MD5 message digest of OBJECT, a buffer or string.
4904 A message digest is a cryptographic checksum of a document, and the
4905 algorithm to calculate it is defined in RFC 1321.
4907 The two optional arguments START and END are character positions
4908 specifying for which part of OBJECT the message digest should be
4909 computed. If nil or omitted, the digest is computed for the whole
4910 OBJECT.
4912 The MD5 message digest is computed from the result of encoding the
4913 text in a coding system, not directly from the internal Emacs form of
4914 the text. The optional fourth argument CODING-SYSTEM specifies which
4915 coding system to encode the text with. It should be the same coding
4916 system that you used or will use when actually writing the text into a
4917 file.
4919 If CODING-SYSTEM is nil or omitted, the default depends on OBJECT. If
4920 OBJECT is a buffer, the default for CODING-SYSTEM is whatever coding
4921 system would be chosen by default for writing this text into a file.
4923 If OBJECT is a string, the most preferred coding system (see the
4924 command `prefer-coding-system') is used.
4926 If NOERROR is non-nil, silently assume the `raw-text' coding if the
4927 guesswork fails. Normally, an error is signaled in such case. */)
4928 (object, start, end, coding_system, noerror)
4929 Lisp_Object object, start, end, coding_system, noerror;
4931 unsigned char digest[16];
4932 unsigned char value[33];
4933 int i;
4934 int size;
4935 int size_byte = 0;
4936 int start_char = 0, end_char = 0;
4937 int start_byte = 0, end_byte = 0;
4938 register int b, e;
4939 register struct buffer *bp;
4940 int temp;
4942 if (STRINGP (object))
4944 if (NILP (coding_system))
4946 /* Decide the coding-system to encode the data with. */
4948 if (STRING_MULTIBYTE (object))
4949 /* use default, we can't guess correct value */
4950 coding_system = preferred_coding_system ();
4951 else
4952 coding_system = Qraw_text;
4955 if (NILP (Fcoding_system_p (coding_system)))
4957 /* Invalid coding system. */
4959 if (!NILP (noerror))
4960 coding_system = Qraw_text;
4961 else
4962 xsignal1 (Qcoding_system_error, coding_system);
4965 if (STRING_MULTIBYTE (object))
4966 object = code_convert_string (object, coding_system, Qnil, 1, 0, 1);
4968 size = SCHARS (object);
4969 size_byte = SBYTES (object);
4971 if (!NILP (start))
4973 CHECK_NUMBER (start);
4975 start_char = XINT (start);
4977 if (start_char < 0)
4978 start_char += size;
4980 start_byte = string_char_to_byte (object, start_char);
4983 if (NILP (end))
4985 end_char = size;
4986 end_byte = size_byte;
4988 else
4990 CHECK_NUMBER (end);
4992 end_char = XINT (end);
4994 if (end_char < 0)
4995 end_char += size;
4997 end_byte = string_char_to_byte (object, end_char);
5000 if (!(0 <= start_char && start_char <= end_char && end_char <= size))
5001 args_out_of_range_3 (object, make_number (start_char),
5002 make_number (end_char));
5004 else
5006 struct buffer *prev = current_buffer;
5008 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
5010 CHECK_BUFFER (object);
5012 bp = XBUFFER (object);
5013 if (bp != current_buffer)
5014 set_buffer_internal (bp);
5016 if (NILP (start))
5017 b = BEGV;
5018 else
5020 CHECK_NUMBER_COERCE_MARKER (start);
5021 b = XINT (start);
5024 if (NILP (end))
5025 e = ZV;
5026 else
5028 CHECK_NUMBER_COERCE_MARKER (end);
5029 e = XINT (end);
5032 if (b > e)
5033 temp = b, b = e, e = temp;
5035 if (!(BEGV <= b && e <= ZV))
5036 args_out_of_range (start, end);
5038 if (NILP (coding_system))
5040 /* Decide the coding-system to encode the data with.
5041 See fileio.c:Fwrite-region */
5043 if (!NILP (Vcoding_system_for_write))
5044 coding_system = Vcoding_system_for_write;
5045 else
5047 int force_raw_text = 0;
5049 coding_system = XBUFFER (object)->buffer_file_coding_system;
5050 if (NILP (coding_system)
5051 || NILP (Flocal_variable_p (Qbuffer_file_coding_system, Qnil)))
5053 coding_system = Qnil;
5054 if (NILP (current_buffer->enable_multibyte_characters))
5055 force_raw_text = 1;
5058 if (NILP (coding_system) && !NILP (Fbuffer_file_name(object)))
5060 /* Check file-coding-system-alist. */
5061 Lisp_Object args[4], val;
5063 args[0] = Qwrite_region; args[1] = start; args[2] = end;
5064 args[3] = Fbuffer_file_name(object);
5065 val = Ffind_operation_coding_system (4, args);
5066 if (CONSP (val) && !NILP (XCDR (val)))
5067 coding_system = XCDR (val);
5070 if (NILP (coding_system)
5071 && !NILP (XBUFFER (object)->buffer_file_coding_system))
5073 /* If we still have not decided a coding system, use the
5074 default value of buffer-file-coding-system. */
5075 coding_system = XBUFFER (object)->buffer_file_coding_system;
5078 if (!force_raw_text
5079 && !NILP (Ffboundp (Vselect_safe_coding_system_function)))
5080 /* Confirm that VAL can surely encode the current region. */
5081 coding_system = call4 (Vselect_safe_coding_system_function,
5082 make_number (b), make_number (e),
5083 coding_system, Qnil);
5085 if (force_raw_text)
5086 coding_system = Qraw_text;
5089 if (NILP (Fcoding_system_p (coding_system)))
5091 /* Invalid coding system. */
5093 if (!NILP (noerror))
5094 coding_system = Qraw_text;
5095 else
5096 xsignal1 (Qcoding_system_error, coding_system);
5100 object = make_buffer_string (b, e, 0);
5101 if (prev != current_buffer)
5102 set_buffer_internal (prev);
5103 /* Discard the unwind protect for recovering the current
5104 buffer. */
5105 specpdl_ptr--;
5107 if (STRING_MULTIBYTE (object))
5108 object = code_convert_string (object, coding_system, Qnil, 1, 0, 0);
5111 md5_buffer (SDATA (object) + start_byte,
5112 SBYTES (object) - (size_byte - end_byte),
5113 digest);
5115 for (i = 0; i < 16; i++)
5116 sprintf (&value[2 * i], "%02x", digest[i]);
5117 value[32] = '\0';
5119 return make_string (value, 32);
5123 void
5124 syms_of_fns ()
5126 /* Hash table stuff. */
5127 Qhash_table_p = intern_c_string ("hash-table-p");
5128 staticpro (&Qhash_table_p);
5129 Qeq = intern_c_string ("eq");
5130 staticpro (&Qeq);
5131 Qeql = intern_c_string ("eql");
5132 staticpro (&Qeql);
5133 Qequal = intern_c_string ("equal");
5134 staticpro (&Qequal);
5135 QCtest = intern_c_string (":test");
5136 staticpro (&QCtest);
5137 QCsize = intern_c_string (":size");
5138 staticpro (&QCsize);
5139 QCrehash_size = intern_c_string (":rehash-size");
5140 staticpro (&QCrehash_size);
5141 QCrehash_threshold = intern_c_string (":rehash-threshold");
5142 staticpro (&QCrehash_threshold);
5143 QCweakness = intern_c_string (":weakness");
5144 staticpro (&QCweakness);
5145 Qkey = intern_c_string ("key");
5146 staticpro (&Qkey);
5147 Qvalue = intern_c_string ("value");
5148 staticpro (&Qvalue);
5149 Qhash_table_test = intern_c_string ("hash-table-test");
5150 staticpro (&Qhash_table_test);
5151 Qkey_or_value = intern_c_string ("key-or-value");
5152 staticpro (&Qkey_or_value);
5153 Qkey_and_value = intern_c_string ("key-and-value");
5154 staticpro (&Qkey_and_value);
5156 defsubr (&Ssxhash);
5157 defsubr (&Smake_hash_table);
5158 defsubr (&Scopy_hash_table);
5159 defsubr (&Shash_table_count);
5160 defsubr (&Shash_table_rehash_size);
5161 defsubr (&Shash_table_rehash_threshold);
5162 defsubr (&Shash_table_size);
5163 defsubr (&Shash_table_test);
5164 defsubr (&Shash_table_weakness);
5165 defsubr (&Shash_table_p);
5166 defsubr (&Sclrhash);
5167 defsubr (&Sgethash);
5168 defsubr (&Sputhash);
5169 defsubr (&Sremhash);
5170 defsubr (&Smaphash);
5171 defsubr (&Sdefine_hash_table_test);
5173 Qstring_lessp = intern_c_string ("string-lessp");
5174 staticpro (&Qstring_lessp);
5175 Qprovide = intern_c_string ("provide");
5176 staticpro (&Qprovide);
5177 Qrequire = intern_c_string ("require");
5178 staticpro (&Qrequire);
5179 Qyes_or_no_p_history = intern_c_string ("yes-or-no-p-history");
5180 staticpro (&Qyes_or_no_p_history);
5181 Qcursor_in_echo_area = intern_c_string ("cursor-in-echo-area");
5182 staticpro (&Qcursor_in_echo_area);
5183 Qwidget_type = intern_c_string ("widget-type");
5184 staticpro (&Qwidget_type);
5186 staticpro (&string_char_byte_cache_string);
5187 string_char_byte_cache_string = Qnil;
5189 require_nesting_list = Qnil;
5190 staticpro (&require_nesting_list);
5192 Fset (Qyes_or_no_p_history, Qnil);
5194 DEFVAR_LISP ("features", &Vfeatures,
5195 doc: /* A list of symbols which are the features of the executing Emacs.
5196 Used by `featurep' and `require', and altered by `provide'. */);
5197 Vfeatures = Fcons (intern_c_string ("emacs"), Qnil);
5198 Qsubfeatures = intern_c_string ("subfeatures");
5199 staticpro (&Qsubfeatures);
5201 #ifdef HAVE_LANGINFO_CODESET
5202 Qcodeset = intern_c_string ("codeset");
5203 staticpro (&Qcodeset);
5204 Qdays = intern_c_string ("days");
5205 staticpro (&Qdays);
5206 Qmonths = intern_c_string ("months");
5207 staticpro (&Qmonths);
5208 Qpaper = intern_c_string ("paper");
5209 staticpro (&Qpaper);
5210 #endif /* HAVE_LANGINFO_CODESET */
5212 DEFVAR_BOOL ("use-dialog-box", &use_dialog_box,
5213 doc: /* *Non-nil means mouse commands use dialog boxes to ask questions.
5214 This applies to `y-or-n-p' and `yes-or-no-p' questions asked by commands
5215 invoked by mouse clicks and mouse menu items.
5217 On some platforms, file selection dialogs are also enabled if this is
5218 non-nil. */);
5219 use_dialog_box = 1;
5221 DEFVAR_BOOL ("use-file-dialog", &use_file_dialog,
5222 doc: /* *Non-nil means mouse commands use a file dialog to ask for files.
5223 This applies to commands from menus and tool bar buttons even when
5224 they are initiated from the keyboard. If `use-dialog-box' is nil,
5225 that disables the use of a file dialog, regardless of the value of
5226 this variable. */);
5227 use_file_dialog = 1;
5229 defsubr (&Sidentity);
5230 defsubr (&Srandom);
5231 defsubr (&Slength);
5232 defsubr (&Ssafe_length);
5233 defsubr (&Sstring_bytes);
5234 defsubr (&Sstring_equal);
5235 defsubr (&Scompare_strings);
5236 defsubr (&Sstring_lessp);
5237 defsubr (&Sappend);
5238 defsubr (&Sconcat);
5239 defsubr (&Svconcat);
5240 defsubr (&Scopy_sequence);
5241 defsubr (&Sstring_make_multibyte);
5242 defsubr (&Sstring_make_unibyte);
5243 defsubr (&Sstring_as_multibyte);
5244 defsubr (&Sstring_as_unibyte);
5245 defsubr (&Sstring_to_multibyte);
5246 defsubr (&Sstring_to_unibyte);
5247 defsubr (&Scopy_alist);
5248 defsubr (&Ssubstring);
5249 defsubr (&Ssubstring_no_properties);
5250 defsubr (&Snthcdr);
5251 defsubr (&Snth);
5252 defsubr (&Selt);
5253 defsubr (&Smember);
5254 defsubr (&Smemq);
5255 defsubr (&Smemql);
5256 defsubr (&Sassq);
5257 defsubr (&Sassoc);
5258 defsubr (&Srassq);
5259 defsubr (&Srassoc);
5260 defsubr (&Sdelq);
5261 defsubr (&Sdelete);
5262 defsubr (&Snreverse);
5263 defsubr (&Sreverse);
5264 defsubr (&Ssort);
5265 defsubr (&Splist_get);
5266 defsubr (&Sget);
5267 defsubr (&Splist_put);
5268 defsubr (&Sput);
5269 defsubr (&Slax_plist_get);
5270 defsubr (&Slax_plist_put);
5271 defsubr (&Seql);
5272 defsubr (&Sequal);
5273 defsubr (&Sequal_including_properties);
5274 defsubr (&Sfillarray);
5275 defsubr (&Sclear_string);
5276 defsubr (&Snconc);
5277 defsubr (&Smapcar);
5278 defsubr (&Smapc);
5279 defsubr (&Smapconcat);
5280 defsubr (&Sy_or_n_p);
5281 defsubr (&Syes_or_no_p);
5282 defsubr (&Sload_average);
5283 defsubr (&Sfeaturep);
5284 defsubr (&Srequire);
5285 defsubr (&Sprovide);
5286 defsubr (&Splist_member);
5287 defsubr (&Swidget_put);
5288 defsubr (&Swidget_get);
5289 defsubr (&Swidget_apply);
5290 defsubr (&Sbase64_encode_region);
5291 defsubr (&Sbase64_decode_region);
5292 defsubr (&Sbase64_encode_string);
5293 defsubr (&Sbase64_decode_string);
5294 defsubr (&Smd5);
5295 defsubr (&Slocale_info);
5299 void
5300 init_fns ()
5304 /* arch-tag: 787f8219-5b74-46bd-8469-7e1cc475fa31
5305 (do not change this comment) */