Fix full-screen mode for secondary monitors
[MacVim.git] / src / quickfix.c
blob32c6cd08d4e7dce529844928c07efbdc6235d205
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 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));
110 static void qf_msg __ARGS((qf_info_T *qi));
111 static void qf_free __ARGS((qf_info_T *qi, int idx));
112 static char_u *qf_types __ARGS((int, int));
113 static int qf_get_fnum __ARGS((char_u *, char_u *));
114 static char_u *qf_push_dir __ARGS((char_u *, struct dir_stack_T **));
115 static char_u *qf_pop_dir __ARGS((struct dir_stack_T **));
116 static char_u *qf_guess_filepath __ARGS((char_u *));
117 static void qf_fmt_text __ARGS((char_u *text, char_u *buf, int bufsize));
118 static void qf_clean_dir_stack __ARGS((struct dir_stack_T **));
119 #ifdef FEAT_WINDOWS
120 static int qf_win_pos_update __ARGS((qf_info_T *qi, int old_qf_index));
121 static int is_qf_win __ARGS((win_T *win, qf_info_T *qi));
122 static win_T *qf_find_win __ARGS((qf_info_T *qi));
123 static buf_T *qf_find_buf __ARGS((qf_info_T *qi));
124 static void qf_update_buffer __ARGS((qf_info_T *qi));
125 static void qf_fill_buffer __ARGS((qf_info_T *qi));
126 #endif
127 static char_u *get_mef_name __ARGS((void));
128 static buf_T *load_dummy_buffer __ARGS((char_u *fname));
129 static void wipe_dummy_buffer __ARGS((buf_T *buf));
130 static void unload_dummy_buffer __ARGS((buf_T *buf));
131 static qf_info_T *ll_get_or_alloc_list __ARGS((win_T *));
133 /* Quickfix window check helper macro */
134 #define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
135 /* Location list window check helper macro */
136 #define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
138 * Return location list for window 'wp'
139 * For location list window, return the referenced location list
141 #define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
144 * Read the errorfile "efile" into memory, line by line, building the error
145 * list.
146 * Return -1 for error, number of errors for success.
149 qf_init(wp, efile, errorformat, newlist)
150 win_T *wp;
151 char_u *efile;
152 char_u *errorformat;
153 int newlist; /* TRUE: start a new error list */
155 qf_info_T *qi = &ql_info;
157 if (efile == NULL)
158 return FAIL;
160 if (wp != NULL)
162 qi = ll_get_or_alloc_list(wp);
163 if (qi == NULL)
164 return FAIL;
167 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
168 (linenr_T)0, (linenr_T)0);
172 * Read the errorfile "efile" into memory, line by line, building the error
173 * list.
174 * Alternative: when "efile" is null read errors from buffer "buf".
175 * Always use 'errorformat' from "buf" if there is a local value.
176 * Then lnumfirst and lnumlast specify the range of lines to use.
177 * Return -1 for error, number of errors for success.
179 static int
180 qf_init_ext(qi, efile, buf, tv, errorformat, newlist, lnumfirst, lnumlast)
181 qf_info_T *qi;
182 char_u *efile;
183 buf_T *buf;
184 typval_T *tv;
185 char_u *errorformat;
186 int newlist; /* TRUE: start a new error list */
187 linenr_T lnumfirst; /* first line number to use */
188 linenr_T lnumlast; /* last line number to use */
190 char_u *namebuf;
191 char_u *errmsg;
192 char_u *pattern;
193 char_u *fmtstr = NULL;
194 int col = 0;
195 char_u use_viscol = FALSE;
196 int type = 0;
197 int valid;
198 linenr_T buflnum = lnumfirst;
199 long lnum = 0L;
200 int enr = 0;
201 FILE *fd = NULL;
202 qfline_T *qfprev = NULL; /* init to make SASC shut up */
203 char_u *efmp;
204 efm_T *fmt_first = NULL;
205 efm_T *fmt_last = NULL;
206 efm_T *fmt_ptr;
207 efm_T *fmt_start = NULL;
208 char_u *efm;
209 char_u *ptr;
210 char_u *srcptr;
211 int len;
212 int i;
213 int round;
214 int idx = 0;
215 int multiline = FALSE;
216 int multiignore = FALSE;
217 int multiscan = FALSE;
218 int retval = -1; /* default: return error flag */
219 char_u *directory = NULL;
220 char_u *currfile = NULL;
221 char_u *tail = NULL;
222 char_u *p_str = NULL;
223 listitem_T *p_li = NULL;
224 struct dir_stack_T *file_stack = NULL;
225 regmatch_T regmatch;
226 static struct fmtpattern
228 char_u convchar;
229 char *pattern;
230 } fmt_pat[FMT_PATTERNS] =
232 {'f', ".\\+"}, /* only used when at end */
233 {'n', "\\d\\+"},
234 {'l', "\\d\\+"},
235 {'c', "\\d\\+"},
236 {'t', "."},
237 {'m', ".\\+"},
238 {'r', ".*"},
239 {'p', "[- .]*"},
240 {'v', "\\d\\+"},
241 {'s', ".\\+"}
244 namebuf = alloc(CMDBUFFSIZE + 1);
245 errmsg = alloc(CMDBUFFSIZE + 1);
246 pattern = alloc(CMDBUFFSIZE + 1);
247 if (namebuf == NULL || errmsg == NULL || pattern == NULL)
248 goto qf_init_end;
250 if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
252 EMSG2(_(e_openerrf), efile);
253 goto qf_init_end;
256 if (newlist || qi->qf_curlist == qi->qf_listcount)
257 /* make place for a new list */
258 qf_new_list(qi);
259 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
260 /* Adding to existing list, find last entry. */
261 for (qfprev = qi->qf_lists[qi->qf_curlist].qf_start;
262 qfprev->qf_next != qfprev; qfprev = qfprev->qf_next)
266 * Each part of the format string is copied and modified from errorformat to
267 * regex prog. Only a few % characters are allowed.
269 /* Use the local value of 'errorformat' if it's set. */
270 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
271 efm = buf->b_p_efm;
272 else
273 efm = errorformat;
275 * Get some space to modify the format string into.
277 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
278 for (round = FMT_PATTERNS; round > 0; )
279 i += (int)STRLEN(fmt_pat[--round].pattern);
280 #ifdef COLON_IN_FILENAME
281 i += 12; /* "%f" can become twelve chars longer */
282 #else
283 i += 2; /* "%f" can become two chars longer */
284 #endif
285 if ((fmtstr = alloc(i)) == NULL)
286 goto error2;
288 while (efm[0] != NUL)
291 * Allocate a new eformat structure and put it at the end of the list
293 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
294 if (fmt_ptr == NULL)
295 goto error2;
296 if (fmt_first == NULL) /* first one */
297 fmt_first = fmt_ptr;
298 else
299 fmt_last->next = fmt_ptr;
300 fmt_last = fmt_ptr;
303 * Isolate one part in the 'errorformat' option
305 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
306 if (efm[len] == '\\' && efm[len + 1] != NUL)
307 ++len;
310 * Build regexp pattern from current 'errorformat' option
312 ptr = fmtstr;
313 *ptr++ = '^';
314 round = 0;
315 for (efmp = efm; efmp < efm + len; ++efmp)
317 if (*efmp == '%')
319 ++efmp;
320 for (idx = 0; idx < FMT_PATTERNS; ++idx)
321 if (fmt_pat[idx].convchar == *efmp)
322 break;
323 if (idx < FMT_PATTERNS)
325 if (fmt_ptr->addr[idx])
327 sprintf((char *)errmsg,
328 _("E372: Too many %%%c in format string"), *efmp);
329 EMSG(errmsg);
330 goto error2;
332 if ((idx
333 && idx < 6
334 && vim_strchr((char_u *)"DXOPQ",
335 fmt_ptr->prefix) != NULL)
336 || (idx == 6
337 && vim_strchr((char_u *)"OPQ",
338 fmt_ptr->prefix) == NULL))
340 sprintf((char *)errmsg,
341 _("E373: Unexpected %%%c in format string"), *efmp);
342 EMSG(errmsg);
343 goto error2;
345 fmt_ptr->addr[idx] = (char_u)++round;
346 *ptr++ = '\\';
347 *ptr++ = '(';
348 #ifdef BACKSLASH_IN_FILENAME
349 if (*efmp == 'f')
351 /* Also match "c:" in the file name, even when
352 * checking for a colon next: "%f:".
353 * "\%(\a:\)\=" */
354 STRCPY(ptr, "\\%(\\a:\\)\\=");
355 ptr += 10;
357 #endif
358 if (*efmp == 'f' && efmp[1] != NUL)
360 if (efmp[1] != '\\' && efmp[1] != '%')
362 /* A file name may contain spaces, but this isn't
363 * in "\f". For "%f:%l:%m" there may be a ":" in
364 * the file name. Use ".\{-1,}x" instead (x is
365 * the next character), the requirement that :999:
366 * follows should work. */
367 STRCPY(ptr, ".\\{-1,}");
368 ptr += 7;
370 else
372 /* File name followed by '\\' or '%': include as
373 * many file name chars as possible. */
374 STRCPY(ptr, "\\f\\+");
375 ptr += 4;
378 else
380 srcptr = (char_u *)fmt_pat[idx].pattern;
381 while ((*ptr = *srcptr++) != NUL)
382 ++ptr;
384 *ptr++ = '\\';
385 *ptr++ = ')';
387 else if (*efmp == '*')
389 if (*++efmp == '[' || *efmp == '\\')
391 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
393 if (efmp[1] == '^')
394 *ptr++ = *++efmp;
395 if (efmp < efm + len)
397 *ptr++ = *++efmp; /* could be ']' */
398 while (efmp < efm + len
399 && (*ptr++ = *++efmp) != ']')
400 /* skip */;
401 if (efmp == efm + len)
403 EMSG(_("E374: Missing ] in format string"));
404 goto error2;
408 else if (efmp < efm + len) /* %*\D, %*\s etc. */
409 *ptr++ = *++efmp;
410 *ptr++ = '\\';
411 *ptr++ = '+';
413 else
415 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
416 sprintf((char *)errmsg,
417 _("E375: Unsupported %%%c in format string"), *efmp);
418 EMSG(errmsg);
419 goto error2;
422 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
423 *ptr++ = *efmp; /* regexp magic characters */
424 else if (*efmp == '#')
425 *ptr++ = '*';
426 else if (*efmp == '>')
427 fmt_ptr->conthere = TRUE;
428 else if (efmp == efm + 1) /* analyse prefix */
430 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
431 fmt_ptr->flags = *efmp++;
432 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
433 fmt_ptr->prefix = *efmp;
434 else
436 sprintf((char *)errmsg,
437 _("E376: Invalid %%%c in format string prefix"), *efmp);
438 EMSG(errmsg);
439 goto error2;
442 else
444 sprintf((char *)errmsg,
445 _("E377: Invalid %%%c in format string"), *efmp);
446 EMSG(errmsg);
447 goto error2;
450 else /* copy normal character */
452 if (*efmp == '\\' && efmp + 1 < efm + len)
453 ++efmp;
454 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
455 *ptr++ = '\\'; /* escape regexp atoms */
456 if (*efmp)
457 *ptr++ = *efmp;
460 *ptr++ = '$';
461 *ptr = NUL;
462 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
463 goto error2;
465 * Advance to next part
467 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
469 if (fmt_first == NULL) /* nothing found */
471 EMSG(_("E378: 'errorformat' contains no pattern"));
472 goto error2;
476 * got_int is reset here, because it was probably set when killing the
477 * ":make" command, but we still want to read the errorfile then.
479 got_int = FALSE;
481 /* Always ignore case when looking for a matching error. */
482 regmatch.rm_ic = TRUE;
484 if (tv != NULL)
486 if (tv->v_type == VAR_STRING)
487 p_str = tv->vval.v_string;
488 else if (tv->v_type == VAR_LIST)
489 p_li = tv->vval.v_list->lv_first;
493 * Read the lines in the error file one by one.
494 * Try to recognize one of the error formats in each line.
496 while (!got_int)
498 /* Get the next line. */
499 if (fd == NULL)
501 if (tv != NULL)
503 if (tv->v_type == VAR_STRING)
505 /* Get the next line from the supplied string */
506 char_u *p;
508 if (!*p_str) /* Reached the end of the string */
509 break;
511 p = vim_strchr(p_str, '\n');
512 if (p)
513 len = (int)(p - p_str + 1);
514 else
515 len = (int)STRLEN(p_str);
517 if (len > CMDBUFFSIZE - 2)
518 vim_strncpy(IObuff, p_str, CMDBUFFSIZE - 2);
519 else
520 vim_strncpy(IObuff, p_str, len);
522 p_str += len;
524 else if (tv->v_type == VAR_LIST)
526 /* Get the next line from the supplied list */
527 while (p_li && p_li->li_tv.v_type != VAR_STRING)
528 p_li = p_li->li_next; /* Skip non-string items */
530 if (!p_li) /* End of the list */
531 break;
533 len = (int)STRLEN(p_li->li_tv.vval.v_string);
534 if (len > CMDBUFFSIZE - 2)
535 len = CMDBUFFSIZE - 2;
537 vim_strncpy(IObuff, p_li->li_tv.vval.v_string, len);
539 p_li = p_li->li_next; /* next item */
542 else
544 /* Get the next line from the supplied buffer */
545 if (buflnum > lnumlast)
546 break;
547 vim_strncpy(IObuff, ml_get_buf(buf, buflnum++, FALSE),
548 CMDBUFFSIZE - 2);
551 else if (fgets((char *)IObuff, CMDBUFFSIZE - 2, fd) == NULL)
552 break;
554 IObuff[CMDBUFFSIZE - 2] = NUL; /* for very long lines */
555 if ((efmp = vim_strrchr(IObuff, '\n')) != NULL)
556 *efmp = NUL;
557 #ifdef USE_CRNL
558 if ((efmp = vim_strrchr(IObuff, '\r')) != NULL)
559 *efmp = NUL;
560 #endif
562 /* If there was no %> item start at the first pattern */
563 if (fmt_start == NULL)
564 fmt_ptr = fmt_first;
565 else
567 fmt_ptr = fmt_start;
568 fmt_start = NULL;
572 * Try to match each part of 'errorformat' until we find a complete
573 * match or no match.
575 valid = TRUE;
576 restofline:
577 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
579 idx = fmt_ptr->prefix;
580 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
581 continue;
582 namebuf[0] = NUL;
583 pattern[0] = NUL;
584 if (!multiscan)
585 errmsg[0] = NUL;
586 lnum = 0;
587 col = 0;
588 use_viscol = FALSE;
589 enr = -1;
590 type = 0;
591 tail = NULL;
593 regmatch.regprog = fmt_ptr->prog;
594 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
596 if ((idx == 'C' || idx == 'Z') && !multiline)
597 continue;
598 if (vim_strchr((char_u *)"EWI", idx) != NULL)
599 type = idx;
600 else
601 type = 0;
603 * Extract error message data from matched line.
604 * We check for an actual submatch, because "\[" and "\]" in
605 * the 'errorformat' may cause the wrong submatch to be used.
607 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
609 int c;
611 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
612 continue;
614 /* Expand ~/file and $HOME/file to full path. */
615 c = *regmatch.endp[i];
616 *regmatch.endp[i] = NUL;
617 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
618 *regmatch.endp[i] = c;
620 if (vim_strchr((char_u *)"OPQ", idx) != NULL
621 && mch_getperm(namebuf) == -1)
622 continue;
624 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
626 if (regmatch.startp[i] == NULL)
627 continue;
628 enr = (int)atol((char *)regmatch.startp[i]);
630 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
632 if (regmatch.startp[i] == NULL)
633 continue;
634 lnum = atol((char *)regmatch.startp[i]);
636 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
638 if (regmatch.startp[i] == NULL)
639 continue;
640 col = (int)atol((char *)regmatch.startp[i]);
642 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
644 if (regmatch.startp[i] == NULL)
645 continue;
646 type = *regmatch.startp[i];
648 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
649 STRCPY(errmsg, IObuff);
650 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
652 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
653 continue;
654 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
655 vim_strncpy(errmsg, regmatch.startp[i], len);
657 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
659 if (regmatch.startp[i] == NULL)
660 continue;
661 tail = regmatch.startp[i];
663 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
665 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
666 continue;
667 col = (int)(regmatch.endp[i] - regmatch.startp[i] + 1);
668 if (*((char_u *)regmatch.startp[i]) != TAB)
669 use_viscol = TRUE;
671 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
673 if (regmatch.startp[i] == NULL)
674 continue;
675 col = (int)atol((char *)regmatch.startp[i]);
676 use_viscol = TRUE;
678 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
680 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
681 continue;
682 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
683 if (len > CMDBUFFSIZE - 5)
684 len = CMDBUFFSIZE - 5;
685 STRCPY(pattern, "^\\V");
686 STRNCAT(pattern, regmatch.startp[i], len);
687 pattern[len + 3] = '\\';
688 pattern[len + 4] = '$';
689 pattern[len + 5] = NUL;
691 break;
694 multiscan = FALSE;
696 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
698 if (fmt_ptr != NULL)
700 if (idx == 'D') /* enter directory */
702 if (*namebuf == NUL)
704 EMSG(_("E379: Missing or empty directory name"));
705 goto error2;
707 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
708 goto error2;
710 else if (idx == 'X') /* leave directory */
711 directory = qf_pop_dir(&dir_stack);
713 namebuf[0] = NUL; /* no match found, remove file name */
714 lnum = 0; /* don't jump to this line */
715 valid = FALSE;
716 STRCPY(errmsg, IObuff); /* copy whole line to error message */
717 if (fmt_ptr == NULL)
718 multiline = multiignore = FALSE;
720 else if (fmt_ptr != NULL)
722 /* honor %> item */
723 if (fmt_ptr->conthere)
724 fmt_start = fmt_ptr;
726 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
727 multiline = TRUE; /* start of a multi-line message */
728 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
729 { /* continuation of multi-line msg */
730 if (qfprev == NULL)
731 goto error2;
732 if (*errmsg && !multiignore)
734 len = (int)STRLEN(qfprev->qf_text);
735 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
736 == NULL)
737 goto error2;
738 STRCPY(ptr, qfprev->qf_text);
739 vim_free(qfprev->qf_text);
740 qfprev->qf_text = ptr;
741 *(ptr += len) = '\n';
742 STRCPY(++ptr, errmsg);
744 if (qfprev->qf_nr == -1)
745 qfprev->qf_nr = enr;
746 if (vim_isprintc(type) && !qfprev->qf_type)
747 qfprev->qf_type = type; /* only printable chars allowed */
748 if (!qfprev->qf_lnum)
749 qfprev->qf_lnum = lnum;
750 if (!qfprev->qf_col)
751 qfprev->qf_col = col;
752 qfprev->qf_viscol = use_viscol;
753 if (!qfprev->qf_fnum)
754 qfprev->qf_fnum = qf_get_fnum(directory,
755 *namebuf || directory ? namebuf
756 : currfile && valid ? currfile : 0);
757 if (idx == 'Z')
758 multiline = multiignore = FALSE;
759 line_breakcheck();
760 continue;
762 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
764 /* global file names */
765 valid = FALSE;
766 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
768 if (*namebuf && idx == 'P')
769 currfile = qf_push_dir(namebuf, &file_stack);
770 else if (idx == 'Q')
771 currfile = qf_pop_dir(&file_stack);
772 *namebuf = NUL;
773 if (tail && *tail)
775 STRCPY(IObuff, skipwhite(tail));
776 multiscan = TRUE;
777 goto restofline;
781 if (fmt_ptr->flags == '-') /* generally exclude this line */
783 if (multiline)
784 multiignore = TRUE; /* also exclude continuation lines */
785 continue;
789 if (qf_add_entry(qi, &qfprev,
790 directory,
791 (*namebuf || directory)
792 ? namebuf
793 : ((currfile && valid) ? currfile : (char_u *)NULL),
795 errmsg,
796 lnum,
797 col,
798 use_viscol,
799 pattern,
800 enr,
801 type,
802 valid) == FAIL)
803 goto error2;
804 line_breakcheck();
806 if (fd == NULL || !ferror(fd))
808 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
810 /* no valid entry found */
811 qi->qf_lists[qi->qf_curlist].qf_ptr =
812 qi->qf_lists[qi->qf_curlist].qf_start;
813 qi->qf_lists[qi->qf_curlist].qf_index = 1;
814 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
816 else
818 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
819 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
820 qi->qf_lists[qi->qf_curlist].qf_ptr =
821 qi->qf_lists[qi->qf_curlist].qf_start;
823 /* return number of matches */
824 retval = qi->qf_lists[qi->qf_curlist].qf_count;
825 goto qf_init_ok;
827 EMSG(_(e_readerrf));
828 error2:
829 qf_free(qi, qi->qf_curlist);
830 qi->qf_listcount--;
831 if (qi->qf_curlist > 0)
832 --qi->qf_curlist;
833 qf_init_ok:
834 if (fd != NULL)
835 fclose(fd);
836 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
838 fmt_first = fmt_ptr->next;
839 vim_free(fmt_ptr->prog);
840 vim_free(fmt_ptr);
842 qf_clean_dir_stack(&dir_stack);
843 qf_clean_dir_stack(&file_stack);
844 qf_init_end:
845 vim_free(namebuf);
846 vim_free(errmsg);
847 vim_free(pattern);
848 vim_free(fmtstr);
850 #ifdef FEAT_WINDOWS
851 qf_update_buffer(qi);
852 #endif
854 return retval;
858 * Prepare for adding a new quickfix list.
860 static void
861 qf_new_list(qi)
862 qf_info_T *qi;
864 int i;
867 * If the current entry is not the last entry, delete entries below
868 * the current entry. This makes it possible to browse in a tree-like
869 * way with ":grep'.
871 while (qi->qf_listcount > qi->qf_curlist + 1)
872 qf_free(qi, --qi->qf_listcount);
875 * When the stack is full, remove to oldest entry
876 * Otherwise, add a new entry.
878 if (qi->qf_listcount == LISTCOUNT)
880 qf_free(qi, 0);
881 for (i = 1; i < LISTCOUNT; ++i)
882 qi->qf_lists[i - 1] = qi->qf_lists[i];
883 qi->qf_curlist = LISTCOUNT - 1;
885 else
886 qi->qf_curlist = qi->qf_listcount++;
887 qi->qf_lists[qi->qf_curlist].qf_index = 0;
888 qi->qf_lists[qi->qf_curlist].qf_count = 0;
892 * Free a location list
894 static void
895 ll_free_all(pqi)
896 qf_info_T **pqi;
898 int i;
899 qf_info_T *qi;
901 qi = *pqi;
902 if (qi == NULL)
903 return;
904 *pqi = NULL; /* Remove reference to this list */
906 qi->qf_refcount--;
907 if (qi->qf_refcount < 1)
909 /* No references to this location list */
910 for (i = 0; i < qi->qf_listcount; ++i)
911 qf_free(qi, i);
912 vim_free(qi);
916 void
917 qf_free_all(wp)
918 win_T *wp;
920 int i;
921 qf_info_T *qi = &ql_info;
923 if (wp != NULL)
925 /* location list */
926 ll_free_all(&wp->w_llist);
927 ll_free_all(&wp->w_llist_ref);
929 else
930 /* quickfix list */
931 for (i = 0; i < qi->qf_listcount; ++i)
932 qf_free(qi, i);
936 * Add an entry to the end of the list of errors.
937 * Returns OK or FAIL.
939 static int
940 qf_add_entry(qi, prevp, dir, fname, bufnum, mesg, lnum, col, vis_col, pattern,
941 nr, type, valid)
942 qf_info_T *qi; /* quickfix list */
943 qfline_T **prevp; /* pointer to previously added entry or NULL */
944 char_u *dir; /* optional directory name */
945 char_u *fname; /* file name or NULL */
946 int bufnum; /* buffer number or zero */
947 char_u *mesg; /* message */
948 long lnum; /* line number */
949 int col; /* column */
950 int vis_col; /* using visual column */
951 char_u *pattern; /* search pattern */
952 int nr; /* error number */
953 int type; /* type character */
954 int valid; /* valid entry */
956 qfline_T *qfp;
958 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
959 return FAIL;
960 if (bufnum != 0)
961 qfp->qf_fnum = bufnum;
962 else
963 qfp->qf_fnum = qf_get_fnum(dir, fname);
964 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
966 vim_free(qfp);
967 return FAIL;
969 qfp->qf_lnum = lnum;
970 qfp->qf_col = col;
971 qfp->qf_viscol = vis_col;
972 if (pattern == NULL || *pattern == NUL)
973 qfp->qf_pattern = NULL;
974 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
976 vim_free(qfp->qf_text);
977 vim_free(qfp);
978 return FAIL;
980 qfp->qf_nr = nr;
981 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
982 type = 0;
983 qfp->qf_type = type;
984 qfp->qf_valid = valid;
986 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
987 /* first element in the list */
989 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
990 qfp->qf_prev = qfp; /* first element points to itself */
992 else
994 qfp->qf_prev = *prevp;
995 (*prevp)->qf_next = qfp;
997 qfp->qf_next = qfp; /* last element points to itself */
998 qfp->qf_cleared = FALSE;
999 *prevp = qfp;
1000 ++qi->qf_lists[qi->qf_curlist].qf_count;
1001 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1002 /* first valid entry */
1004 qi->qf_lists[qi->qf_curlist].qf_index =
1005 qi->qf_lists[qi->qf_curlist].qf_count;
1006 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
1009 return OK;
1013 * Allocate a new location list
1015 static qf_info_T *
1016 ll_new_list()
1018 qf_info_T *qi;
1020 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1021 if (qi != NULL)
1023 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1024 qi->qf_refcount++;
1027 return qi;
1031 * Return the location list for window 'wp'.
1032 * If not present, allocate a location list
1034 static qf_info_T *
1035 ll_get_or_alloc_list(wp)
1036 win_T *wp;
1038 if (IS_LL_WINDOW(wp))
1039 /* For a location list window, use the referenced location list */
1040 return wp->w_llist_ref;
1043 * For a non-location list window, w_llist_ref should not point to a
1044 * location list.
1046 ll_free_all(&wp->w_llist_ref);
1048 if (wp->w_llist == NULL)
1049 wp->w_llist = ll_new_list(); /* new location list */
1050 return wp->w_llist;
1054 * Copy the location list from window "from" to window "to".
1056 void
1057 copy_loclist(from, to)
1058 win_T *from;
1059 win_T *to;
1061 qf_info_T *qi;
1062 int idx;
1063 int i;
1066 * When copying from a location list window, copy the referenced
1067 * location list. For other windows, copy the location list for
1068 * that window.
1070 if (IS_LL_WINDOW(from))
1071 qi = from->w_llist_ref;
1072 else
1073 qi = from->w_llist;
1075 if (qi == NULL) /* no location list to copy */
1076 return;
1078 /* allocate a new location list */
1079 if ((to->w_llist = ll_new_list()) == NULL)
1080 return;
1082 to->w_llist->qf_listcount = qi->qf_listcount;
1084 /* Copy the location lists one at a time */
1085 for (idx = 0; idx < qi->qf_listcount; idx++)
1087 qf_list_T *from_qfl;
1088 qf_list_T *to_qfl;
1090 to->w_llist->qf_curlist = idx;
1092 from_qfl = &qi->qf_lists[idx];
1093 to_qfl = &to->w_llist->qf_lists[idx];
1095 /* Some of the fields are populated by qf_add_entry() */
1096 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1097 to_qfl->qf_count = 0;
1098 to_qfl->qf_index = 0;
1099 to_qfl->qf_start = NULL;
1100 to_qfl->qf_ptr = NULL;
1102 if (from_qfl->qf_count)
1104 qfline_T *from_qfp;
1105 qfline_T *prevp = NULL;
1107 /* copy all the location entries in this list */
1108 for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
1109 ++i, from_qfp = from_qfp->qf_next)
1111 if (qf_add_entry(to->w_llist, &prevp,
1112 NULL,
1113 NULL,
1115 from_qfp->qf_text,
1116 from_qfp->qf_lnum,
1117 from_qfp->qf_col,
1118 from_qfp->qf_viscol,
1119 from_qfp->qf_pattern,
1120 from_qfp->qf_nr,
1122 from_qfp->qf_valid) == FAIL)
1124 qf_free_all(to);
1125 return;
1128 * qf_add_entry() will not set the qf_num field, as the
1129 * directory and file names are not supplied. So the qf_fnum
1130 * field is copied here.
1132 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1133 prevp->qf_type = from_qfp->qf_type; /* error type */
1134 if (from_qfl->qf_ptr == from_qfp)
1135 to_qfl->qf_ptr = prevp; /* current location */
1139 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1141 /* When no valid entries are present in the list, qf_ptr points to
1142 * the first item in the list */
1143 if (to_qfl->qf_nonevalid == TRUE)
1144 to_qfl->qf_ptr = to_qfl->qf_start;
1147 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1151 * get buffer number for file "dir.name"
1153 static int
1154 qf_get_fnum(directory, fname)
1155 char_u *directory;
1156 char_u *fname;
1158 if (fname == NULL || *fname == NUL) /* no file name */
1159 return 0;
1161 #ifdef RISCOS
1162 /* Name is reported as `main.c', but file is `c.main' */
1163 return ro_buflist_add(fname);
1164 #else
1165 char_u *ptr;
1166 int fnum;
1168 # ifdef VMS
1169 vms_remove_version(fname);
1170 # endif
1171 # ifdef BACKSLASH_IN_FILENAME
1172 if (directory != NULL)
1173 slash_adjust(directory);
1174 slash_adjust(fname);
1175 # endif
1176 if (directory != NULL && !vim_isAbsName(fname)
1177 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1180 * Here we check if the file really exists.
1181 * This should normally be true, but if make works without
1182 * "leaving directory"-messages we might have missed a
1183 * directory change.
1185 if (mch_getperm(ptr) < 0)
1187 vim_free(ptr);
1188 directory = qf_guess_filepath(fname);
1189 if (directory)
1190 ptr = concat_fnames(directory, fname, TRUE);
1191 else
1192 ptr = vim_strsave(fname);
1194 /* Use concatenated directory name and file name */
1195 fnum = buflist_add(ptr, 0);
1196 vim_free(ptr);
1197 return fnum;
1199 return buflist_add(fname, 0);
1200 #endif
1205 * push dirbuf onto the directory stack and return pointer to actual dir or
1206 * NULL on error
1208 static char_u *
1209 qf_push_dir(dirbuf, stackptr)
1210 char_u *dirbuf;
1211 struct dir_stack_T **stackptr;
1213 struct dir_stack_T *ds_new;
1214 struct dir_stack_T *ds_ptr;
1216 /* allocate new stack element and hook it in */
1217 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1218 if (ds_new == NULL)
1219 return NULL;
1221 ds_new->next = *stackptr;
1222 *stackptr = ds_new;
1224 /* store directory on the stack */
1225 if (vim_isAbsName(dirbuf)
1226 || (*stackptr)->next == NULL
1227 || (*stackptr && dir_stack != *stackptr))
1228 (*stackptr)->dirname = vim_strsave(dirbuf);
1229 else
1231 /* Okay we don't have an absolute path.
1232 * dirbuf must be a subdir of one of the directories on the stack.
1233 * Let's search...
1235 ds_new = (*stackptr)->next;
1236 (*stackptr)->dirname = NULL;
1237 while (ds_new)
1239 vim_free((*stackptr)->dirname);
1240 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1241 TRUE);
1242 if (mch_isdir((*stackptr)->dirname) == TRUE)
1243 break;
1245 ds_new = ds_new->next;
1248 /* clean up all dirs we already left */
1249 while ((*stackptr)->next != ds_new)
1251 ds_ptr = (*stackptr)->next;
1252 (*stackptr)->next = (*stackptr)->next->next;
1253 vim_free(ds_ptr->dirname);
1254 vim_free(ds_ptr);
1257 /* Nothing found -> it must be on top level */
1258 if (ds_new == NULL)
1260 vim_free((*stackptr)->dirname);
1261 (*stackptr)->dirname = vim_strsave(dirbuf);
1265 if ((*stackptr)->dirname != NULL)
1266 return (*stackptr)->dirname;
1267 else
1269 ds_ptr = *stackptr;
1270 *stackptr = (*stackptr)->next;
1271 vim_free(ds_ptr);
1272 return NULL;
1278 * pop dirbuf from the directory stack and return previous directory or NULL if
1279 * stack is empty
1281 static char_u *
1282 qf_pop_dir(stackptr)
1283 struct dir_stack_T **stackptr;
1285 struct dir_stack_T *ds_ptr;
1287 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1288 * What to do if it isn't? */
1290 /* pop top element and free it */
1291 if (*stackptr != NULL)
1293 ds_ptr = *stackptr;
1294 *stackptr = (*stackptr)->next;
1295 vim_free(ds_ptr->dirname);
1296 vim_free(ds_ptr);
1299 /* return NEW top element as current dir or NULL if stack is empty*/
1300 return *stackptr ? (*stackptr)->dirname : NULL;
1304 * clean up directory stack
1306 static void
1307 qf_clean_dir_stack(stackptr)
1308 struct dir_stack_T **stackptr;
1310 struct dir_stack_T *ds_ptr;
1312 while ((ds_ptr = *stackptr) != NULL)
1314 *stackptr = (*stackptr)->next;
1315 vim_free(ds_ptr->dirname);
1316 vim_free(ds_ptr);
1321 * Check in which directory of the directory stack the given file can be
1322 * found.
1323 * Returns a pointer to the directory name or NULL if not found
1324 * Cleans up intermediate directory entries.
1326 * TODO: How to solve the following problem?
1327 * If we have the this directory tree:
1328 * ./
1329 * ./aa
1330 * ./aa/bb
1331 * ./bb
1332 * ./bb/x.c
1333 * and make says:
1334 * making all in aa
1335 * making all in bb
1336 * x.c:9: Error
1337 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1338 * qf_guess_filepath will return NULL.
1340 static char_u *
1341 qf_guess_filepath(filename)
1342 char_u *filename;
1344 struct dir_stack_T *ds_ptr;
1345 struct dir_stack_T *ds_tmp;
1346 char_u *fullname;
1348 /* no dirs on the stack - there's nothing we can do */
1349 if (dir_stack == NULL)
1350 return NULL;
1352 ds_ptr = dir_stack->next;
1353 fullname = NULL;
1354 while (ds_ptr)
1356 vim_free(fullname);
1357 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1359 /* If concat_fnames failed, just go on. The worst thing that can happen
1360 * is that we delete the entire stack.
1362 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1363 break;
1365 ds_ptr = ds_ptr->next;
1368 vim_free(fullname);
1370 /* clean up all dirs we already left */
1371 while (dir_stack->next != ds_ptr)
1373 ds_tmp = dir_stack->next;
1374 dir_stack->next = dir_stack->next->next;
1375 vim_free(ds_tmp->dirname);
1376 vim_free(ds_tmp);
1379 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1384 * jump to a quickfix line
1385 * if dir == FORWARD go "errornr" valid entries forward
1386 * if dir == BACKWARD go "errornr" valid entries backward
1387 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1388 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1389 * else if "errornr" is zero, redisplay the same line
1390 * else go to entry "errornr"
1392 void
1393 qf_jump(qi, dir, errornr, forceit)
1394 qf_info_T *qi;
1395 int dir;
1396 int errornr;
1397 int forceit;
1399 qf_info_T *ll_ref;
1400 qfline_T *qf_ptr;
1401 qfline_T *old_qf_ptr;
1402 int qf_index;
1403 int old_qf_fnum;
1404 int old_qf_index;
1405 int prev_index;
1406 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1407 char_u *err = e_no_more_items;
1408 linenr_T i;
1409 buf_T *old_curbuf;
1410 linenr_T old_lnum;
1411 colnr_T screen_col;
1412 colnr_T char_col;
1413 char_u *line;
1414 #ifdef FEAT_WINDOWS
1415 char_u *old_swb = p_swb;
1416 int opened_window = FALSE;
1417 win_T *win;
1418 win_T *altwin;
1419 #endif
1420 int print_message = TRUE;
1421 int len;
1422 #ifdef FEAT_FOLDING
1423 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1424 #endif
1425 int ok = OK;
1426 int usable_win;
1428 if (qi == NULL)
1429 qi = &ql_info;
1431 if (qi->qf_curlist >= qi->qf_listcount
1432 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
1434 EMSG(_(e_quickfix));
1435 return;
1438 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
1439 old_qf_ptr = qf_ptr;
1440 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
1441 old_qf_index = qf_index;
1442 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1444 while (errornr--)
1446 old_qf_ptr = qf_ptr;
1447 prev_index = qf_index;
1448 old_qf_fnum = qf_ptr->qf_fnum;
1451 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
1452 || qf_ptr->qf_next == NULL)
1454 qf_ptr = old_qf_ptr;
1455 qf_index = prev_index;
1456 if (err != NULL)
1458 EMSG(_(err));
1459 goto theend;
1461 errornr = 0;
1462 break;
1464 ++qf_index;
1465 qf_ptr = qf_ptr->qf_next;
1466 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1467 && !qf_ptr->qf_valid)
1468 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1469 err = NULL;
1472 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1474 while (errornr--)
1476 old_qf_ptr = qf_ptr;
1477 prev_index = qf_index;
1478 old_qf_fnum = qf_ptr->qf_fnum;
1481 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1483 qf_ptr = old_qf_ptr;
1484 qf_index = prev_index;
1485 if (err != NULL)
1487 EMSG(_(err));
1488 goto theend;
1490 errornr = 0;
1491 break;
1493 --qf_index;
1494 qf_ptr = qf_ptr->qf_prev;
1495 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1496 && !qf_ptr->qf_valid)
1497 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1498 err = NULL;
1501 else if (errornr != 0) /* go to specified number */
1503 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1505 --qf_index;
1506 qf_ptr = qf_ptr->qf_prev;
1508 while (errornr > qf_index && qf_index <
1509 qi->qf_lists[qi->qf_curlist].qf_count
1510 && qf_ptr->qf_next != NULL)
1512 ++qf_index;
1513 qf_ptr = qf_ptr->qf_next;
1517 #ifdef FEAT_WINDOWS
1518 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1519 if (qf_win_pos_update(qi, old_qf_index))
1520 /* No need to print the error message if it's visible in the error
1521 * window */
1522 print_message = FALSE;
1525 * For ":helpgrep" find a help window or open one.
1527 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
1529 win_T *wp;
1530 int n;
1532 if (cmdmod.tab != 0)
1533 wp = NULL;
1534 else
1535 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1536 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1537 break;
1538 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1539 win_enter(wp, TRUE);
1540 else
1543 * Split off help window; put it at far top if no position
1544 * specified, the current window is vertically split and narrow.
1546 n = WSP_HELP;
1547 # ifdef FEAT_VERTSPLIT
1548 if (cmdmod.split == 0 && curwin->w_width != Columns
1549 && curwin->w_width < 80)
1550 n |= WSP_TOP;
1551 # endif
1552 if (win_split(0, n) == FAIL)
1553 goto theend;
1554 opened_window = TRUE; /* close it when fail */
1556 if (curwin->w_height < p_hh)
1557 win_setheight((int)p_hh);
1559 if (qi != &ql_info) /* not a quickfix list */
1561 /* The new window should use the supplied location list */
1562 qf_free_all(curwin);
1563 curwin->w_llist = qi;
1564 qi->qf_refcount++;
1568 if (!p_im)
1569 restart_edit = 0; /* don't want insert mode in help file */
1573 * If currently in the quickfix window, find another window to show the
1574 * file in.
1576 if (bt_quickfix(curbuf) && !opened_window)
1579 * If there is no file specified, we don't know where to go.
1580 * But do advance, otherwise ":cn" gets stuck.
1582 if (qf_ptr->qf_fnum == 0)
1583 goto theend;
1585 /* Locate a window showing a normal buffer */
1586 usable_win = 0;
1587 FOR_ALL_WINDOWS(win)
1588 if (win->w_buffer->b_p_bt[0] == NUL)
1590 usable_win = 1;
1591 break;
1595 * If no usable window is found and 'switchbuf' is set to 'usetab'
1596 * then search in other tabs.
1598 if (!usable_win && vim_strchr(p_swb, 'a') != NULL)
1600 tabpage_T *tp;
1601 win_T *wp;
1603 FOR_ALL_TAB_WINDOWS(tp, wp)
1605 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
1607 goto_tabpage_win(tp, wp);
1608 usable_win = 1;
1609 break;
1615 * If there is only one window and it is the quickfix window, create a
1616 * new one above the quickfix window.
1618 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
1620 ll_ref = curwin->w_llist_ref;
1622 if (win_split(0, WSP_ABOVE) == FAIL)
1623 goto failed; /* not enough room for window */
1624 opened_window = TRUE; /* close it when fail */
1625 p_swb = empty_option; /* don't split again */
1626 # ifdef FEAT_SCROLLBIND
1627 curwin->w_p_scb = FALSE;
1628 # endif
1629 if (ll_ref != NULL)
1631 /* The new window should use the location list from the
1632 * location list window */
1633 qf_free_all(curwin);
1634 curwin->w_llist = ll_ref;
1635 ll_ref->qf_refcount++;
1638 else
1640 if (curwin->w_llist_ref != NULL)
1642 /* In a location window */
1643 ll_ref = curwin->w_llist_ref;
1645 /* Find the window with the same location list */
1646 FOR_ALL_WINDOWS(win)
1647 if (win->w_llist == ll_ref)
1648 break;
1649 if (win == NULL)
1651 /* Find the window showing the selected file */
1652 FOR_ALL_WINDOWS(win)
1653 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1654 break;
1655 if (win == NULL)
1657 /* Find a previous usable window */
1658 win = curwin;
1661 if (win->w_buffer->b_p_bt[0] == NUL)
1662 break;
1663 if (win->w_prev == NULL)
1664 win = lastwin; /* wrap around the top */
1665 else
1666 win = win->w_prev; /* go to previous window */
1667 } while (win != curwin);
1670 win_goto(win);
1672 /* If the location list for the window is not set, then set it
1673 * to the location list from the location window */
1674 if (win->w_llist == NULL)
1676 win->w_llist = ll_ref;
1677 ll_ref->qf_refcount++;
1680 else
1684 * Try to find a window that shows the right buffer.
1685 * Default to the window just above the quickfix buffer.
1687 win = curwin;
1688 altwin = NULL;
1689 for (;;)
1691 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1692 break;
1693 if (win->w_prev == NULL)
1694 win = lastwin; /* wrap around the top */
1695 else
1696 win = win->w_prev; /* go to previous window */
1698 if (IS_QF_WINDOW(win))
1700 /* Didn't find it, go to the window before the quickfix
1701 * window. */
1702 if (altwin != NULL)
1703 win = altwin;
1704 else if (curwin->w_prev != NULL)
1705 win = curwin->w_prev;
1706 else
1707 win = curwin->w_next;
1708 break;
1711 /* Remember a usable window. */
1712 if (altwin == NULL && !win->w_p_pvw
1713 && win->w_buffer->b_p_bt[0] == NUL)
1714 altwin = win;
1717 win_goto(win);
1721 #endif
1724 * If there is a file name,
1725 * read the wanted file if needed, and check autowrite etc.
1727 old_curbuf = curbuf;
1728 old_lnum = curwin->w_cursor.lnum;
1730 if (qf_ptr->qf_fnum != 0)
1732 if (qf_ptr->qf_type == 1)
1734 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1735 * set b_p_ro flag). */
1736 if (!can_abandon(curbuf, forceit))
1738 EMSG(_(e_nowrtmsg));
1739 ok = FALSE;
1741 else
1742 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
1743 ECMD_HIDE + ECMD_SET_HELP);
1745 else
1746 ok = buflist_getfile(qf_ptr->qf_fnum,
1747 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
1750 if (ok == OK)
1752 /* When not switched to another buffer, still need to set pc mark */
1753 if (curbuf == old_curbuf)
1754 setpcmark();
1756 if (qf_ptr->qf_pattern == NULL)
1759 * Go to line with error, unless qf_lnum is 0.
1761 i = qf_ptr->qf_lnum;
1762 if (i > 0)
1764 if (i > curbuf->b_ml.ml_line_count)
1765 i = curbuf->b_ml.ml_line_count;
1766 curwin->w_cursor.lnum = i;
1768 if (qf_ptr->qf_col > 0)
1770 curwin->w_cursor.col = qf_ptr->qf_col - 1;
1771 if (qf_ptr->qf_viscol == TRUE)
1774 * Check each character from the beginning of the error
1775 * line up to the error column. For each tab character
1776 * found, reduce the error column value by the length of
1777 * a tab character.
1779 line = ml_get_curline();
1780 screen_col = 0;
1781 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1783 if (*line == NUL)
1784 break;
1785 if (*line++ == '\t')
1787 curwin->w_cursor.col -= 7 - (screen_col % 8);
1788 screen_col += 8 - (screen_col % 8);
1790 else
1791 ++screen_col;
1794 check_cursor();
1796 else
1797 beginline(BL_WHITE | BL_FIX);
1799 else
1801 pos_T save_cursor;
1803 /* Move the cursor to the first line in the buffer */
1804 save_cursor = curwin->w_cursor;
1805 curwin->w_cursor.lnum = 0;
1806 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1, SEARCH_KEEP))
1807 curwin->w_cursor = save_cursor;
1810 #ifdef FEAT_FOLDING
1811 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1812 foldOpenCursor();
1813 #endif
1814 if (print_message)
1816 /* Update the screen before showing the message */
1817 update_topline_redraw();
1818 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
1819 qi->qf_lists[qi->qf_curlist].qf_count,
1820 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1821 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1822 /* Add the message, skipping leading whitespace and newlines. */
1823 len = (int)STRLEN(IObuff);
1824 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1826 /* Output the message. Overwrite to avoid scrolling when the 'O'
1827 * flag is present in 'shortmess'; But when not jumping, print the
1828 * whole message. */
1829 i = msg_scroll;
1830 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1831 msg_scroll = TRUE;
1832 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1833 msg_scroll = FALSE;
1834 msg_attr_keep(IObuff, 0, TRUE);
1835 msg_scroll = i;
1838 else
1840 #ifdef FEAT_WINDOWS
1841 if (opened_window)
1842 win_close(curwin, TRUE); /* Close opened window */
1843 #endif
1844 if (qf_ptr->qf_fnum != 0)
1847 * Couldn't open file, so put index back where it was. This could
1848 * happen if the file was readonly and we changed something.
1850 #ifdef FEAT_WINDOWS
1851 failed:
1852 #endif
1853 qf_ptr = old_qf_ptr;
1854 qf_index = old_qf_index;
1857 theend:
1858 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
1859 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1860 #ifdef FEAT_WINDOWS
1861 if (p_swb != old_swb && opened_window)
1863 /* Restore old 'switchbuf' value, but not when an autocommand or
1864 * modeline has changed the value. */
1865 if (p_swb == empty_option)
1866 p_swb = old_swb;
1867 else
1868 free_string_option(old_swb);
1870 #endif
1874 * ":clist": list all errors
1875 * ":llist": list all locations
1877 void
1878 qf_list(eap)
1879 exarg_T *eap;
1881 buf_T *buf;
1882 char_u *fname;
1883 qfline_T *qfp;
1884 int i;
1885 int idx1 = 1;
1886 int idx2 = -1;
1887 int need_return = TRUE;
1888 char_u *arg = eap->arg;
1889 int all = eap->forceit; /* if not :cl!, only show
1890 recognised errors */
1891 qf_info_T *qi = &ql_info;
1893 if (eap->cmdidx == CMD_llist)
1895 qi = GET_LOC_LIST(curwin);
1896 if (qi == NULL)
1898 EMSG(_(e_loclist));
1899 return;
1903 if (qi->qf_curlist >= qi->qf_listcount
1904 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
1906 EMSG(_(e_quickfix));
1907 return;
1909 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
1911 EMSG(_(e_trailing));
1912 return;
1914 i = qi->qf_lists[qi->qf_curlist].qf_count;
1915 if (idx1 < 0)
1916 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
1917 if (idx2 < 0)
1918 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
1920 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
1921 all = TRUE;
1922 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
1923 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
1925 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
1927 if (need_return)
1929 msg_putchar('\n');
1930 if (got_int)
1931 break;
1932 need_return = FALSE;
1935 fname = NULL;
1936 if (qfp->qf_fnum != 0
1937 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
1939 fname = buf->b_fname;
1940 if (qfp->qf_type == 1) /* :helpgrep */
1941 fname = gettail(fname);
1943 if (fname == NULL)
1944 sprintf((char *)IObuff, "%2d", i);
1945 else
1946 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
1947 i, (char *)fname);
1948 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
1949 ? hl_attr(HLF_L) : hl_attr(HLF_D));
1950 if (qfp->qf_lnum == 0)
1951 IObuff[0] = NUL;
1952 else if (qfp->qf_col == 0)
1953 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
1954 else
1955 sprintf((char *)IObuff, ":%ld col %d",
1956 qfp->qf_lnum, qfp->qf_col);
1957 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
1958 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
1959 msg_puts_attr(IObuff, hl_attr(HLF_N));
1960 if (qfp->qf_pattern != NULL)
1962 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
1963 STRCAT(IObuff, ":");
1964 msg_puts(IObuff);
1966 msg_puts((char_u *)" ");
1968 /* Remove newlines and leading whitespace from the text. For an
1969 * unrecognized line keep the indent, the compiler may mark a word
1970 * with ^^^^. */
1971 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
1972 ? skipwhite(qfp->qf_text) : qfp->qf_text,
1973 IObuff, IOSIZE);
1974 msg_prt_line(IObuff, FALSE);
1975 out_flush(); /* show one line at a time */
1976 need_return = TRUE;
1979 qfp = qfp->qf_next;
1980 ++i;
1981 ui_breakcheck();
1986 * Remove newlines and leading whitespace from an error message.
1987 * Put the result in "buf[bufsize]".
1989 static void
1990 qf_fmt_text(text, buf, bufsize)
1991 char_u *text;
1992 char_u *buf;
1993 int bufsize;
1995 int i;
1996 char_u *p = text;
1998 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2000 if (*p == '\n')
2002 buf[i] = ' ';
2003 while (*++p != NUL)
2004 if (!vim_iswhite(*p) && *p != '\n')
2005 break;
2007 else
2008 buf[i] = *p++;
2010 buf[i] = NUL;
2014 * ":colder [count]": Up in the quickfix stack.
2015 * ":cnewer [count]": Down in the quickfix stack.
2016 * ":lolder [count]": Up in the location list stack.
2017 * ":lnewer [count]": Down in the location list stack.
2019 void
2020 qf_age(eap)
2021 exarg_T *eap;
2023 qf_info_T *qi = &ql_info;
2024 int count;
2026 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2028 qi = GET_LOC_LIST(curwin);
2029 if (qi == NULL)
2031 EMSG(_(e_loclist));
2032 return;
2036 if (eap->addr_count != 0)
2037 count = eap->line2;
2038 else
2039 count = 1;
2040 while (count--)
2042 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
2044 if (qi->qf_curlist == 0)
2046 EMSG(_("E380: At bottom of quickfix stack"));
2047 return;
2049 --qi->qf_curlist;
2051 else
2053 if (qi->qf_curlist >= qi->qf_listcount - 1)
2055 EMSG(_("E381: At top of quickfix stack"));
2056 return;
2058 ++qi->qf_curlist;
2061 qf_msg(qi);
2065 static void
2066 qf_msg(qi)
2067 qf_info_T *qi;
2069 smsg((char_u *)_("error list %d of %d; %d errors"),
2070 qi->qf_curlist + 1, qi->qf_listcount,
2071 qi->qf_lists[qi->qf_curlist].qf_count);
2072 #ifdef FEAT_WINDOWS
2073 qf_update_buffer(qi);
2074 #endif
2078 * Free error list "idx".
2080 static void
2081 qf_free(qi, idx)
2082 qf_info_T *qi;
2083 int idx;
2085 qfline_T *qfp;
2087 while (qi->qf_lists[idx].qf_count)
2089 qfp = qi->qf_lists[idx].qf_start->qf_next;
2090 vim_free(qi->qf_lists[idx].qf_start->qf_text);
2091 vim_free(qi->qf_lists[idx].qf_start->qf_pattern);
2092 vim_free(qi->qf_lists[idx].qf_start);
2093 qi->qf_lists[idx].qf_start = qfp;
2094 --qi->qf_lists[idx].qf_count;
2099 * qf_mark_adjust: adjust marks
2101 void
2102 qf_mark_adjust(wp, line1, line2, amount, amount_after)
2103 win_T *wp;
2104 linenr_T line1;
2105 linenr_T line2;
2106 long amount;
2107 long amount_after;
2109 int i;
2110 qfline_T *qfp;
2111 int idx;
2112 qf_info_T *qi = &ql_info;
2114 if (wp != NULL)
2116 if (wp->w_llist == NULL)
2117 return;
2118 qi = wp->w_llist;
2121 for (idx = 0; idx < qi->qf_listcount; ++idx)
2122 if (qi->qf_lists[idx].qf_count)
2123 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
2124 i < qi->qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
2125 if (qfp->qf_fnum == curbuf->b_fnum)
2127 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2129 if (amount == MAXLNUM)
2130 qfp->qf_cleared = TRUE;
2131 else
2132 qfp->qf_lnum += amount;
2134 else if (amount_after && qfp->qf_lnum > line2)
2135 qfp->qf_lnum += amount_after;
2140 * Make a nice message out of the error character and the error number:
2141 * char number message
2142 * e or E 0 " error"
2143 * w or W 0 " warning"
2144 * i or I 0 " info"
2145 * 0 0 ""
2146 * other 0 " c"
2147 * e or E n " error n"
2148 * w or W n " warning n"
2149 * i or I n " info n"
2150 * 0 n " error n"
2151 * other n " c n"
2152 * 1 x "" :helpgrep
2154 static char_u *
2155 qf_types(c, nr)
2156 int c, nr;
2158 static char_u buf[20];
2159 static char_u cc[3];
2160 char_u *p;
2162 if (c == 'W' || c == 'w')
2163 p = (char_u *)" warning";
2164 else if (c == 'I' || c == 'i')
2165 p = (char_u *)" info";
2166 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2167 p = (char_u *)" error";
2168 else if (c == 0 || c == 1)
2169 p = (char_u *)"";
2170 else
2172 cc[0] = ' ';
2173 cc[1] = c;
2174 cc[2] = NUL;
2175 p = cc;
2178 if (nr <= 0)
2179 return p;
2181 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2182 return buf;
2185 #if defined(FEAT_WINDOWS) || defined(PROTO)
2187 * ":cwindow": open the quickfix window if we have errors to display,
2188 * close it if not.
2189 * ":lwindow": open the location list window if we have locations to display,
2190 * close it if not.
2192 void
2193 ex_cwindow(eap)
2194 exarg_T *eap;
2196 qf_info_T *qi = &ql_info;
2197 win_T *win;
2199 if (eap->cmdidx == CMD_lwindow)
2201 qi = GET_LOC_LIST(curwin);
2202 if (qi == NULL)
2203 return;
2206 /* Look for an existing quickfix window. */
2207 win = qf_find_win(qi);
2210 * If a quickfix window is open but we have no errors to display,
2211 * close the window. If a quickfix window is not open, then open
2212 * it if we have errors; otherwise, leave it closed.
2214 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
2215 || qi->qf_curlist >= qi->qf_listcount)
2217 if (win != NULL)
2218 ex_cclose(eap);
2220 else if (win == NULL)
2221 ex_copen(eap);
2225 * ":cclose": close the window showing the list of errors.
2226 * ":lclose": close the window showing the location list
2228 /*ARGSUSED*/
2229 void
2230 ex_cclose(eap)
2231 exarg_T *eap;
2233 win_T *win = NULL;
2234 qf_info_T *qi = &ql_info;
2236 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2238 qi = GET_LOC_LIST(curwin);
2239 if (qi == NULL)
2240 return;
2243 /* Find existing quickfix window and close it. */
2244 win = qf_find_win(qi);
2245 if (win != NULL)
2246 win_close(win, FALSE);
2250 * ":copen": open a window that shows the list of errors.
2251 * ":lopen": open a window that shows the location list.
2253 void
2254 ex_copen(eap)
2255 exarg_T *eap;
2257 qf_info_T *qi = &ql_info;
2258 int height;
2259 win_T *win;
2260 tabpage_T *prevtab = curtab;
2261 buf_T *qf_buf;
2263 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2265 qi = GET_LOC_LIST(curwin);
2266 if (qi == NULL)
2268 EMSG(_(e_loclist));
2269 return;
2273 if (eap->addr_count != 0)
2274 height = eap->line2;
2275 else
2276 height = QF_WINHEIGHT;
2278 #ifdef FEAT_VISUAL
2279 reset_VIsual_and_resel(); /* stop Visual mode */
2280 #endif
2281 #ifdef FEAT_GUI
2282 need_mouse_correct = TRUE;
2283 #endif
2286 * Find existing quickfix window, or open a new one.
2288 win = qf_find_win(qi);
2290 if (win != NULL && cmdmod.tab == 0)
2291 win_goto(win);
2292 else
2294 qf_buf = qf_find_buf(qi);
2296 /* The current window becomes the previous window afterwards. */
2297 win = curwin;
2299 if (eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2300 /* Create the new window at the very bottom. */
2301 win_goto(lastwin);
2302 if (win_split(height, WSP_BELOW) == FAIL)
2303 return; /* not enough room for window */
2304 #ifdef FEAT_SCROLLBIND
2305 curwin->w_p_scb = FALSE;
2306 #endif
2308 /* Remove the location list for the quickfix window */
2309 qf_free_all(curwin);
2311 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2314 * For the location list window, create a reference to the
2315 * location list from the window 'win'.
2317 curwin->w_llist_ref = win->w_llist;
2318 win->w_llist->qf_refcount++;
2321 if (qf_buf != NULL)
2322 /* Use the existing quickfix buffer */
2323 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
2324 ECMD_HIDE + ECMD_OLDBUF);
2325 else
2327 /* Create a new quickfix buffer */
2328 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
2329 /* switch off 'swapfile' */
2330 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2331 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
2332 OPT_LOCAL);
2333 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
2334 set_option_value((char_u *)"diff", 0L, NULL, OPT_LOCAL);
2337 /* Only set the height when still in the same tab page and there is no
2338 * window to the side. */
2339 if (curtab == prevtab
2340 #ifdef FEAT_VERTSPLIT
2341 && curwin->w_width == Columns
2342 #endif
2344 win_setheight(height);
2345 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2346 if (win_valid(win))
2347 prevwin = win;
2351 * Fill the buffer with the quickfix list.
2353 qf_fill_buffer(qi);
2355 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
2356 curwin->w_cursor.col = 0;
2357 check_cursor();
2358 update_topline(); /* scroll to show the line */
2362 * Return the number of the current entry (line number in the quickfix
2363 * window).
2365 linenr_T
2366 qf_current_entry(wp)
2367 win_T *wp;
2369 qf_info_T *qi = &ql_info;
2371 if (IS_LL_WINDOW(wp))
2372 /* In the location list window, use the referenced location list */
2373 qi = wp->w_llist_ref;
2375 return qi->qf_lists[qi->qf_curlist].qf_index;
2379 * Update the cursor position in the quickfix window to the current error.
2380 * Return TRUE if there is a quickfix window.
2382 static int
2383 qf_win_pos_update(qi, old_qf_index)
2384 qf_info_T *qi;
2385 int old_qf_index; /* previous qf_index or zero */
2387 win_T *win;
2388 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
2391 * Put the cursor on the current error in the quickfix window, so that
2392 * it's viewable.
2394 win = qf_find_win(qi);
2395 if (win != NULL
2396 && qf_index <= win->w_buffer->b_ml.ml_line_count
2397 && old_qf_index != qf_index)
2399 win_T *old_curwin = curwin;
2401 curwin = win;
2402 curbuf = win->w_buffer;
2403 if (qf_index > old_qf_index)
2405 curwin->w_redraw_top = old_qf_index;
2406 curwin->w_redraw_bot = qf_index;
2408 else
2410 curwin->w_redraw_top = qf_index;
2411 curwin->w_redraw_bot = old_qf_index;
2413 curwin->w_cursor.lnum = qf_index;
2414 curwin->w_cursor.col = 0;
2415 update_topline(); /* scroll to show the line */
2416 redraw_later(VALID);
2417 curwin->w_redr_status = TRUE; /* update ruler */
2418 curwin = old_curwin;
2419 curbuf = curwin->w_buffer;
2421 return win != NULL;
2425 * Check whether the given window is displaying the specified quickfix/location
2426 * list buffer
2428 static int
2429 is_qf_win(win, qi)
2430 win_T *win;
2431 qf_info_T *qi;
2434 * A window displaying the quickfix buffer will have the w_llist_ref field
2435 * set to NULL.
2436 * A window displaying a location list buffer will have the w_llist_ref
2437 * pointing to the location list.
2439 if (bt_quickfix(win->w_buffer))
2440 if ((qi == &ql_info && win->w_llist_ref == NULL)
2441 || (qi != &ql_info && win->w_llist_ref == qi))
2442 return TRUE;
2444 return FALSE;
2448 * Find a window displaying the quickfix/location list 'qi'
2449 * Searches in only the windows opened in the current tab.
2451 static win_T *
2452 qf_find_win(qi)
2453 qf_info_T *qi;
2455 win_T *win;
2457 FOR_ALL_WINDOWS(win)
2458 if (is_qf_win(win, qi))
2459 break;
2461 return win;
2465 * Find a quickfix buffer.
2466 * Searches in windows opened in all the tabs.
2468 static buf_T *
2469 qf_find_buf(qi)
2470 qf_info_T *qi;
2472 tabpage_T *tp;
2473 win_T *win;
2475 FOR_ALL_TAB_WINDOWS(tp, win)
2476 if (is_qf_win(win, qi))
2477 return win->w_buffer;
2479 return NULL;
2483 * Find the quickfix buffer. If it exists, update the contents.
2485 static void
2486 qf_update_buffer(qi)
2487 qf_info_T *qi;
2489 buf_T *buf;
2490 aco_save_T aco;
2492 /* Check if a buffer for the quickfix list exists. Update it. */
2493 buf = qf_find_buf(qi);
2494 if (buf != NULL)
2496 /* set curwin/curbuf to buf and save a few things */
2497 aucmd_prepbuf(&aco, buf);
2499 qf_fill_buffer(qi);
2501 /* restore curwin/curbuf and a few other things */
2502 aucmd_restbuf(&aco);
2504 (void)qf_win_pos_update(qi, 0);
2509 * Fill current buffer with quickfix errors, replacing any previous contents.
2510 * curbuf must be the quickfix buffer!
2512 static void
2513 qf_fill_buffer(qi)
2514 qf_info_T *qi;
2516 linenr_T lnum;
2517 qfline_T *qfp;
2518 buf_T *errbuf;
2519 int len;
2520 int old_KeyTyped = KeyTyped;
2522 /* delete all existing lines */
2523 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2524 (void)ml_delete((linenr_T)1, FALSE);
2526 /* Check if there is anything to display */
2527 if (qi->qf_curlist < qi->qf_listcount)
2529 /* Add one line for each error */
2530 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2531 for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
2533 if (qfp->qf_fnum != 0
2534 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2535 && errbuf->b_fname != NULL)
2537 if (qfp->qf_type == 1) /* :helpgrep */
2538 STRCPY(IObuff, gettail(errbuf->b_fname));
2539 else
2540 STRCPY(IObuff, errbuf->b_fname);
2541 len = (int)STRLEN(IObuff);
2543 else
2544 len = 0;
2545 IObuff[len++] = '|';
2547 if (qfp->qf_lnum > 0)
2549 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2550 len += (int)STRLEN(IObuff + len);
2552 if (qfp->qf_col > 0)
2554 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2555 len += (int)STRLEN(IObuff + len);
2558 sprintf((char *)IObuff + len, "%s",
2559 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2560 len += (int)STRLEN(IObuff + len);
2562 else if (qfp->qf_pattern != NULL)
2564 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2565 len += (int)STRLEN(IObuff + len);
2567 IObuff[len++] = '|';
2568 IObuff[len++] = ' ';
2570 /* Remove newlines and leading whitespace from the text.
2571 * For an unrecognized line keep the indent, the compiler may
2572 * mark a word with ^^^^. */
2573 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2574 IObuff + len, IOSIZE - len);
2576 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2577 == FAIL)
2578 break;
2579 qfp = qfp->qf_next;
2581 /* Delete the empty line which is now at the end */
2582 (void)ml_delete(lnum + 1, FALSE);
2585 /* correct cursor position */
2586 check_lnums(TRUE);
2588 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2589 * resembles reading a file into a buffer, it's more logical when using
2590 * autocommands. */
2591 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2592 curbuf->b_p_ma = FALSE;
2594 #ifdef FEAT_AUTOCMD
2595 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2596 FALSE, curbuf);
2597 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2598 FALSE, curbuf);
2599 #endif
2601 /* make sure it will be redrawn */
2602 redraw_curbuf_later(NOT_VALID);
2604 /* Restore KeyTyped, setting 'filetype' may reset it. */
2605 KeyTyped = old_KeyTyped;
2608 #endif /* FEAT_WINDOWS */
2611 * Return TRUE if "buf" is the quickfix buffer.
2614 bt_quickfix(buf)
2615 buf_T *buf;
2617 return (buf->b_p_bt[0] == 'q');
2621 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2622 * This means the buffer name is not a file name.
2625 bt_nofile(buf)
2626 buf_T *buf;
2628 return (buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2629 || buf->b_p_bt[0] == 'a';
2633 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2636 bt_dontwrite(buf)
2637 buf_T *buf;
2639 return (buf->b_p_bt[0] == 'n');
2643 bt_dontwrite_msg(buf)
2644 buf_T *buf;
2646 if (bt_dontwrite(buf))
2648 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2649 return TRUE;
2651 return FALSE;
2655 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2656 * and 'bufhidden'.
2659 buf_hide(buf)
2660 buf_T *buf;
2662 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2663 switch (buf->b_p_bh[0])
2665 case 'u': /* "unload" */
2666 case 'w': /* "wipe" */
2667 case 'd': return FALSE; /* "delete" */
2668 case 'h': return TRUE; /* "hide" */
2670 return (p_hid || cmdmod.hide);
2674 * Return TRUE when using ":vimgrep" for ":grep".
2677 grep_internal(cmdidx)
2678 cmdidx_T cmdidx;
2680 return ((cmdidx == CMD_grep
2681 || cmdidx == CMD_lgrep
2682 || cmdidx == CMD_grepadd
2683 || cmdidx == CMD_lgrepadd)
2684 && STRCMP("internal",
2685 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2689 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
2691 void
2692 ex_make(eap)
2693 exarg_T *eap;
2695 char_u *fname;
2696 char_u *cmd;
2697 unsigned len;
2698 win_T *wp = NULL;
2699 qf_info_T *qi = &ql_info;
2700 int res;
2701 #ifdef FEAT_AUTOCMD
2702 char_u *au_name = NULL;
2704 switch (eap->cmdidx)
2706 case CMD_make: au_name = (char_u *)"make"; break;
2707 case CMD_lmake: au_name = (char_u *)"lmake"; break;
2708 case CMD_grep: au_name = (char_u *)"grep"; break;
2709 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
2710 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
2711 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
2712 default: break;
2714 if (au_name != NULL)
2716 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2717 curbuf->b_fname, TRUE, curbuf);
2718 # ifdef FEAT_EVAL
2719 if (did_throw || force_abort)
2720 return;
2721 # endif
2723 #endif
2725 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
2726 if (grep_internal(eap->cmdidx))
2728 ex_vimgrep(eap);
2729 return;
2732 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
2733 || eap->cmdidx == CMD_lgrepadd)
2734 wp = curwin;
2736 autowrite_all();
2737 fname = get_mef_name();
2738 if (fname == NULL)
2739 return;
2740 mch_remove(fname); /* in case it's not unique */
2743 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2745 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2746 if (*p_sp != NUL)
2747 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
2748 cmd = alloc(len);
2749 if (cmd == NULL)
2750 return;
2751 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2752 (char *)p_shq);
2753 if (*p_sp != NUL)
2754 append_redir(cmd, p_sp, fname);
2756 * Output a newline if there's something else than the :make command that
2757 * was typed (in which case the cursor is in column 0).
2759 if (msg_col == 0)
2760 msg_didout = FALSE;
2761 msg_start();
2762 MSG_PUTS(":!");
2763 msg_outtrans(cmd); /* show what we are doing */
2765 /* let the shell know if we are redirecting output or not */
2766 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2768 #ifdef AMIGA
2769 out_flush();
2770 /* read window status report and redraw before message */
2771 (void)char_avail();
2772 #endif
2774 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
2775 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
2776 (eap->cmdidx != CMD_grepadd
2777 && eap->cmdidx != CMD_lgrepadd));
2778 #ifdef FEAT_AUTOCMD
2779 if (au_name != NULL)
2780 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2781 curbuf->b_fname, TRUE, curbuf);
2782 #endif
2783 if (res > 0 && !eap->forceit)
2785 if (wp != NULL)
2786 qi = GET_LOC_LIST(wp);
2787 qf_jump(qi, 0, 0, FALSE); /* display first error */
2790 mch_remove(fname);
2791 vim_free(fname);
2792 vim_free(cmd);
2796 * Return the name for the errorfile, in allocated memory.
2797 * Find a new unique name when 'makeef' contains "##".
2798 * Returns NULL for error.
2800 static char_u *
2801 get_mef_name()
2803 char_u *p;
2804 char_u *name;
2805 static int start = -1;
2806 static int off = 0;
2807 #ifdef HAVE_LSTAT
2808 struct stat sb;
2809 #endif
2811 if (*p_mef == NUL)
2813 name = vim_tempname('e');
2814 if (name == NULL)
2815 EMSG(_(e_notmp));
2816 return name;
2819 for (p = p_mef; *p; ++p)
2820 if (p[0] == '#' && p[1] == '#')
2821 break;
2823 if (*p == NUL)
2824 return vim_strsave(p_mef);
2826 /* Keep trying until the name doesn't exist yet. */
2827 for (;;)
2829 if (start == -1)
2830 start = mch_get_pid();
2831 else
2832 off += 19;
2834 name = alloc((unsigned)STRLEN(p_mef) + 30);
2835 if (name == NULL)
2836 break;
2837 STRCPY(name, p_mef);
2838 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2839 STRCAT(name, p + 2);
2840 if (mch_getperm(name) < 0
2841 #ifdef HAVE_LSTAT
2842 /* Don't accept a symbolic link, its a security risk. */
2843 && mch_lstat((char *)name, &sb) < 0
2844 #endif
2846 break;
2847 vim_free(name);
2849 return name;
2853 * ":cc", ":crewind", ":cfirst" and ":clast".
2854 * ":ll", ":lrewind", ":lfirst" and ":llast".
2856 void
2857 ex_cc(eap)
2858 exarg_T *eap;
2860 qf_info_T *qi = &ql_info;
2862 if (eap->cmdidx == CMD_ll
2863 || eap->cmdidx == CMD_lrewind
2864 || eap->cmdidx == CMD_lfirst
2865 || eap->cmdidx == CMD_llast)
2867 qi = GET_LOC_LIST(curwin);
2868 if (qi == NULL)
2870 EMSG(_(e_loclist));
2871 return;
2875 qf_jump(qi, 0,
2876 eap->addr_count > 0
2877 ? (int)eap->line2
2878 : (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
2880 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
2881 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
2883 : 32767,
2884 eap->forceit);
2888 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
2889 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
2891 void
2892 ex_cnext(eap)
2893 exarg_T *eap;
2895 qf_info_T *qi = &ql_info;
2897 if (eap->cmdidx == CMD_lnext
2898 || eap->cmdidx == CMD_lNext
2899 || eap->cmdidx == CMD_lprevious
2900 || eap->cmdidx == CMD_lnfile
2901 || eap->cmdidx == CMD_lNfile
2902 || eap->cmdidx == CMD_lpfile)
2904 qi = GET_LOC_LIST(curwin);
2905 if (qi == NULL)
2907 EMSG(_(e_loclist));
2908 return;
2912 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext)
2913 ? FORWARD
2914 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile)
2915 ? FORWARD_FILE
2916 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
2917 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
2918 ? BACKWARD_FILE
2919 : BACKWARD,
2920 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
2924 * ":cfile"/":cgetfile"/":caddfile" commands.
2925 * ":lfile"/":lgetfile"/":laddfile" commands.
2927 void
2928 ex_cfile(eap)
2929 exarg_T *eap;
2931 win_T *wp = NULL;
2932 qf_info_T *qi = &ql_info;
2934 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
2935 || eap->cmdidx == CMD_laddfile)
2936 wp = curwin;
2938 if (*eap->arg != NUL)
2939 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
2942 * This function is used by the :cfile, :cgetfile and :caddfile
2943 * commands.
2944 * :cfile always creates a new quickfix list and jumps to the
2945 * first error.
2946 * :cgetfile creates a new quickfix list but doesn't jump to the
2947 * first error.
2948 * :caddfile adds to an existing quickfix list. If there is no
2949 * quickfix list then a new list is created.
2951 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
2952 && eap->cmdidx != CMD_laddfile)) > 0
2953 && (eap->cmdidx == CMD_cfile
2954 || eap->cmdidx == CMD_lfile))
2956 if (wp != NULL)
2957 qi = GET_LOC_LIST(wp);
2958 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
2963 * ":vimgrep {pattern} file(s)"
2964 * ":vimgrepadd {pattern} file(s)"
2965 * ":lvimgrep {pattern} file(s)"
2966 * ":lvimgrepadd {pattern} file(s)"
2968 void
2969 ex_vimgrep(eap)
2970 exarg_T *eap;
2972 regmmatch_T regmatch;
2973 int fcount;
2974 char_u **fnames;
2975 char_u *fname;
2976 char_u *s;
2977 char_u *p;
2978 int fi;
2979 qf_info_T *qi = &ql_info;
2980 qfline_T *prevp = NULL;
2981 long lnum;
2982 buf_T *buf;
2983 int duplicate_name = FALSE;
2984 int using_dummy;
2985 int redraw_for_dummy = FALSE;
2986 int found_match;
2987 buf_T *first_match_buf = NULL;
2988 time_t seconds = 0;
2989 int save_mls;
2990 #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2991 char_u *save_ei = NULL;
2992 #endif
2993 aco_save_T aco;
2994 #ifdef FEAT_AUTOCMD
2995 char_u *au_name = NULL;
2996 int flags = 0;
2997 colnr_T col;
2998 long tomatch;
2999 char_u dirname_start[MAXPATHL];
3000 char_u dirname_now[MAXPATHL];
3001 char_u *target_dir = NULL;
3003 switch (eap->cmdidx)
3005 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3006 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3007 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
3008 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
3009 default: break;
3011 if (au_name != NULL)
3013 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3014 curbuf->b_fname, TRUE, curbuf);
3015 if (did_throw || force_abort)
3016 return;
3018 #endif
3020 if (eap->cmdidx == CMD_lgrep
3021 || eap->cmdidx == CMD_lvimgrep
3022 || eap->cmdidx == CMD_lgrepadd
3023 || eap->cmdidx == CMD_lvimgrepadd)
3025 qi = ll_get_or_alloc_list(curwin);
3026 if (qi == NULL)
3027 return;
3030 if (eap->addr_count > 0)
3031 tomatch = eap->line2;
3032 else
3033 tomatch = MAXLNUM;
3035 /* Get the search pattern: either white-separated or enclosed in // */
3036 regmatch.regprog = NULL;
3037 p = skip_vimgrep_pat(eap->arg, &s, &flags);
3038 if (p == NULL)
3040 EMSG(_(e_invalpat));
3041 goto theend;
3043 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
3044 if (regmatch.regprog == NULL)
3045 goto theend;
3046 regmatch.rmm_ic = p_ic;
3047 regmatch.rmm_maxcol = 0;
3049 p = skipwhite(p);
3050 if (*p == NUL)
3052 EMSG(_("E683: File name missing or invalid pattern"));
3053 goto theend;
3056 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
3057 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
3058 || qi->qf_curlist == qi->qf_listcount)
3059 /* make place for a new list */
3060 qf_new_list(qi);
3061 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
3062 /* Adding to existing list, find last entry. */
3063 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
3064 prevp->qf_next != prevp; prevp = prevp->qf_next)
3067 /* parse the list of arguments */
3068 if (get_arglist_exp(p, &fcount, &fnames) == FAIL)
3069 goto theend;
3070 if (fcount == 0)
3072 EMSG(_(e_nomatch));
3073 goto theend;
3076 /* Remember the current directory, because a BufRead autocommand that does
3077 * ":lcd %:p:h" changes the meaning of short path names. */
3078 mch_dirname(dirname_start, MAXPATHL);
3080 seconds = (time_t)0;
3081 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
3083 fname = shorten_fname1(fnames[fi]);
3084 if (time(NULL) > seconds)
3086 /* Display the file name every second or so, show the user we are
3087 * working on it. */
3088 seconds = time(NULL);
3089 msg_start();
3090 p = msg_strtrunc(fname, TRUE);
3091 if (p == NULL)
3092 msg_outtrans(fname);
3093 else
3095 msg_outtrans(p);
3096 vim_free(p);
3098 msg_clr_eos();
3099 msg_didout = FALSE; /* overwrite this message */
3100 msg_nowait = TRUE; /* don't wait for this message */
3101 msg_col = 0;
3102 out_flush();
3105 buf = buflist_findname_exp(fnames[fi]);
3106 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3108 /* Remember that a buffer with this name already exists. */
3109 duplicate_name = (buf != NULL);
3110 using_dummy = TRUE;
3111 redraw_for_dummy = TRUE;
3113 #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3114 /* Don't do Filetype autocommands to avoid loading syntax and
3115 * indent scripts, a great speed improvement. */
3116 save_ei = au_event_disable(",Filetype");
3117 #endif
3118 /* Don't use modelines here, it's useless. */
3119 save_mls = p_mls;
3120 p_mls = 0;
3122 /* Load file into a buffer, so that 'fileencoding' is detected,
3123 * autocommands applied, etc. */
3124 buf = load_dummy_buffer(fname);
3126 /* When autocommands changed directory: go back. We assume it was
3127 * ":lcd %:p:h". */
3128 mch_dirname(dirname_now, MAXPATHL);
3129 if (STRCMP(dirname_start, dirname_now) != 0)
3131 exarg_T ea;
3133 ea.arg = dirname_start;
3134 ea.cmdidx = CMD_lcd;
3135 ex_cd(&ea);
3138 p_mls = save_mls;
3139 #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3140 au_event_restore(save_ei);
3141 #endif
3143 else
3144 /* Use existing, loaded buffer. */
3145 using_dummy = FALSE;
3147 if (buf == NULL)
3149 if (!got_int)
3150 smsg((char_u *)_("Cannot open file \"%s\""), fname);
3152 else
3154 /* Try for a match in all lines of the buffer.
3155 * For ":1vimgrep" look for first match only. */
3156 found_match = FALSE;
3157 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3158 ++lnum)
3160 col = 0;
3161 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
3162 col) > 0)
3165 if (qf_add_entry(qi, &prevp,
3166 NULL, /* dir */
3167 fname,
3169 ml_get_buf(buf,
3170 regmatch.startpos[0].lnum + lnum, FALSE),
3171 regmatch.startpos[0].lnum + lnum,
3172 regmatch.startpos[0].col + 1,
3173 FALSE, /* vis_col */
3174 NULL, /* search pattern */
3175 0, /* nr */
3176 0, /* type */
3177 TRUE /* valid */
3178 ) == FAIL)
3180 got_int = TRUE;
3181 break;
3183 found_match = TRUE;
3184 if (--tomatch == 0)
3185 break;
3186 if ((flags & VGR_GLOBAL) == 0
3187 || regmatch.endpos[0].lnum > 0)
3188 break;
3189 col = regmatch.endpos[0].col
3190 + (col == regmatch.endpos[0].col);
3191 if (col > STRLEN(ml_get_buf(buf, lnum, FALSE)))
3192 break;
3194 line_breakcheck();
3195 if (got_int)
3196 break;
3199 if (using_dummy)
3201 if (found_match && first_match_buf == NULL)
3202 first_match_buf = buf;
3203 if (duplicate_name)
3205 /* Never keep a dummy buffer if there is another buffer
3206 * with the same name. */
3207 wipe_dummy_buffer(buf);
3208 buf = NULL;
3210 else if (!cmdmod.hide
3211 || buf->b_p_bh[0] == 'u' /* "unload" */
3212 || buf->b_p_bh[0] == 'w' /* "wipe" */
3213 || buf->b_p_bh[0] == 'd') /* "delete" */
3215 /* When no match was found we don't need to remember the
3216 * buffer, wipe it out. If there was a match and it
3217 * wasn't the first one or we won't jump there: only
3218 * unload the buffer.
3219 * Ignore 'hidden' here, because it may lead to having too
3220 * many swap files. */
3221 if (!found_match)
3223 wipe_dummy_buffer(buf);
3224 buf = NULL;
3226 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
3228 unload_dummy_buffer(buf);
3229 buf = NULL;
3233 if (buf != NULL)
3235 /* If the buffer is still loaded we need to use the
3236 * directory we jumped to below. */
3237 if (buf == first_match_buf
3238 && target_dir == NULL
3239 && STRCMP(dirname_start, dirname_now) != 0)
3240 target_dir = vim_strsave(dirname_now);
3242 /* The buffer is still loaded, the Filetype autocommands
3243 * need to be done now, in that buffer. And the modelines
3244 * need to be done (again). But not the window-local
3245 * options! */
3246 aucmd_prepbuf(&aco, buf);
3247 #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3248 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3249 buf->b_fname, TRUE, buf);
3250 #endif
3251 do_modelines(OPT_NOWIN);
3252 aucmd_restbuf(&aco);
3258 FreeWild(fcount, fnames);
3260 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3261 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3262 qi->qf_lists[qi->qf_curlist].qf_index = 1;
3264 #ifdef FEAT_WINDOWS
3265 qf_update_buffer(qi);
3266 #endif
3268 #ifdef FEAT_AUTOCMD
3269 if (au_name != NULL)
3270 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3271 curbuf->b_fname, TRUE, curbuf);
3272 #endif
3274 /* Jump to first match. */
3275 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
3277 if ((flags & VGR_NOJUMP) == 0)
3279 buf = curbuf;
3280 qf_jump(qi, 0, 0, eap->forceit);
3281 if (buf != curbuf)
3282 /* If we jumped to another buffer redrawing will already be
3283 * taken care of. */
3284 redraw_for_dummy = FALSE;
3286 /* Jump to the directory used after loading the buffer. */
3287 if (curbuf == first_match_buf && target_dir != NULL)
3289 exarg_T ea;
3291 ea.arg = target_dir;
3292 ea.cmdidx = CMD_lcd;
3293 ex_cd(&ea);
3297 else
3298 EMSG2(_(e_nomatch2), s);
3300 /* If we loaded a dummy buffer into the current window, the autocommands
3301 * may have messed up things, need to redraw and recompute folds. */
3302 if (redraw_for_dummy)
3304 #ifdef FEAT_FOLDING
3305 foldUpdateAll(curwin);
3306 #else
3307 redraw_later(NOT_VALID);
3308 #endif
3311 theend:
3312 vim_free(target_dir);
3313 vim_free(regmatch.regprog);
3317 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
3318 * Put the start of the pattern in "*s", unless "s" is NULL.
3319 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3320 * If "s" is not NULL terminate the pattern with a NUL.
3321 * Return a pointer to the char just past the pattern plus flags.
3323 char_u *
3324 skip_vimgrep_pat(p, s, flags)
3325 char_u *p;
3326 char_u **s;
3327 int *flags;
3329 int c;
3331 if (vim_isIDc(*p))
3333 /* ":vimgrep pattern fname" */
3334 if (s != NULL)
3335 *s = p;
3336 p = skiptowhite(p);
3337 if (s != NULL && *p != NUL)
3338 *p++ = NUL;
3340 else
3342 /* ":vimgrep /pattern/[g][j] fname" */
3343 if (s != NULL)
3344 *s = p + 1;
3345 c = *p;
3346 p = skip_regexp(p + 1, c, TRUE, NULL);
3347 if (*p != c)
3348 return NULL;
3350 /* Truncate the pattern. */
3351 if (s != NULL)
3352 *p = NUL;
3353 ++p;
3355 /* Find the flags */
3356 while (*p == 'g' || *p == 'j')
3358 if (flags != NULL)
3360 if (*p == 'g')
3361 *flags |= VGR_GLOBAL;
3362 else
3363 *flags |= VGR_NOJUMP;
3365 ++p;
3368 return p;
3372 * Load file "fname" into a dummy buffer and return the buffer pointer.
3373 * Returns NULL if it fails.
3374 * Must call unload_dummy_buffer() or wipe_dummy_buffer() later!
3376 static buf_T *
3377 load_dummy_buffer(fname)
3378 char_u *fname;
3380 buf_T *newbuf;
3381 int failed = TRUE;
3382 aco_save_T aco;
3384 /* Allocate a buffer without putting it in the buffer list. */
3385 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
3386 if (newbuf == NULL)
3387 return NULL;
3389 /* Init the options. */
3390 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
3392 /* set curwin/curbuf to buf and save a few things */
3393 aucmd_prepbuf(&aco, newbuf);
3395 /* Need to set the filename for autocommands. */
3396 (void)setfname(curbuf, fname, NULL, FALSE);
3398 if (ml_open(curbuf) == OK)
3400 /* Create swap file now to avoid the ATTENTION message. */
3401 check_need_swap(TRUE);
3403 /* Remove the "dummy" flag, otherwise autocommands may not
3404 * work. */
3405 curbuf->b_flags &= ~BF_DUMMY;
3407 if (readfile(fname, NULL,
3408 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
3409 NULL, READ_NEW | READ_DUMMY) == OK
3410 && !got_int
3411 && !(curbuf->b_flags & BF_NEW))
3413 failed = FALSE;
3414 if (curbuf != newbuf)
3416 /* Bloody autocommands changed the buffer! */
3417 if (buf_valid(newbuf))
3418 wipe_buffer(newbuf, FALSE);
3419 newbuf = curbuf;
3424 /* restore curwin/curbuf and a few other things */
3425 aucmd_restbuf(&aco);
3427 if (!buf_valid(newbuf))
3428 return NULL;
3429 if (failed)
3431 wipe_dummy_buffer(newbuf);
3432 return NULL;
3434 return newbuf;
3438 * Wipe out the dummy buffer that load_dummy_buffer() created.
3440 static void
3441 wipe_dummy_buffer(buf)
3442 buf_T *buf;
3444 if (curbuf != buf) /* safety check */
3446 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3447 cleanup_T cs;
3449 /* Reset the error/interrupt/exception state here so that aborting()
3450 * returns FALSE when wiping out the buffer. Otherwise it doesn't
3451 * work when got_int is set. */
3452 enter_cleanup(&cs);
3453 #endif
3455 wipe_buffer(buf, FALSE);
3457 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3458 /* Restore the error/interrupt/exception state if not discarded by a
3459 * new aborting error, interrupt, or uncaught exception. */
3460 leave_cleanup(&cs);
3461 #endif
3466 * Unload the dummy buffer that load_dummy_buffer() created.
3468 static void
3469 unload_dummy_buffer(buf)
3470 buf_T *buf;
3472 if (curbuf != buf) /* safety check */
3473 close_buffer(NULL, buf, DOBUF_UNLOAD);
3476 #if defined(FEAT_EVAL) || defined(PROTO)
3478 * Add each quickfix error to list "list" as a dictionary.
3481 get_errorlist(wp, list)
3482 win_T *wp;
3483 list_T *list;
3485 qf_info_T *qi = &ql_info;
3486 dict_T *dict;
3487 char_u buf[2];
3488 qfline_T *qfp;
3489 int i;
3490 int bufnum;
3492 if (wp != NULL)
3494 qi = GET_LOC_LIST(wp);
3495 if (qi == NULL)
3496 return FAIL;
3499 if (qi->qf_curlist >= qi->qf_listcount
3500 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
3501 return FAIL;
3503 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3504 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
3506 /* Handle entries with a non-existing buffer number. */
3507 bufnum = qfp->qf_fnum;
3508 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3509 bufnum = 0;
3511 if ((dict = dict_alloc()) == NULL)
3512 return FAIL;
3513 if (list_append_dict(list, dict) == FAIL)
3514 return FAIL;
3516 buf[0] = qfp->qf_type;
3517 buf[1] = NUL;
3518 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
3519 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
3520 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
3521 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
3522 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
3523 || dict_add_nr_str(dict, "pattern", 0L,
3524 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
3525 || dict_add_nr_str(dict, "text", 0L,
3526 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
3527 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
3528 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
3529 return FAIL;
3531 qfp = qfp->qf_next;
3533 return OK;
3537 * Populate the quickfix list with the items supplied in the list
3538 * of dictionaries.
3541 set_errorlist(wp, list, action)
3542 win_T *wp;
3543 list_T *list;
3544 int action;
3546 listitem_T *li;
3547 dict_T *d;
3548 char_u *filename, *pattern, *text, *type;
3549 int bufnum;
3550 long lnum;
3551 int col, nr;
3552 int vcol;
3553 qfline_T *prevp = NULL;
3554 int valid, status;
3555 int retval = OK;
3556 qf_info_T *qi = &ql_info;
3557 int did_bufnr_emsg = FALSE;
3559 if (wp != NULL)
3561 qi = ll_get_or_alloc_list(wp);
3562 if (qi == NULL)
3563 return FAIL;
3566 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
3567 /* make place for a new list */
3568 qf_new_list(qi);
3569 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
3570 /* Adding to existing list, find last entry. */
3571 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
3572 prevp->qf_next != prevp; prevp = prevp->qf_next)
3574 else if (action == 'r')
3575 qf_free(qi, qi->qf_curlist);
3577 for (li = list->lv_first; li != NULL; li = li->li_next)
3579 if (li->li_tv.v_type != VAR_DICT)
3580 continue; /* Skip non-dict items */
3582 d = li->li_tv.vval.v_dict;
3583 if (d == NULL)
3584 continue;
3586 filename = get_dict_string(d, (char_u *)"filename", TRUE);
3587 bufnum = get_dict_number(d, (char_u *)"bufnr");
3588 lnum = get_dict_number(d, (char_u *)"lnum");
3589 col = get_dict_number(d, (char_u *)"col");
3590 vcol = get_dict_number(d, (char_u *)"vcol");
3591 nr = get_dict_number(d, (char_u *)"nr");
3592 type = get_dict_string(d, (char_u *)"type", TRUE);
3593 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
3594 text = get_dict_string(d, (char_u *)"text", TRUE);
3595 if (text == NULL)
3596 text = vim_strsave((char_u *)"");
3598 valid = TRUE;
3599 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
3600 valid = FALSE;
3602 /* Mark entries with non-existing buffer number as not valid. Give the
3603 * error message only once. */
3604 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3606 if (!did_bufnr_emsg)
3608 did_bufnr_emsg = TRUE;
3609 EMSGN(_("E92: Buffer %ld not found"), bufnum);
3611 valid = FALSE;
3612 bufnum = 0;
3615 status = qf_add_entry(qi, &prevp,
3616 NULL, /* dir */
3617 filename,
3618 bufnum,
3619 text,
3620 lnum,
3621 col,
3622 vcol, /* vis_col */
3623 pattern, /* search pattern */
3625 type == NULL ? NUL : *type,
3626 valid);
3628 vim_free(filename);
3629 vim_free(pattern);
3630 vim_free(text);
3631 vim_free(type);
3633 if (status == FAIL)
3635 retval = FAIL;
3636 break;
3640 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3641 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3642 qi->qf_lists[qi->qf_curlist].qf_index = 1;
3644 #ifdef FEAT_WINDOWS
3645 qf_update_buffer(qi);
3646 #endif
3648 return retval;
3650 #endif
3653 * ":[range]cbuffer [bufnr]" command.
3654 * ":[range]caddbuffer [bufnr]" command.
3655 * ":[range]cgetbuffer [bufnr]" command.
3656 * ":[range]lbuffer [bufnr]" command.
3657 * ":[range]laddbuffer [bufnr]" command.
3658 * ":[range]lgetbuffer [bufnr]" command.
3660 void
3661 ex_cbuffer(eap)
3662 exarg_T *eap;
3664 buf_T *buf = NULL;
3665 qf_info_T *qi = &ql_info;
3667 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
3668 || eap->cmdidx == CMD_laddbuffer)
3670 qi = ll_get_or_alloc_list(curwin);
3671 if (qi == NULL)
3672 return;
3675 if (*eap->arg == NUL)
3676 buf = curbuf;
3677 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
3678 buf = buflist_findnr(atoi((char *)eap->arg));
3679 if (buf == NULL)
3680 EMSG(_(e_invarg));
3681 else if (buf->b_ml.ml_mfp == NULL)
3682 EMSG(_("E681: Buffer is not loaded"));
3683 else
3685 if (eap->addr_count == 0)
3687 eap->line1 = 1;
3688 eap->line2 = buf->b_ml.ml_line_count;
3690 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
3691 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
3692 EMSG(_(e_invrange));
3693 else
3695 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
3696 (eap->cmdidx != CMD_caddbuffer
3697 && eap->cmdidx != CMD_laddbuffer),
3698 eap->line1, eap->line2) > 0
3699 && (eap->cmdidx == CMD_cbuffer
3700 || eap->cmdidx == CMD_lbuffer))
3701 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
3706 #if defined(FEAT_EVAL) || defined(PROTO)
3708 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
3709 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
3711 void
3712 ex_cexpr(eap)
3713 exarg_T *eap;
3715 typval_T *tv;
3716 qf_info_T *qi = &ql_info;
3718 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
3719 || eap->cmdidx == CMD_laddexpr)
3721 qi = ll_get_or_alloc_list(curwin);
3722 if (qi == NULL)
3723 return;
3726 /* Evaluate the expression. When the result is a string or a list we can
3727 * use it to fill the errorlist. */
3728 tv = eval_expr(eap->arg, NULL);
3729 if (tv != NULL)
3731 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
3732 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
3734 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
3735 (eap->cmdidx != CMD_caddexpr
3736 && eap->cmdidx != CMD_laddexpr),
3737 (linenr_T)0, (linenr_T)0) > 0
3738 && (eap->cmdidx == CMD_cexpr
3739 || eap->cmdidx == CMD_lexpr))
3740 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
3742 else
3743 EMSG(_("E777: String or List expected"));
3744 free_tv(tv);
3747 #endif
3750 * ":helpgrep {pattern}"
3752 void
3753 ex_helpgrep(eap)
3754 exarg_T *eap;
3756 regmatch_T regmatch;
3757 char_u *save_cpo;
3758 char_u *p;
3759 int fcount;
3760 char_u **fnames;
3761 FILE *fd;
3762 int fi;
3763 qfline_T *prevp = NULL;
3764 long lnum;
3765 #ifdef FEAT_MULTI_LANG
3766 char_u *lang;
3767 #endif
3768 qf_info_T *qi = &ql_info;
3769 int new_qi = FALSE;
3770 win_T *wp;
3772 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
3773 save_cpo = p_cpo;
3774 p_cpo = (char_u *)"";
3776 #ifdef FEAT_MULTI_LANG
3777 /* Check for a specified language */
3778 lang = check_help_lang(eap->arg);
3779 #endif
3781 if (eap->cmdidx == CMD_lhelpgrep)
3783 /* Find an existing help window */
3784 FOR_ALL_WINDOWS(wp)
3785 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
3786 break;
3788 if (wp == NULL) /* Help window not found */
3789 qi = NULL;
3790 else
3791 qi = wp->w_llist;
3793 if (qi == NULL)
3795 /* Allocate a new location list for help text matches */
3796 if ((qi = ll_new_list()) == NULL)
3797 return;
3798 new_qi = TRUE;
3802 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
3803 regmatch.rm_ic = FALSE;
3804 if (regmatch.regprog != NULL)
3806 /* create a new quickfix list */
3807 qf_new_list(qi);
3809 /* Go through all directories in 'runtimepath' */
3810 p = p_rtp;
3811 while (*p != NUL && !got_int)
3813 copy_option_part(&p, NameBuff, MAXPATHL, ",");
3815 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
3816 add_pathsep(NameBuff);
3817 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
3818 if (gen_expand_wildcards(1, &NameBuff, &fcount,
3819 &fnames, EW_FILE|EW_SILENT) == OK
3820 && fcount > 0)
3822 for (fi = 0; fi < fcount && !got_int; ++fi)
3824 #ifdef FEAT_MULTI_LANG
3825 /* Skip files for a different language. */
3826 if (lang != NULL
3827 && STRNICMP(lang, fnames[fi]
3828 + STRLEN(fnames[fi]) - 3, 2) != 0
3829 && !(STRNICMP(lang, "en", 2) == 0
3830 && STRNICMP("txt", fnames[fi]
3831 + STRLEN(fnames[fi]) - 3, 3) == 0))
3832 continue;
3833 #endif
3834 fd = mch_fopen((char *)fnames[fi], "r");
3835 if (fd != NULL)
3837 lnum = 1;
3838 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
3840 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
3842 int l = (int)STRLEN(IObuff);
3844 /* remove trailing CR, LF, spaces, etc. */
3845 while (l > 0 && IObuff[l - 1] <= ' ')
3846 IObuff[--l] = NUL;
3848 if (qf_add_entry(qi, &prevp,
3849 NULL, /* dir */
3850 fnames[fi],
3852 IObuff,
3853 lnum,
3854 (int)(regmatch.startp[0] - IObuff)
3855 + 1, /* col */
3856 FALSE, /* vis_col */
3857 NULL, /* search pattern */
3858 0, /* nr */
3859 1, /* type */
3860 TRUE /* valid */
3861 ) == FAIL)
3863 got_int = TRUE;
3864 break;
3867 ++lnum;
3868 line_breakcheck();
3870 fclose(fd);
3873 FreeWild(fcount, fnames);
3876 vim_free(regmatch.regprog);
3878 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3879 qi->qf_lists[qi->qf_curlist].qf_ptr =
3880 qi->qf_lists[qi->qf_curlist].qf_start;
3881 qi->qf_lists[qi->qf_curlist].qf_index = 1;
3884 p_cpo = save_cpo;
3886 #ifdef FEAT_WINDOWS
3887 qf_update_buffer(qi);
3888 #endif
3890 /* Jump to first match. */
3891 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
3892 qf_jump(qi, 0, 0, FALSE);
3893 else
3894 EMSG2(_(e_nomatch2), eap->arg);
3896 if (eap->cmdidx == CMD_lhelpgrep)
3898 /* If the help window is not opened or if it already points to the
3899 * correct location list, then free the new location list. */
3900 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
3902 if (new_qi)
3903 ll_free_all(&qi);
3905 else if (curwin->w_llist == NULL)
3906 curwin->w_llist = qi;
3910 #endif /* FEAT_QUICKFIX */