blob: d0a2e153e93ed10d496ff477a2a1e013be701844 [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 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000190 {'f', ".\\+"}, /* only used when at end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 {'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
Bram Moolenaare344bea2005-09-01 20:46:49 +0000322 if (*efmp == 'f' && efmp[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000324 if (efmp[1] != '\\' && efmp[1] != '%')
325 {
326 /* A file name may contain spaces, but this isn't
327 * in "\f". For "%f:%l:%m" there may be a ":" in
328 * the file name. Use ".\{-1,}x" instead (x is
329 * the next character), the requirement that :999:
330 * follows should work. */
331 STRCPY(ptr, ".\\{-1,}");
332 ptr += 7;
333 }
334 else
335 {
336 /* File name followed by '\\' or '%': include as
337 * many file name chars as possible. */
338 STRCPY(ptr, "\\f\\+");
339 ptr += 4;
340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 }
342 else
343 {
344 srcptr = (char_u *)fmt_pat[idx].pattern;
345 while ((*ptr = *srcptr++) != NUL)
346 ++ptr;
347 }
348 *ptr++ = '\\';
349 *ptr++ = ')';
350 }
351 else if (*efmp == '*')
352 {
353 if (*++efmp == '[' || *efmp == '\\')
354 {
355 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
356 {
357 if (efmp[1] == '^')
358 *ptr++ = *++efmp;
359 if (efmp < efm + len)
360 {
361 *ptr++ = *++efmp; /* could be ']' */
362 while (efmp < efm + len
363 && (*ptr++ = *++efmp) != ']')
364 /* skip */;
365 if (efmp == efm + len)
366 {
367 EMSG(_("E374: Missing ] in format string"));
368 goto error2;
369 }
370 }
371 }
372 else if (efmp < efm + len) /* %*\D, %*\s etc. */
373 *ptr++ = *++efmp;
374 *ptr++ = '\\';
375 *ptr++ = '+';
376 }
377 else
378 {
379 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
380 sprintf((char *)errmsg,
381 _("E375: Unsupported %%%c in format string"), *efmp);
382 EMSG(errmsg);
383 goto error2;
384 }
385 }
386 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
387 *ptr++ = *efmp; /* regexp magic characters */
388 else if (*efmp == '#')
389 *ptr++ = '*';
390 else if (efmp == efm + 1) /* analyse prefix */
391 {
392 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
393 fmt_ptr->flags = *efmp++;
394 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
395 fmt_ptr->prefix = *efmp;
396 else
397 {
398 sprintf((char *)errmsg,
399 _("E376: Invalid %%%c in format string prefix"), *efmp);
400 EMSG(errmsg);
401 goto error2;
402 }
403 }
404 else
405 {
406 sprintf((char *)errmsg,
407 _("E377: Invalid %%%c in format string"), *efmp);
408 EMSG(errmsg);
409 goto error2;
410 }
411 }
412 else /* copy normal character */
413 {
414 if (*efmp == '\\' && efmp + 1 < efm + len)
415 ++efmp;
416 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
417 *ptr++ = '\\'; /* escape regexp atoms */
418 if (*efmp)
419 *ptr++ = *efmp;
420 }
421 }
422 *ptr++ = '$';
423 *ptr = NUL;
424 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
425 goto error2;
426 /*
427 * Advance to next part
428 */
429 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
430 }
431 if (fmt_first == NULL) /* nothing found */
432 {
433 EMSG(_("E378: 'errorformat' contains no pattern"));
434 goto error2;
435 }
436
437 /*
438 * got_int is reset here, because it was probably set when killing the
439 * ":make" command, but we still want to read the errorfile then.
440 */
441 got_int = FALSE;
442
443 /* Always ignore case when looking for a matching error. */
444 regmatch.rm_ic = TRUE;
445
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000446 if (tv != NULL)
447 {
448 if (tv->v_type == VAR_STRING)
449 p_str = tv->vval.v_string;
450 else if (tv->v_type == VAR_LIST)
451 p_li = tv->vval.v_list->lv_first;
452 }
453
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 /*
455 * Read the lines in the error file one by one.
456 * Try to recognize one of the error formats in each line.
457 */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000458 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 {
Bram Moolenaar86b68352004-12-27 21:59:20 +0000460 /* Get the next line. */
461 if (fd == NULL)
462 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000463 if (tv != NULL)
464 {
465 int len;
466
467 if (tv->v_type == VAR_STRING)
468 {
469 /* Get the next line from the supplied string */
470 char_u *p;
471
472 if (!*p_str) /* Reached the end of the string */
473 break;
474
475 p = vim_strchr(p_str, '\n');
476 if (p)
477 len = p - p_str + 1;
478 else
479 len = STRLEN(p_str);
480
481 if (len > CMDBUFFSIZE - 2)
482 vim_strncpy(IObuff, p_str, CMDBUFFSIZE - 2);
483 else
484 vim_strncpy(IObuff, p_str, len);
485
486 p_str += len;
487 }
488 else if (tv->v_type == VAR_LIST)
489 {
490 /* Get the next line from the supplied list */
491 while (p_li && p_li->li_tv.v_type != VAR_STRING)
492 p_li = p_li->li_next; /* Skip non-string items */
493
494 if (!p_li) /* End of the list */
495 break;
496
497 len = STRLEN(p_li->li_tv.vval.v_string);
498 if (len > CMDBUFFSIZE - 2)
499 len = CMDBUFFSIZE - 2;
500
501 vim_strncpy(IObuff, p_li->li_tv.vval.v_string, len);
502
503 p_li = p_li->li_next; /* next item */
504 }
505 }
506 else
507 {
508 /* Get the next line from the supplied buffer */
509 if (buflnum > lnumlast)
510 break;
511 vim_strncpy(IObuff, ml_get_buf(buf, buflnum++, FALSE),
512 CMDBUFFSIZE - 2);
513 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000514 }
515 else if (fgets((char *)IObuff, CMDBUFFSIZE - 2, fd) == NULL)
516 break;
517
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 IObuff[CMDBUFFSIZE - 2] = NUL; /* for very long lines */
519 if ((efmp = vim_strrchr(IObuff, '\n')) != NULL)
520 *efmp = NUL;
521#ifdef USE_CRNL
522 if ((efmp = vim_strrchr(IObuff, '\r')) != NULL)
523 *efmp = NUL;
524#endif
525
526 /*
527 * Try to match each part of 'errorformat' until we find a complete
528 * match or no match.
529 */
530 valid = TRUE;
531restofline:
532 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
533 {
534 idx = fmt_ptr->prefix;
535 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
536 continue;
537 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000538 pattern[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 if (!multiscan)
540 errmsg[0] = NUL;
541 lnum = 0;
542 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000543 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 enr = -1;
545 type = 0;
546 tail = NULL;
547
548 regmatch.regprog = fmt_ptr->prog;
549 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
550 {
551 if ((idx == 'C' || idx == 'Z') && !multiline)
552 continue;
553 if (vim_strchr((char_u *)"EWI", idx) != NULL)
554 type = idx;
555 else
556 type = 0;
557 /*
558 * Extract error message data from matched line
559 */
560 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
561 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000562 int c = *regmatch.endp[i];
563
564 /* Expand ~/file and $HOME/file to full path. */
565 *regmatch.endp[i] = NUL;
566 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
567 *regmatch.endp[i] = c;
568
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000570 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 continue;
572 }
573 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
574 enr = (int)atol((char *)regmatch.startp[i]);
575 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
576 lnum = atol((char *)regmatch.startp[i]);
577 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
578 col = (int)atol((char *)regmatch.startp[i]);
579 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
580 type = *regmatch.startp[i];
581 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
582 STRCPY(errmsg, IObuff);
583 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
584 {
585 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaarbbebc852005-07-18 21:47:53 +0000586 vim_strncpy(errmsg, regmatch.startp[i], len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 }
588 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
589 tail = regmatch.startp[i];
590 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
591 {
592 col = (int)(regmatch.endp[i] - regmatch.startp[i] + 1);
593 if (*((char_u *)regmatch.startp[i]) != TAB)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000594 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 }
596 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
597 {
598 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000599 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000601 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
602 {
603 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
604 if (len > CMDBUFFSIZE - 5)
605 len = CMDBUFFSIZE - 5;
606 STRCPY(pattern, "^\\V");
607 STRNCAT(pattern, regmatch.startp[i], len);
608 pattern[len + 3] = '\\';
609 pattern[len + 4] = '$';
610 pattern[len + 5] = NUL;
611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 break;
613 }
614 }
615 multiscan = FALSE;
616 if (!fmt_ptr || idx == 'D' || idx == 'X')
617 {
618 if (fmt_ptr)
619 {
620 if (idx == 'D') /* enter directory */
621 {
622 if (*namebuf == NUL)
623 {
624 EMSG(_("E379: Missing or empty directory name"));
625 goto error2;
626 }
627 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
628 goto error2;
629 }
630 else if (idx == 'X') /* leave directory */
631 directory = qf_pop_dir(&dir_stack);
632 }
633 namebuf[0] = NUL; /* no match found, remove file name */
634 lnum = 0; /* don't jump to this line */
635 valid = FALSE;
636 STRCPY(errmsg, IObuff); /* copy whole line to error message */
637 if (!fmt_ptr)
638 multiline = multiignore = FALSE;
639 }
640 else if (fmt_ptr)
641 {
642 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
643 multiline = TRUE; /* start of a multi-line message */
644 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
645 { /* continuation of multi-line msg */
646 if (qfprev == NULL)
647 goto error2;
648 if (*errmsg && !multiignore)
649 {
650 len = (int)STRLEN(qfprev->qf_text);
651 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
652 == NULL)
653 goto error2;
654 STRCPY(ptr, qfprev->qf_text);
655 vim_free(qfprev->qf_text);
656 qfprev->qf_text = ptr;
657 *(ptr += len) = '\n';
658 STRCPY(++ptr, errmsg);
659 }
660 if (qfprev->qf_nr == -1)
661 qfprev->qf_nr = enr;
662 if (vim_isprintc(type) && !qfprev->qf_type)
663 qfprev->qf_type = type; /* only printable chars allowed */
664 if (!qfprev->qf_lnum)
665 qfprev->qf_lnum = lnum;
666 if (!qfprev->qf_col)
667 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000668 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 if (!qfprev->qf_fnum)
670 qfprev->qf_fnum = qf_get_fnum(directory,
671 *namebuf || directory ? namebuf
672 : currfile && valid ? currfile : 0);
673 if (idx == 'Z')
674 multiline = multiignore = FALSE;
675 line_breakcheck();
676 continue;
677 }
678 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
679 {
680 /* global file names */
681 valid = FALSE;
682 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
683 {
684 if (*namebuf && idx == 'P')
685 currfile = qf_push_dir(namebuf, &file_stack);
686 else if (idx == 'Q')
687 currfile = qf_pop_dir(&file_stack);
688 *namebuf = NUL;
689 if (tail && *tail)
690 {
691 STRCPY(IObuff, skipwhite(tail));
692 multiscan = TRUE;
693 goto restofline;
694 }
695 }
696 }
697 if (fmt_ptr->flags == '-') /* generally exclude this line */
698 {
699 if (multiline)
700 multiignore = TRUE; /* also exclude continuation lines */
701 continue;
702 }
703 }
704
705 if (qf_add_entry(&qfprev,
706 directory,
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000707 (*namebuf || directory)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 ? namebuf
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000709 : ((currfile && valid) ? currfile : (char_u *)NULL),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 errmsg,
711 lnum,
712 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +0000713 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000714 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 enr,
716 type,
717 valid) == FAIL)
718 goto error2;
719 line_breakcheck();
720 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000721 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 {
723 if (qf_lists[qf_curlist].qf_index == 0) /* no valid entry found */
724 {
725 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
726 qf_lists[qf_curlist].qf_index = 1;
727 qf_lists[qf_curlist].qf_nonevalid = TRUE;
728 }
729 else
730 {
731 qf_lists[qf_curlist].qf_nonevalid = FALSE;
732 if (qf_lists[qf_curlist].qf_ptr == NULL)
733 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
734 }
735 retval = qf_lists[qf_curlist].qf_count; /* return number of matches */
736 goto qf_init_ok;
737 }
738 EMSG(_(e_readerrf));
739error2:
740 qf_free(qf_curlist);
741 qf_listcount--;
742 if (qf_curlist > 0)
743 --qf_curlist;
744qf_init_ok:
Bram Moolenaar86b68352004-12-27 21:59:20 +0000745 if (fd != NULL)
746 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
748 {
749 fmt_first = fmt_ptr->next;
750 vim_free(fmt_ptr->prog);
751 vim_free(fmt_ptr);
752 }
753 qf_clean_dir_stack(&dir_stack);
754 qf_clean_dir_stack(&file_stack);
755qf_init_end:
756 vim_free(namebuf);
757 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000758 vim_free(pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 vim_free(fmtstr);
760
761#ifdef FEAT_WINDOWS
762 qf_update_buffer();
763#endif
764
765 return retval;
766}
767
768/*
769 * Prepare for adding a new quickfix list.
770 */
771 static void
772qf_new_list()
773{
774 int i;
775
776 /*
777 * If the current entry is not the last entry, delete entries below
778 * the current entry. This makes it possible to browse in a tree-like
779 * way with ":grep'.
780 */
781 while (qf_listcount > qf_curlist + 1)
782 qf_free(--qf_listcount);
783
784 /*
785 * When the stack is full, remove to oldest entry
786 * Otherwise, add a new entry.
787 */
788 if (qf_listcount == LISTCOUNT)
789 {
790 qf_free(0);
791 for (i = 1; i < LISTCOUNT; ++i)
792 qf_lists[i - 1] = qf_lists[i];
793 qf_curlist = LISTCOUNT - 1;
794 }
795 else
796 qf_curlist = qf_listcount++;
797 qf_lists[qf_curlist].qf_index = 0;
798 qf_lists[qf_curlist].qf_count = 0;
799}
800
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000801#if defined(EXITFREE) || defined(PROTO)
802 void
803qf_free_all()
804{
805 int i;
806
807 for (i = 0; i < qf_listcount; ++i)
808 qf_free(i);
809}
810#endif
811
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812/*
813 * Add an entry to the end of the list of errors.
814 * Returns OK or FAIL.
815 */
816 static int
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000817qf_add_entry(prevp, dir, fname, mesg, lnum, col, vis_col, pattern, nr, type,
818 valid)
819 qfline_T **prevp; /* pointer to previously added entry or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 char_u *dir; /* optional directory name */
821 char_u *fname; /* file name or NULL */
822 char_u *mesg; /* message */
823 long lnum; /* line number */
824 int col; /* column */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000825 int vis_col; /* using visual column */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000826 char_u *pattern; /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 int nr; /* error number */
828 int type; /* type character */
829 int valid; /* valid entry */
830{
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000831 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000833 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 return FAIL;
835 qfp->qf_fnum = qf_get_fnum(dir, fname);
836 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
837 {
838 vim_free(qfp);
839 return FAIL;
840 }
841 qfp->qf_lnum = lnum;
842 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000843 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000844 if (pattern == NULL || *pattern == NUL)
845 qfp->qf_pattern = NULL;
846 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
847 {
848 vim_free(qfp->qf_text);
849 vim_free(qfp);
850 return FAIL;
851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 qfp->qf_nr = nr;
853 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
854 type = 0;
855 qfp->qf_type = type;
856 qfp->qf_valid = valid;
857
858 if (qf_lists[qf_curlist].qf_count == 0) /* first element in the list */
859 {
860 qf_lists[qf_curlist].qf_start = qfp;
861 qfp->qf_prev = qfp; /* first element points to itself */
862 }
863 else
864 {
865 qfp->qf_prev = *prevp;
866 (*prevp)->qf_next = qfp;
867 }
868 qfp->qf_next = qfp; /* last element points to itself */
869 qfp->qf_cleared = FALSE;
870 *prevp = qfp;
871 ++qf_lists[qf_curlist].qf_count;
872 if (qf_lists[qf_curlist].qf_index == 0 && qfp->qf_valid)
873 /* first valid entry */
874 {
875 qf_lists[qf_curlist].qf_index = qf_lists[qf_curlist].qf_count;
876 qf_lists[qf_curlist].qf_ptr = qfp;
877 }
878
879 return OK;
880}
881
882/*
883 * get buffer number for file "dir.name"
884 */
885 static int
886qf_get_fnum(directory, fname)
887 char_u *directory;
888 char_u *fname;
889{
890 if (fname == NULL || *fname == NUL) /* no file name */
891 return 0;
892 {
893#ifdef RISCOS
894 /* Name is reported as `main.c', but file is `c.main' */
895 return ro_buflist_add(fname);
896#else
897 char_u *ptr;
898 int fnum;
899
900# ifdef VMS
901 vms_remove_version(fname);
902# endif
903# ifdef BACKSLASH_IN_FILENAME
904 if (directory != NULL)
905 slash_adjust(directory);
906 slash_adjust(fname);
907# endif
908 if (directory != NULL && !vim_isAbsName(fname)
909 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
910 {
911 /*
912 * Here we check if the file really exists.
913 * This should normally be true, but if make works without
914 * "leaving directory"-messages we might have missed a
915 * directory change.
916 */
917 if (mch_getperm(ptr) < 0)
918 {
919 vim_free(ptr);
920 directory = qf_guess_filepath(fname);
921 if (directory)
922 ptr = concat_fnames(directory, fname, TRUE);
923 else
924 ptr = vim_strsave(fname);
925 }
926 /* Use concatenated directory name and file name */
927 fnum = buflist_add(ptr, 0);
928 vim_free(ptr);
929 return fnum;
930 }
931 return buflist_add(fname, 0);
932#endif
933 }
934}
935
936/*
937 * push dirbuf onto the directory stack and return pointer to actual dir or
938 * NULL on error
939 */
940 static char_u *
941qf_push_dir(dirbuf, stackptr)
942 char_u *dirbuf;
943 struct dir_stack_T **stackptr;
944{
945 struct dir_stack_T *ds_new;
946 struct dir_stack_T *ds_ptr;
947
948 /* allocate new stack element and hook it in */
949 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
950 if (ds_new == NULL)
951 return NULL;
952
953 ds_new->next = *stackptr;
954 *stackptr = ds_new;
955
956 /* store directory on the stack */
957 if (vim_isAbsName(dirbuf)
958 || (*stackptr)->next == NULL
959 || (*stackptr && dir_stack != *stackptr))
960 (*stackptr)->dirname = vim_strsave(dirbuf);
961 else
962 {
963 /* Okay we don't have an absolute path.
964 * dirbuf must be a subdir of one of the directories on the stack.
965 * Let's search...
966 */
967 ds_new = (*stackptr)->next;
968 (*stackptr)->dirname = NULL;
969 while (ds_new)
970 {
971 vim_free((*stackptr)->dirname);
972 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
973 TRUE);
974 if (mch_isdir((*stackptr)->dirname) == TRUE)
975 break;
976
977 ds_new = ds_new->next;
978 }
979
980 /* clean up all dirs we already left */
981 while ((*stackptr)->next != ds_new)
982 {
983 ds_ptr = (*stackptr)->next;
984 (*stackptr)->next = (*stackptr)->next->next;
985 vim_free(ds_ptr->dirname);
986 vim_free(ds_ptr);
987 }
988
989 /* Nothing found -> it must be on top level */
990 if (ds_new == NULL)
991 {
992 vim_free((*stackptr)->dirname);
993 (*stackptr)->dirname = vim_strsave(dirbuf);
994 }
995 }
996
997 if ((*stackptr)->dirname != NULL)
998 return (*stackptr)->dirname;
999 else
1000 {
1001 ds_ptr = *stackptr;
1002 *stackptr = (*stackptr)->next;
1003 vim_free(ds_ptr);
1004 return NULL;
1005 }
1006}
1007
1008
1009/*
1010 * pop dirbuf from the directory stack and return previous directory or NULL if
1011 * stack is empty
1012 */
1013 static char_u *
1014qf_pop_dir(stackptr)
1015 struct dir_stack_T **stackptr;
1016{
1017 struct dir_stack_T *ds_ptr;
1018
1019 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1020 * What to do if it isn't? */
1021
1022 /* pop top element and free it */
1023 if (*stackptr != NULL)
1024 {
1025 ds_ptr = *stackptr;
1026 *stackptr = (*stackptr)->next;
1027 vim_free(ds_ptr->dirname);
1028 vim_free(ds_ptr);
1029 }
1030
1031 /* return NEW top element as current dir or NULL if stack is empty*/
1032 return *stackptr ? (*stackptr)->dirname : NULL;
1033}
1034
1035/*
1036 * clean up directory stack
1037 */
1038 static void
1039qf_clean_dir_stack(stackptr)
1040 struct dir_stack_T **stackptr;
1041{
1042 struct dir_stack_T *ds_ptr;
1043
1044 while ((ds_ptr = *stackptr) != NULL)
1045 {
1046 *stackptr = (*stackptr)->next;
1047 vim_free(ds_ptr->dirname);
1048 vim_free(ds_ptr);
1049 }
1050}
1051
1052/*
1053 * Check in which directory of the directory stack the given file can be
1054 * found.
1055 * Returns a pointer to the directory name or NULL if not found
1056 * Cleans up intermediate directory entries.
1057 *
1058 * TODO: How to solve the following problem?
1059 * If we have the this directory tree:
1060 * ./
1061 * ./aa
1062 * ./aa/bb
1063 * ./bb
1064 * ./bb/x.c
1065 * and make says:
1066 * making all in aa
1067 * making all in bb
1068 * x.c:9: Error
1069 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1070 * qf_guess_filepath will return NULL.
1071 */
1072 static char_u *
1073qf_guess_filepath(filename)
1074 char_u *filename;
1075{
1076 struct dir_stack_T *ds_ptr;
1077 struct dir_stack_T *ds_tmp;
1078 char_u *fullname;
1079
1080 /* no dirs on the stack - there's nothing we can do */
1081 if (dir_stack == NULL)
1082 return NULL;
1083
1084 ds_ptr = dir_stack->next;
1085 fullname = NULL;
1086 while (ds_ptr)
1087 {
1088 vim_free(fullname);
1089 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1090
1091 /* If concat_fnames failed, just go on. The worst thing that can happen
1092 * is that we delete the entire stack.
1093 */
1094 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1095 break;
1096
1097 ds_ptr = ds_ptr->next;
1098 }
1099
1100 vim_free(fullname);
1101
1102 /* clean up all dirs we already left */
1103 while (dir_stack->next != ds_ptr)
1104 {
1105 ds_tmp = dir_stack->next;
1106 dir_stack->next = dir_stack->next->next;
1107 vim_free(ds_tmp->dirname);
1108 vim_free(ds_tmp);
1109 }
1110
1111 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1112
1113}
1114
1115/*
1116 * jump to a quickfix line
1117 * if dir == FORWARD go "errornr" valid entries forward
1118 * if dir == BACKWARD go "errornr" valid entries backward
1119 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1120 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1121 * else if "errornr" is zero, redisplay the same line
1122 * else go to entry "errornr"
1123 */
1124 void
1125qf_jump(dir, errornr, forceit)
1126 int dir;
1127 int errornr;
1128 int forceit;
1129{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001130 qfline_T *qf_ptr;
1131 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 int qf_index;
1133 int old_qf_fnum;
1134 int old_qf_index;
1135 int prev_index;
1136 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1137 char_u *err = e_no_more_items;
1138 linenr_T i;
1139 buf_T *old_curbuf;
1140 linenr_T old_lnum;
1141 char_u *old_swb = p_swb;
1142 colnr_T screen_col;
1143 colnr_T char_col;
1144 char_u *line;
1145#ifdef FEAT_WINDOWS
1146 int opened_window = FALSE;
1147 win_T *win;
1148 win_T *altwin;
1149#endif
1150 int print_message = TRUE;
1151 int len;
1152#ifdef FEAT_FOLDING
1153 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1154#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001155 int ok = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156
1157 if (qf_curlist >= qf_listcount || qf_lists[qf_curlist].qf_count == 0)
1158 {
1159 EMSG(_(e_quickfix));
1160 return;
1161 }
1162
1163 qf_ptr = qf_lists[qf_curlist].qf_ptr;
1164 old_qf_ptr = qf_ptr;
1165 qf_index = qf_lists[qf_curlist].qf_index;
1166 old_qf_index = qf_index;
1167 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1168 {
1169 while (errornr--)
1170 {
1171 old_qf_ptr = qf_ptr;
1172 prev_index = qf_index;
1173 old_qf_fnum = qf_ptr->qf_fnum;
1174 do
1175 {
1176 if (qf_index == qf_lists[qf_curlist].qf_count
1177 || qf_ptr->qf_next == NULL)
1178 {
1179 qf_ptr = old_qf_ptr;
1180 qf_index = prev_index;
1181 if (err != NULL)
1182 {
1183 EMSG(_(err));
1184 goto theend;
1185 }
1186 errornr = 0;
1187 break;
1188 }
1189 ++qf_index;
1190 qf_ptr = qf_ptr->qf_next;
1191 } while ((!qf_lists[qf_curlist].qf_nonevalid && !qf_ptr->qf_valid)
1192 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1193 err = NULL;
1194 }
1195 }
1196 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1197 {
1198 while (errornr--)
1199 {
1200 old_qf_ptr = qf_ptr;
1201 prev_index = qf_index;
1202 old_qf_fnum = qf_ptr->qf_fnum;
1203 do
1204 {
1205 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1206 {
1207 qf_ptr = old_qf_ptr;
1208 qf_index = prev_index;
1209 if (err != NULL)
1210 {
1211 EMSG(_(err));
1212 goto theend;
1213 }
1214 errornr = 0;
1215 break;
1216 }
1217 --qf_index;
1218 qf_ptr = qf_ptr->qf_prev;
1219 } while ((!qf_lists[qf_curlist].qf_nonevalid && !qf_ptr->qf_valid)
1220 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1221 err = NULL;
1222 }
1223 }
1224 else if (errornr != 0) /* go to specified number */
1225 {
1226 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1227 {
1228 --qf_index;
1229 qf_ptr = qf_ptr->qf_prev;
1230 }
1231 while (errornr > qf_index && qf_index < qf_lists[qf_curlist].qf_count
1232 && qf_ptr->qf_next != NULL)
1233 {
1234 ++qf_index;
1235 qf_ptr = qf_ptr->qf_next;
1236 }
1237 }
1238
1239#ifdef FEAT_WINDOWS
1240 qf_lists[qf_curlist].qf_index = qf_index;
1241 if (qf_win_pos_update(old_qf_index))
1242 /* No need to print the error message if it's visible in the error
1243 * window */
1244 print_message = FALSE;
1245
1246 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001247 * For ":helpgrep" find a help window or open one.
1248 */
1249 if (qf_ptr->qf_type == 1 && !curwin->w_buffer->b_help)
1250 {
1251 win_T *wp;
1252 int n;
1253
1254 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1255 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1256 break;
1257 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1258 win_enter(wp, TRUE);
1259 else
1260 {
1261 /*
1262 * Split off help window; put it at far top if no position
1263 * specified, the current window is vertically split and narrow.
1264 */
1265 n = WSP_HELP;
1266# ifdef FEAT_VERTSPLIT
1267 if (cmdmod.split == 0 && curwin->w_width != Columns
1268 && curwin->w_width < 80)
1269 n |= WSP_TOP;
1270# endif
1271 if (win_split(0, n) == FAIL)
1272 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001273 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001274
1275 if (curwin->w_height < p_hh)
1276 win_setheight((int)p_hh);
1277 }
1278
1279 if (!p_im)
1280 restart_edit = 0; /* don't want insert mode in help file */
1281 }
1282
1283 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 * If currently in the quickfix window, find another window to show the
1285 * file in.
1286 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001287 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 {
1289 /*
1290 * If there is no file specified, we don't know where to go.
1291 * But do advance, otherwise ":cn" gets stuck.
1292 */
1293 if (qf_ptr->qf_fnum == 0)
1294 goto theend;
1295
1296 /*
1297 * If there is only one window, create a new one above the quickfix
1298 * window.
1299 */
1300 if (firstwin == lastwin)
1301 {
1302 if (win_split(0, WSP_ABOVE) == FAIL)
1303 goto failed; /* not enough room for window */
1304 opened_window = TRUE; /* close it when fail */
1305 p_swb = empty_option; /* don't split again */
1306# ifdef FEAT_SCROLLBIND
1307 curwin->w_p_scb = FALSE;
1308# endif
1309 }
1310 else
1311 {
1312 /*
1313 * Try to find a window that shows the right buffer.
1314 * Default to the window just above the quickfix buffer.
1315 */
1316 win = curwin;
1317 altwin = NULL;
1318 for (;;)
1319 {
1320 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1321 break;
1322 if (win->w_prev == NULL)
1323 win = lastwin; /* wrap around the top */
1324 else
1325 win = win->w_prev; /* go to previous window */
1326
1327 if (bt_quickfix(win->w_buffer))
1328 {
1329 /* Didn't find it, go to the window before the quickfix
1330 * window. */
1331 if (altwin != NULL)
1332 win = altwin;
1333 else if (curwin->w_prev != NULL)
1334 win = curwin->w_prev;
1335 else
1336 win = curwin->w_next;
1337 break;
1338 }
1339
1340 /* Remember a usable window. */
1341 if (altwin == NULL && !win->w_p_pvw
1342 && win->w_buffer->b_p_bt[0] == NUL)
1343 altwin = win;
1344 }
1345
1346 win_goto(win);
1347 }
1348 }
1349#endif
1350
1351 /*
1352 * If there is a file name,
1353 * read the wanted file if needed, and check autowrite etc.
1354 */
1355 old_curbuf = curbuf;
1356 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001357
1358 if (qf_ptr->qf_fnum != 0)
1359 {
1360 if (qf_ptr->qf_type == 1)
1361 {
1362 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1363 * set b_p_ro flag). */
1364 if (!can_abandon(curbuf, forceit))
1365 {
1366 EMSG(_(e_nowrtmsg));
1367 ok = FALSE;
1368 }
1369 else
1370 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
1371 ECMD_HIDE + ECMD_SET_HELP);
1372 }
1373 else
1374 ok = buflist_getfile(qf_ptr->qf_fnum,
1375 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
1376 }
1377
1378 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 {
1380 /* When not switched to another buffer, still need to set pc mark */
1381 if (curbuf == old_curbuf)
1382 setpcmark();
1383
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001384 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001386 /*
1387 * Go to line with error, unless qf_lnum is 0.
1388 */
1389 i = qf_ptr->qf_lnum;
1390 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001392 if (i > curbuf->b_ml.ml_line_count)
1393 i = curbuf->b_ml.ml_line_count;
1394 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001396 if (qf_ptr->qf_col > 0)
1397 {
1398 curwin->w_cursor.col = qf_ptr->qf_col - 1;
1399 if (qf_ptr->qf_viscol == TRUE)
1400 {
1401 /*
1402 * Check each character from the beginning of the error
1403 * line up to the error column. For each tab character
1404 * found, reduce the error column value by the length of
1405 * a tab character.
1406 */
1407 line = ml_get_curline();
1408 screen_col = 0;
1409 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1410 {
1411 if (*line == NUL)
1412 break;
1413 if (*line++ == '\t')
1414 {
1415 curwin->w_cursor.col -= 7 - (screen_col % 8);
1416 screen_col += 8 - (screen_col % 8);
1417 }
1418 else
1419 ++screen_col;
1420 }
1421 }
1422 check_cursor();
1423 }
1424 else
1425 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 }
1427 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001428 {
1429 pos_T save_cursor;
1430
1431 /* Move the cursor to the first line in the buffer */
1432 save_cursor = curwin->w_cursor;
1433 curwin->w_cursor.lnum = 0;
1434 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1, SEARCH_KEEP))
1435 curwin->w_cursor = save_cursor;
1436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437
1438#ifdef FEAT_FOLDING
1439 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1440 foldOpenCursor();
1441#endif
1442 if (print_message)
1443 {
1444 /* Update the screen before showing the message */
1445 update_topline_redraw();
1446 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
1447 qf_lists[qf_curlist].qf_count,
1448 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1449 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1450 /* Add the message, skipping leading whitespace and newlines. */
1451 len = (int)STRLEN(IObuff);
1452 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1453
1454 /* Output the message. Overwrite to avoid scrolling when the 'O'
1455 * flag is present in 'shortmess'; But when not jumping, print the
1456 * whole message. */
1457 i = msg_scroll;
1458 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1459 msg_scroll = TRUE;
1460 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1461 msg_scroll = FALSE;
1462 msg_attr_keep(IObuff, 0, TRUE);
1463 msg_scroll = i;
1464 }
1465 }
1466 else
1467 {
1468#ifdef FEAT_WINDOWS
1469 if (opened_window)
1470 win_close(curwin, TRUE); /* Close opened window */
1471#endif
1472 if (qf_ptr->qf_fnum != 0)
1473 {
1474 /*
1475 * Couldn't open file, so put index back where it was. This could
1476 * happen if the file was readonly and we changed something.
1477 */
1478#ifdef FEAT_WINDOWS
1479failed:
1480#endif
1481 qf_ptr = old_qf_ptr;
1482 qf_index = old_qf_index;
1483 }
1484 }
1485theend:
1486 qf_lists[qf_curlist].qf_ptr = qf_ptr;
1487 qf_lists[qf_curlist].qf_index = qf_index;
1488#ifdef FEAT_WINDOWS
1489 if (p_swb != old_swb && opened_window)
1490 {
1491 /* Restore old 'switchbuf' value, but not when an autocommand or
1492 * modeline has changed the value. */
1493 if (p_swb == empty_option)
1494 p_swb = old_swb;
1495 else
1496 free_string_option(old_swb);
1497 }
1498#endif
1499}
1500
1501/*
1502 * ":clist": list all errors
1503 */
1504 void
1505qf_list(eap)
1506 exarg_T *eap;
1507{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001508 buf_T *buf;
1509 char_u *fname;
1510 qfline_T *qfp;
1511 int i;
1512 int idx1 = 1;
1513 int idx2 = -1;
1514 int need_return = TRUE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001515 char_u *arg = eap->arg;
1516 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 recognised errors */
1518
1519 if (qf_curlist >= qf_listcount || qf_lists[qf_curlist].qf_count == 0)
1520 {
1521 EMSG(_(e_quickfix));
1522 return;
1523 }
1524 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
1525 {
1526 EMSG(_(e_trailing));
1527 return;
1528 }
1529 i = qf_lists[qf_curlist].qf_count;
1530 if (idx1 < 0)
1531 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
1532 if (idx2 < 0)
1533 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
1534
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 if (qf_lists[qf_curlist].qf_nonevalid)
1536 all = TRUE;
1537 qfp = qf_lists[qf_curlist].qf_start;
1538 for (i = 1; !got_int && i <= qf_lists[qf_curlist].qf_count; )
1539 {
1540 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
1541 {
1542 if (need_return)
1543 {
1544 msg_putchar('\n');
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001545 if (got_int)
1546 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 need_return = FALSE;
1548 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001549
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001550 fname = NULL;
1551 if (qfp->qf_fnum != 0
1552 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
1553 {
1554 fname = buf->b_fname;
1555 if (qfp->qf_type == 1) /* :helpgrep */
1556 fname = gettail(fname);
1557 }
1558 if (fname == NULL)
1559 sprintf((char *)IObuff, "%2d", i);
1560 else
1561 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
1562 i, (char *)fname);
1563 msg_outtrans_attr(IObuff, i == qf_lists[qf_curlist].qf_index
1564 ? hl_attr(HLF_L) : hl_attr(HLF_D));
1565 if (qfp->qf_lnum == 0)
1566 IObuff[0] = NUL;
1567 else if (qfp->qf_col == 0)
1568 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
1569 else
1570 sprintf((char *)IObuff, ":%ld col %d",
1571 qfp->qf_lnum, qfp->qf_col);
1572 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
1573 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
1574 msg_puts_attr(IObuff, hl_attr(HLF_N));
1575 if (qfp->qf_pattern != NULL)
1576 {
1577 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
1578 STRCAT(IObuff, ":");
1579 msg_puts(IObuff);
1580 }
1581 msg_puts((char_u *)" ");
1582
1583 /* Remove newlines and leading whitespace from the text. For an
1584 * unrecognized line keep the indent, the compiler may mark a word
1585 * with ^^^^. */
1586 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 ? skipwhite(qfp->qf_text) : qfp->qf_text,
1588 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001589 msg_prt_line(IObuff, FALSE);
1590 out_flush(); /* show one line at a time */
1591 need_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001593
1594 qfp = qfp->qf_next;
1595 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 ui_breakcheck();
1597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598}
1599
1600/*
1601 * Remove newlines and leading whitespace from an error message.
1602 * Put the result in "buf[bufsize]".
1603 */
1604 static void
1605qf_fmt_text(text, buf, bufsize)
1606 char_u *text;
1607 char_u *buf;
1608 int bufsize;
1609{
1610 int i;
1611 char_u *p = text;
1612
1613 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
1614 {
1615 if (*p == '\n')
1616 {
1617 buf[i] = ' ';
1618 while (*++p != NUL)
1619 if (!vim_iswhite(*p) && *p != '\n')
1620 break;
1621 }
1622 else
1623 buf[i] = *p++;
1624 }
1625 buf[i] = NUL;
1626}
1627
1628/*
1629 * ":colder [count]": Up in the quickfix stack.
1630 * ":cnewer [count]": Down in the quickfix stack.
1631 */
1632 void
1633qf_age(eap)
1634 exarg_T *eap;
1635{
1636 int count;
1637
1638 if (eap->addr_count != 0)
1639 count = eap->line2;
1640 else
1641 count = 1;
1642 while (count--)
1643 {
1644 if (eap->cmdidx == CMD_colder)
1645 {
1646 if (qf_curlist == 0)
1647 {
1648 EMSG(_("E380: At bottom of quickfix stack"));
1649 return;
1650 }
1651 --qf_curlist;
1652 }
1653 else
1654 {
1655 if (qf_curlist >= qf_listcount - 1)
1656 {
1657 EMSG(_("E381: At top of quickfix stack"));
1658 return;
1659 }
1660 ++qf_curlist;
1661 }
1662 }
1663 qf_msg();
1664}
1665
1666 static void
1667qf_msg()
1668{
1669 smsg((char_u *)_("error list %d of %d; %d errors"),
1670 qf_curlist + 1, qf_listcount, qf_lists[qf_curlist].qf_count);
1671#ifdef FEAT_WINDOWS
1672 qf_update_buffer();
1673#endif
1674}
1675
1676/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00001677 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 */
1679 static void
1680qf_free(idx)
1681 int idx;
1682{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001683 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684
1685 while (qf_lists[idx].qf_count)
1686 {
1687 qfp = qf_lists[idx].qf_start->qf_next;
1688 vim_free(qf_lists[idx].qf_start->qf_text);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001689 vim_free(qf_lists[idx].qf_start->qf_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 vim_free(qf_lists[idx].qf_start);
1691 qf_lists[idx].qf_start = qfp;
1692 --qf_lists[idx].qf_count;
1693 }
1694}
1695
1696/*
1697 * qf_mark_adjust: adjust marks
1698 */
1699 void
1700qf_mark_adjust(line1, line2, amount, amount_after)
1701 linenr_T line1;
1702 linenr_T line2;
1703 long amount;
1704 long amount_after;
1705{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001706 int i;
1707 qfline_T *qfp;
1708 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709
1710 for (idx = 0; idx < qf_listcount; ++idx)
1711 if (qf_lists[idx].qf_count)
1712 for (i = 0, qfp = qf_lists[idx].qf_start;
1713 i < qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
1714 if (qfp->qf_fnum == curbuf->b_fnum)
1715 {
1716 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
1717 {
1718 if (amount == MAXLNUM)
1719 qfp->qf_cleared = TRUE;
1720 else
1721 qfp->qf_lnum += amount;
1722 }
1723 else if (amount_after && qfp->qf_lnum > line2)
1724 qfp->qf_lnum += amount_after;
1725 }
1726}
1727
1728/*
1729 * Make a nice message out of the error character and the error number:
1730 * char number message
1731 * e or E 0 " error"
1732 * w or W 0 " warning"
1733 * i or I 0 " info"
1734 * 0 0 ""
1735 * other 0 " c"
1736 * e or E n " error n"
1737 * w or W n " warning n"
1738 * i or I n " info n"
1739 * 0 n " error n"
1740 * other n " c n"
1741 * 1 x "" :helpgrep
1742 */
1743 static char_u *
1744qf_types(c, nr)
1745 int c, nr;
1746{
1747 static char_u buf[20];
1748 static char_u cc[3];
1749 char_u *p;
1750
1751 if (c == 'W' || c == 'w')
1752 p = (char_u *)" warning";
1753 else if (c == 'I' || c == 'i')
1754 p = (char_u *)" info";
1755 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
1756 p = (char_u *)" error";
1757 else if (c == 0 || c == 1)
1758 p = (char_u *)"";
1759 else
1760 {
1761 cc[0] = ' ';
1762 cc[1] = c;
1763 cc[2] = NUL;
1764 p = cc;
1765 }
1766
1767 if (nr <= 0)
1768 return p;
1769
1770 sprintf((char *)buf, "%s %3d", (char *)p, nr);
1771 return buf;
1772}
1773
1774#if defined(FEAT_WINDOWS) || defined(PROTO)
1775/*
1776 * ":cwindow": open the quickfix window if we have errors to display,
1777 * close it if not.
1778 */
1779 void
1780ex_cwindow(eap)
1781 exarg_T *eap;
1782{
1783 win_T *win;
1784
1785 /*
1786 * Look for an existing quickfix window.
1787 */
1788 for (win = firstwin; win != NULL; win = win->w_next)
1789 if (bt_quickfix(win->w_buffer))
1790 break;
1791
1792 /*
1793 * If a quickfix window is open but we have no errors to display,
1794 * close the window. If a quickfix window is not open, then open
1795 * it if we have errors; otherwise, leave it closed.
1796 */
1797 if (qf_lists[qf_curlist].qf_nonevalid || qf_curlist >= qf_listcount)
1798 {
1799 if (win != NULL)
1800 ex_cclose(eap);
1801 }
1802 else if (win == NULL)
1803 ex_copen(eap);
1804}
1805
1806/*
1807 * ":cclose": close the window showing the list of errors.
1808 */
1809/*ARGSUSED*/
1810 void
1811ex_cclose(eap)
1812 exarg_T *eap;
1813{
1814 win_T *win;
1815
1816 /*
1817 * Find existing quickfix window and close it.
1818 */
1819 for (win = firstwin; win != NULL; win = win->w_next)
1820 if (bt_quickfix(win->w_buffer))
1821 break;
1822
1823 if (win != NULL)
1824 win_close(win, FALSE);
1825}
1826
1827/*
1828 * ":copen": open a window that shows the list of errors.
1829 */
1830 void
1831ex_copen(eap)
1832 exarg_T *eap;
1833{
1834 int height;
1835 buf_T *buf;
1836 win_T *win;
1837
1838 if (eap->addr_count != 0)
1839 height = eap->line2;
1840 else
1841 height = QF_WINHEIGHT;
1842
1843#ifdef FEAT_VISUAL
1844 reset_VIsual_and_resel(); /* stop Visual mode */
1845#endif
1846#ifdef FEAT_GUI
1847 need_mouse_correct = TRUE;
1848#endif
1849
1850 /*
1851 * Find existing quickfix window, or open a new one.
1852 */
1853 for (win = firstwin; win != NULL; win = win->w_next)
1854 if (bt_quickfix(win->w_buffer))
1855 break;
1856 if (win != NULL)
1857 win_goto(win);
1858 else
1859 {
1860 /* The current window becomes the previous window afterwards. */
1861 win = curwin;
1862
1863 /* Create the new window at the very bottom. */
1864 win_goto(lastwin);
1865 if (win_split(height, WSP_BELOW) == FAIL)
1866 return; /* not enough room for window */
1867#ifdef FEAT_SCROLLBIND
1868 curwin->w_p_scb = FALSE;
1869#endif
1870
1871 /*
1872 * Find existing quickfix buffer, or create a new one.
1873 */
1874 buf = qf_find_buf();
1875 if (buf == NULL)
1876 {
1877 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
1878 /* switch off 'swapfile' */
1879 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
1880 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
1881 OPT_LOCAL);
1882 set_option_value((char_u *)"bh", 0L, (char_u *)"delete", OPT_LOCAL);
1883 set_option_value((char_u *)"diff", 0L, (char_u *)"", OPT_LOCAL);
1884 }
1885 else if (buf != curbuf)
1886 set_curbuf(buf, DOBUF_GOTO);
1887
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00001888#ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 /* Only set the height when there is no window to the side. */
1890 if (curwin->w_width == Columns)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00001891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 win_setheight(height);
1893 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
1894 if (win_valid(win))
1895 prevwin = win;
1896 }
1897
1898 /*
1899 * Fill the buffer with the quickfix list.
1900 */
1901 qf_fill_buffer();
1902
1903 curwin->w_cursor.lnum = qf_lists[qf_curlist].qf_index;
1904 curwin->w_cursor.col = 0;
1905 check_cursor();
1906 update_topline(); /* scroll to show the line */
1907}
1908
1909/*
1910 * Return the number of the current entry (line number in the quickfix
1911 * window).
1912 */
1913 linenr_T
1914qf_current_entry()
1915{
1916 return qf_lists[qf_curlist].qf_index;
1917}
1918
1919/*
1920 * Update the cursor position in the quickfix window to the current error.
1921 * Return TRUE if there is a quickfix window.
1922 */
1923 static int
1924qf_win_pos_update(old_qf_index)
1925 int old_qf_index; /* previous qf_index or zero */
1926{
1927 win_T *win;
1928 int qf_index = qf_lists[qf_curlist].qf_index;
1929
1930 /*
1931 * Put the cursor on the current error in the quickfix window, so that
1932 * it's viewable.
1933 */
1934 for (win = firstwin; win != NULL; win = win->w_next)
1935 if (bt_quickfix(win->w_buffer))
1936 break;
1937 if (win != NULL
1938 && qf_index <= win->w_buffer->b_ml.ml_line_count
1939 && old_qf_index != qf_index)
1940 {
1941 win_T *old_curwin = curwin;
1942
1943 curwin = win;
1944 curbuf = win->w_buffer;
1945 if (qf_index > old_qf_index)
1946 {
1947 curwin->w_redraw_top = old_qf_index;
1948 curwin->w_redraw_bot = qf_index;
1949 }
1950 else
1951 {
1952 curwin->w_redraw_top = qf_index;
1953 curwin->w_redraw_bot = old_qf_index;
1954 }
1955 curwin->w_cursor.lnum = qf_index;
1956 curwin->w_cursor.col = 0;
1957 update_topline(); /* scroll to show the line */
1958 redraw_later(VALID);
1959 curwin->w_redr_status = TRUE; /* update ruler */
1960 curwin = old_curwin;
1961 curbuf = curwin->w_buffer;
1962 }
1963 return win != NULL;
1964}
1965
1966/*
1967 * Find quickfix buffer.
1968 */
1969 static buf_T *
1970qf_find_buf()
1971{
1972 buf_T *buf;
1973
1974 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1975 if (bt_quickfix(buf))
1976 break;
1977 return buf;
1978}
1979
1980/*
1981 * Find the quickfix buffer. If it exists, update the contents.
1982 */
1983 static void
1984qf_update_buffer()
1985{
1986 buf_T *buf;
1987#ifdef FEAT_AUTOCMD
1988 aco_save_T aco;
1989#else
1990 buf_T *save_curbuf;
1991#endif
1992
1993 /* Check if a buffer for the quickfix list exists. Update it. */
1994 buf = qf_find_buf();
1995 if (buf != NULL)
1996 {
1997#ifdef FEAT_AUTOCMD
1998 /* set curwin/curbuf to buf and save a few things */
1999 aucmd_prepbuf(&aco, buf);
2000#else
2001 save_curbuf = curbuf;
2002 curbuf = buf;
2003#endif
2004
2005 qf_fill_buffer();
2006
2007#ifdef FEAT_AUTOCMD
2008 /* restore curwin/curbuf and a few other things */
2009 aucmd_restbuf(&aco);
2010#else
2011 curbuf = save_curbuf;
2012#endif
2013
2014 (void)qf_win_pos_update(0);
2015 }
2016}
2017
2018/*
2019 * Fill current buffer with quickfix errors, replacing any previous contents.
2020 * curbuf must be the quickfix buffer!
2021 */
2022 static void
2023qf_fill_buffer()
2024{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002025 linenr_T lnum;
2026 qfline_T *qfp;
2027 buf_T *errbuf;
2028 int len;
2029 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030
2031 /* delete all existing lines */
2032 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2033 (void)ml_delete((linenr_T)1, FALSE);
2034
2035 /* Check if there is anything to display */
2036 if (qf_curlist < qf_listcount)
2037 {
2038 /* Add one line for each error */
2039 qfp = qf_lists[qf_curlist].qf_start;
2040 for (lnum = 0; lnum < qf_lists[qf_curlist].qf_count; ++lnum)
2041 {
2042 if (qfp->qf_fnum != 0
2043 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2044 && errbuf->b_fname != NULL)
2045 {
2046 if (qfp->qf_type == 1) /* :helpgrep */
2047 STRCPY(IObuff, gettail(errbuf->b_fname));
2048 else
2049 STRCPY(IObuff, errbuf->b_fname);
2050 len = (int)STRLEN(IObuff);
2051 }
2052 else
2053 len = 0;
2054 IObuff[len++] = '|';
2055
2056 if (qfp->qf_lnum > 0)
2057 {
2058 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2059 len += (int)STRLEN(IObuff + len);
2060
2061 if (qfp->qf_col > 0)
2062 {
2063 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2064 len += (int)STRLEN(IObuff + len);
2065 }
2066
2067 sprintf((char *)IObuff + len, "%s",
2068 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2069 len += (int)STRLEN(IObuff + len);
2070 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002071 else if (qfp->qf_pattern != NULL)
2072 {
2073 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2074 len += (int)STRLEN(IObuff + len);
2075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 IObuff[len++] = '|';
2077 IObuff[len++] = ' ';
2078
2079 /* Remove newlines and leading whitespace from the text.
2080 * For an unrecognized line keep the indent, the compiler may
2081 * mark a word with ^^^^. */
2082 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2083 IObuff + len, IOSIZE - len);
2084
2085 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2086 == FAIL)
2087 break;
2088 qfp = qfp->qf_next;
2089 }
2090 /* Delete the empty line which is now at the end */
2091 (void)ml_delete(lnum + 1, FALSE);
2092 }
2093
2094 /* correct cursor position */
2095 check_lnums(TRUE);
2096
2097 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2098 * resembles reading a file into a buffer, it's more logical when using
2099 * autocommands. */
2100 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2101 curbuf->b_p_ma = FALSE;
2102
2103#ifdef FEAT_AUTOCMD
2104 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2105 FALSE, curbuf);
2106 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2107 FALSE, curbuf);
2108#endif
2109
2110 /* make sure it will be redrawn */
2111 redraw_curbuf_later(NOT_VALID);
2112
2113 /* Restore KeyTyped, setting 'filetype' may reset it. */
2114 KeyTyped = old_KeyTyped;
2115}
2116
2117#endif /* FEAT_WINDOWS */
2118
2119/*
2120 * Return TRUE if "buf" is the quickfix buffer.
2121 */
2122 int
2123bt_quickfix(buf)
2124 buf_T *buf;
2125{
2126 return (buf->b_p_bt[0] == 'q');
2127}
2128
2129/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002130 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2131 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 */
2133 int
2134bt_nofile(buf)
2135 buf_T *buf;
2136{
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002137 return (buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2138 || buf->b_p_bt[0] == 'a';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139}
2140
2141/*
2142 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2143 */
2144 int
2145bt_dontwrite(buf)
2146 buf_T *buf;
2147{
2148 return (buf->b_p_bt[0] == 'n');
2149}
2150
2151 int
2152bt_dontwrite_msg(buf)
2153 buf_T *buf;
2154{
2155 if (bt_dontwrite(buf))
2156 {
2157 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2158 return TRUE;
2159 }
2160 return FALSE;
2161}
2162
2163/*
2164 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2165 * and 'bufhidden'.
2166 */
2167 int
2168buf_hide(buf)
2169 buf_T *buf;
2170{
2171 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2172 switch (buf->b_p_bh[0])
2173 {
2174 case 'u': /* "unload" */
2175 case 'w': /* "wipe" */
2176 case 'd': return FALSE; /* "delete" */
2177 case 'h': return TRUE; /* "hide" */
2178 }
2179 return (p_hid || cmdmod.hide);
2180}
2181
2182/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002183 * Return TRUE when using ":vimgrep" for ":grep".
2184 */
2185 int
Bram Moolenaar81695252004-12-29 20:58:21 +00002186grep_internal(cmdidx)
2187 cmdidx_T cmdidx;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002188{
Bram Moolenaar81695252004-12-29 20:58:21 +00002189 return ((cmdidx == CMD_grep || cmdidx == CMD_grepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002190 && STRCMP("internal",
2191 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2192}
2193
2194/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 * Used for ":make", ":grep" and ":grepadd".
2196 */
2197 void
2198ex_make(eap)
2199 exarg_T *eap;
2200{
Bram Moolenaar7c626922005-02-07 22:01:03 +00002201 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 char_u *cmd;
2203 unsigned len;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002204#ifdef FEAT_AUTOCMD
2205 char_u *au_name = NULL;
2206
2207 switch (eap->cmdidx)
2208 {
2209 case CMD_make: au_name = (char_u *)"make"; break;
2210 case CMD_grep: au_name = (char_u *)"grep"; break;
2211 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
2212 default: break;
2213 }
2214 if (au_name != NULL)
2215 {
2216 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2217 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00002218# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00002219 if (did_throw || force_abort)
2220 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00002221# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002222 }
2223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224
Bram Moolenaar86b68352004-12-27 21:59:20 +00002225 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
Bram Moolenaar81695252004-12-29 20:58:21 +00002226 if (grep_internal(eap->cmdidx))
Bram Moolenaar86b68352004-12-27 21:59:20 +00002227 {
2228 ex_vimgrep(eap);
2229 return;
2230 }
2231
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002233 fname = get_mef_name();
2234 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002236 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237
2238 /*
2239 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2240 */
2241 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2242 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002243 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 cmd = alloc(len);
2245 if (cmd == NULL)
2246 return;
2247 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2248 (char *)p_shq);
2249 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002250 append_redir(cmd, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 /*
2252 * Output a newline if there's something else than the :make command that
2253 * was typed (in which case the cursor is in column 0).
2254 */
2255 if (msg_col == 0)
2256 msg_didout = FALSE;
2257 msg_start();
2258 MSG_PUTS(":!");
2259 msg_outtrans(cmd); /* show what we are doing */
2260
2261 /* let the shell know if we are redirecting output or not */
2262 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2263
2264#ifdef AMIGA
2265 out_flush();
2266 /* read window status report and redraw before message */
2267 (void)char_avail();
2268#endif
2269
Bram Moolenaar7c626922005-02-07 22:01:03 +00002270 if (qf_init(fname, eap->cmdidx != CMD_make ? p_gefm : p_efm,
Bram Moolenaar86b68352004-12-27 21:59:20 +00002271 eap->cmdidx != CMD_grepadd) > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 && !eap->forceit)
2273 qf_jump(0, 0, FALSE); /* display first error */
2274
Bram Moolenaar7c626922005-02-07 22:01:03 +00002275 mch_remove(fname);
2276 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 vim_free(cmd);
Bram Moolenaar7c626922005-02-07 22:01:03 +00002278
2279#ifdef FEAT_AUTOCMD
2280 if (au_name != NULL)
2281 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2282 curbuf->b_fname, TRUE, curbuf);
2283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284}
2285
2286/*
2287 * Return the name for the errorfile, in allocated memory.
2288 * Find a new unique name when 'makeef' contains "##".
2289 * Returns NULL for error.
2290 */
2291 static char_u *
2292get_mef_name()
2293{
2294 char_u *p;
2295 char_u *name;
2296 static int start = -1;
2297 static int off = 0;
2298#ifdef HAVE_LSTAT
2299 struct stat sb;
2300#endif
2301
2302 if (*p_mef == NUL)
2303 {
2304 name = vim_tempname('e');
2305 if (name == NULL)
2306 EMSG(_(e_notmp));
2307 return name;
2308 }
2309
2310 for (p = p_mef; *p; ++p)
2311 if (p[0] == '#' && p[1] == '#')
2312 break;
2313
2314 if (*p == NUL)
2315 return vim_strsave(p_mef);
2316
2317 /* Keep trying until the name doesn't exist yet. */
2318 for (;;)
2319 {
2320 if (start == -1)
2321 start = mch_get_pid();
2322 else
2323 off += 19;
2324
2325 name = alloc((unsigned)STRLEN(p_mef) + 30);
2326 if (name == NULL)
2327 break;
2328 STRCPY(name, p_mef);
2329 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2330 STRCAT(name, p + 2);
2331 if (mch_getperm(name) < 0
2332#ifdef HAVE_LSTAT
2333 /* Don't accept a symbolic link, its a security risk. */
2334 && mch_lstat((char *)name, &sb) < 0
2335#endif
2336 )
2337 break;
2338 vim_free(name);
2339 }
2340 return name;
2341}
2342
2343/*
2344 * ":cc", ":crewind", ":cfirst" and ":clast".
2345 */
2346 void
2347ex_cc(eap)
2348 exarg_T *eap;
2349{
2350 qf_jump(0,
2351 eap->addr_count > 0
2352 ? (int)eap->line2
2353 : eap->cmdidx == CMD_cc
2354 ? 0
2355 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_cfirst)
2356 ? 1
2357 : 32767,
2358 eap->forceit);
2359}
2360
2361/*
2362 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
2363 */
2364 void
2365ex_cnext(eap)
2366 exarg_T *eap;
2367{
2368 qf_jump(eap->cmdidx == CMD_cnext
2369 ? FORWARD
2370 : eap->cmdidx == CMD_cnfile
2371 ? FORWARD_FILE
2372 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_cNfile)
2373 ? BACKWARD_FILE
2374 : BACKWARD,
2375 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
2376}
2377
2378/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002379 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 */
2381 void
2382ex_cfile(eap)
2383 exarg_T *eap;
2384{
2385 if (*eap->arg != NUL)
2386 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002387
2388 /*
2389 * This function is used by the :cfile, :cgetfile and :caddfile
2390 * commands.
2391 * :cfile always creates a new quickfix list and jumps to the
2392 * first error.
2393 * :cgetfile creates a new quickfix list but doesn't jump to the
2394 * first error.
2395 * :caddfile adds to an existing quickfix list. If there is no
2396 * quickfix list then a new list is created.
2397 */
2398 if (qf_init(p_ef, p_efm, eap->cmdidx != CMD_caddfile) > 0
2399 && eap->cmdidx == CMD_cfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 qf_jump(0, 0, eap->forceit); /* display first error */
2401}
2402
2403/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002404 * ":vimgrep {pattern} file(s)"
2405 */
2406 void
2407ex_vimgrep(eap)
2408 exarg_T *eap;
2409{
Bram Moolenaar81695252004-12-29 20:58:21 +00002410 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002411 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002412 char_u **fnames;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002413 char_u *s;
2414 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002415 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002416 qfline_T *prevp = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002417 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00002418 buf_T *buf;
2419 int duplicate_name = FALSE;
2420 int using_dummy;
2421 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002422 buf_T *first_match_buf = NULL;
2423 time_t seconds = 0;
2424#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2425 char_u *save_ei = NULL;
2426 aco_save_T aco;
2427#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002428#ifdef FEAT_AUTOCMD
2429 char_u *au_name = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002430 int flags = 0;
2431 colnr_T col;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002432
2433 switch (eap->cmdidx)
2434 {
2435 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
2436 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
2437 default: break;
2438 }
2439 if (au_name != NULL)
2440 {
2441 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2442 curbuf->b_fname, TRUE, curbuf);
2443 if (did_throw || force_abort)
2444 return;
2445 }
2446#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00002447
Bram Moolenaar81695252004-12-29 20:58:21 +00002448 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00002449 regmatch.regprog = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002450 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00002451 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002452 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00002453 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00002454 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00002455 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002456 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002457 if (regmatch.regprog == NULL)
2458 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002459 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00002460 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002461
2462 p = skipwhite(p);
2463 if (*p == NUL)
2464 {
2465 EMSG(_("E683: File name missing or invalid pattern"));
2466 goto theend;
2467 }
2468
2469 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_vimgrepadd)
2470 || qf_curlist == qf_listcount)
2471 /* make place for a new list */
2472 qf_new_list();
2473 else if (qf_lists[qf_curlist].qf_count > 0)
2474 /* Adding to existing list, find last entry. */
2475 for (prevp = qf_lists[qf_curlist].qf_start;
2476 prevp->qf_next != prevp; prevp = prevp->qf_next)
2477 ;
2478
2479 /* parse the list of arguments */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002480 if (get_arglist_exp(p, &fcount, &fnames) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002481 goto theend;
2482 if (fcount == 0)
2483 {
2484 EMSG(_(e_nomatch));
2485 goto theend;
2486 }
2487
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002488 seconds = (time_t)0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002489 for (fi = 0; fi < fcount && !got_int; ++fi)
2490 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002491 if (time(NULL) > seconds)
2492 {
2493 /* Display the file name every second or so. */
2494 seconds = time(NULL);
2495 msg_start();
Bram Moolenaara5373fa2005-09-09 19:47:12 +00002496 p = msg_strtrunc(fnames[fi], TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002497 if (p == NULL)
2498 msg_outtrans(fnames[fi]);
2499 else
2500 {
2501 msg_outtrans(p);
2502 vim_free(p);
2503 }
2504 msg_clr_eos();
2505 msg_didout = FALSE; /* overwrite this message */
2506 msg_nowait = TRUE; /* don't wait for this message */
2507 msg_col = 0;
2508 out_flush();
2509 }
2510
Bram Moolenaar81695252004-12-29 20:58:21 +00002511 buf = buflist_findname_exp(fnames[fi]);
2512 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
2513 {
2514 /* Remember that a buffer with this name already exists. */
2515 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002516 using_dummy = TRUE;
2517
2518#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2519 /* Don't do Filetype autocommands to avoid loading syntax and
2520 * indent scripts, a great speed improvement. */
2521 save_ei = au_event_disable(",Filetype");
2522#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00002523
2524 /* Load file into a buffer, so that 'fileencoding' is detected,
2525 * autocommands applied, etc. */
2526 buf = load_dummy_buffer(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002527
2528#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2529 au_event_restore(save_ei);
2530#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00002531 }
2532 else
2533 /* Use existing, loaded buffer. */
2534 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002535
Bram Moolenaar81695252004-12-29 20:58:21 +00002536 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002537 {
2538 if (!got_int)
2539 smsg((char_u *)_("Cannot open file \"%s\""), fnames[fi]);
2540 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00002541 else
2542 {
Bram Moolenaar81695252004-12-29 20:58:21 +00002543 found_match = FALSE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002544 /* Try for a match in all lines of the buffer. */
Bram Moolenaar81695252004-12-29 20:58:21 +00002545 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002546 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002547 /* For ":1vimgrep" look for multiple matches. */
2548 col = 0;
2549 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
2550 col) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002551 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00002552 if (qf_add_entry(&prevp,
2553 NULL, /* dir */
2554 fnames[fi],
Bram Moolenaar81695252004-12-29 20:58:21 +00002555 ml_get_buf(buf,
2556 regmatch.startpos[0].lnum + lnum, FALSE),
2557 regmatch.startpos[0].lnum + lnum,
2558 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00002559 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002560 NULL, /* search pattern */
Bram Moolenaar86b68352004-12-27 21:59:20 +00002561 0, /* nr */
2562 0, /* type */
2563 TRUE /* valid */
2564 ) == FAIL)
2565 {
2566 got_int = TRUE;
2567 break;
2568 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002569 else
2570 found_match = TRUE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002571 if ((flags & VGR_GLOBAL) == 0
2572 || regmatch.endpos[0].lnum > 0)
2573 break;
2574 col = regmatch.endpos[0].col
2575 + (col == regmatch.endpos[0].col);
2576 if (col > STRLEN(ml_get_buf(buf, lnum, FALSE)))
2577 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002578 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00002579 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00002580 if (got_int)
2581 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002582 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002583
2584 if (using_dummy)
2585 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002586 if (found_match && first_match_buf == NULL)
2587 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00002588 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002589 {
Bram Moolenaar81695252004-12-29 20:58:21 +00002590 /* Never keep a dummy buffer if there is another buffer
2591 * with the same name. */
2592 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002593 buf = NULL;
2594 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002595 else if (!buf_hide(buf))
2596 {
2597 /* When not hiding the buffer and no match was found we
2598 * don't need to remember the buffer, wipe it out. If
Bram Moolenaar05159a02005-02-26 23:04:13 +00002599 * there was a match and it wasn't the first one or we
2600 * won't jump there: only unload the buffer. */
Bram Moolenaar81695252004-12-29 20:58:21 +00002601 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002602 {
Bram Moolenaar81695252004-12-29 20:58:21 +00002603 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002604 buf = NULL;
2605 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002606 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002607 {
Bram Moolenaar81695252004-12-29 20:58:21 +00002608 unload_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002609 buf = NULL;
2610 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002611 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002612
2613#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2614 if (buf != NULL)
2615 {
2616 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaar748bf032005-02-02 23:04:36 +00002617 * need to be done now, in that buffer. And then the
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00002618 * modelines need to be done (again). */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002619 aucmd_prepbuf(&aco, buf);
2620 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
2621 buf->b_fname, TRUE, buf);
Bram Moolenaar748bf032005-02-02 23:04:36 +00002622 do_modelines(FALSE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002623 aucmd_restbuf(&aco);
2624 }
2625#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00002626 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00002627 }
2628 }
2629
2630 FreeWild(fcount, fnames);
2631
2632 qf_lists[qf_curlist].qf_nonevalid = FALSE;
2633 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
2634 qf_lists[qf_curlist].qf_index = 1;
2635
2636#ifdef FEAT_WINDOWS
2637 qf_update_buffer();
2638#endif
2639
2640 /* Jump to first match. */
2641 if (qf_lists[qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002642 {
2643 if ((flags & VGR_NOJUMP) == 0)
2644 qf_jump(0, 0, eap->forceit);
2645 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002646 else
2647 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002648
Bram Moolenaar7c626922005-02-07 22:01:03 +00002649#ifdef FEAT_AUTOCMD
2650 if (au_name != NULL)
2651 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2652 curbuf->b_fname, TRUE, curbuf);
2653#endif
2654
Bram Moolenaar86b68352004-12-27 21:59:20 +00002655theend:
2656 vim_free(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002657}
2658
2659/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00002660 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00002661 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00002662 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
2663 * If "s" is not NULL terminate the pattern with a NUL.
2664 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00002665 */
2666 char_u *
Bram Moolenaar05159a02005-02-26 23:04:13 +00002667skip_vimgrep_pat(p, s, flags)
2668 char_u *p;
2669 char_u **s;
2670 int *flags;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002671{
2672 int c;
2673
2674 if (vim_isIDc(*p))
2675 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002676 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00002677 if (s != NULL)
2678 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002679 p = skiptowhite(p);
2680 if (s != NULL && *p != NUL)
2681 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00002682 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002683 else
2684 {
2685 /* ":vimgrep /pattern/[g][j] fname" */
2686 if (s != NULL)
2687 *s = p + 1;
2688 c = *p;
2689 p = skip_regexp(p + 1, c, TRUE, NULL);
2690 if (*p != c)
2691 return NULL;
2692
2693 /* Truncate the pattern. */
2694 if (s != NULL)
2695 *p = NUL;
2696 ++p;
2697
2698 /* Find the flags */
2699 while (*p == 'g' || *p == 'j')
2700 {
2701 if (flags != NULL)
2702 {
2703 if (*p == 'g')
2704 *flags |= VGR_GLOBAL;
2705 else
2706 *flags |= VGR_NOJUMP;
2707 }
2708 ++p;
2709 }
2710 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00002711 return p;
2712}
2713
2714/*
Bram Moolenaar81695252004-12-29 20:58:21 +00002715 * Load file "fname" into a dummy buffer and return the buffer pointer.
2716 * Returns NULL if it fails.
2717 * Must call unload_dummy_buffer() or wipe_dummy_buffer() later!
2718 */
2719 static buf_T *
2720load_dummy_buffer(fname)
2721 char_u *fname;
2722{
2723 buf_T *newbuf;
2724 int failed = TRUE;
2725#ifdef FEAT_AUTOCMD
2726 aco_save_T aco;
2727#else
2728 buf_T *old_curbuf = curbuf;
2729#endif
2730
2731 /* Allocate a buffer without putting it in the buffer list. */
2732 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
2733 if (newbuf == NULL)
2734 return NULL;
2735
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00002736 /* Init the options. */
2737 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
2738
Bram Moolenaar81695252004-12-29 20:58:21 +00002739#ifdef FEAT_AUTOCMD
2740 /* set curwin/curbuf to buf and save a few things */
2741 aucmd_prepbuf(&aco, newbuf);
2742#else
2743 curbuf = newbuf;
2744 curwin->w_buffer = newbuf;
2745#endif
2746
2747 /* Need to set the filename for autocommands. */
2748 (void)setfname(curbuf, fname, NULL, FALSE);
2749
2750 if (ml_open() == OK)
2751 {
2752 /* Create swap file now to avoid the ATTENTION message. */
2753 check_need_swap(TRUE);
2754
2755 /* Remove the "dummy" flag, otherwise autocommands may not
2756 * work. */
2757 curbuf->b_flags &= ~BF_DUMMY;
2758
2759 if (readfile(fname, NULL,
2760 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
2761 NULL, READ_NEW | READ_DUMMY) == OK
2762 && !(curbuf->b_flags & BF_NEW))
2763 {
2764 failed = FALSE;
2765 if (curbuf != newbuf)
2766 {
2767 /* Bloody autocommands changed the buffer! */
2768 if (buf_valid(newbuf))
2769 wipe_buffer(newbuf, FALSE);
2770 newbuf = curbuf;
2771 }
2772 }
2773 }
2774
2775#ifdef FEAT_AUTOCMD
2776 /* restore curwin/curbuf and a few other things */
2777 aucmd_restbuf(&aco);
2778#else
2779 curbuf = old_curbuf;
2780 curwin->w_buffer = old_curbuf;
2781#endif
2782
2783 if (!buf_valid(newbuf))
2784 return NULL;
2785 if (failed)
2786 {
2787 wipe_dummy_buffer(newbuf);
2788 return NULL;
2789 }
2790 return newbuf;
2791}
2792
2793/*
2794 * Wipe out the dummy buffer that load_dummy_buffer() created.
2795 */
2796 static void
2797wipe_dummy_buffer(buf)
2798 buf_T *buf;
2799{
2800 if (curbuf != buf) /* safety check */
2801 wipe_buffer(buf, FALSE);
2802}
2803
2804/*
2805 * Unload the dummy buffer that load_dummy_buffer() created.
2806 */
2807 static void
2808unload_dummy_buffer(buf)
2809 buf_T *buf;
2810{
2811 if (curbuf != buf) /* safety check */
2812 close_buffer(NULL, buf, DOBUF_UNLOAD);
2813}
2814
Bram Moolenaar05159a02005-02-26 23:04:13 +00002815#if defined(FEAT_EVAL) || defined(PROTO)
2816/*
2817 * Add each quickfix error to list "list" as a dictionary.
2818 */
2819 int
2820get_errorlist(list)
2821 list_T *list;
2822{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002823 dict_T *dict;
2824 char_u buf[2];
2825 qfline_T *qfp;
2826 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002827
2828 if (qf_curlist >= qf_listcount || qf_lists[qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002829 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002830
2831 qfp = qf_lists[qf_curlist].qf_start;
2832 for (i = 1; !got_int && i <= qf_lists[qf_curlist].qf_count; ++i)
2833 {
2834 if ((dict = dict_alloc()) == NULL)
2835 return FAIL;
2836 if (list_append_dict(list, dict) == FAIL)
2837 return FAIL;
2838
2839 buf[0] = qfp->qf_type;
2840 buf[1] = NUL;
2841 if ( dict_add_nr_str(dict, "bufnr", (long)qfp->qf_fnum, NULL) == FAIL
2842 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
2843 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
2844 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
2845 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002846 || dict_add_nr_str(dict, "pattern", 0L, qfp->qf_pattern) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002847 || dict_add_nr_str(dict, "text", 0L, qfp->qf_text) == FAIL
2848 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
2849 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
2850 return FAIL;
2851
2852 qfp = qfp->qf_next;
2853 }
2854 return OK;
2855}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002856
2857/*
2858 * Populate the quickfix list with the items supplied in the list
2859 * of dictionaries.
2860 */
2861 int
Bram Moolenaar35c54e52005-05-20 21:25:31 +00002862set_errorlist(list, action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002863 list_T *list;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00002864 int action;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002865{
2866 listitem_T *li;
2867 dict_T *d;
2868 char_u *filename, *pattern, *text, *type;
2869 long lnum;
2870 int col, nr;
2871 int vcol;
2872 qfline_T *prevp = NULL;
2873 int valid, status;
2874 int retval = OK;
2875
Bram Moolenaar35c54e52005-05-20 21:25:31 +00002876 if (action == ' ' || qf_curlist == qf_listcount)
2877 /* make place for a new list */
2878 qf_new_list();
2879 else if (action == 'a' && qf_lists[qf_curlist].qf_count > 0)
2880 /* Adding to existing list, find last entry. */
2881 for (prevp = qf_lists[qf_curlist].qf_start;
2882 prevp->qf_next != prevp; prevp = prevp->qf_next)
2883 ;
2884 else if (action == 'r')
2885 qf_free(qf_curlist);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002886
2887 for (li = list->lv_first; li != NULL; li = li->li_next)
2888 {
2889 if (li->li_tv.v_type != VAR_DICT)
2890 continue; /* Skip non-dict items */
2891
2892 d = li->li_tv.vval.v_dict;
2893 if (d == NULL)
2894 continue;
2895
2896 filename = get_dict_string(d, (char_u *)"filename");
2897 lnum = get_dict_number(d, (char_u *)"lnum");
2898 col = get_dict_number(d, (char_u *)"col");
2899 vcol = get_dict_number(d, (char_u *)"vcol");
2900 nr = get_dict_number(d, (char_u *)"nr");
2901 type = get_dict_string(d, (char_u *)"type");
2902 pattern = get_dict_string(d, (char_u *)"pattern");
2903 text = get_dict_string(d, (char_u *)"text");
2904 if (text == NULL)
2905 text = vim_strsave((char_u *)"");
2906
2907 valid = TRUE;
2908 if (filename == NULL || (lnum == 0 && pattern == NULL))
2909 valid = FALSE;
2910
2911 status = qf_add_entry(&prevp,
2912 NULL, /* dir */
2913 filename,
2914 text,
2915 lnum,
2916 col,
2917 vcol, /* vis_col */
2918 pattern, /* search pattern */
2919 nr,
2920 type == NULL ? NUL : *type,
2921 valid);
2922
2923 vim_free(filename);
2924 vim_free(pattern);
2925 vim_free(text);
2926 vim_free(type);
2927
2928 if (status == FAIL)
2929 {
2930 retval = FAIL;
2931 break;
2932 }
2933 }
2934
2935 qf_lists[qf_curlist].qf_nonevalid = FALSE;
2936 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
2937 qf_lists[qf_curlist].qf_index = 1;
2938
2939#ifdef FEAT_WINDOWS
2940 qf_update_buffer();
2941#endif
2942
2943 return retval;
2944}
Bram Moolenaar05159a02005-02-26 23:04:13 +00002945#endif
2946
Bram Moolenaar81695252004-12-29 20:58:21 +00002947/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002948 * ":[range]cbuffer [bufnr]" command.
2949 */
2950 void
2951ex_cbuffer(eap)
2952 exarg_T *eap;
2953{
2954 buf_T *buf = NULL;
2955
2956 if (*eap->arg == NUL)
2957 buf = curbuf;
2958 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
2959 buf = buflist_findnr(atoi((char *)eap->arg));
2960 if (buf == NULL)
2961 EMSG(_(e_invarg));
2962 else if (buf->b_ml.ml_mfp == NULL)
2963 EMSG(_("E681: Buffer is not loaded"));
2964 else
2965 {
2966 if (eap->addr_count == 0)
2967 {
2968 eap->line1 = 1;
2969 eap->line2 = buf->b_ml.ml_line_count;
2970 }
2971 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
2972 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
2973 EMSG(_(e_invrange));
2974 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002975 qf_init_ext(NULL, buf, NULL, p_efm, TRUE, eap->line1, eap->line2);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002976 }
2977}
2978
Bram Moolenaar1e015462005-09-25 22:16:38 +00002979#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002980/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002981 * ":cexpr {expr}" command.
2982 */
2983 void
2984ex_cexpr(eap)
2985 exarg_T *eap;
2986{
2987 typval_T *tv;
2988
2989 tv = eval_expr(eap->arg, NULL);
2990 if (!tv || (tv->v_type != VAR_STRING && tv->v_type != VAR_LIST) ||
2991 (tv->v_type == VAR_STRING && !tv->vval.v_string) ||
2992 (tv->v_type == VAR_LIST && !tv->vval.v_list))
2993 return;
2994
2995 if (qf_init_ext(NULL, NULL, tv, p_efm, TRUE, (linenr_T)0, (linenr_T)0) > 0)
2996 qf_jump(0, 0, eap->forceit); /* display first error */
2997
2998 clear_tv(tv);
2999}
Bram Moolenaar1e015462005-09-25 22:16:38 +00003000#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003001
3002/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 * ":helpgrep {pattern}"
3004 */
3005 void
3006ex_helpgrep(eap)
3007 exarg_T *eap;
3008{
3009 regmatch_T regmatch;
3010 char_u *save_cpo;
3011 char_u *p;
3012 int fcount;
3013 char_u **fnames;
3014 FILE *fd;
3015 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003016 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003018#ifdef FEAT_MULTI_LANG
3019 char_u *lang;
3020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021
3022 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
3023 save_cpo = p_cpo;
3024 p_cpo = (char_u *)"";
3025
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003026#ifdef FEAT_MULTI_LANG
3027 /* Check for a specified language */
3028 lang = check_help_lang(eap->arg);
3029#endif
3030
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
3032 regmatch.rm_ic = FALSE;
3033 if (regmatch.regprog != NULL)
3034 {
3035 /* create a new quickfix list */
3036 qf_new_list();
3037
3038 /* Go through all directories in 'runtimepath' */
3039 p = p_rtp;
3040 while (*p != NUL && !got_int)
3041 {
3042 copy_option_part(&p, NameBuff, MAXPATHL, ",");
3043
3044 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
3045 add_pathsep(NameBuff);
3046 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
3047 if (gen_expand_wildcards(1, &NameBuff, &fcount,
3048 &fnames, EW_FILE|EW_SILENT) == OK
3049 && fcount > 0)
3050 {
3051 for (fi = 0; fi < fcount && !got_int; ++fi)
3052 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003053#ifdef FEAT_MULTI_LANG
3054 /* Skip files for a different language. */
3055 if (lang != NULL
3056 && STRNICMP(lang, fnames[fi]
3057 + STRLEN(fnames[fi]) - 3, 2) != 0
3058 && !(STRNICMP(lang, "en", 2) == 0
3059 && STRNICMP("txt", fnames[fi]
3060 + STRLEN(fnames[fi]) - 3, 3) == 0))
3061 continue;
3062#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003063 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 if (fd != NULL)
3065 {
3066 lnum = 1;
3067 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
3068 {
3069 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
3070 {
3071 int l = STRLEN(IObuff);
3072
3073 /* remove trailing CR, LF, spaces, etc. */
3074 while (l > 0 && IObuff[l - 1] <= ' ')
3075 IObuff[--l] = NUL;
3076
3077 if (qf_add_entry(&prevp,
3078 NULL, /* dir */
3079 fnames[fi],
3080 IObuff,
3081 lnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00003082 (int)(regmatch.startp[0] - IObuff)
3083 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00003084 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003085 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 0, /* nr */
3087 1, /* type */
3088 TRUE /* valid */
3089 ) == FAIL)
3090 {
3091 got_int = TRUE;
3092 break;
3093 }
3094 }
3095 ++lnum;
3096 line_breakcheck();
3097 }
3098 fclose(fd);
3099 }
3100 }
3101 FreeWild(fcount, fnames);
3102 }
3103 }
3104 vim_free(regmatch.regprog);
3105
3106 qf_lists[qf_curlist].qf_nonevalid = FALSE;
3107 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
3108 qf_lists[qf_curlist].qf_index = 1;
3109 }
3110
3111 p_cpo = save_cpo;
3112
3113#ifdef FEAT_WINDOWS
3114 qf_update_buffer();
3115#endif
3116
3117 /* Jump to first match. */
3118 if (qf_lists[qf_curlist].qf_count > 0)
3119 qf_jump(0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003120 else
3121 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122}
3123
3124#endif /* FEAT_QUICKFIX */