Merge branch 'vim'
[MacVim.git] / src / quickfix.c
blob5c3ae381ee45596a684905f65ec2d6128f58d9f0
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
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 */
11 * quickfix.c: functions for quickfix mode, using a file with error messages
14 #include "vim.h"
16 #if defined(FEAT_QUICKFIX) || defined(PROTO)
18 struct dir_stack_T
20 struct dir_stack_T *next;
21 char_u *dirname;
24 static struct dir_stack_T *dir_stack = NULL;
27 * For each error the next struct is allocated and linked in a list.
29 typedef struct qfline_S qfline_T;
30 struct qfline_S
32 qfline_T *qf_next; /* pointer to next error in the list */
33 qfline_T *qf_prev; /* pointer to previous error in the list */
34 linenr_T qf_lnum; /* line number where the error occurred */
35 int qf_fnum; /* file number for the line */
36 int qf_col; /* column where the error occurred */
37 int qf_nr; /* error number */
38 char_u *qf_pattern; /* search pattern for the error */
39 char_u *qf_text; /* description of the error */
40 char_u qf_viscol; /* set to TRUE if qf_col is screen column */
41 char_u qf_cleared; /* set to TRUE if line has been deleted */
42 char_u qf_type; /* type of the error (mostly 'E'); 1 for
43 :helpgrep */
44 char_u qf_valid; /* valid error message detected */
48 * There is a stack of error lists.
50 #define LISTCOUNT 10
52 typedef struct qf_list_S
54 qfline_T *qf_start; /* pointer to the first error */
55 qfline_T *qf_ptr; /* pointer to the current error */
56 int qf_count; /* number of errors (0 means no error list) */
57 int qf_index; /* current index in the error list */
58 int qf_nonevalid; /* TRUE if not a single valid entry found */
59 } qf_list_T;
61 struct qf_info_S
64 * Count of references to this list. Used only for location lists.
65 * When a location list window reference this list, qf_refcount
66 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
67 * reaches 0, the list is freed.
69 int qf_refcount;
70 int qf_listcount; /* current number of lists */
71 int qf_curlist; /* current error list */
72 qf_list_T qf_lists[LISTCOUNT];
75 static qf_info_T ql_info; /* global quickfix list */
77 #define FMT_PATTERNS 10 /* maximum number of % recognized */
80 * Structure used to hold the info of one part of 'errorformat'
82 typedef struct efm_S efm_T;
83 struct efm_S
85 regprog_T *prog; /* pre-formatted part of 'errorformat' */
86 efm_T *next; /* pointer to next (NULL if last) */
87 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
88 char_u prefix; /* prefix of this format line: */
89 /* 'D' enter directory */
90 /* 'X' leave directory */
91 /* 'A' start of multi-line message */
92 /* 'E' error message */
93 /* 'W' warning message */
94 /* 'I' informational message */
95 /* 'C' continuation line */
96 /* 'Z' end of multi-line message */
97 /* 'G' general, unspecific message */
98 /* 'P' push file (partial) message */
99 /* 'Q' pop/quit file (partial) message */
100 /* 'O' overread (partial) message */
101 char_u flags; /* additional flags given in prefix */
102 /* '-' do not include this line */
103 /* '+' include whole line in message */
104 int conthere; /* %> used */
107 static int qf_init_ext __ARGS((qf_info_T *qi, char_u *efile, buf_T *buf, typval_T *tv, char_u *errorformat, int newlist, linenr_T lnumfirst, linenr_T lnumlast));
108 static void qf_new_list __ARGS((qf_info_T *qi));
109 static void ll_free_all __ARGS((qf_info_T **pqi));
110 static int qf_add_entry __ARGS((qf_info_T *qi, qfline_T **prevp, char_u *dir, char_u *fname, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid));
111 static qf_info_T *ll_new_list __ARGS((void));
112 static void qf_msg __ARGS((qf_info_T *qi));
113 static void qf_free __ARGS((qf_info_T *qi, int idx));
114 static char_u *qf_types __ARGS((int, int));
115 static int qf_get_fnum __ARGS((char_u *, char_u *));
116 static char_u *qf_push_dir __ARGS((char_u *, struct dir_stack_T **));
117 static char_u *qf_pop_dir __ARGS((struct dir_stack_T **));
118 static char_u *qf_guess_filepath __ARGS((char_u *));
119 static void qf_fmt_text __ARGS((char_u *text, char_u *buf, int bufsize));
120 static void qf_clean_dir_stack __ARGS((struct dir_stack_T **));
121 #ifdef FEAT_WINDOWS
122 static int qf_win_pos_update __ARGS((qf_info_T *qi, int old_qf_index));
123 static int is_qf_win __ARGS((win_T *win, qf_info_T *qi));
124 static win_T *qf_find_win __ARGS((qf_info_T *qi));
125 static buf_T *qf_find_buf __ARGS((qf_info_T *qi));
126 static void qf_update_buffer __ARGS((qf_info_T *qi));
127 static void qf_fill_buffer __ARGS((qf_info_T *qi));
128 #endif
129 static char_u *get_mef_name __ARGS((void));
130 static buf_T *load_dummy_buffer __ARGS((char_u *fname));
131 static void wipe_dummy_buffer __ARGS((buf_T *buf));
132 static void unload_dummy_buffer __ARGS((buf_T *buf));
133 static qf_info_T *ll_get_or_alloc_list __ARGS((win_T *));
135 /* Quickfix window check helper macro */
136 #define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
137 /* Location list window check helper macro */
138 #define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
140 * Return location list for window 'wp'
141 * For location list window, return the referenced location list
143 #define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
146 * Read the errorfile "efile" into memory, line by line, building the error
147 * list.
148 * Return -1 for error, number of errors for success.
151 qf_init(wp, efile, errorformat, newlist)
152 win_T *wp;
153 char_u *efile;
154 char_u *errorformat;
155 int newlist; /* TRUE: start a new error list */
157 qf_info_T *qi = &ql_info;
159 if (efile == NULL)
160 return FAIL;
162 if (wp != NULL)
164 qi = ll_get_or_alloc_list(wp);
165 if (qi == NULL)
166 return FAIL;
169 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
170 (linenr_T)0, (linenr_T)0);
174 * Read the errorfile "efile" into memory, line by line, building the error
175 * list.
176 * Alternative: when "efile" is null read errors from buffer "buf".
177 * Always use 'errorformat' from "buf" if there is a local value.
178 * Then lnumfirst and lnumlast specify the range of lines to use.
179 * Return -1 for error, number of errors for success.
181 static int
182 qf_init_ext(qi, efile, buf, tv, errorformat, newlist, lnumfirst, lnumlast)
183 qf_info_T *qi;
184 char_u *efile;
185 buf_T *buf;
186 typval_T *tv;
187 char_u *errorformat;
188 int newlist; /* TRUE: start a new error list */
189 linenr_T lnumfirst; /* first line number to use */
190 linenr_T lnumlast; /* last line number to use */
192 char_u *namebuf;
193 char_u *errmsg;
194 char_u *pattern;
195 char_u *fmtstr = NULL;
196 int col = 0;
197 char_u use_viscol = FALSE;
198 int type = 0;
199 int valid;
200 linenr_T buflnum = lnumfirst;
201 long lnum = 0L;
202 int enr = 0;
203 FILE *fd = NULL;
204 qfline_T *qfprev = NULL; /* init to make SASC shut up */
205 char_u *efmp;
206 efm_T *fmt_first = NULL;
207 efm_T *fmt_last = NULL;
208 efm_T *fmt_ptr;
209 efm_T *fmt_start = NULL;
210 char_u *efm;
211 char_u *ptr;
212 char_u *srcptr;
213 int len;
214 int i;
215 int round;
216 int idx = 0;
217 int multiline = FALSE;
218 int multiignore = FALSE;
219 int multiscan = FALSE;
220 int retval = -1; /* default: return error flag */
221 char_u *directory = NULL;
222 char_u *currfile = NULL;
223 char_u *tail = NULL;
224 char_u *p_str = NULL;
225 listitem_T *p_li = NULL;
226 struct dir_stack_T *file_stack = NULL;
227 regmatch_T regmatch;
228 static struct fmtpattern
230 char_u convchar;
231 char *pattern;
232 } fmt_pat[FMT_PATTERNS] =
234 {'f', ".\\+"}, /* only used when at end */
235 {'n', "\\d\\+"},
236 {'l', "\\d\\+"},
237 {'c', "\\d\\+"},
238 {'t', "."},
239 {'m', ".\\+"},
240 {'r', ".*"},
241 {'p', "[- .]*"},
242 {'v', "\\d\\+"},
243 {'s', ".\\+"}
246 namebuf = alloc(CMDBUFFSIZE + 1);
247 errmsg = alloc(CMDBUFFSIZE + 1);
248 pattern = alloc(CMDBUFFSIZE + 1);
249 if (namebuf == NULL || errmsg == NULL || pattern == NULL)
250 goto qf_init_end;
252 if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
254 EMSG2(_(e_openerrf), efile);
255 goto qf_init_end;
258 if (newlist || qi->qf_curlist == qi->qf_listcount)
259 /* make place for a new list */
260 qf_new_list(qi);
261 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
262 /* Adding to existing list, find last entry. */
263 for (qfprev = qi->qf_lists[qi->qf_curlist].qf_start;
264 qfprev->qf_next != qfprev; qfprev = qfprev->qf_next)
268 * Each part of the format string is copied and modified from errorformat to
269 * regex prog. Only a few % characters are allowed.
271 /* Use the local value of 'errorformat' if it's set. */
272 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
273 efm = buf->b_p_efm;
274 else
275 efm = errorformat;
277 * Get some space to modify the format string into.
279 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
280 for (round = FMT_PATTERNS; round > 0; )
281 i += (int)STRLEN(fmt_pat[--round].pattern);
282 #ifdef COLON_IN_FILENAME
283 i += 12; /* "%f" can become twelve chars longer */
284 #else
285 i += 2; /* "%f" can become two chars longer */
286 #endif
287 if ((fmtstr = alloc(i)) == NULL)
288 goto error2;
290 while (efm[0] != NUL)
293 * Allocate a new eformat structure and put it at the end of the list
295 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
296 if (fmt_ptr == NULL)
297 goto error2;
298 if (fmt_first == NULL) /* first one */
299 fmt_first = fmt_ptr;
300 else
301 fmt_last->next = fmt_ptr;
302 fmt_last = fmt_ptr;
305 * Isolate one part in the 'errorformat' option
307 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
308 if (efm[len] == '\\' && efm[len + 1] != NUL)
309 ++len;
312 * Build regexp pattern from current 'errorformat' option
314 ptr = fmtstr;
315 *ptr++ = '^';
316 round = 0;
317 for (efmp = efm; efmp < efm + len; ++efmp)
319 if (*efmp == '%')
321 ++efmp;
322 for (idx = 0; idx < FMT_PATTERNS; ++idx)
323 if (fmt_pat[idx].convchar == *efmp)
324 break;
325 if (idx < FMT_PATTERNS)
327 if (fmt_ptr->addr[idx])
329 sprintf((char *)errmsg,
330 _("E372: Too many %%%c in format string"), *efmp);
331 EMSG(errmsg);
332 goto error2;
334 if ((idx
335 && idx < 6
336 && vim_strchr((char_u *)"DXOPQ",
337 fmt_ptr->prefix) != NULL)
338 || (idx == 6
339 && vim_strchr((char_u *)"OPQ",
340 fmt_ptr->prefix) == NULL))
342 sprintf((char *)errmsg,
343 _("E373: Unexpected %%%c in format string"), *efmp);
344 EMSG(errmsg);
345 goto error2;
347 fmt_ptr->addr[idx] = (char_u)++round;
348 *ptr++ = '\\';
349 *ptr++ = '(';
350 #ifdef BACKSLASH_IN_FILENAME
351 if (*efmp == 'f')
353 /* Also match "c:" in the file name, even when
354 * checking for a colon next: "%f:".
355 * "\%(\a:\)\=" */
356 STRCPY(ptr, "\\%(\\a:\\)\\=");
357 ptr += 10;
359 #endif
360 if (*efmp == 'f' && efmp[1] != NUL)
362 if (efmp[1] != '\\' && efmp[1] != '%')
364 /* A file name may contain spaces, but this isn't
365 * in "\f". For "%f:%l:%m" there may be a ":" in
366 * the file name. Use ".\{-1,}x" instead (x is
367 * the next character), the requirement that :999:
368 * follows should work. */
369 STRCPY(ptr, ".\\{-1,}");
370 ptr += 7;
372 else
374 /* File name followed by '\\' or '%': include as
375 * many file name chars as possible. */
376 STRCPY(ptr, "\\f\\+");
377 ptr += 4;
380 else
382 srcptr = (char_u *)fmt_pat[idx].pattern;
383 while ((*ptr = *srcptr++) != NUL)
384 ++ptr;
386 *ptr++ = '\\';
387 *ptr++ = ')';
389 else if (*efmp == '*')
391 if (*++efmp == '[' || *efmp == '\\')
393 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
395 if (efmp[1] == '^')
396 *ptr++ = *++efmp;
397 if (efmp < efm + len)
399 *ptr++ = *++efmp; /* could be ']' */
400 while (efmp < efm + len
401 && (*ptr++ = *++efmp) != ']')
402 /* skip */;
403 if (efmp == efm + len)
405 EMSG(_("E374: Missing ] in format string"));
406 goto error2;
410 else if (efmp < efm + len) /* %*\D, %*\s etc. */
411 *ptr++ = *++efmp;
412 *ptr++ = '\\';
413 *ptr++ = '+';
415 else
417 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
418 sprintf((char *)errmsg,
419 _("E375: Unsupported %%%c in format string"), *efmp);
420 EMSG(errmsg);
421 goto error2;
424 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
425 *ptr++ = *efmp; /* regexp magic characters */
426 else if (*efmp == '#')
427 *ptr++ = '*';
428 else if (*efmp == '>')
429 fmt_ptr->conthere = TRUE;
430 else if (efmp == efm + 1) /* analyse prefix */
432 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
433 fmt_ptr->flags = *efmp++;
434 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
435 fmt_ptr->prefix = *efmp;
436 else
438 sprintf((char *)errmsg,
439 _("E376: Invalid %%%c in format string prefix"), *efmp);
440 EMSG(errmsg);
441 goto error2;
444 else
446 sprintf((char *)errmsg,
447 _("E377: Invalid %%%c in format string"), *efmp);
448 EMSG(errmsg);
449 goto error2;
452 else /* copy normal character */
454 if (*efmp == '\\' && efmp + 1 < efm + len)
455 ++efmp;
456 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
457 *ptr++ = '\\'; /* escape regexp atoms */
458 if (*efmp)
459 *ptr++ = *efmp;
462 *ptr++ = '$';
463 *ptr = NUL;
464 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
465 goto error2;
467 * Advance to next part
469 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
471 if (fmt_first == NULL) /* nothing found */
473 EMSG(_("E378: 'errorformat' contains no pattern"));
474 goto error2;
478 * got_int is reset here, because it was probably set when killing the
479 * ":make" command, but we still want to read the errorfile then.
481 got_int = FALSE;
483 /* Always ignore case when looking for a matching error. */
484 regmatch.rm_ic = TRUE;
486 if (tv != NULL)
488 if (tv->v_type == VAR_STRING)
489 p_str = tv->vval.v_string;
490 else if (tv->v_type == VAR_LIST)
491 p_li = tv->vval.v_list->lv_first;
495 * Read the lines in the error file one by one.
496 * Try to recognize one of the error formats in each line.
498 while (!got_int)
500 /* Get the next line. */
501 if (fd == NULL)
503 if (tv != NULL)
505 if (tv->v_type == VAR_STRING)
507 /* Get the next line from the supplied string */
508 char_u *p;
510 if (!*p_str) /* Reached the end of the string */
511 break;
513 p = vim_strchr(p_str, '\n');
514 if (p)
515 len = (int)(p - p_str + 1);
516 else
517 len = (int)STRLEN(p_str);
519 if (len > CMDBUFFSIZE - 2)
520 vim_strncpy(IObuff, p_str, CMDBUFFSIZE - 2);
521 else
522 vim_strncpy(IObuff, p_str, len);
524 p_str += len;
526 else if (tv->v_type == VAR_LIST)
528 /* Get the next line from the supplied list */
529 while (p_li && p_li->li_tv.v_type != VAR_STRING)
530 p_li = p_li->li_next; /* Skip non-string items */
532 if (!p_li) /* End of the list */
533 break;
535 len = (int)STRLEN(p_li->li_tv.vval.v_string);
536 if (len > CMDBUFFSIZE - 2)
537 len = CMDBUFFSIZE - 2;
539 vim_strncpy(IObuff, p_li->li_tv.vval.v_string, len);
541 p_li = p_li->li_next; /* next item */
544 else
546 /* Get the next line from the supplied buffer */
547 if (buflnum > lnumlast)
548 break;
549 vim_strncpy(IObuff, ml_get_buf(buf, buflnum++, FALSE),
550 CMDBUFFSIZE - 2);
553 else if (fgets((char *)IObuff, CMDBUFFSIZE - 2, fd) == NULL)
554 break;
556 IObuff[CMDBUFFSIZE - 2] = NUL; /* for very long lines */
557 if ((efmp = vim_strrchr(IObuff, '\n')) != NULL)
558 *efmp = NUL;
559 #ifdef USE_CRNL
560 if ((efmp = vim_strrchr(IObuff, '\r')) != NULL)
561 *efmp = NUL;
562 #endif
564 /* If there was no %> item start at the first pattern */
565 if (fmt_start == NULL)
566 fmt_ptr = fmt_first;
567 else
569 fmt_ptr = fmt_start;
570 fmt_start = NULL;
574 * Try to match each part of 'errorformat' until we find a complete
575 * match or no match.
577 valid = TRUE;
578 restofline:
579 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
581 idx = fmt_ptr->prefix;
582 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
583 continue;
584 namebuf[0] = NUL;
585 pattern[0] = NUL;
586 if (!multiscan)
587 errmsg[0] = NUL;
588 lnum = 0;
589 col = 0;
590 use_viscol = FALSE;
591 enr = -1;
592 type = 0;
593 tail = NULL;
595 regmatch.regprog = fmt_ptr->prog;
596 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
598 if ((idx == 'C' || idx == 'Z') && !multiline)
599 continue;
600 if (vim_strchr((char_u *)"EWI", idx) != NULL)
601 type = idx;
602 else
603 type = 0;
605 * Extract error message data from matched line.
606 * We check for an actual submatch, because "\[" and "\]" in
607 * the 'errorformat' may cause the wrong submatch to be used.
609 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
611 int c;
613 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
614 continue;
616 /* Expand ~/file and $HOME/file to full path. */
617 c = *regmatch.endp[i];
618 *regmatch.endp[i] = NUL;
619 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
620 *regmatch.endp[i] = c;
622 if (vim_strchr((char_u *)"OPQ", idx) != NULL
623 && mch_getperm(namebuf) == -1)
624 continue;
626 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
628 if (regmatch.startp[i] == NULL)
629 continue;
630 enr = (int)atol((char *)regmatch.startp[i]);
632 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
634 if (regmatch.startp[i] == NULL)
635 continue;
636 lnum = atol((char *)regmatch.startp[i]);
638 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
640 if (regmatch.startp[i] == NULL)
641 continue;
642 col = (int)atol((char *)regmatch.startp[i]);
644 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
646 if (regmatch.startp[i] == NULL)
647 continue;
648 type = *regmatch.startp[i];
650 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
651 STRCPY(errmsg, IObuff);
652 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
654 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
655 continue;
656 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
657 vim_strncpy(errmsg, regmatch.startp[i], len);
659 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
661 if (regmatch.startp[i] == NULL)
662 continue;
663 tail = regmatch.startp[i];
665 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
667 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
668 continue;
669 col = (int)(regmatch.endp[i] - regmatch.startp[i] + 1);
670 if (*((char_u *)regmatch.startp[i]) != TAB)
671 use_viscol = TRUE;
673 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
675 if (regmatch.startp[i] == NULL)
676 continue;
677 col = (int)atol((char *)regmatch.startp[i]);
678 use_viscol = TRUE;
680 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
682 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
683 continue;
684 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
685 if (len > CMDBUFFSIZE - 5)
686 len = CMDBUFFSIZE - 5;
687 STRCPY(pattern, "^\\V");
688 STRNCAT(pattern, regmatch.startp[i], len);
689 pattern[len + 3] = '\\';
690 pattern[len + 4] = '$';
691 pattern[len + 5] = NUL;
693 break;
696 multiscan = FALSE;
698 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
700 if (fmt_ptr != NULL)
702 if (idx == 'D') /* enter directory */
704 if (*namebuf == NUL)
706 EMSG(_("E379: Missing or empty directory name"));
707 goto error2;
709 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
710 goto error2;
712 else if (idx == 'X') /* leave directory */
713 directory = qf_pop_dir(&dir_stack);
715 namebuf[0] = NUL; /* no match found, remove file name */
716 lnum = 0; /* don't jump to this line */
717 valid = FALSE;
718 STRCPY(errmsg, IObuff); /* copy whole line to error message */
719 if (fmt_ptr == NULL)
720 multiline = multiignore = FALSE;
722 else if (fmt_ptr != NULL)
724 /* honor %> item */
725 if (fmt_ptr->conthere)
726 fmt_start = fmt_ptr;
728 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
729 multiline = TRUE; /* start of a multi-line message */
730 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
731 { /* continuation of multi-line msg */
732 if (qfprev == NULL)
733 goto error2;
734 if (*errmsg && !multiignore)
736 len = (int)STRLEN(qfprev->qf_text);
737 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
738 == NULL)
739 goto error2;
740 STRCPY(ptr, qfprev->qf_text);
741 vim_free(qfprev->qf_text);
742 qfprev->qf_text = ptr;
743 *(ptr += len) = '\n';
744 STRCPY(++ptr, errmsg);
746 if (qfprev->qf_nr == -1)
747 qfprev->qf_nr = enr;
748 if (vim_isprintc(type) && !qfprev->qf_type)
749 qfprev->qf_type = type; /* only printable chars allowed */
750 if (!qfprev->qf_lnum)
751 qfprev->qf_lnum = lnum;
752 if (!qfprev->qf_col)
753 qfprev->qf_col = col;
754 qfprev->qf_viscol = use_viscol;
755 if (!qfprev->qf_fnum)
756 qfprev->qf_fnum = qf_get_fnum(directory,
757 *namebuf || directory ? namebuf
758 : currfile && valid ? currfile : 0);
759 if (idx == 'Z')
760 multiline = multiignore = FALSE;
761 line_breakcheck();
762 continue;
764 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
766 /* global file names */
767 valid = FALSE;
768 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
770 if (*namebuf && idx == 'P')
771 currfile = qf_push_dir(namebuf, &file_stack);
772 else if (idx == 'Q')
773 currfile = qf_pop_dir(&file_stack);
774 *namebuf = NUL;
775 if (tail && *tail)
777 STRMOVE(IObuff, skipwhite(tail));
778 multiscan = TRUE;
779 goto restofline;
783 if (fmt_ptr->flags == '-') /* generally exclude this line */
785 if (multiline)
786 multiignore = TRUE; /* also exclude continuation lines */
787 continue;
791 if (qf_add_entry(qi, &qfprev,
792 directory,
793 (*namebuf || directory)
794 ? namebuf
795 : ((currfile && valid) ? currfile : (char_u *)NULL),
797 errmsg,
798 lnum,
799 col,
800 use_viscol,
801 pattern,
802 enr,
803 type,
804 valid) == FAIL)
805 goto error2;
806 line_breakcheck();
808 if (fd == NULL || !ferror(fd))
810 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
812 /* no valid entry found */
813 qi->qf_lists[qi->qf_curlist].qf_ptr =
814 qi->qf_lists[qi->qf_curlist].qf_start;
815 qi->qf_lists[qi->qf_curlist].qf_index = 1;
816 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
818 else
820 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
821 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
822 qi->qf_lists[qi->qf_curlist].qf_ptr =
823 qi->qf_lists[qi->qf_curlist].qf_start;
825 /* return number of matches */
826 retval = qi->qf_lists[qi->qf_curlist].qf_count;
827 goto qf_init_ok;
829 EMSG(_(e_readerrf));
830 error2:
831 qf_free(qi, qi->qf_curlist);
832 qi->qf_listcount--;
833 if (qi->qf_curlist > 0)
834 --qi->qf_curlist;
835 qf_init_ok:
836 if (fd != NULL)
837 fclose(fd);
838 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
840 fmt_first = fmt_ptr->next;
841 vim_free(fmt_ptr->prog);
842 vim_free(fmt_ptr);
844 qf_clean_dir_stack(&dir_stack);
845 qf_clean_dir_stack(&file_stack);
846 qf_init_end:
847 vim_free(namebuf);
848 vim_free(errmsg);
849 vim_free(pattern);
850 vim_free(fmtstr);
852 #ifdef FEAT_WINDOWS
853 qf_update_buffer(qi);
854 #endif
856 return retval;
860 * Prepare for adding a new quickfix list.
862 static void
863 qf_new_list(qi)
864 qf_info_T *qi;
866 int i;
869 * If the current entry is not the last entry, delete entries below
870 * the current entry. This makes it possible to browse in a tree-like
871 * way with ":grep'.
873 while (qi->qf_listcount > qi->qf_curlist + 1)
874 qf_free(qi, --qi->qf_listcount);
877 * When the stack is full, remove to oldest entry
878 * Otherwise, add a new entry.
880 if (qi->qf_listcount == LISTCOUNT)
882 qf_free(qi, 0);
883 for (i = 1; i < LISTCOUNT; ++i)
884 qi->qf_lists[i - 1] = qi->qf_lists[i];
885 qi->qf_curlist = LISTCOUNT - 1;
887 else
888 qi->qf_curlist = qi->qf_listcount++;
889 qi->qf_lists[qi->qf_curlist].qf_index = 0;
890 qi->qf_lists[qi->qf_curlist].qf_count = 0;
894 * Free a location list
896 static void
897 ll_free_all(pqi)
898 qf_info_T **pqi;
900 int i;
901 qf_info_T *qi;
903 qi = *pqi;
904 if (qi == NULL)
905 return;
906 *pqi = NULL; /* Remove reference to this list */
908 qi->qf_refcount--;
909 if (qi->qf_refcount < 1)
911 /* No references to this location list */
912 for (i = 0; i < qi->qf_listcount; ++i)
913 qf_free(qi, i);
914 vim_free(qi);
918 void
919 qf_free_all(wp)
920 win_T *wp;
922 int i;
923 qf_info_T *qi = &ql_info;
925 if (wp != NULL)
927 /* location list */
928 ll_free_all(&wp->w_llist);
929 ll_free_all(&wp->w_llist_ref);
931 else
932 /* quickfix list */
933 for (i = 0; i < qi->qf_listcount; ++i)
934 qf_free(qi, i);
938 * Add an entry to the end of the list of errors.
939 * Returns OK or FAIL.
941 static int
942 qf_add_entry(qi, prevp, dir, fname, bufnum, mesg, lnum, col, vis_col, pattern,
943 nr, type, valid)
944 qf_info_T *qi; /* quickfix list */
945 qfline_T **prevp; /* pointer to previously added entry or NULL */
946 char_u *dir; /* optional directory name */
947 char_u *fname; /* file name or NULL */
948 int bufnum; /* buffer number or zero */
949 char_u *mesg; /* message */
950 long lnum; /* line number */
951 int col; /* column */
952 int vis_col; /* using visual column */
953 char_u *pattern; /* search pattern */
954 int nr; /* error number */
955 int type; /* type character */
956 int valid; /* valid entry */
958 qfline_T *qfp;
960 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
961 return FAIL;
962 if (bufnum != 0)
963 qfp->qf_fnum = bufnum;
964 else
965 qfp->qf_fnum = qf_get_fnum(dir, fname);
966 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
968 vim_free(qfp);
969 return FAIL;
971 qfp->qf_lnum = lnum;
972 qfp->qf_col = col;
973 qfp->qf_viscol = vis_col;
974 if (pattern == NULL || *pattern == NUL)
975 qfp->qf_pattern = NULL;
976 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
978 vim_free(qfp->qf_text);
979 vim_free(qfp);
980 return FAIL;
982 qfp->qf_nr = nr;
983 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
984 type = 0;
985 qfp->qf_type = type;
986 qfp->qf_valid = valid;
988 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
989 /* first element in the list */
991 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
992 qfp->qf_prev = qfp; /* first element points to itself */
994 else
996 qfp->qf_prev = *prevp;
997 (*prevp)->qf_next = qfp;
999 qfp->qf_next = qfp; /* last element points to itself */
1000 qfp->qf_cleared = FALSE;
1001 *prevp = qfp;
1002 ++qi->qf_lists[qi->qf_curlist].qf_count;
1003 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1004 /* first valid entry */
1006 qi->qf_lists[qi->qf_curlist].qf_index =
1007 qi->qf_lists[qi->qf_curlist].qf_count;
1008 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
1011 return OK;
1015 * Allocate a new location list
1017 static qf_info_T *
1018 ll_new_list()
1020 qf_info_T *qi;
1022 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1023 if (qi != NULL)
1025 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1026 qi->qf_refcount++;
1029 return qi;
1033 * Return the location list for window 'wp'.
1034 * If not present, allocate a location list
1036 static qf_info_T *
1037 ll_get_or_alloc_list(wp)
1038 win_T *wp;
1040 if (IS_LL_WINDOW(wp))
1041 /* For a location list window, use the referenced location list */
1042 return wp->w_llist_ref;
1045 * For a non-location list window, w_llist_ref should not point to a
1046 * location list.
1048 ll_free_all(&wp->w_llist_ref);
1050 if (wp->w_llist == NULL)
1051 wp->w_llist = ll_new_list(); /* new location list */
1052 return wp->w_llist;
1056 * Copy the location list from window "from" to window "to".
1058 void
1059 copy_loclist(from, to)
1060 win_T *from;
1061 win_T *to;
1063 qf_info_T *qi;
1064 int idx;
1065 int i;
1068 * When copying from a location list window, copy the referenced
1069 * location list. For other windows, copy the location list for
1070 * that window.
1072 if (IS_LL_WINDOW(from))
1073 qi = from->w_llist_ref;
1074 else
1075 qi = from->w_llist;
1077 if (qi == NULL) /* no location list to copy */
1078 return;
1080 /* allocate a new location list */
1081 if ((to->w_llist = ll_new_list()) == NULL)
1082 return;
1084 to->w_llist->qf_listcount = qi->qf_listcount;
1086 /* Copy the location lists one at a time */
1087 for (idx = 0; idx < qi->qf_listcount; idx++)
1089 qf_list_T *from_qfl;
1090 qf_list_T *to_qfl;
1092 to->w_llist->qf_curlist = idx;
1094 from_qfl = &qi->qf_lists[idx];
1095 to_qfl = &to->w_llist->qf_lists[idx];
1097 /* Some of the fields are populated by qf_add_entry() */
1098 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1099 to_qfl->qf_count = 0;
1100 to_qfl->qf_index = 0;
1101 to_qfl->qf_start = NULL;
1102 to_qfl->qf_ptr = NULL;
1104 if (from_qfl->qf_count)
1106 qfline_T *from_qfp;
1107 qfline_T *prevp = NULL;
1109 /* copy all the location entries in this list */
1110 for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
1111 ++i, from_qfp = from_qfp->qf_next)
1113 if (qf_add_entry(to->w_llist, &prevp,
1114 NULL,
1115 NULL,
1117 from_qfp->qf_text,
1118 from_qfp->qf_lnum,
1119 from_qfp->qf_col,
1120 from_qfp->qf_viscol,
1121 from_qfp->qf_pattern,
1122 from_qfp->qf_nr,
1124 from_qfp->qf_valid) == FAIL)
1126 qf_free_all(to);
1127 return;
1130 * qf_add_entry() will not set the qf_num field, as the
1131 * directory and file names are not supplied. So the qf_fnum
1132 * field is copied here.
1134 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1135 prevp->qf_type = from_qfp->qf_type; /* error type */
1136 if (from_qfl->qf_ptr == from_qfp)
1137 to_qfl->qf_ptr = prevp; /* current location */
1141 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1143 /* When no valid entries are present in the list, qf_ptr points to
1144 * the first item in the list */
1145 if (to_qfl->qf_nonevalid == TRUE)
1146 to_qfl->qf_ptr = to_qfl->qf_start;
1149 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1153 * get buffer number for file "dir.name"
1155 static int
1156 qf_get_fnum(directory, fname)
1157 char_u *directory;
1158 char_u *fname;
1160 if (fname == NULL || *fname == NUL) /* no file name */
1161 return 0;
1163 #ifdef RISCOS
1164 /* Name is reported as `main.c', but file is `c.main' */
1165 return ro_buflist_add(fname);
1166 #else
1167 char_u *ptr;
1168 int fnum;
1170 # ifdef VMS
1171 vms_remove_version(fname);
1172 # endif
1173 # ifdef BACKSLASH_IN_FILENAME
1174 if (directory != NULL)
1175 slash_adjust(directory);
1176 slash_adjust(fname);
1177 # endif
1178 if (directory != NULL && !vim_isAbsName(fname)
1179 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1182 * Here we check if the file really exists.
1183 * This should normally be true, but if make works without
1184 * "leaving directory"-messages we might have missed a
1185 * directory change.
1187 if (mch_getperm(ptr) < 0)
1189 vim_free(ptr);
1190 directory = qf_guess_filepath(fname);
1191 if (directory)
1192 ptr = concat_fnames(directory, fname, TRUE);
1193 else
1194 ptr = vim_strsave(fname);
1196 /* Use concatenated directory name and file name */
1197 fnum = buflist_add(ptr, 0);
1198 vim_free(ptr);
1199 return fnum;
1201 return buflist_add(fname, 0);
1202 #endif
1207 * push dirbuf onto the directory stack and return pointer to actual dir or
1208 * NULL on error
1210 static char_u *
1211 qf_push_dir(dirbuf, stackptr)
1212 char_u *dirbuf;
1213 struct dir_stack_T **stackptr;
1215 struct dir_stack_T *ds_new;
1216 struct dir_stack_T *ds_ptr;
1218 /* allocate new stack element and hook it in */
1219 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1220 if (ds_new == NULL)
1221 return NULL;
1223 ds_new->next = *stackptr;
1224 *stackptr = ds_new;
1226 /* store directory on the stack */
1227 if (vim_isAbsName(dirbuf)
1228 || (*stackptr)->next == NULL
1229 || (*stackptr && dir_stack != *stackptr))
1230 (*stackptr)->dirname = vim_strsave(dirbuf);
1231 else
1233 /* Okay we don't have an absolute path.
1234 * dirbuf must be a subdir of one of the directories on the stack.
1235 * Let's search...
1237 ds_new = (*stackptr)->next;
1238 (*stackptr)->dirname = NULL;
1239 while (ds_new)
1241 vim_free((*stackptr)->dirname);
1242 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1243 TRUE);
1244 if (mch_isdir((*stackptr)->dirname) == TRUE)
1245 break;
1247 ds_new = ds_new->next;
1250 /* clean up all dirs we already left */
1251 while ((*stackptr)->next != ds_new)
1253 ds_ptr = (*stackptr)->next;
1254 (*stackptr)->next = (*stackptr)->next->next;
1255 vim_free(ds_ptr->dirname);
1256 vim_free(ds_ptr);
1259 /* Nothing found -> it must be on top level */
1260 if (ds_new == NULL)
1262 vim_free((*stackptr)->dirname);
1263 (*stackptr)->dirname = vim_strsave(dirbuf);
1267 if ((*stackptr)->dirname != NULL)
1268 return (*stackptr)->dirname;
1269 else
1271 ds_ptr = *stackptr;
1272 *stackptr = (*stackptr)->next;
1273 vim_free(ds_ptr);
1274 return NULL;
1280 * pop dirbuf from the directory stack and return previous directory or NULL if
1281 * stack is empty
1283 static char_u *
1284 qf_pop_dir(stackptr)
1285 struct dir_stack_T **stackptr;
1287 struct dir_stack_T *ds_ptr;
1289 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1290 * What to do if it isn't? */
1292 /* pop top element and free it */
1293 if (*stackptr != NULL)
1295 ds_ptr = *stackptr;
1296 *stackptr = (*stackptr)->next;
1297 vim_free(ds_ptr->dirname);
1298 vim_free(ds_ptr);
1301 /* return NEW top element as current dir or NULL if stack is empty*/
1302 return *stackptr ? (*stackptr)->dirname : NULL;
1306 * clean up directory stack
1308 static void
1309 qf_clean_dir_stack(stackptr)
1310 struct dir_stack_T **stackptr;
1312 struct dir_stack_T *ds_ptr;
1314 while ((ds_ptr = *stackptr) != NULL)
1316 *stackptr = (*stackptr)->next;
1317 vim_free(ds_ptr->dirname);
1318 vim_free(ds_ptr);
1323 * Check in which directory of the directory stack the given file can be
1324 * found.
1325 * Returns a pointer to the directory name or NULL if not found
1326 * Cleans up intermediate directory entries.
1328 * TODO: How to solve the following problem?
1329 * If we have the this directory tree:
1330 * ./
1331 * ./aa
1332 * ./aa/bb
1333 * ./bb
1334 * ./bb/x.c
1335 * and make says:
1336 * making all in aa
1337 * making all in bb
1338 * x.c:9: Error
1339 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1340 * qf_guess_filepath will return NULL.
1342 static char_u *
1343 qf_guess_filepath(filename)
1344 char_u *filename;
1346 struct dir_stack_T *ds_ptr;
1347 struct dir_stack_T *ds_tmp;
1348 char_u *fullname;
1350 /* no dirs on the stack - there's nothing we can do */
1351 if (dir_stack == NULL)
1352 return NULL;
1354 ds_ptr = dir_stack->next;
1355 fullname = NULL;
1356 while (ds_ptr)
1358 vim_free(fullname);
1359 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1361 /* If concat_fnames failed, just go on. The worst thing that can happen
1362 * is that we delete the entire stack.
1364 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1365 break;
1367 ds_ptr = ds_ptr->next;
1370 vim_free(fullname);
1372 /* clean up all dirs we already left */
1373 while (dir_stack->next != ds_ptr)
1375 ds_tmp = dir_stack->next;
1376 dir_stack->next = dir_stack->next->next;
1377 vim_free(ds_tmp->dirname);
1378 vim_free(ds_tmp);
1381 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1386 * jump to a quickfix line
1387 * if dir == FORWARD go "errornr" valid entries forward
1388 * if dir == BACKWARD go "errornr" valid entries backward
1389 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1390 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1391 * else if "errornr" is zero, redisplay the same line
1392 * else go to entry "errornr"
1394 void
1395 qf_jump(qi, dir, errornr, forceit)
1396 qf_info_T *qi;
1397 int dir;
1398 int errornr;
1399 int forceit;
1401 qf_info_T *ll_ref;
1402 qfline_T *qf_ptr;
1403 qfline_T *old_qf_ptr;
1404 int qf_index;
1405 int old_qf_fnum;
1406 int old_qf_index;
1407 int prev_index;
1408 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1409 char_u *err = e_no_more_items;
1410 linenr_T i;
1411 buf_T *old_curbuf;
1412 linenr_T old_lnum;
1413 colnr_T screen_col;
1414 colnr_T char_col;
1415 char_u *line;
1416 #ifdef FEAT_WINDOWS
1417 char_u *old_swb = p_swb;
1418 unsigned old_swb_flags = swb_flags;
1419 int opened_window = FALSE;
1420 win_T *win;
1421 win_T *altwin;
1422 #endif
1423 win_T *oldwin = curwin;
1424 int print_message = TRUE;
1425 int len;
1426 #ifdef FEAT_FOLDING
1427 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1428 #endif
1429 int ok = OK;
1430 int usable_win;
1432 if (qi == NULL)
1433 qi = &ql_info;
1435 if (qi->qf_curlist >= qi->qf_listcount
1436 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
1438 EMSG(_(e_quickfix));
1439 return;
1442 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
1443 old_qf_ptr = qf_ptr;
1444 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
1445 old_qf_index = qf_index;
1446 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1448 while (errornr--)
1450 old_qf_ptr = qf_ptr;
1451 prev_index = qf_index;
1452 old_qf_fnum = qf_ptr->qf_fnum;
1455 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
1456 || qf_ptr->qf_next == NULL)
1458 qf_ptr = old_qf_ptr;
1459 qf_index = prev_index;
1460 if (err != NULL)
1462 EMSG(_(err));
1463 goto theend;
1465 errornr = 0;
1466 break;
1468 ++qf_index;
1469 qf_ptr = qf_ptr->qf_next;
1470 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1471 && !qf_ptr->qf_valid)
1472 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1473 err = NULL;
1476 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1478 while (errornr--)
1480 old_qf_ptr = qf_ptr;
1481 prev_index = qf_index;
1482 old_qf_fnum = qf_ptr->qf_fnum;
1485 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1487 qf_ptr = old_qf_ptr;
1488 qf_index = prev_index;
1489 if (err != NULL)
1491 EMSG(_(err));
1492 goto theend;
1494 errornr = 0;
1495 break;
1497 --qf_index;
1498 qf_ptr = qf_ptr->qf_prev;
1499 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1500 && !qf_ptr->qf_valid)
1501 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1502 err = NULL;
1505 else if (errornr != 0) /* go to specified number */
1507 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1509 --qf_index;
1510 qf_ptr = qf_ptr->qf_prev;
1512 while (errornr > qf_index && qf_index <
1513 qi->qf_lists[qi->qf_curlist].qf_count
1514 && qf_ptr->qf_next != NULL)
1516 ++qf_index;
1517 qf_ptr = qf_ptr->qf_next;
1521 #ifdef FEAT_WINDOWS
1522 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1523 if (qf_win_pos_update(qi, old_qf_index))
1524 /* No need to print the error message if it's visible in the error
1525 * window */
1526 print_message = FALSE;
1529 * For ":helpgrep" find a help window or open one.
1531 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
1533 win_T *wp;
1534 int n;
1536 if (cmdmod.tab != 0)
1537 wp = NULL;
1538 else
1539 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1540 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1541 break;
1542 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1543 win_enter(wp, TRUE);
1544 else
1547 * Split off help window; put it at far top if no position
1548 * specified, the current window is vertically split and narrow.
1550 n = WSP_HELP;
1551 # ifdef FEAT_VERTSPLIT
1552 if (cmdmod.split == 0 && curwin->w_width != Columns
1553 && curwin->w_width < 80)
1554 n |= WSP_TOP;
1555 # endif
1556 if (win_split(0, n) == FAIL)
1557 goto theend;
1558 opened_window = TRUE; /* close it when fail */
1560 if (curwin->w_height < p_hh)
1561 win_setheight((int)p_hh);
1563 if (qi != &ql_info) /* not a quickfix list */
1565 /* The new window should use the supplied location list */
1566 qf_free_all(curwin);
1567 curwin->w_llist = qi;
1568 qi->qf_refcount++;
1572 if (!p_im)
1573 restart_edit = 0; /* don't want insert mode in help file */
1577 * If currently in the quickfix window, find another window to show the
1578 * file in.
1580 if (bt_quickfix(curbuf) && !opened_window)
1583 * If there is no file specified, we don't know where to go.
1584 * But do advance, otherwise ":cn" gets stuck.
1586 if (qf_ptr->qf_fnum == 0)
1587 goto theend;
1589 /* Locate a window showing a normal buffer */
1590 usable_win = 0;
1591 FOR_ALL_WINDOWS(win)
1592 if (win->w_buffer->b_p_bt[0] == NUL)
1594 usable_win = 1;
1595 break;
1599 * If no usable window is found and 'switchbuf' contains "usetab"
1600 * then search in other tabs.
1602 if (!usable_win && (swb_flags & SWB_USETAB))
1604 tabpage_T *tp;
1605 win_T *wp;
1607 FOR_ALL_TAB_WINDOWS(tp, wp)
1609 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
1611 goto_tabpage_win(tp, wp);
1612 usable_win = 1;
1613 break;
1619 * If there is only one window and it is the quickfix window, create a
1620 * new one above the quickfix window.
1622 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
1624 ll_ref = curwin->w_llist_ref;
1626 if (win_split(0, WSP_ABOVE) == FAIL)
1627 goto failed; /* not enough room for window */
1628 opened_window = TRUE; /* close it when fail */
1629 p_swb = empty_option; /* don't split again */
1630 swb_flags = 0;
1631 # ifdef FEAT_SCROLLBIND
1632 curwin->w_p_scb = FALSE;
1633 # endif
1634 if (ll_ref != NULL)
1636 /* The new window should use the location list from the
1637 * location list window */
1638 qf_free_all(curwin);
1639 curwin->w_llist = ll_ref;
1640 ll_ref->qf_refcount++;
1643 else
1645 if (curwin->w_llist_ref != NULL)
1647 /* In a location window */
1648 ll_ref = curwin->w_llist_ref;
1650 /* Find the window with the same location list */
1651 FOR_ALL_WINDOWS(win)
1652 if (win->w_llist == ll_ref)
1653 break;
1654 if (win == NULL)
1656 /* Find the window showing the selected file */
1657 FOR_ALL_WINDOWS(win)
1658 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1659 break;
1660 if (win == NULL)
1662 /* Find a previous usable window */
1663 win = curwin;
1666 if (win->w_buffer->b_p_bt[0] == NUL)
1667 break;
1668 if (win->w_prev == NULL)
1669 win = lastwin; /* wrap around the top */
1670 else
1671 win = win->w_prev; /* go to previous window */
1672 } while (win != curwin);
1675 win_goto(win);
1677 /* If the location list for the window is not set, then set it
1678 * to the location list from the location window */
1679 if (win->w_llist == NULL)
1681 win->w_llist = ll_ref;
1682 ll_ref->qf_refcount++;
1685 else
1689 * Try to find a window that shows the right buffer.
1690 * Default to the window just above the quickfix buffer.
1692 win = curwin;
1693 altwin = NULL;
1694 for (;;)
1696 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1697 break;
1698 if (win->w_prev == NULL)
1699 win = lastwin; /* wrap around the top */
1700 else
1701 win = win->w_prev; /* go to previous window */
1703 if (IS_QF_WINDOW(win))
1705 /* Didn't find it, go to the window before the quickfix
1706 * window. */
1707 if (altwin != NULL)
1708 win = altwin;
1709 else if (curwin->w_prev != NULL)
1710 win = curwin->w_prev;
1711 else
1712 win = curwin->w_next;
1713 break;
1716 /* Remember a usable window. */
1717 if (altwin == NULL && !win->w_p_pvw
1718 && win->w_buffer->b_p_bt[0] == NUL)
1719 altwin = win;
1722 win_goto(win);
1726 #endif
1729 * If there is a file name,
1730 * read the wanted file if needed, and check autowrite etc.
1732 old_curbuf = curbuf;
1733 old_lnum = curwin->w_cursor.lnum;
1735 if (qf_ptr->qf_fnum != 0)
1737 if (qf_ptr->qf_type == 1)
1739 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1740 * set b_p_ro flag). */
1741 if (!can_abandon(curbuf, forceit))
1743 EMSG(_(e_nowrtmsg));
1744 ok = FALSE;
1746 else
1747 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
1748 ECMD_HIDE + ECMD_SET_HELP,
1749 oldwin == curwin ? curwin : NULL);
1751 else
1752 ok = buflist_getfile(qf_ptr->qf_fnum,
1753 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
1756 if (ok == OK)
1758 /* When not switched to another buffer, still need to set pc mark */
1759 if (curbuf == old_curbuf)
1760 setpcmark();
1762 if (qf_ptr->qf_pattern == NULL)
1765 * Go to line with error, unless qf_lnum is 0.
1767 i = qf_ptr->qf_lnum;
1768 if (i > 0)
1770 if (i > curbuf->b_ml.ml_line_count)
1771 i = curbuf->b_ml.ml_line_count;
1772 curwin->w_cursor.lnum = i;
1774 if (qf_ptr->qf_col > 0)
1776 curwin->w_cursor.col = qf_ptr->qf_col - 1;
1777 if (qf_ptr->qf_viscol == TRUE)
1780 * Check each character from the beginning of the error
1781 * line up to the error column. For each tab character
1782 * found, reduce the error column value by the length of
1783 * a tab character.
1785 line = ml_get_curline();
1786 screen_col = 0;
1787 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1789 if (*line == NUL)
1790 break;
1791 if (*line++ == '\t')
1793 curwin->w_cursor.col -= 7 - (screen_col % 8);
1794 screen_col += 8 - (screen_col % 8);
1796 else
1797 ++screen_col;
1800 check_cursor();
1802 else
1803 beginline(BL_WHITE | BL_FIX);
1805 else
1807 pos_T save_cursor;
1809 /* Move the cursor to the first line in the buffer */
1810 save_cursor = curwin->w_cursor;
1811 curwin->w_cursor.lnum = 0;
1812 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
1813 SEARCH_KEEP, NULL))
1814 curwin->w_cursor = save_cursor;
1817 #ifdef FEAT_FOLDING
1818 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1819 foldOpenCursor();
1820 #endif
1821 if (print_message)
1823 /* Update the screen before showing the message */
1824 update_topline_redraw();
1825 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
1826 qi->qf_lists[qi->qf_curlist].qf_count,
1827 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1828 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1829 /* Add the message, skipping leading whitespace and newlines. */
1830 len = (int)STRLEN(IObuff);
1831 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1833 /* Output the message. Overwrite to avoid scrolling when the 'O'
1834 * flag is present in 'shortmess'; But when not jumping, print the
1835 * whole message. */
1836 i = msg_scroll;
1837 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1838 msg_scroll = TRUE;
1839 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1840 msg_scroll = FALSE;
1841 msg_attr_keep(IObuff, 0, TRUE);
1842 msg_scroll = i;
1845 else
1847 #ifdef FEAT_WINDOWS
1848 if (opened_window)
1849 win_close(curwin, TRUE); /* Close opened window */
1850 #endif
1851 if (qf_ptr->qf_fnum != 0)
1854 * Couldn't open file, so put index back where it was. This could
1855 * happen if the file was readonly and we changed something.
1857 #ifdef FEAT_WINDOWS
1858 failed:
1859 #endif
1860 qf_ptr = old_qf_ptr;
1861 qf_index = old_qf_index;
1864 theend:
1865 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
1866 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1867 #ifdef FEAT_WINDOWS
1868 if (p_swb != old_swb && opened_window)
1870 /* Restore old 'switchbuf' value, but not when an autocommand or
1871 * modeline has changed the value. */
1872 if (p_swb == empty_option)
1874 p_swb = old_swb;
1875 swb_flags = old_swb_flags;
1877 else
1878 free_string_option(old_swb);
1880 #endif
1884 * ":clist": list all errors
1885 * ":llist": list all locations
1887 void
1888 qf_list(eap)
1889 exarg_T *eap;
1891 buf_T *buf;
1892 char_u *fname;
1893 qfline_T *qfp;
1894 int i;
1895 int idx1 = 1;
1896 int idx2 = -1;
1897 int need_return = TRUE;
1898 char_u *arg = eap->arg;
1899 int all = eap->forceit; /* if not :cl!, only show
1900 recognised errors */
1901 qf_info_T *qi = &ql_info;
1903 if (eap->cmdidx == CMD_llist)
1905 qi = GET_LOC_LIST(curwin);
1906 if (qi == NULL)
1908 EMSG(_(e_loclist));
1909 return;
1913 if (qi->qf_curlist >= qi->qf_listcount
1914 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
1916 EMSG(_(e_quickfix));
1917 return;
1919 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
1921 EMSG(_(e_trailing));
1922 return;
1924 i = qi->qf_lists[qi->qf_curlist].qf_count;
1925 if (idx1 < 0)
1926 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
1927 if (idx2 < 0)
1928 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
1930 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
1931 all = TRUE;
1932 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
1933 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
1935 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
1937 if (need_return)
1939 msg_putchar('\n');
1940 if (got_int)
1941 break;
1942 need_return = FALSE;
1945 fname = NULL;
1946 if (qfp->qf_fnum != 0
1947 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
1949 fname = buf->b_fname;
1950 if (qfp->qf_type == 1) /* :helpgrep */
1951 fname = gettail(fname);
1953 if (fname == NULL)
1954 sprintf((char *)IObuff, "%2d", i);
1955 else
1956 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
1957 i, (char *)fname);
1958 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
1959 ? hl_attr(HLF_L) : hl_attr(HLF_D));
1960 if (qfp->qf_lnum == 0)
1961 IObuff[0] = NUL;
1962 else if (qfp->qf_col == 0)
1963 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
1964 else
1965 sprintf((char *)IObuff, ":%ld col %d",
1966 qfp->qf_lnum, qfp->qf_col);
1967 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
1968 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
1969 msg_puts_attr(IObuff, hl_attr(HLF_N));
1970 if (qfp->qf_pattern != NULL)
1972 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
1973 STRCAT(IObuff, ":");
1974 msg_puts(IObuff);
1976 msg_puts((char_u *)" ");
1978 /* Remove newlines and leading whitespace from the text. For an
1979 * unrecognized line keep the indent, the compiler may mark a word
1980 * with ^^^^. */
1981 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
1982 ? skipwhite(qfp->qf_text) : qfp->qf_text,
1983 IObuff, IOSIZE);
1984 msg_prt_line(IObuff, FALSE);
1985 out_flush(); /* show one line at a time */
1986 need_return = TRUE;
1989 qfp = qfp->qf_next;
1990 ++i;
1991 ui_breakcheck();
1996 * Remove newlines and leading whitespace from an error message.
1997 * Put the result in "buf[bufsize]".
1999 static void
2000 qf_fmt_text(text, buf, bufsize)
2001 char_u *text;
2002 char_u *buf;
2003 int bufsize;
2005 int i;
2006 char_u *p = text;
2008 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2010 if (*p == '\n')
2012 buf[i] = ' ';
2013 while (*++p != NUL)
2014 if (!vim_iswhite(*p) && *p != '\n')
2015 break;
2017 else
2018 buf[i] = *p++;
2020 buf[i] = NUL;
2024 * ":colder [count]": Up in the quickfix stack.
2025 * ":cnewer [count]": Down in the quickfix stack.
2026 * ":lolder [count]": Up in the location list stack.
2027 * ":lnewer [count]": Down in the location list stack.
2029 void
2030 qf_age(eap)
2031 exarg_T *eap;
2033 qf_info_T *qi = &ql_info;
2034 int count;
2036 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2038 qi = GET_LOC_LIST(curwin);
2039 if (qi == NULL)
2041 EMSG(_(e_loclist));
2042 return;
2046 if (eap->addr_count != 0)
2047 count = eap->line2;
2048 else
2049 count = 1;
2050 while (count--)
2052 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
2054 if (qi->qf_curlist == 0)
2056 EMSG(_("E380: At bottom of quickfix stack"));
2057 return;
2059 --qi->qf_curlist;
2061 else
2063 if (qi->qf_curlist >= qi->qf_listcount - 1)
2065 EMSG(_("E381: At top of quickfix stack"));
2066 return;
2068 ++qi->qf_curlist;
2071 qf_msg(qi);
2075 static void
2076 qf_msg(qi)
2077 qf_info_T *qi;
2079 smsg((char_u *)_("error list %d of %d; %d errors"),
2080 qi->qf_curlist + 1, qi->qf_listcount,
2081 qi->qf_lists[qi->qf_curlist].qf_count);
2082 #ifdef FEAT_WINDOWS
2083 qf_update_buffer(qi);
2084 #endif
2088 * Free error list "idx".
2090 static void
2091 qf_free(qi, idx)
2092 qf_info_T *qi;
2093 int idx;
2095 qfline_T *qfp;
2097 while (qi->qf_lists[idx].qf_count)
2099 qfp = qi->qf_lists[idx].qf_start->qf_next;
2100 vim_free(qi->qf_lists[idx].qf_start->qf_text);
2101 vim_free(qi->qf_lists[idx].qf_start->qf_pattern);
2102 vim_free(qi->qf_lists[idx].qf_start);
2103 qi->qf_lists[idx].qf_start = qfp;
2104 --qi->qf_lists[idx].qf_count;
2109 * qf_mark_adjust: adjust marks
2111 void
2112 qf_mark_adjust(wp, line1, line2, amount, amount_after)
2113 win_T *wp;
2114 linenr_T line1;
2115 linenr_T line2;
2116 long amount;
2117 long amount_after;
2119 int i;
2120 qfline_T *qfp;
2121 int idx;
2122 qf_info_T *qi = &ql_info;
2124 if (wp != NULL)
2126 if (wp->w_llist == NULL)
2127 return;
2128 qi = wp->w_llist;
2131 for (idx = 0; idx < qi->qf_listcount; ++idx)
2132 if (qi->qf_lists[idx].qf_count)
2133 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
2134 i < qi->qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
2135 if (qfp->qf_fnum == curbuf->b_fnum)
2137 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2139 if (amount == MAXLNUM)
2140 qfp->qf_cleared = TRUE;
2141 else
2142 qfp->qf_lnum += amount;
2144 else if (amount_after && qfp->qf_lnum > line2)
2145 qfp->qf_lnum += amount_after;
2150 * Make a nice message out of the error character and the error number:
2151 * char number message
2152 * e or E 0 " error"
2153 * w or W 0 " warning"
2154 * i or I 0 " info"
2155 * 0 0 ""
2156 * other 0 " c"
2157 * e or E n " error n"
2158 * w or W n " warning n"
2159 * i or I n " info n"
2160 * 0 n " error n"
2161 * other n " c n"
2162 * 1 x "" :helpgrep
2164 static char_u *
2165 qf_types(c, nr)
2166 int c, nr;
2168 static char_u buf[20];
2169 static char_u cc[3];
2170 char_u *p;
2172 if (c == 'W' || c == 'w')
2173 p = (char_u *)" warning";
2174 else if (c == 'I' || c == 'i')
2175 p = (char_u *)" info";
2176 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2177 p = (char_u *)" error";
2178 else if (c == 0 || c == 1)
2179 p = (char_u *)"";
2180 else
2182 cc[0] = ' ';
2183 cc[1] = c;
2184 cc[2] = NUL;
2185 p = cc;
2188 if (nr <= 0)
2189 return p;
2191 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2192 return buf;
2195 #if defined(FEAT_WINDOWS) || defined(PROTO)
2197 * ":cwindow": open the quickfix window if we have errors to display,
2198 * close it if not.
2199 * ":lwindow": open the location list window if we have locations to display,
2200 * close it if not.
2202 void
2203 ex_cwindow(eap)
2204 exarg_T *eap;
2206 qf_info_T *qi = &ql_info;
2207 win_T *win;
2209 if (eap->cmdidx == CMD_lwindow)
2211 qi = GET_LOC_LIST(curwin);
2212 if (qi == NULL)
2213 return;
2216 /* Look for an existing quickfix window. */
2217 win = qf_find_win(qi);
2220 * If a quickfix window is open but we have no errors to display,
2221 * close the window. If a quickfix window is not open, then open
2222 * it if we have errors; otherwise, leave it closed.
2224 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
2225 || qi->qf_curlist >= qi->qf_listcount)
2227 if (win != NULL)
2228 ex_cclose(eap);
2230 else if (win == NULL)
2231 ex_copen(eap);
2235 * ":cclose": close the window showing the list of errors.
2236 * ":lclose": close the window showing the location list
2238 /*ARGSUSED*/
2239 void
2240 ex_cclose(eap)
2241 exarg_T *eap;
2243 win_T *win = NULL;
2244 qf_info_T *qi = &ql_info;
2246 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2248 qi = GET_LOC_LIST(curwin);
2249 if (qi == NULL)
2250 return;
2253 /* Find existing quickfix window and close it. */
2254 win = qf_find_win(qi);
2255 if (win != NULL)
2256 win_close(win, FALSE);
2260 * ":copen": open a window that shows the list of errors.
2261 * ":lopen": open a window that shows the location list.
2263 void
2264 ex_copen(eap)
2265 exarg_T *eap;
2267 qf_info_T *qi = &ql_info;
2268 int height;
2269 win_T *win;
2270 tabpage_T *prevtab = curtab;
2271 buf_T *qf_buf;
2272 win_T *oldwin = curwin;
2274 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2276 qi = GET_LOC_LIST(curwin);
2277 if (qi == NULL)
2279 EMSG(_(e_loclist));
2280 return;
2284 if (eap->addr_count != 0)
2285 height = eap->line2;
2286 else
2287 height = QF_WINHEIGHT;
2289 #ifdef FEAT_VISUAL
2290 reset_VIsual_and_resel(); /* stop Visual mode */
2291 #endif
2292 #ifdef FEAT_GUI
2293 need_mouse_correct = TRUE;
2294 #endif
2297 * Find existing quickfix window, or open a new one.
2299 win = qf_find_win(qi);
2301 if (win != NULL && cmdmod.tab == 0)
2302 win_goto(win);
2303 else
2305 qf_buf = qf_find_buf(qi);
2307 /* The current window becomes the previous window afterwards. */
2308 win = curwin;
2310 if (eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2311 /* Create the new window at the very bottom. */
2312 win_goto(lastwin);
2313 if (win_split(height, WSP_BELOW) == FAIL)
2314 return; /* not enough room for window */
2315 #ifdef FEAT_SCROLLBIND
2316 curwin->w_p_scb = FALSE;
2317 #endif
2319 /* Remove the location list for the quickfix window */
2320 qf_free_all(curwin);
2322 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2325 * For the location list window, create a reference to the
2326 * location list from the window 'win'.
2328 curwin->w_llist_ref = win->w_llist;
2329 win->w_llist->qf_refcount++;
2332 if (oldwin != curwin)
2333 oldwin = NULL; /* don't store info when in another window */
2334 if (qf_buf != NULL)
2335 /* Use the existing quickfix buffer */
2336 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
2337 ECMD_HIDE + ECMD_OLDBUF, oldwin);
2338 else
2340 /* Create a new quickfix buffer */
2341 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
2342 /* switch off 'swapfile' */
2343 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2344 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
2345 OPT_LOCAL);
2346 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
2347 set_option_value((char_u *)"diff", 0L, NULL, OPT_LOCAL);
2350 /* Only set the height when still in the same tab page and there is no
2351 * window to the side. */
2352 if (curtab == prevtab
2353 #ifdef FEAT_VERTSPLIT
2354 && curwin->w_width == Columns
2355 #endif
2357 win_setheight(height);
2358 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2359 if (win_valid(win))
2360 prevwin = win;
2364 * Fill the buffer with the quickfix list.
2366 qf_fill_buffer(qi);
2368 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
2369 curwin->w_cursor.col = 0;
2370 check_cursor();
2371 update_topline(); /* scroll to show the line */
2375 * Return the number of the current entry (line number in the quickfix
2376 * window).
2378 linenr_T
2379 qf_current_entry(wp)
2380 win_T *wp;
2382 qf_info_T *qi = &ql_info;
2384 if (IS_LL_WINDOW(wp))
2385 /* In the location list window, use the referenced location list */
2386 qi = wp->w_llist_ref;
2388 return qi->qf_lists[qi->qf_curlist].qf_index;
2392 * Update the cursor position in the quickfix window to the current error.
2393 * Return TRUE if there is a quickfix window.
2395 static int
2396 qf_win_pos_update(qi, old_qf_index)
2397 qf_info_T *qi;
2398 int old_qf_index; /* previous qf_index or zero */
2400 win_T *win;
2401 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
2404 * Put the cursor on the current error in the quickfix window, so that
2405 * it's viewable.
2407 win = qf_find_win(qi);
2408 if (win != NULL
2409 && qf_index <= win->w_buffer->b_ml.ml_line_count
2410 && old_qf_index != qf_index)
2412 win_T *old_curwin = curwin;
2414 curwin = win;
2415 curbuf = win->w_buffer;
2416 if (qf_index > old_qf_index)
2418 curwin->w_redraw_top = old_qf_index;
2419 curwin->w_redraw_bot = qf_index;
2421 else
2423 curwin->w_redraw_top = qf_index;
2424 curwin->w_redraw_bot = old_qf_index;
2426 curwin->w_cursor.lnum = qf_index;
2427 curwin->w_cursor.col = 0;
2428 update_topline(); /* scroll to show the line */
2429 redraw_later(VALID);
2430 curwin->w_redr_status = TRUE; /* update ruler */
2431 curwin = old_curwin;
2432 curbuf = curwin->w_buffer;
2434 return win != NULL;
2438 * Check whether the given window is displaying the specified quickfix/location
2439 * list buffer
2441 static int
2442 is_qf_win(win, qi)
2443 win_T *win;
2444 qf_info_T *qi;
2447 * A window displaying the quickfix buffer will have the w_llist_ref field
2448 * set to NULL.
2449 * A window displaying a location list buffer will have the w_llist_ref
2450 * pointing to the location list.
2452 if (bt_quickfix(win->w_buffer))
2453 if ((qi == &ql_info && win->w_llist_ref == NULL)
2454 || (qi != &ql_info && win->w_llist_ref == qi))
2455 return TRUE;
2457 return FALSE;
2461 * Find a window displaying the quickfix/location list 'qi'
2462 * Searches in only the windows opened in the current tab.
2464 static win_T *
2465 qf_find_win(qi)
2466 qf_info_T *qi;
2468 win_T *win;
2470 FOR_ALL_WINDOWS(win)
2471 if (is_qf_win(win, qi))
2472 break;
2474 return win;
2478 * Find a quickfix buffer.
2479 * Searches in windows opened in all the tabs.
2481 static buf_T *
2482 qf_find_buf(qi)
2483 qf_info_T *qi;
2485 tabpage_T *tp;
2486 win_T *win;
2488 FOR_ALL_TAB_WINDOWS(tp, win)
2489 if (is_qf_win(win, qi))
2490 return win->w_buffer;
2492 return NULL;
2496 * Find the quickfix buffer. If it exists, update the contents.
2498 static void
2499 qf_update_buffer(qi)
2500 qf_info_T *qi;
2502 buf_T *buf;
2503 aco_save_T aco;
2505 /* Check if a buffer for the quickfix list exists. Update it. */
2506 buf = qf_find_buf(qi);
2507 if (buf != NULL)
2509 /* set curwin/curbuf to buf and save a few things */
2510 aucmd_prepbuf(&aco, buf);
2512 qf_fill_buffer(qi);
2514 /* restore curwin/curbuf and a few other things */
2515 aucmd_restbuf(&aco);
2517 (void)qf_win_pos_update(qi, 0);
2522 * Fill current buffer with quickfix errors, replacing any previous contents.
2523 * curbuf must be the quickfix buffer!
2525 static void
2526 qf_fill_buffer(qi)
2527 qf_info_T *qi;
2529 linenr_T lnum;
2530 qfline_T *qfp;
2531 buf_T *errbuf;
2532 int len;
2533 int old_KeyTyped = KeyTyped;
2535 /* delete all existing lines */
2536 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2537 (void)ml_delete((linenr_T)1, FALSE);
2539 /* Check if there is anything to display */
2540 if (qi->qf_curlist < qi->qf_listcount)
2542 /* Add one line for each error */
2543 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2544 for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
2546 if (qfp->qf_fnum != 0
2547 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2548 && errbuf->b_fname != NULL)
2550 if (qfp->qf_type == 1) /* :helpgrep */
2551 STRCPY(IObuff, gettail(errbuf->b_fname));
2552 else
2553 STRCPY(IObuff, errbuf->b_fname);
2554 len = (int)STRLEN(IObuff);
2556 else
2557 len = 0;
2558 IObuff[len++] = '|';
2560 if (qfp->qf_lnum > 0)
2562 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2563 len += (int)STRLEN(IObuff + len);
2565 if (qfp->qf_col > 0)
2567 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2568 len += (int)STRLEN(IObuff + len);
2571 sprintf((char *)IObuff + len, "%s",
2572 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2573 len += (int)STRLEN(IObuff + len);
2575 else if (qfp->qf_pattern != NULL)
2577 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2578 len += (int)STRLEN(IObuff + len);
2580 IObuff[len++] = '|';
2581 IObuff[len++] = ' ';
2583 /* Remove newlines and leading whitespace from the text.
2584 * For an unrecognized line keep the indent, the compiler may
2585 * mark a word with ^^^^. */
2586 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2587 IObuff + len, IOSIZE - len);
2589 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2590 == FAIL)
2591 break;
2592 qfp = qfp->qf_next;
2594 /* Delete the empty line which is now at the end */
2595 (void)ml_delete(lnum + 1, FALSE);
2598 /* correct cursor position */
2599 check_lnums(TRUE);
2601 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2602 * resembles reading a file into a buffer, it's more logical when using
2603 * autocommands. */
2604 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2605 curbuf->b_p_ma = FALSE;
2607 #ifdef FEAT_AUTOCMD
2608 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2609 FALSE, curbuf);
2610 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2611 FALSE, curbuf);
2612 #endif
2614 /* make sure it will be redrawn */
2615 redraw_curbuf_later(NOT_VALID);
2617 /* Restore KeyTyped, setting 'filetype' may reset it. */
2618 KeyTyped = old_KeyTyped;
2621 #endif /* FEAT_WINDOWS */
2624 * Return TRUE if "buf" is the quickfix buffer.
2627 bt_quickfix(buf)
2628 buf_T *buf;
2630 return (buf->b_p_bt[0] == 'q');
2634 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2635 * This means the buffer name is not a file name.
2638 bt_nofile(buf)
2639 buf_T *buf;
2641 return (buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2642 || buf->b_p_bt[0] == 'a';
2646 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2649 bt_dontwrite(buf)
2650 buf_T *buf;
2652 return (buf->b_p_bt[0] == 'n');
2656 bt_dontwrite_msg(buf)
2657 buf_T *buf;
2659 if (bt_dontwrite(buf))
2661 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2662 return TRUE;
2664 return FALSE;
2668 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2669 * and 'bufhidden'.
2672 buf_hide(buf)
2673 buf_T *buf;
2675 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2676 switch (buf->b_p_bh[0])
2678 case 'u': /* "unload" */
2679 case 'w': /* "wipe" */
2680 case 'd': return FALSE; /* "delete" */
2681 case 'h': return TRUE; /* "hide" */
2683 return (p_hid || cmdmod.hide);
2687 * Return TRUE when using ":vimgrep" for ":grep".
2690 grep_internal(cmdidx)
2691 cmdidx_T cmdidx;
2693 return ((cmdidx == CMD_grep
2694 || cmdidx == CMD_lgrep
2695 || cmdidx == CMD_grepadd
2696 || cmdidx == CMD_lgrepadd)
2697 && STRCMP("internal",
2698 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2702 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
2704 void
2705 ex_make(eap)
2706 exarg_T *eap;
2708 char_u *fname;
2709 char_u *cmd;
2710 unsigned len;
2711 win_T *wp = NULL;
2712 qf_info_T *qi = &ql_info;
2713 int res;
2714 #ifdef FEAT_AUTOCMD
2715 char_u *au_name = NULL;
2717 switch (eap->cmdidx)
2719 case CMD_make: au_name = (char_u *)"make"; break;
2720 case CMD_lmake: au_name = (char_u *)"lmake"; break;
2721 case CMD_grep: au_name = (char_u *)"grep"; break;
2722 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
2723 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
2724 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
2725 default: break;
2727 if (au_name != NULL)
2729 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2730 curbuf->b_fname, TRUE, curbuf);
2731 # ifdef FEAT_EVAL
2732 if (did_throw || force_abort)
2733 return;
2734 # endif
2736 #endif
2738 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
2739 if (grep_internal(eap->cmdidx))
2741 ex_vimgrep(eap);
2742 return;
2745 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
2746 || eap->cmdidx == CMD_lgrepadd)
2747 wp = curwin;
2749 autowrite_all();
2750 fname = get_mef_name();
2751 if (fname == NULL)
2752 return;
2753 mch_remove(fname); /* in case it's not unique */
2756 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2758 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2759 if (*p_sp != NUL)
2760 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
2761 cmd = alloc(len);
2762 if (cmd == NULL)
2763 return;
2764 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2765 (char *)p_shq);
2766 if (*p_sp != NUL)
2767 append_redir(cmd, p_sp, fname);
2769 * Output a newline if there's something else than the :make command that
2770 * was typed (in which case the cursor is in column 0).
2772 if (msg_col == 0)
2773 msg_didout = FALSE;
2774 msg_start();
2775 MSG_PUTS(":!");
2776 msg_outtrans(cmd); /* show what we are doing */
2778 /* let the shell know if we are redirecting output or not */
2779 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2781 #ifdef AMIGA
2782 out_flush();
2783 /* read window status report and redraw before message */
2784 (void)char_avail();
2785 #endif
2787 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
2788 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
2789 (eap->cmdidx != CMD_grepadd
2790 && eap->cmdidx != CMD_lgrepadd));
2791 #ifdef FEAT_AUTOCMD
2792 if (au_name != NULL)
2793 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2794 curbuf->b_fname, TRUE, curbuf);
2795 #endif
2796 if (res > 0 && !eap->forceit)
2798 if (wp != NULL)
2799 qi = GET_LOC_LIST(wp);
2800 qf_jump(qi, 0, 0, FALSE); /* display first error */
2803 mch_remove(fname);
2804 vim_free(fname);
2805 vim_free(cmd);
2809 * Return the name for the errorfile, in allocated memory.
2810 * Find a new unique name when 'makeef' contains "##".
2811 * Returns NULL for error.
2813 static char_u *
2814 get_mef_name()
2816 char_u *p;
2817 char_u *name;
2818 static int start = -1;
2819 static int off = 0;
2820 #ifdef HAVE_LSTAT
2821 struct stat sb;
2822 #endif
2824 if (*p_mef == NUL)
2826 name = vim_tempname('e');
2827 if (name == NULL)
2828 EMSG(_(e_notmp));
2829 return name;
2832 for (p = p_mef; *p; ++p)
2833 if (p[0] == '#' && p[1] == '#')
2834 break;
2836 if (*p == NUL)
2837 return vim_strsave(p_mef);
2839 /* Keep trying until the name doesn't exist yet. */
2840 for (;;)
2842 if (start == -1)
2843 start = mch_get_pid();
2844 else
2845 off += 19;
2847 name = alloc((unsigned)STRLEN(p_mef) + 30);
2848 if (name == NULL)
2849 break;
2850 STRCPY(name, p_mef);
2851 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2852 STRCAT(name, p + 2);
2853 if (mch_getperm(name) < 0
2854 #ifdef HAVE_LSTAT
2855 /* Don't accept a symbolic link, its a security risk. */
2856 && mch_lstat((char *)name, &sb) < 0
2857 #endif
2859 break;
2860 vim_free(name);
2862 return name;
2866 * ":cc", ":crewind", ":cfirst" and ":clast".
2867 * ":ll", ":lrewind", ":lfirst" and ":llast".
2869 void
2870 ex_cc(eap)
2871 exarg_T *eap;
2873 qf_info_T *qi = &ql_info;
2875 if (eap->cmdidx == CMD_ll
2876 || eap->cmdidx == CMD_lrewind
2877 || eap->cmdidx == CMD_lfirst
2878 || eap->cmdidx == CMD_llast)
2880 qi = GET_LOC_LIST(curwin);
2881 if (qi == NULL)
2883 EMSG(_(e_loclist));
2884 return;
2888 qf_jump(qi, 0,
2889 eap->addr_count > 0
2890 ? (int)eap->line2
2891 : (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
2893 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
2894 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
2896 : 32767,
2897 eap->forceit);
2901 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
2902 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
2904 void
2905 ex_cnext(eap)
2906 exarg_T *eap;
2908 qf_info_T *qi = &ql_info;
2910 if (eap->cmdidx == CMD_lnext
2911 || eap->cmdidx == CMD_lNext
2912 || eap->cmdidx == CMD_lprevious
2913 || eap->cmdidx == CMD_lnfile
2914 || eap->cmdidx == CMD_lNfile
2915 || eap->cmdidx == CMD_lpfile)
2917 qi = GET_LOC_LIST(curwin);
2918 if (qi == NULL)
2920 EMSG(_(e_loclist));
2921 return;
2925 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext)
2926 ? FORWARD
2927 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile)
2928 ? FORWARD_FILE
2929 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
2930 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
2931 ? BACKWARD_FILE
2932 : BACKWARD,
2933 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
2937 * ":cfile"/":cgetfile"/":caddfile" commands.
2938 * ":lfile"/":lgetfile"/":laddfile" commands.
2940 void
2941 ex_cfile(eap)
2942 exarg_T *eap;
2944 win_T *wp = NULL;
2945 qf_info_T *qi = &ql_info;
2947 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
2948 || eap->cmdidx == CMD_laddfile)
2949 wp = curwin;
2951 if (*eap->arg != NUL)
2952 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
2955 * This function is used by the :cfile, :cgetfile and :caddfile
2956 * commands.
2957 * :cfile always creates a new quickfix list and jumps to the
2958 * first error.
2959 * :cgetfile creates a new quickfix list but doesn't jump to the
2960 * first error.
2961 * :caddfile adds to an existing quickfix list. If there is no
2962 * quickfix list then a new list is created.
2964 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
2965 && eap->cmdidx != CMD_laddfile)) > 0
2966 && (eap->cmdidx == CMD_cfile
2967 || eap->cmdidx == CMD_lfile))
2969 if (wp != NULL)
2970 qi = GET_LOC_LIST(wp);
2971 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
2976 * ":vimgrep {pattern} file(s)"
2977 * ":vimgrepadd {pattern} file(s)"
2978 * ":lvimgrep {pattern} file(s)"
2979 * ":lvimgrepadd {pattern} file(s)"
2981 void
2982 ex_vimgrep(eap)
2983 exarg_T *eap;
2985 regmmatch_T regmatch;
2986 int fcount;
2987 char_u **fnames;
2988 char_u *fname;
2989 char_u *s;
2990 char_u *p;
2991 int fi;
2992 qf_info_T *qi = &ql_info;
2993 qfline_T *prevp = NULL;
2994 long lnum;
2995 buf_T *buf;
2996 int duplicate_name = FALSE;
2997 int using_dummy;
2998 int redraw_for_dummy = FALSE;
2999 int found_match;
3000 buf_T *first_match_buf = NULL;
3001 time_t seconds = 0;
3002 int save_mls;
3003 #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3004 char_u *save_ei = NULL;
3005 #endif
3006 aco_save_T aco;
3007 int flags = 0;
3008 colnr_T col;
3009 long tomatch;
3010 char_u dirname_start[MAXPATHL];
3011 char_u dirname_now[MAXPATHL];
3012 char_u *target_dir = NULL;
3013 #ifdef FEAT_AUTOCMD
3014 char_u *au_name = NULL;
3016 switch (eap->cmdidx)
3018 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3019 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3020 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
3021 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
3022 default: break;
3024 if (au_name != NULL)
3026 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3027 curbuf->b_fname, TRUE, curbuf);
3028 if (did_throw || force_abort)
3029 return;
3031 #endif
3033 if (eap->cmdidx == CMD_lgrep
3034 || eap->cmdidx == CMD_lvimgrep
3035 || eap->cmdidx == CMD_lgrepadd
3036 || eap->cmdidx == CMD_lvimgrepadd)
3038 qi = ll_get_or_alloc_list(curwin);
3039 if (qi == NULL)
3040 return;
3043 if (eap->addr_count > 0)
3044 tomatch = eap->line2;
3045 else
3046 tomatch = MAXLNUM;
3048 /* Get the search pattern: either white-separated or enclosed in // */
3049 regmatch.regprog = NULL;
3050 p = skip_vimgrep_pat(eap->arg, &s, &flags);
3051 if (p == NULL)
3053 EMSG(_(e_invalpat));
3054 goto theend;
3056 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
3057 if (regmatch.regprog == NULL)
3058 goto theend;
3059 regmatch.rmm_ic = p_ic;
3060 regmatch.rmm_maxcol = 0;
3062 p = skipwhite(p);
3063 if (*p == NUL)
3065 EMSG(_("E683: File name missing or invalid pattern"));
3066 goto theend;
3069 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
3070 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
3071 || qi->qf_curlist == qi->qf_listcount)
3072 /* make place for a new list */
3073 qf_new_list(qi);
3074 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
3075 /* Adding to existing list, find last entry. */
3076 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
3077 prevp->qf_next != prevp; prevp = prevp->qf_next)
3080 /* parse the list of arguments */
3081 if (get_arglist_exp(p, &fcount, &fnames) == FAIL)
3082 goto theend;
3083 if (fcount == 0)
3085 EMSG(_(e_nomatch));
3086 goto theend;
3089 /* Remember the current directory, because a BufRead autocommand that does
3090 * ":lcd %:p:h" changes the meaning of short path names. */
3091 mch_dirname(dirname_start, MAXPATHL);
3093 seconds = (time_t)0;
3094 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
3096 fname = shorten_fname1(fnames[fi]);
3097 if (time(NULL) > seconds)
3099 /* Display the file name every second or so, show the user we are
3100 * working on it. */
3101 seconds = time(NULL);
3102 msg_start();
3103 p = msg_strtrunc(fname, TRUE);
3104 if (p == NULL)
3105 msg_outtrans(fname);
3106 else
3108 msg_outtrans(p);
3109 vim_free(p);
3111 msg_clr_eos();
3112 msg_didout = FALSE; /* overwrite this message */
3113 msg_nowait = TRUE; /* don't wait for this message */
3114 msg_col = 0;
3115 out_flush();
3118 buf = buflist_findname_exp(fnames[fi]);
3119 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3121 /* Remember that a buffer with this name already exists. */
3122 duplicate_name = (buf != NULL);
3123 using_dummy = TRUE;
3124 redraw_for_dummy = TRUE;
3126 #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3127 /* Don't do Filetype autocommands to avoid loading syntax and
3128 * indent scripts, a great speed improvement. */
3129 save_ei = au_event_disable(",Filetype");
3130 #endif
3131 /* Don't use modelines here, it's useless. */
3132 save_mls = p_mls;
3133 p_mls = 0;
3135 /* Load file into a buffer, so that 'fileencoding' is detected,
3136 * autocommands applied, etc. */
3137 buf = load_dummy_buffer(fname);
3139 /* When autocommands changed directory: go back. We assume it was
3140 * ":lcd %:p:h". */
3141 mch_dirname(dirname_now, MAXPATHL);
3142 if (STRCMP(dirname_start, dirname_now) != 0)
3144 exarg_T ea;
3146 ea.arg = dirname_start;
3147 ea.cmdidx = CMD_lcd;
3148 ex_cd(&ea);
3151 p_mls = save_mls;
3152 #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3153 au_event_restore(save_ei);
3154 #endif
3156 else
3157 /* Use existing, loaded buffer. */
3158 using_dummy = FALSE;
3160 if (buf == NULL)
3162 if (!got_int)
3163 smsg((char_u *)_("Cannot open file \"%s\""), fname);
3165 else
3167 /* Try for a match in all lines of the buffer.
3168 * For ":1vimgrep" look for first match only. */
3169 found_match = FALSE;
3170 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3171 ++lnum)
3173 col = 0;
3174 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
3175 col, NULL) > 0)
3178 if (qf_add_entry(qi, &prevp,
3179 NULL, /* dir */
3180 fname,
3182 ml_get_buf(buf,
3183 regmatch.startpos[0].lnum + lnum, FALSE),
3184 regmatch.startpos[0].lnum + lnum,
3185 regmatch.startpos[0].col + 1,
3186 FALSE, /* vis_col */
3187 NULL, /* search pattern */
3188 0, /* nr */
3189 0, /* type */
3190 TRUE /* valid */
3191 ) == FAIL)
3193 got_int = TRUE;
3194 break;
3196 found_match = TRUE;
3197 if (--tomatch == 0)
3198 break;
3199 if ((flags & VGR_GLOBAL) == 0
3200 || regmatch.endpos[0].lnum > 0)
3201 break;
3202 col = regmatch.endpos[0].col
3203 + (col == regmatch.endpos[0].col);
3204 if (col > STRLEN(ml_get_buf(buf, lnum, FALSE)))
3205 break;
3207 line_breakcheck();
3208 if (got_int)
3209 break;
3212 if (using_dummy)
3214 if (found_match && first_match_buf == NULL)
3215 first_match_buf = buf;
3216 if (duplicate_name)
3218 /* Never keep a dummy buffer if there is another buffer
3219 * with the same name. */
3220 wipe_dummy_buffer(buf);
3221 buf = NULL;
3223 else if (!cmdmod.hide
3224 || buf->b_p_bh[0] == 'u' /* "unload" */
3225 || buf->b_p_bh[0] == 'w' /* "wipe" */
3226 || buf->b_p_bh[0] == 'd') /* "delete" */
3228 /* When no match was found we don't need to remember the
3229 * buffer, wipe it out. If there was a match and it
3230 * wasn't the first one or we won't jump there: only
3231 * unload the buffer.
3232 * Ignore 'hidden' here, because it may lead to having too
3233 * many swap files. */
3234 if (!found_match)
3236 wipe_dummy_buffer(buf);
3237 buf = NULL;
3239 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
3241 unload_dummy_buffer(buf);
3242 buf = NULL;
3246 if (buf != NULL)
3248 /* If the buffer is still loaded we need to use the
3249 * directory we jumped to below. */
3250 if (buf == first_match_buf
3251 && target_dir == NULL
3252 && STRCMP(dirname_start, dirname_now) != 0)
3253 target_dir = vim_strsave(dirname_now);
3255 /* The buffer is still loaded, the Filetype autocommands
3256 * need to be done now, in that buffer. And the modelines
3257 * need to be done (again). But not the window-local
3258 * options! */
3259 aucmd_prepbuf(&aco, buf);
3260 #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3261 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3262 buf->b_fname, TRUE, buf);
3263 #endif
3264 do_modelines(OPT_NOWIN);
3265 aucmd_restbuf(&aco);
3271 FreeWild(fcount, fnames);
3273 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3274 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3275 qi->qf_lists[qi->qf_curlist].qf_index = 1;
3277 #ifdef FEAT_WINDOWS
3278 qf_update_buffer(qi);
3279 #endif
3281 #ifdef FEAT_AUTOCMD
3282 if (au_name != NULL)
3283 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3284 curbuf->b_fname, TRUE, curbuf);
3285 #endif
3287 /* Jump to first match. */
3288 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
3290 if ((flags & VGR_NOJUMP) == 0)
3292 buf = curbuf;
3293 qf_jump(qi, 0, 0, eap->forceit);
3294 if (buf != curbuf)
3295 /* If we jumped to another buffer redrawing will already be
3296 * taken care of. */
3297 redraw_for_dummy = FALSE;
3299 /* Jump to the directory used after loading the buffer. */
3300 if (curbuf == first_match_buf && target_dir != NULL)
3302 exarg_T ea;
3304 ea.arg = target_dir;
3305 ea.cmdidx = CMD_lcd;
3306 ex_cd(&ea);
3310 else
3311 EMSG2(_(e_nomatch2), s);
3313 /* If we loaded a dummy buffer into the current window, the autocommands
3314 * may have messed up things, need to redraw and recompute folds. */
3315 if (redraw_for_dummy)
3317 #ifdef FEAT_FOLDING
3318 foldUpdateAll(curwin);
3319 #else
3320 redraw_later(NOT_VALID);
3321 #endif
3324 theend:
3325 vim_free(target_dir);
3326 vim_free(regmatch.regprog);
3330 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
3331 * Put the start of the pattern in "*s", unless "s" is NULL.
3332 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3333 * If "s" is not NULL terminate the pattern with a NUL.
3334 * Return a pointer to the char just past the pattern plus flags.
3336 char_u *
3337 skip_vimgrep_pat(p, s, flags)
3338 char_u *p;
3339 char_u **s;
3340 int *flags;
3342 int c;
3344 if (vim_isIDc(*p))
3346 /* ":vimgrep pattern fname" */
3347 if (s != NULL)
3348 *s = p;
3349 p = skiptowhite(p);
3350 if (s != NULL && *p != NUL)
3351 *p++ = NUL;
3353 else
3355 /* ":vimgrep /pattern/[g][j] fname" */
3356 if (s != NULL)
3357 *s = p + 1;
3358 c = *p;
3359 p = skip_regexp(p + 1, c, TRUE, NULL);
3360 if (*p != c)
3361 return NULL;
3363 /* Truncate the pattern. */
3364 if (s != NULL)
3365 *p = NUL;
3366 ++p;
3368 /* Find the flags */
3369 while (*p == 'g' || *p == 'j')
3371 if (flags != NULL)
3373 if (*p == 'g')
3374 *flags |= VGR_GLOBAL;
3375 else
3376 *flags |= VGR_NOJUMP;
3378 ++p;
3381 return p;
3385 * Load file "fname" into a dummy buffer and return the buffer pointer.
3386 * Returns NULL if it fails.
3387 * Must call unload_dummy_buffer() or wipe_dummy_buffer() later!
3389 static buf_T *
3390 load_dummy_buffer(fname)
3391 char_u *fname;
3393 buf_T *newbuf;
3394 int failed = TRUE;
3395 aco_save_T aco;
3397 /* Allocate a buffer without putting it in the buffer list. */
3398 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
3399 if (newbuf == NULL)
3400 return NULL;
3402 /* Init the options. */
3403 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
3405 /* set curwin/curbuf to buf and save a few things */
3406 aucmd_prepbuf(&aco, newbuf);
3408 /* Need to set the filename for autocommands. */
3409 (void)setfname(curbuf, fname, NULL, FALSE);
3411 if (ml_open(curbuf) == OK)
3413 /* Create swap file now to avoid the ATTENTION message. */
3414 check_need_swap(TRUE);
3416 /* Remove the "dummy" flag, otherwise autocommands may not
3417 * work. */
3418 curbuf->b_flags &= ~BF_DUMMY;
3420 if (readfile(fname, NULL,
3421 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
3422 NULL, READ_NEW | READ_DUMMY) == OK
3423 && !got_int
3424 && !(curbuf->b_flags & BF_NEW))
3426 failed = FALSE;
3427 if (curbuf != newbuf)
3429 /* Bloody autocommands changed the buffer! */
3430 if (buf_valid(newbuf))
3431 wipe_buffer(newbuf, FALSE);
3432 newbuf = curbuf;
3437 /* restore curwin/curbuf and a few other things */
3438 aucmd_restbuf(&aco);
3440 if (!buf_valid(newbuf))
3441 return NULL;
3442 if (failed)
3444 wipe_dummy_buffer(newbuf);
3445 return NULL;
3447 return newbuf;
3451 * Wipe out the dummy buffer that load_dummy_buffer() created.
3453 static void
3454 wipe_dummy_buffer(buf)
3455 buf_T *buf;
3457 if (curbuf != buf) /* safety check */
3459 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3460 cleanup_T cs;
3462 /* Reset the error/interrupt/exception state here so that aborting()
3463 * returns FALSE when wiping out the buffer. Otherwise it doesn't
3464 * work when got_int is set. */
3465 enter_cleanup(&cs);
3466 #endif
3468 wipe_buffer(buf, FALSE);
3470 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3471 /* Restore the error/interrupt/exception state if not discarded by a
3472 * new aborting error, interrupt, or uncaught exception. */
3473 leave_cleanup(&cs);
3474 #endif
3479 * Unload the dummy buffer that load_dummy_buffer() created.
3481 static void
3482 unload_dummy_buffer(buf)
3483 buf_T *buf;
3485 if (curbuf != buf) /* safety check */
3486 close_buffer(NULL, buf, DOBUF_UNLOAD);
3489 #if defined(FEAT_EVAL) || defined(PROTO)
3491 * Add each quickfix error to list "list" as a dictionary.
3494 get_errorlist(wp, list)
3495 win_T *wp;
3496 list_T *list;
3498 qf_info_T *qi = &ql_info;
3499 dict_T *dict;
3500 char_u buf[2];
3501 qfline_T *qfp;
3502 int i;
3503 int bufnum;
3505 if (wp != NULL)
3507 qi = GET_LOC_LIST(wp);
3508 if (qi == NULL)
3509 return FAIL;
3512 if (qi->qf_curlist >= qi->qf_listcount
3513 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
3514 return FAIL;
3516 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3517 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
3519 /* Handle entries with a non-existing buffer number. */
3520 bufnum = qfp->qf_fnum;
3521 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3522 bufnum = 0;
3524 if ((dict = dict_alloc()) == NULL)
3525 return FAIL;
3526 if (list_append_dict(list, dict) == FAIL)
3527 return FAIL;
3529 buf[0] = qfp->qf_type;
3530 buf[1] = NUL;
3531 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
3532 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
3533 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
3534 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
3535 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
3536 || dict_add_nr_str(dict, "pattern", 0L,
3537 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
3538 || dict_add_nr_str(dict, "text", 0L,
3539 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
3540 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
3541 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
3542 return FAIL;
3544 qfp = qfp->qf_next;
3546 return OK;
3550 * Populate the quickfix list with the items supplied in the list
3551 * of dictionaries.
3554 set_errorlist(wp, list, action)
3555 win_T *wp;
3556 list_T *list;
3557 int action;
3559 listitem_T *li;
3560 dict_T *d;
3561 char_u *filename, *pattern, *text, *type;
3562 int bufnum;
3563 long lnum;
3564 int col, nr;
3565 int vcol;
3566 qfline_T *prevp = NULL;
3567 int valid, status;
3568 int retval = OK;
3569 qf_info_T *qi = &ql_info;
3570 int did_bufnr_emsg = FALSE;
3572 if (wp != NULL)
3574 qi = ll_get_or_alloc_list(wp);
3575 if (qi == NULL)
3576 return FAIL;
3579 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
3580 /* make place for a new list */
3581 qf_new_list(qi);
3582 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
3583 /* Adding to existing list, find last entry. */
3584 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
3585 prevp->qf_next != prevp; prevp = prevp->qf_next)
3587 else if (action == 'r')
3588 qf_free(qi, qi->qf_curlist);
3590 for (li = list->lv_first; li != NULL; li = li->li_next)
3592 if (li->li_tv.v_type != VAR_DICT)
3593 continue; /* Skip non-dict items */
3595 d = li->li_tv.vval.v_dict;
3596 if (d == NULL)
3597 continue;
3599 filename = get_dict_string(d, (char_u *)"filename", TRUE);
3600 bufnum = get_dict_number(d, (char_u *)"bufnr");
3601 lnum = get_dict_number(d, (char_u *)"lnum");
3602 col = get_dict_number(d, (char_u *)"col");
3603 vcol = get_dict_number(d, (char_u *)"vcol");
3604 nr = get_dict_number(d, (char_u *)"nr");
3605 type = get_dict_string(d, (char_u *)"type", TRUE);
3606 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
3607 text = get_dict_string(d, (char_u *)"text", TRUE);
3608 if (text == NULL)
3609 text = vim_strsave((char_u *)"");
3611 valid = TRUE;
3612 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
3613 valid = FALSE;
3615 /* Mark entries with non-existing buffer number as not valid. Give the
3616 * error message only once. */
3617 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3619 if (!did_bufnr_emsg)
3621 did_bufnr_emsg = TRUE;
3622 EMSGN(_("E92: Buffer %ld not found"), bufnum);
3624 valid = FALSE;
3625 bufnum = 0;
3628 status = qf_add_entry(qi, &prevp,
3629 NULL, /* dir */
3630 filename,
3631 bufnum,
3632 text,
3633 lnum,
3634 col,
3635 vcol, /* vis_col */
3636 pattern, /* search pattern */
3638 type == NULL ? NUL : *type,
3639 valid);
3641 vim_free(filename);
3642 vim_free(pattern);
3643 vim_free(text);
3644 vim_free(type);
3646 if (status == FAIL)
3648 retval = FAIL;
3649 break;
3653 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3654 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3655 qi->qf_lists[qi->qf_curlist].qf_index = 1;
3657 #ifdef FEAT_WINDOWS
3658 qf_update_buffer(qi);
3659 #endif
3661 return retval;
3663 #endif
3666 * ":[range]cbuffer [bufnr]" command.
3667 * ":[range]caddbuffer [bufnr]" command.
3668 * ":[range]cgetbuffer [bufnr]" command.
3669 * ":[range]lbuffer [bufnr]" command.
3670 * ":[range]laddbuffer [bufnr]" command.
3671 * ":[range]lgetbuffer [bufnr]" command.
3673 void
3674 ex_cbuffer(eap)
3675 exarg_T *eap;
3677 buf_T *buf = NULL;
3678 qf_info_T *qi = &ql_info;
3680 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
3681 || eap->cmdidx == CMD_laddbuffer)
3683 qi = ll_get_or_alloc_list(curwin);
3684 if (qi == NULL)
3685 return;
3688 if (*eap->arg == NUL)
3689 buf = curbuf;
3690 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
3691 buf = buflist_findnr(atoi((char *)eap->arg));
3692 if (buf == NULL)
3693 EMSG(_(e_invarg));
3694 else if (buf->b_ml.ml_mfp == NULL)
3695 EMSG(_("E681: Buffer is not loaded"));
3696 else
3698 if (eap->addr_count == 0)
3700 eap->line1 = 1;
3701 eap->line2 = buf->b_ml.ml_line_count;
3703 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
3704 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
3705 EMSG(_(e_invrange));
3706 else
3708 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
3709 (eap->cmdidx != CMD_caddbuffer
3710 && eap->cmdidx != CMD_laddbuffer),
3711 eap->line1, eap->line2) > 0
3712 && (eap->cmdidx == CMD_cbuffer
3713 || eap->cmdidx == CMD_lbuffer))
3714 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
3719 #if defined(FEAT_EVAL) || defined(PROTO)
3721 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
3722 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
3724 void
3725 ex_cexpr(eap)
3726 exarg_T *eap;
3728 typval_T *tv;
3729 qf_info_T *qi = &ql_info;
3731 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
3732 || eap->cmdidx == CMD_laddexpr)
3734 qi = ll_get_or_alloc_list(curwin);
3735 if (qi == NULL)
3736 return;
3739 /* Evaluate the expression. When the result is a string or a list we can
3740 * use it to fill the errorlist. */
3741 tv = eval_expr(eap->arg, NULL);
3742 if (tv != NULL)
3744 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
3745 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
3747 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
3748 (eap->cmdidx != CMD_caddexpr
3749 && eap->cmdidx != CMD_laddexpr),
3750 (linenr_T)0, (linenr_T)0) > 0
3751 && (eap->cmdidx == CMD_cexpr
3752 || eap->cmdidx == CMD_lexpr))
3753 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
3755 else
3756 EMSG(_("E777: String or List expected"));
3757 free_tv(tv);
3760 #endif
3763 * ":helpgrep {pattern}"
3765 void
3766 ex_helpgrep(eap)
3767 exarg_T *eap;
3769 regmatch_T regmatch;
3770 char_u *save_cpo;
3771 char_u *p;
3772 int fcount;
3773 char_u **fnames;
3774 FILE *fd;
3775 int fi;
3776 qfline_T *prevp = NULL;
3777 long lnum;
3778 #ifdef FEAT_MULTI_LANG
3779 char_u *lang;
3780 #endif
3781 qf_info_T *qi = &ql_info;
3782 int new_qi = FALSE;
3783 win_T *wp;
3785 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
3786 save_cpo = p_cpo;
3787 p_cpo = empty_option;
3789 #ifdef FEAT_MULTI_LANG
3790 /* Check for a specified language */
3791 lang = check_help_lang(eap->arg);
3792 #endif
3794 if (eap->cmdidx == CMD_lhelpgrep)
3796 /* Find an existing help window */
3797 FOR_ALL_WINDOWS(wp)
3798 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
3799 break;
3801 if (wp == NULL) /* Help window not found */
3802 qi = NULL;
3803 else
3804 qi = wp->w_llist;
3806 if (qi == NULL)
3808 /* Allocate a new location list for help text matches */
3809 if ((qi = ll_new_list()) == NULL)
3810 return;
3811 new_qi = TRUE;
3815 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
3816 regmatch.rm_ic = FALSE;
3817 if (regmatch.regprog != NULL)
3819 /* create a new quickfix list */
3820 qf_new_list(qi);
3822 /* Go through all directories in 'runtimepath' */
3823 p = p_rtp;
3824 while (*p != NUL && !got_int)
3826 copy_option_part(&p, NameBuff, MAXPATHL, ",");
3828 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
3829 add_pathsep(NameBuff);
3830 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
3831 if (gen_expand_wildcards(1, &NameBuff, &fcount,
3832 &fnames, EW_FILE|EW_SILENT) == OK
3833 && fcount > 0)
3835 for (fi = 0; fi < fcount && !got_int; ++fi)
3837 #ifdef FEAT_MULTI_LANG
3838 /* Skip files for a different language. */
3839 if (lang != NULL
3840 && STRNICMP(lang, fnames[fi]
3841 + STRLEN(fnames[fi]) - 3, 2) != 0
3842 && !(STRNICMP(lang, "en", 2) == 0
3843 && STRNICMP("txt", fnames[fi]
3844 + STRLEN(fnames[fi]) - 3, 3) == 0))
3845 continue;
3846 #endif
3847 fd = mch_fopen((char *)fnames[fi], "r");
3848 if (fd != NULL)
3850 lnum = 1;
3851 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
3853 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
3855 int l = (int)STRLEN(IObuff);
3857 /* remove trailing CR, LF, spaces, etc. */
3858 while (l > 0 && IObuff[l - 1] <= ' ')
3859 IObuff[--l] = NUL;
3861 if (qf_add_entry(qi, &prevp,
3862 NULL, /* dir */
3863 fnames[fi],
3865 IObuff,
3866 lnum,
3867 (int)(regmatch.startp[0] - IObuff)
3868 + 1, /* col */
3869 FALSE, /* vis_col */
3870 NULL, /* search pattern */
3871 0, /* nr */
3872 1, /* type */
3873 TRUE /* valid */
3874 ) == FAIL)
3876 got_int = TRUE;
3877 break;
3880 ++lnum;
3881 line_breakcheck();
3883 fclose(fd);
3886 FreeWild(fcount, fnames);
3889 vim_free(regmatch.regprog);
3891 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3892 qi->qf_lists[qi->qf_curlist].qf_ptr =
3893 qi->qf_lists[qi->qf_curlist].qf_start;
3894 qi->qf_lists[qi->qf_curlist].qf_index = 1;
3897 if (p_cpo == empty_option)
3898 p_cpo = save_cpo;
3899 else
3900 /* Darn, some plugin changed the value. */
3901 free_string_option(save_cpo);
3903 #ifdef FEAT_WINDOWS
3904 qf_update_buffer(qi);
3905 #endif
3907 /* Jump to first match. */
3908 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
3909 qf_jump(qi, 0, 0, FALSE);
3910 else
3911 EMSG2(_(e_nomatch2), eap->arg);
3913 if (eap->cmdidx == CMD_lhelpgrep)
3915 /* If the help window is not opened or if it already points to the
3916 * correct location list, then free the new location list. */
3917 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
3919 if (new_qi)
3920 ll_free_all(&qi);
3922 else if (curwin->w_llist == NULL)
3923 curwin->w_llist = qi;
3927 #endif /* FEAT_QUICKFIX */