save state in completed objects
[protos/libecoli.git] / lib / main-readline.c
index 80ef88d..141c8cd 100644 (file)
@@ -58,6 +58,8 @@ static char *my_completion_entry(const char *s, int state)
        static struct ec_completed *c;
        static struct ec_completed_iter *iter;
        const struct ec_completed_item *item;
+       enum ec_completed_type item_type;
+       const char *item_str, *item_display;
 
        (void)s;
 
@@ -81,7 +83,7 @@ static char *my_completion_entry(const char *s, int state)
                        return NULL;
 
                ec_completed_iter_free(iter);
-               iter = ec_completed_iter(c, EC_MATCH | EC_PARTIAL_MATCH);
+               iter = ec_completed_iter(c, EC_COMP_FULL | EC_PARTIAL_MATCH);
                if (iter == NULL)
                        return NULL;
        }
@@ -90,23 +92,26 @@ static char *my_completion_entry(const char *s, int state)
        if (item == NULL)
                return NULL;
 
+       item_str = ec_completed_item_get_str(item);
        if (c->count_match == 1) {
 
                /* don't add the trailing space for partial completions */
                if (state == 0) {
-                       if (item->type == EC_MATCH)
+                       item_type = ec_completed_item_get_type(item);
+                       if (item_type == EC_COMP_FULL)
                                rl_completion_suppress_append = 0;
                        else
                                rl_completion_suppress_append = 1;
                }
 
-               return strdup(item->str);
+               return strdup(item_str);
        } else if (rl_completion_type == '?') {
                /* on second try only show the display part */
-               return strdup(item->display);
+               item_display = ec_completed_item_get_display(item);
+               return strdup(item_display);
        }
 
-       return strdup(item->str);
+       return strdup(item_str);
 }
 
 static char **my_attempted_completion(const char *text, int start, int end)
@@ -121,29 +126,36 @@ static char **my_attempted_completion(const char *text, int start, int end)
 }
 
 /* this function builds the help string */
-static char *get_node_help(const struct ec_completed_node *compnode)
+static char *get_node_help(const struct ec_completed_item *item)
 {
-       const struct ec_completed_item *item;
+       const struct ec_completed_group *grp;
+       struct ec_parsed *state; // XXX keep const with macro
        const struct ec_node *node;
        char *help = NULL;
        const char *node_help = NULL;
        const char *node_desc = NULL;
-       size_t i;
-
-       /* Since we display only one help per node, only look at the first item
-        * to get the path. The objective is to retrieve the most precise
-        * help for this node. */
-       item = TAILQ_FIRST(&compnode->items);
-       for (i = 0; i < item->pathlen; i++) {
-               node = item->path[i];
+//     size_t i;
+
+       (void)item;
+#if 1
+       grp = ec_completed_item_get_grp(item);
+       state = grp->state;
+       for (state = grp->state; state != NULL;
+            state = ec_parsed_get_parent(state)) {
+               node = ec_parsed_get_node(state);
                if (node_help == NULL)
                        node_help = ec_keyval_get(ec_node_attrs(node), "help");
                if (node_desc == NULL)
                        node_desc = ec_node_desc(node);
        }
+#else
+       node = ec_completed_item_get_node(item);
+       node_help = ec_keyval_get(ec_node_attrs(node), "help");
+       node_desc = ec_node_desc(node);
+#endif
 
        if (node_help == NULL)
-               node_help = "";
+               node_help = "-";
        if (node_desc == NULL)
                return NULL;
 
@@ -155,11 +167,13 @@ static char *get_node_help(const struct ec_completed_node *compnode)
 
 static int show_help(int ignore, int invoking_key)
 {
-       const struct ec_completed_node *compnode;
+       struct ec_completed_iter *iter;
+       const struct ec_completed_group *grp, *prev_grp = NULL;
+       const struct ec_completed_item *item;
        struct ec_completed *c;
        struct ec_parsed *p;
-       char *line;
-       unsigned int count, i;
+       char *line = NULL;
+       unsigned int count;
        char **helps = NULL;
        int match = 0;
 
@@ -168,54 +182,76 @@ static int show_help(int ignore, int invoking_key)
 
        line = strdup(rl_line_buffer);
        if (line == NULL)
-               return 1;
+               goto fail;
 
        /* check if the current line matches */
        p = ec_node_parse(commands, line);
        if (ec_parsed_matches(p))
                match = 1;
        ec_parsed_free(p);
+       p = NULL;
 
        /* complete at current cursor position */
        line[rl_point] = '\0';
        c = ec_node_complete(commands, line);
-       //ec_completed_dump(stdout, c);
        free(line);
+       line = NULL;
        if (c == NULL)
-               return 1;
+               goto fail;
+
+       //ec_completed_dump(stdout, c);
 
        /* let's display one contextual help per node */
        count = 0;
-       TAILQ_FOREACH(compnode, &c->nodes, next) {
-               if (TAILQ_EMPTY(&compnode->items))
-                       continue;
-               count++;
-       }
+       iter = ec_completed_iter(c,
+               EC_COMP_UNKNOWN | EC_COMP_FULL | EC_PARTIAL_MATCH);
+       if (iter == NULL)
+               goto fail;
 
-       helps = calloc(count + match + 1, sizeof(char *));
+       /* strangely, rl_display_match_list() expects first index at 1 */
+       helps = calloc(match + 1, sizeof(char *));
        if (helps == NULL)
-               return 1;
-
+               goto fail;
        if (match)
                helps[1] = "<return>";
 
-       /* strangely, rl_display_match_list() expects first index at 1 */
-       i = match + 1;
-       TAILQ_FOREACH(compnode, &c->nodes, next) {
-               if (TAILQ_EMPTY(&compnode->items))
+       while ((item = ec_completed_iter_next(iter)) != NULL) {
+               char **tmp;
+
+               /* keep one help per group, skip other items  */
+               grp = ec_completed_item_get_grp(item);
+               if (grp == prev_grp)
                        continue;
-               helps[i++] = get_node_help(compnode);
+
+               prev_grp = grp;
+
+               tmp = realloc(helps, (count + match + 2) * sizeof(char *));
+               if (tmp == NULL)
+                       goto fail;
+               helps = tmp;
+               helps[count + match + 1] = get_node_help(item);
+               count++;
        }
 
+       ec_completed_iter_free(iter);
        ec_completed_free(c);
-
        rl_display_match_list(helps, count + match, 1000); /* XXX 1000 */
-
        rl_forced_update_display();
 
        return 0;
 
-       // free helps[n] XXX on error ?
+fail:
+       ec_completed_iter_free(iter);
+       ec_parsed_free(p);
+       free(line);
+       ec_completed_free(c);
+       if (helps != NULL) {
+               while (count--)
+                       free(helps[count + match + 1]);
+       }
+       free(helps);
+
+       return 1;
 }
 
 static int create_commands(void)
@@ -260,7 +296,7 @@ static int create_commands(void)
        ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "name")), "help",
                "the person to greet", NULL);
        ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "count")), "help",
-               "how many times to greet", NULL);
+               "how many times to greet (0-10)", NULL);
        if (ec_node_or_add(cmdlist, cmd) < 0)
                goto fail;