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