]> git.neil.brown.name Git - edlib.git/commitdiff
Avoid indexing in ci->key.
authorNeilBrown <neil@brown.name>
Thu, 13 Feb 2020 00:11:45 +0000 (11:11 +1100)
committerNeilBrown <neil@brown.name>
Thu, 13 Feb 2020 00:13:49 +0000 (11:13 +1100)
Most uses of ci->key[foo] use a magic constant which
isn't robust.
Check these to use ksuffix or strcmp() to make it clear
what the expected prefix is.

Signed-off-by: NeilBrown <neil@brown.name>
emacs-search.c
lib-history.c
lib-view.c
mode-emacs.c
render-complete.c

index 8dd66c73b147bc7a858a7ae14f93396fee2c5044..74b918fc4cd2325285b2db6bffd669f3adc66ea5 100644 (file)
@@ -224,7 +224,8 @@ DEF_CMD(search_add)
 
 DEF_CMD(search_insert_quoted)
 {
-       if (strchr(must_quote, ci->key[4]) == NULL)
+       const char *suffix = ksuffix(ci, "Chr-");
+       if (strchr(must_quote, suffix[0]) == NULL)
                return 0;
        call("Replace", ci->focus, 1, NULL, "\\");
        call("Replace", ci->focus, 1, NULL, ksuffix(ci, "Chr-"),
@@ -242,15 +243,15 @@ DEF_CMD(search_insert_meta)
        char *bracket;
        const char *brackets = "{}()[]";
        const char *k = ksuffix(ci, "M-Chr-");
-       if (strchr(may_quote, ci->key[6])) {
+       if (strchr(may_quote, *k)) {
                call("Replace", ci->focus, 1, NULL, "\\");
                call("Replace", ci->focus, 1, NULL, k,
                     1, NULL, ",auto=1");
                return 1;
        }
-       if (strchr(must_quote, ci->key[6]) == NULL || !ci->mark)
+       if (strchr(must_quote, *k) == NULL || !ci->mark)
                return 0;
-       bracket = strchr(brackets, ci->key[6]);
+       bracket = strchr(brackets, *k);
        if (!bracket) {
                call("Replace", ci->focus, 1, NULL, k);
        } else if ((bracket - brackets) % 2) {
@@ -442,7 +443,7 @@ DEF_CMD(search_replace)
        esi->replace_pane = p;
        if (p)
                home_call(esi->target, "highlight:set-popup", p, 1);
-       if (ci->key[6] == '%')
+       if (strcmp(ci->key, "M-Chr-%") == 0)
                pane_focus(ci->focus);
        else
                pane_focus(p);
index b5bb9fc8fb0bdd1e2296ebf5db8cd633ecec87ef..0269d6d495a0a86a64ab127c2497e8047287bd7c 100644 (file)
@@ -112,10 +112,11 @@ DEF_CMD(history_move)
        struct history_info *hi = ci->home->data;
        struct mark *m;
        char *l, *e;
+       const char *suffix = ksuffix(ci, "M-Chr-");
 
        if (!hi->history || !ci->mark)
                return 0;
-       if (ci->key[6] == 'p') {
+       if (*suffix == 'p') {
                m = mark_at_point(hi->history, NULL, MARK_UNGROUPED);
                call("Move-EOL", hi->history, -2);
        } else {
@@ -129,7 +130,7 @@ DEF_CMD(history_move)
        if (!l || !*l) {
                /* No more history */
                free(l);
-               if (ci->key[6] == 'p') {
+               if (*suffix == 'p') {
                        mark_free(m);
                        return 1;
                } else
index d3cf0bec2100e00ed84118fb91f7e48eb8df9642..308c54232e0df5c86c00154cae6294b2d5f67388 100644 (file)
@@ -451,7 +451,7 @@ DEF_CMD(view_scroll)
 {
        struct view_data *vd = ci->home->data;
 
-       if (ci->key[6] == '4')
+       if (strcmp(ci->key, "Press-4") == 0)
                vd->move_small -= 2;
        else
                vd->move_small += 2;
index 2dc2b9b7459d1abb294cdd83eb9ff8643ff1da8c..5b65eab404c790fcf2bb3a1bb9495de432a74a95 100644 (file)
@@ -1329,9 +1329,9 @@ DEF_CMD(emacs_start_search)
        attr_set_str(&p->attrs, "prompt", "Search");
        attr_set_str(&p->attrs, "done-key", "Search String");
        call("doc:set-name", p, 0, NULL, "Search", -1);
-       if (ci->key[6] == 'R')
+       if (strcmp(ci->key, "C-Chr-R") == 0)
                mode |= 1;
-       if (ci->key[6] == '%')
+       if (strcmp(ci->key, "M-Chr-%") == 0)
                mode |= 2;
        call_ret(pane, "attach-emacs-search", p, mode);
 
index 9307e3975e58d8486bb3faab6e3936b091c912a1..0df15afc0726386b30a5193f210039622774ef2b 100644 (file)
@@ -293,10 +293,11 @@ DEF_CMD(complete_char)
        char *np;
        struct call_return cr;
        int pl = strlen(cd->prefix);
+       const char *suffix = ksuffix(ci, "Chr-");
 
        np = malloc(pl + 2);
        strcpy(np, cd->prefix);
-       np[pl] = ci->key[4];
+       np[pl] = *suffix;
        np[pl+1] = 0;
        cr = call_ret(all, "Complete:prefix", ci->focus, !cd->prefix_only, NULL, np);
        if (cr.i == 0) {