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