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