]> code.delx.au - gnu-emacs/blob - src/fns.c
(Fstring_to_multibyte): Fix typo in the docstring.
[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, they won't need any more bytes
1057 once converted. In that case, we can return STRING itself. */
1058 if (nbytes == SBYTES (string))
1059 return string;
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 category ITEM, if available.
3452
3453 ITEM may be one of the following:
3454 `codeset', returning the character set as a string (CODESET);
3455 `days', returning a 7-element vector of day names (DAY_n);
3456 `months', returning a 12-element vector of month names (MON_n).
3457
3458 If the system can't provide such information through a call to
3459 nl_langinfo(3), return nil.
3460
3461 The data read from the system are decoded using `locale-coding-system'. */)
3462 (item)
3463 Lisp_Object item;
3464 {
3465 char *str = NULL;
3466 #ifdef HAVE_LANGINFO_CODESET
3467 Lisp_Object val;
3468 if (EQ (item, Qcodeset))
3469 {
3470 str = nl_langinfo (CODESET);
3471 return build_string (str);
3472 }
3473 #ifdef DAY_1
3474 else if (EQ (item, Qdays)) /* e.g. for calendar-day-name-array */
3475 {
3476 Lisp_Object v = Fmake_vector (make_number (7), Qnil);
3477 int days[7] = {DAY_1, DAY_2, DAY_3, DAY_4, DAY_5, DAY_6, DAY_7};
3478 int i;
3479 synchronize_system_time_locale ();
3480 for (i = 0; i < 7; i++)
3481 {
3482 str = nl_langinfo (days[i]);
3483 val = make_unibyte_string (str, strlen (str));
3484 /* Fixme: Is this coding system necessarily right, even if
3485 it is consistent with CODESET? If not, what to do? */
3486 Faset (v, make_number (i),
3487 code_convert_string_norecord (val, Vlocale_coding_system,
3488 Qnil));
3489 }
3490 return v;
3491 }
3492 #endif /* DAY_1 */
3493 #ifdef MON_1
3494 else if (EQ (item, Qmonths)) /* e.g. for calendar-month-name-array */
3495 {
3496 struct Lisp_Vector *p = allocate_vector (12);
3497 int months[12] = {MON_1, MON_2, MON_3, MON_4, MON_5, MON_6, MON_7,
3498 MON_8, MON_9, MON_10, MON_11, MON_12};
3499 int i;
3500 synchronize_system_time_locale ();
3501 for (i = 0; i < 12; i++)
3502 {
3503 str = nl_langinfo (months[i]);
3504 val = make_unibyte_string (str, strlen (str));
3505 p->contents[i] =
3506 code_convert_string_norecord (val, Vlocale_coding_system, Qnil);
3507 }
3508 XSETVECTOR (val, p);
3509 return val;
3510 }
3511 #endif /* MON_1 */
3512 /* LC_PAPER stuff isn't defined as accessible in glibc as of 2.3.1,
3513 but is in the locale files. This could be used by ps-print. */
3514 #ifdef PAPER_WIDTH
3515 else if (EQ (item, Qpaper))
3516 {
3517 return list2 (make_number (nl_langinfo (PAPER_WIDTH)),
3518 make_number (nl_langinfo (PAPER_HEIGHT)));
3519 }
3520 #endif /* PAPER_WIDTH */
3521 #endif /* HAVE_LANGINFO_CODESET*/
3522 return Qnil;
3523 }
3524 \f
3525 /* base64 encode/decode functions (RFC 2045).
3526 Based on code from GNU recode. */
3527
3528 #define MIME_LINE_LENGTH 76
3529
3530 #define IS_ASCII(Character) \
3531 ((Character) < 128)
3532 #define IS_BASE64(Character) \
3533 (IS_ASCII (Character) && base64_char_to_value[Character] >= 0)
3534 #define IS_BASE64_IGNORABLE(Character) \
3535 ((Character) == ' ' || (Character) == '\t' || (Character) == '\n' \
3536 || (Character) == '\f' || (Character) == '\r')
3537
3538 /* Used by base64_decode_1 to retrieve a non-base64-ignorable
3539 character or return retval if there are no characters left to
3540 process. */
3541 #define READ_QUADRUPLET_BYTE(retval) \
3542 do \
3543 { \
3544 if (i == length) \
3545 { \
3546 if (nchars_return) \
3547 *nchars_return = nchars; \
3548 return (retval); \
3549 } \
3550 c = from[i++]; \
3551 } \
3552 while (IS_BASE64_IGNORABLE (c))
3553
3554 /* Don't use alloca for regions larger than this, lest we overflow
3555 their stack. */
3556 #define MAX_ALLOCA 16*1024
3557
3558 /* Table of characters coding the 64 values. */
3559 static char base64_value_to_char[64] =
3560 {
3561 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 0- 9 */
3562 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 10-19 */
3563 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', /* 20-29 */
3564 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', /* 30-39 */
3565 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', /* 40-49 */
3566 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', /* 50-59 */
3567 '8', '9', '+', '/' /* 60-63 */
3568 };
3569
3570 /* Table of base64 values for first 128 characters. */
3571 static short base64_char_to_value[128] =
3572 {
3573 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0- 9 */
3574 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10- 19 */
3575 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20- 29 */
3576 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 30- 39 */
3577 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, /* 40- 49 */
3578 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, /* 50- 59 */
3579 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, /* 60- 69 */
3580 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 70- 79 */
3581 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, /* 80- 89 */
3582 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, /* 90- 99 */
3583 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, /* 100-109 */
3584 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, /* 110-119 */
3585 49, 50, 51, -1, -1, -1, -1, -1 /* 120-127 */
3586 };
3587
3588 /* The following diagram shows the logical steps by which three octets
3589 get transformed into four base64 characters.
3590
3591 .--------. .--------. .--------.
3592 |aaaaaabb| |bbbbcccc| |ccdddddd|
3593 `--------' `--------' `--------'
3594 6 2 4 4 2 6
3595 .--------+--------+--------+--------.
3596 |00aaaaaa|00bbbbbb|00cccccc|00dddddd|
3597 `--------+--------+--------+--------'
3598
3599 .--------+--------+--------+--------.
3600 |AAAAAAAA|BBBBBBBB|CCCCCCCC|DDDDDDDD|
3601 `--------+--------+--------+--------'
3602
3603 The octets are divided into 6 bit chunks, which are then encoded into
3604 base64 characters. */
3605
3606
3607 static int base64_encode_1 P_ ((const char *, char *, int, int, int));
3608 static int base64_decode_1 P_ ((const char *, char *, int, int, int *));
3609
3610 DEFUN ("base64-encode-region", Fbase64_encode_region, Sbase64_encode_region,
3611 2, 3, "r",
3612 doc: /* Base64-encode the region between BEG and END.
3613 Return the length of the encoded text.
3614 Optional third argument NO-LINE-BREAK means do not break long lines
3615 into shorter lines. */)
3616 (beg, end, no_line_break)
3617 Lisp_Object beg, end, no_line_break;
3618 {
3619 char *encoded;
3620 int allength, length;
3621 int ibeg, iend, encoded_length;
3622 int old_pos = PT;
3623
3624 validate_region (&beg, &end);
3625
3626 ibeg = CHAR_TO_BYTE (XFASTINT (beg));
3627 iend = CHAR_TO_BYTE (XFASTINT (end));
3628 move_gap_both (XFASTINT (beg), ibeg);
3629
3630 /* We need to allocate enough room for encoding the text.
3631 We need 33 1/3% more space, plus a newline every 76
3632 characters, and then we round up. */
3633 length = iend - ibeg;
3634 allength = length + length/3 + 1;
3635 allength += allength / MIME_LINE_LENGTH + 1 + 6;
3636
3637 if (allength <= MAX_ALLOCA)
3638 encoded = (char *) alloca (allength);
3639 else
3640 encoded = (char *) xmalloc (allength);
3641 encoded_length = base64_encode_1 (BYTE_POS_ADDR (ibeg), encoded, length,
3642 NILP (no_line_break),
3643 !NILP (current_buffer->enable_multibyte_characters));
3644 if (encoded_length > allength)
3645 abort ();
3646
3647 if (encoded_length < 0)
3648 {
3649 /* The encoding wasn't possible. */
3650 if (length > MAX_ALLOCA)
3651 xfree (encoded);
3652 error ("Multibyte character in data for base64 encoding");
3653 }
3654
3655 /* Now we have encoded the region, so we insert the new contents
3656 and delete the old. (Insert first in order to preserve markers.) */
3657 SET_PT_BOTH (XFASTINT (beg), ibeg);
3658 insert (encoded, encoded_length);
3659 if (allength > MAX_ALLOCA)
3660 xfree (encoded);
3661 del_range_byte (ibeg + encoded_length, iend + encoded_length, 1);
3662
3663 /* If point was outside of the region, restore it exactly; else just
3664 move to the beginning of the region. */
3665 if (old_pos >= XFASTINT (end))
3666 old_pos += encoded_length - (XFASTINT (end) - XFASTINT (beg));
3667 else if (old_pos > XFASTINT (beg))
3668 old_pos = XFASTINT (beg);
3669 SET_PT (old_pos);
3670
3671 /* We return the length of the encoded text. */
3672 return make_number (encoded_length);
3673 }
3674
3675 DEFUN ("base64-encode-string", Fbase64_encode_string, Sbase64_encode_string,
3676 1, 2, 0,
3677 doc: /* Base64-encode STRING and return the result.
3678 Optional second argument NO-LINE-BREAK means do not break long lines
3679 into shorter lines. */)
3680 (string, no_line_break)
3681 Lisp_Object string, no_line_break;
3682 {
3683 int allength, length, encoded_length;
3684 char *encoded;
3685 Lisp_Object encoded_string;
3686
3687 CHECK_STRING (string);
3688
3689 /* We need to allocate enough room for encoding the text.
3690 We need 33 1/3% more space, plus a newline every 76
3691 characters, and then we round up. */
3692 length = SBYTES (string);
3693 allength = length + length/3 + 1;
3694 allength += allength / MIME_LINE_LENGTH + 1 + 6;
3695
3696 /* We need to allocate enough room for decoding the text. */
3697 if (allength <= MAX_ALLOCA)
3698 encoded = (char *) alloca (allength);
3699 else
3700 encoded = (char *) xmalloc (allength);
3701
3702 encoded_length = base64_encode_1 (SDATA (string),
3703 encoded, length, NILP (no_line_break),
3704 STRING_MULTIBYTE (string));
3705 if (encoded_length > allength)
3706 abort ();
3707
3708 if (encoded_length < 0)
3709 {
3710 /* The encoding wasn't possible. */
3711 if (length > MAX_ALLOCA)
3712 xfree (encoded);
3713 error ("Multibyte character in data for base64 encoding");
3714 }
3715
3716 encoded_string = make_unibyte_string (encoded, encoded_length);
3717 if (allength > MAX_ALLOCA)
3718 xfree (encoded);
3719
3720 return encoded_string;
3721 }
3722
3723 static int
3724 base64_encode_1 (from, to, length, line_break, multibyte)
3725 const char *from;
3726 char *to;
3727 int length;
3728 int line_break;
3729 int multibyte;
3730 {
3731 int counter = 0, i = 0;
3732 char *e = to;
3733 int c;
3734 unsigned int value;
3735 int bytes;
3736
3737 while (i < length)
3738 {
3739 if (multibyte)
3740 {
3741 c = STRING_CHAR_AND_LENGTH (from + i, length - i, bytes);
3742 if (c >= 256)
3743 return -1;
3744 i += bytes;
3745 }
3746 else
3747 c = from[i++];
3748
3749 /* Wrap line every 76 characters. */
3750
3751 if (line_break)
3752 {
3753 if (counter < MIME_LINE_LENGTH / 4)
3754 counter++;
3755 else
3756 {
3757 *e++ = '\n';
3758 counter = 1;
3759 }
3760 }
3761
3762 /* Process first byte of a triplet. */
3763
3764 *e++ = base64_value_to_char[0x3f & c >> 2];
3765 value = (0x03 & c) << 4;
3766
3767 /* Process second byte of a triplet. */
3768
3769 if (i == length)
3770 {
3771 *e++ = base64_value_to_char[value];
3772 *e++ = '=';
3773 *e++ = '=';
3774 break;
3775 }
3776
3777 if (multibyte)
3778 {
3779 c = STRING_CHAR_AND_LENGTH (from + i, length - i, bytes);
3780 if (c >= 256)
3781 return -1;
3782 i += bytes;
3783 }
3784 else
3785 c = from[i++];
3786
3787 *e++ = base64_value_to_char[value | (0x0f & c >> 4)];
3788 value = (0x0f & c) << 2;
3789
3790 /* Process third byte of a triplet. */
3791
3792 if (i == length)
3793 {
3794 *e++ = base64_value_to_char[value];
3795 *e++ = '=';
3796 break;
3797 }
3798
3799 if (multibyte)
3800 {
3801 c = STRING_CHAR_AND_LENGTH (from + i, length - i, bytes);
3802 if (c >= 256)
3803 return -1;
3804 i += bytes;
3805 }
3806 else
3807 c = from[i++];
3808
3809 *e++ = base64_value_to_char[value | (0x03 & c >> 6)];
3810 *e++ = base64_value_to_char[0x3f & c];
3811 }
3812
3813 return e - to;
3814 }
3815
3816
3817 DEFUN ("base64-decode-region", Fbase64_decode_region, Sbase64_decode_region,
3818 2, 2, "r",
3819 doc: /* Base64-decode the region between BEG and END.
3820 Return the length of the decoded text.
3821 If the region can't be decoded, signal an error and don't modify the buffer. */)
3822 (beg, end)
3823 Lisp_Object beg, end;
3824 {
3825 int ibeg, iend, length, allength;
3826 char *decoded;
3827 int old_pos = PT;
3828 int decoded_length;
3829 int inserted_chars;
3830 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
3831
3832 validate_region (&beg, &end);
3833
3834 ibeg = CHAR_TO_BYTE (XFASTINT (beg));
3835 iend = CHAR_TO_BYTE (XFASTINT (end));
3836
3837 length = iend - ibeg;
3838
3839 /* We need to allocate enough room for decoding the text. If we are
3840 working on a multibyte buffer, each decoded code may occupy at
3841 most two bytes. */
3842 allength = multibyte ? length * 2 : length;
3843 if (allength <= MAX_ALLOCA)
3844 decoded = (char *) alloca (allength);
3845 else
3846 decoded = (char *) xmalloc (allength);
3847
3848 move_gap_both (XFASTINT (beg), ibeg);
3849 decoded_length = base64_decode_1 (BYTE_POS_ADDR (ibeg), decoded, length,
3850 multibyte, &inserted_chars);
3851 if (decoded_length > allength)
3852 abort ();
3853
3854 if (decoded_length < 0)
3855 {
3856 /* The decoding wasn't possible. */
3857 if (allength > MAX_ALLOCA)
3858 xfree (decoded);
3859 error ("Invalid base64 data");
3860 }
3861
3862 /* Now we have decoded the region, so we insert the new contents
3863 and delete the old. (Insert first in order to preserve markers.) */
3864 TEMP_SET_PT_BOTH (XFASTINT (beg), ibeg);
3865 insert_1_both (decoded, inserted_chars, decoded_length, 0, 1, 0);
3866 if (allength > MAX_ALLOCA)
3867 xfree (decoded);
3868 /* Delete the original text. */
3869 del_range_both (PT, PT_BYTE, XFASTINT (end) + inserted_chars,
3870 iend + decoded_length, 1);
3871
3872 /* If point was outside of the region, restore it exactly; else just
3873 move to the beginning of the region. */
3874 if (old_pos >= XFASTINT (end))
3875 old_pos += inserted_chars - (XFASTINT (end) - XFASTINT (beg));
3876 else if (old_pos > XFASTINT (beg))
3877 old_pos = XFASTINT (beg);
3878 SET_PT (old_pos > ZV ? ZV : old_pos);
3879
3880 return make_number (inserted_chars);
3881 }
3882
3883 DEFUN ("base64-decode-string", Fbase64_decode_string, Sbase64_decode_string,
3884 1, 1, 0,
3885 doc: /* Base64-decode STRING and return the result. */)
3886 (string)
3887 Lisp_Object string;
3888 {
3889 char *decoded;
3890 int length, decoded_length;
3891 Lisp_Object decoded_string;
3892
3893 CHECK_STRING (string);
3894
3895 length = SBYTES (string);
3896 /* We need to allocate enough room for decoding the text. */
3897 if (length <= MAX_ALLOCA)
3898 decoded = (char *) alloca (length);
3899 else
3900 decoded = (char *) xmalloc (length);
3901
3902 /* The decoded result should be unibyte. */
3903 decoded_length = base64_decode_1 (SDATA (string), decoded, length,
3904 0, NULL);
3905 if (decoded_length > length)
3906 abort ();
3907 else if (decoded_length >= 0)
3908 decoded_string = make_unibyte_string (decoded, decoded_length);
3909 else
3910 decoded_string = Qnil;
3911
3912 if (length > MAX_ALLOCA)
3913 xfree (decoded);
3914 if (!STRINGP (decoded_string))
3915 error ("Invalid base64 data");
3916
3917 return decoded_string;
3918 }
3919
3920 /* Base64-decode the data at FROM of LENGHT bytes into TO. If
3921 MULTIBYTE is nonzero, the decoded result should be in multibyte
3922 form. If NCHARS_RETRUN is not NULL, store the number of produced
3923 characters in *NCHARS_RETURN. */
3924
3925 static int
3926 base64_decode_1 (from, to, length, multibyte, nchars_return)
3927 const char *from;
3928 char *to;
3929 int length;
3930 int multibyte;
3931 int *nchars_return;
3932 {
3933 int i = 0;
3934 char *e = to;
3935 unsigned char c;
3936 unsigned long value;
3937 int nchars = 0;
3938
3939 while (1)
3940 {
3941 /* Process first byte of a quadruplet. */
3942
3943 READ_QUADRUPLET_BYTE (e-to);
3944
3945 if (!IS_BASE64 (c))
3946 return -1;
3947 value = base64_char_to_value[c] << 18;
3948
3949 /* Process second byte of a quadruplet. */
3950
3951 READ_QUADRUPLET_BYTE (-1);
3952
3953 if (!IS_BASE64 (c))
3954 return -1;
3955 value |= base64_char_to_value[c] << 12;
3956
3957 c = (unsigned char) (value >> 16);
3958 if (multibyte)
3959 e += CHAR_STRING (c, e);
3960 else
3961 *e++ = c;
3962 nchars++;
3963
3964 /* Process third byte of a quadruplet. */
3965
3966 READ_QUADRUPLET_BYTE (-1);
3967
3968 if (c == '=')
3969 {
3970 READ_QUADRUPLET_BYTE (-1);
3971
3972 if (c != '=')
3973 return -1;
3974 continue;
3975 }
3976
3977 if (!IS_BASE64 (c))
3978 return -1;
3979 value |= base64_char_to_value[c] << 6;
3980
3981 c = (unsigned char) (0xff & value >> 8);
3982 if (multibyte)
3983 e += CHAR_STRING (c, e);
3984 else
3985 *e++ = c;
3986 nchars++;
3987
3988 /* Process fourth byte of a quadruplet. */
3989
3990 READ_QUADRUPLET_BYTE (-1);
3991
3992 if (c == '=')
3993 continue;
3994
3995 if (!IS_BASE64 (c))
3996 return -1;
3997 value |= base64_char_to_value[c];
3998
3999 c = (unsigned char) (0xff & value);
4000 if (multibyte)
4001 e += CHAR_STRING (c, e);
4002 else
4003 *e++ = c;
4004 nchars++;
4005 }
4006 }
4007
4008
4009 \f
4010 /***********************************************************************
4011 ***** *****
4012 ***** Hash Tables *****
4013 ***** *****
4014 ***********************************************************************/
4015
4016 /* Implemented by gerd@gnu.org. This hash table implementation was
4017 inspired by CMUCL hash tables. */
4018
4019 /* Ideas:
4020
4021 1. For small tables, association lists are probably faster than
4022 hash tables because they have lower overhead.
4023
4024 For uses of hash tables where the O(1) behavior of table
4025 operations is not a requirement, it might therefore be a good idea
4026 not to hash. Instead, we could just do a linear search in the
4027 key_and_value vector of the hash table. This could be done
4028 if a `:linear-search t' argument is given to make-hash-table. */
4029
4030
4031 /* The list of all weak hash tables. Don't staticpro this one. */
4032
4033 Lisp_Object Vweak_hash_tables;
4034
4035 /* Various symbols. */
4036
4037 Lisp_Object Qhash_table_p, Qeq, Qeql, Qequal, Qkey, Qvalue;
4038 Lisp_Object QCtest, QCsize, QCrehash_size, QCrehash_threshold, QCweakness;
4039 Lisp_Object Qhash_table_test, Qkey_or_value, Qkey_and_value;
4040
4041 /* Function prototypes. */
4042
4043 static struct Lisp_Hash_Table *check_hash_table P_ ((Lisp_Object));
4044 static int get_key_arg P_ ((Lisp_Object, int, Lisp_Object *, char *));
4045 static void maybe_resize_hash_table P_ ((struct Lisp_Hash_Table *));
4046 static int cmpfn_eql P_ ((struct Lisp_Hash_Table *, Lisp_Object, unsigned,
4047 Lisp_Object, unsigned));
4048 static int cmpfn_equal P_ ((struct Lisp_Hash_Table *, Lisp_Object, unsigned,
4049 Lisp_Object, unsigned));
4050 static int cmpfn_user_defined P_ ((struct Lisp_Hash_Table *, Lisp_Object,
4051 unsigned, Lisp_Object, unsigned));
4052 static unsigned hashfn_eq P_ ((struct Lisp_Hash_Table *, Lisp_Object));
4053 static unsigned hashfn_eql P_ ((struct Lisp_Hash_Table *, Lisp_Object));
4054 static unsigned hashfn_equal P_ ((struct Lisp_Hash_Table *, Lisp_Object));
4055 static unsigned hashfn_user_defined P_ ((struct Lisp_Hash_Table *,
4056 Lisp_Object));
4057 static unsigned sxhash_string P_ ((unsigned char *, int));
4058 static unsigned sxhash_list P_ ((Lisp_Object, int));
4059 static unsigned sxhash_vector P_ ((Lisp_Object, int));
4060 static unsigned sxhash_bool_vector P_ ((Lisp_Object));
4061 static int sweep_weak_table P_ ((struct Lisp_Hash_Table *, int));
4062
4063
4064 \f
4065 /***********************************************************************
4066 Utilities
4067 ***********************************************************************/
4068
4069 /* If OBJ is a Lisp hash table, return a pointer to its struct
4070 Lisp_Hash_Table. Otherwise, signal an error. */
4071
4072 static struct Lisp_Hash_Table *
4073 check_hash_table (obj)
4074 Lisp_Object obj;
4075 {
4076 CHECK_HASH_TABLE (obj);
4077 return XHASH_TABLE (obj);
4078 }
4079
4080
4081 /* Value is the next integer I >= N, N >= 0 which is "almost" a prime
4082 number. */
4083
4084 int
4085 next_almost_prime (n)
4086 int n;
4087 {
4088 if (n % 2 == 0)
4089 n += 1;
4090 if (n % 3 == 0)
4091 n += 2;
4092 if (n % 7 == 0)
4093 n += 4;
4094 return n;
4095 }
4096
4097
4098 /* Find KEY in ARGS which has size NARGS. Don't consider indices for
4099 which USED[I] is non-zero. If found at index I in ARGS, set
4100 USED[I] and USED[I + 1] to 1, and return I + 1. Otherwise return
4101 -1. This function is used to extract a keyword/argument pair from
4102 a DEFUN parameter list. */
4103
4104 static int
4105 get_key_arg (key, nargs, args, used)
4106 Lisp_Object key;
4107 int nargs;
4108 Lisp_Object *args;
4109 char *used;
4110 {
4111 int i;
4112
4113 for (i = 0; i < nargs - 1; ++i)
4114 if (!used[i] && EQ (args[i], key))
4115 break;
4116
4117 if (i >= nargs - 1)
4118 i = -1;
4119 else
4120 {
4121 used[i++] = 1;
4122 used[i] = 1;
4123 }
4124
4125 return i;
4126 }
4127
4128
4129 /* Return a Lisp vector which has the same contents as VEC but has
4130 size NEW_SIZE, NEW_SIZE >= VEC->size. Entries in the resulting
4131 vector that are not copied from VEC are set to INIT. */
4132
4133 Lisp_Object
4134 larger_vector (vec, new_size, init)
4135 Lisp_Object vec;
4136 int new_size;
4137 Lisp_Object init;
4138 {
4139 struct Lisp_Vector *v;
4140 int i, old_size;
4141
4142 xassert (VECTORP (vec));
4143 old_size = XVECTOR (vec)->size;
4144 xassert (new_size >= old_size);
4145
4146 v = allocate_vector (new_size);
4147 bcopy (XVECTOR (vec)->contents, v->contents,
4148 old_size * sizeof *v->contents);
4149 for (i = old_size; i < new_size; ++i)
4150 v->contents[i] = init;
4151 XSETVECTOR (vec, v);
4152 return vec;
4153 }
4154
4155
4156 /***********************************************************************
4157 Low-level Functions
4158 ***********************************************************************/
4159
4160 /* Compare KEY1 which has hash code HASH1 and KEY2 with hash code
4161 HASH2 in hash table H using `eql'. Value is non-zero if KEY1 and
4162 KEY2 are the same. */
4163
4164 static int
4165 cmpfn_eql (h, key1, hash1, key2, hash2)
4166 struct Lisp_Hash_Table *h;
4167 Lisp_Object key1, key2;
4168 unsigned hash1, hash2;
4169 {
4170 return (FLOATP (key1)
4171 && FLOATP (key2)
4172 && XFLOAT_DATA (key1) == XFLOAT_DATA (key2));
4173 }
4174
4175
4176 /* Compare KEY1 which has hash code HASH1 and KEY2 with hash code
4177 HASH2 in hash table H using `equal'. Value is non-zero if KEY1 and
4178 KEY2 are the same. */
4179
4180 static int
4181 cmpfn_equal (h, key1, hash1, key2, hash2)
4182 struct Lisp_Hash_Table *h;
4183 Lisp_Object key1, key2;
4184 unsigned hash1, hash2;
4185 {
4186 return hash1 == hash2 && !NILP (Fequal (key1, key2));
4187 }
4188
4189
4190 /* Compare KEY1 which has hash code HASH1, and KEY2 with hash code
4191 HASH2 in hash table H using H->user_cmp_function. Value is non-zero
4192 if KEY1 and KEY2 are the same. */
4193
4194 static int
4195 cmpfn_user_defined (h, key1, hash1, key2, hash2)
4196 struct Lisp_Hash_Table *h;
4197 Lisp_Object key1, key2;
4198 unsigned hash1, hash2;
4199 {
4200 if (hash1 == hash2)
4201 {
4202 Lisp_Object args[3];
4203
4204 args[0] = h->user_cmp_function;
4205 args[1] = key1;
4206 args[2] = key2;
4207 return !NILP (Ffuncall (3, args));
4208 }
4209 else
4210 return 0;
4211 }
4212
4213
4214 /* Value is a hash code for KEY for use in hash table H which uses
4215 `eq' to compare keys. The hash code returned is guaranteed to fit
4216 in a Lisp integer. */
4217
4218 static unsigned
4219 hashfn_eq (h, key)
4220 struct Lisp_Hash_Table *h;
4221 Lisp_Object key;
4222 {
4223 unsigned hash = XUINT (key) ^ XGCTYPE (key);
4224 xassert ((hash & ~VALMASK) == 0);
4225 return hash;
4226 }
4227
4228
4229 /* Value is a hash code for KEY for use in hash table H which uses
4230 `eql' to compare keys. The hash code returned is guaranteed to fit
4231 in a Lisp integer. */
4232
4233 static unsigned
4234 hashfn_eql (h, key)
4235 struct Lisp_Hash_Table *h;
4236 Lisp_Object key;
4237 {
4238 unsigned hash;
4239 if (FLOATP (key))
4240 hash = sxhash (key, 0);
4241 else
4242 hash = XUINT (key) ^ XGCTYPE (key);
4243 xassert ((hash & ~VALMASK) == 0);
4244 return hash;
4245 }
4246
4247
4248 /* Value is a hash code for KEY for use in hash table H which uses
4249 `equal' to compare keys. The hash code returned is guaranteed to fit
4250 in a Lisp integer. */
4251
4252 static unsigned
4253 hashfn_equal (h, key)
4254 struct Lisp_Hash_Table *h;
4255 Lisp_Object key;
4256 {
4257 unsigned hash = sxhash (key, 0);
4258 xassert ((hash & ~VALMASK) == 0);
4259 return hash;
4260 }
4261
4262
4263 /* Value is a hash code for KEY for use in hash table H which uses as
4264 user-defined function to compare keys. The hash code returned is
4265 guaranteed to fit in a Lisp integer. */
4266
4267 static unsigned
4268 hashfn_user_defined (h, key)
4269 struct Lisp_Hash_Table *h;
4270 Lisp_Object key;
4271 {
4272 Lisp_Object args[2], hash;
4273
4274 args[0] = h->user_hash_function;
4275 args[1] = key;
4276 hash = Ffuncall (2, args);
4277 if (!INTEGERP (hash))
4278 Fsignal (Qerror,
4279 list2 (build_string ("Invalid hash code returned from \
4280 user-supplied hash function"),
4281 hash));
4282 return XUINT (hash);
4283 }
4284
4285
4286 /* Create and initialize a new hash table.
4287
4288 TEST specifies the test the hash table will use to compare keys.
4289 It must be either one of the predefined tests `eq', `eql' or
4290 `equal' or a symbol denoting a user-defined test named TEST with
4291 test and hash functions USER_TEST and USER_HASH.
4292
4293 Give the table initial capacity SIZE, SIZE >= 0, an integer.
4294
4295 If REHASH_SIZE is an integer, it must be > 0, and this hash table's
4296 new size when it becomes full is computed by adding REHASH_SIZE to
4297 its old size. If REHASH_SIZE is a float, it must be > 1.0, and the
4298 table's new size is computed by multiplying its old size with
4299 REHASH_SIZE.
4300
4301 REHASH_THRESHOLD must be a float <= 1.0, and > 0. The table will
4302 be resized when the ratio of (number of entries in the table) /
4303 (table size) is >= REHASH_THRESHOLD.
4304
4305 WEAK specifies the weakness of the table. If non-nil, it must be
4306 one of the symbols `key', `value', `key-or-value', or `key-and-value'. */
4307
4308 Lisp_Object
4309 make_hash_table (test, size, rehash_size, rehash_threshold, weak,
4310 user_test, user_hash)
4311 Lisp_Object test, size, rehash_size, rehash_threshold, weak;
4312 Lisp_Object user_test, user_hash;
4313 {
4314 struct Lisp_Hash_Table *h;
4315 Lisp_Object table;
4316 int index_size, i, sz;
4317
4318 /* Preconditions. */
4319 xassert (SYMBOLP (test));
4320 xassert (INTEGERP (size) && XINT (size) >= 0);
4321 xassert ((INTEGERP (rehash_size) && XINT (rehash_size) > 0)
4322 || (FLOATP (rehash_size) && XFLOATINT (rehash_size) > 1.0));
4323 xassert (FLOATP (rehash_threshold)
4324 && XFLOATINT (rehash_threshold) > 0
4325 && XFLOATINT (rehash_threshold) <= 1.0);
4326
4327 if (XFASTINT (size) == 0)
4328 size = make_number (1);
4329
4330 /* Allocate a table and initialize it. */
4331 h = allocate_hash_table ();
4332
4333 /* Initialize hash table slots. */
4334 sz = XFASTINT (size);
4335
4336 h->test = test;
4337 if (EQ (test, Qeql))
4338 {
4339 h->cmpfn = cmpfn_eql;
4340 h->hashfn = hashfn_eql;
4341 }
4342 else if (EQ (test, Qeq))
4343 {
4344 h->cmpfn = NULL;
4345 h->hashfn = hashfn_eq;
4346 }
4347 else if (EQ (test, Qequal))
4348 {
4349 h->cmpfn = cmpfn_equal;
4350 h->hashfn = hashfn_equal;
4351 }
4352 else
4353 {
4354 h->user_cmp_function = user_test;
4355 h->user_hash_function = user_hash;
4356 h->cmpfn = cmpfn_user_defined;
4357 h->hashfn = hashfn_user_defined;
4358 }
4359
4360 h->weak = weak;
4361 h->rehash_threshold = rehash_threshold;
4362 h->rehash_size = rehash_size;
4363 h->count = make_number (0);
4364 h->key_and_value = Fmake_vector (make_number (2 * sz), Qnil);
4365 h->hash = Fmake_vector (size, Qnil);
4366 h->next = Fmake_vector (size, Qnil);
4367 /* Cast to int here avoids losing with gcc 2.95 on Tru64/Alpha... */
4368 index_size = next_almost_prime ((int) (sz / XFLOATINT (rehash_threshold)));
4369 h->index = Fmake_vector (make_number (index_size), Qnil);
4370
4371 /* Set up the free list. */
4372 for (i = 0; i < sz - 1; ++i)
4373 HASH_NEXT (h, i) = make_number (i + 1);
4374 h->next_free = make_number (0);
4375
4376 XSET_HASH_TABLE (table, h);
4377 xassert (HASH_TABLE_P (table));
4378 xassert (XHASH_TABLE (table) == h);
4379
4380 /* Maybe add this hash table to the list of all weak hash tables. */
4381 if (NILP (h->weak))
4382 h->next_weak = Qnil;
4383 else
4384 {
4385 h->next_weak = Vweak_hash_tables;
4386 Vweak_hash_tables = table;
4387 }
4388
4389 return table;
4390 }
4391
4392
4393 /* Return a copy of hash table H1. Keys and values are not copied,
4394 only the table itself is. */
4395
4396 Lisp_Object
4397 copy_hash_table (h1)
4398 struct Lisp_Hash_Table *h1;
4399 {
4400 Lisp_Object table;
4401 struct Lisp_Hash_Table *h2;
4402 struct Lisp_Vector *next;
4403
4404 h2 = allocate_hash_table ();
4405 next = h2->vec_next;
4406 bcopy (h1, h2, sizeof *h2);
4407 h2->vec_next = next;
4408 h2->key_and_value = Fcopy_sequence (h1->key_and_value);
4409 h2->hash = Fcopy_sequence (h1->hash);
4410 h2->next = Fcopy_sequence (h1->next);
4411 h2->index = Fcopy_sequence (h1->index);
4412 XSET_HASH_TABLE (table, h2);
4413
4414 /* Maybe add this hash table to the list of all weak hash tables. */
4415 if (!NILP (h2->weak))
4416 {
4417 h2->next_weak = Vweak_hash_tables;
4418 Vweak_hash_tables = table;
4419 }
4420
4421 return table;
4422 }
4423
4424
4425 /* Resize hash table H if it's too full. If H cannot be resized
4426 because it's already too large, throw an error. */
4427
4428 static INLINE void
4429 maybe_resize_hash_table (h)
4430 struct Lisp_Hash_Table *h;
4431 {
4432 if (NILP (h->next_free))
4433 {
4434 int old_size = HASH_TABLE_SIZE (h);
4435 int i, new_size, index_size;
4436
4437 if (INTEGERP (h->rehash_size))
4438 new_size = old_size + XFASTINT (h->rehash_size);
4439 else
4440 new_size = old_size * XFLOATINT (h->rehash_size);
4441 new_size = max (old_size + 1, new_size);
4442 index_size = next_almost_prime ((int)
4443 (new_size
4444 / XFLOATINT (h->rehash_threshold)));
4445 if (max (index_size, 2 * new_size) & ~VALMASK)
4446 error ("Hash table too large to resize");
4447
4448 h->key_and_value = larger_vector (h->key_and_value, 2 * new_size, Qnil);
4449 h->next = larger_vector (h->next, new_size, Qnil);
4450 h->hash = larger_vector (h->hash, new_size, Qnil);
4451 h->index = Fmake_vector (make_number (index_size), Qnil);
4452
4453 /* Update the free list. Do it so that new entries are added at
4454 the end of the free list. This makes some operations like
4455 maphash faster. */
4456 for (i = old_size; i < new_size - 1; ++i)
4457 HASH_NEXT (h, i) = make_number (i + 1);
4458
4459 if (!NILP (h->next_free))
4460 {
4461 Lisp_Object last, next;
4462
4463 last = h->next_free;
4464 while (next = HASH_NEXT (h, XFASTINT (last)),
4465 !NILP (next))
4466 last = next;
4467
4468 HASH_NEXT (h, XFASTINT (last)) = make_number (old_size);
4469 }
4470 else
4471 XSETFASTINT (h->next_free, old_size);
4472
4473 /* Rehash. */
4474 for (i = 0; i < old_size; ++i)
4475 if (!NILP (HASH_HASH (h, i)))
4476 {
4477 unsigned hash_code = XUINT (HASH_HASH (h, i));
4478 int start_of_bucket = hash_code % XVECTOR (h->index)->size;
4479 HASH_NEXT (h, i) = HASH_INDEX (h, start_of_bucket);
4480 HASH_INDEX (h, start_of_bucket) = make_number (i);
4481 }
4482 }
4483 }
4484
4485
4486 /* Lookup KEY in hash table H. If HASH is non-null, return in *HASH
4487 the hash code of KEY. Value is the index of the entry in H
4488 matching KEY, or -1 if not found. */
4489
4490 int
4491 hash_lookup (h, key, hash)
4492 struct Lisp_Hash_Table *h;
4493 Lisp_Object key;
4494 unsigned *hash;
4495 {
4496 unsigned hash_code;
4497 int start_of_bucket;
4498 Lisp_Object idx;
4499
4500 hash_code = h->hashfn (h, key);
4501 if (hash)
4502 *hash = hash_code;
4503
4504 start_of_bucket = hash_code % XVECTOR (h->index)->size;
4505 idx = HASH_INDEX (h, start_of_bucket);
4506
4507 /* We need not gcpro idx since it's either an integer or nil. */
4508 while (!NILP (idx))
4509 {
4510 int i = XFASTINT (idx);
4511 if (EQ (key, HASH_KEY (h, i))
4512 || (h->cmpfn
4513 && h->cmpfn (h, key, hash_code,
4514 HASH_KEY (h, i), XUINT (HASH_HASH (h, i)))))
4515 break;
4516 idx = HASH_NEXT (h, i);
4517 }
4518
4519 return NILP (idx) ? -1 : XFASTINT (idx);
4520 }
4521
4522
4523 /* Put an entry into hash table H that associates KEY with VALUE.
4524 HASH is a previously computed hash code of KEY.
4525 Value is the index of the entry in H matching KEY. */
4526
4527 int
4528 hash_put (h, key, value, hash)
4529 struct Lisp_Hash_Table *h;
4530 Lisp_Object key, value;
4531 unsigned hash;
4532 {
4533 int start_of_bucket, i;
4534
4535 xassert ((hash & ~VALMASK) == 0);
4536
4537 /* Increment count after resizing because resizing may fail. */
4538 maybe_resize_hash_table (h);
4539 h->count = make_number (XFASTINT (h->count) + 1);
4540
4541 /* Store key/value in the key_and_value vector. */
4542 i = XFASTINT (h->next_free);
4543 h->next_free = HASH_NEXT (h, i);
4544 HASH_KEY (h, i) = key;
4545 HASH_VALUE (h, i) = value;
4546
4547 /* Remember its hash code. */
4548 HASH_HASH (h, i) = make_number (hash);
4549
4550 /* Add new entry to its collision chain. */
4551 start_of_bucket = hash % XVECTOR (h->index)->size;
4552 HASH_NEXT (h, i) = HASH_INDEX (h, start_of_bucket);
4553 HASH_INDEX (h, start_of_bucket) = make_number (i);
4554 return i;
4555 }
4556
4557
4558 /* Remove the entry matching KEY from hash table H, if there is one. */
4559
4560 void
4561 hash_remove (h, key)
4562 struct Lisp_Hash_Table *h;
4563 Lisp_Object key;
4564 {
4565 unsigned hash_code;
4566 int start_of_bucket;
4567 Lisp_Object idx, prev;
4568
4569 hash_code = h->hashfn (h, key);
4570 start_of_bucket = hash_code % XVECTOR (h->index)->size;
4571 idx = HASH_INDEX (h, start_of_bucket);
4572 prev = Qnil;
4573
4574 /* We need not gcpro idx, prev since they're either integers or nil. */
4575 while (!NILP (idx))
4576 {
4577 int i = XFASTINT (idx);
4578
4579 if (EQ (key, HASH_KEY (h, i))
4580 || (h->cmpfn
4581 && h->cmpfn (h, key, hash_code,
4582 HASH_KEY (h, i), XUINT (HASH_HASH (h, i)))))
4583 {
4584 /* Take entry out of collision chain. */
4585 if (NILP (prev))
4586 HASH_INDEX (h, start_of_bucket) = HASH_NEXT (h, i);
4587 else
4588 HASH_NEXT (h, XFASTINT (prev)) = HASH_NEXT (h, i);
4589
4590 /* Clear slots in key_and_value and add the slots to
4591 the free list. */
4592 HASH_KEY (h, i) = HASH_VALUE (h, i) = HASH_HASH (h, i) = Qnil;
4593 HASH_NEXT (h, i) = h->next_free;
4594 h->next_free = make_number (i);
4595 h->count = make_number (XFASTINT (h->count) - 1);
4596 xassert (XINT (h->count) >= 0);
4597 break;
4598 }
4599 else
4600 {
4601 prev = idx;
4602 idx = HASH_NEXT (h, i);
4603 }
4604 }
4605 }
4606
4607
4608 /* Clear hash table H. */
4609
4610 void
4611 hash_clear (h)
4612 struct Lisp_Hash_Table *h;
4613 {
4614 if (XFASTINT (h->count) > 0)
4615 {
4616 int i, size = HASH_TABLE_SIZE (h);
4617
4618 for (i = 0; i < size; ++i)
4619 {
4620 HASH_NEXT (h, i) = i < size - 1 ? make_number (i + 1) : Qnil;
4621 HASH_KEY (h, i) = Qnil;
4622 HASH_VALUE (h, i) = Qnil;
4623 HASH_HASH (h, i) = Qnil;
4624 }
4625
4626 for (i = 0; i < XVECTOR (h->index)->size; ++i)
4627 XVECTOR (h->index)->contents[i] = Qnil;
4628
4629 h->next_free = make_number (0);
4630 h->count = make_number (0);
4631 }
4632 }
4633
4634
4635 \f
4636 /************************************************************************
4637 Weak Hash Tables
4638 ************************************************************************/
4639
4640 /* Sweep weak hash table H. REMOVE_ENTRIES_P non-zero means remove
4641 entries from the table that don't survive the current GC.
4642 REMOVE_ENTRIES_P zero means mark entries that are in use. Value is
4643 non-zero if anything was marked. */
4644
4645 static int
4646 sweep_weak_table (h, remove_entries_p)
4647 struct Lisp_Hash_Table *h;
4648 int remove_entries_p;
4649 {
4650 int bucket, n, marked;
4651
4652 n = XVECTOR (h->index)->size & ~ARRAY_MARK_FLAG;
4653 marked = 0;
4654
4655 for (bucket = 0; bucket < n; ++bucket)
4656 {
4657 Lisp_Object idx, next, prev;
4658
4659 /* Follow collision chain, removing entries that
4660 don't survive this garbage collection. */
4661 prev = Qnil;
4662 for (idx = HASH_INDEX (h, bucket); !GC_NILP (idx); idx = next)
4663 {
4664 int i = XFASTINT (idx);
4665 int key_known_to_survive_p = survives_gc_p (HASH_KEY (h, i));
4666 int value_known_to_survive_p = survives_gc_p (HASH_VALUE (h, i));
4667 int remove_p;
4668
4669 if (EQ (h->weak, Qkey))
4670 remove_p = !key_known_to_survive_p;
4671 else if (EQ (h->weak, Qvalue))
4672 remove_p = !value_known_to_survive_p;
4673 else if (EQ (h->weak, Qkey_or_value))
4674 remove_p = !(key_known_to_survive_p || value_known_to_survive_p);
4675 else if (EQ (h->weak, Qkey_and_value))
4676 remove_p = !(key_known_to_survive_p && value_known_to_survive_p);
4677 else
4678 abort ();
4679
4680 next = HASH_NEXT (h, i);
4681
4682 if (remove_entries_p)
4683 {
4684 if (remove_p)
4685 {
4686 /* Take out of collision chain. */
4687 if (GC_NILP (prev))
4688 HASH_INDEX (h, bucket) = next;
4689 else
4690 HASH_NEXT (h, XFASTINT (prev)) = next;
4691
4692 /* Add to free list. */
4693 HASH_NEXT (h, i) = h->next_free;
4694 h->next_free = idx;
4695
4696 /* Clear key, value, and hash. */
4697 HASH_KEY (h, i) = HASH_VALUE (h, i) = Qnil;
4698 HASH_HASH (h, i) = Qnil;
4699
4700 h->count = make_number (XFASTINT (h->count) - 1);
4701 }
4702 }
4703 else
4704 {
4705 if (!remove_p)
4706 {
4707 /* Make sure key and value survive. */
4708 if (!key_known_to_survive_p)
4709 {
4710 mark_object (&HASH_KEY (h, i));
4711 marked = 1;
4712 }
4713
4714 if (!value_known_to_survive_p)
4715 {
4716 mark_object (&HASH_VALUE (h, i));
4717 marked = 1;
4718 }
4719 }
4720 }
4721 }
4722 }
4723
4724 return marked;
4725 }
4726
4727 /* Remove elements from weak hash tables that don't survive the
4728 current garbage collection. Remove weak tables that don't survive
4729 from Vweak_hash_tables. Called from gc_sweep. */
4730
4731 void
4732 sweep_weak_hash_tables ()
4733 {
4734 Lisp_Object table, used, next;
4735 struct Lisp_Hash_Table *h;
4736 int marked;
4737
4738 /* Mark all keys and values that are in use. Keep on marking until
4739 there is no more change. This is necessary for cases like
4740 value-weak table A containing an entry X -> Y, where Y is used in a
4741 key-weak table B, Z -> Y. If B comes after A in the list of weak
4742 tables, X -> Y might be removed from A, although when looking at B
4743 one finds that it shouldn't. */
4744 do
4745 {
4746 marked = 0;
4747 for (table = Vweak_hash_tables; !GC_NILP (table); table = h->next_weak)
4748 {
4749 h = XHASH_TABLE (table);
4750 if (h->size & ARRAY_MARK_FLAG)
4751 marked |= sweep_weak_table (h, 0);
4752 }
4753 }
4754 while (marked);
4755
4756 /* Remove tables and entries that aren't used. */
4757 for (table = Vweak_hash_tables, used = Qnil; !GC_NILP (table); table = next)
4758 {
4759 h = XHASH_TABLE (table);
4760 next = h->next_weak;
4761
4762 if (h->size & ARRAY_MARK_FLAG)
4763 {
4764 /* TABLE is marked as used. Sweep its contents. */
4765 if (XFASTINT (h->count) > 0)
4766 sweep_weak_table (h, 1);
4767
4768 /* Add table to the list of used weak hash tables. */
4769 h->next_weak = used;
4770 used = table;
4771 }
4772 }
4773
4774 Vweak_hash_tables = used;
4775 }
4776
4777
4778 \f
4779 /***********************************************************************
4780 Hash Code Computation
4781 ***********************************************************************/
4782
4783 /* Maximum depth up to which to dive into Lisp structures. */
4784
4785 #define SXHASH_MAX_DEPTH 3
4786
4787 /* Maximum length up to which to take list and vector elements into
4788 account. */
4789
4790 #define SXHASH_MAX_LEN 7
4791
4792 /* Combine two integers X and Y for hashing. */
4793
4794 #define SXHASH_COMBINE(X, Y) \
4795 ((((unsigned)(X) << 4) + (((unsigned)(X) >> 24) & 0x0fffffff)) \
4796 + (unsigned)(Y))
4797
4798
4799 /* Return a hash for string PTR which has length LEN. The hash
4800 code returned is guaranteed to fit in a Lisp integer. */
4801
4802 static unsigned
4803 sxhash_string (ptr, len)
4804 unsigned char *ptr;
4805 int len;
4806 {
4807 unsigned char *p = ptr;
4808 unsigned char *end = p + len;
4809 unsigned char c;
4810 unsigned hash = 0;
4811
4812 while (p != end)
4813 {
4814 c = *p++;
4815 if (c >= 0140)
4816 c -= 40;
4817 hash = ((hash << 3) + (hash >> 28) + c);
4818 }
4819
4820 return hash & VALMASK;
4821 }
4822
4823
4824 /* Return a hash for list LIST. DEPTH is the current depth in the
4825 list. We don't recurse deeper than SXHASH_MAX_DEPTH in it. */
4826
4827 static unsigned
4828 sxhash_list (list, depth)
4829 Lisp_Object list;
4830 int depth;
4831 {
4832 unsigned hash = 0;
4833 int i;
4834
4835 if (depth < SXHASH_MAX_DEPTH)
4836 for (i = 0;
4837 CONSP (list) && i < SXHASH_MAX_LEN;
4838 list = XCDR (list), ++i)
4839 {
4840 unsigned hash2 = sxhash (XCAR (list), depth + 1);
4841 hash = SXHASH_COMBINE (hash, hash2);
4842 }
4843
4844 return hash;
4845 }
4846
4847
4848 /* Return a hash for vector VECTOR. DEPTH is the current depth in
4849 the Lisp structure. */
4850
4851 static unsigned
4852 sxhash_vector (vec, depth)
4853 Lisp_Object vec;
4854 int depth;
4855 {
4856 unsigned hash = XVECTOR (vec)->size;
4857 int i, n;
4858
4859 n = min (SXHASH_MAX_LEN, XVECTOR (vec)->size);
4860 for (i = 0; i < n; ++i)
4861 {
4862 unsigned hash2 = sxhash (XVECTOR (vec)->contents[i], depth + 1);
4863 hash = SXHASH_COMBINE (hash, hash2);
4864 }
4865
4866 return hash;
4867 }
4868
4869
4870 /* Return a hash for bool-vector VECTOR. */
4871
4872 static unsigned
4873 sxhash_bool_vector (vec)
4874 Lisp_Object vec;
4875 {
4876 unsigned hash = XBOOL_VECTOR (vec)->size;
4877 int i, n;
4878
4879 n = min (SXHASH_MAX_LEN, XBOOL_VECTOR (vec)->vector_size);
4880 for (i = 0; i < n; ++i)
4881 hash = SXHASH_COMBINE (hash, XBOOL_VECTOR (vec)->data[i]);
4882
4883 return hash;
4884 }
4885
4886
4887 /* Return a hash code for OBJ. DEPTH is the current depth in the Lisp
4888 structure. Value is an unsigned integer clipped to VALMASK. */
4889
4890 unsigned
4891 sxhash (obj, depth)
4892 Lisp_Object obj;
4893 int depth;
4894 {
4895 unsigned hash;
4896
4897 if (depth > SXHASH_MAX_DEPTH)
4898 return 0;
4899
4900 switch (XTYPE (obj))
4901 {
4902 case Lisp_Int:
4903 hash = XUINT (obj);
4904 break;
4905
4906 case Lisp_Symbol:
4907 hash = sxhash_string (SDATA (SYMBOL_NAME (obj)),
4908 SCHARS (SYMBOL_NAME (obj)));
4909 break;
4910
4911 case Lisp_Misc:
4912 hash = XUINT (obj);
4913 break;
4914
4915 case Lisp_String:
4916 hash = sxhash_string (SDATA (obj), SCHARS (obj));
4917 break;
4918
4919 /* This can be everything from a vector to an overlay. */
4920 case Lisp_Vectorlike:
4921 if (VECTORP (obj))
4922 /* According to the CL HyperSpec, two arrays are equal only if
4923 they are `eq', except for strings and bit-vectors. In
4924 Emacs, this works differently. We have to compare element
4925 by element. */
4926 hash = sxhash_vector (obj, depth);
4927 else if (BOOL_VECTOR_P (obj))
4928 hash = sxhash_bool_vector (obj);
4929 else
4930 /* Others are `equal' if they are `eq', so let's take their
4931 address as hash. */
4932 hash = XUINT (obj);
4933 break;
4934
4935 case Lisp_Cons:
4936 hash = sxhash_list (obj, depth);
4937 break;
4938
4939 case Lisp_Float:
4940 {
4941 unsigned char *p = (unsigned char *) &XFLOAT_DATA (obj);
4942 unsigned char *e = p + sizeof XFLOAT_DATA (obj);
4943 for (hash = 0; p < e; ++p)
4944 hash = SXHASH_COMBINE (hash, *p);
4945 break;
4946 }
4947
4948 default:
4949 abort ();
4950 }
4951
4952 return hash & VALMASK;
4953 }
4954
4955
4956 \f
4957 /***********************************************************************
4958 Lisp Interface
4959 ***********************************************************************/
4960
4961
4962 DEFUN ("sxhash", Fsxhash, Ssxhash, 1, 1, 0,
4963 doc: /* Compute a hash code for OBJ and return it as integer. */)
4964 (obj)
4965 Lisp_Object obj;
4966 {
4967 unsigned hash = sxhash (obj, 0);;
4968 return make_number (hash);
4969 }
4970
4971
4972 DEFUN ("make-hash-table", Fmake_hash_table, Smake_hash_table, 0, MANY, 0,
4973 doc: /* Create and return a new hash table.
4974
4975 Arguments are specified as keyword/argument pairs. The following
4976 arguments are defined:
4977
4978 :test TEST -- TEST must be a symbol that specifies how to compare
4979 keys. Default is `eql'. Predefined are the tests `eq', `eql', and
4980 `equal'. User-supplied test and hash functions can be specified via
4981 `define-hash-table-test'.
4982
4983 :size SIZE -- A hint as to how many elements will be put in the table.
4984 Default is 65.
4985
4986 :rehash-size REHASH-SIZE - Indicates how to expand the table when it
4987 fills up. If REHASH-SIZE is an integer, add that many space. If it
4988 is a float, it must be > 1.0, and the new size is computed by
4989 multiplying the old size with that factor. Default is 1.5.
4990
4991 :rehash-threshold THRESHOLD -- THRESHOLD must a float > 0, and <= 1.0.
4992 Resize the hash table when ratio of the number of entries in the
4993 table. Default is 0.8.
4994
4995 :weakness WEAK -- WEAK must be one of nil, t, `key', `value',
4996 `key-or-value', or `key-and-value'. If WEAK is not nil, the table
4997 returned is a weak table. Key/value pairs are removed from a weak
4998 hash table when there are no non-weak references pointing to their
4999 key, value, one of key or value, or both key and value, depending on
5000 WEAK. WEAK t is equivalent to `key-and-value'. Default value of WEAK
5001 is nil.
5002
5003 usage: (make-hash-table &rest KEYWORD-ARGS) */)
5004 (nargs, args)
5005 int nargs;
5006 Lisp_Object *args;
5007 {
5008 Lisp_Object test, size, rehash_size, rehash_threshold, weak;
5009 Lisp_Object user_test, user_hash;
5010 char *used;
5011 int i;
5012
5013 /* The vector `used' is used to keep track of arguments that
5014 have been consumed. */
5015 used = (char *) alloca (nargs * sizeof *used);
5016 bzero (used, nargs * sizeof *used);
5017
5018 /* See if there's a `:test TEST' among the arguments. */
5019 i = get_key_arg (QCtest, nargs, args, used);
5020 test = i < 0 ? Qeql : args[i];
5021 if (!EQ (test, Qeq) && !EQ (test, Qeql) && !EQ (test, Qequal))
5022 {
5023 /* See if it is a user-defined test. */
5024 Lisp_Object prop;
5025
5026 prop = Fget (test, Qhash_table_test);
5027 if (!CONSP (prop) || !CONSP (XCDR (prop)))
5028 Fsignal (Qerror, list2 (build_string ("Invalid hash table test"),
5029 test));
5030 user_test = XCAR (prop);
5031 user_hash = XCAR (XCDR (prop));
5032 }
5033 else
5034 user_test = user_hash = Qnil;
5035
5036 /* See if there's a `:size SIZE' argument. */
5037 i = get_key_arg (QCsize, nargs, args, used);
5038 size = i < 0 ? Qnil : args[i];
5039 if (NILP (size))
5040 size = make_number (DEFAULT_HASH_SIZE);
5041 else if (!INTEGERP (size) || XINT (size) < 0)
5042 Fsignal (Qerror,
5043 list2 (build_string ("Invalid hash table size"),
5044 size));
5045
5046 /* Look for `:rehash-size SIZE'. */
5047 i = get_key_arg (QCrehash_size, nargs, args, used);
5048 rehash_size = i < 0 ? make_float (DEFAULT_REHASH_SIZE) : args[i];
5049 if (!NUMBERP (rehash_size)
5050 || (INTEGERP (rehash_size) && XINT (rehash_size) <= 0)
5051 || XFLOATINT (rehash_size) <= 1.0)
5052 Fsignal (Qerror,
5053 list2 (build_string ("Invalid hash table rehash size"),
5054 rehash_size));
5055
5056 /* Look for `:rehash-threshold THRESHOLD'. */
5057 i = get_key_arg (QCrehash_threshold, nargs, args, used);
5058 rehash_threshold = i < 0 ? make_float (DEFAULT_REHASH_THRESHOLD) : args[i];
5059 if (!FLOATP (rehash_threshold)
5060 || XFLOATINT (rehash_threshold) <= 0.0
5061 || XFLOATINT (rehash_threshold) > 1.0)
5062 Fsignal (Qerror,
5063 list2 (build_string ("Invalid hash table rehash threshold"),
5064 rehash_threshold));
5065
5066 /* Look for `:weakness WEAK'. */
5067 i = get_key_arg (QCweakness, nargs, args, used);
5068 weak = i < 0 ? Qnil : args[i];
5069 if (EQ (weak, Qt))
5070 weak = Qkey_and_value;
5071 if (!NILP (weak)
5072 && !EQ (weak, Qkey)
5073 && !EQ (weak, Qvalue)
5074 && !EQ (weak, Qkey_or_value)
5075 && !EQ (weak, Qkey_and_value))
5076 Fsignal (Qerror, list2 (build_string ("Invalid hash table weakness"),
5077 weak));
5078
5079 /* Now, all args should have been used up, or there's a problem. */
5080 for (i = 0; i < nargs; ++i)
5081 if (!used[i])
5082 Fsignal (Qerror,
5083 list2 (build_string ("Invalid argument list"), args[i]));
5084
5085 return make_hash_table (test, size, rehash_size, rehash_threshold, weak,
5086 user_test, user_hash);
5087 }
5088
5089
5090 DEFUN ("copy-hash-table", Fcopy_hash_table, Scopy_hash_table, 1, 1, 0,
5091 doc: /* Return a copy of hash table TABLE. */)
5092 (table)
5093 Lisp_Object table;
5094 {
5095 return copy_hash_table (check_hash_table (table));
5096 }
5097
5098
5099 DEFUN ("hash-table-count", Fhash_table_count, Shash_table_count, 1, 1, 0,
5100 doc: /* Return the number of elements in TABLE. */)
5101 (table)
5102 Lisp_Object table;
5103 {
5104 return check_hash_table (table)->count;
5105 }
5106
5107
5108 DEFUN ("hash-table-rehash-size", Fhash_table_rehash_size,
5109 Shash_table_rehash_size, 1, 1, 0,
5110 doc: /* Return the current rehash size of TABLE. */)
5111 (table)
5112 Lisp_Object table;
5113 {
5114 return check_hash_table (table)->rehash_size;
5115 }
5116
5117
5118 DEFUN ("hash-table-rehash-threshold", Fhash_table_rehash_threshold,
5119 Shash_table_rehash_threshold, 1, 1, 0,
5120 doc: /* Return the current rehash threshold of TABLE. */)
5121 (table)
5122 Lisp_Object table;
5123 {
5124 return check_hash_table (table)->rehash_threshold;
5125 }
5126
5127
5128 DEFUN ("hash-table-size", Fhash_table_size, Shash_table_size, 1, 1, 0,
5129 doc: /* Return the size of TABLE.
5130 The size can be used as an argument to `make-hash-table' to create
5131 a hash table than can hold as many elements of TABLE holds
5132 without need for resizing. */)
5133 (table)
5134 Lisp_Object table;
5135 {
5136 struct Lisp_Hash_Table *h = check_hash_table (table);
5137 return make_number (HASH_TABLE_SIZE (h));
5138 }
5139
5140
5141 DEFUN ("hash-table-test", Fhash_table_test, Shash_table_test, 1, 1, 0,
5142 doc: /* Return the test TABLE uses. */)
5143 (table)
5144 Lisp_Object table;
5145 {
5146 return check_hash_table (table)->test;
5147 }
5148
5149
5150 DEFUN ("hash-table-weakness", Fhash_table_weakness, Shash_table_weakness,
5151 1, 1, 0,
5152 doc: /* Return the weakness of TABLE. */)
5153 (table)
5154 Lisp_Object table;
5155 {
5156 return check_hash_table (table)->weak;
5157 }
5158
5159
5160 DEFUN ("hash-table-p", Fhash_table_p, Shash_table_p, 1, 1, 0,
5161 doc: /* Return t if OBJ is a Lisp hash table object. */)
5162 (obj)
5163 Lisp_Object obj;
5164 {
5165 return HASH_TABLE_P (obj) ? Qt : Qnil;
5166 }
5167
5168
5169 DEFUN ("clrhash", Fclrhash, Sclrhash, 1, 1, 0,
5170 doc: /* Clear hash table TABLE. */)
5171 (table)
5172 Lisp_Object table;
5173 {
5174 hash_clear (check_hash_table (table));
5175 return Qnil;
5176 }
5177
5178
5179 DEFUN ("gethash", Fgethash, Sgethash, 2, 3, 0,
5180 doc: /* Look up KEY in TABLE and return its associated value.
5181 If KEY is not found, return DFLT which defaults to nil. */)
5182 (key, table, dflt)
5183 Lisp_Object key, table, dflt;
5184 {
5185 struct Lisp_Hash_Table *h = check_hash_table (table);
5186 int i = hash_lookup (h, key, NULL);
5187 return i >= 0 ? HASH_VALUE (h, i) : dflt;
5188 }
5189
5190
5191 DEFUN ("puthash", Fputhash, Sputhash, 3, 3, 0,
5192 doc: /* Associate KEY with VALUE in hash table TABLE.
5193 If KEY is already present in table, replace its current value with
5194 VALUE. */)
5195 (key, value, table)
5196 Lisp_Object key, value, table;
5197 {
5198 struct Lisp_Hash_Table *h = check_hash_table (table);
5199 int i;
5200 unsigned hash;
5201
5202 i = hash_lookup (h, key, &hash);
5203 if (i >= 0)
5204 HASH_VALUE (h, i) = value;
5205 else
5206 hash_put (h, key, value, hash);
5207
5208 return value;
5209 }
5210
5211
5212 DEFUN ("remhash", Fremhash, Sremhash, 2, 2, 0,
5213 doc: /* Remove KEY from TABLE. */)
5214 (key, table)
5215 Lisp_Object key, table;
5216 {
5217 struct Lisp_Hash_Table *h = check_hash_table (table);
5218 hash_remove (h, key);
5219 return Qnil;
5220 }
5221
5222
5223 DEFUN ("maphash", Fmaphash, Smaphash, 2, 2, 0,
5224 doc: /* Call FUNCTION for all entries in hash table TABLE.
5225 FUNCTION is called with 2 arguments KEY and VALUE. */)
5226 (function, table)
5227 Lisp_Object function, table;
5228 {
5229 struct Lisp_Hash_Table *h = check_hash_table (table);
5230 Lisp_Object args[3];
5231 int i;
5232
5233 for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
5234 if (!NILP (HASH_HASH (h, i)))
5235 {
5236 args[0] = function;
5237 args[1] = HASH_KEY (h, i);
5238 args[2] = HASH_VALUE (h, i);
5239 Ffuncall (3, args);
5240 }
5241
5242 return Qnil;
5243 }
5244
5245
5246 DEFUN ("define-hash-table-test", Fdefine_hash_table_test,
5247 Sdefine_hash_table_test, 3, 3, 0,
5248 doc: /* Define a new hash table test with name NAME, a symbol.
5249
5250 In hash tables created with NAME specified as test, use TEST to
5251 compare keys, and HASH for computing hash codes of keys.
5252
5253 TEST must be a function taking two arguments and returning non-nil if
5254 both arguments are the same. HASH must be a function taking one
5255 argument and return an integer that is the hash code of the argument.
5256 Hash code computation should use the whole value range of integers,
5257 including negative integers. */)
5258 (name, test, hash)
5259 Lisp_Object name, test, hash;
5260 {
5261 return Fput (name, Qhash_table_test, list2 (test, hash));
5262 }
5263
5264
5265 \f
5266 /************************************************************************
5267 MD5
5268 ************************************************************************/
5269
5270 #include "md5.h"
5271 #include "coding.h"
5272
5273 DEFUN ("md5", Fmd5, Smd5, 1, 5, 0,
5274 doc: /* Return MD5 message digest of OBJECT, a buffer or string.
5275
5276 A message digest is a cryptographic checksum of a document, and the
5277 algorithm to calculate it is defined in RFC 1321.
5278
5279 The two optional arguments START and END are character positions
5280 specifying for which part of OBJECT the message digest should be
5281 computed. If nil or omitted, the digest is computed for the whole
5282 OBJECT.
5283
5284 The MD5 message digest is computed from the result of encoding the
5285 text in a coding system, not directly from the internal Emacs form of
5286 the text. The optional fourth argument CODING-SYSTEM specifies which
5287 coding system to encode the text with. It should be the same coding
5288 system that you used or will use when actually writing the text into a
5289 file.
5290
5291 If CODING-SYSTEM is nil or omitted, the default depends on OBJECT. If
5292 OBJECT is a buffer, the default for CODING-SYSTEM is whatever coding
5293 system would be chosen by default for writing this text into a file.
5294
5295 If OBJECT is a string, the most preferred coding system (see the
5296 command `prefer-coding-system') is used.
5297
5298 If NOERROR is non-nil, silently assume the `raw-text' coding if the
5299 guesswork fails. Normally, an error is signaled in such case. */)
5300 (object, start, end, coding_system, noerror)
5301 Lisp_Object object, start, end, coding_system, noerror;
5302 {
5303 unsigned char digest[16];
5304 unsigned char value[33];
5305 int i;
5306 int size;
5307 int size_byte = 0;
5308 int start_char = 0, end_char = 0;
5309 int start_byte = 0, end_byte = 0;
5310 register int b, e;
5311 register struct buffer *bp;
5312 int temp;
5313
5314 if (STRINGP (object))
5315 {
5316 if (NILP (coding_system))
5317 {
5318 /* Decide the coding-system to encode the data with. */
5319
5320 if (STRING_MULTIBYTE (object))
5321 /* use default, we can't guess correct value */
5322 coding_system = SYMBOL_VALUE (XCAR (Vcoding_category_list));
5323 else
5324 coding_system = Qraw_text;
5325 }
5326
5327 if (NILP (Fcoding_system_p (coding_system)))
5328 {
5329 /* Invalid coding system. */
5330
5331 if (!NILP (noerror))
5332 coding_system = Qraw_text;
5333 else
5334 while (1)
5335 Fsignal (Qcoding_system_error, Fcons (coding_system, Qnil));
5336 }
5337
5338 if (STRING_MULTIBYTE (object))
5339 object = code_convert_string1 (object, coding_system, Qnil, 1);
5340
5341 size = SCHARS (object);
5342 size_byte = SBYTES (object);
5343
5344 if (!NILP (start))
5345 {
5346 CHECK_NUMBER (start);
5347
5348 start_char = XINT (start);
5349
5350 if (start_char < 0)
5351 start_char += size;
5352
5353 start_byte = string_char_to_byte (object, start_char);
5354 }
5355
5356 if (NILP (end))
5357 {
5358 end_char = size;
5359 end_byte = size_byte;
5360 }
5361 else
5362 {
5363 CHECK_NUMBER (end);
5364
5365 end_char = XINT (end);
5366
5367 if (end_char < 0)
5368 end_char += size;
5369
5370 end_byte = string_char_to_byte (object, end_char);
5371 }
5372
5373 if (!(0 <= start_char && start_char <= end_char && end_char <= size))
5374 args_out_of_range_3 (object, make_number (start_char),
5375 make_number (end_char));
5376 }
5377 else
5378 {
5379 CHECK_BUFFER (object);
5380
5381 bp = XBUFFER (object);
5382
5383 if (NILP (start))
5384 b = BUF_BEGV (bp);
5385 else
5386 {
5387 CHECK_NUMBER_COERCE_MARKER (start);
5388 b = XINT (start);
5389 }
5390
5391 if (NILP (end))
5392 e = BUF_ZV (bp);
5393 else
5394 {
5395 CHECK_NUMBER_COERCE_MARKER (end);
5396 e = XINT (end);
5397 }
5398
5399 if (b > e)
5400 temp = b, b = e, e = temp;
5401
5402 if (!(BUF_BEGV (bp) <= b && e <= BUF_ZV (bp)))
5403 args_out_of_range (start, end);
5404
5405 if (NILP (coding_system))
5406 {
5407 /* Decide the coding-system to encode the data with.
5408 See fileio.c:Fwrite-region */
5409
5410 if (!NILP (Vcoding_system_for_write))
5411 coding_system = Vcoding_system_for_write;
5412 else
5413 {
5414 int force_raw_text = 0;
5415
5416 coding_system = XBUFFER (object)->buffer_file_coding_system;
5417 if (NILP (coding_system)
5418 || NILP (Flocal_variable_p (Qbuffer_file_coding_system, Qnil)))
5419 {
5420 coding_system = Qnil;
5421 if (NILP (current_buffer->enable_multibyte_characters))
5422 force_raw_text = 1;
5423 }
5424
5425 if (NILP (coding_system) && !NILP (Fbuffer_file_name(object)))
5426 {
5427 /* Check file-coding-system-alist. */
5428 Lisp_Object args[4], val;
5429
5430 args[0] = Qwrite_region; args[1] = start; args[2] = end;
5431 args[3] = Fbuffer_file_name(object);
5432 val = Ffind_operation_coding_system (4, args);
5433 if (CONSP (val) && !NILP (XCDR (val)))
5434 coding_system = XCDR (val);
5435 }
5436
5437 if (NILP (coding_system)
5438 && !NILP (XBUFFER (object)->buffer_file_coding_system))
5439 {
5440 /* If we still have not decided a coding system, use the
5441 default value of buffer-file-coding-system. */
5442 coding_system = XBUFFER (object)->buffer_file_coding_system;
5443 }
5444
5445 if (!force_raw_text
5446 && !NILP (Ffboundp (Vselect_safe_coding_system_function)))
5447 /* Confirm that VAL can surely encode the current region. */
5448 coding_system = call4 (Vselect_safe_coding_system_function,
5449 make_number (b), make_number (e),
5450 coding_system, Qnil);
5451
5452 if (force_raw_text)
5453 coding_system = Qraw_text;
5454 }
5455
5456 if (NILP (Fcoding_system_p (coding_system)))
5457 {
5458 /* Invalid coding system. */
5459
5460 if (!NILP (noerror))
5461 coding_system = Qraw_text;
5462 else
5463 while (1)
5464 Fsignal (Qcoding_system_error, Fcons (coding_system, Qnil));
5465 }
5466 }
5467
5468 object = make_buffer_string (b, e, 0);
5469
5470 if (STRING_MULTIBYTE (object))
5471 object = code_convert_string1 (object, coding_system, Qnil, 1);
5472 }
5473
5474 md5_buffer (SDATA (object) + start_byte,
5475 SBYTES (object) - (size_byte - end_byte),
5476 digest);
5477
5478 for (i = 0; i < 16; i++)
5479 sprintf (&value[2 * i], "%02x", digest[i]);
5480 value[32] = '\0';
5481
5482 return make_string (value, 32);
5483 }
5484
5485 \f
5486 void
5487 syms_of_fns ()
5488 {
5489 /* Hash table stuff. */
5490 Qhash_table_p = intern ("hash-table-p");
5491 staticpro (&Qhash_table_p);
5492 Qeq = intern ("eq");
5493 staticpro (&Qeq);
5494 Qeql = intern ("eql");
5495 staticpro (&Qeql);
5496 Qequal = intern ("equal");
5497 staticpro (&Qequal);
5498 QCtest = intern (":test");
5499 staticpro (&QCtest);
5500 QCsize = intern (":size");
5501 staticpro (&QCsize);
5502 QCrehash_size = intern (":rehash-size");
5503 staticpro (&QCrehash_size);
5504 QCrehash_threshold = intern (":rehash-threshold");
5505 staticpro (&QCrehash_threshold);
5506 QCweakness = intern (":weakness");
5507 staticpro (&QCweakness);
5508 Qkey = intern ("key");
5509 staticpro (&Qkey);
5510 Qvalue = intern ("value");
5511 staticpro (&Qvalue);
5512 Qhash_table_test = intern ("hash-table-test");
5513 staticpro (&Qhash_table_test);
5514 Qkey_or_value = intern ("key-or-value");
5515 staticpro (&Qkey_or_value);
5516 Qkey_and_value = intern ("key-and-value");
5517 staticpro (&Qkey_and_value);
5518
5519 defsubr (&Ssxhash);
5520 defsubr (&Smake_hash_table);
5521 defsubr (&Scopy_hash_table);
5522 defsubr (&Shash_table_count);
5523 defsubr (&Shash_table_rehash_size);
5524 defsubr (&Shash_table_rehash_threshold);
5525 defsubr (&Shash_table_size);
5526 defsubr (&Shash_table_test);
5527 defsubr (&Shash_table_weakness);
5528 defsubr (&Shash_table_p);
5529 defsubr (&Sclrhash);
5530 defsubr (&Sgethash);
5531 defsubr (&Sputhash);
5532 defsubr (&Sremhash);
5533 defsubr (&Smaphash);
5534 defsubr (&Sdefine_hash_table_test);
5535
5536 Qstring_lessp = intern ("string-lessp");
5537 staticpro (&Qstring_lessp);
5538 Qprovide = intern ("provide");
5539 staticpro (&Qprovide);
5540 Qrequire = intern ("require");
5541 staticpro (&Qrequire);
5542 Qyes_or_no_p_history = intern ("yes-or-no-p-history");
5543 staticpro (&Qyes_or_no_p_history);
5544 Qcursor_in_echo_area = intern ("cursor-in-echo-area");
5545 staticpro (&Qcursor_in_echo_area);
5546 Qwidget_type = intern ("widget-type");
5547 staticpro (&Qwidget_type);
5548
5549 staticpro (&string_char_byte_cache_string);
5550 string_char_byte_cache_string = Qnil;
5551
5552 require_nesting_list = Qnil;
5553 staticpro (&require_nesting_list);
5554
5555 Fset (Qyes_or_no_p_history, Qnil);
5556
5557 DEFVAR_LISP ("features", &Vfeatures,
5558 doc: /* A list of symbols which are the features of the executing emacs.
5559 Used by `featurep' and `require', and altered by `provide'. */);
5560 Vfeatures = Qnil;
5561 Qsubfeatures = intern ("subfeatures");
5562 staticpro (&Qsubfeatures);
5563
5564 #ifdef HAVE_LANGINFO_CODESET
5565 Qcodeset = intern ("codeset");
5566 staticpro (&Qcodeset);
5567 Qdays = intern ("days");
5568 staticpro (&Qdays);
5569 Qmonths = intern ("months");
5570 staticpro (&Qmonths);
5571 Qpaper = intern ("paper");
5572 staticpro (&Qpaper);
5573 #endif /* HAVE_LANGINFO_CODESET */
5574
5575 DEFVAR_BOOL ("use-dialog-box", &use_dialog_box,
5576 doc: /* *Non-nil means mouse commands use dialog boxes to ask questions.
5577 This applies to `y-or-n-p' and `yes-or-no-p' questions asked by commands
5578 invoked by mouse clicks and mouse menu items. */);
5579 use_dialog_box = 1;
5580
5581 defsubr (&Sidentity);
5582 defsubr (&Srandom);
5583 defsubr (&Slength);
5584 defsubr (&Ssafe_length);
5585 defsubr (&Sstring_bytes);
5586 defsubr (&Sstring_equal);
5587 defsubr (&Scompare_strings);
5588 defsubr (&Sstring_lessp);
5589 defsubr (&Sappend);
5590 defsubr (&Sconcat);
5591 defsubr (&Svconcat);
5592 defsubr (&Scopy_sequence);
5593 defsubr (&Sstring_make_multibyte);
5594 defsubr (&Sstring_make_unibyte);
5595 defsubr (&Sstring_as_multibyte);
5596 defsubr (&Sstring_as_unibyte);
5597 defsubr (&Sstring_to_multibyte);
5598 defsubr (&Scopy_alist);
5599 defsubr (&Ssubstring);
5600 defsubr (&Ssubstring_no_properties);
5601 defsubr (&Snthcdr);
5602 defsubr (&Snth);
5603 defsubr (&Selt);
5604 defsubr (&Smember);
5605 defsubr (&Smemq);
5606 defsubr (&Sassq);
5607 defsubr (&Sassoc);
5608 defsubr (&Srassq);
5609 defsubr (&Srassoc);
5610 defsubr (&Sdelq);
5611 defsubr (&Sdelete);
5612 defsubr (&Snreverse);
5613 defsubr (&Sreverse);
5614 defsubr (&Ssort);
5615 defsubr (&Splist_get);
5616 defsubr (&Sget);
5617 defsubr (&Splist_put);
5618 defsubr (&Sput);
5619 defsubr (&Slax_plist_get);
5620 defsubr (&Slax_plist_put);
5621 defsubr (&Sequal);
5622 defsubr (&Sfillarray);
5623 defsubr (&Schar_table_subtype);
5624 defsubr (&Schar_table_parent);
5625 defsubr (&Sset_char_table_parent);
5626 defsubr (&Schar_table_extra_slot);
5627 defsubr (&Sset_char_table_extra_slot);
5628 defsubr (&Schar_table_range);
5629 defsubr (&Sset_char_table_range);
5630 defsubr (&Sset_char_table_default);
5631 defsubr (&Soptimize_char_table);
5632 defsubr (&Smap_char_table);
5633 defsubr (&Snconc);
5634 defsubr (&Smapcar);
5635 defsubr (&Smapc);
5636 defsubr (&Smapconcat);
5637 defsubr (&Sy_or_n_p);
5638 defsubr (&Syes_or_no_p);
5639 defsubr (&Sload_average);
5640 defsubr (&Sfeaturep);
5641 defsubr (&Srequire);
5642 defsubr (&Sprovide);
5643 defsubr (&Splist_member);
5644 defsubr (&Swidget_put);
5645 defsubr (&Swidget_get);
5646 defsubr (&Swidget_apply);
5647 defsubr (&Sbase64_encode_region);
5648 defsubr (&Sbase64_decode_region);
5649 defsubr (&Sbase64_encode_string);
5650 defsubr (&Sbase64_decode_string);
5651 defsubr (&Smd5);
5652 defsubr (&Slanginfo);
5653 }
5654
5655
5656 void
5657 init_fns ()
5658 {
5659 Vweak_hash_tables = Qnil;
5660 }