blob: 2ffce40a81094f73711a5c2c3eae146a8ac5258d [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 Moolenaarc7453f52006-02-10 23:20:28 +0000128static qf_info_T *ll_get_or_alloc_list __ARGS((win_T *));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000130/* Quickfix window check helper macro */
131#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
132/* Location list window check helper macro */
133#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
134/*
135 * Return location list for window 'wp'
136 * For location list window, return the referenced location list
137 */
138#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
139
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000141 * Read the errorfile "efile" into memory, line by line, building the error
142 * list.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 * Return -1 for error, number of errors for success.
144 */
145 int
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000146qf_init(wp, efile, errorformat, newlist)
147 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 char_u *efile;
149 char_u *errorformat;
150 int newlist; /* TRUE: start a new error list */
151{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000152 qf_info_T *qi = &ql_info;
153
Bram Moolenaar86b68352004-12-27 21:59:20 +0000154 if (efile == NULL)
155 return FAIL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000156
157 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000158 {
159 qi = ll_get_or_alloc_list(wp);
160 if (qi == NULL)
161 return FAIL;
162 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000163
164 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar86b68352004-12-27 21:59:20 +0000165 (linenr_T)0, (linenr_T)0);
166}
167
168/*
169 * Read the errorfile "efile" into memory, line by line, building the error
170 * list.
171 * Alternative: when "efile" is null read errors from buffer "buf".
172 * Always use 'errorformat' from "buf" if there is a local value.
173 * Then lnumfirst and lnumlast specify the range of lines to use.
174 * Return -1 for error, number of errors for success.
175 */
176 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000177qf_init_ext(qi, efile, buf, tv, errorformat, newlist, lnumfirst, lnumlast)
178 qf_info_T *qi;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000179 char_u *efile;
180 buf_T *buf;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000181 typval_T *tv;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000182 char_u *errorformat;
183 int newlist; /* TRUE: start a new error list */
184 linenr_T lnumfirst; /* first line number to use */
185 linenr_T lnumlast; /* last line number to use */
186{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 char_u *namebuf;
188 char_u *errmsg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000189 char_u *pattern;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 char_u *fmtstr = NULL;
191 int col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000192 char_u use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 int type = 0;
194 int valid;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000195 linenr_T buflnum = lnumfirst;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 long lnum = 0L;
197 int enr = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000198 FILE *fd = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000199 qfline_T *qfprev = NULL; /* init to make SASC shut up */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 char_u *efmp;
201 struct eformat *fmt_first = NULL;
202 struct eformat *fmt_last = NULL;
203 struct eformat *fmt_ptr;
204 char_u *efm;
205 char_u *ptr;
206 char_u *srcptr;
207 int len;
208 int i;
209 int round;
210 int idx = 0;
211 int multiline = FALSE;
212 int multiignore = FALSE;
213 int multiscan = FALSE;
214 int retval = -1; /* default: return error flag */
215 char_u *directory = NULL;
216 char_u *currfile = NULL;
217 char_u *tail = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000218 char_u *p_str = NULL;
219 listitem_T *p_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 struct dir_stack_T *file_stack = NULL;
221 regmatch_T regmatch;
222 static struct fmtpattern
223 {
224 char_u convchar;
225 char *pattern;
226 } fmt_pat[FMT_PATTERNS] =
227 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000228 {'f', ".\\+"}, /* only used when at end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 {'n', "\\d\\+"},
230 {'l', "\\d\\+"},
231 {'c', "\\d\\+"},
232 {'t', "."},
233 {'m', ".\\+"},
234 {'r', ".*"},
235 {'p', "[- .]*"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000236 {'v', "\\d\\+"},
237 {'s', ".\\+"}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 };
239
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 namebuf = alloc(CMDBUFFSIZE + 1);
241 errmsg = alloc(CMDBUFFSIZE + 1);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000242 pattern = alloc(CMDBUFFSIZE + 1);
243 if (namebuf == NULL || errmsg == NULL || pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 goto qf_init_end;
245
Bram Moolenaar86b68352004-12-27 21:59:20 +0000246 if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 {
248 EMSG2(_(e_openerrf), efile);
249 goto qf_init_end;
250 }
251
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000252 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 /* make place for a new list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000254 qf_new_list(qi);
255 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000257 for (qfprev = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 qfprev->qf_next != qfprev; qfprev = qfprev->qf_next)
259 ;
260
261/*
262 * Each part of the format string is copied and modified from errorformat to
263 * regex prog. Only a few % characters are allowed.
264 */
265 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000266 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000267 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 else
269 efm = errorformat;
270 /*
271 * Get some space to modify the format string into.
272 */
273 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
274 for (round = FMT_PATTERNS; round > 0; )
275 i += (int)STRLEN(fmt_pat[--round].pattern);
276#ifdef COLON_IN_FILENAME
277 i += 12; /* "%f" can become twelve chars longer */
278#else
279 i += 2; /* "%f" can become two chars longer */
280#endif
281 if ((fmtstr = alloc(i)) == NULL)
282 goto error2;
283
284 while (efm[0])
285 {
286 /*
287 * Allocate a new eformat structure and put it at the end of the list
288 */
289 fmt_ptr = (struct eformat *)alloc((unsigned)sizeof(struct eformat));
290 if (fmt_ptr == NULL)
291 goto error2;
292 if (fmt_first == NULL) /* first one */
293 fmt_first = fmt_ptr;
294 else
295 fmt_last->next = fmt_ptr;
296 fmt_last = fmt_ptr;
297 fmt_ptr->prefix = NUL;
298 fmt_ptr->flags = NUL;
299 fmt_ptr->next = NULL;
300 fmt_ptr->prog = NULL;
301 for (round = FMT_PATTERNS; round > 0; )
302 fmt_ptr->addr[--round] = NUL;
303 /* round is 0 now */
304
305 /*
306 * Isolate one part in the 'errorformat' option
307 */
308 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
309 if (efm[len] == '\\' && efm[len + 1] != NUL)
310 ++len;
311
312 /*
313 * Build regexp pattern from current 'errorformat' option
314 */
315 ptr = fmtstr;
316 *ptr++ = '^';
317 for (efmp = efm; efmp < efm + len; ++efmp)
318 {
319 if (*efmp == '%')
320 {
321 ++efmp;
322 for (idx = 0; idx < FMT_PATTERNS; ++idx)
323 if (fmt_pat[idx].convchar == *efmp)
324 break;
325 if (idx < FMT_PATTERNS)
326 {
327 if (fmt_ptr->addr[idx])
328 {
329 sprintf((char *)errmsg,
330 _("E372: Too many %%%c in format string"), *efmp);
331 EMSG(errmsg);
332 goto error2;
333 }
334 if ((idx
335 && idx < 6
336 && vim_strchr((char_u *)"DXOPQ",
337 fmt_ptr->prefix) != NULL)
338 || (idx == 6
339 && vim_strchr((char_u *)"OPQ",
340 fmt_ptr->prefix) == NULL))
341 {
342 sprintf((char *)errmsg,
343 _("E373: Unexpected %%%c in format string"), *efmp);
344 EMSG(errmsg);
345 goto error2;
346 }
347 fmt_ptr->addr[idx] = (char_u)++round;
348 *ptr++ = '\\';
349 *ptr++ = '(';
350#ifdef BACKSLASH_IN_FILENAME
351 if (*efmp == 'f')
352 {
353 /* Also match "c:" in the file name, even when
354 * checking for a colon next: "%f:".
355 * "\%(\a:\)\=" */
356 STRCPY(ptr, "\\%(\\a:\\)\\=");
357 ptr += 10;
358 }
359#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +0000360 if (*efmp == 'f' && efmp[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000362 if (efmp[1] != '\\' && efmp[1] != '%')
363 {
364 /* A file name may contain spaces, but this isn't
365 * in "\f". For "%f:%l:%m" there may be a ":" in
366 * the file name. Use ".\{-1,}x" instead (x is
367 * the next character), the requirement that :999:
368 * follows should work. */
369 STRCPY(ptr, ".\\{-1,}");
370 ptr += 7;
371 }
372 else
373 {
374 /* File name followed by '\\' or '%': include as
375 * many file name chars as possible. */
376 STRCPY(ptr, "\\f\\+");
377 ptr += 4;
378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 }
380 else
381 {
382 srcptr = (char_u *)fmt_pat[idx].pattern;
383 while ((*ptr = *srcptr++) != NUL)
384 ++ptr;
385 }
386 *ptr++ = '\\';
387 *ptr++ = ')';
388 }
389 else if (*efmp == '*')
390 {
391 if (*++efmp == '[' || *efmp == '\\')
392 {
393 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
394 {
395 if (efmp[1] == '^')
396 *ptr++ = *++efmp;
397 if (efmp < efm + len)
398 {
399 *ptr++ = *++efmp; /* could be ']' */
400 while (efmp < efm + len
401 && (*ptr++ = *++efmp) != ']')
402 /* skip */;
403 if (efmp == efm + len)
404 {
405 EMSG(_("E374: Missing ] in format string"));
406 goto error2;
407 }
408 }
409 }
410 else if (efmp < efm + len) /* %*\D, %*\s etc. */
411 *ptr++ = *++efmp;
412 *ptr++ = '\\';
413 *ptr++ = '+';
414 }
415 else
416 {
417 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
418 sprintf((char *)errmsg,
419 _("E375: Unsupported %%%c in format string"), *efmp);
420 EMSG(errmsg);
421 goto error2;
422 }
423 }
424 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
425 *ptr++ = *efmp; /* regexp magic characters */
426 else if (*efmp == '#')
427 *ptr++ = '*';
428 else if (efmp == efm + 1) /* analyse prefix */
429 {
430 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
431 fmt_ptr->flags = *efmp++;
432 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
433 fmt_ptr->prefix = *efmp;
434 else
435 {
436 sprintf((char *)errmsg,
437 _("E376: Invalid %%%c in format string prefix"), *efmp);
438 EMSG(errmsg);
439 goto error2;
440 }
441 }
442 else
443 {
444 sprintf((char *)errmsg,
445 _("E377: Invalid %%%c in format string"), *efmp);
446 EMSG(errmsg);
447 goto error2;
448 }
449 }
450 else /* copy normal character */
451 {
452 if (*efmp == '\\' && efmp + 1 < efm + len)
453 ++efmp;
454 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
455 *ptr++ = '\\'; /* escape regexp atoms */
456 if (*efmp)
457 *ptr++ = *efmp;
458 }
459 }
460 *ptr++ = '$';
461 *ptr = NUL;
462 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
463 goto error2;
464 /*
465 * Advance to next part
466 */
467 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
468 }
469 if (fmt_first == NULL) /* nothing found */
470 {
471 EMSG(_("E378: 'errorformat' contains no pattern"));
472 goto error2;
473 }
474
475 /*
476 * got_int is reset here, because it was probably set when killing the
477 * ":make" command, but we still want to read the errorfile then.
478 */
479 got_int = FALSE;
480
481 /* Always ignore case when looking for a matching error. */
482 regmatch.rm_ic = TRUE;
483
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000484 if (tv != NULL)
485 {
486 if (tv->v_type == VAR_STRING)
487 p_str = tv->vval.v_string;
488 else if (tv->v_type == VAR_LIST)
489 p_li = tv->vval.v_list->lv_first;
490 }
491
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 /*
493 * Read the lines in the error file one by one.
494 * Try to recognize one of the error formats in each line.
495 */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000496 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 {
Bram Moolenaar86b68352004-12-27 21:59:20 +0000498 /* Get the next line. */
499 if (fd == NULL)
500 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000501 if (tv != NULL)
502 {
503 int len;
504
505 if (tv->v_type == VAR_STRING)
506 {
507 /* Get the next line from the supplied string */
508 char_u *p;
509
510 if (!*p_str) /* Reached the end of the string */
511 break;
512
513 p = vim_strchr(p_str, '\n');
514 if (p)
515 len = p - p_str + 1;
516 else
517 len = STRLEN(p_str);
518
519 if (len > CMDBUFFSIZE - 2)
520 vim_strncpy(IObuff, p_str, CMDBUFFSIZE - 2);
521 else
522 vim_strncpy(IObuff, p_str, len);
523
524 p_str += len;
525 }
526 else if (tv->v_type == VAR_LIST)
527 {
528 /* Get the next line from the supplied list */
529 while (p_li && p_li->li_tv.v_type != VAR_STRING)
530 p_li = p_li->li_next; /* Skip non-string items */
531
532 if (!p_li) /* End of the list */
533 break;
534
535 len = STRLEN(p_li->li_tv.vval.v_string);
536 if (len > CMDBUFFSIZE - 2)
537 len = CMDBUFFSIZE - 2;
538
539 vim_strncpy(IObuff, p_li->li_tv.vval.v_string, len);
540
541 p_li = p_li->li_next; /* next item */
542 }
543 }
544 else
545 {
546 /* Get the next line from the supplied buffer */
547 if (buflnum > lnumlast)
548 break;
549 vim_strncpy(IObuff, ml_get_buf(buf, buflnum++, FALSE),
550 CMDBUFFSIZE - 2);
551 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000552 }
553 else if (fgets((char *)IObuff, CMDBUFFSIZE - 2, fd) == NULL)
554 break;
555
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 IObuff[CMDBUFFSIZE - 2] = NUL; /* for very long lines */
557 if ((efmp = vim_strrchr(IObuff, '\n')) != NULL)
558 *efmp = NUL;
559#ifdef USE_CRNL
560 if ((efmp = vim_strrchr(IObuff, '\r')) != NULL)
561 *efmp = NUL;
562#endif
563
564 /*
565 * Try to match each part of 'errorformat' until we find a complete
566 * match or no match.
567 */
568 valid = TRUE;
569restofline:
570 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
571 {
572 idx = fmt_ptr->prefix;
573 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
574 continue;
575 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000576 pattern[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 if (!multiscan)
578 errmsg[0] = NUL;
579 lnum = 0;
580 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000581 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 enr = -1;
583 type = 0;
584 tail = NULL;
585
586 regmatch.regprog = fmt_ptr->prog;
587 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
588 {
589 if ((idx == 'C' || idx == 'Z') && !multiline)
590 continue;
591 if (vim_strchr((char_u *)"EWI", idx) != NULL)
592 type = idx;
593 else
594 type = 0;
595 /*
596 * Extract error message data from matched line
597 */
598 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
599 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000600 int c = *regmatch.endp[i];
601
602 /* Expand ~/file and $HOME/file to full path. */
603 *regmatch.endp[i] = NUL;
604 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
605 *regmatch.endp[i] = c;
606
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000608 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 continue;
610 }
611 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
612 enr = (int)atol((char *)regmatch.startp[i]);
613 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
614 lnum = atol((char *)regmatch.startp[i]);
615 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
616 col = (int)atol((char *)regmatch.startp[i]);
617 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
618 type = *regmatch.startp[i];
Bram Moolenaar4770d092006-01-12 23:22:24 +0000619 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 STRCPY(errmsg, IObuff);
621 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
622 {
623 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaarbbebc852005-07-18 21:47:53 +0000624 vim_strncpy(errmsg, regmatch.startp[i], len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 }
626 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
627 tail = regmatch.startp[i];
628 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
629 {
630 col = (int)(regmatch.endp[i] - regmatch.startp[i] + 1);
631 if (*((char_u *)regmatch.startp[i]) != TAB)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000632 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 }
634 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
635 {
636 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000637 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000639 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
640 {
641 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
642 if (len > CMDBUFFSIZE - 5)
643 len = CMDBUFFSIZE - 5;
644 STRCPY(pattern, "^\\V");
645 STRNCAT(pattern, regmatch.startp[i], len);
646 pattern[len + 3] = '\\';
647 pattern[len + 4] = '$';
648 pattern[len + 5] = NUL;
649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 break;
651 }
652 }
653 multiscan = FALSE;
Bram Moolenaar4770d092006-01-12 23:22:24 +0000654 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 {
Bram Moolenaar4770d092006-01-12 23:22:24 +0000656 if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 {
658 if (idx == 'D') /* enter directory */
659 {
660 if (*namebuf == NUL)
661 {
662 EMSG(_("E379: Missing or empty directory name"));
663 goto error2;
664 }
665 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
666 goto error2;
667 }
668 else if (idx == 'X') /* leave directory */
669 directory = qf_pop_dir(&dir_stack);
670 }
671 namebuf[0] = NUL; /* no match found, remove file name */
672 lnum = 0; /* don't jump to this line */
673 valid = FALSE;
674 STRCPY(errmsg, IObuff); /* copy whole line to error message */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000675 if (fmt_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 multiline = multiignore = FALSE;
677 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000678 else if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 {
680 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
681 multiline = TRUE; /* start of a multi-line message */
682 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
683 { /* continuation of multi-line msg */
684 if (qfprev == NULL)
685 goto error2;
686 if (*errmsg && !multiignore)
687 {
688 len = (int)STRLEN(qfprev->qf_text);
689 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
690 == NULL)
691 goto error2;
692 STRCPY(ptr, qfprev->qf_text);
693 vim_free(qfprev->qf_text);
694 qfprev->qf_text = ptr;
695 *(ptr += len) = '\n';
696 STRCPY(++ptr, errmsg);
697 }
698 if (qfprev->qf_nr == -1)
699 qfprev->qf_nr = enr;
700 if (vim_isprintc(type) && !qfprev->qf_type)
701 qfprev->qf_type = type; /* only printable chars allowed */
702 if (!qfprev->qf_lnum)
703 qfprev->qf_lnum = lnum;
704 if (!qfprev->qf_col)
705 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000706 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 if (!qfprev->qf_fnum)
708 qfprev->qf_fnum = qf_get_fnum(directory,
709 *namebuf || directory ? namebuf
710 : currfile && valid ? currfile : 0);
711 if (idx == 'Z')
712 multiline = multiignore = FALSE;
713 line_breakcheck();
714 continue;
715 }
716 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
717 {
718 /* global file names */
719 valid = FALSE;
720 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
721 {
722 if (*namebuf && idx == 'P')
723 currfile = qf_push_dir(namebuf, &file_stack);
724 else if (idx == 'Q')
725 currfile = qf_pop_dir(&file_stack);
726 *namebuf = NUL;
727 if (tail && *tail)
728 {
729 STRCPY(IObuff, skipwhite(tail));
730 multiscan = TRUE;
731 goto restofline;
732 }
733 }
734 }
735 if (fmt_ptr->flags == '-') /* generally exclude this line */
736 {
737 if (multiline)
738 multiignore = TRUE; /* also exclude continuation lines */
739 continue;
740 }
741 }
742
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000743 if (qf_add_entry(qi, &qfprev,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 directory,
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000745 (*namebuf || directory)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 ? namebuf
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000747 : ((currfile && valid) ? currfile : (char_u *)NULL),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 errmsg,
749 lnum,
750 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +0000751 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000752 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 enr,
754 type,
755 valid) == FAIL)
756 goto error2;
757 line_breakcheck();
758 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000759 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000761 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000763 /* no valid entry found */
764 qi->qf_lists[qi->qf_curlist].qf_ptr =
765 qi->qf_lists[qi->qf_curlist].qf_start;
766 qi->qf_lists[qi->qf_curlist].qf_index = 1;
767 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 }
769 else
770 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000771 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
772 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
773 qi->qf_lists[qi->qf_curlist].qf_ptr =
774 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000776 /* return number of matches */
777 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 goto qf_init_ok;
779 }
780 EMSG(_(e_readerrf));
781error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000782 qf_free(qi, qi->qf_curlist);
783 qi->qf_listcount--;
784 if (qi->qf_curlist > 0)
785 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786qf_init_ok:
Bram Moolenaar86b68352004-12-27 21:59:20 +0000787 if (fd != NULL)
788 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
790 {
791 fmt_first = fmt_ptr->next;
792 vim_free(fmt_ptr->prog);
793 vim_free(fmt_ptr);
794 }
795 qf_clean_dir_stack(&dir_stack);
796 qf_clean_dir_stack(&file_stack);
797qf_init_end:
798 vim_free(namebuf);
799 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000800 vim_free(pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 vim_free(fmtstr);
802
803#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000804 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805#endif
806
807 return retval;
808}
809
810/*
811 * Prepare for adding a new quickfix list.
812 */
813 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000814qf_new_list(qi)
815 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816{
817 int i;
818
819 /*
820 * If the current entry is not the last entry, delete entries below
821 * the current entry. This makes it possible to browse in a tree-like
822 * way with ":grep'.
823 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000824 while (qi->qf_listcount > qi->qf_curlist + 1)
825 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826
827 /*
828 * When the stack is full, remove to oldest entry
829 * Otherwise, add a new entry.
830 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000831 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000833 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000835 qi->qf_lists[i - 1] = qi->qf_lists[i];
836 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 }
838 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000839 qi->qf_curlist = qi->qf_listcount++;
840 qi->qf_lists[qi->qf_curlist].qf_index = 0;
841 qi->qf_lists[qi->qf_curlist].qf_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842}
843
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000844/*
845 * Free a location list
846 */
847 static void
848ll_free_all(pqi)
849 qf_info_T **pqi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000850{
851 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000852 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000853
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000854 qi = *pqi;
855 if (qi == NULL)
856 return;
857 *pqi = NULL; /* Remove reference to this list */
858
859 qi->qf_refcount--;
860 if (qi->qf_refcount < 1)
861 {
862 /* No references to this location list */
863 for (i = 0; i < qi->qf_listcount; ++i)
864 qf_free(qi, i);
865 vim_free(qi);
866 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000867}
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000868
869 void
870qf_free_all(wp)
871 win_T *wp;
872{
873 int i;
874 qf_info_T *qi = &ql_info;
875
876 if (wp != NULL)
877 {
878 /* location list */
879 ll_free_all(&wp->w_llist);
880 ll_free_all(&wp->w_llist_ref);
881 }
882 else
883 /* quickfix list */
884 for (i = 0; i < qi->qf_listcount; ++i)
885 qf_free(qi, i);
886}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000887
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888/*
889 * Add an entry to the end of the list of errors.
890 * Returns OK or FAIL.
891 */
892 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000893qf_add_entry(qi, prevp, dir, fname, mesg, lnum, col, vis_col, pattern, nr, type,
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000894 valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000895 qf_info_T *qi; /* quickfix list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000896 qfline_T **prevp; /* pointer to previously added entry or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 char_u *dir; /* optional directory name */
898 char_u *fname; /* file name or NULL */
899 char_u *mesg; /* message */
900 long lnum; /* line number */
901 int col; /* column */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000902 int vis_col; /* using visual column */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000903 char_u *pattern; /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 int nr; /* error number */
905 int type; /* type character */
906 int valid; /* valid entry */
907{
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000908 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000910 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 return FAIL;
912 qfp->qf_fnum = qf_get_fnum(dir, fname);
913 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
914 {
915 vim_free(qfp);
916 return FAIL;
917 }
918 qfp->qf_lnum = lnum;
919 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000920 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000921 if (pattern == NULL || *pattern == NUL)
922 qfp->qf_pattern = NULL;
923 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
924 {
925 vim_free(qfp->qf_text);
926 vim_free(qfp);
927 return FAIL;
928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 qfp->qf_nr = nr;
930 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
931 type = 0;
932 qfp->qf_type = type;
933 qfp->qf_valid = valid;
934
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000935 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
936 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000938 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 qfp->qf_prev = qfp; /* first element points to itself */
940 }
941 else
942 {
943 qfp->qf_prev = *prevp;
944 (*prevp)->qf_next = qfp;
945 }
946 qfp->qf_next = qfp; /* last element points to itself */
947 qfp->qf_cleared = FALSE;
948 *prevp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000949 ++qi->qf_lists[qi->qf_curlist].qf_count;
950 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
951 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000953 qi->qf_lists[qi->qf_curlist].qf_index =
954 qi->qf_lists[qi->qf_curlist].qf_count;
955 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 }
957
958 return OK;
959}
960
961/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000962 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000963 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000964 static qf_info_T *
965ll_new_list()
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000966{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000967 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000968
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000969 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
970 if (qi != NULL)
971 {
972 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
973 qi->qf_refcount++;
974 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000975
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000976 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000977}
978
979/*
980 * Return the location list for window 'wp'.
981 * If not present, allocate a location list
982 */
983 static qf_info_T *
984ll_get_or_alloc_list(wp)
985 win_T *wp;
986{
987 if (IS_LL_WINDOW(wp))
988 /* For a location list window, use the referenced location list */
989 return wp->w_llist_ref;
990
991 /*
992 * For a non-location list window, w_llist_ref should not point to a
993 * location list.
994 */
995 ll_free_all(&wp->w_llist_ref);
996
997 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000998 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000999 return wp->w_llist;
1000}
1001
1002/*
1003 * Copy the location list from window "from" to window "to".
1004 */
1005 void
1006copy_loclist(from, to)
1007 win_T *from;
1008 win_T *to;
1009{
1010 qf_info_T *qi;
1011 int idx;
1012 int i;
1013
1014 /*
1015 * When copying from a location list window, copy the referenced
1016 * location list. For other windows, copy the location list for
1017 * that window.
1018 */
1019 if (IS_LL_WINDOW(from))
1020 qi = from->w_llist_ref;
1021 else
1022 qi = from->w_llist;
1023
1024 if (qi == NULL) /* no location list to copy */
1025 return;
1026
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001027 /* allocate a new location list */
1028 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001029 return;
1030
1031 to->w_llist->qf_listcount = qi->qf_listcount;
1032
1033 /* Copy the location lists one at a time */
1034 for (idx = 0; idx < qi->qf_listcount; idx++)
1035 {
1036 qf_list_T *from_qfl;
1037 qf_list_T *to_qfl;
1038
1039 to->w_llist->qf_curlist = idx;
1040
1041 from_qfl = &qi->qf_lists[idx];
1042 to_qfl = &to->w_llist->qf_lists[idx];
1043
1044 /* Some of the fields are populated by qf_add_entry() */
1045 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1046 to_qfl->qf_count = 0;
1047 to_qfl->qf_index = 0;
1048 to_qfl->qf_start = NULL;
1049 to_qfl->qf_ptr = NULL;
1050
1051 if (from_qfl->qf_count)
1052 {
1053 qfline_T *from_qfp;
1054 qfline_T *prevp = NULL;
1055
1056 /* copy all the location entries in this list */
1057 for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
1058 ++i, from_qfp = from_qfp->qf_next)
1059 {
1060 if (qf_add_entry(to->w_llist, &prevp,
1061 NULL,
1062 NULL,
1063 from_qfp->qf_text,
1064 from_qfp->qf_lnum,
1065 from_qfp->qf_col,
1066 from_qfp->qf_viscol,
1067 from_qfp->qf_pattern,
1068 from_qfp->qf_nr,
1069 0,
1070 from_qfp->qf_valid) == FAIL)
1071 {
1072 qf_free_all(to);
1073 return;
1074 }
1075 /*
1076 * qf_add_entry() will not set the qf_num field, as the
1077 * directory and file names are not supplied. So the qf_fnum
1078 * field is copied here.
1079 */
1080 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1081 prevp->qf_type = from_qfp->qf_type; /* error type */
1082 if (from_qfl->qf_ptr == from_qfp)
1083 to_qfl->qf_ptr = prevp; /* current location */
1084 }
1085 }
1086
1087 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1088
1089 /* When no valid entries are present in the list, qf_ptr points to
1090 * the first item in the list */
1091 if (to_qfl->qf_nonevalid == TRUE)
1092 to_qfl->qf_ptr = to_qfl->qf_start;
1093 }
1094
1095 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1096}
1097
1098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 * get buffer number for file "dir.name"
1100 */
1101 static int
1102qf_get_fnum(directory, fname)
1103 char_u *directory;
1104 char_u *fname;
1105{
1106 if (fname == NULL || *fname == NUL) /* no file name */
1107 return 0;
1108 {
1109#ifdef RISCOS
1110 /* Name is reported as `main.c', but file is `c.main' */
1111 return ro_buflist_add(fname);
1112#else
1113 char_u *ptr;
1114 int fnum;
1115
1116# ifdef VMS
1117 vms_remove_version(fname);
1118# endif
1119# ifdef BACKSLASH_IN_FILENAME
1120 if (directory != NULL)
1121 slash_adjust(directory);
1122 slash_adjust(fname);
1123# endif
1124 if (directory != NULL && !vim_isAbsName(fname)
1125 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1126 {
1127 /*
1128 * Here we check if the file really exists.
1129 * This should normally be true, but if make works without
1130 * "leaving directory"-messages we might have missed a
1131 * directory change.
1132 */
1133 if (mch_getperm(ptr) < 0)
1134 {
1135 vim_free(ptr);
1136 directory = qf_guess_filepath(fname);
1137 if (directory)
1138 ptr = concat_fnames(directory, fname, TRUE);
1139 else
1140 ptr = vim_strsave(fname);
1141 }
1142 /* Use concatenated directory name and file name */
1143 fnum = buflist_add(ptr, 0);
1144 vim_free(ptr);
1145 return fnum;
1146 }
1147 return buflist_add(fname, 0);
1148#endif
1149 }
1150}
1151
1152/*
1153 * push dirbuf onto the directory stack and return pointer to actual dir or
1154 * NULL on error
1155 */
1156 static char_u *
1157qf_push_dir(dirbuf, stackptr)
1158 char_u *dirbuf;
1159 struct dir_stack_T **stackptr;
1160{
1161 struct dir_stack_T *ds_new;
1162 struct dir_stack_T *ds_ptr;
1163
1164 /* allocate new stack element and hook it in */
1165 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1166 if (ds_new == NULL)
1167 return NULL;
1168
1169 ds_new->next = *stackptr;
1170 *stackptr = ds_new;
1171
1172 /* store directory on the stack */
1173 if (vim_isAbsName(dirbuf)
1174 || (*stackptr)->next == NULL
1175 || (*stackptr && dir_stack != *stackptr))
1176 (*stackptr)->dirname = vim_strsave(dirbuf);
1177 else
1178 {
1179 /* Okay we don't have an absolute path.
1180 * dirbuf must be a subdir of one of the directories on the stack.
1181 * Let's search...
1182 */
1183 ds_new = (*stackptr)->next;
1184 (*stackptr)->dirname = NULL;
1185 while (ds_new)
1186 {
1187 vim_free((*stackptr)->dirname);
1188 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1189 TRUE);
1190 if (mch_isdir((*stackptr)->dirname) == TRUE)
1191 break;
1192
1193 ds_new = ds_new->next;
1194 }
1195
1196 /* clean up all dirs we already left */
1197 while ((*stackptr)->next != ds_new)
1198 {
1199 ds_ptr = (*stackptr)->next;
1200 (*stackptr)->next = (*stackptr)->next->next;
1201 vim_free(ds_ptr->dirname);
1202 vim_free(ds_ptr);
1203 }
1204
1205 /* Nothing found -> it must be on top level */
1206 if (ds_new == NULL)
1207 {
1208 vim_free((*stackptr)->dirname);
1209 (*stackptr)->dirname = vim_strsave(dirbuf);
1210 }
1211 }
1212
1213 if ((*stackptr)->dirname != NULL)
1214 return (*stackptr)->dirname;
1215 else
1216 {
1217 ds_ptr = *stackptr;
1218 *stackptr = (*stackptr)->next;
1219 vim_free(ds_ptr);
1220 return NULL;
1221 }
1222}
1223
1224
1225/*
1226 * pop dirbuf from the directory stack and return previous directory or NULL if
1227 * stack is empty
1228 */
1229 static char_u *
1230qf_pop_dir(stackptr)
1231 struct dir_stack_T **stackptr;
1232{
1233 struct dir_stack_T *ds_ptr;
1234
1235 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1236 * What to do if it isn't? */
1237
1238 /* pop top element and free it */
1239 if (*stackptr != NULL)
1240 {
1241 ds_ptr = *stackptr;
1242 *stackptr = (*stackptr)->next;
1243 vim_free(ds_ptr->dirname);
1244 vim_free(ds_ptr);
1245 }
1246
1247 /* return NEW top element as current dir or NULL if stack is empty*/
1248 return *stackptr ? (*stackptr)->dirname : NULL;
1249}
1250
1251/*
1252 * clean up directory stack
1253 */
1254 static void
1255qf_clean_dir_stack(stackptr)
1256 struct dir_stack_T **stackptr;
1257{
1258 struct dir_stack_T *ds_ptr;
1259
1260 while ((ds_ptr = *stackptr) != NULL)
1261 {
1262 *stackptr = (*stackptr)->next;
1263 vim_free(ds_ptr->dirname);
1264 vim_free(ds_ptr);
1265 }
1266}
1267
1268/*
1269 * Check in which directory of the directory stack the given file can be
1270 * found.
1271 * Returns a pointer to the directory name or NULL if not found
1272 * Cleans up intermediate directory entries.
1273 *
1274 * TODO: How to solve the following problem?
1275 * If we have the this directory tree:
1276 * ./
1277 * ./aa
1278 * ./aa/bb
1279 * ./bb
1280 * ./bb/x.c
1281 * and make says:
1282 * making all in aa
1283 * making all in bb
1284 * x.c:9: Error
1285 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1286 * qf_guess_filepath will return NULL.
1287 */
1288 static char_u *
1289qf_guess_filepath(filename)
1290 char_u *filename;
1291{
1292 struct dir_stack_T *ds_ptr;
1293 struct dir_stack_T *ds_tmp;
1294 char_u *fullname;
1295
1296 /* no dirs on the stack - there's nothing we can do */
1297 if (dir_stack == NULL)
1298 return NULL;
1299
1300 ds_ptr = dir_stack->next;
1301 fullname = NULL;
1302 while (ds_ptr)
1303 {
1304 vim_free(fullname);
1305 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1306
1307 /* If concat_fnames failed, just go on. The worst thing that can happen
1308 * is that we delete the entire stack.
1309 */
1310 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1311 break;
1312
1313 ds_ptr = ds_ptr->next;
1314 }
1315
1316 vim_free(fullname);
1317
1318 /* clean up all dirs we already left */
1319 while (dir_stack->next != ds_ptr)
1320 {
1321 ds_tmp = dir_stack->next;
1322 dir_stack->next = dir_stack->next->next;
1323 vim_free(ds_tmp->dirname);
1324 vim_free(ds_tmp);
1325 }
1326
1327 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1328
1329}
1330
1331/*
1332 * jump to a quickfix line
1333 * if dir == FORWARD go "errornr" valid entries forward
1334 * if dir == BACKWARD go "errornr" valid entries backward
1335 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1336 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1337 * else if "errornr" is zero, redisplay the same line
1338 * else go to entry "errornr"
1339 */
1340 void
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001341qf_jump(qi, dir, errornr, forceit)
1342 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 int dir;
1344 int errornr;
1345 int forceit;
1346{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001347 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001348 qfline_T *qf_ptr;
1349 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 int qf_index;
1351 int old_qf_fnum;
1352 int old_qf_index;
1353 int prev_index;
1354 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1355 char_u *err = e_no_more_items;
1356 linenr_T i;
1357 buf_T *old_curbuf;
1358 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 colnr_T screen_col;
1360 colnr_T char_col;
1361 char_u *line;
1362#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001363 char_u *old_swb = p_swb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 int opened_window = FALSE;
1365 win_T *win;
1366 win_T *altwin;
1367#endif
1368 int print_message = TRUE;
1369 int len;
1370#ifdef FEAT_FOLDING
1371 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1372#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001373 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001374 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001376 if (qi == NULL)
1377 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001378
1379 if (qi->qf_curlist >= qi->qf_listcount
1380 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 {
1382 EMSG(_(e_quickfix));
1383 return;
1384 }
1385
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001386 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001388 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 old_qf_index = qf_index;
1390 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1391 {
1392 while (errornr--)
1393 {
1394 old_qf_ptr = qf_ptr;
1395 prev_index = qf_index;
1396 old_qf_fnum = qf_ptr->qf_fnum;
1397 do
1398 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001399 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 || qf_ptr->qf_next == NULL)
1401 {
1402 qf_ptr = old_qf_ptr;
1403 qf_index = prev_index;
1404 if (err != NULL)
1405 {
1406 EMSG(_(err));
1407 goto theend;
1408 }
1409 errornr = 0;
1410 break;
1411 }
1412 ++qf_index;
1413 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001414 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1415 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1417 err = NULL;
1418 }
1419 }
1420 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1421 {
1422 while (errornr--)
1423 {
1424 old_qf_ptr = qf_ptr;
1425 prev_index = qf_index;
1426 old_qf_fnum = qf_ptr->qf_fnum;
1427 do
1428 {
1429 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1430 {
1431 qf_ptr = old_qf_ptr;
1432 qf_index = prev_index;
1433 if (err != NULL)
1434 {
1435 EMSG(_(err));
1436 goto theend;
1437 }
1438 errornr = 0;
1439 break;
1440 }
1441 --qf_index;
1442 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001443 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1444 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1446 err = NULL;
1447 }
1448 }
1449 else if (errornr != 0) /* go to specified number */
1450 {
1451 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1452 {
1453 --qf_index;
1454 qf_ptr = qf_ptr->qf_prev;
1455 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001456 while (errornr > qf_index && qf_index <
1457 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 && qf_ptr->qf_next != NULL)
1459 {
1460 ++qf_index;
1461 qf_ptr = qf_ptr->qf_next;
1462 }
1463 }
1464
1465#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001466 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1467 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 /* No need to print the error message if it's visible in the error
1469 * window */
1470 print_message = FALSE;
1471
1472 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001473 * For ":helpgrep" find a help window or open one.
1474 */
1475 if (qf_ptr->qf_type == 1 && !curwin->w_buffer->b_help)
1476 {
1477 win_T *wp;
1478 int n;
1479
1480 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1481 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1482 break;
1483 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1484 win_enter(wp, TRUE);
1485 else
1486 {
1487 /*
1488 * Split off help window; put it at far top if no position
1489 * specified, the current window is vertically split and narrow.
1490 */
1491 n = WSP_HELP;
1492# ifdef FEAT_VERTSPLIT
1493 if (cmdmod.split == 0 && curwin->w_width != Columns
1494 && curwin->w_width < 80)
1495 n |= WSP_TOP;
1496# endif
1497 if (win_split(0, n) == FAIL)
1498 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001499 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001500
1501 if (curwin->w_height < p_hh)
1502 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001503
1504 if (qi != &ql_info) /* not a quickfix list */
1505 {
1506 /* The new window should use the supplied location list */
1507 qf_free_all(curwin);
1508 curwin->w_llist = qi;
1509 qi->qf_refcount++;
1510 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001511 }
1512
1513 if (!p_im)
1514 restart_edit = 0; /* don't want insert mode in help file */
1515 }
1516
1517 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 * If currently in the quickfix window, find another window to show the
1519 * file in.
1520 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001521 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 {
1523 /*
1524 * If there is no file specified, we don't know where to go.
1525 * But do advance, otherwise ":cn" gets stuck.
1526 */
1527 if (qf_ptr->qf_fnum == 0)
1528 goto theend;
1529
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001530 /* Locate a window showing a normal buffer */
1531 usable_win = 0;
1532 FOR_ALL_WINDOWS(win)
1533 if (win->w_buffer->b_p_bt[0] == NUL)
1534 {
1535 usable_win = 1;
1536 break;
1537 }
1538
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 /*
1540 * If there is only one window, create a new one above the quickfix
1541 * window.
1542 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001543 if (firstwin == lastwin || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001545 ll_ref = curwin->w_llist_ref;
1546
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 if (win_split(0, WSP_ABOVE) == FAIL)
1548 goto failed; /* not enough room for window */
1549 opened_window = TRUE; /* close it when fail */
1550 p_swb = empty_option; /* don't split again */
1551# ifdef FEAT_SCROLLBIND
1552 curwin->w_p_scb = FALSE;
1553# endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001554 if (ll_ref != NULL)
1555 {
1556 /* The new window should use the location list from the
1557 * location list window */
1558 qf_free_all(curwin);
1559 curwin->w_llist = ll_ref;
1560 ll_ref->qf_refcount++;
1561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 }
1563 else
1564 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001565 if (curwin->w_llist_ref != NULL)
1566 {
1567 /* In a location window */
1568 ll_ref = curwin->w_llist_ref;
1569
1570 /* Find the window with the same location list */
1571 FOR_ALL_WINDOWS(win)
1572 if (win->w_llist == ll_ref)
1573 break;
1574 if (win == NULL)
1575 {
1576 /* Find the window showing the selected file */
1577 FOR_ALL_WINDOWS(win)
1578 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1579 break;
1580 if (win == NULL)
1581 {
1582 /* Find a previous usable window */
1583 win = curwin;
1584 do
1585 {
1586 if (win->w_buffer->b_p_bt[0] == NUL)
1587 break;
1588 if (win->w_prev == NULL)
1589 win = lastwin; /* wrap around the top */
1590 else
1591 win = win->w_prev; /* go to previous window */
1592 } while (win != curwin);
1593 }
1594 }
1595 win_goto(win);
1596
1597 /* If the location list for the window is not set, then set it
1598 * to the location list from the location window */
1599 if (win->w_llist == NULL)
1600 {
1601 win->w_llist = ll_ref;
1602 ll_ref->qf_refcount++;
1603 }
1604 }
1605 else
1606 {
1607
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 /*
1609 * Try to find a window that shows the right buffer.
1610 * Default to the window just above the quickfix buffer.
1611 */
1612 win = curwin;
1613 altwin = NULL;
1614 for (;;)
1615 {
1616 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1617 break;
1618 if (win->w_prev == NULL)
1619 win = lastwin; /* wrap around the top */
1620 else
1621 win = win->w_prev; /* go to previous window */
1622
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001623 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 {
1625 /* Didn't find it, go to the window before the quickfix
1626 * window. */
1627 if (altwin != NULL)
1628 win = altwin;
1629 else if (curwin->w_prev != NULL)
1630 win = curwin->w_prev;
1631 else
1632 win = curwin->w_next;
1633 break;
1634 }
1635
1636 /* Remember a usable window. */
1637 if (altwin == NULL && !win->w_p_pvw
1638 && win->w_buffer->b_p_bt[0] == NUL)
1639 altwin = win;
1640 }
1641
1642 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 }
1645 }
1646#endif
1647
1648 /*
1649 * If there is a file name,
1650 * read the wanted file if needed, and check autowrite etc.
1651 */
1652 old_curbuf = curbuf;
1653 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001654
1655 if (qf_ptr->qf_fnum != 0)
1656 {
1657 if (qf_ptr->qf_type == 1)
1658 {
1659 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1660 * set b_p_ro flag). */
1661 if (!can_abandon(curbuf, forceit))
1662 {
1663 EMSG(_(e_nowrtmsg));
1664 ok = FALSE;
1665 }
1666 else
1667 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
1668 ECMD_HIDE + ECMD_SET_HELP);
1669 }
1670 else
1671 ok = buflist_getfile(qf_ptr->qf_fnum,
1672 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
1673 }
1674
1675 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 {
1677 /* When not switched to another buffer, still need to set pc mark */
1678 if (curbuf == old_curbuf)
1679 setpcmark();
1680
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001681 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001683 /*
1684 * Go to line with error, unless qf_lnum is 0.
1685 */
1686 i = qf_ptr->qf_lnum;
1687 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001689 if (i > curbuf->b_ml.ml_line_count)
1690 i = curbuf->b_ml.ml_line_count;
1691 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001693 if (qf_ptr->qf_col > 0)
1694 {
1695 curwin->w_cursor.col = qf_ptr->qf_col - 1;
1696 if (qf_ptr->qf_viscol == TRUE)
1697 {
1698 /*
1699 * Check each character from the beginning of the error
1700 * line up to the error column. For each tab character
1701 * found, reduce the error column value by the length of
1702 * a tab character.
1703 */
1704 line = ml_get_curline();
1705 screen_col = 0;
1706 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1707 {
1708 if (*line == NUL)
1709 break;
1710 if (*line++ == '\t')
1711 {
1712 curwin->w_cursor.col -= 7 - (screen_col % 8);
1713 screen_col += 8 - (screen_col % 8);
1714 }
1715 else
1716 ++screen_col;
1717 }
1718 }
1719 check_cursor();
1720 }
1721 else
1722 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 }
1724 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001725 {
1726 pos_T save_cursor;
1727
1728 /* Move the cursor to the first line in the buffer */
1729 save_cursor = curwin->w_cursor;
1730 curwin->w_cursor.lnum = 0;
1731 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1, SEARCH_KEEP))
1732 curwin->w_cursor = save_cursor;
1733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734
1735#ifdef FEAT_FOLDING
1736 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1737 foldOpenCursor();
1738#endif
1739 if (print_message)
1740 {
1741 /* Update the screen before showing the message */
1742 update_topline_redraw();
1743 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001744 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1746 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1747 /* Add the message, skipping leading whitespace and newlines. */
1748 len = (int)STRLEN(IObuff);
1749 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1750
1751 /* Output the message. Overwrite to avoid scrolling when the 'O'
1752 * flag is present in 'shortmess'; But when not jumping, print the
1753 * whole message. */
1754 i = msg_scroll;
1755 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1756 msg_scroll = TRUE;
1757 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1758 msg_scroll = FALSE;
1759 msg_attr_keep(IObuff, 0, TRUE);
1760 msg_scroll = i;
1761 }
1762 }
1763 else
1764 {
1765#ifdef FEAT_WINDOWS
1766 if (opened_window)
1767 win_close(curwin, TRUE); /* Close opened window */
1768#endif
1769 if (qf_ptr->qf_fnum != 0)
1770 {
1771 /*
1772 * Couldn't open file, so put index back where it was. This could
1773 * happen if the file was readonly and we changed something.
1774 */
1775#ifdef FEAT_WINDOWS
1776failed:
1777#endif
1778 qf_ptr = old_qf_ptr;
1779 qf_index = old_qf_index;
1780 }
1781 }
1782theend:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001783 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
1784 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785#ifdef FEAT_WINDOWS
1786 if (p_swb != old_swb && opened_window)
1787 {
1788 /* Restore old 'switchbuf' value, but not when an autocommand or
1789 * modeline has changed the value. */
1790 if (p_swb == empty_option)
1791 p_swb = old_swb;
1792 else
1793 free_string_option(old_swb);
1794 }
1795#endif
1796}
1797
1798/*
1799 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001800 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 */
1802 void
1803qf_list(eap)
1804 exarg_T *eap;
1805{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001806 buf_T *buf;
1807 char_u *fname;
1808 qfline_T *qfp;
1809 int i;
1810 int idx1 = 1;
1811 int idx2 = -1;
1812 int need_return = TRUE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001813 char_u *arg = eap->arg;
1814 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001816 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001818 if (eap->cmdidx == CMD_llist)
1819 {
1820 qi = GET_LOC_LIST(curwin);
1821 if (qi == NULL)
1822 {
1823 EMSG(_(e_loclist));
1824 return;
1825 }
1826 }
1827
1828 if (qi->qf_curlist >= qi->qf_listcount
1829 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 {
1831 EMSG(_(e_quickfix));
1832 return;
1833 }
1834 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
1835 {
1836 EMSG(_(e_trailing));
1837 return;
1838 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001839 i = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 if (idx1 < 0)
1841 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
1842 if (idx2 < 0)
1843 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
1844
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001845 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001847 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
1848 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 {
1850 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
1851 {
1852 if (need_return)
1853 {
1854 msg_putchar('\n');
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001855 if (got_int)
1856 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 need_return = FALSE;
1858 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001859
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001860 fname = NULL;
1861 if (qfp->qf_fnum != 0
1862 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
1863 {
1864 fname = buf->b_fname;
1865 if (qfp->qf_type == 1) /* :helpgrep */
1866 fname = gettail(fname);
1867 }
1868 if (fname == NULL)
1869 sprintf((char *)IObuff, "%2d", i);
1870 else
1871 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
1872 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001873 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001874 ? hl_attr(HLF_L) : hl_attr(HLF_D));
1875 if (qfp->qf_lnum == 0)
1876 IObuff[0] = NUL;
1877 else if (qfp->qf_col == 0)
1878 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
1879 else
1880 sprintf((char *)IObuff, ":%ld col %d",
1881 qfp->qf_lnum, qfp->qf_col);
1882 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
1883 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
1884 msg_puts_attr(IObuff, hl_attr(HLF_N));
1885 if (qfp->qf_pattern != NULL)
1886 {
1887 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
1888 STRCAT(IObuff, ":");
1889 msg_puts(IObuff);
1890 }
1891 msg_puts((char_u *)" ");
1892
1893 /* Remove newlines and leading whitespace from the text. For an
1894 * unrecognized line keep the indent, the compiler may mark a word
1895 * with ^^^^. */
1896 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 ? skipwhite(qfp->qf_text) : qfp->qf_text,
1898 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001899 msg_prt_line(IObuff, FALSE);
1900 out_flush(); /* show one line at a time */
1901 need_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001903
1904 qfp = qfp->qf_next;
1905 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 ui_breakcheck();
1907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908}
1909
1910/*
1911 * Remove newlines and leading whitespace from an error message.
1912 * Put the result in "buf[bufsize]".
1913 */
1914 static void
1915qf_fmt_text(text, buf, bufsize)
1916 char_u *text;
1917 char_u *buf;
1918 int bufsize;
1919{
1920 int i;
1921 char_u *p = text;
1922
1923 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
1924 {
1925 if (*p == '\n')
1926 {
1927 buf[i] = ' ';
1928 while (*++p != NUL)
1929 if (!vim_iswhite(*p) && *p != '\n')
1930 break;
1931 }
1932 else
1933 buf[i] = *p++;
1934 }
1935 buf[i] = NUL;
1936}
1937
1938/*
1939 * ":colder [count]": Up in the quickfix stack.
1940 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001941 * ":lolder [count]": Up in the location list stack.
1942 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 */
1944 void
1945qf_age(eap)
1946 exarg_T *eap;
1947{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001948 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 int count;
1950
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001951 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
1952 {
1953 qi = GET_LOC_LIST(curwin);
1954 if (qi == NULL)
1955 {
1956 EMSG(_(e_loclist));
1957 return;
1958 }
1959 }
1960
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 if (eap->addr_count != 0)
1962 count = eap->line2;
1963 else
1964 count = 1;
1965 while (count--)
1966 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001967 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001969 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 {
1971 EMSG(_("E380: At bottom of quickfix stack"));
1972 return;
1973 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001974 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 }
1976 else
1977 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001978 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 {
1980 EMSG(_("E381: At top of quickfix stack"));
1981 return;
1982 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001983 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 }
1985 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001986 qf_msg(qi);
1987
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988}
1989
1990 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001991qf_msg(qi)
1992 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993{
1994 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001995 qi->qf_curlist + 1, qi->qf_listcount,
1996 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001998 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999#endif
2000}
2001
2002/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002003 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 */
2005 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002006qf_free(qi, idx)
2007 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 int idx;
2009{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002010 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002012 while (qi->qf_lists[idx].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002014 qfp = qi->qf_lists[idx].qf_start->qf_next;
2015 vim_free(qi->qf_lists[idx].qf_start->qf_text);
2016 vim_free(qi->qf_lists[idx].qf_start->qf_pattern);
2017 vim_free(qi->qf_lists[idx].qf_start);
2018 qi->qf_lists[idx].qf_start = qfp;
2019 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 }
2021}
2022
2023/*
2024 * qf_mark_adjust: adjust marks
2025 */
2026 void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002027qf_mark_adjust(wp, line1, line2, amount, amount_after)
2028 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 linenr_T line1;
2030 linenr_T line2;
2031 long amount;
2032 long amount_after;
2033{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002034 int i;
2035 qfline_T *qfp;
2036 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002037 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002039 if (wp != NULL)
2040 {
2041 if (wp->w_llist == NULL)
2042 return;
2043 qi = wp->w_llist;
2044 }
2045
2046 for (idx = 0; idx < qi->qf_listcount; ++idx)
2047 if (qi->qf_lists[idx].qf_count)
2048 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
2049 i < qi->qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 if (qfp->qf_fnum == curbuf->b_fnum)
2051 {
2052 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2053 {
2054 if (amount == MAXLNUM)
2055 qfp->qf_cleared = TRUE;
2056 else
2057 qfp->qf_lnum += amount;
2058 }
2059 else if (amount_after && qfp->qf_lnum > line2)
2060 qfp->qf_lnum += amount_after;
2061 }
2062}
2063
2064/*
2065 * Make a nice message out of the error character and the error number:
2066 * char number message
2067 * e or E 0 " error"
2068 * w or W 0 " warning"
2069 * i or I 0 " info"
2070 * 0 0 ""
2071 * other 0 " c"
2072 * e or E n " error n"
2073 * w or W n " warning n"
2074 * i or I n " info n"
2075 * 0 n " error n"
2076 * other n " c n"
2077 * 1 x "" :helpgrep
2078 */
2079 static char_u *
2080qf_types(c, nr)
2081 int c, nr;
2082{
2083 static char_u buf[20];
2084 static char_u cc[3];
2085 char_u *p;
2086
2087 if (c == 'W' || c == 'w')
2088 p = (char_u *)" warning";
2089 else if (c == 'I' || c == 'i')
2090 p = (char_u *)" info";
2091 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2092 p = (char_u *)" error";
2093 else if (c == 0 || c == 1)
2094 p = (char_u *)"";
2095 else
2096 {
2097 cc[0] = ' ';
2098 cc[1] = c;
2099 cc[2] = NUL;
2100 p = cc;
2101 }
2102
2103 if (nr <= 0)
2104 return p;
2105
2106 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2107 return buf;
2108}
2109
2110#if defined(FEAT_WINDOWS) || defined(PROTO)
2111/*
2112 * ":cwindow": open the quickfix window if we have errors to display,
2113 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002114 * ":lwindow": open the location list window if we have locations to display,
2115 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 */
2117 void
2118ex_cwindow(eap)
2119 exarg_T *eap;
2120{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002121 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 win_T *win;
2123
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002124 if (eap->cmdidx == CMD_lwindow)
2125 {
2126 qi = GET_LOC_LIST(curwin);
2127 if (qi == NULL)
2128 return;
2129 }
2130
2131 /* Look for an existing quickfix window. */
2132 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133
2134 /*
2135 * If a quickfix window is open but we have no errors to display,
2136 * close the window. If a quickfix window is not open, then open
2137 * it if we have errors; otherwise, leave it closed.
2138 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002139 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
2140 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 {
2142 if (win != NULL)
2143 ex_cclose(eap);
2144 }
2145 else if (win == NULL)
2146 ex_copen(eap);
2147}
2148
2149/*
2150 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002151 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 */
2153/*ARGSUSED*/
2154 void
2155ex_cclose(eap)
2156 exarg_T *eap;
2157{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002158 win_T *win = NULL;
2159 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002161 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2162 {
2163 qi = GET_LOC_LIST(curwin);
2164 if (qi == NULL)
2165 return;
2166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002168 /* Find existing quickfix window and close it. */
2169 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 if (win != NULL)
2171 win_close(win, FALSE);
2172}
2173
2174/*
2175 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002176 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 */
2178 void
2179ex_copen(eap)
2180 exarg_T *eap;
2181{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002182 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 win_T *win;
2185
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002186 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2187 {
2188 qi = GET_LOC_LIST(curwin);
2189 if (qi == NULL)
2190 {
2191 EMSG(_(e_loclist));
2192 return;
2193 }
2194 }
2195
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 if (eap->addr_count != 0)
2197 height = eap->line2;
2198 else
2199 height = QF_WINHEIGHT;
2200
2201#ifdef FEAT_VISUAL
2202 reset_VIsual_and_resel(); /* stop Visual mode */
2203#endif
2204#ifdef FEAT_GUI
2205 need_mouse_correct = TRUE;
2206#endif
2207
2208 /*
2209 * Find existing quickfix window, or open a new one.
2210 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002211 win = qf_find_win(qi);
2212
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 if (win != NULL)
2214 win_goto(win);
2215 else
2216 {
2217 /* The current window becomes the previous window afterwards. */
2218 win = curwin;
2219
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002220 if (eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2221 /* Create the new window at the very bottom. */
2222 win_goto(lastwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 if (win_split(height, WSP_BELOW) == FAIL)
2224 return; /* not enough room for window */
2225#ifdef FEAT_SCROLLBIND
2226 curwin->w_p_scb = FALSE;
2227#endif
2228
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002229 /* Remove the location list for the quickfix window */
2230 qf_free_all(curwin);
2231
2232 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002234 /*
2235 * For the location list window, create a reference to the
2236 * location list from the window 'win'.
2237 */
2238 curwin->w_llist_ref = win->w_llist;
2239 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002241
2242 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
2243 /* switch off 'swapfile' */
2244 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2245 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
2246 OPT_LOCAL);
2247 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
2248 set_option_value((char_u *)"diff", 0L, (char_u *)"", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002250#ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 /* Only set the height when there is no window to the side. */
2252 if (curwin->w_width == Columns)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 win_setheight(height);
2255 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2256 if (win_valid(win))
2257 prevwin = win;
2258 }
2259
2260 /*
2261 * Fill the buffer with the quickfix list.
2262 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002263 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002265 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 curwin->w_cursor.col = 0;
2267 check_cursor();
2268 update_topline(); /* scroll to show the line */
2269}
2270
2271/*
2272 * Return the number of the current entry (line number in the quickfix
2273 * window).
2274 */
2275 linenr_T
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002276qf_current_entry(wp)
2277 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002279 qf_info_T *qi = &ql_info;
2280
2281 if (IS_LL_WINDOW(wp))
2282 /* In the location list window, use the referenced location list */
2283 qi = wp->w_llist_ref;
2284
2285 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286}
2287
2288/*
2289 * Update the cursor position in the quickfix window to the current error.
2290 * Return TRUE if there is a quickfix window.
2291 */
2292 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002293qf_win_pos_update(qi, old_qf_index)
2294 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 int old_qf_index; /* previous qf_index or zero */
2296{
2297 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002298 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299
2300 /*
2301 * Put the cursor on the current error in the quickfix window, so that
2302 * it's viewable.
2303 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002304 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 if (win != NULL
2306 && qf_index <= win->w_buffer->b_ml.ml_line_count
2307 && old_qf_index != qf_index)
2308 {
2309 win_T *old_curwin = curwin;
2310
2311 curwin = win;
2312 curbuf = win->w_buffer;
2313 if (qf_index > old_qf_index)
2314 {
2315 curwin->w_redraw_top = old_qf_index;
2316 curwin->w_redraw_bot = qf_index;
2317 }
2318 else
2319 {
2320 curwin->w_redraw_top = qf_index;
2321 curwin->w_redraw_bot = old_qf_index;
2322 }
2323 curwin->w_cursor.lnum = qf_index;
2324 curwin->w_cursor.col = 0;
2325 update_topline(); /* scroll to show the line */
2326 redraw_later(VALID);
2327 curwin->w_redr_status = TRUE; /* update ruler */
2328 curwin = old_curwin;
2329 curbuf = curwin->w_buffer;
2330 }
2331 return win != NULL;
2332}
2333
2334/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002335 * Find a window displaying the quickfix/location list 'qi'
2336 */
2337 static win_T *
2338qf_find_win(qi)
2339 qf_info_T *qi;
2340{
2341 win_T *win;
2342
2343 /*
2344 * When searching for the quickfix buffer, find a window
2345 * with w_llist_ref set to NULL.
2346 * When searching for the location list buffer, find a window
2347 * with w_llist_ref pointing to the supplied location list.
2348 */
2349 FOR_ALL_WINDOWS(win)
2350 if (bt_quickfix(win->w_buffer))
2351 if ((qi == &ql_info && win->w_llist_ref == NULL)
2352 || (qi != &ql_info && win->w_llist_ref == qi))
2353 break;
2354
2355 return win;
2356}
2357
2358/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 * Find quickfix buffer.
2360 */
2361 static buf_T *
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002362qf_find_buf(qi)
2363 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002365 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002367 win = qf_find_win(qi);
2368
2369 return (win != NULL) ? win->w_buffer: NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370}
2371
2372/*
2373 * Find the quickfix buffer. If it exists, update the contents.
2374 */
2375 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002376qf_update_buffer(qi)
2377 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378{
2379 buf_T *buf;
2380#ifdef FEAT_AUTOCMD
2381 aco_save_T aco;
2382#else
2383 buf_T *save_curbuf;
2384#endif
2385
2386 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002387 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 if (buf != NULL)
2389 {
2390#ifdef FEAT_AUTOCMD
2391 /* set curwin/curbuf to buf and save a few things */
2392 aucmd_prepbuf(&aco, buf);
2393#else
2394 save_curbuf = curbuf;
2395 curbuf = buf;
2396#endif
2397
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002398 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399
2400#ifdef FEAT_AUTOCMD
2401 /* restore curwin/curbuf and a few other things */
2402 aucmd_restbuf(&aco);
2403#else
2404 curbuf = save_curbuf;
2405#endif
2406
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002407 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 }
2409}
2410
2411/*
2412 * Fill current buffer with quickfix errors, replacing any previous contents.
2413 * curbuf must be the quickfix buffer!
2414 */
2415 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002416qf_fill_buffer(qi)
2417 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002419 linenr_T lnum;
2420 qfline_T *qfp;
2421 buf_T *errbuf;
2422 int len;
2423 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424
2425 /* delete all existing lines */
2426 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2427 (void)ml_delete((linenr_T)1, FALSE);
2428
2429 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002430 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 {
2432 /* Add one line for each error */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002433 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2434 for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 {
2436 if (qfp->qf_fnum != 0
2437 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2438 && errbuf->b_fname != NULL)
2439 {
2440 if (qfp->qf_type == 1) /* :helpgrep */
2441 STRCPY(IObuff, gettail(errbuf->b_fname));
2442 else
2443 STRCPY(IObuff, errbuf->b_fname);
2444 len = (int)STRLEN(IObuff);
2445 }
2446 else
2447 len = 0;
2448 IObuff[len++] = '|';
2449
2450 if (qfp->qf_lnum > 0)
2451 {
2452 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2453 len += (int)STRLEN(IObuff + len);
2454
2455 if (qfp->qf_col > 0)
2456 {
2457 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2458 len += (int)STRLEN(IObuff + len);
2459 }
2460
2461 sprintf((char *)IObuff + len, "%s",
2462 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2463 len += (int)STRLEN(IObuff + len);
2464 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002465 else if (qfp->qf_pattern != NULL)
2466 {
2467 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2468 len += (int)STRLEN(IObuff + len);
2469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 IObuff[len++] = '|';
2471 IObuff[len++] = ' ';
2472
2473 /* Remove newlines and leading whitespace from the text.
2474 * For an unrecognized line keep the indent, the compiler may
2475 * mark a word with ^^^^. */
2476 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2477 IObuff + len, IOSIZE - len);
2478
2479 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2480 == FAIL)
2481 break;
2482 qfp = qfp->qf_next;
2483 }
2484 /* Delete the empty line which is now at the end */
2485 (void)ml_delete(lnum + 1, FALSE);
2486 }
2487
2488 /* correct cursor position */
2489 check_lnums(TRUE);
2490
2491 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2492 * resembles reading a file into a buffer, it's more logical when using
2493 * autocommands. */
2494 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2495 curbuf->b_p_ma = FALSE;
2496
2497#ifdef FEAT_AUTOCMD
2498 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2499 FALSE, curbuf);
2500 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2501 FALSE, curbuf);
2502#endif
2503
2504 /* make sure it will be redrawn */
2505 redraw_curbuf_later(NOT_VALID);
2506
2507 /* Restore KeyTyped, setting 'filetype' may reset it. */
2508 KeyTyped = old_KeyTyped;
2509}
2510
2511#endif /* FEAT_WINDOWS */
2512
2513/*
2514 * Return TRUE if "buf" is the quickfix buffer.
2515 */
2516 int
2517bt_quickfix(buf)
2518 buf_T *buf;
2519{
2520 return (buf->b_p_bt[0] == 'q');
2521}
2522
2523/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002524 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2525 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 */
2527 int
2528bt_nofile(buf)
2529 buf_T *buf;
2530{
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002531 return (buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2532 || buf->b_p_bt[0] == 'a';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533}
2534
2535/*
2536 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2537 */
2538 int
2539bt_dontwrite(buf)
2540 buf_T *buf;
2541{
2542 return (buf->b_p_bt[0] == 'n');
2543}
2544
2545 int
2546bt_dontwrite_msg(buf)
2547 buf_T *buf;
2548{
2549 if (bt_dontwrite(buf))
2550 {
2551 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2552 return TRUE;
2553 }
2554 return FALSE;
2555}
2556
2557/*
2558 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2559 * and 'bufhidden'.
2560 */
2561 int
2562buf_hide(buf)
2563 buf_T *buf;
2564{
2565 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2566 switch (buf->b_p_bh[0])
2567 {
2568 case 'u': /* "unload" */
2569 case 'w': /* "wipe" */
2570 case 'd': return FALSE; /* "delete" */
2571 case 'h': return TRUE; /* "hide" */
2572 }
2573 return (p_hid || cmdmod.hide);
2574}
2575
2576/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002577 * Return TRUE when using ":vimgrep" for ":grep".
2578 */
2579 int
Bram Moolenaar81695252004-12-29 20:58:21 +00002580grep_internal(cmdidx)
2581 cmdidx_T cmdidx;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002582{
Bram Moolenaar754b5602006-02-09 23:53:20 +00002583 return ((cmdidx == CMD_grep
2584 || cmdidx == CMD_lgrep
2585 || cmdidx == CMD_grepadd
2586 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002587 && STRCMP("internal",
2588 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2589}
2590
2591/*
Bram Moolenaara6557602006-02-04 22:43:20 +00002592 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 */
2594 void
2595ex_make(eap)
2596 exarg_T *eap;
2597{
Bram Moolenaar7c626922005-02-07 22:01:03 +00002598 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 char_u *cmd;
2600 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00002601 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002602 qf_info_T *qi = &ql_info;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002603#ifdef FEAT_AUTOCMD
2604 char_u *au_name = NULL;
2605
2606 switch (eap->cmdidx)
2607 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00002608 case CMD_make: au_name = (char_u *)"make"; break;
2609 case CMD_lmake: au_name = (char_u *)"lmake"; break;
2610 case CMD_grep: au_name = (char_u *)"grep"; break;
2611 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
2612 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
2613 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002614 default: break;
2615 }
2616 if (au_name != NULL)
2617 {
2618 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2619 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00002620# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00002621 if (did_throw || force_abort)
2622 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00002623# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002624 }
2625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626
Bram Moolenaar86b68352004-12-27 21:59:20 +00002627 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
Bram Moolenaar81695252004-12-29 20:58:21 +00002628 if (grep_internal(eap->cmdidx))
Bram Moolenaar86b68352004-12-27 21:59:20 +00002629 {
2630 ex_vimgrep(eap);
2631 return;
2632 }
2633
Bram Moolenaara6557602006-02-04 22:43:20 +00002634 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
2635 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00002636 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00002637
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002639 fname = get_mef_name();
2640 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002642 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643
2644 /*
2645 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2646 */
2647 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2648 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002649 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 cmd = alloc(len);
2651 if (cmd == NULL)
2652 return;
2653 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2654 (char *)p_shq);
2655 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002656 append_redir(cmd, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 /*
2658 * Output a newline if there's something else than the :make command that
2659 * was typed (in which case the cursor is in column 0).
2660 */
2661 if (msg_col == 0)
2662 msg_didout = FALSE;
2663 msg_start();
2664 MSG_PUTS(":!");
2665 msg_outtrans(cmd); /* show what we are doing */
2666
2667 /* let the shell know if we are redirecting output or not */
2668 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2669
2670#ifdef AMIGA
2671 out_flush();
2672 /* read window status report and redraw before message */
2673 (void)char_avail();
2674#endif
2675
Bram Moolenaara6557602006-02-04 22:43:20 +00002676 if (qf_init(wp, fname, (eap->cmdidx != CMD_make
2677 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
2678 (eap->cmdidx != CMD_grepadd
2679 && eap->cmdidx != CMD_lgrepadd)) > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 && !eap->forceit)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002681 {
2682 if (wp != NULL)
2683 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002684 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686
Bram Moolenaar7c626922005-02-07 22:01:03 +00002687 mch_remove(fname);
2688 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 vim_free(cmd);
Bram Moolenaar7c626922005-02-07 22:01:03 +00002690
2691#ifdef FEAT_AUTOCMD
2692 if (au_name != NULL)
2693 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2694 curbuf->b_fname, TRUE, curbuf);
2695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696}
2697
2698/*
2699 * Return the name for the errorfile, in allocated memory.
2700 * Find a new unique name when 'makeef' contains "##".
2701 * Returns NULL for error.
2702 */
2703 static char_u *
2704get_mef_name()
2705{
2706 char_u *p;
2707 char_u *name;
2708 static int start = -1;
2709 static int off = 0;
2710#ifdef HAVE_LSTAT
2711 struct stat sb;
2712#endif
2713
2714 if (*p_mef == NUL)
2715 {
2716 name = vim_tempname('e');
2717 if (name == NULL)
2718 EMSG(_(e_notmp));
2719 return name;
2720 }
2721
2722 for (p = p_mef; *p; ++p)
2723 if (p[0] == '#' && p[1] == '#')
2724 break;
2725
2726 if (*p == NUL)
2727 return vim_strsave(p_mef);
2728
2729 /* Keep trying until the name doesn't exist yet. */
2730 for (;;)
2731 {
2732 if (start == -1)
2733 start = mch_get_pid();
2734 else
2735 off += 19;
2736
2737 name = alloc((unsigned)STRLEN(p_mef) + 30);
2738 if (name == NULL)
2739 break;
2740 STRCPY(name, p_mef);
2741 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2742 STRCAT(name, p + 2);
2743 if (mch_getperm(name) < 0
2744#ifdef HAVE_LSTAT
2745 /* Don't accept a symbolic link, its a security risk. */
2746 && mch_lstat((char *)name, &sb) < 0
2747#endif
2748 )
2749 break;
2750 vim_free(name);
2751 }
2752 return name;
2753}
2754
2755/*
2756 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002757 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 */
2759 void
2760ex_cc(eap)
2761 exarg_T *eap;
2762{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002763 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002764
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002765 if (eap->cmdidx == CMD_ll
2766 || eap->cmdidx == CMD_lrewind
2767 || eap->cmdidx == CMD_lfirst
2768 || eap->cmdidx == CMD_llast)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002769 {
2770 qi = GET_LOC_LIST(curwin);
2771 if (qi == NULL)
2772 {
2773 EMSG(_(e_loclist));
2774 return;
2775 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002776 }
2777
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002778 qf_jump(qi, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 eap->addr_count > 0
2780 ? (int)eap->line2
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002781 : (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 ? 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002783 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
2784 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 ? 1
2786 : 32767,
2787 eap->forceit);
2788}
2789
2790/*
2791 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002792 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 */
2794 void
2795ex_cnext(eap)
2796 exarg_T *eap;
2797{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002798 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002799
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002800 if (eap->cmdidx == CMD_lnext
2801 || eap->cmdidx == CMD_lNext
2802 || eap->cmdidx == CMD_lprevious
2803 || eap->cmdidx == CMD_lnfile
2804 || eap->cmdidx == CMD_lNfile
2805 || eap->cmdidx == CMD_lpfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002806 {
2807 qi = GET_LOC_LIST(curwin);
2808 if (qi == NULL)
2809 {
2810 EMSG(_(e_loclist));
2811 return;
2812 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002813 }
2814
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002815 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 ? FORWARD
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002817 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002819 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
2820 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 ? BACKWARD_FILE
2822 : BACKWARD,
2823 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
2824}
2825
2826/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002827 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002828 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 */
2830 void
2831ex_cfile(eap)
2832 exarg_T *eap;
2833{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002834 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002835 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002836
2837 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
2838 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002839 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002840
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 if (*eap->arg != NUL)
2842 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002843
2844 /*
2845 * This function is used by the :cfile, :cgetfile and :caddfile
2846 * commands.
2847 * :cfile always creates a new quickfix list and jumps to the
2848 * first error.
2849 * :cgetfile creates a new quickfix list but doesn't jump to the
2850 * first error.
2851 * :caddfile adds to an existing quickfix list. If there is no
2852 * quickfix list then a new list is created.
2853 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002854 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
2855 && eap->cmdidx != CMD_laddfile)) > 0
2856 && (eap->cmdidx == CMD_cfile
2857 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002858 {
2859 if (wp != NULL)
2860 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002861 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863}
2864
2865/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002866 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00002867 * ":vimgrepadd {pattern} file(s)"
2868 * ":lvimgrep {pattern} file(s)"
2869 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00002870 */
2871 void
2872ex_vimgrep(eap)
2873 exarg_T *eap;
2874{
Bram Moolenaar81695252004-12-29 20:58:21 +00002875 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002876 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002877 char_u **fnames;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002878 char_u *s;
2879 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002880 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00002881 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002882 qfline_T *prevp = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002883 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00002884 buf_T *buf;
2885 int duplicate_name = FALSE;
2886 int using_dummy;
2887 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002888 buf_T *first_match_buf = NULL;
2889 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002890 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002891#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2892 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002893#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002894#ifndef FEAT_AUTOCMD
2895 buf_T *save_curbuf;
2896#else
2897 aco_save_T aco;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002898 char_u *au_name = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002899 int flags = 0;
2900 colnr_T col;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002901
2902 switch (eap->cmdidx)
2903 {
2904 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00002905 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002906 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00002907 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002908 default: break;
2909 }
2910 if (au_name != NULL)
2911 {
2912 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2913 curbuf->b_fname, TRUE, curbuf);
2914 if (did_throw || force_abort)
2915 return;
2916 }
2917#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00002918
Bram Moolenaar754b5602006-02-09 23:53:20 +00002919 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002920 || eap->cmdidx == CMD_lvimgrep
2921 || eap->cmdidx == CMD_lgrepadd
2922 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00002923 {
2924 qi = ll_get_or_alloc_list(curwin);
2925 if (qi == NULL)
2926 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00002927 }
2928
Bram Moolenaar81695252004-12-29 20:58:21 +00002929 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00002930 regmatch.regprog = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002931 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00002932 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002933 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00002934 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00002935 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00002936 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002937 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002938 if (regmatch.regprog == NULL)
2939 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002940 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00002941 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002942
2943 p = skipwhite(p);
2944 if (*p == NUL)
2945 {
2946 EMSG(_("E683: File name missing or invalid pattern"));
2947 goto theend;
2948 }
2949
Bram Moolenaar754b5602006-02-09 23:53:20 +00002950 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00002951 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002952 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002953 /* make place for a new list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002954 qf_new_list(qi);
2955 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002956 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002957 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002958 prevp->qf_next != prevp; prevp = prevp->qf_next)
2959 ;
2960
2961 /* parse the list of arguments */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002962 if (get_arglist_exp(p, &fcount, &fnames) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002963 goto theend;
2964 if (fcount == 0)
2965 {
2966 EMSG(_(e_nomatch));
2967 goto theend;
2968 }
2969
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002970 seconds = (time_t)0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002971 for (fi = 0; fi < fcount && !got_int; ++fi)
2972 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002973 if (time(NULL) > seconds)
2974 {
2975 /* Display the file name every second or so. */
2976 seconds = time(NULL);
2977 msg_start();
Bram Moolenaara5373fa2005-09-09 19:47:12 +00002978 p = msg_strtrunc(fnames[fi], TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002979 if (p == NULL)
2980 msg_outtrans(fnames[fi]);
2981 else
2982 {
2983 msg_outtrans(p);
2984 vim_free(p);
2985 }
2986 msg_clr_eos();
2987 msg_didout = FALSE; /* overwrite this message */
2988 msg_nowait = TRUE; /* don't wait for this message */
2989 msg_col = 0;
2990 out_flush();
2991 }
2992
Bram Moolenaar81695252004-12-29 20:58:21 +00002993 buf = buflist_findname_exp(fnames[fi]);
2994 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
2995 {
2996 /* Remember that a buffer with this name already exists. */
2997 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002998 using_dummy = TRUE;
2999
3000#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3001 /* Don't do Filetype autocommands to avoid loading syntax and
3002 * indent scripts, a great speed improvement. */
3003 save_ei = au_event_disable(",Filetype");
3004#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003005 /* Don't use modelines here, it's useless. */
3006 save_mls = p_mls;
3007 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00003008
3009 /* Load file into a buffer, so that 'fileencoding' is detected,
3010 * autocommands applied, etc. */
3011 buf = load_dummy_buffer(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003012
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003013 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003014#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3015 au_event_restore(save_ei);
3016#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003017 }
3018 else
3019 /* Use existing, loaded buffer. */
3020 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003021
Bram Moolenaar81695252004-12-29 20:58:21 +00003022 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003023 {
3024 if (!got_int)
3025 smsg((char_u *)_("Cannot open file \"%s\""), fnames[fi]);
3026 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003027 else
3028 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003029 found_match = FALSE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003030 /* Try for a match in all lines of the buffer. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003031 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003032 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003033 /* For ":1vimgrep" look for multiple matches. */
3034 col = 0;
3035 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
3036 col) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003037 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003038 if (qf_add_entry(qi, &prevp,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003039 NULL, /* dir */
3040 fnames[fi],
Bram Moolenaar81695252004-12-29 20:58:21 +00003041 ml_get_buf(buf,
3042 regmatch.startpos[0].lnum + lnum, FALSE),
3043 regmatch.startpos[0].lnum + lnum,
3044 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003045 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003046 NULL, /* search pattern */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003047 0, /* nr */
3048 0, /* type */
3049 TRUE /* valid */
3050 ) == FAIL)
3051 {
3052 got_int = TRUE;
3053 break;
3054 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003055 else
3056 found_match = TRUE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003057 if ((flags & VGR_GLOBAL) == 0
3058 || regmatch.endpos[0].lnum > 0)
3059 break;
3060 col = regmatch.endpos[0].col
3061 + (col == regmatch.endpos[0].col);
3062 if (col > STRLEN(ml_get_buf(buf, lnum, FALSE)))
3063 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003064 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003065 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003066 if (got_int)
3067 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003068 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003069
3070 if (using_dummy)
3071 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003072 if (found_match && first_match_buf == NULL)
3073 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003074 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003075 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003076 /* Never keep a dummy buffer if there is another buffer
3077 * with the same name. */
3078 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003079 buf = NULL;
3080 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003081 else if (!buf_hide(buf))
3082 {
3083 /* When not hiding the buffer and no match was found we
3084 * don't need to remember the buffer, wipe it out. If
Bram Moolenaar05159a02005-02-26 23:04:13 +00003085 * there was a match and it wasn't the first one or we
3086 * won't jump there: only unload the buffer. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003087 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003088 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003089 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003090 buf = NULL;
3091 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003092 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003093 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003094 unload_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003095 buf = NULL;
3096 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003097 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003098
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003099 if (buf != NULL)
3100 {
3101 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003102 * need to be done now, in that buffer. And the modelines
3103 * need to be done (again). */
3104#if defined(FEAT_AUTOCMD)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003105 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003106#else
3107 save_curbuf = curbuf;
3108 curbuf = buf;
3109 curwin->w_buffer = curbuf;
3110#endif
3111#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003112 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3113 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003114#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003115 do_modelines(FALSE);
3116#if defined(FEAT_AUTOCMD)
3117 aucmd_restbuf(&aco);
3118#else
3119 curbuf = save_curbuf;
3120 curwin->w_buffer = curbuf;
3121#endif
3122 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003123 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003124 }
3125 }
3126
3127 FreeWild(fcount, fnames);
3128
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003129 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3130 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3131 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003132
3133#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003134 qf_update_buffer(qi);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003135#endif
3136
3137 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003138 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003139 {
3140 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003141 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003142 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003143 else
3144 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003145
Bram Moolenaar7c626922005-02-07 22:01:03 +00003146#ifdef FEAT_AUTOCMD
3147 if (au_name != NULL)
3148 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3149 curbuf->b_fname, TRUE, curbuf);
3150#endif
3151
Bram Moolenaar86b68352004-12-27 21:59:20 +00003152theend:
3153 vim_free(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003154}
3155
3156/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00003157 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00003158 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003159 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3160 * If "s" is not NULL terminate the pattern with a NUL.
3161 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00003162 */
3163 char_u *
Bram Moolenaar05159a02005-02-26 23:04:13 +00003164skip_vimgrep_pat(p, s, flags)
3165 char_u *p;
3166 char_u **s;
3167 int *flags;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003168{
3169 int c;
3170
3171 if (vim_isIDc(*p))
3172 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003173 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003174 if (s != NULL)
3175 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003176 p = skiptowhite(p);
3177 if (s != NULL && *p != NUL)
3178 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003179 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003180 else
3181 {
3182 /* ":vimgrep /pattern/[g][j] fname" */
3183 if (s != NULL)
3184 *s = p + 1;
3185 c = *p;
3186 p = skip_regexp(p + 1, c, TRUE, NULL);
3187 if (*p != c)
3188 return NULL;
3189
3190 /* Truncate the pattern. */
3191 if (s != NULL)
3192 *p = NUL;
3193 ++p;
3194
3195 /* Find the flags */
3196 while (*p == 'g' || *p == 'j')
3197 {
3198 if (flags != NULL)
3199 {
3200 if (*p == 'g')
3201 *flags |= VGR_GLOBAL;
3202 else
3203 *flags |= VGR_NOJUMP;
3204 }
3205 ++p;
3206 }
3207 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00003208 return p;
3209}
3210
3211/*
Bram Moolenaar81695252004-12-29 20:58:21 +00003212 * Load file "fname" into a dummy buffer and return the buffer pointer.
3213 * Returns NULL if it fails.
3214 * Must call unload_dummy_buffer() or wipe_dummy_buffer() later!
3215 */
3216 static buf_T *
3217load_dummy_buffer(fname)
3218 char_u *fname;
3219{
3220 buf_T *newbuf;
3221 int failed = TRUE;
3222#ifdef FEAT_AUTOCMD
3223 aco_save_T aco;
3224#else
3225 buf_T *old_curbuf = curbuf;
3226#endif
3227
3228 /* Allocate a buffer without putting it in the buffer list. */
3229 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
3230 if (newbuf == NULL)
3231 return NULL;
3232
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00003233 /* Init the options. */
3234 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
3235
Bram Moolenaar81695252004-12-29 20:58:21 +00003236#ifdef FEAT_AUTOCMD
3237 /* set curwin/curbuf to buf and save a few things */
3238 aucmd_prepbuf(&aco, newbuf);
3239#else
3240 curbuf = newbuf;
3241 curwin->w_buffer = newbuf;
3242#endif
3243
3244 /* Need to set the filename for autocommands. */
3245 (void)setfname(curbuf, fname, NULL, FALSE);
3246
Bram Moolenaar4770d092006-01-12 23:22:24 +00003247 if (ml_open(curbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00003248 {
3249 /* Create swap file now to avoid the ATTENTION message. */
3250 check_need_swap(TRUE);
3251
3252 /* Remove the "dummy" flag, otherwise autocommands may not
3253 * work. */
3254 curbuf->b_flags &= ~BF_DUMMY;
3255
3256 if (readfile(fname, NULL,
3257 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
3258 NULL, READ_NEW | READ_DUMMY) == OK
3259 && !(curbuf->b_flags & BF_NEW))
3260 {
3261 failed = FALSE;
3262 if (curbuf != newbuf)
3263 {
3264 /* Bloody autocommands changed the buffer! */
3265 if (buf_valid(newbuf))
3266 wipe_buffer(newbuf, FALSE);
3267 newbuf = curbuf;
3268 }
3269 }
3270 }
3271
3272#ifdef FEAT_AUTOCMD
3273 /* restore curwin/curbuf and a few other things */
3274 aucmd_restbuf(&aco);
3275#else
3276 curbuf = old_curbuf;
3277 curwin->w_buffer = old_curbuf;
3278#endif
3279
3280 if (!buf_valid(newbuf))
3281 return NULL;
3282 if (failed)
3283 {
3284 wipe_dummy_buffer(newbuf);
3285 return NULL;
3286 }
3287 return newbuf;
3288}
3289
3290/*
3291 * Wipe out the dummy buffer that load_dummy_buffer() created.
3292 */
3293 static void
3294wipe_dummy_buffer(buf)
3295 buf_T *buf;
3296{
3297 if (curbuf != buf) /* safety check */
3298 wipe_buffer(buf, FALSE);
3299}
3300
3301/*
3302 * Unload the dummy buffer that load_dummy_buffer() created.
3303 */
3304 static void
3305unload_dummy_buffer(buf)
3306 buf_T *buf;
3307{
3308 if (curbuf != buf) /* safety check */
3309 close_buffer(NULL, buf, DOBUF_UNLOAD);
3310}
3311
Bram Moolenaar05159a02005-02-26 23:04:13 +00003312#if defined(FEAT_EVAL) || defined(PROTO)
3313/*
3314 * Add each quickfix error to list "list" as a dictionary.
3315 */
3316 int
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003317get_errorlist(wp, list)
3318 win_T *wp;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003319 list_T *list;
3320{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003321 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003322 dict_T *dict;
3323 char_u buf[2];
3324 qfline_T *qfp;
3325 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003326
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003327 if (wp != NULL)
3328 {
3329 qi = GET_LOC_LIST(wp);
3330 if (qi == NULL)
3331 return FAIL;
3332 }
3333
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003334 if (qi->qf_curlist >= qi->qf_listcount
3335 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003336 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003337
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003338 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3339 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003340 {
3341 if ((dict = dict_alloc()) == NULL)
3342 return FAIL;
3343 if (list_append_dict(list, dict) == FAIL)
3344 return FAIL;
3345
3346 buf[0] = qfp->qf_type;
3347 buf[1] = NUL;
3348 if ( dict_add_nr_str(dict, "bufnr", (long)qfp->qf_fnum, NULL) == FAIL
3349 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
3350 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
3351 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
3352 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003353 || dict_add_nr_str(dict, "pattern", 0L, qfp->qf_pattern) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003354 || dict_add_nr_str(dict, "text", 0L, qfp->qf_text) == FAIL
3355 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
3356 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
3357 return FAIL;
3358
3359 qfp = qfp->qf_next;
3360 }
3361 return OK;
3362}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003363
3364/*
3365 * Populate the quickfix list with the items supplied in the list
3366 * of dictionaries.
3367 */
3368 int
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003369set_errorlist(wp, list, action)
3370 win_T *wp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003371 list_T *list;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003372 int action;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003373{
3374 listitem_T *li;
3375 dict_T *d;
3376 char_u *filename, *pattern, *text, *type;
3377 long lnum;
3378 int col, nr;
3379 int vcol;
3380 qfline_T *prevp = NULL;
3381 int valid, status;
3382 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003383 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003384
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003385 if (wp != NULL)
3386 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00003387 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003388 if (qi == NULL)
3389 return FAIL;
3390 }
3391
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003392 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003393 /* make place for a new list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003394 qf_new_list(qi);
3395 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003396 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003397 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003398 prevp->qf_next != prevp; prevp = prevp->qf_next)
3399 ;
3400 else if (action == 'r')
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003401 qf_free(qi, qi->qf_curlist);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003402
3403 for (li = list->lv_first; li != NULL; li = li->li_next)
3404 {
3405 if (li->li_tv.v_type != VAR_DICT)
3406 continue; /* Skip non-dict items */
3407
3408 d = li->li_tv.vval.v_dict;
3409 if (d == NULL)
3410 continue;
3411
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003412 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003413 lnum = get_dict_number(d, (char_u *)"lnum");
3414 col = get_dict_number(d, (char_u *)"col");
3415 vcol = get_dict_number(d, (char_u *)"vcol");
3416 nr = get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003417 type = get_dict_string(d, (char_u *)"type", TRUE);
3418 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
3419 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003420 if (text == NULL)
3421 text = vim_strsave((char_u *)"");
3422
3423 valid = TRUE;
3424 if (filename == NULL || (lnum == 0 && pattern == NULL))
3425 valid = FALSE;
3426
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003427 status = qf_add_entry(qi, &prevp,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003428 NULL, /* dir */
3429 filename,
3430 text,
3431 lnum,
3432 col,
3433 vcol, /* vis_col */
3434 pattern, /* search pattern */
3435 nr,
3436 type == NULL ? NUL : *type,
3437 valid);
3438
3439 vim_free(filename);
3440 vim_free(pattern);
3441 vim_free(text);
3442 vim_free(type);
3443
3444 if (status == FAIL)
3445 {
3446 retval = FAIL;
3447 break;
3448 }
3449 }
3450
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003451 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3452 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3453 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003454
3455#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003456 qf_update_buffer(qi);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003457#endif
3458
3459 return retval;
3460}
Bram Moolenaar05159a02005-02-26 23:04:13 +00003461#endif
3462
Bram Moolenaar81695252004-12-29 20:58:21 +00003463/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003464 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003465 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003466 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003467 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00003468 */
3469 void
3470ex_cbuffer(eap)
3471 exarg_T *eap;
3472{
3473 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003474 qf_info_T *qi = &ql_info;
3475
Bram Moolenaara6557602006-02-04 22:43:20 +00003476 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003477 {
3478 qi = ll_get_or_alloc_list(curwin);
3479 if (qi == NULL)
3480 return;
3481 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003482
3483 if (*eap->arg == NUL)
3484 buf = curbuf;
3485 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
3486 buf = buflist_findnr(atoi((char *)eap->arg));
3487 if (buf == NULL)
3488 EMSG(_(e_invarg));
3489 else if (buf->b_ml.ml_mfp == NULL)
3490 EMSG(_("E681: Buffer is not loaded"));
3491 else
3492 {
3493 if (eap->addr_count == 0)
3494 {
3495 eap->line1 = 1;
3496 eap->line2 = buf->b_ml.ml_line_count;
3497 }
3498 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
3499 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
3500 EMSG(_(e_invrange));
3501 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00003502 {
3503 int buffer_cmd = (eap->cmdidx == CMD_cbuffer
3504 || eap->cmdidx == CMD_lbuffer);
3505
3506 if (qf_init_ext(qi, NULL, buf, NULL, p_efm, buffer_cmd,
3507 eap->line1, eap->line2) > 0
3508 && buffer_cmd)
3509 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
3510 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003511 }
3512}
3513
Bram Moolenaar1e015462005-09-25 22:16:38 +00003514#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003515/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003516 * ":cexpr {expr}" and ":caddexpr {expr}" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003517 * ":lexpr {expr}" and ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003518 */
3519 void
3520ex_cexpr(eap)
3521 exarg_T *eap;
3522{
3523 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003524 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003525
3526 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_laddexpr)
3527 {
3528 qi = ll_get_or_alloc_list(curwin);
3529 if (qi == NULL)
3530 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003531 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003532
Bram Moolenaar4770d092006-01-12 23:22:24 +00003533 /* Evaluate the expression. When the result is a string or a list we can
3534 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003535 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003536 if (tv != NULL)
3537 {
3538 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
3539 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
3540 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003541 int expr_cmd = (eap->cmdidx == CMD_cexpr
3542 || eap->cmdidx == CMD_lexpr);
3543 if (qf_init_ext(qi, NULL, NULL, tv, p_efm, expr_cmd,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003544 (linenr_T)0, (linenr_T)0) > 0
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003545 && expr_cmd)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003546 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003547 }
3548 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003549 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00003550 free_tv(tv);
3551 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003552}
Bram Moolenaar1e015462005-09-25 22:16:38 +00003553#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003554
3555/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 * ":helpgrep {pattern}"
3557 */
3558 void
3559ex_helpgrep(eap)
3560 exarg_T *eap;
3561{
3562 regmatch_T regmatch;
3563 char_u *save_cpo;
3564 char_u *p;
3565 int fcount;
3566 char_u **fnames;
3567 FILE *fd;
3568 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003569 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003571#ifdef FEAT_MULTI_LANG
3572 char_u *lang;
3573#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003574 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003575 int new_qi = FALSE;
3576 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577
3578 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
3579 save_cpo = p_cpo;
3580 p_cpo = (char_u *)"";
3581
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003582#ifdef FEAT_MULTI_LANG
3583 /* Check for a specified language */
3584 lang = check_help_lang(eap->arg);
3585#endif
3586
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003587 if (eap->cmdidx == CMD_lhelpgrep)
3588 {
3589 /* Find an existing help window */
3590 FOR_ALL_WINDOWS(wp)
3591 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
3592 break;
3593
3594 if (wp == NULL) /* Help window not found */
3595 qi = NULL;
3596 else
3597 qi = wp->w_llist;
3598
3599 if (qi == NULL)
3600 {
3601 /* Allocate a new location list for help text matches */
3602 if ((qi = ll_new_list()) == NULL)
3603 return;
3604 new_qi = TRUE;
3605 }
3606 }
3607
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
3609 regmatch.rm_ic = FALSE;
3610 if (regmatch.regprog != NULL)
3611 {
3612 /* create a new quickfix list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003613 qf_new_list(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614
3615 /* Go through all directories in 'runtimepath' */
3616 p = p_rtp;
3617 while (*p != NUL && !got_int)
3618 {
3619 copy_option_part(&p, NameBuff, MAXPATHL, ",");
3620
3621 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
3622 add_pathsep(NameBuff);
3623 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
3624 if (gen_expand_wildcards(1, &NameBuff, &fcount,
3625 &fnames, EW_FILE|EW_SILENT) == OK
3626 && fcount > 0)
3627 {
3628 for (fi = 0; fi < fcount && !got_int; ++fi)
3629 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003630#ifdef FEAT_MULTI_LANG
3631 /* Skip files for a different language. */
3632 if (lang != NULL
3633 && STRNICMP(lang, fnames[fi]
3634 + STRLEN(fnames[fi]) - 3, 2) != 0
3635 && !(STRNICMP(lang, "en", 2) == 0
3636 && STRNICMP("txt", fnames[fi]
3637 + STRLEN(fnames[fi]) - 3, 3) == 0))
3638 continue;
3639#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003640 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 if (fd != NULL)
3642 {
3643 lnum = 1;
3644 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
3645 {
3646 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
3647 {
3648 int l = STRLEN(IObuff);
3649
3650 /* remove trailing CR, LF, spaces, etc. */
3651 while (l > 0 && IObuff[l - 1] <= ' ')
3652 IObuff[--l] = NUL;
3653
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003654 if (qf_add_entry(qi, &prevp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 NULL, /* dir */
3656 fnames[fi],
3657 IObuff,
3658 lnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00003659 (int)(regmatch.startp[0] - IObuff)
3660 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00003661 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003662 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 0, /* nr */
3664 1, /* type */
3665 TRUE /* valid */
3666 ) == FAIL)
3667 {
3668 got_int = TRUE;
3669 break;
3670 }
3671 }
3672 ++lnum;
3673 line_breakcheck();
3674 }
3675 fclose(fd);
3676 }
3677 }
3678 FreeWild(fcount, fnames);
3679 }
3680 }
3681 vim_free(regmatch.regprog);
3682
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003683 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3684 qi->qf_lists[qi->qf_curlist].qf_ptr =
3685 qi->qf_lists[qi->qf_curlist].qf_start;
3686 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 }
3688
3689 p_cpo = save_cpo;
3690
3691#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003692 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693#endif
3694
3695 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003696 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003697 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003698 else
3699 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003700
3701 if (eap->cmdidx == CMD_lhelpgrep)
3702 {
3703 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00003704 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003705 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
3706 {
3707 if (new_qi)
3708 ll_free_all(&qi);
3709 }
3710 else if (curwin->w_llist == NULL)
3711 curwin->w_llist = qi;
3712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713}
3714
3715#endif /* FEAT_QUICKFIX */