@@ -4159,6 +4159,21 @@ ex_cfile(exarg_T *eap)
41594159 qf_jump (qi , 0 , 0 , eap -> forceit ); /* display first error */
41604160}
41614161
4162+ /*
4163+ * Return the quickfix/location list number with the given identifier.
4164+ * Returns -1 if list is not found.
4165+ */
4166+ static int
4167+ qf_id2nr (qf_info_T * qi , int_u qfid )
4168+ {
4169+ int qf_idx ;
4170+
4171+ for (qf_idx = 0 ; qf_idx < qi -> qf_listcount ; qf_idx ++ )
4172+ if (qi -> qf_lists [qf_idx ].qf_id == qfid )
4173+ return qf_idx ;
4174+ return -1 ;
4175+ }
4176+
41624177/*
41634178 * Return the vimgrep autocmd name.
41644179 */
@@ -4272,40 +4287,32 @@ vgr_load_dummy_buf(
42724287 */
42734288 static int
42744289vgr_qflist_valid (
4290+ win_T * wp ,
42754291 qf_info_T * qi ,
4276- int_u save_qfid ,
4277- qfline_T * cur_qf_start ,
4278- int loclist_cmd ,
4292+ int_u qfid ,
42794293 char_u * title )
42804294{
4281- if (loclist_cmd )
4295+ /* Verify that the quickfix/location list was not freed by an autocmd */
4296+ if (!qflist_valid (wp , qfid ))
42824297 {
4283- /*
4284- * Verify that the location list is still valid. An autocmd might
4285- * have freed the location list.
4286- */
4287- if (!qflist_valid (curwin , save_qfid ))
4298+ if (wp != NULL )
42884299 {
4300+ /* An autocmd has freed the location list. */
42894301 EMSG (_ (e_loc_list_changed ));
42904302 return FALSE;
42914303 }
4304+ else
4305+ {
4306+ /* Quickfix list is not found, create a new one. */
4307+ qf_new_list (qi , title );
4308+ return TRUE;
4309+ }
42924310 }
4293- if (cur_qf_start != qi -> qf_lists [qi -> qf_curlist ].qf_start )
4294- {
4295- int idx ;
42964311
4312+ if (qi -> qf_lists [qi -> qf_curlist ].qf_id != qfid )
42974313 /* Autocommands changed the quickfix list. Find the one we were
42984314 * using and restore it. */
4299- for (idx = 0 ; idx < LISTCOUNT ; ++ idx )
4300- if (cur_qf_start == qi -> qf_lists [idx ].qf_start )
4301- {
4302- qi -> qf_curlist = idx ;
4303- break ;
4304- }
4305- if (idx == LISTCOUNT )
4306- /* List cannot be found, create a new one. */
4307- qf_new_list (qi , title );
4308- }
4315+ qi -> qf_curlist = qf_id2nr (qi , qfid );
43094316
43104317 return TRUE;
43114318}
@@ -4424,10 +4431,8 @@ ex_vimgrep(exarg_T *eap)
44244431 char_u * p ;
44254432 int fi ;
44264433 qf_info_T * qi = & ql_info ;
4427- int loclist_cmd = FALSE;
44284434 int_u save_qfid ;
4429- qfline_T * cur_qf_start ;
4430- win_T * wp ;
4435+ win_T * wp = NULL ;
44314436 buf_T * buf ;
44324437 int duplicate_name = FALSE;
44334438 int using_dummy ;
@@ -4461,7 +4466,7 @@ ex_vimgrep(exarg_T *eap)
44614466 qi = ll_get_or_alloc_list (curwin );
44624467 if (qi == NULL )
44634468 return ;
4464- loclist_cmd = TRUE ;
4469+ wp = curwin ;
44654470 }
44664471
44674472 if (eap -> addr_count > 0 )
@@ -4518,10 +4523,9 @@ ex_vimgrep(exarg_T *eap)
45184523 * ":lcd %:p:h" changes the meaning of short path names. */
45194524 mch_dirname (dirname_start , MAXPATHL );
45204525
4521- /* Remember the current values of the quickfix list and qf_start , so that
4522- * we can check for autocommands changing the current quickfix list. */
4526+ /* Remember the current quickfix list identifier , so that we can check for
4527+ * autocommands changing the current quickfix list. */
45234528 save_qfid = qi -> qf_lists [qi -> qf_curlist ].qf_id ;
4524- cur_qf_start = qi -> qf_lists [qi -> qf_curlist ].qf_start ;
45254529
45264530 seconds = (time_t )0 ;
45274531 for (fi = 0 ; fi < fcount && !got_int && tomatch > 0 ; ++ fi )
@@ -4549,11 +4553,11 @@ ex_vimgrep(exarg_T *eap)
45494553 /* Use existing, loaded buffer. */
45504554 using_dummy = FALSE;
45514555
4552- /* Check whether the quickfix list is still valid */
4553- if (! vgr_qflist_valid ( qi , save_qfid , cur_qf_start , loclist_cmd ,
4554- * eap -> cmdlinep ))
4556+ /* Check whether the quickfix list is still valid. When loading a
4557+ * buffer above, autocommands might have changed the quickfix list. */
4558+ if (! vgr_qflist_valid ( wp , qi , save_qfid , * eap -> cmdlinep ))
45554559 goto theend ;
4556- cur_qf_start = qi -> qf_lists [qi -> qf_curlist ].qf_start ;
4560+ save_qfid = qi -> qf_lists [qi -> qf_curlist ].qf_id ;
45574561
45584562 if (buf == NULL )
45594563 {
@@ -4567,8 +4571,6 @@ ex_vimgrep(exarg_T *eap)
45674571 found_match = vgr_match_buflines (qi , fname , buf , & regmatch ,
45684572 tomatch , duplicate_name , flags );
45694573
4570- cur_qf_start = qi -> qf_lists [qi -> qf_curlist ].qf_start ;
4571-
45724574 if (using_dummy )
45734575 {
45744576 if (found_match && first_match_buf == NULL )
@@ -4649,7 +4651,6 @@ ex_vimgrep(exarg_T *eap)
46494651 * The QuickFixCmdPost autocmd may free the quickfix list. Check the list
46504652 * is still valid.
46514653 */
4652- wp = loclist_cmd ? curwin : NULL ;
46534654 if (!qflist_valid (wp , save_qfid ))
46544655 goto theend ;
46554656
@@ -4994,21 +4995,6 @@ qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
49944995 return status ;
49954996}
49964997
4997- /*
4998- * Return the quickfix/location list number with the given identifier.
4999- * Returns -1 if list is not found.
5000- */
5001- static int
5002- qf_id2nr (qf_info_T * qi , int_u qfid )
5003- {
5004- int qf_idx ;
5005-
5006- for (qf_idx = 0 ; qf_idx < qi -> qf_listcount ; qf_idx ++ )
5007- if (qi -> qf_lists [qf_idx ].qf_id == qfid )
5008- return qf_idx ;
5009- return -1 ;
5010- }
5011-
50124998/*
50134999 * Return the quickfix/location list window identifier in the current tabpage.
50145000 */
0 commit comments