From: NeilBrown Date: Wed, 2 Dec 2015 07:54:15 +0000 (+1100) Subject: Better handling of formatting with render-complete. X-Git-Tag: lca2016~133 X-Git-Url: http://git.neil.brown.name/?a=commitdiff_plain;h=fd848a8ba88a1ec0d83ac6c7866d6004331c8658;p=edlib.git Better handling of formatting with render-complete. We highlight the common prefix and stripe all highlights and quoting before using the string. Signed-off-by: NeilBrown --- diff --git a/render-complete.c b/render-complete.c index 882da34a..7cf4440b 100644 --- a/render-complete.c +++ b/render-complete.c @@ -17,11 +17,31 @@ #include #include #include "core.h" +#include "misc.h" struct complete_data { char *prefix; }; +static char *add_highlight_prefix(char *orig, int plen, char *attr) +{ + struct buf ret; + + if (orig == NULL) + return orig; + buf_init(&ret); + buf_concat(&ret, attr); + while (plen > 0 && *orig) { + if (*orig == '<') + buf_append_byte(&ret, *orig++); + buf_append_byte(&ret, *orig++); + plen -= 1; + } + buf_concat(&ret, ""); + buf_concat(&ret, orig); + return buf_final(&ret); +} + DEF_CMD(render_complete_line) { /* The first line *must* match the prefix. @@ -30,7 +50,7 @@ DEF_CMD(render_complete_line) struct cmd_info ci2 = {0}; struct complete_data *cd = ci->home->data; struct doc *d = doc_from_pane(ci->home); - int plen; + int plen = strlen(cd->prefix); if (!d || !ci->mark) return -1; @@ -42,11 +62,11 @@ DEF_CMD(render_complete_line) ci2.numeric = ci->numeric; if (key_handle(&ci2) == 0) return 0; - ci->str = ci2.str; + ci->str = add_highlight_prefix(ci2.str, plen, ""); + free(ci2.str); if (ci->numeric != NO_NUMERIC) return 1; /* Need to continue over other matching lines */ - plen = strlen(cd->prefix); ci2.mark = mark_dup(ci->mark, 1); while (1) { ci2.numeric = ci->numeric; @@ -253,6 +273,7 @@ DEF_CMD(complete_return) struct cmd_info ci2 = {0}; char *str; int l; + char *c1, *c2; ci2.key = "render-line"; ci2.focus = ci->home; @@ -266,6 +287,21 @@ DEF_CMD(complete_return) l = strlen(str); if (l && str[l-1] == '\n') str[l-1] = 0; + c1 = c2 = str; + while (*c2) { + if (*c2 != '<') { + *c1++ = *c2++; + continue; + } + c2 += 1; + if (*c2 == '<') { + *c1++ = *c2++; + continue; + } + while (*c2 && c2[-1] != '>') + c2++; + } + *c1 = 0; memset(&ci2, 0, sizeof(ci2)); ci2.key = ci->key;