Merge branch 'feat/tagfunc'
[vim_extended.git] / src / quickfix.c
blobad10370802ad73276e492fdb940b7a589397311e
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 char *qf_title; /* title derived from the command that created
60 * the error list
62 char *qf_user_title; /* title specified by the :csettitle or
63 * :lsettitle command
65 int qf_use_user_title; /* indicates that qf_user_title should
66 * be used instead of qf_title
68 } qf_list_T;
70 struct qf_info_S
73 * Count of references to this list. Used only for location lists.
74 * When a location list window reference this list, qf_refcount
75 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
76 * reaches 0, the list is freed.
78 int qf_refcount;
79 int qf_listcount; /* current number of lists */
80 int qf_curlist; /* current error list */
81 qf_list_T qf_lists[LISTCOUNT];
84 static qf_info_T ql_info; /* global quickfix list */
86 #define FMT_PATTERNS 10 /* maximum number of % recognized */
89 * Structure used to hold the info of one part of 'errorformat'
91 typedef struct efm_S efm_T;
92 struct efm_S
94 regprog_T *prog; /* pre-formatted part of 'errorformat' */
95 efm_T *next; /* pointer to next (NULL if last) */
96 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
97 char_u prefix; /* prefix of this format line: */
98 /* 'D' enter directory */
99 /* 'X' leave directory */
100 /* 'A' start of multi-line message */
101 /* 'E' error message */
102 /* 'W' warning message */
103 /* 'I' informational message */
104 /* 'C' continuation line */
105 /* 'Z' end of multi-line message */
106 /* 'G' general, unspecific message */
107 /* 'P' push file (partial) message */
108 /* 'Q' pop/quit file (partial) message */
109 /* 'O' overread (partial) message */
110 char_u flags; /* additional flags given in prefix */
111 /* '-' do not include this line */
112 /* '+' include whole line in message */
113 int conthere; /* %> used */
116 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, char *qf_title));
117 static void qf_new_list __ARGS((qf_info_T *qi, char *qf_title));
118 static void ll_free_all __ARGS((qf_info_T **pqi));
119 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));
120 static qf_info_T *ll_new_list __ARGS((void));
121 static void qf_msg __ARGS((qf_info_T *qi));
122 static void qf_free __ARGS((qf_info_T *qi, int idx));
123 static char_u *qf_types __ARGS((int, int));
124 static int qf_get_fnum __ARGS((char_u *, char_u *));
125 static char_u *qf_push_dir __ARGS((char_u *, struct dir_stack_T **));
126 static char_u *qf_pop_dir __ARGS((struct dir_stack_T **));
127 static char_u *qf_guess_filepath __ARGS((char_u *));
128 static void qf_fmt_text __ARGS((char_u *text, char_u *buf, int bufsize));
129 static void qf_clean_dir_stack __ARGS((struct dir_stack_T **));
130 #ifdef FEAT_WINDOWS
131 static int qf_win_pos_update __ARGS((qf_info_T *qi, int old_qf_index));
132 static int is_qf_win __ARGS((win_T *win, qf_info_T *qi));
133 static win_T *qf_find_win __ARGS((qf_info_T *qi));
134 static buf_T *qf_find_buf __ARGS((qf_info_T *qi));
135 static void qf_update_buffer __ARGS((qf_info_T *qi));
136 static void qf_fill_buffer __ARGS((qf_info_T *qi));
137 #endif
138 static char_u *get_mef_name __ARGS((void));
139 static buf_T *load_dummy_buffer __ARGS((char_u *fname));
140 static void wipe_dummy_buffer __ARGS((buf_T *buf));
141 static void unload_dummy_buffer __ARGS((buf_T *buf));
142 static qf_info_T *ll_get_or_alloc_list __ARGS((win_T *));
144 /* Quickfix window check helper macro */
145 #define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
146 /* Location list window check helper macro */
147 #define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
149 * Return location list for window 'wp'
150 * For location list window, return the referenced location list
152 #define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
155 * Read the errorfile "efile" into memory, line by line, building the error
156 * list. Set the error list's title to qf_title.
157 * Return -1 for error, number of errors for success.
160 qf_init(wp, efile, errorformat, newlist, qf_title)
161 win_T *wp;
162 char_u *efile;
163 char_u *errorformat;
164 int newlist; /* TRUE: start a new error list */
165 char *qf_title;
167 qf_info_T *qi = &ql_info;
169 if (efile == NULL)
170 return FAIL;
172 if (wp != NULL)
174 qi = ll_get_or_alloc_list(wp);
175 if (qi == NULL)
176 return FAIL;
179 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
180 (linenr_T)0, (linenr_T)0,
181 qf_title);
185 * Returns the title of the qf_info_T's current qf_list_T.
187 char*
188 qf_get_name(qi)
189 qf_info_T *qi;
191 if (!qi)
192 qi = &ql_info;
194 if (qi->qf_lists[qi->qf_curlist].qf_use_user_title)
195 return qi->qf_lists[qi->qf_curlist].qf_user_title;
197 return qi->qf_lists[qi->qf_curlist].qf_title;
201 * Sets the title of a quickfix window / location list window.
203 void
204 ex_csettitle(eap)
205 exarg_T *eap;
207 qf_info_T *list_ref;
208 win_T *win;
209 char_u *err_msg;
211 if (eap->cmdidx == CMD_csettitle)
213 list_ref = &ql_info;
214 err_msg = e_quickfix;
216 else
218 list_ref = GET_LOC_LIST(curwin);
219 err_msg = e_loclist;
222 if (!list_ref || !list_ref->qf_listcount)
224 EMSG(_(err_msg));
225 return;
228 if (eap->forceit)
230 if (!list_ref->qf_lists[list_ref->qf_curlist].qf_use_user_title)
231 return;
233 /* restore the title derived from the command that created the error list */
234 vim_free(list_ref->qf_lists[list_ref->qf_curlist].qf_user_title);
235 list_ref->qf_lists[list_ref->qf_curlist].qf_user_title = NULL;
236 list_ref->qf_lists[list_ref->qf_curlist].qf_use_user_title = FALSE;
238 else
240 /* set the user's title */
241 vim_free(list_ref->qf_lists[list_ref->qf_curlist].qf_user_title);
243 if (eap->arg && *eap->arg != NUL)
244 list_ref->qf_lists[list_ref->qf_curlist].qf_user_title = (char *)vim_strsave(eap->arg);
245 else
246 list_ref->qf_lists[list_ref->qf_curlist].qf_user_title = NULL;
248 list_ref->qf_lists[list_ref->qf_curlist].qf_use_user_title = TRUE;
251 if (eap->cmdidx == CMD_csettitle)
252 list_ref = NULL;
254 need_maketitle = TRUE;
255 FOR_ALL_WINDOWS(win)
257 if (bt_quickfix(win->w_buffer) && list_ref == win->w_llist_ref)
258 win_redr_status(win);
263 * Read the errorfile "efile" into memory, line by line, building the error
264 * list.
265 * Alternative: when "efile" is null read errors from buffer "buf".
266 * Always use 'errorformat' from "buf" if there is a local value.
267 * Then lnumfirst and lnumlast specify the range of lines to use.
268 * Set the title of the list to qf_title.
269 * Return -1 for error, number of errors for success.
271 static int
272 qf_init_ext(qi, efile, buf, tv, errorformat, newlist, lnumfirst, lnumlast, qf_title)
273 qf_info_T *qi;
274 char_u *efile;
275 buf_T *buf;
276 typval_T *tv;
277 char_u *errorformat;
278 int newlist; /* TRUE: start a new error list */
279 linenr_T lnumfirst; /* first line number to use */
280 linenr_T lnumlast; /* last line number to use */
281 char *qf_title;
283 char_u *namebuf;
284 char_u *errmsg;
285 char_u *pattern;
286 char_u *fmtstr = NULL;
287 int col = 0;
288 char_u use_viscol = FALSE;
289 int type = 0;
290 int valid;
291 linenr_T buflnum = lnumfirst;
292 long lnum = 0L;
293 int enr = 0;
294 FILE *fd = NULL;
295 qfline_T *qfprev = NULL; /* init to make SASC shut up */
296 char_u *efmp;
297 efm_T *fmt_first = NULL;
298 efm_T *fmt_last = NULL;
299 efm_T *fmt_ptr;
300 efm_T *fmt_start = NULL;
301 char_u *efm;
302 char_u *ptr;
303 char_u *srcptr;
304 int len;
305 int i;
306 int round;
307 int idx = 0;
308 int multiline = FALSE;
309 int multiignore = FALSE;
310 int multiscan = FALSE;
311 int retval = -1; /* default: return error flag */
312 char_u *directory = NULL;
313 char_u *currfile = NULL;
314 char_u *tail = NULL;
315 char_u *p_str = NULL;
316 listitem_T *p_li = NULL;
317 struct dir_stack_T *file_stack = NULL;
318 regmatch_T regmatch;
319 static struct fmtpattern
321 char_u convchar;
322 char *pattern;
323 } fmt_pat[FMT_PATTERNS] =
325 {'f', ".\\+"}, /* only used when at end */
326 {'n', "\\d\\+"},
327 {'l', "\\d\\+"},
328 {'c', "\\d\\+"},
329 {'t', "."},
330 {'m', ".\\+"},
331 {'r', ".*"},
332 {'p', "[- .]*"},
333 {'v', "\\d\\+"},
334 {'s', ".\\+"}
337 namebuf = alloc(CMDBUFFSIZE + 1);
338 errmsg = alloc(CMDBUFFSIZE + 1);
339 pattern = alloc(CMDBUFFSIZE + 1);
340 if (namebuf == NULL || errmsg == NULL || pattern == NULL)
341 goto qf_init_end;
343 if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
345 EMSG2(_(e_openerrf), efile);
346 goto qf_init_end;
349 if (newlist || qi->qf_curlist == qi->qf_listcount)
350 /* make place for a new list */
351 qf_new_list(qi, qf_title);
352 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
353 /* Adding to existing list, find last entry. */
354 for (qfprev = qi->qf_lists[qi->qf_curlist].qf_start;
355 qfprev->qf_next != qfprev;
356 qfprev = qfprev->qf_next)
357 { /* do nothing */ }
360 * Each part of the format string is copied and modified from errorformat to
361 * regex prog. Only a few % characters are allowed.
363 /* Use the local value of 'errorformat' if it's set. */
364 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
365 efm = buf->b_p_efm;
366 else
367 efm = errorformat;
369 * Get some space to modify the format string into.
371 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
372 for (round = FMT_PATTERNS; round > 0; )
373 i += (int)STRLEN(fmt_pat[--round].pattern);
374 #ifdef COLON_IN_FILENAME
375 i += 12; /* "%f" can become twelve chars longer */
376 #else
377 i += 2; /* "%f" can become two chars longer */
378 #endif
379 if ((fmtstr = alloc(i)) == NULL)
380 goto error2;
382 while (efm[0] != NUL)
385 * Allocate a new eformat structure and put it at the end of the list
387 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
388 if (fmt_ptr == NULL)
389 goto error2;
390 if (fmt_first == NULL) /* first one */
391 fmt_first = fmt_ptr;
392 else
393 fmt_last->next = fmt_ptr;
394 fmt_last = fmt_ptr;
397 * Isolate one part in the 'errorformat' option
399 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
400 if (efm[len] == '\\' && efm[len + 1] != NUL)
401 ++len;
404 * Build regexp pattern from current 'errorformat' option
406 ptr = fmtstr;
407 *ptr++ = '^';
408 round = 0;
409 for (efmp = efm; efmp < efm + len; ++efmp)
411 if (*efmp == '%')
413 ++efmp;
414 for (idx = 0; idx < FMT_PATTERNS; ++idx)
415 if (fmt_pat[idx].convchar == *efmp)
416 break;
417 if (idx < FMT_PATTERNS)
419 if (fmt_ptr->addr[idx])
421 sprintf((char *)errmsg,
422 _("E372: Too many %%%c in format string"), *efmp);
423 EMSG(errmsg);
424 goto error2;
426 if ((idx
427 && idx < 6
428 && vim_strchr((char_u *)"DXOPQ",
429 fmt_ptr->prefix) != NULL)
430 || (idx == 6
431 && vim_strchr((char_u *)"OPQ",
432 fmt_ptr->prefix) == NULL))
434 sprintf((char *)errmsg,
435 _("E373: Unexpected %%%c in format string"), *efmp);
436 EMSG(errmsg);
437 goto error2;
439 fmt_ptr->addr[idx] = (char_u)++round;
440 *ptr++ = '\\';
441 *ptr++ = '(';
442 #ifdef BACKSLASH_IN_FILENAME
443 if (*efmp == 'f')
445 /* Also match "c:" in the file name, even when
446 * checking for a colon next: "%f:".
447 * "\%(\a:\)\=" */
448 STRCPY(ptr, "\\%(\\a:\\)\\=");
449 ptr += 10;
451 #endif
452 if (*efmp == 'f' && efmp[1] != NUL)
454 if (efmp[1] != '\\' && efmp[1] != '%')
456 /* A file name may contain spaces, but this isn't
457 * in "\f". For "%f:%l:%m" there may be a ":" in
458 * the file name. Use ".\{-1,}x" instead (x is
459 * the next character), the requirement that :999:
460 * follows should work. */
461 STRCPY(ptr, ".\\{-1,}");
462 ptr += 7;
464 else
466 /* File name followed by '\\' or '%': include as
467 * many file name chars as possible. */
468 STRCPY(ptr, "\\f\\+");
469 ptr += 4;
472 else
474 srcptr = (char_u *)fmt_pat[idx].pattern;
475 while ((*ptr = *srcptr++) != NUL)
476 ++ptr;
478 *ptr++ = '\\';
479 *ptr++ = ')';
481 else if (*efmp == '*')
483 if (*++efmp == '[' || *efmp == '\\')
485 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
487 if (efmp[1] == '^')
488 *ptr++ = *++efmp;
489 if (efmp < efm + len)
491 *ptr++ = *++efmp; /* could be ']' */
492 while (efmp < efm + len
493 && (*ptr++ = *++efmp) != ']')
494 /* skip */;
495 if (efmp == efm + len)
497 EMSG(_("E374: Missing ] in format string"));
498 goto error2;
502 else if (efmp < efm + len) /* %*\D, %*\s etc. */
503 *ptr++ = *++efmp;
504 *ptr++ = '\\';
505 *ptr++ = '+';
507 else
509 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
510 sprintf((char *)errmsg,
511 _("E375: Unsupported %%%c in format string"), *efmp);
512 EMSG(errmsg);
513 goto error2;
516 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
517 *ptr++ = *efmp; /* regexp magic characters */
518 else if (*efmp == '#')
519 *ptr++ = '*';
520 else if (*efmp == '>')
521 fmt_ptr->conthere = TRUE;
522 else if (efmp == efm + 1) /* analyse prefix */
524 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
525 fmt_ptr->flags = *efmp++;
526 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
527 fmt_ptr->prefix = *efmp;
528 else
530 sprintf((char *)errmsg,
531 _("E376: Invalid %%%c in format string prefix"), *efmp);
532 EMSG(errmsg);
533 goto error2;
536 else
538 sprintf((char *)errmsg,
539 _("E377: Invalid %%%c in format string"), *efmp);
540 EMSG(errmsg);
541 goto error2;
544 else /* copy normal character */
546 if (*efmp == '\\' && efmp + 1 < efm + len)
547 ++efmp;
548 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
549 *ptr++ = '\\'; /* escape regexp atoms */
550 if (*efmp)
551 *ptr++ = *efmp;
554 *ptr++ = '$';
555 *ptr = NUL;
556 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
557 goto error2;
559 * Advance to next part
561 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
563 if (fmt_first == NULL) /* nothing found */
565 EMSG(_("E378: 'errorformat' contains no pattern"));
566 goto error2;
570 * got_int is reset here, because it was probably set when killing the
571 * ":make" command, but we still want to read the errorfile then.
573 got_int = FALSE;
575 /* Always ignore case when looking for a matching error. */
576 regmatch.rm_ic = TRUE;
578 if (tv != NULL)
580 if (tv->v_type == VAR_STRING)
581 p_str = tv->vval.v_string;
582 else if (tv->v_type == VAR_LIST)
583 p_li = tv->vval.v_list->lv_first;
587 * Read the lines in the error file one by one.
588 * Try to recognize one of the error formats in each line.
590 while (!got_int)
592 /* Get the next line. */
593 if (fd == NULL)
595 if (tv != NULL)
597 if (tv->v_type == VAR_STRING)
599 /* Get the next line from the supplied string */
600 char_u *p;
602 if (!*p_str) /* Reached the end of the string */
603 break;
605 p = vim_strchr(p_str, '\n');
606 if (p)
607 len = (int)(p - p_str + 1);
608 else
609 len = (int)STRLEN(p_str);
611 if (len > CMDBUFFSIZE - 2)
612 vim_strncpy(IObuff, p_str, CMDBUFFSIZE - 2);
613 else
614 vim_strncpy(IObuff, p_str, len);
616 p_str += len;
618 else if (tv->v_type == VAR_LIST)
620 /* Get the next line from the supplied list */
621 while (p_li && p_li->li_tv.v_type != VAR_STRING)
622 p_li = p_li->li_next; /* Skip non-string items */
624 if (!p_li) /* End of the list */
625 break;
627 len = (int)STRLEN(p_li->li_tv.vval.v_string);
628 if (len > CMDBUFFSIZE - 2)
629 len = CMDBUFFSIZE - 2;
631 vim_strncpy(IObuff, p_li->li_tv.vval.v_string, len);
633 p_li = p_li->li_next; /* next item */
636 else
638 /* Get the next line from the supplied buffer */
639 if (buflnum > lnumlast)
640 break;
641 vim_strncpy(IObuff, ml_get_buf(buf, buflnum++, FALSE),
642 CMDBUFFSIZE - 2);
645 else if (fgets((char *)IObuff, CMDBUFFSIZE - 2, fd) == NULL)
646 break;
648 IObuff[CMDBUFFSIZE - 2] = NUL; /* for very long lines */
649 if ((efmp = vim_strrchr(IObuff, '\n')) != NULL)
650 *efmp = NUL;
651 #ifdef USE_CRNL
652 if ((efmp = vim_strrchr(IObuff, '\r')) != NULL)
653 *efmp = NUL;
654 #endif
656 /* If there was no %> item start at the first pattern */
657 if (fmt_start == NULL)
658 fmt_ptr = fmt_first;
659 else
661 fmt_ptr = fmt_start;
662 fmt_start = NULL;
666 * Try to match each part of 'errorformat' until we find a complete
667 * match or no match.
669 valid = TRUE;
670 restofline:
671 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
673 idx = fmt_ptr->prefix;
674 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
675 continue;
676 namebuf[0] = NUL;
677 pattern[0] = NUL;
678 if (!multiscan)
679 errmsg[0] = NUL;
680 lnum = 0;
681 col = 0;
682 use_viscol = FALSE;
683 enr = -1;
684 type = 0;
685 tail = NULL;
687 regmatch.regprog = fmt_ptr->prog;
688 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
690 if ((idx == 'C' || idx == 'Z') && !multiline)
691 continue;
692 if (vim_strchr((char_u *)"EWI", idx) != NULL)
693 type = idx;
694 else
695 type = 0;
697 * Extract error message data from matched line.
698 * We check for an actual submatch, because "\[" and "\]" in
699 * the 'errorformat' may cause the wrong submatch to be used.
701 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
703 int c;
705 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
706 continue;
708 /* Expand ~/file and $HOME/file to full path. */
709 c = *regmatch.endp[i];
710 *regmatch.endp[i] = NUL;
711 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
712 *regmatch.endp[i] = c;
714 if (vim_strchr((char_u *)"OPQ", idx) != NULL
715 && mch_getperm(namebuf) == -1)
716 continue;
718 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
720 if (regmatch.startp[i] == NULL)
721 continue;
722 enr = (int)atol((char *)regmatch.startp[i]);
724 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
726 if (regmatch.startp[i] == NULL)
727 continue;
728 lnum = atol((char *)regmatch.startp[i]);
730 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
732 if (regmatch.startp[i] == NULL)
733 continue;
734 col = (int)atol((char *)regmatch.startp[i]);
736 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
738 if (regmatch.startp[i] == NULL)
739 continue;
740 type = *regmatch.startp[i];
742 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
743 STRCPY(errmsg, IObuff);
744 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
746 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
747 continue;
748 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
749 vim_strncpy(errmsg, regmatch.startp[i], len);
751 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
753 if (regmatch.startp[i] == NULL)
754 continue;
755 tail = regmatch.startp[i];
757 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
759 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
760 continue;
761 col = (int)(regmatch.endp[i] - regmatch.startp[i] + 1);
762 if (*((char_u *)regmatch.startp[i]) != TAB)
763 use_viscol = TRUE;
765 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
767 if (regmatch.startp[i] == NULL)
768 continue;
769 col = (int)atol((char *)regmatch.startp[i]);
770 use_viscol = TRUE;
772 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
774 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
775 continue;
776 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
777 if (len > CMDBUFFSIZE - 5)
778 len = CMDBUFFSIZE - 5;
779 STRCPY(pattern, "^\\V");
780 STRNCAT(pattern, regmatch.startp[i], len);
781 pattern[len + 3] = '\\';
782 pattern[len + 4] = '$';
783 pattern[len + 5] = NUL;
785 break;
788 multiscan = FALSE;
790 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
792 if (fmt_ptr != NULL)
794 if (idx == 'D') /* enter directory */
796 if (*namebuf == NUL)
798 EMSG(_("E379: Missing or empty directory name"));
799 goto error2;
801 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
802 goto error2;
804 else if (idx == 'X') /* leave directory */
805 directory = qf_pop_dir(&dir_stack);
807 namebuf[0] = NUL; /* no match found, remove file name */
808 lnum = 0; /* don't jump to this line */
809 valid = FALSE;
810 STRCPY(errmsg, IObuff); /* copy whole line to error message */
811 if (fmt_ptr == NULL)
812 multiline = multiignore = FALSE;
814 else if (fmt_ptr != NULL)
816 /* honor %> item */
817 if (fmt_ptr->conthere)
818 fmt_start = fmt_ptr;
820 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
821 multiline = TRUE; /* start of a multi-line message */
822 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
823 { /* continuation of multi-line msg */
824 if (qfprev == NULL)
825 goto error2;
826 if (*errmsg && !multiignore)
828 len = (int)STRLEN(qfprev->qf_text);
829 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
830 == NULL)
831 goto error2;
832 STRCPY(ptr, qfprev->qf_text);
833 vim_free(qfprev->qf_text);
834 qfprev->qf_text = ptr;
835 *(ptr += len) = '\n';
836 STRCPY(++ptr, errmsg);
838 if (qfprev->qf_nr == -1)
839 qfprev->qf_nr = enr;
840 if (vim_isprintc(type) && !qfprev->qf_type)
841 qfprev->qf_type = type; /* only printable chars allowed */
842 if (!qfprev->qf_lnum)
843 qfprev->qf_lnum = lnum;
844 if (!qfprev->qf_col)
845 qfprev->qf_col = col;
846 qfprev->qf_viscol = use_viscol;
847 if (!qfprev->qf_fnum)
848 qfprev->qf_fnum = qf_get_fnum(directory,
849 *namebuf || directory ? namebuf
850 : currfile && valid ? currfile : 0);
851 if (idx == 'Z')
852 multiline = multiignore = FALSE;
853 line_breakcheck();
854 continue;
856 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
858 /* global file names */
859 valid = FALSE;
860 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
862 if (*namebuf && idx == 'P')
863 currfile = qf_push_dir(namebuf, &file_stack);
864 else if (idx == 'Q')
865 currfile = qf_pop_dir(&file_stack);
866 *namebuf = NUL;
867 if (tail && *tail)
869 STRMOVE(IObuff, skipwhite(tail));
870 multiscan = TRUE;
871 goto restofline;
875 if (fmt_ptr->flags == '-') /* generally exclude this line */
877 if (multiline)
878 multiignore = TRUE; /* also exclude continuation lines */
879 continue;
883 if (qf_add_entry(qi, &qfprev,
884 directory,
885 (*namebuf || directory)
886 ? namebuf
887 : ((currfile && valid) ? currfile : (char_u *)NULL),
889 errmsg,
890 lnum,
891 col,
892 use_viscol,
893 pattern,
894 enr,
895 type,
896 valid) == FAIL)
897 goto error2;
898 line_breakcheck();
900 if (fd == NULL || !ferror(fd))
902 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
904 /* no valid entry found */
905 qi->qf_lists[qi->qf_curlist].qf_ptr =
906 qi->qf_lists[qi->qf_curlist].qf_start;
907 qi->qf_lists[qi->qf_curlist].qf_index = 1;
908 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
910 else
912 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
913 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
914 qi->qf_lists[qi->qf_curlist].qf_ptr =
915 qi->qf_lists[qi->qf_curlist].qf_start;
917 /* return number of matches */
918 retval = qi->qf_lists[qi->qf_curlist].qf_count;
919 goto qf_init_ok;
921 EMSG(_(e_readerrf));
922 error2:
923 qf_free(qi, qi->qf_curlist);
924 qi->qf_listcount--;
925 if (qi->qf_curlist > 0)
926 --qi->qf_curlist;
927 qf_init_ok:
928 if (fd != NULL)
929 fclose(fd);
930 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
932 fmt_first = fmt_ptr->next;
933 vim_free(fmt_ptr->prog);
934 vim_free(fmt_ptr);
936 qf_clean_dir_stack(&dir_stack);
937 qf_clean_dir_stack(&file_stack);
938 qf_init_end:
939 vim_free(namebuf);
940 vim_free(errmsg);
941 vim_free(pattern);
942 vim_free(fmtstr);
944 #ifdef FEAT_WINDOWS
945 qf_update_buffer(qi);
946 #endif
948 return retval;
952 * Prepare for adding a new quickfix list.
954 static void
955 qf_new_list(qi, qf_title)
956 qf_info_T *qi;
957 char *qf_title;
959 int i;
962 * If the current entry is not the last entry, delete entries below
963 * the current entry. This makes it possible to browse in a tree-like
964 * way with ":grep'.
966 while (qi->qf_listcount > qi->qf_curlist + 1)
967 qf_free(qi, --qi->qf_listcount);
970 * When the stack is full, remove to oldest entry
971 * Otherwise, add a new entry.
973 if (qi->qf_listcount == LISTCOUNT)
975 qf_free(qi, 0);
976 for (i = 1; i < LISTCOUNT; ++i)
977 qi->qf_lists[i - 1] = qi->qf_lists[i];
978 qi->qf_curlist = LISTCOUNT - 1;
980 else
981 qi->qf_curlist = qi->qf_listcount++;
982 qi->qf_lists[qi->qf_curlist].qf_index = 0;
983 qi->qf_lists[qi->qf_curlist].qf_count = 0;
984 if (qf_title)
986 if ((qi->qf_lists[qi->qf_curlist].qf_title = (char *)alloc(STRLEN(qf_title) + 2)))
987 sprintf(qi->qf_lists[qi->qf_curlist].qf_title, ":%s", qf_title);
989 else
990 qi->qf_lists[qi->qf_curlist].qf_title = NULL;
992 qi->qf_lists[qi->qf_curlist].qf_user_title = NULL;
993 qi->qf_lists[qi->qf_curlist].qf_use_user_title = FALSE;
997 * Free a location list
999 static void
1000 ll_free_all(pqi)
1001 qf_info_T **pqi;
1003 int i;
1004 qf_info_T *qi;
1006 qi = *pqi;
1007 if (qi == NULL)
1008 return;
1009 *pqi = NULL; /* Remove reference to this list */
1011 qi->qf_refcount--;
1012 if (qi->qf_refcount < 1)
1014 /* No references to this location list */
1015 for (i = 0; i < qi->qf_listcount; ++i)
1016 qf_free(qi, i);
1017 vim_free(qi);
1021 void
1022 qf_free_all(wp)
1023 win_T *wp;
1025 int i;
1026 qf_info_T *qi = &ql_info;
1028 if (wp != NULL)
1030 /* location list */
1031 ll_free_all(&wp->w_llist);
1032 ll_free_all(&wp->w_llist_ref);
1034 else
1035 /* quickfix list */
1036 for (i = 0; i < qi->qf_listcount; ++i)
1037 qf_free(qi, i);
1041 * Add an entry to the end of the list of errors.
1042 * Returns OK or FAIL.
1044 static int
1045 qf_add_entry(qi, prevp, dir, fname, bufnum, mesg, lnum, col, vis_col, pattern,
1046 nr, type, valid)
1047 qf_info_T *qi; /* quickfix list */
1048 qfline_T **prevp; /* pointer to previously added entry or NULL */
1049 char_u *dir; /* optional directory name */
1050 char_u *fname; /* file name or NULL */
1051 int bufnum; /* buffer number or zero */
1052 char_u *mesg; /* message */
1053 long lnum; /* line number */
1054 int col; /* column */
1055 int vis_col; /* using visual column */
1056 char_u *pattern; /* search pattern */
1057 int nr; /* error number */
1058 int type; /* type character */
1059 int valid; /* valid entry */
1061 qfline_T *qfp;
1063 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
1064 return FAIL;
1065 if (bufnum != 0)
1066 qfp->qf_fnum = bufnum;
1067 else
1068 qfp->qf_fnum = qf_get_fnum(dir, fname);
1069 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1071 vim_free(qfp);
1072 return FAIL;
1074 qfp->qf_lnum = lnum;
1075 qfp->qf_col = col;
1076 qfp->qf_viscol = vis_col;
1077 if (pattern == NULL || *pattern == NUL)
1078 qfp->qf_pattern = NULL;
1079 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1081 vim_free(qfp->qf_text);
1082 vim_free(qfp);
1083 return FAIL;
1085 qfp->qf_nr = nr;
1086 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1087 type = 0;
1088 qfp->qf_type = type;
1089 qfp->qf_valid = valid;
1091 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1092 /* first element in the list */
1094 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
1095 qfp->qf_prev = qfp; /* first element points to itself */
1097 else
1099 qfp->qf_prev = *prevp;
1100 (*prevp)->qf_next = qfp;
1102 qfp->qf_next = qfp; /* last element points to itself */
1103 qfp->qf_cleared = FALSE;
1104 *prevp = qfp;
1105 ++qi->qf_lists[qi->qf_curlist].qf_count;
1106 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1107 /* first valid entry */
1109 qi->qf_lists[qi->qf_curlist].qf_index =
1110 qi->qf_lists[qi->qf_curlist].qf_count;
1111 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
1114 return OK;
1118 * Allocate a new location list
1120 static qf_info_T *
1121 ll_new_list()
1123 qf_info_T *qi;
1125 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1126 if (qi != NULL)
1128 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1129 qi->qf_refcount++;
1132 return qi;
1136 * Return the location list for window 'wp'.
1137 * If not present, allocate a location list
1139 static qf_info_T *
1140 ll_get_or_alloc_list(wp)
1141 win_T *wp;
1143 if (IS_LL_WINDOW(wp))
1144 /* For a location list window, use the referenced location list */
1145 return wp->w_llist_ref;
1148 * For a non-location list window, w_llist_ref should not point to a
1149 * location list.
1151 ll_free_all(&wp->w_llist_ref);
1153 if (wp->w_llist == NULL)
1154 wp->w_llist = ll_new_list(); /* new location list */
1155 return wp->w_llist;
1159 * Copy the location list from window "from" to window "to".
1161 void
1162 copy_loclist(from, to)
1163 win_T *from;
1164 win_T *to;
1166 qf_info_T *qi;
1167 int idx;
1168 int i;
1171 * When copying from a location list window, copy the referenced
1172 * location list. For other windows, copy the location list for
1173 * that window.
1175 if (IS_LL_WINDOW(from))
1176 qi = from->w_llist_ref;
1177 else
1178 qi = from->w_llist;
1180 if (qi == NULL) /* no location list to copy */
1181 return;
1183 /* allocate a new location list */
1184 if ((to->w_llist = ll_new_list()) == NULL)
1185 return;
1187 to->w_llist->qf_listcount = qi->qf_listcount;
1189 /* Copy the location lists one at a time */
1190 for (idx = 0; idx < qi->qf_listcount; idx++)
1192 qf_list_T *from_qfl;
1193 qf_list_T *to_qfl;
1195 to->w_llist->qf_curlist = idx;
1197 from_qfl = &qi->qf_lists[idx];
1198 to_qfl = &to->w_llist->qf_lists[idx];
1200 /* Some of the fields are populated by qf_add_entry() */
1201 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1202 to_qfl->qf_count = 0;
1203 to_qfl->qf_index = 0;
1204 to_qfl->qf_start = NULL;
1205 to_qfl->qf_ptr = NULL;
1206 if (from_qfl->qf_title)
1207 to_qfl->qf_title = (char *)vim_strsave((char_u *)from_qfl->qf_title);
1208 else
1209 to_qfl->qf_title = NULL;
1211 if (from_qfl->qf_user_title)
1212 to_qfl->qf_user_title = (char *)vim_strsave((char_u *)from_qfl->qf_user_title);
1213 else
1214 to_qfl->qf_user_title = NULL;
1215 to_qfl->qf_use_user_title = from_qfl->qf_use_user_title;
1217 if (from_qfl->qf_count)
1219 qfline_T *from_qfp;
1220 qfline_T *prevp = NULL;
1222 /* copy all the location entries in this list */
1223 for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
1224 ++i, from_qfp = from_qfp->qf_next)
1226 if (qf_add_entry(to->w_llist, &prevp,
1227 NULL,
1228 NULL,
1230 from_qfp->qf_text,
1231 from_qfp->qf_lnum,
1232 from_qfp->qf_col,
1233 from_qfp->qf_viscol,
1234 from_qfp->qf_pattern,
1235 from_qfp->qf_nr,
1237 from_qfp->qf_valid) == FAIL)
1239 qf_free_all(to);
1240 return;
1243 * qf_add_entry() will not set the qf_num field, as the
1244 * directory and file names are not supplied. So the qf_fnum
1245 * field is copied here.
1247 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1248 prevp->qf_type = from_qfp->qf_type; /* error type */
1249 if (from_qfl->qf_ptr == from_qfp)
1250 to_qfl->qf_ptr = prevp; /* current location */
1254 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1256 /* When no valid entries are present in the list, qf_ptr points to
1257 * the first item in the list */
1258 if (to_qfl->qf_nonevalid == TRUE)
1259 to_qfl->qf_ptr = to_qfl->qf_start;
1262 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1266 * get buffer number for file "dir.name"
1268 static int
1269 qf_get_fnum(directory, fname)
1270 char_u *directory;
1271 char_u *fname;
1273 if (fname == NULL || *fname == NUL) /* no file name */
1274 return 0;
1276 #ifdef RISCOS
1277 /* Name is reported as `main.c', but file is `c.main' */
1278 return ro_buflist_add(fname);
1279 #else
1280 char_u *ptr;
1281 int fnum;
1283 # ifdef VMS
1284 vms_remove_version(fname);
1285 # endif
1286 # ifdef BACKSLASH_IN_FILENAME
1287 if (directory != NULL)
1288 slash_adjust(directory);
1289 slash_adjust(fname);
1290 # endif
1291 if (directory != NULL && !vim_isAbsName(fname)
1292 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1295 * Here we check if the file really exists.
1296 * This should normally be true, but if make works without
1297 * "leaving directory"-messages we might have missed a
1298 * directory change.
1300 if (mch_getperm(ptr) < 0)
1302 vim_free(ptr);
1303 directory = qf_guess_filepath(fname);
1304 if (directory)
1305 ptr = concat_fnames(directory, fname, TRUE);
1306 else
1307 ptr = vim_strsave(fname);
1309 /* Use concatenated directory name and file name */
1310 fnum = buflist_add(ptr, 0);
1311 vim_free(ptr);
1312 return fnum;
1314 return buflist_add(fname, 0);
1315 #endif
1320 * push dirbuf onto the directory stack and return pointer to actual dir or
1321 * NULL on error
1323 static char_u *
1324 qf_push_dir(dirbuf, stackptr)
1325 char_u *dirbuf;
1326 struct dir_stack_T **stackptr;
1328 struct dir_stack_T *ds_new;
1329 struct dir_stack_T *ds_ptr;
1331 /* allocate new stack element and hook it in */
1332 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1333 if (ds_new == NULL)
1334 return NULL;
1336 ds_new->next = *stackptr;
1337 *stackptr = ds_new;
1339 /* store directory on the stack */
1340 if (vim_isAbsName(dirbuf)
1341 || (*stackptr)->next == NULL
1342 || (*stackptr && dir_stack != *stackptr))
1343 (*stackptr)->dirname = vim_strsave(dirbuf);
1344 else
1346 /* Okay we don't have an absolute path.
1347 * dirbuf must be a subdir of one of the directories on the stack.
1348 * Let's search...
1350 ds_new = (*stackptr)->next;
1351 (*stackptr)->dirname = NULL;
1352 while (ds_new)
1354 vim_free((*stackptr)->dirname);
1355 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1356 TRUE);
1357 if (mch_isdir((*stackptr)->dirname) == TRUE)
1358 break;
1360 ds_new = ds_new->next;
1363 /* clean up all dirs we already left */
1364 while ((*stackptr)->next != ds_new)
1366 ds_ptr = (*stackptr)->next;
1367 (*stackptr)->next = (*stackptr)->next->next;
1368 vim_free(ds_ptr->dirname);
1369 vim_free(ds_ptr);
1372 /* Nothing found -> it must be on top level */
1373 if (ds_new == NULL)
1375 vim_free((*stackptr)->dirname);
1376 (*stackptr)->dirname = vim_strsave(dirbuf);
1380 if ((*stackptr)->dirname != NULL)
1381 return (*stackptr)->dirname;
1382 else
1384 ds_ptr = *stackptr;
1385 *stackptr = (*stackptr)->next;
1386 vim_free(ds_ptr);
1387 return NULL;
1393 * pop dirbuf from the directory stack and return previous directory or NULL if
1394 * stack is empty
1396 static char_u *
1397 qf_pop_dir(stackptr)
1398 struct dir_stack_T **stackptr;
1400 struct dir_stack_T *ds_ptr;
1402 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1403 * What to do if it isn't? */
1405 /* pop top element and free it */
1406 if (*stackptr != NULL)
1408 ds_ptr = *stackptr;
1409 *stackptr = (*stackptr)->next;
1410 vim_free(ds_ptr->dirname);
1411 vim_free(ds_ptr);
1414 /* return NEW top element as current dir or NULL if stack is empty*/
1415 return *stackptr ? (*stackptr)->dirname : NULL;
1419 * clean up directory stack
1421 static void
1422 qf_clean_dir_stack(stackptr)
1423 struct dir_stack_T **stackptr;
1425 struct dir_stack_T *ds_ptr;
1427 while ((ds_ptr = *stackptr) != NULL)
1429 *stackptr = (*stackptr)->next;
1430 vim_free(ds_ptr->dirname);
1431 vim_free(ds_ptr);
1436 * Check in which directory of the directory stack the given file can be
1437 * found.
1438 * Returns a pointer to the directory name or NULL if not found
1439 * Cleans up intermediate directory entries.
1441 * TODO: How to solve the following problem?
1442 * If we have the this directory tree:
1443 * ./
1444 * ./aa
1445 * ./aa/bb
1446 * ./bb
1447 * ./bb/x.c
1448 * and make says:
1449 * making all in aa
1450 * making all in bb
1451 * x.c:9: Error
1452 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1453 * qf_guess_filepath will return NULL.
1455 static char_u *
1456 qf_guess_filepath(filename)
1457 char_u *filename;
1459 struct dir_stack_T *ds_ptr;
1460 struct dir_stack_T *ds_tmp;
1461 char_u *fullname;
1463 /* no dirs on the stack - there's nothing we can do */
1464 if (dir_stack == NULL)
1465 return NULL;
1467 ds_ptr = dir_stack->next;
1468 fullname = NULL;
1469 while (ds_ptr)
1471 vim_free(fullname);
1472 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1474 /* If concat_fnames failed, just go on. The worst thing that can happen
1475 * is that we delete the entire stack.
1477 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1478 break;
1480 ds_ptr = ds_ptr->next;
1483 vim_free(fullname);
1485 /* clean up all dirs we already left */
1486 while (dir_stack->next != ds_ptr)
1488 ds_tmp = dir_stack->next;
1489 dir_stack->next = dir_stack->next->next;
1490 vim_free(ds_tmp->dirname);
1491 vim_free(ds_tmp);
1494 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1499 * jump to a quickfix line
1500 * if dir == FORWARD go "errornr" valid entries forward
1501 * if dir == BACKWARD go "errornr" valid entries backward
1502 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1503 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1504 * else if "errornr" is zero, redisplay the same line
1505 * else go to entry "errornr"
1507 void
1508 qf_jump(qi, dir, errornr, forceit)
1509 qf_info_T *qi;
1510 int dir;
1511 int errornr;
1512 int forceit;
1514 qf_info_T *ll_ref;
1515 qfline_T *qf_ptr;
1516 qfline_T *old_qf_ptr;
1517 int qf_index;
1518 int old_qf_fnum;
1519 int old_qf_index;
1520 int prev_index;
1521 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1522 char_u *err = e_no_more_items;
1523 linenr_T i;
1524 buf_T *old_curbuf;
1525 linenr_T old_lnum;
1526 colnr_T screen_col;
1527 colnr_T char_col;
1528 char_u *line;
1529 #ifdef FEAT_WINDOWS
1530 char_u *old_swb = p_swb;
1531 unsigned old_swb_flags = swb_flags;
1532 int opened_window = FALSE;
1533 win_T *win;
1534 win_T *altwin;
1535 int flags;
1536 #endif
1537 win_T *oldwin = curwin;
1538 int print_message = TRUE;
1539 int len;
1540 #ifdef FEAT_FOLDING
1541 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1542 #endif
1543 int ok = OK;
1544 int usable_win;
1546 if (qi == NULL)
1547 qi = &ql_info;
1549 if (qi->qf_curlist >= qi->qf_listcount
1550 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
1552 EMSG(_(e_quickfix));
1553 return;
1556 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
1557 old_qf_ptr = qf_ptr;
1558 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
1559 old_qf_index = qf_index;
1560 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1562 while (errornr--)
1564 old_qf_ptr = qf_ptr;
1565 prev_index = qf_index;
1566 old_qf_fnum = qf_ptr->qf_fnum;
1569 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
1570 || qf_ptr->qf_next == NULL)
1572 qf_ptr = old_qf_ptr;
1573 qf_index = prev_index;
1574 if (err != NULL)
1576 EMSG(_(err));
1577 goto theend;
1579 errornr = 0;
1580 break;
1582 ++qf_index;
1583 qf_ptr = qf_ptr->qf_next;
1584 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1585 && !qf_ptr->qf_valid)
1586 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1587 err = NULL;
1590 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1592 while (errornr--)
1594 old_qf_ptr = qf_ptr;
1595 prev_index = qf_index;
1596 old_qf_fnum = qf_ptr->qf_fnum;
1599 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1601 qf_ptr = old_qf_ptr;
1602 qf_index = prev_index;
1603 if (err != NULL)
1605 EMSG(_(err));
1606 goto theend;
1608 errornr = 0;
1609 break;
1611 --qf_index;
1612 qf_ptr = qf_ptr->qf_prev;
1613 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1614 && !qf_ptr->qf_valid)
1615 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1616 err = NULL;
1619 else if (errornr != 0) /* go to specified number */
1621 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1623 --qf_index;
1624 qf_ptr = qf_ptr->qf_prev;
1626 while (errornr > qf_index && qf_index <
1627 qi->qf_lists[qi->qf_curlist].qf_count
1628 && qf_ptr->qf_next != NULL)
1630 ++qf_index;
1631 qf_ptr = qf_ptr->qf_next;
1635 #ifdef FEAT_WINDOWS
1636 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1637 if (qf_win_pos_update(qi, old_qf_index))
1638 /* No need to print the error message if it's visible in the error
1639 * window */
1640 print_message = FALSE;
1643 * For ":helpgrep" find a help window or open one.
1645 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
1647 win_T *wp;
1649 if (cmdmod.tab != 0)
1650 wp = NULL;
1651 else
1652 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1653 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1654 break;
1655 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1656 win_enter(wp, TRUE);
1657 else
1660 * Split off help window; put it at far top if no position
1661 * specified, the current window is vertically split and narrow.
1663 flags = WSP_HELP;
1664 # ifdef FEAT_VERTSPLIT
1665 if (cmdmod.split == 0 && curwin->w_width != Columns
1666 && curwin->w_width < 80)
1667 flags |= WSP_TOP;
1668 # endif
1669 if (qi != &ql_info)
1670 flags |= WSP_NEWLOC; /* don't copy the location list */
1672 if (win_split(0, flags) == FAIL)
1673 goto theend;
1674 opened_window = TRUE; /* close it when fail */
1676 if (curwin->w_height < p_hh)
1677 win_setheight((int)p_hh);
1679 if (qi != &ql_info) /* not a quickfix list */
1681 /* The new window should use the supplied location list */
1682 curwin->w_llist = qi;
1683 qi->qf_refcount++;
1687 if (!p_im)
1688 restart_edit = 0; /* don't want insert mode in help file */
1692 * If currently in the quickfix window, find another window to show the
1693 * file in.
1695 if (bt_quickfix(curbuf) && !opened_window)
1698 * If there is no file specified, we don't know where to go.
1699 * But do advance, otherwise ":cn" gets stuck.
1701 if (qf_ptr->qf_fnum == 0)
1702 goto theend;
1704 /* Locate a window showing a normal buffer */
1705 usable_win = 0;
1706 FOR_ALL_WINDOWS(win)
1707 if (win->w_buffer->b_p_bt[0] == NUL)
1709 usable_win = 1;
1710 break;
1714 * If no usable window is found and 'switchbuf' contains "usetab"
1715 * then search in other tabs.
1717 if (!usable_win && (swb_flags & SWB_USETAB))
1719 tabpage_T *tp;
1720 win_T *wp;
1722 FOR_ALL_TAB_WINDOWS(tp, wp)
1724 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
1726 goto_tabpage_win(tp, wp);
1727 usable_win = 1;
1728 goto win_found;
1732 win_found:
1735 * If there is only one window and it is the quickfix window, create a
1736 * new one above the quickfix window.
1738 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
1740 ll_ref = curwin->w_llist_ref;
1742 flags = WSP_ABOVE;
1743 if (ll_ref != NULL)
1744 flags |= WSP_NEWLOC;
1745 if (win_split(0, flags) == FAIL)
1746 goto failed; /* not enough room for window */
1747 opened_window = TRUE; /* close it when fail */
1748 p_swb = empty_option; /* don't split again */
1749 swb_flags = 0;
1750 # ifdef FEAT_SCROLLBIND
1751 curwin->w_p_scb = FALSE;
1752 # endif
1753 if (ll_ref != NULL)
1755 /* The new window should use the location list from the
1756 * location list window */
1757 curwin->w_llist = ll_ref;
1758 ll_ref->qf_refcount++;
1761 else
1763 if (curwin->w_llist_ref != NULL)
1765 /* In a location window */
1766 ll_ref = curwin->w_llist_ref;
1768 /* Find the window with the same location list */
1769 FOR_ALL_WINDOWS(win)
1770 if (win->w_llist == ll_ref)
1771 break;
1772 if (win == NULL)
1774 /* Find the window showing the selected file */
1775 FOR_ALL_WINDOWS(win)
1776 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1777 break;
1778 if (win == NULL)
1780 /* Find a previous usable window */
1781 win = curwin;
1784 if (win->w_buffer->b_p_bt[0] == NUL)
1785 break;
1786 if (win->w_prev == NULL)
1787 win = lastwin; /* wrap around the top */
1788 else
1789 win = win->w_prev; /* go to previous window */
1790 } while (win != curwin);
1793 win_goto(win);
1795 /* If the location list for the window is not set, then set it
1796 * to the location list from the location window */
1797 if (win->w_llist == NULL)
1799 win->w_llist = ll_ref;
1800 ll_ref->qf_refcount++;
1803 else
1807 * Try to find a window that shows the right buffer.
1808 * Default to the window just above the quickfix buffer.
1810 win = curwin;
1811 altwin = NULL;
1812 for (;;)
1814 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1815 break;
1816 if (win->w_prev == NULL)
1817 win = lastwin; /* wrap around the top */
1818 else
1819 win = win->w_prev; /* go to previous window */
1821 if (IS_QF_WINDOW(win))
1823 /* Didn't find it, go to the window before the quickfix
1824 * window. */
1825 if (altwin != NULL)
1826 win = altwin;
1827 else if (curwin->w_prev != NULL)
1828 win = curwin->w_prev;
1829 else
1830 win = curwin->w_next;
1831 break;
1834 /* Remember a usable window. */
1835 if (altwin == NULL && !win->w_p_pvw
1836 && win->w_buffer->b_p_bt[0] == NUL)
1837 altwin = win;
1840 win_goto(win);
1844 #endif
1847 * If there is a file name,
1848 * read the wanted file if needed, and check autowrite etc.
1850 old_curbuf = curbuf;
1851 old_lnum = curwin->w_cursor.lnum;
1853 if (qf_ptr->qf_fnum != 0)
1855 if (qf_ptr->qf_type == 1)
1857 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1858 * set b_p_ro flag). */
1859 if (!can_abandon(curbuf, forceit))
1861 EMSG(_(e_nowrtmsg));
1862 ok = FALSE;
1864 else
1865 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
1866 ECMD_HIDE + ECMD_SET_HELP,
1867 oldwin == curwin ? curwin : NULL);
1869 else
1870 ok = buflist_getfile(qf_ptr->qf_fnum,
1871 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
1874 if (ok == OK)
1876 /* When not switched to another buffer, still need to set pc mark */
1877 if (curbuf == old_curbuf)
1878 setpcmark();
1880 if (qf_ptr->qf_pattern == NULL)
1883 * Go to line with error, unless qf_lnum is 0.
1885 i = qf_ptr->qf_lnum;
1886 if (i > 0)
1888 if (i > curbuf->b_ml.ml_line_count)
1889 i = curbuf->b_ml.ml_line_count;
1890 curwin->w_cursor.lnum = i;
1892 if (qf_ptr->qf_col > 0)
1894 curwin->w_cursor.col = qf_ptr->qf_col - 1;
1895 if (qf_ptr->qf_viscol == TRUE)
1898 * Check each character from the beginning of the error
1899 * line up to the error column. For each tab character
1900 * found, reduce the error column value by the length of
1901 * a tab character.
1903 line = ml_get_curline();
1904 screen_col = 0;
1905 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1907 if (*line == NUL)
1908 break;
1909 if (*line++ == '\t')
1911 curwin->w_cursor.col -= 7 - (screen_col % 8);
1912 screen_col += 8 - (screen_col % 8);
1914 else
1915 ++screen_col;
1918 check_cursor();
1920 else
1921 beginline(BL_WHITE | BL_FIX);
1923 else
1925 pos_T save_cursor;
1927 /* Move the cursor to the first line in the buffer */
1928 save_cursor = curwin->w_cursor;
1929 curwin->w_cursor.lnum = 0;
1930 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
1931 SEARCH_KEEP, NULL))
1932 curwin->w_cursor = save_cursor;
1935 #ifdef FEAT_FOLDING
1936 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1937 foldOpenCursor();
1938 #endif
1939 if (print_message)
1941 /* Update the screen before showing the message */
1942 update_topline_redraw();
1943 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
1944 qi->qf_lists[qi->qf_curlist].qf_count,
1945 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1946 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1947 /* Add the message, skipping leading whitespace and newlines. */
1948 len = (int)STRLEN(IObuff);
1949 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1951 /* Output the message. Overwrite to avoid scrolling when the 'O'
1952 * flag is present in 'shortmess'; But when not jumping, print the
1953 * whole message. */
1954 i = msg_scroll;
1955 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1956 msg_scroll = TRUE;
1957 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1958 msg_scroll = FALSE;
1959 msg_attr_keep(IObuff, 0, TRUE);
1960 msg_scroll = i;
1963 else
1965 #ifdef FEAT_WINDOWS
1966 if (opened_window)
1967 win_close(curwin, TRUE); /* Close opened window */
1968 #endif
1969 if (qf_ptr->qf_fnum != 0)
1972 * Couldn't open file, so put index back where it was. This could
1973 * happen if the file was readonly and we changed something.
1975 #ifdef FEAT_WINDOWS
1976 failed:
1977 #endif
1978 qf_ptr = old_qf_ptr;
1979 qf_index = old_qf_index;
1982 theend:
1983 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
1984 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1985 #ifdef FEAT_WINDOWS
1986 if (p_swb != old_swb && opened_window)
1988 /* Restore old 'switchbuf' value, but not when an autocommand or
1989 * modeline has changed the value. */
1990 if (p_swb == empty_option)
1992 p_swb = old_swb;
1993 swb_flags = old_swb_flags;
1995 else
1996 free_string_option(old_swb);
1998 #endif
2002 * ":clist": list all errors
2003 * ":llist": list all locations
2005 void
2006 qf_list(eap)
2007 exarg_T *eap;
2009 buf_T *buf;
2010 char_u *fname;
2011 qfline_T *qfp;
2012 int i;
2013 int idx1 = 1;
2014 int idx2 = -1;
2015 char_u *arg = eap->arg;
2016 int all = eap->forceit; /* if not :cl!, only show
2017 recognised errors */
2018 qf_info_T *qi = &ql_info;
2020 if (eap->cmdidx == CMD_llist)
2022 qi = GET_LOC_LIST(curwin);
2023 if (qi == NULL)
2025 EMSG(_(e_loclist));
2026 return;
2030 if (qi->qf_curlist >= qi->qf_listcount
2031 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
2033 EMSG(_(e_quickfix));
2034 return;
2036 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2038 EMSG(_(e_trailing));
2039 return;
2041 i = qi->qf_lists[qi->qf_curlist].qf_count;
2042 if (idx1 < 0)
2043 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2044 if (idx2 < 0)
2045 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2047 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
2048 all = TRUE;
2049 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2050 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
2052 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2054 msg_putchar('\n');
2055 if (got_int)
2056 break;
2058 fname = NULL;
2059 if (qfp->qf_fnum != 0
2060 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2062 fname = buf->b_fname;
2063 if (qfp->qf_type == 1) /* :helpgrep */
2064 fname = gettail(fname);
2066 if (fname == NULL)
2067 sprintf((char *)IObuff, "%2d", i);
2068 else
2069 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2070 i, (char *)fname);
2071 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
2072 ? hl_attr(HLF_L) : hl_attr(HLF_D));
2073 if (qfp->qf_lnum == 0)
2074 IObuff[0] = NUL;
2075 else if (qfp->qf_col == 0)
2076 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2077 else
2078 sprintf((char *)IObuff, ":%ld col %d",
2079 qfp->qf_lnum, qfp->qf_col);
2080 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2081 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2082 msg_puts_attr(IObuff, hl_attr(HLF_N));
2083 if (qfp->qf_pattern != NULL)
2085 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2086 STRCAT(IObuff, ":");
2087 msg_puts(IObuff);
2089 msg_puts((char_u *)" ");
2091 /* Remove newlines and leading whitespace from the text. For an
2092 * unrecognized line keep the indent, the compiler may mark a word
2093 * with ^^^^. */
2094 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
2095 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2096 IObuff, IOSIZE);
2097 msg_prt_line(IObuff, FALSE);
2098 out_flush(); /* show one line at a time */
2101 qfp = qfp->qf_next;
2102 ++i;
2103 ui_breakcheck();
2108 * Remove newlines and leading whitespace from an error message.
2109 * Put the result in "buf[bufsize]".
2111 static void
2112 qf_fmt_text(text, buf, bufsize)
2113 char_u *text;
2114 char_u *buf;
2115 int bufsize;
2117 int i;
2118 char_u *p = text;
2120 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2122 if (*p == '\n')
2124 buf[i] = ' ';
2125 while (*++p != NUL)
2126 if (!vim_iswhite(*p) && *p != '\n')
2127 break;
2129 else
2130 buf[i] = *p++;
2132 buf[i] = NUL;
2136 * ":colder [count]": Up in the quickfix stack.
2137 * ":cnewer [count]": Down in the quickfix stack.
2138 * ":lolder [count]": Up in the location list stack.
2139 * ":lnewer [count]": Down in the location list stack.
2141 void
2142 qf_age(eap)
2143 exarg_T *eap;
2145 qf_info_T *qi = &ql_info;
2146 int count;
2148 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2150 qi = GET_LOC_LIST(curwin);
2151 if (qi == NULL)
2153 EMSG(_(e_loclist));
2154 return;
2158 if (eap->addr_count != 0)
2159 count = eap->line2;
2160 else
2161 count = 1;
2162 while (count--)
2164 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
2166 if (qi->qf_curlist == 0)
2168 EMSG(_("E380: At bottom of quickfix stack"));
2169 return;
2171 --qi->qf_curlist;
2173 else
2175 if (qi->qf_curlist >= qi->qf_listcount - 1)
2177 EMSG(_("E381: At top of quickfix stack"));
2178 return;
2180 ++qi->qf_curlist;
2183 qf_msg(qi);
2187 static void
2188 qf_msg(qi)
2189 qf_info_T *qi;
2191 smsg((char_u *)_("error list %d of %d; %d errors"),
2192 qi->qf_curlist + 1, qi->qf_listcount,
2193 qi->qf_lists[qi->qf_curlist].qf_count);
2194 #ifdef FEAT_WINDOWS
2195 qf_update_buffer(qi);
2196 #endif
2200 * Free error list "idx".
2202 static void
2203 qf_free(qi, idx)
2204 qf_info_T *qi;
2205 int idx;
2207 qfline_T *qfp;
2209 while (qi->qf_lists[idx].qf_count)
2211 qfp = qi->qf_lists[idx].qf_start->qf_next;
2212 vim_free(qi->qf_lists[idx].qf_start->qf_text);
2213 vim_free(qi->qf_lists[idx].qf_start->qf_pattern);
2214 vim_free(qi->qf_lists[idx].qf_start);
2215 qi->qf_lists[idx].qf_start = qfp;
2216 --qi->qf_lists[idx].qf_count;
2218 vim_free(qi->qf_lists[idx].qf_title);
2219 vim_free(qi->qf_lists[idx].qf_user_title);
2223 * qf_mark_adjust: adjust marks
2225 void
2226 qf_mark_adjust(wp, line1, line2, amount, amount_after)
2227 win_T *wp;
2228 linenr_T line1;
2229 linenr_T line2;
2230 long amount;
2231 long amount_after;
2233 int i;
2234 qfline_T *qfp;
2235 int idx;
2236 qf_info_T *qi = &ql_info;
2238 if (wp != NULL)
2240 if (wp->w_llist == NULL)
2241 return;
2242 qi = wp->w_llist;
2245 for (idx = 0; idx < qi->qf_listcount; ++idx)
2246 if (qi->qf_lists[idx].qf_count)
2247 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
2248 i < qi->qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
2249 if (qfp->qf_fnum == curbuf->b_fnum)
2251 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2253 if (amount == MAXLNUM)
2254 qfp->qf_cleared = TRUE;
2255 else
2256 qfp->qf_lnum += amount;
2258 else if (amount_after && qfp->qf_lnum > line2)
2259 qfp->qf_lnum += amount_after;
2264 * Make a nice message out of the error character and the error number:
2265 * char number message
2266 * e or E 0 " error"
2267 * w or W 0 " warning"
2268 * i or I 0 " info"
2269 * 0 0 ""
2270 * other 0 " c"
2271 * e or E n " error n"
2272 * w or W n " warning n"
2273 * i or I n " info n"
2274 * 0 n " error n"
2275 * other n " c n"
2276 * 1 x "" :helpgrep
2278 static char_u *
2279 qf_types(c, nr)
2280 int c, nr;
2282 static char_u buf[20];
2283 static char_u cc[3];
2284 char_u *p;
2286 if (c == 'W' || c == 'w')
2287 p = (char_u *)" warning";
2288 else if (c == 'I' || c == 'i')
2289 p = (char_u *)" info";
2290 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2291 p = (char_u *)" error";
2292 else if (c == 0 || c == 1)
2293 p = (char_u *)"";
2294 else
2296 cc[0] = ' ';
2297 cc[1] = c;
2298 cc[2] = NUL;
2299 p = cc;
2302 if (nr <= 0)
2303 return p;
2305 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2306 return buf;
2309 #if defined(FEAT_WINDOWS) || defined(PROTO)
2311 * ":cwindow": open the quickfix window if we have errors to display,
2312 * close it if not.
2313 * ":lwindow": open the location list window if we have locations to display,
2314 * close it if not.
2316 void
2317 ex_cwindow(eap)
2318 exarg_T *eap;
2320 qf_info_T *qi = &ql_info;
2321 win_T *win;
2323 if (eap->cmdidx == CMD_lwindow)
2325 qi = GET_LOC_LIST(curwin);
2326 if (qi == NULL)
2327 return;
2330 /* Look for an existing quickfix window. */
2331 win = qf_find_win(qi);
2334 * If a quickfix window is open but we have no errors to display,
2335 * close the window. If a quickfix window is not open, then open
2336 * it if we have errors; otherwise, leave it closed.
2338 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
2339 || qi->qf_curlist >= qi->qf_listcount)
2341 if (win != NULL)
2342 ex_cclose(eap);
2344 else if (win == NULL)
2345 ex_copen(eap);
2349 * ":cclose": close the window showing the list of errors.
2350 * ":lclose": close the window showing the location list
2352 void
2353 ex_cclose(eap)
2354 exarg_T *eap;
2356 win_T *win = NULL;
2357 qf_info_T *qi = &ql_info;
2359 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2361 qi = GET_LOC_LIST(curwin);
2362 if (qi == NULL)
2363 return;
2366 /* Find existing quickfix window and close it. */
2367 win = qf_find_win(qi);
2368 if (win != NULL)
2369 win_close(win, FALSE);
2373 * ":copen": open a window that shows the list of errors.
2374 * ":lopen": open a window that shows the location list.
2376 void
2377 ex_copen(eap)
2378 exarg_T *eap;
2380 qf_info_T *qi = &ql_info;
2381 int height;
2382 win_T *win;
2383 tabpage_T *prevtab = curtab;
2384 buf_T *qf_buf;
2385 win_T *oldwin = curwin;
2387 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2389 qi = GET_LOC_LIST(curwin);
2390 if (qi == NULL)
2392 EMSG(_(e_loclist));
2393 return;
2397 if (eap->addr_count != 0)
2398 height = eap->line2;
2399 else
2400 height = QF_WINHEIGHT;
2402 #ifdef FEAT_VISUAL
2403 reset_VIsual_and_resel(); /* stop Visual mode */
2404 #endif
2405 #ifdef FEAT_GUI
2406 need_mouse_correct = TRUE;
2407 #endif
2410 * Find existing quickfix window, or open a new one.
2412 win = qf_find_win(qi);
2414 if (win != NULL && cmdmod.tab == 0)
2415 win_goto(win);
2416 else
2418 qf_buf = qf_find_buf(qi);
2420 /* The current window becomes the previous window afterwards. */
2421 win = curwin;
2423 if (eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2424 /* Create the new window at the very bottom. */
2425 win_goto(lastwin);
2426 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
2427 return; /* not enough room for window */
2428 #ifdef FEAT_SCROLLBIND
2429 curwin->w_p_scb = FALSE;
2430 #endif
2432 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2435 * For the location list window, create a reference to the
2436 * location list from the window 'win'.
2438 curwin->w_llist_ref = win->w_llist;
2439 win->w_llist->qf_refcount++;
2442 if (oldwin != curwin)
2443 oldwin = NULL; /* don't store info when in another window */
2444 if (qf_buf != NULL)
2445 /* Use the existing quickfix buffer */
2446 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
2447 ECMD_HIDE + ECMD_OLDBUF, oldwin);
2448 else
2450 /* Create a new quickfix buffer */
2451 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
2452 /* switch off 'swapfile' */
2453 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2454 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
2455 OPT_LOCAL);
2456 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
2457 #ifdef FEAT_DIFF
2458 curwin->w_p_diff = FALSE;
2459 #endif
2460 #ifdef FEAT_FOLDING
2461 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2462 OPT_LOCAL);
2463 #endif
2466 /* Only set the height when still in the same tab page and there is no
2467 * window to the side. */
2468 if (curtab == prevtab
2469 #ifdef FEAT_VERTSPLIT
2470 && curwin->w_width == Columns
2471 #endif
2473 win_setheight(height);
2474 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2475 if (win_valid(win))
2476 prevwin = win;
2480 * Fill the buffer with the quickfix list.
2482 qf_fill_buffer(qi);
2484 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
2485 curwin->w_cursor.col = 0;
2486 check_cursor();
2487 update_topline(); /* scroll to show the line */
2491 * Return the number of the current entry (line number in the quickfix
2492 * window).
2494 linenr_T
2495 qf_current_entry(wp)
2496 win_T *wp;
2498 qf_info_T *qi = &ql_info;
2500 if (IS_LL_WINDOW(wp))
2501 /* In the location list window, use the referenced location list */
2502 qi = wp->w_llist_ref;
2504 return qi->qf_lists[qi->qf_curlist].qf_index;
2508 * Update the cursor position in the quickfix window to the current error.
2509 * Return TRUE if there is a quickfix window.
2511 static int
2512 qf_win_pos_update(qi, old_qf_index)
2513 qf_info_T *qi;
2514 int old_qf_index; /* previous qf_index or zero */
2516 win_T *win;
2517 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
2520 * Put the cursor on the current error in the quickfix window, so that
2521 * it's viewable.
2523 win = qf_find_win(qi);
2524 if (win != NULL
2525 && qf_index <= win->w_buffer->b_ml.ml_line_count
2526 && old_qf_index != qf_index)
2528 win_T *old_curwin = curwin;
2530 curwin = win;
2531 curbuf = win->w_buffer;
2532 if (qf_index > old_qf_index)
2534 curwin->w_redraw_top = old_qf_index;
2535 curwin->w_redraw_bot = qf_index;
2537 else
2539 curwin->w_redraw_top = qf_index;
2540 curwin->w_redraw_bot = old_qf_index;
2542 curwin->w_cursor.lnum = qf_index;
2543 curwin->w_cursor.col = 0;
2544 update_topline(); /* scroll to show the line */
2545 redraw_later(VALID);
2546 curwin->w_redr_status = TRUE; /* update ruler */
2547 curwin = old_curwin;
2548 curbuf = curwin->w_buffer;
2550 return win != NULL;
2554 * Check whether the given window is displaying the specified quickfix/location
2555 * list buffer
2557 static int
2558 is_qf_win(win, qi)
2559 win_T *win;
2560 qf_info_T *qi;
2563 * A window displaying the quickfix buffer will have the w_llist_ref field
2564 * set to NULL.
2565 * A window displaying a location list buffer will have the w_llist_ref
2566 * pointing to the location list.
2568 if (bt_quickfix(win->w_buffer))
2569 if ((qi == &ql_info && win->w_llist_ref == NULL)
2570 || (qi != &ql_info && win->w_llist_ref == qi))
2571 return TRUE;
2573 return FALSE;
2577 * Find a window displaying the quickfix/location list 'qi'
2578 * Searches in only the windows opened in the current tab.
2580 static win_T *
2581 qf_find_win(qi)
2582 qf_info_T *qi;
2584 win_T *win;
2586 FOR_ALL_WINDOWS(win)
2587 if (is_qf_win(win, qi))
2588 break;
2590 return win;
2594 * Find a quickfix buffer.
2595 * Searches in windows opened in all the tabs.
2597 static buf_T *
2598 qf_find_buf(qi)
2599 qf_info_T *qi;
2601 tabpage_T *tp;
2602 win_T *win;
2604 FOR_ALL_TAB_WINDOWS(tp, win)
2605 if (is_qf_win(win, qi))
2606 return win->w_buffer;
2608 return NULL;
2612 * Find the quickfix buffer. If it exists, update the contents.
2614 static void
2615 qf_update_buffer(qi)
2616 qf_info_T *qi;
2618 buf_T *buf;
2619 aco_save_T aco;
2621 /* Check if a buffer for the quickfix list exists. Update it. */
2622 buf = qf_find_buf(qi);
2623 if (buf != NULL)
2625 /* set curwin/curbuf to buf and save a few things */
2626 aucmd_prepbuf(&aco, buf);
2628 qf_fill_buffer(qi);
2630 /* restore curwin/curbuf and a few other things */
2631 aucmd_restbuf(&aco);
2633 (void)qf_win_pos_update(qi, 0);
2638 * Fill current buffer with quickfix errors, replacing any previous contents.
2639 * curbuf must be the quickfix buffer!
2641 static void
2642 qf_fill_buffer(qi)
2643 qf_info_T *qi;
2645 linenr_T lnum;
2646 qfline_T *qfp;
2647 buf_T *errbuf;
2648 int len;
2649 int old_KeyTyped = KeyTyped;
2651 /* delete all existing lines */
2652 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2653 (void)ml_delete((linenr_T)1, FALSE);
2655 /* Check if there is anything to display */
2656 if (qi->qf_curlist < qi->qf_listcount)
2658 /* Add one line for each error */
2659 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2660 for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
2662 if (qfp->qf_fnum != 0
2663 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2664 && errbuf->b_fname != NULL)
2666 if (qfp->qf_type == 1) /* :helpgrep */
2667 STRCPY(IObuff, gettail(errbuf->b_fname));
2668 else
2669 STRCPY(IObuff, errbuf->b_fname);
2670 len = (int)STRLEN(IObuff);
2672 else
2673 len = 0;
2674 IObuff[len++] = '|';
2676 if (qfp->qf_lnum > 0)
2678 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2679 len += (int)STRLEN(IObuff + len);
2681 if (qfp->qf_col > 0)
2683 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2684 len += (int)STRLEN(IObuff + len);
2687 sprintf((char *)IObuff + len, "%s",
2688 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2689 len += (int)STRLEN(IObuff + len);
2691 else if (qfp->qf_pattern != NULL)
2693 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2694 len += (int)STRLEN(IObuff + len);
2696 IObuff[len++] = '|';
2697 IObuff[len++] = ' ';
2699 /* Remove newlines and leading whitespace from the text.
2700 * For an unrecognized line keep the indent, the compiler may
2701 * mark a word with ^^^^. */
2702 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2703 IObuff + len, IOSIZE - len);
2705 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2706 == FAIL)
2707 break;
2708 qfp = qfp->qf_next;
2710 /* Delete the empty line which is now at the end */
2711 (void)ml_delete(lnum + 1, FALSE);
2714 /* correct cursor position */
2715 check_lnums(TRUE);
2717 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2718 * resembles reading a file into a buffer, it's more logical when using
2719 * autocommands. */
2720 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2721 curbuf->b_p_ma = FALSE;
2723 #ifdef FEAT_AUTOCMD
2724 keep_filetype = TRUE; /* don't detect 'filetype' */
2725 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2726 FALSE, curbuf);
2727 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2728 FALSE, curbuf);
2729 keep_filetype = FALSE;
2730 #endif
2732 /* make sure it will be redrawn */
2733 redraw_curbuf_later(NOT_VALID);
2735 /* Restore KeyTyped, setting 'filetype' may reset it. */
2736 KeyTyped = old_KeyTyped;
2739 #endif /* FEAT_WINDOWS */
2742 * Return TRUE if "buf" is the quickfix buffer.
2745 bt_quickfix(buf)
2746 buf_T *buf;
2748 return (buf->b_p_bt[0] == 'q');
2752 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2753 * This means the buffer name is not a file name.
2756 bt_nofile(buf)
2757 buf_T *buf;
2759 return (buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2760 || buf->b_p_bt[0] == 'a';
2764 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2767 bt_dontwrite(buf)
2768 buf_T *buf;
2770 return (buf->b_p_bt[0] == 'n');
2774 bt_dontwrite_msg(buf)
2775 buf_T *buf;
2777 if (bt_dontwrite(buf))
2779 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2780 return TRUE;
2782 return FALSE;
2786 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2787 * and 'bufhidden'.
2790 buf_hide(buf)
2791 buf_T *buf;
2793 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2794 switch (buf->b_p_bh[0])
2796 case 'u': /* "unload" */
2797 case 'w': /* "wipe" */
2798 case 'd': return FALSE; /* "delete" */
2799 case 'h': return TRUE; /* "hide" */
2801 return (p_hid || cmdmod.hide);
2805 * Return TRUE when using ":vimgrep" for ":grep".
2808 grep_internal(cmdidx)
2809 cmdidx_T cmdidx;
2811 return ((cmdidx == CMD_grep
2812 || cmdidx == CMD_lgrep
2813 || cmdidx == CMD_grepadd
2814 || cmdidx == CMD_lgrepadd)
2815 && STRCMP("internal",
2816 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2820 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
2822 void
2823 ex_make(eap)
2824 exarg_T *eap;
2826 char_u *fname;
2827 char_u *cmd;
2828 unsigned len;
2829 win_T *wp = NULL;
2830 qf_info_T *qi = &ql_info;
2831 int res;
2832 #ifdef FEAT_AUTOCMD
2833 char_u *au_name = NULL;
2835 switch (eap->cmdidx)
2837 case CMD_make: au_name = (char_u *)"make"; break;
2838 case CMD_lmake: au_name = (char_u *)"lmake"; break;
2839 case CMD_grep: au_name = (char_u *)"grep"; break;
2840 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
2841 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
2842 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
2843 default: break;
2845 if (au_name != NULL)
2847 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2848 curbuf->b_fname, TRUE, curbuf);
2849 # ifdef FEAT_EVAL
2850 if (did_throw || force_abort)
2851 return;
2852 # endif
2854 #endif
2856 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
2857 if (grep_internal(eap->cmdidx))
2859 ex_vimgrep(eap);
2860 return;
2863 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
2864 || eap->cmdidx == CMD_lgrepadd)
2865 wp = curwin;
2867 autowrite_all();
2868 fname = get_mef_name();
2869 if (fname == NULL)
2870 return;
2871 mch_remove(fname); /* in case it's not unique */
2874 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2876 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2877 if (*p_sp != NUL)
2878 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
2879 cmd = alloc(len);
2880 if (cmd == NULL)
2881 return;
2882 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2883 (char *)p_shq);
2884 if (*p_sp != NUL)
2885 append_redir(cmd, len, p_sp, fname);
2887 * Output a newline if there's something else than the :make command that
2888 * was typed (in which case the cursor is in column 0).
2890 if (msg_col == 0)
2891 msg_didout = FALSE;
2892 msg_start();
2893 MSG_PUTS(":!");
2894 msg_outtrans(cmd); /* show what we are doing */
2896 /* let the shell know if we are redirecting output or not */
2897 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2899 #ifdef AMIGA
2900 out_flush();
2901 /* read window status report and redraw before message */
2902 (void)char_avail();
2903 #endif
2905 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
2906 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
2907 (eap->cmdidx != CMD_grepadd
2908 && eap->cmdidx != CMD_lgrepadd),
2909 (char *)*eap->cmdlinep);
2910 #ifdef FEAT_AUTOCMD
2911 if (au_name != NULL)
2912 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2913 curbuf->b_fname, TRUE, curbuf);
2914 #endif
2915 if (res > 0 && !eap->forceit)
2917 if (wp != NULL)
2918 qi = GET_LOC_LIST(wp);
2919 qf_jump(qi, 0, 0, FALSE); /* display first error */
2922 mch_remove(fname);
2923 vim_free(fname);
2924 vim_free(cmd);
2928 * Return the name for the errorfile, in allocated memory.
2929 * Find a new unique name when 'makeef' contains "##".
2930 * Returns NULL for error.
2932 static char_u *
2933 get_mef_name()
2935 char_u *p;
2936 char_u *name;
2937 static int start = -1;
2938 static int off = 0;
2939 #ifdef HAVE_LSTAT
2940 struct stat sb;
2941 #endif
2943 if (*p_mef == NUL)
2945 name = vim_tempname('e');
2946 if (name == NULL)
2947 EMSG(_(e_notmp));
2948 return name;
2951 for (p = p_mef; *p; ++p)
2952 if (p[0] == '#' && p[1] == '#')
2953 break;
2955 if (*p == NUL)
2956 return vim_strsave(p_mef);
2958 /* Keep trying until the name doesn't exist yet. */
2959 for (;;)
2961 if (start == -1)
2962 start = mch_get_pid();
2963 else
2964 off += 19;
2966 name = alloc((unsigned)STRLEN(p_mef) + 30);
2967 if (name == NULL)
2968 break;
2969 STRCPY(name, p_mef);
2970 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2971 STRCAT(name, p + 2);
2972 if (mch_getperm(name) < 0
2973 #ifdef HAVE_LSTAT
2974 /* Don't accept a symbolic link, its a security risk. */
2975 && mch_lstat((char *)name, &sb) < 0
2976 #endif
2978 break;
2979 vim_free(name);
2981 return name;
2985 * ":cc", ":crewind", ":cfirst" and ":clast".
2986 * ":ll", ":lrewind", ":lfirst" and ":llast".
2988 void
2989 ex_cc(eap)
2990 exarg_T *eap;
2992 qf_info_T *qi = &ql_info;
2994 if (eap->cmdidx == CMD_ll
2995 || eap->cmdidx == CMD_lrewind
2996 || eap->cmdidx == CMD_lfirst
2997 || eap->cmdidx == CMD_llast)
2999 qi = GET_LOC_LIST(curwin);
3000 if (qi == NULL)
3002 EMSG(_(e_loclist));
3003 return;
3007 qf_jump(qi, 0,
3008 eap->addr_count > 0
3009 ? (int)eap->line2
3010 : (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3012 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3013 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3015 : 32767,
3016 eap->forceit);
3020 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
3021 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
3023 void
3024 ex_cnext(eap)
3025 exarg_T *eap;
3027 qf_info_T *qi = &ql_info;
3029 if (eap->cmdidx == CMD_lnext
3030 || eap->cmdidx == CMD_lNext
3031 || eap->cmdidx == CMD_lprevious
3032 || eap->cmdidx == CMD_lnfile
3033 || eap->cmdidx == CMD_lNfile
3034 || eap->cmdidx == CMD_lpfile)
3036 qi = GET_LOC_LIST(curwin);
3037 if (qi == NULL)
3039 EMSG(_(e_loclist));
3040 return;
3044 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext)
3045 ? FORWARD
3046 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile)
3047 ? FORWARD_FILE
3048 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3049 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
3050 ? BACKWARD_FILE
3051 : BACKWARD,
3052 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
3056 * ":cfile"/":cgetfile"/":caddfile" commands.
3057 * ":lfile"/":lgetfile"/":laddfile" commands.
3059 void
3060 ex_cfile(eap)
3061 exarg_T *eap;
3063 win_T *wp = NULL;
3064 qf_info_T *qi = &ql_info;
3066 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
3067 || eap->cmdidx == CMD_laddfile)
3068 wp = curwin;
3070 if (*eap->arg != NUL)
3071 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
3074 * This function is used by the :cfile, :cgetfile and :caddfile
3075 * commands.
3076 * :cfile always creates a new quickfix list and jumps to the
3077 * first error.
3078 * :cgetfile creates a new quickfix list but doesn't jump to the
3079 * first error.
3080 * :caddfile adds to an existing quickfix list. If there is no
3081 * quickfix list then a new list is created.
3083 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
3084 && eap->cmdidx != CMD_laddfile),
3085 (char *)*eap->cmdlinep) > 0
3086 && (eap->cmdidx == CMD_cfile
3087 || eap->cmdidx == CMD_lfile))
3089 if (wp != NULL)
3090 qi = GET_LOC_LIST(wp);
3091 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
3096 * ":vimgrep {pattern} file(s)"
3097 * ":vimgrepadd {pattern} file(s)"
3098 * ":lvimgrep {pattern} file(s)"
3099 * ":lvimgrepadd {pattern} file(s)"
3101 void
3102 ex_vimgrep(eap)
3103 exarg_T *eap;
3105 regmmatch_T regmatch;
3106 int fcount;
3107 char_u **fnames;
3108 char_u *fname;
3109 char_u *s;
3110 char_u *p;
3111 int fi;
3112 qf_info_T *qi = &ql_info;
3113 qfline_T *prevp = NULL;
3114 long lnum;
3115 buf_T *buf;
3116 int duplicate_name = FALSE;
3117 int using_dummy;
3118 int redraw_for_dummy = FALSE;
3119 int found_match;
3120 buf_T *first_match_buf = NULL;
3121 time_t seconds = 0;
3122 int save_mls;
3123 #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3124 char_u *save_ei = NULL;
3125 #endif
3126 aco_save_T aco;
3127 int flags = 0;
3128 colnr_T col;
3129 long tomatch;
3130 char_u dirname_start[MAXPATHL];
3131 char_u dirname_now[MAXPATHL];
3132 char_u *target_dir = NULL;
3133 #ifdef FEAT_AUTOCMD
3134 char_u *au_name = NULL;
3136 switch (eap->cmdidx)
3138 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3139 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3140 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
3141 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
3142 default: break;
3144 if (au_name != NULL)
3146 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3147 curbuf->b_fname, TRUE, curbuf);
3148 if (did_throw || force_abort)
3149 return;
3151 #endif
3153 if (eap->cmdidx == CMD_lgrep
3154 || eap->cmdidx == CMD_lvimgrep
3155 || eap->cmdidx == CMD_lgrepadd
3156 || eap->cmdidx == CMD_lvimgrepadd)
3158 qi = ll_get_or_alloc_list(curwin);
3159 if (qi == NULL)
3160 return;
3163 if (eap->addr_count > 0)
3164 tomatch = eap->line2;
3165 else
3166 tomatch = MAXLNUM;
3168 /* Get the search pattern: either white-separated or enclosed in // */
3169 regmatch.regprog = NULL;
3170 p = skip_vimgrep_pat(eap->arg, &s, &flags);
3171 if (p == NULL)
3173 EMSG(_(e_invalpat));
3174 goto theend;
3176 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
3177 if (regmatch.regprog == NULL)
3178 goto theend;
3179 regmatch.rmm_ic = p_ic;
3180 regmatch.rmm_maxcol = 0;
3182 p = skipwhite(p);
3183 if (*p == NUL)
3185 EMSG(_("E683: File name missing or invalid pattern"));
3186 goto theend;
3189 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
3190 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
3191 || qi->qf_curlist == qi->qf_listcount)
3192 /* make place for a new list */
3193 qf_new_list(qi, (char *)*eap->cmdlinep);
3194 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
3195 /* Adding to existing list, find last entry. */
3196 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
3197 prevp->qf_next != prevp; prevp = prevp->qf_next)
3200 /* parse the list of arguments */
3201 if (get_arglist_exp(p, &fcount, &fnames) == FAIL)
3202 goto theend;
3203 if (fcount == 0)
3205 EMSG(_(e_nomatch));
3206 goto theend;
3209 /* Remember the current directory, because a BufRead autocommand that does
3210 * ":lcd %:p:h" changes the meaning of short path names. */
3211 mch_dirname(dirname_start, MAXPATHL);
3213 seconds = (time_t)0;
3214 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
3216 fname = shorten_fname1(fnames[fi]);
3217 if (time(NULL) > seconds)
3219 /* Display the file name every second or so, show the user we are
3220 * working on it. */
3221 seconds = time(NULL);
3222 msg_start();
3223 p = msg_strtrunc(fname, TRUE);
3224 if (p == NULL)
3225 msg_outtrans(fname);
3226 else
3228 msg_outtrans(p);
3229 vim_free(p);
3231 msg_clr_eos();
3232 msg_didout = FALSE; /* overwrite this message */
3233 msg_nowait = TRUE; /* don't wait for this message */
3234 msg_col = 0;
3235 out_flush();
3238 buf = buflist_findname_exp(fnames[fi]);
3239 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3241 /* Remember that a buffer with this name already exists. */
3242 duplicate_name = (buf != NULL);
3243 using_dummy = TRUE;
3244 redraw_for_dummy = TRUE;
3246 #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3247 /* Don't do Filetype autocommands to avoid loading syntax and
3248 * indent scripts, a great speed improvement. */
3249 save_ei = au_event_disable(",Filetype");
3250 #endif
3251 /* Don't use modelines here, it's useless. */
3252 save_mls = p_mls;
3253 p_mls = 0;
3255 /* Load file into a buffer, so that 'fileencoding' is detected,
3256 * autocommands applied, etc. */
3257 buf = load_dummy_buffer(fname);
3259 /* When autocommands changed directory: go back. We assume it was
3260 * ":lcd %:p:h". */
3261 mch_dirname(dirname_now, MAXPATHL);
3262 if (STRCMP(dirname_start, dirname_now) != 0)
3264 exarg_T ea;
3266 ea.arg = dirname_start;
3267 ea.cmdidx = CMD_lcd;
3268 ex_cd(&ea);
3271 p_mls = save_mls;
3272 #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3273 au_event_restore(save_ei);
3274 #endif
3276 else
3277 /* Use existing, loaded buffer. */
3278 using_dummy = FALSE;
3280 if (buf == NULL)
3282 if (!got_int)
3283 smsg((char_u *)_("Cannot open file \"%s\""), fname);
3285 else
3287 /* Try for a match in all lines of the buffer.
3288 * For ":1vimgrep" look for first match only. */
3289 found_match = FALSE;
3290 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3291 ++lnum)
3293 col = 0;
3294 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
3295 col, NULL) > 0)
3298 if (qf_add_entry(qi, &prevp,
3299 NULL, /* dir */
3300 fname,
3302 ml_get_buf(buf,
3303 regmatch.startpos[0].lnum + lnum, FALSE),
3304 regmatch.startpos[0].lnum + lnum,
3305 regmatch.startpos[0].col + 1,
3306 FALSE, /* vis_col */
3307 NULL, /* search pattern */
3308 0, /* nr */
3309 0, /* type */
3310 TRUE /* valid */
3311 ) == FAIL)
3313 got_int = TRUE;
3314 break;
3316 found_match = TRUE;
3317 if (--tomatch == 0)
3318 break;
3319 if ((flags & VGR_GLOBAL) == 0
3320 || regmatch.endpos[0].lnum > 0)
3321 break;
3322 col = regmatch.endpos[0].col
3323 + (col == regmatch.endpos[0].col);
3324 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
3325 break;
3327 line_breakcheck();
3328 if (got_int)
3329 break;
3332 if (using_dummy)
3334 if (found_match && first_match_buf == NULL)
3335 first_match_buf = buf;
3336 if (duplicate_name)
3338 /* Never keep a dummy buffer if there is another buffer
3339 * with the same name. */
3340 wipe_dummy_buffer(buf);
3341 buf = NULL;
3343 else if (!cmdmod.hide
3344 || buf->b_p_bh[0] == 'u' /* "unload" */
3345 || buf->b_p_bh[0] == 'w' /* "wipe" */
3346 || buf->b_p_bh[0] == 'd') /* "delete" */
3348 /* When no match was found we don't need to remember the
3349 * buffer, wipe it out. If there was a match and it
3350 * wasn't the first one or we won't jump there: only
3351 * unload the buffer.
3352 * Ignore 'hidden' here, because it may lead to having too
3353 * many swap files. */
3354 if (!found_match)
3356 wipe_dummy_buffer(buf);
3357 buf = NULL;
3359 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
3361 unload_dummy_buffer(buf);
3362 buf = NULL;
3366 if (buf != NULL)
3368 /* If the buffer is still loaded we need to use the
3369 * directory we jumped to below. */
3370 if (buf == first_match_buf
3371 && target_dir == NULL
3372 && STRCMP(dirname_start, dirname_now) != 0)
3373 target_dir = vim_strsave(dirname_now);
3375 /* The buffer is still loaded, the Filetype autocommands
3376 * need to be done now, in that buffer. And the modelines
3377 * need to be done (again). But not the window-local
3378 * options! */
3379 aucmd_prepbuf(&aco, buf);
3380 #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3381 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3382 buf->b_fname, TRUE, buf);
3383 #endif
3384 do_modelines(OPT_NOWIN);
3385 aucmd_restbuf(&aco);
3391 FreeWild(fcount, fnames);
3393 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3394 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3395 qi->qf_lists[qi->qf_curlist].qf_index = 1;
3397 #ifdef FEAT_WINDOWS
3398 qf_update_buffer(qi);
3399 #endif
3401 #ifdef FEAT_AUTOCMD
3402 if (au_name != NULL)
3403 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3404 curbuf->b_fname, TRUE, curbuf);
3405 #endif
3407 /* Jump to first match. */
3408 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
3410 if ((flags & VGR_NOJUMP) == 0)
3412 buf = curbuf;
3413 qf_jump(qi, 0, 0, eap->forceit);
3414 if (buf != curbuf)
3415 /* If we jumped to another buffer redrawing will already be
3416 * taken care of. */
3417 redraw_for_dummy = FALSE;
3419 /* Jump to the directory used after loading the buffer. */
3420 if (curbuf == first_match_buf && target_dir != NULL)
3422 exarg_T ea;
3424 ea.arg = target_dir;
3425 ea.cmdidx = CMD_lcd;
3426 ex_cd(&ea);
3430 else
3431 EMSG2(_(e_nomatch2), s);
3433 /* If we loaded a dummy buffer into the current window, the autocommands
3434 * may have messed up things, need to redraw and recompute folds. */
3435 if (redraw_for_dummy)
3437 #ifdef FEAT_FOLDING
3438 foldUpdateAll(curwin);
3439 #else
3440 redraw_later(NOT_VALID);
3441 #endif
3444 theend:
3445 vim_free(target_dir);
3446 vim_free(regmatch.regprog);
3450 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
3451 * Put the start of the pattern in "*s", unless "s" is NULL.
3452 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3453 * If "s" is not NULL terminate the pattern with a NUL.
3454 * Return a pointer to the char just past the pattern plus flags.
3456 char_u *
3457 skip_vimgrep_pat(p, s, flags)
3458 char_u *p;
3459 char_u **s;
3460 int *flags;
3462 int c;
3464 if (vim_isIDc(*p))
3466 /* ":vimgrep pattern fname" */
3467 if (s != NULL)
3468 *s = p;
3469 p = skiptowhite(p);
3470 if (s != NULL && *p != NUL)
3471 *p++ = NUL;
3473 else
3475 /* ":vimgrep /pattern/[g][j] fname" */
3476 if (s != NULL)
3477 *s = p + 1;
3478 c = *p;
3479 p = skip_regexp(p + 1, c, TRUE, NULL);
3480 if (*p != c)
3481 return NULL;
3483 /* Truncate the pattern. */
3484 if (s != NULL)
3485 *p = NUL;
3486 ++p;
3488 /* Find the flags */
3489 while (*p == 'g' || *p == 'j')
3491 if (flags != NULL)
3493 if (*p == 'g')
3494 *flags |= VGR_GLOBAL;
3495 else
3496 *flags |= VGR_NOJUMP;
3498 ++p;
3501 return p;
3505 * Load file "fname" into a dummy buffer and return the buffer pointer.
3506 * Returns NULL if it fails.
3507 * Must call unload_dummy_buffer() or wipe_dummy_buffer() later!
3509 static buf_T *
3510 load_dummy_buffer(fname)
3511 char_u *fname;
3513 buf_T *newbuf;
3514 int failed = TRUE;
3515 aco_save_T aco;
3517 /* Allocate a buffer without putting it in the buffer list. */
3518 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
3519 if (newbuf == NULL)
3520 return NULL;
3522 /* Init the options. */
3523 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
3525 /* need to open the memfile before putting the buffer in a window */
3526 if (ml_open(newbuf) == OK)
3528 /* set curwin/curbuf to buf and save a few things */
3529 aucmd_prepbuf(&aco, newbuf);
3531 /* Need to set the filename for autocommands. */
3532 (void)setfname(curbuf, fname, NULL, FALSE);
3534 /* Create swap file now to avoid the ATTENTION message. */
3535 check_need_swap(TRUE);
3537 /* Remove the "dummy" flag, otherwise autocommands may not
3538 * work. */
3539 curbuf->b_flags &= ~BF_DUMMY;
3541 if (readfile(fname, NULL,
3542 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
3543 NULL, READ_NEW | READ_DUMMY) == OK
3544 && !got_int
3545 && !(curbuf->b_flags & BF_NEW))
3547 failed = FALSE;
3548 if (curbuf != newbuf)
3550 /* Bloody autocommands changed the buffer! */
3551 if (buf_valid(newbuf))
3552 wipe_buffer(newbuf, FALSE);
3553 newbuf = curbuf;
3557 /* restore curwin/curbuf and a few other things */
3558 aucmd_restbuf(&aco);
3561 if (!buf_valid(newbuf))
3562 return NULL;
3563 if (failed)
3565 wipe_dummy_buffer(newbuf);
3566 return NULL;
3568 return newbuf;
3572 * Wipe out the dummy buffer that load_dummy_buffer() created.
3574 static void
3575 wipe_dummy_buffer(buf)
3576 buf_T *buf;
3578 if (curbuf != buf) /* safety check */
3580 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3581 cleanup_T cs;
3583 /* Reset the error/interrupt/exception state here so that aborting()
3584 * returns FALSE when wiping out the buffer. Otherwise it doesn't
3585 * work when got_int is set. */
3586 enter_cleanup(&cs);
3587 #endif
3589 wipe_buffer(buf, FALSE);
3591 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3592 /* Restore the error/interrupt/exception state if not discarded by a
3593 * new aborting error, interrupt, or uncaught exception. */
3594 leave_cleanup(&cs);
3595 #endif
3600 * Unload the dummy buffer that load_dummy_buffer() created.
3602 static void
3603 unload_dummy_buffer(buf)
3604 buf_T *buf;
3606 if (curbuf != buf) /* safety check */
3607 close_buffer(NULL, buf, DOBUF_UNLOAD);
3610 #if defined(FEAT_EVAL) || defined(PROTO)
3612 * Add each quickfix error to list "list" as a dictionary.
3615 get_errorlist(wp, list)
3616 win_T *wp;
3617 list_T *list;
3619 qf_info_T *qi = &ql_info;
3620 dict_T *dict;
3621 char_u buf[2];
3622 qfline_T *qfp;
3623 int i;
3624 int bufnum;
3626 if (wp != NULL)
3628 qi = GET_LOC_LIST(wp);
3629 if (qi == NULL)
3630 return FAIL;
3633 if (qi->qf_curlist >= qi->qf_listcount
3634 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
3635 return FAIL;
3637 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3638 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
3640 /* Handle entries with a non-existing buffer number. */
3641 bufnum = qfp->qf_fnum;
3642 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3643 bufnum = 0;
3645 if ((dict = dict_alloc()) == NULL)
3646 return FAIL;
3647 if (list_append_dict(list, dict) == FAIL)
3648 return FAIL;
3650 buf[0] = qfp->qf_type;
3651 buf[1] = NUL;
3652 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
3653 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
3654 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
3655 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
3656 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
3657 || dict_add_nr_str(dict, "pattern", 0L,
3658 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
3659 || dict_add_nr_str(dict, "text", 0L,
3660 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
3661 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
3662 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
3663 return FAIL;
3665 qfp = qfp->qf_next;
3667 return OK;
3671 * Populate the quickfix list with the items supplied in the list
3672 * of dictionaries.
3675 set_errorlist(wp, list, action)
3676 win_T *wp;
3677 list_T *list;
3678 int action;
3680 listitem_T *li;
3681 dict_T *d;
3682 char_u *filename, *pattern, *text, *type;
3683 int bufnum;
3684 long lnum;
3685 int col, nr;
3686 int vcol;
3687 qfline_T *prevp = NULL;
3688 int valid, status;
3689 int retval = OK;
3690 qf_info_T *qi = &ql_info;
3691 int did_bufnr_emsg = FALSE;
3693 if (wp != NULL)
3695 qi = ll_get_or_alloc_list(wp);
3696 if (qi == NULL)
3697 return FAIL;
3700 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
3701 /* make place for a new list */
3702 qf_new_list(qi, NULL);
3703 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
3704 /* Adding to existing list, find last entry. */
3705 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
3706 prevp->qf_next != prevp; prevp = prevp->qf_next)
3708 else if (action == 'r')
3709 qf_free(qi, qi->qf_curlist);
3711 for (li = list->lv_first; li != NULL; li = li->li_next)
3713 if (li->li_tv.v_type != VAR_DICT)
3714 continue; /* Skip non-dict items */
3716 d = li->li_tv.vval.v_dict;
3717 if (d == NULL)
3718 continue;
3720 filename = get_dict_string(d, (char_u *)"filename", TRUE);
3721 bufnum = get_dict_number(d, (char_u *)"bufnr");
3722 lnum = get_dict_number(d, (char_u *)"lnum");
3723 col = get_dict_number(d, (char_u *)"col");
3724 vcol = get_dict_number(d, (char_u *)"vcol");
3725 nr = get_dict_number(d, (char_u *)"nr");
3726 type = get_dict_string(d, (char_u *)"type", TRUE);
3727 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
3728 text = get_dict_string(d, (char_u *)"text", TRUE);
3729 if (text == NULL)
3730 text = vim_strsave((char_u *)"");
3732 valid = TRUE;
3733 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
3734 valid = FALSE;
3736 /* Mark entries with non-existing buffer number as not valid. Give the
3737 * error message only once. */
3738 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3740 if (!did_bufnr_emsg)
3742 did_bufnr_emsg = TRUE;
3743 EMSGN(_("E92: Buffer %ld not found"), bufnum);
3745 valid = FALSE;
3746 bufnum = 0;
3749 status = qf_add_entry(qi, &prevp,
3750 NULL, /* dir */
3751 filename,
3752 bufnum,
3753 text,
3754 lnum,
3755 col,
3756 vcol, /* vis_col */
3757 pattern, /* search pattern */
3759 type == NULL ? NUL : *type,
3760 valid);
3762 vim_free(filename);
3763 vim_free(pattern);
3764 vim_free(text);
3765 vim_free(type);
3767 if (status == FAIL)
3769 retval = FAIL;
3770 break;
3774 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
3775 /* empty list or no valid entry */
3776 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
3777 else
3778 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3779 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3780 qi->qf_lists[qi->qf_curlist].qf_index = 1;
3782 #ifdef FEAT_WINDOWS
3783 qf_update_buffer(qi);
3784 #endif
3786 return retval;
3788 #endif
3791 * ":[range]cbuffer [bufnr]" command.
3792 * ":[range]caddbuffer [bufnr]" command.
3793 * ":[range]cgetbuffer [bufnr]" command.
3794 * ":[range]lbuffer [bufnr]" command.
3795 * ":[range]laddbuffer [bufnr]" command.
3796 * ":[range]lgetbuffer [bufnr]" command.
3798 void
3799 ex_cbuffer(eap)
3800 exarg_T *eap;
3802 buf_T *buf = NULL;
3803 qf_info_T *qi = &ql_info;
3805 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
3806 || eap->cmdidx == CMD_laddbuffer)
3808 qi = ll_get_or_alloc_list(curwin);
3809 if (qi == NULL)
3810 return;
3813 if (*eap->arg == NUL)
3814 buf = curbuf;
3815 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
3816 buf = buflist_findnr(atoi((char *)eap->arg));
3817 if (buf == NULL)
3818 EMSG(_(e_invarg));
3819 else if (buf->b_ml.ml_mfp == NULL)
3820 EMSG(_("E681: Buffer is not loaded"));
3821 else
3823 if (eap->addr_count == 0)
3825 eap->line1 = 1;
3826 eap->line2 = buf->b_ml.ml_line_count;
3828 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
3829 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
3830 EMSG(_(e_invrange));
3831 else
3833 char *qf_title = (char *)*eap->cmdlinep;
3834 if (buf->b_sfname)
3836 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)", qf_title,
3837 (char *)buf->b_sfname);
3838 qf_title = (char *)IObuff;
3841 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
3842 (eap->cmdidx != CMD_caddbuffer
3843 && eap->cmdidx != CMD_laddbuffer),
3844 eap->line1, eap->line2,
3845 qf_title) > 0
3846 && (eap->cmdidx == CMD_cbuffer
3847 || eap->cmdidx == CMD_lbuffer))
3848 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
3853 #if defined(FEAT_EVAL) || defined(PROTO)
3855 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
3856 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
3858 void
3859 ex_cexpr(eap)
3860 exarg_T *eap;
3862 typval_T *tv;
3863 qf_info_T *qi = &ql_info;
3865 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
3866 || eap->cmdidx == CMD_laddexpr)
3868 qi = ll_get_or_alloc_list(curwin);
3869 if (qi == NULL)
3870 return;
3873 /* Evaluate the expression. When the result is a string or a list we can
3874 * use it to fill the errorlist. */
3875 tv = eval_expr(eap->arg, NULL);
3876 if (tv != NULL)
3878 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
3879 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
3881 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
3882 (eap->cmdidx != CMD_caddexpr
3883 && eap->cmdidx != CMD_laddexpr),
3884 (linenr_T)0, (linenr_T)0,
3885 (char *)*eap->cmdlinep) > 0
3886 && (eap->cmdidx == CMD_cexpr
3887 || eap->cmdidx == CMD_lexpr))
3888 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
3890 else
3891 EMSG(_("E777: String or List expected"));
3892 free_tv(tv);
3895 #endif
3898 * ":helpgrep {pattern}"
3900 void
3901 ex_helpgrep(eap)
3902 exarg_T *eap;
3904 regmatch_T regmatch;
3905 char_u *save_cpo;
3906 char_u *p;
3907 int fcount;
3908 char_u **fnames;
3909 FILE *fd;
3910 int fi;
3911 qfline_T *prevp = NULL;
3912 long lnum;
3913 #ifdef FEAT_MULTI_LANG
3914 char_u *lang;
3915 #endif
3916 qf_info_T *qi = &ql_info;
3917 int new_qi = FALSE;
3918 win_T *wp;
3920 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
3921 save_cpo = p_cpo;
3922 p_cpo = empty_option;
3924 #ifdef FEAT_MULTI_LANG
3925 /* Check for a specified language */
3926 lang = check_help_lang(eap->arg);
3927 #endif
3929 if (eap->cmdidx == CMD_lhelpgrep)
3931 /* Find an existing help window */
3932 FOR_ALL_WINDOWS(wp)
3933 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
3934 break;
3936 if (wp == NULL) /* Help window not found */
3937 qi = NULL;
3938 else
3939 qi = wp->w_llist;
3941 if (qi == NULL)
3943 /* Allocate a new location list for help text matches */
3944 if ((qi = ll_new_list()) == NULL)
3945 return;
3946 new_qi = TRUE;
3950 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
3951 regmatch.rm_ic = FALSE;
3952 if (regmatch.regprog != NULL)
3954 /* create a new quickfix list */
3955 qf_new_list(qi, (char *)*eap->cmdlinep);
3957 /* Go through all directories in 'runtimepath' */
3958 p = p_rtp;
3959 while (*p != NUL && !got_int)
3961 copy_option_part(&p, NameBuff, MAXPATHL, ",");
3963 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
3964 add_pathsep(NameBuff);
3965 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
3966 if (gen_expand_wildcards(1, &NameBuff, &fcount,
3967 &fnames, EW_FILE|EW_SILENT) == OK
3968 && fcount > 0)
3970 for (fi = 0; fi < fcount && !got_int; ++fi)
3972 #ifdef FEAT_MULTI_LANG
3973 /* Skip files for a different language. */
3974 if (lang != NULL
3975 && STRNICMP(lang, fnames[fi]
3976 + STRLEN(fnames[fi]) - 3, 2) != 0
3977 && !(STRNICMP(lang, "en", 2) == 0
3978 && STRNICMP("txt", fnames[fi]
3979 + STRLEN(fnames[fi]) - 3, 3) == 0))
3980 continue;
3981 #endif
3982 fd = mch_fopen((char *)fnames[fi], "r");
3983 if (fd != NULL)
3985 lnum = 1;
3986 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
3988 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
3990 int l = (int)STRLEN(IObuff);
3992 /* remove trailing CR, LF, spaces, etc. */
3993 while (l > 0 && IObuff[l - 1] <= ' ')
3994 IObuff[--l] = NUL;
3996 if (qf_add_entry(qi, &prevp,
3997 NULL, /* dir */
3998 fnames[fi],
4000 IObuff,
4001 lnum,
4002 (int)(regmatch.startp[0] - IObuff)
4003 + 1, /* col */
4004 FALSE, /* vis_col */
4005 NULL, /* search pattern */
4006 0, /* nr */
4007 1, /* type */
4008 TRUE /* valid */
4009 ) == FAIL)
4011 got_int = TRUE;
4012 break;
4015 ++lnum;
4016 line_breakcheck();
4018 fclose(fd);
4021 FreeWild(fcount, fnames);
4024 vim_free(regmatch.regprog);
4026 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4027 qi->qf_lists[qi->qf_curlist].qf_ptr =
4028 qi->qf_lists[qi->qf_curlist].qf_start;
4029 qi->qf_lists[qi->qf_curlist].qf_index = 1;
4032 if (p_cpo == empty_option)
4033 p_cpo = save_cpo;
4034 else
4035 /* Darn, some plugin changed the value. */
4036 free_string_option(save_cpo);
4038 #ifdef FEAT_WINDOWS
4039 qf_update_buffer(qi);
4040 #endif
4042 /* Jump to first match. */
4043 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
4044 qf_jump(qi, 0, 0, FALSE);
4045 else
4046 EMSG2(_(e_nomatch2), eap->arg);
4048 if (eap->cmdidx == CMD_lhelpgrep)
4050 /* If the help window is not opened or if it already points to the
4051 * correct location list, then free the new location list. */
4052 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
4054 if (new_qi)
4055 ll_free_all(&qi);
4057 else if (curwin->w_llist == NULL)
4058 curwin->w_llist = qi;
4062 #endif /* FEAT_QUICKFIX */