(tex-print): Call shell-quote-argument on the file name.
[emacs.git] / src / fns.c
blobce8efbfbcd1cc8b6a8466beca939ee99e2d0f992
1 /* Random utility Lisp functions.
2 Copyright (C) 1985, 86, 87, 93, 94, 95, 97, 98, 99, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include <config.h>
24 #ifdef HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27 #include <time.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 "charset.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 #if defined (HAVE_MENUS) && defined (HAVE_X_WINDOWS)
46 #include "xterm.h"
47 #endif
49 #ifndef NULL
50 #define NULL (void *)0
51 #endif
53 /* Nonzero enables use of dialog boxes for questions
54 asked by mouse commands. */
55 int use_dialog_box;
57 extern int minibuffer_auto_raise;
58 extern Lisp_Object minibuf_window;
60 Lisp_Object Qstring_lessp, Qprovide, Qrequire;
61 Lisp_Object Qyes_or_no_p_history;
62 Lisp_Object Qcursor_in_echo_area;
63 Lisp_Object Qwidget_type;
65 extern Lisp_Object Qinput_method_function;
67 static int internal_equal ();
69 extern long get_random ();
70 extern void seed_random ();
72 #ifndef HAVE_UNISTD_H
73 extern long time ();
74 #endif
76 DEFUN ("identity", Fidentity, Sidentity, 1, 1, 0,
77 doc: /* Return the argument unchanged. */)
78 (arg)
79 Lisp_Object arg;
81 return arg;
84 DEFUN ("random", Frandom, Srandom, 0, 1, 0,
85 doc: /* Return a pseudo-random number.
86 All integers representable in Lisp are equally likely.
87 On most systems, this is 28 bits' worth.
88 With positive integer argument N, return random number in interval [0,N).
89 With argument t, set the random number seed from the current time and pid. */)
90 (n)
91 Lisp_Object n;
93 EMACS_INT val;
94 Lisp_Object lispy_val;
95 unsigned long denominator;
97 if (EQ (n, Qt))
98 seed_random (getpid () + time (NULL));
99 if (NATNUMP (n) && XFASTINT (n) != 0)
101 /* Try to take our random number from the higher bits of VAL,
102 not the lower, since (says Gentzel) the low bits of `random'
103 are less random than the higher ones. We do this by using the
104 quotient rather than the remainder. At the high end of the RNG
105 it's possible to get a quotient larger than n; discarding
106 these values eliminates the bias that would otherwise appear
107 when using a large n. */
108 denominator = ((unsigned long)1 << VALBITS) / XFASTINT (n);
110 val = get_random () / denominator;
111 while (val >= XFASTINT (n));
113 else
114 val = get_random ();
115 XSETINT (lispy_val, val);
116 return lispy_val;
119 /* Random data-structure functions */
121 DEFUN ("length", Flength, Slength, 1, 1, 0,
122 doc: /* Return the length of vector, list or string SEQUENCE.
123 A byte-code function object is also allowed.
124 If the string contains multibyte characters, this is not necessarily
125 the number of bytes in the string; it is the number of characters.
126 To get the number of bytes, use `string-bytes'. */)
127 (sequence)
128 register Lisp_Object sequence;
130 register Lisp_Object val;
131 register int i;
133 retry:
134 if (STRINGP (sequence))
135 XSETFASTINT (val, SCHARS (sequence));
136 else if (VECTORP (sequence))
137 XSETFASTINT (val, XVECTOR (sequence)->size);
138 else if (CHAR_TABLE_P (sequence))
139 XSETFASTINT (val, MAX_CHAR);
140 else if (BOOL_VECTOR_P (sequence))
141 XSETFASTINT (val, XBOOL_VECTOR (sequence)->size);
142 else if (COMPILEDP (sequence))
143 XSETFASTINT (val, XVECTOR (sequence)->size & PSEUDOVECTOR_SIZE_MASK);
144 else if (CONSP (sequence))
146 i = 0;
147 while (CONSP (sequence))
149 sequence = XCDR (sequence);
150 ++i;
152 if (!CONSP (sequence))
153 break;
155 sequence = XCDR (sequence);
156 ++i;
157 QUIT;
160 if (!NILP (sequence))
161 wrong_type_argument (Qlistp, sequence);
163 val = make_number (i);
165 else if (NILP (sequence))
166 XSETFASTINT (val, 0);
167 else
169 sequence = wrong_type_argument (Qsequencep, sequence);
170 goto retry;
172 return val;
175 /* This does not check for quits. That is safe
176 since it must terminate. */
178 DEFUN ("safe-length", Fsafe_length, Ssafe_length, 1, 1, 0,
179 doc: /* Return the length of a list, but avoid error or infinite loop.
180 This function never gets an error. If LIST is not really a list,
181 it returns 0. If LIST is circular, it returns a finite value
182 which is at least the number of distinct elements. */)
183 (list)
184 Lisp_Object list;
186 Lisp_Object tail, halftail, length;
187 int len = 0;
189 /* halftail is used to detect circular lists. */
190 halftail = list;
191 for (tail = list; CONSP (tail); tail = XCDR (tail))
193 if (EQ (tail, halftail) && len != 0)
194 break;
195 len++;
196 if ((len & 1) == 0)
197 halftail = XCDR (halftail);
200 XSETINT (length, len);
201 return length;
204 DEFUN ("string-bytes", Fstring_bytes, Sstring_bytes, 1, 1, 0,
205 doc: /* Return the number of bytes in STRING.
206 If STRING is a multibyte string, this is greater than the length of STRING. */)
207 (string)
208 Lisp_Object string;
210 CHECK_STRING (string);
211 return make_number (SBYTES (string));
214 DEFUN ("string-equal", Fstring_equal, Sstring_equal, 2, 2, 0,
215 doc: /* Return t if two strings have identical contents.
216 Case is significant, but text properties are ignored.
217 Symbols are also allowed; their print names are used instead. */)
218 (s1, s2)
219 register Lisp_Object s1, s2;
221 if (SYMBOLP (s1))
222 s1 = SYMBOL_NAME (s1);
223 if (SYMBOLP (s2))
224 s2 = SYMBOL_NAME (s2);
225 CHECK_STRING (s1);
226 CHECK_STRING (s2);
228 if (SCHARS (s1) != SCHARS (s2)
229 || SBYTES (s1) != SBYTES (s2)
230 || bcmp (SDATA (s1), SDATA (s2), SBYTES (s1)))
231 return Qnil;
232 return Qt;
235 DEFUN ("compare-strings", Fcompare_strings,
236 Scompare_strings, 6, 7, 0,
237 doc: /* Compare the contents of two strings, converting to multibyte if needed.
238 In string STR1, skip the first START1 characters and stop at END1.
239 In string STR2, skip the first START2 characters and stop at END2.
240 END1 and END2 default to the full lengths of the respective strings.
242 Case is significant in this comparison if IGNORE-CASE is nil.
243 Unibyte strings are converted to multibyte for comparison.
245 The value is t if the strings (or specified portions) match.
246 If string STR1 is less, the value is a negative number N;
247 - 1 - N is the number of characters that match at the beginning.
248 If string STR1 is greater, the value is a positive number N;
249 N - 1 is the number of characters that match at the beginning. */)
250 (str1, start1, end1, str2, start2, end2, ignore_case)
251 Lisp_Object str1, start1, end1, start2, str2, end2, ignore_case;
253 register int end1_char, end2_char;
254 register int i1, i1_byte, i2, i2_byte;
256 CHECK_STRING (str1);
257 CHECK_STRING (str2);
258 if (NILP (start1))
259 start1 = make_number (0);
260 if (NILP (start2))
261 start2 = make_number (0);
262 CHECK_NATNUM (start1);
263 CHECK_NATNUM (start2);
264 if (! NILP (end1))
265 CHECK_NATNUM (end1);
266 if (! NILP (end2))
267 CHECK_NATNUM (end2);
269 i1 = XINT (start1);
270 i2 = XINT (start2);
272 i1_byte = string_char_to_byte (str1, i1);
273 i2_byte = string_char_to_byte (str2, i2);
275 end1_char = SCHARS (str1);
276 if (! NILP (end1) && end1_char > XINT (end1))
277 end1_char = XINT (end1);
279 end2_char = SCHARS (str2);
280 if (! NILP (end2) && end2_char > XINT (end2))
281 end2_char = XINT (end2);
283 while (i1 < end1_char && i2 < end2_char)
285 /* When we find a mismatch, we must compare the
286 characters, not just the bytes. */
287 int c1, c2;
289 if (STRING_MULTIBYTE (str1))
290 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c1, str1, i1, i1_byte);
291 else
293 c1 = SREF (str1, i1++);
294 c1 = unibyte_char_to_multibyte (c1);
297 if (STRING_MULTIBYTE (str2))
298 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c2, str2, i2, i2_byte);
299 else
301 c2 = SREF (str2, i2++);
302 c2 = unibyte_char_to_multibyte (c2);
305 if (c1 == c2)
306 continue;
308 if (! NILP (ignore_case))
310 Lisp_Object tem;
312 tem = Fupcase (make_number (c1));
313 c1 = XINT (tem);
314 tem = Fupcase (make_number (c2));
315 c2 = XINT (tem);
318 if (c1 == c2)
319 continue;
321 /* Note that I1 has already been incremented
322 past the character that we are comparing;
323 hence we don't add or subtract 1 here. */
324 if (c1 < c2)
325 return make_number (- i1 + XINT (start1));
326 else
327 return make_number (i1 - XINT (start1));
330 if (i1 < end1_char)
331 return make_number (i1 - XINT (start1) + 1);
332 if (i2 < end2_char)
333 return make_number (- i1 + XINT (start1) - 1);
335 return Qt;
338 DEFUN ("string-lessp", Fstring_lessp, Sstring_lessp, 2, 2, 0,
339 doc: /* Return t if first arg string is less than second in lexicographic order.
340 Case is significant.
341 Symbols are also allowed; their print names are used instead. */)
342 (s1, s2)
343 register Lisp_Object s1, s2;
345 register int end;
346 register int i1, i1_byte, i2, i2_byte;
348 if (SYMBOLP (s1))
349 s1 = SYMBOL_NAME (s1);
350 if (SYMBOLP (s2))
351 s2 = SYMBOL_NAME (s2);
352 CHECK_STRING (s1);
353 CHECK_STRING (s2);
355 i1 = i1_byte = i2 = i2_byte = 0;
357 end = SCHARS (s1);
358 if (end > SCHARS (s2))
359 end = SCHARS (s2);
361 while (i1 < end)
363 /* When we find a mismatch, we must compare the
364 characters, not just the bytes. */
365 int c1, c2;
367 FETCH_STRING_CHAR_ADVANCE (c1, s1, i1, i1_byte);
368 FETCH_STRING_CHAR_ADVANCE (c2, s2, i2, i2_byte);
370 if (c1 != c2)
371 return c1 < c2 ? Qt : Qnil;
373 return i1 < SCHARS (s2) ? Qt : Qnil;
376 static Lisp_Object concat ();
378 /* ARGSUSED */
379 Lisp_Object
380 concat2 (s1, s2)
381 Lisp_Object s1, s2;
383 #ifdef NO_ARG_ARRAY
384 Lisp_Object args[2];
385 args[0] = s1;
386 args[1] = s2;
387 return concat (2, args, Lisp_String, 0);
388 #else
389 return concat (2, &s1, Lisp_String, 0);
390 #endif /* NO_ARG_ARRAY */
393 /* ARGSUSED */
394 Lisp_Object
395 concat3 (s1, s2, s3)
396 Lisp_Object s1, s2, s3;
398 #ifdef NO_ARG_ARRAY
399 Lisp_Object args[3];
400 args[0] = s1;
401 args[1] = s2;
402 args[2] = s3;
403 return concat (3, args, Lisp_String, 0);
404 #else
405 return concat (3, &s1, Lisp_String, 0);
406 #endif /* NO_ARG_ARRAY */
409 DEFUN ("append", Fappend, Sappend, 0, MANY, 0,
410 doc: /* Concatenate all the arguments and make the result a list.
411 The result is a list whose elements are the elements of all the arguments.
412 Each argument may be a list, vector or string.
413 The last argument is not copied, just used as the tail of the new list.
414 usage: (append &rest SEQUENCES) */)
415 (nargs, args)
416 int nargs;
417 Lisp_Object *args;
419 return concat (nargs, args, Lisp_Cons, 1);
422 DEFUN ("concat", Fconcat, Sconcat, 0, MANY, 0,
423 doc: /* Concatenate all the arguments and make the result a string.
424 The result is a string whose elements are the elements of all the arguments.
425 Each argument may be a string or a list or vector of characters (integers).
426 usage: (concat &rest SEQUENCES) */)
427 (nargs, args)
428 int nargs;
429 Lisp_Object *args;
431 return concat (nargs, args, Lisp_String, 0);
434 DEFUN ("vconcat", Fvconcat, Svconcat, 0, MANY, 0,
435 doc: /* Concatenate all the arguments and make the result a vector.
436 The result is a vector whose elements are the elements of all the arguments.
437 Each argument may be a list, vector or string.
438 usage: (vconcat &rest SEQUENCES) */)
439 (nargs, args)
440 int nargs;
441 Lisp_Object *args;
443 return concat (nargs, args, Lisp_Vectorlike, 0);
446 /* Return a copy of a sub char table ARG. The elements except for a
447 nested sub char table are not copied. */
448 static Lisp_Object
449 copy_sub_char_table (arg)
450 Lisp_Object arg;
452 Lisp_Object copy = make_sub_char_table (XCHAR_TABLE (arg)->defalt);
453 int i;
455 /* Copy all the contents. */
456 bcopy (XCHAR_TABLE (arg)->contents, XCHAR_TABLE (copy)->contents,
457 SUB_CHAR_TABLE_ORDINARY_SLOTS * sizeof (Lisp_Object));
458 /* Recursively copy any sub char-tables in the ordinary slots. */
459 for (i = 32; i < SUB_CHAR_TABLE_ORDINARY_SLOTS; i++)
460 if (SUB_CHAR_TABLE_P (XCHAR_TABLE (arg)->contents[i]))
461 XCHAR_TABLE (copy)->contents[i]
462 = copy_sub_char_table (XCHAR_TABLE (copy)->contents[i]);
464 return copy;
468 DEFUN ("copy-sequence", Fcopy_sequence, Scopy_sequence, 1, 1, 0,
469 doc: /* Return a copy of a list, vector, string or char-table.
470 The elements of a list or vector are not copied; they are shared
471 with the original. */)
472 (arg)
473 Lisp_Object arg;
475 if (NILP (arg)) return arg;
477 if (CHAR_TABLE_P (arg))
479 int i;
480 Lisp_Object copy;
482 copy = Fmake_char_table (XCHAR_TABLE (arg)->purpose, Qnil);
483 /* Copy all the slots, including the extra ones. */
484 bcopy (XVECTOR (arg)->contents, XVECTOR (copy)->contents,
485 ((XCHAR_TABLE (arg)->size & PSEUDOVECTOR_SIZE_MASK)
486 * sizeof (Lisp_Object)));
488 /* Recursively copy any sub char tables in the ordinary slots
489 for multibyte characters. */
490 for (i = CHAR_TABLE_SINGLE_BYTE_SLOTS;
491 i < CHAR_TABLE_ORDINARY_SLOTS; i++)
492 if (SUB_CHAR_TABLE_P (XCHAR_TABLE (arg)->contents[i]))
493 XCHAR_TABLE (copy)->contents[i]
494 = copy_sub_char_table (XCHAR_TABLE (copy)->contents[i]);
496 return copy;
499 if (BOOL_VECTOR_P (arg))
501 Lisp_Object val;
502 int size_in_chars
503 = (XBOOL_VECTOR (arg)->size + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
505 val = Fmake_bool_vector (Flength (arg), Qnil);
506 bcopy (XBOOL_VECTOR (arg)->data, XBOOL_VECTOR (val)->data,
507 size_in_chars);
508 return val;
511 if (!CONSP (arg) && !VECTORP (arg) && !STRINGP (arg))
512 arg = wrong_type_argument (Qsequencep, arg);
513 return concat (1, &arg, CONSP (arg) ? Lisp_Cons : XTYPE (arg), 0);
516 /* In string STR of length LEN, see if bytes before STR[I] combine
517 with bytes after STR[I] to form a single character. If so, return
518 the number of bytes after STR[I] which combine in this way.
519 Otherwize, return 0. */
521 static int
522 count_combining (str, len, i)
523 unsigned char *str;
524 int len, i;
526 int j = i - 1, bytes;
528 if (i == 0 || i == len || CHAR_HEAD_P (str[i]))
529 return 0;
530 while (j >= 0 && !CHAR_HEAD_P (str[j])) j--;
531 if (j < 0 || ! BASE_LEADING_CODE_P (str[j]))
532 return 0;
533 PARSE_MULTIBYTE_SEQ (str + j, len - j, bytes);
534 return (bytes <= i - j ? 0 : bytes - (i - j));
537 /* This structure holds information of an argument of `concat' that is
538 a string and has text properties to be copied. */
539 struct textprop_rec
541 int argnum; /* refer to ARGS (arguments of `concat') */
542 int from; /* refer to ARGS[argnum] (argument string) */
543 int to; /* refer to VAL (the target string) */
546 static Lisp_Object
547 concat (nargs, args, target_type, last_special)
548 int nargs;
549 Lisp_Object *args;
550 enum Lisp_Type target_type;
551 int last_special;
553 Lisp_Object val;
554 register Lisp_Object tail;
555 register Lisp_Object this;
556 int toindex;
557 int toindex_byte = 0;
558 register int result_len;
559 register int result_len_byte;
560 register int argnum;
561 Lisp_Object last_tail;
562 Lisp_Object prev;
563 int some_multibyte;
564 /* When we make a multibyte string, we can't copy text properties
565 while concatinating each string because the length of resulting
566 string can't be decided until we finish the whole concatination.
567 So, we record strings that have text properties to be copied
568 here, and copy the text properties after the concatination. */
569 struct textprop_rec *textprops = NULL;
570 /* Number of elments in textprops. */
571 int num_textprops = 0;
573 tail = Qnil;
575 /* In append, the last arg isn't treated like the others */
576 if (last_special && nargs > 0)
578 nargs--;
579 last_tail = args[nargs];
581 else
582 last_tail = Qnil;
584 /* Canonicalize each argument. */
585 for (argnum = 0; argnum < nargs; argnum++)
587 this = args[argnum];
588 if (!(CONSP (this) || NILP (this) || VECTORP (this) || STRINGP (this)
589 || COMPILEDP (this) || BOOL_VECTOR_P (this)))
591 args[argnum] = wrong_type_argument (Qsequencep, this);
595 /* Compute total length in chars of arguments in RESULT_LEN.
596 If desired output is a string, also compute length in bytes
597 in RESULT_LEN_BYTE, and determine in SOME_MULTIBYTE
598 whether the result should be a multibyte string. */
599 result_len_byte = 0;
600 result_len = 0;
601 some_multibyte = 0;
602 for (argnum = 0; argnum < nargs; argnum++)
604 int len;
605 this = args[argnum];
606 len = XFASTINT (Flength (this));
607 if (target_type == Lisp_String)
609 /* We must count the number of bytes needed in the string
610 as well as the number of characters. */
611 int i;
612 Lisp_Object ch;
613 int this_len_byte;
615 if (VECTORP (this))
616 for (i = 0; i < len; i++)
618 ch = XVECTOR (this)->contents[i];
619 if (! INTEGERP (ch))
620 wrong_type_argument (Qintegerp, ch);
621 this_len_byte = CHAR_BYTES (XINT (ch));
622 result_len_byte += this_len_byte;
623 if (!SINGLE_BYTE_CHAR_P (XINT (ch)))
624 some_multibyte = 1;
626 else if (BOOL_VECTOR_P (this) && XBOOL_VECTOR (this)->size > 0)
627 wrong_type_argument (Qintegerp, Faref (this, make_number (0)));
628 else if (CONSP (this))
629 for (; CONSP (this); this = XCDR (this))
631 ch = XCAR (this);
632 if (! INTEGERP (ch))
633 wrong_type_argument (Qintegerp, ch);
634 this_len_byte = CHAR_BYTES (XINT (ch));
635 result_len_byte += this_len_byte;
636 if (!SINGLE_BYTE_CHAR_P (XINT (ch)))
637 some_multibyte = 1;
639 else if (STRINGP (this))
641 if (STRING_MULTIBYTE (this))
643 some_multibyte = 1;
644 result_len_byte += SBYTES (this);
646 else
647 result_len_byte += count_size_as_multibyte (SDATA (this),
648 SCHARS (this));
652 result_len += len;
655 if (! some_multibyte)
656 result_len_byte = result_len;
658 /* Create the output object. */
659 if (target_type == Lisp_Cons)
660 val = Fmake_list (make_number (result_len), Qnil);
661 else if (target_type == Lisp_Vectorlike)
662 val = Fmake_vector (make_number (result_len), Qnil);
663 else if (some_multibyte)
664 val = make_uninit_multibyte_string (result_len, result_len_byte);
665 else
666 val = make_uninit_string (result_len);
668 /* In `append', if all but last arg are nil, return last arg. */
669 if (target_type == Lisp_Cons && EQ (val, Qnil))
670 return last_tail;
672 /* Copy the contents of the args into the result. */
673 if (CONSP (val))
674 tail = val, toindex = -1; /* -1 in toindex is flag we are making a list */
675 else
676 toindex = 0, toindex_byte = 0;
678 prev = Qnil;
679 if (STRINGP (val))
680 textprops
681 = (struct textprop_rec *) alloca (sizeof (struct textprop_rec) * nargs);
683 for (argnum = 0; argnum < nargs; argnum++)
685 Lisp_Object thislen;
686 int thisleni = 0;
687 register unsigned int thisindex = 0;
688 register unsigned int thisindex_byte = 0;
690 this = args[argnum];
691 if (!CONSP (this))
692 thislen = Flength (this), thisleni = XINT (thislen);
694 /* Between strings of the same kind, copy fast. */
695 if (STRINGP (this) && STRINGP (val)
696 && STRING_MULTIBYTE (this) == some_multibyte)
698 int thislen_byte = SBYTES (this);
699 int combined;
701 bcopy (SDATA (this), SDATA (val) + toindex_byte,
702 SBYTES (this));
703 combined = (some_multibyte && toindex_byte > 0
704 ? count_combining (SDATA (val),
705 toindex_byte + thislen_byte,
706 toindex_byte)
707 : 0);
708 if (! NULL_INTERVAL_P (STRING_INTERVALS (this)))
710 textprops[num_textprops].argnum = argnum;
711 /* We ignore text properties on characters being combined. */
712 textprops[num_textprops].from = combined;
713 textprops[num_textprops++].to = toindex;
715 toindex_byte += thislen_byte;
716 toindex += thisleni - combined;
717 STRING_SET_CHARS (val, SCHARS (val) - combined);
719 /* Copy a single-byte string to a multibyte string. */
720 else if (STRINGP (this) && STRINGP (val))
722 if (! NULL_INTERVAL_P (STRING_INTERVALS (this)))
724 textprops[num_textprops].argnum = argnum;
725 textprops[num_textprops].from = 0;
726 textprops[num_textprops++].to = toindex;
728 toindex_byte += copy_text (SDATA (this),
729 SDATA (val) + toindex_byte,
730 SCHARS (this), 0, 1);
731 toindex += thisleni;
733 else
734 /* Copy element by element. */
735 while (1)
737 register Lisp_Object elt;
739 /* Fetch next element of `this' arg into `elt', or break if
740 `this' is exhausted. */
741 if (NILP (this)) break;
742 if (CONSP (this))
743 elt = XCAR (this), this = XCDR (this);
744 else if (thisindex >= thisleni)
745 break;
746 else if (STRINGP (this))
748 int c;
749 if (STRING_MULTIBYTE (this))
751 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, this,
752 thisindex,
753 thisindex_byte);
754 XSETFASTINT (elt, c);
756 else
758 XSETFASTINT (elt, SREF (this, thisindex++));
759 if (some_multibyte
760 && (XINT (elt) >= 0240
761 || (XINT (elt) >= 0200
762 && ! NILP (Vnonascii_translation_table)))
763 && XINT (elt) < 0400)
765 c = unibyte_char_to_multibyte (XINT (elt));
766 XSETINT (elt, c);
770 else if (BOOL_VECTOR_P (this))
772 int byte;
773 byte = XBOOL_VECTOR (this)->data[thisindex / BITS_PER_CHAR];
774 if (byte & (1 << (thisindex % BITS_PER_CHAR)))
775 elt = Qt;
776 else
777 elt = Qnil;
778 thisindex++;
780 else
781 elt = XVECTOR (this)->contents[thisindex++];
783 /* Store this element into the result. */
784 if (toindex < 0)
786 XSETCAR (tail, elt);
787 prev = tail;
788 tail = XCDR (tail);
790 else if (VECTORP (val))
791 XVECTOR (val)->contents[toindex++] = elt;
792 else
794 CHECK_NUMBER (elt);
795 if (SINGLE_BYTE_CHAR_P (XINT (elt)))
797 if (some_multibyte)
798 toindex_byte
799 += CHAR_STRING (XINT (elt),
800 SDATA (val) + toindex_byte);
801 else
802 SSET (val, toindex_byte++, XINT (elt));
803 if (some_multibyte
804 && toindex_byte > 0
805 && count_combining (SDATA (val),
806 toindex_byte, toindex_byte - 1))
807 STRING_SET_CHARS (val, SCHARS (val) - 1);
808 else
809 toindex++;
811 else
812 /* If we have any multibyte characters,
813 we already decided to make a multibyte string. */
815 int c = XINT (elt);
816 /* P exists as a variable
817 to avoid a bug on the Masscomp C compiler. */
818 unsigned char *p = SDATA (val) + toindex_byte;
820 toindex_byte += CHAR_STRING (c, p);
821 toindex++;
826 if (!NILP (prev))
827 XSETCDR (prev, last_tail);
829 if (num_textprops > 0)
831 Lisp_Object props;
832 int last_to_end = -1;
834 for (argnum = 0; argnum < num_textprops; argnum++)
836 this = args[textprops[argnum].argnum];
837 props = text_property_list (this,
838 make_number (0),
839 make_number (SCHARS (this)),
840 Qnil);
841 /* If successive arguments have properites, be sure that the
842 value of `composition' property be the copy. */
843 if (last_to_end == textprops[argnum].to)
844 make_composition_value_copy (props);
845 add_text_properties_from_list (val, props,
846 make_number (textprops[argnum].to));
847 last_to_end = textprops[argnum].to + SCHARS (this);
850 return val;
853 static Lisp_Object string_char_byte_cache_string;
854 static int string_char_byte_cache_charpos;
855 static int string_char_byte_cache_bytepos;
857 void
858 clear_string_char_byte_cache ()
860 string_char_byte_cache_string = Qnil;
863 /* Return the character index corresponding to CHAR_INDEX in STRING. */
866 string_char_to_byte (string, char_index)
867 Lisp_Object string;
868 int char_index;
870 int i, i_byte;
871 int best_below, best_below_byte;
872 int best_above, best_above_byte;
874 if (! STRING_MULTIBYTE (string))
875 return char_index;
877 best_below = best_below_byte = 0;
878 best_above = SCHARS (string);
879 best_above_byte = SBYTES (string);
881 if (EQ (string, string_char_byte_cache_string))
883 if (string_char_byte_cache_charpos < char_index)
885 best_below = string_char_byte_cache_charpos;
886 best_below_byte = string_char_byte_cache_bytepos;
888 else
890 best_above = string_char_byte_cache_charpos;
891 best_above_byte = string_char_byte_cache_bytepos;
895 if (char_index - best_below < best_above - char_index)
897 while (best_below < char_index)
899 int c;
900 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, string,
901 best_below, best_below_byte);
903 i = best_below;
904 i_byte = best_below_byte;
906 else
908 while (best_above > char_index)
910 unsigned char *pend = SDATA (string) + best_above_byte;
911 unsigned char *pbeg = pend - best_above_byte;
912 unsigned char *p = pend - 1;
913 int bytes;
915 while (p > pbeg && !CHAR_HEAD_P (*p)) p--;
916 PARSE_MULTIBYTE_SEQ (p, pend - p, bytes);
917 if (bytes == pend - p)
918 best_above_byte -= bytes;
919 else if (bytes > pend - p)
920 best_above_byte -= (pend - p);
921 else
922 best_above_byte--;
923 best_above--;
925 i = best_above;
926 i_byte = best_above_byte;
929 string_char_byte_cache_bytepos = i_byte;
930 string_char_byte_cache_charpos = i;
931 string_char_byte_cache_string = string;
933 return i_byte;
936 /* Return the character index corresponding to BYTE_INDEX in STRING. */
939 string_byte_to_char (string, byte_index)
940 Lisp_Object string;
941 int byte_index;
943 int i, i_byte;
944 int best_below, best_below_byte;
945 int best_above, best_above_byte;
947 if (! STRING_MULTIBYTE (string))
948 return byte_index;
950 best_below = best_below_byte = 0;
951 best_above = SCHARS (string);
952 best_above_byte = SBYTES (string);
954 if (EQ (string, string_char_byte_cache_string))
956 if (string_char_byte_cache_bytepos < byte_index)
958 best_below = string_char_byte_cache_charpos;
959 best_below_byte = string_char_byte_cache_bytepos;
961 else
963 best_above = string_char_byte_cache_charpos;
964 best_above_byte = string_char_byte_cache_bytepos;
968 if (byte_index - best_below_byte < best_above_byte - byte_index)
970 while (best_below_byte < byte_index)
972 int c;
973 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, string,
974 best_below, best_below_byte);
976 i = best_below;
977 i_byte = best_below_byte;
979 else
981 while (best_above_byte > byte_index)
983 unsigned char *pend = SDATA (string) + best_above_byte;
984 unsigned char *pbeg = pend - best_above_byte;
985 unsigned char *p = pend - 1;
986 int bytes;
988 while (p > pbeg && !CHAR_HEAD_P (*p)) p--;
989 PARSE_MULTIBYTE_SEQ (p, pend - p, bytes);
990 if (bytes == pend - p)
991 best_above_byte -= bytes;
992 else if (bytes > pend - p)
993 best_above_byte -= (pend - p);
994 else
995 best_above_byte--;
996 best_above--;
998 i = best_above;
999 i_byte = best_above_byte;
1002 string_char_byte_cache_bytepos = i_byte;
1003 string_char_byte_cache_charpos = i;
1004 string_char_byte_cache_string = string;
1006 return i;
1009 /* Convert STRING to a multibyte string.
1010 Single-byte characters 0240 through 0377 are converted
1011 by adding nonascii_insert_offset to each. */
1013 Lisp_Object
1014 string_make_multibyte (string)
1015 Lisp_Object string;
1017 unsigned char *buf;
1018 int nbytes;
1020 if (STRING_MULTIBYTE (string))
1021 return string;
1023 nbytes = count_size_as_multibyte (SDATA (string),
1024 SCHARS (string));
1025 /* If all the chars are ASCII, they won't need any more bytes
1026 once converted. In that case, we can return STRING itself. */
1027 if (nbytes == SBYTES (string))
1028 return string;
1030 buf = (unsigned char *) alloca (nbytes);
1031 copy_text (SDATA (string), buf, SBYTES (string),
1032 0, 1);
1034 return make_multibyte_string (buf, SCHARS (string), nbytes);
1037 /* Convert STRING to a single-byte string. */
1039 Lisp_Object
1040 string_make_unibyte (string)
1041 Lisp_Object string;
1043 unsigned char *buf;
1045 if (! STRING_MULTIBYTE (string))
1046 return string;
1048 buf = (unsigned char *) alloca (SCHARS (string));
1050 copy_text (SDATA (string), buf, SBYTES (string),
1051 1, 0);
1053 return make_unibyte_string (buf, SCHARS (string));
1056 DEFUN ("string-make-multibyte", Fstring_make_multibyte, Sstring_make_multibyte,
1057 1, 1, 0,
1058 doc: /* Return the multibyte equivalent of STRING.
1059 The function `unibyte-char-to-multibyte' is used to convert
1060 each unibyte character to a multibyte character. */)
1061 (string)
1062 Lisp_Object string;
1064 CHECK_STRING (string);
1066 return string_make_multibyte (string);
1069 DEFUN ("string-make-unibyte", Fstring_make_unibyte, Sstring_make_unibyte,
1070 1, 1, 0,
1071 doc: /* Return the unibyte equivalent of STRING.
1072 Multibyte character codes are converted to unibyte according to
1073 `nonascii-translation-table' or, if that is nil, `nonascii-insert-offset'.
1074 If the lookup in the translation table fails, this function takes just
1075 the low 8 bits of each character. */)
1076 (string)
1077 Lisp_Object string;
1079 CHECK_STRING (string);
1081 return string_make_unibyte (string);
1084 DEFUN ("string-as-unibyte", Fstring_as_unibyte, Sstring_as_unibyte,
1085 1, 1, 0,
1086 doc: /* Return a unibyte string with the same individual bytes as STRING.
1087 If STRING is unibyte, the result is STRING itself.
1088 Otherwise it is a newly created string, with no text properties.
1089 If STRING is multibyte and contains a character of charset
1090 `eight-bit-control' or `eight-bit-graphic', it is converted to the
1091 corresponding single byte. */)
1092 (string)
1093 Lisp_Object string;
1095 CHECK_STRING (string);
1097 if (STRING_MULTIBYTE (string))
1099 int bytes = SBYTES (string);
1100 unsigned char *str = (unsigned char *) xmalloc (bytes);
1102 bcopy (SDATA (string), str, bytes);
1103 bytes = str_as_unibyte (str, bytes);
1104 string = make_unibyte_string (str, bytes);
1105 xfree (str);
1107 return string;
1110 DEFUN ("string-as-multibyte", Fstring_as_multibyte, Sstring_as_multibyte,
1111 1, 1, 0,
1112 doc: /* Return a multibyte string with the same individual bytes as STRING.
1113 If STRING is multibyte, the result is STRING itself.
1114 Otherwise it is a newly created string, with no text properties.
1115 If STRING is unibyte and contains an individual 8-bit byte (i.e. not
1116 part of a multibyte form), it is converted to the corresponding
1117 multibyte character of charset `eight-bit-control' or `eight-bit-graphic'. */)
1118 (string)
1119 Lisp_Object string;
1121 CHECK_STRING (string);
1123 if (! STRING_MULTIBYTE (string))
1125 Lisp_Object new_string;
1126 int nchars, nbytes;
1128 parse_str_as_multibyte (SDATA (string),
1129 SBYTES (string),
1130 &nchars, &nbytes);
1131 new_string = make_uninit_multibyte_string (nchars, nbytes);
1132 bcopy (SDATA (string), SDATA (new_string),
1133 SBYTES (string));
1134 if (nbytes != SBYTES (string))
1135 str_as_multibyte (SDATA (new_string), nbytes,
1136 SBYTES (string), NULL);
1137 string = new_string;
1138 STRING_SET_INTERVALS (string, NULL_INTERVAL);
1140 return string;
1143 DEFUN ("copy-alist", Fcopy_alist, Scopy_alist, 1, 1, 0,
1144 doc: /* Return a copy of ALIST.
1145 This is an alist which represents the same mapping from objects to objects,
1146 but does not share the alist structure with ALIST.
1147 The objects mapped (cars and cdrs of elements of the alist)
1148 are shared, however.
1149 Elements of ALIST that are not conses are also shared. */)
1150 (alist)
1151 Lisp_Object alist;
1153 register Lisp_Object tem;
1155 CHECK_LIST (alist);
1156 if (NILP (alist))
1157 return alist;
1158 alist = concat (1, &alist, Lisp_Cons, 0);
1159 for (tem = alist; CONSP (tem); tem = XCDR (tem))
1161 register Lisp_Object car;
1162 car = XCAR (tem);
1164 if (CONSP (car))
1165 XSETCAR (tem, Fcons (XCAR (car), XCDR (car)));
1167 return alist;
1170 DEFUN ("substring", Fsubstring, Ssubstring, 2, 3, 0,
1171 doc: /* Return a substring of STRING, starting at index FROM and ending before TO.
1172 TO may be nil or omitted; then the substring runs to the end of STRING.
1173 If FROM or TO is negative, it counts from the end.
1175 This function allows vectors as well as strings. */)
1176 (string, from, to)
1177 Lisp_Object string;
1178 register Lisp_Object from, to;
1180 Lisp_Object res;
1181 int size;
1182 int size_byte = 0;
1183 int from_char, to_char;
1184 int from_byte = 0, to_byte = 0;
1186 if (! (STRINGP (string) || VECTORP (string)))
1187 wrong_type_argument (Qarrayp, string);
1189 CHECK_NUMBER (from);
1191 if (STRINGP (string))
1193 size = SCHARS (string);
1194 size_byte = SBYTES (string);
1196 else
1197 size = XVECTOR (string)->size;
1199 if (NILP (to))
1201 to_char = size;
1202 to_byte = size_byte;
1204 else
1206 CHECK_NUMBER (to);
1208 to_char = XINT (to);
1209 if (to_char < 0)
1210 to_char += size;
1212 if (STRINGP (string))
1213 to_byte = string_char_to_byte (string, to_char);
1216 from_char = XINT (from);
1217 if (from_char < 0)
1218 from_char += size;
1219 if (STRINGP (string))
1220 from_byte = string_char_to_byte (string, from_char);
1222 if (!(0 <= from_char && from_char <= to_char && to_char <= size))
1223 args_out_of_range_3 (string, make_number (from_char),
1224 make_number (to_char));
1226 if (STRINGP (string))
1228 res = make_specified_string (SDATA (string) + from_byte,
1229 to_char - from_char, to_byte - from_byte,
1230 STRING_MULTIBYTE (string));
1231 copy_text_properties (make_number (from_char), make_number (to_char),
1232 string, make_number (0), res, Qnil);
1234 else
1235 res = Fvector (to_char - from_char,
1236 XVECTOR (string)->contents + from_char);
1238 return res;
1242 DEFUN ("substring-no-properties", Fsubstring_no_properties, Ssubstring_no_properties, 1, 3, 0,
1243 doc: /* Return a substring of STRING, without text properties.
1244 It starts at index FROM and ending before TO.
1245 TO may be nil or omitted; then the substring runs to the end of STRING.
1246 If FROM is nil or omitted, the substring starts at the beginning of STRING.
1247 If FROM or TO is negative, it counts from the end.
1249 With one argument, just copy STRING without its properties. */)
1250 (string, from, to)
1251 Lisp_Object string;
1252 register Lisp_Object from, to;
1254 int size, size_byte;
1255 int from_char, to_char;
1256 int from_byte, to_byte;
1258 CHECK_STRING (string);
1260 size = SCHARS (string);
1261 size_byte = SBYTES (string);
1263 if (NILP (from))
1264 from_char = from_byte = 0;
1265 else
1267 CHECK_NUMBER (from);
1268 from_char = XINT (from);
1269 if (from_char < 0)
1270 from_char += size;
1272 from_byte = string_char_to_byte (string, from_char);
1275 if (NILP (to))
1277 to_char = size;
1278 to_byte = size_byte;
1280 else
1282 CHECK_NUMBER (to);
1284 to_char = XINT (to);
1285 if (to_char < 0)
1286 to_char += size;
1288 to_byte = string_char_to_byte (string, to_char);
1291 if (!(0 <= from_char && from_char <= to_char && to_char <= size))
1292 args_out_of_range_3 (string, make_number (from_char),
1293 make_number (to_char));
1295 return make_specified_string (SDATA (string) + from_byte,
1296 to_char - from_char, to_byte - from_byte,
1297 STRING_MULTIBYTE (string));
1300 /* Extract a substring of STRING, giving start and end positions
1301 both in characters and in bytes. */
1303 Lisp_Object
1304 substring_both (string, from, from_byte, to, to_byte)
1305 Lisp_Object string;
1306 int from, from_byte, to, to_byte;
1308 Lisp_Object res;
1309 int size;
1310 int size_byte;
1312 if (! (STRINGP (string) || VECTORP (string)))
1313 wrong_type_argument (Qarrayp, string);
1315 if (STRINGP (string))
1317 size = SCHARS (string);
1318 size_byte = SBYTES (string);
1320 else
1321 size = XVECTOR (string)->size;
1323 if (!(0 <= from && from <= to && to <= size))
1324 args_out_of_range_3 (string, make_number (from), make_number (to));
1326 if (STRINGP (string))
1328 res = make_specified_string (SDATA (string) + from_byte,
1329 to - from, to_byte - from_byte,
1330 STRING_MULTIBYTE (string));
1331 copy_text_properties (make_number (from), make_number (to),
1332 string, make_number (0), res, Qnil);
1334 else
1335 res = Fvector (to - from,
1336 XVECTOR (string)->contents + from);
1338 return res;
1341 DEFUN ("nthcdr", Fnthcdr, Snthcdr, 2, 2, 0,
1342 doc: /* Take cdr N times on LIST, returns the result. */)
1343 (n, list)
1344 Lisp_Object n;
1345 register Lisp_Object list;
1347 register int i, num;
1348 CHECK_NUMBER (n);
1349 num = XINT (n);
1350 for (i = 0; i < num && !NILP (list); i++)
1352 QUIT;
1353 if (! CONSP (list))
1354 wrong_type_argument (Qlistp, list);
1355 list = XCDR (list);
1357 return list;
1360 DEFUN ("nth", Fnth, Snth, 2, 2, 0,
1361 doc: /* Return the Nth element of LIST.
1362 N counts from zero. If LIST is not that long, nil is returned. */)
1363 (n, list)
1364 Lisp_Object n, list;
1366 return Fcar (Fnthcdr (n, list));
1369 DEFUN ("elt", Felt, Selt, 2, 2, 0,
1370 doc: /* Return element of SEQUENCE at index N. */)
1371 (sequence, n)
1372 register Lisp_Object sequence, n;
1374 CHECK_NUMBER (n);
1375 while (1)
1377 if (CONSP (sequence) || NILP (sequence))
1378 return Fcar (Fnthcdr (n, sequence));
1379 else if (STRINGP (sequence) || VECTORP (sequence)
1380 || BOOL_VECTOR_P (sequence) || CHAR_TABLE_P (sequence))
1381 return Faref (sequence, n);
1382 else
1383 sequence = wrong_type_argument (Qsequencep, sequence);
1387 DEFUN ("member", Fmember, Smember, 2, 2, 0,
1388 doc: /* Return non-nil if ELT is an element of LIST. Comparison done with `equal'.
1389 The value is actually the tail of LIST whose car is ELT. */)
1390 (elt, list)
1391 register Lisp_Object elt;
1392 Lisp_Object list;
1394 register Lisp_Object tail;
1395 for (tail = list; !NILP (tail); tail = XCDR (tail))
1397 register Lisp_Object tem;
1398 if (! CONSP (tail))
1399 wrong_type_argument (Qlistp, list);
1400 tem = XCAR (tail);
1401 if (! NILP (Fequal (elt, tem)))
1402 return tail;
1403 QUIT;
1405 return Qnil;
1408 DEFUN ("memq", Fmemq, Smemq, 2, 2, 0,
1409 doc: /* Return non-nil if ELT is an element of LIST.
1410 Comparison done with EQ. The value is actually the tail of LIST
1411 whose car is ELT. */)
1412 (elt, list)
1413 Lisp_Object elt, list;
1415 while (1)
1417 if (!CONSP (list) || EQ (XCAR (list), elt))
1418 break;
1420 list = XCDR (list);
1421 if (!CONSP (list) || EQ (XCAR (list), elt))
1422 break;
1424 list = XCDR (list);
1425 if (!CONSP (list) || EQ (XCAR (list), elt))
1426 break;
1428 list = XCDR (list);
1429 QUIT;
1432 if (!CONSP (list) && !NILP (list))
1433 list = wrong_type_argument (Qlistp, list);
1435 return list;
1438 DEFUN ("assq", Fassq, Sassq, 2, 2, 0,
1439 doc: /* Return non-nil if KEY is `eq' to the car of an element of LIST.
1440 The value is actually the element of LIST whose car is KEY.
1441 Elements of LIST that are not conses are ignored. */)
1442 (key, list)
1443 Lisp_Object key, list;
1445 Lisp_Object result;
1447 while (1)
1449 if (!CONSP (list)
1450 || (CONSP (XCAR (list))
1451 && EQ (XCAR (XCAR (list)), key)))
1452 break;
1454 list = XCDR (list);
1455 if (!CONSP (list)
1456 || (CONSP (XCAR (list))
1457 && EQ (XCAR (XCAR (list)), key)))
1458 break;
1460 list = XCDR (list);
1461 if (!CONSP (list)
1462 || (CONSP (XCAR (list))
1463 && EQ (XCAR (XCAR (list)), key)))
1464 break;
1466 list = XCDR (list);
1467 QUIT;
1470 if (CONSP (list))
1471 result = XCAR (list);
1472 else if (NILP (list))
1473 result = Qnil;
1474 else
1475 result = wrong_type_argument (Qlistp, list);
1477 return result;
1480 /* Like Fassq but never report an error and do not allow quits.
1481 Use only on lists known never to be circular. */
1483 Lisp_Object
1484 assq_no_quit (key, list)
1485 Lisp_Object key, list;
1487 while (CONSP (list)
1488 && (!CONSP (XCAR (list))
1489 || !EQ (XCAR (XCAR (list)), key)))
1490 list = XCDR (list);
1492 return CONSP (list) ? XCAR (list) : Qnil;
1495 DEFUN ("assoc", Fassoc, Sassoc, 2, 2, 0,
1496 doc: /* Return non-nil if KEY is `equal' to the car of an element of LIST.
1497 The value is actually the element of LIST whose car equals KEY. */)
1498 (key, list)
1499 Lisp_Object key, list;
1501 Lisp_Object result, car;
1503 while (1)
1505 if (!CONSP (list)
1506 || (CONSP (XCAR (list))
1507 && (car = XCAR (XCAR (list)),
1508 EQ (car, key) || !NILP (Fequal (car, key)))))
1509 break;
1511 list = XCDR (list);
1512 if (!CONSP (list)
1513 || (CONSP (XCAR (list))
1514 && (car = XCAR (XCAR (list)),
1515 EQ (car, key) || !NILP (Fequal (car, key)))))
1516 break;
1518 list = XCDR (list);
1519 if (!CONSP (list)
1520 || (CONSP (XCAR (list))
1521 && (car = XCAR (XCAR (list)),
1522 EQ (car, key) || !NILP (Fequal (car, key)))))
1523 break;
1525 list = XCDR (list);
1526 QUIT;
1529 if (CONSP (list))
1530 result = XCAR (list);
1531 else if (NILP (list))
1532 result = Qnil;
1533 else
1534 result = wrong_type_argument (Qlistp, list);
1536 return result;
1539 DEFUN ("rassq", Frassq, Srassq, 2, 2, 0,
1540 doc: /* Return non-nil if KEY is `eq' to the cdr of an element of LIST.
1541 The value is actually the element of LIST whose cdr is KEY. */)
1542 (key, list)
1543 register Lisp_Object key;
1544 Lisp_Object list;
1546 Lisp_Object result;
1548 while (1)
1550 if (!CONSP (list)
1551 || (CONSP (XCAR (list))
1552 && EQ (XCDR (XCAR (list)), key)))
1553 break;
1555 list = XCDR (list);
1556 if (!CONSP (list)
1557 || (CONSP (XCAR (list))
1558 && EQ (XCDR (XCAR (list)), key)))
1559 break;
1561 list = XCDR (list);
1562 if (!CONSP (list)
1563 || (CONSP (XCAR (list))
1564 && EQ (XCDR (XCAR (list)), key)))
1565 break;
1567 list = XCDR (list);
1568 QUIT;
1571 if (NILP (list))
1572 result = Qnil;
1573 else if (CONSP (list))
1574 result = XCAR (list);
1575 else
1576 result = wrong_type_argument (Qlistp, list);
1578 return result;
1581 DEFUN ("rassoc", Frassoc, Srassoc, 2, 2, 0,
1582 doc: /* Return non-nil if KEY is `equal' to the cdr of an element of LIST.
1583 The value is actually the element of LIST whose cdr equals KEY. */)
1584 (key, list)
1585 Lisp_Object key, list;
1587 Lisp_Object result, cdr;
1589 while (1)
1591 if (!CONSP (list)
1592 || (CONSP (XCAR (list))
1593 && (cdr = XCDR (XCAR (list)),
1594 EQ (cdr, key) || !NILP (Fequal (cdr, key)))))
1595 break;
1597 list = XCDR (list);
1598 if (!CONSP (list)
1599 || (CONSP (XCAR (list))
1600 && (cdr = XCDR (XCAR (list)),
1601 EQ (cdr, key) || !NILP (Fequal (cdr, key)))))
1602 break;
1604 list = XCDR (list);
1605 if (!CONSP (list)
1606 || (CONSP (XCAR (list))
1607 && (cdr = XCDR (XCAR (list)),
1608 EQ (cdr, key) || !NILP (Fequal (cdr, key)))))
1609 break;
1611 list = XCDR (list);
1612 QUIT;
1615 if (CONSP (list))
1616 result = XCAR (list);
1617 else if (NILP (list))
1618 result = Qnil;
1619 else
1620 result = wrong_type_argument (Qlistp, list);
1622 return result;
1625 DEFUN ("delq", Fdelq, Sdelq, 2, 2, 0,
1626 doc: /* Delete by side effect any occurrences of ELT as a member of LIST.
1627 The modified LIST is returned. Comparison is done with `eq'.
1628 If the first member of LIST is ELT, there is no way to remove it by side effect;
1629 therefore, write `(setq foo (delq element foo))'
1630 to be sure of changing the value of `foo'. */)
1631 (elt, list)
1632 register Lisp_Object elt;
1633 Lisp_Object list;
1635 register Lisp_Object tail, prev;
1636 register Lisp_Object tem;
1638 tail = list;
1639 prev = Qnil;
1640 while (!NILP (tail))
1642 if (! CONSP (tail))
1643 wrong_type_argument (Qlistp, list);
1644 tem = XCAR (tail);
1645 if (EQ (elt, tem))
1647 if (NILP (prev))
1648 list = XCDR (tail);
1649 else
1650 Fsetcdr (prev, XCDR (tail));
1652 else
1653 prev = tail;
1654 tail = XCDR (tail);
1655 QUIT;
1657 return list;
1660 DEFUN ("delete", Fdelete, Sdelete, 2, 2, 0,
1661 doc: /* Delete by side effect any occurrences of ELT as a member of SEQ.
1662 SEQ must be a list, a vector, or a string.
1663 The modified SEQ is returned. Comparison is done with `equal'.
1664 If SEQ is not a list, or the first member of SEQ is ELT, deleting it
1665 is not a side effect; it is simply using a different sequence.
1666 Therefore, write `(setq foo (delete element foo))'
1667 to be sure of changing the value of `foo'. */)
1668 (elt, seq)
1669 Lisp_Object elt, seq;
1671 if (VECTORP (seq))
1673 EMACS_INT i, n;
1675 for (i = n = 0; i < ASIZE (seq); ++i)
1676 if (NILP (Fequal (AREF (seq, i), elt)))
1677 ++n;
1679 if (n != ASIZE (seq))
1681 struct Lisp_Vector *p = allocate_vector (n);
1683 for (i = n = 0; i < ASIZE (seq); ++i)
1684 if (NILP (Fequal (AREF (seq, i), elt)))
1685 p->contents[n++] = AREF (seq, i);
1687 XSETVECTOR (seq, p);
1690 else if (STRINGP (seq))
1692 EMACS_INT i, ibyte, nchars, nbytes, cbytes;
1693 int c;
1695 for (i = nchars = nbytes = ibyte = 0;
1696 i < SCHARS (seq);
1697 ++i, ibyte += cbytes)
1699 if (STRING_MULTIBYTE (seq))
1701 c = STRING_CHAR (SDATA (seq) + ibyte,
1702 SBYTES (seq) - ibyte);
1703 cbytes = CHAR_BYTES (c);
1705 else
1707 c = SREF (seq, i);
1708 cbytes = 1;
1711 if (!INTEGERP (elt) || c != XINT (elt))
1713 ++nchars;
1714 nbytes += cbytes;
1718 if (nchars != SCHARS (seq))
1720 Lisp_Object tem;
1722 tem = make_uninit_multibyte_string (nchars, nbytes);
1723 if (!STRING_MULTIBYTE (seq))
1724 STRING_SET_UNIBYTE (tem);
1726 for (i = nchars = nbytes = ibyte = 0;
1727 i < SCHARS (seq);
1728 ++i, ibyte += cbytes)
1730 if (STRING_MULTIBYTE (seq))
1732 c = STRING_CHAR (SDATA (seq) + ibyte,
1733 SBYTES (seq) - ibyte);
1734 cbytes = CHAR_BYTES (c);
1736 else
1738 c = SREF (seq, i);
1739 cbytes = 1;
1742 if (!INTEGERP (elt) || c != XINT (elt))
1744 unsigned char *from = SDATA (seq) + ibyte;
1745 unsigned char *to = SDATA (tem) + nbytes;
1746 EMACS_INT n;
1748 ++nchars;
1749 nbytes += cbytes;
1751 for (n = cbytes; n--; )
1752 *to++ = *from++;
1756 seq = tem;
1759 else
1761 Lisp_Object tail, prev;
1763 for (tail = seq, prev = Qnil; !NILP (tail); tail = XCDR (tail))
1765 if (!CONSP (tail))
1766 wrong_type_argument (Qlistp, seq);
1768 if (!NILP (Fequal (elt, XCAR (tail))))
1770 if (NILP (prev))
1771 seq = XCDR (tail);
1772 else
1773 Fsetcdr (prev, XCDR (tail));
1775 else
1776 prev = tail;
1777 QUIT;
1781 return seq;
1784 DEFUN ("nreverse", Fnreverse, Snreverse, 1, 1, 0,
1785 doc: /* Reverse LIST by modifying cdr pointers.
1786 Returns the beginning of the reversed list. */)
1787 (list)
1788 Lisp_Object list;
1790 register Lisp_Object prev, tail, next;
1792 if (NILP (list)) return list;
1793 prev = Qnil;
1794 tail = list;
1795 while (!NILP (tail))
1797 QUIT;
1798 if (! CONSP (tail))
1799 wrong_type_argument (Qlistp, list);
1800 next = XCDR (tail);
1801 Fsetcdr (tail, prev);
1802 prev = tail;
1803 tail = next;
1805 return prev;
1808 DEFUN ("reverse", Freverse, Sreverse, 1, 1, 0,
1809 doc: /* Reverse LIST, copying. Returns the beginning of the reversed list.
1810 See also the function `nreverse', which is used more often. */)
1811 (list)
1812 Lisp_Object list;
1814 Lisp_Object new;
1816 for (new = Qnil; CONSP (list); list = XCDR (list))
1817 new = Fcons (XCAR (list), new);
1818 if (!NILP (list))
1819 wrong_type_argument (Qconsp, list);
1820 return new;
1823 Lisp_Object merge ();
1825 DEFUN ("sort", Fsort, Ssort, 2, 2, 0,
1826 doc: /* Sort LIST, stably, comparing elements using PREDICATE.
1827 Returns the sorted list. LIST is modified by side effects.
1828 PREDICATE is called with two elements of LIST, and should return t
1829 if the first element is "less" than the second. */)
1830 (list, predicate)
1831 Lisp_Object list, predicate;
1833 Lisp_Object front, back;
1834 register Lisp_Object len, tem;
1835 struct gcpro gcpro1, gcpro2;
1836 register int length;
1838 front = list;
1839 len = Flength (list);
1840 length = XINT (len);
1841 if (length < 2)
1842 return list;
1844 XSETINT (len, (length / 2) - 1);
1845 tem = Fnthcdr (len, list);
1846 back = Fcdr (tem);
1847 Fsetcdr (tem, Qnil);
1849 GCPRO2 (front, back);
1850 front = Fsort (front, predicate);
1851 back = Fsort (back, predicate);
1852 UNGCPRO;
1853 return merge (front, back, predicate);
1856 Lisp_Object
1857 merge (org_l1, org_l2, pred)
1858 Lisp_Object org_l1, org_l2;
1859 Lisp_Object pred;
1861 Lisp_Object value;
1862 register Lisp_Object tail;
1863 Lisp_Object tem;
1864 register Lisp_Object l1, l2;
1865 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1867 l1 = org_l1;
1868 l2 = org_l2;
1869 tail = Qnil;
1870 value = Qnil;
1872 /* It is sufficient to protect org_l1 and org_l2.
1873 When l1 and l2 are updated, we copy the new values
1874 back into the org_ vars. */
1875 GCPRO4 (org_l1, org_l2, pred, value);
1877 while (1)
1879 if (NILP (l1))
1881 UNGCPRO;
1882 if (NILP (tail))
1883 return l2;
1884 Fsetcdr (tail, l2);
1885 return value;
1887 if (NILP (l2))
1889 UNGCPRO;
1890 if (NILP (tail))
1891 return l1;
1892 Fsetcdr (tail, l1);
1893 return value;
1895 tem = call2 (pred, Fcar (l2), Fcar (l1));
1896 if (NILP (tem))
1898 tem = l1;
1899 l1 = Fcdr (l1);
1900 org_l1 = l1;
1902 else
1904 tem = l2;
1905 l2 = Fcdr (l2);
1906 org_l2 = l2;
1908 if (NILP (tail))
1909 value = tem;
1910 else
1911 Fsetcdr (tail, tem);
1912 tail = tem;
1917 DEFUN ("plist-get", Fplist_get, Splist_get, 2, 2, 0,
1918 doc: /* Extract a value from a property list.
1919 PLIST is a property list, which is a list of the form
1920 \(PROP1 VALUE1 PROP2 VALUE2...). This function returns the value
1921 corresponding to the given PROP, or nil if PROP is not
1922 one of the properties on the list. */)
1923 (plist, prop)
1924 Lisp_Object plist;
1925 Lisp_Object prop;
1927 Lisp_Object tail;
1929 for (tail = plist;
1930 CONSP (tail) && CONSP (XCDR (tail));
1931 tail = XCDR (XCDR (tail)))
1933 if (EQ (prop, XCAR (tail)))
1934 return XCAR (XCDR (tail));
1936 /* This function can be called asynchronously
1937 (setup_coding_system). Don't QUIT in that case. */
1938 if (!interrupt_input_blocked)
1939 QUIT;
1942 if (!NILP (tail))
1943 wrong_type_argument (Qlistp, prop);
1945 return Qnil;
1948 DEFUN ("get", Fget, Sget, 2, 2, 0,
1949 doc: /* Return the value of SYMBOL's PROPNAME property.
1950 This is the last value stored with `(put SYMBOL PROPNAME VALUE)'. */)
1951 (symbol, propname)
1952 Lisp_Object symbol, propname;
1954 CHECK_SYMBOL (symbol);
1955 return Fplist_get (XSYMBOL (symbol)->plist, propname);
1958 DEFUN ("plist-put", Fplist_put, Splist_put, 3, 3, 0,
1959 doc: /* Change value in PLIST of PROP to VAL.
1960 PLIST is a property list, which is a list of the form
1961 \(PROP1 VALUE1 PROP2 VALUE2 ...). PROP is a symbol and VAL is any object.
1962 If PROP is already a property on the list, its value is set to VAL,
1963 otherwise the new PROP VAL pair is added. The new plist is returned;
1964 use `(setq x (plist-put x prop val))' to be sure to use the new value.
1965 The PLIST is modified by side effects. */)
1966 (plist, prop, val)
1967 Lisp_Object plist;
1968 register Lisp_Object prop;
1969 Lisp_Object val;
1971 register Lisp_Object tail, prev;
1972 Lisp_Object newcell;
1973 prev = Qnil;
1974 for (tail = plist; CONSP (tail) && CONSP (XCDR (tail));
1975 tail = XCDR (XCDR (tail)))
1977 if (EQ (prop, XCAR (tail)))
1979 Fsetcar (XCDR (tail), val);
1980 return plist;
1983 prev = tail;
1984 QUIT;
1986 newcell = Fcons (prop, Fcons (val, Qnil));
1987 if (NILP (prev))
1988 return newcell;
1989 else
1990 Fsetcdr (XCDR (prev), newcell);
1991 return plist;
1994 DEFUN ("put", Fput, Sput, 3, 3, 0,
1995 doc: /* Store SYMBOL's PROPNAME property with value VALUE.
1996 It can be retrieved with `(get SYMBOL PROPNAME)'. */)
1997 (symbol, propname, value)
1998 Lisp_Object symbol, propname, value;
2000 CHECK_SYMBOL (symbol);
2001 XSYMBOL (symbol)->plist
2002 = Fplist_put (XSYMBOL (symbol)->plist, propname, value);
2003 return value;
2006 DEFUN ("lax-plist-get", Flax_plist_get, Slax_plist_get, 2, 2, 0,
2007 doc: /* Extract a value from a property list, comparing with `equal'.
2008 PLIST is a property list, which is a list of the form
2009 \(PROP1 VALUE1 PROP2 VALUE2...). This function returns the value
2010 corresponding to the given PROP, or nil if PROP is not
2011 one of the properties on the list. */)
2012 (plist, prop)
2013 Lisp_Object plist;
2014 Lisp_Object prop;
2016 Lisp_Object tail;
2018 for (tail = plist;
2019 CONSP (tail) && CONSP (XCDR (tail));
2020 tail = XCDR (XCDR (tail)))
2022 if (! NILP (Fequal (prop, XCAR (tail))))
2023 return XCAR (XCDR (tail));
2025 QUIT;
2028 if (!NILP (tail))
2029 wrong_type_argument (Qlistp, prop);
2031 return Qnil;
2034 DEFUN ("lax-plist-put", Flax_plist_put, Slax_plist_put, 3, 3, 0,
2035 doc: /* Change value in PLIST of PROP to VAL, comparing with `equal'.
2036 PLIST is a property list, which is a list of the form
2037 \(PROP1 VALUE1 PROP2 VALUE2 ...). PROP and VAL are any objects.
2038 If PROP is already a property on the list, its value is set to VAL,
2039 otherwise the new PROP VAL pair is added. The new plist is returned;
2040 use `(setq x (lax-plist-put x prop val))' to be sure to use the new value.
2041 The PLIST is modified by side effects. */)
2042 (plist, prop, val)
2043 Lisp_Object plist;
2044 register Lisp_Object prop;
2045 Lisp_Object val;
2047 register Lisp_Object tail, prev;
2048 Lisp_Object newcell;
2049 prev = Qnil;
2050 for (tail = plist; CONSP (tail) && CONSP (XCDR (tail));
2051 tail = XCDR (XCDR (tail)))
2053 if (! NILP (Fequal (prop, XCAR (tail))))
2055 Fsetcar (XCDR (tail), val);
2056 return plist;
2059 prev = tail;
2060 QUIT;
2062 newcell = Fcons (prop, Fcons (val, Qnil));
2063 if (NILP (prev))
2064 return newcell;
2065 else
2066 Fsetcdr (XCDR (prev), newcell);
2067 return plist;
2070 DEFUN ("equal", Fequal, Sequal, 2, 2, 0,
2071 doc: /* Return t if two Lisp objects have similar structure and contents.
2072 They must have the same data type.
2073 Conses are compared by comparing the cars and the cdrs.
2074 Vectors and strings are compared element by element.
2075 Numbers are compared by value, but integers cannot equal floats.
2076 (Use `=' if you want integers and floats to be able to be equal.)
2077 Symbols must match exactly. */)
2078 (o1, o2)
2079 register Lisp_Object o1, o2;
2081 return internal_equal (o1, o2, 0) ? Qt : Qnil;
2084 static int
2085 internal_equal (o1, o2, depth)
2086 register Lisp_Object o1, o2;
2087 int depth;
2089 if (depth > 200)
2090 error ("Stack overflow in equal");
2092 tail_recurse:
2093 QUIT;
2094 if (EQ (o1, o2))
2095 return 1;
2096 if (XTYPE (o1) != XTYPE (o2))
2097 return 0;
2099 switch (XTYPE (o1))
2101 case Lisp_Float:
2102 return (extract_float (o1) == extract_float (o2));
2104 case Lisp_Cons:
2105 if (!internal_equal (XCAR (o1), XCAR (o2), depth + 1))
2106 return 0;
2107 o1 = XCDR (o1);
2108 o2 = XCDR (o2);
2109 goto tail_recurse;
2111 case Lisp_Misc:
2112 if (XMISCTYPE (o1) != XMISCTYPE (o2))
2113 return 0;
2114 if (OVERLAYP (o1))
2116 if (!internal_equal (OVERLAY_START (o1), OVERLAY_START (o2),
2117 depth + 1)
2118 || !internal_equal (OVERLAY_END (o1), OVERLAY_END (o2),
2119 depth + 1))
2120 return 0;
2121 o1 = XOVERLAY (o1)->plist;
2122 o2 = XOVERLAY (o2)->plist;
2123 goto tail_recurse;
2125 if (MARKERP (o1))
2127 return (XMARKER (o1)->buffer == XMARKER (o2)->buffer
2128 && (XMARKER (o1)->buffer == 0
2129 || XMARKER (o1)->bytepos == XMARKER (o2)->bytepos));
2131 break;
2133 case Lisp_Vectorlike:
2135 register int i, size;
2136 size = XVECTOR (o1)->size;
2137 /* Pseudovectors have the type encoded in the size field, so this test
2138 actually checks that the objects have the same type as well as the
2139 same size. */
2140 if (XVECTOR (o2)->size != size)
2141 return 0;
2142 /* Boolvectors are compared much like strings. */
2143 if (BOOL_VECTOR_P (o1))
2145 int size_in_chars
2146 = (XBOOL_VECTOR (o1)->size + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
2148 if (XBOOL_VECTOR (o1)->size != XBOOL_VECTOR (o2)->size)
2149 return 0;
2150 if (bcmp (XBOOL_VECTOR (o1)->data, XBOOL_VECTOR (o2)->data,
2151 size_in_chars))
2152 return 0;
2153 return 1;
2155 if (WINDOW_CONFIGURATIONP (o1))
2156 return compare_window_configurations (o1, o2, 0);
2158 /* Aside from them, only true vectors, char-tables, and compiled
2159 functions are sensible to compare, so eliminate the others now. */
2160 if (size & PSEUDOVECTOR_FLAG)
2162 if (!(size & (PVEC_COMPILED | PVEC_CHAR_TABLE)))
2163 return 0;
2164 size &= PSEUDOVECTOR_SIZE_MASK;
2166 for (i = 0; i < size; i++)
2168 Lisp_Object v1, v2;
2169 v1 = XVECTOR (o1)->contents [i];
2170 v2 = XVECTOR (o2)->contents [i];
2171 if (!internal_equal (v1, v2, depth + 1))
2172 return 0;
2174 return 1;
2176 break;
2178 case Lisp_String:
2179 if (SCHARS (o1) != SCHARS (o2))
2180 return 0;
2181 if (SBYTES (o1) != SBYTES (o2))
2182 return 0;
2183 if (bcmp (SDATA (o1), SDATA (o2),
2184 SBYTES (o1)))
2185 return 0;
2186 return 1;
2188 case Lisp_Int:
2189 case Lisp_Symbol:
2190 case Lisp_Type_Limit:
2191 break;
2194 return 0;
2197 extern Lisp_Object Fmake_char_internal ();
2199 DEFUN ("fillarray", Ffillarray, Sfillarray, 2, 2, 0,
2200 doc: /* Store each element of ARRAY with ITEM.
2201 ARRAY is a vector, string, char-table, or bool-vector. */)
2202 (array, item)
2203 Lisp_Object array, item;
2205 register int size, index, charval;
2206 retry:
2207 if (VECTORP (array))
2209 register Lisp_Object *p = XVECTOR (array)->contents;
2210 size = XVECTOR (array)->size;
2211 for (index = 0; index < size; index++)
2212 p[index] = item;
2214 else if (CHAR_TABLE_P (array))
2216 register Lisp_Object *p = XCHAR_TABLE (array)->contents;
2217 size = CHAR_TABLE_ORDINARY_SLOTS;
2218 for (index = 0; index < size; index++)
2219 p[index] = item;
2220 XCHAR_TABLE (array)->defalt = Qnil;
2222 else if (STRINGP (array))
2224 register unsigned char *p = SDATA (array);
2225 CHECK_NUMBER (item);
2226 charval = XINT (item);
2227 size = SCHARS (array);
2228 if (STRING_MULTIBYTE (array))
2230 unsigned char str[MAX_MULTIBYTE_LENGTH];
2231 int len = CHAR_STRING (charval, str);
2232 int size_byte = SBYTES (array);
2233 unsigned char *p1 = p, *endp = p + size_byte;
2234 int i;
2236 if (size != size_byte)
2237 while (p1 < endp)
2239 int this_len = MULTIBYTE_FORM_LENGTH (p1, endp - p1);
2240 if (len != this_len)
2241 error ("Attempt to change byte length of a string");
2242 p1 += this_len;
2244 for (i = 0; i < size_byte; i++)
2245 *p++ = str[i % len];
2247 else
2248 for (index = 0; index < size; index++)
2249 p[index] = charval;
2251 else if (BOOL_VECTOR_P (array))
2253 register unsigned char *p = XBOOL_VECTOR (array)->data;
2254 int size_in_chars
2255 = (XBOOL_VECTOR (array)->size + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
2257 charval = (! NILP (item) ? -1 : 0);
2258 for (index = 0; index < size_in_chars; index++)
2259 p[index] = charval;
2261 else
2263 array = wrong_type_argument (Qarrayp, array);
2264 goto retry;
2266 return array;
2269 DEFUN ("char-table-subtype", Fchar_table_subtype, Schar_table_subtype,
2270 1, 1, 0,
2271 doc: /* Return the subtype of char-table CHAR-TABLE. The value is a symbol. */)
2272 (char_table)
2273 Lisp_Object char_table;
2275 CHECK_CHAR_TABLE (char_table);
2277 return XCHAR_TABLE (char_table)->purpose;
2280 DEFUN ("char-table-parent", Fchar_table_parent, Schar_table_parent,
2281 1, 1, 0,
2282 doc: /* Return the parent char-table of CHAR-TABLE.
2283 The value is either nil or another char-table.
2284 If CHAR-TABLE holds nil for a given character,
2285 then the actual applicable value is inherited from the parent char-table
2286 \(or from its parents, if necessary). */)
2287 (char_table)
2288 Lisp_Object char_table;
2290 CHECK_CHAR_TABLE (char_table);
2292 return XCHAR_TABLE (char_table)->parent;
2295 DEFUN ("set-char-table-parent", Fset_char_table_parent, Sset_char_table_parent,
2296 2, 2, 0,
2297 doc: /* Set the parent char-table of CHAR-TABLE to PARENT.
2298 PARENT must be either nil or another char-table. */)
2299 (char_table, parent)
2300 Lisp_Object char_table, parent;
2302 Lisp_Object temp;
2304 CHECK_CHAR_TABLE (char_table);
2306 if (!NILP (parent))
2308 CHECK_CHAR_TABLE (parent);
2310 for (temp = parent; !NILP (temp); temp = XCHAR_TABLE (temp)->parent)
2311 if (EQ (temp, char_table))
2312 error ("Attempt to make a chartable be its own parent");
2315 XCHAR_TABLE (char_table)->parent = parent;
2317 return parent;
2320 DEFUN ("char-table-extra-slot", Fchar_table_extra_slot, Schar_table_extra_slot,
2321 2, 2, 0,
2322 doc: /* Return the value of CHAR-TABLE's extra-slot number N. */)
2323 (char_table, n)
2324 Lisp_Object char_table, n;
2326 CHECK_CHAR_TABLE (char_table);
2327 CHECK_NUMBER (n);
2328 if (XINT (n) < 0
2329 || XINT (n) >= CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (char_table)))
2330 args_out_of_range (char_table, n);
2332 return XCHAR_TABLE (char_table)->extras[XINT (n)];
2335 DEFUN ("set-char-table-extra-slot", Fset_char_table_extra_slot,
2336 Sset_char_table_extra_slot,
2337 3, 3, 0,
2338 doc: /* Set CHAR-TABLE's extra-slot number N to VALUE. */)
2339 (char_table, n, value)
2340 Lisp_Object char_table, n, value;
2342 CHECK_CHAR_TABLE (char_table);
2343 CHECK_NUMBER (n);
2344 if (XINT (n) < 0
2345 || XINT (n) >= CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (char_table)))
2346 args_out_of_range (char_table, n);
2348 return XCHAR_TABLE (char_table)->extras[XINT (n)] = value;
2351 DEFUN ("char-table-range", Fchar_table_range, Schar_table_range,
2352 2, 2, 0,
2353 doc: /* Return the value in CHAR-TABLE for a range of characters RANGE.
2354 RANGE should be nil (for the default value)
2355 a vector which identifies a character set or a row of a character set,
2356 a character set name, or a character code. */)
2357 (char_table, range)
2358 Lisp_Object char_table, range;
2360 CHECK_CHAR_TABLE (char_table);
2362 if (EQ (range, Qnil))
2363 return XCHAR_TABLE (char_table)->defalt;
2364 else if (INTEGERP (range))
2365 return Faref (char_table, range);
2366 else if (SYMBOLP (range))
2368 Lisp_Object charset_info;
2370 charset_info = Fget (range, Qcharset);
2371 CHECK_VECTOR (charset_info);
2373 return Faref (char_table,
2374 make_number (XINT (XVECTOR (charset_info)->contents[0])
2375 + 128));
2377 else if (VECTORP (range))
2379 if (XVECTOR (range)->size == 1)
2380 return Faref (char_table,
2381 make_number (XINT (XVECTOR (range)->contents[0]) + 128));
2382 else
2384 int size = XVECTOR (range)->size;
2385 Lisp_Object *val = XVECTOR (range)->contents;
2386 Lisp_Object ch = Fmake_char_internal (size <= 0 ? Qnil : val[0],
2387 size <= 1 ? Qnil : val[1],
2388 size <= 2 ? Qnil : val[2]);
2389 return Faref (char_table, ch);
2392 else
2393 error ("Invalid RANGE argument to `char-table-range'");
2394 return Qt;
2397 DEFUN ("set-char-table-range", Fset_char_table_range, Sset_char_table_range,
2398 3, 3, 0,
2399 doc: /* Set the value in CHAR-TABLE for a range of characters RANGE to VALUE.
2400 RANGE should be t (for all characters), nil (for the default value)
2401 a vector which identifies a character set or a row of a character set,
2402 a coding system, or a character code. */)
2403 (char_table, range, value)
2404 Lisp_Object char_table, range, value;
2406 int i;
2408 CHECK_CHAR_TABLE (char_table);
2410 if (EQ (range, Qt))
2411 for (i = 0; i < CHAR_TABLE_ORDINARY_SLOTS; i++)
2412 XCHAR_TABLE (char_table)->contents[i] = value;
2413 else if (EQ (range, Qnil))
2414 XCHAR_TABLE (char_table)->defalt = value;
2415 else if (SYMBOLP (range))
2417 Lisp_Object charset_info;
2419 charset_info = Fget (range, Qcharset);
2420 CHECK_VECTOR (charset_info);
2422 return Faset (char_table,
2423 make_number (XINT (XVECTOR (charset_info)->contents[0])
2424 + 128),
2425 value);
2427 else if (INTEGERP (range))
2428 Faset (char_table, range, value);
2429 else if (VECTORP (range))
2431 if (XVECTOR (range)->size == 1)
2432 return Faset (char_table,
2433 make_number (XINT (XVECTOR (range)->contents[0]) + 128),
2434 value);
2435 else
2437 int size = XVECTOR (range)->size;
2438 Lisp_Object *val = XVECTOR (range)->contents;
2439 Lisp_Object ch = Fmake_char_internal (size <= 0 ? Qnil : val[0],
2440 size <= 1 ? Qnil : val[1],
2441 size <= 2 ? Qnil : val[2]);
2442 return Faset (char_table, ch, value);
2445 else
2446 error ("Invalid RANGE argument to `set-char-table-range'");
2448 return value;
2451 DEFUN ("set-char-table-default", Fset_char_table_default,
2452 Sset_char_table_default, 3, 3, 0,
2453 doc: /* Set the default value in CHAR-TABLE for a generic character CHAR to VALUE.
2454 The generic character specifies the group of characters.
2455 See also the documentation of make-char. */)
2456 (char_table, ch, value)
2457 Lisp_Object char_table, ch, value;
2459 int c, charset, code1, code2;
2460 Lisp_Object temp;
2462 CHECK_CHAR_TABLE (char_table);
2463 CHECK_NUMBER (ch);
2465 c = XINT (ch);
2466 SPLIT_CHAR (c, charset, code1, code2);
2468 /* Since we may want to set the default value for a character set
2469 not yet defined, we check only if the character set is in the
2470 valid range or not, instead of it is already defined or not. */
2471 if (! CHARSET_VALID_P (charset))
2472 invalid_character (c);
2474 if (charset == CHARSET_ASCII)
2475 return (XCHAR_TABLE (char_table)->defalt = value);
2477 /* Even if C is not a generic char, we had better behave as if a
2478 generic char is specified. */
2479 if (!CHARSET_DEFINED_P (charset) || CHARSET_DIMENSION (charset) == 1)
2480 code1 = 0;
2481 temp = XCHAR_TABLE (char_table)->contents[charset + 128];
2482 if (!code1)
2484 if (SUB_CHAR_TABLE_P (temp))
2485 XCHAR_TABLE (temp)->defalt = value;
2486 else
2487 XCHAR_TABLE (char_table)->contents[charset + 128] = value;
2488 return value;
2490 if (SUB_CHAR_TABLE_P (temp))
2491 char_table = temp;
2492 else
2493 char_table = (XCHAR_TABLE (char_table)->contents[charset + 128]
2494 = make_sub_char_table (temp));
2495 temp = XCHAR_TABLE (char_table)->contents[code1];
2496 if (SUB_CHAR_TABLE_P (temp))
2497 XCHAR_TABLE (temp)->defalt = value;
2498 else
2499 XCHAR_TABLE (char_table)->contents[code1] = value;
2500 return value;
2503 /* Look up the element in TABLE at index CH,
2504 and return it as an integer.
2505 If the element is nil, return CH itself.
2506 (Actually we do that for any non-integer.) */
2509 char_table_translate (table, ch)
2510 Lisp_Object table;
2511 int ch;
2513 Lisp_Object value;
2514 value = Faref (table, make_number (ch));
2515 if (! INTEGERP (value))
2516 return ch;
2517 return XINT (value);
2520 static void
2521 optimize_sub_char_table (table, chars)
2522 Lisp_Object *table;
2523 int chars;
2525 Lisp_Object elt;
2526 int from, to;
2528 if (chars == 94)
2529 from = 33, to = 127;
2530 else
2531 from = 32, to = 128;
2533 if (!SUB_CHAR_TABLE_P (*table))
2534 return;
2535 elt = XCHAR_TABLE (*table)->contents[from++];
2536 for (; from < to; from++)
2537 if (NILP (Fequal (elt, XCHAR_TABLE (*table)->contents[from])))
2538 return;
2539 *table = elt;
2542 DEFUN ("optimize-char-table", Foptimize_char_table, Soptimize_char_table,
2543 1, 1, 0, doc: /* Optimize char table TABLE. */)
2544 (table)
2545 Lisp_Object table;
2547 Lisp_Object elt;
2548 int dim;
2549 int i, j;
2551 CHECK_CHAR_TABLE (table);
2553 for (i = CHAR_TABLE_SINGLE_BYTE_SLOTS; i < CHAR_TABLE_ORDINARY_SLOTS; i++)
2555 elt = XCHAR_TABLE (table)->contents[i];
2556 if (!SUB_CHAR_TABLE_P (elt))
2557 continue;
2558 dim = CHARSET_DIMENSION (i - 128);
2559 if (dim == 2)
2560 for (j = 32; j < SUB_CHAR_TABLE_ORDINARY_SLOTS; j++)
2561 optimize_sub_char_table (XCHAR_TABLE (elt)->contents + j, dim);
2562 optimize_sub_char_table (XCHAR_TABLE (table)->contents + i, dim);
2564 return Qnil;
2568 /* Map C_FUNCTION or FUNCTION over SUBTABLE, calling it for each
2569 character or group of characters that share a value.
2570 DEPTH is the current depth in the originally specified
2571 chartable, and INDICES contains the vector indices
2572 for the levels our callers have descended.
2574 ARG is passed to C_FUNCTION when that is called. */
2576 void
2577 map_char_table (c_function, function, subtable, arg, depth, indices)
2578 void (*c_function) P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
2579 Lisp_Object function, subtable, arg, *indices;
2580 int depth;
2582 int i, to;
2584 if (depth == 0)
2586 /* At first, handle ASCII and 8-bit European characters. */
2587 for (i = 0; i < CHAR_TABLE_SINGLE_BYTE_SLOTS; i++)
2589 Lisp_Object elt = XCHAR_TABLE (subtable)->contents[i];
2590 if (c_function)
2591 (*c_function) (arg, make_number (i), elt);
2592 else
2593 call2 (function, make_number (i), elt);
2595 #if 0 /* If the char table has entries for higher characters,
2596 we should report them. */
2597 if (NILP (current_buffer->enable_multibyte_characters))
2598 return;
2599 #endif
2600 to = CHAR_TABLE_ORDINARY_SLOTS;
2602 else
2604 int charset = XFASTINT (indices[0]) - 128;
2606 i = 32;
2607 to = SUB_CHAR_TABLE_ORDINARY_SLOTS;
2608 if (CHARSET_CHARS (charset) == 94)
2609 i++, to--;
2612 for (; i < to; i++)
2614 Lisp_Object elt;
2615 int charset;
2617 elt = XCHAR_TABLE (subtable)->contents[i];
2618 XSETFASTINT (indices[depth], i);
2619 charset = XFASTINT (indices[0]) - 128;
2620 if (depth == 0
2621 && (!CHARSET_DEFINED_P (charset)
2622 || charset == CHARSET_8_BIT_CONTROL
2623 || charset == CHARSET_8_BIT_GRAPHIC))
2624 continue;
2626 if (SUB_CHAR_TABLE_P (elt))
2628 if (depth >= 3)
2629 error ("Too deep char table");
2630 map_char_table (c_function, function, elt, arg, depth + 1, indices);
2632 else
2634 int c1, c2, c;
2636 if (NILP (elt))
2637 elt = XCHAR_TABLE (subtable)->defalt;
2638 c1 = depth >= 1 ? XFASTINT (indices[1]) : 0;
2639 c2 = depth >= 2 ? XFASTINT (indices[2]) : 0;
2640 c = MAKE_CHAR (charset, c1, c2);
2641 if (c_function)
2642 (*c_function) (arg, make_number (c), elt);
2643 else
2644 call2 (function, make_number (c), elt);
2649 DEFUN ("map-char-table", Fmap_char_table, Smap_char_table,
2650 2, 2, 0,
2651 doc: /* Call FUNCTION for each (normal and generic) characters in CHAR-TABLE.
2652 FUNCTION is called with two arguments--a key and a value.
2653 The key is always a possible IDX argument to `aref'. */)
2654 (function, char_table)
2655 Lisp_Object function, char_table;
2657 /* The depth of char table is at most 3. */
2658 Lisp_Object indices[3];
2660 CHECK_CHAR_TABLE (char_table);
2662 map_char_table ((void *) call2, Qnil, char_table, function, 0, indices);
2663 return Qnil;
2666 /* Return a value for character C in char-table TABLE. Store the
2667 actual index for that value in *IDX. Ignore the default value of
2668 TABLE. */
2670 Lisp_Object
2671 char_table_ref_and_index (table, c, idx)
2672 Lisp_Object table;
2673 int c, *idx;
2675 int charset, c1, c2;
2676 Lisp_Object elt;
2678 if (SINGLE_BYTE_CHAR_P (c))
2680 *idx = c;
2681 return XCHAR_TABLE (table)->contents[c];
2683 SPLIT_CHAR (c, charset, c1, c2);
2684 elt = XCHAR_TABLE (table)->contents[charset + 128];
2685 *idx = MAKE_CHAR (charset, 0, 0);
2686 if (!SUB_CHAR_TABLE_P (elt))
2687 return elt;
2688 if (c1 < 32 || NILP (XCHAR_TABLE (elt)->contents[c1]))
2689 return XCHAR_TABLE (elt)->defalt;
2690 elt = XCHAR_TABLE (elt)->contents[c1];
2691 *idx = MAKE_CHAR (charset, c1, 0);
2692 if (!SUB_CHAR_TABLE_P (elt))
2693 return elt;
2694 if (c2 < 32 || NILP (XCHAR_TABLE (elt)->contents[c2]))
2695 return XCHAR_TABLE (elt)->defalt;
2696 *idx = c;
2697 return XCHAR_TABLE (elt)->contents[c2];
2701 /* ARGSUSED */
2702 Lisp_Object
2703 nconc2 (s1, s2)
2704 Lisp_Object s1, s2;
2706 #ifdef NO_ARG_ARRAY
2707 Lisp_Object args[2];
2708 args[0] = s1;
2709 args[1] = s2;
2710 return Fnconc (2, args);
2711 #else
2712 return Fnconc (2, &s1);
2713 #endif /* NO_ARG_ARRAY */
2716 DEFUN ("nconc", Fnconc, Snconc, 0, MANY, 0,
2717 doc: /* Concatenate any number of lists by altering them.
2718 Only the last argument is not altered, and need not be a list.
2719 usage: (nconc &rest LISTS) */)
2720 (nargs, args)
2721 int nargs;
2722 Lisp_Object *args;
2724 register int argnum;
2725 register Lisp_Object tail, tem, val;
2727 val = tail = Qnil;
2729 for (argnum = 0; argnum < nargs; argnum++)
2731 tem = args[argnum];
2732 if (NILP (tem)) continue;
2734 if (NILP (val))
2735 val = tem;
2737 if (argnum + 1 == nargs) break;
2739 if (!CONSP (tem))
2740 tem = wrong_type_argument (Qlistp, tem);
2742 while (CONSP (tem))
2744 tail = tem;
2745 tem = XCDR (tail);
2746 QUIT;
2749 tem = args[argnum + 1];
2750 Fsetcdr (tail, tem);
2751 if (NILP (tem))
2752 args[argnum + 1] = tail;
2755 return val;
2758 /* This is the guts of all mapping functions.
2759 Apply FN to each element of SEQ, one by one,
2760 storing the results into elements of VALS, a C vector of Lisp_Objects.
2761 LENI is the length of VALS, which should also be the length of SEQ. */
2763 static void
2764 mapcar1 (leni, vals, fn, seq)
2765 int leni;
2766 Lisp_Object *vals;
2767 Lisp_Object fn, seq;
2769 register Lisp_Object tail;
2770 Lisp_Object dummy;
2771 register int i;
2772 struct gcpro gcpro1, gcpro2, gcpro3;
2774 if (vals)
2776 /* Don't let vals contain any garbage when GC happens. */
2777 for (i = 0; i < leni; i++)
2778 vals[i] = Qnil;
2780 GCPRO3 (dummy, fn, seq);
2781 gcpro1.var = vals;
2782 gcpro1.nvars = leni;
2784 else
2785 GCPRO2 (fn, seq);
2786 /* We need not explicitly protect `tail' because it is used only on lists, and
2787 1) lists are not relocated and 2) the list is marked via `seq' so will not be freed */
2789 if (VECTORP (seq))
2791 for (i = 0; i < leni; i++)
2793 dummy = XVECTOR (seq)->contents[i];
2794 dummy = call1 (fn, dummy);
2795 if (vals)
2796 vals[i] = dummy;
2799 else if (BOOL_VECTOR_P (seq))
2801 for (i = 0; i < leni; i++)
2803 int byte;
2804 byte = XBOOL_VECTOR (seq)->data[i / BITS_PER_CHAR];
2805 if (byte & (1 << (i % BITS_PER_CHAR)))
2806 dummy = Qt;
2807 else
2808 dummy = Qnil;
2810 dummy = call1 (fn, dummy);
2811 if (vals)
2812 vals[i] = dummy;
2815 else if (STRINGP (seq))
2817 int i_byte;
2819 for (i = 0, i_byte = 0; i < leni;)
2821 int c;
2822 int i_before = i;
2824 FETCH_STRING_CHAR_ADVANCE (c, seq, i, i_byte);
2825 XSETFASTINT (dummy, c);
2826 dummy = call1 (fn, dummy);
2827 if (vals)
2828 vals[i_before] = dummy;
2831 else /* Must be a list, since Flength did not get an error */
2833 tail = seq;
2834 for (i = 0; i < leni; i++)
2836 dummy = call1 (fn, Fcar (tail));
2837 if (vals)
2838 vals[i] = dummy;
2839 tail = XCDR (tail);
2843 UNGCPRO;
2846 DEFUN ("mapconcat", Fmapconcat, Smapconcat, 3, 3, 0,
2847 doc: /* Apply FUNCTION to each element of SEQUENCE, and concat the results as strings.
2848 In between each pair of results, stick in SEPARATOR. Thus, " " as
2849 SEPARATOR results in spaces between the values returned by FUNCTION.
2850 SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
2851 (function, sequence, separator)
2852 Lisp_Object function, sequence, separator;
2854 Lisp_Object len;
2855 register int leni;
2856 int nargs;
2857 register Lisp_Object *args;
2858 register int i;
2859 struct gcpro gcpro1;
2861 len = Flength (sequence);
2862 leni = XINT (len);
2863 nargs = leni + leni - 1;
2864 if (nargs < 0) return build_string ("");
2866 args = (Lisp_Object *) alloca (nargs * sizeof (Lisp_Object));
2868 GCPRO1 (separator);
2869 mapcar1 (leni, args, function, sequence);
2870 UNGCPRO;
2872 for (i = leni - 1; i >= 0; i--)
2873 args[i + i] = args[i];
2875 for (i = 1; i < nargs; i += 2)
2876 args[i] = separator;
2878 return Fconcat (nargs, args);
2881 DEFUN ("mapcar", Fmapcar, Smapcar, 2, 2, 0,
2882 doc: /* Apply FUNCTION to each element of SEQUENCE, and make a list of the results.
2883 The result is a list just as long as SEQUENCE.
2884 SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
2885 (function, sequence)
2886 Lisp_Object function, sequence;
2888 register Lisp_Object len;
2889 register int leni;
2890 register Lisp_Object *args;
2892 len = Flength (sequence);
2893 leni = XFASTINT (len);
2894 args = (Lisp_Object *) alloca (leni * sizeof (Lisp_Object));
2896 mapcar1 (leni, args, function, sequence);
2898 return Flist (leni, args);
2901 DEFUN ("mapc", Fmapc, Smapc, 2, 2, 0,
2902 doc: /* Apply FUNCTION to each element of SEQUENCE for side effects only.
2903 Unlike `mapcar', don't accumulate the results. Return SEQUENCE.
2904 SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
2905 (function, sequence)
2906 Lisp_Object function, sequence;
2908 register int leni;
2910 leni = XFASTINT (Flength (sequence));
2911 mapcar1 (leni, 0, function, sequence);
2913 return sequence;
2916 /* Anything that calls this function must protect from GC! */
2918 DEFUN ("y-or-n-p", Fy_or_n_p, Sy_or_n_p, 1, 1, 0,
2919 doc: /* Ask user a "y or n" question. Return t if answer is "y".
2920 Takes one argument, which is the string to display to ask the question.
2921 It should end in a space; `y-or-n-p' adds `(y or n) ' to it.
2922 No confirmation of the answer is requested; a single character is enough.
2923 Also accepts Space to mean yes, or Delete to mean no. \(Actually, it uses
2924 the bindings in `query-replace-map'; see the documentation of that variable
2925 for more information. In this case, the useful bindings are `act', `skip',
2926 `recenter', and `quit'.\)
2928 Under a windowing system a dialog box will be used if `last-nonmenu-event'
2929 is nil and `use-dialog-box' is non-nil. */)
2930 (prompt)
2931 Lisp_Object prompt;
2933 register Lisp_Object obj, key, def, map;
2934 register int answer;
2935 Lisp_Object xprompt;
2936 Lisp_Object args[2];
2937 struct gcpro gcpro1, gcpro2;
2938 int count = SPECPDL_INDEX ();
2940 specbind (Qcursor_in_echo_area, Qt);
2942 map = Fsymbol_value (intern ("query-replace-map"));
2944 CHECK_STRING (prompt);
2945 xprompt = prompt;
2946 GCPRO2 (prompt, xprompt);
2948 #ifdef HAVE_X_WINDOWS
2949 if (display_hourglass_p)
2950 cancel_hourglass ();
2951 #endif
2953 while (1)
2956 #ifdef HAVE_MENUS
2957 if ((NILP (last_nonmenu_event) || CONSP (last_nonmenu_event))
2958 && use_dialog_box
2959 && have_menus_p ())
2961 Lisp_Object pane, menu;
2962 redisplay_preserve_echo_area (3);
2963 pane = Fcons (Fcons (build_string ("Yes"), Qt),
2964 Fcons (Fcons (build_string ("No"), Qnil),
2965 Qnil));
2966 menu = Fcons (prompt, pane);
2967 obj = Fx_popup_dialog (Qt, menu);
2968 answer = !NILP (obj);
2969 break;
2971 #endif /* HAVE_MENUS */
2972 cursor_in_echo_area = 1;
2973 choose_minibuf_frame ();
2976 Lisp_Object pargs[3];
2978 /* Colorize prompt according to `minibuffer-prompt' face. */
2979 pargs[0] = build_string ("%s(y or n) ");
2980 pargs[1] = intern ("face");
2981 pargs[2] = intern ("minibuffer-prompt");
2982 args[0] = Fpropertize (3, pargs);
2983 args[1] = xprompt;
2984 Fmessage (2, args);
2987 if (minibuffer_auto_raise)
2989 Lisp_Object mini_frame;
2991 mini_frame = WINDOW_FRAME (XWINDOW (minibuf_window));
2993 Fraise_frame (mini_frame);
2996 obj = read_filtered_event (1, 0, 0, 0);
2997 cursor_in_echo_area = 0;
2998 /* If we need to quit, quit with cursor_in_echo_area = 0. */
2999 QUIT;
3001 key = Fmake_vector (make_number (1), obj);
3002 def = Flookup_key (map, key, Qt);
3004 if (EQ (def, intern ("skip")))
3006 answer = 0;
3007 break;
3009 else if (EQ (def, intern ("act")))
3011 answer = 1;
3012 break;
3014 else if (EQ (def, intern ("recenter")))
3016 Frecenter (Qnil);
3017 xprompt = prompt;
3018 continue;
3020 else if (EQ (def, intern ("quit")))
3021 Vquit_flag = Qt;
3022 /* We want to exit this command for exit-prefix,
3023 and this is the only way to do it. */
3024 else if (EQ (def, intern ("exit-prefix")))
3025 Vquit_flag = Qt;
3027 QUIT;
3029 /* If we don't clear this, then the next call to read_char will
3030 return quit_char again, and we'll enter an infinite loop. */
3031 Vquit_flag = Qnil;
3033 Fding (Qnil);
3034 Fdiscard_input ();
3035 if (EQ (xprompt, prompt))
3037 args[0] = build_string ("Please answer y or n. ");
3038 args[1] = prompt;
3039 xprompt = Fconcat (2, args);
3042 UNGCPRO;
3044 if (! noninteractive)
3046 cursor_in_echo_area = -1;
3047 message_with_string (answer ? "%s(y or n) y" : "%s(y or n) n",
3048 xprompt, 0);
3051 unbind_to (count, Qnil);
3052 return answer ? Qt : Qnil;
3055 /* This is how C code calls `yes-or-no-p' and allows the user
3056 to redefined it.
3058 Anything that calls this function must protect from GC! */
3060 Lisp_Object
3061 do_yes_or_no_p (prompt)
3062 Lisp_Object prompt;
3064 return call1 (intern ("yes-or-no-p"), prompt);
3067 /* Anything that calls this function must protect from GC! */
3069 DEFUN ("yes-or-no-p", Fyes_or_no_p, Syes_or_no_p, 1, 1, 0,
3070 doc: /* Ask user a yes-or-no question. Return t if answer is yes.
3071 Takes one argument, which is the string to display to ask the question.
3072 It should end in a space; `yes-or-no-p' adds `(yes or no) ' to it.
3073 The user must confirm the answer with RET,
3074 and can edit it until it has been confirmed.
3076 Under a windowing system a dialog box will be used if `last-nonmenu-event'
3077 is nil, and `use-dialog-box' is non-nil. */)
3078 (prompt)
3079 Lisp_Object prompt;
3081 register Lisp_Object ans;
3082 Lisp_Object args[2];
3083 struct gcpro gcpro1;
3085 CHECK_STRING (prompt);
3087 #ifdef HAVE_MENUS
3088 if ((NILP (last_nonmenu_event) || CONSP (last_nonmenu_event))
3089 && use_dialog_box
3090 && have_menus_p ())
3092 Lisp_Object pane, menu, obj;
3093 redisplay_preserve_echo_area (4);
3094 pane = Fcons (Fcons (build_string ("Yes"), Qt),
3095 Fcons (Fcons (build_string ("No"), Qnil),
3096 Qnil));
3097 GCPRO1 (pane);
3098 menu = Fcons (prompt, pane);
3099 obj = Fx_popup_dialog (Qt, menu);
3100 UNGCPRO;
3101 return obj;
3103 #endif /* HAVE_MENUS */
3105 args[0] = prompt;
3106 args[1] = build_string ("(yes or no) ");
3107 prompt = Fconcat (2, args);
3109 GCPRO1 (prompt);
3111 while (1)
3113 ans = Fdowncase (Fread_from_minibuffer (prompt, Qnil, Qnil, Qnil,
3114 Qyes_or_no_p_history, Qnil,
3115 Qnil));
3116 if (SCHARS (ans) == 3 && !strcmp (SDATA (ans), "yes"))
3118 UNGCPRO;
3119 return Qt;
3121 if (SCHARS (ans) == 2 && !strcmp (SDATA (ans), "no"))
3123 UNGCPRO;
3124 return Qnil;
3127 Fding (Qnil);
3128 Fdiscard_input ();
3129 message ("Please answer yes or no.");
3130 Fsleep_for (make_number (2), Qnil);
3134 DEFUN ("load-average", Fload_average, Sload_average, 0, 1, 0,
3135 doc: /* Return list of 1 minute, 5 minute and 15 minute load averages.
3137 Each of the three load averages is multiplied by 100, then converted
3138 to integer.
3140 When USE-FLOATS is non-nil, floats will be used instead of integers.
3141 These floats are not multiplied by 100.
3143 If the 5-minute or 15-minute load averages are not available, return a
3144 shortened list, containing only those averages which are available. */)
3145 (use_floats)
3146 Lisp_Object use_floats;
3148 double load_ave[3];
3149 int loads = getloadavg (load_ave, 3);
3150 Lisp_Object ret = Qnil;
3152 if (loads < 0)
3153 error ("load-average not implemented for this operating system");
3155 while (loads-- > 0)
3157 Lisp_Object load = (NILP (use_floats) ?
3158 make_number ((int) (100.0 * load_ave[loads]))
3159 : make_float (load_ave[loads]));
3160 ret = Fcons (load, ret);
3163 return ret;
3166 Lisp_Object Vfeatures, Qsubfeatures;
3167 extern Lisp_Object Vafter_load_alist;
3169 DEFUN ("featurep", Ffeaturep, Sfeaturep, 1, 2, 0,
3170 doc: /* Returns t if FEATURE is present in this Emacs.
3172 Use this to conditionalize execution of lisp code based on the
3173 presence or absence of emacs or environment extensions.
3174 Use `provide' to declare that a feature is available. This function
3175 looks at the value of the variable `features'. The optional argument
3176 SUBFEATURE can be used to check a specific subfeature of FEATURE. */)
3177 (feature, subfeature)
3178 Lisp_Object feature, subfeature;
3180 register Lisp_Object tem;
3181 CHECK_SYMBOL (feature);
3182 tem = Fmemq (feature, Vfeatures);
3183 if (!NILP (tem) && !NILP (subfeature))
3184 tem = Fmember (subfeature, Fget (feature, Qsubfeatures));
3185 return (NILP (tem)) ? Qnil : Qt;
3188 DEFUN ("provide", Fprovide, Sprovide, 1, 2, 0,
3189 doc: /* Announce that FEATURE is a feature of the current Emacs.
3190 The optional argument SUBFEATURES should be a list of symbols listing
3191 particular subfeatures supported in this version of FEATURE. */)
3192 (feature, subfeatures)
3193 Lisp_Object feature, subfeatures;
3195 register Lisp_Object tem;
3196 CHECK_SYMBOL (feature);
3197 CHECK_LIST (subfeatures);
3198 if (!NILP (Vautoload_queue))
3199 Vautoload_queue = Fcons (Fcons (Vfeatures, Qnil), Vautoload_queue);
3200 tem = Fmemq (feature, Vfeatures);
3201 if (NILP (tem))
3202 Vfeatures = Fcons (feature, Vfeatures);
3203 if (!NILP (subfeatures))
3204 Fput (feature, Qsubfeatures, subfeatures);
3205 LOADHIST_ATTACH (Fcons (Qprovide, feature));
3207 /* Run any load-hooks for this file. */
3208 tem = Fassq (feature, Vafter_load_alist);
3209 if (CONSP (tem))
3210 Fprogn (XCDR (tem));
3212 return feature;
3215 /* `require' and its subroutines. */
3217 /* List of features currently being require'd, innermost first. */
3219 Lisp_Object require_nesting_list;
3221 Lisp_Object
3222 require_unwind (old_value)
3223 Lisp_Object old_value;
3225 return require_nesting_list = old_value;
3228 DEFUN ("require", Frequire, Srequire, 1, 3, 0,
3229 doc: /* If feature FEATURE is not loaded, load it from FILENAME.
3230 If FEATURE is not a member of the list `features', then the feature
3231 is not loaded; so load the file FILENAME.
3232 If FILENAME is omitted, the printname of FEATURE is used as the file name,
3233 and `load' will try to load this name appended with the suffix `.elc',
3234 `.el' or the unmodified name, in that order.
3235 If the optional third argument NOERROR is non-nil,
3236 then return nil if the file is not found instead of signaling an error.
3237 Normally the return value is FEATURE.
3238 The normal messages at start and end of loading FILENAME are suppressed. */)
3239 (feature, filename, noerror)
3240 Lisp_Object feature, filename, noerror;
3242 register Lisp_Object tem;
3243 struct gcpro gcpro1, gcpro2;
3245 CHECK_SYMBOL (feature);
3247 tem = Fmemq (feature, Vfeatures);
3249 if (NILP (tem))
3251 int count = SPECPDL_INDEX ();
3252 int nesting = 0;
3254 LOADHIST_ATTACH (Fcons (Qrequire, feature));
3256 /* This is to make sure that loadup.el gives a clear picture
3257 of what files are preloaded and when. */
3258 if (! NILP (Vpurify_flag))
3259 error ("(require %s) while preparing to dump",
3260 SDATA (SYMBOL_NAME (feature)));
3262 /* A certain amount of recursive `require' is legitimate,
3263 but if we require the same feature recursively 3 times,
3264 signal an error. */
3265 tem = require_nesting_list;
3266 while (! NILP (tem))
3268 if (! NILP (Fequal (feature, XCAR (tem))))
3269 nesting++;
3270 tem = XCDR (tem);
3272 if (nesting > 3)
3273 error ("Recursive `require' for feature `%s'",
3274 SDATA (SYMBOL_NAME (feature)));
3276 /* Update the list for any nested `require's that occur. */
3277 record_unwind_protect (require_unwind, require_nesting_list);
3278 require_nesting_list = Fcons (feature, require_nesting_list);
3280 /* Value saved here is to be restored into Vautoload_queue */
3281 record_unwind_protect (un_autoload, Vautoload_queue);
3282 Vautoload_queue = Qt;
3284 /* Load the file. */
3285 GCPRO2 (feature, filename);
3286 tem = Fload (NILP (filename) ? Fsymbol_name (feature) : filename,
3287 noerror, Qt, Qnil, (NILP (filename) ? Qt : Qnil));
3288 UNGCPRO;
3290 /* If load failed entirely, return nil. */
3291 if (NILP (tem))
3292 return unbind_to (count, Qnil);
3294 tem = Fmemq (feature, Vfeatures);
3295 if (NILP (tem))
3296 error ("Required feature `%s' was not provided",
3297 SDATA (SYMBOL_NAME (feature)));
3299 /* Once loading finishes, don't undo it. */
3300 Vautoload_queue = Qt;
3301 feature = unbind_to (count, feature);
3304 return feature;
3307 /* Primitives for work of the "widget" library.
3308 In an ideal world, this section would not have been necessary.
3309 However, lisp function calls being as slow as they are, it turns
3310 out that some functions in the widget library (wid-edit.el) are the
3311 bottleneck of Widget operation. Here is their translation to C,
3312 for the sole reason of efficiency. */
3314 DEFUN ("plist-member", Fplist_member, Splist_member, 2, 2, 0,
3315 doc: /* Return non-nil if PLIST has the property PROP.
3316 PLIST is a property list, which is a list of the form
3317 \(PROP1 VALUE1 PROP2 VALUE2 ...\). PROP is a symbol.
3318 Unlike `plist-get', this allows you to distinguish between a missing
3319 property and a property with the value nil.
3320 The value is actually the tail of PLIST whose car is PROP. */)
3321 (plist, prop)
3322 Lisp_Object plist, prop;
3324 while (CONSP (plist) && !EQ (XCAR (plist), prop))
3326 QUIT;
3327 plist = XCDR (plist);
3328 plist = CDR (plist);
3330 return plist;
3333 DEFUN ("widget-put", Fwidget_put, Swidget_put, 3, 3, 0,
3334 doc: /* In WIDGET, set PROPERTY to VALUE.
3335 The value can later be retrieved with `widget-get'. */)
3336 (widget, property, value)
3337 Lisp_Object widget, property, value;
3339 CHECK_CONS (widget);
3340 XSETCDR (widget, Fplist_put (XCDR (widget), property, value));
3341 return value;
3344 DEFUN ("widget-get", Fwidget_get, Swidget_get, 2, 2, 0,
3345 doc: /* In WIDGET, get the value of PROPERTY.
3346 The value could either be specified when the widget was created, or
3347 later with `widget-put'. */)
3348 (widget, property)
3349 Lisp_Object widget, property;
3351 Lisp_Object tmp;
3353 while (1)
3355 if (NILP (widget))
3356 return Qnil;
3357 CHECK_CONS (widget);
3358 tmp = Fplist_member (XCDR (widget), property);
3359 if (CONSP (tmp))
3361 tmp = XCDR (tmp);
3362 return CAR (tmp);
3364 tmp = XCAR (widget);
3365 if (NILP (tmp))
3366 return Qnil;
3367 widget = Fget (tmp, Qwidget_type);
3371 DEFUN ("widget-apply", Fwidget_apply, Swidget_apply, 2, MANY, 0,
3372 doc: /* Apply the value of WIDGET's PROPERTY to the widget itself.
3373 ARGS are passed as extra arguments to the function.
3374 usage: (widget-apply WIDGET PROPERTY &rest ARGS) */)
3375 (nargs, args)
3376 int nargs;
3377 Lisp_Object *args;
3379 /* This function can GC. */
3380 Lisp_Object newargs[3];
3381 struct gcpro gcpro1, gcpro2;
3382 Lisp_Object result;
3384 newargs[0] = Fwidget_get (args[0], args[1]);
3385 newargs[1] = args[0];
3386 newargs[2] = Flist (nargs - 2, args + 2);
3387 GCPRO2 (newargs[0], newargs[2]);
3388 result = Fapply (3, newargs);
3389 UNGCPRO;
3390 return result;
3393 /* base64 encode/decode functions (RFC 2045).
3394 Based on code from GNU recode. */
3396 #define MIME_LINE_LENGTH 76
3398 #define IS_ASCII(Character) \
3399 ((Character) < 128)
3400 #define IS_BASE64(Character) \
3401 (IS_ASCII (Character) && base64_char_to_value[Character] >= 0)
3402 #define IS_BASE64_IGNORABLE(Character) \
3403 ((Character) == ' ' || (Character) == '\t' || (Character) == '\n' \
3404 || (Character) == '\f' || (Character) == '\r')
3406 /* Used by base64_decode_1 to retrieve a non-base64-ignorable
3407 character or return retval if there are no characters left to
3408 process. */
3409 #define READ_QUADRUPLET_BYTE(retval) \
3410 do \
3412 if (i == length) \
3414 if (nchars_return) \
3415 *nchars_return = nchars; \
3416 return (retval); \
3418 c = from[i++]; \
3420 while (IS_BASE64_IGNORABLE (c))
3422 /* Don't use alloca for regions larger than this, lest we overflow
3423 their stack. */
3424 #define MAX_ALLOCA 16*1024
3426 /* Table of characters coding the 64 values. */
3427 static char base64_value_to_char[64] =
3429 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 0- 9 */
3430 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 10-19 */
3431 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', /* 20-29 */
3432 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', /* 30-39 */
3433 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', /* 40-49 */
3434 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', /* 50-59 */
3435 '8', '9', '+', '/' /* 60-63 */
3438 /* Table of base64 values for first 128 characters. */
3439 static short base64_char_to_value[128] =
3441 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0- 9 */
3442 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10- 19 */
3443 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20- 29 */
3444 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 30- 39 */
3445 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, /* 40- 49 */
3446 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, /* 50- 59 */
3447 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, /* 60- 69 */
3448 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 70- 79 */
3449 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, /* 80- 89 */
3450 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, /* 90- 99 */
3451 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, /* 100-109 */
3452 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, /* 110-119 */
3453 49, 50, 51, -1, -1, -1, -1, -1 /* 120-127 */
3456 /* The following diagram shows the logical steps by which three octets
3457 get transformed into four base64 characters.
3459 .--------. .--------. .--------.
3460 |aaaaaabb| |bbbbcccc| |ccdddddd|
3461 `--------' `--------' `--------'
3462 6 2 4 4 2 6
3463 .--------+--------+--------+--------.
3464 |00aaaaaa|00bbbbbb|00cccccc|00dddddd|
3465 `--------+--------+--------+--------'
3467 .--------+--------+--------+--------.
3468 |AAAAAAAA|BBBBBBBB|CCCCCCCC|DDDDDDDD|
3469 `--------+--------+--------+--------'
3471 The octets are divided into 6 bit chunks, which are then encoded into
3472 base64 characters. */
3475 static int base64_encode_1 P_ ((const char *, char *, int, int, int));
3476 static int base64_decode_1 P_ ((const char *, char *, int, int, int *));
3478 DEFUN ("base64-encode-region", Fbase64_encode_region, Sbase64_encode_region,
3479 2, 3, "r",
3480 doc: /* Base64-encode the region between BEG and END.
3481 Return the length of the encoded text.
3482 Optional third argument NO-LINE-BREAK means do not break long lines
3483 into shorter lines. */)
3484 (beg, end, no_line_break)
3485 Lisp_Object beg, end, no_line_break;
3487 char *encoded;
3488 int allength, length;
3489 int ibeg, iend, encoded_length;
3490 int old_pos = PT;
3492 validate_region (&beg, &end);
3494 ibeg = CHAR_TO_BYTE (XFASTINT (beg));
3495 iend = CHAR_TO_BYTE (XFASTINT (end));
3496 move_gap_both (XFASTINT (beg), ibeg);
3498 /* We need to allocate enough room for encoding the text.
3499 We need 33 1/3% more space, plus a newline every 76
3500 characters, and then we round up. */
3501 length = iend - ibeg;
3502 allength = length + length/3 + 1;
3503 allength += allength / MIME_LINE_LENGTH + 1 + 6;
3505 if (allength <= MAX_ALLOCA)
3506 encoded = (char *) alloca (allength);
3507 else
3508 encoded = (char *) xmalloc (allength);
3509 encoded_length = base64_encode_1 (BYTE_POS_ADDR (ibeg), encoded, length,
3510 NILP (no_line_break),
3511 !NILP (current_buffer->enable_multibyte_characters));
3512 if (encoded_length > allength)
3513 abort ();
3515 if (encoded_length < 0)
3517 /* The encoding wasn't possible. */
3518 if (length > MAX_ALLOCA)
3519 xfree (encoded);
3520 error ("Multibyte character in data for base64 encoding");
3523 /* Now we have encoded the region, so we insert the new contents
3524 and delete the old. (Insert first in order to preserve markers.) */
3525 SET_PT_BOTH (XFASTINT (beg), ibeg);
3526 insert (encoded, encoded_length);
3527 if (allength > MAX_ALLOCA)
3528 xfree (encoded);
3529 del_range_byte (ibeg + encoded_length, iend + encoded_length, 1);
3531 /* If point was outside of the region, restore it exactly; else just
3532 move to the beginning of the region. */
3533 if (old_pos >= XFASTINT (end))
3534 old_pos += encoded_length - (XFASTINT (end) - XFASTINT (beg));
3535 else if (old_pos > XFASTINT (beg))
3536 old_pos = XFASTINT (beg);
3537 SET_PT (old_pos);
3539 /* We return the length of the encoded text. */
3540 return make_number (encoded_length);
3543 DEFUN ("base64-encode-string", Fbase64_encode_string, Sbase64_encode_string,
3544 1, 2, 0,
3545 doc: /* Base64-encode STRING and return the result.
3546 Optional second argument NO-LINE-BREAK means do not break long lines
3547 into shorter lines. */)
3548 (string, no_line_break)
3549 Lisp_Object string, no_line_break;
3551 int allength, length, encoded_length;
3552 char *encoded;
3553 Lisp_Object encoded_string;
3555 CHECK_STRING (string);
3557 /* We need to allocate enough room for encoding the text.
3558 We need 33 1/3% more space, plus a newline every 76
3559 characters, and then we round up. */
3560 length = SBYTES (string);
3561 allength = length + length/3 + 1;
3562 allength += allength / MIME_LINE_LENGTH + 1 + 6;
3564 /* We need to allocate enough room for decoding the text. */
3565 if (allength <= MAX_ALLOCA)
3566 encoded = (char *) alloca (allength);
3567 else
3568 encoded = (char *) xmalloc (allength);
3570 encoded_length = base64_encode_1 (SDATA (string),
3571 encoded, length, NILP (no_line_break),
3572 STRING_MULTIBYTE (string));
3573 if (encoded_length > allength)
3574 abort ();
3576 if (encoded_length < 0)
3578 /* The encoding wasn't possible. */
3579 if (length > MAX_ALLOCA)
3580 xfree (encoded);
3581 error ("Multibyte character in data for base64 encoding");
3584 encoded_string = make_unibyte_string (encoded, encoded_length);
3585 if (allength > MAX_ALLOCA)
3586 xfree (encoded);
3588 return encoded_string;
3591 static int
3592 base64_encode_1 (from, to, length, line_break, multibyte)
3593 const char *from;
3594 char *to;
3595 int length;
3596 int line_break;
3597 int multibyte;
3599 int counter = 0, i = 0;
3600 char *e = to;
3601 int c;
3602 unsigned int value;
3603 int bytes;
3605 while (i < length)
3607 if (multibyte)
3609 c = STRING_CHAR_AND_LENGTH (from + i, length - i, bytes);
3610 if (c >= 256)
3611 return -1;
3612 i += bytes;
3614 else
3615 c = from[i++];
3617 /* Wrap line every 76 characters. */
3619 if (line_break)
3621 if (counter < MIME_LINE_LENGTH / 4)
3622 counter++;
3623 else
3625 *e++ = '\n';
3626 counter = 1;
3630 /* Process first byte of a triplet. */
3632 *e++ = base64_value_to_char[0x3f & c >> 2];
3633 value = (0x03 & c) << 4;
3635 /* Process second byte of a triplet. */
3637 if (i == length)
3639 *e++ = base64_value_to_char[value];
3640 *e++ = '=';
3641 *e++ = '=';
3642 break;
3645 if (multibyte)
3647 c = STRING_CHAR_AND_LENGTH (from + i, length - i, bytes);
3648 if (c >= 256)
3649 return -1;
3650 i += bytes;
3652 else
3653 c = from[i++];
3655 *e++ = base64_value_to_char[value | (0x0f & c >> 4)];
3656 value = (0x0f & c) << 2;
3658 /* Process third byte of a triplet. */
3660 if (i == length)
3662 *e++ = base64_value_to_char[value];
3663 *e++ = '=';
3664 break;
3667 if (multibyte)
3669 c = STRING_CHAR_AND_LENGTH (from + i, length - i, bytes);
3670 if (c >= 256)
3671 return -1;
3672 i += bytes;
3674 else
3675 c = from[i++];
3677 *e++ = base64_value_to_char[value | (0x03 & c >> 6)];
3678 *e++ = base64_value_to_char[0x3f & c];
3681 return e - to;
3685 DEFUN ("base64-decode-region", Fbase64_decode_region, Sbase64_decode_region,
3686 2, 2, "r",
3687 doc: /* Base64-decode the region between BEG and END.
3688 Return the length of the decoded text.
3689 If the region can't be decoded, signal an error and don't modify the buffer. */)
3690 (beg, end)
3691 Lisp_Object beg, end;
3693 int ibeg, iend, length, allength;
3694 char *decoded;
3695 int old_pos = PT;
3696 int decoded_length;
3697 int inserted_chars;
3698 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
3700 validate_region (&beg, &end);
3702 ibeg = CHAR_TO_BYTE (XFASTINT (beg));
3703 iend = CHAR_TO_BYTE (XFASTINT (end));
3705 length = iend - ibeg;
3707 /* We need to allocate enough room for decoding the text. If we are
3708 working on a multibyte buffer, each decoded code may occupy at
3709 most two bytes. */
3710 allength = multibyte ? length * 2 : length;
3711 if (allength <= MAX_ALLOCA)
3712 decoded = (char *) alloca (allength);
3713 else
3714 decoded = (char *) xmalloc (allength);
3716 move_gap_both (XFASTINT (beg), ibeg);
3717 decoded_length = base64_decode_1 (BYTE_POS_ADDR (ibeg), decoded, length,
3718 multibyte, &inserted_chars);
3719 if (decoded_length > allength)
3720 abort ();
3722 if (decoded_length < 0)
3724 /* The decoding wasn't possible. */
3725 if (allength > MAX_ALLOCA)
3726 xfree (decoded);
3727 error ("Invalid base64 data");
3730 /* Now we have decoded the region, so we insert the new contents
3731 and delete the old. (Insert first in order to preserve markers.) */
3732 TEMP_SET_PT_BOTH (XFASTINT (beg), ibeg);
3733 insert_1_both (decoded, inserted_chars, decoded_length, 0, 1, 0);
3734 if (allength > MAX_ALLOCA)
3735 xfree (decoded);
3736 /* Delete the original text. */
3737 del_range_both (PT, PT_BYTE, XFASTINT (end) + inserted_chars,
3738 iend + decoded_length, 1);
3740 /* If point was outside of the region, restore it exactly; else just
3741 move to the beginning of the region. */
3742 if (old_pos >= XFASTINT (end))
3743 old_pos += inserted_chars - (XFASTINT (end) - XFASTINT (beg));
3744 else if (old_pos > XFASTINT (beg))
3745 old_pos = XFASTINT (beg);
3746 SET_PT (old_pos > ZV ? ZV : old_pos);
3748 return make_number (inserted_chars);
3751 DEFUN ("base64-decode-string", Fbase64_decode_string, Sbase64_decode_string,
3752 1, 1, 0,
3753 doc: /* Base64-decode STRING and return the result. */)
3754 (string)
3755 Lisp_Object string;
3757 char *decoded;
3758 int length, decoded_length;
3759 Lisp_Object decoded_string;
3761 CHECK_STRING (string);
3763 length = SBYTES (string);
3764 /* We need to allocate enough room for decoding the text. */
3765 if (length <= MAX_ALLOCA)
3766 decoded = (char *) alloca (length);
3767 else
3768 decoded = (char *) xmalloc (length);
3770 /* The decoded result should be unibyte. */
3771 decoded_length = base64_decode_1 (SDATA (string), decoded, length,
3772 0, NULL);
3773 if (decoded_length > length)
3774 abort ();
3775 else if (decoded_length >= 0)
3776 decoded_string = make_unibyte_string (decoded, decoded_length);
3777 else
3778 decoded_string = Qnil;
3780 if (length > MAX_ALLOCA)
3781 xfree (decoded);
3782 if (!STRINGP (decoded_string))
3783 error ("Invalid base64 data");
3785 return decoded_string;
3788 /* Base64-decode the data at FROM of LENGHT bytes into TO. If
3789 MULTIBYTE is nonzero, the decoded result should be in multibyte
3790 form. If NCHARS_RETRUN is not NULL, store the number of produced
3791 characters in *NCHARS_RETURN. */
3793 static int
3794 base64_decode_1 (from, to, length, multibyte, nchars_return)
3795 const char *from;
3796 char *to;
3797 int length;
3798 int multibyte;
3799 int *nchars_return;
3801 int i = 0;
3802 char *e = to;
3803 unsigned char c;
3804 unsigned long value;
3805 int nchars = 0;
3807 while (1)
3809 /* Process first byte of a quadruplet. */
3811 READ_QUADRUPLET_BYTE (e-to);
3813 if (!IS_BASE64 (c))
3814 return -1;
3815 value = base64_char_to_value[c] << 18;
3817 /* Process second byte of a quadruplet. */
3819 READ_QUADRUPLET_BYTE (-1);
3821 if (!IS_BASE64 (c))
3822 return -1;
3823 value |= base64_char_to_value[c] << 12;
3825 c = (unsigned char) (value >> 16);
3826 if (multibyte)
3827 e += CHAR_STRING (c, e);
3828 else
3829 *e++ = c;
3830 nchars++;
3832 /* Process third byte of a quadruplet. */
3834 READ_QUADRUPLET_BYTE (-1);
3836 if (c == '=')
3838 READ_QUADRUPLET_BYTE (-1);
3840 if (c != '=')
3841 return -1;
3842 continue;
3845 if (!IS_BASE64 (c))
3846 return -1;
3847 value |= base64_char_to_value[c] << 6;
3849 c = (unsigned char) (0xff & value >> 8);
3850 if (multibyte)
3851 e += CHAR_STRING (c, e);
3852 else
3853 *e++ = c;
3854 nchars++;
3856 /* Process fourth byte of a quadruplet. */
3858 READ_QUADRUPLET_BYTE (-1);
3860 if (c == '=')
3861 continue;
3863 if (!IS_BASE64 (c))
3864 return -1;
3865 value |= base64_char_to_value[c];
3867 c = (unsigned char) (0xff & value);
3868 if (multibyte)
3869 e += CHAR_STRING (c, e);
3870 else
3871 *e++ = c;
3872 nchars++;
3878 /***********************************************************************
3879 ***** *****
3880 ***** Hash Tables *****
3881 ***** *****
3882 ***********************************************************************/
3884 /* Implemented by gerd@gnu.org. This hash table implementation was
3885 inspired by CMUCL hash tables. */
3887 /* Ideas:
3889 1. For small tables, association lists are probably faster than
3890 hash tables because they have lower overhead.
3892 For uses of hash tables where the O(1) behavior of table
3893 operations is not a requirement, it might therefore be a good idea
3894 not to hash. Instead, we could just do a linear search in the
3895 key_and_value vector of the hash table. This could be done
3896 if a `:linear-search t' argument is given to make-hash-table. */
3899 /* The list of all weak hash tables. Don't staticpro this one. */
3901 Lisp_Object Vweak_hash_tables;
3903 /* Various symbols. */
3905 Lisp_Object Qhash_table_p, Qeq, Qeql, Qequal, Qkey, Qvalue;
3906 Lisp_Object QCtest, QCsize, QCrehash_size, QCrehash_threshold, QCweakness;
3907 Lisp_Object Qhash_table_test, Qkey_or_value, Qkey_and_value;
3909 /* Function prototypes. */
3911 static struct Lisp_Hash_Table *check_hash_table P_ ((Lisp_Object));
3912 static int get_key_arg P_ ((Lisp_Object, int, Lisp_Object *, char *));
3913 static void maybe_resize_hash_table P_ ((struct Lisp_Hash_Table *));
3914 static int cmpfn_eql P_ ((struct Lisp_Hash_Table *, Lisp_Object, unsigned,
3915 Lisp_Object, unsigned));
3916 static int cmpfn_equal P_ ((struct Lisp_Hash_Table *, Lisp_Object, unsigned,
3917 Lisp_Object, unsigned));
3918 static int cmpfn_user_defined P_ ((struct Lisp_Hash_Table *, Lisp_Object,
3919 unsigned, Lisp_Object, unsigned));
3920 static unsigned hashfn_eq P_ ((struct Lisp_Hash_Table *, Lisp_Object));
3921 static unsigned hashfn_eql P_ ((struct Lisp_Hash_Table *, Lisp_Object));
3922 static unsigned hashfn_equal P_ ((struct Lisp_Hash_Table *, Lisp_Object));
3923 static unsigned hashfn_user_defined P_ ((struct Lisp_Hash_Table *,
3924 Lisp_Object));
3925 static unsigned sxhash_string P_ ((unsigned char *, int));
3926 static unsigned sxhash_list P_ ((Lisp_Object, int));
3927 static unsigned sxhash_vector P_ ((Lisp_Object, int));
3928 static unsigned sxhash_bool_vector P_ ((Lisp_Object));
3929 static int sweep_weak_table P_ ((struct Lisp_Hash_Table *, int));
3933 /***********************************************************************
3934 Utilities
3935 ***********************************************************************/
3937 /* If OBJ is a Lisp hash table, return a pointer to its struct
3938 Lisp_Hash_Table. Otherwise, signal an error. */
3940 static struct Lisp_Hash_Table *
3941 check_hash_table (obj)
3942 Lisp_Object obj;
3944 CHECK_HASH_TABLE (obj);
3945 return XHASH_TABLE (obj);
3949 /* Value is the next integer I >= N, N >= 0 which is "almost" a prime
3950 number. */
3953 next_almost_prime (n)
3954 int n;
3956 if (n % 2 == 0)
3957 n += 1;
3958 if (n % 3 == 0)
3959 n += 2;
3960 if (n % 7 == 0)
3961 n += 4;
3962 return n;
3966 /* Find KEY in ARGS which has size NARGS. Don't consider indices for
3967 which USED[I] is non-zero. If found at index I in ARGS, set
3968 USED[I] and USED[I + 1] to 1, and return I + 1. Otherwise return
3969 -1. This function is used to extract a keyword/argument pair from
3970 a DEFUN parameter list. */
3972 static int
3973 get_key_arg (key, nargs, args, used)
3974 Lisp_Object key;
3975 int nargs;
3976 Lisp_Object *args;
3977 char *used;
3979 int i;
3981 for (i = 0; i < nargs - 1; ++i)
3982 if (!used[i] && EQ (args[i], key))
3983 break;
3985 if (i >= nargs - 1)
3986 i = -1;
3987 else
3989 used[i++] = 1;
3990 used[i] = 1;
3993 return i;
3997 /* Return a Lisp vector which has the same contents as VEC but has
3998 size NEW_SIZE, NEW_SIZE >= VEC->size. Entries in the resulting
3999 vector that are not copied from VEC are set to INIT. */
4001 Lisp_Object
4002 larger_vector (vec, new_size, init)
4003 Lisp_Object vec;
4004 int new_size;
4005 Lisp_Object init;
4007 struct Lisp_Vector *v;
4008 int i, old_size;
4010 xassert (VECTORP (vec));
4011 old_size = XVECTOR (vec)->size;
4012 xassert (new_size >= old_size);
4014 v = allocate_vector (new_size);
4015 bcopy (XVECTOR (vec)->contents, v->contents,
4016 old_size * sizeof *v->contents);
4017 for (i = old_size; i < new_size; ++i)
4018 v->contents[i] = init;
4019 XSETVECTOR (vec, v);
4020 return vec;
4024 /***********************************************************************
4025 Low-level Functions
4026 ***********************************************************************/
4028 /* Compare KEY1 which has hash code HASH1 and KEY2 with hash code
4029 HASH2 in hash table H using `eql'. Value is non-zero if KEY1 and
4030 KEY2 are the same. */
4032 static int
4033 cmpfn_eql (h, key1, hash1, key2, hash2)
4034 struct Lisp_Hash_Table *h;
4035 Lisp_Object key1, key2;
4036 unsigned hash1, hash2;
4038 return (FLOATP (key1)
4039 && FLOATP (key2)
4040 && XFLOAT_DATA (key1) == XFLOAT_DATA (key2));
4044 /* Compare KEY1 which has hash code HASH1 and KEY2 with hash code
4045 HASH2 in hash table H using `equal'. Value is non-zero if KEY1 and
4046 KEY2 are the same. */
4048 static int
4049 cmpfn_equal (h, key1, hash1, key2, hash2)
4050 struct Lisp_Hash_Table *h;
4051 Lisp_Object key1, key2;
4052 unsigned hash1, hash2;
4054 return hash1 == hash2 && !NILP (Fequal (key1, key2));
4058 /* Compare KEY1 which has hash code HASH1, and KEY2 with hash code
4059 HASH2 in hash table H using H->user_cmp_function. Value is non-zero
4060 if KEY1 and KEY2 are the same. */
4062 static int
4063 cmpfn_user_defined (h, key1, hash1, key2, hash2)
4064 struct Lisp_Hash_Table *h;
4065 Lisp_Object key1, key2;
4066 unsigned hash1, hash2;
4068 if (hash1 == hash2)
4070 Lisp_Object args[3];
4072 args[0] = h->user_cmp_function;
4073 args[1] = key1;
4074 args[2] = key2;
4075 return !NILP (Ffuncall (3, args));
4077 else
4078 return 0;
4082 /* Value is a hash code for KEY for use in hash table H which uses
4083 `eq' to compare keys. The hash code returned is guaranteed to fit
4084 in a Lisp integer. */
4086 static unsigned
4087 hashfn_eq (h, key)
4088 struct Lisp_Hash_Table *h;
4089 Lisp_Object key;
4091 unsigned hash = XUINT (key) ^ XGCTYPE (key);
4092 xassert ((hash & ~VALMASK) == 0);
4093 return hash;
4097 /* Value is a hash code for KEY for use in hash table H which uses
4098 `eql' to compare keys. The hash code returned is guaranteed to fit
4099 in a Lisp integer. */
4101 static unsigned
4102 hashfn_eql (h, key)
4103 struct Lisp_Hash_Table *h;
4104 Lisp_Object key;
4106 unsigned hash;
4107 if (FLOATP (key))
4108 hash = sxhash (key, 0);
4109 else
4110 hash = XUINT (key) ^ XGCTYPE (key);
4111 xassert ((hash & ~VALMASK) == 0);
4112 return hash;
4116 /* Value is a hash code for KEY for use in hash table H which uses
4117 `equal' to compare keys. The hash code returned is guaranteed to fit
4118 in a Lisp integer. */
4120 static unsigned
4121 hashfn_equal (h, key)
4122 struct Lisp_Hash_Table *h;
4123 Lisp_Object key;
4125 unsigned hash = sxhash (key, 0);
4126 xassert ((hash & ~VALMASK) == 0);
4127 return hash;
4131 /* Value is a hash code for KEY for use in hash table H which uses as
4132 user-defined function to compare keys. The hash code returned is
4133 guaranteed to fit in a Lisp integer. */
4135 static unsigned
4136 hashfn_user_defined (h, key)
4137 struct Lisp_Hash_Table *h;
4138 Lisp_Object key;
4140 Lisp_Object args[2], hash;
4142 args[0] = h->user_hash_function;
4143 args[1] = key;
4144 hash = Ffuncall (2, args);
4145 if (!INTEGERP (hash))
4146 Fsignal (Qerror,
4147 list2 (build_string ("Invalid hash code returned from \
4148 user-supplied hash function"),
4149 hash));
4150 return XUINT (hash);
4154 /* Create and initialize a new hash table.
4156 TEST specifies the test the hash table will use to compare keys.
4157 It must be either one of the predefined tests `eq', `eql' or
4158 `equal' or a symbol denoting a user-defined test named TEST with
4159 test and hash functions USER_TEST and USER_HASH.
4161 Give the table initial capacity SIZE, SIZE >= 0, an integer.
4163 If REHASH_SIZE is an integer, it must be > 0, and this hash table's
4164 new size when it becomes full is computed by adding REHASH_SIZE to
4165 its old size. If REHASH_SIZE is a float, it must be > 1.0, and the
4166 table's new size is computed by multiplying its old size with
4167 REHASH_SIZE.
4169 REHASH_THRESHOLD must be a float <= 1.0, and > 0. The table will
4170 be resized when the ratio of (number of entries in the table) /
4171 (table size) is >= REHASH_THRESHOLD.
4173 WEAK specifies the weakness of the table. If non-nil, it must be
4174 one of the symbols `key', `value', `key-or-value', or `key-and-value'. */
4176 Lisp_Object
4177 make_hash_table (test, size, rehash_size, rehash_threshold, weak,
4178 user_test, user_hash)
4179 Lisp_Object test, size, rehash_size, rehash_threshold, weak;
4180 Lisp_Object user_test, user_hash;
4182 struct Lisp_Hash_Table *h;
4183 Lisp_Object table;
4184 int index_size, i, sz;
4186 /* Preconditions. */
4187 xassert (SYMBOLP (test));
4188 xassert (INTEGERP (size) && XINT (size) >= 0);
4189 xassert ((INTEGERP (rehash_size) && XINT (rehash_size) > 0)
4190 || (FLOATP (rehash_size) && XFLOATINT (rehash_size) > 1.0));
4191 xassert (FLOATP (rehash_threshold)
4192 && XFLOATINT (rehash_threshold) > 0
4193 && XFLOATINT (rehash_threshold) <= 1.0);
4195 if (XFASTINT (size) == 0)
4196 size = make_number (1);
4198 /* Allocate a table and initialize it. */
4199 h = allocate_hash_table ();
4201 /* Initialize hash table slots. */
4202 sz = XFASTINT (size);
4204 h->test = test;
4205 if (EQ (test, Qeql))
4207 h->cmpfn = cmpfn_eql;
4208 h->hashfn = hashfn_eql;
4210 else if (EQ (test, Qeq))
4212 h->cmpfn = NULL;
4213 h->hashfn = hashfn_eq;
4215 else if (EQ (test, Qequal))
4217 h->cmpfn = cmpfn_equal;
4218 h->hashfn = hashfn_equal;
4220 else
4222 h->user_cmp_function = user_test;
4223 h->user_hash_function = user_hash;
4224 h->cmpfn = cmpfn_user_defined;
4225 h->hashfn = hashfn_user_defined;
4228 h->weak = weak;
4229 h->rehash_threshold = rehash_threshold;
4230 h->rehash_size = rehash_size;
4231 h->count = make_number (0);
4232 h->key_and_value = Fmake_vector (make_number (2 * sz), Qnil);
4233 h->hash = Fmake_vector (size, Qnil);
4234 h->next = Fmake_vector (size, Qnil);
4235 /* Cast to int here avoids losing with gcc 2.95 on Tru64/Alpha... */
4236 index_size = next_almost_prime ((int) (sz / XFLOATINT (rehash_threshold)));
4237 h->index = Fmake_vector (make_number (index_size), Qnil);
4239 /* Set up the free list. */
4240 for (i = 0; i < sz - 1; ++i)
4241 HASH_NEXT (h, i) = make_number (i + 1);
4242 h->next_free = make_number (0);
4244 XSET_HASH_TABLE (table, h);
4245 xassert (HASH_TABLE_P (table));
4246 xassert (XHASH_TABLE (table) == h);
4248 /* Maybe add this hash table to the list of all weak hash tables. */
4249 if (NILP (h->weak))
4250 h->next_weak = Qnil;
4251 else
4253 h->next_weak = Vweak_hash_tables;
4254 Vweak_hash_tables = table;
4257 return table;
4261 /* Return a copy of hash table H1. Keys and values are not copied,
4262 only the table itself is. */
4264 Lisp_Object
4265 copy_hash_table (h1)
4266 struct Lisp_Hash_Table *h1;
4268 Lisp_Object table;
4269 struct Lisp_Hash_Table *h2;
4270 struct Lisp_Vector *next;
4272 h2 = allocate_hash_table ();
4273 next = h2->vec_next;
4274 bcopy (h1, h2, sizeof *h2);
4275 h2->vec_next = next;
4276 h2->key_and_value = Fcopy_sequence (h1->key_and_value);
4277 h2->hash = Fcopy_sequence (h1->hash);
4278 h2->next = Fcopy_sequence (h1->next);
4279 h2->index = Fcopy_sequence (h1->index);
4280 XSET_HASH_TABLE (table, h2);
4282 /* Maybe add this hash table to the list of all weak hash tables. */
4283 if (!NILP (h2->weak))
4285 h2->next_weak = Vweak_hash_tables;
4286 Vweak_hash_tables = table;
4289 return table;
4293 /* Resize hash table H if it's too full. If H cannot be resized
4294 because it's already too large, throw an error. */
4296 static INLINE void
4297 maybe_resize_hash_table (h)
4298 struct Lisp_Hash_Table *h;
4300 if (NILP (h->next_free))
4302 int old_size = HASH_TABLE_SIZE (h);
4303 int i, new_size, index_size;
4305 if (INTEGERP (h->rehash_size))
4306 new_size = old_size + XFASTINT (h->rehash_size);
4307 else
4308 new_size = old_size * XFLOATINT (h->rehash_size);
4309 new_size = max (old_size + 1, new_size);
4310 index_size = next_almost_prime ((int)
4311 (new_size
4312 / XFLOATINT (h->rehash_threshold)));
4313 if (max (index_size, 2 * new_size) & ~VALMASK)
4314 error ("Hash table too large to resize");
4316 h->key_and_value = larger_vector (h->key_and_value, 2 * new_size, Qnil);
4317 h->next = larger_vector (h->next, new_size, Qnil);
4318 h->hash = larger_vector (h->hash, new_size, Qnil);
4319 h->index = Fmake_vector (make_number (index_size), Qnil);
4321 /* Update the free list. Do it so that new entries are added at
4322 the end of the free list. This makes some operations like
4323 maphash faster. */
4324 for (i = old_size; i < new_size - 1; ++i)
4325 HASH_NEXT (h, i) = make_number (i + 1);
4327 if (!NILP (h->next_free))
4329 Lisp_Object last, next;
4331 last = h->next_free;
4332 while (next = HASH_NEXT (h, XFASTINT (last)),
4333 !NILP (next))
4334 last = next;
4336 HASH_NEXT (h, XFASTINT (last)) = make_number (old_size);
4338 else
4339 XSETFASTINT (h->next_free, old_size);
4341 /* Rehash. */
4342 for (i = 0; i < old_size; ++i)
4343 if (!NILP (HASH_HASH (h, i)))
4345 unsigned hash_code = XUINT (HASH_HASH (h, i));
4346 int start_of_bucket = hash_code % XVECTOR (h->index)->size;
4347 HASH_NEXT (h, i) = HASH_INDEX (h, start_of_bucket);
4348 HASH_INDEX (h, start_of_bucket) = make_number (i);
4354 /* Lookup KEY in hash table H. If HASH is non-null, return in *HASH
4355 the hash code of KEY. Value is the index of the entry in H
4356 matching KEY, or -1 if not found. */
4359 hash_lookup (h, key, hash)
4360 struct Lisp_Hash_Table *h;
4361 Lisp_Object key;
4362 unsigned *hash;
4364 unsigned hash_code;
4365 int start_of_bucket;
4366 Lisp_Object idx;
4368 hash_code = h->hashfn (h, key);
4369 if (hash)
4370 *hash = hash_code;
4372 start_of_bucket = hash_code % XVECTOR (h->index)->size;
4373 idx = HASH_INDEX (h, start_of_bucket);
4375 /* We need not gcpro idx since it's either an integer or nil. */
4376 while (!NILP (idx))
4378 int i = XFASTINT (idx);
4379 if (EQ (key, HASH_KEY (h, i))
4380 || (h->cmpfn
4381 && h->cmpfn (h, key, hash_code,
4382 HASH_KEY (h, i), XUINT (HASH_HASH (h, i)))))
4383 break;
4384 idx = HASH_NEXT (h, i);
4387 return NILP (idx) ? -1 : XFASTINT (idx);
4391 /* Put an entry into hash table H that associates KEY with VALUE.
4392 HASH is a previously computed hash code of KEY.
4393 Value is the index of the entry in H matching KEY. */
4396 hash_put (h, key, value, hash)
4397 struct Lisp_Hash_Table *h;
4398 Lisp_Object key, value;
4399 unsigned hash;
4401 int start_of_bucket, i;
4403 xassert ((hash & ~VALMASK) == 0);
4405 /* Increment count after resizing because resizing may fail. */
4406 maybe_resize_hash_table (h);
4407 h->count = make_number (XFASTINT (h->count) + 1);
4409 /* Store key/value in the key_and_value vector. */
4410 i = XFASTINT (h->next_free);
4411 h->next_free = HASH_NEXT (h, i);
4412 HASH_KEY (h, i) = key;
4413 HASH_VALUE (h, i) = value;
4415 /* Remember its hash code. */
4416 HASH_HASH (h, i) = make_number (hash);
4418 /* Add new entry to its collision chain. */
4419 start_of_bucket = hash % XVECTOR (h->index)->size;
4420 HASH_NEXT (h, i) = HASH_INDEX (h, start_of_bucket);
4421 HASH_INDEX (h, start_of_bucket) = make_number (i);
4422 return i;
4426 /* Remove the entry matching KEY from hash table H, if there is one. */
4428 void
4429 hash_remove (h, key)
4430 struct Lisp_Hash_Table *h;
4431 Lisp_Object key;
4433 unsigned hash_code;
4434 int start_of_bucket;
4435 Lisp_Object idx, prev;
4437 hash_code = h->hashfn (h, key);
4438 start_of_bucket = hash_code % XVECTOR (h->index)->size;
4439 idx = HASH_INDEX (h, start_of_bucket);
4440 prev = Qnil;
4442 /* We need not gcpro idx, prev since they're either integers or nil. */
4443 while (!NILP (idx))
4445 int i = XFASTINT (idx);
4447 if (EQ (key, HASH_KEY (h, i))
4448 || (h->cmpfn
4449 && h->cmpfn (h, key, hash_code,
4450 HASH_KEY (h, i), XUINT (HASH_HASH (h, i)))))
4452 /* Take entry out of collision chain. */
4453 if (NILP (prev))
4454 HASH_INDEX (h, start_of_bucket) = HASH_NEXT (h, i);
4455 else
4456 HASH_NEXT (h, XFASTINT (prev)) = HASH_NEXT (h, i);
4458 /* Clear slots in key_and_value and add the slots to
4459 the free list. */
4460 HASH_KEY (h, i) = HASH_VALUE (h, i) = HASH_HASH (h, i) = Qnil;
4461 HASH_NEXT (h, i) = h->next_free;
4462 h->next_free = make_number (i);
4463 h->count = make_number (XFASTINT (h->count) - 1);
4464 xassert (XINT (h->count) >= 0);
4465 break;
4467 else
4469 prev = idx;
4470 idx = HASH_NEXT (h, i);
4476 /* Clear hash table H. */
4478 void
4479 hash_clear (h)
4480 struct Lisp_Hash_Table *h;
4482 if (XFASTINT (h->count) > 0)
4484 int i, size = HASH_TABLE_SIZE (h);
4486 for (i = 0; i < size; ++i)
4488 HASH_NEXT (h, i) = i < size - 1 ? make_number (i + 1) : Qnil;
4489 HASH_KEY (h, i) = Qnil;
4490 HASH_VALUE (h, i) = Qnil;
4491 HASH_HASH (h, i) = Qnil;
4494 for (i = 0; i < XVECTOR (h->index)->size; ++i)
4495 XVECTOR (h->index)->contents[i] = Qnil;
4497 h->next_free = make_number (0);
4498 h->count = make_number (0);
4504 /************************************************************************
4505 Weak Hash Tables
4506 ************************************************************************/
4508 /* Sweep weak hash table H. REMOVE_ENTRIES_P non-zero means remove
4509 entries from the table that don't survive the current GC.
4510 REMOVE_ENTRIES_P zero means mark entries that are in use. Value is
4511 non-zero if anything was marked. */
4513 static int
4514 sweep_weak_table (h, remove_entries_p)
4515 struct Lisp_Hash_Table *h;
4516 int remove_entries_p;
4518 int bucket, n, marked;
4520 n = XVECTOR (h->index)->size & ~ARRAY_MARK_FLAG;
4521 marked = 0;
4523 for (bucket = 0; bucket < n; ++bucket)
4525 Lisp_Object idx, next, prev;
4527 /* Follow collision chain, removing entries that
4528 don't survive this garbage collection. */
4529 prev = Qnil;
4530 for (idx = HASH_INDEX (h, bucket); !GC_NILP (idx); idx = next)
4532 int i = XFASTINT (idx);
4533 int key_known_to_survive_p = survives_gc_p (HASH_KEY (h, i));
4534 int value_known_to_survive_p = survives_gc_p (HASH_VALUE (h, i));
4535 int remove_p;
4537 if (EQ (h->weak, Qkey))
4538 remove_p = !key_known_to_survive_p;
4539 else if (EQ (h->weak, Qvalue))
4540 remove_p = !value_known_to_survive_p;
4541 else if (EQ (h->weak, Qkey_or_value))
4542 remove_p = !(key_known_to_survive_p || value_known_to_survive_p);
4543 else if (EQ (h->weak, Qkey_and_value))
4544 remove_p = !(key_known_to_survive_p && value_known_to_survive_p);
4545 else
4546 abort ();
4548 next = HASH_NEXT (h, i);
4550 if (remove_entries_p)
4552 if (remove_p)
4554 /* Take out of collision chain. */
4555 if (GC_NILP (prev))
4556 HASH_INDEX (h, bucket) = next;
4557 else
4558 HASH_NEXT (h, XFASTINT (prev)) = next;
4560 /* Add to free list. */
4561 HASH_NEXT (h, i) = h->next_free;
4562 h->next_free = idx;
4564 /* Clear key, value, and hash. */
4565 HASH_KEY (h, i) = HASH_VALUE (h, i) = Qnil;
4566 HASH_HASH (h, i) = Qnil;
4568 h->count = make_number (XFASTINT (h->count) - 1);
4571 else
4573 if (!remove_p)
4575 /* Make sure key and value survive. */
4576 if (!key_known_to_survive_p)
4578 mark_object (&HASH_KEY (h, i));
4579 marked = 1;
4582 if (!value_known_to_survive_p)
4584 mark_object (&HASH_VALUE (h, i));
4585 marked = 1;
4592 return marked;
4595 /* Remove elements from weak hash tables that don't survive the
4596 current garbage collection. Remove weak tables that don't survive
4597 from Vweak_hash_tables. Called from gc_sweep. */
4599 void
4600 sweep_weak_hash_tables ()
4602 Lisp_Object table, used, next;
4603 struct Lisp_Hash_Table *h;
4604 int marked;
4606 /* Mark all keys and values that are in use. Keep on marking until
4607 there is no more change. This is necessary for cases like
4608 value-weak table A containing an entry X -> Y, where Y is used in a
4609 key-weak table B, Z -> Y. If B comes after A in the list of weak
4610 tables, X -> Y might be removed from A, although when looking at B
4611 one finds that it shouldn't. */
4614 marked = 0;
4615 for (table = Vweak_hash_tables; !GC_NILP (table); table = h->next_weak)
4617 h = XHASH_TABLE (table);
4618 if (h->size & ARRAY_MARK_FLAG)
4619 marked |= sweep_weak_table (h, 0);
4622 while (marked);
4624 /* Remove tables and entries that aren't used. */
4625 for (table = Vweak_hash_tables, used = Qnil; !GC_NILP (table); table = next)
4627 h = XHASH_TABLE (table);
4628 next = h->next_weak;
4630 if (h->size & ARRAY_MARK_FLAG)
4632 /* TABLE is marked as used. Sweep its contents. */
4633 if (XFASTINT (h->count) > 0)
4634 sweep_weak_table (h, 1);
4636 /* Add table to the list of used weak hash tables. */
4637 h->next_weak = used;
4638 used = table;
4642 Vweak_hash_tables = used;
4647 /***********************************************************************
4648 Hash Code Computation
4649 ***********************************************************************/
4651 /* Maximum depth up to which to dive into Lisp structures. */
4653 #define SXHASH_MAX_DEPTH 3
4655 /* Maximum length up to which to take list and vector elements into
4656 account. */
4658 #define SXHASH_MAX_LEN 7
4660 /* Combine two integers X and Y for hashing. */
4662 #define SXHASH_COMBINE(X, Y) \
4663 ((((unsigned)(X) << 4) + (((unsigned)(X) >> 24) & 0x0fffffff)) \
4664 + (unsigned)(Y))
4667 /* Return a hash for string PTR which has length LEN. The hash
4668 code returned is guaranteed to fit in a Lisp integer. */
4670 static unsigned
4671 sxhash_string (ptr, len)
4672 unsigned char *ptr;
4673 int len;
4675 unsigned char *p = ptr;
4676 unsigned char *end = p + len;
4677 unsigned char c;
4678 unsigned hash = 0;
4680 while (p != end)
4682 c = *p++;
4683 if (c >= 0140)
4684 c -= 40;
4685 hash = ((hash << 3) + (hash >> 28) + c);
4688 return hash & VALMASK;
4692 /* Return a hash for list LIST. DEPTH is the current depth in the
4693 list. We don't recurse deeper than SXHASH_MAX_DEPTH in it. */
4695 static unsigned
4696 sxhash_list (list, depth)
4697 Lisp_Object list;
4698 int depth;
4700 unsigned hash = 0;
4701 int i;
4703 if (depth < SXHASH_MAX_DEPTH)
4704 for (i = 0;
4705 CONSP (list) && i < SXHASH_MAX_LEN;
4706 list = XCDR (list), ++i)
4708 unsigned hash2 = sxhash (XCAR (list), depth + 1);
4709 hash = SXHASH_COMBINE (hash, hash2);
4712 return hash;
4716 /* Return a hash for vector VECTOR. DEPTH is the current depth in
4717 the Lisp structure. */
4719 static unsigned
4720 sxhash_vector (vec, depth)
4721 Lisp_Object vec;
4722 int depth;
4724 unsigned hash = XVECTOR (vec)->size;
4725 int i, n;
4727 n = min (SXHASH_MAX_LEN, XVECTOR (vec)->size);
4728 for (i = 0; i < n; ++i)
4730 unsigned hash2 = sxhash (XVECTOR (vec)->contents[i], depth + 1);
4731 hash = SXHASH_COMBINE (hash, hash2);
4734 return hash;
4738 /* Return a hash for bool-vector VECTOR. */
4740 static unsigned
4741 sxhash_bool_vector (vec)
4742 Lisp_Object vec;
4744 unsigned hash = XBOOL_VECTOR (vec)->size;
4745 int i, n;
4747 n = min (SXHASH_MAX_LEN, XBOOL_VECTOR (vec)->vector_size);
4748 for (i = 0; i < n; ++i)
4749 hash = SXHASH_COMBINE (hash, XBOOL_VECTOR (vec)->data[i]);
4751 return hash;
4755 /* Return a hash code for OBJ. DEPTH is the current depth in the Lisp
4756 structure. Value is an unsigned integer clipped to VALMASK. */
4758 unsigned
4759 sxhash (obj, depth)
4760 Lisp_Object obj;
4761 int depth;
4763 unsigned hash;
4765 if (depth > SXHASH_MAX_DEPTH)
4766 return 0;
4768 switch (XTYPE (obj))
4770 case Lisp_Int:
4771 hash = XUINT (obj);
4772 break;
4774 case Lisp_Symbol:
4775 hash = sxhash_string (SDATA (SYMBOL_NAME (obj)),
4776 SCHARS (SYMBOL_NAME (obj)));
4777 break;
4779 case Lisp_Misc:
4780 hash = XUINT (obj);
4781 break;
4783 case Lisp_String:
4784 hash = sxhash_string (SDATA (obj), SCHARS (obj));
4785 break;
4787 /* This can be everything from a vector to an overlay. */
4788 case Lisp_Vectorlike:
4789 if (VECTORP (obj))
4790 /* According to the CL HyperSpec, two arrays are equal only if
4791 they are `eq', except for strings and bit-vectors. In
4792 Emacs, this works differently. We have to compare element
4793 by element. */
4794 hash = sxhash_vector (obj, depth);
4795 else if (BOOL_VECTOR_P (obj))
4796 hash = sxhash_bool_vector (obj);
4797 else
4798 /* Others are `equal' if they are `eq', so let's take their
4799 address as hash. */
4800 hash = XUINT (obj);
4801 break;
4803 case Lisp_Cons:
4804 hash = sxhash_list (obj, depth);
4805 break;
4807 case Lisp_Float:
4809 unsigned char *p = (unsigned char *) &XFLOAT_DATA (obj);
4810 unsigned char *e = p + sizeof XFLOAT_DATA (obj);
4811 for (hash = 0; p < e; ++p)
4812 hash = SXHASH_COMBINE (hash, *p);
4813 break;
4816 default:
4817 abort ();
4820 return hash & VALMASK;
4825 /***********************************************************************
4826 Lisp Interface
4827 ***********************************************************************/
4830 DEFUN ("sxhash", Fsxhash, Ssxhash, 1, 1, 0,
4831 doc: /* Compute a hash code for OBJ and return it as integer. */)
4832 (obj)
4833 Lisp_Object obj;
4835 unsigned hash = sxhash (obj, 0);;
4836 return make_number (hash);
4840 DEFUN ("make-hash-table", Fmake_hash_table, Smake_hash_table, 0, MANY, 0,
4841 doc: /* Create and return a new hash table.
4843 Arguments are specified as keyword/argument pairs. The following
4844 arguments are defined:
4846 :test TEST -- TEST must be a symbol that specifies how to compare
4847 keys. Default is `eql'. Predefined are the tests `eq', `eql', and
4848 `equal'. User-supplied test and hash functions can be specified via
4849 `define-hash-table-test'.
4851 :size SIZE -- A hint as to how many elements will be put in the table.
4852 Default is 65.
4854 :rehash-size REHASH-SIZE - Indicates how to expand the table when it
4855 fills up. If REHASH-SIZE is an integer, add that many space. If it
4856 is a float, it must be > 1.0, and the new size is computed by
4857 multiplying the old size with that factor. Default is 1.5.
4859 :rehash-threshold THRESHOLD -- THRESHOLD must a float > 0, and <= 1.0.
4860 Resize the hash table when ratio of the number of entries in the
4861 table. Default is 0.8.
4863 :weakness WEAK -- WEAK must be one of nil, t, `key', `value',
4864 `key-or-value', or `key-and-value'. If WEAK is not nil, the table
4865 returned is a weak table. Key/value pairs are removed from a weak
4866 hash table when there are no non-weak references pointing to their
4867 key, value, one of key or value, or both key and value, depending on
4868 WEAK. WEAK t is equivalent to `key-and-value'. Default value of WEAK
4869 is nil.
4871 usage: (make-hash-table &rest KEYWORD-ARGS) */)
4872 (nargs, args)
4873 int nargs;
4874 Lisp_Object *args;
4876 Lisp_Object test, size, rehash_size, rehash_threshold, weak;
4877 Lisp_Object user_test, user_hash;
4878 char *used;
4879 int i;
4881 /* The vector `used' is used to keep track of arguments that
4882 have been consumed. */
4883 used = (char *) alloca (nargs * sizeof *used);
4884 bzero (used, nargs * sizeof *used);
4886 /* See if there's a `:test TEST' among the arguments. */
4887 i = get_key_arg (QCtest, nargs, args, used);
4888 test = i < 0 ? Qeql : args[i];
4889 if (!EQ (test, Qeq) && !EQ (test, Qeql) && !EQ (test, Qequal))
4891 /* See if it is a user-defined test. */
4892 Lisp_Object prop;
4894 prop = Fget (test, Qhash_table_test);
4895 if (!CONSP (prop) || !CONSP (XCDR (prop)))
4896 Fsignal (Qerror, list2 (build_string ("Invalid hash table test"),
4897 test));
4898 user_test = XCAR (prop);
4899 user_hash = XCAR (XCDR (prop));
4901 else
4902 user_test = user_hash = Qnil;
4904 /* See if there's a `:size SIZE' argument. */
4905 i = get_key_arg (QCsize, nargs, args, used);
4906 size = i < 0 ? Qnil : args[i];
4907 if (NILP (size))
4908 size = make_number (DEFAULT_HASH_SIZE);
4909 else if (!INTEGERP (size) || XINT (size) < 0)
4910 Fsignal (Qerror,
4911 list2 (build_string ("Invalid hash table size"),
4912 size));
4914 /* Look for `:rehash-size SIZE'. */
4915 i = get_key_arg (QCrehash_size, nargs, args, used);
4916 rehash_size = i < 0 ? make_float (DEFAULT_REHASH_SIZE) : args[i];
4917 if (!NUMBERP (rehash_size)
4918 || (INTEGERP (rehash_size) && XINT (rehash_size) <= 0)
4919 || XFLOATINT (rehash_size) <= 1.0)
4920 Fsignal (Qerror,
4921 list2 (build_string ("Invalid hash table rehash size"),
4922 rehash_size));
4924 /* Look for `:rehash-threshold THRESHOLD'. */
4925 i = get_key_arg (QCrehash_threshold, nargs, args, used);
4926 rehash_threshold = i < 0 ? make_float (DEFAULT_REHASH_THRESHOLD) : args[i];
4927 if (!FLOATP (rehash_threshold)
4928 || XFLOATINT (rehash_threshold) <= 0.0
4929 || XFLOATINT (rehash_threshold) > 1.0)
4930 Fsignal (Qerror,
4931 list2 (build_string ("Invalid hash table rehash threshold"),
4932 rehash_threshold));
4934 /* Look for `:weakness WEAK'. */
4935 i = get_key_arg (QCweakness, nargs, args, used);
4936 weak = i < 0 ? Qnil : args[i];
4937 if (EQ (weak, Qt))
4938 weak = Qkey_and_value;
4939 if (!NILP (weak)
4940 && !EQ (weak, Qkey)
4941 && !EQ (weak, Qvalue)
4942 && !EQ (weak, Qkey_or_value)
4943 && !EQ (weak, Qkey_and_value))
4944 Fsignal (Qerror, list2 (build_string ("Invalid hash table weakness"),
4945 weak));
4947 /* Now, all args should have been used up, or there's a problem. */
4948 for (i = 0; i < nargs; ++i)
4949 if (!used[i])
4950 Fsignal (Qerror,
4951 list2 (build_string ("Invalid argument list"), args[i]));
4953 return make_hash_table (test, size, rehash_size, rehash_threshold, weak,
4954 user_test, user_hash);
4958 DEFUN ("copy-hash-table", Fcopy_hash_table, Scopy_hash_table, 1, 1, 0,
4959 doc: /* Return a copy of hash table TABLE. */)
4960 (table)
4961 Lisp_Object table;
4963 return copy_hash_table (check_hash_table (table));
4967 DEFUN ("hash-table-count", Fhash_table_count, Shash_table_count, 1, 1, 0,
4968 doc: /* Return the number of elements in TABLE. */)
4969 (table)
4970 Lisp_Object table;
4972 return check_hash_table (table)->count;
4976 DEFUN ("hash-table-rehash-size", Fhash_table_rehash_size,
4977 Shash_table_rehash_size, 1, 1, 0,
4978 doc: /* Return the current rehash size of TABLE. */)
4979 (table)
4980 Lisp_Object table;
4982 return check_hash_table (table)->rehash_size;
4986 DEFUN ("hash-table-rehash-threshold", Fhash_table_rehash_threshold,
4987 Shash_table_rehash_threshold, 1, 1, 0,
4988 doc: /* Return the current rehash threshold of TABLE. */)
4989 (table)
4990 Lisp_Object table;
4992 return check_hash_table (table)->rehash_threshold;
4996 DEFUN ("hash-table-size", Fhash_table_size, Shash_table_size, 1, 1, 0,
4997 doc: /* Return the size of TABLE.
4998 The size can be used as an argument to `make-hash-table' to create
4999 a hash table than can hold as many elements of TABLE holds
5000 without need for resizing. */)
5001 (table)
5002 Lisp_Object table;
5004 struct Lisp_Hash_Table *h = check_hash_table (table);
5005 return make_number (HASH_TABLE_SIZE (h));
5009 DEFUN ("hash-table-test", Fhash_table_test, Shash_table_test, 1, 1, 0,
5010 doc: /* Return the test TABLE uses. */)
5011 (table)
5012 Lisp_Object table;
5014 return check_hash_table (table)->test;
5018 DEFUN ("hash-table-weakness", Fhash_table_weakness, Shash_table_weakness,
5019 1, 1, 0,
5020 doc: /* Return the weakness of TABLE. */)
5021 (table)
5022 Lisp_Object table;
5024 return check_hash_table (table)->weak;
5028 DEFUN ("hash-table-p", Fhash_table_p, Shash_table_p, 1, 1, 0,
5029 doc: /* Return t if OBJ is a Lisp hash table object. */)
5030 (obj)
5031 Lisp_Object obj;
5033 return HASH_TABLE_P (obj) ? Qt : Qnil;
5037 DEFUN ("clrhash", Fclrhash, Sclrhash, 1, 1, 0,
5038 doc: /* Clear hash table TABLE. */)
5039 (table)
5040 Lisp_Object table;
5042 hash_clear (check_hash_table (table));
5043 return Qnil;
5047 DEFUN ("gethash", Fgethash, Sgethash, 2, 3, 0,
5048 doc: /* Look up KEY in TABLE and return its associated value.
5049 If KEY is not found, return DFLT which defaults to nil. */)
5050 (key, table, dflt)
5051 Lisp_Object key, table, dflt;
5053 struct Lisp_Hash_Table *h = check_hash_table (table);
5054 int i = hash_lookup (h, key, NULL);
5055 return i >= 0 ? HASH_VALUE (h, i) : dflt;
5059 DEFUN ("puthash", Fputhash, Sputhash, 3, 3, 0,
5060 doc: /* Associate KEY with VALUE in hash table TABLE.
5061 If KEY is already present in table, replace its current value with
5062 VALUE. */)
5063 (key, value, table)
5064 Lisp_Object key, value, table;
5066 struct Lisp_Hash_Table *h = check_hash_table (table);
5067 int i;
5068 unsigned hash;
5070 i = hash_lookup (h, key, &hash);
5071 if (i >= 0)
5072 HASH_VALUE (h, i) = value;
5073 else
5074 hash_put (h, key, value, hash);
5076 return value;
5080 DEFUN ("remhash", Fremhash, Sremhash, 2, 2, 0,
5081 doc: /* Remove KEY from TABLE. */)
5082 (key, table)
5083 Lisp_Object key, table;
5085 struct Lisp_Hash_Table *h = check_hash_table (table);
5086 hash_remove (h, key);
5087 return Qnil;
5091 DEFUN ("maphash", Fmaphash, Smaphash, 2, 2, 0,
5092 doc: /* Call FUNCTION for all entries in hash table TABLE.
5093 FUNCTION is called with 2 arguments KEY and VALUE. */)
5094 (function, table)
5095 Lisp_Object function, table;
5097 struct Lisp_Hash_Table *h = check_hash_table (table);
5098 Lisp_Object args[3];
5099 int i;
5101 for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
5102 if (!NILP (HASH_HASH (h, i)))
5104 args[0] = function;
5105 args[1] = HASH_KEY (h, i);
5106 args[2] = HASH_VALUE (h, i);
5107 Ffuncall (3, args);
5110 return Qnil;
5114 DEFUN ("define-hash-table-test", Fdefine_hash_table_test,
5115 Sdefine_hash_table_test, 3, 3, 0,
5116 doc: /* Define a new hash table test with name NAME, a symbol.
5118 In hash tables created with NAME specified as test, use TEST to
5119 compare keys, and HASH for computing hash codes of keys.
5121 TEST must be a function taking two arguments and returning non-nil if
5122 both arguments are the same. HASH must be a function taking one
5123 argument and return an integer that is the hash code of the argument.
5124 Hash code computation should use the whole value range of integers,
5125 including negative integers. */)
5126 (name, test, hash)
5127 Lisp_Object name, test, hash;
5129 return Fput (name, Qhash_table_test, list2 (test, hash));
5134 /************************************************************************
5136 ************************************************************************/
5138 #include "md5.h"
5139 #include "coding.h"
5141 DEFUN ("md5", Fmd5, Smd5, 1, 5, 0,
5142 doc: /* Return MD5 message digest of OBJECT, a buffer or string.
5144 A message digest is a cryptographic checksum of a document, and the
5145 algorithm to calculate it is defined in RFC 1321.
5147 The two optional arguments START and END are character positions
5148 specifying for which part of OBJECT the message digest should be
5149 computed. If nil or omitted, the digest is computed for the whole
5150 OBJECT.
5152 The MD5 message digest is computed from the result of encoding the
5153 text in a coding system, not directly from the internal Emacs form of
5154 the text. The optional fourth argument CODING-SYSTEM specifies which
5155 coding system to encode the text with. It should be the same coding
5156 system that you used or will use when actually writing the text into a
5157 file.
5159 If CODING-SYSTEM is nil or omitted, the default depends on OBJECT. If
5160 OBJECT is a buffer, the default for CODING-SYSTEM is whatever coding
5161 system would be chosen by default for writing this text into a file.
5163 If OBJECT is a string, the most preferred coding system (see the
5164 command `prefer-coding-system') is used.
5166 If NOERROR is non-nil, silently assume the `raw-text' coding if the
5167 guesswork fails. Normally, an error is signaled in such case. */)
5168 (object, start, end, coding_system, noerror)
5169 Lisp_Object object, start, end, coding_system, noerror;
5171 unsigned char digest[16];
5172 unsigned char value[33];
5173 int i;
5174 int size;
5175 int size_byte = 0;
5176 int start_char = 0, end_char = 0;
5177 int start_byte = 0, end_byte = 0;
5178 register int b, e;
5179 register struct buffer *bp;
5180 int temp;
5182 if (STRINGP (object))
5184 if (NILP (coding_system))
5186 /* Decide the coding-system to encode the data with. */
5188 if (STRING_MULTIBYTE (object))
5189 /* use default, we can't guess correct value */
5190 coding_system = SYMBOL_VALUE (XCAR (Vcoding_category_list));
5191 else
5192 coding_system = Qraw_text;
5195 if (NILP (Fcoding_system_p (coding_system)))
5197 /* Invalid coding system. */
5199 if (!NILP (noerror))
5200 coding_system = Qraw_text;
5201 else
5202 while (1)
5203 Fsignal (Qcoding_system_error, Fcons (coding_system, Qnil));
5206 if (STRING_MULTIBYTE (object))
5207 object = code_convert_string1 (object, coding_system, Qnil, 1);
5209 size = SCHARS (object);
5210 size_byte = SBYTES (object);
5212 if (!NILP (start))
5214 CHECK_NUMBER (start);
5216 start_char = XINT (start);
5218 if (start_char < 0)
5219 start_char += size;
5221 start_byte = string_char_to_byte (object, start_char);
5224 if (NILP (end))
5226 end_char = size;
5227 end_byte = size_byte;
5229 else
5231 CHECK_NUMBER (end);
5233 end_char = XINT (end);
5235 if (end_char < 0)
5236 end_char += size;
5238 end_byte = string_char_to_byte (object, end_char);
5241 if (!(0 <= start_char && start_char <= end_char && end_char <= size))
5242 args_out_of_range_3 (object, make_number (start_char),
5243 make_number (end_char));
5245 else
5247 CHECK_BUFFER (object);
5249 bp = XBUFFER (object);
5251 if (NILP (start))
5252 b = BUF_BEGV (bp);
5253 else
5255 CHECK_NUMBER_COERCE_MARKER (start);
5256 b = XINT (start);
5259 if (NILP (end))
5260 e = BUF_ZV (bp);
5261 else
5263 CHECK_NUMBER_COERCE_MARKER (end);
5264 e = XINT (end);
5267 if (b > e)
5268 temp = b, b = e, e = temp;
5270 if (!(BUF_BEGV (bp) <= b && e <= BUF_ZV (bp)))
5271 args_out_of_range (start, end);
5273 if (NILP (coding_system))
5275 /* Decide the coding-system to encode the data with.
5276 See fileio.c:Fwrite-region */
5278 if (!NILP (Vcoding_system_for_write))
5279 coding_system = Vcoding_system_for_write;
5280 else
5282 int force_raw_text = 0;
5284 coding_system = XBUFFER (object)->buffer_file_coding_system;
5285 if (NILP (coding_system)
5286 || NILP (Flocal_variable_p (Qbuffer_file_coding_system, Qnil)))
5288 coding_system = Qnil;
5289 if (NILP (current_buffer->enable_multibyte_characters))
5290 force_raw_text = 1;
5293 if (NILP (coding_system) && !NILP (Fbuffer_file_name(object)))
5295 /* Check file-coding-system-alist. */
5296 Lisp_Object args[4], val;
5298 args[0] = Qwrite_region; args[1] = start; args[2] = end;
5299 args[3] = Fbuffer_file_name(object);
5300 val = Ffind_operation_coding_system (4, args);
5301 if (CONSP (val) && !NILP (XCDR (val)))
5302 coding_system = XCDR (val);
5305 if (NILP (coding_system)
5306 && !NILP (XBUFFER (object)->buffer_file_coding_system))
5308 /* If we still have not decided a coding system, use the
5309 default value of buffer-file-coding-system. */
5310 coding_system = XBUFFER (object)->buffer_file_coding_system;
5313 if (!force_raw_text
5314 && !NILP (Ffboundp (Vselect_safe_coding_system_function)))
5315 /* Confirm that VAL can surely encode the current region. */
5316 coding_system = call4 (Vselect_safe_coding_system_function,
5317 make_number (b), make_number (e),
5318 coding_system, Qnil);
5320 if (force_raw_text)
5321 coding_system = Qraw_text;
5324 if (NILP (Fcoding_system_p (coding_system)))
5326 /* Invalid coding system. */
5328 if (!NILP (noerror))
5329 coding_system = Qraw_text;
5330 else
5331 while (1)
5332 Fsignal (Qcoding_system_error, Fcons (coding_system, Qnil));
5336 object = make_buffer_string (b, e, 0);
5338 if (STRING_MULTIBYTE (object))
5339 object = code_convert_string1 (object, coding_system, Qnil, 1);
5342 md5_buffer (SDATA (object) + start_byte,
5343 SBYTES (object) - (size_byte - end_byte),
5344 digest);
5346 for (i = 0; i < 16; i++)
5347 sprintf (&value[2 * i], "%02x", digest[i]);
5348 value[32] = '\0';
5350 return make_string (value, 32);
5354 void
5355 syms_of_fns ()
5357 /* Hash table stuff. */
5358 Qhash_table_p = intern ("hash-table-p");
5359 staticpro (&Qhash_table_p);
5360 Qeq = intern ("eq");
5361 staticpro (&Qeq);
5362 Qeql = intern ("eql");
5363 staticpro (&Qeql);
5364 Qequal = intern ("equal");
5365 staticpro (&Qequal);
5366 QCtest = intern (":test");
5367 staticpro (&QCtest);
5368 QCsize = intern (":size");
5369 staticpro (&QCsize);
5370 QCrehash_size = intern (":rehash-size");
5371 staticpro (&QCrehash_size);
5372 QCrehash_threshold = intern (":rehash-threshold");
5373 staticpro (&QCrehash_threshold);
5374 QCweakness = intern (":weakness");
5375 staticpro (&QCweakness);
5376 Qkey = intern ("key");
5377 staticpro (&Qkey);
5378 Qvalue = intern ("value");
5379 staticpro (&Qvalue);
5380 Qhash_table_test = intern ("hash-table-test");
5381 staticpro (&Qhash_table_test);
5382 Qkey_or_value = intern ("key-or-value");
5383 staticpro (&Qkey_or_value);
5384 Qkey_and_value = intern ("key-and-value");
5385 staticpro (&Qkey_and_value);
5387 defsubr (&Ssxhash);
5388 defsubr (&Smake_hash_table);
5389 defsubr (&Scopy_hash_table);
5390 defsubr (&Shash_table_count);
5391 defsubr (&Shash_table_rehash_size);
5392 defsubr (&Shash_table_rehash_threshold);
5393 defsubr (&Shash_table_size);
5394 defsubr (&Shash_table_test);
5395 defsubr (&Shash_table_weakness);
5396 defsubr (&Shash_table_p);
5397 defsubr (&Sclrhash);
5398 defsubr (&Sgethash);
5399 defsubr (&Sputhash);
5400 defsubr (&Sremhash);
5401 defsubr (&Smaphash);
5402 defsubr (&Sdefine_hash_table_test);
5404 Qstring_lessp = intern ("string-lessp");
5405 staticpro (&Qstring_lessp);
5406 Qprovide = intern ("provide");
5407 staticpro (&Qprovide);
5408 Qrequire = intern ("require");
5409 staticpro (&Qrequire);
5410 Qyes_or_no_p_history = intern ("yes-or-no-p-history");
5411 staticpro (&Qyes_or_no_p_history);
5412 Qcursor_in_echo_area = intern ("cursor-in-echo-area");
5413 staticpro (&Qcursor_in_echo_area);
5414 Qwidget_type = intern ("widget-type");
5415 staticpro (&Qwidget_type);
5417 staticpro (&string_char_byte_cache_string);
5418 string_char_byte_cache_string = Qnil;
5420 require_nesting_list = Qnil;
5421 staticpro (&require_nesting_list);
5423 Fset (Qyes_or_no_p_history, Qnil);
5425 DEFVAR_LISP ("features", &Vfeatures,
5426 doc: /* A list of symbols which are the features of the executing emacs.
5427 Used by `featurep' and `require', and altered by `provide'. */);
5428 Vfeatures = Qnil;
5429 Qsubfeatures = intern ("subfeatures");
5430 staticpro (&Qsubfeatures);
5432 DEFVAR_BOOL ("use-dialog-box", &use_dialog_box,
5433 doc: /* *Non-nil means mouse commands use dialog boxes to ask questions.
5434 This applies to `y-or-n-p' and `yes-or-no-p' questions asked by commands
5435 invoked by mouse clicks and mouse menu items. */);
5436 use_dialog_box = 1;
5438 defsubr (&Sidentity);
5439 defsubr (&Srandom);
5440 defsubr (&Slength);
5441 defsubr (&Ssafe_length);
5442 defsubr (&Sstring_bytes);
5443 defsubr (&Sstring_equal);
5444 defsubr (&Scompare_strings);
5445 defsubr (&Sstring_lessp);
5446 defsubr (&Sappend);
5447 defsubr (&Sconcat);
5448 defsubr (&Svconcat);
5449 defsubr (&Scopy_sequence);
5450 defsubr (&Sstring_make_multibyte);
5451 defsubr (&Sstring_make_unibyte);
5452 defsubr (&Sstring_as_multibyte);
5453 defsubr (&Sstring_as_unibyte);
5454 defsubr (&Scopy_alist);
5455 defsubr (&Ssubstring);
5456 defsubr (&Ssubstring_no_properties);
5457 defsubr (&Snthcdr);
5458 defsubr (&Snth);
5459 defsubr (&Selt);
5460 defsubr (&Smember);
5461 defsubr (&Smemq);
5462 defsubr (&Sassq);
5463 defsubr (&Sassoc);
5464 defsubr (&Srassq);
5465 defsubr (&Srassoc);
5466 defsubr (&Sdelq);
5467 defsubr (&Sdelete);
5468 defsubr (&Snreverse);
5469 defsubr (&Sreverse);
5470 defsubr (&Ssort);
5471 defsubr (&Splist_get);
5472 defsubr (&Sget);
5473 defsubr (&Splist_put);
5474 defsubr (&Sput);
5475 defsubr (&Slax_plist_get);
5476 defsubr (&Slax_plist_put);
5477 defsubr (&Sequal);
5478 defsubr (&Sfillarray);
5479 defsubr (&Schar_table_subtype);
5480 defsubr (&Schar_table_parent);
5481 defsubr (&Sset_char_table_parent);
5482 defsubr (&Schar_table_extra_slot);
5483 defsubr (&Sset_char_table_extra_slot);
5484 defsubr (&Schar_table_range);
5485 defsubr (&Sset_char_table_range);
5486 defsubr (&Sset_char_table_default);
5487 defsubr (&Soptimize_char_table);
5488 defsubr (&Smap_char_table);
5489 defsubr (&Snconc);
5490 defsubr (&Smapcar);
5491 defsubr (&Smapc);
5492 defsubr (&Smapconcat);
5493 defsubr (&Sy_or_n_p);
5494 defsubr (&Syes_or_no_p);
5495 defsubr (&Sload_average);
5496 defsubr (&Sfeaturep);
5497 defsubr (&Srequire);
5498 defsubr (&Sprovide);
5499 defsubr (&Splist_member);
5500 defsubr (&Swidget_put);
5501 defsubr (&Swidget_get);
5502 defsubr (&Swidget_apply);
5503 defsubr (&Sbase64_encode_region);
5504 defsubr (&Sbase64_decode_region);
5505 defsubr (&Sbase64_encode_string);
5506 defsubr (&Sbase64_decode_string);
5507 defsubr (&Smd5);
5511 void
5512 init_fns ()
5514 Vweak_hash_tables = Qnil;