In gdb, try to init comint history from gdbhist.
[bpt/emacs.git] / src / fontset.c
index 6e2e34d..86b9ceb 100644 (file)
@@ -1,8 +1,8 @@
 /* Fontset handler.
-   Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
+   Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
      Free Software Foundation, Inc.
    Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
-     2005, 2006, 2007, 2008, 2009
+     2005, 2006, 2007, 2008, 2009, 2010
      National Institute of Advanced Industrial Science and Technology (AIST)
      Registration Number H14PRO021
    Copyright (C) 2003, 2006
@@ -28,6 +28,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <config.h>
 #include <stdio.h>
+#include <setjmp.h>
 
 #include "lisp.h"
 #include "blockinput.h"
@@ -176,11 +177,10 @@ EXFUN (Fclear_face_cache, 1);
 
 /********** VARIABLES and FUNCTION PROTOTYPES **********/
 
-extern Lisp_Object Qfont;
 static Lisp_Object Qfontset;
 static Lisp_Object Qfontset_info;
 static Lisp_Object Qprepend, Qappend;
-static Lisp_Object Qlatin;
+Lisp_Object Qlatin;
 
 /* Vector containing all fontsets.  */
 static Lisp_Object Vfontset_table;
@@ -202,23 +202,23 @@ Lisp_Object Vvertical_centering_font_regexp;
 Lisp_Object Votf_script_alist;
 
 /* Check if any window system is used now.  */
-void (*check_window_system_func) P_ ((void));
+void (*check_window_system_func) (void);
 
 
 /* Prototype declarations for static functions.  */
-static Lisp_Object fontset_add P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
-                                   Lisp_Object));
-static Lisp_Object fontset_find_font P_ ((Lisp_Object, int, struct face *,
-                                         int, int));
-static void reorder_font_vector P_ ((Lisp_Object, struct font *));
-static Lisp_Object fontset_font P_ ((Lisp_Object, int, struct face *, int));
-static Lisp_Object make_fontset P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
-static Lisp_Object fontset_pattern_regexp P_ ((Lisp_Object));
-static void accumulate_script_ranges P_ ((Lisp_Object, Lisp_Object,
-                                         Lisp_Object));
-Lisp_Object find_font_encoding P_ ((Lisp_Object));
-
-static void set_fontset_font P_ ((Lisp_Object, Lisp_Object));
+static Lisp_Object fontset_add (Lisp_Object, Lisp_Object, Lisp_Object,
+                                Lisp_Object);
+static Lisp_Object fontset_find_font (Lisp_Object, int, struct face *,
+                                      int, int);
+static void reorder_font_vector (Lisp_Object, struct font *);
+static Lisp_Object fontset_font (Lisp_Object, int, struct face *, int);
+static Lisp_Object make_fontset (Lisp_Object, Lisp_Object, Lisp_Object);
+static Lisp_Object fontset_pattern_regexp (Lisp_Object);
+static void accumulate_script_ranges (Lisp_Object, Lisp_Object,
+                                      Lisp_Object);
+Lisp_Object find_font_encoding (Lisp_Object);
+
+static void set_fontset_font (Lisp_Object, Lisp_Object);
 
 #ifdef FONTSET_DEBUG
 
@@ -283,6 +283,10 @@ fontset_id_valid_p (id)
 #define RFONT_DEF_OBJECT(rfont_def) AREF (rfont_def, 2)
 #define RFONT_DEF_SET_OBJECT(rfont_def, object)        \
   ASET ((rfont_def), 2, (object))
+/* Score of RFONT_DEF is an integer value; the lowest 8 bits represent
+   the order of listing by font backends, the higher bits represents
+   the order given by charset priority list.  The smaller value is
+   preferable.  */
 #define RFONT_DEF_SCORE(rfont_def) XINT (AREF (rfont_def, 3))
 #define RFONT_DEF_SET_SCORE(rfont_def, score) \
   ASET ((rfont_def), 3, make_number (score))
@@ -305,9 +309,7 @@ fontset_id_valid_p (id)
    : fontset_ref ((fontset), (c)))
 
 static Lisp_Object
-fontset_ref (fontset, c)
-     Lisp_Object fontset;
-     int c;
+fontset_ref (Lisp_Object fontset, int c)
 {
   Lisp_Object elt;
 
@@ -342,8 +344,7 @@ fontset_ref (fontset, c)
    : fontset_add ((fontset), (range), (elt), (add)))
 
 static Lisp_Object
-fontset_add (fontset, range, elt, add)
-     Lisp_Object fontset, range, elt, add;
+fontset_add (Lisp_Object fontset, Lisp_Object range, Lisp_Object elt, Lisp_Object add)
 {
   Lisp_Object args[2];
   int idx = (EQ (add, Qappend) ? 0 : 1);
@@ -375,8 +376,7 @@ fontset_add (fontset, range, elt, add)
 }
 
 static int
-fontset_compare_rfontdef (val1, val2)
-     const void *val1, *val2;
+fontset_compare_rfontdef (const void *val1, const void *val2)
 {
   return (RFONT_DEF_SCORE (*(Lisp_Object *) val1)
          - RFONT_DEF_SCORE (*(Lisp_Object *) val2));
@@ -391,12 +391,8 @@ fontset_compare_rfontdef (val1, val2)
    If PREFERRED_FAMILY is not nil, that family has the higher priority
    if the encoding charsets or languages in font-specs are the same.  */
 
-extern Lisp_Object Fassoc_string ();
-
 static void
-reorder_font_vector (font_group, font)
-     Lisp_Object font_group;
-     struct font *font;
+reorder_font_vector (Lisp_Object font_group, struct font *font)
 {
   Lisp_Object vec, font_object;
   int size;
@@ -420,8 +416,13 @@ reorder_font_vector (font_group, font)
       Lisp_Object font_def = RFONT_DEF_FONT_DEF (rfont_def);
       Lisp_Object font_spec = FONT_DEF_SPEC (font_def);
       int score = RFONT_DEF_SCORE (rfont_def) & 0xFF;
+      Lisp_Object otf_spec = Ffont_get (font_spec, QCotf);
 
-      if (! font_match_p (font_spec, font_object))
+      if (! NILP (otf_spec))
+       /* A font-spec with :otf is preferable regardless of encoding
+          and language..  */
+       ;
+      else if (! font_match_p (font_spec, font_object))
        {
          Lisp_Object encoding = FONT_DEF_ENCODING (font_def);
 
@@ -459,6 +460,12 @@ reorder_font_vector (font_group, font)
   XSETCAR (font_group, make_number (charset_ordered_list_tick));
 }
 
+/* Return a font-group (actually a cons (-1 . FONT-GROUP-VECTOR)) for
+   character C in FONTSET.  If C is -1, return a fallback font-group.
+   If C is not -1, the value may be Qt (FONTSET doesn't have a font
+   for C even in the fallback group, or 0 (a font for C may be found
+   only in the fallback group).  */
+
 static Lisp_Object
 fontset_get_font_group (Lisp_Object fontset, int c)
 {
@@ -480,9 +487,10 @@ fontset_get_font_group (Lisp_Object fontset, int c)
     font_group = FONTSET_FALLBACK (base_fontset);
   if (NILP (font_group))
     {
+      font_group = make_number (0);
       if (c >= 0)
-       char_table_set_range (fontset, from, to, make_number (0));
-      return Qnil;
+       char_table_set_range (fontset, from, to, font_group);
+      return font_group;
     }
   font_group = Fcopy_sequence (font_group);
   for (i = 0; i < ASIZE (font_group); i++)
@@ -517,16 +525,13 @@ fontset_get_font_group (Lisp_Object fontset, int c)
    If FALLBACK is nonzero, search only fallback fonts.  */
 
 static Lisp_Object
-fontset_find_font (fontset, c, face, id, fallback)
-     Lisp_Object fontset;
-     int c;
-     struct face *face;
-     int id, fallback;
+fontset_find_font (Lisp_Object fontset, int c, struct face *face, int id, int fallback)
 {
   Lisp_Object vec, font_group;
-  int i, charset_matched = -1;
-  FRAME_PTR f = (FRAMEP (FONTSET_FRAME (fontset)))
-    ? XFRAME (selected_frame) : XFRAME (FONTSET_FRAME (fontset));
+  int i, charset_matched = 0, found_index;
+  FRAME_PTR f = (FRAMEP (FONTSET_FRAME (fontset))
+                ? XFRAME (FONTSET_FRAME (fontset)) : XFRAME (selected_frame));
+  Lisp_Object rfont_def;
 
   font_group = fontset_get_font_group (fontset, fallback ? -1 : c);
   if (! CONSP (font_group))
@@ -546,9 +551,12 @@ fontset_find_font (fontset, c, face, id, fallback)
           first.  */
        for (i = 0; i < ASIZE (vec); i++)
          {
-           Lisp_Object rfont_def = AREF (vec, i);
-           Lisp_Object repertory
-             = FONT_DEF_REPERTORY (RFONT_DEF_FONT_DEF (rfont_def));
+           Lisp_Object repertory;
+
+           rfont_def = AREF (vec, i);
+           if (NILP (rfont_def))
+             break;
+           repertory = FONT_DEF_REPERTORY (RFONT_DEF_FONT_DEF (rfont_def));
 
            if (XINT (repertory) == id)
              {
@@ -561,24 +569,49 @@ fontset_find_font (fontset, c, face, id, fallback)
   /* Find the first available font in the vector of RFONT-DEF.  */
   for (i = 0; i < ASIZE (vec); i++)
     {
-      Lisp_Object rfont_def, font_def;
+      Lisp_Object font_def;
       Lisp_Object font_entity, font_object;
 
-      if (i == 0 && charset_matched >= 0)
+      found_index = i;
+      if (i == 0)
        {
-         /* Try the element matching with the charset ID at first.  */
-         rfont_def = AREF (vec, charset_matched);
-         charset_matched = -1;
-         i--;
+         if (charset_matched > 0)
+           {
+             /* Try the element matching with the charset ID at first.  */
+             found_index = charset_matched;
+             /* Make this negative so that we don't come here in the
+                next loop.  */
+             charset_matched = - charset_matched;
+             /* We must try the first element in the next loop.  */
+             i--;
+           }
+       }
+      else if (i == - charset_matched)
+       {
+         /* We have already tried this element and the followings
+            that have the same font specifications in the first
+            iteration.  So, skip them all.  */
+         rfont_def = AREF (vec, i);
+         font_def = RFONT_DEF_FONT_DEF (rfont_def);
+         for (; i + 1 < ASIZE (vec); i++)
+           {
+             rfont_def = AREF (vec, i + 1);
+             if (NILP (rfont_def))
+               break;
+             if (! EQ (RFONT_DEF_FONT_DEF (rfont_def), font_def))
+               break;
+           }
+         continue;
        }
-      else if (i != charset_matched)
-       rfont_def = AREF (vec, i);
-      else
-       continue;
 
+      rfont_def = AREF (vec, found_index);
       if (NILP (rfont_def))
-       /* This is a sign of not to try the other fonts.  */
-       return Qt;
+       {
+         if (i < 0)
+           continue;
+         /* This is a sign of not to try the other fonts.  */
+         return Qt;
+       }
       if (INTEGERP (RFONT_DEF_FACE (rfont_def))
          && XINT (RFONT_DEF_FACE (rfont_def)) < 0)
        /* We couldn't open this font last time.  */
@@ -620,19 +653,24 @@ fontset_find_font (fontset, c, face, id, fallback)
        }
 
       if (font_has_char (f, font_object, c))
-       return rfont_def;
+       goto found;
 
       /* Find a font already opened, maching with the current spec,
         and supporting C. */
       font_def = RFONT_DEF_FONT_DEF (rfont_def);
-      for (i++; i < ASIZE (vec); i++)
+      for (; found_index + 1 < ASIZE (vec); found_index++)
        {
-         rfont_def = AREF (vec, i);
+         rfont_def = AREF (vec, found_index + 1);
+         if (NILP (rfont_def))
+           break;
          if (! EQ (RFONT_DEF_FONT_DEF (rfont_def), font_def))
            break;
-         font_object = RFONT_DEF_OBJECT (AREF (vec, i));
+         font_object = RFONT_DEF_OBJECT (rfont_def);
          if (! NILP (font_object) && font_has_char (f, font_object, c))
-           return rfont_def;
+           {
+             found_index++;
+             goto found;
+           }
        }
 
       /* Find a font-entity with the current spec and supporting C.  */
@@ -653,40 +691,51 @@ fontset_find_font (fontset, c, face, id, fallback)
          RFONT_DEF_SET_OBJECT (rfont_def, font_object);
          RFONT_DEF_SET_SCORE (rfont_def, RFONT_DEF_SCORE (rfont_def));
          new_vec = Fmake_vector (make_number (ASIZE (vec) + 1), Qnil);
-         for (j = 0; j < i; j++)
+         found_index++;
+         for (j = 0; j < found_index; j++)
            ASET (new_vec, j, AREF (vec, j));
          ASET (new_vec, j, rfont_def);
          for (j++; j < ASIZE (new_vec); j++)
            ASET (new_vec, j, AREF (vec, j - 1));
          XSETCDR (font_group, new_vec);
-         return rfont_def;
+         vec = new_vec;
+         goto found;
        }
-
-      /* No font of the current spec for C.  Try the next spec.  */
-      i--;
+      if (i >= 0)
+       i = found_index;
     }
 
   FONTSET_SET (fontset, make_number (c), make_number (0));
   return Qnil;
+
+ found:
+  if (fallback && found_index > 0)
+    {
+      /* The order of fonts in the fallback font-group is not that
+        important, and it is better to move the found font to the
+        first of the group so that the next try will find it
+        quickly. */
+      for (i = found_index; i > 0; i--)
+       ASET (vec, i, AREF (vec, i - 1));
+      ASET (vec, 0, rfont_def);
+    }
+  return rfont_def;
 }
 
 
 static Lisp_Object
-fontset_font (fontset, c, face, id)
-     Lisp_Object fontset;
-     int c;
-     struct face *face;
-     int id;
+fontset_font (Lisp_Object fontset, int c, struct face *face, int id)
 {
-  Lisp_Object rfont_def;
+  Lisp_Object rfont_def, default_rfont_def;
   Lisp_Object base_fontset;
 
   /* Try a font-group of FONTSET. */
+  FONT_DEFERRED_LOG ("current fontset: font for", make_number (c), Qnil);
   rfont_def = fontset_find_font (fontset, c, face, id, 0);
   if (VECTORP (rfont_def))
     return rfont_def;
-  if (EQ (rfont_def, Qt))
-    goto no_font;
+  if (NILP (rfont_def))
+    FONTSET_SET (fontset, make_number (c), make_number (0));
 
   /* Try a font-group of the default fontset. */
   base_fontset = FONTSET_BASE (fontset);
@@ -695,32 +744,39 @@ fontset_font (fontset, c, face, id)
       if (NILP (FONTSET_DEFAULT (fontset)))
        FONTSET_DEFAULT (fontset)
          = make_fontset (FONTSET_FRAME (fontset), Qnil, Vdefault_fontset);
-      rfont_def = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 0);
-      if (VECTORP (rfont_def))
-       return rfont_def;
-      if (EQ (rfont_def, Qt))
-       goto no_font;
+      FONT_DEFERRED_LOG ("default fontset: font for", make_number (c), Qnil);
+      default_rfont_def
+       = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 0);
+      if (VECTORP (default_rfont_def))
+       return default_rfont_def;
+      if (NILP (default_rfont_def))
+       FONTSET_SET (FONTSET_DEFAULT (fontset), make_number (c),
+                    make_number (0));
     }
 
   /* Try a fallback font-group of FONTSET. */
-  rfont_def = fontset_find_font (fontset, c, face, id, 1);
-  if (VECTORP (rfont_def))
-    return rfont_def;
-  if (EQ (rfont_def, Qt))
-    goto no_font;
+  if (! EQ (rfont_def, Qt))
+    {
+      FONT_DEFERRED_LOG ("current fallback: font for", make_number (c), Qnil);
+      rfont_def = fontset_find_font (fontset, c, face, id, 1);
+      if (VECTORP (rfont_def))
+       return rfont_def;
+      /* Remember that FONTSET has no font for C.  */
+      FONTSET_SET (fontset, make_number (c), Qt);
+    }
 
-  /* Try a fallback font-group of the default fontset . */
-  if (! EQ (base_fontset, Vdefault_fontset))
+  /* Try a fallback font-group of the default fontset. */
+  if (! EQ (base_fontset, Vdefault_fontset)
+      && ! EQ (default_rfont_def, Qt))
     {
+      FONT_DEFERRED_LOG ("default fallback: font for", make_number (c), Qnil);
       rfont_def = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 1);
       if (VECTORP (rfont_def))
        return rfont_def;
+      /* Remember that the default fontset has no font for C.  */
+      FONTSET_SET (FONTSET_DEFAULT (fontset), make_number (c), Qt);
     }
 
- no_font:
-  /* Remember that we have no font for C.  */
-  FONTSET_SET (fontset, make_number (c), Qt);
-
   return Qnil;
 }
 
@@ -729,8 +785,7 @@ fontset_font (fontset, c, face, id)
    BASE.  */
 
 static Lisp_Object
-make_fontset (frame, name, base)
-     Lisp_Object frame, name, base;
+make_fontset (Lisp_Object frame, Lisp_Object name, Lisp_Object base)
 {
   Lisp_Object fontset;
   int size = ASIZE (Vfontset_table);
@@ -765,31 +820,13 @@ make_fontset (frame, name, base)
   return fontset;
 }
 
-
-/* Set the ASCII font of the default fontset to FONTNAME if that is
-   not yet set.  */
-void
-set_default_ascii_font (fontname)
-     Lisp_Object fontname;
-{
-  if (! STRINGP (FONTSET_ASCII (Vdefault_fontset)))
-    {
-      int id = fs_query_fontset (fontname, 2);
-
-      if (id >= 0)
-       fontname = FONTSET_ASCII (FONTSET_FROM_ID (id));
-      FONTSET_ASCII (Vdefault_fontset)= fontname;
-    }
-}
-
 \f
 /********** INTERFACES TO xfaces.c, xfns.c, and dispextern.h **********/
 
 /* Return the name of the fontset who has ID.  */
 
 Lisp_Object
-fontset_name (id)
-     int id;
+fontset_name (int id)
 {
   Lisp_Object fontset;
 
@@ -801,8 +838,7 @@ fontset_name (id)
 /* Return the ASCII font name of the fontset who has ID.  */
 
 Lisp_Object
-fontset_ascii (id)
-     int id;
+fontset_ascii (int id)
 {
   Lisp_Object fontset, elt;
 
@@ -814,9 +850,7 @@ fontset_ascii (id)
 }
 
 void
-free_realized_fontset (f, fontset)
-     FRAME_PTR f;
-     Lisp_Object fontset;
+free_realized_fontset (FRAME_PTR f, Lisp_Object fontset)
 {
   Lisp_Object tail;
 
@@ -832,9 +866,7 @@ free_realized_fontset (f, fontset)
    free_realized_face.  */
 
 void
-free_face_fontset (f, face)
-     FRAME_PTR f;
-     struct face *face;
+free_face_fontset (FRAME_PTR f, struct face *face)
 {
   Lisp_Object fontset;
 
@@ -868,9 +900,7 @@ free_face_fontset (f, face)
    when C is not an ASCII character.  */
 
 int
-face_suitable_for_char_p (face, c)
-     struct face *face;
-     int c;
+face_suitable_for_char_p (struct face *face, int c)
 {
   Lisp_Object fontset, rfont_def;
 
@@ -887,11 +917,7 @@ face_suitable_for_char_p (face, c)
    the macro FACE_FOR_CHAR.  */
 
 int
-face_for_char (f, face, c, pos, object)
-     FRAME_PTR f;
-     struct face *face;
-     int c, pos;
-     Lisp_Object object;
+face_for_char (FRAME_PTR f, struct face *face, int c, int pos, Lisp_Object object)
 {
   Lisp_Object fontset, rfont_def, charset;
   int face_id;
@@ -929,7 +955,6 @@ face_for_char (f, face, c, pos, object)
        id = -1;
     }
 
-  font_deferred_log ("font for", Fcons (make_number (c), charset), Qnil);
   rfont_def = fontset_font (fontset, c, face, id);
   if (VECTORP (rfont_def))
     {
@@ -960,13 +985,9 @@ face_for_char (f, face, c, pos, object)
 
 
 Lisp_Object
-font_for_char (face, c, pos, object)
-     struct face *face;
-     int c, pos;
-     Lisp_Object object;
+font_for_char (struct face *face, int c, int pos, Lisp_Object object)
 {
   Lisp_Object fontset, rfont_def, charset;
-  int face_id;
   int id;
 
   if (ASCII_CHAR_P (c))
@@ -1001,7 +1022,6 @@ font_for_char (face, c, pos, object)
        id = -1;
     }
 
-  font_deferred_log ("font for", Fcons (make_number (c), charset), Qnil);
   rfont_def = fontset_font (fontset, c, face, id);
   return (VECTORP (rfont_def)
          ? RFONT_DEF_OBJECT (rfont_def)
@@ -1015,10 +1035,7 @@ font_for_char (face, c, pos, object)
    Called from realize_x_face.  */
 
 int
-make_fontset_for_ascii_face (f, base_fontset_id, face)
-     FRAME_PTR f;
-     int base_fontset_id;
-     struct face *face;
+make_fontset_for_ascii_face (FRAME_PTR f, int base_fontset_id, struct face *face)
 {
   Lisp_Object base_fontset, fontset, frame;
 
@@ -1052,11 +1069,10 @@ static Lisp_Object Vcached_fontset_data;
    expression corresponding to PATTERN.  */
 
 static Lisp_Object
-fontset_pattern_regexp (pattern)
-     Lisp_Object pattern;
+fontset_pattern_regexp (Lisp_Object pattern)
 {
-  if (!index ((char *) SDATA (pattern), '*')
-      && !index ((char *) SDATA (pattern), '?'))
+  if (!strchr ((char *) SDATA (pattern), '*')
+      && !strchr ((char *) SDATA (pattern), '?'))
     /* PATTERN does not contain any wild cards.  */
     return Qnil;
 
@@ -1127,9 +1143,7 @@ fontset_pattern_regexp (pattern)
 */
 
 int
-fs_query_fontset (name, name_pattern)
-     Lisp_Object name;
-     int name_pattern;
+fs_query_fontset (Lisp_Object name, int name_pattern)
 {
   Lisp_Object tem;
   int i;
@@ -1178,8 +1192,7 @@ The value is nil if there is no matching fontset.
 PATTERN can contain `*' or `?' as a wildcard
 just as X font name matching algorithm allows.
 If REGEXPP is non-nil, PATTERN is a regular expression.  */)
-     (pattern, regexpp)
-     Lisp_Object pattern, regexpp;
+  (Lisp_Object pattern, Lisp_Object regexpp)
 {
   Lisp_Object fontset;
   int id;
@@ -1202,10 +1215,7 @@ If REGEXPP is non-nil, PATTERN is a regular expression.  */)
 /* Return a list of base fontset names matching PATTERN on frame F.  */
 
 Lisp_Object
-list_fontsets (f, pattern, size)
-     FRAME_PTR f;
-     Lisp_Object pattern;
-     int size;
+list_fontsets (FRAME_PTR f, Lisp_Object pattern, int size)
 {
   Lisp_Object frame, regexp, val;
   int id;
@@ -1241,8 +1251,7 @@ list_fontsets (f, pattern, size)
 /* Free all realized fontsets whose base fontset is BASE.  */
 
 static void
-free_realized_fontsets (base)
-     Lisp_Object base;
+free_realized_fontsets (Lisp_Object base)
 {
   int id;
 
@@ -1299,8 +1308,7 @@ free_realized_fontsets (base)
    Set *FRAME to the actual frame.  */
 
 static Lisp_Object
-check_fontset_name (name, frame)
-     Lisp_Object name, *frame;
+check_fontset_name (Lisp_Object name, Lisp_Object *frame)
 {
   int id;
 
@@ -1329,8 +1337,7 @@ check_fontset_name (name, frame)
 }
 
 static void
-accumulate_script_ranges (arg, range, val)
-     Lisp_Object arg, range, val;
+accumulate_script_ranges (Lisp_Object arg, Lisp_Object range, Lisp_Object val)
 {
   if (EQ (XCAR (arg), val))
     {
@@ -1342,28 +1349,6 @@ accumulate_script_ranges (arg, range, val)
 }
 
 
-/* Return an ASCII font name generated from fontset name NAME and
-   font-spec ASCII_SPEC.  NAME is a string conforming to XLFD.  */
-
-static INLINE Lisp_Object
-generate_ascii_font_name (name, ascii_spec)
-     Lisp_Object name, ascii_spec;
-{
-  Lisp_Object font_spec = Ffont_spec (0, NULL);
-  int i;
-  char xlfd[256];
-
-  if (font_parse_xlfd ((char *) SDATA (name), font_spec) < 0)
-    error ("Not an XLFD font name: %s", SDATA (name));
-  for (i = FONT_FOUNDRY_INDEX; i < FONT_EXTRA_INDEX; i++)
-    if (! NILP (AREF (ascii_spec, i)))
-      ASET (font_spec, i, AREF (ascii_spec, i));
-  i = font_unparse_xlfd (font_spec, 0, xlfd, 256);
-  if (i < 0)
-    error ("Not an XLFD font name: %s", SDATA (name));
-  return make_unibyte_string (xlfd, i);
-}
-
 /* Callback function for map_charset_chars in Fset_fontset_font.
    ARG is a vector [ FONTSET FONT_DEF ADD ASCII SCRIPT_RANGE_LIST ].
 
@@ -1377,8 +1362,7 @@ generate_ascii_font_name (name, ascii_spec)
    case.  */
 
 static void
-set_fontset_font (arg, range)
-     Lisp_Object arg, range;
+set_fontset_font (Lisp_Object arg, Lisp_Object range)
 {
   Lisp_Object fontset, font_def, add, ascii, script_range_list;
   int from = XINT (XCAR (range)), to = XINT (XCDR (range));
@@ -1417,8 +1401,7 @@ set_fontset_font (arg, range)
   ASET (arg, 4, script_range_list);
 }
 
-extern Lisp_Object QCfamily, QCregistry;
-static void update_auto_fontset_alist P_ ((Lisp_Object, Lisp_Object));
+static void update_auto_fontset_alist (Lisp_Object, Lisp_Object);
 
 
 DEFUN ("set-fontset-font", Fset_fontset_font, Sset_fontset_font, 3, 5, 0,
@@ -1456,8 +1439,7 @@ Optional 5th argument ADD, if non-nil, specifies how to add FONT-SPEC
 to the font specifications for TARGET previously set.  If it is
 `prepend', FONT-SPEC is prepended.  If it is `append', FONT-SPEC is
 appended.  By default, FONT-SPEC overrides the previous settings.  */)
-     (name, target, font_spec, frame, add)
-     Lisp_Object name, target, font_spec, frame, add;
+  (Lisp_Object name, Lisp_Object target, Lisp_Object font_spec, Lisp_Object frame, Lisp_Object add)
 {
   Lisp_Object fontset;
   Lisp_Object font_def, registry, family;
@@ -1480,7 +1462,6 @@ appended.  By default, FONT-SPEC overrides the previous settings.  */)
   else if (STRINGP (font_spec))
     {
       Lisp_Object args[2];
-      extern Lisp_Object QCname;
 
       fontname = font_spec;
       args[0] = QCname;
@@ -1532,7 +1513,7 @@ appended.  By default, FONT-SPEC overrides the previous settings.  */)
   if (CHARACTERP (target))
     {
       if (XFASTINT (target) < 0x80)
-       error ("Can't set a font for partial ASCII range.");
+       error ("Can't set a font for partial ASCII range");
       range_list = Fcons (Fcons (target, target), Qnil);
     }
   else if (CONSP (target))
@@ -1546,7 +1527,7 @@ appended.  By default, FONT-SPEC overrides the previous settings.  */)
       if (XFASTINT (from) < 0x80)
        {
          if (XFASTINT (from) != 0 || XFASTINT (to) < 0x7F)
-           error ("Can't set a font for partial ASCII range.");
+           error ("Can't set a font for partial ASCII range");
          ascii_changed = 1;
        }
       range_list = Fcons (target, Qnil);
@@ -1666,8 +1647,7 @@ char-table `char-script-table'.
 
 FONT-SPEC is a vector, a cons, or a string.  See the documentation of
 `set-fontset-font' for the meaning.  */)
-  (name, fontlist)
-     Lisp_Object name, fontlist;
+  (Lisp_Object name, Lisp_Object fontlist)
 {
   Lisp_Object fontset;
   int id;
@@ -1740,15 +1720,13 @@ static int num_auto_fontsets;
    user intends to use FONT-OBJECT for Latin characters.  */
 
 int
-fontset_from_font (font_object)
-     Lisp_Object font_object;
+fontset_from_font (Lisp_Object font_object)
 {
   Lisp_Object font_name = font_get_name (font_object);
   Lisp_Object font_spec = Fcopy_font_spec (font_object);
   Lisp_Object registry = AREF (font_spec, FONT_REGISTRY_INDEX);
   Lisp_Object fontset_spec, alias, name, fontset;
   Lisp_Object val;
-  int i;
 
   val = assoc_no_quit (font_spec, auto_fontset_alist);
   if (CONSP (val))
@@ -1788,10 +1766,6 @@ fontset_from_font (font_object)
 
   FONTSET_ASCII (fontset) = font_name;
 
-#ifdef HAVE_NS
-  nsfont_make_fontset_for_font(name, font_object);
-#endif
-
   return XINT (FONTSET_ID (fontset));
 }
 
@@ -1802,8 +1776,7 @@ fontset_from_font (font_object)
    fontset_from_font.  */
 
 static void
-update_auto_fontset_alist (font_object, fontset)
-     Lisp_Object font_object, fontset;
+update_auto_fontset_alist (Lisp_Object font_object, Lisp_Object fontset)
 {
   Lisp_Object prev, tail;
 
@@ -1849,8 +1822,7 @@ update_auto_fontset_alist (font_object, fontset)
 
 DEFUN ("internal-char-font", Finternal_char_font, Sinternal_char_font, 1, 2, 0,
        doc: /* For internal use only.  */)
-     (position, ch)
-     Lisp_Object position, ch;
+  (Lisp_Object position, Lisp_Object ch)
 {
   EMACS_INT pos, pos_byte, dummy;
   int face_id;
@@ -1890,7 +1862,8 @@ DEFUN ("internal-char-font", Finternal_char_font, Sinternal_char_font, 1, 2, 0,
        return Qnil;
       w = XWINDOW (window);
       f = XFRAME (w->frame);
-      face_id = face_at_buffer_position (w, pos, -1, -1, &dummy, pos + 100, 0);
+      face_id = face_at_buffer_position (w, pos, -1, -1, &dummy,
+                                        pos + 100, 0, -1);
       charset = Fget_char_property (position, Qcharset, Qnil);
       if (CHARSETP (charset))
        cs_id = XINT (CHARSET_SYMBOL_ID (charset));
@@ -1939,8 +1912,7 @@ The char-table has one extra slot.  If FONTSET is not the default
 fontset, the value the extra slot is a char-table containing the
 information about the derived fonts from the default fontset.  The
 format is the same as above.  */)
-     (fontset, frame)
-     Lisp_Object fontset, frame;
+  (Lisp_Object fontset, Lisp_Object frame)
 {
   FRAME_PTR f;
   Lisp_Object *realized[2], fontsets[2], tables[2];
@@ -2066,6 +2038,7 @@ format is the same as above.  */)
 DEFUN ("fontset-font", Ffontset_font, Sfontset_font, 2, 3, 0,
        doc: /* Return a font name pattern for character CH in fontset NAME.
 If NAME is t, find a pattern in the default fontset.
+If NAME is nil, find a pattern in the fontset of the selected frame.
 
 The value has the form (FAMILY . REGISTRY), where FAMILY is a font
 family name and REGISTRY is a font registry name.  This is actually
@@ -2074,8 +2047,7 @@ fontset.
 
 If the 2nd optional arg ALL is non-nil, return a list of all font name
 patterns.  */)
-  (name, ch, all)
-     Lisp_Object name, ch, all;
+  (Lisp_Object name, Lisp_Object ch, Lisp_Object all)
 {
   int c;
   Lisp_Object fontset, elt, list, repertory, val;
@@ -2098,6 +2070,8 @@ patterns.  */)
              Lisp_Object family, registry;
 
              val = AREF (elt, j);
+             if (NILP (val))
+               return Qnil;
              repertory = AREF (val, 1);
              if (INTEGERP (repertory))
                {
@@ -2133,7 +2107,7 @@ patterns.  */)
 
 DEFUN ("fontset-list", Ffontset_list, Sfontset_list, 0, 0, 0,
        doc: /* Return a list of all defined fontset names.  */)
-     ()
+  (void)
 {
   Lisp_Object fontset, list;
   int i;
@@ -2190,7 +2164,7 @@ dump_fontset (fontset)
 
 DEFUN ("fontset-list-all", Ffontset_list_all, Sfontset_list_all, 0, 0, 0,
        doc: /* Return a brief summary of all fontsets for debug use.  */)
-     ()
+  (void)
 {
   Lisp_Object val;
   int i;
@@ -2203,7 +2177,7 @@ DEFUN ("fontset-list-all", Ffontset_list_all, Sfontset_list_all, 0, 0, 0,
 #endif /* FONTSET_DEBUG */
 
 void
-syms_of_fontset ()
+syms_of_fontset (void)
 {
   DEFSYM (Qfontset, "fontset");
   Fput (Qfontset, Qchar_table_extra_slots, make_number (9));
@@ -2224,7 +2198,7 @@ syms_of_fontset ()
   staticpro (&Vdefault_fontset);
   FONTSET_ID (Vdefault_fontset) = make_number (0);
   FONTSET_NAME (Vdefault_fontset)
-    = build_string ("-*-*-*-*-*-*-*-*-*-*-*-*-fontset-default");
+    = make_pure_c_string ("-*-*-*-*-*-*-*-*-*-*-*-*-fontset-default");
   ASET (Vfontset_table, 0, Vdefault_fontset);
   next_fontset_id = 1;
 
@@ -2270,7 +2244,7 @@ alternate fontnames (if any) are tried instead.  */);
   DEFVAR_LISP ("fontset-alias-alist", &Vfontset_alias_alist,
               doc: /* Alist of fontset names vs the aliases.  */);
   Vfontset_alias_alist = Fcons (Fcons (FONTSET_NAME (Vdefault_fontset),
-                                      build_string ("fontset-default")),
+                                      make_pure_c_string ("fontset-default")),
                                Qnil);
 
   DEFVAR_LISP ("vertical-centering-font-regexp",