<<< Date Index >>>     <<< Thread Index >>>

[broken-patch] unbind command



Ok.. I don't have any more time to work on this today, but it's not
functional yet.  The goal is to add an "unbind" command to reverse the
effect of a "bind" or "macro" command.

I'm posting the code now, in case it would provide the spark or basis
for someone else to really implement this thing.  Rodrigo, if you want
to take the ball, please do. :)

Ja,
 Allister

-- 
Allister MacLeod <amacleod@xxxxxxxx> | http://amacleod.is-a-geek.org/
 Elen síla lúmenn'omentielvo.
Common subdirectories: /home/amacleod/src/mutt/autom4te.cache and 
./autom4te.cache
Common subdirectories: /home/amacleod/src/mutt/contrib and ./contrib
Common subdirectories: /home/amacleod/src/mutt/CVS and ./CVS
Common subdirectories: /home/amacleod/src/mutt/doc and ./doc
Common subdirectories: /home/amacleod/src/mutt/imap and ./imap
diff -u /home/amacleod/src/mutt/init.h ./init.h
--- /home/amacleod/src/mutt/init.h      2003-09-04 11:10:09.000000000 -0400
+++ ./init.h    2003-12-12 10:09:36.000000000 -0500
@@ -2799,6 +2799,7 @@
   { "unalias",         parse_unalias,          0 },
   { "unalternative_order",parse_unlist,                UL 
&AlternativeOrderList },
   { "unauto_view",     parse_unlist,           UL &AutoViewList },
+  { "unbind",           mutt_parse_unbind,      0 },
   { "unhdr_order",     parse_unlist,           UL &HeaderOrderList },
   { "unhook",          mutt_parse_unhook,      0 },
   { "unignore",                parse_unignore,         0 },
Common subdirectories: /home/amacleod/src/mutt/intl and ./intl
diff -u /home/amacleod/src/mutt/keymap.c ./keymap.c
--- /home/amacleod/src/mutt/keymap.c    2003-07-24 14:40:50.000000000 -0400
+++ ./keymap.c  2003-12-12 10:32:43.000000000 -0500
@@ -248,6 +248,51 @@
   km_bind (s, menu, op, NULL, NULL);
 }
 
+/* delete a keybinding from a map altogether */
+void km_unbind (char *s, int menu)
+{
+  struct keymap_t *cur, *last = 0, *next = 0;
+  keycode_t buf[MAX_SEQ];
+  int len, pos = 0;
+
+  len = parsekeys (s, buf, MAX_SEQ);
+
+  cur = Keymaps[menu];
+  while (cur)
+  {
+    if (pos >= len || pos >= cur->len) /* longest match, for better or worse */
+    {
+      if (len == cur->len) /* exact match */
+      {
+       next = cur->next;
+       if (last)
+       {
+         if (last->eq > cur->eq)
+           last->eq = cur->eq;
+         last->next = next;
+       }
+       else
+         Keymaps[menu] = next;
+       FREE (&cur->macro);
+       FREE (&cur->keys);
+       FREE (&cur->descr);
+       FREE (&cur);
+      }
+      return;
+    }
+    else if (buf[pos] < cur->keys[pos]) /* gone too far, it doesn't exist */
+      return;
+    else if (buf[pos] == cur->keys[pos])
+      pos++;
+    else /* (buf[pos] > cur->keys[pos]), so walk the list */
+    {
+      cur = cur->next;
+      if (pos > cur->eq)
+       pos = cur->eq;
+    }
+  }
+}
+
 static int get_op (struct binding_t *bindings, const char *start, size_t len)
 {
   int i;
@@ -793,7 +838,7 @@
     r = -1;
   }
   else if (ascii_strcasecmp ("noop", buf->data) == 0)
-    km_bindkey (key, menu, OP_NULL); /* the `unbind' command */
+    km_bindkey (key, menu, OP_NULL);
   else
   {
     /* First check the "generic" list of commands */
@@ -813,6 +858,29 @@
   return (r);
 }
 
+/* unbind menu-name '<key_sequence>' */
+int mutt_parse_unbind (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
+{
+  int menu;
+  char *key;
+  int r = 0;
+
+  if ((key = parse_keymap (&menu, s, err)) == NULL)
+    return (-1);
+
+  if (MoreArgs (s))
+  {
+    strfcpy (err->data, _("unbind: too many arguments"), err->dsize);
+    r = -1;
+  }
+  else
+  {
+    km_unbind (key, menu);
+  }
+  FREE (&key);
+  return (r);
+}
+
 /* macro <menu> <key> <macro> <description> */
 int mutt_parse_macro (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
 {
Common subdirectories: /home/amacleod/src/mutt/m4 and ./m4
diff -u /home/amacleod/src/mutt/PATCHES ./PATCHES
--- /home/amacleod/src/mutt/PATCHES     2002-12-09 12:44:54.000000000 -0500
+++ ./PATCHES   2003-12-12 10:52:24.000000000 -0500
@@ -0,0 +1 @@
+patch-1.5.5.1.am.unbind.1
Common subdirectories: /home/amacleod/src/mutt/po and ./po
diff -u /home/amacleod/src/mutt/protos.h ./protos.h
--- /home/amacleod/src/mutt/protos.h    2003-10-04 16:34:59.000000000 -0400
+++ ./protos.h  2003-12-12 10:10:01.000000000 -0500
@@ -292,6 +292,7 @@
 int mutt_needs_mailcap (BODY *);
 int mutt_num_postponed (int);
 int mutt_parse_bind (BUFFER *, BUFFER *, unsigned long, BUFFER *);
+int mutt_parse_unbind (BUFFER *, BUFFER *, unsigned long, BUFFER *);
 int mutt_parse_exec (BUFFER *, BUFFER *, unsigned long, BUFFER *);
 int mutt_parse_color (BUFFER *, BUFFER *, unsigned long, BUFFER *);
 int mutt_parse_uncolor (BUFFER *, BUFFER *, unsigned long, BUFFER *);