* progmodes/ada-mode.el: Update copyright notice.
[emacs.git] / src / fns.c
blobdd527ddc76b20dfae059cd5b90eab11a5b3f7fde
1 /* Random utility Lisp functions.
2 Copyright (C) 1985, 86, 87, 93, 94, 95, 97, 98, 99, 2000, 2001
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 the 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, XSTRING (sequence)->size);
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 (STRING_BYTES (XSTRING (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 (XSTRING (s1)->size != XSTRING (s2)->size
229 || STRING_BYTES (XSTRING (s1)) != STRING_BYTES (XSTRING (s2))
230 || bcmp (XSTRING (s1)->data, XSTRING (s2)->data, STRING_BYTES (XSTRING (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 = XSTRING (str1)->size;
276 if (! NILP (end1) && end1_char > XINT (end1))
277 end1_char = XINT (end1);
279 end2_char = XSTRING (str2)->size;
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 = XSTRING (str1)->data[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 = XSTRING (str2)->data[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 = XSTRING (s1)->size;
358 if (end > XSTRING (s2)->size)
359 end = XSTRING (s2)->size;
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 < XSTRING (s2)->size ? 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 /* Retrun 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 or string.
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 += STRING_BYTES (XSTRING (this));
646 else
647 result_len_byte += count_size_as_multibyte (XSTRING (this)->data,
648 XSTRING (this)->size);
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 = STRING_BYTES (XSTRING (this));
699 int combined;
701 bcopy (XSTRING (this)->data, XSTRING (val)->data + toindex_byte,
702 STRING_BYTES (XSTRING (this)));
703 combined = (some_multibyte && toindex_byte > 0
704 ? count_combining (XSTRING (val)->data,
705 toindex_byte + thislen_byte,
706 toindex_byte)
707 : 0);
708 if (! NULL_INTERVAL_P (XSTRING (this)->intervals))
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 XSTRING (val)->size -= combined;
719 /* Copy a single-byte string to a multibyte string. */
720 else if (STRINGP (this) && STRINGP (val))
722 if (! NULL_INTERVAL_P (XSTRING (this)->intervals))
724 textprops[num_textprops].argnum = argnum;
725 textprops[num_textprops].from = 0;
726 textprops[num_textprops++].to = toindex;
728 toindex_byte += copy_text (XSTRING (this)->data,
729 XSTRING (val)->data + toindex_byte,
730 XSTRING (this)->size, 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, XSTRING (this)->data[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 XSTRING (val)->data + toindex_byte);
801 else
802 XSTRING (val)->data[toindex_byte++] = XINT (elt);
803 if (some_multibyte
804 && toindex_byte > 0
805 && count_combining (XSTRING (val)->data,
806 toindex_byte, toindex_byte - 1))
807 XSTRING (val)->size--;
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 = & XSTRING (val)->data[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 (XSTRING (this)->size),
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 + XSTRING (this)->size;
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 = XSTRING (string)->size;
879 best_above_byte = STRING_BYTES (XSTRING (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 = XSTRING (string)->data + 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 = XSTRING (string)->size;
952 best_above_byte = STRING_BYTES (XSTRING (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 = XSTRING (string)->data + 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 (XSTRING (string)->data,
1024 XSTRING (string)->size);
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 == STRING_BYTES (XSTRING (string)))
1028 return string;
1030 buf = (unsigned char *) alloca (nbytes);
1031 copy_text (XSTRING (string)->data, buf, STRING_BYTES (XSTRING (string)),
1032 0, 1);
1034 return make_multibyte_string (buf, XSTRING (string)->size, 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 (XSTRING (string)->size);
1050 copy_text (XSTRING (string)->data, buf, STRING_BYTES (XSTRING (string)),
1051 1, 0);
1053 return make_unibyte_string (buf, XSTRING (string)->size);
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 = STRING_BYTES (XSTRING (string));
1100 unsigned char *str = (unsigned char *) xmalloc (bytes);
1102 bcopy (XSTRING (string)->data, 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 (XSTRING (string)->data,
1129 STRING_BYTES (XSTRING (string)),
1130 &nchars, &nbytes);
1131 new_string = make_uninit_multibyte_string (nchars, nbytes);
1132 bcopy (XSTRING (string)->data, XSTRING (new_string)->data,
1133 STRING_BYTES (XSTRING (string)));
1134 if (nbytes != STRING_BYTES (XSTRING (string)))
1135 str_as_multibyte (XSTRING (new_string)->data, nbytes,
1136 STRING_BYTES (XSTRING (string)), NULL);
1137 string = new_string;
1138 XSTRING (string)->intervals = 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 = XSTRING (string)->size;
1194 size_byte = STRING_BYTES (XSTRING (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 (XSTRING (string)->data + 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 = XSTRING (string)->size;
1261 size_byte = STRING_BYTES (XSTRING (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 (XSTRING (string)->data + 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 = XSTRING (string)->size;
1318 size_byte = STRING_BYTES (XSTRING (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 (XSTRING (string)->data + 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 < XSTRING (seq)->size;
1697 ++i, ibyte += cbytes)
1699 if (STRING_MULTIBYTE (seq))
1701 c = STRING_CHAR (&XSTRING (seq)->data[ibyte],
1702 STRING_BYTES (XSTRING (seq)) - ibyte);
1703 cbytes = CHAR_BYTES (c);
1705 else
1707 c = XSTRING (seq)->data[i];
1708 cbytes = 1;
1711 if (!INTEGERP (elt) || c != XINT (elt))
1713 ++nchars;
1714 nbytes += cbytes;
1718 if (nchars != XSTRING (seq)->size)
1720 Lisp_Object tem;
1722 tem = make_uninit_multibyte_string (nchars, nbytes);
1723 if (!STRING_MULTIBYTE (seq))
1724 SET_STRING_BYTES (XSTRING (tem), -1);
1726 for (i = nchars = nbytes = ibyte = 0;
1727 i < XSTRING (seq)->size;
1728 ++i, ibyte += cbytes)
1730 if (STRING_MULTIBYTE (seq))
1732 c = STRING_CHAR (&XSTRING (seq)->data[ibyte],
1733 STRING_BYTES (XSTRING (seq)) - ibyte);
1734 cbytes = CHAR_BYTES (c);
1736 else
1738 c = XSTRING (seq)->data[i];
1739 cbytes = 1;
1742 if (!INTEGERP (elt) || c != XINT (elt))
1744 unsigned char *from = &XSTRING (seq)->data[ibyte];
1745 unsigned char *to = &XSTRING (tem)->data[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 (XSTRING (o1)->size != XSTRING (o2)->size)
2180 return 0;
2181 if (STRING_BYTES (XSTRING (o1)) != STRING_BYTES (XSTRING (o2)))
2182 return 0;
2183 if (bcmp (XSTRING (o1)->data, XSTRING (o2)->data,
2184 STRING_BYTES (XSTRING (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 = XSTRING (array)->data;
2225 CHECK_NUMBER (item);
2226 charval = XINT (item);
2227 size = XSTRING (array)->size;
2228 if (STRING_MULTIBYTE (array))
2230 unsigned char str[MAX_MULTIBYTE_LENGTH];
2231 int len = CHAR_STRING (charval, str);
2232 int size_byte = STRING_BYTES (XSTRING (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 (NULL, function, char_table, char_table, 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 = Fcdr (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_ptr - specpdl;
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 (XSTRING (ans)->size == 3 && !strcmp (XSTRING (ans)->data, "yes"))
3118 UNGCPRO;
3119 return Qt;
3121 if (XSTRING (ans)->size == 2 && !strcmp (XSTRING (ans)->data, "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 (!NILP (tem))
3210 Fprogn (Fcdr (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 LOADHIST_ATTACH (Fcons (Qrequire, feature));
3251 if (NILP (tem))
3253 int count = specpdl_ptr - specpdl;
3254 int nesting = 0;
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 XSTRING (SYMBOL_NAME (feature))->data);
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 > 2)
3273 error ("Recursive `require' for feature `%s'",
3274 XSTRING (SYMBOL_NAME (feature))->data);
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 XSTRING (SYMBOL_NAME (feature))->data);
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 = STRING_BYTES (XSTRING (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 (XSTRING (string)->data,
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 = STRING_BYTES (XSTRING (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 (XSTRING (string)->data, 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 /* Value is the key part of entry IDX in hash table H. */
3901 #define HASH_KEY(H, IDX) AREF ((H)->key_and_value, 2 * (IDX))
3903 /* Value is the value part of entry IDX in hash table H. */
3905 #define HASH_VALUE(H, IDX) AREF ((H)->key_and_value, 2 * (IDX) + 1)
3907 /* Value is the index of the next entry following the one at IDX
3908 in hash table H. */
3910 #define HASH_NEXT(H, IDX) AREF ((H)->next, (IDX))
3912 /* Value is the hash code computed for entry IDX in hash table H. */
3914 #define HASH_HASH(H, IDX) AREF ((H)->hash, (IDX))
3916 /* Value is the index of the element in hash table H that is the
3917 start of the collision list at index IDX in the index vector of H. */
3919 #define HASH_INDEX(H, IDX) AREF ((H)->index, (IDX))
3921 /* Value is the size of hash table H. */
3923 #define HASH_TABLE_SIZE(H) XVECTOR ((H)->next)->size
3925 /* The list of all weak hash tables. Don't staticpro this one. */
3927 Lisp_Object Vweak_hash_tables;
3929 /* Various symbols. */
3931 Lisp_Object Qhash_table_p, Qeq, Qeql, Qequal, Qkey, Qvalue;
3932 Lisp_Object QCtest, QCsize, QCrehash_size, QCrehash_threshold, QCweakness;
3933 Lisp_Object Qhash_table_test, Qkey_or_value, Qkey_and_value;
3935 /* Function prototypes. */
3937 static struct Lisp_Hash_Table *check_hash_table P_ ((Lisp_Object));
3938 static int get_key_arg P_ ((Lisp_Object, int, Lisp_Object *, char *));
3939 static void maybe_resize_hash_table P_ ((struct Lisp_Hash_Table *));
3940 static int cmpfn_eql P_ ((struct Lisp_Hash_Table *, Lisp_Object, unsigned,
3941 Lisp_Object, unsigned));
3942 static int cmpfn_equal P_ ((struct Lisp_Hash_Table *, Lisp_Object, unsigned,
3943 Lisp_Object, unsigned));
3944 static int cmpfn_user_defined P_ ((struct Lisp_Hash_Table *, Lisp_Object,
3945 unsigned, Lisp_Object, unsigned));
3946 static unsigned hashfn_eq P_ ((struct Lisp_Hash_Table *, Lisp_Object));
3947 static unsigned hashfn_eql P_ ((struct Lisp_Hash_Table *, Lisp_Object));
3948 static unsigned hashfn_equal P_ ((struct Lisp_Hash_Table *, Lisp_Object));
3949 static unsigned hashfn_user_defined P_ ((struct Lisp_Hash_Table *,
3950 Lisp_Object));
3951 static unsigned sxhash_string P_ ((unsigned char *, int));
3952 static unsigned sxhash_list P_ ((Lisp_Object, int));
3953 static unsigned sxhash_vector P_ ((Lisp_Object, int));
3954 static unsigned sxhash_bool_vector P_ ((Lisp_Object));
3955 static int sweep_weak_table P_ ((struct Lisp_Hash_Table *, int));
3959 /***********************************************************************
3960 Utilities
3961 ***********************************************************************/
3963 /* If OBJ is a Lisp hash table, return a pointer to its struct
3964 Lisp_Hash_Table. Otherwise, signal an error. */
3966 static struct Lisp_Hash_Table *
3967 check_hash_table (obj)
3968 Lisp_Object obj;
3970 CHECK_HASH_TABLE (obj);
3971 return XHASH_TABLE (obj);
3975 /* Value is the next integer I >= N, N >= 0 which is "almost" a prime
3976 number. */
3979 next_almost_prime (n)
3980 int n;
3982 if (n % 2 == 0)
3983 n += 1;
3984 if (n % 3 == 0)
3985 n += 2;
3986 if (n % 7 == 0)
3987 n += 4;
3988 return n;
3992 /* Find KEY in ARGS which has size NARGS. Don't consider indices for
3993 which USED[I] is non-zero. If found at index I in ARGS, set
3994 USED[I] and USED[I + 1] to 1, and return I + 1. Otherwise return
3995 -1. This function is used to extract a keyword/argument pair from
3996 a DEFUN parameter list. */
3998 static int
3999 get_key_arg (key, nargs, args, used)
4000 Lisp_Object key;
4001 int nargs;
4002 Lisp_Object *args;
4003 char *used;
4005 int i;
4007 for (i = 0; i < nargs - 1; ++i)
4008 if (!used[i] && EQ (args[i], key))
4009 break;
4011 if (i >= nargs - 1)
4012 i = -1;
4013 else
4015 used[i++] = 1;
4016 used[i] = 1;
4019 return i;
4023 /* Return a Lisp vector which has the same contents as VEC but has
4024 size NEW_SIZE, NEW_SIZE >= VEC->size. Entries in the resulting
4025 vector that are not copied from VEC are set to INIT. */
4027 Lisp_Object
4028 larger_vector (vec, new_size, init)
4029 Lisp_Object vec;
4030 int new_size;
4031 Lisp_Object init;
4033 struct Lisp_Vector *v;
4034 int i, old_size;
4036 xassert (VECTORP (vec));
4037 old_size = XVECTOR (vec)->size;
4038 xassert (new_size >= old_size);
4040 v = allocate_vector (new_size);
4041 bcopy (XVECTOR (vec)->contents, v->contents,
4042 old_size * sizeof *v->contents);
4043 for (i = old_size; i < new_size; ++i)
4044 v->contents[i] = init;
4045 XSETVECTOR (vec, v);
4046 return vec;
4050 /***********************************************************************
4051 Low-level Functions
4052 ***********************************************************************/
4054 /* Compare KEY1 which has hash code HASH1 and KEY2 with hash code
4055 HASH2 in hash table H using `eql'. Value is non-zero if KEY1 and
4056 KEY2 are the same. */
4058 static int
4059 cmpfn_eql (h, key1, hash1, key2, hash2)
4060 struct Lisp_Hash_Table *h;
4061 Lisp_Object key1, key2;
4062 unsigned hash1, hash2;
4064 return (FLOATP (key1)
4065 && FLOATP (key2)
4066 && XFLOAT_DATA (key1) == XFLOAT_DATA (key2));
4070 /* Compare KEY1 which has hash code HASH1 and KEY2 with hash code
4071 HASH2 in hash table H using `equal'. Value is non-zero if KEY1 and
4072 KEY2 are the same. */
4074 static int
4075 cmpfn_equal (h, key1, hash1, key2, hash2)
4076 struct Lisp_Hash_Table *h;
4077 Lisp_Object key1, key2;
4078 unsigned hash1, hash2;
4080 return hash1 == hash2 && !NILP (Fequal (key1, key2));
4084 /* Compare KEY1 which has hash code HASH1, and KEY2 with hash code
4085 HASH2 in hash table H using H->user_cmp_function. Value is non-zero
4086 if KEY1 and KEY2 are the same. */
4088 static int
4089 cmpfn_user_defined (h, key1, hash1, key2, hash2)
4090 struct Lisp_Hash_Table *h;
4091 Lisp_Object key1, key2;
4092 unsigned hash1, hash2;
4094 if (hash1 == hash2)
4096 Lisp_Object args[3];
4098 args[0] = h->user_cmp_function;
4099 args[1] = key1;
4100 args[2] = key2;
4101 return !NILP (Ffuncall (3, args));
4103 else
4104 return 0;
4108 /* Value is a hash code for KEY for use in hash table H which uses
4109 `eq' to compare keys. The hash code returned is guaranteed to fit
4110 in a Lisp integer. */
4112 static unsigned
4113 hashfn_eq (h, key)
4114 struct Lisp_Hash_Table *h;
4115 Lisp_Object key;
4117 unsigned hash = XUINT (key) ^ XGCTYPE (key);
4118 xassert ((hash & ~VALMASK) == 0);
4119 return hash;
4123 /* Value is a hash code for KEY for use in hash table H which uses
4124 `eql' to compare keys. The hash code returned is guaranteed to fit
4125 in a Lisp integer. */
4127 static unsigned
4128 hashfn_eql (h, key)
4129 struct Lisp_Hash_Table *h;
4130 Lisp_Object key;
4132 unsigned hash;
4133 if (FLOATP (key))
4134 hash = sxhash (key, 0);
4135 else
4136 hash = XUINT (key) ^ XGCTYPE (key);
4137 xassert ((hash & ~VALMASK) == 0);
4138 return hash;
4142 /* Value is a hash code for KEY for use in hash table H which uses
4143 `equal' to compare keys. The hash code returned is guaranteed to fit
4144 in a Lisp integer. */
4146 static unsigned
4147 hashfn_equal (h, key)
4148 struct Lisp_Hash_Table *h;
4149 Lisp_Object key;
4151 unsigned hash = sxhash (key, 0);
4152 xassert ((hash & ~VALMASK) == 0);
4153 return hash;
4157 /* Value is a hash code for KEY for use in hash table H which uses as
4158 user-defined function to compare keys. The hash code returned is
4159 guaranteed to fit in a Lisp integer. */
4161 static unsigned
4162 hashfn_user_defined (h, key)
4163 struct Lisp_Hash_Table *h;
4164 Lisp_Object key;
4166 Lisp_Object args[2], hash;
4168 args[0] = h->user_hash_function;
4169 args[1] = key;
4170 hash = Ffuncall (2, args);
4171 if (!INTEGERP (hash))
4172 Fsignal (Qerror,
4173 list2 (build_string ("Invalid hash code returned from \
4174 user-supplied hash function"),
4175 hash));
4176 return XUINT (hash);
4180 /* Create and initialize a new hash table.
4182 TEST specifies the test the hash table will use to compare keys.
4183 It must be either one of the predefined tests `eq', `eql' or
4184 `equal' or a symbol denoting a user-defined test named TEST with
4185 test and hash functions USER_TEST and USER_HASH.
4187 Give the table initial capacity SIZE, SIZE >= 0, an integer.
4189 If REHASH_SIZE is an integer, it must be > 0, and this hash table's
4190 new size when it becomes full is computed by adding REHASH_SIZE to
4191 its old size. If REHASH_SIZE is a float, it must be > 1.0, and the
4192 table's new size is computed by multiplying its old size with
4193 REHASH_SIZE.
4195 REHASH_THRESHOLD must be a float <= 1.0, and > 0. The table will
4196 be resized when the ratio of (number of entries in the table) /
4197 (table size) is >= REHASH_THRESHOLD.
4199 WEAK specifies the weakness of the table. If non-nil, it must be
4200 one of the symbols `key', `value', `key-or-value', or `key-and-value'. */
4202 Lisp_Object
4203 make_hash_table (test, size, rehash_size, rehash_threshold, weak,
4204 user_test, user_hash)
4205 Lisp_Object test, size, rehash_size, rehash_threshold, weak;
4206 Lisp_Object user_test, user_hash;
4208 struct Lisp_Hash_Table *h;
4209 Lisp_Object table;
4210 int index_size, i, sz;
4212 /* Preconditions. */
4213 xassert (SYMBOLP (test));
4214 xassert (INTEGERP (size) && XINT (size) >= 0);
4215 xassert ((INTEGERP (rehash_size) && XINT (rehash_size) > 0)
4216 || (FLOATP (rehash_size) && XFLOATINT (rehash_size) > 1.0));
4217 xassert (FLOATP (rehash_threshold)
4218 && XFLOATINT (rehash_threshold) > 0
4219 && XFLOATINT (rehash_threshold) <= 1.0);
4221 if (XFASTINT (size) == 0)
4222 size = make_number (1);
4224 /* Allocate a table and initialize it. */
4225 h = allocate_hash_table ();
4227 /* Initialize hash table slots. */
4228 sz = XFASTINT (size);
4230 h->test = test;
4231 if (EQ (test, Qeql))
4233 h->cmpfn = cmpfn_eql;
4234 h->hashfn = hashfn_eql;
4236 else if (EQ (test, Qeq))
4238 h->cmpfn = NULL;
4239 h->hashfn = hashfn_eq;
4241 else if (EQ (test, Qequal))
4243 h->cmpfn = cmpfn_equal;
4244 h->hashfn = hashfn_equal;
4246 else
4248 h->user_cmp_function = user_test;
4249 h->user_hash_function = user_hash;
4250 h->cmpfn = cmpfn_user_defined;
4251 h->hashfn = hashfn_user_defined;
4254 h->weak = weak;
4255 h->rehash_threshold = rehash_threshold;
4256 h->rehash_size = rehash_size;
4257 h->count = make_number (0);
4258 h->key_and_value = Fmake_vector (make_number (2 * sz), Qnil);
4259 h->hash = Fmake_vector (size, Qnil);
4260 h->next = Fmake_vector (size, Qnil);
4261 /* Cast to int here avoids losing with gcc 2.95 on Tru64/Alpha... */
4262 index_size = next_almost_prime ((int) (sz / XFLOATINT (rehash_threshold)));
4263 h->index = Fmake_vector (make_number (index_size), Qnil);
4265 /* Set up the free list. */
4266 for (i = 0; i < sz - 1; ++i)
4267 HASH_NEXT (h, i) = make_number (i + 1);
4268 h->next_free = make_number (0);
4270 XSET_HASH_TABLE (table, h);
4271 xassert (HASH_TABLE_P (table));
4272 xassert (XHASH_TABLE (table) == h);
4274 /* Maybe add this hash table to the list of all weak hash tables. */
4275 if (NILP (h->weak))
4276 h->next_weak = Qnil;
4277 else
4279 h->next_weak = Vweak_hash_tables;
4280 Vweak_hash_tables = table;
4283 return table;
4287 /* Return a copy of hash table H1. Keys and values are not copied,
4288 only the table itself is. */
4290 Lisp_Object
4291 copy_hash_table (h1)
4292 struct Lisp_Hash_Table *h1;
4294 Lisp_Object table;
4295 struct Lisp_Hash_Table *h2;
4296 struct Lisp_Vector *next;
4298 h2 = allocate_hash_table ();
4299 next = h2->vec_next;
4300 bcopy (h1, h2, sizeof *h2);
4301 h2->vec_next = next;
4302 h2->key_and_value = Fcopy_sequence (h1->key_and_value);
4303 h2->hash = Fcopy_sequence (h1->hash);
4304 h2->next = Fcopy_sequence (h1->next);
4305 h2->index = Fcopy_sequence (h1->index);
4306 XSET_HASH_TABLE (table, h2);
4308 /* Maybe add this hash table to the list of all weak hash tables. */
4309 if (!NILP (h2->weak))
4311 h2->next_weak = Vweak_hash_tables;
4312 Vweak_hash_tables = table;
4315 return table;
4319 /* Resize hash table H if it's too full. If H cannot be resized
4320 because it's already too large, throw an error. */
4322 static INLINE void
4323 maybe_resize_hash_table (h)
4324 struct Lisp_Hash_Table *h;
4326 if (NILP (h->next_free))
4328 int old_size = HASH_TABLE_SIZE (h);
4329 int i, new_size, index_size;
4331 if (INTEGERP (h->rehash_size))
4332 new_size = old_size + XFASTINT (h->rehash_size);
4333 else
4334 new_size = old_size * XFLOATINT (h->rehash_size);
4335 new_size = max (old_size + 1, new_size);
4336 index_size = next_almost_prime ((int)
4337 (new_size
4338 / XFLOATINT (h->rehash_threshold)));
4339 if (max (index_size, 2 * new_size) & ~VALMASK)
4340 error ("Hash table too large to resize");
4342 h->key_and_value = larger_vector (h->key_and_value, 2 * new_size, Qnil);
4343 h->next = larger_vector (h->next, new_size, Qnil);
4344 h->hash = larger_vector (h->hash, new_size, Qnil);
4345 h->index = Fmake_vector (make_number (index_size), Qnil);
4347 /* Update the free list. Do it so that new entries are added at
4348 the end of the free list. This makes some operations like
4349 maphash faster. */
4350 for (i = old_size; i < new_size - 1; ++i)
4351 HASH_NEXT (h, i) = make_number (i + 1);
4353 if (!NILP (h->next_free))
4355 Lisp_Object last, next;
4357 last = h->next_free;
4358 while (next = HASH_NEXT (h, XFASTINT (last)),
4359 !NILP (next))
4360 last = next;
4362 HASH_NEXT (h, XFASTINT (last)) = make_number (old_size);
4364 else
4365 XSETFASTINT (h->next_free, old_size);
4367 /* Rehash. */
4368 for (i = 0; i < old_size; ++i)
4369 if (!NILP (HASH_HASH (h, i)))
4371 unsigned hash_code = XUINT (HASH_HASH (h, i));
4372 int start_of_bucket = hash_code % XVECTOR (h->index)->size;
4373 HASH_NEXT (h, i) = HASH_INDEX (h, start_of_bucket);
4374 HASH_INDEX (h, start_of_bucket) = make_number (i);
4380 /* Lookup KEY in hash table H. If HASH is non-null, return in *HASH
4381 the hash code of KEY. Value is the index of the entry in H
4382 matching KEY, or -1 if not found. */
4385 hash_lookup (h, key, hash)
4386 struct Lisp_Hash_Table *h;
4387 Lisp_Object key;
4388 unsigned *hash;
4390 unsigned hash_code;
4391 int start_of_bucket;
4392 Lisp_Object idx;
4394 hash_code = h->hashfn (h, key);
4395 if (hash)
4396 *hash = hash_code;
4398 start_of_bucket = hash_code % XVECTOR (h->index)->size;
4399 idx = HASH_INDEX (h, start_of_bucket);
4401 /* We need not gcpro idx since it's either an integer or nil. */
4402 while (!NILP (idx))
4404 int i = XFASTINT (idx);
4405 if (EQ (key, HASH_KEY (h, i))
4406 || (h->cmpfn
4407 && h->cmpfn (h, key, hash_code,
4408 HASH_KEY (h, i), XUINT (HASH_HASH (h, i)))))
4409 break;
4410 idx = HASH_NEXT (h, i);
4413 return NILP (idx) ? -1 : XFASTINT (idx);
4417 /* Put an entry into hash table H that associates KEY with VALUE.
4418 HASH is a previously computed hash code of KEY.
4419 Value is the index of the entry in H matching KEY. */
4422 hash_put (h, key, value, hash)
4423 struct Lisp_Hash_Table *h;
4424 Lisp_Object key, value;
4425 unsigned hash;
4427 int start_of_bucket, i;
4429 xassert ((hash & ~VALMASK) == 0);
4431 /* Increment count after resizing because resizing may fail. */
4432 maybe_resize_hash_table (h);
4433 h->count = make_number (XFASTINT (h->count) + 1);
4435 /* Store key/value in the key_and_value vector. */
4436 i = XFASTINT (h->next_free);
4437 h->next_free = HASH_NEXT (h, i);
4438 HASH_KEY (h, i) = key;
4439 HASH_VALUE (h, i) = value;
4441 /* Remember its hash code. */
4442 HASH_HASH (h, i) = make_number (hash);
4444 /* Add new entry to its collision chain. */
4445 start_of_bucket = hash % XVECTOR (h->index)->size;
4446 HASH_NEXT (h, i) = HASH_INDEX (h, start_of_bucket);
4447 HASH_INDEX (h, start_of_bucket) = make_number (i);
4448 return i;
4452 /* Remove the entry matching KEY from hash table H, if there is one. */
4454 void
4455 hash_remove (h, key)
4456 struct Lisp_Hash_Table *h;
4457 Lisp_Object key;
4459 unsigned hash_code;
4460 int start_of_bucket;
4461 Lisp_Object idx, prev;
4463 hash_code = h->hashfn (h, key);
4464 start_of_bucket = hash_code % XVECTOR (h->index)->size;
4465 idx = HASH_INDEX (h, start_of_bucket);
4466 prev = Qnil;
4468 /* We need not gcpro idx, prev since they're either integers or nil. */
4469 while (!NILP (idx))
4471 int i = XFASTINT (idx);
4473 if (EQ (key, HASH_KEY (h, i))
4474 || (h->cmpfn
4475 && h->cmpfn (h, key, hash_code,
4476 HASH_KEY (h, i), XUINT (HASH_HASH (h, i)))))
4478 /* Take entry out of collision chain. */
4479 if (NILP (prev))
4480 HASH_INDEX (h, start_of_bucket) = HASH_NEXT (h, i);
4481 else
4482 HASH_NEXT (h, XFASTINT (prev)) = HASH_NEXT (h, i);
4484 /* Clear slots in key_and_value and add the slots to
4485 the free list. */
4486 HASH_KEY (h, i) = HASH_VALUE (h, i) = HASH_HASH (h, i) = Qnil;
4487 HASH_NEXT (h, i) = h->next_free;
4488 h->next_free = make_number (i);
4489 h->count = make_number (XFASTINT (h->count) - 1);
4490 xassert (XINT (h->count) >= 0);
4491 break;
4493 else
4495 prev = idx;
4496 idx = HASH_NEXT (h, i);
4502 /* Clear hash table H. */
4504 void
4505 hash_clear (h)
4506 struct Lisp_Hash_Table *h;
4508 if (XFASTINT (h->count) > 0)
4510 int i, size = HASH_TABLE_SIZE (h);
4512 for (i = 0; i < size; ++i)
4514 HASH_NEXT (h, i) = i < size - 1 ? make_number (i + 1) : Qnil;
4515 HASH_KEY (h, i) = Qnil;
4516 HASH_VALUE (h, i) = Qnil;
4517 HASH_HASH (h, i) = Qnil;
4520 for (i = 0; i < XVECTOR (h->index)->size; ++i)
4521 XVECTOR (h->index)->contents[i] = Qnil;
4523 h->next_free = make_number (0);
4524 h->count = make_number (0);
4530 /************************************************************************
4531 Weak Hash Tables
4532 ************************************************************************/
4534 /* Sweep weak hash table H. REMOVE_ENTRIES_P non-zero means remove
4535 entries from the table that don't survive the current GC.
4536 REMOVE_ENTRIES_P zero means mark entries that are in use. Value is
4537 non-zero if anything was marked. */
4539 static int
4540 sweep_weak_table (h, remove_entries_p)
4541 struct Lisp_Hash_Table *h;
4542 int remove_entries_p;
4544 int bucket, n, marked;
4546 n = XVECTOR (h->index)->size & ~ARRAY_MARK_FLAG;
4547 marked = 0;
4549 for (bucket = 0; bucket < n; ++bucket)
4551 Lisp_Object idx, next, prev;
4553 /* Follow collision chain, removing entries that
4554 don't survive this garbage collection. */
4555 prev = Qnil;
4556 for (idx = HASH_INDEX (h, bucket); !GC_NILP (idx); idx = next)
4558 int i = XFASTINT (idx);
4559 int key_known_to_survive_p = survives_gc_p (HASH_KEY (h, i));
4560 int value_known_to_survive_p = survives_gc_p (HASH_VALUE (h, i));
4561 int remove_p;
4563 if (EQ (h->weak, Qkey))
4564 remove_p = !key_known_to_survive_p;
4565 else if (EQ (h->weak, Qvalue))
4566 remove_p = !value_known_to_survive_p;
4567 else if (EQ (h->weak, Qkey_or_value))
4568 remove_p = !(key_known_to_survive_p || value_known_to_survive_p);
4569 else if (EQ (h->weak, Qkey_and_value))
4570 remove_p = !(key_known_to_survive_p && value_known_to_survive_p);
4571 else
4572 abort ();
4574 next = HASH_NEXT (h, i);
4576 if (remove_entries_p)
4578 if (remove_p)
4580 /* Take out of collision chain. */
4581 if (GC_NILP (prev))
4582 HASH_INDEX (h, bucket) = next;
4583 else
4584 HASH_NEXT (h, XFASTINT (prev)) = next;
4586 /* Add to free list. */
4587 HASH_NEXT (h, i) = h->next_free;
4588 h->next_free = idx;
4590 /* Clear key, value, and hash. */
4591 HASH_KEY (h, i) = HASH_VALUE (h, i) = Qnil;
4592 HASH_HASH (h, i) = Qnil;
4594 h->count = make_number (XFASTINT (h->count) - 1);
4597 else
4599 if (!remove_p)
4601 /* Make sure key and value survive. */
4602 if (!key_known_to_survive_p)
4604 mark_object (&HASH_KEY (h, i));
4605 marked = 1;
4608 if (!value_known_to_survive_p)
4610 mark_object (&HASH_VALUE (h, i));
4611 marked = 1;
4618 return marked;
4621 /* Remove elements from weak hash tables that don't survive the
4622 current garbage collection. Remove weak tables that don't survive
4623 from Vweak_hash_tables. Called from gc_sweep. */
4625 void
4626 sweep_weak_hash_tables ()
4628 Lisp_Object table, used, next;
4629 struct Lisp_Hash_Table *h;
4630 int marked;
4632 /* Mark all keys and values that are in use. Keep on marking until
4633 there is no more change. This is necessary for cases like
4634 value-weak table A containing an entry X -> Y, where Y is used in a
4635 key-weak table B, Z -> Y. If B comes after A in the list of weak
4636 tables, X -> Y might be removed from A, although when looking at B
4637 one finds that it shouldn't. */
4640 marked = 0;
4641 for (table = Vweak_hash_tables; !GC_NILP (table); table = h->next_weak)
4643 h = XHASH_TABLE (table);
4644 if (h->size & ARRAY_MARK_FLAG)
4645 marked |= sweep_weak_table (h, 0);
4648 while (marked);
4650 /* Remove tables and entries that aren't used. */
4651 for (table = Vweak_hash_tables, used = Qnil; !GC_NILP (table); table = next)
4653 h = XHASH_TABLE (table);
4654 next = h->next_weak;
4656 if (h->size & ARRAY_MARK_FLAG)
4658 /* TABLE is marked as used. Sweep its contents. */
4659 if (XFASTINT (h->count) > 0)
4660 sweep_weak_table (h, 1);
4662 /* Add table to the list of used weak hash tables. */
4663 h->next_weak = used;
4664 used = table;
4668 Vweak_hash_tables = used;
4673 /***********************************************************************
4674 Hash Code Computation
4675 ***********************************************************************/
4677 /* Maximum depth up to which to dive into Lisp structures. */
4679 #define SXHASH_MAX_DEPTH 3
4681 /* Maximum length up to which to take list and vector elements into
4682 account. */
4684 #define SXHASH_MAX_LEN 7
4686 /* Combine two integers X and Y for hashing. */
4688 #define SXHASH_COMBINE(X, Y) \
4689 ((((unsigned)(X) << 4) + (((unsigned)(X) >> 24) & 0x0fffffff)) \
4690 + (unsigned)(Y))
4693 /* Return a hash for string PTR which has length LEN. The hash
4694 code returned is guaranteed to fit in a Lisp integer. */
4696 static unsigned
4697 sxhash_string (ptr, len)
4698 unsigned char *ptr;
4699 int len;
4701 unsigned char *p = ptr;
4702 unsigned char *end = p + len;
4703 unsigned char c;
4704 unsigned hash = 0;
4706 while (p != end)
4708 c = *p++;
4709 if (c >= 0140)
4710 c -= 40;
4711 hash = ((hash << 3) + (hash >> 28) + c);
4714 return hash & VALMASK;
4718 /* Return a hash for list LIST. DEPTH is the current depth in the
4719 list. We don't recurse deeper than SXHASH_MAX_DEPTH in it. */
4721 static unsigned
4722 sxhash_list (list, depth)
4723 Lisp_Object list;
4724 int depth;
4726 unsigned hash = 0;
4727 int i;
4729 if (depth < SXHASH_MAX_DEPTH)
4730 for (i = 0;
4731 CONSP (list) && i < SXHASH_MAX_LEN;
4732 list = XCDR (list), ++i)
4734 unsigned hash2 = sxhash (XCAR (list), depth + 1);
4735 hash = SXHASH_COMBINE (hash, hash2);
4738 return hash;
4742 /* Return a hash for vector VECTOR. DEPTH is the current depth in
4743 the Lisp structure. */
4745 static unsigned
4746 sxhash_vector (vec, depth)
4747 Lisp_Object vec;
4748 int depth;
4750 unsigned hash = XVECTOR (vec)->size;
4751 int i, n;
4753 n = min (SXHASH_MAX_LEN, XVECTOR (vec)->size);
4754 for (i = 0; i < n; ++i)
4756 unsigned hash2 = sxhash (XVECTOR (vec)->contents[i], depth + 1);
4757 hash = SXHASH_COMBINE (hash, hash2);
4760 return hash;
4764 /* Return a hash for bool-vector VECTOR. */
4766 static unsigned
4767 sxhash_bool_vector (vec)
4768 Lisp_Object vec;
4770 unsigned hash = XBOOL_VECTOR (vec)->size;
4771 int i, n;
4773 n = min (SXHASH_MAX_LEN, XBOOL_VECTOR (vec)->vector_size);
4774 for (i = 0; i < n; ++i)
4775 hash = SXHASH_COMBINE (hash, XBOOL_VECTOR (vec)->data[i]);
4777 return hash;
4781 /* Return a hash code for OBJ. DEPTH is the current depth in the Lisp
4782 structure. Value is an unsigned integer clipped to VALMASK. */
4784 unsigned
4785 sxhash (obj, depth)
4786 Lisp_Object obj;
4787 int depth;
4789 unsigned hash;
4791 if (depth > SXHASH_MAX_DEPTH)
4792 return 0;
4794 switch (XTYPE (obj))
4796 case Lisp_Int:
4797 hash = XUINT (obj);
4798 break;
4800 case Lisp_Symbol:
4801 hash = sxhash_string (XSTRING (SYMBOL_NAME (obj))->data,
4802 XSTRING (SYMBOL_NAME (obj))->size);
4803 break;
4805 case Lisp_Misc:
4806 hash = XUINT (obj);
4807 break;
4809 case Lisp_String:
4810 hash = sxhash_string (XSTRING (obj)->data, XSTRING (obj)->size);
4811 break;
4813 /* This can be everything from a vector to an overlay. */
4814 case Lisp_Vectorlike:
4815 if (VECTORP (obj))
4816 /* According to the CL HyperSpec, two arrays are equal only if
4817 they are `eq', except for strings and bit-vectors. In
4818 Emacs, this works differently. We have to compare element
4819 by element. */
4820 hash = sxhash_vector (obj, depth);
4821 else if (BOOL_VECTOR_P (obj))
4822 hash = sxhash_bool_vector (obj);
4823 else
4824 /* Others are `equal' if they are `eq', so let's take their
4825 address as hash. */
4826 hash = XUINT (obj);
4827 break;
4829 case Lisp_Cons:
4830 hash = sxhash_list (obj, depth);
4831 break;
4833 case Lisp_Float:
4835 unsigned char *p = (unsigned char *) &XFLOAT_DATA (obj);
4836 unsigned char *e = p + sizeof XFLOAT_DATA (obj);
4837 for (hash = 0; p < e; ++p)
4838 hash = SXHASH_COMBINE (hash, *p);
4839 break;
4842 default:
4843 abort ();
4846 return hash & VALMASK;
4851 /***********************************************************************
4852 Lisp Interface
4853 ***********************************************************************/
4856 DEFUN ("sxhash", Fsxhash, Ssxhash, 1, 1, 0,
4857 doc: /* Compute a hash code for OBJ and return it as integer. */)
4858 (obj)
4859 Lisp_Object obj;
4861 unsigned hash = sxhash (obj, 0);;
4862 return make_number (hash);
4866 DEFUN ("make-hash-table", Fmake_hash_table, Smake_hash_table, 0, MANY, 0,
4867 doc: /* Create and return a new hash table.
4869 Arguments are specified as keyword/argument pairs. The following
4870 arguments are defined:
4872 :test TEST -- TEST must be a symbol that specifies how to compare
4873 keys. Default is `eql'. Predefined are the tests `eq', `eql', and
4874 `equal'. User-supplied test and hash functions can be specified via
4875 `define-hash-table-test'.
4877 :size SIZE -- A hint as to how many elements will be put in the table.
4878 Default is 65.
4880 :rehash-size REHASH-SIZE - Indicates how to expand the table when it
4881 fills up. If REHASH-SIZE is an integer, add that many space. If it
4882 is a float, it must be > 1.0, and the new size is computed by
4883 multiplying the old size with that factor. Default is 1.5.
4885 :rehash-threshold THRESHOLD -- THRESHOLD must a float > 0, and <= 1.0.
4886 Resize the hash table when ratio of the number of entries in the
4887 table. Default is 0.8.
4889 :weakness WEAK -- WEAK must be one of nil, t, `key', `value',
4890 `key-or-value', or `key-and-value'. If WEAK is not nil, the table
4891 returned is a weak table. Key/value pairs are removed from a weak
4892 hash table when there are no non-weak references pointing to their
4893 key, value, one of key or value, or both key and value, depending on
4894 WEAK. WEAK t is equivalent to `key-and-value'. Default value of WEAK
4895 is nil.
4897 usage: (make-hash-table &rest KEYWORD-ARGS) */)
4898 (nargs, args)
4899 int nargs;
4900 Lisp_Object *args;
4902 Lisp_Object test, size, rehash_size, rehash_threshold, weak;
4903 Lisp_Object user_test, user_hash;
4904 char *used;
4905 int i;
4907 /* The vector `used' is used to keep track of arguments that
4908 have been consumed. */
4909 used = (char *) alloca (nargs * sizeof *used);
4910 bzero (used, nargs * sizeof *used);
4912 /* See if there's a `:test TEST' among the arguments. */
4913 i = get_key_arg (QCtest, nargs, args, used);
4914 test = i < 0 ? Qeql : args[i];
4915 if (!EQ (test, Qeq) && !EQ (test, Qeql) && !EQ (test, Qequal))
4917 /* See if it is a user-defined test. */
4918 Lisp_Object prop;
4920 prop = Fget (test, Qhash_table_test);
4921 if (!CONSP (prop) || !CONSP (XCDR (prop)))
4922 Fsignal (Qerror, list2 (build_string ("Invalid hash table test"),
4923 test));
4924 user_test = XCAR (prop);
4925 user_hash = XCAR (XCDR (prop));
4927 else
4928 user_test = user_hash = Qnil;
4930 /* See if there's a `:size SIZE' argument. */
4931 i = get_key_arg (QCsize, nargs, args, used);
4932 size = i < 0 ? make_number (DEFAULT_HASH_SIZE) : args[i];
4933 if (!INTEGERP (size) || XINT (size) < 0)
4934 Fsignal (Qerror,
4935 list2 (build_string ("Invalid hash table size"),
4936 size));
4938 /* Look for `:rehash-size SIZE'. */
4939 i = get_key_arg (QCrehash_size, nargs, args, used);
4940 rehash_size = i < 0 ? make_float (DEFAULT_REHASH_SIZE) : args[i];
4941 if (!NUMBERP (rehash_size)
4942 || (INTEGERP (rehash_size) && XINT (rehash_size) <= 0)
4943 || XFLOATINT (rehash_size) <= 1.0)
4944 Fsignal (Qerror,
4945 list2 (build_string ("Invalid hash table rehash size"),
4946 rehash_size));
4948 /* Look for `:rehash-threshold THRESHOLD'. */
4949 i = get_key_arg (QCrehash_threshold, nargs, args, used);
4950 rehash_threshold = i < 0 ? make_float (DEFAULT_REHASH_THRESHOLD) : args[i];
4951 if (!FLOATP (rehash_threshold)
4952 || XFLOATINT (rehash_threshold) <= 0.0
4953 || XFLOATINT (rehash_threshold) > 1.0)
4954 Fsignal (Qerror,
4955 list2 (build_string ("Invalid hash table rehash threshold"),
4956 rehash_threshold));
4958 /* Look for `:weakness WEAK'. */
4959 i = get_key_arg (QCweakness, nargs, args, used);
4960 weak = i < 0 ? Qnil : args[i];
4961 if (EQ (weak, Qt))
4962 weak = Qkey_and_value;
4963 if (!NILP (weak)
4964 && !EQ (weak, Qkey)
4965 && !EQ (weak, Qvalue)
4966 && !EQ (weak, Qkey_or_value)
4967 && !EQ (weak, Qkey_and_value))
4968 Fsignal (Qerror, list2 (build_string ("Invalid hash table weakness"),
4969 weak));
4971 /* Now, all args should have been used up, or there's a problem. */
4972 for (i = 0; i < nargs; ++i)
4973 if (!used[i])
4974 Fsignal (Qerror,
4975 list2 (build_string ("Invalid argument list"), args[i]));
4977 return make_hash_table (test, size, rehash_size, rehash_threshold, weak,
4978 user_test, user_hash);
4982 DEFUN ("copy-hash-table", Fcopy_hash_table, Scopy_hash_table, 1, 1, 0,
4983 doc: /* Return a copy of hash table TABLE. */)
4984 (table)
4985 Lisp_Object table;
4987 return copy_hash_table (check_hash_table (table));
4991 DEFUN ("makehash", Fmakehash, Smakehash, 0, 1, 0,
4992 doc: /* Create a new hash table.
4994 Optional first argument TEST specifies how to compare keys in the
4995 table. Predefined tests are `eq', `eql', and `equal'. Default is
4996 `eql'. New tests can be defined with `define-hash-table-test'. */)
4997 (test)
4998 Lisp_Object test;
5000 Lisp_Object args[2];
5001 args[0] = QCtest;
5002 args[1] = NILP (test) ? Qeql : test;
5003 return Fmake_hash_table (2, args);
5007 DEFUN ("hash-table-count", Fhash_table_count, Shash_table_count, 1, 1, 0,
5008 doc: /* Return the number of elements in TABLE. */)
5009 (table)
5010 Lisp_Object table;
5012 return check_hash_table (table)->count;
5016 DEFUN ("hash-table-rehash-size", Fhash_table_rehash_size,
5017 Shash_table_rehash_size, 1, 1, 0,
5018 doc: /* Return the current rehash size of TABLE. */)
5019 (table)
5020 Lisp_Object table;
5022 return check_hash_table (table)->rehash_size;
5026 DEFUN ("hash-table-rehash-threshold", Fhash_table_rehash_threshold,
5027 Shash_table_rehash_threshold, 1, 1, 0,
5028 doc: /* Return the current rehash threshold of TABLE. */)
5029 (table)
5030 Lisp_Object table;
5032 return check_hash_table (table)->rehash_threshold;
5036 DEFUN ("hash-table-size", Fhash_table_size, Shash_table_size, 1, 1, 0,
5037 doc: /* Return the size of TABLE.
5038 The size can be used as an argument to `make-hash-table' to create
5039 a hash table than can hold as many elements of TABLE holds
5040 without need for resizing. */)
5041 (table)
5042 Lisp_Object table;
5044 struct Lisp_Hash_Table *h = check_hash_table (table);
5045 return make_number (HASH_TABLE_SIZE (h));
5049 DEFUN ("hash-table-test", Fhash_table_test, Shash_table_test, 1, 1, 0,
5050 doc: /* Return the test TABLE uses. */)
5051 (table)
5052 Lisp_Object table;
5054 return check_hash_table (table)->test;
5058 DEFUN ("hash-table-weakness", Fhash_table_weakness, Shash_table_weakness,
5059 1, 1, 0,
5060 doc: /* Return the weakness of TABLE. */)
5061 (table)
5062 Lisp_Object table;
5064 return check_hash_table (table)->weak;
5068 DEFUN ("hash-table-p", Fhash_table_p, Shash_table_p, 1, 1, 0,
5069 doc: /* Return t if OBJ is a Lisp hash table object. */)
5070 (obj)
5071 Lisp_Object obj;
5073 return HASH_TABLE_P (obj) ? Qt : Qnil;
5077 DEFUN ("clrhash", Fclrhash, Sclrhash, 1, 1, 0,
5078 doc: /* Clear hash table TABLE. */)
5079 (table)
5080 Lisp_Object table;
5082 hash_clear (check_hash_table (table));
5083 return Qnil;
5087 DEFUN ("gethash", Fgethash, Sgethash, 2, 3, 0,
5088 doc: /* Look up KEY in TABLE and return its associated value.
5089 If KEY is not found, return DFLT which defaults to nil. */)
5090 (key, table, dflt)
5091 Lisp_Object key, table, dflt;
5093 struct Lisp_Hash_Table *h = check_hash_table (table);
5094 int i = hash_lookup (h, key, NULL);
5095 return i >= 0 ? HASH_VALUE (h, i) : dflt;
5099 DEFUN ("puthash", Fputhash, Sputhash, 3, 3, 0,
5100 doc: /* Associate KEY with VALUE in hash table TABLE.
5101 If KEY is already present in table, replace its current value with
5102 VALUE. */)
5103 (key, value, table)
5104 Lisp_Object key, value, table;
5106 struct Lisp_Hash_Table *h = check_hash_table (table);
5107 int i;
5108 unsigned hash;
5110 i = hash_lookup (h, key, &hash);
5111 if (i >= 0)
5112 HASH_VALUE (h, i) = value;
5113 else
5114 hash_put (h, key, value, hash);
5116 return value;
5120 DEFUN ("remhash", Fremhash, Sremhash, 2, 2, 0,
5121 doc: /* Remove KEY from TABLE. */)
5122 (key, table)
5123 Lisp_Object key, table;
5125 struct Lisp_Hash_Table *h = check_hash_table (table);
5126 hash_remove (h, key);
5127 return Qnil;
5131 DEFUN ("maphash", Fmaphash, Smaphash, 2, 2, 0,
5132 doc: /* Call FUNCTION for all entries in hash table TABLE.
5133 FUNCTION is called with 2 arguments KEY and VALUE. */)
5134 (function, table)
5135 Lisp_Object function, table;
5137 struct Lisp_Hash_Table *h = check_hash_table (table);
5138 Lisp_Object args[3];
5139 int i;
5141 for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
5142 if (!NILP (HASH_HASH (h, i)))
5144 args[0] = function;
5145 args[1] = HASH_KEY (h, i);
5146 args[2] = HASH_VALUE (h, i);
5147 Ffuncall (3, args);
5150 return Qnil;
5154 DEFUN ("define-hash-table-test", Fdefine_hash_table_test,
5155 Sdefine_hash_table_test, 3, 3, 0,
5156 doc: /* Define a new hash table test with name NAME, a symbol.
5158 In hash tables created with NAME specified as test, use TEST to
5159 compare keys, and HASH for computing hash codes of keys.
5161 TEST must be a function taking two arguments and returning non-nil if
5162 both arguments are the same. HASH must be a function taking one
5163 argument and return an integer that is the hash code of the argument.
5164 Hash code computation should use the whole value range of integers,
5165 including negative integers. */)
5166 (name, test, hash)
5167 Lisp_Object name, test, hash;
5169 return Fput (name, Qhash_table_test, list2 (test, hash));
5174 /************************************************************************
5176 ************************************************************************/
5178 #include "md5.h"
5179 #include "coding.h"
5181 DEFUN ("md5", Fmd5, Smd5, 1, 5, 0,
5182 doc: /* Return MD5 message digest of OBJECT, a buffer or string.
5184 A message digest is a cryptographic checksum of a document, and the
5185 algorithm to calculate it is defined in RFC 1321.
5187 The two optional arguments START and END are character positions
5188 specifying for which part of OBJECT the message digest should be
5189 computed. If nil or omitted, the digest is computed for the whole
5190 OBJECT.
5192 The MD5 message digest is computed from the result of encoding the
5193 text in a coding system, not directly from the internal Emacs form of
5194 the text. The optional fourth argument CODING-SYSTEM specifies which
5195 coding system to encode the text with. It should be the same coding
5196 system that you used or will use when actually writing the text into a
5197 file.
5199 If CODING-SYSTEM is nil or omitted, the default depends on OBJECT. If
5200 OBJECT is a buffer, the default for CODING-SYSTEM is whatever coding
5201 system would be chosen by default for writing this text into a file.
5203 If OBJECT is a string, the most preferred coding system (see the
5204 command `prefer-coding-system') is used.
5206 If NOERROR is non-nil, silently assume the `raw-text' coding if the
5207 guesswork fails. Normally, an error is signaled in such case. */)
5208 (object, start, end, coding_system, noerror)
5209 Lisp_Object object, start, end, coding_system, noerror;
5211 unsigned char digest[16];
5212 unsigned char value[33];
5213 int i;
5214 int size;
5215 int size_byte = 0;
5216 int start_char = 0, end_char = 0;
5217 int start_byte = 0, end_byte = 0;
5218 register int b, e;
5219 register struct buffer *bp;
5220 int temp;
5222 if (STRINGP (object))
5224 if (NILP (coding_system))
5226 /* Decide the coding-system to encode the data with. */
5228 if (STRING_MULTIBYTE (object))
5229 /* use default, we can't guess correct value */
5230 coding_system = SYMBOL_VALUE (XCAR (Vcoding_category_list));
5231 else
5232 coding_system = Qraw_text;
5235 if (NILP (Fcoding_system_p (coding_system)))
5237 /* Invalid coding system. */
5239 if (!NILP (noerror))
5240 coding_system = Qraw_text;
5241 else
5242 while (1)
5243 Fsignal (Qcoding_system_error, Fcons (coding_system, Qnil));
5246 if (STRING_MULTIBYTE (object))
5247 object = code_convert_string1 (object, coding_system, Qnil, 1);
5249 size = XSTRING (object)->size;
5250 size_byte = STRING_BYTES (XSTRING (object));
5252 if (!NILP (start))
5254 CHECK_NUMBER (start);
5256 start_char = XINT (start);
5258 if (start_char < 0)
5259 start_char += size;
5261 start_byte = string_char_to_byte (object, start_char);
5264 if (NILP (end))
5266 end_char = size;
5267 end_byte = size_byte;
5269 else
5271 CHECK_NUMBER (end);
5273 end_char = XINT (end);
5275 if (end_char < 0)
5276 end_char += size;
5278 end_byte = string_char_to_byte (object, end_char);
5281 if (!(0 <= start_char && start_char <= end_char && end_char <= size))
5282 args_out_of_range_3 (object, make_number (start_char),
5283 make_number (end_char));
5285 else
5287 CHECK_BUFFER (object);
5289 bp = XBUFFER (object);
5291 if (NILP (start))
5292 b = BUF_BEGV (bp);
5293 else
5295 CHECK_NUMBER_COERCE_MARKER (start);
5296 b = XINT (start);
5299 if (NILP (end))
5300 e = BUF_ZV (bp);
5301 else
5303 CHECK_NUMBER_COERCE_MARKER (end);
5304 e = XINT (end);
5307 if (b > e)
5308 temp = b, b = e, e = temp;
5310 if (!(BUF_BEGV (bp) <= b && e <= BUF_ZV (bp)))
5311 args_out_of_range (start, end);
5313 if (NILP (coding_system))
5315 /* Decide the coding-system to encode the data with.
5316 See fileio.c:Fwrite-region */
5318 if (!NILP (Vcoding_system_for_write))
5319 coding_system = Vcoding_system_for_write;
5320 else
5322 int force_raw_text = 0;
5324 coding_system = XBUFFER (object)->buffer_file_coding_system;
5325 if (NILP (coding_system)
5326 || NILP (Flocal_variable_p (Qbuffer_file_coding_system, Qnil)))
5328 coding_system = Qnil;
5329 if (NILP (current_buffer->enable_multibyte_characters))
5330 force_raw_text = 1;
5333 if (NILP (coding_system) && !NILP (Fbuffer_file_name(object)))
5335 /* Check file-coding-system-alist. */
5336 Lisp_Object args[4], val;
5338 args[0] = Qwrite_region; args[1] = start; args[2] = end;
5339 args[3] = Fbuffer_file_name(object);
5340 val = Ffind_operation_coding_system (4, args);
5341 if (CONSP (val) && !NILP (XCDR (val)))
5342 coding_system = XCDR (val);
5345 if (NILP (coding_system)
5346 && !NILP (XBUFFER (object)->buffer_file_coding_system))
5348 /* If we still have not decided a coding system, use the
5349 default value of buffer-file-coding-system. */
5350 coding_system = XBUFFER (object)->buffer_file_coding_system;
5353 if (!force_raw_text
5354 && !NILP (Ffboundp (Vselect_safe_coding_system_function)))
5355 /* Confirm that VAL can surely encode the current region. */
5356 coding_system = call4 (Vselect_safe_coding_system_function,
5357 make_number (b), make_number (e),
5358 coding_system, Qnil);
5360 if (force_raw_text)
5361 coding_system = Qraw_text;
5364 if (NILP (Fcoding_system_p (coding_system)))
5366 /* Invalid coding system. */
5368 if (!NILP (noerror))
5369 coding_system = Qraw_text;
5370 else
5371 while (1)
5372 Fsignal (Qcoding_system_error, Fcons (coding_system, Qnil));
5376 object = make_buffer_string (b, e, 0);
5378 if (STRING_MULTIBYTE (object))
5379 object = code_convert_string1 (object, coding_system, Qnil, 1);
5382 md5_buffer (XSTRING (object)->data + start_byte,
5383 STRING_BYTES(XSTRING (object)) - (size_byte - end_byte),
5384 digest);
5386 for (i = 0; i < 16; i++)
5387 sprintf (&value[2 * i], "%02x", digest[i]);
5388 value[32] = '\0';
5390 return make_string (value, 32);
5394 void
5395 syms_of_fns ()
5397 /* Hash table stuff. */
5398 Qhash_table_p = intern ("hash-table-p");
5399 staticpro (&Qhash_table_p);
5400 Qeq = intern ("eq");
5401 staticpro (&Qeq);
5402 Qeql = intern ("eql");
5403 staticpro (&Qeql);
5404 Qequal = intern ("equal");
5405 staticpro (&Qequal);
5406 QCtest = intern (":test");
5407 staticpro (&QCtest);
5408 QCsize = intern (":size");
5409 staticpro (&QCsize);
5410 QCrehash_size = intern (":rehash-size");
5411 staticpro (&QCrehash_size);
5412 QCrehash_threshold = intern (":rehash-threshold");
5413 staticpro (&QCrehash_threshold);
5414 QCweakness = intern (":weakness");
5415 staticpro (&QCweakness);
5416 Qkey = intern ("key");
5417 staticpro (&Qkey);
5418 Qvalue = intern ("value");
5419 staticpro (&Qvalue);
5420 Qhash_table_test = intern ("hash-table-test");
5421 staticpro (&Qhash_table_test);
5422 Qkey_or_value = intern ("key-or-value");
5423 staticpro (&Qkey_or_value);
5424 Qkey_and_value = intern ("key-and-value");
5425 staticpro (&Qkey_and_value);
5427 defsubr (&Ssxhash);
5428 defsubr (&Smake_hash_table);
5429 defsubr (&Scopy_hash_table);
5430 defsubr (&Smakehash);
5431 defsubr (&Shash_table_count);
5432 defsubr (&Shash_table_rehash_size);
5433 defsubr (&Shash_table_rehash_threshold);
5434 defsubr (&Shash_table_size);
5435 defsubr (&Shash_table_test);
5436 defsubr (&Shash_table_weakness);
5437 defsubr (&Shash_table_p);
5438 defsubr (&Sclrhash);
5439 defsubr (&Sgethash);
5440 defsubr (&Sputhash);
5441 defsubr (&Sremhash);
5442 defsubr (&Smaphash);
5443 defsubr (&Sdefine_hash_table_test);
5445 Qstring_lessp = intern ("string-lessp");
5446 staticpro (&Qstring_lessp);
5447 Qprovide = intern ("provide");
5448 staticpro (&Qprovide);
5449 Qrequire = intern ("require");
5450 staticpro (&Qrequire);
5451 Qyes_or_no_p_history = intern ("yes-or-no-p-history");
5452 staticpro (&Qyes_or_no_p_history);
5453 Qcursor_in_echo_area = intern ("cursor-in-echo-area");
5454 staticpro (&Qcursor_in_echo_area);
5455 Qwidget_type = intern ("widget-type");
5456 staticpro (&Qwidget_type);
5458 staticpro (&string_char_byte_cache_string);
5459 string_char_byte_cache_string = Qnil;
5461 require_nesting_list = Qnil;
5462 staticpro (&require_nesting_list);
5464 Fset (Qyes_or_no_p_history, Qnil);
5466 DEFVAR_LISP ("features", &Vfeatures,
5467 doc: /* A list of symbols which are the features of the executing emacs.
5468 Used by `featurep' and `require', and altered by `provide'. */);
5469 Vfeatures = Qnil;
5470 Qsubfeatures = intern ("subfeatures");
5471 staticpro (&Qsubfeatures);
5473 DEFVAR_BOOL ("use-dialog-box", &use_dialog_box,
5474 doc: /* *Non-nil means mouse commands use dialog boxes to ask questions.
5475 This applies to `y-or-n-p' and `yes-or-no-p' questions asked by commands
5476 invoked by mouse clicks and mouse menu items. */);
5477 use_dialog_box = 1;
5479 defsubr (&Sidentity);
5480 defsubr (&Srandom);
5481 defsubr (&Slength);
5482 defsubr (&Ssafe_length);
5483 defsubr (&Sstring_bytes);
5484 defsubr (&Sstring_equal);
5485 defsubr (&Scompare_strings);
5486 defsubr (&Sstring_lessp);
5487 defsubr (&Sappend);
5488 defsubr (&Sconcat);
5489 defsubr (&Svconcat);
5490 defsubr (&Scopy_sequence);
5491 defsubr (&Sstring_make_multibyte);
5492 defsubr (&Sstring_make_unibyte);
5493 defsubr (&Sstring_as_multibyte);
5494 defsubr (&Sstring_as_unibyte);
5495 defsubr (&Scopy_alist);
5496 defsubr (&Ssubstring);
5497 defsubr (&Ssubstring_no_properties);
5498 defsubr (&Snthcdr);
5499 defsubr (&Snth);
5500 defsubr (&Selt);
5501 defsubr (&Smember);
5502 defsubr (&Smemq);
5503 defsubr (&Sassq);
5504 defsubr (&Sassoc);
5505 defsubr (&Srassq);
5506 defsubr (&Srassoc);
5507 defsubr (&Sdelq);
5508 defsubr (&Sdelete);
5509 defsubr (&Snreverse);
5510 defsubr (&Sreverse);
5511 defsubr (&Ssort);
5512 defsubr (&Splist_get);
5513 defsubr (&Sget);
5514 defsubr (&Splist_put);
5515 defsubr (&Sput);
5516 defsubr (&Slax_plist_get);
5517 defsubr (&Slax_plist_put);
5518 defsubr (&Sequal);
5519 defsubr (&Sfillarray);
5520 defsubr (&Schar_table_subtype);
5521 defsubr (&Schar_table_parent);
5522 defsubr (&Sset_char_table_parent);
5523 defsubr (&Schar_table_extra_slot);
5524 defsubr (&Sset_char_table_extra_slot);
5525 defsubr (&Schar_table_range);
5526 defsubr (&Sset_char_table_range);
5527 defsubr (&Sset_char_table_default);
5528 defsubr (&Soptimize_char_table);
5529 defsubr (&Smap_char_table);
5530 defsubr (&Snconc);
5531 defsubr (&Smapcar);
5532 defsubr (&Smapc);
5533 defsubr (&Smapconcat);
5534 defsubr (&Sy_or_n_p);
5535 defsubr (&Syes_or_no_p);
5536 defsubr (&Sload_average);
5537 defsubr (&Sfeaturep);
5538 defsubr (&Srequire);
5539 defsubr (&Sprovide);
5540 defsubr (&Splist_member);
5541 defsubr (&Swidget_put);
5542 defsubr (&Swidget_get);
5543 defsubr (&Swidget_apply);
5544 defsubr (&Sbase64_encode_region);
5545 defsubr (&Sbase64_decode_region);
5546 defsubr (&Sbase64_encode_string);
5547 defsubr (&Sbase64_decode_string);
5548 defsubr (&Smd5);
5552 void
5553 init_fns ()
5555 Vweak_hash_tables = Qnil;