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