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