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