1 /* Random utility Lisp functions.
3 Copyright (C) 1985-1987, 1993-1995, 1997-2016 Free Software Foundation,
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
24 #include <filevercmp.h>
30 #include "character.h"
32 #include "composite.h"
34 #include "intervals.h"
37 static void sort_vector_copy (Lisp_Object
, ptrdiff_t,
38 Lisp_Object
[restrict
], Lisp_Object
[restrict
]);
39 static bool internal_equal (Lisp_Object
, Lisp_Object
, int, bool, Lisp_Object
);
41 DEFUN ("identity", Fidentity
, Sidentity
, 1, 1, 0,
42 doc
: /* Return the argument unchanged. */
49 DEFUN ("random", Frandom
, Srandom
, 0, 1, 0,
50 doc
: /* Return a pseudo-random number.
51 All integers representable in Lisp, i.e. between `most-negative-fixnum'
52 and `most-positive-fixnum', inclusive, are equally likely.
54 With positive integer LIMIT, return random number in interval [0,LIMIT).
55 With argument t, set the random number seed from the system's entropy
56 pool if available, otherwise from less-random volatile data such as the time.
57 With a string argument, set the seed based on the string's contents.
58 Other values of LIMIT are ignored.
60 See Info node `(elisp)Random Numbers' for more details. */)
67 else if (STRINGP (limit
))
68 seed_random (SSDATA (limit
), SBYTES (limit
));
71 if (INTEGERP (limit
) && 0 < XINT (limit
))
74 /* Return the remainder, except reject the rare case where
75 get_random returns a number so close to INTMASK that the
76 remainder isn't random. */
77 EMACS_INT remainder
= val
% XINT (limit
);
78 if (val
- remainder
<= INTMASK
- XINT (limit
) + 1)
79 return make_number (remainder
);
82 return make_number (val
);
85 /* Heuristic on how many iterations of a tight loop can be safely done
86 before it's time to do a QUIT. This must be a power of 2. */
87 enum { QUIT_COUNT_HEURISTIC
= 1 << 16 };
89 /* Random data-structure functions. */
92 CHECK_LIST_END (Lisp_Object x
, Lisp_Object y
)
94 CHECK_TYPE (NILP (x
), Qlistp
, y
);
97 DEFUN ("length", Flength
, Slength
, 1, 1, 0,
98 doc
: /* Return the length of vector, list or string SEQUENCE.
99 A byte-code function object is also allowed.
100 If the string contains multibyte characters, this is not necessarily
101 the number of bytes in the string; it is the number of characters.
102 To get the number of bytes, use `string-bytes'. */)
103 (register Lisp_Object sequence
)
105 register Lisp_Object val
;
107 if (STRINGP (sequence
))
108 XSETFASTINT (val
, SCHARS (sequence
));
109 else if (VECTORP (sequence
))
110 XSETFASTINT (val
, ASIZE (sequence
));
111 else if (CHAR_TABLE_P (sequence
))
112 XSETFASTINT (val
, MAX_CHAR
);
113 else if (BOOL_VECTOR_P (sequence
))
114 XSETFASTINT (val
, bool_vector_size (sequence
));
115 else if (COMPILEDP (sequence
))
116 XSETFASTINT (val
, ASIZE (sequence
) & PSEUDOVECTOR_SIZE_MASK
);
117 else if (CONSP (sequence
))
124 if ((i
& (QUIT_COUNT_HEURISTIC
- 1)) == 0)
126 if (MOST_POSITIVE_FIXNUM
< i
)
127 error ("List too long");
130 sequence
= XCDR (sequence
);
132 while (CONSP (sequence
));
134 CHECK_LIST_END (sequence
, sequence
);
136 val
= make_number (i
);
138 else if (NILP (sequence
))
139 XSETFASTINT (val
, 0);
141 wrong_type_argument (Qsequencep
, sequence
);
146 DEFUN ("safe-length", Fsafe_length
, Ssafe_length
, 1, 1, 0,
147 doc
: /* Return the length of a list, but avoid error or infinite loop.
148 This function never gets an error. If LIST is not really a list,
149 it returns 0. If LIST is circular, it returns a finite value
150 which is at least the number of distinct elements. */)
153 Lisp_Object tail
, halftail
;
158 return make_number (0);
160 /* halftail is used to detect circular lists. */
161 for (tail
= halftail
= list
; ; )
166 if (EQ (tail
, halftail
))
169 if ((lolen
& 1) == 0)
171 halftail
= XCDR (halftail
);
172 if ((lolen
& (QUIT_COUNT_HEURISTIC
- 1)) == 0)
176 hilen
+= UINTMAX_MAX
+ 1.0;
181 /* If the length does not fit into a fixnum, return a float.
182 On all known practical machines this returns an upper bound on
184 return hilen
? make_float (hilen
+ lolen
) : make_fixnum_or_float (lolen
);
187 DEFUN ("string-bytes", Fstring_bytes
, Sstring_bytes
, 1, 1, 0,
188 doc
: /* Return the number of bytes in STRING.
189 If STRING is multibyte, this may be greater than the length of STRING. */)
192 CHECK_STRING (string
);
193 return make_number (SBYTES (string
));
196 DEFUN ("string-equal", Fstring_equal
, Sstring_equal
, 2, 2, 0,
197 doc
: /* Return t if two strings have identical contents.
198 Case is significant, but text properties are ignored.
199 Symbols are also allowed; their print names are used instead. */)
200 (register Lisp_Object s1
, Lisp_Object s2
)
203 s1
= SYMBOL_NAME (s1
);
205 s2
= SYMBOL_NAME (s2
);
209 if (SCHARS (s1
) != SCHARS (s2
)
210 || SBYTES (s1
) != SBYTES (s2
)
211 || memcmp (SDATA (s1
), SDATA (s2
), SBYTES (s1
)))
216 DEFUN ("compare-strings", Fcompare_strings
, Scompare_strings
, 6, 7, 0,
217 doc
: /* Compare the contents of two strings, converting to multibyte if needed.
218 The arguments START1, END1, START2, and END2, if non-nil, are
219 positions specifying which parts of STR1 or STR2 to compare. In
220 string STR1, compare the part between START1 (inclusive) and END1
221 (exclusive). If START1 is nil, it defaults to 0, the beginning of
222 the string; if END1 is nil, it defaults to the length of the string.
223 Likewise, in string STR2, compare the part between START2 and END2.
224 Like in `substring', negative values are counted from the end.
226 The strings are compared by the numeric values of their characters.
227 For instance, STR1 is "less than" STR2 if its first differing
228 character has a smaller numeric value. If IGNORE-CASE is non-nil,
229 characters are converted to lower-case before comparing them. Unibyte
230 strings are converted to multibyte for comparison.
232 The value is t if the strings (or specified portions) match.
233 If string STR1 is less, the value is a negative number N;
234 - 1 - N is the number of characters that match at the beginning.
235 If string STR1 is greater, the value is a positive number N;
236 N - 1 is the number of characters that match at the beginning. */)
237 (Lisp_Object str1
, Lisp_Object start1
, Lisp_Object end1
, Lisp_Object str2
,
238 Lisp_Object start2
, Lisp_Object end2
, Lisp_Object ignore_case
)
240 ptrdiff_t from1
, to1
, from2
, to2
, i1
, i1_byte
, i2
, i2_byte
;
245 /* For backward compatibility, silently bring too-large positive end
246 values into range. */
247 if (INTEGERP (end1
) && SCHARS (str1
) < XINT (end1
))
248 end1
= make_number (SCHARS (str1
));
249 if (INTEGERP (end2
) && SCHARS (str2
) < XINT (end2
))
250 end2
= make_number (SCHARS (str2
));
252 validate_subarray (str1
, start1
, end1
, SCHARS (str1
), &from1
, &to1
);
253 validate_subarray (str2
, start2
, end2
, SCHARS (str2
), &from2
, &to2
);
258 i1_byte
= string_char_to_byte (str1
, i1
);
259 i2_byte
= string_char_to_byte (str2
, i2
);
261 while (i1
< to1
&& i2
< to2
)
263 /* When we find a mismatch, we must compare the
264 characters, not just the bytes. */
267 FETCH_STRING_CHAR_AS_MULTIBYTE_ADVANCE (c1
, str1
, i1
, i1_byte
);
268 FETCH_STRING_CHAR_AS_MULTIBYTE_ADVANCE (c2
, str2
, i2
, i2_byte
);
273 if (! NILP (ignore_case
))
275 c1
= XINT (Fupcase (make_number (c1
)));
276 c2
= XINT (Fupcase (make_number (c2
)));
282 /* Note that I1 has already been incremented
283 past the character that we are comparing;
284 hence we don't add or subtract 1 here. */
286 return make_number (- i1
+ from1
);
288 return make_number (i1
- from1
);
292 return make_number (i1
- from1
+ 1);
294 return make_number (- i1
+ from1
- 1);
299 DEFUN ("string-lessp", Fstring_lessp
, Sstring_lessp
, 2, 2, 0,
300 doc
: /* Return non-nil if STRING1 is less than STRING2 in lexicographic order.
302 Symbols are also allowed; their print names are used instead. */)
303 (register Lisp_Object string1
, Lisp_Object string2
)
305 register ptrdiff_t end
;
306 register ptrdiff_t i1
, i1_byte
, i2
, i2_byte
;
308 if (SYMBOLP (string1
))
309 string1
= SYMBOL_NAME (string1
);
310 if (SYMBOLP (string2
))
311 string2
= SYMBOL_NAME (string2
);
312 CHECK_STRING (string1
);
313 CHECK_STRING (string2
);
315 i1
= i1_byte
= i2
= i2_byte
= 0;
317 end
= SCHARS (string1
);
318 if (end
> SCHARS (string2
))
319 end
= SCHARS (string2
);
323 /* When we find a mismatch, we must compare the
324 characters, not just the bytes. */
327 FETCH_STRING_CHAR_ADVANCE (c1
, string1
, i1
, i1_byte
);
328 FETCH_STRING_CHAR_ADVANCE (c2
, string2
, i2
, i2_byte
);
331 return c1
< c2
? Qt
: Qnil
;
333 return i1
< SCHARS (string2
) ? Qt
: Qnil
;
336 DEFUN ("string-version-lessp", Fstring_version_lessp
,
337 Sstring_version_lessp
, 2, 2, 0,
338 doc
: /* Return non-nil if S1 is less than S2, as version strings.
340 This function compares version strings S1 and S2:
341 1) By prefix lexicographically.
342 2) Then by version (similarly to version comparison of Debian's dpkg).
343 Leading zeros in version numbers are ignored.
344 3) If both prefix and version are equal, compare as ordinary strings.
346 For example, \"foo2.png\" compares less than \"foo12.png\".
348 Symbols are also allowed; their print names are used instead. */)
349 (Lisp_Object string1
, Lisp_Object string2
)
351 if (SYMBOLP (string1
))
352 string1
= SYMBOL_NAME (string1
);
353 if (SYMBOLP (string2
))
354 string2
= SYMBOL_NAME (string2
);
355 CHECK_STRING (string1
);
356 CHECK_STRING (string2
);
358 char *p1
= SSDATA (string1
);
359 char *p2
= SSDATA (string2
);
360 char *lim1
= p1
+ SBYTES (string1
);
361 char *lim2
= p2
+ SBYTES (string2
);
364 while ((cmp
= filevercmp (p1
, p2
)) == 0)
366 /* If the strings are identical through their first null bytes,
367 skip past identical prefixes and try again. */
368 ptrdiff_t size
= strlen (p1
) + 1;
372 return lim2
< p2
? Qnil
: Qt
;
377 return cmp
< 0 ? Qt
: Qnil
;
380 DEFUN ("string-collate-lessp", Fstring_collate_lessp
, Sstring_collate_lessp
, 2, 4, 0,
381 doc
: /* Return t if first arg string is less than second in collation order.
382 Symbols are also allowed; their print names are used instead.
384 This function obeys the conventions for collation order in your
385 locale settings. For example, punctuation and whitespace characters
386 might be considered less significant for sorting:
388 (sort \\='("11" "12" "1 1" "1 2" "1.1" "1.2") \\='string-collate-lessp)
389 => ("11" "1 1" "1.1" "12" "1 2" "1.2")
391 The optional argument LOCALE, a string, overrides the setting of your
392 current locale identifier for collation. The value is system
393 dependent; a LOCALE \"en_US.UTF-8\" is applicable on POSIX systems,
394 while it would be, e.g., \"enu_USA.1252\" on MS-Windows systems.
396 If IGNORE-CASE is non-nil, characters are converted to lower-case
397 before comparing them.
399 To emulate Unicode-compliant collation on MS-Windows systems,
400 bind `w32-collate-ignore-punctuation' to a non-nil value, since
401 the codeset part of the locale cannot be \"UTF-8\" on MS-Windows.
403 If your system does not support a locale environment, this function
404 behaves like `string-lessp'. */)
405 (Lisp_Object s1
, Lisp_Object s2
, Lisp_Object locale
, Lisp_Object ignore_case
)
407 #if defined __STDC_ISO_10646__ || defined WINDOWSNT
408 /* Check parameters. */
410 s1
= SYMBOL_NAME (s1
);
412 s2
= SYMBOL_NAME (s2
);
416 CHECK_STRING (locale
);
418 return (str_collate (s1
, s2
, locale
, ignore_case
) < 0) ? Qt
: Qnil
;
420 #else /* !__STDC_ISO_10646__, !WINDOWSNT */
421 return Fstring_lessp (s1
, s2
);
422 #endif /* !__STDC_ISO_10646__, !WINDOWSNT */
425 DEFUN ("string-collate-equalp", Fstring_collate_equalp
, Sstring_collate_equalp
, 2, 4, 0,
426 doc
: /* Return t if two strings have identical contents.
427 Symbols are also allowed; their print names are used instead.
429 This function obeys the conventions for collation order in your locale
430 settings. For example, characters with different coding points but
431 the same meaning might be considered as equal, like different grave
432 accent Unicode characters:
434 (string-collate-equalp (string ?\\uFF40) (string ?\\u1FEF))
437 The optional argument LOCALE, a string, overrides the setting of your
438 current locale identifier for collation. The value is system
439 dependent; a LOCALE \"en_US.UTF-8\" is applicable on POSIX systems,
440 while it would be \"enu_USA.1252\" on MS Windows systems.
442 If IGNORE-CASE is non-nil, characters are converted to lower-case
443 before comparing them.
445 To emulate Unicode-compliant collation on MS-Windows systems,
446 bind `w32-collate-ignore-punctuation' to a non-nil value, since
447 the codeset part of the locale cannot be \"UTF-8\" on MS-Windows.
449 If your system does not support a locale environment, this function
450 behaves like `string-equal'.
452 Do NOT use this function to compare file names for equality, only
453 for sorting them. */)
454 (Lisp_Object s1
, Lisp_Object s2
, Lisp_Object locale
, Lisp_Object ignore_case
)
456 #if defined __STDC_ISO_10646__ || defined WINDOWSNT
457 /* Check parameters. */
459 s1
= SYMBOL_NAME (s1
);
461 s2
= SYMBOL_NAME (s2
);
465 CHECK_STRING (locale
);
467 return (str_collate (s1
, s2
, locale
, ignore_case
) == 0) ? Qt
: Qnil
;
469 #else /* !__STDC_ISO_10646__, !WINDOWSNT */
470 return Fstring_equal (s1
, s2
);
471 #endif /* !__STDC_ISO_10646__, !WINDOWSNT */
474 static Lisp_Object
concat (ptrdiff_t nargs
, Lisp_Object
*args
,
475 enum Lisp_Type target_type
, bool last_special
);
479 concat2 (Lisp_Object s1
, Lisp_Object s2
)
481 return concat (2, ((Lisp_Object
[]) {s1
, s2
}), Lisp_String
, 0);
486 concat3 (Lisp_Object s1
, Lisp_Object s2
, Lisp_Object s3
)
488 return concat (3, ((Lisp_Object
[]) {s1
, s2
, s3
}), Lisp_String
, 0);
491 DEFUN ("append", Fappend
, Sappend
, 0, MANY
, 0,
492 doc
: /* Concatenate all the arguments and make the result a list.
493 The result is a list whose elements are the elements of all the arguments.
494 Each argument may be a list, vector or string.
495 The last argument is not copied, just used as the tail of the new list.
496 usage: (append &rest SEQUENCES) */)
497 (ptrdiff_t nargs
, Lisp_Object
*args
)
499 return concat (nargs
, args
, Lisp_Cons
, 1);
502 DEFUN ("concat", Fconcat
, Sconcat
, 0, MANY
, 0,
503 doc
: /* Concatenate all the arguments and make the result a string.
504 The result is a string whose elements are the elements of all the arguments.
505 Each argument may be a string or a list or vector of characters (integers).
506 usage: (concat &rest SEQUENCES) */)
507 (ptrdiff_t nargs
, Lisp_Object
*args
)
509 return concat (nargs
, args
, Lisp_String
, 0);
512 DEFUN ("vconcat", Fvconcat
, Svconcat
, 0, MANY
, 0,
513 doc
: /* Concatenate all the arguments and make the result a vector.
514 The result is a vector whose elements are the elements of all the arguments.
515 Each argument may be a list, vector or string.
516 usage: (vconcat &rest SEQUENCES) */)
517 (ptrdiff_t nargs
, Lisp_Object
*args
)
519 return concat (nargs
, args
, Lisp_Vectorlike
, 0);
523 DEFUN ("copy-sequence", Fcopy_sequence
, Scopy_sequence
, 1, 1, 0,
524 doc
: /* Return a copy of a list, vector, string or char-table.
525 The elements of a list or vector are not copied; they are shared
526 with the original. */)
529 if (NILP (arg
)) return arg
;
531 if (CHAR_TABLE_P (arg
))
533 return copy_char_table (arg
);
536 if (BOOL_VECTOR_P (arg
))
538 EMACS_INT nbits
= bool_vector_size (arg
);
539 ptrdiff_t nbytes
= bool_vector_bytes (nbits
);
540 Lisp_Object val
= make_uninit_bool_vector (nbits
);
541 memcpy (bool_vector_data (val
), bool_vector_data (arg
), nbytes
);
545 if (!CONSP (arg
) && !VECTORP (arg
) && !STRINGP (arg
))
546 wrong_type_argument (Qsequencep
, arg
);
548 return concat (1, &arg
, XTYPE (arg
), 0);
551 /* This structure holds information of an argument of `concat' that is
552 a string and has text properties to be copied. */
555 ptrdiff_t argnum
; /* refer to ARGS (arguments of `concat') */
556 ptrdiff_t from
; /* refer to ARGS[argnum] (argument string) */
557 ptrdiff_t to
; /* refer to VAL (the target string) */
561 concat (ptrdiff_t nargs
, Lisp_Object
*args
,
562 enum Lisp_Type target_type
, bool last_special
)
568 ptrdiff_t toindex_byte
= 0;
569 EMACS_INT result_len
;
570 EMACS_INT result_len_byte
;
572 Lisp_Object last_tail
;
575 /* When we make a multibyte string, we can't copy text properties
576 while concatenating each string because the length of resulting
577 string can't be decided until we finish the whole concatenation.
578 So, we record strings that have text properties to be copied
579 here, and copy the text properties after the concatenation. */
580 struct textprop_rec
*textprops
= NULL
;
581 /* Number of elements in textprops. */
582 ptrdiff_t num_textprops
= 0;
587 /* In append, the last arg isn't treated like the others */
588 if (last_special
&& nargs
> 0)
591 last_tail
= args
[nargs
];
596 /* Check each argument. */
597 for (argnum
= 0; argnum
< nargs
; argnum
++)
600 if (!(CONSP (this) || NILP (this) || VECTORP (this) || STRINGP (this)
601 || COMPILEDP (this) || BOOL_VECTOR_P (this)))
602 wrong_type_argument (Qsequencep
, this);
605 /* Compute total length in chars of arguments in RESULT_LEN.
606 If desired output is a string, also compute length in bytes
607 in RESULT_LEN_BYTE, and determine in SOME_MULTIBYTE
608 whether the result should be a multibyte string. */
612 for (argnum
= 0; argnum
< nargs
; argnum
++)
616 len
= XFASTINT (Flength (this));
617 if (target_type
== Lisp_String
)
619 /* We must count the number of bytes needed in the string
620 as well as the number of characters. */
624 ptrdiff_t this_len_byte
;
626 if (VECTORP (this) || COMPILEDP (this))
627 for (i
= 0; i
< len
; i
++)
630 CHECK_CHARACTER (ch
);
632 this_len_byte
= CHAR_BYTES (c
);
633 if (STRING_BYTES_BOUND
- result_len_byte
< this_len_byte
)
635 result_len_byte
+= this_len_byte
;
636 if (! ASCII_CHAR_P (c
) && ! CHAR_BYTE8_P (c
))
639 else if (BOOL_VECTOR_P (this) && bool_vector_size (this) > 0)
640 wrong_type_argument (Qintegerp
, Faref (this, make_number (0)));
641 else if (CONSP (this))
642 for (; CONSP (this); this = XCDR (this))
645 CHECK_CHARACTER (ch
);
647 this_len_byte
= CHAR_BYTES (c
);
648 if (STRING_BYTES_BOUND
- result_len_byte
< this_len_byte
)
650 result_len_byte
+= this_len_byte
;
651 if (! ASCII_CHAR_P (c
) && ! CHAR_BYTE8_P (c
))
654 else if (STRINGP (this))
656 if (STRING_MULTIBYTE (this))
659 this_len_byte
= SBYTES (this);
662 this_len_byte
= count_size_as_multibyte (SDATA (this),
664 if (STRING_BYTES_BOUND
- result_len_byte
< this_len_byte
)
666 result_len_byte
+= this_len_byte
;
671 if (MOST_POSITIVE_FIXNUM
< result_len
)
672 memory_full (SIZE_MAX
);
675 if (! some_multibyte
)
676 result_len_byte
= result_len
;
678 /* Create the output object. */
679 if (target_type
== Lisp_Cons
)
680 val
= Fmake_list (make_number (result_len
), Qnil
);
681 else if (target_type
== Lisp_Vectorlike
)
682 val
= Fmake_vector (make_number (result_len
), Qnil
);
683 else if (some_multibyte
)
684 val
= make_uninit_multibyte_string (result_len
, result_len_byte
);
686 val
= make_uninit_string (result_len
);
688 /* In `append', if all but last arg are nil, return last arg. */
689 if (target_type
== Lisp_Cons
&& EQ (val
, Qnil
))
692 /* Copy the contents of the args into the result. */
694 tail
= val
, toindex
= -1; /* -1 in toindex is flag we are making a list */
696 toindex
= 0, toindex_byte
= 0;
700 SAFE_NALLOCA (textprops
, 1, nargs
);
702 for (argnum
= 0; argnum
< nargs
; argnum
++)
705 ptrdiff_t thisleni
= 0;
706 register ptrdiff_t thisindex
= 0;
707 register ptrdiff_t thisindex_byte
= 0;
711 thislen
= Flength (this), thisleni
= XINT (thislen
);
713 /* Between strings of the same kind, copy fast. */
714 if (STRINGP (this) && STRINGP (val
)
715 && STRING_MULTIBYTE (this) == some_multibyte
)
717 ptrdiff_t thislen_byte
= SBYTES (this);
719 memcpy (SDATA (val
) + toindex_byte
, SDATA (this), SBYTES (this));
720 if (string_intervals (this))
722 textprops
[num_textprops
].argnum
= argnum
;
723 textprops
[num_textprops
].from
= 0;
724 textprops
[num_textprops
++].to
= toindex
;
726 toindex_byte
+= thislen_byte
;
729 /* Copy a single-byte string to a multibyte string. */
730 else if (STRINGP (this) && STRINGP (val
))
732 if (string_intervals (this))
734 textprops
[num_textprops
].argnum
= argnum
;
735 textprops
[num_textprops
].from
= 0;
736 textprops
[num_textprops
++].to
= toindex
;
738 toindex_byte
+= copy_text (SDATA (this),
739 SDATA (val
) + toindex_byte
,
740 SCHARS (this), 0, 1);
744 /* Copy element by element. */
747 register Lisp_Object elt
;
749 /* Fetch next element of `this' arg into `elt', or break if
750 `this' is exhausted. */
751 if (NILP (this)) break;
753 elt
= XCAR (this), this = XCDR (this);
754 else if (thisindex
>= thisleni
)
756 else if (STRINGP (this))
759 if (STRING_MULTIBYTE (this))
760 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c
, this,
765 c
= SREF (this, thisindex
); thisindex
++;
766 if (some_multibyte
&& !ASCII_CHAR_P (c
))
767 c
= BYTE8_TO_CHAR (c
);
769 XSETFASTINT (elt
, c
);
771 else if (BOOL_VECTOR_P (this))
773 elt
= bool_vector_ref (this, thisindex
);
778 elt
= AREF (this, thisindex
);
782 /* Store this element into the result. */
789 else if (VECTORP (val
))
791 ASET (val
, toindex
, elt
);
797 CHECK_CHARACTER (elt
);
800 toindex_byte
+= CHAR_STRING (c
, SDATA (val
) + toindex_byte
);
802 SSET (val
, toindex_byte
++, c
);
808 XSETCDR (prev
, last_tail
);
810 if (num_textprops
> 0)
813 ptrdiff_t last_to_end
= -1;
815 for (argnum
= 0; argnum
< num_textprops
; argnum
++)
817 this = args
[textprops
[argnum
].argnum
];
818 props
= text_property_list (this,
820 make_number (SCHARS (this)),
822 /* If successive arguments have properties, be sure that the
823 value of `composition' property be the copy. */
824 if (last_to_end
== textprops
[argnum
].to
)
825 make_composition_value_copy (props
);
826 add_text_properties_from_list (val
, props
,
827 make_number (textprops
[argnum
].to
));
828 last_to_end
= textprops
[argnum
].to
+ SCHARS (this);
836 static Lisp_Object string_char_byte_cache_string
;
837 static ptrdiff_t string_char_byte_cache_charpos
;
838 static ptrdiff_t string_char_byte_cache_bytepos
;
841 clear_string_char_byte_cache (void)
843 string_char_byte_cache_string
= Qnil
;
846 /* Return the byte index corresponding to CHAR_INDEX in STRING. */
849 string_char_to_byte (Lisp_Object string
, ptrdiff_t char_index
)
852 ptrdiff_t best_below
, best_below_byte
;
853 ptrdiff_t best_above
, best_above_byte
;
855 best_below
= best_below_byte
= 0;
856 best_above
= SCHARS (string
);
857 best_above_byte
= SBYTES (string
);
858 if (best_above
== best_above_byte
)
861 if (EQ (string
, string_char_byte_cache_string
))
863 if (string_char_byte_cache_charpos
< char_index
)
865 best_below
= string_char_byte_cache_charpos
;
866 best_below_byte
= string_char_byte_cache_bytepos
;
870 best_above
= string_char_byte_cache_charpos
;
871 best_above_byte
= string_char_byte_cache_bytepos
;
875 if (char_index
- best_below
< best_above
- char_index
)
877 unsigned char *p
= SDATA (string
) + best_below_byte
;
879 while (best_below
< char_index
)
881 p
+= BYTES_BY_CHAR_HEAD (*p
);
884 i_byte
= p
- SDATA (string
);
888 unsigned char *p
= SDATA (string
) + best_above_byte
;
890 while (best_above
> char_index
)
893 while (!CHAR_HEAD_P (*p
)) p
--;
896 i_byte
= p
- SDATA (string
);
899 string_char_byte_cache_bytepos
= i_byte
;
900 string_char_byte_cache_charpos
= char_index
;
901 string_char_byte_cache_string
= string
;
906 /* Return the character index corresponding to BYTE_INDEX in STRING. */
909 string_byte_to_char (Lisp_Object string
, ptrdiff_t byte_index
)
912 ptrdiff_t best_below
, best_below_byte
;
913 ptrdiff_t best_above
, best_above_byte
;
915 best_below
= best_below_byte
= 0;
916 best_above
= SCHARS (string
);
917 best_above_byte
= SBYTES (string
);
918 if (best_above
== best_above_byte
)
921 if (EQ (string
, string_char_byte_cache_string
))
923 if (string_char_byte_cache_bytepos
< byte_index
)
925 best_below
= string_char_byte_cache_charpos
;
926 best_below_byte
= string_char_byte_cache_bytepos
;
930 best_above
= string_char_byte_cache_charpos
;
931 best_above_byte
= string_char_byte_cache_bytepos
;
935 if (byte_index
- best_below_byte
< best_above_byte
- byte_index
)
937 unsigned char *p
= SDATA (string
) + best_below_byte
;
938 unsigned char *pend
= SDATA (string
) + byte_index
;
942 p
+= BYTES_BY_CHAR_HEAD (*p
);
946 i_byte
= p
- SDATA (string
);
950 unsigned char *p
= SDATA (string
) + best_above_byte
;
951 unsigned char *pbeg
= SDATA (string
) + byte_index
;
956 while (!CHAR_HEAD_P (*p
)) p
--;
960 i_byte
= p
- SDATA (string
);
963 string_char_byte_cache_bytepos
= i_byte
;
964 string_char_byte_cache_charpos
= i
;
965 string_char_byte_cache_string
= string
;
970 /* Convert STRING to a multibyte string. */
973 string_make_multibyte (Lisp_Object string
)
980 if (STRING_MULTIBYTE (string
))
983 nbytes
= count_size_as_multibyte (SDATA (string
),
985 /* If all the chars are ASCII, they won't need any more bytes
986 once converted. In that case, we can return STRING itself. */
987 if (nbytes
== SBYTES (string
))
990 buf
= SAFE_ALLOCA (nbytes
);
991 copy_text (SDATA (string
), buf
, SBYTES (string
),
994 ret
= make_multibyte_string ((char *) buf
, SCHARS (string
), nbytes
);
1001 /* Convert STRING (if unibyte) to a multibyte string without changing
1002 the number of characters. Characters 0200 trough 0237 are
1003 converted to eight-bit characters. */
1006 string_to_multibyte (Lisp_Object string
)
1013 if (STRING_MULTIBYTE (string
))
1016 nbytes
= count_size_as_multibyte (SDATA (string
), SBYTES (string
));
1017 /* If all the chars are ASCII, they won't need any more bytes once
1019 if (nbytes
== SBYTES (string
))
1020 return make_multibyte_string (SSDATA (string
), nbytes
, nbytes
);
1022 buf
= SAFE_ALLOCA (nbytes
);
1023 memcpy (buf
, SDATA (string
), SBYTES (string
));
1024 str_to_multibyte (buf
, nbytes
, SBYTES (string
));
1026 ret
= make_multibyte_string ((char *) buf
, SCHARS (string
), nbytes
);
1033 /* Convert STRING to a single-byte string. */
1036 string_make_unibyte (Lisp_Object string
)
1043 if (! STRING_MULTIBYTE (string
))
1046 nchars
= SCHARS (string
);
1048 buf
= SAFE_ALLOCA (nchars
);
1049 copy_text (SDATA (string
), buf
, SBYTES (string
),
1052 ret
= make_unibyte_string ((char *) buf
, nchars
);
1058 DEFUN ("string-make-multibyte", Fstring_make_multibyte
, Sstring_make_multibyte
,
1060 doc
: /* Return the multibyte equivalent of STRING.
1061 If STRING is unibyte and contains non-ASCII characters, the function
1062 `unibyte-char-to-multibyte' is used to convert each unibyte character
1063 to a multibyte character. In this case, the returned string is a
1064 newly created string with no text properties. If STRING is multibyte
1065 or entirely ASCII, it is returned unchanged. In particular, when
1066 STRING is unibyte and entirely ASCII, the returned string is unibyte.
1067 (When the characters are all ASCII, Emacs primitives will treat the
1068 string the same way whether it is unibyte or multibyte.) */)
1069 (Lisp_Object string
)
1071 CHECK_STRING (string
);
1073 return string_make_multibyte (string
);
1076 DEFUN ("string-make-unibyte", Fstring_make_unibyte
, Sstring_make_unibyte
,
1078 doc
: /* Return the unibyte equivalent of STRING.
1079 Multibyte character codes are converted to unibyte according to
1080 `nonascii-translation-table' or, if that is nil, `nonascii-insert-offset'.
1081 If the lookup in the translation table fails, this function takes just
1082 the low 8 bits of each character. */)
1083 (Lisp_Object string
)
1085 CHECK_STRING (string
);
1087 return string_make_unibyte (string
);
1090 DEFUN ("string-as-unibyte", Fstring_as_unibyte
, Sstring_as_unibyte
,
1092 doc
: /* Return a unibyte string with the same individual bytes as STRING.
1093 If STRING is unibyte, the result is STRING itself.
1094 Otherwise it is a newly created string, with no text properties.
1095 If STRING is multibyte and contains a character of charset
1096 `eight-bit', it is converted to the corresponding single byte. */)
1097 (Lisp_Object string
)
1099 CHECK_STRING (string
);
1101 if (STRING_MULTIBYTE (string
))
1103 unsigned char *str
= (unsigned char *) xlispstrdup (string
);
1104 ptrdiff_t bytes
= str_as_unibyte (str
, SBYTES (string
));
1106 string
= make_unibyte_string ((char *) str
, bytes
);
1112 DEFUN ("string-as-multibyte", Fstring_as_multibyte
, Sstring_as_multibyte
,
1114 doc
: /* Return a multibyte string with the same individual bytes as STRING.
1115 If STRING is multibyte, the result is STRING itself.
1116 Otherwise it is a newly created string, with no text properties.
1118 If STRING is unibyte and contains an individual 8-bit byte (i.e. not
1119 part of a correct utf-8 sequence), it is converted to the corresponding
1120 multibyte character of charset `eight-bit'.
1121 See also `string-to-multibyte'.
1123 Beware, this often doesn't really do what you think it does.
1124 It is similar to (decode-coding-string STRING \\='utf-8-emacs).
1125 If you're not sure, whether to use `string-as-multibyte' or
1126 `string-to-multibyte', use `string-to-multibyte'. */)
1127 (Lisp_Object string
)
1129 CHECK_STRING (string
);
1131 if (! STRING_MULTIBYTE (string
))
1133 Lisp_Object new_string
;
1134 ptrdiff_t nchars
, nbytes
;
1136 parse_str_as_multibyte (SDATA (string
),
1139 new_string
= make_uninit_multibyte_string (nchars
, nbytes
);
1140 memcpy (SDATA (new_string
), SDATA (string
), SBYTES (string
));
1141 if (nbytes
!= SBYTES (string
))
1142 str_as_multibyte (SDATA (new_string
), nbytes
,
1143 SBYTES (string
), NULL
);
1144 string
= new_string
;
1145 set_string_intervals (string
, NULL
);
1150 DEFUN ("string-to-multibyte", Fstring_to_multibyte
, Sstring_to_multibyte
,
1152 doc
: /* Return a multibyte string with the same individual chars as STRING.
1153 If STRING is multibyte, the result is STRING itself.
1154 Otherwise it is a newly created string, with no text properties.
1156 If STRING is unibyte and contains an 8-bit byte, it is converted to
1157 the corresponding multibyte character of charset `eight-bit'.
1159 This differs from `string-as-multibyte' by converting each byte of a correct
1160 utf-8 sequence to an eight-bit character, not just bytes that don't form a
1161 correct sequence. */)
1162 (Lisp_Object string
)
1164 CHECK_STRING (string
);
1166 return string_to_multibyte (string
);
1169 DEFUN ("string-to-unibyte", Fstring_to_unibyte
, Sstring_to_unibyte
,
1171 doc
: /* Return a unibyte string with the same individual chars as STRING.
1172 If STRING is unibyte, the result is STRING itself.
1173 Otherwise it is a newly created string, with no text properties,
1174 where each `eight-bit' character is converted to the corresponding byte.
1175 If STRING contains a non-ASCII, non-`eight-bit' character,
1176 an error is signaled. */)
1177 (Lisp_Object string
)
1179 CHECK_STRING (string
);
1181 if (STRING_MULTIBYTE (string
))
1183 ptrdiff_t chars
= SCHARS (string
);
1184 unsigned char *str
= xmalloc (chars
);
1185 ptrdiff_t converted
= str_to_unibyte (SDATA (string
), str
, chars
);
1187 if (converted
< chars
)
1188 error ("Can't convert the %"pD
"dth character to unibyte", converted
);
1189 string
= make_unibyte_string ((char *) str
, chars
);
1196 DEFUN ("copy-alist", Fcopy_alist
, Scopy_alist
, 1, 1, 0,
1197 doc
: /* Return a copy of ALIST.
1198 This is an alist which represents the same mapping from objects to objects,
1199 but does not share the alist structure with ALIST.
1200 The objects mapped (cars and cdrs of elements of the alist)
1201 are shared, however.
1202 Elements of ALIST that are not conses are also shared. */)
1205 register Lisp_Object tem
;
1210 alist
= concat (1, &alist
, Lisp_Cons
, 0);
1211 for (tem
= alist
; CONSP (tem
); tem
= XCDR (tem
))
1213 register Lisp_Object car
;
1217 XSETCAR (tem
, Fcons (XCAR (car
), XCDR (car
)));
1222 /* Check that ARRAY can have a valid subarray [FROM..TO),
1223 given that its size is SIZE.
1224 If FROM is nil, use 0; if TO is nil, use SIZE.
1225 Count negative values backwards from the end.
1226 Set *IFROM and *ITO to the two indexes used. */
1229 validate_subarray (Lisp_Object array
, Lisp_Object from
, Lisp_Object to
,
1230 ptrdiff_t size
, ptrdiff_t *ifrom
, ptrdiff_t *ito
)
1234 if (INTEGERP (from
))
1240 else if (NILP (from
))
1243 wrong_type_argument (Qintegerp
, from
);
1254 wrong_type_argument (Qintegerp
, to
);
1256 if (! (0 <= f
&& f
<= t
&& t
<= size
))
1257 args_out_of_range_3 (array
, from
, to
);
1263 DEFUN ("substring", Fsubstring
, Ssubstring
, 1, 3, 0,
1264 doc
: /* Return a new string whose contents are a substring of STRING.
1265 The returned string consists of the characters between index FROM
1266 (inclusive) and index TO (exclusive) of STRING. FROM and TO are
1267 zero-indexed: 0 means the first character of STRING. Negative values
1268 are counted from the end of STRING. If TO is nil, the substring runs
1269 to the end of STRING.
1271 The STRING argument may also be a vector. In that case, the return
1272 value is a new vector that contains the elements between index FROM
1273 (inclusive) and index TO (exclusive) of that vector argument.
1275 With one argument, just copy STRING (with properties, if any). */)
1276 (Lisp_Object string
, Lisp_Object from
, Lisp_Object to
)
1279 ptrdiff_t size
, ifrom
, ito
;
1281 size
= CHECK_VECTOR_OR_STRING (string
);
1282 validate_subarray (string
, from
, to
, size
, &ifrom
, &ito
);
1284 if (STRINGP (string
))
1287 = !ifrom
? 0 : string_char_to_byte (string
, ifrom
);
1289 = ito
== size
? SBYTES (string
) : string_char_to_byte (string
, ito
);
1290 res
= make_specified_string (SSDATA (string
) + from_byte
,
1291 ito
- ifrom
, to_byte
- from_byte
,
1292 STRING_MULTIBYTE (string
));
1293 copy_text_properties (make_number (ifrom
), make_number (ito
),
1294 string
, make_number (0), res
, Qnil
);
1297 res
= Fvector (ito
- ifrom
, aref_addr (string
, ifrom
));
1303 DEFUN ("substring-no-properties", Fsubstring_no_properties
, Ssubstring_no_properties
, 1, 3, 0,
1304 doc
: /* Return a substring of STRING, without text properties.
1305 It starts at index FROM and ends before TO.
1306 TO may be nil or omitted; then the substring runs to the end of STRING.
1307 If FROM is nil or omitted, the substring starts at the beginning of STRING.
1308 If FROM or TO is negative, it counts from the end.
1310 With one argument, just copy STRING without its properties. */)
1311 (Lisp_Object string
, register Lisp_Object from
, Lisp_Object to
)
1313 ptrdiff_t from_char
, to_char
, from_byte
, to_byte
, size
;
1315 CHECK_STRING (string
);
1317 size
= SCHARS (string
);
1318 validate_subarray (string
, from
, to
, size
, &from_char
, &to_char
);
1320 from_byte
= !from_char
? 0 : string_char_to_byte (string
, from_char
);
1322 to_char
== size
? SBYTES (string
) : string_char_to_byte (string
, to_char
);
1323 return make_specified_string (SSDATA (string
) + from_byte
,
1324 to_char
- from_char
, to_byte
- from_byte
,
1325 STRING_MULTIBYTE (string
));
1328 /* Extract a substring of STRING, giving start and end positions
1329 both in characters and in bytes. */
1332 substring_both (Lisp_Object string
, ptrdiff_t from
, ptrdiff_t from_byte
,
1333 ptrdiff_t to
, ptrdiff_t to_byte
)
1336 ptrdiff_t size
= CHECK_VECTOR_OR_STRING (string
);
1338 if (!(0 <= from
&& from
<= to
&& to
<= size
))
1339 args_out_of_range_3 (string
, make_number (from
), make_number (to
));
1341 if (STRINGP (string
))
1343 res
= make_specified_string (SSDATA (string
) + from_byte
,
1344 to
- from
, to_byte
- from_byte
,
1345 STRING_MULTIBYTE (string
));
1346 copy_text_properties (make_number (from
), make_number (to
),
1347 string
, make_number (0), res
, Qnil
);
1350 res
= Fvector (to
- from
, aref_addr (string
, from
));
1355 DEFUN ("nthcdr", Fnthcdr
, Snthcdr
, 2, 2, 0,
1356 doc
: /* Take cdr N times on LIST, return the result. */)
1357 (Lisp_Object n
, Lisp_Object list
)
1362 for (i
= 0; i
< num
&& !NILP (list
); i
++)
1365 CHECK_LIST_CONS (list
, list
);
1371 DEFUN ("nth", Fnth
, Snth
, 2, 2, 0,
1372 doc
: /* Return the Nth element of LIST.
1373 N counts from zero. If LIST is not that long, nil is returned. */)
1374 (Lisp_Object n
, Lisp_Object list
)
1376 return Fcar (Fnthcdr (n
, list
));
1379 DEFUN ("elt", Felt
, Selt
, 2, 2, 0,
1380 doc
: /* Return element of SEQUENCE at index N. */)
1381 (register Lisp_Object sequence
, Lisp_Object n
)
1384 if (CONSP (sequence
) || NILP (sequence
))
1385 return Fcar (Fnthcdr (n
, sequence
));
1387 /* Faref signals a "not array" error, so check here. */
1388 CHECK_ARRAY (sequence
, Qsequencep
);
1389 return Faref (sequence
, n
);
1392 DEFUN ("member", Fmember
, Smember
, 2, 2, 0,
1393 doc
: /* Return non-nil if ELT is an element of LIST. Comparison done with `equal'.
1394 The value is actually the tail of LIST whose car is ELT. */)
1395 (register Lisp_Object elt
, Lisp_Object list
)
1397 register Lisp_Object tail
;
1398 for (tail
= list
; !NILP (tail
); tail
= XCDR (tail
))
1400 register Lisp_Object tem
;
1401 CHECK_LIST_CONS (tail
, list
);
1403 if (! NILP (Fequal (elt
, tem
)))
1410 DEFUN ("memq", Fmemq
, Smemq
, 2, 2, 0,
1411 doc
: /* Return non-nil if ELT is an element of LIST. Comparison done with `eq'.
1412 The value is actually the tail of LIST whose car is ELT. */)
1413 (register Lisp_Object elt
, Lisp_Object list
)
1417 if (!CONSP (list
) || EQ (XCAR (list
), elt
))
1421 if (!CONSP (list
) || EQ (XCAR (list
), elt
))
1425 if (!CONSP (list
) || EQ (XCAR (list
), elt
))
1436 DEFUN ("memql", Fmemql
, Smemql
, 2, 2, 0,
1437 doc
: /* Return non-nil if ELT is an element of LIST. Comparison done with `eql'.
1438 The value is actually the tail of LIST whose car is ELT. */)
1439 (register Lisp_Object elt
, Lisp_Object list
)
1441 register Lisp_Object tail
;
1444 return Fmemq (elt
, list
);
1446 for (tail
= list
; !NILP (tail
); tail
= XCDR (tail
))
1448 register Lisp_Object tem
;
1449 CHECK_LIST_CONS (tail
, list
);
1451 if (FLOATP (tem
) && internal_equal (elt
, tem
, 0, 0, Qnil
))
1458 DEFUN ("assq", Fassq
, Sassq
, 2, 2, 0,
1459 doc
: /* Return non-nil if KEY is `eq' to the car of an element of LIST.
1460 The value is actually the first element of LIST whose car is KEY.
1461 Elements of LIST that are not conses are ignored. */)
1462 (Lisp_Object key
, Lisp_Object list
)
1467 || (CONSP (XCAR (list
))
1468 && EQ (XCAR (XCAR (list
)), key
)))
1473 || (CONSP (XCAR (list
))
1474 && EQ (XCAR (XCAR (list
)), key
)))
1479 || (CONSP (XCAR (list
))
1480 && EQ (XCAR (XCAR (list
)), key
)))
1490 /* Like Fassq but never report an error and do not allow quits.
1491 Use only on lists known never to be circular. */
1494 assq_no_quit (Lisp_Object key
, Lisp_Object list
)
1497 && (!CONSP (XCAR (list
))
1498 || !EQ (XCAR (XCAR (list
)), key
)))
1501 return CAR_SAFE (list
);
1504 DEFUN ("assoc", Fassoc
, Sassoc
, 2, 2, 0,
1505 doc
: /* Return non-nil if KEY is `equal' to the car of an element of LIST.
1506 The value is actually the first element of LIST whose car equals KEY. */)
1507 (Lisp_Object key
, Lisp_Object list
)
1514 || (CONSP (XCAR (list
))
1515 && (car
= XCAR (XCAR (list
)),
1516 EQ (car
, key
) || !NILP (Fequal (car
, key
)))))
1521 || (CONSP (XCAR (list
))
1522 && (car
= XCAR (XCAR (list
)),
1523 EQ (car
, key
) || !NILP (Fequal (car
, key
)))))
1528 || (CONSP (XCAR (list
))
1529 && (car
= XCAR (XCAR (list
)),
1530 EQ (car
, key
) || !NILP (Fequal (car
, key
)))))
1540 /* Like Fassoc but never report an error and do not allow quits.
1541 Use only on lists known never to be circular. */
1544 assoc_no_quit (Lisp_Object key
, Lisp_Object list
)
1547 && (!CONSP (XCAR (list
))
1548 || (!EQ (XCAR (XCAR (list
)), key
)
1549 && NILP (Fequal (XCAR (XCAR (list
)), key
)))))
1552 return CONSP (list
) ? XCAR (list
) : Qnil
;
1555 DEFUN ("rassq", Frassq
, Srassq
, 2, 2, 0,
1556 doc
: /* Return non-nil if KEY is `eq' to the cdr of an element of LIST.
1557 The value is actually the first element of LIST whose cdr is KEY. */)
1558 (register Lisp_Object key
, Lisp_Object list
)
1563 || (CONSP (XCAR (list
))
1564 && EQ (XCDR (XCAR (list
)), key
)))
1569 || (CONSP (XCAR (list
))
1570 && EQ (XCDR (XCAR (list
)), key
)))
1575 || (CONSP (XCAR (list
))
1576 && EQ (XCDR (XCAR (list
)), key
)))
1586 DEFUN ("rassoc", Frassoc
, Srassoc
, 2, 2, 0,
1587 doc
: /* Return non-nil if KEY is `equal' to the cdr of an element of LIST.
1588 The value is actually the first element of LIST whose cdr equals KEY. */)
1589 (Lisp_Object key
, Lisp_Object list
)
1596 || (CONSP (XCAR (list
))
1597 && (cdr
= XCDR (XCAR (list
)),
1598 EQ (cdr
, key
) || !NILP (Fequal (cdr
, key
)))))
1603 || (CONSP (XCAR (list
))
1604 && (cdr
= XCDR (XCAR (list
)),
1605 EQ (cdr
, key
) || !NILP (Fequal (cdr
, key
)))))
1610 || (CONSP (XCAR (list
))
1611 && (cdr
= XCDR (XCAR (list
)),
1612 EQ (cdr
, key
) || !NILP (Fequal (cdr
, key
)))))
1622 DEFUN ("delq", Fdelq
, Sdelq
, 2, 2, 0,
1623 doc
: /* Delete members of LIST which are `eq' to ELT, and return the result.
1624 More precisely, this function skips any members `eq' to ELT at the
1625 front of LIST, then removes members `eq' to ELT from the remaining
1626 sublist by modifying its list structure, then returns the resulting
1629 Write `(setq foo (delq element foo))' to be sure of correctly changing
1630 the value of a list `foo'. See also `remq', which does not modify the
1632 (register Lisp_Object elt
, Lisp_Object list
)
1634 Lisp_Object tail
, tortoise
, prev
= Qnil
;
1637 FOR_EACH_TAIL (tail
, list
, tortoise
, skip
)
1639 Lisp_Object tem
= XCAR (tail
);
1645 Fsetcdr (prev
, XCDR (tail
));
1653 DEFUN ("delete", Fdelete
, Sdelete
, 2, 2, 0,
1654 doc
: /* Delete members of SEQ which are `equal' to ELT, and return the result.
1655 SEQ must be a sequence (i.e. a list, a vector, or a string).
1656 The return value is a sequence of the same type.
1658 If SEQ is a list, this behaves like `delq', except that it compares
1659 with `equal' instead of `eq'. In particular, it may remove elements
1660 by altering the list structure.
1662 If SEQ is not a list, deletion is never performed destructively;
1663 instead this function creates and returns a new vector or string.
1665 Write `(setq foo (delete element foo))' to be sure of correctly
1666 changing the value of a sequence `foo'. */)
1667 (Lisp_Object elt
, Lisp_Object seq
)
1673 for (i
= n
= 0; i
< ASIZE (seq
); ++i
)
1674 if (NILP (Fequal (AREF (seq
, i
), elt
)))
1677 if (n
!= ASIZE (seq
))
1679 struct Lisp_Vector
*p
= allocate_vector (n
);
1681 for (i
= n
= 0; i
< ASIZE (seq
); ++i
)
1682 if (NILP (Fequal (AREF (seq
, i
), elt
)))
1683 p
->contents
[n
++] = AREF (seq
, i
);
1685 XSETVECTOR (seq
, p
);
1688 else if (STRINGP (seq
))
1690 ptrdiff_t i
, ibyte
, nchars
, nbytes
, cbytes
;
1693 for (i
= nchars
= nbytes
= ibyte
= 0;
1695 ++i
, ibyte
+= cbytes
)
1697 if (STRING_MULTIBYTE (seq
))
1699 c
= STRING_CHAR (SDATA (seq
) + ibyte
);
1700 cbytes
= CHAR_BYTES (c
);
1708 if (!INTEGERP (elt
) || c
!= XINT (elt
))
1715 if (nchars
!= SCHARS (seq
))
1719 tem
= make_uninit_multibyte_string (nchars
, nbytes
);
1720 if (!STRING_MULTIBYTE (seq
))
1721 STRING_SET_UNIBYTE (tem
);
1723 for (i
= nchars
= nbytes
= ibyte
= 0;
1725 ++i
, ibyte
+= cbytes
)
1727 if (STRING_MULTIBYTE (seq
))
1729 c
= STRING_CHAR (SDATA (seq
) + ibyte
);
1730 cbytes
= CHAR_BYTES (c
);
1738 if (!INTEGERP (elt
) || c
!= XINT (elt
))
1740 unsigned char *from
= SDATA (seq
) + ibyte
;
1741 unsigned char *to
= SDATA (tem
) + nbytes
;
1747 for (n
= cbytes
; n
--; )
1757 Lisp_Object tail
, prev
;
1759 for (tail
= seq
, prev
= Qnil
; !NILP (tail
); tail
= XCDR (tail
))
1761 CHECK_LIST_CONS (tail
, seq
);
1763 if (!NILP (Fequal (elt
, XCAR (tail
))))
1768 Fsetcdr (prev
, XCDR (tail
));
1779 DEFUN ("nreverse", Fnreverse
, Snreverse
, 1, 1, 0,
1780 doc
: /* Reverse order of items in a list, vector or string SEQ.
1781 If SEQ is a list, it should be nil-terminated.
1782 This function may destructively modify SEQ to produce the value. */)
1787 else if (STRINGP (seq
))
1788 return Freverse (seq
);
1789 else if (CONSP (seq
))
1791 Lisp_Object prev
, tail
, next
;
1793 for (prev
= Qnil
, tail
= seq
; !NILP (tail
); tail
= next
)
1796 CHECK_LIST_CONS (tail
, tail
);
1798 Fsetcdr (tail
, prev
);
1803 else if (VECTORP (seq
))
1805 ptrdiff_t i
, size
= ASIZE (seq
);
1807 for (i
= 0; i
< size
/ 2; i
++)
1809 Lisp_Object tem
= AREF (seq
, i
);
1810 ASET (seq
, i
, AREF (seq
, size
- i
- 1));
1811 ASET (seq
, size
- i
- 1, tem
);
1814 else if (BOOL_VECTOR_P (seq
))
1816 ptrdiff_t i
, size
= bool_vector_size (seq
);
1818 for (i
= 0; i
< size
/ 2; i
++)
1820 bool tem
= bool_vector_bitref (seq
, i
);
1821 bool_vector_set (seq
, i
, bool_vector_bitref (seq
, size
- i
- 1));
1822 bool_vector_set (seq
, size
- i
- 1, tem
);
1826 wrong_type_argument (Qarrayp
, seq
);
1830 DEFUN ("reverse", Freverse
, Sreverse
, 1, 1, 0,
1831 doc
: /* Return the reversed copy of list, vector, or string SEQ.
1832 See also the function `nreverse', which is used more often. */)
1839 else if (CONSP (seq
))
1841 for (new = Qnil
; CONSP (seq
); seq
= XCDR (seq
))
1844 new = Fcons (XCAR (seq
), new);
1846 CHECK_LIST_END (seq
, seq
);
1848 else if (VECTORP (seq
))
1850 ptrdiff_t i
, size
= ASIZE (seq
);
1852 new = make_uninit_vector (size
);
1853 for (i
= 0; i
< size
; i
++)
1854 ASET (new, i
, AREF (seq
, size
- i
- 1));
1856 else if (BOOL_VECTOR_P (seq
))
1859 EMACS_INT nbits
= bool_vector_size (seq
);
1861 new = make_uninit_bool_vector (nbits
);
1862 for (i
= 0; i
< nbits
; i
++)
1863 bool_vector_set (new, i
, bool_vector_bitref (seq
, nbits
- i
- 1));
1865 else if (STRINGP (seq
))
1867 ptrdiff_t size
= SCHARS (seq
), bytes
= SBYTES (seq
);
1873 new = make_uninit_string (size
);
1874 for (i
= 0; i
< size
; i
++)
1875 SSET (new, i
, SREF (seq
, size
- i
- 1));
1879 unsigned char *p
, *q
;
1881 new = make_uninit_multibyte_string (size
, bytes
);
1882 p
= SDATA (seq
), q
= SDATA (new) + bytes
;
1883 while (q
> SDATA (new))
1887 ch
= STRING_CHAR_AND_LENGTH (p
, len
);
1889 CHAR_STRING (ch
, q
);
1894 wrong_type_argument (Qsequencep
, seq
);
1898 /* Sort LIST using PREDICATE, preserving original order of elements
1899 considered as equal. */
1902 sort_list (Lisp_Object list
, Lisp_Object predicate
)
1904 Lisp_Object front
, back
;
1905 Lisp_Object len
, tem
;
1909 len
= Flength (list
);
1910 length
= XINT (len
);
1914 XSETINT (len
, (length
/ 2) - 1);
1915 tem
= Fnthcdr (len
, list
);
1917 Fsetcdr (tem
, Qnil
);
1919 front
= Fsort (front
, predicate
);
1920 back
= Fsort (back
, predicate
);
1921 return merge (front
, back
, predicate
);
1924 /* Using PRED to compare, return whether A and B are in order.
1925 Compare stably when A appeared before B in the input. */
1927 inorder (Lisp_Object pred
, Lisp_Object a
, Lisp_Object b
)
1929 return NILP (call2 (pred
, b
, a
));
1932 /* Using PRED to compare, merge from ALEN-length A and BLEN-length B
1933 into DEST. Argument arrays must be nonempty and must not overlap,
1934 except that B might be the last part of DEST. */
1936 merge_vectors (Lisp_Object pred
,
1937 ptrdiff_t alen
, Lisp_Object
const a
[restrict
VLA_ELEMS (alen
)],
1938 ptrdiff_t blen
, Lisp_Object
const b
[VLA_ELEMS (blen
)],
1939 Lisp_Object dest
[VLA_ELEMS (alen
+ blen
)])
1941 eassume (0 < alen
&& 0 < blen
);
1942 Lisp_Object
const *alim
= a
+ alen
;
1943 Lisp_Object
const *blim
= b
+ blen
;
1947 if (inorder (pred
, a
[0], b
[0]))
1953 memcpy (dest
, b
, (blim
- b
) * sizeof *dest
);
1962 memcpy (dest
, a
, (alim
- a
) * sizeof *dest
);
1969 /* Using PRED to compare, sort LEN-length VEC in place, using TMP for
1970 temporary storage. LEN must be at least 2. */
1972 sort_vector_inplace (Lisp_Object pred
, ptrdiff_t len
,
1973 Lisp_Object vec
[restrict
VLA_ELEMS (len
)],
1974 Lisp_Object tmp
[restrict
VLA_ELEMS (len
>> 1)])
1977 ptrdiff_t halflen
= len
>> 1;
1978 sort_vector_copy (pred
, halflen
, vec
, tmp
);
1979 if (1 < len
- halflen
)
1980 sort_vector_inplace (pred
, len
- halflen
, vec
+ halflen
, vec
);
1981 merge_vectors (pred
, halflen
, tmp
, len
- halflen
, vec
+ halflen
, vec
);
1984 /* Using PRED to compare, sort from LEN-length SRC into DST.
1985 Len must be positive. */
1987 sort_vector_copy (Lisp_Object pred
, ptrdiff_t len
,
1988 Lisp_Object src
[restrict
VLA_ELEMS (len
)],
1989 Lisp_Object dest
[restrict
VLA_ELEMS (len
)])
1992 ptrdiff_t halflen
= len
>> 1;
1998 sort_vector_inplace (pred
, halflen
, src
, dest
);
1999 if (1 < len
- halflen
)
2000 sort_vector_inplace (pred
, len
- halflen
, src
+ halflen
, dest
);
2001 merge_vectors (pred
, halflen
, src
, len
- halflen
, src
+ halflen
, dest
);
2005 /* Sort VECTOR in place using PREDICATE, preserving original order of
2006 elements considered as equal. */
2009 sort_vector (Lisp_Object vector
, Lisp_Object predicate
)
2011 ptrdiff_t len
= ASIZE (vector
);
2014 ptrdiff_t halflen
= len
>> 1;
2017 SAFE_ALLOCA_LISP (tmp
, halflen
);
2018 for (ptrdiff_t i
= 0; i
< halflen
; i
++)
2019 tmp
[i
] = make_number (0);
2020 sort_vector_inplace (predicate
, len
, XVECTOR (vector
)->contents
, tmp
);
2024 DEFUN ("sort", Fsort
, Ssort
, 2, 2, 0,
2025 doc
: /* Sort SEQ, stably, comparing elements using PREDICATE.
2026 Returns the sorted sequence. SEQ should be a list or vector. SEQ is
2027 modified by side effects. PREDICATE is called with two elements of
2028 SEQ, and should return non-nil if the first element should sort before
2030 (Lisp_Object seq
, Lisp_Object predicate
)
2033 seq
= sort_list (seq
, predicate
);
2034 else if (VECTORP (seq
))
2035 sort_vector (seq
, predicate
);
2036 else if (!NILP (seq
))
2037 wrong_type_argument (Qsequencep
, seq
);
2042 merge (Lisp_Object org_l1
, Lisp_Object org_l2
, Lisp_Object pred
)
2044 Lisp_Object l1
= org_l1
;
2045 Lisp_Object l2
= org_l2
;
2046 Lisp_Object tail
= Qnil
;
2047 Lisp_Object value
= Qnil
;
2067 if (inorder (pred
, Fcar (l1
), Fcar (l2
)))
2082 Fsetcdr (tail
, tem
);
2088 /* This does not check for quits. That is safe since it must terminate. */
2090 DEFUN ("plist-get", Fplist_get
, Splist_get
, 2, 2, 0,
2091 doc
: /* Extract a value from a property list.
2092 PLIST is a property list, which is a list of the form
2093 (PROP1 VALUE1 PROP2 VALUE2...). This function returns the value
2094 corresponding to the given PROP, or nil if PROP is not one of the
2095 properties on the list. This function never signals an error. */)
2096 (Lisp_Object plist
, Lisp_Object prop
)
2098 Lisp_Object tail
, halftail
;
2100 /* halftail is used to detect circular lists. */
2101 tail
= halftail
= plist
;
2102 while (CONSP (tail
) && CONSP (XCDR (tail
)))
2104 if (EQ (prop
, XCAR (tail
)))
2105 return XCAR (XCDR (tail
));
2107 tail
= XCDR (XCDR (tail
));
2108 halftail
= XCDR (halftail
);
2109 if (EQ (tail
, halftail
))
2116 DEFUN ("get", Fget
, Sget
, 2, 2, 0,
2117 doc
: /* Return the value of SYMBOL's PROPNAME property.
2118 This is the last value stored with `(put SYMBOL PROPNAME VALUE)'. */)
2119 (Lisp_Object symbol
, Lisp_Object propname
)
2121 CHECK_SYMBOL (symbol
);
2122 return Fplist_get (XSYMBOL (symbol
)->plist
, propname
);
2125 DEFUN ("plist-put", Fplist_put
, Splist_put
, 3, 3, 0,
2126 doc
: /* Change value in PLIST of PROP to VAL.
2127 PLIST is a property list, which is a list of the form
2128 (PROP1 VALUE1 PROP2 VALUE2 ...). PROP is a symbol and VAL is any object.
2129 If PROP is already a property on the list, its value is set to VAL,
2130 otherwise the new PROP VAL pair is added. The new plist is returned;
2131 use `(setq x (plist-put x prop val))' to be sure to use the new value.
2132 The PLIST is modified by side effects. */)
2133 (Lisp_Object plist
, register Lisp_Object prop
, Lisp_Object val
)
2135 register Lisp_Object tail
, prev
;
2136 Lisp_Object newcell
;
2138 for (tail
= plist
; CONSP (tail
) && CONSP (XCDR (tail
));
2139 tail
= XCDR (XCDR (tail
)))
2141 if (EQ (prop
, XCAR (tail
)))
2143 Fsetcar (XCDR (tail
), val
);
2150 newcell
= Fcons (prop
, Fcons (val
, NILP (prev
) ? plist
: XCDR (XCDR (prev
))));
2154 Fsetcdr (XCDR (prev
), newcell
);
2158 DEFUN ("put", Fput
, Sput
, 3, 3, 0,
2159 doc
: /* Store SYMBOL's PROPNAME property with value VALUE.
2160 It can be retrieved with `(get SYMBOL PROPNAME)'. */)
2161 (Lisp_Object symbol
, Lisp_Object propname
, Lisp_Object value
)
2163 CHECK_SYMBOL (symbol
);
2165 (symbol
, Fplist_put (XSYMBOL (symbol
)->plist
, propname
, value
));
2169 DEFUN ("lax-plist-get", Flax_plist_get
, Slax_plist_get
, 2, 2, 0,
2170 doc
: /* Extract a value from a property list, comparing with `equal'.
2171 PLIST is a property list, which is a list of the form
2172 (PROP1 VALUE1 PROP2 VALUE2...). This function returns the value
2173 corresponding to the given PROP, or nil if PROP is not
2174 one of the properties on the list. */)
2175 (Lisp_Object plist
, Lisp_Object prop
)
2180 CONSP (tail
) && CONSP (XCDR (tail
));
2181 tail
= XCDR (XCDR (tail
)))
2183 if (! NILP (Fequal (prop
, XCAR (tail
))))
2184 return XCAR (XCDR (tail
));
2189 CHECK_LIST_END (tail
, prop
);
2194 DEFUN ("lax-plist-put", Flax_plist_put
, Slax_plist_put
, 3, 3, 0,
2195 doc
: /* Change value in PLIST of PROP to VAL, comparing with `equal'.
2196 PLIST is a property list, which is a list of the form
2197 (PROP1 VALUE1 PROP2 VALUE2 ...). PROP and VAL are any objects.
2198 If PROP is already a property on the list, its value is set to VAL,
2199 otherwise the new PROP VAL pair is added. The new plist is returned;
2200 use `(setq x (lax-plist-put x prop val))' to be sure to use the new value.
2201 The PLIST is modified by side effects. */)
2202 (Lisp_Object plist
, register Lisp_Object prop
, Lisp_Object val
)
2204 register Lisp_Object tail
, prev
;
2205 Lisp_Object newcell
;
2207 for (tail
= plist
; CONSP (tail
) && CONSP (XCDR (tail
));
2208 tail
= XCDR (XCDR (tail
)))
2210 if (! NILP (Fequal (prop
, XCAR (tail
))))
2212 Fsetcar (XCDR (tail
), val
);
2219 newcell
= list2 (prop
, val
);
2223 Fsetcdr (XCDR (prev
), newcell
);
2227 DEFUN ("eql", Feql
, Seql
, 2, 2, 0,
2228 doc
: /* Return t if the two args are the same Lisp object.
2229 Floating-point numbers of equal value are `eql', but they may not be `eq'. */)
2230 (Lisp_Object obj1
, Lisp_Object obj2
)
2233 return internal_equal (obj1
, obj2
, 0, 0, Qnil
) ? Qt
: Qnil
;
2235 return EQ (obj1
, obj2
) ? Qt
: Qnil
;
2238 DEFUN ("equal", Fequal
, Sequal
, 2, 2, 0,
2239 doc
: /* Return t if two Lisp objects have similar structure and contents.
2240 They must have the same data type.
2241 Conses are compared by comparing the cars and the cdrs.
2242 Vectors and strings are compared element by element.
2243 Numbers are compared by value, but integers cannot equal floats.
2244 (Use `=' if you want integers and floats to be able to be equal.)
2245 Symbols must match exactly. */)
2246 (register Lisp_Object o1
, Lisp_Object o2
)
2248 return internal_equal (o1
, o2
, 0, 0, Qnil
) ? Qt
: Qnil
;
2251 DEFUN ("equal-including-properties", Fequal_including_properties
, Sequal_including_properties
, 2, 2, 0,
2252 doc
: /* Return t if two Lisp objects have similar structure and contents.
2253 This is like `equal' except that it compares the text properties
2254 of strings. (`equal' ignores text properties.) */)
2255 (register Lisp_Object o1
, Lisp_Object o2
)
2257 return internal_equal (o1
, o2
, 0, 1, Qnil
) ? Qt
: Qnil
;
2260 /* DEPTH is current depth of recursion. Signal an error if it
2262 PROPS means compare string text properties too. */
2265 internal_equal (Lisp_Object o1
, Lisp_Object o2
, int depth
, bool props
,
2271 error ("Stack overflow in equal");
2273 ht
= CALLN (Fmake_hash_table
, QCtest
, Qeq
);
2276 case Lisp_Cons
: case Lisp_Misc
: case Lisp_Vectorlike
:
2278 struct Lisp_Hash_Table
*h
= XHASH_TABLE (ht
);
2280 ptrdiff_t i
= hash_lookup (h
, o1
, &hash
);
2282 { /* `o1' was seen already. */
2283 Lisp_Object o2s
= HASH_VALUE (h
, i
);
2284 if (!NILP (Fmemq (o2
, o2s
)))
2287 set_hash_value_slot (h
, i
, Fcons (o2
, o2s
));
2290 hash_put (h
, o1
, Fcons (o2
, Qnil
), hash
);
2300 if (XTYPE (o1
) != XTYPE (o2
))
2309 d1
= extract_float (o1
);
2310 d2
= extract_float (o2
);
2311 /* If d is a NaN, then d != d. Two NaNs should be `equal' even
2312 though they are not =. */
2313 return d1
== d2
|| (d1
!= d1
&& d2
!= d2
);
2317 if (!internal_equal (XCAR (o1
), XCAR (o2
), depth
+ 1, props
, ht
))
2321 /* FIXME: This inf-loops in a circular list! */
2325 if (XMISCTYPE (o1
) != XMISCTYPE (o2
))
2329 if (!internal_equal (OVERLAY_START (o1
), OVERLAY_START (o2
),
2330 depth
+ 1, props
, ht
)
2331 || !internal_equal (OVERLAY_END (o1
), OVERLAY_END (o2
),
2332 depth
+ 1, props
, ht
))
2334 o1
= XOVERLAY (o1
)->plist
;
2335 o2
= XOVERLAY (o2
)->plist
;
2340 return (XMARKER (o1
)->buffer
== XMARKER (o2
)->buffer
2341 && (XMARKER (o1
)->buffer
== 0
2342 || XMARKER (o1
)->bytepos
== XMARKER (o2
)->bytepos
));
2346 case Lisp_Vectorlike
:
2349 ptrdiff_t size
= ASIZE (o1
);
2350 /* Pseudovectors have the type encoded in the size field, so this test
2351 actually checks that the objects have the same type as well as the
2353 if (ASIZE (o2
) != size
)
2355 /* Boolvectors are compared much like strings. */
2356 if (BOOL_VECTOR_P (o1
))
2358 EMACS_INT size
= bool_vector_size (o1
);
2359 if (size
!= bool_vector_size (o2
))
2361 if (memcmp (bool_vector_data (o1
), bool_vector_data (o2
),
2362 bool_vector_bytes (size
)))
2366 if (WINDOW_CONFIGURATIONP (o1
))
2367 return compare_window_configurations (o1
, o2
, 0);
2369 /* Aside from them, only true vectors, char-tables, compiled
2370 functions, and fonts (font-spec, font-entity, font-object)
2371 are sensible to compare, so eliminate the others now. */
2372 if (size
& PSEUDOVECTOR_FLAG
)
2374 if (((size
& PVEC_TYPE_MASK
) >> PSEUDOVECTOR_AREA_BITS
)
2377 size
&= PSEUDOVECTOR_SIZE_MASK
;
2379 for (i
= 0; i
< size
; i
++)
2384 if (!internal_equal (v1
, v2
, depth
+ 1, props
, ht
))
2392 if (SCHARS (o1
) != SCHARS (o2
))
2394 if (SBYTES (o1
) != SBYTES (o2
))
2396 if (memcmp (SDATA (o1
), SDATA (o2
), SBYTES (o1
)))
2398 if (props
&& !compare_string_intervals (o1
, o2
))
2410 DEFUN ("fillarray", Ffillarray
, Sfillarray
, 2, 2, 0,
2411 doc
: /* Store each element of ARRAY with ITEM.
2412 ARRAY is a vector, string, char-table, or bool-vector. */)
2413 (Lisp_Object array
, Lisp_Object item
)
2415 register ptrdiff_t size
, idx
;
2417 if (VECTORP (array
))
2418 for (idx
= 0, size
= ASIZE (array
); idx
< size
; idx
++)
2419 ASET (array
, idx
, item
);
2420 else if (CHAR_TABLE_P (array
))
2424 for (i
= 0; i
< (1 << CHARTAB_SIZE_BITS_0
); i
++)
2425 set_char_table_contents (array
, i
, item
);
2426 set_char_table_defalt (array
, item
);
2428 else if (STRINGP (array
))
2430 register unsigned char *p
= SDATA (array
);
2432 CHECK_CHARACTER (item
);
2433 charval
= XFASTINT (item
);
2434 size
= SCHARS (array
);
2435 if (STRING_MULTIBYTE (array
))
2437 unsigned char str
[MAX_MULTIBYTE_LENGTH
];
2438 int len
= CHAR_STRING (charval
, str
);
2439 ptrdiff_t size_byte
= SBYTES (array
);
2442 if (INT_MULTIPLY_WRAPV (size
, len
, &product
) || product
!= size_byte
)
2443 error ("Attempt to change byte length of a string");
2444 for (idx
= 0; idx
< size_byte
; idx
++)
2445 *p
++ = str
[idx
% len
];
2448 for (idx
= 0; idx
< size
; idx
++)
2451 else if (BOOL_VECTOR_P (array
))
2452 return bool_vector_fill (array
, item
);
2454 wrong_type_argument (Qarrayp
, array
);
2458 DEFUN ("clear-string", Fclear_string
, Sclear_string
,
2460 doc
: /* Clear the contents of STRING.
2461 This makes STRING unibyte and may change its length. */)
2462 (Lisp_Object string
)
2465 CHECK_STRING (string
);
2466 len
= SBYTES (string
);
2467 memset (SDATA (string
), 0, len
);
2468 STRING_SET_CHARS (string
, len
);
2469 STRING_SET_UNIBYTE (string
);
2475 nconc2 (Lisp_Object s1
, Lisp_Object s2
)
2477 return CALLN (Fnconc
, s1
, s2
);
2480 DEFUN ("nconc", Fnconc
, Snconc
, 0, MANY
, 0,
2481 doc
: /* Concatenate any number of lists by altering them.
2482 Only the last argument is not altered, and need not be a list.
2483 usage: (nconc &rest LISTS) */)
2484 (ptrdiff_t nargs
, Lisp_Object
*args
)
2487 register Lisp_Object tail
, tem
, val
;
2491 for (argnum
= 0; argnum
< nargs
; argnum
++)
2494 if (NILP (tem
)) continue;
2499 if (argnum
+ 1 == nargs
) break;
2501 CHECK_LIST_CONS (tem
, tem
);
2510 tem
= args
[argnum
+ 1];
2511 Fsetcdr (tail
, tem
);
2513 args
[argnum
+ 1] = tail
;
2519 /* This is the guts of all mapping functions.
2520 Apply FN to each element of SEQ, one by one,
2521 storing the results into elements of VALS, a C vector of Lisp_Objects.
2522 LENI is the length of VALS, which should also be the length of SEQ. */
2525 mapcar1 (EMACS_INT leni
, Lisp_Object
*vals
, Lisp_Object fn
, Lisp_Object seq
)
2527 Lisp_Object tail
, dummy
;
2530 if (VECTORP (seq
) || COMPILEDP (seq
))
2532 for (i
= 0; i
< leni
; i
++)
2534 dummy
= call1 (fn
, AREF (seq
, i
));
2539 else if (BOOL_VECTOR_P (seq
))
2541 for (i
= 0; i
< leni
; i
++)
2543 dummy
= call1 (fn
, bool_vector_ref (seq
, i
));
2548 else if (STRINGP (seq
))
2552 for (i
= 0, i_byte
= 0; i
< leni
;)
2555 ptrdiff_t i_before
= i
;
2557 FETCH_STRING_CHAR_ADVANCE (c
, seq
, i
, i_byte
);
2558 XSETFASTINT (dummy
, c
);
2559 dummy
= call1 (fn
, dummy
);
2561 vals
[i_before
] = dummy
;
2564 else /* Must be a list, since Flength did not get an error */
2567 for (i
= 0; i
< leni
&& CONSP (tail
); i
++)
2569 dummy
= call1 (fn
, XCAR (tail
));
2577 DEFUN ("mapconcat", Fmapconcat
, Smapconcat
, 3, 3, 0,
2578 doc
: /* Apply FUNCTION to each element of SEQUENCE, and concat the results as strings.
2579 In between each pair of results, stick in SEPARATOR. Thus, " " as
2580 SEPARATOR results in spaces between the values returned by FUNCTION.
2581 SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
2582 (Lisp_Object function
, Lisp_Object sequence
, Lisp_Object separator
)
2592 len
= Flength (sequence
);
2593 if (CHAR_TABLE_P (sequence
))
2594 wrong_type_argument (Qlistp
, sequence
);
2596 nargs
= leni
+ leni
- 1;
2597 if (nargs
< 0) return empty_unibyte_string
;
2599 SAFE_ALLOCA_LISP (args
, nargs
);
2601 mapcar1 (leni
, args
, function
, sequence
);
2603 for (i
= leni
- 1; i
> 0; i
--)
2604 args
[i
+ i
] = args
[i
];
2606 for (i
= 1; i
< nargs
; i
+= 2)
2607 args
[i
] = separator
;
2609 ret
= Fconcat (nargs
, args
);
2615 DEFUN ("mapcar", Fmapcar
, Smapcar
, 2, 2, 0,
2616 doc
: /* Apply FUNCTION to each element of SEQUENCE, and make a list of the results.
2617 The result is a list just as long as SEQUENCE.
2618 SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
2619 (Lisp_Object function
, Lisp_Object sequence
)
2621 register Lisp_Object len
;
2622 register EMACS_INT leni
;
2623 register Lisp_Object
*args
;
2627 len
= Flength (sequence
);
2628 if (CHAR_TABLE_P (sequence
))
2629 wrong_type_argument (Qlistp
, sequence
);
2630 leni
= XFASTINT (len
);
2632 SAFE_ALLOCA_LISP (args
, leni
);
2634 mapcar1 (leni
, args
, function
, sequence
);
2636 ret
= Flist (leni
, args
);
2642 DEFUN ("mapc", Fmapc
, Smapc
, 2, 2, 0,
2643 doc
: /* Apply FUNCTION to each element of SEQUENCE for side effects only.
2644 Unlike `mapcar', don't accumulate the results. Return SEQUENCE.
2645 SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
2646 (Lisp_Object function
, Lisp_Object sequence
)
2648 register EMACS_INT leni
;
2650 leni
= XFASTINT (Flength (sequence
));
2651 if (CHAR_TABLE_P (sequence
))
2652 wrong_type_argument (Qlistp
, sequence
);
2653 mapcar1 (leni
, 0, function
, sequence
);
2658 /* This is how C code calls `yes-or-no-p' and allows the user
2662 do_yes_or_no_p (Lisp_Object prompt
)
2664 return call1 (intern ("yes-or-no-p"), prompt
);
2667 DEFUN ("yes-or-no-p", Fyes_or_no_p
, Syes_or_no_p
, 1, 1, 0,
2668 doc
: /* Ask user a yes-or-no question.
2669 Return t if answer is yes, and nil if the answer is no.
2670 PROMPT is the string to display to ask the question. It should end in
2671 a space; `yes-or-no-p' adds \"(yes or no) \" to it.
2673 The user must confirm the answer with RET, and can edit it until it
2676 If dialog boxes are supported, a dialog box will be used
2677 if `last-nonmenu-event' is nil, and `use-dialog-box' is non-nil. */)
2678 (Lisp_Object prompt
)
2682 CHECK_STRING (prompt
);
2684 if ((NILP (last_nonmenu_event
) || CONSP (last_nonmenu_event
))
2685 && use_dialog_box
&& ! NILP (last_input_event
))
2687 Lisp_Object pane
, menu
, obj
;
2688 redisplay_preserve_echo_area (4);
2689 pane
= list2 (Fcons (build_string ("Yes"), Qt
),
2690 Fcons (build_string ("No"), Qnil
));
2691 menu
= Fcons (prompt
, pane
);
2692 obj
= Fx_popup_dialog (Qt
, menu
, Qnil
);
2696 AUTO_STRING (yes_or_no
, "(yes or no) ");
2697 prompt
= CALLN (Fconcat
, prompt
, yes_or_no
);
2701 ans
= Fdowncase (Fread_from_minibuffer (prompt
, Qnil
, Qnil
, Qnil
,
2702 Qyes_or_no_p_history
, Qnil
,
2704 if (SCHARS (ans
) == 3 && !strcmp (SSDATA (ans
), "yes"))
2706 if (SCHARS (ans
) == 2 && !strcmp (SSDATA (ans
), "no"))
2711 message1 ("Please answer yes or no.");
2712 Fsleep_for (make_number (2), Qnil
);
2716 DEFUN ("load-average", Fload_average
, Sload_average
, 0, 1, 0,
2717 doc
: /* Return list of 1 minute, 5 minute and 15 minute load averages.
2719 Each of the three load averages is multiplied by 100, then converted
2722 When USE-FLOATS is non-nil, floats will be used instead of integers.
2723 These floats are not multiplied by 100.
2725 If the 5-minute or 15-minute load averages are not available, return a
2726 shortened list, containing only those averages which are available.
2728 An error is thrown if the load average can't be obtained. In some
2729 cases making it work would require Emacs being installed setuid or
2730 setgid so that it can read kernel information, and that usually isn't
2732 (Lisp_Object use_floats
)
2735 int loads
= getloadavg (load_ave
, 3);
2736 Lisp_Object ret
= Qnil
;
2739 error ("load-average not implemented for this operating system");
2743 Lisp_Object load
= (NILP (use_floats
)
2744 ? make_number (100.0 * load_ave
[loads
])
2745 : make_float (load_ave
[loads
]));
2746 ret
= Fcons (load
, ret
);
2752 DEFUN ("featurep", Ffeaturep
, Sfeaturep
, 1, 2, 0,
2753 doc
: /* Return t if FEATURE is present in this Emacs.
2755 Use this to conditionalize execution of lisp code based on the
2756 presence or absence of Emacs or environment extensions.
2757 Use `provide' to declare that a feature is available. This function
2758 looks at the value of the variable `features'. The optional argument
2759 SUBFEATURE can be used to check a specific subfeature of FEATURE. */)
2760 (Lisp_Object feature
, Lisp_Object subfeature
)
2762 register Lisp_Object tem
;
2763 CHECK_SYMBOL (feature
);
2764 tem
= Fmemq (feature
, Vfeatures
);
2765 if (!NILP (tem
) && !NILP (subfeature
))
2766 tem
= Fmember (subfeature
, Fget (feature
, Qsubfeatures
));
2767 return (NILP (tem
)) ? Qnil
: Qt
;
2770 DEFUN ("provide", Fprovide
, Sprovide
, 1, 2, 0,
2771 doc
: /* Announce that FEATURE is a feature of the current Emacs.
2772 The optional argument SUBFEATURES should be a list of symbols listing
2773 particular subfeatures supported in this version of FEATURE. */)
2774 (Lisp_Object feature
, Lisp_Object subfeatures
)
2776 register Lisp_Object tem
;
2777 CHECK_SYMBOL (feature
);
2778 CHECK_LIST (subfeatures
);
2779 if (!NILP (Vautoload_queue
))
2780 Vautoload_queue
= Fcons (Fcons (make_number (0), Vfeatures
),
2782 tem
= Fmemq (feature
, Vfeatures
);
2784 Vfeatures
= Fcons (feature
, Vfeatures
);
2785 if (!NILP (subfeatures
))
2786 Fput (feature
, Qsubfeatures
, subfeatures
);
2787 LOADHIST_ATTACH (Fcons (Qprovide
, feature
));
2789 /* Run any load-hooks for this file. */
2790 tem
= Fassq (feature
, Vafter_load_alist
);
2792 Fmapc (Qfuncall
, XCDR (tem
));
2797 /* `require' and its subroutines. */
2799 /* List of features currently being require'd, innermost first. */
2801 static Lisp_Object require_nesting_list
;
2804 require_unwind (Lisp_Object old_value
)
2806 require_nesting_list
= old_value
;
2809 DEFUN ("require", Frequire
, Srequire
, 1, 3, 0,
2810 doc
: /* If feature FEATURE is not loaded, load it from FILENAME.
2811 If FEATURE is not a member of the list `features', then the feature
2812 is not loaded; so load the file FILENAME.
2813 If FILENAME is omitted, the printname of FEATURE is used as the file name,
2814 and `load' will try to load this name appended with the suffix `.elc',
2815 `.el', or the system-dependent suffix for dynamic module files, in that
2816 order. The name without appended suffix will not be used.
2817 See `get-load-suffixes' for the complete list of suffixes.
2818 If the optional third argument NOERROR is non-nil,
2819 then return nil if the file is not found instead of signaling an error.
2820 Normally the return value is FEATURE.
2821 The normal messages at start and end of loading FILENAME are suppressed. */)
2822 (Lisp_Object feature
, Lisp_Object filename
, Lisp_Object noerror
)
2825 bool from_file
= load_in_progress
;
2827 CHECK_SYMBOL (feature
);
2829 /* Record the presence of `require' in this file
2830 even if the feature specified is already loaded.
2831 But not more than once in any file,
2832 and not when we aren't loading or reading from a file. */
2834 for (tem
= Vcurrent_load_list
; CONSP (tem
); tem
= XCDR (tem
))
2835 if (NILP (XCDR (tem
)) && STRINGP (XCAR (tem
)))
2840 tem
= Fcons (Qrequire
, feature
);
2841 if (NILP (Fmember (tem
, Vcurrent_load_list
)))
2842 LOADHIST_ATTACH (tem
);
2844 tem
= Fmemq (feature
, Vfeatures
);
2848 ptrdiff_t count
= SPECPDL_INDEX ();
2851 /* This is to make sure that loadup.el gives a clear picture
2852 of what files are preloaded and when. */
2853 if (! NILP (Vpurify_flag
))
2854 error ("(require %s) while preparing to dump",
2855 SDATA (SYMBOL_NAME (feature
)));
2857 /* A certain amount of recursive `require' is legitimate,
2858 but if we require the same feature recursively 3 times,
2860 tem
= require_nesting_list
;
2861 while (! NILP (tem
))
2863 if (! NILP (Fequal (feature
, XCAR (tem
))))
2868 error ("Recursive `require' for feature `%s'",
2869 SDATA (SYMBOL_NAME (feature
)));
2871 /* Update the list for any nested `require's that occur. */
2872 record_unwind_protect (require_unwind
, require_nesting_list
);
2873 require_nesting_list
= Fcons (feature
, require_nesting_list
);
2875 /* Value saved here is to be restored into Vautoload_queue */
2876 record_unwind_protect (un_autoload
, Vautoload_queue
);
2877 Vautoload_queue
= Qt
;
2879 /* Load the file. */
2880 tem
= Fload (NILP (filename
) ? Fsymbol_name (feature
) : filename
,
2881 noerror
, Qt
, Qnil
, (NILP (filename
) ? Qt
: Qnil
));
2883 /* If load failed entirely, return nil. */
2885 return unbind_to (count
, Qnil
);
2887 tem
= Fmemq (feature
, Vfeatures
);
2889 error ("Required feature `%s' was not provided",
2890 SDATA (SYMBOL_NAME (feature
)));
2892 /* Once loading finishes, don't undo it. */
2893 Vautoload_queue
= Qt
;
2894 feature
= unbind_to (count
, feature
);
2900 /* Primitives for work of the "widget" library.
2901 In an ideal world, this section would not have been necessary.
2902 However, lisp function calls being as slow as they are, it turns
2903 out that some functions in the widget library (wid-edit.el) are the
2904 bottleneck of Widget operation. Here is their translation to C,
2905 for the sole reason of efficiency. */
2907 DEFUN ("plist-member", Fplist_member
, Splist_member
, 2, 2, 0,
2908 doc
: /* Return non-nil if PLIST has the property PROP.
2909 PLIST is a property list, which is a list of the form
2910 (PROP1 VALUE1 PROP2 VALUE2 ...). PROP is a symbol.
2911 Unlike `plist-get', this allows you to distinguish between a missing
2912 property and a property with the value nil.
2913 The value is actually the tail of PLIST whose car is PROP. */)
2914 (Lisp_Object plist
, Lisp_Object prop
)
2916 while (CONSP (plist
) && !EQ (XCAR (plist
), prop
))
2918 plist
= XCDR (plist
);
2919 plist
= CDR (plist
);
2925 DEFUN ("widget-put", Fwidget_put
, Swidget_put
, 3, 3, 0,
2926 doc
: /* In WIDGET, set PROPERTY to VALUE.
2927 The value can later be retrieved with `widget-get'. */)
2928 (Lisp_Object widget
, Lisp_Object property
, Lisp_Object value
)
2930 CHECK_CONS (widget
);
2931 XSETCDR (widget
, Fplist_put (XCDR (widget
), property
, value
));
2935 DEFUN ("widget-get", Fwidget_get
, Swidget_get
, 2, 2, 0,
2936 doc
: /* In WIDGET, get the value of PROPERTY.
2937 The value could either be specified when the widget was created, or
2938 later with `widget-put'. */)
2939 (Lisp_Object widget
, Lisp_Object property
)
2947 CHECK_CONS (widget
);
2948 tmp
= Fplist_member (XCDR (widget
), property
);
2954 tmp
= XCAR (widget
);
2957 widget
= Fget (tmp
, Qwidget_type
);
2961 DEFUN ("widget-apply", Fwidget_apply
, Swidget_apply
, 2, MANY
, 0,
2962 doc
: /* Apply the value of WIDGET's PROPERTY to the widget itself.
2963 ARGS are passed as extra arguments to the function.
2964 usage: (widget-apply WIDGET PROPERTY &rest ARGS) */)
2965 (ptrdiff_t nargs
, Lisp_Object
*args
)
2967 Lisp_Object widget
= args
[0];
2968 Lisp_Object property
= args
[1];
2969 Lisp_Object propval
= Fwidget_get (widget
, property
);
2970 Lisp_Object trailing_args
= Flist (nargs
- 2, args
+ 2);
2971 Lisp_Object result
= CALLN (Fapply
, propval
, widget
, trailing_args
);
2975 #ifdef HAVE_LANGINFO_CODESET
2976 #include <langinfo.h>
2979 DEFUN ("locale-info", Flocale_info
, Slocale_info
, 1, 1, 0,
2980 doc
: /* Access locale data ITEM for the current C locale, if available.
2981 ITEM should be one of the following:
2983 `codeset', returning the character set as a string (locale item CODESET);
2985 `days', returning a 7-element vector of day names (locale items DAY_n);
2987 `months', returning a 12-element vector of month names (locale items MON_n);
2989 `paper', returning a list (WIDTH HEIGHT) for the default paper size,
2990 both measured in millimeters (locale items PAPER_WIDTH, PAPER_HEIGHT).
2992 If the system can't provide such information through a call to
2993 `nl_langinfo', or if ITEM isn't from the list above, return nil.
2995 See also Info node `(libc)Locales'.
2997 The data read from the system are decoded using `locale-coding-system'. */)
3001 #ifdef HAVE_LANGINFO_CODESET
3003 if (EQ (item
, Qcodeset
))
3005 str
= nl_langinfo (CODESET
);
3006 return build_string (str
);
3009 else if (EQ (item
, Qdays
)) /* e.g. for calendar-day-name-array */
3011 Lisp_Object v
= Fmake_vector (make_number (7), Qnil
);
3012 const int days
[7] = {DAY_1
, DAY_2
, DAY_3
, DAY_4
, DAY_5
, DAY_6
, DAY_7
};
3014 synchronize_system_time_locale ();
3015 for (i
= 0; i
< 7; i
++)
3017 str
= nl_langinfo (days
[i
]);
3018 val
= build_unibyte_string (str
);
3019 /* Fixme: Is this coding system necessarily right, even if
3020 it is consistent with CODESET? If not, what to do? */
3021 ASET (v
, i
, code_convert_string_norecord (val
, Vlocale_coding_system
,
3028 else if (EQ (item
, Qmonths
)) /* e.g. for calendar-month-name-array */
3030 Lisp_Object v
= Fmake_vector (make_number (12), Qnil
);
3031 const int months
[12] = {MON_1
, MON_2
, MON_3
, MON_4
, MON_5
, MON_6
, MON_7
,
3032 MON_8
, MON_9
, MON_10
, MON_11
, MON_12
};
3034 synchronize_system_time_locale ();
3035 for (i
= 0; i
< 12; i
++)
3037 str
= nl_langinfo (months
[i
]);
3038 val
= build_unibyte_string (str
);
3039 ASET (v
, i
, code_convert_string_norecord (val
, Vlocale_coding_system
,
3045 /* LC_PAPER stuff isn't defined as accessible in glibc as of 2.3.1,
3046 but is in the locale files. This could be used by ps-print. */
3048 else if (EQ (item
, Qpaper
))
3049 return list2i (nl_langinfo (PAPER_WIDTH
), nl_langinfo (PAPER_HEIGHT
));
3050 #endif /* PAPER_WIDTH */
3051 #endif /* HAVE_LANGINFO_CODESET*/
3055 /* base64 encode/decode functions (RFC 2045).
3056 Based on code from GNU recode. */
3058 #define MIME_LINE_LENGTH 76
3060 #define IS_ASCII(Character) \
3062 #define IS_BASE64(Character) \
3063 (IS_ASCII (Character) && base64_char_to_value[Character] >= 0)
3064 #define IS_BASE64_IGNORABLE(Character) \
3065 ((Character) == ' ' || (Character) == '\t' || (Character) == '\n' \
3066 || (Character) == '\f' || (Character) == '\r')
3068 /* Used by base64_decode_1 to retrieve a non-base64-ignorable
3069 character or return retval if there are no characters left to
3071 #define READ_QUADRUPLET_BYTE(retval) \
3076 if (nchars_return) \
3077 *nchars_return = nchars; \
3082 while (IS_BASE64_IGNORABLE (c))
3084 /* Table of characters coding the 64 values. */
3085 static const char base64_value_to_char
[64] =
3087 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 0- 9 */
3088 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 10-19 */
3089 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', /* 20-29 */
3090 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', /* 30-39 */
3091 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', /* 40-49 */
3092 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', /* 50-59 */
3093 '8', '9', '+', '/' /* 60-63 */
3096 /* Table of base64 values for first 128 characters. */
3097 static const short base64_char_to_value
[128] =
3099 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0- 9 */
3100 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10- 19 */
3101 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20- 29 */
3102 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 30- 39 */
3103 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, /* 40- 49 */
3104 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, /* 50- 59 */
3105 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, /* 60- 69 */
3106 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 70- 79 */
3107 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, /* 80- 89 */
3108 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, /* 90- 99 */
3109 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, /* 100-109 */
3110 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, /* 110-119 */
3111 49, 50, 51, -1, -1, -1, -1, -1 /* 120-127 */
3114 /* The following diagram shows the logical steps by which three octets
3115 get transformed into four base64 characters.
3117 .--------. .--------. .--------.
3118 |aaaaaabb| |bbbbcccc| |ccdddddd|
3119 `--------' `--------' `--------'
3121 .--------+--------+--------+--------.
3122 |00aaaaaa|00bbbbbb|00cccccc|00dddddd|
3123 `--------+--------+--------+--------'
3125 .--------+--------+--------+--------.
3126 |AAAAAAAA|BBBBBBBB|CCCCCCCC|DDDDDDDD|
3127 `--------+--------+--------+--------'
3129 The octets are divided into 6 bit chunks, which are then encoded into
3130 base64 characters. */
3133 static ptrdiff_t base64_encode_1 (const char *, char *, ptrdiff_t, bool, bool);
3134 static ptrdiff_t base64_decode_1 (const char *, char *, ptrdiff_t, bool,
3137 DEFUN ("base64-encode-region", Fbase64_encode_region
, Sbase64_encode_region
,
3139 doc
: /* Base64-encode the region between BEG and END.
3140 Return the length of the encoded text.
3141 Optional third argument NO-LINE-BREAK means do not break long lines
3142 into shorter lines. */)
3143 (Lisp_Object beg
, Lisp_Object end
, Lisp_Object no_line_break
)
3146 ptrdiff_t allength
, length
;
3147 ptrdiff_t ibeg
, iend
, encoded_length
;
3148 ptrdiff_t old_pos
= PT
;
3151 validate_region (&beg
, &end
);
3153 ibeg
= CHAR_TO_BYTE (XFASTINT (beg
));
3154 iend
= CHAR_TO_BYTE (XFASTINT (end
));
3155 move_gap_both (XFASTINT (beg
), ibeg
);
3157 /* We need to allocate enough room for encoding the text.
3158 We need 33 1/3% more space, plus a newline every 76
3159 characters, and then we round up. */
3160 length
= iend
- ibeg
;
3161 allength
= length
+ length
/3 + 1;
3162 allength
+= allength
/ MIME_LINE_LENGTH
+ 1 + 6;
3164 encoded
= SAFE_ALLOCA (allength
);
3165 encoded_length
= base64_encode_1 ((char *) BYTE_POS_ADDR (ibeg
),
3166 encoded
, length
, NILP (no_line_break
),
3167 !NILP (BVAR (current_buffer
, enable_multibyte_characters
)));
3168 if (encoded_length
> allength
)
3171 if (encoded_length
< 0)
3173 /* The encoding wasn't possible. */
3175 error ("Multibyte character in data for base64 encoding");
3178 /* Now we have encoded the region, so we insert the new contents
3179 and delete the old. (Insert first in order to preserve markers.) */
3180 SET_PT_BOTH (XFASTINT (beg
), ibeg
);
3181 insert (encoded
, encoded_length
);
3183 del_range_byte (ibeg
+ encoded_length
, iend
+ encoded_length
, 1);
3185 /* If point was outside of the region, restore it exactly; else just
3186 move to the beginning of the region. */
3187 if (old_pos
>= XFASTINT (end
))
3188 old_pos
+= encoded_length
- (XFASTINT (end
) - XFASTINT (beg
));
3189 else if (old_pos
> XFASTINT (beg
))
3190 old_pos
= XFASTINT (beg
);
3193 /* We return the length of the encoded text. */
3194 return make_number (encoded_length
);
3197 DEFUN ("base64-encode-string", Fbase64_encode_string
, Sbase64_encode_string
,
3199 doc
: /* Base64-encode STRING and return the result.
3200 Optional second argument NO-LINE-BREAK means do not break long lines
3201 into shorter lines. */)
3202 (Lisp_Object string
, Lisp_Object no_line_break
)
3204 ptrdiff_t allength
, length
, encoded_length
;
3206 Lisp_Object encoded_string
;
3209 CHECK_STRING (string
);
3211 /* We need to allocate enough room for encoding the text.
3212 We need 33 1/3% more space, plus a newline every 76
3213 characters, and then we round up. */
3214 length
= SBYTES (string
);
3215 allength
= length
+ length
/3 + 1;
3216 allength
+= allength
/ MIME_LINE_LENGTH
+ 1 + 6;
3218 /* We need to allocate enough room for decoding the text. */
3219 encoded
= SAFE_ALLOCA (allength
);
3221 encoded_length
= base64_encode_1 (SSDATA (string
),
3222 encoded
, length
, NILP (no_line_break
),
3223 STRING_MULTIBYTE (string
));
3224 if (encoded_length
> allength
)
3227 if (encoded_length
< 0)
3229 /* The encoding wasn't possible. */
3230 error ("Multibyte character in data for base64 encoding");
3233 encoded_string
= make_unibyte_string (encoded
, encoded_length
);
3236 return encoded_string
;
3240 base64_encode_1 (const char *from
, char *to
, ptrdiff_t length
,
3241 bool line_break
, bool multibyte
)
3254 c
= STRING_CHAR_AND_LENGTH ((unsigned char *) from
+ i
, bytes
);
3255 if (CHAR_BYTE8_P (c
))
3256 c
= CHAR_TO_BYTE8 (c
);
3264 /* Wrap line every 76 characters. */
3268 if (counter
< MIME_LINE_LENGTH
/ 4)
3277 /* Process first byte of a triplet. */
3279 *e
++ = base64_value_to_char
[0x3f & c
>> 2];
3280 value
= (0x03 & c
) << 4;
3282 /* Process second byte of a triplet. */
3286 *e
++ = base64_value_to_char
[value
];
3294 c
= STRING_CHAR_AND_LENGTH ((unsigned char *) from
+ i
, bytes
);
3295 if (CHAR_BYTE8_P (c
))
3296 c
= CHAR_TO_BYTE8 (c
);
3304 *e
++ = base64_value_to_char
[value
| (0x0f & c
>> 4)];
3305 value
= (0x0f & c
) << 2;
3307 /* Process third byte of a triplet. */
3311 *e
++ = base64_value_to_char
[value
];
3318 c
= STRING_CHAR_AND_LENGTH ((unsigned char *) from
+ i
, bytes
);
3319 if (CHAR_BYTE8_P (c
))
3320 c
= CHAR_TO_BYTE8 (c
);
3328 *e
++ = base64_value_to_char
[value
| (0x03 & c
>> 6)];
3329 *e
++ = base64_value_to_char
[0x3f & c
];
3336 DEFUN ("base64-decode-region", Fbase64_decode_region
, Sbase64_decode_region
,
3338 doc
: /* Base64-decode the region between BEG and END.
3339 Return the length of the decoded text.
3340 If the region can't be decoded, signal an error and don't modify the buffer. */)
3341 (Lisp_Object beg
, Lisp_Object end
)
3343 ptrdiff_t ibeg
, iend
, length
, allength
;
3345 ptrdiff_t old_pos
= PT
;
3346 ptrdiff_t decoded_length
;
3347 ptrdiff_t inserted_chars
;
3348 bool multibyte
= !NILP (BVAR (current_buffer
, enable_multibyte_characters
));
3351 validate_region (&beg
, &end
);
3353 ibeg
= CHAR_TO_BYTE (XFASTINT (beg
));
3354 iend
= CHAR_TO_BYTE (XFASTINT (end
));
3356 length
= iend
- ibeg
;
3358 /* We need to allocate enough room for decoding the text. If we are
3359 working on a multibyte buffer, each decoded code may occupy at
3361 allength
= multibyte
? length
* 2 : length
;
3362 decoded
= SAFE_ALLOCA (allength
);
3364 move_gap_both (XFASTINT (beg
), ibeg
);
3365 decoded_length
= base64_decode_1 ((char *) BYTE_POS_ADDR (ibeg
),
3367 multibyte
, &inserted_chars
);
3368 if (decoded_length
> allength
)
3371 if (decoded_length
< 0)
3373 /* The decoding wasn't possible. */
3374 error ("Invalid base64 data");
3377 /* Now we have decoded the region, so we insert the new contents
3378 and delete the old. (Insert first in order to preserve markers.) */
3379 TEMP_SET_PT_BOTH (XFASTINT (beg
), ibeg
);
3380 insert_1_both (decoded
, inserted_chars
, decoded_length
, 0, 1, 0);
3383 /* Delete the original text. */
3384 del_range_both (PT
, PT_BYTE
, XFASTINT (end
) + inserted_chars
,
3385 iend
+ decoded_length
, 1);
3387 /* If point was outside of the region, restore it exactly; else just
3388 move to the beginning of the region. */
3389 if (old_pos
>= XFASTINT (end
))
3390 old_pos
+= inserted_chars
- (XFASTINT (end
) - XFASTINT (beg
));
3391 else if (old_pos
> XFASTINT (beg
))
3392 old_pos
= XFASTINT (beg
);
3393 SET_PT (old_pos
> ZV
? ZV
: old_pos
);
3395 return make_number (inserted_chars
);
3398 DEFUN ("base64-decode-string", Fbase64_decode_string
, Sbase64_decode_string
,
3400 doc
: /* Base64-decode STRING and return the result. */)
3401 (Lisp_Object string
)
3404 ptrdiff_t length
, decoded_length
;
3405 Lisp_Object decoded_string
;
3408 CHECK_STRING (string
);
3410 length
= SBYTES (string
);
3411 /* We need to allocate enough room for decoding the text. */
3412 decoded
= SAFE_ALLOCA (length
);
3414 /* The decoded result should be unibyte. */
3415 decoded_length
= base64_decode_1 (SSDATA (string
), decoded
, length
,
3417 if (decoded_length
> length
)
3419 else if (decoded_length
>= 0)
3420 decoded_string
= make_unibyte_string (decoded
, decoded_length
);
3422 decoded_string
= Qnil
;
3425 if (!STRINGP (decoded_string
))
3426 error ("Invalid base64 data");
3428 return decoded_string
;
3431 /* Base64-decode the data at FROM of LENGTH bytes into TO. If
3432 MULTIBYTE, the decoded result should be in multibyte
3433 form. If NCHARS_RETURN is not NULL, store the number of produced
3434 characters in *NCHARS_RETURN. */
3437 base64_decode_1 (const char *from
, char *to
, ptrdiff_t length
,
3438 bool multibyte
, ptrdiff_t *nchars_return
)
3440 ptrdiff_t i
= 0; /* Used inside READ_QUADRUPLET_BYTE */
3443 unsigned long value
;
3444 ptrdiff_t nchars
= 0;
3448 /* Process first byte of a quadruplet. */
3450 READ_QUADRUPLET_BYTE (e
-to
);
3454 value
= base64_char_to_value
[c
] << 18;
3456 /* Process second byte of a quadruplet. */
3458 READ_QUADRUPLET_BYTE (-1);
3462 value
|= base64_char_to_value
[c
] << 12;
3464 c
= (unsigned char) (value
>> 16);
3465 if (multibyte
&& c
>= 128)
3466 e
+= BYTE8_STRING (c
, e
);
3471 /* Process third byte of a quadruplet. */
3473 READ_QUADRUPLET_BYTE (-1);
3477 READ_QUADRUPLET_BYTE (-1);
3486 value
|= base64_char_to_value
[c
] << 6;
3488 c
= (unsigned char) (0xff & value
>> 8);
3489 if (multibyte
&& c
>= 128)
3490 e
+= BYTE8_STRING (c
, e
);
3495 /* Process fourth byte of a quadruplet. */
3497 READ_QUADRUPLET_BYTE (-1);
3504 value
|= base64_char_to_value
[c
];
3506 c
= (unsigned char) (0xff & value
);
3507 if (multibyte
&& c
>= 128)
3508 e
+= BYTE8_STRING (c
, e
);
3517 /***********************************************************************
3519 ***** Hash Tables *****
3521 ***********************************************************************/
3523 /* Implemented by gerd@gnu.org. This hash table implementation was
3524 inspired by CMUCL hash tables. */
3528 1. For small tables, association lists are probably faster than
3529 hash tables because they have lower overhead.
3531 For uses of hash tables where the O(1) behavior of table
3532 operations is not a requirement, it might therefore be a good idea
3533 not to hash. Instead, we could just do a linear search in the
3534 key_and_value vector of the hash table. This could be done
3535 if a `:linear-search t' argument is given to make-hash-table. */
3538 /* The list of all weak hash tables. Don't staticpro this one. */
3540 static struct Lisp_Hash_Table
*weak_hash_tables
;
3543 /***********************************************************************
3545 ***********************************************************************/
3548 CHECK_HASH_TABLE (Lisp_Object x
)
3550 CHECK_TYPE (HASH_TABLE_P (x
), Qhash_table_p
, x
);
3554 set_hash_key_and_value (struct Lisp_Hash_Table
*h
, Lisp_Object key_and_value
)
3556 h
->key_and_value
= key_and_value
;
3559 set_hash_next (struct Lisp_Hash_Table
*h
, Lisp_Object next
)
3564 set_hash_next_slot (struct Lisp_Hash_Table
*h
, ptrdiff_t idx
, Lisp_Object val
)
3566 gc_aset (h
->next
, idx
, val
);
3569 set_hash_hash (struct Lisp_Hash_Table
*h
, Lisp_Object hash
)
3574 set_hash_hash_slot (struct Lisp_Hash_Table
*h
, ptrdiff_t idx
, Lisp_Object val
)
3576 gc_aset (h
->hash
, idx
, val
);
3579 set_hash_index (struct Lisp_Hash_Table
*h
, Lisp_Object index
)
3584 set_hash_index_slot (struct Lisp_Hash_Table
*h
, ptrdiff_t idx
, Lisp_Object val
)
3586 gc_aset (h
->index
, idx
, val
);
3589 /* If OBJ is a Lisp hash table, return a pointer to its struct
3590 Lisp_Hash_Table. Otherwise, signal an error. */
3592 static struct Lisp_Hash_Table
*
3593 check_hash_table (Lisp_Object obj
)
3595 CHECK_HASH_TABLE (obj
);
3596 return XHASH_TABLE (obj
);
3600 /* Value is the next integer I >= N, N >= 0 which is "almost" a prime
3601 number. A number is "almost" a prime number if it is not divisible
3602 by any integer in the range 2 .. (NEXT_ALMOST_PRIME_LIMIT - 1). */
3605 next_almost_prime (EMACS_INT n
)
3607 verify (NEXT_ALMOST_PRIME_LIMIT
== 11);
3608 for (n
|= 1; ; n
+= 2)
3609 if (n
% 3 != 0 && n
% 5 != 0 && n
% 7 != 0)
3614 /* Find KEY in ARGS which has size NARGS. Don't consider indices for
3615 which USED[I] is non-zero. If found at index I in ARGS, set
3616 USED[I] and USED[I + 1] to 1, and return I + 1. Otherwise return
3617 0. This function is used to extract a keyword/argument pair from
3618 a DEFUN parameter list. */
3621 get_key_arg (Lisp_Object key
, ptrdiff_t nargs
, Lisp_Object
*args
, char *used
)
3625 for (i
= 1; i
< nargs
; i
++)
3626 if (!used
[i
- 1] && EQ (args
[i
- 1], key
))
3637 /* Return a Lisp vector which has the same contents as VEC but has
3638 at least INCR_MIN more entries, where INCR_MIN is positive.
3639 If NITEMS_MAX is not -1, do not grow the vector to be any larger
3640 than NITEMS_MAX. Entries in the resulting
3641 vector that are not copied from VEC are set to nil. */
3644 larger_vector (Lisp_Object vec
, ptrdiff_t incr_min
, ptrdiff_t nitems_max
)
3646 struct Lisp_Vector
*v
;
3647 ptrdiff_t incr
, incr_max
, old_size
, new_size
;
3648 ptrdiff_t C_language_max
= min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *v
->contents
;
3649 ptrdiff_t n_max
= (0 <= nitems_max
&& nitems_max
< C_language_max
3650 ? nitems_max
: C_language_max
);
3651 eassert (VECTORP (vec
));
3652 eassert (0 < incr_min
&& -1 <= nitems_max
);
3653 old_size
= ASIZE (vec
);
3654 incr_max
= n_max
- old_size
;
3655 incr
= max (incr_min
, min (old_size
>> 1, incr_max
));
3656 if (incr_max
< incr
)
3657 memory_full (SIZE_MAX
);
3658 new_size
= old_size
+ incr
;
3659 v
= allocate_vector (new_size
);
3660 memcpy (v
->contents
, XVECTOR (vec
)->contents
, old_size
* sizeof *v
->contents
);
3661 memclear (v
->contents
+ old_size
, incr
* word_size
);
3662 XSETVECTOR (vec
, v
);
3667 /***********************************************************************
3669 ***********************************************************************/
3671 struct hash_table_test hashtest_eq
, hashtest_eql
, hashtest_equal
;
3673 /* Compare KEY1 which has hash code HASH1 and KEY2 with hash code
3674 HASH2 in hash table H using `eql'. Value is true if KEY1 and
3675 KEY2 are the same. */
3678 cmpfn_eql (struct hash_table_test
*ht
,
3682 return (FLOATP (key1
)
3684 && XFLOAT_DATA (key1
) == XFLOAT_DATA (key2
));
3688 /* Compare KEY1 which has hash code HASH1 and KEY2 with hash code
3689 HASH2 in hash table H using `equal'. Value is true if KEY1 and
3690 KEY2 are the same. */
3693 cmpfn_equal (struct hash_table_test
*ht
,
3697 return !NILP (Fequal (key1
, key2
));
3701 /* Compare KEY1 which has hash code HASH1, and KEY2 with hash code
3702 HASH2 in hash table H using H->user_cmp_function. Value is true
3703 if KEY1 and KEY2 are the same. */
3706 cmpfn_user_defined (struct hash_table_test
*ht
,
3710 return !NILP (call2 (ht
->user_cmp_function
, key1
, key2
));
3714 /* Value is a hash code for KEY for use in hash table H which uses
3715 `eq' to compare keys. The hash code returned is guaranteed to fit
3716 in a Lisp integer. */
3719 hashfn_eq (struct hash_table_test
*ht
, Lisp_Object key
)
3721 EMACS_UINT hash
= XHASH (key
) ^ XTYPE (key
);
3725 /* Value is a hash code for KEY for use in hash table H which uses
3726 `eql' to compare keys. The hash code returned is guaranteed to fit
3727 in a Lisp integer. */
3730 hashfn_eql (struct hash_table_test
*ht
, Lisp_Object key
)
3734 hash
= sxhash (key
, 0);
3736 hash
= XHASH (key
) ^ XTYPE (key
);
3740 /* Value is a hash code for KEY for use in hash table H which uses
3741 `equal' to compare keys. The hash code returned is guaranteed to fit
3742 in a Lisp integer. */
3745 hashfn_equal (struct hash_table_test
*ht
, Lisp_Object key
)
3747 EMACS_UINT hash
= sxhash (key
, 0);
3751 /* Value is a hash code for KEY for use in hash table H which uses as
3752 user-defined function to compare keys. The hash code returned is
3753 guaranteed to fit in a Lisp integer. */
3756 hashfn_user_defined (struct hash_table_test
*ht
, Lisp_Object key
)
3758 Lisp_Object hash
= call1 (ht
->user_hash_function
, key
);
3759 return hashfn_eq (ht
, hash
);
3762 /* Allocate basically initialized hash table. */
3764 static struct Lisp_Hash_Table
*
3765 allocate_hash_table (void)
3767 return ALLOCATE_PSEUDOVECTOR (struct Lisp_Hash_Table
,
3768 count
, PVEC_HASH_TABLE
);
3771 /* An upper bound on the size of a hash table index. It must fit in
3772 ptrdiff_t and be a valid Emacs fixnum. */
3773 #define INDEX_SIZE_BOUND \
3774 ((ptrdiff_t) min (MOST_POSITIVE_FIXNUM, PTRDIFF_MAX / word_size))
3776 /* Create and initialize a new hash table.
3778 TEST specifies the test the hash table will use to compare keys.
3779 It must be either one of the predefined tests `eq', `eql' or
3780 `equal' or a symbol denoting a user-defined test named TEST with
3781 test and hash functions USER_TEST and USER_HASH.
3783 Give the table initial capacity SIZE, SIZE >= 0, an integer.
3785 If REHASH_SIZE is an integer, it must be > 0, and this hash table's
3786 new size when it becomes full is computed by adding REHASH_SIZE to
3787 its old size. If REHASH_SIZE is a float, it must be > 1.0, and the
3788 table's new size is computed by multiplying its old size with
3791 REHASH_THRESHOLD must be a float <= 1.0, and > 0. The table will
3792 be resized when the ratio of (number of entries in the table) /
3793 (table size) is >= REHASH_THRESHOLD.
3795 WEAK specifies the weakness of the table. If non-nil, it must be
3796 one of the symbols `key', `value', `key-or-value', or `key-and-value'. */
3799 make_hash_table (struct hash_table_test test
,
3800 Lisp_Object size
, Lisp_Object rehash_size
,
3801 Lisp_Object rehash_threshold
, Lisp_Object weak
)
3803 struct Lisp_Hash_Table
*h
;
3805 EMACS_INT index_size
, sz
;
3809 /* Preconditions. */
3810 eassert (SYMBOLP (test
.name
));
3811 eassert (INTEGERP (size
) && XINT (size
) >= 0);
3812 eassert ((INTEGERP (rehash_size
) && XINT (rehash_size
) > 0)
3813 || (FLOATP (rehash_size
) && 1 < XFLOAT_DATA (rehash_size
)));
3814 eassert (FLOATP (rehash_threshold
)
3815 && 0 < XFLOAT_DATA (rehash_threshold
)
3816 && XFLOAT_DATA (rehash_threshold
) <= 1.0);
3818 if (XFASTINT (size
) == 0)
3819 size
= make_number (1);
3821 sz
= XFASTINT (size
);
3822 index_float
= sz
/ XFLOAT_DATA (rehash_threshold
);
3823 index_size
= (index_float
< INDEX_SIZE_BOUND
+ 1
3824 ? next_almost_prime (index_float
)
3825 : INDEX_SIZE_BOUND
+ 1);
3826 if (INDEX_SIZE_BOUND
< max (index_size
, 2 * sz
))
3827 error ("Hash table too large");
3829 /* Allocate a table and initialize it. */
3830 h
= allocate_hash_table ();
3832 /* Initialize hash table slots. */
3835 h
->rehash_threshold
= rehash_threshold
;
3836 h
->rehash_size
= rehash_size
;
3838 h
->key_and_value
= Fmake_vector (make_number (2 * sz
), Qnil
);
3839 h
->hash
= Fmake_vector (size
, Qnil
);
3840 h
->next
= Fmake_vector (size
, Qnil
);
3841 h
->index
= Fmake_vector (make_number (index_size
), Qnil
);
3843 /* Set up the free list. */
3844 for (i
= 0; i
< sz
- 1; ++i
)
3845 set_hash_next_slot (h
, i
, make_number (i
+ 1));
3846 h
->next_free
= make_number (0);
3848 XSET_HASH_TABLE (table
, h
);
3849 eassert (HASH_TABLE_P (table
));
3850 eassert (XHASH_TABLE (table
) == h
);
3852 /* Maybe add this hash table to the list of all weak hash tables. */
3854 h
->next_weak
= NULL
;
3857 h
->next_weak
= weak_hash_tables
;
3858 weak_hash_tables
= h
;
3865 /* Return a copy of hash table H1. Keys and values are not copied,
3866 only the table itself is. */
3869 copy_hash_table (struct Lisp_Hash_Table
*h1
)
3872 struct Lisp_Hash_Table
*h2
;
3874 h2
= allocate_hash_table ();
3876 h2
->key_and_value
= Fcopy_sequence (h1
->key_and_value
);
3877 h2
->hash
= Fcopy_sequence (h1
->hash
);
3878 h2
->next
= Fcopy_sequence (h1
->next
);
3879 h2
->index
= Fcopy_sequence (h1
->index
);
3880 XSET_HASH_TABLE (table
, h2
);
3882 /* Maybe add this hash table to the list of all weak hash tables. */
3883 if (!NILP (h2
->weak
))
3885 h2
->next_weak
= weak_hash_tables
;
3886 weak_hash_tables
= h2
;
3893 /* Resize hash table H if it's too full. If H cannot be resized
3894 because it's already too large, throw an error. */
3897 maybe_resize_hash_table (struct Lisp_Hash_Table
*h
)
3899 if (NILP (h
->next_free
))
3901 ptrdiff_t old_size
= HASH_TABLE_SIZE (h
);
3902 EMACS_INT new_size
, index_size
, nsize
;
3906 if (INTEGERP (h
->rehash_size
))
3907 new_size
= old_size
+ XFASTINT (h
->rehash_size
);
3910 double float_new_size
= old_size
* XFLOAT_DATA (h
->rehash_size
);
3911 if (float_new_size
< INDEX_SIZE_BOUND
+ 1)
3913 new_size
= float_new_size
;
3914 if (new_size
<= old_size
)
3915 new_size
= old_size
+ 1;
3918 new_size
= INDEX_SIZE_BOUND
+ 1;
3920 index_float
= new_size
/ XFLOAT_DATA (h
->rehash_threshold
);
3921 index_size
= (index_float
< INDEX_SIZE_BOUND
+ 1
3922 ? next_almost_prime (index_float
)
3923 : INDEX_SIZE_BOUND
+ 1);
3924 nsize
= max (index_size
, 2 * new_size
);
3925 if (INDEX_SIZE_BOUND
< nsize
)
3926 error ("Hash table too large to resize");
3928 #ifdef ENABLE_CHECKING
3929 if (HASH_TABLE_P (Vpurify_flag
)
3930 && XHASH_TABLE (Vpurify_flag
) == h
)
3931 message ("Growing hash table to: %"pI
"d", new_size
);
3934 set_hash_key_and_value (h
, larger_vector (h
->key_and_value
,
3935 2 * (new_size
- old_size
), -1));
3936 set_hash_next (h
, larger_vector (h
->next
, new_size
- old_size
, -1));
3937 set_hash_hash (h
, larger_vector (h
->hash
, new_size
- old_size
, -1));
3938 set_hash_index (h
, Fmake_vector (make_number (index_size
), Qnil
));
3940 /* Update the free list. Do it so that new entries are added at
3941 the end of the free list. This makes some operations like
3943 for (i
= old_size
; i
< new_size
- 1; ++i
)
3944 set_hash_next_slot (h
, i
, make_number (i
+ 1));
3946 if (!NILP (h
->next_free
))
3948 Lisp_Object last
, next
;
3950 last
= h
->next_free
;
3951 while (next
= HASH_NEXT (h
, XFASTINT (last
)),
3955 set_hash_next_slot (h
, XFASTINT (last
), make_number (old_size
));
3958 XSETFASTINT (h
->next_free
, old_size
);
3961 for (i
= 0; i
< old_size
; ++i
)
3962 if (!NILP (HASH_HASH (h
, i
)))
3964 EMACS_UINT hash_code
= XUINT (HASH_HASH (h
, i
));
3965 ptrdiff_t start_of_bucket
= hash_code
% ASIZE (h
->index
);
3966 set_hash_next_slot (h
, i
, HASH_INDEX (h
, start_of_bucket
));
3967 set_hash_index_slot (h
, start_of_bucket
, make_number (i
));
3973 /* Lookup KEY in hash table H. If HASH is non-null, return in *HASH
3974 the hash code of KEY. Value is the index of the entry in H
3975 matching KEY, or -1 if not found. */
3978 hash_lookup (struct Lisp_Hash_Table
*h
, Lisp_Object key
, EMACS_UINT
*hash
)
3980 EMACS_UINT hash_code
;
3981 ptrdiff_t start_of_bucket
;
3984 hash_code
= h
->test
.hashfn (&h
->test
, key
);
3985 eassert ((hash_code
& ~INTMASK
) == 0);
3989 start_of_bucket
= hash_code
% ASIZE (h
->index
);
3990 idx
= HASH_INDEX (h
, start_of_bucket
);
3994 ptrdiff_t i
= XFASTINT (idx
);
3995 if (EQ (key
, HASH_KEY (h
, i
))
3997 && hash_code
== XUINT (HASH_HASH (h
, i
))
3998 && h
->test
.cmpfn (&h
->test
, key
, HASH_KEY (h
, i
))))
4000 idx
= HASH_NEXT (h
, i
);
4003 return NILP (idx
) ? -1 : XFASTINT (idx
);
4007 /* Put an entry into hash table H that associates KEY with VALUE.
4008 HASH is a previously computed hash code of KEY.
4009 Value is the index of the entry in H matching KEY. */
4012 hash_put (struct Lisp_Hash_Table
*h
, Lisp_Object key
, Lisp_Object value
,
4015 ptrdiff_t start_of_bucket
, i
;
4017 eassert ((hash
& ~INTMASK
) == 0);
4019 /* Increment count after resizing because resizing may fail. */
4020 maybe_resize_hash_table (h
);
4023 /* Store key/value in the key_and_value vector. */
4024 i
= XFASTINT (h
->next_free
);
4025 h
->next_free
= HASH_NEXT (h
, i
);
4026 set_hash_key_slot (h
, i
, key
);
4027 set_hash_value_slot (h
, i
, value
);
4029 /* Remember its hash code. */
4030 set_hash_hash_slot (h
, i
, make_number (hash
));
4032 /* Add new entry to its collision chain. */
4033 start_of_bucket
= hash
% ASIZE (h
->index
);
4034 set_hash_next_slot (h
, i
, HASH_INDEX (h
, start_of_bucket
));
4035 set_hash_index_slot (h
, start_of_bucket
, make_number (i
));
4040 /* Remove the entry matching KEY from hash table H, if there is one. */
4043 hash_remove_from_table (struct Lisp_Hash_Table
*h
, Lisp_Object key
)
4045 EMACS_UINT hash_code
;
4046 ptrdiff_t start_of_bucket
;
4047 Lisp_Object idx
, prev
;
4049 hash_code
= h
->test
.hashfn (&h
->test
, key
);
4050 eassert ((hash_code
& ~INTMASK
) == 0);
4051 start_of_bucket
= hash_code
% ASIZE (h
->index
);
4052 idx
= HASH_INDEX (h
, start_of_bucket
);
4057 ptrdiff_t i
= XFASTINT (idx
);
4059 if (EQ (key
, HASH_KEY (h
, i
))
4061 && hash_code
== XUINT (HASH_HASH (h
, i
))
4062 && h
->test
.cmpfn (&h
->test
, key
, HASH_KEY (h
, i
))))
4064 /* Take entry out of collision chain. */
4066 set_hash_index_slot (h
, start_of_bucket
, HASH_NEXT (h
, i
));
4068 set_hash_next_slot (h
, XFASTINT (prev
), HASH_NEXT (h
, i
));
4070 /* Clear slots in key_and_value and add the slots to
4072 set_hash_key_slot (h
, i
, Qnil
);
4073 set_hash_value_slot (h
, i
, Qnil
);
4074 set_hash_hash_slot (h
, i
, Qnil
);
4075 set_hash_next_slot (h
, i
, h
->next_free
);
4076 h
->next_free
= make_number (i
);
4078 eassert (h
->count
>= 0);
4084 idx
= HASH_NEXT (h
, i
);
4090 /* Clear hash table H. */
4093 hash_clear (struct Lisp_Hash_Table
*h
)
4097 ptrdiff_t i
, size
= HASH_TABLE_SIZE (h
);
4099 for (i
= 0; i
< size
; ++i
)
4101 set_hash_next_slot (h
, i
, i
< size
- 1 ? make_number (i
+ 1) : Qnil
);
4102 set_hash_key_slot (h
, i
, Qnil
);
4103 set_hash_value_slot (h
, i
, Qnil
);
4104 set_hash_hash_slot (h
, i
, Qnil
);
4107 for (i
= 0; i
< ASIZE (h
->index
); ++i
)
4108 ASET (h
->index
, i
, Qnil
);
4110 h
->next_free
= make_number (0);
4117 /************************************************************************
4119 ************************************************************************/
4121 /* Sweep weak hash table H. REMOVE_ENTRIES_P means remove
4122 entries from the table that don't survive the current GC.
4123 !REMOVE_ENTRIES_P means mark entries that are in use. Value is
4124 true if anything was marked. */
4127 sweep_weak_table (struct Lisp_Hash_Table
*h
, bool remove_entries_p
)
4129 ptrdiff_t n
= gc_asize (h
->index
);
4130 bool marked
= false;
4132 for (ptrdiff_t bucket
= 0; bucket
< n
; ++bucket
)
4134 Lisp_Object idx
, next
, prev
;
4136 /* Follow collision chain, removing entries that
4137 don't survive this garbage collection. */
4139 for (idx
= HASH_INDEX (h
, bucket
); !NILP (idx
); idx
= next
)
4141 ptrdiff_t i
= XFASTINT (idx
);
4142 bool key_known_to_survive_p
= survives_gc_p (HASH_KEY (h
, i
));
4143 bool value_known_to_survive_p
= survives_gc_p (HASH_VALUE (h
, i
));
4146 if (EQ (h
->weak
, Qkey
))
4147 remove_p
= !key_known_to_survive_p
;
4148 else if (EQ (h
->weak
, Qvalue
))
4149 remove_p
= !value_known_to_survive_p
;
4150 else if (EQ (h
->weak
, Qkey_or_value
))
4151 remove_p
= !(key_known_to_survive_p
|| value_known_to_survive_p
);
4152 else if (EQ (h
->weak
, Qkey_and_value
))
4153 remove_p
= !(key_known_to_survive_p
&& value_known_to_survive_p
);
4157 next
= HASH_NEXT (h
, i
);
4159 if (remove_entries_p
)
4163 /* Take out of collision chain. */
4165 set_hash_index_slot (h
, bucket
, next
);
4167 set_hash_next_slot (h
, XFASTINT (prev
), next
);
4169 /* Add to free list. */
4170 set_hash_next_slot (h
, i
, h
->next_free
);
4173 /* Clear key, value, and hash. */
4174 set_hash_key_slot (h
, i
, Qnil
);
4175 set_hash_value_slot (h
, i
, Qnil
);
4176 set_hash_hash_slot (h
, i
, Qnil
);
4189 /* Make sure key and value survive. */
4190 if (!key_known_to_survive_p
)
4192 mark_object (HASH_KEY (h
, i
));
4196 if (!value_known_to_survive_p
)
4198 mark_object (HASH_VALUE (h
, i
));
4209 /* Remove elements from weak hash tables that don't survive the
4210 current garbage collection. Remove weak tables that don't survive
4211 from Vweak_hash_tables. Called from gc_sweep. */
4213 NO_INLINE
/* For better stack traces */
4215 sweep_weak_hash_tables (void)
4217 struct Lisp_Hash_Table
*h
, *used
, *next
;
4220 /* Mark all keys and values that are in use. Keep on marking until
4221 there is no more change. This is necessary for cases like
4222 value-weak table A containing an entry X -> Y, where Y is used in a
4223 key-weak table B, Z -> Y. If B comes after A in the list of weak
4224 tables, X -> Y might be removed from A, although when looking at B
4225 one finds that it shouldn't. */
4229 for (h
= weak_hash_tables
; h
; h
= h
->next_weak
)
4231 if (h
->header
.size
& ARRAY_MARK_FLAG
)
4232 marked
|= sweep_weak_table (h
, 0);
4237 /* Remove tables and entries that aren't used. */
4238 for (h
= weak_hash_tables
, used
= NULL
; h
; h
= next
)
4240 next
= h
->next_weak
;
4242 if (h
->header
.size
& ARRAY_MARK_FLAG
)
4244 /* TABLE is marked as used. Sweep its contents. */
4246 sweep_weak_table (h
, 1);
4248 /* Add table to the list of used weak hash tables. */
4249 h
->next_weak
= used
;
4254 weak_hash_tables
= used
;
4259 /***********************************************************************
4260 Hash Code Computation
4261 ***********************************************************************/
4263 /* Maximum depth up to which to dive into Lisp structures. */
4265 #define SXHASH_MAX_DEPTH 3
4267 /* Maximum length up to which to take list and vector elements into
4270 #define SXHASH_MAX_LEN 7
4272 /* Return a hash for string PTR which has length LEN. The hash value
4273 can be any EMACS_UINT value. */
4276 hash_string (char const *ptr
, ptrdiff_t len
)
4278 char const *p
= ptr
;
4279 char const *end
= p
+ len
;
4281 EMACS_UINT hash
= 0;
4286 hash
= sxhash_combine (hash
, c
);
4292 /* Return a hash for string PTR which has length LEN. The hash
4293 code returned is guaranteed to fit in a Lisp integer. */
4296 sxhash_string (char const *ptr
, ptrdiff_t len
)
4298 EMACS_UINT hash
= hash_string (ptr
, len
);
4299 return SXHASH_REDUCE (hash
);
4302 /* Return a hash for the floating point value VAL. */
4305 sxhash_float (double val
)
4307 EMACS_UINT hash
= 0;
4309 WORDS_PER_DOUBLE
= (sizeof val
/ sizeof hash
4310 + (sizeof val
% sizeof hash
!= 0))
4314 EMACS_UINT word
[WORDS_PER_DOUBLE
];
4318 memset (&u
.val
+ 1, 0, sizeof u
- sizeof u
.val
);
4319 for (i
= 0; i
< WORDS_PER_DOUBLE
; i
++)
4320 hash
= sxhash_combine (hash
, u
.word
[i
]);
4321 return SXHASH_REDUCE (hash
);
4324 /* Return a hash for list LIST. DEPTH is the current depth in the
4325 list. We don't recurse deeper than SXHASH_MAX_DEPTH in it. */
4328 sxhash_list (Lisp_Object list
, int depth
)
4330 EMACS_UINT hash
= 0;
4333 if (depth
< SXHASH_MAX_DEPTH
)
4335 CONSP (list
) && i
< SXHASH_MAX_LEN
;
4336 list
= XCDR (list
), ++i
)
4338 EMACS_UINT hash2
= sxhash (XCAR (list
), depth
+ 1);
4339 hash
= sxhash_combine (hash
, hash2
);
4344 EMACS_UINT hash2
= sxhash (list
, depth
+ 1);
4345 hash
= sxhash_combine (hash
, hash2
);
4348 return SXHASH_REDUCE (hash
);
4352 /* Return a hash for vector VECTOR. DEPTH is the current depth in
4353 the Lisp structure. */
4356 sxhash_vector (Lisp_Object vec
, int depth
)
4358 EMACS_UINT hash
= ASIZE (vec
);
4361 n
= min (SXHASH_MAX_LEN
, ASIZE (vec
));
4362 for (i
= 0; i
< n
; ++i
)
4364 EMACS_UINT hash2
= sxhash (AREF (vec
, i
), depth
+ 1);
4365 hash
= sxhash_combine (hash
, hash2
);
4368 return SXHASH_REDUCE (hash
);
4371 /* Return a hash for bool-vector VECTOR. */
4374 sxhash_bool_vector (Lisp_Object vec
)
4376 EMACS_INT size
= bool_vector_size (vec
);
4377 EMACS_UINT hash
= size
;
4380 n
= min (SXHASH_MAX_LEN
, bool_vector_words (size
));
4381 for (i
= 0; i
< n
; ++i
)
4382 hash
= sxhash_combine (hash
, bool_vector_data (vec
)[i
]);
4384 return SXHASH_REDUCE (hash
);
4388 /* Return a hash code for OBJ. DEPTH is the current depth in the Lisp
4389 structure. Value is an unsigned integer clipped to INTMASK. */
4392 sxhash (Lisp_Object obj
, int depth
)
4396 if (depth
> SXHASH_MAX_DEPTH
)
4399 switch (XTYPE (obj
))
4411 hash
= sxhash_string (SSDATA (obj
), SBYTES (obj
));
4414 /* This can be everything from a vector to an overlay. */
4415 case Lisp_Vectorlike
:
4417 /* According to the CL HyperSpec, two arrays are equal only if
4418 they are `eq', except for strings and bit-vectors. In
4419 Emacs, this works differently. We have to compare element
4421 hash
= sxhash_vector (obj
, depth
);
4422 else if (BOOL_VECTOR_P (obj
))
4423 hash
= sxhash_bool_vector (obj
);
4425 /* Others are `equal' if they are `eq', so let's take their
4431 hash
= sxhash_list (obj
, depth
);
4435 hash
= sxhash_float (XFLOAT_DATA (obj
));
4447 /***********************************************************************
4449 ***********************************************************************/
4452 DEFUN ("sxhash", Fsxhash
, Ssxhash
, 1, 1, 0,
4453 doc
: /* Compute a hash code for OBJ and return it as integer. */)
4456 EMACS_UINT hash
= sxhash (obj
, 0);
4457 return make_number (hash
);
4461 DEFUN ("make-hash-table", Fmake_hash_table
, Smake_hash_table
, 0, MANY
, 0,
4462 doc
: /* Create and return a new hash table.
4464 Arguments are specified as keyword/argument pairs. The following
4465 arguments are defined:
4467 :test TEST -- TEST must be a symbol that specifies how to compare
4468 keys. Default is `eql'. Predefined are the tests `eq', `eql', and
4469 `equal'. User-supplied test and hash functions can be specified via
4470 `define-hash-table-test'.
4472 :size SIZE -- A hint as to how many elements will be put in the table.
4475 :rehash-size REHASH-SIZE - Indicates how to expand the table when it
4476 fills up. If REHASH-SIZE is an integer, increase the size by that
4477 amount. If it is a float, it must be > 1.0, and the new size is the
4478 old size multiplied by that factor. Default is 1.5.
4480 :rehash-threshold THRESHOLD -- THRESHOLD must a float > 0, and <= 1.0.
4481 Resize the hash table when the ratio (number of entries / table size)
4482 is greater than or equal to THRESHOLD. Default is 0.8.
4484 :weakness WEAK -- WEAK must be one of nil, t, `key', `value',
4485 `key-or-value', or `key-and-value'. If WEAK is not nil, the table
4486 returned is a weak table. Key/value pairs are removed from a weak
4487 hash table when there are no non-weak references pointing to their
4488 key, value, one of key or value, or both key and value, depending on
4489 WEAK. WEAK t is equivalent to `key-and-value'. Default value of WEAK
4492 usage: (make-hash-table &rest KEYWORD-ARGS) */)
4493 (ptrdiff_t nargs
, Lisp_Object
*args
)
4495 Lisp_Object test
, size
, rehash_size
, rehash_threshold
, weak
;
4496 struct hash_table_test testdesc
;
4500 /* The vector `used' is used to keep track of arguments that
4501 have been consumed. */
4502 char *used
= SAFE_ALLOCA (nargs
* sizeof *used
);
4503 memset (used
, 0, nargs
* sizeof *used
);
4505 /* See if there's a `:test TEST' among the arguments. */
4506 i
= get_key_arg (QCtest
, nargs
, args
, used
);
4507 test
= i
? args
[i
] : Qeql
;
4509 testdesc
= hashtest_eq
;
4510 else if (EQ (test
, Qeql
))
4511 testdesc
= hashtest_eql
;
4512 else if (EQ (test
, Qequal
))
4513 testdesc
= hashtest_equal
;
4516 /* See if it is a user-defined test. */
4519 prop
= Fget (test
, Qhash_table_test
);
4520 if (!CONSP (prop
) || !CONSP (XCDR (prop
)))
4521 signal_error ("Invalid hash table test", test
);
4522 testdesc
.name
= test
;
4523 testdesc
.user_cmp_function
= XCAR (prop
);
4524 testdesc
.user_hash_function
= XCAR (XCDR (prop
));
4525 testdesc
.hashfn
= hashfn_user_defined
;
4526 testdesc
.cmpfn
= cmpfn_user_defined
;
4529 /* See if there's a `:size SIZE' argument. */
4530 i
= get_key_arg (QCsize
, nargs
, args
, used
);
4531 size
= i
? args
[i
] : Qnil
;
4533 size
= make_number (DEFAULT_HASH_SIZE
);
4534 else if (!INTEGERP (size
) || XINT (size
) < 0)
4535 signal_error ("Invalid hash table size", size
);
4537 /* Look for `:rehash-size SIZE'. */
4538 i
= get_key_arg (QCrehash_size
, nargs
, args
, used
);
4539 rehash_size
= i
? args
[i
] : make_float (DEFAULT_REHASH_SIZE
);
4540 if (! ((INTEGERP (rehash_size
) && 0 < XINT (rehash_size
))
4541 || (FLOATP (rehash_size
) && 1 < XFLOAT_DATA (rehash_size
))))
4542 signal_error ("Invalid hash table rehash size", rehash_size
);
4544 /* Look for `:rehash-threshold THRESHOLD'. */
4545 i
= get_key_arg (QCrehash_threshold
, nargs
, args
, used
);
4546 rehash_threshold
= i
? args
[i
] : make_float (DEFAULT_REHASH_THRESHOLD
);
4547 if (! (FLOATP (rehash_threshold
)
4548 && 0 < XFLOAT_DATA (rehash_threshold
)
4549 && XFLOAT_DATA (rehash_threshold
) <= 1))
4550 signal_error ("Invalid hash table rehash threshold", rehash_threshold
);
4552 /* Look for `:weakness WEAK'. */
4553 i
= get_key_arg (QCweakness
, nargs
, args
, used
);
4554 weak
= i
? args
[i
] : Qnil
;
4556 weak
= Qkey_and_value
;
4559 && !EQ (weak
, Qvalue
)
4560 && !EQ (weak
, Qkey_or_value
)
4561 && !EQ (weak
, Qkey_and_value
))
4562 signal_error ("Invalid hash table weakness", weak
);
4564 /* Now, all args should have been used up, or there's a problem. */
4565 for (i
= 0; i
< nargs
; ++i
)
4567 signal_error ("Invalid argument list", args
[i
]);
4570 return make_hash_table (testdesc
, size
, rehash_size
, rehash_threshold
, weak
);
4574 DEFUN ("copy-hash-table", Fcopy_hash_table
, Scopy_hash_table
, 1, 1, 0,
4575 doc
: /* Return a copy of hash table TABLE. */)
4578 return copy_hash_table (check_hash_table (table
));
4582 DEFUN ("hash-table-count", Fhash_table_count
, Shash_table_count
, 1, 1, 0,
4583 doc
: /* Return the number of elements in TABLE. */)
4586 return make_number (check_hash_table (table
)->count
);
4590 DEFUN ("hash-table-rehash-size", Fhash_table_rehash_size
,
4591 Shash_table_rehash_size
, 1, 1, 0,
4592 doc
: /* Return the current rehash size of TABLE. */)
4595 return check_hash_table (table
)->rehash_size
;
4599 DEFUN ("hash-table-rehash-threshold", Fhash_table_rehash_threshold
,
4600 Shash_table_rehash_threshold
, 1, 1, 0,
4601 doc
: /* Return the current rehash threshold of TABLE. */)
4604 return check_hash_table (table
)->rehash_threshold
;
4608 DEFUN ("hash-table-size", Fhash_table_size
, Shash_table_size
, 1, 1, 0,
4609 doc
: /* Return the size of TABLE.
4610 The size can be used as an argument to `make-hash-table' to create
4611 a hash table than can hold as many elements as TABLE holds
4612 without need for resizing. */)
4615 struct Lisp_Hash_Table
*h
= check_hash_table (table
);
4616 return make_number (HASH_TABLE_SIZE (h
));
4620 DEFUN ("hash-table-test", Fhash_table_test
, Shash_table_test
, 1, 1, 0,
4621 doc
: /* Return the test TABLE uses. */)
4624 return check_hash_table (table
)->test
.name
;
4628 DEFUN ("hash-table-weakness", Fhash_table_weakness
, Shash_table_weakness
,
4630 doc
: /* Return the weakness of TABLE. */)
4633 return check_hash_table (table
)->weak
;
4637 DEFUN ("hash-table-p", Fhash_table_p
, Shash_table_p
, 1, 1, 0,
4638 doc
: /* Return t if OBJ is a Lisp hash table object. */)
4641 return HASH_TABLE_P (obj
) ? Qt
: Qnil
;
4645 DEFUN ("clrhash", Fclrhash
, Sclrhash
, 1, 1, 0,
4646 doc
: /* Clear hash table TABLE and return it. */)
4649 hash_clear (check_hash_table (table
));
4650 /* Be compatible with XEmacs. */
4655 DEFUN ("gethash", Fgethash
, Sgethash
, 2, 3, 0,
4656 doc
: /* Look up KEY in TABLE and return its associated value.
4657 If KEY is not found, return DFLT which defaults to nil. */)
4658 (Lisp_Object key
, Lisp_Object table
, Lisp_Object dflt
)
4660 struct Lisp_Hash_Table
*h
= check_hash_table (table
);
4661 ptrdiff_t i
= hash_lookup (h
, key
, NULL
);
4662 return i
>= 0 ? HASH_VALUE (h
, i
) : dflt
;
4666 DEFUN ("puthash", Fputhash
, Sputhash
, 3, 3, 0,
4667 doc
: /* Associate KEY with VALUE in hash table TABLE.
4668 If KEY is already present in table, replace its current value with
4669 VALUE. In any case, return VALUE. */)
4670 (Lisp_Object key
, Lisp_Object value
, Lisp_Object table
)
4672 struct Lisp_Hash_Table
*h
= check_hash_table (table
);
4676 i
= hash_lookup (h
, key
, &hash
);
4678 set_hash_value_slot (h
, i
, value
);
4680 hash_put (h
, key
, value
, hash
);
4686 DEFUN ("remhash", Fremhash
, Sremhash
, 2, 2, 0,
4687 doc
: /* Remove KEY from TABLE. */)
4688 (Lisp_Object key
, Lisp_Object table
)
4690 struct Lisp_Hash_Table
*h
= check_hash_table (table
);
4691 hash_remove_from_table (h
, key
);
4696 DEFUN ("maphash", Fmaphash
, Smaphash
, 2, 2, 0,
4697 doc
: /* Call FUNCTION for all entries in hash table TABLE.
4698 FUNCTION is called with two arguments, KEY and VALUE.
4699 `maphash' always returns nil. */)
4700 (Lisp_Object function
, Lisp_Object table
)
4702 struct Lisp_Hash_Table
*h
= check_hash_table (table
);
4704 for (ptrdiff_t i
= 0; i
< HASH_TABLE_SIZE (h
); ++i
)
4705 if (!NILP (HASH_HASH (h
, i
)))
4706 call2 (function
, HASH_KEY (h
, i
), HASH_VALUE (h
, i
));
4712 DEFUN ("define-hash-table-test", Fdefine_hash_table_test
,
4713 Sdefine_hash_table_test
, 3, 3, 0,
4714 doc
: /* Define a new hash table test with name NAME, a symbol.
4716 In hash tables created with NAME specified as test, use TEST to
4717 compare keys, and HASH for computing hash codes of keys.
4719 TEST must be a function taking two arguments and returning non-nil if
4720 both arguments are the same. HASH must be a function taking one
4721 argument and returning an object that is the hash code of the argument.
4722 It should be the case that if (eq (funcall HASH x1) (funcall HASH x2))
4723 returns nil, then (funcall TEST x1 x2) also returns nil. */)
4724 (Lisp_Object name
, Lisp_Object test
, Lisp_Object hash
)
4726 return Fput (name
, Qhash_table_test
, list2 (test
, hash
));
4731 /************************************************************************
4732 MD5, SHA-1, and SHA-2
4733 ************************************************************************/
4740 /* ALGORITHM is a symbol: md5, sha1, sha224 and so on. */
4743 secure_hash (Lisp_Object algorithm
, Lisp_Object object
, Lisp_Object start
,
4744 Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object noerror
,
4748 ptrdiff_t size
, start_char
= 0, start_byte
, end_char
= 0, end_byte
;
4749 register EMACS_INT b
, e
;
4750 register struct buffer
*bp
;
4753 void *(*hash_func
) (const char *, size_t, void *);
4756 CHECK_SYMBOL (algorithm
);
4758 if (STRINGP (object
))
4760 if (NILP (coding_system
))
4762 /* Decide the coding-system to encode the data with. */
4764 if (STRING_MULTIBYTE (object
))
4765 /* use default, we can't guess correct value */
4766 coding_system
= preferred_coding_system ();
4768 coding_system
= Qraw_text
;
4771 if (NILP (Fcoding_system_p (coding_system
)))
4773 /* Invalid coding system. */
4775 if (!NILP (noerror
))
4776 coding_system
= Qraw_text
;
4778 xsignal1 (Qcoding_system_error
, coding_system
);
4781 if (STRING_MULTIBYTE (object
))
4782 object
= code_convert_string (object
, coding_system
, Qnil
, 1, 0, 1);
4784 size
= SCHARS (object
);
4785 validate_subarray (object
, start
, end
, size
, &start_char
, &end_char
);
4787 start_byte
= !start_char
? 0 : string_char_to_byte (object
, start_char
);
4788 end_byte
= (end_char
== size
4790 : string_char_to_byte (object
, end_char
));
4794 struct buffer
*prev
= current_buffer
;
4796 record_unwind_current_buffer ();
4798 CHECK_BUFFER (object
);
4800 bp
= XBUFFER (object
);
4801 set_buffer_internal (bp
);
4807 CHECK_NUMBER_COERCE_MARKER (start
);
4815 CHECK_NUMBER_COERCE_MARKER (end
);
4820 temp
= b
, b
= e
, e
= temp
;
4822 if (!(BEGV
<= b
&& e
<= ZV
))
4823 args_out_of_range (start
, end
);
4825 if (NILP (coding_system
))
4827 /* Decide the coding-system to encode the data with.
4828 See fileio.c:Fwrite-region */
4830 if (!NILP (Vcoding_system_for_write
))
4831 coding_system
= Vcoding_system_for_write
;
4834 bool force_raw_text
= 0;
4836 coding_system
= BVAR (XBUFFER (object
), buffer_file_coding_system
);
4837 if (NILP (coding_system
)
4838 || NILP (Flocal_variable_p (Qbuffer_file_coding_system
, Qnil
)))
4840 coding_system
= Qnil
;
4841 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
4845 if (NILP (coding_system
) && !NILP (Fbuffer_file_name (object
)))
4847 /* Check file-coding-system-alist. */
4848 Lisp_Object val
= CALLN (Ffind_operation_coding_system
,
4849 Qwrite_region
, start
, end
,
4850 Fbuffer_file_name (object
));
4851 if (CONSP (val
) && !NILP (XCDR (val
)))
4852 coding_system
= XCDR (val
);
4855 if (NILP (coding_system
)
4856 && !NILP (BVAR (XBUFFER (object
), buffer_file_coding_system
)))
4858 /* If we still have not decided a coding system, use the
4859 default value of buffer-file-coding-system. */
4860 coding_system
= BVAR (XBUFFER (object
), buffer_file_coding_system
);
4864 && !NILP (Ffboundp (Vselect_safe_coding_system_function
)))
4865 /* Confirm that VAL can surely encode the current region. */
4866 coding_system
= call4 (Vselect_safe_coding_system_function
,
4867 make_number (b
), make_number (e
),
4868 coding_system
, Qnil
);
4871 coding_system
= Qraw_text
;
4874 if (NILP (Fcoding_system_p (coding_system
)))
4876 /* Invalid coding system. */
4878 if (!NILP (noerror
))
4879 coding_system
= Qraw_text
;
4881 xsignal1 (Qcoding_system_error
, coding_system
);
4885 object
= make_buffer_string (b
, e
, 0);
4886 set_buffer_internal (prev
);
4887 /* Discard the unwind protect for recovering the current
4891 if (STRING_MULTIBYTE (object
))
4892 object
= code_convert_string (object
, coding_system
, Qnil
, 1, 0, 0);
4894 end_byte
= SBYTES (object
);
4897 if (EQ (algorithm
, Qmd5
))
4899 digest_size
= MD5_DIGEST_SIZE
;
4900 hash_func
= md5_buffer
;
4902 else if (EQ (algorithm
, Qsha1
))
4904 digest_size
= SHA1_DIGEST_SIZE
;
4905 hash_func
= sha1_buffer
;
4907 else if (EQ (algorithm
, Qsha224
))
4909 digest_size
= SHA224_DIGEST_SIZE
;
4910 hash_func
= sha224_buffer
;
4912 else if (EQ (algorithm
, Qsha256
))
4914 digest_size
= SHA256_DIGEST_SIZE
;
4915 hash_func
= sha256_buffer
;
4917 else if (EQ (algorithm
, Qsha384
))
4919 digest_size
= SHA384_DIGEST_SIZE
;
4920 hash_func
= sha384_buffer
;
4922 else if (EQ (algorithm
, Qsha512
))
4924 digest_size
= SHA512_DIGEST_SIZE
;
4925 hash_func
= sha512_buffer
;
4928 error ("Invalid algorithm arg: %s", SDATA (Fsymbol_name (algorithm
)));
4930 /* allocate 2 x digest_size so that it can be re-used to hold the
4932 digest
= make_uninit_string (digest_size
* 2);
4934 hash_func (SSDATA (object
) + start_byte
,
4935 end_byte
- start_byte
,
4940 unsigned char *p
= SDATA (digest
);
4941 for (i
= digest_size
- 1; i
>= 0; i
--)
4943 static char const hexdigit
[16] = "0123456789abcdef";
4945 p
[2 * i
] = hexdigit
[p_i
>> 4];
4946 p
[2 * i
+ 1] = hexdigit
[p_i
& 0xf];
4951 return make_unibyte_string (SSDATA (digest
), digest_size
);
4954 DEFUN ("md5", Fmd5
, Smd5
, 1, 5, 0,
4955 doc
: /* Return MD5 message digest of OBJECT, a buffer or string.
4957 A message digest is a cryptographic checksum of a document, and the
4958 algorithm to calculate it is defined in RFC 1321.
4960 The two optional arguments START and END are character positions
4961 specifying for which part of OBJECT the message digest should be
4962 computed. If nil or omitted, the digest is computed for the whole
4965 The MD5 message digest is computed from the result of encoding the
4966 text in a coding system, not directly from the internal Emacs form of
4967 the text. The optional fourth argument CODING-SYSTEM specifies which
4968 coding system to encode the text with. It should be the same coding
4969 system that you used or will use when actually writing the text into a
4972 If CODING-SYSTEM is nil or omitted, the default depends on OBJECT. If
4973 OBJECT is a buffer, the default for CODING-SYSTEM is whatever coding
4974 system would be chosen by default for writing this text into a file.
4976 If OBJECT is a string, the most preferred coding system (see the
4977 command `prefer-coding-system') is used.
4979 If NOERROR is non-nil, silently assume the `raw-text' coding if the
4980 guesswork fails. Normally, an error is signaled in such case. */)
4981 (Lisp_Object object
, Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object noerror
)
4983 return secure_hash (Qmd5
, object
, start
, end
, coding_system
, noerror
, Qnil
);
4986 DEFUN ("secure-hash", Fsecure_hash
, Ssecure_hash
, 2, 5, 0,
4987 doc
: /* Return the secure hash of OBJECT, a buffer or string.
4988 ALGORITHM is a symbol specifying the hash to use:
4989 md5, sha1, sha224, sha256, sha384 or sha512.
4991 The two optional arguments START and END are positions specifying for
4992 which part of OBJECT to compute the hash. If nil or omitted, uses the
4995 If BINARY is non-nil, returns a string in binary form. */)
4996 (Lisp_Object algorithm
, Lisp_Object object
, Lisp_Object start
, Lisp_Object end
, Lisp_Object binary
)
4998 return secure_hash (algorithm
, object
, start
, end
, Qnil
, Qnil
, binary
);
5004 DEFSYM (Qmd5
, "md5");
5005 DEFSYM (Qsha1
, "sha1");
5006 DEFSYM (Qsha224
, "sha224");
5007 DEFSYM (Qsha256
, "sha256");
5008 DEFSYM (Qsha384
, "sha384");
5009 DEFSYM (Qsha512
, "sha512");
5011 /* Hash table stuff. */
5012 DEFSYM (Qhash_table_p
, "hash-table-p");
5014 DEFSYM (Qeql
, "eql");
5015 DEFSYM (Qequal
, "equal");
5016 DEFSYM (QCtest
, ":test");
5017 DEFSYM (QCsize
, ":size");
5018 DEFSYM (QCrehash_size
, ":rehash-size");
5019 DEFSYM (QCrehash_threshold
, ":rehash-threshold");
5020 DEFSYM (QCweakness
, ":weakness");
5021 DEFSYM (Qkey
, "key");
5022 DEFSYM (Qvalue
, "value");
5023 DEFSYM (Qhash_table_test
, "hash-table-test");
5024 DEFSYM (Qkey_or_value
, "key-or-value");
5025 DEFSYM (Qkey_and_value
, "key-and-value");
5028 defsubr (&Smake_hash_table
);
5029 defsubr (&Scopy_hash_table
);
5030 defsubr (&Shash_table_count
);
5031 defsubr (&Shash_table_rehash_size
);
5032 defsubr (&Shash_table_rehash_threshold
);
5033 defsubr (&Shash_table_size
);
5034 defsubr (&Shash_table_test
);
5035 defsubr (&Shash_table_weakness
);
5036 defsubr (&Shash_table_p
);
5037 defsubr (&Sclrhash
);
5038 defsubr (&Sgethash
);
5039 defsubr (&Sputhash
);
5040 defsubr (&Sremhash
);
5041 defsubr (&Smaphash
);
5042 defsubr (&Sdefine_hash_table_test
);
5044 DEFSYM (Qstring_lessp
, "string-lessp");
5045 DEFSYM (Qprovide
, "provide");
5046 DEFSYM (Qrequire
, "require");
5047 DEFSYM (Qyes_or_no_p_history
, "yes-or-no-p-history");
5048 DEFSYM (Qcursor_in_echo_area
, "cursor-in-echo-area");
5049 DEFSYM (Qwidget_type
, "widget-type");
5051 staticpro (&string_char_byte_cache_string
);
5052 string_char_byte_cache_string
= Qnil
;
5054 require_nesting_list
= Qnil
;
5055 staticpro (&require_nesting_list
);
5057 Fset (Qyes_or_no_p_history
, Qnil
);
5059 DEFVAR_LISP ("features", Vfeatures
,
5060 doc
: /* A list of symbols which are the features of the executing Emacs.
5061 Used by `featurep' and `require', and altered by `provide'. */);
5062 Vfeatures
= list1 (Qemacs
);
5063 DEFSYM (Qsubfeatures
, "subfeatures");
5064 DEFSYM (Qfuncall
, "funcall");
5066 #ifdef HAVE_LANGINFO_CODESET
5067 DEFSYM (Qcodeset
, "codeset");
5068 DEFSYM (Qdays
, "days");
5069 DEFSYM (Qmonths
, "months");
5070 DEFSYM (Qpaper
, "paper");
5071 #endif /* HAVE_LANGINFO_CODESET */
5073 DEFVAR_BOOL ("use-dialog-box", use_dialog_box
,
5074 doc
: /* Non-nil means mouse commands use dialog boxes to ask questions.
5075 This applies to `y-or-n-p' and `yes-or-no-p' questions asked by commands
5076 invoked by mouse clicks and mouse menu items.
5078 On some platforms, file selection dialogs are also enabled if this is
5082 DEFVAR_BOOL ("use-file-dialog", use_file_dialog
,
5083 doc
: /* Non-nil means mouse commands use a file dialog to ask for files.
5084 This applies to commands from menus and tool bar buttons even when
5085 they are initiated from the keyboard. If `use-dialog-box' is nil,
5086 that disables the use of a file dialog, regardless of the value of
5088 use_file_dialog
= 1;
5090 defsubr (&Sidentity
);
5093 defsubr (&Ssafe_length
);
5094 defsubr (&Sstring_bytes
);
5095 defsubr (&Sstring_equal
);
5096 defsubr (&Scompare_strings
);
5097 defsubr (&Sstring_lessp
);
5098 defsubr (&Sstring_version_lessp
);
5099 defsubr (&Sstring_collate_lessp
);
5100 defsubr (&Sstring_collate_equalp
);
5103 defsubr (&Svconcat
);
5104 defsubr (&Scopy_sequence
);
5105 defsubr (&Sstring_make_multibyte
);
5106 defsubr (&Sstring_make_unibyte
);
5107 defsubr (&Sstring_as_multibyte
);
5108 defsubr (&Sstring_as_unibyte
);
5109 defsubr (&Sstring_to_multibyte
);
5110 defsubr (&Sstring_to_unibyte
);
5111 defsubr (&Scopy_alist
);
5112 defsubr (&Ssubstring
);
5113 defsubr (&Ssubstring_no_properties
);
5126 defsubr (&Snreverse
);
5127 defsubr (&Sreverse
);
5129 defsubr (&Splist_get
);
5131 defsubr (&Splist_put
);
5133 defsubr (&Slax_plist_get
);
5134 defsubr (&Slax_plist_put
);
5137 defsubr (&Sequal_including_properties
);
5138 defsubr (&Sfillarray
);
5139 defsubr (&Sclear_string
);
5143 defsubr (&Smapconcat
);
5144 defsubr (&Syes_or_no_p
);
5145 defsubr (&Sload_average
);
5146 defsubr (&Sfeaturep
);
5147 defsubr (&Srequire
);
5148 defsubr (&Sprovide
);
5149 defsubr (&Splist_member
);
5150 defsubr (&Swidget_put
);
5151 defsubr (&Swidget_get
);
5152 defsubr (&Swidget_apply
);
5153 defsubr (&Sbase64_encode_region
);
5154 defsubr (&Sbase64_decode_region
);
5155 defsubr (&Sbase64_encode_string
);
5156 defsubr (&Sbase64_decode_string
);
5158 defsubr (&Ssecure_hash
);
5159 defsubr (&Slocale_info
);
5161 hashtest_eq
.name
= Qeq
;
5162 hashtest_eq
.user_hash_function
= Qnil
;
5163 hashtest_eq
.user_cmp_function
= Qnil
;
5164 hashtest_eq
.cmpfn
= 0;
5165 hashtest_eq
.hashfn
= hashfn_eq
;
5167 hashtest_eql
.name
= Qeql
;
5168 hashtest_eql
.user_hash_function
= Qnil
;
5169 hashtest_eql
.user_cmp_function
= Qnil
;
5170 hashtest_eql
.cmpfn
= cmpfn_eql
;
5171 hashtest_eql
.hashfn
= hashfn_eql
;
5173 hashtest_equal
.name
= Qequal
;
5174 hashtest_equal
.user_hash_function
= Qnil
;
5175 hashtest_equal
.user_cmp_function
= Qnil
;
5176 hashtest_equal
.cmpfn
= cmpfn_equal
;
5177 hashtest_equal
.hashfn
= hashfn_equal
;