Allow multiple substitutions in a single format.

pull/1985/head
nicm 2019-11-25 15:02:48 +00:00
parent 20c1f1aec6
commit 5d0504ee11
1 changed files with 19 additions and 14 deletions

View File

@ -1560,15 +1560,15 @@ static int
format_replace(struct format_tree *ft, const char *key, size_t keylen,
char **buf, size_t *len, size_t *off)
{
struct window_pane *wp = ft->wp;
const char *errptr, *copy, *cp, *marker = NULL;
char *copy0, *condition, *found, *new;
char *value, *left, *right;
size_t valuelen;
int modifiers = 0, limit = 0, j;
struct format_modifier *list, *fm, *cmp = NULL, *search = NULL;
struct format_modifier *sub = NULL;
u_int i, count;
struct window_pane *wp = ft->wp;
const char *errptr, *copy, *cp, *marker = NULL;
char *copy0, *condition, *found, *new;
char *value, *left, *right;
size_t valuelen;
int modifiers = 0, limit = 0, j;
struct format_modifier *list, *fm, *cmp = NULL, *search = NULL;
struct format_modifier **sub = NULL;
u_int i, count, nsub = 0;
/* Make a copy of the key. */
copy = copy0 = xstrndup(key, keylen);
@ -1597,7 +1597,9 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen,
case 's':
if (fm->argc < 2)
break;
sub = fm;
sub = xreallocarray (sub, nsub + 1,
sizeof *sub);
sub[nsub++] = fm;
break;
case '=':
if (fm->argc < 1)
@ -1809,10 +1811,10 @@ done:
}
/* Perform substitution if any. */
if (sub != NULL) {
left = format_expand(ft, sub->argv[0]);
right = format_expand(ft, sub->argv[1]);
new = format_sub(sub, value, left, right);
for (i = 0; i < nsub; i++) {
left = format_expand(ft, sub[i]->argv[0]);
right = format_expand(ft, sub[i]->argv[1]);
new = format_sub(sub[i], value, left, right);
format_log(ft, "substitute '%s' to '%s': %s", left, right, new);
free(value);
value = new;
@ -1855,12 +1857,15 @@ done:
format_log(ft, "replaced '%s' with '%s'", copy0, value);
free(value);
free(sub);
format_free_modifiers(list, count);
free(copy0);
return (0);
fail:
format_log(ft, "failed %s", copy0);
free(sub);
format_free_modifiers(list, count);
free(copy0);
return (-1);