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