blob: f9278a2f0d98dbcc5834ad930817d82791842c05 [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 */
82struct eformat
83{
84 regprog_T *prog; /* pre-formatted part of 'errorformat' */
85 struct eformat *next; /* pointer to next (NULL if last) */
86 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
87 char_u prefix; /* prefix of this format line: */
88 /* 'D' enter directory */
89 /* 'X' leave directory */
90 /* 'A' start of multi-line message */
91 /* 'E' error message */
92 /* 'W' warning message */
93 /* 'I' informational message */
94 /* 'C' continuation line */
95 /* 'Z' end of multi-line message */
96 /* 'G' general, unspecific message */
97 /* 'P' push file (partial) message */
98 /* 'Q' pop/quit file (partial) message */
99 /* 'O' overread (partial) message */
100 char_u flags; /* additional flags given in prefix */
101 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000102 /* '+' include whole line in message */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103};
104
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000105static 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));
106static void qf_new_list __ARGS((qf_info_T *qi));
107static int qf_add_entry __ARGS((qf_info_T *qi, qfline_T **prevp, char_u *dir, char_u *fname, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid));
108static void qf_msg __ARGS((qf_info_T *qi));
109static void qf_free __ARGS((qf_info_T *qi, int idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110static char_u *qf_types __ARGS((int, int));
111static int qf_get_fnum __ARGS((char_u *, char_u *));
112static char_u *qf_push_dir __ARGS((char_u *, struct dir_stack_T **));
113static char_u *qf_pop_dir __ARGS((struct dir_stack_T **));
114static char_u *qf_guess_filepath __ARGS((char_u *));
115static void qf_fmt_text __ARGS((char_u *text, char_u *buf, int bufsize));
116static void qf_clean_dir_stack __ARGS((struct dir_stack_T **));
117#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000118static int qf_win_pos_update __ARGS((qf_info_T *qi, int old_qf_index));
119static win_T *qf_find_win __ARGS((qf_info_T *qi));
120static buf_T *qf_find_buf __ARGS((qf_info_T *qi));
121static void qf_update_buffer __ARGS((qf_info_T *qi));
122static void qf_fill_buffer __ARGS((qf_info_T *qi));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123#endif
124static char_u *get_mef_name __ARGS((void));
Bram Moolenaar81695252004-12-29 20:58:21 +0000125static buf_T *load_dummy_buffer __ARGS((char_u *fname));
126static void wipe_dummy_buffer __ARGS((buf_T *buf));
127static void unload_dummy_buffer __ARGS((buf_T *buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000129/* Quickfix window check helper macro */
130#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
131/* Location list window check helper macro */
132#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
133/*
134 * Return location list for window 'wp'
135 * For location list window, return the referenced location list
136 */
137#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
138
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000140 * Read the errorfile "efile" into memory, line by line, building the error
141 * list.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 * Return -1 for error, number of errors for success.
143 */
144 int
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000145qf_init(wp, efile, errorformat, newlist)
146 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 char_u *efile;
148 char_u *errorformat;
149 int newlist; /* TRUE: start a new error list */
150{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000151 qf_info_T *qi = &ql_info;
152
Bram Moolenaar86b68352004-12-27 21:59:20 +0000153 if (efile == NULL)
154 return FAIL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000155
156 if (wp != NULL)
157 qi = GET_LOC_LIST(wp);
158
159 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar86b68352004-12-27 21:59:20 +0000160 (linenr_T)0, (linenr_T)0);
161}
162
163/*
164 * Read the errorfile "efile" into memory, line by line, building the error
165 * list.
166 * Alternative: when "efile" is null read errors from buffer "buf".
167 * Always use 'errorformat' from "buf" if there is a local value.
168 * Then lnumfirst and lnumlast specify the range of lines to use.
169 * Return -1 for error, number of errors for success.
170 */
171 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000172qf_init_ext(qi, efile, buf, tv, errorformat, newlist, lnumfirst, lnumlast)
173 qf_info_T *qi;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000174 char_u *efile;
175 buf_T *buf;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000176 typval_T *tv;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000177 char_u *errorformat;
178 int newlist; /* TRUE: start a new error list */
179 linenr_T lnumfirst; /* first line number to use */
180 linenr_T lnumlast; /* last line number to use */
181{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 char_u *namebuf;
183 char_u *errmsg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000184 char_u *pattern;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 char_u *fmtstr = NULL;
186 int col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000187 char_u use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 int type = 0;
189 int valid;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000190 linenr_T buflnum = lnumfirst;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 long lnum = 0L;
192 int enr = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000193 FILE *fd = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000194 qfline_T *qfprev = NULL; /* init to make SASC shut up */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 char_u *efmp;
196 struct eformat *fmt_first = NULL;
197 struct eformat *fmt_last = NULL;
198 struct eformat *fmt_ptr;
199 char_u *efm;
200 char_u *ptr;
201 char_u *srcptr;
202 int len;
203 int i;
204 int round;
205 int idx = 0;
206 int multiline = FALSE;
207 int multiignore = FALSE;
208 int multiscan = FALSE;
209 int retval = -1; /* default: return error flag */
210 char_u *directory = NULL;
211 char_u *currfile = NULL;
212 char_u *tail = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000213 char_u *p_str = NULL;
214 listitem_T *p_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 struct dir_stack_T *file_stack = NULL;
216 regmatch_T regmatch;
217 static struct fmtpattern
218 {
219 char_u convchar;
220 char *pattern;
221 } fmt_pat[FMT_PATTERNS] =
222 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000223 {'f', ".\\+"}, /* only used when at end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 {'n', "\\d\\+"},
225 {'l', "\\d\\+"},
226 {'c', "\\d\\+"},
227 {'t', "."},
228 {'m', ".\\+"},
229 {'r', ".*"},
230 {'p', "[- .]*"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000231 {'v', "\\d\\+"},
232 {'s', ".\\+"}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 };
234
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 namebuf = alloc(CMDBUFFSIZE + 1);
236 errmsg = alloc(CMDBUFFSIZE + 1);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000237 pattern = alloc(CMDBUFFSIZE + 1);
238 if (namebuf == NULL || errmsg == NULL || pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 goto qf_init_end;
240
Bram Moolenaar86b68352004-12-27 21:59:20 +0000241 if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 {
243 EMSG2(_(e_openerrf), efile);
244 goto qf_init_end;
245 }
246
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000247 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 /* make place for a new list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000249 qf_new_list(qi);
250 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000252 for (qfprev = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 qfprev->qf_next != qfprev; qfprev = qfprev->qf_next)
254 ;
255
256/*
257 * Each part of the format string is copied and modified from errorformat to
258 * regex prog. Only a few % characters are allowed.
259 */
260 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000261 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000262 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 else
264 efm = errorformat;
265 /*
266 * Get some space to modify the format string into.
267 */
268 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
269 for (round = FMT_PATTERNS; round > 0; )
270 i += (int)STRLEN(fmt_pat[--round].pattern);
271#ifdef COLON_IN_FILENAME
272 i += 12; /* "%f" can become twelve chars longer */
273#else
274 i += 2; /* "%f" can become two chars longer */
275#endif
276 if ((fmtstr = alloc(i)) == NULL)
277 goto error2;
278
279 while (efm[0])
280 {
281 /*
282 * Allocate a new eformat structure and put it at the end of the list
283 */
284 fmt_ptr = (struct eformat *)alloc((unsigned)sizeof(struct eformat));
285 if (fmt_ptr == NULL)
286 goto error2;
287 if (fmt_first == NULL) /* first one */
288 fmt_first = fmt_ptr;
289 else
290 fmt_last->next = fmt_ptr;
291 fmt_last = fmt_ptr;
292 fmt_ptr->prefix = NUL;
293 fmt_ptr->flags = NUL;
294 fmt_ptr->next = NULL;
295 fmt_ptr->prog = NULL;
296 for (round = FMT_PATTERNS; round > 0; )
297 fmt_ptr->addr[--round] = NUL;
298 /* round is 0 now */
299
300 /*
301 * Isolate one part in the 'errorformat' option
302 */
303 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
304 if (efm[len] == '\\' && efm[len + 1] != NUL)
305 ++len;
306
307 /*
308 * Build regexp pattern from current 'errorformat' option
309 */
310 ptr = fmtstr;
311 *ptr++ = '^';
312 for (efmp = efm; efmp < efm + len; ++efmp)
313 {
314 if (*efmp == '%')
315 {
316 ++efmp;
317 for (idx = 0; idx < FMT_PATTERNS; ++idx)
318 if (fmt_pat[idx].convchar == *efmp)
319 break;
320 if (idx < FMT_PATTERNS)
321 {
322 if (fmt_ptr->addr[idx])
323 {
324 sprintf((char *)errmsg,
325 _("E372: Too many %%%c in format string"), *efmp);
326 EMSG(errmsg);
327 goto error2;
328 }
329 if ((idx
330 && idx < 6
331 && vim_strchr((char_u *)"DXOPQ",
332 fmt_ptr->prefix) != NULL)
333 || (idx == 6
334 && vim_strchr((char_u *)"OPQ",
335 fmt_ptr->prefix) == NULL))
336 {
337 sprintf((char *)errmsg,
338 _("E373: Unexpected %%%c in format string"), *efmp);
339 EMSG(errmsg);
340 goto error2;
341 }
342 fmt_ptr->addr[idx] = (char_u)++round;
343 *ptr++ = '\\';
344 *ptr++ = '(';
345#ifdef BACKSLASH_IN_FILENAME
346 if (*efmp == 'f')
347 {
348 /* Also match "c:" in the file name, even when
349 * checking for a colon next: "%f:".
350 * "\%(\a:\)\=" */
351 STRCPY(ptr, "\\%(\\a:\\)\\=");
352 ptr += 10;
353 }
354#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +0000355 if (*efmp == 'f' && efmp[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000357 if (efmp[1] != '\\' && efmp[1] != '%')
358 {
359 /* A file name may contain spaces, but this isn't
360 * in "\f". For "%f:%l:%m" there may be a ":" in
361 * the file name. Use ".\{-1,}x" instead (x is
362 * the next character), the requirement that :999:
363 * follows should work. */
364 STRCPY(ptr, ".\\{-1,}");
365 ptr += 7;
366 }
367 else
368 {
369 /* File name followed by '\\' or '%': include as
370 * many file name chars as possible. */
371 STRCPY(ptr, "\\f\\+");
372 ptr += 4;
373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 }
375 else
376 {
377 srcptr = (char_u *)fmt_pat[idx].pattern;
378 while ((*ptr = *srcptr++) != NUL)
379 ++ptr;
380 }
381 *ptr++ = '\\';
382 *ptr++ = ')';
383 }
384 else if (*efmp == '*')
385 {
386 if (*++efmp == '[' || *efmp == '\\')
387 {
388 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
389 {
390 if (efmp[1] == '^')
391 *ptr++ = *++efmp;
392 if (efmp < efm + len)
393 {
394 *ptr++ = *++efmp; /* could be ']' */
395 while (efmp < efm + len
396 && (*ptr++ = *++efmp) != ']')
397 /* skip */;
398 if (efmp == efm + len)
399 {
400 EMSG(_("E374: Missing ] in format string"));
401 goto error2;
402 }
403 }
404 }
405 else if (efmp < efm + len) /* %*\D, %*\s etc. */
406 *ptr++ = *++efmp;
407 *ptr++ = '\\';
408 *ptr++ = '+';
409 }
410 else
411 {
412 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
413 sprintf((char *)errmsg,
414 _("E375: Unsupported %%%c in format string"), *efmp);
415 EMSG(errmsg);
416 goto error2;
417 }
418 }
419 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
420 *ptr++ = *efmp; /* regexp magic characters */
421 else if (*efmp == '#')
422 *ptr++ = '*';
423 else if (efmp == efm + 1) /* analyse prefix */
424 {
425 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
426 fmt_ptr->flags = *efmp++;
427 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
428 fmt_ptr->prefix = *efmp;
429 else
430 {
431 sprintf((char *)errmsg,
432 _("E376: Invalid %%%c in format string prefix"), *efmp);
433 EMSG(errmsg);
434 goto error2;
435 }
436 }
437 else
438 {
439 sprintf((char *)errmsg,
440 _("E377: Invalid %%%c in format string"), *efmp);
441 EMSG(errmsg);
442 goto error2;
443 }
444 }
445 else /* copy normal character */
446 {
447 if (*efmp == '\\' && efmp + 1 < efm + len)
448 ++efmp;
449 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
450 *ptr++ = '\\'; /* escape regexp atoms */
451 if (*efmp)
452 *ptr++ = *efmp;
453 }
454 }
455 *ptr++ = '$';
456 *ptr = NUL;
457 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
458 goto error2;
459 /*
460 * Advance to next part
461 */
462 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
463 }
464 if (fmt_first == NULL) /* nothing found */
465 {
466 EMSG(_("E378: 'errorformat' contains no pattern"));
467 goto error2;
468 }
469
470 /*
471 * got_int is reset here, because it was probably set when killing the
472 * ":make" command, but we still want to read the errorfile then.
473 */
474 got_int = FALSE;
475
476 /* Always ignore case when looking for a matching error. */
477 regmatch.rm_ic = TRUE;
478
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000479 if (tv != NULL)
480 {
481 if (tv->v_type == VAR_STRING)
482 p_str = tv->vval.v_string;
483 else if (tv->v_type == VAR_LIST)
484 p_li = tv->vval.v_list->lv_first;
485 }
486
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 /*
488 * Read the lines in the error file one by one.
489 * Try to recognize one of the error formats in each line.
490 */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000491 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 {
Bram Moolenaar86b68352004-12-27 21:59:20 +0000493 /* Get the next line. */
494 if (fd == NULL)
495 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000496 if (tv != NULL)
497 {
498 int len;
499
500 if (tv->v_type == VAR_STRING)
501 {
502 /* Get the next line from the supplied string */
503 char_u *p;
504
505 if (!*p_str) /* Reached the end of the string */
506 break;
507
508 p = vim_strchr(p_str, '\n');
509 if (p)
510 len = p - p_str + 1;
511 else
512 len = STRLEN(p_str);
513
514 if (len > CMDBUFFSIZE - 2)
515 vim_strncpy(IObuff, p_str, CMDBUFFSIZE - 2);
516 else
517 vim_strncpy(IObuff, p_str, len);
518
519 p_str += len;
520 }
521 else if (tv->v_type == VAR_LIST)
522 {
523 /* Get the next line from the supplied list */
524 while (p_li && p_li->li_tv.v_type != VAR_STRING)
525 p_li = p_li->li_next; /* Skip non-string items */
526
527 if (!p_li) /* End of the list */
528 break;
529
530 len = STRLEN(p_li->li_tv.vval.v_string);
531 if (len > CMDBUFFSIZE - 2)
532 len = CMDBUFFSIZE - 2;
533
534 vim_strncpy(IObuff, p_li->li_tv.vval.v_string, len);
535
536 p_li = p_li->li_next; /* next item */
537 }
538 }
539 else
540 {
541 /* Get the next line from the supplied buffer */
542 if (buflnum > lnumlast)
543 break;
544 vim_strncpy(IObuff, ml_get_buf(buf, buflnum++, FALSE),
545 CMDBUFFSIZE - 2);
546 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000547 }
548 else if (fgets((char *)IObuff, CMDBUFFSIZE - 2, fd) == NULL)
549 break;
550
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 IObuff[CMDBUFFSIZE - 2] = NUL; /* for very long lines */
552 if ((efmp = vim_strrchr(IObuff, '\n')) != NULL)
553 *efmp = NUL;
554#ifdef USE_CRNL
555 if ((efmp = vim_strrchr(IObuff, '\r')) != NULL)
556 *efmp = NUL;
557#endif
558
559 /*
560 * Try to match each part of 'errorformat' until we find a complete
561 * match or no match.
562 */
563 valid = TRUE;
564restofline:
565 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
566 {
567 idx = fmt_ptr->prefix;
568 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
569 continue;
570 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000571 pattern[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 if (!multiscan)
573 errmsg[0] = NUL;
574 lnum = 0;
575 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000576 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 enr = -1;
578 type = 0;
579 tail = NULL;
580
581 regmatch.regprog = fmt_ptr->prog;
582 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
583 {
584 if ((idx == 'C' || idx == 'Z') && !multiline)
585 continue;
586 if (vim_strchr((char_u *)"EWI", idx) != NULL)
587 type = idx;
588 else
589 type = 0;
590 /*
591 * Extract error message data from matched line
592 */
593 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
594 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000595 int c = *regmatch.endp[i];
596
597 /* Expand ~/file and $HOME/file to full path. */
598 *regmatch.endp[i] = NUL;
599 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
600 *regmatch.endp[i] = c;
601
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000603 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 continue;
605 }
606 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
607 enr = (int)atol((char *)regmatch.startp[i]);
608 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
609 lnum = atol((char *)regmatch.startp[i]);
610 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
611 col = (int)atol((char *)regmatch.startp[i]);
612 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
613 type = *regmatch.startp[i];
Bram Moolenaar4770d092006-01-12 23:22:24 +0000614 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 STRCPY(errmsg, IObuff);
616 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
617 {
618 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaarbbebc852005-07-18 21:47:53 +0000619 vim_strncpy(errmsg, regmatch.startp[i], len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 }
621 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
622 tail = regmatch.startp[i];
623 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
624 {
625 col = (int)(regmatch.endp[i] - regmatch.startp[i] + 1);
626 if (*((char_u *)regmatch.startp[i]) != TAB)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000627 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 }
629 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
630 {
631 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000632 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000634 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
635 {
636 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
637 if (len > CMDBUFFSIZE - 5)
638 len = CMDBUFFSIZE - 5;
639 STRCPY(pattern, "^\\V");
640 STRNCAT(pattern, regmatch.startp[i], len);
641 pattern[len + 3] = '\\';
642 pattern[len + 4] = '$';
643 pattern[len + 5] = NUL;
644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 break;
646 }
647 }
648 multiscan = FALSE;
Bram Moolenaar4770d092006-01-12 23:22:24 +0000649 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 {
Bram Moolenaar4770d092006-01-12 23:22:24 +0000651 if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 {
653 if (idx == 'D') /* enter directory */
654 {
655 if (*namebuf == NUL)
656 {
657 EMSG(_("E379: Missing or empty directory name"));
658 goto error2;
659 }
660 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
661 goto error2;
662 }
663 else if (idx == 'X') /* leave directory */
664 directory = qf_pop_dir(&dir_stack);
665 }
666 namebuf[0] = NUL; /* no match found, remove file name */
667 lnum = 0; /* don't jump to this line */
668 valid = FALSE;
669 STRCPY(errmsg, IObuff); /* copy whole line to error message */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000670 if (fmt_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 multiline = multiignore = FALSE;
672 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000673 else if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 {
675 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
676 multiline = TRUE; /* start of a multi-line message */
677 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
678 { /* continuation of multi-line msg */
679 if (qfprev == NULL)
680 goto error2;
681 if (*errmsg && !multiignore)
682 {
683 len = (int)STRLEN(qfprev->qf_text);
684 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
685 == NULL)
686 goto error2;
687 STRCPY(ptr, qfprev->qf_text);
688 vim_free(qfprev->qf_text);
689 qfprev->qf_text = ptr;
690 *(ptr += len) = '\n';
691 STRCPY(++ptr, errmsg);
692 }
693 if (qfprev->qf_nr == -1)
694 qfprev->qf_nr = enr;
695 if (vim_isprintc(type) && !qfprev->qf_type)
696 qfprev->qf_type = type; /* only printable chars allowed */
697 if (!qfprev->qf_lnum)
698 qfprev->qf_lnum = lnum;
699 if (!qfprev->qf_col)
700 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000701 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 if (!qfprev->qf_fnum)
703 qfprev->qf_fnum = qf_get_fnum(directory,
704 *namebuf || directory ? namebuf
705 : currfile && valid ? currfile : 0);
706 if (idx == 'Z')
707 multiline = multiignore = FALSE;
708 line_breakcheck();
709 continue;
710 }
711 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
712 {
713 /* global file names */
714 valid = FALSE;
715 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
716 {
717 if (*namebuf && idx == 'P')
718 currfile = qf_push_dir(namebuf, &file_stack);
719 else if (idx == 'Q')
720 currfile = qf_pop_dir(&file_stack);
721 *namebuf = NUL;
722 if (tail && *tail)
723 {
724 STRCPY(IObuff, skipwhite(tail));
725 multiscan = TRUE;
726 goto restofline;
727 }
728 }
729 }
730 if (fmt_ptr->flags == '-') /* generally exclude this line */
731 {
732 if (multiline)
733 multiignore = TRUE; /* also exclude continuation lines */
734 continue;
735 }
736 }
737
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000738 if (qf_add_entry(qi, &qfprev,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 directory,
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000740 (*namebuf || directory)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 ? namebuf
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000742 : ((currfile && valid) ? currfile : (char_u *)NULL),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 errmsg,
744 lnum,
745 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +0000746 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000747 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 enr,
749 type,
750 valid) == FAIL)
751 goto error2;
752 line_breakcheck();
753 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000754 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000756 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000758 /* no valid entry found */
759 qi->qf_lists[qi->qf_curlist].qf_ptr =
760 qi->qf_lists[qi->qf_curlist].qf_start;
761 qi->qf_lists[qi->qf_curlist].qf_index = 1;
762 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 }
764 else
765 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000766 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
767 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
768 qi->qf_lists[qi->qf_curlist].qf_ptr =
769 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000771 /* return number of matches */
772 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 goto qf_init_ok;
774 }
775 EMSG(_(e_readerrf));
776error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000777 qf_free(qi, qi->qf_curlist);
778 qi->qf_listcount--;
779 if (qi->qf_curlist > 0)
780 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781qf_init_ok:
Bram Moolenaar86b68352004-12-27 21:59:20 +0000782 if (fd != NULL)
783 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
785 {
786 fmt_first = fmt_ptr->next;
787 vim_free(fmt_ptr->prog);
788 vim_free(fmt_ptr);
789 }
790 qf_clean_dir_stack(&dir_stack);
791 qf_clean_dir_stack(&file_stack);
792qf_init_end:
793 vim_free(namebuf);
794 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000795 vim_free(pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 vim_free(fmtstr);
797
798#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000799 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800#endif
801
802 return retval;
803}
804
805/*
806 * Prepare for adding a new quickfix list.
807 */
808 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000809qf_new_list(qi)
810 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811{
812 int i;
813
814 /*
815 * If the current entry is not the last entry, delete entries below
816 * the current entry. This makes it possible to browse in a tree-like
817 * way with ":grep'.
818 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000819 while (qi->qf_listcount > qi->qf_curlist + 1)
820 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821
822 /*
823 * When the stack is full, remove to oldest entry
824 * Otherwise, add a new entry.
825 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000826 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000828 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000830 qi->qf_lists[i - 1] = qi->qf_lists[i];
831 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 }
833 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000834 qi->qf_curlist = qi->qf_listcount++;
835 qi->qf_lists[qi->qf_curlist].qf_index = 0;
836 qi->qf_lists[qi->qf_curlist].qf_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837}
838
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000839/*
840 * Free a location list
841 */
842 static void
843ll_free_all(pqi)
844 qf_info_T **pqi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000845{
846 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000847 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000848
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000849 qi = *pqi;
850 if (qi == NULL)
851 return;
852 *pqi = NULL; /* Remove reference to this list */
853
854 qi->qf_refcount--;
855 if (qi->qf_refcount < 1)
856 {
857 /* No references to this location list */
858 for (i = 0; i < qi->qf_listcount; ++i)
859 qf_free(qi, i);
860 vim_free(qi);
861 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000862}
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000863
864 void
865qf_free_all(wp)
866 win_T *wp;
867{
868 int i;
869 qf_info_T *qi = &ql_info;
870
871 if (wp != NULL)
872 {
873 /* location list */
874 ll_free_all(&wp->w_llist);
875 ll_free_all(&wp->w_llist_ref);
876 }
877 else
878 /* quickfix list */
879 for (i = 0; i < qi->qf_listcount; ++i)
880 qf_free(qi, i);
881}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000882
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883/*
884 * Add an entry to the end of the list of errors.
885 * Returns OK or FAIL.
886 */
887 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000888qf_add_entry(qi, prevp, dir, fname, mesg, lnum, col, vis_col, pattern, nr, type,
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000889 valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000890 qf_info_T *qi; /* quickfix list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000891 qfline_T **prevp; /* pointer to previously added entry or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 char_u *dir; /* optional directory name */
893 char_u *fname; /* file name or NULL */
894 char_u *mesg; /* message */
895 long lnum; /* line number */
896 int col; /* column */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000897 int vis_col; /* using visual column */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000898 char_u *pattern; /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 int nr; /* error number */
900 int type; /* type character */
901 int valid; /* valid entry */
902{
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000903 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000905 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 return FAIL;
907 qfp->qf_fnum = qf_get_fnum(dir, fname);
908 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
909 {
910 vim_free(qfp);
911 return FAIL;
912 }
913 qfp->qf_lnum = lnum;
914 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000915 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000916 if (pattern == NULL || *pattern == NUL)
917 qfp->qf_pattern = NULL;
918 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
919 {
920 vim_free(qfp->qf_text);
921 vim_free(qfp);
922 return FAIL;
923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 qfp->qf_nr = nr;
925 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
926 type = 0;
927 qfp->qf_type = type;
928 qfp->qf_valid = valid;
929
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000930 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
931 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000933 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 qfp->qf_prev = qfp; /* first element points to itself */
935 }
936 else
937 {
938 qfp->qf_prev = *prevp;
939 (*prevp)->qf_next = qfp;
940 }
941 qfp->qf_next = qfp; /* last element points to itself */
942 qfp->qf_cleared = FALSE;
943 *prevp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000944 ++qi->qf_lists[qi->qf_curlist].qf_count;
945 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
946 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000948 qi->qf_lists[qi->qf_curlist].qf_index =
949 qi->qf_lists[qi->qf_curlist].qf_count;
950 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 }
952
953 return OK;
954}
955
956/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000957 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000958 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000959 static qf_info_T *
960ll_new_list()
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000961{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000962 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000963
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000964 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
965 if (qi != NULL)
966 {
967 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
968 qi->qf_refcount++;
969 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000970
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000971 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000972}
973
974/*
975 * Return the location list for window 'wp'.
976 * If not present, allocate a location list
977 */
978 static qf_info_T *
979ll_get_or_alloc_list(wp)
980 win_T *wp;
981{
982 if (IS_LL_WINDOW(wp))
983 /* For a location list window, use the referenced location list */
984 return wp->w_llist_ref;
985
986 /*
987 * For a non-location list window, w_llist_ref should not point to a
988 * location list.
989 */
990 ll_free_all(&wp->w_llist_ref);
991
992 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000993 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000994 return wp->w_llist;
995}
996
997/*
998 * Copy the location list from window "from" to window "to".
999 */
1000 void
1001copy_loclist(from, to)
1002 win_T *from;
1003 win_T *to;
1004{
1005 qf_info_T *qi;
1006 int idx;
1007 int i;
1008
1009 /*
1010 * When copying from a location list window, copy the referenced
1011 * location list. For other windows, copy the location list for
1012 * that window.
1013 */
1014 if (IS_LL_WINDOW(from))
1015 qi = from->w_llist_ref;
1016 else
1017 qi = from->w_llist;
1018
1019 if (qi == NULL) /* no location list to copy */
1020 return;
1021
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001022 /* allocate a new location list */
1023 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001024 return;
1025
1026 to->w_llist->qf_listcount = qi->qf_listcount;
1027
1028 /* Copy the location lists one at a time */
1029 for (idx = 0; idx < qi->qf_listcount; idx++)
1030 {
1031 qf_list_T *from_qfl;
1032 qf_list_T *to_qfl;
1033
1034 to->w_llist->qf_curlist = idx;
1035
1036 from_qfl = &qi->qf_lists[idx];
1037 to_qfl = &to->w_llist->qf_lists[idx];
1038
1039 /* Some of the fields are populated by qf_add_entry() */
1040 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1041 to_qfl->qf_count = 0;
1042 to_qfl->qf_index = 0;
1043 to_qfl->qf_start = NULL;
1044 to_qfl->qf_ptr = NULL;
1045
1046 if (from_qfl->qf_count)
1047 {
1048 qfline_T *from_qfp;
1049 qfline_T *prevp = NULL;
1050
1051 /* copy all the location entries in this list */
1052 for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
1053 ++i, from_qfp = from_qfp->qf_next)
1054 {
1055 if (qf_add_entry(to->w_llist, &prevp,
1056 NULL,
1057 NULL,
1058 from_qfp->qf_text,
1059 from_qfp->qf_lnum,
1060 from_qfp->qf_col,
1061 from_qfp->qf_viscol,
1062 from_qfp->qf_pattern,
1063 from_qfp->qf_nr,
1064 0,
1065 from_qfp->qf_valid) == FAIL)
1066 {
1067 qf_free_all(to);
1068 return;
1069 }
1070 /*
1071 * qf_add_entry() will not set the qf_num field, as the
1072 * directory and file names are not supplied. So the qf_fnum
1073 * field is copied here.
1074 */
1075 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1076 prevp->qf_type = from_qfp->qf_type; /* error type */
1077 if (from_qfl->qf_ptr == from_qfp)
1078 to_qfl->qf_ptr = prevp; /* current location */
1079 }
1080 }
1081
1082 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1083
1084 /* When no valid entries are present in the list, qf_ptr points to
1085 * the first item in the list */
1086 if (to_qfl->qf_nonevalid == TRUE)
1087 to_qfl->qf_ptr = to_qfl->qf_start;
1088 }
1089
1090 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1091}
1092
1093/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 * get buffer number for file "dir.name"
1095 */
1096 static int
1097qf_get_fnum(directory, fname)
1098 char_u *directory;
1099 char_u *fname;
1100{
1101 if (fname == NULL || *fname == NUL) /* no file name */
1102 return 0;
1103 {
1104#ifdef RISCOS
1105 /* Name is reported as `main.c', but file is `c.main' */
1106 return ro_buflist_add(fname);
1107#else
1108 char_u *ptr;
1109 int fnum;
1110
1111# ifdef VMS
1112 vms_remove_version(fname);
1113# endif
1114# ifdef BACKSLASH_IN_FILENAME
1115 if (directory != NULL)
1116 slash_adjust(directory);
1117 slash_adjust(fname);
1118# endif
1119 if (directory != NULL && !vim_isAbsName(fname)
1120 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1121 {
1122 /*
1123 * Here we check if the file really exists.
1124 * This should normally be true, but if make works without
1125 * "leaving directory"-messages we might have missed a
1126 * directory change.
1127 */
1128 if (mch_getperm(ptr) < 0)
1129 {
1130 vim_free(ptr);
1131 directory = qf_guess_filepath(fname);
1132 if (directory)
1133 ptr = concat_fnames(directory, fname, TRUE);
1134 else
1135 ptr = vim_strsave(fname);
1136 }
1137 /* Use concatenated directory name and file name */
1138 fnum = buflist_add(ptr, 0);
1139 vim_free(ptr);
1140 return fnum;
1141 }
1142 return buflist_add(fname, 0);
1143#endif
1144 }
1145}
1146
1147/*
1148 * push dirbuf onto the directory stack and return pointer to actual dir or
1149 * NULL on error
1150 */
1151 static char_u *
1152qf_push_dir(dirbuf, stackptr)
1153 char_u *dirbuf;
1154 struct dir_stack_T **stackptr;
1155{
1156 struct dir_stack_T *ds_new;
1157 struct dir_stack_T *ds_ptr;
1158
1159 /* allocate new stack element and hook it in */
1160 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1161 if (ds_new == NULL)
1162 return NULL;
1163
1164 ds_new->next = *stackptr;
1165 *stackptr = ds_new;
1166
1167 /* store directory on the stack */
1168 if (vim_isAbsName(dirbuf)
1169 || (*stackptr)->next == NULL
1170 || (*stackptr && dir_stack != *stackptr))
1171 (*stackptr)->dirname = vim_strsave(dirbuf);
1172 else
1173 {
1174 /* Okay we don't have an absolute path.
1175 * dirbuf must be a subdir of one of the directories on the stack.
1176 * Let's search...
1177 */
1178 ds_new = (*stackptr)->next;
1179 (*stackptr)->dirname = NULL;
1180 while (ds_new)
1181 {
1182 vim_free((*stackptr)->dirname);
1183 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1184 TRUE);
1185 if (mch_isdir((*stackptr)->dirname) == TRUE)
1186 break;
1187
1188 ds_new = ds_new->next;
1189 }
1190
1191 /* clean up all dirs we already left */
1192 while ((*stackptr)->next != ds_new)
1193 {
1194 ds_ptr = (*stackptr)->next;
1195 (*stackptr)->next = (*stackptr)->next->next;
1196 vim_free(ds_ptr->dirname);
1197 vim_free(ds_ptr);
1198 }
1199
1200 /* Nothing found -> it must be on top level */
1201 if (ds_new == NULL)
1202 {
1203 vim_free((*stackptr)->dirname);
1204 (*stackptr)->dirname = vim_strsave(dirbuf);
1205 }
1206 }
1207
1208 if ((*stackptr)->dirname != NULL)
1209 return (*stackptr)->dirname;
1210 else
1211 {
1212 ds_ptr = *stackptr;
1213 *stackptr = (*stackptr)->next;
1214 vim_free(ds_ptr);
1215 return NULL;
1216 }
1217}
1218
1219
1220/*
1221 * pop dirbuf from the directory stack and return previous directory or NULL if
1222 * stack is empty
1223 */
1224 static char_u *
1225qf_pop_dir(stackptr)
1226 struct dir_stack_T **stackptr;
1227{
1228 struct dir_stack_T *ds_ptr;
1229
1230 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1231 * What to do if it isn't? */
1232
1233 /* pop top element and free it */
1234 if (*stackptr != NULL)
1235 {
1236 ds_ptr = *stackptr;
1237 *stackptr = (*stackptr)->next;
1238 vim_free(ds_ptr->dirname);
1239 vim_free(ds_ptr);
1240 }
1241
1242 /* return NEW top element as current dir or NULL if stack is empty*/
1243 return *stackptr ? (*stackptr)->dirname : NULL;
1244}
1245
1246/*
1247 * clean up directory stack
1248 */
1249 static void
1250qf_clean_dir_stack(stackptr)
1251 struct dir_stack_T **stackptr;
1252{
1253 struct dir_stack_T *ds_ptr;
1254
1255 while ((ds_ptr = *stackptr) != NULL)
1256 {
1257 *stackptr = (*stackptr)->next;
1258 vim_free(ds_ptr->dirname);
1259 vim_free(ds_ptr);
1260 }
1261}
1262
1263/*
1264 * Check in which directory of the directory stack the given file can be
1265 * found.
1266 * Returns a pointer to the directory name or NULL if not found
1267 * Cleans up intermediate directory entries.
1268 *
1269 * TODO: How to solve the following problem?
1270 * If we have the this directory tree:
1271 * ./
1272 * ./aa
1273 * ./aa/bb
1274 * ./bb
1275 * ./bb/x.c
1276 * and make says:
1277 * making all in aa
1278 * making all in bb
1279 * x.c:9: Error
1280 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1281 * qf_guess_filepath will return NULL.
1282 */
1283 static char_u *
1284qf_guess_filepath(filename)
1285 char_u *filename;
1286{
1287 struct dir_stack_T *ds_ptr;
1288 struct dir_stack_T *ds_tmp;
1289 char_u *fullname;
1290
1291 /* no dirs on the stack - there's nothing we can do */
1292 if (dir_stack == NULL)
1293 return NULL;
1294
1295 ds_ptr = dir_stack->next;
1296 fullname = NULL;
1297 while (ds_ptr)
1298 {
1299 vim_free(fullname);
1300 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1301
1302 /* If concat_fnames failed, just go on. The worst thing that can happen
1303 * is that we delete the entire stack.
1304 */
1305 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1306 break;
1307
1308 ds_ptr = ds_ptr->next;
1309 }
1310
1311 vim_free(fullname);
1312
1313 /* clean up all dirs we already left */
1314 while (dir_stack->next != ds_ptr)
1315 {
1316 ds_tmp = dir_stack->next;
1317 dir_stack->next = dir_stack->next->next;
1318 vim_free(ds_tmp->dirname);
1319 vim_free(ds_tmp);
1320 }
1321
1322 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1323
1324}
1325
1326/*
1327 * jump to a quickfix line
1328 * if dir == FORWARD go "errornr" valid entries forward
1329 * if dir == BACKWARD go "errornr" valid entries backward
1330 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1331 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1332 * else if "errornr" is zero, redisplay the same line
1333 * else go to entry "errornr"
1334 */
1335 void
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001336qf_jump(qi, dir, errornr, forceit)
1337 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 int dir;
1339 int errornr;
1340 int forceit;
1341{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001342 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001343 qfline_T *qf_ptr;
1344 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 int qf_index;
1346 int old_qf_fnum;
1347 int old_qf_index;
1348 int prev_index;
1349 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1350 char_u *err = e_no_more_items;
1351 linenr_T i;
1352 buf_T *old_curbuf;
1353 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 colnr_T screen_col;
1355 colnr_T char_col;
1356 char_u *line;
1357#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001358 char_u *old_swb = p_swb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 int opened_window = FALSE;
1360 win_T *win;
1361 win_T *altwin;
1362#endif
1363 int print_message = TRUE;
1364 int len;
1365#ifdef FEAT_FOLDING
1366 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1367#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001368 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001369 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001371 if (qi == NULL)
1372 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001373
1374 if (qi->qf_curlist >= qi->qf_listcount
1375 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 {
1377 EMSG(_(e_quickfix));
1378 return;
1379 }
1380
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001381 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001383 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 old_qf_index = qf_index;
1385 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1386 {
1387 while (errornr--)
1388 {
1389 old_qf_ptr = qf_ptr;
1390 prev_index = qf_index;
1391 old_qf_fnum = qf_ptr->qf_fnum;
1392 do
1393 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001394 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 || qf_ptr->qf_next == NULL)
1396 {
1397 qf_ptr = old_qf_ptr;
1398 qf_index = prev_index;
1399 if (err != NULL)
1400 {
1401 EMSG(_(err));
1402 goto theend;
1403 }
1404 errornr = 0;
1405 break;
1406 }
1407 ++qf_index;
1408 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001409 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1410 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1412 err = NULL;
1413 }
1414 }
1415 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1416 {
1417 while (errornr--)
1418 {
1419 old_qf_ptr = qf_ptr;
1420 prev_index = qf_index;
1421 old_qf_fnum = qf_ptr->qf_fnum;
1422 do
1423 {
1424 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1425 {
1426 qf_ptr = old_qf_ptr;
1427 qf_index = prev_index;
1428 if (err != NULL)
1429 {
1430 EMSG(_(err));
1431 goto theend;
1432 }
1433 errornr = 0;
1434 break;
1435 }
1436 --qf_index;
1437 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001438 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1439 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1441 err = NULL;
1442 }
1443 }
1444 else if (errornr != 0) /* go to specified number */
1445 {
1446 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1447 {
1448 --qf_index;
1449 qf_ptr = qf_ptr->qf_prev;
1450 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001451 while (errornr > qf_index && qf_index <
1452 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 && qf_ptr->qf_next != NULL)
1454 {
1455 ++qf_index;
1456 qf_ptr = qf_ptr->qf_next;
1457 }
1458 }
1459
1460#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001461 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1462 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 /* No need to print the error message if it's visible in the error
1464 * window */
1465 print_message = FALSE;
1466
1467 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001468 * For ":helpgrep" find a help window or open one.
1469 */
1470 if (qf_ptr->qf_type == 1 && !curwin->w_buffer->b_help)
1471 {
1472 win_T *wp;
1473 int n;
1474
1475 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1476 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1477 break;
1478 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1479 win_enter(wp, TRUE);
1480 else
1481 {
1482 /*
1483 * Split off help window; put it at far top if no position
1484 * specified, the current window is vertically split and narrow.
1485 */
1486 n = WSP_HELP;
1487# ifdef FEAT_VERTSPLIT
1488 if (cmdmod.split == 0 && curwin->w_width != Columns
1489 && curwin->w_width < 80)
1490 n |= WSP_TOP;
1491# endif
1492 if (win_split(0, n) == FAIL)
1493 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001494 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001495
1496 if (curwin->w_height < p_hh)
1497 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001498
1499 if (qi != &ql_info) /* not a quickfix list */
1500 {
1501 /* The new window should use the supplied location list */
1502 qf_free_all(curwin);
1503 curwin->w_llist = qi;
1504 qi->qf_refcount++;
1505 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001506 }
1507
1508 if (!p_im)
1509 restart_edit = 0; /* don't want insert mode in help file */
1510 }
1511
1512 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 * If currently in the quickfix window, find another window to show the
1514 * file in.
1515 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001516 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 {
1518 /*
1519 * If there is no file specified, we don't know where to go.
1520 * But do advance, otherwise ":cn" gets stuck.
1521 */
1522 if (qf_ptr->qf_fnum == 0)
1523 goto theend;
1524
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001525 /* Locate a window showing a normal buffer */
1526 usable_win = 0;
1527 FOR_ALL_WINDOWS(win)
1528 if (win->w_buffer->b_p_bt[0] == NUL)
1529 {
1530 usable_win = 1;
1531 break;
1532 }
1533
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 /*
1535 * If there is only one window, create a new one above the quickfix
1536 * window.
1537 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001538 if (firstwin == lastwin || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001540 ll_ref = curwin->w_llist_ref;
1541
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 if (win_split(0, WSP_ABOVE) == FAIL)
1543 goto failed; /* not enough room for window */
1544 opened_window = TRUE; /* close it when fail */
1545 p_swb = empty_option; /* don't split again */
1546# ifdef FEAT_SCROLLBIND
1547 curwin->w_p_scb = FALSE;
1548# endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001549 if (ll_ref != NULL)
1550 {
1551 /* The new window should use the location list from the
1552 * location list window */
1553 qf_free_all(curwin);
1554 curwin->w_llist = ll_ref;
1555 ll_ref->qf_refcount++;
1556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 }
1558 else
1559 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001560 if (curwin->w_llist_ref != NULL)
1561 {
1562 /* In a location window */
1563 ll_ref = curwin->w_llist_ref;
1564
1565 /* Find the window with the same location list */
1566 FOR_ALL_WINDOWS(win)
1567 if (win->w_llist == ll_ref)
1568 break;
1569 if (win == NULL)
1570 {
1571 /* Find the window showing the selected file */
1572 FOR_ALL_WINDOWS(win)
1573 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1574 break;
1575 if (win == NULL)
1576 {
1577 /* Find a previous usable window */
1578 win = curwin;
1579 do
1580 {
1581 if (win->w_buffer->b_p_bt[0] == NUL)
1582 break;
1583 if (win->w_prev == NULL)
1584 win = lastwin; /* wrap around the top */
1585 else
1586 win = win->w_prev; /* go to previous window */
1587 } while (win != curwin);
1588 }
1589 }
1590 win_goto(win);
1591
1592 /* If the location list for the window is not set, then set it
1593 * to the location list from the location window */
1594 if (win->w_llist == NULL)
1595 {
1596 win->w_llist = ll_ref;
1597 ll_ref->qf_refcount++;
1598 }
1599 }
1600 else
1601 {
1602
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 /*
1604 * Try to find a window that shows the right buffer.
1605 * Default to the window just above the quickfix buffer.
1606 */
1607 win = curwin;
1608 altwin = NULL;
1609 for (;;)
1610 {
1611 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1612 break;
1613 if (win->w_prev == NULL)
1614 win = lastwin; /* wrap around the top */
1615 else
1616 win = win->w_prev; /* go to previous window */
1617
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001618 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 {
1620 /* Didn't find it, go to the window before the quickfix
1621 * window. */
1622 if (altwin != NULL)
1623 win = altwin;
1624 else if (curwin->w_prev != NULL)
1625 win = curwin->w_prev;
1626 else
1627 win = curwin->w_next;
1628 break;
1629 }
1630
1631 /* Remember a usable window. */
1632 if (altwin == NULL && !win->w_p_pvw
1633 && win->w_buffer->b_p_bt[0] == NUL)
1634 altwin = win;
1635 }
1636
1637 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 }
1640 }
1641#endif
1642
1643 /*
1644 * If there is a file name,
1645 * read the wanted file if needed, and check autowrite etc.
1646 */
1647 old_curbuf = curbuf;
1648 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001649
1650 if (qf_ptr->qf_fnum != 0)
1651 {
1652 if (qf_ptr->qf_type == 1)
1653 {
1654 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1655 * set b_p_ro flag). */
1656 if (!can_abandon(curbuf, forceit))
1657 {
1658 EMSG(_(e_nowrtmsg));
1659 ok = FALSE;
1660 }
1661 else
1662 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
1663 ECMD_HIDE + ECMD_SET_HELP);
1664 }
1665 else
1666 ok = buflist_getfile(qf_ptr->qf_fnum,
1667 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
1668 }
1669
1670 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 {
1672 /* When not switched to another buffer, still need to set pc mark */
1673 if (curbuf == old_curbuf)
1674 setpcmark();
1675
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001676 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001678 /*
1679 * Go to line with error, unless qf_lnum is 0.
1680 */
1681 i = qf_ptr->qf_lnum;
1682 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001684 if (i > curbuf->b_ml.ml_line_count)
1685 i = curbuf->b_ml.ml_line_count;
1686 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001688 if (qf_ptr->qf_col > 0)
1689 {
1690 curwin->w_cursor.col = qf_ptr->qf_col - 1;
1691 if (qf_ptr->qf_viscol == TRUE)
1692 {
1693 /*
1694 * Check each character from the beginning of the error
1695 * line up to the error column. For each tab character
1696 * found, reduce the error column value by the length of
1697 * a tab character.
1698 */
1699 line = ml_get_curline();
1700 screen_col = 0;
1701 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1702 {
1703 if (*line == NUL)
1704 break;
1705 if (*line++ == '\t')
1706 {
1707 curwin->w_cursor.col -= 7 - (screen_col % 8);
1708 screen_col += 8 - (screen_col % 8);
1709 }
1710 else
1711 ++screen_col;
1712 }
1713 }
1714 check_cursor();
1715 }
1716 else
1717 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 }
1719 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001720 {
1721 pos_T save_cursor;
1722
1723 /* Move the cursor to the first line in the buffer */
1724 save_cursor = curwin->w_cursor;
1725 curwin->w_cursor.lnum = 0;
1726 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1, SEARCH_KEEP))
1727 curwin->w_cursor = save_cursor;
1728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729
1730#ifdef FEAT_FOLDING
1731 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1732 foldOpenCursor();
1733#endif
1734 if (print_message)
1735 {
1736 /* Update the screen before showing the message */
1737 update_topline_redraw();
1738 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001739 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1741 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1742 /* Add the message, skipping leading whitespace and newlines. */
1743 len = (int)STRLEN(IObuff);
1744 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1745
1746 /* Output the message. Overwrite to avoid scrolling when the 'O'
1747 * flag is present in 'shortmess'; But when not jumping, print the
1748 * whole message. */
1749 i = msg_scroll;
1750 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1751 msg_scroll = TRUE;
1752 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1753 msg_scroll = FALSE;
1754 msg_attr_keep(IObuff, 0, TRUE);
1755 msg_scroll = i;
1756 }
1757 }
1758 else
1759 {
1760#ifdef FEAT_WINDOWS
1761 if (opened_window)
1762 win_close(curwin, TRUE); /* Close opened window */
1763#endif
1764 if (qf_ptr->qf_fnum != 0)
1765 {
1766 /*
1767 * Couldn't open file, so put index back where it was. This could
1768 * happen if the file was readonly and we changed something.
1769 */
1770#ifdef FEAT_WINDOWS
1771failed:
1772#endif
1773 qf_ptr = old_qf_ptr;
1774 qf_index = old_qf_index;
1775 }
1776 }
1777theend:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001778 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
1779 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780#ifdef FEAT_WINDOWS
1781 if (p_swb != old_swb && opened_window)
1782 {
1783 /* Restore old 'switchbuf' value, but not when an autocommand or
1784 * modeline has changed the value. */
1785 if (p_swb == empty_option)
1786 p_swb = old_swb;
1787 else
1788 free_string_option(old_swb);
1789 }
1790#endif
1791}
1792
1793/*
1794 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001795 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 */
1797 void
1798qf_list(eap)
1799 exarg_T *eap;
1800{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001801 buf_T *buf;
1802 char_u *fname;
1803 qfline_T *qfp;
1804 int i;
1805 int idx1 = 1;
1806 int idx2 = -1;
1807 int need_return = TRUE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001808 char_u *arg = eap->arg;
1809 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001811 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001813 if (eap->cmdidx == CMD_llist)
1814 {
1815 qi = GET_LOC_LIST(curwin);
1816 if (qi == NULL)
1817 {
1818 EMSG(_(e_loclist));
1819 return;
1820 }
1821 }
1822
1823 if (qi->qf_curlist >= qi->qf_listcount
1824 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 {
1826 EMSG(_(e_quickfix));
1827 return;
1828 }
1829 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
1830 {
1831 EMSG(_(e_trailing));
1832 return;
1833 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001834 i = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 if (idx1 < 0)
1836 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
1837 if (idx2 < 0)
1838 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
1839
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001840 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001842 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
1843 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 {
1845 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
1846 {
1847 if (need_return)
1848 {
1849 msg_putchar('\n');
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001850 if (got_int)
1851 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 need_return = FALSE;
1853 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001854
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001855 fname = NULL;
1856 if (qfp->qf_fnum != 0
1857 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
1858 {
1859 fname = buf->b_fname;
1860 if (qfp->qf_type == 1) /* :helpgrep */
1861 fname = gettail(fname);
1862 }
1863 if (fname == NULL)
1864 sprintf((char *)IObuff, "%2d", i);
1865 else
1866 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
1867 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001868 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001869 ? hl_attr(HLF_L) : hl_attr(HLF_D));
1870 if (qfp->qf_lnum == 0)
1871 IObuff[0] = NUL;
1872 else if (qfp->qf_col == 0)
1873 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
1874 else
1875 sprintf((char *)IObuff, ":%ld col %d",
1876 qfp->qf_lnum, qfp->qf_col);
1877 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
1878 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
1879 msg_puts_attr(IObuff, hl_attr(HLF_N));
1880 if (qfp->qf_pattern != NULL)
1881 {
1882 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
1883 STRCAT(IObuff, ":");
1884 msg_puts(IObuff);
1885 }
1886 msg_puts((char_u *)" ");
1887
1888 /* Remove newlines and leading whitespace from the text. For an
1889 * unrecognized line keep the indent, the compiler may mark a word
1890 * with ^^^^. */
1891 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 ? skipwhite(qfp->qf_text) : qfp->qf_text,
1893 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001894 msg_prt_line(IObuff, FALSE);
1895 out_flush(); /* show one line at a time */
1896 need_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001898
1899 qfp = qfp->qf_next;
1900 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 ui_breakcheck();
1902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903}
1904
1905/*
1906 * Remove newlines and leading whitespace from an error message.
1907 * Put the result in "buf[bufsize]".
1908 */
1909 static void
1910qf_fmt_text(text, buf, bufsize)
1911 char_u *text;
1912 char_u *buf;
1913 int bufsize;
1914{
1915 int i;
1916 char_u *p = text;
1917
1918 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
1919 {
1920 if (*p == '\n')
1921 {
1922 buf[i] = ' ';
1923 while (*++p != NUL)
1924 if (!vim_iswhite(*p) && *p != '\n')
1925 break;
1926 }
1927 else
1928 buf[i] = *p++;
1929 }
1930 buf[i] = NUL;
1931}
1932
1933/*
1934 * ":colder [count]": Up in the quickfix stack.
1935 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001936 * ":lolder [count]": Up in the location list stack.
1937 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 */
1939 void
1940qf_age(eap)
1941 exarg_T *eap;
1942{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001943 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 int count;
1945
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001946 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
1947 {
1948 qi = GET_LOC_LIST(curwin);
1949 if (qi == NULL)
1950 {
1951 EMSG(_(e_loclist));
1952 return;
1953 }
1954 }
1955
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 if (eap->addr_count != 0)
1957 count = eap->line2;
1958 else
1959 count = 1;
1960 while (count--)
1961 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001962 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001964 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 {
1966 EMSG(_("E380: At bottom of quickfix stack"));
1967 return;
1968 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001969 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 }
1971 else
1972 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001973 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 {
1975 EMSG(_("E381: At top of quickfix stack"));
1976 return;
1977 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001978 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 }
1980 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001981 qf_msg(qi);
1982
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983}
1984
1985 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001986qf_msg(qi)
1987 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988{
1989 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001990 qi->qf_curlist + 1, qi->qf_listcount,
1991 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001993 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994#endif
1995}
1996
1997/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00001998 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 */
2000 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002001qf_free(qi, idx)
2002 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 int idx;
2004{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002005 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002007 while (qi->qf_lists[idx].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002009 qfp = qi->qf_lists[idx].qf_start->qf_next;
2010 vim_free(qi->qf_lists[idx].qf_start->qf_text);
2011 vim_free(qi->qf_lists[idx].qf_start->qf_pattern);
2012 vim_free(qi->qf_lists[idx].qf_start);
2013 qi->qf_lists[idx].qf_start = qfp;
2014 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 }
2016}
2017
2018/*
2019 * qf_mark_adjust: adjust marks
2020 */
2021 void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002022qf_mark_adjust(wp, line1, line2, amount, amount_after)
2023 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 linenr_T line1;
2025 linenr_T line2;
2026 long amount;
2027 long amount_after;
2028{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002029 int i;
2030 qfline_T *qfp;
2031 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002032 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002034 if (wp != NULL)
2035 {
2036 if (wp->w_llist == NULL)
2037 return;
2038 qi = wp->w_llist;
2039 }
2040
2041 for (idx = 0; idx < qi->qf_listcount; ++idx)
2042 if (qi->qf_lists[idx].qf_count)
2043 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
2044 i < qi->qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 if (qfp->qf_fnum == curbuf->b_fnum)
2046 {
2047 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2048 {
2049 if (amount == MAXLNUM)
2050 qfp->qf_cleared = TRUE;
2051 else
2052 qfp->qf_lnum += amount;
2053 }
2054 else if (amount_after && qfp->qf_lnum > line2)
2055 qfp->qf_lnum += amount_after;
2056 }
2057}
2058
2059/*
2060 * Make a nice message out of the error character and the error number:
2061 * char number message
2062 * e or E 0 " error"
2063 * w or W 0 " warning"
2064 * i or I 0 " info"
2065 * 0 0 ""
2066 * other 0 " c"
2067 * e or E n " error n"
2068 * w or W n " warning n"
2069 * i or I n " info n"
2070 * 0 n " error n"
2071 * other n " c n"
2072 * 1 x "" :helpgrep
2073 */
2074 static char_u *
2075qf_types(c, nr)
2076 int c, nr;
2077{
2078 static char_u buf[20];
2079 static char_u cc[3];
2080 char_u *p;
2081
2082 if (c == 'W' || c == 'w')
2083 p = (char_u *)" warning";
2084 else if (c == 'I' || c == 'i')
2085 p = (char_u *)" info";
2086 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2087 p = (char_u *)" error";
2088 else if (c == 0 || c == 1)
2089 p = (char_u *)"";
2090 else
2091 {
2092 cc[0] = ' ';
2093 cc[1] = c;
2094 cc[2] = NUL;
2095 p = cc;
2096 }
2097
2098 if (nr <= 0)
2099 return p;
2100
2101 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2102 return buf;
2103}
2104
2105#if defined(FEAT_WINDOWS) || defined(PROTO)
2106/*
2107 * ":cwindow": open the quickfix window if we have errors to display,
2108 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002109 * ":lwindow": open the location list window if we have locations to display,
2110 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 */
2112 void
2113ex_cwindow(eap)
2114 exarg_T *eap;
2115{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002116 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 win_T *win;
2118
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002119 if (eap->cmdidx == CMD_lwindow)
2120 {
2121 qi = GET_LOC_LIST(curwin);
2122 if (qi == NULL)
2123 return;
2124 }
2125
2126 /* Look for an existing quickfix window. */
2127 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128
2129 /*
2130 * If a quickfix window is open but we have no errors to display,
2131 * close the window. If a quickfix window is not open, then open
2132 * it if we have errors; otherwise, leave it closed.
2133 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002134 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
2135 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 {
2137 if (win != NULL)
2138 ex_cclose(eap);
2139 }
2140 else if (win == NULL)
2141 ex_copen(eap);
2142}
2143
2144/*
2145 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002146 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 */
2148/*ARGSUSED*/
2149 void
2150ex_cclose(eap)
2151 exarg_T *eap;
2152{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002153 win_T *win = NULL;
2154 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002156 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2157 {
2158 qi = GET_LOC_LIST(curwin);
2159 if (qi == NULL)
2160 return;
2161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002163 /* Find existing quickfix window and close it. */
2164 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 if (win != NULL)
2166 win_close(win, FALSE);
2167}
2168
2169/*
2170 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002171 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 */
2173 void
2174ex_copen(eap)
2175 exarg_T *eap;
2176{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002177 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 win_T *win;
2180
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002181 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2182 {
2183 qi = GET_LOC_LIST(curwin);
2184 if (qi == NULL)
2185 {
2186 EMSG(_(e_loclist));
2187 return;
2188 }
2189 }
2190
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 if (eap->addr_count != 0)
2192 height = eap->line2;
2193 else
2194 height = QF_WINHEIGHT;
2195
2196#ifdef FEAT_VISUAL
2197 reset_VIsual_and_resel(); /* stop Visual mode */
2198#endif
2199#ifdef FEAT_GUI
2200 need_mouse_correct = TRUE;
2201#endif
2202
2203 /*
2204 * Find existing quickfix window, or open a new one.
2205 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002206 win = qf_find_win(qi);
2207
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 if (win != NULL)
2209 win_goto(win);
2210 else
2211 {
2212 /* The current window becomes the previous window afterwards. */
2213 win = curwin;
2214
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002215 if (eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2216 /* Create the new window at the very bottom. */
2217 win_goto(lastwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 if (win_split(height, WSP_BELOW) == FAIL)
2219 return; /* not enough room for window */
2220#ifdef FEAT_SCROLLBIND
2221 curwin->w_p_scb = FALSE;
2222#endif
2223
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002224 /* Remove the location list for the quickfix window */
2225 qf_free_all(curwin);
2226
2227 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002229 /*
2230 * For the location list window, create a reference to the
2231 * location list from the window 'win'.
2232 */
2233 curwin->w_llist_ref = win->w_llist;
2234 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002236
2237 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
2238 /* switch off 'swapfile' */
2239 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2240 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
2241 OPT_LOCAL);
2242 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
2243 set_option_value((char_u *)"diff", 0L, (char_u *)"", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002245#ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 /* Only set the height when there is no window to the side. */
2247 if (curwin->w_width == Columns)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 win_setheight(height);
2250 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2251 if (win_valid(win))
2252 prevwin = win;
2253 }
2254
2255 /*
2256 * Fill the buffer with the quickfix list.
2257 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002258 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002260 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 curwin->w_cursor.col = 0;
2262 check_cursor();
2263 update_topline(); /* scroll to show the line */
2264}
2265
2266/*
2267 * Return the number of the current entry (line number in the quickfix
2268 * window).
2269 */
2270 linenr_T
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002271qf_current_entry(wp)
2272 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002274 qf_info_T *qi = &ql_info;
2275
2276 if (IS_LL_WINDOW(wp))
2277 /* In the location list window, use the referenced location list */
2278 qi = wp->w_llist_ref;
2279
2280 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281}
2282
2283/*
2284 * Update the cursor position in the quickfix window to the current error.
2285 * Return TRUE if there is a quickfix window.
2286 */
2287 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002288qf_win_pos_update(qi, old_qf_index)
2289 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 int old_qf_index; /* previous qf_index or zero */
2291{
2292 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002293 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294
2295 /*
2296 * Put the cursor on the current error in the quickfix window, so that
2297 * it's viewable.
2298 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002299 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 if (win != NULL
2301 && qf_index <= win->w_buffer->b_ml.ml_line_count
2302 && old_qf_index != qf_index)
2303 {
2304 win_T *old_curwin = curwin;
2305
2306 curwin = win;
2307 curbuf = win->w_buffer;
2308 if (qf_index > old_qf_index)
2309 {
2310 curwin->w_redraw_top = old_qf_index;
2311 curwin->w_redraw_bot = qf_index;
2312 }
2313 else
2314 {
2315 curwin->w_redraw_top = qf_index;
2316 curwin->w_redraw_bot = old_qf_index;
2317 }
2318 curwin->w_cursor.lnum = qf_index;
2319 curwin->w_cursor.col = 0;
2320 update_topline(); /* scroll to show the line */
2321 redraw_later(VALID);
2322 curwin->w_redr_status = TRUE; /* update ruler */
2323 curwin = old_curwin;
2324 curbuf = curwin->w_buffer;
2325 }
2326 return win != NULL;
2327}
2328
2329/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002330 * Find a window displaying the quickfix/location list 'qi'
2331 */
2332 static win_T *
2333qf_find_win(qi)
2334 qf_info_T *qi;
2335{
2336 win_T *win;
2337
2338 /*
2339 * When searching for the quickfix buffer, find a window
2340 * with w_llist_ref set to NULL.
2341 * When searching for the location list buffer, find a window
2342 * with w_llist_ref pointing to the supplied location list.
2343 */
2344 FOR_ALL_WINDOWS(win)
2345 if (bt_quickfix(win->w_buffer))
2346 if ((qi == &ql_info && win->w_llist_ref == NULL)
2347 || (qi != &ql_info && win->w_llist_ref == qi))
2348 break;
2349
2350 return win;
2351}
2352
2353/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 * Find quickfix buffer.
2355 */
2356 static buf_T *
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002357qf_find_buf(qi)
2358 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002360 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002362 win = qf_find_win(qi);
2363
2364 return (win != NULL) ? win->w_buffer: NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365}
2366
2367/*
2368 * Find the quickfix buffer. If it exists, update the contents.
2369 */
2370 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002371qf_update_buffer(qi)
2372 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373{
2374 buf_T *buf;
2375#ifdef FEAT_AUTOCMD
2376 aco_save_T aco;
2377#else
2378 buf_T *save_curbuf;
2379#endif
2380
2381 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002382 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 if (buf != NULL)
2384 {
2385#ifdef FEAT_AUTOCMD
2386 /* set curwin/curbuf to buf and save a few things */
2387 aucmd_prepbuf(&aco, buf);
2388#else
2389 save_curbuf = curbuf;
2390 curbuf = buf;
2391#endif
2392
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002393 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394
2395#ifdef FEAT_AUTOCMD
2396 /* restore curwin/curbuf and a few other things */
2397 aucmd_restbuf(&aco);
2398#else
2399 curbuf = save_curbuf;
2400#endif
2401
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002402 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 }
2404}
2405
2406/*
2407 * Fill current buffer with quickfix errors, replacing any previous contents.
2408 * curbuf must be the quickfix buffer!
2409 */
2410 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002411qf_fill_buffer(qi)
2412 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002414 linenr_T lnum;
2415 qfline_T *qfp;
2416 buf_T *errbuf;
2417 int len;
2418 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419
2420 /* delete all existing lines */
2421 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2422 (void)ml_delete((linenr_T)1, FALSE);
2423
2424 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002425 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 {
2427 /* Add one line for each error */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002428 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2429 for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 {
2431 if (qfp->qf_fnum != 0
2432 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2433 && errbuf->b_fname != NULL)
2434 {
2435 if (qfp->qf_type == 1) /* :helpgrep */
2436 STRCPY(IObuff, gettail(errbuf->b_fname));
2437 else
2438 STRCPY(IObuff, errbuf->b_fname);
2439 len = (int)STRLEN(IObuff);
2440 }
2441 else
2442 len = 0;
2443 IObuff[len++] = '|';
2444
2445 if (qfp->qf_lnum > 0)
2446 {
2447 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2448 len += (int)STRLEN(IObuff + len);
2449
2450 if (qfp->qf_col > 0)
2451 {
2452 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2453 len += (int)STRLEN(IObuff + len);
2454 }
2455
2456 sprintf((char *)IObuff + len, "%s",
2457 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2458 len += (int)STRLEN(IObuff + len);
2459 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002460 else if (qfp->qf_pattern != NULL)
2461 {
2462 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2463 len += (int)STRLEN(IObuff + len);
2464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 IObuff[len++] = '|';
2466 IObuff[len++] = ' ';
2467
2468 /* Remove newlines and leading whitespace from the text.
2469 * For an unrecognized line keep the indent, the compiler may
2470 * mark a word with ^^^^. */
2471 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2472 IObuff + len, IOSIZE - len);
2473
2474 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2475 == FAIL)
2476 break;
2477 qfp = qfp->qf_next;
2478 }
2479 /* Delete the empty line which is now at the end */
2480 (void)ml_delete(lnum + 1, FALSE);
2481 }
2482
2483 /* correct cursor position */
2484 check_lnums(TRUE);
2485
2486 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2487 * resembles reading a file into a buffer, it's more logical when using
2488 * autocommands. */
2489 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2490 curbuf->b_p_ma = FALSE;
2491
2492#ifdef FEAT_AUTOCMD
2493 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2494 FALSE, curbuf);
2495 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2496 FALSE, curbuf);
2497#endif
2498
2499 /* make sure it will be redrawn */
2500 redraw_curbuf_later(NOT_VALID);
2501
2502 /* Restore KeyTyped, setting 'filetype' may reset it. */
2503 KeyTyped = old_KeyTyped;
2504}
2505
2506#endif /* FEAT_WINDOWS */
2507
2508/*
2509 * Return TRUE if "buf" is the quickfix buffer.
2510 */
2511 int
2512bt_quickfix(buf)
2513 buf_T *buf;
2514{
2515 return (buf->b_p_bt[0] == 'q');
2516}
2517
2518/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002519 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2520 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 */
2522 int
2523bt_nofile(buf)
2524 buf_T *buf;
2525{
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002526 return (buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2527 || buf->b_p_bt[0] == 'a';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528}
2529
2530/*
2531 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2532 */
2533 int
2534bt_dontwrite(buf)
2535 buf_T *buf;
2536{
2537 return (buf->b_p_bt[0] == 'n');
2538}
2539
2540 int
2541bt_dontwrite_msg(buf)
2542 buf_T *buf;
2543{
2544 if (bt_dontwrite(buf))
2545 {
2546 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2547 return TRUE;
2548 }
2549 return FALSE;
2550}
2551
2552/*
2553 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2554 * and 'bufhidden'.
2555 */
2556 int
2557buf_hide(buf)
2558 buf_T *buf;
2559{
2560 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2561 switch (buf->b_p_bh[0])
2562 {
2563 case 'u': /* "unload" */
2564 case 'w': /* "wipe" */
2565 case 'd': return FALSE; /* "delete" */
2566 case 'h': return TRUE; /* "hide" */
2567 }
2568 return (p_hid || cmdmod.hide);
2569}
2570
2571/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002572 * Return TRUE when using ":vimgrep" for ":grep".
2573 */
2574 int
Bram Moolenaar81695252004-12-29 20:58:21 +00002575grep_internal(cmdidx)
2576 cmdidx_T cmdidx;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002577{
Bram Moolenaara6557602006-02-04 22:43:20 +00002578 return ((cmdidx == CMD_grep || cmdidx == CMD_lgrep
2579 || cmdidx == CMD_grepadd || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002580 && STRCMP("internal",
2581 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2582}
2583
2584/*
Bram Moolenaara6557602006-02-04 22:43:20 +00002585 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 */
2587 void
2588ex_make(eap)
2589 exarg_T *eap;
2590{
Bram Moolenaar7c626922005-02-07 22:01:03 +00002591 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 char_u *cmd;
2593 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00002594 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002595 qf_info_T *qi = &ql_info;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002596#ifdef FEAT_AUTOCMD
2597 char_u *au_name = NULL;
2598
2599 switch (eap->cmdidx)
2600 {
2601 case CMD_make: au_name = (char_u *)"make"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00002602 case CMD_lmake: au_name = (char_u *)"lmake"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002603 case CMD_grep: au_name = (char_u *)"grep"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00002604 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002605 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00002606 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002607 default: break;
2608 }
2609 if (au_name != NULL)
2610 {
2611 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2612 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00002613# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00002614 if (did_throw || force_abort)
2615 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00002616# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002617 }
2618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619
Bram Moolenaar86b68352004-12-27 21:59:20 +00002620 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
Bram Moolenaar81695252004-12-29 20:58:21 +00002621 if (grep_internal(eap->cmdidx))
Bram Moolenaar86b68352004-12-27 21:59:20 +00002622 {
2623 ex_vimgrep(eap);
2624 return;
2625 }
2626
Bram Moolenaara6557602006-02-04 22:43:20 +00002627 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
2628 || eap->cmdidx == CMD_lgrepadd)
2629 {
2630 qi = ll_get_or_alloc_list(curwin);
2631 if (qi == NULL)
2632 return;
2633 wp = curwin;
2634 }
2635
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002637 fname = get_mef_name();
2638 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002640 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641
2642 /*
2643 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2644 */
2645 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2646 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002647 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 cmd = alloc(len);
2649 if (cmd == NULL)
2650 return;
2651 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2652 (char *)p_shq);
2653 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002654 append_redir(cmd, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 /*
2656 * Output a newline if there's something else than the :make command that
2657 * was typed (in which case the cursor is in column 0).
2658 */
2659 if (msg_col == 0)
2660 msg_didout = FALSE;
2661 msg_start();
2662 MSG_PUTS(":!");
2663 msg_outtrans(cmd); /* show what we are doing */
2664
2665 /* let the shell know if we are redirecting output or not */
2666 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2667
2668#ifdef AMIGA
2669 out_flush();
2670 /* read window status report and redraw before message */
2671 (void)char_avail();
2672#endif
2673
Bram Moolenaara6557602006-02-04 22:43:20 +00002674 if (qf_init(wp, fname, (eap->cmdidx != CMD_make
2675 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
2676 (eap->cmdidx != CMD_grepadd
2677 && eap->cmdidx != CMD_lgrepadd)) > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002679 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680
Bram Moolenaar7c626922005-02-07 22:01:03 +00002681 mch_remove(fname);
2682 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 vim_free(cmd);
Bram Moolenaar7c626922005-02-07 22:01:03 +00002684
2685#ifdef FEAT_AUTOCMD
2686 if (au_name != NULL)
2687 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2688 curbuf->b_fname, TRUE, curbuf);
2689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690}
2691
2692/*
2693 * Return the name for the errorfile, in allocated memory.
2694 * Find a new unique name when 'makeef' contains "##".
2695 * Returns NULL for error.
2696 */
2697 static char_u *
2698get_mef_name()
2699{
2700 char_u *p;
2701 char_u *name;
2702 static int start = -1;
2703 static int off = 0;
2704#ifdef HAVE_LSTAT
2705 struct stat sb;
2706#endif
2707
2708 if (*p_mef == NUL)
2709 {
2710 name = vim_tempname('e');
2711 if (name == NULL)
2712 EMSG(_(e_notmp));
2713 return name;
2714 }
2715
2716 for (p = p_mef; *p; ++p)
2717 if (p[0] == '#' && p[1] == '#')
2718 break;
2719
2720 if (*p == NUL)
2721 return vim_strsave(p_mef);
2722
2723 /* Keep trying until the name doesn't exist yet. */
2724 for (;;)
2725 {
2726 if (start == -1)
2727 start = mch_get_pid();
2728 else
2729 off += 19;
2730
2731 name = alloc((unsigned)STRLEN(p_mef) + 30);
2732 if (name == NULL)
2733 break;
2734 STRCPY(name, p_mef);
2735 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2736 STRCAT(name, p + 2);
2737 if (mch_getperm(name) < 0
2738#ifdef HAVE_LSTAT
2739 /* Don't accept a symbolic link, its a security risk. */
2740 && mch_lstat((char *)name, &sb) < 0
2741#endif
2742 )
2743 break;
2744 vim_free(name);
2745 }
2746 return name;
2747}
2748
2749/*
2750 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002751 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 */
2753 void
2754ex_cc(eap)
2755 exarg_T *eap;
2756{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002757 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002758
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002759 if (eap->cmdidx == CMD_ll
2760 || eap->cmdidx == CMD_lrewind
2761 || eap->cmdidx == CMD_lfirst
2762 || eap->cmdidx == CMD_llast)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002763 {
2764 qi = GET_LOC_LIST(curwin);
2765 if (qi == NULL)
2766 {
2767 EMSG(_(e_loclist));
2768 return;
2769 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002770 }
2771
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002772 qf_jump(qi, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 eap->addr_count > 0
2774 ? (int)eap->line2
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002775 : (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 ? 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002777 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
2778 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 ? 1
2780 : 32767,
2781 eap->forceit);
2782}
2783
2784/*
2785 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002786 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 */
2788 void
2789ex_cnext(eap)
2790 exarg_T *eap;
2791{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002792 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002793
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002794 if (eap->cmdidx == CMD_lnext
2795 || eap->cmdidx == CMD_lNext
2796 || eap->cmdidx == CMD_lprevious
2797 || eap->cmdidx == CMD_lnfile
2798 || eap->cmdidx == CMD_lNfile
2799 || eap->cmdidx == CMD_lpfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002800 {
2801 qi = GET_LOC_LIST(curwin);
2802 if (qi == NULL)
2803 {
2804 EMSG(_(e_loclist));
2805 return;
2806 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002807 }
2808
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002809 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 ? FORWARD
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002811 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002813 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
2814 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 ? BACKWARD_FILE
2816 : BACKWARD,
2817 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
2818}
2819
2820/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002821 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002822 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 */
2824 void
2825ex_cfile(eap)
2826 exarg_T *eap;
2827{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002828 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002829 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002830
2831 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
2832 || eap->cmdidx == CMD_laddfile)
2833 {
2834 qi = ll_get_or_alloc_list(curwin);
2835 if (qi == NULL)
2836 return;
2837 wp = curwin;
2838 }
2839
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 if (*eap->arg != NUL)
2841 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002842
2843 /*
2844 * This function is used by the :cfile, :cgetfile and :caddfile
2845 * commands.
2846 * :cfile always creates a new quickfix list and jumps to the
2847 * first error.
2848 * :cgetfile creates a new quickfix list but doesn't jump to the
2849 * first error.
2850 * :caddfile adds to an existing quickfix list. If there is no
2851 * quickfix list then a new list is created.
2852 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002853 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
2854 && eap->cmdidx != CMD_laddfile)) > 0
2855 && (eap->cmdidx == CMD_cfile
2856 || eap->cmdidx == CMD_lfile))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002857 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858}
2859
2860/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002861 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00002862 * ":vimgrepadd {pattern} file(s)"
2863 * ":lvimgrep {pattern} file(s)"
2864 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00002865 */
2866 void
2867ex_vimgrep(eap)
2868 exarg_T *eap;
2869{
Bram Moolenaar81695252004-12-29 20:58:21 +00002870 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002871 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002872 char_u **fnames;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002873 char_u *s;
2874 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002875 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00002876 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002877 qfline_T *prevp = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002878 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00002879 buf_T *buf;
2880 int duplicate_name = FALSE;
2881 int using_dummy;
2882 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002883 buf_T *first_match_buf = NULL;
2884 time_t seconds = 0;
2885#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2886 char_u *save_ei = NULL;
2887 aco_save_T aco;
2888#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002889#ifdef FEAT_AUTOCMD
2890 char_u *au_name = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002891 int flags = 0;
2892 colnr_T col;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002893
2894 switch (eap->cmdidx)
2895 {
2896 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00002897 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002898 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00002899 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002900 default: break;
2901 }
2902 if (au_name != NULL)
2903 {
2904 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2905 curbuf->b_fname, TRUE, curbuf);
2906 if (did_throw || force_abort)
2907 return;
2908 }
2909#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00002910
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002911 if (eap->cmdidx == CMD_grep
2912 || eap->cmdidx == CMD_lvimgrep
2913 || eap->cmdidx == CMD_lgrepadd
2914 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00002915 {
2916 qi = ll_get_or_alloc_list(curwin);
2917 if (qi == NULL)
2918 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00002919 }
2920
Bram Moolenaar81695252004-12-29 20:58:21 +00002921 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00002922 regmatch.regprog = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002923 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00002924 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002925 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00002926 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00002927 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00002928 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002929 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002930 if (regmatch.regprog == NULL)
2931 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002932 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00002933 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002934
2935 p = skipwhite(p);
2936 if (*p == NUL)
2937 {
2938 EMSG(_("E683: File name missing or invalid pattern"));
2939 goto theend;
2940 }
2941
Bram Moolenaara6557602006-02-04 22:43:20 +00002942 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
2943 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002944 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002945 /* make place for a new list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002946 qf_new_list(qi);
2947 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002948 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002949 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002950 prevp->qf_next != prevp; prevp = prevp->qf_next)
2951 ;
2952
2953 /* parse the list of arguments */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002954 if (get_arglist_exp(p, &fcount, &fnames) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002955 goto theend;
2956 if (fcount == 0)
2957 {
2958 EMSG(_(e_nomatch));
2959 goto theend;
2960 }
2961
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002962 seconds = (time_t)0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002963 for (fi = 0; fi < fcount && !got_int; ++fi)
2964 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002965 if (time(NULL) > seconds)
2966 {
2967 /* Display the file name every second or so. */
2968 seconds = time(NULL);
2969 msg_start();
Bram Moolenaara5373fa2005-09-09 19:47:12 +00002970 p = msg_strtrunc(fnames[fi], TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002971 if (p == NULL)
2972 msg_outtrans(fnames[fi]);
2973 else
2974 {
2975 msg_outtrans(p);
2976 vim_free(p);
2977 }
2978 msg_clr_eos();
2979 msg_didout = FALSE; /* overwrite this message */
2980 msg_nowait = TRUE; /* don't wait for this message */
2981 msg_col = 0;
2982 out_flush();
2983 }
2984
Bram Moolenaar81695252004-12-29 20:58:21 +00002985 buf = buflist_findname_exp(fnames[fi]);
2986 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
2987 {
2988 /* Remember that a buffer with this name already exists. */
2989 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002990 using_dummy = TRUE;
2991
2992#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2993 /* Don't do Filetype autocommands to avoid loading syntax and
2994 * indent scripts, a great speed improvement. */
2995 save_ei = au_event_disable(",Filetype");
2996#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00002997
2998 /* Load file into a buffer, so that 'fileencoding' is detected,
2999 * autocommands applied, etc. */
3000 buf = load_dummy_buffer(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003001
3002#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3003 au_event_restore(save_ei);
3004#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003005 }
3006 else
3007 /* Use existing, loaded buffer. */
3008 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003009
Bram Moolenaar81695252004-12-29 20:58:21 +00003010 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003011 {
3012 if (!got_int)
3013 smsg((char_u *)_("Cannot open file \"%s\""), fnames[fi]);
3014 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003015 else
3016 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003017 found_match = FALSE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003018 /* Try for a match in all lines of the buffer. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003019 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003020 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003021 /* For ":1vimgrep" look for multiple matches. */
3022 col = 0;
3023 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
3024 col) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003025 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003026 if (qf_add_entry(qi, &prevp,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003027 NULL, /* dir */
3028 fnames[fi],
Bram Moolenaar81695252004-12-29 20:58:21 +00003029 ml_get_buf(buf,
3030 regmatch.startpos[0].lnum + lnum, FALSE),
3031 regmatch.startpos[0].lnum + lnum,
3032 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003033 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003034 NULL, /* search pattern */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003035 0, /* nr */
3036 0, /* type */
3037 TRUE /* valid */
3038 ) == FAIL)
3039 {
3040 got_int = TRUE;
3041 break;
3042 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003043 else
3044 found_match = TRUE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003045 if ((flags & VGR_GLOBAL) == 0
3046 || regmatch.endpos[0].lnum > 0)
3047 break;
3048 col = regmatch.endpos[0].col
3049 + (col == regmatch.endpos[0].col);
3050 if (col > STRLEN(ml_get_buf(buf, lnum, FALSE)))
3051 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003052 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003053 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003054 if (got_int)
3055 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003056 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003057
3058 if (using_dummy)
3059 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003060 if (found_match && first_match_buf == NULL)
3061 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003062 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003063 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003064 /* Never keep a dummy buffer if there is another buffer
3065 * with the same name. */
3066 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003067 buf = NULL;
3068 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003069 else if (!buf_hide(buf))
3070 {
3071 /* When not hiding the buffer and no match was found we
3072 * don't need to remember the buffer, wipe it out. If
Bram Moolenaar05159a02005-02-26 23:04:13 +00003073 * there was a match and it wasn't the first one or we
3074 * won't jump there: only unload the buffer. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003075 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003076 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003077 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003078 buf = NULL;
3079 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003080 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003081 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003082 unload_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003083 buf = NULL;
3084 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003085 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003086
3087#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3088 if (buf != NULL)
3089 {
3090 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaar748bf032005-02-02 23:04:36 +00003091 * need to be done now, in that buffer. And then the
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00003092 * modelines need to be done (again). */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003093 aucmd_prepbuf(&aco, buf);
3094 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3095 buf->b_fname, TRUE, buf);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003096 do_modelines(FALSE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003097 aucmd_restbuf(&aco);
3098 }
3099#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003100 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003101 }
3102 }
3103
3104 FreeWild(fcount, fnames);
3105
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003106 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3107 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3108 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003109
3110#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003111 qf_update_buffer(qi);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003112#endif
3113
3114 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003115 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003116 {
3117 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003118 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003119 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003120 else
3121 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003122
Bram Moolenaar7c626922005-02-07 22:01:03 +00003123#ifdef FEAT_AUTOCMD
3124 if (au_name != NULL)
3125 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3126 curbuf->b_fname, TRUE, curbuf);
3127#endif
3128
Bram Moolenaar86b68352004-12-27 21:59:20 +00003129theend:
3130 vim_free(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003131}
3132
3133/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00003134 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00003135 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003136 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3137 * If "s" is not NULL terminate the pattern with a NUL.
3138 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00003139 */
3140 char_u *
Bram Moolenaar05159a02005-02-26 23:04:13 +00003141skip_vimgrep_pat(p, s, flags)
3142 char_u *p;
3143 char_u **s;
3144 int *flags;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003145{
3146 int c;
3147
3148 if (vim_isIDc(*p))
3149 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003150 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003151 if (s != NULL)
3152 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003153 p = skiptowhite(p);
3154 if (s != NULL && *p != NUL)
3155 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003156 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003157 else
3158 {
3159 /* ":vimgrep /pattern/[g][j] fname" */
3160 if (s != NULL)
3161 *s = p + 1;
3162 c = *p;
3163 p = skip_regexp(p + 1, c, TRUE, NULL);
3164 if (*p != c)
3165 return NULL;
3166
3167 /* Truncate the pattern. */
3168 if (s != NULL)
3169 *p = NUL;
3170 ++p;
3171
3172 /* Find the flags */
3173 while (*p == 'g' || *p == 'j')
3174 {
3175 if (flags != NULL)
3176 {
3177 if (*p == 'g')
3178 *flags |= VGR_GLOBAL;
3179 else
3180 *flags |= VGR_NOJUMP;
3181 }
3182 ++p;
3183 }
3184 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00003185 return p;
3186}
3187
3188/*
Bram Moolenaar81695252004-12-29 20:58:21 +00003189 * Load file "fname" into a dummy buffer and return the buffer pointer.
3190 * Returns NULL if it fails.
3191 * Must call unload_dummy_buffer() or wipe_dummy_buffer() later!
3192 */
3193 static buf_T *
3194load_dummy_buffer(fname)
3195 char_u *fname;
3196{
3197 buf_T *newbuf;
3198 int failed = TRUE;
3199#ifdef FEAT_AUTOCMD
3200 aco_save_T aco;
3201#else
3202 buf_T *old_curbuf = curbuf;
3203#endif
3204
3205 /* Allocate a buffer without putting it in the buffer list. */
3206 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
3207 if (newbuf == NULL)
3208 return NULL;
3209
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00003210 /* Init the options. */
3211 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
3212
Bram Moolenaar81695252004-12-29 20:58:21 +00003213#ifdef FEAT_AUTOCMD
3214 /* set curwin/curbuf to buf and save a few things */
3215 aucmd_prepbuf(&aco, newbuf);
3216#else
3217 curbuf = newbuf;
3218 curwin->w_buffer = newbuf;
3219#endif
3220
3221 /* Need to set the filename for autocommands. */
3222 (void)setfname(curbuf, fname, NULL, FALSE);
3223
Bram Moolenaar4770d092006-01-12 23:22:24 +00003224 if (ml_open(curbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00003225 {
3226 /* Create swap file now to avoid the ATTENTION message. */
3227 check_need_swap(TRUE);
3228
3229 /* Remove the "dummy" flag, otherwise autocommands may not
3230 * work. */
3231 curbuf->b_flags &= ~BF_DUMMY;
3232
3233 if (readfile(fname, NULL,
3234 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
3235 NULL, READ_NEW | READ_DUMMY) == OK
3236 && !(curbuf->b_flags & BF_NEW))
3237 {
3238 failed = FALSE;
3239 if (curbuf != newbuf)
3240 {
3241 /* Bloody autocommands changed the buffer! */
3242 if (buf_valid(newbuf))
3243 wipe_buffer(newbuf, FALSE);
3244 newbuf = curbuf;
3245 }
3246 }
3247 }
3248
3249#ifdef FEAT_AUTOCMD
3250 /* restore curwin/curbuf and a few other things */
3251 aucmd_restbuf(&aco);
3252#else
3253 curbuf = old_curbuf;
3254 curwin->w_buffer = old_curbuf;
3255#endif
3256
3257 if (!buf_valid(newbuf))
3258 return NULL;
3259 if (failed)
3260 {
3261 wipe_dummy_buffer(newbuf);
3262 return NULL;
3263 }
3264 return newbuf;
3265}
3266
3267/*
3268 * Wipe out the dummy buffer that load_dummy_buffer() created.
3269 */
3270 static void
3271wipe_dummy_buffer(buf)
3272 buf_T *buf;
3273{
3274 if (curbuf != buf) /* safety check */
3275 wipe_buffer(buf, FALSE);
3276}
3277
3278/*
3279 * Unload the dummy buffer that load_dummy_buffer() created.
3280 */
3281 static void
3282unload_dummy_buffer(buf)
3283 buf_T *buf;
3284{
3285 if (curbuf != buf) /* safety check */
3286 close_buffer(NULL, buf, DOBUF_UNLOAD);
3287}
3288
Bram Moolenaar05159a02005-02-26 23:04:13 +00003289#if defined(FEAT_EVAL) || defined(PROTO)
3290/*
3291 * Add each quickfix error to list "list" as a dictionary.
3292 */
3293 int
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003294get_errorlist(wp, list)
3295 win_T *wp;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003296 list_T *list;
3297{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003298 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003299 dict_T *dict;
3300 char_u buf[2];
3301 qfline_T *qfp;
3302 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003303
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003304 if (wp != NULL)
3305 {
3306 qi = GET_LOC_LIST(wp);
3307 if (qi == NULL)
3308 return FAIL;
3309 }
3310
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003311 if (qi->qf_curlist >= qi->qf_listcount
3312 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003313 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003314
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003315 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3316 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003317 {
3318 if ((dict = dict_alloc()) == NULL)
3319 return FAIL;
3320 if (list_append_dict(list, dict) == FAIL)
3321 return FAIL;
3322
3323 buf[0] = qfp->qf_type;
3324 buf[1] = NUL;
3325 if ( dict_add_nr_str(dict, "bufnr", (long)qfp->qf_fnum, NULL) == FAIL
3326 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
3327 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
3328 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
3329 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003330 || dict_add_nr_str(dict, "pattern", 0L, qfp->qf_pattern) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003331 || dict_add_nr_str(dict, "text", 0L, qfp->qf_text) == FAIL
3332 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
3333 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
3334 return FAIL;
3335
3336 qfp = qfp->qf_next;
3337 }
3338 return OK;
3339}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003340
3341/*
3342 * Populate the quickfix list with the items supplied in the list
3343 * of dictionaries.
3344 */
3345 int
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003346set_errorlist(wp, list, action)
3347 win_T *wp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003348 list_T *list;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003349 int action;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003350{
3351 listitem_T *li;
3352 dict_T *d;
3353 char_u *filename, *pattern, *text, *type;
3354 long lnum;
3355 int col, nr;
3356 int vcol;
3357 qfline_T *prevp = NULL;
3358 int valid, status;
3359 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003360 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003361
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003362 if (wp != NULL)
3363 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00003364 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003365 if (qi == NULL)
3366 return FAIL;
3367 }
3368
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003369 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003370 /* make place for a new list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003371 qf_new_list(qi);
3372 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003373 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003374 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003375 prevp->qf_next != prevp; prevp = prevp->qf_next)
3376 ;
3377 else if (action == 'r')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003378 qf_free(qi, qi->qf_curlist);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003379
3380 for (li = list->lv_first; li != NULL; li = li->li_next)
3381 {
3382 if (li->li_tv.v_type != VAR_DICT)
3383 continue; /* Skip non-dict items */
3384
3385 d = li->li_tv.vval.v_dict;
3386 if (d == NULL)
3387 continue;
3388
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003389 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003390 lnum = get_dict_number(d, (char_u *)"lnum");
3391 col = get_dict_number(d, (char_u *)"col");
3392 vcol = get_dict_number(d, (char_u *)"vcol");
3393 nr = get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003394 type = get_dict_string(d, (char_u *)"type", TRUE);
3395 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
3396 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003397 if (text == NULL)
3398 text = vim_strsave((char_u *)"");
3399
3400 valid = TRUE;
3401 if (filename == NULL || (lnum == 0 && pattern == NULL))
3402 valid = FALSE;
3403
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003404 status = qf_add_entry(qi, &prevp,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003405 NULL, /* dir */
3406 filename,
3407 text,
3408 lnum,
3409 col,
3410 vcol, /* vis_col */
3411 pattern, /* search pattern */
3412 nr,
3413 type == NULL ? NUL : *type,
3414 valid);
3415
3416 vim_free(filename);
3417 vim_free(pattern);
3418 vim_free(text);
3419 vim_free(type);
3420
3421 if (status == FAIL)
3422 {
3423 retval = FAIL;
3424 break;
3425 }
3426 }
3427
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003428 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3429 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3430 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003431
3432#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003433 qf_update_buffer(qi);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003434#endif
3435
3436 return retval;
3437}
Bram Moolenaar05159a02005-02-26 23:04:13 +00003438#endif
3439
Bram Moolenaar81695252004-12-29 20:58:21 +00003440/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003441 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003442 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003443 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003444 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00003445 */
3446 void
3447ex_cbuffer(eap)
3448 exarg_T *eap;
3449{
3450 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003451 qf_info_T *qi = &ql_info;
3452
Bram Moolenaara6557602006-02-04 22:43:20 +00003453 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003454 {
3455 qi = ll_get_or_alloc_list(curwin);
3456 if (qi == NULL)
3457 return;
3458 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003459
3460 if (*eap->arg == NUL)
3461 buf = curbuf;
3462 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
3463 buf = buflist_findnr(atoi((char *)eap->arg));
3464 if (buf == NULL)
3465 EMSG(_(e_invarg));
3466 else if (buf->b_ml.ml_mfp == NULL)
3467 EMSG(_("E681: Buffer is not loaded"));
3468 else
3469 {
3470 if (eap->addr_count == 0)
3471 {
3472 eap->line1 = 1;
3473 eap->line2 = buf->b_ml.ml_line_count;
3474 }
3475 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
3476 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
3477 EMSG(_(e_invrange));
3478 else
Bram Moolenaara6557602006-02-04 22:43:20 +00003479 qf_init_ext(qi, NULL, buf, NULL, p_efm,
3480 (eap->cmdidx == CMD_cbuffer
3481 || eap->cmdidx == CMD_lbuffer),
3482 eap->line1, eap->line2);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003483 }
3484}
3485
Bram Moolenaar1e015462005-09-25 22:16:38 +00003486#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003487/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003488 * ":cexpr {expr}" and ":caddexpr {expr}" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003489 * ":lexpr {expr}" and ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003490 */
3491 void
3492ex_cexpr(eap)
3493 exarg_T *eap;
3494{
3495 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003496 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003497
3498 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_laddexpr)
3499 {
3500 qi = ll_get_or_alloc_list(curwin);
3501 if (qi == NULL)
3502 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003503 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003504
Bram Moolenaar4770d092006-01-12 23:22:24 +00003505 /* Evaluate the expression. When the result is a string or a list we can
3506 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003507 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003508 if (tv != NULL)
3509 {
3510 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
3511 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
3512 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003513 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
3514 (eap->cmdidx == CMD_cexpr
3515 || eap->cmdidx == CMD_lexpr),
Bram Moolenaar4770d092006-01-12 23:22:24 +00003516 (linenr_T)0, (linenr_T)0) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003517 && (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003518 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003519 }
3520 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003521 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00003522 free_tv(tv);
3523 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003524}
Bram Moolenaar1e015462005-09-25 22:16:38 +00003525#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003526
3527/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 * ":helpgrep {pattern}"
3529 */
3530 void
3531ex_helpgrep(eap)
3532 exarg_T *eap;
3533{
3534 regmatch_T regmatch;
3535 char_u *save_cpo;
3536 char_u *p;
3537 int fcount;
3538 char_u **fnames;
3539 FILE *fd;
3540 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003541 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003543#ifdef FEAT_MULTI_LANG
3544 char_u *lang;
3545#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003546 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003547 int new_qi = FALSE;
3548 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549
3550 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
3551 save_cpo = p_cpo;
3552 p_cpo = (char_u *)"";
3553
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003554#ifdef FEAT_MULTI_LANG
3555 /* Check for a specified language */
3556 lang = check_help_lang(eap->arg);
3557#endif
3558
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003559 if (eap->cmdidx == CMD_lhelpgrep)
3560 {
3561 /* Find an existing help window */
3562 FOR_ALL_WINDOWS(wp)
3563 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
3564 break;
3565
3566 if (wp == NULL) /* Help window not found */
3567 qi = NULL;
3568 else
3569 qi = wp->w_llist;
3570
3571 if (qi == NULL)
3572 {
3573 /* Allocate a new location list for help text matches */
3574 if ((qi = ll_new_list()) == NULL)
3575 return;
3576 new_qi = TRUE;
3577 }
3578 }
3579
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
3581 regmatch.rm_ic = FALSE;
3582 if (regmatch.regprog != NULL)
3583 {
3584 /* create a new quickfix list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003585 qf_new_list(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586
3587 /* Go through all directories in 'runtimepath' */
3588 p = p_rtp;
3589 while (*p != NUL && !got_int)
3590 {
3591 copy_option_part(&p, NameBuff, MAXPATHL, ",");
3592
3593 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
3594 add_pathsep(NameBuff);
3595 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
3596 if (gen_expand_wildcards(1, &NameBuff, &fcount,
3597 &fnames, EW_FILE|EW_SILENT) == OK
3598 && fcount > 0)
3599 {
3600 for (fi = 0; fi < fcount && !got_int; ++fi)
3601 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003602#ifdef FEAT_MULTI_LANG
3603 /* Skip files for a different language. */
3604 if (lang != NULL
3605 && STRNICMP(lang, fnames[fi]
3606 + STRLEN(fnames[fi]) - 3, 2) != 0
3607 && !(STRNICMP(lang, "en", 2) == 0
3608 && STRNICMP("txt", fnames[fi]
3609 + STRLEN(fnames[fi]) - 3, 3) == 0))
3610 continue;
3611#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003612 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 if (fd != NULL)
3614 {
3615 lnum = 1;
3616 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
3617 {
3618 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
3619 {
3620 int l = STRLEN(IObuff);
3621
3622 /* remove trailing CR, LF, spaces, etc. */
3623 while (l > 0 && IObuff[l - 1] <= ' ')
3624 IObuff[--l] = NUL;
3625
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003626 if (qf_add_entry(qi, &prevp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 NULL, /* dir */
3628 fnames[fi],
3629 IObuff,
3630 lnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00003631 (int)(regmatch.startp[0] - IObuff)
3632 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00003633 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003634 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 0, /* nr */
3636 1, /* type */
3637 TRUE /* valid */
3638 ) == FAIL)
3639 {
3640 got_int = TRUE;
3641 break;
3642 }
3643 }
3644 ++lnum;
3645 line_breakcheck();
3646 }
3647 fclose(fd);
3648 }
3649 }
3650 FreeWild(fcount, fnames);
3651 }
3652 }
3653 vim_free(regmatch.regprog);
3654
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003655 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3656 qi->qf_lists[qi->qf_curlist].qf_ptr =
3657 qi->qf_lists[qi->qf_curlist].qf_start;
3658 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 }
3660
3661 p_cpo = save_cpo;
3662
3663#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003664 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665#endif
3666
3667 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003668 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003669 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003670 else
3671 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003672
3673 if (eap->cmdidx == CMD_lhelpgrep)
3674 {
3675 /* If the help window is not opened or if it already points to the
3676 * correct location list, then free the new location list
3677 */
3678 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
3679 {
3680 if (new_qi)
3681 ll_free_all(&qi);
3682 }
3683 else if (curwin->w_llist == NULL)
3684 curwin->w_llist = qi;
3685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686}
3687
3688#endif /* FEAT_QUICKFIX */