Skip to content

Commit 531b9a3

Browse files
committed
patch 8.1.0141: :cexpr no longer jumps to the first error
Problem: :cexpr no longer jumps to the first error. Solution: Use the quickfix list identifier. (Yegappan Lakshmanan, closes #3092)
1 parent 6dc819b commit 531b9a3

3 files changed

Lines changed: 206 additions & 43 deletions

File tree

‎src/quickfix.c‎

Lines changed: 90 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -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"));

‎src/testdir/test_quickfix.vim‎

Lines changed: 114 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3363,13 +3363,124 @@ func Test_lbuffer_with_bwipe()
33633363
augroup END
33643364
endfunc
33653365

3366-
func Test_setloclist_in_aucmd()
3366+
" Test for an autocmd freeing the quickfix/location list when cexpr/lexpr is
3367+
" running
3368+
func Xexpr_acmd_freelist(cchar)
3369+
call s:setup_commands(a:cchar)
3370+
33673371
" This was using freed memory.
33683372
augroup nasty
3369-
au * * call setloclist(0, [], 'f')
3373+
au * * call g:Xsetlist([], 'f')
33703374
augroup END
3371-
lexpr "x"
3375+
Xexpr "x"
33723376
augroup nasty
33733377
au!
33743378
augroup END
33753379
endfunc
3380+
3381+
func Test_cexpr_acmd_freelist()
3382+
call Xexpr_acmd_freelist('c')
3383+
call Xexpr_acmd_freelist('l')
3384+
endfunc
3385+
3386+
" Test for commands that create a new quickfix/location list and jump to the
3387+
" first error automatically.
3388+
func Xjumpto_first_error_test(cchar)
3389+
call s:setup_commands(a:cchar)
3390+
3391+
call s:create_test_file('Xtestfile1')
3392+
call s:create_test_file('Xtestfile2')
3393+
let l = ['Xtestfile1:2:Line2', 'Xtestfile2:4:Line4']
3394+
3395+
" Test for cexpr/lexpr
3396+
enew
3397+
Xexpr l
3398+
call assert_equal('Xtestfile1', bufname(''))
3399+
call assert_equal(2, line('.'))
3400+
3401+
" Test for cfile/lfile
3402+
enew
3403+
call writefile(l, 'Xerr')
3404+
Xfile Xerr
3405+
call assert_equal('Xtestfile1', bufname(''))
3406+
call assert_equal(2, line('.'))
3407+
3408+
" Test for cbuffer/lbuffer
3409+
edit Xerr
3410+
Xbuffer
3411+
call assert_equal('Xtestfile1', bufname(''))
3412+
call assert_equal(2, line('.'))
3413+
3414+
call delete('Xerr')
3415+
call delete('Xtestfile1')
3416+
call delete('Xtestfile2')
3417+
endfunc
3418+
3419+
func Test_jumpto_first_error()
3420+
call Xjumpto_first_error_test('c')
3421+
call Xjumpto_first_error_test('l')
3422+
endfunc
3423+
3424+
" Test for a quickfix autocmd changing the quickfix/location list before
3425+
" jumping to the first error in the new list.
3426+
func Xautocmd_changelist(cchar)
3427+
call s:setup_commands(a:cchar)
3428+
3429+
" Test for cfile/lfile
3430+
call s:create_test_file('Xtestfile1')
3431+
call s:create_test_file('Xtestfile2')
3432+
Xexpr 'Xtestfile1:2:Line2'
3433+
autocmd QuickFixCmdPost * Xolder
3434+
call writefile(['Xtestfile2:4:Line4'], 'Xerr')
3435+
Xfile Xerr
3436+
call assert_equal('Xtestfile2', bufname(''))
3437+
call assert_equal(4, line('.'))
3438+
autocmd! QuickFixCmdPost
3439+
3440+
" Test for cbuffer/lbuffer
3441+
call g:Xsetlist([], 'f')
3442+
Xexpr 'Xtestfile1:2:Line2'
3443+
autocmd QuickFixCmdPost * Xolder
3444+
call writefile(['Xtestfile2:4:Line4'], 'Xerr')
3445+
edit Xerr
3446+
Xbuffer
3447+
call assert_equal('Xtestfile2', bufname(''))
3448+
call assert_equal(4, line('.'))
3449+
autocmd! QuickFixCmdPost
3450+
3451+
" Test for cexpr/lexpr
3452+
call g:Xsetlist([], 'f')
3453+
Xexpr 'Xtestfile1:2:Line2'
3454+
autocmd QuickFixCmdPost * Xolder
3455+
Xexpr 'Xtestfile2:4:Line4'
3456+
call assert_equal('Xtestfile2', bufname(''))
3457+
call assert_equal(4, line('.'))
3458+
autocmd! QuickFixCmdPost
3459+
3460+
" Test for grep/lgrep
3461+
call g:Xsetlist([], 'f')
3462+
Xexpr 'Xtestfile1:2:Line2'
3463+
autocmd QuickFixCmdPost * Xolder
3464+
silent Xgrep Line5 Xtestfile2
3465+
call assert_equal('Xtestfile2', bufname(''))
3466+
call assert_equal(5, line('.'))
3467+
autocmd! QuickFixCmdPost
3468+
3469+
" Test for vimgrep/lvimgrep
3470+
call g:Xsetlist([], 'f')
3471+
Xexpr 'Xtestfile1:2:Line2'
3472+
autocmd QuickFixCmdPost * Xolder
3473+
silent Xvimgrep Line5 Xtestfile2
3474+
call assert_equal('Xtestfile2', bufname(''))
3475+
call assert_equal(5, line('.'))
3476+
autocmd! QuickFixCmdPost
3477+
3478+
call delete('Xerr')
3479+
call delete('Xtestfile1')
3480+
call delete('Xtestfile2')
3481+
endfunc
3482+
3483+
func Test_autocmd_changelist()
3484+
call Xautocmd_changelist('c')
3485+
call Xautocmd_changelist('l')
3486+
endfunc

0 commit comments

Comments
 (0)