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