0
0
mirror of https://github.com/vim/vim.git synced 2025-07-26 11:04:33 -04:00

patch 8.1.1062: quickfix code is repeated

Problem:    Quickfix code is repeated.
Solution:   Define FOR_ALL_QFL_ITEMS(). Move some code to separate functions.
            (Yegappan Lakshmanan, closes #4166)
This commit is contained in:
Bram Moolenaar 2019-03-28 20:31:07 +01:00
parent 0e97b94875
commit a16123a666
2 changed files with 217 additions and 144 deletions

View File

@ -195,6 +195,11 @@ static qf_info_T *ll_get_or_alloc_list(win_T *);
*/ */
#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist) #define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
#define FOR_ALL_QFL_ITEMS(qfl, qfp, i) \
for (i = 0, qfp = qfl->qf_start; \
!got_int && i < qfl->qf_count && qfp != NULL; \
++i, qfp = qfp->qf_next)
/* /*
* Looking up a buffer can be slow if there are many. Remember the last one * Looking up a buffer can be slow if there are many. Remember the last one
* to make this a lot faster if there are multiple matches in the same file. * to make this a lot faster if there are multiple matches in the same file.
@ -2148,9 +2153,7 @@ copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl)
qfline_T *prevp; qfline_T *prevp;
// copy all the location entries in this list // copy all the location entries in this list
for (i = 0, from_qfp = from_qfl->qf_start; FOR_ALL_QFL_ITEMS(from_qfl, from_qfp, i)
i < from_qfl->qf_count && from_qfp != NULL;
++i, from_qfp = from_qfp->qf_next)
{ {
if (qf_add_entry(to_qfl, if (qf_add_entry(to_qfl,
NULL, NULL,
@ -2544,9 +2547,8 @@ is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
int i; int i;
// Search for the entry in the current list // Search for the entry in the current list
for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count; FOR_ALL_QFL_ITEMS(qfl, qfp, i)
++i, qfp = qfp->qf_next) if (qfp == qf_ptr)
if (qfp == NULL || qfp == qf_ptr)
break; break;
if (i == qfl->qf_count) // Entry is not found if (i == qfl->qf_count) // Entry is not found
@ -3792,9 +3794,7 @@ qf_mark_adjust(
qf_list_T *qfl = qf_get_list(qi, idx); qf_list_T *qfl = qf_get_list(qi, idx);
if (!qf_list_empty(qfl)) if (!qf_list_empty(qfl))
for (i = 0, qfp = qfl->qf_start; FOR_ALL_QFL_ITEMS(qfl, qfp, i)
i < qfl->qf_count && qfp != NULL;
++i, qfp = qfp->qf_next)
if (qfp->qf_fnum == curbuf->b_fnum) if (qfp->qf_fnum == curbuf->b_fnum)
{ {
found_one = TRUE; found_one = TRUE;
@ -4819,8 +4819,7 @@ qf_get_size(exarg_T *eap)
} }
qfl = qf_get_curlist(qi); qfl = qf_get_curlist(qi);
for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count && qfp != NULL; FOR_ALL_QFL_ITEMS(qfl, qfp, i)
++i, qfp = qfp->qf_next)
{ {
if (qfp->qf_valid) if (qfp->qf_valid)
{ {
@ -5055,6 +5054,24 @@ ex_cnext(exarg_T *eap)
qf_jump(qi, dir, errornr, eap->forceit); qf_jump(qi, dir, errornr, eap->forceit);
} }
/*
* Return the autocmd name for the :cfile Ex commands
*/
static char_u *
cfile_get_auname(cmdidx_T cmdidx)
{
switch (cmdidx)
{
case CMD_cfile: return (char_u *)"cfile";
case CMD_cgetfile: return (char_u *)"cgetfile";
case CMD_caddfile: return (char_u *)"caddfile";
case CMD_lfile: return (char_u *)"lfile";
case CMD_lgetfile: return (char_u *)"lgetfile";
case CMD_laddfile: return (char_u *)"laddfile";
default: return NULL;
}
}
/* /*
* ":cfile"/":cgetfile"/":caddfile" commands. * ":cfile"/":cgetfile"/":caddfile" commands.
* ":lfile"/":lgetfile"/":laddfile" commands. * ":lfile"/":lgetfile"/":laddfile" commands.
@ -5069,18 +5086,10 @@ ex_cfile(exarg_T *eap)
int_u save_qfid = 0; // init for gcc int_u save_qfid = 0; // init for gcc
int res; int res;
switch (eap->cmdidx) au_name = cfile_get_auname(eap->cmdidx);
{
case CMD_cfile: au_name = (char_u *)"cfile"; break;
case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
case CMD_lfile: au_name = (char_u *)"lfile"; break;
case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
default: break;
}
if (au_name != NULL) if (au_name != NULL)
apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf); apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc; enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
#ifdef FEAT_BROWSE #ifdef FEAT_BROWSE
if (cmdmod.browse) if (cmdmod.browse)
@ -5832,44 +5841,16 @@ unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
#if defined(FEAT_EVAL) || defined(PROTO) #if defined(FEAT_EVAL) || defined(PROTO)
/* /*
* Add each quickfix error to list "list" as a dictionary. * Copy the specified quickfix entry items into a new dict and appened the dict
* If qf_idx is -1, use the current list. Otherwise, use the specified list. * to 'list'. Returns OK on success.
*/ */
int static int
get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list) get_qfline_items(qfline_T *qfp, list_T *list)
{ {
qf_info_T *qi = qi_arg; int bufnum;
qf_list_T *qfl;
dict_T *dict; dict_T *dict;
char_u buf[2]; char_u buf[2];
qfline_T *qfp;
int i;
int bufnum;
if (qi == NULL)
{
qi = &ql_info;
if (wp != NULL)
{
qi = GET_LOC_LIST(wp);
if (qi == NULL)
return FAIL;
}
}
if (qf_idx == INVALID_QFIDX)
qf_idx = qi->qf_curlist;
if (qf_idx >= qi->qf_listcount)
return FAIL;
qfl = qf_get_list(qi, qf_idx);
if (qf_list_empty(qfl))
return FAIL;
qfp = qfl->qf_start;
for (i = 1; !got_int && i <= qfl->qf_count; ++i)
{
// Handle entries with a non-existing buffer number. // Handle entries with a non-existing buffer number.
bufnum = qfp->qf_fnum; bufnum = qfp->qf_fnum;
if (bufnum != 0 && (buflist_findnr(bufnum) == NULL)) if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
@ -5894,10 +5875,48 @@ get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
|| dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL) || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
return FAIL; return FAIL;
qfp = qfp->qf_next; return OK;
if (qfp == NULL)
break;
} }
/*
* Add each quickfix error to list "list" as a dictionary.
* If qf_idx is -1, use the current list. Otherwise, use the specified list.
*/
int
get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
{
qf_info_T *qi = qi_arg;
qf_list_T *qfl;
qfline_T *qfp;
int i;
if (qi == NULL)
{
qi = &ql_info;
if (wp != NULL)
{
qi = GET_LOC_LIST(wp);
if (qi == NULL)
return FAIL;
}
}
if (qf_idx == INVALID_QFIDX)
qf_idx = qi->qf_curlist;
if (qf_idx >= qi->qf_listcount)
return FAIL;
qfl = qf_get_list(qi, qf_idx);
if (qf_list_empty(qfl))
return FAIL;
FOR_ALL_QFL_ITEMS(qfl, qfp, i)
{
if (get_qfline_items(qfp, list) == FAIL)
return FAIL;
}
return OK; return OK;
} }
@ -6841,6 +6860,74 @@ set_ref_in_quickfix(int copyID)
} }
#endif #endif
/*
* Return the autocmd name for the :cbuffer Ex commands
*/
static char_u *
cbuffer_get_auname(cmdidx_T cmdidx)
{
switch (cmdidx)
{
case CMD_cbuffer: return (char_u *)"cbuffer";
case CMD_cgetbuffer: return (char_u *)"cgetbuffer";
case CMD_caddbuffer: return (char_u *)"caddbuffer";
case CMD_lbuffer: return (char_u *)"lbuffer";
case CMD_lgetbuffer: return (char_u *)"lgetbuffer";
case CMD_laddbuffer: return (char_u *)"laddbuffer";
default: return NULL;
}
}
/*
* Process and validate the arguments passed to the :cbuffer, :caddbuffer,
* :cgetbuffer, :lbuffer, :laddbuffer, :lgetbuffer Ex commands.
*/
static int
cbuffer_process_args(
exarg_T *eap,
buf_T **bufp,
linenr_T *line1,
linenr_T *line2)
{
buf_T *buf = NULL;
if (*eap->arg == NUL)
buf = curbuf;
else if (*skipwhite(skipdigits(eap->arg)) == NUL)
buf = buflist_findnr(atoi((char *)eap->arg));
if (buf == NULL)
{
emsg(_(e_invarg));
return FAIL;
}
if (buf->b_ml.ml_mfp == NULL)
{
emsg(_("E681: Buffer is not loaded"));
return FAIL;
}
if (eap->addr_count == 0)
{
eap->line1 = 1;
eap->line2 = buf->b_ml.ml_line_count;
}
if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
|| eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
{
emsg(_(e_invrange));
return FAIL;
}
*line1 = eap->line1;
*line2 = eap->line2;
*bufp = buf;
return OK;
}
/* /*
* ":[range]cbuffer [bufnr]" command. * ":[range]cbuffer [bufnr]" command.
* ":[range]caddbuffer [bufnr]" command. * ":[range]caddbuffer [bufnr]" command.
@ -6858,17 +6945,11 @@ ex_cbuffer(exarg_T *eap)
int res; int res;
int_u save_qfid; int_u save_qfid;
win_T *wp = NULL; win_T *wp = NULL;
char_u *qf_title;
linenr_T line1;
linenr_T line2;
switch (eap->cmdidx) au_name = cbuffer_get_auname(eap->cmdidx);
{
case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
default: break;
}
if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
curbuf->b_fname, TRUE, curbuf)) curbuf->b_fname, TRUE, curbuf))
{ {
@ -6887,27 +6968,10 @@ ex_cbuffer(exarg_T *eap)
wp = curwin; wp = curwin;
} }
if (*eap->arg == NUL) if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
buf = curbuf; return;
else if (*skipwhite(skipdigits(eap->arg)) == NUL)
buf = buflist_findnr(atoi((char *)eap->arg)); qf_title = qf_cmdtitle(*eap->cmdlinep);
if (buf == NULL)
emsg(_(e_invarg));
else if (buf->b_ml.ml_mfp == NULL)
emsg(_("E681: Buffer is not loaded"));
else
{
if (eap->addr_count == 0)
{
eap->line1 = 1;
eap->line2 = buf->b_ml.ml_line_count;
}
if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
|| eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
emsg(_(e_invrange));
else
{
char_u *qf_title = qf_cmdtitle(*eap->cmdlinep);
if (buf->b_sfname) if (buf->b_sfname)
{ {
@ -6921,7 +6985,7 @@ ex_cbuffer(exarg_T *eap)
res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm, res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
(eap->cmdidx != CMD_caddbuffer (eap->cmdidx != CMD_caddbuffer
&& eap->cmdidx != CMD_laddbuffer), && eap->cmdidx != CMD_laddbuffer),
eap->line1, eap->line2, line1, line2,
qf_title, NULL); qf_title, NULL);
if (qf_stack_empty(qi)) if (qf_stack_empty(qi))
{ {
@ -6938,8 +7002,8 @@ ex_cbuffer(exarg_T *eap)
{ {
buf_T *curbuf_old = curbuf; buf_T *curbuf_old = curbuf;
apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname,
curbuf->b_fname, TRUE, curbuf); TRUE, curbuf);
if (curbuf != curbuf_old) if (curbuf != curbuf_old)
// Autocommands changed buffer, don't jump now, "qi" may // Autocommands changed buffer, don't jump now, "qi" may
// be invalid. // be invalid.
@ -6955,10 +7019,26 @@ ex_cbuffer(exarg_T *eap)
decr_quickfix_busy(); decr_quickfix_busy();
} }
#if defined(FEAT_EVAL) || defined(PROTO)
/*
* Return the autocmd name for the :cexpr Ex commands.
*/
static char_u *
cexpr_get_auname(cmdidx_T cmdidx)
{
switch (cmdidx)
{
case CMD_cexpr: return (char_u *)"cexpr";
case CMD_cgetexpr: return (char_u *)"cgetexpr";
case CMD_caddexpr: return (char_u *)"caddexpr";
case CMD_lexpr: return (char_u *)"lexpr";
case CMD_lgetexpr: return (char_u *)"lgetexpr";
case CMD_laddexpr: return (char_u *)"laddexpr";
default: return NULL;
} }
} }
#if defined(FEAT_EVAL) || defined(PROTO)
/* /*
* ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command. * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
* ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command. * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
@ -6973,16 +7053,7 @@ ex_cexpr(exarg_T *eap)
int_u save_qfid; int_u save_qfid;
win_T *wp = NULL; win_T *wp = NULL;
switch (eap->cmdidx) au_name = cexpr_get_auname(eap->cmdidx);
{
case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
default: break;
}
if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
curbuf->b_fname, TRUE, curbuf)) curbuf->b_fname, TRUE, curbuf))
{ {
@ -7257,10 +7328,6 @@ ex_helpgrep(exarg_T *eap)
#endif #endif
} }
// Make 'cpoptions' empty, the 'l' flag should not be used here.
save_cpo = p_cpo;
p_cpo = empty_option;
if (is_loclist_cmd(eap->cmdidx)) if (is_loclist_cmd(eap->cmdidx))
{ {
qi = hgr_get_ll(&new_qi); qi = hgr_get_ll(&new_qi);
@ -7268,6 +7335,10 @@ ex_helpgrep(exarg_T *eap)
return; return;
} }
// Make 'cpoptions' empty, the 'l' flag should not be used here.
save_cpo = p_cpo;
p_cpo = empty_option;
incr_quickfix_busy(); incr_quickfix_busy();
#ifdef FEAT_MULTI_LANG #ifdef FEAT_MULTI_LANG

View File

@ -775,6 +775,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
1062,
/**/ /**/
1061, 1061,
/**/ /**/