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