blob: 0c5a050d9be480d2ee70ba00bf3b1907b6348184 [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/*
27 * for each error the next struct is allocated and linked in a list
28 */
29struct qf_line
30{
31 struct qf_line *qf_next; /* pointer to next error in the list */
32 struct qf_line *qf_prev; /* pointer to previous error in the list */
33 linenr_T qf_lnum; /* line number where the error occurred */
34 int qf_fnum; /* file number for the line */
35 int qf_col; /* column where the error occurred */
36 int qf_nr; /* error number */
37 char_u *qf_text; /* description of the error */
38 char_u qf_virt_col; /* set to TRUE if qf_col is screen column */
39 char_u qf_cleared;/* set to TRUE if line has been deleted */
40 char_u qf_type; /* type of the error (mostly 'E'); 1 for
41 :helpgrep */
42 char_u qf_valid; /* valid error message detected */
43};
44
45/*
46 * There is a stack of error lists.
47 */
48#define LISTCOUNT 10
49
50struct qf_list
51{
52 struct qf_line *qf_start; /* pointer to the first error */
53 struct qf_line *qf_ptr; /* pointer to the current error */
54 int qf_count; /* number of errors (0 means no error list) */
55 int qf_index; /* current index in the error list */
56 int qf_nonevalid; /* TRUE if not a single valid entry found */
57} qf_lists[LISTCOUNT];
58
59static int qf_curlist = 0; /* current error list */
60static int qf_listcount = 0; /* current number of lists */
61
62#define FMT_PATTERNS 9 /* maximum number of % recognized */
63
64/*
65 * Structure used to hold the info of one part of 'errorformat'
66 */
67struct eformat
68{
69 regprog_T *prog; /* pre-formatted part of 'errorformat' */
70 struct eformat *next; /* pointer to next (NULL if last) */
71 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
72 char_u prefix; /* prefix of this format line: */
73 /* 'D' enter directory */
74 /* 'X' leave directory */
75 /* 'A' start of multi-line message */
76 /* 'E' error message */
77 /* 'W' warning message */
78 /* 'I' informational message */
79 /* 'C' continuation line */
80 /* 'Z' end of multi-line message */
81 /* 'G' general, unspecific message */
82 /* 'P' push file (partial) message */
83 /* 'Q' pop/quit file (partial) message */
84 /* 'O' overread (partial) message */
85 char_u flags; /* additional flags given in prefix */
86 /* '-' do not include this line */
87};
88
Bram Moolenaar86b68352004-12-27 21:59:20 +000089static int qf_init_ext __ARGS((char_u *efile, buf_T *buf, char_u *errorformat, int newlist, linenr_T lnumfirst, linenr_T lnumlast));
Bram Moolenaar071d4272004-06-13 20:20:40 +000090static void qf_new_list __ARGS((void));
91static int qf_add_entry __ARGS((struct qf_line **prevp, char_u *dir, char_u *fname, char_u *mesg, long lnum, int col, int virt_col, int nr, int type, int valid));
92static void qf_msg __ARGS((void));
93static void qf_free __ARGS((int idx));
94static char_u *qf_types __ARGS((int, int));
95static int qf_get_fnum __ARGS((char_u *, char_u *));
96static char_u *qf_push_dir __ARGS((char_u *, struct dir_stack_T **));
97static char_u *qf_pop_dir __ARGS((struct dir_stack_T **));
98static char_u *qf_guess_filepath __ARGS((char_u *));
99static void qf_fmt_text __ARGS((char_u *text, char_u *buf, int bufsize));
100static void qf_clean_dir_stack __ARGS((struct dir_stack_T **));
101#ifdef FEAT_WINDOWS
102static int qf_win_pos_update __ARGS((int old_qf_index));
103static buf_T *qf_find_buf __ARGS((void));
104static void qf_update_buffer __ARGS((void));
105static void qf_fill_buffer __ARGS((void));
106#endif
107static char_u *get_mef_name __ARGS((void));
Bram Moolenaar81695252004-12-29 20:58:21 +0000108static buf_T *load_dummy_buffer __ARGS((char_u *fname));
109static void wipe_dummy_buffer __ARGS((buf_T *buf));
110static void unload_dummy_buffer __ARGS((buf_T *buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111
112/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000113 * Read the errorfile "efile" into memory, line by line, building the error
114 * list.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 * Return -1 for error, number of errors for success.
116 */
117 int
118qf_init(efile, errorformat, newlist)
119 char_u *efile;
120 char_u *errorformat;
121 int newlist; /* TRUE: start a new error list */
122{
Bram Moolenaar86b68352004-12-27 21:59:20 +0000123 if (efile == NULL)
124 return FAIL;
125 return qf_init_ext(efile, curbuf, errorformat, newlist,
126 (linenr_T)0, (linenr_T)0);
127}
128
129/*
130 * Read the errorfile "efile" into memory, line by line, building the error
131 * list.
132 * Alternative: when "efile" is null read errors from buffer "buf".
133 * Always use 'errorformat' from "buf" if there is a local value.
134 * Then lnumfirst and lnumlast specify the range of lines to use.
135 * Return -1 for error, number of errors for success.
136 */
137 static int
138qf_init_ext(efile, buf, errorformat, newlist, lnumfirst, lnumlast)
139 char_u *efile;
140 buf_T *buf;
141 char_u *errorformat;
142 int newlist; /* TRUE: start a new error list */
143 linenr_T lnumfirst; /* first line number to use */
144 linenr_T lnumlast; /* last line number to use */
145{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 char_u *namebuf;
147 char_u *errmsg;
148 char_u *fmtstr = NULL;
149 int col = 0;
150 char_u use_virt_col = FALSE;
151 int type = 0;
152 int valid;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000153 linenr_T buflnum = lnumfirst;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 long lnum = 0L;
155 int enr = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000156 FILE *fd = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 struct qf_line *qfprev = NULL; /* init to make SASC shut up */
158 char_u *efmp;
159 struct eformat *fmt_first = NULL;
160 struct eformat *fmt_last = NULL;
161 struct eformat *fmt_ptr;
162 char_u *efm;
163 char_u *ptr;
164 char_u *srcptr;
165 int len;
166 int i;
167 int round;
168 int idx = 0;
169 int multiline = FALSE;
170 int multiignore = FALSE;
171 int multiscan = FALSE;
172 int retval = -1; /* default: return error flag */
173 char_u *directory = NULL;
174 char_u *currfile = NULL;
175 char_u *tail = NULL;
176 struct dir_stack_T *file_stack = NULL;
177 regmatch_T regmatch;
178 static struct fmtpattern
179 {
180 char_u convchar;
181 char *pattern;
182 } fmt_pat[FMT_PATTERNS] =
183 {
184 {'f', "\\f\\+"},
185 {'n', "\\d\\+"},
186 {'l', "\\d\\+"},
187 {'c', "\\d\\+"},
188 {'t', "."},
189 {'m', ".\\+"},
190 {'r', ".*"},
191 {'p', "[- .]*"},
192 {'v', "\\d\\+"}
193 };
194
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 namebuf = alloc(CMDBUFFSIZE + 1);
196 errmsg = alloc(CMDBUFFSIZE + 1);
197 if (namebuf == NULL || errmsg == NULL)
198 goto qf_init_end;
199
Bram Moolenaar86b68352004-12-27 21:59:20 +0000200 if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 {
202 EMSG2(_(e_openerrf), efile);
203 goto qf_init_end;
204 }
205
206 if (newlist || qf_curlist == qf_listcount)
207 /* make place for a new list */
208 qf_new_list();
209 else if (qf_lists[qf_curlist].qf_count > 0)
210 /* Adding to existing list, find last entry. */
211 for (qfprev = qf_lists[qf_curlist].qf_start;
212 qfprev->qf_next != qfprev; qfprev = qfprev->qf_next)
213 ;
214
215/*
216 * Each part of the format string is copied and modified from errorformat to
217 * regex prog. Only a few % characters are allowed.
218 */
219 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000220 if (errorformat == p_efm && *buf->b_p_efm != NUL)
221 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222 else
223 efm = errorformat;
224 /*
225 * Get some space to modify the format string into.
226 */
227 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
228 for (round = FMT_PATTERNS; round > 0; )
229 i += (int)STRLEN(fmt_pat[--round].pattern);
230#ifdef COLON_IN_FILENAME
231 i += 12; /* "%f" can become twelve chars longer */
232#else
233 i += 2; /* "%f" can become two chars longer */
234#endif
235 if ((fmtstr = alloc(i)) == NULL)
236 goto error2;
237
238 while (efm[0])
239 {
240 /*
241 * Allocate a new eformat structure and put it at the end of the list
242 */
243 fmt_ptr = (struct eformat *)alloc((unsigned)sizeof(struct eformat));
244 if (fmt_ptr == NULL)
245 goto error2;
246 if (fmt_first == NULL) /* first one */
247 fmt_first = fmt_ptr;
248 else
249 fmt_last->next = fmt_ptr;
250 fmt_last = fmt_ptr;
251 fmt_ptr->prefix = NUL;
252 fmt_ptr->flags = NUL;
253 fmt_ptr->next = NULL;
254 fmt_ptr->prog = NULL;
255 for (round = FMT_PATTERNS; round > 0; )
256 fmt_ptr->addr[--round] = NUL;
257 /* round is 0 now */
258
259 /*
260 * Isolate one part in the 'errorformat' option
261 */
262 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
263 if (efm[len] == '\\' && efm[len + 1] != NUL)
264 ++len;
265
266 /*
267 * Build regexp pattern from current 'errorformat' option
268 */
269 ptr = fmtstr;
270 *ptr++ = '^';
271 for (efmp = efm; efmp < efm + len; ++efmp)
272 {
273 if (*efmp == '%')
274 {
275 ++efmp;
276 for (idx = 0; idx < FMT_PATTERNS; ++idx)
277 if (fmt_pat[idx].convchar == *efmp)
278 break;
279 if (idx < FMT_PATTERNS)
280 {
281 if (fmt_ptr->addr[idx])
282 {
283 sprintf((char *)errmsg,
284 _("E372: Too many %%%c in format string"), *efmp);
285 EMSG(errmsg);
286 goto error2;
287 }
288 if ((idx
289 && idx < 6
290 && vim_strchr((char_u *)"DXOPQ",
291 fmt_ptr->prefix) != NULL)
292 || (idx == 6
293 && vim_strchr((char_u *)"OPQ",
294 fmt_ptr->prefix) == NULL))
295 {
296 sprintf((char *)errmsg,
297 _("E373: Unexpected %%%c in format string"), *efmp);
298 EMSG(errmsg);
299 goto error2;
300 }
301 fmt_ptr->addr[idx] = (char_u)++round;
302 *ptr++ = '\\';
303 *ptr++ = '(';
304#ifdef BACKSLASH_IN_FILENAME
305 if (*efmp == 'f')
306 {
307 /* Also match "c:" in the file name, even when
308 * checking for a colon next: "%f:".
309 * "\%(\a:\)\=" */
310 STRCPY(ptr, "\\%(\\a:\\)\\=");
311 ptr += 10;
312 }
313#endif
314 if (*efmp == 'f' && efmp[1] != NUL
315 && efmp[1] != '\\' && efmp[1] != '%')
316 {
317 /* A file name may contain spaces, but this isn't in
318 * "\f". use "[^x]\+" instead (x is next character) */
319 *ptr++ = '[';
320 *ptr++ = '^';
321 *ptr++ = efmp[1];
322 *ptr++ = ']';
323 *ptr++ = '\\';
324 *ptr++ = '+';
325 }
326 else
327 {
328 srcptr = (char_u *)fmt_pat[idx].pattern;
329 while ((*ptr = *srcptr++) != NUL)
330 ++ptr;
331 }
332 *ptr++ = '\\';
333 *ptr++ = ')';
334 }
335 else if (*efmp == '*')
336 {
337 if (*++efmp == '[' || *efmp == '\\')
338 {
339 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
340 {
341 if (efmp[1] == '^')
342 *ptr++ = *++efmp;
343 if (efmp < efm + len)
344 {
345 *ptr++ = *++efmp; /* could be ']' */
346 while (efmp < efm + len
347 && (*ptr++ = *++efmp) != ']')
348 /* skip */;
349 if (efmp == efm + len)
350 {
351 EMSG(_("E374: Missing ] in format string"));
352 goto error2;
353 }
354 }
355 }
356 else if (efmp < efm + len) /* %*\D, %*\s etc. */
357 *ptr++ = *++efmp;
358 *ptr++ = '\\';
359 *ptr++ = '+';
360 }
361 else
362 {
363 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
364 sprintf((char *)errmsg,
365 _("E375: Unsupported %%%c in format string"), *efmp);
366 EMSG(errmsg);
367 goto error2;
368 }
369 }
370 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
371 *ptr++ = *efmp; /* regexp magic characters */
372 else if (*efmp == '#')
373 *ptr++ = '*';
374 else if (efmp == efm + 1) /* analyse prefix */
375 {
376 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
377 fmt_ptr->flags = *efmp++;
378 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
379 fmt_ptr->prefix = *efmp;
380 else
381 {
382 sprintf((char *)errmsg,
383 _("E376: Invalid %%%c in format string prefix"), *efmp);
384 EMSG(errmsg);
385 goto error2;
386 }
387 }
388 else
389 {
390 sprintf((char *)errmsg,
391 _("E377: Invalid %%%c in format string"), *efmp);
392 EMSG(errmsg);
393 goto error2;
394 }
395 }
396 else /* copy normal character */
397 {
398 if (*efmp == '\\' && efmp + 1 < efm + len)
399 ++efmp;
400 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
401 *ptr++ = '\\'; /* escape regexp atoms */
402 if (*efmp)
403 *ptr++ = *efmp;
404 }
405 }
406 *ptr++ = '$';
407 *ptr = NUL;
408 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
409 goto error2;
410 /*
411 * Advance to next part
412 */
413 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
414 }
415 if (fmt_first == NULL) /* nothing found */
416 {
417 EMSG(_("E378: 'errorformat' contains no pattern"));
418 goto error2;
419 }
420
421 /*
422 * got_int is reset here, because it was probably set when killing the
423 * ":make" command, but we still want to read the errorfile then.
424 */
425 got_int = FALSE;
426
427 /* Always ignore case when looking for a matching error. */
428 regmatch.rm_ic = TRUE;
429
430 /*
431 * Read the lines in the error file one by one.
432 * Try to recognize one of the error formats in each line.
433 */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000434 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 {
Bram Moolenaar86b68352004-12-27 21:59:20 +0000436 /* Get the next line. */
437 if (fd == NULL)
438 {
439 if (buflnum > lnumlast)
440 break;
441 STRNCPY(IObuff, ml_get_buf(buf, buflnum++, FALSE), CMDBUFFSIZE - 2);
442 }
443 else if (fgets((char *)IObuff, CMDBUFFSIZE - 2, fd) == NULL)
444 break;
445
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 IObuff[CMDBUFFSIZE - 2] = NUL; /* for very long lines */
447 if ((efmp = vim_strrchr(IObuff, '\n')) != NULL)
448 *efmp = NUL;
449#ifdef USE_CRNL
450 if ((efmp = vim_strrchr(IObuff, '\r')) != NULL)
451 *efmp = NUL;
452#endif
453
454 /*
455 * Try to match each part of 'errorformat' until we find a complete
456 * match or no match.
457 */
458 valid = TRUE;
459restofline:
460 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
461 {
462 idx = fmt_ptr->prefix;
463 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
464 continue;
465 namebuf[0] = NUL;
466 if (!multiscan)
467 errmsg[0] = NUL;
468 lnum = 0;
469 col = 0;
470 use_virt_col = FALSE;
471 enr = -1;
472 type = 0;
473 tail = NULL;
474
475 regmatch.regprog = fmt_ptr->prog;
476 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
477 {
478 if ((idx == 'C' || idx == 'Z') && !multiline)
479 continue;
480 if (vim_strchr((char_u *)"EWI", idx) != NULL)
481 type = idx;
482 else
483 type = 0;
484 /*
485 * Extract error message data from matched line
486 */
487 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
488 {
489 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
490 STRNCPY(namebuf, regmatch.startp[i], len);
491 namebuf[len] = NUL;
492 if (vim_strchr((char_u *)"OPQ", idx) != NULL
493 && mch_getperm(namebuf) == -1)
494 continue;
495 }
496 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
497 enr = (int)atol((char *)regmatch.startp[i]);
498 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
499 lnum = atol((char *)regmatch.startp[i]);
500 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
501 col = (int)atol((char *)regmatch.startp[i]);
502 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
503 type = *regmatch.startp[i];
504 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
505 STRCPY(errmsg, IObuff);
506 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
507 {
508 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
509 STRNCPY(errmsg, regmatch.startp[i], len);
510 errmsg[len] = NUL;
511 }
512 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
513 tail = regmatch.startp[i];
514 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
515 {
516 col = (int)(regmatch.endp[i] - regmatch.startp[i] + 1);
517 if (*((char_u *)regmatch.startp[i]) != TAB)
518 use_virt_col = TRUE;
519 }
520 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
521 {
522 col = (int)atol((char *)regmatch.startp[i]);
523 use_virt_col = TRUE;
524 }
525 break;
526 }
527 }
528 multiscan = FALSE;
529 if (!fmt_ptr || idx == 'D' || idx == 'X')
530 {
531 if (fmt_ptr)
532 {
533 if (idx == 'D') /* enter directory */
534 {
535 if (*namebuf == NUL)
536 {
537 EMSG(_("E379: Missing or empty directory name"));
538 goto error2;
539 }
540 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
541 goto error2;
542 }
543 else if (idx == 'X') /* leave directory */
544 directory = qf_pop_dir(&dir_stack);
545 }
546 namebuf[0] = NUL; /* no match found, remove file name */
547 lnum = 0; /* don't jump to this line */
548 valid = FALSE;
549 STRCPY(errmsg, IObuff); /* copy whole line to error message */
550 if (!fmt_ptr)
551 multiline = multiignore = FALSE;
552 }
553 else if (fmt_ptr)
554 {
555 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
556 multiline = TRUE; /* start of a multi-line message */
557 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
558 { /* continuation of multi-line msg */
559 if (qfprev == NULL)
560 goto error2;
561 if (*errmsg && !multiignore)
562 {
563 len = (int)STRLEN(qfprev->qf_text);
564 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
565 == NULL)
566 goto error2;
567 STRCPY(ptr, qfprev->qf_text);
568 vim_free(qfprev->qf_text);
569 qfprev->qf_text = ptr;
570 *(ptr += len) = '\n';
571 STRCPY(++ptr, errmsg);
572 }
573 if (qfprev->qf_nr == -1)
574 qfprev->qf_nr = enr;
575 if (vim_isprintc(type) && !qfprev->qf_type)
576 qfprev->qf_type = type; /* only printable chars allowed */
577 if (!qfprev->qf_lnum)
578 qfprev->qf_lnum = lnum;
579 if (!qfprev->qf_col)
580 qfprev->qf_col = col;
581 qfprev->qf_virt_col = use_virt_col;
582 if (!qfprev->qf_fnum)
583 qfprev->qf_fnum = qf_get_fnum(directory,
584 *namebuf || directory ? namebuf
585 : currfile && valid ? currfile : 0);
586 if (idx == 'Z')
587 multiline = multiignore = FALSE;
588 line_breakcheck();
589 continue;
590 }
591 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
592 {
593 /* global file names */
594 valid = FALSE;
595 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
596 {
597 if (*namebuf && idx == 'P')
598 currfile = qf_push_dir(namebuf, &file_stack);
599 else if (idx == 'Q')
600 currfile = qf_pop_dir(&file_stack);
601 *namebuf = NUL;
602 if (tail && *tail)
603 {
604 STRCPY(IObuff, skipwhite(tail));
605 multiscan = TRUE;
606 goto restofline;
607 }
608 }
609 }
610 if (fmt_ptr->flags == '-') /* generally exclude this line */
611 {
612 if (multiline)
613 multiignore = TRUE; /* also exclude continuation lines */
614 continue;
615 }
616 }
617
618 if (qf_add_entry(&qfprev,
619 directory,
620 *namebuf || directory
621 ? namebuf
622 : currfile && valid ? currfile : NULL,
623 errmsg,
624 lnum,
625 col,
626 use_virt_col,
627 enr,
628 type,
629 valid) == FAIL)
630 goto error2;
631 line_breakcheck();
632 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000633 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 {
635 if (qf_lists[qf_curlist].qf_index == 0) /* no valid entry found */
636 {
637 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
638 qf_lists[qf_curlist].qf_index = 1;
639 qf_lists[qf_curlist].qf_nonevalid = TRUE;
640 }
641 else
642 {
643 qf_lists[qf_curlist].qf_nonevalid = FALSE;
644 if (qf_lists[qf_curlist].qf_ptr == NULL)
645 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
646 }
647 retval = qf_lists[qf_curlist].qf_count; /* return number of matches */
648 goto qf_init_ok;
649 }
650 EMSG(_(e_readerrf));
651error2:
652 qf_free(qf_curlist);
653 qf_listcount--;
654 if (qf_curlist > 0)
655 --qf_curlist;
656qf_init_ok:
Bram Moolenaar86b68352004-12-27 21:59:20 +0000657 if (fd != NULL)
658 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
660 {
661 fmt_first = fmt_ptr->next;
662 vim_free(fmt_ptr->prog);
663 vim_free(fmt_ptr);
664 }
665 qf_clean_dir_stack(&dir_stack);
666 qf_clean_dir_stack(&file_stack);
667qf_init_end:
668 vim_free(namebuf);
669 vim_free(errmsg);
670 vim_free(fmtstr);
671
672#ifdef FEAT_WINDOWS
673 qf_update_buffer();
674#endif
675
676 return retval;
677}
678
679/*
680 * Prepare for adding a new quickfix list.
681 */
682 static void
683qf_new_list()
684{
685 int i;
686
687 /*
688 * If the current entry is not the last entry, delete entries below
689 * the current entry. This makes it possible to browse in a tree-like
690 * way with ":grep'.
691 */
692 while (qf_listcount > qf_curlist + 1)
693 qf_free(--qf_listcount);
694
695 /*
696 * When the stack is full, remove to oldest entry
697 * Otherwise, add a new entry.
698 */
699 if (qf_listcount == LISTCOUNT)
700 {
701 qf_free(0);
702 for (i = 1; i < LISTCOUNT; ++i)
703 qf_lists[i - 1] = qf_lists[i];
704 qf_curlist = LISTCOUNT - 1;
705 }
706 else
707 qf_curlist = qf_listcount++;
708 qf_lists[qf_curlist].qf_index = 0;
709 qf_lists[qf_curlist].qf_count = 0;
710}
711
712/*
713 * Add an entry to the end of the list of errors.
714 * Returns OK or FAIL.
715 */
716 static int
717qf_add_entry(prevp, dir, fname, mesg, lnum, col, virt_col, nr, type, valid)
718 struct qf_line **prevp; /* pointer to previously added entry or NULL */
719 char_u *dir; /* optional directory name */
720 char_u *fname; /* file name or NULL */
721 char_u *mesg; /* message */
722 long lnum; /* line number */
723 int col; /* column */
724 int virt_col; /* using virtual column */
725 int nr; /* error number */
726 int type; /* type character */
727 int valid; /* valid entry */
728{
729 struct qf_line *qfp;
730
731 if ((qfp = (struct qf_line *)alloc((unsigned)sizeof(struct qf_line)))
732 == NULL)
733 return FAIL;
734 qfp->qf_fnum = qf_get_fnum(dir, fname);
735 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
736 {
737 vim_free(qfp);
738 return FAIL;
739 }
740 qfp->qf_lnum = lnum;
741 qfp->qf_col = col;
742 qfp->qf_virt_col = virt_col;
743 qfp->qf_nr = nr;
744 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
745 type = 0;
746 qfp->qf_type = type;
747 qfp->qf_valid = valid;
748
749 if (qf_lists[qf_curlist].qf_count == 0) /* first element in the list */
750 {
751 qf_lists[qf_curlist].qf_start = qfp;
752 qfp->qf_prev = qfp; /* first element points to itself */
753 }
754 else
755 {
756 qfp->qf_prev = *prevp;
757 (*prevp)->qf_next = qfp;
758 }
759 qfp->qf_next = qfp; /* last element points to itself */
760 qfp->qf_cleared = FALSE;
761 *prevp = qfp;
762 ++qf_lists[qf_curlist].qf_count;
763 if (qf_lists[qf_curlist].qf_index == 0 && qfp->qf_valid)
764 /* first valid entry */
765 {
766 qf_lists[qf_curlist].qf_index = qf_lists[qf_curlist].qf_count;
767 qf_lists[qf_curlist].qf_ptr = qfp;
768 }
769
770 return OK;
771}
772
773/*
774 * get buffer number for file "dir.name"
775 */
776 static int
777qf_get_fnum(directory, fname)
778 char_u *directory;
779 char_u *fname;
780{
781 if (fname == NULL || *fname == NUL) /* no file name */
782 return 0;
783 {
784#ifdef RISCOS
785 /* Name is reported as `main.c', but file is `c.main' */
786 return ro_buflist_add(fname);
787#else
788 char_u *ptr;
789 int fnum;
790
791# ifdef VMS
792 vms_remove_version(fname);
793# endif
794# ifdef BACKSLASH_IN_FILENAME
795 if (directory != NULL)
796 slash_adjust(directory);
797 slash_adjust(fname);
798# endif
799 if (directory != NULL && !vim_isAbsName(fname)
800 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
801 {
802 /*
803 * Here we check if the file really exists.
804 * This should normally be true, but if make works without
805 * "leaving directory"-messages we might have missed a
806 * directory change.
807 */
808 if (mch_getperm(ptr) < 0)
809 {
810 vim_free(ptr);
811 directory = qf_guess_filepath(fname);
812 if (directory)
813 ptr = concat_fnames(directory, fname, TRUE);
814 else
815 ptr = vim_strsave(fname);
816 }
817 /* Use concatenated directory name and file name */
818 fnum = buflist_add(ptr, 0);
819 vim_free(ptr);
820 return fnum;
821 }
822 return buflist_add(fname, 0);
823#endif
824 }
825}
826
827/*
828 * push dirbuf onto the directory stack and return pointer to actual dir or
829 * NULL on error
830 */
831 static char_u *
832qf_push_dir(dirbuf, stackptr)
833 char_u *dirbuf;
834 struct dir_stack_T **stackptr;
835{
836 struct dir_stack_T *ds_new;
837 struct dir_stack_T *ds_ptr;
838
839 /* allocate new stack element and hook it in */
840 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
841 if (ds_new == NULL)
842 return NULL;
843
844 ds_new->next = *stackptr;
845 *stackptr = ds_new;
846
847 /* store directory on the stack */
848 if (vim_isAbsName(dirbuf)
849 || (*stackptr)->next == NULL
850 || (*stackptr && dir_stack != *stackptr))
851 (*stackptr)->dirname = vim_strsave(dirbuf);
852 else
853 {
854 /* Okay we don't have an absolute path.
855 * dirbuf must be a subdir of one of the directories on the stack.
856 * Let's search...
857 */
858 ds_new = (*stackptr)->next;
859 (*stackptr)->dirname = NULL;
860 while (ds_new)
861 {
862 vim_free((*stackptr)->dirname);
863 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
864 TRUE);
865 if (mch_isdir((*stackptr)->dirname) == TRUE)
866 break;
867
868 ds_new = ds_new->next;
869 }
870
871 /* clean up all dirs we already left */
872 while ((*stackptr)->next != ds_new)
873 {
874 ds_ptr = (*stackptr)->next;
875 (*stackptr)->next = (*stackptr)->next->next;
876 vim_free(ds_ptr->dirname);
877 vim_free(ds_ptr);
878 }
879
880 /* Nothing found -> it must be on top level */
881 if (ds_new == NULL)
882 {
883 vim_free((*stackptr)->dirname);
884 (*stackptr)->dirname = vim_strsave(dirbuf);
885 }
886 }
887
888 if ((*stackptr)->dirname != NULL)
889 return (*stackptr)->dirname;
890 else
891 {
892 ds_ptr = *stackptr;
893 *stackptr = (*stackptr)->next;
894 vim_free(ds_ptr);
895 return NULL;
896 }
897}
898
899
900/*
901 * pop dirbuf from the directory stack and return previous directory or NULL if
902 * stack is empty
903 */
904 static char_u *
905qf_pop_dir(stackptr)
906 struct dir_stack_T **stackptr;
907{
908 struct dir_stack_T *ds_ptr;
909
910 /* TODO: Should we check if dirbuf is the directory on top of the stack?
911 * What to do if it isn't? */
912
913 /* pop top element and free it */
914 if (*stackptr != NULL)
915 {
916 ds_ptr = *stackptr;
917 *stackptr = (*stackptr)->next;
918 vim_free(ds_ptr->dirname);
919 vim_free(ds_ptr);
920 }
921
922 /* return NEW top element as current dir or NULL if stack is empty*/
923 return *stackptr ? (*stackptr)->dirname : NULL;
924}
925
926/*
927 * clean up directory stack
928 */
929 static void
930qf_clean_dir_stack(stackptr)
931 struct dir_stack_T **stackptr;
932{
933 struct dir_stack_T *ds_ptr;
934
935 while ((ds_ptr = *stackptr) != NULL)
936 {
937 *stackptr = (*stackptr)->next;
938 vim_free(ds_ptr->dirname);
939 vim_free(ds_ptr);
940 }
941}
942
943/*
944 * Check in which directory of the directory stack the given file can be
945 * found.
946 * Returns a pointer to the directory name or NULL if not found
947 * Cleans up intermediate directory entries.
948 *
949 * TODO: How to solve the following problem?
950 * If we have the this directory tree:
951 * ./
952 * ./aa
953 * ./aa/bb
954 * ./bb
955 * ./bb/x.c
956 * and make says:
957 * making all in aa
958 * making all in bb
959 * x.c:9: Error
960 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
961 * qf_guess_filepath will return NULL.
962 */
963 static char_u *
964qf_guess_filepath(filename)
965 char_u *filename;
966{
967 struct dir_stack_T *ds_ptr;
968 struct dir_stack_T *ds_tmp;
969 char_u *fullname;
970
971 /* no dirs on the stack - there's nothing we can do */
972 if (dir_stack == NULL)
973 return NULL;
974
975 ds_ptr = dir_stack->next;
976 fullname = NULL;
977 while (ds_ptr)
978 {
979 vim_free(fullname);
980 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
981
982 /* If concat_fnames failed, just go on. The worst thing that can happen
983 * is that we delete the entire stack.
984 */
985 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
986 break;
987
988 ds_ptr = ds_ptr->next;
989 }
990
991 vim_free(fullname);
992
993 /* clean up all dirs we already left */
994 while (dir_stack->next != ds_ptr)
995 {
996 ds_tmp = dir_stack->next;
997 dir_stack->next = dir_stack->next->next;
998 vim_free(ds_tmp->dirname);
999 vim_free(ds_tmp);
1000 }
1001
1002 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1003
1004}
1005
1006/*
1007 * jump to a quickfix line
1008 * if dir == FORWARD go "errornr" valid entries forward
1009 * if dir == BACKWARD go "errornr" valid entries backward
1010 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1011 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1012 * else if "errornr" is zero, redisplay the same line
1013 * else go to entry "errornr"
1014 */
1015 void
1016qf_jump(dir, errornr, forceit)
1017 int dir;
1018 int errornr;
1019 int forceit;
1020{
1021 struct qf_line *qf_ptr;
1022 struct qf_line *old_qf_ptr;
1023 int qf_index;
1024 int old_qf_fnum;
1025 int old_qf_index;
1026 int prev_index;
1027 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1028 char_u *err = e_no_more_items;
1029 linenr_T i;
1030 buf_T *old_curbuf;
1031 linenr_T old_lnum;
1032 char_u *old_swb = p_swb;
1033 colnr_T screen_col;
1034 colnr_T char_col;
1035 char_u *line;
1036#ifdef FEAT_WINDOWS
1037 int opened_window = FALSE;
1038 win_T *win;
1039 win_T *altwin;
1040#endif
1041 int print_message = TRUE;
1042 int len;
1043#ifdef FEAT_FOLDING
1044 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1045#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001046 int ok = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047
1048 if (qf_curlist >= qf_listcount || qf_lists[qf_curlist].qf_count == 0)
1049 {
1050 EMSG(_(e_quickfix));
1051 return;
1052 }
1053
1054 qf_ptr = qf_lists[qf_curlist].qf_ptr;
1055 old_qf_ptr = qf_ptr;
1056 qf_index = qf_lists[qf_curlist].qf_index;
1057 old_qf_index = qf_index;
1058 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1059 {
1060 while (errornr--)
1061 {
1062 old_qf_ptr = qf_ptr;
1063 prev_index = qf_index;
1064 old_qf_fnum = qf_ptr->qf_fnum;
1065 do
1066 {
1067 if (qf_index == qf_lists[qf_curlist].qf_count
1068 || qf_ptr->qf_next == NULL)
1069 {
1070 qf_ptr = old_qf_ptr;
1071 qf_index = prev_index;
1072 if (err != NULL)
1073 {
1074 EMSG(_(err));
1075 goto theend;
1076 }
1077 errornr = 0;
1078 break;
1079 }
1080 ++qf_index;
1081 qf_ptr = qf_ptr->qf_next;
1082 } while ((!qf_lists[qf_curlist].qf_nonevalid && !qf_ptr->qf_valid)
1083 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1084 err = NULL;
1085 }
1086 }
1087 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1088 {
1089 while (errornr--)
1090 {
1091 old_qf_ptr = qf_ptr;
1092 prev_index = qf_index;
1093 old_qf_fnum = qf_ptr->qf_fnum;
1094 do
1095 {
1096 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1097 {
1098 qf_ptr = old_qf_ptr;
1099 qf_index = prev_index;
1100 if (err != NULL)
1101 {
1102 EMSG(_(err));
1103 goto theend;
1104 }
1105 errornr = 0;
1106 break;
1107 }
1108 --qf_index;
1109 qf_ptr = qf_ptr->qf_prev;
1110 } while ((!qf_lists[qf_curlist].qf_nonevalid && !qf_ptr->qf_valid)
1111 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1112 err = NULL;
1113 }
1114 }
1115 else if (errornr != 0) /* go to specified number */
1116 {
1117 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1118 {
1119 --qf_index;
1120 qf_ptr = qf_ptr->qf_prev;
1121 }
1122 while (errornr > qf_index && qf_index < qf_lists[qf_curlist].qf_count
1123 && qf_ptr->qf_next != NULL)
1124 {
1125 ++qf_index;
1126 qf_ptr = qf_ptr->qf_next;
1127 }
1128 }
1129
1130#ifdef FEAT_WINDOWS
1131 qf_lists[qf_curlist].qf_index = qf_index;
1132 if (qf_win_pos_update(old_qf_index))
1133 /* No need to print the error message if it's visible in the error
1134 * window */
1135 print_message = FALSE;
1136
1137 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001138 * For ":helpgrep" find a help window or open one.
1139 */
1140 if (qf_ptr->qf_type == 1 && !curwin->w_buffer->b_help)
1141 {
1142 win_T *wp;
1143 int n;
1144
1145 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1146 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1147 break;
1148 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1149 win_enter(wp, TRUE);
1150 else
1151 {
1152 /*
1153 * Split off help window; put it at far top if no position
1154 * specified, the current window is vertically split and narrow.
1155 */
1156 n = WSP_HELP;
1157# ifdef FEAT_VERTSPLIT
1158 if (cmdmod.split == 0 && curwin->w_width != Columns
1159 && curwin->w_width < 80)
1160 n |= WSP_TOP;
1161# endif
1162 if (win_split(0, n) == FAIL)
1163 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001164 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001165
1166 if (curwin->w_height < p_hh)
1167 win_setheight((int)p_hh);
1168 }
1169
1170 if (!p_im)
1171 restart_edit = 0; /* don't want insert mode in help file */
1172 }
1173
1174 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 * If currently in the quickfix window, find another window to show the
1176 * file in.
1177 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001178 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 {
1180 /*
1181 * If there is no file specified, we don't know where to go.
1182 * But do advance, otherwise ":cn" gets stuck.
1183 */
1184 if (qf_ptr->qf_fnum == 0)
1185 goto theend;
1186
1187 /*
1188 * If there is only one window, create a new one above the quickfix
1189 * window.
1190 */
1191 if (firstwin == lastwin)
1192 {
1193 if (win_split(0, WSP_ABOVE) == FAIL)
1194 goto failed; /* not enough room for window */
1195 opened_window = TRUE; /* close it when fail */
1196 p_swb = empty_option; /* don't split again */
1197# ifdef FEAT_SCROLLBIND
1198 curwin->w_p_scb = FALSE;
1199# endif
1200 }
1201 else
1202 {
1203 /*
1204 * Try to find a window that shows the right buffer.
1205 * Default to the window just above the quickfix buffer.
1206 */
1207 win = curwin;
1208 altwin = NULL;
1209 for (;;)
1210 {
1211 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1212 break;
1213 if (win->w_prev == NULL)
1214 win = lastwin; /* wrap around the top */
1215 else
1216 win = win->w_prev; /* go to previous window */
1217
1218 if (bt_quickfix(win->w_buffer))
1219 {
1220 /* Didn't find it, go to the window before the quickfix
1221 * window. */
1222 if (altwin != NULL)
1223 win = altwin;
1224 else if (curwin->w_prev != NULL)
1225 win = curwin->w_prev;
1226 else
1227 win = curwin->w_next;
1228 break;
1229 }
1230
1231 /* Remember a usable window. */
1232 if (altwin == NULL && !win->w_p_pvw
1233 && win->w_buffer->b_p_bt[0] == NUL)
1234 altwin = win;
1235 }
1236
1237 win_goto(win);
1238 }
1239 }
1240#endif
1241
1242 /*
1243 * If there is a file name,
1244 * read the wanted file if needed, and check autowrite etc.
1245 */
1246 old_curbuf = curbuf;
1247 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001248
1249 if (qf_ptr->qf_fnum != 0)
1250 {
1251 if (qf_ptr->qf_type == 1)
1252 {
1253 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1254 * set b_p_ro flag). */
1255 if (!can_abandon(curbuf, forceit))
1256 {
1257 EMSG(_(e_nowrtmsg));
1258 ok = FALSE;
1259 }
1260 else
1261 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
1262 ECMD_HIDE + ECMD_SET_HELP);
1263 }
1264 else
1265 ok = buflist_getfile(qf_ptr->qf_fnum,
1266 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
1267 }
1268
1269 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 {
1271 /* When not switched to another buffer, still need to set pc mark */
1272 if (curbuf == old_curbuf)
1273 setpcmark();
1274
1275 /*
1276 * Go to line with error, unless qf_lnum is 0.
1277 */
1278 i = qf_ptr->qf_lnum;
1279 if (i > 0)
1280 {
1281 if (i > curbuf->b_ml.ml_line_count)
1282 i = curbuf->b_ml.ml_line_count;
1283 curwin->w_cursor.lnum = i;
1284 }
1285 if (qf_ptr->qf_col > 0)
1286 {
1287 curwin->w_cursor.col = qf_ptr->qf_col - 1;
1288 if (qf_ptr->qf_virt_col == TRUE)
1289 {
1290 /*
1291 * Check each character from the beginning of the error
1292 * line up to the error column. For each tab character
1293 * found, reduce the error column value by the length of
1294 * a tab character.
1295 */
1296 line = ml_get_curline();
1297 screen_col = 0;
1298 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1299 {
1300 if (*line == NUL)
1301 break;
1302 if (*line++ == '\t')
1303 {
1304 curwin->w_cursor.col -= 7 - (screen_col % 8);
1305 screen_col += 8 - (screen_col % 8);
1306 }
1307 else
1308 ++screen_col;
1309 }
1310 }
1311 check_cursor();
1312 }
1313 else
1314 beginline(BL_WHITE | BL_FIX);
1315
1316#ifdef FEAT_FOLDING
1317 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1318 foldOpenCursor();
1319#endif
1320 if (print_message)
1321 {
1322 /* Update the screen before showing the message */
1323 update_topline_redraw();
1324 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
1325 qf_lists[qf_curlist].qf_count,
1326 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1327 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1328 /* Add the message, skipping leading whitespace and newlines. */
1329 len = (int)STRLEN(IObuff);
1330 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1331
1332 /* Output the message. Overwrite to avoid scrolling when the 'O'
1333 * flag is present in 'shortmess'; But when not jumping, print the
1334 * whole message. */
1335 i = msg_scroll;
1336 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1337 msg_scroll = TRUE;
1338 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1339 msg_scroll = FALSE;
1340 msg_attr_keep(IObuff, 0, TRUE);
1341 msg_scroll = i;
1342 }
1343 }
1344 else
1345 {
1346#ifdef FEAT_WINDOWS
1347 if (opened_window)
1348 win_close(curwin, TRUE); /* Close opened window */
1349#endif
1350 if (qf_ptr->qf_fnum != 0)
1351 {
1352 /*
1353 * Couldn't open file, so put index back where it was. This could
1354 * happen if the file was readonly and we changed something.
1355 */
1356#ifdef FEAT_WINDOWS
1357failed:
1358#endif
1359 qf_ptr = old_qf_ptr;
1360 qf_index = old_qf_index;
1361 }
1362 }
1363theend:
1364 qf_lists[qf_curlist].qf_ptr = qf_ptr;
1365 qf_lists[qf_curlist].qf_index = qf_index;
1366#ifdef FEAT_WINDOWS
1367 if (p_swb != old_swb && opened_window)
1368 {
1369 /* Restore old 'switchbuf' value, but not when an autocommand or
1370 * modeline has changed the value. */
1371 if (p_swb == empty_option)
1372 p_swb = old_swb;
1373 else
1374 free_string_option(old_swb);
1375 }
1376#endif
1377}
1378
1379/*
1380 * ":clist": list all errors
1381 */
1382 void
1383qf_list(eap)
1384 exarg_T *eap;
1385{
1386 buf_T *buf;
1387 char_u *fname;
1388 struct qf_line *qfp;
1389 int i;
1390 int idx1 = 1;
1391 int idx2 = -1;
1392 int need_return = TRUE;
1393 int last_printed = 1;
1394 char_u *arg = eap->arg;
1395 int all = eap->forceit; /* if not :cl!, only show
1396 recognised errors */
1397
1398 if (qf_curlist >= qf_listcount || qf_lists[qf_curlist].qf_count == 0)
1399 {
1400 EMSG(_(e_quickfix));
1401 return;
1402 }
1403 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
1404 {
1405 EMSG(_(e_trailing));
1406 return;
1407 }
1408 i = qf_lists[qf_curlist].qf_count;
1409 if (idx1 < 0)
1410 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
1411 if (idx2 < 0)
1412 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
1413
1414 more_back_used = TRUE;
1415 if (qf_lists[qf_curlist].qf_nonevalid)
1416 all = TRUE;
1417 qfp = qf_lists[qf_curlist].qf_start;
1418 for (i = 1; !got_int && i <= qf_lists[qf_curlist].qf_count; )
1419 {
1420 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
1421 {
1422 if (need_return)
1423 {
1424 msg_putchar('\n');
1425 need_return = FALSE;
1426 }
1427 if (more_back == 0)
1428 {
1429 fname = NULL;
1430 if (qfp->qf_fnum != 0
1431 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
1432 {
1433 fname = buf->b_fname;
1434 if (qfp->qf_type == 1) /* :helpgrep */
1435 fname = gettail(fname);
1436 }
1437 if (fname == NULL)
1438 sprintf((char *)IObuff, "%2d", i);
1439 else
1440 sprintf((char *)IObuff, "%2d %s", i, (char *)fname);
1441 msg_outtrans_attr(IObuff, i == qf_lists[qf_curlist].qf_index
1442 ? hl_attr(HLF_L) : hl_attr(HLF_D));
1443 if (qfp->qf_lnum == 0)
1444 IObuff[0] = NUL;
1445 else if (qfp->qf_col == 0)
1446 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
1447 else
1448 sprintf((char *)IObuff, ":%ld col %d",
1449 qfp->qf_lnum, qfp->qf_col);
1450 sprintf((char *)IObuff + STRLEN(IObuff), "%s: ",
1451 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
1452 msg_puts_attr(IObuff, hl_attr(HLF_N));
1453 /* Remove newlines and leading whitespace from the text.
1454 * For an unrecognized line keep the indent, the compiler may
1455 * mark a word with ^^^^. */
1456 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
1457 ? skipwhite(qfp->qf_text) : qfp->qf_text,
1458 IObuff, IOSIZE);
1459 msg_prt_line(IObuff);
1460 out_flush(); /* show one line at a time */
1461 need_return = TRUE;
1462 last_printed = i;
1463 }
1464 }
1465 if (more_back)
1466 {
1467 /* scrolling backwards from the more-prompt */
1468 /* TODO: compute the number of items from the screen lines */
1469 more_back = more_back * 2 - 1;
1470 while (i > last_printed - more_back && i > idx1)
1471 {
1472 do
1473 {
1474 qfp = qfp->qf_prev;
1475 --i;
1476 }
1477 while (i > idx1 && !qfp->qf_valid && !all);
1478 }
1479 more_back = 0;
1480 }
1481 else
1482 {
1483 qfp = qfp->qf_next;
1484 ++i;
1485 }
1486 ui_breakcheck();
1487 }
1488 more_back_used = FALSE;
1489}
1490
1491/*
1492 * Remove newlines and leading whitespace from an error message.
1493 * Put the result in "buf[bufsize]".
1494 */
1495 static void
1496qf_fmt_text(text, buf, bufsize)
1497 char_u *text;
1498 char_u *buf;
1499 int bufsize;
1500{
1501 int i;
1502 char_u *p = text;
1503
1504 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
1505 {
1506 if (*p == '\n')
1507 {
1508 buf[i] = ' ';
1509 while (*++p != NUL)
1510 if (!vim_iswhite(*p) && *p != '\n')
1511 break;
1512 }
1513 else
1514 buf[i] = *p++;
1515 }
1516 buf[i] = NUL;
1517}
1518
1519/*
1520 * ":colder [count]": Up in the quickfix stack.
1521 * ":cnewer [count]": Down in the quickfix stack.
1522 */
1523 void
1524qf_age(eap)
1525 exarg_T *eap;
1526{
1527 int count;
1528
1529 if (eap->addr_count != 0)
1530 count = eap->line2;
1531 else
1532 count = 1;
1533 while (count--)
1534 {
1535 if (eap->cmdidx == CMD_colder)
1536 {
1537 if (qf_curlist == 0)
1538 {
1539 EMSG(_("E380: At bottom of quickfix stack"));
1540 return;
1541 }
1542 --qf_curlist;
1543 }
1544 else
1545 {
1546 if (qf_curlist >= qf_listcount - 1)
1547 {
1548 EMSG(_("E381: At top of quickfix stack"));
1549 return;
1550 }
1551 ++qf_curlist;
1552 }
1553 }
1554 qf_msg();
1555}
1556
1557 static void
1558qf_msg()
1559{
1560 smsg((char_u *)_("error list %d of %d; %d errors"),
1561 qf_curlist + 1, qf_listcount, qf_lists[qf_curlist].qf_count);
1562#ifdef FEAT_WINDOWS
1563 qf_update_buffer();
1564#endif
1565}
1566
1567/*
1568 * free the error list
1569 */
1570 static void
1571qf_free(idx)
1572 int idx;
1573{
1574 struct qf_line *qfp;
1575
1576 while (qf_lists[idx].qf_count)
1577 {
1578 qfp = qf_lists[idx].qf_start->qf_next;
1579 vim_free(qf_lists[idx].qf_start->qf_text);
1580 vim_free(qf_lists[idx].qf_start);
1581 qf_lists[idx].qf_start = qfp;
1582 --qf_lists[idx].qf_count;
1583 }
1584}
1585
1586/*
1587 * qf_mark_adjust: adjust marks
1588 */
1589 void
1590qf_mark_adjust(line1, line2, amount, amount_after)
1591 linenr_T line1;
1592 linenr_T line2;
1593 long amount;
1594 long amount_after;
1595{
1596 int i;
1597 struct qf_line *qfp;
1598 int idx;
1599
1600 for (idx = 0; idx < qf_listcount; ++idx)
1601 if (qf_lists[idx].qf_count)
1602 for (i = 0, qfp = qf_lists[idx].qf_start;
1603 i < qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
1604 if (qfp->qf_fnum == curbuf->b_fnum)
1605 {
1606 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
1607 {
1608 if (amount == MAXLNUM)
1609 qfp->qf_cleared = TRUE;
1610 else
1611 qfp->qf_lnum += amount;
1612 }
1613 else if (amount_after && qfp->qf_lnum > line2)
1614 qfp->qf_lnum += amount_after;
1615 }
1616}
1617
1618/*
1619 * Make a nice message out of the error character and the error number:
1620 * char number message
1621 * e or E 0 " error"
1622 * w or W 0 " warning"
1623 * i or I 0 " info"
1624 * 0 0 ""
1625 * other 0 " c"
1626 * e or E n " error n"
1627 * w or W n " warning n"
1628 * i or I n " info n"
1629 * 0 n " error n"
1630 * other n " c n"
1631 * 1 x "" :helpgrep
1632 */
1633 static char_u *
1634qf_types(c, nr)
1635 int c, nr;
1636{
1637 static char_u buf[20];
1638 static char_u cc[3];
1639 char_u *p;
1640
1641 if (c == 'W' || c == 'w')
1642 p = (char_u *)" warning";
1643 else if (c == 'I' || c == 'i')
1644 p = (char_u *)" info";
1645 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
1646 p = (char_u *)" error";
1647 else if (c == 0 || c == 1)
1648 p = (char_u *)"";
1649 else
1650 {
1651 cc[0] = ' ';
1652 cc[1] = c;
1653 cc[2] = NUL;
1654 p = cc;
1655 }
1656
1657 if (nr <= 0)
1658 return p;
1659
1660 sprintf((char *)buf, "%s %3d", (char *)p, nr);
1661 return buf;
1662}
1663
1664#if defined(FEAT_WINDOWS) || defined(PROTO)
1665/*
1666 * ":cwindow": open the quickfix window if we have errors to display,
1667 * close it if not.
1668 */
1669 void
1670ex_cwindow(eap)
1671 exarg_T *eap;
1672{
1673 win_T *win;
1674
1675 /*
1676 * Look for an existing quickfix window.
1677 */
1678 for (win = firstwin; win != NULL; win = win->w_next)
1679 if (bt_quickfix(win->w_buffer))
1680 break;
1681
1682 /*
1683 * If a quickfix window is open but we have no errors to display,
1684 * close the window. If a quickfix window is not open, then open
1685 * it if we have errors; otherwise, leave it closed.
1686 */
1687 if (qf_lists[qf_curlist].qf_nonevalid || qf_curlist >= qf_listcount)
1688 {
1689 if (win != NULL)
1690 ex_cclose(eap);
1691 }
1692 else if (win == NULL)
1693 ex_copen(eap);
1694}
1695
1696/*
1697 * ":cclose": close the window showing the list of errors.
1698 */
1699/*ARGSUSED*/
1700 void
1701ex_cclose(eap)
1702 exarg_T *eap;
1703{
1704 win_T *win;
1705
1706 /*
1707 * Find existing quickfix window and close it.
1708 */
1709 for (win = firstwin; win != NULL; win = win->w_next)
1710 if (bt_quickfix(win->w_buffer))
1711 break;
1712
1713 if (win != NULL)
1714 win_close(win, FALSE);
1715}
1716
1717/*
1718 * ":copen": open a window that shows the list of errors.
1719 */
1720 void
1721ex_copen(eap)
1722 exarg_T *eap;
1723{
1724 int height;
1725 buf_T *buf;
1726 win_T *win;
1727
1728 if (eap->addr_count != 0)
1729 height = eap->line2;
1730 else
1731 height = QF_WINHEIGHT;
1732
1733#ifdef FEAT_VISUAL
1734 reset_VIsual_and_resel(); /* stop Visual mode */
1735#endif
1736#ifdef FEAT_GUI
1737 need_mouse_correct = TRUE;
1738#endif
1739
1740 /*
1741 * Find existing quickfix window, or open a new one.
1742 */
1743 for (win = firstwin; win != NULL; win = win->w_next)
1744 if (bt_quickfix(win->w_buffer))
1745 break;
1746 if (win != NULL)
1747 win_goto(win);
1748 else
1749 {
1750 /* The current window becomes the previous window afterwards. */
1751 win = curwin;
1752
1753 /* Create the new window at the very bottom. */
1754 win_goto(lastwin);
1755 if (win_split(height, WSP_BELOW) == FAIL)
1756 return; /* not enough room for window */
1757#ifdef FEAT_SCROLLBIND
1758 curwin->w_p_scb = FALSE;
1759#endif
1760
1761 /*
1762 * Find existing quickfix buffer, or create a new one.
1763 */
1764 buf = qf_find_buf();
1765 if (buf == NULL)
1766 {
1767 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
1768 /* switch off 'swapfile' */
1769 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
1770 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
1771 OPT_LOCAL);
1772 set_option_value((char_u *)"bh", 0L, (char_u *)"delete", OPT_LOCAL);
1773 set_option_value((char_u *)"diff", 0L, (char_u *)"", OPT_LOCAL);
1774 }
1775 else if (buf != curbuf)
1776 set_curbuf(buf, DOBUF_GOTO);
1777
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00001778#ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 /* Only set the height when there is no window to the side. */
1780 if (curwin->w_width == Columns)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00001781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 win_setheight(height);
1783 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
1784 if (win_valid(win))
1785 prevwin = win;
1786 }
1787
1788 /*
1789 * Fill the buffer with the quickfix list.
1790 */
1791 qf_fill_buffer();
1792
1793 curwin->w_cursor.lnum = qf_lists[qf_curlist].qf_index;
1794 curwin->w_cursor.col = 0;
1795 check_cursor();
1796 update_topline(); /* scroll to show the line */
1797}
1798
1799/*
1800 * Return the number of the current entry (line number in the quickfix
1801 * window).
1802 */
1803 linenr_T
1804qf_current_entry()
1805{
1806 return qf_lists[qf_curlist].qf_index;
1807}
1808
1809/*
1810 * Update the cursor position in the quickfix window to the current error.
1811 * Return TRUE if there is a quickfix window.
1812 */
1813 static int
1814qf_win_pos_update(old_qf_index)
1815 int old_qf_index; /* previous qf_index or zero */
1816{
1817 win_T *win;
1818 int qf_index = qf_lists[qf_curlist].qf_index;
1819
1820 /*
1821 * Put the cursor on the current error in the quickfix window, so that
1822 * it's viewable.
1823 */
1824 for (win = firstwin; win != NULL; win = win->w_next)
1825 if (bt_quickfix(win->w_buffer))
1826 break;
1827 if (win != NULL
1828 && qf_index <= win->w_buffer->b_ml.ml_line_count
1829 && old_qf_index != qf_index)
1830 {
1831 win_T *old_curwin = curwin;
1832
1833 curwin = win;
1834 curbuf = win->w_buffer;
1835 if (qf_index > old_qf_index)
1836 {
1837 curwin->w_redraw_top = old_qf_index;
1838 curwin->w_redraw_bot = qf_index;
1839 }
1840 else
1841 {
1842 curwin->w_redraw_top = qf_index;
1843 curwin->w_redraw_bot = old_qf_index;
1844 }
1845 curwin->w_cursor.lnum = qf_index;
1846 curwin->w_cursor.col = 0;
1847 update_topline(); /* scroll to show the line */
1848 redraw_later(VALID);
1849 curwin->w_redr_status = TRUE; /* update ruler */
1850 curwin = old_curwin;
1851 curbuf = curwin->w_buffer;
1852 }
1853 return win != NULL;
1854}
1855
1856/*
1857 * Find quickfix buffer.
1858 */
1859 static buf_T *
1860qf_find_buf()
1861{
1862 buf_T *buf;
1863
1864 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1865 if (bt_quickfix(buf))
1866 break;
1867 return buf;
1868}
1869
1870/*
1871 * Find the quickfix buffer. If it exists, update the contents.
1872 */
1873 static void
1874qf_update_buffer()
1875{
1876 buf_T *buf;
1877#ifdef FEAT_AUTOCMD
1878 aco_save_T aco;
1879#else
1880 buf_T *save_curbuf;
1881#endif
1882
1883 /* Check if a buffer for the quickfix list exists. Update it. */
1884 buf = qf_find_buf();
1885 if (buf != NULL)
1886 {
1887#ifdef FEAT_AUTOCMD
1888 /* set curwin/curbuf to buf and save a few things */
1889 aucmd_prepbuf(&aco, buf);
1890#else
1891 save_curbuf = curbuf;
1892 curbuf = buf;
1893#endif
1894
1895 qf_fill_buffer();
1896
1897#ifdef FEAT_AUTOCMD
1898 /* restore curwin/curbuf and a few other things */
1899 aucmd_restbuf(&aco);
1900#else
1901 curbuf = save_curbuf;
1902#endif
1903
1904 (void)qf_win_pos_update(0);
1905 }
1906}
1907
1908/*
1909 * Fill current buffer with quickfix errors, replacing any previous contents.
1910 * curbuf must be the quickfix buffer!
1911 */
1912 static void
1913qf_fill_buffer()
1914{
1915 linenr_T lnum;
1916 struct qf_line *qfp;
1917 buf_T *errbuf;
1918 int len;
1919 int old_KeyTyped = KeyTyped;
1920
1921 /* delete all existing lines */
1922 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
1923 (void)ml_delete((linenr_T)1, FALSE);
1924
1925 /* Check if there is anything to display */
1926 if (qf_curlist < qf_listcount)
1927 {
1928 /* Add one line for each error */
1929 qfp = qf_lists[qf_curlist].qf_start;
1930 for (lnum = 0; lnum < qf_lists[qf_curlist].qf_count; ++lnum)
1931 {
1932 if (qfp->qf_fnum != 0
1933 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
1934 && errbuf->b_fname != NULL)
1935 {
1936 if (qfp->qf_type == 1) /* :helpgrep */
1937 STRCPY(IObuff, gettail(errbuf->b_fname));
1938 else
1939 STRCPY(IObuff, errbuf->b_fname);
1940 len = (int)STRLEN(IObuff);
1941 }
1942 else
1943 len = 0;
1944 IObuff[len++] = '|';
1945
1946 if (qfp->qf_lnum > 0)
1947 {
1948 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
1949 len += (int)STRLEN(IObuff + len);
1950
1951 if (qfp->qf_col > 0)
1952 {
1953 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
1954 len += (int)STRLEN(IObuff + len);
1955 }
1956
1957 sprintf((char *)IObuff + len, "%s",
1958 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
1959 len += (int)STRLEN(IObuff + len);
1960 }
1961 IObuff[len++] = '|';
1962 IObuff[len++] = ' ';
1963
1964 /* Remove newlines and leading whitespace from the text.
1965 * For an unrecognized line keep the indent, the compiler may
1966 * mark a word with ^^^^. */
1967 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
1968 IObuff + len, IOSIZE - len);
1969
1970 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
1971 == FAIL)
1972 break;
1973 qfp = qfp->qf_next;
1974 }
1975 /* Delete the empty line which is now at the end */
1976 (void)ml_delete(lnum + 1, FALSE);
1977 }
1978
1979 /* correct cursor position */
1980 check_lnums(TRUE);
1981
1982 /* Set the 'filetype' to "qf" each time after filling the buffer. This
1983 * resembles reading a file into a buffer, it's more logical when using
1984 * autocommands. */
1985 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
1986 curbuf->b_p_ma = FALSE;
1987
1988#ifdef FEAT_AUTOCMD
1989 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
1990 FALSE, curbuf);
1991 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
1992 FALSE, curbuf);
1993#endif
1994
1995 /* make sure it will be redrawn */
1996 redraw_curbuf_later(NOT_VALID);
1997
1998 /* Restore KeyTyped, setting 'filetype' may reset it. */
1999 KeyTyped = old_KeyTyped;
2000}
2001
2002#endif /* FEAT_WINDOWS */
2003
2004/*
2005 * Return TRUE if "buf" is the quickfix buffer.
2006 */
2007 int
2008bt_quickfix(buf)
2009 buf_T *buf;
2010{
2011 return (buf->b_p_bt[0] == 'q');
2012}
2013
2014/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002015 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2016 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 */
2018 int
2019bt_nofile(buf)
2020 buf_T *buf;
2021{
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002022 return (buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2023 || buf->b_p_bt[0] == 'a';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024}
2025
2026/*
2027 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2028 */
2029 int
2030bt_dontwrite(buf)
2031 buf_T *buf;
2032{
2033 return (buf->b_p_bt[0] == 'n');
2034}
2035
2036 int
2037bt_dontwrite_msg(buf)
2038 buf_T *buf;
2039{
2040 if (bt_dontwrite(buf))
2041 {
2042 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2043 return TRUE;
2044 }
2045 return FALSE;
2046}
2047
2048/*
2049 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2050 * and 'bufhidden'.
2051 */
2052 int
2053buf_hide(buf)
2054 buf_T *buf;
2055{
2056 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2057 switch (buf->b_p_bh[0])
2058 {
2059 case 'u': /* "unload" */
2060 case 'w': /* "wipe" */
2061 case 'd': return FALSE; /* "delete" */
2062 case 'h': return TRUE; /* "hide" */
2063 }
2064 return (p_hid || cmdmod.hide);
2065}
2066
2067/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002068 * Return TRUE when using ":vimgrep" for ":grep".
2069 */
2070 int
Bram Moolenaar81695252004-12-29 20:58:21 +00002071grep_internal(cmdidx)
2072 cmdidx_T cmdidx;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002073{
Bram Moolenaar81695252004-12-29 20:58:21 +00002074 return ((cmdidx == CMD_grep || cmdidx == CMD_grepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002075 && STRCMP("internal",
2076 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2077}
2078
2079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 * Used for ":make", ":grep" and ":grepadd".
2081 */
2082 void
2083ex_make(eap)
2084 exarg_T *eap;
2085{
2086 char_u *name;
2087 char_u *cmd;
2088 unsigned len;
2089
Bram Moolenaar86b68352004-12-27 21:59:20 +00002090 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
Bram Moolenaar81695252004-12-29 20:58:21 +00002091 if (grep_internal(eap->cmdidx))
Bram Moolenaar86b68352004-12-27 21:59:20 +00002092 {
2093 ex_vimgrep(eap);
2094 return;
2095 }
2096
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 autowrite_all();
2098 name = get_mef_name();
2099 if (name == NULL)
2100 return;
2101 mch_remove(name); /* in case it's not unique */
2102
2103 /*
2104 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2105 */
2106 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2107 if (*p_sp != NUL)
2108 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(name) + 3;
2109 cmd = alloc(len);
2110 if (cmd == NULL)
2111 return;
2112 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2113 (char *)p_shq);
2114 if (*p_sp != NUL)
2115 append_redir(cmd, p_sp, name);
2116 /*
2117 * Output a newline if there's something else than the :make command that
2118 * was typed (in which case the cursor is in column 0).
2119 */
2120 if (msg_col == 0)
2121 msg_didout = FALSE;
2122 msg_start();
2123 MSG_PUTS(":!");
2124 msg_outtrans(cmd); /* show what we are doing */
2125
2126 /* let the shell know if we are redirecting output or not */
2127 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2128
2129#ifdef AMIGA
2130 out_flush();
2131 /* read window status report and redraw before message */
2132 (void)char_avail();
2133#endif
2134
2135 if (qf_init(name, eap->cmdidx != CMD_make ? p_gefm : p_efm,
Bram Moolenaar86b68352004-12-27 21:59:20 +00002136 eap->cmdidx != CMD_grepadd) > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 && !eap->forceit)
2138 qf_jump(0, 0, FALSE); /* display first error */
2139
2140 mch_remove(name);
2141 vim_free(name);
2142 vim_free(cmd);
2143}
2144
2145/*
2146 * Return the name for the errorfile, in allocated memory.
2147 * Find a new unique name when 'makeef' contains "##".
2148 * Returns NULL for error.
2149 */
2150 static char_u *
2151get_mef_name()
2152{
2153 char_u *p;
2154 char_u *name;
2155 static int start = -1;
2156 static int off = 0;
2157#ifdef HAVE_LSTAT
2158 struct stat sb;
2159#endif
2160
2161 if (*p_mef == NUL)
2162 {
2163 name = vim_tempname('e');
2164 if (name == NULL)
2165 EMSG(_(e_notmp));
2166 return name;
2167 }
2168
2169 for (p = p_mef; *p; ++p)
2170 if (p[0] == '#' && p[1] == '#')
2171 break;
2172
2173 if (*p == NUL)
2174 return vim_strsave(p_mef);
2175
2176 /* Keep trying until the name doesn't exist yet. */
2177 for (;;)
2178 {
2179 if (start == -1)
2180 start = mch_get_pid();
2181 else
2182 off += 19;
2183
2184 name = alloc((unsigned)STRLEN(p_mef) + 30);
2185 if (name == NULL)
2186 break;
2187 STRCPY(name, p_mef);
2188 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2189 STRCAT(name, p + 2);
2190 if (mch_getperm(name) < 0
2191#ifdef HAVE_LSTAT
2192 /* Don't accept a symbolic link, its a security risk. */
2193 && mch_lstat((char *)name, &sb) < 0
2194#endif
2195 )
2196 break;
2197 vim_free(name);
2198 }
2199 return name;
2200}
2201
2202/*
2203 * ":cc", ":crewind", ":cfirst" and ":clast".
2204 */
2205 void
2206ex_cc(eap)
2207 exarg_T *eap;
2208{
2209 qf_jump(0,
2210 eap->addr_count > 0
2211 ? (int)eap->line2
2212 : eap->cmdidx == CMD_cc
2213 ? 0
2214 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_cfirst)
2215 ? 1
2216 : 32767,
2217 eap->forceit);
2218}
2219
2220/*
2221 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
2222 */
2223 void
2224ex_cnext(eap)
2225 exarg_T *eap;
2226{
2227 qf_jump(eap->cmdidx == CMD_cnext
2228 ? FORWARD
2229 : eap->cmdidx == CMD_cnfile
2230 ? FORWARD_FILE
2231 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_cNfile)
2232 ? BACKWARD_FILE
2233 : BACKWARD,
2234 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
2235}
2236
2237/*
2238 * ":cfile" command.
2239 */
2240 void
2241ex_cfile(eap)
2242 exarg_T *eap;
2243{
2244 if (*eap->arg != NUL)
2245 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE);
2246 if (qf_init(p_ef, p_efm, TRUE) > 0 && eap->cmdidx == CMD_cfile)
2247 qf_jump(0, 0, eap->forceit); /* display first error */
2248}
2249
2250/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002251 * ":vimgrep {pattern} file(s)"
2252 */
2253 void
2254ex_vimgrep(eap)
2255 exarg_T *eap;
2256{
Bram Moolenaar81695252004-12-29 20:58:21 +00002257 regmmatch_T regmatch;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002258 char_u *save_cpo;
2259 int fcount;
2260 char_u **fnames;
Bram Moolenaar81695252004-12-29 20:58:21 +00002261 char_u *s;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002262 char_u *p;
2263 int i;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002264 int fi;
2265 struct qf_line *prevp = NULL;
2266 long lnum;
2267 garray_T ga;
Bram Moolenaar81695252004-12-29 20:58:21 +00002268 buf_T *buf;
2269 int duplicate_name = FALSE;
2270 int using_dummy;
2271 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002272 buf_T *first_match_buf = NULL;
2273 time_t seconds = 0;
2274#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2275 char_u *save_ei = NULL;
2276 aco_save_T aco;
2277#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00002278
2279 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
2280 save_cpo = p_cpo;
2281 p_cpo = empty_option;
2282
Bram Moolenaar81695252004-12-29 20:58:21 +00002283 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00002284 regmatch.regprog = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00002285 if (vim_isIDc(*eap->arg))
Bram Moolenaar86b68352004-12-27 21:59:20 +00002286 {
Bram Moolenaar81695252004-12-29 20:58:21 +00002287 s = eap->arg;
2288 p = skiptowhite(s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002289 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002290 else
2291 {
2292 s = eap->arg + 1;
2293 p = skip_regexp(s, *eap->arg, TRUE, NULL);
2294 if (*p != *eap->arg)
2295 {
2296 EMSG(_("E682: Invalid search pattern or delimiter"));
2297 goto theend;
2298 }
2299 }
2300 if (*p != NUL)
2301 *p++ = NUL;
2302 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002303 if (regmatch.regprog == NULL)
2304 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002305 regmatch.rmm_ic = p_ic;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002306
2307 p = skipwhite(p);
2308 if (*p == NUL)
2309 {
2310 EMSG(_("E683: File name missing or invalid pattern"));
2311 goto theend;
2312 }
2313
2314 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_vimgrepadd)
2315 || qf_curlist == qf_listcount)
2316 /* make place for a new list */
2317 qf_new_list();
2318 else if (qf_lists[qf_curlist].qf_count > 0)
2319 /* Adding to existing list, find last entry. */
2320 for (prevp = qf_lists[qf_curlist].qf_start;
2321 prevp->qf_next != prevp; prevp = prevp->qf_next)
2322 ;
2323
2324 /* parse the list of arguments */
2325 if (get_arglist(&ga, p) == FAIL)
2326 goto theend;
2327 i = gen_expand_wildcards(ga.ga_len, (char_u **)ga.ga_data,
2328 &fcount, &fnames, EW_FILE|EW_NOTFOUND);
2329 ga_clear(&ga);
2330 if (i == FAIL)
2331 goto theend;
2332 if (fcount == 0)
2333 {
2334 EMSG(_(e_nomatch));
2335 goto theend;
2336 }
2337
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002338 seconds = (time_t)0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002339 for (fi = 0; fi < fcount && !got_int; ++fi)
2340 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002341 if (time(NULL) > seconds)
2342 {
2343 /* Display the file name every second or so. */
2344 seconds = time(NULL);
2345 msg_start();
2346 p = msg_strtrunc(fnames[fi]);
2347 if (p == NULL)
2348 msg_outtrans(fnames[fi]);
2349 else
2350 {
2351 msg_outtrans(p);
2352 vim_free(p);
2353 }
2354 msg_clr_eos();
2355 msg_didout = FALSE; /* overwrite this message */
2356 msg_nowait = TRUE; /* don't wait for this message */
2357 msg_col = 0;
2358 out_flush();
2359 }
2360
Bram Moolenaar81695252004-12-29 20:58:21 +00002361 buf = buflist_findname_exp(fnames[fi]);
2362 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
2363 {
2364 /* Remember that a buffer with this name already exists. */
2365 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002366 using_dummy = TRUE;
2367
2368#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2369 /* Don't do Filetype autocommands to avoid loading syntax and
2370 * indent scripts, a great speed improvement. */
2371 save_ei = au_event_disable(",Filetype");
2372#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00002373
2374 /* Load file into a buffer, so that 'fileencoding' is detected,
2375 * autocommands applied, etc. */
2376 buf = load_dummy_buffer(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002377
2378#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2379 au_event_restore(save_ei);
2380#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00002381 }
2382 else
2383 /* Use existing, loaded buffer. */
2384 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002385
Bram Moolenaar81695252004-12-29 20:58:21 +00002386 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002387 {
2388 if (!got_int)
2389 smsg((char_u *)_("Cannot open file \"%s\""), fnames[fi]);
2390 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00002391 else
2392 {
Bram Moolenaar81695252004-12-29 20:58:21 +00002393 found_match = FALSE;
2394 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002395 {
Bram Moolenaar81695252004-12-29 20:58:21 +00002396 if (vim_regexec_multi(&regmatch, curwin, buf, lnum,
2397 (colnr_T)0) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002398 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00002399 if (qf_add_entry(&prevp,
2400 NULL, /* dir */
2401 fnames[fi],
Bram Moolenaar81695252004-12-29 20:58:21 +00002402 ml_get_buf(buf,
2403 regmatch.startpos[0].lnum + lnum, FALSE),
2404 regmatch.startpos[0].lnum + lnum,
2405 regmatch.startpos[0].col + 1,
Bram Moolenaar86b68352004-12-27 21:59:20 +00002406 FALSE, /* virt_col */
2407 0, /* nr */
2408 0, /* type */
2409 TRUE /* valid */
2410 ) == FAIL)
2411 {
2412 got_int = TRUE;
2413 break;
2414 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002415 else
2416 found_match = TRUE;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002417 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00002418 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00002419 if (got_int)
2420 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002421 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002422
2423 if (using_dummy)
2424 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002425 if (found_match && first_match_buf == NULL)
2426 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00002427 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002428 {
Bram Moolenaar81695252004-12-29 20:58:21 +00002429 /* Never keep a dummy buffer if there is another buffer
2430 * with the same name. */
2431 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002432 buf = NULL;
2433 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002434 else if (!buf_hide(buf))
2435 {
2436 /* When not hiding the buffer and no match was found we
2437 * don't need to remember the buffer, wipe it out. If
2438 * there was a match and it wasn't the first one: only
2439 * unload the buffer. */
2440 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002441 {
Bram Moolenaar81695252004-12-29 20:58:21 +00002442 wipe_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002443 buf = NULL;
2444 }
2445 else if (buf != first_match_buf)
2446 {
Bram Moolenaar81695252004-12-29 20:58:21 +00002447 unload_dummy_buffer(buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002448 buf = NULL;
2449 }
Bram Moolenaar81695252004-12-29 20:58:21 +00002450 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002451
2452#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2453 if (buf != NULL)
2454 {
2455 /* The buffer is still loaded, the Filetype autocommands
2456 * need to be done now, in that buffer. */
2457 aucmd_prepbuf(&aco, buf);
2458 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
2459 buf->b_fname, TRUE, buf);
2460 aucmd_restbuf(&aco);
2461 }
2462#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00002463 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00002464 }
2465 }
2466
2467 FreeWild(fcount, fnames);
2468
2469 qf_lists[qf_curlist].qf_nonevalid = FALSE;
2470 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
2471 qf_lists[qf_curlist].qf_index = 1;
2472
2473#ifdef FEAT_WINDOWS
2474 qf_update_buffer();
2475#endif
2476
2477 /* Jump to first match. */
2478 if (qf_lists[qf_curlist].qf_count > 0)
2479 qf_jump(0, 0, FALSE);
Bram Moolenaar81695252004-12-29 20:58:21 +00002480 else
2481 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002482
2483theend:
2484 vim_free(regmatch.regprog);
2485
2486 /* Only resture 'cpo' when it wasn't set in the mean time. */
2487 if (p_cpo == empty_option)
2488 p_cpo = save_cpo;
2489 else
2490 free_string_option(save_cpo);
2491}
2492
2493/*
Bram Moolenaar81695252004-12-29 20:58:21 +00002494 * Load file "fname" into a dummy buffer and return the buffer pointer.
2495 * Returns NULL if it fails.
2496 * Must call unload_dummy_buffer() or wipe_dummy_buffer() later!
2497 */
2498 static buf_T *
2499load_dummy_buffer(fname)
2500 char_u *fname;
2501{
2502 buf_T *newbuf;
2503 int failed = TRUE;
2504#ifdef FEAT_AUTOCMD
2505 aco_save_T aco;
2506#else
2507 buf_T *old_curbuf = curbuf;
2508#endif
2509
2510 /* Allocate a buffer without putting it in the buffer list. */
2511 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
2512 if (newbuf == NULL)
2513 return NULL;
2514
2515#ifdef FEAT_AUTOCMD
2516 /* set curwin/curbuf to buf and save a few things */
2517 aucmd_prepbuf(&aco, newbuf);
2518#else
2519 curbuf = newbuf;
2520 curwin->w_buffer = newbuf;
2521#endif
2522
2523 /* Need to set the filename for autocommands. */
2524 (void)setfname(curbuf, fname, NULL, FALSE);
2525
2526 if (ml_open() == OK)
2527 {
2528 /* Create swap file now to avoid the ATTENTION message. */
2529 check_need_swap(TRUE);
2530
2531 /* Remove the "dummy" flag, otherwise autocommands may not
2532 * work. */
2533 curbuf->b_flags &= ~BF_DUMMY;
2534
2535 if (readfile(fname, NULL,
2536 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
2537 NULL, READ_NEW | READ_DUMMY) == OK
2538 && !(curbuf->b_flags & BF_NEW))
2539 {
2540 failed = FALSE;
2541 if (curbuf != newbuf)
2542 {
2543 /* Bloody autocommands changed the buffer! */
2544 if (buf_valid(newbuf))
2545 wipe_buffer(newbuf, FALSE);
2546 newbuf = curbuf;
2547 }
2548 }
2549 }
2550
2551#ifdef FEAT_AUTOCMD
2552 /* restore curwin/curbuf and a few other things */
2553 aucmd_restbuf(&aco);
2554#else
2555 curbuf = old_curbuf;
2556 curwin->w_buffer = old_curbuf;
2557#endif
2558
2559 if (!buf_valid(newbuf))
2560 return NULL;
2561 if (failed)
2562 {
2563 wipe_dummy_buffer(newbuf);
2564 return NULL;
2565 }
2566 return newbuf;
2567}
2568
2569/*
2570 * Wipe out the dummy buffer that load_dummy_buffer() created.
2571 */
2572 static void
2573wipe_dummy_buffer(buf)
2574 buf_T *buf;
2575{
2576 if (curbuf != buf) /* safety check */
2577 wipe_buffer(buf, FALSE);
2578}
2579
2580/*
2581 * Unload the dummy buffer that load_dummy_buffer() created.
2582 */
2583 static void
2584unload_dummy_buffer(buf)
2585 buf_T *buf;
2586{
2587 if (curbuf != buf) /* safety check */
2588 close_buffer(NULL, buf, DOBUF_UNLOAD);
2589}
2590
2591/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002592 * ":[range]cbuffer [bufnr]" command.
2593 */
2594 void
2595ex_cbuffer(eap)
2596 exarg_T *eap;
2597{
2598 buf_T *buf = NULL;
2599
2600 if (*eap->arg == NUL)
2601 buf = curbuf;
2602 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
2603 buf = buflist_findnr(atoi((char *)eap->arg));
2604 if (buf == NULL)
2605 EMSG(_(e_invarg));
2606 else if (buf->b_ml.ml_mfp == NULL)
2607 EMSG(_("E681: Buffer is not loaded"));
2608 else
2609 {
2610 if (eap->addr_count == 0)
2611 {
2612 eap->line1 = 1;
2613 eap->line2 = buf->b_ml.ml_line_count;
2614 }
2615 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
2616 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
2617 EMSG(_(e_invrange));
2618 else
2619 qf_init_ext(NULL, buf, p_efm, TRUE, eap->line1, eap->line2);
2620 }
2621}
2622
2623/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 * ":helpgrep {pattern}"
2625 */
2626 void
2627ex_helpgrep(eap)
2628 exarg_T *eap;
2629{
2630 regmatch_T regmatch;
2631 char_u *save_cpo;
2632 char_u *p;
2633 int fcount;
2634 char_u **fnames;
2635 FILE *fd;
2636 int fi;
2637 struct qf_line *prevp = NULL;
2638 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002639#ifdef FEAT_MULTI_LANG
2640 char_u *lang;
2641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642
2643 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
2644 save_cpo = p_cpo;
2645 p_cpo = (char_u *)"";
2646
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002647#ifdef FEAT_MULTI_LANG
2648 /* Check for a specified language */
2649 lang = check_help_lang(eap->arg);
2650#endif
2651
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
2653 regmatch.rm_ic = FALSE;
2654 if (regmatch.regprog != NULL)
2655 {
2656 /* create a new quickfix list */
2657 qf_new_list();
2658
2659 /* Go through all directories in 'runtimepath' */
2660 p = p_rtp;
2661 while (*p != NUL && !got_int)
2662 {
2663 copy_option_part(&p, NameBuff, MAXPATHL, ",");
2664
2665 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
2666 add_pathsep(NameBuff);
2667 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
2668 if (gen_expand_wildcards(1, &NameBuff, &fcount,
2669 &fnames, EW_FILE|EW_SILENT) == OK
2670 && fcount > 0)
2671 {
2672 for (fi = 0; fi < fcount && !got_int; ++fi)
2673 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002674#ifdef FEAT_MULTI_LANG
2675 /* Skip files for a different language. */
2676 if (lang != NULL
2677 && STRNICMP(lang, fnames[fi]
2678 + STRLEN(fnames[fi]) - 3, 2) != 0
2679 && !(STRNICMP(lang, "en", 2) == 0
2680 && STRNICMP("txt", fnames[fi]
2681 + STRLEN(fnames[fi]) - 3, 3) == 0))
2682 continue;
2683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 fd = fopen((char *)fnames[fi], "r");
2685 if (fd != NULL)
2686 {
2687 lnum = 1;
2688 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
2689 {
2690 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
2691 {
2692 int l = STRLEN(IObuff);
2693
2694 /* remove trailing CR, LF, spaces, etc. */
2695 while (l > 0 && IObuff[l - 1] <= ' ')
2696 IObuff[--l] = NUL;
2697
2698 if (qf_add_entry(&prevp,
2699 NULL, /* dir */
2700 fnames[fi],
2701 IObuff,
2702 lnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00002703 (int)(regmatch.startp[0] - IObuff)
2704 + 1, /* col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 FALSE, /* virt_col */
2706 0, /* nr */
2707 1, /* type */
2708 TRUE /* valid */
2709 ) == FAIL)
2710 {
2711 got_int = TRUE;
2712 break;
2713 }
2714 }
2715 ++lnum;
2716 line_breakcheck();
2717 }
2718 fclose(fd);
2719 }
2720 }
2721 FreeWild(fcount, fnames);
2722 }
2723 }
2724 vim_free(regmatch.regprog);
2725
2726 qf_lists[qf_curlist].qf_nonevalid = FALSE;
2727 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
2728 qf_lists[qf_curlist].qf_index = 1;
2729 }
2730
2731 p_cpo = save_cpo;
2732
2733#ifdef FEAT_WINDOWS
2734 qf_update_buffer();
2735#endif
2736
2737 /* Jump to first match. */
2738 if (qf_lists[qf_curlist].qf_count > 0)
2739 qf_jump(0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002740 else
2741 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742}
2743
2744#endif /* FEAT_QUICKFIX */