Code

screen_keydef: support adding keys with CMD_ADD
[ncmpc.git] / src / command.c
index 6d06368123e47918c02dcd083eae8e0b7a124cbe..85e499a00873ad2c985dbfbb1241c26b7b1006f2 100644 (file)
@@ -241,8 +241,10 @@ static command_definition_t cmds[] = {
          N_("Interrupt action") },
        { {'u', 0, 0 }, 0, CMD_LYRICS_UPDATE, "lyrics-update",
          N_("Update Lyrics") },
-       { {'e', 0, 0 }, 0, CMD_LYRICS_EDIT, "lyrics-edit",
-         N_("Edit Lyrics") },
+       /* this command may move out of #ifdef ENABLE_LYRICS_SCREEN
+          at some point */
+       { {'e', 0, 0 }, 0, CMD_EDIT, "edit",
+         N_("Edit the current item") },
 #endif
 
 #ifdef ENABLE_OUTPUTS_SCREEN
@@ -260,6 +262,30 @@ get_command_definitions(void)
 {
        return cmds;
 }
+
+size_t
+get_cmds_max_name_width(command_definition_t *c)
+{
+       static size_t max = 0;
+
+       if (max != 0)
+               return max;
+
+       size_t len;
+       command_definition_t *p;
+
+       for (p = c; p->name != NULL; p++) {
+               /*
+                * width and length are considered the same here, as command
+                * names are not translated.
+                */
+               len = (size_t) strlen(p->name);
+               if (len > max)
+                       max = len;
+       }
+
+       return max;
+}
 #endif
 
 const char *
@@ -326,57 +352,46 @@ key2str(int key)
 void
 command_dump_keys(void)
 {
-       int i = 0;
-
-       while (cmds[i].description) {
+       for (int i = 0; cmds[i].description; i++)
                if (cmds[i].command != CMD_NONE)
-                       printf(" %20s : %s\n", get_key_names(cmds[i].command,1),cmds[i].name);
-               i++;
-       }
+                       printf(" %20s : %s\n",
+                              get_key_names(cmds[i].command, true),
+                              cmds[i].name);
 }
 
 #ifndef NCMPC_MINI
 
-static int
+static void
 set_key_flags(command_definition_t *cp, command_t command, int flags)
 {
-       int i = 0;
-
-       while (cp[i].name) {
+       for (int i = 0; cp[i].name; i++) {
                if (cp[i].command == command) {
                        cp[i].flags |= flags;
-                       return 0;
+                       break;
                }
-               i++;
        }
-
-       return 1;
 }
 
 #endif
 
 const char *
-get_key_names(command_t command, int all)
+get_key_names(command_t command, bool all)
 {
-       int i = 0;
-
-       while (cmds[i].description) {
+       for (int i = 0; cmds[i].description; i++) {
                if (cmds[i].command == command) {
-                       int j;
                        static char keystr[80];
 
                        g_strlcpy(keystr, key2str(cmds[i].keys[0]), sizeof(keystr));
                        if (!all)
                                return keystr;
-                       j=1;
-                       while (j < MAX_COMMAND_KEYS && cmds[i].keys[j] > 0) {
+
+                       for (int j = 1; j < MAX_COMMAND_KEYS &&
+                                       cmds[i].keys[j] > 0; j++) {
                                g_strlcat(keystr, " ", sizeof(keystr));
                                g_strlcat(keystr, key2str(cmds[i].keys[j]), sizeof(keystr));
-                               j++;
                        }
                        return keystr;
                }
-               i++;
        }
        return NULL;
 }
@@ -384,13 +399,9 @@ get_key_names(command_t command, int all)
 const char *
 get_key_description(command_t command)
 {
-       int i = 0;
-
-       while (cmds[i].description) {
+       for (int i = 0; cmds[i].description; i++)
                if (cmds[i].command == command)
                        return _(cmds[i].description);
-               i++;
-       }
 
        return NULL;
 }
@@ -398,26 +409,19 @@ get_key_description(command_t command)
 const char *
 get_key_command_name(command_t command)
 {
-       int i = 0;
-
-       while (cmds[i].name) {
+       for (int i = 0; cmds[i].name; i++)
                if (cmds[i].command == command)
                        return cmds[i].name;
-               i++;
-       }
+
        return NULL;
 }
 
 command_t
 get_key_command_from_name(char *name)
 {
-       int i = 0;
-
-       while (cmds[i].name) {
+       for (int i = 0; cmds[i].name; i++)
                if (strcmp(name, cmds[i].name) == 0)
                        return cmds[i].command;
-               i++;
-       }
 
        return CMD_NONE;
 }
@@ -425,17 +429,13 @@ get_key_command_from_name(char *name)
 command_t
 find_key_command(int key, command_definition_t *c)
 {
-       int i = 0;
-
        assert(key != 0);
        assert(c != NULL);
 
-       while (c[i].name) {
-               if (c[i].keys[0] == key ||
-                   c[i].keys[1] == key ||
-                   c[i].keys[2] == key)
-                       return c[i].command;
-               i++;
+       for (int i = 0; c[i].name; i++) {
+               for (int j = 0; j < MAX_COMMAND_KEYS; j++)
+                       if (c[i].keys[j] == key)
+                               return c[i].command;
        }
 
        return CMD_NONE;
@@ -467,9 +467,7 @@ get_keyboard_command(void)
 int
 assign_keys(command_t command, int keys[MAX_COMMAND_KEYS])
 {
-       int i = 0;
-
-       while (cmds[i].name) {
+       for (int i = 0; cmds[i].name; i++) {
                if (cmds[i].command == command) {
                        memcpy(cmds[i].keys, keys, sizeof(int)*MAX_COMMAND_KEYS);
 #ifndef NCMPC_MINI
@@ -477,8 +475,8 @@ assign_keys(command_t command, int keys[MAX_COMMAND_KEYS])
 #endif
                        return 0;
                }
-               i++;
        }
+
        return -1;
 }
 
@@ -530,19 +528,17 @@ check_key_bindings(command_definition_t *cp, char *buf, size_t bufsize)
 int
 write_key_bindings(FILE *f, int flags)
 {
-       int i = 0, j;
-
        if (flags & KEYDEF_WRITE_HEADER)
                fprintf(f, "## Key bindings for ncmpc (generated by ncmpc)\n\n");
 
-       while (cmds[i].name && !ferror(f)) {
+       for (int i = 0; cmds[i].name && !ferror(f); i++) {
                if (cmds[i].flags & COMMAND_KEY_MODIFIED ||
                    flags & KEYDEF_WRITE_ALL) {
                        fprintf(f, "## %s\n", cmds[i].description);
                        if (flags & KEYDEF_COMMENT_ALL)
                                fprintf(f, "#");
                        fprintf(f, "key %s = ", cmds[i].name);
-                       for (j = 0; j < MAX_COMMAND_KEYS; j++) {
+                       for (int j = 0; j < MAX_COMMAND_KEYS; j++) {
                                if (j && cmds[i].keys[j])
                                        fprintf(f, ",  ");
                                if (!j || cmds[i].keys[j]) {
@@ -555,10 +551,9 @@ write_key_bindings(FILE *f, int flags)
                        }
                        fprintf(f,"\n\n");
                }
-               i++;
        }
 
        return ferror(f);
 }
 
-#endif
+#endif /* NCMPC_MINI */