blob: 0cb9c6956c108ae0339d8ce0d2f4a89a6c9f227b [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
89static void qf_new_list __ARGS((void));
90static 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));
91static void qf_msg __ARGS((void));
92static void qf_free __ARGS((int idx));
93static char_u *qf_types __ARGS((int, int));
94static int qf_get_fnum __ARGS((char_u *, char_u *));
95static char_u *qf_push_dir __ARGS((char_u *, struct dir_stack_T **));
96static char_u *qf_pop_dir __ARGS((struct dir_stack_T **));
97static char_u *qf_guess_filepath __ARGS((char_u *));
98static void qf_fmt_text __ARGS((char_u *text, char_u *buf, int bufsize));
99static void qf_clean_dir_stack __ARGS((struct dir_stack_T **));
100#ifdef FEAT_WINDOWS
101static int qf_win_pos_update __ARGS((int old_qf_index));
102static buf_T *qf_find_buf __ARGS((void));
103static void qf_update_buffer __ARGS((void));
104static void qf_fill_buffer __ARGS((void));
105#endif
106static char_u *get_mef_name __ARGS((void));
107
108/*
109 * Read the errorfile into memory, line by line, building the error list.
110 * Return -1 for error, number of errors for success.
111 */
112 int
113qf_init(efile, errorformat, newlist)
114 char_u *efile;
115 char_u *errorformat;
116 int newlist; /* TRUE: start a new error list */
117{
118 char_u *namebuf;
119 char_u *errmsg;
120 char_u *fmtstr = NULL;
121 int col = 0;
122 char_u use_virt_col = FALSE;
123 int type = 0;
124 int valid;
125 long lnum = 0L;
126 int enr = 0;
127 FILE *fd;
128 struct qf_line *qfprev = NULL; /* init to make SASC shut up */
129 char_u *efmp;
130 struct eformat *fmt_first = NULL;
131 struct eformat *fmt_last = NULL;
132 struct eformat *fmt_ptr;
133 char_u *efm;
134 char_u *ptr;
135 char_u *srcptr;
136 int len;
137 int i;
138 int round;
139 int idx = 0;
140 int multiline = FALSE;
141 int multiignore = FALSE;
142 int multiscan = FALSE;
143 int retval = -1; /* default: return error flag */
144 char_u *directory = NULL;
145 char_u *currfile = NULL;
146 char_u *tail = NULL;
147 struct dir_stack_T *file_stack = NULL;
148 regmatch_T regmatch;
149 static struct fmtpattern
150 {
151 char_u convchar;
152 char *pattern;
153 } fmt_pat[FMT_PATTERNS] =
154 {
155 {'f', "\\f\\+"},
156 {'n', "\\d\\+"},
157 {'l', "\\d\\+"},
158 {'c', "\\d\\+"},
159 {'t', "."},
160 {'m', ".\\+"},
161 {'r', ".*"},
162 {'p', "[- .]*"},
163 {'v', "\\d\\+"}
164 };
165
166 if (efile == NULL)
167 return FAIL;
168
169 namebuf = alloc(CMDBUFFSIZE + 1);
170 errmsg = alloc(CMDBUFFSIZE + 1);
171 if (namebuf == NULL || errmsg == NULL)
172 goto qf_init_end;
173
174 if ((fd = mch_fopen((char *)efile, "r")) == NULL)
175 {
176 EMSG2(_(e_openerrf), efile);
177 goto qf_init_end;
178 }
179
180 if (newlist || qf_curlist == qf_listcount)
181 /* make place for a new list */
182 qf_new_list();
183 else if (qf_lists[qf_curlist].qf_count > 0)
184 /* Adding to existing list, find last entry. */
185 for (qfprev = qf_lists[qf_curlist].qf_start;
186 qfprev->qf_next != qfprev; qfprev = qfprev->qf_next)
187 ;
188
189/*
190 * Each part of the format string is copied and modified from errorformat to
191 * regex prog. Only a few % characters are allowed.
192 */
193 /* Use the local value of 'errorformat' if it's set. */
194 if (errorformat == p_efm && *curbuf->b_p_efm != NUL)
195 efm = curbuf->b_p_efm;
196 else
197 efm = errorformat;
198 /*
199 * Get some space to modify the format string into.
200 */
201 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
202 for (round = FMT_PATTERNS; round > 0; )
203 i += (int)STRLEN(fmt_pat[--round].pattern);
204#ifdef COLON_IN_FILENAME
205 i += 12; /* "%f" can become twelve chars longer */
206#else
207 i += 2; /* "%f" can become two chars longer */
208#endif
209 if ((fmtstr = alloc(i)) == NULL)
210 goto error2;
211
212 while (efm[0])
213 {
214 /*
215 * Allocate a new eformat structure and put it at the end of the list
216 */
217 fmt_ptr = (struct eformat *)alloc((unsigned)sizeof(struct eformat));
218 if (fmt_ptr == NULL)
219 goto error2;
220 if (fmt_first == NULL) /* first one */
221 fmt_first = fmt_ptr;
222 else
223 fmt_last->next = fmt_ptr;
224 fmt_last = fmt_ptr;
225 fmt_ptr->prefix = NUL;
226 fmt_ptr->flags = NUL;
227 fmt_ptr->next = NULL;
228 fmt_ptr->prog = NULL;
229 for (round = FMT_PATTERNS; round > 0; )
230 fmt_ptr->addr[--round] = NUL;
231 /* round is 0 now */
232
233 /*
234 * Isolate one part in the 'errorformat' option
235 */
236 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
237 if (efm[len] == '\\' && efm[len + 1] != NUL)
238 ++len;
239
240 /*
241 * Build regexp pattern from current 'errorformat' option
242 */
243 ptr = fmtstr;
244 *ptr++ = '^';
245 for (efmp = efm; efmp < efm + len; ++efmp)
246 {
247 if (*efmp == '%')
248 {
249 ++efmp;
250 for (idx = 0; idx < FMT_PATTERNS; ++idx)
251 if (fmt_pat[idx].convchar == *efmp)
252 break;
253 if (idx < FMT_PATTERNS)
254 {
255 if (fmt_ptr->addr[idx])
256 {
257 sprintf((char *)errmsg,
258 _("E372: Too many %%%c in format string"), *efmp);
259 EMSG(errmsg);
260 goto error2;
261 }
262 if ((idx
263 && idx < 6
264 && vim_strchr((char_u *)"DXOPQ",
265 fmt_ptr->prefix) != NULL)
266 || (idx == 6
267 && vim_strchr((char_u *)"OPQ",
268 fmt_ptr->prefix) == NULL))
269 {
270 sprintf((char *)errmsg,
271 _("E373: Unexpected %%%c in format string"), *efmp);
272 EMSG(errmsg);
273 goto error2;
274 }
275 fmt_ptr->addr[idx] = (char_u)++round;
276 *ptr++ = '\\';
277 *ptr++ = '(';
278#ifdef BACKSLASH_IN_FILENAME
279 if (*efmp == 'f')
280 {
281 /* Also match "c:" in the file name, even when
282 * checking for a colon next: "%f:".
283 * "\%(\a:\)\=" */
284 STRCPY(ptr, "\\%(\\a:\\)\\=");
285 ptr += 10;
286 }
287#endif
288 if (*efmp == 'f' && efmp[1] != NUL
289 && efmp[1] != '\\' && efmp[1] != '%')
290 {
291 /* A file name may contain spaces, but this isn't in
292 * "\f". use "[^x]\+" instead (x is next character) */
293 *ptr++ = '[';
294 *ptr++ = '^';
295 *ptr++ = efmp[1];
296 *ptr++ = ']';
297 *ptr++ = '\\';
298 *ptr++ = '+';
299 }
300 else
301 {
302 srcptr = (char_u *)fmt_pat[idx].pattern;
303 while ((*ptr = *srcptr++) != NUL)
304 ++ptr;
305 }
306 *ptr++ = '\\';
307 *ptr++ = ')';
308 }
309 else if (*efmp == '*')
310 {
311 if (*++efmp == '[' || *efmp == '\\')
312 {
313 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
314 {
315 if (efmp[1] == '^')
316 *ptr++ = *++efmp;
317 if (efmp < efm + len)
318 {
319 *ptr++ = *++efmp; /* could be ']' */
320 while (efmp < efm + len
321 && (*ptr++ = *++efmp) != ']')
322 /* skip */;
323 if (efmp == efm + len)
324 {
325 EMSG(_("E374: Missing ] in format string"));
326 goto error2;
327 }
328 }
329 }
330 else if (efmp < efm + len) /* %*\D, %*\s etc. */
331 *ptr++ = *++efmp;
332 *ptr++ = '\\';
333 *ptr++ = '+';
334 }
335 else
336 {
337 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
338 sprintf((char *)errmsg,
339 _("E375: Unsupported %%%c in format string"), *efmp);
340 EMSG(errmsg);
341 goto error2;
342 }
343 }
344 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
345 *ptr++ = *efmp; /* regexp magic characters */
346 else if (*efmp == '#')
347 *ptr++ = '*';
348 else if (efmp == efm + 1) /* analyse prefix */
349 {
350 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
351 fmt_ptr->flags = *efmp++;
352 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
353 fmt_ptr->prefix = *efmp;
354 else
355 {
356 sprintf((char *)errmsg,
357 _("E376: Invalid %%%c in format string prefix"), *efmp);
358 EMSG(errmsg);
359 goto error2;
360 }
361 }
362 else
363 {
364 sprintf((char *)errmsg,
365 _("E377: Invalid %%%c in format string"), *efmp);
366 EMSG(errmsg);
367 goto error2;
368 }
369 }
370 else /* copy normal character */
371 {
372 if (*efmp == '\\' && efmp + 1 < efm + len)
373 ++efmp;
374 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
375 *ptr++ = '\\'; /* escape regexp atoms */
376 if (*efmp)
377 *ptr++ = *efmp;
378 }
379 }
380 *ptr++ = '$';
381 *ptr = NUL;
382 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
383 goto error2;
384 /*
385 * Advance to next part
386 */
387 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
388 }
389 if (fmt_first == NULL) /* nothing found */
390 {
391 EMSG(_("E378: 'errorformat' contains no pattern"));
392 goto error2;
393 }
394
395 /*
396 * got_int is reset here, because it was probably set when killing the
397 * ":make" command, but we still want to read the errorfile then.
398 */
399 got_int = FALSE;
400
401 /* Always ignore case when looking for a matching error. */
402 regmatch.rm_ic = TRUE;
403
404 /*
405 * Read the lines in the error file one by one.
406 * Try to recognize one of the error formats in each line.
407 */
408 while (fgets((char *)IObuff, CMDBUFFSIZE - 2, fd) != NULL && !got_int)
409 {
410 IObuff[CMDBUFFSIZE - 2] = NUL; /* for very long lines */
411 if ((efmp = vim_strrchr(IObuff, '\n')) != NULL)
412 *efmp = NUL;
413#ifdef USE_CRNL
414 if ((efmp = vim_strrchr(IObuff, '\r')) != NULL)
415 *efmp = NUL;
416#endif
417
418 /*
419 * Try to match each part of 'errorformat' until we find a complete
420 * match or no match.
421 */
422 valid = TRUE;
423restofline:
424 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
425 {
426 idx = fmt_ptr->prefix;
427 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
428 continue;
429 namebuf[0] = NUL;
430 if (!multiscan)
431 errmsg[0] = NUL;
432 lnum = 0;
433 col = 0;
434 use_virt_col = FALSE;
435 enr = -1;
436 type = 0;
437 tail = NULL;
438
439 regmatch.regprog = fmt_ptr->prog;
440 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
441 {
442 if ((idx == 'C' || idx == 'Z') && !multiline)
443 continue;
444 if (vim_strchr((char_u *)"EWI", idx) != NULL)
445 type = idx;
446 else
447 type = 0;
448 /*
449 * Extract error message data from matched line
450 */
451 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
452 {
453 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
454 STRNCPY(namebuf, regmatch.startp[i], len);
455 namebuf[len] = NUL;
456 if (vim_strchr((char_u *)"OPQ", idx) != NULL
457 && mch_getperm(namebuf) == -1)
458 continue;
459 }
460 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
461 enr = (int)atol((char *)regmatch.startp[i]);
462 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
463 lnum = atol((char *)regmatch.startp[i]);
464 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
465 col = (int)atol((char *)regmatch.startp[i]);
466 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
467 type = *regmatch.startp[i];
468 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
469 STRCPY(errmsg, IObuff);
470 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
471 {
472 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
473 STRNCPY(errmsg, regmatch.startp[i], len);
474 errmsg[len] = NUL;
475 }
476 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
477 tail = regmatch.startp[i];
478 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
479 {
480 col = (int)(regmatch.endp[i] - regmatch.startp[i] + 1);
481 if (*((char_u *)regmatch.startp[i]) != TAB)
482 use_virt_col = TRUE;
483 }
484 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
485 {
486 col = (int)atol((char *)regmatch.startp[i]);
487 use_virt_col = TRUE;
488 }
489 break;
490 }
491 }
492 multiscan = FALSE;
493 if (!fmt_ptr || idx == 'D' || idx == 'X')
494 {
495 if (fmt_ptr)
496 {
497 if (idx == 'D') /* enter directory */
498 {
499 if (*namebuf == NUL)
500 {
501 EMSG(_("E379: Missing or empty directory name"));
502 goto error2;
503 }
504 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
505 goto error2;
506 }
507 else if (idx == 'X') /* leave directory */
508 directory = qf_pop_dir(&dir_stack);
509 }
510 namebuf[0] = NUL; /* no match found, remove file name */
511 lnum = 0; /* don't jump to this line */
512 valid = FALSE;
513 STRCPY(errmsg, IObuff); /* copy whole line to error message */
514 if (!fmt_ptr)
515 multiline = multiignore = FALSE;
516 }
517 else if (fmt_ptr)
518 {
519 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
520 multiline = TRUE; /* start of a multi-line message */
521 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
522 { /* continuation of multi-line msg */
523 if (qfprev == NULL)
524 goto error2;
525 if (*errmsg && !multiignore)
526 {
527 len = (int)STRLEN(qfprev->qf_text);
528 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
529 == NULL)
530 goto error2;
531 STRCPY(ptr, qfprev->qf_text);
532 vim_free(qfprev->qf_text);
533 qfprev->qf_text = ptr;
534 *(ptr += len) = '\n';
535 STRCPY(++ptr, errmsg);
536 }
537 if (qfprev->qf_nr == -1)
538 qfprev->qf_nr = enr;
539 if (vim_isprintc(type) && !qfprev->qf_type)
540 qfprev->qf_type = type; /* only printable chars allowed */
541 if (!qfprev->qf_lnum)
542 qfprev->qf_lnum = lnum;
543 if (!qfprev->qf_col)
544 qfprev->qf_col = col;
545 qfprev->qf_virt_col = use_virt_col;
546 if (!qfprev->qf_fnum)
547 qfprev->qf_fnum = qf_get_fnum(directory,
548 *namebuf || directory ? namebuf
549 : currfile && valid ? currfile : 0);
550 if (idx == 'Z')
551 multiline = multiignore = FALSE;
552 line_breakcheck();
553 continue;
554 }
555 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
556 {
557 /* global file names */
558 valid = FALSE;
559 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
560 {
561 if (*namebuf && idx == 'P')
562 currfile = qf_push_dir(namebuf, &file_stack);
563 else if (idx == 'Q')
564 currfile = qf_pop_dir(&file_stack);
565 *namebuf = NUL;
566 if (tail && *tail)
567 {
568 STRCPY(IObuff, skipwhite(tail));
569 multiscan = TRUE;
570 goto restofline;
571 }
572 }
573 }
574 if (fmt_ptr->flags == '-') /* generally exclude this line */
575 {
576 if (multiline)
577 multiignore = TRUE; /* also exclude continuation lines */
578 continue;
579 }
580 }
581
582 if (qf_add_entry(&qfprev,
583 directory,
584 *namebuf || directory
585 ? namebuf
586 : currfile && valid ? currfile : NULL,
587 errmsg,
588 lnum,
589 col,
590 use_virt_col,
591 enr,
592 type,
593 valid) == FAIL)
594 goto error2;
595 line_breakcheck();
596 }
597 if (!ferror(fd))
598 {
599 if (qf_lists[qf_curlist].qf_index == 0) /* no valid entry found */
600 {
601 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
602 qf_lists[qf_curlist].qf_index = 1;
603 qf_lists[qf_curlist].qf_nonevalid = TRUE;
604 }
605 else
606 {
607 qf_lists[qf_curlist].qf_nonevalid = FALSE;
608 if (qf_lists[qf_curlist].qf_ptr == NULL)
609 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
610 }
611 retval = qf_lists[qf_curlist].qf_count; /* return number of matches */
612 goto qf_init_ok;
613 }
614 EMSG(_(e_readerrf));
615error2:
616 qf_free(qf_curlist);
617 qf_listcount--;
618 if (qf_curlist > 0)
619 --qf_curlist;
620qf_init_ok:
621 fclose(fd);
622 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
623 {
624 fmt_first = fmt_ptr->next;
625 vim_free(fmt_ptr->prog);
626 vim_free(fmt_ptr);
627 }
628 qf_clean_dir_stack(&dir_stack);
629 qf_clean_dir_stack(&file_stack);
630qf_init_end:
631 vim_free(namebuf);
632 vim_free(errmsg);
633 vim_free(fmtstr);
634
635#ifdef FEAT_WINDOWS
636 qf_update_buffer();
637#endif
638
639 return retval;
640}
641
642/*
643 * Prepare for adding a new quickfix list.
644 */
645 static void
646qf_new_list()
647{
648 int i;
649
650 /*
651 * If the current entry is not the last entry, delete entries below
652 * the current entry. This makes it possible to browse in a tree-like
653 * way with ":grep'.
654 */
655 while (qf_listcount > qf_curlist + 1)
656 qf_free(--qf_listcount);
657
658 /*
659 * When the stack is full, remove to oldest entry
660 * Otherwise, add a new entry.
661 */
662 if (qf_listcount == LISTCOUNT)
663 {
664 qf_free(0);
665 for (i = 1; i < LISTCOUNT; ++i)
666 qf_lists[i - 1] = qf_lists[i];
667 qf_curlist = LISTCOUNT - 1;
668 }
669 else
670 qf_curlist = qf_listcount++;
671 qf_lists[qf_curlist].qf_index = 0;
672 qf_lists[qf_curlist].qf_count = 0;
673}
674
675/*
676 * Add an entry to the end of the list of errors.
677 * Returns OK or FAIL.
678 */
679 static int
680qf_add_entry(prevp, dir, fname, mesg, lnum, col, virt_col, nr, type, valid)
681 struct qf_line **prevp; /* pointer to previously added entry or NULL */
682 char_u *dir; /* optional directory name */
683 char_u *fname; /* file name or NULL */
684 char_u *mesg; /* message */
685 long lnum; /* line number */
686 int col; /* column */
687 int virt_col; /* using virtual column */
688 int nr; /* error number */
689 int type; /* type character */
690 int valid; /* valid entry */
691{
692 struct qf_line *qfp;
693
694 if ((qfp = (struct qf_line *)alloc((unsigned)sizeof(struct qf_line)))
695 == NULL)
696 return FAIL;
697 qfp->qf_fnum = qf_get_fnum(dir, fname);
698 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
699 {
700 vim_free(qfp);
701 return FAIL;
702 }
703 qfp->qf_lnum = lnum;
704 qfp->qf_col = col;
705 qfp->qf_virt_col = virt_col;
706 qfp->qf_nr = nr;
707 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
708 type = 0;
709 qfp->qf_type = type;
710 qfp->qf_valid = valid;
711
712 if (qf_lists[qf_curlist].qf_count == 0) /* first element in the list */
713 {
714 qf_lists[qf_curlist].qf_start = qfp;
715 qfp->qf_prev = qfp; /* first element points to itself */
716 }
717 else
718 {
719 qfp->qf_prev = *prevp;
720 (*prevp)->qf_next = qfp;
721 }
722 qfp->qf_next = qfp; /* last element points to itself */
723 qfp->qf_cleared = FALSE;
724 *prevp = qfp;
725 ++qf_lists[qf_curlist].qf_count;
726 if (qf_lists[qf_curlist].qf_index == 0 && qfp->qf_valid)
727 /* first valid entry */
728 {
729 qf_lists[qf_curlist].qf_index = qf_lists[qf_curlist].qf_count;
730 qf_lists[qf_curlist].qf_ptr = qfp;
731 }
732
733 return OK;
734}
735
736/*
737 * get buffer number for file "dir.name"
738 */
739 static int
740qf_get_fnum(directory, fname)
741 char_u *directory;
742 char_u *fname;
743{
744 if (fname == NULL || *fname == NUL) /* no file name */
745 return 0;
746 {
747#ifdef RISCOS
748 /* Name is reported as `main.c', but file is `c.main' */
749 return ro_buflist_add(fname);
750#else
751 char_u *ptr;
752 int fnum;
753
754# ifdef VMS
755 vms_remove_version(fname);
756# endif
757# ifdef BACKSLASH_IN_FILENAME
758 if (directory != NULL)
759 slash_adjust(directory);
760 slash_adjust(fname);
761# endif
762 if (directory != NULL && !vim_isAbsName(fname)
763 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
764 {
765 /*
766 * Here we check if the file really exists.
767 * This should normally be true, but if make works without
768 * "leaving directory"-messages we might have missed a
769 * directory change.
770 */
771 if (mch_getperm(ptr) < 0)
772 {
773 vim_free(ptr);
774 directory = qf_guess_filepath(fname);
775 if (directory)
776 ptr = concat_fnames(directory, fname, TRUE);
777 else
778 ptr = vim_strsave(fname);
779 }
780 /* Use concatenated directory name and file name */
781 fnum = buflist_add(ptr, 0);
782 vim_free(ptr);
783 return fnum;
784 }
785 return buflist_add(fname, 0);
786#endif
787 }
788}
789
790/*
791 * push dirbuf onto the directory stack and return pointer to actual dir or
792 * NULL on error
793 */
794 static char_u *
795qf_push_dir(dirbuf, stackptr)
796 char_u *dirbuf;
797 struct dir_stack_T **stackptr;
798{
799 struct dir_stack_T *ds_new;
800 struct dir_stack_T *ds_ptr;
801
802 /* allocate new stack element and hook it in */
803 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
804 if (ds_new == NULL)
805 return NULL;
806
807 ds_new->next = *stackptr;
808 *stackptr = ds_new;
809
810 /* store directory on the stack */
811 if (vim_isAbsName(dirbuf)
812 || (*stackptr)->next == NULL
813 || (*stackptr && dir_stack != *stackptr))
814 (*stackptr)->dirname = vim_strsave(dirbuf);
815 else
816 {
817 /* Okay we don't have an absolute path.
818 * dirbuf must be a subdir of one of the directories on the stack.
819 * Let's search...
820 */
821 ds_new = (*stackptr)->next;
822 (*stackptr)->dirname = NULL;
823 while (ds_new)
824 {
825 vim_free((*stackptr)->dirname);
826 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
827 TRUE);
828 if (mch_isdir((*stackptr)->dirname) == TRUE)
829 break;
830
831 ds_new = ds_new->next;
832 }
833
834 /* clean up all dirs we already left */
835 while ((*stackptr)->next != ds_new)
836 {
837 ds_ptr = (*stackptr)->next;
838 (*stackptr)->next = (*stackptr)->next->next;
839 vim_free(ds_ptr->dirname);
840 vim_free(ds_ptr);
841 }
842
843 /* Nothing found -> it must be on top level */
844 if (ds_new == NULL)
845 {
846 vim_free((*stackptr)->dirname);
847 (*stackptr)->dirname = vim_strsave(dirbuf);
848 }
849 }
850
851 if ((*stackptr)->dirname != NULL)
852 return (*stackptr)->dirname;
853 else
854 {
855 ds_ptr = *stackptr;
856 *stackptr = (*stackptr)->next;
857 vim_free(ds_ptr);
858 return NULL;
859 }
860}
861
862
863/*
864 * pop dirbuf from the directory stack and return previous directory or NULL if
865 * stack is empty
866 */
867 static char_u *
868qf_pop_dir(stackptr)
869 struct dir_stack_T **stackptr;
870{
871 struct dir_stack_T *ds_ptr;
872
873 /* TODO: Should we check if dirbuf is the directory on top of the stack?
874 * What to do if it isn't? */
875
876 /* pop top element and free it */
877 if (*stackptr != NULL)
878 {
879 ds_ptr = *stackptr;
880 *stackptr = (*stackptr)->next;
881 vim_free(ds_ptr->dirname);
882 vim_free(ds_ptr);
883 }
884
885 /* return NEW top element as current dir or NULL if stack is empty*/
886 return *stackptr ? (*stackptr)->dirname : NULL;
887}
888
889/*
890 * clean up directory stack
891 */
892 static void
893qf_clean_dir_stack(stackptr)
894 struct dir_stack_T **stackptr;
895{
896 struct dir_stack_T *ds_ptr;
897
898 while ((ds_ptr = *stackptr) != NULL)
899 {
900 *stackptr = (*stackptr)->next;
901 vim_free(ds_ptr->dirname);
902 vim_free(ds_ptr);
903 }
904}
905
906/*
907 * Check in which directory of the directory stack the given file can be
908 * found.
909 * Returns a pointer to the directory name or NULL if not found
910 * Cleans up intermediate directory entries.
911 *
912 * TODO: How to solve the following problem?
913 * If we have the this directory tree:
914 * ./
915 * ./aa
916 * ./aa/bb
917 * ./bb
918 * ./bb/x.c
919 * and make says:
920 * making all in aa
921 * making all in bb
922 * x.c:9: Error
923 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
924 * qf_guess_filepath will return NULL.
925 */
926 static char_u *
927qf_guess_filepath(filename)
928 char_u *filename;
929{
930 struct dir_stack_T *ds_ptr;
931 struct dir_stack_T *ds_tmp;
932 char_u *fullname;
933
934 /* no dirs on the stack - there's nothing we can do */
935 if (dir_stack == NULL)
936 return NULL;
937
938 ds_ptr = dir_stack->next;
939 fullname = NULL;
940 while (ds_ptr)
941 {
942 vim_free(fullname);
943 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
944
945 /* If concat_fnames failed, just go on. The worst thing that can happen
946 * is that we delete the entire stack.
947 */
948 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
949 break;
950
951 ds_ptr = ds_ptr->next;
952 }
953
954 vim_free(fullname);
955
956 /* clean up all dirs we already left */
957 while (dir_stack->next != ds_ptr)
958 {
959 ds_tmp = dir_stack->next;
960 dir_stack->next = dir_stack->next->next;
961 vim_free(ds_tmp->dirname);
962 vim_free(ds_tmp);
963 }
964
965 return ds_ptr==NULL? NULL: ds_ptr->dirname;
966
967}
968
969/*
970 * jump to a quickfix line
971 * if dir == FORWARD go "errornr" valid entries forward
972 * if dir == BACKWARD go "errornr" valid entries backward
973 * if dir == FORWARD_FILE go "errornr" valid entries files backward
974 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
975 * else if "errornr" is zero, redisplay the same line
976 * else go to entry "errornr"
977 */
978 void
979qf_jump(dir, errornr, forceit)
980 int dir;
981 int errornr;
982 int forceit;
983{
984 struct qf_line *qf_ptr;
985 struct qf_line *old_qf_ptr;
986 int qf_index;
987 int old_qf_fnum;
988 int old_qf_index;
989 int prev_index;
990 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
991 char_u *err = e_no_more_items;
992 linenr_T i;
993 buf_T *old_curbuf;
994 linenr_T old_lnum;
995 char_u *old_swb = p_swb;
996 colnr_T screen_col;
997 colnr_T char_col;
998 char_u *line;
999#ifdef FEAT_WINDOWS
1000 int opened_window = FALSE;
1001 win_T *win;
1002 win_T *altwin;
1003#endif
1004 int print_message = TRUE;
1005 int len;
1006#ifdef FEAT_FOLDING
1007 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1008#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001009 int ok = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010
1011 if (qf_curlist >= qf_listcount || qf_lists[qf_curlist].qf_count == 0)
1012 {
1013 EMSG(_(e_quickfix));
1014 return;
1015 }
1016
1017 qf_ptr = qf_lists[qf_curlist].qf_ptr;
1018 old_qf_ptr = qf_ptr;
1019 qf_index = qf_lists[qf_curlist].qf_index;
1020 old_qf_index = qf_index;
1021 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1022 {
1023 while (errornr--)
1024 {
1025 old_qf_ptr = qf_ptr;
1026 prev_index = qf_index;
1027 old_qf_fnum = qf_ptr->qf_fnum;
1028 do
1029 {
1030 if (qf_index == qf_lists[qf_curlist].qf_count
1031 || qf_ptr->qf_next == NULL)
1032 {
1033 qf_ptr = old_qf_ptr;
1034 qf_index = prev_index;
1035 if (err != NULL)
1036 {
1037 EMSG(_(err));
1038 goto theend;
1039 }
1040 errornr = 0;
1041 break;
1042 }
1043 ++qf_index;
1044 qf_ptr = qf_ptr->qf_next;
1045 } while ((!qf_lists[qf_curlist].qf_nonevalid && !qf_ptr->qf_valid)
1046 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1047 err = NULL;
1048 }
1049 }
1050 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1051 {
1052 while (errornr--)
1053 {
1054 old_qf_ptr = qf_ptr;
1055 prev_index = qf_index;
1056 old_qf_fnum = qf_ptr->qf_fnum;
1057 do
1058 {
1059 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1060 {
1061 qf_ptr = old_qf_ptr;
1062 qf_index = prev_index;
1063 if (err != NULL)
1064 {
1065 EMSG(_(err));
1066 goto theend;
1067 }
1068 errornr = 0;
1069 break;
1070 }
1071 --qf_index;
1072 qf_ptr = qf_ptr->qf_prev;
1073 } while ((!qf_lists[qf_curlist].qf_nonevalid && !qf_ptr->qf_valid)
1074 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1075 err = NULL;
1076 }
1077 }
1078 else if (errornr != 0) /* go to specified number */
1079 {
1080 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1081 {
1082 --qf_index;
1083 qf_ptr = qf_ptr->qf_prev;
1084 }
1085 while (errornr > qf_index && qf_index < qf_lists[qf_curlist].qf_count
1086 && qf_ptr->qf_next != NULL)
1087 {
1088 ++qf_index;
1089 qf_ptr = qf_ptr->qf_next;
1090 }
1091 }
1092
1093#ifdef FEAT_WINDOWS
1094 qf_lists[qf_curlist].qf_index = qf_index;
1095 if (qf_win_pos_update(old_qf_index))
1096 /* No need to print the error message if it's visible in the error
1097 * window */
1098 print_message = FALSE;
1099
1100 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001101 * For ":helpgrep" find a help window or open one.
1102 */
1103 if (qf_ptr->qf_type == 1 && !curwin->w_buffer->b_help)
1104 {
1105 win_T *wp;
1106 int n;
1107
1108 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1109 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1110 break;
1111 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1112 win_enter(wp, TRUE);
1113 else
1114 {
1115 /*
1116 * Split off help window; put it at far top if no position
1117 * specified, the current window is vertically split and narrow.
1118 */
1119 n = WSP_HELP;
1120# ifdef FEAT_VERTSPLIT
1121 if (cmdmod.split == 0 && curwin->w_width != Columns
1122 && curwin->w_width < 80)
1123 n |= WSP_TOP;
1124# endif
1125 if (win_split(0, n) == FAIL)
1126 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001127 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001128
1129 if (curwin->w_height < p_hh)
1130 win_setheight((int)p_hh);
1131 }
1132
1133 if (!p_im)
1134 restart_edit = 0; /* don't want insert mode in help file */
1135 }
1136
1137 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 * If currently in the quickfix window, find another window to show the
1139 * file in.
1140 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001141 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 {
1143 /*
1144 * If there is no file specified, we don't know where to go.
1145 * But do advance, otherwise ":cn" gets stuck.
1146 */
1147 if (qf_ptr->qf_fnum == 0)
1148 goto theend;
1149
1150 /*
1151 * If there is only one window, create a new one above the quickfix
1152 * window.
1153 */
1154 if (firstwin == lastwin)
1155 {
1156 if (win_split(0, WSP_ABOVE) == FAIL)
1157 goto failed; /* not enough room for window */
1158 opened_window = TRUE; /* close it when fail */
1159 p_swb = empty_option; /* don't split again */
1160# ifdef FEAT_SCROLLBIND
1161 curwin->w_p_scb = FALSE;
1162# endif
1163 }
1164 else
1165 {
1166 /*
1167 * Try to find a window that shows the right buffer.
1168 * Default to the window just above the quickfix buffer.
1169 */
1170 win = curwin;
1171 altwin = NULL;
1172 for (;;)
1173 {
1174 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1175 break;
1176 if (win->w_prev == NULL)
1177 win = lastwin; /* wrap around the top */
1178 else
1179 win = win->w_prev; /* go to previous window */
1180
1181 if (bt_quickfix(win->w_buffer))
1182 {
1183 /* Didn't find it, go to the window before the quickfix
1184 * window. */
1185 if (altwin != NULL)
1186 win = altwin;
1187 else if (curwin->w_prev != NULL)
1188 win = curwin->w_prev;
1189 else
1190 win = curwin->w_next;
1191 break;
1192 }
1193
1194 /* Remember a usable window. */
1195 if (altwin == NULL && !win->w_p_pvw
1196 && win->w_buffer->b_p_bt[0] == NUL)
1197 altwin = win;
1198 }
1199
1200 win_goto(win);
1201 }
1202 }
1203#endif
1204
1205 /*
1206 * If there is a file name,
1207 * read the wanted file if needed, and check autowrite etc.
1208 */
1209 old_curbuf = curbuf;
1210 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001211
1212 if (qf_ptr->qf_fnum != 0)
1213 {
1214 if (qf_ptr->qf_type == 1)
1215 {
1216 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1217 * set b_p_ro flag). */
1218 if (!can_abandon(curbuf, forceit))
1219 {
1220 EMSG(_(e_nowrtmsg));
1221 ok = FALSE;
1222 }
1223 else
1224 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
1225 ECMD_HIDE + ECMD_SET_HELP);
1226 }
1227 else
1228 ok = buflist_getfile(qf_ptr->qf_fnum,
1229 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
1230 }
1231
1232 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 {
1234 /* When not switched to another buffer, still need to set pc mark */
1235 if (curbuf == old_curbuf)
1236 setpcmark();
1237
1238 /*
1239 * Go to line with error, unless qf_lnum is 0.
1240 */
1241 i = qf_ptr->qf_lnum;
1242 if (i > 0)
1243 {
1244 if (i > curbuf->b_ml.ml_line_count)
1245 i = curbuf->b_ml.ml_line_count;
1246 curwin->w_cursor.lnum = i;
1247 }
1248 if (qf_ptr->qf_col > 0)
1249 {
1250 curwin->w_cursor.col = qf_ptr->qf_col - 1;
1251 if (qf_ptr->qf_virt_col == TRUE)
1252 {
1253 /*
1254 * Check each character from the beginning of the error
1255 * line up to the error column. For each tab character
1256 * found, reduce the error column value by the length of
1257 * a tab character.
1258 */
1259 line = ml_get_curline();
1260 screen_col = 0;
1261 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1262 {
1263 if (*line == NUL)
1264 break;
1265 if (*line++ == '\t')
1266 {
1267 curwin->w_cursor.col -= 7 - (screen_col % 8);
1268 screen_col += 8 - (screen_col % 8);
1269 }
1270 else
1271 ++screen_col;
1272 }
1273 }
1274 check_cursor();
1275 }
1276 else
1277 beginline(BL_WHITE | BL_FIX);
1278
1279#ifdef FEAT_FOLDING
1280 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1281 foldOpenCursor();
1282#endif
1283 if (print_message)
1284 {
1285 /* Update the screen before showing the message */
1286 update_topline_redraw();
1287 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
1288 qf_lists[qf_curlist].qf_count,
1289 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1290 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1291 /* Add the message, skipping leading whitespace and newlines. */
1292 len = (int)STRLEN(IObuff);
1293 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1294
1295 /* Output the message. Overwrite to avoid scrolling when the 'O'
1296 * flag is present in 'shortmess'; But when not jumping, print the
1297 * whole message. */
1298 i = msg_scroll;
1299 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1300 msg_scroll = TRUE;
1301 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1302 msg_scroll = FALSE;
1303 msg_attr_keep(IObuff, 0, TRUE);
1304 msg_scroll = i;
1305 }
1306 }
1307 else
1308 {
1309#ifdef FEAT_WINDOWS
1310 if (opened_window)
1311 win_close(curwin, TRUE); /* Close opened window */
1312#endif
1313 if (qf_ptr->qf_fnum != 0)
1314 {
1315 /*
1316 * Couldn't open file, so put index back where it was. This could
1317 * happen if the file was readonly and we changed something.
1318 */
1319#ifdef FEAT_WINDOWS
1320failed:
1321#endif
1322 qf_ptr = old_qf_ptr;
1323 qf_index = old_qf_index;
1324 }
1325 }
1326theend:
1327 qf_lists[qf_curlist].qf_ptr = qf_ptr;
1328 qf_lists[qf_curlist].qf_index = qf_index;
1329#ifdef FEAT_WINDOWS
1330 if (p_swb != old_swb && opened_window)
1331 {
1332 /* Restore old 'switchbuf' value, but not when an autocommand or
1333 * modeline has changed the value. */
1334 if (p_swb == empty_option)
1335 p_swb = old_swb;
1336 else
1337 free_string_option(old_swb);
1338 }
1339#endif
1340}
1341
1342/*
1343 * ":clist": list all errors
1344 */
1345 void
1346qf_list(eap)
1347 exarg_T *eap;
1348{
1349 buf_T *buf;
1350 char_u *fname;
1351 struct qf_line *qfp;
1352 int i;
1353 int idx1 = 1;
1354 int idx2 = -1;
1355 int need_return = TRUE;
1356 int last_printed = 1;
1357 char_u *arg = eap->arg;
1358 int all = eap->forceit; /* if not :cl!, only show
1359 recognised errors */
1360
1361 if (qf_curlist >= qf_listcount || qf_lists[qf_curlist].qf_count == 0)
1362 {
1363 EMSG(_(e_quickfix));
1364 return;
1365 }
1366 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
1367 {
1368 EMSG(_(e_trailing));
1369 return;
1370 }
1371 i = qf_lists[qf_curlist].qf_count;
1372 if (idx1 < 0)
1373 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
1374 if (idx2 < 0)
1375 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
1376
1377 more_back_used = TRUE;
1378 if (qf_lists[qf_curlist].qf_nonevalid)
1379 all = TRUE;
1380 qfp = qf_lists[qf_curlist].qf_start;
1381 for (i = 1; !got_int && i <= qf_lists[qf_curlist].qf_count; )
1382 {
1383 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
1384 {
1385 if (need_return)
1386 {
1387 msg_putchar('\n');
1388 need_return = FALSE;
1389 }
1390 if (more_back == 0)
1391 {
1392 fname = NULL;
1393 if (qfp->qf_fnum != 0
1394 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
1395 {
1396 fname = buf->b_fname;
1397 if (qfp->qf_type == 1) /* :helpgrep */
1398 fname = gettail(fname);
1399 }
1400 if (fname == NULL)
1401 sprintf((char *)IObuff, "%2d", i);
1402 else
1403 sprintf((char *)IObuff, "%2d %s", i, (char *)fname);
1404 msg_outtrans_attr(IObuff, i == qf_lists[qf_curlist].qf_index
1405 ? hl_attr(HLF_L) : hl_attr(HLF_D));
1406 if (qfp->qf_lnum == 0)
1407 IObuff[0] = NUL;
1408 else if (qfp->qf_col == 0)
1409 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
1410 else
1411 sprintf((char *)IObuff, ":%ld col %d",
1412 qfp->qf_lnum, qfp->qf_col);
1413 sprintf((char *)IObuff + STRLEN(IObuff), "%s: ",
1414 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
1415 msg_puts_attr(IObuff, hl_attr(HLF_N));
1416 /* Remove newlines and leading whitespace from the text.
1417 * For an unrecognized line keep the indent, the compiler may
1418 * mark a word with ^^^^. */
1419 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
1420 ? skipwhite(qfp->qf_text) : qfp->qf_text,
1421 IObuff, IOSIZE);
1422 msg_prt_line(IObuff);
1423 out_flush(); /* show one line at a time */
1424 need_return = TRUE;
1425 last_printed = i;
1426 }
1427 }
1428 if (more_back)
1429 {
1430 /* scrolling backwards from the more-prompt */
1431 /* TODO: compute the number of items from the screen lines */
1432 more_back = more_back * 2 - 1;
1433 while (i > last_printed - more_back && i > idx1)
1434 {
1435 do
1436 {
1437 qfp = qfp->qf_prev;
1438 --i;
1439 }
1440 while (i > idx1 && !qfp->qf_valid && !all);
1441 }
1442 more_back = 0;
1443 }
1444 else
1445 {
1446 qfp = qfp->qf_next;
1447 ++i;
1448 }
1449 ui_breakcheck();
1450 }
1451 more_back_used = FALSE;
1452}
1453
1454/*
1455 * Remove newlines and leading whitespace from an error message.
1456 * Put the result in "buf[bufsize]".
1457 */
1458 static void
1459qf_fmt_text(text, buf, bufsize)
1460 char_u *text;
1461 char_u *buf;
1462 int bufsize;
1463{
1464 int i;
1465 char_u *p = text;
1466
1467 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
1468 {
1469 if (*p == '\n')
1470 {
1471 buf[i] = ' ';
1472 while (*++p != NUL)
1473 if (!vim_iswhite(*p) && *p != '\n')
1474 break;
1475 }
1476 else
1477 buf[i] = *p++;
1478 }
1479 buf[i] = NUL;
1480}
1481
1482/*
1483 * ":colder [count]": Up in the quickfix stack.
1484 * ":cnewer [count]": Down in the quickfix stack.
1485 */
1486 void
1487qf_age(eap)
1488 exarg_T *eap;
1489{
1490 int count;
1491
1492 if (eap->addr_count != 0)
1493 count = eap->line2;
1494 else
1495 count = 1;
1496 while (count--)
1497 {
1498 if (eap->cmdidx == CMD_colder)
1499 {
1500 if (qf_curlist == 0)
1501 {
1502 EMSG(_("E380: At bottom of quickfix stack"));
1503 return;
1504 }
1505 --qf_curlist;
1506 }
1507 else
1508 {
1509 if (qf_curlist >= qf_listcount - 1)
1510 {
1511 EMSG(_("E381: At top of quickfix stack"));
1512 return;
1513 }
1514 ++qf_curlist;
1515 }
1516 }
1517 qf_msg();
1518}
1519
1520 static void
1521qf_msg()
1522{
1523 smsg((char_u *)_("error list %d of %d; %d errors"),
1524 qf_curlist + 1, qf_listcount, qf_lists[qf_curlist].qf_count);
1525#ifdef FEAT_WINDOWS
1526 qf_update_buffer();
1527#endif
1528}
1529
1530/*
1531 * free the error list
1532 */
1533 static void
1534qf_free(idx)
1535 int idx;
1536{
1537 struct qf_line *qfp;
1538
1539 while (qf_lists[idx].qf_count)
1540 {
1541 qfp = qf_lists[idx].qf_start->qf_next;
1542 vim_free(qf_lists[idx].qf_start->qf_text);
1543 vim_free(qf_lists[idx].qf_start);
1544 qf_lists[idx].qf_start = qfp;
1545 --qf_lists[idx].qf_count;
1546 }
1547}
1548
1549/*
1550 * qf_mark_adjust: adjust marks
1551 */
1552 void
1553qf_mark_adjust(line1, line2, amount, amount_after)
1554 linenr_T line1;
1555 linenr_T line2;
1556 long amount;
1557 long amount_after;
1558{
1559 int i;
1560 struct qf_line *qfp;
1561 int idx;
1562
1563 for (idx = 0; idx < qf_listcount; ++idx)
1564 if (qf_lists[idx].qf_count)
1565 for (i = 0, qfp = qf_lists[idx].qf_start;
1566 i < qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
1567 if (qfp->qf_fnum == curbuf->b_fnum)
1568 {
1569 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
1570 {
1571 if (amount == MAXLNUM)
1572 qfp->qf_cleared = TRUE;
1573 else
1574 qfp->qf_lnum += amount;
1575 }
1576 else if (amount_after && qfp->qf_lnum > line2)
1577 qfp->qf_lnum += amount_after;
1578 }
1579}
1580
1581/*
1582 * Make a nice message out of the error character and the error number:
1583 * char number message
1584 * e or E 0 " error"
1585 * w or W 0 " warning"
1586 * i or I 0 " info"
1587 * 0 0 ""
1588 * other 0 " c"
1589 * e or E n " error n"
1590 * w or W n " warning n"
1591 * i or I n " info n"
1592 * 0 n " error n"
1593 * other n " c n"
1594 * 1 x "" :helpgrep
1595 */
1596 static char_u *
1597qf_types(c, nr)
1598 int c, nr;
1599{
1600 static char_u buf[20];
1601 static char_u cc[3];
1602 char_u *p;
1603
1604 if (c == 'W' || c == 'w')
1605 p = (char_u *)" warning";
1606 else if (c == 'I' || c == 'i')
1607 p = (char_u *)" info";
1608 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
1609 p = (char_u *)" error";
1610 else if (c == 0 || c == 1)
1611 p = (char_u *)"";
1612 else
1613 {
1614 cc[0] = ' ';
1615 cc[1] = c;
1616 cc[2] = NUL;
1617 p = cc;
1618 }
1619
1620 if (nr <= 0)
1621 return p;
1622
1623 sprintf((char *)buf, "%s %3d", (char *)p, nr);
1624 return buf;
1625}
1626
1627#if defined(FEAT_WINDOWS) || defined(PROTO)
1628/*
1629 * ":cwindow": open the quickfix window if we have errors to display,
1630 * close it if not.
1631 */
1632 void
1633ex_cwindow(eap)
1634 exarg_T *eap;
1635{
1636 win_T *win;
1637
1638 /*
1639 * Look for an existing quickfix window.
1640 */
1641 for (win = firstwin; win != NULL; win = win->w_next)
1642 if (bt_quickfix(win->w_buffer))
1643 break;
1644
1645 /*
1646 * If a quickfix window is open but we have no errors to display,
1647 * close the window. If a quickfix window is not open, then open
1648 * it if we have errors; otherwise, leave it closed.
1649 */
1650 if (qf_lists[qf_curlist].qf_nonevalid || qf_curlist >= qf_listcount)
1651 {
1652 if (win != NULL)
1653 ex_cclose(eap);
1654 }
1655 else if (win == NULL)
1656 ex_copen(eap);
1657}
1658
1659/*
1660 * ":cclose": close the window showing the list of errors.
1661 */
1662/*ARGSUSED*/
1663 void
1664ex_cclose(eap)
1665 exarg_T *eap;
1666{
1667 win_T *win;
1668
1669 /*
1670 * Find existing quickfix window and close it.
1671 */
1672 for (win = firstwin; win != NULL; win = win->w_next)
1673 if (bt_quickfix(win->w_buffer))
1674 break;
1675
1676 if (win != NULL)
1677 win_close(win, FALSE);
1678}
1679
1680/*
1681 * ":copen": open a window that shows the list of errors.
1682 */
1683 void
1684ex_copen(eap)
1685 exarg_T *eap;
1686{
1687 int height;
1688 buf_T *buf;
1689 win_T *win;
1690
1691 if (eap->addr_count != 0)
1692 height = eap->line2;
1693 else
1694 height = QF_WINHEIGHT;
1695
1696#ifdef FEAT_VISUAL
1697 reset_VIsual_and_resel(); /* stop Visual mode */
1698#endif
1699#ifdef FEAT_GUI
1700 need_mouse_correct = TRUE;
1701#endif
1702
1703 /*
1704 * Find existing quickfix window, or open a new one.
1705 */
1706 for (win = firstwin; win != NULL; win = win->w_next)
1707 if (bt_quickfix(win->w_buffer))
1708 break;
1709 if (win != NULL)
1710 win_goto(win);
1711 else
1712 {
1713 /* The current window becomes the previous window afterwards. */
1714 win = curwin;
1715
1716 /* Create the new window at the very bottom. */
1717 win_goto(lastwin);
1718 if (win_split(height, WSP_BELOW) == FAIL)
1719 return; /* not enough room for window */
1720#ifdef FEAT_SCROLLBIND
1721 curwin->w_p_scb = FALSE;
1722#endif
1723
1724 /*
1725 * Find existing quickfix buffer, or create a new one.
1726 */
1727 buf = qf_find_buf();
1728 if (buf == NULL)
1729 {
1730 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
1731 /* switch off 'swapfile' */
1732 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
1733 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
1734 OPT_LOCAL);
1735 set_option_value((char_u *)"bh", 0L, (char_u *)"delete", OPT_LOCAL);
1736 set_option_value((char_u *)"diff", 0L, (char_u *)"", OPT_LOCAL);
1737 }
1738 else if (buf != curbuf)
1739 set_curbuf(buf, DOBUF_GOTO);
1740
1741 /* Only set the height when there is no window to the side. */
1742 if (curwin->w_width == Columns)
1743 win_setheight(height);
1744 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
1745 if (win_valid(win))
1746 prevwin = win;
1747 }
1748
1749 /*
1750 * Fill the buffer with the quickfix list.
1751 */
1752 qf_fill_buffer();
1753
1754 curwin->w_cursor.lnum = qf_lists[qf_curlist].qf_index;
1755 curwin->w_cursor.col = 0;
1756 check_cursor();
1757 update_topline(); /* scroll to show the line */
1758}
1759
1760/*
1761 * Return the number of the current entry (line number in the quickfix
1762 * window).
1763 */
1764 linenr_T
1765qf_current_entry()
1766{
1767 return qf_lists[qf_curlist].qf_index;
1768}
1769
1770/*
1771 * Update the cursor position in the quickfix window to the current error.
1772 * Return TRUE if there is a quickfix window.
1773 */
1774 static int
1775qf_win_pos_update(old_qf_index)
1776 int old_qf_index; /* previous qf_index or zero */
1777{
1778 win_T *win;
1779 int qf_index = qf_lists[qf_curlist].qf_index;
1780
1781 /*
1782 * Put the cursor on the current error in the quickfix window, so that
1783 * it's viewable.
1784 */
1785 for (win = firstwin; win != NULL; win = win->w_next)
1786 if (bt_quickfix(win->w_buffer))
1787 break;
1788 if (win != NULL
1789 && qf_index <= win->w_buffer->b_ml.ml_line_count
1790 && old_qf_index != qf_index)
1791 {
1792 win_T *old_curwin = curwin;
1793
1794 curwin = win;
1795 curbuf = win->w_buffer;
1796 if (qf_index > old_qf_index)
1797 {
1798 curwin->w_redraw_top = old_qf_index;
1799 curwin->w_redraw_bot = qf_index;
1800 }
1801 else
1802 {
1803 curwin->w_redraw_top = qf_index;
1804 curwin->w_redraw_bot = old_qf_index;
1805 }
1806 curwin->w_cursor.lnum = qf_index;
1807 curwin->w_cursor.col = 0;
1808 update_topline(); /* scroll to show the line */
1809 redraw_later(VALID);
1810 curwin->w_redr_status = TRUE; /* update ruler */
1811 curwin = old_curwin;
1812 curbuf = curwin->w_buffer;
1813 }
1814 return win != NULL;
1815}
1816
1817/*
1818 * Find quickfix buffer.
1819 */
1820 static buf_T *
1821qf_find_buf()
1822{
1823 buf_T *buf;
1824
1825 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1826 if (bt_quickfix(buf))
1827 break;
1828 return buf;
1829}
1830
1831/*
1832 * Find the quickfix buffer. If it exists, update the contents.
1833 */
1834 static void
1835qf_update_buffer()
1836{
1837 buf_T *buf;
1838#ifdef FEAT_AUTOCMD
1839 aco_save_T aco;
1840#else
1841 buf_T *save_curbuf;
1842#endif
1843
1844 /* Check if a buffer for the quickfix list exists. Update it. */
1845 buf = qf_find_buf();
1846 if (buf != NULL)
1847 {
1848#ifdef FEAT_AUTOCMD
1849 /* set curwin/curbuf to buf and save a few things */
1850 aucmd_prepbuf(&aco, buf);
1851#else
1852 save_curbuf = curbuf;
1853 curbuf = buf;
1854#endif
1855
1856 qf_fill_buffer();
1857
1858#ifdef FEAT_AUTOCMD
1859 /* restore curwin/curbuf and a few other things */
1860 aucmd_restbuf(&aco);
1861#else
1862 curbuf = save_curbuf;
1863#endif
1864
1865 (void)qf_win_pos_update(0);
1866 }
1867}
1868
1869/*
1870 * Fill current buffer with quickfix errors, replacing any previous contents.
1871 * curbuf must be the quickfix buffer!
1872 */
1873 static void
1874qf_fill_buffer()
1875{
1876 linenr_T lnum;
1877 struct qf_line *qfp;
1878 buf_T *errbuf;
1879 int len;
1880 int old_KeyTyped = KeyTyped;
1881
1882 /* delete all existing lines */
1883 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
1884 (void)ml_delete((linenr_T)1, FALSE);
1885
1886 /* Check if there is anything to display */
1887 if (qf_curlist < qf_listcount)
1888 {
1889 /* Add one line for each error */
1890 qfp = qf_lists[qf_curlist].qf_start;
1891 for (lnum = 0; lnum < qf_lists[qf_curlist].qf_count; ++lnum)
1892 {
1893 if (qfp->qf_fnum != 0
1894 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
1895 && errbuf->b_fname != NULL)
1896 {
1897 if (qfp->qf_type == 1) /* :helpgrep */
1898 STRCPY(IObuff, gettail(errbuf->b_fname));
1899 else
1900 STRCPY(IObuff, errbuf->b_fname);
1901 len = (int)STRLEN(IObuff);
1902 }
1903 else
1904 len = 0;
1905 IObuff[len++] = '|';
1906
1907 if (qfp->qf_lnum > 0)
1908 {
1909 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
1910 len += (int)STRLEN(IObuff + len);
1911
1912 if (qfp->qf_col > 0)
1913 {
1914 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
1915 len += (int)STRLEN(IObuff + len);
1916 }
1917
1918 sprintf((char *)IObuff + len, "%s",
1919 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
1920 len += (int)STRLEN(IObuff + len);
1921 }
1922 IObuff[len++] = '|';
1923 IObuff[len++] = ' ';
1924
1925 /* Remove newlines and leading whitespace from the text.
1926 * For an unrecognized line keep the indent, the compiler may
1927 * mark a word with ^^^^. */
1928 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
1929 IObuff + len, IOSIZE - len);
1930
1931 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
1932 == FAIL)
1933 break;
1934 qfp = qfp->qf_next;
1935 }
1936 /* Delete the empty line which is now at the end */
1937 (void)ml_delete(lnum + 1, FALSE);
1938 }
1939
1940 /* correct cursor position */
1941 check_lnums(TRUE);
1942
1943 /* Set the 'filetype' to "qf" each time after filling the buffer. This
1944 * resembles reading a file into a buffer, it's more logical when using
1945 * autocommands. */
1946 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
1947 curbuf->b_p_ma = FALSE;
1948
1949#ifdef FEAT_AUTOCMD
1950 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
1951 FALSE, curbuf);
1952 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
1953 FALSE, curbuf);
1954#endif
1955
1956 /* make sure it will be redrawn */
1957 redraw_curbuf_later(NOT_VALID);
1958
1959 /* Restore KeyTyped, setting 'filetype' may reset it. */
1960 KeyTyped = old_KeyTyped;
1961}
1962
1963#endif /* FEAT_WINDOWS */
1964
1965/*
1966 * Return TRUE if "buf" is the quickfix buffer.
1967 */
1968 int
1969bt_quickfix(buf)
1970 buf_T *buf;
1971{
1972 return (buf->b_p_bt[0] == 'q');
1973}
1974
1975/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001976 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
1977 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 */
1979 int
1980bt_nofile(buf)
1981 buf_T *buf;
1982{
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001983 return (buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
1984 || buf->b_p_bt[0] == 'a';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985}
1986
1987/*
1988 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
1989 */
1990 int
1991bt_dontwrite(buf)
1992 buf_T *buf;
1993{
1994 return (buf->b_p_bt[0] == 'n');
1995}
1996
1997 int
1998bt_dontwrite_msg(buf)
1999 buf_T *buf;
2000{
2001 if (bt_dontwrite(buf))
2002 {
2003 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2004 return TRUE;
2005 }
2006 return FALSE;
2007}
2008
2009/*
2010 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2011 * and 'bufhidden'.
2012 */
2013 int
2014buf_hide(buf)
2015 buf_T *buf;
2016{
2017 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2018 switch (buf->b_p_bh[0])
2019 {
2020 case 'u': /* "unload" */
2021 case 'w': /* "wipe" */
2022 case 'd': return FALSE; /* "delete" */
2023 case 'h': return TRUE; /* "hide" */
2024 }
2025 return (p_hid || cmdmod.hide);
2026}
2027
2028/*
2029 * Used for ":make", ":grep" and ":grepadd".
2030 */
2031 void
2032ex_make(eap)
2033 exarg_T *eap;
2034{
2035 char_u *name;
2036 char_u *cmd;
2037 unsigned len;
2038
2039 autowrite_all();
2040 name = get_mef_name();
2041 if (name == NULL)
2042 return;
2043 mch_remove(name); /* in case it's not unique */
2044
2045 /*
2046 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2047 */
2048 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2049 if (*p_sp != NUL)
2050 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(name) + 3;
2051 cmd = alloc(len);
2052 if (cmd == NULL)
2053 return;
2054 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2055 (char *)p_shq);
2056 if (*p_sp != NUL)
2057 append_redir(cmd, p_sp, name);
2058 /*
2059 * Output a newline if there's something else than the :make command that
2060 * was typed (in which case the cursor is in column 0).
2061 */
2062 if (msg_col == 0)
2063 msg_didout = FALSE;
2064 msg_start();
2065 MSG_PUTS(":!");
2066 msg_outtrans(cmd); /* show what we are doing */
2067
2068 /* let the shell know if we are redirecting output or not */
2069 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2070
2071#ifdef AMIGA
2072 out_flush();
2073 /* read window status report and redraw before message */
2074 (void)char_avail();
2075#endif
2076
2077 if (qf_init(name, eap->cmdidx != CMD_make ? p_gefm : p_efm,
2078 eap->cmdidx != CMD_grepadd) > 0
2079 && !eap->forceit)
2080 qf_jump(0, 0, FALSE); /* display first error */
2081
2082 mch_remove(name);
2083 vim_free(name);
2084 vim_free(cmd);
2085}
2086
2087/*
2088 * Return the name for the errorfile, in allocated memory.
2089 * Find a new unique name when 'makeef' contains "##".
2090 * Returns NULL for error.
2091 */
2092 static char_u *
2093get_mef_name()
2094{
2095 char_u *p;
2096 char_u *name;
2097 static int start = -1;
2098 static int off = 0;
2099#ifdef HAVE_LSTAT
2100 struct stat sb;
2101#endif
2102
2103 if (*p_mef == NUL)
2104 {
2105 name = vim_tempname('e');
2106 if (name == NULL)
2107 EMSG(_(e_notmp));
2108 return name;
2109 }
2110
2111 for (p = p_mef; *p; ++p)
2112 if (p[0] == '#' && p[1] == '#')
2113 break;
2114
2115 if (*p == NUL)
2116 return vim_strsave(p_mef);
2117
2118 /* Keep trying until the name doesn't exist yet. */
2119 for (;;)
2120 {
2121 if (start == -1)
2122 start = mch_get_pid();
2123 else
2124 off += 19;
2125
2126 name = alloc((unsigned)STRLEN(p_mef) + 30);
2127 if (name == NULL)
2128 break;
2129 STRCPY(name, p_mef);
2130 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2131 STRCAT(name, p + 2);
2132 if (mch_getperm(name) < 0
2133#ifdef HAVE_LSTAT
2134 /* Don't accept a symbolic link, its a security risk. */
2135 && mch_lstat((char *)name, &sb) < 0
2136#endif
2137 )
2138 break;
2139 vim_free(name);
2140 }
2141 return name;
2142}
2143
2144/*
2145 * ":cc", ":crewind", ":cfirst" and ":clast".
2146 */
2147 void
2148ex_cc(eap)
2149 exarg_T *eap;
2150{
2151 qf_jump(0,
2152 eap->addr_count > 0
2153 ? (int)eap->line2
2154 : eap->cmdidx == CMD_cc
2155 ? 0
2156 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_cfirst)
2157 ? 1
2158 : 32767,
2159 eap->forceit);
2160}
2161
2162/*
2163 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
2164 */
2165 void
2166ex_cnext(eap)
2167 exarg_T *eap;
2168{
2169 qf_jump(eap->cmdidx == CMD_cnext
2170 ? FORWARD
2171 : eap->cmdidx == CMD_cnfile
2172 ? FORWARD_FILE
2173 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_cNfile)
2174 ? BACKWARD_FILE
2175 : BACKWARD,
2176 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
2177}
2178
2179/*
2180 * ":cfile" command.
2181 */
2182 void
2183ex_cfile(eap)
2184 exarg_T *eap;
2185{
2186 if (*eap->arg != NUL)
2187 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE);
2188 if (qf_init(p_ef, p_efm, TRUE) > 0 && eap->cmdidx == CMD_cfile)
2189 qf_jump(0, 0, eap->forceit); /* display first error */
2190}
2191
2192/*
2193 * ":helpgrep {pattern}"
2194 */
2195 void
2196ex_helpgrep(eap)
2197 exarg_T *eap;
2198{
2199 regmatch_T regmatch;
2200 char_u *save_cpo;
2201 char_u *p;
2202 int fcount;
2203 char_u **fnames;
2204 FILE *fd;
2205 int fi;
2206 struct qf_line *prevp = NULL;
2207 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002208#ifdef FEAT_MULTI_LANG
2209 char_u *lang;
2210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211
2212 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
2213 save_cpo = p_cpo;
2214 p_cpo = (char_u *)"";
2215
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002216#ifdef FEAT_MULTI_LANG
2217 /* Check for a specified language */
2218 lang = check_help_lang(eap->arg);
2219#endif
2220
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
2222 regmatch.rm_ic = FALSE;
2223 if (regmatch.regprog != NULL)
2224 {
2225 /* create a new quickfix list */
2226 qf_new_list();
2227
2228 /* Go through all directories in 'runtimepath' */
2229 p = p_rtp;
2230 while (*p != NUL && !got_int)
2231 {
2232 copy_option_part(&p, NameBuff, MAXPATHL, ",");
2233
2234 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
2235 add_pathsep(NameBuff);
2236 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
2237 if (gen_expand_wildcards(1, &NameBuff, &fcount,
2238 &fnames, EW_FILE|EW_SILENT) == OK
2239 && fcount > 0)
2240 {
2241 for (fi = 0; fi < fcount && !got_int; ++fi)
2242 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002243#ifdef FEAT_MULTI_LANG
2244 /* Skip files for a different language. */
2245 if (lang != NULL
2246 && STRNICMP(lang, fnames[fi]
2247 + STRLEN(fnames[fi]) - 3, 2) != 0
2248 && !(STRNICMP(lang, "en", 2) == 0
2249 && STRNICMP("txt", fnames[fi]
2250 + STRLEN(fnames[fi]) - 3, 3) == 0))
2251 continue;
2252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 fd = fopen((char *)fnames[fi], "r");
2254 if (fd != NULL)
2255 {
2256 lnum = 1;
2257 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
2258 {
2259 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
2260 {
2261 int l = STRLEN(IObuff);
2262
2263 /* remove trailing CR, LF, spaces, etc. */
2264 while (l > 0 && IObuff[l - 1] <= ' ')
2265 IObuff[--l] = NUL;
2266
2267 if (qf_add_entry(&prevp,
2268 NULL, /* dir */
2269 fnames[fi],
2270 IObuff,
2271 lnum,
2272 0, /* col */
2273 FALSE, /* virt_col */
2274 0, /* nr */
2275 1, /* type */
2276 TRUE /* valid */
2277 ) == FAIL)
2278 {
2279 got_int = TRUE;
2280 break;
2281 }
2282 }
2283 ++lnum;
2284 line_breakcheck();
2285 }
2286 fclose(fd);
2287 }
2288 }
2289 FreeWild(fcount, fnames);
2290 }
2291 }
2292 vim_free(regmatch.regprog);
2293
2294 qf_lists[qf_curlist].qf_nonevalid = FALSE;
2295 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
2296 qf_lists[qf_curlist].qf_index = 1;
2297 }
2298
2299 p_cpo = save_cpo;
2300
2301#ifdef FEAT_WINDOWS
2302 qf_update_buffer();
2303#endif
2304
2305 /* Jump to first match. */
2306 if (qf_lists[qf_curlist].qf_count > 0)
2307 qf_jump(0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002308 else
2309 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310}
2311
2312#endif /* FEAT_QUICKFIX */