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