blob: fc3a144819a5032d184e913477e578bf60db8ee5 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * quickfix.c: functions for quickfix mode, using a file with error messages
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_QUICKFIX) || defined(PROTO)
17
18struct dir_stack_T
19{
20 struct dir_stack_T *next;
21 char_u *dirname;
22};
23
24static struct dir_stack_T *dir_stack = NULL;
25
26/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000028 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000029typedef struct qfline_S qfline_T;
30struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000031{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000032 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
Bram Moolenaar071d4272004-06-13 20:20:40 +000043 :helpgrep */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000044 char_u qf_valid; /* valid error message detected */
Bram Moolenaar071d4272004-06-13 20:20:40 +000045};
46
47/*
48 * There is a stack of error lists.
49 */
50#define LISTCOUNT 10
51
Bram Moolenaard12f5c12006-01-25 22:10:52 +000052typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000053{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000054 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 */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020059 char_u *qf_title; /* title derived from the command that created
60 * the error list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000061} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000062
Bram Moolenaard12f5c12006-01-25 22:10:52 +000063struct qf_info_S
64{
65 /*
66 * Count of references to this list. Used only for location lists.
67 * When a location list window reference this list, qf_refcount
68 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
69 * reaches 0, the list is freed.
70 */
71 int qf_refcount;
72 int qf_listcount; /* current number of lists */
73 int qf_curlist; /* current error list */
74 qf_list_T qf_lists[LISTCOUNT];
75};
76
77static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaar071d4272004-06-13 20:20:40 +000078
Bram Moolenaar68b76a62005-03-25 21:53:48 +000079#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
81/*
82 * Structure used to hold the info of one part of 'errorformat'
83 */
Bram Moolenaar01265852006-03-20 21:50:15 +000084typedef struct efm_S efm_T;
85struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000086{
87 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +000088 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000089 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
90 char_u prefix; /* prefix of this format line: */
91 /* 'D' enter directory */
92 /* 'X' leave directory */
93 /* 'A' start of multi-line message */
94 /* 'E' error message */
95 /* 'W' warning message */
96 /* 'I' informational message */
97 /* 'C' continuation line */
98 /* 'Z' end of multi-line message */
99 /* 'G' general, unspecific message */
100 /* 'P' push file (partial) message */
101 /* 'Q' pop/quit file (partial) message */
102 /* 'O' overread (partial) message */
103 char_u flags; /* additional flags given in prefix */
104 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000105 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000106 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107};
108
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200109static 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_u *qf_title));
Bram Moolenaarfb604092014-07-23 15:55:00 +0200110static void qf_store_title __ARGS((qf_info_T *qi, char_u *title));
Bram Moolenaar94116152012-11-28 17:41:59 +0100111static void qf_new_list __ARGS((qf_info_T *qi, char_u *qf_title));
Bram Moolenaard9ff7d52008-03-20 12:23:49 +0000112static void ll_free_all __ARGS((qf_info_T **pqi));
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000113static 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));
Bram Moolenaard9ff7d52008-03-20 12:23:49 +0000114static qf_info_T *ll_new_list __ARGS((void));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000115static void qf_msg __ARGS((qf_info_T *qi));
116static void qf_free __ARGS((qf_info_T *qi, int idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117static char_u *qf_types __ARGS((int, int));
118static int qf_get_fnum __ARGS((char_u *, char_u *));
119static char_u *qf_push_dir __ARGS((char_u *, struct dir_stack_T **));
120static char_u *qf_pop_dir __ARGS((struct dir_stack_T **));
121static char_u *qf_guess_filepath __ARGS((char_u *));
122static void qf_fmt_text __ARGS((char_u *text, char_u *buf, int bufsize));
123static void qf_clean_dir_stack __ARGS((struct dir_stack_T **));
124#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000125static int qf_win_pos_update __ARGS((qf_info_T *qi, int old_qf_index));
Bram Moolenaar9c102382006-05-03 21:26:49 +0000126static int is_qf_win __ARGS((win_T *win, qf_info_T *qi));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000127static win_T *qf_find_win __ARGS((qf_info_T *qi));
128static buf_T *qf_find_buf __ARGS((qf_info_T *qi));
129static void qf_update_buffer __ARGS((qf_info_T *qi));
Bram Moolenaarfb604092014-07-23 15:55:00 +0200130static void qf_set_title_var __ARGS((qf_info_T *qi));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000131static void qf_fill_buffer __ARGS((qf_info_T *qi));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132#endif
133static char_u *get_mef_name __ARGS((void));
Bram Moolenaar7f51a822012-04-25 18:57:21 +0200134static void restore_start_dir __ARGS((char_u *dirname_start));
135static buf_T *load_dummy_buffer __ARGS((char_u *fname, char_u *dirname_start, char_u *resulting_dir));
136static void wipe_dummy_buffer __ARGS((buf_T *buf, char_u *dirname_start));
137static void unload_dummy_buffer __ARGS((buf_T *buf, char_u *dirname_start));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000138static qf_info_T *ll_get_or_alloc_list __ARGS((win_T *));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000140/* Quickfix window check helper macro */
141#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
142/* Location list window check helper macro */
143#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
144/*
145 * Return location list for window 'wp'
146 * For location list window, return the referenced location list
147 */
148#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
149
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000151 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200152 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153 * Return -1 for error, number of errors for success.
154 */
155 int
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200156qf_init(wp, efile, errorformat, newlist, qf_title)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000157 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 char_u *efile;
159 char_u *errorformat;
160 int newlist; /* TRUE: start a new error list */
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200161 char_u *qf_title;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000163 qf_info_T *qi = &ql_info;
164
Bram Moolenaar86b68352004-12-27 21:59:20 +0000165 if (efile == NULL)
166 return FAIL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000167
168 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000169 {
170 qi = ll_get_or_alloc_list(wp);
171 if (qi == NULL)
172 return FAIL;
173 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000174
175 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200176 (linenr_T)0, (linenr_T)0,
177 qf_title);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000178}
179
180/*
181 * Read the errorfile "efile" into memory, line by line, building the error
182 * list.
183 * Alternative: when "efile" is null read errors from buffer "buf".
184 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200185 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
186 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +0000187 * Return -1 for error, number of errors for success.
188 */
189 static int
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200190qf_init_ext(qi, efile, buf, tv, errorformat, newlist, lnumfirst, lnumlast,
191 qf_title)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000192 qf_info_T *qi;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000193 char_u *efile;
194 buf_T *buf;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000195 typval_T *tv;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000196 char_u *errorformat;
197 int newlist; /* TRUE: start a new error list */
198 linenr_T lnumfirst; /* first line number to use */
199 linenr_T lnumlast; /* last line number to use */
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200200 char_u *qf_title;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000201{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 char_u *namebuf;
203 char_u *errmsg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000204 char_u *pattern;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 char_u *fmtstr = NULL;
206 int col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000207 char_u use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 int type = 0;
209 int valid;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000210 linenr_T buflnum = lnumfirst;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 long lnum = 0L;
212 int enr = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000213 FILE *fd = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000214 qfline_T *qfprev = NULL; /* init to make SASC shut up */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 char_u *efmp;
Bram Moolenaar01265852006-03-20 21:50:15 +0000216 efm_T *fmt_first = NULL;
217 efm_T *fmt_last = NULL;
218 efm_T *fmt_ptr;
219 efm_T *fmt_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 char_u *efm;
221 char_u *ptr;
222 char_u *srcptr;
223 int len;
224 int i;
225 int round;
226 int idx = 0;
227 int multiline = FALSE;
228 int multiignore = FALSE;
229 int multiscan = FALSE;
230 int retval = -1; /* default: return error flag */
231 char_u *directory = NULL;
232 char_u *currfile = NULL;
233 char_u *tail = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000234 char_u *p_str = NULL;
235 listitem_T *p_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 struct dir_stack_T *file_stack = NULL;
237 regmatch_T regmatch;
238 static struct fmtpattern
239 {
240 char_u convchar;
241 char *pattern;
242 } fmt_pat[FMT_PATTERNS] =
243 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000244 {'f', ".\\+"}, /* only used when at end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 {'n', "\\d\\+"},
246 {'l', "\\d\\+"},
247 {'c', "\\d\\+"},
248 {'t', "."},
249 {'m', ".\\+"},
250 {'r', ".*"},
Bram Moolenaarf13de072012-06-01 18:34:41 +0200251 {'p', "[- .]*"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000252 {'v', "\\d\\+"},
253 {'s', ".\\+"}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 };
255
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 namebuf = alloc(CMDBUFFSIZE + 1);
257 errmsg = alloc(CMDBUFFSIZE + 1);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000258 pattern = alloc(CMDBUFFSIZE + 1);
259 if (namebuf == NULL || errmsg == NULL || pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 goto qf_init_end;
261
Bram Moolenaar86b68352004-12-27 21:59:20 +0000262 if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 {
264 EMSG2(_(e_openerrf), efile);
265 goto qf_init_end;
266 }
267
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000268 if (newlist || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +0100270 qf_new_list(qi, qf_title);
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000271 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000273 for (qfprev = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200274 qfprev->qf_next != qfprev; qfprev = qfprev->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 ;
276
277/*
278 * Each part of the format string is copied and modified from errorformat to
279 * regex prog. Only a few % characters are allowed.
280 */
281 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000282 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +0000283 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 else
285 efm = errorformat;
286 /*
287 * Get some space to modify the format string into.
288 */
289 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
290 for (round = FMT_PATTERNS; round > 0; )
291 i += (int)STRLEN(fmt_pat[--round].pattern);
292#ifdef COLON_IN_FILENAME
293 i += 12; /* "%f" can become twelve chars longer */
294#else
295 i += 2; /* "%f" can become two chars longer */
296#endif
297 if ((fmtstr = alloc(i)) == NULL)
298 goto error2;
299
Bram Moolenaar01265852006-03-20 21:50:15 +0000300 while (efm[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 {
302 /*
303 * Allocate a new eformat structure and put it at the end of the list
304 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000305 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 if (fmt_ptr == NULL)
307 goto error2;
308 if (fmt_first == NULL) /* first one */
309 fmt_first = fmt_ptr;
310 else
311 fmt_last->next = fmt_ptr;
312 fmt_last = fmt_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313
314 /*
315 * Isolate one part in the 'errorformat' option
316 */
317 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
318 if (efm[len] == '\\' && efm[len + 1] != NUL)
319 ++len;
320
321 /*
322 * Build regexp pattern from current 'errorformat' option
323 */
324 ptr = fmtstr;
325 *ptr++ = '^';
Bram Moolenaar01265852006-03-20 21:50:15 +0000326 round = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 for (efmp = efm; efmp < efm + len; ++efmp)
328 {
329 if (*efmp == '%')
330 {
331 ++efmp;
332 for (idx = 0; idx < FMT_PATTERNS; ++idx)
333 if (fmt_pat[idx].convchar == *efmp)
334 break;
335 if (idx < FMT_PATTERNS)
336 {
337 if (fmt_ptr->addr[idx])
338 {
339 sprintf((char *)errmsg,
340 _("E372: Too many %%%c in format string"), *efmp);
341 EMSG(errmsg);
342 goto error2;
343 }
344 if ((idx
345 && idx < 6
346 && vim_strchr((char_u *)"DXOPQ",
347 fmt_ptr->prefix) != NULL)
348 || (idx == 6
349 && vim_strchr((char_u *)"OPQ",
350 fmt_ptr->prefix) == NULL))
351 {
352 sprintf((char *)errmsg,
353 _("E373: Unexpected %%%c in format string"), *efmp);
354 EMSG(errmsg);
355 goto error2;
356 }
357 fmt_ptr->addr[idx] = (char_u)++round;
358 *ptr++ = '\\';
359 *ptr++ = '(';
360#ifdef BACKSLASH_IN_FILENAME
361 if (*efmp == 'f')
362 {
363 /* Also match "c:" in the file name, even when
364 * checking for a colon next: "%f:".
365 * "\%(\a:\)\=" */
366 STRCPY(ptr, "\\%(\\a:\\)\\=");
367 ptr += 10;
368 }
369#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +0000370 if (*efmp == 'f' && efmp[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000372 if (efmp[1] != '\\' && efmp[1] != '%')
373 {
374 /* A file name may contain spaces, but this isn't
375 * in "\f". For "%f:%l:%m" there may be a ":" in
376 * the file name. Use ".\{-1,}x" instead (x is
377 * the next character), the requirement that :999:
378 * follows should work. */
379 STRCPY(ptr, ".\\{-1,}");
380 ptr += 7;
381 }
382 else
383 {
384 /* File name followed by '\\' or '%': include as
385 * many file name chars as possible. */
386 STRCPY(ptr, "\\f\\+");
387 ptr += 4;
388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 }
390 else
391 {
392 srcptr = (char_u *)fmt_pat[idx].pattern;
393 while ((*ptr = *srcptr++) != NUL)
394 ++ptr;
395 }
396 *ptr++ = '\\';
397 *ptr++ = ')';
398 }
399 else if (*efmp == '*')
400 {
401 if (*++efmp == '[' || *efmp == '\\')
402 {
403 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
404 {
405 if (efmp[1] == '^')
406 *ptr++ = *++efmp;
407 if (efmp < efm + len)
408 {
409 *ptr++ = *++efmp; /* could be ']' */
410 while (efmp < efm + len
411 && (*ptr++ = *++efmp) != ']')
412 /* skip */;
413 if (efmp == efm + len)
414 {
415 EMSG(_("E374: Missing ] in format string"));
416 goto error2;
417 }
418 }
419 }
420 else if (efmp < efm + len) /* %*\D, %*\s etc. */
421 *ptr++ = *++efmp;
422 *ptr++ = '\\';
423 *ptr++ = '+';
424 }
425 else
426 {
427 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
428 sprintf((char *)errmsg,
429 _("E375: Unsupported %%%c in format string"), *efmp);
430 EMSG(errmsg);
431 goto error2;
432 }
433 }
434 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
435 *ptr++ = *efmp; /* regexp magic characters */
436 else if (*efmp == '#')
437 *ptr++ = '*';
Bram Moolenaar01265852006-03-20 21:50:15 +0000438 else if (*efmp == '>')
439 fmt_ptr->conthere = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 else if (efmp == efm + 1) /* analyse prefix */
441 {
442 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
443 fmt_ptr->flags = *efmp++;
444 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
445 fmt_ptr->prefix = *efmp;
446 else
447 {
448 sprintf((char *)errmsg,
449 _("E376: Invalid %%%c in format string prefix"), *efmp);
450 EMSG(errmsg);
451 goto error2;
452 }
453 }
454 else
455 {
456 sprintf((char *)errmsg,
457 _("E377: Invalid %%%c in format string"), *efmp);
458 EMSG(errmsg);
459 goto error2;
460 }
461 }
462 else /* copy normal character */
463 {
464 if (*efmp == '\\' && efmp + 1 < efm + len)
465 ++efmp;
466 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
467 *ptr++ = '\\'; /* escape regexp atoms */
468 if (*efmp)
469 *ptr++ = *efmp;
470 }
471 }
472 *ptr++ = '$';
473 *ptr = NUL;
474 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
475 goto error2;
476 /*
477 * Advance to next part
478 */
479 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
480 }
481 if (fmt_first == NULL) /* nothing found */
482 {
483 EMSG(_("E378: 'errorformat' contains no pattern"));
484 goto error2;
485 }
486
487 /*
488 * got_int is reset here, because it was probably set when killing the
489 * ":make" command, but we still want to read the errorfile then.
490 */
491 got_int = FALSE;
492
493 /* Always ignore case when looking for a matching error. */
494 regmatch.rm_ic = TRUE;
495
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000496 if (tv != NULL)
497 {
498 if (tv->v_type == VAR_STRING)
499 p_str = tv->vval.v_string;
500 else if (tv->v_type == VAR_LIST)
501 p_li = tv->vval.v_list->lv_first;
502 }
503
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 /*
505 * Read the lines in the error file one by one.
506 * Try to recognize one of the error formats in each line.
507 */
Bram Moolenaar86b68352004-12-27 21:59:20 +0000508 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 {
Bram Moolenaar86b68352004-12-27 21:59:20 +0000510 /* Get the next line. */
511 if (fd == NULL)
512 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000513 if (tv != NULL)
514 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000515 if (tv->v_type == VAR_STRING)
516 {
517 /* Get the next line from the supplied string */
518 char_u *p;
519
520 if (!*p_str) /* Reached the end of the string */
521 break;
522
523 p = vim_strchr(p_str, '\n');
524 if (p)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000525 len = (int)(p - p_str + 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000526 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000527 len = (int)STRLEN(p_str);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000528
529 if (len > CMDBUFFSIZE - 2)
530 vim_strncpy(IObuff, p_str, CMDBUFFSIZE - 2);
531 else
532 vim_strncpy(IObuff, p_str, len);
533
534 p_str += len;
535 }
536 else if (tv->v_type == VAR_LIST)
537 {
538 /* Get the next line from the supplied list */
539 while (p_li && p_li->li_tv.v_type != VAR_STRING)
540 p_li = p_li->li_next; /* Skip non-string items */
541
542 if (!p_li) /* End of the list */
543 break;
544
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000545 len = (int)STRLEN(p_li->li_tv.vval.v_string);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000546 if (len > CMDBUFFSIZE - 2)
547 len = CMDBUFFSIZE - 2;
548
549 vim_strncpy(IObuff, p_li->li_tv.vval.v_string, len);
550
551 p_li = p_li->li_next; /* next item */
552 }
553 }
554 else
555 {
556 /* Get the next line from the supplied buffer */
557 if (buflnum > lnumlast)
558 break;
559 vim_strncpy(IObuff, ml_get_buf(buf, buflnum++, FALSE),
560 CMDBUFFSIZE - 2);
561 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000562 }
563 else if (fgets((char *)IObuff, CMDBUFFSIZE - 2, fd) == NULL)
564 break;
565
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 IObuff[CMDBUFFSIZE - 2] = NUL; /* for very long lines */
Bram Moolenaar836082d2011-08-10 13:21:46 +0200567#ifdef FEAT_MBYTE
568 remove_bom(IObuff);
569#endif
570
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 if ((efmp = vim_strrchr(IObuff, '\n')) != NULL)
572 *efmp = NUL;
573#ifdef USE_CRNL
574 if ((efmp = vim_strrchr(IObuff, '\r')) != NULL)
575 *efmp = NUL;
576#endif
577
Bram Moolenaar01265852006-03-20 21:50:15 +0000578 /* If there was no %> item start at the first pattern */
579 if (fmt_start == NULL)
580 fmt_ptr = fmt_first;
581 else
582 {
583 fmt_ptr = fmt_start;
584 fmt_start = NULL;
585 }
586
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 /*
588 * Try to match each part of 'errorformat' until we find a complete
589 * match or no match.
590 */
591 valid = TRUE;
592restofline:
Bram Moolenaar01265852006-03-20 21:50:15 +0000593 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 {
595 idx = fmt_ptr->prefix;
596 if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
597 continue;
598 namebuf[0] = NUL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000599 pattern[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 if (!multiscan)
601 errmsg[0] = NUL;
602 lnum = 0;
603 col = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000604 use_viscol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 enr = -1;
606 type = 0;
607 tail = NULL;
608
609 regmatch.regprog = fmt_ptr->prog;
610 if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
611 {
612 if ((idx == 'C' || idx == 'Z') && !multiline)
613 continue;
614 if (vim_strchr((char_u *)"EWI", idx) != NULL)
615 type = idx;
616 else
617 type = 0;
618 /*
Bram Moolenaar4169da72006-06-20 18:49:32 +0000619 * Extract error message data from matched line.
620 * We check for an actual submatch, because "\[" and "\]" in
621 * the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 */
623 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
624 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000625 int c;
626
627 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
628 continue;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000629
630 /* Expand ~/file and $HOME/file to full path. */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000631 c = *regmatch.endp[i];
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000632 *regmatch.endp[i] = NUL;
633 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
634 *regmatch.endp[i] = c;
635
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 if (vim_strchr((char_u *)"OPQ", idx) != NULL
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000637 && mch_getperm(namebuf) == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 continue;
639 }
640 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000641 {
642 if (regmatch.startp[i] == NULL)
643 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 enr = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000647 {
648 if (regmatch.startp[i] == NULL)
649 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 lnum = atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000653 {
654 if (regmatch.startp[i] == NULL)
655 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar4169da72006-06-20 18:49:32 +0000657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000659 {
660 if (regmatch.startp[i] == NULL)
661 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 type = *regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000663 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000664 if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 STRCPY(errmsg, IObuff);
666 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
667 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000668 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
669 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaarbbebc852005-07-18 21:47:53 +0000671 vim_strncpy(errmsg, regmatch.startp[i], len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 }
673 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
Bram Moolenaar4169da72006-06-20 18:49:32 +0000674 {
675 if (regmatch.startp[i] == NULL)
676 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 tail = regmatch.startp[i];
Bram Moolenaar4169da72006-06-20 18:49:32 +0000678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
680 {
Bram Moolenaarf13de072012-06-01 18:34:41 +0200681 char_u *match_ptr;
682
Bram Moolenaar4169da72006-06-20 18:49:32 +0000683 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
684 continue;
Bram Moolenaarf13de072012-06-01 18:34:41 +0200685 col = 0;
686 for (match_ptr = regmatch.startp[i];
687 match_ptr != regmatch.endp[i]; ++match_ptr)
688 {
689 ++col;
690 if (*match_ptr == TAB)
691 {
692 col += 7;
693 col -= col % 8;
694 }
695 }
696 ++col;
697 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 }
699 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
700 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000701 if (regmatch.startp[i] == NULL)
702 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 col = (int)atol((char *)regmatch.startp[i]);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000704 use_viscol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000706 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
707 {
Bram Moolenaar4169da72006-06-20 18:49:32 +0000708 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
709 continue;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000710 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
711 if (len > CMDBUFFSIZE - 5)
712 len = CMDBUFFSIZE - 5;
713 STRCPY(pattern, "^\\V");
714 STRNCAT(pattern, regmatch.startp[i], len);
715 pattern[len + 3] = '\\';
716 pattern[len + 4] = '$';
717 pattern[len + 5] = NUL;
718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 break;
720 }
721 }
722 multiscan = FALSE;
Bram Moolenaar01265852006-03-20 21:50:15 +0000723
Bram Moolenaar4770d092006-01-12 23:22:24 +0000724 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 {
Bram Moolenaar4770d092006-01-12 23:22:24 +0000726 if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 {
728 if (idx == 'D') /* enter directory */
729 {
730 if (*namebuf == NUL)
731 {
732 EMSG(_("E379: Missing or empty directory name"));
733 goto error2;
734 }
735 if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
736 goto error2;
737 }
738 else if (idx == 'X') /* leave directory */
739 directory = qf_pop_dir(&dir_stack);
740 }
741 namebuf[0] = NUL; /* no match found, remove file name */
742 lnum = 0; /* don't jump to this line */
743 valid = FALSE;
744 STRCPY(errmsg, IObuff); /* copy whole line to error message */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000745 if (fmt_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 multiline = multiignore = FALSE;
747 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000748 else if (fmt_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 {
Bram Moolenaar01265852006-03-20 21:50:15 +0000750 /* honor %> item */
751 if (fmt_ptr->conthere)
752 fmt_start = fmt_ptr;
753
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
Bram Moolenaar8eded092014-03-12 19:41:55 +0100755 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 multiline = TRUE; /* start of a multi-line message */
Bram Moolenaar8eded092014-03-12 19:41:55 +0100757 multiignore = FALSE; /* reset continuation */
758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
760 { /* continuation of multi-line msg */
761 if (qfprev == NULL)
762 goto error2;
763 if (*errmsg && !multiignore)
764 {
765 len = (int)STRLEN(qfprev->qf_text);
766 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
767 == NULL)
768 goto error2;
769 STRCPY(ptr, qfprev->qf_text);
770 vim_free(qfprev->qf_text);
771 qfprev->qf_text = ptr;
772 *(ptr += len) = '\n';
773 STRCPY(++ptr, errmsg);
774 }
775 if (qfprev->qf_nr == -1)
776 qfprev->qf_nr = enr;
777 if (vim_isprintc(type) && !qfprev->qf_type)
778 qfprev->qf_type = type; /* only printable chars allowed */
779 if (!qfprev->qf_lnum)
780 qfprev->qf_lnum = lnum;
781 if (!qfprev->qf_col)
782 qfprev->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000783 qfprev->qf_viscol = use_viscol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 if (!qfprev->qf_fnum)
785 qfprev->qf_fnum = qf_get_fnum(directory,
786 *namebuf || directory ? namebuf
787 : currfile && valid ? currfile : 0);
788 if (idx == 'Z')
789 multiline = multiignore = FALSE;
790 line_breakcheck();
791 continue;
792 }
793 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
794 {
795 /* global file names */
796 valid = FALSE;
797 if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
798 {
799 if (*namebuf && idx == 'P')
800 currfile = qf_push_dir(namebuf, &file_stack);
801 else if (idx == 'Q')
802 currfile = qf_pop_dir(&file_stack);
803 *namebuf = NUL;
804 if (tail && *tail)
805 {
Bram Moolenaarc236c162008-07-13 17:41:49 +0000806 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 multiscan = TRUE;
808 goto restofline;
809 }
810 }
811 }
812 if (fmt_ptr->flags == '-') /* generally exclude this line */
813 {
814 if (multiline)
815 multiignore = TRUE; /* also exclude continuation lines */
816 continue;
817 }
818 }
819
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000820 if (qf_add_entry(qi, &qfprev,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 directory,
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000822 (*namebuf || directory)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 ? namebuf
Bram Moolenaar9d75c832005-01-25 21:57:23 +0000824 : ((currfile && valid) ? currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000825 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 errmsg,
827 lnum,
828 col,
Bram Moolenaar05159a02005-02-26 23:04:13 +0000829 use_viscol,
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000830 pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 enr,
832 type,
833 valid) == FAIL)
834 goto error2;
835 line_breakcheck();
836 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000837 if (fd == NULL || !ferror(fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000839 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000841 /* no valid entry found */
842 qi->qf_lists[qi->qf_curlist].qf_ptr =
843 qi->qf_lists[qi->qf_curlist].qf_start;
844 qi->qf_lists[qi->qf_curlist].qf_index = 1;
845 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 }
847 else
848 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000849 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
850 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
851 qi->qf_lists[qi->qf_curlist].qf_ptr =
852 qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000854 /* return number of matches */
855 retval = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 goto qf_init_ok;
857 }
858 EMSG(_(e_readerrf));
859error2:
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000860 qf_free(qi, qi->qf_curlist);
861 qi->qf_listcount--;
862 if (qi->qf_curlist > 0)
863 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864qf_init_ok:
Bram Moolenaar86b68352004-12-27 21:59:20 +0000865 if (fd != NULL)
866 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
868 {
869 fmt_first = fmt_ptr->next;
Bram Moolenaar473de612013-06-08 18:19:48 +0200870 vim_regfree(fmt_ptr->prog);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 vim_free(fmt_ptr);
872 }
873 qf_clean_dir_stack(&dir_stack);
874 qf_clean_dir_stack(&file_stack);
875qf_init_end:
876 vim_free(namebuf);
877 vim_free(errmsg);
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000878 vim_free(pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 vim_free(fmtstr);
880
881#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000882 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883#endif
884
885 return retval;
886}
887
Bram Moolenaarfb604092014-07-23 15:55:00 +0200888 static void
889qf_store_title(qi, title)
890 qf_info_T *qi;
891 char_u *title;
892{
893 if (title != NULL)
894 {
895 char_u *p = alloc((int)STRLEN(title) + 2);
896
897 qi->qf_lists[qi->qf_curlist].qf_title = p;
898 if (p != NULL)
899 sprintf((char *)p, ":%s", (char *)title);
900 }
901}
902
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903/*
904 * Prepare for adding a new quickfix list.
905 */
906 static void
Bram Moolenaar94116152012-11-28 17:41:59 +0100907qf_new_list(qi, qf_title)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000908 qf_info_T *qi;
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200909 char_u *qf_title;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910{
911 int i;
912
913 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +0200914 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 * the current entry. This makes it possible to browse in a tree-like
916 * way with ":grep'.
917 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000918 while (qi->qf_listcount > qi->qf_curlist + 1)
919 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920
921 /*
922 * When the stack is full, remove to oldest entry
923 * Otherwise, add a new entry.
924 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000925 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000927 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000929 qi->qf_lists[i - 1] = qi->qf_lists[i];
930 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 }
932 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000933 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +0100934 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaarfb604092014-07-23 15:55:00 +0200935 qf_store_title(qi, qf_title);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936}
937
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000938/*
939 * Free a location list
940 */
941 static void
942ll_free_all(pqi)
943 qf_info_T **pqi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000944{
945 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000946 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000947
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000948 qi = *pqi;
949 if (qi == NULL)
950 return;
951 *pqi = NULL; /* Remove reference to this list */
952
953 qi->qf_refcount--;
954 if (qi->qf_refcount < 1)
955 {
956 /* No references to this location list */
957 for (i = 0; i < qi->qf_listcount; ++i)
958 qf_free(qi, i);
959 vim_free(qi);
960 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000961}
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000962
963 void
964qf_free_all(wp)
965 win_T *wp;
966{
967 int i;
968 qf_info_T *qi = &ql_info;
969
970 if (wp != NULL)
971 {
972 /* location list */
973 ll_free_all(&wp->w_llist);
974 ll_free_all(&wp->w_llist_ref);
975 }
976 else
977 /* quickfix list */
978 for (i = 0; i < qi->qf_listcount; ++i)
979 qf_free(qi, i);
980}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000981
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982/*
983 * Add an entry to the end of the list of errors.
984 * Returns OK or FAIL.
985 */
986 static int
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000987qf_add_entry(qi, prevp, dir, fname, bufnum, mesg, lnum, col, vis_col, pattern,
988 nr, type, valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000989 qf_info_T *qi; /* quickfix list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000990 qfline_T **prevp; /* pointer to previously added entry or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 char_u *dir; /* optional directory name */
992 char_u *fname; /* file name or NULL */
Bram Moolenaar48b66fb2007-02-04 01:58:18 +0000993 int bufnum; /* buffer number or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 char_u *mesg; /* message */
995 long lnum; /* line number */
996 int col; /* column */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000997 int vis_col; /* using visual column */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000998 char_u *pattern; /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 int nr; /* error number */
1000 int type; /* type character */
1001 int valid; /* valid entry */
1002{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001003 qfline_T *qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001005 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001007 if (bufnum != 0)
1008 qfp->qf_fnum = bufnum;
1009 else
1010 qfp->qf_fnum = qf_get_fnum(dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1012 {
1013 vim_free(qfp);
1014 return FAIL;
1015 }
1016 qfp->qf_lnum = lnum;
1017 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001018 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001019 if (pattern == NULL || *pattern == NUL)
1020 qfp->qf_pattern = NULL;
1021 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1022 {
1023 vim_free(qfp->qf_text);
1024 vim_free(qfp);
1025 return FAIL;
1026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 qfp->qf_nr = nr;
1028 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1029 type = 0;
1030 qfp->qf_type = type;
1031 qfp->qf_valid = valid;
1032
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001033 if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
1034 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001036 qi->qf_lists[qi->qf_curlist].qf_start = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 qfp->qf_prev = qfp; /* first element points to itself */
1038 }
1039 else
1040 {
1041 qfp->qf_prev = *prevp;
1042 (*prevp)->qf_next = qfp;
1043 }
1044 qfp->qf_next = qfp; /* last element points to itself */
1045 qfp->qf_cleared = FALSE;
1046 *prevp = qfp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001047 ++qi->qf_lists[qi->qf_curlist].qf_count;
1048 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
1049 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001051 qi->qf_lists[qi->qf_curlist].qf_index =
1052 qi->qf_lists[qi->qf_curlist].qf_count;
1053 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 }
1055
1056 return OK;
1057}
1058
1059/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001060 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001061 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001062 static qf_info_T *
1063ll_new_list()
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001064{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001065 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001066
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001067 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1068 if (qi != NULL)
1069 {
1070 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1071 qi->qf_refcount++;
1072 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001073
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001074 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001075}
1076
1077/*
1078 * Return the location list for window 'wp'.
1079 * If not present, allocate a location list
1080 */
1081 static qf_info_T *
1082ll_get_or_alloc_list(wp)
1083 win_T *wp;
1084{
1085 if (IS_LL_WINDOW(wp))
1086 /* For a location list window, use the referenced location list */
1087 return wp->w_llist_ref;
1088
1089 /*
1090 * For a non-location list window, w_llist_ref should not point to a
1091 * location list.
1092 */
1093 ll_free_all(&wp->w_llist_ref);
1094
1095 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001096 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001097 return wp->w_llist;
1098}
1099
1100/*
1101 * Copy the location list from window "from" to window "to".
1102 */
1103 void
1104copy_loclist(from, to)
1105 win_T *from;
1106 win_T *to;
1107{
1108 qf_info_T *qi;
1109 int idx;
1110 int i;
1111
1112 /*
1113 * When copying from a location list window, copy the referenced
1114 * location list. For other windows, copy the location list for
1115 * that window.
1116 */
1117 if (IS_LL_WINDOW(from))
1118 qi = from->w_llist_ref;
1119 else
1120 qi = from->w_llist;
1121
1122 if (qi == NULL) /* no location list to copy */
1123 return;
1124
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001125 /* allocate a new location list */
1126 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001127 return;
1128
1129 to->w_llist->qf_listcount = qi->qf_listcount;
1130
1131 /* Copy the location lists one at a time */
1132 for (idx = 0; idx < qi->qf_listcount; idx++)
1133 {
1134 qf_list_T *from_qfl;
1135 qf_list_T *to_qfl;
1136
1137 to->w_llist->qf_curlist = idx;
1138
1139 from_qfl = &qi->qf_lists[idx];
1140 to_qfl = &to->w_llist->qf_lists[idx];
1141
1142 /* Some of the fields are populated by qf_add_entry() */
1143 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1144 to_qfl->qf_count = 0;
1145 to_qfl->qf_index = 0;
1146 to_qfl->qf_start = NULL;
1147 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001148 if (from_qfl->qf_title != NULL)
1149 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1150 else
1151 to_qfl->qf_title = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001152
1153 if (from_qfl->qf_count)
1154 {
1155 qfline_T *from_qfp;
1156 qfline_T *prevp = NULL;
1157
1158 /* copy all the location entries in this list */
1159 for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
1160 ++i, from_qfp = from_qfp->qf_next)
1161 {
1162 if (qf_add_entry(to->w_llist, &prevp,
1163 NULL,
1164 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001165 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001166 from_qfp->qf_text,
1167 from_qfp->qf_lnum,
1168 from_qfp->qf_col,
1169 from_qfp->qf_viscol,
1170 from_qfp->qf_pattern,
1171 from_qfp->qf_nr,
1172 0,
1173 from_qfp->qf_valid) == FAIL)
1174 {
1175 qf_free_all(to);
1176 return;
1177 }
1178 /*
1179 * qf_add_entry() will not set the qf_num field, as the
1180 * directory and file names are not supplied. So the qf_fnum
1181 * field is copied here.
1182 */
1183 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1184 prevp->qf_type = from_qfp->qf_type; /* error type */
1185 if (from_qfl->qf_ptr == from_qfp)
1186 to_qfl->qf_ptr = prevp; /* current location */
1187 }
1188 }
1189
1190 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1191
1192 /* When no valid entries are present in the list, qf_ptr points to
1193 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001194 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001195 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001196 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001197 to_qfl->qf_index = 1;
1198 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001199 }
1200
1201 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1202}
1203
1204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 * get buffer number for file "dir.name"
1206 */
1207 static int
1208qf_get_fnum(directory, fname)
1209 char_u *directory;
1210 char_u *fname;
1211{
1212 if (fname == NULL || *fname == NUL) /* no file name */
1213 return 0;
1214 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 char_u *ptr;
1216 int fnum;
1217
Bram Moolenaare60acc12011-05-10 16:41:25 +02001218#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001220#endif
1221#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 if (directory != NULL)
1223 slash_adjust(directory);
1224 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 if (directory != NULL && !vim_isAbsName(fname)
1227 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1228 {
1229 /*
1230 * Here we check if the file really exists.
1231 * This should normally be true, but if make works without
1232 * "leaving directory"-messages we might have missed a
1233 * directory change.
1234 */
1235 if (mch_getperm(ptr) < 0)
1236 {
1237 vim_free(ptr);
1238 directory = qf_guess_filepath(fname);
1239 if (directory)
1240 ptr = concat_fnames(directory, fname, TRUE);
1241 else
1242 ptr = vim_strsave(fname);
1243 }
1244 /* Use concatenated directory name and file name */
1245 fnum = buflist_add(ptr, 0);
1246 vim_free(ptr);
1247 return fnum;
1248 }
1249 return buflist_add(fname, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 }
1251}
1252
1253/*
1254 * push dirbuf onto the directory stack and return pointer to actual dir or
1255 * NULL on error
1256 */
1257 static char_u *
1258qf_push_dir(dirbuf, stackptr)
1259 char_u *dirbuf;
1260 struct dir_stack_T **stackptr;
1261{
1262 struct dir_stack_T *ds_new;
1263 struct dir_stack_T *ds_ptr;
1264
1265 /* allocate new stack element and hook it in */
1266 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1267 if (ds_new == NULL)
1268 return NULL;
1269
1270 ds_new->next = *stackptr;
1271 *stackptr = ds_new;
1272
1273 /* store directory on the stack */
1274 if (vim_isAbsName(dirbuf)
1275 || (*stackptr)->next == NULL
1276 || (*stackptr && dir_stack != *stackptr))
1277 (*stackptr)->dirname = vim_strsave(dirbuf);
1278 else
1279 {
1280 /* Okay we don't have an absolute path.
1281 * dirbuf must be a subdir of one of the directories on the stack.
1282 * Let's search...
1283 */
1284 ds_new = (*stackptr)->next;
1285 (*stackptr)->dirname = NULL;
1286 while (ds_new)
1287 {
1288 vim_free((*stackptr)->dirname);
1289 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1290 TRUE);
1291 if (mch_isdir((*stackptr)->dirname) == TRUE)
1292 break;
1293
1294 ds_new = ds_new->next;
1295 }
1296
1297 /* clean up all dirs we already left */
1298 while ((*stackptr)->next != ds_new)
1299 {
1300 ds_ptr = (*stackptr)->next;
1301 (*stackptr)->next = (*stackptr)->next->next;
1302 vim_free(ds_ptr->dirname);
1303 vim_free(ds_ptr);
1304 }
1305
1306 /* Nothing found -> it must be on top level */
1307 if (ds_new == NULL)
1308 {
1309 vim_free((*stackptr)->dirname);
1310 (*stackptr)->dirname = vim_strsave(dirbuf);
1311 }
1312 }
1313
1314 if ((*stackptr)->dirname != NULL)
1315 return (*stackptr)->dirname;
1316 else
1317 {
1318 ds_ptr = *stackptr;
1319 *stackptr = (*stackptr)->next;
1320 vim_free(ds_ptr);
1321 return NULL;
1322 }
1323}
1324
1325
1326/*
1327 * pop dirbuf from the directory stack and return previous directory or NULL if
1328 * stack is empty
1329 */
1330 static char_u *
1331qf_pop_dir(stackptr)
1332 struct dir_stack_T **stackptr;
1333{
1334 struct dir_stack_T *ds_ptr;
1335
1336 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1337 * What to do if it isn't? */
1338
1339 /* pop top element and free it */
1340 if (*stackptr != NULL)
1341 {
1342 ds_ptr = *stackptr;
1343 *stackptr = (*stackptr)->next;
1344 vim_free(ds_ptr->dirname);
1345 vim_free(ds_ptr);
1346 }
1347
1348 /* return NEW top element as current dir or NULL if stack is empty*/
1349 return *stackptr ? (*stackptr)->dirname : NULL;
1350}
1351
1352/*
1353 * clean up directory stack
1354 */
1355 static void
1356qf_clean_dir_stack(stackptr)
1357 struct dir_stack_T **stackptr;
1358{
1359 struct dir_stack_T *ds_ptr;
1360
1361 while ((ds_ptr = *stackptr) != NULL)
1362 {
1363 *stackptr = (*stackptr)->next;
1364 vim_free(ds_ptr->dirname);
1365 vim_free(ds_ptr);
1366 }
1367}
1368
1369/*
1370 * Check in which directory of the directory stack the given file can be
1371 * found.
1372 * Returns a pointer to the directory name or NULL if not found
1373 * Cleans up intermediate directory entries.
1374 *
1375 * TODO: How to solve the following problem?
1376 * If we have the this directory tree:
1377 * ./
1378 * ./aa
1379 * ./aa/bb
1380 * ./bb
1381 * ./bb/x.c
1382 * and make says:
1383 * making all in aa
1384 * making all in bb
1385 * x.c:9: Error
1386 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1387 * qf_guess_filepath will return NULL.
1388 */
1389 static char_u *
1390qf_guess_filepath(filename)
1391 char_u *filename;
1392{
1393 struct dir_stack_T *ds_ptr;
1394 struct dir_stack_T *ds_tmp;
1395 char_u *fullname;
1396
1397 /* no dirs on the stack - there's nothing we can do */
1398 if (dir_stack == NULL)
1399 return NULL;
1400
1401 ds_ptr = dir_stack->next;
1402 fullname = NULL;
1403 while (ds_ptr)
1404 {
1405 vim_free(fullname);
1406 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1407
1408 /* If concat_fnames failed, just go on. The worst thing that can happen
1409 * is that we delete the entire stack.
1410 */
1411 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1412 break;
1413
1414 ds_ptr = ds_ptr->next;
1415 }
1416
1417 vim_free(fullname);
1418
1419 /* clean up all dirs we already left */
1420 while (dir_stack->next != ds_ptr)
1421 {
1422 ds_tmp = dir_stack->next;
1423 dir_stack->next = dir_stack->next->next;
1424 vim_free(ds_tmp->dirname);
1425 vim_free(ds_tmp);
1426 }
1427
1428 return ds_ptr==NULL? NULL: ds_ptr->dirname;
1429
1430}
1431
1432/*
1433 * jump to a quickfix line
1434 * if dir == FORWARD go "errornr" valid entries forward
1435 * if dir == BACKWARD go "errornr" valid entries backward
1436 * if dir == FORWARD_FILE go "errornr" valid entries files backward
1437 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
1438 * else if "errornr" is zero, redisplay the same line
1439 * else go to entry "errornr"
1440 */
1441 void
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001442qf_jump(qi, dir, errornr, forceit)
1443 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 int dir;
1445 int errornr;
1446 int forceit;
1447{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001448 qf_info_T *ll_ref;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001449 qfline_T *qf_ptr;
1450 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 int qf_index;
1452 int old_qf_fnum;
1453 int old_qf_index;
1454 int prev_index;
1455 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
1456 char_u *err = e_no_more_items;
1457 linenr_T i;
1458 buf_T *old_curbuf;
1459 linenr_T old_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 colnr_T screen_col;
1461 colnr_T char_col;
1462 char_u *line;
1463#ifdef FEAT_WINDOWS
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001464 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001465 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 int opened_window = FALSE;
1467 win_T *win;
1468 win_T *altwin;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001469 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001471 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 int print_message = TRUE;
1473 int len;
1474#ifdef FEAT_FOLDING
1475 int old_KeyTyped = KeyTyped; /* getting file may reset it */
1476#endif
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001477 int ok = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001478 int usable_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001480 if (qi == NULL)
1481 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001482
1483 if (qi->qf_curlist >= qi->qf_listcount
1484 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 {
1486 EMSG(_(e_quickfix));
1487 return;
1488 }
1489
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001490 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001492 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 old_qf_index = qf_index;
1494 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
1495 {
1496 while (errornr--)
1497 {
1498 old_qf_ptr = qf_ptr;
1499 prev_index = qf_index;
1500 old_qf_fnum = qf_ptr->qf_fnum;
1501 do
1502 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001503 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 || qf_ptr->qf_next == NULL)
1505 {
1506 qf_ptr = old_qf_ptr;
1507 qf_index = prev_index;
1508 if (err != NULL)
1509 {
1510 EMSG(_(err));
1511 goto theend;
1512 }
1513 errornr = 0;
1514 break;
1515 }
1516 ++qf_index;
1517 qf_ptr = qf_ptr->qf_next;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001518 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1519 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1521 err = NULL;
1522 }
1523 }
1524 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
1525 {
1526 while (errornr--)
1527 {
1528 old_qf_ptr = qf_ptr;
1529 prev_index = qf_index;
1530 old_qf_fnum = qf_ptr->qf_fnum;
1531 do
1532 {
1533 if (qf_index == 1 || qf_ptr->qf_prev == NULL)
1534 {
1535 qf_ptr = old_qf_ptr;
1536 qf_index = prev_index;
1537 if (err != NULL)
1538 {
1539 EMSG(_(err));
1540 goto theend;
1541 }
1542 errornr = 0;
1543 break;
1544 }
1545 --qf_index;
1546 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001547 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1548 && !qf_ptr->qf_valid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1550 err = NULL;
1551 }
1552 }
1553 else if (errornr != 0) /* go to specified number */
1554 {
1555 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
1556 {
1557 --qf_index;
1558 qf_ptr = qf_ptr->qf_prev;
1559 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001560 while (errornr > qf_index && qf_index <
1561 qi->qf_lists[qi->qf_curlist].qf_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 && qf_ptr->qf_next != NULL)
1563 {
1564 ++qf_index;
1565 qf_ptr = qf_ptr->qf_next;
1566 }
1567 }
1568
1569#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001570 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
1571 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 /* No need to print the error message if it's visible in the error
1573 * window */
1574 print_message = FALSE;
1575
1576 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001577 * For ":helpgrep" find a help window or open one.
1578 */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001579 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001580 {
1581 win_T *wp;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001582
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001583 if (cmdmod.tab != 0)
1584 wp = NULL;
1585 else
1586 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1587 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
1588 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001589 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
1590 win_enter(wp, TRUE);
1591 else
1592 {
1593 /*
1594 * Split off help window; put it at far top if no position
1595 * specified, the current window is vertically split and narrow.
1596 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001597 flags = WSP_HELP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001598# ifdef FEAT_VERTSPLIT
1599 if (cmdmod.split == 0 && curwin->w_width != Columns
1600 && curwin->w_width < 80)
Bram Moolenaar884ae642009-02-22 01:37:59 +00001601 flags |= WSP_TOP;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001602# endif
Bram Moolenaar884ae642009-02-22 01:37:59 +00001603 if (qi != &ql_info)
1604 flags |= WSP_NEWLOC; /* don't copy the location list */
1605
1606 if (win_split(0, flags) == FAIL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001607 goto theend;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001608 opened_window = TRUE; /* close it when fail */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001609
1610 if (curwin->w_height < p_hh)
1611 win_setheight((int)p_hh);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001612
1613 if (qi != &ql_info) /* not a quickfix list */
1614 {
1615 /* The new window should use the supplied location list */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001616 curwin->w_llist = qi;
1617 qi->qf_refcount++;
1618 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001619 }
1620
1621 if (!p_im)
1622 restart_edit = 0; /* don't want insert mode in help file */
1623 }
1624
1625 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 * If currently in the quickfix window, find another window to show the
1627 * file in.
1628 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00001629 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 {
Bram Moolenaar24862852013-06-30 13:57:45 +02001631 win_T *usable_win_ptr = NULL;
1632
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 /*
1634 * If there is no file specified, we don't know where to go.
1635 * But do advance, otherwise ":cn" gets stuck.
1636 */
1637 if (qf_ptr->qf_fnum == 0)
1638 goto theend;
1639
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001640 usable_win = 0;
Bram Moolenaar24862852013-06-30 13:57:45 +02001641
1642 ll_ref = curwin->w_llist_ref;
1643 if (ll_ref != NULL)
1644 {
1645 /* Find a window using the same location list that is not a
1646 * quickfix window. */
1647 FOR_ALL_WINDOWS(usable_win_ptr)
1648 if (usable_win_ptr->w_llist == ll_ref
1649 && usable_win_ptr->w_buffer->b_p_bt[0] != 'q')
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02001650 {
1651 usable_win = 1;
Bram Moolenaar24862852013-06-30 13:57:45 +02001652 break;
Bram Moolenaarf5901aa2013-07-01 21:25:25 +02001653 }
Bram Moolenaar24862852013-06-30 13:57:45 +02001654 }
1655
1656 if (!usable_win)
1657 {
1658 /* Locate a window showing a normal buffer */
1659 FOR_ALL_WINDOWS(win)
1660 if (win->w_buffer->b_p_bt[0] == NUL)
1661 {
1662 usable_win = 1;
1663 break;
1664 }
1665 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001666
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 /*
Bram Moolenaar446cb832008-06-24 21:56:24 +00001668 * If no usable window is found and 'switchbuf' contains "usetab"
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001669 * then search in other tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001671 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001672 {
1673 tabpage_T *tp;
1674 win_T *wp;
1675
1676 FOR_ALL_TAB_WINDOWS(tp, wp)
1677 {
1678 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
1679 {
1680 goto_tabpage_win(tp, wp);
1681 usable_win = 1;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001682 goto win_found;
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001683 }
1684 }
1685 }
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00001686win_found:
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001687
1688 /*
Bram Moolenaar1042fa32007-09-16 11:27:42 +00001689 * If there is only one window and it is the quickfix window, create a
1690 * new one above the quickfix window.
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001691 */
1692 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 {
Bram Moolenaar884ae642009-02-22 01:37:59 +00001694 flags = WSP_ABOVE;
1695 if (ll_ref != NULL)
1696 flags |= WSP_NEWLOC;
1697 if (win_split(0, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 goto failed; /* not enough room for window */
1699 opened_window = TRUE; /* close it when fail */
1700 p_swb = empty_option; /* don't split again */
Bram Moolenaar446cb832008-06-24 21:56:24 +00001701 swb_flags = 0;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001702 RESET_BINDING(curwin);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001703 if (ll_ref != NULL)
1704 {
1705 /* The new window should use the location list from the
1706 * location list window */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001707 curwin->w_llist = ll_ref;
1708 ll_ref->qf_refcount++;
1709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 }
1711 else
1712 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001713 if (curwin->w_llist_ref != NULL)
1714 {
1715 /* In a location window */
Bram Moolenaar24862852013-06-30 13:57:45 +02001716 win = usable_win_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001717 if (win == NULL)
1718 {
1719 /* Find the window showing the selected file */
1720 FOR_ALL_WINDOWS(win)
1721 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1722 break;
1723 if (win == NULL)
1724 {
1725 /* Find a previous usable window */
1726 win = curwin;
1727 do
1728 {
1729 if (win->w_buffer->b_p_bt[0] == NUL)
1730 break;
1731 if (win->w_prev == NULL)
1732 win = lastwin; /* wrap around the top */
1733 else
1734 win = win->w_prev; /* go to previous window */
1735 } while (win != curwin);
1736 }
1737 }
1738 win_goto(win);
1739
1740 /* If the location list for the window is not set, then set it
1741 * to the location list from the location window */
1742 if (win->w_llist == NULL)
1743 {
1744 win->w_llist = ll_ref;
1745 ll_ref->qf_refcount++;
1746 }
1747 }
1748 else
1749 {
1750
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 /*
1752 * Try to find a window that shows the right buffer.
1753 * Default to the window just above the quickfix buffer.
1754 */
1755 win = curwin;
1756 altwin = NULL;
1757 for (;;)
1758 {
1759 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
1760 break;
1761 if (win->w_prev == NULL)
1762 win = lastwin; /* wrap around the top */
1763 else
1764 win = win->w_prev; /* go to previous window */
1765
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001766 if (IS_QF_WINDOW(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 {
1768 /* Didn't find it, go to the window before the quickfix
1769 * window. */
1770 if (altwin != NULL)
1771 win = altwin;
1772 else if (curwin->w_prev != NULL)
1773 win = curwin->w_prev;
1774 else
1775 win = curwin->w_next;
1776 break;
1777 }
1778
1779 /* Remember a usable window. */
1780 if (altwin == NULL && !win->w_p_pvw
1781 && win->w_buffer->b_p_bt[0] == NUL)
1782 altwin = win;
1783 }
1784
1785 win_goto(win);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 }
1788 }
1789#endif
1790
1791 /*
1792 * If there is a file name,
1793 * read the wanted file if needed, and check autowrite etc.
1794 */
1795 old_curbuf = curbuf;
1796 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001797
1798 if (qf_ptr->qf_fnum != 0)
1799 {
1800 if (qf_ptr->qf_type == 1)
1801 {
1802 /* Open help file (do_ecmd() will set b_help flag, readfile() will
1803 * set b_p_ro flag). */
1804 if (!can_abandon(curbuf, forceit))
1805 {
1806 EMSG(_(e_nowrtmsg));
1807 ok = FALSE;
1808 }
1809 else
1810 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001811 ECMD_HIDE + ECMD_SET_HELP,
1812 oldwin == curwin ? curwin : NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001813 }
1814 else
1815 ok = buflist_getfile(qf_ptr->qf_fnum,
1816 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
1817 }
1818
1819 if (ok == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 {
1821 /* When not switched to another buffer, still need to set pc mark */
1822 if (curbuf == old_curbuf)
1823 setpcmark();
1824
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001825 if (qf_ptr->qf_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001827 /*
1828 * Go to line with error, unless qf_lnum is 0.
1829 */
1830 i = qf_ptr->qf_lnum;
1831 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001833 if (i > curbuf->b_ml.ml_line_count)
1834 i = curbuf->b_ml.ml_line_count;
1835 curwin->w_cursor.lnum = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001837 if (qf_ptr->qf_col > 0)
1838 {
1839 curwin->w_cursor.col = qf_ptr->qf_col - 1;
1840 if (qf_ptr->qf_viscol == TRUE)
1841 {
1842 /*
1843 * Check each character from the beginning of the error
1844 * line up to the error column. For each tab character
1845 * found, reduce the error column value by the length of
1846 * a tab character.
1847 */
1848 line = ml_get_curline();
1849 screen_col = 0;
1850 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
1851 {
1852 if (*line == NUL)
1853 break;
1854 if (*line++ == '\t')
1855 {
1856 curwin->w_cursor.col -= 7 - (screen_col % 8);
1857 screen_col += 8 - (screen_col % 8);
1858 }
1859 else
1860 ++screen_col;
1861 }
1862 }
1863 check_cursor();
1864 }
1865 else
1866 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 }
1868 else
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001869 {
1870 pos_T save_cursor;
1871
1872 /* Move the cursor to the first line in the buffer */
1873 save_cursor = curwin->w_cursor;
1874 curwin->w_cursor.lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001875 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
1876 SEARCH_KEEP, NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001877 curwin->w_cursor = save_cursor;
1878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879
1880#ifdef FEAT_FOLDING
1881 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
1882 foldOpenCursor();
1883#endif
1884 if (print_message)
1885 {
Bram Moolenaar8f55d102012-01-20 13:28:34 +01001886 /* Update the screen before showing the message, unless the screen
1887 * scrolled up. */
1888 if (!msg_scrolled)
1889 update_topline_redraw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001891 qi->qf_lists[qi->qf_curlist].qf_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
1893 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
1894 /* Add the message, skipping leading whitespace and newlines. */
1895 len = (int)STRLEN(IObuff);
1896 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
1897
1898 /* Output the message. Overwrite to avoid scrolling when the 'O'
1899 * flag is present in 'shortmess'; But when not jumping, print the
1900 * whole message. */
1901 i = msg_scroll;
1902 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
1903 msg_scroll = TRUE;
1904 else if (!msg_scrolled && shortmess(SHM_OVERALL))
1905 msg_scroll = FALSE;
1906 msg_attr_keep(IObuff, 0, TRUE);
1907 msg_scroll = i;
1908 }
1909 }
1910 else
1911 {
1912#ifdef FEAT_WINDOWS
1913 if (opened_window)
1914 win_close(curwin, TRUE); /* Close opened window */
1915#endif
1916 if (qf_ptr->qf_fnum != 0)
1917 {
1918 /*
1919 * Couldn't open file, so put index back where it was. This could
1920 * happen if the file was readonly and we changed something.
1921 */
1922#ifdef FEAT_WINDOWS
1923failed:
1924#endif
1925 qf_ptr = old_qf_ptr;
1926 qf_index = old_qf_index;
1927 }
1928 }
1929theend:
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001930 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
1931 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932#ifdef FEAT_WINDOWS
1933 if (p_swb != old_swb && opened_window)
1934 {
1935 /* Restore old 'switchbuf' value, but not when an autocommand or
1936 * modeline has changed the value. */
1937 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00001938 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00001940 swb_flags = old_swb_flags;
1941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 else
1943 free_string_option(old_swb);
1944 }
1945#endif
1946}
1947
1948/*
1949 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001950 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 */
1952 void
1953qf_list(eap)
1954 exarg_T *eap;
1955{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001956 buf_T *buf;
1957 char_u *fname;
1958 qfline_T *qfp;
1959 int i;
1960 int idx1 = 1;
1961 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001962 char_u *arg = eap->arg;
1963 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001965 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001967 if (eap->cmdidx == CMD_llist)
1968 {
1969 qi = GET_LOC_LIST(curwin);
1970 if (qi == NULL)
1971 {
1972 EMSG(_(e_loclist));
1973 return;
1974 }
1975 }
1976
1977 if (qi->qf_curlist >= qi->qf_listcount
1978 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 {
1980 EMSG(_(e_quickfix));
1981 return;
1982 }
1983 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
1984 {
1985 EMSG(_(e_trailing));
1986 return;
1987 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001988 i = qi->qf_lists[qi->qf_curlist].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 if (idx1 < 0)
1990 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
1991 if (idx2 < 0)
1992 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
1993
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001994 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001996 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
1997 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 {
1999 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2000 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002001 msg_putchar('\n');
2002 if (got_int)
2003 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002004
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002005 fname = NULL;
2006 if (qfp->qf_fnum != 0
2007 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2008 {
2009 fname = buf->b_fname;
2010 if (qfp->qf_type == 1) /* :helpgrep */
2011 fname = gettail(fname);
2012 }
2013 if (fname == NULL)
2014 sprintf((char *)IObuff, "%2d", i);
2015 else
2016 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2017 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002018 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002019 ? hl_attr(HLF_L) : hl_attr(HLF_D));
2020 if (qfp->qf_lnum == 0)
2021 IObuff[0] = NUL;
2022 else if (qfp->qf_col == 0)
2023 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
2024 else
2025 sprintf((char *)IObuff, ":%ld col %d",
2026 qfp->qf_lnum, qfp->qf_col);
2027 sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
2028 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2029 msg_puts_attr(IObuff, hl_attr(HLF_N));
2030 if (qfp->qf_pattern != NULL)
2031 {
2032 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
2033 STRCAT(IObuff, ":");
2034 msg_puts(IObuff);
2035 }
2036 msg_puts((char_u *)" ");
2037
2038 /* Remove newlines and leading whitespace from the text. For an
2039 * unrecognized line keep the indent, the compiler may mark a word
2040 * with ^^^^. */
2041 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2043 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002044 msg_prt_line(IObuff, FALSE);
2045 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002047
2048 qfp = qfp->qf_next;
2049 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 ui_breakcheck();
2051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052}
2053
2054/*
2055 * Remove newlines and leading whitespace from an error message.
2056 * Put the result in "buf[bufsize]".
2057 */
2058 static void
2059qf_fmt_text(text, buf, bufsize)
2060 char_u *text;
2061 char_u *buf;
2062 int bufsize;
2063{
2064 int i;
2065 char_u *p = text;
2066
2067 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2068 {
2069 if (*p == '\n')
2070 {
2071 buf[i] = ' ';
2072 while (*++p != NUL)
2073 if (!vim_iswhite(*p) && *p != '\n')
2074 break;
2075 }
2076 else
2077 buf[i] = *p++;
2078 }
2079 buf[i] = NUL;
2080}
2081
2082/*
2083 * ":colder [count]": Up in the quickfix stack.
2084 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002085 * ":lolder [count]": Up in the location list stack.
2086 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 */
2088 void
2089qf_age(eap)
2090 exarg_T *eap;
2091{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002092 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 int count;
2094
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002095 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2096 {
2097 qi = GET_LOC_LIST(curwin);
2098 if (qi == NULL)
2099 {
2100 EMSG(_(e_loclist));
2101 return;
2102 }
2103 }
2104
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 if (eap->addr_count != 0)
2106 count = eap->line2;
2107 else
2108 count = 1;
2109 while (count--)
2110 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002111 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002113 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 {
2115 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002116 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002118 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 }
2120 else
2121 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002122 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 {
2124 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002125 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002127 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 }
2129 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002130 qf_msg(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131}
2132
2133 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002134qf_msg(qi)
2135 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136{
2137 smsg((char_u *)_("error list %d of %d; %d errors"),
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002138 qi->qf_curlist + 1, qi->qf_listcount,
2139 qi->qf_lists[qi->qf_curlist].qf_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002141 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142#endif
2143}
2144
2145/*
Bram Moolenaar77197e42005-12-08 22:00:22 +00002146 * Free error list "idx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 */
2148 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002149qf_free(qi, idx)
2150 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 int idx;
2152{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002153 qfline_T *qfp;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002154 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002156 while (qi->qf_lists[idx].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002158 qfp = qi->qf_lists[idx].qf_start->qf_next;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002159 if (qi->qf_lists[idx].qf_title != NULL && !stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002160 {
2161 vim_free(qi->qf_lists[idx].qf_start->qf_text);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002162 stop = (qi->qf_lists[idx].qf_start == qfp);
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002163 vim_free(qi->qf_lists[idx].qf_start->qf_pattern);
2164 vim_free(qi->qf_lists[idx].qf_start);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002165 if (stop)
2166 /* Somehow qf_count may have an incorrect value, set it to 1
2167 * to avoid crashing when it's wrong.
2168 * TODO: Avoid qf_count being incorrect. */
2169 qi->qf_lists[idx].qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002170 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002171 qi->qf_lists[idx].qf_start = qfp;
2172 --qi->qf_lists[idx].qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 }
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002174 vim_free(qi->qf_lists[idx].qf_title);
Bram Moolenaar832f80e2010-08-17 20:26:59 +02002175 qi->qf_lists[idx].qf_title = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176}
2177
2178/*
2179 * qf_mark_adjust: adjust marks
2180 */
2181 void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002182qf_mark_adjust(wp, line1, line2, amount, amount_after)
2183 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 linenr_T line1;
2185 linenr_T line2;
2186 long amount;
2187 long amount_after;
2188{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002189 int i;
2190 qfline_T *qfp;
2191 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002192 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002194 if (wp != NULL)
2195 {
2196 if (wp->w_llist == NULL)
2197 return;
2198 qi = wp->w_llist;
2199 }
2200
2201 for (idx = 0; idx < qi->qf_listcount; ++idx)
2202 if (qi->qf_lists[idx].qf_count)
2203 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
2204 i < qi->qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 if (qfp->qf_fnum == curbuf->b_fnum)
2206 {
2207 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
2208 {
2209 if (amount == MAXLNUM)
2210 qfp->qf_cleared = TRUE;
2211 else
2212 qfp->qf_lnum += amount;
2213 }
2214 else if (amount_after && qfp->qf_lnum > line2)
2215 qfp->qf_lnum += amount_after;
2216 }
2217}
2218
2219/*
2220 * Make a nice message out of the error character and the error number:
2221 * char number message
2222 * e or E 0 " error"
2223 * w or W 0 " warning"
2224 * i or I 0 " info"
2225 * 0 0 ""
2226 * other 0 " c"
2227 * e or E n " error n"
2228 * w or W n " warning n"
2229 * i or I n " info n"
2230 * 0 n " error n"
2231 * other n " c n"
2232 * 1 x "" :helpgrep
2233 */
2234 static char_u *
2235qf_types(c, nr)
2236 int c, nr;
2237{
2238 static char_u buf[20];
2239 static char_u cc[3];
2240 char_u *p;
2241
2242 if (c == 'W' || c == 'w')
2243 p = (char_u *)" warning";
2244 else if (c == 'I' || c == 'i')
2245 p = (char_u *)" info";
2246 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
2247 p = (char_u *)" error";
2248 else if (c == 0 || c == 1)
2249 p = (char_u *)"";
2250 else
2251 {
2252 cc[0] = ' ';
2253 cc[1] = c;
2254 cc[2] = NUL;
2255 p = cc;
2256 }
2257
2258 if (nr <= 0)
2259 return p;
2260
2261 sprintf((char *)buf, "%s %3d", (char *)p, nr);
2262 return buf;
2263}
2264
2265#if defined(FEAT_WINDOWS) || defined(PROTO)
2266/*
2267 * ":cwindow": open the quickfix window if we have errors to display,
2268 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002269 * ":lwindow": open the location list window if we have locations to display,
2270 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 */
2272 void
2273ex_cwindow(eap)
2274 exarg_T *eap;
2275{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002276 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 win_T *win;
2278
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002279 if (eap->cmdidx == CMD_lwindow)
2280 {
2281 qi = GET_LOC_LIST(curwin);
2282 if (qi == NULL)
2283 return;
2284 }
2285
2286 /* Look for an existing quickfix window. */
2287 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288
2289 /*
2290 * If a quickfix window is open but we have no errors to display,
2291 * close the window. If a quickfix window is not open, then open
2292 * it if we have errors; otherwise, leave it closed.
2293 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002294 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02002295 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00002296 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 {
2298 if (win != NULL)
2299 ex_cclose(eap);
2300 }
2301 else if (win == NULL)
2302 ex_copen(eap);
2303}
2304
2305/*
2306 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002307 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 void
2310ex_cclose(eap)
2311 exarg_T *eap;
2312{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002313 win_T *win = NULL;
2314 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002316 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
2317 {
2318 qi = GET_LOC_LIST(curwin);
2319 if (qi == NULL)
2320 return;
2321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002323 /* Find existing quickfix window and close it. */
2324 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 if (win != NULL)
2326 win_close(win, FALSE);
2327}
2328
2329/*
2330 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002331 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 */
2333 void
2334ex_copen(eap)
2335 exarg_T *eap;
2336{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002337 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002340 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00002341 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002342 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002344 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
2345 {
2346 qi = GET_LOC_LIST(curwin);
2347 if (qi == NULL)
2348 {
2349 EMSG(_(e_loclist));
2350 return;
2351 }
2352 }
2353
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 if (eap->addr_count != 0)
2355 height = eap->line2;
2356 else
2357 height = QF_WINHEIGHT;
2358
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360#ifdef FEAT_GUI
2361 need_mouse_correct = TRUE;
2362#endif
2363
2364 /*
2365 * Find existing quickfix window, or open a new one.
2366 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002367 win = qf_find_win(qi);
2368
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002369 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01002370 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01002372 if (eap->addr_count != 0)
2373 {
2374#ifdef FEAT_VERTSPLIT
2375 if (cmdmod.split & WSP_VERT)
2376 {
2377 if (height != W_WIDTH(win))
2378 win_setwidth(height);
2379 }
2380 else
2381#endif
2382 if (height != win->w_height)
2383 win_setheight(height);
2384 }
2385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 else
2387 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00002388 qf_buf = qf_find_buf(qi);
2389
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 /* The current window becomes the previous window afterwards. */
2391 win = curwin;
2392
Bram Moolenaar77642c02012-11-20 17:55:10 +01002393 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
2394 && cmdmod.split == 0)
2395 /* Create the new window at the very bottom, except when
2396 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002397 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00002398 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002400 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002402 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002404 /*
2405 * For the location list window, create a reference to the
2406 * location list from the window 'win'.
2407 */
2408 curwin->w_llist_ref = win->w_llist;
2409 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002411
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002412 if (oldwin != curwin)
2413 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00002414 if (qf_buf != NULL)
2415 /* Use the existing quickfix buffer */
2416 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002417 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002418 else
2419 {
2420 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002421 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002422 /* switch off 'swapfile' */
2423 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
2424 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00002425 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00002426 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01002427 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002428#ifdef FEAT_DIFF
2429 curwin->w_p_diff = FALSE;
2430#endif
2431#ifdef FEAT_FOLDING
2432 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
2433 OPT_LOCAL);
2434#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00002435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002437 /* Only set the height when still in the same tab page and there is no
2438 * window to the side. */
2439 if (curtab == prevtab
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002440#ifdef FEAT_VERTSPLIT
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002441 && curwin->w_width == Columns
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002442#endif
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002443 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 win_setheight(height);
2445 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
2446 if (win_valid(win))
2447 prevwin = win;
2448 }
2449
2450 /*
2451 * Fill the buffer with the quickfix list.
2452 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002453 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002455 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
Bram Moolenaarfb604092014-07-23 15:55:00 +02002456 qf_set_title_var(qi);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002457
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002458 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 curwin->w_cursor.col = 0;
2460 check_cursor();
2461 update_topline(); /* scroll to show the line */
2462}
2463
2464/*
2465 * Return the number of the current entry (line number in the quickfix
2466 * window).
2467 */
2468 linenr_T
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002469qf_current_entry(wp)
2470 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002472 qf_info_T *qi = &ql_info;
2473
2474 if (IS_LL_WINDOW(wp))
2475 /* In the location list window, use the referenced location list */
2476 qi = wp->w_llist_ref;
2477
2478 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479}
2480
2481/*
2482 * Update the cursor position in the quickfix window to the current error.
2483 * Return TRUE if there is a quickfix window.
2484 */
2485 static int
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002486qf_win_pos_update(qi, old_qf_index)
2487 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 int old_qf_index; /* previous qf_index or zero */
2489{
2490 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002491 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492
2493 /*
2494 * Put the cursor on the current error in the quickfix window, so that
2495 * it's viewable.
2496 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002497 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 if (win != NULL
2499 && qf_index <= win->w_buffer->b_ml.ml_line_count
2500 && old_qf_index != qf_index)
2501 {
2502 win_T *old_curwin = curwin;
2503
2504 curwin = win;
2505 curbuf = win->w_buffer;
2506 if (qf_index > old_qf_index)
2507 {
2508 curwin->w_redraw_top = old_qf_index;
2509 curwin->w_redraw_bot = qf_index;
2510 }
2511 else
2512 {
2513 curwin->w_redraw_top = qf_index;
2514 curwin->w_redraw_bot = old_qf_index;
2515 }
2516 curwin->w_cursor.lnum = qf_index;
2517 curwin->w_cursor.col = 0;
2518 update_topline(); /* scroll to show the line */
2519 redraw_later(VALID);
2520 curwin->w_redr_status = TRUE; /* update ruler */
2521 curwin = old_curwin;
2522 curbuf = curwin->w_buffer;
2523 }
2524 return win != NULL;
2525}
2526
2527/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002528 * Check whether the given window is displaying the specified quickfix/location
2529 * list buffer
2530 */
2531 static int
2532is_qf_win(win, qi)
2533 win_T *win;
2534 qf_info_T *qi;
2535{
2536 /*
2537 * A window displaying the quickfix buffer will have the w_llist_ref field
2538 * set to NULL.
2539 * A window displaying a location list buffer will have the w_llist_ref
2540 * pointing to the location list.
2541 */
2542 if (bt_quickfix(win->w_buffer))
2543 if ((qi == &ql_info && win->w_llist_ref == NULL)
2544 || (qi != &ql_info && win->w_llist_ref == qi))
2545 return TRUE;
2546
2547 return FALSE;
2548}
2549
2550/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002551 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00002552 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002553 */
2554 static win_T *
2555qf_find_win(qi)
2556 qf_info_T *qi;
2557{
2558 win_T *win;
2559
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002560 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00002561 if (is_qf_win(win, qi))
2562 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002563
2564 return win;
2565}
2566
2567/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00002568 * Find a quickfix buffer.
2569 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 */
2571 static buf_T *
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002572qf_find_buf(qi)
2573 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574{
Bram Moolenaar9c102382006-05-03 21:26:49 +00002575 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002576 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577
Bram Moolenaar9c102382006-05-03 21:26:49 +00002578 FOR_ALL_TAB_WINDOWS(tp, win)
2579 if (is_qf_win(win, qi))
2580 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002581
Bram Moolenaar9c102382006-05-03 21:26:49 +00002582 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583}
2584
2585/*
2586 * Find the quickfix buffer. If it exists, update the contents.
2587 */
2588 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002589qf_update_buffer(qi)
2590 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591{
2592 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002593 win_T *win;
2594 win_T *curwin_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596
2597 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002598 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 if (buf != NULL)
2600 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 /* set curwin/curbuf to buf and save a few things */
2602 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002604 qf_fill_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002606 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL
2607 && (win = qf_find_win(qi)) != NULL)
2608 {
2609 curwin_save = curwin;
2610 curwin = win;
Bram Moolenaarfb604092014-07-23 15:55:00 +02002611 qf_set_title_var(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002612 curwin = curwin_save;
2613
2614 }
2615
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 /* restore curwin/curbuf and a few other things */
2617 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002619 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 }
2621}
2622
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002623 static void
Bram Moolenaarfb604092014-07-23 15:55:00 +02002624qf_set_title_var(qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02002625 qf_info_T *qi;
2626{
2627 set_internal_string_var((char_u *)"w:quickfix_title",
2628 qi->qf_lists[qi->qf_curlist].qf_title);
2629}
2630
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631/*
2632 * Fill current buffer with quickfix errors, replacing any previous contents.
2633 * curbuf must be the quickfix buffer!
2634 */
2635 static void
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002636qf_fill_buffer(qi)
2637 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002639 linenr_T lnum;
2640 qfline_T *qfp;
2641 buf_T *errbuf;
2642 int len;
2643 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644
2645 /* delete all existing lines */
2646 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2647 (void)ml_delete((linenr_T)1, FALSE);
2648
2649 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002650 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 {
2652 /* Add one line for each error */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002653 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2654 for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 {
2656 if (qfp->qf_fnum != 0
2657 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
2658 && errbuf->b_fname != NULL)
2659 {
2660 if (qfp->qf_type == 1) /* :helpgrep */
2661 STRCPY(IObuff, gettail(errbuf->b_fname));
2662 else
2663 STRCPY(IObuff, errbuf->b_fname);
2664 len = (int)STRLEN(IObuff);
2665 }
2666 else
2667 len = 0;
2668 IObuff[len++] = '|';
2669
2670 if (qfp->qf_lnum > 0)
2671 {
2672 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
2673 len += (int)STRLEN(IObuff + len);
2674
2675 if (qfp->qf_col > 0)
2676 {
2677 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
2678 len += (int)STRLEN(IObuff + len);
2679 }
2680
2681 sprintf((char *)IObuff + len, "%s",
2682 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
2683 len += (int)STRLEN(IObuff + len);
2684 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002685 else if (qfp->qf_pattern != NULL)
2686 {
2687 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
2688 len += (int)STRLEN(IObuff + len);
2689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 IObuff[len++] = '|';
2691 IObuff[len++] = ' ';
2692
2693 /* Remove newlines and leading whitespace from the text.
2694 * For an unrecognized line keep the indent, the compiler may
2695 * mark a word with ^^^^. */
2696 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2697 IObuff + len, IOSIZE - len);
2698
2699 if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
2700 == FAIL)
2701 break;
2702 qfp = qfp->qf_next;
2703 }
2704 /* Delete the empty line which is now at the end */
2705 (void)ml_delete(lnum + 1, FALSE);
2706 }
2707
2708 /* correct cursor position */
2709 check_lnums(TRUE);
2710
2711 /* Set the 'filetype' to "qf" each time after filling the buffer. This
2712 * resembles reading a file into a buffer, it's more logical when using
2713 * autocommands. */
2714 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
2715 curbuf->b_p_ma = FALSE;
2716
2717#ifdef FEAT_AUTOCMD
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002718 keep_filetype = TRUE; /* don't detect 'filetype' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
2720 FALSE, curbuf);
2721 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
2722 FALSE, curbuf);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00002723 keep_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724#endif
2725
2726 /* make sure it will be redrawn */
2727 redraw_curbuf_later(NOT_VALID);
2728
2729 /* Restore KeyTyped, setting 'filetype' may reset it. */
2730 KeyTyped = old_KeyTyped;
2731}
2732
2733#endif /* FEAT_WINDOWS */
2734
2735/*
2736 * Return TRUE if "buf" is the quickfix buffer.
2737 */
2738 int
2739bt_quickfix(buf)
2740 buf_T *buf;
2741{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002742 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743}
2744
2745/*
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002746 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
2747 * This means the buffer name is not a file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 */
2749 int
2750bt_nofile(buf)
2751 buf_T *buf;
2752{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002753 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
2754 || buf->b_p_bt[0] == 'a');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755}
2756
2757/*
2758 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
2759 */
2760 int
2761bt_dontwrite(buf)
2762 buf_T *buf;
2763{
Bram Moolenaarfc573802011-12-30 15:01:59 +01002764 return buf != NULL && buf->b_p_bt[0] == 'n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765}
2766
2767 int
2768bt_dontwrite_msg(buf)
2769 buf_T *buf;
2770{
2771 if (bt_dontwrite(buf))
2772 {
2773 EMSG(_("E382: Cannot write, 'buftype' option is set"));
2774 return TRUE;
2775 }
2776 return FALSE;
2777}
2778
2779/*
2780 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
2781 * and 'bufhidden'.
2782 */
2783 int
2784buf_hide(buf)
2785 buf_T *buf;
2786{
2787 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
2788 switch (buf->b_p_bh[0])
2789 {
2790 case 'u': /* "unload" */
2791 case 'w': /* "wipe" */
2792 case 'd': return FALSE; /* "delete" */
2793 case 'h': return TRUE; /* "hide" */
2794 }
2795 return (p_hid || cmdmod.hide);
2796}
2797
2798/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002799 * Return TRUE when using ":vimgrep" for ":grep".
2800 */
2801 int
Bram Moolenaar81695252004-12-29 20:58:21 +00002802grep_internal(cmdidx)
2803 cmdidx_T cmdidx;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002804{
Bram Moolenaar754b5602006-02-09 23:53:20 +00002805 return ((cmdidx == CMD_grep
2806 || cmdidx == CMD_lgrep
2807 || cmdidx == CMD_grepadd
2808 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002809 && STRCMP("internal",
2810 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
2811}
2812
2813/*
Bram Moolenaara6557602006-02-04 22:43:20 +00002814 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 */
2816 void
2817ex_make(eap)
2818 exarg_T *eap;
2819{
Bram Moolenaar7c626922005-02-07 22:01:03 +00002820 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 char_u *cmd;
2822 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00002823 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002824 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002825 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002826#ifdef FEAT_AUTOCMD
2827 char_u *au_name = NULL;
2828
Bram Moolenaard88e02d2011-04-28 17:27:09 +02002829 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
2830 if (grep_internal(eap->cmdidx))
2831 {
2832 ex_vimgrep(eap);
2833 return;
2834 }
2835
Bram Moolenaar7c626922005-02-07 22:01:03 +00002836 switch (eap->cmdidx)
2837 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00002838 case CMD_make: au_name = (char_u *)"make"; break;
2839 case CMD_lmake: au_name = (char_u *)"lmake"; break;
2840 case CMD_grep: au_name = (char_u *)"grep"; break;
2841 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
2842 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
2843 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002844 default: break;
2845 }
2846 if (au_name != NULL)
2847 {
2848 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
2849 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar1e015462005-09-25 22:16:38 +00002850# ifdef FEAT_EVAL
Bram Moolenaar7c626922005-02-07 22:01:03 +00002851 if (did_throw || force_abort)
2852 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00002853# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00002854 }
2855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856
Bram Moolenaara6557602006-02-04 22:43:20 +00002857 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
2858 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00002859 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00002860
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002862 fname = get_mef_name();
2863 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002865 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866
2867 /*
2868 * If 'shellpipe' empty: don't redirect to 'errorfile'.
2869 */
2870 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
2871 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002872 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 cmd = alloc(len);
2874 if (cmd == NULL)
2875 return;
2876 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
2877 (char *)p_shq);
2878 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002879 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 /*
2881 * Output a newline if there's something else than the :make command that
2882 * was typed (in which case the cursor is in column 0).
2883 */
2884 if (msg_col == 0)
2885 msg_didout = FALSE;
2886 msg_start();
2887 MSG_PUTS(":!");
2888 msg_outtrans(cmd); /* show what we are doing */
2889
2890 /* let the shell know if we are redirecting output or not */
2891 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
2892
2893#ifdef AMIGA
2894 out_flush();
2895 /* read window status report and redraw before message */
2896 (void)char_avail();
2897#endif
2898
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002899 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00002900 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
2901 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002902 && eap->cmdidx != CMD_lgrepadd),
2903 *eap->cmdlinep);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002904 if (wp != NULL)
2905 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002906#ifdef FEAT_AUTOCMD
2907 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002908 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002909 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
2910 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02002911 if (qi->qf_curlist < qi->qf_listcount)
2912 res = qi->qf_lists[qi->qf_curlist].qf_count;
2913 else
2914 res = 0;
2915 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002916#endif
2917 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002918 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919
Bram Moolenaar7c626922005-02-07 22:01:03 +00002920 mch_remove(fname);
2921 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 vim_free(cmd);
2923}
2924
2925/*
2926 * Return the name for the errorfile, in allocated memory.
2927 * Find a new unique name when 'makeef' contains "##".
2928 * Returns NULL for error.
2929 */
2930 static char_u *
2931get_mef_name()
2932{
2933 char_u *p;
2934 char_u *name;
2935 static int start = -1;
2936 static int off = 0;
2937#ifdef HAVE_LSTAT
2938 struct stat sb;
2939#endif
2940
2941 if (*p_mef == NUL)
2942 {
2943 name = vim_tempname('e');
2944 if (name == NULL)
2945 EMSG(_(e_notmp));
2946 return name;
2947 }
2948
2949 for (p = p_mef; *p; ++p)
2950 if (p[0] == '#' && p[1] == '#')
2951 break;
2952
2953 if (*p == NUL)
2954 return vim_strsave(p_mef);
2955
2956 /* Keep trying until the name doesn't exist yet. */
2957 for (;;)
2958 {
2959 if (start == -1)
2960 start = mch_get_pid();
2961 else
2962 off += 19;
2963
2964 name = alloc((unsigned)STRLEN(p_mef) + 30);
2965 if (name == NULL)
2966 break;
2967 STRCPY(name, p_mef);
2968 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
2969 STRCAT(name, p + 2);
2970 if (mch_getperm(name) < 0
2971#ifdef HAVE_LSTAT
2972 /* Don't accept a symbolic link, its a security risk. */
2973 && mch_lstat((char *)name, &sb) < 0
2974#endif
2975 )
2976 break;
2977 vim_free(name);
2978 }
2979 return name;
2980}
2981
2982/*
2983 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002984 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 */
2986 void
2987ex_cc(eap)
2988 exarg_T *eap;
2989{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002990 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002991
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002992 if (eap->cmdidx == CMD_ll
2993 || eap->cmdidx == CMD_lrewind
2994 || eap->cmdidx == CMD_lfirst
2995 || eap->cmdidx == CMD_llast)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002996 {
2997 qi = GET_LOC_LIST(curwin);
2998 if (qi == NULL)
2999 {
3000 EMSG(_(e_loclist));
3001 return;
3002 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003003 }
3004
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003005 qf_jump(qi, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 eap->addr_count > 0
3007 ? (int)eap->line2
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003008 : (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 ? 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003010 : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3011 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 ? 1
3013 : 32767,
3014 eap->forceit);
3015}
3016
3017/*
3018 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003019 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 */
3021 void
3022ex_cnext(eap)
3023 exarg_T *eap;
3024{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003025 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003026
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003027 if (eap->cmdidx == CMD_lnext
3028 || eap->cmdidx == CMD_lNext
3029 || eap->cmdidx == CMD_lprevious
3030 || eap->cmdidx == CMD_lnfile
3031 || eap->cmdidx == CMD_lNfile
3032 || eap->cmdidx == CMD_lpfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003033 {
3034 qi = GET_LOC_LIST(curwin);
3035 if (qi == NULL)
3036 {
3037 EMSG(_(e_loclist));
3038 return;
3039 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003040 }
3041
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003042 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 ? FORWARD
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003044 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003046 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
3047 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 ? BACKWARD_FILE
3049 : BACKWARD,
3050 eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit);
3051}
3052
3053/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003054 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003055 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 */
3057 void
3058ex_cfile(eap)
3059 exarg_T *eap;
3060{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003061 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003062 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003063#ifdef FEAT_AUTOCMD
3064 char_u *au_name = NULL;
3065#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003066
3067 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003068 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003069 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003070
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003071#ifdef FEAT_AUTOCMD
3072 switch (eap->cmdidx)
3073 {
3074 case CMD_cfile: au_name = (char_u *)"cfile"; break;
3075 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
3076 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
3077 case CMD_lfile: au_name = (char_u *)"lfile"; break;
3078 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
3079 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
3080 default: break;
3081 }
3082 if (au_name != NULL)
3083 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
3084#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02003085#ifdef FEAT_BROWSE
3086 if (cmdmod.browse)
3087 {
3088 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
3089 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
3090 if (browse_file == NULL)
3091 return;
3092 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
3093 vim_free(browse_file);
3094 }
3095 else
3096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003098 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003099
3100 /*
3101 * This function is used by the :cfile, :cgetfile and :caddfile
3102 * commands.
3103 * :cfile always creates a new quickfix list and jumps to the
3104 * first error.
3105 * :cgetfile creates a new quickfix list but doesn't jump to the
3106 * first error.
3107 * :caddfile adds to an existing quickfix list. If there is no
3108 * quickfix list then a new list is created.
3109 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003110 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003111 && eap->cmdidx != CMD_laddfile),
3112 *eap->cmdlinep) > 0
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003113 && (eap->cmdidx == CMD_cfile
3114 || eap->cmdidx == CMD_lfile))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003115 {
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003116#ifdef FEAT_AUTOCMD
3117 if (au_name != NULL)
3118 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3119#endif
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003120 if (wp != NULL)
3121 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003122 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003123 }
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01003124
3125 else
3126 {
3127#ifdef FEAT_AUTOCMD
3128 if (au_name != NULL)
3129 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
3130#endif
3131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132}
3133
3134/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003135 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00003136 * ":vimgrepadd {pattern} file(s)"
3137 * ":lvimgrep {pattern} file(s)"
3138 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00003139 */
3140 void
3141ex_vimgrep(eap)
3142 exarg_T *eap;
3143{
Bram Moolenaar81695252004-12-29 20:58:21 +00003144 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003145 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003146 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003147 char_u *fname;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003148 char_u *s;
3149 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003150 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00003151 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003152#ifdef FEAT_AUTOCMD
3153 qfline_T *cur_qf_start;
3154#endif
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003155 qfline_T *prevp = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003156 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00003157 buf_T *buf;
3158 int duplicate_name = FALSE;
3159 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003160 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003161 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003162 buf_T *first_match_buf = NULL;
3163 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003164 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003165#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3166 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003167#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003168 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003169 int flags = 0;
3170 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003171 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003172 char_u *dirname_start = NULL;
3173 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003174 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00003175#ifdef FEAT_AUTOCMD
3176 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003177
3178 switch (eap->cmdidx)
3179 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003180 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
3181 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
3182 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00003183 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003184 case CMD_grep: au_name = (char_u *)"grep"; break;
3185 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3186 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3187 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003188 default: break;
3189 }
3190 if (au_name != NULL)
3191 {
3192 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3193 curbuf->b_fname, TRUE, curbuf);
3194 if (did_throw || force_abort)
3195 return;
3196 }
3197#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003198
Bram Moolenaar754b5602006-02-09 23:53:20 +00003199 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003200 || eap->cmdidx == CMD_lvimgrep
3201 || eap->cmdidx == CMD_lgrepadd
3202 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003203 {
3204 qi = ll_get_or_alloc_list(curwin);
3205 if (qi == NULL)
3206 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00003207 }
3208
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003209 if (eap->addr_count > 0)
3210 tomatch = eap->line2;
3211 else
3212 tomatch = MAXLNUM;
3213
Bram Moolenaar81695252004-12-29 20:58:21 +00003214 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003215 regmatch.regprog = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003216 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003217 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003218 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00003219 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00003220 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00003221 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01003222
3223 if (s != NULL && *s == NUL)
3224 {
3225 /* Pattern is empty, use last search pattern. */
3226 if (last_search_pat() == NULL)
3227 {
3228 EMSG(_(e_noprevre));
3229 goto theend;
3230 }
3231 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
3232 }
3233 else
3234 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
3235
Bram Moolenaar86b68352004-12-27 21:59:20 +00003236 if (regmatch.regprog == NULL)
3237 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003238 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003239 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003240
3241 p = skipwhite(p);
3242 if (*p == NUL)
3243 {
3244 EMSG(_("E683: File name missing or invalid pattern"));
3245 goto theend;
3246 }
3247
Bram Moolenaar754b5602006-02-09 23:53:20 +00003248 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd &&
Bram Moolenaara6557602006-02-04 22:43:20 +00003249 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003250 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003251 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01003252 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003253 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003254 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003255 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003256 prevp->qf_next != prevp; prevp = prevp->qf_next)
3257 ;
3258
3259 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02003260 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003261 goto theend;
3262 if (fcount == 0)
3263 {
3264 EMSG(_(e_nomatch));
3265 goto theend;
3266 }
3267
Bram Moolenaard9462e32011-04-11 21:35:11 +02003268 dirname_start = alloc(MAXPATHL);
3269 dirname_now = alloc(MAXPATHL);
3270 if (dirname_start == NULL || dirname_now == NULL)
3271 goto theend;
3272
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003273 /* Remember the current directory, because a BufRead autocommand that does
3274 * ":lcd %:p:h" changes the meaning of short path names. */
3275 mch_dirname(dirname_start, MAXPATHL);
3276
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003277#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003278 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003279 * changing the current quickfix list. */
3280 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3281#endif
3282
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003283 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003284 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003285 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003286 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003287 if (time(NULL) > seconds)
3288 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003289 /* Display the file name every second or so, show the user we are
3290 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003291 seconds = time(NULL);
3292 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003293 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003294 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003295 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003296 else
3297 {
3298 msg_outtrans(p);
3299 vim_free(p);
3300 }
3301 msg_clr_eos();
3302 msg_didout = FALSE; /* overwrite this message */
3303 msg_nowait = TRUE; /* don't wait for this message */
3304 msg_col = 0;
3305 out_flush();
3306 }
3307
Bram Moolenaar81695252004-12-29 20:58:21 +00003308 buf = buflist_findname_exp(fnames[fi]);
3309 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
3310 {
3311 /* Remember that a buffer with this name already exists. */
3312 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003313 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003314 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003315
3316#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3317 /* Don't do Filetype autocommands to avoid loading syntax and
3318 * indent scripts, a great speed improvement. */
3319 save_ei = au_event_disable(",Filetype");
3320#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003321 /* Don't use modelines here, it's useless. */
3322 save_mls = p_mls;
3323 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00003324
3325 /* Load file into a buffer, so that 'fileencoding' is detected,
3326 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003327 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003328
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003329 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003330#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
3331 au_event_restore(save_ei);
3332#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003333 }
3334 else
3335 /* Use existing, loaded buffer. */
3336 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003337
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003338#ifdef FEAT_AUTOCMD
3339 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
3340 {
3341 int idx;
3342
3343 /* Autocommands changed the quickfix list. Find the one we were
3344 * using and restore it. */
3345 for (idx = 0; idx < LISTCOUNT; ++idx)
3346 if (cur_qf_start == qi->qf_lists[idx].qf_start)
3347 {
3348 qi->qf_curlist = idx;
3349 break;
3350 }
3351 if (idx == LISTCOUNT)
3352 {
3353 /* List cannot be found, create a new one. */
3354 qf_new_list(qi, *eap->cmdlinep);
3355 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3356 }
3357 }
3358#endif
3359
Bram Moolenaar81695252004-12-29 20:58:21 +00003360 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003361 {
3362 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003363 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003364 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003365 else
3366 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003367 /* Try for a match in all lines of the buffer.
3368 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003369 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003370 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
3371 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003372 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003373 col = 0;
3374 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003375 col, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003376 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003377 ;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003378 if (qf_add_entry(qi, &prevp,
Bram Moolenaar86b68352004-12-27 21:59:20 +00003379 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003380 fname,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003381 0,
Bram Moolenaar81695252004-12-29 20:58:21 +00003382 ml_get_buf(buf,
3383 regmatch.startpos[0].lnum + lnum, FALSE),
3384 regmatch.startpos[0].lnum + lnum,
3385 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00003386 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003387 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003388 0, /* nr */
3389 0, /* type */
3390 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003391 ) == FAIL)
3392 {
3393 got_int = TRUE;
3394 break;
3395 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003396 found_match = TRUE;
3397 if (--tomatch == 0)
3398 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003399 if ((flags & VGR_GLOBAL) == 0
3400 || regmatch.endpos[0].lnum > 0)
3401 break;
3402 col = regmatch.endpos[0].col
3403 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003404 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00003405 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003406 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003407 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00003408 if (got_int)
3409 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003410 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01003411#ifdef FEAT_AUTOCMD
3412 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
3413#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00003414
3415 if (using_dummy)
3416 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003417 if (found_match && first_match_buf == NULL)
3418 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003419 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003420 {
Bram Moolenaar81695252004-12-29 20:58:21 +00003421 /* Never keep a dummy buffer if there is another buffer
3422 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003423 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003424 buf = NULL;
3425 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00003426 else if (!cmdmod.hide
3427 || buf->b_p_bh[0] == 'u' /* "unload" */
3428 || buf->b_p_bh[0] == 'w' /* "wipe" */
3429 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00003430 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00003431 /* When no match was found we don't need to remember the
3432 * buffer, wipe it out. If there was a match and it
3433 * wasn't the first one or we won't jump there: only
3434 * unload the buffer.
3435 * Ignore 'hidden' here, because it may lead to having too
3436 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00003437 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003438 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003439 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003440 buf = NULL;
3441 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003442 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003443 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003444 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003445 buf = NULL;
3446 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003447 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003448
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003449 if (buf != NULL)
3450 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003451 /* If the buffer is still loaded we need to use the
3452 * directory we jumped to below. */
3453 if (buf == first_match_buf
3454 && target_dir == NULL
3455 && STRCMP(dirname_start, dirname_now) != 0)
3456 target_dir = vim_strsave(dirname_now);
3457
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003458 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003459 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00003460 * need to be done (again). But not the window-local
3461 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003462 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003463#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003464 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
3465 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00003466#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00003467 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003468 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003469 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003470 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003471 }
3472 }
3473
3474 FreeWild(fcount, fnames);
3475
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003476 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
3477 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3478 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003479
3480#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003481 qf_update_buffer(qi);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003482#endif
3483
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003484#ifdef FEAT_AUTOCMD
3485 if (au_name != NULL)
3486 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3487 curbuf->b_fname, TRUE, curbuf);
3488#endif
3489
Bram Moolenaar86b68352004-12-27 21:59:20 +00003490 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003491 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003492 {
3493 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003494 {
3495 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003496 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003497 if (buf != curbuf)
3498 /* If we jumped to another buffer redrawing will already be
3499 * taken care of. */
3500 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003501
3502 /* Jump to the directory used after loading the buffer. */
3503 if (curbuf == first_match_buf && target_dir != NULL)
3504 {
3505 exarg_T ea;
3506
3507 ea.arg = target_dir;
3508 ea.cmdidx = CMD_lcd;
3509 ex_cd(&ea);
3510 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003511 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003512 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003513 else
3514 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003515
Bram Moolenaar1042fa32007-09-16 11:27:42 +00003516 /* If we loaded a dummy buffer into the current window, the autocommands
3517 * may have messed up things, need to redraw and recompute folds. */
3518 if (redraw_for_dummy)
3519 {
3520#ifdef FEAT_FOLDING
3521 foldUpdateAll(curwin);
3522#else
3523 redraw_later(NOT_VALID);
3524#endif
3525 }
3526
Bram Moolenaar86b68352004-12-27 21:59:20 +00003527theend:
Bram Moolenaard9462e32011-04-11 21:35:11 +02003528 vim_free(dirname_now);
3529 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00003530 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02003531 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003532}
3533
3534/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00003535 * Skip over the pattern argument of ":vimgrep /pat/[g][j]".
Bram Moolenaar748bf032005-02-02 23:04:36 +00003536 * Put the start of the pattern in "*s", unless "s" is NULL.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003537 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
3538 * If "s" is not NULL terminate the pattern with a NUL.
3539 * Return a pointer to the char just past the pattern plus flags.
Bram Moolenaar748bf032005-02-02 23:04:36 +00003540 */
3541 char_u *
Bram Moolenaar05159a02005-02-26 23:04:13 +00003542skip_vimgrep_pat(p, s, flags)
3543 char_u *p;
3544 char_u **s;
3545 int *flags;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003546{
3547 int c;
3548
3549 if (vim_isIDc(*p))
3550 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003551 /* ":vimgrep pattern fname" */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003552 if (s != NULL)
3553 *s = p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003554 p = skiptowhite(p);
3555 if (s != NULL && *p != NUL)
3556 *p++ = NUL;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003557 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003558 else
3559 {
3560 /* ":vimgrep /pattern/[g][j] fname" */
3561 if (s != NULL)
3562 *s = p + 1;
3563 c = *p;
3564 p = skip_regexp(p + 1, c, TRUE, NULL);
3565 if (*p != c)
3566 return NULL;
3567
3568 /* Truncate the pattern. */
3569 if (s != NULL)
3570 *p = NUL;
3571 ++p;
3572
3573 /* Find the flags */
3574 while (*p == 'g' || *p == 'j')
3575 {
3576 if (flags != NULL)
3577 {
3578 if (*p == 'g')
3579 *flags |= VGR_GLOBAL;
3580 else
3581 *flags |= VGR_NOJUMP;
3582 }
3583 ++p;
3584 }
3585 }
Bram Moolenaar748bf032005-02-02 23:04:36 +00003586 return p;
3587}
3588
3589/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003590 * Restore current working directory to "dirname_start" if they differ, taking
3591 * into account whether it is set locally or globally.
3592 */
3593 static void
3594restore_start_dir(dirname_start)
3595 char_u *dirname_start;
3596{
3597 char_u *dirname_now = alloc(MAXPATHL);
3598
3599 if (NULL != dirname_now)
3600 {
3601 mch_dirname(dirname_now, MAXPATHL);
3602 if (STRCMP(dirname_start, dirname_now) != 0)
3603 {
3604 /* If the directory has changed, change it back by building up an
3605 * appropriate ex command and executing it. */
3606 exarg_T ea;
3607
3608 ea.arg = dirname_start;
3609 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
3610 ex_cd(&ea);
3611 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01003612 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003613 }
3614}
3615
3616/*
3617 * Load file "fname" into a dummy buffer and return the buffer pointer,
3618 * placing the directory resulting from the buffer load into the
3619 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
3620 * prior to calling this function. Restores directory to "dirname_start" prior
3621 * to returning, if autocmds or the 'autochdir' option have changed it.
3622 *
3623 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
3624 * or wipe_dummy_buffer() later!
3625 *
Bram Moolenaar81695252004-12-29 20:58:21 +00003626 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00003627 */
3628 static buf_T *
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003629load_dummy_buffer(fname, dirname_start, resulting_dir)
Bram Moolenaar81695252004-12-29 20:58:21 +00003630 char_u *fname;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003631 char_u *dirname_start; /* in: old directory */
3632 char_u *resulting_dir; /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00003633{
3634 buf_T *newbuf;
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003635 buf_T *newbuf_to_wipe = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00003636 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00003637 aco_save_T aco;
Bram Moolenaar81695252004-12-29 20:58:21 +00003638
3639 /* Allocate a buffer without putting it in the buffer list. */
3640 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
3641 if (newbuf == NULL)
3642 return NULL;
3643
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00003644 /* Init the options. */
3645 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
3646
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003647 /* need to open the memfile before putting the buffer in a window */
3648 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00003649 {
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003650 /* set curwin/curbuf to buf and save a few things */
3651 aucmd_prepbuf(&aco, newbuf);
3652
3653 /* Need to set the filename for autocommands. */
3654 (void)setfname(curbuf, fname, NULL, FALSE);
3655
Bram Moolenaar81695252004-12-29 20:58:21 +00003656 /* Create swap file now to avoid the ATTENTION message. */
3657 check_need_swap(TRUE);
3658
3659 /* Remove the "dummy" flag, otherwise autocommands may not
3660 * work. */
3661 curbuf->b_flags &= ~BF_DUMMY;
3662
3663 if (readfile(fname, NULL,
3664 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
3665 NULL, READ_NEW | READ_DUMMY) == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00003666 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00003667 && !(curbuf->b_flags & BF_NEW))
3668 {
3669 failed = FALSE;
3670 if (curbuf != newbuf)
3671 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003672 /* Bloody autocommands changed the buffer! Can happen when
3673 * using netrw and editing a remote file. Use the current
3674 * buffer instead, delete the dummy one after restoring the
3675 * window stuff. */
3676 newbuf_to_wipe = newbuf;
Bram Moolenaar81695252004-12-29 20:58:21 +00003677 newbuf = curbuf;
3678 }
3679 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003680
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003681 /* restore curwin/curbuf and a few other things */
3682 aucmd_restbuf(&aco);
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01003683 if (newbuf_to_wipe != NULL && buf_valid(newbuf_to_wipe))
3684 wipe_buffer(newbuf_to_wipe, FALSE);
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003685 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003686
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003687 /*
3688 * When autocommands/'autochdir' option changed directory: go back.
3689 * Let the caller know what the resulting dir was first, in case it is
3690 * important.
3691 */
3692 mch_dirname(resulting_dir, MAXPATHL);
3693 restore_start_dir(dirname_start);
3694
Bram Moolenaar81695252004-12-29 20:58:21 +00003695 if (!buf_valid(newbuf))
3696 return NULL;
3697 if (failed)
3698 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003699 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00003700 return NULL;
3701 }
3702 return newbuf;
3703}
3704
3705/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003706 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
3707 * directory to "dirname_start" prior to returning, if autocmds or the
3708 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00003709 */
3710 static void
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003711wipe_dummy_buffer(buf, dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00003712 buf_T *buf;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003713 char_u *dirname_start;
Bram Moolenaar81695252004-12-29 20:58:21 +00003714{
3715 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003716 {
3717#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3718 cleanup_T cs;
3719
3720 /* Reset the error/interrupt/exception state here so that aborting()
3721 * returns FALSE when wiping out the buffer. Otherwise it doesn't
3722 * work when got_int is set. */
3723 enter_cleanup(&cs);
3724#endif
3725
Bram Moolenaar81695252004-12-29 20:58:21 +00003726 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00003727
3728#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
3729 /* Restore the error/interrupt/exception state if not discarded by a
3730 * new aborting error, interrupt, or uncaught exception. */
3731 leave_cleanup(&cs);
3732#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003733 /* When autocommands/'autochdir' option changed directory: go back. */
3734 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00003735 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003736}
3737
3738/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003739 * Unload the dummy buffer that load_dummy_buffer() created. Restores
3740 * directory to "dirname_start" prior to returning, if autocmds or the
3741 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00003742 */
3743 static void
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003744unload_dummy_buffer(buf, dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00003745 buf_T *buf;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003746 char_u *dirname_start;
Bram Moolenaar81695252004-12-29 20:58:21 +00003747{
3748 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003749 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003750 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02003751
3752 /* When autocommands/'autochdir' option changed directory: go back. */
3753 restore_start_dir(dirname_start);
3754 }
Bram Moolenaar81695252004-12-29 20:58:21 +00003755}
3756
Bram Moolenaar05159a02005-02-26 23:04:13 +00003757#if defined(FEAT_EVAL) || defined(PROTO)
3758/*
3759 * Add each quickfix error to list "list" as a dictionary.
3760 */
3761 int
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003762get_errorlist(wp, list)
3763 win_T *wp;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003764 list_T *list;
3765{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003766 qf_info_T *qi = &ql_info;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003767 dict_T *dict;
3768 char_u buf[2];
3769 qfline_T *qfp;
3770 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003771 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003772
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003773 if (wp != NULL)
3774 {
3775 qi = GET_LOC_LIST(wp);
3776 if (qi == NULL)
3777 return FAIL;
3778 }
3779
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003780 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003781 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003782 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003783
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003784 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3785 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003786 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003787 /* Handle entries with a non-existing buffer number. */
3788 bufnum = qfp->qf_fnum;
3789 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3790 bufnum = 0;
3791
Bram Moolenaar05159a02005-02-26 23:04:13 +00003792 if ((dict = dict_alloc()) == NULL)
3793 return FAIL;
3794 if (list_append_dict(list, dict) == FAIL)
3795 return FAIL;
3796
3797 buf[0] = qfp->qf_type;
3798 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003799 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003800 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
3801 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
3802 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
3803 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00003804 || dict_add_nr_str(dict, "pattern", 0L,
3805 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
3806 || dict_add_nr_str(dict, "text", 0L,
3807 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00003808 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
3809 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
3810 return FAIL;
3811
3812 qfp = qfp->qf_next;
3813 }
3814 return OK;
3815}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003816
3817/*
3818 * Populate the quickfix list with the items supplied in the list
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003819 * of dictionaries. "title" will be copied to w:quickfix_title
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003820 */
3821 int
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003822set_errorlist(wp, list, action, title)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003823 win_T *wp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003824 list_T *list;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003825 int action;
Bram Moolenaarbc226b62010-08-09 22:14:48 +02003826 char_u *title;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003827{
3828 listitem_T *li;
3829 dict_T *d;
3830 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003831 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003832 long lnum;
3833 int col, nr;
3834 int vcol;
3835 qfline_T *prevp = NULL;
3836 int valid, status;
3837 int retval = OK;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003838 qf_info_T *qi = &ql_info;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003839 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003840
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003841 if (wp != NULL)
3842 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00003843 qi = ll_get_or_alloc_list(wp);
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003844 if (qi == NULL)
3845 return FAIL;
3846 }
3847
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003848 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003849 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01003850 qf_new_list(qi, title);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003851 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003852 /* Adding to existing list, find last entry. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003853 for (prevp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00003854 prevp->qf_next != prevp; prevp = prevp->qf_next)
3855 ;
3856 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02003857 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003858 qf_free(qi, qi->qf_curlist);
Bram Moolenaarfb604092014-07-23 15:55:00 +02003859 qf_store_title(qi, title);
3860 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003861
3862 for (li = list->lv_first; li != NULL; li = li->li_next)
3863 {
3864 if (li->li_tv.v_type != VAR_DICT)
3865 continue; /* Skip non-dict items */
3866
3867 d = li->li_tv.vval.v_dict;
3868 if (d == NULL)
3869 continue;
3870
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003871 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003872 bufnum = get_dict_number(d, (char_u *)"bufnr");
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003873 lnum = get_dict_number(d, (char_u *)"lnum");
3874 col = get_dict_number(d, (char_u *)"col");
3875 vcol = get_dict_number(d, (char_u *)"vcol");
3876 nr = get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003877 type = get_dict_string(d, (char_u *)"type", TRUE);
3878 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
3879 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003880 if (text == NULL)
3881 text = vim_strsave((char_u *)"");
3882
3883 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003884 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003885 valid = FALSE;
3886
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003887 /* Mark entries with non-existing buffer number as not valid. Give the
3888 * error message only once. */
3889 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
3890 {
3891 if (!did_bufnr_emsg)
3892 {
3893 did_bufnr_emsg = TRUE;
3894 EMSGN(_("E92: Buffer %ld not found"), bufnum);
3895 }
3896 valid = FALSE;
3897 bufnum = 0;
3898 }
3899
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003900 status = qf_add_entry(qi, &prevp,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003901 NULL, /* dir */
3902 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00003903 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003904 text,
3905 lnum,
3906 col,
3907 vcol, /* vis_col */
3908 pattern, /* search pattern */
3909 nr,
3910 type == NULL ? NUL : *type,
3911 valid);
3912
3913 vim_free(filename);
3914 vim_free(pattern);
3915 vim_free(text);
3916 vim_free(type);
3917
3918 if (status == FAIL)
3919 {
3920 retval = FAIL;
3921 break;
3922 }
3923 }
3924
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02003925 if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02003926 /* no valid entry */
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02003927 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
3928 else
3929 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003930 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
3931 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003932
3933#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003934 qf_update_buffer(qi);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003935#endif
3936
3937 return retval;
3938}
Bram Moolenaar05159a02005-02-26 23:04:13 +00003939#endif
3940
Bram Moolenaar81695252004-12-29 20:58:21 +00003941/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003942 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003943 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003944 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003945 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00003946 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003947 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00003948 */
3949 void
3950ex_cbuffer(eap)
3951 exarg_T *eap;
3952{
3953 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003954 qf_info_T *qi = &ql_info;
3955
Bram Moolenaardb552d602006-03-23 22:59:57 +00003956 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
3957 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003958 {
3959 qi = ll_get_or_alloc_list(curwin);
3960 if (qi == NULL)
3961 return;
3962 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00003963
3964 if (*eap->arg == NUL)
3965 buf = curbuf;
3966 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
3967 buf = buflist_findnr(atoi((char *)eap->arg));
3968 if (buf == NULL)
3969 EMSG(_(e_invarg));
3970 else if (buf->b_ml.ml_mfp == NULL)
3971 EMSG(_("E681: Buffer is not loaded"));
3972 else
3973 {
3974 if (eap->addr_count == 0)
3975 {
3976 eap->line1 = 1;
3977 eap->line2 = buf->b_ml.ml_line_count;
3978 }
3979 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
3980 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
3981 EMSG(_(e_invrange));
3982 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00003983 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003984 char_u *qf_title = *eap->cmdlinep;
3985
3986 if (buf->b_sfname)
3987 {
3988 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
3989 (char *)qf_title, (char *)buf->b_sfname);
3990 qf_title = IObuff;
3991 }
3992
Bram Moolenaardb552d602006-03-23 22:59:57 +00003993 if (qf_init_ext(qi, NULL, buf, NULL, p_efm,
3994 (eap->cmdidx != CMD_caddbuffer
3995 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003996 eap->line1, eap->line2,
3997 qf_title) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00003998 && (eap->cmdidx == CMD_cbuffer
3999 || eap->cmdidx == CMD_lbuffer))
Bram Moolenaar754b5602006-02-09 23:53:20 +00004000 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
4001 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004002 }
4003}
4004
Bram Moolenaar1e015462005-09-25 22:16:38 +00004005#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004006/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00004007 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
4008 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004009 */
4010 void
4011ex_cexpr(eap)
4012 exarg_T *eap;
4013{
4014 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004015 qf_info_T *qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004016
Bram Moolenaardb552d602006-03-23 22:59:57 +00004017 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
4018 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004019 {
4020 qi = ll_get_or_alloc_list(curwin);
4021 if (qi == NULL)
4022 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004023 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004024
Bram Moolenaar4770d092006-01-12 23:22:24 +00004025 /* Evaluate the expression. When the result is a string or a list we can
4026 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004027 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004028 if (tv != NULL)
4029 {
4030 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
4031 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
4032 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00004033 if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
4034 (eap->cmdidx != CMD_caddexpr
4035 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004036 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0
Bram Moolenaardb552d602006-03-23 22:59:57 +00004037 && (eap->cmdidx == CMD_cexpr
4038 || eap->cmdidx == CMD_lexpr))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004039 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004040 }
4041 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004042 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004043 free_tv(tv);
4044 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004045}
Bram Moolenaar1e015462005-09-25 22:16:38 +00004046#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004047
4048/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 * ":helpgrep {pattern}"
4050 */
4051 void
4052ex_helpgrep(eap)
4053 exarg_T *eap;
4054{
4055 regmatch_T regmatch;
4056 char_u *save_cpo;
4057 char_u *p;
4058 int fcount;
4059 char_u **fnames;
4060 FILE *fd;
4061 int fi;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004062 qfline_T *prevp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004064#ifdef FEAT_MULTI_LANG
4065 char_u *lang;
4066#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004067 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004068 int new_qi = FALSE;
4069 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01004070#ifdef FEAT_AUTOCMD
4071 char_u *au_name = NULL;
4072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004074#ifdef FEAT_MULTI_LANG
4075 /* Check for a specified language */
4076 lang = check_help_lang(eap->arg);
4077#endif
4078
Bram Moolenaar73633f82012-01-20 13:39:07 +01004079#ifdef FEAT_AUTOCMD
4080 switch (eap->cmdidx)
4081 {
4082 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
4083 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
4084 default: break;
4085 }
4086 if (au_name != NULL)
4087 {
4088 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4089 curbuf->b_fname, TRUE, curbuf);
4090 if (did_throw || force_abort)
4091 return;
4092 }
4093#endif
4094
4095 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
4096 save_cpo = p_cpo;
4097 p_cpo = empty_option;
4098
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004099 if (eap->cmdidx == CMD_lhelpgrep)
4100 {
4101 /* Find an existing help window */
4102 FOR_ALL_WINDOWS(wp)
4103 if (wp->w_buffer != NULL && wp->w_buffer->b_help)
4104 break;
4105
4106 if (wp == NULL) /* Help window not found */
4107 qi = NULL;
4108 else
4109 qi = wp->w_llist;
4110
4111 if (qi == NULL)
4112 {
4113 /* Allocate a new location list for help text matches */
4114 if ((qi = ll_new_list()) == NULL)
4115 return;
4116 new_qi = TRUE;
4117 }
4118 }
4119
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
4121 regmatch.rm_ic = FALSE;
4122 if (regmatch.regprog != NULL)
4123 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004124#ifdef FEAT_MBYTE
4125 vimconv_T vc;
4126
4127 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
4128 * differs. */
4129 vc.vc_type = CONV_NONE;
4130 if (!enc_utf8)
4131 convert_setup(&vc, (char_u *)"utf-8", p_enc);
4132#endif
4133
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01004135 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136
4137 /* Go through all directories in 'runtimepath' */
4138 p = p_rtp;
4139 while (*p != NUL && !got_int)
4140 {
4141 copy_option_part(&p, NameBuff, MAXPATHL, ",");
4142
4143 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
4144 add_pathsep(NameBuff);
4145 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
4146 if (gen_expand_wildcards(1, &NameBuff, &fcount,
4147 &fnames, EW_FILE|EW_SILENT) == OK
4148 && fcount > 0)
4149 {
4150 for (fi = 0; fi < fcount && !got_int; ++fi)
4151 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004152#ifdef FEAT_MULTI_LANG
4153 /* Skip files for a different language. */
4154 if (lang != NULL
4155 && STRNICMP(lang, fnames[fi]
4156 + STRLEN(fnames[fi]) - 3, 2) != 0
4157 && !(STRNICMP(lang, "en", 2) == 0
4158 && STRNICMP("txt", fnames[fi]
4159 + STRLEN(fnames[fi]) - 3, 3) == 0))
4160 continue;
4161#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004162 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 if (fd != NULL)
4164 {
4165 lnum = 1;
4166 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
4167 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004168 char_u *line = IObuff;
4169#ifdef FEAT_MBYTE
4170 /* Convert a line if 'encoding' is not utf-8 and
4171 * the line contains a non-ASCII character. */
4172 if (vc.vc_type != CONV_NONE
4173 && has_non_ascii(IObuff)) {
4174 line = string_convert(&vc, IObuff, NULL);
4175 if (line == NULL)
4176 line = IObuff;
4177 }
4178#endif
4179
4180 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004182 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183
4184 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004185 while (l > 0 && line[l - 1] <= ' ')
4186 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004188 if (qf_add_entry(qi, &prevp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 NULL, /* dir */
4190 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004191 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004192 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004194 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00004195 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004196 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004197 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 0, /* nr */
4199 1, /* type */
4200 TRUE /* valid */
4201 ) == FAIL)
4202 {
4203 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004204#ifdef FEAT_MBYTE
4205 if (line != IObuff)
4206 vim_free(line);
4207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 break;
4209 }
4210 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004211#ifdef FEAT_MBYTE
4212 if (line != IObuff)
4213 vim_free(line);
4214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 ++lnum;
4216 line_breakcheck();
4217 }
4218 fclose(fd);
4219 }
4220 }
4221 FreeWild(fcount, fnames);
4222 }
4223 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004224
Bram Moolenaar473de612013-06-08 18:19:48 +02004225 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01004226#ifdef FEAT_MBYTE
4227 if (vc.vc_type != CONV_NONE)
4228 convert_setup(&vc, NULL, NULL);
4229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004231 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4232 qi->qf_lists[qi->qf_curlist].qf_ptr =
4233 qi->qf_lists[qi->qf_curlist].qf_start;
4234 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 }
4236
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00004237 if (p_cpo == empty_option)
4238 p_cpo = save_cpo;
4239 else
4240 /* Darn, some plugin changed the value. */
4241 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242
4243#ifdef FEAT_WINDOWS
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004244 qf_update_buffer(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245#endif
4246
Bram Moolenaar73633f82012-01-20 13:39:07 +01004247#ifdef FEAT_AUTOCMD
4248 if (au_name != NULL)
4249 {
4250 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4251 curbuf->b_fname, TRUE, curbuf);
4252 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
4253 /* autocommands made "qi" invalid */
4254 return;
4255 }
4256#endif
4257
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004259 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004260 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004261 else
4262 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004263
4264 if (eap->cmdidx == CMD_lhelpgrep)
4265 {
4266 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00004267 * correct location list, then free the new location list. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004268 if (!curwin->w_buffer->b_help || curwin->w_llist == qi)
4269 {
4270 if (new_qi)
4271 ll_free_all(&qi);
4272 }
4273 else if (curwin->w_llist == NULL)
4274 curwin->w_llist = qi;
4275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276}
4277
4278#endif /* FEAT_QUICKFIX */