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