ratpoison-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[RP] The `link' command, ported to current CVS


From: Gergely Nagy
Subject: [RP] The `link' command, ported to current CVS
Date: Thu Oct 18 14:21:19 2001
User-agent: Mutt/1.3.23i

Greetings!

I forward ported my `link' command to current CVS, patch is attached
below.

This command can be used to link one keybinding to another. Well,
let's seen an example:

bind o exec oh-my-god
bind C-o o
bind o exec oh-something-else

This will result in C-t C-o executing oh-something-else. I hope it's
clear, if not, see the source, it's pretty trivial :)

diff -urNad ratpoison/doc/ratpoison.texi ratpoison-link/doc/ratpoison.texi
--- ratpoison/doc/ratpoison.texi        Thu Oct 18 22:48:18 2001
+++ ratpoison-link/doc/ratpoison.texi   Thu Oct 18 23:15:05 2001
@@ -491,6 +491,15 @@
 @item lastmsg
 Display the last message.
 
address@hidden link @var{key}
+Do whatever @var{key} would do. Useful for making command aliases,
+for example, to make @kbd{C-t C-c} behave exactly the same as
address@hidden c}, no matter what the latter is:
+
address@hidden
+: bind C-c link c
address@hidden example
+
 @item redisplay
 Redisplay the current window, just like @kbd{C-t l} would do.
 
diff -urNad ratpoison/src/actions.c ratpoison-link/src/actions.c
--- ratpoison/src/actions.c     Thu Oct 18 22:48:26 2001
+++ ratpoison-link/src/actions.c        Thu Oct 18 23:13:56 2001
@@ -77,6 +77,7 @@
     {"lastmsg",                cmd_lastmsg,    arg_VOID},
     {"restart",                cmd_restart,    arg_VOID},
     {"startup_message",        cmd_startup_message, arg_STRING},
+    {"link",           cmd_link,       arg_STRING},
 
     /* Commands to set default behavior. */
     {"defbarloc",              cmd_defbarloc,          arg_STRING},
@@ -139,6 +140,45 @@
   return NULL;
 }
 
+static char *
+find_command_by_keydesc (char *desc)
+{
+  int i = 0;
+  char *keysym_name;
+
+  while (i < key_actions_last)
+    {
+      keysym_name = keysym_to_string (key_actions[i].key, 
key_actions[i].state);
+      if (!strcmp (keysym_name, desc))
+        {
+          free (keysym_name);
+          return key_actions[i].data;
+        }
+      free (keysym_name);
+      i++;
+    }
+
+  return NULL;
+}
+
+static char *
+resolve_command_from_keydesc (char *desc, int depth)
+{
+  char *cmd, *command;
+
+  command = find_command_by_keydesc (desc);
+  if (!command)
+    return NULL;
+
+  /* is it a link? */
+  if (strncmp (command, "link", 4) || depth > MAX_LINK_DEPTH)
+    /* it is not */
+    return command;
+
+  cmd = resolve_command_from_keydesc (&command[5], depth + 1);
+  return (cmd != NULL) ? cmd : command;
+}
+
 static void
 add_keybinding (KeySym keysym, int state, char *cmd)
 {
@@ -1227,22 +1267,24 @@
                       keysym_name, strlen (keysym_name));
 
          if (XTextWidth (defaults.font, keysym_name, strlen (keysym_name)) > 
max_width)
-           {
              max_width = XTextWidth (defaults.font, keysym_name, strlen 
(keysym_name));
-           }
 
          free (keysym_name);
        }
       else
        {
-         XDrawString (dpy, s->help_window, s->normal_gc,
-                      x, y + defaults.font->max_bounds.ascent,
-                      key_actions[i].data, strlen (key_actions[i].data));
-
-         if (XTextWidth (defaults.font, key_actions[i].data, strlen 
(key_actions[i].data)) > max_width)
-           {
-             max_width = XTextWidth (defaults.font, key_actions[i].data, 
strlen (key_actions[i].data));
-           }
+         char *cmd;
+         cmd = resolve_command_from_keydesc (
+                       keysym_to_string (key_actions[i].key,
+                                         key_actions[i].state), 0);
+          if (cmd)
+            {
+              XDrawString (dpy, s->help_window, s->normal_gc, x,
+                           y + defaults.font->max_bounds.ascent, cmd,
+                           strlen (cmd));
+              if (XTextWidth (defaults.font, cmd, strlen (cmd)) > max_width)
+                max_width = XTextWidth (defaults.font, cmd, strlen (cmd));
+            }
        }
 
       y += FONT_HEIGHT (defaults.font);
@@ -1937,4 +1979,19 @@
     message (" focuslast: No other frame ");
 
   return NULL;
+}
+
+char *
+cmd_link (int interactive, void *data)
+{
+  char *cmd = NULL;
+
+  if (!data)
+    return NULL;
+
+  cmd = resolve_command_from_keydesc ((char *)data, 0);
+  if (cmd)
+    command (interactive, cmd);
+
+ return NULL;
 }
diff -urNad ratpoison/src/actions.h ratpoison-link/src/actions.h
--- ratpoison/src/actions.h     Thu Oct 18 22:48:26 2001
+++ ratpoison-link/src/actions.h        Thu Oct 18 23:14:26 2001
@@ -102,6 +102,7 @@
 char * cmd_restart (int interactive, void *data);
 char * cmd_startup_message (int interactive, void *data);
 char * cmd_focuslast (int interactive, void *data);
+char * cmd_link (int interactive, void *data);
 
 /* void cmd_xterm (void *data); */
 
diff -urNad ratpoison/src/conf.h ratpoison-link/src/conf.h
--- ratpoison/src/conf.h        Thu Oct 18 22:48:26 2001
+++ ratpoison-link/src/conf.h   Thu Oct 18 23:15:32 2001
@@ -47,4 +47,7 @@
    mostly for use with hand-helds. */
 #define UNMANAGED_WINDOW_LIST "xapm","xclock","xscribble"
 
+/* Maximum depth of a link */
+#define MAX_LINK_DEPTH 16
+
 #endif /* !_ _RATPOISON_CONF_H */

Attachment: pgpCXGdxuYlhC.pgp
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]