Skip to content
This repository
Browse code

Add new command unbindkey for removing keybindings

  • Loading branch information...
commit 8a921b5e61689654774fef672165f4c56c01036d 1 parent 5643f99
Leonid Grinberg authored March 21, 2010
39  commands.c
@@ -90,7 +90,16 @@ const owl_cmd commands_to_init[]
90 90
 	      "Binds a key sequence to a command within a keymap.\n"
91 91
 	      "Use 'show keymaps' to see the existing keymaps.\n"
92 92
 	      "Key sequences may be things like M-C-t or NPAGE.\n\n"
93  
-	      "Ex.: bindkey recv C-b command zwrite -c barnowl"),
  93
+	      "Ex.: bindkey recv C-b command zwrite -c barnowl"
  94
+              "SEE ALSO: bindkey"),
  95
+
  96
+  OWLCMD_ARGS("unbindkey", owl_command_unbindkey, OWL_CTX_ANY,
  97
+	      "removes a binding in a keymap",
  98
+	      "bindkey <keymap> <keyseq>",
  99
+	      "Removes a binding of a key sequence within a keymap.\n"
  100
+	      "Use 'show keymaps' to see the existing keymaps.\n"
  101
+	      "Ex.: unbindkey recv H"
  102
+              "SEE ALSO: bindkey"),
94 103
 
95 104
   OWLCMD_ARGS("zwrite", owl_command_zwrite, OWL_CTX_INTERACTIVE,
96 105
 	      "send a zephyr",
@@ -1672,6 +1681,34 @@ char *owl_command_bindkey(int argc, const char *const *argv, const char *buff)
1672 1681
   return NULL;
1673 1682
 }
1674 1683
 
  1684
+
  1685
+char *owl_command_unbindkey(int argc, const char *const *argv)
  1686
+{
  1687
+  owl_keymap *km;
  1688
+  int ret;
  1689
+
  1690
+  if (argc < 3) {
  1691
+    owl_function_makemsg("Usage: bindkey <keymap> <binding>");
  1692
+    return NULL;
  1693
+  }
  1694
+  km = owl_keyhandler_get_keymap(owl_global_get_keyhandler(&g), argv[1]);
  1695
+  if (!km) {
  1696
+    owl_function_makemsg("No such keymap '%s'", argv[1]);
  1697
+    return NULL;
  1698
+  }
  1699
+  ret = owl_keymap_remove_binding(km, argv[2]);
  1700
+  if (ret == -1) {
  1701
+    owl_function_makemsg("Unable to unbind '%s' in keymap '%s'.",
  1702
+			 argv[2], argv[1]);
  1703
+    return NULL;
  1704
+  } else if (ret == -2) {
  1705
+    owl_function_makemsg("No such binding '%s' in keymap '%s'.",
  1706
+                         argv[2], argv[1]);
  1707
+  }
  1708
+  return NULL;
  1709
+}
  1710
+
  1711
+
1675 1712
 void owl_command_quit(void)
1676 1713
 {
1677 1714
   owl_function_quit();
2  keybinding.c
@@ -45,7 +45,7 @@ int owl_keybinding_make_keys(owl_keybinding *kb, const char *keyseq)
45 45
     return(-1);
46 46
   }
47 47
   kb->keys = owl_malloc(nktokens*sizeof(int));
48  
-  for (i = 0; i < nktokens; i++) {
  48
+  for (i=0; i<nktokens; i++) {
49 49
     kb->keys[i] = owl_keypress_fromstring(ktokens[i]);
50 50
     if (kb->keys[i] == ERR) { 
51 51
       atokenize_delete(ktokens, nktokens);
25  keymap.c
@@ -50,8 +50,33 @@ int owl_keymap_create_binding(owl_keymap *km, const char *keyseq, const char *co
50 50
     }
51 51
   }
52 52
   return owl_list_append_element(&km->bindings, kb);  
  53
+
53 54
 }
54 55
 
  56
+/* removes the binding associated with the keymap */
  57
+int owl_keymap_remove_binding(owl_keymap *km, const char *keyseq)
  58
+{
  59
+  owl_keybinding *kb, *curkb;
  60
+  int i;
  61
+
  62
+  if ((kb = owl_malloc(sizeof(owl_keybinding))) == NULL) return(-1);
  63
+  if (0 != owl_keybinding_make_keys(kb, keyseq)) {
  64
+    owl_free(kb);
  65
+    return(-1);
  66
+  }
  67
+
  68
+  for (i = owl_list_get_size(&km->bindings)-1; i >= 0; i--) {
  69
+    curkb = owl_list_get_element(&km->bindings, i);
  70
+    if (owl_keybinding_equal(curkb, kb)) {
  71
+      owl_list_remove_element(&km->bindings, i);
  72
+      owl_keybinding_delete(curkb);
  73
+      return(0);
  74
+    }
  75
+  }
  76
+  return(-2);
  77
+}
  78
+
  79
+
55 80
 /* returns a summary line describing this keymap.  the caller must free. */
56 81
 char *owl_keymap_summary(const owl_keymap *km)
57 82
 {

0 notes on commit 8a921b5

Please sign in to comment.
Something went wrong with that request. Please try again.