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