Skip to content

Commit 95e5147

Browse files
committed
patch 8.1.0218: cannot add matches to another window
Problem: Cannot add matches to another window. (Qiming Zhao) Solution: Add the "window" argument to matchadd() and matchaddpos(). (closes #3260)
1 parent fd24946 commit 95e5147

4 files changed

Lines changed: 68 additions & 27 deletions

File tree

runtime/doc/eval.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6016,7 +6016,7 @@ match({expr}, {pat} [, {start} [, {count}]]) *match()*
60166016
the pattern. 'smartcase' is NOT used. The matching is always
60176017
done like 'magic' is set and 'cpoptions' is empty.
60186018

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

60596061
The number of matches is not limited, as it is the case with
60606062
the |:match| commands.

src/evalfunc.c

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7988,6 +7988,36 @@ f_match(typval_T *argvars, typval_T *rettv)
79887988
find_some_match(argvars, rettv, MATCH_MATCH);
79897989
}
79907990

7991+
#ifdef FEAT_SEARCH_EXTRA
7992+
static int
7993+
matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
7994+
{
7995+
dictitem_T *di;
7996+
7997+
if (tv->v_type != VAR_DICT)
7998+
{
7999+
EMSG(_(e_dictreq));
8000+
return FAIL;
8001+
}
8002+
8003+
if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
8004+
*conceal_char = get_dict_string(tv->vval.v_dict,
8005+
(char_u *)"conceal", FALSE);
8006+
8007+
if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8008+
{
8009+
*win = find_win_by_nr(&di->di_tv, NULL);
8010+
if (*win == NULL)
8011+
{
8012+
EMSG(_("E957: Invalid window number"));
8013+
return FAIL;
8014+
}
8015+
}
8016+
8017+
return OK;
8018+
}
8019+
#endif
8020+
79918021
/*
79928022
* "matchadd()" function
79938023
*/
@@ -8002,6 +8032,7 @@ f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
80028032
int id = -1;
80038033
int error = FALSE;
80048034
char_u *conceal_char = NULL;
8035+
win_T *win = curwin;
80058036

80068037
rettv->vval.v_number = -1;
80078038

@@ -8013,18 +8044,9 @@ f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
80138044
if (argvars[3].v_type != VAR_UNKNOWN)
80148045
{
80158046
id = (int)get_tv_number_chk(&argvars[3], &error);
8016-
if (argvars[4].v_type != VAR_UNKNOWN)
8017-
{
8018-
if (argvars[4].v_type != VAR_DICT)
8019-
{
8020-
EMSG(_(e_dictreq));
8021-
return;
8022-
}
8023-
if (dict_find(argvars[4].vval.v_dict,
8024-
(char_u *)"conceal", -1) != NULL)
8025-
conceal_char = get_dict_string(argvars[4].vval.v_dict,
8026-
(char_u *)"conceal", FALSE);
8027-
}
8047+
if (argvars[4].v_type != VAR_UNKNOWN
8048+
&& matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8049+
return;
80288050
}
80298051
}
80308052
if (error == TRUE)
@@ -8035,7 +8057,7 @@ f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
80358057
return;
80368058
}
80378059

8038-
rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
8060+
rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
80398061
conceal_char);
80408062
#endif
80418063
}
@@ -8054,6 +8076,7 @@ f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
80548076
int error = FALSE;
80558077
list_T *l;
80568078
char_u *conceal_char = NULL;
8079+
win_T *win = curwin;
80578080

80588081
rettv->vval.v_number = -1;
80598082

@@ -8076,18 +8099,10 @@ f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
80768099
if (argvars[3].v_type != VAR_UNKNOWN)
80778100
{
80788101
id = (int)get_tv_number_chk(&argvars[3], &error);
8079-
if (argvars[4].v_type != VAR_UNKNOWN)
8080-
{
8081-
if (argvars[4].v_type != VAR_DICT)
8082-
{
8083-
EMSG(_(e_dictreq));
8084-
return;
8085-
}
8086-
if (dict_find(argvars[4].vval.v_dict,
8087-
(char_u *)"conceal", -1) != NULL)
8088-
conceal_char = get_dict_string(argvars[4].vval.v_dict,
8089-
(char_u *)"conceal", FALSE);
8090-
}
8102+
8103+
if (argvars[4].v_type != VAR_UNKNOWN
8104+
&& matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8105+
return;
80918106
}
80928107
}
80938108
if (error == TRUE)
@@ -8100,7 +8115,7 @@ f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
81008115
return;
81018116
}
81028117

8103-
rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
8118+
rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
81048119
conceal_char);
81058120
#endif
81068121
}

src/testdir/test_match.vim

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,28 @@ func Test_matchaddpos()
192192
set hlsearch&
193193
endfunc
194194

195+
func Test_matchaddpos_otherwin()
196+
syntax on
197+
new
198+
call setline(1, ['12345', 'NP'])
199+
let winid = win_getid()
200+
201+
wincmd w
202+
call matchadd('Search', '4', 10, -1, {'window': winid})
203+
call matchaddpos('Error', [[1,2], [2,2]], 10, -1, {'window': winid})
204+
redraw!
205+
call assert_notequal(screenattr(1,2), 0)
206+
call assert_notequal(screenattr(1,4), 0)
207+
call assert_notequal(screenattr(2,2), 0)
208+
call assert_equal(screenattr(1,2), screenattr(2,2))
209+
call assert_notequal(screenattr(1,2), screenattr(1,4))
210+
211+
wincmd w
212+
bwipe!
213+
call clearmatches()
214+
syntax off
215+
endfunc
216+
195217
func Test_matchaddpos_using_negative_priority()
196218
set hlsearch
197219

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,8 @@ static char *(features[]) =
798798

799799
static int included_patches[] =
800800
{ /* Add new patch number below this line */
801+
/**/
802+
218,
801803
/**/
802804
217,
803805
/**/

0 commit comments

Comments
 (0)