Do not include stdlib.h and string.h, config.h does it.
[bpt/emacs.git] / src / charset.c
index 84df00d..0ab776b 100644 (file)
@@ -127,13 +127,6 @@ int inhibit_load_charset_map;
 
 Lisp_Object Vcurrent_iso639_language;
 
-/* Defined in chartab.c */
-extern void
-map_char_table_for_charset (void (*c_function) (Lisp_Object, Lisp_Object),
-                            Lisp_Object function, Lisp_Object table,
-                            Lisp_Object arg, struct charset *charset,
-                            unsigned from, unsigned to);
-
 #define CODE_POINT_TO_INDEX(charset, code)                             \
   ((charset)->code_linear_p                                            \
    ? (code) - (charset)->min_code                                      \
@@ -433,7 +426,7 @@ load_charset_map (struct charset *charset, struct charset_map_entries *entries,
 
 
 /* Read a hexadecimal number (preceded by "0x") from the file FP while
-   paying attention to comment charcter '#'.  */
+   paying attention to comment character '#'.  */
 
 static INLINE unsigned
 read_hex (FILE *fp, int *eof)
@@ -472,8 +465,6 @@ read_hex (FILE *fp, int *eof)
   return n;
 }
 
-extern Lisp_Object Qfile_name_handler_alist;
-
 /* Return a mapping vector for CHARSET loaded from MAPFILE.
    Each line of MAPFILE has this form
        0xAAAA 0xCCCC
@@ -491,8 +482,6 @@ extern Lisp_Object Qfile_name_handler_alist;
    Note that this function uses `openp' to open MAPFILE but ignores
    `file-name-handler-alist' to avoid running any Lisp code.  */
 
-extern void add_to_log (char *, Lisp_Object, Lisp_Object);
-
 static void
 load_charset_map_from_file (struct charset *charset, Lisp_Object mapfile, int control_flag)
 {
@@ -1276,7 +1265,7 @@ usage: (define-charset-internal ...)  */)
 static int
 define_charset_internal (Lisp_Object name,
                         int dimension,
-                        unsigned char *code_space,
+                        const unsigned char *code_space,
                         unsigned min_code, unsigned max_code,
                         int iso_final, int iso_revision, int emacs_mule_id,
                         int ascii_compatible, int supplementary,
@@ -2082,23 +2071,22 @@ that case, find the charset from what supported by that coding system.  */)
     charset = CHAR_CHARSET (XINT (ch));
   else
     {
-      Lisp_Object charset_list;
-
       if (CONSP (restriction))
        {
-         for (charset_list = Qnil; CONSP (restriction);
-              restriction = XCDR (restriction))
+         int c = XFASTINT (ch);
+
+         for (; CONSP (restriction); restriction = XCDR (restriction))
            {
-             int id;
+             struct charset *charset;
 
-             CHECK_CHARSET_GET_ID (XCAR (restriction), id);
-             charset_list = Fcons (make_number (id), charset_list);
+             CHECK_CHARSET_GET_CHARSET (XCAR (restriction), charset);
+             if (ENCODE_CHAR (charset, c) != CHARSET_INVALID_CODE (charset))
+               return XCAR (restriction);
            }
-         charset_list = Fnreverse (charset_list);
+         return Qnil;
        }
-      else
-       charset_list = coding_system_charset_list (restriction);
-      charset = char_charset (XINT (ch), charset_list, NULL);
+      restriction = coding_system_charset_list (restriction);
+      charset = char_charset (XINT (ch), restriction, NULL);
       if (! charset)
        return Qnil;
     }
@@ -2249,6 +2237,69 @@ Return charset identification number of CHARSET.  */)
   return make_number (id);
 }
 
+struct charset_sort_data
+{
+  Lisp_Object charset;
+  int id;
+  int priority;
+};
+
+static int
+charset_compare (const void *d1, const void *d2)
+{
+  const struct charset_sort_data *data1 = d1, *data2 = d2;
+  return (data1->priority - data2->priority);
+}
+
+DEFUN ("sort-charsets", Fsort_charsets, Ssort_charsets, 1, 1, 0,
+       doc: /* Sort charset list CHARSETS by a priority of each charset.
+Return the sorted list.  CHARSETS is modified by side effects.
+See also `charset-priority-list' and `set-charset-priority'.  */)
+     (Lisp_Object charsets)
+{
+  Lisp_Object len = Flength (charsets);
+  int n = XFASTINT (len), i, j, done;
+  Lisp_Object tail, elt, attrs;
+  struct charset_sort_data *sort_data;
+  int id, min_id, max_id;
+  USE_SAFE_ALLOCA;
+
+  if (n == 0)
+    return Qnil;
+  SAFE_ALLOCA (sort_data, struct charset_sort_data *, sizeof (*sort_data) * n);
+  for (tail = charsets, i = 0; CONSP (tail); tail = XCDR (tail), i++)
+    {
+      elt = XCAR (tail);
+      CHECK_CHARSET_GET_ATTR (elt, attrs);
+      sort_data[i].charset = elt;
+      sort_data[i].id = id = XINT (CHARSET_ATTR_ID (attrs));
+      if (i == 0)
+       min_id = max_id = id;
+      else if (id < min_id)
+       min_id = id;
+      else if (id > max_id)
+       max_id = id;
+    }
+  for (done = 0, tail = Vcharset_ordered_list, i = 0;
+       done < n && CONSP (tail); tail = XCDR (tail), i++)
+    {
+      elt = XCAR (tail);
+      id = XFASTINT (elt);
+      if (id >= min_id && id <= max_id)
+       for (j = 0; j < n; j++)
+         if (sort_data[j].id == id)
+           {
+             sort_data[j].priority = i;
+             done++;
+           }
+    }
+  qsort (sort_data, n, sizeof *sort_data, charset_compare);
+  for (i = 0, tail = charsets; CONSP (tail); tail = XCDR (tail), i++)
+    XSETCAR (tail, sort_data[i].charset);
+  SAFE_FREE ();
+  return charsets;
+}
+
 \f
 void
 init_charset (void)
@@ -2313,8 +2364,8 @@ syms_of_charset (void)
   Vemacs_mule_charset_list = Qnil;
 
   /* Don't staticpro them here.  It's done in syms_of_fns.  */
-  QCtest = intern (":test");
-  Qeq = intern ("eq");
+  QCtest = intern_c_string (":test");
+  Qeq = intern_c_string ("eq");
 
   staticpro (&Vcharset_hash_table);
   {
@@ -2351,6 +2402,7 @@ syms_of_charset (void)
   defsubr (&Scharset_priority_list);
   defsubr (&Sset_charset_priority);
   defsubr (&Scharset_id_internal);
+  defsubr (&Ssort_charsets);
 
   DEFVAR_LISP ("charset-map-path", &Vcharset_map_path,
               doc: /* *List of directories to search for charset map files.  */);