* systty.h: Remove code for Aix on 386, unsupported platform.
[bpt/emacs.git] / src / keymap.c
index b624359..545ab3d 100644 (file)
@@ -1,14 +1,14 @@
 /* Manipulation of keymaps
    Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
                  1998, 1999, 2000, 2001, 2002, 2003, 2004,
-                 2005, 2006, 2007 Free Software Foundation, Inc.
+                 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
-GNU Emacs is free software; you can redistribute it and/or modify
+GNU Emacs is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 3, or (at your option)
-any later version.
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
 
 GNU Emacs is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -16,9 +16,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with GNU Emacs; see the file COPYING.  If not, write to
-the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-Boston, MA 02110-1301, USA.  */
+along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 
 #include <config.h>
@@ -76,7 +74,7 @@ Lisp_Object Vminibuffer_local_filename_completion_map;
 
 /* keymap used for minibuffers when doing completion in filenames
    with require-match*/
-Lisp_Object Vminibuffer_local_must_match_filename_map;
+Lisp_Object Vminibuffer_local_filename_must_match_map;
 
 /* keymap used for minibuffers when doing completion and require a match */
 /* was MinibufLocalMustMatchMap */
@@ -113,6 +111,10 @@ extern Lisp_Object meta_prefix_char;
 
 extern Lisp_Object Voverriding_local_map;
 
+#ifdef HAVE_NS
+extern Lisp_Object Qalt, Qcontrol, Qhyper, Qmeta, Qsuper;
+#endif
+
 /* Hash table used to cache a reverse-map to speed up calls to where-is.  */
 static Lisp_Object where_is_cache;
 /* Which keymaps are reverse-stored in the cache.  */
@@ -563,9 +565,6 @@ access_keymap (map, idx, t_ok, noinherit, autoload)
 
     GCPRO4 (map, tail, idx, t_binding);
 
-    /* If `t_ok' is 2, both `t' is accepted.  */
-    t_ok = t_ok ? 2 : 0;
-
     for (tail = XCDR (map);
         (CONSP (tail)
          || (tail = get_keymap (tail, 0, autoload), CONSP (tail)));
@@ -587,10 +586,10 @@ access_keymap (map, idx, t_ok, noinherit, autoload)
 
            if (EQ (key, idx))
              val = XCDR (binding);
-           else if (t_ok > 1 && EQ (key, Qt))
+           else if (t_ok && EQ (key, Qt))
              {
                t_binding = XCDR (binding);
-               t_ok = 1;
+               t_ok = 0;
              }
          }
        else if (VECTORP (binding))
@@ -660,25 +659,20 @@ map_keymap_char_table_item (args, key, val)
     }
 }
 
-/* Call FUN for every binding in MAP.
-   FUN is called with 4 arguments: FUN (KEY, BINDING, ARGS, DATA).
-   AUTOLOAD if non-zero means that we can autoload keymaps if necessary.  */
-void
-map_keymap (map, fun, args, data, autoload)
-     map_keymap_function_t fun;
-     Lisp_Object map, args;
-     void *data;
-     int autoload;
+/* Call FUN for every binding in MAP and stop at (and return) the parent.
+   FUN is called with 4 arguments: FUN (KEY, BINDING, ARGS, DATA).  */
+Lisp_Object
+map_keymap_internal (Lisp_Object map,
+                    map_keymap_function_t fun,
+                    Lisp_Object args,
+                    void *data)
 {
   struct gcpro gcpro1, gcpro2, gcpro3;
-  Lisp_Object tail;
+  Lisp_Object tail
+    = (CONSP (map) && EQ (Qkeymap, XCAR (map))) ? XCDR (map) : map;
 
-  tail = Qnil;
   GCPRO3 (map, args, tail);
-  map = get_keymap (map, 1, autoload);
-  for (tail = (CONSP (map) && EQ (Qkeymap, XCAR (map))) ? XCDR (map) : map;
-       CONSP (tail) || (tail = get_keymap (tail, 0, autoload), CONSP (tail));
-       tail = XCDR (tail))
+  for (; CONSP (tail) && !EQ (Qkeymap, XCAR (tail)); tail = XCDR (tail))
     {
       Lisp_Object binding = XCAR (tail);
 
@@ -705,6 +699,7 @@ map_keymap (map, fun, args, data, autoload)
        }
     }
   UNGCPRO;
+  return tail;
 }
 
 static void
@@ -715,13 +710,66 @@ map_keymap_call (key, val, fun, dummy)
   call2 (fun, key, val);
 }
 
+/* Same as map_keymap_internal, but doesn't traverses parent keymaps as well.
+   A non-zero AUTOLOAD indicates that autoloaded keymaps should be loaded.  */
+void
+map_keymap (map, fun, args, data, autoload)
+     map_keymap_function_t fun;
+     Lisp_Object map, args;
+     void *data;
+     int autoload;
+{
+  struct gcpro gcpro1;
+  GCPRO1 (args);
+  map = get_keymap (map, 1, autoload);
+  while (CONSP (map))
+    {
+      map = map_keymap_internal (map, fun, args, data);
+      map = get_keymap (map, 0, autoload);
+    }
+  UNGCPRO;
+}
+
+Lisp_Object Qkeymap_canonicalize;
+
+/* Same as map_keymap, but does it right, properly eliminating duplicate
+   bindings due to inheritance.   */
+void
+map_keymap_canonical (map, fun, args, data)
+     map_keymap_function_t fun;
+     Lisp_Object map, args;
+     void *data;
+{
+  struct gcpro gcpro1;
+  GCPRO1 (args);
+  /* map_keymap_canonical may be used from redisplay (e.g. when building menus)
+     so be careful to ignore errors and to inhibit redisplay.  */
+  map = safe_call1 (Qkeymap_canonicalize, map);
+  /* No need to use `map_keymap' here because canonical map has no parent.  */
+  map_keymap_internal (map, fun, args, data);
+  UNGCPRO;
+}
+
+DEFUN ("map-keymap-internal", Fmap_keymap_internal, Smap_keymap_internal, 2, 2, 0,
+       doc: /* Call FUNCTION once for each event binding in KEYMAP.
+FUNCTION is called with two arguments: the event that is bound, and
+the definition it is bound to.  The event may be a character range.
+If KEYMAP has a parent, this function returns it without processing it.  */)
+     (function, keymap)
+     Lisp_Object function, keymap;
+{
+  struct gcpro gcpro1;
+  GCPRO1 (function);
+  keymap = get_keymap (keymap, 1, 1);
+  keymap = map_keymap_internal (keymap, map_keymap_call, function, NULL);
+  UNGCPRO;
+  return keymap;
+}
+
 DEFUN ("map-keymap", Fmap_keymap, Smap_keymap, 2, 3, 0,
        doc: /* Call FUNCTION once for each event binding in KEYMAP.
 FUNCTION is called with two arguments: the event that is bound, and
-the definition it is bound to.  If the event is an integer, it may be
-a generic character (see Info node `(elisp)Splitting Characters'), and
-that means that all actual character events belonging to that generic
-character are bound to the definition.
+the definition it is bound to.  The event may be a character range.
 
 If KEYMAP has a parent, the parent's bindings are included as well.
 This works recursively: if the parent has itself a parent, then the
@@ -730,12 +778,8 @@ usage: (map-keymap FUNCTION KEYMAP)  */)
      (function, keymap, sort_first)
      Lisp_Object function, keymap, sort_first;
 {
-  if (INTEGERP (function))
-    /* We have to stop integers early since map_keymap gives them special
-       significance.  */
-    xsignal1 (Qinvalid_function, function);
   if (! NILP (sort_first))
-    return call3 (intern ("map-keymap-internal"), function, keymap, Qt);
+    return call2 (intern ("map-keymap-sorted"), function, keymap);
 
   map_keymap (keymap, map_keymap_call, function, NULL, 1);
   return Qnil;
@@ -1280,9 +1324,9 @@ remapping in all currently active keymaps.  */)
     {
       Lisp_Object maps, binding;
 
-      for (maps = keymaps; !NILP (maps); maps = Fcdr (maps))
+      for (maps = keymaps; CONSP (maps); maps = XCDR (maps))
        {
-         binding = Flookup_key (Fcar (maps), command_remapping_vector, Qnil);
+         binding = Flookup_key (XCAR (maps), command_remapping_vector, Qnil);
          if (!NILP (binding) && !INTEGERP (binding))
            return binding;
        }
@@ -2047,7 +2091,8 @@ If KEYMAP is nil, that means no local keymap.  */)
 }
 
 DEFUN ("current-local-map", Fcurrent_local_map, Scurrent_local_map, 0, 0, 0,
-       doc: /* Return current buffer's local keymap, or nil if it has none.  */)
+       doc: /* Return current buffer's local keymap, or nil if it has none.
+Normally the local keymap is set by the major mode with `use-local-map'.  */)
      ()
 {
   return current_buffer->keymap;
@@ -2303,7 +2348,7 @@ spaces are put between sequence elements, etc.  */)
        }
       else if (VECTORP (list))
        {
-         key = AREF (list, i++);
+         key = AREF (list, i); i++;
        }
       else
        {
@@ -2580,6 +2625,41 @@ ascii_sequence_p (seq)
   return 1;
 }
 
+#ifdef HAVE_NS
+int lisp_to_mod(Lisp_Object lmod)
+/* -------------------------------------------------------------------------
+     Convert lisp symbol to emacs modifier code.
+   ------------------------------------------------------------------------- */
+{
+  if (EQ(lmod, Qmeta))
+    return meta_modifier;
+  else if (EQ(lmod, Qsuper))
+    return super_modifier;
+  else if (EQ(lmod, Qcontrol))
+    return ctrl_modifier;
+  else if (EQ(lmod, Qalt))
+    return alt_modifier;
+  else if (EQ(lmod, Qhyper))
+    return hyper_modifier;
+  return 0;
+}
+
+/* Return non-zero if SEQ starts w/a char modified by given modifier only. */
+static int
+modifier_sequence_p (Lisp_Object seq, Lisp_Object modifier)
+{
+  Lisp_Object idx, elt;
+
+  if (XINT (Flength (seq)) == 0)
+    return 0;
+  XSETFASTINT(idx, 0);
+  elt = Faref(seq, idx);
+
+  return  (XUINT(elt) & (CHAR_MODIFIER_MASK ^ shift_modifier))
+    == lisp_to_mod(modifier);
+}
+#endif
+
 \f
 /* where-is - finding a command in a set of keymaps.                   */
 
@@ -2653,7 +2733,7 @@ where_is_internal (definition, keymaps, firstonly, noindirect, no_remap)
       && !NILP (Fcommand_remapping (definition, Qnil, keymaps)))
     RETURN_UNGCPRO (Qnil);
 
-  for (; !NILP (maps); maps = Fcdr (maps))
+  for (; CONSP (maps); maps = XCDR (maps))
     {
       /* Key sequence to reach map, and the map that it reaches */
       register Lisp_Object this, map, tem;
@@ -2665,8 +2745,8 @@ where_is_internal (definition, keymaps, firstonly, noindirect, no_remap)
       Lisp_Object last;
       int last_is_meta;
 
-      this = Fcar (Fcar (maps));
-      map  = Fcdr (Fcar (maps));
+      this = Fcar (XCAR (maps));
+      map  = Fcdr (XCAR (maps));
       last = make_number (XINT (Flength (this)) - 1);
       last_is_meta = (XINT (last) >= 0
                      && EQ (Faref (this, last), meta_prefix_char));
@@ -2762,6 +2842,14 @@ where_is_internal (definition, keymaps, firstonly, noindirect, no_remap)
             we find.  */
          if (EQ (firstonly, Qnon_ascii))
            RETURN_UNGCPRO (sequence);
+#ifdef HAVE_NS
+          /* respond to modifier preference */
+          else if ((EQ (firstonly, Qalt) || EQ (firstonly, Qcontrol)
+                    || EQ (firstonly, Qhyper) || EQ (firstonly, Qmeta)
+                    || EQ (firstonly, Qsuper)))
+            if (modifier_sequence_p(sequence, firstonly))
+              RETURN_UNGCPRO (sequence);
+#endif
          else if (!NILP (firstonly) && ascii_sequence_p (sequence))
            RETURN_UNGCPRO (sequence);
 
@@ -2795,6 +2883,10 @@ If KEYMAP is a list of keymaps, search only those keymaps.
 
 If optional 3rd arg FIRSTONLY is non-nil, return the first key sequence found,
 rather than a list of all possible key sequences.
+#ifdef HAVE_NS
+If FIRSTONLY is the symbol for a modifier key, return the first binding found,
+that is modified by that modifier only.
+#endif
 If FIRSTONLY is the symbol `non-ascii', return the first binding found,
 no matter what it is.
 If FIRSTONLY has another non-nil value, prefer sequences of ASCII characters
@@ -2868,10 +2960,19 @@ remapped command in the returned list.  */)
       for (i = n - 1; i >= 0; --i)
        if (EQ (shadow_lookup (keymaps, defns[i], Qnil), definition))
          {
-           if (ascii_sequence_p (defns[i]))
-             break;
-           else if (j < 0)
-             j = i;
+#ifdef HAVE_NS
+            if ((EQ (firstonly, Qalt) || EQ (firstonly, Qcontrol)
+                 || EQ (firstonly, Qhyper) || EQ (firstonly, Qmeta)
+                 || EQ (firstonly, Qsuper))
+                && modifier_sequence_p(defns[i], firstonly))
+              break;
+            else if (EQ (firstonly, Qt) && ascii_sequence_p (defns[i]))
+#else
+              if (ascii_sequence_p (defns[i]))
+#endif
+                break;
+              else if (j < 0)
+                j = i;
          }
 
       result = i >= 0 ? defns[i] : (j >= 0 ? defns[j] : Qnil);
@@ -3155,11 +3256,11 @@ key             binding\n\
       Lisp_Object list;
 
       /* Delete from MAPS each element that is for the menu bar.  */
-      for (list = maps; !NILP (list); list = XCDR (list))
+      for (list = maps; CONSP (list); list = XCDR (list))
        {
          Lisp_Object elt, prefix, tem;
 
-         elt = Fcar (list);
+         elt = XCAR (list);
          prefix = Fcar (elt);
          if (XVECTOR (prefix)->size >= 1)
            {
@@ -3186,11 +3287,11 @@ key             binding\n\
       something = 1;
     }
 
-  for (; !NILP (maps); maps = Fcdr (maps))
+  for (; CONSP (maps); maps = XCDR (maps))
     {
       register Lisp_Object elt, prefix, tail;
 
-      elt = Fcar (maps);
+      elt = XCAR (maps);
       prefix = Fcar (elt);
 
       sub_shadows = Qnil;
@@ -3381,14 +3482,16 @@ describe_map (map, prefix, elt_describer, partial, shadow,
   kludge = Fmake_vector (make_number (1), Qnil);
   definition = Qnil;
 
+  GCPRO3 (prefix, definition, kludge);
+
+  map = call1 (Qkeymap_canonicalize, map);
+
   for (tail = map; CONSP (tail); tail = XCDR (tail))
     length_needed++;
 
   vect = ((struct describe_map_elt *)
          alloca (sizeof (struct describe_map_elt) * length_needed));
 
-  GCPRO3 (prefix, definition, kludge);
-
   for (tail = map; CONSP (tail); tail = XCDR (tail))
     {
       QUIT;
@@ -3824,6 +3927,9 @@ syms_of_keymap ()
   apropos_predicate = Qnil;
   apropos_accumulate = Qnil;
 
+  Qkeymap_canonicalize = intern ("keymap-canonicalize");
+  staticpro (&Qkeymap_canonicalize);
+
   /* Now we are ready to set up this property, so we can
      create char tables.  */
   Fput (Qkeymap, Qchar_table_extra_slots, make_number (0));
@@ -3890,11 +3996,11 @@ don't alter it yourself.  */);
   Fset_keymap_parent (Vminibuffer_local_must_match_map,
                      Vminibuffer_local_completion_map);
 
-  DEFVAR_LISP ("minibuffer-local-must-match-filename-map",
-              &Vminibuffer_local_must_match_filename_map,
+  DEFVAR_LISP ("minibuffer-local-filename-must-match-map",
+              &Vminibuffer_local_filename_must_match_map,
               doc: /* Local keymap for minibuffer input with completion for filenames with exact match.  */);
-  Vminibuffer_local_must_match_filename_map = Fmake_sparse_keymap (Qnil);
-  Fset_keymap_parent (Vminibuffer_local_must_match_filename_map,
+  Vminibuffer_local_filename_must_match_map = Fmake_sparse_keymap (Qnil);
+  Fset_keymap_parent (Vminibuffer_local_filename_must_match_map,
                      Vminibuffer_local_must_match_map);
 
   DEFVAR_LISP ("minor-mode-map-alist", &Vminor_mode_map_alist,
@@ -3966,6 +4072,7 @@ the same way.  The "active" keymaps in each alist are used before
   defsubr (&Sset_keymap_parent);
   defsubr (&Smake_keymap);
   defsubr (&Smake_sparse_keymap);
+  defsubr (&Smap_keymap_internal);
   defsubr (&Smap_keymap);
   defsubr (&Scopy_keymap);
   defsubr (&Scommand_remapping);