blob: b939275a37a382760c951cbb0e6abf8d03fe7092 [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;
1127
1128 if (curwin->w_height < p_hh)
1129 win_setheight((int)p_hh);
1130 }
1131
1132 if (!p_im)
1133 restart_edit = 0; /* don't want insert mode in help file */
1134 }
1135
1136 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 * If currently in the quickfix window, find another window to show the
1138 * file in.
1139 */
1140 if (bt_quickfix(curbuf))
1141 {
1142 /*
1143 * If there is no file specified, we don't know where to go.
1144 * But do advance, otherwise ":cn" gets stuck.
1145 */
1146 if (qf_ptr->qf_fnum == 0)
1147 goto theend;
1148
1149 /*
1150 * If there is only one window, create a new one above the quickfix
1151 * window.
1152 */
1153 if (firstwin == lastwin)
1154 {
1155 if (win_split(0, WSP_ABOVE) == FAIL)
1156 goto failed; /* not enough room for window */
1157 opened_window = TRUE; /* close it when fail */
1158 p_swb = empty_option; /* don't split again */
1159# ifdef FEAT_SCROLLBIND
1160 curwin->w_p_scb = FALSE;
1161# endif
1162 }
1163 else
1164 {
1165 /*
1166 * Try to find a window that shows the right buffer.
1167 * Default to the window just above the quickfix buffer.
1168 */
1169 win = curwin;
1170 altwin = NULL;
1171 for (;;)
1172 {
1173 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1174 break;
1175 if (win->w_prev == NULL)
1176 win = lastwin; /* wrap around the top */
1177 else
1178 win = win->w_prev; /* go to previous window */
1179
1180 if (bt_quickfix(win->w_buffer))
1181 {
1182 /* Didn't find it, go to the window before the quickfix
1183 * window. */
1184 if (altwin != NULL)
1185 win = altwin;
1186 else if (curwin->w_prev != NULL)
1187 win = curwin->w_prev;
1188 else
1189 win = curwin->w_next;
1190 break;
1191 }
1192
1193 /* Remember a usable window. */
1194 if (altwin == NULL && !win->w_p_pvw
1195 && win->w_buffer->b_p_bt[0] == NUL)
1196 altwin = win;
1197 }
1198
1199 win_goto(win);
1200 }
1201 }
1202#endif
1203
1204 /*
1205 * If there is a file name,
1206 * read the wanted file if needed, and check autowrite etc.
1207 */
1208 old_curbuf = curbuf;
1209 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001210
1211 if (qf_ptr->qf_fnum != 0)
1212 {
1213 if (qf_ptr->qf_type == 1)
1214 {
1215 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1216 * set b_p_ro flag). */
1217 if (!can_abandon(curbuf, forceit))
1218 {
1219 EMSG(_(e_nowrtmsg));
1220 ok = FALSE;
1221 }
1222 else
1223 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
1224 ECMD_HIDE + ECMD_SET_HELP);
1225 }
1226 else
1227 ok = buflist_getfile(qf_ptr->qf_fnum,
1228 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
1229 }
1230
1231 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 {
1233 /* When not switched to another buffer, still need to set pc mark */
1234 if (curbuf == old_curbuf)
1235 setpcmark();
1236
1237 /*
1238 * Go to line with error, unless qf_lnum is 0.
1239 */
1240 i = qf_ptr->qf_lnum;
1241 if (i > 0)
1242 {
1243 if (i > curbuf->b_ml.ml_line_count)
1244 i = curbuf->b_ml.ml_line_count;
1245 curwin->w_cursor.lnum = i;
1246 }
1247 if (qf_ptr->qf_col > 0)
1248 {
1249 curwin->w_cursor.col = qf_ptr->qf_col - 1;
1250 if (qf_ptr->qf_virt_col == TRUE)
1251 {
1252 /*
1253 * Check each character from the beginning of the error
1254 * line up to the error column. For each tab character
1255 * found, reduce the error column value by the length of
1256 * a tab character.
1257 */
1258 line = ml_get_curline();
1259 screen_col = 0;
1260 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1261 {
1262 if (*line == NUL)
1263 break;
1264 if (*line++ == '\t')
1265 {
1266 curwin->w_cursor.col -= 7 - (screen_col % 8);
1267 screen_col += 8 - (screen_col % 8);
1268 }
1269 else
1270 ++screen_col;
1271 }
1272 }
1273 check_cursor();
1274 }
1275 else
1276 beginline(BL_WHITE | BL_FIX);
1277
1278#ifdef FEAT_FOLDING
1279 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1280 foldOpenCursor();
1281#endif
1282 if (print_message)
1283 {
1284 /* Update the screen before showing the message */
1285 update_topline_redraw();
1286 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
1287 qf_lists[qf_curlist].qf_count,
1288 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1289 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1290 /* Add the message, skipping leading whitespace and newlines. */
1291 len = (int)STRLEN(IObuff);
1292 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1293
1294 /* Output the message. Overwrite to avoid scrolling when the 'O'
1295 * flag is present in 'shortmess'; But when not jumping, print the
1296 * whole message. */
1297 i = msg_scroll;
1298 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1299 msg_scroll = TRUE;
1300 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1301 msg_scroll = FALSE;
1302 msg_attr_keep(IObuff, 0, TRUE);
1303 msg_scroll = i;
1304 }
1305 }
1306 else
1307 {
1308#ifdef FEAT_WINDOWS
1309 if (opened_window)
1310 win_close(curwin, TRUE); /* Close opened window */
1311#endif
1312 if (qf_ptr->qf_fnum != 0)
1313 {
1314 /*
1315 * Couldn't open file, so put index back where it was. This could
1316 * happen if the file was readonly and we changed something.
1317 */
1318#ifdef FEAT_WINDOWS
1319failed:
1320#endif
1321 qf_ptr = old_qf_ptr;
1322 qf_index = old_qf_index;
1323 }
1324 }
1325theend:
1326 qf_lists[qf_curlist].qf_ptr = qf_ptr;
1327 qf_lists[qf_curlist].qf_index = qf_index;
1328#ifdef FEAT_WINDOWS
1329 if (p_swb != old_swb && opened_window)
1330 {
1331 /* Restore old 'switchbuf' value, but not when an autocommand or
1332 * modeline has changed the value. */
1333 if (p_swb == empty_option)
1334 p_swb = old_swb;
1335 else
1336 free_string_option(old_swb);
1337 }
1338#endif
1339}
1340
1341/*
1342 * ":clist": list all errors
1343 */
1344 void
1345qf_list(eap)
1346 exarg_T *eap;
1347{
1348 buf_T *buf;
1349 char_u *fname;
1350 struct qf_line *qfp;
1351 int i;
1352 int idx1 = 1;
1353 int idx2 = -1;
1354 int need_return = TRUE;
1355 int last_printed = 1;
1356 char_u *arg = eap->arg;
1357 int all = eap->forceit; /* if not :cl!, only show
1358 recognised errors */
1359
1360 if (qf_curlist >= qf_listcount || qf_lists[qf_curlist].qf_count == 0)
1361 {
1362 EMSG(_(e_quickfix));
1363 return;
1364 }
1365 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
1366 {
1367 EMSG(_(e_trailing));
1368 return;
1369 }
1370 i = qf_lists[qf_curlist].qf_count;
1371 if (idx1 < 0)
1372 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
1373 if (idx2 < 0)
1374 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
1375
1376 more_back_used = TRUE;
1377 if (qf_lists[qf_curlist].qf_nonevalid)
1378 all = TRUE;
1379 qfp = qf_lists[qf_curlist].qf_start;
1380 for (i = 1; !got_int && i <= qf_lists[qf_curlist].qf_count; )
1381 {
1382 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
1383 {
1384 if (need_return)
1385 {
1386 msg_putchar('\n');
1387 need_return = FALSE;
1388 }
1389 if (more_back == 0)
1390 {
1391 fname = NULL;
1392 if (qfp->qf_fnum != 0
1393 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
1394 {
1395 fname = buf->b_fname;
1396 if (qfp->qf_type == 1) /* :helpgrep */
1397 fname = gettail(fname);
1398 }
1399 if (fname == NULL)
1400 sprintf((char *)IObuff, "%2d", i);
1401 else
1402 sprintf((char *)IObuff, "%2d %s", i, (char *)fname);
1403 msg_outtrans_attr(IObuff, i == qf_lists[qf_curlist].qf_index
1404 ? hl_attr(HLF_L) : hl_attr(HLF_D));
1405 if (qfp->qf_lnum == 0)
1406 IObuff[0] = NUL;
1407 else if (qfp->qf_col == 0)
1408 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
1409 else
1410 sprintf((char *)IObuff, ":%ld col %d",
1411 qfp->qf_lnum, qfp->qf_col);
1412 sprintf((char *)IObuff + STRLEN(IObuff), "%s: ",
1413 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
1414 msg_puts_attr(IObuff, hl_attr(HLF_N));
1415 /* Remove newlines and leading whitespace from the text.
1416 * For an unrecognized line keep the indent, the compiler may
1417 * mark a word with ^^^^. */
1418 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
1419 ? skipwhite(qfp->qf_text) : qfp->qf_text,
1420 IObuff, IOSIZE);
1421 msg_prt_line(IObuff);
1422 out_flush(); /* show one line at a time */
1423 need_return = TRUE;
1424 last_printed = i;
1425 }
1426 }
1427 if (more_back)
1428 {
1429 /* scrolling backwards from the more-prompt */
1430 /* TODO: compute the number of items from the screen lines */
1431 more_back = more_back * 2 - 1;
1432 while (i > last_printed - more_back && i > idx1)
1433 {
1434 do
1435 {
1436 qfp = qfp->qf_prev;
1437 --i;
1438 }
1439 while (i > idx1 && !qfp->qf_valid && !all);
1440 }
1441 more_back = 0;
1442 }
1443 else
1444 {
1445 qfp = qfp->qf_next;
1446 ++i;
1447 }
1448 ui_breakcheck();
1449 }
1450 more_back_used = FALSE;
1451}
1452
1453/*
1454 * Remove newlines and leading whitespace from an error message.
1455 * Put the result in "buf[bufsize]".
1456 */
1457 static void
1458qf_fmt_text(text, buf, bufsize)
1459 char_u *text;
1460 char_u *buf;
1461 int bufsize;
1462{
1463 int i;
1464 char_u *p = text;
1465
1466 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
1467 {
1468 if (*p == '\n')
1469 {
1470 buf[i] = ' ';
1471 while (*++p != NUL)
1472 if (!vim_iswhite(*p) && *p != '\n')
1473 break;
1474 }
1475 else
1476 buf[i] = *p++;
1477 }
1478 buf[i] = NUL;
1479}
1480
1481/*
1482 * ":colder [count]": Up in the quickfix stack.
1483 * ":cnewer [count]": Down in the quickfix stack.
1484 */
1485 void
1486qf_age(eap)
1487 exarg_T *eap;
1488{
1489 int count;
1490
1491 if (eap->addr_count != 0)
1492 count = eap->line2;
1493 else
1494 count = 1;
1495 while (count--)
1496 {
1497 if (eap->cmdidx == CMD_colder)
1498 {
1499 if (qf_curlist == 0)
1500 {
1501 EMSG(_("E380: At bottom of quickfix stack"));
1502 return;
1503 }
1504 --qf_curlist;
1505 }
1506 else
1507 {
1508 if (qf_curlist >= qf_listcount - 1)
1509 {
1510 EMSG(_("E381: At top of quickfix stack"));
1511 return;
1512 }
1513 ++qf_curlist;
1514 }
1515 }
1516 qf_msg();
1517}
1518
1519 static void
1520qf_msg()
1521{
1522 smsg((char_u *)_("error list %d of %d; %d errors"),
1523 qf_curlist + 1, qf_listcount, qf_lists[qf_curlist].qf_count);
1524#ifdef FEAT_WINDOWS
1525 qf_update_buffer();
1526#endif
1527}
1528
1529/*
1530 * free the error list
1531 */
1532 static void
1533qf_free(idx)
1534 int idx;
1535{
1536 struct qf_line *qfp;
1537
1538 while (qf_lists[idx].qf_count)
1539 {
1540 qfp = qf_lists[idx].qf_start->qf_next;
1541 vim_free(qf_lists[idx].qf_start->qf_text);
1542 vim_free(qf_lists[idx].qf_start);
1543 qf_lists[idx].qf_start = qfp;
1544 --qf_lists[idx].qf_count;
1545 }
1546}
1547
1548/*
1549 * qf_mark_adjust: adjust marks
1550 */
1551 void
1552qf_mark_adjust(line1, line2, amount, amount_after)
1553 linenr_T line1;
1554 linenr_T line2;
1555 long amount;
1556 long amount_after;
1557{
1558 int i;
1559 struct qf_line *qfp;
1560 int idx;
1561
1562 for (idx = 0; idx < qf_listcount; ++idx)
1563 if (qf_lists[idx].qf_count)
1564 for (i = 0, qfp = qf_lists[idx].qf_start;
1565 i < qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
1566 if (qfp->qf_fnum == curbuf->b_fnum)
1567 {
1568 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
1569 {
1570 if (amount == MAXLNUM)
1571 qfp->qf_cleared = TRUE;
1572 else
1573 qfp->qf_lnum += amount;
1574 }
1575 else if (amount_after && qfp->qf_lnum > line2)
1576 qfp->qf_lnum += amount_after;
1577 }
1578}
1579
1580/*
1581 * Make a nice message out of the error character and the error number:
1582 * char number message
1583 * e or E 0 " error"
1584 * w or W 0 " warning"
1585 * i or I 0 " info"
1586 * 0 0 ""
1587 * other 0 " c"
1588 * e or E n " error n"
1589 * w or W n " warning n"
1590 * i or I n " info n"
1591 * 0 n " error n"
1592 * other n " c n"
1593 * 1 x "" :helpgrep
1594 */
1595 static char_u *
1596qf_types(c, nr)
1597 int c, nr;
1598{
1599 static char_u buf[20];
1600 static char_u cc[3];
1601 char_u *p;
1602
1603 if (c == 'W' || c == 'w')
1604 p = (char_u *)" warning";
1605 else if (c == 'I' || c == 'i')
1606 p = (char_u *)" info";
1607 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
1608 p = (char_u *)" error";
1609 else if (c == 0 || c == 1)
1610 p = (char_u *)"";
1611 else
1612 {
1613 cc[0] = ' ';
1614 cc[1] = c;
1615 cc[2] = NUL;
1616 p = cc;
1617 }
1618
1619 if (nr <= 0)
1620 return p;
1621
1622 sprintf((char *)buf, "%s %3d", (char *)p, nr);
1623 return buf;
1624}
1625
1626#if defined(FEAT_WINDOWS) || defined(PROTO)
1627/*
1628 * ":cwindow": open the quickfix window if we have errors to display,
1629 * close it if not.
1630 */
1631 void
1632ex_cwindow(eap)
1633 exarg_T *eap;
1634{
1635 win_T *win;
1636
1637 /*
1638 * Look for an existing quickfix window.
1639 */
1640 for (win = firstwin; win != NULL; win = win->w_next)
1641 if (bt_quickfix(win->w_buffer))
1642 break;
1643
1644 /*
1645 * If a quickfix window is open but we have no errors to display,
1646 * close the window. If a quickfix window is not open, then open
1647 * it if we have errors; otherwise, leave it closed.
1648 */
1649 if (qf_lists[qf_curlist].qf_nonevalid || qf_curlist >= qf_listcount)
1650 {
1651 if (win != NULL)
1652 ex_cclose(eap);
1653 }
1654 else if (win == NULL)
1655 ex_copen(eap);
1656}
1657
1658/*
1659 * ":cclose": close the window showing the list of errors.
1660 */
1661/*ARGSUSED*/
1662 void
1663ex_cclose(eap)
1664 exarg_T *eap;
1665{
1666 win_T *win;
1667
1668 /*
1669 * Find existing quickfix window and close it.
1670 */
1671 for (win = firstwin; win != NULL; win = win->w_next)
1672 if (bt_quickfix(win->w_buffer))
1673 break;
1674
1675 if (win != NULL)
1676 win_close(win, FALSE);
1677}
1678
1679/*
1680 * ":copen": open a window that shows the list of errors.
1681 */
1682 void
1683ex_copen(eap)
1684 exarg_T *eap;
1685{
1686 int height;
1687 buf_T *buf;
1688 win_T *win;
1689
1690 if (eap->addr_count != 0)
1691 height = eap->line2;
1692 else
1693 height = QF_WINHEIGHT;
1694
1695#ifdef FEAT_VISUAL
1696 reset_VIsual_and_resel(); /* stop Visual mode */
1697#endif
1698#ifdef FEAT_GUI
1699 need_mouse_correct = TRUE;
1700#endif
1701
1702 /*
1703 * Find existing quickfix window, or open a new one.
1704 */
1705 for (win = firstwin; win != NULL; win = win->w_next)
1706 if (bt_quickfix(win->w_buffer))
1707 break;
1708 if (win != NULL)
1709 win_goto(win);
1710 else
1711 {
1712 /* The current window becomes the previous window afterwards. */
1713 win = curwin;
1714
1715 /* Create the new window at the very bottom. */
1716 win_goto(lastwin);
1717 if (win_split(height, WSP_BELOW) == FAIL)
1718 return; /* not enough room for window */
1719#ifdef FEAT_SCROLLBIND
1720 curwin->w_p_scb = FALSE;
1721#endif
1722
1723 /*
1724 * Find existing quickfix buffer, or create a new one.
1725 */
1726 buf = qf_find_buf();
1727 if (buf == NULL)
1728 {
1729 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
1730 /* switch off 'swapfile' */
1731 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
1732 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
1733 OPT_LOCAL);
1734 set_option_value((char_u *)"bh", 0L, (char_u *)"delete", OPT_LOCAL);
1735 set_option_value((char_u *)"diff", 0L, (char_u *)"", OPT_LOCAL);
1736 }
1737 else if (buf != curbuf)
1738 set_curbuf(buf, DOBUF_GOTO);
1739
1740 /* Only set the height when there is no window to the side. */
1741 if (curwin->w_width == Columns)
1742 win_setheight(height);
1743 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
1744 if (win_valid(win))
1745 prevwin = win;
1746 }
1747
1748 /*
1749 * Fill the buffer with the quickfix list.
1750 */
1751 qf_fill_buffer();
1752
1753 curwin->w_cursor.lnum = qf_lists[qf_curlist].qf_index;
1754 curwin->w_cursor.col = 0;
1755 check_cursor();
1756 update_topline(); /* scroll to show the line */
1757}
1758
1759/*
1760 * Return the number of the current entry (line number in the quickfix
1761 * window).
1762 */
1763 linenr_T
1764qf_current_entry()
1765{
1766 return qf_lists[qf_curlist].qf_index;
1767}
1768
1769/*
1770 * Update the cursor position in the quickfix window to the current error.
1771 * Return TRUE if there is a quickfix window.
1772 */
1773 static int
1774qf_win_pos_update(old_qf_index)
1775 int old_qf_index; /* previous qf_index or zero */
1776{
1777 win_T *win;
1778 int qf_index = qf_lists[qf_curlist].qf_index;
1779
1780 /*
1781 * Put the cursor on the current error in the quickfix window, so that
1782 * it's viewable.
1783 */
1784 for (win = firstwin; win != NULL; win = win->w_next)
1785 if (bt_quickfix(win->w_buffer))
1786 break;
1787 if (win != NULL
1788 && qf_index <= win->w_buffer->b_ml.ml_line_count
1789 && old_qf_index != qf_index)
1790 {
1791 win_T *old_curwin = curwin;
1792
1793 curwin = win;
1794 curbuf = win->w_buffer;
1795 if (qf_index > old_qf_index)
1796 {
1797 curwin->w_redraw_top = old_qf_index;
1798 curwin->w_redraw_bot = qf_index;
1799 }
1800 else
1801 {
1802 curwin->w_redraw_top = qf_index;
1803 curwin->w_redraw_bot = old_qf_index;
1804 }
1805 curwin->w_cursor.lnum = qf_index;
1806 curwin->w_cursor.col = 0;
1807 update_topline(); /* scroll to show the line */
1808 redraw_later(VALID);
1809 curwin->w_redr_status = TRUE; /* update ruler */
1810 curwin = old_curwin;
1811 curbuf = curwin->w_buffer;
1812 }
1813 return win != NULL;
1814}
1815
1816/*
1817 * Find quickfix buffer.
1818 */
1819 static buf_T *
1820qf_find_buf()
1821{
1822 buf_T *buf;
1823
1824 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1825 if (bt_quickfix(buf))
1826 break;
1827 return buf;
1828}
1829
1830/*
1831 * Find the quickfix buffer. If it exists, update the contents.
1832 */
1833 static void
1834qf_update_buffer()
1835{
1836 buf_T *buf;
1837#ifdef FEAT_AUTOCMD
1838 aco_save_T aco;
1839#else
1840 buf_T *save_curbuf;
1841#endif
1842
1843 /* Check if a buffer for the quickfix list exists. Update it. */
1844 buf = qf_find_buf();
1845 if (buf != NULL)
1846 {
1847#ifdef FEAT_AUTOCMD
1848 /* set curwin/curbuf to buf and save a few things */
1849 aucmd_prepbuf(&aco, buf);
1850#else
1851 save_curbuf = curbuf;
1852 curbuf = buf;
1853#endif
1854
1855 qf_fill_buffer();
1856
1857#ifdef FEAT_AUTOCMD
1858 /* restore curwin/curbuf and a few other things */
1859 aucmd_restbuf(&aco);
1860#else
1861 curbuf = save_curbuf;
1862#endif
1863
1864 (void)qf_win_pos_update(0);
1865 }
1866}
1867
1868/*
1869 * Fill current buffer with quickfix errors, replacing any previous contents.
1870 * curbuf must be the quickfix buffer!
1871 */
1872 static void
1873qf_fill_buffer()
1874{
1875 linenr_T lnum;
1876 struct qf_line *qfp;
1877 buf_T *errbuf;
1878 int len;
1879 int old_KeyTyped = KeyTyped;
1880
1881 /* delete all existing lines */
1882 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
1883 (void)ml_delete((linenr_T)1, FALSE);
1884
1885 /* Check if there is anything to display */
1886 if (qf_curlist < qf_listcount)
1887 {
1888 /* Add one line for each error */
1889 qfp = qf_lists[qf_curlist].qf_start;
1890 for (lnum = 0; lnum < qf_lists[qf_curlist].qf_count; ++lnum)
1891 {
1892 if (qfp->qf_fnum != 0
1893 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
1894 && errbuf->b_fname != NULL)
1895 {
1896 if (qfp->qf_type == 1) /* :helpgrep */
1897 STRCPY(IObuff, gettail(errbuf->b_fname));
1898 else
1899 STRCPY(IObuff, errbuf->b_fname);
1900 len = (int)STRLEN(IObuff);
1901 }
1902 else
1903 len = 0;
1904 IObuff[len++] = '|';
1905
1906 if (qfp->qf_lnum > 0)
1907 {
1908 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
1909 len += (int)STRLEN(IObuff + len);
1910
1911 if (qfp->qf_col > 0)
1912 {
1913 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
1914 len += (int)STRLEN(IObuff + len);
1915 }
1916
1917 sprintf((char *)IObuff + len, "%s",
1918 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
1919 len += (int)STRLEN(IObuff + len);
1920 }
1921 IObuff[len++] = '|';
1922 IObuff[len++] = ' ';
1923
1924 /* Remove newlines and leading whitespace from the text.
1925 * For an unrecognized line keep the indent, the compiler may
1926 * mark a word with ^^^^. */
1927 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
1928 IObuff + len, IOSIZE - len);
1929
1930 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
1931 == FAIL)
1932 break;
1933 qfp = qfp->qf_next;
1934 }
1935 /* Delete the empty line which is now at the end */
1936 (void)ml_delete(lnum + 1, FALSE);
1937 }
1938
1939 /* correct cursor position */
1940 check_lnums(TRUE);
1941
1942 /* Set the 'filetype' to "qf" each time after filling the buffer. This
1943 * resembles reading a file into a buffer, it's more logical when using
1944 * autocommands. */
1945 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
1946 curbuf->b_p_ma = FALSE;
1947
1948#ifdef FEAT_AUTOCMD
1949 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
1950 FALSE, curbuf);
1951 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
1952 FALSE, curbuf);
1953#endif
1954
1955 /* make sure it will be redrawn */
1956 redraw_curbuf_later(NOT_VALID);
1957
1958 /* Restore KeyTyped, setting 'filetype' may reset it. */
1959 KeyTyped = old_KeyTyped;
1960}
1961
1962#endif /* FEAT_WINDOWS */
1963
1964/*
1965 * Return TRUE if "buf" is the quickfix buffer.
1966 */
1967 int
1968bt_quickfix(buf)
1969 buf_T *buf;
1970{
1971 return (buf->b_p_bt[0] == 'q');
1972}
1973
1974/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001975 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
1976 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 */
1978 int
1979bt_nofile(buf)
1980 buf_T *buf;
1981{
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001982 return (buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
1983 || buf->b_p_bt[0] == 'a';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984}
1985
1986/*
1987 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
1988 */
1989 int
1990bt_dontwrite(buf)
1991 buf_T *buf;
1992{
1993 return (buf->b_p_bt[0] == 'n');
1994}
1995
1996 int
1997bt_dontwrite_msg(buf)
1998 buf_T *buf;
1999{
2000 if (bt_dontwrite(buf))
2001 {
2002 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2003 return TRUE;
2004 }
2005 return FALSE;
2006}
2007
2008/*
2009 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2010 * and 'bufhidden'.
2011 */
2012 int
2013buf_hide(buf)
2014 buf_T *buf;
2015{
2016 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2017 switch (buf->b_p_bh[0])
2018 {
2019 case 'u': /* "unload" */
2020 case 'w': /* "wipe" */
2021 case 'd': return FALSE; /* "delete" */
2022 case 'h': return TRUE; /* "hide" */
2023 }
2024 return (p_hid || cmdmod.hide);
2025}
2026
2027/*
2028 * Used for ":make", ":grep" and ":grepadd".
2029 */
2030 void
2031ex_make(eap)
2032 exarg_T *eap;
2033{
2034 char_u *name;
2035 char_u *cmd;
2036 unsigned len;
2037
2038 autowrite_all();
2039 name = get_mef_name();
2040 if (name == NULL)
2041 return;
2042 mch_remove(name); /* in case it's not unique */
2043
2044 /*
2045 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2046 */
2047 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2048 if (*p_sp != NUL)
2049 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(name) + 3;
2050 cmd = alloc(len);
2051 if (cmd == NULL)
2052 return;
2053 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2054 (char *)p_shq);
2055 if (*p_sp != NUL)
2056 append_redir(cmd, p_sp, name);
2057 /*
2058 * Output a newline if there's something else than the :make command that
2059 * was typed (in which case the cursor is in column 0).
2060 */
2061 if (msg_col == 0)
2062 msg_didout = FALSE;
2063 msg_start();
2064 MSG_PUTS(":!");
2065 msg_outtrans(cmd); /* show what we are doing */
2066
2067 /* let the shell know if we are redirecting output or not */
2068 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2069
2070#ifdef AMIGA
2071 out_flush();
2072 /* read window status report and redraw before message */
2073 (void)char_avail();
2074#endif
2075
2076 if (qf_init(name, eap->cmdidx != CMD_make ? p_gefm : p_efm,
2077 eap->cmdidx != CMD_grepadd) > 0
2078 && !eap->forceit)
2079 qf_jump(0, 0, FALSE); /* display first error */
2080
2081 mch_remove(name);
2082 vim_free(name);
2083 vim_free(cmd);
2084}
2085
2086/*
2087 * Return the name for the errorfile, in allocated memory.
2088 * Find a new unique name when 'makeef' contains "##".
2089 * Returns NULL for error.
2090 */
2091 static char_u *
2092get_mef_name()
2093{
2094 char_u *p;
2095 char_u *name;
2096 static int start = -1;
2097 static int off = 0;
2098#ifdef HAVE_LSTAT
2099 struct stat sb;
2100#endif
2101
2102 if (*p_mef == NUL)
2103 {
2104 name = vim_tempname('e');
2105 if (name == NULL)
2106 EMSG(_(e_notmp));
2107 return name;
2108 }
2109
2110 for (p = p_mef; *p; ++p)
2111 if (p[0] == '#' && p[1] == '#')
2112 break;
2113
2114 if (*p == NUL)
2115 return vim_strsave(p_mef);
2116
2117 /* Keep trying until the name doesn't exist yet. */
2118 for (;;)
2119 {
2120 if (start == -1)
2121 start = mch_get_pid();
2122 else
2123 off += 19;
2124
2125 name = alloc((unsigned)STRLEN(p_mef) + 30);
2126 if (name == NULL)
2127 break;
2128 STRCPY(name, p_mef);
2129 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2130 STRCAT(name, p + 2);
2131 if (mch_getperm(name) < 0
2132#ifdef HAVE_LSTAT
2133 /* Don't accept a symbolic link, its a security risk. */
2134 && mch_lstat((char *)name, &sb) < 0
2135#endif
2136 )
2137 break;
2138 vim_free(name);
2139 }
2140 return name;
2141}
2142
2143/*
2144 * ":cc", ":crewind", ":cfirst" and ":clast".
2145 */
2146 void
2147ex_cc(eap)
2148 exarg_T *eap;
2149{
2150 qf_jump(0,
2151 eap->addr_count > 0
2152 ? (int)eap->line2
2153 : eap->cmdidx == CMD_cc
2154 ? 0
2155 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_cfirst)
2156 ? 1
2157 : 32767,
2158 eap->forceit);
2159}
2160
2161/*
2162 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
2163 */
2164 void
2165ex_cnext(eap)
2166 exarg_T *eap;
2167{
2168 qf_jump(eap->cmdidx == CMD_cnext
2169 ? FORWARD
2170 : eap->cmdidx == CMD_cnfile
2171 ? FORWARD_FILE
2172 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_cNfile)
2173 ? BACKWARD_FILE
2174 : BACKWARD,
2175 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
2176}
2177
2178/*
2179 * ":cfile" command.
2180 */
2181 void
2182ex_cfile(eap)
2183 exarg_T *eap;
2184{
2185 if (*eap->arg != NUL)
2186 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE);
2187 if (qf_init(p_ef, p_efm, TRUE) > 0 && eap->cmdidx == CMD_cfile)
2188 qf_jump(0, 0, eap->forceit); /* display first error */
2189}
2190
2191/*
2192 * ":helpgrep {pattern}"
2193 */
2194 void
2195ex_helpgrep(eap)
2196 exarg_T *eap;
2197{
2198 regmatch_T regmatch;
2199 char_u *save_cpo;
2200 char_u *p;
2201 int fcount;
2202 char_u **fnames;
2203 FILE *fd;
2204 int fi;
2205 struct qf_line *prevp = NULL;
2206 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002207#ifdef FEAT_MULTI_LANG
2208 char_u *lang;
2209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210
2211 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
2212 save_cpo = p_cpo;
2213 p_cpo = (char_u *)"";
2214
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002215#ifdef FEAT_MULTI_LANG
2216 /* Check for a specified language */
2217 lang = check_help_lang(eap->arg);
2218#endif
2219
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
2221 regmatch.rm_ic = FALSE;
2222 if (regmatch.regprog != NULL)
2223 {
2224 /* create a new quickfix list */
2225 qf_new_list();
2226
2227 /* Go through all directories in 'runtimepath' */
2228 p = p_rtp;
2229 while (*p != NUL && !got_int)
2230 {
2231 copy_option_part(&p, NameBuff, MAXPATHL, ",");
2232
2233 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
2234 add_pathsep(NameBuff);
2235 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
2236 if (gen_expand_wildcards(1, &NameBuff, &fcount,
2237 &fnames, EW_FILE|EW_SILENT) == OK
2238 && fcount > 0)
2239 {
2240 for (fi = 0; fi < fcount && !got_int; ++fi)
2241 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002242#ifdef FEAT_MULTI_LANG
2243 /* Skip files for a different language. */
2244 if (lang != NULL
2245 && STRNICMP(lang, fnames[fi]
2246 + STRLEN(fnames[fi]) - 3, 2) != 0
2247 && !(STRNICMP(lang, "en", 2) == 0
2248 && STRNICMP("txt", fnames[fi]
2249 + STRLEN(fnames[fi]) - 3, 3) == 0))
2250 continue;
2251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 fd = fopen((char *)fnames[fi], "r");
2253 if (fd != NULL)
2254 {
2255 lnum = 1;
2256 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
2257 {
2258 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
2259 {
2260 int l = STRLEN(IObuff);
2261
2262 /* remove trailing CR, LF, spaces, etc. */
2263 while (l > 0 && IObuff[l - 1] <= ' ')
2264 IObuff[--l] = NUL;
2265
2266 if (qf_add_entry(&prevp,
2267 NULL, /* dir */
2268 fnames[fi],
2269 IObuff,
2270 lnum,
2271 0, /* col */
2272 FALSE, /* virt_col */
2273 0, /* nr */
2274 1, /* type */
2275 TRUE /* valid */
2276 ) == FAIL)
2277 {
2278 got_int = TRUE;
2279 break;
2280 }
2281 }
2282 ++lnum;
2283 line_breakcheck();
2284 }
2285 fclose(fd);
2286 }
2287 }
2288 FreeWild(fcount, fnames);
2289 }
2290 }
2291 vim_free(regmatch.regprog);
2292
2293 qf_lists[qf_curlist].qf_nonevalid = FALSE;
2294 qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start;
2295 qf_lists[qf_curlist].qf_index = 1;
2296 }
2297
2298 p_cpo = save_cpo;
2299
2300#ifdef FEAT_WINDOWS
2301 qf_update_buffer();
2302#endif
2303
2304 /* Jump to first match. */
2305 if (qf_lists[qf_curlist].qf_count > 0)
2306 qf_jump(0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002307 else
2308 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309}
2310
2311#endif /* FEAT_QUICKFIX */