@@ -2231,7 +2231,7 @@ qflist_valid (win_T *wp, int_u qf_id)
22312231}
22322232
22332233/*
2234- * When loading a file from the quickfix, the auto commands may modify it.
2234+ * When loading a file from the quickfix, the autocommands may modify it.
22352235 * This may invalidate the current quickfix entry. This function checks
22362236 * whether an entry is still present in the quickfix list.
22372237 * Similar to location list.
@@ -2579,7 +2579,8 @@ qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
25792579 if (win -> w_llist == NULL )
25802580 {
25812581 win -> w_llist = ll_ref ;
2582- ll_ref -> qf_refcount ++ ;
2582+ if (ll_ref != NULL )
2583+ ll_ref -> qf_refcount ++ ;
25832584 }
25842585}
25852586
@@ -2986,8 +2987,9 @@ qf_jump(qf_info_T *qi,
29862987 if (curbuf == old_curbuf )
29872988 setpcmark ();
29882989
2989- qf_jump_goto_line (qf_ptr -> qf_lnum , qf_ptr -> qf_col , qf_ptr -> qf_viscol ,
2990- qf_ptr -> qf_pattern );
2990+ if (qf_ptr != NULL )
2991+ qf_jump_goto_line (qf_ptr -> qf_lnum , qf_ptr -> qf_col ,
2992+ qf_ptr -> qf_viscol , qf_ptr -> qf_pattern );
29912993
29922994#ifdef FEAT_FOLDING
29932995 if ((fdo_flags & FDO_QUICKFIX ) && old_KeyTyped )
@@ -4055,6 +4057,21 @@ qf_list_changed(qf_info_T *qi, int qf_idx)
40554057 qi -> qf_lists [qf_idx ].qf_changedtick ++ ;
40564058}
40574059
4060+ /*
4061+ * Return the quickfix/location list number with the given identifier.
4062+ * Returns -1 if list is not found.
4063+ */
4064+ static int
4065+ qf_id2nr (qf_info_T * qi , int_u qfid )
4066+ {
4067+ int qf_idx ;
4068+
4069+ for (qf_idx = 0 ; qf_idx < qi -> qf_listcount ; qf_idx ++ )
4070+ if (qi -> qf_lists [qf_idx ].qf_id == qfid )
4071+ return qf_idx ;
4072+ return INVALID_QFIDX ;
4073+ }
4074+
40584075/*
40594076 * Return TRUE when using ":vimgrep" for ":grep".
40604077 */
@@ -4083,6 +4100,7 @@ ex_make(exarg_T *eap)
40834100 qf_info_T * qi = & ql_info ;
40844101 int res ;
40854102 char_u * au_name = NULL ;
4103+ int_u save_qfid ;
40864104
40874105 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
40884106 if (grep_internal (eap -> cmdidx ))
@@ -4161,21 +4179,28 @@ ex_make(exarg_T *eap)
41614179 && eap -> cmdidx != CMD_lgrepadd ),
41624180 qf_cmdtitle (* eap -> cmdlinep ), enc );
41634181 if (wp != NULL )
4182+ {
41644183 qi = GET_LOC_LIST (wp );
4165- if (res >= 0 && qi != NULL )
4184+ if (qi == NULL )
4185+ goto cleanup ;
4186+ }
4187+ if (res >= 0 )
41664188 qf_list_changed (qi , qi -> qf_curlist );
4189+ // Remember the current quickfix list identifier, so that we can
4190+ // check for autocommands changing the current quickfix list.
4191+ save_qfid = qi -> qf_lists [qi -> qf_curlist ].qf_id ;
41674192 if (au_name != NULL )
4168- {
41694193 apply_autocmds (EVENT_QUICKFIXCMDPOST , au_name ,
41704194 curbuf -> b_fname , TRUE, curbuf );
4171- if (qi != NULL && qi -> qf_curlist < qi -> qf_listcount )
4172- res = qi -> qf_lists [qi -> qf_curlist ].qf_count ;
4173- else
4174- res = 0 ;
4175- }
4176- if (res > 0 && !eap -> forceit )
4195+ if (res > 0 && !eap -> forceit && qflist_valid (wp , save_qfid ))
4196+ {
4197+ // If autocommands changed the current list, then restore it
4198+ if (qi -> qf_lists [qi -> qf_curlist ].qf_id != save_qfid )
4199+ qi -> qf_curlist = qf_id2nr (qi , save_qfid );
41774200 qf_jump (qi , 0 , 0 , FALSE); /* display first error */
4201+ }
41784202
4203+ cleanup :
41794204 mch_remove (fname );
41804205 vim_free (fname );
41814206 vim_free (cmd );
@@ -4502,7 +4527,7 @@ ex_cfile(exarg_T *eap)
45024527 win_T * wp = NULL ;
45034528 qf_info_T * qi = & ql_info ;
45044529 char_u * au_name = NULL ;
4505- int save_qfid = 0 ; /* init for gcc */
4530+ int_u save_qfid = 0 ; /* init for gcc */
45064531 int res ;
45074532
45084533 switch (eap -> cmdidx )
@@ -4555,35 +4580,27 @@ ex_cfile(exarg_T *eap)
45554580 && eap -> cmdidx != CMD_laddfile ),
45564581 qf_cmdtitle (* eap -> cmdlinep ), enc );
45574582 if (wp != NULL )
4583+ {
45584584 qi = GET_LOC_LIST (wp );
4559- if (res >= 0 && qi != NULL )
4585+ if (qi == NULL )
4586+ return ;
4587+ }
4588+ if (res >= 0 )
45604589 qf_list_changed (qi , qi -> qf_curlist );
4561- if (qi != NULL )
4562- save_qfid = qi -> qf_lists [qi -> qf_curlist ].qf_id ;
4590+ save_qfid = qi -> qf_lists [qi -> qf_curlist ].qf_id ;
45634591 if (au_name != NULL )
45644592 apply_autocmds (EVENT_QUICKFIXCMDPOST , au_name , NULL , FALSE, curbuf );
45654593
4566- /* An autocmd might have freed the quickfix/location list. Check whether it
4567- * is still valid. */
4568- if (qi != NULL && !qflist_valid (wp , save_qfid ))
4569- return ;
4570- if (res > 0 && (eap -> cmdidx == CMD_cfile || eap -> cmdidx == CMD_lfile ))
4594+ // Jump to the first error for a new list and if autocmds didn't
4595+ // free the list.
4596+ if (res > 0 && (eap -> cmdidx == CMD_cfile || eap -> cmdidx == CMD_lfile )
4597+ && qflist_valid (wp , save_qfid ))
4598+ {
4599+ // If autocommands changed the current list, then restore it
4600+ if (qi -> qf_lists [qi -> qf_curlist ].qf_id != save_qfid )
4601+ qi -> qf_curlist = qf_id2nr (qi , save_qfid );
45714602 qf_jump (qi , 0 , 0 , eap -> forceit ); /* display first error */
4572- }
4573-
4574- /*
4575- * Return the quickfix/location list number with the given identifier.
4576- * Returns -1 if list is not found.
4577- */
4578- static int
4579- qf_id2nr (qf_info_T * qi , int_u qfid )
4580- {
4581- int qf_idx ;
4582-
4583- for (qf_idx = 0 ; qf_idx < qi -> qf_listcount ; qf_idx ++ )
4584- if (qi -> qf_lists [qf_idx ].qf_id == qfid )
4585- return qf_idx ;
4586- return INVALID_QFIDX ;
4603+ }
45874604}
45884605
45894606/*
@@ -5070,6 +5087,10 @@ ex_vimgrep(exarg_T *eap)
50705087 if (!qflist_valid (wp , save_qfid ))
50715088 goto theend ;
50725089
5090+ // If autocommands changed the current list, then restore it
5091+ if (qi -> qf_lists [qi -> qf_curlist ].qf_id != save_qfid )
5092+ qi -> qf_curlist = qf_id2nr (qi , save_qfid );
5093+
50735094 /* Jump to first match. */
50745095 if (qi -> qf_lists [qi -> qf_curlist ].qf_count > 0 )
50755096 {
@@ -5162,7 +5183,7 @@ load_dummy_buffer(
51625183 /* need to open the memfile before putting the buffer in a window */
51635184 if (ml_open (newbuf ) == OK )
51645185 {
5165- /* Make sure this buffer isn't wiped out by auto commands . */
5186+ /* Make sure this buffer isn't wiped out by autocommands . */
51665187 ++ newbuf -> b_locked ;
51675188
51685189 /* set curwin/curbuf to buf and save a few things */
@@ -6205,6 +6226,8 @@ ex_cbuffer(exarg_T *eap)
62056226 qf_info_T * qi = & ql_info ;
62066227 char_u * au_name = NULL ;
62076228 int res ;
6229+ int_u save_qfid ;
6230+ win_T * wp = NULL ;
62086231
62096232 switch (eap -> cmdidx )
62106233 {
@@ -6233,6 +6256,7 @@ ex_cbuffer(exarg_T *eap)
62336256 qi = ll_get_or_alloc_list (curwin );
62346257 if (qi == NULL )
62356258 return ;
6259+ wp = curwin ;
62366260 }
62376261
62386262 if (* eap -> arg == NUL )
@@ -6271,6 +6295,10 @@ ex_cbuffer(exarg_T *eap)
62716295 qf_title , NULL );
62726296 if (res >= 0 )
62736297 qf_list_changed (qi , qi -> qf_curlist );
6298+
6299+ // Remember the current quickfix list identifier, so that we can
6300+ // check for autocommands changing the current quickfix list.
6301+ save_qfid = qi -> qf_lists [qi -> qf_curlist ].qf_id ;
62746302 if (au_name != NULL )
62756303 {
62766304 buf_T * curbuf_old = curbuf ;
@@ -6282,9 +6310,17 @@ ex_cbuffer(exarg_T *eap)
62826310 // be invalid.
62836311 res = 0 ;
62846312 }
6313+ // Jump to the first error for a new list and if autocmds didn't
6314+ // free the list.
62856315 if (res > 0 && (eap -> cmdidx == CMD_cbuffer ||
6286- eap -> cmdidx == CMD_lbuffer ))
6316+ eap -> cmdidx == CMD_lbuffer )
6317+ && qflist_valid (wp , save_qfid ))
6318+ {
6319+ // If autocommands changed the current list, then restore it
6320+ if (qi -> qf_lists [qi -> qf_curlist ].qf_id != save_qfid )
6321+ qi -> qf_curlist = qf_id2nr (qi , save_qfid );
62876322 qf_jump (qi , 0 , 0 , eap -> forceit ); /* display first error */
6323+ }
62886324 }
62896325 }
62906326}
@@ -6301,6 +6337,8 @@ ex_cexpr(exarg_T *eap)
63016337 qf_info_T * qi = & ql_info ;
63026338 char_u * au_name = NULL ;
63036339 int res ;
6340+ int_u save_qfid ;
6341+ win_T * wp = NULL ;
63046342
63056343 switch (eap -> cmdidx )
63066344 {
@@ -6328,6 +6366,7 @@ ex_cexpr(exarg_T *eap)
63286366 qi = ll_get_or_alloc_list (curwin );
63296367 if (qi == NULL )
63306368 return ;
6369+ wp = curwin ;
63316370 }
63326371
63336372 /* Evaluate the expression. When the result is a string or a list we can
@@ -6345,14 +6384,25 @@ ex_cexpr(exarg_T *eap)
63456384 qf_cmdtitle (* eap -> cmdlinep ), NULL );
63466385 if (res >= 0 )
63476386 qf_list_changed (qi , qi -> qf_curlist );
6387+
6388+ // Remember the current quickfix list identifier, so that we can
6389+ // check for autocommands changing the current quickfix list.
6390+ save_qfid = qi -> qf_lists [qi -> qf_curlist ].qf_id ;
63486391 if (au_name != NULL )
63496392 apply_autocmds (EVENT_QUICKFIXCMDPOST , au_name ,
63506393 curbuf -> b_fname , TRUE, curbuf );
6394+
6395+ // Jump to the first error for a new list and if autocmds didn't
6396+ // free the list.
63516397 if (res > 0 && (eap -> cmdidx == CMD_cexpr
63526398 || eap -> cmdidx == CMD_lexpr )
6353- && qi == GET_LOC_LIST (curwin ))
6354- // Jump to the first error if autocmds didn't free the list.
6399+ && qflist_valid (wp , save_qfid ))
6400+ {
6401+ // If autocommands changed the current list, then restore it
6402+ if (qi -> qf_lists [qi -> qf_curlist ].qf_id != save_qfid )
6403+ qi -> qf_curlist = qf_id2nr (qi , save_qfid );
63556404 qf_jump (qi , 0 , 0 , eap -> forceit );
6405+ }
63566406 }
63576407 else
63586408 EMSG (_ ("E777: String or List expected" ));
0 commit comments