blob: 5f69a925b7fab7f3d19888ca7485e6dccd022efe [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 Moolenaard12f5c12006-01-25 22:10:52 +000059} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000060
Bram Moolenaard12f5c12006-01-25 22:10:52 +000061struct qf_info_S
62{
63 /*
64 * Count of references to this list. Used only for location lists.
65 * When a location list window reference this list, qf_refcount
66 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
67 * reaches 0, the list is freed.
68 */
69 int qf_refcount;
70 int qf_listcount; /* current number of lists */
71 int qf_curlist; /* current error list */
72 qf_list_T qf_lists[LISTCOUNT];
73};
74
75static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaar071d4272004-06-13 20:20:40 +000076
Bram Moolenaar68b76a62005-03-25 21:53:48 +000077#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +000078
79/*
80 * Structure used to hold the info of one part of 'errorformat'
81 */
Bram Moolenaar01265852006-03-20 21:50:15 +000082typedef struct efm_S efm_T;
83struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000084{
85 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +000086 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
88 char_u prefix; /* prefix of this format line: */
89 /* 'D' enter directory */
90 /* 'X' leave directory */
91 /* 'A' start of multi-line message */
92 /* 'E' error message */
93 /* 'W' warning message */
94 /* 'I' informational message */
95 /* 'C' continuation line */
96 /* 'Z' end of multi-line message */
97 /* 'G' general, unspecific message */
98 /* 'P' push file (partial) message */
99 /* 'Q' pop/quit file (partial) message */
100 /* 'O' overread (partial) message */
101 char_u flags; /* additional flags given in prefix */
102 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000103 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000104 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105};
106
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000107static 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));
108static void qf_new_list __ARGS((qf_info_T *qi));
Bram Moolenaard9ff7d52008-03-20 12:23:49 +0000109static void ll_free_all __ARGS((qf_info_T **pqi));
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000110static 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 +0000111static qf_info_T *ll_new_list __ARGS((void));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000112static void qf_msg __ARGS((qf_info_T *qi));
113static void qf_free __ARGS((qf_info_T *qi, int idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114static char_u *qf_types __ARGS((int, int));
115static int qf_get_fnum __ARGS((char_u *, char_u *));
116static char_u *qf_push_dir __ARGS((char_u *, struct dir_stack_T **));
117static char_u *qf_pop_dir __ARGS((struct dir_stack_T **));
118static char_u *qf_guess_filepath __ARGS((char_u *));
119static void qf_fmt_text __ARGS((char_u *text, char_u *buf, int bufsize));
120static void qf_clean_dir_stack __ARGS((struct dir_stack_T **));
121#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000122static int qf_win_pos_update __ARGS((qf_info_T *qi, int old_qf_index));
Bram Moolenaar9c102382006-05-03 21:26:49 +0000123static int is_qf_win __ARGS((win_T *win, qf_info_T *qi));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000124static win_T *qf_find_win __ARGS((qf_info_T *qi));
125static buf_T *qf_find_buf __ARGS((qf_info_T *qi));
126static void qf_update_buffer __ARGS((qf_info_T *qi));
127static void qf_fill_buffer __ARGS((qf_info_T *qi));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#endif
129static char_u *get_mef_name __ARGS((void));
Bram Moolenaar81695252004-12-29 20:58:21 +0000130static buf_T *load_dummy_buffer __ARGS((char_u *fname));
131static void wipe_dummy_buffer __ARGS((buf_T *buf));
132static void unload_dummy_buffer __ARGS((buf_T *buf));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000133static qf_info_T *ll_get_or_alloc_list __ARGS((win_T *));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000135/* Quickfix window check helper macro */
136#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
137/* Location list window check helper macro */
138#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
139/*
140 * Return location list for window 'wp'
141 * For location list window, return the referenced location list
142 */
143#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
144
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000146 * Read the errorfile "efile" into memory, line by line, building the error
147 * list.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 * Return -1 for error, number of errors for success.
149 */
150 int
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000151qf_init(wp, efile, errorformat, newlist)
152 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153 char_u *efile;
154 char_u *errorformat;
155 int newlist; /* TRUE: start a new error list */
156{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000157 qf_info_T *qi = &ql_info;
158
Bram Moolenaar86b68352004-12-27 21:59:20 +0000159 if (efile == NULL)
160 return FAIL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000161
162 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000163 {
164 qi = ll_get_or_alloc_list(wp);
165 if (qi == NULL)
166 return FAIL;
167 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000168
169 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar86b68352004-12-27 21:59:20 +0000170 (linenr_T)0, (linenr_T)0);
171}
172
173/*
174 * Read the errorfile "efile" into memory, line by line, building the error
175 * list.
176 * Alternative: when "efile" is null read errors from buffer "buf".
177 * Always use 'errorformat' from "buf" if there is a local value.
178 * Then lnumfirst and lnumlast specify the range of lines to use.
179 * Return -1 for error, number of errors for success.
180 */
181 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000182qf_init_ext(qi, efile, buf, tv, errorformat, newlist, lnumfirst, lnumlast)
183 qf_info_T *qi;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000184 char_u *efile;
185 buf_T *buf;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000186 typval_T *tv;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000187 char_u *errorformat;
188 int newlist; /* TRUE: start a new error list */
189 linenr_T lnumfirst; /* first line number to use */
190 linenr_T lnumlast; /* last line number to use */
191{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 char_u *namebuf;
193 char_u *errmsg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000194 char_u *pattern;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 char_u *fmtstr = NULL;
196 int col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000197 char_u use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 int type = 0;
199 int valid;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000200 linenr_T buflnum = lnumfirst;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 long lnum = 0L;
202 int enr = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000203 FILE *fd = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000204 qfline_T *qfprev = NULL; /* init to make SASC shut up */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 char_u *efmp;
Bram Moolenaar01265852006-03-20 21:50:15 +0000206 efm_T *fmt_first = NULL;
207 efm_T *fmt_last = NULL;
208 efm_T *fmt_ptr;
209 efm_T *fmt_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 char_u *efm;
211 char_u *ptr;
212 char_u *srcptr;
213 int len;
214 int i;
215 int round;
216 int idx = 0;
217 int multiline = FALSE;
218 int multiignore = FALSE;
219 int multiscan = FALSE;
220 int retval = -1; /* default: return error flag */
221 char_u *directory = NULL;
222 char_u *currfile = NULL;
223 char_u *tail = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000224 char_u *p_str = NULL;
225 listitem_T *p_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 struct dir_stack_T *file_stack = NULL;
227 regmatch_T regmatch;
228 static struct fmtpattern
229 {
230 char_u convchar;
231 char *pattern;
232 } fmt_pat[FMT_PATTERNS] =
233 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000234 {'f', ".\\+"}, /* only used when at end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 {'n', "\\d\\+"},
236 {'l', "\\d\\+"},
237 {'c', "\\d\\+"},
238 {'t', "."},
239 {'m', ".\\+"},
240 {'r', ".*"},
241 {'p', "[- .]*"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000242 {'v', "\\d\\+"},
243 {'s', ".\\+"}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 };
245
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 namebuf = alloc(CMDBUFFSIZE + 1);
247 errmsg = alloc(CMDBUFFSIZE + 1);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000248 pattern = alloc(CMDBUFFSIZE + 1);
249 if (namebuf == NULL || errmsg == NULL || pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 goto qf_init_end;
251
Bram Moolenaar86b68352004-12-27 21:59:20 +0000252 if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 {
254 EMSG2(_(e_openerrf), efile);
255 goto qf_init_end;
256 }
257
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000258 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 /* make place for a new list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000260 qf_new_list(qi);
261 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000263 for (qfprev = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 qfprev->qf_next != qfprev; qfprev = qfprev->qf_next)
265 ;
266
267/*
268 * Each part of the format string is copied and modified from errorformat to
269 * regex prog. Only a few % characters are allowed.
270 */
271 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000272 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000273 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 else
275 efm = errorformat;
276 /*
277 * Get some space to modify the format string into.
278 */
279 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
280 for (round = FMT_PATTERNS; round > 0; )
281 i += (int)STRLEN(fmt_pat[--round].pattern);
282#ifdef COLON_IN_FILENAME
283 i += 12; /* "%f" can become twelve chars longer */
284#else
285 i += 2; /* "%f" can become two chars longer */
286#endif
287 if ((fmtstr = alloc(i)) == NULL)
288 goto error2;
289
Bram Moolenaar01265852006-03-20 21:50:15 +0000290 while (efm[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 {
292 /*
293 * Allocate a new eformat structure and put it at the end of the list
294 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000295 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 if (fmt_ptr == NULL)
297 goto error2;
298 if (fmt_first == NULL) /* first one */
299 fmt_first = fmt_ptr;
300 else
301 fmt_last->next = fmt_ptr;
302 fmt_last = fmt_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303
304 /*
305 * Isolate one part in the 'errorformat' option
306 */
307 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
308 if (efm[len] == '\\' && efm[len + 1] != NUL)
309 ++len;
310
311 /*
312 * Build regexp pattern from current 'errorformat' option
313 */
314 ptr = fmtstr;
315 *ptr++ = '^';
Bram Moolenaar01265852006-03-20 21:50:15 +0000316 round = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 for (efmp = efm; efmp < efm + len; ++efmp)
318 {
319 if (*efmp == '%')
320 {
321 ++efmp;
322 for (idx = 0; idx < FMT_PATTERNS; ++idx)
323 if (fmt_pat[idx].convchar == *efmp)
324 break;
325 if (idx < FMT_PATTERNS)
326 {
327 if (fmt_ptr->addr[idx])
328 {
329 sprintf((char *)errmsg,
330 _("E372: Too many %%%c in format string"), *efmp);
331 EMSG(errmsg);
332 goto error2;
333 }
334 if ((idx
335 && idx < 6
336 && vim_strchr((char_u *)"DXOPQ",
337 fmt_ptr->prefix) != NULL)
338 || (idx == 6
339 && vim_strchr((char_u *)"OPQ",
340 fmt_ptr->prefix) == NULL))
341 {
342 sprintf((char *)errmsg,
343 _("E373: Unexpected %%%c in format string"), *efmp);
344 EMSG(errmsg);
345 goto error2;
346 }
347 fmt_ptr->addr[idx] = (char_u)++round;
348 *ptr++ = '\\';
349 *ptr++ = '(';
350#ifdef BACKSLASH_IN_FILENAME
351 if (*efmp == 'f')
352 {
353 /* Also match "c:" in the file name, even when
354 * checking for a colon next: "%f:".
355 * "\%(\a:\)\=" */
356 STRCPY(ptr, "\\%(\\a:\\)\\=");
357 ptr += 10;
358 }
359#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +0000360 if (*efmp == 'f' && efmp[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000362 if (efmp[1] != '\\' && efmp[1] != '%')
363 {
364 /* A file name may contain spaces, but this isn't
365 * in "\f". For "%f:%l:%m" there may be a ":" in
366 * the file name. Use ".\{-1,}x" instead (x is
367 * the next character), the requirement that :999:
368 * follows should work. */
369 STRCPY(ptr, ".\\{-1,}");
370 ptr += 7;
371 }
372 else
373 {
374 /* File name followed by '\\' or '%': include as
375 * many file name chars as possible. */
376 STRCPY(ptr, "\\f\\+");
377 ptr += 4;
378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 }
380 else
381 {
382 srcptr = (char_u *)fmt_pat[idx].pattern;
383 while ((*ptr = *srcptr++) != NUL)
384 ++ptr;
385 }
386 *ptr++ = '\\';
387 *ptr++ = ')';
388 }
389 else if (*efmp == '*')
390 {
391 if (*++efmp == '[' || *efmp == '\\')
392 {
393 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
394 {
395 if (efmp[1] == '^')
396 *ptr++ = *++efmp;
397 if (efmp < efm + len)
398 {
399 *ptr++ = *++efmp; /* could be ']' */
400 while (efmp < efm + len
401 && (*ptr++ = *++efmp) != ']')
402 /* skip */;
403 if (efmp == efm + len)
404 {
405 EMSG(_("E374: Missing ] in format string"));
406 goto error2;
407 }
408 }
409 }
410 else if (efmp < efm + len) /* %*\D, %*\s etc. */
411 *ptr++ = *++efmp;
412 *ptr++ = '\\';
413 *ptr++ = '+';
414 }
415 else
416 {
417 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
418 sprintf((char *)errmsg,
419 _("E375: Unsupported %%%c in format string"), *efmp);
420 EMSG(errmsg);
421 goto error2;
422 }
423 }
424 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
425 *ptr++ = *efmp; /* regexp magic characters */
426 else if (*efmp == '#')
427 *ptr++ = '*';
Bram Moolenaar01265852006-03-20 21:50:15 +0000428 else if (*efmp == '>')
429 fmt_ptr->conthere = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 else if (efmp == efm + 1) /* analyse prefix */
431 {
432 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
433 fmt_ptr->flags = *efmp++;
434 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
435 fmt_ptr->prefix = *efmp;
436 else
437 {
438 sprintf((char *)errmsg,
439 _("E376: Invalid %%%c in format string prefix"), *efmp);
440 EMSG(errmsg);
441 goto error2;
442 }
443 }
444 else
445 {
446 sprintf((char *)errmsg,
447 _("E377: Invalid %%%c in format string"), *efmp);
448 EMSG(errmsg);
449 goto error2;
450 }
451 }
452 else /* copy normal character */
453 {
454 if (*efmp == '\\' && efmp + 1 < efm + len)
455 ++efmp;
456 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
457 *ptr++ = '\\'; /* escape regexp atoms */
458 if (*efmp)
459 *ptr++ = *efmp;
460 }
461 }
462 *ptr++ = '$';
463 *ptr = NUL;
464 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
465 goto error2;
466 /*
467 * Advance to next part
468 */
469 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
470 }
471 if (fmt_first == NULL) /* nothing found */
472 {
473 EMSG(_("E378: 'errorformat' contains no pattern"));
474 goto error2;
475 }
476
477 /*
478 * got_int is reset here, because it was probably set when killing the
479 * ":make" command, but we still want to read the errorfile then.
480 */
481 got_int = FALSE;
482
483 /* Always ignore case when looking for a matching error. */
484 regmatch.rm_ic = TRUE;
485
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000486 if (tv != NULL)
487 {
488 if (tv->v_type == VAR_STRING)
489 p_str = tv->vval.v_string;
490 else if (tv->v_type == VAR_LIST)
491 p_li = tv->vval.v_list->lv_first;
492 }
493
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 /*
495 * Read the lines in the error file one by one.
496 * Try to recognize one of the error formats in each line.
497 */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000498 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 {
Bram Moolenaar86b68352004-12-27 21:59:20 +0000500 /* Get the next line. */
501 if (fd == NULL)
502 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000503 if (tv != NULL)
504 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000505 if (tv->v_type == VAR_STRING)
506 {
507 /* Get the next line from the supplied string */
508 char_u *p;
509
510 if (!*p_str) /* Reached the end of the string */
511 break;
512
513 p = vim_strchr(p_str, '\n');
514 if (p)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000515 len = (int)(p - p_str + 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000516 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000517 len = (int)STRLEN(p_str);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000518
519 if (len > CMDBUFFSIZE - 2)
520 vim_strncpy(IObuff, p_str, CMDBUFFSIZE - 2);
521 else
522 vim_strncpy(IObuff, p_str, len);
523
524 p_str += len;
525 }
526 else if (tv->v_type == VAR_LIST)
527 {
528 /* Get the next line from the supplied list */
529 while (p_li && p_li->li_tv.v_type != VAR_STRING)
530 p_li = p_li->li_next; /* Skip non-string items */
531
532 if (!p_li) /* End of the list */
533 break;
534
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000535 len = (int)STRLEN(p_li->li_tv.vval.v_string);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000536 if (len > CMDBUFFSIZE - 2)
537 len = CMDBUFFSIZE - 2;
538
539 vim_strncpy(IObuff, p_li->li_tv.vval.v_string, len);
540
541 p_li = p_li->li_next; /* next item */
542 }
543 }
544 else
545 {
546 /* Get the next line from the supplied buffer */
547 if (buflnum > lnumlast)
548 break;
549 vim_strncpy(IObuff, ml_get_buf(buf, buflnum++, FALSE),
550 CMDBUFFSIZE - 2);
551 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000552 }
553 else if (fgets((char *)IObuff, CMDBUFFSIZE - 2, fd) == NULL)
554 break;
555
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 IObuff[CMDBUFFSIZE - 2] = NUL; /* for very long lines */
557 if ((efmp = vim_strrchr(IObuff, '\n')) != NULL)
558 *efmp = NUL;
559#ifdef USE_CRNL
560 if ((efmp = vim_strrchr(IObuff, '\r')) != NULL)
561 *efmp = NUL;
562#endif
563
Bram Moolenaar01265852006-03-20 21:50:15 +0000564 /* If there was no %> item start at the first pattern */
565 if (fmt_start == NULL)
566 fmt_ptr = fmt_first;
567 else
568 {
569 fmt_ptr = fmt_start;
570 fmt_start = NULL;
571 }
572
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 /*
574 * Try to match each part of 'errorformat' until we find a complete
575 * match or no match.
576 */
577 valid = TRUE;
578restofline:
Bram Moolenaar01265852006-03-20 21:50:15 +0000579 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 {
581 idx = fmt_ptr->prefix;
582 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
583 continue;
584 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000585 pattern[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 if (!multiscan)
587 errmsg[0] = NUL;
588 lnum = 0;
589 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000590 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 enr = -1;
592 type = 0;
593 tail = NULL;
594
595 regmatch.regprog = fmt_ptr->prog;
596 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
597 {
598 if ((idx == 'C' || idx == 'Z') && !multiline)
599 continue;
600 if (vim_strchr((char_u *)"EWI", idx) != NULL)
601 type = idx;
602 else
603 type = 0;
604 /*
Bram Moolenaar4169da72006-06-20 18:49:32 +0000605 * Extract error message data from matched line.
606 * We check for an actual submatch, because "\[" and "\]" in
607 * the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 */
609 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
610 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000611 int c;
612
613 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
614 continue;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000615
616 /* Expand ~/file and $HOME/file to full path. */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000617 c = *regmatch.endp[i];
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000618 *regmatch.endp[i] = NUL;
619 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
620 *regmatch.endp[i] = c;
621
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000623 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 continue;
625 }
626 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000627 {
628 if (regmatch.startp[i] == NULL)
629 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 enr = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000633 {
634 if (regmatch.startp[i] == NULL)
635 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 lnum = atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000639 {
640 if (regmatch.startp[i] == NULL)
641 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000645 {
646 if (regmatch.startp[i] == NULL)
647 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 type = *regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000649 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000650 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 STRCPY(errmsg, IObuff);
652 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
653 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000654 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
655 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaarbbebc852005-07-18 21:47:53 +0000657 vim_strncpy(errmsg, regmatch.startp[i], len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 }
659 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000660 {
661 if (regmatch.startp[i] == NULL)
662 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 tail = regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
666 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000667 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
668 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 col = (int)(regmatch.endp[i] - regmatch.startp[i] + 1);
670 if (*((char_u *)regmatch.startp[i]) != TAB)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000671 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 }
673 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
674 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000675 if (regmatch.startp[i] == NULL)
676 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000678 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000680 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
681 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000682 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
683 continue;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000684 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
685 if (len > CMDBUFFSIZE - 5)
686 len = CMDBUFFSIZE - 5;
687 STRCPY(pattern, "^\\V");
688 STRNCAT(pattern, regmatch.startp[i], len);
689 pattern[len + 3] = '\\';
690 pattern[len + 4] = '$';
691 pattern[len + 5] = NUL;
692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 break;
694 }
695 }
696 multiscan = FALSE;
Bram Moolenaar01265852006-03-20 21:50:15 +0000697
Bram Moolenaar4770d092006-01-12 23:22:24 +0000698 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 {
Bram Moolenaar4770d092006-01-12 23:22:24 +0000700 if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 {
702 if (idx == 'D') /* enter directory */
703 {
704 if (*namebuf == NUL)
705 {
706 EMSG(_("E379: Missing or empty directory name"));
707 goto error2;
708 }
709 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
710 goto error2;
711 }
712 else if (idx == 'X') /* leave directory */
713 directory = qf_pop_dir(&dir_stack);
714 }
715 namebuf[0] = NUL; /* no match found, remove file name */
716 lnum = 0; /* don't jump to this line */
717 valid = FALSE;
718 STRCPY(errmsg, IObuff); /* copy whole line to error message */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000719 if (fmt_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 multiline = multiignore = FALSE;
721 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000722 else if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 {
Bram Moolenaar01265852006-03-20 21:50:15 +0000724 /* honor %> item */
725 if (fmt_ptr->conthere)
726 fmt_start = fmt_ptr;
727
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
729 multiline = TRUE; /* start of a multi-line message */
730 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
731 { /* continuation of multi-line msg */
732 if (qfprev == NULL)
733 goto error2;
734 if (*errmsg && !multiignore)
735 {
736 len = (int)STRLEN(qfprev->qf_text);
737 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
738 == NULL)
739 goto error2;
740 STRCPY(ptr, qfprev->qf_text);
741 vim_free(qfprev->qf_text);
742 qfprev->qf_text = ptr;
743 *(ptr += len) = '\n';
744 STRCPY(++ptr, errmsg);
745 }
746 if (qfprev->qf_nr == -1)
747 qfprev->qf_nr = enr;
748 if (vim_isprintc(type) && !qfprev->qf_type)
749 qfprev->qf_type = type; /* only printable chars allowed */
750 if (!qfprev->qf_lnum)
751 qfprev->qf_lnum = lnum;
752 if (!qfprev->qf_col)
753 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000754 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 if (!qfprev->qf_fnum)
756 qfprev->qf_fnum = qf_get_fnum(directory,
757 *namebuf || directory ? namebuf
758 : currfile && valid ? currfile : 0);
759 if (idx == 'Z')
760 multiline = multiignore = FALSE;
761 line_breakcheck();
762 continue;
763 }
764 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
765 {
766 /* global file names */
767 valid = FALSE;
768 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
769 {
770 if (*namebuf && idx == 'P')
771 currfile = qf_push_dir(namebuf, &file_stack);
772 else if (idx == 'Q')
773 currfile = qf_pop_dir(&file_stack);
774 *namebuf = NUL;
775 if (tail && *tail)
776 {
Bram Moolenaarc236c162008-07-13 17:41:49 +0000777 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 multiscan = TRUE;
779 goto restofline;
780 }
781 }
782 }
783 if (fmt_ptr->flags == '-') /* generally exclude this line */
784 {
785 if (multiline)
786 multiignore = TRUE; /* also exclude continuation lines */
787 continue;
788 }
789 }
790
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000791 if (qf_add_entry(qi, &qfprev,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 directory,
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000793 (*namebuf || directory)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 ? namebuf
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000795 : ((currfile && valid) ? currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000796 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 errmsg,
798 lnum,
799 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +0000800 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000801 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 enr,
803 type,
804 valid) == FAIL)
805 goto error2;
806 line_breakcheck();
807 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000808 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000810 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000812 /* no valid entry found */
813 qi->qf_lists[qi->qf_curlist].qf_ptr =
814 qi->qf_lists[qi->qf_curlist].qf_start;
815 qi->qf_lists[qi->qf_curlist].qf_index = 1;
816 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 }
818 else
819 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000820 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
821 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
822 qi->qf_lists[qi->qf_curlist].qf_ptr =
823 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000825 /* return number of matches */
826 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 goto qf_init_ok;
828 }
829 EMSG(_(e_readerrf));
830error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000831 qf_free(qi, qi->qf_curlist);
832 qi->qf_listcount--;
833 if (qi->qf_curlist > 0)
834 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835qf_init_ok:
Bram Moolenaar86b68352004-12-27 21:59:20 +0000836 if (fd != NULL)
837 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
839 {
840 fmt_first = fmt_ptr->next;
841 vim_free(fmt_ptr->prog);
842 vim_free(fmt_ptr);
843 }
844 qf_clean_dir_stack(&dir_stack);
845 qf_clean_dir_stack(&file_stack);
846qf_init_end:
847 vim_free(namebuf);
848 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000849 vim_free(pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 vim_free(fmtstr);
851
852#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000853 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854#endif
855
856 return retval;
857}
858
859/*
860 * Prepare for adding a new quickfix list.
861 */
862 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000863qf_new_list(qi)
864 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865{
866 int i;
867
868 /*
869 * If the current entry is not the last entry, delete entries below
870 * the current entry. This makes it possible to browse in a tree-like
871 * way with ":grep'.
872 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000873 while (qi->qf_listcount > qi->qf_curlist + 1)
874 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875
876 /*
877 * When the stack is full, remove to oldest entry
878 * Otherwise, add a new entry.
879 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000880 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000882 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000884 qi->qf_lists[i - 1] = qi->qf_lists[i];
885 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 }
887 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000888 qi->qf_curlist = qi->qf_listcount++;
889 qi->qf_lists[qi->qf_curlist].qf_index = 0;
890 qi->qf_lists[qi->qf_curlist].qf_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891}
892
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000893/*
894 * Free a location list
895 */
896 static void
897ll_free_all(pqi)
898 qf_info_T **pqi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000899{
900 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000901 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000902
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000903 qi = *pqi;
904 if (qi == NULL)
905 return;
906 *pqi = NULL; /* Remove reference to this list */
907
908 qi->qf_refcount--;
909 if (qi->qf_refcount < 1)
910 {
911 /* No references to this location list */
912 for (i = 0; i < qi->qf_listcount; ++i)
913 qf_free(qi, i);
914 vim_free(qi);
915 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000916}
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000917
918 void
919qf_free_all(wp)
920 win_T *wp;
921{
922 int i;
923 qf_info_T *qi = &ql_info;
924
925 if (wp != NULL)
926 {
927 /* location list */
928 ll_free_all(&wp->w_llist);
929 ll_free_all(&wp->w_llist_ref);
930 }
931 else
932 /* quickfix list */
933 for (i = 0; i < qi->qf_listcount; ++i)
934 qf_free(qi, i);
935}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000936
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937/*
938 * Add an entry to the end of the list of errors.
939 * Returns OK or FAIL.
940 */
941 static int
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000942qf_add_entry(qi, prevp, dir, fname, bufnum, mesg, lnum, col, vis_col, pattern,
943 nr, type, valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000944 qf_info_T *qi; /* quickfix list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000945 qfline_T **prevp; /* pointer to previously added entry or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 char_u *dir; /* optional directory name */
947 char_u *fname; /* file name or NULL */
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000948 int bufnum; /* buffer number or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 char_u *mesg; /* message */
950 long lnum; /* line number */
951 int col; /* column */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000952 int vis_col; /* using visual column */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000953 char_u *pattern; /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 int nr; /* error number */
955 int type; /* type character */
956 int valid; /* valid entry */
957{
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000958 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000960 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000962 if (bufnum != 0)
963 qfp->qf_fnum = bufnum;
964 else
965 qfp->qf_fnum = qf_get_fnum(dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
967 {
968 vim_free(qfp);
969 return FAIL;
970 }
971 qfp->qf_lnum = lnum;
972 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000973 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000974 if (pattern == NULL || *pattern == NUL)
975 qfp->qf_pattern = NULL;
976 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
977 {
978 vim_free(qfp->qf_text);
979 vim_free(qfp);
980 return FAIL;
981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 qfp->qf_nr = nr;
983 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
984 type = 0;
985 qfp->qf_type = type;
986 qfp->qf_valid = valid;
987
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000988 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
989 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000991 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 qfp->qf_prev = qfp; /* first element points to itself */
993 }
994 else
995 {
996 qfp->qf_prev = *prevp;
997 (*prevp)->qf_next = qfp;
998 }
999 qfp->qf_next = qfp; /* last element points to itself */
1000 qfp->qf_cleared = FALSE;
1001 *prevp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001002 ++qi->qf_lists[qi->qf_curlist].qf_count;
1003 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1004 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001006 qi->qf_lists[qi->qf_curlist].qf_index =
1007 qi->qf_lists[qi->qf_curlist].qf_count;
1008 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 }
1010
1011 return OK;
1012}
1013
1014/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001015 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001016 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001017 static qf_info_T *
1018ll_new_list()
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001019{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001020 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001021
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001022 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1023 if (qi != NULL)
1024 {
1025 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1026 qi->qf_refcount++;
1027 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001028
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001029 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001030}
1031
1032/*
1033 * Return the location list for window 'wp'.
1034 * If not present, allocate a location list
1035 */
1036 static qf_info_T *
1037ll_get_or_alloc_list(wp)
1038 win_T *wp;
1039{
1040 if (IS_LL_WINDOW(wp))
1041 /* For a location list window, use the referenced location list */
1042 return wp->w_llist_ref;
1043
1044 /*
1045 * For a non-location list window, w_llist_ref should not point to a
1046 * location list.
1047 */
1048 ll_free_all(&wp->w_llist_ref);
1049
1050 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001051 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001052 return wp->w_llist;
1053}
1054
1055/*
1056 * Copy the location list from window "from" to window "to".
1057 */
1058 void
1059copy_loclist(from, to)
1060 win_T *from;
1061 win_T *to;
1062{
1063 qf_info_T *qi;
1064 int idx;
1065 int i;
1066
1067 /*
1068 * When copying from a location list window, copy the referenced
1069 * location list. For other windows, copy the location list for
1070 * that window.
1071 */
1072 if (IS_LL_WINDOW(from))
1073 qi = from->w_llist_ref;
1074 else
1075 qi = from->w_llist;
1076
1077 if (qi == NULL) /* no location list to copy */
1078 return;
1079
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001080 /* allocate a new location list */
1081 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001082 return;
1083
1084 to->w_llist->qf_listcount = qi->qf_listcount;
1085
1086 /* Copy the location lists one at a time */
1087 for (idx = 0; idx < qi->qf_listcount; idx++)
1088 {
1089 qf_list_T *from_qfl;
1090 qf_list_T *to_qfl;
1091
1092 to->w_llist->qf_curlist = idx;
1093
1094 from_qfl = &qi->qf_lists[idx];
1095 to_qfl = &to->w_llist->qf_lists[idx];
1096
1097 /* Some of the fields are populated by qf_add_entry() */
1098 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1099 to_qfl->qf_count = 0;
1100 to_qfl->qf_index = 0;
1101 to_qfl->qf_start = NULL;
1102 to_qfl->qf_ptr = NULL;
1103
1104 if (from_qfl->qf_count)
1105 {
1106 qfline_T *from_qfp;
1107 qfline_T *prevp = NULL;
1108
1109 /* copy all the location entries in this list */
1110 for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
1111 ++i, from_qfp = from_qfp->qf_next)
1112 {
1113 if (qf_add_entry(to->w_llist, &prevp,
1114 NULL,
1115 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001116 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001117 from_qfp->qf_text,
1118 from_qfp->qf_lnum,
1119 from_qfp->qf_col,
1120 from_qfp->qf_viscol,
1121 from_qfp->qf_pattern,
1122 from_qfp->qf_nr,
1123 0,
1124 from_qfp->qf_valid) == FAIL)
1125 {
1126 qf_free_all(to);
1127 return;
1128 }
1129 /*
1130 * qf_add_entry() will not set the qf_num field, as the
1131 * directory and file names are not supplied. So the qf_fnum
1132 * field is copied here.
1133 */
1134 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1135 prevp->qf_type = from_qfp->qf_type; /* error type */
1136 if (from_qfl->qf_ptr == from_qfp)
1137 to_qfl->qf_ptr = prevp; /* current location */
1138 }
1139 }
1140
1141 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1142
1143 /* When no valid entries are present in the list, qf_ptr points to
1144 * the first item in the list */
1145 if (to_qfl->qf_nonevalid == TRUE)
1146 to_qfl->qf_ptr = to_qfl->qf_start;
1147 }
1148
1149 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1150}
1151
1152/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 * get buffer number for file "dir.name"
1154 */
1155 static int
1156qf_get_fnum(directory, fname)
1157 char_u *directory;
1158 char_u *fname;
1159{
1160 if (fname == NULL || *fname == NUL) /* no file name */
1161 return 0;
1162 {
1163#ifdef RISCOS
1164 /* Name is reported as `main.c', but file is `c.main' */
1165 return ro_buflist_add(fname);
1166#else
1167 char_u *ptr;
1168 int fnum;
1169
1170# ifdef VMS
1171 vms_remove_version(fname);
1172# endif
1173# ifdef BACKSLASH_IN_FILENAME
1174 if (directory != NULL)
1175 slash_adjust(directory);
1176 slash_adjust(fname);
1177# endif
1178 if (directory != NULL && !vim_isAbsName(fname)
1179 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1180 {
1181 /*
1182 * Here we check if the file really exists.
1183 * This should normally be true, but if make works without
1184 * "leaving directory"-messages we might have missed a
1185 * directory change.
1186 */
1187 if (mch_getperm(ptr) < 0)
1188 {
1189 vim_free(ptr);
1190 directory = qf_guess_filepath(fname);
1191 if (directory)
1192 ptr = concat_fnames(directory, fname, TRUE);
1193 else
1194 ptr = vim_strsave(fname);
1195 }
1196 /* Use concatenated directory name and file name */
1197 fnum = buflist_add(ptr, 0);
1198 vim_free(ptr);
1199 return fnum;
1200 }
1201 return buflist_add(fname, 0);
1202#endif
1203 }
1204}
1205
1206/*
1207 * push dirbuf onto the directory stack and return pointer to actual dir or
1208 * NULL on error
1209 */
1210 static char_u *
1211qf_push_dir(dirbuf, stackptr)
1212 char_u *dirbuf;
1213 struct dir_stack_T **stackptr;
1214{
1215 struct dir_stack_T *ds_new;
1216 struct dir_stack_T *ds_ptr;
1217
1218 /* allocate new stack element and hook it in */
1219 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1220 if (ds_new == NULL)
1221 return NULL;
1222
1223 ds_new->next = *stackptr;
1224 *stackptr = ds_new;
1225
1226 /* store directory on the stack */
1227 if (vim_isAbsName(dirbuf)
1228 || (*stackptr)->next == NULL
1229 || (*stackptr && dir_stack != *stackptr))
1230 (*stackptr)->dirname = vim_strsave(dirbuf);
1231 else
1232 {
1233 /* Okay we don't have an absolute path.
1234 * dirbuf must be a subdir of one of the directories on the stack.
1235 * Let's search...
1236 */
1237 ds_new = (*stackptr)->next;
1238 (*stackptr)->dirname = NULL;
1239 while (ds_new)
1240 {
1241 vim_free((*stackptr)->dirname);
1242 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1243 TRUE);
1244 if (mch_isdir((*stackptr)->dirname) == TRUE)
1245 break;
1246
1247 ds_new = ds_new->next;
1248 }
1249
1250 /* clean up all dirs we already left */
1251 while ((*stackptr)->next != ds_new)
1252 {
1253 ds_ptr = (*stackptr)->next;
1254 (*stackptr)->next = (*stackptr)->next->next;
1255 vim_free(ds_ptr->dirname);
1256 vim_free(ds_ptr);
1257 }
1258
1259 /* Nothing found -> it must be on top level */
1260 if (ds_new == NULL)
1261 {
1262 vim_free((*stackptr)->dirname);
1263 (*stackptr)->dirname = vim_strsave(dirbuf);
1264 }
1265 }
1266
1267 if ((*stackptr)->dirname != NULL)
1268 return (*stackptr)->dirname;
1269 else
1270 {
1271 ds_ptr = *stackptr;
1272 *stackptr = (*stackptr)->next;
1273 vim_free(ds_ptr);
1274 return NULL;
1275 }
1276}
1277
1278
1279/*
1280 * pop dirbuf from the directory stack and return previous directory or NULL if
1281 * stack is empty
1282 */
1283 static char_u *
1284qf_pop_dir(stackptr)
1285 struct dir_stack_T **stackptr;
1286{
1287 struct dir_stack_T *ds_ptr;
1288
1289 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1290 * What to do if it isn't? */
1291
1292 /* pop top element and free it */
1293 if (*stackptr != NULL)
1294 {
1295 ds_ptr = *stackptr;
1296 *stackptr = (*stackptr)->next;
1297 vim_free(ds_ptr->dirname);
1298 vim_free(ds_ptr);
1299 }
1300
1301 /* return NEW top element as current dir or NULL if stack is empty*/
1302 return *stackptr ? (*stackptr)->dirname : NULL;
1303}
1304
1305/*
1306 * clean up directory stack
1307 */
1308 static void
1309qf_clean_dir_stack(stackptr)
1310 struct dir_stack_T **stackptr;
1311{
1312 struct dir_stack_T *ds_ptr;
1313
1314 while ((ds_ptr = *stackptr) != NULL)
1315 {
1316 *stackptr = (*stackptr)->next;
1317 vim_free(ds_ptr->dirname);
1318 vim_free(ds_ptr);
1319 }
1320}
1321
1322/*
1323 * Check in which directory of the directory stack the given file can be
1324 * found.
1325 * Returns a pointer to the directory name or NULL if not found
1326 * Cleans up intermediate directory entries.
1327 *
1328 * TODO: How to solve the following problem?
1329 * If we have the this directory tree:
1330 * ./
1331 * ./aa
1332 * ./aa/bb
1333 * ./bb
1334 * ./bb/x.c
1335 * and make says:
1336 * making all in aa
1337 * making all in bb
1338 * x.c:9: Error
1339 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1340 * qf_guess_filepath will return NULL.
1341 */
1342 static char_u *
1343qf_guess_filepath(filename)
1344 char_u *filename;
1345{
1346 struct dir_stack_T *ds_ptr;
1347 struct dir_stack_T *ds_tmp;
1348 char_u *fullname;
1349
1350 /* no dirs on the stack - there's nothing we can do */
1351 if (dir_stack == NULL)
1352 return NULL;
1353
1354 ds_ptr = dir_stack->next;
1355 fullname = NULL;
1356 while (ds_ptr)
1357 {
1358 vim_free(fullname);
1359 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1360
1361 /* If concat_fnames failed, just go on. The worst thing that can happen
1362 * is that we delete the entire stack.
1363 */
1364 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1365 break;
1366
1367 ds_ptr = ds_ptr->next;
1368 }
1369
1370 vim_free(fullname);
1371
1372 /* clean up all dirs we already left */
1373 while (dir_stack->next != ds_ptr)
1374 {
1375 ds_tmp = dir_stack->next;
1376 dir_stack->next = dir_stack->next->next;
1377 vim_free(ds_tmp->dirname);
1378 vim_free(ds_tmp);
1379 }
1380
1381 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1382
1383}
1384
1385/*
1386 * jump to a quickfix line
1387 * if dir == FORWARD go "errornr" valid entries forward
1388 * if dir == BACKWARD go "errornr" valid entries backward
1389 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1390 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1391 * else if "errornr" is zero, redisplay the same line
1392 * else go to entry "errornr"
1393 */
1394 void
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001395qf_jump(qi, dir, errornr, forceit)
1396 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 int dir;
1398 int errornr;
1399 int forceit;
1400{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001401 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001402 qfline_T *qf_ptr;
1403 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 int qf_index;
1405 int old_qf_fnum;
1406 int old_qf_index;
1407 int prev_index;
1408 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1409 char_u *err = e_no_more_items;
1410 linenr_T i;
1411 buf_T *old_curbuf;
1412 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 colnr_T screen_col;
1414 colnr_T char_col;
1415 char_u *line;
1416#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001417 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001418 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 int opened_window = FALSE;
1420 win_T *win;
1421 win_T *altwin;
1422#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001423 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 int print_message = TRUE;
1425 int len;
1426#ifdef FEAT_FOLDING
1427 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1428#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001429 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001430 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001432 if (qi == NULL)
1433 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001434
1435 if (qi->qf_curlist >= qi->qf_listcount
1436 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 {
1438 EMSG(_(e_quickfix));
1439 return;
1440 }
1441
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001442 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001444 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 old_qf_index = qf_index;
1446 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1447 {
1448 while (errornr--)
1449 {
1450 old_qf_ptr = qf_ptr;
1451 prev_index = qf_index;
1452 old_qf_fnum = qf_ptr->qf_fnum;
1453 do
1454 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001455 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 || qf_ptr->qf_next == NULL)
1457 {
1458 qf_ptr = old_qf_ptr;
1459 qf_index = prev_index;
1460 if (err != NULL)
1461 {
1462 EMSG(_(err));
1463 goto theend;
1464 }
1465 errornr = 0;
1466 break;
1467 }
1468 ++qf_index;
1469 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001470 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1471 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1473 err = NULL;
1474 }
1475 }
1476 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1477 {
1478 while (errornr--)
1479 {
1480 old_qf_ptr = qf_ptr;
1481 prev_index = qf_index;
1482 old_qf_fnum = qf_ptr->qf_fnum;
1483 do
1484 {
1485 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1486 {
1487 qf_ptr = old_qf_ptr;
1488 qf_index = prev_index;
1489 if (err != NULL)
1490 {
1491 EMSG(_(err));
1492 goto theend;
1493 }
1494 errornr = 0;
1495 break;
1496 }
1497 --qf_index;
1498 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001499 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1500 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1502 err = NULL;
1503 }
1504 }
1505 else if (errornr != 0) /* go to specified number */
1506 {
1507 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1508 {
1509 --qf_index;
1510 qf_ptr = qf_ptr->qf_prev;
1511 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001512 while (errornr > qf_index && qf_index <
1513 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 && qf_ptr->qf_next != NULL)
1515 {
1516 ++qf_index;
1517 qf_ptr = qf_ptr->qf_next;
1518 }
1519 }
1520
1521#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001522 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1523 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 /* No need to print the error message if it's visible in the error
1525 * window */
1526 print_message = FALSE;
1527
1528 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001529 * For ":helpgrep" find a help window or open one.
1530 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001531 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001532 {
1533 win_T *wp;
1534 int n;
1535
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001536 if (cmdmod.tab != 0)
1537 wp = NULL;
1538 else
1539 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1540 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1541 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001542 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1543 win_enter(wp, TRUE);
1544 else
1545 {
1546 /*
1547 * Split off help window; put it at far top if no position
1548 * specified, the current window is vertically split and narrow.
1549 */
1550 n = WSP_HELP;
1551# ifdef FEAT_VERTSPLIT
1552 if (cmdmod.split == 0 && curwin->w_width != Columns
1553 && curwin->w_width < 80)
1554 n |= WSP_TOP;
1555# endif
1556 if (win_split(0, n) == FAIL)
1557 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001558 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001559
1560 if (curwin->w_height < p_hh)
1561 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001562
1563 if (qi != &ql_info) /* not a quickfix list */
1564 {
1565 /* The new window should use the supplied location list */
1566 qf_free_all(curwin);
1567 curwin->w_llist = qi;
1568 qi->qf_refcount++;
1569 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001570 }
1571
1572 if (!p_im)
1573 restart_edit = 0; /* don't want insert mode in help file */
1574 }
1575
1576 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 * If currently in the quickfix window, find another window to show the
1578 * file in.
1579 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001580 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 {
1582 /*
1583 * If there is no file specified, we don't know where to go.
1584 * But do advance, otherwise ":cn" gets stuck.
1585 */
1586 if (qf_ptr->qf_fnum == 0)
1587 goto theend;
1588
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001589 /* Locate a window showing a normal buffer */
1590 usable_win = 0;
1591 FOR_ALL_WINDOWS(win)
1592 if (win->w_buffer->b_p_bt[0] == NUL)
1593 {
1594 usable_win = 1;
1595 break;
1596 }
1597
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00001599 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001600 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001602 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001603 {
1604 tabpage_T *tp;
1605 win_T *wp;
1606
1607 FOR_ALL_TAB_WINDOWS(tp, wp)
1608 {
1609 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
1610 {
1611 goto_tabpage_win(tp, wp);
1612 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001613 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001614 }
1615 }
1616 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001617win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001618
1619 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00001620 * If there is only one window and it is the quickfix window, create a
1621 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001622 */
1623 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001625 ll_ref = curwin->w_llist_ref;
1626
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 if (win_split(0, WSP_ABOVE) == FAIL)
1628 goto failed; /* not enough room for window */
1629 opened_window = TRUE; /* close it when fail */
1630 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001631 swb_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632# ifdef FEAT_SCROLLBIND
1633 curwin->w_p_scb = FALSE;
1634# endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001635 if (ll_ref != NULL)
1636 {
1637 /* The new window should use the location list from the
1638 * location list window */
1639 qf_free_all(curwin);
1640 curwin->w_llist = ll_ref;
1641 ll_ref->qf_refcount++;
1642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 }
1644 else
1645 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001646 if (curwin->w_llist_ref != NULL)
1647 {
1648 /* In a location window */
1649 ll_ref = curwin->w_llist_ref;
1650
1651 /* Find the window with the same location list */
1652 FOR_ALL_WINDOWS(win)
1653 if (win->w_llist == ll_ref)
1654 break;
1655 if (win == NULL)
1656 {
1657 /* Find the window showing the selected file */
1658 FOR_ALL_WINDOWS(win)
1659 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1660 break;
1661 if (win == NULL)
1662 {
1663 /* Find a previous usable window */
1664 win = curwin;
1665 do
1666 {
1667 if (win->w_buffer->b_p_bt[0] == NUL)
1668 break;
1669 if (win->w_prev == NULL)
1670 win = lastwin; /* wrap around the top */
1671 else
1672 win = win->w_prev; /* go to previous window */
1673 } while (win != curwin);
1674 }
1675 }
1676 win_goto(win);
1677
1678 /* If the location list for the window is not set, then set it
1679 * to the location list from the location window */
1680 if (win->w_llist == NULL)
1681 {
1682 win->w_llist = ll_ref;
1683 ll_ref->qf_refcount++;
1684 }
1685 }
1686 else
1687 {
1688
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 /*
1690 * Try to find a window that shows the right buffer.
1691 * Default to the window just above the quickfix buffer.
1692 */
1693 win = curwin;
1694 altwin = NULL;
1695 for (;;)
1696 {
1697 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1698 break;
1699 if (win->w_prev == NULL)
1700 win = lastwin; /* wrap around the top */
1701 else
1702 win = win->w_prev; /* go to previous window */
1703
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001704 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 {
1706 /* Didn't find it, go to the window before the quickfix
1707 * window. */
1708 if (altwin != NULL)
1709 win = altwin;
1710 else if (curwin->w_prev != NULL)
1711 win = curwin->w_prev;
1712 else
1713 win = curwin->w_next;
1714 break;
1715 }
1716
1717 /* Remember a usable window. */
1718 if (altwin == NULL && !win->w_p_pvw
1719 && win->w_buffer->b_p_bt[0] == NUL)
1720 altwin = win;
1721 }
1722
1723 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 }
1726 }
1727#endif
1728
1729 /*
1730 * If there is a file name,
1731 * read the wanted file if needed, and check autowrite etc.
1732 */
1733 old_curbuf = curbuf;
1734 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001735
1736 if (qf_ptr->qf_fnum != 0)
1737 {
1738 if (qf_ptr->qf_type == 1)
1739 {
1740 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1741 * set b_p_ro flag). */
1742 if (!can_abandon(curbuf, forceit))
1743 {
1744 EMSG(_(e_nowrtmsg));
1745 ok = FALSE;
1746 }
1747 else
1748 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001749 ECMD_HIDE + ECMD_SET_HELP,
1750 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001751 }
1752 else
1753 ok = buflist_getfile(qf_ptr->qf_fnum,
1754 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
1755 }
1756
1757 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 {
1759 /* When not switched to another buffer, still need to set pc mark */
1760 if (curbuf == old_curbuf)
1761 setpcmark();
1762
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001763 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001765 /*
1766 * Go to line with error, unless qf_lnum is 0.
1767 */
1768 i = qf_ptr->qf_lnum;
1769 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001771 if (i > curbuf->b_ml.ml_line_count)
1772 i = curbuf->b_ml.ml_line_count;
1773 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001775 if (qf_ptr->qf_col > 0)
1776 {
1777 curwin->w_cursor.col = qf_ptr->qf_col - 1;
1778 if (qf_ptr->qf_viscol == TRUE)
1779 {
1780 /*
1781 * Check each character from the beginning of the error
1782 * line up to the error column. For each tab character
1783 * found, reduce the error column value by the length of
1784 * a tab character.
1785 */
1786 line = ml_get_curline();
1787 screen_col = 0;
1788 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1789 {
1790 if (*line == NUL)
1791 break;
1792 if (*line++ == '\t')
1793 {
1794 curwin->w_cursor.col -= 7 - (screen_col % 8);
1795 screen_col += 8 - (screen_col % 8);
1796 }
1797 else
1798 ++screen_col;
1799 }
1800 }
1801 check_cursor();
1802 }
1803 else
1804 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 }
1806 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001807 {
1808 pos_T save_cursor;
1809
1810 /* Move the cursor to the first line in the buffer */
1811 save_cursor = curwin->w_cursor;
1812 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001813 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
1814 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001815 curwin->w_cursor = save_cursor;
1816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817
1818#ifdef FEAT_FOLDING
1819 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1820 foldOpenCursor();
1821#endif
1822 if (print_message)
1823 {
1824 /* Update the screen before showing the message */
1825 update_topline_redraw();
1826 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001827 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1829 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1830 /* Add the message, skipping leading whitespace and newlines. */
1831 len = (int)STRLEN(IObuff);
1832 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1833
1834 /* Output the message. Overwrite to avoid scrolling when the 'O'
1835 * flag is present in 'shortmess'; But when not jumping, print the
1836 * whole message. */
1837 i = msg_scroll;
1838 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1839 msg_scroll = TRUE;
1840 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1841 msg_scroll = FALSE;
1842 msg_attr_keep(IObuff, 0, TRUE);
1843 msg_scroll = i;
1844 }
1845 }
1846 else
1847 {
1848#ifdef FEAT_WINDOWS
1849 if (opened_window)
1850 win_close(curwin, TRUE); /* Close opened window */
1851#endif
1852 if (qf_ptr->qf_fnum != 0)
1853 {
1854 /*
1855 * Couldn't open file, so put index back where it was. This could
1856 * happen if the file was readonly and we changed something.
1857 */
1858#ifdef FEAT_WINDOWS
1859failed:
1860#endif
1861 qf_ptr = old_qf_ptr;
1862 qf_index = old_qf_index;
1863 }
1864 }
1865theend:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001866 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
1867 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868#ifdef FEAT_WINDOWS
1869 if (p_swb != old_swb && opened_window)
1870 {
1871 /* Restore old 'switchbuf' value, but not when an autocommand or
1872 * modeline has changed the value. */
1873 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00001874 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001876 swb_flags = old_swb_flags;
1877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 else
1879 free_string_option(old_swb);
1880 }
1881#endif
1882}
1883
1884/*
1885 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001886 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 */
1888 void
1889qf_list(eap)
1890 exarg_T *eap;
1891{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001892 buf_T *buf;
1893 char_u *fname;
1894 qfline_T *qfp;
1895 int i;
1896 int idx1 = 1;
1897 int idx2 = -1;
1898 int need_return = TRUE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001899 char_u *arg = eap->arg;
1900 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001902 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001904 if (eap->cmdidx == CMD_llist)
1905 {
1906 qi = GET_LOC_LIST(curwin);
1907 if (qi == NULL)
1908 {
1909 EMSG(_(e_loclist));
1910 return;
1911 }
1912 }
1913
1914 if (qi->qf_curlist >= qi->qf_listcount
1915 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 {
1917 EMSG(_(e_quickfix));
1918 return;
1919 }
1920 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
1921 {
1922 EMSG(_(e_trailing));
1923 return;
1924 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001925 i = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 if (idx1 < 0)
1927 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
1928 if (idx2 < 0)
1929 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
1930
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001931 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001933 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
1934 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 {
1936 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
1937 {
1938 if (need_return)
1939 {
1940 msg_putchar('\n');
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001941 if (got_int)
1942 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 need_return = FALSE;
1944 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001945
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001946 fname = NULL;
1947 if (qfp->qf_fnum != 0
1948 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
1949 {
1950 fname = buf->b_fname;
1951 if (qfp->qf_type == 1) /* :helpgrep */
1952 fname = gettail(fname);
1953 }
1954 if (fname == NULL)
1955 sprintf((char *)IObuff, "%2d", i);
1956 else
1957 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
1958 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001959 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001960 ? hl_attr(HLF_L) : hl_attr(HLF_D));
1961 if (qfp->qf_lnum == 0)
1962 IObuff[0] = NUL;
1963 else if (qfp->qf_col == 0)
1964 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
1965 else
1966 sprintf((char *)IObuff, ":%ld col %d",
1967 qfp->qf_lnum, qfp->qf_col);
1968 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
1969 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
1970 msg_puts_attr(IObuff, hl_attr(HLF_N));
1971 if (qfp->qf_pattern != NULL)
1972 {
1973 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
1974 STRCAT(IObuff, ":");
1975 msg_puts(IObuff);
1976 }
1977 msg_puts((char_u *)" ");
1978
1979 /* Remove newlines and leading whitespace from the text. For an
1980 * unrecognized line keep the indent, the compiler may mark a word
1981 * with ^^^^. */
1982 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 ? skipwhite(qfp->qf_text) : qfp->qf_text,
1984 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001985 msg_prt_line(IObuff, FALSE);
1986 out_flush(); /* show one line at a time */
1987 need_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001989
1990 qfp = qfp->qf_next;
1991 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 ui_breakcheck();
1993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994}
1995
1996/*
1997 * Remove newlines and leading whitespace from an error message.
1998 * Put the result in "buf[bufsize]".
1999 */
2000 static void
2001qf_fmt_text(text, buf, bufsize)
2002 char_u *text;
2003 char_u *buf;
2004 int bufsize;
2005{
2006 int i;
2007 char_u *p = text;
2008
2009 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2010 {
2011 if (*p == '\n')
2012 {
2013 buf[i] = ' ';
2014 while (*++p != NUL)
2015 if (!vim_iswhite(*p) && *p != '\n')
2016 break;
2017 }
2018 else
2019 buf[i] = *p++;
2020 }
2021 buf[i] = NUL;
2022}
2023
2024/*
2025 * ":colder [count]": Up in the quickfix stack.
2026 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002027 * ":lolder [count]": Up in the location list stack.
2028 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 */
2030 void
2031qf_age(eap)
2032 exarg_T *eap;
2033{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002034 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 int count;
2036
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002037 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2038 {
2039 qi = GET_LOC_LIST(curwin);
2040 if (qi == NULL)
2041 {
2042 EMSG(_(e_loclist));
2043 return;
2044 }
2045 }
2046
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 if (eap->addr_count != 0)
2048 count = eap->line2;
2049 else
2050 count = 1;
2051 while (count--)
2052 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002053 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002055 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 {
2057 EMSG(_("E380: At bottom of quickfix stack"));
2058 return;
2059 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002060 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 }
2062 else
2063 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002064 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 {
2066 EMSG(_("E381: At top of quickfix stack"));
2067 return;
2068 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002069 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 }
2071 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002072 qf_msg(qi);
2073
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074}
2075
2076 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002077qf_msg(qi)
2078 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079{
2080 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002081 qi->qf_curlist + 1, qi->qf_listcount,
2082 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002084 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085#endif
2086}
2087
2088/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002089 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 */
2091 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002092qf_free(qi, idx)
2093 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 int idx;
2095{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002096 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002098 while (qi->qf_lists[idx].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002100 qfp = qi->qf_lists[idx].qf_start->qf_next;
2101 vim_free(qi->qf_lists[idx].qf_start->qf_text);
2102 vim_free(qi->qf_lists[idx].qf_start->qf_pattern);
2103 vim_free(qi->qf_lists[idx].qf_start);
2104 qi->qf_lists[idx].qf_start = qfp;
2105 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 }
2107}
2108
2109/*
2110 * qf_mark_adjust: adjust marks
2111 */
2112 void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002113qf_mark_adjust(wp, line1, line2, amount, amount_after)
2114 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 linenr_T line1;
2116 linenr_T line2;
2117 long amount;
2118 long amount_after;
2119{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002120 int i;
2121 qfline_T *qfp;
2122 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002123 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002125 if (wp != NULL)
2126 {
2127 if (wp->w_llist == NULL)
2128 return;
2129 qi = wp->w_llist;
2130 }
2131
2132 for (idx = 0; idx < qi->qf_listcount; ++idx)
2133 if (qi->qf_lists[idx].qf_count)
2134 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
2135 i < qi->qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 if (qfp->qf_fnum == curbuf->b_fnum)
2137 {
2138 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2139 {
2140 if (amount == MAXLNUM)
2141 qfp->qf_cleared = TRUE;
2142 else
2143 qfp->qf_lnum += amount;
2144 }
2145 else if (amount_after && qfp->qf_lnum > line2)
2146 qfp->qf_lnum += amount_after;
2147 }
2148}
2149
2150/*
2151 * Make a nice message out of the error character and the error number:
2152 * char number message
2153 * e or E 0 " error"
2154 * w or W 0 " warning"
2155 * i or I 0 " info"
2156 * 0 0 ""
2157 * other 0 " c"
2158 * e or E n " error n"
2159 * w or W n " warning n"
2160 * i or I n " info n"
2161 * 0 n " error n"
2162 * other n " c n"
2163 * 1 x "" :helpgrep
2164 */
2165 static char_u *
2166qf_types(c, nr)
2167 int c, nr;
2168{
2169 static char_u buf[20];
2170 static char_u cc[3];
2171 char_u *p;
2172
2173 if (c == 'W' || c == 'w')
2174 p = (char_u *)" warning";
2175 else if (c == 'I' || c == 'i')
2176 p = (char_u *)" info";
2177 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2178 p = (char_u *)" error";
2179 else if (c == 0 || c == 1)
2180 p = (char_u *)"";
2181 else
2182 {
2183 cc[0] = ' ';
2184 cc[1] = c;
2185 cc[2] = NUL;
2186 p = cc;
2187 }
2188
2189 if (nr <= 0)
2190 return p;
2191
2192 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2193 return buf;
2194}
2195
2196#if defined(FEAT_WINDOWS) || defined(PROTO)
2197/*
2198 * ":cwindow": open the quickfix window if we have errors to display,
2199 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002200 * ":lwindow": open the location list window if we have locations to display,
2201 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 */
2203 void
2204ex_cwindow(eap)
2205 exarg_T *eap;
2206{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002207 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 win_T *win;
2209
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002210 if (eap->cmdidx == CMD_lwindow)
2211 {
2212 qi = GET_LOC_LIST(curwin);
2213 if (qi == NULL)
2214 return;
2215 }
2216
2217 /* Look for an existing quickfix window. */
2218 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219
2220 /*
2221 * If a quickfix window is open but we have no errors to display,
2222 * close the window. If a quickfix window is not open, then open
2223 * it if we have errors; otherwise, leave it closed.
2224 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002225 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard68071d2006-05-02 22:08:30 +00002226 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 {
2228 if (win != NULL)
2229 ex_cclose(eap);
2230 }
2231 else if (win == NULL)
2232 ex_copen(eap);
2233}
2234
2235/*
2236 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002237 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 */
2239/*ARGSUSED*/
2240 void
2241ex_cclose(eap)
2242 exarg_T *eap;
2243{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002244 win_T *win = NULL;
2245 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002247 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2248 {
2249 qi = GET_LOC_LIST(curwin);
2250 if (qi == NULL)
2251 return;
2252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002254 /* Find existing quickfix window and close it. */
2255 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 if (win != NULL)
2257 win_close(win, FALSE);
2258}
2259
2260/*
2261 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002262 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 */
2264 void
2265ex_copen(eap)
2266 exarg_T *eap;
2267{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002268 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002271 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002272 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002273 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002275 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2276 {
2277 qi = GET_LOC_LIST(curwin);
2278 if (qi == NULL)
2279 {
2280 EMSG(_(e_loclist));
2281 return;
2282 }
2283 }
2284
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 if (eap->addr_count != 0)
2286 height = eap->line2;
2287 else
2288 height = QF_WINHEIGHT;
2289
2290#ifdef FEAT_VISUAL
2291 reset_VIsual_and_resel(); /* stop Visual mode */
2292#endif
2293#ifdef FEAT_GUI
2294 need_mouse_correct = TRUE;
2295#endif
2296
2297 /*
2298 * Find existing quickfix window, or open a new one.
2299 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002300 win = qf_find_win(qi);
2301
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002302 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 win_goto(win);
2304 else
2305 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002306 qf_buf = qf_find_buf(qi);
2307
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 /* The current window becomes the previous window afterwards. */
2309 win = curwin;
2310
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002311 if (eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2312 /* Create the new window at the very bottom. */
2313 win_goto(lastwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 if (win_split(height, WSP_BELOW) == FAIL)
2315 return; /* not enough room for window */
2316#ifdef FEAT_SCROLLBIND
2317 curwin->w_p_scb = FALSE;
2318#endif
2319
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002320 /* Remove the location list for the quickfix window */
2321 qf_free_all(curwin);
2322
2323 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002325 /*
2326 * For the location list window, create a reference to the
2327 * location list from the window 'win'.
2328 */
2329 curwin->w_llist_ref = win->w_llist;
2330 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002332
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002333 if (oldwin != curwin)
2334 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002335 if (qf_buf != NULL)
2336 /* Use the existing quickfix buffer */
2337 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002338 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002339 else
2340 {
2341 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002342 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002343 /* switch off 'swapfile' */
2344 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2345 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002346 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002347 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar7f514742007-06-28 19:33:43 +00002348 set_option_value((char_u *)"diff", 0L, NULL, OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002351 /* Only set the height when still in the same tab page and there is no
2352 * window to the side. */
2353 if (curtab == prevtab
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002354#ifdef FEAT_VERTSPLIT
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002355 && curwin->w_width == Columns
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002356#endif
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002357 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 win_setheight(height);
2359 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2360 if (win_valid(win))
2361 prevwin = win;
2362 }
2363
2364 /*
2365 * Fill the buffer with the quickfix list.
2366 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002367 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002369 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 curwin->w_cursor.col = 0;
2371 check_cursor();
2372 update_topline(); /* scroll to show the line */
2373}
2374
2375/*
2376 * Return the number of the current entry (line number in the quickfix
2377 * window).
2378 */
2379 linenr_T
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002380qf_current_entry(wp)
2381 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002383 qf_info_T *qi = &ql_info;
2384
2385 if (IS_LL_WINDOW(wp))
2386 /* In the location list window, use the referenced location list */
2387 qi = wp->w_llist_ref;
2388
2389 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390}
2391
2392/*
2393 * Update the cursor position in the quickfix window to the current error.
2394 * Return TRUE if there is a quickfix window.
2395 */
2396 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002397qf_win_pos_update(qi, old_qf_index)
2398 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 int old_qf_index; /* previous qf_index or zero */
2400{
2401 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002402 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403
2404 /*
2405 * Put the cursor on the current error in the quickfix window, so that
2406 * it's viewable.
2407 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002408 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 if (win != NULL
2410 && qf_index <= win->w_buffer->b_ml.ml_line_count
2411 && old_qf_index != qf_index)
2412 {
2413 win_T *old_curwin = curwin;
2414
2415 curwin = win;
2416 curbuf = win->w_buffer;
2417 if (qf_index > old_qf_index)
2418 {
2419 curwin->w_redraw_top = old_qf_index;
2420 curwin->w_redraw_bot = qf_index;
2421 }
2422 else
2423 {
2424 curwin->w_redraw_top = qf_index;
2425 curwin->w_redraw_bot = old_qf_index;
2426 }
2427 curwin->w_cursor.lnum = qf_index;
2428 curwin->w_cursor.col = 0;
2429 update_topline(); /* scroll to show the line */
2430 redraw_later(VALID);
2431 curwin->w_redr_status = TRUE; /* update ruler */
2432 curwin = old_curwin;
2433 curbuf = curwin->w_buffer;
2434 }
2435 return win != NULL;
2436}
2437
2438/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002439 * Check whether the given window is displaying the specified quickfix/location
2440 * list buffer
2441 */
2442 static int
2443is_qf_win(win, qi)
2444 win_T *win;
2445 qf_info_T *qi;
2446{
2447 /*
2448 * A window displaying the quickfix buffer will have the w_llist_ref field
2449 * set to NULL.
2450 * A window displaying a location list buffer will have the w_llist_ref
2451 * pointing to the location list.
2452 */
2453 if (bt_quickfix(win->w_buffer))
2454 if ((qi == &ql_info && win->w_llist_ref == NULL)
2455 || (qi != &ql_info && win->w_llist_ref == qi))
2456 return TRUE;
2457
2458 return FALSE;
2459}
2460
2461/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002462 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00002463 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002464 */
2465 static win_T *
2466qf_find_win(qi)
2467 qf_info_T *qi;
2468{
2469 win_T *win;
2470
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002471 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002472 if (is_qf_win(win, qi))
2473 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002474
2475 return win;
2476}
2477
2478/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002479 * Find a quickfix buffer.
2480 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 */
2482 static buf_T *
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002483qf_find_buf(qi)
2484 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485{
Bram Moolenaar9c102382006-05-03 21:26:49 +00002486 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002487 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488
Bram Moolenaar9c102382006-05-03 21:26:49 +00002489 FOR_ALL_TAB_WINDOWS(tp, win)
2490 if (is_qf_win(win, qi))
2491 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002492
Bram Moolenaar9c102382006-05-03 21:26:49 +00002493 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494}
2495
2496/*
2497 * Find the quickfix buffer. If it exists, update the contents.
2498 */
2499 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002500qf_update_buffer(qi)
2501 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502{
2503 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505
2506 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002507 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 if (buf != NULL)
2509 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 /* set curwin/curbuf to buf and save a few things */
2511 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002513 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 /* restore curwin/curbuf and a few other things */
2516 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002518 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 }
2520}
2521
2522/*
2523 * Fill current buffer with quickfix errors, replacing any previous contents.
2524 * curbuf must be the quickfix buffer!
2525 */
2526 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002527qf_fill_buffer(qi)
2528 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002530 linenr_T lnum;
2531 qfline_T *qfp;
2532 buf_T *errbuf;
2533 int len;
2534 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535
2536 /* delete all existing lines */
2537 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2538 (void)ml_delete((linenr_T)1, FALSE);
2539
2540 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002541 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 {
2543 /* Add one line for each error */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002544 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2545 for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 {
2547 if (qfp->qf_fnum != 0
2548 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2549 && errbuf->b_fname != NULL)
2550 {
2551 if (qfp->qf_type == 1) /* :helpgrep */
2552 STRCPY(IObuff, gettail(errbuf->b_fname));
2553 else
2554 STRCPY(IObuff, errbuf->b_fname);
2555 len = (int)STRLEN(IObuff);
2556 }
2557 else
2558 len = 0;
2559 IObuff[len++] = '|';
2560
2561 if (qfp->qf_lnum > 0)
2562 {
2563 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2564 len += (int)STRLEN(IObuff + len);
2565
2566 if (qfp->qf_col > 0)
2567 {
2568 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2569 len += (int)STRLEN(IObuff + len);
2570 }
2571
2572 sprintf((char *)IObuff + len, "%s",
2573 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2574 len += (int)STRLEN(IObuff + len);
2575 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002576 else if (qfp->qf_pattern != NULL)
2577 {
2578 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2579 len += (int)STRLEN(IObuff + len);
2580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 IObuff[len++] = '|';
2582 IObuff[len++] = ' ';
2583
2584 /* Remove newlines and leading whitespace from the text.
2585 * For an unrecognized line keep the indent, the compiler may
2586 * mark a word with ^^^^. */
2587 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2588 IObuff + len, IOSIZE - len);
2589
2590 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2591 == FAIL)
2592 break;
2593 qfp = qfp->qf_next;
2594 }
2595 /* Delete the empty line which is now at the end */
2596 (void)ml_delete(lnum + 1, FALSE);
2597 }
2598
2599 /* correct cursor position */
2600 check_lnums(TRUE);
2601
2602 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2603 * resembles reading a file into a buffer, it's more logical when using
2604 * autocommands. */
2605 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2606 curbuf->b_p_ma = FALSE;
2607
2608#ifdef FEAT_AUTOCMD
2609 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2610 FALSE, curbuf);
2611 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2612 FALSE, curbuf);
2613#endif
2614
2615 /* make sure it will be redrawn */
2616 redraw_curbuf_later(NOT_VALID);
2617
2618 /* Restore KeyTyped, setting 'filetype' may reset it. */
2619 KeyTyped = old_KeyTyped;
2620}
2621
2622#endif /* FEAT_WINDOWS */
2623
2624/*
2625 * Return TRUE if "buf" is the quickfix buffer.
2626 */
2627 int
2628bt_quickfix(buf)
2629 buf_T *buf;
2630{
2631 return (buf->b_p_bt[0] == 'q');
2632}
2633
2634/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002635 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2636 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 */
2638 int
2639bt_nofile(buf)
2640 buf_T *buf;
2641{
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002642 return (buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2643 || buf->b_p_bt[0] == 'a';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644}
2645
2646/*
2647 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2648 */
2649 int
2650bt_dontwrite(buf)
2651 buf_T *buf;
2652{
2653 return (buf->b_p_bt[0] == 'n');
2654}
2655
2656 int
2657bt_dontwrite_msg(buf)
2658 buf_T *buf;
2659{
2660 if (bt_dontwrite(buf))
2661 {
2662 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2663 return TRUE;
2664 }
2665 return FALSE;
2666}
2667
2668/*
2669 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2670 * and 'bufhidden'.
2671 */
2672 int
2673buf_hide(buf)
2674 buf_T *buf;
2675{
2676 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2677 switch (buf->b_p_bh[0])
2678 {
2679 case 'u': /* "unload" */
2680 case 'w': /* "wipe" */
2681 case 'd': return FALSE; /* "delete" */
2682 case 'h': return TRUE; /* "hide" */
2683 }
2684 return (p_hid || cmdmod.hide);
2685}
2686
2687/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002688 * Return TRUE when using ":vimgrep" for ":grep".
2689 */
2690 int
Bram Moolenaar81695252004-12-29 20:58:21 +00002691grep_internal(cmdidx)
2692 cmdidx_T cmdidx;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002693{
Bram Moolenaar754b5602006-02-09 23:53:20 +00002694 return ((cmdidx == CMD_grep
2695 || cmdidx == CMD_lgrep
2696 || cmdidx == CMD_grepadd
2697 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002698 && STRCMP("internal",
2699 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2700}
2701
2702/*
Bram Moolenaara6557602006-02-04 22:43:20 +00002703 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 */
2705 void
2706ex_make(eap)
2707 exarg_T *eap;
2708{
Bram Moolenaar7c626922005-02-07 22:01:03 +00002709 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 char_u *cmd;
2711 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00002712 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002713 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002714 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002715#ifdef FEAT_AUTOCMD
2716 char_u *au_name = NULL;
2717
2718 switch (eap->cmdidx)
2719 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00002720 case CMD_make: au_name = (char_u *)"make"; break;
2721 case CMD_lmake: au_name = (char_u *)"lmake"; break;
2722 case CMD_grep: au_name = (char_u *)"grep"; break;
2723 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
2724 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
2725 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002726 default: break;
2727 }
2728 if (au_name != NULL)
2729 {
2730 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2731 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00002732# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00002733 if (did_throw || force_abort)
2734 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00002735# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002736 }
2737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738
Bram Moolenaar86b68352004-12-27 21:59:20 +00002739 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
Bram Moolenaar81695252004-12-29 20:58:21 +00002740 if (grep_internal(eap->cmdidx))
Bram Moolenaar86b68352004-12-27 21:59:20 +00002741 {
2742 ex_vimgrep(eap);
2743 return;
2744 }
2745
Bram Moolenaara6557602006-02-04 22:43:20 +00002746 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
2747 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00002748 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00002749
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002751 fname = get_mef_name();
2752 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002754 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755
2756 /*
2757 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2758 */
2759 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2760 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002761 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 cmd = alloc(len);
2763 if (cmd == NULL)
2764 return;
2765 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2766 (char *)p_shq);
2767 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002768 append_redir(cmd, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 /*
2770 * Output a newline if there's something else than the :make command that
2771 * was typed (in which case the cursor is in column 0).
2772 */
2773 if (msg_col == 0)
2774 msg_didout = FALSE;
2775 msg_start();
2776 MSG_PUTS(":!");
2777 msg_outtrans(cmd); /* show what we are doing */
2778
2779 /* let the shell know if we are redirecting output or not */
2780 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2781
2782#ifdef AMIGA
2783 out_flush();
2784 /* read window status report and redraw before message */
2785 (void)char_avail();
2786#endif
2787
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002788 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00002789 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
2790 (eap->cmdidx != CMD_grepadd
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002791 && eap->cmdidx != CMD_lgrepadd));
2792#ifdef FEAT_AUTOCMD
2793 if (au_name != NULL)
2794 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2795 curbuf->b_fname, TRUE, curbuf);
2796#endif
2797 if (res > 0 && !eap->forceit)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002798 {
2799 if (wp != NULL)
2800 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002801 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803
Bram Moolenaar7c626922005-02-07 22:01:03 +00002804 mch_remove(fname);
2805 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 vim_free(cmd);
2807}
2808
2809/*
2810 * Return the name for the errorfile, in allocated memory.
2811 * Find a new unique name when 'makeef' contains "##".
2812 * Returns NULL for error.
2813 */
2814 static char_u *
2815get_mef_name()
2816{
2817 char_u *p;
2818 char_u *name;
2819 static int start = -1;
2820 static int off = 0;
2821#ifdef HAVE_LSTAT
2822 struct stat sb;
2823#endif
2824
2825 if (*p_mef == NUL)
2826 {
2827 name = vim_tempname('e');
2828 if (name == NULL)
2829 EMSG(_(e_notmp));
2830 return name;
2831 }
2832
2833 for (p = p_mef; *p; ++p)
2834 if (p[0] == '#' && p[1] == '#')
2835 break;
2836
2837 if (*p == NUL)
2838 return vim_strsave(p_mef);
2839
2840 /* Keep trying until the name doesn't exist yet. */
2841 for (;;)
2842 {
2843 if (start == -1)
2844 start = mch_get_pid();
2845 else
2846 off += 19;
2847
2848 name = alloc((unsigned)STRLEN(p_mef) + 30);
2849 if (name == NULL)
2850 break;
2851 STRCPY(name, p_mef);
2852 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2853 STRCAT(name, p + 2);
2854 if (mch_getperm(name) < 0
2855#ifdef HAVE_LSTAT
2856 /* Don't accept a symbolic link, its a security risk. */
2857 && mch_lstat((char *)name, &sb) < 0
2858#endif
2859 )
2860 break;
2861 vim_free(name);
2862 }
2863 return name;
2864}
2865
2866/*
2867 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002868 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 */
2870 void
2871ex_cc(eap)
2872 exarg_T *eap;
2873{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002874 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002875
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002876 if (eap->cmdidx == CMD_ll
2877 || eap->cmdidx == CMD_lrewind
2878 || eap->cmdidx == CMD_lfirst
2879 || eap->cmdidx == CMD_llast)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002880 {
2881 qi = GET_LOC_LIST(curwin);
2882 if (qi == NULL)
2883 {
2884 EMSG(_(e_loclist));
2885 return;
2886 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002887 }
2888
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002889 qf_jump(qi, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 eap->addr_count > 0
2891 ? (int)eap->line2
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002892 : (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 ? 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002894 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
2895 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 ? 1
2897 : 32767,
2898 eap->forceit);
2899}
2900
2901/*
2902 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002903 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 */
2905 void
2906ex_cnext(eap)
2907 exarg_T *eap;
2908{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002909 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002910
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002911 if (eap->cmdidx == CMD_lnext
2912 || eap->cmdidx == CMD_lNext
2913 || eap->cmdidx == CMD_lprevious
2914 || eap->cmdidx == CMD_lnfile
2915 || eap->cmdidx == CMD_lNfile
2916 || eap->cmdidx == CMD_lpfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002917 {
2918 qi = GET_LOC_LIST(curwin);
2919 if (qi == NULL)
2920 {
2921 EMSG(_(e_loclist));
2922 return;
2923 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002924 }
2925
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002926 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 ? FORWARD
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002928 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002930 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
2931 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 ? BACKWARD_FILE
2933 : BACKWARD,
2934 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
2935}
2936
2937/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002938 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002939 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 */
2941 void
2942ex_cfile(eap)
2943 exarg_T *eap;
2944{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002945 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002946 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002947
2948 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
2949 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002950 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002951
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002953 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002954
2955 /*
2956 * This function is used by the :cfile, :cgetfile and :caddfile
2957 * commands.
2958 * :cfile always creates a new quickfix list and jumps to the
2959 * first error.
2960 * :cgetfile creates a new quickfix list but doesn't jump to the
2961 * first error.
2962 * :caddfile adds to an existing quickfix list. If there is no
2963 * quickfix list then a new list is created.
2964 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002965 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
2966 && eap->cmdidx != CMD_laddfile)) > 0
2967 && (eap->cmdidx == CMD_cfile
2968 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002969 {
2970 if (wp != NULL)
2971 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002972 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974}
2975
2976/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002977 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00002978 * ":vimgrepadd {pattern} file(s)"
2979 * ":lvimgrep {pattern} file(s)"
2980 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00002981 */
2982 void
2983ex_vimgrep(eap)
2984 exarg_T *eap;
2985{
Bram Moolenaar81695252004-12-29 20:58:21 +00002986 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002987 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002988 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00002989 char_u *fname;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002990 char_u *s;
2991 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002992 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00002993 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002994 qfline_T *prevp = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002995 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00002996 buf_T *buf;
2997 int duplicate_name = FALSE;
2998 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00002999 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003000 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003001 buf_T *first_match_buf = NULL;
3002 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003003 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003004#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3005 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003006#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003007 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003008 int flags = 0;
3009 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003010 long tomatch;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003011 char_u dirname_start[MAXPATHL];
3012 char_u dirname_now[MAXPATHL];
3013 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003014#ifdef FEAT_AUTOCMD
3015 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003016
3017 switch (eap->cmdidx)
3018 {
3019 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003020 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003021 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003022 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003023 default: break;
3024 }
3025 if (au_name != NULL)
3026 {
3027 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3028 curbuf->b_fname, TRUE, curbuf);
3029 if (did_throw || force_abort)
3030 return;
3031 }
3032#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003033
Bram Moolenaar754b5602006-02-09 23:53:20 +00003034 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003035 || eap->cmdidx == CMD_lvimgrep
3036 || eap->cmdidx == CMD_lgrepadd
3037 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003038 {
3039 qi = ll_get_or_alloc_list(curwin);
3040 if (qi == NULL)
3041 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003042 }
3043
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003044 if (eap->addr_count > 0)
3045 tomatch = eap->line2;
3046 else
3047 tomatch = MAXLNUM;
3048
Bram Moolenaar81695252004-12-29 20:58:21 +00003049 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003050 regmatch.regprog = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003051 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003052 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003053 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003054 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003055 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003056 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003057 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003058 if (regmatch.regprog == NULL)
3059 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003060 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003061 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003062
3063 p = skipwhite(p);
3064 if (*p == NUL)
3065 {
3066 EMSG(_("E683: File name missing or invalid pattern"));
3067 goto theend;
3068 }
3069
Bram Moolenaar754b5602006-02-09 23:53:20 +00003070 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003071 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003072 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003073 /* make place for a new list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003074 qf_new_list(qi);
3075 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003076 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003077 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003078 prevp->qf_next != prevp; prevp = prevp->qf_next)
3079 ;
3080
3081 /* parse the list of arguments */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003082 if (get_arglist_exp(p, &fcount, &fnames) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003083 goto theend;
3084 if (fcount == 0)
3085 {
3086 EMSG(_(e_nomatch));
3087 goto theend;
3088 }
3089
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003090 /* Remember the current directory, because a BufRead autocommand that does
3091 * ":lcd %:p:h" changes the meaning of short path names. */
3092 mch_dirname(dirname_start, MAXPATHL);
3093
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003094 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003095 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003096 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003097 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003098 if (time(NULL) > seconds)
3099 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003100 /* Display the file name every second or so, show the user we are
3101 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003102 seconds = time(NULL);
3103 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003104 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003105 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003106 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003107 else
3108 {
3109 msg_outtrans(p);
3110 vim_free(p);
3111 }
3112 msg_clr_eos();
3113 msg_didout = FALSE; /* overwrite this message */
3114 msg_nowait = TRUE; /* don't wait for this message */
3115 msg_col = 0;
3116 out_flush();
3117 }
3118
Bram Moolenaar81695252004-12-29 20:58:21 +00003119 buf = buflist_findname_exp(fnames[fi]);
3120 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3121 {
3122 /* Remember that a buffer with this name already exists. */
3123 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003124 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003125 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003126
3127#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3128 /* Don't do Filetype autocommands to avoid loading syntax and
3129 * indent scripts, a great speed improvement. */
3130 save_ei = au_event_disable(",Filetype");
3131#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003132 /* Don't use modelines here, it's useless. */
3133 save_mls = p_mls;
3134 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00003135
3136 /* Load file into a buffer, so that 'fileencoding' is detected,
3137 * autocommands applied, etc. */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003138 buf = load_dummy_buffer(fname);
3139
3140 /* When autocommands changed directory: go back. We assume it was
3141 * ":lcd %:p:h". */
3142 mch_dirname(dirname_now, MAXPATHL);
3143 if (STRCMP(dirname_start, dirname_now) != 0)
3144 {
3145 exarg_T ea;
3146
3147 ea.arg = dirname_start;
3148 ea.cmdidx = CMD_lcd;
3149 ex_cd(&ea);
3150 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003151
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003152 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003153#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3154 au_event_restore(save_ei);
3155#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003156 }
3157 else
3158 /* Use existing, loaded buffer. */
3159 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003160
Bram Moolenaar81695252004-12-29 20:58:21 +00003161 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003162 {
3163 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003164 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003165 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003166 else
3167 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003168 /* Try for a match in all lines of the buffer.
3169 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003170 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003171 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3172 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003173 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003174 col = 0;
3175 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003176 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003177 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003178 ;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003179 if (qf_add_entry(qi, &prevp,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003180 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003181 fname,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003182 0,
Bram Moolenaar81695252004-12-29 20:58:21 +00003183 ml_get_buf(buf,
3184 regmatch.startpos[0].lnum + lnum, FALSE),
3185 regmatch.startpos[0].lnum + lnum,
3186 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003187 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003188 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003189 0, /* nr */
3190 0, /* type */
3191 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003192 ) == FAIL)
3193 {
3194 got_int = TRUE;
3195 break;
3196 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003197 found_match = TRUE;
3198 if (--tomatch == 0)
3199 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003200 if ((flags & VGR_GLOBAL) == 0
3201 || regmatch.endpos[0].lnum > 0)
3202 break;
3203 col = regmatch.endpos[0].col
3204 + (col == regmatch.endpos[0].col);
3205 if (col > STRLEN(ml_get_buf(buf, lnum, FALSE)))
3206 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003207 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003208 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003209 if (got_int)
3210 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003211 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003212
3213 if (using_dummy)
3214 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003215 if (found_match && first_match_buf == NULL)
3216 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003217 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003218 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003219 /* Never keep a dummy buffer if there is another buffer
3220 * with the same name. */
3221 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003222 buf = NULL;
3223 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00003224 else if (!cmdmod.hide
3225 || buf->b_p_bh[0] == 'u' /* "unload" */
3226 || buf->b_p_bh[0] == 'w' /* "wipe" */
3227 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00003228 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003229 /* When no match was found we don't need to remember the
3230 * buffer, wipe it out. If there was a match and it
3231 * wasn't the first one or we won't jump there: only
3232 * unload the buffer.
3233 * Ignore 'hidden' here, because it may lead to having too
3234 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003235 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003236 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003237 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003238 buf = NULL;
3239 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003240 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003241 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003242 unload_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003243 buf = NULL;
3244 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003245 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003246
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003247 if (buf != NULL)
3248 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003249 /* If the buffer is still loaded we need to use the
3250 * directory we jumped to below. */
3251 if (buf == first_match_buf
3252 && target_dir == NULL
3253 && STRCMP(dirname_start, dirname_now) != 0)
3254 target_dir = vim_strsave(dirname_now);
3255
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003256 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003257 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00003258 * need to be done (again). But not the window-local
3259 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003260 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003261#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003262 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3263 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003264#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00003265 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003266 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003267 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003268 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003269 }
3270 }
3271
3272 FreeWild(fcount, fnames);
3273
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003274 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3275 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3276 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003277
3278#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003279 qf_update_buffer(qi);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003280#endif
3281
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003282#ifdef FEAT_AUTOCMD
3283 if (au_name != NULL)
3284 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3285 curbuf->b_fname, TRUE, curbuf);
3286#endif
3287
Bram Moolenaar86b68352004-12-27 21:59:20 +00003288 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003289 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003290 {
3291 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003292 {
3293 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003294 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003295 if (buf != curbuf)
3296 /* If we jumped to another buffer redrawing will already be
3297 * taken care of. */
3298 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003299
3300 /* Jump to the directory used after loading the buffer. */
3301 if (curbuf == first_match_buf && target_dir != NULL)
3302 {
3303 exarg_T ea;
3304
3305 ea.arg = target_dir;
3306 ea.cmdidx = CMD_lcd;
3307 ex_cd(&ea);
3308 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003309 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003310 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003311 else
3312 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003313
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003314 /* If we loaded a dummy buffer into the current window, the autocommands
3315 * may have messed up things, need to redraw and recompute folds. */
3316 if (redraw_for_dummy)
3317 {
3318#ifdef FEAT_FOLDING
3319 foldUpdateAll(curwin);
3320#else
3321 redraw_later(NOT_VALID);
3322#endif
3323 }
3324
Bram Moolenaar86b68352004-12-27 21:59:20 +00003325theend:
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003326 vim_free(target_dir);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003327 vim_free(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003328}
3329
3330/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00003331 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00003332 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003333 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3334 * If "s" is not NULL terminate the pattern with a NUL.
3335 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00003336 */
3337 char_u *
Bram Moolenaar05159a02005-02-26 23:04:13 +00003338skip_vimgrep_pat(p, s, flags)
3339 char_u *p;
3340 char_u **s;
3341 int *flags;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003342{
3343 int c;
3344
3345 if (vim_isIDc(*p))
3346 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003347 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003348 if (s != NULL)
3349 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003350 p = skiptowhite(p);
3351 if (s != NULL && *p != NUL)
3352 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003353 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003354 else
3355 {
3356 /* ":vimgrep /pattern/[g][j] fname" */
3357 if (s != NULL)
3358 *s = p + 1;
3359 c = *p;
3360 p = skip_regexp(p + 1, c, TRUE, NULL);
3361 if (*p != c)
3362 return NULL;
3363
3364 /* Truncate the pattern. */
3365 if (s != NULL)
3366 *p = NUL;
3367 ++p;
3368
3369 /* Find the flags */
3370 while (*p == 'g' || *p == 'j')
3371 {
3372 if (flags != NULL)
3373 {
3374 if (*p == 'g')
3375 *flags |= VGR_GLOBAL;
3376 else
3377 *flags |= VGR_NOJUMP;
3378 }
3379 ++p;
3380 }
3381 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00003382 return p;
3383}
3384
3385/*
Bram Moolenaar81695252004-12-29 20:58:21 +00003386 * Load file "fname" into a dummy buffer and return the buffer pointer.
3387 * Returns NULL if it fails.
3388 * Must call unload_dummy_buffer() or wipe_dummy_buffer() later!
3389 */
3390 static buf_T *
3391load_dummy_buffer(fname)
3392 char_u *fname;
3393{
3394 buf_T *newbuf;
3395 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003396 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00003397
3398 /* Allocate a buffer without putting it in the buffer list. */
3399 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
3400 if (newbuf == NULL)
3401 return NULL;
3402
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00003403 /* Init the options. */
3404 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
3405
Bram Moolenaar81695252004-12-29 20:58:21 +00003406 /* set curwin/curbuf to buf and save a few things */
3407 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00003408
3409 /* Need to set the filename for autocommands. */
3410 (void)setfname(curbuf, fname, NULL, FALSE);
3411
Bram Moolenaar4770d092006-01-12 23:22:24 +00003412 if (ml_open(curbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00003413 {
3414 /* Create swap file now to avoid the ATTENTION message. */
3415 check_need_swap(TRUE);
3416
3417 /* Remove the "dummy" flag, otherwise autocommands may not
3418 * work. */
3419 curbuf->b_flags &= ~BF_DUMMY;
3420
3421 if (readfile(fname, NULL,
3422 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
3423 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00003424 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00003425 && !(curbuf->b_flags & BF_NEW))
3426 {
3427 failed = FALSE;
3428 if (curbuf != newbuf)
3429 {
3430 /* Bloody autocommands changed the buffer! */
3431 if (buf_valid(newbuf))
3432 wipe_buffer(newbuf, FALSE);
3433 newbuf = curbuf;
3434 }
3435 }
3436 }
3437
Bram Moolenaar81695252004-12-29 20:58:21 +00003438 /* restore curwin/curbuf and a few other things */
3439 aucmd_restbuf(&aco);
Bram Moolenaar81695252004-12-29 20:58:21 +00003440
3441 if (!buf_valid(newbuf))
3442 return NULL;
3443 if (failed)
3444 {
3445 wipe_dummy_buffer(newbuf);
3446 return NULL;
3447 }
3448 return newbuf;
3449}
3450
3451/*
3452 * Wipe out the dummy buffer that load_dummy_buffer() created.
3453 */
3454 static void
3455wipe_dummy_buffer(buf)
3456 buf_T *buf;
3457{
3458 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003459 {
3460#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3461 cleanup_T cs;
3462
3463 /* Reset the error/interrupt/exception state here so that aborting()
3464 * returns FALSE when wiping out the buffer. Otherwise it doesn't
3465 * work when got_int is set. */
3466 enter_cleanup(&cs);
3467#endif
3468
Bram Moolenaar81695252004-12-29 20:58:21 +00003469 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00003470
3471#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3472 /* Restore the error/interrupt/exception state if not discarded by a
3473 * new aborting error, interrupt, or uncaught exception. */
3474 leave_cleanup(&cs);
3475#endif
3476 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003477}
3478
3479/*
3480 * Unload the dummy buffer that load_dummy_buffer() created.
3481 */
3482 static void
3483unload_dummy_buffer(buf)
3484 buf_T *buf;
3485{
3486 if (curbuf != buf) /* safety check */
3487 close_buffer(NULL, buf, DOBUF_UNLOAD);
3488}
3489
Bram Moolenaar05159a02005-02-26 23:04:13 +00003490#if defined(FEAT_EVAL) || defined(PROTO)
3491/*
3492 * Add each quickfix error to list "list" as a dictionary.
3493 */
3494 int
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003495get_errorlist(wp, list)
3496 win_T *wp;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003497 list_T *list;
3498{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003499 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003500 dict_T *dict;
3501 char_u buf[2];
3502 qfline_T *qfp;
3503 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003504 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003505
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003506 if (wp != NULL)
3507 {
3508 qi = GET_LOC_LIST(wp);
3509 if (qi == NULL)
3510 return FAIL;
3511 }
3512
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003513 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003514 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003515 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003516
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003517 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3518 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003519 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003520 /* Handle entries with a non-existing buffer number. */
3521 bufnum = qfp->qf_fnum;
3522 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3523 bufnum = 0;
3524
Bram Moolenaar05159a02005-02-26 23:04:13 +00003525 if ((dict = dict_alloc()) == NULL)
3526 return FAIL;
3527 if (list_append_dict(list, dict) == FAIL)
3528 return FAIL;
3529
3530 buf[0] = qfp->qf_type;
3531 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003532 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003533 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
3534 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
3535 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
3536 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00003537 || dict_add_nr_str(dict, "pattern", 0L,
3538 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
3539 || dict_add_nr_str(dict, "text", 0L,
3540 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003541 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
3542 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
3543 return FAIL;
3544
3545 qfp = qfp->qf_next;
3546 }
3547 return OK;
3548}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003549
3550/*
3551 * Populate the quickfix list with the items supplied in the list
3552 * of dictionaries.
3553 */
3554 int
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003555set_errorlist(wp, list, action)
3556 win_T *wp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003557 list_T *list;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003558 int action;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003559{
3560 listitem_T *li;
3561 dict_T *d;
3562 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003563 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003564 long lnum;
3565 int col, nr;
3566 int vcol;
3567 qfline_T *prevp = NULL;
3568 int valid, status;
3569 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003570 qf_info_T *qi = &ql_info;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003571 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003572
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003573 if (wp != NULL)
3574 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00003575 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003576 if (qi == NULL)
3577 return FAIL;
3578 }
3579
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003580 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003581 /* make place for a new list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003582 qf_new_list(qi);
3583 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003584 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003585 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003586 prevp->qf_next != prevp; prevp = prevp->qf_next)
3587 ;
3588 else if (action == 'r')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003589 qf_free(qi, qi->qf_curlist);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003590
3591 for (li = list->lv_first; li != NULL; li = li->li_next)
3592 {
3593 if (li->li_tv.v_type != VAR_DICT)
3594 continue; /* Skip non-dict items */
3595
3596 d = li->li_tv.vval.v_dict;
3597 if (d == NULL)
3598 continue;
3599
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003600 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003601 bufnum = get_dict_number(d, (char_u *)"bufnr");
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003602 lnum = get_dict_number(d, (char_u *)"lnum");
3603 col = get_dict_number(d, (char_u *)"col");
3604 vcol = get_dict_number(d, (char_u *)"vcol");
3605 nr = get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003606 type = get_dict_string(d, (char_u *)"type", TRUE);
3607 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
3608 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003609 if (text == NULL)
3610 text = vim_strsave((char_u *)"");
3611
3612 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003613 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003614 valid = FALSE;
3615
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003616 /* Mark entries with non-existing buffer number as not valid. Give the
3617 * error message only once. */
3618 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3619 {
3620 if (!did_bufnr_emsg)
3621 {
3622 did_bufnr_emsg = TRUE;
3623 EMSGN(_("E92: Buffer %ld not found"), bufnum);
3624 }
3625 valid = FALSE;
3626 bufnum = 0;
3627 }
3628
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003629 status = qf_add_entry(qi, &prevp,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003630 NULL, /* dir */
3631 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003632 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003633 text,
3634 lnum,
3635 col,
3636 vcol, /* vis_col */
3637 pattern, /* search pattern */
3638 nr,
3639 type == NULL ? NUL : *type,
3640 valid);
3641
3642 vim_free(filename);
3643 vim_free(pattern);
3644 vim_free(text);
3645 vim_free(type);
3646
3647 if (status == FAIL)
3648 {
3649 retval = FAIL;
3650 break;
3651 }
3652 }
3653
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003654 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3655 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3656 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003657
3658#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003659 qf_update_buffer(qi);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003660#endif
3661
3662 return retval;
3663}
Bram Moolenaar05159a02005-02-26 23:04:13 +00003664#endif
3665
Bram Moolenaar81695252004-12-29 20:58:21 +00003666/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003667 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003668 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003669 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003670 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003671 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003672 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00003673 */
3674 void
3675ex_cbuffer(eap)
3676 exarg_T *eap;
3677{
3678 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003679 qf_info_T *qi = &ql_info;
3680
Bram Moolenaardb552d602006-03-23 22:59:57 +00003681 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
3682 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003683 {
3684 qi = ll_get_or_alloc_list(curwin);
3685 if (qi == NULL)
3686 return;
3687 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003688
3689 if (*eap->arg == NUL)
3690 buf = curbuf;
3691 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
3692 buf = buflist_findnr(atoi((char *)eap->arg));
3693 if (buf == NULL)
3694 EMSG(_(e_invarg));
3695 else if (buf->b_ml.ml_mfp == NULL)
3696 EMSG(_("E681: Buffer is not loaded"));
3697 else
3698 {
3699 if (eap->addr_count == 0)
3700 {
3701 eap->line1 = 1;
3702 eap->line2 = buf->b_ml.ml_line_count;
3703 }
3704 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
3705 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
3706 EMSG(_(e_invrange));
3707 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00003708 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00003709 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
3710 (eap->cmdidx != CMD_caddbuffer
3711 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar754b5602006-02-09 23:53:20 +00003712 eap->line1, eap->line2) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00003713 && (eap->cmdidx == CMD_cbuffer
3714 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00003715 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
3716 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003717 }
3718}
3719
Bram Moolenaar1e015462005-09-25 22:16:38 +00003720#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003721/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00003722 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
3723 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003724 */
3725 void
3726ex_cexpr(eap)
3727 exarg_T *eap;
3728{
3729 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003730 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003731
Bram Moolenaardb552d602006-03-23 22:59:57 +00003732 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
3733 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003734 {
3735 qi = ll_get_or_alloc_list(curwin);
3736 if (qi == NULL)
3737 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003738 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003739
Bram Moolenaar4770d092006-01-12 23:22:24 +00003740 /* Evaluate the expression. When the result is a string or a list we can
3741 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003742 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003743 if (tv != NULL)
3744 {
3745 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
3746 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
3747 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00003748 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
3749 (eap->cmdidx != CMD_caddexpr
3750 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar4770d092006-01-12 23:22:24 +00003751 (linenr_T)0, (linenr_T)0) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00003752 && (eap->cmdidx == CMD_cexpr
3753 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003754 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003755 }
3756 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003757 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00003758 free_tv(tv);
3759 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003760}
Bram Moolenaar1e015462005-09-25 22:16:38 +00003761#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003762
3763/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 * ":helpgrep {pattern}"
3765 */
3766 void
3767ex_helpgrep(eap)
3768 exarg_T *eap;
3769{
3770 regmatch_T regmatch;
3771 char_u *save_cpo;
3772 char_u *p;
3773 int fcount;
3774 char_u **fnames;
3775 FILE *fd;
3776 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003777 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003779#ifdef FEAT_MULTI_LANG
3780 char_u *lang;
3781#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003782 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003783 int new_qi = FALSE;
3784 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785
3786 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
3787 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00003788 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003790#ifdef FEAT_MULTI_LANG
3791 /* Check for a specified language */
3792 lang = check_help_lang(eap->arg);
3793#endif
3794
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003795 if (eap->cmdidx == CMD_lhelpgrep)
3796 {
3797 /* Find an existing help window */
3798 FOR_ALL_WINDOWS(wp)
3799 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
3800 break;
3801
3802 if (wp == NULL) /* Help window not found */
3803 qi = NULL;
3804 else
3805 qi = wp->w_llist;
3806
3807 if (qi == NULL)
3808 {
3809 /* Allocate a new location list for help text matches */
3810 if ((qi = ll_new_list()) == NULL)
3811 return;
3812 new_qi = TRUE;
3813 }
3814 }
3815
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
3817 regmatch.rm_ic = FALSE;
3818 if (regmatch.regprog != NULL)
3819 {
3820 /* create a new quickfix list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003821 qf_new_list(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822
3823 /* Go through all directories in 'runtimepath' */
3824 p = p_rtp;
3825 while (*p != NUL && !got_int)
3826 {
3827 copy_option_part(&p, NameBuff, MAXPATHL, ",");
3828
3829 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
3830 add_pathsep(NameBuff);
3831 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
3832 if (gen_expand_wildcards(1, &NameBuff, &fcount,
3833 &fnames, EW_FILE|EW_SILENT) == OK
3834 && fcount > 0)
3835 {
3836 for (fi = 0; fi < fcount && !got_int; ++fi)
3837 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003838#ifdef FEAT_MULTI_LANG
3839 /* Skip files for a different language. */
3840 if (lang != NULL
3841 && STRNICMP(lang, fnames[fi]
3842 + STRLEN(fnames[fi]) - 3, 2) != 0
3843 && !(STRNICMP(lang, "en", 2) == 0
3844 && STRNICMP("txt", fnames[fi]
3845 + STRLEN(fnames[fi]) - 3, 3) == 0))
3846 continue;
3847#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003848 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 if (fd != NULL)
3850 {
3851 lnum = 1;
3852 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
3853 {
3854 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
3855 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003856 int l = (int)STRLEN(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857
3858 /* remove trailing CR, LF, spaces, etc. */
3859 while (l > 0 && IObuff[l - 1] <= ' ')
3860 IObuff[--l] = NUL;
3861
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003862 if (qf_add_entry(qi, &prevp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 NULL, /* dir */
3864 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003865 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 IObuff,
3867 lnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00003868 (int)(regmatch.startp[0] - IObuff)
3869 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00003870 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003871 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 0, /* nr */
3873 1, /* type */
3874 TRUE /* valid */
3875 ) == FAIL)
3876 {
3877 got_int = TRUE;
3878 break;
3879 }
3880 }
3881 ++lnum;
3882 line_breakcheck();
3883 }
3884 fclose(fd);
3885 }
3886 }
3887 FreeWild(fcount, fnames);
3888 }
3889 }
3890 vim_free(regmatch.regprog);
3891
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003892 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3893 qi->qf_lists[qi->qf_curlist].qf_ptr =
3894 qi->qf_lists[qi->qf_curlist].qf_start;
3895 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 }
3897
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00003898 if (p_cpo == empty_option)
3899 p_cpo = save_cpo;
3900 else
3901 /* Darn, some plugin changed the value. */
3902 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903
3904#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003905 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906#endif
3907
3908 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003909 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003910 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003911 else
3912 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003913
3914 if (eap->cmdidx == CMD_lhelpgrep)
3915 {
3916 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00003917 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003918 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
3919 {
3920 if (new_qi)
3921 ll_free_all(&qi);
3922 }
3923 else if (curwin->w_llist == NULL)
3924 curwin->w_llist = qi;
3925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926}
3927
3928#endif /* FEAT_QUICKFIX */