(VALBITS, GCTYPEBITS): Deleted; default is better.
[bpt/emacs.git] / src / keymap.c
index 2224553..b6a1575 100644 (file)
@@ -163,8 +163,8 @@ synkey (frommap, fromchar, tomap, tochar)
      int fromchar, tochar;
 {
   Lisp_Object v, c;
-  XSET (v, Lisp_Vector, tomap);
-  XFASTINT (c) = tochar;
+  XSETVECTOR (v, tomap);
+  XSETFASTINT (c, tochar);
   frommap->contents[fromchar] = Fcons (v, c);
 }
 #endif /* 0 */
@@ -188,6 +188,7 @@ is also allowed as an element.")
 
    If AUTOLOAD is non-zero and OBJECT is a symbol whose function value
    is an autoload form, do the autoload and try again.
+   If AUTOLOAD is nonzero, callers must assume GC is possible.
 
    ERROR controls how we respond if OBJECT isn't a keymap.
    If ERROR is non-zero, signal an error; otherwise, just return Qnil.
@@ -213,7 +214,7 @@ get_keymap_1 (object, error, autoload)
   /* Should we do an autoload?  Autoload forms for keymaps have
      Qkeymap as their fifth element.  */
   if (autoload
-      && XTYPE (object) == Lisp_Symbol
+      && SYMBOLP (object)
       && CONSP (tem)
       && EQ (XCONS (tem)->car, Qautoload))
     {
@@ -278,12 +279,12 @@ access_keymap (map, idx, t_ok, noinherit)
 
   /* If idx is a symbol, it might have modifiers, which need to
      be put in the canonical order.  */
-  if (XTYPE (idx) == Lisp_Symbol)
+  if (SYMBOLP (idx))
     idx = reorder_modifiers (idx);
   else if (INTEGERP (idx))
     /* Clobber the high bits that can be present on a machine
        with more than 24 bits of integer.  */
-    XFASTINT (idx) = XINT (idx) & (CHAR_META | (CHAR_META - 1));
+    XSETFASTINT (idx, XINT (idx) & (CHAR_META | (CHAR_META - 1)));
 
   {
     Lisp_Object tail;
@@ -295,16 +296,15 @@ access_keymap (map, idx, t_ok, noinherit)
        Lisp_Object binding;
 
        binding = XCONS (tail)->car;
-       switch (XTYPE (binding))
+       if (SYMBOLP (binding))
          {
-         case Lisp_Symbol:
            /* If NOINHERIT, stop finding prefix definitions
               after we pass a second occurrence of the `keymap' symbol.  */
            if (noinherit && EQ (binding, Qkeymap) && ! EQ (tail, map))
              noprefix = 1;
-           break;
-
-         case Lisp_Cons:
+         }
+       else if (CONSP (binding))
+         {
            if (EQ (XCONS (binding)->car, idx))
              {
                val = XCONS (binding)->cdr;
@@ -314,19 +314,16 @@ access_keymap (map, idx, t_ok, noinherit)
              }
            if (t_ok && EQ (XCONS (binding)->car, Qt))
              t_binding = XCONS (binding)->cdr;
-           break;
-
-         case Lisp_Vector:
-           if (XTYPE (idx) == Lisp_Int
-               && XINT (idx) >= 0
-               && XINT (idx) < XVECTOR (binding)->size)
+         }
+       else if (VECTORP (binding))
+         {
+           if (NATNUMP (idx) && XFASTINT (idx) < XVECTOR (binding)->size)
              {
-               val = XVECTOR (binding)->contents[XINT (idx)];
+               val = XVECTOR (binding)->contents[XFASTINT (idx)];
                if (noprefix && CONSP (val) && EQ (XCONS (val)->car, Qkeymap))
                  return Qnil;
                return val;
              }
-           break;
          }
 
        QUIT;
@@ -367,14 +364,13 @@ get_keyelt (object, autoload)
         use DEFN.
         Keymap alist elements like (CHAR MENUSTRING . DEFN)
         will be used by HierarKey menus.  */
-      else if (XTYPE (object) == Lisp_Cons
-              && XTYPE (XCONS (object)->car) == Lisp_String)
+      else if (CONSP (object)
+              && STRINGP (XCONS (object)->car))
        {
          object = XCONS (object)->cdr;
          /* Also remove a menu help string, if any,
             following the menu item name.  */
-         if (XTYPE (object) == Lisp_Cons
-             && XTYPE (XCONS (object)->car) == Lisp_String)
+         if (CONSP (object) && STRINGP (XCONS (object)->car))
            object = XCONS (object)->cdr;
          /* Also remove the sublist that caches key equivalences, if any.  */
          if (CONSP (object)
@@ -399,8 +395,12 @@ store_in_keymap (keymap, idx, def)
      register Lisp_Object idx;
      register Lisp_Object def;
 {
-  if (XTYPE (keymap) != Lisp_Cons
-      || ! EQ (XCONS (keymap)->car, Qkeymap))
+  /* If we are preparing to dump, and DEF might be pure,
+     copy it to ensure it is not pure.  */
+  if (!NILP (Vpurify_flag) && CONSP (def))
+    def = Fcons (XCONS (def)->car, XCONS (def)->cdr);
+
+  if (!CONSP (keymap) || ! EQ (XCONS (keymap)->car, Qkeymap))
     error ("attempt to define a key in a non-keymap");
 
   /* If idx is a list (some sort of mouse click, perhaps?),
@@ -410,12 +410,12 @@ store_in_keymap (keymap, idx, def)
 
   /* If idx is a symbol, it might have modifiers, which need to
      be put in the canonical order.  */
-  if (XTYPE (idx) == Lisp_Symbol)
+  if (SYMBOLP (idx))
     idx = reorder_modifiers (idx);
   else if (INTEGERP (idx))
     /* Clobber the high bits that can be present on a machine
        with more than 24 bits of integer.  */
-    XFASTINT (idx) = XINT (idx) & (CHAR_META | (CHAR_META - 1));
+    XSETFASTINT (idx, XINT (idx) & (CHAR_META | (CHAR_META - 1)));
 
   /* Scan the keymap for a binding of idx.  */
   {
@@ -435,34 +435,31 @@ store_in_keymap (keymap, idx, def)
        Lisp_Object elt;
 
        elt = XCONS (tail)->car;
-       switch (XTYPE (elt))
+       if (VECTORP (elt))
          {
-         case Lisp_Vector:
-           if (XTYPE (idx) == Lisp_Int
-               && XINT (idx) >= 0 && XINT (idx) < XVECTOR (elt)->size)
+           if (NATNUMP (idx) && XFASTINT (idx) < XVECTOR (elt)->size)
              {
                XVECTOR (elt)->contents[XFASTINT (idx)] = def;
                return def;
              }
            insertion_point = tail;
-           break;
-
-         case Lisp_Cons:
+         }
+       else if (CONSP (elt))
+         {
            if (EQ (idx, XCONS (elt)->car))
              {
                XCONS (elt)->cdr = def;
                return def;
              }
-           break;
-
-         case Lisp_Symbol:
+         }
+       else if (SYMBOLP (elt))
+         {
            /* If we find a 'keymap' symbol in the spine of KEYMAP,
                then we must have found the start of a second keymap
                being used as the tail of KEYMAP, and a binding for IDX
                should be inserted before it.  */
            if (EQ (elt, Qkeymap))
              goto keymap_end;
-           break;
          }
 
        QUIT;
@@ -471,8 +468,8 @@ store_in_keymap (keymap, idx, def)
   keymap_end:
     /* We have scanned the entire keymap, and not found a binding for
        IDX.  Let's add one.  */
-    XCONS (insertion_point)->cdr =
-      Fcons (Fcons (idx, def), XCONS (insertion_point)->cdr);
+    XCONS (insertion_point)->cdr
+      Fcons (Fcons (idx, def), XCONS (insertion_point)->cdr);
   }
          
   return def;
@@ -498,7 +495,7 @@ is not copied.")
       Lisp_Object elt;
 
       elt = XCONS (tail)->car;
-      if (XTYPE (elt) == Lisp_Vector)
+      if (VECTORP (elt))
        {
          int i;
 
@@ -506,7 +503,7 @@ is not copied.")
          XCONS (tail)->car = elt;
 
          for (i = 0; i < XVECTOR (elt)->size; i++)
-           if (XTYPE (XVECTOR (elt)->contents[i]) != Lisp_Symbol
+           if (!SYMBOLP (XVECTOR (elt)->contents[i])
                && ! NILP (Fkeymapp (XVECTOR (elt)->contents[i])))
              XVECTOR (elt)->contents[i] =
                Fcopy_keymap (XVECTOR (elt)->contents[i]);
@@ -552,6 +549,8 @@ is not copied.")
 \f
 /* Simple Keymap mutators and accessors.                               */
 
+/* GC is possible in this function if it autoloads a keymap.  */
+
 DEFUN ("define-key", Fdefine_key, Sdefine_key, 3, 3, 0,
   "Args KEYMAP, KEY, DEF.  Define key sequence KEY, in KEYMAP, as DEF.\n\
 KEYMAP is a keymap.  KEY is a string or a vector of symbols and characters\n\
@@ -588,8 +587,7 @@ the front of KEYMAP.")
 
   keymap = get_keymap_1 (keymap, 1, 1);
 
-  if (XTYPE (key) != Lisp_Vector
-      && XTYPE (key) != Lisp_String)
+  if (!VECTORP (key) && !STRINGP (key))
     key = wrong_type_argument (Qarrayp, key);
 
   length = XFASTINT (Flength (key));
@@ -598,7 +596,7 @@ the front of KEYMAP.")
 
   GCPRO3 (keymap, key, def);
 
-  if (XTYPE (key) == Lisp_Vector)
+  if (VECTORP (key))
     meta_bit = meta_modifier;
   else
     meta_bit = 0x80;
@@ -608,7 +606,7 @@ the front of KEYMAP.")
     {
       c = Faref (key, make_number (idx));
 
-      if (XTYPE (c) == Lisp_Int
+      if (INTEGERP (c)
          && (XINT (c) & meta_bit)
          && !metized)
        {
@@ -617,7 +615,7 @@ the front of KEYMAP.")
        }
       else
        {
-         if (XTYPE (c) == Lisp_Int)
+         if (INTEGERP (c))
            XSETINT (c, XINT (c) & ~meta_bit);
 
          metized = 0;
@@ -625,7 +623,7 @@ the front of KEYMAP.")
        }
 
       if (! INTEGERP (c) && ! SYMBOLP (c) && ! CONSP (c))
-       error ("Key sequence contains illegal events");
+       error ("Key sequence contains invalid events");
 
       if (idx == length)
        RETURN_UNGCPRO (store_in_keymap (keymap, c, def));
@@ -646,6 +644,7 @@ the front of KEYMAP.")
 }
 
 /* Value is number if KEY is too long; NIL if valid but has no definition. */
+/* GC is possible in this function if it autoloads a keymap.  */
 
 DEFUN ("lookup-key", Flookup_key, Slookup_key, 2, 3, 0,
   "In keymap KEYMAP, look up key sequence KEY.  Return the definition.\n\
@@ -675,28 +674,30 @@ recognize the default bindings, just as `read-key-sequence' does.")
   int length;
   int t_ok = ! NILP (accept_default);
   int meta_bit;
+  struct gcpro gcpro1;
 
   keymap = get_keymap_1 (keymap, 1, 1);
 
-  if (XTYPE (key) != Lisp_Vector
-      && XTYPE (key) != Lisp_String)
+  if (!VECTORP (key) && !STRINGP (key))
     key = wrong_type_argument (Qarrayp, key);
 
   length = XFASTINT (Flength (key));
   if (length == 0)
     return keymap;
 
-  if (XTYPE (key) == Lisp_Vector)
+  if (VECTORP (key))
     meta_bit = meta_modifier;
   else
     meta_bit = 0x80;
 
+  GCPRO1 (key);
+
   idx = 0;
   while (1)
     {
       c = Faref (key, make_number (idx));
 
-      if (XTYPE (c) == Lisp_Int
+      if (INTEGERP (c)
          && (XINT (c) & meta_bit)
          && !metized)
        {
@@ -705,7 +706,7 @@ recognize the default bindings, just as `read-key-sequence' does.")
        }
       else
        {
-         if (XTYPE (c) == Lisp_Int)
+         if (INTEGERP (c))
            XSETINT (c, XINT (c) & ~meta_bit);
 
          metized = 0;
@@ -714,11 +715,11 @@ recognize the default bindings, just as `read-key-sequence' does.")
 
       cmd = get_keyelt (access_keymap (keymap, c, t_ok, 0), 1);
       if (idx == length)
-       return cmd;
+       RETURN_UNGCPRO (cmd);
 
       keymap = get_keymap_1 (cmd, 0, 1);
       if (NILP (keymap))
-       return make_number (idx);
+       RETURN_UNGCPRO (make_number (idx));
 
       QUIT;
     }
@@ -809,9 +810,9 @@ current_minor_maps (modeptr, mapptr)
   for (alist = Vminor_mode_map_alist;
        CONSP (alist);
        alist = XCONS (alist)->cdr)
-    if (CONSP (assoc = XCONS (alist)->car)
-       && XTYPE (var = XCONS (assoc)->car) == Lisp_Symbol
-       && ! EQ ((val = find_symbol_value (var)), Qunbound)
+    if ((assoc = XCONS (alist)->car, CONSP (assoc))
+       && (var = XCONS (assoc)->car, SYMBOLP (var))
+       && (val = find_symbol_value (var), ! EQ (val, Qunbound))
        && ! NILP (val))
       {
        if (i >= cmm_size)
@@ -859,6 +860,8 @@ current_minor_maps (modeptr, mapptr)
   return i;
 }
 
+/* GC is possible in this function if it autoloads a keymap.  */
+
 DEFUN ("key-binding", Fkey_binding, Skey_binding, 1, 2, 0,
   "Return the binding for command KEY in current keymaps.\n\
 KEY is a string or vector, a sequence of keystrokes.\n\
@@ -874,39 +877,48 @@ recognize the default bindings, just as `read-key-sequence' does.")
 {
   Lisp_Object *maps, value;
   int nmaps, i;
+  struct gcpro gcpro1;
+
+  GCPRO1 (key);
 
   if (!NILP (Voverriding_local_map))
     {
       value = Flookup_key (Voverriding_local_map, key, accept_default);
-      if (! NILP (value) && XTYPE (value) != Lisp_Int)
-       return value;
+      if (! NILP (value) && !INTEGERP (value))
+       RETURN_UNGCPRO (value);
     }
   else
     { 
       nmaps = current_minor_maps (0, &maps);
+      /* Note that all these maps are GCPRO'd
+        in the places where we found them.  */
+
       for (i = 0; i < nmaps; i++)
        if (! NILP (maps[i]))
          {
            value = Flookup_key (maps[i], key, accept_default);
-           if (! NILP (value) && XTYPE (value) != Lisp_Int)
-             return value;
+           if (! NILP (value) && !INTEGERP (value))
+             RETURN_UNGCPRO (value);
          }
 
       if (! NILP (current_buffer->keymap))
        {
          value = Flookup_key (current_buffer->keymap, key, accept_default);
-         if (! NILP (value) && XTYPE (value) != Lisp_Int)
-           return value;
+         if (! NILP (value) && !INTEGERP (value))
+           RETURN_UNGCPRO (value);
        }
     }
 
   value = Flookup_key (current_global_map, key, accept_default);
-  if (! NILP (value) && XTYPE (value) != Lisp_Int)
+  UNGCPRO;
+  if (! NILP (value) && !INTEGERP (value))
     return value;
   
   return Qnil;
 }
 
+/* GC is possible in this function if it autoloads a keymap.  */
+
 DEFUN ("local-key-binding", Flocal_key_binding, Slocal_key_binding, 1, 2, 0,
   "Return the binding for command KEYS in current local keymap only.\n\
 KEYS is a string, a sequence of keystrokes.\n\
@@ -924,12 +936,14 @@ bindings; see the description of `lookup-key' for more details about this.")
   return Flookup_key (map, keys, accept_default);
 }
 
+/* GC is possible in this function if it autoloads a keymap.  */
+
 DEFUN ("global-key-binding", Fglobal_key_binding, Sglobal_key_binding, 1, 2, 0,
   "Return the binding for command KEYS in current global keymap only.\n\
 KEYS is a string, a sequence of keystrokes.\n\
 The binding is probably a symbol with a function definition.\n\
 This function's return values are the same as those of lookup-key\n\
-(which see).\n\
+\(which see).\n\
 \n\
 If optional argument ACCEPT-DEFAULT is non-nil, recognize default\n\
 bindings; see the description of `lookup-key' for more details about this.")
@@ -939,6 +953,8 @@ bindings; see the description of `lookup-key' for more details about this.")
   return Flookup_key (current_global_map, keys, accept_default);
 }
 
+/* GC is possible in this function if it autoloads a keymap.  */
+
 DEFUN ("minor-mode-key-binding", Fminor_mode_key_binding, Sminor_mode_key_binding, 1, 2, 0,
   "Find the visible minor mode bindings of KEY.\n\
 Return an alist of pairs (MODENAME . BINDING), where MODENAME is the\n\
@@ -958,20 +974,27 @@ bindings; see the description of `lookup-key' for more details about this.")
   int nmaps;
   Lisp_Object binding;
   int i, j;
+  struct gcpro gcpro1, gcpro2;
 
   nmaps = current_minor_maps (&modes, &maps);
+  /* Note that all these maps are GCPRO'd
+     in the places where we found them.  */
+
+  binding = Qnil;
+  GCPRO2 (key, binding);
 
   for (i = j = 0; i < nmaps; i++)
     if (! NILP (maps[i])
        && ! NILP (binding = Flookup_key (maps[i], key, accept_default))
-       && XTYPE (binding) != Lisp_Int)
+       && !INTEGERP (binding))
       {
        if (! NILP (get_keymap (binding)))
          maps[j++] = Fcons (modes[i], binding);
        else if (j == 0)
-         return Fcons (Fcons (modes[i], binding), Qnil);
+         RETURN_UNGCPRO (Fcons (Fcons (modes[i], binding), Qnil));
       }
 
+  UNGCPRO;
   return Flist (j, maps);
 }
 
@@ -987,8 +1010,7 @@ that local binding will continue to shadow any global binding.")
   (keys, function)
      Lisp_Object keys, function;
 {
-  if (XTYPE (keys) != Lisp_Vector
-      && XTYPE (keys) != Lisp_String)
+  if (!VECTORP (keys) && !STRINGP (keys))
     keys = wrong_type_argument (Qarrayp, keys);
 
   Fdefine_key (current_global_map, keys, function);
@@ -1015,8 +1037,7 @@ which in most cases is shared with all other buffers in the same major mode.")
       current_buffer->keymap = map;
     }
 
-  if (XTYPE (keys) != Lisp_Vector
-      && XTYPE (keys) != Lisp_String)
+  if (!VECTORP (keys) && !STRINGP (keys))
     keys = wrong_type_argument (Qarrayp, keys);
 
   Fdefine_key (map, keys, function);
@@ -1071,6 +1092,8 @@ DEFUN ("use-global-map", Fuse_global_map, Suse_global_map, 1, 1, 0,
 {
   keymap = get_keymap (keymap);
   current_global_map = keymap;
+  record_asynch_buffer_change ();
+
   return Qnil;
 }
 
@@ -1084,6 +1107,7 @@ If KEYMAP is nil, that means no local keymap.")
     keymap = get_keymap (keymap);
 
   current_buffer->keymap = keymap;
+  record_asynch_buffer_change ();
 
   return Qnil;
 }
@@ -1114,6 +1138,8 @@ DEFUN ("current-minor-mode-maps", Fcurrent_minor_mode_maps, Scurrent_minor_mode_
 \f
 /* Help functions for describing and documenting keymaps.              */
 
+/* This function cannot GC.  */
+
 DEFUN ("accessible-keymaps", Faccessible_keymaps, Saccessible_keymaps,
   1, 2, 0,
   "Find all keymaps accessible via prefix characters from KEYMAP.\n\
@@ -1128,12 +1154,30 @@ then the value includes only maps for prefixes that start with PREFIX.")
   Lisp_Object maps, good_maps, tail;
   int prefixlen = 0;
 
+  /* no need for gcpro because we don't autoload any keymaps.  */
+
   if (!NILP (prefix))
     prefixlen = XINT (Flength (prefix));
 
-  maps = Fcons (Fcons (Fmake_vector (make_number (0), Qnil),
-                      get_keymap (startmap)),
-               Qnil);
+  if (!NILP (prefix))
+    {
+      /* If a prefix was specified, start with the keymap (if any) for
+        that prefix, so we don't waste time considering other prefixes.  */
+      Lisp_Object tem;
+      tem = Flookup_key (startmap, prefix, Qt);
+      /* Flookup_key may give us nil, or a number,
+        if the prefix is not defined in this particular map.
+        It might even give us a list that isn't a keymap.  */
+      tem = get_keymap_1 (tem, 0, 0);
+      if (!NILP (tem))
+       maps = Fcons (Fcons (prefix, tem), Qnil);
+      else
+       return Qnil;
+    }
+  else
+    maps = Fcons (Fcons (Fmake_vector (make_number (0), Qnil),
+                        get_keymap (startmap)),
+                 Qnil);
 
   /* For each map in the list maps,
      look at any other maps it points to,
@@ -1163,7 +1207,7 @@ then the value includes only maps for prefixes that start with PREFIX.")
 
          QUIT;
 
-         if (XTYPE (elt) == Lisp_Vector)
+         if (VECTORP (elt))
            {
              register int i;
 
@@ -1230,7 +1274,7 @@ then the value includes only maps for prefixes that start with PREFIX.")
                      /* If the last key in thisseq is meta-prefix-char, and
                         this entry is a binding for an ascii keystroke,
                         turn it into a meta-ized keystroke.  */
-                     if (is_metized && XTYPE (elt) == Lisp_Int)
+                     if (is_metized && INTEGERP (elt))
                        {
                          tem = Fcopy_sequence (thisseq);
                          Faset (tem, last,
@@ -1271,7 +1315,7 @@ then the value includes only maps for prefixes that start with PREFIX.")
          for (i = 0; i < prefixlen; i++)
            {
              Lisp_Object i1;
-             XFASTINT (i1) = i;
+             XSETFASTINT (i1, i);
              if (!EQ (Faref (thisseq, i1), Faref (prefix, i1)))
                break;
            }
@@ -1285,6 +1329,8 @@ then the value includes only maps for prefixes that start with PREFIX.")
 
 Lisp_Object Qsingle_key_description, Qkey_description;
 
+/* This function cannot GC.  */
+
 DEFUN ("key-description", Fkey_description, Skey_description, 1, 1, 0,
   "Return a pretty description of key-sequence KEYS.\n\
 Control characters turn into \"C-foo\" sequences, meta into \"M-foo\"\n\
@@ -1292,23 +1338,45 @@ spaces are put between sequence elements, etc.")
   (keys)
      Lisp_Object keys;
 {
-  if (XTYPE (keys) == Lisp_String)
+  int len;
+  int i;
+  Lisp_Object sep;
+  Lisp_Object *args;
+
+  if (STRINGP (keys))
     {
       Lisp_Object vector;
-      int i;
       vector = Fmake_vector (Flength (keys), Qnil);
       for (i = 0; i < XSTRING (keys)->size; i++)
        {
          if (XSTRING (keys)->data[i] & 0x80)
-           XFASTINT (XVECTOR (vector)->contents[i])
-             = meta_modifier | (XSTRING (keys)->data[i] & ~0x80);
+           XSETFASTINT (XVECTOR (vector)->contents[i],
+                        meta_modifier | (XSTRING (keys)->data[i] & ~0x80));
          else
-           XFASTINT (XVECTOR (vector)->contents[i])
-             = XSTRING (keys)->data[i];
+           XSETFASTINT (XVECTOR (vector)->contents[i],
+                        XSTRING (keys)->data[i]);
        }
       keys = vector;
     }
-  return Fmapconcat (Qsingle_key_description, keys, build_string (" "));
+  else if (!VECTORP (keys))
+    keys = wrong_type_argument (Qarrayp, keys);
+
+  /* In effect, this computes
+     (mapconcat 'single-key-description keys " ")
+     but we shouldn't use mapconcat because it can do GC.  */
+
+  len = XVECTOR (keys)->size;
+  sep = build_string (" ");
+  /* This has one extra element at the end that we don't pass to Fconcat.  */
+  args = (Lisp_Object *) alloca (len * 2 * sizeof (Lisp_Object));
+
+  for (i = 0; i < len; i++)
+    {
+      args[i * 2] = Fsingle_key_description (XVECTOR (keys)->contents[i]);
+      args[i * 2 + 1] = sep;
+    }
+
+  return Fconcat (len * 2 - 1, args);
 }
 
 char *
@@ -1419,6 +1487,8 @@ push_key_description (c, p)
   return p;  
 }
 
+/* This function cannot GC.  */
+
 DEFUN ("single-key-description", Fsingle_key_description, Ssingle_key_description, 1, 1, 0,
   "Return a pretty description of command character KEY.\n\
 Control characters turn into C-whatever, etc.")
@@ -1429,18 +1499,17 @@ Control characters turn into C-whatever, etc.")
 
   key = EVENT_HEAD (key);
 
-  switch (XTYPE (key))
+  if (INTEGERP (key))          /* Normal character */
     {
-    case Lisp_Int:             /* Normal character */
       *push_key_description (XUINT (key), tem) = 0;
       return build_string (tem);
-
-    case Lisp_Symbol:          /* Function key or event-symbol */
-      return Fsymbol_name (key);
-
-    default:
-      error ("KEY must be an integer, cons, or symbol.");
     }
+  else if (SYMBOLP (key))      /* Function key or event-symbol */
+    return Fsymbol_name (key);
+  else if (STRINGP (key))      /* Buffer names in the menubar.  */
+    return Fcopy_sequence (key);
+  else
+    error ("KEY must be an integer, cons, symbol, or string");
 }
 
 char *
@@ -1469,6 +1538,8 @@ push_text_char_description (c, p)
   return p;  
 }
 
+/* This function cannot GC.  */
+
 DEFUN ("text-char-description", Ftext_char_description, Stext_char_description, 1, 1, 0,
   "Return a pretty description of file-character CHAR.\n\
 Control characters turn into \"^char\", etc.")
@@ -1490,16 +1561,17 @@ static int
 ascii_sequence_p (seq)
      Lisp_Object seq;
 {
-  Lisp_Object i;
+  int i;
   int len = XINT (Flength (seq));
 
-  for (XFASTINT (i) = 0; XFASTINT (i) < len; XFASTINT (i)++)
+  for (i = 0; i < len; i++)
     {
-      Lisp_Object elt;
+      Lisp_Object ii, elt;
 
-      elt = Faref (seq, i);
+      XSETFASTINT (ii, i);
+      elt = Faref (seq, ii);
 
-      if (XTYPE (elt) != Lisp_Int
+      if (!INTEGERP (elt)
          || (XUINT (elt) & ~CHAR_META) >= 0x80)
        return 0;
     }
@@ -1510,6 +1582,8 @@ ascii_sequence_p (seq)
 \f
 /* where-is - finding a command in a set of keymaps.                   */
 
+/* This function can GC if Flookup_key autoloads any keymaps.  */
+
 DEFUN ("where-is-internal", Fwhere_is_internal, Swhere_is_internal, 1, 4, 0,
   "Return list of keys that invoke DEFINITION.\n\
 If KEYMAP is non-nil, search only KEYMAP and the global keymap.\n\
@@ -1529,9 +1603,10 @@ indirect definition itself.")
      Lisp_Object definition, keymap;
      Lisp_Object firstonly, noindirect;
 {
-  register Lisp_Object maps;
-  Lisp_Object found;
+  Lisp_Object maps;
+  Lisp_Object found, sequence;
   int keymap_specified = !NILP (keymap);
+  struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
 
   if (! keymap_specified)
     {
@@ -1563,7 +1638,9 @@ indirect definition itself.")
        }
     }
 
+  GCPRO5 (definition, keymap, maps, found, sequence);
   found = Qnil;
+  sequence = Qnil;
 
   for (; !NILP (maps); maps = Fcdr (maps))
     {
@@ -1598,18 +1675,18 @@ indirect definition itself.")
             advance map to the next element until i indicates that we
             have finished off the vector.  */
          
-         Lisp_Object elt, key, binding, sequence;
+         Lisp_Object elt, key, binding;
          elt = XCONS (map)->car;
 
          QUIT;
 
          /* Set key and binding to the current key and binding, and
             advance map and i to the next binding.  */
-         if (XTYPE (elt) == Lisp_Vector)
+         if (VECTORP (elt))
            {
              /* In a vector, look at each element.  */
              binding = XVECTOR (elt)->contents[i];
-             XFASTINT (key) = i;
+             XSETFASTINT (key, i);
              i++;
 
              /* If we've just finished scanning a vector, advance map
@@ -1643,7 +1720,7 @@ indirect definition itself.")
          /* End this iteration if this element does not match
             the target.  */
 
-         if (XTYPE (definition) == Lisp_Cons)
+         if (CONSP (definition))
            {
              Lisp_Object tem;
              tem = Fequal (binding, definition);
@@ -1656,7 +1733,7 @@ indirect definition itself.")
 
          /* We have found a match.
             Construct the key sequence where we found it.  */
-         if (XTYPE (key) == Lisp_Int && last_is_meta)
+         if (INTEGERP (key) && last_is_meta)
            {
              sequence = Fcopy_sequence (this);
              Faset (sequence, last, make_number (XINT (key) | meta_modifier));
@@ -1676,9 +1753,9 @@ indirect definition itself.")
          if (keymap_specified)
            {
              binding = Flookup_key (keymap, sequence, Qnil);
-             if (!NILP (binding) && XTYPE (binding) != Lisp_Int)
+             if (!NILP (binding) && !INTEGERP (binding))
                {
-                 if (XTYPE (definition) == Lisp_Cons)
+                 if (CONSP (definition))
                    {
                      Lisp_Object tem;
                      tem = Fequal (binding, definition);
@@ -1707,12 +1784,14 @@ indirect definition itself.")
             nil, then we should return the first ascii-only binding
             we find.  */
          if (EQ (firstonly, Qnon_ascii))
-           return sequence;
+           RETURN_UNGCPRO (sequence);
          else if (! NILP (firstonly) && ascii_sequence_p (sequence))
-           return sequence;
+           RETURN_UNGCPRO (sequence);
        }
     }
 
+  UNGCPRO;
+
   found = Fnreverse (found);
 
   /* firstonly may have been t, but we may have gone all the way through
@@ -1735,7 +1814,7 @@ then we display only bindings that start with that prefix.")
      Lisp_Object prefix;
 {
   register Lisp_Object thisbuf;
-  XSET (thisbuf, Lisp_Buffer, current_buffer);
+  XSETBUFFER (thisbuf, current_buffer);
   internal_with_output_to_temp_buffer ("*Help*",
                                       describe_buffer_bindings,
                                       Fcons (thisbuf, prefix));
@@ -1766,7 +1845,7 @@ nominal         alternate\n\
   Fset_buffer (Vstandard_output);
 
   /* Report on alternates for keys.  */
-  if (XTYPE (Vkeyboard_translate_table) == Lisp_String)
+  if (STRINGP (Vkeyboard_translate_table))
     {
       int c;
       unsigned char *translate = XSTRING (Vkeyboard_translate_table)->data;
@@ -1818,7 +1897,7 @@ nominal         alternate\n\
           because it takes care of other features when doing so.  */
        char *title, *p;
 
-       if (XTYPE (modes[i]) != Lisp_Symbol)
+       if (!SYMBOLP (modes[i]))
          abort();
 
        p = title = (char *) alloca (40 + XSYMBOL (modes[i])->name->size);
@@ -1852,6 +1931,7 @@ nominal         alternate\n\
   describe_map_tree (current_global_map, 0, shadow, prefix,
                     "Global Bindings", 0);
 
+  call0 (intern ("help-mode"));
   Fset_buffer (descbuf);
   UNGCPRO;
   return Qnil;
@@ -1941,10 +2021,8 @@ key             binding\n\
 
          /* If the sequence by which we reach this keymap is zero-length,
             then the shadow map for this keymap is just SHADOW.  */
-         if ((XTYPE (prefix) == Lisp_String
-              && XSTRING (prefix)->size == 0)
-             || (XTYPE (prefix) == Lisp_Vector
-                 && XVECTOR (prefix)->size == 0))
+         if ((STRINGP (prefix) && XSTRING (prefix)->size == 0)
+             || (VECTORP (prefix) && XVECTOR (prefix)->size == 0))
            ;
          /* If the sequence by which we reach this keymap actually has
             some elements, then the sequence's definition in SHADOW is
@@ -1952,7 +2030,7 @@ key             binding\n\
          else
            {
              shmap = Flookup_key (shmap, Fcar (elt), Qt);
-             if (XTYPE (shmap) == Lisp_Int)
+             if (INTEGERP (shmap))
                shmap = Qnil;
            }
 
@@ -1986,9 +2064,9 @@ describe_command (definition)
 
   Findent_to (make_number (16), make_number (1));
 
-  if (XTYPE (definition) == Lisp_Symbol)
+  if (SYMBOLP (definition))
     {
-      XSET (tem1, Lisp_String, XSYMBOL (definition)->name);
+      XSETSTRING (tem1, XSYMBOL (definition)->name);
       insert1 (tem1);
       insert_string ("\n");
     }
@@ -2067,7 +2145,7 @@ describe_map (map, keys, elt_describer, partial, shadow, seen)
     {
       QUIT;
 
-      if (XTYPE (XCONS (tail)->car) == Lisp_Vector)
+      if (VECTORP (XCONS (tail)->car))
        describe_vector (XCONS (tail)->car,
                         elt_prefix, elt_describer, partial, shadow);
       else if (CONSP (XCONS (tail)->car))
@@ -2083,7 +2161,7 @@ describe_map (map, keys, elt_describer, partial, shadow, seen)
 
          /* Don't show undefined commands or suppressed commands.  */
          if (NILP (definition)) continue;
-         if (XTYPE (definition) == Lisp_Symbol && partial)
+         if (SYMBOLP (definition) && partial)
            {
              tem = Fget (definition, suppress);
              if (!NILP (tem))
@@ -2126,7 +2204,7 @@ describe_map (map, keys, elt_describer, partial, shadow, seen)
             using an inherited keymap.  So skip anything we've already
             encountered.  */
          tem = Fassq (tail, *seen);
-         if (CONSP (tem) && Fequal (XCONS (tem)->car, keys))
+         if (CONSP (tem) && !NILP (Fequal (XCONS (tem)->car, keys)))
            break;
          *seen = Fcons (Fcons (tail, keys), *seen);
        }
@@ -2194,7 +2272,7 @@ describe_vector (vector, elt_prefix, elt_describer, partial, shadow)
       if (NILP (tem1)) continue;      
 
       /* Don't mention suppressed commands.  */
-      if (XTYPE (tem1) == Lisp_Symbol && partial)
+      if (SYMBOLP (tem1) && partial)
        {
          this = Fget (tem1, suppress);
          if (!NILP (this))
@@ -2224,7 +2302,7 @@ describe_vector (vector, elt_prefix, elt_describer, partial, shadow)
        insert1 (elt_prefix);
 
       /* Get the string to describe the character I, and print it.  */
-      XFASTINT (dummy) = i;
+      XSETFASTINT (dummy, i);
 
       /* THIS gets the string to describe the character DUMMY.  */
       this = Fsingle_key_description (dummy);
@@ -2245,7 +2323,7 @@ describe_vector (vector, elt_prefix, elt_describer, partial, shadow)
          if (!NILP (elt_prefix))
            insert1 (elt_prefix);
 
-         XFASTINT (dummy) = i;
+         XSETFASTINT (dummy, i);
          insert1 (Fsingle_key_description (dummy));
        }
 
@@ -2348,15 +2426,18 @@ in the list takes precedence.");
 This allows Emacs to recognize function keys sent from ASCII\n\
 terminals at any point in a key sequence.\n\
 \n\
-The read-key-sequence function replaces subsequences bound by\n\
-function-key-map with their bindings.  When the current local and global\n\
+The `read-key-sequence' function replaces any subsequence bound by\n\
+`function-key-map' with its binding.  More precisely, when the active\n\
 keymaps have no binding for the current key sequence but\n\
-function-key-map binds a suffix of the sequence to a vector or string,\n\
-read-key-sequence replaces the matching suffix with its binding, and\n\
+`function-key-map' binds a suffix of the sequence to a vector or string,\n\
+`read-key-sequence' replaces the matching suffix with its binding, and\n\
 continues with the new sequence.\n\
 \n\
-For example, suppose function-key-map binds `ESC O P' to [f1].\n\
-Typing `ESC O P' to read-key-sequence would return [f1].  Typing\n\
+The events that come from bindings in `function-key-map' are not\n\
+themselves looked up in `function-key-map'.\n\
+\n\
+For example, suppose `function-key-map' binds `ESC O P' to [f1].\n\
+Typing `ESC O P' to `read-key-sequence' would return [f1].  Typing\n\
 `C-x ESC O P' would return [?\\C-x f1].  If [f1] were a prefix\n\
 key, typing `ESC O P x' would return [f1 x].");
   Vfunction_key_map = Fmake_sparse_keymap (Qnil);