Spelling fixes.
[bpt/emacs.git] / src / fns.c
CommitLineData
7b863bd5 1/* Random utility Lisp functions.
f0ddbf7b
GM
2
3Copyright (C) 1985-1987, 1993-1995, 1997-2013 Free Software Foundation, Inc.
7b863bd5
JB
4
5This file is part of GNU Emacs.
6
9ec0b715 7GNU Emacs is free software: you can redistribute it and/or modify
7b863bd5 8it under the terms of the GNU General Public License as published by
9ec0b715
GM
9the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
7b863bd5
JB
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
9ec0b715 18along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
7b863bd5 19
18160b98 20#include <config.h>
7b863bd5 21
dfcf069d 22#include <unistd.h>
58edb572 23#include <time.h>
dfcf069d 24
f03dc6ef
PE
25#include <intprops.h>
26
7b863bd5
JB
27#include "lisp.h"
28#include "commands.h"
38583a69 29#include "character.h"
dec002ca 30#include "coding.h"
7b863bd5 31#include "buffer.h"
f812877e 32#include "keyboard.h"
8feddab4 33#include "keymap.h"
ac811a55 34#include "intervals.h"
2d8e7e1f
RS
35#include "frame.h"
36#include "window.h"
91b11d9d 37#include "blockinput.h"
3df07ecd
YM
38#ifdef HAVE_MENUS
39#if defined (HAVE_X_WINDOWS)
dfcf069d
AS
40#include "xterm.h"
41#endif
2629aa37 42#endif /* HAVE_MENUS */
7b863bd5 43
955cbe7b
PE
44Lisp_Object Qstring_lessp;
45static Lisp_Object Qprovide, Qrequire;
46static Lisp_Object Qyes_or_no_p_history;
eb4ffa4e 47Lisp_Object Qcursor_in_echo_area;
955cbe7b
PE
48static Lisp_Object Qwidget_type;
49static Lisp_Object Qcodeset, Qdays, Qmonths, Qpaper;
7b863bd5 50
7f3f739f
LL
51static Lisp_Object Qmd5, Qsha1, Qsha224, Qsha256, Qsha384, Qsha512;
52
f75d7a91 53static bool internal_equal (Lisp_Object, Lisp_Object, int, bool);
e0f5cf5a 54\f
a7ca3326 55DEFUN ("identity", Fidentity, Sidentity, 1, 1, 0,
ddb67bdc 56 doc: /* Return the argument unchanged. */)
5842a27b 57 (Lisp_Object arg)
7b863bd5
JB
58{
59 return arg;
60}
61
62DEFUN ("random", Frandom, Srandom, 0, 1, 0,
e9d8ddc9 63 doc: /* Return a pseudo-random number.
48de8b12
CY
64All integers representable in Lisp, i.e. between `most-negative-fixnum'
65and `most-positive-fixnum', inclusive, are equally likely.
66
13d62fad
JB
67With positive integer LIMIT, return random number in interval [0,LIMIT).
68With argument t, set the random number seed from the current time and pid.
085d34c4
GM
69With a string argument, set the seed based on the string's contents.
70Other values of LIMIT are ignored.
71
72See Info node `(elisp)Random Numbers' for more details. */)
5842a27b 73 (Lisp_Object limit)
7b863bd5 74{
e2d6972a 75 EMACS_INT val;
7b863bd5 76
13d62fad 77 if (EQ (limit, Qt))
0e23ef9d
PE
78 init_random ();
79 else if (STRINGP (limit))
80 seed_random (SSDATA (limit), SBYTES (limit));
d8ed26bd 81
0e23ef9d 82 val = get_random ();
13d62fad 83 if (NATNUMP (limit) && XFASTINT (limit) != 0)
0e23ef9d
PE
84 val %= XFASTINT (limit);
85 return make_number (val);
7b863bd5
JB
86}
87\f
e6966cd6
PE
88/* Heuristic on how many iterations of a tight loop can be safely done
89 before it's time to do a QUIT. This must be a power of 2. */
90enum { QUIT_COUNT_HEURISTIC = 1 << 16 };
91
4a74c818 92/* Random data-structure functions. */
7b863bd5 93
84575e67
PE
94static void
95CHECK_LIST_END (Lisp_Object x, Lisp_Object y)
96{
97 CHECK_TYPE (NILP (x), Qlistp, y);
98}
99
a7ca3326 100DEFUN ("length", Flength, Slength, 1, 1, 0,
e9d8ddc9 101 doc: /* Return the length of vector, list or string SEQUENCE.
47cebab1 102A byte-code function object is also allowed.
f5965ada 103If the string contains multibyte characters, this is not necessarily
47cebab1 104the number of bytes in the string; it is the number of characters.
adf2c803 105To get the number of bytes, use `string-bytes'. */)
5842a27b 106 (register Lisp_Object sequence)
7b863bd5 107{
504f24f1 108 register Lisp_Object val;
7b863bd5 109
88fe8140 110 if (STRINGP (sequence))
d5db4077 111 XSETFASTINT (val, SCHARS (sequence));
88fe8140 112 else if (VECTORP (sequence))
7edbb0da 113 XSETFASTINT (val, ASIZE (sequence));
88fe8140 114 else if (CHAR_TABLE_P (sequence))
64a5094a 115 XSETFASTINT (val, MAX_CHAR);
88fe8140 116 else if (BOOL_VECTOR_P (sequence))
1c0a7493 117 XSETFASTINT (val, bool_vector_size (sequence));
876c194c
SM
118 else if (COMPILEDP (sequence))
119 XSETFASTINT (val, ASIZE (sequence) & PSEUDOVECTOR_SIZE_MASK);
88fe8140 120 else if (CONSP (sequence))
7b863bd5 121 {
00c604f2
PE
122 EMACS_INT i = 0;
123
124 do
7b863bd5 125 {
7843e09c 126 ++i;
e6966cd6 127 if ((i & (QUIT_COUNT_HEURISTIC - 1)) == 0)
00c604f2
PE
128 {
129 if (MOST_POSITIVE_FIXNUM < i)
130 error ("List too long");
131 QUIT;
132 }
7843e09c 133 sequence = XCDR (sequence);
7b863bd5 134 }
00c604f2 135 while (CONSP (sequence));
7b863bd5 136
89662fc3 137 CHECK_LIST_END (sequence, sequence);
f2be3671
GM
138
139 val = make_number (i);
7b863bd5 140 }
88fe8140 141 else if (NILP (sequence))
a2ad3e19 142 XSETFASTINT (val, 0);
7b863bd5 143 else
692ae65c 144 wrong_type_argument (Qsequencep, sequence);
89662fc3 145
a2ad3e19 146 return val;
7b863bd5
JB
147}
148
5a30fab8 149DEFUN ("safe-length", Fsafe_length, Ssafe_length, 1, 1, 0,
e9d8ddc9 150 doc: /* Return the length of a list, but avoid error or infinite loop.
47cebab1
GM
151This function never gets an error. If LIST is not really a list,
152it returns 0. If LIST is circular, it returns a finite value
adf2c803 153which is at least the number of distinct elements. */)
5842a27b 154 (Lisp_Object list)
5a30fab8 155{
e6966cd6
PE
156 Lisp_Object tail, halftail;
157 double hilen = 0;
158 uintmax_t lolen = 1;
159
160 if (! CONSP (list))
ff2bc410 161 return make_number (0);
5a30fab8
RS
162
163 /* halftail is used to detect circular lists. */
e6966cd6 164 for (tail = halftail = list; ; )
5a30fab8 165 {
e6966cd6
PE
166 tail = XCDR (tail);
167 if (! CONSP (tail))
cb3d1a0a 168 break;
e6966cd6
PE
169 if (EQ (tail, halftail))
170 break;
171 lolen++;
172 if ((lolen & 1) == 0)
173 {
174 halftail = XCDR (halftail);
175 if ((lolen & (QUIT_COUNT_HEURISTIC - 1)) == 0)
176 {
177 QUIT;
178 if (lolen == 0)
179 hilen += UINTMAX_MAX + 1.0;
180 }
181 }
5a30fab8
RS
182 }
183
e6966cd6
PE
184 /* If the length does not fit into a fixnum, return a float.
185 On all known practical machines this returns an upper bound on
186 the true length. */
187 return hilen ? make_float (hilen + lolen) : make_fixnum_or_float (lolen);
5a30fab8
RS
188}
189
91f78c99 190DEFUN ("string-bytes", Fstring_bytes, Sstring_bytes, 1, 1, 0,
e9d8ddc9 191 doc: /* Return the number of bytes in STRING.
eeb7eaa8 192If STRING is multibyte, this may be greater than the length of STRING. */)
5842a27b 193 (Lisp_Object string)
026f59ce 194{
b7826503 195 CHECK_STRING (string);
d5db4077 196 return make_number (SBYTES (string));
026f59ce
RS
197}
198
a7ca3326 199DEFUN ("string-equal", Fstring_equal, Sstring_equal, 2, 2, 0,
e9d8ddc9 200 doc: /* Return t if two strings have identical contents.
47cebab1 201Case is significant, but text properties are ignored.
adf2c803 202Symbols are also allowed; their print names are used instead. */)
5842a27b 203 (register Lisp_Object s1, Lisp_Object s2)
7b863bd5 204{
7650760e 205 if (SYMBOLP (s1))
c06583e1 206 s1 = SYMBOL_NAME (s1);
7650760e 207 if (SYMBOLP (s2))
c06583e1 208 s2 = SYMBOL_NAME (s2);
b7826503
PJ
209 CHECK_STRING (s1);
210 CHECK_STRING (s2);
7b863bd5 211
d5db4077
KR
212 if (SCHARS (s1) != SCHARS (s2)
213 || SBYTES (s1) != SBYTES (s2)
72af86bd 214 || memcmp (SDATA (s1), SDATA (s2), SBYTES (s1)))
7b863bd5
JB
215 return Qnil;
216 return Qt;
217}
218
a7ca3326 219DEFUN ("compare-strings", Fcompare_strings, Scompare_strings, 6, 7, 0,
b756c005 220 doc: /* Compare the contents of two strings, converting to multibyte if needed.
5bec25eb
CY
221The arguments START1, END1, START2, and END2, if non-nil, are
222positions specifying which parts of STR1 or STR2 to compare. In
223string STR1, compare the part between START1 (inclusive) and END1
224\(exclusive). If START1 is nil, it defaults to 0, the beginning of
225the string; if END1 is nil, it defaults to the length of the string.
226Likewise, in string STR2, compare the part between START2 and END2.
227
228The strings are compared by the numeric values of their characters.
229For instance, STR1 is "less than" STR2 if its first differing
230character has a smaller numeric value. If IGNORE-CASE is non-nil,
231characters are converted to lower-case before comparing them. Unibyte
232strings are converted to multibyte for comparison.
47cebab1
GM
233
234The value is t if the strings (or specified portions) match.
235If string STR1 is less, the value is a negative number N;
236 - 1 - N is the number of characters that match at the beginning.
237If string STR1 is greater, the value is a positive number N;
adf2c803 238 N - 1 is the number of characters that match at the beginning. */)
5842a27b 239 (Lisp_Object str1, Lisp_Object start1, Lisp_Object end1, Lisp_Object str2, Lisp_Object start2, Lisp_Object end2, Lisp_Object ignore_case)
0e1e9f8d 240{
d311d28c
PE
241 register ptrdiff_t end1_char, end2_char;
242 register ptrdiff_t i1, i1_byte, i2, i2_byte;
0e1e9f8d 243
b7826503
PJ
244 CHECK_STRING (str1);
245 CHECK_STRING (str2);
0e1e9f8d
RS
246 if (NILP (start1))
247 start1 = make_number (0);
248 if (NILP (start2))
249 start2 = make_number (0);
b7826503
PJ
250 CHECK_NATNUM (start1);
251 CHECK_NATNUM (start2);
0e1e9f8d 252 if (! NILP (end1))
b7826503 253 CHECK_NATNUM (end1);
0e1e9f8d 254 if (! NILP (end2))
b7826503 255 CHECK_NATNUM (end2);
0e1e9f8d 256
d5db4077 257 end1_char = SCHARS (str1);
0e1e9f8d
RS
258 if (! NILP (end1) && end1_char > XINT (end1))
259 end1_char = XINT (end1);
d311d28c
PE
260 if (end1_char < XINT (start1))
261 args_out_of_range (str1, start1);
0e1e9f8d 262
d5db4077 263 end2_char = SCHARS (str2);
0e1e9f8d
RS
264 if (! NILP (end2) && end2_char > XINT (end2))
265 end2_char = XINT (end2);
d311d28c
PE
266 if (end2_char < XINT (start2))
267 args_out_of_range (str2, start2);
268
269 i1 = XINT (start1);
270 i2 = XINT (start2);
271
272 i1_byte = string_char_to_byte (str1, i1);
273 i2_byte = string_char_to_byte (str2, i2);
0e1e9f8d
RS
274
275 while (i1 < end1_char && i2 < end2_char)
276 {
277 /* When we find a mismatch, we must compare the
278 characters, not just the bytes. */
279 int c1, c2;
280
281 if (STRING_MULTIBYTE (str1))
2efdd1b9 282 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c1, str1, i1, i1_byte);
0e1e9f8d
RS
283 else
284 {
d5db4077 285 c1 = SREF (str1, i1++);
4c0354d7 286 MAKE_CHAR_MULTIBYTE (c1);
0e1e9f8d
RS
287 }
288
289 if (STRING_MULTIBYTE (str2))
2efdd1b9 290 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c2, str2, i2, i2_byte);
0e1e9f8d
RS
291 else
292 {
d5db4077 293 c2 = SREF (str2, i2++);
4c0354d7 294 MAKE_CHAR_MULTIBYTE (c2);
0e1e9f8d
RS
295 }
296
297 if (c1 == c2)
298 continue;
299
300 if (! NILP (ignore_case))
301 {
302 Lisp_Object tem;
303
304 tem = Fupcase (make_number (c1));
305 c1 = XINT (tem);
306 tem = Fupcase (make_number (c2));
307 c2 = XINT (tem);
308 }
309
310 if (c1 == c2)
311 continue;
312
313 /* Note that I1 has already been incremented
314 past the character that we are comparing;
315 hence we don't add or subtract 1 here. */
316 if (c1 < c2)
60f8d735 317 return make_number (- i1 + XINT (start1));
0e1e9f8d 318 else
60f8d735 319 return make_number (i1 - XINT (start1));
0e1e9f8d
RS
320 }
321
322 if (i1 < end1_char)
323 return make_number (i1 - XINT (start1) + 1);
324 if (i2 < end2_char)
325 return make_number (- i1 + XINT (start1) - 1);
326
327 return Qt;
328}
329
a7ca3326 330DEFUN ("string-lessp", Fstring_lessp, Sstring_lessp, 2, 2, 0,
e9d8ddc9 331 doc: /* Return t if first arg string is less than second in lexicographic order.
47cebab1 332Case is significant.
adf2c803 333Symbols are also allowed; their print names are used instead. */)
5842a27b 334 (register Lisp_Object s1, Lisp_Object s2)
7b863bd5 335{
d311d28c
PE
336 register ptrdiff_t end;
337 register ptrdiff_t i1, i1_byte, i2, i2_byte;
7b863bd5 338
7650760e 339 if (SYMBOLP (s1))
c06583e1 340 s1 = SYMBOL_NAME (s1);
7650760e 341 if (SYMBOLP (s2))
c06583e1 342 s2 = SYMBOL_NAME (s2);
b7826503
PJ
343 CHECK_STRING (s1);
344 CHECK_STRING (s2);
7b863bd5 345
09ab3c3b
KH
346 i1 = i1_byte = i2 = i2_byte = 0;
347
d5db4077
KR
348 end = SCHARS (s1);
349 if (end > SCHARS (s2))
350 end = SCHARS (s2);
7b863bd5 351
09ab3c3b 352 while (i1 < end)
7b863bd5 353 {
09ab3c3b
KH
354 /* When we find a mismatch, we must compare the
355 characters, not just the bytes. */
356 int c1, c2;
357
2efdd1b9
KH
358 FETCH_STRING_CHAR_ADVANCE (c1, s1, i1, i1_byte);
359 FETCH_STRING_CHAR_ADVANCE (c2, s2, i2, i2_byte);
09ab3c3b
KH
360
361 if (c1 != c2)
362 return c1 < c2 ? Qt : Qnil;
7b863bd5 363 }
d5db4077 364 return i1 < SCHARS (s2) ? Qt : Qnil;
7b863bd5
JB
365}
366\f
f66c7cf8 367static Lisp_Object concat (ptrdiff_t nargs, Lisp_Object *args,
f75d7a91 368 enum Lisp_Type target_type, bool last_special);
7b863bd5
JB
369
370/* ARGSUSED */
371Lisp_Object
971de7fb 372concat2 (Lisp_Object s1, Lisp_Object s2)
7b863bd5 373{
7b863bd5
JB
374 Lisp_Object args[2];
375 args[0] = s1;
376 args[1] = s2;
377 return concat (2, args, Lisp_String, 0);
7b863bd5
JB
378}
379
d4af3687
RS
380/* ARGSUSED */
381Lisp_Object
971de7fb 382concat3 (Lisp_Object s1, Lisp_Object s2, Lisp_Object s3)
d4af3687 383{
d4af3687
RS
384 Lisp_Object args[3];
385 args[0] = s1;
386 args[1] = s2;
387 args[2] = s3;
388 return concat (3, args, Lisp_String, 0);
d4af3687
RS
389}
390
a7ca3326 391DEFUN ("append", Fappend, Sappend, 0, MANY, 0,
e9d8ddc9 392 doc: /* Concatenate all the arguments and make the result a list.
47cebab1
GM
393The result is a list whose elements are the elements of all the arguments.
394Each argument may be a list, vector or string.
4bf8e2a3
MB
395The last argument is not copied, just used as the tail of the new list.
396usage: (append &rest SEQUENCES) */)
f66c7cf8 397 (ptrdiff_t nargs, Lisp_Object *args)
7b863bd5
JB
398{
399 return concat (nargs, args, Lisp_Cons, 1);
400}
401
a7ca3326 402DEFUN ("concat", Fconcat, Sconcat, 0, MANY, 0,
e9d8ddc9 403 doc: /* Concatenate all the arguments and make the result a string.
47cebab1 404The result is a string whose elements are the elements of all the arguments.
4bf8e2a3
MB
405Each argument may be a string or a list or vector of characters (integers).
406usage: (concat &rest SEQUENCES) */)
f66c7cf8 407 (ptrdiff_t nargs, Lisp_Object *args)
7b863bd5
JB
408{
409 return concat (nargs, args, Lisp_String, 0);
410}
411
a7ca3326 412DEFUN ("vconcat", Fvconcat, Svconcat, 0, MANY, 0,
e9d8ddc9 413 doc: /* Concatenate all the arguments and make the result a vector.
47cebab1 414The result is a vector whose elements are the elements of all the arguments.
4bf8e2a3
MB
415Each argument may be a list, vector or string.
416usage: (vconcat &rest SEQUENCES) */)
f66c7cf8 417 (ptrdiff_t nargs, Lisp_Object *args)
7b863bd5 418{
3e7383eb 419 return concat (nargs, args, Lisp_Vectorlike, 0);
7b863bd5
JB
420}
421
3720677d 422
a7ca3326 423DEFUN ("copy-sequence", Fcopy_sequence, Scopy_sequence, 1, 1, 0,
7652ade0 424 doc: /* Return a copy of a list, vector, string or char-table.
47cebab1 425The elements of a list or vector are not copied; they are shared
adf2c803 426with the original. */)
5842a27b 427 (Lisp_Object arg)
7b863bd5 428{
265a9e55 429 if (NILP (arg)) return arg;
e03f7933
RS
430
431 if (CHAR_TABLE_P (arg))
432 {
38583a69 433 return copy_char_table (arg);
e03f7933
RS
434 }
435
436 if (BOOL_VECTOR_P (arg))
437 {
2cf00efc
PE
438 EMACS_INT nbits = bool_vector_size (arg);
439 ptrdiff_t nbytes = bool_vector_bytes (nbits);
440 Lisp_Object val = make_uninit_bool_vector (nbits);
441 memcpy (bool_vector_data (val), bool_vector_data (arg), nbytes);
e03f7933
RS
442 return val;
443 }
444
7650760e 445 if (!CONSP (arg) && !VECTORP (arg) && !STRINGP (arg))
89662fc3
KS
446 wrong_type_argument (Qsequencep, arg);
447
210272ce 448 return concat (1, &arg, XTYPE (arg), 0);
7b863bd5
JB
449}
450
2d6115c8
KH
451/* This structure holds information of an argument of `concat' that is
452 a string and has text properties to be copied. */
87f0532f 453struct textprop_rec
2d6115c8 454{
f66c7cf8 455 ptrdiff_t argnum; /* refer to ARGS (arguments of `concat') */
d311d28c
PE
456 ptrdiff_t from; /* refer to ARGS[argnum] (argument string) */
457 ptrdiff_t to; /* refer to VAL (the target string) */
2d6115c8
KH
458};
459
7b863bd5 460static Lisp_Object
f66c7cf8 461concat (ptrdiff_t nargs, Lisp_Object *args,
f75d7a91 462 enum Lisp_Type target_type, bool last_special)
7b863bd5
JB
463{
464 Lisp_Object val;
f75d7a91
PE
465 Lisp_Object tail;
466 Lisp_Object this;
d311d28c
PE
467 ptrdiff_t toindex;
468 ptrdiff_t toindex_byte = 0;
f75d7a91
PE
469 EMACS_INT result_len;
470 EMACS_INT result_len_byte;
f66c7cf8 471 ptrdiff_t argnum;
7b863bd5
JB
472 Lisp_Object last_tail;
473 Lisp_Object prev;
f75d7a91 474 bool some_multibyte;
2d6115c8 475 /* When we make a multibyte string, we can't copy text properties
66699ad3
PE
476 while concatenating each string because the length of resulting
477 string can't be decided until we finish the whole concatenation.
2d6115c8 478 So, we record strings that have text properties to be copied
66699ad3 479 here, and copy the text properties after the concatenation. */
093386ca 480 struct textprop_rec *textprops = NULL;
78edd3b7 481 /* Number of elements in textprops. */
f66c7cf8 482 ptrdiff_t num_textprops = 0;
2ec7f67a 483 USE_SAFE_ALLOCA;
7b863bd5 484
093386ca
GM
485 tail = Qnil;
486
7b863bd5
JB
487 /* In append, the last arg isn't treated like the others */
488 if (last_special && nargs > 0)
489 {
490 nargs--;
491 last_tail = args[nargs];
492 }
493 else
494 last_tail = Qnil;
495
89662fc3 496 /* Check each argument. */
7b863bd5
JB
497 for (argnum = 0; argnum < nargs; argnum++)
498 {
499 this = args[argnum];
7650760e 500 if (!(CONSP (this) || NILP (this) || VECTORP (this) || STRINGP (this)
876c194c 501 || COMPILEDP (this) || BOOL_VECTOR_P (this)))
89662fc3 502 wrong_type_argument (Qsequencep, this);
7b863bd5
JB
503 }
504
ea35ce3d
RS
505 /* Compute total length in chars of arguments in RESULT_LEN.
506 If desired output is a string, also compute length in bytes
507 in RESULT_LEN_BYTE, and determine in SOME_MULTIBYTE
508 whether the result should be a multibyte string. */
509 result_len_byte = 0;
510 result_len = 0;
511 some_multibyte = 0;
512 for (argnum = 0; argnum < nargs; argnum++)
7b863bd5 513 {
e6d4aefa 514 EMACS_INT len;
7b863bd5 515 this = args[argnum];
ea35ce3d
RS
516 len = XFASTINT (Flength (this));
517 if (target_type == Lisp_String)
5b6dddaa 518 {
09ab3c3b
KH
519 /* We must count the number of bytes needed in the string
520 as well as the number of characters. */
d311d28c 521 ptrdiff_t i;
5b6dddaa 522 Lisp_Object ch;
c1f134b5 523 int c;
d311d28c 524 ptrdiff_t this_len_byte;
5b6dddaa 525
876c194c 526 if (VECTORP (this) || COMPILEDP (this))
ea35ce3d 527 for (i = 0; i < len; i++)
dec58e65 528 {
7edbb0da 529 ch = AREF (this, i);
63db3c1b 530 CHECK_CHARACTER (ch);
c1f134b5
PE
531 c = XFASTINT (ch);
532 this_len_byte = CHAR_BYTES (c);
d311d28c
PE
533 if (STRING_BYTES_BOUND - result_len_byte < this_len_byte)
534 string_overflow ();
ea35ce3d 535 result_len_byte += this_len_byte;
c1f134b5 536 if (! ASCII_CHAR_P (c) && ! CHAR_BYTE8_P (c))
ea35ce3d 537 some_multibyte = 1;
dec58e65 538 }
1c0a7493 539 else if (BOOL_VECTOR_P (this) && bool_vector_size (this) > 0)
6d475204 540 wrong_type_argument (Qintegerp, Faref (this, make_number (0)));
ea35ce3d 541 else if (CONSP (this))
70949dac 542 for (; CONSP (this); this = XCDR (this))
dec58e65 543 {
70949dac 544 ch = XCAR (this);
63db3c1b 545 CHECK_CHARACTER (ch);
c1f134b5
PE
546 c = XFASTINT (ch);
547 this_len_byte = CHAR_BYTES (c);
d311d28c
PE
548 if (STRING_BYTES_BOUND - result_len_byte < this_len_byte)
549 string_overflow ();
ea35ce3d 550 result_len_byte += this_len_byte;
c1f134b5 551 if (! ASCII_CHAR_P (c) && ! CHAR_BYTE8_P (c))
ea35ce3d 552 some_multibyte = 1;
dec58e65 553 }
470730a8 554 else if (STRINGP (this))
ea35ce3d 555 {
06f57aa7 556 if (STRING_MULTIBYTE (this))
09ab3c3b
KH
557 {
558 some_multibyte = 1;
d311d28c 559 this_len_byte = SBYTES (this);
09ab3c3b
KH
560 }
561 else
d311d28c
PE
562 this_len_byte = count_size_as_multibyte (SDATA (this),
563 SCHARS (this));
564 if (STRING_BYTES_BOUND - result_len_byte < this_len_byte)
565 string_overflow ();
566 result_len_byte += this_len_byte;
ea35ce3d 567 }
5b6dddaa 568 }
ea35ce3d
RS
569
570 result_len += len;
d311d28c
PE
571 if (MOST_POSITIVE_FIXNUM < result_len)
572 memory_full (SIZE_MAX);
7b863bd5
JB
573 }
574
09ab3c3b
KH
575 if (! some_multibyte)
576 result_len_byte = result_len;
7b863bd5 577
ea35ce3d 578 /* Create the output object. */
7b863bd5 579 if (target_type == Lisp_Cons)
ea35ce3d 580 val = Fmake_list (make_number (result_len), Qnil);
3e7383eb 581 else if (target_type == Lisp_Vectorlike)
ea35ce3d 582 val = Fmake_vector (make_number (result_len), Qnil);
b10b2daa 583 else if (some_multibyte)
ea35ce3d 584 val = make_uninit_multibyte_string (result_len, result_len_byte);
b10b2daa
RS
585 else
586 val = make_uninit_string (result_len);
7b863bd5 587
09ab3c3b
KH
588 /* In `append', if all but last arg are nil, return last arg. */
589 if (target_type == Lisp_Cons && EQ (val, Qnil))
590 return last_tail;
7b863bd5 591
ea35ce3d 592 /* Copy the contents of the args into the result. */
7b863bd5 593 if (CONSP (val))
2d6115c8 594 tail = val, toindex = -1; /* -1 in toindex is flag we are making a list */
7b863bd5 595 else
ea35ce3d 596 toindex = 0, toindex_byte = 0;
7b863bd5
JB
597
598 prev = Qnil;
2d6115c8 599 if (STRINGP (val))
0065d054 600 SAFE_NALLOCA (textprops, 1, nargs);
7b863bd5
JB
601
602 for (argnum = 0; argnum < nargs; argnum++)
603 {
604 Lisp_Object thislen;
d311d28c
PE
605 ptrdiff_t thisleni = 0;
606 register ptrdiff_t thisindex = 0;
607 register ptrdiff_t thisindex_byte = 0;
7b863bd5
JB
608
609 this = args[argnum];
610 if (!CONSP (this))
611 thislen = Flength (this), thisleni = XINT (thislen);
612
ea35ce3d
RS
613 /* Between strings of the same kind, copy fast. */
614 if (STRINGP (this) && STRINGP (val)
615 && STRING_MULTIBYTE (this) == some_multibyte)
7b863bd5 616 {
d311d28c 617 ptrdiff_t thislen_byte = SBYTES (this);
2d6115c8 618
72af86bd 619 memcpy (SDATA (val) + toindex_byte, SDATA (this), SBYTES (this));
0c94c8d6 620 if (string_intervals (this))
2d6115c8 621 {
87f0532f 622 textprops[num_textprops].argnum = argnum;
38583a69 623 textprops[num_textprops].from = 0;
87f0532f 624 textprops[num_textprops++].to = toindex;
2d6115c8 625 }
ea35ce3d 626 toindex_byte += thislen_byte;
38583a69 627 toindex += thisleni;
ea35ce3d 628 }
09ab3c3b
KH
629 /* Copy a single-byte string to a multibyte string. */
630 else if (STRINGP (this) && STRINGP (val))
631 {
0c94c8d6 632 if (string_intervals (this))
2d6115c8 633 {
87f0532f
KH
634 textprops[num_textprops].argnum = argnum;
635 textprops[num_textprops].from = 0;
636 textprops[num_textprops++].to = toindex;
2d6115c8 637 }
d5db4077
KR
638 toindex_byte += copy_text (SDATA (this),
639 SDATA (val) + toindex_byte,
640 SCHARS (this), 0, 1);
09ab3c3b
KH
641 toindex += thisleni;
642 }
ea35ce3d
RS
643 else
644 /* Copy element by element. */
645 while (1)
646 {
647 register Lisp_Object elt;
648
649 /* Fetch next element of `this' arg into `elt', or break if
650 `this' is exhausted. */
651 if (NILP (this)) break;
652 if (CONSP (this))
70949dac 653 elt = XCAR (this), this = XCDR (this);
6a7df83b
RS
654 else if (thisindex >= thisleni)
655 break;
656 else if (STRINGP (this))
ea35ce3d 657 {
2cef5737 658 int c;
6a7df83b 659 if (STRING_MULTIBYTE (this))
c1f134b5
PE
660 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, this,
661 thisindex,
662 thisindex_byte);
6a7df83b 663 else
ea35ce3d 664 {
c1f134b5
PE
665 c = SREF (this, thisindex); thisindex++;
666 if (some_multibyte && !ASCII_CHAR_P (c))
667 c = BYTE8_TO_CHAR (c);
ea35ce3d 668 }
c1f134b5 669 XSETFASTINT (elt, c);
6a7df83b
RS
670 }
671 else if (BOOL_VECTOR_P (this))
672 {
df5b4930 673 elt = bool_vector_ref (this, thisindex);
6a7df83b 674 thisindex++;
ea35ce3d 675 }
6a7df83b 676 else
68b587a6
SM
677 {
678 elt = AREF (this, thisindex);
679 thisindex++;
680 }
7b863bd5 681
ea35ce3d
RS
682 /* Store this element into the result. */
683 if (toindex < 0)
7b863bd5 684 {
f3fbd155 685 XSETCAR (tail, elt);
ea35ce3d 686 prev = tail;
70949dac 687 tail = XCDR (tail);
7b863bd5 688 }
ea35ce3d 689 else if (VECTORP (val))
68b587a6
SM
690 {
691 ASET (val, toindex, elt);
692 toindex++;
693 }
ea35ce3d
RS
694 else
695 {
13bdea59
PE
696 int c;
697 CHECK_CHARACTER (elt);
698 c = XFASTINT (elt);
38583a69 699 if (some_multibyte)
13bdea59 700 toindex_byte += CHAR_STRING (c, SDATA (val) + toindex_byte);
ea35ce3d 701 else
13bdea59 702 SSET (val, toindex_byte++, c);
38583a69 703 toindex++;
ea35ce3d
RS
704 }
705 }
7b863bd5 706 }
265a9e55 707 if (!NILP (prev))
f3fbd155 708 XSETCDR (prev, last_tail);
7b863bd5 709
87f0532f 710 if (num_textprops > 0)
2d6115c8 711 {
33f37824 712 Lisp_Object props;
d311d28c 713 ptrdiff_t last_to_end = -1;
33f37824 714
87f0532f 715 for (argnum = 0; argnum < num_textprops; argnum++)
2d6115c8 716 {
87f0532f 717 this = args[textprops[argnum].argnum];
33f37824
KH
718 props = text_property_list (this,
719 make_number (0),
d5db4077 720 make_number (SCHARS (this)),
33f37824 721 Qnil);
66699ad3 722 /* If successive arguments have properties, be sure that the
33f37824 723 value of `composition' property be the copy. */
3bd00f3b 724 if (last_to_end == textprops[argnum].to)
33f37824
KH
725 make_composition_value_copy (props);
726 add_text_properties_from_list (val, props,
727 make_number (textprops[argnum].to));
d5db4077 728 last_to_end = textprops[argnum].to + SCHARS (this);
2d6115c8
KH
729 }
730 }
2ec7f67a
KS
731
732 SAFE_FREE ();
b4f334f7 733 return val;
7b863bd5
JB
734}
735\f
09ab3c3b 736static Lisp_Object string_char_byte_cache_string;
d311d28c
PE
737static ptrdiff_t string_char_byte_cache_charpos;
738static ptrdiff_t string_char_byte_cache_bytepos;
09ab3c3b 739
57247650 740void
971de7fb 741clear_string_char_byte_cache (void)
57247650
KH
742{
743 string_char_byte_cache_string = Qnil;
744}
745
13818c30 746/* Return the byte index corresponding to CHAR_INDEX in STRING. */
ea35ce3d 747
d311d28c
PE
748ptrdiff_t
749string_char_to_byte (Lisp_Object string, ptrdiff_t char_index)
ea35ce3d 750{
d311d28c
PE
751 ptrdiff_t i_byte;
752 ptrdiff_t best_below, best_below_byte;
753 ptrdiff_t best_above, best_above_byte;
ea35ce3d 754
09ab3c3b 755 best_below = best_below_byte = 0;
d5db4077
KR
756 best_above = SCHARS (string);
757 best_above_byte = SBYTES (string);
95ac7579
KH
758 if (best_above == best_above_byte)
759 return char_index;
09ab3c3b
KH
760
761 if (EQ (string, string_char_byte_cache_string))
762 {
763 if (string_char_byte_cache_charpos < char_index)
764 {
765 best_below = string_char_byte_cache_charpos;
766 best_below_byte = string_char_byte_cache_bytepos;
767 }
768 else
769 {
770 best_above = string_char_byte_cache_charpos;
771 best_above_byte = string_char_byte_cache_bytepos;
772 }
773 }
774
775 if (char_index - best_below < best_above - char_index)
776 {
8f924df7 777 unsigned char *p = SDATA (string) + best_below_byte;
38583a69 778
09ab3c3b
KH
779 while (best_below < char_index)
780 {
38583a69
KH
781 p += BYTES_BY_CHAR_HEAD (*p);
782 best_below++;
09ab3c3b 783 }
8f924df7 784 i_byte = p - SDATA (string);
09ab3c3b
KH
785 }
786 else
ea35ce3d 787 {
8f924df7 788 unsigned char *p = SDATA (string) + best_above_byte;
38583a69 789
09ab3c3b
KH
790 while (best_above > char_index)
791 {
38583a69
KH
792 p--;
793 while (!CHAR_HEAD_P (*p)) p--;
09ab3c3b
KH
794 best_above--;
795 }
8f924df7 796 i_byte = p - SDATA (string);
ea35ce3d
RS
797 }
798
09ab3c3b 799 string_char_byte_cache_bytepos = i_byte;
38583a69 800 string_char_byte_cache_charpos = char_index;
09ab3c3b
KH
801 string_char_byte_cache_string = string;
802
ea35ce3d
RS
803 return i_byte;
804}
09ab3c3b 805\f
ea35ce3d
RS
806/* Return the character index corresponding to BYTE_INDEX in STRING. */
807
d311d28c
PE
808ptrdiff_t
809string_byte_to_char (Lisp_Object string, ptrdiff_t byte_index)
ea35ce3d 810{
d311d28c
PE
811 ptrdiff_t i, i_byte;
812 ptrdiff_t best_below, best_below_byte;
813 ptrdiff_t best_above, best_above_byte;
ea35ce3d 814
09ab3c3b 815 best_below = best_below_byte = 0;
d5db4077
KR
816 best_above = SCHARS (string);
817 best_above_byte = SBYTES (string);
95ac7579
KH
818 if (best_above == best_above_byte)
819 return byte_index;
09ab3c3b
KH
820
821 if (EQ (string, string_char_byte_cache_string))
822 {
823 if (string_char_byte_cache_bytepos < byte_index)
824 {
825 best_below = string_char_byte_cache_charpos;
826 best_below_byte = string_char_byte_cache_bytepos;
827 }
828 else
829 {
830 best_above = string_char_byte_cache_charpos;
831 best_above_byte = string_char_byte_cache_bytepos;
832 }
833 }
834
835 if (byte_index - best_below_byte < best_above_byte - byte_index)
836 {
8f924df7
KH
837 unsigned char *p = SDATA (string) + best_below_byte;
838 unsigned char *pend = SDATA (string) + byte_index;
38583a69
KH
839
840 while (p < pend)
09ab3c3b 841 {
38583a69
KH
842 p += BYTES_BY_CHAR_HEAD (*p);
843 best_below++;
09ab3c3b
KH
844 }
845 i = best_below;
8f924df7 846 i_byte = p - SDATA (string);
09ab3c3b
KH
847 }
848 else
ea35ce3d 849 {
8f924df7
KH
850 unsigned char *p = SDATA (string) + best_above_byte;
851 unsigned char *pbeg = SDATA (string) + byte_index;
38583a69
KH
852
853 while (p > pbeg)
09ab3c3b 854 {
38583a69
KH
855 p--;
856 while (!CHAR_HEAD_P (*p)) p--;
09ab3c3b
KH
857 best_above--;
858 }
859 i = best_above;
8f924df7 860 i_byte = p - SDATA (string);
ea35ce3d
RS
861 }
862
09ab3c3b
KH
863 string_char_byte_cache_bytepos = i_byte;
864 string_char_byte_cache_charpos = i;
865 string_char_byte_cache_string = string;
866
ea35ce3d
RS
867 return i;
868}
09ab3c3b 869\f
9d6d303b 870/* Convert STRING to a multibyte string. */
ea35ce3d 871
2f7c71a1 872static Lisp_Object
971de7fb 873string_make_multibyte (Lisp_Object string)
ea35ce3d
RS
874{
875 unsigned char *buf;
d311d28c 876 ptrdiff_t nbytes;
e76ca790
MB
877 Lisp_Object ret;
878 USE_SAFE_ALLOCA;
ea35ce3d
RS
879
880 if (STRING_MULTIBYTE (string))
881 return string;
882
d5db4077
KR
883 nbytes = count_size_as_multibyte (SDATA (string),
884 SCHARS (string));
6d475204
RS
885 /* If all the chars are ASCII, they won't need any more bytes
886 once converted. In that case, we can return STRING itself. */
d5db4077 887 if (nbytes == SBYTES (string))
6d475204
RS
888 return string;
889
98c6f1e3 890 buf = SAFE_ALLOCA (nbytes);
d5db4077 891 copy_text (SDATA (string), buf, SBYTES (string),
ea35ce3d
RS
892 0, 1);
893
f1e59824 894 ret = make_multibyte_string ((char *) buf, SCHARS (string), nbytes);
233f3db6 895 SAFE_FREE ();
799c08ac
KS
896
897 return ret;
ea35ce3d
RS
898}
899
2df18cdb 900
8f924df7
KH
901/* Convert STRING (if unibyte) to a multibyte string without changing
902 the number of characters. Characters 0200 trough 0237 are
903 converted to eight-bit characters. */
2df18cdb
KH
904
905Lisp_Object
971de7fb 906string_to_multibyte (Lisp_Object string)
2df18cdb
KH
907{
908 unsigned char *buf;
d311d28c 909 ptrdiff_t nbytes;
799c08ac
KS
910 Lisp_Object ret;
911 USE_SAFE_ALLOCA;
2df18cdb
KH
912
913 if (STRING_MULTIBYTE (string))
914 return string;
915
de883a70 916 nbytes = count_size_as_multibyte (SDATA (string), SBYTES (string));
8f924df7
KH
917 /* If all the chars are ASCII, they won't need any more bytes once
918 converted. */
2df18cdb 919 if (nbytes == SBYTES (string))
42a5b22f 920 return make_multibyte_string (SSDATA (string), nbytes, nbytes);
2df18cdb 921
98c6f1e3 922 buf = SAFE_ALLOCA (nbytes);
72af86bd 923 memcpy (buf, SDATA (string), SBYTES (string));
2df18cdb
KH
924 str_to_multibyte (buf, nbytes, SBYTES (string));
925
f1e59824 926 ret = make_multibyte_string ((char *) buf, SCHARS (string), nbytes);
233f3db6 927 SAFE_FREE ();
799c08ac
KS
928
929 return ret;
2df18cdb
KH
930}
931
932
ea35ce3d
RS
933/* Convert STRING to a single-byte string. */
934
935Lisp_Object
971de7fb 936string_make_unibyte (Lisp_Object string)
ea35ce3d 937{
d311d28c 938 ptrdiff_t nchars;
ea35ce3d 939 unsigned char *buf;
a6cb6b78 940 Lisp_Object ret;
799c08ac 941 USE_SAFE_ALLOCA;
ea35ce3d
RS
942
943 if (! STRING_MULTIBYTE (string))
944 return string;
945
799c08ac 946 nchars = SCHARS (string);
ea35ce3d 947
98c6f1e3 948 buf = SAFE_ALLOCA (nchars);
d5db4077 949 copy_text (SDATA (string), buf, SBYTES (string),
ea35ce3d
RS
950 1, 0);
951
f1e59824 952 ret = make_unibyte_string ((char *) buf, nchars);
233f3db6 953 SAFE_FREE ();
a6cb6b78
JD
954
955 return ret;
ea35ce3d 956}
09ab3c3b 957
a7ca3326 958DEFUN ("string-make-multibyte", Fstring_make_multibyte, Sstring_make_multibyte,
09ab3c3b 959 1, 1, 0,
e9d8ddc9 960 doc: /* Return the multibyte equivalent of STRING.
6b61353c
KH
961If STRING is unibyte and contains non-ASCII characters, the function
962`unibyte-char-to-multibyte' is used to convert each unibyte character
963to a multibyte character. In this case, the returned string is a
964newly created string with no text properties. If STRING is multibyte
965or entirely ASCII, it is returned unchanged. In particular, when
966STRING is unibyte and entirely ASCII, the returned string is unibyte.
967\(When the characters are all ASCII, Emacs primitives will treat the
968string the same way whether it is unibyte or multibyte.) */)
5842a27b 969 (Lisp_Object string)
09ab3c3b 970{
b7826503 971 CHECK_STRING (string);
aabd38ec 972
09ab3c3b
KH
973 return string_make_multibyte (string);
974}
975
a7ca3326 976DEFUN ("string-make-unibyte", Fstring_make_unibyte, Sstring_make_unibyte,
09ab3c3b 977 1, 1, 0,
e9d8ddc9 978 doc: /* Return the unibyte equivalent of STRING.
f8f2fbf9
EZ
979Multibyte character codes are converted to unibyte according to
980`nonascii-translation-table' or, if that is nil, `nonascii-insert-offset'.
981If the lookup in the translation table fails, this function takes just
adf2c803 982the low 8 bits of each character. */)
5842a27b 983 (Lisp_Object string)
09ab3c3b 984{
b7826503 985 CHECK_STRING (string);
aabd38ec 986
09ab3c3b
KH
987 return string_make_unibyte (string);
988}
6d475204 989
a7ca3326 990DEFUN ("string-as-unibyte", Fstring_as_unibyte, Sstring_as_unibyte,
6d475204 991 1, 1, 0,
e9d8ddc9 992 doc: /* Return a unibyte string with the same individual bytes as STRING.
47cebab1
GM
993If STRING is unibyte, the result is STRING itself.
994Otherwise it is a newly created string, with no text properties.
995If STRING is multibyte and contains a character of charset
6b61353c 996`eight-bit', it is converted to the corresponding single byte. */)
5842a27b 997 (Lisp_Object string)
6d475204 998{
b7826503 999 CHECK_STRING (string);
aabd38ec 1000
6d475204
RS
1001 if (STRING_MULTIBYTE (string))
1002 {
17b9dc45 1003 unsigned char *str = (unsigned char *) xlispstrdup (string);
04d47595 1004 ptrdiff_t bytes = str_as_unibyte (str, SBYTES (string));
2efdd1b9 1005
f1e59824 1006 string = make_unibyte_string ((char *) str, bytes);
2efdd1b9 1007 xfree (str);
6d475204
RS
1008 }
1009 return string;
1010}
1011
a7ca3326 1012DEFUN ("string-as-multibyte", Fstring_as_multibyte, Sstring_as_multibyte,
6d475204 1013 1, 1, 0,
e9d8ddc9 1014 doc: /* Return a multibyte string with the same individual bytes as STRING.
47cebab1
GM
1015If STRING is multibyte, the result is STRING itself.
1016Otherwise it is a newly created string, with no text properties.
2d5cc537 1017
47cebab1 1018If STRING is unibyte and contains an individual 8-bit byte (i.e. not
2d5cc537
DL
1019part of a correct utf-8 sequence), it is converted to the corresponding
1020multibyte character of charset `eight-bit'.
3100d59f
KH
1021See also `string-to-multibyte'.
1022
1023Beware, this often doesn't really do what you think it does.
1024It is similar to (decode-coding-string STRING 'utf-8-emacs).
1025If you're not sure, whether to use `string-as-multibyte' or
1026`string-to-multibyte', use `string-to-multibyte'. */)
5842a27b 1027 (Lisp_Object string)
6d475204 1028{
b7826503 1029 CHECK_STRING (string);
aabd38ec 1030
6d475204
RS
1031 if (! STRING_MULTIBYTE (string))
1032 {
2efdd1b9 1033 Lisp_Object new_string;
d311d28c 1034 ptrdiff_t nchars, nbytes;
2efdd1b9 1035
d5db4077
KR
1036 parse_str_as_multibyte (SDATA (string),
1037 SBYTES (string),
2efdd1b9
KH
1038 &nchars, &nbytes);
1039 new_string = make_uninit_multibyte_string (nchars, nbytes);
72af86bd 1040 memcpy (SDATA (new_string), SDATA (string), SBYTES (string));
d5db4077
KR
1041 if (nbytes != SBYTES (string))
1042 str_as_multibyte (SDATA (new_string), nbytes,
1043 SBYTES (string), NULL);
2efdd1b9 1044 string = new_string;
0c94c8d6 1045 set_string_intervals (string, NULL);
6d475204
RS
1046 }
1047 return string;
1048}
2df18cdb 1049
a7ca3326 1050DEFUN ("string-to-multibyte", Fstring_to_multibyte, Sstring_to_multibyte,
2df18cdb
KH
1051 1, 1, 0,
1052 doc: /* Return a multibyte string with the same individual chars as STRING.
9c7a329a 1053If STRING is multibyte, the result is STRING itself.
2df18cdb 1054Otherwise it is a newly created string, with no text properties.
88dad6e7
KH
1055
1056If STRING is unibyte and contains an 8-bit byte, it is converted to
2d5cc537
DL
1057the corresponding multibyte character of charset `eight-bit'.
1058
1059This differs from `string-as-multibyte' by converting each byte of a correct
1060utf-8 sequence to an eight-bit character, not just bytes that don't form a
1061correct sequence. */)
5842a27b 1062 (Lisp_Object string)
2df18cdb
KH
1063{
1064 CHECK_STRING (string);
1065
1066 return string_to_multibyte (string);
1067}
1068
b4480f16 1069DEFUN ("string-to-unibyte", Fstring_to_unibyte, Sstring_to_unibyte,
6e8b42de 1070 1, 1, 0,
b4480f16
KH
1071 doc: /* Return a unibyte string with the same individual chars as STRING.
1072If STRING is unibyte, the result is STRING itself.
1073Otherwise it is a newly created string, with no text properties,
1074where each `eight-bit' character is converted to the corresponding byte.
1075If STRING contains a non-ASCII, non-`eight-bit' character,
6e8b42de 1076an error is signaled. */)
5842a27b 1077 (Lisp_Object string)
b4480f16
KH
1078{
1079 CHECK_STRING (string);
1080
1081 if (STRING_MULTIBYTE (string))
1082 {
d311d28c 1083 ptrdiff_t chars = SCHARS (string);
23f86fce 1084 unsigned char *str = xmalloc (chars);
67dbec33 1085 ptrdiff_t converted = str_to_unibyte (SDATA (string), str, chars);
6e8b42de 1086
b4480f16 1087 if (converted < chars)
7c85f529 1088 error ("Can't convert the %"pD"dth character to unibyte", converted);
f1e59824 1089 string = make_unibyte_string ((char *) str, chars);
b4480f16
KH
1090 xfree (str);
1091 }
1092 return string;
1093}
1094
ea35ce3d 1095\f
a7ca3326 1096DEFUN ("copy-alist", Fcopy_alist, Scopy_alist, 1, 1, 0,
e9d8ddc9 1097 doc: /* Return a copy of ALIST.
47cebab1
GM
1098This is an alist which represents the same mapping from objects to objects,
1099but does not share the alist structure with ALIST.
1100The objects mapped (cars and cdrs of elements of the alist)
1101are shared, however.
e9d8ddc9 1102Elements of ALIST that are not conses are also shared. */)
5842a27b 1103 (Lisp_Object alist)
7b863bd5
JB
1104{
1105 register Lisp_Object tem;
1106
b7826503 1107 CHECK_LIST (alist);
265a9e55 1108 if (NILP (alist))
7b863bd5
JB
1109 return alist;
1110 alist = concat (1, &alist, Lisp_Cons, 0);
70949dac 1111 for (tem = alist; CONSP (tem); tem = XCDR (tem))
7b863bd5
JB
1112 {
1113 register Lisp_Object car;
70949dac 1114 car = XCAR (tem);
7b863bd5
JB
1115
1116 if (CONSP (car))
f3fbd155 1117 XSETCAR (tem, Fcons (XCAR (car), XCDR (car)));
7b863bd5
JB
1118 }
1119 return alist;
1120}
1121
a7ca3326 1122DEFUN ("substring", Fsubstring, Ssubstring, 2, 3, 0,
753169bd
CY
1123 doc: /* Return a new string whose contents are a substring of STRING.
1124The returned string consists of the characters between index FROM
1125\(inclusive) and index TO (exclusive) of STRING. FROM and TO are
1126zero-indexed: 0 means the first character of STRING. Negative values
1127are counted from the end of STRING. If TO is nil, the substring runs
1128to the end of STRING.
1129
1130The STRING argument may also be a vector. In that case, the return
1131value is a new vector that contains the elements between index FROM
1132\(inclusive) and index TO (exclusive) of that vector argument. */)
5842a27b 1133 (Lisp_Object string, register Lisp_Object from, Lisp_Object to)
7b863bd5 1134{
ac811a55 1135 Lisp_Object res;
d311d28c 1136 ptrdiff_t size;
e6d4aefa 1137 EMACS_INT from_char, to_char;
21fbc8e5 1138
89662fc3 1139 CHECK_VECTOR_OR_STRING (string);
b7826503 1140 CHECK_NUMBER (from);
21fbc8e5
RS
1141
1142 if (STRINGP (string))
d311d28c 1143 size = SCHARS (string);
21fbc8e5 1144 else
7edbb0da 1145 size = ASIZE (string);
21fbc8e5 1146
265a9e55 1147 if (NILP (to))
d311d28c 1148 to_char = size;
7b863bd5 1149 else
ea35ce3d 1150 {
b7826503 1151 CHECK_NUMBER (to);
ea35ce3d
RS
1152
1153 to_char = XINT (to);
1154 if (to_char < 0)
1155 to_char += size;
ea35ce3d
RS
1156 }
1157
1158 from_char = XINT (from);
1159 if (from_char < 0)
1160 from_char += size;
ea35ce3d
RS
1161 if (!(0 <= from_char && from_char <= to_char && to_char <= size))
1162 args_out_of_range_3 (string, make_number (from_char),
1163 make_number (to_char));
8c172e82 1164
21fbc8e5
RS
1165 if (STRINGP (string))
1166 {
d311d28c
PE
1167 ptrdiff_t to_byte =
1168 (NILP (to) ? SBYTES (string) : string_char_to_byte (string, to_char));
1169 ptrdiff_t from_byte = string_char_to_byte (string, from_char);
42a5b22f 1170 res = make_specified_string (SSDATA (string) + from_byte,
b10b2daa
RS
1171 to_char - from_char, to_byte - from_byte,
1172 STRING_MULTIBYTE (string));
21ab867f
AS
1173 copy_text_properties (make_number (from_char), make_number (to_char),
1174 string, make_number (0), res, Qnil);
ea35ce3d
RS
1175 }
1176 else
4939150c 1177 res = Fvector (to_char - from_char, aref_addr (string, from_char));
ea35ce3d
RS
1178
1179 return res;
1180}
1181
aebf4d42
RS
1182
1183DEFUN ("substring-no-properties", Fsubstring_no_properties, Ssubstring_no_properties, 1, 3, 0,
1184 doc: /* Return a substring of STRING, without text properties.
b756c005 1185It starts at index FROM and ends before TO.
aebf4d42
RS
1186TO may be nil or omitted; then the substring runs to the end of STRING.
1187If FROM is nil or omitted, the substring starts at the beginning of STRING.
1188If FROM or TO is negative, it counts from the end.
1189
1190With one argument, just copy STRING without its properties. */)
5842a27b 1191 (Lisp_Object string, register Lisp_Object from, Lisp_Object to)
aebf4d42 1192{
d311d28c 1193 ptrdiff_t size;
e6d4aefa 1194 EMACS_INT from_char, to_char;
d311d28c 1195 ptrdiff_t from_byte, to_byte;
aebf4d42
RS
1196
1197 CHECK_STRING (string);
1198
d5db4077 1199 size = SCHARS (string);
aebf4d42
RS
1200
1201 if (NILP (from))
d311d28c 1202 from_char = 0;
aebf4d42
RS
1203 else
1204 {
1205 CHECK_NUMBER (from);
1206 from_char = XINT (from);
1207 if (from_char < 0)
1208 from_char += size;
aebf4d42
RS
1209 }
1210
1211 if (NILP (to))
d311d28c 1212 to_char = size;
aebf4d42
RS
1213 else
1214 {
1215 CHECK_NUMBER (to);
aebf4d42
RS
1216 to_char = XINT (to);
1217 if (to_char < 0)
1218 to_char += size;
aebf4d42
RS
1219 }
1220
1221 if (!(0 <= from_char && from_char <= to_char && to_char <= size))
1222 args_out_of_range_3 (string, make_number (from_char),
1223 make_number (to_char));
1224
d311d28c
PE
1225 from_byte = NILP (from) ? 0 : string_char_to_byte (string, from_char);
1226 to_byte =
1227 NILP (to) ? SBYTES (string) : string_char_to_byte (string, to_char);
42a5b22f 1228 return make_specified_string (SSDATA (string) + from_byte,
aebf4d42
RS
1229 to_char - from_char, to_byte - from_byte,
1230 STRING_MULTIBYTE (string));
1231}
1232
ea35ce3d
RS
1233/* Extract a substring of STRING, giving start and end positions
1234 both in characters and in bytes. */
1235
1236Lisp_Object
d311d28c
PE
1237substring_both (Lisp_Object string, ptrdiff_t from, ptrdiff_t from_byte,
1238 ptrdiff_t to, ptrdiff_t to_byte)
ea35ce3d
RS
1239{
1240 Lisp_Object res;
d311d28c 1241 ptrdiff_t size;
ea35ce3d 1242
89662fc3 1243 CHECK_VECTOR_OR_STRING (string);
ea35ce3d 1244
0bc0b309 1245 size = STRINGP (string) ? SCHARS (string) : ASIZE (string);
ea35ce3d
RS
1246
1247 if (!(0 <= from && from <= to && to <= size))
1248 args_out_of_range_3 (string, make_number (from), make_number (to));
1249
1250 if (STRINGP (string))
1251 {
42a5b22f 1252 res = make_specified_string (SSDATA (string) + from_byte,
b10b2daa
RS
1253 to - from, to_byte - from_byte,
1254 STRING_MULTIBYTE (string));
21ab867f
AS
1255 copy_text_properties (make_number (from), make_number (to),
1256 string, make_number (0), res, Qnil);
21fbc8e5
RS
1257 }
1258 else
4939150c 1259 res = Fvector (to - from, aref_addr (string, from));
b4f334f7 1260
ac811a55 1261 return res;
7b863bd5
JB
1262}
1263\f
a7ca3326 1264DEFUN ("nthcdr", Fnthcdr, Snthcdr, 2, 2, 0,
b756c005 1265 doc: /* Take cdr N times on LIST, return the result. */)
5842a27b 1266 (Lisp_Object n, Lisp_Object list)
7b863bd5 1267{
6346d301 1268 EMACS_INT i, num;
b7826503 1269 CHECK_NUMBER (n);
7b863bd5 1270 num = XINT (n);
265a9e55 1271 for (i = 0; i < num && !NILP (list); i++)
7b863bd5
JB
1272 {
1273 QUIT;
89662fc3 1274 CHECK_LIST_CONS (list, list);
71a8e74b 1275 list = XCDR (list);
7b863bd5
JB
1276 }
1277 return list;
1278}
1279
a7ca3326 1280DEFUN ("nth", Fnth, Snth, 2, 2, 0,
e9d8ddc9
MB
1281 doc: /* Return the Nth element of LIST.
1282N counts from zero. If LIST is not that long, nil is returned. */)
5842a27b 1283 (Lisp_Object n, Lisp_Object list)
7b863bd5
JB
1284{
1285 return Fcar (Fnthcdr (n, list));
1286}
1287
a7ca3326 1288DEFUN ("elt", Felt, Selt, 2, 2, 0,
e9d8ddc9 1289 doc: /* Return element of SEQUENCE at index N. */)
5842a27b 1290 (register Lisp_Object sequence, Lisp_Object n)
7b863bd5 1291{
b7826503 1292 CHECK_NUMBER (n);
89662fc3
KS
1293 if (CONSP (sequence) || NILP (sequence))
1294 return Fcar (Fnthcdr (n, sequence));
1295
1296 /* Faref signals a "not array" error, so check here. */
876c194c 1297 CHECK_ARRAY (sequence, Qsequencep);
89662fc3 1298 return Faref (sequence, n);
7b863bd5
JB
1299}
1300
a7ca3326 1301DEFUN ("member", Fmember, Smember, 2, 2, 0,
b756c005 1302 doc: /* Return non-nil if ELT is an element of LIST. Comparison done with `equal'.
e9d8ddc9 1303The value is actually the tail of LIST whose car is ELT. */)
5842a27b 1304 (register Lisp_Object elt, Lisp_Object list)
7b863bd5
JB
1305{
1306 register Lisp_Object tail;
9beb8baa 1307 for (tail = list; CONSP (tail); tail = XCDR (tail))
7b863bd5
JB
1308 {
1309 register Lisp_Object tem;
89662fc3 1310 CHECK_LIST_CONS (tail, list);
71a8e74b 1311 tem = XCAR (tail);
265a9e55 1312 if (! NILP (Fequal (elt, tem)))
7b863bd5
JB
1313 return tail;
1314 QUIT;
1315 }
1316 return Qnil;
1317}
1318
a7ca3326 1319DEFUN ("memq", Fmemq, Smemq, 2, 2, 0,
b756c005 1320 doc: /* Return non-nil if ELT is an element of LIST. Comparison done with `eq'.
008ef0ef 1321The value is actually the tail of LIST whose car is ELT. */)
5842a27b 1322 (register Lisp_Object elt, Lisp_Object list)
7b863bd5 1323{
f2be3671 1324 while (1)
7b863bd5 1325 {
f2be3671
GM
1326 if (!CONSP (list) || EQ (XCAR (list), elt))
1327 break;
59f953a2 1328
f2be3671
GM
1329 list = XCDR (list);
1330 if (!CONSP (list) || EQ (XCAR (list), elt))
1331 break;
1332
1333 list = XCDR (list);
1334 if (!CONSP (list) || EQ (XCAR (list), elt))
1335 break;
1336
1337 list = XCDR (list);
7b863bd5
JB
1338 QUIT;
1339 }
f2be3671 1340
89662fc3 1341 CHECK_LIST (list);
f2be3671 1342 return list;
7b863bd5
JB
1343}
1344
008ef0ef 1345DEFUN ("memql", Fmemql, Smemql, 2, 2, 0,
b756c005 1346 doc: /* Return non-nil if ELT is an element of LIST. Comparison done with `eql'.
008ef0ef 1347The value is actually the tail of LIST whose car is ELT. */)
5842a27b 1348 (register Lisp_Object elt, Lisp_Object list)
008ef0ef
KS
1349{
1350 register Lisp_Object tail;
1351
1352 if (!FLOATP (elt))
1353 return Fmemq (elt, list);
1354
9beb8baa 1355 for (tail = list; CONSP (tail); tail = XCDR (tail))
008ef0ef
KS
1356 {
1357 register Lisp_Object tem;
1358 CHECK_LIST_CONS (tail, list);
1359 tem = XCAR (tail);
1360 if (FLOATP (tem) && internal_equal (elt, tem, 0, 0))
1361 return tail;
1362 QUIT;
1363 }
1364 return Qnil;
1365}
1366
a7ca3326 1367DEFUN ("assq", Fassq, Sassq, 2, 2, 0,
e9d8ddc9 1368 doc: /* Return non-nil if KEY is `eq' to the car of an element of LIST.
6b61353c 1369The value is actually the first element of LIST whose car is KEY.
e9d8ddc9 1370Elements of LIST that are not conses are ignored. */)
5842a27b 1371 (Lisp_Object key, Lisp_Object list)
7b863bd5 1372{
f2be3671 1373 while (1)
7b863bd5 1374 {
f2be3671
GM
1375 if (!CONSP (list)
1376 || (CONSP (XCAR (list))
1377 && EQ (XCAR (XCAR (list)), key)))
1378 break;
59f953a2 1379
f2be3671
GM
1380 list = XCDR (list);
1381 if (!CONSP (list)
1382 || (CONSP (XCAR (list))
1383 && EQ (XCAR (XCAR (list)), key)))
1384 break;
59f953a2 1385
f2be3671
GM
1386 list = XCDR (list);
1387 if (!CONSP (list)
1388 || (CONSP (XCAR (list))
1389 && EQ (XCAR (XCAR (list)), key)))
1390 break;
59f953a2 1391
f2be3671 1392 list = XCDR (list);
7b863bd5
JB
1393 QUIT;
1394 }
f2be3671 1395
89662fc3 1396 return CAR (list);
7b863bd5
JB
1397}
1398
1399/* Like Fassq but never report an error and do not allow quits.
1400 Use only on lists known never to be circular. */
1401
1402Lisp_Object
971de7fb 1403assq_no_quit (Lisp_Object key, Lisp_Object list)
7b863bd5 1404{
f2be3671
GM
1405 while (CONSP (list)
1406 && (!CONSP (XCAR (list))
1407 || !EQ (XCAR (XCAR (list)), key)))
1408 list = XCDR (list);
1409
89662fc3 1410 return CAR_SAFE (list);
7b863bd5
JB
1411}
1412
a7ca3326 1413DEFUN ("assoc", Fassoc, Sassoc, 2, 2, 0,
e9d8ddc9 1414 doc: /* Return non-nil if KEY is `equal' to the car of an element of LIST.
6b61353c 1415The value is actually the first element of LIST whose car equals KEY. */)
5842a27b 1416 (Lisp_Object key, Lisp_Object list)
7b863bd5 1417{
89662fc3 1418 Lisp_Object car;
f2be3671
GM
1419
1420 while (1)
7b863bd5 1421 {
f2be3671
GM
1422 if (!CONSP (list)
1423 || (CONSP (XCAR (list))
1424 && (car = XCAR (XCAR (list)),
1425 EQ (car, key) || !NILP (Fequal (car, key)))))
1426 break;
59f953a2 1427
f2be3671
GM
1428 list = XCDR (list);
1429 if (!CONSP (list)
1430 || (CONSP (XCAR (list))
1431 && (car = XCAR (XCAR (list)),
1432 EQ (car, key) || !NILP (Fequal (car, key)))))
1433 break;
59f953a2 1434
f2be3671
GM
1435 list = XCDR (list);
1436 if (!CONSP (list)
1437 || (CONSP (XCAR (list))
1438 && (car = XCAR (XCAR (list)),
1439 EQ (car, key) || !NILP (Fequal (car, key)))))
1440 break;
59f953a2 1441
f2be3671 1442 list = XCDR (list);
7b863bd5
JB
1443 QUIT;
1444 }
f2be3671 1445
89662fc3 1446 return CAR (list);
7b863bd5
JB
1447}
1448
86840809
KH
1449/* Like Fassoc but never report an error and do not allow quits.
1450 Use only on lists known never to be circular. */
1451
1452Lisp_Object
971de7fb 1453assoc_no_quit (Lisp_Object key, Lisp_Object list)
86840809
KH
1454{
1455 while (CONSP (list)
1456 && (!CONSP (XCAR (list))
1457 || (!EQ (XCAR (XCAR (list)), key)
1458 && NILP (Fequal (XCAR (XCAR (list)), key)))))
1459 list = XCDR (list);
1460
1461 return CONSP (list) ? XCAR (list) : Qnil;
1462}
1463
a7ca3326 1464DEFUN ("rassq", Frassq, Srassq, 2, 2, 0,
e9d8ddc9 1465 doc: /* Return non-nil if KEY is `eq' to the cdr of an element of LIST.
6b61353c 1466The value is actually the first element of LIST whose cdr is KEY. */)
5842a27b 1467 (register Lisp_Object key, Lisp_Object list)
7b863bd5 1468{
f2be3671 1469 while (1)
7b863bd5 1470 {
f2be3671
GM
1471 if (!CONSP (list)
1472 || (CONSP (XCAR (list))
1473 && EQ (XCDR (XCAR (list)), key)))
1474 break;
59f953a2 1475
f2be3671
GM
1476 list = XCDR (list);
1477 if (!CONSP (list)
1478 || (CONSP (XCAR (list))
1479 && EQ (XCDR (XCAR (list)), key)))
1480 break;
59f953a2 1481
f2be3671
GM
1482 list = XCDR (list);
1483 if (!CONSP (list)
1484 || (CONSP (XCAR (list))
1485 && EQ (XCDR (XCAR (list)), key)))
1486 break;
59f953a2 1487
f2be3671 1488 list = XCDR (list);
7b863bd5
JB
1489 QUIT;
1490 }
f2be3671 1491
89662fc3 1492 return CAR (list);
7b863bd5 1493}
0fb5a19c 1494
a7ca3326 1495DEFUN ("rassoc", Frassoc, Srassoc, 2, 2, 0,
e9d8ddc9 1496 doc: /* Return non-nil if KEY is `equal' to the cdr of an element of LIST.
6b61353c 1497The value is actually the first element of LIST whose cdr equals KEY. */)
5842a27b 1498 (Lisp_Object key, Lisp_Object list)
0fb5a19c 1499{
89662fc3 1500 Lisp_Object cdr;
f2be3671
GM
1501
1502 while (1)
0fb5a19c 1503 {
f2be3671
GM
1504 if (!CONSP (list)
1505 || (CONSP (XCAR (list))
1506 && (cdr = XCDR (XCAR (list)),
1507 EQ (cdr, key) || !NILP (Fequal (cdr, key)))))
1508 break;
59f953a2 1509
f2be3671
GM
1510 list = XCDR (list);
1511 if (!CONSP (list)
1512 || (CONSP (XCAR (list))
1513 && (cdr = XCDR (XCAR (list)),
1514 EQ (cdr, key) || !NILP (Fequal (cdr, key)))))
1515 break;
59f953a2 1516
f2be3671
GM
1517 list = XCDR (list);
1518 if (!CONSP (list)
1519 || (CONSP (XCAR (list))
1520 && (cdr = XCDR (XCAR (list)),
1521 EQ (cdr, key) || !NILP (Fequal (cdr, key)))))
1522 break;
59f953a2 1523
f2be3671 1524 list = XCDR (list);
0fb5a19c
RS
1525 QUIT;
1526 }
f2be3671 1527
89662fc3 1528 return CAR (list);
0fb5a19c 1529}
7b863bd5 1530\f
a7ca3326 1531DEFUN ("delq", Fdelq, Sdelq, 2, 2, 0,
d105a573
CY
1532 doc: /* Delete members of LIST which are `eq' to ELT, and return the result.
1533More precisely, this function skips any members `eq' to ELT at the
1534front of LIST, then removes members `eq' to ELT from the remaining
1535sublist by modifying its list structure, then returns the resulting
1536list.
1537
1538Write `(setq foo (delq element foo))' to be sure of correctly changing
1539the value of a list `foo'. */)
5842a27b 1540 (register Lisp_Object elt, Lisp_Object list)
7b863bd5
JB
1541{
1542 register Lisp_Object tail, prev;
1543 register Lisp_Object tem;
1544
1545 tail = list;
1546 prev = Qnil;
7ffe7ef6 1547 while (CONSP (tail))
7b863bd5 1548 {
89662fc3 1549 CHECK_LIST_CONS (tail, list);
71a8e74b 1550 tem = XCAR (tail);
7b863bd5
JB
1551 if (EQ (elt, tem))
1552 {
265a9e55 1553 if (NILP (prev))
70949dac 1554 list = XCDR (tail);
7b863bd5 1555 else
70949dac 1556 Fsetcdr (prev, XCDR (tail));
7b863bd5
JB
1557 }
1558 else
1559 prev = tail;
70949dac 1560 tail = XCDR (tail);
7b863bd5
JB
1561 QUIT;
1562 }
1563 return list;
1564}
1565
a7ca3326 1566DEFUN ("delete", Fdelete, Sdelete, 2, 2, 0,
d105a573
CY
1567 doc: /* Delete members of SEQ which are `equal' to ELT, and return the result.
1568SEQ must be a sequence (i.e. a list, a vector, or a string).
1569The return value is a sequence of the same type.
1570
1571If SEQ is a list, this behaves like `delq', except that it compares
1572with `equal' instead of `eq'. In particular, it may remove elements
1573by altering the list structure.
1574
1575If SEQ is not a list, deletion is never performed destructively;
1576instead this function creates and returns a new vector or string.
1577
1578Write `(setq foo (delete element foo))' to be sure of correctly
1579changing the value of a sequence `foo'. */)
5842a27b 1580 (Lisp_Object elt, Lisp_Object seq)
1e134a5f 1581{
e517f19d
GM
1582 if (VECTORP (seq))
1583 {
d311d28c 1584 ptrdiff_t i, n;
1e134a5f 1585
e517f19d
GM
1586 for (i = n = 0; i < ASIZE (seq); ++i)
1587 if (NILP (Fequal (AREF (seq, i), elt)))
1588 ++n;
1589
1590 if (n != ASIZE (seq))
1591 {
b3660ef6 1592 struct Lisp_Vector *p = allocate_vector (n);
59f953a2 1593
e517f19d
GM
1594 for (i = n = 0; i < ASIZE (seq); ++i)
1595 if (NILP (Fequal (AREF (seq, i), elt)))
91f2d272 1596 p->contents[n++] = AREF (seq, i);
e517f19d 1597
e517f19d
GM
1598 XSETVECTOR (seq, p);
1599 }
1600 }
1601 else if (STRINGP (seq))
1e134a5f 1602 {
d311d28c 1603 ptrdiff_t i, ibyte, nchars, nbytes, cbytes;
e517f19d
GM
1604 int c;
1605
1606 for (i = nchars = nbytes = ibyte = 0;
d5db4077 1607 i < SCHARS (seq);
e517f19d 1608 ++i, ibyte += cbytes)
1e134a5f 1609 {
e517f19d
GM
1610 if (STRING_MULTIBYTE (seq))
1611 {
62a6e103 1612 c = STRING_CHAR (SDATA (seq) + ibyte);
e517f19d
GM
1613 cbytes = CHAR_BYTES (c);
1614 }
1e134a5f 1615 else
e517f19d 1616 {
d5db4077 1617 c = SREF (seq, i);
e517f19d
GM
1618 cbytes = 1;
1619 }
59f953a2 1620
e517f19d
GM
1621 if (!INTEGERP (elt) || c != XINT (elt))
1622 {
1623 ++nchars;
1624 nbytes += cbytes;
1625 }
1626 }
1627
d5db4077 1628 if (nchars != SCHARS (seq))
e517f19d
GM
1629 {
1630 Lisp_Object tem;
1631
1632 tem = make_uninit_multibyte_string (nchars, nbytes);
1633 if (!STRING_MULTIBYTE (seq))
d5db4077 1634 STRING_SET_UNIBYTE (tem);
59f953a2 1635
e517f19d 1636 for (i = nchars = nbytes = ibyte = 0;
d5db4077 1637 i < SCHARS (seq);
e517f19d
GM
1638 ++i, ibyte += cbytes)
1639 {
1640 if (STRING_MULTIBYTE (seq))
1641 {
62a6e103 1642 c = STRING_CHAR (SDATA (seq) + ibyte);
e517f19d
GM
1643 cbytes = CHAR_BYTES (c);
1644 }
1645 else
1646 {
d5db4077 1647 c = SREF (seq, i);
e517f19d
GM
1648 cbytes = 1;
1649 }
59f953a2 1650
e517f19d
GM
1651 if (!INTEGERP (elt) || c != XINT (elt))
1652 {
08663750
KR
1653 unsigned char *from = SDATA (seq) + ibyte;
1654 unsigned char *to = SDATA (tem) + nbytes;
d311d28c 1655 ptrdiff_t n;
59f953a2 1656
e517f19d
GM
1657 ++nchars;
1658 nbytes += cbytes;
59f953a2 1659
e517f19d
GM
1660 for (n = cbytes; n--; )
1661 *to++ = *from++;
1662 }
1663 }
1664
1665 seq = tem;
1e134a5f 1666 }
1e134a5f 1667 }
e517f19d
GM
1668 else
1669 {
1670 Lisp_Object tail, prev;
1671
9beb8baa 1672 for (tail = seq, prev = Qnil; CONSP (tail); tail = XCDR (tail))
e517f19d 1673 {
89662fc3 1674 CHECK_LIST_CONS (tail, seq);
59f953a2 1675
e517f19d
GM
1676 if (!NILP (Fequal (elt, XCAR (tail))))
1677 {
1678 if (NILP (prev))
1679 seq = XCDR (tail);
1680 else
1681 Fsetcdr (prev, XCDR (tail));
1682 }
1683 else
1684 prev = tail;
1685 QUIT;
1686 }
1687 }
59f953a2 1688
e517f19d 1689 return seq;
1e134a5f
RM
1690}
1691
a7ca3326 1692DEFUN ("nreverse", Fnreverse, Snreverse, 1, 1, 0,
e9d8ddc9 1693 doc: /* Reverse LIST by modifying cdr pointers.
f60d391f 1694Return the reversed list. Expects a properly nil-terminated list. */)
5842a27b 1695 (Lisp_Object list)
7b863bd5
JB
1696{
1697 register Lisp_Object prev, tail, next;
1698
265a9e55 1699 if (NILP (list)) return list;
7b863bd5
JB
1700 prev = Qnil;
1701 tail = list;
265a9e55 1702 while (!NILP (tail))
7b863bd5
JB
1703 {
1704 QUIT;
f60d391f 1705 CHECK_LIST_CONS (tail, tail);
71a8e74b 1706 next = XCDR (tail);
7b863bd5
JB
1707 Fsetcdr (tail, prev);
1708 prev = tail;
1709 tail = next;
1710 }
1711 return prev;
1712}
1713
a7ca3326 1714DEFUN ("reverse", Freverse, Sreverse, 1, 1, 0,
6b61353c 1715 doc: /* Reverse LIST, copying. Return the reversed list.
e9d8ddc9 1716See also the function `nreverse', which is used more often. */)
5842a27b 1717 (Lisp_Object list)
7b863bd5 1718{
9d14ae76 1719 Lisp_Object new;
7b863bd5 1720
70949dac 1721 for (new = Qnil; CONSP (list); list = XCDR (list))
5c3ea973
DL
1722 {
1723 QUIT;
1724 new = Fcons (XCAR (list), new);
1725 }
89662fc3 1726 CHECK_LIST_END (list, list);
9d14ae76 1727 return new;
7b863bd5
JB
1728}
1729\f
a7ca3326 1730DEFUN ("sort", Fsort, Ssort, 2, 2, 0,
e9d8ddc9 1731 doc: /* Sort LIST, stably, comparing elements using PREDICATE.
47cebab1 1732Returns the sorted list. LIST is modified by side effects.
5c796e80 1733PREDICATE is called with two elements of LIST, and should return non-nil
71f6424d 1734if the first element should sort before the second. */)
5842a27b 1735 (Lisp_Object list, Lisp_Object predicate)
7b863bd5
JB
1736{
1737 Lisp_Object front, back;
1738 register Lisp_Object len, tem;
1739 struct gcpro gcpro1, gcpro2;
6346d301 1740 EMACS_INT length;
7b863bd5
JB
1741
1742 front = list;
1743 len = Flength (list);
1744 length = XINT (len);
1745 if (length < 2)
1746 return list;
1747
1748 XSETINT (len, (length / 2) - 1);
1749 tem = Fnthcdr (len, list);
1750 back = Fcdr (tem);
1751 Fsetcdr (tem, Qnil);
1752
1753 GCPRO2 (front, back);
88fe8140
EN
1754 front = Fsort (front, predicate);
1755 back = Fsort (back, predicate);
7b863bd5 1756 UNGCPRO;
88fe8140 1757 return merge (front, back, predicate);
7b863bd5
JB
1758}
1759
1760Lisp_Object
971de7fb 1761merge (Lisp_Object org_l1, Lisp_Object org_l2, Lisp_Object pred)
7b863bd5
JB
1762{
1763 Lisp_Object value;
1764 register Lisp_Object tail;
1765 Lisp_Object tem;
1766 register Lisp_Object l1, l2;
1767 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1768
1769 l1 = org_l1;
1770 l2 = org_l2;
1771 tail = Qnil;
1772 value = Qnil;
1773
1774 /* It is sufficient to protect org_l1 and org_l2.
1775 When l1 and l2 are updated, we copy the new values
1776 back into the org_ vars. */
1777 GCPRO4 (org_l1, org_l2, pred, value);
1778
1779 while (1)
1780 {
265a9e55 1781 if (NILP (l1))
7b863bd5
JB
1782 {
1783 UNGCPRO;
265a9e55 1784 if (NILP (tail))
7b863bd5
JB
1785 return l2;
1786 Fsetcdr (tail, l2);
1787 return value;
1788 }
265a9e55 1789 if (NILP (l2))
7b863bd5
JB
1790 {
1791 UNGCPRO;
265a9e55 1792 if (NILP (tail))
7b863bd5
JB
1793 return l1;
1794 Fsetcdr (tail, l1);
1795 return value;
1796 }
1797 tem = call2 (pred, Fcar (l2), Fcar (l1));
265a9e55 1798 if (NILP (tem))
7b863bd5
JB
1799 {
1800 tem = l1;
1801 l1 = Fcdr (l1);
1802 org_l1 = l1;
1803 }
1804 else
1805 {
1806 tem = l2;
1807 l2 = Fcdr (l2);
1808 org_l2 = l2;
1809 }
265a9e55 1810 if (NILP (tail))
7b863bd5
JB
1811 value = tem;
1812 else
1813 Fsetcdr (tail, tem);
1814 tail = tem;
1815 }
1816}
be9d483d 1817
2d6fabfc 1818\f
12ae7fc6 1819/* This does not check for quits. That is safe since it must terminate. */
7b863bd5 1820
a7ca3326 1821DEFUN ("plist-get", Fplist_get, Splist_get, 2, 2, 0,
27f604dd
KS
1822 doc: /* Extract a value from a property list.
1823PLIST is a property list, which is a list of the form
1824\(PROP1 VALUE1 PROP2 VALUE2...). This function returns the value
12ae7fc6
KS
1825corresponding to the given PROP, or nil if PROP is not one of the
1826properties on the list. This function never signals an error. */)
5842a27b 1827 (Lisp_Object plist, Lisp_Object prop)
27f604dd
KS
1828{
1829 Lisp_Object tail, halftail;
1830
1831 /* halftail is used to detect circular lists. */
1832 tail = halftail = plist;
1833 while (CONSP (tail) && CONSP (XCDR (tail)))
1834 {
1835 if (EQ (prop, XCAR (tail)))
1836 return XCAR (XCDR (tail));
1837
1838 tail = XCDR (XCDR (tail));
1839 halftail = XCDR (halftail);
1840 if (EQ (tail, halftail))
1841 break;
1842 }
1843
1844 return Qnil;
1845}
1846
a7ca3326 1847DEFUN ("get", Fget, Sget, 2, 2, 0,
e9d8ddc9
MB
1848 doc: /* Return the value of SYMBOL's PROPNAME property.
1849This is the last value stored with `(put SYMBOL PROPNAME VALUE)'. */)
5842a27b 1850 (Lisp_Object symbol, Lisp_Object propname)
be9d483d 1851{
b7826503 1852 CHECK_SYMBOL (symbol);
c644523b 1853 return Fplist_get (XSYMBOL (symbol)->plist, propname);
be9d483d
BG
1854}
1855
a7ca3326 1856DEFUN ("plist-put", Fplist_put, Splist_put, 3, 3, 0,
e9d8ddc9 1857 doc: /* Change value in PLIST of PROP to VAL.
47cebab1
GM
1858PLIST is a property list, which is a list of the form
1859\(PROP1 VALUE1 PROP2 VALUE2 ...). PROP is a symbol and VAL is any object.
1860If PROP is already a property on the list, its value is set to VAL,
1861otherwise the new PROP VAL pair is added. The new plist is returned;
1862use `(setq x (plist-put x prop val))' to be sure to use the new value.
e9d8ddc9 1863The PLIST is modified by side effects. */)
5842a27b 1864 (Lisp_Object plist, register Lisp_Object prop, Lisp_Object val)
7b863bd5
JB
1865{
1866 register Lisp_Object tail, prev;
1867 Lisp_Object newcell;
1868 prev = Qnil;
70949dac
KR
1869 for (tail = plist; CONSP (tail) && CONSP (XCDR (tail));
1870 tail = XCDR (XCDR (tail)))
7b863bd5 1871 {
70949dac 1872 if (EQ (prop, XCAR (tail)))
be9d483d 1873 {
70949dac 1874 Fsetcar (XCDR (tail), val);
be9d483d
BG
1875 return plist;
1876 }
91f78c99 1877
7b863bd5 1878 prev = tail;
2d6fabfc 1879 QUIT;
7b863bd5 1880 }
088c8c37 1881 newcell = Fcons (prop, Fcons (val, NILP (prev) ? plist : XCDR (XCDR (prev))));
265a9e55 1882 if (NILP (prev))
be9d483d 1883 return newcell;
7b863bd5 1884 else
70949dac 1885 Fsetcdr (XCDR (prev), newcell);
be9d483d
BG
1886 return plist;
1887}
1888
a7ca3326 1889DEFUN ("put", Fput, Sput, 3, 3, 0,
e9d8ddc9
MB
1890 doc: /* Store SYMBOL's PROPNAME property with value VALUE.
1891It can be retrieved with `(get SYMBOL PROPNAME)'. */)
5842a27b 1892 (Lisp_Object symbol, Lisp_Object propname, Lisp_Object value)
be9d483d 1893{
b7826503 1894 CHECK_SYMBOL (symbol);
c644523b
DA
1895 set_symbol_plist
1896 (symbol, Fplist_put (XSYMBOL (symbol)->plist, propname, value));
c07289e0 1897 return value;
7b863bd5 1898}
aebf4d42
RS
1899\f
1900DEFUN ("lax-plist-get", Flax_plist_get, Slax_plist_get, 2, 2, 0,
1901 doc: /* Extract a value from a property list, comparing with `equal'.
1902PLIST is a property list, which is a list of the form
1903\(PROP1 VALUE1 PROP2 VALUE2...). This function returns the value
1904corresponding to the given PROP, or nil if PROP is not
1905one of the properties on the list. */)
5842a27b 1906 (Lisp_Object plist, Lisp_Object prop)
aebf4d42
RS
1907{
1908 Lisp_Object tail;
91f78c99 1909
aebf4d42
RS
1910 for (tail = plist;
1911 CONSP (tail) && CONSP (XCDR (tail));
1912 tail = XCDR (XCDR (tail)))
1913 {
1914 if (! NILP (Fequal (prop, XCAR (tail))))
1915 return XCAR (XCDR (tail));
1916
1917 QUIT;
1918 }
1919
89662fc3 1920 CHECK_LIST_END (tail, prop);
91f78c99 1921
aebf4d42
RS
1922 return Qnil;
1923}
7b863bd5 1924
aebf4d42
RS
1925DEFUN ("lax-plist-put", Flax_plist_put, Slax_plist_put, 3, 3, 0,
1926 doc: /* Change value in PLIST of PROP to VAL, comparing with `equal'.
1927PLIST is a property list, which is a list of the form
9e76ae05 1928\(PROP1 VALUE1 PROP2 VALUE2 ...). PROP and VAL are any objects.
aebf4d42
RS
1929If PROP is already a property on the list, its value is set to VAL,
1930otherwise the new PROP VAL pair is added. The new plist is returned;
1931use `(setq x (lax-plist-put x prop val))' to be sure to use the new value.
1932The PLIST is modified by side effects. */)
5842a27b 1933 (Lisp_Object plist, register Lisp_Object prop, Lisp_Object val)
aebf4d42
RS
1934{
1935 register Lisp_Object tail, prev;
1936 Lisp_Object newcell;
1937 prev = Qnil;
1938 for (tail = plist; CONSP (tail) && CONSP (XCDR (tail));
1939 tail = XCDR (XCDR (tail)))
1940 {
1941 if (! NILP (Fequal (prop, XCAR (tail))))
1942 {
1943 Fsetcar (XCDR (tail), val);
1944 return plist;
1945 }
91f78c99 1946
aebf4d42
RS
1947 prev = tail;
1948 QUIT;
1949 }
6c6f1994 1950 newcell = list2 (prop, val);
aebf4d42
RS
1951 if (NILP (prev))
1952 return newcell;
1953 else
1954 Fsetcdr (XCDR (prev), newcell);
1955 return plist;
1956}
1957\f
95f8c3b9
JPW
1958DEFUN ("eql", Feql, Seql, 2, 2, 0,
1959 doc: /* Return t if the two args are the same Lisp object.
1960Floating-point numbers of equal value are `eql', but they may not be `eq'. */)
5842a27b 1961 (Lisp_Object obj1, Lisp_Object obj2)
95f8c3b9
JPW
1962{
1963 if (FLOATP (obj1))
1964 return internal_equal (obj1, obj2, 0, 0) ? Qt : Qnil;
1965 else
1966 return EQ (obj1, obj2) ? Qt : Qnil;
1967}
1968
a7ca3326 1969DEFUN ("equal", Fequal, Sequal, 2, 2, 0,
e9d8ddc9 1970 doc: /* Return t if two Lisp objects have similar structure and contents.
47cebab1
GM
1971They must have the same data type.
1972Conses are compared by comparing the cars and the cdrs.
1973Vectors and strings are compared element by element.
1974Numbers are compared by value, but integers cannot equal floats.
1975 (Use `=' if you want integers and floats to be able to be equal.)
e9d8ddc9 1976Symbols must match exactly. */)
5842a27b 1977 (register Lisp_Object o1, Lisp_Object o2)
7b863bd5 1978{
6b61353c
KH
1979 return internal_equal (o1, o2, 0, 0) ? Qt : Qnil;
1980}
1981
1982DEFUN ("equal-including-properties", Fequal_including_properties, Sequal_including_properties, 2, 2, 0,
1983 doc: /* Return t if two Lisp objects have similar structure and contents.
1984This is like `equal' except that it compares the text properties
1985of strings. (`equal' ignores text properties.) */)
5842a27b 1986 (register Lisp_Object o1, Lisp_Object o2)
6b61353c
KH
1987{
1988 return internal_equal (o1, o2, 0, 1) ? Qt : Qnil;
e0f5cf5a
RS
1989}
1990
6b61353c
KH
1991/* DEPTH is current depth of recursion. Signal an error if it
1992 gets too deep.
f75d7a91 1993 PROPS means compare string text properties too. */
6b61353c 1994
f75d7a91
PE
1995static bool
1996internal_equal (Lisp_Object o1, Lisp_Object o2, int depth, bool props)
e0f5cf5a
RS
1997{
1998 if (depth > 200)
1999 error ("Stack overflow in equal");
4ff1aed9 2000
6cb9cafb 2001 tail_recurse:
7b863bd5 2002 QUIT;
4ff1aed9
RS
2003 if (EQ (o1, o2))
2004 return 1;
2005 if (XTYPE (o1) != XTYPE (o2))
2006 return 0;
2007
2008 switch (XTYPE (o1))
2009 {
4ff1aed9 2010 case Lisp_Float:
6b61353c
KH
2011 {
2012 double d1, d2;
2013
2014 d1 = extract_float (o1);
2015 d2 = extract_float (o2);
2016 /* If d is a NaN, then d != d. Two NaNs should be `equal' even
b7432bb2 2017 though they are not =. */
6b61353c
KH
2018 return d1 == d2 || (d1 != d1 && d2 != d2);
2019 }
4ff1aed9
RS
2020
2021 case Lisp_Cons:
6b61353c 2022 if (!internal_equal (XCAR (o1), XCAR (o2), depth + 1, props))
4cab5074 2023 return 0;
70949dac
KR
2024 o1 = XCDR (o1);
2025 o2 = XCDR (o2);
4cab5074 2026 goto tail_recurse;
4ff1aed9
RS
2027
2028 case Lisp_Misc:
81d1fba6 2029 if (XMISCTYPE (o1) != XMISCTYPE (o2))
6cb9cafb 2030 return 0;
4ff1aed9 2031 if (OVERLAYP (o1))
7b863bd5 2032 {
e23f814f 2033 if (!internal_equal (OVERLAY_START (o1), OVERLAY_START (o2),
6b61353c 2034 depth + 1, props)
e23f814f 2035 || !internal_equal (OVERLAY_END (o1), OVERLAY_END (o2),
43f15d4a 2036 depth + 1, props))
6cb9cafb 2037 return 0;
c644523b
DA
2038 o1 = XOVERLAY (o1)->plist;
2039 o2 = XOVERLAY (o2)->plist;
4ff1aed9 2040 goto tail_recurse;
7b863bd5 2041 }
4ff1aed9
RS
2042 if (MARKERP (o1))
2043 {
2044 return (XMARKER (o1)->buffer == XMARKER (o2)->buffer
2045 && (XMARKER (o1)->buffer == 0
6ced1284 2046 || XMARKER (o1)->bytepos == XMARKER (o2)->bytepos));
4ff1aed9
RS
2047 }
2048 break;
2049
2050 case Lisp_Vectorlike:
4cab5074 2051 {
6b61353c 2052 register int i;
d311d28c 2053 ptrdiff_t size = ASIZE (o1);
4cab5074
KH
2054 /* Pseudovectors have the type encoded in the size field, so this test
2055 actually checks that the objects have the same type as well as the
2056 same size. */
7edbb0da 2057 if (ASIZE (o2) != size)
4cab5074 2058 return 0;
e03f7933
RS
2059 /* Boolvectors are compared much like strings. */
2060 if (BOOL_VECTOR_P (o1))
2061 {
1c0a7493
PE
2062 EMACS_INT size = bool_vector_size (o1);
2063 if (size != bool_vector_size (o2))
e03f7933 2064 return 0;
df5b4930 2065 if (memcmp (bool_vector_data (o1), bool_vector_data (o2),
2cf00efc 2066 bool_vector_bytes (size)))
e03f7933
RS
2067 return 0;
2068 return 1;
2069 }
ed73fcc1 2070 if (WINDOW_CONFIGURATIONP (o1))
48646924 2071 return compare_window_configurations (o1, o2, 0);
e03f7933 2072
876c194c 2073 /* Aside from them, only true vectors, char-tables, compiled
66699ad3 2074 functions, and fonts (font-spec, font-entity, font-object)
876c194c 2075 are sensible to compare, so eliminate the others now. */
4cab5074
KH
2076 if (size & PSEUDOVECTOR_FLAG)
2077 {
914adc42
DA
2078 if (((size & PVEC_TYPE_MASK) >> PSEUDOVECTOR_AREA_BITS)
2079 < PVEC_COMPILED)
4cab5074
KH
2080 return 0;
2081 size &= PSEUDOVECTOR_SIZE_MASK;
2082 }
2083 for (i = 0; i < size; i++)
2084 {
2085 Lisp_Object v1, v2;
7edbb0da
SM
2086 v1 = AREF (o1, i);
2087 v2 = AREF (o2, i);
6b61353c 2088 if (!internal_equal (v1, v2, depth + 1, props))
4cab5074
KH
2089 return 0;
2090 }
2091 return 1;
2092 }
4ff1aed9
RS
2093 break;
2094
2095 case Lisp_String:
d5db4077 2096 if (SCHARS (o1) != SCHARS (o2))
4cab5074 2097 return 0;
d5db4077 2098 if (SBYTES (o1) != SBYTES (o2))
ea35ce3d 2099 return 0;
72af86bd 2100 if (memcmp (SDATA (o1), SDATA (o2), SBYTES (o1)))
4cab5074 2101 return 0;
6b61353c
KH
2102 if (props && !compare_string_intervals (o1, o2))
2103 return 0;
4cab5074 2104 return 1;
093386ca 2105
2de9f71c 2106 default:
093386ca 2107 break;
7b863bd5 2108 }
91f78c99 2109
6cb9cafb 2110 return 0;
7b863bd5
JB
2111}
2112\f
2e34157c 2113
7b863bd5 2114DEFUN ("fillarray", Ffillarray, Sfillarray, 2, 2, 0,
e9d8ddc9
MB
2115 doc: /* Store each element of ARRAY with ITEM.
2116ARRAY is a vector, string, char-table, or bool-vector. */)
5842a27b 2117 (Lisp_Object array, Lisp_Object item)
7b863bd5 2118{
d311d28c 2119 register ptrdiff_t size, idx;
e6d4aefa 2120
7650760e 2121 if (VECTORP (array))
086ca913
DA
2122 for (idx = 0, size = ASIZE (array); idx < size; idx++)
2123 ASET (array, idx, item);
e03f7933
RS
2124 else if (CHAR_TABLE_P (array))
2125 {
38583a69
KH
2126 int i;
2127
2128 for (i = 0; i < (1 << CHARTAB_SIZE_BITS_0); i++)
34dabdb7 2129 set_char_table_contents (array, i, item);
742af32f 2130 set_char_table_defalt (array, item);
e03f7933 2131 }
7650760e 2132 else if (STRINGP (array))
7b863bd5 2133 {
d5db4077 2134 register unsigned char *p = SDATA (array);
a4cf38e4
PE
2135 int charval;
2136 CHECK_CHARACTER (item);
2137 charval = XFASTINT (item);
d5db4077 2138 size = SCHARS (array);
57247650
KH
2139 if (STRING_MULTIBYTE (array))
2140 {
64a5094a
KH
2141 unsigned char str[MAX_MULTIBYTE_LENGTH];
2142 int len = CHAR_STRING (charval, str);
d311d28c 2143 ptrdiff_t size_byte = SBYTES (array);
57247650 2144
f03dc6ef
PE
2145 if (INT_MULTIPLY_OVERFLOW (SCHARS (array), len)
2146 || SCHARS (array) * len != size_byte)
2147 error ("Attempt to change byte length of a string");
436b4815
PE
2148 for (idx = 0; idx < size_byte; idx++)
2149 *p++ = str[idx % len];
57247650
KH
2150 }
2151 else
612f56df
PE
2152 for (idx = 0; idx < size; idx++)
2153 p[idx] = charval;
7b863bd5 2154 }
e03f7933 2155 else if (BOOL_VECTOR_P (array))
2cf00efc 2156 return bool_vector_fill (array, item);
7b863bd5 2157 else
89662fc3 2158 wrong_type_argument (Qarrayp, array);
7b863bd5
JB
2159 return array;
2160}
85cad579
RS
2161
2162DEFUN ("clear-string", Fclear_string, Sclear_string,
2163 1, 1, 0,
2164 doc: /* Clear the contents of STRING.
2165This makes STRING unibyte and may change its length. */)
5842a27b 2166 (Lisp_Object string)
85cad579 2167{
d311d28c 2168 ptrdiff_t len;
a085bf9d 2169 CHECK_STRING (string);
cfd23693 2170 len = SBYTES (string);
72af86bd 2171 memset (SDATA (string), 0, len);
85cad579
RS
2172 STRING_SET_CHARS (string, len);
2173 STRING_SET_UNIBYTE (string);
2174 return Qnil;
2175}
ea35ce3d 2176\f
7b863bd5
JB
2177/* ARGSUSED */
2178Lisp_Object
971de7fb 2179nconc2 (Lisp_Object s1, Lisp_Object s2)
7b863bd5 2180{
7b863bd5
JB
2181 Lisp_Object args[2];
2182 args[0] = s1;
2183 args[1] = s2;
2184 return Fnconc (2, args);
7b863bd5
JB
2185}
2186
a7ca3326 2187DEFUN ("nconc", Fnconc, Snconc, 0, MANY, 0,
e9d8ddc9 2188 doc: /* Concatenate any number of lists by altering them.
4bf8e2a3
MB
2189Only the last argument is not altered, and need not be a list.
2190usage: (nconc &rest LISTS) */)
f66c7cf8 2191 (ptrdiff_t nargs, Lisp_Object *args)
7b863bd5 2192{
f66c7cf8 2193 ptrdiff_t argnum;
7b863bd5
JB
2194 register Lisp_Object tail, tem, val;
2195
093386ca 2196 val = tail = Qnil;
7b863bd5
JB
2197
2198 for (argnum = 0; argnum < nargs; argnum++)
2199 {
2200 tem = args[argnum];
265a9e55 2201 if (NILP (tem)) continue;
7b863bd5 2202
265a9e55 2203 if (NILP (val))
7b863bd5
JB
2204 val = tem;
2205
2206 if (argnum + 1 == nargs) break;
2207
89662fc3 2208 CHECK_LIST_CONS (tem, tem);
7b863bd5
JB
2209
2210 while (CONSP (tem))
2211 {
2212 tail = tem;
cf42cb72 2213 tem = XCDR (tail);
7b863bd5
JB
2214 QUIT;
2215 }
2216
2217 tem = args[argnum + 1];
2218 Fsetcdr (tail, tem);
265a9e55 2219 if (NILP (tem))
7b863bd5
JB
2220 args[argnum + 1] = tail;
2221 }
2222
2223 return val;
2224}
2225\f
2226/* This is the guts of all mapping functions.
ea35ce3d
RS
2227 Apply FN to each element of SEQ, one by one,
2228 storing the results into elements of VALS, a C vector of Lisp_Objects.
2229 LENI is the length of VALS, which should also be the length of SEQ. */
7b863bd5
JB
2230
2231static void
e6d4aefa 2232mapcar1 (EMACS_INT leni, Lisp_Object *vals, Lisp_Object fn, Lisp_Object seq)
7b863bd5
JB
2233{
2234 register Lisp_Object tail;
2235 Lisp_Object dummy;
e6d4aefa 2236 register EMACS_INT i;
7b863bd5
JB
2237 struct gcpro gcpro1, gcpro2, gcpro3;
2238
f5c75033
DL
2239 if (vals)
2240 {
2241 /* Don't let vals contain any garbage when GC happens. */
2242 for (i = 0; i < leni; i++)
2243 vals[i] = Qnil;
7b863bd5 2244
f5c75033
DL
2245 GCPRO3 (dummy, fn, seq);
2246 gcpro1.var = vals;
2247 gcpro1.nvars = leni;
2248 }
2249 else
2250 GCPRO2 (fn, seq);
7b863bd5 2251 /* We need not explicitly protect `tail' because it is used only on lists, and
7edbb0da
SM
2252 1) lists are not relocated and 2) the list is marked via `seq' so will not
2253 be freed */
7b863bd5 2254
876c194c 2255 if (VECTORP (seq) || COMPILEDP (seq))
7b863bd5
JB
2256 {
2257 for (i = 0; i < leni; i++)
2258 {
7edbb0da 2259 dummy = call1 (fn, AREF (seq, i));
f5c75033
DL
2260 if (vals)
2261 vals[i] = dummy;
7b863bd5
JB
2262 }
2263 }
33aa0881
KH
2264 else if (BOOL_VECTOR_P (seq))
2265 {
2266 for (i = 0; i < leni; i++)
2267 {
df5b4930 2268 dummy = call1 (fn, bool_vector_ref (seq, i));
f5c75033
DL
2269 if (vals)
2270 vals[i] = dummy;
33aa0881
KH
2271 }
2272 }
ea35ce3d
RS
2273 else if (STRINGP (seq))
2274 {
d311d28c 2275 ptrdiff_t i_byte;
ea35ce3d
RS
2276
2277 for (i = 0, i_byte = 0; i < leni;)
2278 {
2279 int c;
d311d28c 2280 ptrdiff_t i_before = i;
0ab6a3d8
KH
2281
2282 FETCH_STRING_CHAR_ADVANCE (c, seq, i, i_byte);
ea35ce3d 2283 XSETFASTINT (dummy, c);
f5c75033
DL
2284 dummy = call1 (fn, dummy);
2285 if (vals)
2286 vals[i_before] = dummy;
ea35ce3d
RS
2287 }
2288 }
7b863bd5
JB
2289 else /* Must be a list, since Flength did not get an error */
2290 {
2291 tail = seq;
85946364 2292 for (i = 0; i < leni && CONSP (tail); i++)
7b863bd5 2293 {
85946364 2294 dummy = call1 (fn, XCAR (tail));
f5c75033
DL
2295 if (vals)
2296 vals[i] = dummy;
70949dac 2297 tail = XCDR (tail);
7b863bd5
JB
2298 }
2299 }
2300
2301 UNGCPRO;
2302}
2303
a7ca3326 2304DEFUN ("mapconcat", Fmapconcat, Smapconcat, 3, 3, 0,
e9d8ddc9 2305 doc: /* Apply FUNCTION to each element of SEQUENCE, and concat the results as strings.
dd8d1e71 2306In between each pair of results, stick in SEPARATOR. Thus, " " as
47cebab1 2307SEPARATOR results in spaces between the values returned by FUNCTION.
e9d8ddc9 2308SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
5842a27b 2309 (Lisp_Object function, Lisp_Object sequence, Lisp_Object separator)
7b863bd5
JB
2310{
2311 Lisp_Object len;
e6d4aefa 2312 register EMACS_INT leni;
d311d28c
PE
2313 EMACS_INT nargs;
2314 ptrdiff_t i;
7b863bd5 2315 register Lisp_Object *args;
7b863bd5 2316 struct gcpro gcpro1;
799c08ac
KS
2317 Lisp_Object ret;
2318 USE_SAFE_ALLOCA;
7b863bd5 2319
88fe8140 2320 len = Flength (sequence);
4187aa82
KH
2321 if (CHAR_TABLE_P (sequence))
2322 wrong_type_argument (Qlistp, sequence);
7b863bd5
JB
2323 leni = XINT (len);
2324 nargs = leni + leni - 1;
b116683c 2325 if (nargs < 0) return empty_unibyte_string;
7b863bd5 2326
7b4cd44a 2327 SAFE_ALLOCA_LISP (args, nargs);
7b863bd5 2328
88fe8140
EN
2329 GCPRO1 (separator);
2330 mapcar1 (leni, args, function, sequence);
7b863bd5
JB
2331 UNGCPRO;
2332
85946364 2333 for (i = leni - 1; i > 0; i--)
7b863bd5 2334 args[i + i] = args[i];
b4f334f7 2335
7b863bd5 2336 for (i = 1; i < nargs; i += 2)
88fe8140 2337 args[i] = separator;
7b863bd5 2338
799c08ac 2339 ret = Fconcat (nargs, args);
233f3db6 2340 SAFE_FREE ();
799c08ac
KS
2341
2342 return ret;
7b863bd5
JB
2343}
2344
a7ca3326 2345DEFUN ("mapcar", Fmapcar, Smapcar, 2, 2, 0,
e9d8ddc9 2346 doc: /* Apply FUNCTION to each element of SEQUENCE, and make a list of the results.
47cebab1 2347The result is a list just as long as SEQUENCE.
e9d8ddc9 2348SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
5842a27b 2349 (Lisp_Object function, Lisp_Object sequence)
7b863bd5
JB
2350{
2351 register Lisp_Object len;
e6d4aefa 2352 register EMACS_INT leni;
7b863bd5 2353 register Lisp_Object *args;
799c08ac
KS
2354 Lisp_Object ret;
2355 USE_SAFE_ALLOCA;
7b863bd5 2356
88fe8140 2357 len = Flength (sequence);
4187aa82
KH
2358 if (CHAR_TABLE_P (sequence))
2359 wrong_type_argument (Qlistp, sequence);
7b863bd5 2360 leni = XFASTINT (len);
799c08ac 2361
7b4cd44a 2362 SAFE_ALLOCA_LISP (args, leni);
7b863bd5 2363
88fe8140 2364 mapcar1 (leni, args, function, sequence);
7b863bd5 2365
799c08ac 2366 ret = Flist (leni, args);
233f3db6 2367 SAFE_FREE ();
799c08ac
KS
2368
2369 return ret;
7b863bd5 2370}
f5c75033
DL
2371
2372DEFUN ("mapc", Fmapc, Smapc, 2, 2, 0,
e9d8ddc9 2373 doc: /* Apply FUNCTION to each element of SEQUENCE for side effects only.
47cebab1 2374Unlike `mapcar', don't accumulate the results. Return SEQUENCE.
e9d8ddc9 2375SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
5842a27b 2376 (Lisp_Object function, Lisp_Object sequence)
f5c75033 2377{
e6d4aefa 2378 register EMACS_INT leni;
f5c75033
DL
2379
2380 leni = XFASTINT (Flength (sequence));
4187aa82
KH
2381 if (CHAR_TABLE_P (sequence))
2382 wrong_type_argument (Qlistp, sequence);
f5c75033
DL
2383 mapcar1 (leni, 0, function, sequence);
2384
2385 return sequence;
2386}
7b863bd5 2387\f
7b863bd5
JB
2388/* This is how C code calls `yes-or-no-p' and allows the user
2389 to redefined it.
2390
2391 Anything that calls this function must protect from GC! */
2392
2393Lisp_Object
971de7fb 2394do_yes_or_no_p (Lisp_Object prompt)
7b863bd5
JB
2395{
2396 return call1 (intern ("yes-or-no-p"), prompt);
2397}
2398
2399/* Anything that calls this function must protect from GC! */
2400
2401DEFUN ("yes-or-no-p", Fyes_or_no_p, Syes_or_no_p, 1, 1, 0,
e9d8ddc9 2402 doc: /* Ask user a yes-or-no question. Return t if answer is yes.
9aea757b
CY
2403PROMPT is the string to display to ask the question. It should end in
2404a space; `yes-or-no-p' adds \"(yes or no) \" to it.
3d91e302
CY
2405
2406The user must confirm the answer with RET, and can edit it until it
2407has been confirmed.
47cebab1 2408
9f8551de
EZ
2409If dialog boxes are supported, a dialog box will be used
2410if `last-nonmenu-event' is nil, and `use-dialog-box' is non-nil. */)
5842a27b 2411 (Lisp_Object prompt)
7b863bd5
JB
2412{
2413 register Lisp_Object ans;
2414 Lisp_Object args[2];
2415 struct gcpro gcpro1;
2416
b7826503 2417 CHECK_STRING (prompt);
7b863bd5 2418
0ef68e8a 2419#ifdef HAVE_MENUS
7452b7bd 2420 if ((NILP (last_nonmenu_event) || CONSP (last_nonmenu_event))
9f8551de 2421 && use_dialog_box)
1db4cfb2
RS
2422 {
2423 Lisp_Object pane, menu, obj;
3007ebfb 2424 redisplay_preserve_echo_area (4);
6c6f1994
PE
2425 pane = list2 (Fcons (build_string ("Yes"), Qt),
2426 Fcons (build_string ("No"), Qnil));
1db4cfb2 2427 GCPRO1 (pane);
ec26e1b9 2428 menu = Fcons (prompt, pane);
f0a31d70 2429 obj = Fx_popup_dialog (Qt, menu, Qnil);
1db4cfb2
RS
2430 UNGCPRO;
2431 return obj;
2432 }
0ef68e8a 2433#endif /* HAVE_MENUS */
1db4cfb2 2434
7b863bd5
JB
2435 args[0] = prompt;
2436 args[1] = build_string ("(yes or no) ");
2437 prompt = Fconcat (2, args);
2438
2439 GCPRO1 (prompt);
1db4cfb2 2440
7b863bd5
JB
2441 while (1)
2442 {
0ce830bc 2443 ans = Fdowncase (Fread_from_minibuffer (prompt, Qnil, Qnil, Qnil,
b24014d4 2444 Qyes_or_no_p_history, Qnil,
ba139299 2445 Qnil));
42a5b22f 2446 if (SCHARS (ans) == 3 && !strcmp (SSDATA (ans), "yes"))
7b863bd5
JB
2447 {
2448 UNGCPRO;
2449 return Qt;
2450 }
42a5b22f 2451 if (SCHARS (ans) == 2 && !strcmp (SSDATA (ans), "no"))
7b863bd5
JB
2452 {
2453 UNGCPRO;
2454 return Qnil;
2455 }
2456
2457 Fding (Qnil);
2458 Fdiscard_input ();
2f73da9c 2459 message1 ("Please answer yes or no.");
99dc4745 2460 Fsleep_for (make_number (2), Qnil);
7b863bd5 2461 }
7b863bd5
JB
2462}
2463\f
f4b50f66 2464DEFUN ("load-average", Fload_average, Sload_average, 0, 1, 0,
e9d8ddc9 2465 doc: /* Return list of 1 minute, 5 minute and 15 minute load averages.
91f78c99 2466
47cebab1
GM
2467Each of the three load averages is multiplied by 100, then converted
2468to integer.
2469
2470When USE-FLOATS is non-nil, floats will be used instead of integers.
2471These floats are not multiplied by 100.
2472
2473If the 5-minute or 15-minute load averages are not available, return a
30b1b0cf
DL
2474shortened list, containing only those averages which are available.
2475
2476An error is thrown if the load average can't be obtained. In some
2477cases making it work would require Emacs being installed setuid or
2478setgid so that it can read kernel information, and that usually isn't
2479advisable. */)
5842a27b 2480 (Lisp_Object use_floats)
7b863bd5 2481{
daa37602
JB
2482 double load_ave[3];
2483 int loads = getloadavg (load_ave, 3);
f4b50f66 2484 Lisp_Object ret = Qnil;
7b863bd5 2485
daa37602
JB
2486 if (loads < 0)
2487 error ("load-average not implemented for this operating system");
2488
f4b50f66
RS
2489 while (loads-- > 0)
2490 {
566684ea
PE
2491 Lisp_Object load = (NILP (use_floats)
2492 ? make_number (100.0 * load_ave[loads])
f4b50f66
RS
2493 : make_float (load_ave[loads]));
2494 ret = Fcons (load, ret);
2495 }
daa37602
JB
2496
2497 return ret;
2498}
7b863bd5 2499\f
955cbe7b 2500static Lisp_Object Qsubfeatures;
7b863bd5 2501
65550192 2502DEFUN ("featurep", Ffeaturep, Sfeaturep, 1, 2, 0,
b756c005 2503 doc: /* Return t if FEATURE is present in this Emacs.
91f78c99 2504
47cebab1 2505Use this to conditionalize execution of lisp code based on the
4774b68e 2506presence or absence of Emacs or environment extensions.
47cebab1
GM
2507Use `provide' to declare that a feature is available. This function
2508looks at the value of the variable `features'. The optional argument
e9d8ddc9 2509SUBFEATURE can be used to check a specific subfeature of FEATURE. */)
5842a27b 2510 (Lisp_Object feature, Lisp_Object subfeature)
7b863bd5
JB
2511{
2512 register Lisp_Object tem;
b7826503 2513 CHECK_SYMBOL (feature);
7b863bd5 2514 tem = Fmemq (feature, Vfeatures);
65550192 2515 if (!NILP (tem) && !NILP (subfeature))
37ebddef 2516 tem = Fmember (subfeature, Fget (feature, Qsubfeatures));
265a9e55 2517 return (NILP (tem)) ? Qnil : Qt;
7b863bd5
JB
2518}
2519
de0503df
SM
2520static Lisp_Object Qfuncall;
2521
a7ca3326 2522DEFUN ("provide", Fprovide, Sprovide, 1, 2, 0,
e9d8ddc9 2523 doc: /* Announce that FEATURE is a feature of the current Emacs.
47cebab1 2524The optional argument SUBFEATURES should be a list of symbols listing
e9d8ddc9 2525particular subfeatures supported in this version of FEATURE. */)
5842a27b 2526 (Lisp_Object feature, Lisp_Object subfeatures)
7b863bd5
JB
2527{
2528 register Lisp_Object tem;
b7826503 2529 CHECK_SYMBOL (feature);
37ebddef 2530 CHECK_LIST (subfeatures);
265a9e55 2531 if (!NILP (Vautoload_queue))
989e66e1
RS
2532 Vautoload_queue = Fcons (Fcons (make_number (0), Vfeatures),
2533 Vautoload_queue);
7b863bd5 2534 tem = Fmemq (feature, Vfeatures);
265a9e55 2535 if (NILP (tem))
7b863bd5 2536 Vfeatures = Fcons (feature, Vfeatures);
65550192
SM
2537 if (!NILP (subfeatures))
2538 Fput (feature, Qsubfeatures, subfeatures);
68732608 2539 LOADHIST_ATTACH (Fcons (Qprovide, feature));
65550192
SM
2540
2541 /* Run any load-hooks for this file. */
2542 tem = Fassq (feature, Vafter_load_alist);
cf42cb72 2543 if (CONSP (tem))
de0503df 2544 Fmapc (Qfuncall, XCDR (tem));
65550192 2545
7b863bd5
JB
2546 return feature;
2547}
1f79789d
RS
2548\f
2549/* `require' and its subroutines. */
2550
2551/* List of features currently being require'd, innermost first. */
2552
2a80c887 2553static Lisp_Object require_nesting_list;
1f79789d 2554
27e498e6 2555static void
971de7fb 2556require_unwind (Lisp_Object old_value)
1f79789d 2557{
27e498e6 2558 require_nesting_list = old_value;
1f79789d 2559}
7b863bd5 2560
53d5acf5 2561DEFUN ("require", Frequire, Srequire, 1, 3, 0,
e9d8ddc9 2562 doc: /* If feature FEATURE is not loaded, load it from FILENAME.
47cebab1
GM
2563If FEATURE is not a member of the list `features', then the feature
2564is not loaded; so load the file FILENAME.
2565If FILENAME is omitted, the printname of FEATURE is used as the file name,
6b61353c
KH
2566and `load' will try to load this name appended with the suffix `.elc' or
2567`.el', in that order. The name without appended suffix will not be used.
90186c68 2568See `get-load-suffixes' for the complete list of suffixes.
47cebab1
GM
2569If the optional third argument NOERROR is non-nil,
2570then return nil if the file is not found instead of signaling an error.
2571Normally the return value is FEATURE.
e9d8ddc9 2572The normal messages at start and end of loading FILENAME are suppressed. */)
5842a27b 2573 (Lisp_Object feature, Lisp_Object filename, Lisp_Object noerror)
7b863bd5 2574{
f75d7a91 2575 Lisp_Object tem;
1f79789d 2576 struct gcpro gcpro1, gcpro2;
f75d7a91 2577 bool from_file = load_in_progress;
1f79789d 2578
b7826503 2579 CHECK_SYMBOL (feature);
1f79789d 2580
5ba8f83d 2581 /* Record the presence of `require' in this file
9d5c2e7e
RS
2582 even if the feature specified is already loaded.
2583 But not more than once in any file,
06100606
RS
2584 and not when we aren't loading or reading from a file. */
2585 if (!from_file)
2586 for (tem = Vcurrent_load_list; CONSP (tem); tem = XCDR (tem))
2587 if (NILP (XCDR (tem)) && STRINGP (XCAR (tem)))
2588 from_file = 1;
2589
2590 if (from_file)
9d5c2e7e
RS
2591 {
2592 tem = Fcons (Qrequire, feature);
2593 if (NILP (Fmember (tem, Vcurrent_load_list)))
2594 LOADHIST_ATTACH (tem);
2595 }
7b863bd5 2596 tem = Fmemq (feature, Vfeatures);
91f78c99 2597
265a9e55 2598 if (NILP (tem))
7b863bd5 2599 {
d311d28c 2600 ptrdiff_t count = SPECPDL_INDEX ();
1f79789d 2601 int nesting = 0;
bcb31b2a 2602
aea6173f
RS
2603 /* This is to make sure that loadup.el gives a clear picture
2604 of what files are preloaded and when. */
bcb31b2a
RS
2605 if (! NILP (Vpurify_flag))
2606 error ("(require %s) while preparing to dump",
d5db4077 2607 SDATA (SYMBOL_NAME (feature)));
91f78c99 2608
1f79789d
RS
2609 /* A certain amount of recursive `require' is legitimate,
2610 but if we require the same feature recursively 3 times,
2611 signal an error. */
2612 tem = require_nesting_list;
2613 while (! NILP (tem))
2614 {
2615 if (! NILP (Fequal (feature, XCAR (tem))))
2616 nesting++;
2617 tem = XCDR (tem);
2618 }
f707342d 2619 if (nesting > 3)
1f79789d 2620 error ("Recursive `require' for feature `%s'",
d5db4077 2621 SDATA (SYMBOL_NAME (feature)));
1f79789d
RS
2622
2623 /* Update the list for any nested `require's that occur. */
2624 record_unwind_protect (require_unwind, require_nesting_list);
2625 require_nesting_list = Fcons (feature, require_nesting_list);
7b863bd5
JB
2626
2627 /* Value saved here is to be restored into Vautoload_queue */
2628 record_unwind_protect (un_autoload, Vautoload_queue);
2629 Vautoload_queue = Qt;
2630
1f79789d
RS
2631 /* Load the file. */
2632 GCPRO2 (feature, filename);
81a81c0f
GM
2633 tem = Fload (NILP (filename) ? Fsymbol_name (feature) : filename,
2634 noerror, Qt, Qnil, (NILP (filename) ? Qt : Qnil));
1f79789d
RS
2635 UNGCPRO;
2636
53d5acf5
RS
2637 /* If load failed entirely, return nil. */
2638 if (NILP (tem))
41857307 2639 return unbind_to (count, Qnil);
7b863bd5
JB
2640
2641 tem = Fmemq (feature, Vfeatures);
265a9e55 2642 if (NILP (tem))
1f79789d 2643 error ("Required feature `%s' was not provided",
d5db4077 2644 SDATA (SYMBOL_NAME (feature)));
7b863bd5
JB
2645
2646 /* Once loading finishes, don't undo it. */
2647 Vautoload_queue = Qt;
2648 feature = unbind_to (count, feature);
2649 }
1f79789d 2650
7b863bd5
JB
2651 return feature;
2652}
2653\f
b4f334f7
KH
2654/* Primitives for work of the "widget" library.
2655 In an ideal world, this section would not have been necessary.
2656 However, lisp function calls being as slow as they are, it turns
2657 out that some functions in the widget library (wid-edit.el) are the
2658 bottleneck of Widget operation. Here is their translation to C,
2659 for the sole reason of efficiency. */
2660
a7ca3326 2661DEFUN ("plist-member", Fplist_member, Splist_member, 2, 2, 0,
e9d8ddc9 2662 doc: /* Return non-nil if PLIST has the property PROP.
47cebab1
GM
2663PLIST is a property list, which is a list of the form
2664\(PROP1 VALUE1 PROP2 VALUE2 ...\). PROP is a symbol.
2665Unlike `plist-get', this allows you to distinguish between a missing
2666property and a property with the value nil.
e9d8ddc9 2667The value is actually the tail of PLIST whose car is PROP. */)
5842a27b 2668 (Lisp_Object plist, Lisp_Object prop)
b4f334f7
KH
2669{
2670 while (CONSP (plist) && !EQ (XCAR (plist), prop))
2671 {
2672 QUIT;
2673 plist = XCDR (plist);
2674 plist = CDR (plist);
2675 }
2676 return plist;
2677}
2678
2679DEFUN ("widget-put", Fwidget_put, Swidget_put, 3, 3, 0,
e9d8ddc9
MB
2680 doc: /* In WIDGET, set PROPERTY to VALUE.
2681The value can later be retrieved with `widget-get'. */)
5842a27b 2682 (Lisp_Object widget, Lisp_Object property, Lisp_Object value)
b4f334f7 2683{
b7826503 2684 CHECK_CONS (widget);
f3fbd155 2685 XSETCDR (widget, Fplist_put (XCDR (widget), property, value));
f7993597 2686 return value;
b4f334f7
KH
2687}
2688
2689DEFUN ("widget-get", Fwidget_get, Swidget_get, 2, 2, 0,
e9d8ddc9 2690 doc: /* In WIDGET, get the value of PROPERTY.
47cebab1 2691The value could either be specified when the widget was created, or
e9d8ddc9 2692later with `widget-put'. */)
5842a27b 2693 (Lisp_Object widget, Lisp_Object property)
b4f334f7
KH
2694{
2695 Lisp_Object tmp;
2696
2697 while (1)
2698 {
2699 if (NILP (widget))
2700 return Qnil;
b7826503 2701 CHECK_CONS (widget);
a5254817 2702 tmp = Fplist_member (XCDR (widget), property);
b4f334f7
KH
2703 if (CONSP (tmp))
2704 {
2705 tmp = XCDR (tmp);
2706 return CAR (tmp);
2707 }
2708 tmp = XCAR (widget);
2709 if (NILP (tmp))
2710 return Qnil;
2711 widget = Fget (tmp, Qwidget_type);
2712 }
2713}
2714
2715DEFUN ("widget-apply", Fwidget_apply, Swidget_apply, 2, MANY, 0,
e9d8ddc9 2716 doc: /* Apply the value of WIDGET's PROPERTY to the widget itself.
4bf8e2a3
MB
2717ARGS are passed as extra arguments to the function.
2718usage: (widget-apply WIDGET PROPERTY &rest ARGS) */)
f66c7cf8 2719 (ptrdiff_t nargs, Lisp_Object *args)
b4f334f7 2720{
b09cca6a 2721 /* This function can GC. */
b4f334f7
KH
2722 Lisp_Object newargs[3];
2723 struct gcpro gcpro1, gcpro2;
2724 Lisp_Object result;
2725
2726 newargs[0] = Fwidget_get (args[0], args[1]);
2727 newargs[1] = args[0];
2728 newargs[2] = Flist (nargs - 2, args + 2);
2729 GCPRO2 (newargs[0], newargs[2]);
2730 result = Fapply (3, newargs);
2731 UNGCPRO;
2732 return result;
2733}
dec002ca
DL
2734
2735#ifdef HAVE_LANGINFO_CODESET
2736#include <langinfo.h>
2737#endif
2738
d68beb2f
RS
2739DEFUN ("locale-info", Flocale_info, Slocale_info, 1, 1, 0,
2740 doc: /* Access locale data ITEM for the current C locale, if available.
2741ITEM should be one of the following:
30b1b0cf 2742
98aeeaa1 2743`codeset', returning the character set as a string (locale item CODESET);
30b1b0cf 2744
98aeeaa1 2745`days', returning a 7-element vector of day names (locale items DAY_n);
30b1b0cf 2746
98aeeaa1 2747`months', returning a 12-element vector of month names (locale items MON_n);
30b1b0cf 2748
d68beb2f 2749`paper', returning a list (WIDTH HEIGHT) for the default paper size,
66699ad3 2750 both measured in millimeters (locale items PAPER_WIDTH, PAPER_HEIGHT).
dec002ca
DL
2751
2752If the system can't provide such information through a call to
d68beb2f 2753`nl_langinfo', or if ITEM isn't from the list above, return nil.
dec002ca 2754
98aeeaa1
DL
2755See also Info node `(libc)Locales'.
2756
dec002ca 2757The data read from the system are decoded using `locale-coding-system'. */)
5842a27b 2758 (Lisp_Object item)
dec002ca
DL
2759{
2760 char *str = NULL;
2761#ifdef HAVE_LANGINFO_CODESET
2762 Lisp_Object val;
2763 if (EQ (item, Qcodeset))
2764 {
2765 str = nl_langinfo (CODESET);
2766 return build_string (str);
2767 }
2768#ifdef DAY_1
2769 else if (EQ (item, Qdays)) /* e.g. for calendar-day-name-array */
2770 {
2771 Lisp_Object v = Fmake_vector (make_number (7), Qnil);
77bf07e1 2772 const int days[7] = {DAY_1, DAY_2, DAY_3, DAY_4, DAY_5, DAY_6, DAY_7};
dec002ca 2773 int i;
77bf07e1
AS
2774 struct gcpro gcpro1;
2775 GCPRO1 (v);
dec002ca
DL
2776 synchronize_system_time_locale ();
2777 for (i = 0; i < 7; i++)
2778 {
2779 str = nl_langinfo (days[i]);
d7ea76b4 2780 val = build_unibyte_string (str);
dec002ca
DL
2781 /* Fixme: Is this coding system necessarily right, even if
2782 it is consistent with CODESET? If not, what to do? */
9a9d91d9
DA
2783 ASET (v, i, code_convert_string_norecord (val, Vlocale_coding_system,
2784 0));
dec002ca 2785 }
77bf07e1 2786 UNGCPRO;
dec002ca
DL
2787 return v;
2788 }
2789#endif /* DAY_1 */
2790#ifdef MON_1
2791 else if (EQ (item, Qmonths)) /* e.g. for calendar-month-name-array */
2792 {
77bf07e1
AS
2793 Lisp_Object v = Fmake_vector (make_number (12), Qnil);
2794 const int months[12] = {MON_1, MON_2, MON_3, MON_4, MON_5, MON_6, MON_7,
2795 MON_8, MON_9, MON_10, MON_11, MON_12};
dec002ca 2796 int i;
77bf07e1
AS
2797 struct gcpro gcpro1;
2798 GCPRO1 (v);
dec002ca
DL
2799 synchronize_system_time_locale ();
2800 for (i = 0; i < 12; i++)
2801 {
2802 str = nl_langinfo (months[i]);
d7ea76b4 2803 val = build_unibyte_string (str);
9a9d91d9
DA
2804 ASET (v, i, code_convert_string_norecord (val, Vlocale_coding_system,
2805 0));
dec002ca 2806 }
77bf07e1
AS
2807 UNGCPRO;
2808 return v;
dec002ca
DL
2809 }
2810#endif /* MON_1 */
2811/* LC_PAPER stuff isn't defined as accessible in glibc as of 2.3.1,
2812 but is in the locale files. This could be used by ps-print. */
2813#ifdef PAPER_WIDTH
2814 else if (EQ (item, Qpaper))
3de717bd 2815 return list2i (nl_langinfo (PAPER_WIDTH), nl_langinfo (PAPER_HEIGHT));
dec002ca
DL
2816#endif /* PAPER_WIDTH */
2817#endif /* HAVE_LANGINFO_CODESET*/
30b1b0cf 2818 return Qnil;
dec002ca 2819}
b4f334f7 2820\f
a90e80bf 2821/* base64 encode/decode functions (RFC 2045).
24c129e4
KH
2822 Based on code from GNU recode. */
2823
2824#define MIME_LINE_LENGTH 76
2825
2826#define IS_ASCII(Character) \
2827 ((Character) < 128)
2828#define IS_BASE64(Character) \
2829 (IS_ASCII (Character) && base64_char_to_value[Character] >= 0)
9a092df0
PF
2830#define IS_BASE64_IGNORABLE(Character) \
2831 ((Character) == ' ' || (Character) == '\t' || (Character) == '\n' \
2832 || (Character) == '\f' || (Character) == '\r')
2833
2834/* Used by base64_decode_1 to retrieve a non-base64-ignorable
2835 character or return retval if there are no characters left to
2836 process. */
caff31d4
KH
2837#define READ_QUADRUPLET_BYTE(retval) \
2838 do \
2839 { \
2840 if (i == length) \
2841 { \
2842 if (nchars_return) \
2843 *nchars_return = nchars; \
2844 return (retval); \
2845 } \
2846 c = from[i++]; \
2847 } \
9a092df0 2848 while (IS_BASE64_IGNORABLE (c))
24c129e4
KH
2849
2850/* Table of characters coding the 64 values. */
91433552 2851static const char base64_value_to_char[64] =
24c129e4
KH
2852{
2853 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 0- 9 */
2854 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 10-19 */
2855 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', /* 20-29 */
2856 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', /* 30-39 */
2857 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', /* 40-49 */
2858 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', /* 50-59 */
2859 '8', '9', '+', '/' /* 60-63 */
2860};
2861
2862/* Table of base64 values for first 128 characters. */
91433552 2863static const short base64_char_to_value[128] =
24c129e4
KH
2864{
2865 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0- 9 */
2866 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10- 19 */
2867 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20- 29 */
2868 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 30- 39 */
2869 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, /* 40- 49 */
2870 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, /* 50- 59 */
2871 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, /* 60- 69 */
2872 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 70- 79 */
2873 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, /* 80- 89 */
2874 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, /* 90- 99 */
2875 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, /* 100-109 */
2876 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, /* 110-119 */
2877 49, 50, 51, -1, -1, -1, -1, -1 /* 120-127 */
2878};
2879
2880/* The following diagram shows the logical steps by which three octets
2881 get transformed into four base64 characters.
2882
2883 .--------. .--------. .--------.
2884 |aaaaaabb| |bbbbcccc| |ccdddddd|
2885 `--------' `--------' `--------'
2886 6 2 4 4 2 6
2887 .--------+--------+--------+--------.
2888 |00aaaaaa|00bbbbbb|00cccccc|00dddddd|
2889 `--------+--------+--------+--------'
2890
2891 .--------+--------+--------+--------.
2892 |AAAAAAAA|BBBBBBBB|CCCCCCCC|DDDDDDDD|
2893 `--------+--------+--------+--------'
2894
2895 The octets are divided into 6 bit chunks, which are then encoded into
2896 base64 characters. */
2897
2898
f75d7a91
PE
2899static ptrdiff_t base64_encode_1 (const char *, char *, ptrdiff_t, bool, bool);
2900static ptrdiff_t base64_decode_1 (const char *, char *, ptrdiff_t, bool,
d311d28c 2901 ptrdiff_t *);
24c129e4
KH
2902
2903DEFUN ("base64-encode-region", Fbase64_encode_region, Sbase64_encode_region,
2904 2, 3, "r",
e9d8ddc9 2905 doc: /* Base64-encode the region between BEG and END.
47cebab1
GM
2906Return the length of the encoded text.
2907Optional third argument NO-LINE-BREAK means do not break long lines
e9d8ddc9 2908into shorter lines. */)
5842a27b 2909 (Lisp_Object beg, Lisp_Object end, Lisp_Object no_line_break)
24c129e4
KH
2910{
2911 char *encoded;
d311d28c
PE
2912 ptrdiff_t allength, length;
2913 ptrdiff_t ibeg, iend, encoded_length;
2914 ptrdiff_t old_pos = PT;
799c08ac 2915 USE_SAFE_ALLOCA;
24c129e4
KH
2916
2917 validate_region (&beg, &end);
2918
2919 ibeg = CHAR_TO_BYTE (XFASTINT (beg));
2920 iend = CHAR_TO_BYTE (XFASTINT (end));
2921 move_gap_both (XFASTINT (beg), ibeg);
2922
2923 /* We need to allocate enough room for encoding the text.
2924 We need 33 1/3% more space, plus a newline every 76
2925 characters, and then we round up. */
2926 length = iend - ibeg;
2927 allength = length + length/3 + 1;
2928 allength += allength / MIME_LINE_LENGTH + 1 + 6;
2929
98c6f1e3 2930 encoded = SAFE_ALLOCA (allength);
f1e59824
PE
2931 encoded_length = base64_encode_1 ((char *) BYTE_POS_ADDR (ibeg),
2932 encoded, length, NILP (no_line_break),
4b4deea2 2933 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
24c129e4 2934 if (encoded_length > allength)
1088b922 2935 emacs_abort ();
24c129e4 2936
2efdd1b9
KH
2937 if (encoded_length < 0)
2938 {
2939 /* The encoding wasn't possible. */
233f3db6 2940 SAFE_FREE ();
a90e80bf 2941 error ("Multibyte character in data for base64 encoding");
2efdd1b9
KH
2942 }
2943
24c129e4
KH
2944 /* Now we have encoded the region, so we insert the new contents
2945 and delete the old. (Insert first in order to preserve markers.) */
8b835738 2946 SET_PT_BOTH (XFASTINT (beg), ibeg);
24c129e4 2947 insert (encoded, encoded_length);
233f3db6 2948 SAFE_FREE ();
24c129e4
KH
2949 del_range_byte (ibeg + encoded_length, iend + encoded_length, 1);
2950
2951 /* If point was outside of the region, restore it exactly; else just
2952 move to the beginning of the region. */
2953 if (old_pos >= XFASTINT (end))
2954 old_pos += encoded_length - (XFASTINT (end) - XFASTINT (beg));
8b835738
AS
2955 else if (old_pos > XFASTINT (beg))
2956 old_pos = XFASTINT (beg);
24c129e4
KH
2957 SET_PT (old_pos);
2958
2959 /* We return the length of the encoded text. */
2960 return make_number (encoded_length);
2961}
2962
2963DEFUN ("base64-encode-string", Fbase64_encode_string, Sbase64_encode_string,
c22554ac 2964 1, 2, 0,
e9d8ddc9 2965 doc: /* Base64-encode STRING and return the result.
47cebab1 2966Optional second argument NO-LINE-BREAK means do not break long lines
e9d8ddc9 2967into shorter lines. */)
5842a27b 2968 (Lisp_Object string, Lisp_Object no_line_break)
24c129e4 2969{
d311d28c 2970 ptrdiff_t allength, length, encoded_length;
24c129e4 2971 char *encoded;
4b2e75e6 2972 Lisp_Object encoded_string;
799c08ac 2973 USE_SAFE_ALLOCA;
24c129e4 2974
b7826503 2975 CHECK_STRING (string);
24c129e4 2976
7f8a0840
KH
2977 /* We need to allocate enough room for encoding the text.
2978 We need 33 1/3% more space, plus a newline every 76
2979 characters, and then we round up. */
d5db4077 2980 length = SBYTES (string);
7f8a0840
KH
2981 allength = length + length/3 + 1;
2982 allength += allength / MIME_LINE_LENGTH + 1 + 6;
24c129e4
KH
2983
2984 /* We need to allocate enough room for decoding the text. */
98c6f1e3 2985 encoded = SAFE_ALLOCA (allength);
24c129e4 2986
42a5b22f 2987 encoded_length = base64_encode_1 (SSDATA (string),
2efdd1b9
KH
2988 encoded, length, NILP (no_line_break),
2989 STRING_MULTIBYTE (string));
24c129e4 2990 if (encoded_length > allength)
1088b922 2991 emacs_abort ();
24c129e4 2992
2efdd1b9
KH
2993 if (encoded_length < 0)
2994 {
2995 /* The encoding wasn't possible. */
233f3db6 2996 SAFE_FREE ();
a90e80bf 2997 error ("Multibyte character in data for base64 encoding");
2efdd1b9
KH
2998 }
2999
4b2e75e6 3000 encoded_string = make_unibyte_string (encoded, encoded_length);
233f3db6 3001 SAFE_FREE ();
4b2e75e6
EZ
3002
3003 return encoded_string;
24c129e4
KH
3004}
3005
d311d28c
PE
3006static ptrdiff_t
3007base64_encode_1 (const char *from, char *to, ptrdiff_t length,
f75d7a91 3008 bool line_break, bool multibyte)
24c129e4 3009{
e6d4aefa 3010 int counter = 0;
d311d28c 3011 ptrdiff_t i = 0;
24c129e4 3012 char *e = to;
844eb643 3013 int c;
24c129e4 3014 unsigned int value;
2efdd1b9 3015 int bytes;
24c129e4
KH
3016
3017 while (i < length)
3018 {
2efdd1b9
KH
3019 if (multibyte)
3020 {
f1e59824 3021 c = STRING_CHAR_AND_LENGTH ((unsigned char *) from + i, bytes);
680d4b87
KH
3022 if (CHAR_BYTE8_P (c))
3023 c = CHAR_TO_BYTE8 (c);
3024 else if (c >= 256)
2efdd1b9 3025 return -1;
caff31d4 3026 i += bytes;
2efdd1b9
KH
3027 }
3028 else
3029 c = from[i++];
24c129e4
KH
3030
3031 /* Wrap line every 76 characters. */
3032
3033 if (line_break)
3034 {
3035 if (counter < MIME_LINE_LENGTH / 4)
3036 counter++;
3037 else
3038 {
3039 *e++ = '\n';
3040 counter = 1;
3041 }
3042 }
3043
3044 /* Process first byte of a triplet. */
3045
3046 *e++ = base64_value_to_char[0x3f & c >> 2];
3047 value = (0x03 & c) << 4;
3048
3049 /* Process second byte of a triplet. */
3050
3051 if (i == length)
3052 {
3053 *e++ = base64_value_to_char[value];
3054 *e++ = '=';
3055 *e++ = '=';
3056 break;
3057 }
3058
2efdd1b9
KH
3059 if (multibyte)
3060 {
f1e59824 3061 c = STRING_CHAR_AND_LENGTH ((unsigned char *) from + i, bytes);
680d4b87
KH
3062 if (CHAR_BYTE8_P (c))
3063 c = CHAR_TO_BYTE8 (c);
3064 else if (c >= 256)
9b40fbe6 3065 return -1;
caff31d4 3066 i += bytes;
2efdd1b9
KH
3067 }
3068 else
3069 c = from[i++];
24c129e4
KH
3070
3071 *e++ = base64_value_to_char[value | (0x0f & c >> 4)];
3072 value = (0x0f & c) << 2;
3073
3074 /* Process third byte of a triplet. */
3075
3076 if (i == length)
3077 {
3078 *e++ = base64_value_to_char[value];
3079 *e++ = '=';
3080 break;
3081 }
3082
2efdd1b9
KH
3083 if (multibyte)
3084 {
f1e59824 3085 c = STRING_CHAR_AND_LENGTH ((unsigned char *) from + i, bytes);
680d4b87
KH
3086 if (CHAR_BYTE8_P (c))
3087 c = CHAR_TO_BYTE8 (c);
3088 else if (c >= 256)
844eb643 3089 return -1;
caff31d4 3090 i += bytes;
2efdd1b9
KH
3091 }
3092 else
3093 c = from[i++];
24c129e4
KH
3094
3095 *e++ = base64_value_to_char[value | (0x03 & c >> 6)];
3096 *e++ = base64_value_to_char[0x3f & c];
3097 }
3098
24c129e4
KH
3099 return e - to;
3100}
3101
3102
3103DEFUN ("base64-decode-region", Fbase64_decode_region, Sbase64_decode_region,
47cebab1 3104 2, 2, "r",
e9d8ddc9 3105 doc: /* Base64-decode the region between BEG and END.
47cebab1 3106Return the length of the decoded text.
e9d8ddc9 3107If the region can't be decoded, signal an error and don't modify the buffer. */)
5842a27b 3108 (Lisp_Object beg, Lisp_Object end)
24c129e4 3109{
d311d28c 3110 ptrdiff_t ibeg, iend, length, allength;
24c129e4 3111 char *decoded;
d311d28c
PE
3112 ptrdiff_t old_pos = PT;
3113 ptrdiff_t decoded_length;
3114 ptrdiff_t inserted_chars;
f75d7a91 3115 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
799c08ac 3116 USE_SAFE_ALLOCA;
24c129e4
KH
3117
3118 validate_region (&beg, &end);
3119
3120 ibeg = CHAR_TO_BYTE (XFASTINT (beg));
3121 iend = CHAR_TO_BYTE (XFASTINT (end));
3122
3123 length = iend - ibeg;
caff31d4
KH
3124
3125 /* We need to allocate enough room for decoding the text. If we are
3126 working on a multibyte buffer, each decoded code may occupy at
3127 most two bytes. */
3128 allength = multibyte ? length * 2 : length;
98c6f1e3 3129 decoded = SAFE_ALLOCA (allength);
24c129e4
KH
3130
3131 move_gap_both (XFASTINT (beg), ibeg);
f1e59824
PE
3132 decoded_length = base64_decode_1 ((char *) BYTE_POS_ADDR (ibeg),
3133 decoded, length,
caff31d4
KH
3134 multibyte, &inserted_chars);
3135 if (decoded_length > allength)
1088b922 3136 emacs_abort ();
24c129e4
KH
3137
3138 if (decoded_length < 0)
8c217645
KH
3139 {
3140 /* The decoding wasn't possible. */
233f3db6 3141 SAFE_FREE ();
a90e80bf 3142 error ("Invalid base64 data");
8c217645 3143 }
24c129e4
KH
3144
3145 /* Now we have decoded the region, so we insert the new contents
3146 and delete the old. (Insert first in order to preserve markers.) */
59f953a2 3147 TEMP_SET_PT_BOTH (XFASTINT (beg), ibeg);
2efdd1b9 3148 insert_1_both (decoded, inserted_chars, decoded_length, 0, 1, 0);
233f3db6 3149 SAFE_FREE ();
799c08ac 3150
2efdd1b9
KH
3151 /* Delete the original text. */
3152 del_range_both (PT, PT_BYTE, XFASTINT (end) + inserted_chars,
3153 iend + decoded_length, 1);
24c129e4
KH
3154
3155 /* If point was outside of the region, restore it exactly; else just
3156 move to the beginning of the region. */
3157 if (old_pos >= XFASTINT (end))
9b703a38
KH
3158 old_pos += inserted_chars - (XFASTINT (end) - XFASTINT (beg));
3159 else if (old_pos > XFASTINT (beg))
3160 old_pos = XFASTINT (beg);
e52ad9c9 3161 SET_PT (old_pos > ZV ? ZV : old_pos);
24c129e4 3162
9b703a38 3163 return make_number (inserted_chars);
24c129e4
KH
3164}
3165
3166DEFUN ("base64-decode-string", Fbase64_decode_string, Sbase64_decode_string,
3167 1, 1, 0,
e9d8ddc9 3168 doc: /* Base64-decode STRING and return the result. */)
5842a27b 3169 (Lisp_Object string)
24c129e4
KH
3170{
3171 char *decoded;
d311d28c 3172 ptrdiff_t length, decoded_length;
4b2e75e6 3173 Lisp_Object decoded_string;
799c08ac 3174 USE_SAFE_ALLOCA;
24c129e4 3175
b7826503 3176 CHECK_STRING (string);
24c129e4 3177
d5db4077 3178 length = SBYTES (string);
24c129e4 3179 /* We need to allocate enough room for decoding the text. */
98c6f1e3 3180 decoded = SAFE_ALLOCA (length);
24c129e4 3181
8ec118cd 3182 /* The decoded result should be unibyte. */
42a5b22f 3183 decoded_length = base64_decode_1 (SSDATA (string), decoded, length,
8ec118cd 3184 0, NULL);
24c129e4 3185 if (decoded_length > length)
1088b922 3186 emacs_abort ();
3d6c79c5 3187 else if (decoded_length >= 0)
2efdd1b9 3188 decoded_string = make_unibyte_string (decoded, decoded_length);
3d6c79c5
GM
3189 else
3190 decoded_string = Qnil;
24c129e4 3191
233f3db6 3192 SAFE_FREE ();
3d6c79c5 3193 if (!STRINGP (decoded_string))
a90e80bf 3194 error ("Invalid base64 data");
4b2e75e6
EZ
3195
3196 return decoded_string;
24c129e4
KH
3197}
3198
53964682 3199/* Base64-decode the data at FROM of LENGTH bytes into TO. If
f75d7a91 3200 MULTIBYTE, the decoded result should be in multibyte
9858f6c3 3201 form. If NCHARS_RETURN is not NULL, store the number of produced
caff31d4
KH
3202 characters in *NCHARS_RETURN. */
3203
d311d28c
PE
3204static ptrdiff_t
3205base64_decode_1 (const char *from, char *to, ptrdiff_t length,
f75d7a91 3206 bool multibyte, ptrdiff_t *nchars_return)
24c129e4 3207{
d311d28c 3208 ptrdiff_t i = 0; /* Used inside READ_QUADRUPLET_BYTE */
24c129e4
KH
3209 char *e = to;
3210 unsigned char c;
3211 unsigned long value;
d311d28c 3212 ptrdiff_t nchars = 0;
24c129e4 3213
9a092df0 3214 while (1)
24c129e4 3215 {
9a092df0 3216 /* Process first byte of a quadruplet. */
24c129e4 3217
9a092df0 3218 READ_QUADRUPLET_BYTE (e-to);
24c129e4
KH
3219
3220 if (!IS_BASE64 (c))
3221 return -1;
3222 value = base64_char_to_value[c] << 18;
3223
3224 /* Process second byte of a quadruplet. */
3225
9a092df0 3226 READ_QUADRUPLET_BYTE (-1);
24c129e4
KH
3227
3228 if (!IS_BASE64 (c))
3229 return -1;
3230 value |= base64_char_to_value[c] << 12;
3231
caff31d4 3232 c = (unsigned char) (value >> 16);
5a38b8c5
KH
3233 if (multibyte && c >= 128)
3234 e += BYTE8_STRING (c, e);
caff31d4
KH
3235 else
3236 *e++ = c;
3237 nchars++;
24c129e4
KH
3238
3239 /* Process third byte of a quadruplet. */
59f953a2 3240
9a092df0 3241 READ_QUADRUPLET_BYTE (-1);
24c129e4
KH
3242
3243 if (c == '=')
3244 {
9a092df0 3245 READ_QUADRUPLET_BYTE (-1);
59f953a2 3246
24c129e4
KH
3247 if (c != '=')
3248 return -1;
3249 continue;
3250 }
3251
3252 if (!IS_BASE64 (c))
3253 return -1;
3254 value |= base64_char_to_value[c] << 6;
3255
caff31d4 3256 c = (unsigned char) (0xff & value >> 8);
5a38b8c5
KH
3257 if (multibyte && c >= 128)
3258 e += BYTE8_STRING (c, e);
caff31d4
KH
3259 else
3260 *e++ = c;
3261 nchars++;
24c129e4
KH
3262
3263 /* Process fourth byte of a quadruplet. */
3264
9a092df0 3265 READ_QUADRUPLET_BYTE (-1);
24c129e4
KH
3266
3267 if (c == '=')
3268 continue;
3269
3270 if (!IS_BASE64 (c))
3271 return -1;
3272 value |= base64_char_to_value[c];
3273
caff31d4 3274 c = (unsigned char) (0xff & value);
5a38b8c5
KH
3275 if (multibyte && c >= 128)
3276 e += BYTE8_STRING (c, e);
caff31d4
KH
3277 else
3278 *e++ = c;
3279 nchars++;
24c129e4 3280 }
24c129e4 3281}
d80c6c11
GM
3282
3283
3284\f
3285/***********************************************************************
3286 ***** *****
3287 ***** Hash Tables *****
3288 ***** *****
3289 ***********************************************************************/
3290
3291/* Implemented by gerd@gnu.org. This hash table implementation was
3292 inspired by CMUCL hash tables. */
3293
3294/* Ideas:
3295
3296 1. For small tables, association lists are probably faster than
3297 hash tables because they have lower overhead.
3298
3299 For uses of hash tables where the O(1) behavior of table
3300 operations is not a requirement, it might therefore be a good idea
3301 not to hash. Instead, we could just do a linear search in the
3302 key_and_value vector of the hash table. This could be done
3303 if a `:linear-search t' argument is given to make-hash-table. */
3304
3305
d80c6c11
GM
3306/* The list of all weak hash tables. Don't staticpro this one. */
3307
dfcf3579 3308static struct Lisp_Hash_Table *weak_hash_tables;
d80c6c11
GM
3309
3310/* Various symbols. */
3311
84575e67
PE
3312static Lisp_Object Qhash_table_p;
3313static Lisp_Object Qkey, Qvalue, Qeql;
53371430 3314Lisp_Object Qeq, Qequal;
ee0403b3 3315Lisp_Object QCtest, QCsize, QCrehash_size, QCrehash_threshold, QCweakness;
955cbe7b 3316static Lisp_Object Qhash_table_test, Qkey_or_value, Qkey_and_value;
d80c6c11 3317
d80c6c11
GM
3318\f
3319/***********************************************************************
3320 Utilities
3321 ***********************************************************************/
3322
84575e67
PE
3323static void
3324CHECK_HASH_TABLE (Lisp_Object x)
3325{
3326 CHECK_TYPE (HASH_TABLE_P (x), Qhash_table_p, x);
3327}
3328
3329static void
3330set_hash_key_and_value (struct Lisp_Hash_Table *h, Lisp_Object key_and_value)
3331{
3332 h->key_and_value = key_and_value;
3333}
3334static void
3335set_hash_next (struct Lisp_Hash_Table *h, Lisp_Object next)
3336{
3337 h->next = next;
3338}
3339static void
3340set_hash_next_slot (struct Lisp_Hash_Table *h, ptrdiff_t idx, Lisp_Object val)
3341{
3342 gc_aset (h->next, idx, val);
3343}
3344static void
3345set_hash_hash (struct Lisp_Hash_Table *h, Lisp_Object hash)
3346{
3347 h->hash = hash;
3348}
3349static void
3350set_hash_hash_slot (struct Lisp_Hash_Table *h, ptrdiff_t idx, Lisp_Object val)
3351{
3352 gc_aset (h->hash, idx, val);
3353}
3354static void
3355set_hash_index (struct Lisp_Hash_Table *h, Lisp_Object index)
3356{
3357 h->index = index;
3358}
3359static void
3360set_hash_index_slot (struct Lisp_Hash_Table *h, ptrdiff_t idx, Lisp_Object val)
3361{
3362 gc_aset (h->index, idx, val);
3363}
3364
d80c6c11
GM
3365/* If OBJ is a Lisp hash table, return a pointer to its struct
3366 Lisp_Hash_Table. Otherwise, signal an error. */
3367
3368static struct Lisp_Hash_Table *
971de7fb 3369check_hash_table (Lisp_Object obj)
d80c6c11 3370{
b7826503 3371 CHECK_HASH_TABLE (obj);
d80c6c11
GM
3372 return XHASH_TABLE (obj);
3373}
3374
3375
3376/* Value is the next integer I >= N, N >= 0 which is "almost" a prime
ca9ce8f2
PE
3377 number. A number is "almost" a prime number if it is not divisible
3378 by any integer in the range 2 .. (NEXT_ALMOST_PRIME_LIMIT - 1). */
d80c6c11 3379
0de4bb68
PE
3380EMACS_INT
3381next_almost_prime (EMACS_INT n)
d80c6c11 3382{
ca9ce8f2 3383 verify (NEXT_ALMOST_PRIME_LIMIT == 11);
86fe5cfe
PE
3384 for (n |= 1; ; n += 2)
3385 if (n % 3 != 0 && n % 5 != 0 && n % 7 != 0)
3386 return n;
d80c6c11
GM
3387}
3388
3389
3390/* Find KEY in ARGS which has size NARGS. Don't consider indices for
3391 which USED[I] is non-zero. If found at index I in ARGS, set
3392 USED[I] and USED[I + 1] to 1, and return I + 1. Otherwise return
c5101a77 3393 0. This function is used to extract a keyword/argument pair from
d80c6c11
GM
3394 a DEFUN parameter list. */
3395
f66c7cf8
PE
3396static ptrdiff_t
3397get_key_arg (Lisp_Object key, ptrdiff_t nargs, Lisp_Object *args, char *used)
d80c6c11 3398{
f66c7cf8 3399 ptrdiff_t i;
59f953a2 3400
c5101a77
PE
3401 for (i = 1; i < nargs; i++)
3402 if (!used[i - 1] && EQ (args[i - 1], key))
3403 {
3404 used[i - 1] = 1;
3405 used[i] = 1;
3406 return i;
3407 }
59f953a2 3408
c5101a77 3409 return 0;
d80c6c11
GM
3410}
3411
3412
3413/* Return a Lisp vector which has the same contents as VEC but has
d311d28c
PE
3414 at least INCR_MIN more entries, where INCR_MIN is positive.
3415 If NITEMS_MAX is not -1, do not grow the vector to be any larger
3416 than NITEMS_MAX. Entries in the resulting
3417 vector that are not copied from VEC are set to nil. */
d80c6c11 3418
fa7dad5b 3419Lisp_Object
8c172e82 3420larger_vector (Lisp_Object vec, ptrdiff_t incr_min, ptrdiff_t nitems_max)
d80c6c11
GM
3421{
3422 struct Lisp_Vector *v;
d311d28c 3423 ptrdiff_t i, incr, incr_max, old_size, new_size;
91f2d272 3424 ptrdiff_t C_language_max = min (PTRDIFF_MAX, SIZE_MAX) / sizeof *v->contents;
8c172e82
PE
3425 ptrdiff_t n_max = (0 <= nitems_max && nitems_max < C_language_max
3426 ? nitems_max : C_language_max);
a54e2c05
DA
3427 eassert (VECTORP (vec));
3428 eassert (0 < incr_min && -1 <= nitems_max);
7edbb0da 3429 old_size = ASIZE (vec);
d311d28c
PE
3430 incr_max = n_max - old_size;
3431 incr = max (incr_min, min (old_size >> 1, incr_max));
3432 if (incr_max < incr)
3433 memory_full (SIZE_MAX);
3434 new_size = old_size + incr;
b3660ef6 3435 v = allocate_vector (new_size);
91f2d272 3436 memcpy (v->contents, XVECTOR (vec)->contents, old_size * sizeof *v->contents);
d80c6c11 3437 for (i = old_size; i < new_size; ++i)
91f2d272 3438 v->contents[i] = Qnil;
d80c6c11
GM
3439 XSETVECTOR (vec, v);
3440 return vec;
3441}
3442
3443
3444/***********************************************************************
3445 Low-level Functions
3446 ***********************************************************************/
3447
53371430
PE
3448static struct hash_table_test hashtest_eq;
3449struct hash_table_test hashtest_eql, hashtest_equal;
b7432bb2 3450
d80c6c11 3451/* Compare KEY1 which has hash code HASH1 and KEY2 with hash code
f75d7a91 3452 HASH2 in hash table H using `eql'. Value is true if KEY1 and
d80c6c11
GM
3453 KEY2 are the same. */
3454
f75d7a91 3455static bool
b7432bb2
SM
3456cmpfn_eql (struct hash_table_test *ht,
3457 Lisp_Object key1,
3458 Lisp_Object key2)
d80c6c11 3459{
2e5da676
GM
3460 return (FLOATP (key1)
3461 && FLOATP (key2)
e84b1dea 3462 && XFLOAT_DATA (key1) == XFLOAT_DATA (key2));
d80c6c11
GM
3463}
3464
3465
3466/* Compare KEY1 which has hash code HASH1 and KEY2 with hash code
f75d7a91 3467 HASH2 in hash table H using `equal'. Value is true if KEY1 and
d80c6c11
GM
3468 KEY2 are the same. */
3469
f75d7a91 3470static bool
b7432bb2
SM
3471cmpfn_equal (struct hash_table_test *ht,
3472 Lisp_Object key1,
3473 Lisp_Object key2)
d80c6c11 3474{
b7432bb2 3475 return !NILP (Fequal (key1, key2));
d80c6c11
GM
3476}
3477
59f953a2 3478
d80c6c11 3479/* Compare KEY1 which has hash code HASH1, and KEY2 with hash code
f75d7a91 3480 HASH2 in hash table H using H->user_cmp_function. Value is true
d80c6c11
GM
3481 if KEY1 and KEY2 are the same. */
3482
f75d7a91 3483static bool
b7432bb2
SM
3484cmpfn_user_defined (struct hash_table_test *ht,
3485 Lisp_Object key1,
3486 Lisp_Object key2)
d80c6c11 3487{
b7432bb2 3488 Lisp_Object args[3];
59f953a2 3489
b7432bb2
SM
3490 args[0] = ht->user_cmp_function;
3491 args[1] = key1;
3492 args[2] = key2;
3493 return !NILP (Ffuncall (3, args));
d80c6c11
GM
3494}
3495
3496
3497/* Value is a hash code for KEY for use in hash table H which uses
3498 `eq' to compare keys. The hash code returned is guaranteed to fit
3499 in a Lisp integer. */
3500
0de4bb68 3501static EMACS_UINT
b7432bb2 3502hashfn_eq (struct hash_table_test *ht, Lisp_Object key)
d80c6c11 3503{
61ddb1b9 3504 EMACS_UINT hash = XHASH (key) ^ XTYPE (key);
cf681889 3505 return hash;
d80c6c11
GM
3506}
3507
d80c6c11
GM
3508/* Value is a hash code for KEY for use in hash table H which uses
3509 `eql' to compare keys. The hash code returned is guaranteed to fit
3510 in a Lisp integer. */
3511
0de4bb68 3512static EMACS_UINT
b7432bb2 3513hashfn_eql (struct hash_table_test *ht, Lisp_Object key)
d80c6c11 3514{
0de4bb68 3515 EMACS_UINT hash;
cf681889
GM
3516 if (FLOATP (key))
3517 hash = sxhash (key, 0);
d80c6c11 3518 else
61ddb1b9 3519 hash = XHASH (key) ^ XTYPE (key);
cf681889 3520 return hash;
d80c6c11
GM
3521}
3522
d80c6c11
GM
3523/* Value is a hash code for KEY for use in hash table H which uses
3524 `equal' to compare keys. The hash code returned is guaranteed to fit
3525 in a Lisp integer. */
3526
0de4bb68 3527static EMACS_UINT
b7432bb2 3528hashfn_equal (struct hash_table_test *ht, Lisp_Object key)
d80c6c11 3529{
0de4bb68 3530 EMACS_UINT hash = sxhash (key, 0);
cf681889 3531 return hash;
d80c6c11
GM
3532}
3533
d80c6c11
GM
3534/* Value is a hash code for KEY for use in hash table H which uses as
3535 user-defined function to compare keys. The hash code returned is
3536 guaranteed to fit in a Lisp integer. */
3537
0de4bb68 3538static EMACS_UINT
b7432bb2 3539hashfn_user_defined (struct hash_table_test *ht, Lisp_Object key)
d80c6c11
GM
3540{
3541 Lisp_Object args[2], hash;
59f953a2 3542
b7432bb2 3543 args[0] = ht->user_hash_function;
d80c6c11
GM
3544 args[1] = key;
3545 hash = Ffuncall (2, args);
79804536 3546 return hashfn_eq (ht, hash);
d80c6c11
GM
3547}
3548
d311d28c
PE
3549/* An upper bound on the size of a hash table index. It must fit in
3550 ptrdiff_t and be a valid Emacs fixnum. */
3551#define INDEX_SIZE_BOUND \
663e2b3f 3552 ((ptrdiff_t) min (MOST_POSITIVE_FIXNUM, PTRDIFF_MAX / word_size))
d80c6c11
GM
3553
3554/* Create and initialize a new hash table.
3555
3556 TEST specifies the test the hash table will use to compare keys.
3557 It must be either one of the predefined tests `eq', `eql' or
3558 `equal' or a symbol denoting a user-defined test named TEST with
3559 test and hash functions USER_TEST and USER_HASH.
59f953a2 3560
1fd4c450 3561 Give the table initial capacity SIZE, SIZE >= 0, an integer.
d80c6c11
GM
3562
3563 If REHASH_SIZE is an integer, it must be > 0, and this hash table's
3564 new size when it becomes full is computed by adding REHASH_SIZE to
3565 its old size. If REHASH_SIZE is a float, it must be > 1.0, and the
3566 table's new size is computed by multiplying its old size with
3567 REHASH_SIZE.
3568
3569 REHASH_THRESHOLD must be a float <= 1.0, and > 0. The table will
3570 be resized when the ratio of (number of entries in the table) /
3571 (table size) is >= REHASH_THRESHOLD.
3572
3573 WEAK specifies the weakness of the table. If non-nil, it must be
ec504e6f 3574 one of the symbols `key', `value', `key-or-value', or `key-and-value'. */
d80c6c11
GM
3575
3576Lisp_Object
b7432bb2
SM
3577make_hash_table (struct hash_table_test test,
3578 Lisp_Object size, Lisp_Object rehash_size,
3579 Lisp_Object rehash_threshold, Lisp_Object weak)
d80c6c11
GM
3580{
3581 struct Lisp_Hash_Table *h;
d80c6c11 3582 Lisp_Object table;
d311d28c
PE
3583 EMACS_INT index_size, sz;
3584 ptrdiff_t i;
0de4bb68 3585 double index_float;
d80c6c11
GM
3586
3587 /* Preconditions. */
b7432bb2 3588 eassert (SYMBOLP (test.name));
a54e2c05
DA
3589 eassert (INTEGERP (size) && XINT (size) >= 0);
3590 eassert ((INTEGERP (rehash_size) && XINT (rehash_size) > 0)
0de4bb68 3591 || (FLOATP (rehash_size) && 1 < XFLOAT_DATA (rehash_size)));
a54e2c05 3592 eassert (FLOATP (rehash_threshold)
0de4bb68
PE
3593 && 0 < XFLOAT_DATA (rehash_threshold)
3594 && XFLOAT_DATA (rehash_threshold) <= 1.0);
d80c6c11 3595
1fd4c450
GM
3596 if (XFASTINT (size) == 0)
3597 size = make_number (1);
3598
0de4bb68
PE
3599 sz = XFASTINT (size);
3600 index_float = sz / XFLOAT_DATA (rehash_threshold);
d311d28c 3601 index_size = (index_float < INDEX_SIZE_BOUND + 1
0de4bb68 3602 ? next_almost_prime (index_float)
d311d28c
PE
3603 : INDEX_SIZE_BOUND + 1);
3604 if (INDEX_SIZE_BOUND < max (index_size, 2 * sz))
0de4bb68
PE
3605 error ("Hash table too large");
3606
b3660ef6
GM
3607 /* Allocate a table and initialize it. */
3608 h = allocate_hash_table ();
d80c6c11
GM
3609
3610 /* Initialize hash table slots. */
d80c6c11 3611 h->test = test;
d80c6c11
GM
3612 h->weak = weak;
3613 h->rehash_threshold = rehash_threshold;
3614 h->rehash_size = rehash_size;
878f97ff 3615 h->count = 0;
d80c6c11
GM
3616 h->key_and_value = Fmake_vector (make_number (2 * sz), Qnil);
3617 h->hash = Fmake_vector (size, Qnil);
3618 h->next = Fmake_vector (size, Qnil);
d80c6c11
GM
3619 h->index = Fmake_vector (make_number (index_size), Qnil);
3620
3621 /* Set up the free list. */
3622 for (i = 0; i < sz - 1; ++i)
e83064be 3623 set_hash_next_slot (h, i, make_number (i + 1));
d80c6c11
GM
3624 h->next_free = make_number (0);
3625
3626 XSET_HASH_TABLE (table, h);
a54e2c05
DA
3627 eassert (HASH_TABLE_P (table));
3628 eassert (XHASH_TABLE (table) == h);
d80c6c11
GM
3629
3630 /* Maybe add this hash table to the list of all weak hash tables. */
3631 if (NILP (h->weak))
6c661ec9 3632 h->next_weak = NULL;
d80c6c11
GM
3633 else
3634 {
6c661ec9
SM
3635 h->next_weak = weak_hash_tables;
3636 weak_hash_tables = h;
d80c6c11
GM
3637 }
3638
3639 return table;
3640}
3641
3642
f899c503
GM
3643/* Return a copy of hash table H1. Keys and values are not copied,
3644 only the table itself is. */
3645
2f7c71a1 3646static Lisp_Object
971de7fb 3647copy_hash_table (struct Lisp_Hash_Table *h1)
f899c503
GM
3648{
3649 Lisp_Object table;
3650 struct Lisp_Hash_Table *h2;
59f953a2 3651
b3660ef6 3652 h2 = allocate_hash_table ();
ae1d87e2 3653 *h2 = *h1;
f899c503
GM
3654 h2->key_and_value = Fcopy_sequence (h1->key_and_value);
3655 h2->hash = Fcopy_sequence (h1->hash);
3656 h2->next = Fcopy_sequence (h1->next);
3657 h2->index = Fcopy_sequence (h1->index);
3658 XSET_HASH_TABLE (table, h2);
3659
3660 /* Maybe add this hash table to the list of all weak hash tables. */
3661 if (!NILP (h2->weak))
3662 {
6c661ec9
SM
3663 h2->next_weak = weak_hash_tables;
3664 weak_hash_tables = h2;
f899c503
GM
3665 }
3666
3667 return table;
3668}
3669
3670
d80c6c11
GM
3671/* Resize hash table H if it's too full. If H cannot be resized
3672 because it's already too large, throw an error. */
3673
b0ab8123 3674static void
971de7fb 3675maybe_resize_hash_table (struct Lisp_Hash_Table *h)
d80c6c11
GM
3676{
3677 if (NILP (h->next_free))
3678 {
d311d28c
PE
3679 ptrdiff_t old_size = HASH_TABLE_SIZE (h);
3680 EMACS_INT new_size, index_size, nsize;
3681 ptrdiff_t i;
0de4bb68 3682 double index_float;
59f953a2 3683
d80c6c11
GM
3684 if (INTEGERP (h->rehash_size))
3685 new_size = old_size + XFASTINT (h->rehash_size);
3686 else
0de4bb68
PE
3687 {
3688 double float_new_size = old_size * XFLOAT_DATA (h->rehash_size);
d311d28c 3689 if (float_new_size < INDEX_SIZE_BOUND + 1)
0de4bb68
PE
3690 {
3691 new_size = float_new_size;
3692 if (new_size <= old_size)
3693 new_size = old_size + 1;
3694 }
3695 else
d311d28c 3696 new_size = INDEX_SIZE_BOUND + 1;
0de4bb68
PE
3697 }
3698 index_float = new_size / XFLOAT_DATA (h->rehash_threshold);
d311d28c 3699 index_size = (index_float < INDEX_SIZE_BOUND + 1
0de4bb68 3700 ? next_almost_prime (index_float)
d311d28c 3701 : INDEX_SIZE_BOUND + 1);
9bd1cd35 3702 nsize = max (index_size, 2 * new_size);
d311d28c 3703 if (INDEX_SIZE_BOUND < nsize)
d80c6c11
GM
3704 error ("Hash table too large to resize");
3705
1ec4b7b2
SM
3706#ifdef ENABLE_CHECKING
3707 if (HASH_TABLE_P (Vpurify_flag)
3708 && XHASH_TABLE (Vpurify_flag) == h)
3709 {
3710 Lisp_Object args[2];
3711 args[0] = build_string ("Growing hash table to: %d");
3712 args[1] = make_number (new_size);
3713 Fmessage (2, args);
3714 }
3715#endif
3716
e83064be
DA
3717 set_hash_key_and_value (h, larger_vector (h->key_and_value,
3718 2 * (new_size - old_size), -1));
3719 set_hash_next (h, larger_vector (h->next, new_size - old_size, -1));
3720 set_hash_hash (h, larger_vector (h->hash, new_size - old_size, -1));
3721 set_hash_index (h, Fmake_vector (make_number (index_size), Qnil));
d80c6c11
GM
3722
3723 /* Update the free list. Do it so that new entries are added at
3724 the end of the free list. This makes some operations like
3725 maphash faster. */
3726 for (i = old_size; i < new_size - 1; ++i)
e83064be 3727 set_hash_next_slot (h, i, make_number (i + 1));
59f953a2 3728
d80c6c11
GM
3729 if (!NILP (h->next_free))
3730 {
3731 Lisp_Object last, next;
59f953a2 3732
d80c6c11
GM
3733 last = h->next_free;
3734 while (next = HASH_NEXT (h, XFASTINT (last)),
3735 !NILP (next))
3736 last = next;
59f953a2 3737
e83064be 3738 set_hash_next_slot (h, XFASTINT (last), make_number (old_size));
d80c6c11
GM
3739 }
3740 else
3741 XSETFASTINT (h->next_free, old_size);
3742
3743 /* Rehash. */
3744 for (i = 0; i < old_size; ++i)
3745 if (!NILP (HASH_HASH (h, i)))
3746 {
0de4bb68 3747 EMACS_UINT hash_code = XUINT (HASH_HASH (h, i));
d311d28c 3748 ptrdiff_t start_of_bucket = hash_code % ASIZE (h->index);
e83064be
DA
3749 set_hash_next_slot (h, i, HASH_INDEX (h, start_of_bucket));
3750 set_hash_index_slot (h, start_of_bucket, make_number (i));
d80c6c11 3751 }
59f953a2 3752 }
d80c6c11
GM
3753}
3754
3755
3756/* Lookup KEY in hash table H. If HASH is non-null, return in *HASH
3757 the hash code of KEY. Value is the index of the entry in H
3758 matching KEY, or -1 if not found. */
3759
d3411f89 3760ptrdiff_t
0de4bb68 3761hash_lookup (struct Lisp_Hash_Table *h, Lisp_Object key, EMACS_UINT *hash)
d80c6c11 3762{
0de4bb68 3763 EMACS_UINT hash_code;
d3411f89 3764 ptrdiff_t start_of_bucket;
d80c6c11
GM
3765 Lisp_Object idx;
3766
b7432bb2
SM
3767 hash_code = h->test.hashfn (&h->test, key);
3768 eassert ((hash_code & ~INTMASK) == 0);
d80c6c11
GM
3769 if (hash)
3770 *hash = hash_code;
59f953a2 3771
7edbb0da 3772 start_of_bucket = hash_code % ASIZE (h->index);
d80c6c11
GM
3773 idx = HASH_INDEX (h, start_of_bucket);
3774
f5c75033 3775 /* We need not gcpro idx since it's either an integer or nil. */
d80c6c11
GM
3776 while (!NILP (idx))
3777 {
d311d28c 3778 ptrdiff_t i = XFASTINT (idx);
2e5da676 3779 if (EQ (key, HASH_KEY (h, i))
b7432bb2
SM
3780 || (h->test.cmpfn
3781 && hash_code == XUINT (HASH_HASH (h, i))
3782 && h->test.cmpfn (&h->test, key, HASH_KEY (h, i))))
d80c6c11
GM
3783 break;
3784 idx = HASH_NEXT (h, i);
3785 }
3786
3787 return NILP (idx) ? -1 : XFASTINT (idx);
3788}
3789
3790
3791/* Put an entry into hash table H that associates KEY with VALUE.
64a5094a
KH
3792 HASH is a previously computed hash code of KEY.
3793 Value is the index of the entry in H matching KEY. */
d80c6c11 3794
d3411f89 3795ptrdiff_t
0de4bb68
PE
3796hash_put (struct Lisp_Hash_Table *h, Lisp_Object key, Lisp_Object value,
3797 EMACS_UINT hash)
d80c6c11 3798{
d3411f89 3799 ptrdiff_t start_of_bucket, i;
d80c6c11 3800
a54e2c05 3801 eassert ((hash & ~INTMASK) == 0);
d80c6c11
GM
3802
3803 /* Increment count after resizing because resizing may fail. */
3804 maybe_resize_hash_table (h);
878f97ff 3805 h->count++;
59f953a2 3806
d80c6c11
GM
3807 /* Store key/value in the key_and_value vector. */
3808 i = XFASTINT (h->next_free);
3809 h->next_free = HASH_NEXT (h, i);
e83064be
DA
3810 set_hash_key_slot (h, i, key);
3811 set_hash_value_slot (h, i, value);
d80c6c11
GM
3812
3813 /* Remember its hash code. */
e83064be 3814 set_hash_hash_slot (h, i, make_number (hash));
d80c6c11
GM
3815
3816 /* Add new entry to its collision chain. */
7edbb0da 3817 start_of_bucket = hash % ASIZE (h->index);
e83064be
DA
3818 set_hash_next_slot (h, i, HASH_INDEX (h, start_of_bucket));
3819 set_hash_index_slot (h, start_of_bucket, make_number (i));
64a5094a 3820 return i;
d80c6c11
GM
3821}
3822
3823
3824/* Remove the entry matching KEY from hash table H, if there is one. */
3825
2749d28e 3826static void
971de7fb 3827hash_remove_from_table (struct Lisp_Hash_Table *h, Lisp_Object key)
d80c6c11 3828{
0de4bb68 3829 EMACS_UINT hash_code;
d311d28c 3830 ptrdiff_t start_of_bucket;
d80c6c11
GM
3831 Lisp_Object idx, prev;
3832
b7432bb2
SM
3833 hash_code = h->test.hashfn (&h->test, key);
3834 eassert ((hash_code & ~INTMASK) == 0);
7edbb0da 3835 start_of_bucket = hash_code % ASIZE (h->index);
d80c6c11
GM
3836 idx = HASH_INDEX (h, start_of_bucket);
3837 prev = Qnil;
3838
f5c75033 3839 /* We need not gcpro idx, prev since they're either integers or nil. */
d80c6c11
GM
3840 while (!NILP (idx))
3841 {
d311d28c 3842 ptrdiff_t i = XFASTINT (idx);
d80c6c11 3843
2e5da676 3844 if (EQ (key, HASH_KEY (h, i))
b7432bb2
SM
3845 || (h->test.cmpfn
3846 && hash_code == XUINT (HASH_HASH (h, i))
3847 && h->test.cmpfn (&h->test, key, HASH_KEY (h, i))))
d80c6c11
GM
3848 {
3849 /* Take entry out of collision chain. */
3850 if (NILP (prev))
e83064be 3851 set_hash_index_slot (h, start_of_bucket, HASH_NEXT (h, i));
d80c6c11 3852 else
e83064be 3853 set_hash_next_slot (h, XFASTINT (prev), HASH_NEXT (h, i));
d80c6c11
GM
3854
3855 /* Clear slots in key_and_value and add the slots to
3856 the free list. */
e83064be
DA
3857 set_hash_key_slot (h, i, Qnil);
3858 set_hash_value_slot (h, i, Qnil);
3859 set_hash_hash_slot (h, i, Qnil);
3860 set_hash_next_slot (h, i, h->next_free);
d80c6c11 3861 h->next_free = make_number (i);
878f97ff 3862 h->count--;
a54e2c05 3863 eassert (h->count >= 0);
d80c6c11
GM
3864 break;
3865 }
3866 else
3867 {
3868 prev = idx;
3869 idx = HASH_NEXT (h, i);
3870 }
3871 }
3872}
3873
3874
3875/* Clear hash table H. */
3876
2f7c71a1 3877static void
971de7fb 3878hash_clear (struct Lisp_Hash_Table *h)
d80c6c11 3879{
878f97ff 3880 if (h->count > 0)
d80c6c11 3881 {
d311d28c 3882 ptrdiff_t i, size = HASH_TABLE_SIZE (h);
d80c6c11
GM
3883
3884 for (i = 0; i < size; ++i)
3885 {
e83064be
DA
3886 set_hash_next_slot (h, i, i < size - 1 ? make_number (i + 1) : Qnil);
3887 set_hash_key_slot (h, i, Qnil);
3888 set_hash_value_slot (h, i, Qnil);
3889 set_hash_hash_slot (h, i, Qnil);
d80c6c11
GM
3890 }
3891
7edbb0da 3892 for (i = 0; i < ASIZE (h->index); ++i)
68b587a6 3893 ASET (h->index, i, Qnil);
d80c6c11
GM
3894
3895 h->next_free = make_number (0);
878f97ff 3896 h->count = 0;
d80c6c11
GM
3897 }
3898}
3899
3900
3901\f
3902/************************************************************************
3903 Weak Hash Tables
3904 ************************************************************************/
3905
f75d7a91 3906/* Sweep weak hash table H. REMOVE_ENTRIES_P means remove
a0b581cc 3907 entries from the table that don't survive the current GC.
f75d7a91
PE
3908 !REMOVE_ENTRIES_P means mark entries that are in use. Value is
3909 true if anything was marked. */
a0b581cc 3910
f75d7a91
PE
3911static bool
3912sweep_weak_table (struct Lisp_Hash_Table *h, bool remove_entries_p)
a0b581cc 3913{
d311d28c 3914 ptrdiff_t bucket, n;
f75d7a91 3915 bool marked;
59f953a2 3916
7edbb0da 3917 n = ASIZE (h->index) & ~ARRAY_MARK_FLAG;
a0b581cc 3918 marked = 0;
59f953a2 3919
a0b581cc
GM
3920 for (bucket = 0; bucket < n; ++bucket)
3921 {
1e546714 3922 Lisp_Object idx, next, prev;
a0b581cc
GM
3923
3924 /* Follow collision chain, removing entries that
3925 don't survive this garbage collection. */
a0b581cc 3926 prev = Qnil;
8e50cc2d 3927 for (idx = HASH_INDEX (h, bucket); !NILP (idx); idx = next)
a0b581cc 3928 {
d311d28c 3929 ptrdiff_t i = XFASTINT (idx);
fce31d69
PE
3930 bool key_known_to_survive_p = survives_gc_p (HASH_KEY (h, i));
3931 bool value_known_to_survive_p = survives_gc_p (HASH_VALUE (h, i));
f75d7a91 3932 bool remove_p;
59f953a2 3933
a0b581cc 3934 if (EQ (h->weak, Qkey))
aee625fa 3935 remove_p = !key_known_to_survive_p;
a0b581cc 3936 else if (EQ (h->weak, Qvalue))
aee625fa 3937 remove_p = !value_known_to_survive_p;
ec504e6f 3938 else if (EQ (h->weak, Qkey_or_value))
728c5d9d 3939 remove_p = !(key_known_to_survive_p || value_known_to_survive_p);
ec504e6f 3940 else if (EQ (h->weak, Qkey_and_value))
728c5d9d 3941 remove_p = !(key_known_to_survive_p && value_known_to_survive_p);
a0b581cc 3942 else
1088b922 3943 emacs_abort ();
59f953a2 3944
a0b581cc
GM
3945 next = HASH_NEXT (h, i);
3946
3947 if (remove_entries_p)
3948 {
3949 if (remove_p)
3950 {
3951 /* Take out of collision chain. */
8e50cc2d 3952 if (NILP (prev))
e83064be 3953 set_hash_index_slot (h, bucket, next);
a0b581cc 3954 else
e83064be 3955 set_hash_next_slot (h, XFASTINT (prev), next);
59f953a2 3956
a0b581cc 3957 /* Add to free list. */
e83064be 3958 set_hash_next_slot (h, i, h->next_free);
a0b581cc 3959 h->next_free = idx;
59f953a2 3960
a0b581cc 3961 /* Clear key, value, and hash. */
e83064be
DA
3962 set_hash_key_slot (h, i, Qnil);
3963 set_hash_value_slot (h, i, Qnil);
3964 set_hash_hash_slot (h, i, Qnil);
59f953a2 3965
878f97ff 3966 h->count--;
a0b581cc 3967 }
d278cde0
KS
3968 else
3969 {
3970 prev = idx;
3971 }
a0b581cc
GM
3972 }
3973 else
3974 {
3975 if (!remove_p)
3976 {
3977 /* Make sure key and value survive. */
aee625fa
GM
3978 if (!key_known_to_survive_p)
3979 {
9568e3d8 3980 mark_object (HASH_KEY (h, i));
aee625fa
GM
3981 marked = 1;
3982 }
3983
3984 if (!value_known_to_survive_p)
3985 {
9568e3d8 3986 mark_object (HASH_VALUE (h, i));
aee625fa
GM
3987 marked = 1;
3988 }
a0b581cc
GM
3989 }
3990 }
a0b581cc
GM
3991 }
3992 }
3993
3994 return marked;
3995}
3996
d80c6c11
GM
3997/* Remove elements from weak hash tables that don't survive the
3998 current garbage collection. Remove weak tables that don't survive
3999 from Vweak_hash_tables. Called from gc_sweep. */
4000
4001void
971de7fb 4002sweep_weak_hash_tables (void)
d80c6c11 4003{
6c661ec9 4004 struct Lisp_Hash_Table *h, *used, *next;
f75d7a91 4005 bool marked;
a0b581cc
GM
4006
4007 /* Mark all keys and values that are in use. Keep on marking until
4008 there is no more change. This is necessary for cases like
4009 value-weak table A containing an entry X -> Y, where Y is used in a
4010 key-weak table B, Z -> Y. If B comes after A in the list of weak
4011 tables, X -> Y might be removed from A, although when looking at B
4012 one finds that it shouldn't. */
4013 do
4014 {
4015 marked = 0;
6c661ec9 4016 for (h = weak_hash_tables; h; h = h->next_weak)
a0b581cc 4017 {
eab3844f 4018 if (h->header.size & ARRAY_MARK_FLAG)
a0b581cc
GM
4019 marked |= sweep_weak_table (h, 0);
4020 }
4021 }
4022 while (marked);
d80c6c11 4023
a0b581cc 4024 /* Remove tables and entries that aren't used. */
6c661ec9 4025 for (h = weak_hash_tables, used = NULL; h; h = next)
d80c6c11 4026 {
ac0e96ee 4027 next = h->next_weak;
91f78c99 4028
eab3844f 4029 if (h->header.size & ARRAY_MARK_FLAG)
d80c6c11 4030 {
ac0e96ee 4031 /* TABLE is marked as used. Sweep its contents. */
878f97ff 4032 if (h->count > 0)
a0b581cc 4033 sweep_weak_table (h, 1);
ac0e96ee
GM
4034
4035 /* Add table to the list of used weak hash tables. */
4036 h->next_weak = used;
6c661ec9 4037 used = h;
d80c6c11
GM
4038 }
4039 }
ac0e96ee 4040
6c661ec9 4041 weak_hash_tables = used;
d80c6c11
GM
4042}
4043
4044
4045\f
4046/***********************************************************************
4047 Hash Code Computation
4048 ***********************************************************************/
4049
4050/* Maximum depth up to which to dive into Lisp structures. */
4051
4052#define SXHASH_MAX_DEPTH 3
4053
4054/* Maximum length up to which to take list and vector elements into
4055 account. */
4056
4057#define SXHASH_MAX_LEN 7
4058
3cc5a532
PE
4059/* Return a hash for string PTR which has length LEN. The hash value
4060 can be any EMACS_UINT value. */
d80c6c11 4061
3cc5a532
PE
4062EMACS_UINT
4063hash_string (char const *ptr, ptrdiff_t len)
d80c6c11 4064{
3cc5a532
PE
4065 char const *p = ptr;
4066 char const *end = p + len;
d80c6c11 4067 unsigned char c;
0de4bb68 4068 EMACS_UINT hash = 0;
d80c6c11
GM
4069
4070 while (p != end)
4071 {
4072 c = *p++;
04a2d0d3 4073 hash = sxhash_combine (hash, c);
d80c6c11 4074 }
59f953a2 4075
3cc5a532
PE
4076 return hash;
4077}
4078
4079/* Return a hash for string PTR which has length LEN. The hash
4080 code returned is guaranteed to fit in a Lisp integer. */
4081
4082static EMACS_UINT
4083sxhash_string (char const *ptr, ptrdiff_t len)
4084{
4085 EMACS_UINT hash = hash_string (ptr, len);
0de4bb68 4086 return SXHASH_REDUCE (hash);
d80c6c11
GM
4087}
4088
0de4bb68
PE
4089/* Return a hash for the floating point value VAL. */
4090
eff1c190 4091static EMACS_UINT
0de4bb68
PE
4092sxhash_float (double val)
4093{
4094 EMACS_UINT hash = 0;
4095 enum {
4096 WORDS_PER_DOUBLE = (sizeof val / sizeof hash
4097 + (sizeof val % sizeof hash != 0))
4098 };
4099 union {
4100 double val;
4101 EMACS_UINT word[WORDS_PER_DOUBLE];
4102 } u;
4103 int i;
4104 u.val = val;
4105 memset (&u.val + 1, 0, sizeof u - sizeof u.val);
4106 for (i = 0; i < WORDS_PER_DOUBLE; i++)
04a2d0d3 4107 hash = sxhash_combine (hash, u.word[i]);
0de4bb68
PE
4108 return SXHASH_REDUCE (hash);
4109}
d80c6c11
GM
4110
4111/* Return a hash for list LIST. DEPTH is the current depth in the
4112 list. We don't recurse deeper than SXHASH_MAX_DEPTH in it. */
4113
0de4bb68 4114static EMACS_UINT
971de7fb 4115sxhash_list (Lisp_Object list, int depth)
d80c6c11 4116{
0de4bb68 4117 EMACS_UINT hash = 0;
d80c6c11 4118 int i;
59f953a2 4119
d80c6c11
GM
4120 if (depth < SXHASH_MAX_DEPTH)
4121 for (i = 0;
4122 CONSP (list) && i < SXHASH_MAX_LEN;
4123 list = XCDR (list), ++i)
4124 {
0de4bb68 4125 EMACS_UINT hash2 = sxhash (XCAR (list), depth + 1);
04a2d0d3 4126 hash = sxhash_combine (hash, hash2);
d80c6c11
GM
4127 }
4128
ea284f33
KS
4129 if (!NILP (list))
4130 {
0de4bb68 4131 EMACS_UINT hash2 = sxhash (list, depth + 1);
04a2d0d3 4132 hash = sxhash_combine (hash, hash2);
ea284f33
KS
4133 }
4134
0de4bb68 4135 return SXHASH_REDUCE (hash);
d80c6c11
GM
4136}
4137
4138
4139/* Return a hash for vector VECTOR. DEPTH is the current depth in
4140 the Lisp structure. */
4141
0de4bb68 4142static EMACS_UINT
971de7fb 4143sxhash_vector (Lisp_Object vec, int depth)
d80c6c11 4144{
0de4bb68 4145 EMACS_UINT hash = ASIZE (vec);
d80c6c11
GM
4146 int i, n;
4147
7edbb0da 4148 n = min (SXHASH_MAX_LEN, ASIZE (vec));
d80c6c11
GM
4149 for (i = 0; i < n; ++i)
4150 {
0de4bb68 4151 EMACS_UINT hash2 = sxhash (AREF (vec, i), depth + 1);
04a2d0d3 4152 hash = sxhash_combine (hash, hash2);
d80c6c11
GM
4153 }
4154
0de4bb68 4155 return SXHASH_REDUCE (hash);
d80c6c11
GM
4156}
4157
d80c6c11
GM
4158/* Return a hash for bool-vector VECTOR. */
4159
0de4bb68 4160static EMACS_UINT
971de7fb 4161sxhash_bool_vector (Lisp_Object vec)
d80c6c11 4162{
1c0a7493
PE
4163 EMACS_INT size = bool_vector_size (vec);
4164 EMACS_UINT hash = size;
d80c6c11
GM
4165 int i, n;
4166
df5b4930 4167 n = min (SXHASH_MAX_LEN, bool_vector_words (size));
d80c6c11 4168 for (i = 0; i < n; ++i)
df5b4930 4169 hash = sxhash_combine (hash, bool_vector_data (vec)[i]);
d80c6c11 4170
0de4bb68 4171 return SXHASH_REDUCE (hash);
d80c6c11
GM
4172}
4173
4174
4175/* Return a hash code for OBJ. DEPTH is the current depth in the Lisp
6b61353c 4176 structure. Value is an unsigned integer clipped to INTMASK. */
d80c6c11 4177
0de4bb68 4178EMACS_UINT
971de7fb 4179sxhash (Lisp_Object obj, int depth)
d80c6c11 4180{
0de4bb68 4181 EMACS_UINT hash;
d80c6c11
GM
4182
4183 if (depth > SXHASH_MAX_DEPTH)
4184 return 0;
59f953a2 4185
d80c6c11
GM
4186 switch (XTYPE (obj))
4187 {
2de9f71c 4188 case_Lisp_Int:
d80c6c11
GM
4189 hash = XUINT (obj);
4190 break;
4191
d80c6c11 4192 case Lisp_Misc:
61ddb1b9 4193 hash = XHASH (obj);
d80c6c11
GM
4194 break;
4195
32bfb2d5
EZ
4196 case Lisp_Symbol:
4197 obj = SYMBOL_NAME (obj);
4198 /* Fall through. */
4199
d80c6c11 4200 case Lisp_String:
3cc5a532 4201 hash = sxhash_string (SSDATA (obj), SBYTES (obj));
d80c6c11
GM
4202 break;
4203
4204 /* This can be everything from a vector to an overlay. */
4205 case Lisp_Vectorlike:
4206 if (VECTORP (obj))
4207 /* According to the CL HyperSpec, two arrays are equal only if
4208 they are `eq', except for strings and bit-vectors. In
4209 Emacs, this works differently. We have to compare element
4210 by element. */
4211 hash = sxhash_vector (obj, depth);
4212 else if (BOOL_VECTOR_P (obj))
4213 hash = sxhash_bool_vector (obj);
4214 else
4215 /* Others are `equal' if they are `eq', so let's take their
4216 address as hash. */
61ddb1b9 4217 hash = XHASH (obj);
d80c6c11
GM
4218 break;
4219
4220 case Lisp_Cons:
4221 hash = sxhash_list (obj, depth);
4222 break;
4223
4224 case Lisp_Float:
0de4bb68
PE
4225 hash = sxhash_float (XFLOAT_DATA (obj));
4226 break;
d80c6c11
GM
4227
4228 default:
1088b922 4229 emacs_abort ();
d80c6c11
GM
4230 }
4231
0de4bb68 4232 return hash;
d80c6c11
GM
4233}
4234
4235
4236\f
4237/***********************************************************************
4238 Lisp Interface
4239 ***********************************************************************/
4240
4241
4242DEFUN ("sxhash", Fsxhash, Ssxhash, 1, 1, 0,
e9d8ddc9 4243 doc: /* Compute a hash code for OBJ and return it as integer. */)
5842a27b 4244 (Lisp_Object obj)
d80c6c11 4245{
0de4bb68 4246 EMACS_UINT hash = sxhash (obj, 0);
d80c6c11
GM
4247 return make_number (hash);
4248}
4249
4250
a7ca3326 4251DEFUN ("make-hash-table", Fmake_hash_table, Smake_hash_table, 0, MANY, 0,
e9d8ddc9 4252 doc: /* Create and return a new hash table.
91f78c99 4253
47cebab1
GM
4254Arguments are specified as keyword/argument pairs. The following
4255arguments are defined:
4256
4257:test TEST -- TEST must be a symbol that specifies how to compare
4258keys. Default is `eql'. Predefined are the tests `eq', `eql', and
4259`equal'. User-supplied test and hash functions can be specified via
4260`define-hash-table-test'.
4261
4262:size SIZE -- A hint as to how many elements will be put in the table.
4263Default is 65.
4264
4265:rehash-size REHASH-SIZE - Indicates how to expand the table when it
79d6f59e
CY
4266fills up. If REHASH-SIZE is an integer, increase the size by that
4267amount. If it is a float, it must be > 1.0, and the new size is the
4268old size multiplied by that factor. Default is 1.5.
47cebab1
GM
4269
4270:rehash-threshold THRESHOLD -- THRESHOLD must a float > 0, and <= 1.0.
b756c005 4271Resize the hash table when the ratio (number of entries / table size)
e1025755 4272is greater than or equal to THRESHOLD. Default is 0.8.
47cebab1
GM
4273
4274:weakness WEAK -- WEAK must be one of nil, t, `key', `value',
4275`key-or-value', or `key-and-value'. If WEAK is not nil, the table
4276returned is a weak table. Key/value pairs are removed from a weak
4277hash table when there are no non-weak references pointing to their
4278key, value, one of key or value, or both key and value, depending on
4279WEAK. WEAK t is equivalent to `key-and-value'. Default value of WEAK
4bf8e2a3
MB
4280is nil.
4281
4282usage: (make-hash-table &rest KEYWORD-ARGS) */)
f66c7cf8 4283 (ptrdiff_t nargs, Lisp_Object *args)
d80c6c11
GM
4284{
4285 Lisp_Object test, size, rehash_size, rehash_threshold, weak;
b7432bb2 4286 struct hash_table_test testdesc;
d80c6c11 4287 char *used;
f66c7cf8 4288 ptrdiff_t i;
d80c6c11
GM
4289
4290 /* The vector `used' is used to keep track of arguments that
4291 have been consumed. */
38182d90 4292 used = alloca (nargs * sizeof *used);
72af86bd 4293 memset (used, 0, nargs * sizeof *used);
d80c6c11
GM
4294
4295 /* See if there's a `:test TEST' among the arguments. */
4296 i = get_key_arg (QCtest, nargs, args, used);
c5101a77 4297 test = i ? args[i] : Qeql;
b7432bb2
SM
4298 if (EQ (test, Qeq))
4299 testdesc = hashtest_eq;
4300 else if (EQ (test, Qeql))
4301 testdesc = hashtest_eql;
4302 else if (EQ (test, Qequal))
4303 testdesc = hashtest_equal;
4304 else
d80c6c11
GM
4305 {
4306 /* See if it is a user-defined test. */
4307 Lisp_Object prop;
59f953a2 4308
d80c6c11 4309 prop = Fget (test, Qhash_table_test);
c1dd95fc 4310 if (!CONSP (prop) || !CONSP (XCDR (prop)))
692ae65c 4311 signal_error ("Invalid hash table test", test);
b7432bb2
SM
4312 testdesc.name = test;
4313 testdesc.user_cmp_function = XCAR (prop);
4314 testdesc.user_hash_function = XCAR (XCDR (prop));
4315 testdesc.hashfn = hashfn_user_defined;
4316 testdesc.cmpfn = cmpfn_user_defined;
d80c6c11 4317 }
d80c6c11
GM
4318
4319 /* See if there's a `:size SIZE' argument. */
4320 i = get_key_arg (QCsize, nargs, args, used);
c5101a77 4321 size = i ? args[i] : Qnil;
cf42cb72
SM
4322 if (NILP (size))
4323 size = make_number (DEFAULT_HASH_SIZE);
4324 else if (!INTEGERP (size) || XINT (size) < 0)
692ae65c 4325 signal_error ("Invalid hash table size", size);
d80c6c11
GM
4326
4327 /* Look for `:rehash-size SIZE'. */
4328 i = get_key_arg (QCrehash_size, nargs, args, used);
c5101a77 4329 rehash_size = i ? args[i] : make_float (DEFAULT_REHASH_SIZE);
0de4bb68
PE
4330 if (! ((INTEGERP (rehash_size) && 0 < XINT (rehash_size))
4331 || (FLOATP (rehash_size) && 1 < XFLOAT_DATA (rehash_size))))
692ae65c 4332 signal_error ("Invalid hash table rehash size", rehash_size);
59f953a2 4333
d80c6c11
GM
4334 /* Look for `:rehash-threshold THRESHOLD'. */
4335 i = get_key_arg (QCrehash_threshold, nargs, args, used);
c5101a77 4336 rehash_threshold = i ? args[i] : make_float (DEFAULT_REHASH_THRESHOLD);
0de4bb68
PE
4337 if (! (FLOATP (rehash_threshold)
4338 && 0 < XFLOAT_DATA (rehash_threshold)
4339 && XFLOAT_DATA (rehash_threshold) <= 1))
692ae65c 4340 signal_error ("Invalid hash table rehash threshold", rehash_threshold);
59f953a2 4341
ee0403b3
GM
4342 /* Look for `:weakness WEAK'. */
4343 i = get_key_arg (QCweakness, nargs, args, used);
c5101a77 4344 weak = i ? args[i] : Qnil;
ec504e6f
GM
4345 if (EQ (weak, Qt))
4346 weak = Qkey_and_value;
d80c6c11 4347 if (!NILP (weak)
f899c503 4348 && !EQ (weak, Qkey)
ec504e6f
GM
4349 && !EQ (weak, Qvalue)
4350 && !EQ (weak, Qkey_or_value)
4351 && !EQ (weak, Qkey_and_value))
692ae65c 4352 signal_error ("Invalid hash table weakness", weak);
59f953a2 4353
d80c6c11
GM
4354 /* Now, all args should have been used up, or there's a problem. */
4355 for (i = 0; i < nargs; ++i)
4356 if (!used[i])
692ae65c 4357 signal_error ("Invalid argument list", args[i]);
d80c6c11 4358
b7432bb2 4359 return make_hash_table (testdesc, size, rehash_size, rehash_threshold, weak);
d80c6c11
GM
4360}
4361
4362
f899c503 4363DEFUN ("copy-hash-table", Fcopy_hash_table, Scopy_hash_table, 1, 1, 0,
e9d8ddc9 4364 doc: /* Return a copy of hash table TABLE. */)
5842a27b 4365 (Lisp_Object table)
f899c503
GM
4366{
4367 return copy_hash_table (check_hash_table (table));
4368}
4369
4370
d80c6c11 4371DEFUN ("hash-table-count", Fhash_table_count, Shash_table_count, 1, 1, 0,
e9d8ddc9 4372 doc: /* Return the number of elements in TABLE. */)
5842a27b 4373 (Lisp_Object table)
d80c6c11 4374{
878f97ff 4375 return make_number (check_hash_table (table)->count);
d80c6c11
GM
4376}
4377
59f953a2 4378
d80c6c11
GM
4379DEFUN ("hash-table-rehash-size", Fhash_table_rehash_size,
4380 Shash_table_rehash_size, 1, 1, 0,
e9d8ddc9 4381 doc: /* Return the current rehash size of TABLE. */)
5842a27b 4382 (Lisp_Object table)
d80c6c11
GM
4383{
4384 return check_hash_table (table)->rehash_size;
4385}
59f953a2 4386
d80c6c11
GM
4387
4388DEFUN ("hash-table-rehash-threshold", Fhash_table_rehash_threshold,
4389 Shash_table_rehash_threshold, 1, 1, 0,
e9d8ddc9 4390 doc: /* Return the current rehash threshold of TABLE. */)
5842a27b 4391 (Lisp_Object table)
d80c6c11
GM
4392{
4393 return check_hash_table (table)->rehash_threshold;
4394}
59f953a2 4395
d80c6c11
GM
4396
4397DEFUN ("hash-table-size", Fhash_table_size, Shash_table_size, 1, 1, 0,
e9d8ddc9 4398 doc: /* Return the size of TABLE.
47cebab1 4399The size can be used as an argument to `make-hash-table' to create
b756c005 4400a hash table than can hold as many elements as TABLE holds
e9d8ddc9 4401without need for resizing. */)
5842a27b 4402 (Lisp_Object table)
d80c6c11
GM
4403{
4404 struct Lisp_Hash_Table *h = check_hash_table (table);
4405 return make_number (HASH_TABLE_SIZE (h));
4406}
59f953a2 4407
d80c6c11
GM
4408
4409DEFUN ("hash-table-test", Fhash_table_test, Shash_table_test, 1, 1, 0,
e9d8ddc9 4410 doc: /* Return the test TABLE uses. */)
5842a27b 4411 (Lisp_Object table)
d80c6c11 4412{
b7432bb2 4413 return check_hash_table (table)->test.name;
d80c6c11
GM
4414}
4415
59f953a2 4416
e84b1dea
GM
4417DEFUN ("hash-table-weakness", Fhash_table_weakness, Shash_table_weakness,
4418 1, 1, 0,
e9d8ddc9 4419 doc: /* Return the weakness of TABLE. */)
5842a27b 4420 (Lisp_Object table)
d80c6c11
GM
4421{
4422 return check_hash_table (table)->weak;
4423}
4424
59f953a2 4425
d80c6c11 4426DEFUN ("hash-table-p", Fhash_table_p, Shash_table_p, 1, 1, 0,
e9d8ddc9 4427 doc: /* Return t if OBJ is a Lisp hash table object. */)
5842a27b 4428 (Lisp_Object obj)
d80c6c11
GM
4429{
4430 return HASH_TABLE_P (obj) ? Qt : Qnil;
4431}
4432
4433
4434DEFUN ("clrhash", Fclrhash, Sclrhash, 1, 1, 0,
ccd8f7fe 4435 doc: /* Clear hash table TABLE and return it. */)
5842a27b 4436 (Lisp_Object table)
d80c6c11
GM
4437{
4438 hash_clear (check_hash_table (table));
ccd8f7fe
TTN
4439 /* Be compatible with XEmacs. */
4440 return table;
d80c6c11
GM
4441}
4442
4443
a7ca3326 4444DEFUN ("gethash", Fgethash, Sgethash, 2, 3, 0,
e9d8ddc9
MB
4445 doc: /* Look up KEY in TABLE and return its associated value.
4446If KEY is not found, return DFLT which defaults to nil. */)
5842a27b 4447 (Lisp_Object key, Lisp_Object table, Lisp_Object dflt)
d80c6c11
GM
4448{
4449 struct Lisp_Hash_Table *h = check_hash_table (table);
d3411f89 4450 ptrdiff_t i = hash_lookup (h, key, NULL);
d80c6c11
GM
4451 return i >= 0 ? HASH_VALUE (h, i) : dflt;
4452}
4453
4454
a7ca3326 4455DEFUN ("puthash", Fputhash, Sputhash, 3, 3, 0,
e9d8ddc9 4456 doc: /* Associate KEY with VALUE in hash table TABLE.
47cebab1 4457If KEY is already present in table, replace its current value with
a54e3482 4458VALUE. In any case, return VALUE. */)
5842a27b 4459 (Lisp_Object key, Lisp_Object value, Lisp_Object table)
d80c6c11
GM
4460{
4461 struct Lisp_Hash_Table *h = check_hash_table (table);
d3411f89 4462 ptrdiff_t i;
0de4bb68 4463 EMACS_UINT hash;
d80c6c11
GM
4464
4465 i = hash_lookup (h, key, &hash);
4466 if (i >= 0)
e83064be 4467 set_hash_value_slot (h, i, value);
d80c6c11
GM
4468 else
4469 hash_put (h, key, value, hash);
59f953a2 4470
d9c4f922 4471 return value;
d80c6c11
GM
4472}
4473
4474
a7ca3326 4475DEFUN ("remhash", Fremhash, Sremhash, 2, 2, 0,
e9d8ddc9 4476 doc: /* Remove KEY from TABLE. */)
5842a27b 4477 (Lisp_Object key, Lisp_Object table)
d80c6c11
GM
4478{
4479 struct Lisp_Hash_Table *h = check_hash_table (table);
5a2d7ab6 4480 hash_remove_from_table (h, key);
d80c6c11
GM
4481 return Qnil;
4482}
4483
4484
4485DEFUN ("maphash", Fmaphash, Smaphash, 2, 2, 0,
e9d8ddc9 4486 doc: /* Call FUNCTION for all entries in hash table TABLE.
c14ec135 4487FUNCTION is called with two arguments, KEY and VALUE. */)
5842a27b 4488 (Lisp_Object function, Lisp_Object table)
d80c6c11
GM
4489{
4490 struct Lisp_Hash_Table *h = check_hash_table (table);
4491 Lisp_Object args[3];
d311d28c 4492 ptrdiff_t i;
d80c6c11
GM
4493
4494 for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
4495 if (!NILP (HASH_HASH (h, i)))
4496 {
4497 args[0] = function;
4498 args[1] = HASH_KEY (h, i);
4499 args[2] = HASH_VALUE (h, i);
4500 Ffuncall (3, args);
4501 }
59f953a2 4502
d80c6c11
GM
4503 return Qnil;
4504}
4505
4506
4507DEFUN ("define-hash-table-test", Fdefine_hash_table_test,
4508 Sdefine_hash_table_test, 3, 3, 0,
e9d8ddc9 4509 doc: /* Define a new hash table test with name NAME, a symbol.
91f78c99 4510
47cebab1
GM
4511In hash tables created with NAME specified as test, use TEST to
4512compare keys, and HASH for computing hash codes of keys.
4513
4514TEST must be a function taking two arguments and returning non-nil if
4515both arguments are the same. HASH must be a function taking one
79804536
SM
4516argument and returning an object that is the hash code of the argument.
4517It should be the case that if (eq (funcall HASH x1) (funcall HASH x2))
4518returns nil, then (funcall TEST x1 x2) also returns nil. */)
5842a27b 4519 (Lisp_Object name, Lisp_Object test, Lisp_Object hash)
d80c6c11
GM
4520{
4521 return Fput (name, Qhash_table_test, list2 (test, hash));
4522}
4523
a3b210c4 4524
57916a7a 4525\f
5c302da4 4526/************************************************************************
7f3f739f 4527 MD5, SHA-1, and SHA-2
5c302da4
GM
4528 ************************************************************************/
4529
57916a7a 4530#include "md5.h"
e1b90ef6 4531#include "sha1.h"
7f3f739f
LL
4532#include "sha256.h"
4533#include "sha512.h"
57916a7a 4534
7f3f739f 4535/* ALGORITHM is a symbol: md5, sha1, sha224 and so on. */
47cebab1 4536
f1b54466 4537static Lisp_Object
7f3f739f 4538secure_hash (Lisp_Object algorithm, Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object noerror, Lisp_Object binary)
57916a7a 4539{
57916a7a 4540 int i;
d311d28c 4541 ptrdiff_t size;
e6d4aefa 4542 EMACS_INT start_char = 0, end_char = 0;
d311d28c 4543 ptrdiff_t start_byte, end_byte;
e6d4aefa 4544 register EMACS_INT b, e;
57916a7a 4545 register struct buffer *bp;
e6d4aefa 4546 EMACS_INT temp;
7f3f739f
LL
4547 int digest_size;
4548 void *(*hash_func) (const char *, size_t, void *);
4549 Lisp_Object digest;
4550
4551 CHECK_SYMBOL (algorithm);
57916a7a 4552
5c302da4 4553 if (STRINGP (object))
57916a7a
GM
4554 {
4555 if (NILP (coding_system))
4556 {
5c302da4 4557 /* Decide the coding-system to encode the data with. */
57916a7a 4558
5c302da4
GM
4559 if (STRING_MULTIBYTE (object))
4560 /* use default, we can't guess correct value */
38583a69 4561 coding_system = preferred_coding_system ();
91f78c99 4562 else
5c302da4 4563 coding_system = Qraw_text;
57916a7a 4564 }
91f78c99 4565
5c302da4 4566 if (NILP (Fcoding_system_p (coding_system)))
57916a7a 4567 {
5c302da4 4568 /* Invalid coding system. */
91f78c99 4569
5c302da4
GM
4570 if (!NILP (noerror))
4571 coding_system = Qraw_text;
4572 else
692ae65c 4573 xsignal1 (Qcoding_system_error, coding_system);
57916a7a
GM
4574 }
4575
5c302da4 4576 if (STRING_MULTIBYTE (object))
38583a69 4577 object = code_convert_string (object, coding_system, Qnil, 1, 0, 1);
5c302da4 4578
d5db4077 4579 size = SCHARS (object);
57916a7a
GM
4580
4581 if (!NILP (start))
4582 {
b7826503 4583 CHECK_NUMBER (start);
57916a7a
GM
4584
4585 start_char = XINT (start);
4586
4587 if (start_char < 0)
4588 start_char += size;
57916a7a
GM
4589 }
4590
4591 if (NILP (end))
d311d28c 4592 end_char = size;
57916a7a
GM
4593 else
4594 {
b7826503 4595 CHECK_NUMBER (end);
91f78c99 4596
57916a7a
GM
4597 end_char = XINT (end);
4598
4599 if (end_char < 0)
4600 end_char += size;
57916a7a 4601 }
91f78c99 4602
57916a7a
GM
4603 if (!(0 <= start_char && start_char <= end_char && end_char <= size))
4604 args_out_of_range_3 (object, make_number (start_char),
4605 make_number (end_char));
d311d28c
PE
4606
4607 start_byte = NILP (start) ? 0 : string_char_to_byte (object, start_char);
4608 end_byte =
4609 NILP (end) ? SBYTES (object) : string_char_to_byte (object, end_char);
57916a7a
GM
4610 }
4611 else
4612 {
6b61353c
KH
4613 struct buffer *prev = current_buffer;
4614
66322887 4615 record_unwind_current_buffer ();
6b61353c 4616
b7826503 4617 CHECK_BUFFER (object);
57916a7a
GM
4618
4619 bp = XBUFFER (object);
a3d794a1 4620 set_buffer_internal (bp);
91f78c99 4621
57916a7a 4622 if (NILP (start))
6b61353c 4623 b = BEGV;
57916a7a
GM
4624 else
4625 {
b7826503 4626 CHECK_NUMBER_COERCE_MARKER (start);
57916a7a
GM
4627 b = XINT (start);
4628 }
4629
4630 if (NILP (end))
6b61353c 4631 e = ZV;
57916a7a
GM
4632 else
4633 {
b7826503 4634 CHECK_NUMBER_COERCE_MARKER (end);
57916a7a
GM
4635 e = XINT (end);
4636 }
91f78c99 4637
57916a7a
GM
4638 if (b > e)
4639 temp = b, b = e, e = temp;
91f78c99 4640
6b61353c 4641 if (!(BEGV <= b && e <= ZV))
57916a7a 4642 args_out_of_range (start, end);
91f78c99 4643
57916a7a
GM
4644 if (NILP (coding_system))
4645 {
91f78c99 4646 /* Decide the coding-system to encode the data with.
5c302da4
GM
4647 See fileio.c:Fwrite-region */
4648
4649 if (!NILP (Vcoding_system_for_write))
4650 coding_system = Vcoding_system_for_write;
4651 else
4652 {
f75d7a91 4653 bool force_raw_text = 0;
5c302da4 4654
4b4deea2 4655 coding_system = BVAR (XBUFFER (object), buffer_file_coding_system);
5c302da4
GM
4656 if (NILP (coding_system)
4657 || NILP (Flocal_variable_p (Qbuffer_file_coding_system, Qnil)))
4658 {
4659 coding_system = Qnil;
4b4deea2 4660 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
5c302da4
GM
4661 force_raw_text = 1;
4662 }
4663
5e617bc2 4664 if (NILP (coding_system) && !NILP (Fbuffer_file_name (object)))
5c302da4
GM
4665 {
4666 /* Check file-coding-system-alist. */
4667 Lisp_Object args[4], val;
91f78c99 4668
5c302da4 4669 args[0] = Qwrite_region; args[1] = start; args[2] = end;
5e617bc2 4670 args[3] = Fbuffer_file_name (object);
5c302da4
GM
4671 val = Ffind_operation_coding_system (4, args);
4672 if (CONSP (val) && !NILP (XCDR (val)))
4673 coding_system = XCDR (val);
4674 }
4675
4676 if (NILP (coding_system)
4b4deea2 4677 && !NILP (BVAR (XBUFFER (object), buffer_file_coding_system)))
5c302da4
GM
4678 {
4679 /* If we still have not decided a coding system, use the
4680 default value of buffer-file-coding-system. */
4b4deea2 4681 coding_system = BVAR (XBUFFER (object), buffer_file_coding_system);
5c302da4
GM
4682 }
4683
4684 if (!force_raw_text
4685 && !NILP (Ffboundp (Vselect_safe_coding_system_function)))
4686 /* Confirm that VAL can surely encode the current region. */
1e59646d 4687 coding_system = call4 (Vselect_safe_coding_system_function,
70da6a76 4688 make_number (b), make_number (e),
1e59646d 4689 coding_system, Qnil);
5c302da4
GM
4690
4691 if (force_raw_text)
4692 coding_system = Qraw_text;
4693 }
4694
4695 if (NILP (Fcoding_system_p (coding_system)))
57916a7a 4696 {
5c302da4
GM
4697 /* Invalid coding system. */
4698
4699 if (!NILP (noerror))
4700 coding_system = Qraw_text;
4701 else
692ae65c 4702 xsignal1 (Qcoding_system_error, coding_system);
57916a7a
GM
4703 }
4704 }
4705
4706 object = make_buffer_string (b, e, 0);
a3d794a1 4707 set_buffer_internal (prev);
6b61353c
KH
4708 /* Discard the unwind protect for recovering the current
4709 buffer. */
4710 specpdl_ptr--;
57916a7a
GM
4711
4712 if (STRING_MULTIBYTE (object))
8f924df7 4713 object = code_convert_string (object, coding_system, Qnil, 1, 0, 0);
d311d28c
PE
4714 start_byte = 0;
4715 end_byte = SBYTES (object);
57916a7a
GM
4716 }
4717
7f3f739f 4718 if (EQ (algorithm, Qmd5))
e1b90ef6 4719 {
7f3f739f
LL
4720 digest_size = MD5_DIGEST_SIZE;
4721 hash_func = md5_buffer;
4722 }
4723 else if (EQ (algorithm, Qsha1))
4724 {
4725 digest_size = SHA1_DIGEST_SIZE;
4726 hash_func = sha1_buffer;
4727 }
4728 else if (EQ (algorithm, Qsha224))
4729 {
4730 digest_size = SHA224_DIGEST_SIZE;
4731 hash_func = sha224_buffer;
4732 }
4733 else if (EQ (algorithm, Qsha256))
4734 {
4735 digest_size = SHA256_DIGEST_SIZE;
4736 hash_func = sha256_buffer;
4737 }
4738 else if (EQ (algorithm, Qsha384))
4739 {
4740 digest_size = SHA384_DIGEST_SIZE;
4741 hash_func = sha384_buffer;
4742 }
4743 else if (EQ (algorithm, Qsha512))
4744 {
4745 digest_size = SHA512_DIGEST_SIZE;
4746 hash_func = sha512_buffer;
4747 }
4748 else
4749 error ("Invalid algorithm arg: %s", SDATA (Fsymbol_name (algorithm)));
57916a7a 4750
7f3f739f
LL
4751 /* allocate 2 x digest_size so that it can be re-used to hold the
4752 hexified value */
4753 digest = make_uninit_string (digest_size * 2);
57916a7a 4754
7f3f739f 4755 hash_func (SSDATA (object) + start_byte,
d311d28c 4756 end_byte - start_byte,
7f3f739f 4757 SSDATA (digest));
e1b90ef6 4758
7f3f739f
LL
4759 if (NILP (binary))
4760 {
4761 unsigned char *p = SDATA (digest);
4762 for (i = digest_size - 1; i >= 0; i--)
4763 {
4764 static char const hexdigit[16] = "0123456789abcdef";
4765 int p_i = p[i];
4766 p[2 * i] = hexdigit[p_i >> 4];
4767 p[2 * i + 1] = hexdigit[p_i & 0xf];
4768 }
4769 return digest;
4770 }
4771 else
a9041e6c 4772 return make_unibyte_string (SSDATA (digest), digest_size);
e1b90ef6
LL
4773}
4774
4775DEFUN ("md5", Fmd5, Smd5, 1, 5, 0,
4776 doc: /* Return MD5 message digest of OBJECT, a buffer or string.
4777
4778A message digest is a cryptographic checksum of a document, and the
4779algorithm to calculate it is defined in RFC 1321.
4780
4781The two optional arguments START and END are character positions
4782specifying for which part of OBJECT the message digest should be
4783computed. If nil or omitted, the digest is computed for the whole
4784OBJECT.
4785
4786The MD5 message digest is computed from the result of encoding the
4787text in a coding system, not directly from the internal Emacs form of
4788the text. The optional fourth argument CODING-SYSTEM specifies which
4789coding system to encode the text with. It should be the same coding
4790system that you used or will use when actually writing the text into a
4791file.
4792
4793If CODING-SYSTEM is nil or omitted, the default depends on OBJECT. If
4794OBJECT is a buffer, the default for CODING-SYSTEM is whatever coding
4795system would be chosen by default for writing this text into a file.
4796
4797If OBJECT is a string, the most preferred coding system (see the
4798command `prefer-coding-system') is used.
4799
4800If NOERROR is non-nil, silently assume the `raw-text' coding if the
4801guesswork fails. Normally, an error is signaled in such case. */)
4802 (Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object noerror)
4803{
7f3f739f 4804 return secure_hash (Qmd5, object, start, end, coding_system, noerror, Qnil);
e1b90ef6
LL
4805}
4806
7f3f739f 4807DEFUN ("secure-hash", Fsecure_hash, Ssecure_hash, 2, 5, 0,
49241268
GM
4808 doc: /* Return the secure hash of OBJECT, a buffer or string.
4809ALGORITHM is a symbol specifying the hash to use:
4810md5, sha1, sha224, sha256, sha384 or sha512.
4811
4812The two optional arguments START and END are positions specifying for
4813which part of OBJECT to compute the hash. If nil or omitted, uses the
4814whole OBJECT.
4815
4816If BINARY is non-nil, returns a string in binary form. */)
7f3f739f 4817 (Lisp_Object algorithm, Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object binary)
e1b90ef6 4818{
7f3f739f 4819 return secure_hash (algorithm, object, start, end, Qnil, Qnil, binary);
57916a7a 4820}
24c129e4 4821\f
dfcf069d 4822void
971de7fb 4823syms_of_fns (void)
7b863bd5 4824{
7f3f739f
LL
4825 DEFSYM (Qmd5, "md5");
4826 DEFSYM (Qsha1, "sha1");
4827 DEFSYM (Qsha224, "sha224");
4828 DEFSYM (Qsha256, "sha256");
4829 DEFSYM (Qsha384, "sha384");
4830 DEFSYM (Qsha512, "sha512");
4831
d80c6c11 4832 /* Hash table stuff. */
cd3520a4
JB
4833 DEFSYM (Qhash_table_p, "hash-table-p");
4834 DEFSYM (Qeq, "eq");
4835 DEFSYM (Qeql, "eql");
4836 DEFSYM (Qequal, "equal");
4837 DEFSYM (QCtest, ":test");
4838 DEFSYM (QCsize, ":size");
4839 DEFSYM (QCrehash_size, ":rehash-size");
4840 DEFSYM (QCrehash_threshold, ":rehash-threshold");
4841 DEFSYM (QCweakness, ":weakness");
4842 DEFSYM (Qkey, "key");
4843 DEFSYM (Qvalue, "value");
4844 DEFSYM (Qhash_table_test, "hash-table-test");
4845 DEFSYM (Qkey_or_value, "key-or-value");
4846 DEFSYM (Qkey_and_value, "key-and-value");
d80c6c11
GM
4847
4848 defsubr (&Ssxhash);
4849 defsubr (&Smake_hash_table);
f899c503 4850 defsubr (&Scopy_hash_table);
d80c6c11
GM
4851 defsubr (&Shash_table_count);
4852 defsubr (&Shash_table_rehash_size);
4853 defsubr (&Shash_table_rehash_threshold);
4854 defsubr (&Shash_table_size);
4855 defsubr (&Shash_table_test);
e84b1dea 4856 defsubr (&Shash_table_weakness);
d80c6c11
GM
4857 defsubr (&Shash_table_p);
4858 defsubr (&Sclrhash);
4859 defsubr (&Sgethash);
4860 defsubr (&Sputhash);
4861 defsubr (&Sremhash);
4862 defsubr (&Smaphash);
4863 defsubr (&Sdefine_hash_table_test);
59f953a2 4864
cd3520a4
JB
4865 DEFSYM (Qstring_lessp, "string-lessp");
4866 DEFSYM (Qprovide, "provide");
4867 DEFSYM (Qrequire, "require");
4868 DEFSYM (Qyes_or_no_p_history, "yes-or-no-p-history");
4869 DEFSYM (Qcursor_in_echo_area, "cursor-in-echo-area");
4870 DEFSYM (Qwidget_type, "widget-type");
7b863bd5 4871
09ab3c3b
KH
4872 staticpro (&string_char_byte_cache_string);
4873 string_char_byte_cache_string = Qnil;
4874
1f79789d
RS
4875 require_nesting_list = Qnil;
4876 staticpro (&require_nesting_list);
4877
52a9879b
RS
4878 Fset (Qyes_or_no_p_history, Qnil);
4879
29208e82 4880 DEFVAR_LISP ("features", Vfeatures,
4774b68e 4881 doc: /* A list of symbols which are the features of the executing Emacs.
47cebab1 4882Used by `featurep' and `require', and altered by `provide'. */);
6c6f1994 4883 Vfeatures = list1 (intern_c_string ("emacs"));
cd3520a4 4884 DEFSYM (Qsubfeatures, "subfeatures");
de0503df 4885 DEFSYM (Qfuncall, "funcall");
7b863bd5 4886
dec002ca 4887#ifdef HAVE_LANGINFO_CODESET
cd3520a4
JB
4888 DEFSYM (Qcodeset, "codeset");
4889 DEFSYM (Qdays, "days");
4890 DEFSYM (Qmonths, "months");
4891 DEFSYM (Qpaper, "paper");
dec002ca
DL
4892#endif /* HAVE_LANGINFO_CODESET */
4893
29208e82 4894 DEFVAR_BOOL ("use-dialog-box", use_dialog_box,
fb7ada5f 4895 doc: /* Non-nil means mouse commands use dialog boxes to ask questions.
436fa78b 4896This applies to `y-or-n-p' and `yes-or-no-p' questions asked by commands
7e861e0d
CY
4897invoked by mouse clicks and mouse menu items.
4898
4899On some platforms, file selection dialogs are also enabled if this is
4900non-nil. */);
bdd8d692
RS
4901 use_dialog_box = 1;
4902
29208e82 4903 DEFVAR_BOOL ("use-file-dialog", use_file_dialog,
fb7ada5f 4904 doc: /* Non-nil means mouse commands use a file dialog to ask for files.
1f1d0797 4905This applies to commands from menus and tool bar buttons even when
2fd0161b
CY
4906they are initiated from the keyboard. If `use-dialog-box' is nil,
4907that disables the use of a file dialog, regardless of the value of
4908this variable. */);
6b61353c
KH
4909 use_file_dialog = 1;
4910
7b863bd5
JB
4911 defsubr (&Sidentity);
4912 defsubr (&Srandom);
4913 defsubr (&Slength);
5a30fab8 4914 defsubr (&Ssafe_length);
026f59ce 4915 defsubr (&Sstring_bytes);
7b863bd5 4916 defsubr (&Sstring_equal);
0e1e9f8d 4917 defsubr (&Scompare_strings);
7b863bd5
JB
4918 defsubr (&Sstring_lessp);
4919 defsubr (&Sappend);
4920 defsubr (&Sconcat);
4921 defsubr (&Svconcat);
4922 defsubr (&Scopy_sequence);
09ab3c3b
KH
4923 defsubr (&Sstring_make_multibyte);
4924 defsubr (&Sstring_make_unibyte);
6d475204
RS
4925 defsubr (&Sstring_as_multibyte);
4926 defsubr (&Sstring_as_unibyte);
2df18cdb 4927 defsubr (&Sstring_to_multibyte);
b4480f16 4928 defsubr (&Sstring_to_unibyte);
7b863bd5
JB
4929 defsubr (&Scopy_alist);
4930 defsubr (&Ssubstring);
aebf4d42 4931 defsubr (&Ssubstring_no_properties);
7b863bd5
JB
4932 defsubr (&Snthcdr);
4933 defsubr (&Snth);
4934 defsubr (&Selt);
4935 defsubr (&Smember);
4936 defsubr (&Smemq);
008ef0ef 4937 defsubr (&Smemql);
7b863bd5
JB
4938 defsubr (&Sassq);
4939 defsubr (&Sassoc);
4940 defsubr (&Srassq);
0fb5a19c 4941 defsubr (&Srassoc);
7b863bd5 4942 defsubr (&Sdelq);
ca8dd546 4943 defsubr (&Sdelete);
7b863bd5
JB
4944 defsubr (&Snreverse);
4945 defsubr (&Sreverse);
4946 defsubr (&Ssort);
be9d483d 4947 defsubr (&Splist_get);
7b863bd5 4948 defsubr (&Sget);
be9d483d 4949 defsubr (&Splist_put);
7b863bd5 4950 defsubr (&Sput);
aebf4d42
RS
4951 defsubr (&Slax_plist_get);
4952 defsubr (&Slax_plist_put);
95f8c3b9 4953 defsubr (&Seql);
7b863bd5 4954 defsubr (&Sequal);
6b61353c 4955 defsubr (&Sequal_including_properties);
7b863bd5 4956 defsubr (&Sfillarray);
85cad579 4957 defsubr (&Sclear_string);
7b863bd5
JB
4958 defsubr (&Snconc);
4959 defsubr (&Smapcar);
5c6740c9 4960 defsubr (&Smapc);
7b863bd5 4961 defsubr (&Smapconcat);
7b863bd5
JB
4962 defsubr (&Syes_or_no_p);
4963 defsubr (&Sload_average);
4964 defsubr (&Sfeaturep);
4965 defsubr (&Srequire);
4966 defsubr (&Sprovide);
a5254817 4967 defsubr (&Splist_member);
b4f334f7
KH
4968 defsubr (&Swidget_put);
4969 defsubr (&Swidget_get);
4970 defsubr (&Swidget_apply);
24c129e4
KH
4971 defsubr (&Sbase64_encode_region);
4972 defsubr (&Sbase64_decode_region);
4973 defsubr (&Sbase64_encode_string);
4974 defsubr (&Sbase64_decode_string);
57916a7a 4975 defsubr (&Smd5);
7f3f739f 4976 defsubr (&Ssecure_hash);
d68beb2f 4977 defsubr (&Slocale_info);
b7432bb2 4978
29abe551
PE
4979 hashtest_eq.name = Qeq;
4980 hashtest_eq.user_hash_function = Qnil;
4981 hashtest_eq.user_cmp_function = Qnil;
4982 hashtest_eq.cmpfn = 0;
4983 hashtest_eq.hashfn = hashfn_eq;
4984
4985 hashtest_eql.name = Qeql;
4986 hashtest_eql.user_hash_function = Qnil;
4987 hashtest_eql.user_cmp_function = Qnil;
4988 hashtest_eql.cmpfn = cmpfn_eql;
4989 hashtest_eql.hashfn = hashfn_eql;
4990
4991 hashtest_equal.name = Qequal;
4992 hashtest_equal.user_hash_function = Qnil;
4993 hashtest_equal.user_cmp_function = Qnil;
4994 hashtest_equal.cmpfn = cmpfn_equal;
4995 hashtest_equal.hashfn = hashfn_equal;
7b863bd5 4996}