3131static char * e_listarg = N_ ("E686: Argument of %s must be a List" );
3232static char * e_listblobarg = N_ ("E899: Argument of %s must be a List or Blob" );
3333static char * e_stringreq = N_ ("E928: String required" );
34+ static char * e_invalwindow = N_ ("E957: Invalid window number" );
3435
3536#ifdef FEAT_FLOAT
3637static void f_abs (typval_T * argvars , typval_T * rettv );
@@ -590,7 +591,7 @@ static struct fst
590591 {"changenr" , 0 , 0 , f_changenr },
591592 {"char2nr" , 1 , 2 , f_char2nr },
592593 {"cindent" , 1 , 1 , f_cindent },
593- {"clearmatches" , 0 , 0 , f_clearmatches },
594+ {"clearmatches" , 0 , 1 , f_clearmatches },
594595 {"col" , 1 , 1 , f_col },
595596#if defined(FEAT_INS_EXPAND )
596597 {"complete" , 2 , 2 , f_complete },
@@ -677,7 +678,7 @@ static struct fst
677678 {"getjumplist" , 0 , 2 , f_getjumplist },
678679 {"getline" , 1 , 2 , f_getline },
679680 {"getloclist" , 1 , 2 , f_getloclist },
680- {"getmatches" , 0 , 0 , f_getmatches },
681+ {"getmatches" , 0 , 1 , f_getmatches },
681682 {"getpid" , 0 , 0 , f_getpid },
682683 {"getpos" , 1 , 1 , f_getpos },
683684 {"getqflist" , 0 , 1 , f_getqflist },
@@ -761,7 +762,7 @@ static struct fst
761762 {"matchadd" , 2 , 5 , f_matchadd },
762763 {"matchaddpos" , 2 , 5 , f_matchaddpos },
763764 {"matcharg" , 1 , 1 , f_matcharg },
764- {"matchdelete" , 1 , 1 , f_matchdelete },
765+ {"matchdelete" , 1 , 2 , f_matchdelete },
765766 {"matchend" , 2 , 4 , f_matchend },
766767 {"matchlist" , 2 , 4 , f_matchlist },
767768 {"matchstr" , 2 , 4 , f_matchstr },
@@ -859,7 +860,7 @@ static struct fst
859860 {"setfperm" , 2 , 2 , f_setfperm },
860861 {"setline" , 2 , 2 , f_setline },
861862 {"setloclist" , 2 , 4 , f_setloclist },
862- {"setmatches" , 1 , 1 , f_setmatches },
863+ {"setmatches" , 1 , 2 , f_setmatches },
863864 {"setpos" , 2 , 2 , f_setpos },
864865 {"setqflist" , 1 , 3 , f_setqflist },
865866 {"setreg" , 2 , 3 , f_setreg },
@@ -2496,14 +2497,34 @@ f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
24962497 rettv -> vval .v_number = -1 ;
24972498}
24982499
2500+ static win_T *
2501+ get_optional_window (typval_T * argvars , int idx )
2502+ {
2503+ win_T * win = curwin ;
2504+
2505+ if (argvars [idx ].v_type != VAR_UNKNOWN )
2506+ {
2507+ win = find_win_by_nr_or_id (& argvars [idx ]);
2508+ if (win == NULL )
2509+ {
2510+ emsg (_ (e_invalwindow ));
2511+ return NULL ;
2512+ }
2513+ }
2514+ return win ;
2515+ }
2516+
24992517/*
25002518 * "clearmatches()" function
25012519 */
25022520 static void
25032521f_clearmatches (typval_T * argvars UNUSED , typval_T * rettv UNUSED )
25042522{
25052523#ifdef FEAT_SEARCH_EXTRA
2506- clear_matches (curwin );
2524+ win_T * win = get_optional_window (argvars , 0 );
2525+
2526+ if (win != NULL )
2527+ clear_matches (win );
25072528#endif
25082529}
25092530
@@ -5412,60 +5433,62 @@ f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
54125433{
54135434#ifdef FEAT_SEARCH_EXTRA
54145435 dict_T * dict ;
5415- matchitem_T * cur = curwin -> w_match_head ;
5436+ matchitem_T * cur ;
54165437 int i ;
5438+ win_T * win = get_optional_window (argvars , 0 );
54175439
5418- if (rettv_list_alloc (rettv ) == OK )
5440+ if (rettv_list_alloc (rettv ) == FAIL || win == NULL )
5441+ return ;
5442+
5443+ cur = win -> w_match_head ;
5444+ while (cur != NULL )
54195445 {
5420- while (cur != NULL )
5446+ dict = dict_alloc ();
5447+ if (dict == NULL )
5448+ return ;
5449+ if (cur -> match .regprog == NULL )
54215450 {
5422- dict = dict_alloc ();
5423- if (dict == NULL )
5424- return ;
5425- if (cur -> match .regprog == NULL )
5451+ /* match added with matchaddpos() */
5452+ for (i = 0 ; i < MAXPOSMATCH ; ++ i )
54265453 {
5427- /* match added with matchaddpos() */
5428- for (i = 0 ; i < MAXPOSMATCH ; ++ i )
5429- {
5430- llpos_T * llpos ;
5431- char buf [6 ];
5432- list_T * l ;
5454+ llpos_T * llpos ;
5455+ char buf [6 ];
5456+ list_T * l ;
54335457
5434- llpos = & cur -> pos .pos [i ];
5435- if (llpos -> lnum == 0 )
5436- break ;
5437- l = list_alloc ();
5438- if (l == NULL )
5439- break ;
5440- list_append_number (l , (varnumber_T )llpos -> lnum );
5441- if (llpos -> col > 0 )
5442- {
5443- list_append_number (l , (varnumber_T )llpos -> col );
5444- list_append_number (l , (varnumber_T )llpos -> len );
5445- }
5446- sprintf (buf , "pos%d" , i + 1 );
5447- dict_add_list (dict , buf , l );
5458+ llpos = & cur -> pos .pos [i ];
5459+ if (llpos -> lnum == 0 )
5460+ break ;
5461+ l = list_alloc ();
5462+ if (l == NULL )
5463+ break ;
5464+ list_append_number (l , (varnumber_T )llpos -> lnum );
5465+ if (llpos -> col > 0 )
5466+ {
5467+ list_append_number (l , (varnumber_T )llpos -> col );
5468+ list_append_number (l , (varnumber_T )llpos -> len );
54485469 }
5470+ sprintf (buf , "pos%d" , i + 1 );
5471+ dict_add_list (dict , buf , l );
54495472 }
5450- else
5451- {
5452- dict_add_string (dict , "pattern" , cur -> pattern );
5453- }
5454- dict_add_string (dict , "group" , syn_id2name (cur -> hlg_id ));
5455- dict_add_number (dict , "priority" , (long )cur -> priority );
5456- dict_add_number (dict , "id" , (long )cur -> id );
5473+ }
5474+ else
5475+ {
5476+ dict_add_string (dict , "pattern" , cur -> pattern );
5477+ }
5478+ dict_add_string (dict , "group" , syn_id2name (cur -> hlg_id ));
5479+ dict_add_number (dict , "priority" , (long )cur -> priority );
5480+ dict_add_number (dict , "id" , (long )cur -> id );
54575481# if defined(FEAT_CONCEAL )
5458- if (cur -> conceal_char )
5459- {
5460- char_u buf [MB_MAXBYTES + 1 ];
5482+ if (cur -> conceal_char )
5483+ {
5484+ char_u buf [MB_MAXBYTES + 1 ];
54615485
5462- buf [(* mb_char2bytes )((int )cur -> conceal_char , buf )] = NUL ;
5463- dict_add_string (dict , "conceal" , (char_u * )& buf );
5464- }
5465- # endif
5466- list_append_dict (rettv -> vval .v_list , dict );
5467- cur = cur -> next ;
5486+ buf [(* mb_char2bytes )((int )cur -> conceal_char , buf )] = NUL ;
5487+ dict_add_string (dict , "conceal" , (char_u * )& buf );
54685488 }
5489+ # endif
5490+ list_append_dict (rettv -> vval .v_list , dict );
5491+ cur = cur -> next ;
54695492 }
54705493#endif
54715494}
@@ -8245,7 +8268,7 @@ matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
82458268 * win = find_win_by_nr_or_id (& di -> di_tv );
82468269 if (* win == NULL )
82478270 {
8248- emsg (_ ("E957: Invalid window number" ));
8271+ emsg (_ (e_invalwindow ));
82498272 return FAIL ;
82508273 }
82518274 }
@@ -8393,7 +8416,12 @@ f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
83938416f_matchdelete (typval_T * argvars UNUSED , typval_T * rettv UNUSED )
83948417{
83958418#ifdef FEAT_SEARCH_EXTRA
8396- rettv -> vval .v_number = match_delete (curwin ,
8419+ win_T * win = get_optional_window (argvars , 1 );
8420+
8421+ if (win == NULL )
8422+ rettv -> vval .v_number = -1 ;
8423+ else
8424+ rettv -> vval .v_number = match_delete (win ,
83978425 (int )tv_get_number (& argvars [0 ]), TRUE);
83988426#endif
83998427}
@@ -11206,16 +11234,19 @@ f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1120611234 listitem_T * li ;
1120711235 dict_T * d ;
1120811236 list_T * s = NULL ;
11237+ win_T * win = get_optional_window (argvars , 1 );
1120911238
1121011239 rettv -> vval .v_number = -1 ;
1121111240 if (argvars [0 ].v_type != VAR_LIST )
1121211241 {
1121311242 emsg (_ (e_listreq ));
1121411243 return ;
1121511244 }
11245+ if (win == NULL )
11246+ return ;
11247+
1121611248 if ((l = argvars [0 ].vval .v_list ) != NULL )
1121711249 {
11218-
1121911250 /* To some extent make sure that we are dealing with a list from
1122011251 * "getmatches()". */
1122111252 li = l -> lv_first ;
@@ -11239,7 +11270,7 @@ f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1123911270 li = li -> li_next ;
1124011271 }
1124111272
11242- clear_matches (curwin );
11273+ clear_matches (win );
1124311274 li = l -> lv_first ;
1124411275 while (li != NULL )
1124511276 {
@@ -11286,13 +11317,13 @@ f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1128611317 : NULL ;
1128711318 if (i == 0 )
1128811319 {
11289- match_add (curwin , group ,
11320+ match_add (win , group ,
1129011321 dict_get_string (d , (char_u * )"pattern" , FALSE),
1129111322 priority , id , NULL , conceal );
1129211323 }
1129311324 else
1129411325 {
11295- match_add (curwin , group , NULL , priority , id , s , conceal );
11326+ match_add (win , group , NULL , priority , id , s , conceal );
1129611327 list_unref (s );
1129711328 s = NULL ;
1129811329 }
0 commit comments