#include "vim.h"
#if defined(FEAT_QUICKFIX) || defined(PROTO)
struct dir_stack_T
{
struct dir_stack_T *next;
char_u *dirname;
};
static struct dir_stack_T *dir_stack = NULL;
typedef struct qfline_S qfline_T;
struct qfline_S
{
qfline_T *qf_next;
qfline_T *qf_prev;
linenr_T qf_lnum;
int qf_fnum;
int qf_col;
int qf_nr;
char_u *qf_pattern;
char_u *qf_text;
char_u qf_viscol;
char_u qf_cleared;
char_u qf_type;
char_u qf_valid;
};
#define LISTCOUNT 10
typedef struct qf_list_S
{
qfline_T *qf_start;
qfline_T *qf_ptr;
int qf_count;
int qf_index;
int qf_nonevalid;
char_u *qf_title;
} qf_list_T;
struct qf_info_S
{
int qf_refcount;
int qf_listcount;
int qf_curlist;
qf_list_T qf_lists[LISTCOUNT];
};
static qf_info_T ql_info;
#define FMT_PATTERNS 10
typedef struct efm_S efm_T;
struct efm_S
{
regprog_T *prog;
efm_T *next;
char_u addr[FMT_PATTERNS];
char_u prefix;
char_u flags;
int conthere;
};
static int qf_init_ext __ARGS((qf_info_T *qi, char_u *efile, buf_T *buf, typval_T *tv, char_u *errorformat, int newlist, linenr_T lnumfirst, linenr_T lnumlast, char_u *qf_title));
static void qf_new_list __ARGS((qf_info_T *qi, char_u *qf_title));
static void ll_free_all __ARGS((qf_info_T **pqi));
static int qf_add_entry __ARGS((qf_info_T *qi, qfline_T **prevp, char_u *dir, char_u *fname, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid));
static qf_info_T *ll_new_list __ARGS((void));
static void qf_msg __ARGS((qf_info_T *qi));
static void qf_free __ARGS((qf_info_T *qi, int idx));
static char_u *qf_types __ARGS((int, int));
static int qf_get_fnum __ARGS((char_u *, char_u *));
static char_u *qf_push_dir __ARGS((char_u *, struct dir_stack_T **));
static char_u *qf_pop_dir __ARGS((struct dir_stack_T **));
static char_u *qf_guess_filepath __ARGS((char_u *));
static void qf_fmt_text __ARGS((char_u *text, char_u *buf, int bufsize));
static void qf_clean_dir_stack __ARGS((struct dir_stack_T **));
#ifdef FEAT_WINDOWS
static int qf_win_pos_update __ARGS((qf_info_T *qi, int old_qf_index));
static int is_qf_win __ARGS((win_T *win, qf_info_T *qi));
static win_T *qf_find_win __ARGS((qf_info_T *qi));
static buf_T *qf_find_buf __ARGS((qf_info_T *qi));
static void qf_update_buffer __ARGS((qf_info_T *qi));
static void qf_fill_buffer __ARGS((qf_info_T *qi));
#endif
static char_u *get_mef_name __ARGS((void));
static buf_T *load_dummy_buffer __ARGS((char_u *fname));
static void wipe_dummy_buffer __ARGS((buf_T *buf));
static void unload_dummy_buffer __ARGS((buf_T *buf));
static qf_info_T *ll_get_or_alloc_list __ARGS((win_T *));
#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
int
qf_init(wp, efile, errorformat, newlist, qf_title)
win_T *wp;
char_u *efile;
char_u *errorformat;
int newlist;
char_u *qf_title;
{
qf_info_T *qi = &ql_info;
if (efile == NULL)
return FAIL;
if (wp != NULL)
{
qi = ll_get_or_alloc_list(wp);
if (qi == NULL)
return FAIL;
}
return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
(linenr_T)0, (linenr_T)0,
qf_title);
}
static int
qf_init_ext(qi, efile, buf, tv, errorformat, newlist, lnumfirst, lnumlast,
qf_title)
qf_info_T *qi;
char_u *efile;
buf_T *buf;
typval_T *tv;
char_u *errorformat;
int newlist;
linenr_T lnumfirst;
linenr_T lnumlast;
char_u *qf_title;
{
char_u *namebuf;
char_u *errmsg;
char_u *pattern;
char_u *fmtstr = NULL;
int col = 0;
char_u use_viscol = FALSE;
int type = 0;
int valid;
linenr_T buflnum = lnumfirst;
long lnum = 0L;
int enr = 0;
FILE *fd = NULL;
qfline_T *qfprev = NULL;
char_u *efmp;
efm_T *fmt_first = NULL;
efm_T *fmt_last = NULL;
efm_T *fmt_ptr;
efm_T *fmt_start = NULL;
char_u *efm;
char_u *ptr;
char_u *srcptr;
int len;
int i;
int round;
int idx = 0;
int multiline = FALSE;
int multiignore = FALSE;
int multiscan = FALSE;
int retval = -1;
char_u *directory = NULL;
char_u *currfile = NULL;
char_u *tail = NULL;
char_u *p_str = NULL;
listitem_T *p_li = NULL;
struct dir_stack_T *file_stack = NULL;
regmatch_T regmatch;
static struct fmtpattern
{
char_u convchar;
char *pattern;
} fmt_pat[FMT_PATTERNS] =
{
{'f', ".\\+"},
{'n', "\\d\\+"},
{'l', "\\d\\+"},
{'c', "\\d\\+"},
{'t', "."},
{'m', ".\\+"},
{'r', ".*"},
{'p', "[- .]*"},
{'v', "\\d\\+"},
{'s', ".\\+"}
};
namebuf = alloc(CMDBUFFSIZE + 1);
errmsg = alloc(CMDBUFFSIZE + 1);
pattern = alloc(CMDBUFFSIZE + 1);
if (namebuf == NULL || errmsg == NULL || pattern == NULL)
goto qf_init_end;
if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
{
EMSG2(_(e_openerrf), efile);
goto qf_init_end;
}
if (newlist || qi->qf_curlist == qi->qf_listcount)
qf_new_list(qi, qf_title);
else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
for (qfprev = qi->qf_lists[qi->qf_curlist].qf_start;
qfprev->qf_next != qfprev; qfprev = qfprev->qf_next)
;
if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
efm = buf->b_p_efm;
else
efm = errorformat;
i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
for (round = FMT_PATTERNS; round > 0; )
i += (int)STRLEN(fmt_pat[--round].pattern);
#ifdef COLON_IN_FILENAME
i += 12;
#else
i += 2;
#endif
if ((fmtstr = alloc(i)) == NULL)
goto error2;
while (efm[0] != NUL)
{
fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
if (fmt_ptr == NULL)
goto error2;
if (fmt_first == NULL)
fmt_first = fmt_ptr;
else
fmt_last->next = fmt_ptr;
fmt_last = fmt_ptr;
for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
if (efm[len] == '\\' && efm[len + 1] != NUL)
++len;
ptr = fmtstr;
*ptr++ = '^';
round = 0;
for (efmp = efm; efmp < efm + len; ++efmp)
{
if (*efmp == '%')
{
++efmp;
for (idx = 0; idx < FMT_PATTERNS; ++idx)
if (fmt_pat[idx].convchar == *efmp)
break;
if (idx < FMT_PATTERNS)
{
if (fmt_ptr->addr[idx])
{
sprintf((char *)errmsg,
_("E372: Too many %%%c in format string"), *efmp);
EMSG(errmsg);
goto error2;
}
if ((idx
&& idx < 6
&& vim_strchr((char_u *)"DXOPQ",
fmt_ptr->prefix) != NULL)
|| (idx == 6
&& vim_strchr((char_u *)"OPQ",
fmt_ptr->prefix) == NULL))
{
sprintf((char *)errmsg,
_("E373: Unexpected %%%c in format string"), *efmp);
EMSG(errmsg);
goto error2;
}
fmt_ptr->addr[idx] = (char_u)++round;
*ptr++ = '\\';
*ptr++ = '(';
#ifdef BACKSLASH_IN_FILENAME
if (*efmp == 'f')
{
STRCPY(ptr, "\\%(\\a:\\)\\=");
ptr += 10;
}
#endif
if (*efmp == 'f' && efmp[1] != NUL)
{
if (efmp[1] != '\\' && efmp[1] != '%')
{
STRCPY(ptr, ".\\{-1,}");
ptr += 7;
}
else
{
STRCPY(ptr, "\\f\\+");
ptr += 4;
}
}
else
{
srcptr = (char_u *)fmt_pat[idx].pattern;
while ((*ptr = *srcptr++) != NUL)
++ptr;
}
*ptr++ = '\\';
*ptr++ = ')';
}
else if (*efmp == '*')
{
if (*++efmp == '[' || *efmp == '\\')
{
if ((*ptr++ = *efmp) == '[')
{
if (efmp[1] == '^')
*ptr++ = *++efmp;
if (efmp < efm + len)
{
*ptr++ = *++efmp;
while (efmp < efm + len
&& (*ptr++ = *++efmp) != ']')
;
if (efmp == efm + len)
{
EMSG(_("E374: Missing ] in format string"));
goto error2;
}
}
}
else if (efmp < efm + len)
*ptr++ = *++efmp;
*ptr++ = '\\';
*ptr++ = '+';
}
else
{
sprintf((char *)errmsg,
_("E375: Unsupported %%%c in format string"), *efmp);
EMSG(errmsg);
goto error2;
}
}
else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
*ptr++ = *efmp;
else if (*efmp == '#')
*ptr++ = '*';
else if (*efmp == '>')
fmt_ptr->conthere = TRUE;
else if (efmp == efm + 1)
{
if (vim_strchr((char_u *)"+-", *efmp) != NULL)
fmt_ptr->flags = *efmp++;
if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
fmt_ptr->prefix = *efmp;
else
{
sprintf((char *)errmsg,
_("E376: Invalid %%%c in format string prefix"), *efmp);
EMSG(errmsg);
goto error2;
}
}
else
{
sprintf((char *)errmsg,
_("E377: Invalid %%%c in format string"), *efmp);
EMSG(errmsg);
goto error2;
}
}
else
{
if (*efmp == '\\' && efmp + 1 < efm + len)
++efmp;
else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
*ptr++ = '\\';
if (*efmp)
*ptr++ = *efmp;
}
}
*ptr++ = '$';
*ptr = NUL;
if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
goto error2;
efm = skip_to_option_part(efm + len);
}
if (fmt_first == NULL)
{
EMSG(_("E378: 'errorformat' contains no pattern"));
goto error2;
}
got_int = FALSE;
regmatch.rm_ic = TRUE;
if (tv != NULL)
{
if (tv->v_type == VAR_STRING)
p_str = tv->vval.v_string;
else if (tv->v_type == VAR_LIST)
p_li = tv->vval.v_list->lv_first;
}
while (!got_int)
{
if (fd == NULL)
{
if (tv != NULL)
{
if (tv->v_type == VAR_STRING)
{
char_u *p;
if (!*p_str)
break;
p = vim_strchr(p_str, '\n');
if (p)
len = (int)(p - p_str + 1);
else
len = (int)STRLEN(p_str);
if (len > CMDBUFFSIZE - 2)
vim_strncpy(IObuff, p_str, CMDBUFFSIZE - 2);
else
vim_strncpy(IObuff, p_str, len);
p_str += len;
}
else if (tv->v_type == VAR_LIST)
{
while (p_li && p_li->li_tv.v_type != VAR_STRING)
p_li = p_li->li_next;
if (!p_li)
break;
len = (int)STRLEN(p_li->li_tv.vval.v_string);
if (len > CMDBUFFSIZE - 2)
len = CMDBUFFSIZE - 2;
vim_strncpy(IObuff, p_li->li_tv.vval.v_string, len);
p_li = p_li->li_next;
}
}
else
{
if (buflnum > lnumlast)
break;
vim_strncpy(IObuff, ml_get_buf(buf, buflnum++, FALSE),
CMDBUFFSIZE - 2);
}
}
else if (fgets((char *)IObuff, CMDBUFFSIZE - 2, fd) == NULL)
break;
IObuff[CMDBUFFSIZE - 2] = NUL;
if ((efmp = vim_strrchr(IObuff, '\n')) != NULL)
*efmp = NUL;
#ifdef USE_CRNL
if ((efmp = vim_strrchr(IObuff, '\r')) != NULL)
*efmp = NUL;
#endif
if (fmt_start == NULL)
fmt_ptr = fmt_first;
else
{
fmt_ptr = fmt_start;
fmt_start = NULL;
}
valid = TRUE;
restofline:
for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
{
idx = fmt_ptr->prefix;
if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
continue;
namebuf[0] = NUL;
pattern[0] = NUL;
if (!multiscan)
errmsg[0] = NUL;
lnum = 0;
col = 0;
use_viscol = FALSE;
enr = -1;
type = 0;
tail = NULL;
regmatch.regprog = fmt_ptr->prog;
if (vim_regexec(®match, IObuff, (colnr_T)0))
{
if ((idx == 'C' || idx == 'Z') && !multiline)
continue;
if (vim_strchr((char_u *)"EWI", idx) != NULL)
type = idx;
else
type = 0;
if ((i = (int)fmt_ptr->addr[0]) > 0)
{
int c;
if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
continue;
c = *regmatch.endp[i];
*regmatch.endp[i] = NUL;
expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
*regmatch.endp[i] = c;
if (vim_strchr((char_u *)"OPQ", idx) != NULL
&& mch_getperm(namebuf) == -1)
continue;
}
if ((i = (int)fmt_ptr->addr[1]) > 0)
{
if (regmatch.startp[i] == NULL)
continue;
enr = (int)atol((char *)regmatch.startp[i]);
}
if ((i = (int)fmt_ptr->addr[2]) > 0)
{
if (regmatch.startp[i] == NULL)
continue;
lnum = atol((char *)regmatch.startp[i]);
}
if ((i = (int)fmt_ptr->addr[3]) > 0)
{
if (regmatch.startp[i] == NULL)
continue;
col = (int)atol((char *)regmatch.startp[i]);
}
if ((i = (int)fmt_ptr->addr[4]) > 0)
{
if (regmatch.startp[i] == NULL)
continue;
type = *regmatch.startp[i];
}
if (fmt_ptr->flags == '+' && !multiscan)
STRCPY(errmsg, IObuff);
else if ((i = (int)fmt_ptr->addr[5]) > 0)
{
if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
continue;
len = (int)(regmatch.endp[i] - regmatch.startp[i]);
vim_strncpy(errmsg, regmatch.startp[i], len);
}
if ((i = (int)fmt_ptr->addr[6]) > 0)
{
if (regmatch.startp[i] == NULL)
continue;
tail = regmatch.startp[i];
}
if ((i = (int)fmt_ptr->addr[7]) > 0)
{
if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
continue;
col = (int)(regmatch.endp[i] - regmatch.startp[i] + 1);
if (*((char_u *)regmatch.startp[i]) != TAB)
use_viscol = TRUE;
}
if ((i = (int)fmt_ptr->addr[8]) > 0)
{
if (regmatch.startp[i] == NULL)
continue;
col = (int)atol((char *)regmatch.startp[i]);
use_viscol = TRUE;
}
if ((i = (int)fmt_ptr->addr[9]) > 0)
{
if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
continue;
len = (int)(regmatch.endp[i] - regmatch.startp[i]);
if (len > CMDBUFFSIZE - 5)
len = CMDBUFFSIZE - 5;
STRCPY(pattern, "^\\V");
STRNCAT(pattern, regmatch.startp[i], len);
pattern[len + 3] = '\\';
pattern[len + 4] = '$';
pattern[len + 5] = NUL;
}
break;
}
}
multiscan = FALSE;
if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
{
if (fmt_ptr != NULL)
{
if (idx == 'D')
{
if (*namebuf == NUL)
{
EMSG(_("E379: Missing or empty directory name"));
goto error2;
}
if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
goto error2;
}
else if (idx == 'X')
directory = qf_pop_dir(&dir_stack);
}
namebuf[0] = NUL;
lnum = 0;
valid = FALSE;
STRCPY(errmsg, IObuff);
if (fmt_ptr == NULL)
multiline = multiignore = FALSE;
}
else if (fmt_ptr != NULL)
{
if (fmt_ptr->conthere)
fmt_start = fmt_ptr;
if (vim_strchr((char_u *)"AEWI", idx) != NULL)
multiline = TRUE;
else if (vim_strchr((char_u *)"CZ", idx) != NULL)
{
if (qfprev == NULL)
goto error2;
if (*errmsg && !multiignore)
{
len = (int)STRLEN(qfprev->qf_text);
if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
== NULL)
goto error2;
STRCPY(ptr, qfprev->qf_text);
vim_free(qfprev->qf_text);
qfprev->qf_text = ptr;
*(ptr += len) = '\n';
STRCPY(++ptr, errmsg);
}
if (qfprev->qf_nr == -1)
qfprev->qf_nr = enr;
if (vim_isprintc(type) && !qfprev->qf_type)
qfprev->qf_type = type;
if (!qfprev->qf_lnum)
qfprev->qf_lnum = lnum;
if (!qfprev->qf_col)
qfprev->qf_col = col;
qfprev->qf_viscol = use_viscol;
if (!qfprev->qf_fnum)
qfprev->qf_fnum = qf_get_fnum(directory,
*namebuf || directory ? namebuf
: currfile && valid ? currfile : 0);
if (idx == 'Z')
multiline = multiignore = FALSE;
line_breakcheck();
continue;
}
else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
{
valid = FALSE;
if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
{
if (*namebuf && idx == 'P')
currfile = qf_push_dir(namebuf, &file_stack);
else if (idx == 'Q')
currfile = qf_pop_dir(&file_stack);
*namebuf = NUL;
if (tail && *tail)
{
STRMOVE(IObuff, skipwhite(tail));
multiscan = TRUE;
goto restofline;
}
}
}
if (fmt_ptr->flags == '-')
{
if (multiline)
multiignore = TRUE;
continue;
}
}
if (qf_add_entry(qi, &qfprev,
directory,
(*namebuf || directory)
? namebuf
: ((currfile && valid) ? currfile : (char_u *)NULL),
0,
errmsg,
lnum,
col,
use_viscol,
pattern,
enr,
type,
valid) == FAIL)
goto error2;
line_breakcheck();
}
if (fd == NULL || !ferror(fd))
{
if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
{
qi->qf_lists[qi->qf_curlist].qf_ptr =
qi->qf_lists[qi->qf_curlist].qf_start;
qi->qf_lists[qi->qf_curlist].qf_index = 1;
qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
}
else
{
qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
qi->qf_lists[qi->qf_curlist].qf_ptr =
qi->qf_lists[qi->qf_curlist].qf_start;
}
retval = qi->qf_lists[qi->qf_curlist].qf_count;
goto qf_init_ok;
}
EMSG(_(e_readerrf));
error2:
qf_free(qi, qi->qf_curlist);
qi->qf_listcount--;
if (qi->qf_curlist > 0)
--qi->qf_curlist;
qf_init_ok:
if (fd != NULL)
fclose(fd);
for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
{
fmt_first = fmt_ptr->next;
vim_free(fmt_ptr->prog);
vim_free(fmt_ptr);
}
qf_clean_dir_stack(&dir_stack);
qf_clean_dir_stack(&file_stack);
qf_init_end:
vim_free(namebuf);
vim_free(errmsg);
vim_free(pattern);
vim_free(fmtstr);
#ifdef FEAT_WINDOWS
qf_update_buffer(qi);
#endif
return retval;
}
static void
qf_new_list(qi, qf_title)
qf_info_T *qi;
char_u *qf_title;
{
int i;
while (qi->qf_listcount > qi->qf_curlist + 1)
qf_free(qi, --qi->qf_listcount);
if (qi->qf_listcount == LISTCOUNT)
{
qf_free(qi, 0);
for (i = 1; i < LISTCOUNT; ++i)
qi->qf_lists[i - 1] = qi->qf_lists[i];
qi->qf_curlist = LISTCOUNT - 1;
}
else
qi->qf_curlist = qi->qf_listcount++;
qi->qf_lists[qi->qf_curlist].qf_index = 0;
qi->qf_lists[qi->qf_curlist].qf_count = 0;
if (qf_title != NULL)
{
char_u *p = alloc((int)STRLEN(qf_title) + 2);
qi->qf_lists[qi->qf_curlist].qf_title = p;
if (p != NULL)
sprintf((char *)p, ":%s", (char *)qf_title);
}
else
qi->qf_lists[qi->qf_curlist].qf_title = NULL;
}
static void
ll_free_all(pqi)
qf_info_T **pqi;
{
int i;
qf_info_T *qi;
qi = *pqi;
if (qi == NULL)
return;
*pqi = NULL;
qi->qf_refcount--;
if (qi->qf_refcount < 1)
{
for (i = 0; i < qi->qf_listcount; ++i)
qf_free(qi, i);
vim_free(qi);
}
}
void
qf_free_all(wp)
win_T *wp;
{
int i;
qf_info_T *qi = &ql_info;
if (wp != NULL)
{
ll_free_all(&wp->w_llist);
ll_free_all(&wp->w_llist_ref);
}
else
for (i = 0; i < qi->qf_listcount; ++i)
qf_free(qi, i);
}
static int
qf_add_entry(qi, prevp, dir, fname, bufnum, mesg, lnum, col, vis_col, pattern,
nr, type, valid)
qf_info_T *qi;
qfline_T **prevp;
char_u *dir;
char_u *fname;
int bufnum;
char_u *mesg;
long lnum;
int col;
int vis_col;
char_u *pattern;
int nr;
int type;
int valid;
{
qfline_T *qfp;
if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
return FAIL;
if (bufnum != 0)
qfp->qf_fnum = bufnum;
else
qfp->qf_fnum = qf_get_fnum(dir, fname);
if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
{
vim_free(qfp);
return FAIL;
}
qfp->qf_lnum = lnum;
qfp->qf_col = col;
qfp->qf_viscol = vis_col;
if (pattern == NULL || *pattern == NUL)
qfp->qf_pattern = NULL;
else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
{
vim_free(qfp->qf_text);
vim_free(qfp);
return FAIL;
}
qfp->qf_nr = nr;
if (type != 1 && !vim_isprintc(type))
type = 0;
qfp->qf_type = type;
qfp->qf_valid = valid;
if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
{
qi->qf_lists[qi->qf_curlist].qf_start = qfp;
qfp->qf_prev = qfp;
}
else
{
qfp->qf_prev = *prevp;
(*prevp)->qf_next = qfp;
}
qfp->qf_next = qfp;
qfp->qf_cleared = FALSE;
*prevp = qfp;
++qi->qf_lists[qi->qf_curlist].qf_count;
if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
{
qi->qf_lists[qi->qf_curlist].qf_index =
qi->qf_lists[qi->qf_curlist].qf_count;
qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
}
return OK;
}
static qf_info_T *
ll_new_list()
{
qf_info_T *qi;
qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
if (qi != NULL)
{
vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
qi->qf_refcount++;
}
return qi;
}
static qf_info_T *
ll_get_or_alloc_list(wp)
win_T *wp;
{
if (IS_LL_WINDOW(wp))
return wp->w_llist_ref;
ll_free_all(&wp->w_llist_ref);
if (wp->w_llist == NULL)
wp->w_llist = ll_new_list();
return wp->w_llist;
}
void
copy_loclist(from, to)
win_T *from;
win_T *to;
{
qf_info_T *qi;
int idx;
int i;
if (IS_LL_WINDOW(from))
qi = from->w_llist_ref;
else
qi = from->w_llist;
if (qi == NULL)
return;
if ((to->w_llist = ll_new_list()) == NULL)
return;
to->w_llist->qf_listcount = qi->qf_listcount;
for (idx = 0; idx < qi->qf_listcount; idx++)
{
qf_list_T *from_qfl;
qf_list_T *to_qfl;
to->w_llist->qf_curlist = idx;
from_qfl = &qi->qf_lists[idx];
to_qfl = &to->w_llist->qf_lists[idx];
to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
to_qfl->qf_count = 0;
to_qfl->qf_index = 0;
to_qfl->qf_start = NULL;
to_qfl->qf_ptr = NULL;
if (from_qfl->qf_title != NULL)
to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
else
to_qfl->qf_title = NULL;
if (from_qfl->qf_count)
{
qfline_T *from_qfp;
qfline_T *prevp = NULL;
for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
++i, from_qfp = from_qfp->qf_next)
{
if (qf_add_entry(to->w_llist, &prevp,
NULL,
NULL,
0,
from_qfp->qf_text,
from_qfp->qf_lnum,
from_qfp->qf_col,
from_qfp->qf_viscol,
from_qfp->qf_pattern,
from_qfp->qf_nr,
0,
from_qfp->qf_valid) == FAIL)
{
qf_free_all(to);
return;
}
prevp->qf_fnum = from_qfp->qf_fnum;
prevp->qf_type = from_qfp->qf_type;
if (from_qfl->qf_ptr == from_qfp)
to_qfl->qf_ptr = prevp;
}
}
to_qfl->qf_index = from_qfl->qf_index;
if (to_qfl->qf_nonevalid == TRUE)
to_qfl->qf_ptr = to_qfl->qf_start;
}
to->w_llist->qf_curlist = qi->qf_curlist;
}
static int
qf_get_fnum(directory, fname)
char_u *directory;
char_u *fname;
{
if (fname == NULL || *fname == NUL)
return 0;
{
#ifdef RISCOS
return ro_buflist_add(fname);
#else
char_u *ptr;
int fnum;
# ifdef VMS
vms_remove_version(fname);
# endif
# ifdef BACKSLASH_IN_FILENAME
if (directory != NULL)
slash_adjust(directory);
slash_adjust(fname);
# endif
if (directory != NULL && !vim_isAbsName(fname)
&& (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
{
if (mch_getperm(ptr) < 0)
{
vim_free(ptr);
directory = qf_guess_filepath(fname);
if (directory)
ptr = concat_fnames(directory, fname, TRUE);
else
ptr = vim_strsave(fname);
}
fnum = buflist_add(ptr, 0);
vim_free(ptr);
return fnum;
}
return buflist_add(fname, 0);
#endif
}
}
static char_u *
qf_push_dir(dirbuf, stackptr)
char_u *dirbuf;
struct dir_stack_T **stackptr;
{
struct dir_stack_T *ds_new;
struct dir_stack_T *ds_ptr;
ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
if (ds_new == NULL)
return NULL;
ds_new->next = *stackptr;
*stackptr = ds_new;
if (vim_isAbsName(dirbuf)
|| (*stackptr)->next == NULL
|| (*stackptr && dir_stack != *stackptr))
(*stackptr)->dirname = vim_strsave(dirbuf);
else
{
ds_new = (*stackptr)->next;
(*stackptr)->dirname = NULL;
while (ds_new)
{
vim_free((*stackptr)->dirname);
(*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
TRUE);
if (mch_isdir((*stackptr)->dirname) == TRUE)
break;
ds_new = ds_new->next;
}
while ((*stackptr)->next != ds_new)
{
ds_ptr = (*stackptr)->next;
(*stackptr)->next = (*stackptr)->next->next;
vim_free(ds_ptr->dirname);
vim_free(ds_ptr);
}
if (ds_new == NULL)
{
vim_free((*stackptr)->dirname);
(*stackptr)->dirname = vim_strsave(dirbuf);
}
}
if ((*stackptr)->dirname != NULL)
return (*stackptr)->dirname;
else
{
ds_ptr = *stackptr;
*stackptr = (*stackptr)->next;
vim_free(ds_ptr);
return NULL;
}
}
static char_u *
qf_pop_dir(stackptr)
struct dir_stack_T **stackptr;
{
struct dir_stack_T *ds_ptr;
if (*stackptr != NULL)
{
ds_ptr = *stackptr;
*stackptr = (*stackptr)->next;
vim_free(ds_ptr->dirname);
vim_free(ds_ptr);
}
return *stackptr ? (*stackptr)->dirname : NULL;
}
static void
qf_clean_dir_stack(stackptr)
struct dir_stack_T **stackptr;
{
struct dir_stack_T *ds_ptr;
while ((ds_ptr = *stackptr) != NULL)
{
*stackptr = (*stackptr)->next;
vim_free(ds_ptr->dirname);
vim_free(ds_ptr);
}
}
static char_u *
qf_guess_filepath(filename)
char_u *filename;
{
struct dir_stack_T *ds_ptr;
struct dir_stack_T *ds_tmp;
char_u *fullname;
if (dir_stack == NULL)
return NULL;
ds_ptr = dir_stack->next;
fullname = NULL;
while (ds_ptr)
{
vim_free(fullname);
fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
break;
ds_ptr = ds_ptr->next;
}
vim_free(fullname);
while (dir_stack->next != ds_ptr)
{
ds_tmp = dir_stack->next;
dir_stack->next = dir_stack->next->next;
vim_free(ds_tmp->dirname);
vim_free(ds_tmp);
}
return ds_ptr==NULL? NULL: ds_ptr->dirname;
}
void
qf_jump(qi, dir, errornr, forceit)
qf_info_T *qi;
int dir;
int errornr;
int forceit;
{
qf_info_T *ll_ref;
qfline_T *qf_ptr;
qfline_T *old_qf_ptr;
int qf_index;
int old_qf_fnum;
int old_qf_index;
int prev_index;
static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
char_u *err = e_no_more_items;
linenr_T i;
buf_T *old_curbuf;
linenr_T old_lnum;
colnr_T screen_col;
colnr_T char_col;
char_u *line;
#ifdef FEAT_WINDOWS
char_u *old_swb = p_swb;
unsigned old_swb_flags = swb_flags;
int opened_window = FALSE;
win_T *win;
win_T *altwin;
int flags;
#endif
win_T *oldwin = curwin;
int print_message = TRUE;
int len;
#ifdef FEAT_FOLDING
int old_KeyTyped = KeyTyped;
#endif
int ok = OK;
int usable_win;
if (qi == NULL)
qi = &ql_info;
if (qi->qf_curlist >= qi->qf_listcount
|| qi->qf_lists[qi->qf_curlist].qf_count == 0)
{
EMSG(_(e_quickfix));
return;
}
qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
old_qf_ptr = qf_ptr;
qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
old_qf_index = qf_index;
if (dir == FORWARD || dir == FORWARD_FILE)
{
while (errornr--)
{
old_qf_ptr = qf_ptr;
prev_index = qf_index;
old_qf_fnum = qf_ptr->qf_fnum;
do
{
if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
|| qf_ptr->qf_next == NULL)
{
qf_ptr = old_qf_ptr;
qf_index = prev_index;
if (err != NULL)
{
EMSG(_(err));
goto theend;
}
errornr = 0;
break;
}
++qf_index;
qf_ptr = qf_ptr->qf_next;
} while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
&& !qf_ptr->qf_valid)
|| (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
err = NULL;
}
}
else if (dir == BACKWARD || dir == BACKWARD_FILE)
{
while (errornr--)
{
old_qf_ptr = qf_ptr;
prev_index = qf_index;
old_qf_fnum = qf_ptr->qf_fnum;
do
{
if (qf_index == 1 || qf_ptr->qf_prev == NULL)
{
qf_ptr = old_qf_ptr;
qf_index = prev_index;
if (err != NULL)
{
EMSG(_(err));
goto theend;
}
errornr = 0;
break;
}
--qf_index;
qf_ptr = qf_ptr->qf_prev;
} while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
&& !qf_ptr->qf_valid)
|| (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
err = NULL;
}
}
else if (errornr != 0)
{
while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
{
--qf_index;
qf_ptr = qf_ptr->qf_prev;
}
while (errornr > qf_index && qf_index <
qi->qf_lists[qi->qf_curlist].qf_count
&& qf_ptr->qf_next != NULL)
{
++qf_index;
qf_ptr = qf_ptr->qf_next;
}
}
#ifdef FEAT_WINDOWS
qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
if (qf_win_pos_update(qi, old_qf_index))
print_message = FALSE;
if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
{
win_T *wp;
if (cmdmod.tab != 0)
wp = NULL;
else
for (wp = firstwin; wp != NULL; wp = wp->w_next)
if (wp->w_buffer != NULL && wp->w_buffer->b_help)
break;
if (wp != NULL && wp->w_buffer->b_nwindows > 0)
win_enter(wp, TRUE);
else
{
flags = WSP_HELP;
# ifdef FEAT_VERTSPLIT
if (cmdmod.split == 0 && curwin->w_width != Columns
&& curwin->w_width < 80)
flags |= WSP_TOP;
# endif
if (qi != &ql_info)
flags |= WSP_NEWLOC;
if (win_split(0, flags) == FAIL)
goto theend;
opened_window = TRUE;
if (curwin->w_height < p_hh)
win_setheight((int)p_hh);
if (qi != &ql_info)
{
curwin->w_llist = qi;
qi->qf_refcount++;
}
}
if (!p_im)
restart_edit = 0;
}
if (bt_quickfix(curbuf) && !opened_window)
{
if (qf_ptr->qf_fnum == 0)
goto theend;
usable_win = 0;
FOR_ALL_WINDOWS(win)
if (win->w_buffer->b_p_bt[0] == NUL)
{
usable_win = 1;
break;
}
if (!usable_win && (swb_flags & SWB_USETAB))
{
tabpage_T *tp;
win_T *wp;
FOR_ALL_TAB_WINDOWS(tp, wp)
{
if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
{
goto_tabpage_win(tp, wp);
usable_win = 1;
goto win_found;
}
}
}
win_found:
if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
{
ll_ref = curwin->w_llist_ref;
flags = WSP_ABOVE;
if (ll_ref != NULL)
flags |= WSP_NEWLOC;
if (win_split(0, flags) == FAIL)
goto failed;
opened_window = TRUE;
p_swb = empty_option;
swb_flags = 0;
# ifdef FEAT_SCROLLBIND
curwin->w_p_scb = FALSE;
# endif
if (ll_ref != NULL)
{
curwin->w_llist = ll_ref;
ll_ref->qf_refcount++;
}
}
else
{
if (curwin->w_llist_ref != NULL)
{
ll_ref = curwin->w_llist_ref;
FOR_ALL_WINDOWS(win)
if (win->w_llist == ll_ref)
break;
if (win == NULL)
{
FOR_ALL_WINDOWS(win)
if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
break;
if (win == NULL)
{
win = curwin;
do
{
if (win->w_buffer->b_p_bt[0] == NUL)
break;
if (win->w_prev == NULL)
win = lastwin;
else
win = win->w_prev;
} while (win != curwin);
}
}
win_goto(win);
if (win->w_llist == NULL)
{
win->w_llist = ll_ref;
ll_ref->qf_refcount++;
}
}
else
{
win = curwin;
altwin = NULL;
for (;;)
{
if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
break;
if (win->w_prev == NULL)
win = lastwin;
else
win = win->w_prev;
if (IS_QF_WINDOW(win))
{
if (altwin != NULL)
win = altwin;
else if (curwin->w_prev != NULL)
win = curwin->w_prev;
else
win = curwin->w_next;
break;
}
if (altwin == NULL && !win->w_p_pvw
&& win->w_buffer->b_p_bt[0] == NUL)
altwin = win;
}
win_goto(win);
}
}
}
#endif
old_curbuf = curbuf;
old_lnum = curwin->w_cursor.lnum;
if (qf_ptr->qf_fnum != 0)
{
if (qf_ptr->qf_type == 1)
{
if (!can_abandon(curbuf, forceit))
{
EMSG(_(e_nowrtmsg));
ok = FALSE;
}
else
ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
ECMD_HIDE + ECMD_SET_HELP,
oldwin == curwin ? curwin : NULL);
}
else
ok = buflist_getfile(qf_ptr->qf_fnum,
(linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
}
if (ok == OK)
{
if (curbuf == old_curbuf)
setpcmark();
if (qf_ptr->qf_pattern == NULL)
{
i = qf_ptr->qf_lnum;
if (i > 0)
{
if (i > curbuf->b_ml.ml_line_count)
i = curbuf->b_ml.ml_line_count;
curwin->w_cursor.lnum = i;
}
if (qf_ptr->qf_col > 0)
{
curwin->w_cursor.col = qf_ptr->qf_col - 1;
if (qf_ptr->qf_viscol == TRUE)
{
line = ml_get_curline();
screen_col = 0;
for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
{
if (*line == NUL)
break;
if (*line++ == '\t')
{
curwin->w_cursor.col -= 7 - (screen_col % 8);
screen_col += 8 - (screen_col % 8);
}
else
++screen_col;
}
}
check_cursor();
}
else
beginline(BL_WHITE | BL_FIX);
}
else
{
pos_T save_cursor;
save_cursor = curwin->w_cursor;
curwin->w_cursor.lnum = 0;
if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
SEARCH_KEEP, NULL))
curwin->w_cursor = save_cursor;
}
#ifdef FEAT_FOLDING
if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
foldOpenCursor();
#endif
if (print_message)
{
update_topline_redraw();
sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
qi->qf_lists[qi->qf_curlist].qf_count,
qf_ptr->qf_cleared ? _(" (line deleted)") : "",
(char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
len = (int)STRLEN(IObuff);
qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
i = msg_scroll;
if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
msg_scroll = TRUE;
else if (!msg_scrolled && shortmess(SHM_OVERALL))
msg_scroll = FALSE;
msg_attr_keep(IObuff, 0, TRUE);
msg_scroll = i;
}
}
else
{
#ifdef FEAT_WINDOWS
if (opened_window)
win_close(curwin, TRUE);
#endif
if (qf_ptr->qf_fnum != 0)
{
#ifdef FEAT_WINDOWS
failed:
#endif
qf_ptr = old_qf_ptr;
qf_index = old_qf_index;
}
}
theend:
qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
#ifdef FEAT_WINDOWS
if (p_swb != old_swb && opened_window)
{
if (p_swb == empty_option)
{
p_swb = old_swb;
swb_flags = old_swb_flags;
}
else
free_string_option(old_swb);
}
#endif
}
void
qf_list(eap)
exarg_T *eap;
{
buf_T *buf;
char_u *fname;
qfline_T *qfp;
int i;
int idx1 = 1;
int idx2 = -1;
char_u *arg = eap->arg;
int all = eap->forceit;
qf_info_T *qi = &ql_info;
if (eap->cmdidx == CMD_llist)
{
qi = GET_LOC_LIST(curwin);
if (qi == NULL)
{
EMSG(_(e_loclist));
return;
}
}
if (qi->qf_curlist >= qi->qf_listcount
|| qi->qf_lists[qi->qf_curlist].qf_count == 0)
{
EMSG(_(e_quickfix));
return;
}
if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
{
EMSG(_(e_trailing));
return;
}
i = qi->qf_lists[qi->qf_curlist].qf_count;
if (idx1 < 0)
idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
if (idx2 < 0)
idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
all = TRUE;
qfp = qi->qf_lists[qi->qf_curlist].qf_start;
for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
{
if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
{
msg_putchar('\n');
if (got_int)
break;
fname = NULL;
if (qfp->qf_fnum != 0
&& (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
{
fname = buf->b_fname;
if (qfp->qf_type == 1)
fname = gettail(fname);
}
if (fname == NULL)
sprintf((char *)IObuff, "%2d", i);
else
vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
i, (char *)fname);
msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
? hl_attr(HLF_L) : hl_attr(HLF_D));
if (qfp->qf_lnum == 0)
IObuff[0] = NUL;
else if (qfp->qf_col == 0)
sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
else
sprintf((char *)IObuff, ":%ld col %d",
qfp->qf_lnum, qfp->qf_col);
sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
(char *)qf_types(qfp->qf_type, qfp->qf_nr));
msg_puts_attr(IObuff, hl_attr(HLF_N));
if (qfp->qf_pattern != NULL)
{
qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
STRCAT(IObuff, ":");
msg_puts(IObuff);
}
msg_puts((char_u *)" ");
qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
? skipwhite(qfp->qf_text) : qfp->qf_text,
IObuff, IOSIZE);
msg_prt_line(IObuff, FALSE);
out_flush();
}
qfp = qfp->qf_next;
++i;
ui_breakcheck();
}
}
static void
qf_fmt_text(text, buf, bufsize)
char_u *text;
char_u *buf;
int bufsize;
{
int i;
char_u *p = text;
for (i = 0; *p != NUL && i < bufsize - 1; ++i)
{
if (*p == '\n')
{
buf[i] = ' ';
while (*++p != NUL)
if (!vim_iswhite(*p) && *p != '\n')
break;
}
else
buf[i] = *p++;
}
buf[i] = NUL;
}
void
qf_age(eap)
exarg_T *eap;
{
qf_info_T *qi = &ql_info;
int count;
if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
{
qi = GET_LOC_LIST(curwin);
if (qi == NULL)
{
EMSG(_(e_loclist));
return;
}
}
if (eap->addr_count != 0)
count = eap->line2;
else
count = 1;
while (count--)
{
if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
{
if (qi->qf_curlist == 0)
{
EMSG(_("E380: At bottom of quickfix stack"));
return;
}
--qi->qf_curlist;
}
else
{
if (qi->qf_curlist >= qi->qf_listcount - 1)
{
EMSG(_("E381: At top of quickfix stack"));
return;
}
++qi->qf_curlist;
}
}
qf_msg(qi);
}
static void
qf_msg(qi)
qf_info_T *qi;
{
smsg((char_u *)_("error list %d of %d; %d errors"),
qi->qf_curlist + 1, qi->qf_listcount,
qi->qf_lists[qi->qf_curlist].qf_count);
#ifdef FEAT_WINDOWS
qf_update_buffer(qi);
#endif
}
static void
qf_free(qi, idx)
qf_info_T *qi;
int idx;
{
qfline_T *qfp;
while (qi->qf_lists[idx].qf_count)
{
qfp = qi->qf_lists[idx].qf_start->qf_next;
vim_free(qi->qf_lists[idx].qf_start->qf_text);
vim_free(qi->qf_lists[idx].qf_start->qf_pattern);
vim_free(qi->qf_lists[idx].qf_start);
qi->qf_lists[idx].qf_start = qfp;
--qi->qf_lists[idx].qf_count;
}
vim_free(qi->qf_lists[idx].qf_title);
}
void
qf_mark_adjust(wp, line1, line2, amount, amount_after)
win_T *wp;
linenr_T line1;
linenr_T line2;
long amount;
long amount_after;
{
int i;
qfline_T *qfp;
int idx;
qf_info_T *qi = &ql_info;
if (wp != NULL)
{
if (wp->w_llist == NULL)
return;
qi = wp->w_llist;
}
for (idx = 0; idx < qi->qf_listcount; ++idx)
if (qi->qf_lists[idx].qf_count)
for (i = 0, qfp = qi->qf_lists[idx].qf_start;
i < qi->qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
if (qfp->qf_fnum == curbuf->b_fnum)
{
if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
{
if (amount == MAXLNUM)
qfp->qf_cleared = TRUE;
else
qfp->qf_lnum += amount;
}
else if (amount_after && qfp->qf_lnum > line2)
qfp->qf_lnum += amount_after;
}
}
static char_u *
qf_types(c, nr)
int c, nr;
{
static char_u buf[20];
static char_u cc[3];
char_u *p;
if (c == 'W' || c == 'w')
p = (char_u *)" warning";
else if (c == 'I' || c == 'i')
p = (char_u *)" info";
else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
p = (char_u *)" error";
else if (c == 0 || c == 1)
p = (char_u *)"";
else
{
cc[0] = ' ';
cc[1] = c;
cc[2] = NUL;
p = cc;
}
if (nr <= 0)
return p;
sprintf((char *)buf, "%s %3d", (char *)p, nr);
return buf;
}
#if defined(FEAT_WINDOWS) || defined(PROTO)
void
ex_cwindow(eap)
exarg_T *eap;
{
qf_info_T *qi = &ql_info;
win_T *win;
if (eap->cmdidx == CMD_lwindow)
{
qi = GET_LOC_LIST(curwin);
if (qi == NULL)
return;
}
win = qf_find_win(qi);
if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
|| qi->qf_curlist >= qi->qf_listcount)
{
if (win != NULL)
ex_cclose(eap);
}
else if (win == NULL)
ex_copen(eap);
}
void
ex_cclose(eap)
exarg_T *eap;
{
win_T *win = NULL;
qf_info_T *qi = &ql_info;
if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
{
qi = GET_LOC_LIST(curwin);
if (qi == NULL)
return;
}
win = qf_find_win(qi);
if (win != NULL)
win_close(win, FALSE);
}
void
ex_copen(eap)
exarg_T *eap;
{
qf_info_T *qi = &ql_info;
int height;
win_T *win;
tabpage_T *prevtab = curtab;
buf_T *qf_buf;
win_T *oldwin = curwin;
if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
{
qi = GET_LOC_LIST(curwin);
if (qi == NULL)
{
EMSG(_(e_loclist));
return;
}
}
if (eap->addr_count != 0)
height = eap->line2;
else
height = QF_WINHEIGHT;
#ifdef FEAT_VISUAL
reset_VIsual_and_resel();
#endif
#ifdef FEAT_GUI
need_mouse_correct = TRUE;
#endif
win = qf_find_win(qi);
if (win != NULL && cmdmod.tab == 0)
win_goto(win);
else
{
qf_buf = qf_find_buf(qi);
win = curwin;
if (eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
win_goto(lastwin);
if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
return;
#ifdef FEAT_SCROLLBIND
curwin->w_p_scb = FALSE;
#endif
if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
{
curwin->w_llist_ref = win->w_llist;
win->w_llist->qf_refcount++;
}
if (oldwin != curwin)
oldwin = NULL;
if (qf_buf != NULL)
(void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
ECMD_HIDE + ECMD_OLDBUF, oldwin);
else
{
(void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
OPT_LOCAL);
set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
#ifdef FEAT_DIFF
curwin->w_p_diff = FALSE;
#endif
#ifdef FEAT_FOLDING
set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
OPT_LOCAL);
#endif
}
if (curtab == prevtab
#ifdef FEAT_VERTSPLIT
&& curwin->w_width == Columns
#endif
)
win_setheight(height);
curwin->w_p_wfh = TRUE;
if (win_valid(win))
prevwin = win;
}
qf_fill_buffer(qi);
if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
set_internal_string_var((char_u *)"w:quickfix_title",
qi->qf_lists[qi->qf_curlist].qf_title);
curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
curwin->w_cursor.col = 0;
check_cursor();
update_topline();
}
linenr_T
qf_current_entry(wp)
win_T *wp;
{
qf_info_T *qi = &ql_info;
if (IS_LL_WINDOW(wp))
qi = wp->w_llist_ref;
return qi->qf_lists[qi->qf_curlist].qf_index;
}
static int
qf_win_pos_update(qi, old_qf_index)
qf_info_T *qi;
int old_qf_index;
{
win_T *win;
int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
win = qf_find_win(qi);
if (win != NULL
&& qf_index <= win->w_buffer->b_ml.ml_line_count
&& old_qf_index != qf_index)
{
win_T *old_curwin = curwin;
curwin = win;
curbuf = win->w_buffer;
if (qf_index > old_qf_index)
{
curwin->w_redraw_top = old_qf_index;
curwin->w_redraw_bot = qf_index;
}
else
{
curwin->w_redraw_top = qf_index;
curwin->w_redraw_bot = old_qf_index;
}
curwin->w_cursor.lnum = qf_index;
curwin->w_cursor.col = 0;
update_topline();
redraw_later(VALID);
curwin->w_redr_status = TRUE;
curwin = old_curwin;
curbuf = curwin->w_buffer;
}
return win != NULL;
}
static int
is_qf_win(win, qi)
win_T *win;
qf_info_T *qi;
{
if (bt_quickfix(win->w_buffer))
if ((qi == &ql_info && win->w_llist_ref == NULL)
|| (qi != &ql_info && win->w_llist_ref == qi))
return TRUE;
return FALSE;
}
static win_T *
qf_find_win(qi)
qf_info_T *qi;
{
win_T *win;
FOR_ALL_WINDOWS(win)
if (is_qf_win(win, qi))
break;
return win;
}
static buf_T *
qf_find_buf(qi)
qf_info_T *qi;
{
tabpage_T *tp;
win_T *win;
FOR_ALL_TAB_WINDOWS(tp, win)
if (is_qf_win(win, qi))
return win->w_buffer;
return NULL;
}
static void
qf_update_buffer(qi)
qf_info_T *qi;
{
buf_T *buf;
aco_save_T aco;
buf = qf_find_buf(qi);
if (buf != NULL)
{
aucmd_prepbuf(&aco, buf);
qf_fill_buffer(qi);
aucmd_restbuf(&aco);
(void)qf_win_pos_update(qi, 0);
}
}
static void
qf_fill_buffer(qi)
qf_info_T *qi;
{
linenr_T lnum;
qfline_T *qfp;
buf_T *errbuf;
int len;
int old_KeyTyped = KeyTyped;
while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
(void)ml_delete((linenr_T)1, FALSE);
if (qi->qf_curlist < qi->qf_listcount)
{
qfp = qi->qf_lists[qi->qf_curlist].qf_start;
for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
{
if (qfp->qf_fnum != 0
&& (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
&& errbuf->b_fname != NULL)
{
if (qfp->qf_type == 1)
STRCPY(IObuff, gettail(errbuf->b_fname));
else
STRCPY(IObuff, errbuf->b_fname);
len = (int)STRLEN(IObuff);
}
else
len = 0;
IObuff[len++] = '|';
if (qfp->qf_lnum > 0)
{
sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
len += (int)STRLEN(IObuff + len);
if (qfp->qf_col > 0)
{
sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
len += (int)STRLEN(IObuff + len);
}
sprintf((char *)IObuff + len, "%s",
(char *)qf_types(qfp->qf_type, qfp->qf_nr));
len += (int)STRLEN(IObuff + len);
}
else if (qfp->qf_pattern != NULL)
{
qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
len += (int)STRLEN(IObuff + len);
}
IObuff[len++] = '|';
IObuff[len++] = ' ';
qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
IObuff + len, IOSIZE - len);
if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
== FAIL)
break;
qfp = qfp->qf_next;
}
(void)ml_delete(lnum + 1, FALSE);
}
check_lnums(TRUE);
set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
curbuf->b_p_ma = FALSE;
#ifdef FEAT_AUTOCMD
keep_filetype = TRUE;
apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
FALSE, curbuf);
apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
FALSE, curbuf);
keep_filetype = FALSE;
#endif
redraw_curbuf_later(NOT_VALID);
KeyTyped = old_KeyTyped;
}
#endif
int
bt_quickfix(buf)
buf_T *buf;
{
return (buf->b_p_bt[0] == 'q');
}
int
bt_nofile(buf)
buf_T *buf;
{
return (buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
|| buf->b_p_bt[0] == 'a';
}
int
bt_dontwrite(buf)
buf_T *buf;
{
return (buf->b_p_bt[0] == 'n');
}
int
bt_dontwrite_msg(buf)
buf_T *buf;
{
if (bt_dontwrite(buf))
{
EMSG(_("E382: Cannot write, 'buftype' option is set"));
return TRUE;
}
return FALSE;
}
int
buf_hide(buf)
buf_T *buf;
{
switch (buf->b_p_bh[0])
{
case 'u':
case 'w':
case 'd': return FALSE;
case 'h': return TRUE;
}
return (p_hid || cmdmod.hide);
}
int
grep_internal(cmdidx)
cmdidx_T cmdidx;
{
return ((cmdidx == CMD_grep
|| cmdidx == CMD_lgrep
|| cmdidx == CMD_grepadd
|| cmdidx == CMD_lgrepadd)
&& STRCMP("internal",
*curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
}
void
ex_make(eap)
exarg_T *eap;
{
char_u *fname;
char_u *cmd;
unsigned len;
win_T *wp = NULL;
qf_info_T *qi = &ql_info;
int res;
#ifdef FEAT_AUTOCMD
char_u *au_name = NULL;
switch (eap->cmdidx)
{
case CMD_make: au_name = (char_u *)"make"; break;
case CMD_lmake: au_name = (char_u *)"lmake"; break;
case CMD_grep: au_name = (char_u *)"grep"; break;
case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
default: break;
}
if (au_name != NULL)
{
apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
curbuf->b_fname, TRUE, curbuf);
# ifdef FEAT_EVAL
if (did_throw || force_abort)
return;
# endif
}
#endif
if (grep_internal(eap->cmdidx))
{
ex_vimgrep(eap);
return;
}
if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
|| eap->cmdidx == CMD_lgrepadd)
wp = curwin;
autowrite_all();
fname = get_mef_name();
if (fname == NULL)
return;
mch_remove(fname);
len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
if (*p_sp != NUL)
len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
cmd = alloc(len);
if (cmd == NULL)
return;
sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
(char *)p_shq);
if (*p_sp != NUL)
append_redir(cmd, len, p_sp, fname);
if (msg_col == 0)
msg_didout = FALSE;
msg_start();
MSG_PUTS(":!");
msg_outtrans(cmd);
do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
#ifdef AMIGA
out_flush();
(void)char_avail();
#endif
res = qf_init(wp, fname, (eap->cmdidx != CMD_make
&& eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
(eap->cmdidx != CMD_grepadd
&& eap->cmdidx != CMD_lgrepadd),
*eap->cmdlinep);
#ifdef FEAT_AUTOCMD
if (au_name != NULL)
apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
curbuf->b_fname, TRUE, curbuf);
#endif
if (res > 0 && !eap->forceit)
{
if (wp != NULL)
qi = GET_LOC_LIST(wp);
qf_jump(qi, 0, 0, FALSE);
}
mch_remove(fname);
vim_free(fname);
vim_free(cmd);
}
static char_u *
get_mef_name()
{
char_u *p;
char_u *name;
static int start = -1;
static int off = 0;
#ifdef HAVE_LSTAT
struct stat sb;
#endif
if (*p_mef == NUL)
{
name = vim_tempname('e');
if (name == NULL)
EMSG(_(e_notmp));
return name;
}
for (p = p_mef; *p; ++p)
if (p[0] == '#' && p[1] == '#')
break;
if (*p == NUL)
return vim_strsave(p_mef);
for (;;)
{
if (start == -1)
start = mch_get_pid();
else
off += 19;
name = alloc((unsigned)STRLEN(p_mef) + 30);
if (name == NULL)
break;
STRCPY(name, p_mef);
sprintf((char *)name + (p - p_mef), "%d%d", start, off);
STRCAT(name, p + 2);
if (mch_getperm(name) < 0
#ifdef HAVE_LSTAT
&& mch_lstat((char *)name, &sb) < 0
#endif
)
break;
vim_free(name);
}
return name;
}
void
ex_cc(eap)
exarg_T *eap;
{
qf_info_T *qi = &ql_info;
if (eap->cmdidx == CMD_ll
|| eap->cmdidx == CMD_lrewind
|| eap->cmdidx == CMD_lfirst
|| eap->cmdidx == CMD_llast)
{
qi = GET_LOC_LIST(curwin);
if (qi == NULL)
{
EMSG(_(e_loclist));
return;
}
}
qf_jump(qi, 0,
eap->addr_count > 0
? (int)eap->line2
: (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
? 0
: (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
|| eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
? 1
: 32767,
eap->forceit);
}
void
ex_cnext(eap)
exarg_T *eap;
{
qf_info_T *qi = &ql_info;
if (eap->cmdidx == CMD_lnext
|| eap->cmdidx == CMD_lNext
|| eap->cmdidx == CMD_lprevious
|| eap->cmdidx == CMD_lnfile
|| eap->cmdidx == CMD_lNfile
|| eap->cmdidx == CMD_lpfile)
{
qi = GET_LOC_LIST(curwin);
if (qi == NULL)
{
EMSG(_(e_loclist));
return;
}
}
qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext)
? FORWARD
: (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile)
? FORWARD_FILE
: (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
|| eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
? BACKWARD_FILE
: BACKWARD,
eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
}
void
ex_cfile(eap)
exarg_T *eap;
{
win_T *wp = NULL;
qf_info_T *qi = &ql_info;
if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
|| eap->cmdidx == CMD_laddfile)
wp = curwin;
#ifdef FEAT_BROWSE
if (cmdmod.browse)
{
char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
if (browse_file == NULL)
return;
set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
vim_free(browse_file);
}
else
#endif
if (*eap->arg != NUL)
set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
&& eap->cmdidx != CMD_laddfile),
*eap->cmdlinep) > 0
&& (eap->cmdidx == CMD_cfile
|| eap->cmdidx == CMD_lfile))
{
if (wp != NULL)
qi = GET_LOC_LIST(wp);
qf_jump(qi, 0, 0, eap->forceit);
}
}
void
ex_vimgrep(eap)
exarg_T *eap;
{
regmmatch_T regmatch;
int fcount;
char_u **fnames;
char_u *fname;
char_u *s;
char_u *p;
int fi;
qf_info_T *qi = &ql_info;
qfline_T *prevp = NULL;
long lnum;
buf_T *buf;
int duplicate_name = FALSE;
int using_dummy;
int redraw_for_dummy = FALSE;
int found_match;
buf_T *first_match_buf = NULL;
time_t seconds = 0;
int save_mls;
#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
char_u *save_ei = NULL;
#endif
aco_save_T aco;
int flags = 0;
colnr_T col;
long tomatch;
char_u dirname_start[MAXPATHL];
char_u dirname_now[MAXPATHL];
char_u *target_dir = NULL;
#ifdef FEAT_AUTOCMD
char_u *au_name = NULL;
switch (eap->cmdidx)
{
case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
default: break;
}
if (au_name != NULL)
{
apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
curbuf->b_fname, TRUE, curbuf);
if (did_throw || force_abort)
return;
}
#endif
if (eap->cmdidx == CMD_lgrep
|| eap->cmdidx == CMD_lvimgrep
|| eap->cmdidx == CMD_lgrepadd
|| eap->cmdidx == CMD_lvimgrepadd)
{
qi = ll_get_or_alloc_list(curwin);
if (qi == NULL)
return;
}
if (eap->addr_count > 0)
tomatch = eap->line2;
else
tomatch = MAXLNUM;
regmatch.regprog = NULL;
p = skip_vimgrep_pat(eap->arg, &s, &flags);
if (p == NULL)
{
EMSG(_(e_invalpat));
goto theend;
}
regmatch.regprog = vim_regcomp(s, RE_MAGIC);
if (regmatch.regprog == NULL)
goto theend;
regmatch.rmm_ic = p_ic;
regmatch.rmm_maxcol = 0;
p = skipwhite(p);
if (*p == NUL)
{
EMSG(_("E683: File name missing or invalid pattern"));
goto theend;
}
if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
|| qi->qf_curlist == qi->qf_listcount)
qf_new_list(qi, *eap->cmdlinep);
else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
prevp->qf_next != prevp; prevp = prevp->qf_next)
;
if (get_arglist_exp(p, &fcount, &fnames) == FAIL)
goto theend;
if (fcount == 0)
{
EMSG(_(e_nomatch));
goto theend;
}
mch_dirname(dirname_start, MAXPATHL);
seconds = (time_t)0;
for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
{
fname = shorten_fname1(fnames[fi]);
if (time(NULL) > seconds)
{
seconds = time(NULL);
msg_start();
p = msg_strtrunc(fname, TRUE);
if (p == NULL)
msg_outtrans(fname);
else
{
msg_outtrans(p);
vim_free(p);
}
msg_clr_eos();
msg_didout = FALSE;
msg_nowait = TRUE;
msg_col = 0;
out_flush();
}
buf = buflist_findname_exp(fnames[fi]);
if (buf == NULL || buf->b_ml.ml_mfp == NULL)
{
duplicate_name = (buf != NULL);
using_dummy = TRUE;
redraw_for_dummy = TRUE;
#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
save_ei = au_event_disable(",Filetype");
#endif
save_mls = p_mls;
p_mls = 0;
buf = load_dummy_buffer(fname);
mch_dirname(dirname_now, MAXPATHL);
if (STRCMP(dirname_start, dirname_now) != 0)
{
exarg_T ea;
ea.arg = dirname_start;
ea.cmdidx = CMD_lcd;
ex_cd(&ea);
}
p_mls = save_mls;
#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
au_event_restore(save_ei);
#endif
}
else
using_dummy = FALSE;
if (buf == NULL)
{
if (!got_int)
smsg((char_u *)_("Cannot open file \"%s\""), fname);
}
else
{
found_match = FALSE;
for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
++lnum)
{
col = 0;
while (vim_regexec_multi(®match, curwin, buf, lnum,
col, NULL) > 0)
{
;
if (qf_add_entry(qi, &prevp,
NULL,
fname,
0,
ml_get_buf(buf,
regmatch.startpos[0].lnum + lnum, FALSE),
regmatch.startpos[0].lnum + lnum,
regmatch.startpos[0].col + 1,
FALSE,
NULL,
0,
0,
TRUE
) == FAIL)
{
got_int = TRUE;
break;
}
found_match = TRUE;
if (--tomatch == 0)
break;
if ((flags & VGR_GLOBAL) == 0
|| regmatch.endpos[0].lnum > 0)
break;
col = regmatch.endpos[0].col
+ (col == regmatch.endpos[0].col);
if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
break;
}
line_breakcheck();
if (got_int)
break;
}
if (using_dummy)
{
if (found_match && first_match_buf == NULL)
first_match_buf = buf;
if (duplicate_name)
{
wipe_dummy_buffer(buf);
buf = NULL;
}
else if (!cmdmod.hide
|| buf->b_p_bh[0] == 'u'
|| buf->b_p_bh[0] == 'w'
|| buf->b_p_bh[0] == 'd')
{
if (!found_match)
{
wipe_dummy_buffer(buf);
buf = NULL;
}
else if (buf != first_match_buf || (flags & VGR_NOJUMP))
{
unload_dummy_buffer(buf);
buf = NULL;
}
}
if (buf != NULL)
{
if (buf == first_match_buf
&& target_dir == NULL
&& STRCMP(dirname_start, dirname_now) != 0)
target_dir = vim_strsave(dirname_now);
aucmd_prepbuf(&aco, buf);
#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
buf->b_fname, TRUE, buf);
#endif
do_modelines(OPT_NOWIN);
aucmd_restbuf(&aco);
}
}
}
}
FreeWild(fcount, fnames);
qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
qi->qf_lists[qi->qf_curlist].qf_index = 1;
#ifdef FEAT_WINDOWS
qf_update_buffer(qi);
#endif
#ifdef FEAT_AUTOCMD
if (au_name != NULL)
apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
curbuf->b_fname, TRUE, curbuf);
#endif
if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
{
if ((flags & VGR_NOJUMP) == 0)
{
buf = curbuf;
qf_jump(qi, 0, 0, eap->forceit);
if (buf != curbuf)
redraw_for_dummy = FALSE;
if (curbuf == first_match_buf && target_dir != NULL)
{
exarg_T ea;
ea.arg = target_dir;
ea.cmdidx = CMD_lcd;
ex_cd(&ea);
}
}
}
else
EMSG2(_(e_nomatch2), s);
if (redraw_for_dummy)
{
#ifdef FEAT_FOLDING
foldUpdateAll(curwin);
#else
redraw_later(NOT_VALID);
#endif
}
theend:
vim_free(target_dir);
vim_free(regmatch.regprog);
}
char_u *
skip_vimgrep_pat(p, s, flags)
char_u *p;
char_u **s;
int *flags;
{
int c;
if (vim_isIDc(*p))
{
if (s != NULL)
*s = p;
p = skiptowhite(p);
if (s != NULL && *p != NUL)
*p++ = NUL;
}
else
{
if (s != NULL)
*s = p + 1;
c = *p;
p = skip_regexp(p + 1, c, TRUE, NULL);
if (*p != c)
return NULL;
if (s != NULL)
*p = NUL;
++p;
while (*p == 'g' || *p == 'j')
{
if (flags != NULL)
{
if (*p == 'g')
*flags |= VGR_GLOBAL;
else
*flags |= VGR_NOJUMP;
}
++p;
}
}
return p;
}
static buf_T *
load_dummy_buffer(fname)
char_u *fname;
{
buf_T *newbuf;
int failed = TRUE;
aco_save_T aco;
newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
if (newbuf == NULL)
return NULL;
buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
if (ml_open(newbuf) == OK)
{
aucmd_prepbuf(&aco, newbuf);
(void)setfname(curbuf, fname, NULL, FALSE);
check_need_swap(TRUE);
curbuf->b_flags &= ~BF_DUMMY;
if (readfile(fname, NULL,
(linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
NULL, READ_NEW | READ_DUMMY) == OK
&& !got_int
&& !(curbuf->b_flags & BF_NEW))
{
failed = FALSE;
if (curbuf != newbuf)
{
if (buf_valid(newbuf))
wipe_buffer(newbuf, FALSE);
newbuf = curbuf;
}
}
aucmd_restbuf(&aco);
}
if (!buf_valid(newbuf))
return NULL;
if (failed)
{
wipe_dummy_buffer(newbuf);
return NULL;
}
return newbuf;
}
static void
wipe_dummy_buffer(buf)
buf_T *buf;
{
if (curbuf != buf)
{
#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
cleanup_T cs;
enter_cleanup(&cs);
#endif
wipe_buffer(buf, FALSE);
#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
leave_cleanup(&cs);
#endif
}
}
static void
unload_dummy_buffer(buf)
buf_T *buf;
{
if (curbuf != buf)
close_buffer(NULL, buf, DOBUF_UNLOAD);
}
#if defined(FEAT_EVAL) || defined(PROTO)
int
get_errorlist(wp, list)
win_T *wp;
list_T *list;
{
qf_info_T *qi = &ql_info;
dict_T *dict;
char_u buf[2];
qfline_T *qfp;
int i;
int bufnum;
if (wp != NULL)
{
qi = GET_LOC_LIST(wp);
if (qi == NULL)
return FAIL;
}
if (qi->qf_curlist >= qi->qf_listcount
|| qi->qf_lists[qi->qf_curlist].qf_count == 0)
return FAIL;
qfp = qi->qf_lists[qi->qf_curlist].qf_start;
for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
{
bufnum = qfp->qf_fnum;
if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
bufnum = 0;
if ((dict = dict_alloc()) == NULL)
return FAIL;
if (list_append_dict(list, dict) == FAIL)
return FAIL;
buf[0] = qfp->qf_type;
buf[1] = NUL;
if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
|| dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
|| dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
|| dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
|| dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
|| dict_add_nr_str(dict, "pattern", 0L,
qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
|| dict_add_nr_str(dict, "text", 0L,
qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
|| dict_add_nr_str(dict, "type", 0L, buf) == FAIL
|| dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
return FAIL;
qfp = qfp->qf_next;
}
return OK;
}
int
set_errorlist(wp, list, action, title)
win_T *wp;
list_T *list;
int action;
char_u *title;
{
listitem_T *li;
dict_T *d;
char_u *filename, *pattern, *text, *type;
int bufnum;
long lnum;
int col, nr;
int vcol;
qfline_T *prevp = NULL;
int valid, status;
int retval = OK;
qf_info_T *qi = &ql_info;
int did_bufnr_emsg = FALSE;
if (wp != NULL)
{
qi = ll_get_or_alloc_list(wp);
if (qi == NULL)
return FAIL;
}
if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
qf_new_list(qi, title);
else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
prevp->qf_next != prevp; prevp = prevp->qf_next)
;
else if (action == 'r')
qf_free(qi, qi->qf_curlist);
for (li = list->lv_first; li != NULL; li = li->li_next)
{
if (li->li_tv.v_type != VAR_DICT)
continue;
d = li->li_tv.vval.v_dict;
if (d == NULL)
continue;
filename = get_dict_string(d, (char_u *)"filename", TRUE);
bufnum = get_dict_number(d, (char_u *)"bufnr");
lnum = get_dict_number(d, (char_u *)"lnum");
col = get_dict_number(d, (char_u *)"col");
vcol = get_dict_number(d, (char_u *)"vcol");
nr = get_dict_number(d, (char_u *)"nr");
type = get_dict_string(d, (char_u *)"type", TRUE);
pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
text = get_dict_string(d, (char_u *)"text", TRUE);
if (text == NULL)
text = vim_strsave((char_u *)"");
valid = TRUE;
if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
valid = FALSE;
if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
{
if (!did_bufnr_emsg)
{
did_bufnr_emsg = TRUE;
EMSGN(_("E92: Buffer %ld not found"), bufnum);
}
valid = FALSE;
bufnum = 0;
}
status = qf_add_entry(qi, &prevp,
NULL,
filename,
bufnum,
text,
lnum,
col,
vcol,
pattern,
nr,
type == NULL ? NUL : *type,
valid);
vim_free(filename);
vim_free(pattern);
vim_free(text);
vim_free(type);
if (status == FAIL)
{
retval = FAIL;
break;
}
}
if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
else
qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
qi->qf_lists[qi->qf_curlist].qf_index = 1;
#ifdef FEAT_WINDOWS
qf_update_buffer(qi);
#endif
return retval;
}
#endif
void
ex_cbuffer(eap)
exarg_T *eap;
{
buf_T *buf = NULL;
qf_info_T *qi = &ql_info;
if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
|| eap->cmdidx == CMD_laddbuffer)
{
qi = ll_get_or_alloc_list(curwin);
if (qi == NULL)
return;
}
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));
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 = *eap->cmdlinep;
if (buf->b_sfname)
{
vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
(char *)qf_title, (char *)buf->b_sfname);
qf_title = IObuff;
}
if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
(eap->cmdidx != CMD_caddbuffer
&& eap->cmdidx != CMD_laddbuffer),
eap->line1, eap->line2,
qf_title) > 0
&& (eap->cmdidx == CMD_cbuffer
|| eap->cmdidx == CMD_lbuffer))
qf_jump(qi, 0, 0, eap->forceit);
}
}
}
#if defined(FEAT_EVAL) || defined(PROTO)
void
ex_cexpr(eap)
exarg_T *eap;
{
typval_T *tv;
qf_info_T *qi = &ql_info;
if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
|| eap->cmdidx == CMD_laddexpr)
{
qi = ll_get_or_alloc_list(curwin);
if (qi == NULL)
return;
}
tv = eval_expr(eap->arg, NULL);
if (tv != NULL)
{
if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
|| (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
{
if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
(eap->cmdidx != CMD_caddexpr
&& eap->cmdidx != CMD_laddexpr),
(linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0
&& (eap->cmdidx == CMD_cexpr
|| eap->cmdidx == CMD_lexpr))
qf_jump(qi, 0, 0, eap->forceit);
}
else
EMSG(_("E777: String or List expected"));
free_tv(tv);
}
}
#endif
void
ex_helpgrep(eap)
exarg_T *eap;
{
regmatch_T regmatch;
char_u *save_cpo;
char_u *p;
int fcount;
char_u **fnames;
FILE *fd;
int fi;
qfline_T *prevp = NULL;
long lnum;
#ifdef FEAT_MULTI_LANG
char_u *lang;
#endif
qf_info_T *qi = &ql_info;
int new_qi = FALSE;
win_T *wp;
save_cpo = p_cpo;
p_cpo = empty_option;
#ifdef FEAT_MULTI_LANG
lang = check_help_lang(eap->arg);
#endif
if (eap->cmdidx == CMD_lhelpgrep)
{
FOR_ALL_WINDOWS(wp)
if (wp->w_buffer != NULL && wp->w_buffer->b_help)
break;
if (wp == NULL)
qi = NULL;
else
qi = wp->w_llist;
if (qi == NULL)
{
if ((qi = ll_new_list()) == NULL)
return;
new_qi = TRUE;
}
}
regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
regmatch.rm_ic = FALSE;
if (regmatch.regprog != NULL)
{
qf_new_list(qi, *eap->cmdlinep);
p = p_rtp;
while (*p != NUL && !got_int)
{
copy_option_part(&p, NameBuff, MAXPATHL, ",");
add_pathsep(NameBuff);
STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
if (gen_expand_wildcards(1, &NameBuff, &fcount,
&fnames, EW_FILE|EW_SILENT) == OK
&& fcount > 0)
{
for (fi = 0; fi < fcount && !got_int; ++fi)
{
#ifdef FEAT_MULTI_LANG
if (lang != NULL
&& STRNICMP(lang, fnames[fi]
+ STRLEN(fnames[fi]) - 3, 2) != 0
&& !(STRNICMP(lang, "en", 2) == 0
&& STRNICMP("txt", fnames[fi]
+ STRLEN(fnames[fi]) - 3, 3) == 0))
continue;
#endif
fd = mch_fopen((char *)fnames[fi], "r");
if (fd != NULL)
{
lnum = 1;
while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
{
if (vim_regexec(®match, IObuff, (colnr_T)0))
{
int l = (int)STRLEN(IObuff);
while (l > 0 && IObuff[l - 1] <= ' ')
IObuff[--l] = NUL;
if (qf_add_entry(qi, &prevp,
NULL,
fnames[fi],
0,
IObuff,
lnum,
(int)(regmatch.startp[0] - IObuff)
+ 1,
FALSE,
NULL,
0,
1,
TRUE
) == FAIL)
{
got_int = TRUE;
break;
}
}
++lnum;
line_breakcheck();
}
fclose(fd);
}
}
FreeWild(fcount, fnames);
}
}
vim_free(regmatch.regprog);
qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
qi->qf_lists[qi->qf_curlist].qf_ptr =
qi->qf_lists[qi->qf_curlist].qf_start;
qi->qf_lists[qi->qf_curlist].qf_index = 1;
}
if (p_cpo == empty_option)
p_cpo = save_cpo;
else
free_string_option(save_cpo);
#ifdef FEAT_WINDOWS
qf_update_buffer(qi);
#endif
if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
qf_jump(qi, 0, 0, FALSE);
else
EMSG2(_(e_nomatch2), eap->arg);
if (eap->cmdidx == CMD_lhelpgrep)
{
if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
{
if (new_qi)
ll_free_all(&qi);
}
else if (curwin->w_llist == NULL)
curwin->w_llist = qi;
}
}
#endif