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