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