blob: bf0b0b8c6c60ec208f5db77c3d5fb1eef751c8fe [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 Moolenaard6f676d2005-06-01 21:51:55 +000052static struct qf_list
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 Moolenaar071d4272004-06-13 20:20:40 +000059} qf_lists[LISTCOUNT];
60
61static int qf_curlist = 0; /* current error list */
62static int qf_listcount = 0; /* current number of lists */
63
Bram Moolenaar68b76a62005-03-25 21:53:48 +000064#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +000065
66/*
67 * Structure used to hold the info of one part of 'errorformat'
68 */
69struct eformat
70{
71 regprog_T *prog; /* pre-formatted part of 'errorformat' */
72 struct eformat *next; /* pointer to next (NULL if last) */
73 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
74 char_u prefix; /* prefix of this format line: */
75 /* 'D' enter directory */
76 /* 'X' leave directory */
77 /* 'A' start of multi-line message */
78 /* 'E' error message */
79 /* 'W' warning message */
80 /* 'I' informational message */
81 /* 'C' continuation line */
82 /* 'Z' end of multi-line message */
83 /* 'G' general, unspecific message */
84 /* 'P' push file (partial) message */
85 /* 'Q' pop/quit file (partial) message */
86 /* 'O' overread (partial) message */
87 char_u flags; /* additional flags given in prefix */
88 /* '-' do not include this line */
89};
90
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000091static int qf_init_ext __ARGS((char_u *efile, buf_T *buf, typval_T *tv, char_u *errorformat, int newlist, linenr_T lnumfirst, linenr_T lnumlast));
Bram Moolenaar071d4272004-06-13 20:20:40 +000092static void qf_new_list __ARGS((void));
Bram Moolenaar68b76a62005-03-25 21:53:48 +000093static int qf_add_entry __ARGS((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));
Bram Moolenaar071d4272004-06-13 20:20:40 +000094static void qf_msg __ARGS((void));
95static void qf_free __ARGS((int idx));
96static char_u *qf_types __ARGS((int, int));
97static int qf_get_fnum __ARGS((char_u *, char_u *));
98static char_u *qf_push_dir __ARGS((char_u *, struct dir_stack_T **));
99static char_u *qf_pop_dir __ARGS((struct dir_stack_T **));
100static char_u *qf_guess_filepath __ARGS((char_u *));
101static void qf_fmt_text __ARGS((char_u *text, char_u *buf, int bufsize));
102static void qf_clean_dir_stack __ARGS((struct dir_stack_T **));
103#ifdef FEAT_WINDOWS
104static int qf_win_pos_update __ARGS((int old_qf_index));
105static buf_T *qf_find_buf __ARGS((void));
106static void qf_update_buffer __ARGS((void));
107static void qf_fill_buffer __ARGS((void));
108#endif
109static char_u *get_mef_name __ARGS((void));
Bram Moolenaar81695252004-12-29 20:58:21 +0000110static buf_T *load_dummy_buffer __ARGS((char_u *fname));
111static void wipe_dummy_buffer __ARGS((buf_T *buf));
112static void unload_dummy_buffer __ARGS((buf_T *buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113
114/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000115 * Read the errorfile "efile" into memory, line by line, building the error
116 * list.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117 * Return -1 for error, number of errors for success.
118 */
119 int
120qf_init(efile, errorformat, newlist)
121 char_u *efile;
122 char_u *errorformat;
123 int newlist; /* TRUE: start a new error list */
124{
Bram Moolenaar86b68352004-12-27 21:59:20 +0000125 if (efile == NULL)
126 return FAIL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000127 return qf_init_ext(efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar86b68352004-12-27 21:59:20 +0000128 (linenr_T)0, (linenr_T)0);
129}
130
131/*
132 * Read the errorfile "efile" into memory, line by line, building the error
133 * list.
134 * Alternative: when "efile" is null read errors from buffer "buf".
135 * Always use 'errorformat' from "buf" if there is a local value.
136 * Then lnumfirst and lnumlast specify the range of lines to use.
137 * Return -1 for error, number of errors for success.
138 */
139 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000140qf_init_ext(efile, buf, tv, errorformat, newlist, lnumfirst, lnumlast)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000141 char_u *efile;
142 buf_T *buf;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000143 typval_T *tv;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000144 char_u *errorformat;
145 int newlist; /* TRUE: start a new error list */
146 linenr_T lnumfirst; /* first line number to use */
147 linenr_T lnumlast; /* last line number to use */
148{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 char_u *namebuf;
150 char_u *errmsg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000151 char_u *pattern;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 char_u *fmtstr = NULL;
153 int col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000154 char_u use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 int type = 0;
156 int valid;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000157 linenr_T buflnum = lnumfirst;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 long lnum = 0L;
159 int enr = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000160 FILE *fd = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000161 qfline_T *qfprev = NULL; /* init to make SASC shut up */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 char_u *efmp;
163 struct eformat *fmt_first = NULL;
164 struct eformat *fmt_last = NULL;
165 struct eformat *fmt_ptr;
166 char_u *efm;
167 char_u *ptr;
168 char_u *srcptr;
169 int len;
170 int i;
171 int round;
172 int idx = 0;
173 int multiline = FALSE;
174 int multiignore = FALSE;
175 int multiscan = FALSE;
176 int retval = -1; /* default: return error flag */
177 char_u *directory = NULL;
178 char_u *currfile = NULL;
179 char_u *tail = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000180 char_u *p_str = NULL;
181 listitem_T *p_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 struct dir_stack_T *file_stack = NULL;
183 regmatch_T regmatch;
184 static struct fmtpattern
185 {
186 char_u convchar;
187 char *pattern;
188 } fmt_pat[FMT_PATTERNS] =
189 {
190 {'f', "\\f\\+"},
191 {'n', "\\d\\+"},
192 {'l', "\\d\\+"},
193 {'c', "\\d\\+"},
194 {'t', "."},
195 {'m', ".\\+"},
196 {'r', ".*"},
197 {'p', "[- .]*"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000198 {'v', "\\d\\+"},
199 {'s', ".\\+"}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 };
201
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 namebuf = alloc(CMDBUFFSIZE + 1);
203 errmsg = alloc(CMDBUFFSIZE + 1);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000204 pattern = alloc(CMDBUFFSIZE + 1);
205 if (namebuf == NULL || errmsg == NULL || pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 goto qf_init_end;
207
Bram Moolenaar86b68352004-12-27 21:59:20 +0000208 if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 {
210 EMSG2(_(e_openerrf), efile);
211 goto qf_init_end;
212 }
213
214 if (newlist || qf_curlist == qf_listcount)
215 /* make place for a new list */
216 qf_new_list();
217 else if (qf_lists[qf_curlist].qf_count > 0)
218 /* Adding to existing list, find last entry. */
219 for (qfprev = qf_lists[qf_curlist].qf_start;
220 qfprev->qf_next != qfprev; qfprev = qfprev->qf_next)
221 ;
222
223/*
224 * Each part of the format string is copied and modified from errorformat to
225 * regex prog. Only a few % characters are allowed.
226 */
227 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000228 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000229 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 else
231 efm = errorformat;
232 /*
233 * Get some space to modify the format string into.
234 */
235 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
236 for (round = FMT_PATTERNS; round > 0; )
237 i += (int)STRLEN(fmt_pat[--round].pattern);
238#ifdef COLON_IN_FILENAME
239 i += 12; /* "%f" can become twelve chars longer */
240#else
241 i += 2; /* "%f" can become two chars longer */
242#endif
243 if ((fmtstr = alloc(i)) == NULL)
244 goto error2;
245
246 while (efm[0])
247 {
248 /*
249 * Allocate a new eformat structure and put it at the end of the list
250 */
251 fmt_ptr = (struct eformat *)alloc((unsigned)sizeof(struct eformat));
252 if (fmt_ptr == NULL)
253 goto error2;
254 if (fmt_first == NULL) /* first one */
255 fmt_first = fmt_ptr;
256 else
257 fmt_last->next = fmt_ptr;
258 fmt_last = fmt_ptr;
259 fmt_ptr->prefix = NUL;
260 fmt_ptr->flags = NUL;
261 fmt_ptr->next = NULL;
262 fmt_ptr->prog = NULL;
263 for (round = FMT_PATTERNS; round > 0; )
264 fmt_ptr->addr[--round] = NUL;
265 /* round is 0 now */
266
267 /*
268 * Isolate one part in the 'errorformat' option
269 */
270 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
271 if (efm[len] == '\\' && efm[len + 1] != NUL)
272 ++len;
273
274 /*
275 * Build regexp pattern from current 'errorformat' option
276 */
277 ptr = fmtstr;
278 *ptr++ = '^';
279 for (efmp = efm; efmp < efm + len; ++efmp)
280 {
281 if (*efmp == '%')
282 {
283 ++efmp;
284 for (idx = 0; idx < FMT_PATTERNS; ++idx)
285 if (fmt_pat[idx].convchar == *efmp)
286 break;
287 if (idx < FMT_PATTERNS)
288 {
289 if (fmt_ptr->addr[idx])
290 {
291 sprintf((char *)errmsg,
292 _("E372: Too many %%%c in format string"), *efmp);
293 EMSG(errmsg);
294 goto error2;
295 }
296 if ((idx
297 && idx < 6
298 && vim_strchr((char_u *)"DXOPQ",
299 fmt_ptr->prefix) != NULL)
300 || (idx == 6
301 && vim_strchr((char_u *)"OPQ",
302 fmt_ptr->prefix) == NULL))
303 {
304 sprintf((char *)errmsg,
305 _("E373: Unexpected %%%c in format string"), *efmp);
306 EMSG(errmsg);
307 goto error2;
308 }
309 fmt_ptr->addr[idx] = (char_u)++round;
310 *ptr++ = '\\';
311 *ptr++ = '(';
312#ifdef BACKSLASH_IN_FILENAME
313 if (*efmp == 'f')
314 {
315 /* Also match "c:" in the file name, even when
316 * checking for a colon next: "%f:".
317 * "\%(\a:\)\=" */
318 STRCPY(ptr, "\\%(\\a:\\)\\=");
319 ptr += 10;
320 }
321#endif
322 if (*efmp == 'f' && efmp[1] != NUL
323 && efmp[1] != '\\' && efmp[1] != '%')
324 {
325 /* A file name may contain spaces, but this isn't in
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +0000326 * "\f". For "%f:%l:%m" there may be a ":" in the
327 * file name. Use ".\{-1,}x" instead (x is the next
328 * character), the requirement that :999: follows
329 * should work. */
330 STRCPY(ptr, ".\\{-1,}");
331 ptr += 7;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 }
333 else
334 {
335 srcptr = (char_u *)fmt_pat[idx].pattern;
336 while ((*ptr = *srcptr++) != NUL)
337 ++ptr;
338 }
339 *ptr++ = '\\';
340 *ptr++ = ')';
341 }
342 else if (*efmp == '*')
343 {
344 if (*++efmp == '[' || *efmp == '\\')
345 {
346 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
347 {
348 if (efmp[1] == '^')
349 *ptr++ = *++efmp;
350 if (efmp < efm + len)
351 {
352 *ptr++ = *++efmp; /* could be ']' */
353 while (efmp < efm + len
354 && (*ptr++ = *++efmp) != ']')
355 /* skip */;
356 if (efmp == efm + len)
357 {
358 EMSG(_("E374: Missing ] in format string"));
359 goto error2;
360 }
361 }
362 }
363 else if (efmp < efm + len) /* %*\D, %*\s etc. */
364 *ptr++ = *++efmp;
365 *ptr++ = '\\';
366 *ptr++ = '+';
367 }
368 else
369 {
370 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
371 sprintf((char *)errmsg,
372 _("E375: Unsupported %%%c in format string"), *efmp);
373 EMSG(errmsg);
374 goto error2;
375 }
376 }
377 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
378 *ptr++ = *efmp; /* regexp magic characters */
379 else if (*efmp == '#')
380 *ptr++ = '*';
381 else if (efmp == efm + 1) /* analyse prefix */
382 {
383 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
384 fmt_ptr->flags = *efmp++;
385 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
386 fmt_ptr->prefix = *efmp;
387 else
388 {
389 sprintf((char *)errmsg,
390 _("E376: Invalid %%%c in format string prefix"), *efmp);
391 EMSG(errmsg);
392 goto error2;
393 }
394 }
395 else
396 {
397 sprintf((char *)errmsg,
398 _("E377: Invalid %%%c in format string"), *efmp);
399 EMSG(errmsg);
400 goto error2;
401 }
402 }
403 else /* copy normal character */
404 {
405 if (*efmp == '\\' && efmp + 1 < efm + len)
406 ++efmp;
407 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
408 *ptr++ = '\\'; /* escape regexp atoms */
409 if (*efmp)
410 *ptr++ = *efmp;
411 }
412 }
413 *ptr++ = '$';
414 *ptr = NUL;
415 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
416 goto error2;
417 /*
418 * Advance to next part
419 */
420 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
421 }
422 if (fmt_first == NULL) /* nothing found */
423 {
424 EMSG(_("E378: 'errorformat' contains no pattern"));
425 goto error2;
426 }
427
428 /*
429 * got_int is reset here, because it was probably set when killing the
430 * ":make" command, but we still want to read the errorfile then.
431 */
432 got_int = FALSE;
433
434 /* Always ignore case when looking for a matching error. */
435 regmatch.rm_ic = TRUE;
436
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000437 if (tv != NULL)
438 {
439 if (tv->v_type == VAR_STRING)
440 p_str = tv->vval.v_string;
441 else if (tv->v_type == VAR_LIST)
442 p_li = tv->vval.v_list->lv_first;
443 }
444
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 /*
446 * Read the lines in the error file one by one.
447 * Try to recognize one of the error formats in each line.
448 */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000449 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 {
Bram Moolenaar86b68352004-12-27 21:59:20 +0000451 /* Get the next line. */
452 if (fd == NULL)
453 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000454 if (tv != NULL)
455 {
456 int len;
457
458 if (tv->v_type == VAR_STRING)
459 {
460 /* Get the next line from the supplied string */
461 char_u *p;
462
463 if (!*p_str) /* Reached the end of the string */
464 break;
465
466 p = vim_strchr(p_str, '\n');
467 if (p)
468 len = p - p_str + 1;
469 else
470 len = STRLEN(p_str);
471
472 if (len > CMDBUFFSIZE - 2)
473 vim_strncpy(IObuff, p_str, CMDBUFFSIZE - 2);
474 else
475 vim_strncpy(IObuff, p_str, len);
476
477 p_str += len;
478 }
479 else if (tv->v_type == VAR_LIST)
480 {
481 /* Get the next line from the supplied list */
482 while (p_li && p_li->li_tv.v_type != VAR_STRING)
483 p_li = p_li->li_next; /* Skip non-string items */
484
485 if (!p_li) /* End of the list */
486 break;
487
488 len = STRLEN(p_li->li_tv.vval.v_string);
489 if (len > CMDBUFFSIZE - 2)
490 len = CMDBUFFSIZE - 2;
491
492 vim_strncpy(IObuff, p_li->li_tv.vval.v_string, len);
493
494 p_li = p_li->li_next; /* next item */
495 }
496 }
497 else
498 {
499 /* Get the next line from the supplied buffer */
500 if (buflnum > lnumlast)
501 break;
502 vim_strncpy(IObuff, ml_get_buf(buf, buflnum++, FALSE),
503 CMDBUFFSIZE - 2);
504 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000505 }
506 else if (fgets((char *)IObuff, CMDBUFFSIZE - 2, fd) == NULL)
507 break;
508
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 IObuff[CMDBUFFSIZE - 2] = NUL; /* for very long lines */
510 if ((efmp = vim_strrchr(IObuff, '\n')) != NULL)
511 *efmp = NUL;
512#ifdef USE_CRNL
513 if ((efmp = vim_strrchr(IObuff, '\r')) != NULL)
514 *efmp = NUL;
515#endif
516
517 /*
518 * Try to match each part of 'errorformat' until we find a complete
519 * match or no match.
520 */
521 valid = TRUE;
522restofline:
523 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
524 {
525 idx = fmt_ptr->prefix;
526 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
527 continue;
528 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000529 pattern[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 if (!multiscan)
531 errmsg[0] = NUL;
532 lnum = 0;
533 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000534 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 enr = -1;
536 type = 0;
537 tail = NULL;
538
539 regmatch.regprog = fmt_ptr->prog;
540 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
541 {
542 if ((idx == 'C' || idx == 'Z') && !multiline)
543 continue;
544 if (vim_strchr((char_u *)"EWI", idx) != NULL)
545 type = idx;
546 else
547 type = 0;
548 /*
549 * Extract error message data from matched line
550 */
551 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
552 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000553 int c = *regmatch.endp[i];
554
555 /* Expand ~/file and $HOME/file to full path. */
556 *regmatch.endp[i] = NUL;
557 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
558 *regmatch.endp[i] = c;
559
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000561 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 continue;
563 }
564 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
565 enr = (int)atol((char *)regmatch.startp[i]);
566 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
567 lnum = atol((char *)regmatch.startp[i]);
568 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
569 col = (int)atol((char *)regmatch.startp[i]);
570 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
571 type = *regmatch.startp[i];
572 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
573 STRCPY(errmsg, IObuff);
574 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
575 {
576 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaarbbebc852005-07-18 21:47:53 +0000577 vim_strncpy(errmsg, regmatch.startp[i], len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 }
579 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
580 tail = regmatch.startp[i];
581 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
582 {
583 col = (int)(regmatch.endp[i] - regmatch.startp[i] + 1);
584 if (*((char_u *)regmatch.startp[i]) != TAB)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000585 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 }
587 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
588 {
589 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000590 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000592 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
593 {
594 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
595 if (len > CMDBUFFSIZE - 5)
596 len = CMDBUFFSIZE - 5;
597 STRCPY(pattern, "^\\V");
598 STRNCAT(pattern, regmatch.startp[i], len);
599 pattern[len + 3] = '\\';
600 pattern[len + 4] = '$';
601 pattern[len + 5] = NUL;
602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 break;
604 }
605 }
606 multiscan = FALSE;
607 if (!fmt_ptr || idx == 'D' || idx == 'X')
608 {
609 if (fmt_ptr)
610 {
611 if (idx == 'D') /* enter directory */
612 {
613 if (*namebuf == NUL)
614 {
615 EMSG(_("E379: Missing or empty directory name"));
616 goto error2;
617 }
618 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
619 goto error2;
620 }
621 else if (idx == 'X') /* leave directory */
622 directory = qf_pop_dir(&dir_stack);
623 }
624 namebuf[0] = NUL; /* no match found, remove file name */
625 lnum = 0; /* don't jump to this line */
626 valid = FALSE;
627 STRCPY(errmsg, IObuff); /* copy whole line to error message */
628 if (!fmt_ptr)
629 multiline = multiignore = FALSE;
630 }
631 else if (fmt_ptr)
632 {
633 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
634 multiline = TRUE; /* start of a multi-line message */
635 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
636 { /* continuation of multi-line msg */
637 if (qfprev == NULL)
638 goto error2;
639 if (*errmsg && !multiignore)
640 {
641 len = (int)STRLEN(qfprev->qf_text);
642 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
643 == NULL)
644 goto error2;
645 STRCPY(ptr, qfprev->qf_text);
646 vim_free(qfprev->qf_text);
647 qfprev->qf_text = ptr;
648 *(ptr += len) = '\n';
649 STRCPY(++ptr, errmsg);
650 }
651 if (qfprev->qf_nr == -1)
652 qfprev->qf_nr = enr;
653 if (vim_isprintc(type) && !qfprev->qf_type)
654 qfprev->qf_type = type; /* only printable chars allowed */
655 if (!qfprev->qf_lnum)
656 qfprev->qf_lnum = lnum;
657 if (!qfprev->qf_col)
658 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000659 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 if (!qfprev->qf_fnum)
661 qfprev->qf_fnum = qf_get_fnum(directory,
662 *namebuf || directory ? namebuf
663 : currfile && valid ? currfile : 0);
664 if (idx == 'Z')
665 multiline = multiignore = FALSE;
666 line_breakcheck();
667 continue;
668 }
669 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
670 {
671 /* global file names */
672 valid = FALSE;
673 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
674 {
675 if (*namebuf && idx == 'P')
676 currfile = qf_push_dir(namebuf, &file_stack);
677 else if (idx == 'Q')
678 currfile = qf_pop_dir(&file_stack);
679 *namebuf = NUL;
680 if (tail && *tail)
681 {
682 STRCPY(IObuff, skipwhite(tail));
683 multiscan = TRUE;
684 goto restofline;
685 }
686 }
687 }
688 if (fmt_ptr->flags == '-') /* generally exclude this line */
689 {
690 if (multiline)
691 multiignore = TRUE; /* also exclude continuation lines */
692 continue;
693 }
694 }
695
696 if (qf_add_entry(&qfprev,
697 directory,
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000698 (*namebuf || directory)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 ? namebuf
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000700 : ((currfile && valid) ? currfile : (char_u *)NULL),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 errmsg,
702 lnum,
703 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +0000704 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000705 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 enr,
707 type,
708 valid) == FAIL)
709 goto error2;
710 line_breakcheck();
711 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000712 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 {
714 if (qf_lists[qf_curlist].qf_index == 0) /* no valid entry found */
715 {
716 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
717 qf_lists[qf_curlist].qf_index = 1;
718 qf_lists[qf_curlist].qf_nonevalid = TRUE;
719 }
720 else
721 {
722 qf_lists[qf_curlist].qf_nonevalid = FALSE;
723 if (qf_lists[qf_curlist].qf_ptr == NULL)
724 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
725 }
726 retval = qf_lists[qf_curlist].qf_count; /* return number of matches */
727 goto qf_init_ok;
728 }
729 EMSG(_(e_readerrf));
730error2:
731 qf_free(qf_curlist);
732 qf_listcount--;
733 if (qf_curlist > 0)
734 --qf_curlist;
735qf_init_ok:
Bram Moolenaar86b68352004-12-27 21:59:20 +0000736 if (fd != NULL)
737 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
739 {
740 fmt_first = fmt_ptr->next;
741 vim_free(fmt_ptr->prog);
742 vim_free(fmt_ptr);
743 }
744 qf_clean_dir_stack(&dir_stack);
745 qf_clean_dir_stack(&file_stack);
746qf_init_end:
747 vim_free(namebuf);
748 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000749 vim_free(pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 vim_free(fmtstr);
751
752#ifdef FEAT_WINDOWS
753 qf_update_buffer();
754#endif
755
756 return retval;
757}
758
759/*
760 * Prepare for adding a new quickfix list.
761 */
762 static void
763qf_new_list()
764{
765 int i;
766
767 /*
768 * If the current entry is not the last entry, delete entries below
769 * the current entry. This makes it possible to browse in a tree-like
770 * way with ":grep'.
771 */
772 while (qf_listcount > qf_curlist + 1)
773 qf_free(--qf_listcount);
774
775 /*
776 * When the stack is full, remove to oldest entry
777 * Otherwise, add a new entry.
778 */
779 if (qf_listcount == LISTCOUNT)
780 {
781 qf_free(0);
782 for (i = 1; i < LISTCOUNT; ++i)
783 qf_lists[i - 1] = qf_lists[i];
784 qf_curlist = LISTCOUNT - 1;
785 }
786 else
787 qf_curlist = qf_listcount++;
788 qf_lists[qf_curlist].qf_index = 0;
789 qf_lists[qf_curlist].qf_count = 0;
790}
791
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000792#if defined(EXITFREE) || defined(PROTO)
793 void
794qf_free_all()
795{
796 int i;
797
798 for (i = 0; i < qf_listcount; ++i)
799 qf_free(i);
800}
801#endif
802
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803/*
804 * Add an entry to the end of the list of errors.
805 * Returns OK or FAIL.
806 */
807 static int
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000808qf_add_entry(prevp, dir, fname, mesg, lnum, col, vis_col, pattern, nr, type,
809 valid)
810 qfline_T **prevp; /* pointer to previously added entry or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 char_u *dir; /* optional directory name */
812 char_u *fname; /* file name or NULL */
813 char_u *mesg; /* message */
814 long lnum; /* line number */
815 int col; /* column */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000816 int vis_col; /* using visual column */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000817 char_u *pattern; /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 int nr; /* error number */
819 int type; /* type character */
820 int valid; /* valid entry */
821{
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000822 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000824 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 return FAIL;
826 qfp->qf_fnum = qf_get_fnum(dir, fname);
827 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
828 {
829 vim_free(qfp);
830 return FAIL;
831 }
832 qfp->qf_lnum = lnum;
833 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000834 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000835 if (pattern == NULL || *pattern == NUL)
836 qfp->qf_pattern = NULL;
837 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
838 {
839 vim_free(qfp->qf_text);
840 vim_free(qfp);
841 return FAIL;
842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 qfp->qf_nr = nr;
844 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
845 type = 0;
846 qfp->qf_type = type;
847 qfp->qf_valid = valid;
848
849 if (qf_lists[qf_curlist].qf_count == 0) /* first element in the list */
850 {
851 qf_lists[qf_curlist].qf_start = qfp;
852 qfp->qf_prev = qfp; /* first element points to itself */
853 }
854 else
855 {
856 qfp->qf_prev = *prevp;
857 (*prevp)->qf_next = qfp;
858 }
859 qfp->qf_next = qfp; /* last element points to itself */
860 qfp->qf_cleared = FALSE;
861 *prevp = qfp;
862 ++qf_lists[qf_curlist].qf_count;
863 if (qf_lists[qf_curlist].qf_index == 0 && qfp->qf_valid)
864 /* first valid entry */
865 {
866 qf_lists[qf_curlist].qf_index = qf_lists[qf_curlist].qf_count;
867 qf_lists[qf_curlist].qf_ptr = qfp;
868 }
869
870 return OK;
871}
872
873/*
874 * get buffer number for file "dir.name"
875 */
876 static int
877qf_get_fnum(directory, fname)
878 char_u *directory;
879 char_u *fname;
880{
881 if (fname == NULL || *fname == NUL) /* no file name */
882 return 0;
883 {
884#ifdef RISCOS
885 /* Name is reported as `main.c', but file is `c.main' */
886 return ro_buflist_add(fname);
887#else
888 char_u *ptr;
889 int fnum;
890
891# ifdef VMS
892 vms_remove_version(fname);
893# endif
894# ifdef BACKSLASH_IN_FILENAME
895 if (directory != NULL)
896 slash_adjust(directory);
897 slash_adjust(fname);
898# endif
899 if (directory != NULL && !vim_isAbsName(fname)
900 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
901 {
902 /*
903 * Here we check if the file really exists.
904 * This should normally be true, but if make works without
905 * "leaving directory"-messages we might have missed a
906 * directory change.
907 */
908 if (mch_getperm(ptr) < 0)
909 {
910 vim_free(ptr);
911 directory = qf_guess_filepath(fname);
912 if (directory)
913 ptr = concat_fnames(directory, fname, TRUE);
914 else
915 ptr = vim_strsave(fname);
916 }
917 /* Use concatenated directory name and file name */
918 fnum = buflist_add(ptr, 0);
919 vim_free(ptr);
920 return fnum;
921 }
922 return buflist_add(fname, 0);
923#endif
924 }
925}
926
927/*
928 * push dirbuf onto the directory stack and return pointer to actual dir or
929 * NULL on error
930 */
931 static char_u *
932qf_push_dir(dirbuf, stackptr)
933 char_u *dirbuf;
934 struct dir_stack_T **stackptr;
935{
936 struct dir_stack_T *ds_new;
937 struct dir_stack_T *ds_ptr;
938
939 /* allocate new stack element and hook it in */
940 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
941 if (ds_new == NULL)
942 return NULL;
943
944 ds_new->next = *stackptr;
945 *stackptr = ds_new;
946
947 /* store directory on the stack */
948 if (vim_isAbsName(dirbuf)
949 || (*stackptr)->next == NULL
950 || (*stackptr && dir_stack != *stackptr))
951 (*stackptr)->dirname = vim_strsave(dirbuf);
952 else
953 {
954 /* Okay we don't have an absolute path.
955 * dirbuf must be a subdir of one of the directories on the stack.
956 * Let's search...
957 */
958 ds_new = (*stackptr)->next;
959 (*stackptr)->dirname = NULL;
960 while (ds_new)
961 {
962 vim_free((*stackptr)->dirname);
963 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
964 TRUE);
965 if (mch_isdir((*stackptr)->dirname) == TRUE)
966 break;
967
968 ds_new = ds_new->next;
969 }
970
971 /* clean up all dirs we already left */
972 while ((*stackptr)->next != ds_new)
973 {
974 ds_ptr = (*stackptr)->next;
975 (*stackptr)->next = (*stackptr)->next->next;
976 vim_free(ds_ptr->dirname);
977 vim_free(ds_ptr);
978 }
979
980 /* Nothing found -> it must be on top level */
981 if (ds_new == NULL)
982 {
983 vim_free((*stackptr)->dirname);
984 (*stackptr)->dirname = vim_strsave(dirbuf);
985 }
986 }
987
988 if ((*stackptr)->dirname != NULL)
989 return (*stackptr)->dirname;
990 else
991 {
992 ds_ptr = *stackptr;
993 *stackptr = (*stackptr)->next;
994 vim_free(ds_ptr);
995 return NULL;
996 }
997}
998
999
1000/*
1001 * pop dirbuf from the directory stack and return previous directory or NULL if
1002 * stack is empty
1003 */
1004 static char_u *
1005qf_pop_dir(stackptr)
1006 struct dir_stack_T **stackptr;
1007{
1008 struct dir_stack_T *ds_ptr;
1009
1010 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1011 * What to do if it isn't? */
1012
1013 /* pop top element and free it */
1014 if (*stackptr != NULL)
1015 {
1016 ds_ptr = *stackptr;
1017 *stackptr = (*stackptr)->next;
1018 vim_free(ds_ptr->dirname);
1019 vim_free(ds_ptr);
1020 }
1021
1022 /* return NEW top element as current dir or NULL if stack is empty*/
1023 return *stackptr ? (*stackptr)->dirname : NULL;
1024}
1025
1026/*
1027 * clean up directory stack
1028 */
1029 static void
1030qf_clean_dir_stack(stackptr)
1031 struct dir_stack_T **stackptr;
1032{
1033 struct dir_stack_T *ds_ptr;
1034
1035 while ((ds_ptr = *stackptr) != NULL)
1036 {
1037 *stackptr = (*stackptr)->next;
1038 vim_free(ds_ptr->dirname);
1039 vim_free(ds_ptr);
1040 }
1041}
1042
1043/*
1044 * Check in which directory of the directory stack the given file can be
1045 * found.
1046 * Returns a pointer to the directory name or NULL if not found
1047 * Cleans up intermediate directory entries.
1048 *
1049 * TODO: How to solve the following problem?
1050 * If we have the this directory tree:
1051 * ./
1052 * ./aa
1053 * ./aa/bb
1054 * ./bb
1055 * ./bb/x.c
1056 * and make says:
1057 * making all in aa
1058 * making all in bb
1059 * x.c:9: Error
1060 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1061 * qf_guess_filepath will return NULL.
1062 */
1063 static char_u *
1064qf_guess_filepath(filename)
1065 char_u *filename;
1066{
1067 struct dir_stack_T *ds_ptr;
1068 struct dir_stack_T *ds_tmp;
1069 char_u *fullname;
1070
1071 /* no dirs on the stack - there's nothing we can do */
1072 if (dir_stack == NULL)
1073 return NULL;
1074
1075 ds_ptr = dir_stack->next;
1076 fullname = NULL;
1077 while (ds_ptr)
1078 {
1079 vim_free(fullname);
1080 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1081
1082 /* If concat_fnames failed, just go on. The worst thing that can happen
1083 * is that we delete the entire stack.
1084 */
1085 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1086 break;
1087
1088 ds_ptr = ds_ptr->next;
1089 }
1090
1091 vim_free(fullname);
1092
1093 /* clean up all dirs we already left */
1094 while (dir_stack->next != ds_ptr)
1095 {
1096 ds_tmp = dir_stack->next;
1097 dir_stack->next = dir_stack->next->next;
1098 vim_free(ds_tmp->dirname);
1099 vim_free(ds_tmp);
1100 }
1101
1102 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1103
1104}
1105
1106/*
1107 * jump to a quickfix line
1108 * if dir == FORWARD go "errornr" valid entries forward
1109 * if dir == BACKWARD go "errornr" valid entries backward
1110 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1111 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1112 * else if "errornr" is zero, redisplay the same line
1113 * else go to entry "errornr"
1114 */
1115 void
1116qf_jump(dir, errornr, forceit)
1117 int dir;
1118 int errornr;
1119 int forceit;
1120{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001121 qfline_T *qf_ptr;
1122 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 int qf_index;
1124 int old_qf_fnum;
1125 int old_qf_index;
1126 int prev_index;
1127 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1128 char_u *err = e_no_more_items;
1129 linenr_T i;
1130 buf_T *old_curbuf;
1131 linenr_T old_lnum;
1132 char_u *old_swb = p_swb;
1133 colnr_T screen_col;
1134 colnr_T char_col;
1135 char_u *line;
1136#ifdef FEAT_WINDOWS
1137 int opened_window = FALSE;
1138 win_T *win;
1139 win_T *altwin;
1140#endif
1141 int print_message = TRUE;
1142 int len;
1143#ifdef FEAT_FOLDING
1144 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1145#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001146 int ok = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147
1148 if (qf_curlist >= qf_listcount || qf_lists[qf_curlist].qf_count == 0)
1149 {
1150 EMSG(_(e_quickfix));
1151 return;
1152 }
1153
1154 qf_ptr = qf_lists[qf_curlist].qf_ptr;
1155 old_qf_ptr = qf_ptr;
1156 qf_index = qf_lists[qf_curlist].qf_index;
1157 old_qf_index = qf_index;
1158 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1159 {
1160 while (errornr--)
1161 {
1162 old_qf_ptr = qf_ptr;
1163 prev_index = qf_index;
1164 old_qf_fnum = qf_ptr->qf_fnum;
1165 do
1166 {
1167 if (qf_index == qf_lists[qf_curlist].qf_count
1168 || qf_ptr->qf_next == NULL)
1169 {
1170 qf_ptr = old_qf_ptr;
1171 qf_index = prev_index;
1172 if (err != NULL)
1173 {
1174 EMSG(_(err));
1175 goto theend;
1176 }
1177 errornr = 0;
1178 break;
1179 }
1180 ++qf_index;
1181 qf_ptr = qf_ptr->qf_next;
1182 } while ((!qf_lists[qf_curlist].qf_nonevalid && !qf_ptr->qf_valid)
1183 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1184 err = NULL;
1185 }
1186 }
1187 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1188 {
1189 while (errornr--)
1190 {
1191 old_qf_ptr = qf_ptr;
1192 prev_index = qf_index;
1193 old_qf_fnum = qf_ptr->qf_fnum;
1194 do
1195 {
1196 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1197 {
1198 qf_ptr = old_qf_ptr;
1199 qf_index = prev_index;
1200 if (err != NULL)
1201 {
1202 EMSG(_(err));
1203 goto theend;
1204 }
1205 errornr = 0;
1206 break;
1207 }
1208 --qf_index;
1209 qf_ptr = qf_ptr->qf_prev;
1210 } while ((!qf_lists[qf_curlist].qf_nonevalid && !qf_ptr->qf_valid)
1211 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1212 err = NULL;
1213 }
1214 }
1215 else if (errornr != 0) /* go to specified number */
1216 {
1217 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1218 {
1219 --qf_index;
1220 qf_ptr = qf_ptr->qf_prev;
1221 }
1222 while (errornr > qf_index && qf_index < qf_lists[qf_curlist].qf_count
1223 && qf_ptr->qf_next != NULL)
1224 {
1225 ++qf_index;
1226 qf_ptr = qf_ptr->qf_next;
1227 }
1228 }
1229
1230#ifdef FEAT_WINDOWS
1231 qf_lists[qf_curlist].qf_index = qf_index;
1232 if (qf_win_pos_update(old_qf_index))
1233 /* No need to print the error message if it's visible in the error
1234 * window */
1235 print_message = FALSE;
1236
1237 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001238 * For ":helpgrep" find a help window or open one.
1239 */
1240 if (qf_ptr->qf_type == 1 && !curwin->w_buffer->b_help)
1241 {
1242 win_T *wp;
1243 int n;
1244
1245 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1246 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1247 break;
1248 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1249 win_enter(wp, TRUE);
1250 else
1251 {
1252 /*
1253 * Split off help window; put it at far top if no position
1254 * specified, the current window is vertically split and narrow.
1255 */
1256 n = WSP_HELP;
1257# ifdef FEAT_VERTSPLIT
1258 if (cmdmod.split == 0 && curwin->w_width != Columns
1259 && curwin->w_width < 80)
1260 n |= WSP_TOP;
1261# endif
1262 if (win_split(0, n) == FAIL)
1263 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001264 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001265
1266 if (curwin->w_height < p_hh)
1267 win_setheight((int)p_hh);
1268 }
1269
1270 if (!p_im)
1271 restart_edit = 0; /* don't want insert mode in help file */
1272 }
1273
1274 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 * If currently in the quickfix window, find another window to show the
1276 * file in.
1277 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001278 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 {
1280 /*
1281 * If there is no file specified, we don't know where to go.
1282 * But do advance, otherwise ":cn" gets stuck.
1283 */
1284 if (qf_ptr->qf_fnum == 0)
1285 goto theend;
1286
1287 /*
1288 * If there is only one window, create a new one above the quickfix
1289 * window.
1290 */
1291 if (firstwin == lastwin)
1292 {
1293 if (win_split(0, WSP_ABOVE) == FAIL)
1294 goto failed; /* not enough room for window */
1295 opened_window = TRUE; /* close it when fail */
1296 p_swb = empty_option; /* don't split again */
1297# ifdef FEAT_SCROLLBIND
1298 curwin->w_p_scb = FALSE;
1299# endif
1300 }
1301 else
1302 {
1303 /*
1304 * Try to find a window that shows the right buffer.
1305 * Default to the window just above the quickfix buffer.
1306 */
1307 win = curwin;
1308 altwin = NULL;
1309 for (;;)
1310 {
1311 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1312 break;
1313 if (win->w_prev == NULL)
1314 win = lastwin; /* wrap around the top */
1315 else
1316 win = win->w_prev; /* go to previous window */
1317
1318 if (bt_quickfix(win->w_buffer))
1319 {
1320 /* Didn't find it, go to the window before the quickfix
1321 * window. */
1322 if (altwin != NULL)
1323 win = altwin;
1324 else if (curwin->w_prev != NULL)
1325 win = curwin->w_prev;
1326 else
1327 win = curwin->w_next;
1328 break;
1329 }
1330
1331 /* Remember a usable window. */
1332 if (altwin == NULL && !win->w_p_pvw
1333 && win->w_buffer->b_p_bt[0] == NUL)
1334 altwin = win;
1335 }
1336
1337 win_goto(win);
1338 }
1339 }
1340#endif
1341
1342 /*
1343 * If there is a file name,
1344 * read the wanted file if needed, and check autowrite etc.
1345 */
1346 old_curbuf = curbuf;
1347 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001348
1349 if (qf_ptr->qf_fnum != 0)
1350 {
1351 if (qf_ptr->qf_type == 1)
1352 {
1353 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1354 * set b_p_ro flag). */
1355 if (!can_abandon(curbuf, forceit))
1356 {
1357 EMSG(_(e_nowrtmsg));
1358 ok = FALSE;
1359 }
1360 else
1361 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
1362 ECMD_HIDE + ECMD_SET_HELP);
1363 }
1364 else
1365 ok = buflist_getfile(qf_ptr->qf_fnum,
1366 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
1367 }
1368
1369 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 {
1371 /* When not switched to another buffer, still need to set pc mark */
1372 if (curbuf == old_curbuf)
1373 setpcmark();
1374
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001375 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001377 /*
1378 * Go to line with error, unless qf_lnum is 0.
1379 */
1380 i = qf_ptr->qf_lnum;
1381 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001383 if (i > curbuf->b_ml.ml_line_count)
1384 i = curbuf->b_ml.ml_line_count;
1385 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001387 if (qf_ptr->qf_col > 0)
1388 {
1389 curwin->w_cursor.col = qf_ptr->qf_col - 1;
1390 if (qf_ptr->qf_viscol == TRUE)
1391 {
1392 /*
1393 * Check each character from the beginning of the error
1394 * line up to the error column. For each tab character
1395 * found, reduce the error column value by the length of
1396 * a tab character.
1397 */
1398 line = ml_get_curline();
1399 screen_col = 0;
1400 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1401 {
1402 if (*line == NUL)
1403 break;
1404 if (*line++ == '\t')
1405 {
1406 curwin->w_cursor.col -= 7 - (screen_col % 8);
1407 screen_col += 8 - (screen_col % 8);
1408 }
1409 else
1410 ++screen_col;
1411 }
1412 }
1413 check_cursor();
1414 }
1415 else
1416 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 }
1418 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001419 {
1420 pos_T save_cursor;
1421
1422 /* Move the cursor to the first line in the buffer */
1423 save_cursor = curwin->w_cursor;
1424 curwin->w_cursor.lnum = 0;
1425 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1, SEARCH_KEEP))
1426 curwin->w_cursor = save_cursor;
1427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428
1429#ifdef FEAT_FOLDING
1430 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1431 foldOpenCursor();
1432#endif
1433 if (print_message)
1434 {
1435 /* Update the screen before showing the message */
1436 update_topline_redraw();
1437 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
1438 qf_lists[qf_curlist].qf_count,
1439 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1440 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1441 /* Add the message, skipping leading whitespace and newlines. */
1442 len = (int)STRLEN(IObuff);
1443 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1444
1445 /* Output the message. Overwrite to avoid scrolling when the 'O'
1446 * flag is present in 'shortmess'; But when not jumping, print the
1447 * whole message. */
1448 i = msg_scroll;
1449 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1450 msg_scroll = TRUE;
1451 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1452 msg_scroll = FALSE;
1453 msg_attr_keep(IObuff, 0, TRUE);
1454 msg_scroll = i;
1455 }
1456 }
1457 else
1458 {
1459#ifdef FEAT_WINDOWS
1460 if (opened_window)
1461 win_close(curwin, TRUE); /* Close opened window */
1462#endif
1463 if (qf_ptr->qf_fnum != 0)
1464 {
1465 /*
1466 * Couldn't open file, so put index back where it was. This could
1467 * happen if the file was readonly and we changed something.
1468 */
1469#ifdef FEAT_WINDOWS
1470failed:
1471#endif
1472 qf_ptr = old_qf_ptr;
1473 qf_index = old_qf_index;
1474 }
1475 }
1476theend:
1477 qf_lists[qf_curlist].qf_ptr = qf_ptr;
1478 qf_lists[qf_curlist].qf_index = qf_index;
1479#ifdef FEAT_WINDOWS
1480 if (p_swb != old_swb && opened_window)
1481 {
1482 /* Restore old 'switchbuf' value, but not when an autocommand or
1483 * modeline has changed the value. */
1484 if (p_swb == empty_option)
1485 p_swb = old_swb;
1486 else
1487 free_string_option(old_swb);
1488 }
1489#endif
1490}
1491
1492/*
1493 * ":clist": list all errors
1494 */
1495 void
1496qf_list(eap)
1497 exarg_T *eap;
1498{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001499 buf_T *buf;
1500 char_u *fname;
1501 qfline_T *qfp;
1502 int i;
1503 int idx1 = 1;
1504 int idx2 = -1;
1505 int need_return = TRUE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001506 char_u *arg = eap->arg;
1507 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 recognised errors */
1509
1510 if (qf_curlist >= qf_listcount || qf_lists[qf_curlist].qf_count == 0)
1511 {
1512 EMSG(_(e_quickfix));
1513 return;
1514 }
1515 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
1516 {
1517 EMSG(_(e_trailing));
1518 return;
1519 }
1520 i = qf_lists[qf_curlist].qf_count;
1521 if (idx1 < 0)
1522 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
1523 if (idx2 < 0)
1524 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
1525
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 if (qf_lists[qf_curlist].qf_nonevalid)
1527 all = TRUE;
1528 qfp = qf_lists[qf_curlist].qf_start;
1529 for (i = 1; !got_int && i <= qf_lists[qf_curlist].qf_count; )
1530 {
1531 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
1532 {
1533 if (need_return)
1534 {
1535 msg_putchar('\n');
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001536 if (got_int)
1537 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 need_return = FALSE;
1539 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001540
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001541 fname = NULL;
1542 if (qfp->qf_fnum != 0
1543 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
1544 {
1545 fname = buf->b_fname;
1546 if (qfp->qf_type == 1) /* :helpgrep */
1547 fname = gettail(fname);
1548 }
1549 if (fname == NULL)
1550 sprintf((char *)IObuff, "%2d", i);
1551 else
1552 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
1553 i, (char *)fname);
1554 msg_outtrans_attr(IObuff, i == qf_lists[qf_curlist].qf_index
1555 ? hl_attr(HLF_L) : hl_attr(HLF_D));
1556 if (qfp->qf_lnum == 0)
1557 IObuff[0] = NUL;
1558 else if (qfp->qf_col == 0)
1559 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
1560 else
1561 sprintf((char *)IObuff, ":%ld col %d",
1562 qfp->qf_lnum, qfp->qf_col);
1563 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
1564 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
1565 msg_puts_attr(IObuff, hl_attr(HLF_N));
1566 if (qfp->qf_pattern != NULL)
1567 {
1568 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
1569 STRCAT(IObuff, ":");
1570 msg_puts(IObuff);
1571 }
1572 msg_puts((char_u *)" ");
1573
1574 /* Remove newlines and leading whitespace from the text. For an
1575 * unrecognized line keep the indent, the compiler may mark a word
1576 * with ^^^^. */
1577 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 ? skipwhite(qfp->qf_text) : qfp->qf_text,
1579 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001580 msg_prt_line(IObuff, FALSE);
1581 out_flush(); /* show one line at a time */
1582 need_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001584
1585 qfp = qfp->qf_next;
1586 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 ui_breakcheck();
1588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589}
1590
1591/*
1592 * Remove newlines and leading whitespace from an error message.
1593 * Put the result in "buf[bufsize]".
1594 */
1595 static void
1596qf_fmt_text(text, buf, bufsize)
1597 char_u *text;
1598 char_u *buf;
1599 int bufsize;
1600{
1601 int i;
1602 char_u *p = text;
1603
1604 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
1605 {
1606 if (*p == '\n')
1607 {
1608 buf[i] = ' ';
1609 while (*++p != NUL)
1610 if (!vim_iswhite(*p) && *p != '\n')
1611 break;
1612 }
1613 else
1614 buf[i] = *p++;
1615 }
1616 buf[i] = NUL;
1617}
1618
1619/*
1620 * ":colder [count]": Up in the quickfix stack.
1621 * ":cnewer [count]": Down in the quickfix stack.
1622 */
1623 void
1624qf_age(eap)
1625 exarg_T *eap;
1626{
1627 int count;
1628
1629 if (eap->addr_count != 0)
1630 count = eap->line2;
1631 else
1632 count = 1;
1633 while (count--)
1634 {
1635 if (eap->cmdidx == CMD_colder)
1636 {
1637 if (qf_curlist == 0)
1638 {
1639 EMSG(_("E380: At bottom of quickfix stack"));
1640 return;
1641 }
1642 --qf_curlist;
1643 }
1644 else
1645 {
1646 if (qf_curlist >= qf_listcount - 1)
1647 {
1648 EMSG(_("E381: At top of quickfix stack"));
1649 return;
1650 }
1651 ++qf_curlist;
1652 }
1653 }
1654 qf_msg();
1655}
1656
1657 static void
1658qf_msg()
1659{
1660 smsg((char_u *)_("error list %d of %d; %d errors"),
1661 qf_curlist + 1, qf_listcount, qf_lists[qf_curlist].qf_count);
1662#ifdef FEAT_WINDOWS
1663 qf_update_buffer();
1664#endif
1665}
1666
1667/*
1668 * free the error list
1669 */
1670 static void
1671qf_free(idx)
1672 int idx;
1673{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001674 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675
1676 while (qf_lists[idx].qf_count)
1677 {
1678 qfp = qf_lists[idx].qf_start->qf_next;
1679 vim_free(qf_lists[idx].qf_start->qf_text);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001680 vim_free(qf_lists[idx].qf_start->qf_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 vim_free(qf_lists[idx].qf_start);
1682 qf_lists[idx].qf_start = qfp;
1683 --qf_lists[idx].qf_count;
1684 }
1685}
1686
1687/*
1688 * qf_mark_adjust: adjust marks
1689 */
1690 void
1691qf_mark_adjust(line1, line2, amount, amount_after)
1692 linenr_T line1;
1693 linenr_T line2;
1694 long amount;
1695 long amount_after;
1696{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001697 int i;
1698 qfline_T *qfp;
1699 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700
1701 for (idx = 0; idx < qf_listcount; ++idx)
1702 if (qf_lists[idx].qf_count)
1703 for (i = 0, qfp = qf_lists[idx].qf_start;
1704 i < qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
1705 if (qfp->qf_fnum == curbuf->b_fnum)
1706 {
1707 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
1708 {
1709 if (amount == MAXLNUM)
1710 qfp->qf_cleared = TRUE;
1711 else
1712 qfp->qf_lnum += amount;
1713 }
1714 else if (amount_after && qfp->qf_lnum > line2)
1715 qfp->qf_lnum += amount_after;
1716 }
1717}
1718
1719/*
1720 * Make a nice message out of the error character and the error number:
1721 * char number message
1722 * e or E 0 " error"
1723 * w or W 0 " warning"
1724 * i or I 0 " info"
1725 * 0 0 ""
1726 * other 0 " c"
1727 * e or E n " error n"
1728 * w or W n " warning n"
1729 * i or I n " info n"
1730 * 0 n " error n"
1731 * other n " c n"
1732 * 1 x "" :helpgrep
1733 */
1734 static char_u *
1735qf_types(c, nr)
1736 int c, nr;
1737{
1738 static char_u buf[20];
1739 static char_u cc[3];
1740 char_u *p;
1741
1742 if (c == 'W' || c == 'w')
1743 p = (char_u *)" warning";
1744 else if (c == 'I' || c == 'i')
1745 p = (char_u *)" info";
1746 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
1747 p = (char_u *)" error";
1748 else if (c == 0 || c == 1)
1749 p = (char_u *)"";
1750 else
1751 {
1752 cc[0] = ' ';
1753 cc[1] = c;
1754 cc[2] = NUL;
1755 p = cc;
1756 }
1757
1758 if (nr <= 0)
1759 return p;
1760
1761 sprintf((char *)buf, "%s %3d", (char *)p, nr);
1762 return buf;
1763}
1764
1765#if defined(FEAT_WINDOWS) || defined(PROTO)
1766/*
1767 * ":cwindow": open the quickfix window if we have errors to display,
1768 * close it if not.
1769 */
1770 void
1771ex_cwindow(eap)
1772 exarg_T *eap;
1773{
1774 win_T *win;
1775
1776 /*
1777 * Look for an existing quickfix window.
1778 */
1779 for (win = firstwin; win != NULL; win = win->w_next)
1780 if (bt_quickfix(win->w_buffer))
1781 break;
1782
1783 /*
1784 * If a quickfix window is open but we have no errors to display,
1785 * close the window. If a quickfix window is not open, then open
1786 * it if we have errors; otherwise, leave it closed.
1787 */
1788 if (qf_lists[qf_curlist].qf_nonevalid || qf_curlist >= qf_listcount)
1789 {
1790 if (win != NULL)
1791 ex_cclose(eap);
1792 }
1793 else if (win == NULL)
1794 ex_copen(eap);
1795}
1796
1797/*
1798 * ":cclose": close the window showing the list of errors.
1799 */
1800/*ARGSUSED*/
1801 void
1802ex_cclose(eap)
1803 exarg_T *eap;
1804{
1805 win_T *win;
1806
1807 /*
1808 * Find existing quickfix window and close it.
1809 */
1810 for (win = firstwin; win != NULL; win = win->w_next)
1811 if (bt_quickfix(win->w_buffer))
1812 break;
1813
1814 if (win != NULL)
1815 win_close(win, FALSE);
1816}
1817
1818/*
1819 * ":copen": open a window that shows the list of errors.
1820 */
1821 void
1822ex_copen(eap)
1823 exarg_T *eap;
1824{
1825 int height;
1826 buf_T *buf;
1827 win_T *win;
1828
1829 if (eap->addr_count != 0)
1830 height = eap->line2;
1831 else
1832 height = QF_WINHEIGHT;
1833
1834#ifdef FEAT_VISUAL
1835 reset_VIsual_and_resel(); /* stop Visual mode */
1836#endif
1837#ifdef FEAT_GUI
1838 need_mouse_correct = TRUE;
1839#endif
1840
1841 /*
1842 * Find existing quickfix window, or open a new one.
1843 */
1844 for (win = firstwin; win != NULL; win = win->w_next)
1845 if (bt_quickfix(win->w_buffer))
1846 break;
1847 if (win != NULL)
1848 win_goto(win);
1849 else
1850 {
1851 /* The current window becomes the previous window afterwards. */
1852 win = curwin;
1853
1854 /* Create the new window at the very bottom. */
1855 win_goto(lastwin);
1856 if (win_split(height, WSP_BELOW) == FAIL)
1857 return; /* not enough room for window */
1858#ifdef FEAT_SCROLLBIND
1859 curwin->w_p_scb = FALSE;
1860#endif
1861
1862 /*
1863 * Find existing quickfix buffer, or create a new one.
1864 */
1865 buf = qf_find_buf();
1866 if (buf == NULL)
1867 {
1868 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
1869 /* switch off 'swapfile' */
1870 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
1871 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
1872 OPT_LOCAL);
1873 set_option_value((char_u *)"bh", 0L, (char_u *)"delete", OPT_LOCAL);
1874 set_option_value((char_u *)"diff", 0L, (char_u *)"", OPT_LOCAL);
1875 }
1876 else if (buf != curbuf)
1877 set_curbuf(buf, DOBUF_GOTO);
1878
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00001879#ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 /* Only set the height when there is no window to the side. */
1881 if (curwin->w_width == Columns)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00001882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 win_setheight(height);
1884 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
1885 if (win_valid(win))
1886 prevwin = win;
1887 }
1888
1889 /*
1890 * Fill the buffer with the quickfix list.
1891 */
1892 qf_fill_buffer();
1893
1894 curwin->w_cursor.lnum = qf_lists[qf_curlist].qf_index;
1895 curwin->w_cursor.col = 0;
1896 check_cursor();
1897 update_topline(); /* scroll to show the line */
1898}
1899
1900/*
1901 * Return the number of the current entry (line number in the quickfix
1902 * window).
1903 */
1904 linenr_T
1905qf_current_entry()
1906{
1907 return qf_lists[qf_curlist].qf_index;
1908}
1909
1910/*
1911 * Update the cursor position in the quickfix window to the current error.
1912 * Return TRUE if there is a quickfix window.
1913 */
1914 static int
1915qf_win_pos_update(old_qf_index)
1916 int old_qf_index; /* previous qf_index or zero */
1917{
1918 win_T *win;
1919 int qf_index = qf_lists[qf_curlist].qf_index;
1920
1921 /*
1922 * Put the cursor on the current error in the quickfix window, so that
1923 * it's viewable.
1924 */
1925 for (win = firstwin; win != NULL; win = win->w_next)
1926 if (bt_quickfix(win->w_buffer))
1927 break;
1928 if (win != NULL
1929 && qf_index <= win->w_buffer->b_ml.ml_line_count
1930 && old_qf_index != qf_index)
1931 {
1932 win_T *old_curwin = curwin;
1933
1934 curwin = win;
1935 curbuf = win->w_buffer;
1936 if (qf_index > old_qf_index)
1937 {
1938 curwin->w_redraw_top = old_qf_index;
1939 curwin->w_redraw_bot = qf_index;
1940 }
1941 else
1942 {
1943 curwin->w_redraw_top = qf_index;
1944 curwin->w_redraw_bot = old_qf_index;
1945 }
1946 curwin->w_cursor.lnum = qf_index;
1947 curwin->w_cursor.col = 0;
1948 update_topline(); /* scroll to show the line */
1949 redraw_later(VALID);
1950 curwin->w_redr_status = TRUE; /* update ruler */
1951 curwin = old_curwin;
1952 curbuf = curwin->w_buffer;
1953 }
1954 return win != NULL;
1955}
1956
1957/*
1958 * Find quickfix buffer.
1959 */
1960 static buf_T *
1961qf_find_buf()
1962{
1963 buf_T *buf;
1964
1965 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1966 if (bt_quickfix(buf))
1967 break;
1968 return buf;
1969}
1970
1971/*
1972 * Find the quickfix buffer. If it exists, update the contents.
1973 */
1974 static void
1975qf_update_buffer()
1976{
1977 buf_T *buf;
1978#ifdef FEAT_AUTOCMD
1979 aco_save_T aco;
1980#else
1981 buf_T *save_curbuf;
1982#endif
1983
1984 /* Check if a buffer for the quickfix list exists. Update it. */
1985 buf = qf_find_buf();
1986 if (buf != NULL)
1987 {
1988#ifdef FEAT_AUTOCMD
1989 /* set curwin/curbuf to buf and save a few things */
1990 aucmd_prepbuf(&aco, buf);
1991#else
1992 save_curbuf = curbuf;
1993 curbuf = buf;
1994#endif
1995
1996 qf_fill_buffer();
1997
1998#ifdef FEAT_AUTOCMD
1999 /* restore curwin/curbuf and a few other things */
2000 aucmd_restbuf(&aco);
2001#else
2002 curbuf = save_curbuf;
2003#endif
2004
2005 (void)qf_win_pos_update(0);
2006 }
2007}
2008
2009/*
2010 * Fill current buffer with quickfix errors, replacing any previous contents.
2011 * curbuf must be the quickfix buffer!
2012 */
2013 static void
2014qf_fill_buffer()
2015{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002016 linenr_T lnum;
2017 qfline_T *qfp;
2018 buf_T *errbuf;
2019 int len;
2020 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021
2022 /* delete all existing lines */
2023 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2024 (void)ml_delete((linenr_T)1, FALSE);
2025
2026 /* Check if there is anything to display */
2027 if (qf_curlist < qf_listcount)
2028 {
2029 /* Add one line for each error */
2030 qfp = qf_lists[qf_curlist].qf_start;
2031 for (lnum = 0; lnum < qf_lists[qf_curlist].qf_count; ++lnum)
2032 {
2033 if (qfp->qf_fnum != 0
2034 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2035 && errbuf->b_fname != NULL)
2036 {
2037 if (qfp->qf_type == 1) /* :helpgrep */
2038 STRCPY(IObuff, gettail(errbuf->b_fname));
2039 else
2040 STRCPY(IObuff, errbuf->b_fname);
2041 len = (int)STRLEN(IObuff);
2042 }
2043 else
2044 len = 0;
2045 IObuff[len++] = '|';
2046
2047 if (qfp->qf_lnum > 0)
2048 {
2049 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2050 len += (int)STRLEN(IObuff + len);
2051
2052 if (qfp->qf_col > 0)
2053 {
2054 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2055 len += (int)STRLEN(IObuff + len);
2056 }
2057
2058 sprintf((char *)IObuff + len, "%s",
2059 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2060 len += (int)STRLEN(IObuff + len);
2061 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002062 else if (qfp->qf_pattern != NULL)
2063 {
2064 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2065 len += (int)STRLEN(IObuff + len);
2066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 IObuff[len++] = '|';
2068 IObuff[len++] = ' ';
2069
2070 /* Remove newlines and leading whitespace from the text.
2071 * For an unrecognized line keep the indent, the compiler may
2072 * mark a word with ^^^^. */
2073 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2074 IObuff + len, IOSIZE - len);
2075
2076 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2077 == FAIL)
2078 break;
2079 qfp = qfp->qf_next;
2080 }
2081 /* Delete the empty line which is now at the end */
2082 (void)ml_delete(lnum + 1, FALSE);
2083 }
2084
2085 /* correct cursor position */
2086 check_lnums(TRUE);
2087
2088 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2089 * resembles reading a file into a buffer, it's more logical when using
2090 * autocommands. */
2091 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2092 curbuf->b_p_ma = FALSE;
2093
2094#ifdef FEAT_AUTOCMD
2095 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2096 FALSE, curbuf);
2097 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2098 FALSE, curbuf);
2099#endif
2100
2101 /* make sure it will be redrawn */
2102 redraw_curbuf_later(NOT_VALID);
2103
2104 /* Restore KeyTyped, setting 'filetype' may reset it. */
2105 KeyTyped = old_KeyTyped;
2106}
2107
2108#endif /* FEAT_WINDOWS */
2109
2110/*
2111 * Return TRUE if "buf" is the quickfix buffer.
2112 */
2113 int
2114bt_quickfix(buf)
2115 buf_T *buf;
2116{
2117 return (buf->b_p_bt[0] == 'q');
2118}
2119
2120/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002121 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2122 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 */
2124 int
2125bt_nofile(buf)
2126 buf_T *buf;
2127{
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002128 return (buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2129 || buf->b_p_bt[0] == 'a';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130}
2131
2132/*
2133 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2134 */
2135 int
2136bt_dontwrite(buf)
2137 buf_T *buf;
2138{
2139 return (buf->b_p_bt[0] == 'n');
2140}
2141
2142 int
2143bt_dontwrite_msg(buf)
2144 buf_T *buf;
2145{
2146 if (bt_dontwrite(buf))
2147 {
2148 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2149 return TRUE;
2150 }
2151 return FALSE;
2152}
2153
2154/*
2155 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2156 * and 'bufhidden'.
2157 */
2158 int
2159buf_hide(buf)
2160 buf_T *buf;
2161{
2162 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2163 switch (buf->b_p_bh[0])
2164 {
2165 case 'u': /* "unload" */
2166 case 'w': /* "wipe" */
2167 case 'd': return FALSE; /* "delete" */
2168 case 'h': return TRUE; /* "hide" */
2169 }
2170 return (p_hid || cmdmod.hide);
2171}
2172
2173/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002174 * Return TRUE when using ":vimgrep" for ":grep".
2175 */
2176 int
Bram Moolenaar81695252004-12-29 20:58:21 +00002177grep_internal(cmdidx)
2178 cmdidx_T cmdidx;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002179{
Bram Moolenaar81695252004-12-29 20:58:21 +00002180 return ((cmdidx == CMD_grep || cmdidx == CMD_grepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002181 && STRCMP("internal",
2182 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2183}
2184
2185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 * Used for ":make", ":grep" and ":grepadd".
2187 */
2188 void
2189ex_make(eap)
2190 exarg_T *eap;
2191{
Bram Moolenaar7c626922005-02-07 22:01:03 +00002192 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 char_u *cmd;
2194 unsigned len;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002195#ifdef FEAT_AUTOCMD
2196 char_u *au_name = NULL;
2197
2198 switch (eap->cmdidx)
2199 {
2200 case CMD_make: au_name = (char_u *)"make"; break;
2201 case CMD_grep: au_name = (char_u *)"grep"; break;
2202 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
2203 default: break;
2204 }
2205 if (au_name != NULL)
2206 {
2207 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2208 curbuf->b_fname, TRUE, curbuf);
2209 if (did_throw || force_abort)
2210 return;
2211 }
2212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213
Bram Moolenaar86b68352004-12-27 21:59:20 +00002214 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
Bram Moolenaar81695252004-12-29 20:58:21 +00002215 if (grep_internal(eap->cmdidx))
Bram Moolenaar86b68352004-12-27 21:59:20 +00002216 {
2217 ex_vimgrep(eap);
2218 return;
2219 }
2220
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002222 fname = get_mef_name();
2223 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002225 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226
2227 /*
2228 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2229 */
2230 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2231 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002232 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 cmd = alloc(len);
2234 if (cmd == NULL)
2235 return;
2236 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2237 (char *)p_shq);
2238 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002239 append_redir(cmd, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 /*
2241 * Output a newline if there's something else than the :make command that
2242 * was typed (in which case the cursor is in column 0).
2243 */
2244 if (msg_col == 0)
2245 msg_didout = FALSE;
2246 msg_start();
2247 MSG_PUTS(":!");
2248 msg_outtrans(cmd); /* show what we are doing */
2249
2250 /* let the shell know if we are redirecting output or not */
2251 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2252
2253#ifdef AMIGA
2254 out_flush();
2255 /* read window status report and redraw before message */
2256 (void)char_avail();
2257#endif
2258
Bram Moolenaar7c626922005-02-07 22:01:03 +00002259 if (qf_init(fname, eap->cmdidx != CMD_make ? p_gefm : p_efm,
Bram Moolenaar86b68352004-12-27 21:59:20 +00002260 eap->cmdidx != CMD_grepadd) > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 && !eap->forceit)
2262 qf_jump(0, 0, FALSE); /* display first error */
2263
Bram Moolenaar7c626922005-02-07 22:01:03 +00002264 mch_remove(fname);
2265 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 vim_free(cmd);
Bram Moolenaar7c626922005-02-07 22:01:03 +00002267
2268#ifdef FEAT_AUTOCMD
2269 if (au_name != NULL)
2270 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2271 curbuf->b_fname, TRUE, curbuf);
2272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273}
2274
2275/*
2276 * Return the name for the errorfile, in allocated memory.
2277 * Find a new unique name when 'makeef' contains "##".
2278 * Returns NULL for error.
2279 */
2280 static char_u *
2281get_mef_name()
2282{
2283 char_u *p;
2284 char_u *name;
2285 static int start = -1;
2286 static int off = 0;
2287#ifdef HAVE_LSTAT
2288 struct stat sb;
2289#endif
2290
2291 if (*p_mef == NUL)
2292 {
2293 name = vim_tempname('e');
2294 if (name == NULL)
2295 EMSG(_(e_notmp));
2296 return name;
2297 }
2298
2299 for (p = p_mef; *p; ++p)
2300 if (p[0] == '#' && p[1] == '#')
2301 break;
2302
2303 if (*p == NUL)
2304 return vim_strsave(p_mef);
2305
2306 /* Keep trying until the name doesn't exist yet. */
2307 for (;;)
2308 {
2309 if (start == -1)
2310 start = mch_get_pid();
2311 else
2312 off += 19;
2313
2314 name = alloc((unsigned)STRLEN(p_mef) + 30);
2315 if (name == NULL)
2316 break;
2317 STRCPY(name, p_mef);
2318 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2319 STRCAT(name, p + 2);
2320 if (mch_getperm(name) < 0
2321#ifdef HAVE_LSTAT
2322 /* Don't accept a symbolic link, its a security risk. */
2323 && mch_lstat((char *)name, &sb) < 0
2324#endif
2325 )
2326 break;
2327 vim_free(name);
2328 }
2329 return name;
2330}
2331
2332/*
2333 * ":cc", ":crewind", ":cfirst" and ":clast".
2334 */
2335 void
2336ex_cc(eap)
2337 exarg_T *eap;
2338{
2339 qf_jump(0,
2340 eap->addr_count > 0
2341 ? (int)eap->line2
2342 : eap->cmdidx == CMD_cc
2343 ? 0
2344 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_cfirst)
2345 ? 1
2346 : 32767,
2347 eap->forceit);
2348}
2349
2350/*
2351 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
2352 */
2353 void
2354ex_cnext(eap)
2355 exarg_T *eap;
2356{
2357 qf_jump(eap->cmdidx == CMD_cnext
2358 ? FORWARD
2359 : eap->cmdidx == CMD_cnfile
2360 ? FORWARD_FILE
2361 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_cNfile)
2362 ? BACKWARD_FILE
2363 : BACKWARD,
2364 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
2365}
2366
2367/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002368 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 */
2370 void
2371ex_cfile(eap)
2372 exarg_T *eap;
2373{
2374 if (*eap->arg != NUL)
2375 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002376
2377 /*
2378 * This function is used by the :cfile, :cgetfile and :caddfile
2379 * commands.
2380 * :cfile always creates a new quickfix list and jumps to the
2381 * first error.
2382 * :cgetfile creates a new quickfix list but doesn't jump to the
2383 * first error.
2384 * :caddfile adds to an existing quickfix list. If there is no
2385 * quickfix list then a new list is created.
2386 */
2387 if (qf_init(p_ef, p_efm, eap->cmdidx != CMD_caddfile) > 0
2388 && eap->cmdidx == CMD_cfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 qf_jump(0, 0, eap->forceit); /* display first error */
2390}
2391
2392/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002393 * ":vimgrep {pattern} file(s)"
2394 */
2395 void
2396ex_vimgrep(eap)
2397 exarg_T *eap;
2398{
Bram Moolenaar81695252004-12-29 20:58:21 +00002399 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002400 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002401 char_u **fnames;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002402 char_u *s;
2403 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002404 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002405 qfline_T *prevp = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002406 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00002407 buf_T *buf;
2408 int duplicate_name = FALSE;
2409 int using_dummy;
2410 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002411 buf_T *first_match_buf = NULL;
2412 time_t seconds = 0;
2413#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2414 char_u *save_ei = NULL;
2415 aco_save_T aco;
2416#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002417#ifdef FEAT_AUTOCMD
2418 char_u *au_name = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002419 int flags = 0;
2420 colnr_T col;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002421
2422 switch (eap->cmdidx)
2423 {
2424 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
2425 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
2426 default: break;
2427 }
2428 if (au_name != NULL)
2429 {
2430 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2431 curbuf->b_fname, TRUE, curbuf);
2432 if (did_throw || force_abort)
2433 return;
2434 }
2435#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00002436
Bram Moolenaar81695252004-12-29 20:58:21 +00002437 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00002438 regmatch.regprog = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002439 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00002440 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002441 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00002442 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00002443 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00002444 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002445 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002446 if (regmatch.regprog == NULL)
2447 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002448 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00002449 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002450
2451 p = skipwhite(p);
2452 if (*p == NUL)
2453 {
2454 EMSG(_("E683: File name missing or invalid pattern"));
2455 goto theend;
2456 }
2457
2458 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_vimgrepadd)
2459 || qf_curlist == qf_listcount)
2460 /* make place for a new list */
2461 qf_new_list();
2462 else if (qf_lists[qf_curlist].qf_count > 0)
2463 /* Adding to existing list, find last entry. */
2464 for (prevp = qf_lists[qf_curlist].qf_start;
2465 prevp->qf_next != prevp; prevp = prevp->qf_next)
2466 ;
2467
2468 /* parse the list of arguments */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002469 if (get_arglist_exp(p, &fcount, &fnames) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002470 goto theend;
2471 if (fcount == 0)
2472 {
2473 EMSG(_(e_nomatch));
2474 goto theend;
2475 }
2476
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002477 seconds = (time_t)0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002478 for (fi = 0; fi < fcount && !got_int; ++fi)
2479 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002480 if (time(NULL) > seconds)
2481 {
2482 /* Display the file name every second or so. */
2483 seconds = time(NULL);
2484 msg_start();
2485 p = msg_strtrunc(fnames[fi]);
2486 if (p == NULL)
2487 msg_outtrans(fnames[fi]);
2488 else
2489 {
2490 msg_outtrans(p);
2491 vim_free(p);
2492 }
2493 msg_clr_eos();
2494 msg_didout = FALSE; /* overwrite this message */
2495 msg_nowait = TRUE; /* don't wait for this message */
2496 msg_col = 0;
2497 out_flush();
2498 }
2499
Bram Moolenaar81695252004-12-29 20:58:21 +00002500 buf = buflist_findname_exp(fnames[fi]);
2501 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
2502 {
2503 /* Remember that a buffer with this name already exists. */
2504 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002505 using_dummy = TRUE;
2506
2507#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2508 /* Don't do Filetype autocommands to avoid loading syntax and
2509 * indent scripts, a great speed improvement. */
2510 save_ei = au_event_disable(",Filetype");
2511#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00002512
2513 /* Load file into a buffer, so that 'fileencoding' is detected,
2514 * autocommands applied, etc. */
2515 buf = load_dummy_buffer(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002516
2517#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2518 au_event_restore(save_ei);
2519#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00002520 }
2521 else
2522 /* Use existing, loaded buffer. */
2523 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002524
Bram Moolenaar81695252004-12-29 20:58:21 +00002525 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002526 {
2527 if (!got_int)
2528 smsg((char_u *)_("Cannot open file \"%s\""), fnames[fi]);
2529 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00002530 else
2531 {
Bram Moolenaar81695252004-12-29 20:58:21 +00002532 found_match = FALSE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002533 /* Try for a match in all lines of the buffer. */
Bram Moolenaar81695252004-12-29 20:58:21 +00002534 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002535 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002536 /* For ":1vimgrep" look for multiple matches. */
2537 col = 0;
2538 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
2539 col) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002540 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00002541 if (qf_add_entry(&prevp,
2542 NULL, /* dir */
2543 fnames[fi],
Bram Moolenaar81695252004-12-29 20:58:21 +00002544 ml_get_buf(buf,
2545 regmatch.startpos[0].lnum + lnum, FALSE),
2546 regmatch.startpos[0].lnum + lnum,
2547 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00002548 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002549 NULL, /* search pattern */
Bram Moolenaar86b68352004-12-27 21:59:20 +00002550 0, /* nr */
2551 0, /* type */
2552 TRUE /* valid */
2553 ) == FAIL)
2554 {
2555 got_int = TRUE;
2556 break;
2557 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002558 else
2559 found_match = TRUE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002560 if ((flags & VGR_GLOBAL) == 0
2561 || regmatch.endpos[0].lnum > 0)
2562 break;
2563 col = regmatch.endpos[0].col
2564 + (col == regmatch.endpos[0].col);
2565 if (col > STRLEN(ml_get_buf(buf, lnum, FALSE)))
2566 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002567 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00002568 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00002569 if (got_int)
2570 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002571 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002572
2573 if (using_dummy)
2574 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002575 if (found_match && first_match_buf == NULL)
2576 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00002577 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002578 {
Bram Moolenaar81695252004-12-29 20:58:21 +00002579 /* Never keep a dummy buffer if there is another buffer
2580 * with the same name. */
2581 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002582 buf = NULL;
2583 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002584 else if (!buf_hide(buf))
2585 {
2586 /* When not hiding the buffer and no match was found we
2587 * don't need to remember the buffer, wipe it out. If
Bram Moolenaar05159a02005-02-26 23:04:13 +00002588 * there was a match and it wasn't the first one or we
2589 * won't jump there: only unload the buffer. */
Bram Moolenaar81695252004-12-29 20:58:21 +00002590 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002591 {
Bram Moolenaar81695252004-12-29 20:58:21 +00002592 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002593 buf = NULL;
2594 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002595 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002596 {
Bram Moolenaar81695252004-12-29 20:58:21 +00002597 unload_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002598 buf = NULL;
2599 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002600 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002601
2602#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2603 if (buf != NULL)
2604 {
2605 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaar748bf032005-02-02 23:04:36 +00002606 * need to be done now, in that buffer. And then the
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00002607 * modelines need to be done (again). */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002608 aucmd_prepbuf(&aco, buf);
2609 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
2610 buf->b_fname, TRUE, buf);
Bram Moolenaar748bf032005-02-02 23:04:36 +00002611 do_modelines(FALSE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002612 aucmd_restbuf(&aco);
2613 }
2614#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00002615 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00002616 }
2617 }
2618
2619 FreeWild(fcount, fnames);
2620
2621 qf_lists[qf_curlist].qf_nonevalid = FALSE;
2622 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
2623 qf_lists[qf_curlist].qf_index = 1;
2624
2625#ifdef FEAT_WINDOWS
2626 qf_update_buffer();
2627#endif
2628
2629 /* Jump to first match. */
2630 if (qf_lists[qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002631 {
2632 if ((flags & VGR_NOJUMP) == 0)
2633 qf_jump(0, 0, eap->forceit);
2634 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002635 else
2636 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002637
Bram Moolenaar7c626922005-02-07 22:01:03 +00002638#ifdef FEAT_AUTOCMD
2639 if (au_name != NULL)
2640 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2641 curbuf->b_fname, TRUE, curbuf);
2642#endif
2643
Bram Moolenaar86b68352004-12-27 21:59:20 +00002644theend:
2645 vim_free(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002646}
2647
2648/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00002649 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00002650 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00002651 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
2652 * If "s" is not NULL terminate the pattern with a NUL.
2653 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00002654 */
2655 char_u *
Bram Moolenaar05159a02005-02-26 23:04:13 +00002656skip_vimgrep_pat(p, s, flags)
2657 char_u *p;
2658 char_u **s;
2659 int *flags;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002660{
2661 int c;
2662
2663 if (vim_isIDc(*p))
2664 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002665 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00002666 if (s != NULL)
2667 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002668 p = skiptowhite(p);
2669 if (s != NULL && *p != NUL)
2670 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002671 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002672 else
2673 {
2674 /* ":vimgrep /pattern/[g][j] fname" */
2675 if (s != NULL)
2676 *s = p + 1;
2677 c = *p;
2678 p = skip_regexp(p + 1, c, TRUE, NULL);
2679 if (*p != c)
2680 return NULL;
2681
2682 /* Truncate the pattern. */
2683 if (s != NULL)
2684 *p = NUL;
2685 ++p;
2686
2687 /* Find the flags */
2688 while (*p == 'g' || *p == 'j')
2689 {
2690 if (flags != NULL)
2691 {
2692 if (*p == 'g')
2693 *flags |= VGR_GLOBAL;
2694 else
2695 *flags |= VGR_NOJUMP;
2696 }
2697 ++p;
2698 }
2699 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00002700 return p;
2701}
2702
2703/*
Bram Moolenaar81695252004-12-29 20:58:21 +00002704 * Load file "fname" into a dummy buffer and return the buffer pointer.
2705 * Returns NULL if it fails.
2706 * Must call unload_dummy_buffer() or wipe_dummy_buffer() later!
2707 */
2708 static buf_T *
2709load_dummy_buffer(fname)
2710 char_u *fname;
2711{
2712 buf_T *newbuf;
2713 int failed = TRUE;
2714#ifdef FEAT_AUTOCMD
2715 aco_save_T aco;
2716#else
2717 buf_T *old_curbuf = curbuf;
2718#endif
2719
2720 /* Allocate a buffer without putting it in the buffer list. */
2721 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
2722 if (newbuf == NULL)
2723 return NULL;
2724
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00002725 /* Init the options. */
2726 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
2727
Bram Moolenaar81695252004-12-29 20:58:21 +00002728#ifdef FEAT_AUTOCMD
2729 /* set curwin/curbuf to buf and save a few things */
2730 aucmd_prepbuf(&aco, newbuf);
2731#else
2732 curbuf = newbuf;
2733 curwin->w_buffer = newbuf;
2734#endif
2735
2736 /* Need to set the filename for autocommands. */
2737 (void)setfname(curbuf, fname, NULL, FALSE);
2738
2739 if (ml_open() == OK)
2740 {
2741 /* Create swap file now to avoid the ATTENTION message. */
2742 check_need_swap(TRUE);
2743
2744 /* Remove the "dummy" flag, otherwise autocommands may not
2745 * work. */
2746 curbuf->b_flags &= ~BF_DUMMY;
2747
2748 if (readfile(fname, NULL,
2749 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
2750 NULL, READ_NEW | READ_DUMMY) == OK
2751 && !(curbuf->b_flags & BF_NEW))
2752 {
2753 failed = FALSE;
2754 if (curbuf != newbuf)
2755 {
2756 /* Bloody autocommands changed the buffer! */
2757 if (buf_valid(newbuf))
2758 wipe_buffer(newbuf, FALSE);
2759 newbuf = curbuf;
2760 }
2761 }
2762 }
2763
2764#ifdef FEAT_AUTOCMD
2765 /* restore curwin/curbuf and a few other things */
2766 aucmd_restbuf(&aco);
2767#else
2768 curbuf = old_curbuf;
2769 curwin->w_buffer = old_curbuf;
2770#endif
2771
2772 if (!buf_valid(newbuf))
2773 return NULL;
2774 if (failed)
2775 {
2776 wipe_dummy_buffer(newbuf);
2777 return NULL;
2778 }
2779 return newbuf;
2780}
2781
2782/*
2783 * Wipe out the dummy buffer that load_dummy_buffer() created.
2784 */
2785 static void
2786wipe_dummy_buffer(buf)
2787 buf_T *buf;
2788{
2789 if (curbuf != buf) /* safety check */
2790 wipe_buffer(buf, FALSE);
2791}
2792
2793/*
2794 * Unload the dummy buffer that load_dummy_buffer() created.
2795 */
2796 static void
2797unload_dummy_buffer(buf)
2798 buf_T *buf;
2799{
2800 if (curbuf != buf) /* safety check */
2801 close_buffer(NULL, buf, DOBUF_UNLOAD);
2802}
2803
Bram Moolenaar05159a02005-02-26 23:04:13 +00002804#if defined(FEAT_EVAL) || defined(PROTO)
2805/*
2806 * Add each quickfix error to list "list" as a dictionary.
2807 */
2808 int
2809get_errorlist(list)
2810 list_T *list;
2811{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002812 dict_T *dict;
2813 char_u buf[2];
2814 qfline_T *qfp;
2815 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002816
2817 if (qf_curlist >= qf_listcount || qf_lists[qf_curlist].qf_count == 0)
2818 {
2819 EMSG(_(e_quickfix));
2820 return FAIL;
2821 }
2822
2823 qfp = qf_lists[qf_curlist].qf_start;
2824 for (i = 1; !got_int && i <= qf_lists[qf_curlist].qf_count; ++i)
2825 {
2826 if ((dict = dict_alloc()) == NULL)
2827 return FAIL;
2828 if (list_append_dict(list, dict) == FAIL)
2829 return FAIL;
2830
2831 buf[0] = qfp->qf_type;
2832 buf[1] = NUL;
2833 if ( dict_add_nr_str(dict, "bufnr", (long)qfp->qf_fnum, NULL) == FAIL
2834 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
2835 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
2836 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
2837 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002838 || dict_add_nr_str(dict, "pattern", 0L, qfp->qf_pattern) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002839 || dict_add_nr_str(dict, "text", 0L, qfp->qf_text) == FAIL
2840 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
2841 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
2842 return FAIL;
2843
2844 qfp = qfp->qf_next;
2845 }
2846 return OK;
2847}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002848
2849/*
2850 * Populate the quickfix list with the items supplied in the list
2851 * of dictionaries.
2852 */
2853 int
Bram Moolenaar35c54e52005-05-20 21:25:31 +00002854set_errorlist(list, action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002855 list_T *list;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00002856 int action;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002857{
2858 listitem_T *li;
2859 dict_T *d;
2860 char_u *filename, *pattern, *text, *type;
2861 long lnum;
2862 int col, nr;
2863 int vcol;
2864 qfline_T *prevp = NULL;
2865 int valid, status;
2866 int retval = OK;
2867
Bram Moolenaar35c54e52005-05-20 21:25:31 +00002868 if (action == ' ' || qf_curlist == qf_listcount)
2869 /* make place for a new list */
2870 qf_new_list();
2871 else if (action == 'a' && qf_lists[qf_curlist].qf_count > 0)
2872 /* Adding to existing list, find last entry. */
2873 for (prevp = qf_lists[qf_curlist].qf_start;
2874 prevp->qf_next != prevp; prevp = prevp->qf_next)
2875 ;
2876 else if (action == 'r')
2877 qf_free(qf_curlist);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002878
2879 for (li = list->lv_first; li != NULL; li = li->li_next)
2880 {
2881 if (li->li_tv.v_type != VAR_DICT)
2882 continue; /* Skip non-dict items */
2883
2884 d = li->li_tv.vval.v_dict;
2885 if (d == NULL)
2886 continue;
2887
2888 filename = get_dict_string(d, (char_u *)"filename");
2889 lnum = get_dict_number(d, (char_u *)"lnum");
2890 col = get_dict_number(d, (char_u *)"col");
2891 vcol = get_dict_number(d, (char_u *)"vcol");
2892 nr = get_dict_number(d, (char_u *)"nr");
2893 type = get_dict_string(d, (char_u *)"type");
2894 pattern = get_dict_string(d, (char_u *)"pattern");
2895 text = get_dict_string(d, (char_u *)"text");
2896 if (text == NULL)
2897 text = vim_strsave((char_u *)"");
2898
2899 valid = TRUE;
2900 if (filename == NULL || (lnum == 0 && pattern == NULL))
2901 valid = FALSE;
2902
2903 status = qf_add_entry(&prevp,
2904 NULL, /* dir */
2905 filename,
2906 text,
2907 lnum,
2908 col,
2909 vcol, /* vis_col */
2910 pattern, /* search pattern */
2911 nr,
2912 type == NULL ? NUL : *type,
2913 valid);
2914
2915 vim_free(filename);
2916 vim_free(pattern);
2917 vim_free(text);
2918 vim_free(type);
2919
2920 if (status == FAIL)
2921 {
2922 retval = FAIL;
2923 break;
2924 }
2925 }
2926
2927 qf_lists[qf_curlist].qf_nonevalid = FALSE;
2928 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
2929 qf_lists[qf_curlist].qf_index = 1;
2930
2931#ifdef FEAT_WINDOWS
2932 qf_update_buffer();
2933#endif
2934
2935 return retval;
2936}
Bram Moolenaar05159a02005-02-26 23:04:13 +00002937#endif
2938
Bram Moolenaar81695252004-12-29 20:58:21 +00002939/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002940 * ":[range]cbuffer [bufnr]" command.
2941 */
2942 void
2943ex_cbuffer(eap)
2944 exarg_T *eap;
2945{
2946 buf_T *buf = NULL;
2947
2948 if (*eap->arg == NUL)
2949 buf = curbuf;
2950 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
2951 buf = buflist_findnr(atoi((char *)eap->arg));
2952 if (buf == NULL)
2953 EMSG(_(e_invarg));
2954 else if (buf->b_ml.ml_mfp == NULL)
2955 EMSG(_("E681: Buffer is not loaded"));
2956 else
2957 {
2958 if (eap->addr_count == 0)
2959 {
2960 eap->line1 = 1;
2961 eap->line2 = buf->b_ml.ml_line_count;
2962 }
2963 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
2964 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
2965 EMSG(_(e_invrange));
2966 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002967 qf_init_ext(NULL, buf, NULL, p_efm, TRUE, eap->line1, eap->line2);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002968 }
2969}
2970
2971/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002972 * ":cexpr {expr}" command.
2973 */
2974 void
2975ex_cexpr(eap)
2976 exarg_T *eap;
2977{
2978 typval_T *tv;
2979
2980 tv = eval_expr(eap->arg, NULL);
2981 if (!tv || (tv->v_type != VAR_STRING && tv->v_type != VAR_LIST) ||
2982 (tv->v_type == VAR_STRING && !tv->vval.v_string) ||
2983 (tv->v_type == VAR_LIST && !tv->vval.v_list))
2984 return;
2985
2986 if (qf_init_ext(NULL, NULL, tv, p_efm, TRUE, (linenr_T)0, (linenr_T)0) > 0)
2987 qf_jump(0, 0, eap->forceit); /* display first error */
2988
2989 clear_tv(tv);
2990}
2991
2992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 * ":helpgrep {pattern}"
2994 */
2995 void
2996ex_helpgrep(eap)
2997 exarg_T *eap;
2998{
2999 regmatch_T regmatch;
3000 char_u *save_cpo;
3001 char_u *p;
3002 int fcount;
3003 char_u **fnames;
3004 FILE *fd;
3005 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003006 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003008#ifdef FEAT_MULTI_LANG
3009 char_u *lang;
3010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011
3012 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
3013 save_cpo = p_cpo;
3014 p_cpo = (char_u *)"";
3015
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003016#ifdef FEAT_MULTI_LANG
3017 /* Check for a specified language */
3018 lang = check_help_lang(eap->arg);
3019#endif
3020
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
3022 regmatch.rm_ic = FALSE;
3023 if (regmatch.regprog != NULL)
3024 {
3025 /* create a new quickfix list */
3026 qf_new_list();
3027
3028 /* Go through all directories in 'runtimepath' */
3029 p = p_rtp;
3030 while (*p != NUL && !got_int)
3031 {
3032 copy_option_part(&p, NameBuff, MAXPATHL, ",");
3033
3034 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
3035 add_pathsep(NameBuff);
3036 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
3037 if (gen_expand_wildcards(1, &NameBuff, &fcount,
3038 &fnames, EW_FILE|EW_SILENT) == OK
3039 && fcount > 0)
3040 {
3041 for (fi = 0; fi < fcount && !got_int; ++fi)
3042 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003043#ifdef FEAT_MULTI_LANG
3044 /* Skip files for a different language. */
3045 if (lang != NULL
3046 && STRNICMP(lang, fnames[fi]
3047 + STRLEN(fnames[fi]) - 3, 2) != 0
3048 && !(STRNICMP(lang, "en", 2) == 0
3049 && STRNICMP("txt", fnames[fi]
3050 + STRLEN(fnames[fi]) - 3, 3) == 0))
3051 continue;
3052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 fd = fopen((char *)fnames[fi], "r");
3054 if (fd != NULL)
3055 {
3056 lnum = 1;
3057 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
3058 {
3059 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
3060 {
3061 int l = STRLEN(IObuff);
3062
3063 /* remove trailing CR, LF, spaces, etc. */
3064 while (l > 0 && IObuff[l - 1] <= ' ')
3065 IObuff[--l] = NUL;
3066
3067 if (qf_add_entry(&prevp,
3068 NULL, /* dir */
3069 fnames[fi],
3070 IObuff,
3071 lnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00003072 (int)(regmatch.startp[0] - IObuff)
3073 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00003074 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003075 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 0, /* nr */
3077 1, /* type */
3078 TRUE /* valid */
3079 ) == FAIL)
3080 {
3081 got_int = TRUE;
3082 break;
3083 }
3084 }
3085 ++lnum;
3086 line_breakcheck();
3087 }
3088 fclose(fd);
3089 }
3090 }
3091 FreeWild(fcount, fnames);
3092 }
3093 }
3094 vim_free(regmatch.regprog);
3095
3096 qf_lists[qf_curlist].qf_nonevalid = FALSE;
3097 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
3098 qf_lists[qf_curlist].qf_index = 1;
3099 }
3100
3101 p_cpo = save_cpo;
3102
3103#ifdef FEAT_WINDOWS
3104 qf_update_buffer();
3105#endif
3106
3107 /* Jump to first match. */
3108 if (qf_lists[qf_curlist].qf_count > 0)
3109 qf_jump(0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003110 else
3111 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112}
3113
3114#endif /* FEAT_QUICKFIX */