To: vim_dev@googlegroups.com Subject: Patch 8.1.0141 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.1.0141 Problem: :cexpr no longer jumps to the first error. Solution: Use the quickfix list identifier. (Yegappan Lakshmanan, closes #3092) Files: src/quickfix.c, src/testdir/test_quickfix.vim *** ../vim-8.1.0140/src/quickfix.c 2018-06-18 21:53:23.514826893 +0200 --- src/quickfix.c 2018-07-03 16:50:52.302649159 +0200 *************** *** 2231,2237 **** } /* ! * When loading a file from the quickfix, the auto commands may modify it. * This may invalidate the current quickfix entry. This function checks * whether an entry is still present in the quickfix list. * Similar to location list. --- 2231,2237 ---- } /* ! * When loading a file from the quickfix, the autocommands may modify it. * This may invalidate the current quickfix entry. This function checks * whether an entry is still present in the quickfix list. * Similar to location list. *************** *** 2579,2585 **** if (win->w_llist == NULL) { win->w_llist = ll_ref; ! ll_ref->qf_refcount++; } } --- 2579,2586 ---- if (win->w_llist == NULL) { win->w_llist = ll_ref; ! if (ll_ref != NULL) ! ll_ref->qf_refcount++; } } *************** *** 2986,2993 **** if (curbuf == old_curbuf) setpcmark(); ! qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol, ! qf_ptr->qf_pattern); #ifdef FEAT_FOLDING if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped) --- 2987,2995 ---- if (curbuf == old_curbuf) setpcmark(); ! if (qf_ptr != NULL) ! qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, ! qf_ptr->qf_viscol, qf_ptr->qf_pattern); #ifdef FEAT_FOLDING if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped) *************** *** 4056,4061 **** --- 4058,4078 ---- } /* + * Return the quickfix/location list number with the given identifier. + * Returns -1 if list is not found. + */ + static int + qf_id2nr(qf_info_T *qi, int_u qfid) + { + int qf_idx; + + for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++) + if (qi->qf_lists[qf_idx].qf_id == qfid) + return qf_idx; + return INVALID_QFIDX; + } + + /* * Return TRUE when using ":vimgrep" for ":grep". */ int *************** *** 4083,4088 **** --- 4100,4106 ---- qf_info_T *qi = &ql_info; int res; char_u *au_name = NULL; + int_u save_qfid; /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */ if (grep_internal(eap->cmdidx)) *************** *** 4161,4181 **** && eap->cmdidx != CMD_lgrepadd), qf_cmdtitle(*eap->cmdlinep), enc); if (wp != NULL) qi = GET_LOC_LIST(wp); ! if (res >= 0 && qi != NULL) qf_list_changed(qi, qi->qf_curlist); if (au_name != NULL) - { apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname, TRUE, curbuf); ! if (qi != NULL && qi->qf_curlist < qi->qf_listcount) ! res = qi->qf_lists[qi->qf_curlist].qf_count; ! else ! res = 0; ! } ! if (res > 0 && !eap->forceit) qf_jump(qi, 0, 0, FALSE); /* display first error */ mch_remove(fname); vim_free(fname); vim_free(cmd); --- 4179,4206 ---- && eap->cmdidx != CMD_lgrepadd), qf_cmdtitle(*eap->cmdlinep), enc); if (wp != NULL) + { qi = GET_LOC_LIST(wp); ! if (qi == NULL) ! goto cleanup; ! } ! if (res >= 0) qf_list_changed(qi, qi->qf_curlist); + // Remember the current quickfix list identifier, so that we can + // check for autocommands changing the current quickfix list. + save_qfid = qi->qf_lists[qi->qf_curlist].qf_id; if (au_name != NULL) apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname, TRUE, curbuf); ! if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid)) ! { ! // If autocommands changed the current list, then restore it ! if (qi->qf_lists[qi->qf_curlist].qf_id != save_qfid) ! qi->qf_curlist = qf_id2nr(qi, save_qfid); qf_jump(qi, 0, 0, FALSE); /* display first error */ + } + cleanup: mch_remove(fname); vim_free(fname); vim_free(cmd); *************** *** 4502,4508 **** win_T *wp = NULL; qf_info_T *qi = &ql_info; char_u *au_name = NULL; ! int save_qfid = 0; /* init for gcc */ int res; switch (eap->cmdidx) --- 4527,4533 ---- win_T *wp = NULL; qf_info_T *qi = &ql_info; char_u *au_name = NULL; ! int_u save_qfid = 0; /* init for gcc */ int res; switch (eap->cmdidx) *************** *** 4555,4589 **** && eap->cmdidx != CMD_laddfile), qf_cmdtitle(*eap->cmdlinep), enc); if (wp != NULL) qi = GET_LOC_LIST(wp); ! if (res >= 0 && qi != NULL) qf_list_changed(qi, qi->qf_curlist); ! if (qi != NULL) ! save_qfid = qi->qf_lists[qi->qf_curlist].qf_id; if (au_name != NULL) apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf); ! /* An autocmd might have freed the quickfix/location list. Check whether it ! * is still valid. */ ! if (qi != NULL && !qflist_valid(wp, save_qfid)) ! return; ! if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)) qf_jump(qi, 0, 0, eap->forceit); /* display first error */ ! } ! ! /* ! * Return the quickfix/location list number with the given identifier. ! * Returns -1 if list is not found. ! */ ! static int ! qf_id2nr(qf_info_T *qi, int_u qfid) ! { ! int qf_idx; ! ! for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++) ! if (qi->qf_lists[qf_idx].qf_id == qfid) ! return qf_idx; ! return INVALID_QFIDX; } /* --- 4580,4606 ---- && eap->cmdidx != CMD_laddfile), qf_cmdtitle(*eap->cmdlinep), enc); if (wp != NULL) + { qi = GET_LOC_LIST(wp); ! if (qi == NULL) ! return; ! } ! if (res >= 0) qf_list_changed(qi, qi->qf_curlist); ! save_qfid = qi->qf_lists[qi->qf_curlist].qf_id; if (au_name != NULL) apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf); ! // Jump to the first error for a new list and if autocmds didn't ! // free the list. ! if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile) ! && qflist_valid(wp, save_qfid)) ! { ! // If autocommands changed the current list, then restore it ! if (qi->qf_lists[qi->qf_curlist].qf_id != save_qfid) ! qi->qf_curlist = qf_id2nr(qi, save_qfid); qf_jump(qi, 0, 0, eap->forceit); /* display first error */ ! } } /* *************** *** 5070,5075 **** --- 5087,5096 ---- if (!qflist_valid(wp, save_qfid)) goto theend; + // If autocommands changed the current list, then restore it + if (qi->qf_lists[qi->qf_curlist].qf_id != save_qfid) + qi->qf_curlist = qf_id2nr(qi, save_qfid); + /* Jump to first match. */ if (qi->qf_lists[qi->qf_curlist].qf_count > 0) { *************** *** 5162,5168 **** /* need to open the memfile before putting the buffer in a window */ if (ml_open(newbuf) == OK) { ! /* Make sure this buffer isn't wiped out by auto commands. */ ++newbuf->b_locked; /* set curwin/curbuf to buf and save a few things */ --- 5183,5189 ---- /* need to open the memfile before putting the buffer in a window */ if (ml_open(newbuf) == OK) { ! /* Make sure this buffer isn't wiped out by autocommands. */ ++newbuf->b_locked; /* set curwin/curbuf to buf and save a few things */ *************** *** 6205,6210 **** --- 6226,6233 ---- qf_info_T *qi = &ql_info; char_u *au_name = NULL; int res; + int_u save_qfid; + win_T *wp = NULL; switch (eap->cmdidx) { *************** *** 6233,6238 **** --- 6256,6262 ---- qi = ll_get_or_alloc_list(curwin); if (qi == NULL) return; + wp = curwin; } if (*eap->arg == NUL) *************** *** 6271,6276 **** --- 6295,6304 ---- qf_title, NULL); if (res >= 0) qf_list_changed(qi, qi->qf_curlist); + + // Remember the current quickfix list identifier, so that we can + // check for autocommands changing the current quickfix list. + save_qfid = qi->qf_lists[qi->qf_curlist].qf_id; if (au_name != NULL) { buf_T *curbuf_old = curbuf; *************** *** 6282,6290 **** // be invalid. res = 0; } if (res > 0 && (eap->cmdidx == CMD_cbuffer || ! eap->cmdidx == CMD_lbuffer)) qf_jump(qi, 0, 0, eap->forceit); /* display first error */ } } } --- 6310,6326 ---- // be invalid. res = 0; } + // Jump to the first error for a new list and if autocmds didn't + // free the list. if (res > 0 && (eap->cmdidx == CMD_cbuffer || ! eap->cmdidx == CMD_lbuffer) ! && qflist_valid(wp, save_qfid)) ! { ! // If autocommands changed the current list, then restore it ! if (qi->qf_lists[qi->qf_curlist].qf_id != save_qfid) ! qi->qf_curlist = qf_id2nr(qi, save_qfid); qf_jump(qi, 0, 0, eap->forceit); /* display first error */ + } } } } *************** *** 6301,6306 **** --- 6337,6344 ---- qf_info_T *qi = &ql_info; char_u *au_name = NULL; int res; + int_u save_qfid; + win_T *wp = NULL; switch (eap->cmdidx) { *************** *** 6328,6333 **** --- 6366,6372 ---- qi = ll_get_or_alloc_list(curwin); if (qi == NULL) return; + wp = curwin; } /* Evaluate the expression. When the result is a string or a list we can *************** *** 6345,6358 **** qf_cmdtitle(*eap->cmdlinep), NULL); if (res >= 0) qf_list_changed(qi, qi->qf_curlist); if (au_name != NULL) apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname, TRUE, curbuf); if (res > 0 && (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr) ! && qi == GET_LOC_LIST(curwin)) ! // Jump to the first error if autocmds didn't free the list. qf_jump(qi, 0, 0, eap->forceit); } else EMSG(_("E777: String or List expected")); --- 6384,6408 ---- qf_cmdtitle(*eap->cmdlinep), NULL); if (res >= 0) qf_list_changed(qi, qi->qf_curlist); + + // Remember the current quickfix list identifier, so that we can + // check for autocommands changing the current quickfix list. + save_qfid = qi->qf_lists[qi->qf_curlist].qf_id; if (au_name != NULL) apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname, TRUE, curbuf); + + // Jump to the first error for a new list and if autocmds didn't + // free the list. if (res > 0 && (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr) ! && qflist_valid(wp, save_qfid)) ! { ! // If autocommands changed the current list, then restore it ! if (qi->qf_lists[qi->qf_curlist].qf_id != save_qfid) ! qi->qf_curlist = qf_id2nr(qi, save_qfid); qf_jump(qi, 0, 0, eap->forceit); + } } else EMSG(_("E777: String or List expected")); *** ../vim-8.1.0140/src/testdir/test_quickfix.vim 2018-06-18 20:52:09.553050181 +0200 --- src/testdir/test_quickfix.vim 2018-07-03 16:50:52.302649159 +0200 *************** *** 3363,3375 **** augroup END endfunc ! func Test_setloclist_in_aucmd() " This was using freed memory. augroup nasty ! au * * call setloclist(0, [], 'f') augroup END ! lexpr "x" augroup nasty au! augroup END endfunc --- 3363,3486 ---- augroup END endfunc ! " Test for an autocmd freeing the quickfix/location list when cexpr/lexpr is ! " running ! func Xexpr_acmd_freelist(cchar) ! call s:setup_commands(a:cchar) ! " This was using freed memory. augroup nasty ! au * * call g:Xsetlist([], 'f') augroup END ! Xexpr "x" augroup nasty au! augroup END endfunc + + func Test_cexpr_acmd_freelist() + call Xexpr_acmd_freelist('c') + call Xexpr_acmd_freelist('l') + endfunc + + " Test for commands that create a new quickfix/location list and jump to the + " first error automatically. + func Xjumpto_first_error_test(cchar) + call s:setup_commands(a:cchar) + + call s:create_test_file('Xtestfile1') + call s:create_test_file('Xtestfile2') + let l = ['Xtestfile1:2:Line2', 'Xtestfile2:4:Line4'] + + " Test for cexpr/lexpr + enew + Xexpr l + call assert_equal('Xtestfile1', bufname('')) + call assert_equal(2, line('.')) + + " Test for cfile/lfile + enew + call writefile(l, 'Xerr') + Xfile Xerr + call assert_equal('Xtestfile1', bufname('')) + call assert_equal(2, line('.')) + + " Test for cbuffer/lbuffer + edit Xerr + Xbuffer + call assert_equal('Xtestfile1', bufname('')) + call assert_equal(2, line('.')) + + call delete('Xerr') + call delete('Xtestfile1') + call delete('Xtestfile2') + endfunc + + func Test_jumpto_first_error() + call Xjumpto_first_error_test('c') + call Xjumpto_first_error_test('l') + endfunc + + " Test for a quickfix autocmd changing the quickfix/location list before + " jumping to the first error in the new list. + func Xautocmd_changelist(cchar) + call s:setup_commands(a:cchar) + + " Test for cfile/lfile + call s:create_test_file('Xtestfile1') + call s:create_test_file('Xtestfile2') + Xexpr 'Xtestfile1:2:Line2' + autocmd QuickFixCmdPost * Xolder + call writefile(['Xtestfile2:4:Line4'], 'Xerr') + Xfile Xerr + call assert_equal('Xtestfile2', bufname('')) + call assert_equal(4, line('.')) + autocmd! QuickFixCmdPost + + " Test for cbuffer/lbuffer + call g:Xsetlist([], 'f') + Xexpr 'Xtestfile1:2:Line2' + autocmd QuickFixCmdPost * Xolder + call writefile(['Xtestfile2:4:Line4'], 'Xerr') + edit Xerr + Xbuffer + call assert_equal('Xtestfile2', bufname('')) + call assert_equal(4, line('.')) + autocmd! QuickFixCmdPost + + " Test for cexpr/lexpr + call g:Xsetlist([], 'f') + Xexpr 'Xtestfile1:2:Line2' + autocmd QuickFixCmdPost * Xolder + Xexpr 'Xtestfile2:4:Line4' + call assert_equal('Xtestfile2', bufname('')) + call assert_equal(4, line('.')) + autocmd! QuickFixCmdPost + + " Test for grep/lgrep + call g:Xsetlist([], 'f') + Xexpr 'Xtestfile1:2:Line2' + autocmd QuickFixCmdPost * Xolder + silent Xgrep Line5 Xtestfile2 + call assert_equal('Xtestfile2', bufname('')) + call assert_equal(5, line('.')) + autocmd! QuickFixCmdPost + + " Test for vimgrep/lvimgrep + call g:Xsetlist([], 'f') + Xexpr 'Xtestfile1:2:Line2' + autocmd QuickFixCmdPost * Xolder + silent Xvimgrep Line5 Xtestfile2 + call assert_equal('Xtestfile2', bufname('')) + call assert_equal(5, line('.')) + autocmd! QuickFixCmdPost + + call delete('Xerr') + call delete('Xtestfile1') + call delete('Xtestfile2') + endfunc + + func Test_autocmd_changelist() + call Xautocmd_changelist('c') + call Xautocmd_changelist('l') + endfunc *** ../vim-8.1.0140/src/version.c 2018-07-03 14:48:11.877218375 +0200 --- src/version.c 2018-07-03 16:50:31.718764552 +0200 *************** *** 791,792 **** --- 791,794 ---- { /* Add new patch number below this line */ + /**/ + 141, /**/ -- hundred-and-one symptoms of being an internet addict: 175. You send yourself e-mail before you go to bed to remind you what to do when you wake up. /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ an exciting new programming language -- http://www.Zimbu.org /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///