(Qforeground_color, Qbackground_color): Declare.
[bpt/emacs.git] / src / fns.c
index dabd05a..5d5f3fc 100644 (file)
--- a/src/fns.c
+++ b/src/fns.c
@@ -21,6 +21,10 @@ Boston, MA 02111-1307, USA.  */
 
 #include <config.h>
 
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
 /* Note on some machines this defines `vector' as a typedef,
    so make sure we don't use that name in this file.  */
 #undef vector
@@ -35,19 +39,18 @@ Boston, MA 02111-1307, USA.  */
 #include "intervals.h"
 #include "frame.h"
 #include "window.h"
+#ifdef HAVE_MENUS
+#include "xterm.h"
+#endif
 
 #ifndef NULL
 #define NULL (void *)0
 #endif
 
-#define DEFAULT_NONASCII_INSERT_OFFSET 0x800
-
 /* Nonzero enables use of dialog boxes for questions
    asked by mouse commands.  */
 int use_dialog_box;
 
-extern Lisp_Object Flookup_key ();
-
 extern int minibuffer_auto_raise;
 extern Lisp_Object minibuf_window;
 
@@ -57,6 +60,13 @@ Lisp_Object Qcursor_in_echo_area;
 Lisp_Object Qwidget_type;
 
 static int internal_equal ();
+
+extern long get_random ();
+extern void seed_random ();
+
+#ifndef HAVE_UNISTD_H
+extern long time ();
+#endif
 \f
 DEFUN ("identity", Fidentity, Sidentity, 1, 1, 0,
   "Return the argument unchanged.")
@@ -66,10 +76,6 @@ DEFUN ("identity", Fidentity, Sidentity, 1, 1, 0,
   return arg;
 }
 
-extern long get_random ();
-extern void seed_random ();
-extern long time ();
-
 DEFUN ("random", Frandom, Srandom, 0, 1, 0,
   "Return a pseudo-random number.\n\
 All integers representable in Lisp are equally likely.\n\
@@ -111,8 +117,8 @@ DEFUN ("length", Flength, Slength, 1, 1, 0,
   "Return the length of vector, list or string SEQUENCE.\n\
 A byte-code function object is also allowed.\n\
 If the string contains multibyte characters, this is not the necessarily\n\
-the number of characters in the string; it is the number of bytes.\n\
-To get the number of characters, use `chars-in-string'")
+the number of bytes in the string; it is the number of characters.\n\
+To get the number of bytes, use `string-bytes'")
   (sequence)
      register Lisp_Object sequence;
 {
@@ -125,7 +131,9 @@ To get the number of characters, use `chars-in-string'")
   else if (VECTORP (sequence))
     XSETFASTINT (val, XVECTOR (sequence)->size);
   else if (CHAR_TABLE_P (sequence))
-    XSETFASTINT (val, CHAR_TABLE_ORDINARY_SLOTS);
+    XSETFASTINT (val, (MIN_CHAR_COMPOSITION
+                      + (CHAR_FIELD2_MASK | CHAR_FIELD3_MASK)
+                      - 1));
   else if (BOOL_VECTOR_P (sequence))
     XSETFASTINT (val, XBOOL_VECTOR (sequence)->size);
   else if (COMPILEDP (sequence))
@@ -179,6 +187,16 @@ which is at least the number of distinct elements.")
   return length;
 }
 
+DEFUN ("string-bytes", Fstring_bytes, Sstring_bytes, 1, 1, 0,
+  "Return the number of bytes in STRING.\n\
+If STRING is a multibyte string, this is greater than the length of STRING.")
+  (string)
+     Lisp_Object string;
+{
+  CHECK_STRING (string, 1);
+  return make_number (STRING_BYTES (XSTRING (string)));
+}
+
 DEFUN ("string-equal", Fstring_equal, Sstring_equal, 2, 2, 0,
   "Return t if two strings have identical contents.\n\
 Case is significant, but text properties are ignored.\n\
@@ -194,8 +212,8 @@ Symbols are also allowed; their print names are used instead.")
   CHECK_STRING (s2, 1);
 
   if (XSTRING (s1)->size != XSTRING (s2)->size
-      || XSTRING (s1)->size_byte != XSTRING (s2)->size_byte
-      || bcmp (XSTRING (s1)->data, XSTRING (s2)->data, XSTRING (s1)->size_byte))
+      || STRING_BYTES (XSTRING (s1)) != STRING_BYTES (XSTRING (s2))
+      || bcmp (XSTRING (s1)->data, XSTRING (s2)->data, STRING_BYTES (XSTRING (s1))))
     return Qnil;
   return Qt;
 }
@@ -460,6 +478,8 @@ concat (nargs, args, target_type, last_special)
                if (this_len_byte > 1)
                  some_multibyte = 1;
              }
+         else if (BOOL_VECTOR_P (this) && XBOOL_VECTOR (this)->size > 0)
+           wrong_type_argument (Qintegerp, Faref (this, make_number (0)));
          else if (CONSP (this))
            for (; CONSP (this); this = XCONS (this)->cdr)
              {
@@ -476,7 +496,7 @@ concat (nargs, args, target_type, last_special)
              if (STRING_MULTIBYTE (this))
                {
                  some_multibyte = 1;
-                 result_len_byte += XSTRING (this)->size_byte;
+                 result_len_byte += STRING_BYTES (XSTRING (this));
                }
              else
                result_len_byte += count_size_as_multibyte (XSTRING (this)->data,
@@ -495,8 +515,10 @@ concat (nargs, args, target_type, last_special)
     val = Fmake_list (make_number (result_len), Qnil);
   else if (target_type == Lisp_Vectorlike)
     val = Fmake_vector (make_number (result_len), Qnil);
-  else
+  else if (some_multibyte)
     val = make_uninit_multibyte_string (result_len, result_len_byte);
+  else
+    val = make_uninit_string (result_len);
 
   /* In `append', if all but last arg are nil, return last arg.  */
   if (target_type == Lisp_Cons && EQ (val, Qnil))
@@ -530,9 +552,9 @@ concat (nargs, args, target_type, last_special)
       if (STRINGP (this) && STRINGP (val)
          && STRING_MULTIBYTE (this) == some_multibyte)
        {
-         int thislen_byte = XSTRING (this)->size_byte;
+         int thislen_byte = STRING_BYTES (XSTRING (this));
          bcopy (XSTRING (this)->data, XSTRING (val)->data + toindex_byte,
-                XSTRING (this)->size_byte);
+                STRING_BYTES (XSTRING (this)));
          toindex_byte += thislen_byte;
          toindex += thisleni;
        }
@@ -555,43 +577,41 @@ concat (nargs, args, target_type, last_special)
            if (NILP (this)) break;
            if (CONSP (this))
              elt = XCONS (this)->car, this = XCONS (this)->cdr;
-           else
+           else if (thisindex >= thisleni)
+             break;
+           else if (STRINGP (this))
              {
-               if (thisindex >= thisleni) break;
-               if (STRINGP (this))
+               int c;
+               if (STRING_MULTIBYTE (this))
                  {
-                   if (STRING_MULTIBYTE (this))
-                     {
-                       int c;
-                       FETCH_STRING_CHAR_ADVANCE (c, this,
-                                                  thisindex,
-                                                  thisindex_byte);
-                       XSETFASTINT (elt, c);
-                     }
-                   else
-                     {
-                       unsigned char c;
-                       XSETFASTINT (elt, XSTRING (this)->data[thisindex++]);
-                       if (some_multibyte)
-                         XSETINT (elt,
-                                  unibyte_char_to_multibyte (XINT (elt)));
-                     }
+                   FETCH_STRING_CHAR_ADVANCE (c, this,
+                                              thisindex,
+                                              thisindex_byte);
+                   XSETFASTINT (elt, c);
                  }
-               else if (BOOL_VECTOR_P (this))
+               else
                  {
-                   int size_in_chars
-                     = ((XBOOL_VECTOR (this)->size + BITS_PER_CHAR - 1)
-                        / BITS_PER_CHAR);
-                   int byte;
-                   byte = XBOOL_VECTOR (val)->data[thisindex / BITS_PER_CHAR];
-                   if (byte & (1 << (thisindex % BITS_PER_CHAR)))
-                     elt = Qt;
-                   else
-                     elt = Qnil;
+                   XSETFASTINT (elt, XSTRING (this)->data[thisindex++]);
+                   if (some_multibyte && XINT (elt) >= 0200
+                       && XINT (elt) < 0400)
+                     {
+                       c = unibyte_char_to_multibyte (XINT (elt));
+                       XSETINT (elt, c);
+                     }
                  }
+             }
+           else if (BOOL_VECTOR_P (this))
+             {
+               int byte;
+               byte = XBOOL_VECTOR (this)->data[thisindex / BITS_PER_CHAR];
+               if (byte & (1 << (thisindex % BITS_PER_CHAR)))
+                 elt = Qt;
                else
-                 elt = XVECTOR (this)->contents[thisindex++];
+                 elt = Qnil;
+               thisindex++;
              }
+           else
+             elt = XVECTOR (this)->contents[thisindex++];
 
            /* Store this element into the result.  */
            if (toindex < 0)
@@ -654,7 +674,7 @@ string_char_to_byte (string, char_index)
 
   best_below = best_below_byte = 0;
   best_above = XSTRING (string)->size;
-  best_above_byte = XSTRING (string)->size_byte;
+  best_above_byte = STRING_BYTES (XSTRING (string));
 
   if (EQ (string, string_char_byte_cache_string))
     {
@@ -720,7 +740,7 @@ string_byte_to_char (string, byte_index)
 
   best_below = best_below_byte = 0;
   best_above = XSTRING (string)->size;
-  best_above_byte = XSTRING (string)->size_byte;
+  best_above_byte = STRING_BYTES (XSTRING (string));
 
   if (EQ (string, string_char_byte_cache_string))
     {
@@ -771,7 +791,7 @@ string_byte_to_char (string, byte_index)
 }
 \f
 /* Convert STRING to a multibyte string.
-   Single-byte characters 0200 through 0377 are converted
+   Single-byte characters 0240 through 0377 are converted
    by adding nonascii_insert_offset to each.  */
 
 Lisp_Object
@@ -786,8 +806,13 @@ string_make_multibyte (string)
 
   nbytes = count_size_as_multibyte (XSTRING (string)->data,
                                    XSTRING (string)->size);
+  /* If all the chars are ASCII, they won't need any more bytes
+     once converted.  In that case, we can return STRING itself.  */
+  if (nbytes == STRING_BYTES (XSTRING (string)))
+    return string;
+
   buf = (unsigned char *) alloca (nbytes);
-  copy_text (XSTRING (string)->data, buf, XSTRING (string)->size_byte,
+  copy_text (XSTRING (string)->data, buf, STRING_BYTES (XSTRING (string)),
             0, 1);
 
   return make_multibyte_string (buf, XSTRING (string)->size, nbytes);
@@ -806,7 +831,7 @@ string_make_unibyte (string)
 
   buf = (unsigned char *) alloca (XSTRING (string)->size);
 
-  copy_text (XSTRING (string)->data, buf, XSTRING (string)->size_byte,
+  copy_text (XSTRING (string)->data, buf, STRING_BYTES (XSTRING (string)),
             1, 0);
 
   return make_unibyte_string (buf, XSTRING (string)->size);
@@ -829,6 +854,41 @@ DEFUN ("string-make-unibyte", Fstring_make_unibyte, Sstring_make_unibyte,
 {
   return string_make_unibyte (string);
 }
+
+DEFUN ("string-as-unibyte", Fstring_as_unibyte, Sstring_as_unibyte,
+       1, 1, 0,
+  "Return a unibyte string with the same individual bytes as STRING.\n\
+If STRING is unibyte, the result is STRING itself.")
+  (string)
+     Lisp_Object string;
+{
+  if (STRING_MULTIBYTE (string))
+    {
+      string = Fcopy_sequence (string);
+      XSTRING (string)->size = STRING_BYTES (XSTRING (string));
+      SET_STRING_BYTES (XSTRING (string), -1);
+    }
+  return string;
+}
+
+DEFUN ("string-as-multibyte", Fstring_as_multibyte, Sstring_as_multibyte,
+       1, 1, 0,
+  "Return a multibyte string with the same individual bytes as STRING.\n\
+If STRING is multibyte, the result is STRING itself.")
+  (string)
+     Lisp_Object string;
+{
+  if (! STRING_MULTIBYTE (string))
+    {
+      int nbytes = STRING_BYTES (XSTRING (string));
+      int newlen = multibyte_chars_in_text (XSTRING (string)->data, nbytes);
+
+      string = Fcopy_sequence (string);
+      XSTRING (string)->size = newlen;
+      XSTRING (string)->size_byte = nbytes;
+    }
+  return string;
+}
 \f
 DEFUN ("copy-alist", Fcopy_alist, Scopy_alist, 1, 1, 0,
   "Return a copy of ALIST.\n\
@@ -881,7 +941,7 @@ This function allows vectors as well as strings.")
   if (STRINGP (string))
     {
       size = XSTRING (string)->size;
-      size_byte = XSTRING (string)->size_byte;
+      size_byte = STRING_BYTES (XSTRING (string));
     }
   else
     size = XVECTOR (string)->size;
@@ -915,10 +975,11 @@ This function allows vectors as well as strings.")
 
   if (STRINGP (string))
     {
-      res = make_multibyte_string (XSTRING (string)->data + from_byte,
-                                  to_char - from_char, to_byte - from_byte);
-      copy_text_properties (from_char, to_char, string,
-                           make_number (0), res, Qnil);
+      res = make_specified_string (XSTRING (string)->data + from_byte,
+                                  to_char - from_char, to_byte - from_byte,
+                                  STRING_MULTIBYTE (string));
+      copy_text_properties (make_number (from_char), make_number (to_char),
+                           string, make_number (0), res, Qnil);
     }
   else
     res = Fvector (to_char - from_char,
@@ -945,7 +1006,7 @@ substring_both (string, from, from_byte, to, to_byte)
   if (STRINGP (string))
     {
       size = XSTRING (string)->size;
-      size_byte = XSTRING (string)->size_byte;
+      size_byte = STRING_BYTES (XSTRING (string));
     }
   else
     size = XVECTOR (string)->size;
@@ -955,9 +1016,11 @@ substring_both (string, from, from_byte, to, to_byte)
 
   if (STRINGP (string))
     {
-      res = make_multibyte_string (XSTRING (string)->data + from_byte,
-                                  to - from, to_byte - from_byte);
-      copy_text_properties (from, to, string, make_number (0), res, Qnil);
+      res = make_specified_string (XSTRING (string)->data + from_byte,
+                                  to - from, to_byte - from_byte,
+                                  STRING_MULTIBYTE (string));
+      copy_text_properties (make_number (from), make_number (to),
+                           string, make_number (0), res, Qnil);
     }
   else
     res = Fvector (to - from,
@@ -1510,6 +1573,8 @@ internal_equal (o1, o2, depth)
              return 0;
            return 1;
          }
+       if (WINDOW_CONFIGURATIONP (o1))
+         return compare_window_configurations (o1, o2, 0);
 
        /* Aside from them, only true vectors, char-tables, and compiled
           functions are sensible to compare, so eliminate the others now.  */
@@ -1534,10 +1599,10 @@ internal_equal (o1, o2, depth)
     case Lisp_String:
       if (XSTRING (o1)->size != XSTRING (o2)->size)
        return 0;
-      if (XSTRING (o1)->size_byte != XSTRING (o2)->size_byte)
+      if (STRING_BYTES (XSTRING (o1)) != STRING_BYTES (XSTRING (o2)))
        return 0;
       if (bcmp (XSTRING (o1)->data, XSTRING (o2)->data,
-               XSTRING (o1)->size_byte))
+               STRING_BYTES (XSTRING (o1))))
        return 0;
       return 1;
     }
@@ -1681,9 +1746,9 @@ DEFUN ("set-char-table-extra-slot", Fset_char_table_extra_slot,
 DEFUN ("char-table-range", Fchar_table_range, Schar_table_range,
        2, 2, 0,
   "Return the value in CHAR-TABLE for a range of characters RANGE.\n\
-RANGE should be t (for all characters), nil (for the default value)\n\
+RANGE should be nil (for the default value)\n\
 a vector which identifies a character set or a row of a character set,\n\
-or a character code.")
+a character set name, or a character code.")
   (char_table, range)
      Lisp_Object char_table, range;
 {
@@ -1695,10 +1760,22 @@ or a character code.")
     return XCHAR_TABLE (char_table)->defalt;
   else if (INTEGERP (range))
     return Faref (char_table, range);
+  else if (SYMBOLP (range))
+    {
+      Lisp_Object charset_info;
+
+      charset_info = Fget (range, Qcharset);
+      CHECK_VECTOR (charset_info, 0);
+
+      return Faref (char_table,
+                   make_number (XINT (XVECTOR (charset_info)->contents[0])
+                                + 128));
+    }
   else if (VECTORP (range))
     {
       if (XVECTOR (range)->size == 1)
-       return Faref (char_table, XVECTOR (range)->contents[0]);
+       return Faref (char_table,
+                     make_number (XINT (XVECTOR (range)->contents[0]) + 128));
       else
        {
          int size = XVECTOR (range)->size;
@@ -1718,7 +1795,7 @@ DEFUN ("set-char-table-range", Fset_char_table_range, Sset_char_table_range,
   "Set the value in CHAR-TABLE for a range of characters RANGE to VALUE.\n\
 RANGE should be t (for all characters), nil (for the default value)\n\
 a vector which identifies a character set or a row of a character set,\n\
-or a character code.")
+a coding system, or a character code.")
   (char_table, range, value)
      Lisp_Object char_table, range, value;
 {
@@ -1731,12 +1808,26 @@ or a character code.")
       XCHAR_TABLE (char_table)->contents[i] = value;
   else if (EQ (range, Qnil))
     XCHAR_TABLE (char_table)->defalt = value;
+  else if (SYMBOLP (range))
+    {
+      Lisp_Object charset_info;
+
+      charset_info = Fget (range, Qcharset);
+      CHECK_VECTOR (charset_info, 0);
+
+      return Faset (char_table,
+                   make_number (XINT (XVECTOR (charset_info)->contents[0])
+                                + 128),
+                   value);
+    }
   else if (INTEGERP (range))
     Faset (char_table, range, value);
   else if (VECTORP (range))
     {
       if (XVECTOR (range)->size == 1)
-       return Faset (char_table, XVECTOR (range)->contents[0], value);
+       return Faset (char_table,
+                     make_number (XINT (XVECTOR (range)->contents[0]) + 128),
+                     value);
       else
        {
          int size = XVECTOR (range)->size;
@@ -1770,7 +1861,7 @@ See also the documentation of make-char.")
   c = XINT (ch);
   SPLIT_NON_ASCII_CHAR (c, charset, code1, code2);
   if (! CHARSET_DEFINED_P (charset))
-    error ("Invalid character: %d", c);
+    invalid_character (c);
 
   if (charset == CHARSET_ASCII)
     return (XCHAR_TABLE (char_table)->defalt = value);
@@ -1799,6 +1890,23 @@ See also the documentation of make-char.")
     XCHAR_TABLE (char_table)->contents[code1] = value;
   return value;
 }
+
+/* Look up the element in TABLE at index CH,
+   and return it as an integer.
+   If the element is nil, return CH itself.
+   (Actually we do that for any non-integer.)  */
+
+int
+char_table_translate (table, ch)
+     Lisp_Object table;
+     int ch;
+{
+  Lisp_Object value;
+  value = Faref (table, make_number (ch));
+  if (! INTEGERP (value))
+    return ch;
+  return XINT (value);
+}
 \f
 /* Map C_FUNCTION or FUNCTION over SUBTABLE, calling it for each
    character or group of characters that share a value.
@@ -1977,6 +2085,20 @@ mapcar1 (leni, vals, fn, seq)
          vals[i] = call1 (fn, dummy);
        }
     }
+  else if (BOOL_VECTOR_P (seq))
+    {
+      for (i = 0; i < leni; i++)
+       {
+         int byte;
+         byte = XBOOL_VECTOR (seq)->data[i / BITS_PER_CHAR];
+         if (byte & (1 << (i % BITS_PER_CHAR)))
+           dummy = Qt;
+         else
+           dummy = Qnil;
+
+         vals[i] = call1 (fn, dummy);
+       }
+    }
   else if (STRINGP (seq) && ! STRING_MULTIBYTE (seq))
     {
       /* Single-byte string.  */
@@ -1989,15 +2111,17 @@ mapcar1 (leni, vals, fn, seq)
   else if (STRINGP (seq))
     {
       /* Multi-byte string.  */
-      int len_byte = XSTRING (seq)->size_byte;
+      int len_byte = STRING_BYTES (XSTRING (seq));
       int i_byte;
 
       for (i = 0, i_byte = 0; i < leni;)
        {
          int c;
-         FETCH_STRING_CHAR_ADVANCE (c, seq, i, i_byte)
+         int i_before = i;
+
+         FETCH_STRING_CHAR_ADVANCE (c, seq, i, i_byte);
          XSETFASTINT (dummy, c);
-         vals[i] = call1 (fn, dummy);
+         vals[i_before] = call1 (fn, dummy);
        }
     }
   else   /* Must be a list, since Flength did not get an error */
@@ -2016,7 +2140,8 @@ mapcar1 (leni, vals, fn, seq)
 DEFUN ("mapconcat", Fmapconcat, Smapconcat, 3, 3, 0,
   "Apply FUNCTION to each element of SEQUENCE, and concat the results as strings.\n\
 In between each pair of results, stick in SEPARATOR.  Thus, \" \" as\n\
-SEPARATOR results in spaces between the values returned by FUNCTION.")
+SEPARATOR results in spaces between the values returned by FUNCTION.\n\
+SEQUENCE may be a list, a vector, a bool-vector, or a string.")
   (function, sequence, separator)
      Lisp_Object function, sequence, separator;
 {
@@ -2050,7 +2175,7 @@ SEPARATOR results in spaces between the values returned by FUNCTION.")
 DEFUN ("mapcar", Fmapcar, Smapcar, 2, 2, 0,
   "Apply FUNCTION to each element of SEQUENCE, and make a list of the results.\n\
 The result is a list just as long as SEQUENCE.\n\
-SEQUENCE may be a list, a vector or a string.")
+SEQUENCE may be a list, a vector, a bool-vector, or a string.")
   (function, sequence)
      Lisp_Object function, sequence;
 {
@@ -2321,7 +2446,8 @@ DEFUN ("require", Frequire, Srequire, 1, 2, 0,
   "If feature FEATURE is not loaded, load it from FILENAME.\n\
 If FEATURE is not a member of the list `features', then the feature\n\
 is not loaded; so load the file FILENAME.\n\
-If FILENAME is omitted, the printname of FEATURE is used as the file name.")
+If FILENAME is omitted, the printname of FEATURE is used as the file name,\n\
+but in this case `load' insists on adding the suffix `.el' or `.elc'.")
   (feature, file_name)
      Lisp_Object feature, file_name;
 {
@@ -2436,6 +2562,7 @@ ARGS are passed as extra arguments to the function.")
   return result;
 }
 \f
+void
 syms_of_fns ()
 {
   Qstring_lessp = intern ("string-lessp");
@@ -2471,6 +2598,7 @@ invoked by mouse clicks and mouse menu items.");
   defsubr (&Srandom);
   defsubr (&Slength);
   defsubr (&Ssafe_length);
+  defsubr (&Sstring_bytes);
   defsubr (&Sstring_equal);
   defsubr (&Sstring_lessp);
   defsubr (&Sappend);
@@ -2479,6 +2607,8 @@ invoked by mouse clicks and mouse menu items.");
   defsubr (&Scopy_sequence);
   defsubr (&Sstring_make_multibyte);
   defsubr (&Sstring_make_unibyte);
+  defsubr (&Sstring_as_multibyte);
+  defsubr (&Sstring_as_unibyte);
   defsubr (&Scopy_alist);
   defsubr (&Ssubstring);
   defsubr (&Snthcdr);