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