blob: 999c66ed9b095fd92f0291f5e4dc1e3bb09aa567 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * quickfix.c: functions for quickfix mode, using a file with error messages
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_QUICKFIX) || defined(PROTO)
17
18struct dir_stack_T
19{
20 struct dir_stack_T *next;
21 char_u *dirname;
22};
23
24static struct dir_stack_T *dir_stack = NULL;
25
26/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000028 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000029typedef struct qfline_S qfline_T;
30struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000031{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000032 qfline_T *qf_next; /* pointer to next error in the list */
33 qfline_T *qf_prev; /* pointer to previous error in the list */
34 linenr_T qf_lnum; /* line number where the error occurred */
35 int qf_fnum; /* file number for the line */
36 int qf_col; /* column where the error occurred */
37 int qf_nr; /* error number */
38 char_u *qf_pattern; /* search pattern for the error */
39 char_u *qf_text; /* description of the error */
40 char_u qf_viscol; /* set to TRUE if qf_col is screen column */
41 char_u qf_cleared; /* set to TRUE if line has been deleted */
42 char_u qf_type; /* type of the error (mostly 'E'); 1 for
Bram Moolenaar071d4272004-06-13 20:20:40 +000043 :helpgrep */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000044 char_u qf_valid; /* valid error message detected */
Bram Moolenaar071d4272004-06-13 20:20:40 +000045};
46
47/*
48 * There is a stack of error lists.
49 */
50#define LISTCOUNT 10
51
Bram Moolenaard12f5c12006-01-25 22:10:52 +000052typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000053{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000054 qfline_T *qf_start; /* pointer to the first error */
55 qfline_T *qf_ptr; /* pointer to the current error */
56 int qf_count; /* number of errors (0 means no error list) */
57 int qf_index; /* current index in the error list */
58 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020059 char_u *qf_title; /* title derived from the command that created
60 * the error list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000061} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000062
Bram Moolenaard12f5c12006-01-25 22:10:52 +000063struct qf_info_S
64{
65 /*
66 * Count of references to this list. Used only for location lists.
67 * When a location list window reference this list, qf_refcount
68 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
69 * reaches 0, the list is freed.
70 */
71 int qf_refcount;
72 int qf_listcount; /* current number of lists */
73 int qf_curlist; /* current error list */
74 qf_list_T qf_lists[LISTCOUNT];
75};
76
77static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaar071d4272004-06-13 20:20:40 +000078
Bram Moolenaar68b76a62005-03-25 21:53:48 +000079#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
81/*
82 * Structure used to hold the info of one part of 'errorformat'
83 */
Bram Moolenaar01265852006-03-20 21:50:15 +000084typedef struct efm_S efm_T;
85struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000086{
87 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +000088 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000089 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
90 char_u prefix; /* prefix of this format line: */
91 /* 'D' enter directory */
92 /* 'X' leave directory */
93 /* 'A' start of multi-line message */
94 /* 'E' error message */
95 /* 'W' warning message */
96 /* 'I' informational message */
97 /* 'C' continuation line */
98 /* 'Z' end of multi-line message */
99 /* 'G' general, unspecific message */
100 /* 'P' push file (partial) message */
101 /* 'Q' pop/quit file (partial) message */
102 /* 'O' overread (partial) message */
103 char_u flags; /* additional flags given in prefix */
104 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000105 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000106 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107};
108
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200109static 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));
110static void qf_new_list __ARGS((qf_info_T *qi, char_u *qf_title));
Bram Moolenaard9ff7d52008-03-20 12:23:49 +0000111static void ll_free_all __ARGS((qf_info_T **pqi));
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000112static 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));
Bram Moolenaard9ff7d52008-03-20 12:23:49 +0000113static qf_info_T *ll_new_list __ARGS((void));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000114static void qf_msg __ARGS((qf_info_T *qi));
115static void qf_free __ARGS((qf_info_T *qi, int idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116static char_u *qf_types __ARGS((int, int));
117static int qf_get_fnum __ARGS((char_u *, char_u *));
118static char_u *qf_push_dir __ARGS((char_u *, struct dir_stack_T **));
119static char_u *qf_pop_dir __ARGS((struct dir_stack_T **));
120static char_u *qf_guess_filepath __ARGS((char_u *));
121static void qf_fmt_text __ARGS((char_u *text, char_u *buf, int bufsize));
122static void qf_clean_dir_stack __ARGS((struct dir_stack_T **));
123#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000124static int qf_win_pos_update __ARGS((qf_info_T *qi, int old_qf_index));
Bram Moolenaar9c102382006-05-03 21:26:49 +0000125static int is_qf_win __ARGS((win_T *win, qf_info_T *qi));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000126static win_T *qf_find_win __ARGS((qf_info_T *qi));
127static buf_T *qf_find_buf __ARGS((qf_info_T *qi));
128static void qf_update_buffer __ARGS((qf_info_T *qi));
129static void qf_fill_buffer __ARGS((qf_info_T *qi));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#endif
131static char_u *get_mef_name __ARGS((void));
Bram Moolenaar81695252004-12-29 20:58:21 +0000132static buf_T *load_dummy_buffer __ARGS((char_u *fname));
133static void wipe_dummy_buffer __ARGS((buf_T *buf));
134static void unload_dummy_buffer __ARGS((buf_T *buf));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000135static qf_info_T *ll_get_or_alloc_list __ARGS((win_T *));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000137/* Quickfix window check helper macro */
138#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
139/* Location list window check helper macro */
140#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
141/*
142 * Return location list for window 'wp'
143 * For location list window, return the referenced location list
144 */
145#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
146
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000148 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200149 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 * Return -1 for error, number of errors for success.
151 */
152 int
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200153qf_init(wp, efile, errorformat, newlist, qf_title)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000154 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 char_u *efile;
156 char_u *errorformat;
157 int newlist; /* TRUE: start a new error list */
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200158 char_u *qf_title;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000160 qf_info_T *qi = &ql_info;
161
Bram Moolenaar86b68352004-12-27 21:59:20 +0000162 if (efile == NULL)
163 return FAIL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000164
165 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000166 {
167 qi = ll_get_or_alloc_list(wp);
168 if (qi == NULL)
169 return FAIL;
170 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000171
172 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200173 (linenr_T)0, (linenr_T)0,
174 qf_title);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000175}
176
177/*
178 * Read the errorfile "efile" into memory, line by line, building the error
179 * list.
180 * Alternative: when "efile" is null read errors from buffer "buf".
181 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200182 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
183 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +0000184 * Return -1 for error, number of errors for success.
185 */
186 static int
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200187qf_init_ext(qi, efile, buf, tv, errorformat, newlist, lnumfirst, lnumlast,
188 qf_title)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000189 qf_info_T *qi;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000190 char_u *efile;
191 buf_T *buf;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000192 typval_T *tv;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000193 char_u *errorformat;
194 int newlist; /* TRUE: start a new error list */
195 linenr_T lnumfirst; /* first line number to use */
196 linenr_T lnumlast; /* last line number to use */
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200197 char_u *qf_title;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000198{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 char_u *namebuf;
200 char_u *errmsg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000201 char_u *pattern;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 char_u *fmtstr = NULL;
203 int col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000204 char_u use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 int type = 0;
206 int valid;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000207 linenr_T buflnum = lnumfirst;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 long lnum = 0L;
209 int enr = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000210 FILE *fd = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000211 qfline_T *qfprev = NULL; /* init to make SASC shut up */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 char_u *efmp;
Bram Moolenaar01265852006-03-20 21:50:15 +0000213 efm_T *fmt_first = NULL;
214 efm_T *fmt_last = NULL;
215 efm_T *fmt_ptr;
216 efm_T *fmt_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 char_u *efm;
218 char_u *ptr;
219 char_u *srcptr;
220 int len;
221 int i;
222 int round;
223 int idx = 0;
224 int multiline = FALSE;
225 int multiignore = FALSE;
226 int multiscan = FALSE;
227 int retval = -1; /* default: return error flag */
228 char_u *directory = NULL;
229 char_u *currfile = NULL;
230 char_u *tail = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000231 char_u *p_str = NULL;
232 listitem_T *p_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 struct dir_stack_T *file_stack = NULL;
234 regmatch_T regmatch;
235 static struct fmtpattern
236 {
237 char_u convchar;
238 char *pattern;
239 } fmt_pat[FMT_PATTERNS] =
240 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000241 {'f', ".\\+"}, /* only used when at end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 {'n', "\\d\\+"},
243 {'l', "\\d\\+"},
244 {'c', "\\d\\+"},
245 {'t', "."},
246 {'m', ".\\+"},
247 {'r', ".*"},
248 {'p', "[- .]*"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000249 {'v', "\\d\\+"},
250 {'s', ".\\+"}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 };
252
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 namebuf = alloc(CMDBUFFSIZE + 1);
254 errmsg = alloc(CMDBUFFSIZE + 1);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000255 pattern = alloc(CMDBUFFSIZE + 1);
256 if (namebuf == NULL || errmsg == NULL || pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 goto qf_init_end;
258
Bram Moolenaar86b68352004-12-27 21:59:20 +0000259 if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 {
261 EMSG2(_(e_openerrf), efile);
262 goto qf_init_end;
263 }
264
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000265 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 /* make place for a new list */
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200267 qf_new_list(qi, qf_title);
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000268 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000270 for (qfprev = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200271 qfprev->qf_next != qfprev; qfprev = qfprev->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 ;
273
274/*
275 * Each part of the format string is copied and modified from errorformat to
276 * regex prog. Only a few % characters are allowed.
277 */
278 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000279 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000280 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 else
282 efm = errorformat;
283 /*
284 * Get some space to modify the format string into.
285 */
286 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
287 for (round = FMT_PATTERNS; round > 0; )
288 i += (int)STRLEN(fmt_pat[--round].pattern);
289#ifdef COLON_IN_FILENAME
290 i += 12; /* "%f" can become twelve chars longer */
291#else
292 i += 2; /* "%f" can become two chars longer */
293#endif
294 if ((fmtstr = alloc(i)) == NULL)
295 goto error2;
296
Bram Moolenaar01265852006-03-20 21:50:15 +0000297 while (efm[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 {
299 /*
300 * Allocate a new eformat structure and put it at the end of the list
301 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000302 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 if (fmt_ptr == NULL)
304 goto error2;
305 if (fmt_first == NULL) /* first one */
306 fmt_first = fmt_ptr;
307 else
308 fmt_last->next = fmt_ptr;
309 fmt_last = fmt_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310
311 /*
312 * Isolate one part in the 'errorformat' option
313 */
314 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
315 if (efm[len] == '\\' && efm[len + 1] != NUL)
316 ++len;
317
318 /*
319 * Build regexp pattern from current 'errorformat' option
320 */
321 ptr = fmtstr;
322 *ptr++ = '^';
Bram Moolenaar01265852006-03-20 21:50:15 +0000323 round = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 for (efmp = efm; efmp < efm + len; ++efmp)
325 {
326 if (*efmp == '%')
327 {
328 ++efmp;
329 for (idx = 0; idx < FMT_PATTERNS; ++idx)
330 if (fmt_pat[idx].convchar == *efmp)
331 break;
332 if (idx < FMT_PATTERNS)
333 {
334 if (fmt_ptr->addr[idx])
335 {
336 sprintf((char *)errmsg,
337 _("E372: Too many %%%c in format string"), *efmp);
338 EMSG(errmsg);
339 goto error2;
340 }
341 if ((idx
342 && idx < 6
343 && vim_strchr((char_u *)"DXOPQ",
344 fmt_ptr->prefix) != NULL)
345 || (idx == 6
346 && vim_strchr((char_u *)"OPQ",
347 fmt_ptr->prefix) == NULL))
348 {
349 sprintf((char *)errmsg,
350 _("E373: Unexpected %%%c in format string"), *efmp);
351 EMSG(errmsg);
352 goto error2;
353 }
354 fmt_ptr->addr[idx] = (char_u)++round;
355 *ptr++ = '\\';
356 *ptr++ = '(';
357#ifdef BACKSLASH_IN_FILENAME
358 if (*efmp == 'f')
359 {
360 /* Also match "c:" in the file name, even when
361 * checking for a colon next: "%f:".
362 * "\%(\a:\)\=" */
363 STRCPY(ptr, "\\%(\\a:\\)\\=");
364 ptr += 10;
365 }
366#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +0000367 if (*efmp == 'f' && efmp[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000369 if (efmp[1] != '\\' && efmp[1] != '%')
370 {
371 /* A file name may contain spaces, but this isn't
372 * in "\f". For "%f:%l:%m" there may be a ":" in
373 * the file name. Use ".\{-1,}x" instead (x is
374 * the next character), the requirement that :999:
375 * follows should work. */
376 STRCPY(ptr, ".\\{-1,}");
377 ptr += 7;
378 }
379 else
380 {
381 /* File name followed by '\\' or '%': include as
382 * many file name chars as possible. */
383 STRCPY(ptr, "\\f\\+");
384 ptr += 4;
385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 }
387 else
388 {
389 srcptr = (char_u *)fmt_pat[idx].pattern;
390 while ((*ptr = *srcptr++) != NUL)
391 ++ptr;
392 }
393 *ptr++ = '\\';
394 *ptr++ = ')';
395 }
396 else if (*efmp == '*')
397 {
398 if (*++efmp == '[' || *efmp == '\\')
399 {
400 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
401 {
402 if (efmp[1] == '^')
403 *ptr++ = *++efmp;
404 if (efmp < efm + len)
405 {
406 *ptr++ = *++efmp; /* could be ']' */
407 while (efmp < efm + len
408 && (*ptr++ = *++efmp) != ']')
409 /* skip */;
410 if (efmp == efm + len)
411 {
412 EMSG(_("E374: Missing ] in format string"));
413 goto error2;
414 }
415 }
416 }
417 else if (efmp < efm + len) /* %*\D, %*\s etc. */
418 *ptr++ = *++efmp;
419 *ptr++ = '\\';
420 *ptr++ = '+';
421 }
422 else
423 {
424 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
425 sprintf((char *)errmsg,
426 _("E375: Unsupported %%%c in format string"), *efmp);
427 EMSG(errmsg);
428 goto error2;
429 }
430 }
431 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
432 *ptr++ = *efmp; /* regexp magic characters */
433 else if (*efmp == '#')
434 *ptr++ = '*';
Bram Moolenaar01265852006-03-20 21:50:15 +0000435 else if (*efmp == '>')
436 fmt_ptr->conthere = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 else if (efmp == efm + 1) /* analyse prefix */
438 {
439 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
440 fmt_ptr->flags = *efmp++;
441 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
442 fmt_ptr->prefix = *efmp;
443 else
444 {
445 sprintf((char *)errmsg,
446 _("E376: Invalid %%%c in format string prefix"), *efmp);
447 EMSG(errmsg);
448 goto error2;
449 }
450 }
451 else
452 {
453 sprintf((char *)errmsg,
454 _("E377: Invalid %%%c in format string"), *efmp);
455 EMSG(errmsg);
456 goto error2;
457 }
458 }
459 else /* copy normal character */
460 {
461 if (*efmp == '\\' && efmp + 1 < efm + len)
462 ++efmp;
463 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
464 *ptr++ = '\\'; /* escape regexp atoms */
465 if (*efmp)
466 *ptr++ = *efmp;
467 }
468 }
469 *ptr++ = '$';
470 *ptr = NUL;
471 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
472 goto error2;
473 /*
474 * Advance to next part
475 */
476 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
477 }
478 if (fmt_first == NULL) /* nothing found */
479 {
480 EMSG(_("E378: 'errorformat' contains no pattern"));
481 goto error2;
482 }
483
484 /*
485 * got_int is reset here, because it was probably set when killing the
486 * ":make" command, but we still want to read the errorfile then.
487 */
488 got_int = FALSE;
489
490 /* Always ignore case when looking for a matching error. */
491 regmatch.rm_ic = TRUE;
492
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000493 if (tv != NULL)
494 {
495 if (tv->v_type == VAR_STRING)
496 p_str = tv->vval.v_string;
497 else if (tv->v_type == VAR_LIST)
498 p_li = tv->vval.v_list->lv_first;
499 }
500
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 /*
502 * Read the lines in the error file one by one.
503 * Try to recognize one of the error formats in each line.
504 */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000505 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 {
Bram Moolenaar86b68352004-12-27 21:59:20 +0000507 /* Get the next line. */
508 if (fd == NULL)
509 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000510 if (tv != NULL)
511 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000512 if (tv->v_type == VAR_STRING)
513 {
514 /* Get the next line from the supplied string */
515 char_u *p;
516
517 if (!*p_str) /* Reached the end of the string */
518 break;
519
520 p = vim_strchr(p_str, '\n');
521 if (p)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000522 len = (int)(p - p_str + 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000523 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000524 len = (int)STRLEN(p_str);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000525
526 if (len > CMDBUFFSIZE - 2)
527 vim_strncpy(IObuff, p_str, CMDBUFFSIZE - 2);
528 else
529 vim_strncpy(IObuff, p_str, len);
530
531 p_str += len;
532 }
533 else if (tv->v_type == VAR_LIST)
534 {
535 /* Get the next line from the supplied list */
536 while (p_li && p_li->li_tv.v_type != VAR_STRING)
537 p_li = p_li->li_next; /* Skip non-string items */
538
539 if (!p_li) /* End of the list */
540 break;
541
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000542 len = (int)STRLEN(p_li->li_tv.vval.v_string);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000543 if (len > CMDBUFFSIZE - 2)
544 len = CMDBUFFSIZE - 2;
545
546 vim_strncpy(IObuff, p_li->li_tv.vval.v_string, len);
547
548 p_li = p_li->li_next; /* next item */
549 }
550 }
551 else
552 {
553 /* Get the next line from the supplied buffer */
554 if (buflnum > lnumlast)
555 break;
556 vim_strncpy(IObuff, ml_get_buf(buf, buflnum++, FALSE),
557 CMDBUFFSIZE - 2);
558 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000559 }
560 else if (fgets((char *)IObuff, CMDBUFFSIZE - 2, fd) == NULL)
561 break;
562
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 IObuff[CMDBUFFSIZE - 2] = NUL; /* for very long lines */
564 if ((efmp = vim_strrchr(IObuff, '\n')) != NULL)
565 *efmp = NUL;
566#ifdef USE_CRNL
567 if ((efmp = vim_strrchr(IObuff, '\r')) != NULL)
568 *efmp = NUL;
569#endif
570
Bram Moolenaar01265852006-03-20 21:50:15 +0000571 /* If there was no %> item start at the first pattern */
572 if (fmt_start == NULL)
573 fmt_ptr = fmt_first;
574 else
575 {
576 fmt_ptr = fmt_start;
577 fmt_start = NULL;
578 }
579
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 /*
581 * Try to match each part of 'errorformat' until we find a complete
582 * match or no match.
583 */
584 valid = TRUE;
585restofline:
Bram Moolenaar01265852006-03-20 21:50:15 +0000586 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 {
588 idx = fmt_ptr->prefix;
589 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
590 continue;
591 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000592 pattern[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 if (!multiscan)
594 errmsg[0] = NUL;
595 lnum = 0;
596 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000597 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 enr = -1;
599 type = 0;
600 tail = NULL;
601
602 regmatch.regprog = fmt_ptr->prog;
603 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
604 {
605 if ((idx == 'C' || idx == 'Z') && !multiline)
606 continue;
607 if (vim_strchr((char_u *)"EWI", idx) != NULL)
608 type = idx;
609 else
610 type = 0;
611 /*
Bram Moolenaar4169da72006-06-20 18:49:32 +0000612 * Extract error message data from matched line.
613 * We check for an actual submatch, because "\[" and "\]" in
614 * the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 */
616 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
617 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000618 int c;
619
620 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
621 continue;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000622
623 /* Expand ~/file and $HOME/file to full path. */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000624 c = *regmatch.endp[i];
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000625 *regmatch.endp[i] = NUL;
626 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
627 *regmatch.endp[i] = c;
628
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000630 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 continue;
632 }
633 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000634 {
635 if (regmatch.startp[i] == NULL)
636 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 enr = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000640 {
641 if (regmatch.startp[i] == NULL)
642 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 lnum = atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000646 {
647 if (regmatch.startp[i] == NULL)
648 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000652 {
653 if (regmatch.startp[i] == NULL)
654 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 type = *regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000656 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000657 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 STRCPY(errmsg, IObuff);
659 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
660 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000661 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
662 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaarbbebc852005-07-18 21:47:53 +0000664 vim_strncpy(errmsg, regmatch.startp[i], len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 }
666 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000667 {
668 if (regmatch.startp[i] == NULL)
669 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 tail = regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
673 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000674 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
675 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 col = (int)(regmatch.endp[i] - regmatch.startp[i] + 1);
677 if (*((char_u *)regmatch.startp[i]) != TAB)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000678 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 }
680 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
681 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000682 if (regmatch.startp[i] == NULL)
683 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000685 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000687 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
688 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000689 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
690 continue;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000691 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
692 if (len > CMDBUFFSIZE - 5)
693 len = CMDBUFFSIZE - 5;
694 STRCPY(pattern, "^\\V");
695 STRNCAT(pattern, regmatch.startp[i], len);
696 pattern[len + 3] = '\\';
697 pattern[len + 4] = '$';
698 pattern[len + 5] = NUL;
699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 break;
701 }
702 }
703 multiscan = FALSE;
Bram Moolenaar01265852006-03-20 21:50:15 +0000704
Bram Moolenaar4770d092006-01-12 23:22:24 +0000705 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 {
Bram Moolenaar4770d092006-01-12 23:22:24 +0000707 if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 {
709 if (idx == 'D') /* enter directory */
710 {
711 if (*namebuf == NUL)
712 {
713 EMSG(_("E379: Missing or empty directory name"));
714 goto error2;
715 }
716 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
717 goto error2;
718 }
719 else if (idx == 'X') /* leave directory */
720 directory = qf_pop_dir(&dir_stack);
721 }
722 namebuf[0] = NUL; /* no match found, remove file name */
723 lnum = 0; /* don't jump to this line */
724 valid = FALSE;
725 STRCPY(errmsg, IObuff); /* copy whole line to error message */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000726 if (fmt_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 multiline = multiignore = FALSE;
728 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000729 else if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 {
Bram Moolenaar01265852006-03-20 21:50:15 +0000731 /* honor %> item */
732 if (fmt_ptr->conthere)
733 fmt_start = fmt_ptr;
734
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
736 multiline = TRUE; /* start of a multi-line message */
737 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
738 { /* continuation of multi-line msg */
739 if (qfprev == NULL)
740 goto error2;
741 if (*errmsg && !multiignore)
742 {
743 len = (int)STRLEN(qfprev->qf_text);
744 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
745 == NULL)
746 goto error2;
747 STRCPY(ptr, qfprev->qf_text);
748 vim_free(qfprev->qf_text);
749 qfprev->qf_text = ptr;
750 *(ptr += len) = '\n';
751 STRCPY(++ptr, errmsg);
752 }
753 if (qfprev->qf_nr == -1)
754 qfprev->qf_nr = enr;
755 if (vim_isprintc(type) && !qfprev->qf_type)
756 qfprev->qf_type = type; /* only printable chars allowed */
757 if (!qfprev->qf_lnum)
758 qfprev->qf_lnum = lnum;
759 if (!qfprev->qf_col)
760 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000761 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 if (!qfprev->qf_fnum)
763 qfprev->qf_fnum = qf_get_fnum(directory,
764 *namebuf || directory ? namebuf
765 : currfile && valid ? currfile : 0);
766 if (idx == 'Z')
767 multiline = multiignore = FALSE;
768 line_breakcheck();
769 continue;
770 }
771 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
772 {
773 /* global file names */
774 valid = FALSE;
775 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
776 {
777 if (*namebuf && idx == 'P')
778 currfile = qf_push_dir(namebuf, &file_stack);
779 else if (idx == 'Q')
780 currfile = qf_pop_dir(&file_stack);
781 *namebuf = NUL;
782 if (tail && *tail)
783 {
Bram Moolenaarc236c162008-07-13 17:41:49 +0000784 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 multiscan = TRUE;
786 goto restofline;
787 }
788 }
789 }
790 if (fmt_ptr->flags == '-') /* generally exclude this line */
791 {
792 if (multiline)
793 multiignore = TRUE; /* also exclude continuation lines */
794 continue;
795 }
796 }
797
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000798 if (qf_add_entry(qi, &qfprev,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 directory,
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000800 (*namebuf || directory)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 ? namebuf
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000802 : ((currfile && valid) ? currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000803 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 errmsg,
805 lnum,
806 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +0000807 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000808 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 enr,
810 type,
811 valid) == FAIL)
812 goto error2;
813 line_breakcheck();
814 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000815 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000817 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000819 /* no valid entry found */
820 qi->qf_lists[qi->qf_curlist].qf_ptr =
821 qi->qf_lists[qi->qf_curlist].qf_start;
822 qi->qf_lists[qi->qf_curlist].qf_index = 1;
823 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 }
825 else
826 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000827 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
828 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
829 qi->qf_lists[qi->qf_curlist].qf_ptr =
830 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000832 /* return number of matches */
833 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 goto qf_init_ok;
835 }
836 EMSG(_(e_readerrf));
837error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000838 qf_free(qi, qi->qf_curlist);
839 qi->qf_listcount--;
840 if (qi->qf_curlist > 0)
841 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842qf_init_ok:
Bram Moolenaar86b68352004-12-27 21:59:20 +0000843 if (fd != NULL)
844 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
846 {
847 fmt_first = fmt_ptr->next;
848 vim_free(fmt_ptr->prog);
849 vim_free(fmt_ptr);
850 }
851 qf_clean_dir_stack(&dir_stack);
852 qf_clean_dir_stack(&file_stack);
853qf_init_end:
854 vim_free(namebuf);
855 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000856 vim_free(pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 vim_free(fmtstr);
858
859#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000860 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861#endif
862
863 return retval;
864}
865
866/*
867 * Prepare for adding a new quickfix list.
868 */
869 static void
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200870qf_new_list(qi, qf_title)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000871 qf_info_T *qi;
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200872 char_u *qf_title;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873{
874 int i;
875
876 /*
877 * If the current entry is not the last entry, delete entries below
878 * the current entry. This makes it possible to browse in a tree-like
879 * way with ":grep'.
880 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000881 while (qi->qf_listcount > qi->qf_curlist + 1)
882 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883
884 /*
885 * When the stack is full, remove to oldest entry
886 * Otherwise, add a new entry.
887 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000888 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000890 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000892 qi->qf_lists[i - 1] = qi->qf_lists[i];
893 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 }
895 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000896 qi->qf_curlist = qi->qf_listcount++;
897 qi->qf_lists[qi->qf_curlist].qf_index = 0;
898 qi->qf_lists[qi->qf_curlist].qf_count = 0;
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200899 if (qf_title != NULL)
900 {
Bram Moolenaar5e109c42010-07-26 22:51:28 +0200901 char_u *p = alloc((int)STRLEN(qf_title) + 2);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200902
903 qi->qf_lists[qi->qf_curlist].qf_title = p;
904 if (p != NULL)
905 sprintf((char *)p, ":%s", (char *)qf_title);
906 }
907 else
908 qi->qf_lists[qi->qf_curlist].qf_title = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909}
910
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000911/*
912 * Free a location list
913 */
914 static void
915ll_free_all(pqi)
916 qf_info_T **pqi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000917{
918 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000919 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000920
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000921 qi = *pqi;
922 if (qi == NULL)
923 return;
924 *pqi = NULL; /* Remove reference to this list */
925
926 qi->qf_refcount--;
927 if (qi->qf_refcount < 1)
928 {
929 /* No references to this location list */
930 for (i = 0; i < qi->qf_listcount; ++i)
931 qf_free(qi, i);
932 vim_free(qi);
933 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000934}
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000935
936 void
937qf_free_all(wp)
938 win_T *wp;
939{
940 int i;
941 qf_info_T *qi = &ql_info;
942
943 if (wp != NULL)
944 {
945 /* location list */
946 ll_free_all(&wp->w_llist);
947 ll_free_all(&wp->w_llist_ref);
948 }
949 else
950 /* quickfix list */
951 for (i = 0; i < qi->qf_listcount; ++i)
952 qf_free(qi, i);
953}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000954
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955/*
956 * Add an entry to the end of the list of errors.
957 * Returns OK or FAIL.
958 */
959 static int
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000960qf_add_entry(qi, prevp, dir, fname, bufnum, mesg, lnum, col, vis_col, pattern,
961 nr, type, valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000962 qf_info_T *qi; /* quickfix list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000963 qfline_T **prevp; /* pointer to previously added entry or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 char_u *dir; /* optional directory name */
965 char_u *fname; /* file name or NULL */
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000966 int bufnum; /* buffer number or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 char_u *mesg; /* message */
968 long lnum; /* line number */
969 int col; /* column */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000970 int vis_col; /* using visual column */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000971 char_u *pattern; /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 int nr; /* error number */
973 int type; /* type character */
974 int valid; /* valid entry */
975{
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000976 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000978 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000980 if (bufnum != 0)
981 qfp->qf_fnum = bufnum;
982 else
983 qfp->qf_fnum = qf_get_fnum(dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
985 {
986 vim_free(qfp);
987 return FAIL;
988 }
989 qfp->qf_lnum = lnum;
990 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000991 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000992 if (pattern == NULL || *pattern == NUL)
993 qfp->qf_pattern = NULL;
994 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
995 {
996 vim_free(qfp->qf_text);
997 vim_free(qfp);
998 return FAIL;
999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 qfp->qf_nr = nr;
1001 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1002 type = 0;
1003 qfp->qf_type = type;
1004 qfp->qf_valid = valid;
1005
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001006 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1007 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001009 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 qfp->qf_prev = qfp; /* first element points to itself */
1011 }
1012 else
1013 {
1014 qfp->qf_prev = *prevp;
1015 (*prevp)->qf_next = qfp;
1016 }
1017 qfp->qf_next = qfp; /* last element points to itself */
1018 qfp->qf_cleared = FALSE;
1019 *prevp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001020 ++qi->qf_lists[qi->qf_curlist].qf_count;
1021 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1022 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001024 qi->qf_lists[qi->qf_curlist].qf_index =
1025 qi->qf_lists[qi->qf_curlist].qf_count;
1026 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 }
1028
1029 return OK;
1030}
1031
1032/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001033 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001034 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001035 static qf_info_T *
1036ll_new_list()
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001037{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001038 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001039
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001040 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1041 if (qi != NULL)
1042 {
1043 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1044 qi->qf_refcount++;
1045 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001046
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001047 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001048}
1049
1050/*
1051 * Return the location list for window 'wp'.
1052 * If not present, allocate a location list
1053 */
1054 static qf_info_T *
1055ll_get_or_alloc_list(wp)
1056 win_T *wp;
1057{
1058 if (IS_LL_WINDOW(wp))
1059 /* For a location list window, use the referenced location list */
1060 return wp->w_llist_ref;
1061
1062 /*
1063 * For a non-location list window, w_llist_ref should not point to a
1064 * location list.
1065 */
1066 ll_free_all(&wp->w_llist_ref);
1067
1068 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001069 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001070 return wp->w_llist;
1071}
1072
1073/*
1074 * Copy the location list from window "from" to window "to".
1075 */
1076 void
1077copy_loclist(from, to)
1078 win_T *from;
1079 win_T *to;
1080{
1081 qf_info_T *qi;
1082 int idx;
1083 int i;
1084
1085 /*
1086 * When copying from a location list window, copy the referenced
1087 * location list. For other windows, copy the location list for
1088 * that window.
1089 */
1090 if (IS_LL_WINDOW(from))
1091 qi = from->w_llist_ref;
1092 else
1093 qi = from->w_llist;
1094
1095 if (qi == NULL) /* no location list to copy */
1096 return;
1097
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001098 /* allocate a new location list */
1099 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001100 return;
1101
1102 to->w_llist->qf_listcount = qi->qf_listcount;
1103
1104 /* Copy the location lists one at a time */
1105 for (idx = 0; idx < qi->qf_listcount; idx++)
1106 {
1107 qf_list_T *from_qfl;
1108 qf_list_T *to_qfl;
1109
1110 to->w_llist->qf_curlist = idx;
1111
1112 from_qfl = &qi->qf_lists[idx];
1113 to_qfl = &to->w_llist->qf_lists[idx];
1114
1115 /* Some of the fields are populated by qf_add_entry() */
1116 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1117 to_qfl->qf_count = 0;
1118 to_qfl->qf_index = 0;
1119 to_qfl->qf_start = NULL;
1120 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001121 if (from_qfl->qf_title != NULL)
1122 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1123 else
1124 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001125
1126 if (from_qfl->qf_count)
1127 {
1128 qfline_T *from_qfp;
1129 qfline_T *prevp = NULL;
1130
1131 /* copy all the location entries in this list */
1132 for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
1133 ++i, from_qfp = from_qfp->qf_next)
1134 {
1135 if (qf_add_entry(to->w_llist, &prevp,
1136 NULL,
1137 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001138 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001139 from_qfp->qf_text,
1140 from_qfp->qf_lnum,
1141 from_qfp->qf_col,
1142 from_qfp->qf_viscol,
1143 from_qfp->qf_pattern,
1144 from_qfp->qf_nr,
1145 0,
1146 from_qfp->qf_valid) == FAIL)
1147 {
1148 qf_free_all(to);
1149 return;
1150 }
1151 /*
1152 * qf_add_entry() will not set the qf_num field, as the
1153 * directory and file names are not supplied. So the qf_fnum
1154 * field is copied here.
1155 */
1156 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1157 prevp->qf_type = from_qfp->qf_type; /* error type */
1158 if (from_qfl->qf_ptr == from_qfp)
1159 to_qfl->qf_ptr = prevp; /* current location */
1160 }
1161 }
1162
1163 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1164
1165 /* When no valid entries are present in the list, qf_ptr points to
1166 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001167 if (to_qfl->qf_nonevalid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001168 to_qfl->qf_ptr = to_qfl->qf_start;
1169 }
1170
1171 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1172}
1173
1174/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 * get buffer number for file "dir.name"
1176 */
1177 static int
1178qf_get_fnum(directory, fname)
1179 char_u *directory;
1180 char_u *fname;
1181{
1182 if (fname == NULL || *fname == NUL) /* no file name */
1183 return 0;
1184 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 char_u *ptr;
1186 int fnum;
1187
Bram Moolenaare60acc12011-05-10 16:41:25 +02001188#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001190#endif
1191#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 if (directory != NULL)
1193 slash_adjust(directory);
1194 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 if (directory != NULL && !vim_isAbsName(fname)
1197 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1198 {
1199 /*
1200 * Here we check if the file really exists.
1201 * This should normally be true, but if make works without
1202 * "leaving directory"-messages we might have missed a
1203 * directory change.
1204 */
1205 if (mch_getperm(ptr) < 0)
1206 {
1207 vim_free(ptr);
1208 directory = qf_guess_filepath(fname);
1209 if (directory)
1210 ptr = concat_fnames(directory, fname, TRUE);
1211 else
1212 ptr = vim_strsave(fname);
1213 }
1214 /* Use concatenated directory name and file name */
1215 fnum = buflist_add(ptr, 0);
1216 vim_free(ptr);
1217 return fnum;
1218 }
1219 return buflist_add(fname, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 }
1221}
1222
1223/*
1224 * push dirbuf onto the directory stack and return pointer to actual dir or
1225 * NULL on error
1226 */
1227 static char_u *
1228qf_push_dir(dirbuf, stackptr)
1229 char_u *dirbuf;
1230 struct dir_stack_T **stackptr;
1231{
1232 struct dir_stack_T *ds_new;
1233 struct dir_stack_T *ds_ptr;
1234
1235 /* allocate new stack element and hook it in */
1236 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1237 if (ds_new == NULL)
1238 return NULL;
1239
1240 ds_new->next = *stackptr;
1241 *stackptr = ds_new;
1242
1243 /* store directory on the stack */
1244 if (vim_isAbsName(dirbuf)
1245 || (*stackptr)->next == NULL
1246 || (*stackptr && dir_stack != *stackptr))
1247 (*stackptr)->dirname = vim_strsave(dirbuf);
1248 else
1249 {
1250 /* Okay we don't have an absolute path.
1251 * dirbuf must be a subdir of one of the directories on the stack.
1252 * Let's search...
1253 */
1254 ds_new = (*stackptr)->next;
1255 (*stackptr)->dirname = NULL;
1256 while (ds_new)
1257 {
1258 vim_free((*stackptr)->dirname);
1259 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1260 TRUE);
1261 if (mch_isdir((*stackptr)->dirname) == TRUE)
1262 break;
1263
1264 ds_new = ds_new->next;
1265 }
1266
1267 /* clean up all dirs we already left */
1268 while ((*stackptr)->next != ds_new)
1269 {
1270 ds_ptr = (*stackptr)->next;
1271 (*stackptr)->next = (*stackptr)->next->next;
1272 vim_free(ds_ptr->dirname);
1273 vim_free(ds_ptr);
1274 }
1275
1276 /* Nothing found -> it must be on top level */
1277 if (ds_new == NULL)
1278 {
1279 vim_free((*stackptr)->dirname);
1280 (*stackptr)->dirname = vim_strsave(dirbuf);
1281 }
1282 }
1283
1284 if ((*stackptr)->dirname != NULL)
1285 return (*stackptr)->dirname;
1286 else
1287 {
1288 ds_ptr = *stackptr;
1289 *stackptr = (*stackptr)->next;
1290 vim_free(ds_ptr);
1291 return NULL;
1292 }
1293}
1294
1295
1296/*
1297 * pop dirbuf from the directory stack and return previous directory or NULL if
1298 * stack is empty
1299 */
1300 static char_u *
1301qf_pop_dir(stackptr)
1302 struct dir_stack_T **stackptr;
1303{
1304 struct dir_stack_T *ds_ptr;
1305
1306 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1307 * What to do if it isn't? */
1308
1309 /* pop top element and free it */
1310 if (*stackptr != NULL)
1311 {
1312 ds_ptr = *stackptr;
1313 *stackptr = (*stackptr)->next;
1314 vim_free(ds_ptr->dirname);
1315 vim_free(ds_ptr);
1316 }
1317
1318 /* return NEW top element as current dir or NULL if stack is empty*/
1319 return *stackptr ? (*stackptr)->dirname : NULL;
1320}
1321
1322/*
1323 * clean up directory stack
1324 */
1325 static void
1326qf_clean_dir_stack(stackptr)
1327 struct dir_stack_T **stackptr;
1328{
1329 struct dir_stack_T *ds_ptr;
1330
1331 while ((ds_ptr = *stackptr) != NULL)
1332 {
1333 *stackptr = (*stackptr)->next;
1334 vim_free(ds_ptr->dirname);
1335 vim_free(ds_ptr);
1336 }
1337}
1338
1339/*
1340 * Check in which directory of the directory stack the given file can be
1341 * found.
1342 * Returns a pointer to the directory name or NULL if not found
1343 * Cleans up intermediate directory entries.
1344 *
1345 * TODO: How to solve the following problem?
1346 * If we have the this directory tree:
1347 * ./
1348 * ./aa
1349 * ./aa/bb
1350 * ./bb
1351 * ./bb/x.c
1352 * and make says:
1353 * making all in aa
1354 * making all in bb
1355 * x.c:9: Error
1356 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1357 * qf_guess_filepath will return NULL.
1358 */
1359 static char_u *
1360qf_guess_filepath(filename)
1361 char_u *filename;
1362{
1363 struct dir_stack_T *ds_ptr;
1364 struct dir_stack_T *ds_tmp;
1365 char_u *fullname;
1366
1367 /* no dirs on the stack - there's nothing we can do */
1368 if (dir_stack == NULL)
1369 return NULL;
1370
1371 ds_ptr = dir_stack->next;
1372 fullname = NULL;
1373 while (ds_ptr)
1374 {
1375 vim_free(fullname);
1376 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1377
1378 /* If concat_fnames failed, just go on. The worst thing that can happen
1379 * is that we delete the entire stack.
1380 */
1381 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1382 break;
1383
1384 ds_ptr = ds_ptr->next;
1385 }
1386
1387 vim_free(fullname);
1388
1389 /* clean up all dirs we already left */
1390 while (dir_stack->next != ds_ptr)
1391 {
1392 ds_tmp = dir_stack->next;
1393 dir_stack->next = dir_stack->next->next;
1394 vim_free(ds_tmp->dirname);
1395 vim_free(ds_tmp);
1396 }
1397
1398 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1399
1400}
1401
1402/*
1403 * jump to a quickfix line
1404 * if dir == FORWARD go "errornr" valid entries forward
1405 * if dir == BACKWARD go "errornr" valid entries backward
1406 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1407 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1408 * else if "errornr" is zero, redisplay the same line
1409 * else go to entry "errornr"
1410 */
1411 void
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001412qf_jump(qi, dir, errornr, forceit)
1413 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 int dir;
1415 int errornr;
1416 int forceit;
1417{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001418 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001419 qfline_T *qf_ptr;
1420 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 int qf_index;
1422 int old_qf_fnum;
1423 int old_qf_index;
1424 int prev_index;
1425 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1426 char_u *err = e_no_more_items;
1427 linenr_T i;
1428 buf_T *old_curbuf;
1429 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 colnr_T screen_col;
1431 colnr_T char_col;
1432 char_u *line;
1433#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001434 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001435 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 int opened_window = FALSE;
1437 win_T *win;
1438 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001439 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001441 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 int print_message = TRUE;
1443 int len;
1444#ifdef FEAT_FOLDING
1445 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1446#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001447 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001448 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001450 if (qi == NULL)
1451 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001452
1453 if (qi->qf_curlist >= qi->qf_listcount
1454 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 {
1456 EMSG(_(e_quickfix));
1457 return;
1458 }
1459
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001460 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001462 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 old_qf_index = qf_index;
1464 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1465 {
1466 while (errornr--)
1467 {
1468 old_qf_ptr = qf_ptr;
1469 prev_index = qf_index;
1470 old_qf_fnum = qf_ptr->qf_fnum;
1471 do
1472 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001473 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 || qf_ptr->qf_next == NULL)
1475 {
1476 qf_ptr = old_qf_ptr;
1477 qf_index = prev_index;
1478 if (err != NULL)
1479 {
1480 EMSG(_(err));
1481 goto theend;
1482 }
1483 errornr = 0;
1484 break;
1485 }
1486 ++qf_index;
1487 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001488 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1489 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1491 err = NULL;
1492 }
1493 }
1494 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1495 {
1496 while (errornr--)
1497 {
1498 old_qf_ptr = qf_ptr;
1499 prev_index = qf_index;
1500 old_qf_fnum = qf_ptr->qf_fnum;
1501 do
1502 {
1503 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1504 {
1505 qf_ptr = old_qf_ptr;
1506 qf_index = prev_index;
1507 if (err != NULL)
1508 {
1509 EMSG(_(err));
1510 goto theend;
1511 }
1512 errornr = 0;
1513 break;
1514 }
1515 --qf_index;
1516 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001517 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1518 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1520 err = NULL;
1521 }
1522 }
1523 else if (errornr != 0) /* go to specified number */
1524 {
1525 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1526 {
1527 --qf_index;
1528 qf_ptr = qf_ptr->qf_prev;
1529 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001530 while (errornr > qf_index && qf_index <
1531 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 && qf_ptr->qf_next != NULL)
1533 {
1534 ++qf_index;
1535 qf_ptr = qf_ptr->qf_next;
1536 }
1537 }
1538
1539#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001540 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1541 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 /* No need to print the error message if it's visible in the error
1543 * window */
1544 print_message = FALSE;
1545
1546 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001547 * For ":helpgrep" find a help window or open one.
1548 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001549 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001550 {
1551 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001552
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001553 if (cmdmod.tab != 0)
1554 wp = NULL;
1555 else
1556 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1557 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1558 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001559 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1560 win_enter(wp, TRUE);
1561 else
1562 {
1563 /*
1564 * Split off help window; put it at far top if no position
1565 * specified, the current window is vertically split and narrow.
1566 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001567 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001568# ifdef FEAT_VERTSPLIT
1569 if (cmdmod.split == 0 && curwin->w_width != Columns
1570 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00001571 flags |= WSP_TOP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001572# endif
Bram Moolenaar884ae642009-02-22 01:37:59 +00001573 if (qi != &ql_info)
1574 flags |= WSP_NEWLOC; /* don't copy the location list */
1575
1576 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001577 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001578 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001579
1580 if (curwin->w_height < p_hh)
1581 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001582
1583 if (qi != &ql_info) /* not a quickfix list */
1584 {
1585 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001586 curwin->w_llist = qi;
1587 qi->qf_refcount++;
1588 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001589 }
1590
1591 if (!p_im)
1592 restart_edit = 0; /* don't want insert mode in help file */
1593 }
1594
1595 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 * If currently in the quickfix window, find another window to show the
1597 * file in.
1598 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001599 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 {
1601 /*
1602 * If there is no file specified, we don't know where to go.
1603 * But do advance, otherwise ":cn" gets stuck.
1604 */
1605 if (qf_ptr->qf_fnum == 0)
1606 goto theend;
1607
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001608 /* Locate a window showing a normal buffer */
1609 usable_win = 0;
1610 FOR_ALL_WINDOWS(win)
1611 if (win->w_buffer->b_p_bt[0] == NUL)
1612 {
1613 usable_win = 1;
1614 break;
1615 }
1616
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00001618 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001619 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001621 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001622 {
1623 tabpage_T *tp;
1624 win_T *wp;
1625
1626 FOR_ALL_TAB_WINDOWS(tp, wp)
1627 {
1628 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
1629 {
1630 goto_tabpage_win(tp, wp);
1631 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001632 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001633 }
1634 }
1635 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001636win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001637
1638 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00001639 * If there is only one window and it is the quickfix window, create a
1640 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001641 */
1642 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001644 ll_ref = curwin->w_llist_ref;
1645
Bram Moolenaar884ae642009-02-22 01:37:59 +00001646 flags = WSP_ABOVE;
1647 if (ll_ref != NULL)
1648 flags |= WSP_NEWLOC;
1649 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 goto failed; /* not enough room for window */
1651 opened_window = TRUE; /* close it when fail */
1652 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001653 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001654 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001655 if (ll_ref != NULL)
1656 {
1657 /* The new window should use the location list from the
1658 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001659 curwin->w_llist = ll_ref;
1660 ll_ref->qf_refcount++;
1661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 }
1663 else
1664 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001665 if (curwin->w_llist_ref != NULL)
1666 {
1667 /* In a location window */
1668 ll_ref = curwin->w_llist_ref;
1669
1670 /* Find the window with the same location list */
1671 FOR_ALL_WINDOWS(win)
1672 if (win->w_llist == ll_ref)
1673 break;
1674 if (win == NULL)
1675 {
1676 /* Find the window showing the selected file */
1677 FOR_ALL_WINDOWS(win)
1678 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1679 break;
1680 if (win == NULL)
1681 {
1682 /* Find a previous usable window */
1683 win = curwin;
1684 do
1685 {
1686 if (win->w_buffer->b_p_bt[0] == NUL)
1687 break;
1688 if (win->w_prev == NULL)
1689 win = lastwin; /* wrap around the top */
1690 else
1691 win = win->w_prev; /* go to previous window */
1692 } while (win != curwin);
1693 }
1694 }
1695 win_goto(win);
1696
1697 /* If the location list for the window is not set, then set it
1698 * to the location list from the location window */
1699 if (win->w_llist == NULL)
1700 {
1701 win->w_llist = ll_ref;
1702 ll_ref->qf_refcount++;
1703 }
1704 }
1705 else
1706 {
1707
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 /*
1709 * Try to find a window that shows the right buffer.
1710 * Default to the window just above the quickfix buffer.
1711 */
1712 win = curwin;
1713 altwin = NULL;
1714 for (;;)
1715 {
1716 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1717 break;
1718 if (win->w_prev == NULL)
1719 win = lastwin; /* wrap around the top */
1720 else
1721 win = win->w_prev; /* go to previous window */
1722
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001723 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 {
1725 /* Didn't find it, go to the window before the quickfix
1726 * window. */
1727 if (altwin != NULL)
1728 win = altwin;
1729 else if (curwin->w_prev != NULL)
1730 win = curwin->w_prev;
1731 else
1732 win = curwin->w_next;
1733 break;
1734 }
1735
1736 /* Remember a usable window. */
1737 if (altwin == NULL && !win->w_p_pvw
1738 && win->w_buffer->b_p_bt[0] == NUL)
1739 altwin = win;
1740 }
1741
1742 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 }
1745 }
1746#endif
1747
1748 /*
1749 * If there is a file name,
1750 * read the wanted file if needed, and check autowrite etc.
1751 */
1752 old_curbuf = curbuf;
1753 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001754
1755 if (qf_ptr->qf_fnum != 0)
1756 {
1757 if (qf_ptr->qf_type == 1)
1758 {
1759 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1760 * set b_p_ro flag). */
1761 if (!can_abandon(curbuf, forceit))
1762 {
1763 EMSG(_(e_nowrtmsg));
1764 ok = FALSE;
1765 }
1766 else
1767 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001768 ECMD_HIDE + ECMD_SET_HELP,
1769 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001770 }
1771 else
1772 ok = buflist_getfile(qf_ptr->qf_fnum,
1773 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
1774 }
1775
1776 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 {
1778 /* When not switched to another buffer, still need to set pc mark */
1779 if (curbuf == old_curbuf)
1780 setpcmark();
1781
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001782 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001784 /*
1785 * Go to line with error, unless qf_lnum is 0.
1786 */
1787 i = qf_ptr->qf_lnum;
1788 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001790 if (i > curbuf->b_ml.ml_line_count)
1791 i = curbuf->b_ml.ml_line_count;
1792 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001794 if (qf_ptr->qf_col > 0)
1795 {
1796 curwin->w_cursor.col = qf_ptr->qf_col - 1;
1797 if (qf_ptr->qf_viscol == TRUE)
1798 {
1799 /*
1800 * Check each character from the beginning of the error
1801 * line up to the error column. For each tab character
1802 * found, reduce the error column value by the length of
1803 * a tab character.
1804 */
1805 line = ml_get_curline();
1806 screen_col = 0;
1807 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1808 {
1809 if (*line == NUL)
1810 break;
1811 if (*line++ == '\t')
1812 {
1813 curwin->w_cursor.col -= 7 - (screen_col % 8);
1814 screen_col += 8 - (screen_col % 8);
1815 }
1816 else
1817 ++screen_col;
1818 }
1819 }
1820 check_cursor();
1821 }
1822 else
1823 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 }
1825 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001826 {
1827 pos_T save_cursor;
1828
1829 /* Move the cursor to the first line in the buffer */
1830 save_cursor = curwin->w_cursor;
1831 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001832 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
1833 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001834 curwin->w_cursor = save_cursor;
1835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836
1837#ifdef FEAT_FOLDING
1838 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1839 foldOpenCursor();
1840#endif
1841 if (print_message)
1842 {
1843 /* Update the screen before showing the message */
1844 update_topline_redraw();
1845 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001846 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1848 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1849 /* Add the message, skipping leading whitespace and newlines. */
1850 len = (int)STRLEN(IObuff);
1851 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1852
1853 /* Output the message. Overwrite to avoid scrolling when the 'O'
1854 * flag is present in 'shortmess'; But when not jumping, print the
1855 * whole message. */
1856 i = msg_scroll;
1857 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1858 msg_scroll = TRUE;
1859 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1860 msg_scroll = FALSE;
1861 msg_attr_keep(IObuff, 0, TRUE);
1862 msg_scroll = i;
1863 }
1864 }
1865 else
1866 {
1867#ifdef FEAT_WINDOWS
1868 if (opened_window)
1869 win_close(curwin, TRUE); /* Close opened window */
1870#endif
1871 if (qf_ptr->qf_fnum != 0)
1872 {
1873 /*
1874 * Couldn't open file, so put index back where it was. This could
1875 * happen if the file was readonly and we changed something.
1876 */
1877#ifdef FEAT_WINDOWS
1878failed:
1879#endif
1880 qf_ptr = old_qf_ptr;
1881 qf_index = old_qf_index;
1882 }
1883 }
1884theend:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001885 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
1886 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887#ifdef FEAT_WINDOWS
1888 if (p_swb != old_swb && opened_window)
1889 {
1890 /* Restore old 'switchbuf' value, but not when an autocommand or
1891 * modeline has changed the value. */
1892 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00001893 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001895 swb_flags = old_swb_flags;
1896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 else
1898 free_string_option(old_swb);
1899 }
1900#endif
1901}
1902
1903/*
1904 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001905 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 */
1907 void
1908qf_list(eap)
1909 exarg_T *eap;
1910{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001911 buf_T *buf;
1912 char_u *fname;
1913 qfline_T *qfp;
1914 int i;
1915 int idx1 = 1;
1916 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001917 char_u *arg = eap->arg;
1918 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001920 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001922 if (eap->cmdidx == CMD_llist)
1923 {
1924 qi = GET_LOC_LIST(curwin);
1925 if (qi == NULL)
1926 {
1927 EMSG(_(e_loclist));
1928 return;
1929 }
1930 }
1931
1932 if (qi->qf_curlist >= qi->qf_listcount
1933 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 {
1935 EMSG(_(e_quickfix));
1936 return;
1937 }
1938 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
1939 {
1940 EMSG(_(e_trailing));
1941 return;
1942 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001943 i = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 if (idx1 < 0)
1945 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
1946 if (idx2 < 0)
1947 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
1948
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001949 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001951 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
1952 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 {
1954 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
1955 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01001956 msg_putchar('\n');
1957 if (got_int)
1958 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001959
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001960 fname = NULL;
1961 if (qfp->qf_fnum != 0
1962 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
1963 {
1964 fname = buf->b_fname;
1965 if (qfp->qf_type == 1) /* :helpgrep */
1966 fname = gettail(fname);
1967 }
1968 if (fname == NULL)
1969 sprintf((char *)IObuff, "%2d", i);
1970 else
1971 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
1972 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001973 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001974 ? hl_attr(HLF_L) : hl_attr(HLF_D));
1975 if (qfp->qf_lnum == 0)
1976 IObuff[0] = NUL;
1977 else if (qfp->qf_col == 0)
1978 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
1979 else
1980 sprintf((char *)IObuff, ":%ld col %d",
1981 qfp->qf_lnum, qfp->qf_col);
1982 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
1983 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
1984 msg_puts_attr(IObuff, hl_attr(HLF_N));
1985 if (qfp->qf_pattern != NULL)
1986 {
1987 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
1988 STRCAT(IObuff, ":");
1989 msg_puts(IObuff);
1990 }
1991 msg_puts((char_u *)" ");
1992
1993 /* Remove newlines and leading whitespace from the text. For an
1994 * unrecognized line keep the indent, the compiler may mark a word
1995 * with ^^^^. */
1996 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 ? skipwhite(qfp->qf_text) : qfp->qf_text,
1998 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001999 msg_prt_line(IObuff, FALSE);
2000 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002002
2003 qfp = qfp->qf_next;
2004 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 ui_breakcheck();
2006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007}
2008
2009/*
2010 * Remove newlines and leading whitespace from an error message.
2011 * Put the result in "buf[bufsize]".
2012 */
2013 static void
2014qf_fmt_text(text, buf, bufsize)
2015 char_u *text;
2016 char_u *buf;
2017 int bufsize;
2018{
2019 int i;
2020 char_u *p = text;
2021
2022 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2023 {
2024 if (*p == '\n')
2025 {
2026 buf[i] = ' ';
2027 while (*++p != NUL)
2028 if (!vim_iswhite(*p) && *p != '\n')
2029 break;
2030 }
2031 else
2032 buf[i] = *p++;
2033 }
2034 buf[i] = NUL;
2035}
2036
2037/*
2038 * ":colder [count]": Up in the quickfix stack.
2039 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002040 * ":lolder [count]": Up in the location list stack.
2041 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 */
2043 void
2044qf_age(eap)
2045 exarg_T *eap;
2046{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002047 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 int count;
2049
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002050 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2051 {
2052 qi = GET_LOC_LIST(curwin);
2053 if (qi == NULL)
2054 {
2055 EMSG(_(e_loclist));
2056 return;
2057 }
2058 }
2059
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 if (eap->addr_count != 0)
2061 count = eap->line2;
2062 else
2063 count = 1;
2064 while (count--)
2065 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002066 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002068 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 {
2070 EMSG(_("E380: At bottom of quickfix stack"));
2071 return;
2072 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002073 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 }
2075 else
2076 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002077 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 {
2079 EMSG(_("E381: At top of quickfix stack"));
2080 return;
2081 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002082 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 }
2084 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002085 qf_msg(qi);
2086
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087}
2088
2089 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002090qf_msg(qi)
2091 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092{
2093 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002094 qi->qf_curlist + 1, qi->qf_listcount,
2095 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002097 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098#endif
2099}
2100
2101/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002102 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 */
2104 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002105qf_free(qi, idx)
2106 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 int idx;
2108{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002109 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002111 while (qi->qf_lists[idx].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002113 qfp = qi->qf_lists[idx].qf_start->qf_next;
2114 vim_free(qi->qf_lists[idx].qf_start->qf_text);
2115 vim_free(qi->qf_lists[idx].qf_start->qf_pattern);
2116 vim_free(qi->qf_lists[idx].qf_start);
2117 qi->qf_lists[idx].qf_start = qfp;
2118 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002120 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002121 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122}
2123
2124/*
2125 * qf_mark_adjust: adjust marks
2126 */
2127 void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002128qf_mark_adjust(wp, line1, line2, amount, amount_after)
2129 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 linenr_T line1;
2131 linenr_T line2;
2132 long amount;
2133 long amount_after;
2134{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002135 int i;
2136 qfline_T *qfp;
2137 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002138 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002140 if (wp != NULL)
2141 {
2142 if (wp->w_llist == NULL)
2143 return;
2144 qi = wp->w_llist;
2145 }
2146
2147 for (idx = 0; idx < qi->qf_listcount; ++idx)
2148 if (qi->qf_lists[idx].qf_count)
2149 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
2150 i < qi->qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 if (qfp->qf_fnum == curbuf->b_fnum)
2152 {
2153 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2154 {
2155 if (amount == MAXLNUM)
2156 qfp->qf_cleared = TRUE;
2157 else
2158 qfp->qf_lnum += amount;
2159 }
2160 else if (amount_after && qfp->qf_lnum > line2)
2161 qfp->qf_lnum += amount_after;
2162 }
2163}
2164
2165/*
2166 * Make a nice message out of the error character and the error number:
2167 * char number message
2168 * e or E 0 " error"
2169 * w or W 0 " warning"
2170 * i or I 0 " info"
2171 * 0 0 ""
2172 * other 0 " c"
2173 * e or E n " error n"
2174 * w or W n " warning n"
2175 * i or I n " info n"
2176 * 0 n " error n"
2177 * other n " c n"
2178 * 1 x "" :helpgrep
2179 */
2180 static char_u *
2181qf_types(c, nr)
2182 int c, nr;
2183{
2184 static char_u buf[20];
2185 static char_u cc[3];
2186 char_u *p;
2187
2188 if (c == 'W' || c == 'w')
2189 p = (char_u *)" warning";
2190 else if (c == 'I' || c == 'i')
2191 p = (char_u *)" info";
2192 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2193 p = (char_u *)" error";
2194 else if (c == 0 || c == 1)
2195 p = (char_u *)"";
2196 else
2197 {
2198 cc[0] = ' ';
2199 cc[1] = c;
2200 cc[2] = NUL;
2201 p = cc;
2202 }
2203
2204 if (nr <= 0)
2205 return p;
2206
2207 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2208 return buf;
2209}
2210
2211#if defined(FEAT_WINDOWS) || defined(PROTO)
2212/*
2213 * ":cwindow": open the quickfix window if we have errors to display,
2214 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002215 * ":lwindow": open the location list window if we have locations to display,
2216 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 */
2218 void
2219ex_cwindow(eap)
2220 exarg_T *eap;
2221{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002222 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 win_T *win;
2224
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002225 if (eap->cmdidx == CMD_lwindow)
2226 {
2227 qi = GET_LOC_LIST(curwin);
2228 if (qi == NULL)
2229 return;
2230 }
2231
2232 /* Look for an existing quickfix window. */
2233 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234
2235 /*
2236 * If a quickfix window is open but we have no errors to display,
2237 * close the window. If a quickfix window is not open, then open
2238 * it if we have errors; otherwise, leave it closed.
2239 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002240 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002241 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002242 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 {
2244 if (win != NULL)
2245 ex_cclose(eap);
2246 }
2247 else if (win == NULL)
2248 ex_copen(eap);
2249}
2250
2251/*
2252 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002253 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 void
2256ex_cclose(eap)
2257 exarg_T *eap;
2258{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002259 win_T *win = NULL;
2260 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002262 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2263 {
2264 qi = GET_LOC_LIST(curwin);
2265 if (qi == NULL)
2266 return;
2267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002269 /* Find existing quickfix window and close it. */
2270 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 if (win != NULL)
2272 win_close(win, FALSE);
2273}
2274
2275/*
2276 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002277 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 */
2279 void
2280ex_copen(eap)
2281 exarg_T *eap;
2282{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002283 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002286 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002287 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002288 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002290 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2291 {
2292 qi = GET_LOC_LIST(curwin);
2293 if (qi == NULL)
2294 {
2295 EMSG(_(e_loclist));
2296 return;
2297 }
2298 }
2299
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 if (eap->addr_count != 0)
2301 height = eap->line2;
2302 else
2303 height = QF_WINHEIGHT;
2304
2305#ifdef FEAT_VISUAL
2306 reset_VIsual_and_resel(); /* stop Visual mode */
2307#endif
2308#ifdef FEAT_GUI
2309 need_mouse_correct = TRUE;
2310#endif
2311
2312 /*
2313 * Find existing quickfix window, or open a new one.
2314 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002315 win = qf_find_win(qi);
2316
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002317 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 win_goto(win);
2319 else
2320 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002321 qf_buf = qf_find_buf(qi);
2322
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 /* The current window becomes the previous window afterwards. */
2324 win = curwin;
2325
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002326 if (eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2327 /* Create the new window at the very bottom. */
2328 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002329 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002331 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002333 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002335 /*
2336 * For the location list window, create a reference to the
2337 * location list from the window 'win'.
2338 */
2339 curwin->w_llist_ref = win->w_llist;
2340 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002342
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002343 if (oldwin != curwin)
2344 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002345 if (qf_buf != NULL)
2346 /* Use the existing quickfix buffer */
2347 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002348 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002349 else
2350 {
2351 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002352 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002353 /* switch off 'swapfile' */
2354 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2355 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002356 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002357 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002358 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002359#ifdef FEAT_DIFF
2360 curwin->w_p_diff = FALSE;
2361#endif
2362#ifdef FEAT_FOLDING
2363 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2364 OPT_LOCAL);
2365#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002368 /* Only set the height when still in the same tab page and there is no
2369 * window to the side. */
2370 if (curtab == prevtab
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002371#ifdef FEAT_VERTSPLIT
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002372 && curwin->w_width == Columns
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002373#endif
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002374 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 win_setheight(height);
2376 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2377 if (win_valid(win))
2378 prevwin = win;
2379 }
2380
2381 /*
2382 * Fill the buffer with the quickfix list.
2383 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002384 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002386 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
2387 set_internal_string_var((char_u *)"w:quickfix_title",
2388 qi->qf_lists[qi->qf_curlist].qf_title);
2389
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002390 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 curwin->w_cursor.col = 0;
2392 check_cursor();
2393 update_topline(); /* scroll to show the line */
2394}
2395
2396/*
2397 * Return the number of the current entry (line number in the quickfix
2398 * window).
2399 */
2400 linenr_T
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002401qf_current_entry(wp)
2402 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002404 qf_info_T *qi = &ql_info;
2405
2406 if (IS_LL_WINDOW(wp))
2407 /* In the location list window, use the referenced location list */
2408 qi = wp->w_llist_ref;
2409
2410 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411}
2412
2413/*
2414 * Update the cursor position in the quickfix window to the current error.
2415 * Return TRUE if there is a quickfix window.
2416 */
2417 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002418qf_win_pos_update(qi, old_qf_index)
2419 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 int old_qf_index; /* previous qf_index or zero */
2421{
2422 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002423 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424
2425 /*
2426 * Put the cursor on the current error in the quickfix window, so that
2427 * it's viewable.
2428 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002429 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 if (win != NULL
2431 && qf_index <= win->w_buffer->b_ml.ml_line_count
2432 && old_qf_index != qf_index)
2433 {
2434 win_T *old_curwin = curwin;
2435
2436 curwin = win;
2437 curbuf = win->w_buffer;
2438 if (qf_index > old_qf_index)
2439 {
2440 curwin->w_redraw_top = old_qf_index;
2441 curwin->w_redraw_bot = qf_index;
2442 }
2443 else
2444 {
2445 curwin->w_redraw_top = qf_index;
2446 curwin->w_redraw_bot = old_qf_index;
2447 }
2448 curwin->w_cursor.lnum = qf_index;
2449 curwin->w_cursor.col = 0;
2450 update_topline(); /* scroll to show the line */
2451 redraw_later(VALID);
2452 curwin->w_redr_status = TRUE; /* update ruler */
2453 curwin = old_curwin;
2454 curbuf = curwin->w_buffer;
2455 }
2456 return win != NULL;
2457}
2458
2459/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002460 * Check whether the given window is displaying the specified quickfix/location
2461 * list buffer
2462 */
2463 static int
2464is_qf_win(win, qi)
2465 win_T *win;
2466 qf_info_T *qi;
2467{
2468 /*
2469 * A window displaying the quickfix buffer will have the w_llist_ref field
2470 * set to NULL.
2471 * A window displaying a location list buffer will have the w_llist_ref
2472 * pointing to the location list.
2473 */
2474 if (bt_quickfix(win->w_buffer))
2475 if ((qi == &ql_info && win->w_llist_ref == NULL)
2476 || (qi != &ql_info && win->w_llist_ref == qi))
2477 return TRUE;
2478
2479 return FALSE;
2480}
2481
2482/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002483 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00002484 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002485 */
2486 static win_T *
2487qf_find_win(qi)
2488 qf_info_T *qi;
2489{
2490 win_T *win;
2491
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002492 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002493 if (is_qf_win(win, qi))
2494 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002495
2496 return win;
2497}
2498
2499/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002500 * Find a quickfix buffer.
2501 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 */
2503 static buf_T *
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002504qf_find_buf(qi)
2505 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506{
Bram Moolenaar9c102382006-05-03 21:26:49 +00002507 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002508 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509
Bram Moolenaar9c102382006-05-03 21:26:49 +00002510 FOR_ALL_TAB_WINDOWS(tp, win)
2511 if (is_qf_win(win, qi))
2512 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002513
Bram Moolenaar9c102382006-05-03 21:26:49 +00002514 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515}
2516
2517/*
2518 * Find the quickfix buffer. If it exists, update the contents.
2519 */
2520 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002521qf_update_buffer(qi)
2522 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523{
2524 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526
2527 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002528 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 if (buf != NULL)
2530 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 /* set curwin/curbuf to buf and save a few things */
2532 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002534 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 /* restore curwin/curbuf and a few other things */
2537 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002539 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 }
2541}
2542
2543/*
2544 * Fill current buffer with quickfix errors, replacing any previous contents.
2545 * curbuf must be the quickfix buffer!
2546 */
2547 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002548qf_fill_buffer(qi)
2549 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002551 linenr_T lnum;
2552 qfline_T *qfp;
2553 buf_T *errbuf;
2554 int len;
2555 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556
2557 /* delete all existing lines */
2558 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2559 (void)ml_delete((linenr_T)1, FALSE);
2560
2561 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002562 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 {
2564 /* Add one line for each error */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002565 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2566 for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 {
2568 if (qfp->qf_fnum != 0
2569 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2570 && errbuf->b_fname != NULL)
2571 {
2572 if (qfp->qf_type == 1) /* :helpgrep */
2573 STRCPY(IObuff, gettail(errbuf->b_fname));
2574 else
2575 STRCPY(IObuff, errbuf->b_fname);
2576 len = (int)STRLEN(IObuff);
2577 }
2578 else
2579 len = 0;
2580 IObuff[len++] = '|';
2581
2582 if (qfp->qf_lnum > 0)
2583 {
2584 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2585 len += (int)STRLEN(IObuff + len);
2586
2587 if (qfp->qf_col > 0)
2588 {
2589 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2590 len += (int)STRLEN(IObuff + len);
2591 }
2592
2593 sprintf((char *)IObuff + len, "%s",
2594 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2595 len += (int)STRLEN(IObuff + len);
2596 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002597 else if (qfp->qf_pattern != NULL)
2598 {
2599 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2600 len += (int)STRLEN(IObuff + len);
2601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 IObuff[len++] = '|';
2603 IObuff[len++] = ' ';
2604
2605 /* Remove newlines and leading whitespace from the text.
2606 * For an unrecognized line keep the indent, the compiler may
2607 * mark a word with ^^^^. */
2608 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2609 IObuff + len, IOSIZE - len);
2610
2611 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2612 == FAIL)
2613 break;
2614 qfp = qfp->qf_next;
2615 }
2616 /* Delete the empty line which is now at the end */
2617 (void)ml_delete(lnum + 1, FALSE);
2618 }
2619
2620 /* correct cursor position */
2621 check_lnums(TRUE);
2622
2623 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2624 * resembles reading a file into a buffer, it's more logical when using
2625 * autocommands. */
2626 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2627 curbuf->b_p_ma = FALSE;
2628
2629#ifdef FEAT_AUTOCMD
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002630 keep_filetype = TRUE; /* don't detect 'filetype' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2632 FALSE, curbuf);
2633 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2634 FALSE, curbuf);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002635 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636#endif
2637
2638 /* make sure it will be redrawn */
2639 redraw_curbuf_later(NOT_VALID);
2640
2641 /* Restore KeyTyped, setting 'filetype' may reset it. */
2642 KeyTyped = old_KeyTyped;
2643}
2644
2645#endif /* FEAT_WINDOWS */
2646
2647/*
2648 * Return TRUE if "buf" is the quickfix buffer.
2649 */
2650 int
2651bt_quickfix(buf)
2652 buf_T *buf;
2653{
2654 return (buf->b_p_bt[0] == 'q');
2655}
2656
2657/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002658 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2659 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 */
2661 int
2662bt_nofile(buf)
2663 buf_T *buf;
2664{
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002665 return (buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2666 || buf->b_p_bt[0] == 'a';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667}
2668
2669/*
2670 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2671 */
2672 int
2673bt_dontwrite(buf)
2674 buf_T *buf;
2675{
2676 return (buf->b_p_bt[0] == 'n');
2677}
2678
2679 int
2680bt_dontwrite_msg(buf)
2681 buf_T *buf;
2682{
2683 if (bt_dontwrite(buf))
2684 {
2685 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2686 return TRUE;
2687 }
2688 return FALSE;
2689}
2690
2691/*
2692 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2693 * and 'bufhidden'.
2694 */
2695 int
2696buf_hide(buf)
2697 buf_T *buf;
2698{
2699 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2700 switch (buf->b_p_bh[0])
2701 {
2702 case 'u': /* "unload" */
2703 case 'w': /* "wipe" */
2704 case 'd': return FALSE; /* "delete" */
2705 case 'h': return TRUE; /* "hide" */
2706 }
2707 return (p_hid || cmdmod.hide);
2708}
2709
2710/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002711 * Return TRUE when using ":vimgrep" for ":grep".
2712 */
2713 int
Bram Moolenaar81695252004-12-29 20:58:21 +00002714grep_internal(cmdidx)
2715 cmdidx_T cmdidx;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002716{
Bram Moolenaar754b5602006-02-09 23:53:20 +00002717 return ((cmdidx == CMD_grep
2718 || cmdidx == CMD_lgrep
2719 || cmdidx == CMD_grepadd
2720 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002721 && STRCMP("internal",
2722 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2723}
2724
2725/*
Bram Moolenaara6557602006-02-04 22:43:20 +00002726 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 */
2728 void
2729ex_make(eap)
2730 exarg_T *eap;
2731{
Bram Moolenaar7c626922005-02-07 22:01:03 +00002732 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 char_u *cmd;
2734 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00002735 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002736 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002737 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002738#ifdef FEAT_AUTOCMD
2739 char_u *au_name = NULL;
2740
Bram Moolenaard88e02d2011-04-28 17:27:09 +02002741 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
2742 if (grep_internal(eap->cmdidx))
2743 {
2744 ex_vimgrep(eap);
2745 return;
2746 }
2747
Bram Moolenaar7c626922005-02-07 22:01:03 +00002748 switch (eap->cmdidx)
2749 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00002750 case CMD_make: au_name = (char_u *)"make"; break;
2751 case CMD_lmake: au_name = (char_u *)"lmake"; break;
2752 case CMD_grep: au_name = (char_u *)"grep"; break;
2753 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
2754 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
2755 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002756 default: break;
2757 }
2758 if (au_name != NULL)
2759 {
2760 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2761 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00002762# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00002763 if (did_throw || force_abort)
2764 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00002765# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002766 }
2767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768
Bram Moolenaara6557602006-02-04 22:43:20 +00002769 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
2770 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00002771 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00002772
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002774 fname = get_mef_name();
2775 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002777 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778
2779 /*
2780 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2781 */
2782 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2783 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002784 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 cmd = alloc(len);
2786 if (cmd == NULL)
2787 return;
2788 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2789 (char *)p_shq);
2790 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002791 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 /*
2793 * Output a newline if there's something else than the :make command that
2794 * was typed (in which case the cursor is in column 0).
2795 */
2796 if (msg_col == 0)
2797 msg_didout = FALSE;
2798 msg_start();
2799 MSG_PUTS(":!");
2800 msg_outtrans(cmd); /* show what we are doing */
2801
2802 /* let the shell know if we are redirecting output or not */
2803 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2804
2805#ifdef AMIGA
2806 out_flush();
2807 /* read window status report and redraw before message */
2808 (void)char_avail();
2809#endif
2810
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002811 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00002812 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
2813 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002814 && eap->cmdidx != CMD_lgrepadd),
2815 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002816 if (wp != NULL)
2817 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002818#ifdef FEAT_AUTOCMD
2819 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002820 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002821 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2822 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002823 if (qi->qf_curlist < qi->qf_listcount)
2824 res = qi->qf_lists[qi->qf_curlist].qf_count;
2825 else
2826 res = 0;
2827 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002828#endif
2829 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002830 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831
Bram Moolenaar7c626922005-02-07 22:01:03 +00002832 mch_remove(fname);
2833 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 vim_free(cmd);
2835}
2836
2837/*
2838 * Return the name for the errorfile, in allocated memory.
2839 * Find a new unique name when 'makeef' contains "##".
2840 * Returns NULL for error.
2841 */
2842 static char_u *
2843get_mef_name()
2844{
2845 char_u *p;
2846 char_u *name;
2847 static int start = -1;
2848 static int off = 0;
2849#ifdef HAVE_LSTAT
2850 struct stat sb;
2851#endif
2852
2853 if (*p_mef == NUL)
2854 {
2855 name = vim_tempname('e');
2856 if (name == NULL)
2857 EMSG(_(e_notmp));
2858 return name;
2859 }
2860
2861 for (p = p_mef; *p; ++p)
2862 if (p[0] == '#' && p[1] == '#')
2863 break;
2864
2865 if (*p == NUL)
2866 return vim_strsave(p_mef);
2867
2868 /* Keep trying until the name doesn't exist yet. */
2869 for (;;)
2870 {
2871 if (start == -1)
2872 start = mch_get_pid();
2873 else
2874 off += 19;
2875
2876 name = alloc((unsigned)STRLEN(p_mef) + 30);
2877 if (name == NULL)
2878 break;
2879 STRCPY(name, p_mef);
2880 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2881 STRCAT(name, p + 2);
2882 if (mch_getperm(name) < 0
2883#ifdef HAVE_LSTAT
2884 /* Don't accept a symbolic link, its a security risk. */
2885 && mch_lstat((char *)name, &sb) < 0
2886#endif
2887 )
2888 break;
2889 vim_free(name);
2890 }
2891 return name;
2892}
2893
2894/*
2895 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002896 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 */
2898 void
2899ex_cc(eap)
2900 exarg_T *eap;
2901{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002902 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002903
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002904 if (eap->cmdidx == CMD_ll
2905 || eap->cmdidx == CMD_lrewind
2906 || eap->cmdidx == CMD_lfirst
2907 || eap->cmdidx == CMD_llast)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002908 {
2909 qi = GET_LOC_LIST(curwin);
2910 if (qi == NULL)
2911 {
2912 EMSG(_(e_loclist));
2913 return;
2914 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002915 }
2916
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002917 qf_jump(qi, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 eap->addr_count > 0
2919 ? (int)eap->line2
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002920 : (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 ? 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002922 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
2923 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 ? 1
2925 : 32767,
2926 eap->forceit);
2927}
2928
2929/*
2930 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002931 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 */
2933 void
2934ex_cnext(eap)
2935 exarg_T *eap;
2936{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002937 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002938
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002939 if (eap->cmdidx == CMD_lnext
2940 || eap->cmdidx == CMD_lNext
2941 || eap->cmdidx == CMD_lprevious
2942 || eap->cmdidx == CMD_lnfile
2943 || eap->cmdidx == CMD_lNfile
2944 || eap->cmdidx == CMD_lpfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002945 {
2946 qi = GET_LOC_LIST(curwin);
2947 if (qi == NULL)
2948 {
2949 EMSG(_(e_loclist));
2950 return;
2951 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002952 }
2953
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002954 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 ? FORWARD
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002956 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002958 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
2959 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 ? BACKWARD_FILE
2961 : BACKWARD,
2962 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
2963}
2964
2965/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002966 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002967 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 */
2969 void
2970ex_cfile(eap)
2971 exarg_T *eap;
2972{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002973 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002974 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002975
2976 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
2977 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002978 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002979
Bram Moolenaar9028b102010-07-11 16:58:51 +02002980#ifdef FEAT_BROWSE
2981 if (cmdmod.browse)
2982 {
2983 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
2984 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
2985 if (browse_file == NULL)
2986 return;
2987 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
2988 vim_free(browse_file);
2989 }
2990 else
2991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002993 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002994
2995 /*
2996 * This function is used by the :cfile, :cgetfile and :caddfile
2997 * commands.
2998 * :cfile always creates a new quickfix list and jumps to the
2999 * first error.
3000 * :cgetfile creates a new quickfix list but doesn't jump to the
3001 * first error.
3002 * :caddfile adds to an existing quickfix list. If there is no
3003 * quickfix list then a new list is created.
3004 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003005 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003006 && eap->cmdidx != CMD_laddfile),
3007 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003008 && (eap->cmdidx == CMD_cfile
3009 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003010 {
3011 if (wp != NULL)
3012 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003013 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015}
3016
3017/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003018 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003019 * ":vimgrepadd {pattern} file(s)"
3020 * ":lvimgrep {pattern} file(s)"
3021 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003022 */
3023 void
3024ex_vimgrep(eap)
3025 exarg_T *eap;
3026{
Bram Moolenaar81695252004-12-29 20:58:21 +00003027 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003028 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003029 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003030 char_u *fname;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003031 char_u *s;
3032 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003033 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003034 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003035 qfline_T *prevp = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003036 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003037 buf_T *buf;
3038 int duplicate_name = FALSE;
3039 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003040 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003041 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003042 buf_T *first_match_buf = NULL;
3043 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003044 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003045#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3046 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003047#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003048 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003049 int flags = 0;
3050 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003051 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003052 char_u *dirname_start = NULL;
3053 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003054 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003055#ifdef FEAT_AUTOCMD
3056 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003057
3058 switch (eap->cmdidx)
3059 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003060 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3061 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3062 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003063 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003064 case CMD_grep: au_name = (char_u *)"grep"; break;
3065 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3066 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3067 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003068 default: break;
3069 }
3070 if (au_name != NULL)
3071 {
3072 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3073 curbuf->b_fname, TRUE, curbuf);
3074 if (did_throw || force_abort)
3075 return;
3076 }
3077#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003078
Bram Moolenaar754b5602006-02-09 23:53:20 +00003079 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003080 || eap->cmdidx == CMD_lvimgrep
3081 || eap->cmdidx == CMD_lgrepadd
3082 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003083 {
3084 qi = ll_get_or_alloc_list(curwin);
3085 if (qi == NULL)
3086 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003087 }
3088
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003089 if (eap->addr_count > 0)
3090 tomatch = eap->line2;
3091 else
3092 tomatch = MAXLNUM;
3093
Bram Moolenaar81695252004-12-29 20:58:21 +00003094 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003095 regmatch.regprog = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003096 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003097 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003098 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003099 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003100 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003101 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003102 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003103 if (regmatch.regprog == NULL)
3104 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003105 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003106 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003107
3108 p = skipwhite(p);
3109 if (*p == NUL)
3110 {
3111 EMSG(_("E683: File name missing or invalid pattern"));
3112 goto theend;
3113 }
3114
Bram Moolenaar754b5602006-02-09 23:53:20 +00003115 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003116 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003117 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003118 /* make place for a new list */
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003119 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003120 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003121 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003122 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003123 prevp->qf_next != prevp; prevp = prevp->qf_next)
3124 ;
3125
3126 /* parse the list of arguments */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003127 if (get_arglist_exp(p, &fcount, &fnames) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003128 goto theend;
3129 if (fcount == 0)
3130 {
3131 EMSG(_(e_nomatch));
3132 goto theend;
3133 }
3134
Bram Moolenaard9462e32011-04-11 21:35:11 +02003135 dirname_start = alloc(MAXPATHL);
3136 dirname_now = alloc(MAXPATHL);
3137 if (dirname_start == NULL || dirname_now == NULL)
3138 goto theend;
3139
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003140 /* Remember the current directory, because a BufRead autocommand that does
3141 * ":lcd %:p:h" changes the meaning of short path names. */
3142 mch_dirname(dirname_start, MAXPATHL);
3143
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003144 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003145 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003146 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003147 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003148 if (time(NULL) > seconds)
3149 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003150 /* Display the file name every second or so, show the user we are
3151 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003152 seconds = time(NULL);
3153 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003154 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003155 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003156 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003157 else
3158 {
3159 msg_outtrans(p);
3160 vim_free(p);
3161 }
3162 msg_clr_eos();
3163 msg_didout = FALSE; /* overwrite this message */
3164 msg_nowait = TRUE; /* don't wait for this message */
3165 msg_col = 0;
3166 out_flush();
3167 }
3168
Bram Moolenaar81695252004-12-29 20:58:21 +00003169 buf = buflist_findname_exp(fnames[fi]);
3170 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3171 {
3172 /* Remember that a buffer with this name already exists. */
3173 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003174 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003175 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003176
3177#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3178 /* Don't do Filetype autocommands to avoid loading syntax and
3179 * indent scripts, a great speed improvement. */
3180 save_ei = au_event_disable(",Filetype");
3181#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003182 /* Don't use modelines here, it's useless. */
3183 save_mls = p_mls;
3184 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00003185
3186 /* Load file into a buffer, so that 'fileencoding' is detected,
3187 * autocommands applied, etc. */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003188 buf = load_dummy_buffer(fname);
3189
3190 /* When autocommands changed directory: go back. We assume it was
3191 * ":lcd %:p:h". */
3192 mch_dirname(dirname_now, MAXPATHL);
3193 if (STRCMP(dirname_start, dirname_now) != 0)
3194 {
3195 exarg_T ea;
3196
3197 ea.arg = dirname_start;
3198 ea.cmdidx = CMD_lcd;
3199 ex_cd(&ea);
3200 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003201
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003202 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003203#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3204 au_event_restore(save_ei);
3205#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003206 }
3207 else
3208 /* Use existing, loaded buffer. */
3209 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003210
Bram Moolenaar81695252004-12-29 20:58:21 +00003211 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003212 {
3213 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003214 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003215 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003216 else
3217 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003218 /* Try for a match in all lines of the buffer.
3219 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003220 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003221 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3222 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003223 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003224 col = 0;
3225 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003226 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003227 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003228 ;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003229 if (qf_add_entry(qi, &prevp,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003230 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003231 fname,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003232 0,
Bram Moolenaar81695252004-12-29 20:58:21 +00003233 ml_get_buf(buf,
3234 regmatch.startpos[0].lnum + lnum, FALSE),
3235 regmatch.startpos[0].lnum + lnum,
3236 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003237 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003238 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003239 0, /* nr */
3240 0, /* type */
3241 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003242 ) == FAIL)
3243 {
3244 got_int = TRUE;
3245 break;
3246 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003247 found_match = TRUE;
3248 if (--tomatch == 0)
3249 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003250 if ((flags & VGR_GLOBAL) == 0
3251 || regmatch.endpos[0].lnum > 0)
3252 break;
3253 col = regmatch.endpos[0].col
3254 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003255 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00003256 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003257 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003258 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003259 if (got_int)
3260 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003261 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003262
3263 if (using_dummy)
3264 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003265 if (found_match && first_match_buf == NULL)
3266 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003267 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003268 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003269 /* Never keep a dummy buffer if there is another buffer
3270 * with the same name. */
3271 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003272 buf = NULL;
3273 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00003274 else if (!cmdmod.hide
3275 || buf->b_p_bh[0] == 'u' /* "unload" */
3276 || buf->b_p_bh[0] == 'w' /* "wipe" */
3277 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00003278 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003279 /* When no match was found we don't need to remember the
3280 * buffer, wipe it out. If there was a match and it
3281 * wasn't the first one or we won't jump there: only
3282 * unload the buffer.
3283 * Ignore 'hidden' here, because it may lead to having too
3284 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003285 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003286 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003287 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003288 buf = NULL;
3289 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003290 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003291 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003292 unload_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003293 buf = NULL;
3294 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003295 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003296
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003297 if (buf != NULL)
3298 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003299 /* If the buffer is still loaded we need to use the
3300 * directory we jumped to below. */
3301 if (buf == first_match_buf
3302 && target_dir == NULL
3303 && STRCMP(dirname_start, dirname_now) != 0)
3304 target_dir = vim_strsave(dirname_now);
3305
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003306 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003307 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00003308 * need to be done (again). But not the window-local
3309 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003310 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003311#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003312 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3313 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003314#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00003315 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003316 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003317 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003318 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003319 }
3320 }
3321
3322 FreeWild(fcount, fnames);
3323
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003324 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3325 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3326 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003327
3328#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003329 qf_update_buffer(qi);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003330#endif
3331
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003332#ifdef FEAT_AUTOCMD
3333 if (au_name != NULL)
3334 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3335 curbuf->b_fname, TRUE, curbuf);
3336#endif
3337
Bram Moolenaar86b68352004-12-27 21:59:20 +00003338 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003339 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003340 {
3341 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003342 {
3343 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003344 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003345 if (buf != curbuf)
3346 /* If we jumped to another buffer redrawing will already be
3347 * taken care of. */
3348 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003349
3350 /* Jump to the directory used after loading the buffer. */
3351 if (curbuf == first_match_buf && target_dir != NULL)
3352 {
3353 exarg_T ea;
3354
3355 ea.arg = target_dir;
3356 ea.cmdidx = CMD_lcd;
3357 ex_cd(&ea);
3358 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003359 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003360 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003361 else
3362 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003363
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003364 /* If we loaded a dummy buffer into the current window, the autocommands
3365 * may have messed up things, need to redraw and recompute folds. */
3366 if (redraw_for_dummy)
3367 {
3368#ifdef FEAT_FOLDING
3369 foldUpdateAll(curwin);
3370#else
3371 redraw_later(NOT_VALID);
3372#endif
3373 }
3374
Bram Moolenaar86b68352004-12-27 21:59:20 +00003375theend:
Bram Moolenaard9462e32011-04-11 21:35:11 +02003376 vim_free(dirname_now);
3377 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003378 vim_free(target_dir);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003379 vim_free(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003380}
3381
3382/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00003383 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00003384 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003385 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3386 * If "s" is not NULL terminate the pattern with a NUL.
3387 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00003388 */
3389 char_u *
Bram Moolenaar05159a02005-02-26 23:04:13 +00003390skip_vimgrep_pat(p, s, flags)
3391 char_u *p;
3392 char_u **s;
3393 int *flags;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003394{
3395 int c;
3396
3397 if (vim_isIDc(*p))
3398 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003399 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003400 if (s != NULL)
3401 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003402 p = skiptowhite(p);
3403 if (s != NULL && *p != NUL)
3404 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003405 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003406 else
3407 {
3408 /* ":vimgrep /pattern/[g][j] fname" */
3409 if (s != NULL)
3410 *s = p + 1;
3411 c = *p;
3412 p = skip_regexp(p + 1, c, TRUE, NULL);
3413 if (*p != c)
3414 return NULL;
3415
3416 /* Truncate the pattern. */
3417 if (s != NULL)
3418 *p = NUL;
3419 ++p;
3420
3421 /* Find the flags */
3422 while (*p == 'g' || *p == 'j')
3423 {
3424 if (flags != NULL)
3425 {
3426 if (*p == 'g')
3427 *flags |= VGR_GLOBAL;
3428 else
3429 *flags |= VGR_NOJUMP;
3430 }
3431 ++p;
3432 }
3433 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00003434 return p;
3435}
3436
3437/*
Bram Moolenaar81695252004-12-29 20:58:21 +00003438 * Load file "fname" into a dummy buffer and return the buffer pointer.
3439 * Returns NULL if it fails.
3440 * Must call unload_dummy_buffer() or wipe_dummy_buffer() later!
3441 */
3442 static buf_T *
3443load_dummy_buffer(fname)
3444 char_u *fname;
3445{
3446 buf_T *newbuf;
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003447 buf_T *newbuf_to_wipe = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00003448 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003449 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00003450
3451 /* Allocate a buffer without putting it in the buffer list. */
3452 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
3453 if (newbuf == NULL)
3454 return NULL;
3455
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00003456 /* Init the options. */
3457 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
3458
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003459 /* need to open the memfile before putting the buffer in a window */
3460 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00003461 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003462 /* set curwin/curbuf to buf and save a few things */
3463 aucmd_prepbuf(&aco, newbuf);
3464
3465 /* Need to set the filename for autocommands. */
3466 (void)setfname(curbuf, fname, NULL, FALSE);
3467
Bram Moolenaar81695252004-12-29 20:58:21 +00003468 /* Create swap file now to avoid the ATTENTION message. */
3469 check_need_swap(TRUE);
3470
3471 /* Remove the "dummy" flag, otherwise autocommands may not
3472 * work. */
3473 curbuf->b_flags &= ~BF_DUMMY;
3474
3475 if (readfile(fname, NULL,
3476 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
3477 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00003478 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00003479 && !(curbuf->b_flags & BF_NEW))
3480 {
3481 failed = FALSE;
3482 if (curbuf != newbuf)
3483 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003484 /* Bloody autocommands changed the buffer! Can happen when
3485 * using netrw and editing a remote file. Use the current
3486 * buffer instead, delete the dummy one after restoring the
3487 * window stuff. */
3488 newbuf_to_wipe = newbuf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003489 newbuf = curbuf;
3490 }
3491 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003492
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003493 /* restore curwin/curbuf and a few other things */
3494 aucmd_restbuf(&aco);
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003495 if (newbuf_to_wipe != NULL && buf_valid(newbuf_to_wipe))
3496 wipe_buffer(newbuf_to_wipe, FALSE);
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003497 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003498
3499 if (!buf_valid(newbuf))
3500 return NULL;
3501 if (failed)
3502 {
3503 wipe_dummy_buffer(newbuf);
3504 return NULL;
3505 }
3506 return newbuf;
3507}
3508
3509/*
3510 * Wipe out the dummy buffer that load_dummy_buffer() created.
3511 */
3512 static void
3513wipe_dummy_buffer(buf)
3514 buf_T *buf;
3515{
3516 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003517 {
3518#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3519 cleanup_T cs;
3520
3521 /* Reset the error/interrupt/exception state here so that aborting()
3522 * returns FALSE when wiping out the buffer. Otherwise it doesn't
3523 * work when got_int is set. */
3524 enter_cleanup(&cs);
3525#endif
3526
Bram Moolenaar81695252004-12-29 20:58:21 +00003527 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00003528
3529#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3530 /* Restore the error/interrupt/exception state if not discarded by a
3531 * new aborting error, interrupt, or uncaught exception. */
3532 leave_cleanup(&cs);
3533#endif
3534 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003535}
3536
3537/*
3538 * Unload the dummy buffer that load_dummy_buffer() created.
3539 */
3540 static void
3541unload_dummy_buffer(buf)
3542 buf_T *buf;
3543{
3544 if (curbuf != buf) /* safety check */
3545 close_buffer(NULL, buf, DOBUF_UNLOAD);
3546}
3547
Bram Moolenaar05159a02005-02-26 23:04:13 +00003548#if defined(FEAT_EVAL) || defined(PROTO)
3549/*
3550 * Add each quickfix error to list "list" as a dictionary.
3551 */
3552 int
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003553get_errorlist(wp, list)
3554 win_T *wp;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003555 list_T *list;
3556{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003557 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003558 dict_T *dict;
3559 char_u buf[2];
3560 qfline_T *qfp;
3561 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003562 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003563
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003564 if (wp != NULL)
3565 {
3566 qi = GET_LOC_LIST(wp);
3567 if (qi == NULL)
3568 return FAIL;
3569 }
3570
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003571 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003572 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003573 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003574
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003575 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3576 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003577 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003578 /* Handle entries with a non-existing buffer number. */
3579 bufnum = qfp->qf_fnum;
3580 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3581 bufnum = 0;
3582
Bram Moolenaar05159a02005-02-26 23:04:13 +00003583 if ((dict = dict_alloc()) == NULL)
3584 return FAIL;
3585 if (list_append_dict(list, dict) == FAIL)
3586 return FAIL;
3587
3588 buf[0] = qfp->qf_type;
3589 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003590 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003591 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
3592 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
3593 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
3594 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00003595 || dict_add_nr_str(dict, "pattern", 0L,
3596 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
3597 || dict_add_nr_str(dict, "text", 0L,
3598 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003599 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
3600 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
3601 return FAIL;
3602
3603 qfp = qfp->qf_next;
3604 }
3605 return OK;
3606}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003607
3608/*
3609 * Populate the quickfix list with the items supplied in the list
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003610 * of dictionaries. "title" will be copied to w:quickfix_title
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003611 */
3612 int
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003613set_errorlist(wp, list, action, title)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003614 win_T *wp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003615 list_T *list;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003616 int action;
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003617 char_u *title;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003618{
3619 listitem_T *li;
3620 dict_T *d;
3621 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003622 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003623 long lnum;
3624 int col, nr;
3625 int vcol;
3626 qfline_T *prevp = NULL;
3627 int valid, status;
3628 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003629 qf_info_T *qi = &ql_info;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003630 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003631
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003632 if (wp != NULL)
3633 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00003634 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003635 if (qi == NULL)
3636 return FAIL;
3637 }
3638
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003639 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003640 /* make place for a new list */
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003641 qf_new_list(qi, title);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003642 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003643 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003644 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003645 prevp->qf_next != prevp; prevp = prevp->qf_next)
3646 ;
3647 else if (action == 'r')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003648 qf_free(qi, qi->qf_curlist);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003649
3650 for (li = list->lv_first; li != NULL; li = li->li_next)
3651 {
3652 if (li->li_tv.v_type != VAR_DICT)
3653 continue; /* Skip non-dict items */
3654
3655 d = li->li_tv.vval.v_dict;
3656 if (d == NULL)
3657 continue;
3658
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003659 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003660 bufnum = get_dict_number(d, (char_u *)"bufnr");
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003661 lnum = get_dict_number(d, (char_u *)"lnum");
3662 col = get_dict_number(d, (char_u *)"col");
3663 vcol = get_dict_number(d, (char_u *)"vcol");
3664 nr = get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003665 type = get_dict_string(d, (char_u *)"type", TRUE);
3666 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
3667 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003668 if (text == NULL)
3669 text = vim_strsave((char_u *)"");
3670
3671 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003672 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003673 valid = FALSE;
3674
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003675 /* Mark entries with non-existing buffer number as not valid. Give the
3676 * error message only once. */
3677 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3678 {
3679 if (!did_bufnr_emsg)
3680 {
3681 did_bufnr_emsg = TRUE;
3682 EMSGN(_("E92: Buffer %ld not found"), bufnum);
3683 }
3684 valid = FALSE;
3685 bufnum = 0;
3686 }
3687
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003688 status = qf_add_entry(qi, &prevp,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003689 NULL, /* dir */
3690 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003691 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003692 text,
3693 lnum,
3694 col,
3695 vcol, /* vis_col */
3696 pattern, /* search pattern */
3697 nr,
3698 type == NULL ? NUL : *type,
3699 valid);
3700
3701 vim_free(filename);
3702 vim_free(pattern);
3703 vim_free(text);
3704 vim_free(type);
3705
3706 if (status == FAIL)
3707 {
3708 retval = FAIL;
3709 break;
3710 }
3711 }
3712
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02003713 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02003714 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02003715 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
3716 else
3717 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003718 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3719 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003720
3721#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003722 qf_update_buffer(qi);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003723#endif
3724
3725 return retval;
3726}
Bram Moolenaar05159a02005-02-26 23:04:13 +00003727#endif
3728
Bram Moolenaar81695252004-12-29 20:58:21 +00003729/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003730 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003731 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003732 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003733 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003734 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003735 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00003736 */
3737 void
3738ex_cbuffer(eap)
3739 exarg_T *eap;
3740{
3741 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003742 qf_info_T *qi = &ql_info;
3743
Bram Moolenaardb552d602006-03-23 22:59:57 +00003744 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
3745 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003746 {
3747 qi = ll_get_or_alloc_list(curwin);
3748 if (qi == NULL)
3749 return;
3750 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003751
3752 if (*eap->arg == NUL)
3753 buf = curbuf;
3754 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
3755 buf = buflist_findnr(atoi((char *)eap->arg));
3756 if (buf == NULL)
3757 EMSG(_(e_invarg));
3758 else if (buf->b_ml.ml_mfp == NULL)
3759 EMSG(_("E681: Buffer is not loaded"));
3760 else
3761 {
3762 if (eap->addr_count == 0)
3763 {
3764 eap->line1 = 1;
3765 eap->line2 = buf->b_ml.ml_line_count;
3766 }
3767 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
3768 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
3769 EMSG(_(e_invrange));
3770 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00003771 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003772 char_u *qf_title = *eap->cmdlinep;
3773
3774 if (buf->b_sfname)
3775 {
3776 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
3777 (char *)qf_title, (char *)buf->b_sfname);
3778 qf_title = IObuff;
3779 }
3780
Bram Moolenaardb552d602006-03-23 22:59:57 +00003781 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
3782 (eap->cmdidx != CMD_caddbuffer
3783 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003784 eap->line1, eap->line2,
3785 qf_title) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00003786 && (eap->cmdidx == CMD_cbuffer
3787 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00003788 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
3789 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003790 }
3791}
3792
Bram Moolenaar1e015462005-09-25 22:16:38 +00003793#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003794/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00003795 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
3796 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003797 */
3798 void
3799ex_cexpr(eap)
3800 exarg_T *eap;
3801{
3802 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003803 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003804
Bram Moolenaardb552d602006-03-23 22:59:57 +00003805 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
3806 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003807 {
3808 qi = ll_get_or_alloc_list(curwin);
3809 if (qi == NULL)
3810 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003811 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003812
Bram Moolenaar4770d092006-01-12 23:22:24 +00003813 /* Evaluate the expression. When the result is a string or a list we can
3814 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003815 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003816 if (tv != NULL)
3817 {
3818 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
3819 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
3820 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00003821 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
3822 (eap->cmdidx != CMD_caddexpr
3823 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003824 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00003825 && (eap->cmdidx == CMD_cexpr
3826 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003827 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003828 }
3829 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003830 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00003831 free_tv(tv);
3832 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003833}
Bram Moolenaar1e015462005-09-25 22:16:38 +00003834#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003835
3836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 * ":helpgrep {pattern}"
3838 */
3839 void
3840ex_helpgrep(eap)
3841 exarg_T *eap;
3842{
3843 regmatch_T regmatch;
3844 char_u *save_cpo;
3845 char_u *p;
3846 int fcount;
3847 char_u **fnames;
3848 FILE *fd;
3849 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003850 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003852#ifdef FEAT_MULTI_LANG
3853 char_u *lang;
3854#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003855 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003856 int new_qi = FALSE;
3857 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858
3859 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
3860 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00003861 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003863#ifdef FEAT_MULTI_LANG
3864 /* Check for a specified language */
3865 lang = check_help_lang(eap->arg);
3866#endif
3867
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003868 if (eap->cmdidx == CMD_lhelpgrep)
3869 {
3870 /* Find an existing help window */
3871 FOR_ALL_WINDOWS(wp)
3872 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
3873 break;
3874
3875 if (wp == NULL) /* Help window not found */
3876 qi = NULL;
3877 else
3878 qi = wp->w_llist;
3879
3880 if (qi == NULL)
3881 {
3882 /* Allocate a new location list for help text matches */
3883 if ((qi = ll_new_list()) == NULL)
3884 return;
3885 new_qi = TRUE;
3886 }
3887 }
3888
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
3890 regmatch.rm_ic = FALSE;
3891 if (regmatch.regprog != NULL)
3892 {
3893 /* create a new quickfix list */
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003894 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895
3896 /* Go through all directories in 'runtimepath' */
3897 p = p_rtp;
3898 while (*p != NUL && !got_int)
3899 {
3900 copy_option_part(&p, NameBuff, MAXPATHL, ",");
3901
3902 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
3903 add_pathsep(NameBuff);
3904 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
3905 if (gen_expand_wildcards(1, &NameBuff, &fcount,
3906 &fnames, EW_FILE|EW_SILENT) == OK
3907 && fcount > 0)
3908 {
3909 for (fi = 0; fi < fcount && !got_int; ++fi)
3910 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003911#ifdef FEAT_MULTI_LANG
3912 /* Skip files for a different language. */
3913 if (lang != NULL
3914 && STRNICMP(lang, fnames[fi]
3915 + STRLEN(fnames[fi]) - 3, 2) != 0
3916 && !(STRNICMP(lang, "en", 2) == 0
3917 && STRNICMP("txt", fnames[fi]
3918 + STRLEN(fnames[fi]) - 3, 3) == 0))
3919 continue;
3920#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003921 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 if (fd != NULL)
3923 {
3924 lnum = 1;
3925 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
3926 {
3927 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
3928 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003929 int l = (int)STRLEN(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930
3931 /* remove trailing CR, LF, spaces, etc. */
3932 while (l > 0 && IObuff[l - 1] <= ' ')
3933 IObuff[--l] = NUL;
3934
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003935 if (qf_add_entry(qi, &prevp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 NULL, /* dir */
3937 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003938 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 IObuff,
3940 lnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00003941 (int)(regmatch.startp[0] - IObuff)
3942 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00003943 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003944 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 0, /* nr */
3946 1, /* type */
3947 TRUE /* valid */
3948 ) == FAIL)
3949 {
3950 got_int = TRUE;
3951 break;
3952 }
3953 }
3954 ++lnum;
3955 line_breakcheck();
3956 }
3957 fclose(fd);
3958 }
3959 }
3960 FreeWild(fcount, fnames);
3961 }
3962 }
3963 vim_free(regmatch.regprog);
3964
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003965 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3966 qi->qf_lists[qi->qf_curlist].qf_ptr =
3967 qi->qf_lists[qi->qf_curlist].qf_start;
3968 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 }
3970
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00003971 if (p_cpo == empty_option)
3972 p_cpo = save_cpo;
3973 else
3974 /* Darn, some plugin changed the value. */
3975 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976
3977#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003978 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979#endif
3980
3981 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003982 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003983 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003984 else
3985 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003986
3987 if (eap->cmdidx == CMD_lhelpgrep)
3988 {
3989 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00003990 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003991 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
3992 {
3993 if (new_qi)
3994 ll_free_all(&qi);
3995 }
3996 else if (curwin->w_llist == NULL)
3997 curwin->w_llist = qi;
3998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999}
4000
4001#endif /* FEAT_QUICKFIX */