Patch 7.0.070
[MacVim/jjgod.git] / src / quickfix.c
blob2e87373436f220fd4444f70d88fbc49119393a1e
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, 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),
794 errmsg,
795 lnum,
796 col,
797 use_viscol,
798 pattern,
799 enr,
800 type,
801 valid) == FAIL)
802 goto error2;
803 line_breakcheck();
805 if (fd == NULL || !ferror(fd))
807 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
809 /* no valid entry found */
810 qi->qf_lists[qi->qf_curlist].qf_ptr =
811 qi->qf_lists[qi->qf_curlist].qf_start;
812 qi->qf_lists[qi->qf_curlist].qf_index = 1;
813 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
815 else
817 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
818 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
819 qi->qf_lists[qi->qf_curlist].qf_ptr =
820 qi->qf_lists[qi->qf_curlist].qf_start;
822 /* return number of matches */
823 retval = qi->qf_lists[qi->qf_curlist].qf_count;
824 goto qf_init_ok;
826 EMSG(_(e_readerrf));
827 error2:
828 qf_free(qi, qi->qf_curlist);
829 qi->qf_listcount--;
830 if (qi->qf_curlist > 0)
831 --qi->qf_curlist;
832 qf_init_ok:
833 if (fd != NULL)
834 fclose(fd);
835 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
837 fmt_first = fmt_ptr->next;
838 vim_free(fmt_ptr->prog);
839 vim_free(fmt_ptr);
841 qf_clean_dir_stack(&dir_stack);
842 qf_clean_dir_stack(&file_stack);
843 qf_init_end:
844 vim_free(namebuf);
845 vim_free(errmsg);
846 vim_free(pattern);
847 vim_free(fmtstr);
849 #ifdef FEAT_WINDOWS
850 qf_update_buffer(qi);
851 #endif
853 return retval;
857 * Prepare for adding a new quickfix list.
859 static void
860 qf_new_list(qi)
861 qf_info_T *qi;
863 int i;
866 * If the current entry is not the last entry, delete entries below
867 * the current entry. This makes it possible to browse in a tree-like
868 * way with ":grep'.
870 while (qi->qf_listcount > qi->qf_curlist + 1)
871 qf_free(qi, --qi->qf_listcount);
874 * When the stack is full, remove to oldest entry
875 * Otherwise, add a new entry.
877 if (qi->qf_listcount == LISTCOUNT)
879 qf_free(qi, 0);
880 for (i = 1; i < LISTCOUNT; ++i)
881 qi->qf_lists[i - 1] = qi->qf_lists[i];
882 qi->qf_curlist = LISTCOUNT - 1;
884 else
885 qi->qf_curlist = qi->qf_listcount++;
886 qi->qf_lists[qi->qf_curlist].qf_index = 0;
887 qi->qf_lists[qi->qf_curlist].qf_count = 0;
891 * Free a location list
893 static void
894 ll_free_all(pqi)
895 qf_info_T **pqi;
897 int i;
898 qf_info_T *qi;
900 qi = *pqi;
901 if (qi == NULL)
902 return;
903 *pqi = NULL; /* Remove reference to this list */
905 qi->qf_refcount--;
906 if (qi->qf_refcount < 1)
908 /* No references to this location list */
909 for (i = 0; i < qi->qf_listcount; ++i)
910 qf_free(qi, i);
911 vim_free(qi);
915 void
916 qf_free_all(wp)
917 win_T *wp;
919 int i;
920 qf_info_T *qi = &ql_info;
922 if (wp != NULL)
924 /* location list */
925 ll_free_all(&wp->w_llist);
926 ll_free_all(&wp->w_llist_ref);
928 else
929 /* quickfix list */
930 for (i = 0; i < qi->qf_listcount; ++i)
931 qf_free(qi, i);
935 * Add an entry to the end of the list of errors.
936 * Returns OK or FAIL.
938 static int
939 qf_add_entry(qi, prevp, dir, fname, mesg, lnum, col, vis_col, pattern, nr, type,
940 valid)
941 qf_info_T *qi; /* quickfix list */
942 qfline_T **prevp; /* pointer to previously added entry or NULL */
943 char_u *dir; /* optional directory name */
944 char_u *fname; /* file name or NULL */
945 char_u *mesg; /* message */
946 long lnum; /* line number */
947 int col; /* column */
948 int vis_col; /* using visual column */
949 char_u *pattern; /* search pattern */
950 int nr; /* error number */
951 int type; /* type character */
952 int valid; /* valid entry */
954 qfline_T *qfp;
956 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
957 return FAIL;
958 qfp->qf_fnum = qf_get_fnum(dir, fname);
959 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
961 vim_free(qfp);
962 return FAIL;
964 qfp->qf_lnum = lnum;
965 qfp->qf_col = col;
966 qfp->qf_viscol = vis_col;
967 if (pattern == NULL || *pattern == NUL)
968 qfp->qf_pattern = NULL;
969 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
971 vim_free(qfp->qf_text);
972 vim_free(qfp);
973 return FAIL;
975 qfp->qf_nr = nr;
976 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
977 type = 0;
978 qfp->qf_type = type;
979 qfp->qf_valid = valid;
981 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
982 /* first element in the list */
984 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
985 qfp->qf_prev = qfp; /* first element points to itself */
987 else
989 qfp->qf_prev = *prevp;
990 (*prevp)->qf_next = qfp;
992 qfp->qf_next = qfp; /* last element points to itself */
993 qfp->qf_cleared = FALSE;
994 *prevp = qfp;
995 ++qi->qf_lists[qi->qf_curlist].qf_count;
996 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
997 /* first valid entry */
999 qi->qf_lists[qi->qf_curlist].qf_index =
1000 qi->qf_lists[qi->qf_curlist].qf_count;
1001 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
1004 return OK;
1008 * Allocate a new location list
1010 static qf_info_T *
1011 ll_new_list()
1013 qf_info_T *qi;
1015 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1016 if (qi != NULL)
1018 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1019 qi->qf_refcount++;
1022 return qi;
1026 * Return the location list for window 'wp'.
1027 * If not present, allocate a location list
1029 static qf_info_T *
1030 ll_get_or_alloc_list(wp)
1031 win_T *wp;
1033 if (IS_LL_WINDOW(wp))
1034 /* For a location list window, use the referenced location list */
1035 return wp->w_llist_ref;
1038 * For a non-location list window, w_llist_ref should not point to a
1039 * location list.
1041 ll_free_all(&wp->w_llist_ref);
1043 if (wp->w_llist == NULL)
1044 wp->w_llist = ll_new_list(); /* new location list */
1045 return wp->w_llist;
1049 * Copy the location list from window "from" to window "to".
1051 void
1052 copy_loclist(from, to)
1053 win_T *from;
1054 win_T *to;
1056 qf_info_T *qi;
1057 int idx;
1058 int i;
1061 * When copying from a location list window, copy the referenced
1062 * location list. For other windows, copy the location list for
1063 * that window.
1065 if (IS_LL_WINDOW(from))
1066 qi = from->w_llist_ref;
1067 else
1068 qi = from->w_llist;
1070 if (qi == NULL) /* no location list to copy */
1071 return;
1073 /* allocate a new location list */
1074 if ((to->w_llist = ll_new_list()) == NULL)
1075 return;
1077 to->w_llist->qf_listcount = qi->qf_listcount;
1079 /* Copy the location lists one at a time */
1080 for (idx = 0; idx < qi->qf_listcount; idx++)
1082 qf_list_T *from_qfl;
1083 qf_list_T *to_qfl;
1085 to->w_llist->qf_curlist = idx;
1087 from_qfl = &qi->qf_lists[idx];
1088 to_qfl = &to->w_llist->qf_lists[idx];
1090 /* Some of the fields are populated by qf_add_entry() */
1091 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1092 to_qfl->qf_count = 0;
1093 to_qfl->qf_index = 0;
1094 to_qfl->qf_start = NULL;
1095 to_qfl->qf_ptr = NULL;
1097 if (from_qfl->qf_count)
1099 qfline_T *from_qfp;
1100 qfline_T *prevp = NULL;
1102 /* copy all the location entries in this list */
1103 for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
1104 ++i, from_qfp = from_qfp->qf_next)
1106 if (qf_add_entry(to->w_llist, &prevp,
1107 NULL,
1108 NULL,
1109 from_qfp->qf_text,
1110 from_qfp->qf_lnum,
1111 from_qfp->qf_col,
1112 from_qfp->qf_viscol,
1113 from_qfp->qf_pattern,
1114 from_qfp->qf_nr,
1116 from_qfp->qf_valid) == FAIL)
1118 qf_free_all(to);
1119 return;
1122 * qf_add_entry() will not set the qf_num field, as the
1123 * directory and file names are not supplied. So the qf_fnum
1124 * field is copied here.
1126 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1127 prevp->qf_type = from_qfp->qf_type; /* error type */
1128 if (from_qfl->qf_ptr == from_qfp)
1129 to_qfl->qf_ptr = prevp; /* current location */
1133 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1135 /* When no valid entries are present in the list, qf_ptr points to
1136 * the first item in the list */
1137 if (to_qfl->qf_nonevalid == TRUE)
1138 to_qfl->qf_ptr = to_qfl->qf_start;
1141 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1145 * get buffer number for file "dir.name"
1147 static int
1148 qf_get_fnum(directory, fname)
1149 char_u *directory;
1150 char_u *fname;
1152 if (fname == NULL || *fname == NUL) /* no file name */
1153 return 0;
1155 #ifdef RISCOS
1156 /* Name is reported as `main.c', but file is `c.main' */
1157 return ro_buflist_add(fname);
1158 #else
1159 char_u *ptr;
1160 int fnum;
1162 # ifdef VMS
1163 vms_remove_version(fname);
1164 # endif
1165 # ifdef BACKSLASH_IN_FILENAME
1166 if (directory != NULL)
1167 slash_adjust(directory);
1168 slash_adjust(fname);
1169 # endif
1170 if (directory != NULL && !vim_isAbsName(fname)
1171 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1174 * Here we check if the file really exists.
1175 * This should normally be true, but if make works without
1176 * "leaving directory"-messages we might have missed a
1177 * directory change.
1179 if (mch_getperm(ptr) < 0)
1181 vim_free(ptr);
1182 directory = qf_guess_filepath(fname);
1183 if (directory)
1184 ptr = concat_fnames(directory, fname, TRUE);
1185 else
1186 ptr = vim_strsave(fname);
1188 /* Use concatenated directory name and file name */
1189 fnum = buflist_add(ptr, 0);
1190 vim_free(ptr);
1191 return fnum;
1193 return buflist_add(fname, 0);
1194 #endif
1199 * push dirbuf onto the directory stack and return pointer to actual dir or
1200 * NULL on error
1202 static char_u *
1203 qf_push_dir(dirbuf, stackptr)
1204 char_u *dirbuf;
1205 struct dir_stack_T **stackptr;
1207 struct dir_stack_T *ds_new;
1208 struct dir_stack_T *ds_ptr;
1210 /* allocate new stack element and hook it in */
1211 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1212 if (ds_new == NULL)
1213 return NULL;
1215 ds_new->next = *stackptr;
1216 *stackptr = ds_new;
1218 /* store directory on the stack */
1219 if (vim_isAbsName(dirbuf)
1220 || (*stackptr)->next == NULL
1221 || (*stackptr && dir_stack != *stackptr))
1222 (*stackptr)->dirname = vim_strsave(dirbuf);
1223 else
1225 /* Okay we don't have an absolute path.
1226 * dirbuf must be a subdir of one of the directories on the stack.
1227 * Let's search...
1229 ds_new = (*stackptr)->next;
1230 (*stackptr)->dirname = NULL;
1231 while (ds_new)
1233 vim_free((*stackptr)->dirname);
1234 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1235 TRUE);
1236 if (mch_isdir((*stackptr)->dirname) == TRUE)
1237 break;
1239 ds_new = ds_new->next;
1242 /* clean up all dirs we already left */
1243 while ((*stackptr)->next != ds_new)
1245 ds_ptr = (*stackptr)->next;
1246 (*stackptr)->next = (*stackptr)->next->next;
1247 vim_free(ds_ptr->dirname);
1248 vim_free(ds_ptr);
1251 /* Nothing found -> it must be on top level */
1252 if (ds_new == NULL)
1254 vim_free((*stackptr)->dirname);
1255 (*stackptr)->dirname = vim_strsave(dirbuf);
1259 if ((*stackptr)->dirname != NULL)
1260 return (*stackptr)->dirname;
1261 else
1263 ds_ptr = *stackptr;
1264 *stackptr = (*stackptr)->next;
1265 vim_free(ds_ptr);
1266 return NULL;
1272 * pop dirbuf from the directory stack and return previous directory or NULL if
1273 * stack is empty
1275 static char_u *
1276 qf_pop_dir(stackptr)
1277 struct dir_stack_T **stackptr;
1279 struct dir_stack_T *ds_ptr;
1281 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1282 * What to do if it isn't? */
1284 /* pop top element and free it */
1285 if (*stackptr != NULL)
1287 ds_ptr = *stackptr;
1288 *stackptr = (*stackptr)->next;
1289 vim_free(ds_ptr->dirname);
1290 vim_free(ds_ptr);
1293 /* return NEW top element as current dir or NULL if stack is empty*/
1294 return *stackptr ? (*stackptr)->dirname : NULL;
1298 * clean up directory stack
1300 static void
1301 qf_clean_dir_stack(stackptr)
1302 struct dir_stack_T **stackptr;
1304 struct dir_stack_T *ds_ptr;
1306 while ((ds_ptr = *stackptr) != NULL)
1308 *stackptr = (*stackptr)->next;
1309 vim_free(ds_ptr->dirname);
1310 vim_free(ds_ptr);
1315 * Check in which directory of the directory stack the given file can be
1316 * found.
1317 * Returns a pointer to the directory name or NULL if not found
1318 * Cleans up intermediate directory entries.
1320 * TODO: How to solve the following problem?
1321 * If we have the this directory tree:
1322 * ./
1323 * ./aa
1324 * ./aa/bb
1325 * ./bb
1326 * ./bb/x.c
1327 * and make says:
1328 * making all in aa
1329 * making all in bb
1330 * x.c:9: Error
1331 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1332 * qf_guess_filepath will return NULL.
1334 static char_u *
1335 qf_guess_filepath(filename)
1336 char_u *filename;
1338 struct dir_stack_T *ds_ptr;
1339 struct dir_stack_T *ds_tmp;
1340 char_u *fullname;
1342 /* no dirs on the stack - there's nothing we can do */
1343 if (dir_stack == NULL)
1344 return NULL;
1346 ds_ptr = dir_stack->next;
1347 fullname = NULL;
1348 while (ds_ptr)
1350 vim_free(fullname);
1351 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1353 /* If concat_fnames failed, just go on. The worst thing that can happen
1354 * is that we delete the entire stack.
1356 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1357 break;
1359 ds_ptr = ds_ptr->next;
1362 vim_free(fullname);
1364 /* clean up all dirs we already left */
1365 while (dir_stack->next != ds_ptr)
1367 ds_tmp = dir_stack->next;
1368 dir_stack->next = dir_stack->next->next;
1369 vim_free(ds_tmp->dirname);
1370 vim_free(ds_tmp);
1373 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1378 * jump to a quickfix line
1379 * if dir == FORWARD go "errornr" valid entries forward
1380 * if dir == BACKWARD go "errornr" valid entries backward
1381 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1382 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1383 * else if "errornr" is zero, redisplay the same line
1384 * else go to entry "errornr"
1386 void
1387 qf_jump(qi, dir, errornr, forceit)
1388 qf_info_T *qi;
1389 int dir;
1390 int errornr;
1391 int forceit;
1393 qf_info_T *ll_ref;
1394 qfline_T *qf_ptr;
1395 qfline_T *old_qf_ptr;
1396 int qf_index;
1397 int old_qf_fnum;
1398 int old_qf_index;
1399 int prev_index;
1400 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1401 char_u *err = e_no_more_items;
1402 linenr_T i;
1403 buf_T *old_curbuf;
1404 linenr_T old_lnum;
1405 colnr_T screen_col;
1406 colnr_T char_col;
1407 char_u *line;
1408 #ifdef FEAT_WINDOWS
1409 char_u *old_swb = p_swb;
1410 int opened_window = FALSE;
1411 win_T *win;
1412 win_T *altwin;
1413 #endif
1414 int print_message = TRUE;
1415 int len;
1416 #ifdef FEAT_FOLDING
1417 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1418 #endif
1419 int ok = OK;
1420 int usable_win;
1422 if (qi == NULL)
1423 qi = &ql_info;
1425 if (qi->qf_curlist >= qi->qf_listcount
1426 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
1428 EMSG(_(e_quickfix));
1429 return;
1432 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
1433 old_qf_ptr = qf_ptr;
1434 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
1435 old_qf_index = qf_index;
1436 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1438 while (errornr--)
1440 old_qf_ptr = qf_ptr;
1441 prev_index = qf_index;
1442 old_qf_fnum = qf_ptr->qf_fnum;
1445 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
1446 || qf_ptr->qf_next == NULL)
1448 qf_ptr = old_qf_ptr;
1449 qf_index = prev_index;
1450 if (err != NULL)
1452 EMSG(_(err));
1453 goto theend;
1455 errornr = 0;
1456 break;
1458 ++qf_index;
1459 qf_ptr = qf_ptr->qf_next;
1460 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1461 && !qf_ptr->qf_valid)
1462 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1463 err = NULL;
1466 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1468 while (errornr--)
1470 old_qf_ptr = qf_ptr;
1471 prev_index = qf_index;
1472 old_qf_fnum = qf_ptr->qf_fnum;
1475 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1477 qf_ptr = old_qf_ptr;
1478 qf_index = prev_index;
1479 if (err != NULL)
1481 EMSG(_(err));
1482 goto theend;
1484 errornr = 0;
1485 break;
1487 --qf_index;
1488 qf_ptr = qf_ptr->qf_prev;
1489 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1490 && !qf_ptr->qf_valid)
1491 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1492 err = NULL;
1495 else if (errornr != 0) /* go to specified number */
1497 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1499 --qf_index;
1500 qf_ptr = qf_ptr->qf_prev;
1502 while (errornr > qf_index && qf_index <
1503 qi->qf_lists[qi->qf_curlist].qf_count
1504 && qf_ptr->qf_next != NULL)
1506 ++qf_index;
1507 qf_ptr = qf_ptr->qf_next;
1511 #ifdef FEAT_WINDOWS
1512 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1513 if (qf_win_pos_update(qi, old_qf_index))
1514 /* No need to print the error message if it's visible in the error
1515 * window */
1516 print_message = FALSE;
1519 * For ":helpgrep" find a help window or open one.
1521 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
1523 win_T *wp;
1524 int n;
1526 if (cmdmod.tab != 0)
1527 wp = NULL;
1528 else
1529 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1530 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1531 break;
1532 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1533 win_enter(wp, TRUE);
1534 else
1537 * Split off help window; put it at far top if no position
1538 * specified, the current window is vertically split and narrow.
1540 n = WSP_HELP;
1541 # ifdef FEAT_VERTSPLIT
1542 if (cmdmod.split == 0 && curwin->w_width != Columns
1543 && curwin->w_width < 80)
1544 n |= WSP_TOP;
1545 # endif
1546 if (win_split(0, n) == FAIL)
1547 goto theend;
1548 opened_window = TRUE; /* close it when fail */
1550 if (curwin->w_height < p_hh)
1551 win_setheight((int)p_hh);
1553 if (qi != &ql_info) /* not a quickfix list */
1555 /* The new window should use the supplied location list */
1556 qf_free_all(curwin);
1557 curwin->w_llist = qi;
1558 qi->qf_refcount++;
1562 if (!p_im)
1563 restart_edit = 0; /* don't want insert mode in help file */
1567 * If currently in the quickfix window, find another window to show the
1568 * file in.
1570 if (bt_quickfix(curbuf) && !opened_window)
1573 * If there is no file specified, we don't know where to go.
1574 * But do advance, otherwise ":cn" gets stuck.
1576 if (qf_ptr->qf_fnum == 0)
1577 goto theend;
1579 /* Locate a window showing a normal buffer */
1580 usable_win = 0;
1581 FOR_ALL_WINDOWS(win)
1582 if (win->w_buffer->b_p_bt[0] == NUL)
1584 usable_win = 1;
1585 break;
1589 * If there is only one window, create a new one above the quickfix
1590 * window.
1592 if (firstwin == lastwin || !usable_win)
1594 ll_ref = curwin->w_llist_ref;
1596 if (win_split(0, WSP_ABOVE) == FAIL)
1597 goto failed; /* not enough room for window */
1598 opened_window = TRUE; /* close it when fail */
1599 p_swb = empty_option; /* don't split again */
1600 # ifdef FEAT_SCROLLBIND
1601 curwin->w_p_scb = FALSE;
1602 # endif
1603 if (ll_ref != NULL)
1605 /* The new window should use the location list from the
1606 * location list window */
1607 qf_free_all(curwin);
1608 curwin->w_llist = ll_ref;
1609 ll_ref->qf_refcount++;
1612 else
1614 if (curwin->w_llist_ref != NULL)
1616 /* In a location window */
1617 ll_ref = curwin->w_llist_ref;
1619 /* Find the window with the same location list */
1620 FOR_ALL_WINDOWS(win)
1621 if (win->w_llist == ll_ref)
1622 break;
1623 if (win == NULL)
1625 /* Find the window showing the selected file */
1626 FOR_ALL_WINDOWS(win)
1627 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1628 break;
1629 if (win == NULL)
1631 /* Find a previous usable window */
1632 win = curwin;
1635 if (win->w_buffer->b_p_bt[0] == NUL)
1636 break;
1637 if (win->w_prev == NULL)
1638 win = lastwin; /* wrap around the top */
1639 else
1640 win = win->w_prev; /* go to previous window */
1641 } while (win != curwin);
1644 win_goto(win);
1646 /* If the location list for the window is not set, then set it
1647 * to the location list from the location window */
1648 if (win->w_llist == NULL)
1650 win->w_llist = ll_ref;
1651 ll_ref->qf_refcount++;
1654 else
1658 * Try to find a window that shows the right buffer.
1659 * Default to the window just above the quickfix buffer.
1661 win = curwin;
1662 altwin = NULL;
1663 for (;;)
1665 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1666 break;
1667 if (win->w_prev == NULL)
1668 win = lastwin; /* wrap around the top */
1669 else
1670 win = win->w_prev; /* go to previous window */
1672 if (IS_QF_WINDOW(win))
1674 /* Didn't find it, go to the window before the quickfix
1675 * window. */
1676 if (altwin != NULL)
1677 win = altwin;
1678 else if (curwin->w_prev != NULL)
1679 win = curwin->w_prev;
1680 else
1681 win = curwin->w_next;
1682 break;
1685 /* Remember a usable window. */
1686 if (altwin == NULL && !win->w_p_pvw
1687 && win->w_buffer->b_p_bt[0] == NUL)
1688 altwin = win;
1691 win_goto(win);
1695 #endif
1698 * If there is a file name,
1699 * read the wanted file if needed, and check autowrite etc.
1701 old_curbuf = curbuf;
1702 old_lnum = curwin->w_cursor.lnum;
1704 if (qf_ptr->qf_fnum != 0)
1706 if (qf_ptr->qf_type == 1)
1708 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1709 * set b_p_ro flag). */
1710 if (!can_abandon(curbuf, forceit))
1712 EMSG(_(e_nowrtmsg));
1713 ok = FALSE;
1715 else
1716 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
1717 ECMD_HIDE + ECMD_SET_HELP);
1719 else
1720 ok = buflist_getfile(qf_ptr->qf_fnum,
1721 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
1724 if (ok == OK)
1726 /* When not switched to another buffer, still need to set pc mark */
1727 if (curbuf == old_curbuf)
1728 setpcmark();
1730 if (qf_ptr->qf_pattern == NULL)
1733 * Go to line with error, unless qf_lnum is 0.
1735 i = qf_ptr->qf_lnum;
1736 if (i > 0)
1738 if (i > curbuf->b_ml.ml_line_count)
1739 i = curbuf->b_ml.ml_line_count;
1740 curwin->w_cursor.lnum = i;
1742 if (qf_ptr->qf_col > 0)
1744 curwin->w_cursor.col = qf_ptr->qf_col - 1;
1745 if (qf_ptr->qf_viscol == TRUE)
1748 * Check each character from the beginning of the error
1749 * line up to the error column. For each tab character
1750 * found, reduce the error column value by the length of
1751 * a tab character.
1753 line = ml_get_curline();
1754 screen_col = 0;
1755 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1757 if (*line == NUL)
1758 break;
1759 if (*line++ == '\t')
1761 curwin->w_cursor.col -= 7 - (screen_col % 8);
1762 screen_col += 8 - (screen_col % 8);
1764 else
1765 ++screen_col;
1768 check_cursor();
1770 else
1771 beginline(BL_WHITE | BL_FIX);
1773 else
1775 pos_T save_cursor;
1777 /* Move the cursor to the first line in the buffer */
1778 save_cursor = curwin->w_cursor;
1779 curwin->w_cursor.lnum = 0;
1780 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1, SEARCH_KEEP))
1781 curwin->w_cursor = save_cursor;
1784 #ifdef FEAT_FOLDING
1785 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1786 foldOpenCursor();
1787 #endif
1788 if (print_message)
1790 /* Update the screen before showing the message */
1791 update_topline_redraw();
1792 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
1793 qi->qf_lists[qi->qf_curlist].qf_count,
1794 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1795 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1796 /* Add the message, skipping leading whitespace and newlines. */
1797 len = (int)STRLEN(IObuff);
1798 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1800 /* Output the message. Overwrite to avoid scrolling when the 'O'
1801 * flag is present in 'shortmess'; But when not jumping, print the
1802 * whole message. */
1803 i = msg_scroll;
1804 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1805 msg_scroll = TRUE;
1806 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1807 msg_scroll = FALSE;
1808 msg_attr_keep(IObuff, 0, TRUE);
1809 msg_scroll = i;
1812 else
1814 #ifdef FEAT_WINDOWS
1815 if (opened_window)
1816 win_close(curwin, TRUE); /* Close opened window */
1817 #endif
1818 if (qf_ptr->qf_fnum != 0)
1821 * Couldn't open file, so put index back where it was. This could
1822 * happen if the file was readonly and we changed something.
1824 #ifdef FEAT_WINDOWS
1825 failed:
1826 #endif
1827 qf_ptr = old_qf_ptr;
1828 qf_index = old_qf_index;
1831 theend:
1832 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
1833 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1834 #ifdef FEAT_WINDOWS
1835 if (p_swb != old_swb && opened_window)
1837 /* Restore old 'switchbuf' value, but not when an autocommand or
1838 * modeline has changed the value. */
1839 if (p_swb == empty_option)
1840 p_swb = old_swb;
1841 else
1842 free_string_option(old_swb);
1844 #endif
1848 * ":clist": list all errors
1849 * ":llist": list all locations
1851 void
1852 qf_list(eap)
1853 exarg_T *eap;
1855 buf_T *buf;
1856 char_u *fname;
1857 qfline_T *qfp;
1858 int i;
1859 int idx1 = 1;
1860 int idx2 = -1;
1861 int need_return = TRUE;
1862 char_u *arg = eap->arg;
1863 int all = eap->forceit; /* if not :cl!, only show
1864 recognised errors */
1865 qf_info_T *qi = &ql_info;
1867 if (eap->cmdidx == CMD_llist)
1869 qi = GET_LOC_LIST(curwin);
1870 if (qi == NULL)
1872 EMSG(_(e_loclist));
1873 return;
1877 if (qi->qf_curlist >= qi->qf_listcount
1878 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
1880 EMSG(_(e_quickfix));
1881 return;
1883 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
1885 EMSG(_(e_trailing));
1886 return;
1888 i = qi->qf_lists[qi->qf_curlist].qf_count;
1889 if (idx1 < 0)
1890 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
1891 if (idx2 < 0)
1892 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
1894 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
1895 all = TRUE;
1896 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
1897 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
1899 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
1901 if (need_return)
1903 msg_putchar('\n');
1904 if (got_int)
1905 break;
1906 need_return = FALSE;
1909 fname = NULL;
1910 if (qfp->qf_fnum != 0
1911 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
1913 fname = buf->b_fname;
1914 if (qfp->qf_type == 1) /* :helpgrep */
1915 fname = gettail(fname);
1917 if (fname == NULL)
1918 sprintf((char *)IObuff, "%2d", i);
1919 else
1920 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
1921 i, (char *)fname);
1922 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
1923 ? hl_attr(HLF_L) : hl_attr(HLF_D));
1924 if (qfp->qf_lnum == 0)
1925 IObuff[0] = NUL;
1926 else if (qfp->qf_col == 0)
1927 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
1928 else
1929 sprintf((char *)IObuff, ":%ld col %d",
1930 qfp->qf_lnum, qfp->qf_col);
1931 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
1932 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
1933 msg_puts_attr(IObuff, hl_attr(HLF_N));
1934 if (qfp->qf_pattern != NULL)
1936 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
1937 STRCAT(IObuff, ":");
1938 msg_puts(IObuff);
1940 msg_puts((char_u *)" ");
1942 /* Remove newlines and leading whitespace from the text. For an
1943 * unrecognized line keep the indent, the compiler may mark a word
1944 * with ^^^^. */
1945 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
1946 ? skipwhite(qfp->qf_text) : qfp->qf_text,
1947 IObuff, IOSIZE);
1948 msg_prt_line(IObuff, FALSE);
1949 out_flush(); /* show one line at a time */
1950 need_return = TRUE;
1953 qfp = qfp->qf_next;
1954 ++i;
1955 ui_breakcheck();
1960 * Remove newlines and leading whitespace from an error message.
1961 * Put the result in "buf[bufsize]".
1963 static void
1964 qf_fmt_text(text, buf, bufsize)
1965 char_u *text;
1966 char_u *buf;
1967 int bufsize;
1969 int i;
1970 char_u *p = text;
1972 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
1974 if (*p == '\n')
1976 buf[i] = ' ';
1977 while (*++p != NUL)
1978 if (!vim_iswhite(*p) && *p != '\n')
1979 break;
1981 else
1982 buf[i] = *p++;
1984 buf[i] = NUL;
1988 * ":colder [count]": Up in the quickfix stack.
1989 * ":cnewer [count]": Down in the quickfix stack.
1990 * ":lolder [count]": Up in the location list stack.
1991 * ":lnewer [count]": Down in the location list stack.
1993 void
1994 qf_age(eap)
1995 exarg_T *eap;
1997 qf_info_T *qi = &ql_info;
1998 int count;
2000 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2002 qi = GET_LOC_LIST(curwin);
2003 if (qi == NULL)
2005 EMSG(_(e_loclist));
2006 return;
2010 if (eap->addr_count != 0)
2011 count = eap->line2;
2012 else
2013 count = 1;
2014 while (count--)
2016 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
2018 if (qi->qf_curlist == 0)
2020 EMSG(_("E380: At bottom of quickfix stack"));
2021 return;
2023 --qi->qf_curlist;
2025 else
2027 if (qi->qf_curlist >= qi->qf_listcount - 1)
2029 EMSG(_("E381: At top of quickfix stack"));
2030 return;
2032 ++qi->qf_curlist;
2035 qf_msg(qi);
2039 static void
2040 qf_msg(qi)
2041 qf_info_T *qi;
2043 smsg((char_u *)_("error list %d of %d; %d errors"),
2044 qi->qf_curlist + 1, qi->qf_listcount,
2045 qi->qf_lists[qi->qf_curlist].qf_count);
2046 #ifdef FEAT_WINDOWS
2047 qf_update_buffer(qi);
2048 #endif
2052 * Free error list "idx".
2054 static void
2055 qf_free(qi, idx)
2056 qf_info_T *qi;
2057 int idx;
2059 qfline_T *qfp;
2061 while (qi->qf_lists[idx].qf_count)
2063 qfp = qi->qf_lists[idx].qf_start->qf_next;
2064 vim_free(qi->qf_lists[idx].qf_start->qf_text);
2065 vim_free(qi->qf_lists[idx].qf_start->qf_pattern);
2066 vim_free(qi->qf_lists[idx].qf_start);
2067 qi->qf_lists[idx].qf_start = qfp;
2068 --qi->qf_lists[idx].qf_count;
2073 * qf_mark_adjust: adjust marks
2075 void
2076 qf_mark_adjust(wp, line1, line2, amount, amount_after)
2077 win_T *wp;
2078 linenr_T line1;
2079 linenr_T line2;
2080 long amount;
2081 long amount_after;
2083 int i;
2084 qfline_T *qfp;
2085 int idx;
2086 qf_info_T *qi = &ql_info;
2088 if (wp != NULL)
2090 if (wp->w_llist == NULL)
2091 return;
2092 qi = wp->w_llist;
2095 for (idx = 0; idx < qi->qf_listcount; ++idx)
2096 if (qi->qf_lists[idx].qf_count)
2097 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
2098 i < qi->qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
2099 if (qfp->qf_fnum == curbuf->b_fnum)
2101 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2103 if (amount == MAXLNUM)
2104 qfp->qf_cleared = TRUE;
2105 else
2106 qfp->qf_lnum += amount;
2108 else if (amount_after && qfp->qf_lnum > line2)
2109 qfp->qf_lnum += amount_after;
2114 * Make a nice message out of the error character and the error number:
2115 * char number message
2116 * e or E 0 " error"
2117 * w or W 0 " warning"
2118 * i or I 0 " info"
2119 * 0 0 ""
2120 * other 0 " c"
2121 * e or E n " error n"
2122 * w or W n " warning n"
2123 * i or I n " info n"
2124 * 0 n " error n"
2125 * other n " c n"
2126 * 1 x "" :helpgrep
2128 static char_u *
2129 qf_types(c, nr)
2130 int c, nr;
2132 static char_u buf[20];
2133 static char_u cc[3];
2134 char_u *p;
2136 if (c == 'W' || c == 'w')
2137 p = (char_u *)" warning";
2138 else if (c == 'I' || c == 'i')
2139 p = (char_u *)" info";
2140 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2141 p = (char_u *)" error";
2142 else if (c == 0 || c == 1)
2143 p = (char_u *)"";
2144 else
2146 cc[0] = ' ';
2147 cc[1] = c;
2148 cc[2] = NUL;
2149 p = cc;
2152 if (nr <= 0)
2153 return p;
2155 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2156 return buf;
2159 #if defined(FEAT_WINDOWS) || defined(PROTO)
2161 * ":cwindow": open the quickfix window if we have errors to display,
2162 * close it if not.
2163 * ":lwindow": open the location list window if we have locations to display,
2164 * close it if not.
2166 void
2167 ex_cwindow(eap)
2168 exarg_T *eap;
2170 qf_info_T *qi = &ql_info;
2171 win_T *win;
2173 if (eap->cmdidx == CMD_lwindow)
2175 qi = GET_LOC_LIST(curwin);
2176 if (qi == NULL)
2177 return;
2180 /* Look for an existing quickfix window. */
2181 win = qf_find_win(qi);
2184 * If a quickfix window is open but we have no errors to display,
2185 * close the window. If a quickfix window is not open, then open
2186 * it if we have errors; otherwise, leave it closed.
2188 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
2189 || qi->qf_curlist >= qi->qf_listcount)
2191 if (win != NULL)
2192 ex_cclose(eap);
2194 else if (win == NULL)
2195 ex_copen(eap);
2199 * ":cclose": close the window showing the list of errors.
2200 * ":lclose": close the window showing the location list
2202 /*ARGSUSED*/
2203 void
2204 ex_cclose(eap)
2205 exarg_T *eap;
2207 win_T *win = NULL;
2208 qf_info_T *qi = &ql_info;
2210 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2212 qi = GET_LOC_LIST(curwin);
2213 if (qi == NULL)
2214 return;
2217 /* Find existing quickfix window and close it. */
2218 win = qf_find_win(qi);
2219 if (win != NULL)
2220 win_close(win, FALSE);
2224 * ":copen": open a window that shows the list of errors.
2225 * ":lopen": open a window that shows the location list.
2227 void
2228 ex_copen(eap)
2229 exarg_T *eap;
2231 qf_info_T *qi = &ql_info;
2232 int height;
2233 win_T *win;
2234 tabpage_T *prevtab = curtab;
2235 buf_T *qf_buf;
2237 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2239 qi = GET_LOC_LIST(curwin);
2240 if (qi == NULL)
2242 EMSG(_(e_loclist));
2243 return;
2247 if (eap->addr_count != 0)
2248 height = eap->line2;
2249 else
2250 height = QF_WINHEIGHT;
2252 #ifdef FEAT_VISUAL
2253 reset_VIsual_and_resel(); /* stop Visual mode */
2254 #endif
2255 #ifdef FEAT_GUI
2256 need_mouse_correct = TRUE;
2257 #endif
2260 * Find existing quickfix window, or open a new one.
2262 win = qf_find_win(qi);
2264 if (win != NULL && cmdmod.tab == 0)
2265 win_goto(win);
2266 else
2268 qf_buf = qf_find_buf(qi);
2270 /* The current window becomes the previous window afterwards. */
2271 win = curwin;
2273 if (eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2274 /* Create the new window at the very bottom. */
2275 win_goto(lastwin);
2276 if (win_split(height, WSP_BELOW) == FAIL)
2277 return; /* not enough room for window */
2278 #ifdef FEAT_SCROLLBIND
2279 curwin->w_p_scb = FALSE;
2280 #endif
2282 /* Remove the location list for the quickfix window */
2283 qf_free_all(curwin);
2285 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2288 * For the location list window, create a reference to the
2289 * location list from the window 'win'.
2291 curwin->w_llist_ref = win->w_llist;
2292 win->w_llist->qf_refcount++;
2295 if (qf_buf != NULL)
2296 /* Use the existing quickfix buffer */
2297 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
2298 ECMD_HIDE + ECMD_OLDBUF);
2299 else
2301 /* Create a new quickfix buffer */
2302 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
2303 /* switch off 'swapfile' */
2304 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2305 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
2306 OPT_LOCAL);
2307 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
2308 set_option_value((char_u *)"diff", 0L, (char_u *)"", OPT_LOCAL);
2311 /* Only set the height when still in the same tab page and there is no
2312 * window to the side. */
2313 if (curtab == prevtab
2314 #ifdef FEAT_VERTSPLIT
2315 && curwin->w_width == Columns
2316 #endif
2318 win_setheight(height);
2319 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2320 if (win_valid(win))
2321 prevwin = win;
2325 * Fill the buffer with the quickfix list.
2327 qf_fill_buffer(qi);
2329 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
2330 curwin->w_cursor.col = 0;
2331 check_cursor();
2332 update_topline(); /* scroll to show the line */
2336 * Return the number of the current entry (line number in the quickfix
2337 * window).
2339 linenr_T
2340 qf_current_entry(wp)
2341 win_T *wp;
2343 qf_info_T *qi = &ql_info;
2345 if (IS_LL_WINDOW(wp))
2346 /* In the location list window, use the referenced location list */
2347 qi = wp->w_llist_ref;
2349 return qi->qf_lists[qi->qf_curlist].qf_index;
2353 * Update the cursor position in the quickfix window to the current error.
2354 * Return TRUE if there is a quickfix window.
2356 static int
2357 qf_win_pos_update(qi, old_qf_index)
2358 qf_info_T *qi;
2359 int old_qf_index; /* previous qf_index or zero */
2361 win_T *win;
2362 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
2365 * Put the cursor on the current error in the quickfix window, so that
2366 * it's viewable.
2368 win = qf_find_win(qi);
2369 if (win != NULL
2370 && qf_index <= win->w_buffer->b_ml.ml_line_count
2371 && old_qf_index != qf_index)
2373 win_T *old_curwin = curwin;
2375 curwin = win;
2376 curbuf = win->w_buffer;
2377 if (qf_index > old_qf_index)
2379 curwin->w_redraw_top = old_qf_index;
2380 curwin->w_redraw_bot = qf_index;
2382 else
2384 curwin->w_redraw_top = qf_index;
2385 curwin->w_redraw_bot = old_qf_index;
2387 curwin->w_cursor.lnum = qf_index;
2388 curwin->w_cursor.col = 0;
2389 update_topline(); /* scroll to show the line */
2390 redraw_later(VALID);
2391 curwin->w_redr_status = TRUE; /* update ruler */
2392 curwin = old_curwin;
2393 curbuf = curwin->w_buffer;
2395 return win != NULL;
2399 * Check whether the given window is displaying the specified quickfix/location
2400 * list buffer
2402 static int
2403 is_qf_win(win, qi)
2404 win_T *win;
2405 qf_info_T *qi;
2408 * A window displaying the quickfix buffer will have the w_llist_ref field
2409 * set to NULL.
2410 * A window displaying a location list buffer will have the w_llist_ref
2411 * pointing to the location list.
2413 if (bt_quickfix(win->w_buffer))
2414 if ((qi == &ql_info && win->w_llist_ref == NULL)
2415 || (qi != &ql_info && win->w_llist_ref == qi))
2416 return TRUE;
2418 return FALSE;
2422 * Find a window displaying the quickfix/location list 'qi'
2423 * Searches in only the windows opened in the current tab.
2425 static win_T *
2426 qf_find_win(qi)
2427 qf_info_T *qi;
2429 win_T *win;
2431 FOR_ALL_WINDOWS(win)
2432 if (is_qf_win(win, qi))
2433 break;
2435 return win;
2439 * Find a quickfix buffer.
2440 * Searches in windows opened in all the tabs.
2442 static buf_T *
2443 qf_find_buf(qi)
2444 qf_info_T *qi;
2446 tabpage_T *tp;
2447 win_T *win;
2449 FOR_ALL_TAB_WINDOWS(tp, win)
2450 if (is_qf_win(win, qi))
2451 return win->w_buffer;
2453 return NULL;
2457 * Find the quickfix buffer. If it exists, update the contents.
2459 static void
2460 qf_update_buffer(qi)
2461 qf_info_T *qi;
2463 buf_T *buf;
2464 aco_save_T aco;
2466 /* Check if a buffer for the quickfix list exists. Update it. */
2467 buf = qf_find_buf(qi);
2468 if (buf != NULL)
2470 /* set curwin/curbuf to buf and save a few things */
2471 aucmd_prepbuf(&aco, buf);
2473 qf_fill_buffer(qi);
2475 /* restore curwin/curbuf and a few other things */
2476 aucmd_restbuf(&aco);
2478 (void)qf_win_pos_update(qi, 0);
2483 * Fill current buffer with quickfix errors, replacing any previous contents.
2484 * curbuf must be the quickfix buffer!
2486 static void
2487 qf_fill_buffer(qi)
2488 qf_info_T *qi;
2490 linenr_T lnum;
2491 qfline_T *qfp;
2492 buf_T *errbuf;
2493 int len;
2494 int old_KeyTyped = KeyTyped;
2496 /* delete all existing lines */
2497 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2498 (void)ml_delete((linenr_T)1, FALSE);
2500 /* Check if there is anything to display */
2501 if (qi->qf_curlist < qi->qf_listcount)
2503 /* Add one line for each error */
2504 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2505 for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
2507 if (qfp->qf_fnum != 0
2508 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2509 && errbuf->b_fname != NULL)
2511 if (qfp->qf_type == 1) /* :helpgrep */
2512 STRCPY(IObuff, gettail(errbuf->b_fname));
2513 else
2514 STRCPY(IObuff, errbuf->b_fname);
2515 len = (int)STRLEN(IObuff);
2517 else
2518 len = 0;
2519 IObuff[len++] = '|';
2521 if (qfp->qf_lnum > 0)
2523 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2524 len += (int)STRLEN(IObuff + len);
2526 if (qfp->qf_col > 0)
2528 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2529 len += (int)STRLEN(IObuff + len);
2532 sprintf((char *)IObuff + len, "%s",
2533 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2534 len += (int)STRLEN(IObuff + len);
2536 else if (qfp->qf_pattern != NULL)
2538 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2539 len += (int)STRLEN(IObuff + len);
2541 IObuff[len++] = '|';
2542 IObuff[len++] = ' ';
2544 /* Remove newlines and leading whitespace from the text.
2545 * For an unrecognized line keep the indent, the compiler may
2546 * mark a word with ^^^^. */
2547 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2548 IObuff + len, IOSIZE - len);
2550 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2551 == FAIL)
2552 break;
2553 qfp = qfp->qf_next;
2555 /* Delete the empty line which is now at the end */
2556 (void)ml_delete(lnum + 1, FALSE);
2559 /* correct cursor position */
2560 check_lnums(TRUE);
2562 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2563 * resembles reading a file into a buffer, it's more logical when using
2564 * autocommands. */
2565 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2566 curbuf->b_p_ma = FALSE;
2568 #ifdef FEAT_AUTOCMD
2569 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2570 FALSE, curbuf);
2571 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2572 FALSE, curbuf);
2573 #endif
2575 /* make sure it will be redrawn */
2576 redraw_curbuf_later(NOT_VALID);
2578 /* Restore KeyTyped, setting 'filetype' may reset it. */
2579 KeyTyped = old_KeyTyped;
2582 #endif /* FEAT_WINDOWS */
2585 * Return TRUE if "buf" is the quickfix buffer.
2588 bt_quickfix(buf)
2589 buf_T *buf;
2591 return (buf->b_p_bt[0] == 'q');
2595 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2596 * This means the buffer name is not a file name.
2599 bt_nofile(buf)
2600 buf_T *buf;
2602 return (buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2603 || buf->b_p_bt[0] == 'a';
2607 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2610 bt_dontwrite(buf)
2611 buf_T *buf;
2613 return (buf->b_p_bt[0] == 'n');
2617 bt_dontwrite_msg(buf)
2618 buf_T *buf;
2620 if (bt_dontwrite(buf))
2622 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2623 return TRUE;
2625 return FALSE;
2629 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2630 * and 'bufhidden'.
2633 buf_hide(buf)
2634 buf_T *buf;
2636 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2637 switch (buf->b_p_bh[0])
2639 case 'u': /* "unload" */
2640 case 'w': /* "wipe" */
2641 case 'd': return FALSE; /* "delete" */
2642 case 'h': return TRUE; /* "hide" */
2644 return (p_hid || cmdmod.hide);
2648 * Return TRUE when using ":vimgrep" for ":grep".
2651 grep_internal(cmdidx)
2652 cmdidx_T cmdidx;
2654 return ((cmdidx == CMD_grep
2655 || cmdidx == CMD_lgrep
2656 || cmdidx == CMD_grepadd
2657 || cmdidx == CMD_lgrepadd)
2658 && STRCMP("internal",
2659 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2663 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
2665 void
2666 ex_make(eap)
2667 exarg_T *eap;
2669 char_u *fname;
2670 char_u *cmd;
2671 unsigned len;
2672 win_T *wp = NULL;
2673 qf_info_T *qi = &ql_info;
2674 int res;
2675 #ifdef FEAT_AUTOCMD
2676 char_u *au_name = NULL;
2678 switch (eap->cmdidx)
2680 case CMD_make: au_name = (char_u *)"make"; break;
2681 case CMD_lmake: au_name = (char_u *)"lmake"; break;
2682 case CMD_grep: au_name = (char_u *)"grep"; break;
2683 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
2684 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
2685 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
2686 default: break;
2688 if (au_name != NULL)
2690 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2691 curbuf->b_fname, TRUE, curbuf);
2692 # ifdef FEAT_EVAL
2693 if (did_throw || force_abort)
2694 return;
2695 # endif
2697 #endif
2699 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
2700 if (grep_internal(eap->cmdidx))
2702 ex_vimgrep(eap);
2703 return;
2706 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
2707 || eap->cmdidx == CMD_lgrepadd)
2708 wp = curwin;
2710 autowrite_all();
2711 fname = get_mef_name();
2712 if (fname == NULL)
2713 return;
2714 mch_remove(fname); /* in case it's not unique */
2717 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2719 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2720 if (*p_sp != NUL)
2721 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
2722 cmd = alloc(len);
2723 if (cmd == NULL)
2724 return;
2725 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2726 (char *)p_shq);
2727 if (*p_sp != NUL)
2728 append_redir(cmd, p_sp, fname);
2730 * Output a newline if there's something else than the :make command that
2731 * was typed (in which case the cursor is in column 0).
2733 if (msg_col == 0)
2734 msg_didout = FALSE;
2735 msg_start();
2736 MSG_PUTS(":!");
2737 msg_outtrans(cmd); /* show what we are doing */
2739 /* let the shell know if we are redirecting output or not */
2740 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2742 #ifdef AMIGA
2743 out_flush();
2744 /* read window status report and redraw before message */
2745 (void)char_avail();
2746 #endif
2748 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
2749 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
2750 (eap->cmdidx != CMD_grepadd
2751 && eap->cmdidx != CMD_lgrepadd));
2752 #ifdef FEAT_AUTOCMD
2753 if (au_name != NULL)
2754 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2755 curbuf->b_fname, TRUE, curbuf);
2756 #endif
2757 if (res > 0 && !eap->forceit)
2759 if (wp != NULL)
2760 qi = GET_LOC_LIST(wp);
2761 qf_jump(qi, 0, 0, FALSE); /* display first error */
2764 mch_remove(fname);
2765 vim_free(fname);
2766 vim_free(cmd);
2770 * Return the name for the errorfile, in allocated memory.
2771 * Find a new unique name when 'makeef' contains "##".
2772 * Returns NULL for error.
2774 static char_u *
2775 get_mef_name()
2777 char_u *p;
2778 char_u *name;
2779 static int start = -1;
2780 static int off = 0;
2781 #ifdef HAVE_LSTAT
2782 struct stat sb;
2783 #endif
2785 if (*p_mef == NUL)
2787 name = vim_tempname('e');
2788 if (name == NULL)
2789 EMSG(_(e_notmp));
2790 return name;
2793 for (p = p_mef; *p; ++p)
2794 if (p[0] == '#' && p[1] == '#')
2795 break;
2797 if (*p == NUL)
2798 return vim_strsave(p_mef);
2800 /* Keep trying until the name doesn't exist yet. */
2801 for (;;)
2803 if (start == -1)
2804 start = mch_get_pid();
2805 else
2806 off += 19;
2808 name = alloc((unsigned)STRLEN(p_mef) + 30);
2809 if (name == NULL)
2810 break;
2811 STRCPY(name, p_mef);
2812 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2813 STRCAT(name, p + 2);
2814 if (mch_getperm(name) < 0
2815 #ifdef HAVE_LSTAT
2816 /* Don't accept a symbolic link, its a security risk. */
2817 && mch_lstat((char *)name, &sb) < 0
2818 #endif
2820 break;
2821 vim_free(name);
2823 return name;
2827 * ":cc", ":crewind", ":cfirst" and ":clast".
2828 * ":ll", ":lrewind", ":lfirst" and ":llast".
2830 void
2831 ex_cc(eap)
2832 exarg_T *eap;
2834 qf_info_T *qi = &ql_info;
2836 if (eap->cmdidx == CMD_ll
2837 || eap->cmdidx == CMD_lrewind
2838 || eap->cmdidx == CMD_lfirst
2839 || eap->cmdidx == CMD_llast)
2841 qi = GET_LOC_LIST(curwin);
2842 if (qi == NULL)
2844 EMSG(_(e_loclist));
2845 return;
2849 qf_jump(qi, 0,
2850 eap->addr_count > 0
2851 ? (int)eap->line2
2852 : (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
2854 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
2855 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
2857 : 32767,
2858 eap->forceit);
2862 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
2863 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
2865 void
2866 ex_cnext(eap)
2867 exarg_T *eap;
2869 qf_info_T *qi = &ql_info;
2871 if (eap->cmdidx == CMD_lnext
2872 || eap->cmdidx == CMD_lNext
2873 || eap->cmdidx == CMD_lprevious
2874 || eap->cmdidx == CMD_lnfile
2875 || eap->cmdidx == CMD_lNfile
2876 || eap->cmdidx == CMD_lpfile)
2878 qi = GET_LOC_LIST(curwin);
2879 if (qi == NULL)
2881 EMSG(_(e_loclist));
2882 return;
2886 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext)
2887 ? FORWARD
2888 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile)
2889 ? FORWARD_FILE
2890 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
2891 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
2892 ? BACKWARD_FILE
2893 : BACKWARD,
2894 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
2898 * ":cfile"/":cgetfile"/":caddfile" commands.
2899 * ":lfile"/":lgetfile"/":laddfile" commands.
2901 void
2902 ex_cfile(eap)
2903 exarg_T *eap;
2905 win_T *wp = NULL;
2906 qf_info_T *qi = &ql_info;
2908 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
2909 || eap->cmdidx == CMD_laddfile)
2910 wp = curwin;
2912 if (*eap->arg != NUL)
2913 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
2916 * This function is used by the :cfile, :cgetfile and :caddfile
2917 * commands.
2918 * :cfile always creates a new quickfix list and jumps to the
2919 * first error.
2920 * :cgetfile creates a new quickfix list but doesn't jump to the
2921 * first error.
2922 * :caddfile adds to an existing quickfix list. If there is no
2923 * quickfix list then a new list is created.
2925 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
2926 && eap->cmdidx != CMD_laddfile)) > 0
2927 && (eap->cmdidx == CMD_cfile
2928 || eap->cmdidx == CMD_lfile))
2930 if (wp != NULL)
2931 qi = GET_LOC_LIST(wp);
2932 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
2937 * ":vimgrep {pattern} file(s)"
2938 * ":vimgrepadd {pattern} file(s)"
2939 * ":lvimgrep {pattern} file(s)"
2940 * ":lvimgrepadd {pattern} file(s)"
2942 void
2943 ex_vimgrep(eap)
2944 exarg_T *eap;
2946 regmmatch_T regmatch;
2947 int fcount;
2948 char_u **fnames;
2949 char_u *s;
2950 char_u *p;
2951 int fi;
2952 qf_info_T *qi = &ql_info;
2953 qfline_T *prevp = NULL;
2954 long lnum;
2955 buf_T *buf;
2956 int duplicate_name = FALSE;
2957 int using_dummy;
2958 int found_match;
2959 buf_T *first_match_buf = NULL;
2960 time_t seconds = 0;
2961 int save_mls;
2962 #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2963 char_u *save_ei = NULL;
2964 #endif
2965 aco_save_T aco;
2966 #ifdef FEAT_AUTOCMD
2967 char_u *au_name = NULL;
2968 int flags = 0;
2969 colnr_T col;
2970 long tomatch;
2972 switch (eap->cmdidx)
2974 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
2975 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
2976 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
2977 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
2978 default: break;
2980 if (au_name != NULL)
2982 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2983 curbuf->b_fname, TRUE, curbuf);
2984 if (did_throw || force_abort)
2985 return;
2987 #endif
2989 if (eap->cmdidx == CMD_lgrep
2990 || eap->cmdidx == CMD_lvimgrep
2991 || eap->cmdidx == CMD_lgrepadd
2992 || eap->cmdidx == CMD_lvimgrepadd)
2994 qi = ll_get_or_alloc_list(curwin);
2995 if (qi == NULL)
2996 return;
2999 if (eap->addr_count > 0)
3000 tomatch = eap->line2;
3001 else
3002 tomatch = MAXLNUM;
3004 /* Get the search pattern: either white-separated or enclosed in // */
3005 regmatch.regprog = NULL;
3006 p = skip_vimgrep_pat(eap->arg, &s, &flags);
3007 if (p == NULL)
3009 EMSG(_(e_invalpat));
3010 goto theend;
3012 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
3013 if (regmatch.regprog == NULL)
3014 goto theend;
3015 regmatch.rmm_ic = p_ic;
3016 regmatch.rmm_maxcol = 0;
3018 p = skipwhite(p);
3019 if (*p == NUL)
3021 EMSG(_("E683: File name missing or invalid pattern"));
3022 goto theend;
3025 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
3026 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
3027 || qi->qf_curlist == qi->qf_listcount)
3028 /* make place for a new list */
3029 qf_new_list(qi);
3030 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
3031 /* Adding to existing list, find last entry. */
3032 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
3033 prevp->qf_next != prevp; prevp = prevp->qf_next)
3036 /* parse the list of arguments */
3037 if (get_arglist_exp(p, &fcount, &fnames) == FAIL)
3038 goto theend;
3039 if (fcount == 0)
3041 EMSG(_(e_nomatch));
3042 goto theend;
3045 seconds = (time_t)0;
3046 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
3048 if (time(NULL) > seconds)
3050 /* Display the file name every second or so. */
3051 seconds = time(NULL);
3052 msg_start();
3053 p = msg_strtrunc(fnames[fi], TRUE);
3054 if (p == NULL)
3055 msg_outtrans(fnames[fi]);
3056 else
3058 msg_outtrans(p);
3059 vim_free(p);
3061 msg_clr_eos();
3062 msg_didout = FALSE; /* overwrite this message */
3063 msg_nowait = TRUE; /* don't wait for this message */
3064 msg_col = 0;
3065 out_flush();
3068 buf = buflist_findname_exp(fnames[fi]);
3069 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3071 /* Remember that a buffer with this name already exists. */
3072 duplicate_name = (buf != NULL);
3073 using_dummy = TRUE;
3075 #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3076 /* Don't do Filetype autocommands to avoid loading syntax and
3077 * indent scripts, a great speed improvement. */
3078 save_ei = au_event_disable(",Filetype");
3079 #endif
3080 /* Don't use modelines here, it's useless. */
3081 save_mls = p_mls;
3082 p_mls = 0;
3084 /* Load file into a buffer, so that 'fileencoding' is detected,
3085 * autocommands applied, etc. */
3086 buf = load_dummy_buffer(fnames[fi]);
3088 p_mls = save_mls;
3089 #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3090 au_event_restore(save_ei);
3091 #endif
3093 else
3094 /* Use existing, loaded buffer. */
3095 using_dummy = FALSE;
3097 if (buf == NULL)
3099 if (!got_int)
3100 smsg((char_u *)_("Cannot open file \"%s\""), fnames[fi]);
3102 else
3104 /* Try for a match in all lines of the buffer.
3105 * For ":1vimgrep" look for first match only. */
3106 found_match = FALSE;
3107 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3108 ++lnum)
3110 col = 0;
3111 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
3112 col) > 0)
3114 if (qf_add_entry(qi, &prevp,
3115 NULL, /* dir */
3116 fnames[fi],
3117 ml_get_buf(buf,
3118 regmatch.startpos[0].lnum + lnum, FALSE),
3119 regmatch.startpos[0].lnum + lnum,
3120 regmatch.startpos[0].col + 1,
3121 FALSE, /* vis_col */
3122 NULL, /* search pattern */
3123 0, /* nr */
3124 0, /* type */
3125 TRUE /* valid */
3126 ) == FAIL)
3128 got_int = TRUE;
3129 break;
3131 found_match = TRUE;
3132 if (--tomatch == 0)
3133 break;
3134 if ((flags & VGR_GLOBAL) == 0
3135 || regmatch.endpos[0].lnum > 0)
3136 break;
3137 col = regmatch.endpos[0].col
3138 + (col == regmatch.endpos[0].col);
3139 if (col > STRLEN(ml_get_buf(buf, lnum, FALSE)))
3140 break;
3142 line_breakcheck();
3143 if (got_int)
3144 break;
3147 if (using_dummy)
3149 if (found_match && first_match_buf == NULL)
3150 first_match_buf = buf;
3151 if (duplicate_name)
3153 /* Never keep a dummy buffer if there is another buffer
3154 * with the same name. */
3155 wipe_dummy_buffer(buf);
3156 buf = NULL;
3158 else if (!cmdmod.hide
3159 || buf->b_p_bh[0] == 'u' /* "unload" */
3160 || buf->b_p_bh[0] == 'w' /* "wipe" */
3161 || buf->b_p_bh[0] == 'd') /* "delete" */
3163 /* When no match was found we don't need to remember the
3164 * buffer, wipe it out. If there was a match and it
3165 * wasn't the first one or we won't jump there: only
3166 * unload the buffer.
3167 * Ignore 'hidden' here, because it may lead to having too
3168 * many swap files. */
3169 if (!found_match)
3171 wipe_dummy_buffer(buf);
3172 buf = NULL;
3174 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
3176 unload_dummy_buffer(buf);
3177 buf = NULL;
3181 if (buf != NULL)
3183 /* The buffer is still loaded, the Filetype autocommands
3184 * need to be done now, in that buffer. And the modelines
3185 * need to be done (again). But not the window-local
3186 * options! */
3187 aucmd_prepbuf(&aco, buf);
3188 #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3189 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3190 buf->b_fname, TRUE, buf);
3191 #endif
3192 do_modelines(OPT_NOWIN);
3193 aucmd_restbuf(&aco);
3199 FreeWild(fcount, fnames);
3201 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3202 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3203 qi->qf_lists[qi->qf_curlist].qf_index = 1;
3205 #ifdef FEAT_WINDOWS
3206 qf_update_buffer(qi);
3207 #endif
3209 #ifdef FEAT_AUTOCMD
3210 if (au_name != NULL)
3211 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3212 curbuf->b_fname, TRUE, curbuf);
3213 #endif
3215 /* Jump to first match. */
3216 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
3218 if ((flags & VGR_NOJUMP) == 0)
3219 qf_jump(qi, 0, 0, eap->forceit);
3221 else
3222 EMSG2(_(e_nomatch2), s);
3224 theend:
3225 vim_free(regmatch.regprog);
3229 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
3230 * Put the start of the pattern in "*s", unless "s" is NULL.
3231 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3232 * If "s" is not NULL terminate the pattern with a NUL.
3233 * Return a pointer to the char just past the pattern plus flags.
3235 char_u *
3236 skip_vimgrep_pat(p, s, flags)
3237 char_u *p;
3238 char_u **s;
3239 int *flags;
3241 int c;
3243 if (vim_isIDc(*p))
3245 /* ":vimgrep pattern fname" */
3246 if (s != NULL)
3247 *s = p;
3248 p = skiptowhite(p);
3249 if (s != NULL && *p != NUL)
3250 *p++ = NUL;
3252 else
3254 /* ":vimgrep /pattern/[g][j] fname" */
3255 if (s != NULL)
3256 *s = p + 1;
3257 c = *p;
3258 p = skip_regexp(p + 1, c, TRUE, NULL);
3259 if (*p != c)
3260 return NULL;
3262 /* Truncate the pattern. */
3263 if (s != NULL)
3264 *p = NUL;
3265 ++p;
3267 /* Find the flags */
3268 while (*p == 'g' || *p == 'j')
3270 if (flags != NULL)
3272 if (*p == 'g')
3273 *flags |= VGR_GLOBAL;
3274 else
3275 *flags |= VGR_NOJUMP;
3277 ++p;
3280 return p;
3284 * Load file "fname" into a dummy buffer and return the buffer pointer.
3285 * Returns NULL if it fails.
3286 * Must call unload_dummy_buffer() or wipe_dummy_buffer() later!
3288 static buf_T *
3289 load_dummy_buffer(fname)
3290 char_u *fname;
3292 buf_T *newbuf;
3293 int failed = TRUE;
3294 aco_save_T aco;
3296 /* Allocate a buffer without putting it in the buffer list. */
3297 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
3298 if (newbuf == NULL)
3299 return NULL;
3301 /* Init the options. */
3302 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
3304 /* set curwin/curbuf to buf and save a few things */
3305 aucmd_prepbuf(&aco, newbuf);
3307 /* Need to set the filename for autocommands. */
3308 (void)setfname(curbuf, fname, NULL, FALSE);
3310 if (ml_open(curbuf) == OK)
3312 /* Create swap file now to avoid the ATTENTION message. */
3313 check_need_swap(TRUE);
3315 /* Remove the "dummy" flag, otherwise autocommands may not
3316 * work. */
3317 curbuf->b_flags &= ~BF_DUMMY;
3319 if (readfile(fname, NULL,
3320 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
3321 NULL, READ_NEW | READ_DUMMY) == OK
3322 && !got_int
3323 && !(curbuf->b_flags & BF_NEW))
3325 failed = FALSE;
3326 if (curbuf != newbuf)
3328 /* Bloody autocommands changed the buffer! */
3329 if (buf_valid(newbuf))
3330 wipe_buffer(newbuf, FALSE);
3331 newbuf = curbuf;
3336 /* restore curwin/curbuf and a few other things */
3337 aucmd_restbuf(&aco);
3339 if (!buf_valid(newbuf))
3340 return NULL;
3341 if (failed)
3343 wipe_dummy_buffer(newbuf);
3344 return NULL;
3346 return newbuf;
3350 * Wipe out the dummy buffer that load_dummy_buffer() created.
3352 static void
3353 wipe_dummy_buffer(buf)
3354 buf_T *buf;
3356 if (curbuf != buf) /* safety check */
3358 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3359 cleanup_T cs;
3361 /* Reset the error/interrupt/exception state here so that aborting()
3362 * returns FALSE when wiping out the buffer. Otherwise it doesn't
3363 * work when got_int is set. */
3364 enter_cleanup(&cs);
3365 #endif
3367 wipe_buffer(buf, FALSE);
3369 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3370 /* Restore the error/interrupt/exception state if not discarded by a
3371 * new aborting error, interrupt, or uncaught exception. */
3372 leave_cleanup(&cs);
3373 #endif
3378 * Unload the dummy buffer that load_dummy_buffer() created.
3380 static void
3381 unload_dummy_buffer(buf)
3382 buf_T *buf;
3384 if (curbuf != buf) /* safety check */
3385 close_buffer(NULL, buf, DOBUF_UNLOAD);
3388 #if defined(FEAT_EVAL) || defined(PROTO)
3390 * Add each quickfix error to list "list" as a dictionary.
3393 get_errorlist(wp, list)
3394 win_T *wp;
3395 list_T *list;
3397 qf_info_T *qi = &ql_info;
3398 dict_T *dict;
3399 char_u buf[2];
3400 qfline_T *qfp;
3401 int i;
3403 if (wp != NULL)
3405 qi = GET_LOC_LIST(wp);
3406 if (qi == NULL)
3407 return FAIL;
3410 if (qi->qf_curlist >= qi->qf_listcount
3411 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
3412 return FAIL;
3414 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3415 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
3417 if ((dict = dict_alloc()) == NULL)
3418 return FAIL;
3419 if (list_append_dict(list, dict) == FAIL)
3420 return FAIL;
3422 buf[0] = qfp->qf_type;
3423 buf[1] = NUL;
3424 if ( dict_add_nr_str(dict, "bufnr", (long)qfp->qf_fnum, NULL) == FAIL
3425 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
3426 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
3427 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
3428 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
3429 || dict_add_nr_str(dict, "pattern", 0L, qfp->qf_pattern) == FAIL
3430 || dict_add_nr_str(dict, "text", 0L, qfp->qf_text) == FAIL
3431 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
3432 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
3433 return FAIL;
3435 qfp = qfp->qf_next;
3437 return OK;
3441 * Populate the quickfix list with the items supplied in the list
3442 * of dictionaries.
3445 set_errorlist(wp, list, action)
3446 win_T *wp;
3447 list_T *list;
3448 int action;
3450 listitem_T *li;
3451 dict_T *d;
3452 char_u *filename, *pattern, *text, *type;
3453 long lnum;
3454 int col, nr;
3455 int vcol;
3456 qfline_T *prevp = NULL;
3457 int valid, status;
3458 int retval = OK;
3459 qf_info_T *qi = &ql_info;
3461 if (wp != NULL)
3463 qi = ll_get_or_alloc_list(wp);
3464 if (qi == NULL)
3465 return FAIL;
3468 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
3469 /* make place for a new list */
3470 qf_new_list(qi);
3471 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
3472 /* Adding to existing list, find last entry. */
3473 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
3474 prevp->qf_next != prevp; prevp = prevp->qf_next)
3476 else if (action == 'r')
3477 qf_free(qi, qi->qf_curlist);
3479 for (li = list->lv_first; li != NULL; li = li->li_next)
3481 if (li->li_tv.v_type != VAR_DICT)
3482 continue; /* Skip non-dict items */
3484 d = li->li_tv.vval.v_dict;
3485 if (d == NULL)
3486 continue;
3488 filename = get_dict_string(d, (char_u *)"filename", TRUE);
3489 lnum = get_dict_number(d, (char_u *)"lnum");
3490 col = get_dict_number(d, (char_u *)"col");
3491 vcol = get_dict_number(d, (char_u *)"vcol");
3492 nr = get_dict_number(d, (char_u *)"nr");
3493 type = get_dict_string(d, (char_u *)"type", TRUE);
3494 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
3495 text = get_dict_string(d, (char_u *)"text", TRUE);
3496 if (text == NULL)
3497 text = vim_strsave((char_u *)"");
3499 valid = TRUE;
3500 if (filename == NULL || (lnum == 0 && pattern == NULL))
3501 valid = FALSE;
3503 status = qf_add_entry(qi, &prevp,
3504 NULL, /* dir */
3505 filename,
3506 text,
3507 lnum,
3508 col,
3509 vcol, /* vis_col */
3510 pattern, /* search pattern */
3512 type == NULL ? NUL : *type,
3513 valid);
3515 vim_free(filename);
3516 vim_free(pattern);
3517 vim_free(text);
3518 vim_free(type);
3520 if (status == FAIL)
3522 retval = FAIL;
3523 break;
3527 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3528 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3529 qi->qf_lists[qi->qf_curlist].qf_index = 1;
3531 #ifdef FEAT_WINDOWS
3532 qf_update_buffer(qi);
3533 #endif
3535 return retval;
3537 #endif
3540 * ":[range]cbuffer [bufnr]" command.
3541 * ":[range]caddbuffer [bufnr]" command.
3542 * ":[range]cgetbuffer [bufnr]" command.
3543 * ":[range]lbuffer [bufnr]" command.
3544 * ":[range]laddbuffer [bufnr]" command.
3545 * ":[range]lgetbuffer [bufnr]" command.
3547 void
3548 ex_cbuffer(eap)
3549 exarg_T *eap;
3551 buf_T *buf = NULL;
3552 qf_info_T *qi = &ql_info;
3554 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
3555 || eap->cmdidx == CMD_laddbuffer)
3557 qi = ll_get_or_alloc_list(curwin);
3558 if (qi == NULL)
3559 return;
3562 if (*eap->arg == NUL)
3563 buf = curbuf;
3564 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
3565 buf = buflist_findnr(atoi((char *)eap->arg));
3566 if (buf == NULL)
3567 EMSG(_(e_invarg));
3568 else if (buf->b_ml.ml_mfp == NULL)
3569 EMSG(_("E681: Buffer is not loaded"));
3570 else
3572 if (eap->addr_count == 0)
3574 eap->line1 = 1;
3575 eap->line2 = buf->b_ml.ml_line_count;
3577 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
3578 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
3579 EMSG(_(e_invrange));
3580 else
3582 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
3583 (eap->cmdidx != CMD_caddbuffer
3584 && eap->cmdidx != CMD_laddbuffer),
3585 eap->line1, eap->line2) > 0
3586 && (eap->cmdidx == CMD_cbuffer
3587 || eap->cmdidx == CMD_lbuffer))
3588 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
3593 #if defined(FEAT_EVAL) || defined(PROTO)
3595 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
3596 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
3598 void
3599 ex_cexpr(eap)
3600 exarg_T *eap;
3602 typval_T *tv;
3603 qf_info_T *qi = &ql_info;
3605 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
3606 || eap->cmdidx == CMD_laddexpr)
3608 qi = ll_get_or_alloc_list(curwin);
3609 if (qi == NULL)
3610 return;
3613 /* Evaluate the expression. When the result is a string or a list we can
3614 * use it to fill the errorlist. */
3615 tv = eval_expr(eap->arg, NULL);
3616 if (tv != NULL)
3618 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
3619 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
3621 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
3622 (eap->cmdidx != CMD_caddexpr
3623 && eap->cmdidx != CMD_laddexpr),
3624 (linenr_T)0, (linenr_T)0) > 0
3625 && (eap->cmdidx == CMD_cexpr
3626 || eap->cmdidx == CMD_lexpr))
3627 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
3629 else
3630 EMSG(_("E777: String or List expected"));
3631 free_tv(tv);
3634 #endif
3637 * ":helpgrep {pattern}"
3639 void
3640 ex_helpgrep(eap)
3641 exarg_T *eap;
3643 regmatch_T regmatch;
3644 char_u *save_cpo;
3645 char_u *p;
3646 int fcount;
3647 char_u **fnames;
3648 FILE *fd;
3649 int fi;
3650 qfline_T *prevp = NULL;
3651 long lnum;
3652 #ifdef FEAT_MULTI_LANG
3653 char_u *lang;
3654 #endif
3655 qf_info_T *qi = &ql_info;
3656 int new_qi = FALSE;
3657 win_T *wp;
3659 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
3660 save_cpo = p_cpo;
3661 p_cpo = (char_u *)"";
3663 #ifdef FEAT_MULTI_LANG
3664 /* Check for a specified language */
3665 lang = check_help_lang(eap->arg);
3666 #endif
3668 if (eap->cmdidx == CMD_lhelpgrep)
3670 /* Find an existing help window */
3671 FOR_ALL_WINDOWS(wp)
3672 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
3673 break;
3675 if (wp == NULL) /* Help window not found */
3676 qi = NULL;
3677 else
3678 qi = wp->w_llist;
3680 if (qi == NULL)
3682 /* Allocate a new location list for help text matches */
3683 if ((qi = ll_new_list()) == NULL)
3684 return;
3685 new_qi = TRUE;
3689 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
3690 regmatch.rm_ic = FALSE;
3691 if (regmatch.regprog != NULL)
3693 /* create a new quickfix list */
3694 qf_new_list(qi);
3696 /* Go through all directories in 'runtimepath' */
3697 p = p_rtp;
3698 while (*p != NUL && !got_int)
3700 copy_option_part(&p, NameBuff, MAXPATHL, ",");
3702 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
3703 add_pathsep(NameBuff);
3704 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
3705 if (gen_expand_wildcards(1, &NameBuff, &fcount,
3706 &fnames, EW_FILE|EW_SILENT) == OK
3707 && fcount > 0)
3709 for (fi = 0; fi < fcount && !got_int; ++fi)
3711 #ifdef FEAT_MULTI_LANG
3712 /* Skip files for a different language. */
3713 if (lang != NULL
3714 && STRNICMP(lang, fnames[fi]
3715 + STRLEN(fnames[fi]) - 3, 2) != 0
3716 && !(STRNICMP(lang, "en", 2) == 0
3717 && STRNICMP("txt", fnames[fi]
3718 + STRLEN(fnames[fi]) - 3, 3) == 0))
3719 continue;
3720 #endif
3721 fd = mch_fopen((char *)fnames[fi], "r");
3722 if (fd != NULL)
3724 lnum = 1;
3725 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
3727 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
3729 int l = (int)STRLEN(IObuff);
3731 /* remove trailing CR, LF, spaces, etc. */
3732 while (l > 0 && IObuff[l - 1] <= ' ')
3733 IObuff[--l] = NUL;
3735 if (qf_add_entry(qi, &prevp,
3736 NULL, /* dir */
3737 fnames[fi],
3738 IObuff,
3739 lnum,
3740 (int)(regmatch.startp[0] - IObuff)
3741 + 1, /* col */
3742 FALSE, /* vis_col */
3743 NULL, /* search pattern */
3744 0, /* nr */
3745 1, /* type */
3746 TRUE /* valid */
3747 ) == FAIL)
3749 got_int = TRUE;
3750 break;
3753 ++lnum;
3754 line_breakcheck();
3756 fclose(fd);
3759 FreeWild(fcount, fnames);
3762 vim_free(regmatch.regprog);
3764 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3765 qi->qf_lists[qi->qf_curlist].qf_ptr =
3766 qi->qf_lists[qi->qf_curlist].qf_start;
3767 qi->qf_lists[qi->qf_curlist].qf_index = 1;
3770 p_cpo = save_cpo;
3772 #ifdef FEAT_WINDOWS
3773 qf_update_buffer(qi);
3774 #endif
3776 /* Jump to first match. */
3777 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
3778 qf_jump(qi, 0, 0, FALSE);
3779 else
3780 EMSG2(_(e_nomatch2), eap->arg);
3782 if (eap->cmdidx == CMD_lhelpgrep)
3784 /* If the help window is not opened or if it already points to the
3785 * correct location list, then free the new location list. */
3786 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
3788 if (new_qi)
3789 ll_free_all(&qi);
3791 else if (curwin->w_llist == NULL)
3792 curwin->w_llist = qi;
3796 #endif /* FEAT_QUICKFIX */