Skip to content

Commit

Permalink
patch 8.1.0218: cannot add matches to another window
Browse files Browse the repository at this point in the history
Problem:    Cannot add matches to another window. (Qiming Zhao)
Solution:   Add the "window" argument to matchadd() and matchaddpos().
            (closes vim#3260)
  • Loading branch information
brammool committed Jul 28, 2018
1 parent fd24946 commit 95e5147
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 27 deletions.
4 changes: 3 additions & 1 deletion runtime/doc/eval.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6016,7 +6016,7 @@ match({expr}, {pat} [, {start} [, {count}]]) *match()*
the pattern. 'smartcase' is NOT used. The matching is always
done like 'magic' is set and 'cpoptions' is empty.

*matchadd()* *E798* *E799* *E801*
*matchadd()* *E798* *E799* *E801* *E957*
matchadd({group}, {pattern} [, {priority} [, {id} [, {dict}]]])
Defines a pattern to be highlighted in the current window (a
"match"). It will be highlighted with {group}. Returns an
Expand Down Expand Up @@ -6055,6 +6055,8 @@ matchadd({group}, {pattern} [, {priority} [, {id} [, {dict}]]])
conceal Special character to show instead of the
match (only for |hl-Conceal| highlighted
matches, see |:syn-cchar|)
window Instead of the current window use the
window with this number or window ID.

The number of matches is not limited, as it is the case with
the |:match| commands.
Expand Down
67 changes: 41 additions & 26 deletions src/evalfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -7988,6 +7988,36 @@ f_match(typval_T *argvars, typval_T *rettv)
find_some_match(argvars, rettv, MATCH_MATCH);
}

#ifdef FEAT_SEARCH_EXTRA
static int
matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
{
dictitem_T *di;

if (tv->v_type != VAR_DICT)
{
EMSG(_(e_dictreq));
return FAIL;
}

if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
*conceal_char = get_dict_string(tv->vval.v_dict,
(char_u *)"conceal", FALSE);

if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
{
*win = find_win_by_nr(&di->di_tv, NULL);
if (*win == NULL)
{
EMSG(_("E957: Invalid window number"));
return FAIL;
}
}

return OK;
}
#endif

/*
* "matchadd()" function
*/
Expand All @@ -8002,6 +8032,7 @@ f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
int id = -1;
int error = FALSE;
char_u *conceal_char = NULL;
win_T *win = curwin;

rettv->vval.v_number = -1;

Expand All @@ -8013,18 +8044,9 @@ f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
if (argvars[3].v_type != VAR_UNKNOWN)
{
id = (int)get_tv_number_chk(&argvars[3], &error);
if (argvars[4].v_type != VAR_UNKNOWN)
{
if (argvars[4].v_type != VAR_DICT)
{
EMSG(_(e_dictreq));
return;
}
if (dict_find(argvars[4].vval.v_dict,
(char_u *)"conceal", -1) != NULL)
conceal_char = get_dict_string(argvars[4].vval.v_dict,
(char_u *)"conceal", FALSE);
}
if (argvars[4].v_type != VAR_UNKNOWN
&& matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
return;
}
}
if (error == TRUE)
Expand All @@ -8035,7 +8057,7 @@ f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
return;
}

rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
conceal_char);
#endif
}
Expand All @@ -8054,6 +8076,7 @@ f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
int error = FALSE;
list_T *l;
char_u *conceal_char = NULL;
win_T *win = curwin;

rettv->vval.v_number = -1;

Expand All @@ -8076,18 +8099,10 @@ f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
if (argvars[3].v_type != VAR_UNKNOWN)
{
id = (int)get_tv_number_chk(&argvars[3], &error);
if (argvars[4].v_type != VAR_UNKNOWN)
{
if (argvars[4].v_type != VAR_DICT)
{
EMSG(_(e_dictreq));
return;
}
if (dict_find(argvars[4].vval.v_dict,
(char_u *)"conceal", -1) != NULL)
conceal_char = get_dict_string(argvars[4].vval.v_dict,
(char_u *)"conceal", FALSE);
}

if (argvars[4].v_type != VAR_UNKNOWN
&& matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
return;
}
}
if (error == TRUE)
Expand All @@ -8100,7 +8115,7 @@ f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
return;
}

rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
conceal_char);
#endif
}
Expand Down
22 changes: 22 additions & 0 deletions src/testdir/test_match.vim
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,28 @@ func Test_matchaddpos()
set hlsearch&
endfunc

func Test_matchaddpos_otherwin()
syntax on
new
call setline(1, ['12345', 'NP'])
let winid = win_getid()

wincmd w
call matchadd('Search', '4', 10, -1, {'window': winid})
call matchaddpos('Error', [[1,2], [2,2]], 10, -1, {'window': winid})
redraw!
call assert_notequal(screenattr(1,2), 0)
call assert_notequal(screenattr(1,4), 0)
call assert_notequal(screenattr(2,2), 0)
call assert_equal(screenattr(1,2), screenattr(2,2))
call assert_notequal(screenattr(1,2), screenattr(1,4))

wincmd w
bwipe!
call clearmatches()
syntax off
endfunc

func Test_matchaddpos_using_negative_priority()
set hlsearch

Expand Down
2 changes: 2 additions & 0 deletions src/version.c
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,8 @@ static char *(features[]) =

static int included_patches[] =
{ /* Add new patch number below this line */
/**/
218,
/**/
217,
/**/
Expand Down

0 comments on commit 95e5147

Please sign in to comment.