1
0
forked from aniani/vim

patch 8.1.1112: duplicate code in quickfix file

Problem:    Duplicate code in quickfix file.
Solution:   Move code into functions. (Yegappan Lakshmanan, closes #4207)
This commit is contained in:
Bram Moolenaar 2019-04-04 14:04:11 +02:00
parent fda1bff39f
commit 87f59b09ea
3 changed files with 102 additions and 125 deletions

View File

@ -2160,6 +2160,54 @@ ll_get_or_alloc_list(win_T *wp)
return wp->w_llist; return wp->w_llist;
} }
/*
* Get the quickfix/location list stack to use for the specified Ex command.
* For a location list command, returns the stack for the current window. If
* the location list is not found, then returns NULL and prints an error
* message if 'print_emsg' is TRUE.
*/
static qf_info_T *
qf_cmd_get_stack(exarg_T *eap, int print_emsg)
{
qf_info_T *qi = &ql_info;
if (is_loclist_cmd(eap->cmdidx))
{
qi = GET_LOC_LIST(curwin);
if (qi == NULL)
{
if (print_emsg)
emsg(_(e_loclist));
return NULL;
}
}
return qi;
}
/*
* Get the quickfix/location list stack to use for the specified Ex command.
* For a location list command, returns the stack for the current window.
* If the location list is not present, then allocates a new one.
* Returns NULL if the allocation fails. For a location list command, sets
* 'pwinp' to curwin.
*/
static qf_info_T *
qf_cmd_get_or_alloc_stack(exarg_T *eap, win_T **pwinp)
{
qf_info_T *qi = &ql_info;
if (is_loclist_cmd(eap->cmdidx))
{
qi = ll_get_or_alloc_list(curwin);
if (qi == NULL)
return NULL;
*pwinp = curwin;
}
return qi;
}
/* /*
* Copy location list entries from 'from_qfl' to 'to_qfl'. * Copy location list entries from 'from_qfl' to 'to_qfl'.
*/ */
@ -3512,17 +3560,10 @@ qf_list(exarg_T *eap)
int plus = FALSE; int plus = FALSE;
int all = eap->forceit; // if not :cl!, only show int all = eap->forceit; // if not :cl!, only show
// recognised errors // recognised errors
qf_info_T *qi = &ql_info; qf_info_T *qi;
if (is_loclist_cmd(eap->cmdidx)) if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
{ return;
qi = GET_LOC_LIST(curwin);
if (qi == NULL)
{
emsg(_(e_loclist));
return;
}
}
if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi))) if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
{ {
@ -3647,18 +3688,11 @@ qf_msg(qf_info_T *qi, int which, char *lead)
void void
qf_age(exarg_T *eap) qf_age(exarg_T *eap)
{ {
qf_info_T *qi = &ql_info; qf_info_T *qi;
int count; int count;
if (is_loclist_cmd(eap->cmdidx)) if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
{ return;
qi = GET_LOC_LIST(curwin);
if (qi == NULL)
{
emsg(_(e_loclist));
return;
}
}
if (eap->addr_count != 0) if (eap->addr_count != 0)
count = eap->line2; count = eap->line2;
@ -3695,11 +3729,9 @@ qf_age(exarg_T *eap)
void void
qf_history(exarg_T *eap) qf_history(exarg_T *eap)
{ {
qf_info_T *qi = &ql_info; qf_info_T *qi = qf_cmd_get_stack(eap, FALSE);
int i; int i;
if (is_loclist_cmd(eap->cmdidx))
qi = GET_LOC_LIST(curwin);
if (qf_stack_empty(qi)) if (qf_stack_empty(qi))
msg(_("No entries")); msg(_("No entries"));
else else
@ -3908,16 +3940,12 @@ qf_view_result(int split)
void void
ex_cwindow(exarg_T *eap) ex_cwindow(exarg_T *eap)
{ {
qf_info_T *qi = &ql_info; qf_info_T *qi;
qf_list_T *qfl; qf_list_T *qfl;
win_T *win; win_T *win;
if (is_loclist_cmd(eap->cmdidx)) if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
{ return;
qi = GET_LOC_LIST(curwin);
if (qi == NULL)
return;
}
qfl = qf_get_curlist(qi); qfl = qf_get_curlist(qi);
@ -3946,14 +3974,10 @@ ex_cwindow(exarg_T *eap)
ex_cclose(exarg_T *eap) ex_cclose(exarg_T *eap)
{ {
win_T *win = NULL; win_T *win = NULL;
qf_info_T *qi = &ql_info; qf_info_T *qi;
if (is_loclist_cmd(eap->cmdidx)) if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
{ return;
qi = GET_LOC_LIST(curwin);
if (qi == NULL)
return;
}
// Find existing quickfix window and close it. // Find existing quickfix window and close it.
win = qf_find_win(qi); win = qf_find_win(qi);
@ -4100,21 +4124,14 @@ qf_open_new_cwindow(qf_info_T *qi, int height)
void void
ex_copen(exarg_T *eap) ex_copen(exarg_T *eap)
{ {
qf_info_T *qi = &ql_info; qf_info_T *qi;
qf_list_T *qfl; qf_list_T *qfl;
int height; int height;
int status = FAIL; int status = FAIL;
int lnum; int lnum;
if (is_loclist_cmd(eap->cmdidx)) if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
{ return;
qi = GET_LOC_LIST(curwin);
if (qi == NULL)
{
emsg(_(e_loclist));
return;
}
}
incr_quickfix_busy(); incr_quickfix_busy();
@ -4183,18 +4200,11 @@ qf_win_goto(win_T *win, linenr_T lnum)
void void
ex_cbottom(exarg_T *eap) ex_cbottom(exarg_T *eap)
{ {
qf_info_T *qi = &ql_info; qf_info_T *qi;
win_T *win; win_T *win;
if (is_loclist_cmd(eap->cmdidx)) if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
{ return;
qi = GET_LOC_LIST(curwin);
if (qi == NULL)
{
emsg(_(e_loclist));
return;
}
}
win = qf_find_win(qi); win = qf_find_win(qi);
if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count) if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
@ -4812,19 +4822,14 @@ cleanup:
int int
qf_get_size(exarg_T *eap) qf_get_size(exarg_T *eap)
{ {
qf_info_T *qi = &ql_info; qf_info_T *qi;
qf_list_T *qfl; qf_list_T *qfl;
qfline_T *qfp; qfline_T *qfp;
int i, sz = 0; int i, sz = 0;
int prev_fnum = 0; int prev_fnum = 0;
if (is_loclist_cmd(eap->cmdidx)) if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
{ return 0;
// Location list
qi = GET_LOC_LIST(curwin);
if (qi == NULL)
return 0;
}
qfl = qf_get_curlist(qi); qfl = qf_get_curlist(qi);
FOR_ALL_QFL_ITEMS(qfl, qfp, i) FOR_ALL_QFL_ITEMS(qfl, qfp, i)
@ -4852,15 +4857,10 @@ qf_get_size(exarg_T *eap)
int int
qf_get_cur_idx(exarg_T *eap) qf_get_cur_idx(exarg_T *eap)
{ {
qf_info_T *qi = &ql_info; qf_info_T *qi;
if (is_loclist_cmd(eap->cmdidx)) if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
{ return 0;
// Location list
qi = GET_LOC_LIST(curwin);
if (qi == NULL)
return 0;
}
return qf_get_curlist(qi)->qf_index; return qf_get_curlist(qi)->qf_index;
} }
@ -4872,19 +4872,14 @@ qf_get_cur_idx(exarg_T *eap)
int int
qf_get_cur_valid_idx(exarg_T *eap) qf_get_cur_valid_idx(exarg_T *eap)
{ {
qf_info_T *qi = &ql_info; qf_info_T *qi;
qf_list_T *qfl; qf_list_T *qfl;
qfline_T *qfp; qfline_T *qfp;
int i, eidx = 0; int i, eidx = 0;
int prev_fnum = 0; int prev_fnum = 0;
if (is_loclist_cmd(eap->cmdidx)) if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
{ return 1;
// Location list
qi = GET_LOC_LIST(curwin);
if (qi == NULL)
return 1;
}
qfl = qf_get_curlist(qi); qfl = qf_get_curlist(qi);
qfp = qfl->qf_start; qfp = qfl->qf_start;
@ -4967,18 +4962,11 @@ qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
void void
ex_cc(exarg_T *eap) ex_cc(exarg_T *eap)
{ {
qf_info_T *qi = &ql_info; qf_info_T *qi;
int errornr; int errornr;
if (is_loclist_cmd(eap->cmdidx)) if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
{ return;
qi = GET_LOC_LIST(curwin);
if (qi == NULL)
{
emsg(_(e_loclist));
return;
}
}
if (eap->addr_count > 0) if (eap->addr_count > 0)
errornr = (int)eap->line2; errornr = (int)eap->line2;
@ -5017,19 +5005,12 @@ ex_cc(exarg_T *eap)
void void
ex_cnext(exarg_T *eap) ex_cnext(exarg_T *eap)
{ {
qf_info_T *qi = &ql_info; qf_info_T *qi;
int errornr; int errornr;
int dir; int dir;
if (is_loclist_cmd(eap->cmdidx)) if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
{ return;
qi = GET_LOC_LIST(curwin);
if (qi == NULL)
{
emsg(_(e_loclist));
return;
}
}
if (eap->addr_count > 0 if (eap->addr_count > 0
&& (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
@ -5410,7 +5391,7 @@ ex_vimgrep(exarg_T *eap)
char_u *s; char_u *s;
char_u *p; char_u *p;
int fi; int fi;
qf_info_T *qi = &ql_info; qf_info_T *qi;
qf_list_T *qfl; qf_list_T *qfl;
int_u save_qfid; int_u save_qfid;
win_T *wp = NULL; win_T *wp = NULL;
@ -5439,13 +5420,9 @@ ex_vimgrep(exarg_T *eap)
#endif #endif
} }
if (is_loclist_cmd(eap->cmdidx)) qi = qf_cmd_get_or_alloc_stack(eap, &wp);
{ if (qi == NULL)
qi = ll_get_or_alloc_list(curwin); return;
if (qi == NULL)
return;
wp = curwin;
}
if (eap->addr_count > 0) if (eap->addr_count > 0)
tomatch = eap->line2; tomatch = eap->line2;
@ -6952,7 +6929,7 @@ cbuffer_process_args(
ex_cbuffer(exarg_T *eap) ex_cbuffer(exarg_T *eap)
{ {
buf_T *buf = NULL; buf_T *buf = NULL;
qf_info_T *qi = &ql_info; qf_info_T *qi;
char_u *au_name = NULL; char_u *au_name = NULL;
int res; int res;
int_u save_qfid; int_u save_qfid;
@ -6972,13 +6949,9 @@ ex_cbuffer(exarg_T *eap)
} }
// Must come after autocommands. // Must come after autocommands.
if (is_loclist_cmd(eap->cmdidx)) qi = qf_cmd_get_or_alloc_stack(eap, &wp);
{ if (qi == NULL)
qi = ll_get_or_alloc_list(curwin); return;
if (qi == NULL)
return;
wp = curwin;
}
if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL) if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
return; return;
@ -7059,7 +7032,7 @@ cexpr_get_auname(cmdidx_T cmdidx)
ex_cexpr(exarg_T *eap) ex_cexpr(exarg_T *eap)
{ {
typval_T *tv; typval_T *tv;
qf_info_T *qi = &ql_info; qf_info_T *qi;
char_u *au_name = NULL; char_u *au_name = NULL;
int res; int res;
int_u save_qfid; int_u save_qfid;
@ -7075,13 +7048,9 @@ ex_cexpr(exarg_T *eap)
#endif #endif
} }
if (is_loclist_cmd(eap->cmdidx)) qi = qf_cmd_get_or_alloc_stack(eap, &wp);
{ if (qi == NULL)
qi = ll_get_or_alloc_list(curwin); return;
if (qi == NULL)
return;
wp = curwin;
}
// Evaluate the expression. When the result is a string or a list we can // Evaluate the expression. When the result is a string or a list we can
// use it to fill the errorlist. // use it to fill the errorlist.

View File

@ -163,6 +163,12 @@ endfunc
func XageTests(cchar) func XageTests(cchar)
call s:setup_commands(a:cchar) call s:setup_commands(a:cchar)
if a:cchar == 'l'
" No location list for the current window
call assert_fails('lolder', 'E776:')
call assert_fails('lnewer', 'E776:')
endif
let list = [{'bufnr': bufnr('%'), 'lnum': 1}] let list = [{'bufnr': bufnr('%'), 'lnum': 1}]
call g:Xsetlist(list) call g:Xsetlist(list)

View File

@ -771,6 +771,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 */
/**/
1112,
/**/ /**/
1111, 1111,
/**/ /**/