1 /* Random utility Lisp functions.
2 Copyright (C) 1985, 86, 87, 93, 94, 95, 97, 98, 99, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
29 /* Note on some machines this defines `vector' as a typedef,
30 so make sure we don't use that name in this file. */
41 #include "intervals.h"
44 #include "blockinput.h"
45 #if defined (HAVE_MENUS) && defined (HAVE_X_WINDOWS)
50 #define NULL (void *)0
53 /* Nonzero enables use of dialog boxes for questions
54 asked by mouse commands. */
57 extern int minibuffer_auto_raise
;
58 extern Lisp_Object minibuf_window
;
60 Lisp_Object Qstring_lessp
, Qprovide
, Qrequire
;
61 Lisp_Object Qyes_or_no_p_history
;
62 Lisp_Object Qcursor_in_echo_area
;
63 Lisp_Object Qwidget_type
;
65 extern Lisp_Object Qinput_method_function
;
67 static int internal_equal ();
69 extern long get_random ();
70 extern void seed_random ();
76 DEFUN ("identity", Fidentity
, Sidentity
, 1, 1, 0,
77 doc
: /* Return the argument unchanged. */)
84 DEFUN ("random", Frandom
, Srandom
, 0, 1, 0,
85 doc
: /* Return a pseudo-random number.
86 All integers representable in Lisp are equally likely.
87 On most systems, this is 28 bits' worth.
88 With positive integer argument N, return random number in interval [0,N).
89 With argument t, set the random number seed from the current time and pid. */)
94 Lisp_Object lispy_val
;
95 unsigned long denominator
;
98 seed_random (getpid () + time (NULL
));
99 if (NATNUMP (n
) && XFASTINT (n
) != 0)
101 /* Try to take our random number from the higher bits of VAL,
102 not the lower, since (says Gentzel) the low bits of `random'
103 are less random than the higher ones. We do this by using the
104 quotient rather than the remainder. At the high end of the RNG
105 it's possible to get a quotient larger than n; discarding
106 these values eliminates the bias that would otherwise appear
107 when using a large n. */
108 denominator
= ((unsigned long)1 << VALBITS
) / XFASTINT (n
);
110 val
= get_random () / denominator
;
111 while (val
>= XFASTINT (n
));
115 XSETINT (lispy_val
, val
);
119 /* Random data-structure functions */
121 DEFUN ("length", Flength
, Slength
, 1, 1, 0,
122 doc
: /* Return the length of vector, list or string SEQUENCE.
123 A byte-code function object is also allowed.
124 If the string contains multibyte characters, this is not necessarily
125 the number of bytes in the string; it is the number of characters.
126 To get the number of bytes, use `string-bytes'. */)
128 register Lisp_Object sequence
;
130 register Lisp_Object val
;
134 if (STRINGP (sequence
))
135 XSETFASTINT (val
, SCHARS (sequence
));
136 else if (VECTORP (sequence
))
137 XSETFASTINT (val
, XVECTOR (sequence
)->size
);
138 else if (CHAR_TABLE_P (sequence
))
139 XSETFASTINT (val
, MAX_CHAR
);
140 else if (BOOL_VECTOR_P (sequence
))
141 XSETFASTINT (val
, XBOOL_VECTOR (sequence
)->size
);
142 else if (COMPILEDP (sequence
))
143 XSETFASTINT (val
, XVECTOR (sequence
)->size
& PSEUDOVECTOR_SIZE_MASK
);
144 else if (CONSP (sequence
))
147 while (CONSP (sequence
))
149 sequence
= XCDR (sequence
);
152 if (!CONSP (sequence
))
155 sequence
= XCDR (sequence
);
160 if (!NILP (sequence
))
161 wrong_type_argument (Qlistp
, sequence
);
163 val
= make_number (i
);
165 else if (NILP (sequence
))
166 XSETFASTINT (val
, 0);
169 sequence
= wrong_type_argument (Qsequencep
, sequence
);
175 /* This does not check for quits. That is safe
176 since it must terminate. */
178 DEFUN ("safe-length", Fsafe_length
, Ssafe_length
, 1, 1, 0,
179 doc
: /* Return the length of a list, but avoid error or infinite loop.
180 This function never gets an error. If LIST is not really a list,
181 it returns 0. If LIST is circular, it returns a finite value
182 which is at least the number of distinct elements. */)
186 Lisp_Object tail
, halftail
, length
;
189 /* halftail is used to detect circular lists. */
191 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
193 if (EQ (tail
, halftail
) && len
!= 0)
197 halftail
= XCDR (halftail
);
200 XSETINT (length
, len
);
204 DEFUN ("string-bytes", Fstring_bytes
, Sstring_bytes
, 1, 1, 0,
205 doc
: /* Return the number of bytes in STRING.
206 If STRING is a multibyte string, this is greater than the length of STRING. */)
210 CHECK_STRING (string
);
211 return make_number (SBYTES (string
));
214 DEFUN ("string-equal", Fstring_equal
, Sstring_equal
, 2, 2, 0,
215 doc
: /* Return t if two strings have identical contents.
216 Case is significant, but text properties are ignored.
217 Symbols are also allowed; their print names are used instead. */)
219 register Lisp_Object s1
, s2
;
222 s1
= SYMBOL_NAME (s1
);
224 s2
= SYMBOL_NAME (s2
);
228 if (SCHARS (s1
) != SCHARS (s2
)
229 || SBYTES (s1
) != SBYTES (s2
)
230 || bcmp (SDATA (s1
), SDATA (s2
), SBYTES (s1
)))
235 DEFUN ("compare-strings", Fcompare_strings
,
236 Scompare_strings
, 6, 7, 0,
237 doc
: /* Compare the contents of two strings, converting to multibyte if needed.
238 In string STR1, skip the first START1 characters and stop at END1.
239 In string STR2, skip the first START2 characters and stop at END2.
240 END1 and END2 default to the full lengths of the respective strings.
242 Case is significant in this comparison if IGNORE-CASE is nil.
243 Unibyte strings are converted to multibyte for comparison.
245 The value is t if the strings (or specified portions) match.
246 If string STR1 is less, the value is a negative number N;
247 - 1 - N is the number of characters that match at the beginning.
248 If string STR1 is greater, the value is a positive number N;
249 N - 1 is the number of characters that match at the beginning. */)
250 (str1
, start1
, end1
, str2
, start2
, end2
, ignore_case
)
251 Lisp_Object str1
, start1
, end1
, start2
, str2
, end2
, ignore_case
;
253 register int end1_char
, end2_char
;
254 register int i1
, i1_byte
, i2
, i2_byte
;
259 start1
= make_number (0);
261 start2
= make_number (0);
262 CHECK_NATNUM (start1
);
263 CHECK_NATNUM (start2
);
272 i1_byte
= string_char_to_byte (str1
, i1
);
273 i2_byte
= string_char_to_byte (str2
, i2
);
275 end1_char
= SCHARS (str1
);
276 if (! NILP (end1
) && end1_char
> XINT (end1
))
277 end1_char
= XINT (end1
);
279 end2_char
= SCHARS (str2
);
280 if (! NILP (end2
) && end2_char
> XINT (end2
))
281 end2_char
= XINT (end2
);
283 while (i1
< end1_char
&& i2
< end2_char
)
285 /* When we find a mismatch, we must compare the
286 characters, not just the bytes. */
289 if (STRING_MULTIBYTE (str1
))
290 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c1
, str1
, i1
, i1_byte
);
293 c1
= SREF (str1
, i1
++);
294 c1
= unibyte_char_to_multibyte (c1
);
297 if (STRING_MULTIBYTE (str2
))
298 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c2
, str2
, i2
, i2_byte
);
301 c2
= SREF (str2
, i2
++);
302 c2
= unibyte_char_to_multibyte (c2
);
308 if (! NILP (ignore_case
))
312 tem
= Fupcase (make_number (c1
));
314 tem
= Fupcase (make_number (c2
));
321 /* Note that I1 has already been incremented
322 past the character that we are comparing;
323 hence we don't add or subtract 1 here. */
325 return make_number (- i1
+ XINT (start1
));
327 return make_number (i1
- XINT (start1
));
331 return make_number (i1
- XINT (start1
) + 1);
333 return make_number (- i1
+ XINT (start1
) - 1);
338 DEFUN ("string-lessp", Fstring_lessp
, Sstring_lessp
, 2, 2, 0,
339 doc
: /* Return t if first arg string is less than second in lexicographic order.
341 Symbols are also allowed; their print names are used instead. */)
343 register Lisp_Object s1
, s2
;
346 register int i1
, i1_byte
, i2
, i2_byte
;
349 s1
= SYMBOL_NAME (s1
);
351 s2
= SYMBOL_NAME (s2
);
355 i1
= i1_byte
= i2
= i2_byte
= 0;
358 if (end
> SCHARS (s2
))
363 /* When we find a mismatch, we must compare the
364 characters, not just the bytes. */
367 FETCH_STRING_CHAR_ADVANCE (c1
, s1
, i1
, i1_byte
);
368 FETCH_STRING_CHAR_ADVANCE (c2
, s2
, i2
, i2_byte
);
371 return c1
< c2
? Qt
: Qnil
;
373 return i1
< SCHARS (s2
) ? Qt
: Qnil
;
376 static Lisp_Object
concat ();
387 return concat (2, args
, Lisp_String
, 0);
389 return concat (2, &s1
, Lisp_String
, 0);
390 #endif /* NO_ARG_ARRAY */
396 Lisp_Object s1
, s2
, s3
;
403 return concat (3, args
, Lisp_String
, 0);
405 return concat (3, &s1
, Lisp_String
, 0);
406 #endif /* NO_ARG_ARRAY */
409 DEFUN ("append", Fappend
, Sappend
, 0, MANY
, 0,
410 doc
: /* Concatenate all the arguments and make the result a list.
411 The result is a list whose elements are the elements of all the arguments.
412 Each argument may be a list, vector or string.
413 The last argument is not copied, just used as the tail of the new list.
414 usage: (append &rest SEQUENCES) */)
419 return concat (nargs
, args
, Lisp_Cons
, 1);
422 DEFUN ("concat", Fconcat
, Sconcat
, 0, MANY
, 0,
423 doc
: /* Concatenate all the arguments and make the result a string.
424 The result is a string whose elements are the elements of all the arguments.
425 Each argument may be a string or a list or vector of characters (integers).
426 usage: (concat &rest SEQUENCES) */)
431 return concat (nargs
, args
, Lisp_String
, 0);
434 DEFUN ("vconcat", Fvconcat
, Svconcat
, 0, MANY
, 0,
435 doc
: /* Concatenate all the arguments and make the result a vector.
436 The result is a vector whose elements are the elements of all the arguments.
437 Each argument may be a list, vector or string.
438 usage: (vconcat &rest SEQUENCES) */)
443 return concat (nargs
, args
, Lisp_Vectorlike
, 0);
446 /* Return a copy of a sub char table ARG. The elements except for a
447 nested sub char table are not copied. */
449 copy_sub_char_table (arg
)
452 Lisp_Object copy
= make_sub_char_table (XCHAR_TABLE (arg
)->defalt
);
455 /* Copy all the contents. */
456 bcopy (XCHAR_TABLE (arg
)->contents
, XCHAR_TABLE (copy
)->contents
,
457 SUB_CHAR_TABLE_ORDINARY_SLOTS
* sizeof (Lisp_Object
));
458 /* Recursively copy any sub char-tables in the ordinary slots. */
459 for (i
= 32; i
< SUB_CHAR_TABLE_ORDINARY_SLOTS
; i
++)
460 if (SUB_CHAR_TABLE_P (XCHAR_TABLE (arg
)->contents
[i
]))
461 XCHAR_TABLE (copy
)->contents
[i
]
462 = copy_sub_char_table (XCHAR_TABLE (copy
)->contents
[i
]);
468 DEFUN ("copy-sequence", Fcopy_sequence
, Scopy_sequence
, 1, 1, 0,
469 doc
: /* Return a copy of a list, vector, string or char-table.
470 The elements of a list or vector are not copied; they are shared
471 with the original. */)
475 if (NILP (arg
)) return arg
;
477 if (CHAR_TABLE_P (arg
))
482 copy
= Fmake_char_table (XCHAR_TABLE (arg
)->purpose
, Qnil
);
483 /* Copy all the slots, including the extra ones. */
484 bcopy (XVECTOR (arg
)->contents
, XVECTOR (copy
)->contents
,
485 ((XCHAR_TABLE (arg
)->size
& PSEUDOVECTOR_SIZE_MASK
)
486 * sizeof (Lisp_Object
)));
488 /* Recursively copy any sub char tables in the ordinary slots
489 for multibyte characters. */
490 for (i
= CHAR_TABLE_SINGLE_BYTE_SLOTS
;
491 i
< CHAR_TABLE_ORDINARY_SLOTS
; i
++)
492 if (SUB_CHAR_TABLE_P (XCHAR_TABLE (arg
)->contents
[i
]))
493 XCHAR_TABLE (copy
)->contents
[i
]
494 = copy_sub_char_table (XCHAR_TABLE (copy
)->contents
[i
]);
499 if (BOOL_VECTOR_P (arg
))
503 = (XBOOL_VECTOR (arg
)->size
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
;
505 val
= Fmake_bool_vector (Flength (arg
), Qnil
);
506 bcopy (XBOOL_VECTOR (arg
)->data
, XBOOL_VECTOR (val
)->data
,
511 if (!CONSP (arg
) && !VECTORP (arg
) && !STRINGP (arg
))
512 arg
= wrong_type_argument (Qsequencep
, arg
);
513 return concat (1, &arg
, CONSP (arg
) ? Lisp_Cons
: XTYPE (arg
), 0);
516 /* In string STR of length LEN, see if bytes before STR[I] combine
517 with bytes after STR[I] to form a single character. If so, return
518 the number of bytes after STR[I] which combine in this way.
519 Otherwize, return 0. */
522 count_combining (str
, len
, i
)
526 int j
= i
- 1, bytes
;
528 if (i
== 0 || i
== len
|| CHAR_HEAD_P (str
[i
]))
530 while (j
>= 0 && !CHAR_HEAD_P (str
[j
])) j
--;
531 if (j
< 0 || ! BASE_LEADING_CODE_P (str
[j
]))
533 PARSE_MULTIBYTE_SEQ (str
+ j
, len
- j
, bytes
);
534 return (bytes
<= i
- j
? 0 : bytes
- (i
- j
));
537 /* This structure holds information of an argument of `concat' that is
538 a string and has text properties to be copied. */
541 int argnum
; /* refer to ARGS (arguments of `concat') */
542 int from
; /* refer to ARGS[argnum] (argument string) */
543 int to
; /* refer to VAL (the target string) */
547 concat (nargs
, args
, target_type
, last_special
)
550 enum Lisp_Type target_type
;
554 register Lisp_Object tail
;
555 register Lisp_Object
this;
557 int toindex_byte
= 0;
558 register int result_len
;
559 register int result_len_byte
;
561 Lisp_Object last_tail
;
564 /* When we make a multibyte string, we can't copy text properties
565 while concatinating each string because the length of resulting
566 string can't be decided until we finish the whole concatination.
567 So, we record strings that have text properties to be copied
568 here, and copy the text properties after the concatination. */
569 struct textprop_rec
*textprops
= NULL
;
570 /* Number of elments in textprops. */
571 int num_textprops
= 0;
575 /* In append, the last arg isn't treated like the others */
576 if (last_special
&& nargs
> 0)
579 last_tail
= args
[nargs
];
584 /* Canonicalize each argument. */
585 for (argnum
= 0; argnum
< nargs
; argnum
++)
588 if (!(CONSP (this) || NILP (this) || VECTORP (this) || STRINGP (this)
589 || COMPILEDP (this) || BOOL_VECTOR_P (this)))
591 args
[argnum
] = wrong_type_argument (Qsequencep
, this);
595 /* Compute total length in chars of arguments in RESULT_LEN.
596 If desired output is a string, also compute length in bytes
597 in RESULT_LEN_BYTE, and determine in SOME_MULTIBYTE
598 whether the result should be a multibyte string. */
602 for (argnum
= 0; argnum
< nargs
; argnum
++)
606 len
= XFASTINT (Flength (this));
607 if (target_type
== Lisp_String
)
609 /* We must count the number of bytes needed in the string
610 as well as the number of characters. */
616 for (i
= 0; i
< len
; i
++)
618 ch
= XVECTOR (this)->contents
[i
];
620 wrong_type_argument (Qintegerp
, ch
);
621 this_len_byte
= CHAR_BYTES (XINT (ch
));
622 result_len_byte
+= this_len_byte
;
623 if (!SINGLE_BYTE_CHAR_P (XINT (ch
)))
626 else if (BOOL_VECTOR_P (this) && XBOOL_VECTOR (this)->size
> 0)
627 wrong_type_argument (Qintegerp
, Faref (this, make_number (0)));
628 else if (CONSP (this))
629 for (; CONSP (this); this = XCDR (this))
633 wrong_type_argument (Qintegerp
, ch
);
634 this_len_byte
= CHAR_BYTES (XINT (ch
));
635 result_len_byte
+= this_len_byte
;
636 if (!SINGLE_BYTE_CHAR_P (XINT (ch
)))
639 else if (STRINGP (this))
641 if (STRING_MULTIBYTE (this))
644 result_len_byte
+= SBYTES (this);
647 result_len_byte
+= count_size_as_multibyte (SDATA (this),
655 if (! some_multibyte
)
656 result_len_byte
= result_len
;
658 /* Create the output object. */
659 if (target_type
== Lisp_Cons
)
660 val
= Fmake_list (make_number (result_len
), Qnil
);
661 else if (target_type
== Lisp_Vectorlike
)
662 val
= Fmake_vector (make_number (result_len
), Qnil
);
663 else if (some_multibyte
)
664 val
= make_uninit_multibyte_string (result_len
, result_len_byte
);
666 val
= make_uninit_string (result_len
);
668 /* In `append', if all but last arg are nil, return last arg. */
669 if (target_type
== Lisp_Cons
&& EQ (val
, Qnil
))
672 /* Copy the contents of the args into the result. */
674 tail
= val
, toindex
= -1; /* -1 in toindex is flag we are making a list */
676 toindex
= 0, toindex_byte
= 0;
681 = (struct textprop_rec
*) alloca (sizeof (struct textprop_rec
) * nargs
);
683 for (argnum
= 0; argnum
< nargs
; argnum
++)
687 register unsigned int thisindex
= 0;
688 register unsigned int thisindex_byte
= 0;
692 thislen
= Flength (this), thisleni
= XINT (thislen
);
694 /* Between strings of the same kind, copy fast. */
695 if (STRINGP (this) && STRINGP (val
)
696 && STRING_MULTIBYTE (this) == some_multibyte
)
698 int thislen_byte
= SBYTES (this);
701 bcopy (SDATA (this), SDATA (val
) + toindex_byte
,
703 combined
= (some_multibyte
&& toindex_byte
> 0
704 ? count_combining (SDATA (val
),
705 toindex_byte
+ thislen_byte
,
708 if (! NULL_INTERVAL_P (STRING_INTERVALS (this)))
710 textprops
[num_textprops
].argnum
= argnum
;
711 /* We ignore text properties on characters being combined. */
712 textprops
[num_textprops
].from
= combined
;
713 textprops
[num_textprops
++].to
= toindex
;
715 toindex_byte
+= thislen_byte
;
716 toindex
+= thisleni
- combined
;
717 STRING_SET_CHARS (val
, SCHARS (val
) - combined
);
719 /* Copy a single-byte string to a multibyte string. */
720 else if (STRINGP (this) && STRINGP (val
))
722 if (! NULL_INTERVAL_P (STRING_INTERVALS (this)))
724 textprops
[num_textprops
].argnum
= argnum
;
725 textprops
[num_textprops
].from
= 0;
726 textprops
[num_textprops
++].to
= toindex
;
728 toindex_byte
+= copy_text (SDATA (this),
729 SDATA (val
) + toindex_byte
,
730 SCHARS (this), 0, 1);
734 /* Copy element by element. */
737 register Lisp_Object elt
;
739 /* Fetch next element of `this' arg into `elt', or break if
740 `this' is exhausted. */
741 if (NILP (this)) break;
743 elt
= XCAR (this), this = XCDR (this);
744 else if (thisindex
>= thisleni
)
746 else if (STRINGP (this))
749 if (STRING_MULTIBYTE (this))
751 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c
, this,
754 XSETFASTINT (elt
, c
);
758 XSETFASTINT (elt
, SREF (this, thisindex
++));
760 && (XINT (elt
) >= 0240
761 || (XINT (elt
) >= 0200
762 && ! NILP (Vnonascii_translation_table
)))
763 && XINT (elt
) < 0400)
765 c
= unibyte_char_to_multibyte (XINT (elt
));
770 else if (BOOL_VECTOR_P (this))
773 byte
= XBOOL_VECTOR (this)->data
[thisindex
/ BITS_PER_CHAR
];
774 if (byte
& (1 << (thisindex
% BITS_PER_CHAR
)))
781 elt
= XVECTOR (this)->contents
[thisindex
++];
783 /* Store this element into the result. */
790 else if (VECTORP (val
))
791 XVECTOR (val
)->contents
[toindex
++] = elt
;
795 if (SINGLE_BYTE_CHAR_P (XINT (elt
)))
799 += CHAR_STRING (XINT (elt
),
800 SDATA (val
) + toindex_byte
);
802 SSET (val
, toindex_byte
++, XINT (elt
));
805 && count_combining (SDATA (val
),
806 toindex_byte
, toindex_byte
- 1))
807 STRING_SET_CHARS (val
, SCHARS (val
) - 1);
812 /* If we have any multibyte characters,
813 we already decided to make a multibyte string. */
816 /* P exists as a variable
817 to avoid a bug on the Masscomp C compiler. */
818 unsigned char *p
= SDATA (val
) + toindex_byte
;
820 toindex_byte
+= CHAR_STRING (c
, p
);
827 XSETCDR (prev
, last_tail
);
829 if (num_textprops
> 0)
832 int last_to_end
= -1;
834 for (argnum
= 0; argnum
< num_textprops
; argnum
++)
836 this = args
[textprops
[argnum
].argnum
];
837 props
= text_property_list (this,
839 make_number (SCHARS (this)),
841 /* If successive arguments have properites, be sure that the
842 value of `composition' property be the copy. */
843 if (last_to_end
== textprops
[argnum
].to
)
844 make_composition_value_copy (props
);
845 add_text_properties_from_list (val
, props
,
846 make_number (textprops
[argnum
].to
));
847 last_to_end
= textprops
[argnum
].to
+ SCHARS (this);
853 static Lisp_Object string_char_byte_cache_string
;
854 static int string_char_byte_cache_charpos
;
855 static int string_char_byte_cache_bytepos
;
858 clear_string_char_byte_cache ()
860 string_char_byte_cache_string
= Qnil
;
863 /* Return the character index corresponding to CHAR_INDEX in STRING. */
866 string_char_to_byte (string
, char_index
)
871 int best_below
, best_below_byte
;
872 int best_above
, best_above_byte
;
874 if (! STRING_MULTIBYTE (string
))
877 best_below
= best_below_byte
= 0;
878 best_above
= SCHARS (string
);
879 best_above_byte
= SBYTES (string
);
881 if (EQ (string
, string_char_byte_cache_string
))
883 if (string_char_byte_cache_charpos
< char_index
)
885 best_below
= string_char_byte_cache_charpos
;
886 best_below_byte
= string_char_byte_cache_bytepos
;
890 best_above
= string_char_byte_cache_charpos
;
891 best_above_byte
= string_char_byte_cache_bytepos
;
895 if (char_index
- best_below
< best_above
- char_index
)
897 while (best_below
< char_index
)
900 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c
, string
,
901 best_below
, best_below_byte
);
904 i_byte
= best_below_byte
;
908 while (best_above
> char_index
)
910 unsigned char *pend
= SDATA (string
) + best_above_byte
;
911 unsigned char *pbeg
= pend
- best_above_byte
;
912 unsigned char *p
= pend
- 1;
915 while (p
> pbeg
&& !CHAR_HEAD_P (*p
)) p
--;
916 PARSE_MULTIBYTE_SEQ (p
, pend
- p
, bytes
);
917 if (bytes
== pend
- p
)
918 best_above_byte
-= bytes
;
919 else if (bytes
> pend
- p
)
920 best_above_byte
-= (pend
- p
);
926 i_byte
= best_above_byte
;
929 string_char_byte_cache_bytepos
= i_byte
;
930 string_char_byte_cache_charpos
= i
;
931 string_char_byte_cache_string
= string
;
936 /* Return the character index corresponding to BYTE_INDEX in STRING. */
939 string_byte_to_char (string
, byte_index
)
944 int best_below
, best_below_byte
;
945 int best_above
, best_above_byte
;
947 if (! STRING_MULTIBYTE (string
))
950 best_below
= best_below_byte
= 0;
951 best_above
= SCHARS (string
);
952 best_above_byte
= SBYTES (string
);
954 if (EQ (string
, string_char_byte_cache_string
))
956 if (string_char_byte_cache_bytepos
< byte_index
)
958 best_below
= string_char_byte_cache_charpos
;
959 best_below_byte
= string_char_byte_cache_bytepos
;
963 best_above
= string_char_byte_cache_charpos
;
964 best_above_byte
= string_char_byte_cache_bytepos
;
968 if (byte_index
- best_below_byte
< best_above_byte
- byte_index
)
970 while (best_below_byte
< byte_index
)
973 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c
, string
,
974 best_below
, best_below_byte
);
977 i_byte
= best_below_byte
;
981 while (best_above_byte
> byte_index
)
983 unsigned char *pend
= SDATA (string
) + best_above_byte
;
984 unsigned char *pbeg
= pend
- best_above_byte
;
985 unsigned char *p
= pend
- 1;
988 while (p
> pbeg
&& !CHAR_HEAD_P (*p
)) p
--;
989 PARSE_MULTIBYTE_SEQ (p
, pend
- p
, bytes
);
990 if (bytes
== pend
- p
)
991 best_above_byte
-= bytes
;
992 else if (bytes
> pend
- p
)
993 best_above_byte
-= (pend
- p
);
999 i_byte
= best_above_byte
;
1002 string_char_byte_cache_bytepos
= i_byte
;
1003 string_char_byte_cache_charpos
= i
;
1004 string_char_byte_cache_string
= string
;
1009 /* Convert STRING to a multibyte string.
1010 Single-byte characters 0240 through 0377 are converted
1011 by adding nonascii_insert_offset to each. */
1014 string_make_multibyte (string
)
1020 if (STRING_MULTIBYTE (string
))
1023 nbytes
= count_size_as_multibyte (SDATA (string
),
1025 /* If all the chars are ASCII, they won't need any more bytes
1026 once converted. In that case, we can return STRING itself. */
1027 if (nbytes
== SBYTES (string
))
1030 buf
= (unsigned char *) alloca (nbytes
);
1031 copy_text (SDATA (string
), buf
, SBYTES (string
),
1034 return make_multibyte_string (buf
, SCHARS (string
), nbytes
);
1037 /* Convert STRING to a single-byte string. */
1040 string_make_unibyte (string
)
1045 if (! STRING_MULTIBYTE (string
))
1048 buf
= (unsigned char *) alloca (SCHARS (string
));
1050 copy_text (SDATA (string
), buf
, SBYTES (string
),
1053 return make_unibyte_string (buf
, SCHARS (string
));
1056 DEFUN ("string-make-multibyte", Fstring_make_multibyte
, Sstring_make_multibyte
,
1058 doc
: /* Return the multibyte equivalent of STRING.
1059 The function `unibyte-char-to-multibyte' is used to convert
1060 each unibyte character to a multibyte character. */)
1064 CHECK_STRING (string
);
1066 return string_make_multibyte (string
);
1069 DEFUN ("string-make-unibyte", Fstring_make_unibyte
, Sstring_make_unibyte
,
1071 doc
: /* Return the unibyte equivalent of STRING.
1072 Multibyte character codes are converted to unibyte according to
1073 `nonascii-translation-table' or, if that is nil, `nonascii-insert-offset'.
1074 If the lookup in the translation table fails, this function takes just
1075 the low 8 bits of each character. */)
1079 CHECK_STRING (string
);
1081 return string_make_unibyte (string
);
1084 DEFUN ("string-as-unibyte", Fstring_as_unibyte
, Sstring_as_unibyte
,
1086 doc
: /* Return a unibyte string with the same individual bytes as STRING.
1087 If STRING is unibyte, the result is STRING itself.
1088 Otherwise it is a newly created string, with no text properties.
1089 If STRING is multibyte and contains a character of charset
1090 `eight-bit-control' or `eight-bit-graphic', it is converted to the
1091 corresponding single byte. */)
1095 CHECK_STRING (string
);
1097 if (STRING_MULTIBYTE (string
))
1099 int bytes
= SBYTES (string
);
1100 unsigned char *str
= (unsigned char *) xmalloc (bytes
);
1102 bcopy (SDATA (string
), str
, bytes
);
1103 bytes
= str_as_unibyte (str
, bytes
);
1104 string
= make_unibyte_string (str
, bytes
);
1110 DEFUN ("string-as-multibyte", Fstring_as_multibyte
, Sstring_as_multibyte
,
1112 doc
: /* Return a multibyte string with the same individual bytes as STRING.
1113 If STRING is multibyte, the result is STRING itself.
1114 Otherwise it is a newly created string, with no text properties.
1115 If STRING is unibyte and contains an individual 8-bit byte (i.e. not
1116 part of a multibyte form), it is converted to the corresponding
1117 multibyte character of charset `eight-bit-control' or `eight-bit-graphic'. */)
1121 CHECK_STRING (string
);
1123 if (! STRING_MULTIBYTE (string
))
1125 Lisp_Object new_string
;
1128 parse_str_as_multibyte (SDATA (string
),
1131 new_string
= make_uninit_multibyte_string (nchars
, nbytes
);
1132 bcopy (SDATA (string
), SDATA (new_string
),
1134 if (nbytes
!= SBYTES (string
))
1135 str_as_multibyte (SDATA (new_string
), nbytes
,
1136 SBYTES (string
), NULL
);
1137 string
= new_string
;
1138 STRING_SET_INTERVALS (string
, NULL_INTERVAL
);
1143 DEFUN ("copy-alist", Fcopy_alist
, Scopy_alist
, 1, 1, 0,
1144 doc
: /* Return a copy of ALIST.
1145 This is an alist which represents the same mapping from objects to objects,
1146 but does not share the alist structure with ALIST.
1147 The objects mapped (cars and cdrs of elements of the alist)
1148 are shared, however.
1149 Elements of ALIST that are not conses are also shared. */)
1153 register Lisp_Object tem
;
1158 alist
= concat (1, &alist
, Lisp_Cons
, 0);
1159 for (tem
= alist
; CONSP (tem
); tem
= XCDR (tem
))
1161 register Lisp_Object car
;
1165 XSETCAR (tem
, Fcons (XCAR (car
), XCDR (car
)));
1170 DEFUN ("substring", Fsubstring
, Ssubstring
, 2, 3, 0,
1171 doc
: /* Return a substring of STRING, starting at index FROM and ending before TO.
1172 TO may be nil or omitted; then the substring runs to the end of STRING.
1173 If FROM or TO is negative, it counts from the end.
1175 This function allows vectors as well as strings. */)
1178 register Lisp_Object from
, to
;
1183 int from_char
, to_char
;
1184 int from_byte
= 0, to_byte
= 0;
1186 if (! (STRINGP (string
) || VECTORP (string
)))
1187 wrong_type_argument (Qarrayp
, string
);
1189 CHECK_NUMBER (from
);
1191 if (STRINGP (string
))
1193 size
= SCHARS (string
);
1194 size_byte
= SBYTES (string
);
1197 size
= XVECTOR (string
)->size
;
1202 to_byte
= size_byte
;
1208 to_char
= XINT (to
);
1212 if (STRINGP (string
))
1213 to_byte
= string_char_to_byte (string
, to_char
);
1216 from_char
= XINT (from
);
1219 if (STRINGP (string
))
1220 from_byte
= string_char_to_byte (string
, from_char
);
1222 if (!(0 <= from_char
&& from_char
<= to_char
&& to_char
<= size
))
1223 args_out_of_range_3 (string
, make_number (from_char
),
1224 make_number (to_char
));
1226 if (STRINGP (string
))
1228 res
= make_specified_string (SDATA (string
) + from_byte
,
1229 to_char
- from_char
, to_byte
- from_byte
,
1230 STRING_MULTIBYTE (string
));
1231 copy_text_properties (make_number (from_char
), make_number (to_char
),
1232 string
, make_number (0), res
, Qnil
);
1235 res
= Fvector (to_char
- from_char
,
1236 XVECTOR (string
)->contents
+ from_char
);
1242 DEFUN ("substring-no-properties", Fsubstring_no_properties
, Ssubstring_no_properties
, 1, 3, 0,
1243 doc
: /* Return a substring of STRING, without text properties.
1244 It starts at index FROM and ending before TO.
1245 TO may be nil or omitted; then the substring runs to the end of STRING.
1246 If FROM is nil or omitted, the substring starts at the beginning of STRING.
1247 If FROM or TO is negative, it counts from the end.
1249 With one argument, just copy STRING without its properties. */)
1252 register Lisp_Object from
, to
;
1254 int size
, size_byte
;
1255 int from_char
, to_char
;
1256 int from_byte
, to_byte
;
1258 CHECK_STRING (string
);
1260 size
= SCHARS (string
);
1261 size_byte
= SBYTES (string
);
1264 from_char
= from_byte
= 0;
1267 CHECK_NUMBER (from
);
1268 from_char
= XINT (from
);
1272 from_byte
= string_char_to_byte (string
, from_char
);
1278 to_byte
= size_byte
;
1284 to_char
= XINT (to
);
1288 to_byte
= string_char_to_byte (string
, to_char
);
1291 if (!(0 <= from_char
&& from_char
<= to_char
&& to_char
<= size
))
1292 args_out_of_range_3 (string
, make_number (from_char
),
1293 make_number (to_char
));
1295 return make_specified_string (SDATA (string
) + from_byte
,
1296 to_char
- from_char
, to_byte
- from_byte
,
1297 STRING_MULTIBYTE (string
));
1300 /* Extract a substring of STRING, giving start and end positions
1301 both in characters and in bytes. */
1304 substring_both (string
, from
, from_byte
, to
, to_byte
)
1306 int from
, from_byte
, to
, to_byte
;
1312 if (! (STRINGP (string
) || VECTORP (string
)))
1313 wrong_type_argument (Qarrayp
, string
);
1315 if (STRINGP (string
))
1317 size
= SCHARS (string
);
1318 size_byte
= SBYTES (string
);
1321 size
= XVECTOR (string
)->size
;
1323 if (!(0 <= from
&& from
<= to
&& to
<= size
))
1324 args_out_of_range_3 (string
, make_number (from
), make_number (to
));
1326 if (STRINGP (string
))
1328 res
= make_specified_string (SDATA (string
) + from_byte
,
1329 to
- from
, to_byte
- from_byte
,
1330 STRING_MULTIBYTE (string
));
1331 copy_text_properties (make_number (from
), make_number (to
),
1332 string
, make_number (0), res
, Qnil
);
1335 res
= Fvector (to
- from
,
1336 XVECTOR (string
)->contents
+ from
);
1341 DEFUN ("nthcdr", Fnthcdr
, Snthcdr
, 2, 2, 0,
1342 doc
: /* Take cdr N times on LIST, returns the result. */)
1345 register Lisp_Object list
;
1347 register int i
, num
;
1350 for (i
= 0; i
< num
&& !NILP (list
); i
++)
1354 wrong_type_argument (Qlistp
, list
);
1360 DEFUN ("nth", Fnth
, Snth
, 2, 2, 0,
1361 doc
: /* Return the Nth element of LIST.
1362 N counts from zero. If LIST is not that long, nil is returned. */)
1364 Lisp_Object n
, list
;
1366 return Fcar (Fnthcdr (n
, list
));
1369 DEFUN ("elt", Felt
, Selt
, 2, 2, 0,
1370 doc
: /* Return element of SEQUENCE at index N. */)
1372 register Lisp_Object sequence
, n
;
1377 if (CONSP (sequence
) || NILP (sequence
))
1378 return Fcar (Fnthcdr (n
, sequence
));
1379 else if (STRINGP (sequence
) || VECTORP (sequence
)
1380 || BOOL_VECTOR_P (sequence
) || CHAR_TABLE_P (sequence
))
1381 return Faref (sequence
, n
);
1383 sequence
= wrong_type_argument (Qsequencep
, sequence
);
1387 DEFUN ("member", Fmember
, Smember
, 2, 2, 0,
1388 doc
: /* Return non-nil if ELT is an element of LIST. Comparison done with `equal'.
1389 The value is actually the tail of LIST whose car is ELT. */)
1391 register Lisp_Object elt
;
1394 register Lisp_Object tail
;
1395 for (tail
= list
; !NILP (tail
); tail
= XCDR (tail
))
1397 register Lisp_Object tem
;
1399 wrong_type_argument (Qlistp
, list
);
1401 if (! NILP (Fequal (elt
, tem
)))
1408 DEFUN ("memq", Fmemq
, Smemq
, 2, 2, 0,
1409 doc
: /* Return non-nil if ELT is an element of LIST.
1410 Comparison done with EQ. The value is actually the tail of LIST
1411 whose car is ELT. */)
1413 Lisp_Object elt
, 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
))
1432 if (!CONSP (list
) && !NILP (list
))
1433 list
= wrong_type_argument (Qlistp
, list
);
1438 DEFUN ("assq", Fassq
, Sassq
, 2, 2, 0,
1439 doc
: /* Return non-nil if KEY is `eq' to the car of an element of LIST.
1440 The value is actually the element of LIST whose car is KEY.
1441 Elements of LIST that are not conses are ignored. */)
1443 Lisp_Object key
, list
;
1450 || (CONSP (XCAR (list
))
1451 && EQ (XCAR (XCAR (list
)), key
)))
1456 || (CONSP (XCAR (list
))
1457 && EQ (XCAR (XCAR (list
)), key
)))
1462 || (CONSP (XCAR (list
))
1463 && EQ (XCAR (XCAR (list
)), key
)))
1471 result
= XCAR (list
);
1472 else if (NILP (list
))
1475 result
= wrong_type_argument (Qlistp
, list
);
1480 /* Like Fassq but never report an error and do not allow quits.
1481 Use only on lists known never to be circular. */
1484 assq_no_quit (key
, list
)
1485 Lisp_Object key
, list
;
1488 && (!CONSP (XCAR (list
))
1489 || !EQ (XCAR (XCAR (list
)), key
)))
1492 return CONSP (list
) ? XCAR (list
) : Qnil
;
1495 DEFUN ("assoc", Fassoc
, Sassoc
, 2, 2, 0,
1496 doc
: /* Return non-nil if KEY is `equal' to the car of an element of LIST.
1497 The value is actually the element of LIST whose car equals KEY. */)
1499 Lisp_Object key
, list
;
1501 Lisp_Object result
, car
;
1506 || (CONSP (XCAR (list
))
1507 && (car
= XCAR (XCAR (list
)),
1508 EQ (car
, key
) || !NILP (Fequal (car
, key
)))))
1513 || (CONSP (XCAR (list
))
1514 && (car
= XCAR (XCAR (list
)),
1515 EQ (car
, key
) || !NILP (Fequal (car
, key
)))))
1520 || (CONSP (XCAR (list
))
1521 && (car
= XCAR (XCAR (list
)),
1522 EQ (car
, key
) || !NILP (Fequal (car
, key
)))))
1530 result
= XCAR (list
);
1531 else if (NILP (list
))
1534 result
= wrong_type_argument (Qlistp
, list
);
1539 DEFUN ("rassq", Frassq
, Srassq
, 2, 2, 0,
1540 doc
: /* Return non-nil if KEY is `eq' to the cdr of an element of LIST.
1541 The value is actually the element of LIST whose cdr is KEY. */)
1543 register Lisp_Object key
;
1551 || (CONSP (XCAR (list
))
1552 && EQ (XCDR (XCAR (list
)), key
)))
1557 || (CONSP (XCAR (list
))
1558 && EQ (XCDR (XCAR (list
)), key
)))
1563 || (CONSP (XCAR (list
))
1564 && EQ (XCDR (XCAR (list
)), key
)))
1573 else if (CONSP (list
))
1574 result
= XCAR (list
);
1576 result
= wrong_type_argument (Qlistp
, list
);
1581 DEFUN ("rassoc", Frassoc
, Srassoc
, 2, 2, 0,
1582 doc
: /* Return non-nil if KEY is `equal' to the cdr of an element of LIST.
1583 The value is actually the element of LIST whose cdr equals KEY. */)
1585 Lisp_Object key
, list
;
1587 Lisp_Object result
, cdr
;
1592 || (CONSP (XCAR (list
))
1593 && (cdr
= XCDR (XCAR (list
)),
1594 EQ (cdr
, key
) || !NILP (Fequal (cdr
, key
)))))
1599 || (CONSP (XCAR (list
))
1600 && (cdr
= XCDR (XCAR (list
)),
1601 EQ (cdr
, key
) || !NILP (Fequal (cdr
, key
)))))
1606 || (CONSP (XCAR (list
))
1607 && (cdr
= XCDR (XCAR (list
)),
1608 EQ (cdr
, key
) || !NILP (Fequal (cdr
, key
)))))
1616 result
= XCAR (list
);
1617 else if (NILP (list
))
1620 result
= wrong_type_argument (Qlistp
, list
);
1625 DEFUN ("delq", Fdelq
, Sdelq
, 2, 2, 0,
1626 doc
: /* Delete by side effect any occurrences of ELT as a member of LIST.
1627 The modified LIST is returned. Comparison is done with `eq'.
1628 If the first member of LIST is ELT, there is no way to remove it by side effect;
1629 therefore, write `(setq foo (delq element foo))'
1630 to be sure of changing the value of `foo'. */)
1632 register Lisp_Object elt
;
1635 register Lisp_Object tail
, prev
;
1636 register Lisp_Object tem
;
1640 while (!NILP (tail
))
1643 wrong_type_argument (Qlistp
, list
);
1650 Fsetcdr (prev
, XCDR (tail
));
1660 DEFUN ("delete", Fdelete
, Sdelete
, 2, 2, 0,
1661 doc
: /* Delete by side effect any occurrences of ELT as a member of SEQ.
1662 SEQ must be a list, a vector, or a string.
1663 The modified SEQ is returned. Comparison is done with `equal'.
1664 If SEQ is not a list, or the first member of SEQ is ELT, deleting it
1665 is not a side effect; it is simply using a different sequence.
1666 Therefore, write `(setq foo (delete element foo))'
1667 to be sure of changing the value of `foo'. */)
1669 Lisp_Object elt
, seq
;
1675 for (i
= n
= 0; i
< ASIZE (seq
); ++i
)
1676 if (NILP (Fequal (AREF (seq
, i
), elt
)))
1679 if (n
!= ASIZE (seq
))
1681 struct Lisp_Vector
*p
= allocate_vector (n
);
1683 for (i
= n
= 0; i
< ASIZE (seq
); ++i
)
1684 if (NILP (Fequal (AREF (seq
, i
), elt
)))
1685 p
->contents
[n
++] = AREF (seq
, i
);
1687 XSETVECTOR (seq
, p
);
1690 else if (STRINGP (seq
))
1692 EMACS_INT i
, ibyte
, nchars
, nbytes
, cbytes
;
1695 for (i
= nchars
= nbytes
= ibyte
= 0;
1697 ++i
, ibyte
+= cbytes
)
1699 if (STRING_MULTIBYTE (seq
))
1701 c
= STRING_CHAR (SDATA (seq
) + ibyte
,
1702 SBYTES (seq
) - ibyte
);
1703 cbytes
= CHAR_BYTES (c
);
1711 if (!INTEGERP (elt
) || c
!= XINT (elt
))
1718 if (nchars
!= SCHARS (seq
))
1722 tem
= make_uninit_multibyte_string (nchars
, nbytes
);
1723 if (!STRING_MULTIBYTE (seq
))
1724 STRING_SET_UNIBYTE (tem
);
1726 for (i
= nchars
= nbytes
= ibyte
= 0;
1728 ++i
, ibyte
+= cbytes
)
1730 if (STRING_MULTIBYTE (seq
))
1732 c
= STRING_CHAR (SDATA (seq
) + ibyte
,
1733 SBYTES (seq
) - ibyte
);
1734 cbytes
= CHAR_BYTES (c
);
1742 if (!INTEGERP (elt
) || c
!= XINT (elt
))
1744 unsigned char *from
= SDATA (seq
) + ibyte
;
1745 unsigned char *to
= SDATA (tem
) + nbytes
;
1751 for (n
= cbytes
; n
--; )
1761 Lisp_Object tail
, prev
;
1763 for (tail
= seq
, prev
= Qnil
; !NILP (tail
); tail
= XCDR (tail
))
1766 wrong_type_argument (Qlistp
, seq
);
1768 if (!NILP (Fequal (elt
, XCAR (tail
))))
1773 Fsetcdr (prev
, XCDR (tail
));
1784 DEFUN ("nreverse", Fnreverse
, Snreverse
, 1, 1, 0,
1785 doc
: /* Reverse LIST by modifying cdr pointers.
1786 Returns the beginning of the reversed list. */)
1790 register Lisp_Object prev
, tail
, next
;
1792 if (NILP (list
)) return list
;
1795 while (!NILP (tail
))
1799 wrong_type_argument (Qlistp
, list
);
1801 Fsetcdr (tail
, prev
);
1808 DEFUN ("reverse", Freverse
, Sreverse
, 1, 1, 0,
1809 doc
: /* Reverse LIST, copying. Returns the beginning of the reversed list.
1810 See also the function `nreverse', which is used more often. */)
1816 for (new = Qnil
; CONSP (list
); list
= XCDR (list
))
1817 new = Fcons (XCAR (list
), new);
1819 wrong_type_argument (Qconsp
, list
);
1823 Lisp_Object
merge ();
1825 DEFUN ("sort", Fsort
, Ssort
, 2, 2, 0,
1826 doc
: /* Sort LIST, stably, comparing elements using PREDICATE.
1827 Returns the sorted list. LIST is modified by side effects.
1828 PREDICATE is called with two elements of LIST, and should return t
1829 if the first element is "less" than the second. */)
1831 Lisp_Object list
, predicate
;
1833 Lisp_Object front
, back
;
1834 register Lisp_Object len
, tem
;
1835 struct gcpro gcpro1
, gcpro2
;
1836 register int length
;
1839 len
= Flength (list
);
1840 length
= XINT (len
);
1844 XSETINT (len
, (length
/ 2) - 1);
1845 tem
= Fnthcdr (len
, list
);
1847 Fsetcdr (tem
, Qnil
);
1849 GCPRO2 (front
, back
);
1850 front
= Fsort (front
, predicate
);
1851 back
= Fsort (back
, predicate
);
1853 return merge (front
, back
, predicate
);
1857 merge (org_l1
, org_l2
, pred
)
1858 Lisp_Object org_l1
, org_l2
;
1862 register Lisp_Object tail
;
1864 register Lisp_Object l1
, l2
;
1865 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
1872 /* It is sufficient to protect org_l1 and org_l2.
1873 When l1 and l2 are updated, we copy the new values
1874 back into the org_ vars. */
1875 GCPRO4 (org_l1
, org_l2
, pred
, value
);
1895 tem
= call2 (pred
, Fcar (l2
), Fcar (l1
));
1911 Fsetcdr (tail
, tem
);
1917 DEFUN ("plist-get", Fplist_get
, Splist_get
, 2, 2, 0,
1918 doc
: /* Extract a value from a property list.
1919 PLIST is a property list, which is a list of the form
1920 \(PROP1 VALUE1 PROP2 VALUE2...). This function returns the value
1921 corresponding to the given PROP, or nil if PROP is not
1922 one of the properties on the list. */)
1930 CONSP (tail
) && CONSP (XCDR (tail
));
1931 tail
= XCDR (XCDR (tail
)))
1933 if (EQ (prop
, XCAR (tail
)))
1934 return XCAR (XCDR (tail
));
1936 /* This function can be called asynchronously
1937 (setup_coding_system). Don't QUIT in that case. */
1938 if (!interrupt_input_blocked
)
1943 wrong_type_argument (Qlistp
, prop
);
1948 DEFUN ("get", Fget
, Sget
, 2, 2, 0,
1949 doc
: /* Return the value of SYMBOL's PROPNAME property.
1950 This is the last value stored with `(put SYMBOL PROPNAME VALUE)'. */)
1952 Lisp_Object symbol
, propname
;
1954 CHECK_SYMBOL (symbol
);
1955 return Fplist_get (XSYMBOL (symbol
)->plist
, propname
);
1958 DEFUN ("plist-put", Fplist_put
, Splist_put
, 3, 3, 0,
1959 doc
: /* Change value in PLIST of PROP to VAL.
1960 PLIST is a property list, which is a list of the form
1961 \(PROP1 VALUE1 PROP2 VALUE2 ...). PROP is a symbol and VAL is any object.
1962 If PROP is already a property on the list, its value is set to VAL,
1963 otherwise the new PROP VAL pair is added. The new plist is returned;
1964 use `(setq x (plist-put x prop val))' to be sure to use the new value.
1965 The PLIST is modified by side effects. */)
1968 register Lisp_Object prop
;
1971 register Lisp_Object tail
, prev
;
1972 Lisp_Object newcell
;
1974 for (tail
= plist
; CONSP (tail
) && CONSP (XCDR (tail
));
1975 tail
= XCDR (XCDR (tail
)))
1977 if (EQ (prop
, XCAR (tail
)))
1979 Fsetcar (XCDR (tail
), val
);
1986 newcell
= Fcons (prop
, Fcons (val
, Qnil
));
1990 Fsetcdr (XCDR (prev
), newcell
);
1994 DEFUN ("put", Fput
, Sput
, 3, 3, 0,
1995 doc
: /* Store SYMBOL's PROPNAME property with value VALUE.
1996 It can be retrieved with `(get SYMBOL PROPNAME)'. */)
1997 (symbol
, propname
, value
)
1998 Lisp_Object symbol
, propname
, value
;
2000 CHECK_SYMBOL (symbol
);
2001 XSYMBOL (symbol
)->plist
2002 = Fplist_put (XSYMBOL (symbol
)->plist
, propname
, value
);
2006 DEFUN ("lax-plist-get", Flax_plist_get
, Slax_plist_get
, 2, 2, 0,
2007 doc
: /* Extract a value from a property list, comparing with `equal'.
2008 PLIST is a property list, which is a list of the form
2009 \(PROP1 VALUE1 PROP2 VALUE2...). This function returns the value
2010 corresponding to the given PROP, or nil if PROP is not
2011 one of the properties on the list. */)
2019 CONSP (tail
) && CONSP (XCDR (tail
));
2020 tail
= XCDR (XCDR (tail
)))
2022 if (! NILP (Fequal (prop
, XCAR (tail
))))
2023 return XCAR (XCDR (tail
));
2029 wrong_type_argument (Qlistp
, prop
);
2034 DEFUN ("lax-plist-put", Flax_plist_put
, Slax_plist_put
, 3, 3, 0,
2035 doc
: /* Change value in PLIST of PROP to VAL, comparing with `equal'.
2036 PLIST is a property list, which is a list of the form
2037 \(PROP1 VALUE1 PROP2 VALUE2 ...). PROP and VAL are any objects.
2038 If PROP is already a property on the list, its value is set to VAL,
2039 otherwise the new PROP VAL pair is added. The new plist is returned;
2040 use `(setq x (lax-plist-put x prop val))' to be sure to use the new value.
2041 The PLIST is modified by side effects. */)
2044 register Lisp_Object prop
;
2047 register Lisp_Object tail
, prev
;
2048 Lisp_Object newcell
;
2050 for (tail
= plist
; CONSP (tail
) && CONSP (XCDR (tail
));
2051 tail
= XCDR (XCDR (tail
)))
2053 if (! NILP (Fequal (prop
, XCAR (tail
))))
2055 Fsetcar (XCDR (tail
), val
);
2062 newcell
= Fcons (prop
, Fcons (val
, Qnil
));
2066 Fsetcdr (XCDR (prev
), newcell
);
2070 DEFUN ("equal", Fequal
, Sequal
, 2, 2, 0,
2071 doc
: /* Return t if two Lisp objects have similar structure and contents.
2072 They must have the same data type.
2073 Conses are compared by comparing the cars and the cdrs.
2074 Vectors and strings are compared element by element.
2075 Numbers are compared by value, but integers cannot equal floats.
2076 (Use `=' if you want integers and floats to be able to be equal.)
2077 Symbols must match exactly. */)
2079 register Lisp_Object o1
, o2
;
2081 return internal_equal (o1
, o2
, 0) ? Qt
: Qnil
;
2085 internal_equal (o1
, o2
, depth
)
2086 register Lisp_Object o1
, o2
;
2090 error ("Stack overflow in equal");
2096 if (XTYPE (o1
) != XTYPE (o2
))
2102 return (extract_float (o1
) == extract_float (o2
));
2105 if (!internal_equal (XCAR (o1
), XCAR (o2
), depth
+ 1))
2112 if (XMISCTYPE (o1
) != XMISCTYPE (o2
))
2116 if (!internal_equal (OVERLAY_START (o1
), OVERLAY_START (o2
),
2118 || !internal_equal (OVERLAY_END (o1
), OVERLAY_END (o2
),
2121 o1
= XOVERLAY (o1
)->plist
;
2122 o2
= XOVERLAY (o2
)->plist
;
2127 return (XMARKER (o1
)->buffer
== XMARKER (o2
)->buffer
2128 && (XMARKER (o1
)->buffer
== 0
2129 || XMARKER (o1
)->bytepos
== XMARKER (o2
)->bytepos
));
2133 case Lisp_Vectorlike
:
2135 register int i
, size
;
2136 size
= XVECTOR (o1
)->size
;
2137 /* Pseudovectors have the type encoded in the size field, so this test
2138 actually checks that the objects have the same type as well as the
2140 if (XVECTOR (o2
)->size
!= size
)
2142 /* Boolvectors are compared much like strings. */
2143 if (BOOL_VECTOR_P (o1
))
2146 = (XBOOL_VECTOR (o1
)->size
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
;
2148 if (XBOOL_VECTOR (o1
)->size
!= XBOOL_VECTOR (o2
)->size
)
2150 if (bcmp (XBOOL_VECTOR (o1
)->data
, XBOOL_VECTOR (o2
)->data
,
2155 if (WINDOW_CONFIGURATIONP (o1
))
2156 return compare_window_configurations (o1
, o2
, 0);
2158 /* Aside from them, only true vectors, char-tables, and compiled
2159 functions are sensible to compare, so eliminate the others now. */
2160 if (size
& PSEUDOVECTOR_FLAG
)
2162 if (!(size
& (PVEC_COMPILED
| PVEC_CHAR_TABLE
)))
2164 size
&= PSEUDOVECTOR_SIZE_MASK
;
2166 for (i
= 0; i
< size
; i
++)
2169 v1
= XVECTOR (o1
)->contents
[i
];
2170 v2
= XVECTOR (o2
)->contents
[i
];
2171 if (!internal_equal (v1
, v2
, depth
+ 1))
2179 if (SCHARS (o1
) != SCHARS (o2
))
2181 if (SBYTES (o1
) != SBYTES (o2
))
2183 if (bcmp (SDATA (o1
), SDATA (o2
),
2190 case Lisp_Type_Limit
:
2197 extern Lisp_Object
Fmake_char_internal ();
2199 DEFUN ("fillarray", Ffillarray
, Sfillarray
, 2, 2, 0,
2200 doc
: /* Store each element of ARRAY with ITEM.
2201 ARRAY is a vector, string, char-table, or bool-vector. */)
2203 Lisp_Object array
, item
;
2205 register int size
, index
, charval
;
2207 if (VECTORP (array
))
2209 register Lisp_Object
*p
= XVECTOR (array
)->contents
;
2210 size
= XVECTOR (array
)->size
;
2211 for (index
= 0; index
< size
; index
++)
2214 else if (CHAR_TABLE_P (array
))
2216 register Lisp_Object
*p
= XCHAR_TABLE (array
)->contents
;
2217 size
= CHAR_TABLE_ORDINARY_SLOTS
;
2218 for (index
= 0; index
< size
; index
++)
2220 XCHAR_TABLE (array
)->defalt
= Qnil
;
2222 else if (STRINGP (array
))
2224 register unsigned char *p
= SDATA (array
);
2225 CHECK_NUMBER (item
);
2226 charval
= XINT (item
);
2227 size
= SCHARS (array
);
2228 if (STRING_MULTIBYTE (array
))
2230 unsigned char str
[MAX_MULTIBYTE_LENGTH
];
2231 int len
= CHAR_STRING (charval
, str
);
2232 int size_byte
= SBYTES (array
);
2233 unsigned char *p1
= p
, *endp
= p
+ size_byte
;
2236 if (size
!= size_byte
)
2239 int this_len
= MULTIBYTE_FORM_LENGTH (p1
, endp
- p1
);
2240 if (len
!= this_len
)
2241 error ("Attempt to change byte length of a string");
2244 for (i
= 0; i
< size_byte
; i
++)
2245 *p
++ = str
[i
% len
];
2248 for (index
= 0; index
< size
; index
++)
2251 else if (BOOL_VECTOR_P (array
))
2253 register unsigned char *p
= XBOOL_VECTOR (array
)->data
;
2255 = (XBOOL_VECTOR (array
)->size
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
;
2257 charval
= (! NILP (item
) ? -1 : 0);
2258 for (index
= 0; index
< size_in_chars
; index
++)
2263 array
= wrong_type_argument (Qarrayp
, array
);
2269 DEFUN ("char-table-subtype", Fchar_table_subtype
, Schar_table_subtype
,
2271 doc
: /* Return the subtype of char-table CHAR-TABLE. The value is a symbol. */)
2273 Lisp_Object char_table
;
2275 CHECK_CHAR_TABLE (char_table
);
2277 return XCHAR_TABLE (char_table
)->purpose
;
2280 DEFUN ("char-table-parent", Fchar_table_parent
, Schar_table_parent
,
2282 doc
: /* Return the parent char-table of CHAR-TABLE.
2283 The value is either nil or another char-table.
2284 If CHAR-TABLE holds nil for a given character,
2285 then the actual applicable value is inherited from the parent char-table
2286 \(or from its parents, if necessary). */)
2288 Lisp_Object char_table
;
2290 CHECK_CHAR_TABLE (char_table
);
2292 return XCHAR_TABLE (char_table
)->parent
;
2295 DEFUN ("set-char-table-parent", Fset_char_table_parent
, Sset_char_table_parent
,
2297 doc
: /* Set the parent char-table of CHAR-TABLE to PARENT.
2298 PARENT must be either nil or another char-table. */)
2299 (char_table
, parent
)
2300 Lisp_Object char_table
, parent
;
2304 CHECK_CHAR_TABLE (char_table
);
2308 CHECK_CHAR_TABLE (parent
);
2310 for (temp
= parent
; !NILP (temp
); temp
= XCHAR_TABLE (temp
)->parent
)
2311 if (EQ (temp
, char_table
))
2312 error ("Attempt to make a chartable be its own parent");
2315 XCHAR_TABLE (char_table
)->parent
= parent
;
2320 DEFUN ("char-table-extra-slot", Fchar_table_extra_slot
, Schar_table_extra_slot
,
2322 doc
: /* Return the value of CHAR-TABLE's extra-slot number N. */)
2324 Lisp_Object char_table
, n
;
2326 CHECK_CHAR_TABLE (char_table
);
2329 || XINT (n
) >= CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (char_table
)))
2330 args_out_of_range (char_table
, n
);
2332 return XCHAR_TABLE (char_table
)->extras
[XINT (n
)];
2335 DEFUN ("set-char-table-extra-slot", Fset_char_table_extra_slot
,
2336 Sset_char_table_extra_slot
,
2338 doc
: /* Set CHAR-TABLE's extra-slot number N to VALUE. */)
2339 (char_table
, n
, value
)
2340 Lisp_Object char_table
, n
, value
;
2342 CHECK_CHAR_TABLE (char_table
);
2345 || XINT (n
) >= CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (char_table
)))
2346 args_out_of_range (char_table
, n
);
2348 return XCHAR_TABLE (char_table
)->extras
[XINT (n
)] = value
;
2351 DEFUN ("char-table-range", Fchar_table_range
, Schar_table_range
,
2353 doc
: /* Return the value in CHAR-TABLE for a range of characters RANGE.
2354 RANGE should be nil (for the default value)
2355 a vector which identifies a character set or a row of a character set,
2356 a character set name, or a character code. */)
2358 Lisp_Object char_table
, range
;
2360 CHECK_CHAR_TABLE (char_table
);
2362 if (EQ (range
, Qnil
))
2363 return XCHAR_TABLE (char_table
)->defalt
;
2364 else if (INTEGERP (range
))
2365 return Faref (char_table
, range
);
2366 else if (SYMBOLP (range
))
2368 Lisp_Object charset_info
;
2370 charset_info
= Fget (range
, Qcharset
);
2371 CHECK_VECTOR (charset_info
);
2373 return Faref (char_table
,
2374 make_number (XINT (XVECTOR (charset_info
)->contents
[0])
2377 else if (VECTORP (range
))
2379 if (XVECTOR (range
)->size
== 1)
2380 return Faref (char_table
,
2381 make_number (XINT (XVECTOR (range
)->contents
[0]) + 128));
2384 int size
= XVECTOR (range
)->size
;
2385 Lisp_Object
*val
= XVECTOR (range
)->contents
;
2386 Lisp_Object ch
= Fmake_char_internal (size
<= 0 ? Qnil
: val
[0],
2387 size
<= 1 ? Qnil
: val
[1],
2388 size
<= 2 ? Qnil
: val
[2]);
2389 return Faref (char_table
, ch
);
2393 error ("Invalid RANGE argument to `char-table-range'");
2397 DEFUN ("set-char-table-range", Fset_char_table_range
, Sset_char_table_range
,
2399 doc
: /* Set the value in CHAR-TABLE for a range of characters RANGE to VALUE.
2400 RANGE should be t (for all characters), nil (for the default value)
2401 a vector which identifies a character set or a row of a character set,
2402 a coding system, or a character code. */)
2403 (char_table
, range
, value
)
2404 Lisp_Object char_table
, range
, value
;
2408 CHECK_CHAR_TABLE (char_table
);
2411 for (i
= 0; i
< CHAR_TABLE_ORDINARY_SLOTS
; i
++)
2412 XCHAR_TABLE (char_table
)->contents
[i
] = value
;
2413 else if (EQ (range
, Qnil
))
2414 XCHAR_TABLE (char_table
)->defalt
= value
;
2415 else if (SYMBOLP (range
))
2417 Lisp_Object charset_info
;
2419 charset_info
= Fget (range
, Qcharset
);
2420 CHECK_VECTOR (charset_info
);
2422 return Faset (char_table
,
2423 make_number (XINT (XVECTOR (charset_info
)->contents
[0])
2427 else if (INTEGERP (range
))
2428 Faset (char_table
, range
, value
);
2429 else if (VECTORP (range
))
2431 if (XVECTOR (range
)->size
== 1)
2432 return Faset (char_table
,
2433 make_number (XINT (XVECTOR (range
)->contents
[0]) + 128),
2437 int size
= XVECTOR (range
)->size
;
2438 Lisp_Object
*val
= XVECTOR (range
)->contents
;
2439 Lisp_Object ch
= Fmake_char_internal (size
<= 0 ? Qnil
: val
[0],
2440 size
<= 1 ? Qnil
: val
[1],
2441 size
<= 2 ? Qnil
: val
[2]);
2442 return Faset (char_table
, ch
, value
);
2446 error ("Invalid RANGE argument to `set-char-table-range'");
2451 DEFUN ("set-char-table-default", Fset_char_table_default
,
2452 Sset_char_table_default
, 3, 3, 0,
2453 doc
: /* Set the default value in CHAR-TABLE for a generic character CHAR to VALUE.
2454 The generic character specifies the group of characters.
2455 See also the documentation of make-char. */)
2456 (char_table
, ch
, value
)
2457 Lisp_Object char_table
, ch
, value
;
2459 int c
, charset
, code1
, code2
;
2462 CHECK_CHAR_TABLE (char_table
);
2466 SPLIT_CHAR (c
, charset
, code1
, code2
);
2468 /* Since we may want to set the default value for a character set
2469 not yet defined, we check only if the character set is in the
2470 valid range or not, instead of it is already defined or not. */
2471 if (! CHARSET_VALID_P (charset
))
2472 invalid_character (c
);
2474 if (charset
== CHARSET_ASCII
)
2475 return (XCHAR_TABLE (char_table
)->defalt
= value
);
2477 /* Even if C is not a generic char, we had better behave as if a
2478 generic char is specified. */
2479 if (!CHARSET_DEFINED_P (charset
) || CHARSET_DIMENSION (charset
) == 1)
2481 temp
= XCHAR_TABLE (char_table
)->contents
[charset
+ 128];
2484 if (SUB_CHAR_TABLE_P (temp
))
2485 XCHAR_TABLE (temp
)->defalt
= value
;
2487 XCHAR_TABLE (char_table
)->contents
[charset
+ 128] = value
;
2490 if (SUB_CHAR_TABLE_P (temp
))
2493 char_table
= (XCHAR_TABLE (char_table
)->contents
[charset
+ 128]
2494 = make_sub_char_table (temp
));
2495 temp
= XCHAR_TABLE (char_table
)->contents
[code1
];
2496 if (SUB_CHAR_TABLE_P (temp
))
2497 XCHAR_TABLE (temp
)->defalt
= value
;
2499 XCHAR_TABLE (char_table
)->contents
[code1
] = value
;
2503 /* Look up the element in TABLE at index CH,
2504 and return it as an integer.
2505 If the element is nil, return CH itself.
2506 (Actually we do that for any non-integer.) */
2509 char_table_translate (table
, ch
)
2514 value
= Faref (table
, make_number (ch
));
2515 if (! INTEGERP (value
))
2517 return XINT (value
);
2521 optimize_sub_char_table (table
, chars
)
2529 from
= 33, to
= 127;
2531 from
= 32, to
= 128;
2533 if (!SUB_CHAR_TABLE_P (*table
))
2535 elt
= XCHAR_TABLE (*table
)->contents
[from
++];
2536 for (; from
< to
; from
++)
2537 if (NILP (Fequal (elt
, XCHAR_TABLE (*table
)->contents
[from
])))
2542 DEFUN ("optimize-char-table", Foptimize_char_table
, Soptimize_char_table
,
2543 1, 1, 0, doc
: /* Optimize char table TABLE. */)
2551 CHECK_CHAR_TABLE (table
);
2553 for (i
= CHAR_TABLE_SINGLE_BYTE_SLOTS
; i
< CHAR_TABLE_ORDINARY_SLOTS
; i
++)
2555 elt
= XCHAR_TABLE (table
)->contents
[i
];
2556 if (!SUB_CHAR_TABLE_P (elt
))
2558 dim
= CHARSET_DIMENSION (i
- 128);
2560 for (j
= 32; j
< SUB_CHAR_TABLE_ORDINARY_SLOTS
; j
++)
2561 optimize_sub_char_table (XCHAR_TABLE (elt
)->contents
+ j
, dim
);
2562 optimize_sub_char_table (XCHAR_TABLE (table
)->contents
+ i
, dim
);
2568 /* Map C_FUNCTION or FUNCTION over SUBTABLE, calling it for each
2569 character or group of characters that share a value.
2570 DEPTH is the current depth in the originally specified
2571 chartable, and INDICES contains the vector indices
2572 for the levels our callers have descended.
2574 ARG is passed to C_FUNCTION when that is called. */
2577 map_char_table (c_function
, function
, subtable
, arg
, depth
, indices
)
2578 void (*c_function
) P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
));
2579 Lisp_Object function
, subtable
, arg
, *indices
;
2586 /* At first, handle ASCII and 8-bit European characters. */
2587 for (i
= 0; i
< CHAR_TABLE_SINGLE_BYTE_SLOTS
; i
++)
2589 Lisp_Object elt
= XCHAR_TABLE (subtable
)->contents
[i
];
2591 (*c_function
) (arg
, make_number (i
), elt
);
2593 call2 (function
, make_number (i
), elt
);
2595 #if 0 /* If the char table has entries for higher characters,
2596 we should report them. */
2597 if (NILP (current_buffer
->enable_multibyte_characters
))
2600 to
= CHAR_TABLE_ORDINARY_SLOTS
;
2604 int charset
= XFASTINT (indices
[0]) - 128;
2607 to
= SUB_CHAR_TABLE_ORDINARY_SLOTS
;
2608 if (CHARSET_CHARS (charset
) == 94)
2617 elt
= XCHAR_TABLE (subtable
)->contents
[i
];
2618 XSETFASTINT (indices
[depth
], i
);
2619 charset
= XFASTINT (indices
[0]) - 128;
2621 && (!CHARSET_DEFINED_P (charset
)
2622 || charset
== CHARSET_8_BIT_CONTROL
2623 || charset
== CHARSET_8_BIT_GRAPHIC
))
2626 if (SUB_CHAR_TABLE_P (elt
))
2629 error ("Too deep char table");
2630 map_char_table (c_function
, function
, elt
, arg
, depth
+ 1, indices
);
2637 elt
= XCHAR_TABLE (subtable
)->defalt
;
2638 c1
= depth
>= 1 ? XFASTINT (indices
[1]) : 0;
2639 c2
= depth
>= 2 ? XFASTINT (indices
[2]) : 0;
2640 c
= MAKE_CHAR (charset
, c1
, c2
);
2642 (*c_function
) (arg
, make_number (c
), elt
);
2644 call2 (function
, make_number (c
), elt
);
2649 DEFUN ("map-char-table", Fmap_char_table
, Smap_char_table
,
2651 doc
: /* Call FUNCTION for each (normal and generic) characters in CHAR-TABLE.
2652 FUNCTION is called with two arguments--a key and a value.
2653 The key is always a possible IDX argument to `aref'. */)
2654 (function
, char_table
)
2655 Lisp_Object function
, char_table
;
2657 /* The depth of char table is at most 3. */
2658 Lisp_Object indices
[3];
2660 CHECK_CHAR_TABLE (char_table
);
2662 map_char_table ((void *) call2
, Qnil
, char_table
, function
, 0, indices
);
2666 /* Return a value for character C in char-table TABLE. Store the
2667 actual index for that value in *IDX. Ignore the default value of
2671 char_table_ref_and_index (table
, c
, idx
)
2675 int charset
, c1
, c2
;
2678 if (SINGLE_BYTE_CHAR_P (c
))
2681 return XCHAR_TABLE (table
)->contents
[c
];
2683 SPLIT_CHAR (c
, charset
, c1
, c2
);
2684 elt
= XCHAR_TABLE (table
)->contents
[charset
+ 128];
2685 *idx
= MAKE_CHAR (charset
, 0, 0);
2686 if (!SUB_CHAR_TABLE_P (elt
))
2688 if (c1
< 32 || NILP (XCHAR_TABLE (elt
)->contents
[c1
]))
2689 return XCHAR_TABLE (elt
)->defalt
;
2690 elt
= XCHAR_TABLE (elt
)->contents
[c1
];
2691 *idx
= MAKE_CHAR (charset
, c1
, 0);
2692 if (!SUB_CHAR_TABLE_P (elt
))
2694 if (c2
< 32 || NILP (XCHAR_TABLE (elt
)->contents
[c2
]))
2695 return XCHAR_TABLE (elt
)->defalt
;
2697 return XCHAR_TABLE (elt
)->contents
[c2
];
2707 Lisp_Object args
[2];
2710 return Fnconc (2, args
);
2712 return Fnconc (2, &s1
);
2713 #endif /* NO_ARG_ARRAY */
2716 DEFUN ("nconc", Fnconc
, Snconc
, 0, MANY
, 0,
2717 doc
: /* Concatenate any number of lists by altering them.
2718 Only the last argument is not altered, and need not be a list.
2719 usage: (nconc &rest LISTS) */)
2724 register int argnum
;
2725 register Lisp_Object tail
, tem
, val
;
2729 for (argnum
= 0; argnum
< nargs
; argnum
++)
2732 if (NILP (tem
)) continue;
2737 if (argnum
+ 1 == nargs
) break;
2740 tem
= wrong_type_argument (Qlistp
, tem
);
2749 tem
= args
[argnum
+ 1];
2750 Fsetcdr (tail
, tem
);
2752 args
[argnum
+ 1] = tail
;
2758 /* This is the guts of all mapping functions.
2759 Apply FN to each element of SEQ, one by one,
2760 storing the results into elements of VALS, a C vector of Lisp_Objects.
2761 LENI is the length of VALS, which should also be the length of SEQ. */
2764 mapcar1 (leni
, vals
, fn
, seq
)
2767 Lisp_Object fn
, seq
;
2769 register Lisp_Object tail
;
2772 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2776 /* Don't let vals contain any garbage when GC happens. */
2777 for (i
= 0; i
< leni
; i
++)
2780 GCPRO3 (dummy
, fn
, seq
);
2782 gcpro1
.nvars
= leni
;
2786 /* We need not explicitly protect `tail' because it is used only on lists, and
2787 1) lists are not relocated and 2) the list is marked via `seq' so will not be freed */
2791 for (i
= 0; i
< leni
; i
++)
2793 dummy
= XVECTOR (seq
)->contents
[i
];
2794 dummy
= call1 (fn
, dummy
);
2799 else if (BOOL_VECTOR_P (seq
))
2801 for (i
= 0; i
< leni
; i
++)
2804 byte
= XBOOL_VECTOR (seq
)->data
[i
/ BITS_PER_CHAR
];
2805 if (byte
& (1 << (i
% BITS_PER_CHAR
)))
2810 dummy
= call1 (fn
, dummy
);
2815 else if (STRINGP (seq
))
2819 for (i
= 0, i_byte
= 0; i
< leni
;)
2824 FETCH_STRING_CHAR_ADVANCE (c
, seq
, i
, i_byte
);
2825 XSETFASTINT (dummy
, c
);
2826 dummy
= call1 (fn
, dummy
);
2828 vals
[i_before
] = dummy
;
2831 else /* Must be a list, since Flength did not get an error */
2834 for (i
= 0; i
< leni
; i
++)
2836 dummy
= call1 (fn
, Fcar (tail
));
2846 DEFUN ("mapconcat", Fmapconcat
, Smapconcat
, 3, 3, 0,
2847 doc
: /* Apply FUNCTION to each element of SEQUENCE, and concat the results as strings.
2848 In between each pair of results, stick in SEPARATOR. Thus, " " as
2849 SEPARATOR results in spaces between the values returned by FUNCTION.
2850 SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
2851 (function
, sequence
, separator
)
2852 Lisp_Object function
, sequence
, separator
;
2857 register Lisp_Object
*args
;
2859 struct gcpro gcpro1
;
2861 len
= Flength (sequence
);
2863 nargs
= leni
+ leni
- 1;
2864 if (nargs
< 0) return build_string ("");
2866 args
= (Lisp_Object
*) alloca (nargs
* sizeof (Lisp_Object
));
2869 mapcar1 (leni
, args
, function
, sequence
);
2872 for (i
= leni
- 1; i
>= 0; i
--)
2873 args
[i
+ i
] = args
[i
];
2875 for (i
= 1; i
< nargs
; i
+= 2)
2876 args
[i
] = separator
;
2878 return Fconcat (nargs
, args
);
2881 DEFUN ("mapcar", Fmapcar
, Smapcar
, 2, 2, 0,
2882 doc
: /* Apply FUNCTION to each element of SEQUENCE, and make a list of the results.
2883 The result is a list just as long as SEQUENCE.
2884 SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
2885 (function
, sequence
)
2886 Lisp_Object function
, sequence
;
2888 register Lisp_Object len
;
2890 register Lisp_Object
*args
;
2892 len
= Flength (sequence
);
2893 leni
= XFASTINT (len
);
2894 args
= (Lisp_Object
*) alloca (leni
* sizeof (Lisp_Object
));
2896 mapcar1 (leni
, args
, function
, sequence
);
2898 return Flist (leni
, args
);
2901 DEFUN ("mapc", Fmapc
, Smapc
, 2, 2, 0,
2902 doc
: /* Apply FUNCTION to each element of SEQUENCE for side effects only.
2903 Unlike `mapcar', don't accumulate the results. Return SEQUENCE.
2904 SEQUENCE may be a list, a vector, a bool-vector, or a string. */)
2905 (function
, sequence
)
2906 Lisp_Object function
, sequence
;
2910 leni
= XFASTINT (Flength (sequence
));
2911 mapcar1 (leni
, 0, function
, sequence
);
2916 /* Anything that calls this function must protect from GC! */
2918 DEFUN ("y-or-n-p", Fy_or_n_p
, Sy_or_n_p
, 1, 1, 0,
2919 doc
: /* Ask user a "y or n" question. Return t if answer is "y".
2920 Takes one argument, which is the string to display to ask the question.
2921 It should end in a space; `y-or-n-p' adds `(y or n) ' to it.
2922 No confirmation of the answer is requested; a single character is enough.
2923 Also accepts Space to mean yes, or Delete to mean no. \(Actually, it uses
2924 the bindings in `query-replace-map'; see the documentation of that variable
2925 for more information. In this case, the useful bindings are `act', `skip',
2926 `recenter', and `quit'.\)
2928 Under a windowing system a dialog box will be used if `last-nonmenu-event'
2929 is nil and `use-dialog-box' is non-nil. */)
2933 register Lisp_Object obj
, key
, def
, map
;
2934 register int answer
;
2935 Lisp_Object xprompt
;
2936 Lisp_Object args
[2];
2937 struct gcpro gcpro1
, gcpro2
;
2938 int count
= SPECPDL_INDEX ();
2940 specbind (Qcursor_in_echo_area
, Qt
);
2942 map
= Fsymbol_value (intern ("query-replace-map"));
2944 CHECK_STRING (prompt
);
2946 GCPRO2 (prompt
, xprompt
);
2948 #ifdef HAVE_X_WINDOWS
2949 if (display_hourglass_p
)
2950 cancel_hourglass ();
2957 if ((NILP (last_nonmenu_event
) || CONSP (last_nonmenu_event
))
2961 Lisp_Object pane
, menu
;
2962 redisplay_preserve_echo_area (3);
2963 pane
= Fcons (Fcons (build_string ("Yes"), Qt
),
2964 Fcons (Fcons (build_string ("No"), Qnil
),
2966 menu
= Fcons (prompt
, pane
);
2967 obj
= Fx_popup_dialog (Qt
, menu
);
2968 answer
= !NILP (obj
);
2971 #endif /* HAVE_MENUS */
2972 cursor_in_echo_area
= 1;
2973 choose_minibuf_frame ();
2976 Lisp_Object pargs
[3];
2978 /* Colorize prompt according to `minibuffer-prompt' face. */
2979 pargs
[0] = build_string ("%s(y or n) ");
2980 pargs
[1] = intern ("face");
2981 pargs
[2] = intern ("minibuffer-prompt");
2982 args
[0] = Fpropertize (3, pargs
);
2987 if (minibuffer_auto_raise
)
2989 Lisp_Object mini_frame
;
2991 mini_frame
= WINDOW_FRAME (XWINDOW (minibuf_window
));
2993 Fraise_frame (mini_frame
);
2996 obj
= read_filtered_event (1, 0, 0, 0);
2997 cursor_in_echo_area
= 0;
2998 /* If we need to quit, quit with cursor_in_echo_area = 0. */
3001 key
= Fmake_vector (make_number (1), obj
);
3002 def
= Flookup_key (map
, key
, Qt
);
3004 if (EQ (def
, intern ("skip")))
3009 else if (EQ (def
, intern ("act")))
3014 else if (EQ (def
, intern ("recenter")))
3020 else if (EQ (def
, intern ("quit")))
3022 /* We want to exit this command for exit-prefix,
3023 and this is the only way to do it. */
3024 else if (EQ (def
, intern ("exit-prefix")))
3029 /* If we don't clear this, then the next call to read_char will
3030 return quit_char again, and we'll enter an infinite loop. */
3035 if (EQ (xprompt
, prompt
))
3037 args
[0] = build_string ("Please answer y or n. ");
3039 xprompt
= Fconcat (2, args
);
3044 if (! noninteractive
)
3046 cursor_in_echo_area
= -1;
3047 message_with_string (answer
? "%s(y or n) y" : "%s(y or n) n",
3051 unbind_to (count
, Qnil
);
3052 return answer
? Qt
: Qnil
;
3055 /* This is how C code calls `yes-or-no-p' and allows the user
3058 Anything that calls this function must protect from GC! */
3061 do_yes_or_no_p (prompt
)
3064 return call1 (intern ("yes-or-no-p"), prompt
);
3067 /* Anything that calls this function must protect from GC! */
3069 DEFUN ("yes-or-no-p", Fyes_or_no_p
, Syes_or_no_p
, 1, 1, 0,
3070 doc
: /* Ask user a yes-or-no question. Return t if answer is yes.
3071 Takes one argument, which is the string to display to ask the question.
3072 It should end in a space; `yes-or-no-p' adds `(yes or no) ' to it.
3073 The user must confirm the answer with RET,
3074 and can edit it until it has been confirmed.
3076 Under a windowing system a dialog box will be used if `last-nonmenu-event'
3077 is nil, and `use-dialog-box' is non-nil. */)
3081 register Lisp_Object ans
;
3082 Lisp_Object args
[2];
3083 struct gcpro gcpro1
;
3085 CHECK_STRING (prompt
);
3088 if ((NILP (last_nonmenu_event
) || CONSP (last_nonmenu_event
))
3092 Lisp_Object pane
, menu
, obj
;
3093 redisplay_preserve_echo_area (4);
3094 pane
= Fcons (Fcons (build_string ("Yes"), Qt
),
3095 Fcons (Fcons (build_string ("No"), Qnil
),
3098 menu
= Fcons (prompt
, pane
);
3099 obj
= Fx_popup_dialog (Qt
, menu
);
3103 #endif /* HAVE_MENUS */
3106 args
[1] = build_string ("(yes or no) ");
3107 prompt
= Fconcat (2, args
);
3113 ans
= Fdowncase (Fread_from_minibuffer (prompt
, Qnil
, Qnil
, Qnil
,
3114 Qyes_or_no_p_history
, Qnil
,
3116 if (SCHARS (ans
) == 3 && !strcmp (SDATA (ans
), "yes"))
3121 if (SCHARS (ans
) == 2 && !strcmp (SDATA (ans
), "no"))
3129 message ("Please answer yes or no.");
3130 Fsleep_for (make_number (2), Qnil
);
3134 DEFUN ("load-average", Fload_average
, Sload_average
, 0, 1, 0,
3135 doc
: /* Return list of 1 minute, 5 minute and 15 minute load averages.
3137 Each of the three load averages is multiplied by 100, then converted
3140 When USE-FLOATS is non-nil, floats will be used instead of integers.
3141 These floats are not multiplied by 100.
3143 If the 5-minute or 15-minute load averages are not available, return a
3144 shortened list, containing only those averages which are available. */)
3146 Lisp_Object use_floats
;
3149 int loads
= getloadavg (load_ave
, 3);
3150 Lisp_Object ret
= Qnil
;
3153 error ("load-average not implemented for this operating system");
3157 Lisp_Object load
= (NILP (use_floats
) ?
3158 make_number ((int) (100.0 * load_ave
[loads
]))
3159 : make_float (load_ave
[loads
]));
3160 ret
= Fcons (load
, ret
);
3166 Lisp_Object Vfeatures
, Qsubfeatures
;
3167 extern Lisp_Object Vafter_load_alist
;
3169 DEFUN ("featurep", Ffeaturep
, Sfeaturep
, 1, 2, 0,
3170 doc
: /* Returns t if FEATURE is present in this Emacs.
3172 Use this to conditionalize execution of lisp code based on the
3173 presence or absence of emacs or environment extensions.
3174 Use `provide' to declare that a feature is available. This function
3175 looks at the value of the variable `features'. The optional argument
3176 SUBFEATURE can be used to check a specific subfeature of FEATURE. */)
3177 (feature
, subfeature
)
3178 Lisp_Object feature
, subfeature
;
3180 register Lisp_Object tem
;
3181 CHECK_SYMBOL (feature
);
3182 tem
= Fmemq (feature
, Vfeatures
);
3183 if (!NILP (tem
) && !NILP (subfeature
))
3184 tem
= Fmember (subfeature
, Fget (feature
, Qsubfeatures
));
3185 return (NILP (tem
)) ? Qnil
: Qt
;
3188 DEFUN ("provide", Fprovide
, Sprovide
, 1, 2, 0,
3189 doc
: /* Announce that FEATURE is a feature of the current Emacs.
3190 The optional argument SUBFEATURES should be a list of symbols listing
3191 particular subfeatures supported in this version of FEATURE. */)
3192 (feature
, subfeatures
)
3193 Lisp_Object feature
, subfeatures
;
3195 register Lisp_Object tem
;
3196 CHECK_SYMBOL (feature
);
3197 CHECK_LIST (subfeatures
);
3198 if (!NILP (Vautoload_queue
))
3199 Vautoload_queue
= Fcons (Fcons (Vfeatures
, Qnil
), Vautoload_queue
);
3200 tem
= Fmemq (feature
, Vfeatures
);
3202 Vfeatures
= Fcons (feature
, Vfeatures
);
3203 if (!NILP (subfeatures
))
3204 Fput (feature
, Qsubfeatures
, subfeatures
);
3205 LOADHIST_ATTACH (Fcons (Qprovide
, feature
));
3207 /* Run any load-hooks for this file. */
3208 tem
= Fassq (feature
, Vafter_load_alist
);
3210 Fprogn (XCDR (tem
));
3215 /* `require' and its subroutines. */
3217 /* List of features currently being require'd, innermost first. */
3219 Lisp_Object require_nesting_list
;
3222 require_unwind (old_value
)
3223 Lisp_Object old_value
;
3225 return require_nesting_list
= old_value
;
3228 DEFUN ("require", Frequire
, Srequire
, 1, 3, 0,
3229 doc
: /* If feature FEATURE is not loaded, load it from FILENAME.
3230 If FEATURE is not a member of the list `features', then the feature
3231 is not loaded; so load the file FILENAME.
3232 If FILENAME is omitted, the printname of FEATURE is used as the file name,
3233 and `load' will try to load this name appended with the suffix `.elc',
3234 `.el' or the unmodified name, in that order.
3235 If the optional third argument NOERROR is non-nil,
3236 then return nil if the file is not found instead of signaling an error.
3237 Normally the return value is FEATURE.
3238 The normal messages at start and end of loading FILENAME are suppressed. */)
3239 (feature
, filename
, noerror
)
3240 Lisp_Object feature
, filename
, noerror
;
3242 register Lisp_Object tem
;
3243 struct gcpro gcpro1
, gcpro2
;
3245 CHECK_SYMBOL (feature
);
3247 tem
= Fmemq (feature
, Vfeatures
);
3251 int count
= SPECPDL_INDEX ();
3254 LOADHIST_ATTACH (Fcons (Qrequire
, feature
));
3256 /* This is to make sure that loadup.el gives a clear picture
3257 of what files are preloaded and when. */
3258 if (! NILP (Vpurify_flag
))
3259 error ("(require %s) while preparing to dump",
3260 SDATA (SYMBOL_NAME (feature
)));
3262 /* A certain amount of recursive `require' is legitimate,
3263 but if we require the same feature recursively 3 times,
3265 tem
= require_nesting_list
;
3266 while (! NILP (tem
))
3268 if (! NILP (Fequal (feature
, XCAR (tem
))))
3273 error ("Recursive `require' for feature `%s'",
3274 SDATA (SYMBOL_NAME (feature
)));
3276 /* Update the list for any nested `require's that occur. */
3277 record_unwind_protect (require_unwind
, require_nesting_list
);
3278 require_nesting_list
= Fcons (feature
, require_nesting_list
);
3280 /* Value saved here is to be restored into Vautoload_queue */
3281 record_unwind_protect (un_autoload
, Vautoload_queue
);
3282 Vautoload_queue
= Qt
;
3284 /* Load the file. */
3285 GCPRO2 (feature
, filename
);
3286 tem
= Fload (NILP (filename
) ? Fsymbol_name (feature
) : filename
,
3287 noerror
, Qt
, Qnil
, (NILP (filename
) ? Qt
: Qnil
));
3290 /* If load failed entirely, return nil. */
3292 return unbind_to (count
, Qnil
);
3294 tem
= Fmemq (feature
, Vfeatures
);
3296 error ("Required feature `%s' was not provided",
3297 SDATA (SYMBOL_NAME (feature
)));
3299 /* Once loading finishes, don't undo it. */
3300 Vautoload_queue
= Qt
;
3301 feature
= unbind_to (count
, feature
);
3307 /* Primitives for work of the "widget" library.
3308 In an ideal world, this section would not have been necessary.
3309 However, lisp function calls being as slow as they are, it turns
3310 out that some functions in the widget library (wid-edit.el) are the
3311 bottleneck of Widget operation. Here is their translation to C,
3312 for the sole reason of efficiency. */
3314 DEFUN ("plist-member", Fplist_member
, Splist_member
, 2, 2, 0,
3315 doc
: /* Return non-nil if PLIST has the property PROP.
3316 PLIST is a property list, which is a list of the form
3317 \(PROP1 VALUE1 PROP2 VALUE2 ...\). PROP is a symbol.
3318 Unlike `plist-get', this allows you to distinguish between a missing
3319 property and a property with the value nil.
3320 The value is actually the tail of PLIST whose car is PROP. */)
3322 Lisp_Object plist
, prop
;
3324 while (CONSP (plist
) && !EQ (XCAR (plist
), prop
))
3327 plist
= XCDR (plist
);
3328 plist
= CDR (plist
);
3333 DEFUN ("widget-put", Fwidget_put
, Swidget_put
, 3, 3, 0,
3334 doc
: /* In WIDGET, set PROPERTY to VALUE.
3335 The value can later be retrieved with `widget-get'. */)
3336 (widget
, property
, value
)
3337 Lisp_Object widget
, property
, value
;
3339 CHECK_CONS (widget
);
3340 XSETCDR (widget
, Fplist_put (XCDR (widget
), property
, value
));
3344 DEFUN ("widget-get", Fwidget_get
, Swidget_get
, 2, 2, 0,
3345 doc
: /* In WIDGET, get the value of PROPERTY.
3346 The value could either be specified when the widget was created, or
3347 later with `widget-put'. */)
3349 Lisp_Object widget
, property
;
3357 CHECK_CONS (widget
);
3358 tmp
= Fplist_member (XCDR (widget
), property
);
3364 tmp
= XCAR (widget
);
3367 widget
= Fget (tmp
, Qwidget_type
);
3371 DEFUN ("widget-apply", Fwidget_apply
, Swidget_apply
, 2, MANY
, 0,
3372 doc
: /* Apply the value of WIDGET's PROPERTY to the widget itself.
3373 ARGS are passed as extra arguments to the function.
3374 usage: (widget-apply WIDGET PROPERTY &rest ARGS) */)
3379 /* This function can GC. */
3380 Lisp_Object newargs
[3];
3381 struct gcpro gcpro1
, gcpro2
;
3384 newargs
[0] = Fwidget_get (args
[0], args
[1]);
3385 newargs
[1] = args
[0];
3386 newargs
[2] = Flist (nargs
- 2, args
+ 2);
3387 GCPRO2 (newargs
[0], newargs
[2]);
3388 result
= Fapply (3, newargs
);
3393 /* base64 encode/decode functions (RFC 2045).
3394 Based on code from GNU recode. */
3396 #define MIME_LINE_LENGTH 76
3398 #define IS_ASCII(Character) \
3400 #define IS_BASE64(Character) \
3401 (IS_ASCII (Character) && base64_char_to_value[Character] >= 0)
3402 #define IS_BASE64_IGNORABLE(Character) \
3403 ((Character) == ' ' || (Character) == '\t' || (Character) == '\n' \
3404 || (Character) == '\f' || (Character) == '\r')
3406 /* Used by base64_decode_1 to retrieve a non-base64-ignorable
3407 character or return retval if there are no characters left to
3409 #define READ_QUADRUPLET_BYTE(retval) \
3414 if (nchars_return) \
3415 *nchars_return = nchars; \
3420 while (IS_BASE64_IGNORABLE (c))
3422 /* Don't use alloca for regions larger than this, lest we overflow
3424 #define MAX_ALLOCA 16*1024
3426 /* Table of characters coding the 64 values. */
3427 static char base64_value_to_char
[64] =
3429 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 0- 9 */
3430 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 10-19 */
3431 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', /* 20-29 */
3432 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', /* 30-39 */
3433 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', /* 40-49 */
3434 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', /* 50-59 */
3435 '8', '9', '+', '/' /* 60-63 */
3438 /* Table of base64 values for first 128 characters. */
3439 static short base64_char_to_value
[128] =
3441 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0- 9 */
3442 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10- 19 */
3443 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20- 29 */
3444 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 30- 39 */
3445 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, /* 40- 49 */
3446 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, /* 50- 59 */
3447 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, /* 60- 69 */
3448 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 70- 79 */
3449 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, /* 80- 89 */
3450 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, /* 90- 99 */
3451 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, /* 100-109 */
3452 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, /* 110-119 */
3453 49, 50, 51, -1, -1, -1, -1, -1 /* 120-127 */
3456 /* The following diagram shows the logical steps by which three octets
3457 get transformed into four base64 characters.
3459 .--------. .--------. .--------.
3460 |aaaaaabb| |bbbbcccc| |ccdddddd|
3461 `--------' `--------' `--------'
3463 .--------+--------+--------+--------.
3464 |00aaaaaa|00bbbbbb|00cccccc|00dddddd|
3465 `--------+--------+--------+--------'
3467 .--------+--------+--------+--------.
3468 |AAAAAAAA|BBBBBBBB|CCCCCCCC|DDDDDDDD|
3469 `--------+--------+--------+--------'
3471 The octets are divided into 6 bit chunks, which are then encoded into
3472 base64 characters. */
3475 static int base64_encode_1
P_ ((const char *, char *, int, int, int));
3476 static int base64_decode_1
P_ ((const char *, char *, int, int, int *));
3478 DEFUN ("base64-encode-region", Fbase64_encode_region
, Sbase64_encode_region
,
3480 doc
: /* Base64-encode the region between BEG and END.
3481 Return the length of the encoded text.
3482 Optional third argument NO-LINE-BREAK means do not break long lines
3483 into shorter lines. */)
3484 (beg
, end
, no_line_break
)
3485 Lisp_Object beg
, end
, no_line_break
;
3488 int allength
, length
;
3489 int ibeg
, iend
, encoded_length
;
3492 validate_region (&beg
, &end
);
3494 ibeg
= CHAR_TO_BYTE (XFASTINT (beg
));
3495 iend
= CHAR_TO_BYTE (XFASTINT (end
));
3496 move_gap_both (XFASTINT (beg
), ibeg
);
3498 /* We need to allocate enough room for encoding the text.
3499 We need 33 1/3% more space, plus a newline every 76
3500 characters, and then we round up. */
3501 length
= iend
- ibeg
;
3502 allength
= length
+ length
/3 + 1;
3503 allength
+= allength
/ MIME_LINE_LENGTH
+ 1 + 6;
3505 if (allength
<= MAX_ALLOCA
)
3506 encoded
= (char *) alloca (allength
);
3508 encoded
= (char *) xmalloc (allength
);
3509 encoded_length
= base64_encode_1 (BYTE_POS_ADDR (ibeg
), encoded
, length
,
3510 NILP (no_line_break
),
3511 !NILP (current_buffer
->enable_multibyte_characters
));
3512 if (encoded_length
> allength
)
3515 if (encoded_length
< 0)
3517 /* The encoding wasn't possible. */
3518 if (length
> MAX_ALLOCA
)
3520 error ("Multibyte character in data for base64 encoding");
3523 /* Now we have encoded the region, so we insert the new contents
3524 and delete the old. (Insert first in order to preserve markers.) */
3525 SET_PT_BOTH (XFASTINT (beg
), ibeg
);
3526 insert (encoded
, encoded_length
);
3527 if (allength
> MAX_ALLOCA
)
3529 del_range_byte (ibeg
+ encoded_length
, iend
+ encoded_length
, 1);
3531 /* If point was outside of the region, restore it exactly; else just
3532 move to the beginning of the region. */
3533 if (old_pos
>= XFASTINT (end
))
3534 old_pos
+= encoded_length
- (XFASTINT (end
) - XFASTINT (beg
));
3535 else if (old_pos
> XFASTINT (beg
))
3536 old_pos
= XFASTINT (beg
);
3539 /* We return the length of the encoded text. */
3540 return make_number (encoded_length
);
3543 DEFUN ("base64-encode-string", Fbase64_encode_string
, Sbase64_encode_string
,
3545 doc
: /* Base64-encode STRING and return the result.
3546 Optional second argument NO-LINE-BREAK means do not break long lines
3547 into shorter lines. */)
3548 (string
, no_line_break
)
3549 Lisp_Object string
, no_line_break
;
3551 int allength
, length
, encoded_length
;
3553 Lisp_Object encoded_string
;
3555 CHECK_STRING (string
);
3557 /* We need to allocate enough room for encoding the text.
3558 We need 33 1/3% more space, plus a newline every 76
3559 characters, and then we round up. */
3560 length
= SBYTES (string
);
3561 allength
= length
+ length
/3 + 1;
3562 allength
+= allength
/ MIME_LINE_LENGTH
+ 1 + 6;
3564 /* We need to allocate enough room for decoding the text. */
3565 if (allength
<= MAX_ALLOCA
)
3566 encoded
= (char *) alloca (allength
);
3568 encoded
= (char *) xmalloc (allength
);
3570 encoded_length
= base64_encode_1 (SDATA (string
),
3571 encoded
, length
, NILP (no_line_break
),
3572 STRING_MULTIBYTE (string
));
3573 if (encoded_length
> allength
)
3576 if (encoded_length
< 0)
3578 /* The encoding wasn't possible. */
3579 if (length
> MAX_ALLOCA
)
3581 error ("Multibyte character in data for base64 encoding");
3584 encoded_string
= make_unibyte_string (encoded
, encoded_length
);
3585 if (allength
> MAX_ALLOCA
)
3588 return encoded_string
;
3592 base64_encode_1 (from
, to
, length
, line_break
, multibyte
)
3599 int counter
= 0, i
= 0;
3609 c
= STRING_CHAR_AND_LENGTH (from
+ i
, length
- i
, bytes
);
3617 /* Wrap line every 76 characters. */
3621 if (counter
< MIME_LINE_LENGTH
/ 4)
3630 /* Process first byte of a triplet. */
3632 *e
++ = base64_value_to_char
[0x3f & c
>> 2];
3633 value
= (0x03 & c
) << 4;
3635 /* Process second byte of a triplet. */
3639 *e
++ = base64_value_to_char
[value
];
3647 c
= STRING_CHAR_AND_LENGTH (from
+ i
, length
- i
, bytes
);
3655 *e
++ = base64_value_to_char
[value
| (0x0f & c
>> 4)];
3656 value
= (0x0f & c
) << 2;
3658 /* Process third byte of a triplet. */
3662 *e
++ = base64_value_to_char
[value
];
3669 c
= STRING_CHAR_AND_LENGTH (from
+ i
, length
- i
, bytes
);
3677 *e
++ = base64_value_to_char
[value
| (0x03 & c
>> 6)];
3678 *e
++ = base64_value_to_char
[0x3f & c
];
3685 DEFUN ("base64-decode-region", Fbase64_decode_region
, Sbase64_decode_region
,
3687 doc
: /* Base64-decode the region between BEG and END.
3688 Return the length of the decoded text.
3689 If the region can't be decoded, signal an error and don't modify the buffer. */)
3691 Lisp_Object beg
, end
;
3693 int ibeg
, iend
, length
, allength
;
3698 int multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
3700 validate_region (&beg
, &end
);
3702 ibeg
= CHAR_TO_BYTE (XFASTINT (beg
));
3703 iend
= CHAR_TO_BYTE (XFASTINT (end
));
3705 length
= iend
- ibeg
;
3707 /* We need to allocate enough room for decoding the text. If we are
3708 working on a multibyte buffer, each decoded code may occupy at
3710 allength
= multibyte
? length
* 2 : length
;
3711 if (allength
<= MAX_ALLOCA
)
3712 decoded
= (char *) alloca (allength
);
3714 decoded
= (char *) xmalloc (allength
);
3716 move_gap_both (XFASTINT (beg
), ibeg
);
3717 decoded_length
= base64_decode_1 (BYTE_POS_ADDR (ibeg
), decoded
, length
,
3718 multibyte
, &inserted_chars
);
3719 if (decoded_length
> allength
)
3722 if (decoded_length
< 0)
3724 /* The decoding wasn't possible. */
3725 if (allength
> MAX_ALLOCA
)
3727 error ("Invalid base64 data");
3730 /* Now we have decoded the region, so we insert the new contents
3731 and delete the old. (Insert first in order to preserve markers.) */
3732 TEMP_SET_PT_BOTH (XFASTINT (beg
), ibeg
);
3733 insert_1_both (decoded
, inserted_chars
, decoded_length
, 0, 1, 0);
3734 if (allength
> MAX_ALLOCA
)
3736 /* Delete the original text. */
3737 del_range_both (PT
, PT_BYTE
, XFASTINT (end
) + inserted_chars
,
3738 iend
+ decoded_length
, 1);
3740 /* If point was outside of the region, restore it exactly; else just
3741 move to the beginning of the region. */
3742 if (old_pos
>= XFASTINT (end
))
3743 old_pos
+= inserted_chars
- (XFASTINT (end
) - XFASTINT (beg
));
3744 else if (old_pos
> XFASTINT (beg
))
3745 old_pos
= XFASTINT (beg
);
3746 SET_PT (old_pos
> ZV
? ZV
: old_pos
);
3748 return make_number (inserted_chars
);
3751 DEFUN ("base64-decode-string", Fbase64_decode_string
, Sbase64_decode_string
,
3753 doc
: /* Base64-decode STRING and return the result. */)
3758 int length
, decoded_length
;
3759 Lisp_Object decoded_string
;
3761 CHECK_STRING (string
);
3763 length
= SBYTES (string
);
3764 /* We need to allocate enough room for decoding the text. */
3765 if (length
<= MAX_ALLOCA
)
3766 decoded
= (char *) alloca (length
);
3768 decoded
= (char *) xmalloc (length
);
3770 /* The decoded result should be unibyte. */
3771 decoded_length
= base64_decode_1 (SDATA (string
), decoded
, length
,
3773 if (decoded_length
> length
)
3775 else if (decoded_length
>= 0)
3776 decoded_string
= make_unibyte_string (decoded
, decoded_length
);
3778 decoded_string
= Qnil
;
3780 if (length
> MAX_ALLOCA
)
3782 if (!STRINGP (decoded_string
))
3783 error ("Invalid base64 data");
3785 return decoded_string
;
3788 /* Base64-decode the data at FROM of LENGHT bytes into TO. If
3789 MULTIBYTE is nonzero, the decoded result should be in multibyte
3790 form. If NCHARS_RETRUN is not NULL, store the number of produced
3791 characters in *NCHARS_RETURN. */
3794 base64_decode_1 (from
, to
, length
, multibyte
, nchars_return
)
3804 unsigned long value
;
3809 /* Process first byte of a quadruplet. */
3811 READ_QUADRUPLET_BYTE (e
-to
);
3815 value
= base64_char_to_value
[c
] << 18;
3817 /* Process second byte of a quadruplet. */
3819 READ_QUADRUPLET_BYTE (-1);
3823 value
|= base64_char_to_value
[c
] << 12;
3825 c
= (unsigned char) (value
>> 16);
3827 e
+= CHAR_STRING (c
, e
);
3832 /* Process third byte of a quadruplet. */
3834 READ_QUADRUPLET_BYTE (-1);
3838 READ_QUADRUPLET_BYTE (-1);
3847 value
|= base64_char_to_value
[c
] << 6;
3849 c
= (unsigned char) (0xff & value
>> 8);
3851 e
+= CHAR_STRING (c
, e
);
3856 /* Process fourth byte of a quadruplet. */
3858 READ_QUADRUPLET_BYTE (-1);
3865 value
|= base64_char_to_value
[c
];
3867 c
= (unsigned char) (0xff & value
);
3869 e
+= CHAR_STRING (c
, e
);
3878 /***********************************************************************
3880 ***** Hash Tables *****
3882 ***********************************************************************/
3884 /* Implemented by gerd@gnu.org. This hash table implementation was
3885 inspired by CMUCL hash tables. */
3889 1. For small tables, association lists are probably faster than
3890 hash tables because they have lower overhead.
3892 For uses of hash tables where the O(1) behavior of table
3893 operations is not a requirement, it might therefore be a good idea
3894 not to hash. Instead, we could just do a linear search in the
3895 key_and_value vector of the hash table. This could be done
3896 if a `:linear-search t' argument is given to make-hash-table. */
3899 /* The list of all weak hash tables. Don't staticpro this one. */
3901 Lisp_Object Vweak_hash_tables
;
3903 /* Various symbols. */
3905 Lisp_Object Qhash_table_p
, Qeq
, Qeql
, Qequal
, Qkey
, Qvalue
;
3906 Lisp_Object QCtest
, QCsize
, QCrehash_size
, QCrehash_threshold
, QCweakness
;
3907 Lisp_Object Qhash_table_test
, Qkey_or_value
, Qkey_and_value
;
3909 /* Function prototypes. */
3911 static struct Lisp_Hash_Table
*check_hash_table
P_ ((Lisp_Object
));
3912 static int get_key_arg
P_ ((Lisp_Object
, int, Lisp_Object
*, char *));
3913 static void maybe_resize_hash_table
P_ ((struct Lisp_Hash_Table
*));
3914 static int cmpfn_eql
P_ ((struct Lisp_Hash_Table
*, Lisp_Object
, unsigned,
3915 Lisp_Object
, unsigned));
3916 static int cmpfn_equal
P_ ((struct Lisp_Hash_Table
*, Lisp_Object
, unsigned,
3917 Lisp_Object
, unsigned));
3918 static int cmpfn_user_defined
P_ ((struct Lisp_Hash_Table
*, Lisp_Object
,
3919 unsigned, Lisp_Object
, unsigned));
3920 static unsigned hashfn_eq
P_ ((struct Lisp_Hash_Table
*, Lisp_Object
));
3921 static unsigned hashfn_eql
P_ ((struct Lisp_Hash_Table
*, Lisp_Object
));
3922 static unsigned hashfn_equal
P_ ((struct Lisp_Hash_Table
*, Lisp_Object
));
3923 static unsigned hashfn_user_defined
P_ ((struct Lisp_Hash_Table
*,
3925 static unsigned sxhash_string
P_ ((unsigned char *, int));
3926 static unsigned sxhash_list
P_ ((Lisp_Object
, int));
3927 static unsigned sxhash_vector
P_ ((Lisp_Object
, int));
3928 static unsigned sxhash_bool_vector
P_ ((Lisp_Object
));
3929 static int sweep_weak_table
P_ ((struct Lisp_Hash_Table
*, int));
3933 /***********************************************************************
3935 ***********************************************************************/
3937 /* If OBJ is a Lisp hash table, return a pointer to its struct
3938 Lisp_Hash_Table. Otherwise, signal an error. */
3940 static struct Lisp_Hash_Table
*
3941 check_hash_table (obj
)
3944 CHECK_HASH_TABLE (obj
);
3945 return XHASH_TABLE (obj
);
3949 /* Value is the next integer I >= N, N >= 0 which is "almost" a prime
3953 next_almost_prime (n
)
3966 /* Find KEY in ARGS which has size NARGS. Don't consider indices for
3967 which USED[I] is non-zero. If found at index I in ARGS, set
3968 USED[I] and USED[I + 1] to 1, and return I + 1. Otherwise return
3969 -1. This function is used to extract a keyword/argument pair from
3970 a DEFUN parameter list. */
3973 get_key_arg (key
, nargs
, args
, used
)
3981 for (i
= 0; i
< nargs
- 1; ++i
)
3982 if (!used
[i
] && EQ (args
[i
], key
))
3997 /* Return a Lisp vector which has the same contents as VEC but has
3998 size NEW_SIZE, NEW_SIZE >= VEC->size. Entries in the resulting
3999 vector that are not copied from VEC are set to INIT. */
4002 larger_vector (vec
, new_size
, init
)
4007 struct Lisp_Vector
*v
;
4010 xassert (VECTORP (vec
));
4011 old_size
= XVECTOR (vec
)->size
;
4012 xassert (new_size
>= old_size
);
4014 v
= allocate_vector (new_size
);
4015 bcopy (XVECTOR (vec
)->contents
, v
->contents
,
4016 old_size
* sizeof *v
->contents
);
4017 for (i
= old_size
; i
< new_size
; ++i
)
4018 v
->contents
[i
] = init
;
4019 XSETVECTOR (vec
, v
);
4024 /***********************************************************************
4026 ***********************************************************************/
4028 /* Compare KEY1 which has hash code HASH1 and KEY2 with hash code
4029 HASH2 in hash table H using `eql'. Value is non-zero if KEY1 and
4030 KEY2 are the same. */
4033 cmpfn_eql (h
, key1
, hash1
, key2
, hash2
)
4034 struct Lisp_Hash_Table
*h
;
4035 Lisp_Object key1
, key2
;
4036 unsigned hash1
, hash2
;
4038 return (FLOATP (key1
)
4040 && XFLOAT_DATA (key1
) == XFLOAT_DATA (key2
));
4044 /* Compare KEY1 which has hash code HASH1 and KEY2 with hash code
4045 HASH2 in hash table H using `equal'. Value is non-zero if KEY1 and
4046 KEY2 are the same. */
4049 cmpfn_equal (h
, key1
, hash1
, key2
, hash2
)
4050 struct Lisp_Hash_Table
*h
;
4051 Lisp_Object key1
, key2
;
4052 unsigned hash1
, hash2
;
4054 return hash1
== hash2
&& !NILP (Fequal (key1
, key2
));
4058 /* Compare KEY1 which has hash code HASH1, and KEY2 with hash code
4059 HASH2 in hash table H using H->user_cmp_function. Value is non-zero
4060 if KEY1 and KEY2 are the same. */
4063 cmpfn_user_defined (h
, key1
, hash1
, key2
, hash2
)
4064 struct Lisp_Hash_Table
*h
;
4065 Lisp_Object key1
, key2
;
4066 unsigned hash1
, hash2
;
4070 Lisp_Object args
[3];
4072 args
[0] = h
->user_cmp_function
;
4075 return !NILP (Ffuncall (3, args
));
4082 /* Value is a hash code for KEY for use in hash table H which uses
4083 `eq' to compare keys. The hash code returned is guaranteed to fit
4084 in a Lisp integer. */
4088 struct Lisp_Hash_Table
*h
;
4091 unsigned hash
= XUINT (key
) ^ XGCTYPE (key
);
4092 xassert ((hash
& ~VALMASK
) == 0);
4097 /* Value is a hash code for KEY for use in hash table H which uses
4098 `eql' to compare keys. The hash code returned is guaranteed to fit
4099 in a Lisp integer. */
4103 struct Lisp_Hash_Table
*h
;
4108 hash
= sxhash (key
, 0);
4110 hash
= XUINT (key
) ^ XGCTYPE (key
);
4111 xassert ((hash
& ~VALMASK
) == 0);
4116 /* Value is a hash code for KEY for use in hash table H which uses
4117 `equal' to compare keys. The hash code returned is guaranteed to fit
4118 in a Lisp integer. */
4121 hashfn_equal (h
, key
)
4122 struct Lisp_Hash_Table
*h
;
4125 unsigned hash
= sxhash (key
, 0);
4126 xassert ((hash
& ~VALMASK
) == 0);
4131 /* Value is a hash code for KEY for use in hash table H which uses as
4132 user-defined function to compare keys. The hash code returned is
4133 guaranteed to fit in a Lisp integer. */
4136 hashfn_user_defined (h
, key
)
4137 struct Lisp_Hash_Table
*h
;
4140 Lisp_Object args
[2], hash
;
4142 args
[0] = h
->user_hash_function
;
4144 hash
= Ffuncall (2, args
);
4145 if (!INTEGERP (hash
))
4147 list2 (build_string ("Invalid hash code returned from \
4148 user-supplied hash function"),
4150 return XUINT (hash
);
4154 /* Create and initialize a new hash table.
4156 TEST specifies the test the hash table will use to compare keys.
4157 It must be either one of the predefined tests `eq', `eql' or
4158 `equal' or a symbol denoting a user-defined test named TEST with
4159 test and hash functions USER_TEST and USER_HASH.
4161 Give the table initial capacity SIZE, SIZE >= 0, an integer.
4163 If REHASH_SIZE is an integer, it must be > 0, and this hash table's
4164 new size when it becomes full is computed by adding REHASH_SIZE to
4165 its old size. If REHASH_SIZE is a float, it must be > 1.0, and the
4166 table's new size is computed by multiplying its old size with
4169 REHASH_THRESHOLD must be a float <= 1.0, and > 0. The table will
4170 be resized when the ratio of (number of entries in the table) /
4171 (table size) is >= REHASH_THRESHOLD.
4173 WEAK specifies the weakness of the table. If non-nil, it must be
4174 one of the symbols `key', `value', `key-or-value', or `key-and-value'. */
4177 make_hash_table (test
, size
, rehash_size
, rehash_threshold
, weak
,
4178 user_test
, user_hash
)
4179 Lisp_Object test
, size
, rehash_size
, rehash_threshold
, weak
;
4180 Lisp_Object user_test
, user_hash
;
4182 struct Lisp_Hash_Table
*h
;
4184 int index_size
, i
, sz
;
4186 /* Preconditions. */
4187 xassert (SYMBOLP (test
));
4188 xassert (INTEGERP (size
) && XINT (size
) >= 0);
4189 xassert ((INTEGERP (rehash_size
) && XINT (rehash_size
) > 0)
4190 || (FLOATP (rehash_size
) && XFLOATINT (rehash_size
) > 1.0));
4191 xassert (FLOATP (rehash_threshold
)
4192 && XFLOATINT (rehash_threshold
) > 0
4193 && XFLOATINT (rehash_threshold
) <= 1.0);
4195 if (XFASTINT (size
) == 0)
4196 size
= make_number (1);
4198 /* Allocate a table and initialize it. */
4199 h
= allocate_hash_table ();
4201 /* Initialize hash table slots. */
4202 sz
= XFASTINT (size
);
4205 if (EQ (test
, Qeql
))
4207 h
->cmpfn
= cmpfn_eql
;
4208 h
->hashfn
= hashfn_eql
;
4210 else if (EQ (test
, Qeq
))
4213 h
->hashfn
= hashfn_eq
;
4215 else if (EQ (test
, Qequal
))
4217 h
->cmpfn
= cmpfn_equal
;
4218 h
->hashfn
= hashfn_equal
;
4222 h
->user_cmp_function
= user_test
;
4223 h
->user_hash_function
= user_hash
;
4224 h
->cmpfn
= cmpfn_user_defined
;
4225 h
->hashfn
= hashfn_user_defined
;
4229 h
->rehash_threshold
= rehash_threshold
;
4230 h
->rehash_size
= rehash_size
;
4231 h
->count
= make_number (0);
4232 h
->key_and_value
= Fmake_vector (make_number (2 * sz
), Qnil
);
4233 h
->hash
= Fmake_vector (size
, Qnil
);
4234 h
->next
= Fmake_vector (size
, Qnil
);
4235 /* Cast to int here avoids losing with gcc 2.95 on Tru64/Alpha... */
4236 index_size
= next_almost_prime ((int) (sz
/ XFLOATINT (rehash_threshold
)));
4237 h
->index
= Fmake_vector (make_number (index_size
), Qnil
);
4239 /* Set up the free list. */
4240 for (i
= 0; i
< sz
- 1; ++i
)
4241 HASH_NEXT (h
, i
) = make_number (i
+ 1);
4242 h
->next_free
= make_number (0);
4244 XSET_HASH_TABLE (table
, h
);
4245 xassert (HASH_TABLE_P (table
));
4246 xassert (XHASH_TABLE (table
) == h
);
4248 /* Maybe add this hash table to the list of all weak hash tables. */
4250 h
->next_weak
= Qnil
;
4253 h
->next_weak
= Vweak_hash_tables
;
4254 Vweak_hash_tables
= table
;
4261 /* Return a copy of hash table H1. Keys and values are not copied,
4262 only the table itself is. */
4265 copy_hash_table (h1
)
4266 struct Lisp_Hash_Table
*h1
;
4269 struct Lisp_Hash_Table
*h2
;
4270 struct Lisp_Vector
*next
;
4272 h2
= allocate_hash_table ();
4273 next
= h2
->vec_next
;
4274 bcopy (h1
, h2
, sizeof *h2
);
4275 h2
->vec_next
= next
;
4276 h2
->key_and_value
= Fcopy_sequence (h1
->key_and_value
);
4277 h2
->hash
= Fcopy_sequence (h1
->hash
);
4278 h2
->next
= Fcopy_sequence (h1
->next
);
4279 h2
->index
= Fcopy_sequence (h1
->index
);
4280 XSET_HASH_TABLE (table
, h2
);
4282 /* Maybe add this hash table to the list of all weak hash tables. */
4283 if (!NILP (h2
->weak
))
4285 h2
->next_weak
= Vweak_hash_tables
;
4286 Vweak_hash_tables
= table
;
4293 /* Resize hash table H if it's too full. If H cannot be resized
4294 because it's already too large, throw an error. */
4297 maybe_resize_hash_table (h
)
4298 struct Lisp_Hash_Table
*h
;
4300 if (NILP (h
->next_free
))
4302 int old_size
= HASH_TABLE_SIZE (h
);
4303 int i
, new_size
, index_size
;
4305 if (INTEGERP (h
->rehash_size
))
4306 new_size
= old_size
+ XFASTINT (h
->rehash_size
);
4308 new_size
= old_size
* XFLOATINT (h
->rehash_size
);
4309 new_size
= max (old_size
+ 1, new_size
);
4310 index_size
= next_almost_prime ((int)
4312 / XFLOATINT (h
->rehash_threshold
)));
4313 if (max (index_size
, 2 * new_size
) & ~VALMASK
)
4314 error ("Hash table too large to resize");
4316 h
->key_and_value
= larger_vector (h
->key_and_value
, 2 * new_size
, Qnil
);
4317 h
->next
= larger_vector (h
->next
, new_size
, Qnil
);
4318 h
->hash
= larger_vector (h
->hash
, new_size
, Qnil
);
4319 h
->index
= Fmake_vector (make_number (index_size
), Qnil
);
4321 /* Update the free list. Do it so that new entries are added at
4322 the end of the free list. This makes some operations like
4324 for (i
= old_size
; i
< new_size
- 1; ++i
)
4325 HASH_NEXT (h
, i
) = make_number (i
+ 1);
4327 if (!NILP (h
->next_free
))
4329 Lisp_Object last
, next
;
4331 last
= h
->next_free
;
4332 while (next
= HASH_NEXT (h
, XFASTINT (last
)),
4336 HASH_NEXT (h
, XFASTINT (last
)) = make_number (old_size
);
4339 XSETFASTINT (h
->next_free
, old_size
);
4342 for (i
= 0; i
< old_size
; ++i
)
4343 if (!NILP (HASH_HASH (h
, i
)))
4345 unsigned hash_code
= XUINT (HASH_HASH (h
, i
));
4346 int start_of_bucket
= hash_code
% XVECTOR (h
->index
)->size
;
4347 HASH_NEXT (h
, i
) = HASH_INDEX (h
, start_of_bucket
);
4348 HASH_INDEX (h
, start_of_bucket
) = make_number (i
);
4354 /* Lookup KEY in hash table H. If HASH is non-null, return in *HASH
4355 the hash code of KEY. Value is the index of the entry in H
4356 matching KEY, or -1 if not found. */
4359 hash_lookup (h
, key
, hash
)
4360 struct Lisp_Hash_Table
*h
;
4365 int start_of_bucket
;
4368 hash_code
= h
->hashfn (h
, key
);
4372 start_of_bucket
= hash_code
% XVECTOR (h
->index
)->size
;
4373 idx
= HASH_INDEX (h
, start_of_bucket
);
4375 /* We need not gcpro idx since it's either an integer or nil. */
4378 int i
= XFASTINT (idx
);
4379 if (EQ (key
, HASH_KEY (h
, i
))
4381 && h
->cmpfn (h
, key
, hash_code
,
4382 HASH_KEY (h
, i
), XUINT (HASH_HASH (h
, i
)))))
4384 idx
= HASH_NEXT (h
, i
);
4387 return NILP (idx
) ? -1 : XFASTINT (idx
);
4391 /* Put an entry into hash table H that associates KEY with VALUE.
4392 HASH is a previously computed hash code of KEY.
4393 Value is the index of the entry in H matching KEY. */
4396 hash_put (h
, key
, value
, hash
)
4397 struct Lisp_Hash_Table
*h
;
4398 Lisp_Object key
, value
;
4401 int start_of_bucket
, i
;
4403 xassert ((hash
& ~VALMASK
) == 0);
4405 /* Increment count after resizing because resizing may fail. */
4406 maybe_resize_hash_table (h
);
4407 h
->count
= make_number (XFASTINT (h
->count
) + 1);
4409 /* Store key/value in the key_and_value vector. */
4410 i
= XFASTINT (h
->next_free
);
4411 h
->next_free
= HASH_NEXT (h
, i
);
4412 HASH_KEY (h
, i
) = key
;
4413 HASH_VALUE (h
, i
) = value
;
4415 /* Remember its hash code. */
4416 HASH_HASH (h
, i
) = make_number (hash
);
4418 /* Add new entry to its collision chain. */
4419 start_of_bucket
= hash
% XVECTOR (h
->index
)->size
;
4420 HASH_NEXT (h
, i
) = HASH_INDEX (h
, start_of_bucket
);
4421 HASH_INDEX (h
, start_of_bucket
) = make_number (i
);
4426 /* Remove the entry matching KEY from hash table H, if there is one. */
4429 hash_remove (h
, key
)
4430 struct Lisp_Hash_Table
*h
;
4434 int start_of_bucket
;
4435 Lisp_Object idx
, prev
;
4437 hash_code
= h
->hashfn (h
, key
);
4438 start_of_bucket
= hash_code
% XVECTOR (h
->index
)->size
;
4439 idx
= HASH_INDEX (h
, start_of_bucket
);
4442 /* We need not gcpro idx, prev since they're either integers or nil. */
4445 int i
= XFASTINT (idx
);
4447 if (EQ (key
, HASH_KEY (h
, i
))
4449 && h
->cmpfn (h
, key
, hash_code
,
4450 HASH_KEY (h
, i
), XUINT (HASH_HASH (h
, i
)))))
4452 /* Take entry out of collision chain. */
4454 HASH_INDEX (h
, start_of_bucket
) = HASH_NEXT (h
, i
);
4456 HASH_NEXT (h
, XFASTINT (prev
)) = HASH_NEXT (h
, i
);
4458 /* Clear slots in key_and_value and add the slots to
4460 HASH_KEY (h
, i
) = HASH_VALUE (h
, i
) = HASH_HASH (h
, i
) = Qnil
;
4461 HASH_NEXT (h
, i
) = h
->next_free
;
4462 h
->next_free
= make_number (i
);
4463 h
->count
= make_number (XFASTINT (h
->count
) - 1);
4464 xassert (XINT (h
->count
) >= 0);
4470 idx
= HASH_NEXT (h
, i
);
4476 /* Clear hash table H. */
4480 struct Lisp_Hash_Table
*h
;
4482 if (XFASTINT (h
->count
) > 0)
4484 int i
, size
= HASH_TABLE_SIZE (h
);
4486 for (i
= 0; i
< size
; ++i
)
4488 HASH_NEXT (h
, i
) = i
< size
- 1 ? make_number (i
+ 1) : Qnil
;
4489 HASH_KEY (h
, i
) = Qnil
;
4490 HASH_VALUE (h
, i
) = Qnil
;
4491 HASH_HASH (h
, i
) = Qnil
;
4494 for (i
= 0; i
< XVECTOR (h
->index
)->size
; ++i
)
4495 XVECTOR (h
->index
)->contents
[i
] = Qnil
;
4497 h
->next_free
= make_number (0);
4498 h
->count
= make_number (0);
4504 /************************************************************************
4506 ************************************************************************/
4508 /* Sweep weak hash table H. REMOVE_ENTRIES_P non-zero means remove
4509 entries from the table that don't survive the current GC.
4510 REMOVE_ENTRIES_P zero means mark entries that are in use. Value is
4511 non-zero if anything was marked. */
4514 sweep_weak_table (h
, remove_entries_p
)
4515 struct Lisp_Hash_Table
*h
;
4516 int remove_entries_p
;
4518 int bucket
, n
, marked
;
4520 n
= XVECTOR (h
->index
)->size
& ~ARRAY_MARK_FLAG
;
4523 for (bucket
= 0; bucket
< n
; ++bucket
)
4525 Lisp_Object idx
, next
, prev
;
4527 /* Follow collision chain, removing entries that
4528 don't survive this garbage collection. */
4530 for (idx
= HASH_INDEX (h
, bucket
); !GC_NILP (idx
); idx
= next
)
4532 int i
= XFASTINT (idx
);
4533 int key_known_to_survive_p
= survives_gc_p (HASH_KEY (h
, i
));
4534 int value_known_to_survive_p
= survives_gc_p (HASH_VALUE (h
, i
));
4537 if (EQ (h
->weak
, Qkey
))
4538 remove_p
= !key_known_to_survive_p
;
4539 else if (EQ (h
->weak
, Qvalue
))
4540 remove_p
= !value_known_to_survive_p
;
4541 else if (EQ (h
->weak
, Qkey_or_value
))
4542 remove_p
= !(key_known_to_survive_p
|| value_known_to_survive_p
);
4543 else if (EQ (h
->weak
, Qkey_and_value
))
4544 remove_p
= !(key_known_to_survive_p
&& value_known_to_survive_p
);
4548 next
= HASH_NEXT (h
, i
);
4550 if (remove_entries_p
)
4554 /* Take out of collision chain. */
4556 HASH_INDEX (h
, bucket
) = next
;
4558 HASH_NEXT (h
, XFASTINT (prev
)) = next
;
4560 /* Add to free list. */
4561 HASH_NEXT (h
, i
) = h
->next_free
;
4564 /* Clear key, value, and hash. */
4565 HASH_KEY (h
, i
) = HASH_VALUE (h
, i
) = Qnil
;
4566 HASH_HASH (h
, i
) = Qnil
;
4568 h
->count
= make_number (XFASTINT (h
->count
) - 1);
4575 /* Make sure key and value survive. */
4576 if (!key_known_to_survive_p
)
4578 mark_object (&HASH_KEY (h
, i
));
4582 if (!value_known_to_survive_p
)
4584 mark_object (&HASH_VALUE (h
, i
));
4595 /* Remove elements from weak hash tables that don't survive the
4596 current garbage collection. Remove weak tables that don't survive
4597 from Vweak_hash_tables. Called from gc_sweep. */
4600 sweep_weak_hash_tables ()
4602 Lisp_Object table
, used
, next
;
4603 struct Lisp_Hash_Table
*h
;
4606 /* Mark all keys and values that are in use. Keep on marking until
4607 there is no more change. This is necessary for cases like
4608 value-weak table A containing an entry X -> Y, where Y is used in a
4609 key-weak table B, Z -> Y. If B comes after A in the list of weak
4610 tables, X -> Y might be removed from A, although when looking at B
4611 one finds that it shouldn't. */
4615 for (table
= Vweak_hash_tables
; !GC_NILP (table
); table
= h
->next_weak
)
4617 h
= XHASH_TABLE (table
);
4618 if (h
->size
& ARRAY_MARK_FLAG
)
4619 marked
|= sweep_weak_table (h
, 0);
4624 /* Remove tables and entries that aren't used. */
4625 for (table
= Vweak_hash_tables
, used
= Qnil
; !GC_NILP (table
); table
= next
)
4627 h
= XHASH_TABLE (table
);
4628 next
= h
->next_weak
;
4630 if (h
->size
& ARRAY_MARK_FLAG
)
4632 /* TABLE is marked as used. Sweep its contents. */
4633 if (XFASTINT (h
->count
) > 0)
4634 sweep_weak_table (h
, 1);
4636 /* Add table to the list of used weak hash tables. */
4637 h
->next_weak
= used
;
4642 Vweak_hash_tables
= used
;
4647 /***********************************************************************
4648 Hash Code Computation
4649 ***********************************************************************/
4651 /* Maximum depth up to which to dive into Lisp structures. */
4653 #define SXHASH_MAX_DEPTH 3
4655 /* Maximum length up to which to take list and vector elements into
4658 #define SXHASH_MAX_LEN 7
4660 /* Combine two integers X and Y for hashing. */
4662 #define SXHASH_COMBINE(X, Y) \
4663 ((((unsigned)(X) << 4) + (((unsigned)(X) >> 24) & 0x0fffffff)) \
4667 /* Return a hash for string PTR which has length LEN. The hash
4668 code returned is guaranteed to fit in a Lisp integer. */
4671 sxhash_string (ptr
, len
)
4675 unsigned char *p
= ptr
;
4676 unsigned char *end
= p
+ len
;
4685 hash
= ((hash
<< 3) + (hash
>> 28) + c
);
4688 return hash
& VALMASK
;
4692 /* Return a hash for list LIST. DEPTH is the current depth in the
4693 list. We don't recurse deeper than SXHASH_MAX_DEPTH in it. */
4696 sxhash_list (list
, depth
)
4703 if (depth
< SXHASH_MAX_DEPTH
)
4705 CONSP (list
) && i
< SXHASH_MAX_LEN
;
4706 list
= XCDR (list
), ++i
)
4708 unsigned hash2
= sxhash (XCAR (list
), depth
+ 1);
4709 hash
= SXHASH_COMBINE (hash
, hash2
);
4716 /* Return a hash for vector VECTOR. DEPTH is the current depth in
4717 the Lisp structure. */
4720 sxhash_vector (vec
, depth
)
4724 unsigned hash
= XVECTOR (vec
)->size
;
4727 n
= min (SXHASH_MAX_LEN
, XVECTOR (vec
)->size
);
4728 for (i
= 0; i
< n
; ++i
)
4730 unsigned hash2
= sxhash (XVECTOR (vec
)->contents
[i
], depth
+ 1);
4731 hash
= SXHASH_COMBINE (hash
, hash2
);
4738 /* Return a hash for bool-vector VECTOR. */
4741 sxhash_bool_vector (vec
)
4744 unsigned hash
= XBOOL_VECTOR (vec
)->size
;
4747 n
= min (SXHASH_MAX_LEN
, XBOOL_VECTOR (vec
)->vector_size
);
4748 for (i
= 0; i
< n
; ++i
)
4749 hash
= SXHASH_COMBINE (hash
, XBOOL_VECTOR (vec
)->data
[i
]);
4755 /* Return a hash code for OBJ. DEPTH is the current depth in the Lisp
4756 structure. Value is an unsigned integer clipped to VALMASK. */
4765 if (depth
> SXHASH_MAX_DEPTH
)
4768 switch (XTYPE (obj
))
4775 hash
= sxhash_string (SDATA (SYMBOL_NAME (obj
)),
4776 SCHARS (SYMBOL_NAME (obj
)));
4784 hash
= sxhash_string (SDATA (obj
), SCHARS (obj
));
4787 /* This can be everything from a vector to an overlay. */
4788 case Lisp_Vectorlike
:
4790 /* According to the CL HyperSpec, two arrays are equal only if
4791 they are `eq', except for strings and bit-vectors. In
4792 Emacs, this works differently. We have to compare element
4794 hash
= sxhash_vector (obj
, depth
);
4795 else if (BOOL_VECTOR_P (obj
))
4796 hash
= sxhash_bool_vector (obj
);
4798 /* Others are `equal' if they are `eq', so let's take their
4804 hash
= sxhash_list (obj
, depth
);
4809 unsigned char *p
= (unsigned char *) &XFLOAT_DATA (obj
);
4810 unsigned char *e
= p
+ sizeof XFLOAT_DATA (obj
);
4811 for (hash
= 0; p
< e
; ++p
)
4812 hash
= SXHASH_COMBINE (hash
, *p
);
4820 return hash
& VALMASK
;
4825 /***********************************************************************
4827 ***********************************************************************/
4830 DEFUN ("sxhash", Fsxhash
, Ssxhash
, 1, 1, 0,
4831 doc
: /* Compute a hash code for OBJ and return it as integer. */)
4835 unsigned hash
= sxhash (obj
, 0);;
4836 return make_number (hash
);
4840 DEFUN ("make-hash-table", Fmake_hash_table
, Smake_hash_table
, 0, MANY
, 0,
4841 doc
: /* Create and return a new hash table.
4843 Arguments are specified as keyword/argument pairs. The following
4844 arguments are defined:
4846 :test TEST -- TEST must be a symbol that specifies how to compare
4847 keys. Default is `eql'. Predefined are the tests `eq', `eql', and
4848 `equal'. User-supplied test and hash functions can be specified via
4849 `define-hash-table-test'.
4851 :size SIZE -- A hint as to how many elements will be put in the table.
4854 :rehash-size REHASH-SIZE - Indicates how to expand the table when it
4855 fills up. If REHASH-SIZE is an integer, add that many space. If it
4856 is a float, it must be > 1.0, and the new size is computed by
4857 multiplying the old size with that factor. Default is 1.5.
4859 :rehash-threshold THRESHOLD -- THRESHOLD must a float > 0, and <= 1.0.
4860 Resize the hash table when ratio of the number of entries in the
4861 table. Default is 0.8.
4863 :weakness WEAK -- WEAK must be one of nil, t, `key', `value',
4864 `key-or-value', or `key-and-value'. If WEAK is not nil, the table
4865 returned is a weak table. Key/value pairs are removed from a weak
4866 hash table when there are no non-weak references pointing to their
4867 key, value, one of key or value, or both key and value, depending on
4868 WEAK. WEAK t is equivalent to `key-and-value'. Default value of WEAK
4871 usage: (make-hash-table &rest KEYWORD-ARGS) */)
4876 Lisp_Object test
, size
, rehash_size
, rehash_threshold
, weak
;
4877 Lisp_Object user_test
, user_hash
;
4881 /* The vector `used' is used to keep track of arguments that
4882 have been consumed. */
4883 used
= (char *) alloca (nargs
* sizeof *used
);
4884 bzero (used
, nargs
* sizeof *used
);
4886 /* See if there's a `:test TEST' among the arguments. */
4887 i
= get_key_arg (QCtest
, nargs
, args
, used
);
4888 test
= i
< 0 ? Qeql
: args
[i
];
4889 if (!EQ (test
, Qeq
) && !EQ (test
, Qeql
) && !EQ (test
, Qequal
))
4891 /* See if it is a user-defined test. */
4894 prop
= Fget (test
, Qhash_table_test
);
4895 if (!CONSP (prop
) || !CONSP (XCDR (prop
)))
4896 Fsignal (Qerror
, list2 (build_string ("Invalid hash table test"),
4898 user_test
= XCAR (prop
);
4899 user_hash
= XCAR (XCDR (prop
));
4902 user_test
= user_hash
= Qnil
;
4904 /* See if there's a `:size SIZE' argument. */
4905 i
= get_key_arg (QCsize
, nargs
, args
, used
);
4906 size
= i
< 0 ? Qnil
: args
[i
];
4908 size
= make_number (DEFAULT_HASH_SIZE
);
4909 else if (!INTEGERP (size
) || XINT (size
) < 0)
4911 list2 (build_string ("Invalid hash table size"),
4914 /* Look for `:rehash-size SIZE'. */
4915 i
= get_key_arg (QCrehash_size
, nargs
, args
, used
);
4916 rehash_size
= i
< 0 ? make_float (DEFAULT_REHASH_SIZE
) : args
[i
];
4917 if (!NUMBERP (rehash_size
)
4918 || (INTEGERP (rehash_size
) && XINT (rehash_size
) <= 0)
4919 || XFLOATINT (rehash_size
) <= 1.0)
4921 list2 (build_string ("Invalid hash table rehash size"),
4924 /* Look for `:rehash-threshold THRESHOLD'. */
4925 i
= get_key_arg (QCrehash_threshold
, nargs
, args
, used
);
4926 rehash_threshold
= i
< 0 ? make_float (DEFAULT_REHASH_THRESHOLD
) : args
[i
];
4927 if (!FLOATP (rehash_threshold
)
4928 || XFLOATINT (rehash_threshold
) <= 0.0
4929 || XFLOATINT (rehash_threshold
) > 1.0)
4931 list2 (build_string ("Invalid hash table rehash threshold"),
4934 /* Look for `:weakness WEAK'. */
4935 i
= get_key_arg (QCweakness
, nargs
, args
, used
);
4936 weak
= i
< 0 ? Qnil
: args
[i
];
4938 weak
= Qkey_and_value
;
4941 && !EQ (weak
, Qvalue
)
4942 && !EQ (weak
, Qkey_or_value
)
4943 && !EQ (weak
, Qkey_and_value
))
4944 Fsignal (Qerror
, list2 (build_string ("Invalid hash table weakness"),
4947 /* Now, all args should have been used up, or there's a problem. */
4948 for (i
= 0; i
< nargs
; ++i
)
4951 list2 (build_string ("Invalid argument list"), args
[i
]));
4953 return make_hash_table (test
, size
, rehash_size
, rehash_threshold
, weak
,
4954 user_test
, user_hash
);
4958 DEFUN ("copy-hash-table", Fcopy_hash_table
, Scopy_hash_table
, 1, 1, 0,
4959 doc
: /* Return a copy of hash table TABLE. */)
4963 return copy_hash_table (check_hash_table (table
));
4967 DEFUN ("hash-table-count", Fhash_table_count
, Shash_table_count
, 1, 1, 0,
4968 doc
: /* Return the number of elements in TABLE. */)
4972 return check_hash_table (table
)->count
;
4976 DEFUN ("hash-table-rehash-size", Fhash_table_rehash_size
,
4977 Shash_table_rehash_size
, 1, 1, 0,
4978 doc
: /* Return the current rehash size of TABLE. */)
4982 return check_hash_table (table
)->rehash_size
;
4986 DEFUN ("hash-table-rehash-threshold", Fhash_table_rehash_threshold
,
4987 Shash_table_rehash_threshold
, 1, 1, 0,
4988 doc
: /* Return the current rehash threshold of TABLE. */)
4992 return check_hash_table (table
)->rehash_threshold
;
4996 DEFUN ("hash-table-size", Fhash_table_size
, Shash_table_size
, 1, 1, 0,
4997 doc
: /* Return the size of TABLE.
4998 The size can be used as an argument to `make-hash-table' to create
4999 a hash table than can hold as many elements of TABLE holds
5000 without need for resizing. */)
5004 struct Lisp_Hash_Table
*h
= check_hash_table (table
);
5005 return make_number (HASH_TABLE_SIZE (h
));
5009 DEFUN ("hash-table-test", Fhash_table_test
, Shash_table_test
, 1, 1, 0,
5010 doc
: /* Return the test TABLE uses. */)
5014 return check_hash_table (table
)->test
;
5018 DEFUN ("hash-table-weakness", Fhash_table_weakness
, Shash_table_weakness
,
5020 doc
: /* Return the weakness of TABLE. */)
5024 return check_hash_table (table
)->weak
;
5028 DEFUN ("hash-table-p", Fhash_table_p
, Shash_table_p
, 1, 1, 0,
5029 doc
: /* Return t if OBJ is a Lisp hash table object. */)
5033 return HASH_TABLE_P (obj
) ? Qt
: Qnil
;
5037 DEFUN ("clrhash", Fclrhash
, Sclrhash
, 1, 1, 0,
5038 doc
: /* Clear hash table TABLE. */)
5042 hash_clear (check_hash_table (table
));
5047 DEFUN ("gethash", Fgethash
, Sgethash
, 2, 3, 0,
5048 doc
: /* Look up KEY in TABLE and return its associated value.
5049 If KEY is not found, return DFLT which defaults to nil. */)
5051 Lisp_Object key
, table
, dflt
;
5053 struct Lisp_Hash_Table
*h
= check_hash_table (table
);
5054 int i
= hash_lookup (h
, key
, NULL
);
5055 return i
>= 0 ? HASH_VALUE (h
, i
) : dflt
;
5059 DEFUN ("puthash", Fputhash
, Sputhash
, 3, 3, 0,
5060 doc
: /* Associate KEY with VALUE in hash table TABLE.
5061 If KEY is already present in table, replace its current value with
5064 Lisp_Object key
, value
, table
;
5066 struct Lisp_Hash_Table
*h
= check_hash_table (table
);
5070 i
= hash_lookup (h
, key
, &hash
);
5072 HASH_VALUE (h
, i
) = value
;
5074 hash_put (h
, key
, value
, hash
);
5080 DEFUN ("remhash", Fremhash
, Sremhash
, 2, 2, 0,
5081 doc
: /* Remove KEY from TABLE. */)
5083 Lisp_Object key
, table
;
5085 struct Lisp_Hash_Table
*h
= check_hash_table (table
);
5086 hash_remove (h
, key
);
5091 DEFUN ("maphash", Fmaphash
, Smaphash
, 2, 2, 0,
5092 doc
: /* Call FUNCTION for all entries in hash table TABLE.
5093 FUNCTION is called with 2 arguments KEY and VALUE. */)
5095 Lisp_Object function
, table
;
5097 struct Lisp_Hash_Table
*h
= check_hash_table (table
);
5098 Lisp_Object args
[3];
5101 for (i
= 0; i
< HASH_TABLE_SIZE (h
); ++i
)
5102 if (!NILP (HASH_HASH (h
, i
)))
5105 args
[1] = HASH_KEY (h
, i
);
5106 args
[2] = HASH_VALUE (h
, i
);
5114 DEFUN ("define-hash-table-test", Fdefine_hash_table_test
,
5115 Sdefine_hash_table_test
, 3, 3, 0,
5116 doc
: /* Define a new hash table test with name NAME, a symbol.
5118 In hash tables created with NAME specified as test, use TEST to
5119 compare keys, and HASH for computing hash codes of keys.
5121 TEST must be a function taking two arguments and returning non-nil if
5122 both arguments are the same. HASH must be a function taking one
5123 argument and return an integer that is the hash code of the argument.
5124 Hash code computation should use the whole value range of integers,
5125 including negative integers. */)
5127 Lisp_Object name
, test
, hash
;
5129 return Fput (name
, Qhash_table_test
, list2 (test
, hash
));
5134 /************************************************************************
5136 ************************************************************************/
5141 DEFUN ("md5", Fmd5
, Smd5
, 1, 5, 0,
5142 doc
: /* Return MD5 message digest of OBJECT, a buffer or string.
5144 A message digest is a cryptographic checksum of a document, and the
5145 algorithm to calculate it is defined in RFC 1321.
5147 The two optional arguments START and END are character positions
5148 specifying for which part of OBJECT the message digest should be
5149 computed. If nil or omitted, the digest is computed for the whole
5152 The MD5 message digest is computed from the result of encoding the
5153 text in a coding system, not directly from the internal Emacs form of
5154 the text. The optional fourth argument CODING-SYSTEM specifies which
5155 coding system to encode the text with. It should be the same coding
5156 system that you used or will use when actually writing the text into a
5159 If CODING-SYSTEM is nil or omitted, the default depends on OBJECT. If
5160 OBJECT is a buffer, the default for CODING-SYSTEM is whatever coding
5161 system would be chosen by default for writing this text into a file.
5163 If OBJECT is a string, the most preferred coding system (see the
5164 command `prefer-coding-system') is used.
5166 If NOERROR is non-nil, silently assume the `raw-text' coding if the
5167 guesswork fails. Normally, an error is signaled in such case. */)
5168 (object
, start
, end
, coding_system
, noerror
)
5169 Lisp_Object object
, start
, end
, coding_system
, noerror
;
5171 unsigned char digest
[16];
5172 unsigned char value
[33];
5176 int start_char
= 0, end_char
= 0;
5177 int start_byte
= 0, end_byte
= 0;
5179 register struct buffer
*bp
;
5182 if (STRINGP (object
))
5184 if (NILP (coding_system
))
5186 /* Decide the coding-system to encode the data with. */
5188 if (STRING_MULTIBYTE (object
))
5189 /* use default, we can't guess correct value */
5190 coding_system
= SYMBOL_VALUE (XCAR (Vcoding_category_list
));
5192 coding_system
= Qraw_text
;
5195 if (NILP (Fcoding_system_p (coding_system
)))
5197 /* Invalid coding system. */
5199 if (!NILP (noerror
))
5200 coding_system
= Qraw_text
;
5203 Fsignal (Qcoding_system_error
, Fcons (coding_system
, Qnil
));
5206 if (STRING_MULTIBYTE (object
))
5207 object
= code_convert_string1 (object
, coding_system
, Qnil
, 1);
5209 size
= SCHARS (object
);
5210 size_byte
= SBYTES (object
);
5214 CHECK_NUMBER (start
);
5216 start_char
= XINT (start
);
5221 start_byte
= string_char_to_byte (object
, start_char
);
5227 end_byte
= size_byte
;
5233 end_char
= XINT (end
);
5238 end_byte
= string_char_to_byte (object
, end_char
);
5241 if (!(0 <= start_char
&& start_char
<= end_char
&& end_char
<= size
))
5242 args_out_of_range_3 (object
, make_number (start_char
),
5243 make_number (end_char
));
5247 CHECK_BUFFER (object
);
5249 bp
= XBUFFER (object
);
5255 CHECK_NUMBER_COERCE_MARKER (start
);
5263 CHECK_NUMBER_COERCE_MARKER (end
);
5268 temp
= b
, b
= e
, e
= temp
;
5270 if (!(BUF_BEGV (bp
) <= b
&& e
<= BUF_ZV (bp
)))
5271 args_out_of_range (start
, end
);
5273 if (NILP (coding_system
))
5275 /* Decide the coding-system to encode the data with.
5276 See fileio.c:Fwrite-region */
5278 if (!NILP (Vcoding_system_for_write
))
5279 coding_system
= Vcoding_system_for_write
;
5282 int force_raw_text
= 0;
5284 coding_system
= XBUFFER (object
)->buffer_file_coding_system
;
5285 if (NILP (coding_system
)
5286 || NILP (Flocal_variable_p (Qbuffer_file_coding_system
, Qnil
)))
5288 coding_system
= Qnil
;
5289 if (NILP (current_buffer
->enable_multibyte_characters
))
5293 if (NILP (coding_system
) && !NILP (Fbuffer_file_name(object
)))
5295 /* Check file-coding-system-alist. */
5296 Lisp_Object args
[4], val
;
5298 args
[0] = Qwrite_region
; args
[1] = start
; args
[2] = end
;
5299 args
[3] = Fbuffer_file_name(object
);
5300 val
= Ffind_operation_coding_system (4, args
);
5301 if (CONSP (val
) && !NILP (XCDR (val
)))
5302 coding_system
= XCDR (val
);
5305 if (NILP (coding_system
)
5306 && !NILP (XBUFFER (object
)->buffer_file_coding_system
))
5308 /* If we still have not decided a coding system, use the
5309 default value of buffer-file-coding-system. */
5310 coding_system
= XBUFFER (object
)->buffer_file_coding_system
;
5314 && !NILP (Ffboundp (Vselect_safe_coding_system_function
)))
5315 /* Confirm that VAL can surely encode the current region. */
5316 coding_system
= call4 (Vselect_safe_coding_system_function
,
5317 make_number (b
), make_number (e
),
5318 coding_system
, Qnil
);
5321 coding_system
= Qraw_text
;
5324 if (NILP (Fcoding_system_p (coding_system
)))
5326 /* Invalid coding system. */
5328 if (!NILP (noerror
))
5329 coding_system
= Qraw_text
;
5332 Fsignal (Qcoding_system_error
, Fcons (coding_system
, Qnil
));
5336 object
= make_buffer_string (b
, e
, 0);
5338 if (STRING_MULTIBYTE (object
))
5339 object
= code_convert_string1 (object
, coding_system
, Qnil
, 1);
5342 md5_buffer (SDATA (object
) + start_byte
,
5343 SBYTES (object
) - (size_byte
- end_byte
),
5346 for (i
= 0; i
< 16; i
++)
5347 sprintf (&value
[2 * i
], "%02x", digest
[i
]);
5350 return make_string (value
, 32);
5357 /* Hash table stuff. */
5358 Qhash_table_p
= intern ("hash-table-p");
5359 staticpro (&Qhash_table_p
);
5360 Qeq
= intern ("eq");
5362 Qeql
= intern ("eql");
5364 Qequal
= intern ("equal");
5365 staticpro (&Qequal
);
5366 QCtest
= intern (":test");
5367 staticpro (&QCtest
);
5368 QCsize
= intern (":size");
5369 staticpro (&QCsize
);
5370 QCrehash_size
= intern (":rehash-size");
5371 staticpro (&QCrehash_size
);
5372 QCrehash_threshold
= intern (":rehash-threshold");
5373 staticpro (&QCrehash_threshold
);
5374 QCweakness
= intern (":weakness");
5375 staticpro (&QCweakness
);
5376 Qkey
= intern ("key");
5378 Qvalue
= intern ("value");
5379 staticpro (&Qvalue
);
5380 Qhash_table_test
= intern ("hash-table-test");
5381 staticpro (&Qhash_table_test
);
5382 Qkey_or_value
= intern ("key-or-value");
5383 staticpro (&Qkey_or_value
);
5384 Qkey_and_value
= intern ("key-and-value");
5385 staticpro (&Qkey_and_value
);
5388 defsubr (&Smake_hash_table
);
5389 defsubr (&Scopy_hash_table
);
5390 defsubr (&Shash_table_count
);
5391 defsubr (&Shash_table_rehash_size
);
5392 defsubr (&Shash_table_rehash_threshold
);
5393 defsubr (&Shash_table_size
);
5394 defsubr (&Shash_table_test
);
5395 defsubr (&Shash_table_weakness
);
5396 defsubr (&Shash_table_p
);
5397 defsubr (&Sclrhash
);
5398 defsubr (&Sgethash
);
5399 defsubr (&Sputhash
);
5400 defsubr (&Sremhash
);
5401 defsubr (&Smaphash
);
5402 defsubr (&Sdefine_hash_table_test
);
5404 Qstring_lessp
= intern ("string-lessp");
5405 staticpro (&Qstring_lessp
);
5406 Qprovide
= intern ("provide");
5407 staticpro (&Qprovide
);
5408 Qrequire
= intern ("require");
5409 staticpro (&Qrequire
);
5410 Qyes_or_no_p_history
= intern ("yes-or-no-p-history");
5411 staticpro (&Qyes_or_no_p_history
);
5412 Qcursor_in_echo_area
= intern ("cursor-in-echo-area");
5413 staticpro (&Qcursor_in_echo_area
);
5414 Qwidget_type
= intern ("widget-type");
5415 staticpro (&Qwidget_type
);
5417 staticpro (&string_char_byte_cache_string
);
5418 string_char_byte_cache_string
= Qnil
;
5420 require_nesting_list
= Qnil
;
5421 staticpro (&require_nesting_list
);
5423 Fset (Qyes_or_no_p_history
, Qnil
);
5425 DEFVAR_LISP ("features", &Vfeatures
,
5426 doc
: /* A list of symbols which are the features of the executing emacs.
5427 Used by `featurep' and `require', and altered by `provide'. */);
5429 Qsubfeatures
= intern ("subfeatures");
5430 staticpro (&Qsubfeatures
);
5432 DEFVAR_BOOL ("use-dialog-box", &use_dialog_box
,
5433 doc
: /* *Non-nil means mouse commands use dialog boxes to ask questions.
5434 This applies to `y-or-n-p' and `yes-or-no-p' questions asked by commands
5435 invoked by mouse clicks and mouse menu items. */);
5438 defsubr (&Sidentity
);
5441 defsubr (&Ssafe_length
);
5442 defsubr (&Sstring_bytes
);
5443 defsubr (&Sstring_equal
);
5444 defsubr (&Scompare_strings
);
5445 defsubr (&Sstring_lessp
);
5448 defsubr (&Svconcat
);
5449 defsubr (&Scopy_sequence
);
5450 defsubr (&Sstring_make_multibyte
);
5451 defsubr (&Sstring_make_unibyte
);
5452 defsubr (&Sstring_as_multibyte
);
5453 defsubr (&Sstring_as_unibyte
);
5454 defsubr (&Scopy_alist
);
5455 defsubr (&Ssubstring
);
5456 defsubr (&Ssubstring_no_properties
);
5468 defsubr (&Snreverse
);
5469 defsubr (&Sreverse
);
5471 defsubr (&Splist_get
);
5473 defsubr (&Splist_put
);
5475 defsubr (&Slax_plist_get
);
5476 defsubr (&Slax_plist_put
);
5478 defsubr (&Sfillarray
);
5479 defsubr (&Schar_table_subtype
);
5480 defsubr (&Schar_table_parent
);
5481 defsubr (&Sset_char_table_parent
);
5482 defsubr (&Schar_table_extra_slot
);
5483 defsubr (&Sset_char_table_extra_slot
);
5484 defsubr (&Schar_table_range
);
5485 defsubr (&Sset_char_table_range
);
5486 defsubr (&Sset_char_table_default
);
5487 defsubr (&Soptimize_char_table
);
5488 defsubr (&Smap_char_table
);
5492 defsubr (&Smapconcat
);
5493 defsubr (&Sy_or_n_p
);
5494 defsubr (&Syes_or_no_p
);
5495 defsubr (&Sload_average
);
5496 defsubr (&Sfeaturep
);
5497 defsubr (&Srequire
);
5498 defsubr (&Sprovide
);
5499 defsubr (&Splist_member
);
5500 defsubr (&Swidget_put
);
5501 defsubr (&Swidget_get
);
5502 defsubr (&Swidget_apply
);
5503 defsubr (&Sbase64_encode_region
);
5504 defsubr (&Sbase64_decode_region
);
5505 defsubr (&Sbase64_encode_string
);
5506 defsubr (&Sbase64_decode_string
);
5514 Vweak_hash_tables
= Qnil
;