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