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